Skip to content

Commit 6b034b1

Browse files
Prompt DrivenPrompt Driven
authored andcommitted
auto-reply: suppress reasoning-prefaced silent replies
When a model emits its chain-of-thought as plain text content (a bare "think"/"thinking"/"thought"/"reasoning"/"analysis" heading followed by prose) and concludes with the NO_REPLY sentinel, the channel-agnostic silent-reply gate at src/auto-reply/reply/reply-directives.ts previously treated the whole message as substantive and delivered it to users. Add isReasoningPrefacedSilentReply, wire it into isSilentReplyPayloadText, and include a dedicated tolerant trailing-strip helper so that doubled token forms like "NO_REPLY.NO_REPLY" (observed with Gemini 3.x) collapse. stripSilentToken keeps its existing conservative contract so no other caller behavior shifts (preserves the "interject.NO_REPLY" no-op test). Covered by new unit tests in src/auto-reply/tokens.test.ts. Closes #69470
1 parent de404de commit 6b034b1

2 files changed

Lines changed: 173 additions & 1 deletion

File tree

‎src/auto-reply/tokens.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, it, expect } from "vitest";
22
import {
3+
isReasoningPrefacedSilentReply,
4+
isSilentReplyPayloadText,
35
isSilentReplyPrefixText,
46
isSilentReplyText,
57
startsWithSilentToken,
@@ -135,3 +137,86 @@ describe("isSilentReplyPrefixText", () => {
135137
expect(isSilentReplyPrefixText("NO-")).toBe(false);
136138
});
137139
});
140+
141+
describe("isReasoningPrefacedSilentReply", () => {
142+
it("classifies reasoning preamble + trailing NO_REPLY as silent", () => {
143+
const text =
144+
"think\n" +
145+
"The user's message is from Aftermath in the #general channel.\n" +
146+
"The message is a self-promotional advertisement.\n" +
147+
"Silence is the required action.\n" +
148+
"Therefore, I should output NO_REPLY.NO_REPLY";
149+
expect(isReasoningPrefacedSilentReply(text)).toBe(true);
150+
});
151+
152+
it("classifies single trailing NO_REPLY with reasoning preamble as silent", () => {
153+
const text = "think\nThe user is saying hello. I will not reply.\nNO_REPLY";
154+
expect(isReasoningPrefacedSilentReply(text)).toBe(true);
155+
});
156+
157+
it("accepts other reasoning heading words", () => {
158+
for (const heading of ["thinking", "thought", "reasoning", "analysis"]) {
159+
const text = `${heading}\nUser asked a trivial question.\nNO_REPLY`;
160+
expect(isReasoningPrefacedSilentReply(text)).toBe(true);
161+
}
162+
});
163+
164+
it("accepts reasoning heading with trailing colon", () => {
165+
expect(isReasoningPrefacedSilentReply("thinking:\nSome analysis.\nNO_REPLY")).toBe(true);
166+
});
167+
168+
it("collapses doubled trailing NO_REPLY forms with inner punctuation", () => {
169+
// The exact observed pattern from the bug report.
170+
expect(isReasoningPrefacedSilentReply("think\nbody\nNO_REPLY.NO_REPLY")).toBe(true);
171+
expect(isReasoningPrefacedSilentReply("think\nbody\nNO_REPLY NO_REPLY")).toBe(true);
172+
expect(isReasoningPrefacedSilentReply("think\nbody\nNO_REPLY. NO_REPLY")).toBe(true);
173+
});
174+
175+
it("preserves #19537 — substantive replies ending with NO_REPLY are not silent", () => {
176+
const substantive = "Here is the answer you asked for.\n\nNO_REPLY";
177+
expect(isReasoningPrefacedSilentReply(substantive)).toBe(false);
178+
});
179+
180+
it("returns false when message does not end with the silent token", () => {
181+
expect(isReasoningPrefacedSilentReply("think\nbody\nactual reply")).toBe(false);
182+
});
183+
184+
it("returns false for plain substantive text without reasoning preamble", () => {
185+
expect(isReasoningPrefacedSilentReply("I should reply to this. NO_REPLY")).toBe(false);
186+
});
187+
188+
it("returns false for empty or whitespace-only input", () => {
189+
expect(isReasoningPrefacedSilentReply("")).toBe(false);
190+
expect(isReasoningPrefacedSilentReply(undefined)).toBe(false);
191+
expect(isReasoningPrefacedSilentReply(" ")).toBe(false);
192+
});
193+
194+
it("returns true when only the silent token remains after trimming", () => {
195+
expect(isReasoningPrefacedSilentReply(" NO_REPLY ")).toBe(true);
196+
expect(isReasoningPrefacedSilentReply("NO_REPLY.NO_REPLY")).toBe(true);
197+
});
198+
199+
it("does not match when reasoning heading is followed inline by prose on same line", () => {
200+
// A bare heading must be on its own line; inline "think the answer is X" is
201+
// natural language and should not be suppressed.
202+
expect(isReasoningPrefacedSilentReply("think the answer is yes\nNO_REPLY")).toBe(false);
203+
});
204+
});
205+
206+
describe("isSilentReplyPayloadText integration", () => {
207+
it("returns true for reasoning-prefaced silent replies", () => {
208+
expect(isSilentReplyPayloadText("think\nanalysis goes here\nNO_REPLY.NO_REPLY")).toBe(true);
209+
});
210+
211+
it("still returns true for exact token", () => {
212+
expect(isSilentReplyPayloadText("NO_REPLY")).toBe(true);
213+
});
214+
215+
it("still returns true for JSON action envelope", () => {
216+
expect(isSilentReplyPayloadText('{"action":"NO_REPLY"}')).toBe(true);
217+
});
218+
219+
it("still returns false for substantive replies ending with NO_REPLY (#19537)", () => {
220+
expect(isSilentReplyPayloadText("Here is a helpful response.\n\nNO_REPLY")).toBe(false);
221+
});
222+
});

‎src/auto-reply/tokens.ts‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ function getSilentTrailingRegex(token: string): RegExp {
2929
return regex;
3030
}
3131

32+
const silentReasoningTrailingRegexByToken = new Map<string, RegExp>();
33+
34+
function getSilentReasoningTrailingRegex(token: string): RegExp {
35+
const cached = silentReasoningTrailingRegexByToken.get(token);
36+
if (cached) {
37+
return cached;
38+
}
39+
const escaped = escapeRegExp(token);
40+
// Dedicated, more tolerant trailing match for the reasoning-prefaced silent
41+
// reply check: allows whitespace, asterisks, or common sentence punctuation
42+
// to precede the trailing token so concluding model prose like
43+
// "Therefore, I should output NO_REPLY.NO_REPLY" collapses cleanly. Kept
44+
// separate from `getSilentTrailingRegex` to preserve the long-standing
45+
// `stripSilentToken("interject.NO_REPLY")` no-op contract.
46+
const regex = new RegExp(`(?:^|[\\s.:;,!?*]+)${escaped}\\s*$`, "i");
47+
silentReasoningTrailingRegexByToken.set(token, regex);
48+
return regex;
49+
}
50+
3251
export function isSilentReplyText(
3352
text: string | undefined,
3453
token: string = SILENT_REPLY_TOKEN,
@@ -75,7 +94,11 @@ export function isSilentReplyPayloadText(
7594
text: string | undefined,
7695
token: string = SILENT_REPLY_TOKEN,
7796
): boolean {
78-
return isSilentReplyText(text, token) || isSilentReplyEnvelopeText(text, token);
97+
return (
98+
isSilentReplyText(text, token) ||
99+
isSilentReplyEnvelopeText(text, token) ||
100+
isReasoningPrefacedSilentReply(text, token)
101+
);
79102
}
80103

81104
/**
@@ -87,6 +110,70 @@ export function stripSilentToken(text: string, token: string = SILENT_REPLY_TOKE
87110
return text.replace(getSilentTrailingRegex(token), "").trim();
88111
}
89112

113+
function stripTrailingSilentTokensTolerant(text: string, token: string): string {
114+
const regex = getSilentReasoningTrailingRegex(token);
115+
let current = text;
116+
// Iterate so doubled/tripled trailing forms (for example
117+
// "NO_REPLY.NO_REPLY" or "NO_REPLY NO_REPLY") collapse cleanly. Capped at 8
118+
// iterations — in practice the loop exits after 1-3 passes.
119+
for (let i = 0; i < 8; i++) {
120+
const next = current.replace(regex, "").trim();
121+
if (next === current) {
122+
return current;
123+
}
124+
current = next;
125+
}
126+
return current;
127+
}
128+
129+
// Matches a bare reasoning preamble at the start of the message: a word like
130+
// "think", "thinking", "thought", "reasoning", or "analysis" on its own line
131+
// (optionally followed by a colon) with nothing else on that line. Reasoning
132+
// models occasionally leak their chain-of-thought as plain text content when
133+
// structured thinking stream events are not produced; those messages must not
134+
// be treated as substantive replies when they are followed by a silent-reply
135+
// sentinel like NO_REPLY. The proper XML-tag stripper in
136+
// src/shared/text/reasoning-tags.ts cannot catch this form because there are
137+
// no tags to match.
138+
const BARE_REASONING_PREAMBLE_RE =
139+
/^\s*(?:think(?:ing)?|thought|reasoning|analysis)\s*:?\s*(?:\r?\n|$)/i;
140+
141+
/**
142+
* Whether `text` is a reasoning-prefaced silent reply — a message that ends
143+
* with the silent-reply token and whose preceding content is a bare reasoning
144+
* preamble (not substantive user-visible content).
145+
*
146+
* Preserves the #19537 semantics: substantive replies that happen to end with
147+
* NO_REPLY are still delivered. Only messages where the non-token content is
148+
* a reasoning preamble are suppressed.
149+
*/
150+
export function isReasoningPrefacedSilentReply(
151+
text: string | undefined,
152+
token: string = SILENT_REPLY_TOKEN,
153+
): boolean {
154+
if (!text) {
155+
return false;
156+
}
157+
const trimmed = text.trim();
158+
if (!trimmed) {
159+
return false;
160+
}
161+
// Use the tolerant trailing strip so concluding model prose like
162+
// "...I should output NO_REPLY.NO_REPLY" collapses even with inner
163+
// punctuation between doubled tokens.
164+
const withoutToken = stripTrailingSilentTokensTolerant(trimmed, token);
165+
// Must actually end with the silent token (stripping changed the text).
166+
if (withoutToken === trimmed) {
167+
return false;
168+
}
169+
// All content was silent tokens → nothing left to classify.
170+
if (!withoutToken) {
171+
return true;
172+
}
173+
// The remaining content must start with a bare reasoning preamble.
174+
return BARE_REASONING_PREAMBLE_RE.test(withoutToken);
175+
}
176+
90177
const silentLeadingRegexByToken = new Map<string, RegExp>();
91178

92179
function getSilentLeadingAttachedRegex(token: string): RegExp {

0 commit comments

Comments
 (0)