Skip to content

fix(session): cache messages across prompt loop to preserve prompt cache byte-identity#31527

Open
BYK wants to merge 1 commit into
anomalyco:devfrom
BYK:fix/prompt-loop-message-cache
Open

fix(session): cache messages across prompt loop to preserve prompt cache byte-identity#31527
BYK wants to merge 1 commit into
anomalyco:devfrom
BYK:fix/prompt-loop-message-cache

Conversation

@BYK

@BYK BYK commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #31525

Type of change

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

What does this PR do?

filterCompactedEffect(sessionID) reloads all messages from the DB at the start of every prompt loop iteration. Between reloads, the DB returns rows with new object identity, breaking Anthropic's prompt cache which relies on byte-identical prefixes.

This PR caches messages across loop iterations:

  • First iteration: full load from DB
  • Subsequent iterations: only append new messages (by ID) to the cached list
  • After subtask, compaction, or overflow: flag needsFullReload to get a fresh load

This preserves object identity for existing messages across iterations, improving prompt cache hit rates.

Re-filing of #25367 which was auto-closed by the cleanup bot.

How did you verify your code works?

Tested locally with multi-turn sessions. Verified via logs that message count stays stable across iterations and only new messages are appended after tool calls.

Screenshots / recordings

N/A — backend performance improvement.

Checklist

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

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

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

No duplicate PRs found.

(The search returned PR #31527 which is the current PR itself, but no other related/duplicate PRs addressing the same issue.)

…che byte-identity

filterCompactedEffect(sessionID) reloads all messages from the DB at
the start of every prompt loop iteration. Between reloads, the DB
returns rows with new object identity, breaking Anthropic's prompt cache
which relies on byte-identical prefixes.

Cache messages across loop iterations — first iteration does a full
load, subsequent iterations only append new messages by ID. Full reload
is flagged after subtask, compaction, or overflow operations.
@BYK BYK force-pushed the fix/prompt-loop-message-cache branch from 810aaff to f6cf0be Compare June 9, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt loop reloads all messages from DB every iteration, breaking prompt cache byte-identity

1 participant