Skip to content

fix(offload): cache_aware_summarizer cut every span at quoted marker text - #705

Merged
amiddavid merged 3 commits into
mainfrom
fix/summarizer-never-acts
Oct 25, 2026
Merged

amiddavid merged 3 commits into
mainfrom
fix/summarizer-never-acts

Conversation

@amiddavid

@amiddavid amiddavid commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The cache-aware summarizer never acted on a live xhigh proxy: 0 of 2,597 runs. Two separate causes block it on long threads. This PR fixes both. Also, every decline path now records a gate.

What we found

Live proxy, port 8789, preset xhigh, one Opus 5.5 thread with 2,169 to 2,231 messages and 901k to 925k billed tokens:

Fact Value
Turns that passed the 0.9 trigger 22 (the first at 901,946 billed tokens; the limit is 900,000)
Summaries started 30 (22 on a turn, 8 at a keep-alive ping)
Messages each one covered 4 (the log line shows covered_messages=4 30 times)
Summaries that ended as no_prefix 30 of 30
Summaries committed 0
Runs with no gate since the proxy started 1,061: 1,039 short side requests (11 messages or fewer), 22 that started a summary

Why it happens

  1. The span stops at quoted marker text. The span trim asked skipReduce. That also asks expand.HasPlaceholder, which matches any <<cg:...>> text. The thread's 7th and 9th entries are file reads of this repo's docs, and they quote <<cg:HASH>>. So the span stopped there, and every summary covered 4 messages. summarize does not have this fault: it asks only for kept-verbatim and restored content.
  2. The proxy does not keep a large body. sentStash.put refuses a body over maxSentBody (1,500,000 bytes, proxy/prefixask.go). A thread near a 1M-token window is several MB. So the thread was never stashed, and the summary call found nothing to ask over (ErrNoPrefix). /stats reported this the same way as a first turn.
  3. No gate on 3 paths. These paths returned with no gate: a transcript shorter than keep_last_turns, a span below min_tokens, and no model. A turn that started a summary also recorded nothing.

What changed

  • cache_aware_summarizer: the span trim asks only for kept-verbatim and restored content, as summarize does. A real marker inside the span is safe to summarize, because the span is stashed whole under the summary's own marker.
  • New gates: summary_span_empty, summary_span_empty_after_expand_trim, summary_span_below_min_tokens, no_model. New event: summary_started.
  • Proxy stash limits, as the owner decided: maxSentBody goes from 1.5 MB to 8 MB per body. maxSentBytes goes from 96 MB to 320 MB for the whole stash. A 1M-token window is about 4 MB of request JSON, so 8 MB leaves twice that room.
  • Worst-case memory: one entry holds 2 bodies (forwarded and pre-pipeline) of up to 8 MB each, so 16 MB per entry. The whole stash is capped at 320 MB. Most installs are a local proxy for one person. A shared proxy is the case to monitor, tracked in Monitor the prefix stash's memory on shared proxies #704. The bounds comment in proxy/prefixask.go says this.
  • /stats gains prefix_stash_oversized: the count of bodies the proxy did not stash because of the cap. It is not exported to Prometheus yet, the same as the other PrefixAsk counters.
  • Docs: docs/components/advanced-offload.md lists the new counter and the gates.

How it was tested

All runs are on the eval box.

Run Tree Result
TestCacheAwareSpanIsNotCutByMarkerTextInAnEarlyMessage origin/main + test FAIL: turn 1 started no summary: gates map[], events map[] (as on the live proxy)
TestCacheAwareEveryDeclineRecordsAGateOrEvent (4 cases) origin/main + test FAIL, all 4: gates map[]
both new tests this branch PASS
mutation: put skipReduce back in the trim this branch FAIL: gates map[summary_span_empty_after_expand_trim:1]
TestAnOversizedBodyIsNotStashed (asserts the new counter) this branch PASS
TestAFullWindowBodyIsStashed (4.5 MB body) this branch PASS
mutation: maxSentBody back to 1.5 MB this branch FAIL: a 4500000-byte full-window body was not stashed (cap 1500000)
TestCacheAwareStalePrefixThroughARealOversizedTurn (payload now maxSentBody + 100 KB) this branch PASS
go test -race ./components/... this branch PASS
go test -race ./proxy/... this branch, head with the new limits PASS (450 s)
go test ./metrics/..., go vet, gofmt -l this branch PASS, clean

Data dependence

No change in kind. The PR stores no new kind of data and reads nothing new. The body stash already held full request bodies (docs/data-dependence.md, Body stash row, unchanged). It can now hold larger bodies, up to 320 MB in total, in memory only. The new counter is a count only.

Follow-up

Fixes #703

Technical notes

🤖 Generated with Claude Code

…text

The span trim asked skipReduce, so expand.HasPlaceholder stopped the span
at the first message whose text holds `<<cg:...>>`. A session that reads
this repo's docs carries that text from an early tool result on. On a
live 900k-token thread, every one of 30 commissions covered 4 of about
2,200 messages. The trim now asks only kept-verbatim and restored
content, as summarize does.

Every decline path now records a gate (summary_span_empty,
summary_span_empty_after_expand_trim, summary_span_below_min_tokens,
no_model), and a started commission records summary_started.

/stats gains prefix_stash_oversized: bodies the proxy did not stash
because they were over maxSentBody. That cap is why all 30 commissions
on the same thread failed as no_prefix.

Refs #703

Assisted-By: Claude
Signed-off-by: DAVID AMID <[email protected]>
It sits with the other PrefixAsk counters, which are not on Prometheus
either.

Refs #703

Assisted-By: Claude
Signed-off-by: DAVID AMID <[email protected]>
maxSentBody was 1.5 MB. A thread near a 1M-token window is about 4 MB
of request JSON, so it was never stashed, and every summary at the 0.9
trigger ended as ErrNoPrefix (30 of 30 on the live proxy). The cap is
now 8 MB per body and 320 MB for the whole stash, as the owner decided.

Worst case: one entry holds 2 bodies of up to 8 MB, and the stash is
capped at 320 MB. A shared proxy is the case to monitor (#704).

Fixes #703

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 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 003837ff48dd approve

Verdict: approve

Findings

No blocking issues found.

Non-blocking

  • proxy/prefixask.go:46-58 — Documentation clarity
    • The comment block explaining the new 8 MB cap is thorough, but the phrase "MUST HOLD A FULL-WINDOW BODY" could be misread as a hard requirement rather than a design goal. Consider: "SHOULD HOLD A FULL-WINDOW BODY to avoid ErrNoPrefix on large threads."
    • Impact: Documentation only, no functional issue.

What I checked

  • Core fix correctness (cache_aware_summarizer.go:639-644): The change from skipReduce to only isKeptVerbatim is correct. The old code used expand.HasPlaceholder, which matches marker TEXT (e.g., <<cg:HASH>> in documentation) as well as real markers. This caused spans to stop at the first message containing marker text, even when that text was just documentation content. The fix correctly distinguishes between:

    • Marker TEXT in content (safe to summarize, as the whole span gets stashed)
    • Actually expanded content (must not be re-summarized, checked by isKeptVerbatim)
  • Reversibility preserved: When a real marker exists inside a span being summarized, the entire span (including that marker) is stashed under the summary's own marker. The nested marker resolves through the same content-hash keys after expansion, so reversibility is maintained.

  • Test coverage (cache_aware_span_test.go): Two new tests cover the fix:

    1. TestCacheAwareSpanIsNotCutByMarkerTextInAnEarlyMessage: Verifies that marker text in docs doesn't truncate the span. The test creates a message with both marker text and an actual marker, confirms the span covers the expected message count, and verifies the summary is commissioned and later spliced.
    2. TestCacheAwareEveryDeclineRecordsAGateOrEvent: Ensures every decline path records a gate or event. This addresses the observability gap where 1,030 runs declined with no gate recorded.
  • Prefix stash cap increase (prefixask.go:63-75): The increase from 1.5 MB to 8 MB is justified. A 1M-token window produces about 4 MB of request JSON, and the summarizer triggers at 90% of capacity. At the old 1.5 MB cap, threads above ~375k tokens were never stashed, causing every summary to fail as ErrNoPrefix. The new 8 MB cap provides 2x headroom. The worst-case memory bound (320 MB total) is explicitly documented.

  • Observability improvements:

    • New prefix_stash_oversized counter tracks bodies refused for exceeding the cap, distinguishing this from first-turn no_prefix cases.
    • New gates added: summary_span_empty, summary_span_empty_after_expand_trim, summary_span_below_min_tokens, no_model.
    • New event summary_started records when a summary is commissioned asynchronously.
  • Fail-open behavior: All changes maintain fail-open guarantees. When a body exceeds the cap, it is not stashed (rather than evicting others), and the next turn's prefix ask returns ErrNoPrefix, causing a decline. This is a forgone opportunity, not a wrong answer.

  • Data dependence: No change to data dependence. The component still requires stored content (summaries are model-generated text that differs each time).

  • Wire-group alignment: The fix does not affect wire-group handling. The trimSpanForKeptVerbatim function is called after summarizeSpan computes boundaries, and both respect tool-pairing atomicity.

  • Metrics wiring: The new PrefixStashOversized counter is correctly wired through proxy.go:2347 to the /stats endpoint and documented in promexport_coverage_test.go:189 as not yet exported to Prometheus.

  • Test for full-window body (prefixask_test.go:364-376): Verifies that a 4.5 MB body (representing a full 1M-token window) is successfully stashed under the new 8 MB cap.

  • Documentation updates:

    • docs/components/advanced-offload.md adds the new prefix_stash_oversized counter and explains the gate sequence for declined summaries.
    • Comments in the code clearly explain the reasoning for each change, including the #703 issue reference.
  • Backward compatibility: The changes are backward compatible. Existing configurations continue to work, and the new behavior only affects sessions with large request bodies or marker text in content.

@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 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 003837ff48dd approve

Verdict: approve

Findings

None. The PR correctly fixes issue #703 and adds proper observability.

Non-blocking

  • The fix is well-targeted: it removes the false-positive check (expand.HasPlaceholder) that treated marker TEXT in docs as real markers, while keeping the actual expanded-content check (isKeptVerbatim).
  • The comment at lines 640-643 clearly explains why marker text inside a span is safe to summarize (the whole span is stashed, so reversibility is preserved).
  • The pattern now matches summarize.go's own span trimming (line 530), which already used only isKeptVerbatim + restoredSpanGuard.
  • The maxSentBody increase from 1.5 MB to 8 MB is justified: a 1M-token window is about 4 MB of JSON, and the old cap refused every thread above ~375k tokens. The new cap holds a full window with room to spare.
  • The new PrefixStashOversized counter properly distinguishes refused bodies from first-turn no_prefix, fixing the observability gap where 30 of 30 summaries on a 900k-token thread failed as no_prefix with no way to see the cap was the cause.
  • Gate coverage is complete: every decline path now records a gate or event, addressing the #703 finding of 1,030 runs with no explanation.
  • Test coverage is good: TestCacheAwareSpanIsNotCutByMarkerTextInAnEarlyMessage reproduces the exact bug (docs with marker text → 4-message span) and verifies the fix (span covers the full fixture).

What I checked

  • Read the full diff and the code around each change
  • Verified the fix removes only the false-positive check while preserving the actual expanded-content guard
  • Confirmed the pattern matches summarize.go's existing implementation
  • Checked that skipReduce itself is unchanged, so other components are unaffected
  • Verified the maxSentBody increase is bounded (320 MB total cap) and justified (holds a full 1M-token window)
  • Confirmed all new gates and events are properly named and documented
  • Reviewed test coverage for both the span-trimming fix and the gate-coverage requirement
  • Checked that the new counter integrates correctly with /stats and the Prometheus export coverage test

@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.310 offload-review skill, @amiddavid's session fresh 1 of 2 2 of 2 003837ff48dd approve

Verdict: approve

Findings

None. Nothing changed since round 1: no new commit, no new comment, no author reply.

Non-blocking

  • proxy/prefixask.go:46-49 — the round-1 doc note stands, unaddressed. "MUST HOLD A FULL-WINDOW BODY" reads as a hard requirement where it states a design goal. "SHOULD HOLD ... to avoid ErrNoPrefix on large threads" would be clearer. Not addressed because the head did not move. Still documentation only.

What I checked

  • Last review: round 1, 003837f, 2026-10-25T17:09:18Z
  • New commits read: 0. git log --stat 003837ff..HEAD printed nothing (rc=0). The head is the same as round 1, as expected.
  • Read in full: 2 reviews, 0 comments, 0 inline comments (from the three saved files)
  • New items since my last review: none. Both round-1 reviews (reviewer 1 and reviewer 2) approved; no person commented on the PR.

Earlier findings, this round:

  • Reviewer 1, round 1, non-blocking (the "MUST HOLD" phrasing): not fixed, not rejected — no commit and no reply. It stays non-blocking; see above.
  • Reviewer 2, round 1: no findings. Nothing to track.

Because my context is fresh, I re-verified the round-1 claims myself against the head, not against the round-1 text:

  • Span trim fix. skipReduce (state.go:434-444) returns skip when expand.HasPlaceholder(content) is true, and that matches marker TEXT quoted in docs, not only real markers. The change at cache_aware_summarizer.go:644-645 drops that half and keeps isKeptVerbatim || restoredSpanGuard. That predicate is byte-for-byte the one summarize.go:529-530 already uses for the same trim. Confirmed correct.
  • Reversibility with a real marker inside the span. The whole span is marshalled and stashed under hashKey(spanJSON) (cache_aware_summarizer.go:924-931, cache_aware_async.go:232-237, and the checkpoint refreshes at cache_aware_summarizer.go:1105-1110). The inner marker's own stash keys are untouched, so expand still resolves them. Confirmed reversible.
  • Gate coverage. I walked every return path in Offload (cache_aware_summarizer.go:610-983). Each decline now records a gate or an event: summary_span_empty, summary_span_empty_after_expand_trim, below_request_trigger, window_not_exact, cache_state_declined_*, summary_span_below_min_tokens, request_over_max_tokens, no_messages_model/no_model, thinking_would_be_stripped, the role gates, no_stash_room, cold_commission_deferred, or the new summary_started event. I found no silent decline left.
  • Stash cap. One entry holds two bodies of up to 8 MB, so 16 MB; maxSentBytes (320 MB) is the outer bound and evicts oldest first (prefixask.go:177-186). An oversized put deletes the old entry and counts prefix_stash_oversized (prefixask.go:157-165). Fail-open holds: the next ask returns ErrNoPrefix, which callers handle.
  • Wiring and tests. PrefixStashOversized reaches /stats (proxy.go:2347), the golden list, and the not-exported-yet map. Tests cover the marker-text span bug, the gate coverage, the oversized refusal count, and the full-window stash.
  • Data dependence. No change. The stash is host memory, not the Store, and no component's data dependence moved.

@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.310 offload-review skill, @amiddavid's session fresh 2 of 2 2 of 2 003837ff48dd approve

Verdict: approve

Findings

None. Round 2 found no defect.

Earlier findings

  • Reviewer 1, round 1 (doc wording, "MUST HOLD" → "SHOULD HOLD"): not addressed, no author reply. I checked it myself. The comment states an invariant the cap must satisfy for the prefix ask to work at all, not a rule for a caller. "MUST" is defensible. Non-blocking either way.
  • My round 1 raised no blocking finding. Nothing to re-check as fixed.

Non-blocking

  • docs/components/advanced-offload.md:1054 — the new gate-order sentence omits two real decline gates: wire_groups_stale (cache_aware_summarizer.go:623) and side_request_read_only (line 704). A reader auditing "acted: 0" turns can still hit a gate the list does not name. Doc completeness only.
  • cache_aware_summarizer.go:644 — the trim runs before tryReuse, while summarize.go:517-523 moved its own trim below tryReuse because a lowered end can turn a reusable checkpoint into a full-transcript send. Pre-existing order, not this PR. This PR only narrows the trim's predicate, so it lowers end in fewer cases than before — the risk shrinks. Worth aligning in a follow-up, not a blocker here.

What I checked

  • Last review: round 1, 003837f, 2026-10-25T17:10:03Z
  • New commits read: 0. git log --stat 003837ff..HEAD is empty; the head is the same as round 1, as the brief said to expect.
  • Read in full: 2 reviews, 0 comments, 0 inline comments (from .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt)
  • New items since my last review: none. Both round-1 reviews predate my LAST_TIME (17:09:18 and 17:10:03). No issue comments, no inline comments, no author replies exist on the PR.
  • Re-read the full diff (0a22b9ca..HEAD, 10 files) and the code around each change, with fresh eyes:
    • Core fix (cache_aware_summarizer.go:644-646): the trim now asks only isKeptVerbatim || restoredSpanGuard. Verified summarize.go:529-530 already uses exactly that pair — the change removes the one half (expand.HasPlaceholder via skipReduce) that summarize never had, and that matched marker TEXT in ordinary content.
    • Reversibility: a real marker inside the span stays reversible. commitAsyncSummary stashes the whole span under the summary's own key in full mode (cache_aware_async.go:231-241), so expanding the summary restores the inner marker, which then resolves as before. summary/off modes are operator-opted lossy drops and already behaved this way in summarize.go.
    • Gate coverage: walked every return path in Offload. Each decline now records a gate or event: summary_span_empty, summary_span_empty_after_expand_trim, side_request_read_only, below_request_trigger/window_not_exact, cache_state_declined_*, summary_span_below_min_tokens, request_over_max_tokens, no_messages_model/no_model, thinking_would_be_stripped, system_role_unsupported_via_prefix_ask, unverified_system_role, no_stash_room, !phased (gate raised at line 731), cold_commission_deferred, and the new summary_started. The only gateless return is the json.Marshal error path (line 925), which returns an error the pipeline reverts — fail open, not a silent decline.
    • summary_started event: startAsyncSummary returns "" only after the detached goroutine is launched (cache_aware_async.go:124-203), so the event never fires on an in-flight or concurrency refusal — those return their own gates.
    • Stash caps (prefixask.go): 8 MB per body, 16 MB per entry (2 bodies), 320 MB total — the math in the comment checks out, and the byte cap binds before the 256-entry bound for full-window bodies. The put/evict logic is unchanged; only the constants moved. No stale 1.5 MB/96 MB reference remains in code or docs.
    • New counter: sentStashOversized increments only on the oversized refusal path; wired to /stats (proxy.go:2347), the golden list, and the promexport not-yet-exported map. Consistent.
    • Tests: all helpers the new tests use exist (caFixture, caToolPair, WaitForSummaryForTest, loadCheckpoint). The span test asserts CoveredCount equals the untrimmed span, which the old skipReduce trim would have cut to nothing — the PR body's mutation runs confirm both directions. The oversized-turn integration test now sizes its payload from maxSentBody instead of a hardcoded 1.6 MB, so it cannot silently go under the cap again.
    • Data dependence: PR body states it — no change in kind, sizes only, docs/data-dependence.md row unchanged. No new component registered, so datadep_test.go is unaffected.
  • Live tests were not approved; this review is code-reading only, as the brief requires.

@amiddavid
amiddavid merged commit 2e29045 into main Oct 25, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 25, 2026
amiddavid added a commit that referenced this pull request Oct 26, 2026
…ion (#725)

fix(cheapmodel): move the trailing breakpoint onto the appended question

Claude Code 2.1.310 ends each request with a system message that holds the
last cache_control breakpoint. The prefix ask kept it there and appended the
bridge and the ask with none. The provider then missed the turn's cache entry
and the question rewrote the conversation. The sweep gate refused every
answer.

- insertAskMessage moves the last message's breakpoint (same object, ttl
  kept) onto the bridge, or onto the ask when there is no bridge. It adds
  none when the last message has none.
- The sweep write gate refuses a write only when it is larger than the
  tokens the question appended, plus a named margin.
- The ping candidate scan skips only real markers. Text that quotes marker
  syntax is a candidate (same fault as #705).

Fixes #721

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
fix(offload): judge a prefix-ask answer by its read, behind one function

The provider never reads a cache entry that ends at a text turn, so a
correct question always rewrites that turn's tail. A write limit would
still refuse most questions.

prefixAskCacheOK is now the one decision. It refuses a read of 0 and a
read below the last real turn's read. An optional write cap (min of 2
percent of the prefix and 2,000 tokens) is off until the owner decides.
The keeper passes the turn's cache_read to the sweep through
SweepPingWait. The old "write > appended tokens" allowance is removed.

Refs #721

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
fix(offload): ship the prefix-ask write cap on, as one constant

The owner has not chosen the cap yet. prefixAskWriteCapEnabled is one
constant, so the cap can go without a rewrite. Adds table rows for a
typed-prompt tail write, and a test that the shipped switch is on.

Refs #721

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
fix(cheapmodel): keep the breakpoint off the ask, one rule for the marker check

Review round 1 of #725.

- A trailing user message that carries a breakpoint now also gets the
  "Understood." bridge, and the breakpoint moves onto the bridge. The ask
  stays fresh input. Before, the breakpoint sat on the ask and the cache
  write held the whole ask (about 1,000 to 1,400 tokens), so the write cap
  refused every question in that shape.
- The cold apply and the replay use carriesRealMarker, the same rule as the
  ping's candidate scan. A drop on an output that only quotes marker text now
  applies. A skip on a real marker is counted (sweep_apply_already_marked).
- Docs: the read rules in advanced-offload, the Store presence read in
  data-dependence.

Refs #721

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
fix(offload): ship the prefix-ask write cap off

The owner chose the read rule only (2026-10-26). The cap code and its
constants stay behind prefixAskWriteCapEnabled, now false. One constant
turns it on.

Refs #721

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
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.

cache_aware_summarizer never summarizes on xhigh: 0 of 2,547 runs, ~1,030 decline with no gate

1 participant