Skip to content

Commit 3869538

Browse files
committed
fix(plugins): revalidate prompt authority per handler
1 parent eebfd36 commit 3869538

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎src/plugins/hooks.phase-hooks.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,48 @@ describe("phase hooks merger", () => {
329329

330330
await expect(run).rejects.toThrow("host turn authority is no longer active");
331331
});
332+
333+
it("does not start a later authorized handler after host authority closes", async () => {
334+
let hostActive = true;
335+
const firstEnrichment = vi.fn(async () => {
336+
hostActive = false;
337+
return { prependContext: "first context" };
338+
});
339+
const laterEnrichment = vi.fn(() => ({ prependContext: "stale later context" }));
340+
registry.typedHooks.push(
341+
{
342+
pluginId: "first-enricher",
343+
hookName: "before_prompt_build",
344+
handler: firstEnrichment,
345+
requiresToolAuthority: true,
346+
source: "test",
347+
},
348+
{
349+
pluginId: "later-enricher",
350+
hookName: "before_prompt_build",
351+
handler: laterEnrichment,
352+
requiresToolAuthority: true,
353+
source: "test",
354+
},
355+
);
356+
const runner = createHookRunner(registry);
357+
358+
await expect(
359+
runner.runAuthorizedPromptBuild(
360+
{ prompt: "test", messages: [] },
361+
{},
362+
{
363+
toolAuthorityFingerprint: "turn-authority",
364+
activeToolNames: ["memory_search"],
365+
assertHostActive: () => {
366+
if (!hostActive) {
367+
throw new Error("host turn authority is no longer active");
368+
}
369+
},
370+
},
371+
),
372+
).rejects.toThrow("host turn authority is no longer active");
373+
expect(firstEnrichment).toHaveBeenCalledOnce();
374+
expect(laterEnrichment).not.toHaveBeenCalled();
375+
});
332376
});

‎src/plugins/hooks.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ type ModifyingHookPolicy<K extends PluginHookName, TResult> = {
226226
terminalLabel?: string;
227227
onTerminal?: (params: { hookName: K; pluginId: string; result: TResult }) => void;
228228
includeRegistration?: (registration: PluginHookRegistration<K>) => boolean;
229+
assertHandlerBoundaryActive?: () => void;
229230
};
230231

231232
type PluginTargetedInboundClaimOutcome =
@@ -728,6 +729,8 @@ export function createHookRunner(
728729
let result: TResult | undefined;
729730

730731
for (const hook of selectedHooks) {
732+
policy.assertHandlerBoundaryActive?.();
733+
let shouldStop = false;
731734
try {
732735
const handler = hook.handler as (event: unknown, ctx: unknown) => Promise<TResult>;
733736
const handlerEvent = policy.isolateEventPerHandler
@@ -752,7 +755,7 @@ export function createHookRunner(
752755
`[hooks] ${hookName}${terminalLabel} decided by ${hook.pluginId} (priority=${priority}); skipping remaining handlers`,
753756
);
754757
policy.onTerminal?.({ hookName, pluginId: hook.pluginId, result });
755-
break;
758+
shouldStop = true;
756759
}
757760
}
758761
} catch (err) {
@@ -761,6 +764,10 @@ export function createHookRunner(
761764
}
762765
handleHookError({ hookName, pluginId: hook.pluginId, error: err });
763766
}
767+
policy.assertHandlerBoundaryActive?.();
768+
if (shouldStop) {
769+
break;
770+
}
764771
}
765772

766773
return result;
@@ -989,7 +996,6 @@ export function createHookRunner(
989996
assertActive,
990997
});
991998
try {
992-
assertActive();
993999
const result = await runModifyingHook<
9941000
"before_prompt_build",
9951001
PluginHookBeforePromptBuildResult
@@ -1000,9 +1006,9 @@ export function createHookRunner(
10001006
{
10011007
mergeResults: mergeBeforePromptBuild,
10021008
includeRegistration: (registration) => registration.requiresToolAuthority === true,
1009+
assertHandlerBoundaryActive: assertActive,
10031010
},
10041011
);
1005-
assertActive();
10061012
if (!result) {
10071013
return undefined;
10081014
}

0 commit comments

Comments
 (0)