Skip to content

Commit 351a89d

Browse files
committed
fix(gateway): keep Talk agent consult input out of user chat history
Browser Talk agent consults submitted their flattened orchestration input through the ordinary chat.send path, persisting a synthetic user-authored turn alongside the real spoken transcript. Dispatch consults through the trusted internal channel instead: system-turn formatted message, deliver: false, suppressed command interpretation, and internal_system provenance, plus a new hideFromDisplay internal option that persists the turn with display: false so the history projection, exports, and compaction all exclude it while the delegated agent still receives the full structured consult input. Fixes #133855
1 parent 12f2f9c commit 351a89d

7 files changed

Lines changed: 171 additions & 6 deletions

File tree

‎src/gateway/server-methods/chat-send-handler.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type ChatSendInternalOptions = {
5656
trustedSystemInput?: boolean;
5757
toolsAllow?: string[];
5858
skillWorkshopProposalRevision?: SkillWorkshopProposalRevisionConstraint;
59+
hideFromDisplay?: boolean;
5960
};
6061

6162
async function handleChatSendWithOptions(
@@ -535,3 +536,21 @@ export async function handleTrustedInternalChatSend(
535536
trustedSystemInput: true,
536537
});
537538
}
539+
540+
/**
541+
* Dispatches an internally delegated turn within its caller-owned tool boundary
542+
* while keeping the synthetic input out of user-visible history, exports, and
543+
* compaction views. Used by realtime voice agent consults, whose structured
544+
* orchestration input must not persist as a user-authored chat turn.
545+
*/
546+
export async function handleTrustedInternalChatSendWithRuntimeTools(
547+
options: GatewayRequestHandlerOptions,
548+
toolsAllow: string[],
549+
onAdmissionOwned?: () => Promise<boolean>,
550+
): Promise<void> {
551+
await handleChatSendWithOptions(options, onAdmissionOwned, undefined, {
552+
trustedSystemInput: true,
553+
toolsAllow,
554+
hideFromDisplay: true,
555+
});
556+
}

‎src/gateway/server-methods/chat-send-request.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,24 @@ describe("normalizeChatSendRequest", () => {
241241
).toEqual({ ok: false, error: "run tool bindings require a paired browser copilot" });
242242
});
243243
});
244+
245+
describe("normalizeChatSendRequest hideFromDisplay", () => {
246+
it("propagates hideFromDisplay for trusted internal dispatches", () => {
247+
expect(
248+
normalizeChatSendRequest({
249+
params: validParams(),
250+
client: null,
251+
trustedSystemInput: true,
252+
hideFromDisplay: true,
253+
}),
254+
).toMatchObject({ ok: true, value: { hideFromDisplay: true } });
255+
});
256+
257+
it("leaves hideFromDisplay unset for ordinary sends", () => {
258+
const result = normalizeChatSendRequest({ params: validParams(), client: null });
259+
expect(result).toMatchObject({ ok: true });
260+
expect((result as { value: { hideFromDisplay?: boolean } }).value.hideFromDisplay).toBe(
261+
undefined,
262+
);
263+
});
264+
});

‎src/gateway/server-methods/chat-send-request.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export type NormalizedChatSendRequest = {
8181
normalizedAttachments: ChatAttachment[];
8282
rawMessage: string;
8383
reconnectResumeRequested: boolean;
84+
hideFromDisplay?: boolean;
8485
};
8586

8687
type NormalizeChatSendRequestResult =
@@ -93,6 +94,7 @@ export function normalizeChatSendRequest(params: {
9394
client: GatewayRequestHandlerOptions["client"];
9495
trustedSystemInput?: boolean;
9596
goalResume?: SessionGoalOperation & { action: "resume" };
97+
hideFromDisplay?: boolean;
9698
}): NormalizeChatSendRequestResult {
9799
const chatSendReceivedAtMs = performance.now();
98100
const client = params.client;
@@ -248,6 +250,7 @@ export function normalizeChatSendRequest(params: {
248250
normalizedAttachments,
249251
rawMessage,
250252
reconnectResumeRequested: controlUiReconnectResume.resumeRequested,
253+
...(params.hideFromDisplay ? { hideFromDisplay: true } : {}),
251254
},
252255
};
253256
}

‎src/gateway/server-methods/chat-send-setup.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ export async function prepareAndAdmitChatSend(
2222
options?: {
2323
trustedSystemInput?: boolean;
2424
goalResume?: SessionGoalOperation & { action: "resume" };
25+
hideFromDisplay?: boolean;
2526
},
2627
) {
2728
const normalizedRequest = normalizeChatSendRequest({
2829
params,
2930
client,
3031
...(options?.trustedSystemInput ? { trustedSystemInput: true } : {}),
3132
...(options?.goalResume ? { goalResume: options.goalResume } : {}),
33+
...(options?.hideFromDisplay ? { hideFromDisplay: true } : {}),
3234
});
3335
if (!normalizedRequest.ok) {
3436
respond(

‎src/gateway/server-methods/chat-user-turn-recorder.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export function createGatewayChatUserTurnController(params: {
4444
? undefined
4545
: gatewayClientSenderFields(params.client).sender;
4646
const baseInput: UserTurnInput = {
47-
...(request.goalOperation?.action === "resume" ? { display: false } : {}),
47+
...(request.goalOperation?.action === "resume" || request.hideFromDisplay
48+
? { display: false }
49+
: {}),
4850
text: request.rawMessage,
4951
timestamp: session.now,
5052
idempotencyKey: buildRunUserTurnIdempotencyKey(session.clientRunId),
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
});

‎src/gateway/talk-agent-consult.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
type ErrorShape,
99
} from "../../packages/gateway-protocol/src/index.js";
1010
import { normalizeTalkSection } from "../config/talk.js";
11+
import { formatSystemTurnPrompt } from "../sessions/system-turn-prompt.js";
1112
import { buildRealtimeVoiceAgentConsultChatMessage } from "../talk/agent-consult-tool.js";
1213
import { abortChatRunById } from "./chat-abort.js";
1314
import {
14-
handleChatSend,
15-
handleChatSendWithRuntimeTools,
15+
handleTrustedInternalChatSend,
16+
handleTrustedInternalChatSendWithRuntimeTools,
1617
} from "./server-methods/chat-send-handler.js";
1718
import type {
1819
GatewayClient,
@@ -99,8 +100,18 @@ export async function startTalkRealtimeAgentConsult(params: {
99100
context: params.context,
100101
params: {
101102
sessionKey: params.sessionKey,
102-
message,
103+
// The consult is an internal delegated run: format it as a system turn
104+
// and keep the synthetic orchestration text out of user-visible
105+
// history via the trusted internal dispatch below.
106+
message: formatSystemTurnPrompt(message),
103107
idempotencyKey,
108+
deliver: false,
109+
suppressCommandInterpretation: true,
110+
systemInputProvenance: {
111+
kind: "internal_system",
112+
sourceSessionKey: params.sessionKey,
113+
sourceTool: "talk.agentConsult",
114+
},
104115
...(normalizedTalk?.consultThinkingLevel
105116
? { thinking: normalizedTalk.consultThinkingLevel }
106117
: {}),
@@ -154,10 +165,12 @@ export async function startTalkRealtimeAgentConsult(params: {
154165
} as GatewayRequestHandlerOptions;
155166
// talk.client.toolCall enters below the normal chat.send scope gate, so its
156167
// delegated run must carry the Talk caller's already-resolved tool boundary.
168+
// Trusted internal dispatch keeps the synthetic consult input from being
169+
// persisted as a user-authored turn in history, exports, and compaction.
157170
const chatSendResult =
158171
authority.toolsAllow !== undefined
159-
? handleChatSendWithRuntimeTools(chatSendOptions, authority.toolsAllow)
160-
: handleChatSend(chatSendOptions);
172+
? handleTrustedInternalChatSendWithRuntimeTools(chatSendOptions, authority.toolsAllow)
173+
: handleTrustedInternalChatSend(chatSendOptions);
161174
void Promise.resolve(chatSendResult).then(
162175
() => {
163176
if (!acknowledged) {

0 commit comments

Comments
 (0)