Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@marius-kilocode
Copy link
Collaborator

@marius-kilocode marius-kilocode commented Jan 19, 2026

Summary

Fixes a bug in the CLI where delayed IPC messages with partial=true could overwrite completed messages, causing context loss during active sessions.

Problem

Users reported that the CLI "constantly drops context" making it unreliable. The context drops happened randomly during active sessions.

Root Cause

In updateChatMessageByTsAtom, when a delayed IPC message with partial=true arrived after a message had already been completed (partial=false), the stale update would overwrite the completed message:

  1. Message completes streaming: {text: "Complete content here", partial: false}
  2. Delayed IPC message arrives: {text: "Partial", partial: true}
  3. BUG: The stale partial message overwrites the complete one → context lost!

Solution

Added a check in updateChatMessageByTsAtom to prevent stale partial updates from overwriting completed messages:

// BUG FIX: Don't allow stale partial updates to overwrite completed messages
const isStalePartialUpdate = existingMessage.partial === false && updatedMessage.partial === true

if (isStalePartialUpdate && newVersion <= currentVersion) {
    return  // Preserve the completed message
}

Testing

  • Added 28 tests specifically for context drop scenarios
  • Updated 2 existing tests to reflect correct behavior
  • All 260 CLI state atom tests pass

Files Changed

  • cli/src/state/atoms/extension.ts - Added the fix
  • cli/src/state/atoms/__tests__/extension-context-drop.test.ts - Updated test to verify fix
  • cli/src/state/atoms/__tests__/message-reconciliation.test.ts - Updated 2 tests
  • cli/src/state/atoms/__tests__/CLI_CONTEXT_DROP_INVESTIGATION.md - Investigation documentation

@changeset-bot
Copy link

changeset-bot bot commented Jan 19, 2026

🦋 Changeset detected

Latest commit: 748b172

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@kilocode/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@marius-kilocode marius-kilocode requested a review from a team January 19, 2026 18:29
@kiloconnect
Copy link
Contributor

kiloconnect bot commented Jan 19, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Overview

This PR fixes a CLI context drop bug where stale partial updates could overwrite completed messages. The fix is well-implemented with proper safeguards:

  1. Root Cause Fix (cli/src/state/atoms/extension.ts): Adds a check to detect when a partial update (partial=true) attempts to overwrite a completed message (partial=false). If the new version has less or equal content, the stale update is rejected.

  2. Comprehensive Test Coverage: The PR includes extensive tests covering:

    • Content length collision scenarios
    • Out-of-order state updates
    • Rapid streaming updates
    • Async race conditions
    • Partial flag transitions
  3. Proper Changeset: Includes a well-documented changeset explaining the bug and fix.

Code Quality

  • The fix is minimal and surgical - only 8 lines of logic added
  • The condition isStalePartialUpdate && newVersion <= currentVersion correctly identifies stale updates
  • Tests verify both the bug scenario and that legitimate streaming updates still work
  • Test naming follows CLI convention (.test.ts)
Files Reviewed (4 files)
  • .changeset/fix-cli-context-drop-stale-partial.md - Changeset documentation
  • cli/src/state/atoms/extension.ts - Core fix implementation
  • cli/src/state/atoms/__tests__/extension-context-drop.test.ts - New comprehensive test suite
  • cli/src/state/atoms/__tests__/message-reconciliation.test.ts - Updated existing tests

@marius-kilocode marius-kilocode enabled auto-merge (squash) January 19, 2026 18:39
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.

3 participants