Skip to content

Commit f0093b7

Browse files
fix(auto-reply): surface empty interactive completions (#100456)
* fix(auto-reply): surface empty interactive completions Co-authored-by: mushuiyu886 <[email protected]> * chore(changelog): defer empty-reply release note --------- Co-authored-by: mushuiyu886 <[email protected]>
1 parent a4b032e commit f0093b7

9 files changed

Lines changed: 840 additions & 91 deletions

‎src/agents/embedded-agent-runner/result-fallback-classifier.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function mergeEmbeddedAgentRunResultForModelFallbackExhaustion(params: {
7272
};
7373
}
7474

75-
function hasDeliberateSilentTerminalReply(result: EmbeddedAgentRunResult): boolean {
75+
export function hasDeliberateSilentTerminalReply(result: EmbeddedAgentRunResult): boolean {
7676
if (result.meta.error?.kind === "hook_block") {
7777
return true;
7878
}

‎src/auto-reply/reply/agent-runner-execution.ts‎

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,12 @@ type ExternalRunFailureReply = {
806806

807807
type ExternalRunFailureInput = string | { message: string; error?: unknown };
808808

809-
function isNonDirectConversationContext(ctx: TemplateContext): boolean {
809+
type ExternalFailureConversationContext = Pick<
810+
TemplateContext,
811+
"ChatType" | "Provider" | "SessionKey" | "Surface"
812+
>;
813+
814+
function isNonDirectConversationContext(ctx: ExternalFailureConversationContext): boolean {
810815
const chatType = normalizeLowercaseStringOrEmpty(ctx.ChatType);
811816
return chatType === "group" || chatType === "channel";
812817
}
@@ -817,7 +822,7 @@ function isVerboseFailureDetailEnabled(level: VerboseLevel | undefined): boolean
817822

818823
function resolveExternalRunFailureTextForConversation(params: {
819824
text: string;
820-
sessionCtx: TemplateContext;
825+
sessionCtx: ExternalFailureConversationContext;
821826
isGenericRunnerFailure: boolean;
822827
cfg?: OpenClawConfig;
823828
}): string {
@@ -1047,6 +1052,57 @@ function markAgentRunFailureReplyPayload<T extends ReplyPayload>(payload: T): T
10471052
return marked;
10481053
}
10491054

1055+
export function buildTerminalAgentRunFailureReplyPayload(params: {
1056+
isHeartbeat?: boolean;
1057+
sessionCtx: ExternalFailureConversationContext;
1058+
cfg?: OpenClawConfig;
1059+
}): ReplyPayload {
1060+
return markAgentRunFailureReplyPayload({
1061+
text: resolveExternalRunFailureTextForConversation({
1062+
text: params.isHeartbeat
1063+
? HEARTBEAT_EXTERNAL_RUN_FAILURE_TEXT
1064+
: GENERIC_EXTERNAL_RUN_FAILURE_TEXT,
1065+
sessionCtx: params.sessionCtx,
1066+
isGenericRunnerFailure: true,
1067+
cfg: params.cfg,
1068+
}),
1069+
});
1070+
}
1071+
1072+
export function buildEmptyInteractiveReplyPayload(params: {
1073+
isInteractive: boolean;
1074+
isHeartbeat?: boolean;
1075+
silentExpected?: boolean;
1076+
allowEmptyAssistantReplyAsSilent?: boolean;
1077+
isMessageToolOnly: boolean;
1078+
hasPendingContinuation: boolean;
1079+
hasExplicitSilentReply: boolean;
1080+
hasCommittedDelivery: boolean;
1081+
sessionCtx: ExternalFailureConversationContext;
1082+
cfg?: OpenClawConfig;
1083+
}): ReplyPayload | undefined {
1084+
if (
1085+
!params.isInteractive ||
1086+
params.isHeartbeat === true ||
1087+
params.silentExpected === true ||
1088+
params.allowEmptyAssistantReplyAsSilent === true ||
1089+
params.isMessageToolOnly ||
1090+
params.hasPendingContinuation ||
1091+
params.hasExplicitSilentReply ||
1092+
params.hasCommittedDelivery
1093+
) {
1094+
return undefined;
1095+
}
1096+
return markAgentRunFailureReplyPayload({
1097+
text: resolveExternalRunFailureTextForConversation({
1098+
text: "I finished the turn, but it did not produce a visible reply. Please try again, or start a new session if this keeps happening.",
1099+
sessionCtx: params.sessionCtx,
1100+
isGenericRunnerFailure: true,
1101+
cfg: params.cfg,
1102+
}),
1103+
});
1104+
}
1105+
10501106
/** Converts known agent-run failures into user-facing reply payloads. */
10511107
export function buildKnownAgentRunFailureReplyPayload(params: {
10521108
err: unknown;
@@ -3530,15 +3586,10 @@ async function runAgentTurnWithFallbackInternal(
35303586
}
35313587
}
35323588
const terminalFailurePayload = terminalRunFailed
3533-
? markAgentRunFailureReplyPayload({
3534-
text: resolveExternalRunFailureTextForConversation({
3535-
text: params.isHeartbeat
3536-
? HEARTBEAT_EXTERNAL_RUN_FAILURE_TEXT
3537-
: GENERIC_EXTERNAL_RUN_FAILURE_TEXT,
3538-
sessionCtx: params.sessionCtx,
3539-
isGenericRunnerFailure: true,
3540-
cfg: params.followupRun.run.config,
3541-
}),
3589+
? buildTerminalAgentRunFailureReplyPayload({
3590+
isHeartbeat: params.isHeartbeat,
3591+
sessionCtx: params.sessionCtx,
3592+
cfg: params.followupRun.run.config,
35423593
})
35433594
: undefined;
35443595

‎src/auto-reply/reply/agent-runner.runreplyagent.e2e.test.ts‎

Lines changed: 146 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ type AgentRunParams = {
3838
onPartialReply?: (payload: { text?: string }) => Promise<void> | void;
3939
onAssistantMessageStart?: () => Promise<void> | void;
4040
onReasoningStream?: (payload: { text?: string }) => Promise<void> | void;
41-
onBlockReply?: (payload: { text?: string; mediaUrls?: string[] }) => Promise<void> | void;
41+
onBlockReply?: (payload: {
42+
text?: string;
43+
mediaUrls?: string[];
44+
isReasoning?: boolean;
45+
isCommentary?: boolean;
46+
}) => Promise<void> | void;
4247
onToolResult?: (payload: ReplyPayload) => Promise<void> | void;
4348
shouldEmitToolResult?: () => boolean;
4449
shouldEmitToolOutput?: () => boolean;
@@ -183,6 +188,7 @@ function createMinimalRun(params?: {
183188
shouldSteer?: boolean;
184189
shouldFollowup?: boolean;
185190
resolvedQueueMode?: string;
191+
currentInboundEventKind?: FollowupRun["currentInboundEventKind"];
186192
sessionCtx?: Partial<TemplateContext>;
187193
runOverrides?: Partial<FollowupRun["run"]>;
188194
}) {
@@ -201,6 +207,7 @@ function createMinimalRun(params?: {
201207
prompt: "hello",
202208
summaryLine: "hello",
203209
enqueuedAt: Date.now(),
210+
currentInboundEventKind: params?.currentInboundEventKind,
204211
run: {
205212
sessionId: "session",
206213
sessionKey,
@@ -1409,6 +1416,108 @@ describe("runReplyAgent typing (heartbeat)", () => {
14091416
});
14101417
});
14111418

1419+
it.each([
1420+
{ label: "empty output", payloads: [] },
1421+
{ label: "reasoning-only output", payloads: [{ text: "internal", isReasoning: true }] },
1422+
{ label: "commentary-only output", payloads: [{ text: "internal", isCommentary: true }] },
1423+
{ label: "directive-only output", payloads: [{ text: "[[reply_to_current]]" }] },
1424+
])("surfaces successful $label through normal reply delivery", async ({ payloads }) => {
1425+
state.runEmbeddedAgentMock.mockResolvedValueOnce({ payloads, meta: {} });
1426+
const { run } = createMinimalRun({
1427+
runOverrides: { config: { channels: { whatsapp: { replyToMode: "first" } } } },
1428+
});
1429+
1430+
const result = await run();
1431+
const payloadsResult = Array.isArray(result) ? result : [result];
1432+
1433+
expect(payloadsResult).toContainEqual(
1434+
expect.objectContaining({
1435+
text: expect.stringContaining("did not produce a visible reply"),
1436+
isError: true,
1437+
replyToId: "msg",
1438+
}),
1439+
);
1440+
});
1441+
1442+
it.each([
1443+
{ lane: "reasoning", payload: { text: "internal", isReasoning: true } },
1444+
{ lane: "commentary", payload: { text: "internal", isCommentary: true } },
1445+
])("does not let streamed $lane suppress the empty-reply fallback", async ({ payload }) => {
1446+
const onBlockReply = vi.fn();
1447+
state.runEmbeddedAgentMock.mockImplementationOnce(async (params: AgentRunParams) => {
1448+
await params.onBlockReply?.(payload);
1449+
return { payloads: [], meta: {} };
1450+
});
1451+
const { run } = createMinimalRun({
1452+
blockStreamingEnabled: true,
1453+
opts: {
1454+
onBlockReply,
1455+
reasoningPayloadsEnabled: true,
1456+
commentaryPayloadsEnabled: true,
1457+
},
1458+
});
1459+
1460+
const result = await run();
1461+
const payloads = Array.isArray(result) ? result : [result];
1462+
1463+
expect(onBlockReply).toHaveBeenCalled();
1464+
expect(onBlockReply.mock.calls[0]?.[0]).toEqual(expect.objectContaining(payload));
1465+
expect(payloads).toContainEqual(
1466+
expect.objectContaining({
1467+
text: expect.stringContaining("did not produce a visible reply"),
1468+
isError: true,
1469+
}),
1470+
);
1471+
});
1472+
1473+
it.each([
1474+
{
1475+
label: "NO_REPLY",
1476+
result: {
1477+
payloads: [{ text: "NO_REPLY" }],
1478+
meta: { finalAssistantVisibleText: "NO_REPLY" },
1479+
},
1480+
},
1481+
{
1482+
label: "accepted child spawn",
1483+
result: {
1484+
payloads: [],
1485+
meta: {},
1486+
acceptedSessionSpawns: [{ runId: "child", childSessionKey: "agent:main:child" }],
1487+
},
1488+
},
1489+
{ label: "yielded continuation", result: { payloads: [], meta: { yielded: true } } },
1490+
{
1491+
label: "pending tool continuation",
1492+
result: { payloads: [], meta: { pendingToolCalls: [{ name: "hosted_tool" }] } },
1493+
},
1494+
])("keeps successful $label completions silent", async ({ result }) => {
1495+
state.runEmbeddedAgentMock.mockResolvedValueOnce(result);
1496+
const { run } = createMinimalRun();
1497+
1498+
await expect(run()).resolves.toBeUndefined();
1499+
});
1500+
1501+
it.each([
1502+
{
1503+
label: "room event",
1504+
params: { currentInboundEventKind: "room_event" as const },
1505+
},
1506+
{
1507+
label: "internal handoff",
1508+
params: {
1509+
runOverrides: {
1510+
inputProvenance: { kind: "internal_system" as const, sourceTool: "restart-sentinel" },
1511+
},
1512+
},
1513+
},
1514+
])("keeps successful empty $label completions silent", async ({ params }) => {
1515+
state.runEmbeddedAgentMock.mockResolvedValueOnce({ payloads: [], meta: {} });
1516+
const { run } = createMinimalRun(params);
1517+
1518+
await expect(run()).resolves.toBeUndefined();
1519+
});
1520+
14121521
it.each([
14131522
{
14141523
label: "silent token",
@@ -1750,10 +1859,40 @@ describe("runReplyAgent typing (heartbeat)", () => {
17501859
}
17511860
});
17521861

1753-
it("surfaces a configured backend failure when fallback produces no visible reply", async () => {
1754-
state.runEmbeddedAgentMock.mockResolvedValueOnce({
1755-
payloads: [{ text: "NO_REPLY" }],
1756-
meta: {},
1862+
it.each([
1863+
{
1864+
label: "NO_REPLY",
1865+
payload: { text: "NO_REPLY" },
1866+
opts: undefined,
1867+
streamed: false,
1868+
},
1869+
{
1870+
label: "reasoning-only output with reasoning enabled",
1871+
payload: { text: "internal reasoning", isReasoning: true },
1872+
opts: { reasoningPayloadsEnabled: true },
1873+
streamed: false,
1874+
},
1875+
{
1876+
label: "streamed reasoning-only output with reasoning enabled",
1877+
payload: { text: "internal reasoning", isReasoning: true },
1878+
opts: { reasoningPayloadsEnabled: true, onBlockReply: vi.fn() },
1879+
streamed: true,
1880+
},
1881+
{
1882+
label: "commentary-only output with commentary enabled",
1883+
payload: { text: "internal commentary", isCommentary: true },
1884+
opts: { commentaryPayloadsEnabled: true },
1885+
streamed: false,
1886+
},
1887+
])("surfaces a configured backend failure when fallback produces $label", async (testCase) => {
1888+
state.runEmbeddedAgentMock.mockImplementationOnce(async (params: AgentRunParams) => {
1889+
if (testCase.streamed) {
1890+
await params.onBlockReply?.(testCase.payload);
1891+
}
1892+
return {
1893+
payloads: [testCase.payload],
1894+
meta: {},
1895+
};
17571896
});
17581897
const fallbackSpy = vi
17591898
.spyOn(modelFallbackModule, "runWithModelFallback")
@@ -1776,6 +1915,8 @@ describe("runReplyAgent typing (heartbeat)", () => {
17761915

17771916
try {
17781917
const { run } = createMinimalRun({
1918+
opts: testCase.opts,
1919+
blockStreamingEnabled: testCase.streamed,
17791920
runOverrides: {
17801921
provider: "lmstudio",
17811922
model: "gemma-4-e4b-it",

0 commit comments

Comments
 (0)