Skip to content

feat: session history compaction support via SDK ^0.1.26#27

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/add-session-history-compaction-support
Draft

feat: session history compaction support via SDK ^0.1.26#27
Copilot wants to merge 2 commits intomainfrom
copilot/add-session-history-compaction-support

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

Long-running clarification and execution sessions risk hitting model context limits without any feedback or mitigation. SDK v0.1.26 introduces InfiniteSessionConfig for automatic background compaction; this PR wires it in end-to-end.

Changes

SDK upgrade

  • @github/copilot-sdk ^0.1.24^0.1.26

copilot.ts — auto-compaction at 80%

  • Exports COMPACTION_THRESHOLD = 0.8
  • Every session created via sendPrompt now passes infiniteSessions: { backgroundCompactionThreshold: COMPACTION_THRESHOLD }, enabling automatic background compaction when context utilization exceeds 80%
const sessionConfig = {
  model: currentModel,
  streaming: true,
  infiniteSessions: { backgroundCompactionThreshold: COMPACTION_THRESHOLD },
};

status-bar.tsx — optional extra slot

  • Added extra?: string prop for ad-hoc right-side status info (used for token %)

execute.tsx — token usage + compaction toasts

  • session.usage_infoctx: X% displayed in the status bar
  • session.compaction_start → "⟳ Compacting session history…" toast
  • session.compaction_complete → "✓ Compacted: N messages removed (pre→post tokens)" toast (auto-clears after 5 s)

clarify.tsx — compaction notifications

  • Passes onSessionEvent to streamClarification; renders compaction toasts during long clarification sessions

planner.ts + refine.tsx — refinement compaction

  • refineWBS accepts an optional onSessionEvent parameter forwarded to sendPromptSync
  • Refine screen handles and displays compaction toasts

Tests

  • 6 new tests: session.usage_info, session.compaction_start, session.compaction_complete event shapes; compaction event forwarding through ExecutionCallbacks.onSessionEvent; COMPACTION_THRESHOLD value and range
Original prompt

This section details on the original issue you should resolve

<issue_title>[enhancement] Add session history compaction support for long-running tasks</issue_title>
<issue_description>## Background

The Copilot SDK v0.1.26 introduces session compaction and agent selection APIs (PR colindembovsky/planeteer#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

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: colindembovsky <1932561+colindembovsky@users.noreply.github.com>
Copilot AI changed the title [WIP] Add session history compaction support for long-running tasks feat: session history compaction support via SDK ^0.1.26 Feb 25, 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.

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

2 participants