@@ -89,7 +89,10 @@ import { resolveAttachmentPath } from "../../attachmentStore.ts";
8989import { resolveClaudeSdkExecutablePath } from "../../provider/Drivers/ClaudeExecutable.ts" ;
9090import { planClaudeSkillDispatch } from "../../provider/Drivers/ClaudeSkillDispatch.ts" ;
9191import { discoverClaudeSkills } from "../../provider/Drivers/ClaudeSkills.ts" ;
92- import { compileClaudeModelSelection } from "../../claudeModelOptions.ts" ;
92+ import {
93+ type CompiledClaudeModelSelection ,
94+ compileClaudeModelSelection ,
95+ } from "../../claudeModelOptions.ts" ;
9396import * as ServerConfig from "../../config.ts" ;
9497import { expandHomePath } from "../../pathExpansion.ts" ;
9598import {
@@ -98,8 +101,9 @@ import {
98101} from "../../provider/Drivers/ClaudeHome.ts" ;
99102import {
100103 BUNDLED_CLAUDE_MODEL_CATALOG ,
101- resolveClaudeCatalogContextWindow ,
104+ type ClaudeModelCatalog ,
102105 resolveClaudeCatalogContextWindowTokens ,
106+ scopeClaudeModelCatalog ,
103107} from "../../provider/ClaudeModelCatalog.ts" ;
104108import {
105109 boundProviderEventForLogging ,
@@ -138,11 +142,11 @@ import {
138142export const CLAUDE_PROVIDER = ProviderDriverKind . make ( "claudeAgent" ) ;
139143export const CLAUDE_AGENT_SDK_QUERY_PROTOCOL = "claude-agent-sdk.query" as const ;
140144
141- function claudeContextWindow ( modelSelection : ModelSelection ) : number | null {
142- if ( modelSelection . model === "claude-opus-4-6" || modelSelection . model === "claude-opus-4-7" ) {
143- return 1_000_000 ;
144- }
145- return resolveClaudeCatalogContextWindow ( BUNDLED_CLAUDE_MODEL_CATALOG , modelSelection ) === "1m"
145+ // Claude Code runs the 1M window when the query's model id carries [1m].
146+ function claudeContextWindow ( modelSelection : ModelSelection , apiModelId : string ) : number {
147+ return modelSelection . model === "claude-opus-4-6" ||
148+ modelSelection . model === "claude-opus-4-7" ||
149+ apiModelId . endsWith ( "[1m]" )
146150 ? 1_000_000
147151 : 200_000 ;
148152}
@@ -155,6 +159,7 @@ export function claudeProviderTurnTokenUsage(
155159 readonly output_tokens : number ;
156160 } ,
157161 modelSelection : ModelSelection ,
162+ apiModelId : string ,
158163 updatedAt : string ,
159164) {
160165 const inputTokens =
@@ -164,7 +169,7 @@ export function claudeProviderTurnTokenUsage(
164169 const outputTokens = usage . output_tokens ;
165170 return {
166171 usedTokens : inputTokens + outputTokens ,
167- maxTokens : claudeContextWindow ( modelSelection ) ,
172+ maxTokens : claudeContextWindow ( modelSelection , apiModelId ) ,
168173 inputTokens,
169174 cachedInputTokens : usage . cache_read_input_tokens ?? 0 ,
170175 outputTokens,
@@ -803,6 +808,7 @@ export const layerQueryRunner: Layer.Layer<
803808
804809export function makeClaudeQueryOptions ( input : {
805810 readonly modelSelection : ModelSelection ;
811+ readonly modelCatalog ?: ClaudeModelCatalog ;
806812 readonly nativeThreadId : string ;
807813 readonly resume : boolean ;
808814 readonly resumeSessionAt ?: string ;
@@ -827,7 +833,7 @@ export function makeClaudeQueryOptions(input: {
827833 readonly supportedDialogKinds ?: ClaudeQueryOptions [ "supportedDialogKinds" ] ;
828834 readonly allowDangerouslySkipPermissions ?: boolean ;
829835} ) : ClaudeAgentSdkQueryOptions {
830- const compiledSelection = compileClaudeModelSelection ( input . modelSelection ) ;
836+ const compiledSelection = compileClaudeModelSelection ( input . modelSelection , input . modelCatalog ) ;
831837 const {
832838 "permission-mode" : launchArgPermissionMode ,
833839 "dangerously-skip-permissions" : launchArgSkipPermissions ,
@@ -2709,6 +2715,9 @@ function formatClaudeUsageLimitWait(waitMs: number): string {
27092715
27102716interface ActiveClaudeTurnContext {
27112717 readonly input : ProviderAdapter . ProviderAdapterV2TurnInput ;
2718+ // Compiled when the turn started. A reused process runs the same query
2719+ // identity, so steers and usage read the effort and window from here.
2720+ readonly compiledSelection : CompiledClaudeModelSelection ;
27122721 readonly nativeTurnId : string ;
27132722 nativeMessageCursor : string | null ;
27142723 readonly providerTurnId : OrchestrationV2ProviderTurn [ "id" ] ;
@@ -3006,6 +3015,8 @@ export interface ClaudeAdapterV2Options {
30063015 readonly crypto : Crypto . Crypto ;
30073016 readonly idAllocator : IdAllocator . IdAllocatorV2Shape ;
30083017 readonly queryRunner : ClaudeAgentSdkQueryRunnerShape ;
3018+ /** The live model catalog the provider snapshot shows; defaults to the bundled one. */
3019+ readonly modelCatalog ?: Effect . Effect < ClaudeModelCatalog > ;
30093020 readonly scopedLimitNames ?: Ref . Ref < ClaudeScopedLimitNames > ;
30103021 readonly onUsageLimits ?: ServerProviderShape [ "applyUsageLimits" ] ;
30113022 /** Sink for wake-turn continuation requests; defaults to dropping them. */
@@ -3023,6 +3034,11 @@ export function makeClaudeAdapterV2(
30233034 const continuationRequests = adapterOptions . continuationRequests ?? {
30243035 offer : ( ) => Effect . void ,
30253036 } ;
3037+ const scopedModelCatalog = (
3038+ adapterOptions . modelCatalog ?? Effect . succeed ( BUNDLED_CLAUDE_MODEL_CATALOG )
3039+ ) . pipe (
3040+ Effect . map ( ( catalog ) => scopeClaudeModelCatalog ( catalog , adapterOptions . settings . customModels ) ) ,
3041+ ) ;
30263042
30273043 // Re-scan on every send: skills are added and switched off mid-session, and
30283044 // the scan is a few directory reads. A skill switched off via skillOverrides,
@@ -3055,6 +3071,9 @@ export function makeClaudeAdapterV2(
30553071 function * ( input : ProviderAdapter . ProviderAdapterV2OpenSessionInput ) {
30563072 const sessionScope = yield * Effect . scope ;
30573073 const now = yield * DateTime . now ;
3074+ // The first read loads the manifest's disk cache. Later reads never
3075+ // suspend, so getModelContextWindow can run them synchronously.
3076+ yield * scopedModelCatalog ;
30583077 const session = providerSession ( {
30593078 providerSessionId : input . providerSessionId ,
30603079 providerInstanceId : adapterOptions . instanceId ,
@@ -5798,7 +5817,10 @@ export function makeClaudeAdapterV2(
57985817 completedAt : null ,
57995818 tokenUsage : {
58005819 usedTokens : afterTokenCount ,
5801- maxTokens : claudeContextWindow ( context . input . modelSelection ) ,
5820+ maxTokens : claudeContextWindow (
5821+ context . input . modelSelection ,
5822+ context . compiledSelection . apiModelId ,
5823+ ) ,
58025824 updatedAt : DateTime . formatIso ( now ) ,
58035825 } ,
58045826 } ,
@@ -5945,6 +5967,7 @@ export function makeClaudeAdapterV2(
59455967 tokenUsage : claudeProviderTurnTokenUsage (
59465968 message . message . usage ,
59475969 context . input . modelSelection ,
5970+ context . compiledSelection . apiModelId ,
59485971 DateTime . formatIso ( now ) ,
59495972 ) ,
59505973 } ,
@@ -7041,6 +7064,7 @@ export function makeClaudeAdapterV2(
70417064 const openQuery = Effect . fnUntraced ( function * (
70427065 turnInput : ProviderAdapter . ProviderAdapterV2TurnInput ,
70437066 nativeThreadId : string ,
7067+ modelCatalog : ClaudeModelCatalog ,
70447068 ) {
70457069 const queryPolicy = claudeRuntimeQueryPolicyForRuntimePolicy ( turnInput . runtimePolicy ) ;
70467070 const mcpOverrides = claudeMcpQueryOverrides ( {
@@ -7052,7 +7076,10 @@ export function makeClaudeAdapterV2(
70527076 : { allowedTools : queryPolicy . allowedTools } ) ,
70537077 } ) ;
70547078 const queryPolicyKey = claudeEffectiveQueryPolicyKey ( queryPolicy , mcpOverrides ) ;
7055- const compiledSelection = compileClaudeModelSelection ( turnInput . modelSelection ) ;
7079+ const compiledSelection = compileClaudeModelSelection (
7080+ turnInput . modelSelection ,
7081+ modelCatalog ,
7082+ ) ;
70567083 const resumeSessionAt = yield * getNativeConversationHeadId ( turnInput . providerThread ) ;
70577084 const existing = yield * Ref . get ( queryContext ) ;
70587085 // A continuation prompts nothing: it drains output the live process
@@ -7122,6 +7149,7 @@ export function makeClaudeAdapterV2(
71227149 resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn ;
71237150 const queryOptions = makeClaudeQueryOptions ( {
71247151 modelSelection : turnInput . modelSelection ,
7152+ modelCatalog,
71257153 nativeThreadId,
71267154 resume : shouldResume ,
71277155 ...( resumeSessionAt === undefined ? { } : { resumeSessionAt } ) ,
@@ -7247,6 +7275,11 @@ export function makeClaudeAdapterV2(
72477275 function * ( turnInput : ProviderAdapter . ProviderAdapterV2TurnInput ) {
72487276 const startedAt = yield * DateTime . now ;
72497277 const nativeThreadId = yield * getNativeThreadId ( turnInput . providerThread ) ;
7278+ const modelCatalog = yield * scopedModelCatalog ;
7279+ const compiledSelection = compileClaudeModelSelection (
7280+ turnInput . modelSelection ,
7281+ modelCatalog ,
7282+ ) ;
72507283 const nativeTurnId = `turn:${ turnInput . attemptId } ` ;
72517284 const promptUuid = isClaudeProviderContinuationTurn ( turnInput )
72527285 ? null
@@ -7279,6 +7312,7 @@ export function makeClaudeAdapterV2(
72797312 }
72807313 const context : ActiveClaudeTurnContext = {
72817314 input : turnInput ,
7315+ compiledSelection,
72827316 nativeTurnId,
72837317 nativeMessageCursor : null ,
72847318 providerTurnId,
@@ -7324,15 +7358,15 @@ export function makeClaudeAdapterV2(
73247358 : yield * makeClaudeUserMessageWithAttachments ( {
73257359 text : applyClaudePromptEffortPrefix (
73267360 turnInput . message . text ,
7327- compileClaudeModelSelection ( turnInput . modelSelection ) . promptEffort ,
7361+ compiledSelection . promptEffort ,
73287362 ) ,
73297363 attachments : turnInput . message . attachments ,
73307364 attachmentsDir,
73317365 fileSystem,
73327366 skillNames : yield * userInvocableSkillNames ( turnInput . runtimePolicy . cwd ) ,
73337367 uuid : promptUuid ,
73347368 } ) ;
7335- const querySession = yield * openQuery ( turnInput , nativeThreadId ) ;
7369+ const querySession = yield * openQuery ( turnInput , nativeThreadId , modelCatalog ) ;
73367370 yield * Ref . set ( activeTurn , context ) ;
73377371 yield * emitProviderEvent ( {
73387372 type : "provider_turn.updated" ,
@@ -7533,7 +7567,7 @@ export function makeClaudeAdapterV2(
75337567 const userMessage = yield * makeClaudeUserMessageWithAttachments ( {
75347568 text : applyClaudePromptEffortPrefix (
75357569 turnInput . message . text ,
7536- compileClaudeModelSelection ( currentTurn . input . modelSelection ) . promptEffort ,
7570+ currentTurn . compiledSelection . promptEffort ,
75377571 ) ,
75387572 attachments : turnInput . message . attachments ,
75397573 priority : "now" ,
@@ -7610,7 +7644,7 @@ export function makeClaudeAdapterV2(
76107644 providerSessionId : input . providerSessionId ,
76117645 providerSession : session ,
76127646 getModelContextWindow : ( selection ) =>
7613- resolveClaudeCatalogContextWindowTokens ( BUNDLED_CLAUDE_MODEL_CATALOG , selection ) ,
7647+ resolveClaudeCatalogContextWindowTokens ( Effect . runSync ( scopedModelCatalog ) , selection ) ,
76147648 events : Stream . fromEffectRepeat ( Queue . take ( events ) ) ,
76157649 hasPendingBackgroundWork : Effect . gen ( function * ( ) {
76167650 // Session capability: any native thread with pending work pins idle.
@@ -7924,7 +7958,7 @@ export type ClaudeAdapterV2DriverEnv =
79247958export const createClaudeAdapterV2 = Effect . fn ( "ClaudeAdapterV2Driver.create" ) (
79257959 function * (
79267960 input : ProviderAdapterDriverCreateInput < ClaudeSettings > ,
7927- hooks : Pick < ClaudeAdapterV2Options , "scopedLimitNames" | "onUsageLimits" > = { } ,
7961+ hooks : Pick < ClaudeAdapterV2Options , "modelCatalog" | " scopedLimitNames" | "onUsageLimits" > = { } ,
79287962 ) {
79297963 const { instanceId, environment, enabled, config } = input ;
79307964 const fileSystem = yield * FileSystem . FileSystem ;
0 commit comments