Skip to content

fix: preserve agent context during compaction - #4556

Merged
rekram1-node merged 2 commits into
anomalyco:devfrom
wolfie82:fix/preserve-agent-during-compaction
Nov 21, 2025
Merged

rekram1-node merged 2 commits into
anomalyco:devfrom
wolfie82:fix/preserve-agent-during-compaction

Conversation

@wolfie82

@wolfie82 wolfie82 commented Nov 20, 2025 •

Copy link
Copy Markdown
Contributor

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

Compaction was hardcoding agent to 'build', causing unwanted agent
switching and tool call failures after compaction.

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

Fixes agent switching issue when compaction is triggered manually
or automatically via context overflow.
@rekram1-node
rekram1-node merged commit 416f296 into anomalyco:dev Nov 21, 2025
3 checks passed
@rekram1-node

Copy link
Copy Markdown
Collaborator

lgtm, thanks!

@wolfie82
wolfie82 deleted the fix/preserve-agent-during-compaction branch November 21, 2025 17:50
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request May 5, 2026
AIALRA-0 pushed a commit to AIALRA-0/opencode-turn-engine that referenced this pull request Jun 10, 2026
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request Jun 14, 2026
meSingh pushed a commit to meSingh/opencode that referenced this pull request Sep 6, 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.

Wrong agent used after compaction

2 participants