Repository navigation
fix: remove heartbeat transcript pruning to prevent session corruption - #60998
Conversation
Greptile SummaryThis PR replaces the racey Confidence Score: 5/5Safe to merge — the core race condition fix is correct and the only finding is a minor false-negative edge case in the new filter that, at worst, leaves extra heartbeat pairs in context until compaction. All findings are P2. The trim-ordering issue in stripMarkup is a false-negative (not a false-positive), so the worst outcome is slightly more context growth between compactions, not data corruption or session misidentification. The primary goal of eliminating the fs.truncate race is fully achieved. src/auto-reply/heartbeat-filter.ts — stripMarkup trim ordering Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/auto-reply/heartbeat-filter.ts
Line: 45-55
Comment:
**Trim ordering leaves trailing markup after newlines**
The `.trim()` call runs after the regex stripping, not before. For a model response like `"**HEARTBEAT_OK**\n"` (trailing newline is common in streamed output): the leading `**` is stripped correctly, but the trailing-markup regex `/[*\`~_]+$/` fails to match because `$` anchors to the end of the string after the `\n`. The final `.trim()` then produces `"HEARTBEAT_OK**"`, which doesn't equal `HEARTBEAT_TOKEN`, so the pair silently escapes filtering and accumulates in context until compaction.
Adding an intermediate `.trim()` before the regex passes fixes this:
```suggestion
function stripMarkup(text: string): string {
return (
text
.replace(/<[^>]*>/g, " ")
.replace(/ /gi, " ")
.trim()
// Strip leading/trailing markdown wrappers
.replace(/^[*`~_]+/, "")
.replace(/[*`~_]+$/, "")
.trim()
);
}
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "feat: filter heartbeat pairs in context ..." | Re-trigger Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 757e88a697
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
757e88a to
cff7c23
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cff7c23237
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
Fixed the trim ordering issue flagged by Greptile in cff7c23 --- |
cff7c23 to
4505d17
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4505d17679
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
4505d17 to
9e424c5
Compare
|
Fixed in 9e424c53 — the duplicate Ollama import was a rebase artifact (upstream moved from |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9e424c53a8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
6e189a1 to
8175727
Compare
CI note (rebase 2026-04-06)Rebased onto |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1dbdddca74
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 140e658d51
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 630ac72061
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ce10d1d83d
ℹ️ 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".
|
@nxmxbbd please stop pushing to the PR so that I can land it. |
80b0907 to
0c85293
Compare
|
All yours — I'll hold off. Thanks for the cleanup commits, much cleaner with |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0c85293226
ℹ️ 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".
openclaw#39609) Remove pruneHeartbeatTranscript() and captureTranscriptState() which used fs.truncate on live session JSONL files without holding the session write lock. This caused a race condition where concurrent normal turns could end up with orphaned parentId chains or data loss when truncation removed entries appended by other writers. HEARTBEAT_OK entries now stay in the JSONL file (append-only). They are filtered out at context build time and cleaned up during compaction. Excluded alternatives: - Acquiring the session write lock for heartbeat: too invasive, blocks normal turns during heartbeat I/O which can be slow. - Writing heartbeat entries to a separate file: adds complexity to session reconstruction and compaction without clear benefit. - Using file-level locking (flock): not portable and doesn't compose with the existing session lock model.
…claw#39609) Add heartbeat-filter.ts with functions to identify and filter no-op heartbeat user+assistant pairs from session context. A pair is filtered only when the user message contains the HEARTBEAT_OK instruction AND the assistant response is purely the HEARTBEAT_OK token (no real content). Integration points: - attempt.ts: filter messages before each prompt call so heartbeat no-ops don't consume context window tokens - session-truncation.ts: drop heartbeat pairs during compaction rewrite to prevent unbounded disk growth from append-only heartbeat entries
Addresses Greptile review feedback: stripMarkup() now trims whitespace before stripping markdown wrappers, preventing false negatives when heartbeat responses have leading/trailing whitespace around markup.
- P1: Limit heartbeat pair pruning to summarized portion of branch
during compaction. Pairs in the unsummarized tail (from
firstKeptEntryId onward) are preserved to avoid dangling compaction
boundaries.
- P2: Narrow isHeartbeatUserMessage to require instruction phrase
pattern ('reply HEARTBEAT_OK' or 'respond...HEARTBEAT_OK'), not
just the bare token. Prevents false positives on normal conversation
turns that discuss or quote the token.
- P2: Broaden isHeartbeatOkResponse to handle responsePrefix, emoji
suffixes, and HTML wrappers — matching the detection logic in
heartbeat-events-filter.ts. Previously only exact match worked.
- P1: Accept short alphanumeric suffixes after HEARTBEAT_OK, using
the same DEFAULT_HEARTBEAT_ACK_MAX_CHARS (300) threshold as
stripHeartbeatToken in heartbeat.ts. Prevents no-op ack turns
like 'HEARTBEAT_OK all good' from accumulating in JSONL history.
- P2: Broaden instruction verb pattern to cover custom heartbeat
prompts ('return', 'say', 'output', 'answer' in addition to
'reply' and 'respond').
runHeartbeatOnce classifies empty replies as 'ok-empty' no-ops, but isHeartbeatOkResponse returned false for empty content, preventing filterHeartbeatPairs from removing these turns. Without transcript pruning, they would accumulate in JSONL indefinitely.
326a6ed to
cac52ca
Compare
obviyus
left a comment
There was a problem hiding this comment.
Reviewed latest changes; landing now.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cac52ca520
ℹ️ 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".
| const filteredMessages = filterHeartbeatPairs( | ||
| activeSession.messages, | ||
| heartbeatSummary?.ackMaxChars, | ||
| heartbeatSummary?.prompt, | ||
| ); |
There was a problem hiding this comment.
Filter heartbeat turns before context shaping passes
filterHeartbeatPairs is applied only at this late stage, but runEmbeddedAttempt has already run limitHistoryTurns(...) and assembleAttemptContextEngine(...) on the unfiltered transcript earlier in the same turn. When no-op heartbeat pairs accumulate, they can consume history slots/token budget and cause real user/assistant turns to be trimmed, and this later filter then drops the heartbeat turns anyway, leaving avoidable context loss for the actual prompt. Heartbeat filtering should happen before history limiting and context-engine assembly.
Useful? React with 👍 / 👎.
Summary
Fixes #39609 — heartbeat transcript pruning via
fs.truncateraces with concurrent session writers, causing orphanedparentIdchains, context collapse, and data loss.Changes
Remove
pruneHeartbeatTranscript()andcaptureTranscriptState()fromsrc/infra/heartbeat-runner.ts. These usedfs.truncateon live session JSONL files without holding the session write lock. HEARTBEAT_OK entries now stay in the JSONL file (append-only).Filter heartbeat pairs at context build time — New
filterHeartbeatPairs()insrc/auto-reply/heartbeat-filter.tsremoves no-op heartbeat user+assistant pairs from the message array before each model prompt. Only filters pairs where the user message contains the HEARTBEAT_OK instruction AND the assistant response is purely the token (no real content). Applied insrc/agents/pi-embedded-runner/run/attempt.ts.Clean up heartbeat pairs during compaction —
truncateSessionAfterCompaction()insrc/agents/pi-embedded-runner/session-truncation.tsnow also drops filtered heartbeat pairs when rewriting the session file. Compaction already holds the session write lock, so this is safe.Excluded alternatives
Test plan
filterHeartbeatPairs,isHeartbeatUserMessage,isHeartbeatOkResponse(18 cases)pnpm checkpasses (pre-existing TS errors only, unrelated to this change)🤖 Generated with Claude Code