Skip to content

[enhancement] Add session history compaction support for long-running tasks #24

@github-actions

Description

@github-actions

Background

The Copilot SDK v0.1.26 introduces session compaction and agent selection APIs (PR #544 by Steve Sanderson, Feb 25, 2026). Session compaction allows SDKs to request that the CLI compress the conversation history, which is critical for long-running tasks that would otherwise hit token limits.

Key SDK features:

  • session.compact() - Requests history compaction from the CLI
  • Returns compacted message count and new token usage stats
  • Useful when session history grows large during multi-turn clarification or complex execution phases
  • Prevents token limit errors and improves response times for later messages

Related commit:

  • 9d998fb - Add SDK support for agent selection and session compaction APIs

Proposal

Leverage session compaction in Planeteer to handle long-running clarification chats and task execution sessions:

  1. Track session token usage in src/services/copilot.ts:
    • Monitor sessionEvent.data.usage fields (e.g., total_tokens, prompt_tokens)
    • Expose current token count via callback or state update to screens
  2. Auto-compact when threshold reached:
    • Call session.compact() when token usage exceeds a threshold (e.g., 80% of model context window)
    • Display compaction events in the UI (e.g., "Session history compacted: 45 → 12 messages")
  3. Manual compaction command (optional):
    • Add a keybinding in Clarify/Refine screens (e.g., c) to trigger manual compaction
    • Useful if user notices slowdowns or wants to reset context
  4. Persist compaction metrics:
    • Log compaction events to plan history for debugging
    • Show token usage stats in the status bar or debug view

Benefit

  • Prevents token limit errors: Long clarification sessions or complex multi-task plans won't crash due to context overflow
  • Faster responses: Compacted history reduces latency for subsequent LLM calls
  • Cost savings: Fewer tokens sent to the model on each request
  • Better UX: Users see transparent feedback about session state and context management

Acceptance Criteria

  • src/services/copilot.ts tracks session token usage from session.context_changed or assistant.message_delta events
  • Auto-compaction triggers when token usage exceeds configurable threshold (default: 80% of 128K context for claude-sonnet-4.6)
  • session.compact() is called and compaction results are logged
  • Compaction events are displayed in Clarify/Refine/Execute screens (e.g., toast notification or event log entry)
  • Token usage is displayed in the status bar or a debug panel
  • Tests verify compaction is triggered and session continues working after compaction
  • Updated to SDK ^0.1.26+ to access compaction APIs

AI generated by Weekly Enhancement Suggestions

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions