Skip to content

fix(agent): retry failed background summaries and track consecutive retry streaks - #7207

Merged
M4n5ter merged 2 commits into
mainfrom
fix/compaction-retry
Nov 23, 2026
Merged

M4n5ter merged 2 commits into
mainfrom
fix/compaction-retry

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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_level set has_proactive_summary from accepts_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_for counted 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 on ContinueFromHistory recovery steps and re-grants the one-time Fallback transition bonus on every later Step), RunView now tracks an explicit persisted RetryStreak state machine:

  • Regular { charged } counts consecutive retries since Run start or the last successful foreground model closure. Only the first foreground ModelClosed(Success, diagnostic: None) resets it (never background proactive_summary closures or new recovery Steps).
  • FallbackTransitionPending { attempt } records the one-time Primary-to-Fallback transition right on ModelTransportSelected, granting a single transition retry for that attempt even when budget.retries == 0 and surviving restarts between ModelTransportSelected and RetryCharged.
  • AfterFallbackTransition { charged } bounds subsequent Fallback retries to budget.retries with continuing exponential backoff until a foreground closure succeeds; later Steps on Fallback get no extra +1 transition retry.

Persisted state

Adds forward migration 0053_retry_streak.sql (sealed in scripts/rust/migrations.lock.json), adding agent_runs.retry_streak TEXT NOT NULL DEFAULT '{"kind":"regular","charged":0}' with an atomic Migration::with_data backfill (sql::migrate_retry_streak) that replays agent_facts in revision order for any Run with retries > 0 OR retry_phase = 'fallback'.

  • Existing Runs migrate in place cleanly inside the migration SAVEPOINT, matching cold rebuild (RunView::apply).
  • ModelTransportSelected keeps the Run-wide self.retries >= self.spec.budget.retries fold check so historical Runs that fell back after scattered retries still validate during cold rebuild.
  • products/maka/cli/tests/upgrade_compat.rs verifies upgraded agent_runs.retry_streak payloads alongside SQLite integrity and monotonic migrations.

Not changed

  • Separate first-frame deadline. Left the 300s idle timeout untouched to avoid falsely timing out slow prefill / long reasoning requests.
  • UI status for "waiting for first frame", "retry k" or "foreground compaction". Left for a separate change.

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_success
    • reconcile::tests::fallback_transition_retry_is_granted_once_even_with_zero_budget
    • reconcile::tests::historical_run_wide_fallback_folds_after_earlier_step_succeeded
    • sql::tests::migration_53_backfills_retry_streak_matching_fact_replay
    • migration_tests::composed_product_migrations_initialize_upgrade_and_reopen
  • cargo 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) and compaction 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:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

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 ContinueFromHistory infinite retry loop and Fallback +1 transition leak, and Maka (Gemini) implemented the persisted RetryStreak state machine, migration 0053 backfill, and updated tests. Commits carry Generated-by trailers.

Checklist

  • Tests cover the change and fail without it
  • Format, typecheck and the affected suites pass locally

Does this PR entail a breaking change or persisted-data migration?

  • Yes — in-place migration 0053_retry_streak.sql + atomic with_data backfill and upgrade_compat.rs coverage described under Persisted state above
  • No

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

… 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)
@github-actions github-actions Bot added the effort/M Under 500 readable lines label Nov 23, 2026

@jackwener jackwener left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jackwener left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
@M4n5ter
M4n5ter force-pushed the fix/compaction-retry branch from e83f8af to 15fe36a Compare November 23, 2026 11:37
@M4n5ter M4n5ter changed the title fix(agent): retry failed background summaries and scope retry budgets to a Step fix(agent): retry failed background summaries and track consecutive retry streaks Nov 23, 2026
@M4n5ter
M4n5ter merged commit 73f2333 into main Nov 23, 2026
13 checks passed
@M4n5ter
M4n5ter deleted the fix/compaction-retry branch November 23, 2026 13:11
M4n5ter pushed a commit that referenced this pull request Nov 23, 2026
Rebased onto main (bumped migration 0053 -> 0054 after #7207).

Generated-by: Maka (ChatGPT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/M Under 500 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants