Skip to content

feat(core): add pinFirstUserTurn compaction option for prefix cache stability#33604

Closed
azhen073 wants to merge 1 commit into
anomalyco:devfrom
azhen073:prefix-cache-protection
Closed

feat(core): add pinFirstUserTurn compaction option for prefix cache stability#33604
azhen073 wants to merge 1 commit into
anomalyco:devfrom
azhen073:prefix-cache-protection

Conversation

@azhen073

@azhen073 azhen073 commented Jun 24, 2026

Copy link
Copy Markdown

Issue for this PR

N/A — incremental improvement, no issue filed.

Type of change

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

What does this PR do?

Adds a pinFirstUserTurn compaction option. When enabled, the first user message is kept verbatim during compaction instead of being summarized. This keeps the prompt prefix stable across turns, which helps providers with prefix caching (DeepSeek, OpenAI, etc.) reuse cached computation.

The change is tiny (~30 lines, 4 files) and defaults to false, so existing users are unaffected.

Related: #31867 improves DeepSeek cache from the system prompt side (date injection), this one improves it from the compaction side (first user turn protection). They complement each other.

How did you verify your code works?

Reviewed the logic manually:

  • V2 select(): the backwards walk now stops before the first user message index when pinFirstUserTurn is set, ensuring it lands in recent instead of head. The index calculation properly accounts for compaction messages being filtered out and serialized messages that evaluate to empty being skipped.
  • V1 select(): the first user turn is prepended to the recent turns list when not already included by tail_turns.
  • Both paths: default false means no change to current behavior.

I haven't been able to run tests — building requires downloading platform-specific Bun dependencies which timed out in my environment. Happy to run them if a maintainer can suggest a simpler test path.

Screenshots / recordings

N/A — not a UI change.

Checklist

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

…tability

When enabled, the first user message is preserved verbatim during compaction. This keeps the prompt prefix byte-identical across turns, maximizing prompt cache hit rates for providers with prefix caching (e.g. DeepSeek).

Configuration:
- V2 (opencode.json): compaction.pinFirstUserTurn = true
- V1 (opencode.json): compaction.pin_first_user_turn = true

Default is false, preserving existing behavior.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jun 24, 2026
@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

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

What needs to be fixed:

  • Not all checklist items are checked. Please confirm you have tested locally and have not included unrelated changes.

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

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

Potential Duplicate/Related PRs Found:

  1. feat: improve deepseek prompt cache reuse #31867feat: improve deepseek prompt cache reuse

    • Directly related to your PR's goal of maximizing prompt cache hit rates for DeepSeek. May already address similar functionality or have overlapping approach.
  2. fix(cache): improve Anthropic prompt cache hit rate with system split and tool stability #14743fix(cache): improve Anthropic prompt cache hit rate with system split and tool stability

    • Addresses prompt cache optimization for another provider (Anthropic). Could have related compaction or prefix-caching logic.
  3. fix(session): preserve model message cache snapshots #31613fix(session): preserve model message cache snapshots

    • May relate to cache preservation during session operations, potentially overlapping with prefix cache stability concerns.

These are worth reviewing to ensure your new pinFirstUserTurn option doesn't duplicate or conflict with existing cache optimization strategies.

@github-actions

Copy link
Copy Markdown
Contributor

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 Jun 24, 2026
@github-actions github-actions Bot closed this Jun 24, 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.

1 participant