@@ -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+ ) ;
26882699const decodeClaudeEnvSettings = Schema . decodeUnknownEffect (
26892700 fromLenientJson (
26902701 Schema . StructWithRest (
@@ -4022,6 +4033,7 @@ function formatClaudeUsageLimitWait(waitMs: number): string {
40224033interface 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 ( / \[ 1 m \] $ / 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