Skip to content

fix(opencode): preserve tool context through compaction and prompt loops#21425

Closed
GuestAUser wants to merge 3 commits intoanomalyco:devfrom
GuestAUser:fix/tool-context-followups
Closed

fix(opencode): preserve tool context through compaction and prompt loops#21425
GuestAUser wants to merge 3 commits intoanomalyco:devfrom
GuestAUser:fix/tool-context-followups

Conversation

@GuestAUser
Copy link
Copy Markdown

@GuestAUser GuestAUser commented Apr 8, 2026

Issue for this PR

Closes #20246

Type of change

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

What does this PR do?

This PR fixes three related session-context problems in packages/opencode/src/session.

  • It preserves bounded tool evidence through compaction so follow-up reasoning can still reference meaningful earlier tool output.
  • It threads OpenAI Responses tool follow-ups with previous_response_id when store: true is explicitly enabled, so the continuation path sends the tool-result delta instead of replaying the full prompt history.
  • It memoizes the stable per-loop prompt fragments from SystemPrompt.skills(agent) and SystemPrompt.environment(model) while still reloading instruction.system() on every iteration so instruction changes remain visible on the next loop step.

Together, these changes make multi-step sessions more context-preserving and more efficient without making instruction loading stale.

How did you verify your code works?

  • bun test test/session/message-v2.test.ts test/session/compaction.test.ts test/session/prompt-effect.test.ts test/session/processor-effect.test.ts
  • bun typecheck
  • Added prompt-loop regression coverage showing updated AGENTS.md instructions are visible on the next iteration.
  • Added prompt-loop proof coverage showing SystemPrompt.skills() and SystemPrompt.environment() are only computed once across a two-iteration loop.

Screenshots / recordings

N/A

Checklist

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

The following comment was made by an LLM, it may be inaccurate:

Based on my search results, I found one potentially related PR:

Related PR:

The current PR (#21425) appears to be the only PR addressing the specific combination of preserving tool context across compaction AND tool follow-ups. While there are other PRs related to compaction and tool handling (like #20289, #18764, #14393), none directly duplicate the functionality being added in PR #21425.

No duplicate PRs found

Memoize stable system prompt fragments across multi-step loop iterations so tool-call continuations stop rebuilding the same environment and skill text, while still reloading instruction files each step for correctness.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
@GuestAUser GuestAUser changed the title fix(opencode): preserve tool context across compaction and tool follow-ups fix(opencode): preserve tool context through compaction and prompt loops Apr 8, 2026
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 8, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 8, 2026
@github-actions github-actions bot closed this Apr 8, 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.

Overflow compaction produces incomplete summaries in tool-heavy sessions

1 participant