Files
openclaw/src/auto-reply/tokens.test.ts
Mitch McAlister f534ea9906 fix: prevent reasoning text leak through handleMessageEnd fallback
When enforceFinalTag is active (Google providers), stripBlockTags
correctly returns empty for text without <final> tags. However, the
handleMessageEnd fallback recovered raw text, bypassing this protection
and leaking internal reasoning (e.g. "**Applying single-bot mention
rule**NO_REPLY") to Discord.

Guard the fallback with enforceFinalTag check: if the provider is
supposed to use <final> tags and none were seen, the text is treated
as leaked reasoning and suppressed.

Also harden stripSilentToken regex to allow bold markdown (**) as
separator before NO_REPLY, matching the pattern Gemini Flash Lite
produces.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 20:32:01 +00:00

96 lines
3.4 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { isSilentReplyPrefixText, isSilentReplyText, stripSilentToken } from "./tokens.js";
describe("isSilentReplyText", () => {
it("returns true for exact token", () => {
expect(isSilentReplyText("NO_REPLY")).toBe(true);
});
it("returns true for token with surrounding whitespace", () => {
expect(isSilentReplyText(" NO_REPLY ")).toBe(true);
expect(isSilentReplyText("\nNO_REPLY\n")).toBe(true);
});
it("returns false for undefined/empty", () => {
expect(isSilentReplyText(undefined)).toBe(false);
expect(isSilentReplyText("")).toBe(false);
});
it("returns false for substantive text ending with token (#19537)", () => {
const text = "Here is a helpful response.\n\nNO_REPLY";
expect(isSilentReplyText(text)).toBe(false);
});
it("returns false for substantive text starting with token", () => {
const text = "NO_REPLY but here is more content";
expect(isSilentReplyText(text)).toBe(false);
});
it("returns false for token embedded in text", () => {
expect(isSilentReplyText("Please NO_REPLY to this")).toBe(false);
});
it("works with custom token", () => {
expect(isSilentReplyText("HEARTBEAT_OK", "HEARTBEAT_OK")).toBe(true);
expect(isSilentReplyText("Checked inbox. HEARTBEAT_OK", "HEARTBEAT_OK")).toBe(false);
});
});
describe("stripSilentToken", () => {
it("strips token from end of text", () => {
expect(stripSilentToken("Done.\n\nNO_REPLY")).toBe("Done.");
});
it("does not strip token from start of text", () => {
expect(stripSilentToken("NO_REPLY 👍")).toBe("NO_REPLY 👍");
});
it("strips token with emoji (#30916)", () => {
expect(stripSilentToken("😄 NO_REPLY")).toBe("😄");
});
it("does not strip embedded token suffix without whitespace delimiter", () => {
expect(stripSilentToken("interject.NO_REPLY")).toBe("interject.NO_REPLY");
});
it("strips only trailing occurrence", () => {
expect(stripSilentToken("NO_REPLY ok NO_REPLY")).toBe("NO_REPLY ok");
});
it("returns empty string when only token remains", () => {
expect(stripSilentToken("NO_REPLY")).toBe("");
expect(stripSilentToken(" NO_REPLY ")).toBe("");
});
it("strips token preceded by bold markdown formatting", () => {
expect(stripSilentToken("**NO_REPLY")).toBe("");
expect(stripSilentToken("some text **NO_REPLY")).toBe("some text");
expect(stripSilentToken("reasoning**NO_REPLY")).toBe("reasoning");
});
it("works with custom token", () => {
expect(stripSilentToken("done HEARTBEAT_OK", "HEARTBEAT_OK")).toBe("done");
});
});
describe("isSilentReplyPrefixText", () => {
it("matches uppercase underscore prefixes", () => {
expect(isSilentReplyPrefixText("NO_")).toBe(true);
expect(isSilentReplyPrefixText("NO_RE")).toBe(true);
expect(isSilentReplyPrefixText("NO_REPLY")).toBe(true);
expect(isSilentReplyPrefixText(" HEARTBEAT_", "HEARTBEAT_OK")).toBe(true);
});
it("rejects ambiguous natural-language prefixes", () => {
expect(isSilentReplyPrefixText("N")).toBe(false);
expect(isSilentReplyPrefixText("No")).toBe(false);
expect(isSilentReplyPrefixText("Hello")).toBe(false);
});
it("rejects non-prefixes and mixed characters", () => {
expect(isSilentReplyPrefixText("NO_X")).toBe(false);
expect(isSilentReplyPrefixText("NO_REPLY more")).toBe(false);
expect(isSilentReplyPrefixText("NO-")).toBe(false);
});
});