Skip to content

Conversation

@continue
Copy link
Contributor

@continue continue bot commented Oct 24, 2025

Summary

Fixes context pollution issue in the Continue CLI where sessions would retain state from previous invocations, causing the LLM to reference conversations from prior sessions even when starting fresh (without --resume).

Problem

As reported in #7916 and described in CON-4530, the CLI was maintaining state in singleton services across separate CLI invocations. This meant:

  1. Running cn "Tell me about dogs", having a conversation, then exiting
  2. Running cn "What were we discussing?" in a new session
  3. The CLI would incorrectly reference the previous conversation about dogs

Root Cause

Three main sources of state pollution:

  1. SessionManager singleton - maintained currentSession across invocations
  2. ChatHistoryService - kept undo/redo stacks and memoized state
  3. ServiceContainer - global singleton persisted service instances

Solution

1. Session Management (session.ts)

  • Added SessionManager.reset() static method for proper cleanup
  • Ensures singleton can be reset for testing and clean state

2. Chat Initialization (chat.ts)

  • Updated initializeChatHistory() to explicitly create a new session when neither --resume nor --fork flags are used
  • This ensures every fresh CLI invocation starts with a clean session

3. Service Cleanup (ChatHistoryService.ts)

  • Clear undo/redo stacks during initialization
  • Reset memoized snapshot cache
  • Prevents state leakage between service reinitializations

4. Tests (session.test.ts)

  • Added comprehensive "session isolation" test suite
  • Verifies new sessions don't inherit history from previous sessions
  • Tests concurrent session operations
  • Validates proper state clearing during transitions

Testing

The fix includes three new test cases:

  • should not pollute new sessions with previous session history
  • should create independent sessions for concurrent operations
  • should properly clear session state when transitioning between sessions

Behavior Changes

Before

# Session 1
$ cn "Tell me about dogs"
> Dogs are loyal companions...
$ # Exit

# Session 2 (WRONG - references previous conversation)
$ cn "What were we discussing?"
> We were discussing dogs...

After

# Session 1
$ cn "Tell me about dogs"
> Dogs are loyal companions...
$ # Exit

# Session 2 (CORRECT - clean session)
$ cn "What were we discussing?"
> I don't have context about a previous discussion...

Flags Still Work as Expected

  • cn --resume: Continues most recent session ✅
  • cn --fork <id>: Forks from specific session ✅
  • cn (no flags): Clean new session ✅

Related Issues

Fixes CON-4530
Closes #7916


This agent session was co-authored by Brian Douglas and Continue.


Summary by cubic

Fixes CLI context pollution so new sessions start clean and don't inherit history from previous runs. Applies to fresh invocations; sessions only carry over when using --resume or --fork. Addresses Linear CON-4530.

  • Bug Fixes
    • Start a new session by default in chat initialization when no resume/fork flags are used.
    • Clear undo/redo stacks and memoized snapshot on ChatHistoryService init to prevent leakage.
    • Add SessionManager.reset for explicit cleanup and add tests that verify session isolation and clean transitions.

Resolves context pollution issue where CLI sessions would retain
state from previous invocations, causing responses to reference
conversations that should have been isolated.

Changes:
- Added SessionManager.reset() static method for proper cleanup
- Updated initializeChatHistory() to create new session by default
- Clear undo/redo stacks in ChatHistoryService on initialization
- Added comprehensive session isolation tests

Fixes CON-4530

Co-authored-by: Username <nate@continue.dev>
Generated with [Continue](https://continue.dev)

Co-Authored-By: Continue <noreply@continue.dev>
@github-project-automation github-project-automation bot moved this from Todo to In Progress in Issues and PRs Oct 24, 2025
The vi.mock setup already properly types the uuid v4 function to return
string, so the 'as any' type assertions are unnecessary and were
creating TypeScript smell in the test file.

This change improves type safety while maintaining the same test behavior.

Co-authored-by: Username <nate@continue.dev>
Generated with [Continue](https://continue.dev)

Co-Authored-By: Continue <noreply@continue.dev>
@bdougie bdougie marked this pull request as ready for review October 24, 2025 05:22
@bdougie bdougie requested a review from a team as a code owner October 24, 2025 05:22
@bdougie bdougie requested review from RomneyDa and removed request for a team October 24, 2025 05:22
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 24, 2025
@github-actions
Copy link

⚠️ PR Title Format

Your PR title doesn't follow the conventional commit format, but this won't block your PR from being merged. We recommend using this format for better project organization.

Expected Format:

<type>[optional scope]: <description>

Examples:

  • feat: add changelog generation support
  • fix: resolve login redirect issue
  • docs: update README with new instructions
  • chore: update dependencies

Valid Types:

feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert

This helps with:

  • 📝 Automatic changelog generation
  • 🚀 Automated semantic versioning
  • 📊 Better project history tracking

This is a non-blocking warning - your PR can still be merged without fixing this.

@bdougie bdougie changed the title Fix CLI context pollution between sessions fix: CLI context pollution between sessions Oct 24, 2025
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 5 files

@bdougie bdougie self-assigned this Oct 24, 2025
Generated with [Continue](https://continue.dev)

Co-Authored-By: Continue <noreply@continue.dev>
@bdougie bdougie changed the title fix: CLI context pollution between sessions fix: CLI context pollution + add video to doc writing guide Oct 24, 2025
@bdougie bdougie changed the title fix: CLI context pollution + add video to doc writing guide fix: CLI context pollution between sessions Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Does the Continue CLI not support starting a new session?

2 participants