Skip to content

Commit c8d92eb

Browse files
Lucas Teixeira Campos AraujoAurora
andcommitted
fix(agents): selective final replies filter for tool-only mode
Replace the blanket isToolOnlyMode ? [] : replies skip with a selective filter using a shared isNonTextVisibleFinal() helper. Text-only finals are suppressed while media, error, interactive, and channelData finals pass through. Applied to both non-ACP and ACP delivery paths for consistency. Addresses Codex review finding on PR #57914. Co-authored-by: Aurora <[email protected]>
1 parent 518d5e8 commit c8d92eb

6 files changed

Lines changed: 186 additions & 10 deletions

File tree

‎src/auto-reply/reply/dispatch-acp-delivery.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,32 @@ describe("tool-only replyMode filtering", () => {
338338
});
339339
});
340340

341+
it("delivers final with interactive blocks when replyMode is tool-only", async () => {
342+
const { coordinator, dispatcher } = createToolOnlyCoordinator("tool-only");
343+
344+
const payload = {
345+
interactive: {
346+
blocks: [{ type: "buttons" as const, buttons: [{ label: "OK", value: "ok" }] }],
347+
},
348+
};
349+
const delivered = await coordinator.deliver("final", payload, { skipTts: true });
350+
351+
expect(delivered).toBe(true);
352+
expect(dispatcher.sendFinalReply).toHaveBeenCalledWith(payload);
353+
});
354+
355+
it("delivers final with channelData when replyMode is tool-only", async () => {
356+
const { coordinator, dispatcher } = createToolOnlyCoordinator("tool-only");
357+
358+
const payload = {
359+
channelData: { telegram: { reply_markup: { inline_keyboard: [] } } },
360+
};
361+
const delivered = await coordinator.deliver("final", payload, { skipTts: true });
362+
363+
expect(delivered).toBe(true);
364+
expect(dispatcher.sendFinalReply).toHaveBeenCalledWith(payload);
365+
});
366+
341367
it("delivers block text when replyMode is undefined (auto behavior preserved)", async () => {
342368
const { coordinator, dispatcher } = createToolOnlyCoordinator(undefined);
343369

‎src/auto-reply/reply/dispatch-acp-delivery.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { FinalizedMsgContext } from "../templating.js";
88
import type { ReplyPayload } from "../types.js";
99
import type { ReplyDispatcher, ReplyDispatchKind } from "./reply-dispatcher.js";
1010
import { routeReply } from "./route-reply.js";
11+
import { isNonTextVisibleFinal } from "./tool-only-filter.js";
1112

1213
export type AcpDispatchDeliveryMeta = {
1314
toolCallId?: string;
@@ -190,22 +191,19 @@ export function createAcpDispatchDeliveryCoordinator(params: {
190191
return false;
191192
}
192193

193-
// ── tool-only mode: suppress everything except media/error finals ──
194+
// ── tool-only mode: suppress everything except non-text visible finals (media, error, interactive, channelData) ──
194195
// Block streaming text, ACP tool summaries (auto-generated narration),
195-
// and text-only finals. Allow media-bearing finals and error finals through.
196+
// and text-only finals. Allow non-text visible finals through
197+
// (media, error, interactive, channelData).
196198
if (params.replyMode === "tool-only") {
197199
if (kind === "block") {
198200
return false;
199201
}
200202
if (kind === "tool") {
201203
return false;
202204
}
203-
if (kind === "final") {
204-
const hasMedia = Boolean(payload.mediaUrl || payload.mediaUrls?.length);
205-
const isError = Boolean(payload.isError);
206-
if (!hasMedia && !isError) {
207-
return false;
208-
}
205+
if (kind === "final" && !isNonTextVisibleFinal(payload)) {
206+
return false;
209207
}
210208
}
211209

‎src/auto-reply/reply/dispatch-from-config.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,6 +2898,60 @@ describe("dispatchReplyFromConfig", () => {
28982898
expect(blockReplySentTexts).not.toContain("Reasoning:\n_thinking..._");
28992899
expect(blockReplySentTexts).toContain("The answer is 42");
29002900
});
2901+
2902+
// ── tool-only replyMode: final replies filter (non-ACP path) ──
2903+
2904+
it("tool-only mode suppresses text-only finals in group context", async () => {
2905+
setNoAbort();
2906+
const dispatcher = createDispatcher();
2907+
const ctx = buildTestCtx({ Provider: "whatsapp", ChatType: "group" });
2908+
const cfg = {
2909+
agents: { defaults: { replyMode: "tool-only" } },
2910+
} as unknown as OpenClawConfig;
2911+
const replyResolver = async () => ({ text: "plain text reply" }) satisfies ReplyPayload;
2912+
await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
2913+
expect(dispatcher.sendFinalReply).not.toHaveBeenCalled();
2914+
});
2915+
2916+
it("tool-only mode delivers interactive-only finals in group context", async () => {
2917+
setNoAbort();
2918+
const dispatcher = createDispatcher();
2919+
const ctx = buildTestCtx({ Provider: "whatsapp", ChatType: "group" });
2920+
const cfg = {
2921+
agents: { defaults: { replyMode: "tool-only" } },
2922+
} as unknown as OpenClawConfig;
2923+
const replyResolver = async () =>
2924+
({
2925+
interactive: { blocks: [{ type: "buttons" as const, buttons: [{ label: "Ok", value: "ok" }] }] },
2926+
}) satisfies ReplyPayload;
2927+
await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
2928+
expect(dispatcher.sendFinalReply).toHaveBeenCalledTimes(1);
2929+
});
2930+
2931+
it("tool-only mode delivers media finals in group context", async () => {
2932+
setNoAbort();
2933+
const dispatcher = createDispatcher();
2934+
const ctx = buildTestCtx({ Provider: "whatsapp", ChatType: "group" });
2935+
const cfg = {
2936+
agents: { defaults: { replyMode: "tool-only" } },
2937+
} as unknown as OpenClawConfig;
2938+
const replyResolver = async () =>
2939+
({ mediaUrl: "https://example.com/image.png" }) satisfies ReplyPayload;
2940+
await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
2941+
expect(dispatcher.sendFinalReply).toHaveBeenCalledTimes(1);
2942+
});
2943+
2944+
it("auto mode still delivers text-only finals in group context", async () => {
2945+
setNoAbort();
2946+
const dispatcher = createDispatcher();
2947+
const ctx = buildTestCtx({ Provider: "whatsapp", ChatType: "group" });
2948+
const cfg = {
2949+
agents: { defaults: { replyMode: "auto" } },
2950+
} as unknown as OpenClawConfig;
2951+
const replyResolver = async () => ({ text: "plain text reply" }) satisfies ReplyPayload;
2952+
await dispatchReplyFromConfig({ ctx, cfg, dispatcher, replyResolver });
2953+
expect(dispatcher.sendFinalReply).toHaveBeenCalledTimes(1);
2954+
});
29012955
});
29022956

29032957
describe("before_dispatch hook", () => {

‎src/auto-reply/reply/dispatch-from-config.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import type { BlockReplyContext, GetReplyOptions, ReplyPayload } from "../types.
4646
import { shouldSkipDuplicateInbound } from "./inbound-dedupe.js";
4747
import type { ReplyDispatcher, ReplyDispatchKind } from "./reply-dispatcher.js";
4848
import { resolveReplyRoutingDecision } from "./routing-policy.js";
49+
import { isNonTextVisibleFinal } from "./tool-only-filter.js";
4950
import { resolveRunTypingPolicy } from "./typing-policy.js";
5051

5152
let routeReplyRuntimePromise: Promise<typeof import("./route-reply.runtime.js")> | null = null;
@@ -678,7 +679,11 @@ export async function dispatchReplyFromConfig(params: {
678679
const isExecApprovalPayload =
679680
(cd?.execApproval && typeof cd.execApproval === "object") ||
680681
(cd?.execApprovalUnavailable && typeof cd.execApprovalUnavailable === "object");
681-
if ((cfg.agents?.defaults?.replyMode ?? "auto") === "tool-only" && !hasMedia && !isExecApprovalPayload) {
682+
if (
683+
(cfg.agents?.defaults?.replyMode ?? "auto") === "tool-only" &&
684+
!hasMedia &&
685+
!isExecApprovalPayload
686+
) {
682687
return;
683688
}
684689
const ttsPayload = await maybeApplyTtsToPayload({
@@ -778,12 +783,17 @@ export async function dispatchReplyFromConfig(params: {
778783

779784
let queuedFinal = false;
780785
let routedFinalCount = 0;
781-
for (const reply of isToolOnlyMode ? [] : replies) {
786+
for (const reply of replies) {
782787
// Suppress reasoning payloads from channel delivery — channels using this
783788
// generic dispatch path do not have a dedicated reasoning lane.
784789
if (reply.isReasoning === true) {
785790
continue;
786791
}
792+
793+
// replyMode "tool-only": skip text-only finals; deliver media/error/interactive/channelData
794+
if (isToolOnlyMode && !isNonTextVisibleFinal(reply)) {
795+
continue;
796+
}
787797
const finalReply = await sendFinalPayload(reply);
788798
queuedFinal = finalReply.queuedFinal || queuedFinal;
789799
routedFinalCount += finalReply.routedFinalCount;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isNonTextVisibleFinal } from "./tool-only-filter.js";
3+
4+
describe("isNonTextVisibleFinal", () => {
5+
it("returns false for text-only payload", () => {
6+
expect(isNonTextVisibleFinal({ text: "hello" })).toBe(false);
7+
});
8+
9+
it("returns false for empty payload", () => {
10+
expect(isNonTextVisibleFinal({})).toBe(false);
11+
});
12+
13+
it("returns true when mediaUrl is present", () => {
14+
expect(isNonTextVisibleFinal({ mediaUrl: "https://example.com/img.png" })).toBe(true);
15+
});
16+
17+
it("returns true when mediaUrls is present", () => {
18+
expect(isNonTextVisibleFinal({ mediaUrls: ["a.png", "b.png"] })).toBe(true);
19+
});
20+
21+
it("returns true when isError is set", () => {
22+
expect(isNonTextVisibleFinal({ text: "failed", isError: true })).toBe(true);
23+
});
24+
25+
it("returns true when interactive blocks are present", () => {
26+
expect(
27+
isNonTextVisibleFinal({
28+
interactive: {
29+
blocks: [{ type: "buttons", buttons: [{ label: "OK", value: "ok" }] }],
30+
},
31+
}),
32+
).toBe(true);
33+
});
34+
35+
it("returns false when interactive is empty object", () => {
36+
expect(isNonTextVisibleFinal({ interactive: {} as never })).toBe(false);
37+
});
38+
39+
it("returns true when channelData is non-empty", () => {
40+
expect(
41+
isNonTextVisibleFinal({
42+
channelData: { telegram: { reply_markup: {} } },
43+
}),
44+
).toBe(true);
45+
});
46+
47+
it("returns false when channelData is empty object", () => {
48+
expect(isNonTextVisibleFinal({ channelData: {} })).toBe(false);
49+
});
50+
51+
it("returns true when multiple non-text fields are present", () => {
52+
expect(
53+
isNonTextVisibleFinal({
54+
text: "result",
55+
mediaUrl: "https://example.com/img.png",
56+
isError: true,
57+
}),
58+
).toBe(true);
59+
});
60+
61+
it("returns false for compaction notice (text-only)", () => {
62+
expect(isNonTextVisibleFinal({ text: "Compacting context...", isCompactionNotice: true })).toBe(
63+
false,
64+
);
65+
});
66+
67+
it("returns false for reasoning payload (text-only)", () => {
68+
expect(isNonTextVisibleFinal({ text: "thinking...", isReasoning: true })).toBe(false);
69+
});
70+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { hasInteractiveReplyBlocks, hasReplyChannelData } from "../../interactive/payload.js";
2+
import type { ReplyPayload } from "../types.js";
3+
4+
/**
5+
* Returns true if a final reply payload carries non-text visible content
6+
* that should be delivered even when replyMode is "tool-only".
7+
*
8+
* Tool-only mode suppresses plain-text assistant narration to prevent
9+
* leakage in shared sessions, but media, errors, interactive elements,
10+
* and channelData payloads are legitimate user-visible output.
11+
*/
12+
export function isNonTextVisibleFinal(payload: ReplyPayload): boolean {
13+
const hasMedia = Boolean(payload.mediaUrl || payload.mediaUrls?.length);
14+
const isError = Boolean(payload.isError);
15+
const hasInteractive = hasInteractiveReplyBlocks(payload.interactive);
16+
const hasChannelData = hasReplyChannelData(payload.channelData);
17+
return hasMedia || isError || hasInteractive || hasChannelData;
18+
}

0 commit comments

Comments
 (0)