Python
Install
Section titled “Install”cd bindings/pythonpython3 -m venv .venv && source .venv/bin/activatepip install maturin && maturin develop --releaseCreate a Store
Section titled “Create a Store”from agentstategraph_py import StateGraph
# In-memory (ephemeral)sg = StateGraph()
# SQLite (durable)sg = StateGraph("state.db")Basic CRUD
Section titled “Basic CRUD”# Set a value — every write is an atomic commit with intentsg.set("/cluster/name", "prod", "Initialize cluster", category="Checkpoint")
# Read it backname = sg.get("/cluster/name") # "prod"
# Set with full provenancesg.set("/cluster/replicas", 3, "Scale to 3 replicas", category="Refine", agent="agent/scaler", reasoning="Traffic increased 40% over last hour", confidence=0.85, tags=["scaling", "auto"])
# Set structured datasg.set_json("/cluster/network", { "subnet": "10.0.0.0/16", "dns": "1.1.1.1"}, "Configure network", category="Checkpoint")
# Deletesg.delete("/cluster/network", "Remove network config", category="Fix")Branches
Section titled “Branches”# Create a branchsg.branch("feature/new-network")
# Write to itsg.set("/cluster/network", "flannel", "Try flannel", ref="feature/new-network", category="Explore")
# List branchesbranches = sg.list_branches() # allbranches = sg.list_branches(prefix="feature/") # filtered
# Diffchanges = sg.diff("main", "feature/new-network")for c in changes: print(c["path"], c["op"])
# Mergesg.merge("feature/new-network", "main", description="Adopt flannel", reasoning="Lower overhead than calico")
# Delete a branchsg.delete_branch("feature/new-network")Speculation
Section titled “Speculation”Lightweight, disposable branches for the “try many, pick one” pattern.
# Create two speculations from mainspec_a = sg.speculate("main", label="approach-nfs")spec_b = sg.speculate("main", label="approach-ceph")
# Modify each independentlysg.spec_set(spec_a, "/storage/type", "nfs")sg.spec_set(spec_b, "/storage/type", "ceph")sg.spec_set(spec_b, "/storage/replicas", 3)
# Read from a speculationval = sg.spec_get(spec_a, "/storage/type") # "nfs"
# Pick the winner, discard the losersg.commit_speculation(spec_a, "Use NFS", category="Checkpoint", reasoning="Only 2 nodes, Ceph needs 3+", confidence=0.9)sg.discard_speculation(spec_b)Composable filters on the commit history. All filters are AND-combined.
# By agentcommits = sg.query(agent_id="agent/scaler")
# By intent categorycommits = sg.query(intent_category="Explore")
# By tagscommits = sg.query(tags=["scaling"])
# By reasoning text (full-text search)commits = sg.query(reasoning_contains="traffic")
# By confidence rangecommits = sg.query(confidence_min=0.8, confidence_max=1.0)
# Combinedcommits = sg.query( agent_id="agent/scaler", intent_category="Refine", confidence_min=0.7, limit=5)Find who last modified a path and why.
entry = sg.blame("/cluster/replicas")# {# "agent": "agent/scaler",# "intent": {"category": "Refine", "description": "Scale to 3 replicas"},# "reasoning": "Traffic increased 40% over last hour",# "confidence": 0.85,# "timestamp": "2026-04-06T..."# }Epochs
Section titled “Epochs”Group related work into bounded, sealable audit bundles.
# Createsg.create_epoch("2026-04-incident", "Node3 recovery", root_intents=["intent-001"])
# ... do work ...
# Seal (immutable, tamper-evident Merkle root)sg.seal_epoch("2026-04-incident", "Recovered node3, replicas restored")
# Listepochs = sg.list_epochs()# Recent historylog = sg.log("main", limit=5)for entry in log: print(f"{entry['id']}: {entry['intent']['description']} " f"(by {entry['agent']}, confidence: {entry['confidence']})")Intent Categories
Section titled “Intent Categories”| Category | Use for |
|---|---|
Checkpoint | Saving known-good state |
Explore | Trying an approach |
Refine | Improving existing state |
Fix | Correcting errors |
Rollback | Reverting to prior state |
Merge | Combining branch work |
Migrate | Schema/structural changes |