Skip to content

Commit 2fe5769

Browse files
committed
fix(server): report Claude settings compaction threshold
1 parent 47f2c8d commit 2fe5769

2 files changed

Lines changed: 162 additions & 16 deletions

File tree

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

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6886,12 +6886,91 @@ describe("ClaudeAdapterV2 background wake turns", () => {
68866886
{ autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
68876887
{ autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined },
68886888
{ autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 617_000 },
6889+
{
6890+
autoCompactWindow: "",
6891+
model: "claude-opus-5-5",
6892+
userSettings: '{"autoCompactWindow":500000}',
6893+
threshold: 467_000,
6894+
},
6895+
{
6896+
autoCompactWindow: "300000",
6897+
model: "claude-opus-5-5",
6898+
userSettings: '{"autoCompactWindow":500000}',
6899+
threshold: 267_000,
6900+
},
6901+
{
6902+
autoCompactWindow: "",
6903+
model: "claude-opus-5-5",
6904+
userSettings: '{"autoCompactWindow":500000}',
6905+
projectSettings: '{"autoCompactWindow":400000}',
6906+
threshold: 367_000,
6907+
},
6908+
{
6909+
autoCompactWindow: "",
6910+
model: "claude-opus-5-5",
6911+
userSettings:
6912+
'{"autoCompactWindow":500000,"modelSettings":{"claude-opus-5-5":{"autoCompactWindow":450000}}}',
6913+
threshold: 417_000,
6914+
},
6915+
{
6916+
autoCompactWindow: "",
6917+
model: "claude-opus-5-5",
6918+
userSettings: '{"autoCompactWindow":500000}',
6919+
projectSettings: '{"modelSettings":{"claude-opus-5-5":{"autoCompactWindow":"auto"}}}',
6920+
threshold: undefined,
6921+
},
6922+
{
6923+
autoCompactWindow: "",
6924+
model: "claude-opus-5-5",
6925+
userSettings: '{"autoCompactWindow":500000}',
6926+
launchArgs: `--settings '{"autoCompactWindow":350000}'`,
6927+
threshold: 317_000,
6928+
},
6929+
{
6930+
autoCompactWindow: "",
6931+
model: "claude-opus-5-5",
6932+
userSettings: '{"autoCompactWindow":"invalid"}',
6933+
threshold: undefined,
6934+
},
6935+
{
6936+
autoCompactWindow: "",
6937+
model: "claude-opus-5-5",
6938+
userSettings: '{"autoCompactWindow":500000}',
6939+
projectSettings: '{"autoCompactWindow":50000}',
6940+
threshold: 467_000,
6941+
},
6942+
{
6943+
autoCompactWindow: "",
6944+
model: "claude-opus-5-5",
6945+
userSettings:
6946+
'{"autoCompactWindow":500000,"modelSettings":{"claude-opus-5-5":{"autoCompactWindow":50000}}}',
6947+
threshold: 467_000,
6948+
},
68896949
])(
68906950
"ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
6891-
({ autoCompactWindow, model, threshold }) =>
6951+
({ autoCompactWindow, model, threshold, userSettings, projectSettings, launchArgs }) =>
68926952
Effect.gen(function* () {
6953+
const fileSystem = yield* FileSystem.FileSystem;
6954+
const path = yield* Path.Path;
6955+
const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-compact-" });
6956+
const project = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-compact-" });
6957+
if (userSettings !== undefined) {
6958+
yield* fileSystem.writeFileString(path.join(home, "settings.json"), userSettings);
6959+
}
6960+
if (projectSettings !== undefined) {
6961+
yield* fileSystem.makeDirectory(path.join(project, ".claude"));
6962+
yield* fileSystem.writeFileString(
6963+
path.join(project, ".claude", "settings.json"),
6964+
projectSettings,
6965+
);
6966+
}
68936967
const harness = yield* makeWakeHarnessWithOptions({
6894-
settings: { ...DEFAULT_CLAUDE_SETTINGS, autoCompactWindow },
6968+
settings: {
6969+
...DEFAULT_CLAUDE_SETTINGS,
6970+
autoCompactWindow,
6971+
homePath: home,
6972+
launchArgs: launchArgs ?? "",
6973+
},
68956974
});
68966975
yield* harness.runtime.startTurn(
68976976
makeClaudeTestTurnInput({
@@ -6902,6 +6981,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
69026981
text: "Continue.",
69036982
attachments: [],
69046983
modelSelection: { ...CLAUDE_TEST_MODEL_SELECTION, model },
6984+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
6985+
...CLAUDE_TEST_RUNTIME_POLICY,
6986+
cwd: project,
6987+
}),
69056988
}),
69066989
);
69076990
const usage = { ...zeroUsage, input_tokens: 1_000, output_tokens: 75 };

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

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,17 @@ const decodeClaudeSubagentCacheSettings = Schema.decodeUnknownEffect(
26852685
),
26862686
),
26872687
);
2688+
const decodeClaudeCompactSettings = Schema.decodeUnknownEffect(
2689+
fromLenientJson(
2690+
Schema.Struct({
2691+
autoCompactWindow: Schema.optionalKey(Schema.Unknown),
2692+
modelSettings: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)),
2693+
}),
2694+
),
2695+
);
2696+
const decodeClaudeCompactWindow = Schema.decodeUnknownOption(
2697+
Schema.Union([Schema.Int, Schema.Literal("auto")]),
2698+
);
26882699
const decodeClaudeEnvSettings = Schema.decodeUnknownEffect(
26892700
fromLenientJson(
26902701
Schema.StructWithRest(
@@ -4022,6 +4033,7 @@ function formatClaudeUsageLimitWait(waitMs: number): string {
40224033
interface ActiveClaudeTurnContext {
40234034
readonly queryPolicy: ClaudeRuntimeQueryPolicy;
40244035
readonly input: ProviderAdapter.ProviderAdapterV2TurnInput;
4036+
readonly autoCompactWindow: string;
40254037
readonly nativeThreadId: string;
40264038
readonly nativeTurnId: string;
40274039
nativeMessageCursor: string | null;
@@ -4675,15 +4687,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
46754687
if (path.dirname(directory) === directory) return cwd;
46764688
}
46774689
});
4678-
// Settings env beats the process env, --settings over local over project over user, and a local
4679-
// file an older Claude Code left in cwd applies under the root one. resolveSettings reads local
4680-
// from cwd only.
4681-
const resolveToolSearchOverride = Effect.fn("ClaudeAdapterV2.resolveToolSearchOverride")(
4682-
function* (cwd: string | null, launchArgSettings: ClaudeSdkSettings | undefined) {
4683-
const launchValue = launchArgSettings?.env?.ENABLE_TOOL_SEARCH;
4684-
if (typeof launchValue === "string") return claudeToolSearchOverride(launchValue);
4690+
const settingsFilesByPrecedence = Effect.fn("ClaudeAdapterV2.settingsFilesByPrecedence")(
4691+
function* (cwd: string | null) {
46854692
const localDirectory = cwd === null ? null : yield* localSettingsDirectory(cwd);
4686-
const settingsFiles = [
4693+
return [
46874694
...(cwd === null || localDirectory === null
46884695
? []
46894696
: [
@@ -4693,14 +4700,68 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
46934700
]),
46944701
path.join(homePath, "settings.json"),
46954702
];
4696-
for (const settingsFile of settingsFiles) {
4703+
},
4704+
);
4705+
// Settings env beats the process env, --settings over local over project over user, and a local
4706+
// file an older Claude Code left in cwd applies under the root one. resolveSettings reads local
4707+
// from cwd only.
4708+
const resolveToolSearchOverride = Effect.fn("ClaudeAdapterV2.resolveToolSearchOverride")(
4709+
function* (cwd: string | null, launchArgSettings: ClaudeSdkSettings | undefined) {
4710+
const launchValue = launchArgSettings?.env?.ENABLE_TOOL_SEARCH;
4711+
if (typeof launchValue === "string") return claudeToolSearchOverride(launchValue);
4712+
for (const settingsFile of yield* settingsFilesByPrecedence(cwd)) {
46974713
const value = yield* readToolSearchSetting(settingsFile);
46984714
if (value !== undefined) return claudeToolSearchOverride(value);
46994715
}
47004716
return claudeToolSearchOverride(adapterOptions.environment.ENABLE_TOOL_SEARCH);
47014717
},
47024718
);
47034719

4720+
const resolveAutoCompactWindow = Effect.fn("ClaudeAdapterV2.resolveAutoCompactWindow")(function* (
4721+
cwd: string | null,
4722+
modelSelection: ModelSelection,
4723+
) {
4724+
if (adapterOptions.settings.autoCompactWindow !== "")
4725+
return adapterOptions.settings.autoCompactWindow;
4726+
const model = compileClaudeModelSelection(modelSelection).apiModelId.replace(/\[1m\]$/i, "");
4727+
const windowFrom = (settings: unknown) => {
4728+
if (typeof settings !== "object" || settings === null) return undefined;
4729+
const models = Reflect.get(settings, "modelSettings");
4730+
const modelSetting =
4731+
typeof models === "object" && models !== null ? Reflect.get(models, model) : undefined;
4732+
const modelWindow =
4733+
typeof modelSetting === "object" && modelSetting !== null
4734+
? Reflect.get(modelSetting, "autoCompactWindow")
4735+
: undefined;
4736+
const validWindow = (value: unknown) => {
4737+
const selected = Option.getOrUndefined(decodeClaudeCompactWindow(value));
4738+
return selected === "auto"
4739+
? ""
4740+
: typeof selected === "number" && selected >= 100_000 && selected <= 1_000_000
4741+
? String(selected)
4742+
: undefined;
4743+
};
4744+
return validWindow(modelWindow) ?? validWindow(Reflect.get(settings, "autoCompactWindow"));
4745+
};
4746+
const launch = yield* readClaudeLaunchArgSettings({
4747+
settings: adapterOptions.settings,
4748+
cwd,
4749+
fileSystem,
4750+
path,
4751+
});
4752+
const launchWindow = windowFrom(launch);
4753+
if (launchWindow !== undefined) return launchWindow;
4754+
for (const settingsFile of yield* settingsFilesByPrecedence(cwd)) {
4755+
const settings = yield* fileSystem.readFileString(settingsFile).pipe(
4756+
Effect.flatMap(decodeClaudeCompactSettings),
4757+
Effect.orElseSucceed(() => undefined),
4758+
);
4759+
const value = windowFrom(settings);
4760+
if (value !== undefined) return value;
4761+
}
4762+
return "";
4763+
});
4764+
47044765
const userInvocableSkills = (cwd: string | null) =>
47054766
discoverClaudeSkills(
47064767
adapterOptions.settings,
@@ -5083,7 +5144,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
50835144
usage,
50845145
context.input.modelSelection,
50855146
DateTime.formatIso(now),
5086-
adapterOptions.settings.autoCompactWindow,
5147+
context.autoCompactWindow,
50875148
context.streamUsage?.requestedAt,
50885149
);
50895150
if (tokenUsage.inputTokens <= 0) return;
@@ -8721,10 +8782,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
87218782
tokenUsage: {
87228783
usedTokens: afterTokenCount,
87238784
maxTokens,
8724-
...claudeAutoCompactThreshold(
8725-
adapterOptions.settings.autoCompactWindow,
8726-
maxTokens,
8727-
),
8785+
...claudeAutoCompactThreshold(context.autoCompactWindow, maxTokens),
87288786
updatedAt: DateTime.formatIso(now),
87298787
},
87308788
},
@@ -10584,9 +10642,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1058410642
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1058510643
Effect.provideService(Path.Path, path),
1058610644
);
10645+
const autoCompactWindow = yield* resolveAutoCompactWindow(
10646+
turnInput.runtimePolicy.cwd,
10647+
turnInput.modelSelection,
10648+
);
1058710649
const querySession = yield* openQuery(turnInput, nativeThreadId);
1058810650
const context: ActiveClaudeTurnContext = {
1058910651
input: turnInput,
10652+
autoCompactWindow,
1059010653
queryPolicy: querySession.queryPolicy,
1059110654
nativeThreadId,
1059210655
nativeTurnId,

0 commit comments

Comments
 (0)