@@ -114,6 +114,7 @@ import { createEmbeddedRunReplayState, observeReplayMetadata } from "./replay-st
114114import { handleAssistantFailover } from "./run/assistant-failover.js" ;
115115import {
116116 createEmbeddedRunStageTracker ,
117+ EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE ,
117118 formatEmbeddedRunStageSummary ,
118119 shouldWarnEmbeddedRunStageSummary ,
119120} from "./run/attempt-stage-timing.js" ;
@@ -194,6 +195,16 @@ const COMPACTION_CONTINUATION_RETRY_INSTRUCTION =
194195 "The previous attempt compacted the conversation context before producing a final user-visible answer. Continue from the compacted transcript and produce the final answer now. Do not restart from scratch, do not repeat completed work, and do not rerun tools unless the transcript clearly lacks required evidence." ;
195196type EmbeddedRunAttemptForRunner = Awaited < ReturnType < typeof runEmbeddedAttemptWithBackend > > ;
196197
198+ function resolveAttemptDispatchApiKey ( params : {
199+ apiKeyInfo : ApiKeyInfo | null ;
200+ runtimeAuthState : RuntimeAuthState | null ;
201+ } ) : string | undefined {
202+ if ( params . runtimeAuthState ) {
203+ return undefined ;
204+ }
205+ return params . apiKeyInfo ?. apiKey ;
206+ }
207+
197208function resolveEmbeddedRunLaneTimeoutMs ( timeoutMs : number ) : number | undefined {
198209 if ( ! Number . isFinite ( timeoutMs ) || timeoutMs <= 0 ) {
199210 return undefined ;
@@ -1271,6 +1282,9 @@ export async function runEmbeddedPiAgent(
12711282 authRetryPending = false ;
12721283 attemptedThinking . add ( thinkLevel ) ;
12731284 await fs . mkdir ( resolvedWorkspace , { recursive : true } ) ;
1285+ if ( ! startupStagesEmitted ) {
1286+ startupStages . mark ( EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE . workspace ) ;
1287+ }
12741288
12751289 const basePrompt =
12761290 nextAttemptPromptOverride ??
@@ -1289,9 +1303,12 @@ export async function runEmbeddedPiAgent(
12891303 promptAdditions . length > 0
12901304 ? `${ basePrompt } \n\n${ promptAdditions . join ( "\n\n" ) } `
12911305 : basePrompt ;
1292- let resolvedStreamApiKey : string | undefined ;
1293- if ( ! runtimeAuthState && apiKeyInfo ) {
1294- resolvedStreamApiKey = ( apiKeyInfo as ApiKeyInfo ) . apiKey ;
1306+ const resolvedStreamApiKey = resolveAttemptDispatchApiKey ( {
1307+ apiKeyInfo,
1308+ runtimeAuthState,
1309+ } ) ;
1310+ if ( ! startupStagesEmitted ) {
1311+ startupStages . mark ( EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE . prompt ) ;
12951312 }
12961313 const runtimePlan = buildAgentRuntimePlan ( {
12971314 provider,
@@ -1323,9 +1340,10 @@ export async function runEmbeddedPiAgent(
13231340 } ,
13241341 } ) ;
13251342 if ( ! startupStagesEmitted ) {
1326- startupStages . mark ( "attempt-dispatch" ) ;
1343+ startupStages . mark ( EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE . runtimePlan ) ;
1344+ startupStages . mark ( EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE . dispatch ) ;
13271345 notifyExecutionPhase ( "attempt_dispatch" , { provider, model : modelId } ) ;
1328- emitStartupStageSummary ( "attempt- dispatch" ) ;
1346+ emitStartupStageSummary ( EMBEDDED_RUN_ATTEMPT_DISPATCH_STAGE . dispatch ) ;
13291347 startupStagesEmitted = true ;
13301348 }
13311349
0 commit comments