Skip to content

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

Closed
BYK wants to merge 1 commit into
anomalyco:devfrom
BYK:byk/prompt-cache
Closed

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

Conversation

@BYK

@BYK BYK commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Re-filed from #31527, which was auto-closed by the periodic PR-cleanup bot (age-based, not on technical grounds). Rebased onto latest dev.

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

…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.
@github-actions

Copy link
Copy Markdown
Contributor

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

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

Related PR:

However, this is a related but distinct fix. PR #36852 is a re-filed version of the earlier #31527 (which was auto-closed by the cleanup bot), so it's not a duplicate—it's a rebased continuation of previous work.

No duplicate open PRs found addressing the exact same message caching mechanism across prompt loop iterations.

@BYK

BYK commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Closing this. After a deeper look, the original rationale doesn't hold and the patch isn't worth the risk:

  • The premise ("a full DB reload each loop iteration breaks Anthropic prompt-cache byte-identity") was already retracted in Prompt loop reloads all messages from DB every iteration, breaking prompt cache byte-identity #31525: the cache is content-based, not object-identity-based, so a reload producing identical content doesn't break it.
  • The only remaining angle was redundant DB I/O, but the patch as written still calls filterCompactedEffect (full-history re-pagination) every iteration — it only preserved JS object identity, so it saved no I/O.
  • A genuine reload-skip would need to avoid re-paginating history, which is entangled with SessionReminders mutating shared message objects in place and with persisted-vs-in-memory synthetic parts (plan mode + compaction). That's a correctness-sensitive rewrite of the core prompt loop for a benefit I'd previously rated minor — poor risk/reward.

Overlaps closed issues #24841 / #22208. Not pursuing.

@BYK BYK closed this Jul 14, 2026
@BYK BYK deleted the byk/prompt-cache branch July 14, 2026 14:37
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