lockgate
by itsolaph
README.md
# Lockgate
Deterministic rollback engine and staged outbox chamber for autonomous AI agents.
**Live demo:** [lockgate.pages.dev](https://lockgate.pages.dev) (runs the exact `LockgateCoreEngine`, SQLite schema, SHA-256 CAS checks, and invariant verifier in an isolated per-tab SQLite 3 WebAssembly database).
File-snapshot tools handle local workspaces well, yet production agents rarely stop at files. They mutate live database rows while other traffic writes to the same tables, issue partner API calls, and queue emails or webhooks that cannot be unsent once they cross the network boundary.
Lockgate splits agent mutations into four reversibility classes and tracks their causal dependencies in a local SQLite WAL ledger:
| Reversibility class | Example tools | How Lockgate unwinds it |
| --- | --- | --- |
| `reversible_local` | `db.update_customer`, `db.insert_customer`, `db.delete_customer` | Captures atomic before/after row snapshots and SHA-256 hashes. Rollback re-checks the live row hash inside a SQLite transaction (CAS). If a human or another service edited the row concurrently, Lockgate blocks the overwrite and shows a 3-way diff (`keep_human` or `force_restore`). |
| `compensatable` | `stripe.create_invoice`, `slack.post_message`, `github.create_issue` | Pairs each forward call with a deterministic inverse action and a unique idempotency key (`comp:<runId>:<externalId>`). Rewinding twice executes the inverse API at most once, and transient 503 failures recover cleanly on retry. |
| `deferrable_outbox` | `email.queue_outbound`, `webhook.queue_outbound` | Holds irreversible outbound messages inside the Outbox Chamber during a configurable hold window. Rolling back before the outer gate opens transitions the item atomically from `held` to `cancelled` with zero external leak. |
| `irreversible_exposed` | Any already-flushed outbox item or non-reversible call | Never pretends an undo succeeded. Emits a structured `ReconciliationTicket` carrying the provider receipt ID, target, idempotency key, and manual correction runbook. |
## Quick start
Requires Node.js 22.5+ (uses built-in `node:sqlite`, zero runtime dependencies).
```bash
npm install
npm run build
npm test
```
Start the local Flight-Recorder Workbench (preloaded with the billing incident scenario):
```bash
node dist/cli.js serve --port 4173
```
Open `http://127.0.0.1:4173` to:
1. Run any of the three live SQLite disaster scenarios (`rogue-billing`, `flushed-email-leak`, `stripe-503-retry`).
2. Click any step on the causal timeline ruler to compute its transitive reverse-topological rollback plan before touching a byte.
3. Inspect the 3-way CAS conflict diff when a concurrent human edit occurs, pick `Keep human edit` or `Force restore`, execute rewind, and re-run rewind to verify zero duplicate compensations.
4. Deliberately open the outer lockgate (`Break seal and flush`) on a held email to watch Lockgate switch from `cancel_outbox` to an honest `ReconciliationTicket`.
Build the standalone static demo bundle (`pages-dist/`):
```bash
npm run build:pages
```
## MCP stdio server
Lockgate ships a JSON-RPC 2.0 Model Context Protocol server over stdio (`initialize`, `tools/list`, `tools/call`, `resources/list`, `resources/read`).
Add it to an MCP client config:
```json
{
"mcpServers": {
"lockgate": {
"command": "node",
"args": ["dist/cli.js", "mcp", "--db", "./lockgate.db"]
}
}
}
```
Exposed MCP tools:
- `lockgate_begin_run`
- `db_update_customer`
- `stripe_create_invoice`
- `slack_post_message`
- `outbox_queue_outbound`
- `outbox_flush`
- `lockgate_plan_rollback`
- `lockgate_execute_rollback`
- `lockgate_verify_invariants`
Resource URI: `lockgate://snapshot`.
## CLI commands
```bash
# Run a built-in incident against a persistent SQLite file
node dist/cli.js scenario rogue-billing --db ./demo.db
# Compute a dry-run rollback plan (full run or scoped to one step)
node dist/cli.js plan <runId> --db ./demo.db
node dist/cli.js plan <runId> --step <stepId> --db ./demo.db
# Execute rollback with explicit conflict resolutions
node dist/cli.js rewind <runId> --keep <conflictStepId> --db ./demo.db
# Verify SQLite state hash, idempotency uniqueness, and outbox invariants
node dist/cli.js verify <runId> --db ./demo.db
```
## Architecture
- `src/core/` is a pure functional core with no I/O: discriminated-union types, canonical SHA-256 hashing (`hash.ts`), reverse-topological causal closure and CAS dry-run planning (`planner.ts`), and ledger invariant verification (`invariants.ts`).
- `src/store/` contains the platform-agnostic `SqlDatabase` driver contract (`sql-driver.ts`), core engine (`core-engine.ts`), and native Node SQLite WAL wrapper (`engine.ts`) with injectable `Clock`, `IdGenerator`, and `ExternalAdapter` interfaces for deterministic replay and fault injection.
- `src/browser/` adapts `sql.js` WebAssembly SQLite (`sqljs-driver.ts`) and shares the exact REST API router (`api-router.ts`) with `src/http/server.ts`.
- `src/mcp/` implements the stdio JSON-RPC 2.0 MCP server and tool schema definitions.
- `src/http/` serves the REST API and static Workbench assets.
- `tests/` covers pure planner unit tests, `fast-check` property invariants (round-trip SQLite state hash, idempotent 2x rewind, human edit protection, transient 503 recovery), HTTP API integration, CWE-22 path traversal regressions, SQLite WASM vs `node:sqlite` parity tests, and a live spawned MCP stdio subprocess E2E test.
## Credits
| Dependency / asset | Use | License |
| --- | --- | --- |
| [Node.js `node:sqlite`](https://nodejs.org/) | Embedded SQLite WAL ledger and business tables (zero runtime npm deps) | MIT |
| [TypeScript](https://github.com/microsoft/TypeScript) | Static type checking and build compiler (dev) | Apache-2.0 |
| [Vitest](https://github.com/vitest-dev/vitest) | Unit, integration, and MCP subprocess test runner (dev) | MIT |
| [fast-check](https://github.com/dubzzz/fast-check) | Property-based invariant testing (dev) | MIT |
| [tsx](https://github.com/privatenumber/tsx) | Local TypeScript execution (dev) | MIT |
| [Playwright Core](https://github.com/microsoft/playwright) | Browser verification scripts (dev) | Apache-2.0 |
| [sql.js](https://github.com/sql-js/sql.js) | SQLite 3 compiled to WebAssembly for the static browser demo and parity tests (dev) | MIT |
| [esbuild](https://github.com/evanw/esbuild) | Bundling the static Cloudflare Pages demo runtime (dev) | MIT |
| [Archivo](https://fonts.google.com/specimen/Archivo), [IBM Plex Sans](https://fonts.google.com/specimen/IBM+Plex+Sans), [IBM Plex Mono](https://fonts.google.com/specimen/IBM+Plex+Mono) | Display, body, and monospace typography via Google Fonts | SIL OFL-1.1 |
| Studio hardware photography (`web/img/*.jpg`) | Generated specifically for this project | MIT |
## License
MIT. See [LICENSE](./LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessOutdated data