research27 min studyLesson 881 in Engineering

Run state machines: separate logical intent from a worker incarnation

Model durable run transitions, revision checks, stale completions, and external-effect uncertainty under an explicitly proposed runtime protocol.

On this page

What you’ll understand

  • Distinguish logical run identity, incarnation, step, and attempt.
  • Specify revision-checked durable transitions and terminal-state rules.
  • Explain why fencing local state does not automatically fence an external side effect.

An agent run is more than a conversation loop. It can wait for approval, dispatch tools, lose a worker, resume elsewhere, or finish while an old request is still executing. Model those situations as explicit durable states with identities and transition rules. The protocol below is a design proposal; a workflow platform's documented guarantees must be checked separately.

A state name must have an operational meaning

Define states such as ready, deciding, waiting-for-approval, executing, reconciling, cancelling, and terminal outcomes. Specify which events permit each transition and which component owns them. Waiting can be a healthy durable condition rather than a process failure. Terminal success should require the declared effect and cleanup conditions, not merely the model emitting a final sentence. Record unresolved external outcomes separately so a timeout cannot silently become either success or a claim that nothing happened.

Give a logical user intent a stable run identity, then distinguish a worker incarnation or execution generation from a tool step and its retry attempts. A replacement worker should not accidentally create a new business effect merely because its process identifier changed. Temporal, for example, distinguishes Workflow IDs and Run IDs under its own lifecycle rules; that terminology is not identical to every proposed incarnation design. Choose identities by purpose and document which survive retries, restarts, or explicit new user intents.

Sn+1=δ(Sn,en+1)S_{n+1}=\delta(S_n,e_{n+1})
S is the proposed durable run state, e is an accepted ordered event, and δ is a versioned transition function. Undefined transitions are rejected or explicitly reconciled. This abstract model does not supply a storage transaction, distributed consensus, or external side-effect guarantee by itself.↔ Scroll wide equations horizontally.

Revision checks and fencing reject stale local decisions

A proposed state store can condition an update on the expected revision and current incarnation. Two workers reading the same revision must not both commit incompatible next states. After one succeeds, the other rereads or abandons its stale decision. The storage system must actually enforce that condition atomically. A timestamp comparison or a process-local mutex does not provide the same cross-worker property. Include event identity so duplicate delivery does not advance a transition twice under the chosen protocol.

An incarnation fence protects only components that check it. If an old worker has already sent an external write to a service that ignores the fence, rejecting its later completion event does not undo the write. External effects need their own idempotency, version, or reconciliation mechanism. Keep a late result as evidence of what occurred even if it cannot drive the new incarnation's state transition directly. A durable run can therefore enter reconciliation rather than pretend every stale message is harmless.

CommitTransition⁡⟹(rexpected=rstored)∧(irequest=icurrent)\operatorname{CommitTransition}\Longrightarrow(r_{\rm expected}=r_{\rm stored})\land(i_{\rm request}=i_{\rm current})
This desired compare-and-set condition uses revision r and incarnation i. It requires an implemented atomic store operation and a declared treatment of duplicate events. It says nothing about an external provider that does not participate in the same fencing protocol.↔ Scroll wide equations horizontally.

Test the lifecycle independently of model quality

Enumerate transitions for duplicate completions, cancellation races, delayed approvals, worker replacement, and a tool result arriving after a terminal decision. Verify which events are retained, ignored for control flow, or sent to reconciliation. Separate a retryable step failure from a failed logical run. Measure recovery and unresolved-effect age as well as completion rate. A model can choose sensible actions while the runtime mishandles them, and a correct state machine can faithfully execute a poor plan; those need different evaluations.

The Markov lab illustrates state progression only. Reset, set p=1 and q=0, predict state-one probability one after one Step, then Step once. Separately Reset, set p=0.50 and q=0, predict probability 0.75 after two Steps, then Step twice. It starts in state zero and has no revisions, workers, or atomic transitions. Its exact probabilities cannot prove durability or fencing. Use it to distinguish an abstract transition model from the actual identity and storage protocol being tested.

Reject a stale transition without erasing an effect

Two workers read durable revision twelve in incarnation three. Both propose a transition requiring revision twelve. The store atomically accepts the first. Separately, an older incarnation-two worker reports that its external request completed.

  1. The first accepted transition advances revision twelve to thirteen under the proposed revision rule.

  2. The second worker still expects twelve, so its incompatible transition is rejected. Only one of the two proposals commits.

  3. The incarnation-two completion cannot directly advance incarnation-three state under the fence. Its effect evidence must still be reconciled, because rejecting a local event does not prove the external request never executed.

The result

One local transition is accepted, one is stale, and the old external completion remains a separate reconciliation obligation.

Explore the model

Interactive lab

Interactive lab · markov

A transition probability is not a durable-state guarantee

Reset, set p=1 and q=0, predict state-one probability 1 after one Step, then Step once. Separately Reset, set p=0.50 and q=0, predict 0.75 after two Steps, then Step twice. This two-state chain has no worker incarnations, storage, revisions, or external effects.

A transition probability is not a durable-state guaranteeExact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Current readings: P(state 1) at step 0: 0%; Stationary P(state 1): 60%; Chain behavior: Monotone convergence. All parameters are adjustable with the labeled controls below.Two-state Markov distribution after 0 steps: state zero probability 1, state one probability 0.Initial distribution (1, 0) · step 0p = 0.3q = 0.201100%0%1−p1−qP(0)P(1)Monotone convergence
P(state 0)P(state 1)Stationary distribution
P(state 1) at step 0
0%
Stationary P(state 1)
60%
Chain behavior
Monotone convergence
t = 0 s

Exact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forImplement and test identity and atomic-transition invariants separately from a state diagram.

Check your understanding

What follows when a local incarnation fence rejects an old worker's completion?

Try it yourself

A proposed store is at revision forty. Two incompatible updates both require revision forty and increment on success. If the first commits, what revision exists and what happens to the second under atomic comparison?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Compare the second request's expected revision with the updated stored value.

Compare with the solution

The store becomes revision forty-one. The second update expecting forty is rejected and must reread or follow its conflict policy. This local result does not establish that either worker's previously dispatched external effects were absent or deduplicated.

Take it with you

  • Run, incarnation, step, and attempt identities serve different purposes.
  • Durable transitions require implemented atomic checks.
  • Local fencing and external-effect reconciliation are separate concerns.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.