Skip to content

fix(cachesplit): keep the Converse cachePoint entry after the stable half - #755

Merged
amiddavid merged 1 commit into
mainfrom
fix/cachesplit-converse
Oct 27, 2026
Merged

amiddavid merged 1 commit into
mainfrom
fix/cachesplit-converse

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

The volatile-tail split now works on the Bedrock Converse shape. Before, it reported a split that did not help.

What changed

  • Chose (a), split correctly. The fix is about 10 lines. It is safe: the text and the breakpoint count do not change.
  • On Converse, cachePoint is its own entry after the block. The split now keeps that entry right after the stable half. The volatile half goes after it.
  • The Anthropic field-on-block case is unchanged.
  • Vertex: the code treats Vertex like Anthropic (field on block), so the existing path applies. I found no code that writes the array-entry shape for Vertex. I did not test a real Vertex request.
  • docs/components/caching.md now says the -34.1% figure is an Anthropic measurement. The Converse path has no cost measurement.

How it was tested

Run (eval box) Result
New test on clean origin/main code FAIL (breakpoint still covers the volatile half)
New test with the fix pass
Mutation: fix disabled, test kept FAIL
gofmt -l, go vet ./apply clean
go test -race ./apply/... pass

Data dependence

None changes. The split reads request structure and the system text, as before. No stored customer data. docs/data-dependence.md needs no change.

Open questions

  • Should we measure the Converse saving on a real Bedrock run before we credit it?

Closes #45

🤖 Generated with Claude Code

…half (#45)

On Bedrock Converse, cachePoint is its own system array entry after the
block it ends. The split put the volatile half before that entry, so the
breakpoint still covered the churn while the split reported Changed.

The split now keeps a standalone cachePoint entry directly after the
stable half and puts the volatile half after it. The Anthropic
field-on-block case is unchanged. The docs state that the -34.1% figure
is an Anthropic measurement.

Signed-off-by: DAVID AMID <[email protected]>
Assisted-By: Claude
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.311 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 2d0c1b078ffa approve

Verdict: approve

Findings

None. The fix is correct. I traced every path I could construct against it.

Non-blocking

  • apply/prefixsplit_test.go:501 — the new test calls splitTail directly. It does not go through BodyFull on the Converse shape. The gate (apply/apply.go:478) and the splice are shared with the Anthropic path, which TestSplitAppliedThroughBodyFull covers. Risk is low. A wiring test per provider shape would close it.
  • apply/apply.go:1239 — hasCacheBreakpoint queries system.#.cache_control but not system.#.cachePoint. A Converse body whose only breakpoint is a standalone system entry reads as "no breakpoint". Today this changes nothing: Converse bodies go to Bedrock, and resolveCacheAware returns true for Bedrock by provider name (apply/apply.go:979). Pre-existing, not this PR.
  • docs/components/caching.md:70 — the doc still says the component's Reformat "always reports Skipped". apply/apply.go:705 sets the flag before the run, and TestSplitIsReportedAgainstCachesplit asserts the opposite. The line is stale. Pre-existing, but this PR edits the same file, so it was cheap to fix here.
  • apply/prefixsplit.go:191 — e.Get("cachePoint").Exists() is true for a JSON null value. {"cachePoint":null} would count as standalone and move after the stable half. That input is invalid on the Converse wire, and the entry is moved, not lost. No action needed.

What I checked

  • Full diff 245982d7..HEAD (one commit, three files) and the code around each change.
  • The reorder logic: on Converse, a cachePoint entry ends the cached prefix at the block before it. Output [stable, cachePoint, volatile] keeps the churn outside the hashed prefix. The old code emitted [stable, volatile, cachePoint], which covered the churn — the defect named in docs/results/improvement-plan.md (§F, "the split on Bedrock Converse") and issue #45.
  • Loop bounds: the inner i++ fires at most once, because the marker search is gated on !split. Only the cachePoint entry is skipped, and it is appended verbatim. No block is lost or duplicated. Blocks after a non-split block keep their own cachePoint neighbours in place.
  • Breakpoint count, both spellings: CountBreakpoints/elemMarks (apply/metawrite.go:161-211) count the standalone entry as 1. The split block in Converse carries no inline mark, so the count stays 1 -> 1. A block with an inline mark plus a standalone follower also stays 2 -> 2 (inline stays on the stable half, standalone is appended, both are deleted from the volatile half).
  • Fail-open and losslessness: the consumed entry is copied from blocks[i+1].Raw before any body rewrite. Error paths return the input untouched. Text concatenation is unchanged; the new test asserts both halves and the preserved count.
  • Byte-shift bookkeeping: shiftAt/shift come from the whole system splice (prefixsplit.go:185), not from entry order, so the reorder cannot corrupt the message writeback.
  • Ordering against neighbours: headTTL runs before the split and only edits existing marks; nothing after the split rewrites system (only prefixsplit.go:177 does).
  • The test itself: traced blockWithGitTail(6000) through the split. at lands after the newline, both halves pass the token floor, and the assertions match the code's output for Bedrock and BedrockMantle.
  • docs/data-dependence.md:184 — the cachesplit row still holds; the PR changes no data dependence, and the PR body says so.
  • The doc edit in caching.md is honest: the −34.1% figure is Anthropic-only, the Converse path has no cost measurement.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.311 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 2d0c1b078ffa approve

Verdict: approve

Findings

None. The fix is correct and lossless. Details of what I verified are under "What I checked".

Non-blocking

  • apply/prefixsplit.go:191 — isStandaloneCachePoint does not check the provider. On an Anthropic- or Vertex-shaped wire, a standalone cachePoint entry is invalid input. If a client sends one after a block that splits, this code reorders it. The entry is broken either way, and the reorder keeps the count. No action needed.
  • apply/prefixsplit.go:155 — The skip consumes exactly one standalone entry. Two back-to-back cachePoint entries after the split block would leave the second one after the volatile half. The Converse API allows this shape but it is pointless on the wire, so I do not expect it. A for loop instead of the single if would cover it.
  • apply/prefixsplit_test.go:501 — The new test does not cover the combined shape: a split block with an inline cachePoint plus a standalone entry after it. I traced that path by hand: the stable half keeps the inline mark, the standalone entry is consumed, and the total count stays 2. A test would pin this down.

What I checked

  • The reorder is semantically right. A Converse breakpoint covers everything before it. Input [block, {cachePoint}] becomes [stable, {cachePoint}, volatile], so the hashed prefix ends at the stable half. Before the fix, the output was [stable, volatile, {cachePoint}] — the breakpoint still covered the churn, and the split reported success while buying nothing. The fix matches its claim.
  • Text seen by the model is unchanged. A cachePoint entry carries no text, so the concatenation of system text is the same in both orders.
  • The consumed entry survives verbatim. apply/prefixsplit.go:156 appends blocks[i+1].Raw unmodified. The i++ plus the loop increment skips exactly that entry; I traced the index arithmetic for the entry last in the array and for entries after it.
  • The !e.Get("text").Exists() guard is needed and correct. A block that carries an inline cachePoint plus text must not be consumed as standalone — its text would be dropped. With the guard, such a block keeps its inline mark on the stable half through the field copy at apply/prefixsplit.go:129-132, and the delete(volatile, ...) at line 142-143 keeps the cap-safe count. The guard also passes guardContent-only entries through untouched.
  • Breakpoint count stays within the cap of four. CountBreakpoints (apply/metawrite.go:161) counts a cachePoint field on any system element, wherever it sits. The move changes order, not count. The new test asserts this (wireBreakpoints before == after), and I checked wireBreakpoints and blockMarks in metawrite.go:109-118 count the standalone spelling.
  • The byte-shift contract still holds. The writeback uses shiftAt = sys.Index + len(sys.Raw) and shift = len(next) - len(body) (apply/prefixsplit.go:185). The reorder happens inside the re-marshaled system value; sjson.SetRawBytes still splices one range, so bytes after system keep moving by a uniform shift. The caller at apply/apply.go:478-481 is unaffected.
  • The loop rewrite is behavior-identical for input without a standalone entry. Existing tests (TestSplitsGitTailOffStableBody and the rest of prefixsplit_test.go) cover those paths.
  • The test is real. blockWithGitTail(6000) clears both the len(txt)/4 >= 1024 and at/4 >= 1024 floors. The assertions check the order, both halves' text, the cachePoint position, and the count, for Bedrock and BedrockMantle. Both constants exist (apply/prefixsplit.go:212).
  • Fail-open paths are untouched. Every error branch still appends the original raw block and continues; the marshal failure and no-split returns are unchanged.
  • The doc change is accurate. docs/components/caching.md now says the Converse path has no cost measurement. That matches the code: the −34.1% figure came from Anthropic-shaped traffic.

@amiddavid
amiddavid merged commit d6edaad into main Oct 27, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 27, 2026
@amiddavid
amiddavid deleted the fix/cachesplit-converse branch October 27, 2026 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(cache): the volatile-tail split is a silent no-op on Bedrock Converse — it reports success and cannot deliver

1 participant