Skip to content

Run a Stateful Agent on Agent Substrate

Category AI Solutions Engineering
Status ๐ŸŸก Partially completed
Repo nextwork-run-a-stateful-agent-on-agent-substrate
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's Run a Stateful Agent on Agent Substrate guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ AI Solutions Engineering
Status: ๐ŸŸก Partially completed

Overview

An agent that loses everything when its process dies can't be trusted with long jobs. Mine writes its state after every step, so a new process carries on where the last one stopped without repeating finished work.

Summary

I built my own small durable-agent runtime in Python and SQLite instead of Agent Substrate. A five-step agent (fetch notes, count words, find TODOs, summarise, write report) commits its state and a checkpoint in the same transaction after every step. A crashed or restarted process picks up at the next unfinished step, a finished session isn't run again, and a memory table persists across sessions. The demo crashes a run after two steps and resumes it in a new process, and the event log shows each step running exactly once. A test crashes four separate processes one step apart and proves the result matches a clean run. 2 tests pass. Partial: no Agent Substrate account or hosting, the runtime is mine, the agent's steps are deterministic Python rather than a model, and Claude Code didn't write it.

The checks behind this are in docs/verification.txt.

Skills & tools

Python, SQLite, Checkpointing, Durable execution, pytest

What I built

  • agent.py: the session, event and memory tables; the step functions; and run(), which loads a checkpoint, resumes, commits each step atomically and marks the session done. --crash-after N simulates a crash.
  • demo.sh: crashes a run after two steps, resumes it in a fresh process, runs it once more (already done), and prints the event log. docs/demo.txt is the output.
  • tests/test_agent.py: four crashes in a row still give the same result as a clean run, with every step once; plus already-done handling and persistent memory.

How to run

./demo.sh            # PY=python3 by default
python -m pytest -q tests

Verification

I ran the verification command on 2026-12-03. The full output is in docs/verification.txt.

PY=/workspace/nextwork-portfolio/.venv/bin/python sh demo.sh /tmp/stateful-verify.db > /tmp/stateful-demo.txt && grep 'already done' /tmp/stateful-demo.txt && /workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider tests 2>&1 | tail -1 | grep -q '2 passed' && echo STATEFUL OK

What isn't done, and why

No Agent Substrate: the durable runtime is my own SQLite version, the agent's steps are plain Python with no model, and it runs locally rather than hosted.


Original project: https://nextwork.ai/projects/ac04a70c-17f1-497a-8533-e2875ffd56ab ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ PY=/workspace/nextwork-portfolio/.venv/bin/python sh demo.sh /tmp/stateful-verify.db > /tmp/stateful-demo.txt && grep 'already done' /tmp/stateful-demo.txt && /workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider tests 2>&1 | tail -1 | grep -q '2 passed' && echo STATEFUL OK

already done -> 3 days, 23 words, 3 open TODOs
STATEFUL OK

[exit code 0]