Skip to content

fix(offload): cover the last tool exchange in an OpenAI summary - #732

Merged
amiddavid merged 2 commits into
mainfrom
fix/openai-summary-span
Oct 26, 2026
Merged

amiddavid merged 2 commits into
mainfrom
fix/openai-summary-span

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

Fixes #730.

What changed

  • components.Ctx.NoCacheBreakpoint: new field. The host sets it from the provider (!explicitBreakpointProvider).
  • cache_aware_summarizer: skips retreatPastLastToolExchange when the field is true. Anthropic, Bedrock and Vertex behave as before.
  • keepalive/substitute.go, keepalive/openaiphase_test.go: comment text now matches the 240 s default idle.
  • apply/apply.go: one line added to the Ctx literal. The rest of the diff there is gofmt realignment.

Fail-open is unchanged. The zero value of the new field keeps the old retreat.

How it was tested

On the eval box, with -race:

  • ./apply/... ./components/... ./keepalive/...: all pass.
  • go vet ./...: clean.

New tests:

  • TestSummarySpanRetreatsOnlyWhereABreakpointCanBeWritten (offload): OpenAI covers more than the retreated span, Anthropic covers exactly it.
  • TestTheSummaryCoversTheLastToolExchangeOnlyOnOpenAI (apply): the spliced OpenAI body holds no last-exchange text.

Mutation checks:

  • Forced the retreat on in the summarizer: the offload test fails.
  • Set the host field to false: the apply test fails.
  • Both restored.

Data dependence

  • Uses metadata only: the provider name.
  • Off / metadata only / everything: no change at any level. The field reads no stored customer data.
  • docs/data-dependence.md does not change.

🤖 Generated with Claude Code

The summary span stopped before the last tool exchange so the host
could anchor a cache breakpoint on it (#727). OpenAI takes no explicit
breakpoint, so the retreat only cost savings there.

- Add Ctx.NoCacheBreakpoint, set from the provider by the host.
- cache_aware_summarizer retreats the span only when a breakpoint can
  be written (Anthropic, Bedrock, Vertex).
- Fix the stale "300 s - 270 s" text in keepalive/substitute.go and
  keepalive/openaiphase_test.go.

Assisted-By: Claude
Co-Authored-By: Claude Opus 5.5 (1M context) <[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 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 1 of 2 0c90804f5e91 blocking

Verdict: blocking

Findings

  1. apply/responses.go:568 — the Responses route never sets NoCacheBreakpoint, so OpenAI Responses traffic still keeps the last tool exchange verbatim.
    • What fails: a request with API: "responses" (always OpenAI-shaped, no cache_control on that wire) and cache_aware_summarizer in the pipeline gets its Ctx from bodyResponsesOpts, which leaves NoCacheBreakpoint at the zero value false. Offload then still runs retreatPastLastToolExchange (components/offload/cache_aware_summarizer.go:745-747), so the last function-call exchange stays out of the summary — and nothing marks it, because the Responses rebuild path never calls anchorCacheBreakpoint. That is the exact waste #730 describes, on the proxy's other OpenAI wire.
    • Proof: apply/apply.go:396-398 routes o.API == "responses" to bodyResponsesOpts before the Ctx at apply/apply.go:666 that sets the flag; the Ctx at apply/responses.go:568-591 sets no NoCacheBreakpoint; the route runs cache_aware_summarizer end to end (apply/responses_test.go:543, TestResponsesCacheAwareOneMessageSpliceIsWritten); the Responses wire normalizes function_call/function_call_output into assistant/tool messages (apply/responses.go:71-78), so the retreat finds an exchange there; and apply/apply.go:787's own anchor gate already treats OpenAI as breakpoint-less, while responses.go has no anchor call at all.
    • Fix: set NoCacheBreakpoint: true in bodyResponsesOpts (that wire is always OpenAI-shaped). Add a Responses-route test that the last function_call_output is summarized away, mirroring TestTheSummaryCoversTheLastToolExchangeOnlyOnOpenAI.

Non-blocking

  • adapters/bifrost/plugin.go:51 builds its Ctx without the flag too. Today it is latent: with the default min_request_frac 0.9 and no CtxWindowExact on that host, the summarizer declines at window_not_exact before the retreat. chat.Provider is at hand there if you want the hint set anyway, so a later config change cannot revive the gap.
  • apply/cache_anchor_test.go:229 — TestTheSummaryCoversTheLastToolExchangeOnlyOnOpenAI checks only the OpenAI half at the apply level. The Anthropic half ("the exchange stays") is covered at the unit level only (components/offload/summary_anchor_test.go). The name promises more than the test asserts.

What I checked

  • The full diff 06b40ed2..HEAD, every hunk, and the code around each change.
  • The retreat gate: summarizeSpan's own advanceSpanBoundary keeps the tail start safe without the retreat, so the splice and emitCheckpoint never begin a kept tail mid-exchange.
  • CoveredCount consistency: end-start flows unchanged through registerKeepAliveCandidate, deferSummary, startAsyncSummary, and back as boundary = start + CoveredCount in tryReuse/replayStale. A checkpoint frozen before this change (retreated count) is reused as-is after it, and a retreated boundary is still a safe tail start.
  • CacheAnchorToolID: apply/apply.go:792 is the only consumer and it is gated on explicitBreakpointProvider, so skipping the retreat on OpenAI cannot strand an anchor.
  • The keepalive hunks are comment-only, and the new numbers check out: DefaultKeepAliveIdle = 240 (config/config.go:197), cacheLifetime(Anthropic, false) = 300 s, tick = 2 s, so the widened default window is 64 s. The test keeps its 270 s idle and its 60 s expectation, which still holds (slack 34 s < 60 s).
  • Providers: explicitBreakpointProvider (Anthropic, Bedrock, BedrockMantle, Vertex) matches the anchor gate at apply/apply.go:787. Gemini and unknown providers get NoCacheBreakpoint = true, which only drops the retreat — no fail-open path is affected.
  • The new tests' helpers all exist (pipe, recordingModel, caCtx, caTurn, caFixture, caToolPair, WaitForSummaryForTest, WaitForAllSummariesForTest, convKey, loadCheckpoint) and the imports are present. I did not run them: live tests are not approved, and Go is not installed here.
  • Data dependence: unchanged. The span extent changes, but the stash and marker machinery is untouched, and docs/data-dependence.md has no row that describes the retreat.

@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 1 of 2 0c90804f5e91 blocking

Verdict: blocking

Findings

  1. apply/responses.go:568 — the OpenAI Responses route does not set NoCacheBreakpoint, so fix #730 does not reach it.
    • What fails: an OpenAI /v1/responses request with client-held history (input is an array, no previous_response_id) runs cache_aware_summarizer through this Ctx. The field's zero value is false, so Offload still calls retreatPastLastToolExchange (components/offload/cache_aware_summarizer.go:746). The last tool exchange stays verbatim although no breakpoint can mark it: the route hardcodes Provider: bschemas.OpenAI (apply/responses.go:559), and its rebuild rebuildResponsesCountChanged (apply/responses.go:273) never calls anchorCacheBreakpoint — that call exists only on the Messages route (apply/apply.go:792). This is the exact "kept for nothing" behavior the PR removes on the Chat Completions route, so every summarized Responses turn leaves those tokens unsummarized, and the PR summary line "OpenAI summaries now cover the last tool exchange" is wrong for this route.
    • Proof: TestResponsesCacheAwareOneMessageSpliceIsWritten (apply/responses_test.go:543) shows the summarizer runs and splices on the Responses route, and TestResponsesSetsRawInputBeforeThePipelineRewrites (apply/responses_test.go:513) shows function_call_output items normalize to role:tool messages, which is what the retreat keys on. grep anchorCacheBreakpoint apply/ returns only apply.go:792, so the Responses rebuild writes no anchor.
    • Fix: add NoCacheBreakpoint: true to the Ctx literal in apply/responses.go (the route is always OpenAI, so !explicitBreakpointProvider(bschemas.OpenAI) is a constant true there). Add a Responses-route test that the summary covers the last function-call exchange, mirroring TestTheSummaryCoversTheLastToolExchangeOnlyOnOpenAI.

Non-blocking

  • A conversation that switches provider mid-thread can replay an OpenAI-commissioned checkpoint (exchange covered) on an Anthropic turn. The anchor is recomputed from the spliced output at replay time (lastKeptToolResultID), so it still lands on a kept tool result when the tail holds one; only a text-only tail loses the #723 read. Fail-open, cost only, and rare. No action needed now.
  • Gemini wires get the new behavior too (NoCacheBreakpoint true) with no test. The component test covers OpenAI and Anthropic only. Low risk, since Gemini is not an explicit-breakpoint provider.
  • The two keepalive comment fixes are correct: config.DefaultKeepAliveIdle = 240 (config/config.go:197), so the Anthropic slack is 300 s − 240 s = 60 s and substituteWindow returns 64 s. The old "270 s" text did not match the code.

What I checked

  • Read the full diff (7 files) and the code around each change: apply/apply.go (BodyOpts Ctx), components/component.go (new field), components/offload/cache_aware_summarizer.go (the gated retreat), keepalive/substitute.go and its test (comment-only).
  • Traced every constructor of components.Ctx: apply/apply.go:666 (sets the field from explicitBreakpointProvider), apply/responses.go:568 (does not — the finding), adapters/bifrost/plugin.go:51 (zero value keeps the old retreat, fail-safe), test-only constructors.
  • Verified the gated retreat stays consistent with its consumers: tryReuse/replayStale compare start + CoveredCount against the pre-retreat end, so the larger OpenAI CoveredCount still passes; the commission, deferral (ResolveDeferredCacheAwareSummary), and keep-alive substitute paths all freeze the span and coveredCount at commission time, so none re-derives the retreat later.
  • Verified reversibility: the span including the exchange is stashed under the summary's marker (markerFull default, stash-room check before the call), and splice + advanceSpanBoundary keep the cut on a safe boundary, so no orphaned tool result is created by covering the exchange.
  • Verified the anchor path is untouched for Anthropic: anchorCacheBreakpoint still runs only under explicitBreakpointProvider, and an empty CacheAnchorToolID is a no-op (apply/metawrite.go:239).
  • Checked both new tests against their helpers (caFixture, caToolPair, caCtx, WaitForSummaryForTest, recordingModel): the index arithmetic gives CoveredCount 17 (OpenAI) vs 15 (Anthropic, equal to the retreat) as the test asserts.
  • Checked the PR description's data-dependence statement: metadata only (provider name), no storage change, docs/data-dependence.md unchanged — consistent with the diff.
  • Live tests were not run (not approved); the PR reports them passing on the eval box.

…reakpoint flag (#730)

The Responses route is always OpenAI. It built its Ctx without
NoCacheBreakpoint, so it still retreated the summary span.

- Set NoCacheBreakpoint on the Responses Ctx.
- Set it on the bifrost adapter Ctx, which never writes the anchor.
- Add a Responses test: the summary covers the last function_call_output.
- Rename the apply-level OpenAI test to say what it checks.

Assisted-By: Claude
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Fixed in the new head.

  • Responses route (blocking): apply/responses.go now sets NoCacheBreakpoint. New test TestResponsesSummaryCoversTheLastToolExchange checks that the summary covers the last function_call_output. Mutation: setting the field to false makes it fail. Restored.
  • Bifrost adapter: adapters/bifrost/plugin.go now sets NoCacheBreakpoint: true. That adapter runs the pipeline in place and never writes the anchor, so the retreat only costs savings there.
  • Test name: renamed the apply-level test to TestTheOpenAISummaryCoversTheLastToolExchange. The Anthropic half (retreat and anchor) stays covered by the existing anchor tests and the offload test.

Tests: -race on ./apply/... ./adapters/... ./components/... ./keepalive/... pass. go vet ./... is clean.

@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 resumed 1 of 2 2 of 2 290802fe29b4 approve

Verdict: approve

Findings

None.

Earlier findings

  1. Mine, round 1 — Responses route missing NoCacheBreakpoint (blocking): fixed. apply/responses.go:571 now sets NoCacheBreakpoint: !explicitBreakpointProvider(bschemas.OpenAI), a constant true on that route. The new TestResponsesSummaryCoversTheLastToolExchange (apply/responses_test.go:570) covers it: I traced the indices — 7 normalized messages, end = 7 - keep_last_turns(2) = 5, so the span msgs[1:5] folds in the function_call_output (index 4). With the flag back at false, the retreat moves end to 3 and LASTEXCHANGE stays in the body, so the test fails. The test discriminates.
  2. Mine, round 1, non-blocking — bifrost adapter without the flag: fixed. adapters/bifrost/plugin.go:58 sets NoCacheBreakpoint: true unconditionally. That is right for every provider on that host: the adapter runs the pipeline in place and has no anchor-writing path at all, so the retreat's premise ("the host marks the exchange") never holds there. No Anthropic-via-bifrost regression: that host never had the #723 mark.
  3. Mine, round 1, non-blocking — test name promised an Anthropic half: fixed. Renamed to TestTheOpenAISummaryCoversTheLastToolExchange, and the comment now points at the tests that hold the Anthropic half.
  4. Reviewer 2, round 1, finding 1 — same defect as my finding 1: fixed (checked myself, see item 1; I add no new proof beyond it).
  5. Reviewer 2, round 1, non-blocking items (mid-thread provider switch, Gemini untested, keepalive comment check): all marked "no action needed" by reviewer 2 themselves. The author left them; I agree that is fine.

Non-blocking

  • The bifrost flag has no test. A cheap one exists: run PreRequestHook with a probe component that reads NoCacheBreakpoint off the Ctx. Low value, since the adapter's Ctx sets the flag literally; note it only if you want every host hint pinned.

What I checked

  • Last review: round 1, 0c90804, 2026-10-26T09:57:36Z.
  • New commits read: 1 — 290802fe "fix(apply): give the Responses route and the bifrost adapter the no-breakpoint flag (#730)".
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt; the inline file is empty).
  • New items since my last review: the fix commit above; the author's comment at 2026-10-26T10:11:35Z ("Fixed in the new head", with a mutation check on the Responses test and a -race test report). Both read and checked against the code.
  • The full new diff: adapters/bifrost/plugin.go, apply/cache_anchor_test.go (rename only), apply/responses.go, apply/responses_test.go.
  • explicitBreakpointProvider is package-local to apply, so the new responses.go expression compiles in that package; bschemas.OpenAI yields false, so the field is true on that route.
  • The new test's body shape: function_call/function_call_output items match what normalizeResponses maps (apply/responses.go:71-78); imports (strings, offload, time, context, gjson, recordingModel) are all already used by the neighboring test at apply/responses_test.go:543.
  • I did not run the tests: live runs are not approved and Go is not installed here. The author reports -race passing on ./apply/... ./adapters/... ./components/... ./keepalive/....

@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 resumed 2 of 2 2 of 2 290802fe29b4 approve

Verdict: approve

Findings

None.

Earlier findings

  1. Mine, round 1 — Responses route missing NoCacheBreakpoint (blocking): fixed.
    • apply/responses.go:571 now sets NoCacheBreakpoint: !explicitBreakpointProvider(bschemas.OpenAI) — a constant true, matching the route's hard-coded OpenAI provider (apply/responses.go:559) and its rebuild that never writes an anchor.
    • New test TestResponsesSummaryCoversTheLastToolExchange (apply/responses_test.go:571) drives the route end to end and asserts the last function_call_output text is gone after the splice. I traced the indices: 7 input items normalize to 7 messages, keep_last_turns: 2 gives end = 5, the old retreat would have moved it to 3, and the span [1, 5) now covers the exchange at 3–4. The test would fail under the old behavior, which the author's mutation check also shows.
  2. Reviewer 1, round 1, finding 1 — same Responses-route defect: fixed by the same change. I checked it myself; my proof above stands on its own.
  3. Reviewer 1, round 1, non-blocking — bifrost adapter leaves the flag unset: addressed. adapters/bifrost/plugin.go:59 now sets NoCacheBreakpoint: true. The author's reason is right: that host runs the pipeline in place and never writes the CacheAnchorToolID mark for any provider, so the retreat buys nothing there. Unconditional true is more correct than a provider check, because the anchor write is a host capability, not a provider fact. Today the value is latent anyway (the summarizer declines at window_not_exact on that host), so no behavior change now.
  4. Reviewer 1, round 1, non-blocking — apply test name promised more than it asserted: addressed. Renamed to TestTheOpenAISummaryCoversTheLastToolExchange, with the comment pointing at the Anthropic-half tests above it.
  5. My round-1 non-blocking notes (cross-provider replay edge, no Gemini test): not acted on. Both were marked "no action needed now" / low risk. I accept that.

Non-blocking

  • apply/responses.go:571 writes !explicitBreakpointProvider(bschemas.OpenAI), a constant true. A plain true with the comment would read the same. Style only; no action needed.

What I checked

  • Last review: round 1, 0c90804, 2026-10-26T09:59:19Z.
  • New commits read: 1 — 290802fe "fix(apply): give the Responses route and the bifrost adapter the no-breakpoint flag (#730)". The worktree is checked out at that head.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three saved files).
  • New items since my last review: the commit above and the author's comment at 2026-10-26T10:11:35Z (fix summary). Both read in full and checked against the code.
  • Read the full new diff (4 files) and the code around each change: the Responses Ctx literal, the bifrost Ctx literal, the renamed apply test, the new Responses test.
  • Verified the new test's helpers and imports already exist in apply/responses_test.go (pipe, recordingModel, offload.WaitForAllSummariesForTest, strings, time).
  • Traced the new test's span arithmetic (see finding 1) and the splice path: count change 7 → 4 goes through rebuildResponsesCountChanged, which the existing TestResponsesCacheAwareOneMessageSpliceIsWritten already exercises on this route.
  • Confirmed the bifrost change cannot break Anthropic traffic through that adapter: the flag's only consumer is the summarizer's retreat, tail-start safety stays guaranteed by advanceSpanBoundary, and reversibility is untouched.
  • Confirmed nothing else in the round-1 diff changed: git diff 0c90804f..HEAD touches only the four files above.
  • Live tests were not run (not approved). The author reports -race passing on ./apply/... ./adapters/... ./components/... ./keepalive/... and a clean go vet.

@amiddavid
amiddavid merged commit 00cd7fd into main Oct 26, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 26, 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.

Follow-ups from v0.6.0: the OpenAI summary span, and a stale 270 s comment

1 participant