Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

8.7 Practice: Resume a Long-Running Task Across Processes

The Performance Factory, described in November 2026, stores the stage, findings, and next steps of a performance regression investigation in a durable task record. The agent proposes a fix, engineers decide what ships, and the agent checks recovery after deployment. Another run can pick up the work when a process or session ends. See the original case study.

This offline exercise simulates publishing a report. SQLite records the job state, a command stands in for approval, and a separate query verifies the artifact. Each command starts a new process.

python examples/chapter08/resumable_task.py start --db demo.db
python examples/chapter08/resumable_task.py run --db demo.db
python examples/chapter08/resumable_task.py approve --db demo.db
python examples/chapter08/resumable_task.py run --db demo.db --stop-after-apply
python examples/chapter08/resumable_task.py run --db demo.db
python examples/chapter08/resumable_task.py run --db demo.db

The expected states are proposed → proposed → approved → applied → verified → verified. The second command checks that execution waits for approval. The fourth simulates a process stopping after it commits the artifact and before verification. The final commands resume verification and retry the task. Only one artifact is stored.

"""Resume a staged task with a SQLite checkpoint and a local, audited effect."""

import argparse
import sqlite3
from contextlib import closing

SCHEMA = """
CREATE TABLE IF NOT EXISTS jobs (
    id TEXT PRIMARY KEY,
    stage TEXT NOT NULL,
    proposal TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS artifacts (
    job_id TEXT PRIMARY KEY,
    content TEXT NOT NULL
);
"""


def resume(db: sqlite3.Connection, job_id: str, stop_after_apply: bool) -> None:
    # In this demo the effect and its checkpoint share one transaction.
    with db:
        apply = db.execute(
            "UPDATE jobs SET stage = 'applied' WHERE id = ? AND stage = 'approved'",
            (job_id,),
        )
        if apply.rowcount:
            db.execute(
                "INSERT INTO artifacts SELECT id, proposal FROM jobs WHERE id = ?",
                (job_id,),
            )

    if stop_after_apply:
        return

    # A resumed process can verify the recorded output without applying it again.
    with db:
        db.execute(
            """
            UPDATE jobs SET stage = 'verified'
            WHERE id = ? AND stage = 'applied'
            AND EXISTS (
                SELECT 1 FROM artifacts
                WHERE job_id = jobs.id AND content = jobs.proposal
            )
            """,
            (job_id,),
        )


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("command", choices=["start", "approve", "run", "status"])
    parser.add_argument("--db", required=True, help="Path to the SQLite database")
    parser.add_argument("--job", default="report-001")
    parser.add_argument("--stop-after-apply", action="store_true")
    args = parser.parse_args()

    with closing(sqlite3.connect(args.db)) as db:
        db.executescript(SCHEMA)
        with db:
            if args.command == "start":
                db.execute(
                    "INSERT OR IGNORE INTO jobs VALUES (?, 'proposed', ?)",
                    (args.job, "Publish the reviewed example report."),
                )
            elif args.command == "approve":
                db.execute(
                    "UPDATE jobs SET stage = 'approved' WHERE id = ? AND stage = 'proposed'",
                    (args.job,),
                )

        if args.command == "run":
            resume(db, args.job, args.stop_after_apply)

        job = db.execute("SELECT stage FROM jobs WHERE id = ?", (args.job,)).fetchone()
        if job is None:
            parser.error(f"Unknown job: {args.job}")
        print(f"{args.job}: {job[0]}")


if __name__ == "__main__":
    main()

The artifact and its checkpoint share a transaction in this example. For effects in external systems, persist the operation identifier, supply an idempotency key, and reconcile the external result before retrying. Bind approval to the artifact revision and enforce it on an authenticated server. See 7.3, context strategies for long tasks, and 13.5, human-in-the-loop.