[PR #4556] fix: preserve agent context during compaction #11018

Closed
opened 2026-02-16 18:15:47 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/4556

State: closed
Merged: Yes


Fixes #4546, potentially #3099

Problem

When compaction is triggered (manually via <leader>c or automatically when context overflows), the system hardcodes the agent to 'build', causing unwanted agent switching. This breaks workflows using custom agents and can cause tool call failures after compaction.

Solution

  • Pass the current agent through SessionCompaction.create() and SessionCompaction.process()
  • Retrieve the agent from the last user message in the API endpoint
  • Default to 'build' if no user message is found (safe fallback)

Testing

Tested with custom agents by:

  1. Starting a session with a custom agent
  2. Triggering manual compaction (<leader>c)
  3. Verifying the agent persists after compaction
  4. Confirming tool calls work correctly post-compaction

Changes

  • compaction.ts: Add agent parameter to create() and process() functions
  • prompt.ts: Pass lastUser.agent to compaction calls
  • server.ts: Retrieve current agent from session messages in API endpoint
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/4556 **State:** closed **Merged:** Yes --- Fixes #4546, potentially #3099 ## Problem When compaction is triggered (manually via `<leader>c` or automatically when context overflows), the system hardcodes the agent to 'build', causing unwanted agent switching. This breaks workflows using custom agents and can cause tool call failures after compaction. ## Solution - Pass the current agent through `SessionCompaction.create()` and `SessionCompaction.process()` - Retrieve the agent from the last user message in the API endpoint - Default to 'build' if no user message is found (safe fallback) ## Testing Tested with custom agents by: 1. Starting a session with a custom agent 2. Triggering manual compaction (`<leader>c`) 3. Verifying the agent persists after compaction 4. Confirming tool calls work correctly post-compaction ## Changes - `compaction.ts`: Add `agent` parameter to `create()` and `process()` functions - `prompt.ts`: Pass `lastUser.agent` to compaction calls - `server.ts`: Retrieve current agent from session messages in API endpoint
yindo added the pull-request label 2026-02-16 18:15:47 -05:00
yindo closed this issue 2026-02-16 18:15:47 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11018