docs: investigation report for CLI context history loss bug - #7357
docs: investigation report for CLI context history loss bug#7357kilo-code-bot[bot] wants to merge 1 commit into
Conversation
Analyzes recent PRs merged March 13-20 that could cause the CLI to lose conversation history mid-session. Identifies PR #6225 (move dynamic editor context to user message) as the most likely culprit due to token inflation from environment_details injection triggering premature context compaction.
| The chain of events: | ||
|
|
||
| 1. PR #6225 injects `<environment_details>` into the last user message on each loop iteration | ||
| 2. Despite fix `801b561c3`, the shallow copy (`msgs[idx] = { ...msgs[idx], parts: [...msgs[idx].parts, envPart] }`) creates a **new array** each iteration but the underlying parts objects may still reference cached data |
There was a problem hiding this comment.
WARNING: This step appears to describe a bug that the current code no longer has. prompt.ts:344 rebuilds msgs from MessageV2.filterCompacted(...) on every loop, and the spread at prompt.ts:712-724 appends envPart to that fresh array without mutating persisted messages. Reusing the existing part objects does not by itself cause the injected block to accumulate, so this weakens the root-cause analysis.
| 7. Compaction runs `SessionCompaction.process()` which: | ||
| - Creates a summary of the conversation (compaction.ts:101-294) | ||
| - If `overflow: true`, finds a user message to "replay" after compaction | ||
| - Creates a new user message with just "Continue if you have next steps..." |
There was a problem hiding this comment.
WARNING: This overstates the overflow path. SessionCompaction.process() only creates the generic "Continue if you have next steps..." message when no replayable user message is found; when replay exists it recreates the earlier user message parts instead (compaction.ts:235-289). That distinction matters because the model often resumes from the replayed request, not just a generic continue prompt.
|
|
||
| 3. **Add compaction event logging visible to users**: Users should be informed when compaction occurs so they can distinguish between "the model forgot" and "the model was deliberately summarized." | ||
|
|
||
| 4. **Consider a more conservative overflow threshold**: The current `COMPACTION_BUFFER` of 20,000 tokens (compaction.ts:30) may be too small for sessions with many tool calls and environment details. Increasing this buffer would delay compaction and preserve more history. |
There was a problem hiding this comment.
WARNING: This recommendation is backwards relative to SessionCompaction.isOverflow(). reserved is subtracted from model.limit.input in compaction.ts:42-47, so increasing COMPACTION_BUFFER makes the usable context window smaller and causes compaction sooner, not later.
Code Review SummaryStatus: 3 Issues Found | Recommendation: Address before merge Overview
Fix these issues in Kilo Cloud Issue Details (click to expand)WARNING
Other Observations (not in diff)No additional observations outside the diff. Files Reviewed (1 files)
Reviewed by gpt-5.4-20260305 · 232,048 tokens |
Summary
Root Cause Analysis
PR #6225 injects
<environment_details>into the last user message on every loop iteration. This PR required 6 follow-up bug-fix commits within 48 hours due to issues including:parts.push()mutated cached message objects, compounding token growthid/sessionID/messageIDfieldsThe inflated token count triggers
SessionCompaction.isOverflow(), which initiates compaction. After compaction,filterCompacted()drops all pre-compaction messages, leaving the model with only a summary. If the summary is incomplete, the model restarts the task from scratch — exactly matching the user's report.Key Files
packages/opencode/src/session/prompt.tspackages/opencode/src/session/compaction.tsisOverflow()threshold checkpackages/opencode/src/session/message-v2.tsfilterCompacted()history truncationpackages/opencode/src/session/processor.ts