Skip to content

Commit 11413bc

Browse files
zeroaltitudevincentkoc
authored andcommitted
fix(agents): recognize explicit current-source replies
Use the canonical delivered current-source matcher for CLI, terminal, and subscription completion paths. Preserve provider, account, target, and thread identity so only the active source route suppresses stranded-reply recovery. Punchcard-Session: golden-river-workshop-vj
1 parent 1be9b56 commit 11413bc

11 files changed

Lines changed: 353 additions & 28 deletions

‎src/agents/cli-runner/execute-tool-tracking.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import {
1818
extractMessagingToolSendResult,
1919
extractMessagingToolSourceReplyPayload,
20+
isDeliveredMessagingToolSendToCurrentSource,
2021
} from "../embedded-agent-messaging-extraction.js";
2122
import {
2223
isMessagingTool,
@@ -246,6 +247,8 @@ export function createCliToolTracking(context: PreparedCliRunContext) {
246247
const toolArgs = params.args ?? {};
247248
const isMessagingSend = isMessagingToolSendAction(params.toolName, toolArgs);
248249
const content = isMessagingSend ? extractCliMessagingContent(toolArgs, params.result) : {};
250+
const confirmedTarget =
251+
params.target && extractMessagingToolSendResult(params.target, params.result);
249252
const deliveredCurrentSourceReply =
250253
isMessagingSend &&
251254
isDeliveredMessageToolOnlySourceReplyResult({
@@ -254,6 +257,16 @@ export function createCliToolTracking(context: PreparedCliRunContext) {
254257
args: params.args,
255258
result: params.result,
256259
isError: params.isError,
260+
allowExplicitSourceRoute: isDeliveredMessagingToolSendToCurrentSource({
261+
send: confirmedTarget,
262+
config: context.params.config,
263+
currentProvider: context.params.messageChannel ?? context.params.messageProvider,
264+
currentAccountId: context.params.agentAccountId,
265+
currentChannelId: context.params.currentChannelId,
266+
currentThreadId: context.params.currentThreadTs,
267+
sessionKey: context.params.sessionKey,
268+
deliveredPayload: params.result,
269+
}),
257270
deliveryConfirmed: true,
258271
});
259272
const sourceReplyFinal = deliveredCurrentSourceReply
@@ -286,11 +299,11 @@ export function createCliToolTracking(context: PreparedCliRunContext) {
286299
}
287300
}
288301
}
289-
if (!params.target) {
302+
if (!confirmedTarget) {
290303
return;
291304
}
292305
const targetWithContent = {
293-
...extractMessagingToolSendResult(params.target, params.result),
306+
...confirmedTarget,
294307
...content,
295308
...(sourceReplyFinal !== undefined ? { sourceReplyFinal } : {}),
296309
};

‎src/agents/cli-runner/execute.supervisor-capture.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,84 @@ describe("executePreparedCliRun supervisor output capture", () => {
25792579
]);
25802580
});
25812581

2582+
it.each([
2583+
{
2584+
label: "the exact source route",
2585+
accountId: "account-1",
2586+
target: "chat123",
2587+
threadId: "thread-1",
2588+
expected: true,
2589+
},
2590+
{
2591+
label: "the same target in another account",
2592+
accountId: "account-2",
2593+
target: "chat123",
2594+
threadId: "thread-1",
2595+
expected: false,
2596+
},
2597+
{
2598+
label: "the same target in another thread",
2599+
accountId: "account-1",
2600+
target: "chat123",
2601+
threadId: "thread-2",
2602+
expected: false,
2603+
},
2604+
{
2605+
label: "another target",
2606+
accountId: "account-1",
2607+
target: "chat456",
2608+
threadId: "thread-1",
2609+
expected: false,
2610+
},
2611+
])("records explicit message sends only for $label", async (testCase) => {
2612+
const context = buildPreparedCliRunContext({ output: "text", provider: "local-cli" });
2613+
context.mcpDeliveryCapture = true;
2614+
context.params.sourceReplyDeliveryMode = "message_tool_only";
2615+
context.params.messageChannel = TEST_MESSAGE_CHANNEL;
2616+
context.params.agentAccountId = "account-1";
2617+
context.params.currentChannelId = "chat123";
2618+
context.params.currentThreadTs = "thread-1";
2619+
supervisorSpawnMock.mockImplementationOnce(async (...args: unknown[]) => {
2620+
const input = args[0] as SupervisorSpawnInput;
2621+
recordMcpLoopbackToolCallResult({
2622+
captureKey: input.env?.OPENCLAW_MCP_CLI_CAPTURE_KEY ?? "",
2623+
toolName: "message",
2624+
args: {
2625+
action: "send",
2626+
channel: TEST_MESSAGE_CHANNEL,
2627+
accountId: testCase.accountId,
2628+
target: testCase.target,
2629+
threadId: testCase.threadId,
2630+
message: "explicit reply",
2631+
},
2632+
result: {
2633+
ok: true,
2634+
details: {
2635+
deliveryStatus: "sent",
2636+
sourceReplySink: "internal-ui",
2637+
sourceReply: { text: "explicit reply" },
2638+
},
2639+
},
2640+
isError: false,
2641+
});
2642+
input.onStdout?.("done");
2643+
return createManagedRun({
2644+
reason: "exit",
2645+
exitCode: 0,
2646+
exitSignal: null,
2647+
durationMs: 50,
2648+
stdout: "",
2649+
stderr: "",
2650+
timedOut: false,
2651+
noOutputTimedOut: false,
2652+
});
2653+
});
2654+
2655+
const result = await executePreparedCliRun(context);
2656+
2657+
expect(result.didDeliverSourceReplyViaMessageTool === true).toBe(testCase.expected);
2658+
});
2659+
25822660
it("retains confirmed delivery for long non-streaming message calls", async () => {
25832661
const context = buildPreparedCliRunContext({ output: "text", provider: "local-cli" });
25842662
context.mcpDeliveryCapture = true;

‎src/agents/embedded-agent-messaging-extraction.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { uniqueStrings } from "@openclaw/normalization-core/string-normalization
1010
import { getChannelPlugin, normalizeChannelId } from "../channels/plugins/index.js";
1111
import type { ChannelMessageActionName } from "../channels/plugins/types.public.js";
1212
import type { OpenClawConfig } from "../config/types.openclaw.js";
13+
import { isDeliveredCurrentSourceReply } from "../infra/outbound/source-reply-mirror.js";
1314
import { normalizeTargetForProvider } from "../infra/outbound/target-normalization.js";
1415
import {
1516
normalizeLegacyInteractiveReply,
@@ -350,3 +351,43 @@ export function extractMessagingToolSendResult(
350351
threadSuppressed: threadEvidence.threadSuppressed === true ? true : undefined,
351352
};
352353
}
354+
355+
export function isDeliveredMessagingToolSendToCurrentSource(params: {
356+
send: MessagingToolSend | undefined;
357+
config?: OpenClawConfig;
358+
currentProvider?: string;
359+
currentAccountId?: string;
360+
currentChannelId?: string;
361+
currentMessagingTarget?: string;
362+
currentThreadId?: string;
363+
sessionKey?: string;
364+
deliveredPayload?: unknown;
365+
}): boolean {
366+
const send = params.send;
367+
if (!send?.to) {
368+
return false;
369+
}
370+
return isDeliveredCurrentSourceReply({
371+
action: "send",
372+
channel: send.provider,
373+
accountId: send.accountId,
374+
currentAccountId: params.currentAccountId,
375+
actionParams: {
376+
target: send.to,
377+
...(send.threadSuppressed
378+
? { topLevel: true }
379+
: send.threadId
380+
? { threadId: send.threadId }
381+
: {}),
382+
},
383+
cfg: params.config ?? {},
384+
sessionKey: params.sessionKey,
385+
toolContext: {
386+
currentChannelProvider: params.currentProvider,
387+
currentChannelId: params.currentChannelId,
388+
currentMessagingTarget: params.currentMessagingTarget,
389+
currentThreadTs: params.currentThreadId,
390+
},
391+
deliveredPayload: params.deliveredPayload,
392+
});
393+
}

‎src/agents/embedded-agent-runner/run/attempt-session-prepare.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ export async function prepareEmbeddedAttemptAgentSession(input: {
231231
agent: activeSession.agent,
232232
sourceReplyDeliveryMode: attempt.sourceReplyDeliveryMode,
233233
onDeliveredSourceReply: markSourceReplyDelivered,
234+
config: attempt.config,
235+
currentProvider: attempt.messageChannel ?? attempt.messageProvider,
236+
currentAccountId: attempt.agentAccountId,
237+
currentChannelId: attempt.currentChannelId,
238+
currentMessagingTarget: attempt.currentMessagingTarget,
239+
currentThreadId: attempt.currentThreadTs,
240+
currentMessageId: attempt.currentMessageId,
241+
replyToMode: attempt.replyToMode,
242+
hasRepliedRef: attempt.hasRepliedRef,
243+
sessionKey: attempt.sessionKey,
234244
});
235245
if (input.clientToolPreparation.codeModeControlsEnabledForRun) {
236246
installCodeModeRepairHook({

‎src/agents/embedded-agent-runner/run/attempt-stream-prepare.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ export function prepareEmbeddedAttemptStream(input: {
319319
sessionKey: attempt.sessionKey,
320320
currentChannelId: attempt.currentChannelId,
321321
currentMessagingTarget: attempt.currentMessagingTarget,
322+
currentAccountId: attempt.agentAccountId,
322323
currentThreadId: attempt.currentThreadTs,
323324
currentMessageId: attempt.currentMessageId,
324325
replyToMode: attempt.replyToMode,

‎src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,74 @@ describe("message-tool-only source replies", () => {
238238
).resolves.toBeUndefined();
239239
});
240240

241+
it.each([
242+
{
243+
label: "the exact source route",
244+
accountId: "account-1",
245+
target: "chat123",
246+
threadId: "thread-1",
247+
expected: true,
248+
},
249+
{
250+
label: "the same target in another account",
251+
accountId: "account-2",
252+
target: "chat123",
253+
threadId: "thread-1",
254+
expected: false,
255+
},
256+
{
257+
label: "the same target in another thread",
258+
accountId: "account-1",
259+
target: "chat123",
260+
threadId: "thread-2",
261+
expected: false,
262+
},
263+
{
264+
label: "another target",
265+
accountId: "account-1",
266+
target: "chat456",
267+
threadId: "thread-1",
268+
expected: false,
269+
},
270+
])("records explicit sends only for $label", async (testCase) => {
271+
const agent = {} as unknown as Agent;
272+
const onDeliveredSourceReply = vi.fn();
273+
installMessageToolOnlyTerminalHook({
274+
agent,
275+
sourceReplyDeliveryMode: "message_tool_only",
276+
onDeliveredSourceReply,
277+
config: {},
278+
currentProvider: "test-channel",
279+
currentAccountId: "account-1",
280+
currentChannelId: "chat123",
281+
currentThreadId: "thread-1",
282+
sessionKey: "agent:main:test-channel:chat123",
283+
} as Parameters<typeof installMessageToolOnlyTerminalHook>[0] & {
284+
config: object;
285+
currentProvider: string;
286+
currentAccountId: string;
287+
currentChannelId: string;
288+
currentThreadId: string;
289+
sessionKey: string;
290+
});
291+
292+
await agent.afterToolCall?.(
293+
createAfterToolCallContext({
294+
toolName: "message",
295+
args: {
296+
action: "send",
297+
channel: "test-channel",
298+
accountId: testCase.accountId,
299+
target: testCase.target,
300+
threadId: testCase.threadId,
301+
message: "explicit reply",
302+
},
303+
}),
304+
);
305+
306+
expect(onDeliveredSourceReply.mock.calls.length > 0).toBe(testCase.expected);
307+
});
308+
241309
it("leaves existing after-tool-call output alone when the send failed", async () => {
242310
const previousAfterToolCall = vi.fn(async () => ({
243311
content: [{ type: "text" as const, text: "failed" }],

0 commit comments

Comments
 (0)