Skip to content

Commit 882a69c

Browse files
vincentkocljy-1351
andcommitted
fix(plugins): keep claiming hook timeouts explicit
Co-authored-by: ljy-1351 <[email protected]>
1 parent 4a19f39 commit 882a69c

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎src/plugins/hooks.before-agent-reply.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ describe("before_agent_reply hook runner (claiming pattern)", () => {
9494
expect(result).toBeUndefined();
9595
});
9696

97+
it("does not inherit modifying hook timeout defaults", async () => {
98+
vi.useFakeTimers();
99+
try {
100+
const handler = vi.fn(async () => {
101+
await new Promise((resolve) => {
102+
setTimeout(resolve, 100);
103+
});
104+
return { handled: true };
105+
});
106+
const registry = createMockPluginRegistry([{ hookName: "before_agent_reply", handler }]);
107+
const runner = createHookRunner(registry, {
108+
modifyingHookTimeoutMsByHook: { before_agent_reply: 10 },
109+
});
110+
111+
const resultPromise = runner.runBeforeAgentReply(EVENT, TEST_PLUGIN_AGENT_CTX);
112+
await vi.advanceTimersByTimeAsync(100);
113+
114+
await expect(resultPromise).resolves.toEqual({ handled: true });
115+
expect(handler).toHaveBeenCalledTimes(1);
116+
} finally {
117+
vi.useRealTimers();
118+
}
119+
});
120+
97121
it("catches errors with catchErrors: true and continues to next handler", async () => {
98122
const logger = { warn: vi.fn(), error: vi.fn() };
99123
const failing = vi.fn().mockRejectedValue(new Error("boom"));

‎src/plugins/hooks.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,8 @@ export function createHookRunner(
524524
normalizePositiveTimeoutMs(hook.timeoutMs) ??
525525
normalizePositiveTimeoutMs(modifyingHookTimeoutMsByHook[hookName]);
526526

527-
const getClaimingHookTimeoutMs = (
528-
hookName: PluginHookName,
529-
hook: PluginHookRegistration,
530-
): number | undefined =>
531-
normalizePositiveTimeoutMs(hook.timeoutMs) ??
532-
normalizePositiveTimeoutMs(modifyingHookTimeoutMsByHook[hookName]);
527+
const getClaimingHookTimeoutMs = (hook: PluginHookRegistration): number | undefined =>
528+
normalizePositiveTimeoutMs(hook.timeoutMs);
533529

534530
const withHookTimeout = async <T>(
535531
promise: Promise<T>,
@@ -711,7 +707,7 @@ export function createHookRunner(
711707
const promise = Promise.resolve(
712708
(hook.handler as (event: unknown, ctx: unknown) => Promise<TResult | void>)(event, ctx),
713709
);
714-
const timeoutMs = getClaimingHookTimeoutMs(hookName, hook);
710+
const timeoutMs = getClaimingHookTimeoutMs(hook);
715711
const handlerResult = timeoutMs ? await withHookTimeout(promise, timeoutMs) : await promise;
716712
if (handlerResult?.handled) {
717713
return handlerResult;
@@ -762,7 +758,7 @@ export function createHookRunner(
762758
const promise = Promise.resolve(
763759
(hook.handler as (event: unknown, ctx: unknown) => Promise<TResult | void>)(event, ctx),
764760
);
765-
const timeoutMs = getClaimingHookTimeoutMs(hookName, hook);
761+
const timeoutMs = getClaimingHookTimeoutMs(hook);
766762
const handlerResult = timeoutMs ? await withHookTimeout(promise, timeoutMs) : await promise;
767763
if (handlerResult?.handled) {
768764
return { status: "handled", result: handlerResult };

0 commit comments

Comments
 (0)