Repository navigation
fix(proxy): check stash coverage and ask on one snapshot - #571
Conversation
OsherElhadad
left a comment
There was a problem hiding this comment.
Review: APPROVE at c1600d7. (Posted as a comment: GitHub does not let the author account approve.)
(1) Reading the entry's slices outside the lock is safe.
putalways installs freshly allocated copies (make+copyfor both bodies), and replacement and eviction onlydeletemap entries. No writer touches an existing entry's backing arrays.- The ask path only reads them.
insertAskMessagebuilds a newmessagesarray and callssjson.SetRawBytes/SetBytes/DeleteByteswithoutReplaceInPlace, sosjsonallocates.CompletePrefixedResponsesdoes the same. - This matches the old
get-then-Askpath, which already readforwardedoutside the lock.
(2) Missing and stale semantics are identical to CoversSpan + Ask.
| case | old (CoversSpan, then Ask) | new (AskCovered) |
|---|---|---|
| nil stash / empty session | covered → Ask: ErrNoPrefix | ErrNoPrefix |
| session not stashed | covered → Ask: ErrNoPrefix | ErrNoPrefix |
count < 0 |
covered → ask on body | no check → ask on the same body |
fewer than count messages, or hash mismatch |
ErrStalePrefix | ErrStalePrefix |
| covered | ask on the body re-read by get |
ask on the body that was checked |
!rawOK (summarizer side) |
ErrStalePrefix before any call | ErrStalePrefix before any call |
putnever stores an emptyforwarded, so the extralen(e.forwarded) == 0check cannot change an outcome.- The summarizer closure returns the same errors either way, so the stale-prefix and no-prefix counters move identically.
- TTL is not a stash concept (there is no expiry, only eviction).
- Provider is read from the same entry in both.
Production wiring. Both real askers, prefixAsker (Anthropic) and responsesPrefixAsker, implement AskCovered, so the fix is live on the routes the summarizer uses.
Mutations.
- With the summarizer's
AskCoveredbranch disabled, TestCacheAskCarriesTheApprovedBodyWhenTheStashIsReplaced and TestCacheAwareChecksAndAsksOnOneSnapshot both fail. gofmtandgo vetare clean.go test -race -count=10passes for./proxyand./components/...(-run 'CacheAware|Prefix|Ask|Stash|Sweep|Covered'). CI is green, and the commit is signed off.
Non-blocking:
- The proxy side's single snapshot is not pinned. With
snapshotCoveringchanged toreturn s.get(session), nil(re-read after the check), every proxyCoveredtest still passes, because none of them replaces the entry between the check and the ask. A test seam (a hook after the check), or a concurrent-put-raceloop, would pin it. - The extract sweep is not strictly race-free. Its check (
extract_sweep.go:1413) and ask (:1466) run within one request, but overlapping requests on the same thread key canputin between, after another upstream call returns. The window is far smaller than the summarizer's detached one, but switching the sweep toAskCoveredwhen it is available would close it for free.
This approval covers c1600d7.
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-review: APPROVE at ad28890 (2e8be05 sweep + ad28890 proxy test, on top of the approved c1600d7). (Posted as a comment: GitHub does not let the author account approve.)
- Sweep. The sweep keeps exactly the count and hash its guard verified (
haveCover, coverCount, coverHash, set only on a passingCoversSpan) and asks throughAskCoveredon that span. Without coverage information, or with an asker that lacksAskCovered, it uses the oldAsk. A refused snapshot is gatedsweep_stale_prefixand not booked as a call.AskCoveredreturnsErrStalePrefixbefore any request is built, so no model call happened. - Mutations. With the
AskCoveredbranch disabled, or thesweep_stale_prefixgate removed, TestSweepAsksOnTheSnapshotItChecked fails. WithsnapshotCoveringre-reading the stash for the returned body, the new TestSnapshotCoveringReturnsTheEntryItChecked fails, which closes my earlier note. gofmtandgo vetare clean.-race -count=10passes for./components/offloadand./proxy(-run 'Sweep|CacheAware|Prefix|Ask|Snapshot'). CI is green, and both commits are signed off.
Non-blocking: counting a refused snapshot as a call (dropping ErrStalePrefix from the recordLeg exclusion) still passes the new test. One assertion that calls/cost stays 0 on that path would pin it.
This approval covers ad28890.
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-confirm: APPROVE at ad28890, the current head, which includes both follow-up commits. The extract sweep now asks through AskCovered on the span its guard verified. The test-only afterEntryRead seam pins the proxy-side single snapshot: with a second s.get, TestSnapshotCoveringReturnsTheEntryItChecked fails. The full review is in the comment above. This approval covers ad28890.
ad28890 to
25d59a2
Compare
The cache-aware summarizer's detached call checked CoversSpan and then called Ask, which read the thread's stash separately. If the thread's next request replaced the stashed body in between, the ask went out over a body the check never approved. A new optional interface, PrefixCoveredAsker, does both on one read of the stash; the summarizer uses it when the asker implements it, and keeps the old path otherwise. The extract sweep's synchronous check-then-ask is unchanged. Signed-off-by: Osher-Elhadad <[email protected]>
The sweep's coverage guard and its ask read the stash separately, like the summarizer's did. It now keeps the span the guard verified and asks through PrefixCoveredAsker.AskCovered when the asker implements it. A snapshot AskCovered refuses is counted as sweep_stale_prefix and is not booked as a call, since nothing was sent. Signed-off-by: Osher-Elhadad <[email protected]>
Signed-off-by: Osher-Elhadad <[email protected]>
cheapmodel moved out of internal/ and no longer takes BaseURL/APIKey; the AskCovered tests failed to compile after rebasing onto main. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Osher Elhadad <[email protected]>
25d59a2 to
1536efc
Compare
What
The cache-aware summarizer's detached call checks
PrefixCoverage.CoversSpanand then callsAsk, and each reads the thread's stash on its own. If the thread's next request replaces the stashed body between the two reads, the ask goes out over a body the check never approved.Impact: the checkpoint is bound to the span hash (it is only spliced into a later request with the same covered prefix), so a summary made over a different body cannot land on a different transcript. What it can do is spend a call on, and commit, a summary of a longer or different conversation than the span it claims to cover. Narrow window, seconds, same thread only; found in review of #568.
Fix
New optional interface
components.PrefixCoveredAsker.AskCovered(ctx, session, count, hash, ask): one read of the stash entry, the coverage check on that entry's pre-pipeline body, and the ask over that entry's forwarded body. ReturnsErrStalePrefix/ErrNoPrefixas the two-step path did.prefixAskerandresponsesPrefixAskerimplement it; the summarizer uses it when available and keeps the old path for askers that do not.The extract sweep has the same check-then-ask on the request path; it keeps the span its guard verified and asks through `AskCovered` too. A snapshot `AskCovered` refuses is counted as `sweep_stale_prefix` (not `sweep_ask_failed`) and is not booked as a call, since nothing was sent.
Tests
TestCacheAskCarriesTheApprovedBodyWhenTheStashIsReplaced: the stash is replaced right after the check; the ask must carry the approved body. Fails on current main's summarizer, passes here.TestCacheAwareChecksAndAsksOnOneSnapshot,TestAskCovered*(real stash and a capture server: covered, stale hash, missing, hash from a replaced body, no call sent when declined).TestSweepAsksOnTheSnapshotItChecked: the sweep asks throughAskCoveredwith the guard's span, and a refused snapshot counts assweep_stale_prefix.go test ./components/... ./proxypasses.