fix: drain postHookActions even with no registered handlers

Remove early return in triggerInternalHook that skipped the post-hook
drain when allHandlers was empty. This was a latent footgun: any
postHookActions pre-populated on the event before calling
triggerInternalHook would be silently dropped if no handlers were
registered for that event type.

Update test to verify post-hooks now drain in the no-handlers case.
This commit is contained in:
zeroaltitude 2026-03-07 10:16:02 -07:00
parent 4040a25bb1
commit 51fdf17867
No known key found for this signature in database
GPG Key ID: 77592FB1C703882E
2 changed files with 6 additions and 8 deletions

View File

@ -529,13 +529,15 @@ describe("hooks", () => {
expect(event.postHookActions).toEqual([]);
});
it("does not run post-hook actions when no handlers are registered", async () => {
it("drains post-hook actions even when no handlers are registered", async () => {
const event = createInternalHookEvent("command", "new", "test-session");
let ran = false;
event.postHookActions.push(() => {
throw new Error("should not run");
ran = true;
});
// triggerInternalHook returns early when no handlers — post-hooks don't run
await expect(triggerInternalHook(event)).resolves.not.toThrow();
// No handlers registered — post-hooks should still drain
await triggerInternalHook(event);
expect(ran).toBe(true);
});
});

View File

@ -279,10 +279,6 @@ export async function triggerInternalHook(event: InternalHookEvent): Promise<voi
const allHandlers = [...typeHandlers, ...specificHandlers];
if (allHandlers.length === 0) {
return;
}
for (const handler of allHandlers) {
try {
await handler(event);