Repository navigation
fix(engine): charge a cache rewrite to the new change at the first difference (MINOR) - #110
Merged
Merged
Conversation
…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
commented
Nov 25, 2026
amiddavid
left a comment
Collaborator
Author
There was a problem hiding this comment.
| 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—repeatsPreviouschecks 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 sameChangeIDwith a different output (thefold2shape 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 againstPrevPrint.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 theirChangeIDfromchangeAt, 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.mdrow 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) anddocs/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
FirstAppliedlifecycle:EndRequest→withReported→withFirstApplied(state.go),nextState(compute.go:2449), andRestore(persist.go:146). A change that entersActiveon this request getsRequests+1; a re-report keeps the earlier count. Sofirst <= c.s.RequestsinrepeatsPreviousseparates "new this request" from "an earlier request applied it". A request that fails beforeComputedoes not raiseRequests, 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.itemsAtkeeps that order and stops the inner span loop only, so several items can cover one position.itemAtreturnsks[0], which is the old result, sochangeAtand its three call sites are unchanged. - Verified both tests by hand. Test 1, "fold stays active": at request 3,
FirstAppliediscf:2, cs1:2, cs2:3andRequestsis 2. The fold repeats,sweep2is new, sobreakItemAtpickssweep2(cs2). On the base commit,itemAtreturns the active fold first, so the same test fails withcf. The test is a real regression test. The "fold reported again" subtest covers theEndRequestre-report path, wherewithFirstAppliedkeepscf:2. - Checked the row 1 guard
!(active && endedAddedAt):endedAddedAtdoes not depend on the chosen item, and an ended item is skipped byitemsAt, 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, soapi_testis unaffected. The new test imports onlytestingandtime, so R3 holds.gen_cases.pyis untouched and the golden cases pass, so no regeneration is needed.
amiddavid
commented
Nov 25, 2026
amiddavid
left a comment
Collaborator
Author
There was a problem hiding this comment.
| 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—repeatsPreviouscan mark this request's own new change as a repeat. It needs the concurrent case (R53): request A reports change X (EndRequeststoresFirstApplied[X] = Requests+1), request B'sComputeruns next and advancesRequestspast that value, then A'sComputerecomputes and readsfirst <= 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 fromaddBreak(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 compareFirstAppliedwith the request's own number, not withc.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 stableChangeID(§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 inbreakItemAt(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.mdrow 1,internal/engine/breakblame_test.go(new),internal/engine/compute.go. - Traced the new code path.
itemsAtreturns the same first item the olditemAtreturned, sochangeAtand themarkPremiumattribution (compute.go:1899, 1913, 1991) keep their behavior. Only the row 1 choice changed. - Traced
FirstAppliedthrough its whole life:EndRequeststores a new change atRequests+1(state.go:221), a report that replaces an active change keeps the earlier value (withFirstApplied),Computebuilds the next state atRequests+1(compute.go:2448),Restorefills 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,
itemAtreturns the first covering item in pipeline order, which is the fold (cf) in both subtests ofTestARewriteIsChargedToTheNewChangeNotTheRepeatedOne; the test wantscs2, so it fails on base.TestARepeatedChangeKeepsTheChargeWhenNoNewChangeCoversTheFirstDifferencepasses 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 fromreadEndand B*, so the change moves attribution between changes, no token figure. Check 5's invariant holds:breakItemAtreturns an item of this request (reported or active). - Ran, in this worktree:
gofmt -l .(nothing),go vet ./...(clean),go test ./...(all packages ok, includingtest/cases). - Checked the repo rules: no new import (R3 holds), no public API change, docs updated in the same PR for a
Computebehavior change (CLAUDE.md rule 5).
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Behavior change
ChangeIDof the break changes.test/casesis unchanged and all cases pass.Computeresults).Summary
Fixes rossoctl/context-guru#1351. The accountant charged a sweep's cache rewrite to
searchfold.What we found
What changed
breakItemAtpicks the covering operation whose change is new on this request.FirstAppliedtells which. A re-report of an older change is not new.docs/design.mdrow 1 describes the rule.How it was tested
go test -race ./...passes.gofmt -l .,go vet ./...andgo mod tidy -diffare clean.TestARewriteIsChargedToTheNewChangeNotTheRepeatedOne, two variants (fold stays active, fold reported again). It fails on origin/main (break charged tocf) and passes now.TestARepeatedChangeKeepsTheChargeWhenNoNewChangeCoversTheFirstDifferenceguards the fallback.Assisted-By: Claude Opus 5.5 [email protected]