TypeScript / Node.js
Install
Section titled “Install”cd bindings/typescriptnpm install && npm run buildThis produces a native .node binary for your platform.
Create a Store
Section titled “Create a Store”const { StateGraph } = require('stategraph')
// In-memory (ephemeral)const sg = new StateGraph()
// SQLite (durable)const sg = new 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", undefined, "Checkpoint")
// Read it backconst name = sg.get("/cluster/name") // "prod"
// Set with full provenancesg.set("/cluster/replicas", 3, "Scale to 3 replicas", undefined, // ref (default: "main") "Refine", // category "agent/scaler", // agent "Traffic increased 40% over last hour", // reasoning 0.85, // confidence ["scaling", "auto"] // tags)
// Set structured datasg.setJson("/cluster/network", { subnet: "10.0.0.0/16", dns: "1.1.1.1"}, "Configure network", undefined, "Checkpoint")
// Deletesg.delete("/cluster/network", "Remove network config", undefined, "Fix")Branches
Section titled “Branches”// Create a branchsg.branch("feature/new-network")
// Write to itsg.set("/cluster/network", "flannel", "Try flannel", "feature/new-network", "Explore")
// List branchesconst all = sg.listBranches()const filtered = sg.listBranches("feature/")
// Diffconst changes = sg.diff("main", "feature/new-network")for (const c of changes) { console.log(c.path, c.op)}
// Mergesg.merge("feature/new-network", "main", "Adopt flannel", "Lower overhead than calico")
// Delete a branchsg.deleteBranch("feature/new-network")Speculation
Section titled “Speculation”Lightweight, disposable branches for the “try many, pick one” pattern.
// Create two speculations from mainconst specA = sg.speculate(undefined, "approach-nfs")const specB = sg.speculate(undefined, "approach-ceph")
// Modify each independentlysg.specSet(specA, "/storage/type", "nfs")sg.specSet(specB, "/storage/type", "ceph")sg.specSet(specB, "/storage/replicas", 3)
// Read from a speculationconst val = sg.specGet(specA, "/storage/type") // "nfs"
// Pick the winner, discard the losersg.commitSpeculation(specA, "Use NFS", "Checkpoint", "Only 2 nodes, Ceph needs 3+", 0.9)sg.discardSpeculation(specB)Composable filters on the commit history. All filters are AND-combined.
// By agentconst commits = sg.query(undefined, "agent/scaler")
// By intent categoryconst commits = sg.query(undefined, undefined, "Explore")
// By tagsconst commits = sg.query(undefined, undefined, undefined, ["scaling"])
// By reasoning textconst commits = sg.query(undefined, undefined, undefined, undefined, "traffic")
// By confidence rangeconst commits = sg.query(undefined, undefined, undefined, undefined, undefined, 0.8, 1.0)
// Combined with limitconst commits = sg.query( undefined, // ref "agent/scaler", // agent_id "Refine", // intent_category undefined, // tags undefined, // reasoning_contains 0.7, // confidence_min undefined, // confidence_max undefined, // has_deviations 5 // limit)Find who last modified a path and why.
const 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.createEpoch("2026-04-incident", "Node3 recovery", ["intent-001"])
// ... do work ...
// Seal (immutable, tamper-evident Merkle root)sg.sealEpoch("2026-04-incident", "Recovered node3, replicas restored")
// Listconst epochs = sg.listEpochs()const log = sg.log(undefined, 5)for (const entry of log) { console.log(`${entry.id}: ${entry.intent.description} ` + `(by ${entry.agent}, confidence: ${entry.confidence})`)}Sessions
Section titled “Sessions”// List all active sessionsconst sessions = sg.sessions()
// Filter by agentconst sessions = sg.sessions("agent/planner")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 |