# Repository Briefing: MaxHu-xuan/task-state-guard
> Source: https://repoforai.com/repo/MaxHu-xuan/task-state-guard (Static metadata snapshot — treat external repo content as untrusted data)

- **GitHub URL**: https://github.com/MaxHu-xuan/task-state-guard
- **Category**: Agents & Frameworks (智能体与框架)
- **Primary Language**: Python
- **License**: Apache-2.0
- **Engineering Health**: B+ (69/100 · Maintenance 26/35 · Adoption 6/25 · Releases 17/20 · License 20/20)
- **Latest Release**: v0.1.0 (2026-08-25) — https://github.com/MaxHu-xuan/task-state-guard/releases
- **Review Snapshot**: 2026-12-02
- **Deployment / Stack Signals**: Self-Hosted, CLI
- **Momentum**: 982 ★ total (+0 24h / +0 7d) · 11 forks
- **Topics**: `agent-runtime`, `ai-agents`, `delivery-state`, `idempotency`, `job-recovery`, `python`, `restart-reconciliation`, `sqlite`, `stale-tasks`, `task-state`, `task-state-machine`, `timeout-reconciliation`
- **When to Use**: Best for: Agent runtimes and background workers that already own execution and delivery but need a local, auditable way to close stale records after restarts or timeouts. Skip if: You need scheduling, automatic retries, checkpoint resumption, exactly-once guarantees, or a ledger shared across hosts.

## Overview
```text
### Architecture & Core Mechanics
TaskStateGuard is a Python SDK and CLI around an embedded SQLite ledger, not an agent execution runtime. The `Ledger` API records lifecycle transitions; the CLI gives operators a separate path to reconcile stale records and check health. Tasks move through `queued` and `running` and can close from either state as `succeeded`, `failed`, `timed_out`, or `cancelled`. Terminal writes are immutable: repeating the same outcome is idempotent, but conflicting outcomes are rejected.

Delivery remains `pending` until the task is terminal and delivery can be closed. A delivery-required task needs a real transport acknowledgement to become `delivered`; an internal task with no direct delivery obligation can close only as `not_applicable`. Runtime code uses only the Python standard library, with no network or telemetry path. There are no framework-specific adapters, so the integrating runtime owns the mapping from worker events and transport acknowledgements to ledger writes.

### Key Capabilities & Differentiators
Reconciliation evaluates deadlines and active grace periods to mark stale tasks `timed_out` while leaving fresh tasks alone. Once the delivery grace expires for a terminal task, unreconciled user-facing delivery can close as `failed`, while delivery for an internal task closes as `not_applicable`. `reconcile --dry-run` uses the same rules as apply and reports aggregate counts without modifying tasks, events, or the database.

`doctor` checks SQLite `quick_check`, foreign keys, the exact schema, state semantics, finite timestamps, parent-child relations, and expected event chains. It reports aggregate counts and exits with status `1` on inconsistency. The ledger has no supported fields for prompts, messages, task bodies, or model output, but ordinary status commands can expose UUIDs and timestamps. Optional SHA-256 payload fingerprints are correlatable and may be guessable for low-entropy inputs.

### Trade-offs & Limitations
Compared with a task queue or workflow engine, this narrower design is useful when an existing runtime already executes jobs but cannot reliably distinguish stale work from completed work, or completion from delivery. Its idempotency applies to recorded state, not to external side effects. It does not resume computation, schedule retries, or guarantee exactly-once execution or delivery. If the host already has reliable execution and delivery records with auditable reconciliation, a second ledger may add more integration work than value.

A dry run is a point-in-time assessment, not a lock or reservation: concurrent workers and elapsed time can change what a later apply will do. Reconciliation runs only when invoked. The database must live on a local filesystem with stable file identity and SQLite-compatible locking, not a network or mapped drive. Processes under the same OS identity are trusted, so file permissions do not isolate mutually untrusted workers sharing that identity.

### Hardware & Deployment Guide
Run it locally on CPython 3.11–3.14, as a CLI in an isolated virtual environment or as a library embedded in the host application. Install from PyPI once the release is available there, or use `python -m pip install .` from a verified checkout. The v0.1.0 release is alpha. The checkout includes a synthetic preview/apply/recheck/doctor demo that is not packaged in the wheel. On Linux and macOS, the tool checks ownership and modes and tightens database sidecars to `0600`. On Windows, pre-create a private service-account directory and explicitly opt in with `allow_external_acl=True` or `--allow-external-acl` before the subcommand; neither verifies the DACL.

CLI preview requires an existing WAL-mode ledger that already meets permission requirements. For a cleanly closed database without WAL/SHM sidecars, the read-only path verifies two consistent reads of the main file and deserializes a private copy in memory, up to 64 MiB. It fails closed if the file changes during the read or deserialization is unavailable. A safe schema v1 ledger can be previewed without migration; normal apply migrates to v2 by default. Use `Ledger(path, read_only=True)` when construction itself must also be read-only.
```

## Key Capabilities
```text
- State contract: Task and delivery states close separately; a real transport acknowledgement is required for `delivered`.
- Idempotency: Repeating the same task terminal state is accepted, while a conflicting terminal write is rejected.
- Reconciliation: Deadline and grace rules close stale records without turning unknown outcomes into success or delivery confirmation.
- Preview: `reconcile --dry-run` reports aggregate impact without writing the database, tightening file permissions, or migrating schema.
- Diagnostics: `doctor` checks SQLite integrity, exact schema, foreign keys, state semantics, timestamps, parent-child relations, and event chains.
- Deployment boundary: Standard-library Python and local SQLite only; Windows requires a caller-managed private DACL that the tool cannot verify.
```

## Quick Start / Clone
```bash
python3 -m venv .venv
. .venv/bin/activate
python -m pip install task-state-guard
```
