Skip to content

Commit 20841cd

Browse files
fix(server): report Claude settings compaction threshold (pingdotgg#29220)
Co-authored-by: maria-rcks <[email protected]>
1 parent 09647d1 commit 20841cd

2 files changed

Lines changed: 268 additions & 16 deletions

File tree

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

Lines changed: 193 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7952,12 +7952,116 @@ describe("ClaudeAdapterV2 background wake turns", () => {
79527952
{ autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
79537953
{ autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined },
79547954
{ autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 617_000 },
7955+
{
7956+
autoCompactWindow: "",
7957+
model: "claude-opus-5-5",
7958+
userSettings: '{"autoCompactWindow":500000}',
7959+
threshold: 467_000,
7960+
},
7961+
{
7962+
autoCompactWindow: "300000",
7963+
model: "claude-opus-5-5",
7964+
userSettings: '{"autoCompactWindow":500000}',
7965+
threshold: 267_000,
7966+
},
7967+
{
7968+
autoCompactWindow: "",
7969+
model: "claude-opus-5-5",
7970+
userSettings: '{"autoCompactWindow":500000}',
7971+
projectSettings: '{"autoCompactWindow":400000}',
7972+
threshold: 367_000,
7973+
},
7974+
{
7975+
autoCompactWindow: "",
7976+
model: "claude-opus-5-5",
7977+
userSettings:
7978+
'{"autoCompactWindow":500000,"modelSettings":{"claude-opus-5-5":{"autoCompactWindow":450000}}}',
7979+
threshold: 417_000,
7980+
},
7981+
{
7982+
autoCompactWindow: "",
7983+
model: "claude-opus-5-5",
7984+
userSettings: '{"autoCompactWindow":500000}',
7985+
projectSettings: '{"modelSettings":{"claude-opus-5-5":{"autoCompactWindow":"auto"}}}',
7986+
threshold: undefined,
7987+
},
7988+
{
7989+
autoCompactWindow: "",
7990+
model: "claude-opus-5-5",
7991+
userSettings: '{"autoCompactWindow":500000}',
7992+
launchArgs: `--settings '{"autoCompactWindow":350000}'`,
7993+
threshold: 317_000,
7994+
},
7995+
{
7996+
autoCompactWindow: "",
7997+
model: "claude-opus-5-5",
7998+
userSettings: '{"autoCompactWindow":"invalid"}',
7999+
threshold: undefined,
8000+
},
8001+
{
8002+
autoCompactWindow: "",
8003+
model: "claude-opus-5-5",
8004+
userSettings: '{"autoCompactWindow":500000,}',
8005+
threshold: undefined,
8006+
},
8007+
{
8008+
autoCompactWindow: "",
8009+
model: "claude-opus-5-5",
8010+
userSettings: '{/* invalid */ "autoCompactWindow":500000}',
8011+
threshold: undefined,
8012+
},
8013+
{
8014+
autoCompactWindow: "",
8015+
model: "claude-opus-5-5",
8016+
userSettings: '{"autoCompactWindow":500000,"modelSettings":null}',
8017+
threshold: 467_000,
8018+
},
8019+
{
8020+
autoCompactWindow: "",
8021+
model: "claude-opus-5-5",
8022+
userSettings: '{"autoCompactWindow":500000}',
8023+
projectSettings: '{"autoCompactWindow":"auto"}',
8024+
threshold: 467_000,
8025+
},
8026+
{
8027+
autoCompactWindow: "",
8028+
model: "claude-opus-5-5",
8029+
userSettings: '{"autoCompactWindow":500000}',
8030+
projectSettings: '{"autoCompactWindow":50000}',
8031+
threshold: 467_000,
8032+
},
8033+
{
8034+
autoCompactWindow: "",
8035+
model: "claude-opus-5-5",
8036+
userSettings:
8037+
'{"autoCompactWindow":500000,"modelSettings":{"claude-opus-5-5":{"autoCompactWindow":50000}}}',
8038+
threshold: 467_000,
8039+
},
79558040
])(
79568041
"ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
7957-
({ autoCompactWindow, model, threshold }) =>
8042+
({ autoCompactWindow, model, threshold, userSettings, projectSettings, launchArgs }) =>
79588043
Effect.gen(function* () {
8044+
const fileSystem = yield* FileSystem.FileSystem;
8045+
const path = yield* Path.Path;
8046+
const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-compact-" });
8047+
const project = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-compact-" });
8048+
if (userSettings !== undefined) {
8049+
yield* fileSystem.writeFileString(path.join(home, "settings.json"), userSettings);
8050+
}
8051+
if (projectSettings !== undefined) {
8052+
yield* fileSystem.makeDirectory(path.join(project, ".claude"));
8053+
yield* fileSystem.writeFileString(
8054+
path.join(project, ".claude", "settings.json"),
8055+
projectSettings,
8056+
);
8057+
}
79598058
const harness = yield* makeWakeHarnessWithOptions({
7960-
settings: { ...DEFAULT_CLAUDE_SETTINGS, autoCompactWindow },
8059+
settings: {
8060+
...DEFAULT_CLAUDE_SETTINGS,
8061+
autoCompactWindow,
8062+
homePath: home,
8063+
launchArgs: launchArgs ?? "",
8064+
},
79618065
});
79628066
yield* harness.runtime.startTurn(
79638067
makeClaudeTestTurnInput({
@@ -7968,6 +8072,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
79688072
text: "Continue.",
79698073
attachments: [],
79708074
modelSelection: { ...CLAUDE_TEST_MODEL_SELECTION, model },
8075+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
8076+
...CLAUDE_TEST_RUNTIME_POLICY,
8077+
cwd: project,
8078+
}),
79718079
}),
79728080
);
79738081
const usage = { ...zeroUsage, input_tokens: 1_000, output_tokens: 75 };
@@ -8013,6 +8121,89 @@ describe("ClaudeAdapterV2 background wake turns", () => {
80138121
),
80148122
);
80158123

8124+
it.effect("reports the settings directory of a reused Claude query", () =>
8125+
Effect.gen(function* () {
8126+
const fileSystem = yield* FileSystem.FileSystem;
8127+
const path = yield* Path.Path;
8128+
const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-home-" });
8129+
const first = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-first-" });
8130+
const second = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-second-" });
8131+
for (const [directory, window] of [
8132+
[first, 150_000],
8133+
[second, 180_000],
8134+
] as const) {
8135+
yield* fileSystem.makeDirectory(path.join(directory, ".claude"));
8136+
yield* fileSystem.writeFileString(
8137+
path.join(directory, ".claude", "settings.json"),
8138+
JSON.stringify({ autoCompactWindow: window }),
8139+
);
8140+
}
8141+
const harness = yield* makeWakeHarnessWithOptions({
8142+
settings: { ...DEFAULT_CLAUDE_SETTINGS, homePath: home },
8143+
});
8144+
let firstOptions: ReturnType<typeof harness.getOpenedOptions>;
8145+
for (const [ordinal, cwd] of [
8146+
[1, first],
8147+
[2, second],
8148+
] as const) {
8149+
yield* harness.runtime.startTurn(
8150+
makeClaudeTestTurnInput({
8151+
threadId: harness.threadId,
8152+
providerThread: harness.providerThread,
8153+
now: yield* DateTime.now,
8154+
attemptId: RunAttemptId.make(`attempt-compact-cwd-${ordinal}`),
8155+
providerTurnOrdinal: ordinal,
8156+
text: "Continue.",
8157+
attachments: [],
8158+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
8159+
...CLAUDE_TEST_RUNTIME_POLICY,
8160+
cwd,
8161+
}),
8162+
}),
8163+
);
8164+
if (ordinal === 1) firstOptions = harness.getOpenedOptions();
8165+
else assert.strictEqual(harness.getOpenedOptions(), firstOptions);
8166+
yield* Queue.offer(
8167+
harness.sdkMessages,
8168+
usageSnapshotFrame(
8169+
`msg_cwd_${ordinal}`,
8170+
`00000000-0000-4000-8000-00000000080${ordinal}`,
8171+
{
8172+
...zeroUsage,
8173+
input_tokens: 1_000,
8174+
output_tokens: 10,
8175+
},
8176+
),
8177+
);
8178+
yield* Queue.offer(
8179+
harness.sdkMessages,
8180+
makeResultFrame({
8181+
uuid: `00000000-0000-4000-8000-00000000081${ordinal}`,
8182+
result: "Done.",
8183+
}),
8184+
);
8185+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
8186+
}
8187+
assert.deepEqual(
8188+
harness.events.flatMap((event) =>
8189+
event.type === "provider_turn.updated" && event.providerTurn.tokenUsage
8190+
? [event.providerTurn.tokenUsage.autoCompactThreshold]
8191+
: [],
8192+
),
8193+
[117_000, 117_000],
8194+
);
8195+
}).pipe(
8196+
Effect.provide(
8197+
Layer.mergeAll(
8198+
IdAllocator.layer,
8199+
McpProviderSessions.layer,
8200+
NodeServices.layer,
8201+
NodeCrypto.layer,
8202+
),
8203+
),
8204+
),
8205+
);
8206+
80168207
it.effect("shows a hook-dropped Claude prompt once and skips info-level lines", () =>
80178208
Effect.gen(function* () {
80188209
const harness = yield* makeWakeHarness;

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

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,6 +2973,15 @@ const decodeClaudeSubagentCacheSettings = Schema.decodeUnknownEffect(
29732973
),
29742974
),
29752975
);
2976+
const decodeClaudeCompactSettings = Schema.decodeUnknownEffect(
2977+
Schema.fromJsonString(
2978+
Schema.Struct({
2979+
autoCompactWindow: Schema.optionalKey(Schema.Unknown),
2980+
modelSettings: Schema.optionalKey(Schema.Unknown),
2981+
}),
2982+
),
2983+
);
2984+
const decodeClaudeCompactWindow = Schema.decodeUnknownOption(Schema.Int);
29762985
const decodeClaudeEnvSettings = Schema.decodeUnknownEffect(
29772986
fromLenientJson(
29782987
Schema.StructWithRest(
@@ -4317,6 +4326,7 @@ function formatClaudeUsageLimitWait(waitMs: number): string {
43174326
interface ActiveClaudeTurnContext {
43184327
readonly queryPolicy: ClaudeRuntimeQueryPolicy;
43194328
readonly input: ProviderAdapter.ProviderAdapterV2TurnInput;
4329+
readonly autoCompactWindow: string;
43204330
readonly nativeThreadId: string;
43214331
readonly nativeTurnId: string;
43224332
nativeMessageCursor: string | null;
@@ -4415,6 +4425,8 @@ interface ActiveClaudeSubagent {
44154425
interface ClaudeLiveQueryContext {
44164426
readonly queryPolicy: ClaudeRuntimeQueryPolicy;
44174427
readonly nativeThreadId: string;
4428+
readonly settingsCwd: string | null;
4429+
readonly launchArgSettings: ClaudeSdkSettings | undefined;
44184430
readonly query: ClaudeAgentSdkQuerySession;
44194431
readonly queryPolicyKey: string;
44204432
selection: CompiledClaudeModelSelection;
@@ -4991,15 +5003,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
49915003
if (path.dirname(directory) === directory) return cwd;
49925004
}
49935005
});
4994-
// Settings env beats the process env, --settings over local over project over user, and a local
4995-
// file an older Claude Code left in cwd applies under the root one. resolveSettings reads local
4996-
// from cwd only.
4997-
const resolveToolSearchOverride = Effect.fn("ClaudeAdapterV2.resolveToolSearchOverride")(
4998-
function* (cwd: string | null, launchArgSettings: ClaudeSdkSettings | undefined) {
4999-
const launchValue = launchArgSettings?.env?.ENABLE_TOOL_SEARCH;
5000-
if (typeof launchValue === "string") return claudeToolSearchOverride(launchValue);
5006+
const settingsFilesByPrecedence = Effect.fn("ClaudeAdapterV2.settingsFilesByPrecedence")(
5007+
function* (cwd: string | null) {
50015008
const localDirectory = cwd === null ? null : yield* localSettingsDirectory(cwd);
5002-
const settingsFiles = [
5009+
return [
50035010
...(cwd === null || localDirectory === null
50045011
? []
50055012
: [
@@ -5009,14 +5016,63 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
50095016
]),
50105017
path.join(homePath, "settings.json"),
50115018
];
5012-
for (const settingsFile of settingsFiles) {
5019+
},
5020+
);
5021+
// Settings env beats the process env, --settings over local over project over user, and a local
5022+
// file an older Claude Code left in cwd applies under the root one. resolveSettings reads local
5023+
// from cwd only.
5024+
const resolveToolSearchOverride = Effect.fn("ClaudeAdapterV2.resolveToolSearchOverride")(
5025+
function* (cwd: string | null, launchArgSettings: ClaudeSdkSettings | undefined) {
5026+
const launchValue = launchArgSettings?.env?.ENABLE_TOOL_SEARCH;
5027+
if (typeof launchValue === "string") return claudeToolSearchOverride(launchValue);
5028+
for (const settingsFile of yield* settingsFilesByPrecedence(cwd)) {
50135029
const value = yield* readToolSearchSetting(settingsFile);
50145030
if (value !== undefined) return claudeToolSearchOverride(value);
50155031
}
50165032
return claudeToolSearchOverride(adapterOptions.environment.ENABLE_TOOL_SEARCH);
50175033
},
50185034
);
50195035

5036+
const resolveAutoCompactWindow = Effect.fn("ClaudeAdapterV2.resolveAutoCompactWindow")(function* (
5037+
cwd: string | null,
5038+
modelSelection: ModelSelection,
5039+
launchArgSettings: ClaudeSdkSettings | undefined,
5040+
) {
5041+
if (adapterOptions.settings.autoCompactWindow !== "")
5042+
return adapterOptions.settings.autoCompactWindow;
5043+
const model = compileClaudeModelSelection(modelSelection).apiModelId.replace(/\[1m\]$/i, "");
5044+
const windowFrom = (settings: unknown) => {
5045+
if (typeof settings !== "object" || settings === null) return undefined;
5046+
const models = Reflect.get(settings, "modelSettings");
5047+
const modelSetting =
5048+
typeof models === "object" && models !== null ? Reflect.get(models, model) : undefined;
5049+
const modelWindow =
5050+
typeof modelSetting === "object" && modelSetting !== null
5051+
? Reflect.get(modelSetting, "autoCompactWindow")
5052+
: undefined;
5053+
const validWindow = (value: unknown) => {
5054+
const selected = Option.getOrUndefined(decodeClaudeCompactWindow(value));
5055+
return typeof selected === "number" && selected >= 100_000 && selected <= 1_000_000
5056+
? String(selected)
5057+
: undefined;
5058+
};
5059+
return modelWindow === "auto"
5060+
? ""
5061+
: (validWindow(modelWindow) ?? validWindow(Reflect.get(settings, "autoCompactWindow")));
5062+
};
5063+
const launchWindow = windowFrom(launchArgSettings);
5064+
if (launchWindow !== undefined) return launchWindow;
5065+
for (const settingsFile of yield* settingsFilesByPrecedence(cwd)) {
5066+
const settings = yield* fileSystem.readFileString(settingsFile).pipe(
5067+
Effect.flatMap(decodeClaudeCompactSettings),
5068+
Effect.orElseSucceed(() => undefined),
5069+
);
5070+
const value = windowFrom(settings);
5071+
if (value !== undefined) return value;
5072+
}
5073+
return "";
5074+
});
5075+
50205076
const userInvocableSkills = (cwd: string | null) =>
50215077
discoverClaudeSkills(
50225078
adapterOptions.settings,
@@ -5405,17 +5461,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
54055461
? {
54065462
usedTokens: usage,
54075463
maxTokens,
5408-
...claudeAutoCompactThreshold(
5409-
adapterOptions.settings.autoCompactWindow,
5410-
maxTokens,
5411-
),
5464+
...claudeAutoCompactThreshold(context.autoCompactWindow, maxTokens),
54125465
updatedAt: DateTime.formatIso(now),
54135466
}
54145467
: claudeProviderTurnTokenUsage(
54155468
usage,
54165469
maxTokens,
54175470
DateTime.formatIso(now),
5418-
adapterOptions.settings.autoCompactWindow,
5471+
context.autoCompactWindow,
54195472
context.streamUsage?.requestedAt,
54205473
);
54215474
if (typeof usage !== "number") {
@@ -10939,6 +10992,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1093910992
const context: ClaudeLiveQueryContext = {
1094010993
queryPolicy,
1094110994
nativeThreadId,
10995+
settingsCwd: turnInput.runtimePolicy.cwd,
10996+
launchArgSettings,
1094210997
query: querySession,
1094310998
queryPolicyKey,
1094410999
selection: compiledSelection,
@@ -11126,8 +11181,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1112611181
Effect.provideService(Path.Path, path),
1112711182
);
1112811183
const querySession = yield* openQuery(turnInput, nativeThreadId);
11184+
const autoCompactWindow = yield* resolveAutoCompactWindow(
11185+
querySession.settingsCwd,
11186+
turnInput.modelSelection,
11187+
querySession.launchArgSettings,
11188+
);
1112911189
const context: ActiveClaudeTurnContext = {
1113011190
input: turnInput,
11191+
autoCompactWindow,
1113111192
queryPolicy: querySession.queryPolicy,
1113211193
nativeThreadId,
1113311194
nativeTurnId,

0 commit comments

Comments
 (0)