Skip to content

Commit 15c9278

Browse files
fix(claude): context meter shows the configured auto-compact threshold (pingdotgg#18332)
Co-authored-by: Julius Marminge <[email protected]>
1 parent f0dac29 commit 15c9278

7 files changed

Lines changed: 109 additions & 47 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4578,7 +4578,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
45784578

45794579
it.effect("merges cumulative stream usage into the message_start counts", () =>
45804580
Effect.gen(function* () {
4581-
const harness = yield* makeWakeHarness;
4581+
const harness = yield* makeWakeHarnessWithOptions({
4582+
settings: { ...DEFAULT_CLAUDE_SETTINGS, autoCompactWindow: "150000" },
4583+
});
45824584
yield* harness.runtime.startTurn(
45834585
makeClaudeTestTurnInput({
45844586
threadId: harness.threadId,
@@ -4639,54 +4641,81 @@ describe("ClaudeAdapterV2 background wake turns", () => {
46394641
const terminal = yield* Queue.take(harness.terminalReceipts);
46404642
assert.equal(terminal.status, "completed");
46414643
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+
);
46424652
}).pipe(
46434653
Effect.provide(
46444654
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
46454655
),
46464656
),
46474657
);
46484658

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+
),
46884718
),
4689-
),
46904719
);
46914720

46924721
it.effect("surfaces a Claude safety model fallback without failing the turn", () =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ function claudePromptCacheTtlMs(
205205
return undefined;
206206
}
207207

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+
208216
function claudeMessageInputTokens(usage: {
209217
readonly input_tokens: number;
210218
readonly cache_creation_input_tokens?: number | null;
@@ -227,18 +235,21 @@ export function claudeProviderTurnTokenUsage(
227235
},
228236
modelSelection: ModelSelection,
229237
updatedAt: string,
238+
autoCompactWindow = "",
230239
) {
231240
const inputTokens = claudeMessageInputTokens(usage);
232241
const outputTokens = usage.output_tokens;
233242
const promptCacheTtlMs = claudePromptCacheTtlMs(usage.cache_creation);
243+
const maxTokens = claudeContextWindow(modelSelection);
234244
return {
235245
usedTokens: inputTokens + outputTokens,
236-
maxTokens: claudeContextWindow(modelSelection),
246+
maxTokens,
237247
inputTokens,
238248
cachedInputTokens: usage.cache_read_input_tokens ?? 0,
239249
outputTokens,
240250
reasoningOutputTokens: 0,
241251
...(promptCacheTtlMs === undefined ? {} : { promptCacheTtlMs }),
252+
...claudeAutoCompactThreshold(autoCompactWindow, maxTokens),
242253
updatedAt,
243254
};
244255
}
@@ -3963,6 +3974,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
39633974
usage,
39643975
context.input.modelSelection,
39653976
DateTime.formatIso(now),
3977+
adapterOptions.settings.autoCompactWindow,
39663978
);
39673979
if (tokenUsage.inputTokens <= 0) return;
39683980
yield* emitProviderEvent({
@@ -7439,6 +7451,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
74397451
? Math.round(postTokens)
74407452
: undefined;
74417453
if (afterTokenCount !== undefined) {
7454+
const maxTokens = claudeContextWindow(context.input.modelSelection);
74427455
yield* emitProviderEvent({
74437456
type: "provider_turn.updated",
74447457
driver: CLAUDE_PROVIDER,
@@ -7459,7 +7472,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
74597472
completedAt: null,
74607473
tokenUsage: {
74617474
usedTokens: afterTokenCount,
7462-
maxTokens: claudeContextWindow(context.input.modelSelection),
7475+
maxTokens,
7476+
...claudeAutoCompactThreshold(
7477+
adapterOptions.settings.autoCompactWindow,
7478+
maxTokens,
7479+
),
74637480
updatedAt: DateTime.formatIso(now),
74647481
},
74657482
},

‎apps/web/src/components/chat/ContextWindowMeter.logic.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe("formatContextWindowCompactionMessage", () => {
135135

136136
it("shows the configured auto-compaction threshold", () => {
137137
expect(formatContextWindowCompactionMessage("Claude Sonnet 5", 300_000)).toBe(
138-
"Compacts automatically at 300,000 tokens.",
138+
"Compacts automatically at about 300,000 tokens.",
139139
);
140140
});
141141
});

‎apps/web/src/components/chat/ContextWindowMeter.logic.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export function formatContextWindowCompactionMessage(
104104
autoCompactThreshold?: number | null,
105105
): string {
106106
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.`;
108108
}
109109
return modelDisplayName
110110
? `Context for ${modelDisplayName} compacts automatically when needed.`

‎apps/web/src/lib/contextWindow.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
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";
25
import * as DateTime from "effect/DateTime";
36
import {
47
ContextTransferId,
@@ -15,6 +18,8 @@ import {
1518
latestProviderTurnTokenUsage,
1619
} from "./contextWindow";
1720

21+
const decodeTokenUsage = Schema.decodeSync(OrchestrationV2ProviderTurnTokenUsage);
22+
1823
describe("V2 context window presentation", () => {
1924
it("uses retained compaction token data when available", () => {
2025
const snapshot = deriveLatestContextWindowSnapshot([
@@ -81,18 +86,24 @@ describe("V2 context window presentation", () => {
8186

8287
describe("live provider-turn usage (#8144)", () => {
8388
it("prefers the provider's live report over compaction items", () => {
84-
const snapshot = deriveLatestContextWindowSnapshot([], {
89+
const usage = decodeTokenUsage({
8590
usedTokens: 42_000,
8691
maxTokens: 200_000,
8792
inputTokens: 40_000,
8893
outputTokens: 2_000,
94+
autoCompactThreshold: 150_000,
8995
updatedAt: "2026-08-27T00:00:00.000Z",
9096
});
97+
const snapshot = deriveLatestContextWindowSnapshot([], usage);
9198
expect(snapshot).not.toBeNull();
9299
expect(snapshot?.usedTokens).toBe(42_000);
93100
expect(snapshot?.maxTokens).toBe(200_000);
94101
expect(snapshot?.remainingTokens).toBe(158_000);
95102
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.");
96107
});
97108

98109
it("handles a report without a known context window", () => {
@@ -102,6 +113,10 @@ describe("live provider-turn usage (#8144)", () => {
102113
});
103114
expect(snapshot?.maxTokens).toBeNull();
104115
expect(snapshot?.usedPercentage).toBeNull();
116+
expect(snapshot?.autoCompactThreshold).toBeNull();
117+
expect(formatContextWindowCompactionMessage(null, snapshot?.autoCompactThreshold)).toBe(
118+
"Context compacts automatically when needed.",
119+
);
105120
});
106121
});
107122

‎apps/web/src/lib/contextWindow.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function deriveLatestContextWindowSnapshot(
9090
toolUses: null,
9191
durationMs: null,
9292
compactsAutomatically: true,
93-
autoCompactThreshold: null,
93+
autoCompactThreshold: liveUsage.autoCompactThreshold ?? null,
9494
cost: null,
9595
updatedAt: liveUsage.updatedAt,
9696
};

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export const OrchestrationV2ProviderTurnTokenUsage = Schema.Struct({
171171
outputTokens: Schema.optional(NonNegativeInt),
172172
reasoningOutputTokens: Schema.optional(NonNegativeInt),
173173
promptCacheTtlMs: Schema.optional(PositiveInt),
174+
autoCompactThreshold: Schema.optional(PositiveInt),
174175
updatedAt: Schema.String,
175176
});
176177
export type OrchestrationV2ProviderTurnTokenUsage =

0 commit comments

Comments
 (0)