Skip to content

Instantly share code, notes, and snippets.

@baymac
Created September 30, 2026 05:57
Show Gist options
  • Select an option

  • Save baymac/cb8065dae85629bf1eb3c98aa89acfda to your computer and use it in GitHub Desktop.

Select an option

Save baymac/cb8065dae85629bf1eb3c98aa89acfda to your computer and use it in GitHub Desktop.
Journal Run Arch
Short version: journal runs got the full durability stack, which is a state machine in SQLite, an operation ledger, leases, a reconciler and a drainer. Chat reused the ledger and idempotency layers but deliberately left out leases and resume, because a chat turn is short enough that recovering one means asking again.
Architecture (journal runs)
Everything durable lives in ensol.db, a single SQLite connection whose writes are serialised by a promise-chain mutex (database.ts). The layers:
1. Run state machine. Each run is a row in agent_runs with a phase, a version and a driver lease. Moving to a new phase is a compare-and-set on version, so if two writers race, only one wins.
2. Command ledger. acceptCommand / loadCommandByIdempotency (repositories/commands.ts). Every client command carries an idempotency keyd. A retry replaysthe stored result; the same key with a different payload
raises a confli
3. Provider operation ledger. Each model call gets a
deterministic ipose, n), and a row in provider_operations. Each try at that call is a separate
attempt row wit, renewed by aheartbeat every 5s.
4. The durable bouvider calls runwith no transaction open. advance() works in committed
passes: commit, result.
5. Reconciler (workflow/reconciler.ts). Runs at startup and
every 15s. It cw much they can betrusted: commit receipt, then an unconsumed provider result,
then a step alrd so on. For eachstuck run it records what the run needs as a job in
run_dispatch_jo
6. Drainer (workflow/drainer.ts). Picks up those jobs, takes
the run's drive). advance() checks what already exists before doing anything, so the drainer
doesn't need to
What broke
Most of these wereaking the code intests, not by reading it.
- A crash erased paid results. At first the provider call ran
inside the writeolled back theprovider_operations row and the resumed run bought the same
call again. Moviansaction fixedthat, but it created the next four problems.
- The attempt leas707). startAttemptnever set lease_owner or lease_expires_at, so the
reconciler's cheL OR < now wasalways true. Once the call left the transaction, the
reconciler wouldnd re-bought them.The existing test "expires an abandoned attempt" had been
passing only becheck.
- Ownership was per process, not per call (d821f184). driverId
was a field on aent advance() calls shared one identity and both "acquired" the lease. That
caused two paid he lease was meantto prevent. Inherited leases were also never renewed, so they
lapsed during 30
- Stalled writers kept writing (5978c764). A laptop that went
to sleep kept lehad expired, thenwoke up and committed over whichever process had taken the
run over. Now evat is both ownedand unexpired. recordOperationResult also checks the run's
current driver lent check.
- Discard didn't stop in-flight work (ec373b50, 04cc8009). A
reply arriving a run back toactive. Attempts on discarded runs were orphaned permanently,
because the recoinal runs.
- Recovery was only half built (ba483041). The reconciler wrote
jobs but nothinghe drainer turnedup two more bugs: a job whose run couldn't be driven was
re-claimed in th 5 attempts, andstop() could outlive the database it was using.
- The UI couldn't ne" (bc1d067e,f2f1db65). The read path backs off while another driver holds
the lease, but i if the run hadsettled. The user typed into a blank box, and one interrupted
entry turned intRunView.driving,derived from the lease itself.
- Resume never reu. resumeDisposition had 18 passing tests and no production callers, so every
resumed eval runad alreadycompleted.
Reliability principles that came out of it
- Lease TTLs signal liveness; they aren't estimates of call
length. Keep lea rather than sizing a TTL to outlast the slowest provider.
- advance() never run. The drainertreats any return as finished and releases the lease.
- Never nest withTlocks permanently;it hangs rather than slowing down.
- Nothing is ever elled with acompare-and-set, so the tables remain a record of what was
spent.
- The guarantee is stated from the commit onwards. A crash
between the respit still loses that one call. Only provider-side idempotency keys could close
that gap, and neproposed "wait forthe in-flight call on shutdown" fix was rejected because it
turns a predicta
How chat reuses it
workflow/chat-run.run, with phasesloading → executing → answered | failed | cancelled. A conversation is mane conversationrow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment