Skip to content

Commit 24c80f3

Browse files
t3dotggclaude
andauthored
fix(claude): ENABLE_TOOL_SEARCH=auto no longer costs 10x cache writes per session (#24654)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 68d5b7a commit 24c80f3

2 files changed

Lines changed: 241 additions & 1 deletion

File tree

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

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,40 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
272272
);
273273
});
274274

275+
it("pins Claude's auto tool search on through flag settings", () => {
276+
const override = ClaudeAdapterV2.claudeToolSearchOverride;
277+
for (const value of ["auto", "auto:10", "auto:99.5", "auto:x", "auto:Infinity"]) {
278+
assert.equal(override(value), "true", value);
279+
}
280+
for (const value of [
281+
undefined,
282+
"true",
283+
"false",
284+
"auto:0",
285+
"auto:-5",
286+
"auto:100",
287+
"auto:1e2",
288+
"auto:1_000",
289+
"auto:1,000",
290+
"AUTO",
291+
]) {
292+
assert.isUndefined(override(value), String(value));
293+
}
294+
295+
const options = ClaudeAdapterV2.makeClaudeQueryOptions({
296+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
297+
nativeThreadId: "native-tool-search",
298+
resume: false,
299+
cwd: "/workspace",
300+
sdkSettings: { env: { OTHER: "kept" } },
301+
toolSearchOverride: "true",
302+
});
303+
assert.deepEqual((options.settings as { env?: Record<string, string> }).env, {
304+
OTHER: "kept",
305+
ENABLE_TOOL_SEARCH: "true",
306+
});
307+
});
308+
275309
it("projects AskUserQuestion input with question text as the answer key", () => {
276310
const preview = "\n **Proposed change**\n\n```ts\n\tconst enabled = true;\n```\n ";
277311
assert.deepEqual(
@@ -2846,6 +2880,96 @@ describe("ClaudeAdapterV2 background wake turns", () => {
28462880
),
28472881
),
28482882
);
2883+
2884+
it.effect("pins tool search on when the user's Claude settings choose auto", () =>
2885+
Effect.gen(function* () {
2886+
const fileSystem = yield* FileSystem.FileSystem;
2887+
const path = yield* Path.Path;
2888+
const home = yield* fileSystem.makeTempDirectoryScoped({
2889+
prefix: "t3-claude-tool-search-",
2890+
});
2891+
const openedToolSearch = (
2892+
settingsJson: string,
2893+
cwd = CLAUDE_TEST_RUNTIME_POLICY.cwd,
2894+
environment?: NodeJS.ProcessEnv,
2895+
) =>
2896+
Effect.scoped(
2897+
Effect.gen(function* () {
2898+
yield* fileSystem.writeFileString(path.join(home, "settings.json"), settingsJson);
2899+
const harness = yield* makeWakeHarnessWithOptions({
2900+
settings: { ...DEFAULT_CLAUDE_SETTINGS, homePath: home },
2901+
...(environment === undefined ? {} : { environment }),
2902+
});
2903+
yield* harness.runtime.startTurn(
2904+
makeClaudeTestTurnInput({
2905+
threadId: harness.threadId,
2906+
providerThread: harness.providerThread,
2907+
now: yield* DateTime.now,
2908+
attemptId: RunAttemptId.make("attempt-tool-search"),
2909+
text: "Run git status.",
2910+
attachments: [],
2911+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2912+
...CLAUDE_TEST_RUNTIME_POLICY,
2913+
cwd,
2914+
}),
2915+
}),
2916+
);
2917+
const settings = harness.getOpenedOptions()?.settings;
2918+
return typeof settings === "object" ? settings.env?.ENABLE_TOOL_SEARCH : undefined;
2919+
}),
2920+
);
2921+
assert.equal(yield* openedToolSearch('{"env":{"ENABLE_TOOL_SEARCH":"auto"}}'), "true");
2922+
assert.isUndefined(yield* openedToolSearch('{"env":{"ENABLE_TOOL_SEARCH":"false"}}'));
2923+
assert.isUndefined(yield* openedToolSearch("{}"));
2924+
2925+
// Claude Code keeps the local file at the repository root, the main checkout's root in a
2926+
// worktree, and that file beats the user's settings.
2927+
const repo = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-repo-" });
2928+
const worktree = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-wt-" });
2929+
const worktreeGitDir = path.join(repo, ".git", "worktrees", "wt");
2930+
yield* fileSystem.makeDirectory(worktreeGitDir, { recursive: true });
2931+
yield* fileSystem.writeFileString(path.join(worktreeGitDir, "commondir"), "../..\n");
2932+
yield* fileSystem.writeFileString(
2933+
path.join(worktree, ".git"),
2934+
`gitdir: ${worktreeGitDir}\n`,
2935+
);
2936+
yield* fileSystem.makeDirectory(path.join(repo, ".claude"));
2937+
yield* fileSystem.makeDirectory(path.join(repo, "packages", "app"), { recursive: true });
2938+
const rootLocal = (value: string) =>
2939+
fileSystem.writeFileString(
2940+
path.join(repo, ".claude", "settings.local.json"),
2941+
`{"env":{"ENABLE_TOOL_SEARCH":"${value}"}}`,
2942+
);
2943+
const userAuto = '{"env":{"ENABLE_TOOL_SEARCH":"auto"}}';
2944+
yield* rootLocal("auto");
2945+
assert.equal(yield* openedToolSearch("{}", worktree), "true");
2946+
yield* rootLocal("false");
2947+
assert.isUndefined(yield* openedToolSearch(userAuto, worktree));
2948+
assert.isUndefined(yield* openedToolSearch(userAuto, path.join(repo, "packages", "app")));
2949+
2950+
// Project settings beat user settings, and the process env applies only when no file sets it.
2951+
const project = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-project-" });
2952+
yield* fileSystem.makeDirectory(path.join(project, ".claude"));
2953+
yield* fileSystem.writeFileString(
2954+
path.join(project, ".claude", "settings.json"),
2955+
'{"env":{"ENABLE_TOOL_SEARCH":"auto"}}',
2956+
);
2957+
assert.equal(
2958+
yield* openedToolSearch('{"env":{"ENABLE_TOOL_SEARCH":"false"}}', project),
2959+
"true",
2960+
);
2961+
assert.equal(
2962+
yield* openedToolSearch("{}", CLAUDE_TEST_RUNTIME_POLICY.cwd, {
2963+
ENABLE_TOOL_SEARCH: "auto",
2964+
}),
2965+
"true",
2966+
);
2967+
}).pipe(
2968+
Effect.provide(
2969+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2970+
),
2971+
),
2972+
);
28492973
});
28502974

28512975
describe("Plan-mode tool approval", () => {

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

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,35 @@ export function claudeQueryEnvironment(
12731273
return { ...environment, CLAUDE_CODE_PROMPT_CACHE_TTL: subagentPromptCacheTtl };
12741274
}
12751275

1276+
// Claude Code's auto tool search sends every MCP schema in full while they fit under a share of the
1277+
// context window, which T3's tools do on 1M models. auto:0 already means on and auto:100 off.
1278+
export function claudeToolSearchOverride(value: string | undefined): "true" | undefined {
1279+
if (value === undefined) return undefined;
1280+
if (value === "auto") return "true";
1281+
if (!value.startsWith("auto:")) return undefined;
1282+
const percent = parseClaudeInteger(value.slice("auto:".length));
1283+
// Claude Code treats an unparsable auto:N as auto, and clamps N to 0..100.
1284+
if (Number.isNaN(percent)) return "true";
1285+
const clamped = Math.min(100, Math.max(0, percent));
1286+
return clamped > 0 && clamped < 100 ? "true" : undefined;
1287+
}
1288+
1289+
// Claude Code 2.1.310's integer parsing for auto:N. Scientific notation counts only when it is an
1290+
// integer, grouped digits (1_000, 1,000, or 1 000) are joined, and anything else uses parseInt.
1291+
const CLAUDE_SCIENTIFIC_INTEGER = /^[+-]?(\d+(\.\d*)?|\.\d+)[eE][+-]?\d+$/;
1292+
const CLAUDE_GROUPED_INTEGER = /^[+-]?\d{1,3}([_,\u00A0\u202F ])\d{3}(?:\1\d{3})*$/;
1293+
function parseClaudeInteger(text: string): number {
1294+
const value = text.trim();
1295+
if (value.length <= 32 && CLAUDE_SCIENTIFIC_INTEGER.test(value)) {
1296+
const number = Number(value);
1297+
return Number.isInteger(number) ? number : Number.NaN;
1298+
}
1299+
if (value.length <= 32 && CLAUDE_GROUPED_INTEGER.test(value)) {
1300+
return Number.parseInt(value.replace(/[_,\u00A0\u202F ]/g, ""), 10);
1301+
}
1302+
return Number.parseInt(value, 10);
1303+
}
1304+
12761305
// Claude Code defers MCP tools behind ToolSearch, so the agent only finds
12771306
// t3_background_wait when the system prompt says when to reach for it.
12781307
const CLAUDE_BACKGROUND_WAIT_INSTRUCTIONS = `
@@ -1302,6 +1331,8 @@ export function makeClaudeQueryOptions(input: {
13021331
readonly supportedDialogKinds?: ClaudeQueryOptions["supportedDialogKinds"];
13031332
readonly allowDangerouslySkipPermissions?: boolean;
13041333
readonly appendSystemPromptFileText?: string;
1334+
// Flag settings, because a user's settings.json env beats the process env in Claude Code.
1335+
readonly toolSearchOverride?: "true";
13051336
}): ClaudeAgentSdkQueryOptions {
13061337
const compiledSelection = compileClaudeModelSelection(input.modelSelection);
13071338
const {
@@ -1338,13 +1369,21 @@ export function makeClaudeQueryOptions(input: {
13381369
: typeof input.sdkSettings === "object" && input.sdkSettings !== null
13391370
? ({ ...input.sdkSettings, ...selectionSettings } as ClaudeSdkSettings)
13401371
: selectionSettings;
1341-
const effectiveQuerySettings =
1372+
const compactQuerySettings =
13421373
input.settings?.autoCompactWindow === undefined || input.settings.autoCompactWindow.length === 0
13431374
? querySettings
13441375
: ({
13451376
...(typeof querySettings === "object" && querySettings !== null ? querySettings : {}),
13461377
autoCompactWindow: Number(input.settings.autoCompactWindow),
13471378
} as ClaudeSdkSettings);
1379+
// A settings file path cannot take extra keys, so it goes through unchanged.
1380+
const effectiveQuerySettings =
1381+
input.toolSearchOverride === undefined || typeof compactQuerySettings === "string"
1382+
? compactQuerySettings
1383+
: ({
1384+
...compactQuerySettings,
1385+
env: { ...compactQuerySettings?.env, ENABLE_TOOL_SEARCH: input.toolSearchOverride },
1386+
} as ClaudeSdkSettings);
13481387
const options: ClaudeAgentSdkQueryOptions = {
13491388
model: compiledSelection.apiModelId,
13501389
tools: claudeAgentSdkQueryToolsForSdk(selectedTools),
@@ -2160,6 +2199,14 @@ const decodeClaudeSubagentCacheSettings = Schema.decodeUnknownEffect(
21602199
),
21612200
),
21622201
);
2202+
const decodeClaudeEnvSettings = Schema.decodeUnknownEffect(
2203+
fromLenientJson(
2204+
Schema.StructWithRest(
2205+
Schema.Struct({ env: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)) }),
2206+
[Schema.Record(Schema.String, Schema.Unknown)],
2207+
),
2208+
),
2209+
);
21632210

21642211
function claudeBypassAvailability(
21652212
settings: typeof ClaudeBypassSettings.Type,
@@ -3946,6 +3993,73 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
39463993
},
39473994
);
39483995

3996+
const readToolSearchSetting = (filePath: string) =>
3997+
fileSystem.readFileString(filePath).pipe(
3998+
Effect.flatMap(decodeClaudeEnvSettings),
3999+
Effect.map((settings) => {
4000+
const value = settings.env?.ENABLE_TOOL_SEARCH;
4001+
return typeof value === "string" ? value : undefined;
4002+
}),
4003+
Effect.orElseSucceed(() => undefined),
4004+
);
4005+
// A worktree's .git file points at <main>/.git/worktrees/<name>, whose commondir leads back to
4006+
// <main>/.git. A bare repository has no main checkout, so its worktree keeps its own file.
4007+
const mainCheckoutRoot = (checkout: string) =>
4008+
fileSystem.readFileString(path.join(checkout, ".git")).pipe(
4009+
Effect.flatMap((pointer) => {
4010+
const gitDir = /^gitdir:\s*(.+)$/m.exec(pointer)?.[1]?.trim();
4011+
if (gitDir === undefined) return Effect.succeed(checkout);
4012+
const worktreeGitDir = path.resolve(checkout, gitDir);
4013+
return fileSystem.readFileString(path.join(worktreeGitDir, "commondir")).pipe(
4014+
Effect.map((commonDir) => {
4015+
const common = path.resolve(worktreeGitDir, commonDir.trim());
4016+
return path.basename(common) === ".git" ? path.dirname(common) : checkout;
4017+
}),
4018+
);
4019+
}),
4020+
Effect.orElseSucceed(() => checkout),
4021+
);
4022+
// Claude Code keeps .claude/settings.local.json at the repository root, or in cwd outside a
4023+
// repository, on Windows, or when the root is home. Its owner check is skipped: missing it only
4024+
// leaves tool search on, Claude Code's default.
4025+
const localSettingsDirectory = Effect.fn("ClaudeAdapterV2.localSettingsDirectory")(function* (
4026+
cwd: string,
4027+
) {
4028+
if ((yield* HostProcess.Platform) === "win32") return cwd;
4029+
for (let directory = cwd; ; directory = path.dirname(directory)) {
4030+
const isCheckout = yield* fileSystem
4031+
.exists(path.join(directory, ".git"))
4032+
.pipe(Effect.orElseSucceed(() => false));
4033+
if (isCheckout) {
4034+
const root = yield* mainCheckoutRoot(directory);
4035+
return root === (yield* HostProcess.HomeDirectory) ? cwd : root;
4036+
}
4037+
if (path.dirname(directory) === directory) return cwd;
4038+
}
4039+
});
4040+
// Settings env beats the process env, local over project over user, and a local file an older
4041+
// Claude Code left in cwd applies under the root one. resolveSettings reads local from cwd only.
4042+
const resolveToolSearchOverride = Effect.fn("ClaudeAdapterV2.resolveToolSearchOverride")(
4043+
function* (cwd: string | null) {
4044+
const localDirectory = cwd === null ? null : yield* localSettingsDirectory(cwd);
4045+
const settingsFiles = [
4046+
...(cwd === null || localDirectory === null
4047+
? []
4048+
: [
4049+
path.join(localDirectory, ".claude", "settings.local.json"),
4050+
...(localDirectory === cwd ? [] : [path.join(cwd, ".claude", "settings.local.json")]),
4051+
path.join(cwd, ".claude", "settings.json"),
4052+
]),
4053+
path.join(homePath, "settings.json"),
4054+
];
4055+
for (const settingsFile of settingsFiles) {
4056+
const value = yield* readToolSearchSetting(settingsFile);
4057+
if (value !== undefined) return claudeToolSearchOverride(value);
4058+
}
4059+
return claudeToolSearchOverride(adapterOptions.environment.ENABLE_TOOL_SEARCH);
4060+
},
4061+
);
4062+
39494063
const userInvocableSkills = (cwd: string | null) =>
39504064
discoverClaudeSkills(
39514065
adapterOptions.settings,
@@ -9220,6 +9334,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92209334
turnInput.nativeThreadHasTurns ?? turnInput.providerTurnOrdinal > 1;
92219335
const shouldResume =
92229336
resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn;
9337+
const toolSearchOverride = yield* resolveToolSearchOverride(turnInput.runtimePolicy.cwd);
92239338
const queryOptions = makeClaudeQueryOptions({
92249339
modelSelection: turnInput.modelSelection,
92259340
nativeThreadId,
@@ -9252,6 +9367,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92529367
onUserDialog,
92539368
supportedDialogKinds: ["resume_return"],
92549369
...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }),
9370+
...(toolSearchOverride === undefined ? {} : { toolSearchOverride }),
92559371
});
92569372
const querySession = yield* queryRunner
92579373
.open({

0 commit comments

Comments
 (0)