Unit 5 of 12 · about 5 hours · implement

Workflow state machines with durable intent and outcome records

Conversation history is not a transaction log. Recovery needs an explicit account of what was authorized, what was sent and what is actually known about the effect.

You'll be able to

  • Define states, events, guarded transitions and terminal invariants for the bounded workflow.
  • Persist versioned intent before dispatch and validated outcome before acknowledging completion.
  • Recover local state without inferring remote completion from a missing record.

Covers

Explicit workflow states, events, transitions and invariantsDurable intent and outcome records around side effects
1

Read

In this order, only the parts listed

0 of 3 done

  1. Durable Tool-Using Workflow: state contractDurable Tool-Using Workflow

    Core requirements; Architecture constraints; Milestones A and B; Correctness requirements

    Implement the state table and history in ordinary Python with SQLite. Do not build a general workflow engine.

  2. AWS: the atomic recipient boundaryMaking Retries Safe with Idempotent APIs

    Reducing client complexity with idempotent API design, especially the all-or-nothing token and mutation paragraph (page read 2026-10-18)

  3. DBOS: deterministic orchestration as a comparisonDBOS Transact Python: Workflows and Steps

    Workflow IDs and Idempotency; Determinism; Workflow Guarantees, for DBOS 3.3.0 (page read 2026-10-18)

    Read as a comparison with your state table, not a second implementation. Completed-step persistence cannot atomically commit an arbitrary remote effect. The guarantee assumes recovery of the application and database.

2

Focus on

  • Use explicit proposed, waiting-for-approval, ready, effect-pending, unknown, succeeded, compensated and manual-review states as needed. State which events and guards permit each transition.
  • Intent carries the caller-scoped operation key, canonical arguments, tool and schema versions, approval reference and authority evidence. Outcome carries a correlated receipt or an explicit unresolved status.
  • Commit local transition, history and any dispatch work record together. The Systems units on transactions and WAL establish local durability; a second recipient database remains outside that transaction.
  • Recover under the recorded workflow version. Either retain compatible logic or migrate state explicitly; do not reinterpret an old cursor under new code.
Common mistakes · 3
  • Persisting success before sending the request. Recovery can report an effect that never happened.
  • Holding a local database transaction open around a remote call and calling the pair atomic. The recipient commits independently.
  • Treating process-kill tests as power-loss or lost-disk evidence. They test only the stated failure model.
3

Try it yourself

Closed-book, before checking anything

Closed-book, implement one SQLite state row plus transition history and a separately persisted fake recipient. Commit intent with a stable operation key, dispatch once, and commit the validated receipt as outcome. Kill your executor process after recipient commit but before outcome persistence. On restart, recover the intent and mark its effect unknown until querying or replaying that same key settles it. A missing local receipt must never mean the effect failed.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Intent exists but outcome does not. What is known?Answer

    The operation was recorded for execution. The recipient may not have received it, may still be working, or may have committed it. Reconcile by operation identity.

  2. 2.Why commit history and state together?Answer

    Otherwise a crash can leave a transition with no audit event or an event for a transition that never committed.

  3. 3.Does a DBOS workflow ID deduplicate every remote effect inside a step?Answer

    No. An interrupted step can rerun before its result is recorded. The remote recipient still needs its own idempotency or reconciliation contract.

You're done when

A restart reconstructs the last committed transition and leaves the recipient-committed/local-unrecorded case explicitly unknown until receipt reconciliation succeeds.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 11 more in the section
  • Durable Tool-Using WorkflowRoadmap-Final · primary

    Build the reduced core through typed tools, one approval, one simulated irreversible effect, one compensable effect, capability policy, idempotency, durable intent and outcome, bounded execution and replay. Run kill tests around every persistence and effect boundary before adding an agent loop.

  • Making Retries Safe with Idempotent APIsAmazon Builders' Library · required

    Read "Retrying and the potential for undesirable side effects", "Reducing client complexity with idempotent API design", "Semantic equivalence and support for default retry strategies", "Late arriving requests and the life span of unique client request identifiers" and the parameter-mismatch section. Record the key and the effect atomically, scope keys by caller, and reject a reused key with different parameters.

  • DBOS Transact Python: Workflows and StepsDBOS · support

    Read the workflow tutorial ("Workflow IDs and Idempotency", "Determinism"), the step tutorial ("Configurable Retries", "Step Timeouts") and the system-database page (SQLite by default). Kill a process mid-workflow and confirm that completed steps do not rerun while the interrupted step does. Choose this or LangGraph as the implementation companion, not both.

Also in this section

  • AI Agents in Depth - Tools and Asynchronous InteractionBojie Li · required

    Read Chapter 4: 4.2 tool design and schema fidelity, 4.3 MCP, 4.5 versus 4.6 (read-only perception tools against execution tools), the "Idempotency and Cancellation Semantics" passage and the observability part of 4.6, and 4.7 human-in-the-loop timeouts. Add 5.1.5 Failure and Error Recovery and 6.2 asynchronous interaction. Translate the advice into explicit state and effect contracts.

  • Model Context Protocol Specification 2026-07-28Model Context Protocol · required

    Read the 2026-07-28 architecture and versioning pages: there is no initialize handshake, every request carries _meta protocol version, client info and capabilities, and server/discover lists versions and capabilities. Then the stdio and Streamable HTTP bindings, tools/list and tools/call (input and output schemas, isError versus JSON-RPC errors), resources and prompts, the tasks extension, and the roots and sampling deprecations. Build one small stdio server and client, record the wire messages, and keep application authorization outside the protocol.

  • Idempotent RequestsStripe · required

    Read the page as one concrete recipient contract: an Idempotency-Key header, the first result saved and replayed, parameter mismatch rejected, keys pruned after at least 24 hours (API v1), and the different API v2 retry semantics. Encode these cases in your fake effect recipient; do not treat 24 hours as universal.

  • Timeouts, Retries, and Backoff with JitterAmazon Builders' Library · required

    Read "Timeouts", "Retries and backoff" and "Jitter": a timeout leaves the outcome unknown, side-effecting calls are unsafe to retry without idempotency, and retries belong at one layer with a token-bucket budget. Model the unknown outcome as its own workflow state.

  • How to Do Distributed Locking (fencing tokens)Martin Kleppmann · required

    Read "What are you using that lock for?", "Protecting a resource with a lock" and "Making the lock safe with fencing". The effect recipient must remember the highest token and reject older ones (34 accepted, delayed 33 rejected); a random lock value is not a fencing token. Fencing rejects stale authority, while idempotency rejects repeated operations.

  • SagasHector Garcia-Molina and Kenneth Salem · reference

    Read Section 1 (p. 250: compensation does not necessarily restore the earlier state) and Section 9 Designing Sagas, with Sections 4 and 5 on backward and forward recovery. Some actions cannot be compensated; model failed compensation and manual handling explicitly.

  • LangGraph Persistence and InterruptsLangChain · support

    Read persistence (checkpointer versus store; the in-memory saver does not survive a restart), interrupts (interrupt, Command(resume=...), the rules of interrupts, "side effects called before interrupt must be idempotent") and the Functional API determinism and idempotency sections. Resuming restarts the interrupted node from its beginning: reproduce the repeated pre-interrupt action with a SQLite checkpointer across two processes.

  • Temporal: Workflow Replay, Activity Retries and TestingTemporal · reference

    Read Event History and replay, the Python workflow logic requirements, "Make Activities idempotent", the default retry policy (unlimited attempts unless set) and Signals versus Updates. Activities may run more than once, so replay does not make an external effect exactly-once.

  • Writing Effective Tools for AI AgentsAnthropic · support

    Read "Running an evaluation" and "Principles for writing effective tools" through "Prompt-engineering your tool descriptions": a small task-shaped tool surface, unambiguous parameter names, bounded and paginated results, actionable errors. Compare with the OpenAI function-definition best practices and rewrite one tool contract.

  • AI Engineering Handbook - Agent EngineeringHandbook Academy · support

    Study the Part 7 chapters on tool design, MCP and security, human review and long-running agents. Use them to audit user-facing workflow behavior and failure paths after the durable core works.

  • The Hitchhiker's Guide to Agentic AI - Harness and MCPHaggai Roitman · support

    Use v3 sections 18.4 to 18.7 (tool integration, orchestration with the explicit state machine in 18.5.5, state management and checkpoints, error handling), then Chapter 23 for MCP vocabulary and 23.6 for its security model. Its transport lists predate the 2026-07-28 specification and 18.4.5 contradicts Chapter 23, so take protocol facts from the specification.