Skip to content

Commit e01362c

Browse files
roboclaw-botVACInc
andcommitted
fix(code-mode): carry owner effect receipts
Worked on by: - @VACInc Co-authored-by: VACInc <[email protected]>
1 parent 13adff0 commit e01362c

22 files changed

Lines changed: 510 additions & 200 deletions

‎extensions/codex/src/app-server/dynamic-tool-execution.owner-timeout.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("dynamic tool owner timeout", () => {
1313
const observeToolTerminal = vi.fn(() => ({
1414
executionStarted: true,
1515
sideEffectEvidence: true,
16+
effectReceipt: { state: "uncertain" as const },
1617
}));
1718
const response = handleDynamicToolCallWithTimeout({
1819
call: {

‎extensions/codex/src/app-server/dynamic-tool-execution.test.ts‎

Lines changed: 4 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// Codex tests cover dynamic tool execution plugin behavior.
2-
import {
3-
embeddedAgentLog,
4-
type EmbeddedRunAttemptParamsV2 as EmbeddedRunAttemptParams,
5-
} from "openclaw/plugin-sdk/agent-harness-runtime";
2+
import type { EmbeddedRunAttemptParamsV2 as EmbeddedRunAttemptParams } from "openclaw/plugin-sdk/agent-harness-runtime";
63
import { afterEach, describe, expect, it, vi } from "vitest";
74
import {
85
handleDynamicToolCallWithTimeout,
@@ -359,6 +356,7 @@ describe("dynamic tool execution helpers", () => {
359356
observeToolTerminal: () => ({
360357
executionStarted: true,
361358
sideEffectEvidence: true,
359+
effectReceipt: { state: "uncertain" as const },
362360
}),
363361
onFallbackSelected,
364362
onTimeout,
@@ -437,6 +435,7 @@ describe("dynamic tool execution helpers", () => {
437435
const observeToolTerminal = vi.fn(() => ({
438436
executionStarted: false,
439437
sideEffectEvidence: false,
438+
effectReceipt: { state: "uncertain" as const },
440439
}));
441440
const response = handleDynamicToolCallWithTimeout({
442441
call: {
@@ -479,6 +478,7 @@ describe("dynamic tool execution helpers", () => {
479478
text: "hello",
480479
},
481480
sideEffectEvidence: false,
481+
effectReceipt: { state: "uncertain" as const },
482482
}),
483483
);
484484
const response = handleDynamicToolCallWithTimeout({
@@ -826,100 +826,6 @@ describe("dynamic tool execution helpers", () => {
826826
expect(onAgentToolResult).toHaveBeenCalledOnce();
827827
});
828828

829-
it("logs process poll timeout context separately from session idle", async () => {
830-
vi.useFakeTimers();
831-
const warn = vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
832-
const response = handleDynamicToolCallWithTimeout({
833-
call: {
834-
threadId: "thread-1",
835-
turnId: "turn-1",
836-
callId: "call-timeout",
837-
namespace: null,
838-
tool: "process",
839-
arguments: { action: "poll", sessionId: "process-session", timeout: 30_000 },
840-
},
841-
toolBridge: {
842-
handleToolCall: vi.fn(() => new Promise<never>(() => {})),
843-
},
844-
signal: new AbortController().signal,
845-
timeoutMs: 1,
846-
observeToolTerminal: () => ({
847-
executionStarted: true,
848-
executedArguments: { action: "poll", sessionId: "adjusted-session" },
849-
sideEffectEvidence: true,
850-
}),
851-
});
852-
853-
await vi.advanceTimersByTimeAsync(1);
854-
855-
await expect(response).resolves.toEqual({
856-
success: false,
857-
contentItems: [
858-
{
859-
type: "inputText",
860-
text: "OpenClaw dynamic tool call timed out after 1ms while waiting for process action=poll sessionId=process-session. This is a tool RPC timeout, not a session idle timeout.",
861-
},
862-
],
863-
});
864-
await expect(response).resolves.toMatchObject({ executionStarted: true });
865-
await expect(response).resolves.toMatchObject({
866-
executedArguments: { action: "poll", sessionId: "adjusted-session" },
867-
});
868-
expect(warn).toHaveBeenCalledWith("codex dynamic tool call timed out", {
869-
tool: "process",
870-
toolCallId: "call-timeout",
871-
threadId: "thread-1",
872-
turnId: "turn-1",
873-
timeoutMs: 1,
874-
timeoutKind: "codex_dynamic_tool_rpc",
875-
processAction: "poll",
876-
processSessionId: "process-session",
877-
processRequestedTimeoutMs: 30_000,
878-
consoleMessage:
879-
"codex process tool timeout: action=poll sessionId=process-session toolTimeoutMs=1 requestedWaitMs=30000; per-tool-call watchdog, not session idle; repeated lines usually mean process-poll retry churn, not model progress",
880-
});
881-
});
882-
883-
it("does not split surrogate pairs when truncating timeout log fields", async () => {
884-
vi.useFakeTimers();
885-
const warn = vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
886-
const action = `${"a".repeat(156)}😀tail`;
887-
const sessionId = `${"s".repeat(156)}😀tail`;
888-
const response = handleDynamicToolCallWithTimeout({
889-
call: {
890-
threadId: "thread-1",
891-
turnId: "turn-1",
892-
callId: "call-utf16-log-field",
893-
namespace: null,
894-
tool: "process",
895-
arguments: { action, sessionId, timeout: 30_000 },
896-
},
897-
toolBridge: {
898-
handleToolCall: vi.fn(() => new Promise<never>(() => {})),
899-
},
900-
signal: new AbortController().signal,
901-
timeoutMs: 1,
902-
});
903-
904-
await vi.advanceTimersByTimeAsync(1);
905-
906-
const result = await response;
907-
const firstResultItem = result.contentItems[0];
908-
const resultText = firstResultItem?.type === "inputText" ? firstResultItem.text : "";
909-
const [, details] = warn.mock.calls[0] ?? [];
910-
const highSurrogate = String.fromCharCode(0xd83d);
911-
912-
expect(result.success).toBe(false);
913-
expect(details).toMatchObject({
914-
processAction: `${"a".repeat(156)}...`,
915-
processSessionId: `${"s".repeat(156)}...`,
916-
});
917-
expect(resultText).not.toContain(highSurrogate);
918-
expect(String((details as Record<string, unknown>).consoleMessage)).not.toContain(
919-
highSurrogate,
920-
);
921-
});
922-
923829
it("keeps async-start metadata on internal dynamic tool progress only", () => {
924830
const response: CodexDynamicToolCallResponse = {
925831
contentItems: [{ type: "inputText", text: "Background task started." }],
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime";
2+
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { handleDynamicToolCallWithTimeout } from "./dynamic-tool-execution.js";
4+
5+
describe("dynamic tool timeout logging", () => {
6+
afterEach(() => {
7+
vi.restoreAllMocks();
8+
vi.useRealTimers();
9+
});
10+
11+
it("logs process poll timeout context separately from session idle", async () => {
12+
vi.useFakeTimers();
13+
const warn = vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
14+
const response = handleDynamicToolCallWithTimeout({
15+
call: {
16+
threadId: "thread-1",
17+
turnId: "turn-1",
18+
callId: "call-timeout",
19+
namespace: null,
20+
tool: "process",
21+
arguments: { action: "poll", sessionId: "process-session", timeout: 30_000 },
22+
},
23+
toolBridge: {
24+
handleToolCall: vi.fn(() => new Promise<never>(() => {})),
25+
},
26+
signal: new AbortController().signal,
27+
timeoutMs: 1,
28+
observeToolTerminal: () => ({
29+
executionStarted: true,
30+
executedArguments: { action: "poll", sessionId: "adjusted-session" },
31+
sideEffectEvidence: true,
32+
effectReceipt: { state: "uncertain" },
33+
}),
34+
});
35+
36+
await vi.advanceTimersByTimeAsync(1);
37+
38+
await expect(response).resolves.toEqual({
39+
success: false,
40+
contentItems: [
41+
{
42+
type: "inputText",
43+
text: "OpenClaw dynamic tool call timed out after 1ms while waiting for process action=poll sessionId=process-session. This is a tool RPC timeout, not a session idle timeout.",
44+
},
45+
],
46+
});
47+
await expect(response).resolves.toMatchObject({ executionStarted: true });
48+
await expect(response).resolves.toMatchObject({
49+
executedArguments: { action: "poll", sessionId: "adjusted-session" },
50+
});
51+
expect(warn).toHaveBeenCalledWith("codex dynamic tool call timed out", {
52+
tool: "process",
53+
toolCallId: "call-timeout",
54+
threadId: "thread-1",
55+
turnId: "turn-1",
56+
timeoutMs: 1,
57+
timeoutKind: "codex_dynamic_tool_rpc",
58+
processAction: "poll",
59+
processSessionId: "process-session",
60+
processRequestedTimeoutMs: 30_000,
61+
consoleMessage:
62+
"codex process tool timeout: action=poll sessionId=process-session toolTimeoutMs=1 requestedWaitMs=30000; per-tool-call watchdog, not session idle; repeated lines usually mean process-poll retry churn, not model progress",
63+
});
64+
});
65+
66+
it("does not split surrogate pairs when truncating timeout log fields", async () => {
67+
vi.useFakeTimers();
68+
const warn = vi.spyOn(embeddedAgentLog, "warn").mockImplementation(() => undefined);
69+
const action = `${"a".repeat(156)}😀tail`;
70+
const sessionId = `${"s".repeat(156)}😀tail`;
71+
const response = handleDynamicToolCallWithTimeout({
72+
call: {
73+
threadId: "thread-1",
74+
turnId: "turn-1",
75+
callId: "call-utf16-log-field",
76+
namespace: null,
77+
tool: "process",
78+
arguments: { action, sessionId, timeout: 30_000 },
79+
},
80+
toolBridge: {
81+
handleToolCall: vi.fn(() => new Promise<never>(() => {})),
82+
},
83+
signal: new AbortController().signal,
84+
timeoutMs: 1,
85+
});
86+
87+
await vi.advanceTimersByTimeAsync(1);
88+
89+
const result = await response;
90+
const firstResultItem = result.contentItems[0];
91+
const resultText = firstResultItem?.type === "inputText" ? firstResultItem.text : "";
92+
const [, details] = warn.mock.calls[0] ?? [];
93+
const highSurrogate = String.fromCharCode(0xd83d);
94+
95+
expect(result.success).toBe(false);
96+
expect(details).toMatchObject({
97+
processAction: `${"a".repeat(156)}...`,
98+
processSessionId: `${"s".repeat(156)}...`,
99+
});
100+
expect(resultText).not.toContain(highSurrogate);
101+
expect(String((details as Record<string, unknown>).consoleMessage)).not.toContain(
102+
highSurrogate,
103+
);
104+
});
105+
});

‎extensions/codex/src/app-server/test-support.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ export function createCodexTestToolTerminalObserver(): NonNullable<
4545
executionStarted,
4646
...(Object.keys(record).length > 0 ? { executedArguments: record } : {}),
4747
sideEffectEvidence: executionStarted && !mutation.replaySafe,
48+
effectReceipt: {
49+
state: !executionStarted
50+
? "uncertain"
51+
: mutation.replaySafe
52+
? observation.outcome === "success"
53+
? "read_completed"
54+
: "failed_no_effect"
55+
: mutation.mutatingAction && observation.outcome === "success"
56+
? "mutation_committed"
57+
: "uncertain",
58+
},
4859
};
4960
};
5061
}

‎extensions/copilot/src/attempt.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ describe("runCopilotAttempt", () => {
598598
...(activeError ? { lastToolError: activeError } : {}),
599599
executionStarted: true,
600600
sideEffectEvidence: observation.toolName === "message",
601+
effectReceipt: {
602+
state: observation.toolName === "message" ? "uncertain" : "read_completed",
603+
} as const,
601604
};
602605
});
603606
const createToolBridge = vi.fn(async (input: CopilotToolBridgeInput) => {
@@ -675,6 +678,9 @@ describe("runCopilotAttempt", () => {
675678
...(activeError ? { lastToolError: activeError } : {}),
676679
executionStarted: true,
677680
sideEffectEvidence: true,
681+
effectReceipt: {
682+
state: observation.outcome === "success" ? "mutation_committed" : "uncertain",
683+
} as const,
678684
};
679685
});
680686
const createToolBridge = vi.fn(async (input: CopilotToolBridgeInput) => {

‎extensions/copilot/src/tool-bridge.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,7 @@ describe("createCopilotToolBridge tool conversion", () => {
20542054
const observeToolTerminal = vi.fn(() => ({
20552055
executionStarted: true,
20562056
sideEffectEvidence: true,
2057+
effectReceipt: { state: "uncertain" as const },
20572058
}));
20582059
const sdkTool = await convertOpenClawToolToSdkToolForTest(
20592060
makeTool({ execute, name: "message" }),

‎src/agents/code-mode-bridge.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ import {
2626
SWARM_CODE_MODE_REQUEST_FINGERPRINT,
2727
} from "./subagents/swarm/swarm-code-mode.js";
2828
import { resolveSwarmConfig } from "./subagents/swarm/swarm-config.js";
29+
import {
30+
consumeToolEffectReceipt,
31+
registerToolEffectReceipt,
32+
type ToolEffectReceipt,
33+
} from "./tool-effect-receipt.js";
2934
import { isToolExecutionAllowed, TOOL_EXECUTION_GATED_MESSAGE } from "./tool-policy-shared.js";
3035
import {
3136
consumeTrustedToolNoStartError,
@@ -398,6 +403,7 @@ export async function runBridgeRequest(params: {
398403
onUpdate?: AgentToolUpdateCallback;
399404
}): Promise<SettledBridgeRequest> {
400405
const catalogProjection = params.catalogProjection;
406+
let effectReceipt: ToolEffectReceipt | undefined;
401407
try {
402408
const values = Array.isArray(params.request.args) ? params.request.args : [];
403409
let value: unknown;
@@ -473,6 +479,7 @@ export async function runBridgeRequest(params: {
473479
signal: params.signal,
474480
onUpdate: params.onUpdate,
475481
});
482+
effectReceipt = consumeToolEffectReceipt(called.result);
476483
value =
477484
isRecord(called.result) && "details" in called.result
478485
? called.result.details
@@ -523,6 +530,7 @@ export async function runBridgeRequest(params: {
523530
signal: params.signal,
524531
onUpdate: params.onUpdate,
525532
});
533+
effectReceipt = consumeToolEffectReceipt(called.result);
526534
if (request.catalogId) {
527535
const guestResult = consumeMcpCodeModeGuestResult(called.result);
528536
if (guestResult === undefined) {
@@ -537,6 +545,7 @@ export async function runBridgeRequest(params: {
537545
: called.result;
538546
},
539547
);
548+
effectReceipt ??= consumeToolEffectReceipt(value);
540549
break;
541550
}
542551
case "agentSpawn": {
@@ -591,16 +600,20 @@ export async function runBridgeRequest(params: {
591600
"Search results exceed the output budget. Narrow the query or lower the limit.",
592601
);
593602
}
594-
return { id: params.request.id, ok: true, value };
603+
const settled: SettledBridgeRequest = { id: params.request.id, ok: true, value };
604+
return effectReceipt ? registerToolEffectReceipt(settled, effectReceipt) : settled;
595605
} catch (error) {
596606
const settled: SettledBridgeRequest = {
597607
id: params.request.id,
598608
ok: false,
599609
error: redactCodeModeCatalogIds(formatErrorMessage(error), catalogProjection.bindings),
600610
};
601-
if (consumeTrustedToolNoStartError(error)) {
611+
const trustedNoStart = consumeTrustedToolNoStartError(error);
612+
if (trustedNoStart) {
602613
registerTrustedToolNoStartError(settled);
603614
}
604-
return settled;
615+
effectReceipt =
616+
consumeToolEffectReceipt(error) ?? (trustedNoStart ? { state: "not_started" } : undefined);
617+
return effectReceipt ? registerToolEffectReceipt(settled, effectReceipt) : settled;
605618
}
606619
}

0 commit comments

Comments
 (0)