





















@@ -20,6 +20,7 @@ installGatewayTestHooks({ scope: "suite" });
20202121const resolveMainKey = () => resolveMainSessionKeyFromConfig();
2222const HOOK_TOKEN = "hook-secret";
23+const HOOKS_MAIN_SESSION_KEY = "agent:hooks:main";
23242425afterEach(() => {
2526vi.restoreAllMocks();
@@ -117,14 +118,24 @@ async function expectHookAgentSessionRouting(params: {
117118sessionKey: params.requestSessionKey,
118119});
119120expect(resAgent.status).toBe(200);
120-await waitForSystemEvent();
121+await waitForSystemEventTexts(HOOKS_MAIN_SESSION_KEY);
121122122123const routedCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as
123124| { sessionKey?: string; job?: { agentId?: string } }
124125| undefined;
125126expect(routedCall?.job?.agentId).toBe("hooks");
126127expect(routedCall?.sessionKey).toBe(params.expectedSessionKey);
127-drainSystemEvents(resolveMainKey());
128+drainSystemEvents(HOOKS_MAIN_SESSION_KEY);
129+}
130+131+async function waitForSystemEventTexts(sessionKey: string, timeoutMs = 2_000) {
132+await expect
133+.poll(() => peekSystemEventEntries(sessionKey).map((event) => event.text), {
134+timeout: timeoutMs,
135+interval: 10,
136+})
137+.not.toHaveLength(0);
138+return peekSystemEventEntries(sessionKey).map((event) => event.text);
128139}
129140130141async function writeHookTransformModule(moduleName: string, source: string): Promise<void> {
@@ -181,12 +192,12 @@ describe("gateway server hooks", () => {
181192agentId: "hooks",
182193});
183194expect(resAgentWithId.status).toBe(200);
184-await waitForSystemEvent();
195+await waitForSystemEventTexts(HOOKS_MAIN_SESSION_KEY);
185196const routedCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
186197job?: { agentId?: string };
187198};
188199expect(routedCall?.job?.agentId).toBe("hooks");
189-drainSystemEvents(resolveMainKey());
200+drainSystemEvents(HOOKS_MAIN_SESSION_KEY);
190201191202mockIsolatedRunOkOnce();
192203const resAgentUnknown = await postHook(port, "/hooks/agent", {
@@ -281,6 +292,26 @@ describe("gateway server hooks", () => {
281292});
282293});
283294295+test("routes explicit-agent hook completion events to the target agent main session", async () => {
296+testState.hooksConfig = { enabled: true, token: HOOK_TOKEN };
297+setMainAndHooksAgents();
298+299+await withGatewayServer(async ({ port }) => {
300+mockIsolatedRunOkOnce();
301+const resAgent = await postHook(port, "/hooks/agent", {
302+message: "Do it",
303+name: "Email",
304+agentId: "hooks",
305+});
306+expect(resAgent.status).toBe(200);
307+308+const targetEvents = await waitForSystemEventTexts(HOOKS_MAIN_SESSION_KEY);
309+expect(targetEvents.some((event) => event.includes("Hook Email: done"))).toBe(true);
310+expect(peekSystemEventEntries(resolveMainKey())).toEqual([]);
311+drainSystemEvents(HOOKS_MAIN_SESSION_KEY);
312+});
313+});
314+284315test("queues direct and mapped wake payloads as untrusted system events", async () => {
285316testState.hooksConfig = {
286317enabled: true,
@@ -700,12 +731,12 @@ describe("gateway server hooks", () => {
700731agentId: "hooks",
701732});
702733expect(resAllowed.status).toBe(200);
703-await waitForSystemEvent();
734+await waitForSystemEventTexts(HOOKS_MAIN_SESSION_KEY);
704735const allowedCall = (cronIsolatedRun.mock.calls[0] as unknown[] | undefined)?.[0] as {
705736job?: { agentId?: string };
706737};
707738expect(allowedCall?.job?.agentId).toBe("hooks");
708-drainSystemEvents(resolveMainKey());
739+drainSystemEvents(HOOKS_MAIN_SESSION_KEY);
709740710741const resDenied = await postHook(port, "/hooks/agent", {
711742message: "Denied",
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。