fix: initialize postHookActions before handler loop, not just at drain

Move postHookActions ??= [] to before the handler loop so handlers
can safely push() during execution. Legacy callers without the field
would otherwise TypeError on .push() inside the session-memory handler.
Removed the now-redundant ?? [] at drain time.
This commit is contained in:
zeroaltitude 2026-03-10 00:21:30 -07:00
parent c0792e5376
commit 79c06d7160
No known key found for this signature in database
GPG Key ID: 77592FB1C703882E

View File

@ -274,6 +274,11 @@ export function getRegisteredEventKeys(): string[] {
* @param event - The event to trigger
*/
export async function triggerInternalHook(event: InternalHookEvent): Promise<void> {
// Normalize postHookActions before entering the handler loop — handlers
// may push to this array during execution. Legacy callers that construct
// hook events without the field would otherwise hit a TypeError on .push().
event.postHookActions ??= [];
const typeHandlers = handlers.get(event.type) ?? [];
const specificHandlers = handlers.get(`${event.type}:${event.action}`) ?? [];
@ -297,10 +302,7 @@ export async function triggerInternalHook(event: InternalHookEvent): Promise<voi
// callbacks do not execute in this drain cycle. Without this, a self-
// scheduling action (one that pushes another action) could loop infinitely
// because Array's for...of iterator is live and re-reads length each step.
// Default to empty array for legacy callers that construct hook events
// without the postHookActions field (pre-dates this PR's event shape change).
const actions = event.postHookActions ?? [];
await drainPostHookActions(actions, (err) => {
await drainPostHookActions(event.postHookActions, (err) => {
const message = err instanceof Error ? err.message : String(err);
log.error(`Post-hook action error [${event.type}:${event.action}]: ${message}`);
});