Skip to content

fix(proxy): check stash coverage and ask on one snapshot - #571

Merged
OsherElhadad merged 4 commits into
mainfrom
fix/prefix-ask-single-snapshot
Oct 21, 2026
Merged

OsherElhadad merged 4 commits into
mainfrom
fix/prefix-ask-single-snapshot

Conversation

@OsherElhadad

@OsherElhadad OsherElhadad commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

What

The cache-aware summarizer's detached call checks PrefixCoverage.CoversSpan and then calls Ask, 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. Returns ErrStalePrefix / ErrNoPrefix as the two-step path did. prefixAsker and responsesPrefixAsker implement 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 through AskCovered with the guard's span, and a refused snapshot counts as sweep_stale_prefix.
    go test ./components/... ./proxy passes.

@OsherElhadad OsherElhadad 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.

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.

  • put always installs freshly allocated copies (make + copy for both bodies), and replacement and eviction only delete map entries. No writer touches an existing entry's backing arrays.
  • The ask path only reads them. insertAskMessage builds a new messages array and calls sjson.SetRawBytes / SetBytes / DeleteBytes without ReplaceInPlace, so sjson allocates. CompletePrefixedResponses does the same.
  • This matches the old get-then-Ask path, which already read forwarded outside 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
  • put never stores an empty forwarded, so the extra len(e.forwarded) == 0 check 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 AskCovered branch disabled, TestCacheAskCarriesTheApprovedBodyWhenTheStashIsReplaced and TestCacheAwareChecksAndAsksOnOneSnapshot both fail.
  • gofmt and go vet are clean. go test -race -count=10 passes for ./proxy and ./components/... (-run 'CacheAware|Prefix|Ask|Stash|Sweep|Covered'). CI is green, and the commit is signed off.

Non-blocking:

  1. The proxy side's single snapshot is not pinned. With snapshotCovering changed to return s.get(session), nil (re-read after the check), every proxy Covered test 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 -race loop, would pin it.
  2. 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 can put in between, after another upstream call returns. The window is far smaller than the summarizer's detached one, but switching the sweep to AskCovered when it is available would close it for free.

This approval covers c1600d7.

@OsherElhadad OsherElhadad 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.

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 passing CoversSpan) and asks through AskCovered on that span. Without coverage information, or with an asker that lacks AskCovered, it uses the old Ask. A refused snapshot is gated sweep_stale_prefix and not booked as a call. AskCovered returns ErrStalePrefix before any request is built, so no model call happened.
  • Mutations. With the AskCovered branch disabled, or the sweep_stale_prefix gate removed, TestSweepAsksOnTheSnapshotItChecked fails. With snapshotCovering re-reading the stash for the returned body, the new TestSnapshotCoveringReturnsTheEntryItChecked fails, which closes my earlier note.
  • gofmt and go vet are clean. -race -count=10 passes for ./components/offload and ./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 OsherElhadad 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.

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.

@OsherElhadad
OsherElhadad force-pushed the fix/prefix-ask-single-snapshot branch from ad28890 to 25d59a2 Compare October 15, 2026 16:31

@OsherElhadad OsherElhadad 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.

Re-confirm: APPROVE at 25d59a2. Rebased onto today's main. git range-diff against the approved ad28890 shows all 3 of the PR's own commits identical (=), CI is green, and the commits are signed off. This approval covers 25d59a2.

@OsherElhadad OsherElhadad mentioned this pull request Oct 21, 2026
14 of 31 tasks
Osher-Elhadad and others added 4 commits October 21, 2026 17:08
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]>
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]>
@OsherElhadad
OsherElhadad force-pushed the fix/prefix-ask-single-snapshot branch from 25d59a2 to 1536efc Compare October 21, 2026 17:25
@OsherElhadad
OsherElhadad merged commit 57beb66 into main Oct 21, 2026
4 checks passed
@OsherElhadad
OsherElhadad deleted the fix/prefix-ask-single-snapshot branch October 21, 2026 17:49
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 21, 2026
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.

1 participant