fix(compaction): preserve agent identity across compaction boundaries#21046
Open
ESRE-dev wants to merge 3 commits intoanomalyco:devfrom
Open
fix(compaction): preserve agent identity across compaction boundaries#21046ESRE-dev wants to merge 3 commits intoanomalyco:devfrom
ESRE-dev wants to merge 3 commits intoanomalyco:devfrom
Conversation
…tions
* Qwen3 and other open-weight models hallucinate tool calls
during compaction despite the prompt saying not to, crashing
the session with "Tool call not allowed while generating
summary"
FIX
* set toolChoice: "none" at the API protocol level so the
model is structurally prevented from emitting tool calls
* Qwen3 and similar models see structured tool-call/tool-result
markup in the conversation history and imitate it during
compaction, either as structured API calls or raw XML text
FIX
* convert tool-call/tool-result parts into plain text summaries
before sending to the compaction model, so it never sees
tool markup to imitate
* silently ignore any remaining hallucinated tool calls in
the processor instead of crashing the session
* specialized agents (judges, reviewers, planners, explorers)
were losing their role after compaction — the summarizer
had no knowledge of which agent was being compacted, so
the summary template produced a generic "continue if you
have next steps" message with no identity context; custom
agents would silently revert to behaving like implementors
* port of commit 9edd821 from origin/dev-safe, adapted
to the current Effect-based architecture
UPDATE
* pass the source agent's system prompt (capped at 4K chars)
as a system message to the compaction LLM so the
summarizer knows the role it is summarizing for
* add an "Agent Role & Constraints" section to the
compaction template, placed before "Goal" to prime the
summary structure toward role-appropriate next steps
* inject a system-reminder for non-native agents after
compaction to reinforce role identity; native/built-in
agents are excluded because the framework always
re-injects their system prompts
* use optional chaining on the source agent lookup to
handle edge cases where an agent was removed from config
mid-session
* expose agent name on the experimental.session.compacting
plugin hook so plugins can customize compaction per-agent
TEST
* cover system prompt forwarding for non-native agents
* cover empty system array when agent has no prompt
* cover post-compaction reminder injection for custom agents
* cover reminder suppression for native agents
* cover graceful handling when the agent is unknown
* cover plugin hook receiving the agent name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #21045
Type of change
What does this PR do?
Specialized agents lose their identity after context compaction because the summarizer has no knowledge of which agent is being compacted. This causes non-native agents (reviewers, judges, planners) to revert to generic implementor behavior post-compaction.
Four changes:
<system-reminder>for non-native agents after compaction to reinforce role constraints. Native agents skip this since the framework always re-injects their prompts.experimental.session.compactingso plugins can customize compaction per-agentThe compaction.txt prompt file also gets matching instructions to preserve agent identity in summaries.
How did you verify your code works?
test/session/compaction.test.tscovering: system array injection for non-native agents, empty system for promptless agents,<system-reminder>injection/absence for non-native/native agents, plugin hook agent field, and graceful handling of unknown agent namespackages/opencodeandpackages/pluginScreenshots / recordings
N/A — no UI changes.
Checklist