Skip to content

fix(compaction): preserve agent identity across compaction boundaries#21046

Open
ESRE-dev wants to merge 3 commits intoanomalyco:devfrom
ESRE-dev:feature/compaction-agent-identity
Open

fix(compaction): preserve agent identity across compaction boundaries#21046
ESRE-dev wants to merge 3 commits intoanomalyco:devfrom
ESRE-dev:feature/compaction-agent-identity

Conversation

@ESRE-dev
Copy link
Copy Markdown

@ESRE-dev ESRE-dev commented Apr 4, 2026

Issue for this PR

Closes #21045

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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:

  1. System prompt forwarding — pass the source agent's system prompt (4K cap) to the compaction LLM so the summarizer knows the agent's role
  2. Template section — add "Agent Role & Constraints" to the compaction summary template, placed before "Goal" to prime the output structure
  3. Identity reminder — inject a <system-reminder> for non-native agents after compaction to reinforce role constraints. Native agents skip this since the framework always re-injects their prompts.
  4. Plugin hook — expose the agent name on experimental.session.compacting so plugins can customize compaction per-agent

The compaction.txt prompt file also gets matching instructions to preserve agent identity in summaries.

How did you verify your code works?

  • 6 new behavioral tests in test/session/compaction.test.ts covering: 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 names
  • All 43 compaction tests pass
  • Type checking passes across both packages/opencode and packages/plugin
  • Manually tested with custom agents (reviewer, planner) — they retain their role after compaction

Screenshots / recordings

N/A — no UI changes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

esrehmki added 3 commits April 4, 2026 22:10
…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
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.

[FEATURE]: Preserve agent identity across context compaction

2 participants