Skip to content

Commit 4e1d6ca

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

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
@@ -541,12 +541,8 @@ export function createHookRunner(
541541
normalizePositiveTimeoutMs(hook.timeoutMs) ??
542542
normalizePositiveTimeoutMs(modifyingHookTimeoutMsByHook[hookName]);
543543

544-
const getClaimingHookTimeoutMs = (
545-
hookName: PluginHookName,
546-
hook: PluginHookRegistration,
547-
): number | undefined =>
548-
normalizePositiveTimeoutMs(hook.timeoutMs) ??
549-
normalizePositiveTimeoutMs(modifyingHookTimeoutMsByHook[hookName]);
544+
const getClaimingHookTimeoutMs = (hook: PluginHookRegistration): number | undefined =>
545+
normalizePositiveTimeoutMs(hook.timeoutMs);
550546

551547
const withHookTimeout = async <T>(
552548
promise: Promise<T>,
@@ -728,7 +724,7 @@ export function createHookRunner(
728724
const promise = Promise.resolve(
729725
(hook.handler as (event: unknown, ctx: unknown) => Promise<TResult | void>)(event, ctx),
730726
);
731-
const timeoutMs = getClaimingHookTimeoutMs(hookName, hook);
727+
const timeoutMs = getClaimingHookTimeoutMs(hook);
732728
const handlerResult = timeoutMs ? await withHookTimeout(promise, timeoutMs) : await promise;
733729
if (handlerResult?.handled) {
734730
return handlerResult;
@@ -779,7 +775,7 @@ export function createHookRunner(
779775
const promise = Promise.resolve(
780776
(hook.handler as (event: unknown, ctx: unknown) => Promise<TResult | void>)(event, ctx),
781777
);
782-
const timeoutMs = getClaimingHookTimeoutMs(hookName, hook);
778+
const timeoutMs = getClaimingHookTimeoutMs(hook);
783779
const handlerResult = timeoutMs ? await withHookTimeout(promise, timeoutMs) : await promise;
784780
if (handlerResult?.handled) {
785781
return { status: "handled", result: handlerResult };

0 commit comments

Comments
 (0)