Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

v0.46.1.0 feat(minions,db): per-job process isolation + pool-starvation fixes (garrytan-agents#5, #6) (#4151)

Garry Tana90547e6febd
diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml
index ab98fb62a1..bb8abe6039 100644
--- a/.github/workflows/e2e.yml
+++ b/.github/workflows/e2e.yml
@@ -96,7 +96,9 @@ jobs:
bun-version: 1.3.13
- run: bun install
- name: Run Tier 1 E2E tests
- run: bun test --timeout=60000 test/e2e/mechanical.test.ts test/e2e/mcp.test.ts
+ # job-isolation rides tier1 deliberately: e2e.yml runs only explicitly
+ # NAMED files (no glob) — an unwired e2e file is silent coverage loss.
+ run: bun test --timeout=60000 test/e2e/mechanical.test.ts test/e2e/mcp.test.ts test/e2e/job-isolation.test.ts
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/gbrain_test
# #3485 preload guard: this job intentionally tests against a DB.
diff --git a/BOOTSTRAP_FOR_AGENTS.md b/BOOTSTRAP_FOR_AGENTS.md
index 44ef0278d9..775f31a6cf 100644
--- a/BOOTSTRAP_FOR_AGENTS.md
+++ b/BOOTSTRAP_FOR_AGENTS.md
@@ -1,4 +1,4 @@
-<!-- gbrain-runbook-stamp: 0.46.0.0 -->
+<!-- gbrain-runbook-stamp: 0.46.1.0 -->
<!-- This stamp must equal the VERSION file at every release; CI enforces it
(scripts/check-bootstrap-tag.sh). `gbrain bootstrap status` compares it to
the installed binary and warns on skew. -->
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 872903ccab..9c9872c375 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,61 @@
All notable changes to GBrain will be documented in this file.
+## [0.46.1.0] - 2026-08-15
+
+**A stuck job can no longer take down your whole worker.** Field reports from
+a production deployment ([#5](https://github.com/garrytan-agents/gbrain/issues/5),
+[#6](https://github.com/garrytan-agents/gbrain/issues/6)) showed two
+compounding failure modes: a handler that ignored its abort signal could only
+be "force-evicted" (abandoned but still running, still holding connections),
+and abandoned probe/renewal queries starved the connection pool until the
+worker killed itself with a misleading "DB unreachable" — while the database
+sat at a fraction of capacity. This release fixes the starvation class and
+adds real per-job blast-radius control.
+
+### Added
+- **`gbrain jobs work --job-isolation process`** (also
+ `gbrain jobs supervisor --job-isolation process`, env
+ `GBRAIN_JOB_ISOLATION`): each claimed job runs in its own child process.
+ A stuck handler is group-SIGKILLed for real instead of abandoned, a crash
+ or memory blowup takes one job instead of all N, and the OS reclaims every
+ leaked resource when the child dies. The worker keeps claiming, renewing,
+ and recording; handler-error semantics (unrecoverable → dead, rate-lease →
+ no attempt burned, backoff otherwise) are preserved across the boundary.
+ Worker shutdown gives children the drain window to finish and report — a
+ routine deploy never burns a job attempt. Recommended for long-running
+ LLM-bound handlers; see the new section in `docs/guides/minions-deployment.md`.
+- **Health-probe verdicts that name the failing layer.** When the worker's DB
+ probe fails, it now disambiguates via the direct session lane and says
+ `pool_starved` ("server IS reachable; the fault is in the
+ transaction-pooler path") or `server_unreachable` — instead of the blanket
+ "DB unreachable" that historically sent operators debugging database
+ capacity while the real fault was client-side. A startup warning also makes
+ single-pool mode (direct-lane kill switch) loud instead of silent, and
+ `docs/guides/queue-operations-runbook.md` gains a verdict-interpretation
+ table.
+- `GBRAIN_POOL_MAX_LIFETIME_S`: explicit client-pool connection max-lifetime
+ knob (0 disables; default stays the per-connection 30–60min jitter).
+
+### Fixed
+- **Timed-out DB probes and lock renewals are now cancelled, not abandoned.**
+ Every place that raced a query against a timer (health probe, minion lock
+ renewal, cycle-drain renewal, submit-time queue probes, DB-lock refresh)
+ previously let the losing query keep running on a checked-out connection —
+ under pool exhaustion each abandoned racer held a slot and made the
+ exhaustion worse, starving the lock heartbeat first. All five sites now
+ abort the query via its cancellation signal so the slot is released.
+- Long-running maintenance holds (index rebuilds, non-transactional
+ migrations, backfill write batches) now reserve from the direct session
+ lane instead of pinning the worker's shared pool — capped so reserved
+ holds always leave a direct-lane slot for the claim/renewal heartbeats,
+ and falling back to the previous behavior when the direct lane is
+ unavailable.
+
+Full operational detail: `docs/guides/minions-deployment.md` (isolation
+sizing: connections, memory, spawn cost) and
+`docs/guides/queue-operations-runbook.md` (probe verdicts).
+
## [0.46.0.0] - 2026-08-14
**Your other agents' sessions become brain knowledge.** Until now only Claude
diff --git a/README.md b/README.md
index d1e450e856..409adb2069 100644
--- a/README.md
+++ b/README.md
@@ -311,7 +311,7 @@ The whole loop is described in [`docs/architecture/topologies.md`](docs/architec
**Self-wiring knowledge graph.** Every `put_page` extracts entity refs from markdown/wikilinks/typed-link syntax and writes edges with zero LLM calls. Typed edges (`attended`, `works_at`, `invested_in`, `founded`, `advises`, `mentions`, …). Multi-hop traversal via `gbrain graph-query`. The graph is what produces the +31.4 P@5 lift over vector-only RAG. **Obsidian-style vaults:** bare `[[note-name]]` wikilinks that point across folders — you wrote `[[struktura]]` but the page lives at `projects/struktura.md` — resolve by basename once you opt in with `gbrain config set link_resolution.global_basename true`. Off by default; `gbrain doctor` tells you how many edges you'd gain before you flip it. See [migrating an Obsidian vault](INSTALL_FOR_AGENTS.md#step-45-wire-the-knowledge-graph).
-**Job queue (Minions).** BullMQ-shaped, Postgres-native job queue. Durable subagents (LLM tool loops that survive crashes via two-phase pending→done persistence), shell jobs with audit, child jobs with cascading timeouts, rate leases for outbound providers, attachments via S3/Supabase storage. Replaces "spawn subagent as fire-and-forget Promise" with something that recovers from anything.
+**Job queue (Minions).** BullMQ-shaped, Postgres-native job queue. Durable subagents (LLM tool loops that survive crashes via two-phase pending→done persistence), shell jobs with audit, child jobs with cascading timeouts, rate leases for outbound providers, attachments via S3/Supabase storage. Opt-in per-job process isolation (`gbrain jobs work --job-isolation process`) runs each claimed job in its own SIGKILL-able child process, so a stuck handler dies for real and a crash takes one job instead of the whole worker; when the worker's DB health probe fails, it names the failing layer (`pool_starved` vs `server_unreachable`) instead of a blanket "DB unreachable". Sizing and rollout guidance in [`docs/guides/minions-deployment.md`](docs/guides/minions-deployment.md); probe-verdict triage in [`docs/guides/queue-operations-runbook.md`](docs/guides/queue-operations-runbook.md). Replaces "spawn subagent as fire-and-forget Promise" with something that recovers from anything.
**Non-English brains (FTS language config).** The Postgres full-text search tokenizer is configurable via `GBRAIN_FTS_LANGUAGE`. Defaults to `english`. Set it to any text-search configuration that exists in your Postgres instance:
diff --git a/TODOS.md b/TODOS.md
index b0dad9de3b..7c7d575852 100644
--- a/TODOS.md
+++ b/TODOS.md
@@ -1,5 +1,87 @@
# TODOS
+## Issues #5+#6 follow-ups (pool starvation + process isolation; plan: ~/.claude/plans/system-instruction-you-are-working-witty-moore.md)
+
+- [ ] **P1-companion — nested-checkout audit + dev-mode detection.** **What:**
+ `transaction()` callers that invoke parent-engine methods (or module helpers
+ taking `engine` not `tx`) take a SECOND read-pool slot while holding the tx
+ slot — e.g. the `operations.ts` advisory-lock loop around `tx.addLink`. Under
+ a saturated pool this is a client-side self-deadlock class. Audit call sites;
+ add a dev-mode warning (e.g. a tx-depth counter consulted by `runUnsafe`).
+ **Why:** the #6 incident's exact 240s-idle sessions were never reproduced
+ under a debugger; this is the strongest remaining candidate — the shipped
+ wave mitigates the starvation class but does not close this path. **Effort:**
+ M. **Priority:** P1-companion.
+- [ ] **P2 — per-handler isolation policy.** **What:** a per-handler-name set
+ (e.g. long-running LLM-bound handlers isolate, sub-second `lint`/`backlinks`
+ stay inline) instead of the all-or-nothing `--job-isolation process`.
+ **Why:** spawn cost (~0.3–1s) is noise for 644s subagent jobs, meaningful
+ for sub-second handlers; one worker should be able to mix. **Context:**
+ `worker.ts` executeJob's `isolated` gate is the seam. **Effort:** M.
+ **Priority:** P2.
+- [ ] **P2 — per-child --max-rss caps.** **What:** RSS watchdog for isolation
+ children (the worker-level watchdog covers the worker only in process mode;
+ a startup note ships today). **Context:** child-job-runner.ts owns the child
+ lifecycle; a poll of the child's RSS + group-kill on breach mirrors the
+ worker watchdog. **Effort:** M. **Priority:** P2.
+- [ ] **P2 — jobs-side connection-budget clamp for isolated workers.** **What:**
+ warn/clamp concurrency when `concurrency × (child pool + 1) + parent pools`
+ exceeds a configured budget (GBRAIN_MAX_CONNECTIONS-style; precedent
+ `sync-concurrency.ts:clampWorkersForConnectionBudget`). **Why:** isolation
+ multiplies pooler CLIENT connections (~73 at concurrency 15); today the
+ budget lives only in docs math. **Effort:** S. **Priority:** P2.
+- [ ] **P3 — --job-isolation pass-through for the autopilot's embedded
+ supervisor.** **What:** `autopilot.ts` builds its own worker args; add the
+ conditional flag there (jobs supervisor already passes through). **Effort:**
+ S. **Priority:** P3.
+- [ ] **P3 — runLockRenewalTick adoption in the cycle drain.** **What:**
+ `synthesize.ts` now uses the minimal `runDrainRenewalTick` (per-call signal +
+ guard); adopting the full tick would add the audit channel + bounded
+ reconnect. **Effort:** S. **Priority:** P3.
+- [ ] **P3 — streaming child progress.** **What:** isolation children report
+ progress via their own token-fenced DB writes today (identical to inline);
+ an IPC stream would only add parent-side visibility (e.g. lifecycle events
+ in `jobs watch`). **Effort:** M. **Priority:** P3.
+- [ ] **P3 — connection-audit release events + plain-idle visibility.**
+ **What:** `logConnectionEvent` never emits `release`, so the JSONL cannot
+ answer "who holds a slot"; and `getIdleBlockers` filters
+ `state='idle in transaction'` only — the #6 incident's plain-`idle` sessions
+ were invisible to it. **Effort:** M. **Priority:** P3.
+- [ ] **P3 — doctor connection_routing check.** **What:** wire
+ `ConnectionManager.describeMode()` + `healthCheck()` (both currently
+ zero-caller outside tests) into a doctor check naming the routing mode,
+ kill-switch state, and per-pool probe latency. Comments in four files
+ already reference this check as if it existed. **Effort:** S.
+ **Priority:** P3.
+- [ ] **P3 — isolation test-gap follow-ups (pre-landing review).** **What:**
+ (a) spawned-CLI negative tests for `jobs run-child` bootstrap guards (PGLite
+ → exit 13; missing job-id/env → exit 13) and for `jobs work` with
+ isolation on + an unresolvable child CLI (fail-fast exit 1) — both need a
+ real engine bootstrap so they live in the e2e lane; (b) a behavioral (not
+ structural) test driving `withRefreshingLock` with a hung injected
+ `handle.refresh` (signal aborted at timeout, no overlapping ticks); (c) a
+ force-evict-skip test for isolation mode (needs the 30s evict window made
+ injectable); (d) operator-flow message tests (verdict-tailored FATAL text,
+ single-pool startup banner). **Why:** the ship coverage audit scored the
+ wave 82% — these are the surviving gaps. **Effort:** M. **Priority:** P3.
+- [ ] **P3 — raceWithAbortTimeout shared helper.** **What:** the
+ "Promise.race a query vs a setTimeout that aborts an AbortController,
+ clearTimeout in finally" pattern now exists at five sites (db-probe
+ withDeadline, synthesize runDrainRenewalTick, lock-renewal-tick callAbort,
+ db-lock tickAbort, supervisor probeAbort), each re-deriving the same
+ invariants. Extract one helper and adopt it. **Effort:** S. **Priority:** P3.
+- [ ] **P3 — lazy handler resolution in run-child.** **What:** every isolation
+ child runs full registerBuiltinHandlers (incl. plugin discovery) to resolve
+ ONE handler; the job name is known from the row — a resolve-by-name path
+ would skip discovery for builtins. Matters only if isolation is ever used
+ for short jobs (documented as not the target). **Effort:** S. **Priority:** P3.
+- [ ] **P3 — full checkout instrumentation via a Sql proxy.** **What:** the
+ CheckoutGauge covers raw/direct/reserved/tx seams only; tagged-template
+ traffic (most engine load) is untracked. A proxy around the postgres.js Sql
+ callable could count real checkouts — investigate cost/fragility before
+ building. **Why:** would turn the probe's "tracked subset" caveat into full
+ coverage. **Effort:** M. **Priority:** P3.
+
## Security-process follow-ups (filed with Wave −1 of the fix-wave campaign, 2026-08-14)
- [ ] **P2 — Vulnerability disclosure policy.** **What:** a written disclosure
@@ -254,14 +336,6 @@ fix-now findings landed on the branch; these four are the review-deferred tail.
`requestToolsPersistLimiter`; the surface_change audit rows already give
a DB-side count to enforce against if needed. **Effort:** medium.
**Priority:** P3.
-- [ ] **P3 — cancel (not just abandon) timed-out submit-time queue probes.**
- **What:** the WP5 wedge/pause probes time-bound via Promise.race, but the
- losing query keeps running on the pool after the race resolves. Wire
- AbortSignal / statement_timeout so a slow probe releases its slot. **Why:**
- under pool exhaustion (the exact regime the probes exist to detect) an
- abandoned probe query holds a pooler slot and makes the exhaustion worse.
- **Context:** `src/core/minion/supervisor.ts` queryWedgeSignals callers in
- `src/core/operations.ts` submit paths. **Effort:** small. **Priority:** P3.
- [ ] **P3 — document the status --json snapshot union under schema_version.**
**What:** a short protocol note (docs/progress-events.md sibling) pinning
the `get_status_snapshot` v2 shape as a discriminated union on
diff --git a/VERSION b/VERSION
index 970690b2b2..288ba78bcf 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.46.0.0
\ No newline at end of file
+0.46.1.0
diff --git a/docs/TESTING.md b/docs/TESTING.md
index b7097c661f..091595f5a2 100644
--- a/docs/TESTING.md
+++ b/docs/TESTING.md
@@ -334,6 +334,18 @@ Unit tests and what they cover:
- `test/enrichment-service.test.ts` — entity slugification, extraction, tier escalation.
- `test/data-research.test.ts` — recipe validation, MRR/ARR extraction, dedup, tracker parsing, HTML stripping.
- `test/minions.test.ts` — Minions job queue: CRUD, state machine, backoff, stall detection, dependencies, worker lifecycle, lock management, claim mechanics, depth/child-cap, timeouts, cascade kill, idempotency, `child_done` inbox, attachments, removeOnComplete/Fail, `max_stalled` clamp/default/plumbing coverage.
+- `test/minion-queue-renewlock-signal.test.ts` — `renewLock` forwards its optional AbortSignal to `executeRawDirect` (stub-engine capture); legacy 3-arg calls unchanged; token-fence miss returns false.
+- `test/cycle-drain-renewal.test.ts` — `runDrainRenewalTick` (cycle drain): per-call signal aborted on timeout (slot released), onLost once on a lost fence, throws swallowed, hung renewal resolves at the deadline.
+- `test/queue-probe-cancellation.test.ts` — `probeQueueState`/`queryWedgeSignals` signal threading: the 1500ms budget CANCELS the losing probe query; fast-path signals never abort; throw still collapses to `{probe_failed: true}`.
+- `test/db-pool-max-lifetime.test.ts` — `resolveMaxLifetimeSeconds`: env forms, 0-disables, 30–60min jitter bounds, warn-once on invalid, per-call jitter variance.
+- `test/pool-gauge.test.ts` — `CheckoutGauge` pure semantics + the PostgresEngine seams with fake pools: counted while in flight, released on resolve, on REJECTED queries, and on the SYNCHRONOUS pre-aborted-signal throw (leak guards); `getPoolDiagnostics` fail-open.
+- `test/db-probe.test.ts` — `runDbProbe` verdict matrix (pool_starved / server_unreachable / unknown), honest-disjunction + no-waiter-arithmetic wording pins, hung probes cancelled via their signals, diagnostics absent/throwing fail open.
+- `test/postgres-engine-reserved-routing.test.ts` — `withReservedConnection` routing: direct pool when dual-pool active, read pool when kill-switched/in-tx, semaphore cap (directPoolSize−1) with read-pool overflow, permit released on fn throw and reserve failure.
+- `test/job-isolation-protocol.test.ts` — outcome-file codec round-trip + every decode failure path (missing/malformed/oversize→UnrecoverableError; byte counts, never content), handler-error instanceof reconstruction, child-CLI invocation resolution, and REAL detached-process `killProcessGroup` tests incl. the grandchild-death guarantee (exercises the Bun negative-pid `/bin/kill` fallback for real under `bun test`).
+- `test/run-child-entry.test.ts` — `runChildJobEntry` on real in-memory PGLite with a REAL claim-minted token: success (fenced updateProgress lands), handler-failure outcome (exit 0), token-mismatch never runs the handler (exit 14), missing job/handler, parent-death watchdog aborts a live handler.
+- `test/child-job-runner.test.ts` — `runJobInChild` against real .mjs children: success + full env contract (incl. `GBRAIN_DIRECT_POOL_SIZE=1`), error/lease outcome reconstruction, crash, SIGTERM-ignorer → group SIGKILL at the injected grace, pre-aborted signal, spawn ENOENT → `ChildSpawnInfraError`, worker-shutdown drain (report-during-drain completes; non-reporting kill → `ChildWorkerShutdownError`).
+- `test/worker-job-isolation.test.ts` — full parent path on PGLite with the `fake-run-child.mjs` fixture: claim → child → fenced completeJob (real token over env), error outcome → failJob, crash burns the attempt, spawn failure RELEASES with zero attempts burned, and the codex-2 #8 serialization-parity pin (unreportable results fail in BOTH modes, never falsely complete).
+- `test/jobs-isolation-flag.test.ts` — `parseJobIsolationFlag`: space/= forms, env fallback + flag-wins, empty-env default, other flags untouched.
- `test/extract.test.ts` — link extraction, timeline extraction, frontmatter parsing, directory type inference.
- `test/extract-db.test.ts` — `gbrain extract --source db`: typed link inference, idempotency, `--type` filter, `--dry-run` JSON output.
- `test/extract-fs.test.ts` — `gbrain extract --source fs`: first-run inserts + second-run reports zero, dry-run dedups candidates across files, second-run perf regression guard for the N+1 dedup bug.
@@ -400,6 +412,7 @@ E2E tests live in `test/e2e/` and run against real Postgres+pgvector (require `D
- `test/e2e/sync.test.ts` — `--skip-failed` failure-loop test alongside happy-path tests: broken file → `performSync` returns `blocked_by_failures` with grouped breakdown → `performSync({skipFailed: true})` advances bookmark and returns `AcknowledgeResult` with code summary → second broken file → second cycle. Saves and restores the user's real `~/.gbrain/sync-failures.jsonl` so the test is hermetic. Asserts bookmark gating, JSONL state, dedup across paths, summary aggregation, and the literal doctor-rendering string format.
- `test/e2e/upgrade.test.ts` — check-update against real GitHub API (network required).
- `test/e2e/minions-shell-pglite.test.ts` — PGLite `--follow` inline shell-job path (in-memory, no `DATABASE_URL` required) — the path the minion-orchestrator skill documents for dev use.
+- `test/e2e/job-isolation.test.ts` — process isolation on real Postgres (DATABASE_URL-gated, wired EXPLICITLY into `.github/workflows/e2e.yml` tier1 — the workflow runs only named files): a concurrency-3 isolated drain through real child processes (the `fake-run-child.mjs` fixture — real spawns, no child DB pools), and the REAL `jobs run-child` CLI entrypoint end-to-end (engine bootstrap incl. the child's own pools, quiet handler registry, token validation, outcome protocol).
- `test/e2e/pglite-cli-exit.serial.test.ts` — real spawned-CLI exit behavior on PGLite (in-memory, no `DATABASE_URL`): read commands (`search`/`get`/`query`) exit 0 promptly; CLI_ONLY `capture` exits clean and frees the single-writer lock; the `#2084` describes pin every swept disconnect site — a failed op exits 1 with the error on stderr, and the dashboard, read-only-timeout, doctor, and `dream --dry-run` paths all exit with no force-exit banner.
- `test/e2e/pgbouncer-teardown.test.ts` — PgBouncer TRANSACTION-mode teardown (#2084 / the #1972→#2015→#2084 class). Pins the bug CLASS, not timings: a CLI op against a txn-mode pooled URL exits 0 with intact stdout and does NOT ride the 10s hard-deadline backstop (the `engine.disconnect() did not return` banner is the smoking gun — pre-#2084 it printed on 100% of query-shaped ops). Gated by `GBRAIN_PGBOUNCER_URL` + `GBRAIN_PGBOUNCER_DIRECT_URL` (NOT `DATABASE_URL`) — set automatically by `bun run ci:local`'s `pgbouncer` compose service; skips gracefully elsewhere. Uses a DEDICATED `gbrain_pgbouncer` database so it never races the `gbrain_test` TRUNCATE fixtures.
- `test/e2e/volunteer-context-postgres.test.ts` — `volunteer_context` on REAL Postgres (#2095; engine parity beyond the hermetic PGLite unit suite): resolution arms through the actual op handler, the fire-and-forget volunteer-event sink landing rows, the stats join, and the RLS pin that `context_volunteer_events` has ROW LEVEL SECURITY enabled (keeps the v35 auto-RLS event trigger honest for migration-created tables). `DATABASE_URL`-gated.
diff --git a/docs/architecture/KEY_FILES.md b/docs/architecture/KEY_FILES.md
index 435b33145b..50a5e1b80c 100644
--- a/docs/architecture/KEY_FILES.md
+++ b/docs/architecture/KEY_FILES.md
@@ -33,7 +33,7 @@ per-release `**vX.Y.Z:**` narration — CI enforces this
- `src/core/pglite-repair.ts` — WAL-repair orchestrator wrapping the resetWal port with the safety layers that make it runnable automatically from `connect()`: `validateWalRepairTarget` (read-only, fail-closed; refuses symlinked dataDir/pg_wal/`global`/pg_control — lstat follows INTERMEDIATE symlinks, so `global/` itself must be checked or surgery would write pg_control through it into a foreign dir; tolerates the in-dir `.gbrain-lock`), rename-based backup (the ENTIRE `pg_wal/` dir + postmaster.pid renamed into a sibling `<dataDir>.wal-repair-backup-<ts>/`, only the 8KB pg_control copied — zero transient disk cost), `restoreWalBackup` (overwrite order: control first via atomic tmp+rename, then a pg_wal dir swap with the reset dir set ASIDE inside the backup — nothing is ever deleted during restore; mtime guard refuses when a foreign segment is newer than the backup; a missing/empty backup NEVER reports `restored:true`), `WalRepairError` (thrown when resetWal fails AFTER the backup — carries the receipt + the best-effort restore's REAL result so the seam's `restored` flag and the `failed-restored`/`failed-not-restored` message arms never lie), a cooldown sidecar `<dataDir>.wal-repair-attempt.json` (skip `'recently-failed'` inside `GBRAIN_PGLITE_WAL_REPAIR_COOLDOWN_SECONDS`, default 3600 — bounds the autopilot/supervisor reconnect loops) with episode-scoped backups (attempts within one corruption episode REUSE the episode's first backup — the pre-damage forensic state, honored only when the sidecar's path is a real non-symlink `<dataDir>.wal-repair-backup-*` sibling since the sidecar is user-writable JSON; retention keeps the newest 3 episodes, never pruning the open episode's), and `attemptWalRepairAndRetry` — the engine seam that NEVER throws (gates: kill-switch → reaped-lock `'possibly-live-writer'` → 10-minute reap-marker quarantine (`msSinceLastReap`, cross-process) → validation → cooldown; then repair → retry create once → restore-and-record on failure; prints a repair-start stderr line so a timeout-killed attempt is self-explaining). `inspectPgliteDataDir` is the read-only diagnosis for `gbrain doctor` + `pglite-repair --dry-run`. Imports runtime values only from `pglite-lock.ts`/`pglite-resetwal.ts`/node:fs — never from `pglite-engine.ts` (no cycle; the engine statically imports THIS file per the #3596 engine-live rule). Pinned by `test/pglite-repair.test.ts` + `test/pglite-wal-repair.serial.test.ts`.
- `src/commands/pglite-repair.ts` — `gbrain pglite-repair`: the manual surface for WAL repair (`--dry-run | --yes | --json | --path <dir>`; CLI_ONLY + SELF_HELP; returns an exit code via `setCliExitVerdict`, never `process.exit`). Never connects an engine — works when the DB won't open and when auto-repair is disabled. `--dry-run` is strictly read-only. Its confirmation prompt (and `src/commands/reinit-pglite.ts`'s) writes to stderr so `--json` stdout stays clean, refuses non-TTY stdin in-prompt (defense-in-depth behind the caller-side "Non-TTY environment requires --yes" guard), resolves `false` on EOF/close instead of parking forever on a closed or piped stdin, and cleans up its listeners; `--yes`/`-y` stays the non-interactive path. The real run validates BEFORE locking (`acquireLock` mkdirs the data dir — a typo'd `--path` must not create directories), refuses a live lock holder (pre-lock diagnosis names the PID; a live `gbrain serve` is called out), refuses a reaped acquisition (`refused_reaped_lock` — no `--force` by design: force-removing `.gbrain-lock` would reopen the #2348 concurrent-writer hole), re-validates under the lock, repairs with episode-backup reuse, and records the attempt in the sidecar. Pinned by `test/pglite-repair-command.serial.test.ts`.
- `src/commands/doctor.ts` — `gbrain doctor [--json] [--fast] [--fix] [--dry-run] [--index-audit]`: health checks. Checks include `jsonb_integrity` + `markdown_body_completeness` (reliability), `schema_version` (fails loudly when `version=0`, routes to `gbrain apply-migrations --yes`), `queue_health` (Postgres-only: stalled-forever active jobs started_at > 1h, waiting-depth-per-name > threshold default 10 via `GBRAIN_QUEUE_WAITING_THRESHOLD`, and dead-lettered subagent jobs with `last_error` matching the `prompt_too_long` classifier in last 24h), `sync_failures` (`[CODE=N, ...]` breakdown for unacked-warn + acked-ok; severity comes from the shared `decideSyncFailureSeverity` in `src/core/sync-failure-ledger.ts` so the LOCAL and REMOTE/thin-client doctor surfaces can never drift — a stuck bookmark escalates to FAIL once an OPEN failure has blocked past the staleness window or ≥10 files block, while already `auto_skipped` rows stay a visible WARN), `rls_event_trigger` (healthy `evtenabled` set is `('O','A')` only; fix hint `gbrain apply-migrations --force-retry 35`), `graph_coverage` (short-circuits to ok when `SELECT COUNT(*) FROM pages WHERE type IN ('entity','person','company','organization')` returns 0; WARN hint is `gbrain extract all`), `embedding_column_registry` (probes each declared column via Postgres `format_type(atttypid, atttypmod)` to catch dim mismatch with a paste-ready `gbrain config set embedding_columns '{...}'` hint, probes HNSW index presence via `pg_indexes`, computes default-column population via `COUNT(*) FILTER (WHERE <col> IS NOT NULL) / COUNT(*)` warning below 90% except empty brains where chunk_count=0 short-circuits to ok; PGLite parity via `executeRaw`), and `skill_brain_first` (walks SKILL.md via `autoDetectSkillsDirReadOnly`, calls `analyzeSkillBrainFirst()` from `src/core/skill-brain-first.ts` per file with structured `Check.issues[]`; warn states `missing_brain_first`/`brain_first_typo`, ok states `compliant_callout`/`compliant_phase`/`compliant_position`/`exempt_frontmatter`/`no_external`; snapshot+diff audit at `~/.gbrain/audit/skill-brain-first-YYYY-Www.jsonl`). `--fix` delegates inlined cross-cutting rules to `> **Convention:** see [path](path).` callouts via `src/core/dry-fix.ts` (and MISSING_RULE_PATTERNS for the brain-first callout); `--fix --dry-run` previews. `--index-audit` (Postgres-only, informational, no auto-drop) reports zero-scan indexes from `pg_stat_user_indexes`. Every DB check runs under a progress phase; `markdown_body_completeness` runs under a 1s heartbeat. `runDoctor` uses `autoDetectSkillsDirReadOnly` (from `src/core/repo-root.ts`; install-path fallback so `cd ~ && gbrain doctor` finds bundled skills); `--fix` carries a D6 install-path safety gate that refuses auto-repair when `detected.source === 'install_path'` (would rewrite the bundled tree). The Lane D supervisor check at `doctor.ts:1011-1043` consumes `summarizeCrashes(events)` from `src/core/minions/handlers/supervisor-audit.ts` (warn at `>=1` real crash; ok message has `clean_exits_24h=N`; warn message has `runtime=A oom=B unknown=C legacy=D` per-cause breakdown) so OOM/runtime/unknown crashes are distinguishable from clean code=0 worker drains; cross-surface parity with `gbrain jobs supervisor status` is pinned by source-grep wiring assertions requiring the breakdown substrings in BOTH `doctor.ts` and `jobs.ts`. `checkSyncFreshness` (exported, in `runDoctor` local + `doctorReportRemote` thin-client) is a staleness probe: warns at 24h, fails at 72h or never-synced; future-`last_sync_at` warns ("clock skew") instead of falling through ok; env overrides `GBRAIN_SYNC_FRESHNESS_WARN_HOURS`/`GBRAIN_SYNC_FRESHNESS_FAIL_HOURS` (invalid fall back with once-per-process stderr warn via `_resolveSyncFreshnessHours`); failure messages embed `source.id` so the printed `gbrain sync --source <id>` matches. A source holding a LIVE, non-expired per-source sync lock (`inspectLock(engine, syncLockId(source.id))` from `src/core/db-lock.ts`) is reported as actively syncing (the message names the holder pid + host) and counted in `synced_recently_count`, NOT flagged stale — the live lock is the only honest in-progress signal (checkpoint banking can't distinguish in-progress from wedged: a blocked sync banks its files but writes no anchor). A blocked/failed sync's process has exited (no lock row) and a wedged holder stops refreshing (TTL lapses), so either falls through to the stale path and is never masked; the dynamic `db-lock` import is swallowed to a no-op on a stub engine or pre-lock-table brain, so this can only ADD an in-progress verdict, never suppress a real stale one. The in-progress note is appended to whatever verdict the buckets produce and is empty when nothing is syncing, so steady-state messages stay byte-for-byte unchanged. It has a `localOnly`-gated git short-circuit (`runDoctor` passes `localOnly: true`; `doctorReportRemote` runs in the HTTP MCP server `src/commands/serve-http.ts` and keeps default `false` so that path never walks DB-supplied `local_path` via subprocess — trust boundary). The local predicate mirrors sync's "do work?" gate (HEAD == `last_commit` AND working tree clean via `requireCleanWorkingTree: 'ignore-untracked'` so a quiet repo with only untracked dirs is `unchanged` not SEVERE, AND `chunker_version === CURRENT`); the inline SELECT carries `last_commit + chunker_version + newest_content_at`. The REMOTE path computes lag via `lagFromContentMs(newest_content_at, lastSync, now)` from the stored column, NO git subprocess; LOCAL fall-through and the `< 0` clock-skew check stay on raw wall-clock. Three-bucket count math populates `Check.details = {unchanged_count, synced_recently_count, stale_count}` with the invariant `sum === sources.length`. `checkCycleFreshness` is DELIBERATELY NOT git-short-circuited or content-relativized (`last_commit == HEAD` can't answer "did the full cycle complete?"; a sync can succeed while later cycle phases fail; different axis `last_full_cycle_at`). Pinned by `test/doctor.test.ts` (incl. IRON-RULE regression banning stale verb names, the sync_freshness boundary matrix, the D4 regression guard verifying git probes are NEVER called when `localOnly` is unset/false, the three-bucket invariant, and the untracked-folders / remote-never-shells-out trust-boundary cases). `pglite_data_dir` check: fs-only check that runs when a PGLite brain FAILS to connect (`!fastMode && !engine && config.engine === 'pglite'`, placed after `orphan_clones`, before the DB-checks gate): `computePgliteDataDirCheck(dataDir, diagnosis)` (exported pure fn, `computeWorkerOomLoopCheck` convention) maps the `inspectPgliteDataDir` verdict to a Check — corruption-likely/looks-healthy-but-unopenable/unsupported-layout → `fail` naming `gbrain pglite-repair --dry-run`/`--yes` or the rebuild path, live-lock/missing-dir → `warn`; all `remediation_status: 'human_only'` (Minion remediation needs the DB that is down). Escalates when ≥2 repair attempts failed inside 7 days (unclean-shutdown genesis still active → engine-switch pointer) and reports retained backup-dir inventory (orphan_clones disk-visibility class). Registered in `doctor-categories.ts` OPS_CHECK_NAMES. Pinned by `test/doctor-pglite-datadir.test.ts`. silent-failure batch (#2250/#2784/#2788): `content_hash_duplicates` (single GROUP BY over `(source_id, content_hash)` with FILTER aggregates — never N² — flagging hash groups that hold BOTH a bare and a path-prefixed slug, the wrong-import-root pattern; warn carries sample pairs + the `pages delete` → `purge-deleted --older-than 0` remediation); `undeclared_db_only_pages` (per source with a local repo: markdown pages with no backing file outside every declared + derive-phase-default db_only prefix — the one check deliberately allowed to stat the repo); `db_only_collector_collision` (configured recipe `output_paths` inside a declared db_only dir — auto-gitignore means sync AND import silently skip the collector's files; same warning fires in sync's `manageGitignore` at config-write time). All warn-level, engine-parity pinned by `test/e2e/doctor-silent-death-parity.test.ts`; units in `test/doctor-silent-death-checks.test.ts`. `graph_signals_coverage` check wired into both `runDoctor` (local) and `doctorReportRemote` (HTTP/JSON thin-client path). Reads `search.graph_signals` config first, falls back to mode default; silent `ok` when disabled. Computes inbound link coverage on the page set; warns at <10% with `gbrain extract all` fix hint; `ok` at ≥30% ("fire on most queries") and 10-29% ("fire occasionally"), each with the percentage embedded. Pinned by cases in `test/doctor.test.ts`. `subagent_provider` check (layer 3 of 3). Resolves subagent model config in runtime order (`models.subagent` > `models.default` > `models.tier.subagent` > built-in default) and warns when the selected model lacks native tool-loop capability (message names the bad value + paste-ready fix `gbrain config set models.tier.subagent anthropic:claude-sonnet-4-6`); also warns when `models.default` would sneak `subagent` into a non-Anthropic provider via tier inheritance. OK when subagent tier resolves to Anthropic. Tests in `test/doctor.test.ts`. `computeWorkerOomLoopCheck(engine)` is the single authoritative OOM-loop signal, unioning supervised `summarizeCrashes(readRecentSupervisorEvents(24)).by_cause.rss_watchdog` (cross-week read via `readRecentSupervisorEvents` so a Monday window can't lose Sunday) + bare-worker `minion_jobs error_text='aborted: watchdog'` count (Postgres-only; the same source `queue_health` subcheck 3 reads). Cap comes from the latest `rss_watchdog_loop` breaker alert's `max_rss_mb`, else `resolveDefaultMaxRssMb()` fallback. fail at breaker-tripped or oomKills≥5, warn at ≥1, null otherwise. `computePoolReapHealthCheck(engine)` is the Postgres-only `pool_reap_health` check reading `readRecentPoolRecoveries(1)` — fail when reconnect failures>0 (reconnect throwing is the actionable signal), warn at ≥10 reaps/hr (pooler thrash), null otherwise. Both registered in `buildChecks` after the `supervisor` block. The `supervisor` causeStr carries `rss=N (see worker_oom_loop)` and `queue_health`'s watchdog message cross-references `worker_oom_loop`. `DoctorReport.top_issues` + the cause-ranked render header. `worker_oom_loop` + `pool_reap_health` registered under ops in `doctor-categories.ts`. Pinned by `test/doctor-worker-oom-loop.test.ts`, `test/doctor-pool-reap-health.test.ts`. `supervisor_singleton` check (#1849), a SEPARATE check from `supervisor` (same split precedent as the niceness check) so a singleton-divergence warn can't clobber the crash/liveness precedence. Runs only when a `started` supervisor event was seen in the last 24h and a live engine is available. Reads the queue-scoped DB lock row (`gbrain_cycle_locks WHERE id = supervisorLockId(queue)`) and compares the lock holder (`holder_host:holder_pid`) against the local pidfile holder via the pure `classifySupervisorSingleton`. `mismatch` → warn (a second supervisor may be running with a different `--max-rss`; message names both holders, the effective cap from the `started` event's `max_rss_mb`, and the fix `gbrain jobs supervisor stop`); `single` → ok (names holder + cap); `no_lock` → no check emitted. Best-effort try/catch (silent skip on brains without the lock table). Registered under ops in `doctor-categories.ts` as `supervisor_singleton`. Pinned by `test/supervisor-db-lock.test.ts` + `test/doctor.test.ts`. `checkBatchRetryHealth`: `batch_retry_health` check surfacing Supavisor circuit-breaker incidents. Wired into both `runDoctor` (local) and `doctorReportRemote` (thin-client). Reads last 24h. States: `ok` (zero exhausted in 24h OR <3 from a single site), `warn` (>=3 same-site OR >=5 cross-site), `fail` (>=20 sustained breaker). Surfaces bad `GBRAIN_BULK_*` env at doctor startup. Corrupt-JSONL tolerant. Paste-ready fix hints in every warn/fail message. Also reads `readRecentDbDisconnects(24)` and appends `Disconnect-call audit: N call(s) in 24h (most recent caller: <frame>).` to ALL three message paths so connection-incident signal is greppable from one `gbrain doctor --json` call (module-import wrapped in try/catch so older brains without the audit file degrade silently). Pinned by `test/doctor-batch-retry.test.ts` (10 cases). three checks wired into `runDoctor()` and the JSON envelope, all warn-only with paste-ready fix hints. (1) `checkSourceRoutingHealth(engine)` scans up to 200 pages on federated brains and flags pages whose `source_id` doesn't match what `resolveSourceWithTier()` would have picked for their `source_path`; single-source brains short-circuit to `ok`; the 200-page cap is total across the brain so doctor stays under 5s. (2) `checkOauthConfidentialHealth(engine)` probes registered confidential clients for `/token` reachability. (3) `checkAutopilotLockScope()` (pure, no engine) compares the resolved lock path to `$GBRAIN_HOME`; warns when set but the lock lives elsewhere, with a PID-safe inspection hint (`kill -0 <pid>` before deletion). Pinned by `test/doctor-v0_37_7_checks.test.ts`. `buildChecks(engine, args, dbSource): Promise<Check[]>` exported as a test seam. `runDoctor` is a thin wrapper: `buildChecks → computeDoctorReport → render + process.exit`. All 10 `process.exit` sites stay in the wrapper; the two early-return paths (no engine, connection failure) return partial check lists instead of inline exits (observable output identical). Pinned by `test/doctor-behavioral.test.ts` (13 cases: pure aggregation math over `computeDoctorReport`, orchestrator cases for `--fast` skip set + `--json` flag + no-engine partial path + snapshot of load-bearing check names) and `test/doctor-cli-smoke.serial.test.ts` (1 subprocess case spawning `bun run src/cli.ts doctor --json` against a fresh PGLite tempdir, asserting schema_version=2 envelope, status enum, non-empty checks array — the render-path coverage buildChecks-only tests miss; quarantined `.serial` because PGLite write-locks don't play with parallel runners). three checks wired into `runDoctor()` and the JSON envelope: `oversized_pages` (warns on pages exceeding `content_sanity.bytes_warn`), `scraper_junk_pages` (warns on live DB pages matching any junk pattern that escaped ingest), and `content_sanity_audit_recent` (reads the last 7 days of audit events, aggregates by pattern+source). Default scans the 1000 most-recent pages; `--content-audit` opts into a full scan. All three warn-only with paste-ready fix hints (junk → `gbrain sources audit <id>` + `git rm` source-of-truth, oversize → split or accept). two checks wired into `runDoctor()` + the JSON envelope: `quarantined_pages` (counts pages carrying the `quarantine` marker via `engine.executeRaw` JSONB `?` existence, works on PGLite + Postgres; warn-only with a `gbrain quarantine list` hint) and `flagged_pages` (counts `content_flag` pages — searchable but odd; warn-only). Both skip gracefully (status ok, "Skipped") on engines/brains where the probe errors. Pinned by `test/doctor.test.ts`. `home_dir_in_worktree`: filesystem check walking up from `gbrainPath()` toward `$HOME` looking for a `.git` directory (main repo) or `.git` file (linked worktree pointer; Conductor + git-worktrees topology). Walk terminates at `$HOME` so a `.git` above the user's home doesn't false-positive. Honors `GBRAIN_HOME` (appends `.gbrain` to the override). Warn (not fail) with worktree-root path + paste-ready fix pointing at `GBRAIN_HOME` override or moving the brain. `--remediation-plan [--json] [--target-score N]` prints what would run (stable `id`, `idempotency_key`, `severity`, `est_seconds`, `est_usd_cost`, `depends_on`); `--remediate [--yes] [--target-score N] [--max-usd N]` submits each plan step as a Minion job in dependency order, re-checking score between steps. `--target-score N` defaults to 90; refuses to start when target exceeds `maxReachableScore()` and lists what's missing. `--max-usd N` is the cron-safety guard — submission refuses when the plan's `est_total_usd_cost` exceeds the cap. JSON envelope adds a `Check.remediation` field (additive, schema_version unchanged). Pinned by tests in `test/doctor.test.ts`. 4 checks: `abandoned_threads`, `calibration_freshness`, `grade_confidence_drift` (mitigation surface; math ships later), `voice_gate_health`.
-- `src/core/postgres-engine.ts` — Postgres + pgvector implementation (Supabase / self-hosted). `addLinksBatch`/`addTimelineEntriesBatch`/`addTakesBatch` pass the batch as one JSONB document — `INSERT ... SELECT FROM jsonb_to_recordset(($1::jsonb)->'rows') AS v(...) JOIN pages ...` bound through `executeRawJsonb({ rows })` — which encodes arbitrary free text safely (the old `unnest(${arr}::text[])` array-literal path crashed Postgres with "malformed array literal" on calendar/Zoom context, gbrain#1861) and sidesteps the 65535-parameter cap; takes declares native recordset column types (`page_id int, weight real, active boolean, …`) so no per-element casts; all three are `batchRetry`-wrapped. `searchKeyword`/`searchVector` scope `statement_timeout` via `sql.begin` + `SET LOCAL` so the GUC dies with the transaction instead of leaking across the pooled postgres.js connection. `getEmbeddingsByChunkIds` uses `tryParseEmbedding` so one corrupt row skips+warns instead of killing the query. `searchKeyword`/`searchKeywordChunks`/`searchVector` apply source-aware ranking by inlining the source-factor CASE and `NOT (col LIKE …)` hard-exclude from `src/core/search/sql-ranking.ts`; `searchVector` is a two-stage CTE (HNSW-safe inner ORDER BY, source-boost re-rank in outer SELECT) carrying `p.source_id` inner→outer. `_savedConfig` retains the connect config; `reconnect()` tears down + recreates the pool from saved config (called by supervisor watchdog after 3 consecutive health-check failures, and by `batchRetry` on a retryable connection error). Concurrent callers share one in-flight `_reconnectPromise` (they await the single reconnect rather than racing a half-rebuilt pool); ownership re-samples through the atomic `db.connect()` token on the connect leg. `reconnect(ctx?)` accepts the triggering error and records a pool-recovery audit event (`reap_detected`/`reconnect_other`/`reconnect_succeeded`/`reconnect_failed`) for the `pool_reap_health` doctor check. `executeRaw` is a single-statement passthrough — no per-call retry (unsound for non-idempotent statements; recovery is supervisor-driven). `connect()` applies `resolveSessionTimeouts()` from `db.ts` as connection-time startup parameters (`statement_timeout`, `idle_in_transaction_session_timeout`) so orphan pgbouncer backends can't hold locks for hours. `countStaleChunks()`+`listStaleChunks()` server-side-filter on `embedding IS NULL` for `embed --stale` (eliminates ~76 MB/call client-side pull); `upsertChunks()` resets both `embedding` AND `embedded_at` to NULL when chunk_text changes without a new embedding. `initSchema()` calls `applyForwardReferenceBootstrap()` BEFORE replaying SCHEMA_SQL on the same probe set as PGLite (extended for column-only forward-reference cases: `files.source_id`, `files.page_id`, `oauth_clients.source_id`, `oauth_clients.federated_read`, `sources.archived`/`archived_at`/`archive_expires_at`, `timeline_entries.event_page_id`); the entire probe path runs on the DDL connection threaded from `initSchema` (closing a concurrent-bootstrap race for Supabase pooler users); closes #1018/#974/#820. `disconnect()` is idempotent — `_connectionStyle` tracks whether the engine owns its pool (worker engines) or shares the module-level singleton; second call on an instance-pool engine is a no-op rather than clobbering the singleton; and a module-style engine only calls `db.disconnect()` when it owns the singleton (`_ownsModuleSingleton`, set from the `db.connect()` creation token), so a borrower probe engine's teardown leaves the cycle owner's connection intact. Pinned by `test/e2e/postgres-engine-disconnect-idempotency.test.ts` + `test/postgres-engine-singleton-ownership.test.ts`. `getBrainScore` empty-brain parity with PGLite — 100/100 with breakdown 35/25/15/15/10 when `pageCount === 0` (both engines must agree to keep `doctor-report-remote.serial.test.ts` deterministic). Implements `deletePages(slugs, {sourceId}): Promise<string[]>` via `DELETE FROM pages WHERE slug = ANY($1::text[]) AND source_id = $2 RETURNING slug` (single round-trip; caller chunks); `resolveSlugsByPaths` does `SELECT slug, source_path FROM pages WHERE source_path = ANY($1::text[]) AND source_id = $2`; FK cascades through `content_chunks`/`links`/`tags`/`raw_data`/`timeline_entries`/`page_versions`, `files.page_id`+`links.origin_page_id` go SET NULL; throws when input exceeds `DELETE_BATCH_SIZE` (from `src/core/engine-constants.ts`); both short-circuit on empty input. Implements the embedding-signature stale-detection quartet (`sumStaleChunkChars`, `setPageEmbeddingSignature`, `invalidateStaleSignatureEmbeddings`, widened `countStaleChunks`, all accept optional `signature` extending "stale" to model/dims-swap drift via the `pages.embedding_signature` JOIN, NULL grandfathered; the `embedding IS NULL` server-side filter is preserved as the no-signature fast path); Pinned by `test/e2e/engine-parity.test.ts`. Retry classifiers, ontology/recency helpers, and disconnect/pool-recovery audit writers bind statically; only the two `ai/gateway.ts` fallback lookups stay lazy and line-marked, in parity with PGLite. `insertFact` + `insertFacts` no longer hardcode `tx.unsafe(\`'${embedLit}'::vector\`)` for the embedding column. `resolveFactsEmbeddingCast()` (private) probes `pg_attribute` once per engine instance (cached in `_factsEmbeddingCastSuffix`) and returns `'::halfvec'` when migration v40 created the column as halfvec, else `'::vector'`; both insert paths use the cached suffix so the cast matches the actual column type (works on older pgvector that lacks implicit auto-cast). Test seam `__resetFactsEmbeddingCastCacheForTest()` clears the per-engine cache.
+- `src/core/postgres-engine.ts` — Postgres + pgvector implementation (Supabase / self-hosted). `addLinksBatch`/`addTimelineEntriesBatch`/`addTakesBatch` pass the batch as one JSONB document — `INSERT ... SELECT FROM jsonb_to_recordset(($1::jsonb)->'rows') AS v(...) JOIN pages ...` bound through `executeRawJsonb({ rows })` — which encodes arbitrary free text safely (the old `unnest(${arr}::text[])` array-literal path crashed Postgres with "malformed array literal" on calendar/Zoom context, gbrain#1861) and sidesteps the 65535-parameter cap; takes declares native recordset column types (`page_id int, weight real, active boolean, …`) so no per-element casts; all three are `batchRetry`-wrapped. `searchKeyword`/`searchVector` scope `statement_timeout` via `sql.begin` + `SET LOCAL` so the GUC dies with the transaction instead of leaking across the pooled postgres.js connection. `getEmbeddingsByChunkIds` uses `tryParseEmbedding` so one corrupt row skips+warns instead of killing the query. `searchKeyword`/`searchKeywordChunks`/`searchVector` apply source-aware ranking by inlining the source-factor CASE and `NOT (col LIKE …)` hard-exclude from `src/core/search/sql-ranking.ts`; `searchVector` is a two-stage CTE (HNSW-safe inner ORDER BY, source-boost re-rank in outer SELECT) carrying `p.source_id` inner→outer. `_savedConfig` retains the connect config; `reconnect()` tears down + recreates the pool from saved config (called by supervisor watchdog after 3 consecutive health-check failures, and by `batchRetry` on a retryable connection error). Concurrent callers share one in-flight `_reconnectPromise` (they await the single reconnect rather than racing a half-rebuilt pool); ownership re-samples through the atomic `db.connect()` token on the connect leg. `reconnect(ctx?)` accepts the triggering error and records a pool-recovery audit event (`reap_detected`/`reconnect_other`/`reconnect_succeeded`/`reconnect_failed`) for the `pool_reap_health` doctor check. `executeRaw` is a single-statement passthrough — no per-call retry (unsound for non-idempotent statements; recovery is supervisor-driven). `connect()` applies `resolveSessionTimeouts()` from `db.ts` as connection-time startup parameters (`statement_timeout`, `idle_in_transaction_session_timeout`) so orphan pgbouncer backends can't hold locks for hours. `countStaleChunks()`+`listStaleChunks()` server-side-filter on `embedding IS NULL` for `embed --stale` (eliminates ~76 MB/call client-side pull); `upsertChunks()` resets both `embedding` AND `embedded_at` to NULL when chunk_text changes without a new embedding. `initSchema()` calls `applyForwardReferenceBootstrap()` BEFORE replaying SCHEMA_SQL on the same probe set as PGLite (extended for column-only forward-reference cases: `files.source_id`, `files.page_id`, `oauth_clients.source_id`, `oauth_clients.federated_read`, `sources.archived`/`archived_at`/`archive_expires_at`, `timeline_entries.event_page_id`); the entire probe path runs on the DDL connection threaded from `initSchema` (closing a concurrent-bootstrap race for Supabase pooler users); closes #1018/#974/#820. `disconnect()` is idempotent — `_connectionStyle` tracks whether the engine owns its pool (worker engines) or shares the module-level singleton; second call on an instance-pool engine is a no-op rather than clobbering the singleton; and a module-style engine only calls `db.disconnect()` when it owns the singleton (`_ownsModuleSingleton`, set from the `db.connect()` creation token), so a borrower probe engine's teardown leaves the cycle owner's connection intact. Pinned by `test/e2e/postgres-engine-disconnect-idempotency.test.ts` + `test/postgres-engine-singleton-ownership.test.ts`. `getBrainScore` empty-brain parity with PGLite — 100/100 with breakdown 35/25/15/15/10 when `pageCount === 0` (both engines must agree to keep `doctor-report-remote.serial.test.ts` deterministic). Implements `deletePages(slugs, {sourceId}): Promise<string[]>` via `DELETE FROM pages WHERE slug = ANY($1::text[]) AND source_id = $2 RETURNING slug` (single round-trip; caller chunks); `resolveSlugsByPaths` does `SELECT slug, source_path FROM pages WHERE source_path = ANY($1::text[]) AND source_id = $2`; FK cascades through `content_chunks`/`links`/`tags`/`raw_data`/`timeline_entries`/`page_versions`, `files.page_id`+`links.origin_page_id` go SET NULL; throws when input exceeds `DELETE_BATCH_SIZE` (from `src/core/engine-constants.ts`); both short-circuit on empty input. Implements the embedding-signature stale-detection quartet (`sumStaleChunkChars`, `setPageEmbeddingSignature`, `invalidateStaleSignatureEmbeddings`, widened `countStaleChunks`, all accept optional `signature` extending "stale" to model/dims-swap drift via the `pages.embedding_signature` JOIN, NULL grandfathered; the `embedding IS NULL` server-side filter is preserved as the no-signature fast path); Pinned by `test/e2e/engine-parity.test.ts`. Retry classifiers, ontology/recency helpers, and disconnect/pool-recovery audit writers bind statically; only the two `ai/gateway.ts` fallback lookups stay lazy and line-marked, in parity with PGLite. `insertFact` + `insertFacts` no longer hardcode `tx.unsafe(\`'${embedLit}'::vector\`)` for the embedding column. `resolveFactsEmbeddingCast()` (private) probes `pg_attribute` once per engine instance (cached in `_factsEmbeddingCastSuffix`) and returns `'::halfvec'` when migration v40 created the column as halfvec, else `'::vector'`; both insert paths use the cached suffix so the cast matches the actual column type (works on older pgvector that lacks implicit auto-cast). Test seam `__resetFactsEmbeddingCastCacheForTest()` clears the per-engine cache. `withReservedConnection` routes long-hold reserved work (CREATE INDEX CONCURRENTLY, `transaction: false` migration DDL, backfill write batches) to the DIRECT session lane when dual-pool is active, so multi-minute holds stop pinning the worker's shared read pool (the issue #6 starvation class) — never rerouted inside an open transaction (same guard shape as `executeRawDirect`), semaphore-capped below `direct_pool_size` with deliberately NO minimum-1 floor (at `direct_pool_size=1` a floor would let one long reserve consume the only direct session and starve the claim/renewLock heartbeats — the same starvation class reintroduced on the direct lane), overflowing to the shared read pool when the direct lane has no spare capacity or is unavailable; the permit is released on both fn throw and reserve failure. `getPoolDiagnostics()` (duck-typed, no BrainEngine change) surfaces the `CheckoutGauge` in-flight counters from `src/core/pool-gauge.ts` at the raw/direct/reserved/tx seams — a tracked SUBSET (tagged-template traffic is untracked) that `db-probe.ts` labels honestly in health-probe failure lines. Pinned by `test/postgres-engine-reserved-routing.test.ts`.
- `src/core/cjk.ts` — Single source of truth for CJK detection. Exports `CJK_RANGES_REGEX`, `CJK_SLUG_CHARS` (character-class fragment for embedding inside other regexes), `CJK_SENTENCE_DELIMITERS` (`。!?`), `CJK_CLAUSE_DELIMITERS` (`;:,、`), `CJK_DENSITY_THRESHOLD = 0.30`, `hasCJK(s)`, `countCJKAwareWords(s)` (30% density threshold — English docs with one Japanese term stay whitespace-tokenized; Chinese-dominant docs get char-counted), and `escapeLikePattern(s)` (escapes `%`, `_`, `\\` for `ILIKE ... ESCAPE '\\'`). BMP-only ranges (Han / Hiragana / Katakana / Hangul Syllables). Consumers: `expansion.ts`, `sync.ts:slugifySegment`, `operations.ts:validatePageSlug + validateFilename`, `chunkers/recursive.ts:countWords + DELIMITERS`, `pglite-engine.ts:searchKeyword + searchKeywordChunks`.
- `src/core/chunkers/recursive.ts` — base chunker: 300-word chunks, 50-word sentence-aware overlap, 5-level delimiter hierarchy. Lossless invariant: non-overlapping portions reassemble to the original. Also strips the facts fence via `stripFactsFence({keepVisibility:['world']})` so private fact visibility tiers never reach embeddings.
- `src/core/chunkers/semantic.ts` — embedding-based topic-boundary detection: embeds sentences, computes cosine-similarity valleys, smooths with a Savitzky-Golay filter (5-window, 3rd-order polynomial) to find chunk boundaries.
@@ -49,7 +49,8 @@ per-release `**vX.Y.Z:**` narration — CI enforces this
- `src/commands/sync.ts` — `gbrain sync` CLI + the `performSync` / `performFullSync` library entrypoints (consumed by the autopilot cycle and the Minion sync handler). `performSync` runs under a writer lock: per-source `gbrain-sync:<sourceId>` whenever `opts.sourceId` is set, wrapped in `withRefreshingLock` from `src/core/db-lock.ts` so long-running sources (250K+ chunks) don't lose the lock at the 30-min TTL mid-run; the bare no-source path uses the SAME refreshing lock (#1794 — it was previously a non-refreshing `tryAcquireDbLock`, stealable mid-run during an incident); `SyncOpts.lockId?: string` is the explicit override. The lock refresh AND its health probe route through the DIRECT session pool so Supavisor transaction-pool exhaustion (`EMAXCONNSESSION`) can't kill renewal; takeover is heartbeat-aware (it will NOT steal a holder whose `last_refreshed_at` is within `GBRAIN_LOCK_STEAL_GRACE_SECONDS`, defending an alive-but-starved holder); the import loop yields the event loop every `GBRAIN_SYNC_YIELD_EVERY` files (`setTimeout(0)`, not `setImmediate` — Bun starves the timers phase) so the refresh `setInterval` heartbeat fires mid-import. This lock-identity invariant prevents a `sync --all` per-source worker racing `sync --source foo` on the global lock from corrupting the same source. `performSync` throws a typed `SyncLockBusyError` when the writer lock is held; the Minion `sync` handler (`src/commands/jobs.ts`) catches it and marks the job SKIPPED (not failed) so a cron/autopilot tick defers to the holder without polluting crash metrics. `performSyncInner` is RESUMABLE (incremental path): it drains a PINNED target commit (`lastCommit..pin`), banking drained file paths via `appendCompleted` (append-only delta into the `op_checkpoint_paths` child table, migration v115 — one row per path, O(delta) not the old O(N²) full-array rewrite), keyed by `syncFingerprint({sourceId, lastCommit})` from `src/core/op-checkpoint.ts` (paths under `op:'sync'`; the pinned target under `op:'sync-target'`), and advances `last_commit`/`last_sync_at` ONLY at full import completion. Checkpoint writes route through the DIRECT session pool + bounded retry so they survive `EMAXCONNSESSION`; the flush cadence is first-file then every `GBRAIN_SYNC_CHECKPOINT_EVERY` (default 1000) files OR `GBRAIN_SYNC_CHECKPOINT_SECONDS` (default 10s), with a race-safe `pendingCheckpointPaths` delta (single-flight swap, re-merge on failure) under parallel workers; a SIGTERM banks the in-flight delta via a no-retry one-shot (`appendCompletedOnce`, ordered before lock release through `registerCleanup`); and sustained flush failure aborts the run with `reason:'checkpoint_unavailable'` after `GBRAIN_SYNC_MAX_CHECKPOINT_FAILURES` consecutive fails rather than importing work it can never bank (every partial/blocked exit logs the banked-file count). A sync killed mid-import banks its progress, leaves the anchor unmoved (the source stays correctly stale to the autopilot scheduler — `last_sync_at` is never bumped on a partial), and the next run `resumeFilter`s the same fixed diff to skip done files. The pin is the checkpoint's stored target when still reachable from HEAD, else live HEAD (a history rewrite / reset re-pins); completion advances to the pin, NOT live HEAD, so commits landing past the pin are a clean next-sync diff — this closes the cross-run staleness window. After import a pin-reachability gate (`git merge-base --is-ancestor pin HEAD`) replaces the old strict head-drift gate: forward commits on top of the pin (e.g. a background enrich process committing to the same repo every ~2 min) are SAFE and no longer block the run; only a real rewrite (pin not an ancestor of HEAD) blocks and discards the checkpoint. A file added in `lastCommit..pin` but gone from disk (deleted by a commit after the pin) is SKIPPED and marked completed, not recorded as a failure. Downstream extract/facts/embed are size-gated: inline only for `totalChanges <= 100`; large syncs defer to the resumable `extract --stale` watermark + `embed --stale`/backfill + the facts cycle phases, so a 44K-page facts/embed pass never re-blocks import convergence (sync convergence == import convergence). Worker engines wrap in try/finally so disconnect always fires; both PGLite-detection sites use `engine.kind === 'pglite'`. CLI accepts `--workers N` (alias `--concurrency N`) validated via `parseWorkers` (explicit bypasses the file-count floor; auto path defers to `autoConcurrency()`). The newest-first descending-lex order uses `sortNewestFirst(addsAndMods)` from `src/core/sort-newest-first.ts` (shared with `gbrain import`). `gbrain sync --all` runs a continuous worker pool: `parseWorkers`-validated `--parallel N` (default `min(sourceCount, --workers, DEFAULT_PARALLEL_SOURCES=4)`), long-lived async workers pulling from a shared FIFO queue (no head-of-line blocking), per-source `withSourcePrefix(src.id, ...)` so every `slog`/`serr` line carries `[<source-id>]`; `--skip-failed`/`--retry-failed` are scoped per source (`acknowledgeFailures(sourceId)`; `--all` acks every source, single-source acks only its own) and run UNDER parallel — the #1939 failure ledger is per-`(source_id, path)` and serialized through `withLedgerLock`, so the old "not supported under parallel, re-run with --serial" refusal is lifted (#2139, which also removed the forcing-function that pushed recovery syncs to `--serial` and thus armed the inline cost gate); a connection-budget stderr warning fires when `parallel × workers × 2 > 16` (the `× 2 per-file pool` factor: each per-file worker opens its own `PostgresEngine` with `poolSize=2`). Exports `resolveParallelism`, `syncOneSource`, `buildSyncStatusReport`, `printSyncStatusReport`, `SyncStatusReport` back the `gbrain sources status` dashboard. `--json` envelope `{schema_version: 1, sources, parallel, ok_count, error_count, skipped_count}` on stdout; human banners route to stderr via `humanSink` so `jq` parses cleanly. Exit matrix: 0 all ok (sources skipped by `--missing-path skip` count as ok), 1 any error. `--missing-path <fail|skip>` (default fail) handles sources whose `local_path` does not exist on this machine — machine-specific state in a brain-wide table, so a brain registered from several machines fails every foreign source on every run; `skip` classifies them `skipped_missing_path` (⊘ line, envelope entry with `local_path`, excluded from `error_count` and the rc gate) via the exported pure helpers `parseMissingPathMode` + `partitionMissingPathSources`, pinned by `test/sync-all-missing-path.test.ts`; default `fail` stays loud because on a single-machine brain a missing path usually means an unmounted volume. (The non-TTY cost gate no longer exits 2 — it auto-defers; #2139.) The dashboard SQL is `content_chunks ch JOIN pages pg ON pg.id = ch.page_id WHERE pg.deleted_at IS NULL` with `archived = false` at the caller; embedding column resolved via `resolveEmbeddingColumn(undefined, cfg)` from `src/core/search/embedding-column.ts` so Voyage/multimodal/non-default-column brains count against the column they use; errors propagate (no swallow-catch). The sync delete loop is interleaved per-batch resolve+delete using `engine.resolveSlugsByPaths` + `engine.deletePages` from `src/core/engine.ts` (73K-delete commit: ~146K SQL round-trips → ~292, closing the cascade-staleness class where one big-delete commit jammed every other source's sync); per-batch try-catch decomposes batch DELETE failures to per-slug `deletePage` fallback, unrecoverable per-slug failures land in `failedFiles`; `pagesAffected` filters to confirmed-deleted slugs. The rename loop gets the same batched slug-resolve; a rename whose destination is un-syncable folds the source path into the delete set so the old page can't orphan (#1970). An entry-time bookmark-reachability guard distinguishes a gc'd anchor (`cat-file` fails → `performFullSync`) from a history-rewrite anchor that is merely no-longer-an-ancestor but still on disk: the latter is diffed tree-to-tree directly (`git diff lastCommit..pin` is an endpoint-tree compare, ancestry not required) so a force-push / `master`→`main` consolidation imports only the real delta instead of re-walking the whole tree forever (#1970); an oversized or failed diff degrades to `performFullSync`. `performFullSync` is itself authoritative for deletes — after an advancing full import it purges file-backed pages (`source_path != null` AND strategy-aware `isSyncable`) whose source file no longer exists, sparing `put_page`/manual pages (null `source_path`) and metafiles. The stale-file decision routes through the pure, exported `planReconcileDeletes(rows, currentFiles, isSyncablePath)`: it normalizes path separators on both sides of the membership test (a Windows `path.relative` backslash path vs a git-derived forward-slash `source_path` would otherwise mark every page stale and wipe the source) and computes a mass-delete signal — when the reconcile would delete more than `MASS_RECONCILE_RATIO` (50%) of the file-backed pages the strategy manages, on a source holding more than `MASS_RECONCILE_MIN_PAGES` (20) of them, the delete is REFUSED with a loud stderr warning (that shape is almost always a path-comparison bug or the wrong repo path, not a genuine bulk deletion); `GBRAIN_ALLOW_MASS_RECONCILE=1` restores the unguarded delete for genuinely intended bulk removals. Pinned by `test/sync-reconcile-mass-delete.test.ts`. Below the valve, stale pages are partitioned by git history via exported `listEverCommittedPaths(repoPath)` (one `git log --all --no-renames --diff-filter=A --name-only` pass; null on non-git dirs → unchanged behavior): a stale path that EVER existed in history was genuinely deleted → reconciled; a path with NO history is DB-only write-through (never committed/pushed, e.g. lost to a fresh clone) → the page is KEPT and its markdown re-exported to the working tree via `writePageThrough`, with a stderr hint to commit it (#2426 — "absent from git" is the symptom of the missing write-through commit, not evidence the content is disposable). Pinned by `test/sync-reconcile-db-only.serial.test.ts`. `resolveSlugByPathOrSourcePath` at `sync.ts:267` delegates to `engine.resolveSlugsByPaths` when `sourceId` is set, keeping legacy `executeRaw` fallback for the no-sourceId path. `failedFiles` is hoisted to the top of `performSyncInner` so both delete-decompose and import loops feed the same bookmark gate. The cost gate is the shared `runInlineCostGate` (one implementation on BOTH the `--all` and single-source paths; runs at the command layer, never inside `performSync`), mode-aware via `willEmbedSynchronously` + posture-aware `shouldBlockSync` from `src/core/embedding.ts` (#2139). The DEFERRED path (v2 on, parallel) is INFORMATIONAL (embedding goes to per-source `embed-backfill` jobs with their own `$X/source/24h` cap, default $25 via `SPEND_CAP_CONFIG_KEY` from `embed-backfill-submit.ts`; prints the cap + backlog + queued-job count, NEVER exits 2). The INLINE path (v2 off, or `--serial`) gates on the DELTA estimate vs `sync.cost_gate_min_usd` (default $0.50): below floor proceeds; above floor in a TTY prompts `[y/N]`; above floor in a non-TTY/`--json` session AUTO-DEFERS embeds to capped backfill jobs and exits 0 (NEVER exit 2 — the wedged-cron fix); `spend.posture=tokenmax` makes it informational and proceeds inline. The estimate MIRRORS EXECUTION instead of pricing the whole tree: `estimateInlineNewTokens` routes through the shared `computeSyncDelta` (`src/core/sync-delta.ts`) — fetch-first against `origin/<branch>`, prices only the committed delta (a dirty-but-caught-up tree → $0), with a full-tree CEILING only on the fail-open rungs (chunker drift, first sync, git-unavailable) honestly labeled; `--full` adds the stale backlog (full sync sweeps it inline). Return shape carries `estimateKind: 'delta'|'ceiling'|'mixed'|'unchanged'` + `ceilingReasons`. Helpers `resolveCostGateFloorUsd(engine)` + `resolveBackfillCapUsd(engine)` resolve via `parseUsdLimit` (`off`/`unlimited` → `Infinity`; floor accepts `0` = block-on-any-spend). JSON envelopes carry `mode` + `gate` discriminators (`dry_run | deferred_notice | below_floor | auto_deferred_embeds | posture_tokenmax`) + a paste-ready `hint`; `Infinity` floors/caps render as the string `'unlimited'` (never raw, which JSON-serializes to `null`); `SyncStatusReportSource` gains `backfill_queued`/`backfill_active`/`backfill_last_completed_at`; cost previews read `getEmbeddingModelName()` (no hardcoded OpenAI). Format splits on the explicit `--json` flag only (human text otherwise). `SyncOpts.noSchemaPack` (CLI `--no-schema-pack`, threaded through `performSync` AND `syncOneSource`) skips `loadActivePack` so pages fall back to legacy prefix typing — an escape hatch when a suspect pack regex wedges a sync. A per-file BEGIN heartbeat `if (process.env.GBRAIN_SYNC_TRACE) serr('[sync] begin import: <path>')` fires BEFORE `importFile` (the `progress.tick` fires only AFTER) so a stuck file is a begin-line with no matching completion. Triage doc: `docs/architecture/serve-sync-concurrency.md` (PGLite single-writer serve↔sync contention + the `GBRAIN_SYNC_TRACE` + `--no-schema-pack` recipes). Pinned by `test/e2e/sync-status-pglite.test.ts` (IRON-RULE: PGLite seeds 2 sources × pages × chunks, soft-deletes 1 page, archives 1 source, validates the SQL excludes both AND uses the active embedding column), `test/sync-cost-gate.serial.test.ts`, `test/sync-cost-preview.test.ts`. Runaway-sync protection: `resolveSyncHardDeadline(args, {isTty, env, defaultNonTtySec?})` resolves a wall-clock hard deadline (precedence `--no-hard-deadline` > `--hard-deadline <s>` > `--timeout <s>`(non-`--all`, which auto-arms the backstop) > `GBRAIN_SYNC_MAX_RUNTIME_SECONDS` env > non-TTY default 3600s > none; `HARD_DEADLINE_GRACE_SEC=30`). `src/cli.ts` installs the out-of-band watchdog (see `src/core/process-watchdog.ts`) for the sync command BEFORE `connectEngine` and disposes it in the dispatch `finally`, so even an event-loop-starved sync — or a connect-phase hang — is SIGTERM-then-SIGKILLed by the deadline instead of orphaning under cron. `runSync` registers a SIGINT handler that aborts an interrupt `AbortController` composed via `composeAbortSignals(...)` (an `AbortSignal.any` wrapper over the defined signals) with the per-source `--timeout` signal, so Ctrl-C returns a clean `partial` and releases the lock through the normal `finally` (process-cleanup.ts owns SIGTERM lock-release; the watchdog owns the hard kill). `withRefreshingLock` `unref()`s its refresh `setInterval`. The spin's own root cause is not yet pinned (leading lead is catastrophic-backtracking in a pack link-inference regex, bounded by the redos-guard); the watchdog heartbeat plus the existing `[gbrain phase]` breadcrumbs are the diagnosis surface. Pinned by `test/sync-hard-deadline.test.ts` (resolution precedence + `composeAbortSignals`). Monorepo subdir sources (#753/#774): `--src-subpath <dir>` (or a repo path that IS a subdir — auto-discovery via `discoverGitRoot`, i.e. `git rev-parse --show-toplevel`) splits the repo path into `gitContextRoot` (all git ops: pull/diff/rev-parse/cat-file) and `syncScopeRoot` (walk/import/delete/rename scope); scoped syncs use git-root-relative slugs + `source_path` (full sync threads `slugRoot` into `runImport`) so full and incremental agree; NAV-1/NAV-2 realpath containment rejects `../`-traversal and symlinked scopes resolving outside the repo BEFORE any git op, and a per-file realpath guard (`isPathSafe`) refuses symlink-escape files in the incremental drain and rename reimport (fail-closed into `failedFiles`, so the bookmark can't advance past an escape); the full-sync reconcile is scope-restricted so a scoped sync never sweeps out-of-scope pages. `--exclude <glob>` (repeatable) filters scope-relative paths in both full and incremental paths; exclusion never deletes previously-imported pages (conservative, matching the #1433 metafile posture); an all-excluded run warns loudly (NAV-4). A warn-and-continue internal `git pull` failure (non-timeout class — e.g. a local-path origin rejected by `protocol.file.allow=never`) still falls through to sync the local working tree, but a ZERO-import run after a failed pull returns `partial` with `reason: 'pull_failed'` instead of `up_to_date`: `last_commit` AND the `last_sync_at` heartbeat stay frozen (so doctor `sync_freshness` / `sources status` staleness fires), the single-source CLI exits non-zero, `sync --all` exits non-zero if any source hit it (JSON envelope carries the per-source `reason`), and the autopilot cycle's sync phase maps it to `warn`. Timeout-class partials keep their pre-existing exit-0 / phase-`ok` semantics (they converge on retry; a failing pull does not). Pinned by `test/sync-pull-failed-anchor.serial.test.ts`. `resolveSlugByPathOrSourcePath`: Resolves a slug by `pages.source_path` first (returns the stored slug for frontmatter-fallback pages whose path doesn't derive a slug), then falls back to `resolveSlugForPath(path)`. Threaded into all 4 delete/rename call sites (`performSync`'s un-syncable cleanup at ~:531, deletes at ~:603, rename oldSlug at ~:622). Without this, emoji-only / Thai / Arabic filenames whose slug came from frontmatter would orphan on delete/rename (the delete path would compute the wrong path-derived slug). Best-effort query — pre-migration brains fall through to the legacy path. #2849: above the size gate (totalChanges > 100) the deferred link/timeline extraction is DURABLY QUEUED, not just hinted — the defer branch submits an `extract` Minion job `{stale: true, sourceId?, deferred_commit: pin}` keyed `extract-stale:<sourceId|default>:<pin>` (repeat submissions toward the same drained pin coalesce; deliberately NO maxWaiting — an unscoped payload's coalesce filter matches ANY waiting extract job and would silently drop the sweep), `timeout_ms` derived from extract.ts's exported `STALE_TIME_BUDGET_MS` + headroom. The returned row is verified to be a live `{stale:true}` job (waiting/delayed/active) before the log claims "queued"; a finished row occupying the key slot (a prior sweep toward the same pin that completed before this run's pages landed — the checkpoint-resume / blocked-advance re-sync case) triggers a fresh submission under a run-unique key so those pages never strand stale. Submission is best-effort (failure falls back to the hint; pages stay stale + doctor-visible, never mis-stamped). Pinned by `test/sync-deferred-extract-queue.serial.test.ts`.
- `src/core/sources-ops.ts` — Multi-source registration + clone-lifecycle ops (`addSource`, `recloneIfMissing`, `defaultCloneDir`, `isOwnedClone`, `unownedHint`). **Reclone-ownership invariant (must-never-violate): gbrain may only delete/re-clone a clone it created, NEVER a user working tree.** `recloneIfMissing` deletes `local_path`, so it gates on `isOwnedClone(src)` and throws a `SourceOpError('unmanaged_path', ...)` BEFORE any filesystem op when ownership is unprovable — fail-closed. Ownership is proven by `config.managed_clone === true` (written by `addSource`'s `--url` path, covering default-location and `--clone-dir` clones) OR `local_path === defaultCloneDir(id)` (back-compat for pre-marker clones, via exact normalized-path equality, symlink-free). A row with `remote_url` + an unowned `local_path` (a user-registered working tree, e.g. `sources add --path`) is refused untouched; re-add with `--url` to regain auto-reclone. The reclone is EXDEV-safe: clone into a SIBLING temp of `local_path` (not the shared `clones/.tmp`, which may sit on a different mount than a `--clone-dir` target), then swap (move old aside → move new in → drop old) so `local_path` is never left missing-and-unrecoverable; on swap failure the original is restored, and if restore fails the error names the `aside` path so it's never reflexively deleted. A TOCTOU re-check re-confirms ownership immediately before the destructive move and rejects a symlink leaf swapped in after the entry check (`symlink_escape`). `unownedHint(src, state)` is the shared recovery message used by both the core error and the `gbrain sync --source` CLI error; `gbrain sources restore` special-cases `unmanaged_path` to print "DB row restored; gbrain syncs this path read-only" instead of the misleading "try sync to recover" guidance. `SourceOpErrorCode` includes `unmanaged_path`. Pinned by `test/sources-ops.test.ts`, `test/sources-resync-recovery.test.ts`.
- `src/core/utils.ts` — Shared SQL utilities extracted from postgres-engine.ts. Exports `parseEmbedding(value)` (throws on unknown input, used by migration + ingest paths where data integrity matters) and `tryParseEmbedding(value)` (returns `null` + warns once per process, used by search/rescore paths where availability matters more than strictness). `isUndefinedColumnError(err)` predicate — pattern-matches Postgres SQLSTATE 42703 / "column ... does not exist" with engine-driver shape variation tolerated; replaces bare `catch {}` blocks in `oauth-provider.ts` so genuine errors (lock timeout, network blip, permission denied) propagate while column-missing falls through to the legacy fallback. `validateSourceId(id)` throws on anything outside `^[a-z0-9_-]+$`, used by the per-source disk-layout fix in patterns.ts/synthesize.ts before any `join(brainDir, '.sources', source_id, slug+'.md')` so source_id can't traverse out of brainDir. `rowToSearchResult` projects email `message_id` / `thread_id` metadata and exposes `source_subject` only when a non-empty Message-ID proves the page is an email, so generated page titles never become authoritative email subjects. `rowToPage` populates the required `Page.source_id` from the SELECT projection (`scripts/check-source-id-projection.sh` enforces every projection feeding `rowToPage` includes the column).
-- `src/core/db.ts` — Connection management, schema initialization. `resolveSessionTimeouts()` returns `statement_timeout` + `idle_in_transaction_session_timeout` (defaults 5min each, env-overridable via `GBRAIN_STATEMENT_TIMEOUT`/`GBRAIN_IDLE_TX_TIMEOUT`/`GBRAIN_CLIENT_CHECK_INTERVAL`). Both `connect()` (module singleton) and `PostgresEngine.connect()` (worker pool) consume the result via postgres.js's `connection` option, sending GUCs as startup parameters that survive PgBouncer transaction mode (`setSessionDefaults` kept as a back-compat no-op shim). `connect()` returns `Promise<boolean>` — `true` iff THIS call created the module singleton, `false` if it joined an existing one; the decision is atomic (no `await` between the `if (sql)` null-check and the synchronous `sql = postgres(...)` assignment), so two concurrent module connects can't both claim creation. `PostgresEngine` stores the return as its `_ownsModuleSingleton` token and only the creating engine may `db.disconnect()` the singleton — a borrower probe engine (lint/doctor config-lift) no-ops its disconnect, so its teardown can't null the connection the long-lived cycle owner is still using (the dream-cycle "connect() has not been called" failure). The module `sql` is only ever nulled by `db.disconnect()` (postgres.js auto-reconnects its own internal pool and never touches our reference). `disconnect()` snapshots + nulls `sql` before awaiting the pool end so a concurrent connect can't join a pool that's already closing. The end routes through `endPoolBounded(pool)` (#1972) — a gbrain-owned `Promise.race` of `pool.end({ timeout: POOL_END_TIMEOUT_SECONDS })` against a hard timer — so a PgBouncer transaction-mode drain that never settles can't hang teardown — the #2084 contract (finishCliTeardown's computed-deadline backstop + flushThenExit's fence-and-grace exit in cli-force-exit.ts) bounds it and delivers pending stdout before exit. `connection-manager.ts` ends its direct + read pools concurrently through the same helper so the per-pool bounds don't stack.
+- `src/core/db.ts` — Connection management, schema initialization. `resolveSessionTimeouts()` returns `statement_timeout` + `idle_in_transaction_session_timeout` (defaults 5min each, env-overridable via `GBRAIN_STATEMENT_TIMEOUT`/`GBRAIN_IDLE_TX_TIMEOUT`/`GBRAIN_CLIENT_CHECK_INTERVAL`). Both `connect()` (module singleton) and `PostgresEngine.connect()` (worker pool) consume the result via postgres.js's `connection` option, sending GUCs as startup parameters that survive PgBouncer transaction mode (`setSessionDefaults` kept as a back-compat no-op shim). `connect()` returns `Promise<boolean>` — `true` iff THIS call created the module singleton, `false` if it joined an existing one; the decision is atomic (no `await` between the `if (sql)` null-check and the synchronous `sql = postgres(...)` assignment), so two concurrent module connects can't both claim creation. `PostgresEngine` stores the return as its `_ownsModuleSingleton` token and only the creating engine may `db.disconnect()` the singleton — a borrower probe engine (lint/doctor config-lift) no-ops its disconnect, so its teardown can't null the connection the long-lived cycle owner is still using (the dream-cycle "connect() has not been called" failure). The module `sql` is only ever nulled by `db.disconnect()` (postgres.js auto-reconnects its own internal pool and never touches our reference). `disconnect()` snapshots + nulls `sql` before awaiting the pool end so a concurrent connect can't join a pool that's already closing. The end routes through `endPoolBounded(pool)` (#1972) — a gbrain-owned `Promise.race` of `pool.end({ timeout: POOL_END_TIMEOUT_SECONDS })` against a hard timer — so a PgBouncer transaction-mode drain that never settles can't hang teardown — the #2084 contract (finishCliTeardown's computed-deadline backstop + flushThenExit's fence-and-grace exit in cli-force-exit.ts) bounds it and delivers pending stdout before exit. `connection-manager.ts` ends its direct + read pools concurrently through the same helper so the per-pool bounds don't stack. `resolveMaxLifetimeSeconds(env?)` — explicit client-pool `max_lifetime` for all four postgres() call sites (matches the postgres.js implicit 30-60min jittered default; `GBRAIN_POOL_MAX_LIFETIME_S` overrides, 0 disables; warn-once on invalid). Pinned by `test/db-pool-max-lifetime.test.ts`.
+- `src/core/pool-gauge.ts` — `CheckoutGauge`: approximate in-flight counters at the engine's raw/direct/reserved/tx seams, surfaced via duck-typed `PostgresEngine.getPoolDiagnostics()` (no BrainEngine change, no PGLite stub). HONESTY CONTRACT in the module doc: tagged-template traffic is untracked; consumers must label counts as a subset and never derive waiter/available figures. Fail-open (clamped release, try/finally around sync-throwing runUnsafe). Consumed by db-probe.ts. Pinned by `test/pool-gauge.test.ts`.
- `src/commands/migrate-engine.ts` — Bidirectional engine migration (`gbrain migrate --to supabase/pglite`). Copies the complete source catalog FIRST (`copyMigrationSources` — every `sources` row incl. archived rows and sync/routing metadata, `ON CONFLICT (id) DO UPDATE`, `default` ordered first) so every page write has a valid `pages.source_id` FK parent and the target preserves per-source behavior; pages copy afterward, tracked in the resume manifest by composite `(source_id, slug)` key. Link copy preserves each row's `to_source_id` (falling back to the origin source only for legacy rows without it), and failed-target filtering uses that same target composite key, so cross-source links migrate without being rebound to the origin source. The resume manifest is target-aware: `migrationTargetId(config)` hashes `(engine, locator)` (`database_url` for Postgres, resolved `database_path` for PGLite) and `manifestMatchesTarget` requires `schema_version === 2` plus a matching `target_id` — a legacy engine-only manifest, or one from a DIFFERENT target of the same engine kind, starts fresh instead of skipping "completed" pages the new target never received. Pinned by `test/migrate-engine-resume.test.ts` (manifest identity) + `test/e2e/migrate-engine-sources-postgres.test.ts` (source catalog lands before overlapping-slug pages, PGLite → real Postgres) + `test/e2e/multi-source-bug-class.test.ts` (cross-source links).
- `src/core/import-file.ts` — importFromFile + importFromContent (chunk + embed + tags). `importFromContent` and `importCodeFile` stamp `pages.embedding_signature` via `setPageEmbeddingSignature(slug, {sourceId, signature: currentEmbeddingSignature()})` when the import actually embedded (not `--no-embed`) so a model/dims swap is detectable as stale; `importCodeFile` only stamps when every chunk was freshly embedded this call (`needsEmbedIndexes.length === chunks.length`), mixed reuse-by-hash pages stay unstamped (`reindex --code --force` / `embed --stale` handle those). `importFromContent`'s tag reconciliation is ADD-ONLY: it only `addTag` (idempotent, ON CONFLICT DO NOTHING). The `tags` table has no provenance column and frontmatter tags are stripped from stored `pages.frontmatter` (markdown.ts:118), so a frontmatter-origin tag can't be distinguished from a DB-enrichment tag (auto-tag / dream synthesize / signal-detector) at re-import — deletion is unsafe (would wipe enrichment under `gbrain reindex --markdown`). Accepted trade-off: removing a tag from frontmatter no longer removes it from the DB on next sync (needs a `tag_source` provenance column). Pinned by `test/reindex-preserve-tags.test.ts` + `test/import-file.test.ts`. identity-based dedup pre-check at `:427-490`. Calls `engine.findDuplicatePage?.(sourceId, {hash, frontmatterId})` (optional `?` so test doubles compile). Posture: SKIP when `frontmatter.id` matches (true external duplicate from overlapping ingest roots), WARN-ALWAYS on content_hash collision with different/missing `frontmatter.id` (templates and daily logs may legitimately share text), FAIL CLOSED on lookup error, bypass via `--force-rechunk`. Soft-deleted pages excluded at the engine layer so tombstones don't block legitimate re-imports under new slugs. Pinned by `test/import-dedup-frontmatter-id.test.ts` (11 cases). `importFromContent` is the narrow waist every ingest path passes through (`gbrain import`, `gbrain sync`, `put_page` MCP, `/ingest` webhook). It runs a three-tier content-quality disposition via `assessContentSanity` from `src/core/content-sanity.ts` BEFORE chunking: (1) high-confidence junk (built-in Cloudflare/CAPTCHA interstitial patterns + operator literals) → QUARANTINE (stamps the `quarantine` frontmatter marker, writes ZERO chunks, hides the page from search) OR REJECT (throw → sync-failure) when `content_sanity.junk_disposition` is `reject`; (2) fuzzy markup-heavy (prose-vs-markup ratio above `content_sanity.max_markup_ratio`, warn-tier byte window, code pages exempt) → `content_flag:markup_heavy` marker (page stays fully searchable, marker rides search results + get_page to warn the agent); (3) oversize → `embed_skip` soft-block via `buildEmbedSkipMarker()` PLUS a `content_flag:oversized` marker, AND deletes any pre-existing chunks in the same transaction so search can't surface stale chunks. Gate-owned markers (`quarantine`, `content_flag`) are STRIPPED from untrusted (remote MCP, `ctx.remote !== false`) frontmatter so a write-scoped client can't hide pages or forge the warning channel; markers are excluded from `content_hash` so a flagged page doesn't re-embed every sync. `gbrain import` honors `errors > 0` for non-zero exit. `classifyErrorCode` in `src/core/sync.ts` recognizes the `PAGE_JUNK_PATTERN` code so sync-failures.jsonl grouping bins these. `extractEntityRefs` (canonical; matches both `[Name](people/slug)` markdown links and Obsidian `[[people/slug|Name]]` wikilinks), `extractPageLinks`, `inferLinkType` heuristics (attended/works_at/invested_in/founded/advises/source/mentions), `parseTimelineEntries`, `isAutoLinkEnabled` config helper. Link candidates match any dir-shaped path (#2576; existence-checked at persist). Used by extract.ts, operations.ts auto-link post-hook, and backlinks.ts. Pinned by `test/import-file-content-sanity.test.ts`.
- `src/core/sync.ts` — Pure sync functions (manifest parsing, filtering, slug conversion). Exported `pruneDir(name: string): boolean` is the single source of truth for descent-time directory exclusion across walkers — blocks `node_modules` (no leading dot, so naive walkers slipped through and inflated MISSING_OPEN counts via vendor packages), `vendor`/`dist`/`build`/`venv`, dot-prefix dirs, and `*.raw` sidecars — NOT `ops/`, which is ordinary user content (#2404; the bundled daily-task-manager stores `ops/tasks` there); `isSyncable` applies it per path segment, and `walkMarkdownFiles` in `src/commands/extract.ts` + `listTextFiles` in `src/core/cycle/transcript-discovery.ts` consult it BEFORE recursing to save the IO of walking thousands of vendor files (closes #923 + #202). `manageGitignore` worktree discriminator matches the gitdir path segment (`/modules/<name>` = submodule, `/worktrees/<name>` = worktree, per Git's documented layout) so Conductor worktrees (first-class repos) get `.gitignore` management for storage-tiering (closes #889). The sync-failure ledger (failure store, error classifier, the shared bookmark gate, and the doctor severity rule) lives in `src/core/sync-failure-ledger.ts`; `sync.ts` re-exports `classifyErrorCode`, `summarizeFailuresByCode`, `loadSyncFailures`, `unacknowledgedSyncFailures`, `acknowledgeSyncFailures`, `recordSyncFailures`, `decideSyncFailureSeverity`, `applySyncFailureGate`, and the `SyncFailure` type for backward-compatible imports — see its entry below. `isSyncable` factored through private `classifySync(path, opts): SyncableReason | null`; exported companion `unsyncableReason(path, opts)` returns the same tagged reason or null when syncable. `SYNC_SKIP_FILES` is a named export (the four canonical metafile basenames `schema.md`, `index.md`, `log.md`, `README.md`). `SyncableReason` union: `'metafile' | 'strategy' | 'pruned-dir' | 'include-glob-miss' | 'exclude-glob-hit'`. The `commands/sync.ts` cleanup loop guards on `unsyncableReason(path)` being `'metafile'` OR `'pruned-dir'` (#2404) so previously-indexed metafile pages AND deliberately put-created pages under pruned dirs survive every re-sync. Does NOT cover `manifest.deleted` (the upstream filter already strips metafiles). Pinned by `test/sync-isSyncable-shape.test.ts` (15 cases, duality contract) + `test/sync-metafile-skip.serial.test.ts` (3 PGLite cases incl. the renamed `.md → .txt` negative). `pruneDir`: `pruneDir(name, parentDir?)` extended with optional `parentDir`. When provided, additionally rejects directories containing `.git` as a FILE — the git submodule gitfile pattern (regular repos have `.git` as a DIRECTORY; submodules as a file pointing into the parent's `.git/modules/`). Sync + extract walkers thread `parentDir` so the gitfile-as-FILE check fires per descend step. Best-effort: `statSync` failures fall through and treat as a normal dir. Closes the phantom-import bug class where syncing a worktree-with-submodules walked into submodule trees. Pinned by `test/sync-walker-submodule.test.ts`.
@@ -180,8 +181,8 @@ per-release `**vX.Y.Z:**` narration — CI enforces this
- `src/core/audit/batch-retry-audit.ts` — JSONL audit primitive for batch-retry events, built on `audit-writer.ts`. Schema: `{ts, site, batch_size, attempt, outcome: 'success' | 'exhausted', delay_ms, error_message_summary, error_code?}`. Privacy: NEVER logs slugs / page IDs / content (mirrors `shell-audit.ts`). `logBatchRetry` fires per successful retry recovery; `logBatchExhausted` fires when retries exhaust and rows are lost. `readRecentBatchRetryEvents(hours=24)` returns `{events, corrupted_lines, files_scanned, files_unreadable}` — corruption + permission errors surface to doctor, not silently swallowed. `pruneOldBatchRetryAuditFiles(daysToKeep=30)` deletes old files, called from `gbrain dream --phase purge`. File: `~/.gbrain/audit/batch-retry-YYYY-Www.jsonl` (honors `GBRAIN_AUDIT_DIR`). `summarizeError` routes error messages through the shared `redactConnectionInfo` helper from `src/core/audit/redact-connection-info.ts` BEFORE truncation so DSNs / hostnames / credentials / IPv4 octets can't leak into operator-shared JSONL dumps. Pinned by `test/audit/batch-retry-audit.test.ts` (12 cases) + `test/audit/batch-retry-redaction.test.ts` (3 privacy regressions).
- `src/core/audit/lock-renewal-audit.ts` — JSONL audit primitive for per-job lock-renewal faults. Sibling of `batch-retry-audit.ts`, built on `audit-writer.ts`. Four outcomes: `failure` (single renewLock throw, counter incremented), `success_after_failure` (recovery; emits the recovery count), `gave_up` (time-based deadline exceeded; abort fired), `executeJob_rejected` (the second unhandledRejection vector — the stored `executeJob(...).finally(...)` promise itself rejected, e.g. failJob threw during the same DB outage). Schema: `{ts, job_id, job_name, attempt?, outcome, error_message_summary?, error_code?}`. Privacy: NEVER logs `lock_token` or `job.data`; error summaries route through `redactConnectionInfo` BEFORE truncation. Defense-in-depth: every audit call inside the lock-renewal tick's catch block is wrapped in its own inner try/catch so a misbehaving audit-writer can't re-introduce the unhandledRejection bug class. `readRecentLockRenewalEvents(hours=24)` walks current + previous ISO week with corrupted-line tolerance. `pruneOldLockRenewalAuditFiles(daysToKeep=30)` is ready for future dream-cycle purge wiring. File: `~/.gbrain/audit/lock-renewal-YYYY-Www.jsonl`. Pinned by `test/audit/lock-renewal-audit.test.ts` (11 cases).
- `src/core/audit/redact-connection-info.ts` — Shared pure helper. `redactConnectionInfo(text: string): string` strips Postgres connection info before any audit JSONL write: `postgres://`/`postgresql://` URLs, `host=foo`, `user=foo`, `password=foo`, `pwd=foo`, IPv4 octets — each match becomes `<REDACTED:kind>`. Negative-lookbehind/lookahead `[\w.@-]` on the IPv4 pattern defeats version-string false positives (`v3.1.4.0`, `[email protected]`) while still matching real IPs in PG errors (`(192.168.1.42)`). Order-sensitive pattern set: URL forms first so substrings inside URLs don't get double-redacted. Idempotent, pure (no I/O), hot-path-safe (regex compiled at module load). Wired into BOTH `lock-renewal-audit.ts` AND `batch-retry-audit.ts`. Known limitations: bare-quoted hostnames (`at "db.example.com"`) and usernames (`for user "postgres.foo"`) are NOT caught — the highest-value leak (the IP in those shapes) IS caught. Pinned by `test/audit/redact-connection-info.test.ts` (15 cases: all 5 patterns + Supabase fixture + ENOTFOUND fixture + version-string false-positive defense).
-- `src/core/minions/lock-renewal-tick.ts` — Pure extracted function from `MinionWorker.launchJob`'s setInterval body; the structural fix that closes the production unhandledRejection crash class. Exports `runLockRenewalTick(deps, state) → Promise<TickResult>` + `resolveLockRenewalKnobs(env, lockDuration) → LockRenewalKnobs`. Three env knobs, all parsed as positive integers with stderr-warn-once-per-process on bad input + default fallback: `GBRAIN_LOCK_RENEWAL_MAX_FAILURES` (default 3, audit-labeling only — abort triggering is time-based), `GBRAIN_LOCK_RENEWAL_CALL_TIMEOUT_MS` (default `lockDuration/3`, bounds the hung-renewLock vector via `Promise.race`), `GBRAIN_LOCK_RENEWAL_SAFETY_MARGIN_MS` (default `lockDuration/6`, ensures release before stall-detector reclaim). The tick checks `state.cancelled()` at three guard points (entry, after await resolves, after await throws) so a long renewLock that resolves after the job ended bails cleanly. Result type is a tagged union `{kind: 'ok' | 'cancelled' | 'lock_lost' | 'should_abort', reason?}`; all `abort.abort()` / `clearInterval` side effects live in the worker's closure scope, not the pure function. Audit calls wrapped in inner try/catch (defense-in-depth). `LockRenewalDeps` has an optional `reconnect?` callback; the renewal-tick's catch branch (after retryable-error classification + the time-based deadline check, before the inter-tick sleep) calls it ONCE, wrapped in its own try/catch. Recovers the mid-process-disconnect case (nulled `_sql`) where postgres.js auto-reconnect can't help because the engine's pool reference is gone. Deliberately NOT a background `withRetry` (a 12s background retry could refresh a lock AFTER the worker already decided `lock-renewal-failed` and another worker reclaimed it → two holders); the reconnect is bounded by the same `callTimeoutMs` race + abort signal as renewLock itself. Pinned by `test/worker-lock-renewal.test.ts` (18 hermetic state-machine cases — no fs, no PGLite, no real setInterval).
-- `src/core/minions/worker-exit-codes.ts` — single source of truth for reserved worker process exit codes, shared by the worker (sets them) and supervisor/CLI (classify them). Exports `WORKER_EXIT_RSS_WATCHDOG = 12`. The RSS watchdog drain must be self-identifying: a code-0 exit is indistinguishable from a healthy queue-drain, so the supervisor's `code===0 → clean_exit` classifier never counted it and a respawn loop stayed invisible. A distinct code makes the drain `likely_cause=rss_watchdog`. Code 12 is deliberately outside {0 clean, 1 runtime_error} and the 128+N signal range.
+- `src/core/minions/lock-renewal-tick.ts` — Pure extracted function from `MinionWorker.launchJob`'s setInterval body; the structural fix that closes the production unhandledRejection crash class. Exports `runLockRenewalTick(deps, state) → Promise<TickResult>` + `resolveLockRenewalKnobs(env, lockDuration) → LockRenewalKnobs`. Three env knobs, all parsed as positive integers with stderr-warn-once-per-process on bad input + default fallback: `GBRAIN_LOCK_RENEWAL_MAX_FAILURES` (default 3, audit-labeling only — abort triggering is time-based), `GBRAIN_LOCK_RENEWAL_CALL_TIMEOUT_MS` (default `lockDuration/3`, bounds the hung-renewLock vector via `Promise.race`), `GBRAIN_LOCK_RENEWAL_SAFETY_MARGIN_MS` (default `lockDuration/6`, ensures release before stall-detector reclaim). The tick checks `state.cancelled()` at three guard points (entry, after await resolves, after await throws) so a long renewLock that resolves after the job ended bails cleanly. Result type is a tagged union `{kind: 'ok' | 'cancelled' | 'lock_lost' | 'should_abort', reason?}`; all `abort.abort()` / `clearInterval` side effects live in the worker's closure scope, not the pure function. Audit calls wrapped in inner try/catch (defense-in-depth). `LockRenewalDeps` has an optional `reconnect?` callback; the renewal-tick's catch branch (after retryable-error classification + the time-based deadline check, before the inter-tick sleep) calls it ONCE, wrapped in its own try/catch. Recovers the mid-process-disconnect case (nulled `_sql`) where postgres.js auto-reconnect can't help because the engine's pool reference is gone. Deliberately NOT a background `withRetry` (a 12s background retry could refresh a lock AFTER the worker already decided `lock-renewal-failed` and another worker reclaimed it → two holders); the reconnect is bounded by the same `callTimeoutMs` race + abort signal as renewLock itself. Pinned by `test/worker-lock-renewal.test.ts` (18 hermetic state-machine cases — no fs, no PGLite, no real setInterval). `LockRenewalDeps.renewLock` carries an optional per-call `{signal}`; the timeout race aborts it so the losing UPDATE releases its slot (issue #6 abandoned-racer class).
+- `src/core/minions/worker-exit-codes.ts` — single source of truth for reserved worker process exit codes, shared by the worker (sets them) and supervisor/CLI (classify them). Exports `WORKER_EXIT_RSS_WATCHDOG = 12`. The RSS watchdog drain must be self-identifying: a code-0 exit is indistinguishable from a healthy queue-drain, so the supervisor's `code===0 → clean_exit` classifier never counted it and a respawn loop stayed invisible. A distinct code makes the drain `likely_cause=rss_watchdog`. Code 12 is deliberately outside {0 clean, 1 runtime_error} and the 128+N signal range. Also reserves the `jobs run-child` codes: 13 usage/PGLite, 14 not-claimed/token-mismatch, 15 result-write-failed (result-file presence, not the exit code, classifies the normal path).
- `src/core/minions/rss-default.ts` — cgroup-aware auto-sized default for the worker RSS watchdog cap. `resolveDefaultMaxRssMb(opts?)` / `describeDefaultMaxRss(opts?)` (provenance for the startup log) / `readCgroupMemLimitBytes(readFile?)`. Replaces a flat 2048MB default at every spawn site (`jobs work`, `jobs supervisor`, autopilot, `MinionSupervisor`). Formula `clamp(round(0.5 × basisMB), 4096, 16384)` where `basis = min(cgroupLimit, totalmem)`. LOAD-BEARING NUANCE: plain `os.totalmem()` reports HOST RAM, so in a 4GB cgroup on a 126GB host it would pick 16GB, the watchdog would never fire, and the kernel OOM-killer would SIGKILL at 4GB. The cap MUST sit below the real ceiling so the graceful drain (distinct exit code, loud log) beats the kernel's silent kill; the 4096 floor applies only when it stays below the basis. Reads cgroup v2 `/sys/fs/cgroup/memory.max` (literal `max` = unlimited) then v1 `/sys/fs/cgroup/memory/memory.limit_in_bytes`. Explicit `--max-rss` (including `0` to disable) always wins. Pinned by `test/rss-default.test.ts`.
- `src/core/cycle/extract-atoms-drain.ts` — pure single-hold bounded drain for the silent lens-phase backlog. `runExtractAtomsDrain(deps, opts)` over injected deps (`withLock`, `runBatch`, `countRemaining`, `now`, optional `onBatch`) loops bounded batches under ONE continuous lock hold, **rediscovering eligibility each batch** (idempotent NOT-EXISTS-on-`source_hash`, so content mutated by a concurrent process simply doesn't match — no cross-window stale cursor), until the backlog is empty OR the time window elapses. Returns `{phase, status, extracted, skipped, remaining, batches, stopped}`. Backs `gbrain dream --phase extract_atoms --drain`. Takes the SAME `cycleLockIdFor(sourceId)` the routine cycle takes (a concurrent autopilot tick genuinely defers with `cycle_already_running`); NO release/reacquire-between-windows primitive. The shared wiring helper `runExtractAtomsDrainForSource(engine, {sourceId, windowSeconds, brainDir?, maxBatches?, onBatch?})` owns the lock+batch+count+defer wiring (dynamic imports of db-lock/cycle/extract-atoms keep the pure loop cheap to unit-test) and is the ONE drain path for three callers — `gbrain dream --drain` (which calls it), the `extract-atoms-drain` Minion handler, and autopilot auto-drain — so lock id / window / defer-on-busy can't drift. `sourceId: undefined` → legacy `gbrain-cycle` lock + `'default'` extraction; a real id → `gbrain-cycle:<id>`. `LockUnavailableError` propagates to the caller (each reports the busy case its own way). Pinned by `test/extract-atoms-drain.test.ts`.
- `scripts/check-worker-lock-renewal-shape.sh` — CI guard wired into `bun run verify`. Two invariants on `src/core/minions/worker.ts`: (1) the bug pattern `lockTimer = setInterval(async ...)` must NOT appear (narrowed via `lockTimer =` prefix so unrelated `setInterval(async)` calls — like the stall detector — don't false-fire), (2) `runLockRenewalTick` must remain referenced so the pure-function test seam survives refactors. Bug-pattern-specific by design — a future refactor to `setTimeout`-recursion or `AbortController`-based scheduling passes as long as the bug pattern stays absent. POSIX ERE + `[[:space:]]` for BSD-grep portability. Honors `GBRAIN_LOCK_RENEWAL_SHAPE_TARGET` env override for fixture-based meta-tests. Pinned by `test/scripts/check-worker-lock-renewal-shape.test.ts` (5 cases).
@@ -225,11 +226,16 @@ per-release `**vX.Y.Z:**` narration — CI enforces this
- `src/commands/quarantine.ts` — `gbrain quarantine <list|clear|scan>` operator surface for the content-quality gate. `list [--json] [--include-flagged]` paginates `listPages` and reports quarantined (HIDDEN) pages, optionally also `content_flag` (FLAGGED, searchable) pages. `clear <slug> [--force] [--no-embed] [--json]` drops both markers and re-imports through the normal pipeline so the page re-chunks + re-embeds and becomes searchable; the gate re-runs on import so genuinely-junk pages re-quarantine (exit 1) unless `--force` sets `GBRAIN_NO_SANITY=1` for that one import. `scan [--limit N] [--apply] [--no-embed] [--json]` re-assesses already-ingested pages so junk predating the gate gets marked (unchanged content short-circuits normal sync, so it never re-assesses otherwise); dry-run uses the SAME effective `content_sanity` config thresholds `--apply` will use, idempotent (skips already-marked pages), `--apply` re-imports with `forceRechunk` to set markers + (for quarantine) drop chunks. Dispatched in `cli.ts`. Pinned by `test/quarantine-cli.test.ts`.
- `src/core/zombie-reap.ts` — idempotent `installSigchldHandler()` so JS-spawned children get reaped via Bun's internal `waitpid()`. Bun (like Node) only auto-reaps when a SIGCHLD listener is registered; without it, every child the worker spawns (shell jobs, embed batches, sub-agents) becomes a zombie on exit and holds connection slots. Called once at module load from `src/cli.ts` (Windows platform guard — SIGCHLD doesn't exist on Windows). Cross-file leak guard via `_uninstallSigchldHandlerForTests()`. Layer 1 of the three-layer zombie defense; Layer 2 is tini-as-PID-1 wrapping the worker subtree (via `src/core/minions/spawn-helpers.ts`); Layer 3 is the container's own tini for hard Bun crashes.
- `src/core/minions/` — Minions job queue: BullMQ-inspired, Postgres-native (queue, worker, backoff, types, protected-names, quiet-hours, stagger, handlers/shell).
-- `src/core/minions/queue.ts` — MinionQueue class (submit, claim, complete, fail, stall detection, parent-child, depth/child-cap, per-job timeouts, cascade-kill, attachments, idempotency keys, child_done inbox, removeOnComplete/Fail). `add()` takes a 4th `trusted` arg (separate from `opts` to prevent spread leakage); protected names in `PROTECTED_JOB_NAMES` require `{allowProtectedSubmit: true}` and the check runs trim-normalized (whitespace-bypass safe). `add()` plumbs `max_stalled` through with a `[1, 100]` clamp; omitted values let the schema DEFAULT (5) kick in. `handleWallClockTimeouts(lockDurationMs)` is Layer 3 kill shot for jobs where `FOR UPDATE SKIP LOCKED` stall detection and the timeout sweep both fail to evict (wedged worker holding a row lock via a pending transaction). The submission backpressure guard covers two options sharing one `pg_advisory_xact_lock` namespace keyed on `(name, queue, source)`: `maxWaiting` (rate cap, counts `waiting` only, NULL-source-as-wildcard scope) and `maxPending` (single-flight, counts `waiting` + live-lock `active` rows where `lock_until > now()`, EXACT source scope via `COALESCE(data->>'sourceId', data->>'source_id')` — an expired-lock active never suppresses, keeping the waitingClaimable wedge detectors fed). All three coalesce return paths (idempotency fast-path, cap-hit, ON CONFLICT race fallback) stamp non-persisted `coalesced: true` metadata on the returned job. Both filter on `queue` in addition to `name` so cross-queue same-name jobs don't suppress each other. `claim` and `renewLock` issue their UPDATE via `engine.executeRawDirect` (not `executeRaw`) so the lock heartbeat runs on the direct session-mode pool that the transaction pooler won't recycle mid-hold; on PGLite this is identical to `executeRaw`. The two terminal dead-letter paths (`handleWallClockTimeouts` wall-clock kill and the stall dead-letter CTE in the stall sweep) BOTH increment `attempts_made` so a long job killed there reads as an honest attempt instead of `attempts 0 / started N`; the stall path also bumps `stalled_counter`, surfaced by `gbrain jobs get` as `Attempts: M/N (started: X, stalled: S/MaxS)`. At submit, `add()` stamps a default `timeout_ms` via `defaultTimeoutMsFor(jobName)` (from `handler-timeouts.ts`) when the caller passed none, and `claim()` COALESCEs a still-NULL `timeout_ms` from `HANDLER_DEFAULT_TIMEOUT_MS` (raw-object jsonb bind) deriving `timeout_at` from the coalesced value — the durable invariant covering rows that predate submit-time stamping; an explicit `opts.timeout_ms` always wins. Guarded by `test/queue-lock-retry.test.ts` (claim never falls back to `executeRaw`), `test/postgres-execute-raw-direct.test.ts` (routing decision matrix), and `test/minions.test.ts` (attempt accounting + default-timeout stamping). `MinionQueue.add()` gates `subagent` jobs on capability, not provider: `data.model` is classified via `classifyCapabilities()` (lazy-imported from `src/core/ai/capabilities.ts` to keep queue's eager-load surface small) and rejected only when the model cannot run a tool loop (`unusable:no_tools`) or names an unknown provider; degraded verdicts pass through with a gateway cost warning. Layer 1 of the three-layer subagent capability enforcement (layers 2+3: `model-config.ts:enforceSubagentCapable` runtime fallback + `src/commands/doctor.ts` `subagent_provider` check). Pinned by `test/agent-cli.test.ts`. All three terminal reaper paths (stall dead-letter, wall-clock kill, cascade) route through the ONE private `killJobs(tx, rows, cause, message)` tail: it emits `child_done(outcome)` to each non-terminal parent's inbox and flips any `waiting-children` parent whose last open child just died back to `waiting` — so a dead-lettered child can never strand its aggregator parent. Callers lock parents FIRST via `lockParentsOrdered` (ascending-id row locks, matching `failJob`'s parent-before-child order) so the reapers and `failJob` can never deadlock on parent/child lock acquisition. An idempotent stranded-parent sweep runs once per stall tick (~30s): any `waiting-children` parent with zero non-terminal children flips back to `waiting` (single UPDATE, NOT EXISTS on an indexed FK) — self-heals every stranding class, including parents stranded before the sweep existed. Every automatic re-run path clears `started_at` (`failJob`'s delayed retry branch, the stall requeue, lease release, `promoteDelayed`, and the parent-unblock flips) so the per-attempt wall clock measures execution, not backoff/queue wait — a retried job can't be dead-lettered by the wall-clock sweep before executing a line. Pinned by `test/queue-stall-parent-unblock.test.ts` + `test/queue-started-at-retry.test.ts`.
-- `src/core/minions/worker.ts` — MinionWorker class (handler registry, lock renewal, graceful shutdown, timeout safety net). Aborted jobs call `failJob` with reason (`timeout`/`cancel`/`lock-lost`/`shutdown`); `shutdownAbort` (instance field) fires on SIGTERM/SIGINT and propagates to `ctx.shutdownSignal` (shell handler listens; non-shell handlers don't). Per-job timeout fires `abort.abort(new Error('timeout'))` then a 30s grace-then-evict safety net force-evicts the job from `inFlight` and marks it dead if the handler ignores the abort signal (the moved-out generic abort listener now fires for ANY abort reason). The `launchJob` lock-renewal block is a thin sync wrapper around the pure `runLockRenewalTick` from `src/core/minions/lock-renewal-tick.ts` (NEVER `setInterval(async () => await renewLock(...))` — that shape was the unhandledRejection crash class during PgBouncer rotation). Gaps closed: (1) `cancelled` flag captured in the timer closure stops in-flight IIFEs writing misleading audit events after the job ended; (2) re-entrancy guard `tickInFlight` + per-call `Promise.race` timeout (`GBRAIN_LOCK_RENEWAL_CALL_TIMEOUT_MS`, default `lockDuration/3`); (3) time-based abort (`Date.now() - lastSuccessfulRenewalAt >= lockDuration - GBRAIN_LOCK_RENEWAL_SAFETY_MARGIN_MS`) so we release the lock BEFORE another worker can reclaim; (4) explicit `.catch()` on the stored `executeJob(...).finally(...)` promise closes the second unhandledRejection vector; (5) exported `INFRASTRUCTURE_ABORT_REASONS = new Set(['lock-renewal-failed', 'lock-lost'])` so executeJob's catch skips `failJob` for these (PgBouncer blips don't dead-letter healthy jobs; the stall detector reclaims). CI guard `scripts/check-worker-lock-renewal-shape.sh` (in `bun run verify`) asserts the bug pattern stays absent AND `launchJob` keeps calling `runLockRenewalTick`. Engine-ownership invariant: `start()` does NOT call `engine.disconnect()` on shutdown — the CLI handler in `src/commands/jobs.ts case 'work'` owns engine lifecycle via try/finally with loud error logging. RSS watchdog uses non-file-backed pages on Linux: exported `parseRssFromProcStatus(status)` (pure parser; field-presence regex so `RssAnon: 0 + RssShmem: 512` parses correctly) and `getAccurateRss(readStatus?)` (reads `/proc/self/status` for `RssAnon + RssShmem`, falls back to `process.memoryUsage().rss` on macOS / restricted containers / kernel <4.5); the default `getRss` in `WorkerOpts` is `getAccurateRss`. `checkMemoryLimit` tracks peak RSS, fires an 80%-of-cap soft-warn (once per crossing, carrying peak + in-flight job kinds), and on exceed sets `_rssWatchdogTriggered=true` (exposed via `get rssWatchdogTriggered()`) so `jobs work`'s finally can `process.exit(WORKER_EXIT_RSS_WATCHDOG)` after disconnect (drain self-identifying instead of an opaque code-0 exit). The poll loop wraps `claim` in try/catch: on a retryable conn error it reconnects ONCE and continues to the next tick rather than blind-retrying (a retry after `UPDATE...RETURNING` committed but the socket died would double-claim). `LockRenewalDeps` is wired with `reconnect` when the engine supports it. The self-health DB-liveness probe now runs EVEN under a supervisor (`GBRAIN_SUPERVISED=1`): the outer guard is `if (this.opts.healthCheckInterval > 0)` and only the STALL-detection block is wrapped in `if (!isSupervisedChild)` — so a supervised worker whose own pool dies self-exits `unhealthy(db_dead)` after `dbFailExitAfter` probes (the supervisor watches a different connection and can't see this worker's dead pool), while the supervisor's progress watchdog owns forward-progress (#1801). Pinned by `test/worker-lock-renewal.test.ts` (18), `test/audit/lock-renewal-audit.test.ts` (11), `test/scripts/check-worker-lock-renewal-shape.test.ts` (5), `test/worker-shutdown-disconnect.test.ts` (asserts `disconnectSpy).not.toHaveBeenCalled()`), `test/worker-rss.test.ts` (11), `test/worker-supervised-db-probe.test.ts` (3).
-- `src/core/minions/supervisor.ts` — MinionSupervisor process manager. Spawns `gbrain jobs work` as a child, restarts on crash with exponential backoff, periodic health check. `consecutiveHealthFailures` counter; on 3 consecutive failures emits `health_warn` with `reason: 'db_connection_degraded'` and calls `engine.reconnect()` to swap in a fresh pool, then resets. Worker exit classifier emits `likely_cause` on `worker_exited` events: `oom_or_external_kill` (SIGKILL), `graceful_shutdown` (SIGTERM), `runtime_error` (code 1), `clean_exit` (code 0), `unknown`. Consumes `detectTini()` + `buildSpawnInvocation()` from `src/core/minions/spawn-helpers.ts` to wrap the worker subtree in tini-as-PID-1 when tini is on `PATH` (handles native-addon zombie reaping the in-process SIGCHLD reaper can't reach); exposes `isTiniDetected` read-only accessor. The spawn-and-respawn loop is the shared `ChildWorkerSupervisor` core: MinionSupervisor composes it via `runSuperviseLoop()` → `new ChildWorkerSupervisor({...})` and maps `ChildSupervisorEvent` back through `emit()` SupervisorEvent (JSONL audit consumers see byte-compatible output). PID lock, signal handlers, health check, and `process.exit` on the HARD crash ceiling stay in MinionSupervisor. Crossing the SOFT budget (`maxCrashes`) no longer permanently gives up (#1994/#2227): the core drops into degraded retry (capped backoff + a `crash_budget_degraded` health_warn) and self-heals when a respawn runs stably; permanent `process.exit(MAX_CRASHES)` fires only at the hard ceiling `resolveHardStopMaxCrashes(maxCrashes)` (default `maxCrashes × 10`, env `GBRAIN_SUPERVISOR_HARD_STOP_CRASHES`, `0` = never). Separately, `gbrain jobs supervisor status` + `gbrain doctor` detect a live supervisor through this queue lock (`inspectLock` + `isLockHolderLive`, freshness-keyed so PID reuse can't false-positive) when the `$HOME`-derived pidfile is absent, so a split-`$HOME` deployment no longer reads a healthy supervisor as "not running" (#2227). `code=0` leaves `crashCount` untouched (so a worker alternating real crashes + watchdog drains still trips `max_crashes`); `cleanRestartBudget` (default 10 restarts per 60s) caps the macOS/non-Linux-fallback tight-loop via `health_warn { reason: 'clean_restart_budget_exceeded' }` + backoff. `shutdown()` drains via `childSupervisor.killChild('SIGTERM')` + `awaitChildExit(35_000)`. Progress watchdog (#1801): `healthCheck()` restarts an alive-but-wedged child via `childSupervisor.restartCurrentChild(35_000)` when a queue has claimable work, 0 live-lock active jobs, and stale completions across `wedgeRestartChecks` (default 3) consecutive checks past `wedgeRestartMinutes` (default 15, 0 disables) + a `startupGraceMs` window; bounded by `wedgeRestartLoopBudget` (default 3 / `wedgeRestartLoopWindowMs`) which switches to a one-shot `wedge_restart_loop` alert. The wedge query is the exported `queryWedgeSignals(engine, queue, handlerNames)` — name+queue-scoped, `active_healthy` = live-lock only (an expired-lock active row does NOT mask the wedge), due-delayed counted. Claimable names are derived at start via a throwaway `registerBuiltinHandlers` worker (its new `quiet` opt). Flags `--wedge-restart-minutes` / `--wedge-restart-checks` + env `GBRAIN_WEDGE_RESTART_MINUTES` / `GBRAIN_WEDGE_RESTART_CHECKS`. The worker argv is built by the exported pure `buildWorkerArgs(opts)` (appends `--nice N` when `opts.nice_requested` is set, alongside `--concurrency`/`--queue`/`--max-rss`); the niceness apply RESULT (`nice_requested`/`nice_effective`/`nice_error`, computed by the CLI in jobs.ts — the supervisor doesn't call setPriority) rides on the `started`/`worker_spawned` audit emissions (#1815). Queue-scoped singleton (#1849): the real authority is a DB lock (`tryAcquireDbLock` from `src/core/db-lock.ts`) keyed on `supervisorLockId(queue)` = `gbrain-supervisor:<queue>` — keyed on the QUEUE ALONE because the lock row lives inside the target database, so the (database) half of the mutex is physical, not part of the key (an earlier revision mixed in a config-derived DB identity, which let two supervisors on the same physical DB via different-but-equivalent URLs compute different ids and both acquire — fixed). Two supervisors with different `$HOME`/`--pid-file` against the same `(database, queue)` no longer both run with conflicting `--max-rss`; the second exits `LOCK_HELD`. The pidfile-cleanup `process.on('exit')` listener is installed BEFORE the DB-lock acquisition so the `LOCK_HELD` early-exit can't strand the pidfile this process just created. The default pidfile is now brain-scoped (`supervisor-<brainId>.pid`) so different brains under one HOME don't false-block. The lock refreshes on its own `setInterval` (TTL 5min, refresh 60s, max 3 failures = 180s < TTL); a refresh that THROWS past the threshold exits `LOCK_LOST` (code 4) rather than risk a split-brain, while a fenced refresh returning `false` (0 rows matched — the lock was stolen or force-cleared) is treated as CERTAIN loss, not a blip: the supervisor emits `health_error { reason: 'supervisor_lock_lost' }` and exits `LOCK_LOST` immediately (counting it toward the failure threshold would let two supervisors drain the same queue for up to two more refresh windows). `shutdown()` releases the lock so a clean restart re-acquires immediately. The `started` audit now records `max_rss_mb` so `gbrain doctor`'s `supervisor_singleton` check can surface the effective cap. Exports `supervisorLockId()` and the pure `classifySupervisorSingleton({lockLive, lockHolderHost, lockHolderPid, localHost, localPid}) → 'no_lock'|'single'|'mismatch'` (host+pid compare, bare pid meaningless cross-host) that doctor consumes. Pinned by `test/supervisor.test.ts` (16 cases), `test/supervisor-tini.test.ts`, `test/supervisor-wedge.test.ts`, `test/supervisor-build-worker-args.test.ts`, and `test/supervisor-db-lock.test.ts`.
+- `src/core/minions/queue.ts` — MinionQueue class (submit, claim, complete, fail, stall detection, parent-child, depth/child-cap, per-job timeouts, cascade-kill, attachments, idempotency keys, child_done inbox, removeOnComplete/Fail). `add()` takes a 4th `trusted` arg (separate from `opts` to prevent spread leakage); protected names in `PROTECTED_JOB_NAMES` require `{allowProtectedSubmit: true}` and the check runs trim-normalized (whitespace-bypass safe). `add()` plumbs `max_stalled` through with a `[1, 100]` clamp; omitted values let the schema DEFAULT (5) kick in. `handleWallClockTimeouts(lockDurationMs)` is Layer 3 kill shot for jobs where `FOR UPDATE SKIP LOCKED` stall detection and the timeout sweep both fail to evict (wedged worker holding a row lock via a pending transaction). The submission backpressure guard covers two options sharing one `pg_advisory_xact_lock` namespace keyed on `(name, queue, source)`: `maxWaiting` (rate cap, counts `waiting` only, NULL-source-as-wildcard scope) and `maxPending` (single-flight, counts `waiting` + live-lock `active` rows where `lock_until > now()`, EXACT source scope via `COALESCE(data->>'sourceId', data->>'source_id')` — an expired-lock active never suppresses, keeping the waitingClaimable wedge detectors fed). All three coalesce return paths (idempotency fast-path, cap-hit, ON CONFLICT race fallback) stamp non-persisted `coalesced: true` metadata on the returned job. Both filter on `queue` in addition to `name` so cross-queue same-name jobs don't suppress each other. `claim` and `renewLock` issue their UPDATE via `engine.executeRawDirect` (not `executeRaw`) so the lock heartbeat runs on the direct session-mode pool that the transaction pooler won't recycle mid-hold; on PGLite this is identical to `executeRaw`. The two terminal dead-letter paths (`handleWallClockTimeouts` wall-clock kill and the stall dead-letter CTE in the stall sweep) BOTH increment `attempts_made` so a long job killed there reads as an honest attempt instead of `attempts 0 / started N`; the stall path also bumps `stalled_counter`, surfaced by `gbrain jobs get` as `Attempts: M/N (started: X, stalled: S/MaxS)`. At submit, `add()` stamps a default `timeout_ms` via `defaultTimeoutMsFor(jobName)` (from `handler-timeouts.ts`) when the caller passed none, and `claim()` COALESCEs a still-NULL `timeout_ms` from `HANDLER_DEFAULT_TIMEOUT_MS` (raw-object jsonb bind) deriving `timeout_at` from the coalesced value — the durable invariant covering rows that predate submit-time stamping; an explicit `opts.timeout_ms` always wins. Guarded by `test/queue-lock-retry.test.ts` (claim never falls back to `executeRaw`), `test/postgres-execute-raw-direct.test.ts` (routing decision matrix), and `test/minions.test.ts` (attempt accounting + default-timeout stamping). `MinionQueue.add()` gates `subagent` jobs on capability, not provider: `data.model` is classified via `classifyCapabilities()` (lazy-imported from `src/core/ai/capabilities.ts` to keep queue's eager-load surface small) and rejected only when the model cannot run a tool loop (`unusable:no_tools`) or names an unknown provider; degraded verdicts pass through with a gateway cost warning. Layer 1 of the three-layer subagent capability enforcement (layers 2+3: `model-config.ts:enforceSubagentCapable` runtime fallback + `src/commands/doctor.ts` `subagent_provider` check). Pinned by `test/agent-cli.test.ts`. All three terminal reaper paths (stall dead-letter, wall-clock kill, cascade) route through the ONE private `killJobs(tx, rows, cause, message)` tail: it emits `child_done(outcome)` to each non-terminal parent's inbox and flips any `waiting-children` parent whose last open child just died back to `waiting` — so a dead-lettered child can never strand its aggregator parent. Callers lock parents FIRST via `lockParentsOrdered` (ascending-id row locks, matching `failJob`'s parent-before-child order) so the reapers and `failJob` can never deadlock on parent/child lock acquisition. An idempotent stranded-parent sweep runs once per stall tick (~30s): any `waiting-children` parent with zero non-terminal children flips back to `waiting` (single UPDATE, NOT EXISTS on an indexed FK) — self-heals every stranding class, including parents stranded before the sweep existed. Every automatic re-run path clears `started_at` (`failJob`'s delayed retry branch, the stall requeue, lease release, `promoteDelayed`, and the parent-unblock flips) so the per-attempt wall clock measures execution, not backoff/queue wait — a retried job can't be dead-lettered by the wall-clock sweep before executing a line. Pinned by `test/queue-stall-parent-unblock.test.ts` + `test/queue-started-at-retry.test.ts`. `renewLock` accepts optional `{signal}` forwarded to executeRawDirect so the renewal tick's timeout race CANCELS a hung UPDATE instead of orphaning a checked-out pool slot.
+- `src/core/minions/worker.ts` — MinionWorker class (handler registry, lock renewal, graceful shutdown, timeout safety net). Aborted jobs call `failJob` with reason (`timeout`/`cancel`/`lock-lost`/`shutdown`); `shutdownAbort` (instance field) fires on SIGTERM/SIGINT and propagates to `ctx.shutdownSignal` (shell handler listens; non-shell handlers don't). Per-job timeout fires `abort.abort(new Error('timeout'))` then a 30s grace-then-evict safety net force-evicts the job from `inFlight` and marks it dead if the handler ignores the abort signal (the moved-out generic abort listener now fires for ANY abort reason). The `launchJob` lock-renewal block is a thin sync wrapper around the pure `runLockRenewalTick` from `src/core/minions/lock-renewal-tick.ts` (NEVER `setInterval(async () => await renewLock(...))` — that shape was the unhandledRejection crash class during PgBouncer rotation). Gaps closed: (1) `cancelled` flag captured in the timer closure stops in-flight IIFEs writing misleading audit events after the job ended; (2) re-entrancy guard `tickInFlight` + per-call `Promise.race` timeout (`GBRAIN_LOCK_RENEWAL_CALL_TIMEOUT_MS`, default `lockDuration/3`); (3) time-based abort (`Date.now() - lastSuccessfulRenewalAt >= lockDuration - GBRAIN_LOCK_RENEWAL_SAFETY_MARGIN_MS`) so we release the lock BEFORE another worker can reclaim; (4) explicit `.catch()` on the stored `executeJob(...).finally(...)` promise closes the second unhandledRejection vector; (5) exported `INFRASTRUCTURE_ABORT_REASONS = new Set(['lock-renewal-failed', 'lock-lost'])` so executeJob's catch skips `failJob` for these (PgBouncer blips don't dead-letter healthy jobs; the stall detector reclaims). CI guard `scripts/check-worker-lock-renewal-shape.sh` (in `bun run verify`) asserts the bug pattern stays absent AND `launchJob` keeps calling `runLockRenewalTick`. Engine-ownership invariant: `start()` does NOT call `engine.disconnect()` on shutdown — the CLI handler in `src/commands/jobs.ts case 'work'` owns engine lifecycle via try/finally with loud error logging. RSS watchdog uses non-file-backed pages on Linux: exported `parseRssFromProcStatus(status)` (pure parser; field-presence regex so `RssAnon: 0 + RssShmem: 512` parses correctly) and `getAccurateRss(readStatus?)` (reads `/proc/self/status` for `RssAnon + RssShmem`, falls back to `process.memoryUsage().rss` on macOS / restricted containers / kernel <4.5); the default `getRss` in `WorkerOpts` is `getAccurateRss`. `checkMemoryLimit` tracks peak RSS, fires an 80%-of-cap soft-warn (once per crossing, carrying peak + in-flight job kinds), and on exceed sets `_rssWatchdogTriggered=true` (exposed via `get rssWatchdogTriggered()`) so `jobs work`'s finally can `process.exit(WORKER_EXIT_RSS_WATCHDOG)` after disconnect (drain self-identifying instead of an opaque code-0 exit). The poll loop wraps `claim` in try/catch: on a retryable conn error it reconnects ONCE and continues to the next tick rather than blind-retrying (a retry after `UPDATE...RETURNING` committed but the socket died would double-claim). `LockRenewalDeps` is wired with `reconnect` when the engine supports it. The self-health DB-liveness probe now runs EVEN under a supervisor (`GBRAIN_SUPERVISED=1`): the outer guard is `if (this.opts.healthCheckInterval > 0)` and only the STALL-detection block is wrapped in `if (!isSupervisedChild)` — so a supervised worker whose own pool dies self-exits `unhealthy(db_dead)` after `dbFailExitAfter` probes (the supervisor watches a different connection and can't see this worker's dead pool), while the supervisor's progress watchdog owns forward-progress (#1801). Pinned by `test/worker-lock-renewal.test.ts` (18), `test/audit/lock-renewal-audit.test.ts` (11), `test/scripts/check-worker-lock-renewal-shape.test.ts` (5), `test/worker-shutdown-disconnect.test.ts` (asserts `disconnectSpy).not.toHaveBeenCalled()`), `test/worker-rss.test.ts` (11), `test/worker-supervised-db-probe.test.ts` (3). Per-job process isolation: `MinionWorkerOpts.jobIsolation` (`inline`|`process`) + `childCliInvocation`/`childTiniPath` swap `handler(context)` for `runJobInChild(...)` in executeJob — every reporting branch (completeJob, failJob, lease release, infra no-burn) is reused on the child outcome; when isolated the parent-side context is not built at all. Three extra no-burn child classes in the catch: `ChildSpawnInfraError` + `ChildWorkerShutdownError` (released, no attempt burned) and `ChildNotClaimedError` (the child proved the claim was already gone — reclaimed/cancelled before the handler ran — so nothing is recorded against it). The health probe delegates to `runDbProbe` (db-probe.ts) with a cancellation signal on every probe and emits a `verdict` (`pool_starved`/`server_unreachable`/`unknown`) on the `db_dead` unhealthy payload. `getHandler(name)` is the read-only registry accessor run-child uses.
+- `src/core/minions/supervisor.ts` — MinionSupervisor process manager. Spawns `gbrain jobs work` as a child, restarts on crash with exponential backoff, periodic health check. `consecutiveHealthFailures` counter; on 3 consecutive failures emits `health_warn` with `reason: 'db_connection_degraded'` and calls `engine.reconnect()` to swap in a fresh pool, then resets. Worker exit classifier emits `likely_cause` on `worker_exited` events: `oom_or_external_kill` (SIGKILL), `graceful_shutdown` (SIGTERM), `runtime_error` (code 1), `clean_exit` (code 0), `unknown`. Consumes `detectTini()` + `buildSpawnInvocation()` from `src/core/minions/spawn-helpers.ts` to wrap the worker subtree in tini-as-PID-1 when tini is on `PATH` (handles native-addon zombie reaping the in-process SIGCHLD reaper can't reach); exposes `isTiniDetected` read-only accessor. The spawn-and-respawn loop is the shared `ChildWorkerSupervisor` core: MinionSupervisor composes it via `runSuperviseLoop()` → `new ChildWorkerSupervisor({...})` and maps `ChildSupervisorEvent` back through `emit()` SupervisorEvent (JSONL audit consumers see byte-compatible output). PID lock, signal handlers, health check, and `process.exit` on the HARD crash ceiling stay in MinionSupervisor. Crossing the SOFT budget (`maxCrashes`) no longer permanently gives up (#1994/#2227): the core drops into degraded retry (capped backoff + a `crash_budget_degraded` health_warn) and self-heals when a respawn runs stably; permanent `process.exit(MAX_CRASHES)` fires only at the hard ceiling `resolveHardStopMaxCrashes(maxCrashes)` (default `maxCrashes × 10`, env `GBRAIN_SUPERVISOR_HARD_STOP_CRASHES`, `0` = never). Separately, `gbrain jobs supervisor status` + `gbrain doctor` detect a live supervisor through this queue lock (`inspectLock` + `isLockHolderLive`, freshness-keyed so PID reuse can't false-positive) when the `$HOME`-derived pidfile is absent, so a split-`$HOME` deployment no longer reads a healthy supervisor as "not running" (#2227). `code=0` leaves `crashCount` untouched (so a worker alternating real crashes + watchdog drains still trips `max_crashes`); `cleanRestartBudget` (default 10 restarts per 60s) caps the macOS/non-Linux-fallback tight-loop via `health_warn { reason: 'clean_restart_budget_exceeded' }` + backoff. `shutdown()` drains via `childSupervisor.killChild('SIGTERM')` + `awaitChildExit(35_000)`. Progress watchdog (#1801): `healthCheck()` restarts an alive-but-wedged child via `childSupervisor.restartCurrentChild(35_000)` when a queue has claimable work, 0 live-lock active jobs, and stale completions across `wedgeRestartChecks` (default 3) consecutive checks past `wedgeRestartMinutes` (default 15, 0 disables) + a `startupGraceMs` window; bounded by `wedgeRestartLoopBudget` (default 3 / `wedgeRestartLoopWindowMs`) which switches to a one-shot `wedge_restart_loop` alert. The wedge query is the exported `queryWedgeSignals(engine, queue, handlerNames)` — name+queue-scoped, `active_healthy` = live-lock only (an expired-lock active row does NOT mask the wedge), due-delayed counted. Claimable names are derived at start via a throwaway `registerBuiltinHandlers` worker (its new `quiet` opt). Flags `--wedge-restart-minutes` / `--wedge-restart-checks` + env `GBRAIN_WEDGE_RESTART_MINUTES` / `GBRAIN_WEDGE_RESTART_CHECKS`. The worker argv is built by the exported pure `buildWorkerArgs(opts)` (appends `--nice N` when `opts.nice_requested` is set, alongside `--concurrency`/`--queue`/`--max-rss`); the niceness apply RESULT (`nice_requested`/`nice_effective`/`nice_error`, computed by the CLI in jobs.ts — the supervisor doesn't call setPriority) rides on the `started`/`worker_spawned` audit emissions (#1815). Queue-scoped singleton (#1849): the real authority is a DB lock (`tryAcquireDbLock` from `src/core/db-lock.ts`) keyed on `supervisorLockId(queue)` = `gbrain-supervisor:<queue>` — keyed on the QUEUE ALONE because the lock row lives inside the target database, so the (database) half of the mutex is physical, not part of the key (an earlier revision mixed in a config-derived DB identity, which let two supervisors on the same physical DB via different-but-equivalent URLs compute different ids and both acquire — fixed). Two supervisors with different `$HOME`/`--pid-file` against the same `(database, queue)` no longer both run with conflicting `--max-rss`; the second exits `LOCK_HELD`. The pidfile-cleanup `process.on('exit')` listener is installed BEFORE the DB-lock acquisition so the `LOCK_HELD` early-exit can't strand the pidfile this process just created. The default pidfile is now brain-scoped (`supervisor-<brainId>.pid`) so different brains under one HOME don't false-block. The lock refreshes on its own `setInterval` (TTL 5min, refresh 60s, max 3 failures = 180s < TTL); a refresh that THROWS past the threshold exits `LOCK_LOST` (code 4) rather than risk a split-brain, while a fenced refresh returning `false` (0 rows matched — the lock was stolen or force-cleared) is treated as CERTAIN loss, not a blip: the supervisor emits `health_error { reason: 'supervisor_lock_lost' }` and exits `LOCK_LOST` immediately (counting it toward the failure threshold would let two supervisors drain the same queue for up to two more refresh windows). `shutdown()` releases the lock so a clean restart re-acquires immediately. The `started` audit now records `max_rss_mb` so `gbrain doctor`'s `supervisor_singleton` check can surface the effective cap. Exports `supervisorLockId()` and the pure `classifySupervisorSingleton({lockLive, lockHolderHost, lockHolderPid, localHost, localPid}) → 'no_lock'|'single'|'mismatch'` (host+pid compare, bare pid meaningless cross-host) that doctor consumes. Pinned by `test/supervisor.test.ts` (16 cases), `test/supervisor-tini.test.ts`, `test/supervisor-wedge.test.ts`, `test/supervisor-build-worker-args.test.ts`, and `test/supervisor-db-lock.test.ts`. `SupervisorOpts.jobIsolation` passes `--job-isolation process` to the spawned worker via a CONDITIONAL `buildWorkerArgs` push (inline argv stays byte-identical). `queryWedgeSignals`/`probeQueueState` thread a per-probe AbortSignal; the probe timeout cancels the losing query (pool-slot release under exhaustion).
- `src/core/minions/child-worker-supervisor.ts` — shared spawn-and-respawn core reused by both `MinionSupervisor` (standalone `gbrain jobs supervisor` daemon) and `src/commands/autopilot.ts` (autopilot daemon), so the two consumers can't drift into parallel-loop bugs. Pure class: NO PID file, NO signal handlers, NO `process.exit`, NO health check. Lifecycle events fire via injected `onEvent: (ChildSupervisorEvent) => void`. Exit classifier: `code === 0` leaves `crashCount` UNCHANGED (preserves flap detection across mixed exit sequences); `code != 0` follows `runDuration > stableRunResetMs ? 1 : ++crashCount`. Clean-restart budget: sliding window of code=0 exits; when count exceeds `cleanRestartBudget` (default 10) inside `cleanRestartWindowMs` (default 60s), emits `health_warn { reason: 'clean_restart_budget_exceeded' }` and applies `cleanRestartBudgetBackoffMs` (default 1s). The exit classifier special-cases `WORKER_EXIT_RSS_WATCHDOG` — `likely_cause='rss_watchdog'`, and that exit does NOT bump `crashCount` (routes to its own breaker); a dedicated `_watchdogExitTimestamps` sliding window trips a loud `rss_watchdog_loop` health_warn naming the cap when N watchdog exits land inside the window, INDEPENDENT of the stable-run reset (which would otherwise hide a >5-min-run watchdog drain loop). New opts `watchdogLoopBudget` (3), `watchdogLoopWindowMs` (600000), `watchdogBackoffMs` (30000); `ChildSupervisorEvent` extended. Public read-only accessors `childAlive`, `inBackoff`, `crashCount`; `killChild(signal)` gates on liveness (`exitCode === null && signalCode === null`), NOT `.killed` — `.killed` flips true once a signal is *sent*, so the old `!this._child.killed` guard made a follow-up SIGKILL after an ignored SIGTERM a silent no-op (#1801; the bug also lived in the `shutdown()` drain). `restartCurrentChild(graceMs)` (#1801 wedge self-heal) captures the CURRENT child ref, SIGTERM→grace→SIGKILLs THAT ref (never the respawn — closes the timer-kills-fresh-worker race), and flags `_intentionalRestart` so the exit is `likelyCause='wedge_restart'`, leaves `crashCount` UNTOUCHED (never trips `max_crashes`; like `rss_watchdog`), and respawns immediately (`backoff ms:0 reason='wedge_restart'`). `awaitChildExit(timeoutMs)` short-circuits when `child.exitCode !== null || child.signalCode !== null` so fast-SIGTERM responders don't cause a 35s shutdown hang. Degraded-retry (#1994/#2227): the `run()` loop no longer fires `onMaxCrashesExceeded` at the soft `maxCrashes`; it announces `health_warn { reason: 'crash_budget_degraded' }` once per episode and keeps respawning with capped backoff (the 60s cap makes it a paced retry, not a hot loop), re-arming after a stable-run reset drops the count. Permanent give-up fires only at `hardStopMaxCrashes` (default `maxCrashes × HARD_STOP_CRASH_MULTIPLIER` = 10×; `0` disables). Test hooks `_backoffFloorMs`, `_now`. `supervisor-audit.ts` adds `rss_watchdog` as a non-clean cause + its own `CrashSummary.by_cause` bucket, and `wedge_restart` to `CLEAN_EXIT_CAUSES` (a self-heal, not a crash; denylist preserved so future causes route to `legacy`). Pinned by `test/child-worker-supervisor.test.ts` (12 cases).
- `src/core/minions/spawn-helpers.ts` — pure `detectTini()` + `buildSpawnInvocation()` consumed by both `supervisor.ts` and `autopilot.ts` (resolves the DRY violation between the two spawn sites and makes tini wrapping testable without `mock.module()`, rule R2 of `scripts/check-test-isolation.sh`). `detectTini()` calls `execFileSync('which', ['tini'])` with explicit `env: process.env` so Bun sees runtime PATH mutations. `buildSpawnInvocation(tiniPath, cmd, args)` returns `{cmd, args}` with tini prepended when present, or the bare invocation otherwise. Pinned by `test/spawn-helpers.test.ts` (5) and `test/supervisor-tini.test.ts` (4).
+- `src/core/minions/job-isolation.ts` — per-job process-isolation protocol (issue #5): atomic outcome-file codec (`writeChildOutcomeFile` tmp+rename, `decodeChildOutcomeFile` with a 32MiB cap that throws `UnrecoverableError` — oversize results die loudly on attempt 1, decode errors report byte counts never content), `encodeHandlerError`/`reconstructHandlerError` preserving the two `instanceof` classes executeJob branches on (UnrecoverableError, RateLeaseUnavailableError), the child argv/env contract (`CHILD_ENV`, `buildChildArgs`), `resolveChildCliInvocation` (env override → compiled binary → bun-dev fallback → null for fail-fast), and `killProcessGroup(pid, sig)` — children are spawned detached (own group) because SIGKILL on a tini pid alone orphans the handler grandchild, and Bun rejects negative pids in process.kill (oven-sh/bun#15791) so group signaling falls back to POSIX /bin/kill. Pinned by `test/job-isolation-protocol.test.ts` (incl. real-process grandchild-death).
+- `src/core/minions/child-job-runner.ts` — parent-side runner: `runJobInChild(opts)` spawns the child (detached + tini when available, stdio inherit for handler logs, per-job lifecycle log lines), maps per-job abort → group SIGTERM now + group SIGKILL at +25s (`CHILD_KILL_GRACE_MS`, inside the 30s force-evict backstop), gives worker-shutdown children the drain window to finish AND report (a non-reporting shutdown kill throws `ChildWorkerShutdownError` → released, no attempt burned), classifies pre-exec spawn failure as `ChildSpawnInfraError` (released, no attempt burned), and bounds child pools via env (`GBRAIN_POOL_SIZE` default 3, `GBRAIN_DIRECT_POOL_SIZE=1`). Pinned by `test/child-job-runner.test.ts` (real children) + `test/worker-job-isolation.test.ts` (full parent path on PGLite).
+- `src/core/minions/run-child.ts` — `runChildJobEntry(engine, opts, injectables)`: the `jobs run-child` core. Re-reads the job row and validates status+token (exit 14 on mismatch, handler never runs), builds the shared token-fenced context against the CHILD's engine, runs the handler, writes ONE atomic outcome file (handler failure = reported outcome = exit 0; only write-failure exits 15). Installs a SIGTERM→shutdownSignal-ONLY handler (inline signal-separation parity: ctx.signal stays live so cooperative handlers finish + report inside the drain window; the parent's group SIGKILL at drain end is the backstop) and a parent-liveness watchdog polling `process.kill(parentPid, 0)` (ppid checks are dead code under tini) that aborts BOTH signals on parent death with a hard exit after grace. No worker machinery (parent owns liveness). Pinned by `test/run-child-entry.test.ts`.
+- `src/core/minions/job-context.ts` — shared `buildJobContext(engine, queue, job, lockToken, signal, shutdownSignal)` (extracted verbatim from executeJob) so inline mode and the run-child child wire identical token-fenced DB callbacks.
+- `src/core/minions/db-probe.ts` — hermetic DB-liveness probe with pool-starvation disambiguation (issue #6): `runDbProbe(deps)` probes the read pool (signal-cancelled at `timeoutMs`), on failure probes the DIRECT lane (`DIRECT_PROBE_TIMEOUT_MS`, only when dual-pool is genuinely active) and returns a verdict — `pool_starved` ("server IS reachable; fault is in the transaction-pooler path — client pool exhaustion or a pooler-layer fault", deliberately an honest disjunction), `server_unreachable`, or `unknown`. Gauge counts render as a labeled tracked SUBSET; no waiter/available arithmetic. Pinned by `test/db-probe.test.ts`.
- `src/core/minions/niceness.ts` — OS scheduling-priority (niceness) primitives for the `--nice` flag. `parseNiceValue(raw)` whole-string parses + range-validates to POSIX `[-20, 19]` (rejects `"3.5"`/`"10abc"` that `parseInt` would silently truncate). `applyNiceness(nice, setPriority?, getPriority?)` calls `os.setPriority(0, n)` and ALWAYS re-reads `os.getPriority(0)` afterwards — in both the success and the catch paths — so a denied renice (EPERM) or an `RLIMIT_NICE` clamp records the real effective value (e.g. `0`), not null; returns `{applied, requested, effective, error?}`. `getEffectiveNiceness(pid, getPriority?)` reads an arbitrary pid's niceness (null on dead/unreadable). `formatNice(n)` → `+10`/`0`/`-5`. Applied only at the CLI layer (jobs.ts) so worker.ts/supervisor.ts stay embeddable. Pinned by `test/niceness.test.ts`.
- `src/core/minions/worker-registry.ts` — live worker registry backing niceness observability. Each running `gbrain jobs work` self-registers `worker-<pid>.json` under `gbrainPath('workers')` (brain-isolated via `GBRAIN_HOME`; entries tagged with `currentBrainId()` so multiple DBs under one home don't cross-report). `registerWorker(info)` is best-effort (never blocks the worker) and returns a cleanup fn the caller wires to BOTH the shutdown `finally` AND `process.on('exit')` (the unhealthy `process.exit(1)` bypasses the awaited finally). `readWorkers(getNice?)` enumerates the dir, drops confirmed-dead pids (`classifyLiveness`: ESRCH = dead/prune, EPERM = alive/keep), applies a pid-reuse start-time guard (`ps -o lstart`, rejects a pid whose process started >5s after the entry was written), and re-measures each live worker's niceness now. Reports the worker's REAL pid, sidestepping the tini-wrapper-pid problem. Pinned by `test/worker-registry.test.ts`.
- `src/core/minions/supervisor-pid.ts` — `readSupervisorPid(pidFile) → {pid, running}`: the shared `existsSync → readFileSync → parseInt → process.kill(pid,0)` PID-file + liveness reader extracted from the three copies in `jobs.ts` (supervisor status), `jobs.ts` (stats), and `doctor.ts`. EPERM from the liveness probe counts as running. Pinned by `test/supervisor-pid.test.ts`.
@@ -264,7 +270,7 @@ per-release `**vX.Y.Z:**` narration — CI enforces this
- `src/core/minions/attachments.ts` — Attachment validation (path traversal, null byte, oversize, base64, duplicate detection).
- `src/commands/agent.ts` — `gbrain agent run <prompt> [flags]` CLI. Submits `subagent` (or N children + 1 aggregator) under `{allowProtectedSubmit: true}`. Single-entry `--fanout-manifest` short-circuits. Children get `on_child_fail: 'continue'` + `max_stalled: 3`. `--follow` is the default on TTY; streams logs + polls `waitForCompletion` in parallel. Ctrl-C detaches, does not cancel.
- `src/commands/agent-logs.ts` — `gbrain agent logs <job> [--follow] [--since]`. Merges JSONL heartbeat audit + `subagent_messages` into a chronological timeline. `parseSince` accepts ISO-8601 or relative (`5m`, `1h`, `2d`). Transcript tail renders only for terminal jobs.
-- `src/commands/jobs.ts` — `gbrain jobs` CLI subcommands + `gbrain jobs work` daemon. Help is real and guarded: `JOBS_HELP` (full block incl. watch/stats/smoke flags + a footer naming exactly the five subcommands with dedicated help) and `JOBS_SUBCOMMAND_HELP` (work/supervisor/submit/watch/prune) print from a guard at the TOP of `runJobs`, BEFORE the thin-client refusal and the subcommand switch — `jobs work --help` can never start a daemon; only `--help`/`-h` are help tokens (bare `help` can be a job name); cli.ts routes it engine-free via `SELF_HELP_WITHOUT_ENGINE` + `CLI_ONLY_SELF_HELP`. `formatJobDetail` prints the effective wall-clock budget (`Timeout:`/`Deadline:` — 1x deadline kill when claimed, 2x wall-clock backstop, or which default applies) with a Date|string-defensive renderer; `timeout_at` is in `JOB_DATE_FIELDS` for thin-client rehydration. `jobs stats` prints a `Backpressure (24h)` line (per-name coalesce counts from the backpressure audit, current+previous ISO-week files, queue-filtered, best-effort) plus a suppressed-by hint naming the in-flight live-lock job when waiting=0 past the shared `GBRAIN_WEDGED_QUEUE_WARN_MINUTES` threshold — the read-only visibility for maxPending single-flight suppression. Pinned by `test/jobs-subcommand-help.serial.test.ts`, `test/jobs-format-detail.test.ts`, `test/jobs-stats-backpressure.serial.test.ts`. `case 'work'` wraps `worker.start()` in try/finally and owns engine lifecycle — calls `engine.disconnect()` on shutdown with loud error logging (the worker must not disconnect an engine it doesn't own; pool slots free immediately on shutdown rather than waiting for TCP keepalive). `jobs submit` surfaces the `MinionJobInput` retry/backoff/timeout/idempotency surface as flags: `--max-stalled`, `--backoff-type fixed|exponential`, `--backoff-delay`, `--backoff-jitter`, `--timeout-ms`, `--idempotency-key`, `--max-waiting` (`maxPending` is deliberately internal-only — no flag; see TODOS). `jobs smoke --sigkill-rescue` is the SIGKILL-rescue regression guard. `registerBuiltinHandlers` always registers `subagent` + `subagent_aggregator` (no env flag — `ANTHROPIC_API_KEY` is the cost gate, trust is via `PROTECTED_JOB_NAMES`) and loads `GBRAIN_PLUGIN_PATH` plugins at startup with a loud per-plugin line; `shell` handler still gated by `GBRAIN_ALLOW_SHELL_JOBS=1` (RCE surface). The `autopilot-cycle` handler forwards `job.data.phases` to `runCycle`, validated against `ALL_PHASES` from `src/core/cycle.ts` (invalid names filtered; empty/missing falls back to the default cycle); when `source_id` is set it binds `brainDir` to that source's `local_path` (null for a pure-DB source, never the global repo — the #2194/#2227 mixed-scope fix) and checks `isSourceInCooldown` before `runCycle`, returning a no-op `skipped` (not a failure) for a source still in its failure cooldown. The sibling `autopilot-global-maintenance` handler runs the brain-wide `GLOBAL_PHASES` once (no `sourceId`, `pull:false`) and stamps `autopilot.last_global_at` on success. `resolveJobPull` gives both cycle and standalone sync jobs one positive-polarity `pull` contract while preserving queued payloads that still carry the inverse legacy `noPull` key; explicit `pull` wins. The `sync` handler resolves `sourceId` at entry from `sources.local_path` (mirrors `cycle.ts:480`) so multi-source brains read the per-source `last_commit` anchor; concurrency routes through `autoConcurrency()` in `src/core/sync-concurrency.ts` (PGLite stays serial); `noEmbed` default is `true`. `gbrain jobs supervisor status` consumes `summarizeCrashes()` from `src/core/minions/handlers/supervisor-audit.ts` for parity with `gbrain doctor`: JSON adds `crashes_by_cause: {runtime_error, oom_or_external_kill, unknown, legacy}` + `clean_exits_24h`; human output gains per-cause + clean-exits lines. Pinned by `test/job-pull-policy.test.ts` and 4 source-grep wiring assertions in `test/doctor.test.ts` requiring `crashes_by_cause` + `clean_exits_24h=` in both `doctor.ts` and `jobs.ts`. `gbrain jobs watch` decouples its two output axes: `--json` picks FORMAT (human default, never gated on isTTY), `--follow` picks LOOP (default `isTTY && !json`). Non-TTY with no flags prints ONE human snapshot then exits (clean for subagent/pipe/cron); `--follow` opts into a continuous stream (human plain per tick, or JSONL with `--json`); a TTY with no flags keeps the live ANSI dashboard. Resolution is the pure `resolveWatchMode(opts, isTTY): {json, follow, useAnsiDashboard}` in `src/commands/jobs-watch.ts`; the dispatch wires `--follow`. Pinned by `test/jobs-watch-mode.test.ts` (format×loop matrix incl. the TTY+`--json`-one-shot case) + `test/e2e/non-tty-output.serial.test.ts` (the `cmd </dev/null` non-empty-stdout contract). registers 11 Minion handlers: `reindex`, `repair-jsonb`, `orphans`, `integrity`, `purge`, `synthesize` (PROTECTED), `patterns` (PROTECTED), `consolidate` (PROTECTED), `extract_facts`, `resolve_symbol_edges`, `recompute_emotional_weight`. Phase wrappers delegate to `runCycle({phases:[name]})` so `src/core/cycle.ts` stays the single source of truth for phase semantics. The standalone `sync` handler passes `noExtract: true` to match `runPhaseSync`'s contract (doctor's remediation plan emitting `[sync, extract]` would otherwise double-extract). The `extract` handler routes `{stale: true}` jobs (submitted by performSync's #2849 size-gate defer branch) through `extractStaleFromDB` scoped to `data.sourceId`, chaining a continuation job (no maxWaiting — same NULL-sourceId coalesce hazard; timeout derived from `STALE_TIME_BUDGET_MS`) when the sweep's budget leaves `staleRemaining > 0` with forward progress.
+- `src/commands/jobs.ts` — `gbrain jobs` CLI subcommands + `gbrain jobs work` daemon. Help is real and guarded: `JOBS_HELP` (full block incl. watch/stats/smoke flags + a footer naming exactly the five subcommands with dedicated help) and `JOBS_SUBCOMMAND_HELP` (work/supervisor/submit/watch/prune) print from a guard at the TOP of `runJobs`, BEFORE the thin-client refusal and the subcommand switch — `jobs work --help` can never start a daemon; only `--help`/`-h` are help tokens (bare `help` can be a job name); cli.ts routes it engine-free via `SELF_HELP_WITHOUT_ENGINE` + `CLI_ONLY_SELF_HELP`. `formatJobDetail` prints the effective wall-clock budget (`Timeout:`/`Deadline:` — 1x deadline kill when claimed, 2x wall-clock backstop, or which default applies) with a Date|string-defensive renderer; `timeout_at` is in `JOB_DATE_FIELDS` for thin-client rehydration. `jobs stats` prints a `Backpressure (24h)` line (per-name coalesce counts from the backpressure audit, current+previous ISO-week files, queue-filtered, best-effort) plus a suppressed-by hint naming the in-flight live-lock job when waiting=0 past the shared `GBRAIN_WEDGED_QUEUE_WARN_MINUTES` threshold — the read-only visibility for maxPending single-flight suppression. Pinned by `test/jobs-subcommand-help.serial.test.ts`, `test/jobs-format-detail.test.ts`, `test/jobs-stats-backpressure.serial.test.ts`. `case 'work'` wraps `worker.start()` in try/finally and owns engine lifecycle — calls `engine.disconnect()` on shutdown with loud error logging (the worker must not disconnect an engine it doesn't own; pool slots free immediately on shutdown rather than waiting for TCP keepalive). `jobs submit` surfaces the `MinionJobInput` retry/backoff/timeout/idempotency surface as flags: `--max-stalled`, `--backoff-type fixed|exponential`, `--backoff-delay`, `--backoff-jitter`, `--timeout-ms`, `--idempotency-key`, `--max-waiting` (`maxPending` is deliberately internal-only — no flag; see TODOS). `jobs smoke --sigkill-rescue` is the SIGKILL-rescue regression guard. `registerBuiltinHandlers` always registers `subagent` + `subagent_aggregator` (no env flag — `ANTHROPIC_API_KEY` is the cost gate, trust is via `PROTECTED_JOB_NAMES`) and loads `GBRAIN_PLUGIN_PATH` plugins at startup with a loud per-plugin line; `shell` handler still gated by `GBRAIN_ALLOW_SHELL_JOBS=1` (RCE surface). The `autopilot-cycle` handler forwards `job.data.phases` to `runCycle`, validated against `ALL_PHASES` from `src/core/cycle.ts` (invalid names filtered; empty/missing falls back to the default cycle); when `source_id` is set it binds `brainDir` to that source's `local_path` (null for a pure-DB source, never the global repo — the #2194/#2227 mixed-scope fix) and checks `isSourceInCooldown` before `runCycle`, returning a no-op `skipped` (not a failure) for a source still in its failure cooldown. The sibling `autopilot-global-maintenance` handler runs the brain-wide `GLOBAL_PHASES` once (no `sourceId`, `pull:false`) and stamps `autopilot.last_global_at` on success. `resolveJobPull` gives both cycle and standalone sync jobs one positive-polarity `pull` contract while preserving queued payloads that still carry the inverse legacy `noPull` key; explicit `pull` wins. The `sync` handler resolves `sourceId` at entry from `sources.local_path` (mirrors `cycle.ts:480`) so multi-source brains read the per-source `last_commit` anchor; concurrency routes through `autoConcurrency()` in `src/core/sync-concurrency.ts` (PGLite stays serial); `noEmbed` default is `true`. `gbrain jobs supervisor status` consumes `summarizeCrashes()` from `src/core/minions/handlers/supervisor-audit.ts` for parity with `gbrain doctor`: JSON adds `crashes_by_cause: {runtime_error, oom_or_external_kill, unknown, legacy}` + `clean_exits_24h`; human output gains per-cause + clean-exits lines. Pinned by `test/job-pull-policy.test.ts` and 4 source-grep wiring assertions in `test/doctor.test.ts` requiring `crashes_by_cause` + `clean_exits_24h=` in both `doctor.ts` and `jobs.ts`. `gbrain jobs watch` decouples its two output axes: `--json` picks FORMAT (human default, never gated on isTTY), `--follow` picks LOOP (default `isTTY && !json`). Non-TTY with no flags prints ONE human snapshot then exits (clean for subagent/pipe/cron); `--follow` opts into a continuous stream (human plain per tick, or JSONL with `--json`); a TTY with no flags keeps the live ANSI dashboard. Resolution is the pure `resolveWatchMode(opts, isTTY): {json, follow, useAnsiDashboard}` in `src/commands/jobs-watch.ts`; the dispatch wires `--follow`. Pinned by `test/jobs-watch-mode.test.ts` (format×loop matrix incl. the TTY+`--json`-one-shot case) + `test/e2e/non-tty-output.serial.test.ts` (the `cmd </dev/null` non-empty-stdout contract). registers 11 Minion handlers: `reindex`, `repair-jsonb`, `orphans`, `integrity`, `purge`, `synthesize` (PROTECTED), `patterns` (PROTECTED), `consolidate` (PROTECTED), `extract_facts`, `resolve_symbol_edges`, `recompute_emotional_weight`. Phase wrappers delegate to `runCycle({phases:[name]})` so `src/core/cycle.ts` stays the single source of truth for phase semantics. The standalone `sync` handler passes `noExtract: true` to match `runPhaseSync`'s contract (doctor's remediation plan emitting `[sync, extract]` would otherwise double-extract). The `extract` handler routes `{stale: true}` jobs (submitted by performSync's #2849 size-gate defer branch) through `extractStaleFromDB` scoped to `data.sourceId`, chaining a continuation job (no maxWaiting — same NULL-sourceId coalesce hazard; timeout derived from `STALE_TIME_BUDGET_MS`) when the sweep's budget leaves `staleRemaining > 0` with forward progress. `parseJobIsolationFlag(args, env?)` (`--job-isolation`, space/= forms, `GBRAIN_JOB_ISOLATION` fallback, default inline); `case 'work'` resolves + fail-fast validates the child CLI invocation and warns when `--max-rss` is combined with isolation (watchdog covers the worker only); `case 'run-child'` is the INTERNAL child entrypoint (quiet registerBuiltinHandlers incl. plugin discovery, CLI owns disconnect+exit); startup warning when a Supabase-shaped engine runs single-pool (kill-switch collapse made loud); the `db_dead` fatal text is verdict-aware (pool starved vs server unreachable).
- `src/commands/features.ts` — `gbrain features --json --auto-fix`: usage scan + feature adoption salesman.
- `src/commands/autopilot.ts` — `gbrain autopilot --install`: self-maintaining brain daemon (sync+extract+embed). Freshness sync jobs always send an explicit positive-polarity `pull` value derived from the source's parsed `remote_url`, so local-only sources skip pull and PGLite JSON-string configs behave like Postgres objects. Consumes `detectTini()` from `src/core/minions/spawn-helpers.ts`, resolved once at startup. Composes a `ChildWorkerSupervisor` instance for spawn-and-respawn (no inline `crashCount`/`startWorker`/`child.on('exit')`); `--max-rss 2048` and `maxCrashes: 5` preserved. `onMaxCrashesExceeded` routes through autopilot's own `shutdown('max_crashes')` so the autopilot lockfile gets cleaned up. `shutdown()` drains via `childSupervisor.killChild('SIGTERM')` + `awaitChildExit(35_000)`. Pinned by `test/autopilot-fanout-wiring.test.ts` and `test/autopilot-supervisor-wiring.test.ts` (6 static-shape guards: composes ChildWorkerSupervisor not legacy names, `--max-rss 2048` in argv, `maxCrashes: 5` literal, shutdown-via-callback, no workerProc reference). tick body invokes `runNightlyQualityProbe` when `cfg.autopilot.nightly_quality_probe.enabled === true` (default OFF — opt-in to protect API spend). NO scheduler-side rate-limit check — `runNightlyQualityProbe`'s internal `shouldRunNightly` (reading the audit JSONL) is the single source of truth. Probe call wrapped in try/catch that logs via `logError` and does NOT bump `consecutiveErrors` (probe failure is informational, never crashes the loop). Default `max_usd` cap = 5. Pinned by `test/autopilot-nightly-probe-wiring.test.ts`. per-source `extract_atoms` auto-drain. Postgres-only block after the freshness fan-out: gated on `autopilot.auto_drain.enabled` (default true) AND `!packDeclaresPhase(engine,'extract_atoms')` (the silent-backlog condition) AND per-source `countExtractAtomsBacklog > threshold` (default 25) AND a daily cap `floor(max_usd_per_day / ~$0.30)`. Enumerates `loadAllSources`. Submits the PROTECTED `extract-atoms-drain` job (`{allowProtectedSubmit:true}`) with a UTC-day time-sloted idempotency key `autopilot-extract-atoms-drain:<src.id>:<utcDay>` (a static key would block the source after the first job completed). `src/core/minions/protected-names.ts` adds `extract-atoms-drain`; `src/commands/jobs.ts` registers the handler (thin wrapper over `runExtractAtomsDrainForSource`, `LockUnavailableError` → `{deferred:true}`); `src/core/config.ts` adds the `autopilot.auto_drain.*` config keys + the `autopilot.` key prefix. Pinned by `test/extract-atoms-drain-handler.test.ts`, `test/autopilot-auto-drain-wiring.test.ts`. federated-brain co-existence + launchd hygiene. (1) `LOCK_PATH` resolves via `gbrainPath('autopilot.lock')` so it honors `GBRAIN_HOME` (two brains can run autopilot simultaneously without lock-stealing); lock file stores PID, startup checks `kill -0 <pid>` before refusing to start (stale lock from a crashed process no longer blocks). (2) exported `classifyReconnectError(err)` returns `'recoverable' | 'unrecoverable'`; unrecoverable causes `process.exit(0)` so launchd backs off instead of looping `config.database_url undefined`. (3) exported pure `generateLaunchdPlist(wrapperPath, home)` sets `ThrottleInterval=300` so launchd respects the exit-0 backoff. Pinned by `test/autopilot-lock-path.test.ts` + `test/autopilot-reconnect-classifier.test.ts`. targeted-submit loop instead of blanket `autopilot-cycle` dispatch. Each tick: cheap `engine.getHealth()` (single SQL count) + `computeRecommendations()`, then route by shape — `score >= 95 AND no plan AND <60min since last full` → sleep; `score >= 95 AND >=60min` → submit `autopilot-cycle` (60-min floor exercises phase-coupling invariants on healthy brains); `plan <= 3 steps AND est <5min` → submit individual handlers; `plan large OR score < 70` → submit full `autopilot-cycle`. The `gbrain-cycle` lock ensures targeted submissions and the full cycle can't run concurrently. `maxWaiting: 1` per submit closes the queue-fan-out vector.
- `src/mcp/server.ts` — MCP stdio server (generated from operations). Tool-call handler delegates to `dispatchToolCall` from `src/mcp/dispatch.ts` so stdio + HTTP transports share one validation, context-build, and error-format path. Stdin `'end'` / `'close'` shutdown hooks are skipped when `process.env.MCP_STDIO === '1'` — gateway-piped stdio MCP wrappers (OpenClaw's `bundle-mcp`) pipe the handshake then close their stdin half, which would otherwise kill the server before the first tool call; signal handlers (SIGTERM/SIGINT/SIGHUP) + the parent-process watchdog still cover legitimate disconnects. `src/commands/serve.ts` exposes `ServeOptions.mcpStdio?: boolean` as a test seam so the guard is exercisable without process.env mutation. Pinned by `test/serve-stdio-lifecycle.test.ts`.
@@ -306,13 +312,13 @@ per-release `**vX.Y.Z:**` narration — CI enforces this
- `src/core/cli-options.ts` — Global CLI flag parser. `parseGlobalFlags(argv)` returns `{cliOpts, rest}` with `--quiet` / `--progress-json` / `--progress-interval=<ms>` / `--brain <id>` stripped. `--brain` is the brain-axis (which database) selector: exact-match only (`--brain-*` per-command flags pass through), value validated against the mount-id regex at parse time, missing/malformed value THROWS — never a silent host fallback. `connectEngine` in `src/cli.ts` feeds it (plus the ambient `GBRAIN_BRAIN_ID` / `.gbrain-mount` / mount-path tiers) through `resolveBrainId` → `BrainRegistry.getBrain`, which throws `UnknownBrainError` for an unregistered id; mounts get no auto-migrations and keep the host-config AI gateway. `getCliOptions()` / `setCliOptions()` expose a module-level singleton so commands reach resolved flags without parameter threading. `cliOptsToProgressOptions()` maps to reporter options. `childGlobalFlags()` returns the flag suffix to append to `execSync('gbrain ...')` calls in migration orchestrators (propagates `--brain=<id>` so children stay on the parent's brain). `OperationContext.cliOpts` extends shared-op dispatch for MCP callers. `CliOptions` gains `explain: boolean`. `parseGlobalFlags` recognizes `--explain` anywhere in argv (stripped before command dispatch). `src/cli.ts` `formatResult` for `search` + `query` cases routes to `formatResultsExplain` from `src/core/search/explain-formatter.ts` when `CliOptions.explain` is set; falls through to the existing JSON / human formatters otherwise. `maybeBackground(opName, fingerprintArgs, runDirect)` helper. Same semantics in TTY and cron (no `--no-tty-detect` flag, no surprise behavior change between contexts): when `--background` is passed, submits the op as a Minion job via `op_checkpoints` for resumability and returns the `job_id`. `--background --follow` execs `gbrain jobs follow <id>` so the user sees the same stderr stream they'd get from a direct call. PGLite degrades to inline execution with a clear stderr note ("PGLite worker pool not yet supported; running inline"). Returns a tagged union the caller dispatches on.
- `src/cli.ts` strict flag validation (#2185) + `src/core/cli-flag-registry.generated.ts` + `scripts/generate-flag-registry.ts` — pre-dispatch, pre-engine unknown-flag rejection for every command: a flag no handler consults fails loud (`unknown flag --x for 'gbrain <cmd>'`, exit 1; `--json` invocations also get a structured `{status:'error', reason:'invalid_flag'}` on stdout) instead of being silently ignored while the un-asked-for real operation runs. `validateCommandFlags(command, subArgs)` runs after the `--help` short-circuit and before any dispatch or engine connect, in two lanes mirroring dispatch order (CLI_ONLY first — `think`/`salience`/`anomalies` are both ops AND CLI_ONLY members whose handlers parse flags the op contract doesn't declare): CLI_ONLY commands validate against the generated `CLI_FLAG_REGISTRY` (per-command legal sets derived from each command's source — case block + imported modules + one level of relative imports + `EXTRA_FLAGS`; deliberately over-inclusive, help-text mentions count; regenerate via `bun run build:flag-registry`); op commands validate via `findUnknownOpFlag`, which mirrors `parseOpArgs`'s traversal (non-boolean flags consume their value token; `--key=value` inline form recognized) plus the CLI-local flags consumed outside the op contract (`json`, `explain`, `help`, `source`, `dry-run`); the CLI_ONLY token scan is the exported `findUnknownFlag(args, legal)`. In `parseOpArgs`, `json`/`dry_run` are CLI-local booleans that never consume a value token, so a trailing `--dry-run` is a real rehearsal switch feeding `makeContext`'s `ctx.dryRun`. Uppercase flag spellings are treated as unknown (every handler is case-sensitive-lowercase, so they'd be silently ignored downstream — the exact class the validator kills). Exempt by contract: `call` (arbitrary `--param` interface), `config` (arbitrary set values), `jobs submit` (handler-defined payload params); everything after a literal `--` is passthrough and never validated. A command missing from the registry fails OPEN at runtime (never bricks a command); `test/cli-flag-validation.test.ts` pins registry freshness, per-command drift, and consumption evidence — a safety flag (`--dry-run`, `--yes`, `--force`) may only be advertised if the command's source actually reads it.
- `src/core/source-id.ts` — single canonical source_id validation, dependency-free by design (imported by both engines, cycle, source-resolver, sources-ops). `SOURCE_ID_RE` (strict: 1-32 lowercase alnum, interior hyphens only, no edge hyphens); `isValidSourceId` (boolean — for tiers that silently fall back: dotfile, brain_default) vs `assertValidSourceId` (throws — for tiers that must reject loudly: explicit `--source`, `GBRAIN_SOURCE`, `cycleLockIdFor`); `ALL_SOURCES = '__all__'` sentinel (#1712 — deliberately NOT a valid id so it can never collide with a real source or leak into lock ids/path joins; `sourceScopeOpts` translates it to an unscoped read for trusted local callers and keeps it unsatisfiable for remote callers, fail-closed). `normalizeSourceInput`/`normalizeFederatedReadInput` normalize the `/admin/api/register-client` HTTP body, mirroring the CLI's `--source`/`--federated-read` flags: omitted `source` → `'default'`; omitted `federatedRead` → `undefined` so `registerClientManual` applies its own `[sourceId]` default; present-but-invalid values throw so the route returns a structured 400 (`invalid_source`) instead of failing at INSERT time. Pinned by `test/register-client-source-normalize.test.ts`.
-- `src/core/db-lock.ts` — generic `tryAcquireDbLock(engine, lockId, ttlMinutes)` over the `gbrain_cycle_locks` table. Parameterized lock id so scopes nest cleanly: `gbrain-cycle` for the broad cycle (held by `cycle.ts`) and `gbrain-sync` (`SYNC_LOCK_ID`) for `performSync`'s narrower writer window. UPSERT-with-TTL semantics survive PgBouncer transaction pooling (unlike session-scoped `pg_try_advisory_lock`); crashed holders auto-release once their TTL expires. Every handle is FENCED to its exact acquisition: `DbLockHandle.acquiredAt` captures the row's `acquired_at` as epoch-seconds text (`extract(epoch from acquired_at)::text` — GUC-independent, unlike `timestamptz::text` which varies with per-session TimeZone/DateStyle across pools) and `refresh()`/`release()`/the cleanup hook all match `(id, holder_pid, fence)`, so a recycled PID or a superseded holder can never refresh or delete a successor's row. `refresh()` returns a boolean: `true` = still owned; `false` = the fenced UPDATE matched 0 rows (stolen or force-cleared — certain loss, the caller must stop relying on mutual exclusion); transient DB errors still THROW (not evidence of a steal; the TTL is the backstop). Exports `LockStolenError` (thrown/used as an AbortSignal reason by consumers like `cycle.ts`'s refresher and the supervisor). It also does automatic same-host dead-pid takeover: when the upsert finds a held, NOT-TTL-expired lock whose holder is on this host and provably dead, it reclaims via a guarded `DELETE WHERE id=$1 AND holder_pid=$2` + one normal-upsert retry returning the standard handle (refresh/release intact). The liveness check is the exported `classifyHolderLiveness(pid, host, ageMs, opts?)` / `isHolderDeadLocally(...)` (injectable `process.kill` seam; `HOLDER_TAKEOVER_GRACE_MS = 60_000` PID-reuse guard; EPERM classified as `alive` so a live process you don't own is never stolen). TTL-expired locks stay the upsert's job; cross-host stays TTL-only. `runBreakLock` (`src/commands/sync.ts`) consumes the same predicate. Background reaper (#1972): `reapDeadHolderLocks(engine)` is the periodic sweep the contention path lacked — it deletes locks whose holder is `isHolderDeadLocally`, scoped to the `gbrain-sync:*` / `gbrain-cycle`/`gbrain-cycle:*` namespaces ONLY (election/supervisor/reindex locks keep TTL-only behavior, untouched), via `deleteLockRowExact(engine, id, pid, acquiredAt)` — a snapshot-matched delete (`date_trunc('milliseconds', acquired_at) = $3`, so the ms a JS Date keeps survives) that's TOCTOU-safe against a reused PID taking the lock between SELECT and DELETE. `cycle.ts` runs it at cycle start (before the sync phase); `gbrain doctor --fix` runs it for no-autopilot brains. `selectLockRows(engine, opts?)` + a shared row→`LockSnapshot` mapper are the single canonical reader now backing `inspectLock` + `listStaleLocks` + the reaper (was triplicated). `isLockHolderLive(snap, ttlMinutes)` (#2227) is the observability liveness predicate — freshness-keyed (`ttl_expired` plus the heartbeat steal-grace), never `process.kill`, so `gbrain jobs supervisor status` / `gbrain doctor` can report a live supervisor via its queue lock without a PID-reuse false-positive. Pinned by `test/db-lock-auto-takeover.test.ts` + `test/db-lock-reap.test.ts` + `test/db-lock-fencing.test.ts`.
+- `src/core/db-lock.ts` — generic `tryAcquireDbLock(engine, lockId, ttlMinutes)` over the `gbrain_cycle_locks` table. Parameterized lock id so scopes nest cleanly: `gbrain-cycle` for the broad cycle (held by `cycle.ts`) and `gbrain-sync` (`SYNC_LOCK_ID`) for `performSync`'s narrower writer window. UPSERT-with-TTL semantics survive PgBouncer transaction pooling (unlike session-scoped `pg_try_advisory_lock`); crashed holders auto-release once their TTL expires. Every handle is FENCED to its exact acquisition: `DbLockHandle.acquiredAt` captures the row's `acquired_at` as epoch-seconds text (`extract(epoch from acquired_at)::text` — GUC-independent, unlike `timestamptz::text` which varies with per-session TimeZone/DateStyle across pools) and `refresh()`/`release()`/the cleanup hook all match `(id, holder_pid, fence)`, so a recycled PID or a superseded holder can never refresh or delete a successor's row. `refresh()` returns a boolean: `true` = still owned; `false` = the fenced UPDATE matched 0 rows (stolen or force-cleared — certain loss, the caller must stop relying on mutual exclusion); transient DB errors still THROW (not evidence of a steal; the TTL is the backstop). Exports `LockStolenError` (thrown/used as an AbortSignal reason by consumers like `cycle.ts`'s refresher and the supervisor). It also does automatic same-host dead-pid takeover: when the upsert finds a held, NOT-TTL-expired lock whose holder is on this host and provably dead, it reclaims via a guarded `DELETE WHERE id=$1 AND holder_pid=$2` + one normal-upsert retry returning the standard handle (refresh/release intact). The liveness check is the exported `classifyHolderLiveness(pid, host, ageMs, opts?)` / `isHolderDeadLocally(...)` (injectable `process.kill` seam; `HOLDER_TAKEOVER_GRACE_MS = 60_000` PID-reuse guard; EPERM classified as `alive` so a live process you don't own is never stolen). TTL-expired locks stay the upsert's job; cross-host stays TTL-only. `runBreakLock` (`src/commands/sync.ts`) consumes the same predicate. Background reaper (#1972): `reapDeadHolderLocks(engine)` is the periodic sweep the contention path lacked — it deletes locks whose holder is `isHolderDeadLocally`, scoped to the `gbrain-sync:*` / `gbrain-cycle`/`gbrain-cycle:*` namespaces ONLY (election/supervisor/reindex locks keep TTL-only behavior, untouched), via `deleteLockRowExact(engine, id, pid, acquiredAt)` — a snapshot-matched delete (`date_trunc('milliseconds', acquired_at) = $3`, so the ms a JS Date keeps survives) that's TOCTOU-safe against a reused PID taking the lock between SELECT and DELETE. `cycle.ts` runs it at cycle start (before the sync phase); `gbrain doctor --fix` runs it for no-autopilot brains. `selectLockRows(engine, opts?)` + a shared row→`LockSnapshot` mapper are the single canonical reader now backing `inspectLock` + `listStaleLocks` + the reaper (was triplicated). `isLockHolderLive(snap, ttlMinutes)` (#2227) is the observability liveness predicate — freshness-keyed (`ttl_expired` plus the heartbeat steal-grace), never `process.kill`, so `gbrain jobs supervisor status` / `gbrain doctor` can report a live supervisor via its queue lock without a PID-reuse false-positive. Pinned by `test/db-lock-auto-takeover.test.ts` + `test/db-lock-reap.test.ts` + `test/db-lock-fencing.test.ts`. `DbLockHandle.refresh(opts?)` accepts `{signal}` (Postgres forwards to executeRawDirect; PGLite ignores); `withRefreshingLock`'s heartbeat aborts the per-tick signal on timeout and guards re-entrancy (15s min cadence vs 30s timeout could stack ticks).
- `src/core/sync-concurrency.ts` — single source of truth for the parallel-sync policy. Exports `autoConcurrency(engine, fileCount, override?)` (PGLite always serial; explicit override clamped to >=1; auto path returns `DEFAULT_PARALLEL_WORKERS=4` when `fileCount > AUTO_CONCURRENCY_FILE_THRESHOLD=100`), `shouldRunParallel(workers, fileCount, explicit)` (explicit `--workers` bypasses the >50-file floor), and `parseWorkers(s)` (rejects `'0'`, `'-3'`, `'foo'`, `'1.5'`, trailing chars). Used by `performSync`, `performFullSync`, `runImport`, and the Minion `sync` handler so the sites can't drift. `DEFAULT_PARALLEL_SOURCES = 4` is a SEPARATE constant for the per-source fan-out under `gbrain sync --all` — kept distinct from `DEFAULT_PARALLEL_WORKERS` because total live Postgres connections per wave ≈ `DEFAULT_PARALLEL_SOURCES × DEFAULT_PARALLEL_WORKERS × 2 (per-file pool)` = 32 at both defaults (each per-file worker opens its own `PostgresEngine` with `poolSize = min(2, resolvePoolSize(2))`); `sync.ts` warns when `parallel × workers × 2 > 16`. `resolveWorkersWithClamp(engine, override, commandName, fileCount)` wraps `autoConcurrency` with a per-command stderr clamp warning on PGLite (per-(command, requested) dedup via module-scoped warned-once set with `_resetWorkersClampWarningsForTest()` seam) and is the canonical surface for every bulk-command `--workers N` flag (extract-conversation-facts, extract, edges-backfill, reindex-multimodal, reindex, reindex-code); embed.ts deliberately bypasses it and keeps `GBRAIN_EMBED_CONCURRENCY || 20`. `resolveMaxConnections()` (reads `GBRAIN_MAX_CONNECTIONS`, undefined when unset) + `clampWorkersForConnectionBudget(workers, perWorkerPool, maxConnections, parentPool)` back the opt-in single-sync connection-footprint clamp so a big sync stays under a low pooler cap (`parent_pool + workers×perWorkerPool ≤ budget`); `gbrain doctor`'s `pool_budget` check (`computePoolBudgetCheck` / `checkPoolBudget` in `src/commands/doctor.ts`) warns when the budget leaves no room for a worker, pointing at `GBRAIN_POOL_SIZE=2`. Pinned by `test/pglite-workers-clamp.test.ts`.
- `src/core/worker-pool.ts` — Canonical sliding-pool + bounded-semaphore primitive (extracted from `src/commands/embed.ts` sliding-pool sites and `src/commands/eval-cross-modal.ts` `runWithLimit` semaphore). Two exports: `runSlidingPool<T>({items, workers, onItem, signal?, onError?, failureLabel?, onProgress?})` + `runWithLimit<TIn, TOut>({items, limit, fn, signal?})`. Atomicity invariant: `const idx = nextIdx++` is one synchronous JS statement (no `await` between read and write — guaranteed by the single-threaded event loop), documented in the module header AND enforced by `scripts/check-worker-pool-atomicity.sh` (wired into `bun run verify`), which rejects importing `worker_threads` in any consuming file and inserting `await` between the `nextIdx` read and write. `MUST_ABORT_ERROR_TAGS` set is seeded with `BUDGET_EXHAUSTED` from `src/core/budget/budget-tracker.ts`; tagged errors (matched via `err.tag === 'BUDGET_EXHAUSTED'` to avoid cross-module import) bypass `onError` and hard-abort the pool via `AbortController.abort()` to in-flight `onItem` — the budget cap is a structural ceiling under concurrency. `failures[]` shape is `{idx, label, error}` records (NOT full items; callers supply `failureLabel(item) => string`) for bounded memory under huge brains. Pinned by `test/worker-pool.test.ts` + `test/scripts/check-worker-pool-atomicity.test.ts`. Drives every `--workers N` bulk command.
- `src/core/embedding-dim-check.ts` — facts.embedding dim drift surface. `readFactsEmbeddingDim(engine): Promise<FactsColumnDimResult>` covers both `vector(N)` and `halfvec(N)` shapes (migration v40 falls back to `vector` on pgvector < 0.7); regex ordering is halfvec-before-vector (substring "vec" appears in "halfvec"; naive `/vector/i` would shadow). `buildFactsAlterRecipe(dims, configured, type)` emits the paste-ready `DROP INDEX IF EXISTS idx_facts_embedding_hnsw; ALTER TABLE facts ALTER COLUMN embedding TYPE halfvec(N) USING embedding::halfvec(N); CREATE INDEX idx_facts_embedding_hnsw ON facts USING hnsw (embedding halfvec_cosine_ops) WHERE ...` flow (NOT bare REINDEX, which doesn't rewrite the index after a column-type change). `assertFactsEmbeddingDimMatchesConfig(engine)` is the preflight — throws `FactsEmbeddingDimMismatchError` (tagged `tag: 'FACTS_EMBEDDING_DIM_MISMATCH'` for parity with the worker-pool MUST_ABORT semantics) when configured dim ≠ column width; cached per-engine via `WeakMap`; PGLite engines silently skip. Doctor check `facts_embedding_width_consistency` (registered after `embedding_width_consistency`) reuses the same helpers with an identical ALTER recipe. Pinned by `test/embedding-dim-check-facts.test.ts`.
- `src/core/sort-newest-first.ts` — single source of truth for the descending-lex sort that `gbrain import` and `gbrain sync` both apply. Mutates in place (Array.prototype.sort semantics), returns the same array reference for fluent chaining. Empty/single-element inputs short-circuit. Future ordering changes flip one line here instead of touching two CLI commands. Pinned by `test/sort-newest-first.test.ts` (descending order, mixed prefixes, empty, single-element, in-place-mutation contract).
- `src/core/cycle.ts` — brain maintenance cycle primitive (23 phases; `ALL_PHASES` is the ordered source of truth). `runCycle(engine: BrainEngine | null, opts: CycleOpts): Promise<CycleReport>` composes phases in semantic order along the core spine lint → backlinks → sync → synthesize → extract → patterns → recompute_emotional_weight → embed → orphans, with the extraction/graph/consolidation phases (extract_facts, extract_atoms, resolve_symbol_edges, …) slotted between per the ordering comments on `ALL_PHASES`. `synthesize` runs after sync (cross-references see a fresh brain) and before extract (auto-link materializes its writes); `patterns` runs after extract so it reads a fresh graph (subagent put_page sets `ctx.remote=true` and skips auto-link/timeline by default, so extract is the canonical materialization); `recompute_emotional_weight` sees the union of `syncPagesAffected` + `synthesizeWrittenSlugs` incrementally, or all pages when neither anchor is set (full backfill via `gbrain dream --phase recompute_emotional_weight`). `CycleReport.schema_version: "1"` is stable; `totals` is additive (`pages_emotional_weight_recomputed`, `transcripts_processed`, `synth_pages_written`, `patterns_written`). Three callers: `gbrain dream` CLI, `gbrain autopilot` daemon inline path, the Minions `autopilot-cycle` handler. Coordination via `gbrain_cycle_locks` DB table + `~/.gbrain/cycle.lock` file lock with PID-liveness for PGLite; the two handles compose into ONE lock whose `refresh()` treats the fenced DB row as the authoritative multi-writer identity and the file half as best-effort freshness — the file lock is refreshed ONLY while the fenced DB refresh reports ownership, so a losing holder can never clobber the successor's file lock on the very tick it detects the steal. A dedicated serialized refresher — `startCycleLockRefresher(lock, controller, lockId)` (exported; unref'd `setInterval` at `max(15s, TTL/6)`, env-only override `GBRAIN_CYCLE_LOCK_REFRESH_MS`, in-flight guard so a slow refresh never overlaps the next tick) — heartbeats the lock through long phases (synthesis/patterns/consolidation waits routinely outlive the TTL with no other heartbeat). A fenced miss aborts the controller with `LockStolenError`; that steal signal combines with the worker's external signal via the exported `anyAbortSignal(signals)` (duck-type-tolerant — stubs without `addEventListener` are observed by poll; returns `{signal, dispose}` and dispose detaches the caller-signal listener + clears the poll timer so daemons don't leak), and the run stops at the next phase boundary with a structured partial report carrying `reason: 'lock_stolen'` (for the 5 long phases — synthesize / extract_atoms / patterns / synthesize_concepts / consolidate — the steal races the phase promise and stops the WAIT immediately; their in-flight work runs to its own bounded timeout because their opts can't carry a signal yet). Transient refresh errors log and retry next tick (the TTL stays the backstop). `yieldBetweenPhases` runs between phases; `yieldDuringPhase` is in-phase keepalive. Engine nullable; lock-skip on read-only phase selections. `CycleOpts.signal?: AbortSignal` propagates the worker's abort signal with `checkAborted()` between every phase. Pinned by `test/cycle-lock-steal.serial.test.ts` (mid-run steal → partial report, no further phases, successor row intact) + `test/cycle-any-abort-signal.test.ts` + `test/db-lock-fencing.test.ts`. `runPhaseSync` returns `pagesAffected` via `SyncPhaseResult` (threaded to `runPhaseExtract` as the 4th arg) and takes `willRunExtractPhase: boolean` setting `noExtract: phases.includes('extract')` so `gbrain dream --phase sync` doesn't silently lose extraction. `resolveSourceForDir(engine, brainDir)` threads `sourceId` to `performSync()` so sync reads the per-source `sources.last_commit` anchor (not the drift-prone global `config.sync.last_commit`). `CycleOpts.brainDir` is `string | null`; when null (checkout-less postgres/Supabase brain) the 6 filesystem phases (lint/backlinks/sync/synthesize/extract/patterns) skip with `details.reason: 'no_brain_dir'` and the DB-only phases run; `resolveSourceForDir` is null-tolerant. `cycleSourceId = opts.sourceId ?? resolveSourceForDir(engine, brainDir)` is the canonical per-source scope for `extract_facts`/`extract_atoms`/calibration — and for `synthesize` (#1586: threaded as `SynthesizePhaseOpts.sourceId` so synthesized pages land in the cycle's resolved source, not `'default'`) — so `gbrain dream --source repo-a` reconciles repo-a's facts even with no checkout (instead of scoping to `'default'` while stamping repo-a fresh). `deriveStatus` counts `edges_resolved`/`edges_ambiguous` as work so an edges-only cycle reports `ok` not `clean`; the `jobs.ts` `autopilot-cycle` + phase-wrapper handlers pass `null` (not `'.'`) when no repo is configured. The cycle is SPLIT for autopilot fan-out (#2194/#2227): `PHASE_SCOPE` partitions `ALL_PHASES` into `GLOBAL_PHASES` (brain-wide: embed/orphans/purge/resolve_symbol_edges/grade_takes/calibration_profile/synthesize_concepts/skillopt) and `NON_GLOBAL_PHASES` (source + mixed). Per-source `autopilot-cycle` jobs run only `NON_GLOBAL_PHASES` and stamp `last_source_cycle_at`; the single `autopilot-global-maintenance` job runs `GLOBAL_PHASES` (no `sourceId`) and stamps the brain-level `autopilot.last_global_at` config key (`LAST_GLOBAL_AT_KEY`). `last_full_cycle_at` is still written alongside `last_source_cycle_at` on a per-source success for doctor/legacy readers (no longer a gate for the brain-wide phases). Pinned by `test/dream-postgres.serial.test.ts` + `test/jobs-autopilot-cycle-braindir.serial.test.ts` + `test/autopilot-global-maintenance.test.ts`. `runPhaseLint` + `runPhaseBacklinks` carry the `export` keyword so behavioral tests can drive them directly (internal helpers exposed for test-only consumption; downstream code should NOT depend on them). Pinned by `test/cycle-legacy-phases.test.ts` (11 cases across both phases: clean run → status='ok', partial fix → status='warn' with `dryRun` in details, dry-run path doesn't write, throw-from-lib → status='fail' with the wrapper's try/catch envelope populated). Future phase wrappers (sync, extract, embed, orphans, extract_facts, resolve_symbol_edges, recompute_emotional_weight) land as additional describes in the same file. with `src/core/cycle/extract-atoms.ts` + `src/core/cycle/synthesize-concepts.ts` + `src/commands/extract.ts` + `src/commands/doctor.ts` + `src/core/op-checkpoint.ts`: six daily-driver ops fixes. (1) Batch idempotency: `atomsExistingForHashes(engine, sourceId, hashes[])` (exported from `src/core/cycle/extract-atoms.ts`) replaces the per-hash loop (7K individual queries) with one batched SQL roundtrip returning already-extracted `content_hash16` values; fail-open (SQL error → empty set, extraction proceeds); powered by migration v104 `pages_atom_source_hash_idx` (partial expression index on `frontmatter->>'source_hash'` for atom rows where `deleted_at IS NULL`; Postgres `CREATE INDEX CONCURRENTLY` with invalid-remnant pre-drop, PGLite plain). (2) Cycle lock TTL + heartbeat: `LOCK_TTL_MINUTES = 5`; `buildYieldDuringPhase(lock, outer)` (exported, with `LockHandle`) calls `lock.refresh()` + any external hook on every fire, throttled to 30s via `maybeYield`, firing both in the main loop AND immediately after every `await chat(...)`; `synthesize_concepts` uses the same throttled hook. A crashed cycle releases its lock within one short TTL, while the dedicated `startCycleLockRefresher` interval (see the coordination sentence above) keeps a healthy long-running cycle alive even across a single multi-minute `await chat(...)` — the timer fires during awaits, so no single slow call can silently expire the lock. (3) Progress wiring: `progress?: ProgressReporter` opt on `ExtractAtomsOpts` and `SynthesizeConceptsOpts`; cycle.ts passes its phase-level reporter down (NOT a child reporter, which would collide on `cycle.extract_atoms.extract_atoms.work`); phases only call `tick()`/`heartbeat()`, cycle.ts owns `start()`/`finish()`. (4) `by-mention` resume: `mentionsFingerprint({source, type, since, gazetteerHash})` in `src/core/op-checkpoint.ts` — the gazetteer hash is load-bearing (adding entity pages mid-pause shifts the hash → new fingerprint → fresh scan against the new gazetteer, never silent skip); `gbrain extract links --by-mention` resumes via `op_checkpoints` with `flushAndCheckpoint` ordering (links flush to DB FIRST, page keys commit to checkpoint SECOND, persist THIRD, so a crash mid-batch leaves the page un-checkpointed and resume re-scans it); persist every 1000 items OR 30s; clean exit clears the checkpoint; `--dry-run` skips both load and write. (5) `sync_consolidation` doctor check (multi-source brains see a paste-ready `gbrain sync --all --parallel 4 --workers 4 --skip-failed`; single-source "not applicable"; SQL errors return `warn` via the check's own try/catch). (6) Test-isolation: `test/cycle-last-full-cycle-at.test.ts` + `test/schema-cli.test.ts` use per-test `GBRAIN_HOME=tempdir`. Pinned by `test/cycle/extract-atoms-batch.test.ts`, `test/cycle/cycle-lock-ttl.test.ts` (regression pin on `LOCK_TTL_MINUTES === 5`), `test/op-checkpoint-mentions-fingerprint.test.ts`, `test/cycle/extract-atoms-progress.test.ts`, `test/cycle/synthesize-concepts-progress.test.ts`, `test/cycle/yield-during-phase-refresh.test.ts`, `test/cycle/yield-during-phase-throttle.test.ts`, `test/extract-by-mention-resume.test.ts`, `test/doctor-sync-consolidation.test.ts`. Companion `sync --all` recipe block in `skills/cron-scheduler/SKILL.md`. `synthesize_concepts` writes concept pages through `importFromContent` (#2163: the same parse→chunk→embed pipeline put_page uses, with put_page's `isAvailable('embedding')` → `noEmbed` gate) so `concepts/` pages carry `content_chunks` + embeddings and are reachable by retrieval (where `source-boost.ts` weights them 1.3×). `purge` phase (soft-delete TTLs) also GCs stale `op_checkpoints` rows older than 7 days. Non-fatal on pre-v67 brains (DROP-target-table check before DELETE). #1737: the cycle threads its abort signal into the embed phase (`runPhaseEmbed(engine, dryRun, signal)`) so a timed-out cycle's long embed phase honors cancellation and releases `gbrain_cycle_locks` right away instead of after a full backlog run.
-- `src/core/cycle/synthesize.ts` — Synthesize phase: conversation-transcript-to-brain pipeline. Reads `dream.synthesize.session_corpus_dir`, runs a cheap Haiku verdict (cached in `dream_verdicts`), then fans out one Sonnet subagent per worth-processing transcript with `allowed_slug_prefixes` (sourced from `skills/_brain-filing-rules.json` `dream_synthesize_paths.globs`; when `dream.synthesize.output_root` is set, `loadAllowedSlugPrefixes(outputRoot)` remaps the `wiki/`-rooted globs to the configured namespace — #2415 — and the same root drives the prompt slug templates; default 'wiki', validated against the slug grammar via the exported `loadOutputRoot`). The phase is source-scoped (#1586): cycle.ts threads `cycleSourceId` as `opts.sourceId` → each child's `SubagentHandlerData.source_id` → the subagent tool registry's `OperationContext.sourceId`, so put_page writes, collected refs, the summary page, and reverse-writes all target the cycle's resolved source ('default' when unscoped; reverse-writes for the cycle's own source land at `brainDir/<slug>.md`, foreign sources under `brainDir/.sources/<id>/`). Orchestrator collects slugs from `subagent_tool_executions` (NOT `pages.updated_at`) and reverse-renders DB → markdown via `serializeMarkdown`. Cooldown via `dream.synthesize.last_completion_ts`, written ONLY on success. Idempotency key `dream:synth:<file_path>:<content_hash>`. `--dry-run` runs Haiku, skips Sonnet. Subagent never gets fs-write access. `renderPageToMarkdown` (exported) stamps `dream_generated: true` + `dream_cycle_date` into every reverse-write's frontmatter; `writeSummaryPage` does the same on the summary index — this marker is the explicit identity surface `isDreamOutput` checks in `transcript-discovery.ts`. `stampDreamProvenance` (#2569) additionally persists the same marker into the `pages.frontmatter` JSONB row (merge via `executeRawJsonb`, raw object bound to `$N::jsonb`) for every child-written page BEFORE reverse-rendering, so generated pages are DB-queryable and a later put_page write-through (which re-renders from the DB row) can't erase the stamp. `judgeSignificance` and `JudgeClient` are exported; `judgeSignificance` takes a `verdictModel` param loaded from `dream.synthesize.verdict_model` via `loadSynthConfig`. `splitTranscriptByBudget(content, contentHash, maxChars)` splits oversized transcripts at paragraph boundaries (`## Topic:` → `---` → `\n` ladder) using a deterministic offset seeded from the first 32 bits of `contentHash` so retries chunk identically; per-chunk char budget = `MODEL_CONTEXT_TOKENS[resolvedModel] × 0.9 × 3.5 chars/token` (non-Anthropic ids fall back to a 180K-token safe default + once-per-process stderr warn); operator overrides `dream.synthesize.max_prompt_tokens` (floor 100K, wins) and `dream.synthesize.max_chunks_per_transcript` (default 24); per-chunk subagent job/wait timeouts are `dream.synthesize.subagent_timeout_ms` / `dream.synthesize.subagent_wait_timeout_ms` (defaults 30/35 min). Per-chunk idempotency keys `dream:synth:<filePath>:<hash16>:c<i>of<n>`; single-chunk transcripts preserve the legacy `dream:synth:<filePath>:<hash16>` key byte-for-byte so existing brains skip with `already_synthesized_legacy_single_chunk` instead of re-spending Sonnet. `collectChildPutPageSlugs` raw-fetches every (job_id, slug) pair (not `SELECT DISTINCT`) and rewrites bare-hash6 slugs to `<hash6>-c<idx>` for chunked children (orchestrator-side, zero Sonnet trust). Cap-hit skips don't write to `dream_verdicts` so raising the cap on next run re-attempts cleanly. Bounds INITIAL prompt size only; tool-loop turn-N accumulation is caught by terminal-error classification in `subagent.ts`. Verdict routing is gateway-routed: `makeJudgeClient(verdictModel)` (exported, replacing `makeHaikuClient()`) mirrors `tryBuildGatewayClient` in `src/core/think/index.ts` — a construction-time provider/key probe returns `null` on a clear miss (unknown provider id via `resolveRecipe` AIConfigError, or Anthropic provider with no key via `hasAnthropicKey()`). The verdict loop wraps `judgeSignificance` in try/catch for `AIConfigError` so mid-run provider failures surface as per-transcript `worth=false, reasons=['gateway error: ...']` instead of crashing the phase. Canonical config key `models.dream.synthesize_verdict` (per `PER_TASK_KEYS` in `src/core/model-config.ts`); `JudgeClient` signature preserved verbatim for test-seam stability; CI guard `scripts/check-gateway-routed-no-direct-anthropic.sh` prevents reintroducing `new Anthropic()` here or in `think/index.ts`. At the queue.add boundary (lines 395-404) a conditional `anthropic:` prefix is applied ONLY when the resolved model has no colon AND starts with `claude-` (because `resolveModel` returns bare ids from `TIER_DEFAULTS`/`DEFAULT_ALIASES` and the subagent validator requires `provider:model` form) — avoids changing the shared constants which would ripple across every `resolveModel` caller. Pinned by `test/cycle/synthesize-gateway-adapter.test.ts`, `test/e2e/dream-synthesize-pglite.test.ts` (gateway-adapter mid-run AIConfigError catch), `test/cycle/regression-pr-wave-r1-r2-r4.test.ts`.
+- `src/core/cycle/synthesize.ts` — Synthesize phase: conversation-transcript-to-brain pipeline. Reads `dream.synthesize.session_corpus_dir`, runs a cheap Haiku verdict (cached in `dream_verdicts`), then fans out one Sonnet subagent per worth-processing transcript with `allowed_slug_prefixes` (sourced from `skills/_brain-filing-rules.json` `dream_synthesize_paths.globs`; when `dream.synthesize.output_root` is set, `loadAllowedSlugPrefixes(outputRoot)` remaps the `wiki/`-rooted globs to the configured namespace — #2415 — and the same root drives the prompt slug templates; default 'wiki', validated against the slug grammar via the exported `loadOutputRoot`). The phase is source-scoped (#1586): cycle.ts threads `cycleSourceId` as `opts.sourceId` → each child's `SubagentHandlerData.source_id` → the subagent tool registry's `OperationContext.sourceId`, so put_page writes, collected refs, the summary page, and reverse-writes all target the cycle's resolved source ('default' when unscoped; reverse-writes for the cycle's own source land at `brainDir/<slug>.md`, foreign sources under `brainDir/.sources/<id>/`). Orchestrator collects slugs from `subagent_tool_executions` (NOT `pages.updated_at`) and reverse-renders DB → markdown via `serializeMarkdown`. Cooldown via `dream.synthesize.last_completion_ts`, written ONLY on success. Idempotency key `dream:synth:<file_path>:<content_hash>`. `--dry-run` runs Haiku, skips Sonnet. Subagent never gets fs-write access. `renderPageToMarkdown` (exported) stamps `dream_generated: true` + `dream_cycle_date` into every reverse-write's frontmatter; `writeSummaryPage` does the same on the summary index — this marker is the explicit identity surface `isDreamOutput` checks in `transcript-discovery.ts`. `stampDreamProvenance` (#2569) additionally persists the same marker into the `pages.frontmatter` JSONB row (merge via `executeRawJsonb`, raw object bound to `$N::jsonb`) for every child-written page BEFORE reverse-rendering, so generated pages are DB-queryable and a later put_page write-through (which re-renders from the DB row) can't erase the stamp. `judgeSignificance` and `JudgeClient` are exported; `judgeSignificance` takes a `verdictModel` param loaded from `dream.synthesize.verdict_model` via `loadSynthConfig`. `splitTranscriptByBudget(content, contentHash, maxChars)` splits oversized transcripts at paragraph boundaries (`## Topic:` → `---` → `\n` ladder) using a deterministic offset seeded from the first 32 bits of `contentHash` so retries chunk identically; per-chunk char budget = `MODEL_CONTEXT_TOKENS[resolvedModel] × 0.9 × 3.5 chars/token` (non-Anthropic ids fall back to a 180K-token safe default + once-per-process stderr warn); operator overrides `dream.synthesize.max_prompt_tokens` (floor 100K, wins) and `dream.synthesize.max_chunks_per_transcript` (default 24); per-chunk subagent job/wait timeouts are `dream.synthesize.subagent_timeout_ms` / `dream.synthesize.subagent_wait_timeout_ms` (defaults 30/35 min). Per-chunk idempotency keys `dream:synth:<filePath>:<hash16>:c<i>of<n>`; single-chunk transcripts preserve the legacy `dream:synth:<filePath>:<hash16>` key byte-for-byte so existing brains skip with `already_synthesized_legacy_single_chunk` instead of re-spending Sonnet. `collectChildPutPageSlugs` raw-fetches every (job_id, slug) pair (not `SELECT DISTINCT`) and rewrites bare-hash6 slugs to `<hash6>-c<idx>` for chunked children (orchestrator-side, zero Sonnet trust). Cap-hit skips don't write to `dream_verdicts` so raising the cap on next run re-attempts cleanly. Bounds INITIAL prompt size only; tool-loop turn-N accumulation is caught by terminal-error classification in `subagent.ts`. Verdict routing is gateway-routed: `makeJudgeClient(verdictModel)` (exported, replacing `makeHaikuClient()`) mirrors `tryBuildGatewayClient` in `src/core/think/index.ts` — a construction-time provider/key probe returns `null` on a clear miss (unknown provider id via `resolveRecipe` AIConfigError, or Anthropic provider with no key via `hasAnthropicKey()`). The verdict loop wraps `judgeSignificance` in try/catch for `AIConfigError` so mid-run provider failures surface as per-transcript `worth=false, reasons=['gateway error: ...']` instead of crashing the phase. Canonical config key `models.dream.synthesize_verdict` (per `PER_TASK_KEYS` in `src/core/model-config.ts`); `JudgeClient` signature preserved verbatim for test-seam stability; CI guard `scripts/check-gateway-routed-no-direct-anthropic.sh` prevents reintroducing `new Anthropic()` here or in `think/index.ts`. At the queue.add boundary (lines 395-404) a conditional `anthropic:` prefix is applied ONLY when the resolved model has no colon AND starts with `claude-` (because `resolveModel` returns bare ids from `TIER_DEFAULTS`/`DEFAULT_ALIASES` and the subagent validator requires `provider:model` form) — avoids changing the shared constants which would ripple across every `resolveModel` caller. Pinned by `test/cycle/synthesize-gateway-adapter.test.ts`, `test/e2e/dream-synthesize-pglite.test.ts` (gateway-adapter mid-run AIConfigError catch), `test/cycle/regression-pr-wave-r1-r2-r4.test.ts`. Drain-loop lock renewal runs through exported `runDrainRenewalTick` (per-call AbortSignal + timeout + re-entrancy guard) — a hung renewLock no longer stacks one checked-out slot per interval firing. Pinned by `test/cycle-drain-renewal.test.ts`.
- `scripts/check-gateway-routed-no-direct-anthropic.sh` — CI guard that fails the build if `src/core/cycle/synthesize.ts` or `src/core/think/index.ts` reintroduces a runtime `new Anthropic()` constructor call or a value-shaped `import Anthropic from '@anthropic-ai/sdk'` import. Type-only imports (`import type Anthropic from '@anthropic-ai/sdk'`) stay allowed for adapter types; comment lines (`//` or ` *` prefixes) are excluded so historical JSDoc doesn't false-fire. Mirrors `scripts/check-jsonb-pattern.sh`. Wired into `bun run verify`. Extend `GUARDED_FILES` when migrating another file off direct SDK construction.
- `src/core/cycle/patterns.ts` — Patterns phase: cross-session theme detection over reflections within `dream.patterns.lookback_days` (default 30). Names a pattern only when ≥`dream.patterns.min_evidence` (default 3) reflections support it. Single Sonnet subagent; same allow-list path as synthesize (imports `loadAllowedSlugPrefixes` + `loadOutputRoot` from synthesize.ts — #2415: the reflections lookup, prompt slug templates, and allow-list all honor `dream.synthesize.output_root`, default 'wiki'). Subagent job/wait timeouts are config keys `dream.patterns.subagent_timeout_ms` / `dream.patterns.subagent_wait_timeout_ms` (defaults 30/35 min, mirroring the `dream.synthesize.*` pair). The phase status reflects the child outcome: non-`completed` outcome with zero writes → `fail` (error code `PATTERNS_CHILD_<OUTCOME>`); non-`completed` with partial writes → `warn`. Runs AFTER `extract` so the graph is fresh.
- `src/core/cycle/extract-facts.ts` — extract_facts cycle phase. Fence is canonical: per-page wipe (`deleteFactsForPage`) + reinsert from `parseFactsFence` + `extractFactsFromFenceText` + `engine.insertFacts`. #1928: the per-page wipe passes `excludeSourcePrefixes: ['cli:']` so conversation facts (written by `extract-conversation-facts`, on pages with NO `## Facts` fence to recreate them from) survive the reconcile instead of being deleted-with-nothing-to-reinsert. The destructive phase no longer inherits a failed sync's full-brain walk: `slugs: []` (a real incremental no-op) is distinguished from `slugs: undefined` (full-walk intent) by presence, not length. `runPhaseExtractFacts` (cycle.ts) surfaces a `warn` (`net_fact_deletion`) when the reconcile deletes at least `NET_DELETION_WARN_FLOOR` (50) more facts than it reinserts — the exact signature of the conversation-facts wipe, which previously read as a silent `ok`. Empty-fence guard refuses when legacy rows (`row_num IS NULL AND entity_slug IS NOT NULL`) pend backfill (status: warn, hint: `gbrain apply-migrations --yes`). A phantom-redirect pre-pass runs AFTER the legacy-row guard, BEFORE the main reconcile loop: when `opts.brainDir` is set, `runPhantomRedirectPass(engine, brainDir, sourceId, dryRun)` walks unprefixed-slug pages capped by `GBRAIN_PHANTOM_REDIRECT_LIMIT` (default 50). The pass returns `touched_canonicals` — canonical slugs whose disk fence merged with phantom rows; `runExtractFacts` UNIONs them into the main reconcile slug set so canonical's DB facts derive from the merged fence in the same cycle (handles phantom-had-only-on-disk-fence). `ExtractFactsResult` gains six phantom fields: `phantomsScanned`, `phantomsRedirected`, `phantomsAmbiguous`, `phantomsSkippedDrift`, `phantomsLockBusy`, `phantomsMorePending`. Three bubble to `CycleReport.totals` (`phantoms_redirected`, `phantoms_ambiguous`, `phantoms_skipped_drift`).
diff --git a/docs/guides/minions-deployment.md b/docs/guides/minions-deployment.md
index a12b335a53..d9781b5160 100644
--- a/docs/guides/minions-deployment.md
+++ b/docs/guides/minions-deployment.md
@@ -87,6 +87,69 @@ check warns if what you asked for isn't what's actually running (e.g. a
negative value denied without privilege, or an OS `RLIMIT_NICE` clamp). This
is distinct from the concurrency / inflight cap and composes with it.
+### Per-job process isolation (`--job-isolation process`)
+
+By default all concurrency slots execute inside one worker process. A
+handler that ignores its abort signal can only be force-evicted — the
+promise is abandoned, still running, still holding connections and memory —
+and any worker exit destroys every in-flight job at once. With isolation on,
+each claimed job runs in its own child process: a stuck handler is
+group-SIGKILLed for real (group signaling under Bun falls back to POSIX
+`/bin/kill`; if that's unavailable the worker logs that isolation is
+degraded), a crash or OOM in a child takes that one job instead of all N,
+and the OS reclaims every leaked resource when the child dies:
+
+```bash
+# Recommended for long-running LLM-bound handlers (subagent):
+gbrain jobs supervisor --concurrency 4 --job-isolation process
+
+# Bare worker, or durably via env:
+GBRAIN_JOB_ISOLATION=process gbrain jobs work --concurrency 4
+```
+
+How it works: the worker keeps claim, lock renewal, and all result
+recording; the child (an internal `run-child` entrypoint of the same gbrain
+binary) re-validates the claim, runs the handler with its own small engine
+pool, and reports one atomic outcome file. Handler-error semantics are
+preserved across the boundary (unrecoverable → dead, rate-lease → no attempt
+burned, everything else → normal backoff). On worker shutdown children get
+the drain window to finish and report; a child killed before reporting is
+released with no attempt burned. If the worker dies hard, the orphaned child
+self-terminates via a parent-liveness watchdog and the stall sweeper
+requeues the job after lock expiry — the lock token fences the orphan's
+queue writes (result recording, progress, state transitions) into no-ops.
+The handler's own side effects (page writes through its engine) can still
+land until the watchdog stops the child; that window is the watchdog's
+poll + grace, not unbounded.
+
+Sizing notes:
+
+- **Connections:** each child opens its own small pools (read 3 by default,
+ override via `GBRAIN_JOB_CHILD_POOL_SIZE`; direct 1). Worked example at
+ concurrency 15: 15×(3+1) + the worker's 10+3 ≈ **73 client connections**
+ total — 55 ride the transaction-pooler lane (multiplexed, no extra server
+ backends) and 18 are lazy direct session-lane connections, each holding a
+ real server backend while open. Budget the pooler-lane count against your
+ pooler's client limit and the session-lane count against
+ `max_connections`.
+- **Memory:** `--max-rss` covers the WORKER process only in this mode
+ (handler memory lives in the children; the worker prints a note when both
+ are set). There is no per-child RSS cap yet — a runaway child is contained
+ only by host/container limits. Size host memory for concurrency × handler
+ footprint.
+- **Spawn cost:** ~0.3–1s per job (engine connect included) — noise for
+ long-running handlers, meaningful for sub-second ones (`lint`,
+ `backlinks`). Keep those inline or on a separate inline worker.
+- **Security note:** the child receives the job's lock token via env. It is
+ a *fencing* token (split-brain protection), not a secret — same-user env
+ already contains the database URL.
+- **Child CLI resolution:** the worker fail-fast validates the child CLI at
+ startup (compiled `gbrain` binary, bun-dev fallback, or the
+ `GBRAIN_JOB_CHILD_CLI` env override — the ops/test escape hatch). Three
+ consecutive child spawn/bootstrap failures self-exit the worker as
+ unhealthy (a deterministically broken child CLI) for process-manager
+ restart instead of burning attempts across the queue.
+
### Which supervisor when?
The supervisor solves in-process crash recovery. Platform-level
diff --git a/docs/guides/queue-operations-runbook.md b/docs/guides/queue-operations-runbook.md
index f7bd241c9f..be1056d87e 100644
--- a/docs/guides/queue-operations-runbook.md
+++ b/docs/guides/queue-operations-runbook.md
@@ -71,7 +71,10 @@ gbrain jobs get <id>
## Rescue actions (in order of escalation)
```bash
-# Force-kill a single stuck job:
+# Cancel a single stuck job (inline mode: cooperative — the handler must
+# observe its abort signal, and after 30s it is force-evicted from tracking
+# but the promise keeps running; with --job-isolation process the child is
+# actually SIGTERM→SIGKILLed once cancellation is detected):
gbrain jobs cancel <id>
# Clear a specific job entirely (last resort):
@@ -119,6 +122,29 @@ claiming. Start one:
GBRAIN_ALLOW_SHELL_JOBS=1 gbrain jobs work --concurrency 4
```
+## Reading the DB-probe verdicts (pool starved vs server unreachable)
+
+When the worker's health probe fails repeatedly, the terminal
+`[health] DB probe failed N consecutive times (verdict: ...)` line — and the
+`unhealthy` payload the supervisor sees — carries a verdict that names the
+failing LAYER (the intermediate `(N/3)` lines log only the failure detail).
+Read it before touching anything — the historical failure mode here was
+hours spent evaluating a database instance upgrade while the server sat at
+10% of max_connections.
+
+| Verdict | What it means | What to do |
+|---|---|---|
+| `pool_starved` | The read-pool probe failed but the DIRECT-lane probe succeeded — the database server is reachable; the fault is in the transaction-pooler path (client pool exhaustion or a pooler-layer fault; the probe deliberately does not distinguish the two). | Look at client-side load: long-running handler queries holding slots, `GBRAIN_POOL_SIZE` too small for the workload, or a pooler-layer incident. Do NOT resize the database. The worker exit is correct recovery — it frees every client-held slot. |
+| `server_unreachable` | Both the pooler lane and the direct lane failed. | Check connectivity/capacity first: network, DNS, the database itself. Both-lanes-failed is the evidence — credential/config errors or a saturated direct lane can also land here, so glance at the probe detail text before concluding the server is down. |
+| `unknown` | The read probe failed and no direct lane exists to disambiguate (single-pool mode: non-Supabase, kill switch active, or no derivable direct URL). | Check the startup log for the single-pool warning; consider `GBRAIN_DIRECT_DATABASE_URL` so future incidents self-diagnose. |
+
+The `gbrain-tracked in flight` counts in the message are a tracked SUBSET
+(raw/direct/reserved/transaction seams only) — most template-path queries are
+untracked, so `0 in flight` next to a `pool_starved` verdict means the
+saturation lives in that untracked traffic or at the pooler layer itself,
+not that the pool is idle. The verdict, not the counts, is the
+authoritative signal.
+
## Related
- [Minions worker deployment](minions-deployment.md) — supervisor lifecycle,
diff --git a/llms-full.txt b/llms-full.txt
index 3985b7cfb6..d09f6a4b70 100644
--- a/llms-full.txt
+++ b/llms-full.txt
@@ -1921,7 +1921,7 @@ The whole loop is described in [`docs/architecture/topologies.md`](docs/architec
**Self-wiring knowledge graph.** Every `put_page` extracts entity refs from markdown/wikilinks/typed-link syntax and writes edges with zero LLM calls. Typed edges (`attended`, `works_at`, `invested_in`, `founded`, `advises`, `mentions`, …). Multi-hop traversal via `gbrain graph-query`. The graph is what produces the +31.4 P@5 lift over vector-only RAG. **Obsidian-style vaults:** bare `[[note-name]]` wikilinks that point across folders — you wrote `[[struktura]]` but the page lives at `projects/struktura.md` — resolve by basename once you opt in with `gbrain config set link_resolution.global_basename true`. Off by default; `gbrain doctor` tells you how many edges you'd gain before you flip it. See [migrating an Obsidian vault](INSTALL_FOR_AGENTS.md#step-45-wire-the-knowledge-graph).
-**Job queue (Minions).** BullMQ-shaped, Postgres-native job queue. Durable subagents (LLM tool loops that survive crashes via two-phase pending→done persistence), shell jobs with audit, child jobs with cascading timeouts, rate leases for outbound providers, attachments via S3/Supabase storage. Replaces "spawn subagent as fire-and-forget Promise" with something that recovers from anything.
+**Job queue (Minions).** BullMQ-shaped, Postgres-native job queue. Durable subagents (LLM tool loops that survive crashes via two-phase pending→done persistence), shell jobs with audit, child jobs with cascading timeouts, rate leases for outbound providers, attachments via S3/Supabase storage. Opt-in per-job process isolation (`gbrain jobs work --job-isolation process`) runs each claimed job in its own SIGKILL-able child process, so a stuck handler dies for real and a crash takes one job instead of the whole worker; when the worker's DB health probe fails, it names the failing layer (`pool_starved` vs `server_unreachable`) instead of a blanket "DB unreachable". Sizing and rollout guidance in [`docs/guides/minions-deployment.md`](docs/guides/minions-deployment.md); probe-verdict triage in [`docs/guides/queue-operations-runbook.md`](docs/guides/queue-operations-runbook.md). Replaces "spawn subagent as fire-and-forget Promise" with something that recovers from anything.
**Non-English brains (FTS language config).** The Postgres full-text search tokenizer is configurable via `GBRAIN_FTS_LANGUAGE`. Defaults to `english`. Set it to any text-search configuration that exists in your Postgres instance:
diff --git a/openclaw.plugin.json b/openclaw.plugin.json
index 2761b33699..304610e95e 100644
--- a/openclaw.plugin.json
+++ b/openclaw.plugin.json
@@ -1,7 +1,7 @@
{
"id": "gbrain-context-engine",
"name": "gbrain",
- "version": "0.46.0.0",
+ "version": "0.46.1.0",
"description": "Personal knowledge brain with Postgres + pgvector hybrid search",
"family": "bundle-plugin",
"configSchema": {
diff --git a/package.json b/package.json
index 462236bb87..0fb3dc057e 100644
--- a/package.json
+++ b/package.json
@@ -157,7 +157,7 @@
"bun": ">=1.3.10"
},
"license": "MIT",
- "version": "0.46.0.0",
+ "version": "0.46.1.0",
"overrides": {
"@hono/node-server": "^2.0.5",
"fast-uri": "^3.1.5",
diff --git a/src/commands/jobs.ts b/src/commands/jobs.ts
index 7acbe2f238..5380c1b424 100644
--- a/src/commands/jobs.ts
+++ b/src/commands/jobs.ts
@@ -6,7 +6,12 @@
import type { BrainEngine } from '../core/engine.ts';
import { MinionQueue } from '../core/minions/queue.ts';
import { MinionWorker } from '../core/minions/worker.ts';
-import { WORKER_EXIT_RSS_WATCHDOG } from '../core/minions/worker-exit-codes.ts';
+import {
+ WORKER_EXIT_RSS_WATCHDOG,
+ JOB_CHILD_EXIT_USAGE,
+} from '../core/minions/worker-exit-codes.ts';
+import { CHILD_ENV, resolveChildCliInvocation } from '../core/minions/job-isolation.ts';
+import { runChildJobEntry } from '../core/minions/run-child.ts';
import type { MinionHandler, MinionJob, MinionJobStatus } from '../core/minions/types.ts';
import type { PaceKeyOverrides } from '../core/pace-mode.ts';
import { loadConfig, isThinClient } from '../core/config.ts';
@@ -154,6 +159,32 @@ export function resolveWorkerConcurrency(args: string[], env: NodeJS.ProcessEnv
return parsed;
}
+export type JobIsolationMode = 'inline' | 'process';
+
+/**
+ * issue #5: `--job-isolation <inline|process>` (space or `=` form), env
+ * fallback GBRAIN_JOB_ISOLATION, default inline. `process` runs each claimed
+ * job in a SIGKILL-able child process — blast radius 1 job instead of N.
+ * Env injected as a param so tests never mutate process.env (rule R1).
+ * Invalid values fail fast (parseMaxRssFlag convention).
+ */
+export function parseJobIsolationFlag(
+ args: string[],
+ env: NodeJS.ProcessEnv = process.env,
+): JobIsolationMode {
+ let raw: string | undefined;
+ const eqForm = args.find((a) => a.startsWith('--job-isolation='));
+ if (eqForm !== undefined) raw = eqForm.slice('--job-isolation='.length);
+ if (raw === undefined) raw = parseFlag(args, '--job-isolation');
+ if (raw === undefined || raw === '') raw = env.GBRAIN_JOB_ISOLATION;
+ if (raw === undefined || raw === '') return 'inline';
+ if (raw === 'inline' || raw === 'process') return raw;
+ console.error(
+ `Error: invalid job isolation mode ${JSON.stringify(raw)}. Valid: inline, process.`,
+ );
+ process.exit(1);
+}
+
/**
* #3026: the thin-client `list`/`get` branches receive jobs as parsed JSON
* off the MCP wire, where every timestamp is an ISO string — but formatJob /
@@ -269,11 +300,13 @@ USAGE
gbrain jobs watch [--json] [--follow] [--refresh-ms=N]
gbrain jobs work [--queue Q] [--concurrency N] [--max-rss MB]
[--health-interval MS] [--nice N]
+ [--job-isolation inline|process]
gbrain jobs supervisor [start] [--detach] [--json]
[--concurrency N] [--queue Q] [--pid-file PATH]
[--max-crashes N] [--health-interval N]
[--allow-shell-jobs] [--cli-path PATH]
[--max-rss MB] [--nice N]
+ [--job-isolation inline|process]
--nice N OS scheduling priority, -20 (highest) to 19 (nicest). Lowers CPU
priority without cutting concurrency — full throughput when the
@@ -344,9 +377,18 @@ const JOBS_SUBCOMMAND_HELP: Record<string, string> = {
USAGE
gbrain jobs work [--queue Q] [--concurrency N] [--max-rss MB]
[--health-interval MS] [--nice N]
+ [--job-isolation inline|process]
OPTIONS
--queue Q Queue to claim from (default: default)
+ --job-isolation M inline (default): handlers run in the worker process.
+ process: each claimed job runs in its own child
+ process — a stuck handler is group-SIGKILLed instead
+ of abandoned, and a crash takes one job, not all N.
+ Env fallback: GBRAIN_JOB_ISOLATION. Recommended for
+ long-running LLM-bound handlers (subagent). Note:
+ --max-rss then covers the worker only, and each child
+ adds ~4 pooler client connections.
--concurrency N Max jobs in flight. Resolution: flag, then
GBRAIN_WORKER_CONCURRENCY env, then 1. Values < 1
are clamped to 1 with a loud stderr note.
@@ -376,6 +418,7 @@ USAGE
[--max-crashes N] [--health-interval N]
[--allow-shell-jobs] [--cli-path PATH]
[--max-rss MB] [--nice N]
+ [--job-isolation inline|process]
gbrain jobs supervisor status [--json] [--pid-file PATH]
gbrain jobs supervisor stop [--json] [--pid-file PATH]
@@ -397,6 +440,7 @@ OPTIONS (start)
--cli-path PATH Explicit gbrain binary for the worker child
--max-rss MB RSS watchdog for the worker (same rules as jobs work)
--nice N OS priority for supervisor + worker children
+ --job-isolation M Passed through to the worker (see jobs work --help)
EXIT CODES (start)
0 clean shutdown 1 max crashes exceeded
@@ -1185,6 +1229,59 @@ export async function runJobs(engineOrNull: BrainEngine | null, args: string[]):
process.exit(0);
}
+ case 'run-child': {
+ // INTERNAL (issue #5 process isolation): spawned by `jobs work` with
+ // process isolation enabled. One job, one process: validate the claim,
+ // run the handler with the child's own engine, write ONE outcome file,
+ // exit. Deliberately absent from user-facing help. The CLI layer owns
+ // engine.disconnect() + process.exit() (engine-ownership invariant).
+ {
+ const config = loadConfig();
+ if (config?.engine === 'pglite') {
+ console.error('[run-child] process isolation requires the Postgres engine.');
+ await engine.disconnect();
+ process.exit(JOB_CHILD_EXIT_USAGE);
+ }
+ const jobIdRaw = parseFlag(args, '--job-id');
+ const jobId = jobIdRaw != null ? parseInt(jobIdRaw, 10) : NaN;
+ const lockToken = process.env[CHILD_ENV.lockToken];
+ const resultPath = process.env[CHILD_ENV.resultPath];
+ const parentPidRaw = parseInt(process.env[CHILD_ENV.parentPid] ?? '0', 10);
+ if (!Number.isInteger(jobId) || jobId <= 0 || !lockToken || !resultPath) {
+ console.error(
+ '[run-child] internal command spawned by the jobs worker; requires ' +
+ `a numeric job id plus ${CHILD_ENV.lockToken} and ${CHILD_ENV.resultPath} in env.`,
+ );
+ await engine.disconnect();
+ process.exit(JOB_CHILD_EXIT_USAGE);
+ }
+
+ // Same handler surface as the worker: registerBuiltinHandlers also
+ // performs plugin discovery, so plugin subagent jobs isolate too.
+ const throwaway = new MinionWorker(engine, { queue: 'default', concurrency: 1 });
+ await registerBuiltinHandlers(throwaway, engine, { quiet: true });
+
+ let code: number;
+ try {
+ code = await runChildJobEntry(
+ engine,
+ {
+ jobId,
+ lockToken,
+ resultPath,
+ parentPid: Number.isInteger(parentPidRaw) && parentPidRaw > 0 ? parentPidRaw : 0,
+ },
+ { resolveHandler: (name) => throwaway.getHandler(name) },
+ );
+ } catch (e) {
+ console.error(`[run-child] fatal: ${e instanceof Error ? e.message : String(e)}`);
+ code = 1;
+ }
+ await engine.disconnect();
+ process.exit(code);
+ }
+ }
+ // eslint-disable-next-line no-fallthrough -- unreachable: the case above always exits
case 'work': {
// Check if PGLite
const config = (await import('../core/config.ts')).loadConfig();
@@ -1245,11 +1342,78 @@ export async function runJobs(engineOrNull: BrainEngine | null, args: string[]):
}
}
+ // issue #5: per-job process isolation. Resolve + validate the child CLI
+ // invocation ONCE at startup and refuse to start on failure — a bad
+ // path discovered per-job would release every claim as infra failures
+ // (never dead-lettering, but never progressing either).
+ const jobIsolation = parseJobIsolationFlag(args);
+ let childCliInvocation: { cmd: string; argsPrefix: string[] } | null = null;
+ let childTiniPath = '';
+ if (jobIsolation === 'process') {
+ const { resolveGbrainCliPath } = await import('./autopilot.ts');
+ const inv = resolveChildCliInvocation(
+ process.env,
+ process.execPath,
+ process.argv[1],
+ () => resolveGbrainCliPath(),
+ );
+ if (!inv) {
+ console.error(
+ 'Error: process isolation needs a resolvable gbrain CLI for job children ' +
+ '(compiled binary on PATH, or GBRAIN_JOB_CHILD_CLI override).',
+ );
+ process.exit(1);
+ }
+ // Canonicalize BEFORE validating: existsSync on a relative name checks
+ // cwd while spawn() resolves via PATH — the validated file and the
+ // executed binary could differ (security review). Resolving to an
+ // absolute path makes the fail-fast check and the spawn agree.
+ const { existsSync: childCliExists } = await import('node:fs');
+ const { resolve: resolveCliPath } = await import('node:path');
+ inv.cmd = resolveCliPath(inv.cmd);
+ if (!childCliExists(inv.cmd)) {
+ console.error(
+ `Error: resolved child CLI does not exist: ${inv.cmd} ` +
+ '(set GBRAIN_JOB_CHILD_CLI to a valid gbrain binary).',
+ );
+ process.exit(1);
+ }
+ childCliInvocation = inv;
+ const { detectTini } = await import('../core/minions/spawn-helpers.ts');
+ childTiniPath = detectTini();
+ if (maxRssMb > 0) {
+ console.error(
+ '[gbrain jobs] note: with process isolation on, the --max-rss watchdog covers the ' +
+ 'WORKER process only — handler memory now lives in job children. Per-child caps are ' +
+ 'a filed follow-up; size host memory for concurrency x handler footprint.',
+ );
+ }
+ }
+
try { await queue.ensureSchema(); }
catch (e) { console.error(e instanceof Error ? e.message : String(e)); process.exit(1); }
+ // issue #6: the direct-pool kill switch collapses lock renewal, health
+ // probes, and handler workload onto ONE shared pool — silently. Make
+ // the collapse loud at startup so a later 'pool_starved' incident has
+ // an obvious prior warning instead of a mystery.
+ {
+ const { getConnectionRouting } = await import('../core/minions/db-probe.ts');
+ const cm = getConnectionRouting(engine);
+ if (cm?.isDualPoolActive && !cm.isDualPoolActive()) {
+ const killSwitched = cm.describeMode?.().kill_switch_active === true;
+ console.error(
+ `[gbrain jobs] single-pool mode: lock renewal, health probes and handler workload share ` +
+ `one connection pool${killSwitched ? ' (direct-lane kill switch is active)' : ''}. ` +
+ `Under heavy handler load this pool can starve the lock heartbeat. For Supabase brains, ` +
+ `ensure the direct (5432) host is reachable or set GBRAIN_DIRECT_DATABASE_URL.`,
+ );
+ }
+ }
+
const worker = new MinionWorker(engine, {
queue: queueName, concurrency, maxRssMb, healthCheckInterval,
+ jobIsolation, childCliInvocation, childTiniPath,
});
await registerBuiltinHandlers(worker, engine);
@@ -1259,9 +1423,37 @@ export async function runJobs(engineOrNull: BrainEngine | null, args: string[]):
// the external PM (systemd, Docker, cron watchdog) restart cleanly.
worker.on('unhealthy', (info) => {
if (info.reason === 'db_dead') {
+ // issue #6: name the failing LAYER, not just "DB unreachable" —
+ // that message sent operators chasing database capacity while the
+ // real fault was client-side pool exhaustion. Exiting is still
+ // correct recovery either way (it frees every client-held slot).
+ if (info.verdict === 'pool_starved') {
+ console.error(
+ `[health] FATAL: connection-pool path saturated after ${info.consecutiveFailures} probes — ` +
+ `the database server itself is reachable. (${info.message}) ` +
+ `Likely causes: long-running handler queries holding pool slots, or too-small GBRAIN_POOL_SIZE ` +
+ `for this workload. Consider --job-isolation process for long-running handlers ` +
+ `(handler connections then die with each job's child process). ` +
+ `Exiting for process-manager restart (frees all client-held slots).`,
+ );
+ } else if (info.verdict === 'server_unreachable') {
+ console.error(
+ `[health] FATAL: database server unreachable after ${info.consecutiveFailures} probes ` +
+ `(both pooler and direct lanes failed). (${info.message}) ` +
+ `Exiting for process-manager restart.`,
+ );
+ } else {
+ console.error(
+ `[health] FATAL: DB probe failed ${info.consecutiveFailures} consecutive times (${info.message}). ` +
+ `Exiting for process-manager restart.`,
+ );
+ }
+ } else if (info.reason === 'child_spawn_failing') {
console.error(
- `[health] FATAL: DB unreachable after ${info.consecutiveFailures} probes (${info.message}). ` +
- `Exiting for process-manager restart.`,
+ `[health] FATAL: ${info.consecutiveFailures} consecutive job-child spawn/bootstrap ` +
+ `failures (${info.message}). The child CLI is deterministically broken — fix the ` +
+ `worker's child CLI configuration (or GBRAIN_JOB_CHILD_CLI). Exiting for ` +
+ `process-manager restart.`,
);
} else {
console.error(
@@ -1290,7 +1482,10 @@ export async function runJobs(engineOrNull: BrainEngine | null, args: string[]):
: `, health-check: ${Math.round(healthCheckInterval / 1000)}s`)
: '';
const niceNote = niceResult ? `, nice: ${formatNice(niceResult.effective ?? niceVal!)}` : '';
- console.log(`Minion worker started (queue: ${queueName}, concurrency: ${concurrency}${watchdogNote}${healthNote}${niceNote})`);
+ const isolationNote = jobIsolation === 'process'
+ ? `, isolation: process (child cli: ${childCliInvocation?.cmd}${childTiniPath ? ', tini' : ''})`
+ : '';
+ console.log(`Minion worker started (queue: ${queueName}, concurrency: ${concurrency}${watchdogNote}${healthNote}${niceNote}${isolationNote})`);
console.log(`Registered handlers: ${worker.registeredNames.join(', ')}`);
// Register in the live worker registry (issue #1815) so jobs stats / doctor
@@ -1609,6 +1804,7 @@ export async function runJobs(engineOrNull: BrainEngine | null, args: string[]):
allowShellJobs,
json: jsonMode,
maxRssMb,
+ jobIsolation: parseJobIsolationFlag(args),
...(supNice !== undefined ? { nice_requested: supNice } : {}),
...(supNiceResult?.effective != null ? { nice_effective: supNiceResult.effective } : {}),
...(supNiceResult?.error ? { nice_error: supNiceResult.error } : {}),
diff --git a/src/core/cli-flag-registry.generated.ts b/src/core/cli-flag-registry.generated.ts
index 3ab8cca267..378f62ce94 100644
--- a/src/core/cli-flag-registry.generated.ts
+++ b/src/core/cli-flag-registry.generated.ts
@@ -40,7 +40,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
'config': ['--aliases', '--all', '--brain', '--column', '--coverage-override', '--detail', '--embedding-dimensions', '--embedding-model', '--fast', '--federated-read', '--follow', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--markdown', '--model', '--multimodal', '--no-embedding', '--no-extract', '--no-federated', '--pattern', '--pending', '--pglite', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--surface', '--thin', '--token-ttl', '--yes'],
'connect': ['--agent', '--bearer-token-env-var', '--bind', '--brain', '--client-id', '--client-secret', '--force', '--grant-types', '--help', '--http', '--install', '--json', '--name', '--oauth', '--public-url', '--register', '--scope', '--scopes', '--show-token', '--source', '--timeout-ms', '--token', '--token-endpoint-auth-method', '--url', '--version', '--yes'],
'conversation-parser': ['--aliases', '--all', '--brain', '--help', '--include-null-signature', '--json', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin'],
- 'doctor': ['--ab', '--abbrev-ref', '--abi', '--abort', '--aliases', '--all', '--allow-shell-jobs', '--allow-unverified-remote', '--auto', '--auto-fix', '--auto-update', '--background', '--batch', '--brain', '--brain-wide-max-cost-usd', '--branch', '--break-lock', '--build-index', '--by-mention', '--by-type', '--cached', '--check', '--column', '--compile', '--concurrency', '--confidence', '--confirm', '--content-audit', '--count', '--days', '--delete-brain', '--detach', '--detail', '--diff-filter', '--dim', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--exclude-standard', '--exclusive', '--explain', '--fast', '--file', '--fix', '--follow', '--force', '--force-break-lock', '--force-retry', '--force-schema', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--get', '--git-dir', '--git-path', '--grant-types', '--harness', '--health-interval', '--help', '--history', '--home', '--http', '--include-flagged', '--include-frontmatter', '--include-null-signature', '--include-pseudo', '--index-audit', '--init', '--input', '--is-inside-work-tree', '--jq', '--json', '--lang', '--limit', '--local', '--locks', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-crashes', '--max-jobs', '--max-rss', '--max-usd', '--mcp-only', '--migrate-only', '--model', '--multimodal', '--name-only', '--name-status', '--near-symbol', '--nice', '--no', '--no-cron', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--no-mutate', '--no-verify', '--oauth-client-secret', '--older-than', '--once', '--others', '--overwrite', '--parallel', '--params', '--pat-file', '--path', '--pattern', '--pending', '--pglite', '--phase', '--pid-file', '--porcelain', '--priority', '--probe-pglite', '--progress-interval', '--progress-json', '--project', '--push-only', '--query', '--queue', '--quiet', '--rebase', '--rebuild-rollup', '--refresh', '--refresh-unqualified', '--regenerate', '--remediate', '--remediation-plan', '--remove', '--repo', '--reset', '--resolve', '--restore-only', '--resume', '--review-lower', '--rollback', '--scope', '--scopes', '--set', '--short', '--show-current', '--show-toplevel', '--since', '--skills-dir', '--skip-bare-tweet', '--skip-failed', '--skip-urls', '--skip-verify', '--slugs', '--source', '--source-id', '--stale', '--stats', '--status', '--strategy', '--strict', '--supabase', '--supersessions', '--surface', '--symbol-kind', '--target', '--target-score', '--thin', '--timeout', '--to', '--token', '--token-ttl', '--top-k', '--type', '--undo-wave', '--unsafe-bypass-dream-guard', '--unset-all', '--untracked-files', '--url', '--use-captured-snapshot', '--verbose', '--verify', '--version', '--window', '--with-calibration', '--workers', '--yes'],
+ 'doctor': ['--ab', '--abbrev-ref', '--abi', '--abort', '--aliases', '--all', '--allow-shell-jobs', '--allow-unverified-remote', '--auto', '--auto-fix', '--auto-update', '--background', '--batch', '--brain', '--brain-wide-max-cost-usd', '--branch', '--break-lock', '--build-index', '--by-mention', '--by-type', '--cached', '--check', '--column', '--compile', '--concurrency', '--confidence', '--confirm', '--content-audit', '--count', '--days', '--delete-brain', '--detach', '--detail', '--diff-filter', '--dim', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--exclude-standard', '--exclusive', '--explain', '--fast', '--file', '--fix', '--follow', '--force', '--force-break-lock', '--force-retry', '--force-schema', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--get', '--git-dir', '--git-path', '--grant-types', '--harness', '--health-interval', '--help', '--history', '--home', '--http', '--include-flagged', '--include-frontmatter', '--include-null-signature', '--include-pseudo', '--index-audit', '--init', '--input', '--is-inside-work-tree', '--job-isolation', '--jq', '--json', '--lang', '--limit', '--local', '--locks', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-crashes', '--max-jobs', '--max-rss', '--max-usd', '--mcp-only', '--migrate-only', '--model', '--multimodal', '--name-only', '--name-status', '--near-symbol', '--nice', '--no', '--no-cron', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--no-mutate', '--no-verify', '--oauth-client-secret', '--older-than', '--once', '--others', '--overwrite', '--parallel', '--params', '--pat-file', '--path', '--pattern', '--pending', '--pglite', '--phase', '--pid-file', '--porcelain', '--priority', '--probe-pglite', '--progress-interval', '--progress-json', '--project', '--push-only', '--query', '--queue', '--quiet', '--rebase', '--rebuild-rollup', '--refresh', '--refresh-unqualified', '--regenerate', '--remediate', '--remediation-plan', '--remove', '--repo', '--reset', '--resolve', '--restore-only', '--resume', '--review-lower', '--rollback', '--scope', '--scopes', '--set', '--short', '--show-current', '--show-toplevel', '--since', '--skills-dir', '--skip-bare-tweet', '--skip-failed', '--skip-urls', '--skip-verify', '--slugs', '--source', '--source-id', '--stale', '--stats', '--status', '--strategy', '--strict', '--supabase', '--supersessions', '--surface', '--symbol-kind', '--target', '--target-score', '--thin', '--timeout', '--to', '--token', '--token-ttl', '--top-k', '--type', '--undo-wave', '--unsafe-bypass-dream-guard', '--unset-all', '--untracked-files', '--url', '--use-captured-snapshot', '--verbose', '--verify', '--version', '--window', '--with-calibration', '--workers', '--yes'],
'dream': ['--against', '--aliases', '--all', '--allow-regression', '--anchor', '--asof', '--background', '--batch', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--budget-usd', '--budget-usd-answer', '--budget-usd-retrieval', '--by-type', '--by-type-floor', '--code', '--committed-baseline', '--compare', '--compile', '--concurrent', '--ctx-size', '--cycles', '--date', '--detail', '--dimensions', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--embeddings', '--expansion', '--explain', '--fast', '--federated', '--fix', '--fixtures', '--follow', '--force', '--force-break-lock', '--force-rechunk', '--force-retry', '--format', '--from', '--from-db', '--from-pages', '--gold', '--harness', '--help', '--http', '--include-holdout', '--include-null-signature', '--input', '--install', '--json', '--judge-model', '--justification', '--keyword-only', '--lang', '--limit', '--llm', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-runtime', '--max-tokens', '--max-usd', '--mcp-only', '--min-recall', '--mode', '--model', '--models', '--modes', '--multimodal', '--name', '--name-only', '--near-symbol', '--no', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--no-llm', '--no-mutate', '--no-trajectory', '--once', '--out', '--output', '--output-dir', '--parallel', '--path', '--pattern', '--pending', '--pglite', '--phase', '--priority', '--progress-interval', '--progress-json', '--pull', '--quiet', '--receipt-dir', '--remediate', '--repo', '--reranking', '--reset', '--resolve', '--restore-only', '--resume-from', '--retrieval-only', '--rounds', '--rubric-version', '--save', '--seed', '--short', '--show-toplevel', '--since', '--skip-replay', '--slot-a-model', '--slot-b-model', '--slot-c-model', '--slug', '--slug-prefix', '--source', '--source-id', '--stale', '--suite', '--suites', '--supabase', '--supersessions', '--surface', '--symbol-kind', '--take', '--task', '--thin', '--threshold', '--timeout', '--to', '--token-ttl', '--top-k', '--undo', '--unsafe-bypass-dream-guard', '--update-baseline', '--verify', '--version', '--window', '--yes'],
'edges-backfill': ['--aliases', '--all', '--all-sources', '--brain', '--concurrency', '--federated', '--help', '--include-null-signature', '--json', '--max-age', '--max-chunks', '--max-cost-usd', '--no-extract', '--no-federated', '--older-than', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--workers'],
'embed': ['--aliases', '--all', '--background', '--batch-size', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--catch-up', '--dry-run', '--embedding-dimensions', '--embedding-model', '--explain', '--fast', '--fix', '--follow', '--force', '--force-break-lock', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--lang', '--markdown', '--max-age', '--max-cost-usd', '--model', '--multimodal', '--name', '--near-symbol', '--no', '--no-embed', '--no-embedding', '--no-extract', '--pace', '--pace-max-concurrency', '--parallel', '--path', '--pattern', '--pending', '--pglite', '--prefix', '--priority', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--restore-only', '--serial', '--slugs', '--source', '--stale', '--supabase', '--supersessions', '--surface', '--symbol-kind', '--thin', '--timeout', '--token-ttl', '--version'],
@@ -61,7 +61,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {
'init': ['--all', '--brain', '--chat-model', '--check', '--ctx-size', '--embedding-dimensions', '--embedding-model', '--entity', '--expansion-model', '--fast', '--flag', '--force', '--from-pages', '--grant-types', '--help', '--http', '--issuer-url', '--json', '--judge-model', '--key', '--mcp-only', '--mcp-url', '--migrate-only', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--path', '--pglite', '--provenance', '--schema-pack', '--scopes', '--skip-embed-check', '--source', '--stale', '--supabase', '--surface', '--to', '--token-ttl', '--touchpoint', '--url', '--version'],
'integrations': ['--auto', '--brain', '--dry-run', '--embeddings', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--no-embedding', '--overwrite', '--refresh', '--reranking', '--source', '--surface', '--target', '--token-ttl'],
'integrity': ['--aliases', '--all', '--auto', '--backend', '--background', '--brain', '--brain-wide-max-cost-usd', '--check', '--confidence', '--cost', '--dry-run', '--explain', '--fast', '--follow', '--force', '--fresh', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--limit', '--no-embedding', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--review-lower', '--skip-bare-tweet', '--skip-urls', '--source', '--stale', '--supabase', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl', '--type', '--url'],
- 'jobs': ['--abbrev-ref', '--aliases', '--all', '--allow-empty', '--allow-protected', '--allow-shell-jobs', '--apply', '--asof', '--auto', '--auto-fix', '--auto-with-prompt', '--background', '--backoff-delay', '--backoff-jitter', '--backoff-type', '--batch', '--batch-size', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--break-lock', '--budget-usd', '--budget-usd-per-day', '--by-mention', '--by-type', '--cached', '--catch-up', '--check', '--cli-path', '--cluster', '--cluster-errors', '--code', '--concurrency', '--confidence', '--confirm-destructive', '--content', '--date', '--days', '--delay', '--detach', '--diff-filter', '--dim', '--dimensions', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--empty', '--entities', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--federated-read', '--ff-only', '--file', '--fix', '--follow', '--force', '--force-break-lock', '--force-retry', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--hard-deadline', '--health-interval', '--held-out', '--help', '--http', '--idempotency-key', '--image', '--include-frontmatter', '--include-gitignored', '--include-null-signature', '--infer-dates', '--inject-bootstrap', '--inline', '--input', '--install', '--interval', '--is-ancestor', '--json', '--kind', '--lang', '--limit', '--lock', '--markdown', '--max-age', '--max-attempts', '--max-cost-usd', '--max-crashes', '--max-rss', '--max-runtime-min', '--max-sources', '--max-stalled', '--max-usd', '--max-waiting', '--mcp-only', '--migrate-only', '--min-context', '--missing-path', '--mode', '--model', '--multimodal', '--name-only', '--name-status', '--near-symbol', '--ner', '--nice', '--no', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-federate', '--no-federated', '--no-gpg-sign', '--no-hard-deadline', '--no-inject', '--no-mutate', '--no-pull', '--no-renames', '--no-schema-pack', '--no-verify', '--no-worker', '--non-interactive', '--now', '--offset', '--older-than', '--once', '--order', '--orphan', '--others', '--output', '--override-disabled', '--pace', '--pace-max-concurrency', '--pack', '--parallel', '--params', '--path', '--pattern', '--pending', '--phase', '--pid-file', '--priority', '--progress-interval', '--progress-json', '--queue', '--quiet', '--redact-secrets', '--reenrich-after', '--refresh-cache', '--refresh-ms', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--resume', '--retry-failed', '--review-lower', '--run-id', '--save', '--segment-limit', '--serial', '--session', '--session-id', '--short', '--show-toplevel', '--sigkill-rescue', '--since', '--skip-bare-tweet', '--skip-failed', '--skip-urls', '--sleep', '--slug', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--stats', '--status', '--strategy', '--supersessions', '--surface', '--swap-only', '--symbol-kind', '--target', '--target-score', '--thin', '--thin-threshold', '--timeout', '--timeout-ms', '--to', '--token-ttl', '--trusted-extraction', '--type', '--types', '--uninstall', '--unsafe-bypass-dream-guard', '--url', '--user', '--verbose', '--verify', '--version', '--watch', '--wedge-rescue', '--with-db', '--workers', '--yes'],
+ 'jobs': ['--abbrev-ref', '--aliases', '--all', '--allow-empty', '--allow-protected', '--allow-shell-jobs', '--apply', '--asof', '--auto', '--auto-fix', '--auto-with-prompt', '--background', '--backoff-delay', '--backoff-jitter', '--backoff-type', '--batch', '--batch-size', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--break-lock', '--budget-usd', '--budget-usd-per-day', '--by-mention', '--by-type', '--cached', '--catch-up', '--check', '--cli-path', '--cluster', '--cluster-errors', '--code', '--concurrency', '--confidence', '--confirm-destructive', '--content', '--date', '--days', '--delay', '--detach', '--diff-filter', '--dim', '--dimensions', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--empty', '--entities', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--federated-read', '--ff-only', '--file', '--fix', '--follow', '--force', '--force-break-lock', '--force-retry', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--hard-deadline', '--health-interval', '--held-out', '--help', '--http', '--idempotency-key', '--image', '--include-frontmatter', '--include-gitignored', '--include-null-signature', '--infer-dates', '--inject-bootstrap', '--inline', '--input', '--install', '--interval', '--is-ancestor', '--job-id', '--job-isolation', '--json', '--kind', '--lang', '--limit', '--lock', '--markdown', '--max-age', '--max-attempts', '--max-cost-usd', '--max-crashes', '--max-rss', '--max-runtime-min', '--max-sources', '--max-stalled', '--max-usd', '--max-waiting', '--mcp-only', '--migrate-only', '--min-context', '--missing-path', '--mode', '--model', '--multimodal', '--name-only', '--name-status', '--near-symbol', '--ner', '--nice', '--no', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-federate', '--no-federated', '--no-gpg-sign', '--no-hard-deadline', '--no-inject', '--no-mutate', '--no-pull', '--no-renames', '--no-schema-pack', '--no-verify', '--no-worker', '--non-interactive', '--now', '--offset', '--older-than', '--once', '--order', '--orphan', '--others', '--output', '--override-disabled', '--pace', '--pace-max-concurrency', '--pack', '--parallel', '--params', '--path', '--pattern', '--pending', '--phase', '--pid-file', '--priority', '--progress-interval', '--progress-json', '--queue', '--quiet', '--redact-secrets', '--reenrich-after', '--refresh-cache', '--refresh-ms', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--resume', '--retry-failed', '--review-lower', '--run-id', '--save', '--segment-limit', '--serial', '--session', '--session-id', '--short', '--show-toplevel', '--sigkill-rescue', '--since', '--skip-bare-tweet', '--skip-failed', '--skip-urls', '--sleep', '--slug', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--stats', '--status', '--strategy', '--supersessions', '--surface', '--swap-only', '--symbol-kind', '--target', '--target-score', '--thin', '--thin-threshold', '--timeout', '--timeout-ms', '--to', '--token-ttl', '--trusted-extraction', '--type', '--types', '--uninstall', '--unsafe-bypass-dream-guard', '--url', '--user', '--verbose', '--verify', '--version', '--watch', '--wedge-rescue', '--with-db', '--workers', '--yes'],
'lint': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--exclude', '--explain', '--fast', '--fix', '--follow', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--no-embedding', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--surface', '--thin', '--timeout', '--token-ttl'],
'lsd': ['--brain', '--force-resume', '--help', '--json', '--judge-model', '--limit', '--list-runs', '--max-cost', '--max-far-set', '--max-ideas-per-judge-call', '--no-save', '--resume', '--retry-judge', '--save', '--source', '--strict-budget', '--yes'],
'maintain': ['--aliases', '--all', '--background', '--brain', '--break-lock', '--by-mention', '--catch-up', '--column', '--concurrency', '--content-audit', '--count', '--detach', '--dim', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--explain', '--fast', '--fix', '--force', '--force-retry', '--force-schema', '--from-meetings', '--full', '--help', '--include-flagged', '--include-frontmatter', '--include-null-signature', '--index-audit', '--infer-dates', '--input', '--json', '--kind', '--lang', '--locks', '--markdown', '--max-cost', '--max-cost-usd', '--max-jobs', '--max-rss', '--max-usd', '--migrate-only', '--multimodal', '--near-symbol', '--ner', '--nice', '--no-extract', '--no-mutate', '--older-than', '--once', '--pack', '--parallel', '--params', '--path', '--pattern', '--pending', '--pglite', '--phase', '--pid-file', '--porcelain', '--probe-pglite', '--progress-json', '--query', '--queue', '--quiet', '--rebuild-rollup', '--regenerate', '--remediate', '--remediation-plan', '--reset', '--resolve', '--restore-only', '--resume', '--run-id', '--safe', '--scope', '--since', '--skills-dir', '--skip-failed', '--slugs', '--source', '--source-id', '--stale', '--status', '--supabase', '--supersessions', '--symbol-kind', '--target', '--target-score', '--thin', '--to', '--top-k', '--type', '--unsafe-bypass-dream-guard', '--url', '--verbose', '--window', '--workers', '--yes'],
diff --git a/src/core/connection-manager.ts b/src/core/connection-manager.ts
index acabbf3737..b7f09d277a 100644
--- a/src/core/connection-manager.ts
+++ b/src/core/connection-manager.ts
@@ -37,7 +37,7 @@
*/
import postgres from 'postgres';
-import { resolvePrepare, resolveSessionTimeouts, resolvePoolSize, endPoolBounded } from './db.ts';
+import { resolvePrepare, resolveSessionTimeouts, resolvePoolSize, resolveMaxLifetimeSeconds, endPoolBounded } from './db.ts';
import { redactPgUrl } from './url-redact.ts';
import { logConnectionEvent } from './connection-audit.ts';
@@ -303,6 +303,8 @@ export class ConnectionManager {
max: resolvePoolSize(this.opts.readPoolSize),
idle_timeout: 20,
connect_timeout: 10,
+ // Explicit (matches the postgres.js implicit default; GBRAIN_POOL_MAX_LIFETIME_S overrides).
+ max_lifetime: resolveMaxLifetimeSeconds(),
types: { bigint: postgres.BigInt },
};
const timeouts = resolveSessionTimeouts();
@@ -406,6 +408,8 @@ export class ConnectionManager {
max: size,
idle_timeout: 20,
connect_timeout: 10,
+ // Explicit (matches the postgres.js implicit default; GBRAIN_POOL_MAX_LIFETIME_S overrides).
+ max_lifetime: resolveMaxLifetimeSeconds(),
types: { bigint: postgres.BigInt },
// Always use prepared statements on the direct pool — no PgBouncer
// here, so the prepare-cache invalidation issue doesn't apply.
diff --git a/src/core/cycle/synthesize.ts b/src/core/cycle/synthesize.ts
index 9fb1bffcfb..3b8f3b1608 100644
--- a/src/core/cycle/synthesize.ts
+++ b/src/core/cycle/synthesize.ts
@@ -241,6 +241,55 @@ export function rewriteChunkedSlug(slug: string, hash6: string, idx: number): st
// ── Public entry ──────────────────────────────────────────────────────
+/**
+ * One drain-loop lock-renewal tick, extracted for hermetic tests (worker.ts
+ * parity is `runLockRenewalTick`; this is the deliberately simpler best-effort
+ * variant — no audit channel, no reconnect, no time-based give-up).
+ *
+ * Fixes the issue #6 abandoned-racer class in the cycle drain: the previous
+ * inline tick had no per-call timeout, so a hung renewLock stacked one
+ * checked-out pool slot per interval firing forever. Now each call carries an
+ * AbortSignal that is aborted when the timeout wins the race (the query is
+ * cancelled and its slot released), and callers guard re-entrancy so at most
+ * one renewal is in flight.
+ *
+ * Returns after the renewal settles or times out; a `false` renewal invokes
+ * `onLost` (token fence lost — caller aborts the handler). Errors and
+ * timeouts are swallowed: best-effort, the next tick retries.
+ */
+export async function runDrainRenewalTick(
+ renewLock: (
+ id: number,
+ lockToken: string,
+ lockMs: number,
+ opts?: { signal?: AbortSignal },
+ ) => Promise<boolean>,
+ jobId: number,
+ lockToken: string,
+ lockMs: number,
+ onLost: () => void,
+ callTimeoutMs: number,
+): Promise<void> {
+ const callAbort = new AbortController();
+ let timer: ReturnType<typeof setTimeout> | null = null;
+ try {
+ const ok = await Promise.race([
+ renewLock(jobId, lockToken, lockMs, { signal: callAbort.signal }),
+ new Promise<never>((_, reject) => {
+ timer = setTimeout(() => {
+ callAbort.abort();
+ reject(new Error(`renewLock timed out after ${callTimeoutMs}ms`));
+ }, callTimeoutMs);
+ }),
+ ]);
+ if (!ok) onLost();
+ } catch {
+ /* best-effort; next tick retries */
+ } finally {
+ if (timer != null) clearTimeout(timer);
+ }
+}
+
export interface SynthesizePhaseOpts {
brainDir: string;
dryRun: boolean;
@@ -433,12 +482,25 @@ export async function runSubagentsInline(
// just its own) can't requeue a live child. A false return means the row
// was cancelled or reclaimed — abort the handler. Errors are swallowed
// (best-effort; the next tick retries), never an unhandledRejection.
+ // Re-entrancy guard + per-call cancellation via runDrainRenewalTick: a
+ // hung renewLock no longer stacks a fresh checked-out pool slot per
+ // interval firing (issue #6 abandoned-racer class).
+ let drainTickInFlight = false;
const renewTimer = setInterval(() => {
- queue.renewLock(job.id, lockToken, lockMs)
- .then((ok) => {
- if (!ok && !abort.signal.aborted) abort.abort(new Error('lock-renewal-failed'));
- })
- .catch(() => { /* best-effort; next tick retries */ });
+ if (drainTickInFlight) return;
+ drainTickInFlight = true;
+ void runDrainRenewalTick(
+ (id, tok, ms, opts) => queue.renewLock(id, tok, ms, opts),
+ job.id,
+ lockToken,
+ lockMs,
+ () => {
+ if (!abort.signal.aborted) abort.abort(new Error('lock-renewal-failed'));
+ },
+ Math.max(1000, Math.floor(lockMs / 3)),
+ ).finally(() => {
+ drainTickInFlight = false;
+ });
}, Math.max(50, Math.floor(lockMs / 3)));
// Run, then record — separated so a completeJob connection error can't
// masquerade as a handler failure, and a failJob connection error can't
diff --git a/src/core/db-lock.ts b/src/core/db-lock.ts
index 656f3e91b6..8e95b12091 100644
--- a/src/core/db-lock.ts
+++ b/src/core/db-lock.ts
@@ -43,8 +43,13 @@ export interface DbLockHandle {
* false means the lock was stolen or released — the caller must stop
* relying on mutual exclusion. Transient DB errors still THROW (they are
* not evidence of a steal; the TTL is the backstop).
+ *
+ * `opts.signal` cancels the in-flight UPDATE when the caller's heartbeat
+ * timeout gives up on it (issue #6 — an abandoned refresh otherwise holds
+ * a checked-out pool slot for its full server-side duration). PGLite
+ * ignores the signal (single embedded connection, no pool to starve).
*/
- refresh: () => Promise<boolean>;
+ refresh: (opts?: { signal?: AbortSignal }) => Promise<boolean>;
}
/**
@@ -268,7 +273,7 @@ export async function tryAcquireDbLock(
return {
id: lockId,
acquiredAt: fence,
- refresh: async () => {
+ refresh: async (refreshOpts?: { signal?: AbortSignal }) => {
// v0.41.13.0: bump BOTH ttl_expires_at AND last_refreshed_at.
// v0.42.x (#1794): route through the DIRECT session pool, not the
// transaction pool, so a Supavisor pooler exhaustion (EMAXCONNSESSION)
@@ -280,6 +285,7 @@ export async function tryAcquireDbLock(
WHERE id = $2 AND holder_pid = $3 AND extract(epoch from acquired_at)::text = $4
RETURNING id`,
[ttl, lockId, pid, fence],
+ refreshOpts,
);
return updated.length > 0;
},
@@ -882,8 +888,13 @@ export async function withRefreshingLock<T>(
if (!handle) throw new LockUnavailableError(lockId);
let healthOk = true;
+ // Re-entrancy guard: with a 15s minimum cadence and a 30s default timeout,
+ // two ticks can overlap on a slow pool — one refresh in flight at a time.
+ let refreshTickInFlight = false;
const interval = setInterval(() => {
+ if (refreshTickInFlight) return;
+ refreshTickInFlight = true;
void (async () => {
try {
// v0.42.x (#1794, V1): the refresh IS the heartbeat. handle.refresh()
@@ -896,10 +907,23 @@ export async function withRefreshingLock<T>(
// health, and we do NOT clearInterval on a transient failure: a blip
// self-heals on the next tick; the TTL is the backstop if the pool stays
// genuinely dead (at which point a steal is correct).
- const timeout = new Promise<never>((_, reject) =>
- setTimeout(() => reject(new Error('refresh_timeout')), heartbeatTimeoutMs)
- );
- const stillOwned = await Promise.race([handle.refresh(), timeout]);
+ // issue #6: abort the per-tick signal when the timeout wins so the
+ // losing UPDATE is cancelled (slot released), not orphaned on the
+ // direct pool for its full server-side duration.
+ const tickAbort = new AbortController();
+ let timeoutTimer: ReturnType<typeof setTimeout> | null = null;
+ const timeout = new Promise<never>((_, reject) => {
+ timeoutTimer = setTimeout(() => {
+ tickAbort.abort();
+ reject(new Error('refresh_timeout'));
+ }, heartbeatTimeoutMs);
+ });
+ let stillOwned: boolean;
+ try {
+ stillOwned = await Promise.race([handle.refresh({ signal: tickAbort.signal }), timeout]);
+ } finally {
+ if (timeoutTimer != null) clearTimeout(timeoutTimer);
+ }
if (stillOwned === false) {
// W0 (D5.10): the fenced refresh matched 0 rows — the lock was
// stolen or force-cleared. Further refreshes are pointless (and a
@@ -918,6 +942,8 @@ export async function withRefreshingLock<T>(
const msg = err instanceof Error ? err.message : String(err);
process.stderr.write(`[lock-refresh] ${lockId}: ${msg}; will retry next tick\n`);
healthOk = false;
+ } finally {
+ refreshTickInFlight = false;
}
})();
}, refreshIntervalMs);
diff --git a/src/core/db.ts b/src/core/db.ts
index 45aec00a46..290ad721d0 100644
--- a/src/core/db.ts
+++ b/src/core/db.ts
@@ -115,6 +115,55 @@ export function resolvePoolSize(explicit?: number): number {
return DEFAULT_POOL_SIZE_FALLBACK;
}
+let warnedBadMaxLifetime = false;
+/** Test-only: reset the warn-once latch. */
+export function _resetMaxLifetimeWarningForTests(): void {
+ warnedBadMaxLifetime = false;
+}
+
+/**
+ * Client-pool connection max lifetime for every postgres() call site
+ * (module singleton, engine instance pool, ConnectionManager read + direct
+ * pools).
+ *
+ * postgres.js already defaults to `60 * (30 + Math.random() * 30)` — and
+ * critically that built-in default is a FUNCTION, re-evaluated PER
+ * CONNECTION (connection.js: `typeof seconds === 'function' ? seconds() :
+ * seconds`), so each connection gets its own 30–60min deadline. A
+ * pre-evaluated number would make every connection in a pool share ONE
+ * recycle deadline — a warm-up burst then reconnects simultaneously
+ * (data-migration specialist finding). The default here is therefore the
+ * same per-connection jitter function; only the env override returns a
+ * fixed number (the explicit escape hatch):
+ *
+ * GBRAIN_POOL_MAX_LIFETIME_S=900 # recycle after 15 min
+ * GBRAIN_POOL_MAX_LIFETIME_S=0 # disable recycling entirely
+ *
+ * max_lifetime only recycles connections as they are RETURNED to the pool;
+ * it cannot reclaim a leaked checkout — this is explicitness + a knob, not
+ * a starvation fix. Invalid values warn once on stderr and fall back to the
+ * default. The env param is injectable so tests never mutate process.env.
+ */
+export function resolveMaxLifetimeSeconds(
+ env: Record<string, string | undefined> = process.env,
+): number | null | (() => number) {
+ const raw = env.GBRAIN_POOL_MAX_LIFETIME_S;
+ if (raw !== undefined && raw !== '') {
+ const parsed = Number(raw);
+ if (Number.isFinite(parsed) && Number.isInteger(parsed) && parsed >= 0) {
+ return parsed === 0 ? null : parsed;
+ }
+ if (!warnedBadMaxLifetime) {
+ warnedBadMaxLifetime = true;
+ process.stderr.write(
+ `[gbrain] Ignoring invalid GBRAIN_POOL_MAX_LIFETIME_S=${JSON.stringify(raw)} (want a non-negative integer of seconds; 0 disables); using the jittered 30-60min default\n`,
+ );
+ }
+ }
+ // Per-connection jitter, matching the postgres.js built-in default shape.
+ return () => Math.floor(60 * (30 + Math.random() * 30));
+}
+
/**
* Session-level GUCs applied to every new backend connection. Prevents
* orphan pgbouncer sessions from holding locks or running queries
@@ -240,6 +289,8 @@ export async function connect(config: EngineConfig): Promise<boolean> {
max: resolvePoolSize(),
idle_timeout: 20,
connect_timeout: 10,
+ // Explicit (matches the postgres.js implicit default; GBRAIN_POOL_MAX_LIFETIME_S overrides).
+ max_lifetime: resolveMaxLifetimeSeconds(),
types: {
// Register pgvector type
bigint: postgres.BigInt,
diff --git a/src/core/minions/child-job-runner.ts b/src/core/minions/child-job-runner.ts
new file mode 100644
index 0000000000..3c9526b97f
--- /dev/null
+++ b/src/core/minions/child-job-runner.ts
@@ -0,0 +1,309 @@
+/**
+ * Parent-side child-process runner for per-job isolation (issue #5).
+ *
+ * `runJobInChild` is the one-line seam executeJob swaps in for
+ * `handler(context)` when isolation is on. The parent keeps claim, lock
+ * renewal and ALL result recording; this module owns spawn → signal → reap →
+ * decode:
+ *
+ * spawn — detached (own process group; group signals reach handler
+ * grandchildren even under tini), tini-wrapped when available,
+ * stdio ['ignore','inherit','inherit'] so handler logs stream
+ * to the operator; results travel by outcome file, never stdout.
+ * signal — per-job abort (timeout / cancel / lock-lost /
+ * lock-renewal-failed) → group SIGTERM now, group SIGKILL at
+ * +CHILD_KILL_GRACE_MS (25s — inside the worker's 30s
+ * force-evict window, which stays as an untouched backstop).
+ * Worker shutdown → same SIGTERM (the child's own handler fires
+ * ctx.shutdownSignal, giving handlers the drain window to
+ * finish AND write their outcome) with the SIGKILL backstop.
+ * classify — outcome file presence rules (job-isolation.ts). No file:
+ * per-job abort → generic throw (executeJob's catch reads
+ * abort.signal.reason, so infra aborts still burn no attempt);
+ * worker shutdown → ChildWorkerShutdownError (released, NO
+ * attempt burned — a routine deploy must not burn attempts;
+ * codex-2 #7); otherwise a crash (attempt burned, correct).
+ * Pre-exec spawn failure → ChildSpawnInfraError (released, no
+ * attempt burned: one bad CLI path must not dead-letter a
+ * queue; the CLI layer also fail-fast validates at startup).
+ */
+
+import { spawn, type ChildProcess } from 'node:child_process';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { buildSpawnInvocation } from './spawn-helpers.ts';
+import {
+ UnrecoverableError,
+ ABORT_REASON_TIMEOUT,
+ ABORT_REASON_LOCK_LOST,
+ ABORT_REASON_LOCK_RENEWAL_FAILED,
+} from './types.ts';
+import {
+ JOB_CHILD_EXIT_USAGE,
+ JOB_CHILD_EXIT_NOT_CLAIMED,
+} from './worker-exit-codes.ts';
+import {
+ CHILD_ENV,
+ CHILD_KILL_GRACE_MS,
+ CHILD_READ_POOL_MAX,
+ buildChildArgs,
+ decodeChildOutcomeFileAsync,
+ killProcessGroup,
+ reconstructHandlerError,
+ unrefTimer,
+ type ChildCliInvocation,
+} from './job-isolation.ts';
+
+/** Pre-exec spawn failure — infrastructure, not a job defect. executeJob
+ * releases the job with no attempt burned (stall sweeper requeues). */
+export class ChildSpawnInfraError extends Error {
+ constructor(message: string) {
+ super(message);
+ this.name = 'ChildSpawnInfraError';
+ }
+}
+
+/** Child terminated by worker shutdown before it could report. Released with
+ * no attempt burned — routine deploys must not burn attempts (codex-2 #7). */
+export class ChildWorkerShutdownError extends Error {
+ constructor(message: string) {
+ super(message);
+ this.name = 'ChildWorkerShutdownError';
+ }
+}
+
+/** Child found the job reclaimed/cancelled (exit 14) — provably owned
+ * elsewhere. The worker releases without failJob (the fenced failJob would
+ * no-op anyway); definitely not an attempt against THIS claim. */
+export class ChildNotClaimedError extends Error {
+ constructor(message: string) {
+ super(message);
+ this.name = 'ChildNotClaimedError';
+ }
+}
+
+/** Per-job abort reasons that mean THE JOB was targeted (timeout / lock
+ * loss) rather than the worker winding down. gracefulShutdown('watchdog')
+ * aborts BOTH the shutdown signal and every per-job signal — the shutdown
+ * classification must win for those (adversarial-review P3: the watchdog
+ * drain otherwise burns an attempt on innocent isolated jobs). Built from
+ * the shared literals in types.ts so a rename at an abort site cannot
+ * silently flip child classification (maintainability review — the
+ * never-produced 'cancel'/'cancelled' entries were dropped: cancellation
+ * surfaces as lock-lost via the fenced renewLock). */
+const PER_JOB_ABORT_REASONS = new Set<string>([
+ ABORT_REASON_TIMEOUT,
+ ABORT_REASON_LOCK_LOST,
+ ABORT_REASON_LOCK_RENEWAL_FAILED,
+]);
+
+export interface RunJobInChildOpts {
+ jobId: number;
+ jobName: string;
+ lockToken: string;
+ /** Per-job abort (timeout / cancel / lock-lost / lock-renewal-failed). */
+ abortSignal: AbortSignal;
+ /** Worker-process SIGTERM/SIGINT. */
+ shutdownSignal: AbortSignal;
+ /** Resolved once at worker startup (fail-fast); how to invoke the CLI. */
+ invocation: ChildCliInvocation;
+ /** tini path ('' when absent — direct spawn, same degradation as the supervisor). */
+ tiniPath: string;
+ /** Injectable for tests. Default CHILD_KILL_GRACE_MS. */
+ killGraceMs?: number;
+ /** Injectable base env for tests. Default process.env. */
+ env?: Record<string, string | undefined>;
+}
+
+interface ChildExit {
+ code: number | null;
+ signal: NodeJS.Signals | null;
+ spawnErr?: Error;
+}
+
+/**
+ * Run one claimed job in a child process. Resolves with the handler result
+ * (parent then runs the normal completeJob path); throws reconstructed
+ * handler errors / classification errors (parent's existing catch handles
+ * them verbatim).
+ */
+export async function runJobInChild(opts: RunJobInChildOpts): Promise<unknown> {
+ const dir = mkdtempSync(join(tmpdir(), `gbrain-job-${opts.jobId}-`));
+ const resultPath = join(dir, 'outcome.json');
+ const graceMs = opts.killGraceMs ?? CHILD_KILL_GRACE_MS;
+ const base = opts.env ?? process.env;
+
+ // Bound the child's pools: sockets die with the process (the isolation
+ // win), but per-child footprint must stay small — read pool <= 3, direct
+ // pool 1 (a child runs no claim/renewal heartbeats; codex-2 #6). An
+ // operator's own GBRAIN_POOL_SIZE is respected when STRICTER than the
+ // default (their pooler MaxClients tuning must not be silently raised);
+ // GBRAIN_JOB_CHILD_POOL_SIZE, when valid, is the explicit per-child knob
+ // and wins outright. Invalid values fall through to the default.
+ const parsePoolSize = (v: string | undefined): number | null => {
+ if (v === undefined || v === '') return null;
+ const n = parseInt(v, 10);
+ return Number.isInteger(n) && n > 0 ? n : null;
+ };
+ const childOverride = parsePoolSize(base[CHILD_ENV.childPoolSize]);
+ const userPool = parsePoolSize(base.GBRAIN_POOL_SIZE);
+ const childPoolSize = childOverride ?? Math.min(userPool ?? CHILD_READ_POOL_MAX, CHILD_READ_POOL_MAX);
+
+ const childEnv: Record<string, string | undefined> = {
+ ...base,
+ [CHILD_ENV.lockToken]: opts.lockToken,
+ [CHILD_ENV.resultPath]: resultPath,
+ [CHILD_ENV.isChild]: '1',
+ [CHILD_ENV.parentPid]: String(process.pid),
+ GBRAIN_POOL_SIZE: String(childPoolSize),
+ GBRAIN_DIRECT_POOL_SIZE: '1',
+ };
+
+ const inv = buildSpawnInvocation(opts.tiniPath, opts.invocation.cmd, [
+ ...opts.invocation.argsPrefix,
+ ...buildChildArgs(opts.jobId),
+ ]);
+
+ let child: ChildProcess;
+ try {
+ child = spawn(inv.cmd, inv.args, {
+ stdio: ['ignore', 'inherit', 'inherit'],
+ env: childEnv as NodeJS.ProcessEnv,
+ detached: true,
+ });
+ } catch (e) {
+ rmSync(dir, { recursive: true, force: true });
+ const msg = e instanceof Error ? e.message : String(e);
+ throw new ChildSpawnInfraError(`job child spawn failed (${inv.cmd}): ${msg}`);
+ }
+
+ let killTimer: ReturnType<typeof setTimeout> | null = null;
+ let termed = false;
+ const terminate = (): void => {
+ if (termed) return;
+ termed = true;
+ if (child.pid != null) {
+ killProcessGroup(child.pid, 'SIGTERM');
+ killTimer = setTimeout(() => {
+ // Loud on failure (red-team finding): the /bin/kill fallback is the
+ // NORMAL delivery path in Bun-compiled binaries, and a container
+ // without /bin/kill (distroless) would otherwise silently void the
+ // SIGKILL guarantee while the child runs to completion and the job
+ // gets requeued elsewhere (duplicate side effects).
+ if (child.pid != null && child.exitCode == null && child.signalCode == null) {
+ const delivered = killProcessGroup(child.pid, 'SIGKILL');
+ if (!delivered) {
+ console.error(
+ `[isolation] job ${opts.jobId} (${opts.jobName}): group SIGKILL was NOT delivered ` +
+ `to pid ${child.pid} (platform=${process.platform}; is /bin/kill present?). ` +
+ `The child may still be running — the SIGKILL guarantee is degraded on this host.`,
+ );
+ }
+ }
+ }, graceMs);
+ unrefTimer(killTimer);
+ }
+ };
+ const onAbort = (): void => terminate();
+ const onShutdown = (): void => terminate();
+ if (opts.abortSignal.aborted) onAbort();
+ else opts.abortSignal.addEventListener('abort', onAbort, { once: true });
+ if (opts.shutdownSignal.aborted) onShutdown();
+ else opts.shutdownSignal.addEventListener('abort', onShutdown, { once: true });
+
+ console.log(
+ `[isolation] job ${opts.jobId} (${opts.jobName}) child pid ${child.pid ?? '?'} spawned`,
+ );
+
+ try {
+ const exit = await new Promise<ChildExit>((resolve) => {
+ child.once('error', (e) => resolve({ code: null, signal: null, spawnErr: e }));
+ child.once('exit', (code, signal) => resolve({ code, signal }));
+ });
+
+ if (exit.spawnErr && child.pid == null) {
+ throw new ChildSpawnInfraError(
+ `job child spawn failed (${inv.cmd}): ${exit.spawnErr.message}`,
+ );
+ }
+
+ console.log(
+ `[isolation] job ${opts.jobId} (${opts.jobName}) child pid ${child.pid ?? '?'} ` +
+ `exited code=${exit.code ?? 'null'} signal=${exit.signal ?? 'null'}`,
+ );
+
+ const abortReason = opts.abortSignal.aborted
+ ? (opts.abortSignal.reason instanceof Error
+ ? opts.abortSignal.reason.message
+ : String(opts.abortSignal.reason ?? 'aborted'))
+ : null;
+ // Shutdown classification wins UNLESS the per-job abort names a
+ // job-targeted reason. gracefulShutdown('watchdog') aborts BOTH signals —
+ // checking abortSignal first would shadow the no-burn shutdown release
+ // and dead-letter innocent isolated jobs (adversarial-review P3).
+ const isShutdownClass =
+ opts.shutdownSignal.aborted &&
+ (abortReason === null || !PER_JOB_ABORT_REASONS.has(abortReason));
+
+ let outcome: Awaited<ReturnType<typeof decodeChildOutcomeFileAsync>>;
+ try {
+ // Async decode: a large-but-allowed outcome must not block the worker
+ // event loop that runs lock-renewal ticks (performance review).
+ outcome = await decodeChildOutcomeFileAsync(resultPath);
+ } catch (decodeErr) {
+ // No usable outcome. Classify by WHY the child died.
+ if (decodeErr instanceof UnrecoverableError) throw decodeErr; // oversize cap — dead on attempt 1
+ if (isShutdownClass) {
+ throw new ChildWorkerShutdownError(
+ `job child terminated by worker shutdown before reporting (exit code=${exit.code} signal=${exit.signal})`,
+ );
+ }
+ if (opts.abortSignal.aborted) {
+ // executeJob's catch reads abort.signal.reason first, so infra
+ // reasons (lock-renewal-failed / lock-lost) still burn no attempt
+ // and timeout/cancel keep their existing semantics.
+ throw new Error(
+ `job child terminated after abort without an outcome (exit code=${exit.code} signal=${exit.signal})`,
+ );
+ }
+ // Bootstrap failures carry reserved exit codes and are NOT handler
+ // defects: 13 = usage/config (ops misconfiguration — release like a
+ // spawn failure), 14 = job reclaimed before the handler ran (owned
+ // elsewhere — release; the fenced failJob would no-op regardless).
+ if (exit.code === JOB_CHILD_EXIT_USAGE) {
+ throw new ChildSpawnInfraError(
+ `job child bootstrap failed (exit ${exit.code}) — check the worker's child CLI/engine configuration`,
+ );
+ }
+ if (exit.code === JOB_CHILD_EXIT_NOT_CLAIMED) {
+ throw new ChildNotClaimedError(
+ `job child found the claim gone (exit ${exit.code}) — reclaimed or cancelled before the handler ran`,
+ );
+ }
+ throw new Error(
+ `${decodeErr instanceof Error ? decodeErr.message : String(decodeErr)} ` +
+ `(exit code=${exit.code} signal=${exit.signal})`,
+ );
+ }
+
+ if (outcome.outcome === 'success') return outcome.result;
+ // A handler-error outcome DURING worker shutdown is presumed
+ // shutdown-induced (cooperative handlers that honor shutdownSignal bail
+ // and report an error): release with no attempt burned rather than
+ // punishing exactly the well-behaved handlers on every deploy
+ // (adversarial-review P2). Worst case a genuinely-failing job that
+ // coincided with a deploy gets one free retry — bounded and benign.
+ if (isShutdownClass) {
+ throw new ChildWorkerShutdownError(
+ `job child reported an error during worker shutdown (${outcome.message}) — released, not burned`,
+ );
+ }
+ throw reconstructHandlerError(outcome);
+ } finally {
+ if (killTimer != null) clearTimeout(killTimer);
+ opts.abortSignal.removeEventListener('abort', onAbort);
+ opts.shutdownSignal.removeEventListener('abort', onShutdown);
+ rmSync(dir, { recursive: true, force: true });
+ }
+}
diff --git a/src/core/minions/db-probe.ts b/src/core/minions/db-probe.ts
new file mode 100644
index 0000000000..ac9faf585f
--- /dev/null
+++ b/src/core/minions/db-probe.ts
@@ -0,0 +1,165 @@
+/**
+ * DB liveness probe with pool-starvation disambiguation (issue #6).
+ *
+ * The incident this exists for: a worker's read pool was exhausted by
+ * checked-out-and-abandoned queries, the `SELECT 1` probe couldn't get a slot
+ * within its budget, and the worker exited with "DB unreachable" — while the
+ * server sat at ~10% of max_connections. Operators spent hours on the wrong
+ * layer (evaluating an instance upgrade that would have changed nothing).
+ *
+ * Mechanism: probe the read pool; on failure, probe the DIRECT session lane
+ * (when dual-pool is active). Direct success proves the server is reachable
+ * and narrows the fault to the transaction-pooler path — client pool
+ * exhaustion or a pooler-layer fault; the probe deliberately does NOT claim
+ * to distinguish those two (codex-2 #2). Either way the operator is pointed
+ * away from "the database is down / too small".
+ *
+ * Verdicts:
+ * pool_starved — read probe failed, direct probe succeeded.
+ * server_unreachable — read AND direct probes failed.
+ * unknown — read probe failed, no direct lane to disambiguate.
+ *
+ * Both probes carry an AbortSignal that fires when their deadline wins, so a
+ * hung probe is cancelled (slot released), never abandoned — same contract
+ * as the lock-renewal tick.
+ *
+ * Pure/hermetic: every effect is injected via `DbProbeDeps`; the worker's
+ * adapter is a thin closure. (lock-renewal-tick.ts is the pattern.)
+ */
+
+import type { PoolGaugeSnapshot } from '../pool-gauge.ts';
+
+export type ProbeVerdict = 'pool_starved' | 'server_unreachable' | 'unknown';
+
+/** Default budget for the direct-lane disambiguation probe. */
+export const DIRECT_PROBE_TIMEOUT_MS = 3_000;
+
+export interface PoolDiagnostics {
+ /** Gauge counts — a tracked SUBSET (see pool-gauge.ts honesty contract). */
+ tracked: PoolGaugeSnapshot;
+ /** Read-pool max, when the engine can report it; null otherwise. */
+ poolMax: number | null;
+}
+
+export interface DbProbeDeps {
+ /** SELECT 1 on the read pool; MUST honor the signal (cancellation). */
+ probeRead: (signal: AbortSignal) => Promise<void>;
+ /**
+ * SELECT 1 on the direct session lane. Present ONLY when dual-pool is
+ * genuinely active — an executeRawDirect that would silently fall back to
+ * the read pool (kill-switch collapse) must NOT be passed here, or the
+ * "disambiguation" would probe the same starved pool twice.
+ */
+ probeDirect?: (signal: AbortSignal) => Promise<void>;
+ /** Optional gauge snapshot for supporting detail. Fail-open: may be absent or throw. */
+ getDiagnostics?: () => PoolDiagnostics | null;
+ /** Read-probe budget (worker default 10s). */
+ timeoutMs: number;
+ /** Direct-probe budget (default 3s). */
+ directTimeoutMs: number;
+}
+
+export type DbProbeResult =
+ | { ok: true }
+ | { ok: false; verdict: ProbeVerdict; detail: string };
+
+/**
+ * Narrow, shared view of the engine's ConnectionManager for routing-aware
+ * callers (the worker's probe adapter, jobs.ts's single-pool startup
+ * warning). One typed accessor instead of hand-rolled structural casts that
+ * drift independently from the real class (maintainability review).
+ */
+export interface EngineConnectionRouting {
+ isDualPoolActive?: () => boolean;
+ describeMode?: () => { kill_switch_active?: boolean; direct_pool_size?: number };
+}
+
+export function getConnectionRouting(engine: unknown): EngineConnectionRouting | null {
+ const cm = (engine as { connectionManager?: EngineConnectionRouting }).connectionManager;
+ return cm ?? null;
+}
+
+async function withDeadline(
+ run: (signal: AbortSignal) => Promise<void>,
+ ms: number,
+ label: string,
+): Promise<void> {
+ const ac = new AbortController();
+ let timer: ReturnType<typeof setTimeout> | null = null;
+ try {
+ await Promise.race([
+ run(ac.signal),
+ new Promise<never>((_, reject) => {
+ timer = setTimeout(() => {
+ ac.abort();
+ reject(new Error(`${label} timeout after ${ms}ms`));
+ }, ms);
+ }),
+ ]);
+ } finally {
+ if (timer != null) clearTimeout(timer);
+ }
+}
+
+/** Render gauge detail. Never throws; empty string when unavailable. */
+function renderDiagnostics(getDiagnostics?: () => PoolDiagnostics | null): string {
+ try {
+ const diag = getDiagnostics?.();
+ if (!diag) return '';
+ const { tracked, poolMax } = diag;
+ const maxNote = poolMax != null ? ` (read pool max ${poolMax})` : '';
+ const base =
+ ` gbrain-tracked in flight (subset — template-path queries untracked):` +
+ ` raw=${tracked.raw}, direct=${tracked.direct}, reserved=${tracked.reserved}, tx=${tracked.tx}${maxNote}.`;
+ const total = tracked.raw + tracked.direct + tracked.reserved + tracked.tx;
+ if (total === 0) {
+ return (
+ base +
+ ' Tracked subset shows 0 in flight — the saturation is in untracked' +
+ ' template-query traffic; see docs/guides/queue-operations-runbook.md.'
+ );
+ }
+ return base;
+ } catch {
+ return '';
+ }
+}
+
+export async function runDbProbe(deps: DbProbeDeps): Promise<DbProbeResult> {
+ let readErrMsg: string;
+ try {
+ await withDeadline(deps.probeRead, deps.timeoutMs, 'probe');
+ return { ok: true };
+ } catch (e) {
+ readErrMsg = e instanceof Error ? e.message : String(e);
+ }
+
+ if (!deps.probeDirect) {
+ return {
+ ok: false,
+ verdict: 'unknown',
+ detail: `${readErrMsg}; no direct lane available to disambiguate pool starvation from a dead server`,
+ };
+ }
+
+ const t0 = Date.now();
+ try {
+ await withDeadline(deps.probeDirect, deps.directTimeoutMs, 'direct probe');
+ return {
+ ok: false,
+ verdict: 'pool_starved',
+ detail:
+ `read-pool probe failed (${readErrMsg}) but the direct-lane probe succeeded in ${Date.now() - t0}ms — ` +
+ `the server IS reachable; the fault is in the transaction-pooler path ` +
+ `(client pool exhaustion or a pooler-layer fault).` +
+ renderDiagnostics(deps.getDiagnostics),
+ };
+ } catch (e) {
+ const directErrMsg = e instanceof Error ? e.message : String(e);
+ return {
+ ok: false,
+ verdict: 'server_unreachable',
+ detail: `read probe: ${readErrMsg}; direct probe: ${directErrMsg} — server/network unreachable`,
+ };
+ }
+}
diff --git a/src/core/minions/job-context.ts b/src/core/minions/job-context.ts
new file mode 100644
index 0000000000..8f982a6291
--- /dev/null
+++ b/src/core/minions/job-context.ts
@@ -0,0 +1,61 @@
+/**
+ * Shared MinionJobContext builder (issue #5 — per-job process isolation).
+ *
+ * Extracted verbatim from MinionWorker.executeJob so the same DB-backed
+ * context wiring serves BOTH execution modes:
+ *
+ * inline — the worker builds it against its own engine/queue;
+ * process — `gbrain jobs run-child` builds it against the CHILD's engine
+ * (child-owns-engine design: every write below is token-fenced,
+ * so a reclaimed job's orphan child degrades to a no-op writer).
+ *
+ * Behavioral no-op for the inline path.
+ */
+
+import type { BrainEngine } from '../engine.ts';
+import type { MinionQueue } from './queue.ts';
+import type { MinionJob, MinionJobContext, TokenUpdate } from './types.ts';
+
+export function buildJobContext(
+ engine: BrainEngine,
+ queue: MinionQueue,
+ job: MinionJob,
+ lockToken: string,
+ signal: AbortSignal,
+ shutdownSignal: AbortSignal,
+): MinionJobContext {
+ return {
+ id: job.id,
+ name: job.name,
+ data: job.data,
+ attempts_made: job.attempts_made,
+ signal,
+ deadlineAtMs: job.timeout_at != null ? job.timeout_at.getTime() : null,
+ shutdownSignal,
+ updateProgress: async (progress: unknown) => {
+ await queue.updateProgress(job.id, lockToken, progress);
+ },
+ updateTokens: async (tokens: TokenUpdate) => {
+ await queue.updateTokens(job.id, lockToken, tokens);
+ },
+ log: async (message: string | Record<string, unknown>) => {
+ const value = typeof message === 'string' ? message : JSON.stringify(message);
+ await engine.executeRaw(
+ `UPDATE minion_jobs SET stacktrace = COALESCE(stacktrace, '[]'::jsonb) || to_jsonb($1::text),
+ updated_at = now()
+ WHERE id = $2 AND status = 'active' AND lock_token = $3`,
+ [value, job.id, lockToken]
+ );
+ },
+ isActive: async () => {
+ const rows = await engine.executeRaw<{ id: number }>(
+ `SELECT id FROM minion_jobs WHERE id = $1 AND status = 'active' AND lock_token = $2`,
+ [job.id, lockToken]
+ );
+ return rows.length > 0;
+ },
+ readInbox: async () => {
+ return queue.readInbox(job.id, lockToken);
+ },
+ };
+}
diff --git a/src/core/minions/job-isolation.ts b/src/core/minions/job-isolation.ts
new file mode 100644
index 0000000000..2160649963
--- /dev/null
+++ b/src/core/minions/job-isolation.ts
@@ -0,0 +1,306 @@
+/**
+ * Per-job process isolation protocol (issue #5).
+ *
+ * The worker (parent) keeps claim / lock-renewal / completeJob / failJob;
+ * `gbrain jobs run-child` (child) owns handler execution with its own small
+ * engine pool. This module is the protocol between them:
+ *
+ * payload in — job id via argv (`jobs run-child --job-id N`, ps-visible
+ * for ops); lock token via GBRAIN_JOB_LOCK_TOKEN env (off
+ * argv — not a secret, it's a fencing token, but no reason
+ * to put it in `ps` output); result path via
+ * GBRAIN_JOB_RESULT_PATH.
+ * result out — ONE JSON file, written atomically (tmp + rename), decoded
+ * by the parent. stdout/stderr stay inherited for handler
+ * logs (handlers print freely — no sentinel parsing), and
+ * node-IPC is deliberately avoided (zero precedent in this
+ * codebase; fd inheritance through a tini wrapper is
+ * unproven here).
+ * termination — killProcessGroup(): children are spawned detached (own
+ * process group) because SIGKILL on the tini pid alone kills
+ * tini, NOT the handler grandchild (tini cannot forward
+ * SIGKILL). Bun rejects negative pids in process.kill()
+ * (oven-sh/bun#15791) and gbrain ships as a Bun-compiled
+ * binary, so the group signal falls back to POSIX
+ * /bin/kill when needed.
+ *
+ * Handler-error semantics survive the boundary: the child encodes the two
+ * error classes executeJob branches on (UnrecoverableError → 'dead',
+ * RateLeaseUnavailableError → lease release, no attempt burned) and
+ * `reconstructHandlerError` rebuilds real instances parent-side so the
+ * existing `instanceof` branches work verbatim. Everything else degrades to
+ * a generic Error → the normal delayed/dead backoff path, same as inline.
+ */
+
+import { readFileSync, writeFileSync, renameSync, statSync } from 'node:fs';
+import { readFile, stat } from 'node:fs/promises';
+import { spawnSync } from 'node:child_process';
+import { UnrecoverableError } from './types.ts';
+import { RateLeaseUnavailableError } from './handlers/subagent.ts';
+
+/** Grace between group-SIGTERM and group-SIGKILL on abort. Deliberately
+ * inside the worker's 30s force-evict window so the evict path stays a
+ * nearly-unreachable backstop. */
+export const CHILD_KILL_GRACE_MS = 25_000;
+
+/** Decode cap for the child's outcome file. Results already round-trip
+ * through the completeJob JSONB column in inline mode, so anything near
+ * this cap is pathological; oversize throws UnrecoverableError (loud dead
+ * on attempt 1 — deterministic failure, retries would fail identically). */
+export const CHILD_OUTCOME_MAX_BYTES = 32 * 1024 * 1024;
+
+/** Default read-pool cap for isolation children. Referenced by the
+ * --job-isolation help copy ("~4 pooler client connections" = this + the
+ * direct pool of 1) and the minions-deployment.md budget math. */
+export const CHILD_READ_POOL_MAX = 3;
+
+/** Bun-compat timer unref (plain cast copy-pasted thrice before this helper). */
+export function unrefTimer(t: unknown): void {
+ (t as { unref?: () => void }).unref?.();
+}
+
+/** Env vars of the parent↔child contract. Spelled once here. */
+export const CHILD_ENV = {
+ lockToken: 'GBRAIN_JOB_LOCK_TOKEN',
+ resultPath: 'GBRAIN_JOB_RESULT_PATH',
+ isChild: 'GBRAIN_JOB_CHILD',
+ parentPid: 'GBRAIN_JOB_PARENT_PID',
+ childCliOverride: 'GBRAIN_JOB_CHILD_CLI',
+ childPoolSize: 'GBRAIN_JOB_CHILD_POOL_SIZE',
+} as const;
+
+export type ChildErrorKind = 'unrecoverable' | 'rate_lease' | 'generic';
+
+export type ChildOutcome =
+ | { outcome: 'success'; result: unknown }
+ | {
+ outcome: 'error';
+ errorKind: ChildErrorKind;
+ message: string;
+ stack?: string;
+ lease?: { key: string; active: number; max: number };
+ };
+
+/** Child-side: classify a handler throw into the wire shape. */
+export function encodeHandlerError(err: unknown): ChildOutcome {
+ if (err instanceof RateLeaseUnavailableError) {
+ return {
+ outcome: 'error',
+ errorKind: 'rate_lease',
+ message: err.message,
+ lease: { key: err.key, active: err.active, max: err.max },
+ };
+ }
+ if (err instanceof UnrecoverableError) {
+ return {
+ outcome: 'error',
+ errorKind: 'unrecoverable',
+ message: err.message,
+ ...(err.stack ? { stack: err.stack } : {}),
+ };
+ }
+ const message = err instanceof Error ? err.message : String(err);
+ const stack = err instanceof Error ? err.stack : undefined;
+ return { outcome: 'error', errorKind: 'generic', message, ...(stack ? { stack } : {}) };
+}
+
+/**
+ * Parent-side: rebuild a real error instance so executeJob's existing
+ * `instanceof` branches (dead / lease-release / delayed+backoff) work
+ * verbatim. Unknown errorKind values degrade to generic (whitelist — the
+ * file is same-user-written but a malformed kind must not crash the worker).
+ */
+export function reconstructHandlerError(o: Extract<ChildOutcome, { outcome: 'error' }>): Error {
+ if (o.errorKind === 'rate_lease' && o.lease) {
+ return new RateLeaseUnavailableError(o.lease.key, o.lease.active, o.lease.max);
+ }
+ if (o.errorKind === 'unrecoverable') {
+ return new UnrecoverableError(o.message);
+ }
+ const err = new Error(o.message);
+ if (o.stack) {
+ (err as Error & { childStack?: string }).childStack = o.stack;
+ }
+ return err;
+}
+
+/** Child-side: atomic outcome write (tmp + rename on the same filesystem). */
+export function writeChildOutcomeFile(path: string, outcome: ChildOutcome): void {
+ const tmp = `${path}.tmp`;
+ writeFileSync(tmp, JSON.stringify(outcome), 'utf8');
+ renameSync(tmp, path);
+}
+
+/**
+ * Pure outcome parser (shared by the sync and async decode paths). Throws:
+ * - UnrecoverableError when the file exceeds `maxBytes` (deterministic —
+ * dead on attempt 1, no silent truncation);
+ * - generic Error for malformed/unrecognized content (byte count only in
+ * the message, NEVER file content — handler output may carry secrets).
+ * The `lease` payload is shape-validated (security review): a corrupt file
+ * must degrade to 'generic', not inject undefined fields into the parent's
+ * lease-release accounting.
+ */
+export function parseChildOutcome(raw: string, size: number, maxBytes = CHILD_OUTCOME_MAX_BYTES): ChildOutcome {
+ if (size > maxBytes) {
+ throw new UnrecoverableError(
+ `job child result exceeds the ${Math.floor(maxBytes / (1024 * 1024))}MiB outcome cap (${size} bytes); ` +
+ `retries would fail identically — return a smaller result or persist large artifacts elsewhere`,
+ );
+ }
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(raw);
+ } catch {
+ throw new Error(`job child outcome file is not valid JSON (${size} bytes)`);
+ }
+ const o = parsed as Partial<ChildOutcome> | null;
+ if (o && o.outcome === 'success') return { outcome: 'success', result: (o as { result?: unknown }).result };
+ if (o && o.outcome === 'error' && typeof (o as { message?: unknown }).message === 'string') {
+ const kind = (o as { errorKind?: unknown }).errorKind;
+ const rawLease = (o as { lease?: unknown }).lease as
+ | { key?: unknown; active?: unknown; max?: unknown }
+ | undefined;
+ const leaseValid =
+ rawLease != null &&
+ typeof rawLease.key === 'string' &&
+ Number.isFinite(rawLease.active as number) &&
+ Number.isFinite(rawLease.max as number);
+ // rate_lease without a valid lease payload degrades to generic — same
+ // policy as the errorKind whitelist.
+ const errorKind =
+ kind === 'unrecoverable' ? 'unrecoverable'
+ : kind === 'rate_lease' && leaseValid ? 'rate_lease'
+ : 'generic';
+ return {
+ outcome: 'error',
+ errorKind,
+ message: (o as { message: string }).message,
+ ...((o as { stack?: unknown }).stack && typeof (o as { stack?: unknown }).stack === 'string'
+ ? { stack: (o as { stack: string }).stack }
+ : {}),
+ ...(errorKind === 'rate_lease' && leaseValid
+ ? { lease: { key: rawLease.key as string, active: rawLease.active as number, max: rawLease.max as number } }
+ : {}),
+ };
+ }
+ throw new Error(`job child outcome file has an unrecognized shape (${size} bytes)`);
+}
+
+const MISSING_OUTCOME_MESSAGE =
+ 'job child exited without writing its outcome file (crash, OOM, or kill before completion)';
+
+/** Sync decode (tests + non-hot-path callers). */
+export function decodeChildOutcomeFile(path: string, maxBytes = CHILD_OUTCOME_MAX_BYTES): ChildOutcome {
+ let size: number;
+ try {
+ size = statSync(path).size;
+ } catch {
+ throw new Error(MISSING_OUTCOME_MESSAGE);
+ }
+ if (size > maxBytes) return parseChildOutcome('', size, maxBytes); // throws the cap error
+ return parseChildOutcome(readFileSync(path, 'utf8'), size, maxBytes);
+}
+
+/**
+ * Async decode for the WORKER's per-job path: a large-but-allowed outcome
+ * (up to 32MiB) must not block the event loop that runs lock-renewal ticks
+ * and the health-probe chain (performance review).
+ */
+export async function decodeChildOutcomeFileAsync(
+ path: string,
+ maxBytes = CHILD_OUTCOME_MAX_BYTES,
+): Promise<ChildOutcome> {
+ let size: number;
+ try {
+ size = (await stat(path)).size;
+ } catch {
+ throw new Error(MISSING_OUTCOME_MESSAGE);
+ }
+ if (size > maxBytes) return parseChildOutcome('', size, maxBytes); // throws the cap error
+ return parseChildOutcome(await readFile(path, 'utf8'), size, maxBytes);
+}
+
+/** argv for the child invocation (appended after the resolved CLI). */
+export function buildChildArgs(jobId: number): string[] {
+ return ['jobs', 'run-child', '--job-id', String(jobId)];
+}
+
+export interface ChildCliInvocation {
+ cmd: string;
+ /** Args that come BEFORE buildChildArgs() output (e.g. the cli.ts path in bun-dev). */
+ argsPrefix: string[];
+}
+
+/**
+ * Resolve how to invoke the gbrain CLI for a child process. Pure — all
+ * inputs injected:
+ *
+ * 1. GBRAIN_JOB_CHILD_CLI env override (ops/test escape hatch)
+ * 2. resolveBinary() — the compiled-binary resolver
+ * (resolveGbrainCliPath; never returns a .ts path)
+ * 3. bun-dev fallback: running from `bun src/cli.ts` → invoke
+ * `<execPath> <argv1>` so dev and tests work without a compiled binary
+ *
+ * Returns null when nothing resolves — the caller must fail fast at worker
+ * startup (one bad path must not dead-letter a queue job-by-job).
+ */
+export function resolveChildCliInvocation(
+ env: Record<string, string | undefined>,
+ execPath: string,
+ argv1: string | undefined,
+ resolveBinary: () => string | null,
+): ChildCliInvocation | null {
+ const override = env[CHILD_ENV.childCliOverride];
+ if (override && override.trim() !== '') {
+ return { cmd: override, argsPrefix: [] };
+ }
+ try {
+ const bin = resolveBinary();
+ if (bin) return { cmd: bin, argsPrefix: [] };
+ } catch {
+ // fall through to the dev fallback
+ }
+ if (argv1 && (argv1.endsWith('/cli.ts') || argv1.endsWith('\\cli.ts') || argv1 === 'cli.ts')) {
+ return { cmd: execPath, argsPrefix: [argv1] };
+ }
+ return null;
+}
+
+/**
+ * Signal an entire process GROUP.
+ *
+ * Children are spawned `detached: true` (own group) so this reaches the
+ * handler grandchildren even under a tini wrapper — SIGKILL on the tini pid
+ * alone kills tini and ORPHANS the still-running handler (tini cannot
+ * forward SIGKILL; that failure mode would silently void issue #5's
+ * headline guarantee exactly in container deployments).
+ *
+ * Bun's process.kill() rejects negative pids (oven-sh/bun#15791), so on any
+ * throw other than ESRCH we fall back to POSIX /bin/kill, which
+ * group-signals fine on darwin + linux. Returns true when the signal was
+ * delivered to a live group; false when the group is already gone (ESRCH —
+ * success for our purposes) or delivery failed.
+ */
+export function killProcessGroup(pid: number, signal: 'SIGTERM' | 'SIGKILL'): boolean {
+ if (!Number.isInteger(pid) || pid <= 1) return false;
+ try {
+ process.kill(-pid, signal);
+ return true;
+ } catch (e) {
+ const code = (e as NodeJS.ErrnoException).code;
+ if (code === 'ESRCH') return false; // group already gone
+ // RangeError on Bun (negative pid unsupported) or EPERM etc. — fall back
+ // to /bin/kill by ABSOLUTE path (a PATH-resolved binary in a kill path is
+ // an unnecessary indirection; security review). This is the NORMAL path
+ // in Bun-compiled production binaries; the sync exec is ~1-3ms and only
+ // runs on abort/shutdown, never in the claim/renewal hot loop.
+ try {
+ const sigName = signal.replace(/^SIG/, '');
+ const res = spawnSync('/bin/kill', ['-s', sigName, '--', `-${pid}`], { stdio: 'ignore' });
+ return res.status === 0;
+ } catch {
+ return false;
+ }
+ }
+}
diff --git a/src/core/minions/lock-renewal-tick.ts b/src/core/minions/lock-renewal-tick.ts
index 65e7d54b08..30b7f22e93 100644
--- a/src/core/minions/lock-renewal-tick.ts
+++ b/src/core/minions/lock-renewal-tick.ts
@@ -147,7 +147,19 @@ function warnAndFallback(name: string, raw: string, fallback: number): number {
* `runLockRenewalTick` is pure and trivially testable.
*/
export interface LockRenewalDeps {
- renewLock: (jobId: number, lockToken: string, lockDurationMs: number) => Promise<boolean>;
+ /**
+ * The optional `opts.signal` is aborted when this call loses the tick's
+ * timeout race, so the underlying UPDATE is CANCELLED (postgres.js
+ * `.cancel()` via executeRawDirect) instead of orphaned on a checked-out
+ * pool slot for its full server-side duration — the #6 starvation class.
+ * Optional-param widening keeps the legacy 3-arg test mocks compiling.
+ */
+ renewLock: (
+ jobId: number,
+ lockToken: string,
+ lockDurationMs: number,
+ opts?: { signal?: AbortSignal },
+ ) => Promise<boolean>;
audit: LockRenewalAuditSinkLike;
/** Injectable for hermetic time-based tests. Production: `Date.now`. */
now: () => number;
@@ -155,8 +167,9 @@ export interface LockRenewalDeps {
* Injectable for hermetic Promise.race tests. Production:
* `globalThis.setTimeout`. The function must return a value that
* `clearTimeout` accepts, but this seam doesn't expose clearTimeout
- * because the timeout race fires-and-forgets (the lost race is
- * harmless — at worst we have a dangling reject that no one awaits).
+ * because the timeout race fires-and-forgets. The losing renewLock is no
+ * longer merely abandoned: the timeout callback also aborts the per-call
+ * signal so the query releases its pool slot.
*/
setTimeout: (cb: () => void, ms: number) => unknown;
/**
@@ -228,11 +241,19 @@ export async function runLockRenewalTick(
if (state.cancelled()) return { kind: 'cancelled' };
let renewed: boolean;
+ // Per-call cancellation: when the timeout wins the race, abort the signal
+ // so the losing UPDATE releases its pool slot instead of holding it until
+ // the server finishes (issue #6 — an abandoned racer under a saturated
+ // pooler pinned a checked-out connection for minutes). On the win path the
+ // late-firing timer aborts an already-settled query, which runUnsafe
+ // ignores (abort listener removed in its .finally).
+ const callAbort = new AbortController();
try {
renewed = await Promise.race([
- deps.renewLock(state.jobId, state.lockToken, state.lockDurationMs),
+ deps.renewLock(state.jobId, state.lockToken, state.lockDurationMs, { signal: callAbort.signal }),
new Promise<never>((_, reject) => {
deps.setTimeout(() => {
+ callAbort.abort();
reject(new Error(`renewLock timed out after ${state.knobs.callTimeoutMs}ms`));
}, state.knobs.callTimeoutMs);
}),
diff --git a/src/core/minions/queue.ts b/src/core/minions/queue.ts
index 6d68c543d7..2c72135d59 100644
--- a/src/core/minions/queue.ts
+++ b/src/core/minions/queue.ts
@@ -1291,8 +1291,18 @@ export class MinionQueue {
return rows.length > 0;
}
- /** Renew lock (token-fenced). Returns false if token mismatch (job was reclaimed). */
- async renewLock(id: number, lockToken: string, lockDurationMs: number): Promise<boolean> {
+ /**
+ * Renew lock (token-fenced). Returns false if token mismatch (job was reclaimed).
+ * `opts.signal` cancels the in-flight UPDATE (postgres.js `.cancel()`) when the
+ * caller's timeout race gives up on it — otherwise the abandoned query holds a
+ * checked-out pool slot for its full server-side duration (issue #6).
+ */
+ async renewLock(
+ id: number,
+ lockToken: string,
+ lockDurationMs: number,
+ opts?: { signal?: AbortSignal },
+ ): Promise<boolean> {
// Direct (session-mode) pool — see claim(). The heartbeat that keeps a job
// alive for minutes cannot run on the transaction pooler without periodic
// CONNECTION_ENDED drops that look like lock-expiry and orphan the job.
@@ -1300,7 +1310,8 @@ export class MinionQueue {
`UPDATE minion_jobs SET lock_until = now() + ($1::double precision * interval '1 millisecond'), updated_at = now()
WHERE id = $2 AND lock_token = $3 AND status = 'active'
RETURNING id`,
- [lockDurationMs, id, lockToken]
+ [lockDurationMs, id, lockToken],
+ opts
);
return rows.length > 0;
}
diff --git a/src/core/minions/run-child.ts b/src/core/minions/run-child.ts
new file mode 100644
index 0000000000..41b92f422b
--- /dev/null
+++ b/src/core/minions/run-child.ts
@@ -0,0 +1,177 @@
+/**
+ * `gbrain jobs run-child` core (issue #5 — per-job process isolation).
+ *
+ * The child side of the isolation boundary. The PARENT worker owns claim,
+ * lock renewal, completeJob/failJob and all attempt accounting; this process
+ * only executes the handler and reports ONE outcome file (see
+ * job-isolation.ts for the protocol). Child-owns-engine: every ctx callback
+ * below is token-fenced, so if the job is reclaimed while we run, our writes
+ * degrade to no-ops.
+ *
+ * Deliberately runs NONE of the worker machinery: no health probe, no stall
+ * detection, no lock timer — the parent is the sole liveness owner. What it
+ * does install:
+ *
+ * - SIGTERM handler → fires shutdownSignal ONLY (inline signal-separation
+ * parity): cooperative handlers keep ctx.signal live, finish inside the
+ * drain window, and write their outcome before the parent escalates to a
+ * group SIGKILL. Handlers that watch shutdownSignal (shell) run their own
+ * SIGTERM→grace→SIGKILL cleanup.
+ * - Parent-liveness watchdog: polls `process.kill(parentPid, 0)` every 15s
+ * (a ppid check is DEAD CODE under tini — the child's ppid is tini,
+ * which outlives the worker). On parent death: abort both signals, and
+ * hard-exit after a 30s grace so an orphaned LLM-bound handler doesn't
+ * burn spend to completion. Lock expiry + the stall sweeper requeue the
+ * job on the parent's side of the world.
+ *
+ * The CLI layer (jobs.ts case 'run-child') owns engine.disconnect() and
+ * process.exit() — this module returns an exit code (engine-ownership
+ * invariant, same as MinionWorker).
+ */
+
+import type { BrainEngine } from '../engine.ts';
+import { MinionQueue } from './queue.ts';
+import type { MinionHandler } from './types.ts';
+import { buildJobContext } from './job-context.ts';
+import {
+ JOB_CHILD_EXIT_NOT_CLAIMED,
+ JOB_CHILD_EXIT_RESULT_WRITE_FAILED,
+} from './worker-exit-codes.ts';
+import { encodeHandlerError, unrefTimer, writeChildOutcomeFile } from './job-isolation.ts';
+
+export interface RunChildOpts {
+ jobId: number;
+ lockToken: string;
+ resultPath: string;
+ /** Worker pid for the liveness watchdog; 0/absent disables the watchdog. */
+ parentPid: number;
+}
+
+export interface RunChildInjectables {
+ /** Hermetic tests inject a handler map instead of registerBuiltinHandlers. */
+ resolveHandler: (name: string) => MinionHandler | undefined;
+ /** Watchdog cadence override (default 15s). */
+ parentPollMs?: number;
+ /** Orphan hard-exit grace override (default 30s). */
+ orphanGraceMs?: number;
+ /** Exit hook for tests (default process.exit for the orphan path only). */
+ hardExit?: (code: number) => void;
+}
+
+export async function runChildJobEntry(
+ engine: BrainEngine,
+ opts: RunChildOpts,
+ injectables: RunChildInjectables,
+): Promise<number> {
+ const queue = new MinionQueue(engine);
+
+ // Ground truth is the DB row, re-read here (one SELECT) rather than a
+ // serialized payload: the parent claimed it, but reclaim/cancel can race
+ // our startup. A mismatch means we must not run the handler at all.
+ const job = await queue.getJob(opts.jobId);
+ if (!job || job.status !== 'active' || job.lock_token !== opts.lockToken) {
+ process.stderr.write(
+ `[run-child] job ${opts.jobId} is not claimed by this token ` +
+ `(status=${job?.status ?? 'missing'}) — exiting without running the handler\n`,
+ );
+ return JOB_CHILD_EXIT_NOT_CLAIMED;
+ }
+
+ const handler = injectables.resolveHandler(job.name);
+ if (!handler) {
+ // Parity with inline mode, which dead-letters a missing handler
+ // immediately (failJob 'dead') — 'unrecoverable' reconstructs to
+ // UnrecoverableError parent-side, so the parent dead-letters on attempt 1
+ // instead of retrying a deterministic condition (adversarial-review P3).
+ try {
+ writeChildOutcomeFile(opts.resultPath, {
+ outcome: 'error',
+ errorKind: 'unrecoverable',
+ message: `No handler for job type '${job.name}' in the isolation child`,
+ });
+ return 0;
+ } catch {
+ return JOB_CHILD_EXIT_RESULT_WRITE_FAILED;
+ }
+ }
+
+ const abort = new AbortController();
+ const shutdown = new AbortController();
+
+ // SIGTERM = worker shutdown: fire ONLY shutdownSignal, preserving the
+ // inline signal-separation contract (worker.ts aborts only shutdownAbort on
+ // SIGTERM; per-job ctx.signal stays live so cooperative handlers finish +
+ // report inside the drain window instead of aborting mid-deploy —
+ // adversarial-review P2). The parent's group SIGKILL at drain end is the
+ // backstop for handlers that keep running.
+ const onSigterm = (): void => {
+ if (!shutdown.signal.aborted) shutdown.abort(new Error('worker-shutdown'));
+ };
+ // Parent death (orphan) aborts BOTH: nobody will SIGKILL us, the lock will
+ // expire and the job will be requeued — stop the handler outright.
+ const onOrphaned = (): void => {
+ onSigterm();
+ if (!abort.signal.aborted) abort.abort(new Error('worker-shutdown'));
+ };
+ process.on('SIGTERM', onSigterm);
+
+ // Parent-liveness watchdog (unref'd — never keeps the child alive).
+ const pollMs = injectables.parentPollMs ?? 15_000;
+ const graceMs = injectables.orphanGraceMs ?? 30_000;
+ const hardExit = injectables.hardExit ?? ((code: number) => process.exit(code));
+ let watchdog: ReturnType<typeof setInterval> | null = null;
+ if (opts.parentPid > 0) {
+ watchdog = setInterval(() => {
+ try {
+ process.kill(opts.parentPid, 0);
+ } catch {
+ process.stderr.write(
+ `[run-child] parent worker (pid ${opts.parentPid}) is gone — aborting handler; ` +
+ `hard exit in ${Math.round(graceMs / 1000)}s\n`,
+ );
+ if (watchdog != null) clearInterval(watchdog);
+ onOrphaned();
+ const t = setTimeout(() => hardExit(1), graceMs);
+ unrefTimer(t);
+ }
+ }, pollMs);
+ unrefTimer(watchdog);
+ }
+
+ try {
+ const context = buildJobContext(
+ engine,
+ queue,
+ job,
+ opts.lockToken,
+ abort.signal,
+ shutdown.signal,
+ );
+ let outcome;
+ try {
+ const result = await handler(context);
+ // completeJob's {value: x} wrap decision must run BEFORE JSON
+ // serialization: a JSON round-trip changes typeof for Date /
+ // toJSON-bearing results (object → string), which would flip the wrap
+ // parent-side (adversarial-review P3, result-shape parity). Wrap here;
+ // the parent's own wrap is then a no-op (object/undefined passthrough).
+ const wrapped = result != null
+ ? (typeof result === 'object' ? (result as Record<string, unknown>) : { value: result })
+ : undefined;
+ outcome = { outcome: 'success' as const, result: wrapped };
+ } catch (err) {
+ outcome = encodeHandlerError(err);
+ }
+ try {
+ writeChildOutcomeFile(opts.resultPath, outcome);
+ } catch (writeErr) {
+ const msg = writeErr instanceof Error ? writeErr.message : String(writeErr);
+ process.stderr.write(`[run-child] failed to write outcome file: ${msg}\n`);
+ return JOB_CHILD_EXIT_RESULT_WRITE_FAILED;
+ }
+ return 0;
+ } finally {
+ if (watchdog != null) clearInterval(watchdog);
+ process.off('SIGTERM', onSigterm);
+ }
+}
diff --git a/src/core/minions/supervisor.ts b/src/core/minions/supervisor.ts
index 658362e43d..df091ab025 100644
--- a/src/core/minions/supervisor.ts
+++ b/src/core/minions/supervisor.ts
@@ -98,6 +98,10 @@ export interface SupervisorOpts {
nice_requested?: number;
/** Effective niceness of the supervisor process after its own renice attempt. */
nice_effective?: number;
+ /** issue #5: when 'process', the spawned worker runs each claimed job in a
+ * SIGKILL-able child process (passed through as `--job-isolation process`).
+ * Omitted/inline: today's shared-process execution. */
+ jobIsolation?: 'inline' | 'process';
/** Error string if the supervisor's own renice failed (e.g. EPERM). */
nice_error?: string;
/**
@@ -174,7 +178,7 @@ const DEFAULTS: Omit<SupervisorOpts, 'cliPath'> = {
* niceness also inherits to the worker's own children automatically.
*/
export function buildWorkerArgs(
- opts: Pick<SupervisorOpts, 'concurrency' | 'queue' | 'maxRssMb' | 'nice_requested'>,
+ opts: Pick<SupervisorOpts, 'concurrency' | 'queue' | 'maxRssMb' | 'nice_requested' | 'jobIsolation'>,
): string[] {
const args = [
'jobs', 'work',
@@ -187,6 +191,11 @@ export function buildWorkerArgs(
if (opts.nice_requested !== undefined) {
args.push('--nice', String(opts.nice_requested));
}
+ // Conditional push (issue #5): omitted for inline so existing deployments'
+ // argv is byte-identical (pinned by supervisor-build-worker-args.test.ts).
+ if (opts.jobIsolation === 'process') {
+ args.push('--job-isolation', 'process');
+ }
return args;
}
@@ -250,6 +259,7 @@ export async function queryWedgeSignals(
engine: BrainEngine,
queue: string,
handlerNames: string[],
+ opts?: { signal?: AbortSignal },
): Promise<WedgeSignals> {
const rows = await engine.executeRaw<{
stalled: string;
@@ -272,6 +282,7 @@ export async function queryWedgeSignals(
FROM minion_jobs
WHERE queue = $1`,
[queue, handlerNames],
+ opts,
);
const row = rows[0] ?? {
stalled: '0', active_healthy: '0', waiting: '0',
@@ -339,11 +350,23 @@ export async function probeQueueState(
): Promise<QueueSubmitState> {
const timeoutMs = opts.timeoutMs ?? QUEUE_PROBE_TIMEOUT_MS;
let timer: ReturnType<typeof setTimeout> | undefined;
+ // issue #6 / TODOS "cancel timed-out submit-time queue probes": the losing
+ // inner probe used to keep running on the pool after the race resolved —
+ // under pool exhaustion the abandoned query held a slot and made the
+ // exhaustion worse. The timeout now aborts a per-probe signal so the
+ // in-flight SQL is cancelled (postgres.js .cancel()) and its slot released.
+ const probeAbort = new AbortController();
const timeout = new Promise<QueueSubmitState>((resolveTimeout) => {
- timer = setTimeout(() => resolveTimeout({ probe_failed: true }), timeoutMs);
+ timer = setTimeout(() => {
+ probeAbort.abort();
+ resolveTimeout({ probe_failed: true });
+ }, timeoutMs);
});
try {
- return await Promise.race([probeQueueStateInner(engine, queue, handlerNames), timeout]);
+ return await Promise.race([
+ probeQueueStateInner(engine, queue, handlerNames, { signal: probeAbort.signal }),
+ timeout,
+ ]);
} catch {
return { probe_failed: true };
} finally {
@@ -355,8 +378,9 @@ async function probeQueueStateInner(
engine: BrainEngine,
queue: string,
handlerNames: string[],
+ opts?: { signal?: AbortSignal },
): Promise<QueueSubmitState> {
- const sig = await queryWedgeSignals(engine, queue, handlerNames);
+ const sig = await queryWedgeSignals(engine, queue, handlerNames, opts);
// Oldest-waiting age, same filter shape as the wedge signals. Perf: the
// wave's migration (landing in another lane) adds the btree
@@ -368,6 +392,7 @@ async function probeQueueStateInner(
FROM minion_jobs
WHERE queue = $1 AND status = 'waiting'`,
[queue],
+ opts,
);
const rawAge = ageRows[0]?.age ?? null;
const oldestAge = rawAge === null ? null : Number(rawAge);
diff --git a/src/core/minions/types.ts b/src/core/minions/types.ts
index e78d7d5001..b43d16e4c0 100644
--- a/src/core/minions/types.ts
+++ b/src/core/minions/types.ts
@@ -218,6 +218,16 @@ export interface MinionWorkerOpts {
* hung probe would wedge the recursive setTimeout chain forever and
* silently disable the health monitor. Default: 10000 (10 seconds). */
dbProbeTimeoutMs?: number;
+ /** issue #5: 'process' runs each claimed job in a SIGKILL-able child
+ * process (blast radius = 1 job). Default 'inline' (today's behavior).
+ * Requires childCliInvocation; the CLI layer resolves + validates it. */
+ jobIsolation?: 'inline' | 'process';
+ /** How to invoke the gbrain CLI for job children (resolved fail-fast at
+ * worker startup by the CLI layer; structurally ChildCliInvocation from
+ * job-isolation.ts — kept inline here to avoid an import cycle). */
+ childCliInvocation?: { cmd: string; argsPrefix: string[] } | null;
+ /** tini path for wrapping job children ('' = absent, direct spawn). */
+ childTiniPath?: string;
}
// --- Job Context (passed to handlers) ---
@@ -372,6 +382,20 @@ export type TranscriptEntry =
| { type: 'llm_turn'; model: string; tokens_in: number; tokens_out: number; ts: string }
| { type: 'error'; message: string; stack?: string; ts: string };
+// --- Abort-reason literals (single source of truth) ---
+//
+// Per-job abort sites construct `new Error(REASON)`; classification sites
+// (worker.ts INFRASTRUCTURE_ABORT_REASONS, child-job-runner.ts
+// PER_JOB_ABORT_REASONS) match on the message. Deriving both sets from these
+// constants keeps a rename at an abort site from silently flipping child
+// classification (maintainability review).
+
+/** Infrastructure faults: released, no attempt burned; stall sweeper requeues. */
+export const ABORT_REASON_LOCK_RENEWAL_FAILED = 'lock-renewal-failed';
+export const ABORT_REASON_LOCK_LOST = 'lock-lost';
+/** Job-targeted aborts: keep their existing attempt semantics. */
+export const ABORT_REASON_TIMEOUT = 'timeout';
+
// --- Errors ---
/** Throw this from a handler to skip all retry logic and go straight to 'dead'. */
diff --git a/src/core/minions/worker-exit-codes.ts b/src/core/minions/worker-exit-codes.ts
index b33e673679..354adf38ef 100644
--- a/src/core/minions/worker-exit-codes.ts
+++ b/src/core/minions/worker-exit-codes.ts
@@ -22,3 +22,17 @@
/** Worker drained itself because RSS crossed the watchdog cap. */
export const WORKER_EXIT_RSS_WATCHDOG = 12;
+
+// --- `gbrain jobs run-child` exit codes (issue #5 process isolation) -------
+//
+// The parent (child-job-runner.ts) classifies a child by RESULT-FILE PRESENCE
+// first: a written outcome file + exit 0 is the normal path even for handler
+// FAILURE (a reported error outcome is a successful report). Non-zero codes
+// mean "could not run or could not report":
+
+/** run-child misuse: bad/missing argv or env, or a PGLite engine (no isolation there). */
+export const JOB_CHILD_EXIT_USAGE = 13;
+/** Job row validation failed: not 'active' or lock-token mismatch (reclaimed). */
+export const JOB_CHILD_EXIT_NOT_CLAIMED = 14;
+/** Handler finished but the outcome file could not be written. */
+export const JOB_CHILD_EXIT_RESULT_WRITE_FAILED = 15;
diff --git a/src/core/minions/worker.ts b/src/core/minions/worker.ts
index 6a82891459..107707715c 100644
--- a/src/core/minions/worker.ts
+++ b/src/core/minions/worker.ts
@@ -18,7 +18,11 @@ import type {
MinionJob, MinionJobContext, MinionHandler, MinionWorkerOpts,
MinionQueueOpts, TokenUpdate,
} from './types.ts';
-import { UnrecoverableError } from './types.ts';
+import {
+ UnrecoverableError,
+ ABORT_REASON_LOCK_RENEWAL_FAILED,
+ ABORT_REASON_LOCK_LOST,
+} from './types.ts';
import { MinionQueue } from './queue.ts';
import { calculateBackoff } from './backoff.ts';
import { RateLeaseUnavailableError } from './handlers/subagent.ts';
@@ -29,6 +33,20 @@ import {
type LockRenewalDeps,
type LockRenewalState,
} from './lock-renewal-tick.ts';
+import {
+ runDbProbe,
+ getConnectionRouting,
+ DIRECT_PROBE_TIMEOUT_MS,
+ type DbProbeResult,
+ type PoolDiagnostics,
+} from './db-probe.ts';
+import { buildJobContext } from './job-context.ts';
+import {
+ runJobInChild,
+ ChildSpawnInfraError,
+ ChildWorkerShutdownError,
+ ChildNotClaimedError,
+} from './child-job-runner.ts';
import { lockRenewalAudit } from '../audit/lock-renewal-audit.ts';
import { isRetryableConnError } from '../retry-matcher.ts';
import { reconnectAfterConnectionError as reconnectEngineAfterConnError } from './reconnect.ts';
@@ -49,8 +67,8 @@ import { reconnectAfterConnectionError as reconnectEngineAfterConnError } from '
* to this set is a deliberate two-line change, not a silent regression).
*/
export const INFRASTRUCTURE_ABORT_REASONS = new Set<string>([
- 'lock-renewal-failed',
- 'lock-lost',
+ ABORT_REASON_LOCK_RENEWAL_FAILED,
+ ABORT_REASON_LOCK_LOST,
]);
import { randomUUID } from 'crypto';
import { EventEmitter } from 'events';
@@ -120,10 +138,19 @@ export function getAccurateRss(
}
/** Reason payload emitted with `'unhealthy'` when self-health-check trips.
- * CLI layer (jobs.ts:work) subscribes and decides whether to call process.exit. */
+ * CLI layer (jobs.ts:work) subscribes and decides whether to call process.exit.
+ * `verdict` (issue #6) distinguishes local pool starvation from a genuinely
+ * unreachable server so operators stop debugging the wrong layer; absent on
+ * engines without the probe's disambiguation lane. */
export type UnhealthyReason =
- | { reason: 'db_dead'; consecutiveFailures: number; message: string }
- | { reason: 'stalled'; waitingCount: number; idleMinutes: number };
+ | {
+ reason: 'db_dead';
+ consecutiveFailures: number;
+ message: string;
+ verdict?: 'pool_starved' | 'server_unreachable' | 'unknown';
+ }
+ | { reason: 'stalled'; waitingCount: number; idleMinutes: number }
+ | { reason: 'child_spawn_failing'; consecutiveFailures: number; message: string };
/**
* Read the quiet_hours JSONB column off a MinionJob, if present. The
@@ -188,6 +215,18 @@ export class MinionWorker extends EventEmitter {
private _peakRssMb = 0;
/** Latch so the 80%-of-cap soft-warn fires once per crossing, not every check. */
private _softWarnFired = false;
+ /**
+ * Circuit breaker for deterministic child-bootstrap failures (red-team
+ * finding): a spawn failure releases the job with no attempt burned, the
+ * stall sweeper requeues it, the same worker re-claims — an infinite
+ * claim/release loop the stall detector cannot see (every settle refreshes
+ * the progress clock). After CHILD_SPAWN_FAIL_EXIT_AFTER consecutive
+ * spawn-class failures we emit 'unhealthy' so the process manager restarts
+ * the worker (and the supervisor's crash budget takes over if the child
+ * CLI stays broken).
+ */
+ private _consecutiveChildSpawnFailures = 0;
+ private static readonly CHILD_SPAWN_FAIL_EXIT_AFTER = 3;
private opts: Required<MinionWorkerOpts>;
@@ -215,7 +254,22 @@ export class MinionWorker extends EventEmitter {
stallExitAfterMs: opts?.stallExitAfterMs ?? 10 * 60_000,
dbFailExitAfter: opts?.dbFailExitAfter ?? 3,
dbProbeTimeoutMs: opts?.dbProbeTimeoutMs ?? 10_000,
+ jobIsolation: opts?.jobIsolation ?? 'inline',
+ childCliInvocation: opts?.childCliInvocation ?? null,
+ childTiniPath: opts?.childTiniPath ?? '',
};
+ // Process isolation contract: 'process' without a resolved child CLI
+ // invocation would silently execute handlers INLINE while the evict path
+ // believed it was isolated (predicate mismatch — red-team finding). The
+ // CLI layer always resolves + validates the invocation; library callers
+ // must too. Loud construction throw, same discipline as the stall
+ // thresholds below.
+ if (this.opts.jobIsolation === 'process' && this.opts.childCliInvocation == null) {
+ throw new Error(
+ "MinionWorkerOpts: jobIsolation 'process' requires childCliInvocation " +
+ '(resolve it via resolveChildCliInvocation and validate it exists before constructing the worker).',
+ );
+ }
// Stall thresholds contract: exit MUST be strictly greater than warn.
// If exit <= warn, the warn-then-exit semantics break: a single tick at
// idle > warn would set stallWarningSince and the subsequent tick at
@@ -231,6 +285,15 @@ export class MinionWorker extends EventEmitter {
}
}
+ /**
+ * Read-only handler lookup. `gbrain jobs run-child` registers the builtin
+ * handlers against a throwaway worker (registerBuiltinHandlers's existing
+ * contract) and resolves the one it needs through this accessor.
+ */
+ getHandler(name: string): MinionHandler | undefined {
+ return this.handlers.get(name);
+ }
+
/** Register a handler for a job type. */
register(name: string, handler: MinionHandler): void {
this.handlers.set(name, handler);
@@ -263,7 +326,9 @@ export class MinionWorker extends EventEmitter {
if (this.listenerCount('unhealthy') === 0) {
const detail = info.reason === 'db_dead'
? `DB unreachable (${info.consecutiveFailures} probes): ${info.message}`
- : `worker stalled (${info.waitingCount} waiting, ${info.idleMinutes}m idle)`;
+ : info.reason === 'child_spawn_failing'
+ ? `job-child spawn failing (${info.consecutiveFailures} consecutive): ${info.message}`
+ : `worker stalled (${info.waitingCount} waiting, ${info.idleMinutes}m idle)`;
console.error(
`[health] FATAL: ${detail}. No 'unhealthy' listener registered; ` +
`defaulting to process.exit(1) for process-manager restart.`,
@@ -388,28 +453,36 @@ export class MinionWorker extends EventEmitter {
let healthRunning = false;
let healthExited = false;
- // Race executeRaw against a wall-clock deadline. A hung connection
- // (network-partitioned PgBouncer, deadlocked backend) would otherwise
- // hold the await forever — the recursive setTimeout's next tick is only
- // scheduled in `finally`, so a hung probe would silently disable the
- // entire health monitor. The timeout treats hangs as failures and feeds
- // them into `dbFailExitAfter`.
- const probeWithTimeout = async (): Promise<void> => {
- const ac = new AbortController();
- const timeoutMs = this.opts.dbProbeTimeoutMs;
- const timer = setTimeout(() => ac.abort(), timeoutMs);
- try {
- await Promise.race([
- this.engine.executeRaw('SELECT 1'),
- new Promise<never>((_, reject) => {
- ac.signal.addEventListener('abort', () => {
- reject(new Error(`probe timeout after ${timeoutMs}ms`));
- });
- }),
- ]);
- } finally {
- clearTimeout(timer);
- }
+ // DB liveness probe with pool-starvation disambiguation (issue #6).
+ // The probe body lives in db-probe.ts (hermetically tested,
+ // lock-renewal-tick pattern); this is the thin adapter. Both probes
+ // carry an AbortSignal — a hung probe is CANCELLED (slot released),
+ // never abandoned. The direct-lane probe runs ONLY when dual-pool is
+ // genuinely active (a kill-switched executeRawDirect would probe the
+ // same starved read pool twice and fake a verdict).
+ const runProbe = async (): Promise<DbProbeResult> => {
+ const cm = getConnectionRouting(this.engine);
+ const dualPool = cm?.isDualPoolActive?.() === true;
+ const getDiag = (this.engine as {
+ getPoolDiagnostics?: () => PoolDiagnostics | null;
+ }).getPoolDiagnostics;
+ return runDbProbe({
+ probeRead: async (signal) => {
+ await this.engine.executeRaw('SELECT 1', undefined, { signal });
+ },
+ ...(dualPool
+ ? {
+ probeDirect: async (signal: AbortSignal) => {
+ await this.engine.executeRawDirect('SELECT 1', undefined, { signal });
+ },
+ }
+ : {}),
+ ...(typeof getDiag === 'function'
+ ? { getDiagnostics: () => getDiag.call(this.engine) }
+ : {}),
+ timeoutMs: this.opts.dbProbeTimeoutMs,
+ directTimeoutMs: DIRECT_PROBE_TIMEOUT_MS,
+ });
};
const runHealthCheck = async (): Promise<void> => {
@@ -417,25 +490,25 @@ export class MinionWorker extends EventEmitter {
healthRunning = true;
try {
// --- 1. DB liveness probe ---
- try {
- await probeWithTimeout();
+ const probe = await runProbe();
+ if (probe.ok) {
consecutiveDbFailures = 0;
- } catch (e) {
+ } else {
consecutiveDbFailures++;
- const msg = e instanceof Error ? e.message : String(e);
console.error(
- `[health] DB probe failed (${consecutiveDbFailures}/${this.opts.dbFailExitAfter}): ${msg}`,
+ `[health] DB probe failed (${consecutiveDbFailures}/${this.opts.dbFailExitAfter}): ${probe.detail}`,
);
if (consecutiveDbFailures >= this.opts.dbFailExitAfter) {
console.error(
- `[health] DB unreachable after ${this.opts.dbFailExitAfter} consecutive probes. ` +
- `Emitting 'unhealthy' for process-manager restart.`,
+ `[health] DB probe failed ${this.opts.dbFailExitAfter} consecutive times ` +
+ `(verdict: ${probe.verdict}). Emitting 'unhealthy' for process-manager restart.`,
);
healthExited = true;
this.emitUnhealthy({
reason: 'db_dead',
consecutiveFailures: consecutiveDbFailures,
- message: msg,
+ message: probe.detail,
+ verdict: probe.verdict,
});
}
return; // Skip stall check when DB is flaky
@@ -876,7 +949,7 @@ export class MinionWorker extends EventEmitter {
// and the tick keeps its legacy no-reconnect behavior.
const engineReconnect = (this.engine as { reconnect?: (ctx?: { error?: unknown }) => Promise<void> }).reconnect;
const renewalDeps: LockRenewalDeps = {
- renewLock: (id, tok, dur) => this.queue.renewLock(id, tok, dur),
+ renewLock: (id, tok, dur, opts) => this.queue.renewLock(id, tok, dur, opts),
audit: lockRenewalAudit,
now: Date.now,
setTimeout: (cb, ms) => globalThis.setTimeout(cb, ms),
@@ -946,11 +1019,12 @@ export class MinionWorker extends EventEmitter {
);
clearInterval(lockTimer);
this.inFlight.delete(job.id);
- // D8a: don't failJob if the abort was infrastructure. The
- // stall detector will reclaim the row cleanly because the
- // lock has expired (lock-renewal aborts only fire after
- // lockDuration - safetyMargin elapsed without renewal).
- if (!INFRASTRUCTURE_ABORT_REASONS.has(reason)) {
+ // D8a: don't failJob on infrastructure aborts (stall detector
+ // reclaims after lock expiry). Isolation mode: also skip — the
+ // group SIGKILL already fired and executeJob's own recording
+ // follows; a competing evict failJob('dead') could dead-letter a
+ // job with attempts remaining (adversarial-review P3).
+ if (!INFRASTRUCTURE_ABORT_REASONS.has(reason) && this.opts.jobIsolation !== 'process') {
this.queue.failJob(
job.id,
lockToken,
@@ -1024,48 +1098,48 @@ export class MinionWorker extends EventEmitter {
return;
}
+ // issue #5: 'process' isolation runs the handler in a SIGKILL-able child;
+ // the parent keeps claim/renewal and ALL result recording below — this
+ // branch swaps ONLY the execution engine. When isolated, the parent-side
+ // context is skipped entirely (the child builds its own against its own
+ // engine; building it here would be dead work holding closures). The
+ // constructor guarantees childCliInvocation != null whenever
+ // jobIsolation === 'process', so this predicate matches the evict guard.
+ const isolated = this.opts.jobIsolation === 'process';
+
// Build job context with per-job AbortSignal + shared shutdown signal.
// Most handlers only care about `signal` (timeout / cancel / lock-loss).
// `shutdownSignal` is separate: fires only on worker process SIGTERM/SIGINT.
// Handlers that need to run cleanup before worker exit (shell handler's
// SIGTERM→5s→SIGKILL on its child) subscribe to shutdownSignal too.
- const context: MinionJobContext = {
- id: job.id,
- name: job.name,
- data: job.data,
- attempts_made: job.attempts_made,
- signal: abort.signal,
- deadlineAtMs: job.timeout_at != null ? job.timeout_at.getTime() : null,
- shutdownSignal: this.shutdownAbort.signal,
- updateProgress: async (progress: unknown) => {
- await this.queue.updateProgress(job.id, lockToken, progress);
- },
- updateTokens: async (tokens: TokenUpdate) => {
- await this.queue.updateTokens(job.id, lockToken, tokens);
- },
- log: async (message: string | Record<string, unknown>) => {
- const value = typeof message === 'string' ? message : JSON.stringify(message);
- await this.engine.executeRaw(
- `UPDATE minion_jobs SET stacktrace = COALESCE(stacktrace, '[]'::jsonb) || to_jsonb($1::text),
- updated_at = now()
- WHERE id = $2 AND status = 'active' AND lock_token = $3`,
- [value, job.id, lockToken]
+ // Builder shared with `gbrain jobs run-child` (job-context.ts) so the
+ // process-isolation child wires the exact same DB-backed callbacks.
+ const context: MinionJobContext | null = isolated
+ ? null
+ : buildJobContext(
+ this.engine,
+ this.queue,
+ job,
+ lockToken,
+ abort.signal,
+ this.shutdownAbort.signal,
);
- },
- isActive: async () => {
- const rows = await this.engine.executeRaw<{ id: number }>(
- `SELECT id FROM minion_jobs WHERE id = $1 AND status = 'active' AND lock_token = $2`,
- [job.id, lockToken]
- );
- return rows.length > 0;
- },
- readInbox: async () => {
- return this.queue.readInbox(job.id, lockToken);
- },
- };
try {
- const result = await handler(context);
+ const result = isolated
+ ? await runJobInChild({
+ jobId: job.id,
+ jobName: job.name,
+ lockToken,
+ abortSignal: abort.signal,
+ shutdownSignal: this.shutdownAbort.signal,
+ invocation: this.opts.childCliInvocation as { cmd: string; argsPrefix: string[] },
+ tiniPath: this.opts.childTiniPath,
+ })
+ : await handler(context as MinionJobContext);
+
+ // The child spawned and ran — the spawn path is healthy again.
+ this._consecutiveChildSpawnFailures = 0;
clearInterval(lockTimer);
@@ -1121,6 +1195,56 @@ export class MinionWorker extends EventEmitter {
return;
}
+ // Any error that ISN'T a spawn failure proves the spawn path works —
+ // keep the breaker's "consecutive" semantics honest.
+ if (!(err instanceof ChildSpawnInfraError)) {
+ this._consecutiveChildSpawnFailures = 0;
+ }
+
+ // issue #5 process isolation — two more infrastructure classes, same
+ // release semantics as the block above (lock expires once launchJob's
+ // finally clears the renewal timer; the stall sweeper requeues):
+ // - spawn failure: an ops misconfiguration (bad child CLI path) must
+ // not burn attempts job-by-job until the queue dead-letters. The
+ // CLI layer also fail-fast validates the invocation at startup.
+ // - worker shutdown: a routine deploy killed the child before it
+ // could report (codex-2 #7); burning an attempt per deploy would
+ // dead-letter long jobs after a few releases.
+ if (err instanceof ChildSpawnInfraError) {
+ console.error(
+ `Job ${job.id} (${job.name}) released after child spawn failure — ` +
+ `check the worker's child CLI configuration: ${errorText} (no attempt burned)`,
+ );
+ this._consecutiveChildSpawnFailures += 1;
+ if (this._consecutiveChildSpawnFailures >= MinionWorker.CHILD_SPAWN_FAIL_EXIT_AFTER) {
+ console.error(
+ `[isolation] ${this._consecutiveChildSpawnFailures} consecutive child spawn/bootstrap ` +
+ `failures — the child CLI is deterministically broken. Emitting 'unhealthy' for ` +
+ `process-manager restart instead of looping claim/release forever.`,
+ );
+ this.emitUnhealthy({
+ reason: 'child_spawn_failing',
+ consecutiveFailures: this._consecutiveChildSpawnFailures,
+ message: errorText,
+ });
+ }
+ return;
+ }
+ if (err instanceof ChildWorkerShutdownError) {
+ console.log(
+ `Job ${job.id} (${job.name}) released after worker shutdown (${errorText}); ` +
+ `stall detector will requeue (no attempt burned)`,
+ );
+ return;
+ }
+ if (err instanceof ChildNotClaimedError) {
+ // The child proved the claim is gone (reclaimed/cancelled before the
+ // handler ran). The token-fenced failJob would no-op anyway — return
+ // without burning anything against a claim we no longer hold.
+ console.log(`Job ${job.id} (${job.name}): ${errorText}`);
+ return;
+ }
+
// v0.41 Bug 2: lease-full bounces don't burn attempts.
//
// Pre-v0.41 every non-`UnrecoverableError` routed to `delayed` with
diff --git a/src/core/pool-gauge.ts b/src/core/pool-gauge.ts
new file mode 100644
index 0000000000..bb358c5ffc
--- /dev/null
+++ b/src/core/pool-gauge.ts
@@ -0,0 +1,48 @@
+/**
+ * CheckoutGauge — approximate in-flight query counters for the health probe's
+ * pool diagnostics (issue #6).
+ *
+ * HONESTY CONTRACT (read before extending): this gauge counts calls through
+ * the engine's raw/direct/reserved/transaction seams ONLY. The majority of
+ * engine traffic — tagged-template queries on `this.sql` (getConfig, CRUD,
+ * search) — is NOT tracked; postgres.js exposes no public checkout counters
+ * and proxying the Sql template function is too invasive for a diagnostic.
+ * Every consumer must label these numbers as a tracked SUBSET and must never
+ * derive "available" or "waiting" figures from them (that arithmetic is
+ * invented telemetry — outside-voice review, codex-2 #3). The authoritative
+ * starvation signal is the direct-lane disambiguation probe in
+ * `src/core/minions/db-probe.ts`; these counts are supporting detail.
+ *
+ * Fail-open by construction: plain integer bumps, no I/O, release() clamps
+ * at zero so a missed acquire can never underflow into negative counts.
+ */
+
+/** Which engine seam the in-flight call went through. */
+export type GaugeKind = 'raw' | 'direct' | 'reserved' | 'tx';
+
+export interface PoolGaugeSnapshot {
+ /** executeRaw on the read pool. */
+ raw: number;
+ /** executeRawDirect (direct session lane when dual-pool, read pool otherwise). */
+ direct: number;
+ /** withReservedConnection holders. */
+ reserved: number;
+ /** transaction() bodies. */
+ tx: number;
+}
+
+export class CheckoutGauge {
+ private counts: PoolGaugeSnapshot = { raw: 0, direct: 0, reserved: 0, tx: 0 };
+
+ acquire(kind: GaugeKind): void {
+ this.counts[kind] += 1;
+ }
+
+ release(kind: GaugeKind): void {
+ if (this.counts[kind] > 0) this.counts[kind] -= 1;
+ }
+
+ snapshot(): PoolGaugeSnapshot {
+ return { ...this.counts };
+ }
+}
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.ts
index a192b6402b..c7d26420ee 100644
--- a/src/core/postgres-engine.ts
+++ b/src/core/postgres-engine.ts
@@ -25,6 +25,7 @@ import {
type BatchAuditSite,
} from './retry.ts';
import { isConnectionEndedError } from './retry-matcher.ts';
+import { CheckoutGauge, type PoolGaugeSnapshot } from './pool-gauge.ts';
import {
valueHash,
normalizeDimension,
@@ -82,7 +83,7 @@ import { GBrainError, PAGE_SORT_SQL, ENRICH_ORDER_SQL } from './types.ts';
import { finalizeLastSeen } from './chronicle/last-seen.ts';
import { computeAnomaliesFromBuckets } from './cycle/anomaly.ts';
import * as db from './db.ts';
-import { ConnectionManager } from './connection-manager.ts';
+import { ConnectionManager, DEFAULT_DIRECT_POOL_SIZE } from './connection-manager.ts';
import { logConnectionEvent } from './connection-audit.ts';
import { validateSlug, contentHash, rowToPage, rowToStalePage, rowToChunk, rowToSearchResult, parseEmbedding, tryParseEmbedding, takeRowToTake, takeHitRowToHit, isUndefinedTableError, warnOncePerProcess } from './utils.ts';
import { resolveBoostMap, resolveHardExcludes } from './search/source-boost.ts';
@@ -132,6 +133,13 @@ export class PostgresEngine implements BrainEngine {
private _savedConfig: (EngineConfig & { poolSize?: number; parentConnectionManager?: ConnectionManager }) | null = null;
/** Whether a reconnect is in progress (prevents concurrent reconnects). */
private _reconnecting = false;
+ /**
+ * Approximate in-flight counters for the health probe's diagnostics
+ * (issue #6). Tracks the raw/direct/reserved/tx seams ONLY — see the
+ * honesty contract in pool-gauge.ts. Shared by tx-scoped engine clones
+ * via the prototype chain (same process, same pools). Fail-open.
+ */
+ private checkoutGauge = new CheckoutGauge();
/**
* #1471: module-singleton OWNERSHIP token. `true` only for the engine whose
* connect() actually created the shared db.ts `sql` singleton (returned
@@ -297,6 +305,8 @@ export class PostgresEngine implements BrainEngine {
max: size,
idle_timeout: 20,
connect_timeout: 10,
+ // Explicit (matches the postgres.js implicit default; GBRAIN_POOL_MAX_LIFETIME_S overrides).
+ max_lifetime: db.resolveMaxLifetimeSeconds(),
types: { bigint: postgres.BigInt },
// Silence postgres NOTICE-level messages by default. See db.ts for
// rationale (stdout-parsing callers like jobs-submit --json break when
@@ -1059,18 +1069,83 @@ export class PostgresEngine implements BrainEngine {
async transaction<T>(fn: (engine: BrainEngine) => Promise<T>): Promise<T> {
const conn = this.sql;
- return conn.begin(async (tx) => {
- // Create a scoped engine with tx as its connection, no shared state mutation
- const txEngine = Object.create(this) as PostgresEngine;
- Object.defineProperty(txEngine, 'sql', { get: () => tx });
- Object.defineProperty(txEngine, '_sql', { value: tx as unknown as ReturnType<typeof postgres>, writable: false });
- return fn(txEngine);
- }) as Promise<T>;
+ // try/finally, not .finally on the chained promise: begin() can throw
+ // SYNCHRONOUSLY (e.g. nested transaction on a tx clone whose conn has no
+ // .begin), which would skip a chained .finally and leak the counter.
+ this.checkoutGauge.acquire('tx');
+ try {
+ return await (conn.begin(async (tx) => {
+ // Create a scoped engine with tx as its connection, no shared state mutation
+ const txEngine = Object.create(this) as PostgresEngine;
+ Object.defineProperty(txEngine, 'sql', { get: () => tx });
+ Object.defineProperty(txEngine, '_sql', { value: tx as unknown as ReturnType<typeof postgres>, writable: false });
+ return fn(txEngine);
+ }) as Promise<T>);
+ } finally {
+ this.checkoutGauge.release('tx');
+ }
}
+ /**
+ * issue #6 (reserved-connection routing): concurrent DIRECT-pool reserves
+ * are capped at directPoolSize - 1 so the claim/renewLock heartbeats always
+ * keep >= 1 direct slot; overflow falls back to the READ pool — exactly the
+ * pre-routing behavior, so this change is strictly never-worse than the
+ * status quo (deliberate rejection of queue-for-a-permit: that would block
+ * migrations behind multi-minute CREATE INDEX holds). Per-process by
+ * design: each process owns its own direct pool, so a CLI migration's
+ * reserves cannot starve a worker's heartbeats.
+ */
+ private _reservedDirectInFlight = 0;
+
async withReservedConnection<T>(fn: (conn: ReservedConnection) => Promise<T>): Promise<T> {
- const pool = this.sql;
- const reserved = await pool.reserve();
+ // Long-hold reserved work (CREATE INDEX CONCURRENTLY, transaction:false
+ // migration DDL, backfill BEGIN..COMMIT batches) belongs on the DIRECT
+ // session lane: 30-min statement_timeout + maintenance_work_mem GUCs and
+ // it stops pinning the worker's shared read pool (the observed 353s
+ // COMMIT in issue #6 was a reserved read-pool slot). Never reroute inside
+ // an open transaction (same guard shape as executeRawDirect).
+ const inTransaction = this._sql !== null && this.connectionManager?.peekReadPool() !== this._sql;
+ let pool = this.sql;
+ let fromDirect = false;
+ if (!inTransaction && this.connectionManager?.isDualPoolActive()) {
+ const size = this.connectionManager.describeMode().direct_pool_size ?? DEFAULT_DIRECT_POOL_SIZE;
+ // NO floor on the cap (red-team finding): at direct_pool_size=1 a
+ // Math.max(1, ...) floor would let a multi-minute reserve consume the
+ // ONLY direct session and starve claim/renewLock heartbeats — the
+ // exact #6 class, reintroduced on the direct pool. cap <= 0 means the
+ // direct lane has no spare capacity for reserves: use the read pool
+ // (the true status quo).
+ const cap = size - 1;
+ if (cap >= 1 && this._reservedDirectInFlight < cap) {
+ // Take the permit in the SAME synchronous frame as the check — a
+ // check-then-increment spanning `await ddl()` is a TOCTOU that lets
+ // same-tick concurrent reserves overshoot the cap and starve the
+ // heartbeat slot the cap exists to protect (adversarial-review P2).
+ this._reservedDirectInFlight += 1;
+ fromDirect = true;
+ try {
+ pool = await this.connectionManager.ddl();
+ } catch {
+ // ddl() failure flips its own kill switch; fall back to the read
+ // pool (status quo) rather than failing the caller.
+ this._reservedDirectInFlight -= 1;
+ fromDirect = false;
+ pool = this.sql;
+ }
+ }
+ }
+ // Gauge BEFORE reserve(): a reserve() stuck waiting for a free slot is
+ // exactly the in-flight pressure the probe diagnostics should surface.
+ this.checkoutGauge.acquire('reserved');
+ let reserved: Awaited<ReturnType<typeof pool.reserve>>;
+ try {
+ reserved = await pool.reserve();
+ } catch (e) {
+ this.checkoutGauge.release('reserved');
+ if (fromDirect) this._reservedDirectInFlight -= 1;
+ throw e;
+ }
try {
const conn: ReservedConnection = {
async executeRaw<R = Record<string, unknown>>(
@@ -1091,7 +1166,34 @@ export class PostgresEngine implements BrainEngine {
};
return await fn(conn);
} finally {
- reserved.release();
+ // Counter/gauge decrements run regardless of release() throwing
+ // (double-release or socket error must not permanently leak a permit
+ // of the small direct-reserve budget — data-migration review).
+ try {
+ reserved.release();
+ } catch {
+ // best-effort; the pool's own lifecycle handles a broken reservation
+ }
+ this.checkoutGauge.release('reserved');
+ if (fromDirect) this._reservedDirectInFlight -= 1;
+ }
+ }
+
+ /**
+ * Health-probe diagnostics (issue #6). Duck-typed — deliberately NOT on the
+ * BrainEngine interface (PGLite has no pool to diagnose; the worker reads
+ * it optionally, same pattern as `engine.reconnect`). Fail-open: returns
+ * null instead of throwing.
+ */
+ getPoolDiagnostics(): { tracked: PoolGaugeSnapshot; poolMax: number | null } | null {
+ try {
+ const max = (this.sql as unknown as { options?: { max?: number } }).options?.max;
+ return {
+ tracked: this.checkoutGauge.snapshot(),
+ poolMax: typeof max === 'number' ? max : null,
+ };
+ } catch {
+ return null;
}
}
@@ -6150,7 +6252,15 @@ export class PostgresEngine implements BrainEngine {
params?: unknown[],
opts?: { signal?: AbortSignal },
): Promise<T[]> {
- return this.runUnsafe<T>(this.sql, sql, params, opts);
+ // try/finally (not .finally on the promise): runUnsafe throws
+ // SYNCHRONOUSLY on a pre-aborted signal, which would skip a chained
+ // .finally and leak the counter.
+ this.checkoutGauge.acquire('raw');
+ try {
+ return await this.runUnsafe<T>(this.sql, sql, params, opts);
+ } finally {
+ this.checkoutGauge.release('raw');
+ }
// Pre-#406 behavior: throw on any error including connection death.
// Per-call auto-retry is not safe here because executeRaw is also used
// for non-transactional mutations (DELETE/UPDATE/INSERT in sources.ts,
@@ -6186,7 +6296,13 @@ export class PostgresEngine implements BrainEngine {
const conn = (!inTransaction && this.connectionManager?.isDualPoolActive())
? await this.connectionManager.ddl()
: this.sql;
- return this.runUnsafe<T>(conn, sql, params, opts);
+ // try/finally, not .finally — see executeRaw (sync throw on pre-aborted signal).
+ this.checkoutGauge.acquire('direct');
+ try {
+ return await this.runUnsafe<T>(conn, sql, params, opts);
+ } finally {
+ this.checkoutGauge.release('direct');
+ }
}
// ============================================================
diff --git a/templates/bootstrap/template-repo/README.md b/templates/bootstrap/template-repo/README.md
index 048e6a0626..afaac7e884 100644
--- a/templates/bootstrap/template-repo/README.md
+++ b/templates/bootstrap/template-repo/README.md
@@ -1,6 +1,6 @@
# gbrain agent workspace — template
-<!-- gbrain-template-stamp: 0.46.0.0 -->
+<!-- gbrain-template-stamp: 0.46.1.0 -->
This repository is the **"Use this template"** distribution artifact for a
[gbrain](https://github.com/garrytan/gbrain) personal-agent workspace — the same
diff --git a/test/child-job-runner.test.ts b/test/child-job-runner.test.ts
new file mode 100644
index 0000000000..6bd55e3c50
--- /dev/null
+++ b/test/child-job-runner.test.ts
@@ -0,0 +1,305 @@
+/**
+ * issue #5 — `runJobInChild` against REAL child processes (.mjs harnesses run
+ * by process.execPath — the child-worker-supervisor.test.ts pattern).
+ *
+ * Paths pinned:
+ * - success outcome file → resolves with the handler result
+ * - error outcome file → throws the reconstructed error class (exit 0!)
+ * - exit 1 with no file → generic throw naming the exit (attempt burned)
+ * - SIGTERM-ignoring child + aborted signal → group SIGKILL at the injected
+ * grace; "terminated after abort" classification
+ * - pre-aborted signal → child killed promptly
+ * - spawn ENOENT → ChildSpawnInfraError (release, no attempt burned)
+ * - worker-shutdown: child finishes + reports during the drain window →
+ * normal success; child that can't report → ChildWorkerShutdownError
+ * - child env contract (result path, lock token, parent pid, pool bounds)
+ */
+
+import { describe, expect, test, beforeAll, afterAll } from 'bun:test';
+import { mkdtempSync, writeFileSync, existsSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import {
+ runJobInChild,
+ ChildSpawnInfraError,
+ ChildWorkerShutdownError,
+ ChildNotClaimedError,
+} from '../src/core/minions/child-job-runner.ts';
+import { UnrecoverableError } from '../src/core/minions/types.ts';
+import { RateLeaseUnavailableError } from '../src/core/minions/handlers/subagent.ts';
+
+const TEST_TIMEOUT_MS = 30_000;
+
+let harnessDir: string;
+
+function makeHarness(name: string, body: string): string {
+ const path = join(harnessDir, `${name}.mjs`);
+ writeFileSync(
+ path,
+ `import { writeFileSync, renameSync } from 'node:fs';\n` +
+ `const RESULT = process.env.GBRAIN_JOB_RESULT_PATH;\n` +
+ `const writeOutcome = (o) => { writeFileSync(RESULT + '.tmp', JSON.stringify(o)); renameSync(RESULT + '.tmp', RESULT); };\n` +
+ body +
+ // Readiness handshake LAST (after the body installed its signal
+ // handlers): fixed sleeps raced child startup on loaded CI runners —
+ // a SIGTERM landing before process.on('SIGTERM') installs takes the
+ // default disposition and flips shutdown-semantics assertions
+ // (testing specialist). The runner's outcome dir is internal, so the
+ // ready path comes from a TEST-provided env var.
+ `\nif (process.env.HARNESS_READY_PATH) writeFileSync(process.env.HARNESS_READY_PATH, '1');\n`,
+ 'utf8',
+ );
+ return path;
+}
+
+/** Make a per-test ready path + the env to hand runJobInChild. */
+function readiness(name: string): { env: Record<string, string | undefined>; wait: () => Promise<void> } {
+ const readyPath = join(harnessDir, `${name}.ready`);
+ return {
+ env: { ...process.env, HARNESS_READY_PATH: readyPath },
+ wait: async () => {
+ const deadline = Date.now() + 10_000;
+ while (Date.now() < deadline) {
+ if (existsSync(readyPath)) return;
+ await new Promise((r) => setTimeout(r, 25));
+ }
+ throw new Error('harness never signalled readiness');
+ },
+ };
+}
+
+beforeAll(() => {
+ harnessDir = mkdtempSync(join(tmpdir(), 'gbrain-cjr-harness-'));
+});
+
+afterAll(() => {
+ rmSync(harnessDir, { recursive: true, force: true });
+});
+
+function baseOpts(harnessPath: string) {
+ return {
+ jobId: 77,
+ jobName: 'subagent',
+ lockToken: 'tok-cjr',
+ abortSignal: new AbortController().signal,
+ shutdownSignal: new AbortController().signal,
+ invocation: { cmd: process.execPath, argsPrefix: [harnessPath] },
+ tiniPath: '', // direct spawn in tests; group signaling covers both shapes
+ };
+}
+
+describe('runJobInChild (real children)', () => {
+ test('success outcome resolves with the result; env contract honored', async () => {
+ const harness = makeHarness(
+ 'success',
+ `writeOutcome({ outcome: 'success', result: {
+ echoedToken: process.env.GBRAIN_JOB_LOCK_TOKEN,
+ isChild: process.env.GBRAIN_JOB_CHILD,
+ parentPid: process.env.GBRAIN_JOB_PARENT_PID,
+ poolSize: process.env.GBRAIN_POOL_SIZE,
+ directPoolSize: process.env.GBRAIN_DIRECT_POOL_SIZE,
+ argv: process.argv.slice(2),
+ }});\n` +
+ `process.exit(0);\n`,
+ );
+ const result = (await runJobInChild(baseOpts(harness))) as Record<string, unknown>;
+ expect(result.echoedToken).toBe('tok-cjr');
+ expect(result.isChild).toBe('1');
+ expect(result.parentPid).toBe(String(process.pid));
+ expect(result.poolSize).toBe('3');
+ expect(result.directPoolSize).toBe('1'); // codex-2 #6: child direct pool bounded
+ expect(result.argv).toEqual(['jobs', 'run-child', '--job-id', '77']);
+ }, TEST_TIMEOUT_MS);
+
+ test('error outcome (exit 0) throws the reconstructed class', async () => {
+ const harness = makeHarness(
+ 'error-unrecoverable',
+ `writeOutcome({ outcome: 'error', errorKind: 'unrecoverable', message: 'bad schema, never retry' });\n` +
+ `process.exit(0);\n`,
+ );
+ await expect(runJobInChild(baseOpts(harness))).rejects.toBeInstanceOf(UnrecoverableError);
+ }, TEST_TIMEOUT_MS);
+
+ test('rate-lease outcome rebuilds RateLeaseUnavailableError with fields', async () => {
+ const harness = makeHarness(
+ 'error-lease',
+ `writeOutcome({ outcome: 'error', errorKind: 'rate_lease', message: 'lease full', lease: { key: 'anthropic', active: 4, max: 4 } });\n` +
+ `process.exit(0);\n`,
+ );
+ try {
+ await runJobInChild(baseOpts(harness));
+ throw new Error('should have thrown');
+ } catch (e) {
+ expect(e).toBeInstanceOf(RateLeaseUnavailableError);
+ expect((e as RateLeaseUnavailableError).key).toBe('anthropic');
+ }
+ }, TEST_TIMEOUT_MS);
+
+ test('exit 1 with no outcome file → generic throw naming the exit code', async () => {
+ const harness = makeHarness('crash', `process.exit(1);\n`);
+ await expect(runJobInChild(baseOpts(harness))).rejects.toThrow(/exit code=1/);
+ }, TEST_TIMEOUT_MS);
+
+ test('SIGTERM-ignoring child: abort → group SIGKILL at the injected grace', async () => {
+ const harness = makeHarness(
+ 'stubborn',
+ `process.on('SIGTERM', () => {});\n` +
+ `setInterval(() => {}, 1000);\n`, // never exits voluntarily
+ );
+ const abort = new AbortController();
+ const ready = readiness('stubborn');
+ const opts = { ...baseOpts(harness), abortSignal: abort.signal, killGraceMs: 400, env: ready.env };
+ const p = runJobInChild(opts);
+ await ready.wait();
+ abort.abort(new Error('timeout'));
+ const started = Date.now();
+ await expect(p).rejects.toThrow(/terminated after abort/);
+ // Died via the SIGKILL escalation, not the 30s force-evict scale.
+ expect(Date.now() - started).toBeLessThan(5_000);
+ }, TEST_TIMEOUT_MS);
+
+ test('pre-aborted signal: child is terminated promptly', async () => {
+ const harness = makeHarness(
+ 'prekilled',
+ `setInterval(() => {}, 1000);\n`,
+ );
+ const abort = new AbortController();
+ abort.abort(new Error('cancel'));
+ const opts = { ...baseOpts(harness), abortSignal: abort.signal, killGraceMs: 400 };
+ await expect(runJobInChild(opts)).rejects.toThrow(/terminated after abort/);
+ }, TEST_TIMEOUT_MS);
+
+ test('spawn ENOENT → ChildSpawnInfraError (infra release, not a job defect)', async () => {
+ const opts = {
+ ...baseOpts('/nonexistent'),
+ invocation: { cmd: '/nonexistent/gbrain-binary', argsPrefix: [] },
+ };
+ await expect(runJobInChild(opts)).rejects.toBeInstanceOf(ChildSpawnInfraError);
+ }, TEST_TIMEOUT_MS);
+
+ test('worker shutdown: child finishes + reports during the drain window → normal success', async () => {
+ const harness = makeHarness(
+ 'graceful-drain',
+ `let done = false;\n` +
+ `process.on('SIGTERM', () => {\n` +
+ ` writeOutcome({ outcome: 'success', result: { finishedDuringDrain: true } });\n` +
+ ` done = true; process.exit(0);\n` +
+ `});\n` +
+ `setInterval(() => {}, 1000);\n`,
+ );
+ const shutdown = new AbortController();
+ const ready = readiness('graceful-drain');
+ const opts = { ...baseOpts(harness), shutdownSignal: shutdown.signal, killGraceMs: 5_000, env: ready.env };
+ const p = runJobInChild(opts);
+ await ready.wait();
+ shutdown.abort(new Error('worker-shutdown'));
+ const result = (await p) as Record<string, unknown>;
+ expect(result.finishedDuringDrain).toBe(true);
+ }, TEST_TIMEOUT_MS);
+
+ test('worker shutdown: child that cannot report → ChildWorkerShutdownError (no attempt burned)', async () => {
+ const harness = makeHarness(
+ 'shutdown-stubborn',
+ `process.on('SIGTERM', () => {});\n` +
+ `setInterval(() => {}, 1000);\n`,
+ );
+ const shutdown = new AbortController();
+ const ready = readiness('shutdown-stubborn');
+ const opts = { ...baseOpts(harness), shutdownSignal: shutdown.signal, killGraceMs: 400, env: ready.env };
+ const p = runJobInChild(opts);
+ await ready.wait();
+ shutdown.abort(new Error('worker-shutdown'));
+ await expect(p).rejects.toBeInstanceOf(ChildWorkerShutdownError);
+ }, TEST_TIMEOUT_MS);
+
+ test('ERROR outcome during worker shutdown → ChildWorkerShutdownError, not a burned attempt (adversarial P2)', async () => {
+ // A cooperative handler that bails on shutdown and reports an error must
+ // be RELEASED — punishing exactly the well-behaved handlers on every
+ // deploy inverts the no-burn guarantee.
+ const harness = makeHarness(
+ 'shutdown-error-report',
+ `process.on('SIGTERM', () => {\n` +
+ ` writeOutcome({ outcome: 'error', errorKind: 'generic', message: 'aborted: shutdown' });\n` +
+ ` process.exit(0);\n` +
+ `});\n` +
+ `setInterval(() => {}, 1000);\n`,
+ );
+ const shutdown = new AbortController();
+ const ready = readiness('shutdown-error-report');
+ const opts = { ...baseOpts(harness), shutdownSignal: shutdown.signal, killGraceMs: 5_000, env: ready.env };
+ const p = runJobInChild(opts);
+ await ready.wait();
+ shutdown.abort(new Error('worker-shutdown'));
+ await expect(p).rejects.toBeInstanceOf(ChildWorkerShutdownError);
+ }, TEST_TIMEOUT_MS);
+
+ test('watchdog drain (BOTH signals aborted, non-per-job reason) → shutdown class, not a burned attempt (adversarial P3)', async () => {
+ const harness = makeHarness(
+ 'watchdog-stubborn',
+ `process.on('SIGTERM', () => {});\n` +
+ `setInterval(() => {}, 1000);\n`,
+ );
+ const abort = new AbortController();
+ const shutdown = new AbortController();
+ const ready = readiness('watchdog-stubborn');
+ const opts = {
+ ...baseOpts(harness),
+ abortSignal: abort.signal,
+ shutdownSignal: shutdown.signal,
+ killGraceMs: 400,
+ env: ready.env,
+ };
+ const p = runJobInChild(opts);
+ await ready.wait();
+ // gracefulShutdown('watchdog') aborts BOTH — shutdown classification must win.
+ shutdown.abort(new Error('watchdog'));
+ abort.abort(new Error('watchdog'));
+ await expect(p).rejects.toBeInstanceOf(ChildWorkerShutdownError);
+ }, TEST_TIMEOUT_MS);
+
+ test('per-job reason (timeout) wins over a concurrent shutdown — attempt semantics preserved', async () => {
+ const harness = makeHarness(
+ 'timeout-during-shutdown',
+ `process.on('SIGTERM', () => {});\n` +
+ `setInterval(() => {}, 1000);\n`,
+ );
+ const abort = new AbortController();
+ const shutdown = new AbortController();
+ const ready = readiness('timeout-during-shutdown');
+ const opts = {
+ ...baseOpts(harness),
+ abortSignal: abort.signal,
+ shutdownSignal: shutdown.signal,
+ killGraceMs: 400,
+ env: ready.env,
+ };
+ const p = runJobInChild(opts);
+ await ready.wait();
+ shutdown.abort(new Error('worker-shutdown'));
+ abort.abort(new Error('timeout')); // the JOB was targeted — not shutdown class
+ await expect(p).rejects.toThrow(/terminated after abort/);
+ }, TEST_TIMEOUT_MS);
+
+ test('bootstrap exit codes: 13 → ChildSpawnInfraError, 14 → ChildNotClaimedError (no burned attempts)', async () => {
+ const usage = makeHarness('exit13', `process.exit(13);\n`);
+ await expect(runJobInChild(baseOpts(usage))).rejects.toBeInstanceOf(ChildSpawnInfraError);
+
+ const notClaimed = makeHarness('exit14', `process.exit(14);\n`);
+ await expect(runJobInChild(baseOpts(notClaimed))).rejects.toBeInstanceOf(ChildNotClaimedError);
+ }, TEST_TIMEOUT_MS);
+
+ test('child pool-size env: a STRICTER user GBRAIN_POOL_SIZE is respected; explicit child override wins; invalid falls back', async () => {
+ const harness = makeHarness(
+ 'pool-echo',
+ `writeOutcome({ outcome: 'success', result: { poolSize: process.env.GBRAIN_POOL_SIZE } });\n` +
+ `process.exit(0);\n`,
+ );
+ const run = (env: Record<string, string | undefined>) =>
+ runJobInChild({ ...baseOpts(harness), env: { ...process.env, ...env } }) as Promise<{ poolSize: string }>;
+
+ expect((await run({ GBRAIN_POOL_SIZE: '2' })).poolSize).toBe('2'); // stricter user tuning respected
+ expect((await run({ GBRAIN_POOL_SIZE: '10' })).poolSize).toBe('3'); // never raised above the child default
+ expect((await run({ GBRAIN_POOL_SIZE: '2', GBRAIN_JOB_CHILD_POOL_SIZE: '5' })).poolSize).toBe('5'); // explicit knob wins
+ expect((await run({ GBRAIN_JOB_CHILD_POOL_SIZE: 'abc' })).poolSize).toBe('3'); // invalid → default
+ }, TEST_TIMEOUT_MS);
+});
diff --git a/test/cycle-drain-renewal.test.ts b/test/cycle-drain-renewal.test.ts
new file mode 100644
index 0000000000..d9cd00d1a6
--- /dev/null
+++ b/test/cycle-drain-renewal.test.ts
@@ -0,0 +1,110 @@
+/**
+ * issue #6 — `runDrainRenewalTick` (cycle drain lock-renewal tick, extracted
+ * from the inline setInterval in synthesize.ts). The previous inline tick had
+ * no per-call timeout: a hung renewLock stacked one checked-out pool slot per
+ * interval firing, forever. The extracted tick:
+ *
+ * - passes a per-call AbortSignal that is aborted when the timeout wins
+ * (the losing UPDATE is cancelled, its slot released)
+ * - invokes onLost exactly once when the token fence is lost (ok === false)
+ * - swallows errors and timeouts (best-effort; the next tick retries)
+ *
+ * Hermetic: injected renewLock, no DB, no real cycle.
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { readFileSync } from 'node:fs';
+import { runDrainRenewalTick } from '../src/core/cycle/synthesize.ts';
+
+describe('drain-loop wiring (structural — the shape guard only covers worker.ts)', () => {
+ const src = readFileSync(
+ new URL('../src/core/cycle/synthesize.ts', import.meta.url),
+ 'utf-8',
+ );
+ test('the renewTimer interval is guarded and routes through runDrainRenewalTick', () => {
+ expect(src).toContain('if (drainTickInFlight) return;');
+ expect(src).toContain('void runDrainRenewalTick(');
+ // The pre-fix inline shape (an unguarded queue.renewLock(...).then chain
+ // inside the interval) must not come back.
+ expect(src).not.toMatch(/setInterval\(\(\) => \{\s*\n\s*queue\.renewLock\(/);
+ });
+});
+
+describe('db-lock heartbeat wiring (structural — issue #6 cancellation)', () => {
+ const src = readFileSync(new URL('../src/core/db-lock.ts', import.meta.url), 'utf-8');
+ test('withRefreshingLock aborts a per-tick signal into handle.refresh and guards re-entrancy', () => {
+ expect(src).toContain('handle.refresh({ signal: tickAbort.signal })');
+ expect(src).toContain('if (refreshTickInFlight) return;');
+ // refresh() forwards the opts to executeRawDirect as the trailing arg.
+ expect(src).toMatch(/executeRawDirect<\{ id: string \}>\([\s\S]*?refreshOpts,\s*\)/);
+ });
+});
+
+describe('runDrainRenewalTick (issue #6)', () => {
+ test('successful renewal: signal not aborted, onLost not called', async () => {
+ let seenSignal: AbortSignal | undefined;
+ let lost = 0;
+ await runDrainRenewalTick(
+ async (_id, _tok, _ms, opts) => {
+ seenSignal = opts?.signal;
+ return true;
+ },
+ 42,
+ 'tok',
+ 30_000,
+ () => { lost += 1; },
+ 1_000,
+ );
+ expect(seenSignal).toBeInstanceOf(AbortSignal);
+ expect(seenSignal!.aborted).toBe(false);
+ expect(lost).toBe(0);
+ });
+
+ test('lost token fence (ok=false): onLost called once', async () => {
+ let lost = 0;
+ await runDrainRenewalTick(
+ async () => false,
+ 42,
+ 'tok',
+ 30_000,
+ () => { lost += 1; },
+ 1_000,
+ );
+ expect(lost).toBe(1);
+ });
+
+ test('hung renewLock: tick resolves at the timeout and aborts the per-call signal', async () => {
+ let seenSignal: AbortSignal | undefined;
+ let lost = 0;
+ const started = Date.now();
+ await runDrainRenewalTick(
+ (_id, _tok, _ms, opts) => {
+ seenSignal = opts?.signal;
+ return new Promise<boolean>(() => { /* hangs forever */ });
+ },
+ 42,
+ 'tok',
+ 30_000,
+ () => { lost += 1; },
+ 50, // short timeout keeps the test fast
+ );
+ // Resolved via the timeout path (not the hung renewal).
+ expect(Date.now() - started).toBeGreaterThanOrEqual(40);
+ expect(seenSignal).toBeInstanceOf(AbortSignal);
+ expect(seenSignal!.aborted).toBe(true);
+ expect(lost).toBe(0); // timeout is NOT a lost fence
+ });
+
+ test('throwing renewLock is swallowed (best-effort; next tick retries)', async () => {
+ let lost = 0;
+ await runDrainRenewalTick(
+ async () => { throw new Error('CONNECTION_ENDED'); },
+ 42,
+ 'tok',
+ 30_000,
+ () => { lost += 1; },
+ 1_000,
+ );
+ expect(lost).toBe(0);
+ });
+});
diff --git a/test/cycle-patterns-deadline-budget.test.ts b/test/cycle-patterns-deadline-budget.test.ts
index 62bdf6ffe4..48521895a7 100644
--- a/test/cycle-patterns-deadline-budget.test.ts
+++ b/test/cycle-patterns-deadline-budget.test.ts
@@ -120,12 +120,17 @@ describe('claim stamps timeout_at (deadlineAtMs ground truth)', () => {
describe('deadline plumbing wiring (structural)', () => {
const workerSrc = readFileSync(new URL('../src/core/minions/worker.ts', import.meta.url), 'utf-8');
+ // The context builder was extracted from executeJob into job-context.ts
+ // (shared with `jobs run-child` for process isolation) — the deadlineAtMs
+ // derivation lives there now; worker.ts calls buildJobContext.
+ const jobContextSrc = readFileSync(new URL('../src/core/minions/job-context.ts', import.meta.url), 'utf-8');
const jobsSrc = readFileSync(new URL('../src/commands/jobs.ts', import.meta.url), 'utf-8');
const cycleSrc = readFileSync(new URL('../src/core/cycle.ts', import.meta.url), 'utf-8');
const patternsSrc = readFileSync(new URL('../src/core/cycle/patterns.ts', import.meta.url), 'utf-8');
- test('worker exposes deadlineAtMs from the claim-time timeout_at stamp', () => {
- expect(workerSrc).toContain('deadlineAtMs: job.timeout_at != null ? job.timeout_at.getTime() : null');
+ test('job context exposes deadlineAtMs from the claim-time timeout_at stamp', () => {
+ expect(jobContextSrc).toContain('deadlineAtMs: job.timeout_at != null ? job.timeout_at.getTime() : null');
+ expect(workerSrc).toContain('buildJobContext(');
});
test('worker arms its abort timer from timeout_at when present (one absolute deadline)', () => {
diff --git a/test/db-pool-max-lifetime.test.ts b/test/db-pool-max-lifetime.test.ts
new file mode 100644
index 0000000000..fb3e93221d
--- /dev/null
+++ b/test/db-pool-max-lifetime.test.ts
@@ -0,0 +1,120 @@
+/**
+ * issue #6 — `resolveMaxLifetimeSeconds`: explicit, env-overridable
+ * max_lifetime for all four postgres() call sites.
+ *
+ * NOT a behavior change at default: postgres.js (verified against 3.4.9)
+ * already defaults max_lifetime to `60 * (30 + Math.random() * 30)`. This
+ * resolver makes the value explicit and adds the GBRAIN_POOL_MAX_LIFETIME_S
+ * incident escape hatch (0 disables recycling; N = seconds).
+ *
+ * Hermetic: resolver only — env injected as a param (rule R1), no pools.
+ */
+
+import { describe, expect, test, beforeEach } from 'bun:test';
+import {
+ resolveMaxLifetimeSeconds,
+ _resetMaxLifetimeWarningForTests,
+} from '../src/core/db.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+beforeEach(() => {
+ _resetMaxLifetimeWarningForTests();
+});
+
+describe('resolveMaxLifetimeSeconds', () => {
+ test('default (no env): a per-CONNECTION jitter FUNCTION, 30-60 minutes', () => {
+ // Must be a function, not a pre-evaluated number: postgres.js re-evaluates
+ // a function default per connection, so connections in one pool get
+ // independent recycle deadlines instead of a synchronized reconnect spike
+ // (data-migration specialist).
+ const v = resolveMaxLifetimeSeconds({});
+ expect(typeof v).toBe('function');
+ const fn = v as () => number;
+ for (let i = 0; i < 20; i++) {
+ const n = fn();
+ expect(Number.isInteger(n)).toBe(true);
+ expect(n).toBeGreaterThanOrEqual(1800);
+ expect(n).toBeLessThanOrEqual(3600);
+ }
+ });
+
+ test('env override: positive integer seconds honored verbatim', () => {
+ expect(resolveMaxLifetimeSeconds({ GBRAIN_POOL_MAX_LIFETIME_S: '900' })).toBe(900);
+ expect(resolveMaxLifetimeSeconds({ GBRAIN_POOL_MAX_LIFETIME_S: '1' })).toBe(1);
+ });
+
+ test('env 0 disables recycling (null — postgres.js accepts null)', () => {
+ expect(resolveMaxLifetimeSeconds({ GBRAIN_POOL_MAX_LIFETIME_S: '0' })).toBeNull();
+ });
+
+ test('empty string falls through to the default (function)', () => {
+ const v = resolveMaxLifetimeSeconds({ GBRAIN_POOL_MAX_LIFETIME_S: '' });
+ expect(typeof v).toBe('function');
+ });
+
+ test('invalid values warn once on stderr and fall back to the default', () => {
+ const writes: string[] = [];
+ const realWrite = process.stderr.write.bind(process.stderr);
+ (process.stderr as { write: unknown }).write = (chunk: string) => {
+ writes.push(String(chunk));
+ return true;
+ };
+ try {
+ for (const bad of ['abc', '-5', '3.5', 'NaN']) {
+ const v = resolveMaxLifetimeSeconds({ GBRAIN_POOL_MAX_LIFETIME_S: bad });
+ expect(typeof v).toBe('function');
+ }
+ } finally {
+ (process.stderr as { write: unknown }).write = realWrite;
+ }
+ // warn-once latch: 4 bad values, exactly 1 warning
+ const warnings = writes.filter((w) => w.includes('GBRAIN_POOL_MAX_LIFETIME_S'));
+ expect(warnings.length).toBe(1);
+ });
+
+ test('jitter varies per CONNECTION (thundering-herd protection)', () => {
+ const fn = resolveMaxLifetimeSeconds({}) as () => number;
+ const values = new Set<number>();
+ for (let i = 0; i < 30; i++) values.add(fn());
+ expect(values.size).toBeGreaterThan(1);
+ });
+
+ test('wiring: the env override reaches a REAL constructed pool (ConnectionManager read pool)', async () => {
+ // postgres() is lazy — constructing the pool performs no I/O, so this
+ // pins the construction seam without a database. Without this, the
+ // resolver could be green while GBRAIN_POOL_MAX_LIFETIME_S is silently
+ // dead at every call site (adversarial-review vacuity finding).
+ const { ConnectionManager } = await import('../src/core/connection-manager.ts');
+ const { endPoolBounded } = await import('../src/core/db.ts');
+ await withEnv({ GBRAIN_POOL_MAX_LIFETIME_S: '900' }, async () => {
+ const cm = new ConnectionManager({
+ url: 'postgresql://[email protected]:5/never-connected',
+ });
+ const pool = await cm.getReadPool();
+ try {
+ expect(
+ (pool as unknown as { options: { max_lifetime: number | null } }).options.max_lifetime,
+ ).toBe(900);
+ } finally {
+ await endPoolBounded(pool);
+ }
+ });
+ });
+
+ test('wiring: the default reaches a real pool as a FUNCTION (per-connection jitter)', async () => {
+ const { ConnectionManager } = await import('../src/core/connection-manager.ts');
+ const { endPoolBounded } = await import('../src/core/db.ts');
+ await withEnv({ GBRAIN_POOL_MAX_LIFETIME_S: undefined }, async () => {
+ const cm = new ConnectionManager({
+ url: 'postgresql://[email protected]:5/never-connected',
+ });
+ const pool = await cm.getReadPool();
+ try {
+ const v = (pool as unknown as { options: { max_lifetime: unknown } }).options.max_lifetime;
+ expect(typeof v).toBe('function');
+ } finally {
+ await endPoolBounded(pool);
+ }
+ });
+ });
+});
diff --git a/test/db-probe.test.ts b/test/db-probe.test.ts
new file mode 100644
index 0000000000..7cec886ad8
--- /dev/null
+++ b/test/db-probe.test.ts
@@ -0,0 +1,136 @@
+/**
+ * issue #6 — `runDbProbe` verdict matrix (hermetic; injected deps only).
+ *
+ * read OK → { ok: true }
+ * read FAIL + direct OK → pool_starved (honest disjunction wording)
+ * read FAIL + direct FAIL → server_unreachable
+ * read FAIL + no direct lane → unknown
+ * hung probes → cancelled via their AbortSignals
+ * diagnostics absent/throwing → fail-open (verdict still produced)
+ * tracked=0 subset → message points at untracked traffic
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { runDbProbe } from '../src/core/minions/db-probe.ts';
+
+const FAST = { timeoutMs: 5_000, directTimeoutMs: 5_000 };
+const SHORT = { timeoutMs: 50, directTimeoutMs: 50 };
+
+describe('runDbProbe verdict matrix', () => {
+ test('read OK → ok:true, no verdict', async () => {
+ const res = await runDbProbe({ probeRead: async () => {}, ...FAST });
+ expect(res).toEqual({ ok: true });
+ });
+
+ test('read FAIL + direct OK → pool_starved with honest-disjunction wording', async () => {
+ const res = await runDbProbe({
+ probeRead: async () => { throw new Error('probe timeout after 10000ms'); },
+ probeDirect: async () => {},
+ getDiagnostics: () => ({
+ tracked: { raw: 9, direct: 0, reserved: 1, tx: 0 },
+ poolMax: 10,
+ }),
+ ...FAST,
+ });
+ expect(res.ok).toBe(false);
+ if (res.ok) throw new Error('unreachable');
+ expect(res.verdict).toBe('pool_starved');
+ expect(res.detail).toContain('server IS reachable');
+ // Codex-2 #2: the message must NOT claim to distinguish client pool
+ // exhaustion from a pooler-layer fault.
+ expect(res.detail).toContain('client pool exhaustion or a pooler-layer fault');
+ expect(res.detail).toContain('raw=9');
+ expect(res.detail).toContain('read pool max 10');
+ // Codex-2 #3: no invented waiter arithmetic anywhere in the message.
+ expect(res.detail).not.toMatch(/waiting/i);
+ expect(res.detail).toContain('subset');
+ });
+
+ test('read FAIL + direct FAIL → server_unreachable', async () => {
+ const res = await runDbProbe({
+ probeRead: async () => { throw new Error('probe timeout after 10000ms'); },
+ probeDirect: async () => { throw new Error('connect ECONNREFUSED'); },
+ ...FAST,
+ });
+ expect(res.ok).toBe(false);
+ if (res.ok) throw new Error('unreachable');
+ expect(res.verdict).toBe('server_unreachable');
+ expect(res.detail).toContain('ECONNREFUSED');
+ });
+
+ test('read FAIL + no direct lane → unknown', async () => {
+ const res = await runDbProbe({
+ probeRead: async () => { throw new Error('boom'); },
+ ...FAST,
+ });
+ expect(res.ok).toBe(false);
+ if (res.ok) throw new Error('unreachable');
+ expect(res.verdict).toBe('unknown');
+ expect(res.detail).toContain('no direct lane');
+ });
+
+ test('hung read probe: cancelled via its signal at the deadline', async () => {
+ let readSignal: AbortSignal | undefined;
+ const res = await runDbProbe({
+ probeRead: (signal) => {
+ readSignal = signal;
+ return new Promise<never>(() => {});
+ },
+ ...SHORT,
+ });
+ expect(res.ok).toBe(false);
+ expect(readSignal?.aborted).toBe(true);
+ });
+
+ test('hung direct probe: cancelled at its own (shorter) deadline', async () => {
+ let directSignal: AbortSignal | undefined;
+ const res = await runDbProbe({
+ probeRead: async () => { throw new Error('read dead'); },
+ probeDirect: (signal) => {
+ directSignal = signal;
+ return new Promise<never>(() => {});
+ },
+ ...SHORT,
+ });
+ expect(res.ok).toBe(false);
+ if (res.ok) throw new Error('unreachable');
+ expect(res.verdict).toBe('server_unreachable');
+ expect(directSignal?.aborted).toBe(true);
+ });
+
+ test('tracked=0 subset: message names untracked template traffic + runbook', async () => {
+ const res = await runDbProbe({
+ probeRead: async () => { throw new Error('probe timeout'); },
+ probeDirect: async () => {},
+ getDiagnostics: () => ({
+ tracked: { raw: 0, direct: 0, reserved: 0, tx: 0 },
+ poolMax: 10,
+ }),
+ ...FAST,
+ });
+ if (res.ok) throw new Error('unreachable');
+ expect(res.detail).toContain('untracked');
+ expect(res.detail).toContain('queue-operations-runbook');
+ });
+
+ test('diagnostics absent → verdict still produced (fail-open)', async () => {
+ const res = await runDbProbe({
+ probeRead: async () => { throw new Error('probe timeout'); },
+ probeDirect: async () => {},
+ ...FAST,
+ });
+ if (res.ok) throw new Error('unreachable');
+ expect(res.verdict).toBe('pool_starved');
+ });
+
+ test('diagnostics THROWING → verdict still produced (fail-open)', async () => {
+ const res = await runDbProbe({
+ probeRead: async () => { throw new Error('probe timeout'); },
+ probeDirect: async () => {},
+ getDiagnostics: () => { throw new Error('gauge exploded'); },
+ ...FAST,
+ });
+ if (res.ok) throw new Error('unreachable');
+ expect(res.verdict).toBe('pool_starved');
+ });
+});
diff --git a/test/e2e/job-isolation.test.ts b/test/e2e/job-isolation.test.ts
new file mode 100644
index 0000000000..30fb99081f
--- /dev/null
+++ b/test/e2e/job-isolation.test.ts
@@ -0,0 +1,133 @@
+/**
+ * issue #5 — process isolation against REAL Postgres (DATABASE_URL-gated;
+ * wired into .github/workflows/e2e.yml tier1 — e2e.yml runs only explicitly
+ * NAMED files, so an unwired e2e file is silent coverage loss).
+ *
+ * Legs:
+ * 1. Worker-level concurrency (codex-2 #6): a real MinionWorker with
+ * isolation on drains 6 jobs at concurrency 3 through real child
+ * processes against the real pooler/DB — the child-pool topology the
+ * per-process pool math describes.
+ * 2. Real CLI entrypoint: `bun src/cli.ts jobs run-child` with the env
+ * contract against a genuinely claimed job — proves engine bootstrap,
+ * handler registry (quiet), token validation, and the outcome protocol
+ * end-to-end. Uses the real 'orphans' handler (cheap on a fresh DB).
+ * 3. Stuck-child kill: a SIGTERM-ignoring child is group-SIGKILLed and the
+ * worker keeps claiming.
+ */
+
+import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
+import { spawnSync } from 'node:child_process';
+import { mkdtempSync, rmSync, existsSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, resolve } from 'node:path';
+import { hasDatabase, setupDB, teardownDB, getEngine } from './helpers.ts';
+import { MinionQueue } from '../../src/core/minions/queue.ts';
+import { MinionWorker } from '../../src/core/minions/worker.ts';
+import { decodeChildOutcomeFile, CHILD_ENV } from '../../src/core/minions/job-isolation.ts';
+import { withEnv } from '../helpers/with-env.ts';
+
+const FIXTURE = resolve(import.meta.dir, '..', 'fixtures', 'fake-run-child.mjs');
+const CLI = resolve(import.meta.dir, '..', '..', 'src', 'cli.ts');
+
+const describeDb = hasDatabase() ? describe : describe.skip;
+
+let queue: MinionQueue;
+let tmp: string;
+
+beforeAll(async () => {
+ if (!hasDatabase()) return;
+ await setupDB();
+ queue = new MinionQueue(getEngine());
+ tmp = mkdtempSync(join(tmpdir(), 'gbrain-e2e-isolation-'));
+});
+
+afterAll(async () => {
+ if (!hasDatabase()) return;
+ await teardownDB();
+ if (tmp) rmSync(tmp, { recursive: true, force: true });
+});
+
+async function rowStatus(id: number): Promise<string> {
+ const rows = await getEngine().executeRaw<{ status: string }>(
+ 'SELECT status FROM minion_jobs WHERE id = $1',
+ [id],
+ );
+ return rows[0]?.status ?? 'missing';
+}
+
+describeDb('process isolation on real Postgres', () => {
+ test('concurrency 3: six isolated jobs drain through real children', async () => {
+ await withEnv({ FAKE_RUN_CHILD_MODE: 'success' }, async () => {
+ const jobs = [];
+ for (let i = 0; i < 6; i++) jobs.push(await queue.add('isotest', { i }));
+
+ const worker = new MinionWorker(getEngine(), {
+ queue: 'default',
+ concurrency: 3,
+ pollInterval: 50,
+ healthCheckInterval: 0,
+ maxRssMb: 0,
+ jobIsolation: 'process',
+ childCliInvocation: { cmd: process.execPath, argsPrefix: [FIXTURE] },
+ });
+ worker.register('isotest', async () => {
+ throw new Error('parent handler must not run when isolated');
+ });
+
+ const run = worker.start();
+ const deadline = Date.now() + 30_000;
+ try {
+ while (Date.now() < deadline) {
+ const statuses = await Promise.all(jobs.map((j) => rowStatus(j.id)));
+ if (statuses.every((s) => s === 'completed')) break;
+ await new Promise((r) => setTimeout(r, 100));
+ }
+ } finally {
+ worker.stop();
+ await run;
+ }
+ const statuses = await Promise.all(jobs.map((j) => rowStatus(j.id)));
+ expect(statuses).toEqual(['completed', 'completed', 'completed', 'completed', 'completed', 'completed']);
+ });
+ }, 60_000);
+
+ test('real CLI run-child: engine bootstrap + protocol end-to-end', async () => {
+ const job = await queue.add('orphans', {});
+ const claimed = await queue.claim('e2e-cli-tok', 60_000, 'default', ['orphans']);
+ expect(claimed?.id).toBe(job.id);
+
+ const resultPath = join(tmp, `cli-${job.id}.json`);
+ const res = spawnSync(
+ process.execPath,
+ [CLI, 'jobs', 'run-child', '--job-id', String(job.id)],
+ {
+ env: {
+ ...process.env,
+ GBRAIN_DATABASE_URL: process.env.DATABASE_URL,
+ GBRAIN_TEST_ALLOW_DATABASE_URL: '1',
+ [CHILD_ENV.lockToken]: 'e2e-cli-tok',
+ [CHILD_ENV.resultPath]: resultPath,
+ [CHILD_ENV.parentPid]: String(process.pid),
+ },
+ timeout: 60_000,
+ stdio: ['ignore', 'pipe', 'pipe'],
+ },
+ );
+ if (res.status !== 0) {
+ throw new Error(
+ `run-child exited ${res.status}: ${String(res.stderr)}\n${String(res.stdout)}`,
+ );
+ }
+ expect(existsSync(resultPath)).toBe(true);
+ const outcome = decodeChildOutcomeFile(resultPath);
+ // The protocol completed; the real handler's own verdict (success or a
+ // reported error on a bare DB) is out of scope for this leg.
+ expect(outcome.outcome === 'success' || outcome.outcome === 'error').toBe(true);
+ }, 90_000);
+
+ // The stuck-child group-SIGKILL path is pinned with REAL processes in
+ // test/child-job-runner.test.ts (SIGTERM-ignorer → SIGKILL at grace) and
+ // the crash/burn semantics in test/worker-job-isolation.test.ts — this
+ // lane deliberately doesn't duplicate them against the shared CI DB.
+});
diff --git a/test/fixtures/fake-run-child.mjs b/test/fixtures/fake-run-child.mjs
new file mode 100644
index 0000000000..617f798ccc
--- /dev/null
+++ b/test/fixtures/fake-run-child.mjs
@@ -0,0 +1,48 @@
+// Fake `jobs run-child` for the isolation tests (worker-job-isolation.test.ts
+// + test/e2e/job-isolation.test.ts): honors the isolation env contract
+// without needing a compiled gbrain binary or a Postgres engine.
+// Mode via FAKE_RUN_CHILD_MODE: success | error | exit15 | crash.
+import { writeFileSync, renameSync } from 'node:fs';
+
+const resultPath = process.env.GBRAIN_JOB_RESULT_PATH;
+const mode = process.env.FAKE_RUN_CHILD_MODE ?? 'success';
+
+function writeOutcome(o) {
+ writeFileSync(resultPath + '.tmp', JSON.stringify(o));
+ renameSync(resultPath + '.tmp', resultPath);
+}
+
+if (!resultPath) {
+ process.stderr.write('[fake-run-child] missing GBRAIN_JOB_RESULT_PATH\n');
+ process.exit(13);
+}
+
+if (mode === 'success') {
+ writeOutcome({
+ outcome: 'success',
+ result: {
+ fromChild: true,
+ token: process.env.GBRAIN_JOB_LOCK_TOKEN ?? null,
+ argv: process.argv.slice(2),
+ },
+ });
+ process.exit(0);
+}
+
+if (mode === 'error') {
+ writeOutcome({
+ outcome: 'error',
+ errorKind: 'generic',
+ message: 'fake child handler failure',
+ });
+ process.exit(0);
+}
+
+if (mode === 'exit15') {
+ // Simulates a result-write failure (JOB_CHILD_EXIT_RESULT_WRITE_FAILED):
+ // handler ran, outcome could not be persisted.
+ process.exit(15);
+}
+
+// crash: no outcome file
+process.exit(1);
diff --git a/test/job-isolation-protocol.test.ts b/test/job-isolation-protocol.test.ts
new file mode 100644
index 0000000000..d53999c315
--- /dev/null
+++ b/test/job-isolation-protocol.test.ts
@@ -0,0 +1,256 @@
+/**
+ * issue #5 — job-isolation protocol unit tests.
+ *
+ * Covers: outcome round-trip, missing/malformed/oversized decode paths
+ * (oversize → UnrecoverableError: deterministic dead on attempt 1, never
+ * silent truncation), instanceof reconstruction for the two error classes
+ * executeJob branches on, child-CLI invocation resolution (env override /
+ * binary / bun-dev fallback / fail-fast null), and killProcessGroup against
+ * REAL detached processes — including the grandchild-death guarantee that
+ * motivated group signaling (SIGKILL on a wrapper pid alone orphans the
+ * handler; Bun rejects negative pids so the /bin/kill fallback is what
+ * actually runs under `bun test`, making this a real-runtime regression
+ * test for oven-sh/bun#15791).
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { spawn } from 'node:child_process';
+import { mkdtempSync, readFileSync, writeFileSync, existsSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import {
+ CHILD_OUTCOME_MAX_BYTES,
+ buildChildArgs,
+ decodeChildOutcomeFile,
+ encodeHandlerError,
+ killProcessGroup,
+ reconstructHandlerError,
+ resolveChildCliInvocation,
+ writeChildOutcomeFile,
+ type ChildOutcome,
+} from '../src/core/minions/job-isolation.ts';
+import { UnrecoverableError } from '../src/core/minions/types.ts';
+import { RateLeaseUnavailableError } from '../src/core/minions/handlers/subagent.ts';
+
+function tmpFile(name: string): { dir: string; path: string } {
+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-isolation-test-'));
+ return { dir, path: join(dir, name) };
+}
+
+describe('outcome file round-trip', () => {
+ test('success outcome survives write + decode; write is atomic (no .tmp left)', () => {
+ const { dir, path } = tmpFile('outcome.json');
+ try {
+ writeChildOutcomeFile(path, { outcome: 'success', result: { pages: 3, ok: true } });
+ expect(existsSync(`${path}.tmp`)).toBe(false);
+ const decoded = decodeChildOutcomeFile(path);
+ expect(decoded).toEqual({ outcome: 'success', result: { pages: 3, ok: true } });
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ test('missing file → generic Error naming the crash class', () => {
+ const { dir, path } = tmpFile('never-written.json');
+ try {
+ expect(() => decodeChildOutcomeFile(path)).toThrow(/without writing its outcome file/);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ test('malformed JSON → generic Error with byte count, NEVER file content', () => {
+ const { dir, path } = tmpFile('garbage.json');
+ try {
+ writeFileSync(path, 'sk-secret-key-do-not-leak {{{', 'utf8');
+ try {
+ decodeChildOutcomeFile(path);
+ throw new Error('should have thrown');
+ } catch (e) {
+ const msg = (e as Error).message;
+ expect(msg).toContain('not valid JSON');
+ expect(msg).toContain('bytes');
+ expect(msg).not.toContain('sk-secret');
+ }
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ test('oversize file → UnrecoverableError naming the cap (dead on attempt 1)', () => {
+ const { dir, path } = tmpFile('huge.json');
+ try {
+ writeFileSync(path, '{"outcome":"success","result":"xx"}', 'utf8');
+ try {
+ decodeChildOutcomeFile(path, 16); // tiny injected cap keeps the test fast
+ throw new Error('should have thrown');
+ } catch (e) {
+ expect(e).toBeInstanceOf(UnrecoverableError);
+ expect((e as Error).message).toContain('outcome cap');
+ }
+ // Default cap sanity.
+ expect(CHILD_OUTCOME_MAX_BYTES).toBe(32 * 1024 * 1024);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ test('unrecognized shape → generic Error (byte count only)', () => {
+ const { dir, path } = tmpFile('weird.json');
+ try {
+ writeFileSync(path, '{"totally":"unrelated"}', 'utf8');
+ expect(() => decodeChildOutcomeFile(path)).toThrow(/unrecognized shape/);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+});
+
+describe('handler-error encode → reconstruct (instanceof parity with inline mode)', () => {
+ test('UnrecoverableError survives the boundary', () => {
+ const enc = encodeHandlerError(new UnrecoverableError('bad config, never retry'));
+ expect(enc.outcome).toBe('error');
+ const rebuilt = reconstructHandlerError(enc as Extract<ChildOutcome, { outcome: 'error' }>);
+ expect(rebuilt).toBeInstanceOf(UnrecoverableError);
+ expect(rebuilt.message).toBe('bad config, never retry');
+ });
+
+ test('RateLeaseUnavailableError survives with lease fields', () => {
+ const enc = encodeHandlerError(new RateLeaseUnavailableError('anthropic', 4, 4));
+ const rebuilt = reconstructHandlerError(enc as Extract<ChildOutcome, { outcome: 'error' }>);
+ expect(rebuilt).toBeInstanceOf(RateLeaseUnavailableError);
+ const lease = rebuilt as RateLeaseUnavailableError;
+ expect(lease.key).toBe('anthropic');
+ expect(lease.active).toBe(4);
+ expect(lease.max).toBe(4);
+ });
+
+ test('generic Error carries message + child stack; unknown kinds degrade to generic', () => {
+ const boom = new Error('handler exploded');
+ const enc = encodeHandlerError(boom) as Extract<ChildOutcome, { outcome: 'error' }>;
+ const rebuilt = reconstructHandlerError(enc);
+ expect(rebuilt).toBeInstanceOf(Error);
+ expect(rebuilt).not.toBeInstanceOf(UnrecoverableError);
+ expect(rebuilt.message).toBe('handler exploded');
+ expect((rebuilt as Error & { childStack?: string }).childStack).toContain('handler exploded');
+
+ const weird = reconstructHandlerError({
+ outcome: 'error',
+ errorKind: 'generic',
+ message: 'from a hostile file',
+ });
+ expect(weird).toBeInstanceOf(Error);
+ expect(weird).not.toBeInstanceOf(UnrecoverableError);
+ });
+
+ test('non-Error throws (strings) encode without crashing', () => {
+ const enc = encodeHandlerError('plain string throw');
+ expect(enc.outcome).toBe('error');
+ if (enc.outcome === 'error') expect(enc.message).toBe('plain string throw');
+ });
+});
+
+describe('child CLI invocation resolution', () => {
+ test('env override wins', () => {
+ const inv = resolveChildCliInvocation(
+ { GBRAIN_JOB_CHILD_CLI: '/opt/custom/gbrain' },
+ '/usr/bin/bun',
+ '/repo/src/cli.ts',
+ () => '/usr/local/bin/gbrain',
+ );
+ expect(inv).toEqual({ cmd: '/opt/custom/gbrain', argsPrefix: [] });
+ });
+
+ test('compiled binary next', () => {
+ const inv = resolveChildCliInvocation({}, '/usr/bin/bun', '/repo/src/cli.ts', () => '/usr/local/bin/gbrain');
+ expect(inv).toEqual({ cmd: '/usr/local/bin/gbrain', argsPrefix: [] });
+ });
+
+ test('bun-dev fallback when no binary resolves', () => {
+ const inv = resolveChildCliInvocation({}, '/usr/bin/bun', '/repo/src/cli.ts', () => null);
+ expect(inv).toEqual({ cmd: '/usr/bin/bun', argsPrefix: ['/repo/src/cli.ts'] });
+ });
+
+ test('nothing resolves → null (caller must fail fast at startup)', () => {
+ const inv = resolveChildCliInvocation({}, '/usr/bin/bun', '/repo/dist/other.js', () => null);
+ expect(inv).toBeNull();
+ });
+
+ test('throwing binary resolver falls through to the dev fallback', () => {
+ const inv = resolveChildCliInvocation({}, '/usr/bin/bun', '/repo/src/cli.ts', () => {
+ throw new Error('which failed');
+ });
+ expect(inv).toEqual({ cmd: '/usr/bin/bun', argsPrefix: ['/repo/src/cli.ts'] });
+ });
+
+ test('child argv shape', () => {
+ expect(buildChildArgs(42)).toEqual(['jobs', 'run-child', '--job-id', '42']);
+ });
+});
+
+describe('killProcessGroup (real detached processes; Bun negative-pid fallback)', () => {
+ async function waitFor(cond: () => boolean, ms: number): Promise<boolean> {
+ const deadline = Date.now() + ms;
+ while (Date.now() < deadline) {
+ if (cond()) return true;
+ await new Promise((r) => setTimeout(r, 25));
+ }
+ return cond();
+ }
+
+ function alive(pid: number): boolean {
+ try {
+ process.kill(pid, 0);
+ return true;
+ } catch {
+ return false;
+ }
+ }
+
+ test('group SIGKILL kills the child AND its grandchild', async () => {
+ const { dir, path: pidFile } = tmpFile('grandchild.pid');
+ try {
+ // Child shell spawns a long-lived grandchild and records its pid.
+ const child = spawn('/bin/sh', ['-c', `sleep 300 & echo $! > ${pidFile}; wait`], {
+ detached: true,
+ stdio: 'ignore',
+ });
+ expect(child.pid).toBeGreaterThan(0);
+ const gotPid = await waitFor(() => existsSync(pidFile) && readFileSync(pidFile, 'utf8').trim() !== '', 3_000);
+ expect(gotPid).toBe(true);
+ const grandchildPid = parseInt(readFileSync(pidFile, 'utf8').trim(), 10);
+ expect(alive(child.pid!)).toBe(true);
+ expect(alive(grandchildPid)).toBe(true);
+
+ killProcessGroup(child.pid!, 'SIGKILL');
+
+ const bothDead = await waitFor(() => !alive(child.pid!) && !alive(grandchildPid), 3_000);
+ expect(bothDead).toBe(true);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ }, 15_000);
+
+ test('SIGTERM delivery to a live group returns true; dead group returns false', async () => {
+ const child = spawn('/bin/sh', ['-c', 'sleep 300'], { detached: true, stdio: 'ignore' });
+ const delivered = killProcessGroup(child.pid!, 'SIGTERM');
+ expect(delivered).toBe(true);
+ await waitFor(() => {
+ try {
+ process.kill(child.pid!, 0);
+ return false;
+ } catch {
+ return true;
+ }
+ }, 3_000);
+ // Group is gone now — a second signal reports not-delivered.
+ expect(killProcessGroup(child.pid!, 'SIGKILL')).toBe(false);
+ }, 15_000);
+
+ test('nonsense pids are refused without throwing', () => {
+ expect(killProcessGroup(0, 'SIGTERM')).toBe(false);
+ expect(killProcessGroup(1, 'SIGTERM')).toBe(false);
+ expect(killProcessGroup(-5, 'SIGTERM')).toBe(false);
+ expect(killProcessGroup(1.5 as unknown as number, 'SIGTERM')).toBe(false);
+ });
+});
diff --git a/test/jobs-isolation-flag.test.ts b/test/jobs-isolation-flag.test.ts
new file mode 100644
index 0000000000..e7a2a32df9
--- /dev/null
+++ b/test/jobs-isolation-flag.test.ts
@@ -0,0 +1,38 @@
+/**
+ * issue #5 — `parseJobIsolationFlag` (jobs-nice-flag.test.ts pattern: pure
+ * parser, env injected as the 2nd param so tests never mutate process.env).
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { parseJobIsolationFlag } from '../src/commands/jobs.ts';
+
+describe('parseJobIsolationFlag', () => {
+ test('default: inline', () => {
+ expect(parseJobIsolationFlag([], {})).toBe('inline');
+ });
+
+ test('space form', () => {
+ expect(parseJobIsolationFlag(['--job-isolation', 'process'], {})).toBe('process');
+ expect(parseJobIsolationFlag(['--job-isolation', 'inline'], {})).toBe('inline');
+ });
+
+ test('= form', () => {
+ expect(parseJobIsolationFlag(['--job-isolation=process'], {})).toBe('process');
+ expect(parseJobIsolationFlag(['--job-isolation=inline'], {})).toBe('inline');
+ });
+
+ test('env fallback, flag wins over env', () => {
+ expect(parseJobIsolationFlag([], { GBRAIN_JOB_ISOLATION: 'process' })).toBe('process');
+ expect(
+ parseJobIsolationFlag(['--job-isolation', 'inline'], { GBRAIN_JOB_ISOLATION: 'process' }),
+ ).toBe('inline');
+ });
+
+ test('empty env value falls through to the default', () => {
+ expect(parseJobIsolationFlag([], { GBRAIN_JOB_ISOLATION: '' })).toBe('inline');
+ });
+
+ test('other flags are untouched', () => {
+ expect(parseJobIsolationFlag(['--queue', 'q', '--concurrency', '3'], {})).toBe('inline');
+ });
+});
diff --git a/test/jobs-subcommand-help.serial.test.ts b/test/jobs-subcommand-help.serial.test.ts
index 7141ae4cfa..3a30e8ec2c 100644
--- a/test/jobs-subcommand-help.serial.test.ts
+++ b/test/jobs-subcommand-help.serial.test.ts
@@ -74,6 +74,9 @@ describe('jobs --help and jobs <subcommand> --help print real help, never the st
expect(out).toContain('--max-rss');
expect(out).toContain('--health-interval');
expect(out).toContain('GBRAIN_WORKER_CONCURRENCY');
+ // issue #5 process isolation: flag + env fallback documented.
+ expect(out).toContain('--job-isolation');
+ expect(out).toContain('GBRAIN_JOB_ISOLATION');
expect(out).not.toContain(STUB_MARKER);
// A real `jobs work` on this fixture would refuse (thin-client/engine
// path) or start a daemon; either output would differ from the help.
diff --git a/test/minion-queue-renewlock-signal.test.ts b/test/minion-queue-renewlock-signal.test.ts
new file mode 100644
index 0000000000..6d3a0fd2ee
--- /dev/null
+++ b/test/minion-queue-renewlock-signal.test.ts
@@ -0,0 +1,71 @@
+/**
+ * issue #6 — `MinionQueue.renewLock` forwards its optional AbortSignal to
+ * `engine.executeRawDirect` so the lock-renewal tick's timeout race can
+ * CANCEL a hung UPDATE (postgres.js `.cancel()`) instead of abandoning it on
+ * a checked-out pool slot.
+ *
+ * Hermetic: stub engine object literal (`as unknown as BrainEngine`), no DB —
+ * the worker-conn-resilience-1720 pattern.
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { MinionQueue } from '../src/core/minions/queue.ts';
+import type { BrainEngine } from '../src/core/engine.ts';
+
+function makeCaptureEngine() {
+ const calls: Array<{
+ sql: string;
+ params: unknown[] | undefined;
+ opts: { signal?: AbortSignal } | undefined;
+ }> = [];
+ const engine = {
+ kind: 'postgres',
+ executeRawDirect: async (
+ sql: string,
+ params?: unknown[],
+ opts?: { signal?: AbortSignal },
+ ) => {
+ calls.push({ sql, params, opts });
+ return [{ id: 1 }];
+ },
+ } as unknown as BrainEngine;
+ return { engine, calls };
+}
+
+describe('MinionQueue.renewLock signal forwarding (issue #6)', () => {
+ test('forwards opts.signal to executeRawDirect', async () => {
+ const { engine, calls } = makeCaptureEngine();
+ const queue = new MinionQueue(engine);
+ const ac = new AbortController();
+
+ const ok = await queue.renewLock(7, 'tok-xyz', 30_000, { signal: ac.signal });
+
+ expect(ok).toBe(true);
+ expect(calls.length).toBe(1);
+ expect(calls[0].sql).toContain('UPDATE minion_jobs SET lock_until');
+ expect(calls[0].params).toEqual([30_000, 7, 'tok-xyz']);
+ expect(calls[0].opts?.signal).toBe(ac.signal);
+ });
+
+ test('legacy 3-arg call still works (opts undefined)', async () => {
+ const { engine, calls } = makeCaptureEngine();
+ const queue = new MinionQueue(engine);
+
+ const ok = await queue.renewLock(7, 'tok-xyz', 30_000);
+
+ expect(ok).toBe(true);
+ expect(calls[0].opts).toBeUndefined();
+ });
+
+ test('token-fence miss returns false regardless of signal', async () => {
+ const engine = {
+ kind: 'postgres',
+ executeRawDirect: async () => [],
+ } as unknown as BrainEngine;
+ const queue = new MinionQueue(engine);
+ const ac = new AbortController();
+
+ const ok = await queue.renewLock(7, 'tok-stale', 30_000, { signal: ac.signal });
+ expect(ok).toBe(false);
+ });
+});
diff --git a/test/pool-gauge.test.ts b/test/pool-gauge.test.ts
new file mode 100644
index 0000000000..e75dd826d9
--- /dev/null
+++ b/test/pool-gauge.test.ts
@@ -0,0 +1,182 @@
+/**
+ * issue #6 — CheckoutGauge (pure) + the PostgresEngine gauge seams.
+ *
+ * The leak guard matters most: a counter that isn't released on a THROWING
+ * query drifts upward forever and turns the diagnostics into fiction. The
+ * engine seams use the Object.create(PostgresEngine.prototype) + fake-pool
+ * pattern (no real DB).
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { CheckoutGauge } from '../src/core/pool-gauge.ts';
+import { PostgresEngine } from '../src/core/postgres-engine.ts';
+
+describe('CheckoutGauge (pure)', () => {
+ test('acquire/release round-trip per kind', () => {
+ const g = new CheckoutGauge();
+ g.acquire('raw');
+ g.acquire('raw');
+ g.acquire('direct');
+ g.acquire('reserved');
+ g.acquire('tx');
+ expect(g.snapshot()).toEqual({ raw: 2, direct: 1, reserved: 1, tx: 1 });
+ g.release('raw');
+ g.release('direct');
+ g.release('reserved');
+ g.release('tx');
+ expect(g.snapshot()).toEqual({ raw: 1, direct: 0, reserved: 0, tx: 0 });
+ });
+
+ test('release clamps at zero (a missed acquire never underflows)', () => {
+ const g = new CheckoutGauge();
+ g.release('raw');
+ g.release('tx');
+ expect(g.snapshot()).toEqual({ raw: 0, direct: 0, reserved: 0, tx: 0 });
+ });
+
+ test('snapshot is a copy, not a live reference', () => {
+ const g = new CheckoutGauge();
+ const snap = g.snapshot();
+ g.acquire('raw');
+ expect(snap.raw).toBe(0);
+ });
+});
+
+// --- engine seams (fake pool, no DB) ---------------------------------------
+
+interface FakePoolBehavior {
+ /** What conn.unsafe returns per call. */
+ unsafe: (sql: string, params?: unknown[]) => unknown;
+}
+
+function makeEngine(behavior: FakePoolBehavior): {
+ engine: PostgresEngine;
+ diagnostics: () => { tracked: Record<string, number>; poolMax: number | null } | null;
+} {
+ const fakePool = {
+ unsafe: behavior.unsafe,
+ options: { max: 10 },
+ };
+ const engine = Object.create(PostgresEngine.prototype) as PostgresEngine;
+ Object.defineProperty(engine, 'sql', { get: () => fakePool });
+ Object.defineProperty(engine, '_sql', { value: fakePool, writable: true });
+ // Fresh gauge per fake engine (the real field initializer doesn't run for
+ // Object.create instances).
+ Object.defineProperty(engine, 'checkoutGauge', {
+ value: new CheckoutGauge(),
+ writable: true,
+ });
+ return {
+ engine,
+ diagnostics: () =>
+ (engine as unknown as {
+ getPoolDiagnostics: () => { tracked: Record<string, number>; poolMax: number | null } | null;
+ }).getPoolDiagnostics(),
+ };
+}
+
+describe('PostgresEngine gauge seams (fake pool)', () => {
+ test('executeRaw: counted while in flight, released on resolve', async () => {
+ let resolveQuery: (rows: unknown[]) => void = () => {};
+ const { engine, diagnostics } = makeEngine({
+ unsafe: () => new Promise((r) => { resolveQuery = r; }),
+ });
+
+ const p = engine.executeRaw('SELECT 42');
+ expect(diagnostics()?.tracked.raw).toBe(1);
+ expect(diagnostics()?.poolMax).toBe(10);
+ resolveQuery([]);
+ await p;
+ expect(diagnostics()?.tracked.raw).toBe(0);
+ });
+
+ test('executeRaw: released on REJECTED query (leak guard)', async () => {
+ const { engine, diagnostics } = makeEngine({
+ unsafe: () => Promise.reject(new Error('query was cancelled')),
+ });
+ await expect(engine.executeRaw('SELECT 42')).rejects.toThrow('query was cancelled');
+ expect(diagnostics()?.tracked.raw).toBe(0);
+ });
+
+ test('executeRaw: released on SYNCHRONOUS throw (pre-aborted signal)', async () => {
+ const { engine, diagnostics } = makeEngine({
+ unsafe: () => Promise.resolve([]),
+ });
+ const ac = new AbortController();
+ ac.abort();
+ await expect(
+ engine.executeRaw('SELECT 42', undefined, { signal: ac.signal }),
+ ).rejects.toThrow();
+ expect(diagnostics()?.tracked.raw).toBe(0);
+ });
+
+ test('getPoolDiagnostics is fail-open (no pool → null, no throw)', () => {
+ const engine = Object.create(PostgresEngine.prototype) as PostgresEngine;
+ // No sql defined — the getter on the prototype will throw internally.
+ const diag = (engine as unknown as {
+ getPoolDiagnostics: () => unknown;
+ }).getPoolDiagnostics();
+ expect(diag).toBeNull();
+ });
+});
+
+describe('PostgresEngine gauge seams: direct + tx (coverage-audit gaps)', () => {
+ test('executeRawDirect: counted as direct while in flight, released on resolve and reject', async () => {
+ let resolveQuery: (rows: unknown[]) => void = () => {};
+ const { engine, diagnostics } = makeEngine({
+ unsafe: () => new Promise((r) => { resolveQuery = r; }),
+ });
+ // No connectionManager on the fake -> executeRawDirect falls through to this.sql.
+ const p = engine.executeRawDirect('SELECT 1');
+ expect(diagnostics()?.tracked.direct).toBe(1);
+ resolveQuery([]);
+ await p;
+ expect(diagnostics()?.tracked.direct).toBe(0);
+
+ const { engine: rejEngine, diagnostics: rejDiag } = makeEngine({
+ unsafe: () => Promise.reject(new Error('boom')),
+ });
+ await expect(rejEngine.executeRawDirect('SELECT 1')).rejects.toThrow('boom');
+ expect(rejDiag()?.tracked.direct).toBe(0);
+ });
+
+ test('transaction: tx counter released on SYNCHRONOUS begin() throw (nested-tx clone class)', async () => {
+ const { engine, diagnostics } = makeEngine({ unsafe: () => Promise.resolve([]) });
+ // Fake pool has no .begin -> conn.begin(...) throws synchronously, the
+ // exact shape a nested transaction on a tx clone produces.
+ await expect(
+ (engine as unknown as { transaction: (fn: unknown) => Promise<unknown> }).transaction(async () => 'x'),
+ ).rejects.toThrow();
+ expect(diagnostics()?.tracked.tx).toBe(0);
+ });
+
+ test('withReservedConnection: ddl() throw falls back to the READ pool and releases the permit', async () => {
+ const log: string[] = [];
+ const readPool = {
+ unsafe: async () => [],
+ options: { max: 10 },
+ reserve: async () => {
+ log.push('reserve:read');
+ return { unsafe: async () => [], release: () => log.push('release:read') };
+ },
+ };
+ const engine = Object.create(PostgresEngine.prototype) as PostgresEngine;
+ Object.defineProperty(engine, 'sql', { get: () => readPool });
+ Object.defineProperty(engine, '_sql', { value: readPool, writable: true });
+ Object.defineProperty(engine, 'checkoutGauge', { value: new CheckoutGauge(), writable: true });
+ Object.defineProperty(engine, '_reservedDirectInFlight', { value: 0, writable: true });
+ Object.defineProperty(engine, 'connectionManager', {
+ value: {
+ peekReadPool: () => readPool,
+ isDualPoolActive: () => true,
+ describeMode: () => ({ direct_pool_size: 3 }),
+ ddl: async () => { throw new Error('EMAXCONNSESSION'); },
+ },
+ writable: true,
+ });
+ const out = await engine.withReservedConnection(async () => 'ok');
+ expect(out).toBe('ok');
+ expect(log).toEqual(['reserve:read', 'release:read']); // fell back, did not fail the caller
+ expect((engine as unknown as { _reservedDirectInFlight: number })._reservedDirectInFlight).toBe(0);
+ });
+});
diff --git a/test/postgres-engine-reserved-routing.test.ts b/test/postgres-engine-reserved-routing.test.ts
new file mode 100644
index 0000000000..6353bb6a76
--- /dev/null
+++ b/test/postgres-engine-reserved-routing.test.ts
@@ -0,0 +1,158 @@
+/**
+ * issue #6 — `withReservedConnection` routing:
+ *
+ * dual-pool active + permit available → reserve from the DIRECT pool
+ * (30-min statement_timeout lane; stops long DDL/backfill holds from
+ * pinning the worker's shared read pool)
+ * semaphore exhausted (directPoolSize-1 in flight) → READ pool (status quo;
+ * >= 1 direct slot always stays free for claim/renewLock heartbeats)
+ * kill-switched / single-pool → READ pool (status quo)
+ * inside an open transaction → never rerouted
+ * throw inside fn / reserve failure → semaphore released (leak guard)
+ *
+ * Hermetic: Object.create(PostgresEngine.prototype) + recording fake pools.
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { PostgresEngine } from '../src/core/postgres-engine.ts';
+import { CheckoutGauge } from '../src/core/pool-gauge.ts';
+
+interface FakeReserved {
+ unsafe: (q: string, params?: unknown[]) => Promise<unknown[]>;
+ release: () => void;
+}
+
+function makeFakePool(label: string, log: string[]) {
+ return {
+ label,
+ unsafe: async () => [],
+ options: { max: 10 },
+ reserve: async (): Promise<FakeReserved> => {
+ log.push(`reserve:${label}`);
+ return {
+ unsafe: async () => [],
+ release: () => log.push(`release:${label}`),
+ };
+ },
+ };
+}
+
+function makeEngine(opts: {
+ dualPool: boolean;
+ directPoolSize?: number;
+ inTransaction?: boolean;
+ log: string[];
+}) {
+ const readPool = makeFakePool('read', opts.log);
+ const directPool = makeFakePool('direct', opts.log);
+ const engine = Object.create(PostgresEngine.prototype) as PostgresEngine;
+ Object.defineProperty(engine, 'sql', { get: () => readPool });
+ // inTransaction detection: _sql !== null && peekReadPool() !== _sql.
+ // Simulate "in transaction" by making _sql differ from peekReadPool().
+ Object.defineProperty(engine, '_sql', {
+ value: opts.inTransaction ? { txMarker: true } : readPool,
+ writable: true,
+ });
+ Object.defineProperty(engine, 'checkoutGauge', { value: new CheckoutGauge(), writable: true });
+ Object.defineProperty(engine, '_reservedDirectInFlight', { value: 0, writable: true });
+ Object.defineProperty(engine, 'connectionManager', {
+ value: {
+ peekReadPool: () => readPool,
+ isDualPoolActive: () => opts.dualPool,
+ describeMode: () => ({ direct_pool_size: opts.directPoolSize ?? 3 }),
+ ddl: async () => directPool,
+ },
+ writable: true,
+ });
+ return engine;
+}
+
+describe('withReservedConnection routing (issue #6)', () => {
+ test('dual-pool active: reserves from the DIRECT pool', async () => {
+ const log: string[] = [];
+ const engine = makeEngine({ dualPool: true, log });
+ await engine.withReservedConnection(async () => 'ok');
+ expect(log).toEqual(['reserve:direct', 'release:direct']);
+ });
+
+ test('single-pool (kill-switched): reserves from the READ pool (status quo)', async () => {
+ const log: string[] = [];
+ const engine = makeEngine({ dualPool: false, log });
+ await engine.withReservedConnection(async () => 'ok');
+ expect(log).toEqual(['reserve:read', 'release:read']);
+ });
+
+ test('inside a transaction: never rerouted', async () => {
+ const log: string[] = [];
+ const engine = makeEngine({ dualPool: true, inTransaction: true, log });
+ await engine.withReservedConnection(async () => 'ok');
+ expect(log).toEqual(['reserve:read', 'release:read']);
+ });
+
+ test('semaphore: directPoolSize-1 concurrent direct reserves; overflow -> read pool', async () => {
+ const log: string[] = [];
+ const engine = makeEngine({ dualPool: true, directPoolSize: 3, log });
+
+ let releaseFirst: () => void = () => {};
+ let releaseSecond: () => void = () => {};
+ const first = engine.withReservedConnection(
+ () => new Promise((r) => { releaseFirst = () => r('one'); }),
+ );
+ const second = engine.withReservedConnection(
+ () => new Promise((r) => { releaseSecond = () => r('two'); }),
+ );
+ // Let both reserves happen.
+ await new Promise((r) => setTimeout(r, 10));
+ expect(log.filter((l) => l === 'reserve:direct').length).toBe(2); // cap = 3-1 = 2
+
+ // Third concurrent reserve overflows to the read pool (status quo, never blocks).
+ const third = await engine.withReservedConnection(async () => 'three');
+ expect(third).toBe('three');
+ expect(log).toContain('reserve:read');
+
+ releaseFirst();
+ releaseSecond();
+ await Promise.all([first, second]);
+
+ // Permits released: the next reserve goes direct again.
+ log.length = 0;
+ await engine.withReservedConnection(async () => 'four');
+ expect(log).toEqual(['reserve:direct', 'release:direct']);
+ });
+
+ test('direct_pool_size=1: NEVER reserves from the direct pool (a reserve would starve ALL heartbeats)', async () => {
+ // Red-team finding: a Math.max(1, size-1) floor made cap=1 at size=1,
+ // letting a multi-minute reserve consume the ONLY direct session that
+ // claim/renewLock heartbeats depend on — the #6 class reintroduced.
+ const log: string[] = [];
+ const engine = makeEngine({ dualPool: true, directPoolSize: 1, log });
+ await engine.withReservedConnection(async () => 'ok');
+ expect(log).toEqual(['reserve:read', 'release:read']);
+ });
+
+ test('fn throw releases the semaphore permit (leak guard)', async () => {
+ const log: string[] = [];
+ const engine = makeEngine({ dualPool: true, directPoolSize: 2, log }); // cap = 1
+ await expect(
+ engine.withReservedConnection(async () => { throw new Error('DDL boom'); }),
+ ).rejects.toThrow('DDL boom');
+ // Permit released: next reserve still goes direct (cap would block if leaked).
+ log.length = 0;
+ await engine.withReservedConnection(async () => 'ok');
+ expect(log).toEqual(['reserve:direct', 'release:direct']);
+ });
+
+ test('reserve() failure releases the permit and rethrows', async () => {
+ const log: string[] = [];
+ const engine = makeEngine({ dualPool: true, directPoolSize: 2, log });
+ const cm = (engine as unknown as { connectionManager: { ddl: () => Promise<unknown> } }).connectionManager;
+ cm.ddl = async () => ({
+ reserve: async () => { throw new Error('EMAXCONNSESSION'); },
+ });
+ await expect(engine.withReservedConnection(async () => 'x')).rejects.toThrow('EMAXCONNSESSION');
+ // Permit released despite the failure.
+ expect(
+ (engine as unknown as { _reservedDirectInFlight: number })._reservedDirectInFlight,
+ ).toBe(0);
+ });
+});
diff --git a/test/queue-probe-cancellation.test.ts b/test/queue-probe-cancellation.test.ts
new file mode 100644
index 0000000000..74a9a3caa0
--- /dev/null
+++ b/test/queue-probe-cancellation.test.ts
@@ -0,0 +1,103 @@
+/**
+ * issue #6 — timed-out probes are CANCELLED, not abandoned.
+ *
+ * Closes the TODOS entry "cancel (not just abandon) timed-out submit-time
+ * queue probes": `probeQueueState` time-bounds via Promise.race, but the
+ * losing query used to keep running on the pool after the race resolved —
+ * under pool exhaustion (the exact regime the probe exists to detect) the
+ * abandoned query held a slot and made the exhaustion worse. The timeout now
+ * aborts a per-probe AbortSignal threaded through `queryWedgeSignals` into
+ * `engine.executeRaw`, and `withRefreshingLock`'s heartbeat does the same for
+ * `handle.refresh`.
+ *
+ * Hermetic: stub engines (`as unknown as BrainEngine`), no DB.
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { probeQueueState, queryWedgeSignals } from '../src/core/minions/supervisor.ts';
+import type { BrainEngine } from '../src/core/engine.ts';
+
+const WEDGE_ROW = {
+ stalled: '0',
+ active_healthy: '1',
+ waiting: '2',
+ waiting_claimable: '2',
+ last_completed: null,
+ last_completed_claimable: null,
+};
+
+describe('queryWedgeSignals signal threading (issue #6)', () => {
+ test('forwards opts.signal to executeRaw', async () => {
+ let seenOpts: { signal?: AbortSignal } | undefined;
+ const engine = {
+ kind: 'postgres',
+ executeRaw: async (_sql: string, _params?: unknown[], opts?: { signal?: AbortSignal }) => {
+ seenOpts = opts;
+ return [WEDGE_ROW];
+ },
+ } as unknown as BrainEngine;
+ const ac = new AbortController();
+
+ const sig = await queryWedgeSignals(engine, 'default', ['sync'], { signal: ac.signal });
+
+ expect(sig.waiting).toBe(2);
+ expect(seenOpts?.signal).toBe(ac.signal);
+ });
+});
+
+describe('probeQueueState cancellation (issue #6)', () => {
+ test('timeout aborts the per-probe signal so the losing query is cancelled', async () => {
+ let seenSignal: AbortSignal | undefined;
+ const engine = {
+ kind: 'postgres',
+ executeRaw: (_sql: string, _params?: unknown[], opts?: { signal?: AbortSignal }) => {
+ seenSignal = opts?.signal;
+ // Hang forever — the abandoned-racer scenario.
+ return new Promise<never>(() => {});
+ },
+ } as unknown as BrainEngine;
+
+ const state = await probeQueueState(engine, 'default', ['sync'], { timeoutMs: 50 });
+
+ expect(state.probe_failed).toBe(true);
+ expect(seenSignal).toBeInstanceOf(AbortSignal);
+ expect(seenSignal!.aborted).toBe(true);
+ });
+
+ test('fast probe: signal delivered but never aborted; result returned', async () => {
+ const seenSignals: Array<AbortSignal | undefined> = [];
+ const engine = {
+ kind: 'postgres',
+ executeRaw: async (sql: string, _params?: unknown[], opts?: { signal?: AbortSignal }) => {
+ seenSignals.push(opts?.signal);
+ if (sql.includes('EXTRACT(EPOCH')) return [{ age: 12 }];
+ return [WEDGE_ROW];
+ },
+ } as unknown as BrainEngine;
+
+ const state = await probeQueueState(engine, 'default', ['sync'], { timeoutMs: 5_000 });
+
+ expect(state.probe_failed).toBeUndefined();
+ expect(state.depth).toBe(2);
+ // The wedge + age queries carry the probe signal. Other engine calls made
+ // by the probe (e.g. inspectLock) pass no opts — filter to the signals we
+ // threaded and assert none were aborted on the fast path.
+ const threaded = seenSignals.filter((s): s is AbortSignal => s instanceof AbortSignal);
+ expect(threaded.length).toBeGreaterThanOrEqual(2);
+ for (const s of threaded) {
+ expect(s.aborted).toBe(false);
+ }
+ });
+
+ test('probe throw still collapses to {probe_failed: true} (fail-open contract)', async () => {
+ const engine = {
+ kind: 'postgres',
+ executeRaw: async () => {
+ throw new Error('query was cancelled');
+ },
+ } as unknown as BrainEngine;
+
+ const state = await probeQueueState(engine, 'default', ['sync'], { timeoutMs: 5_000 });
+ expect(state.probe_failed).toBe(true);
+ });
+});
diff --git a/test/run-child-entry.test.ts b/test/run-child-entry.test.ts
new file mode 100644
index 0000000000..1c26cf1b00
--- /dev/null
+++ b/test/run-child-entry.test.ts
@@ -0,0 +1,249 @@
+/**
+ * issue #5 — `runChildJobEntry` (the `jobs run-child` core) against a real
+ * in-memory PGLite engine with a REAL claim-minted lock token. (PGLite is
+ * rejected at the CLI layer for actual isolation runs — Postgres-only — but
+ * the core function is engine-agnostic, which is exactly what makes it
+ * testable without a DATABASE_URL.)
+ *
+ * Paths pinned:
+ * - success: handler result lands in the outcome file, exit 0
+ * - handler failure: encoded error outcome, STILL exit 0 (a reported
+ * failure is a successful report)
+ * - token mismatch / reclaimed job: exit 14, handler NEVER runs
+ * - missing handler: generic error outcome, exit 0
+ * - SIGTERM semantics via direct signal wiring (ctx.signal + shutdown)
+ */
+
+import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { MinionQueue } from '../src/core/minions/queue.ts';
+import { runChildJobEntry } from '../src/core/minions/run-child.ts';
+import { decodeChildOutcomeFile } from '../src/core/minions/job-isolation.ts';
+import {
+ JOB_CHILD_EXIT_NOT_CLAIMED,
+} from '../src/core/minions/worker-exit-codes.ts';
+import { UnrecoverableError, type MinionHandler } from '../src/core/minions/types.ts';
+
+let engine: PGLiteEngine;
+let queue: MinionQueue;
+let dir: string;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({ database_url: '' }); // in-memory
+ await engine.initSchema();
+ queue = new MinionQueue(engine);
+ dir = mkdtempSync(join(tmpdir(), 'gbrain-run-child-test-'));
+});
+
+afterAll(async () => {
+ await engine.disconnect();
+ rmSync(dir, { recursive: true, force: true });
+});
+
+beforeEach(async () => {
+ await engine.executeRaw('DELETE FROM minion_jobs');
+});
+
+/** Claim a freshly-added job the same way the parent worker does. */
+async function addAndClaim(name: string, data: Record<string, unknown> = {}) {
+ await queue.add(name, data);
+ const job = await queue.claim('parent-tok-1', 30_000, 'default', [name]);
+ if (!job) throw new Error('claim failed in test setup');
+ return job;
+}
+
+function handlers(map: Record<string, MinionHandler>) {
+ return { resolveHandler: (n: string) => map[n] };
+}
+
+describe('runChildJobEntry', () => {
+ test('success: outcome file carries the handler result; exit 0; ctx wired to the job row', async () => {
+ const job = await addAndClaim('sync', { full: true });
+ const resultPath = join(dir, `ok-${job.id}.json`);
+ let sawData: unknown;
+
+ const code = await runChildJobEntry(
+ engine,
+ { jobId: job.id, lockToken: 'parent-tok-1', resultPath, parentPid: 0 },
+ handlers({
+ sync: async (ctx) => {
+ sawData = ctx.data;
+ await ctx.updateProgress({ step: 'half' });
+ return { pages: 7 };
+ },
+ }),
+ );
+
+ expect(code).toBe(0);
+ expect(sawData).toEqual({ full: true });
+ const outcome = decodeChildOutcomeFile(resultPath);
+ expect(outcome).toEqual({ outcome: 'success', result: { pages: 7 } });
+ // The token-fenced progress write really landed.
+ const rows = await engine.executeRaw<{ progress: unknown }>(
+ 'SELECT progress FROM minion_jobs WHERE id = $1',
+ [job.id],
+ );
+ const progress = rows[0]?.progress;
+ expect(typeof progress === 'string' ? JSON.parse(progress) : progress).toEqual({ step: 'half' });
+ });
+
+ test('handler failure: encoded error outcome, exit 0 (reported failure = successful report)', async () => {
+ const job = await addAndClaim('sync');
+ const resultPath = join(dir, `err-${job.id}.json`);
+
+ const code = await runChildJobEntry(
+ engine,
+ { jobId: job.id, lockToken: 'parent-tok-1', resultPath, parentPid: 0 },
+ handlers({
+ sync: async () => {
+ throw new UnrecoverableError('schema mismatch, never retry');
+ },
+ }),
+ );
+
+ expect(code).toBe(0);
+ const outcome = decodeChildOutcomeFile(resultPath);
+ expect(outcome.outcome).toBe('error');
+ if (outcome.outcome === 'error') {
+ expect(outcome.errorKind).toBe('unrecoverable');
+ expect(outcome.message).toBe('schema mismatch, never retry');
+ }
+ });
+
+ test('token mismatch (job reclaimed): exit 14, handler never runs', async () => {
+ const job = await addAndClaim('sync');
+ const resultPath = join(dir, `mismatch-${job.id}.json`);
+ let handlerRan = false;
+
+ const code = await runChildJobEntry(
+ engine,
+ { jobId: job.id, lockToken: 'STALE-token', resultPath, parentPid: 0 },
+ handlers({
+ sync: async () => {
+ handlerRan = true;
+ return null;
+ },
+ }),
+ );
+
+ expect(code).toBe(JOB_CHILD_EXIT_NOT_CLAIMED);
+ expect(handlerRan).toBe(false);
+ expect(() => decodeChildOutcomeFile(resultPath)).toThrow(); // nothing written
+ });
+
+ test('missing job id: exit 14', async () => {
+ const code = await runChildJobEntry(
+ engine,
+ { jobId: 999_999, lockToken: 'parent-tok-1', resultPath: join(dir, 'none.json'), parentPid: 0 },
+ handlers({}),
+ );
+ expect(code).toBe(JOB_CHILD_EXIT_NOT_CLAIMED);
+ });
+
+ test('missing handler: UNRECOVERABLE error outcome (inline parity: dead on attempt 1), exit 0', async () => {
+ const job = await addAndClaim('exotic-plugin-job');
+ const resultPath = join(dir, `nohandler-${job.id}.json`);
+
+ const code = await runChildJobEntry(
+ engine,
+ { jobId: job.id, lockToken: 'parent-tok-1', resultPath, parentPid: 0 },
+ handlers({}),
+ );
+
+ expect(code).toBe(0);
+ const outcome = decodeChildOutcomeFile(resultPath);
+ expect(outcome.outcome).toBe('error');
+ if (outcome.outcome === 'error') {
+ expect(outcome.errorKind).toBe('unrecoverable');
+ expect(outcome.message).toContain("No handler for job type 'exotic-plugin-job'");
+ }
+ });
+
+ test('SIGTERM aborts ONLY shutdownSignal — ctx.signal stays live (inline signal-separation parity)', async () => {
+ const job = await addAndClaim('sync');
+ const resultPath = join(dir, `sigterm-${job.id}.json`);
+ let ctxSignalAbortedAtShutdown: boolean | null = null;
+
+ const entry = runChildJobEntry(
+ engine,
+ { jobId: job.id, lockToken: 'parent-tok-1', resultPath, parentPid: 0 },
+ handlers({
+ sync: (ctx) =>
+ new Promise((resolve) => {
+ ctx.shutdownSignal.addEventListener('abort', () => {
+ // The whole point: a cooperative handler gets the drain window
+ // with its per-job signal STILL LIVE, finishes, and reports.
+ ctxSignalAbortedAtShutdown = ctx.signal.aborted;
+ resolve('finished-during-drain');
+ });
+ }),
+ }),
+ );
+ await new Promise((r) => setTimeout(r, 100));
+ // Trigger the entry's process.on('SIGTERM') handler in-process without
+ // sending a real signal to the test runner.
+ (process as unknown as { emit: (event: string) => boolean }).emit('SIGTERM');
+ const code = await entry;
+
+ expect(code).toBe(0);
+ // TS control-flow can't see the closure write; compare explicitly.
+ expect(ctxSignalAbortedAtShutdown === false).toBe(true);
+ const outcome = decodeChildOutcomeFile(resultPath);
+ expect(outcome).toEqual({ outcome: 'success', result: { value: 'finished-during-drain' } });
+ });
+
+ test('primitive results are wrapped {value: x} CHILD-side (completeJob shape parity across the JSON boundary)', async () => {
+ const job = await addAndClaim('sync');
+ const resultPath = join(dir, `wrap-${job.id}.json`);
+
+ const code = await runChildJobEntry(
+ engine,
+ { jobId: job.id, lockToken: 'parent-tok-1', resultPath, parentPid: 0 },
+ handlers({ sync: async () => 42 }),
+ );
+
+ expect(code).toBe(0);
+ expect(decodeChildOutcomeFile(resultPath)).toEqual({ outcome: 'success', result: { value: 42 } });
+ });
+
+ test('parent-death watchdog: aborts the handler and schedules the hard exit', async () => {
+ const job = await addAndClaim('sync');
+ const resultPath = join(dir, `orphan-${job.id}.json`);
+ let sawAbort = false;
+ const hardExits: number[] = [];
+
+ // A GUARANTEED-dead parent pid: spawn a real short-lived process, wait
+ // for it to exit, and use its reaped pid. (A magic high pid is
+ // allocatable on Linux — default kernel.pid_max is 4,194,304 — so a real
+ // process could hold it and hang the test; testing specialist.)
+ const { spawnSync: spawnDead } = await import('node:child_process');
+ const deadPid = spawnDead('/bin/sh', ['-c', 'exit 0']).pid ?? 0;
+ expect(deadPid).toBeGreaterThan(0);
+ const code = await runChildJobEntry(
+ engine,
+ { jobId: job.id, lockToken: 'parent-tok-1', resultPath, parentPid: deadPid },
+ {
+ resolveHandler: () => async (ctx) =>
+ new Promise((resolve) => {
+ ctx.signal.addEventListener('abort', () => {
+ sawAbort = true;
+ resolve('aborted-cleanly');
+ });
+ }),
+ parentPollMs: 30,
+ orphanGraceMs: 60_000, // never fires in-test; we capture the schedule
+ hardExit: (c) => { hardExits.push(c); },
+ },
+ );
+
+ expect(sawAbort).toBe(true);
+ expect(code).toBe(0); // the handler resolved after abort; outcome written
+ const outcome = decodeChildOutcomeFile(resultPath);
+ expect(outcome.outcome).toBe('success');
+ expect(hardExits.length).toBe(0); // grace not reached — clean wind-down
+ });
+});
diff --git a/test/supervisor-build-worker-args.test.ts b/test/supervisor-build-worker-args.test.ts
index c6b72a2a28..542946a206 100644
--- a/test/supervisor-build-worker-args.test.ts
+++ b/test/supervisor-build-worker-args.test.ts
@@ -36,4 +36,18 @@ describe('buildWorkerArgs', () => {
expect(buildWorkerArgs({ concurrency: 1, queue: 'q', maxRssMb: 0 }))
.not.toContain('--nice');
});
+
+ // issue #5 — conditional pass-through: inline/omitted keeps existing
+ // deployments' argv byte-identical (the pinned arrays above never change).
+ test('appends --job-isolation process when set', () => {
+ expect(buildWorkerArgs({ concurrency: 2, queue: 'default', maxRssMb: 0, jobIsolation: 'process' }))
+ .toEqual(['jobs', 'work', '--concurrency', '2', '--queue', 'default', '--job-isolation', 'process']);
+ });
+
+ test('omits --job-isolation when inline or undefined', () => {
+ expect(buildWorkerArgs({ concurrency: 1, queue: 'q', maxRssMb: 0, jobIsolation: 'inline' }))
+ .not.toContain('--job-isolation');
+ expect(buildWorkerArgs({ concurrency: 1, queue: 'q', maxRssMb: 0 }))
+ .not.toContain('--job-isolation');
+ });
});
diff --git a/test/worker-job-isolation.test.ts b/test/worker-job-isolation.test.ts
new file mode 100644
index 0000000000..0292fee412
--- /dev/null
+++ b/test/worker-job-isolation.test.ts
@@ -0,0 +1,214 @@
+/**
+ * issue #5 — full parent path with isolation on: claim → spawn child →
+ * decode outcome → completeJob / failJob / release. Real in-memory PGLite
+ * worker + the fake-run-child.mjs fixture standing in for the compiled CLI
+ * (reads the isolation env contract, writes canned outcomes).
+ *
+ * Pins:
+ * - success: job completes with the CHILD's result (fenced completeJob)
+ * - error outcome: failJob path — attempt burned, delayed/dead per policy
+ * - crash (exit 1, no file): attempt burned
+ * - spawn failure (bad child CLI): RELEASED — status stays 'active',
+ * attempts NOT burned (infra class; stall sweeper would requeue)
+ */
+
+import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
+import { join } from 'node:path';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { MinionQueue } from '../src/core/minions/queue.ts';
+import { MinionWorker } from '../src/core/minions/worker.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const FIXTURE = join(import.meta.dir, 'fixtures', 'fake-run-child.mjs');
+
+let engine: PGLiteEngine;
+let queue: MinionQueue;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({ database_url: '' });
+ await engine.initSchema();
+ queue = new MinionQueue(engine);
+});
+
+afterAll(async () => {
+ await engine.disconnect();
+});
+
+beforeEach(async () => {
+ await engine.executeRaw('DELETE FROM minion_jobs');
+});
+
+function makeWorker(invocationCmd = process.execPath, argsPrefix = [FIXTURE]) {
+ const worker = new MinionWorker(engine, {
+ queue: 'default',
+ concurrency: 1,
+ pollInterval: 25,
+ healthCheckInterval: 0,
+ maxRssMb: 0,
+ jobIsolation: 'process',
+ childCliInvocation: { cmd: invocationCmd, argsPrefix },
+ });
+ // Handler must exist in the parent registry (name-scoped claiming); its
+ // body never runs in isolation mode.
+ worker.register('isotest', async () => {
+ throw new Error('parent-side handler must not run when isolated');
+ });
+ return worker;
+}
+
+async function runWorkerUntil(
+ worker: MinionWorker,
+ done: () => Promise<boolean>,
+ timeoutMs = 10_000,
+): Promise<void> {
+ const run = worker.start();
+ const deadline = Date.now() + timeoutMs;
+ try {
+ while (Date.now() < deadline) {
+ if (await done()) return;
+ await new Promise((r) => setTimeout(r, 50));
+ }
+ throw new Error('runWorkerUntil timed out');
+ } finally {
+ worker.stop();
+ await run;
+ }
+}
+
+async function jobRow(id: number): Promise<{ status: string; attempts_made: number; result: unknown; error_text: string | null }> {
+ const rows = await engine.executeRaw<{
+ status: string;
+ attempts_made: number;
+ result: unknown;
+ error_text: string | null;
+ }>('SELECT status, attempts_made, result, error_text FROM minion_jobs WHERE id = $1', [id]);
+ if (!rows[0]) throw new Error('job row missing');
+ return rows[0];
+}
+
+describe('worker with jobIsolation=process (PGLite + fake child)', () => {
+ test("construction throws for jobIsolation 'process' without childCliInvocation (predicate-mismatch guard)", () => {
+ // Red-team finding: 'process' without an invocation silently executed
+ // handlers INLINE while the evict path believed it was isolated.
+ expect(
+ () => new MinionWorker(engine, { queue: 'default', jobIsolation: 'process' }),
+ ).toThrow(/childCliInvocation/);
+ });
+
+ test('spawn-failure circuit breaker: 3 consecutive bootstrap failures emit unhealthy(child_spawn_failing)', async () => {
+ // Red-team finding: a deterministic child-bootstrap failure looped
+ // claim/release forever, invisible to the stall detector (every settle
+ // refreshes the progress clock).
+ const j1 = await queue.add('isotest', {});
+ const j2 = await queue.add('isotest', {});
+ const j3 = await queue.add('isotest', {});
+ const worker = makeWorker('/nonexistent/gbrain-binary', []);
+ const unhealthy: unknown[] = [];
+ worker.on('unhealthy', (i) => unhealthy.push(i));
+ const run = worker.start();
+ const deadline = Date.now() + 10_000;
+ try {
+ while (Date.now() < deadline && unhealthy.length === 0) {
+ await new Promise((r) => setTimeout(r, 50));
+ }
+ } finally {
+ worker.stop();
+ await run;
+ }
+ expect(unhealthy.length).toBeGreaterThan(0);
+ const info = unhealthy[0] as { reason: string; consecutiveFailures: number };
+ expect(info.reason).toBe('child_spawn_failing');
+ expect(info.consecutiveFailures).toBeGreaterThanOrEqual(3);
+ // No attempts burned anywhere — all three rows released, not failed.
+ for (const j of [j1, j2, j3]) {
+ const row = await jobRow(j.id);
+ expect(row.status).toBe('active');
+ expect(row.attempts_made).toBe(0);
+ }
+ }, 20_000);
+
+ test('success: child result lands via the fenced completeJob', async () => {
+ await withEnv({ FAKE_RUN_CHILD_MODE: 'success' }, async () => {
+ const job = await queue.add('isotest', { prompt: 'hi' });
+ const worker = makeWorker();
+ await runWorkerUntil(worker, async () => (await jobRow(job.id)).status === 'completed');
+ const row = await jobRow(job.id);
+ expect(row.status).toBe('completed');
+ const result = typeof row.result === 'string' ? JSON.parse(row.result) : row.result;
+ expect((result as { fromChild?: boolean }).fromChild).toBe(true);
+ // The child got the REAL claim token through the env contract.
+ expect((result as { token?: string }).token).toMatch(/^.+:.+$/);
+ });
+ }, 20_000);
+
+ test('error outcome: failJob path, attempt burned', async () => {
+ await withEnv({ FAKE_RUN_CHILD_MODE: 'error' }, async () => {
+ const job = await queue.add('isotest', {}, { max_attempts: 1 });
+ const worker = makeWorker();
+ await runWorkerUntil(worker, async () => {
+ const s = (await jobRow(job.id)).status;
+ return s === 'dead' || s === 'failed';
+ });
+ const row = await jobRow(job.id);
+ expect(row.status).toBe('dead'); // maxAttempts 1 → attempt burned → dead
+ expect(row.error_text).toContain('fake child handler failure');
+ });
+ }, 20_000);
+
+ test('crash (exit 1, no outcome file): attempt burned', async () => {
+ await withEnv({ FAKE_RUN_CHILD_MODE: 'crash' }, async () => {
+ const job = await queue.add('isotest', {}, { max_attempts: 1 });
+ const worker = makeWorker();
+ await runWorkerUntil(worker, async () => {
+ const s = (await jobRow(job.id)).status;
+ return s === 'dead' || s === 'failed';
+ });
+ const row = await jobRow(job.id);
+ expect(row.status).toBe('dead');
+ expect(row.error_text).toContain('exit code=1');
+ });
+ }, 20_000);
+
+ test('serialization parity (codex-2 #8): unreportable results fail in BOTH modes, never complete', async () => {
+ // Inline: a circular result blows up in completeJob's serialization →
+ // failJob (attempt burned).
+ const inlineWorker = new MinionWorker(engine, {
+ queue: 'default', concurrency: 1, pollInterval: 25, healthCheckInterval: 0, maxRssMb: 0,
+ });
+ inlineWorker.register('isotest', async () => {
+ const a: Record<string, unknown> = {};
+ a.self = a; // circular — not JSONB-serializable
+ return a;
+ });
+ const j1 = await queue.add('isotest', {}, { max_attempts: 1 });
+ await runWorkerUntil(inlineWorker, async () => {
+ const s = (await jobRow(j1.id)).status;
+ return s !== 'waiting' && s !== 'active';
+ });
+ expect((await jobRow(j1.id)).status).not.toBe('completed');
+
+ // Process mode: a child that cannot persist its outcome (exit 15) lands
+ // in the same terminal class — failed loudly, never falsely completed.
+ await withEnv({ FAKE_RUN_CHILD_MODE: 'exit15' }, async () => {
+ const j2 = await queue.add('isotest', {}, { max_attempts: 1 });
+ const worker = makeWorker();
+ await runWorkerUntil(worker, async () => (await jobRow(j2.id)).status === 'dead');
+ expect((await jobRow(j2.id)).error_text).toContain('outcome file');
+ });
+ }, 30_000);
+
+ test('spawn failure: RELEASED — still active, attempts NOT burned (infra class)', async () => {
+ const job = await queue.add('isotest', {});
+ const worker = makeWorker('/nonexistent/gbrain-binary', []);
+ // The claim happens, the spawn fails, the job is released (stays
+ // 'active' until lock expiry — the stall sweeper's requeue territory).
+ const run = worker.start();
+ await new Promise((r) => setTimeout(r, 1_500));
+ worker.stop();
+ await run;
+ const row = await jobRow(job.id);
+ expect(row.status).toBe('active'); // NOT dead, NOT failed
+ expect(row.attempts_made).toBe(0); // release = no failJob = no attempt burned
+ }, 20_000);
+});
diff --git a/test/worker-lock-renewal.test.ts b/test/worker-lock-renewal.test.ts
index b13daa5aec..53ee44e8c4 100644
--- a/test/worker-lock-renewal.test.ts
+++ b/test/worker-lock-renewal.test.ts
@@ -569,3 +569,50 @@ describe('runLockRenewalTick: reconnect-once dep (issue #1678)', () => {
expect(reconnectCalls).toBe(0); // pointless to reconnect when we're giving up the lock
});
});
+
+describe('runLockRenewalTick: per-call cancellation signal (issue #6)', () => {
+ test('renewLock receives a live AbortSignal; not aborted on the happy path', async () => {
+ const audit = freshAudit();
+ const timer = makeFakeTimer();
+ let seenSignal: AbortSignal | undefined;
+ const deps: LockRenewalDeps = {
+ renewLock: async (_id, _tok, _dur, opts) => {
+ seenSignal = opts?.signal;
+ return true;
+ },
+ audit: audit.sink,
+ now: () => 1000,
+ setTimeout: timer.setTimeout,
+ };
+ const result = await runLockRenewalTick(deps, makeState());
+ expect(result.kind).toBe('ok');
+ expect(seenSignal).toBeInstanceOf(AbortSignal);
+ expect(seenSignal!.aborted).toBe(false);
+ });
+
+ test('hung renewLock: the timeout aborts the per-call signal so the query is cancelled, not orphaned', async () => {
+ const audit = freshAudit();
+ const timer = makeFakeTimer();
+ let seenSignal: AbortSignal | undefined;
+ const deps: LockRenewalDeps = {
+ renewLock: (_id, _tok, _dur, opts) => {
+ seenSignal = opts?.signal;
+ return new Promise<boolean>(() => { /* hangs forever — abandoned racer */ });
+ },
+ audit: audit.sink,
+ now: () => 1000,
+ setTimeout: timer.setTimeout,
+ };
+ // Renewed recently: the timeout counts as failure #1, not a give-up.
+ const state = makeState({ lastSuccessfulRenewalAt: 1000 });
+ const tick = runLockRenewalTick(deps, state);
+ // The race installs the timeout synchronously; firing it must abort the
+ // per-call signal (this is what releases the checked-out pool slot).
+ timer.runAll();
+ const result = await tick;
+ expect(result.kind).toBe('ok'); // counter bumped, deadline not yet crossed
+ expect(state.consecutiveFailures).toBe(1);
+ expect(seenSignal).toBeInstanceOf(AbortSignal);
+ expect(seenSignal!.aborted).toBe(true);
+ });
+});
diff --git a/test/worker-supervised-db-probe.test.ts b/test/worker-supervised-db-probe.test.ts
index ffd4469b22..29a8396df0 100644
--- a/test/worker-supervised-db-probe.test.ts
+++ b/test/worker-supervised-db-probe.test.ts
@@ -35,11 +35,19 @@ afterAll(async () => {
/** Make the DB-liveness probe (`SELECT 1`) throw; delegate every other query.
* Returns a restore fn that removes the instance override (falls back to the
- * prototype method). */
+ * prototype method). Also records the probe's `opts` so tests can pin that
+ * the worker passes a cancellation signal (issue #6: a hung probe must be
+ * cancelled, not abandoned on a checked-out pool slot). */
+const probeOptsSeen: Array<{ signal?: AbortSignal } | undefined> = [];
function breakLivenessProbe(eng: PGLiteEngine): () => void {
const real = eng.executeRaw.bind(eng);
- (eng as { executeRaw: unknown }).executeRaw = async (sql: string, params?: unknown[]) => {
+ (eng as { executeRaw: unknown }).executeRaw = async (
+ sql: string,
+ params?: unknown[],
+ opts?: { signal?: AbortSignal },
+ ) => {
if (typeof sql === 'string' && sql.trim() === 'SELECT 1') {
+ probeOptsSeen.push(opts);
throw new Error('probe boom (simulated dead pool)');
}
return real(sql, params as never);
@@ -95,6 +103,74 @@ describe('issue #1801 fix #2 — supervised DB self-defense', () => {
expect(info?.reason).toBe('db_dead');
}, 10_000);
+ it('the probe passes a cancellation AbortSignal so a hung SELECT 1 releases its slot (issue #6)', () => {
+ // Populated by the two runUntilUnhealthy() calls above (>= 3 probes each).
+ expect(probeOptsSeen.length).toBeGreaterThan(0);
+ for (const opts of probeOptsSeen) {
+ expect(opts?.signal).toBeInstanceOf(AbortSignal);
+ }
+ });
+
+ it('dual-pool probe gating: probeDirect is wired ONLY when isDualPoolActive() is true', async () => {
+ // The load-bearing guard the adapter comments on: a kill-switched
+ // executeRawDirect would probe the same starved read pool twice and
+ // fabricate a verdict (testing specialist coverage gap).
+ const directCalls: string[] = [];
+ const runCase = async (dualPool: boolean): Promise<void> => {
+ const cmStub = { isDualPoolActive: () => dualPool };
+ (engine as unknown as { connectionManager?: unknown }).connectionManager = cmStub;
+ const realDirect = engine.executeRawDirect.bind(engine);
+ (engine as unknown as { executeRawDirect: unknown }).executeRawDirect = async (
+ sql: string,
+ params?: unknown[],
+ opts?: { signal?: AbortSignal },
+ ) => {
+ if (typeof sql === 'string' && sql.trim() === 'SELECT 1') {
+ directCalls.push(`dual=${dualPool}`);
+ return [];
+ }
+ return realDirect(sql, params as never, opts);
+ };
+ const restore = breakLivenessProbe(engine);
+ try {
+ await withEnv({ GBRAIN_SUPERVISED: '1' }, async () => {
+ const worker = new MinionWorker(engine, {
+ queue: 'default', concurrency: 1, pollInterval: 25, maxRssMb: 0,
+ healthCheckInterval: 20, dbFailExitAfter: 2, dbProbeTimeoutMs: 200,
+ });
+ worker.register('noop', async () => {});
+ const got = new Promise<UnhealthyReason>((resolve) => {
+ worker.on('unhealthy', (i) => resolve(i));
+ });
+ const runPromise = worker.start();
+ const info = await Promise.race([
+ got,
+ new Promise<null>((r) => setTimeout(() => r(null), 3000)),
+ ]);
+ worker.stop();
+ await runPromise;
+ expect(info).not.toBeNull();
+ if (dualPool) {
+ // Direct lane succeeded -> verdict names the pooler path.
+ expect(info!.reason === 'db_dead' && info!.verdict).toBe('pool_starved');
+ } else {
+ expect(info!.reason === 'db_dead' && info!.verdict).toBe('unknown');
+ }
+ });
+ } finally {
+ restore();
+ delete (engine as unknown as { executeRawDirect?: unknown }).executeRawDirect;
+ delete (engine as unknown as { connectionManager?: unknown }).connectionManager;
+ }
+ };
+
+ await runCase(false);
+ expect(directCalls.length).toBe(0); // single-pool: NEVER probes direct
+
+ await runCase(true);
+ expect(directCalls.filter((c) => c === 'dual=true').length).toBeGreaterThan(0);
+ }, 15_000);
+
it('structural: DB probe is NOT gated on !isSupervisedChild; stall detection IS', () => {
const src = readFileSync(
join(import.meta.dir, '..', 'src', 'core', 'minions', 'worker.ts'),