Skip to content

feat(reliability): retry triggering execution after SUB-003 auto-switch (#792) - #1364

Merged
vybe merged 1 commit into
devfrom
AndriiPasternak31/issue-792
Jun 27, 2026
Merged

vybe merged 1 commit into
devfrom
AndriiPasternak31/issue-792

Conversation

@AndriiPasternak31

Copy link
Copy Markdown
Contributor

Summary

Extends SUB-003 subscription auto-switch so that when a switch fires
mid-execution, the triggering execution is retried once and lands SUCCESS
instead of FAILED.

Today a switch mid-turn marks the row FAILED. Three recovery patterns already
exist — interactive chat retries client-side (routers/chat.py) and recurring
cron recovers on the next tick — but one-shot triggers had no recovery:

  • manual POST …/schedules/{id}/trigger
  • webhook
  • MCP trigger_agent_schedule

These produced a single dead FAILED execution. This PR closes that gap.

How it works

TaskExecutionService.execute_task now intercepts a returned 429/auth response
pre-raise_for_status — agent_post_with_retry returns the response
rather than raising, so the interception sits adjacent to the #678 reader-race
502 block and falls through to the single shared success-parse path by
reassigning response (no duplicated finalize logic). When SUB-003 reports
switched=True, it re-issues the turn once with the same execution_id.

New module helpers:

  • classify_switch_failure(response) — full SUB-003 surface (429→rate_limit;
    503/401/403/402 or is_auth_failure body→auth; else None), so "any
    switch-success retries" actually holds, not just status codes.
  • _extract_agent_error(response, fallback) — shared body→(msg, metadata)
    extractor, also adopted by the except httpx.HTTPError handler so both paths
    read the body identically.
  • _salvage_attempt_cost(metadata) — rolls first-attempt cost into
    previous_attempt_cost (Async chat_with_agent: long execution silently fails with null response (reader-thread) #678 R2).

Key decisions:

Out of scope / follow-ups

Testing

tests/unit/test_792_subscription_retry.py — 11 cases (incl. parametrized
503/401/403/402 auth surface, #678↔#792 interplay, cascade single-switch,
bounded retry timeout). All green:

11 passed in 0.12s

Docs

  • requirements.md §20 + docs/requirements/SUB-003-subscription-auto-switch.md
    (acceptance criteria + history)
  • architecture.md service catalog + feature-flows/task-execution-service.md
    • feature-flows index row
  • /cso --diff report committed under docs/security-reports/ — CLEAN, no
    findings above the gate.

Closes #792

🤖 Generated with Claude Code

…ch (#792)

When a subscription auto-switch fires mid-execution the triggering row was
marked FAILED. Interactive chat retries client-side and recurring cron recovers
on the next tick, but one-shot triggers (manual .../schedules/{id}/trigger,
webhook, MCP trigger_agent_schedule) had no recovery.

TaskExecutionService.execute_task now intercepts a returned 429/auth response
pre-raise_for_status (mirroring the #678 reader-race retry, since
agent_post_with_retry returns rather than raises) and, when SUB-003 reports a
successful switch, re-issues the turn once with the same execution_id so the row
lands SUCCESS instead of FAILED.

- classify_switch_failure(): full switch surface (429 -> rate_limit;
  503/401/403/402 or is_auth_failure body -> auth), not just status codes.
- _extract_agent_error(): shared body->( msg, metadata) extractor, also used by
  the except httpx.HTTPError handler so both read the body identically.
- _salvage_attempt_cost(): rolls first-attempt cost into previous_attempt_cost
  (#678 R2).
- Dedicated subscription_switch_attempted one-shot guard (NOT retry_count, which
  #678 owns) that also gates the except-handler switch so a cascade never
  double-switches.
- Retry is the readiness probe (short _SWITCH_RETRY_DELAY_S pre-delay only, no
  circuit-aware /health poll, no restart_result trust); retry timeout capped to
  the remaining original budget. Same execution_id => #1084 effect_guard dedups
  wired sinks.

Out of scope: #1083 DISPATCH_ASYNC path and a concurrent switch-lock loser.

Tests: tests/unit/test_792_subscription_retry.py (11 cases, all green).

Closes #792

Co-Authored-By: Claude <[email protected]>
@AndriiPasternak31
AndriiPasternak31 marked this pull request as ready for review June 27, 2026 18:48
@AndriiPasternak31
AndriiPasternak31 requested a review from dolho June 27, 2026 18:48
@AndriiPasternak31 AndriiPasternak31 self-assigned this Jun 27, 2026
@AndriiPasternak31
AndriiPasternak31 requested a review from vybe June 27, 2026 18:48

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved via /validate-pr: clean SUB-003 retry. Issue link+closing keyword ✓, no schema/config changes, security clean, named regression test (test_792_subscription_retry.py) covers the non-happy retry-after-switch path, SUB-003 stays inline-only (async #1083 bypass preserved), all 18 checks green.

@vybe
vybe merged commit b32a10e into dev Jun 27, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants