@@ -98,8 +98,9 @@ import {
9898} from "../../provider/Drivers/ClaudeHome.ts" ;
9999import {
100100 BUNDLED_CLAUDE_MODEL_CATALOG ,
101- resolveClaudeCatalogContextWindow ,
101+ type ClaudeModelCatalog ,
102102 resolveClaudeCatalogContextWindowTokens ,
103+ scopeClaudeModelCatalog ,
103104} from "../../provider/ClaudeModelCatalog.ts" ;
104105import {
105106 boundProviderEventForLogging ,
@@ -138,13 +139,18 @@ import {
138139export const CLAUDE_PROVIDER = ProviderDriverKind . make ( "claudeAgent" ) ;
139140export const CLAUDE_AGENT_SDK_QUERY_PROTOCOL = "claude-agent-sdk.query" as const ;
140141
141- function claudeContextWindow ( modelSelection : ModelSelection ) : number | null {
142+ function claudeContextWindow (
143+ modelCatalog : ClaudeModelCatalog ,
144+ modelSelection : ModelSelection ,
145+ apiModelId : string ,
146+ ) : number {
142147 if ( modelSelection . model === "claude-opus-4-6" || modelSelection . model === "claude-opus-4-7" ) {
143148 return 1_000_000 ;
144149 }
145- return resolveClaudeCatalogContextWindow ( BUNDLED_CLAUDE_MODEL_CATALOG , modelSelection ) === "1m"
146- ? 1_000_000
147- : 200_000 ;
150+ return (
151+ resolveClaudeCatalogContextWindowTokens ( modelCatalog , modelSelection ) ??
152+ ( apiModelId . endsWith ( "[1m]" ) ? 1_000_000 : 200_000 )
153+ ) ;
148154}
149155
150156export function claudeProviderTurnTokenUsage (
@@ -154,7 +160,7 @@ export function claudeProviderTurnTokenUsage(
154160 readonly cache_read_input_tokens ?: number | null ;
155161 readonly output_tokens : number ;
156162 } ,
157- modelSelection : ModelSelection ,
163+ maxTokens : number ,
158164 updatedAt : string ,
159165) {
160166 const inputTokens =
@@ -164,7 +170,7 @@ export function claudeProviderTurnTokenUsage(
164170 const outputTokens = usage . output_tokens ;
165171 return {
166172 usedTokens : inputTokens + outputTokens ,
167- maxTokens : claudeContextWindow ( modelSelection ) ,
173+ maxTokens,
168174 inputTokens,
169175 cachedInputTokens : usage . cache_read_input_tokens ?? 0 ,
170176 outputTokens,
@@ -803,6 +809,7 @@ export const layerQueryRunner: Layer.Layer<
803809
804810export function makeClaudeQueryOptions ( input : {
805811 readonly modelSelection : ModelSelection ;
812+ readonly modelCatalog ?: ClaudeModelCatalog ;
806813 readonly nativeThreadId : string ;
807814 readonly resume : boolean ;
808815 readonly resumeSessionAt ?: string ;
@@ -827,7 +834,7 @@ export function makeClaudeQueryOptions(input: {
827834 readonly supportedDialogKinds ?: ClaudeQueryOptions [ "supportedDialogKinds" ] ;
828835 readonly allowDangerouslySkipPermissions ?: boolean ;
829836} ) : ClaudeAgentSdkQueryOptions {
830- const compiledSelection = compileClaudeModelSelection ( input . modelSelection ) ;
837+ const compiledSelection = compileClaudeModelSelection ( input . modelSelection , input . modelCatalog ) ;
831838 const {
832839 "permission-mode" : launchArgPermissionMode ,
833840 "dangerously-skip-permissions" : launchArgSkipPermissions ,
@@ -2709,6 +2716,8 @@ function formatClaudeUsageLimitWait(waitMs: number): string {
27092716
27102717interface ActiveClaudeTurnContext {
27112718 readonly input : ProviderAdapter . ProviderAdapterV2TurnInput ;
2719+ // The effort prefix of this turn's prompt, repeated on its steers.
2720+ readonly promptEffort : string | undefined ;
27122721 readonly nativeTurnId : string ;
27132722 nativeMessageCursor : string | null ;
27142723 readonly providerTurnId : OrchestrationV2ProviderTurn [ "id" ] ;
@@ -2788,6 +2797,8 @@ interface ClaudeLiveQueryContext {
27882797 readonly query : ClaudeAgentSdkQuerySession ;
27892798 readonly queryPolicyKey : string ;
27902799 readonly selectionKey : string ;
2800+ // Resolved when the process opened; usage reports what the process runs.
2801+ readonly contextWindow : number ;
27912802 readonly closed : Deferred . Deferred < void , never > ;
27922803 // Whether this CLI process echoes a prompt's uuid on the first frame of
27932804 // the turn answering it ("early") or only on its result. Learned from the
@@ -3006,6 +3017,8 @@ export interface ClaudeAdapterV2Options {
30063017 readonly crypto : Crypto . Crypto ;
30073018 readonly idAllocator : IdAllocator . IdAllocatorV2Shape ;
30083019 readonly queryRunner : ClaudeAgentSdkQueryRunnerShape ;
3020+ /** The live model catalog the provider snapshot shows; defaults to the bundled one. */
3021+ readonly modelCatalog ?: Effect . Effect < ClaudeModelCatalog > ;
30093022 readonly scopedLimitNames ?: Ref . Ref < ClaudeScopedLimitNames > ;
30103023 readonly onUsageLimits ?: ServerProviderShape [ "applyUsageLimits" ] ;
30113024 /** Sink for wake-turn continuation requests; defaults to dropping them. */
@@ -3023,6 +3036,11 @@ export function makeClaudeAdapterV2(
30233036 const continuationRequests = adapterOptions . continuationRequests ?? {
30243037 offer : ( ) => Effect . void ,
30253038 } ;
3039+ const scopedModelCatalog = (
3040+ adapterOptions . modelCatalog ?? Effect . succeed ( BUNDLED_CLAUDE_MODEL_CATALOG )
3041+ ) . pipe (
3042+ Effect . map ( ( catalog ) => scopeClaudeModelCatalog ( catalog , adapterOptions . settings . customModels ) ) ,
3043+ ) ;
30263044
30273045 // Re-scan on every send: skills are added and switched off mid-session, and
30283046 // the scan is a few directory reads. A skill switched off via skillOverrides,
@@ -3055,6 +3073,9 @@ export function makeClaudeAdapterV2(
30553073 function * ( input : ProviderAdapter . ProviderAdapterV2OpenSessionInput ) {
30563074 const sessionScope = yield * Effect . scope ;
30573075 const now = yield * DateTime . now ;
3076+ // The first read loads the manifest's disk cache. Later reads never
3077+ // suspend, so getModelContextWindow can run them synchronously.
3078+ yield * scopedModelCatalog ;
30583079 const session = providerSession ( {
30593080 providerSessionId : input . providerSessionId ,
30603081 providerInstanceId : adapterOptions . instanceId ,
@@ -5798,7 +5819,7 @@ export function makeClaudeAdapterV2(
57985819 completedAt : null ,
57995820 tokenUsage : {
58005821 usedTokens : afterTokenCount ,
5801- maxTokens : claudeContextWindow ( context . input . modelSelection ) ,
5822+ maxTokens : liveQuery . contextWindow ,
58025823 updatedAt : DateTime . formatIso ( now ) ,
58035824 } ,
58045825 } ,
@@ -5944,7 +5965,7 @@ export function makeClaudeAdapterV2(
59445965 completedAt : null ,
59455966 tokenUsage : claudeProviderTurnTokenUsage (
59465967 message . message . usage ,
5947- context . input . modelSelection ,
5968+ liveQuery . contextWindow ,
59485969 DateTime . formatIso ( now ) ,
59495970 ) ,
59505971 } ,
@@ -7041,6 +7062,7 @@ export function makeClaudeAdapterV2(
70417062 const openQuery = Effect . fnUntraced ( function * (
70427063 turnInput : ProviderAdapter . ProviderAdapterV2TurnInput ,
70437064 nativeThreadId : string ,
7065+ modelCatalog : ClaudeModelCatalog ,
70447066 ) {
70457067 const queryPolicy = claudeRuntimeQueryPolicyForRuntimePolicy ( turnInput . runtimePolicy ) ;
70467068 const mcpOverrides = claudeMcpQueryOverrides ( {
@@ -7052,7 +7074,10 @@ export function makeClaudeAdapterV2(
70527074 : { allowedTools : queryPolicy . allowedTools } ) ,
70537075 } ) ;
70547076 const queryPolicyKey = claudeEffectiveQueryPolicyKey ( queryPolicy , mcpOverrides ) ;
7055- const compiledSelection = compileClaudeModelSelection ( turnInput . modelSelection ) ;
7077+ const compiledSelection = compileClaudeModelSelection (
7078+ turnInput . modelSelection ,
7079+ modelCatalog ,
7080+ ) ;
70567081 const resumeSessionAt = yield * getNativeConversationHeadId ( turnInput . providerThread ) ;
70577082 const existing = yield * Ref . get ( queryContext ) ;
70587083 // A continuation prompts nothing: it drains output the live process
@@ -7122,6 +7147,7 @@ export function makeClaudeAdapterV2(
71227147 resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn ;
71237148 const queryOptions = makeClaudeQueryOptions ( {
71247149 modelSelection : turnInput . modelSelection ,
7150+ modelCatalog,
71257151 nativeThreadId,
71267152 resume : shouldResume ,
71277153 ...( resumeSessionAt === undefined ? { } : { resumeSessionAt } ) ,
@@ -7190,6 +7216,11 @@ export function makeClaudeAdapterV2(
71907216 query : querySession ,
71917217 queryPolicyKey,
71927218 selectionKey : compiledSelection . queryIdentity ,
7219+ contextWindow : claudeContextWindow (
7220+ modelCatalog ,
7221+ turnInput . modelSelection ,
7222+ compiledSelection . apiModelId ,
7223+ ) ,
71937224 closed,
71947225 promptEchoMode : "unknown" ,
71957226 openedPermissionMode : queryOptions . permissionMode ,
@@ -7247,6 +7278,11 @@ export function makeClaudeAdapterV2(
72477278 function * ( turnInput : ProviderAdapter . ProviderAdapterV2TurnInput ) {
72487279 const startedAt = yield * DateTime . now ;
72497280 const nativeThreadId = yield * getNativeThreadId ( turnInput . providerThread ) ;
7281+ const modelCatalog = yield * scopedModelCatalog ;
7282+ const promptEffort = compileClaudeModelSelection (
7283+ turnInput . modelSelection ,
7284+ modelCatalog ,
7285+ ) . promptEffort ;
72507286 const nativeTurnId = `turn:${ turnInput . attemptId } ` ;
72517287 const promptUuid = isClaudeProviderContinuationTurn ( turnInput )
72527288 ? null
@@ -7279,6 +7315,7 @@ export function makeClaudeAdapterV2(
72797315 }
72807316 const context : ActiveClaudeTurnContext = {
72817317 input : turnInput ,
7318+ promptEffort,
72827319 nativeTurnId,
72837320 nativeMessageCursor : null ,
72847321 providerTurnId,
@@ -7322,17 +7359,14 @@ export function makeClaudeAdapterV2(
73227359 promptUuid === null
73237360 ? null
73247361 : yield * makeClaudeUserMessageWithAttachments ( {
7325- text : applyClaudePromptEffortPrefix (
7326- turnInput . message . text ,
7327- compileClaudeModelSelection ( turnInput . modelSelection ) . promptEffort ,
7328- ) ,
7362+ text : applyClaudePromptEffortPrefix ( turnInput . message . text , promptEffort ) ,
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" ,
@@ -7531,10 +7565,7 @@ export function makeClaudeAdapterV2(
75317565 } ) ;
75327566 }
75337567 const userMessage = yield * makeClaudeUserMessageWithAttachments ( {
7534- text : applyClaudePromptEffortPrefix (
7535- turnInput . message . text ,
7536- compileClaudeModelSelection ( currentTurn . input . modelSelection ) . promptEffort ,
7537- ) ,
7568+ text : applyClaudePromptEffortPrefix ( turnInput . message . text , currentTurn . promptEffort ) ,
75387569 attachments : turnInput . message . attachments ,
75397570 priority : "now" ,
75407571 attachmentsDir,
@@ -7610,7 +7641,7 @@ export function makeClaudeAdapterV2(
76107641 providerSessionId : input . providerSessionId ,
76117642 providerSession : session ,
76127643 getModelContextWindow : ( selection ) =>
7613- resolveClaudeCatalogContextWindowTokens ( BUNDLED_CLAUDE_MODEL_CATALOG , selection ) ,
7644+ resolveClaudeCatalogContextWindowTokens ( Effect . runSync ( scopedModelCatalog ) , selection ) ,
76147645 events : Stream . fromEffectRepeat ( Queue . take ( events ) ) ,
76157646 hasPendingBackgroundWork : Effect . gen ( function * ( ) {
76167647 // Session capability: any native thread with pending work pins idle.
@@ -7924,7 +7955,7 @@ export type ClaudeAdapterV2DriverEnv =
79247955export const createClaudeAdapterV2 = Effect . fn ( "ClaudeAdapterV2Driver.create" ) (
79257956 function * (
79267957 input : ProviderAdapterDriverCreateInput < ClaudeSettings > ,
7927- hooks : Pick < ClaudeAdapterV2Options , "scopedLimitNames" | "onUsageLimits" > = { } ,
7958+ hooks : Pick < ClaudeAdapterV2Options , "modelCatalog" | " scopedLimitNames" | "onUsageLimits" > = { } ,
79287959 ) {
79297960 const { instanceId, environment, enabled, config } = input ;
79307961 const fileSystem = yield * FileSystem . FileSystem ;
0 commit comments