Skip to content

Commit 8cc647d

Browse files
Prompt DrivenPrompt Driven
authored andcommitted
incomplete-turn: don't surface 'couldn't generate' for silent replies
Follow-on to PR #69472 (auto-reply silent-reply suppressor for reasoning- prefaced sentinels like "think\n...\nNO_REPLY"). The outbound suppressor correctly stripped these from delivery, but the runner's incomplete-turn classifier and three retry resolvers (planning-only / reasoning-only / empty-response) saw payloadCount=0 + assistantTexts=[] and re-prompted the model on every spam decision. Each retry burned LLM tokens and the final classifier surfaced a "Agent couldn't generate a response" warning to the channel even though the original silence was intentional. Three minimal call-site changes in src/agents/pi-embedded-runner/run/ incomplete-turn.ts: - hasOnlySilentAssistantReply now also reads lastAssistant.content text parts (assistantTexts can be empty when streaming/chunker filters drop the assistant text before the run wrapper records it). - isReasoningOnlyAssistantTurn returns false when the message text is a silent reply payload — silent NO_REPLY is intentional silence, not an unfinished reasoning turn that should be retried. - isEmptyResponseAssistantTurn returns false for the same reason — gates the empty-response retry resolver against firing on intentional silence. Regression test in run.incomplete-turn.test.ts uses the exact Discord session payload that triggered this on April 24 and 27. Closes #69470 (follow-on)
1 parent 6388f29 commit 8cc647d

2 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎src/agents/pi-embedded-runner/run.incomplete-turn.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,49 @@ describe("runEmbeddedPiAgent incomplete-turn safety", () => {
7878
expect(result.payloads?.[0]?.text).toContain("verify before retrying");
7979
});
8080

81+
it("does not surface couldn't-generate when last assistant is a reasoning-prefaced silent reply", async () => {
82+
// Regression for the false-positive where Gemini emits its chain-of-thought
83+
// as a "think\n...\nNO_REPLY" plain-text payload. The outbound silent-reply
84+
// suppressor strips the assistant text, leaving `assistantTexts: []`. The
85+
// classifier must consult `lastAssistant.content` directly so it does not
86+
// surface "Agent couldn't generate a response" for an intentional silence.
87+
mockedClassifyFailoverReason.mockReturnValue(null);
88+
mockedRunEmbeddedAttempt.mockResolvedValueOnce(
89+
makeAttemptResult({
90+
assistantTexts: [],
91+
toolMetas: [],
92+
didSendViaMessagingTool: false,
93+
lastAssistant: {
94+
stopReason: "stop",
95+
provider: "google-vertex",
96+
model: "gemini-3.1-pro-preview",
97+
content: [
98+
{
99+
type: "text",
100+
text: "think\nAnother advertisement. Ignore.NO_REPLY",
101+
},
102+
],
103+
} as unknown as EmbeddedRunAttemptResult["lastAssistant"],
104+
}),
105+
);
106+
107+
const result = await runEmbeddedPiAgent({
108+
...overflowBaseRunParams,
109+
provider: "google-vertex",
110+
model: "gemini-3.1-pro-preview",
111+
runId: "run-incomplete-turn-reasoning-prefaced-silent",
112+
});
113+
114+
expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(1);
115+
const payloads = result.payloads ?? [];
116+
expect(payloads).not.toContainEqual(
117+
expect.objectContaining({
118+
isError: true,
119+
text: expect.stringContaining("couldn't generate a response"),
120+
}),
121+
);
122+
});
123+
81124
it("synthesizes a silent cron payload from a trailing current-attempt NO_REPLY tool result", () => {
82125
const payload = resolveSilentToolResultReplyPayload({
83126
isCronTrigger: true,

‎src/agents/pi-embedded-runner/run/incomplete-turn.ts‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export function resolveIncompleteTurnPayloadText(params: {
240240
return null;
241241
}
242242

243-
if (hasOnlySilentAssistantReply(params.attempt.assistantTexts)) {
243+
if (hasOnlySilentAssistantReply(params.attempt.assistantTexts, params.attempt.lastAssistant)) {
244244
return null;
245245
}
246246

@@ -278,14 +278,51 @@ function joinAssistantTexts(assistantTexts?: readonly string[]): string {
278278
return (assistantTexts ?? []).join("\n\n").trim();
279279
}
280280

281-
function hasOnlySilentAssistantReply(assistantTexts?: readonly string[]): boolean {
282-
const nonEmptyTexts = (assistantTexts ?? []).filter((text) => text.trim().length > 0);
281+
function hasOnlySilentAssistantReply(
282+
assistantTexts?: readonly string[],
283+
lastAssistant?: AssistantMessage | null,
284+
): boolean {
285+
const candidates = collectAssistantSilentReplyCandidates(assistantTexts, lastAssistant);
283286
return (
284-
nonEmptyTexts.length > 0 &&
285-
nonEmptyTexts.every((text) => isSilentReplyPayloadText(text, SILENT_REPLY_TOKEN))
287+
candidates.length > 0 &&
288+
candidates.every((text) => isSilentReplyPayloadText(text, SILENT_REPLY_TOKEN))
286289
);
287290
}
288291

292+
/**
293+
* Collects candidate assistant text strings that should be checked against
294+
* `isSilentReplyPayloadText`.
295+
*
296+
* Pulls from two sources: `assistantTexts` (the openclaw run wrapper's
297+
* post-chunker text collection — can be empty when streaming/chunking
298+
* filters drop the assistant text before it reaches the wrapper) and
299+
* `lastAssistant.content` text parts (the raw model output recorded in the
300+
* session). The session text is the authoritative source for "did the
301+
* assistant say a silent-reply sentinel?", and consulting it prevents the
302+
* incomplete-turn warning from surfacing as a false positive on
303+
* reasoning-prefaced silent replies (e.g. "think\n...\nNO_REPLY") that the
304+
* outbound silent-reply suppressor correctly stripped from delivery.
305+
*/
306+
function collectAssistantSilentReplyCandidates(
307+
assistantTexts?: readonly string[],
308+
lastAssistant?: AssistantMessage | null,
309+
): string[] {
310+
const collected: string[] = [];
311+
for (const text of assistantTexts ?? []) {
312+
if (typeof text === "string" && text.trim().length > 0) {
313+
collected.push(text);
314+
}
315+
}
316+
const lastAssistantText =
317+
lastAssistant && typeof lastAssistant === "object"
318+
? readMessageTextContent(lastAssistant as AgentMessage)
319+
: undefined;
320+
if (lastAssistantText && lastAssistantText.trim().length > 0) {
321+
collected.push(lastAssistantText);
322+
}
323+
return collected;
324+
}
325+
289326
function isToolResultRole(role: string): boolean {
290327
return role === "toolresult" || role === "tool_result" || role === "tool";
291328
}
@@ -402,6 +439,17 @@ export function isReasoningOnlyAssistantTurn(message: unknown): boolean {
402439
if (!message || typeof message !== "object") {
403440
return false;
404441
}
442+
// A reasoning-prefaced silent reply (for example "think\n...\nNO_REPLY"
443+
// from Gemini's chain-of-thought leaking as plain text) is *intentional
444+
// silence*, not a model that "didn't finish thinking." Without this guard
445+
// the empty-response/reasoning-only/planning-only retry resolvers each
446+
// re-trigger the model and the incomplete-turn classifier surfaces a
447+
// "couldn't generate a response" warning even though the outbound
448+
// silent-reply suppressor correctly stripped the noise from delivery.
449+
const text = readMessageTextContent(message as AgentMessage);
450+
if (text && isSilentReplyPayloadText(text, SILENT_REPLY_TOKEN)) {
451+
return false;
452+
}
405453
return assessLastAssistantMessage(message as AgentMessage) === "incomplete-text";
406454
}
407455

@@ -425,6 +473,13 @@ function isEmptyResponseAssistantTurn(params: {
425473
if (assistant.stopReason === "error") {
426474
return false;
427475
}
476+
// Reasoning-prefaced silent reply (for example "think\n...\nNO_REPLY") is
477+
// intentional silence. Treat it the same as an exact NO_REPLY so the
478+
// empty-response retry path doesn't burn tokens re-prompting Gemini.
479+
const assistantText = readMessageTextContent(assistant as AgentMessage);
480+
if (assistantText && isSilentReplyPayloadText(assistantText, SILENT_REPLY_TOKEN)) {
481+
return false;
482+
}
428483
if (
429484
isIncompleteTerminalAssistantTurn({
430485
hasAssistantVisibleText: false,

0 commit comments

Comments
 (0)