All posts

6 min readPost 23

A dozen agents writing at once: measure where it is slow before changing anything

Running a dozen AI coding agents simultaneously slowed Obituary's hooks to a crawl. Before rewriting storage, we profiled real hook processes and real Git under load—discovering 1,049 ms single-hook p95 and 469 ms list latency at 16 concurrent writers—then gated five optimization batches on paired benchmarks and shipped Batch A the same day.

On this page
  1. The real concurrency baseline
  2. Where the time went: lock queueing and per-hook Git, not JSON parsing
  3. Five gated batches: no merge without paired numbers
  4. Shipping Batch A the same day with zero format changes

Run a single AI coding agent and Obituary's local hooks feel invisible. Open a dozen agents at once, and every file edit spawns a fresh obit hook process that checks Git and updates the local JSON ledger (store.json)—turning crisp terminal output and obit list noticeably sluggish.

Early on 22 November we resisted the urge to jump straight into a storage rewrite and profiled the real system end to end first.

On a ledger with 1,000 active entries and 1,000 graves, 16 concurrent hooks pushed single-hook p95 to 1,049 ms and a concurrent obit list to 469 ms.

The real concurrency baseline ​

Our benchmark drove real obit hook claude (PostToolUse in a consented project), real Git, and real NTFS file IDs inside isolated temp directories. Each wave launched k hooks staggered at 0, 5, 10, and 15 ms, fired a real obit list 65 ms later, and ran 7 waves per level:

Concurrent hooksWhole-wave p95Single-hook p95Concurrent obit list p95
4504 ms438 ms288 ms
8584 ms560 ms228 ms
161,272 ms1,049 ms469 ms

Across 112 writes at 16-way concurrency, 1 write exceeded its lock budget and completed via the existing replay queue—112/112 matched with zero lost items, yet latency was far past what an editor hook should cost.

Bar chart of p95 latencies at 4, 8, and 16 concurrent hooks on a 1,000+1,000 ledger: at 16 hooks single-hook is 1,049 ms, wave is 1,272 ms, and concurrent list is 469 ms, all above the 600/800/250 ms gates
Pre-optimization p95 latencies under 4, 8, and 16 concurrent hooks, compared against our 600 / 800 / 250 ms target gates at 16-way load.

Where the time went: lock queueing and per-hook Git, not JSON parsing ​

Breaking the latency down showed no single step looked outrageous in isolation:

  • Empty Go binary spawn-to-exit: 33.4 ms median, 48.3 ms p95.
  • Ordinary off-ledger PreToolUse hook: 48.0 ms median, 68.5 ms p95.
  • All Go package init() calls combined: 11.0 ms median (i18n 4.7 ms, net 3.7 ms).

Scaling ledger size tenfold from 1k+1k to 10k+10k exposed linear rewrite growth (medians):

Ledger operation (median)1,000 + 1,000 entries10,000 + 10,000 entries
Read hot store.json4.3 ms35.9 ms
Read full hot + cold snapshot12.3 ms111.4 ms
Update one revision and commit14.3 ms82.2 ms

Directory burial compounded the stall: on 10,000 entries, a reader waiting purely for the shared lock (without even decoding JSON) waited 2,043 ms median (2,500 ms p95), and an unlocked single-threaded identity walk across 10,000 files alone took 1,728 ms.

Neither loosening hook timeouts nor swapping JSON libraries could fix 16-way contention: every hook process was lining up for the full-ledger lock and spawning its own Git subprocesses.

Five gated batches: no merge without paired numbers ​

We broke optimization into five independently verifiable batches (A–E), each bound to paired same-machine gates:

BatchFocusMerge gate
AKeep JSON format unchanged: lazy hook subcommand init, on-demand i18n, display-projection reads for listOrdinary PreToolUse median down ≥ 4 ms with no p95 regression; 10k+10k list p95 heading under 300 ms
BEvaluate row-level ledger storage (SQLite + idempotent receipts)Persistent 1-row update at 10k entries p95 ≤ 15 ms; single-process scattered writes not slower than JSON
CResident daemon as single writer; hooks exit on durable receipt1k+1k at 16 concurrent hooks: hook p95 ≤ 600 ms (stretch 300), wave ≤ 800 ms, concurrent list ≤ 250 ms
DTrim directory burial and doctor lock stallsExtra reader wait during 10k directory burial p95 < 100 ms
EEvidence-gated micro-optimizations≥ 10% paired gain on the same machine

Three anti-shortcut rules stayed locked: zero skipped per-item identity checks during directory burial; hooks must hold a durable on-disk receipt before exiting (returning early without durability fails the gate); and no schema-changing batch ships standalone. A quick named-pipe storage prototype confirmed the single-writer thesis: 16 clients hitting 10k rows fell from 60.8 ms per commit to 8.8 ms with a 2 ms / 32-item batch window.

Shipping Batch A the same day with zero format changes ​

At 18:38 on 22 November, Batch A merged cleanly with zero schema changes and zero new dependencies:

  • Lazy hook startup: main.go previously constructed all 30+ subcommands and loaded localized help and config before recognizing a hook call, while i18n parsed both JSON catalogs at package init. Loading only the hook command path and wrapping catalog parsing in sync.OnceValue kept ordinary hooks lean.
  • Projection reads for list and summaries: cold graveyard snapshot bytes, SHA-256 hashes, ownership markers, IDs, and safety indices are still verified in full, without unmarshaling unused nested fields; any legacy ledger or unsupported evidence falls back to full reads, and all write paths stay untouched.

Paired AB/BA runs on the same machine (identical binary names, 2 warm-up pairs, zero outliers discarded):

Real processPairsOld p50New p50Old p95New p95
Ordinary PreToolUse6153.13 ms48.10 ms60.03 ms57.94 ms
obit list (10k active + 10k graves)35372.20 ms242.93 ms427.87 ms265.40 ms

Two catches before merge ​

The gates paid for themselves before Batch A even landed:

  • Rejected an intermediate build: hook p95 had dropped to 56.39 ms, yet 10k list p95 still sat at 567 ms; we held the branch until list dropped to 265.40 ms.
  • Red-first snapshot corruption test: setting parentRef = -1 inside a cold snapshot and recomputing its hash initially fooled the projection into reporting healthy; adding bounds verification turned the test green, alongside 44 byte-identical bilingual CLI checks, 3 new compile-time mutants, and full 3-OS CI.

Batch A cleared startup and read waste cleanly, leaving multi-hook lock queueing and repeated Git spawns for the resident-writer phase next.

All posts