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, 03:15 UTC
fix(server): report Claude settings compaction threshold (#29220)
Co-authored-by: maria-rcks <maria@kuuro.net>
Lakshmi Tanmay committed
Nov 6, 2026 at 10:02 UTC
20841cdfb613a33a471841f8304c4d00980f39b6
2 files changed
+268
-16
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+193
-2
@@ -7952,12 +7952,116 @@ describe("ClaudeAdapterV2 background wake turns", () => {
7952
{ autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
7953
{ autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined },
7954
{ 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
+ },
8040
])(
8041
"ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
7957
- ({ autoCompactWindow, model, threshold }) =>
8042
+ ({ autoCompactWindow, model, threshold, userSettings, projectSettings, launchArgs }) =>
8043
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
+ }
8058
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
+ },
8065
});
8066
yield* harness.runtime.startTurn(
8067
makeClaudeTestTurnInput({
@@ -7968,6 +8072,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
8072
text: "Continue.",
8073
attachments: [],
8074
modelSelection: { ...CLAUDE_TEST_MODEL_SELECTION, model },
8075
+ runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
8076
+ ...CLAUDE_TEST_RUNTIME_POLICY,
8077
+ cwd: project,
8078
+ }),
8079
}),
8080
);
8081
const usage = { ...zeroUsage, input_tokens: 1_000, output_tokens: 75 };
@@ -8013,6 +8121,89 @@ describe("ClaudeAdapterV2 background wake turns", () => {
8121
),
8122
);
8123
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
+
8207
it.effect("shows a hook-dropped Claude prompt once and skips info-level lines", () =>
8208
Effect.gen(function* () {
8209
const harness = yield* makeWakeHarness;
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+75
-14
@@ -2973,6 +2973,15 @@ const decodeClaudeSubagentCacheSettings = Schema.decodeUnknownEffect(
2973
),
2974
),
2975
);
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);
2985
const decodeClaudeEnvSettings = Schema.decodeUnknownEffect(
2986
fromLenientJson(
2987
Schema.StructWithRest(
@@ -4317,6 +4326,7 @@ function formatClaudeUsageLimitWait(waitMs: number): string {
4326
interface ActiveClaudeTurnContext {
4327
readonly queryPolicy: ClaudeRuntimeQueryPolicy;
4328
readonly input: ProviderAdapter.ProviderAdapterV2TurnInput;
4329
+ readonly autoCompactWindow: string;
4330
readonly nativeThreadId: string;
4331
readonly nativeTurnId: string;
4332
nativeMessageCursor: string | null;
@@ -4415,6 +4425,8 @@ interface ActiveClaudeSubagent {
4425
interface ClaudeLiveQueryContext {
4426
readonly queryPolicy: ClaudeRuntimeQueryPolicy;
4427
readonly nativeThreadId: string;
4428
+ readonly settingsCwd: string | null;
4429
+ readonly launchArgSettings: ClaudeSdkSettings | undefined;
4430
readonly query: ClaudeAgentSdkQuerySession;
4431
readonly queryPolicyKey: string;
4432
selection: CompiledClaudeModelSelection;
@@ -4991,15 +5003,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5003
if (path.dirname(directory) === directory) return cwd;
5004
}
5005
});
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) {
5008
const localDirectory = cwd === null ? null : yield* localSettingsDirectory(cwd);
5002
- const settingsFiles = [
5009
+ return [
5010
...(cwd === null || localDirectory === null
5011
? []
5012
: [
@@ -5009,7 +5016,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5016
]),
5017
path.join(homePath, "settings.json"),
5018
];
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)) {
5029
const value = yield* readToolSearchSetting(settingsFile);
5030
if (value !== undefined) return claudeToolSearchOverride(value);
5031
}
@@ -5017,6 +5033,46 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5033
},
5034
);
5035
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
+
5076
const userInvocableSkills = (cwd: string | null) =>
5077
discoverClaudeSkills(
5078
adapterOptions.settings,
@@ -5405,17 +5461,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5461
? {
5462
usedTokens: usage,
5463
maxTokens,
5408
- ...claudeAutoCompactThreshold(
5409
- adapterOptions.settings.autoCompactWindow,
5410
- maxTokens,
5411
- ),
5464
+ ...claudeAutoCompactThreshold(context.autoCompactWindow, maxTokens),
5465
updatedAt: DateTime.formatIso(now),
5466
}
5467
: claudeProviderTurnTokenUsage(
5468
usage,
5469
maxTokens,
5470
DateTime.formatIso(now),
5418
- adapterOptions.settings.autoCompactWindow,
5471
+ context.autoCompactWindow,
5472
context.streamUsage?.requestedAt,
5473
);
5474
if (typeof usage !== "number") {
@@ -10939,6 +10992,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10992
const context: ClaudeLiveQueryContext = {
10993
queryPolicy,
10994
nativeThreadId,
10995
+ settingsCwd: turnInput.runtimePolicy.cwd,
10996
+ launchArgSettings,
10997
query: querySession,
10998
queryPolicyKey,
10999
selection: compiledSelection,
@@ -11126,8 +11181,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
11181
Effect.provideService(Path.Path, path),
11182
);
11183
const querySession = yield* openQuery(turnInput, nativeThreadId);
11184
+ const autoCompactWindow = yield* resolveAutoCompactWindow(
11185
+ querySession.settingsCwd,
11186
+ turnInput.modelSelection,
11187
+ querySession.launchArgSettings,
11188
+ );
11189
const context: ActiveClaudeTurnContext = {
11190
input: turnInput,
11191
+ autoCompactWindow,
11192
queryPolicy: querySession.queryPolicy,
11193
nativeThreadId,
11194
nativeTurnId,