refactor(agent): consolidate inner-retry-loop recovery flags into TurnRetryState (god-file Phase 1b)#41828
Merged
Merged
Conversation
…nRetryState (god-file Phase 1b) run_conversation's inner retry loop tracked recovery state in ~15 scattered bare booleans (per-provider OAuth refresh guards, format-recovery guards, restart signals). They are now fields on a single TurnRetryState dataclass the loop mutates in place (_retry.<flag>), giving the recovery bookkeeping a named, testable home. Loop-control vars (retry_count, max_retries, max_compression_attempts) stay as plain locals — they're while-mechanics, not recovery bookkeeping. Behavior-neutral: pure local→attribute rewrite of 42 references; kwarg NAMES preserved (e.g. has_retried_429=_retry.has_retried_429). Live simple + tool turns OK. Validation: tests/run_agent/ 1615 passed / 0 failed under per-file process isolation; new test_turn_retry_state.py pins the field contract.
Contributor
🔎 Lint report:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
run_conversation's inner retry loop no longer tracks recovery state in ~15 scattered bare booleans — they're now fields on a singleTurnRetryStatedataclass.Phase 1b of the god-file decomposition plan. The inner
while retry_count < max_retriesloop (~2,400 LOC) guards each recovery branch (per-provider OAuth refresh, format recovery, compression/length-continuation restart) with a one-shot boolean. They were inline locals threaded through the whole loop body; consolidating them gives the recovery bookkeeping a named, testable home.Changes
agent/turn_retry_state.py(new):TurnRetryStatedataclass — 15 recovery guards + restart signals, all defaultingFalse. Dependency-free.agent/conversation_loop.py: the 15-line inline flag-init block becomes_retry = TurnRetryState(); 42 references rewrittenflag→_retry.flag. Kwarg names preserved (e.g.has_retried_429=_retry.has_retried_429).retry_count,max_retries,max_compression_attempts) intentionally stay as locals — they'rewhile-mechanics, not recovery bookkeeping.tests/agent/test_turn_retry_state.py(new): pins the field contract + default semantics.Validation
run_conversationTurnRetryStateobjecttests/run_agent/(per-file process isolation)Behavior-neutral: pure local→attribute rewrite, no control-flow change. The recovery-branch tests (
test_multimodal_tool_content_recovery,test_jsondecodeerror_retryable,test_31273_402_not_retried, etc.) all pass, exercising the flags via_retry.<flag>.Note on scope
The deeper extraction of each recovery branch into a standalone handler was evaluated and deliberately not done here: those branches
continue/break/returnthe loop and mutateagent+ a dozen locals, so factoring them out cleanly needs a larger redesign of the live failover logic. This PR does the safe, valuable enabler (flag consolidation) without touching the error-recovery control flow.Infographic