Skip to content

Commit 8b1a9cc

Browse files
authored
fix(voice-call): preserve tool invocation context (#116856)
* fix(voice-call): preserve tool invocation context * fix(voice-call): trust requester tool context * style(voice-call): format invocation context test
1 parent 4fb19fb commit 8b1a9cc

3 files changed

Lines changed: 63 additions & 39 deletions

File tree

‎extensions/voice-call/index.test.ts‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -758,23 +758,41 @@ describe("voice-call plugin", () => {
758758
expect(error?.message).not.toContain("endedAt=");
759759
});
760760

761-
it("freezes the invoking agent on tool-created calls", async () => {
762-
const { tools } = setup({ provider: "mock" }, { agentId: "support" });
763-
const tool = tools[0] as {
764-
execute: (id: string, params: unknown) => Promise<unknown>;
765-
};
761+
it.each([{ action: "initiate_call", message: "Hello" }, { message: "Hello" }])(
762+
"freezes invocation context for tool-created calls ($action)",
763+
async (params) => {
764+
const { tools } = setup(
765+
{ provider: "mock" },
766+
{ agentId: "support", sessionKey: "agent:support:discord:channel:general" },
767+
);
768+
const tool = tools[0] as {
769+
execute: (id: string, params: unknown) => Promise<unknown>;
770+
};
766771

767-
await tool.execute("id", {
768-
action: "initiate_call",
769-
to: "+15550001234",
770-
message: "Hello",
771-
});
772+
await tool.execute("id", {
773+
...params,
774+
to: "+15550001234",
775+
requesterSessionKey: "agent:spoofed:requester",
776+
sessionKey: "agent:support:voice:call-1",
777+
});
772778

773-
expect(runtimeStub.manager["initiateCall"]).toHaveBeenCalledWith(
774-
"+15550001234",
775-
undefined,
776-
expect.objectContaining({ agentId: "support", message: "Hello" }),
777-
);
779+
expect(runtimeStub.manager["initiateCall"]).toHaveBeenCalledWith(
780+
"+15550001234",
781+
"agent:support:voice:call-1",
782+
expect.objectContaining({
783+
agentId: "support",
784+
message: "Hello",
785+
requesterSessionKey: "agent:support:discord:channel:general",
786+
}),
787+
);
788+
},
789+
);
790+
791+
it("does not expose requester session identity to the model", () => {
792+
const { tools } = setup({ provider: "mock" });
793+
const tool = tools[0] as { parameters: unknown };
794+
795+
expect(JSON.stringify(tool.parameters)).not.toContain("requesterSessionKey");
778796
});
779797

780798
it("tool get_status returns json payload", async () => {

‎extensions/voice-call/index.ts‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
33
import { ErrorCodes, errorShape } from "openclaw/plugin-sdk/gateway-runtime";
44
import { timestampMsToIsoString } from "openclaw/plugin-sdk/number-runtime";
5-
import { normalizeAgentId } from "openclaw/plugin-sdk/routing";
5+
import { normalizeAgentId, parseAgentSessionKey } from "openclaw/plugin-sdk/routing";
66
import {
77
asOptionalRecord,
88
normalizeOptionalString,
@@ -189,9 +189,6 @@ const VoiceCallToolSchema = Type.Union([
189189
message: Type.String({ description: "Intro message" }),
190190
mode: Type.Optional(Type.Union([Type.Literal("notify"), Type.Literal("conversation")])),
191191
sessionKey: Type.Optional(Type.String({ description: "OpenClaw session key for the call" })),
192-
requesterSessionKey: Type.Optional(
193-
Type.String({ description: "OpenClaw session key that initiated the call" }),
194-
),
195192
dtmfSequence: Type.Optional(Type.String({ description: "DTMF digits to play before connect" })),
196193
}),
197194
Type.Object({
@@ -223,9 +220,6 @@ const VoiceCallToolSchema = Type.Union([
223220
sid: Type.Optional(Type.String({ description: "Call SID" })),
224221
message: Type.Optional(Type.String({ description: "Optional intro message" })),
225222
sessionKey: Type.Optional(Type.String({ description: "OpenClaw session key for the call" })),
226-
requesterSessionKey: Type.Optional(
227-
Type.String({ description: "OpenClaw session key that initiated the call" }),
228-
),
229223
dtmfSequence: Type.Optional(Type.String({ description: "DTMF digits to play before connect" })),
230224
}),
231225
]);
@@ -723,7 +717,13 @@ export default definePluginEntry({
723717
parameters: VoiceCallToolSchema,
724718
async execute(_toolCallId, params) {
725719
const rawParams = asParamRecord(params);
726-
const agentId = normalizeOptionalString(toolContext.agentId);
720+
const requesterSessionKey = normalizeOptionalString(toolContext.sessionKey);
721+
// Agent ownership and requester lineage come from trusted tool context.
722+
// Some harnesses omit agentId but retain its canonical session key.
723+
const contextAgentId =
724+
normalizeOptionalString(toolContext.agentId) ??
725+
parseAgentSessionKey(requesterSessionKey)?.agentId;
726+
const agentId = contextAgentId ? normalizeAgentId(contextAgentId) : undefined;
727727
try {
728728
const rt = await ensureRuntime();
729729

@@ -738,15 +738,20 @@ export default definePluginEntry({
738738
if (!to) {
739739
throw new Error("to required");
740740
}
741-
const result = await rt.manager.initiateCall(to, undefined, {
742-
message,
743-
dtmfSequence: normalizeOptionalString(rawParams.dtmfSequence),
744-
mode:
745-
rawParams.mode === "notify" || rawParams.mode === "conversation"
746-
? rawParams.mode
747-
: undefined,
748-
...(agentId ? { agentId } : {}),
749-
});
741+
const result = await rt.manager.initiateCall(
742+
to,
743+
normalizeOptionalString(rawParams.sessionKey),
744+
{
745+
message,
746+
dtmfSequence: normalizeOptionalString(rawParams.dtmfSequence),
747+
mode:
748+
rawParams.mode === "notify" || rawParams.mode === "conversation"
749+
? rawParams.mode
750+
: undefined,
751+
...(agentId ? { agentId } : {}),
752+
...(requesterSessionKey ? { requesterSessionKey } : {}),
753+
},
754+
);
750755
if (!result.success) {
751756
throw new Error(result.error || "initiate failed");
752757
}
@@ -833,9 +838,7 @@ export default definePluginEntry({
833838
dtmfSequence: normalizeOptionalString(rawParams.dtmfSequence),
834839
message: normalizeOptionalString(rawParams.message),
835840
...(agentId ? { agentId } : {}),
836-
...(normalizeOptionalString(rawParams.requesterSessionKey)
837-
? { requesterSessionKey: normalizeOptionalString(rawParams.requesterSessionKey) }
838-
: {}),
841+
...(requesterSessionKey ? { requesterSessionKey } : {}),
839842
},
840843
);
841844
if (!result.success) {

‎test/e2e/qa-lab/runtime/voice-call-gateway.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,22 +207,21 @@ async function runVoiceCallProof(options: ProducerOptions): Promise<string> {
207207
to: "+15550002222",
208208
message: "Gateway RPC fixture",
209209
mode: "conversation",
210-
sessionKey: "agent:main:voice-rpc",
210+
sessionKey: "agent:qa:voice-rpc",
211211
});
212212
const rpcCallId = findStringByKey(rpc, "callId");
213213
if (!rpcCallId) {
214214
throw new Error(`voicecall.initiate did not return a callId: ${JSON.stringify(rpc)}`);
215215
}
216216
const tool = await gateway.call("tools.invoke", {
217217
name: "voice_call",
218-
sessionKey: "agent:main:requester",
218+
sessionKey: "agent:qa:requester",
219219
args: {
220220
action: "initiate_call",
221221
to: "+15550003333",
222222
message: "Agent tool fixture",
223223
mode: "conversation",
224-
sessionKey: "agent:main:voice-consult",
225-
requesterSessionKey: "agent:main:requester",
224+
sessionKey: "agent:qa:voice-consult",
226225
},
227226
});
228227
const toolCallId = findStringByKey(tool, "callId");
@@ -255,6 +254,10 @@ async function runVoiceCallProof(options: ProducerOptions): Promise<string> {
255254
streamUrl: stream.streamUrl,
256255
});
257256
const toolResults = await waitForFinalToolResult(fixture.toolResultsPath);
257+
const finalToolResult = toolResults.final.result as Record<string, unknown>;
258+
if (typeof finalToolResult.error === "string") {
259+
throw new Error(`embedded consult failed: ${finalToolResult.error}`);
260+
}
258261
const bridgeCalls = (await fs.readFile(fixture.bridgeCallsPath, "utf8"))
259262
.split("\n")
260263
.map((line) => line.trim())

0 commit comments

Comments
 (0)