Ankit Sinha

Design study

Keel

An operations agent for an online store: deterministic settlement reconciliation in plain code, and an LLM restricted to unresolved exceptions.

TypeScript · Node 22 · PostgreSQL · Drizzle · pgvector · Zod · Vitest · Docker

The problem #

Payment settlements arrive batched, late, fee-adjusted, or duplicated. Plain code should handle the clear matches; the model is only useful for reading messy exceptions—and should never be trusted to move money directly or guarantee once-only execution.

Architecture #

settlement files
      │
      ▼
reconcile (plain code) ────▶ matched rows, closed
      │
      ▼
typed exceptions
      │
      ▼
agent loop ◀──▶ read tools · skills · policy chunks
      │
      │  proposes: refund + amount + citation
      ▼
guardrails      cap · allow-list · dry-run
      │
      ▼
approval        auto under cap · human above cap
      │
      ▼
executor        intent key · row lock · ledger
      │
      ▼
fake PSP        flaky: delays · 5xx · duplicates

side path       agent parser ──▶ sealed container
                no net · ro root · mem cap · timeout
Refund intent states: proposed, then approved or rejected; approved becomes executed, then confirmed, or unknown on a provider timeout proposed approved executed confirmed rejected unknown GUARDS PSP CALL ACK LIMIT/HUMAN TIMEOUT EVERY TRANSITION PERSISTED · NO MONEY MOVES WITHOUT A KEY

A timeout lands in unknown—not failed—until the executor queries the provider by its own idempotency key.

Select a scenario above to step through live state transitions.

Key decisions #

Code reconciles; the model sees leftovers

Deterministic matchers close clean rows first. Ambiguous pairs (same amount, same day) and mismatches escalate once per exception, keeping cost proportional to problems rather than volume.

Derived idempotency keys + payload hashes

Keys come from refund:<exception_id>, not the model. Matching payloads replay; changed amounts reject; expired locks reconcile with the PSP before retrying.

Caps enforced inside the write

Daily limits use an atomic UPDATE ... WHERE total + amount <= cap in the same transaction as the refund. Over-cap intents pause in proposed until human approval.

Grant-enforced ledger + cited policy

The app DB role has only SELECT and INSERT on the ledger. Proposals must cite real pgvector + full-text policy chunks, while skills load on demand to keep prompts flat.

Sealed parser sandbox + flaky PSP

Agent-written CSV parsers run in a read-only, networkless container with memory, PID, and timeout caps. A chaos fake PSP injects latency, 5xx errors, and duplicate acks alongside the executor.

Status #

6exception
classes
1path that
moves money
0floats in
money columns
4loop stop
conditions

The model proposes; policy, row locks, and idempotency keys decide.

Written design—no implementation or evals yet. Build order starts with the deterministic reconciler against planted-mistake fixtures, followed by executor concurrency and timeout tests.