Repository navigation
Description
Problem
The workflow engine exposes two surfaces with very different execution-policy reach, and the streaming one has none of it.
Static nodes (step(), map_source, and a RouteArm) carry a full per-node execution policy: max_concurrent, strict reservations (ConcurrencyReservation), priority, start_to_close, result_ttl, and capacity_owner.
The streaming chain surface carries nothing comparable. Step holds only body / outcomes / route; Chain adds a single actor and a single queue for every row of every record; chain_source accepts only actor, queue, max_attempts.
Consequences for any downstream consumer that runs a long-lived record stream:
- One placement for the whole chain. A record pipeline whose stages have different resource profiles — a serialized selector, a bounded-concurrency leaf, a strictly reserved single-slot stage — cannot be expressed as one chain. Every stage lands on the chain's one
(actor, queue). - No admission terms on the rows. The prompt that emits the chain starts cannot declare a per-leaf cap, a strict reservation, a claim priority, or a per-attempt deadline, so the engine's own admission machinery is bypassed for exactly the rows that need it most.
- The engine refuses the workaround. A body already carrying capacity terms (
@app.actor(max_concurrent=…)) is rejected at wiring, because the chain has no passthrough and the terms would be silently dropped by the placement override. The author is told to "wire the capped body as a graph step" — i.e. to abandon the streaming shape. - The retry contract is not a policy surface. The ladder carries only
max_attemptsplus a two-valueretry_kind("transient"/"permanent") and a fixed0.05 * 2**nbackoff. There is no per-exception classifier, no configurable base/cap/jitter, and no exhaustion hook, so a body cannot express "429 with an advertised delay", "permanent configuration error, fail now", or "terminal — close the domain record exactly once". loopis not a workaround. Replacing a retried chain step with a loop iteration converts a transient failure into a terminal run failure (see Evidence), so it regresses precisely the behaviour the chain cannot declare.
This is the gating defect for adopting the streaming shape at all: without an execution-policy surface, converting a recursive actor pipeline to a chain means rebuilding the actor tier's admission, deadline, priority and retry semantics beside the graph — a shadow capacity framework, which is the thing a workflow engine exists to delete.
Evidence
Chain surface has no policy terms:
src/taskq/workflows/chain.py:183-214—Step(body, outcomes, route);Chain(name, start, steps, actor="wf", queue="default"). No capacity, priority, deadline, or retry fields; one actor/queue for every row of the chain.src/taskq/workflows/api/_graph.py:1892-1900—chain_source(chain, source_body, *args, key=None, actor="wf", queue="default", max_attempts=3). That is the entire source policy surface.src/taskq/workflows/api/_graph.py:1913-1930— the wiring loop refuses any capped body handle: "the chain surface has no capacity passthrough: the handle's capacity terms would be dropped by the placement override (the guard failing open by rename), the handle'scapacity_ownerdeclaration included."tests/test_wf_capacity_surface.py:1449-1475—test_chain_step_capped_body_refusedpins that refusal withmatch="capacity terms would be dropped". The refusal is intentional and tested; it is the capability that is missing.
The static surface, for contrast, is documented as accepting the whole policy:
docs/guides/workflows.md§ "THE CAPACITY SURFACE" (:981-1039) — the table ofmax_concurrent,reservations,result_ttl,capacity_owner,priority,start_to_close, each with its per-row resolution law (step declaration > actor declaration > row NULL > worker default), and the statement thatreservationsis the strict instrument whilemax_concurrentis only a per-round damper.
Retry is not a policy surface:
src/taskq/workflows/api/_runner_ladder.py:175-307—_ladder_or_failreadsmax_attemptsandretry_kind; the re-pend delay is literally0.05 * (2 ** failed_count); the only classifier isretry_kind != "permanent"; terminal exhaustion callsfinalize_node(outcome="failed")with no author-supplied hook. A per-exception classifier, a base/cap/jitter, an advertised-delay arm, or an on-exhaustion callback has no place to live.
loop is disproof, not a workaround:
src/taskq/workflows/api/_runner_loop.py:562-580— every exception crossing the body boundary is routed to_exhaust_loop(...), which terminalizes the flow in the same transaction. The comment states the intent explicitly: "every exception crossing it is a body failure — the loop's typed failure (the named class, the flow terminalized in the same tx)." A transient failure inside a loop iteration therefore kills the run instead of re-pending the row.docs/api-reference/workflows.md:81-135—loopis scoped to agent refinement/holds with iteration and time walls, not to per-record retry.
Row columns that already exist (the gap is projection, not schema):
jobs.priorityandjobs.start_to_closeare pre-existing columns the static surface already stamps at node insert (docs/guides/workflows.md:981-1039). A chain row could carry both today; nothing writes them.
Required
- Per-step execution policy on the chain surface, equivalent to static steps and map arms, declared on
Stepand defaulted onChain, and resolvable per row by the same precedence law the static surface documents:actor/queueper step (so one chain can span tiers),max_concurrentand strictreservations,capacity_owner(the code-vs-operator seed law),priority,start_to_close,- retry terms (below).
- A policy surface on the chain source, not just
max_attempts: actor/queue, capacity, reservations, priority, deadline, retry policy. - Retry policy as a real contract: a per-exception classifier (callable or declarative mapping), configurable base/cap/jitter, an advertised-delay arm that does not spend budget, and an exhaustion hook (or a documented equivalent — e.g. an authored terminal step the ladder routes to) that runs exactly once per terminal exhaustion, transactionally with the node terminal.
- Cohort attribution: chain cohorts must project into
actor_configwith the samecapacity_ownersemantics as static steps, and two chain steps with different capacities must be allowed to declare different actor names so they form two cohorts rather than colliding on one. - Honour or narrow the refusal: either accept a capped body handle at the wiring and project its terms, or keep the refusal scoped to the cases where the terms genuinely cannot be projected. The current blanket refusal is a capability statement, not a guard against a bug.
Acceptance criteria
Fleet-level proof, not stored values. Every item must be observed on a real Postgres with at least two workers claiming from the same queues.
- Strict bound holds. A chain step declaring
reservations=[ConcurrencyReservation(max_concurrent=N)]never has more than N of its rows executing simultaneously, across M concurrent runs (M ≫ N) and two workers. Prove it fromjobsclaim/finalize timestamps (max overlap computed from the rows), not from the projectedactor_configvalue. - Distinct cohorts. A two-step chain with different
max_concurrent/reservations/priorityprojects twoactor_configrows on a fresh boot, and each is enforced independently — saturating step A does not throttle step B. - Priority orders claims. A chain step declaring
priority=9is claimed ahead of a deeper backlog ofpriority=0vanilla jobs on the same queue, proven by observed claim order (the claim SQL'spriority DESC), including for rows materialized aftercreate_flow. - Deadline enforced per attempt. A chain step declaring
start_to_closekills an over-running body with the deadline terminal (not a bodyTimeoutError), the ladder applies the declared policy, and the row'sstart_to_closecolumn carries the value. - Classifier honoured. With a declared classifier: a classified-transient exception re-pends at the declared base/cap (measured delay within tolerance over ≥ 4 attempts); a classified-permanent exception fails on attempt 1; a classified-advertised-delay exception re-pends at the advertised delay without consuming the attempt budget.
- Exhaustion hook runs once. On terminal exhaustion the hook (or terminal route) runs exactly once, in the same transaction as the node terminal, and is not re-run by the crash-reclaim path. A test kills the worker inside the hook window and asserts at-most-once + the domain record's state.
- Capacity ownership rolls. With
capacity_owner="code", changing a chain step'smax_concurrentliteral and rolling the fleet converges the stored value and the enforcement follows; with"operator", a literal change logs a mismatch and does not write. Both observed after a real rolling restart. - No static-surface regression.
tests/test_wf_capacity_surface.pystays green, including the map/arm inheritance pins. - Capped handle path. A body carrying
@app.actor(max_concurrent=…)wired as a chain step either (a) is accepted and its terms appear in the projected cohort, or (b) still raises, with a new test proving the equivalent per-step declaration enforces the same bound at the fleet level. - Docs state the precedence law for chains alongside the existing static table, and name which of
max_pending's deliberate absence reasons still apply.
Impact
This is the gating blocker for any downstream consumer whose workload is genuinely page → one row per record → recurse, and whose current implementation is a recursive actor chain rather than boilerplate. Such a consumer needs, at minimum: a source that runs at one instance, leaves that must never exceed a provider's concurrency budget, stages with a hard single-slot reservation, a claim priority that lets interactive work jump a deep sweep backlog, per-attempt deadlines, and a retry curve that matches the provider's advertised behaviour.
Today every one of those has to be re-implemented outside the workflow graph, or the migration is accepted with the behaviour degraded. That is not a tuning detail: it is the difference between the engine owning admission and the application owning it beside the engine.
Downstream consumers cannot work around it from the outside either. The wiring refuses capped bodies by design, so the terms cannot be smuggled in through a decorator; the ladder has no extension point, so the retry contract cannot be injected; and loop terminalizes on the first body exception, so it cannot substitute for a retried step.
Related: #717 (record identity at scale), #718 (backpressure), #719 (worker-scoped deps), #720 (cancellation state/origin). This one is gating — it is the only one that changes what the engine can express rather than how it behaves at scale.
Environment
- taskq-py @
99e9a7d0(feat(workflows): adoption hardening, feat(workflows): adoption hardening #715) - Python 3.12+, PostgreSQL 18 (pgvector not required for this)
- No
flowsextra beyond the opt-in marker
Activity
feat(workflows): #716 chain policy, bounded retry, capability guard, …
chore(measurements): the re-stamp at main's #716 merge head 4441dd6 —…