swarm.db: one queryable SQLite trace mirror for swarm runs (status defaults to fail; kills finalise; per-run spend) #447

Closed
opened 2026-08-11 22:16:15 +00:00 by ben · 1 comment
Owner

Context

Learning L4 of docs/research/2026-08-11-software-factory-learnings.md: reconstructing one ticket's history means grepping
five sinks (host runlog, /tmp verdict artifacts, Mattermost, worker logs,
healer evidence, journalctl). Nothing records per-run/per-issue cost (the
2026-07-25 quota exhaustion burned ~14 h invisibly), and a hung worker emits
nothing anywhere (#435 green-and-empty). SSSF streams everything into one
WAL SQLite mirror; files stay the raw record; a processes table exists
because "a hung agent emits nothing, which is exactly when you need its pid."

What to build

A SQLite db on the workstation (e.g. ~/swarm/state/swarm.db, NOT in-repo),
written by run-swarm.sh and the worker wrapper as events happen (WAL mode,
busy_timeout, one small transaction per event). This is a MIRROR — every
existing sink stays; losing the db must lose nothing.

Tables (adapt SSSF's shape to swarm vocabulary):

  • runs — run id, repo, trigger (cron/label/dispatch), started/ended,
    verdict, verdict source line
  • attempts — run id, issue number, iteration, status TEXT DEFAULT
    'fail'
    (success must be earned by a verified clean exit — a crash can
    never read green), commit SHAs, close-report outcome
  • events — attempt-scoped: gate results with evidence, limit pauses, heals,
    asks, rescue-branch actions
  • processes — run id, kind (worker container / claude pid), pid or
    container id, command, started/ended (ended_at NULL = believed alive) —
    the #435 answer: a wedge is now a row with no end and no events
  • spend — per attempt where extractable from Claude Code output: tokens
    and/or request counts; the column exists even while extraction is partial

Two invariants stolen verbatim from SSSF:

  1. Status defaults to fail — only a clean, gate-verified exit writes
    success.
  2. Kills finalise the trace — SIGTERM/SIGINT handlers close open rows so
    nothing reads running forever.

Reader surface: 3–4 canned queries in a swarm-db.sh helper ("history of
issue N", "open processes", "spend by week", "runs red by stage").

Acceptance criteria

  • Db created/migrated idempotently by the harness (CREATE TABLE IF NOT
    EXISTS); WAL pragmas on every connection
  • A normal green run, a red run, and a killed run each leave correct,
    finalised rows (fixtures/tests)
  • worker_wedge detection also writes the wedge as an unfinalised
    processes row + event
  • Canned queries return sane output on a seeded fixture db
  • Existing sinks unchanged (runlog line format untouched — it is the
    dependable trail when the Actions API hangs)

References

  • docs/research/2026-08-11-software-factory-learnings.md (L4)
  • SSSF references/observability.md (schema + WAL pragmas + "success must be
    earned") in disler/super-simple-software-factory
## Context Learning L4 of `docs/research/2026-08-11-software-factory-learnings.md`: reconstructing one ticket's history means grepping five sinks (host runlog, /tmp verdict artifacts, Mattermost, worker logs, healer evidence, journalctl). Nothing records per-run/per-issue cost (the 2026-07-25 quota exhaustion burned ~14 h invisibly), and a hung worker emits nothing anywhere (#435 green-and-empty). SSSF streams everything into one WAL SQLite mirror; files stay the raw record; a `processes` table exists because "a hung agent emits nothing, which is exactly when you need its pid." ## What to build A SQLite db on the workstation (e.g. `~/swarm/state/swarm.db`, NOT in-repo), written by `run-swarm.sh` and the worker wrapper as events happen (WAL mode, `busy_timeout`, one small transaction per event). This is a MIRROR — every existing sink stays; losing the db must lose nothing. Tables (adapt SSSF's shape to swarm vocabulary): - `runs` — run id, repo, trigger (cron/label/dispatch), started/ended, verdict, verdict source line - `attempts` — run id, issue number, iteration, **status TEXT DEFAULT 'fail'** (success must be earned by a verified clean exit — a crash can never read green), commit SHAs, close-report outcome - `events` — attempt-scoped: gate results with evidence, limit pauses, heals, asks, rescue-branch actions - `processes` — run id, kind (worker container / claude pid), pid or container id, command, started/ended (`ended_at` NULL = believed alive) — the #435 answer: a wedge is now a row with no end and no events - `spend` — per attempt where extractable from Claude Code output: tokens and/or request counts; the column exists even while extraction is partial Two invariants stolen verbatim from SSSF: 1. **Status defaults to fail** — only a clean, gate-verified exit writes success. 2. **Kills finalise the trace** — SIGTERM/SIGINT handlers close open rows so nothing reads `running` forever. Reader surface: 3–4 canned queries in a `swarm-db.sh` helper ("history of issue N", "open processes", "spend by week", "runs red by stage"). ## Acceptance criteria - [ ] Db created/migrated idempotently by the harness (CREATE TABLE IF NOT EXISTS); WAL pragmas on every connection - [ ] A normal green run, a red run, and a killed run each leave correct, finalised rows (fixtures/tests) - [ ] `worker_wedge` detection also writes the wedge as an unfinalised `processes` row + event - [ ] Canned queries return sane output on a seeded fixture db - [ ] Existing sinks unchanged (runlog line format untouched — it is the dependable trail when the Actions API hangs) ## References - `docs/research/2026-08-11-software-factory-learnings.md` (L4) - SSSF `references/observability.md` (schema + WAL pragmas + "success must be earned") in `disler/super-simple-software-factory`
Author
Owner

Landed in a234766 (on origin/main).

swarm.db — one queryable SQLite trace MIRROR for swarm runs, per L4.

New files

  • .sandcastle/swarm-db.py — the engine. python3 (guaranteed on every swarm host and in the worker sandbox; the sqlite3 CLI is not — and python's parameter binding quotes free-text verdict lines/commands safely). WAL + busy_timeout + CREATE TABLE IF NOT EXISTS on every connection. Tables runs/attempts/events/processes/spend; attempts.status NOT NULL DEFAULT 'fail'.
  • .sandcastle/swarm-db-lib.sh — best-effort bash writers sourced by run-swarm.sh; every writer swallows its own failure (a mirror we cannot write must never red a run) and no-ops silently if python3 is absent.
  • .sandcastle/swarm-db.sh — reader: issue <N> | open-processes | spend-weekly | red-by-stage.
  • .sandcastle/tests/swarm-db-test.sh — 9 groups, all green.

Two invariants made law

  1. status defaults to fail — success is EARNED by a verified clean exit; run-end never promotes a status (test proves an earned success survives finalisation while an un-earned attempt reads fail).
  2. kills finalise the trace — run-swarm.sh now traps SIGTERM/SIGINT and routes them through its EXIT trap, whose run-end closes the run row + every open attempt so nothing reads running forever.

Acceptance

  • Db created/migrated idempotently by the harness (CREATE TABLE IF NOT EXISTS); WAL pragmas on every connection.
  • A normal green run, a red run, and a killed run each leave correct, finalised rows (fixtures/tests).
  • worker_wedge detection also writes the wedge as an UNFINALISED processes row + worker_wedge event (the #435 answer — a hung agent emits nothing, so the open row IS the evidence; surfaced by swarm-db.sh open-processes).
  • Canned queries return sane output on a seeded fixture db.
  • Existing sinks unchanged — runlog line format untouched (tests/runlog-lib-test.sh still green).

run-swarm.sh wiring: opens a run row up front (so an early-death/killed run still traces), events the ready-set / limit-pause / #435 wedge, records born workers as finalised proc rows, finalises in on_exit.

Follow-up (noted, not blocking): per-attempt status + token spend extraction from the worker wrapper (main.mts) — the columns exist while extraction is partial, exactly as the ticket allows.

The db lives at ~/swarm/state/swarm.db (off-repo, gitignored); ~/swarm/logs/run-swarm-verdicts.log and the /tmp verdict artifacts stay the raw record.

Landed in a234766 (on `origin/main`). **swarm.db** — one queryable SQLite trace MIRROR for swarm runs, per L4. **New files** - `.sandcastle/swarm-db.py` — the engine. python3 (guaranteed on every swarm host **and** in the worker sandbox; the `sqlite3` CLI is not — and python's parameter binding quotes free-text verdict lines/commands safely). WAL + `busy_timeout` + `CREATE TABLE IF NOT EXISTS` on every connection. Tables `runs`/`attempts`/`events`/`processes`/`spend`; `attempts.status NOT NULL DEFAULT 'fail'`. - `.sandcastle/swarm-db-lib.sh` — best-effort bash writers sourced by `run-swarm.sh`; every writer swallows its own failure (a mirror we cannot write must never red a run) and no-ops silently if python3 is absent. - `.sandcastle/swarm-db.sh` — reader: `issue <N>` | `open-processes` | `spend-weekly` | `red-by-stage`. - `.sandcastle/tests/swarm-db-test.sh` — 9 groups, all green. **Two invariants made law** 1. **status defaults to fail** — success is EARNED by a verified clean exit; `run-end` never promotes a status (test proves an earned `success` survives finalisation while an un-earned attempt reads `fail`). 2. **kills finalise the trace** — `run-swarm.sh` now traps SIGTERM/SIGINT and routes them through its EXIT trap, whose `run-end` closes the run row + every open attempt so nothing reads `running` forever. **Acceptance** - [x] Db created/migrated idempotently by the harness (`CREATE TABLE IF NOT EXISTS`); WAL pragmas on every connection. - [x] A normal green run, a red run, and a killed run each leave correct, finalised rows (fixtures/tests). - [x] `worker_wedge` detection also writes the wedge as an UNFINALISED `processes` row + `worker_wedge` event (the #435 answer — a hung agent emits nothing, so the open row IS the evidence; surfaced by `swarm-db.sh open-processes`). - [x] Canned queries return sane output on a seeded fixture db. - [x] Existing sinks unchanged — runlog line format untouched (`tests/runlog-lib-test.sh` still green). **run-swarm.sh wiring**: opens a run row up front (so an early-death/killed run still traces), events the ready-set / limit-pause / #435 wedge, records born workers as finalised proc rows, finalises in `on_exit`. **Follow-up (noted, not blocking)**: per-attempt `status` + token `spend` extraction from the worker wrapper (`main.mts`) — the columns exist while extraction is partial, exactly as the ticket allows. The db lives at `~/swarm/state/swarm.db` (off-repo, gitignored); `~/swarm/logs/run-swarm-verdicts.log` and the /tmp verdict artifacts stay the raw record.
ben closed this issue 2026-08-11 23:36:52 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Matou/idss#447
No description provided.