|
| 1 | +// Talk agent consult dispatch tests pin the trusted internal chat.send |
| 2 | +// contract: synthetic consult input must never persist as a visible |
| 3 | +// user-authored turn (see issue #133855). |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +const dispatchCalls = vi.hoisted(() => ({ |
| 7 | + trustedInternal: vi.fn(), |
| 8 | + trustedInternalWithTools: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("./server-methods/chat-send-handler.js", () => ({ |
| 12 | + handleChatSend: vi.fn(), |
| 13 | + handleChatSendWithRuntimeTools: vi.fn(), |
| 14 | + handleTrustedInternalChatSend: dispatchCalls.trustedInternal, |
| 15 | + handleTrustedInternalChatSendWithRuntimeTools: dispatchCalls.trustedInternalWithTools, |
| 16 | +})); |
| 17 | + |
| 18 | +import { startTalkRealtimeAgentConsult } from "./talk-agent-consult.js"; |
| 19 | + |
| 20 | +type CapturedChatSendOptions = { |
| 21 | + params: Record<string, unknown>; |
| 22 | + respond: (ok: boolean, result?: unknown, error?: unknown) => void; |
| 23 | +}; |
| 24 | + |
| 25 | +function captureDispatch(impl: (options: CapturedChatSendOptions) => void) { |
| 26 | + return (options: CapturedChatSendOptions) => { |
| 27 | + impl(options); |
| 28 | + options.respond(true, { status: "started", runId: "run-consult-1" }); |
| 29 | + return Promise.resolve(); |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function consultParams() { |
| 34 | + return { |
| 35 | + context: { |
| 36 | + getRuntimeConfig: () => ({}), |
| 37 | + logGateway: { warn: vi.fn() }, |
| 38 | + }, |
| 39 | + client: null, |
| 40 | + isWebchatConnect: () => false, |
| 41 | + requestId: "req-1", |
| 42 | + sessionKey: "agent:main:main", |
| 43 | + callId: "call-1", |
| 44 | + args: { |
| 45 | + question: "What changes were made in the Dockerfile to fix it?", |
| 46 | + responseStyle: "concise spoken summary", |
| 47 | + }, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +describe("startTalkRealtimeAgentConsult dispatch", () => { |
| 52 | + beforeEach(() => { |
| 53 | + dispatchCalls.trustedInternal.mockReset(); |
| 54 | + dispatchCalls.trustedInternalWithTools.mockReset(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("dispatches through the trusted internal channel with a hidden system turn", async () => { |
| 58 | + let captured: CapturedChatSendOptions | undefined; |
| 59 | + dispatchCalls.trustedInternal.mockImplementation( |
| 60 | + captureDispatch((options) => { |
| 61 | + captured = options; |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + const result = await startTalkRealtimeAgentConsult({ |
| 66 | + ...consultParams(), |
| 67 | + client: { connect: { scopes: ["operator.admin"] } } as never, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(result).toMatchObject({ ok: true, runId: "run-consult-1" }); |
| 71 | + expect(dispatchCalls.trustedInternal).toHaveBeenCalledTimes(1); |
| 72 | + expect(dispatchCalls.trustedInternalWithTools).not.toHaveBeenCalled(); |
| 73 | + expect(captured).toBeDefined(); |
| 74 | + const params = captured!.params; |
| 75 | + expect(String(params.message).startsWith("[System] ")).toBe(true); |
| 76 | + expect(params.deliver).toBe(false); |
| 77 | + expect(params.suppressCommandInterpretation).toBe(true); |
| 78 | + expect(params.systemInputProvenance).toEqual({ |
| 79 | + kind: "internal_system", |
| 80 | + sourceSessionKey: "agent:main:main", |
| 81 | + sourceTool: "talk.agentConsult", |
| 82 | + }); |
| 83 | + // The structured consult input must survive flattening. |
| 84 | + expect(params.message).toContain("What changes were made in the Dockerfile to fix it?"); |
| 85 | + expect(params.message).toContain("concise spoken summary"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("carries the Talk caller's resolved tool boundary on the trusted internal dispatch", async () => { |
| 89 | + dispatchCalls.trustedInternalWithTools.mockImplementation(captureDispatch(() => {})); |
| 90 | + |
| 91 | + const result = await startTalkRealtimeAgentConsult({ |
| 92 | + ...consultParams(), |
| 93 | + client: { |
| 94 | + connect: { scopes: ["operator.read"] }, |
| 95 | + } as never, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result).toMatchObject({ ok: true, runId: "run-consult-1" }); |
| 99 | + expect(dispatchCalls.trustedInternalWithTools).toHaveBeenCalledTimes(1); |
| 100 | + expect(dispatchCalls.trustedInternal).not.toHaveBeenCalled(); |
| 101 | + const toolsAllow = dispatchCalls.trustedInternalWithTools.mock.calls[0][1] as string[]; |
| 102 | + expect(toolsAllow).toContain("memory_search"); |
| 103 | + expect(toolsAllow).not.toContain("exec"); |
| 104 | + }); |
| 105 | +}); |
0 commit comments