Skip to content

Commit aaf5d23

Browse files
authored
fix(heartbeat): stop reporting success after tool failures (#107735)
* fix(heartbeat): preserve terminal tool failures * fix(heartbeat): handle absent failure payloads * test(agents): align execution-state consumers * fix(heartbeat): retain unmatched pending finals * fix(heartbeat): preserve failures across retries * refactor(agents): narrow terminal outcome boundary * fix(copilot): preserve terminal tool failures * fix(agents): bound tool execution tracking * docs(plugins): document harness terminal outcomes * fix(codex): preserve exact terminal boundaries * test(codex): harden abort race fixtures
1 parent c90833a commit aaf5d23

51 files changed

Lines changed: 3036 additions & 438 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/docs_map.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7283,6 +7283,7 @@ Do not edit it by hand; run `pnpm docs:map:gen`.
72837283
- H2: Runtime strictness
72847284
- H2: Native sessions and transcript mirror
72857285
- H2: Tool and media results
7286+
- H3: Terminal tool outcomes
72867287
- H2: Current limitations
72877288
- H2: Related
72887289

‎docs/plugins/sdk-agent-harness.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,39 @@ yourself.
435435
This keeps text, image, video, music, TTS, approval, and messaging-tool
436436
outputs on the same delivery path as OpenClaw-backed runs.
437437

438+
### Terminal tool outcomes
439+
440+
`AgentHarnessAttemptParams.observeToolTerminal` is the host-owned terminal
441+
outcome accumulator. A harness that executes OpenClaw dynamic tools or native
442+
tools must call it when each tool reaches one terminal outcome, before the
443+
attempt result is finalized. Harnesses that do not execute tools do not need to
444+
call it.
445+
446+
Report facts from the execution boundary:
447+
448+
- Pass the protocol call id when one exists, the canonical tool name, and the
449+
arguments that actually reached the tool after preparation or hook rewrites.
450+
- Set `executionStarted: false` when validation, approval, or another guard
451+
stopped the call before the tool implementation began. Once dispatch may
452+
have happened, report `true` conservatively.
453+
- Report `outcome: "success"` or `outcome: "failure"`. Include the structured
454+
failure fields available from the runtime instead of inferring failure from
455+
display text.
456+
- Use `nativeMutation` only for native tools that do not use an OpenClaw tool
457+
definition. Supply protocol-owned mutation and replay facts there; do not
458+
copy OpenClaw's mutation classifier into the harness.
459+
460+
The callback returns the canonical resolution for that call. Carry its
461+
`lastToolError` into `AgentHarnessAttemptResult` and use its execution,
462+
arguments, and side-effect facts in the harness projection instead of deriving
463+
parallel state. The host keeps an unresolved mutating failure across unrelated
464+
successful tools and clears it only after the matching action succeeds.
465+
466+
The callback remains optional for source compatibility with older experimental
467+
harnesses. Optional does not mean ignorable for a harness that executes tools:
468+
without terminal reports, OpenClaw cannot preserve mutating-tool failure truth
469+
across later tool calls, including quiet heartbeat completion.
470+
438471
## Current limitations
439472

440473
- The public import path is generic, but some attempt/result type aliases

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

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Codex tests cover dynamic tool execution plugin behavior.
2-
import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime";
2+
import {
3+
embeddedAgentLog,
4+
type EmbeddedRunAttemptParams,
5+
} from "openclaw/plugin-sdk/agent-harness-runtime";
36
import { afterEach, describe, expect, it, vi } from "vitest";
47
import {
58
handleDynamicToolCallWithTimeout,
@@ -311,6 +314,10 @@ describe("dynamic tool execution helpers", () => {
311314
signal: new AbortController().signal,
312315
timeoutMs: 1,
313316
onAgentToolResult,
317+
observeToolTerminal: () => ({
318+
executionStarted: true,
319+
sideEffectEvidence: true,
320+
}),
314321
onFallbackSelected,
315322
onTimeout,
316323
});
@@ -327,6 +334,7 @@ describe("dynamic tool execution helpers", () => {
327334
],
328335
});
329336
expect((await response).diagnosticTerminalReason).toBe("timed_out");
337+
expect((await response).executionStarted).toBe(true);
330338
expect(capturedSignal?.aborted).toBe(true);
331339
expect(onFallbackSelected).toHaveBeenCalledOnce();
332340
expect(onTimeout).toHaveBeenCalledTimes(1);
@@ -348,6 +356,126 @@ describe("dynamic tool execution helpers", () => {
348356
});
349357
});
350358

359+
it("marks a timeout during pre-execution hooks as unstarted", async () => {
360+
vi.useFakeTimers();
361+
const observeToolTerminal = vi.fn(() => ({
362+
executionStarted: false,
363+
sideEffectEvidence: false,
364+
}));
365+
const response = handleDynamicToolCallWithTimeout({
366+
call: {
367+
threadId: "thread-1",
368+
turnId: "turn-1",
369+
callId: "call-prehook-timeout",
370+
namespace: null,
371+
tool: "message",
372+
arguments: { action: "send", text: "hello" },
373+
},
374+
toolBridge: { handleToolCall: vi.fn(() => new Promise<never>(() => {})) },
375+
signal: new AbortController().signal,
376+
timeoutMs: 1,
377+
observeToolTerminal,
378+
});
379+
380+
await vi.advanceTimersByTimeAsync(1);
381+
382+
await expect(response).resolves.toMatchObject({ executionStarted: false, success: false });
383+
expect((await response).sideEffectEvidence).toBeUndefined();
384+
expect(observeToolTerminal).toHaveBeenCalledWith(
385+
expect.objectContaining({
386+
toolCallId: "call-prehook-timeout",
387+
toolName: "message",
388+
outcome: "failure",
389+
}),
390+
);
391+
});
392+
393+
it("delegates an unpublished abort boundary to the terminal observer", async () => {
394+
vi.useFakeTimers();
395+
const observeToolTerminal = vi.fn(
396+
(
397+
_observation: Parameters<NonNullable<EmbeddedRunAttemptParams["observeToolTerminal"]>>[0],
398+
) => ({
399+
executionStarted: false,
400+
executedArguments: {
401+
action: "send",
402+
target: "channel:adjusted",
403+
text: "hello",
404+
},
405+
sideEffectEvidence: false,
406+
}),
407+
);
408+
const response = handleDynamicToolCallWithTimeout({
409+
call: {
410+
threadId: "thread-1",
411+
turnId: "turn-1",
412+
callId: "call-abort-aware-timeout",
413+
namespace: null,
414+
tool: "message",
415+
arguments: { action: "send", target: "channel:original", text: "hello" },
416+
},
417+
toolBridge: {
418+
handleToolCall: vi.fn((_call, options) => {
419+
expect(options?.retainExecutionSnapshot).toBe(true);
420+
return new Promise<never>((_resolve, reject) => {
421+
options?.signal?.addEventListener(
422+
"abort",
423+
() => {
424+
const reason = options.signal?.reason;
425+
reject(reason instanceof Error ? reason : new Error("tool call aborted"));
426+
},
427+
{ once: true },
428+
);
429+
});
430+
}),
431+
consumeToolExecutionSnapshot: vi.fn(() => undefined),
432+
},
433+
signal: new AbortController().signal,
434+
timeoutMs: 1,
435+
observeToolTerminal,
436+
});
437+
438+
await vi.advanceTimersByTimeAsync(1);
439+
440+
await expect(response).resolves.toMatchObject({ executionStarted: false, success: false });
441+
expect(observeToolTerminal).toHaveBeenCalledWith(
442+
expect.objectContaining({
443+
arguments: { action: "send", target: "channel:original", text: "hello" },
444+
outcome: "failure",
445+
}),
446+
);
447+
expect(observeToolTerminal.mock.calls[0]?.[0]).not.toHaveProperty("executionStarted");
448+
await expect(response).resolves.toMatchObject({
449+
executedArguments: {
450+
action: "send",
451+
target: "channel:adjusted",
452+
text: "hello",
453+
},
454+
});
455+
});
456+
457+
it("uses a conservative dispatched fallback without a terminal observer", async () => {
458+
vi.useFakeTimers();
459+
const response = handleDynamicToolCallWithTimeout({
460+
call: {
461+
threadId: "thread-1",
462+
turnId: "turn-1",
463+
callId: "call-untracked-timeout",
464+
namespace: null,
465+
tool: "custom_mutation",
466+
arguments: {},
467+
},
468+
toolBridge: { handleToolCall: vi.fn(() => new Promise<never>(() => {})) },
469+
signal: new AbortController().signal,
470+
timeoutMs: 1,
471+
});
472+
473+
await vi.advanceTimersByTimeAsync(1);
474+
475+
await expect(response).resolves.toMatchObject({ executionStarted: true, success: false });
476+
expect((await response).sideEffectEvidence).toBe(true);
477+
});
478+
351479
it("lets a structured sessions_send timeout win after setup work", async () => {
352480
vi.useFakeTimers();
353481
const call = {
@@ -420,6 +548,7 @@ describe("dynamic tool execution helpers", () => {
420548
],
421549
});
422550
expect(result.diagnosticTerminalReason).toBe("cancelled");
551+
expect(result.executionStarted).toBe(false);
423552
expect(handleToolCall).not.toHaveBeenCalled();
424553
expect(onAgentToolResult).toHaveBeenCalledOnce();
425554
expect(onAgentToolResult).toHaveBeenCalledWith({
@@ -626,6 +755,11 @@ describe("dynamic tool execution helpers", () => {
626755
},
627756
signal: new AbortController().signal,
628757
timeoutMs: 1,
758+
observeToolTerminal: () => ({
759+
executionStarted: true,
760+
executedArguments: { action: "poll", sessionId: "adjusted-session" },
761+
sideEffectEvidence: true,
762+
}),
629763
});
630764

631765
await vi.advanceTimersByTimeAsync(1);
@@ -639,6 +773,10 @@ describe("dynamic tool execution helpers", () => {
639773
},
640774
],
641775
});
776+
await expect(response).resolves.toMatchObject({ executionStarted: true });
777+
await expect(response).resolves.toMatchObject({
778+
executedArguments: { action: "poll", sessionId: "adjusted-session" },
779+
});
642780
expect(warn).toHaveBeenCalledWith("codex dynamic tool call timed out", {
643781
tool: "process",
644782
toolCallId: "call-timeout",
@@ -704,6 +842,18 @@ describe("dynamic tool execution helpers", () => {
704842
enumerable: false,
705843
value: true,
706844
});
845+
Object.defineProperties(response, {
846+
executedArguments: {
847+
configurable: true,
848+
enumerable: false,
849+
value: { action: "send", to: "channel:123" },
850+
},
851+
executionStarted: {
852+
configurable: true,
853+
enumerable: false,
854+
value: true,
855+
},
856+
});
707857

708858
const protocolResponse = toCodexDynamicToolProtocolResponse(response);
709859
const progressResponse = toCodexDynamicToolProgressResponse(response, protocolResponse);
@@ -713,6 +863,8 @@ describe("dynamic tool execution helpers", () => {
713863
success: true,
714864
});
715865
expect(Object.keys(protocolResponse)).not.toContain("asyncStarted");
866+
expect("executionStarted" in protocolResponse).toBe(false);
867+
expect("executedArguments" in protocolResponse).toBe(false);
716868
expect(progressResponse).toEqual({
717869
contentItems: [{ type: "inputText", text: "Background task started." }],
718870
details: { async: true, status: "started" },

0 commit comments

Comments
 (0)