Repository navigation
fix(agent): retry failed background summaries and track consecutive retry streaks - #7207
Conversation
… to a Step A background summary that failed (for example on a stream idle timeout) kept counting as present for its checkpoint, so no later Step prepared another one. Once the context reached the compaction threshold the Run fell back to a blocking foreground Summary. A failed or rejected summary now leaves the compaction level unset, and the next Step prepares a new one. Preparation is still decided only when a Step has no attempt yet, so at most one background summary starts per Step. Model retries were counted across the whole Run: every recovered failure grew the backoff exponent (up to 64s) and consumed the 9-retry budget, eventually forcing fallback or failing long Runs whose individual failures had all recovered. The budget and backoff now count retries of the current Step only. The fallback selection check uses the Run-wide retry count, which an exhausted Step budget implies and which recorded Runs still satisfy during rebuild. Generated-by: Maka (Claude Opus 5.5)
jackwener
left a comment
There was a problem hiding this comment.
I found no P0–P2 issues in 6ec0aff673d362962908afecfa07b9701a2dfae5. It merges cleanly with current main 19e5caf512ebce68cbce8907079a88bbfea8ffca; the materialized merge tree is fa82336166fe00fba295efddd80daf1a16b00923.
A failed background summary no longer suppresses preparation on a later Step after its cleanup completes. Pending and usable summaries retain their existing behavior. Retry counting and backoff now use the current Step and transport phase; all charged attempts remain available in the frontier projection, so pruning settled history does not bypass the same-Step limit. The Run-wide counter remains available for validating previously recorded transport fallback decisions. No persisted schema or fact format changes were introduced.
I independently verified both regressions: the new tests pass with this implementation and fail after restoring the corresponding production code to main. The background-summary test times out with the old compaction code; the retry-budget test rejects a later Step after an earlier Step consumed its budget. Additional transition probes passed for exhausting the same-Step budget and replaying a legacy fallback decision after retries on earlier Steps.
All 72 State and 34 Engine tests passed, including on the current-main merge. All 221 native tests have passing results: 213 passed in the first full invocation, and eight resource-dependent tests passed after correcting my direct test invocation to use Cargo's crate working directory. Formatting and diff checks passed. I have not independently reproduced the incident's reported 8.6-minute wait or run the full product upgrade harness. Hosted CI is still running or queued.
Automated review notice: This comment was posted by an automated review agent operated by jackwener. It is not an independent human review and does not replace one.
jackwener
left a comment
There was a problem hiding this comment.
I approve 6ec0aff673d362962908afecfa07b9701a2dfae5 at M4n5ter's request. My review and verification results found no P0–P2 issues. The head is unchanged and still merges cleanly with main 19e5caf512ebce68cbce8907079a88bbfea8ffca, producing the previously tested merge tree fa82336166fe00fba295efddd80daf1a16b00923.
The hosted tooling check has succeeded; root-build, linux-checks, native-macos, and native-windows are still running. This approval does not establish that all required checks have passed. The previously stated validation limits remain applicable.
Automated review notice: This comment was posted by an automated review agent operated by jackwener. It is not an independent human review and does not replace one.
… by StepId
Scoping retries by StepId broke two invariants:
- AttemptRecovery::ContinueFromHistory prepares a fresh StepId on every
retry, resetting the per-step retry count and backoff to 250ms forever
under repeated truncated streams.
- Because view.retry_phase stays Fallback for the rest of the Run, adding
+1 whenever phase == Fallback re-granted the one-time Primary->Fallback
transition retry on every subsequent Step.
Replace retries_for(step, phase) with an explicit persisted RetryStreak
state machine on RunView / agent_runs (migration 0053 with an atomic
with_data backfill from agent_facts):
- Regular { charged } counts consecutive retries and resets only on the
first foreground ModelClosed(Success, diagnostic: None) (never on
background proactive_summary closures or new recovery Steps).
- FallbackTransitionPending { attempt } grants the one-time transition
retry for the selected attempt even when budget.retries == 0, surviving
restarts between ModelTransportSelected and RetryCharged.
- AfterFallbackTransition { charged } bounds subsequent Fallback retries
to budget.retries with continuing backoff until foreground success.
Also replace the step-scoped reconcile test with behavioral coverage for
ContinueFromHistory budget/backoff/reset, single Fallback transition with
zero budget, historical Run-wide Fallback cold fold, migration 0053
backfill parity, and upgrade_compat retry_streak validation; trim brittle
assertions from the background summary retry test.
Generated-by: Maka (Gemini)
e83f8af to
15fe36a
Compare
Rebased onto main (bumped migration 0053 -> 0054 after #7207). Generated-by: Maka (ChatGPT)
Summary
A production session stalled for many minutes after a provider hiccup. Two Engine/State issues turned recoverable upstream failures into long foreground waits.
A failed background summary blocked every later one.
model_levelsethas_proactive_summaryfromaccepts_background_summary, which only compares checkpoint and options. A proactive summary that ended in a stream idle timeout therefore kept counting as present. No later Step in that checkpoint prepared another one, even though the fold already allows it. When the context reached the threshold, the Run fell back to a blocking foreground Summary: 8.6 minutes in the incident, during which steering is not polled.The flag is removed. A summary now sets the compaction level only while it is in flight or usable, so a failed or rejected one leaves the level unset and the next Step prepares a new one. Preparation is still decided only when a Step has no attempt yet, so even under persistent upstream failure at most one background summary starts per Step.
Retry budget and backoff were counted across the whole Run. Previously
retries_forcounted every charged retry in the Run, so recovered failures kept growing the backoff exponent (up to 64s) and draining the retry budget on long Runs.Instead of scoping retries by
StepId(which resets onContinueFromHistoryrecovery steps and re-grants the one-timeFallbacktransition bonus on every later Step),RunViewnow tracks an explicit persistedRetryStreakstate machine:Regular { charged }counts consecutive retries since Run start or the last successful foreground model closure. Only the first foregroundModelClosed(Success, diagnostic: None)resets it (never backgroundproactive_summaryclosures or new recovery Steps).FallbackTransitionPending { attempt }records the one-time Primary-to-Fallback transition right onModelTransportSelected, granting a single transition retry for that attempt even whenbudget.retries == 0and surviving restarts betweenModelTransportSelectedandRetryCharged.AfterFallbackTransition { charged }bounds subsequent Fallback retries tobudget.retrieswith continuing exponential backoff until a foreground closure succeeds; later Steps on Fallback get no extra+1transition retry.Persisted state
Adds forward migration
0053_retry_streak.sql(sealed inscripts/rust/migrations.lock.json), addingagent_runs.retry_streak TEXT NOT NULL DEFAULT '{"kind":"regular","charged":0}'with an atomicMigration::with_databackfill (sql::migrate_retry_streak) that replaysagent_factsin revision order for any Run withretries > 0 OR retry_phase = 'fallback'.SAVEPOINT, matching coldrebuild(RunView::apply).ModelTransportSelectedkeeps the Run-wideself.retries >= self.spec.budget.retriesfold check so historical Runs that fell back after scattered retries still validate during cold rebuild.products/maka/cli/tests/upgrade_compat.rsverifies upgradedagent_runs.retry_streakpayloads alongside SQLite integrity and monotonic migrations.Not changed
Verification
cargo test -p maka-agent-state -p maka-agent-engine: pass (73 + 1 + 36 tests), including:reconcile::tests::retry_streak_persists_across_continue_from_history_and_resets_after_foreground_successreconcile::tests::fallback_transition_retry_is_granted_once_even_with_zero_budgetreconcile::tests::historical_run_wide_fallback_folds_after_earlier_step_succeededsql::tests::migration_53_backfills_retry_streak_matching_fact_replaymigration_tests::composed_product_migrations_initialize_upgrade_and_reopencargo test -p maka-agent-native --lib -- failed_background_summary_is_prepared_again_on_the_next_step continue_from_history_enforces_retry_budget_and_resets_after_success fallback retry(9 passed) andcompaction transport preset_acceptance(43 passed).cargo test -p maka-cli --test upgrade_compat: pass (upgrade_compat_capsule_roundtrip).node scripts/ci.mjs check migrations: pass (57 locked, 0 pending).cargo clippy -p maka-agent-state -p maka-agent-engine -p maka-agent-native --all-targets -- -D warnings: clean.cargo fmt -p maka-agent-state -p maka-agent-engine -p maka-agent-native -p maka-cli -- --check: clean.AI use
Select exactly one:
Tool(s) and scope: Maka (Claude Opus 5.5) analyzed the incident and drafted the initial compaction/retry fix; Maka (Gemini) and Magpie (GPT-6.2-Astra) reviewed and caught the
ContinueFromHistoryinfinite retry loop andFallback+1transition leak, and Maka (Gemini) implemented the persistedRetryStreakstate machine, migration 0053 backfill, and updated tests. Commits carryGenerated-bytrailers.Checklist
Does this PR entail a breaking change or persisted-data migration?
0053_retry_streak.sql+ atomicwith_databackfill andupgrade_compat.rscoverage described under Persisted state aboveDoes this PR entail a change in behavior?