Skip to content

fix(heartbeat): allow reasoning/thinking blocks in heartbeat ack filt… - #95798

Closed
hanZeng-08 wants to merge 1 commit into
openclaw:mainfrom
hanZeng-08:fix/95796-heartbeat-reasoning-filter
Closed

hanZeng-08 wants to merge 1 commit into
openclaw:mainfrom
hanZeng-08:fix/95796-heartbeat-reasoning-filter

Conversation

@hanZeng-08

@hanZeng-08 hanZeng-08 commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #95796

Summary

Problem: Heartbeat transcript filtering inconsistently preserved or removed suppressed HEARTBEAT_OK turns from model request history depending on whether the final assistant message contained non-text reasoning/thinking content. When the assistant's heartbeat acknowledgement included a thinking, reasoning, or redacted_thinking block, the entire heartbeat prompt/ack pair was left in the context, wasting tokens and polluting the model prompt.

Solution: Treat model-internal reasoning/thinking blocks as invisible to the heartbeat filter. resolveMessageText now skips thinking/reasoning/redacted_thinking block types when computing hasNonTextContent, allowing isHeartbeatOkResponse to correctly recognize short HEARTBEAT_OK acks even when the model emits reasoning alongside the text response.

What changed:

  • Updated resolveMessageText in heartbeat-filter.ts to ignore reasoning blocks; added regression tests in heartbeat-filter.test.ts for isHeartbeatOkResponse and filterHeartbeatTranscriptArtifacts with reasoning/thinking blocks.
  • Added redacted_thinking to the skipped reasoning block types (was missing in the original fix).
  • Improved inline comment in resolveMessageText to explain WHY reasoning blocks are skipped (ephemeral model artifacts, not user-visible content).
  • Added boundary test for reasoning mixed with non-text content (e.g., tool_use) to ensure it correctly returns false.

What did NOT change: Heartbeat token detection logic, maxAckChars enforcement, tool-call handling in heartbeat filtering, user-message heartbeat prompt detection, or the stripHeartbeatToken contract.

Real behavior proof

Behavior addressed: Assistant messages containing both a reasoning block and a short HEARTBEAT_OK text block were incorrectly classified as non-heartbeat content, causing the heartbeat span to remain in the transcript.

Real environment tested: Linux, Node v22.23.0, OpenClaw worktree SHA 034787c

Exact steps or command run after this patch:

# Test: Verify heartbeat ack with reasoning blocks is correctly identified
node -e "
const { isHeartbeatOkResponse } = require('./src/auto-reply/heartbeat-filter.ts');
const result = isHeartbeatOkResponse({
  role: 'assistant',
  content: [
    { type: 'thinking', thinking: 'Checking for new tasks...' },
    { type: 'text', text: 'HEARTBEAT_OK' },
  ],
});
console.log('isHeartbeatOkResponse:', result);
"

Evidence after fix:

isHeartbeatOkResponse: true

Before evidence (Negative control — old code):

node -e "
const { isHeartbeatOkResponse } = require('./src/auto-reply/heartbeat-filter.ts');
const result = isHeartbeatOkResponse({
  role: 'assistant',
  content: [
    { type: 'thinking', thinking: 'Checking for new tasks...' },
    { type: 'text', text: 'HEARTBEAT_OK' },
  ],
});
console.log('isHeartbeatOkResponse:', result);
"

Output:

isHeartbeatOkResponse: false

Test suite results:

pnpm test src/auto-reply/heartbeat-filter.test.ts --run

Output:

[test] starting test/vitest/vitest.unit-fast.config.ts

 ✓ |unit-fast| src/auto-reply/heartbeat-filter.test.ts (37 tests) 19ms

 Test Files  1 passed (1)
      Tests  37 passed (37)

All 37 tests pass. Key regression assertions:

  • matches heartbeat acks that include reasoning blocks — verifies isHeartbeatOkResponse returns true for assistant messages with thinking/reasoning/redacted_thinking blocks + HEARTBEAT_OK text
  • removes heartbeat pairs where the assistant response includes reasoning blocks — verifies filterHeartbeatTranscriptArtifacts strips the full heartbeat span from the transcript when the assistant ack includes reasoning blocks
  • does not match heartbeat when reasoning is mixed with non-text content — verifies isHeartbeatOkResponse returns false when reasoning is mixed with tool calls or other non-text content

Observed result after fix: Messages like [{ type: "thinking", thinking: "Checking..." }, { type: "text", text: "HEARTBEAT_OK" }] are now correctly recognized as heartbeat acks and removed from the transcript.

What was not tested: Live model streaming with actual heartbeat reasoning blocks; the fix is verified through unit tests and the logic path is deterministic.

Regression Test Plan

Coverage level: Unit-fast regression tests

Target test file: src/auto-reply/heartbeat-filter.test.ts

Scenario locked in:

  1. isHeartbeatOkResponse returns true for assistant messages containing thinking/reasoning/redacted_thinking blocks alongside a short HEARTBEAT_OK text block.
  2. filterHeartbeatTranscriptArtifacts removes the full heartbeat span (user prompt + assistant ack) when the assistant ack includes reasoning blocks.
  3. isHeartbeatOkResponse returns false when reasoning blocks are mixed with other non-text content (e.g., tool_use), ensuring we don't over-match.

Why this is the smallest reliable guardrail: The three new tests cover the exact boundary that was broken (resolveMessageText flagging reasoning blocks as non-text), the end-to-end behavior (filterHeartbeatTranscriptArtifacts span removal), and a safety boundary (reasoning + non-text returns false), without touching other heartbeat logic paths.

Root Cause

Root cause: resolveMessageText iterated over every block in the message content array and set hasNonTextContent = true for any block type other than text/input_text/output_text. This included model-internal reasoning blocks (thinking, reasoning, redacted_thinking), which are not user-visible and should not disqualify a message from being a heartbeat ack.

Missing detection / guardrail: Existing tests covered heartbeat filtering with plain text, tool calls, and tool results, but did not test the case where a heartbeat ack message contained reasoning blocks. The hasNonTextContent check was too broad and lacked a carve-out for reasoning content.

Risk checklist

  • Did user-visible behavior change? No — reasoning blocks are already hidden from users; this fix only affects internal transcript filtering.
  • Did config/environment/migration behavior change? No — no new config keys.
  • Did security/auth behavior change? No — the change is purely in message classification logic.

AI-assisted.

@hanZeng-08
hanZeng-08 force-pushed the fix/95796-heartbeat-reasoning-filter branch from 2b5274b to 034787c Compare June 23, 2026 10:50
@hanZeng-08

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper

clawsweeper Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 26, 2026, 4:34 AM ET / 08:34 UTC.

Summary
The PR makes heartbeat acknowledgement text extraction ignore thinking/reasoning/redacted_thinking blocks and adds regression coverage for direct ACK detection, transcript pruning, and the tool-use boundary.

PR surface: Source +12, Tests +69. Total +81 across 2 files.

Reproducibility: yes. Source inspection of current main shows a thinking/reasoning block sets hasNonTextContent and makes isHeartbeatOkResponse return false before HEARTBEAT_OK stripping runs; I did not execute tests in this read-only cleanup review.

Review metrics: none identified.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #95796
Summary: This PR is a candidate fix for the canonical heartbeat reasoning/thinking ACK transcript-filter bug; several open duplicate PRs target the same root cause, while one merged reasoning-heartbeat PR fixed an adjacent delivery-selector path.

Members:

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

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Maintainers should choose this PR or another open candidate as the canonical fix before landing duplicates.

Risk before merge

  • [P1] This patch changes session prompt-history pruning and chat-history hiding semantics for reasoning-wrapped heartbeat ACKs, so maintainers should preserve the tool/media non-text guard before merge.
  • [P1] Several open PRs target the same canonical bug; landing more than one variant would create avoidable duplicate churn around session-history filtering.

Maintainer options:

  1. Pick the canonical heartbeat-filter fix (recommended)
    Before merging, maintainers should choose this PR or another proof-positive duplicate as the single session-history classifier change to land.
  2. Accept this PR's narrow boundary
    If this PR is selected, merge the reasoning-block carve-out with its tool-use boundary coverage and close the remaining duplicate candidates afterward.
  3. Close if another candidate lands first
    If a different PR lands the same source and test coverage first, close this PR as superseded by that merged fix.

Next step before merge

  • No automated repair is needed; the maintainer action is to choose the canonical duplicate and run normal landing checks.

Security
Cleared: The diff only changes deterministic heartbeat message classification and colocated tests, with no dependency, workflow, secret, or code-execution surface changes.

Review details

Best possible solution:

Land one canonical narrow heartbeat-filter fix that treats only internal reasoning blocks as invisible while preserving tool/media guards, then close duplicate candidate PRs after the linked bug is fixed.

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

Yes. Source inspection of current main shows a thinking/reasoning block sets hasNonTextContent and makes isHeartbeatOkResponse return false before HEARTBEAT_OK stripping runs; I did not execute tests in this read-only cleanup review.

Is this the best way to solve the issue?

Yes. The PR fixes the classifier at the right narrow layer and is better than removing the non-text guard entirely because it keeps tool/media content from being misclassified as a silent heartbeat ACK.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is a bounded behavior fix for heartbeat prompt-history filtering that affects session/context quality without a core outage.
  • merge-risk: 🚨 session-state: The diff changes which assistant transcript blocks are pruned from future prompt history, so an over-broad merge could affect retained session context.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes before/after terminal output for the changed classifier plus focused test output; live model streaming was not shown, but the patched path is deterministic.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes before/after terminal output for the changed classifier plus focused test output; live model streaming was not shown, but the patched path is deterministic.
Evidence reviewed

PR surface:

Source +12, Tests +69. Total +81 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 12 0 +12
Tests 1 69 0 +69
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 81 0 +81

Acceptance criteria:

  • [P1] pnpm test src/auto-reply/heartbeat-filter.test.ts --run.
  • [P1] pnpm check:changed.

What I checked:

  • Repository policy read: Root AGENTS.md was present and read fully; no scoped AGENTS.md applies to src/auto-reply, and the policy requires whole-path review plus best-fix assessment for PR verdicts. (AGENTS.md:1, 9a735bea03f6)
  • Current main still rejects the reported shape: On current main, resolveMessageText marks every block outside text/input_text/output_text as non-text, and isHeartbeatOkResponse returns false before HEARTBEAT_OK stripping when hasNonTextContent is true. (src/auto-reply/heartbeat-filter.ts:271, 9a735bea03f6)
  • Latest release still has the same behavior: The v2026.6.10 copy of heartbeat-filter.ts has the same non-text gate, so the central fix is not shipped in the latest release. (src/auto-reply/heartbeat-filter.ts:271, aa69b12d0086)
  • Patch preserves the non-text guard narrowly: The PR skips only reasoning, thinking, and redacted_thinking blocks inside resolveMessageText, leaving other non-text blocks to keep hasNonTextContent true. (src/auto-reply/heartbeat-filter.ts:271, 034787c9a890)
  • Regression tests cover the useful boundary: The diff adds tests for reasoning/thinking/redacted_thinking plus HEARTBEAT_OK, full heartbeat pair removal, and rejection when reasoning is mixed with tool_use. (src/auto-reply/heartbeat-filter.test.ts:135, 034787c9a890)
  • Runtime caller uses this classifier before prompt history: The embedded runner calls filterHeartbeatTranscriptArtifacts on activeSession.messages before prompt history and tool-result truncation are assembled for the next model request. (src/agents/embedded-agent-runner/run/attempt.ts:4171, 9a735bea03f6)

Likely related people:

  • nxmxbbd: Authored the merged heartbeat transcript pruning replacement that introduced 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: GitHub commit metadata and adjacent cleanup commits tie this account to landing and shaping the current heartbeat-filter baseline. (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 merged PR 92356 on heartbeat reasoning payload selection, which is adjacent reasoning/heartbeat behavior but not the same transcript-filter bug. (role: recent adjacent contributor; confidence: medium; commits: 43890ebc3b25; files: src/auto-reply/heartbeat-reply-payload.ts, src/infra/heartbeat-runner.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@hanZeng-08

Copy link
Copy Markdown
Contributor Author

Hi! This PR is ready for maintainer review — ClawSweeper has re-reviewed it and rated it platinum hermit (status: 👀 ready for maintainer look). It fixes the heartbeat filtering issue where reasoning/thinking blocks incorrectly prevent HEARTBEAT_OK detection.

Could a maintainer please take a look when you have a moment? Thanks! 🙏

/cc @obviyus @vincentkoc

1 similar comment
@hanZeng-08

Copy link
Copy Markdown
Contributor Author

Hi! This PR is ready for maintainer review — ClawSweeper has re-reviewed it and rated it platinum hermit (status: 👀 ready for maintainer look). It fixes the heartbeat filtering issue where reasoning/thinking blocks incorrectly prevent HEARTBEAT_OK detection.

Could a maintainer please take a look when you have a moment? Thanks! 🙏

/cc @obviyus @vincentkoc

@clawsweeper clawsweeper Bot added the merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. label Jun 27, 2026
@hanZeng-08

Copy link
Copy Markdown
Contributor Author

Hi! This PR is ready for maintainer review — ClawSweeper has re-reviewed it and rated it platinum hermit (status: 👀 ready for maintainer look). It fixes the heartbeat filtering issue where reasoning/thinking blocks incorrectly prevent HEARTBEAT_OK detection.

Could a maintainer please take a look when you have a moment? Thanks! 🙏

/cc @obviyus @vincentkoc

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Jul 16, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #clawtributors on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

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. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S stale Marked as stale due to inactivity status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

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

1 participant