Skip to content

Commit cd7b7f6

Browse files
Glucksbergobviyus849261680
authored
fix(agent): allow progress before final message-tool replies
Treat message(final=false) as progress and final=true or omission as the terminal source reply. Repeated terminal delivery now returns a non-error outcome without another provider send. Co-authored-by: Ayaan Zaidi <[email protected]> Co-authored-by: 宇宙熊Yzx <[email protected]> Co-authored-by: Markus <[email protected]>
1 parent 4f046b5 commit cd7b7f6

28 files changed

Lines changed: 304 additions & 253 deletions

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
isSystemAgentOnlyCodexDynamicToolAllowlist,
3131
normalizeCodexDynamicToolName,
3232
} from "./dynamic-tool-profile.js";
33-
import { addCodexMessageToolOnlyFinalControl } from "./message-tool-final-control.js";
3433
import {
3534
resolveCodexNodeExecToolOverrides,
3635
resolveCodexNativeExecutionPolicy,
@@ -359,13 +358,9 @@ export async function buildDynamicTools(input: DynamicToolBuildParams) {
359358
run: buildOpenClawCodingTools,
360359
})
361360
: buildOpenClawCodingTools();
362-
const codexScopedTools = addCodexMessageToolOnlyFinalControl(
363-
allTools,
364-
params.sourceReplyDeliveryMode,
365-
);
366361
toolBuildStages.mark("create-openclaw-coding-tools");
367362
const preNormalizationDiagnostics: RuntimeToolSchemaDiagnostic[] = [];
368-
const readableAllToolProjection = filterProviderNormalizableTools(codexScopedTools);
363+
const readableAllToolProjection = filterProviderNormalizableTools(allTools);
369364
preNormalizationDiagnostics.push(...readableAllToolProjection.diagnostics);
370365
const webSearchPlan = resolveCodexWebSearchPlan({
371366
config: params.config,

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

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {
4141
type JsonValue,
4242
} from "./protocol.js";
4343
import type { CodexRemoteWorkspaceFileReader } from "./remote-workspace-media.js";
44-
import { settleCodexSourceReplyFinality } from "./source-reply-finality.js";
4544

4645
const CODEX_OPENCLAW_DYNAMIC_TOOL_NAMESPACE = "openclaw";
4746

@@ -1638,7 +1637,7 @@ describe("createCodexDynamicToolBridge", () => {
16381637
]);
16391638
});
16401639

1641-
it("keeps omitted source-reply finality non-terminal until a successful attempt settles", async () => {
1640+
it("treats omitted source-reply finality as terminal", async () => {
16421641
const bridge = createBridgeWithToolResult(
16431642
"message",
16441643
textToolResult("Sent.", { messageId: "imessage-6264" }),
@@ -1651,19 +1650,15 @@ describe("createCodexDynamicToolBridge", () => {
16511650
});
16521651

16531652
expect(result).toEqual(expectInputText("Sent."));
1654-
expect(result.terminate).toBeUndefined();
1653+
expect(result.terminate).toBe(true);
16551654
expect(bridge.telemetry.didDeliverSourceReplyViaMessageTool).toBe(true);
1656-
expect(bridge.telemetry.messagingToolSentTargets.at(-1)).not.toHaveProperty("sourceReplyFinal");
1657-
1658-
expect(settleCodexSourceReplyFinality(bridge.telemetry, true)).toBe(true);
1659-
16601655
expect(bridge.telemetry.messagingToolSentTargets.at(-1)).toMatchObject({
16611656
sourceReplyFinal: true,
16621657
});
16631658
expect(Object.keys(result)).not.toContain("terminate");
16641659
});
16651660

1666-
it("settles omitted source-reply finality as progress when the attempt fails", async () => {
1661+
it("keeps omitted source-reply finality terminal when the tool requests termination", async () => {
16671662
const bridge = createBridgeWithToolResult(
16681663
"message",
16691664
{
@@ -1677,68 +1672,11 @@ describe("createCodexDynamicToolBridge", () => {
16771672
action: "send",
16781673
message: "visible reply",
16791674
});
1680-
expect(result.terminate).toBeUndefined();
1681-
expect(settleCodexSourceReplyFinality(bridge.telemetry, false)).toBe(false);
1675+
expect(result.terminate).toBe(true);
16821676

16831677
expect(bridge.telemetry.messagingToolSentTargets.at(-1)).toMatchObject({
1684-
sourceReplyFinal: false,
1685-
});
1686-
});
1687-
1688-
it("settles only the latest omitted source reply as final after success", async () => {
1689-
const bridge = createBridgeWithToolResult(
1690-
"message",
1691-
textToolResult("Sent.", { messageId: "imessage-6264" }),
1692-
{ sourceReplyDeliveryMode: "message_tool_only" },
1693-
);
1694-
1695-
await handleMessageToolCall(bridge, { action: "send", message: "first update" });
1696-
await handleMessageToolCall(bridge, { action: "send", message: "second update" });
1697-
settleCodexSourceReplyFinality(bridge.telemetry, true);
1698-
1699-
expect(
1700-
bridge.telemetry.messagingToolSentTargets.map((target) => target.sourceReplyFinal),
1701-
).toEqual([false, true]);
1702-
});
1703-
1704-
it("does not promote an omitted reply past a later explicit progress reply", async () => {
1705-
const bridge = createBridgeWithToolResult(
1706-
"message",
1707-
textToolResult("Sent.", { messageId: "imessage-6264" }),
1708-
{ sourceReplyDeliveryMode: "message_tool_only" },
1709-
);
1710-
1711-
await handleMessageToolCall(bridge, { action: "send", message: "first update" });
1712-
await handleMessageToolCall(bridge, {
1713-
action: "send",
1714-
message: "still working",
1715-
final: false,
1716-
});
1717-
settleCodexSourceReplyFinality(bridge.telemetry, true);
1718-
1719-
expect(
1720-
bridge.telemetry.messagingToolSentTargets.map((target) => target.sourceReplyFinal),
1721-
).toEqual([false, false]);
1722-
});
1723-
1724-
it("keeps a later explicit final reply authoritative over an omitted reply", async () => {
1725-
const bridge = createBridgeWithToolResult(
1726-
"message",
1727-
textToolResult("Sent.", { messageId: "imessage-6264" }),
1728-
{ sourceReplyDeliveryMode: "message_tool_only" },
1729-
);
1730-
1731-
await handleMessageToolCall(bridge, { action: "send", message: "first update" });
1732-
await handleMessageToolCall(bridge, {
1733-
action: "send",
1734-
message: "finished",
1735-
final: true,
1678+
sourceReplyFinal: true,
17361679
});
1737-
settleCodexSourceReplyFinality(bridge.telemetry, true);
1738-
1739-
expect(
1740-
bridge.telemetry.messagingToolSentTargets.map((target) => target.sourceReplyFinal),
1741-
).toEqual([false, true]);
17421680
});
17431681

17441682
it("honors explicit finality for delivered message-tool-only source replies", async () => {
@@ -2094,7 +2032,7 @@ describe("createCodexDynamicToolBridge", () => {
20942032
expect(Object.keys(result)).not.toContain("terminate");
20952033
});
20962034

2097-
it("defers omitted finality even when the message tool returns legacy termination", async () => {
2035+
it("keeps omitted finality terminal when the message tool returns termination", async () => {
20982036
const bridge = createBridgeWithToolResult(
20992037
"message",
21002038
{
@@ -2114,12 +2052,8 @@ describe("createCodexDynamicToolBridge", () => {
21142052
});
21152053

21162054
expect(result).toEqual(expectInputText("Sent."));
2117-
expect(result.terminate).toBeUndefined();
2055+
expect(result.terminate).toBe(true);
21182056
expect(bridge.telemetry.didDeliverSourceReplyViaMessageTool).toBe(true);
2119-
expect(bridge.telemetry.messagingToolSentTargets.at(-1)).not.toHaveProperty("sourceReplyFinal");
2120-
2121-
settleCodexSourceReplyFinality(bridge.telemetry, true);
2122-
21232057
expect(bridge.telemetry.messagingToolSentTargets.at(-1)).toMatchObject({
21242058
sourceReplyFinal: true,
21252059
});
@@ -2193,7 +2127,7 @@ describe("createCodexDynamicToolBridge", () => {
21932127
arguments: { action: "inspect" },
21942128
});
21952129

2196-
expect(firstResult.terminate).toBeUndefined();
2130+
expect(firstResult.terminate).toBe(true);
21972131
expect(bridge.telemetry.didSendViaMessagingTool).toBe(true);
21982132
expect(secondResult).toEqual(expectInputText("No message sent."));
21992133
expect(secondResult.terminate).toBeUndefined();

‎extensions/codex/src/app-server/dynamic-tools.ts‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ import {
7979
prepareCodexRemoteWorkspaceMessageMedia,
8080
type CodexRemoteWorkspaceFileReader,
8181
} from "./remote-workspace-media.js";
82-
import { recordCodexSourceReplyDeliveryIntent } from "./source-reply-finality.js";
8382
import { resolveCodexToolAbortTerminalReason } from "./tool-abort-terminal-reason.js";
8483

8584
type CodexDynamicToolHookContext = NonNullable<
@@ -779,17 +778,12 @@ export function createCodexDynamicToolBridge(params: {
779778
toolName === "message" &&
780779
!resultIsError &&
781780
(rawResult.terminate === true || result.terminate === true);
782-
const hasExplicitFinalControl = typeof executedArgs.final === "boolean";
783781
const confirmedSourceReply =
784782
params.hookContext?.sourceReplyDeliveryMode === "message_tool_only" &&
785783
toolName === "message" &&
786784
(toolConfirmedSourceReply || deliveredSourceReply || receiptConfirmedSourceReply);
787-
const sourceReplyFinal = confirmedSourceReply
788-
? hasExplicitFinalControl
789-
? executedArgs.final === true
790-
: undefined
791-
: undefined;
792-
const sourceReplyRecord = collectToolTelemetry({
785+
const sourceReplyFinal = confirmedSourceReply ? executedArgs.final !== false : undefined;
786+
collectToolTelemetry({
793787
toolName,
794788
args: executedArgs,
795789
result,
@@ -799,26 +793,19 @@ export function createCodexDynamicToolBridge(params: {
799793
messagingTarget: confirmedMessagingTarget,
800794
sourceReplyFinal,
801795
});
802-
if (confirmedSourceReply && sourceReplyRecord) {
803-
recordCodexSourceReplyDeliveryIntent(telemetry, {
804-
record: sourceReplyRecord,
805-
final: sourceReplyFinal,
806-
});
807-
}
808796
if (deliveredSourceReply || receiptConfirmedSourceReply || toolConfirmedSourceReply) {
809797
telemetry.didDeliverSourceReplyViaMessageTool = true;
810798
}
811-
const defersInferredSourceReplyTermination =
812-
confirmedSourceReply && executedArgs.final !== true;
799+
const continuesSourceReplyProgress = confirmedSourceReply && sourceReplyFinal === false;
813800
withDynamicToolTermination(
814801
response,
815802
((rawResult.terminate === true || result.terminate === true) &&
816-
!defersInferredSourceReplyTermination) ||
803+
!continuesSourceReplyProgress) ||
817804
// Yield is an explicit owner-level turn handoff, not termination
818805
// inferred from source-reply delivery, so finality does not mask it.
819806
isToolResultYield(rawResult) ||
820807
isToolResultYield(result) ||
821-
(confirmedSourceReply && executedArgs.final === true),
808+
(confirmedSourceReply && sourceReplyFinal === true),
822809
);
823810
const asyncStarted =
824811
isAsyncStartedToolResult(rawResult) || isAsyncStartedToolResult(result);

‎extensions/codex/src/app-server/message-tool-final-control.ts‎

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎extensions/codex/src/app-server/run-attempt-finalize.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
import type { prepareCodexAttemptTurnRequest } from "./run-attempt-turn-request.js";
3232
import type { CodexAttemptTurnState } from "./run-attempt-turn-state.js";
3333
import { captureCodexSettledTurnFinalizationContext } from "./settled-turn-context.js";
34-
import { settleCodexSourceReplyFinality } from "./source-reply-finality.js";
3534
import { normalizeCodexTrajectoryError, recordCodexTrajectoryCompletion } from "./trajectory.js";
3635
import { codexTranscriptMirrorRuntime } from "./transcript-mirror.js";
3736
import {
@@ -270,10 +269,9 @@ export async function finalizeCodexAttempt(
270269
!effectiveTimedOut &&
271270
(finalPromptError === null || finalPromptError === undefined) &&
272271
(completedTurnStatus === "completed" || recoveredTurnWatchTimeout || locallyCompletedTurn);
273-
// buildResult retains the bridge's delivery records. Resolve omitted final
274-
// intent only after the authoritative turn outcome is known, before any
275-
// terminal observer consumes the result.
276-
const completedSourceReply = settleCodexSourceReplyFinality(toolBridge.telemetry, turnSucceeded);
272+
const completedSourceReply = toolBridge.telemetry.messagingToolSentTargets.some(
273+
(target) => target.sourceReplyFinal === true,
274+
);
277275
if (completedSourceReply) {
278276
// Harness classification only sees assistant/reasoning/plan projections.
279277
// A reply delivered entirely through the source message tool is visible

‎extensions/codex/src/app-server/source-reply-finality.ts‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎extensions/codex/src/app-server/thread-prompt.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function buildVisibleReplyInstruction(
8484
messageToolAvailable: boolean,
8585
): string {
8686
if (params.sourceReplyDeliveryMode === "message_tool_only" && messageToolAvailable) {
87-
return "Visible source replies are not automatically delivered for this run. Use `message(action=send)` for user-visible source-channel output. For progress, set `final=false`. When the message is the completed reply to the current source conversation, set `final=true`; OpenClaw stops after confirming delivery. If `final` is omitted, OpenClaw continues and resolves the latest omitted source reply only when the turn ends successfully. Do not repeat visible message content in your final answer.";
87+
return "Visible source replies are not automatically delivered for this run. Use `message(action=send)` for user-visible source-channel output. For progress, set `final=false`. Set `final=true`, or omit it, for the completed reply to the current source conversation; OpenClaw stops after confirming delivery. Do not repeat visible message content in your final answer.";
8888
}
8989
if (messageToolAvailable) {
9090
return "For the current source conversation, reply normally in your final assistant message; OpenClaw will deliver it through the active source conversation. Use `message` for supported non-text actions in the current conversation, such as reacting to its current message. Reserve other `message` actions for explicit out-of-band sends or media/file delivery. Reactions are not delivered automatically.";

0 commit comments

Comments
 (0)