fix(server): preserve Claude context over cumulative result usage #8617
main fromrefs/pull/8617/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/provider/Layers/ClaudeAdapter.ts lines 2297 to 2298OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2297,20 +2298 @@2297- totalProcessedTokens: accumulatedTotalProcessedTokens,2298- }2299- : {}),2300- }2301- : resultIterationSnapshot) ??2302- (lastGoodUsage2303- ? {2304- ...lastGoodUsage,2305- ...(typeof maxTokens === "number" && Number.isFinite(maxTokens) && maxTokens > 02306- ? { maxTokens }2307- : {}),2308- ...(typeof accumulatedTotalProcessedTokens === "number" &&2309- Number.isFinite(accumulatedTotalProcessedTokens) &&2310- accumulatedTotalProcessedTokens > lastGoodUsage.usedTokens2311- ? {2312- totalProcessedTokens: accumulatedTotalProcessedTokens,2313- }2314- : {}),2315- }2316- : undefined);2298+ contextUsageSnapshot ?? updatedLastGood ?? resultIterationSnapshot;
- apps/server/src/provider/Layers/ClaudeAdapter.ts lines 2262 to 2262OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2263,20 +2262 @@2263- totalProcessedTokens: accumulatedTotalProcessedTokens,2264- }2265- : {}),2266- }2267- : resultIterationSnapshot) ??2268- (lastGoodUsage2269- ? {2270- ...lastGoodUsage,2271- ...(typeof maxTokens === "number" && Number.isFinite(maxTokens) && maxTokens > 02272- ? { maxTokens }2273- : {}),2274- ...(typeof accumulatedTotalProcessedTokens === "number" &&2275- Number.isFinite(accumulatedTotalProcessedTokens) &&2276- accumulatedTotalProcessedTokens > lastGoodUsage.usedTokens2277- ? {2278- totalProcessedTokens: accumulatedTotalProcessedTokens,2279- }2280- : {}),2281- }2282- : undefined);2262+ contextUsageSnapshot ?? updatedLastGood ?? resultIterationSnapshot;
- apps/server/src/provider/Layers/ClaudeAdapter.ts lines 2261 to 2262OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2263,20 +2262 @@2263- totalProcessedTokens: accumulatedTotalProcessedTokens,2264- }2265- : {}),2266- }2267- : resultIterationSnapshot) ??2268- (lastGoodUsage2269- ? {2270- ...lastGoodUsage,2271- ...(typeof maxTokens === "number" && Number.isFinite(maxTokens) && maxTokens > 02272- ? { maxTokens }2273- : {}),2274- ...(typeof accumulatedTotalProcessedTokens === "number" &&2275- Number.isFinite(accumulatedTotalProcessedTokens) &&2276- accumulatedTotalProcessedTokens > lastGoodUsage.usedTokens2277- ? {2278- totalProcessedTokens: accumulatedTotalProcessedTokens,2279- }2280- : {}),2281- }2282- : undefined);2262+ latestAssistantSnapshot ?? updatedLastGood ?? resultIterationSnapshot;
Mina-Sayed(GitHub profile, opens in a new tab) changed the title
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/provider/Layers/ClaudeAdapter.usageRegression.test.ts line 308Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +296,21 @@296+ cache_creation_input_tokens: 0,297+ cache_read_input_tokens: 10000,298+ output_tokens: 5000,299+ total_tokens: 215000,300+ iterations: [],301+ },302+ modelUsage: {303+ "claude-opus-4-6": { contextWindow: 1000000, maxOutputTokens: 64000 },304+ },305+ } as unknown as SDKMessage);306+ harness.query.finish();307+308+ const events = Array.from(yield* Fiber.join(secondTurnEvents));309+ const finalUsage = events.findLast(310+ (event) => event.type === "thread.token-usage.updated",311+ );312+ assert.equal(finalUsage?.type, "thread.token-usage.updated");313+ if (finalUsage?.type === "thread.token-usage.updated") {314+ assert.equal(finalUsage.payload.usage.usedTokens, 215000);315+ assert.equal(finalUsage.payload.usage.inputTokens, 210000);316+ assert.equal(finalUsage.payload.usage.outputTokens, 5000);
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge closed this
Sign in to join this conversation.


Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
60e906a1-80b1-4702-8418-cf519b3a9027📥 Commits
Reviewing files that changed from the base of the PR and between de5fe65ffb38bccc0d95c4d04e94021eaf0f70ca and 4c20ae902b04bf66b218f0fa619691d9a1f95d45.
📒 Files selected for processing (1)
apps/server/src/provider/Layers/ClaudeAdapter.tsIncluded review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.
📝 Walkthrough
Walkthrough#
Claude turn completion now preserves valid per-request
message_deltausage and clears the turn’s compaction marker. Regression tests cover cumulativeresultusage and current usage on later turns.Changes#
Claude token usage correction
apps/server/src/provider/Layers/ClaudeAdapter.tsmessage_deltausage as the latest assistant usage and clear the compaction marker.apps/server/src/provider/Layers/ClaudeAdapter.usageRegression.test.tsPriority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
juliusmarmingeMerge Risk: ⚪ Minimal · up to
4c20aThe Claude usage correction and post-compaction fallback are covered, with no remaining actionable merge risk identified.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#8594. The production change stores valid parent-sessionmessage_deltausage in the current turn’slatestAssistantUsage, so the end-of-turn fal…#8594. The four production lines directly support the parent-session context-meter fix. The added fake-query harness and regression tests verify the required fallba…✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.