Skip to content

docs: investigation report for CLI context history loss bug - #7357

Closed
kilo-code-bot[bot] wants to merge 1 commit into
mainfrom
investigate/context-history-loss-bug
Closed

docs: investigation report for CLI context history loss bug#7357
kilo-code-bot[bot] wants to merge 1 commit into
mainfrom
investigate/context-history-loss-bug

Conversation

@kilo-code-bot

@kilo-code-bot kilo-code-bot Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

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:

  1. Persistent accumulation — environment details were originally saved to DB, causing duplicate blocks
  2. Mutation of stored messagesparts.push() mutated cached message objects, compounding token growth
  3. Missing fields — injected parts lacked required id/sessionID/messageID fields

The 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

File Line What
packages/opencode/src/session/prompt.ts 704-726 Environment details injection
packages/opencode/src/session/compaction.ts 32-48 isOverflow() threshold check
packages/opencode/src/session/message-v2.ts 819-835 filterCompacted() history truncation
packages/opencode/src/session/processor.ts 305-310 Stream overflow detection

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kilo-code-bot

kilo-code-bot Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor Author

Code Review Summary

Status: 3 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 3
SUGGESTION 0

Fix these issues in Kilo Cloud

Issue Details (click to expand)

WARNING

File Line Issue
specs/context-history-loss-investigation.md 51 Report claims environment details still accumulate after 801b561c3, but prompt.ts rebuilds msgs each loop and appends the env block ephemerally.
specs/context-history-loss-investigation.md 59 Overflow path description is inaccurate: compaction replays an earlier user message when available instead of always creating a generic continue prompt.
specs/context-history-loss-investigation.md 121 Recommendation to increase COMPACTION_BUFFER has the opposite effect because reserved is subtracted from the usable input window.
Other Observations (not in diff)

No additional observations outside the diff.

Files Reviewed (1 files)
  • specs/context-history-loss-investigation.md - 3 issues

Reviewed by gpt-5.4-20260305 · 232,048 tokens

@alex-alecu alex-alecu closed this Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant