Skip to main content
>_ supraj.dev

Module 4: State & Graph Workflows · 2.25h

01 · UNDERSTAND

Day 21 Theory — Persistence, Checkpoints and Fault Recovery

What a checkpoint captures

A graph execution may run for seconds, minutes, or much longer if humans are involved. If the process dies halfway through, we do not want to restart from the beginning and repeat completed work.

A checkpoint stores enough graph state to resume execution.

Checkpointing versus business persistence

A graph checkpoint is not automatically your system of record.

For example:

Graph checkpoint: current node, workflow state, messages
Ticket system: authoritative ticket status

Keep workflow-resume state separate from authoritative domain data.

Thread/session identity

Checkpointed graph runs need an identifier that lets later invocations locate the right stored state.

This identifier becomes part of the persistence contract and must be scoped securely.

Cross-user or cross-tenant checkpoint access is a data leak.

Recovery after failure

Suppose the graph completed retrieval and classification, then the process restarted before human approval.

With durable checkpoints:

completed steps -> checkpoint -> crash
                               ↓
restart -> load checkpoint -> resume from intended point

Without them, the system may repeat expensive calls or side effects.

Exactly-once is difficult

Checkpointing does not magically make external side effects exactly-once.

If the graph calls an external API and crashes after the API commits but before the checkpoint records success, the resumed workflow may try again.

This is why Day 11's idempotency concepts still matter.

Checkpoint granularity

Saving after every tiny operation can increase storage and latency. Saving too rarely increases replay work.

Choose granularity based on failure cost, side effects and workflow duration.

State migrations

Long-lived persisted workflows may outlive the code version that created them.

If the state schema changes, old checkpoints may no longer load cleanly.

Production systems need a versioning/migration strategy rather than assuming all stored state matches today's code.

Service Desk connection

Today our LangGraph workflow becomes restartable. We can resume a support process without pretending a worker process will live forever.

The principle is:

Checkpoints make workflow execution resumable, but external side effects still require idempotency and persisted state still requires versioning and authorization.

02 · APPLY

Lesson Overview

This is the applied companion for Day 21. Read DAY_21_THEORY.md first for the beginner-first teaching of Persistence, Checkpoints and Fault Recovery. Then use the real service-desk-day-21/ project to trace, run, debug, and explain the concept.

Service Desk Alignment

Day 21 adds Persistence, Checkpoints and Fault Recovery to the running Service Desk. Start with checkpointer.py, graph.py, agent.py, workflow.py, debugger.py, serializer.py, then follow imports and tests to identify the actual runtime path.

Why This Topic Matters

The theory chapter explains why Persistence, Checkpoints and Fault Recovery is needed. Here the goal is evidence: identify where the capability is implemented, what observable behavior changes, and how the repository proves both success and failure behavior.

System Architecture

This is a repository surface map, not a claim that modules call each other in the displayed order. The modules are ranked by relevance to today's theory.

graph LR
    T[Day 21: Persistence, Checkpoints and Fault Recovery]
    T --> M1[checkpointer.py]
    T --> M2[graph.py]
    T --> M3[agent.py]
    T --> M4[workflow.py]
    T --> M5[debugger.py]
    T --> M6[serializer.py]
    T --> M7[models.py]

Follow imports and tests to discover the actual runtime flow.

Repository Implementation Map

Use the real Day 21 repository, not a fabricated sample, to connect theory to implementation.

Theory concepts to locate:

  • What a checkpoint captures
  • Checkpointing versus business persistence
  • Thread/session identity
  • Recovery after failure

Most relevant implementation modules first:

  • service_desk/checkpointer.py
  • service_desk/graph.py
  • service_desk/agent.py
  • service_desk/workflow.py
  • service_desk/debugger.py
  • service_desk/serializer.py
  • service_desk/models.py

Follow imports/calls from the relevant module and confirm behavior in tests. Record input → mechanism → observable output/state → failure evidence.

Code Walkthrough & Mechanics

Read checkpointer.py, graph.py, agent.py, workflow.py, debugger.py, serializer.py, models.py with these theory sections beside you:

  • What a checkpoint captures — locate its implementation and evidence.
  • Checkpointing versus business persistence — locate its implementation and evidence.
  • Thread/session identity — locate its implementation and evidence.
  • Recovery after failure — locate its implementation and evidence.
  • Exactly-once is difficult — locate its implementation and evidence.

For each concept identify the real function/class/protocol boundary, its input/state, its observable result, and the assertion in tests/test_checkpoints.py that proves the behavior. If a concept has no implementation or evidence, record that as a gap rather than inventing one.

Common Mistakes & Debugging Guidance

Use the theory—not generic timeout or .env advice—to decide what can fail today.

Failure lens: revisit Recovery after failure, Exactly-once is difficult.

  • Reproduce the smallest case that violates one of those expectations.
  • Trace the real Day 21 modules until you find the first incorrect state/output/decision.
  • Use tests/test_checkpoints.py as executable evidence.
  • Add a regression test if the failure is not already represented.
  • Fix the smallest responsible boundary and rerun the relevant test before the full suite.

Your debugging explanation must name the topic-specific invariant that failed, not merely say “an exception occurred.”

Practical Lab Instructions

  1. Summarize these theory ideas before opening the implementation:
    • What a checkpoint captures
    • Checkpointing versus business persistence
    • Thread/session identity
    • Recovery after failure
  2. Inspect the most relevant real Day 21 modules first:
    • service_desk/checkpointer.py
    • service_desk/graph.py
    • service_desk/agent.py
    • service_desk/workflow.py
    • service_desk/debugger.py
    • service_desk/serializer.py
    • service_desk/models.py
  3. Inspect the automated evidence:
    • tests/test_checkpoints.py
  4. Establish the baseline:
    cd service-desk-day-21
    PYTHONPATH=. pytest tests/test_checkpoints.py -q
    
  5. Trace one theory concept through the actual nested modules and tests.
  6. Run one success case and record input → mechanism → observable result.
  7. Exercise one topic-specific failure/boundary case and name the invariant that protects the system.
  8. Re-run the relevant tests and explain theory → implementation → evidence.

Done when: another student can reproduce your trace without relying on an invented sample.

Key Takeaways

  1. Be able to explain What a checkpoint captures and point to its implementation/evidence in Day 21.
  2. Be able to explain Checkpointing versus business persistence and point to its implementation/evidence in Day 21.
  3. Be able to explain Thread/session identity and point to its implementation/evidence in Day 21.

Knowledge Check & Scenario Questions

  1. Concept: Using What a checkpoint captures, explain the engineering problem Day 21 is solving without naming a framework as the answer.
  2. Mechanism: How does Checkpointing versus business persistence appear in the real project? Start from service_desk/checkpointer.py and name the observable state/output/event that changes.
  3. Failure: For Thread/session identity, describe one incorrect implementation or boundary condition and the evidence you would expect in tests/test_checkpoints.py.
  4. Design review: Which assumption in today's design would you verify before reusing this implementation in a different production system?

Official References

03 · EXPLAIN

Interview checkpoint

Explain one design decision from this lesson, the alternative you rejected, and the failure mode or evidence that justified your choice.