@@ -1495,6 +1495,7 @@ async function runEmbeddedAgentInternal(
14951495 agentHarness = selectHarnessForModel ( effectiveModel ) ;
14961496 pluginHarnessOwnsTransport = agentHarness . id !== "openclaw" ;
14971497
1498+ const authStages = log . isEnabled ( "trace" ) ? createEmbeddedRunStageTracker ( ) : undefined ;
14981499 const usesOpenAIAuthRouting = provider === OPENAI_PROVIDER_ID ;
14991500 const openClawNativeCodexResponsesNeedsAuthBootstrap =
15001501 ! pluginHarnessOwnsTransport &&
@@ -1532,6 +1533,7 @@ async function runEmbeddedAgentInternal(
15321533 params . authProfileIdSource === "user" ? params . authProfileId : undefined ,
15331534 } ) ;
15341535 }
1536+ authStages ?. mark ( "scope" ) ;
15351537 const attemptAuthProfileStore = usesOpenAIAuthRouting
15361538 ? ensureAuthProfileStore ( agentDir , {
15371539 externalCliProviderIds : [ OPENAI_PROVIDER_ID ] ,
@@ -1550,6 +1552,7 @@ async function runEmbeddedAgentInternal(
15501552 ensureAuthProfileStoreWithoutExternalProfiles ( agentDir , {
15511553 allowKeychainPrompt : false ,
15521554 } ) ) ;
1555+ authStages ?. mark ( "store" ) ;
15531556 const requestedProfileId = params . authProfileId ?. trim ( ) || undefined ;
15541557 const lockedProfileId =
15551558 params . authProfileIdSource === "user" ? requestedProfileId : undefined ;
@@ -1586,20 +1589,29 @@ async function runEmbeddedAgentInternal(
15861589 } ) ,
15871590 } ) ;
15881591
1589- const materializeAuthPlan = async ( plan : AgentRuntimeAuthPlan ) => {
1592+ const materializedRouteModels = new WeakMap <
1593+ AgentRuntimeAuthPlan ,
1594+ Promise < typeof runtimeModel >
1595+ > ( ) ;
1596+ const materializeAuthPlanUncached = async ( plan : AgentRuntimeAuthPlan ) => {
15901597 // Native harness sessions own their model tuple. Route preparation may
15911598 // attest auth/transport, but must not rediscover or replace that model.
15921599 if ( nativeModelOwned ) {
15931600 return runtimeModel ;
15941601 }
1602+ const requiresCredentialScopedResolve = Boolean (
1603+ plan . modelRoute && ( plan . forwardedAuthProfileId || params . authProfileId ) ,
1604+ ) ;
15951605 return (
15961606 ( await materializePreparedRuntimeModel ( {
15971607 plan,
15981608 provider,
15991609 modelId,
16001610 config : params . config ,
16011611 model : runtimeModel ,
1602- forceResolve : Boolean ( plan . modelRoute ) ,
1612+ // Unscoped direct auth cannot change credential-scoped metadata.
1613+ // Reuse an already matching tuple; route mismatches still resolve.
1614+ forceResolve : requiresCredentialScopedResolve ,
16031615 resolveModel : ( { config, authProfileId, authProfileMode } ) =>
16041616 resolveModelAsync ( provider , modelId , agentDir , config , {
16051617 authStorage,
@@ -1614,10 +1626,25 @@ async function runEmbeddedAgentInternal(
16141626 } ) ) ?? runtimeModel
16151627 ) ;
16161628 } ;
1629+ const materializeAuthPlan = ( plan : AgentRuntimeAuthPlan ) => {
1630+ if ( ! plan . modelRoute ) {
1631+ return materializeAuthPlanUncached ( plan ) ;
1632+ }
1633+ const cached = materializedRouteModels . get ( plan ) ;
1634+ if ( cached ) {
1635+ return cached ;
1636+ }
1637+ // Prepared plans are immutable within one run. Carry their exact model
1638+ // tuple into auth initialization instead of repeating provider discovery.
1639+ const materialized = materializeAuthPlanUncached ( plan ) ;
1640+ materializedRouteModels . set ( plan , materialized ) ;
1641+ return materialized ;
1642+ } ;
16171643 let resolvedAuthPreparation = createAuthPreparation ( ) ;
16181644 let preparedAuthAttempts = resolvedAuthPreparation . attempts ;
16191645 let activePreparedAuthPlan = resolvedAuthPreparation . plan ;
16201646 applyResolvedRuntimeModel ( await materializeAuthPlan ( activePreparedAuthPlan ) ) ;
1647+ authStages ?. mark ( "prepare-plan" ) ;
16211648
16221649 const finalizedHarness = selectHarnessForPreparedAttempts (
16231650 effectiveModel ,
@@ -1640,6 +1667,7 @@ async function runEmbeddedAgentInternal(
16401667 ) ;
16411668 }
16421669 }
1670+ authStages ?. mark ( "harness" ) ;
16431671 // A selected plugin harness owns context pressure with its native transcript,
16441672 // even if it cannot expose manual compaction. Generic recovery is OpenClaw-only.
16451673 const genericCompactionRecoveryAllowed = ! pluginHarnessOwnsTransport ;
@@ -1851,6 +1879,7 @@ async function runEmbeddedAgentInternal(
18511879 } ,
18521880 log,
18531881 } ) ;
1882+ authStages ?. mark ( "controller" ) ;
18541883 const advancePluginHarnessAuthAttempt = async ( ) : Promise < boolean > => {
18551884 if ( ! pluginHarnessOwnsTransport || lockedProfileId ) {
18561885 return false ;
@@ -1951,6 +1980,15 @@ async function runEmbeddedAgentInternal(
19511980 lastProfileId = forwardedPluginHarnessProfileId ;
19521981 }
19531982 }
1983+ authStages ?. mark ( "initialize" ) ;
1984+ if ( authStages ) {
1985+ log . trace (
1986+ formatEmbeddedRunStageSummary (
1987+ `[trace:embedded-run] auth stages: runId=${ params . runId } sessionId=${ params . sessionId } phase=auth` ,
1988+ authStages . snapshot ( ) ,
1989+ ) ,
1990+ ) ;
1991+ }
19541992 startupStages . mark ( "auth" ) ;
19551993 notifyExecutionPhase ( "auth" , { provider, model : modelId } ) ;
19561994 const resolveRunAttemptAuthProfileStore = ( ) : AuthProfileStore => {
0 commit comments