Repository navigation
fix(sessions): keep the usage anchor across a stopped run's abort marker - #176969
leapdragon wants to merge 1 commit into
Conversation
The transcript usage tail scan treats any assistant message whose usage is stamped `contextUsage: unavailable` with no token counts as a barrier: it stops scanning and discards the trailing messages, so the preflight has no usage anchor at all. The gateway stamps exactly that on the host-authored partial it appends when a run is stopped (`chat-transcript-inject.ts`), so the first prompt after every abort falls back to estimating the whole raw transcript projection. On a session owned by a lossless context engine the raw transcript is never trimmed, so that estimate is far above what the model actually sees (about 460k tokens against 45 to 73k real on a 262k-window session) and the host runs the pre-compaction memory flush plus a forced engine compaction on the next turn, every time the user stops a run. On this host each of those turns cost 4 to 5 minutes of flush prefill on the configured flush model. The abort marker carries no model call: its unavailable usage exists so the persisted session total is re-derived, not because the context after it is unknown. Treat it as trailing growth over the previous usage anchor instead of a barrier. Other unavailable snapshots keep their barrier semantics. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs real behavior proof before merge. What this changesPreserve the previous transcript usage anchor when scanning past a Gateway-authored abort partial. Example: A transcript contains a 72,912-token prompt usage snapshot followed by a Gateway-injected abort partial.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Regression provenancePossible regression — suspected (reviewed change). No predecessor PR is attributed. Merge readiness⛔ Blocked before merge - 1 item remains This PR addresses a concrete accounting failure still present on current main, and its narrow exception preserves ordinary unavailable-usage barriers; no introduced correctness defect was identified. Priority: P2 Before merge
FindingsNone. Tests
Agent review detailsHow this fits togetherGateway cancellation persists an assistant partial; the shared transcript accounting policy reads stored events and supplies usage anchors and trailing messages to preflight token estimation. flowchart TD
A[Stopped Gateway run] --> B[Persisted abort partial]
B --> C[Shared transcript accounting]
D[Earlier provider usage] --> C
C --> E[Preflight token estimate]
E --> F[Memory flush and compaction decisions]
Technical reviewBest possible solution: Keep abort partials as trailing growth in the existing shared accounting owner while preserving genuine unavailable-context, reset, and compaction barriers. Do we have a high-confidence way to reproduce the issue? Source inspection establishes the producer-to-barrier-to-fallback path; no target code was executed during this read-only review. Is this the best way to solve the issue? The exception is a small fix at the shared decision owner, serves durable and memory accounting consumers, and leaves actual unknown-usage boundaries intact. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against b37e563d26a3. Provenance checked
TestingProof path: unit tests only. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +18, Tests +76. Total +94 across 2 files. Added test files: 1. View PR surface stats
LabelsLabel changes: No label changes. Label justifications:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed October 19, 2026, 1:45 AM ET / 05:45 UTC. |
|
This pull request has been automatically marked as stale after 7 days without qualifying human activity. |
|
Closing due to inactivity. |
Summary
The transcript usage tail scan (
session-transcript-accounting-policy.ts) treats any assistant message whose usage is stampedcontextUsage: { state: "unavailable" }with zero token counts as a barrier: it stops scanning and discards the trailing messages, leaving the preflight with no usage anchor. The gateway stamps exactly that on the host-authored partial it appends when a run is stopped (chat-transcript-inject.ts,openclawAbort), so the first prompt after every abort falls back to estimating the whole raw transcript projection.On a session owned by a lossless context engine the raw transcript is never trimmed, so that estimate is far above what the model sees. On the reporting host (lossless-claw, 262k window) the preflight saw about 460k tokens while the model's own usage was 45 to 73k, and on every post-abort turn it ran the pre-compaction memory flush (4 to 5 minutes of prefill on the flush model) plus a forced engine compaction that the engine itself reported as unnecessary (
shouldCompact=false).The abort marker carries no model call: its unavailable usage is there so the persisted session total gets re-derived, not because the context after it is unknown. The previous usage anchor plus the messages appended since (the partial included) still bounds the next prompt.
Change
isGatewayInjectedAbortMarker(message.openclawAbort.aborted === true) exempts the stopped run's partial from the unavailable-context barrier; it is counted as a trailing message over the previous anchor instead.Tests
New
session-transcript-accounting-policy.test.ts:Also ran
agent-runner-memory*.test.tsandmemory-flush.test.ts(all passing), tsgo, oxlint, oxfmt.Observed sequence (before)
Every turn not preceded by an abort prepared in under 2 s. The forced compaction re-arms the "one flush per compaction" guard, which is why the flush repeated.
Follow-up (not in this PR)
When no usage anchor exists at all,
estimatePromptTokensFromSessionTranscriptstill estimates the full model-context projection, which overstates sessions owned by a context engine. That fallback could consult the engine or cap the projection, but it is a larger change and the abort case was the one hitting it every day.🤖 Generated with Claude Code