Skip to content

fix(engine): charge a cache rewrite to the new change at the first difference (MINOR) - #110

Merged
amiddavid merged 1 commit into
mainfrom
fix/rewrite-blame-first-new-change
Nov 25, 2026
Merged

amiddavid merged 1 commit into
mainfrom
fix/rewrite-blame-first-new-change

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Behavior change

  • Which component pays for a cache rewrite changes in one case. Two operations cover the first changed position. The old code charged the earlier one. The new code charges the one whose change is new on this request.
  • Totals stay the same. Only the ChangeID of the break changes.
  • No shared case changes. test/cases is unchanged and all cases pass.
  • Approved by the owner on 2026-11-25.
  • Release kind: MINOR (a change to Compute results).

Summary

Fixes rossoctl/context-guru#1351. The accountant charged a sweep's cache rewrite to searchfold.

What we found

  • The break at the first changed position took the first operation in pipeline order that covers it.
  • Active operations come first in pipeline order.
  • A fold that the host applies again forwards the same bytes. It still covers the message.
  • A new sweep cut on that message changes the bytes. The fold won the charge anyway.

What changed

  • breakItemAt picks the covering operation whose change is new on this request. FirstApplied tells which. A re-report of an older change is not new.
  • If no new change covers the position, the first covering operation pays, as before.
  • docs/design.md row 1 describes the rule.

How it was tested

  • macOS, Go 1.27.4.
  • go test -race ./... passes. gofmt -l ., go vet ./... and go mod tidy -diff are clean.
  • New test TestARewriteIsChargedToTheNewChangeNotTheRepeatedOne, two variants (fold stays active, fold reported again). It fails on origin/main (break charged to cf) and passes now.
  • New test TestARepeatedChangeKeepsTheChargeWhenNoNewChangeCoversTheFirstDifference guards the fallback.
  • Mutations: always taking the first covering operation fails the first test. Dropping the fallback fails the second.

Assisted-By: Claude Opus 5.5 [email protected]

…fference

When several operations cover the first changed position, the break went
to the first one in pipeline order. An active change that forwards the same
bytes as before (a re-applied fold) won over a new change on the same
message, so searchfold paid for a rewrite that the sweep caused.

The break now goes to the operation whose change is new on this request.
The earlier operation pays only when no new change covers the position.
Totals do not change. Only the ChangeID of the break does.

Refs rossoctl/context-guru#1351

Assisted-By: Claude Opus 5.5 <[email protected]>
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 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 92a6ff9 approve

Verdict: approve

Findings

None.

Non-blocking

  • internal/engine/compute.go:1619 — repeatsPrevious checks change identity (FirstApplied), not the forwarded bytes. The design row 1 justifies the rule with "an operation that forwards what an earlier request already forwarded cannot move the first difference". The code cannot tell that case from a host that re-reports the same ChangeID with a different output (the fold2 shape of the second test). Scenario: a re-reported change with a new output and a new change whose span covers the same first difference. The break then goes to the new change, even when the re-reported one moved the position. The doc is ambiguous there, so this is not a doc/code conflict. A byte comparison against PrevPrint.Forwarded, or one doc sentence, would close it.
  • internal/engine/compute.go:1899, :1913, :1991 — the mark premium, the TTL premium and the mark payback still take their ChangeID from changeAt, the first covering item. The same "a repeated change did not cause it" reasoning is not applied to them. Those effects are not cache-miss breaks, so this is out of scope here. Note only, for a possible follow-up.

What I checked

  • Read the full diff: docs/design.md row 1, internal/engine/compute.go (itemAt, itemsAt, breakItemAt, repeatsPrevious, the row 1 call site), internal/engine/breakblame_test.go.
  • Read docs/spec.md (R22, R23, R31) and docs/design.md §5.2 in full. The doc row 1 update matches the code: the new change pays, a repeated one pays only as the fallback.
  • Traced the FirstApplied lifecycle: EndRequest → withReported → withFirstApplied (state.go), nextState (compute.go:2449), and Restore (persist.go:146). A change that enters Active on this request gets Requests+1; a re-report keeps the earlier count. So first <= c.s.Requests in repeatsPrevious separates "new this request" from "an earlier request applied it". A request that fails before Compute does not raise Requests, so its change still counts as new on the next request that stores state. That is right: its output reached no cache.
  • Traced items() order: active items first, then reported ones. itemsAt keeps that order and stops the inner span loop only, so several items can cover one position. itemAt returns ks[0], which is the old result, so changeAt and its three call sites are unchanged.
  • Verified both tests by hand. Test 1, "fold stays active": at request 3, FirstApplied is cf:2, cs1:2, cs2:3 and Requests is 2. The fold repeats, sweep2 is new, so breakItemAt picks sweep2 (cs2). On the base commit, itemAt returns the active fold first, so the same test fails with cf. The test is a real regression test. The "fold reported again" subtest covers the EndRequest re-report path, where withFirstApplied keeps cf:2.
  • Checked the row 1 guard !(active && endedAddedAt): endedAddedAt does not depend on the chosen item, and an ended item is skipped by itemsAt, so the guard behaves as before. It changes outcome only when a new change covers the position, which the new doc row allows.
  • Ran gofmt -l . (nothing), go vet ./... (clean), go test ./... (all packages ok). No new exported names, so api_test is unaffected. The new test imports only testing and time, so R3 holds. gen_cases.py is untouched and the golden cases pass, so no regeneration is needed.

@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.332 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 92a6ff9 approve

Verdict: approve

Findings

None.

Non-blocking

  • internal/engine/compute.go:1620 — repeatsPrevious can mark this request's own new change as a repeat. It needs the concurrent case (R53): request A reports change X (EndRequest stores FirstApplied[X] = Requests+1), request B's Compute runs next and advances Requests past that value, then A's Compute recomputes and reads first <= c.s.Requests. The break then goes to an older change, and the doc rule "the one whose change is new on this request pays" does not hold. Attribution only: the penalty tokens come from addBreak(res, c.readEnd, id) and do not depend on the id. R53 already makes concurrent results order-dependent, so I do not block on it. A fix would compare FirstApplied with the request's own number, not with c.s.Requests.
  • docs/design.md:1034 — the rationale "an operation that forwards what an earlier request already forwarded cannot move the first difference" has two exceptions the design itself names. An active change can churn its replacement bytes under a stable ChangeID (§4.2 rule 6: readlifecycle, skeleton), and the observe-to-enforce report (§4.6) is a repeat whose prefix does change. In both, the fallback in breakItemAt (ks[0]) still charges the change when it alone covers the position, so the outcome is right; only the justification is broader than the truth. When such a change overlaps a new change at the same position, §4.6's "row 1 adds a break with that ChangeID" now yields to the new row 1 preference. Consider a sentence that names the exceptions.
  • No test covers several new changes covering the first difference (the first in pipeline order pays). The doc does not pin that choice either. Low value, so I leave it out.

What I checked

  • Read CLAUDE.md, docs/spec.md, docs/design.md (§4.4, §4.5, §4.6, §5.1, §5.2, §6.2, §6.5, Q12) for the rules this change touches.
  • Read the full diff: docs/design.md row 1, internal/engine/breakblame_test.go (new), internal/engine/compute.go.
  • Traced the new code path. itemsAt returns the same first item the old itemAt returned, so changeAt and the markPremium attribution (compute.go:1899, 1913, 1991) keep their behavior. Only the row 1 choice changed.
  • Traced FirstApplied through its whole life: EndRequest stores a new change at Requests+1 (state.go:221), a report that replaces an active change keeps the earlier value (withFirstApplied), Compute builds the next state at Requests+1 (compute.go:2448), Restore fills absent values (persist.go:146). So: active item = repeat; re-reported change = repeat; first report = new. This matches the updated doc row.
  • Verified the tests are real regression tests. On the base commit, itemAt returns the first covering item in pipeline order, which is the fold (cf) in both subtests of TestARewriteIsChargedToTheNewChangeNotTheRepeatedOne; the test wants cs2, so it fails on base. TestARepeatedChangeKeepsTheChargeWhenNoNewChangeCoversTheFirstDifference passes on base too; it guards the fallback.
  • Checked the penalty does not depend on the chosen id: addBreak(res, c.readEnd, id) takes the tokens from readEnd and B*, so the change moves attribution between changes, no token figure. Check 5's invariant holds: breakItemAt returns an item of this request (reported or active).
  • Ran, in this worktree: gofmt -l . (nothing), go vet ./... (clean), go test ./... (all packages ok, including test/cases).
  • Checked the repo rules: no new import (R3 holds), no public API change, docs updated in the same PR for a Compute behavior change (CLAUDE.md rule 5).

@amiddavid
amiddavid merged commit ac23a62 into main Nov 25, 2026
3 checks passed
amiddavid added a commit to rossoctl/context-guru that referenced this pull request Nov 25, 2026
…hange, #1351) (#1403)

chore(deps): bump accountant to v0.7.0 (charge a rewrite to the new change, #1351)

accountant v0.7.0 charges a cache rewrite to the operation whose change is
new at the first changed position. A repeated fold no longer pays for a
new sweep cut on the same message. Totals do not change.

Add a recorder test for #1351: a repeated searchfold fold plus a new sweep
cut on the same message. The break goes to the sweep.

Fixes #1351

test(accrec): cover a fold that the proxy reports again next to the sweep

The proxy reports every operation on every turn. Run the #1351 test for
both shapes: the fold stays active, and the fold is reported again.


Refs #1351. Behavior change (accounting only: a cache rewrite caused by a new sweep cut on a message searchfold had folded is charged to the sweep, not searchfold; totals unchanged) approved by the owner (David Amid, 2026-11-25, integrator session): "1403 yes". Accountant v0.7.0 (rossoctl/accountant#110). Local tests waived (owner rule).

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
boazdavid added a commit to rossoctl/cortex that referenced this pull request Nov 26, 2026
v0.8.0 adds two engine fixes over v0.6.0:
- A cache rewrite is charged to the new change at the first difference
  (rossoctl/accountant#110).
- An in-place change on a whole item ends when a later change rewrites
  that same client item to 0 bytes (rossoctl/accountant#113, fixes
  rossoctl/accountant#108). Found reviewing #2057: with
  tool-prune, turn 1 remove:[NotebookEdit] moves the cache mark onto the
  survivor, and turn 2 remove:[Bash,NotebookEdit] deletes that survivor,
  so the stale in-place rewrite failed the sum and conservation checks on
  every later turn.

The nested metrics module is now tagged at this commit, so metrics moves
from a pseudo-version to v0.8.0 (rossoctl/accountant#112, SizedPricer): it
prices savings by the whole bill difference at each prompt size. The new
interface is additive, so cortex builds unchanged.

Add a toolprune regression test for the #108 shape (fails on v0.6.0,
passes on v0.8.0). Widen the savings-agreement thread-total tolerance to
absorb the per-step micro-dollar rounding the new v0.8.0 case exposes.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
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.

fix(accountant): a sweep's cache rewrite is charged to searchfold

1 participant