Skip to content

Commit a742596

Browse files
committed
fix(server): keep Claude threshold with reused query
1 parent 2fe5769 commit a742596

2 files changed

Lines changed: 87 additions & 9 deletions

File tree

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7025,6 +7025,84 @@ describe("ClaudeAdapterV2 background wake turns", () => {
70257025
),
70267026
);
70277027

7028+
it.effect("reports the settings directory of a reused Claude query", () =>
7029+
Effect.gen(function* () {
7030+
const fileSystem = yield* FileSystem.FileSystem;
7031+
const path = yield* Path.Path;
7032+
const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-home-" });
7033+
const first = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-first-" });
7034+
const second = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-second-" });
7035+
for (const [directory, window] of [
7036+
[first, 150_000],
7037+
[second, 180_000],
7038+
] as const) {
7039+
yield* fileSystem.makeDirectory(path.join(directory, ".claude"));
7040+
yield* fileSystem.writeFileString(
7041+
path.join(directory, ".claude", "settings.json"),
7042+
JSON.stringify({ autoCompactWindow: window }),
7043+
);
7044+
}
7045+
const harness = yield* makeWakeHarnessWithOptions({
7046+
settings: { ...DEFAULT_CLAUDE_SETTINGS, homePath: home },
7047+
});
7048+
let firstOptions: ReturnType<typeof harness.getOpenedOptions>;
7049+
for (const [ordinal, cwd] of [
7050+
[1, first],
7051+
[2, second],
7052+
] as const) {
7053+
yield* harness.runtime.startTurn(
7054+
makeClaudeTestTurnInput({
7055+
threadId: harness.threadId,
7056+
providerThread: harness.providerThread,
7057+
now: yield* DateTime.now,
7058+
attemptId: RunAttemptId.make(`attempt-compact-cwd-${ordinal}`),
7059+
providerTurnOrdinal: ordinal,
7060+
text: "Continue.",
7061+
attachments: [],
7062+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
7063+
...CLAUDE_TEST_RUNTIME_POLICY,
7064+
cwd,
7065+
}),
7066+
}),
7067+
);
7068+
if (ordinal === 1) firstOptions = harness.getOpenedOptions();
7069+
else assert.strictEqual(harness.getOpenedOptions(), firstOptions);
7070+
yield* Queue.offer(
7071+
harness.sdkMessages,
7072+
usageSnapshotFrame(
7073+
`msg_cwd_${ordinal}`,
7074+
`00000000-0000-4000-8000-00000000080${ordinal}`,
7075+
{
7076+
...zeroUsage,
7077+
input_tokens: 1_000,
7078+
output_tokens: 10,
7079+
},
7080+
),
7081+
);
7082+
yield* Queue.offer(
7083+
harness.sdkMessages,
7084+
makeResultFrame({
7085+
uuid: `00000000-0000-4000-8000-00000000081${ordinal}`,
7086+
result: "Done.",
7087+
}),
7088+
);
7089+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
7090+
}
7091+
assert.deepEqual(
7092+
harness.events.flatMap((event) =>
7093+
event.type === "provider_turn.updated" && event.providerTurn.tokenUsage
7094+
? [event.providerTurn.tokenUsage.autoCompactThreshold]
7095+
: [],
7096+
),
7097+
[117_000, 117_000],
7098+
);
7099+
}).pipe(
7100+
Effect.provide(
7101+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7102+
),
7103+
),
7104+
);
7105+
70287106
it.effect("shows a hook-dropped Claude prompt once and skips info-level lines", () =>
70297107
Effect.gen(function* () {
70307108
const harness = yield* makeWakeHarness;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,6 +4131,8 @@ interface ActiveClaudeSubagent {
41314131
interface ClaudeLiveQueryContext {
41324132
readonly queryPolicy: ClaudeRuntimeQueryPolicy;
41334133
readonly nativeThreadId: string;
4134+
readonly settingsCwd: string | null;
4135+
readonly launchArgSettings: ClaudeSdkSettings | undefined;
41344136
readonly query: ClaudeAgentSdkQuerySession;
41354137
readonly queryPolicyKey: string;
41364138
selection: CompiledClaudeModelSelection;
@@ -4720,6 +4722,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
47204722
const resolveAutoCompactWindow = Effect.fn("ClaudeAdapterV2.resolveAutoCompactWindow")(function* (
47214723
cwd: string | null,
47224724
modelSelection: ModelSelection,
4725+
launchArgSettings: ClaudeSdkSettings | undefined,
47234726
) {
47244727
if (adapterOptions.settings.autoCompactWindow !== "")
47254728
return adapterOptions.settings.autoCompactWindow;
@@ -4743,13 +4746,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
47434746
};
47444747
return validWindow(modelWindow) ?? validWindow(Reflect.get(settings, "autoCompactWindow"));
47454748
};
4746-
const launch = yield* readClaudeLaunchArgSettings({
4747-
settings: adapterOptions.settings,
4748-
cwd,
4749-
fileSystem,
4750-
path,
4751-
});
4752-
const launchWindow = windowFrom(launch);
4749+
const launchWindow = windowFrom(launchArgSettings);
47534750
if (launchWindow !== undefined) return launchWindow;
47544751
for (const settingsFile of yield* settingsFilesByPrecedence(cwd)) {
47554752
const settings = yield* fileSystem.readFileString(settingsFile).pipe(
@@ -10480,6 +10477,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1048010477
const context: ClaudeLiveQueryContext = {
1048110478
queryPolicy,
1048210479
nativeThreadId,
10480+
settingsCwd: turnInput.runtimePolicy.cwd,
10481+
launchArgSettings,
1048310482
query: querySession,
1048410483
queryPolicyKey,
1048510484
selection: compiledSelection,
@@ -10642,11 +10641,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1064210641
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1064310642
Effect.provideService(Path.Path, path),
1064410643
);
10644+
const querySession = yield* openQuery(turnInput, nativeThreadId);
1064510645
const autoCompactWindow = yield* resolveAutoCompactWindow(
10646-
turnInput.runtimePolicy.cwd,
10646+
querySession.settingsCwd,
1064710647
turnInput.modelSelection,
10648+
querySession.launchArgSettings,
1064810649
);
10649-
const querySession = yield* openQuery(turnInput, nativeThreadId);
1065010650
const context: ActiveClaudeTurnContext = {
1065110651
input: turnInput,
1065210652
autoCompactWindow,

0 commit comments

Comments
 (0)