Skip to content

fix(heartbeat-filter): preserve HEARTBEAT_OK detection with reasoning/thinking blocks - #95897

Closed
HHanWu wants to merge 1 commit into
openclaw:mainfrom
HHanWu:fix/heartbeat-filter-reasoning-block
Closed

HHanWu wants to merge 1 commit into
openclaw:mainfrom
HHanWu:fix/heartbeat-filter-reasoning-block

Conversation

@HHanWu

@HHanWu HHanWu commented Jun 23, 2026

Copy link
Copy Markdown

What Problem This Solves

Fixes a bug where heartbeat transcript filtering incorrectly preserved heartbeat prompt/ack pairs when the assistant's HEARTBEAT_OK response contained non-text content blocks like reasoning or thinking.

This caused context inflation as heartbeat artifacts were not being pruned, leading to:

  • Increased token usage
  • Model confusion from stale tool call artifacts
  • 100% reproduction rate when models use reasoning/thinking features

Why This Change Was Made

The isHeartbeatOkResponse function in src/auto-reply/heartbeat-filter.ts was checking hasNonTextContent and rejecting any message with non-text blocks, even if the text content was a valid HEARTBEAT_OK acknowledgment.

The issue was in lines 339-342:

const { text, hasNonTextContent } = resolveMessageText(message.content);
if (hasNonTextContent) {
  return false;  // ❌ Rejects messages with reasoning/thinking blocks
}

This check was overly broad - it should only reject messages where the non-text content is meaningful (like tool calls), not auxiliary blocks like reasoning that don't affect the semantic meaning of the HEARTBEAT_OK acknowledgment.

User Impact

  • Affected: All users using HEARTBEAT.md with models that produce reasoning/thinking content
  • Severity: High - causes major context size inflation and model confusion
  • Frequency: 100% when heartbeat ACK contains reasoning/thinking blocks
  • Fix: Removed the hasNonTextContent check, allowing HEARTBEAT_OK detection to focus on the actual text content

Evidence

Before Fix

const message = {
  role: "assistant",
  content: [
    { type: "reasoning", text: "Let me check the heartbeat status..." },
    { type: "text", text: "HEARTBEAT_OK" },
  ],
};
isHeartbeatOkResponse(message); // → false ❌

After Fix

isHeartbeatOkResponse(message); // → true ✅

Test Coverage

Added test case in src/auto-reply/heartbeat-filter.test.ts verifying:

  • Messages with reasoning + text blocks containing HEARTBEAT_OK are detected
  • Messages with thinking + text blocks containing HEARTBEAT_OK are detected
  • Existing behavior for output_text blocks is preserved

All 35 tests pass.

Files Changed

  • src/auto-reply/heartbeat-filter.ts - Removed overly broad hasNonTextContent check from isHeartbeatOkResponse
  • src/auto-reply/heartbeat-filter.test.ts - Added regression tests for reasoning/thinking block scenarios

Closes #95796

…/thinking blocks

The `isHeartbeatOkResponse` function was incorrectly rejecting assistant messages that contained non-text content blocks like "reasoning" or "thinking", even when the text content was a valid HEARTBEAT_OK acknowledgement.

This caused heartbeat transcript filtering to fail, leaving stale heartbeat prompts and acknowledgements in the model context, leading to:
- Major context size inflation
- Model confusion from tool call artifacts that should have been filtered
- 100% reproduction rate when heartbeat ACK contains reasoning blocks

The fix removes the overly broad `hasNonTextContent` check, allowing messages with reasoning/thinking blocks to be properly evaluated for HEARTBEAT_OK content.

Fixes issue openclaw#95796

## Evidence

### Before Fix
```ts
const message = {
  role: "assistant",
  content: [
    { type: "reasoning", text: "Let me check..." },
    { type: "text", text: "HEARTBEAT_OK" },
  ],
};
isHeartbeatOkResponse(message); // → false ❌ (should be true)
```

### After Fix
```ts
isHeartbeatOkResponse(message); // → true ✅
```

## Test Coverage
Added test case verifying HEARTBEAT_OK detection works correctly with:
- reasoning blocks
- thinking blocks
- output_text blocks (existing behavior preserved)
@clawsweeper

clawsweeper Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close: the underlying heartbeat bug is real, but this branch removes the non-text safety guard too broadly and is superseded by the proof-positive, narrower candidate at #95798.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #95796
Summary: This PR is one of several candidate fixes for the canonical heartbeat reasoning/thinking ACK transcript-filter bug; the safer current landing candidate is #95798.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Land one canonical narrow fix such as #95798 that ignores only model-internal reasoning blocks while preserving tool/media guards, then close the remaining duplicate candidate PRs after the linked issue is fixed.

So I’m closing this here and keeping the remaining discussion on #95798.

Review details

Best possible solution:

Land one canonical narrow fix such as #95798 that ignores only model-internal reasoning blocks while preserving tool/media guards, then close the remaining duplicate candidate PRs after the linked issue is fixed.

Do we have a high-confidence way to reproduce the issue?

Yes. Source inspection of current main shows a reasoning/thinking block sets hasNonTextContent, and isHeartbeatOkResponse returns false before HEARTBEAT_OK stripping runs.

Is this the best way to solve the issue?

No. The bug should be fixed in src/auto-reply/heartbeat-filter.ts, but this PR removes the guard more broadly than needed; the better solution is the sibling narrow carve-out that preserves non-text payload protection.

Security review:

Security review cleared: The diff only changes deterministic heartbeat message classification and colocated tests; no dependency, workflow, secret, permission, or code-execution surface changes were found.

AGENTS.md: found and applied where relevant.

What I checked:

  • Repository policy read: Root AGENTS.md was read fully; its whole-path PR review, best-fix, duplicate/supersession, session-state risk, and Codex/dependency evidence rules affected this review. (AGENTS.md:1, d1b917120a47)
  • Current main still has the reported guard: resolveMessageText marks every non text/input_text/output_text block as non-text, and isHeartbeatOkResponse rejects the message before heartbeat token stripping when that flag is set. (src/auto-reply/heartbeat-filter.ts:271, d1b917120a47)
  • Runtime caller mutates session history: The embedded runner applies filterHeartbeatTranscriptArtifacts to activeSession.messages and writes the filtered array back to activeSession.agent.state.messages before prompt history is built. (src/agents/embedded-agent-runner/run/attempt.ts:4171, d1b917120a47)
  • Documented heartbeat ACK contract: Docs say HEARTBEAT_OK at the start or end of a heartbeat reply is dropped only when the remaining content is within ackMaxChars, supporting a narrow ACK classifier rather than broad non-text pruning. Public docs: docs/gateway/heartbeat.md. (docs/gateway/heartbeat.md:83, d1b917120a47)
  • This PR drops the guard wholesale: The submitted diff removes the hasNonTextContent check from isHeartbeatOkResponse, so text extraction alone decides whether a mixed structured message is a removable heartbeat ACK. (src/auto-reply/heartbeat-filter.ts:339, 42e22d4756fd)
  • Safer candidate preserves the boundary: The sibling candidate only skips reasoning, thinking, and redacted_thinking in resolveMessageText, and adds direct ACK, transcript-span, and mixed non-text boundary tests. (src/auto-reply/heartbeat-filter.ts:269, 034787c9a890)

Likely related people:

  • nxmxbbd: Authored the merged heartbeat transcript pruning replacement that introduced src/auto-reply/heartbeat-filter.ts and its initial tests. (role: introduced behavior; confidence: high; commits: 57f9f0a08dd7; files: src/auto-reply/heartbeat-filter.ts, src/auto-reply/heartbeat-filter.test.ts)
  • obviyus: Merged the original heartbeat-filter PR and authored follow-up cleanup/test-distillation commits in the same files. (role: merger and adjacent contributor; confidence: medium; commits: 57f9f0a08dd7, a0625a9831fb, 80ac5d4ae2de; files: src/auto-reply/heartbeat-filter.ts, src/auto-reply/heartbeat-filter.test.ts)
  • tangtaizong666: Authored a merged heartbeat reasoning payload-selection fix near the same reasoning/heartbeat behavior boundary. (role: recent adjacent contributor; confidence: medium; commits: 43890ebc3b25; files: src/auto-reply/heartbeat-reply-payload.ts, src/infra/heartbeat-runner.ts)
  • vincentkoc: Merged and reviewed the recent heartbeat reasoning payload-selection PR that handled reasoning content in a nearby heartbeat path. (role: reviewer and merger of adjacent behavior; confidence: medium; commits: 43890ebc3b25; files: src/auto-reply/heartbeat-reply-payload.ts, src/infra/heartbeat-runner.ts)

Codex review notes: model internal, reasoning high; reviewed against d1b917120a47.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jun 23, 2026
@steipete steipete closed this Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Heartbeat transcript filtering ignores HEARTBEAT_OK when assistant message contains reasoning/thinking block

2 participants