The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(claude): context meter shows the configured auto-compact threshold (#18332)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Akbar Danial Akma committed
Oct 20, 2026 at 11:55 UTC
15c927899db272365623aa06b4635606156bf8e2
7 files changed
+109
-47
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+70
-41
@@ -4578,7 +4578,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
4578
4579
it.effect("merges cumulative stream usage into the message_start counts", () =>
4580
Effect.gen(function* () {
4581
- const harness = yield* makeWakeHarness;
4581
+ const harness = yield* makeWakeHarnessWithOptions({
4582
+ settings: { ...DEFAULT_CLAUDE_SETTINGS, autoCompactWindow: "150000" },
4583
+ });
4584
yield* harness.runtime.startTurn(
4585
makeClaudeTestTurnInput({
4586
threadId: harness.threadId,
@@ -4639,6 +4641,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
4641
const terminal = yield* Queue.take(harness.terminalReceipts);
4642
assert.equal(terminal.status, "completed");
4643
assert.deepEqual(reportedContextTokens(harness.events), [1_211, 1_230, 1_075, 400]);
4644
+ assert.deepEqual(
4645
+ harness.events.flatMap((event) =>
4646
+ event.type === "provider_turn.updated" && event.providerTurn.tokenUsage
4647
+ ? [event.providerTurn.tokenUsage.autoCompactThreshold]
4648
+ : [],
4649
+ ),
4650
+ [150_000, 150_000, 150_000, 150_000],
4651
+ );
4652
}).pipe(
4653
Effect.provide(
4654
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -4646,47 +4656,66 @@ describe("ClaudeAdapterV2 background wake turns", () => {
4656
),
4657
);
4658
4649
- it.effect("ignores snapshot usage from before a compaction without stream usage", () =>
4650
- Effect.gen(function* () {
4651
- const harness = yield* makeWakeHarness;
4652
- yield* harness.runtime.startTurn(
4653
- makeClaudeTestTurnInput({
4654
- threadId: harness.threadId,
4655
- providerThread: harness.providerThread,
4656
- now: yield* DateTime.now,
4657
- attemptId: RunAttemptId.make("attempt-snapshot-usage-compaction"),
4658
- text: "Continue.",
4659
- attachments: [],
4660
- }),
4661
- );
4662
- const usage = { ...zeroUsage, input_tokens: 1_000, output_tokens: 75 };
4663
- const frames = [
4664
- usageSnapshotFrame("msg_fallback", "00000000-0000-4000-8000-000000000790", usage),
4665
- claudeSdkFrame({
4666
- type: "system",
4667
- subtype: "compact_boundary",
4668
- compact_metadata: { trigger: "auto", pre_tokens: 1_075, post_tokens: 400 },
4669
- uuid: "00000000-0000-4000-8000-000000000791",
4670
- session_id: WAKE_NATIVE_SESSION,
4671
- }),
4672
- usageSnapshotFrame("msg_fallback", "00000000-0000-4000-8000-000000000792", usage),
4673
- // A new request after the boundary reports the compacted context.
4674
- usageSnapshotFrame("msg_after", "00000000-0000-4000-8000-000000000793", {
4675
- ...zeroUsage,
4676
- input_tokens: 450,
4677
- output_tokens: 10,
4678
- }),
4679
- makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000794", result: "Done." }),
4680
- ];
4681
- for (const frame of frames) yield* Queue.offer(harness.sdkMessages, frame);
4682
- const terminal = yield* Queue.take(harness.terminalReceipts);
4683
- assert.equal(terminal.status, "completed");
4684
- assert.deepEqual(reportedContextTokens(harness.events), [1_075, 400, 460]);
4685
- }).pipe(
4686
- Effect.provide(
4687
- Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
4659
+ it.effect.each([
4660
+ { autoCompactWindow: "", model: "claude-sonnet-4-6", threshold: undefined },
4661
+ { autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 150_000 },
4662
+ { autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
4663
+ { autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined },
4664
+ { autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 650_000 },
4665
+ ])(
4666
+ "ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
4667
+ ({ autoCompactWindow, model, threshold }) =>
4668
+ Effect.gen(function* () {
4669
+ const harness = yield* makeWakeHarnessWithOptions({
4670
+ settings: { ...DEFAULT_CLAUDE_SETTINGS, autoCompactWindow },
4671
+ });
4672
+ yield* harness.runtime.startTurn(
4673
+ makeClaudeTestTurnInput({
4674
+ threadId: harness.threadId,
4675
+ providerThread: harness.providerThread,
4676
+ now: yield* DateTime.now,
4677
+ attemptId: RunAttemptId.make("attempt-snapshot-usage-compaction"),
4678
+ text: "Continue.",
4679
+ attachments: [],
4680
+ modelSelection: { ...CLAUDE_TEST_MODEL_SELECTION, model },
4681
+ }),
4682
+ );
4683
+ const usage = { ...zeroUsage, input_tokens: 1_000, output_tokens: 75 };
4684
+ const frames = [
4685
+ usageSnapshotFrame("msg_fallback", "00000000-0000-4000-8000-000000000790", usage),
4686
+ claudeSdkFrame({
4687
+ type: "system",
4688
+ subtype: "compact_boundary",
4689
+ compact_metadata: { trigger: "auto", pre_tokens: 1_075, post_tokens: 400 },
4690
+ uuid: "00000000-0000-4000-8000-000000000791",
4691
+ session_id: WAKE_NATIVE_SESSION,
4692
+ }),
4693
+ usageSnapshotFrame("msg_fallback", "00000000-0000-4000-8000-000000000792", usage),
4694
+ // A new request after the boundary reports the compacted context.
4695
+ usageSnapshotFrame("msg_after", "00000000-0000-4000-8000-000000000793", {
4696
+ ...zeroUsage,
4697
+ input_tokens: 450,
4698
+ output_tokens: 10,
4699
+ }),
4700
+ makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000794", result: "Done." }),
4701
+ ];
4702
+ for (const frame of frames) yield* Queue.offer(harness.sdkMessages, frame);
4703
+ const terminal = yield* Queue.take(harness.terminalReceipts);
4704
+ assert.equal(terminal.status, "completed");
4705
+ assert.deepEqual(reportedContextTokens(harness.events), [1_075, 400, 460]);
4706
+ assert.deepEqual(
4707
+ harness.events.flatMap((event) =>
4708
+ event.type === "provider_turn.updated" && event.providerTurn.tokenUsage
4709
+ ? [event.providerTurn.tokenUsage.autoCompactThreshold]
4710
+ : [],
4711
+ ),
4712
+ [threshold, threshold, threshold],
4713
+ );
4714
+ }).pipe(
4715
+ Effect.provide(
4716
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
4717
+ ),
4718
),
4689
- ),
4719
);
4720
4721
it.effect("surfaces a Claude safety model fallback without failing the turn", () =>
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+19
-2
@@ -205,6 +205,14 @@ function claudePromptCacheTtlMs(
205
return undefined;
206
}
207
208
+// Claude caps the window at the model's context, so a setting at or above it never applies.
209
+function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) {
210
+ const threshold = Number(autoCompactWindow);
211
+ return threshold > 0 && (maxTokens === null || threshold < maxTokens)
212
+ ? { autoCompactThreshold: threshold }
213
+ : {};
214
+}
215
+
216
function claudeMessageInputTokens(usage: {
217
readonly input_tokens: number;
218
readonly cache_creation_input_tokens?: number | null;
@@ -227,18 +235,21 @@ export function claudeProviderTurnTokenUsage(
235
},
236
modelSelection: ModelSelection,
237
updatedAt: string,
238
+ autoCompactWindow = "",
239
) {
240
const inputTokens = claudeMessageInputTokens(usage);
241
const outputTokens = usage.output_tokens;
242
const promptCacheTtlMs = claudePromptCacheTtlMs(usage.cache_creation);
243
+ const maxTokens = claudeContextWindow(modelSelection);
244
return {
245
usedTokens: inputTokens + outputTokens,
236
- maxTokens: claudeContextWindow(modelSelection),
246
+ maxTokens,
247
inputTokens,
248
cachedInputTokens: usage.cache_read_input_tokens ?? 0,
249
outputTokens,
250
reasoningOutputTokens: 0,
251
...(promptCacheTtlMs === undefined ? {} : { promptCacheTtlMs }),
252
+ ...claudeAutoCompactThreshold(autoCompactWindow, maxTokens),
253
updatedAt,
254
};
255
}
@@ -3963,6 +3974,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
3974
usage,
3975
context.input.modelSelection,
3976
DateTime.formatIso(now),
3977
+ adapterOptions.settings.autoCompactWindow,
3978
);
3979
if (tokenUsage.inputTokens <= 0) return;
3980
yield* emitProviderEvent({
@@ -7439,6 +7451,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7451
? Math.round(postTokens)
7452
: undefined;
7453
if (afterTokenCount !== undefined) {
7454
+ const maxTokens = claudeContextWindow(context.input.modelSelection);
7455
yield* emitProviderEvent({
7456
type: "provider_turn.updated",
7457
driver: CLAUDE_PROVIDER,
@@ -7459,7 +7472,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7472
completedAt: null,
7473
tokenUsage: {
7474
usedTokens: afterTokenCount,
7462
- maxTokens: claudeContextWindow(context.input.modelSelection),
7475
+ maxTokens,
7476
+ ...claudeAutoCompactThreshold(
7477
+ adapterOptions.settings.autoCompactWindow,
7478
+ maxTokens,
7479
+ ),
7480
updatedAt: DateTime.formatIso(now),
7481
},
7482
},
apps/web/src/components/chat/ContextWindowMeter.logic.test.ts
+1
-1
@@ -135,7 +135,7 @@ describe("formatContextWindowCompactionMessage", () => {
135
136
it("shows the configured auto-compaction threshold", () => {
137
expect(formatContextWindowCompactionMessage("Claude Sonnet 5", 300_000)).toBe(
138
- "Compacts automatically at 300,000 tokens.",
138
+ "Compacts automatically at about 300,000 tokens.",
139
);
140
});
141
});
apps/web/src/components/chat/ContextWindowMeter.logic.ts
+1
-1
@@ -104,7 +104,7 @@ export function formatContextWindowCompactionMessage(
104
autoCompactThreshold?: number | null,
105
): string {
106
if (typeof autoCompactThreshold === "number" && autoCompactThreshold > 0) {
107
- return `Compacts automatically at ${autoCompactThreshold.toLocaleString("en-US")} tokens.`;
107
+ return `Compacts automatically at about ${autoCompactThreshold.toLocaleString("en-US")} tokens.`;
108
}
109
return modelDisplayName
110
? `Context for ${modelDisplayName} compacts automatically when needed.`
apps/web/src/lib/contextWindow.test.ts
+16
-1
@@ -1,4 +1,7 @@
1
import { describe, expect, it } from "vite-plus/test";
2
+import { OrchestrationV2ProviderTurnTokenUsage } from "@t3tools/contracts";
3
+import * as Schema from "effect/Schema";
4
+import { formatContextWindowCompactionMessage } from "../components/chat/ContextWindowMeter.logic";
5
import * as DateTime from "effect/DateTime";
6
import {
7
ContextTransferId,
@@ -15,6 +18,8 @@ import {
18
latestProviderTurnTokenUsage,
19
} from "./contextWindow";
20
21
+const decodeTokenUsage = Schema.decodeSync(OrchestrationV2ProviderTurnTokenUsage);
22
+
23
describe("V2 context window presentation", () => {
24
it("uses retained compaction token data when available", () => {
25
const snapshot = deriveLatestContextWindowSnapshot([
@@ -81,18 +86,24 @@ describe("V2 context window presentation", () => {
86
87
describe("live provider-turn usage (#8144)", () => {
88
it("prefers the provider's live report over compaction items", () => {
84
- const snapshot = deriveLatestContextWindowSnapshot([], {
89
+ const usage = decodeTokenUsage({
90
usedTokens: 42_000,
91
maxTokens: 200_000,
92
inputTokens: 40_000,
93
outputTokens: 2_000,
94
+ autoCompactThreshold: 150_000,
95
updatedAt: "2026-08-27T00:00:00.000Z",
96
});
97
+ const snapshot = deriveLatestContextWindowSnapshot([], usage);
98
expect(snapshot).not.toBeNull();
99
expect(snapshot?.usedTokens).toBe(42_000);
100
expect(snapshot?.maxTokens).toBe(200_000);
101
expect(snapshot?.remainingTokens).toBe(158_000);
102
expect(snapshot?.usedPercentage).toBe(21);
103
+ expect(snapshot?.autoCompactThreshold).toBe(150_000);
104
+ expect(
105
+ formatContextWindowCompactionMessage("Claude Sonnet 4.6", snapshot?.autoCompactThreshold),
106
+ ).toBe("Compacts automatically at about 150,000 tokens.");
107
});
108
109
it("handles a report without a known context window", () => {
@@ -102,6 +113,10 @@ describe("live provider-turn usage (#8144)", () => {
113
});
114
expect(snapshot?.maxTokens).toBeNull();
115
expect(snapshot?.usedPercentage).toBeNull();
116
+ expect(snapshot?.autoCompactThreshold).toBeNull();
117
+ expect(formatContextWindowCompactionMessage(null, snapshot?.autoCompactThreshold)).toBe(
118
+ "Context compacts automatically when needed.",
119
+ );
120
});
121
});
122
apps/web/src/lib/contextWindow.ts
+1
-1
@@ -90,7 +90,7 @@ export function deriveLatestContextWindowSnapshot(
90
toolUses: null,
91
durationMs: null,
92
compactsAutomatically: true,
93
- autoCompactThreshold: null,
93
+ autoCompactThreshold: liveUsage.autoCompactThreshold ?? null,
94
cost: null,
95
updatedAt: liveUsage.updatedAt,
96
};
packages/contracts/src/orchestrationV2.ts
+1
@@ -171,6 +171,7 @@ export const OrchestrationV2ProviderTurnTokenUsage = Schema.Struct({
171
outputTokens: Schema.optional(NonNegativeInt),
172
reasoningOutputTokens: Schema.optional(NonNegativeInt),
173
promptCacheTtlMs: Schema.optional(PositiveInt),
174
+ autoCompactThreshold: Schema.optional(PositiveInt),
175
updatedAt: Schema.String,
176
});
177
export type OrchestrationV2ProviderTurnTokenUsage =