@@ -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 + ) [ e E ] [ + - ] ? \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.
12781307const 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
21642211function 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 = / ^ g i t d i r : \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