Repository navigation
fix(subscriptions): a pull-dispatched terminal triggers SUB-003 (#2643) - #2651
Conversation
`pull_coordination_service.apply_task_result` is a CAS-won terminal writer and carried every OTHER terminal hook — the #1578 completion event, the #1804 activity close — but no SUB-003 hook. So a pull-owned turn the provider refused: * recorded no `subscription_rate_limit_events` row, so the subscription was never skip-listed and no usage card or pressure badge counted it; * never called `handle_subscription_failure`, so the agent stayed pinned to the subscription that had just refused it; * landed FAILED, where `redelivery_governor` treats `billing` as a correlated code — so the re-delivery that might have recovered it is the thing most likely to be paused. Everything the push path gained in #441/#471/#792, and everything #2638 added on top, was unreachable from a pull-dispatched turn. Inert today (`PULL_MODE_PILOT_AGENTS` gates the path and nobody is piloted), which is why it needed a test rather than a soak: piloting a subscription-backed agent would have silently removed SUB-003 from that agent with nothing saying so. Three properties are load-bearing. **The vocabularies do not line up.** The worker's typed `error_code` calls the quota class `billing` (`result_callback._STATUS_MAP` maps an agent 429 to it); this subsystem calls it `rate_limit`. `_switch_failure_kind` is the map, and it is an ALLOWLIST — a code it has not heard of switches nothing, because "switch unless the code looks benign" would churn an agent through every subscription it owns the first time a worker reports an unfamiliar crash class. **CAS-won branch only**, beside the two hooks it sits with. A replayed terminal short-circuits above the write and a late one loses the CAS, so neither spends a second switch (the #1083 rule). Past that gate, `handle_subscription_failure` owns the rest: it records the event unconditionally, then takes the #799 per-agent `agent_switch_lock` and re-reads under it, so two failures racing on one agent still switch once. **A SUCCESS terminal is excluded, on the merits.** The gate is `error_code` rather than the FAILED/CANCELLED split — a worker that labels a quota refusal `cancelled` still refused for a quota reason — but a turn the provider SERVED is evidence the subscription works, so a stray `error_code` riding a success must not move the agent off it. It is also the one branch where the sink never binds `err_text`, so an ungated hook would raise NameError and turn a committed, billed terminal into a 500. Re-delivery, stated explicitly per AC #3: this hook re-delivers nothing. A switched agent's next re-delivery is expected to succeed IF the lease reaper re-queues the row (under `MAX_REDELIVERY`) and the #1085 governor is not paused — and `billing` is exactly the class the governor counts, so a fleet-wide quota event can legitimately hold re-delivery back even after a successful switch. The switch is not wasted then; the next scheduled or claimed turn benefits. The sink is sync and its caller async, so the call goes through `subscription_auto_switch.spawn_subscription_failure` — the same wrapper shape as `spawn_task_terminal_event` and `spawn_close_execution_activity`: one coroutine (not a wrapper around a pre-built one, which leaves two objects to close and still warns "never awaited"), a strong reference held until it finishes, a raising switch logged and swallowed, and no-running-loop treated as a clean skip. Still NOT wired on the pull sink, named so it is not mistaken for done: the #526 AUTH dispatch breaker and the governor's own `record_terminal_failure`. `apply_result` has both; neither is a SUB-003 concern. 23 tests, mutation-checked: a pass-through failure-kind map, hoisting the call above `if won:`, and dropping the SUCCESS exclusion each turn the suite red. Plus a structural AST pin that the one call site stays inside the CAS gate. Related to #2643. Related to #1081. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Bd71qsYbFodvofba8P69eP
|
merge-train 2026-09-09: on this train. No criticals. I edited the PR body twice — no code touched — flagging both so they aren't silent. 1. 2. Corrected a factual claim that would have ridden into the squash commit. The body's third bullet said a pull terminal "landed FAILED, where
Your own "Deliberately not in scope" section has this right ("the pull sink still has no Why this passed where its sibling #2645 did not, since they touch the same area and the difference is worth stating: #2645 keys on Verified rather than assumed: the hook at Two follow-ups, neither blocking:
Noted for the record, not as an objection: |
Closes the last SUB-003 gap on the pull path. Split out of #2638 (gap 5).
The bug
services/pull_coordination_service.py::apply_task_resultis a CAS-won terminal writer and carried every other terminal hook — the #1578 completion event, the #1804 activity close — but no SUB-003 hook. A pull-owned turn the provider refused therefore:subscription_rate_limit_eventsrow, so the subscription was never skip-listed and no usage card or pressure badge counted it;handle_subscription_failure, so the agent stayed pinned to the subscription that had just refused it;redelivery_governortreatsbillingas a correlated code here and would pause the recovering re-delivery. That is wrong on the pull path in both directions, and the "Deliberately not in scope" section below has it right:record_terminal_failurehas exactly one caller,task_execution_service.py:2556on the pushapply_result, so the pull sink never arms the governor; andlease_reaper_service, which is the pull re-delivery path, never consults it. Corrected here so the claim does not outlive the PR in the squash commit.)Everything the push path gained in #441 / #471 / #792, and everything #2638 added on top, was unreachable from a pull-dispatched turn.
Inert today (
PULL_MODE_PILOT_AGENTSgates the path and nobody is piloted) — which is exactly why it needed a test rather than a soak. Piloting a subscription-backed agent would have silently removed SUB-003 from that agent, with nothing saying so.The fix
One hook on the CAS-won branch, beside the two it sits with. Three properties are load-bearing.
The vocabularies do not line up. The worker's typed
error_codecalls the quota classbilling(result_callback._STATUS_MAPmaps an agent 429 to it); this subsystem calls itrate_limit._switch_failure_kindis the map, and it is an allowlist — a code it has not heard of switches nothing, because "switch unless the code looks benign" would churn an agent through every subscription it owns the first time a worker reports an unfamiliar crash class.CAS-won branch only. A replayed terminal short-circuits above the write and a late one loses the CAS, so neither spends a second switch (the #1083 rule). Past that gate
handle_subscription_failureowns the rest: it records the event unconditionally, then takes the #799 per-agentagent_switch_lockand re-reads under it, so two failures racing on one agent still switch once.A SUCCESS terminal is excluded, on the merits. The gate is
error_coderather than the FAILED/CANCELLED split — a worker that labels a quota refusalcancelledstill refused for a quota reason — but a turn the provider served is evidence the subscription works, so a strayerror_coderiding a success must not move the agent off it. It is also the one branch where the sink never bindserr_text, so an ungated hook would raiseNameErrorand turn a committed, billed terminal into a 500. (Found while writing the tests, not after.)The sink is synchronous and its caller async, so the call goes through the new
subscription_auto_switch.spawn_subscription_failure— the same wrapper shape asspawn_task_terminal_event(#1578) andspawn_close_execution_activity(#1804): one coroutine (wrapping a pre-built one leaves two objects to close and still warns "never awaited"), a strong reference held until it finishes, a raising switch logged and swallowed, and no-running-loop treated as a clean skip. It runs after a committed, billed terminal and must never be able to turn one into a 500.Acceptance criteria
error_codein{billing, auth}records the failure event and triggers the same switch the push path does, once, under the existing per-agentagent_switch_lock. (handle_subscription_failureowns both; the sink just calls it.)if won:, so a refactor that hoists it fails even if it keeps some test green by accident.MAX_REDELIVERY). The feat: correlated-failure / thundering-herd controls (jitter + re-delivery caps + shared-cause pause) #1085 governor does not gate this path —lease_reaper_servicenever consults it — so re-delivery turns only on the reaper and the retry cap. Either way the switch is not wasted: the next scheduled or claimed turn benefits from it.docs/planning/TARGET_ARCHITECTURE.mdbeside the other gate items, as closed rather than pending, so a future reader evaluating the pull default-ON gate does not re-derive it.Deliberately not in scope
The pull sink still has no #526 AUTH dispatch breaker and no
redelivery_governor.record_terminal_failure.apply_resulthas both. Neither is a SUB-003 concern, and both change failure semantics on a path this PR only reads — named here and in the flow doc so they are not mistaken for done.Verification
Mutation-checked — each of these turns the suite red:
billing→billing) → 3 failures;if won:→ the lost-CAS test and the AST pin;NameErrortest.Docs:
docs/memory/feature-flows/subscription-auto-switch.mdgains the two pull rows in the trigger-surface table and a#2643section carrying the reasoning above.Fixes #2643. Related to #1081.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Bd71qsYbFodvofba8P69eP