Skip to content

fix: suppress silent-reply partial tokens during streaming - #19648

Closed
bradleypriest wants to merge 1 commit into
openclaw:mainfrom
bradleypriest:fix/silent-reply-streaming-prefix
Closed

bradleypriest wants to merge 1 commit into
openclaw:mainfrom
bradleypriest:fix/silent-reply-streaming-prefix

Conversation

@bradleypriest

@bradleypriest bradleypriest commented Feb 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: During streaming, partial tokens like NO_R, HEARTBEAT leak to Telegram before the full NO_REPLY/HEARTBEAT_OK silent-reply check can match. Users briefly see ghost text before it disappears.
  • Why it matters: Breaks the silent-reply contract on streaming channels (Telegram streamMode: "partial", webchat). Same root cause as Webchat: ghost bubble artifact when model replies with NO_REPLY during streaming #15060.
  • What changed: Added isSilentReplyPrefix() that checks if accumulated stream text is a prefix of a silent token. Applied in normalizeStreamingText, parseChunk, and startTypingOnText to hold back output until the token completes (suppress) or diverges (flush normally).
  • What did NOT change: Final delivery logic (isSilentReplyText) is untouched. Non-streaming paths are unaffected. No config changes.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

User-visible / Behavior Changes

  • Silent replies (NO_REPLY, HEARTBEAT_OK) no longer flash partial tokens on streaming channels before being suppressed.
image

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Environment

  • OS: Linux (Raspberry Pi arm64)
  • Runtime/container: Node v24
  • Model/provider: Any (streaming-capable)
  • Integration/channel: Telegram with streamMode: "partial"
  • Relevant config: Default streaming config

Steps

  1. Send a message to a Telegram group where the agent is configured to stay silent (e.g. casual banter that triggers NO_REPLY)
  2. Watch the chat during streaming

Expected

  • No visible output — message is fully suppressed

Actual

  • Brief flash of NO_R or NO_RE etc. before the message disappears

Evidence

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

New test file src/auto-reply/tokens.test.ts covers isSilentReplyPrefix (prefix matching, whitespace trimming, non-matches, undefined input) and existing isSilentReplyText behavior.

Human Verification (required)

  • Verified scenarios: Unit tests pass for prefix matching of both NO_REPLY and HEARTBEAT_OK tokens, including edge cases (empty string, whitespace, partial divergence like NOPE)
  • Edge cases checked: Single-char prefix "N" (matches, causes one-token delay — acceptable since normal responses diverge immediately), empty/whitespace input (returns false), full token as prefix of itself (returns true, consistent with isSilentReplyText)
  • What you did not verify: Live Telegram streaming (no live bot in test environment), tsc full build (timed out on Pi — lint + vitest passed)

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Failure Recovery (if this breaks)

  • How to disable/revert: Revert this commit. Alternatively, disable streaming (streamMode: "off") as a workaround.
  • Files/config to restore: src/auto-reply/tokens.ts, src/auto-reply/reply/agent-runner-execution.ts, src/auto-reply/reply/streaming-directives.ts, src/auto-reply/reply/typing.ts
  • Known bad symptoms: If prefix matching is too aggressive, normal responses starting with N or H could experience a one-token delay before the first stream flush. This is by design and should be imperceptible.

Risks and Mitigations

  • Risk: Responses starting with NO_ (e.g. "NO_RULES apply here") are delayed by a few tokens before the prefix check diverges and flushes.
  • Mitigation: Divergence happens on the very next token after the prefix stops matching. Delay is sub-100ms in practice. Only affects the streaming layer — final delivery is unaffected.

Appendix

Log of debug/fix conversation with LLM

Greptile Summary

This PR adds streaming-time suppression of partial silent-reply tokens (NO_REPLY, HEARTBEAT_OK) to prevent ghost text from leaking to external messaging channels (e.g., Telegram). A new isSilentReplyPrefix() utility function in tokens.ts checks whether accumulated stream text is a prefix of a known silent token, and this check is wired into the three streaming code paths: normalizeStreamingText (agent runner), parseChunk (streaming directives), and startTypingOnText (typing controller).

  • The core isSilentReplyPrefix function is clean, well-tested, and handles edge cases (undefined, empty, whitespace).
  • The fix is applied consistently across the three streaming layers where partial tokens could leak.
  • agent-runner-execution.ts checks both SILENT_REPLY_TOKEN and HEARTBEAT_TOKEN prefixes, while streaming-directives.ts and typing.ts only check the configured silentToken (defaults to SILENT_REPLY_TOKEN). This is acceptable because: (1) heartbeat runs disable typing entirely via resolveTypingMode, and (2) normalizeStreamingText in the agent runner handles both tokens and wraps the lower-level paths.
  • New test file tokens.test.ts provides thorough coverage including incremental prefix matching, whitespace handling, divergence cases, and existing isSilentReplyText regression tests.
  • Non-streaming paths and final delivery logic remain unchanged.

Confidence Score: 4/5

  • This PR is safe to merge — it adds defensive suppression for partial tokens during streaming with no changes to final delivery logic or configuration.
  • The changes are well-scoped to the streaming layer, the new utility function is simple and correct, tests cover the important cases, and the three integration points are applied consistently with the existing architecture. Score is 4 rather than 5 because the streaming-directives.ts parseChunk only checks SILENT_REPLY_TOKEN prefix (not HEARTBEAT_TOKEN), creating a minor inconsistency with agent-runner-execution.ts — though this is mitigated by the higher-level heartbeat handling. The PR was not verified with live Telegram streaming or a full tsc build.
  • src/auto-reply/reply/streaming-directives.ts — only checks SILENT_REPLY_TOKEN prefix, not HEARTBEAT_TOKEN, unlike the agent runner.

Last reviewed commit: ec795c9

During streaming, partial tokens like NO_R or HEARTBEAT_ would leak to
Telegram before the full NO_REPLY/HEARTBEAT_OK check could match.

Add isSilentReplyPrefix() that checks if accumulated text is a prefix of
any silent reply token. Applied in normalizeStreamingText, parseChunk,
and startTypingOnText to hold back output until the token either completes
(suppress) or diverges (flush normally).

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile


const silentToken = options?.silentToken ?? SILENT_REPLY_TOKEN;
const isSilent = isSilentReplyText(text, silentToken);
const isSilent = isSilentReplyText(text, silentToken) || isSilentReplyPrefix(text, silentToken);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing HEARTBEAT_TOKEN prefix check

In agent-runner-execution.ts (lines 141-143), normalizeStreamingText checks prefixes for both SILENT_REPLY_TOKEN and HEARTBEAT_TOKEN, but here parseChunk only checks the single silentToken parameter (which defaults to SILENT_REPLY_TOKEN).

This means if parseChunk is ever invoked in a context where partial HEARTBEAT_OK tokens could appear (e.g., "HEART", "HEARTBEAT") without first passing through normalizeStreamingText, they would not be suppressed.

Currently this is mitigated because:

  1. The main streaming path goes through normalizeStreamingText which handles both tokens.
  2. stripHeartbeatToken handles complete tokens upstream.

However, for consistency with the intent of this fix, consider either:

  • Also checking HEARTBEAT_TOKEN here, or
  • Documenting that heartbeat prefix suppression is intentionally handled only at the normalizeStreamingText level.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/auto-reply/reply/streaming-directives.ts
Line: 50:50

Comment:
**Missing `HEARTBEAT_TOKEN` prefix check**

In `agent-runner-execution.ts` (lines 141-143), `normalizeStreamingText` checks prefixes for **both** `SILENT_REPLY_TOKEN` and `HEARTBEAT_TOKEN`, but here `parseChunk` only checks the single `silentToken` parameter (which defaults to `SILENT_REPLY_TOKEN`).

This means if `parseChunk` is ever invoked in a context where partial `HEARTBEAT_OK` tokens could appear (e.g., "HEART", "HEARTBEAT") without first passing through `normalizeStreamingText`, they would not be suppressed.

Currently this is mitigated because:
1. The main streaming path goes through `normalizeStreamingText` which handles both tokens.
2. `stripHeartbeatToken` handles complete tokens upstream.

However, for consistency with the intent of this fix, consider either:
- Also checking `HEARTBEAT_TOKEN` here, or
- Documenting that heartbeat prefix suppression is intentionally handled only at the `normalizeStreamingText` level.

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ec795c902a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +141 to +144
if (
isSilentReplyPrefix(text, SILENT_REPLY_TOKEN) ||
isSilentReplyPrefix(text, HEARTBEAT_TOKEN)
) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scope prefix suppression to cumulative stream text

normalizeStreamingText now drops any text that is a prefix of NO_REPLY/HEARTBEAT_OK, but this helper is reused for block-stream delivery via createBlockReplyDeliveryHandler, where payload.text is a chunk delta, not the full accumulated message. In that path, if an early chunk is a prefix (for example "N" followed by "ow..."), the prefix chunk is skipped permanently and later chunks do not restore it, so streamed block replies can be truncated rather than merely delayed.

Useful? React with 👍 / 👎.


const silentToken = options?.silentToken ?? SILENT_REPLY_TOKEN;
const isSilent = isSilentReplyText(text, silentToken);
const isSilent = isSilentReplyText(text, silentToken) || isSilentReplyPrefix(text, silentToken);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid prefix-based silent detection on directive deltas

parseChunk now marks any prefix of the silent token as isSilent, but createStreamingDirectiveAccumulator.consume() is fed streaming deltas (not cumulative text) from the subscribe pipeline. Applying prefix checks to each delta can clear ordinary leading chunks like "N"/"NO" before divergence arrives, causing later emitted chunks to miss the dropped prefix (for example "otice"), which corrupts streamed output under chunked delivery.

Useful? React with 👍 / 👎.

@Glucksberg

Copy link
Copy Markdown
Contributor

This might already be covered:

PR #19576 by @aldoeliacim seems to address the same problem.

Both address isSilentReplyText() being too permissive — the suffix regex can match NO_REPLY at the end of substantive replies, silently suppressing real content.

Both approaches have merit — might be worth coordinating.

Related issue(s): #19537

If any of these links don't look right, let me know and I'll correct them.

@obviyus

obviyus commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR and the solid streaming-prefix direction here.

These changes have been merged into #19576 (consolidated there with the exact NO_REPLY fix + streaming partial suppression + test updates), so we’re tracking the final land path in that PR.

Appreciate the contribution.

@obviyus obviyus closed this Feb 26, 2026
@obviyus

obviyus commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator

Extra thank-you to @bradleypriest for the PR and streaming investigation here. The final consolidated fix path is in #19576, but your input directly helped shape it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants