← Back to PRs

#17743: fix(agents): disable orphaned user message deletion that causes session amnesia

by clawrl3000 open 2026-02-16 04:31 View on GitHub →
agents stale size: XS
## Summary The orphan repair logic in `src/agents/pi-embedded-runner/run/attempt.ts` (lines 883-899) deletes user messages at the leaf position to prevent consecutive user turns. However, this causes **complete session amnesia** in common scenarios. ## The Bug When a user message sits at the leaf position (after a failed assistant turn, between heartbeat polls, after tool errors, etc.), the orphan repair code: 1. Detects the user message as "orphaned" 2. Branches away from it or resets the leaf 3. Replaces the session messages without the user's input 4. The model never sees the message — the user's input is silently deleted ## Impact This affects every OpenClaw user running the embedded runner. Symptoms: - Agent appears to forget everything between messages - Session JSONL files shrink instead of growing - Conversation history disappears mid-session - Particularly bad with heartbeat polling, cron jobs, and rapid message sequences ## The Fix Comment out the orphan repair block entirely. The consecutive-user-turn constraint should be handled at the provider serialization layer (e.g. merging adjacent user messages) rather than by discarding messages from the transcript. ## Testing We've been running with this patch for over a week in production with: - Telegram channel with frequent messages - Heartbeat polling every 30 minutes - Multiple cron jobs triggering isolated sessions - Heavy browser automation and tool use Zero amnesia incidents since applying the patch. Before the patch, amnesia occurred on nearly every session. ## Related Issues - #5806 — \[Bug\]: Tool Use/Result Pairing and Consecutive Assistant Messages - #4650 — Session history loss after compaction - #3815 — Messages disappearing from session transcript All three issues describe the same root cause: user messages being deleted by the orphan detection logic. <!-- greptile_comment --> <h3>Greptile Summary</h3> Removes the orphaned user message deletion logic that was causing session amnesia by discarding user messages before the model could process them. The fix correctly delegates consecutive-user-turn handling to the existing validation layer (`validateAnthropicTurns`/`validateGeminiTurns` at lines 651-652) which merges adjacent user messages rather than deleting them. - Preserves user input that would previously be silently deleted when at leaf position (after failed assistant turns, between heartbeat polls, etc.) - Well-documented comment explains the rationale and links to related issues - Validation logic at lines 651-652 runs on existing history before `activeSession.prompt()` adds the new user message, ensuring proper turn alternation without data loss <h3>Confidence Score: 4/5</h3> - This PR is safe to merge with high confidence - it removes buggy code that was causing data loss - Score reflects that the fix correctly addresses a critical bug (session amnesia) by removing problematic deletion logic. The consecutive-user-turn constraint is properly handled by existing validation at lines 651-652. The PR has been production-tested for over a week with no amnesia incidents. Confidence is not 5 because the interaction between validation (lines 651-652) and the subsequent `activeSession.prompt()` call (lines 998/1000) should ideally be verified with integration tests to ensure the `@mariozechner/pi-coding-agent` library handles the merged messages correctly - No files require special attention - the change is a straightforward deletion of buggy code with excellent documentation <sub>Last reviewed commit: d2f96b2</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs