@@ -44,6 +44,12 @@ import {
4444 DEFAULT_REASONING_ONLY_RETRY_LIMIT ,
4545} from "./run/incomplete-turn.js" ;
4646import { measureEmbeddedAgentPreparation } from "./run/preparation-timing.js" ;
47+ import {
48+ beginRunAttempt ,
49+ createRunRetryBudget ,
50+ isRunRetryBudgetExhausted ,
51+ recordRunRetry ,
52+ } from "./run/retry-budget.js" ;
4753import { handleRetryLimitExhaustion } from "./run/retry-limit.js" ;
4854import { prepareEmbeddedRunRuntime } from "./run/runtime-preparation.js" ;
4955import { createEmbeddedRunSessionPromptState } from "./run/session-prompt-state.js" ;
@@ -174,14 +180,14 @@ export async function runPreparedEmbeddedLoop(
174180 const maxReasoningOnlyRetryAttempts = DEFAULT_REASONING_ONLY_RETRY_LIMIT ;
175181 const maxEmptyResponseRetryAttempts = DEFAULT_EMPTY_RESPONSE_RETRY_LIMIT ;
176182
177- const MAX_RUN_LOOP_ITERATIONS = resolveMaxRunRetryIterations ( profileCandidates . length ) ;
183+ const MAX_RUN_RETRY_ATTEMPTS = resolveMaxRunRetryIterations ( profileCandidates . length ) ;
184+ const runRetryBudget = createRunRetryBudget ( MAX_RUN_RETRY_ATTEMPTS ) ;
178185 const contextRecoveryState = createEmbeddedRunContextRecoveryState ( ) ;
179186 let bootstrapPromptWarningSignaturesSeen =
180187 params . bootstrapPromptWarningSignaturesSeen ??
181188 ( params . bootstrapPromptWarningSignature ? [ params . bootstrapPromptWarningSignature ] : [ ] ) ;
182189 const usageAccumulator = createUsageAccumulator ( ) ;
183190 let lastRunPromptUsage : ReturnType < typeof normalizeUsage > | undefined ;
184- let runLoopIterations = 0 ;
185191 let overloadProfileRotations = 0 ;
186192 const terminalRetryState = createEmbeddedRunTerminalRetryState ( ) ;
187193 let sameModelIdleTimeoutRetries = 0 ;
@@ -285,14 +291,14 @@ export async function runPreparedEmbeddedLoop(
285291 let latestMcpAppChannelView : McpAppChannelView | undefined ;
286292 while ( true ) {
287293 refreshPreparedRuntimeSnapshot ( ) ;
288- if ( runLoopIterations >= MAX_RUN_LOOP_ITERATIONS ) {
294+ if ( isRunRetryBudgetExhausted ( runRetryBudget ) ) {
289295 const message =
290- `Exceeded retry limit after ${ runLoopIterations } attempts ` +
291- `(max=${ MAX_RUN_LOOP_ITERATIONS } ).` ;
296+ `Exceeded retry limit after ${ runRetryBudget . attemptsDispatched } attempts ` +
297+ `(counted attempts= ${ runRetryBudget . attemptsCounted } , max=${ runRetryBudget . maxAttempts } ).` ;
292298 log . error (
293299 `[run-retry-limit] sessionKey=${ params . sessionKey ?? params . sessionId } ` +
294- `provider=${ provider } /${ modelId } attempts=${ runLoopIterations } ` +
295- `maxAttempts=${ MAX_RUN_LOOP_ITERATIONS } ` ,
300+ `provider=${ provider } /${ modelId } attempts=${ runRetryBudget . attemptsDispatched } ` +
301+ `countedAttempts= ${ runRetryBudget . attemptsCounted } maxAttempts=${ runRetryBudget . maxAttempts } ` ,
296302 ) ;
297303 const retryLimitDecision = resolveRunFailoverDecision ( {
298304 stage : "retry_limit" ,
@@ -319,14 +325,14 @@ export async function runPreparedEmbeddedLoop(
319325 livenessState : "blocked" ,
320326 } ) ;
321327 }
322- runLoopIterations += 1 ;
328+ beginRunAttempt ( runRetryBudget ) ;
323329 const runtimeAuthRetry : boolean = authRetryPending ;
324330 authRetryPending = false ;
325331 attemptedThinking . add ( thinkLevel ) ;
326332 const codexAppServerRecoveryRetryAvailable = hasCodexAppServerRecoveryRetryBudget ( {
327333 alreadyRetried : codexAppServerRecoveryRetries > 0 ,
328- runLoopIterations,
329- maxRunLoopIterations : MAX_RUN_LOOP_ITERATIONS ,
334+ runLoopIterations : runRetryBudget . attemptsCounted ,
335+ maxRunLoopIterations : runRetryBudget . maxAttempts ,
330336 } ) ;
331337 const dispatch = await prepareAndDispatchEmbeddedRunAttempt ( {
332338 runInput : input ,
@@ -382,6 +388,7 @@ export async function runPreparedEmbeddedLoop(
382388 normalizedAttempt . bootstrapPromptWarningSignaturesSeen ;
383389 lastRunPromptUsage = normalizedAttempt . lastRunPromptUsage ;
384390 accumulatedReplayState = normalizedAttempt . replayState ;
391+ recordRunRetry ( runRetryBudget , normalizedAttempt . retryKind ) ;
385392 continue ;
386393 }
387394 bootstrapPromptWarningSignaturesSeen = normalizedAttempt . bootstrapPromptWarningSignaturesSeen ;
0 commit comments