Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
v0.42.76.0 fix(upgrade,security,cli): strict flag validation, bootstrap-wedge class kill, federated chunk scope (#3902)
Garry Tan130d321d2347
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0680908b4d..7b725864be 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,23 @@All notable changes to GBrain will be documented in this file.+## [0.42.76.0] - 2026-08-08++**Mistyped or unsupported flags now fail loudly instead of being silently ignored — including the ones that were supposed to make a command safe.**++**Strict flag validation, CLI-wide.** Every gbrain command now rejects a flag it does not understand, with a clear error naming the flag and the command, before any work runs. Before, commands read their flags ad hoc and ignored the rest — so `gbrain post-upgrade --dry-run` accepted the flag, ignored it, and applied migrations for real. That class is gone: the legal flags for every command are derived from each command's own source into a generated registry, checked before dispatch, and a command may only advertise a safety flag like `--dry-run` if its code actually reads it. On commands routed through the operations contract, a trailing `--dry-run` is now a real rehearsal switch rather than a no-op. `--json` invocations get the same error as a structured payload, so scripts fail cleanly too.++**A word of warning (intentional breaking change):** cron jobs or scripts that pass stray, misspelled, or long-removed flags have been running on luck — the flag did nothing. Those invocations now exit with an error naming the flag. That is the point: fix the invocation once and it means what it says forever. Everything after `--` is passthrough and remains untouched.++**Upgrades can't wedge on forward-referenced columns anymore — as a class.** The v0.42.56.0-era startup wedge (a schema blob referencing a column that pre-existing brains didn't have yet) had two more latent instances waiting in the jobs table. Both are now probed and healed at startup, and the schema coverage guard was rewritten to cross-reference every column referenced by the embedded schema against the set of columns any migration has ever added — so a new forward reference cannot ship without its startup probe. A recovery test walks the exact journey an affected brain takes: failed upgrade, retry on the fixed binary, converge with no leftover state blocking the way.++**Remote agents get more, within the same fences.** The `think` operation is now available to remote MCP callers as a read-only synthesis — the local CLI can still persist results, while remote callers are forced read-only. Chunk reads now resolve through the same source-scope rules as page reads, so a federated grant that can open a page can also read that page's chunks, and a caller without the grant cannot reach chunks outside its own floor. Chunk payloads also stop carrying raw embedding vectors over the wire — noticeably smaller responses with no behavior change, since no consumer ever read them. Two internal call sites that forward caller identity now treat anything ambiguous as untrusted, matching the fail-closed rule the rest of the codebase already follows.++**Source-bound clients can be minted over HTTP.** The `/admin/api/register-client` endpoint now accepts `source` and `federatedRead` bindings, mirroring the CLI's `--source` / `--federated-read` flags — so an admin UI or provisioning proxy can create a client confined to a specific brain source without shelling out to the CLI. Omitting both preserves the historical default, and invalid source ids get a structured 400.++**`gbrain doctor` and `repair-jsonb` see further and misfire less.** The double-encoded-JSON scan now covers the subagent execution columns, and the damage test requires the stored text to actually parse as JSON before flagging it — a legitimate string value that merely starts with `[` or `{` (a log line, a code snippet) is no longer misclassified, and a repair pass can no longer corrupt it. One damaged table no longer aborts the scan of the rest.++### To take advantage of v0.42.76.0## [0.42.75.0] - 2026-08-08**The "PGLite crashes on macOS 26" era is over: gbrain now repairs a torn brain in place, automatically, with your data preserved.**@@ -31,6 +48,13 @@ Credit where due: @yang1996202-cpu (#2575), @AndreLYL (#223), and @roysaurav (#1gbrain upgrade```+Nothing to configure. If a cron job or script starts failing with `unknown flag`, that invocation was passing a flag that did nothing — remove or fix the flag and it will not regress silently again.++### For contributors++Community fixes absorbed with credit: @colinagent (#2598 think read-scope; the upgrade-rewind e2e pattern from #2623), @guim4dev (#2016 register-client source bindings), @vinsew (#597 repair-jsonb coverage extension), @javieraldape (#2494/#2531 output-correctness class — BigInt-safe local rendering and the search `--json` regression pin land here; parts of both PRs shipped earlier from master). Thank you — superseded PRs are being closed with notes.++The unit-test runner is now memory-safe on machines running multiple workspaces: shard concurrency adapts to actually-available memory, and a serial rescue lane re-runs files that died to OOM or external kills before calling them failures — a red suite now means real failures, not memory pressure. The flag registry regenerates via `bun run build:flag-registry` and is pinned by freshness, drift, and consumption-evidence guards.If your brain currently won't open, that's it — the next command repairs it. If you'd rather look first: `gbrain pglite-repair --dry-run`.## [0.42.74.0] - 2026-08-07diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex d6408cd20d..8bd3a60de8 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -81,7 +81,7 @@ docs/ Architecture docs```bash# Inner edit loop (~85s on a Mac dev box, 3700+ unit tests)-bun run test # parallel 8-shard fan-out + serial post-pass+bun run test # parallel 4-shard fan-out (memory-adaptive) + serial post-passbun test test/markdown.test.ts # specific unit test# Pre-push gate (matches what CI runs on shard 1 + typecheck)@@ -118,9 +118,12 @@ trailing-newline and exports-count checks.### Writing tests that survive the parallel loop-`bun run test` shards 92+ unit-test files across 8 worker processes. Files in the-same shard share a process, so process-global state leaks between them. Four-lint rules (`scripts/check-test-isolation.sh`, R1-R4) enforce isolation:+`bun run test` shards 1000+ unit-test files across up to 4 worker processes,+capping total concurrency (shards × intra-shard files) to available memory and+re-running OOM-killed or externally-killed files serially before calling them+failures (see `docs/TESTING.md` for the rescue-pass details and knobs). Files+in the same shard share a process, so process-global state leaks between them.+Four lint rules (`scripts/check-test-isolation.sh`, R1-R4) enforce isolation:| Rule | What it bans | Fix ||---|---|---|@@ -217,6 +220,15 @@ automatically appears in the CLI, MCP server, and tools-json:For CLI-only commands (init, upgrade, import, export, files, embed, doctor, sync):1. Create `src/commands/mycommand.ts`2. Add the case to `src/cli.ts`+3. Regenerate the flag registry: `bun run build:flag-registry`. The CLI rejects+ unknown flags before dispatch; each CLI-only command's legal flag set is+ derived from its source into `src/core/cli-flag-registry.generated.ts`.+ `test/cli-flag-validation.test.ts` pins registry freshness, drift, and+ consumption evidence (a safety flag like `--dry-run` may only be advertised+ if the command's code actually reads it), so a stale registry fails the+ build. At runtime a missing registry entry fails open — a forgotten regen+ never bricks a command. Rerun the regen whenever you add or remove a flag+ on an existing command, too.Parity tests (`test/parity.test.ts`) verify CLI/MCP/tools-json stay in sync.diff --git a/TODOS.md b/TODOS.mdindex 34ff1362bb..9974a7c4d9 100644--- a/TODOS.md+++ b/TODOS.md@@ -1,5 +1,62 @@# TODOS+## Fix-wave 1 follow-ups (upgrade-wedge + trust-seam wave, 2026-08)++Deferred from the un-wedge-v121 hotfix wave (eng review + codex outside voice+CLEARED; every item an explicit review decision). Waves 2–6 of the sequence are+planned separately (provider-compat rescue is next; its original 2026-07-24+DeepSeek-deprecation deadline has now PASSED — re-verify each cluster against+master before starting, several fixes landed independently).++- [ ] **P2 — Shared strict `parseFlags` helper as the #2185 end-state (eng+ review 2B).** This wave ships the generated known-flags registry ++ pre-dispatch validator (parser and registry can drift only until the+ freshness guard fires). The structural end-state migrates commands onto one+ shared strict parser so parser == registry by construction; mechanical but+ touches 60+ command files — its own PR. Where: `src/commands/*.ts`,+ `src/cli.ts`, `scripts/generate-flag-registry.ts` (retires).+- [ ] **P2 — `whoknows` CLI routing (surfaced by the #2035-class sweep).**+ `handleCliOnly`'s `whoknows` case (the dedicated CLI renderer with+ thin-client routing) is dead code — the command resolves via the+ `find_experts` op alias, and adding it to CLI_ONLY trips the alias-collision+ guard. Decide the intended surface alongside PR #2509 (whoknows --explain+ per-result factor breakdown) and delete whichever lane loses. Where:+ `src/cli.ts`, `src/commands/whoknows.ts`, PR #2509.+- [ ] **P3 — #2544 second half: per-put_page `getAllSlugs` full scan.** The+ getChunks egress half shipped in this wave (explicit non-vector column+ list). The remaining Postgres-egress cost is put_page's per-call+ `getAllSlugs` table scan — needs a targeted existence probe or cached slug+ set. Where: `src/core/operations.ts` put_page path, both engines.+- [ ] **P3 — #1558 admin-UI register form.** The `/admin/api/register-client`+ API now accepts `source` + `federatedRead` (this wave, PR #2016 absorbed);+ the admin SPA form fields + `/admin/api/sources` picker are the UI layer.+ Where: `src/commands/serve-http.ts` admin SPA blob.+- [ ] **P3 — jsonb-integrity surfaces: batch + share (ship-review follow-up).**+ doctor's jsonbIntegrityCheck runs 2 queries per target (16 round-trips) and+ duplicates the TARGETS table with repair-jsonb (already drifted once on the+ jsonPayloadOnly predicate before being mirrored by hand). Batch the counts+ into one UNION ALL query and extract a shared targets constant+ (src/core/jsonb-integrity-targets.ts) consumed by both. Where:+ `src/commands/doctor.ts` jsonbIntegrityCheck, `src/commands/repair-jsonb.ts`.+- [ ] **P3 — register-client HTTP-level e2e (ship-review follow-up).** The+ source/federatedRead lane is covered by unit normalizers + a structural+ route pin; a DATABASE_URL-gated serve-http e2e (register with bindings →+ assert stored client via /admin/api/agents; invalid source → 400+ invalid_source) closes the wire-level gap. Where:+ `test/e2e/serve-http-oauth.test.ts`.+- [ ] **P3 — get_chunks `__all__` sentinel narrows to 'default' (red-team,+ Wave 3 territory).** `sourceScopeOpts` returns `{}` for a trusted local+ `--source __all__` caller (documented "spans the brain"), but both engines'+ getChunks map empty scope to the 'default' floor — the one read op where+ `{}` is reinterpreted. Fold into the Wave 3 source-federation cluster's+ `__all__` work (an explicit unscoped signal in the engine signature, or+ handler-side expansion for trusted callers). Where: `src/core/operations.ts`+ get_chunks, both engines' getChunks.+- [ ] **P3 — #2536 wedged-migration diagnostics.** The v121 wedge aborted+ initSchema BEFORE runMigrations, so the wedged-migration diagnostics row was+ never written — operators got a bare SQL error with no remediation hint.+ Write the diagnostics row (or a stderr remediation block) from the blob-replay+ catch path too. Where: `src/core/migrate.ts`, `src/commands/apply-migrations.ts`.## WAL-repair wave follow-ups (#223/#1670/#2575)- [ ] **P2 — gate auto-repair on an unclean-shutdown marker (adversarial F7).** The classifierdiff --git a/VERSION b/VERSIONindex b1b737ca82..fe69db4019 100644--- a/VERSION+++ b/VERSION@@ -1 +1 @@-0.42.75.0+0.42.76.0\ No newline at end of filediff --git a/docs/TESTING.md b/docs/TESTING.mdindex e97f36a46f..7aa3e7234b 100644--- a/docs/TESTING.md+++ b/docs/TESTING.md@@ -11,7 +11,7 @@ Seven test command tiers, each with a clear scope:| Command | What it runs | Wallclock | When to use ||---|---|---|---|-| `bun run test` | Parallel unit-test fast loop. 8-shard fan-out via `scripts/run-unit-parallel.sh`, then a serial pass over `*.serial.test.ts`. Excludes `*.slow.test.ts` and `test/e2e/*`. No pre-checks, no typecheck. | ~85s on a Mac dev box (3650+ tests) | Inner edit loop. Default. |+| `bun run test` | Parallel unit-test fast loop. Up-to-4-shard fan-out via `scripts/run-unit-parallel.sh` (min(CPUs, 4)), then a serial pass over `*.serial.test.ts`. Excludes `*.slow.test.ts` and `test/e2e/*`. No pre-checks, no typecheck. Memory-safe by default: total concurrency (shards × intra-shard files) is capped to available memory at `GBRAIN_TEST_MEM_PER_FILE_MB` (default 1536 — a PGLite WASM instance) per concurrent file, and two phantom-failure classes are automatically re-run serially (the rescue pass): failures carrying the WASM out-of-memory signature, and shards killed externally (SIGTERM/SIGKILL well before the shard timeout — sibling workspaces' process cleanup, memory jetsam). Phantoms pass serially and the run goes green with an `oom_rescued` note; real failures fail again serially and stay red. Knobs: `GBRAIN_TEST_NO_MEM_ADAPT=1`, `GBRAIN_TEST_NO_OOM_FALLBACK=1`, `GBRAIN_TEST_MAX_CONCURRENCY` (intra-shard, default 4), `GBRAIN_TEST_SHARD_TIMEOUT` / `GBRAIN_TEST_SHARD_KILL_AFTER`, plus `--shards N` / `--max-concurrency N` / `--dry-run` script args. | ~85s on a Mac dev box (3700+ tests) | Inner edit loop. Default. || `bun run verify` | CI's authoritative pre-test gate set, fanned out in parallel by `scripts/run-verify-parallel.sh`: the full `check:*` battery (~30 checks — privacy, jsonb, progress, source-id, test-isolation, wasm, …) plus `bun run typecheck`. The `CHECKS` array in that script is the single source of truth — CI literally calls `bun run verify` in a dedicated job. | ~16s (parallel; typecheck dominates) | Before pushing; before `/ship`. || `bun run test:full` | `verify && bun run test && bun run test:slow && [smart e2e]`. The local equivalent of "everything CI runs." Smart e2e: runs e2e only when `DATABASE_URL` is set; else loud skip notice to stderr. | ~3-5min depending on slow + e2e | Pre-merge sanity, before opening a PR. || `bun run test:slow` | Just the `*.slow.test.ts` set (intentional cold-path correctness checks). | seconds-to-minutes | When touching slow-path code. |@@ -50,7 +50,7 @@ there even though they pass on Linux and macOS.- **CI matrix** (`.github/workflows/test.yml`) runs `scripts/test-shard.sh` across 10 matrix shards partitioned by weight-aware LPT bin-packing (`scripts/sharding.ts`) and INCLUDES `*.slow.test.ts` (the two outlier slow files run as dedicated jobs alongside the matrix). CI EXCLUDES `*.serial.test.ts` from the shards and runs them in a dedicated job via `bun run test:serial`, one bun process per file — keeping serial files out of the shard processes is what preserves the `mock.module` quarantine (a top-level mock in one file leaks into every other file sharing its process). `bun run verify` gets its own job too. CI is the ground truth for "did everything pass."- **Local fast loop** (`scripts/run-unit-shard.sh` via the parallel wrapper) uses round-robin-by-index sharding and EXCLUDES `*.slow.test.ts` AND `*.serial.test.ts`. Local trades coverage for inner-loop speed; CI catches what local skips.-This divergence is intentional. Don't try to make them equal — the two scripts deliberately solve different problems. The regression test at `test/scripts/run-unit-shard.test.ts` pins what the local fast loop should and shouldn't include.+This divergence is intentional. Don't try to make them equal — the two scripts deliberately solve different problems. The regression test at `test/scripts/run-unit-shard.test.ts` pins what the local fast loop should and shouldn't include; `test/scripts/run-unit-parallel.test.ts` pins the wrapper's memory-adaptive concurrency and the OOM/external-kill serial rescue pass.### Failure-first logging@@ -61,11 +61,11 @@ When `bun run test` finds any failure, the wrapper:3. Writes a one-line-per-shard summary to `.context/test-summary.txt` (`shard N/M: pass=X fail=Y skip=Z rc=W`).4. Exits non-zero. Empty failure log + non-zero exit = infrastructure problem (wedged shard, killed child); the banner says so.-If a shard wedges (per-shard `GBRAIN_TEST_SHARD_TIMEOUT` cap, default 600s), the wrapper writes `--- shard N: WEDGED after ${SHARD_TIMEOUT}s ---` to the failure log, includes the last 50 lines of the shard log, and proceeds with other shards' results.+If a shard wedges (per-shard `GBRAIN_TEST_SHARD_TIMEOUT` cap, default 3000s; `GBRAIN_TEST_SHARD_KILL_AFTER` grace after TERM before KILL, default 30s), the wrapper writes `--- shard N: WEDGED after ${SHARD_TIMEOUT}s ---` to the failure log, includes the last 50 lines of the shard log, and proceeds with other shards' results.### File taxonomy-- `*.test.ts` → fast loop (parallel 8-shard fan-out).+- `*.test.ts` → fast loop (parallel up-to-4-shard fan-out, memory-adaptive).- `*.slow.test.ts` → run via `bun run test:slow` only (intentional cold-path tests; would dominate the fast loop's wallclock).- `*.serial.test.ts` → run via `bun run test:serial` after the parallel pass completes; one bun process per file (`--max-concurrency=1` within a shared process is not enough — the module registry still leaks `mock.module`). Quarantine for tests that share file-wide state and race when run alongside other files in the same `bun test` process. Several dozen files, discovered by the `*.serial.test.ts` glob — no list to maintain. Typical residents: `mock.module(...)` users (top-level mocks leak across files in a shard process, e.g. `test/embed.serial.test.ts`), env-coupled files (e.g. `test/brain-registry.serial.test.ts`), and process-lifecycle suites that assert on `process.exitCode` (e.g. `test/pglite-engine-disconnect.serial.test.ts`). **Do not put the parallelism back on a serial file unless you've fixed the contention root cause** (it just re-introduces the flake).- `test/e2e/*.test.ts` → real-Postgres E2E. Skipped when `DATABASE_URL` is unset.diff --git a/docs/architecture/KEY_FILES.md b/docs/architecture/KEY_FILES.mdindex 1355626032..e5166f1aac 100644--- a/docs/architecture/KEY_FILES.md+++ b/docs/architecture/KEY_FILES.md@@ -12,8 +12,8 @@ per-release `**vX.Y.Z:**` narration — CI enforces this- `src/commands/serve-http.ts` confidential revoke extension — a pre-router `/revoke` handler validates the RFC 7009 body, verifies hash-only secrets for both `client_secret_post` and `client_secret_basic`, rejects mixed authentication, preserves the SDK path for public clients, and separates opaque client-auth failures from retryable/backend failures. OAuth metadata advertises both confidential methods. Pinned by `test/e2e/serve-http-oauth.test.ts`.-- `src/core/operations.ts` — Contract-first operation definitions (the foundation). Exports upload validators `validateUploadPath`, `validatePageSlug`, `validateFilename`, plus `matchesSlugAllowList(slug, prefixes)` (glob matcher: `<prefix>/*` matches recursive children; bare `<prefix>` matches exact only). `OperationContext.remote` is a REQUIRED field flagging untrusted callers; `OperationContext.allowedSlugPrefixes` is the trusted-workspace allow-list set by the dream cycle; `OperationContext.auth?: AuthInfo` is threaded through HTTP dispatch for scope enforcement in `serve-http.ts` before the op runs. OAuth `whoami` exposes the authenticated `AuthInfo.sourceId` and `AuthInfo.allowedSources` grants as `source_id` and `federated_read`; absent grants serialize fail-closed as `null` and `[]`, while local, legacy, and stdio response shapes stay unchanged. `enforceSubagentSlugFence(ctx, slug, opName)` is the shared fail-closed subagent write fence: when `viaSubagent` and `allowedSlugPrefixes` is set, the slug must match the allow-list; else the legacy `wiki/agents/<id>/...` namespace check applies. Both `put_page` and `add_timeline_entry` (subagent-allowlisted) route through it. Auto-link skipped only when `remote=true && !trustedWorkspace`. `enforceClientSlugFence(ctx, slug, opName)` is the OAuth-client write fence: when `ctx.auth.boundSlugPrefixes` is present (threaded from `oauth_clients.bound_slug_prefixes` at token-verification time), every direct slug-mutating write op — `put_page`, `delete_page`, `restore_page`, `add_tag`, `remove_tag`, `add_link`/`remove_link` (`from` endpoint only; linking TO a readable page is a reference), `add_timeline_entry`, `revert_version`, `put_raw_data` — rejects out-of-prefix slugs with `permission_denied`, BEFORE each op's dry-run short-circuit. Plain-startsWith semantics matching `submit_agent`'s check for the same column (NOT the glob grammar of the subagent allow-list); empty-array binding is deny-all (fail-closed); no auth / no binding = no fence. The match rule itself lives in the exported `slugUnderBoundPrefixes(prefixes, slug)` so non-op write surfaces reuse it verbatim. It is BOUNDARY-AWARE (a prefix matches whole segments, so `emp-alice` does not admit `emp-alice-2/…`), lowercases both sides (stored slugs are lowercased by `validateSlug`, so comparing the caller's raw string let a mixed-case slug commit and only then trip the resolved-slug re-check), accepts BOTH the trailing-slash and the v85 `<prefix>/*` glob spelling via `normalizeSlugPrefix` (the column predates this fence as submit_agent's binding, so one stored value must mean one span of slugs on both paths), and ignores empty-string prefixes. `assertValidSlugPrefixes` (`oauth-provider.ts`) rejects empty, whitespace-bearing, non-lowercase, and boundary-less entries at registration and rescope. `submit_agent` applies the same boundary-aware rule when validating a requested prefix against the binding, normalizes trailing-slash prefixes to the glob form `matchesSlugAllowList` expects before handing them to the child job, and collapses an EXPLICIT empty `allowed_tools`/`allowed_slug_prefixes` to the binding (the worker reads empty as "full registry" / "legacy `wiki/agents/<id>/` namespace", so `??` — which only substitutes null/undefined — left a vacuous-subset bypass). `put_page` additionally fences the RESOLVED slug when importFromContent's dedup pre-check redirects the write to a different page (same content_hash / `frontmatter.id`), since the disk write-through runs against that slug. That re-check applies whichever confinement the CALLER is under — OAuth binding and/or subagent allow-list/legacy namespace — via `slugOutsideCallerFence(ctx, slug)`, which composes `slugUnderBoundPrefixes` with the subagent fence's own match rule: the delegated `submit_agent` → subagent context carries `viaSubagent` + `allowedSlugPrefixes` but NO `auth`, so an auth-only test let a slug-bound client holding `agent` scope reach an out-of-fence page simply by delegating the write. Denials never name the resolved slug (it would be a slug-enumeration oracle). Pinned by `test/put-page-dedup-fence.test.ts`. `CLIENT_FENCED_WRITE_OPS` + `enforceBoundClientOpAllowList(auth, op)` are the fail-closed companion, applied once in `src/mcp/dispatch.ts` (the choke point both MCP transports share): a slug-bound client calling ANY `write`/`admin` op not on the allow-list gets `permission_denied`. This covers the ops that write by a key other than a slug and therefore cannot be fenced — `extract_entities`/`extract_facts` (mutate `people/*`, `companies/*`), `forget_fact` (numeric fact id, crosses sources), `ontology_propose` — and makes a write op added later denied-by-default instead of silently unfenced. `think` is on the allow-list because remote callers cannot persist from it. Pinned by `test/client-slug-fence.test.ts` and over-the-wire by `test/e2e/qm-provisioning.test.ts`. Every `Operation` carries `scope?: 'read' | 'write' | 'admin'` + `localOnly?: boolean`; `sync_brain`, `file_upload`, `file_list`, `file_url` are `admin + localOnly` (rejected over HTTP). Four trust-boundary call sites (`put_page` allowlist, file_upload trust-narrowing, submit_job protected-name guard, auto-link skip) use FAIL-CLOSED semantics: `ctx.remote === false` for trusted-only sites, `ctx.remote !== false` for "untrust unless explicit-false" — anything not strictly `false` is treated as remote (closes the HTTP MCP shell-job RCE where a read+write OAuth token could submit `shell` jobs). `sourceScopeOpts(ctx)` encodes the source-scoped read precedence ladder — federated array (`ctx.auth.allowedSources`) wins over scalar (`ctx.sourceId`/`ctx.auth.sourceId`) over nothing; every read-side op handler routes through it so a source-bound OAuth client can't see neighboring sources via `search`/`query`/`list_pages`/`get_page`/`find_experts`/query's image path, plus the by-slug reads `get_tags`/`get_links`/`get_backlinks`/`get_timeline` (and `get_page`'s tag fetch, which resolves against the concrete page's own `source_id`). `linkReadScopeOpts(ctx)` is the link-read sibling for `get_links`/`get_backlinks`: a link row references three pages (from/to/origin), and the engine's federated (`sourceIds[]`) branch scopes ALL THREE while its scalar (`sourceId`) branch scopes only the near endpoint (by design — trusted internal callers like `reconcileLinks` and back-link validators call the engine with a scalar scope and need the cross-source view). For an UNTRUSTED remote caller (`ctx.remote !== false`) carrying only a scalar scope it promotes that scope to a single-element `sourceIds:[id]`, routing them through the all-endpoint branch so a far/origin slug from outside the grant isn't disclosed; a federated array passes through unchanged and trusted local CLI (`ctx.remote === false`) keeps the scalar cross-source view. `thinkSourceScopeOpts(ctx)` maps the same precedence ladder onto `runThink`'s public options (`allowedSources`/`sourceId`) so the `think` op's gather and trajectory stages inherit the caller's source grant. `put_page`'s inline disk write-through is the shared `writePageThrough` helper (`src/core/write-through.ts`), ATOMIC via temp-sibling + rename so a crash or concurrent `gbrain sync` can't read a half-written `.md`; same helper backs `gbrain brainstorm/lsd --save`. Link provenance surface (#1941): `add_link` (`gbrain link`/`link-add`) + `remove_link` (`gbrain unlink`/`link-rm`) expose `link_source`/`link_type`; `add_link` rejects the reconciliation-managed built-ins via `MANAGED_LINK_SOURCES` (`markdown`/`frontmatter`/`mentions`/`wikilink-resolved`) and defaults omitted provenance to `'manual'` (the engine's own default stays `'markdown'` for internal callers); `list_link_sources` (`gbrain link-sources`, read) lists provenances via `sourceScopeOpts`. CLI aliases register through `cliHints.aliases` (collision-guarded in `src/cli.ts`).-- `src/core/engine.ts` — Pluggable engine interface (BrainEngine). `clampSearchLimit(limit, default, cap)` takes an explicit cap so per-operation caps can be tighter than `MAX_SEARCH_LIMIT`. Exports `LinkBatchInput`/`TimelineBatchInput` for the bulk-insert API (`addLinksBatch`/`addTimelineEntriesBatch`). `readonly kind: 'postgres' | 'pglite'` discriminator lets `src/core/migrate.ts` and others branch without `instanceof` + dynamic imports. Methods: `batchLoadEmotionalInputs(slugs?)` (CTE-shaped read with per-table aggregates so page × N tags × M takes never produces N×M rows), `setEmotionalWeightBatch(rows)` (`UPDATE FROM unnest($1::text[],$2::text[],$3::real[])` composite-keyed on `(slug, source_id)`), `getRecentSalience(opts)`, `findAnomalies(opts)`. `PageFilters` has `sort?: 'updated_desc'|'updated_asc'|'created_desc'|'slug'` + `PAGE_SORT_SQL` whitelist consumed by both engines. `listAllPageRefs(): Promise<Array<{slug, source_id}>>` ordered by `(source_id, slug)` — cheap cross-source enumeration replacing the `getAllSlugs()→getPage(slug)` N+1 (which silently defaulted to `source_id='default'`); parity across postgres-engine.ts + pglite-engine.ts; Pinned by `test/e2e/multi-source-bug-class.test.ts`. `SearchOpts`+`PageFilters` add `sourceIds?: string[]` (federated read axis; both engines apply `WHERE source_id = ANY($N::text[])` when set, preserve scalar `sourceId` fast path when unset); `traverseGraph(slug, depth, opts?)` and `traversePaths(slug, opts?)` accept `opts.sourceId`/`opts.sourceIds`. The by-slug read methods carry the same federated axis: `getTags`/`getLinks`/`getBacklinks` opts and `TimelineOpts` (consumed by `getTimeline`) accept `sourceIds?: string[]` taking precedence over the scalar `sourceId` (`source_id = ANY($::text[])` scoping the slug→page-id lookup); the link reads (`getLinks`/`getBacklinks`) scope ALL THREE endpoints (from/to/origin) on the federated branch while the scalar branch scopes only the near endpoint for trusted internal cross-source callers. `traverseGraph` opts has `frontierCap?: number` (per-iteration recursive-CTE cap, approx per-BFS-layer); return type `Promise<GraphNode[]>` for MCP wire stability; export `TraverseGraphOpts`; Postgres uses parenthesized `LIMIT N ORDER BY (slug, id)` inside the recursive term, PGLite mirrors with positional params; Pinned by `test/regressions/v0_36_frontier_cap.test.ts`. Phantom-redirect methods: `refreshPageBody(slug, sourceId, compiled_truth, timeline, content_hash)` narrow-UPDATEs three columns + updated_at, skipping soft-deleted rows (content_hash refresh required so `gbrain sync` sees the canonical as unchanged after fence merge); `migrateFactsToCanonical(phantomSlug, canonicalSlug, sourceId)` UPDATEs `entity_slug`+`source_markdown_slug` on every active fact row keyed on the phantom, preserving embedding/validUntil/kind/status/source_session/confidence; parity at `test/phantom-redirect-engine-parity.test.ts`. `getAdjacencyBoosts(pageIds): Promise<Map<number, AdjacencyRow>>` powers the per-query graph-signals stage — one SQL query returning inbound-link counts among top-K plus a cross-source count (links from differing `source_id`); `COALESCE(p.source_id,'default')` null safety, `HAVING >= 1`, cross-source CASE-WHEN excludes the target's own source; parity SQL across both engines; `SearchResult` gains optional `base_score`, `backlink_boost`, `salience_boost`, `recency_boost`, `exact_match_boost`, `graph_adjacency_boost`, `graph_cross_source_boost`, `session_demote_factor`, `reranker_delta` + internal staging fields; Pinned by `test/e2e/graph-signals-engine.test.ts`. Two REQUIRED methods: `deletePages(slugs, {sourceId}): Promise<string[]>` (single-batch primitive returning slugs actually deleted) and `resolveSlugsByPaths(paths, {sourceId}): Promise<Map<path,slug>>` (batch path→slug lookup); `sourceId` REQUIRED on both at the type level (asymmetric with single-row `deletePage` which keeps optional/'default'); both short-circuit on empty input and throw when `> DELETE_BATCH_SIZE`. Embedding-signature stale-detection quartet: `countStaleChunks(opts?)` gains optional `signature?: string` widening the stale predicate from `embedding IS NULL` to ALSO include chunks whose JOINed page `embedding_signature IS NOT NULL AND <> $signature` (NULL signature is GRANDFATHERED, never counted; omit `signature` for the legacy NULL-only count); `sumStaleChunkChars(opts?: {sourceId?, signature?}): Promise<number>` = `SUM(LENGTH(chunk_text))` over stale chunks (same predicate + embed_skip filter + optional sourceId scope), used by `gbrain sync --all` cost preview via `estimateCostFromChars`; `setPageEmbeddingSignature(slug, {sourceId?, signature})` stamps `pages.embedding_signature` after a page's chunks (re)embed, idempotent no-op when page absent; `invalidateStaleSignatureEmbeddings({signature, sourceId?}): Promise<number>` NULLs `embedding`+`embedded_at` on every chunk whose page signature is set AND differs, returning the count, called BEFORE `listStaleChunks` so signature-drift pages flow through the NULL-embedding keyset cursor unchanged (NULL never invalidated). Widens `findOrphanPages(opts?: {sourceId?, sourceIds?})` (candidate-side scoping only; inbound links counted from any source). Pinned by `test/sum-stale-chunk-chars.test.ts`, `test/embedding-signature-stale.test.ts`, `test/e2e/engine-parity.test.ts`. Free-text alias layer: `resolveAliases(aliasNorms, opts?): Promise<Map<string, Array<{slug, source_id}>>>` (READ; maps each normalized alias to declaring `(slug, source_id)` pairs, source-scoped) and `setPageAliases(slug, sourceId, aliasNorms)` (WRITE; replaces the full alias set, delete-then-insert, empty clears, idempotent on the unique triple), called by the `importFromContent` ingest projection and the `reindex --aliases` backfill; parity across both engines, Pinned by `test/search/page-aliases-engine.test.ts`. `searchVector` in both engines injects the shared `buildBestPerPagePoolCte` per-page max-pool so a page surfaces on its strongest chunk. `executeRawDirect(sql, params?, opts?)` is the lock-hot-path sibling of `executeRaw`: same single-statement contract, but routes to the direct session-mode pool when dual-pool is active (Postgres/Supabase port 5432) so a long-held lock heartbeat survives the transaction pooler's per-transaction connection recycling; PGLite delegates straight to `executeRaw` (no pooler). Both engines implement it; the Minion lock path (`claim`/`renewLock`) is the consumer. `reconnect(ctx?: {error?})` is a REQUIRED lifecycle method on BOTH engines: it recovers a dropped connection using the config captured at the last `connect()`, so callers (autopilot health probe, `batchRetry`) never `disconnect()` + bare `connect()` (which loses the config and throws `database_url undefined` forever, and opens a null-connection window). PostgresEngine rebuilds its pool with a `_reconnecting` reentrancy guard and emits pool-recovery audit; PGLite is single-writer in-process so it just restores the saved data dir for parity.+- `src/core/operations.ts` — Contract-first operation definitions (the foundation). Exports upload validators `validateUploadPath`, `validatePageSlug`, `validateFilename`, plus `matchesSlugAllowList(slug, prefixes)` (glob matcher: `<prefix>/*` matches recursive children; bare `<prefix>` matches exact only). `OperationContext.remote` is a REQUIRED field flagging untrusted callers; `OperationContext.allowedSlugPrefixes` is the trusted-workspace allow-list set by the dream cycle; `OperationContext.auth?: AuthInfo` is threaded through HTTP dispatch for scope enforcement in `serve-http.ts` before the op runs. OAuth `whoami` exposes the authenticated `AuthInfo.sourceId` and `AuthInfo.allowedSources` grants as `source_id` and `federated_read`; absent grants serialize fail-closed as `null` and `[]`, while local, legacy, and stdio response shapes stay unchanged. `enforceSubagentSlugFence(ctx, slug, opName)` is the shared fail-closed subagent write fence: when `viaSubagent` and `allowedSlugPrefixes` is set, the slug must match the allow-list; else the legacy `wiki/agents/<id>/...` namespace check applies. Both `put_page` and `add_timeline_entry` (subagent-allowlisted) route through it. Auto-link skipped only when `remote=true && !trustedWorkspace`. `enforceClientSlugFence(ctx, slug, opName)` is the OAuth-client write fence: when `ctx.auth.boundSlugPrefixes` is present (threaded from `oauth_clients.bound_slug_prefixes` at token-verification time), every direct slug-mutating write op — `put_page`, `delete_page`, `restore_page`, `add_tag`, `remove_tag`, `add_link`/`remove_link` (`from` endpoint only; linking TO a readable page is a reference), `add_timeline_entry`, `revert_version`, `put_raw_data` — rejects out-of-prefix slugs with `permission_denied`, BEFORE each op's dry-run short-circuit. Plain-startsWith semantics matching `submit_agent`'s check for the same column (NOT the glob grammar of the subagent allow-list); empty-array binding is deny-all (fail-closed); no auth / no binding = no fence. The match rule itself lives in the exported `slugUnderBoundPrefixes(prefixes, slug)` so non-op write surfaces reuse it verbatim. It is BOUNDARY-AWARE (a prefix matches whole segments, so `emp-alice` does not admit `emp-alice-2/…`), lowercases both sides (stored slugs are lowercased by `validateSlug`, so comparing the caller's raw string let a mixed-case slug commit and only then trip the resolved-slug re-check), accepts BOTH the trailing-slash and the v85 `<prefix>/*` glob spelling via `normalizeSlugPrefix` (the column predates this fence as submit_agent's binding, so one stored value must mean one span of slugs on both paths), and ignores empty-string prefixes. `assertValidSlugPrefixes` (`oauth-provider.ts`) rejects empty, whitespace-bearing, non-lowercase, and boundary-less entries at registration and rescope. `submit_agent` applies the same boundary-aware rule when validating a requested prefix against the binding, normalizes trailing-slash prefixes to the glob form `matchesSlugAllowList` expects before handing them to the child job, and collapses an EXPLICIT empty `allowed_tools`/`allowed_slug_prefixes` to the binding (the worker reads empty as "full registry" / "legacy `wiki/agents/<id>/` namespace", so `??` — which only substitutes null/undefined — left a vacuous-subset bypass). `put_page` additionally fences the RESOLVED slug when importFromContent's dedup pre-check redirects the write to a different page (same content_hash / `frontmatter.id`), since the disk write-through runs against that slug. That re-check applies whichever confinement the CALLER is under — OAuth binding and/or subagent allow-list/legacy namespace — via `slugOutsideCallerFence(ctx, slug)`, which composes `slugUnderBoundPrefixes` with the subagent fence's own match rule: the delegated `submit_agent` → subagent context carries `viaSubagent` + `allowedSlugPrefixes` but NO `auth`, so an auth-only test let a slug-bound client holding `agent` scope reach an out-of-fence page simply by delegating the write. Denials never name the resolved slug (it would be a slug-enumeration oracle). Pinned by `test/put-page-dedup-fence.test.ts`. `CLIENT_FENCED_WRITE_OPS` + `enforceBoundClientOpAllowList(auth, op)` are the fail-closed companion, applied once in `src/mcp/dispatch.ts` (the choke point both MCP transports share): a slug-bound client calling ANY `write`/`admin` op not on the allow-list gets `permission_denied`. This covers the ops that write by a key other than a slug and therefore cannot be fenced — `extract_entities`/`extract_facts` (mutate `people/*`, `companies/*`), `forget_fact` (numeric fact id, crosses sources), `ontology_propose` — and makes a write op added later denied-by-default instead of silently unfenced. `think` is on the allow-list because remote callers cannot persist from it. Pinned by `test/client-slug-fence.test.ts` and over-the-wire by `test/e2e/qm-provisioning.test.ts`. Every `Operation` carries `scope?: 'read' | 'write' | 'admin'` + `localOnly?: boolean`; `think` is read-scoped for OAuth/MCP because remote callers have `save`/`take` forced off before persistence, while local CLI can still persist via `remote:false`; `sync_brain`, `file_upload`, `file_list`, `file_url` are `admin + localOnly` (rejected over HTTP). Four trust-boundary call sites (`put_page` allowlist, file_upload trust-narrowing, submit_job protected-name guard, auto-link skip) use FAIL-CLOSED semantics: `ctx.remote === false` for trusted-only sites, `ctx.remote !== false` for "untrust unless explicit-false" — anything not strictly `false` is treated as remote (closes the HTTP MCP shell-job RCE where a read+write OAuth token could submit `shell` jobs). `sourceScopeOpts(ctx)` encodes the source-scoped read precedence ladder — federated array (`ctx.auth.allowedSources`) wins over scalar (`ctx.sourceId`/`ctx.auth.sourceId`) over nothing; every read-side op handler routes through it so a source-bound OAuth client can't see neighboring sources via `search`/`query`/`list_pages`/`get_page`/`find_experts`/query's image path, plus the by-slug reads `get_tags`/`get_links`/`get_backlinks`/`get_timeline`/`get_chunks` (chunks follow the same ladder as `get_page`, so a federated grant that can open a page can read its chunks — and the chunk payload never carries embedding vectors) (and `get_page`'s tag fetch, which resolves against the concrete page's own `source_id`). `linkReadScopeOpts(ctx)` is the link-read sibling for `get_links`/`get_backlinks`: a link row references three pages (from/to/origin), and the engine's federated (`sourceIds[]`) branch scopes ALL THREE while its scalar (`sourceId`) branch scopes only the near endpoint (by design — trusted internal callers like `reconcileLinks` and back-link validators call the engine with a scalar scope and need the cross-source view). For an UNTRUSTED remote caller (`ctx.remote !== false`) carrying only a scalar scope it promotes that scope to a single-element `sourceIds:[id]`, routing them through the all-endpoint branch so a far/origin slug from outside the grant isn't disclosed; a federated array passes through unchanged and trusted local CLI (`ctx.remote === false`) keeps the scalar cross-source view. `thinkSourceScopeOpts(ctx)` maps the same precedence ladder onto `runThink`'s public options (`allowedSources`/`sourceId`) so the `think` op's gather and trajectory stages inherit the caller's source grant. `put_page`'s inline disk write-through is the shared `writePageThrough` helper (`src/core/write-through.ts`), ATOMIC via temp-sibling + rename so a crash or concurrent `gbrain sync` can't read a half-written `.md`; same helper backs `gbrain brainstorm/lsd --save`. Link provenance surface (#1941): `add_link` (`gbrain link`/`link-add`) + `remove_link` (`gbrain unlink`/`link-rm`) expose `link_source`/`link_type`; `add_link` rejects the reconciliation-managed built-ins via `MANAGED_LINK_SOURCES` (`markdown`/`frontmatter`/`mentions`/`wikilink-resolved`) and defaults omitted provenance to `'manual'` (the engine's own default stays `'markdown'` for internal callers); `list_link_sources` (`gbrain link-sources`, read) lists provenances via `sourceScopeOpts`. CLI aliases register through `cliHints.aliases` (collision-guarded in `src/cli.ts`).+- `src/core/engine.ts` — Pluggable engine interface (BrainEngine). `clampSearchLimit(limit, default, cap)` takes an explicit cap so per-operation caps can be tighter than `MAX_SEARCH_LIMIT`. Exports `LinkBatchInput`/`TimelineBatchInput` for the bulk-insert API (`addLinksBatch`/`addTimelineEntriesBatch`). `readonly kind: 'postgres' | 'pglite'` discriminator lets `src/core/migrate.ts` and others branch without `instanceof` + dynamic imports. Methods: `batchLoadEmotionalInputs(slugs?)` (CTE-shaped read with per-table aggregates so page × N tags × M takes never produces N×M rows), `setEmotionalWeightBatch(rows)` (`UPDATE FROM unnest($1::text[],$2::text[],$3::real[])` composite-keyed on `(slug, source_id)`), `getRecentSalience(opts)`, `findAnomalies(opts)`. `PageFilters` has `sort?: 'updated_desc'|'updated_asc'|'created_desc'|'slug'` + `PAGE_SORT_SQL` whitelist consumed by both engines. `listAllPageRefs(): Promise<Array<{slug, source_id}>>` ordered by `(source_id, slug)` — cheap cross-source enumeration replacing the `getAllSlugs()→getPage(slug)` N+1 (which silently defaulted to `source_id='default'`); parity across postgres-engine.ts + pglite-engine.ts; Pinned by `test/e2e/multi-source-bug-class.test.ts`. `SearchOpts`+`PageFilters` add `sourceIds?: string[]` (federated read axis; both engines apply `WHERE source_id = ANY($N::text[])` when set, preserve scalar `sourceId` fast path when unset); `traverseGraph(slug, depth, opts?)` and `traversePaths(slug, opts?)` accept `opts.sourceId`/`opts.sourceIds`. The by-slug read methods carry the same federated axis: `getTags`/`getLinks`/`getBacklinks`/`getChunks` opts and `TimelineOpts` (consumed by `getTimeline`) accept `sourceIds?: string[]` taking precedence over the scalar `sourceId` (`source_id = ANY($::text[])` scoping the slug→page-id lookup); `getChunks` falls back to the `'default'` source when neither is set (importCodeFile's incremental-embedding reuse relies on it) and SELECTs an explicit non-vector column list — embedding vectors never ride the payload since rowToChunk discards them (`getChunksWithEmbeddings` stays scalar-only by design: engine-internal, zero remote-reachable callers); the link reads (`getLinks`/`getBacklinks`) scope ALL THREE endpoints (from/to/origin) on the federated branch while the scalar branch scopes only the near endpoint for trusted internal cross-source callers. `traverseGraph` opts has `frontierCap?: number` (per-iteration recursive-CTE cap, approx per-BFS-layer); return type `Promise<GraphNode[]>` for MCP wire stability; export `TraverseGraphOpts`; Postgres uses parenthesized `LIMIT N ORDER BY (slug, id)` inside the recursive term, PGLite mirrors with positional params; Pinned by `test/regressions/v0_36_frontier_cap.test.ts`. Phantom-redirect methods: `refreshPageBody(slug, sourceId, compiled_truth, timeline, content_hash)` narrow-UPDATEs three columns + updated_at, skipping soft-deleted rows (content_hash refresh required so `gbrain sync` sees the canonical as unchanged after fence merge); `migrateFactsToCanonical(phantomSlug, canonicalSlug, sourceId)` UPDATEs `entity_slug`+`source_markdown_slug` on every active fact row keyed on the phantom, preserving embedding/validUntil/kind/status/source_session/confidence; parity at `test/phantom-redirect-engine-parity.test.ts`. `getAdjacencyBoosts(pageIds): Promise<Map<number, AdjacencyRow>>` powers the per-query graph-signals stage — one SQL query returning inbound-link counts among top-K plus a cross-source count (links from differing `source_id`); `COALESCE(p.source_id,'default')` null safety, `HAVING >= 1`, cross-source CASE-WHEN excludes the target's own source; parity SQL across both engines; `SearchResult` gains optional `base_score`, `backlink_boost`, `salience_boost`, `recency_boost`, `exact_match_boost`, `graph_adjacency_boost`, `graph_cross_source_boost`, `session_demote_factor`, `reranker_delta` + internal staging fields; Pinned by `test/e2e/graph-signals-engine.test.ts`. Two REQUIRED methods: `deletePages(slugs, {sourceId}): Promise<string[]>` (single-batch primitive returning slugs actually deleted) and `resolveSlugsByPaths(paths, {sourceId}): Promise<Map<path,slug>>` (batch path→slug lookup); `sourceId` REQUIRED on both at the type level (asymmetric with single-row `deletePage` which keeps optional/'default'); both short-circuit on empty input and throw when `> DELETE_BATCH_SIZE`. Embedding-signature stale-detection quartet: `countStaleChunks(opts?)` gains optional `signature?: string` widening the stale predicate from `embedding IS NULL` to ALSO include chunks whose JOINed page `embedding_signature IS NOT NULL AND <> $signature` (NULL signature is GRANDFATHERED, never counted; omit `signature` for the legacy NULL-only count); `sumStaleChunkChars(opts?: {sourceId?, signature?}): Promise<number>` = `SUM(LENGTH(chunk_text))` over stale chunks (same predicate + embed_skip filter + optional sourceId scope), used by `gbrain sync --all` cost preview via `estimateCostFromChars`; `setPageEmbeddingSignature(slug, {sourceId?, signature})` stamps `pages.embedding_signature` after a page's chunks (re)embed, idempotent no-op when page absent; `invalidateStaleSignatureEmbeddings({signature, sourceId?}): Promise<number>` NULLs `embedding`+`embedded_at` on every chunk whose page signature is set AND differs, returning the count, called BEFORE `listStaleChunks` so signature-drift pages flow through the NULL-embedding keyset cursor unchanged (NULL never invalidated). Widens `findOrphanPages(opts?: {sourceId?, sourceIds?})` (candidate-side scoping only; inbound links counted from any source). Pinned by `test/sum-stale-chunk-chars.test.ts`, `test/embedding-signature-stale.test.ts`, `test/e2e/engine-parity.test.ts`. Free-text alias layer: `resolveAliases(aliasNorms, opts?): Promise<Map<string, Array<{slug, source_id}>>>` (READ; maps each normalized alias to declaring `(slug, source_id)` pairs, source-scoped) and `setPageAliases(slug, sourceId, aliasNorms)` (WRITE; replaces the full alias set, delete-then-insert, empty clears, idempotent on the unique triple), called by the `importFromContent` ingest projection and the `reindex --aliases` backfill; parity across both engines, Pinned by `test/search/page-aliases-engine.test.ts`. `searchVector` in both engines injects the shared `buildBestPerPagePoolCte` per-page max-pool so a page surfaces on its strongest chunk. `executeRawDirect(sql, params?, opts?)` is the lock-hot-path sibling of `executeRaw`: same single-statement contract, but routes to the direct session-mode pool when dual-pool is active (Postgres/Supabase port 5432) so a long-held lock heartbeat survives the transaction pooler's per-transaction connection recycling; PGLite delegates straight to `executeRaw` (no pooler). Both engines implement it; the Minion lock path (`claim`/`renewLock`) is the consumer. `reconnect(ctx?: {error?})` is a REQUIRED lifecycle method on BOTH engines: it recovers a dropped connection using the config captured at the last `connect()`, so callers (autopilot health probe, `batchRetry`) never `disconnect()` + bare `connect()` (which loses the config and throws `database_url undefined` forever, and opens a null-connection window). PostgresEngine rebuilds its pool with a `_reconnecting` reentrancy guard and emits pool-recovery audit; PGLite is single-writer in-process so it just restores the saved data dir for parity.- `src/core/engine-constants.ts` — single source of truth for engine batch-sizing constants. Exports `DELETE_BATCH_SIZE = 500` consumed by both engines' `deletePages` + `resolveSlugsByPaths` and by the sync delete + rename loops. Lives outside `engine.ts` (the interface module) to avoid circular-import worry — bounded per-statement work for predictable lock hold time + write amplification.- `src/core/background-work.ts` (#1762/#1745/#1775) — process background-work registry: the single owner of "drain every fire-and-forget DB-write sink before the CLI disconnects." `registerBackgroundWorkDrainer({name, order, drain(timeoutMs), abort?})` + `drainAllBackgroundWorkForCliExit({timeoutMs})` over a `Map<name, BackgroundWorkDrainer>` (idempotent registration by name; `__registerDrainerForTest` returns an unregister handle). Drains in explicit `(order, name)` order — facts FIRST (order 0) so its abort-path DB `logIngest` runs against the freshest live engine — and AWAITS `abort()` only when `drain()` reports `unfinished>0`. Best-effort per drainer: one sink's failure never blocks the others or the disconnect. FIVE sinks register at module import: `facts/queue.ts` (order 0; `abort`=`shutdown()` cancels a hung facts:absorb Haiku via internalAbort), `last-retrieved.ts` (order 1), `search/hybrid.ts` (order 2; `awaitPendingSearchCacheWrites` bounded via `Promise.race`), `eval-capture.ts` (order 3; `captureEvalCandidate` self-tracks its promise via `awaitPendingEvalCaptures`), `context/volunteer-events.ts` (order 4, #2095 — batched volunteer-event INSERTs, drained like the rest). Every cli.ts teardown site reaches it through `finishCliTeardown` (`src/core/cli-force-exit.ts`), which drains the registry before `engine.disconnect()` — closing the PGLite busy-loop where `db.close()` raced an in-flight job and pinned the single-writer lock (#1762). Exports `backgroundWorkSinkCount()` so the teardown helper computes its backstop deadline from the registered sink count. CLI-EXIT-ONLY: the facts `shutdown()` abort is permanent process state, never call in a long-lived `gbrain serve`. Companion changes: `src/core/ai/gateway.ts` `withDefaultTimeout(caller, ms)` bounds every outbound AI call (chat 300s, embed+multimodal 60s; env `GBRAIN_AI_{CHAT,EMBED,MULTIMODAL}_TIMEOUT_MS`; composed with caller signals via `AbortSignal.any`) and the teardown backstop honors an errored op's exit code so a hung disconnect can't mask failure as success (see `cli-force-exit.ts`); `src/core/postgres-engine.ts` `reconnect()` module-mode branch re-establishes via idempotent `db.connect()` + `connectionManager.setReadPool` refresh instead of `db.disconnect()` (no null window for concurrent ops; fail-loud on real connect failure — #1745); `src/core/search/hybrid.ts` `embedQueryBounded` + a shared `QueryEmbedDeadline` (6s, floored 2s per embed via `MIN_QUERY_EMBED_BUDGET_MS`; env `GBRAIN_QUERY_EMBED_TIMEOUT_MS`) bounds the cache-lookup AND inner query embeds so a stalled provider falls back to keyword instead of stalling the whole op (#1775). Incorporates + hardens PR #1763 (@ElliotDrel). Pinned by `test/core/background-work.test.ts`, `test/search/query-embed-deadline.test.ts`, `test/eval-capture-drain.test.ts`, `test/e2e/postgres-reconnect-singleton.test.ts`, `test/e2e/pglite-cli-exit.serial.test.ts`, `test/fix-wave-structural.test.ts`.- `src/core/search/graph-signals.ts` — per-query graph-signals helper. `applyGraphSignals(results, engine, opts)` runs as the 4th post-fusion stage (after backlink/salience/recency). Three boosts: `ADJACENCY_BOOST=1.05` (page linked from 2+ OTHER top-K results — local hub for THIS query), `CROSS_SOURCE_BOOST=1.10` (page linked from 2+ DIFFERENT sources — corroborated across team brains, dormant in single-source brains), `SESSION_DEMOTE=0.95` (3+ results from same chat session — keep the highest-scoring at full score, demote the rest). All three inherit the floor-ratio gate preventing weak pages from being boosted past strong ones via popularity. `computeScoreDistribution(results)` emits min/p25/p50/p75/p95/max + `reorder_band_width`. `sessionPrefix(slug)` extracts the chat-session anchor (`chat/2026-05-15-...`). Pure `pairedBootstrapPValue(deltas, resamples, rng)` exported for eval gates. Test seam via `adjacencyFn` DI. Fail-open: any error logs via `logGraphSignalsFailure` (JSONL audit via `audit-writer`) and returns the input array unchanged. Pinned by `test/search/graph-signals.test.ts` (incl. the IRON-RULE floor-gate regression).@@ -285,7 +285,7 @@ per-release `**vX.Y.Z:**` narration — CI enforces this- `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`.- `src/mcp/dispatch.ts` — shared tool-call dispatch consumed by both stdio (`server.ts`) and HTTP transports. Exports `dispatchToolCall(engine, name, params, opts)`, `buildOperationContext(engine, params, opts)`, `validateParams(op, params)`. Single source of truth for `(ctx, params)` handler arg order and the 5-field `OperationContext` shape (engine + config + logger + dryRun + remote). Defaults `remote: true` (untrusted); local CLI callers pass `remote: false`. Also exports `summarizeMcpParams(opName, params)` — privacy-preserving redactor for `mcp_request_log` and the admin SSE feed, returns `{redacted, kind, declared_keys, unknown_key_count, approx_bytes}`. Intersects submitted top-level keys against the operation's declared `params` allow-list (declared keys preserved sorted; unknown keys counted but never named, closing the attacker-controlled-key-name leak). Byte counts bucketed up to nearest 1KB so an attacker can't binary-search secret-content sizes by probing. Raw payload visibility is opt-in via `gbrain serve --http --log-full-params` (loud stderr warning). New logging paths route through this helper, not `JSON.stringify(params)`.- `src/mcp/rate-limit.ts` — Bounded-LRU token-bucket limiter. `buildDefaultLimiters()` returns the two-bucket pipeline: pre-auth IP (30/60s, fires BEFORE the DB lookup so brute-force load against `access_tokens` is capped) + post-auth token-id (60/60s). Tracks `lastTouchedMs` separately from `lastRefillMs` so an exhausted key can't be reset by hammering past the TTL. LRU cap bounds memory under attacker-controlled key growth.-- `src/commands/serve-http.ts` — Express 5 HTTP MCP server with OAuth 2.1, admin dashboard, and SSE live activity feed. Started via `gbrain serve --http [--port N] [--token-ttl N] [--enable-dcr] [--public-url URL] [--bind HOST] [--log-full-params]`. Combines MCP SDK's `mcpAuthRouter` (authorize/token/register/revoke), a custom `client_credentials` handler running BEFORE the router (SDK's token endpoint throws `UnsupportedGrantTypeError` for CC; custom handler falls through for `auth_code` / `refresh_token`), `requireBearerAuth` middleware for `/mcp` with scope enforcement + `localOnly` rejection before op dispatch, and `express-rate-limit` at 50 req / 15 min on `/token`. Serves the built admin SPA from `admin/dist/` with SPA fallback. `/admin/events` SSE broadcasts every MCP request. `cookie-parser` wired (Express 5 has no built-in). Startup logging prints port, engine, issuer URL (honors `--public-url`), client count, DCR status, and the admin bootstrap token line — but the generated token's raw value only prints when stderr is an interactive TTY (`shouldSuppressBootstrapPrint`): a non-TTY (containerized/piped) start hides it so the secret never lands in centralized log storage, env-sourced tokens (`$GBRAIN_ADMIN_BOOTSTRAP_TOKEN`) are always hidden, `--print-admin-token` forces the raw value on a trusted terminal, and `--suppress-bootstrap-token` hides everything. The `/mcp` request handler's OperationContext literal sets `remote: true` explicitly (without it `submit_job`'s protected-name guard at `operations.ts:1391` saw a falsy undefined and a `read+write`-scoped OAuth token could submit `shell` jobs — RCE). `summarizeMcpParams` from `src/mcp/dispatch.ts` feeds both `mcp_request_log` writes and the SSE feed by default (raw via `--log-full-params`). Cookie `Secure` flag set behind HTTPS or a public-URL proxy; magic-link nonce store LRU-bounded; DCR disable routes through the `GBrainOAuthProvider` `dcrDisabled` constructor option (not a router monkey-patch); `transport.handleRequest` wrapped in try/catch to return a JSON-RPC 500 envelope; OperationError + unexpected exceptions unified through `buildError` / `serializeError` so `/mcp` always returns the same envelope. `/health` is liveness-only via `probeLiveness(sql, engineName, version, timeoutMs)` racing `sql\`SELECT 1\`` against the exported `HEALTH_TIMEOUT_MS = 3000` (returns the same `ProbeHealthResult` tagged-union as `probeHealth`, single timer-cleanup site, single 503 envelope); body shape `{status, version, engine}` only. Full stats moved to admin-only `/admin/api/full-stats` (gated by `requireAdmin`, calls `probeHealth(engine, ...)`) — keeps `getStats()`'s 6× count(*) off the public route so a saturated pool doesn't trigger orchestrator restart cascades. Every OAuth/admin/audit SQL call routes through `sqlQueryForEngine(engine)` from `src/core/sql-query.ts` so it works against PGLite; the four `mcp_request_log.params` INSERT sites (success / auth_failed / scope_denied / server-error) go through `executeRawJsonb(engine, ...)` so the column stores real objects (`params->>'op'` returns `search`, not the quoted string). `--bind HOST` defaults `127.0.0.1` (self-hosters pass `--bind 0.0.0.0`); a stderr WARN fires when `--public-url` is set without `--bind`; the banner prints a `Bind:` line. `AuthInfo.sourceId` + `AuthInfo.allowedSources` + `AuthInfo.takesHoldersAllowList` are the typed source of truth, populated by `oauth-provider.ts:verifyAccessToken` (source scope from the `oauth_clients` row; takes-holders from `access_tokens.permissions.takes_holders` for legacy bearer tokens). The `/mcp` dispatch site reads `authInfo.takesHoldersAllowList ?? ['world']` — absent grants (OAuth-client tokens, pre-v29 brains) fail closed to world-only takes visibility, while an explicit `[]` grant is preserved as deny-all; pinned end-to-end by `test/e2e/serve-http-takes-holders.test.ts`. The HTTP MCP `tools/list` handler at `:837-849` uses `paramDefToSchema(v)` from `src/mcp/tool-defs.ts` so array params keep `items` (strict-mode OAuth clients otherwise reject the whole tool list). `POST /ingest` enforces the slug-prefix write fence at the ROUTE, not the op layer: the route hands its payload to the `ingest_capture` minion handler, which deliberately bypasses `put_page`, so no `OperationContext` exists and `enforceClientSlugFence` never runs — a slug-bound client must therefore supply `X-Gbrain-Slug` and it must satisfy `slugUnderBoundPrefixes`, else 403 (without the check a bound client could overwrite any page, in the `default` source, since untrusted payloads carry no source grant).+- `src/commands/serve-http.ts` — Express 5 HTTP MCP server with OAuth 2.1, admin dashboard, and SSE live activity feed. Started via `gbrain serve --http [--port N] [--token-ttl N] [--enable-dcr] [--public-url URL] [--bind HOST] [--log-full-params]`. Combines MCP SDK's `mcpAuthRouter` (authorize/token/register/revoke), a custom `client_credentials` handler running BEFORE the router (SDK's token endpoint throws `UnsupportedGrantTypeError` for CC; custom handler falls through for `auth_code` / `refresh_token`), `requireBearerAuth` middleware for `/mcp` with scope enforcement + `localOnly` rejection before op dispatch, and `express-rate-limit` at 50 req / 15 min on `/token`. Serves the built admin SPA from `admin/dist/` with SPA fallback. `/admin/events` SSE broadcasts every MCP request. `cookie-parser` wired (Express 5 has no built-in). Startup logging prints port, engine, issuer URL (honors `--public-url`), client count, DCR status, and the admin bootstrap token line — but the generated token's raw value only prints when stderr is an interactive TTY (`shouldSuppressBootstrapPrint`): a non-TTY (containerized/piped) start hides it so the secret never lands in centralized log storage, env-sourced tokens (`$GBRAIN_ADMIN_BOOTSTRAP_TOKEN`) are always hidden, `--print-admin-token` forces the raw value on a trusted terminal, and `--suppress-bootstrap-token` hides everything. The `/mcp` request handler's OperationContext literal sets `remote: true` explicitly (without it `submit_job`'s protected-name guard at `operations.ts:1391` saw a falsy undefined and a `read+write`-scoped OAuth token could submit `shell` jobs — RCE). `summarizeMcpParams` from `src/mcp/dispatch.ts` feeds both `mcp_request_log` writes and the SSE feed by default (raw via `--log-full-params`). Cookie `Secure` flag set behind HTTPS or a public-URL proxy; magic-link nonce store LRU-bounded; DCR disable routes through the `GBrainOAuthProvider` `dcrDisabled` constructor option (not a router monkey-patch); `transport.handleRequest` wrapped in try/catch to return a JSON-RPC 500 envelope; OperationError + unexpected exceptions unified through `buildError` / `serializeError` so `/mcp` always returns the same envelope. `/health` is liveness-only via `probeLiveness(sql, engineName, version, timeoutMs)` racing `sql\`SELECT 1\`` against the exported `HEALTH_TIMEOUT_MS = 3000` (returns the same `ProbeHealthResult` tagged-union as `probeHealth`, single timer-cleanup site, single 503 envelope); body shape `{status, version, engine}` only. Full stats moved to admin-only `/admin/api/full-stats` (gated by `requireAdmin`, calls `probeHealth(engine, ...)`) — keeps `getStats()`'s 6× count(*) off the public route so a saturated pool doesn't trigger orchestrator restart cascades. Every OAuth/admin/audit SQL call routes through `sqlQueryForEngine(engine)` from `src/core/sql-query.ts` so it works against PGLite; the four `mcp_request_log.params` INSERT sites (success / auth_failed / scope_denied / server-error) go through `executeRawJsonb(engine, ...)` so the column stores real objects (`params->>'op'` returns `search`, not the quoted string). `--bind HOST` defaults `127.0.0.1` (self-hosters pass `--bind 0.0.0.0`); a stderr WARN fires when `--public-url` is set without `--bind`; the banner prints a `Bind:` line. `AuthInfo.sourceId` + `AuthInfo.allowedSources` + `AuthInfo.takesHoldersAllowList` are the typed source of truth, populated by `oauth-provider.ts:verifyAccessToken` (source scope from the `oauth_clients` row; takes-holders from `access_tokens.permissions.takes_holders` for legacy bearer tokens). The `/mcp` dispatch site reads `authInfo.takesHoldersAllowList ?? ['world']` — absent grants (OAuth-client tokens, pre-v29 brains) fail closed to world-only takes visibility, while an explicit `[]` grant is preserved as deny-all; pinned end-to-end by `test/e2e/serve-http-takes-holders.test.ts`. The HTTP MCP `tools/list` handler at `:837-849` uses `paramDefToSchema(v)` from `src/mcp/tool-defs.ts` so array params keep `items` (strict-mode OAuth clients otherwise reject the whole tool list). `POST /ingest` enforces the slug-prefix write fence at the ROUTE, not the op layer: the route hands its payload to the `ingest_capture` minion handler, which deliberately bypasses `put_page`, so no `OperationContext` exists and `enforceClientSlugFence` never runs — a slug-bound client must therefore supply `X-Gbrain-Slug` and it must satisfy `slugUnderBoundPrefixes`, else 403 (without the check a bound client could overwrite any page, in the `default` source, since untrusted payloads carry no source grant). The manual mint route `/admin/api/register-client` accepts optional `source` + `federatedRead` bindings mirroring the CLI's `--source`/`--federated-read` (validated via `normalizeSourceInput`/`normalizeFederatedReadInput` from `src/core/source-id.ts`; omitting both preserves the default binding `source_id='default'` / `federated_read=[source_id]`, invalid values return a structured 400 `invalid_source`), so an admin SPA or provisioning proxy can mint a source-confined client over HTTP; pinned by `test/register-client-source-normalize.test.ts`.- `src/core/sql-query.ts` — engine-aware tagged-template SQL adapter for OAuth/admin/auth infrastructure. `sqlQueryForEngine(engine)` returns a `SqlQuery` (`(strings, ...values) => Promise<rows[]>`) that walks the template, builds `$N` positional SQL, asserts every value is a `SqlValue` (string | number | bigint | boolean | Date | null), and routes through `engine.executeRaw(sql, params)` (Postgres via postgres.js `unsafe(sql, params)`, PGLite via `db.query(sql, params)`). Deliberately narrower than postgres.js's `sql` tag: no nested fragments, `sql.json()`, `sql.unsafe()`, `sql.begin()`, or array binding — the narrow scalar-only surface is the feature (keeps it from drifting into a partial postgres.js clone). JSONB writes go through `executeRawJsonb(engine, sql, scalarParams, jsonbParams)` which composes positional `$N::jsonb` casts and passes JS **objects** through; an object reaches the wire with the correct type oid, so executeRawJsonb is safe (verified by `test/sql-query.test.ts` on PGLite, `test/e2e/auth-permissions.test.ts:67` on Postgres). Positional binding is NOT universally immune, though: binding a `JSON.stringify(x)` **string** to a bare `$N::jsonb` via `unsafe()` double-encodes it into a jsonb string scalar on real Postgres (the #2339 class; PGLite hides it). Fixes: pass a raw object (executeRawJsonb / `sql.json`), or cast through `$N::text::jsonb`. `scripts/check-jsonb-pattern.sh` (template grep) doesn't fire on `executeRawJsonb(...)` because it passes objects; the positional `$N::jsonb` + `JSON.stringify` form is caught by `scripts/check-jsonb-params.mjs`. Consumed by `src/commands/auth.ts`, `src/commands/serve-http.ts`, `src/core/oauth-provider.ts`, `src/commands/files.ts`, `src/mcp/http-transport.ts` so all five work uniformly against PGLite and Postgres.- `src/commands/serve.ts` — `gbrain serve` stdio MCP entrypoint with idempotent shutdown across every parent-disconnect signal. Stdio EOF, SIGTERM, SIGINT, SIGHUP, and parent-process death (every reparent case — PID 1, launchd subreaper, systemd, tmux, or a parent shell with `PR_SET_CHILD_SUBREAPER`) all funnel into one `cleanup(reason)` that releases the engine and the PGLite write-lock dir within 5 seconds (otherwise the lock is held indefinitely after Claude Desktop / Cursor / launchd-managed gateways disconnect, forcing a 5-minute stale-lock wait on next start). Watchdog reparent check is `getParentPid() !== initialParentPid` (the `=== 1` check missed the subreaper case under launchd/systemd). Bun's `process.ppid` cache is stale across reparenting ([oven-sh/bun#30305](https://github.com/oven-sh/bun/issues/30305)) so `getParentPid()` runs `spawnSync('ps', ['-o', 'ppid=', '-p', PID])` per tick. Startup probe verifies `ps` is on PATH; if not (stripped containers, busybox), the watchdog skips installing AND emits a loud `[gbrain serve] watchdog disabled: ps unavailable ...` stderr line so operators see the degraded mode. Pinned by `test/serve-stdio-lifecycle.test.ts` (22 cases). Credit @Aragorn2046 + @seungsu-kr.- `src/core/oauth-provider.ts` — `GBrainOAuthProvider` implementing the MCP SDK's `OAuthServerProvider` + `OAuthRegisteredClientsStore`. Backed by raw SQL (works on both PGLite and Postgres — OAuth is infrastructure, not a BrainEngine concern). Full OAuth 2.1: `authorize` + `exchangeAuthorizationCode` with PKCE, `client_credentials`, `refresh_token` with rotation, `revokeToken`, `registerClient` (DCR validates redirect_uri is `https://` or loopback per RFC 6749 §3.1.2.1). All tokens + client secrets SHA-256 hashed before storage. Auth codes single-use with 10-minute TTL via atomic `DELETE...RETURNING` (closes RFC 6749 §10.5 TOCTOU); refresh rotation also `DELETE...RETURNING` (§10.4 stolen-token detection). `pgArray()` escapes commas/quotes/braces so a comma-bearing redirect_uri can't smuggle a second array element. Legacy `access_tokens` fallback in `verifyAccessToken` grandfathers pre-v0.26 bearer tokens as `read+write+admin`, and threads BOTH stored grants off the token's `permissions` JSONB: `source_id` via `parseLegacyTokenScope` and `takes_holders` via `parseTakesHoldersAllowList` (both in `src/core/legacy-token-scope.ts`, shared with the legacy HTTP transport so the two transports cannot drift; `[]` takes-holders preserved as explicit deny-all, missing/non-array → undefined → the `/mcp` dispatch site's fail-closed `['world']`; OAuth-client tokens carry no takes-holders grant pending per-client storage — TODOS.md). `sweepExpiredTokens()` runs on startup in try/catch and returns the count via `RETURNING 1` + array length. RFC hardening: `client_id` folded atomically into the `DELETE WHERE` for both auth-code exchange and refresh rotation (wrong-client paths don't burn the row); refresh-scope-subset enforced against the original grant on the row (RFC 6749 §6, so revoking a scope shrinks existing refresh tokens); `client_id` bound on `revokeToken` (RFC 7009 §2.1); `/token` `redirect_uri` validated against the `/authorize` value (RFC 6749 §4.1.3, empty-string treated as missing not wildcard); bare `catch {}` in `verifyAccessToken`/`getClient` replaced by `isUndefinedColumnError` from `src/core/utils.ts` (only SQLSTATE 42703 falls through to legacy; lock timeouts/network blips throw); `dcrDisabled` constructor option lets `serve-http.ts` disable `/register` without monkey-patching the router. Module-private `coerceTimestamp()` normalizes postgres-driver-as-string BIGINT columns to JS numbers at 5 read sites (`getClient` for RFC 7591 §3.2.1 numeric timestamps, `exchangeRefreshToken` + `verifyAccessToken` for the SDK's `typeof === 'number'` check); throws on NaN/Infinity (fail loud at boundary), returns undefined for SQL NULL (callers treat NULL as expired). Not promoted to `utils.ts` — generic BIGINT precision-loss risk. `registerClient` honors `token_endpoint_auth_method: "none"` (RFC 7591 §3.2.1): public PKCE clients store `client_secret_hash = NULL` and the response omits `client_secret`; confidential clients (`client_secret_post` / `client_secret_basic`) keep their one-time-reveal shape; `getClient` normalizes NULL `client_secret_hash` to JS `undefined` so the SDK's clientAuth path accepts public clients. `verifyAccessToken` JOINs `oauth_clients.source_id` (write scope, scalar) + `oauth_clients.federated_read` (read scope, TEXT[]) + `oauth_clients.bound_slug_prefixes` (write fence, TEXT[] — consumed by `enforceClientSlugFence` in `operations.ts`) onto the returned `AuthInfo`; legacy brains degrade via `isUndefinedColumnError` fallback, dropping the newest projection first. `rescopeClient(clientId, {sourceId?, federatedRead?, boundSlugPrefixes?})` is the trusted-operator rescope (CLI `gbrain auth rescope-client`, admin `POST /admin/api/rescope-client`); `boundSlugPrefixes` is tri-state — undefined leaves the binding untouched, `null` clears it, a non-empty array replaces it (explicit empty array rejected as ambiguous deny-all) — so roster churn updates the write fence in place without rotating secrets.@@ -294,7 +294,7 @@ per-release `**vX.Y.Z:**` narration — CI enforces this- `src/commands/connect.ts` + `src/core/connect-probe.ts` — `gbrain connect <mcp-url> [--token <bearer>]` one-command coding-agent onboarding from a bearer token. Turns an MCP URL + token into a paste-ready `claude mcp add ... -H "Authorization: Bearer ..."` block (default) or, with `--install`, runs it directly and smoke-tests the token. Direct HTTP MCP — Claude Code talks straight to a remote `gbrain serve --http`, no local install needed. Token resolution: `--token` > `$GBRAIN_REMOTE_TOKEN` > placeholder (print) / error (install). The generated block tells the agent to call `get_brain_identity` + `list_skills` (the `LEARN_INSTRUCTION` export, which names `put_page` not `capture` since `capture` is CLI-only, not an MCP tool) with a core-tools fallback for hosts without skill publishing. URL normalization appends `/mcp` to a bare host but REJECTS a scheme-less host; pure helpers (`isLinkLocalOrMetadata`, URL parse, render) are unit-tested. Flags: `--token`, `--name <id>` (default `gbrain`, validated against `NAME_RE`), `--agent claude-code|codex|perplexity|generic`, `--install`, `--yes` (required for `--install` in non-TTY), `--force`, `--json` (token redacted unless `--show-token`), `--timeout-ms`. `connect` is in `CLI_ONLY` + `CLI_ONLY_SELF_HELP`; dispatched in `cli.ts:handleCliOnly` with no local DB connect. `AGENT_SPECS` drives per-agent rendering + `--install`: `claude-code` → `buildClaudeMcpAddArgv` (literal `-H "Authorization: Bearer <tok>"`); `codex` → `buildCodexMcpAddArgv` = `codex mcp add <name> --url <url> --bearer-token-env-var GBRAIN_REMOTE_TOKEN` (Codex reads the token from the env var at runtime, never written to config; `--install` runs it and prints an `export GBRAIN_REMOTE_TOKEN` hint when missing); `perplexity` + `generic` are `installable:false` and reject `--install`. `--oauth` (`supportsOAuth:true` = perplexity/generic only) emits an OAuth 2.1 client-credentials connector block (Issuer URL via `issuerFromMcpUrl` = mcp-url minus `/mcp`, Client ID, Client Secret) — least-privilege scopes + short-lived rotating tokens vs a long-lived full-access secret. Creds from `--client-id`/`--client-secret` (BYO) or `--register` (`deps.registerOAuthClient` shells `gbrain auth register-client <name> --grant-types client_credentials --scopes <DEFAULT_SCOPES="read write"> --token-endpoint-auth-method client_secret_post` and parses `Client ID:`/`Client Secret:`); `--oauth` rejected for claude-code/codex and incompatible with `--install`. `buildJson` is a generic shape (`agent`, `command`/`command_argv` null for perplexity/generic, `header`, `env_var`, oauth fields with redaction); the codex `command` carries only the env-var name, never the token. `cmdString(binary, argv)` POSIX-single-quotes args. `ConnectDeps` = `{isTTY, promptYesNo, hasBinary(bin), runBinary(bin, argv), probe, env(name)}` — binary-generic so `claude` and `codex` share the path; `env` injectable for tests. Security: rendered command single-quotes the token so shell metacharacters can't run code when pasted; token validated before it lands in an HTTP header; link-local / cloud-metadata addresses (incl. IPv4-mapped IPv6 `::ffff:169.254.x.x` and AWS IMDSv2-over-IPv6 `fd00:ec2::254`) refused as a token-exfil guard while localhost/RFC1918/LAN stay allowed; token redacted from all error output. `src/core/connect-probe.ts` is the raw-bearer MCP smoke probe backing `--install`: connects the official MCP SDK `Client` over `StreamableHTTPClientTransport` with a STATIC `Authorization` header (no OAuth/discovery — distinct from `mcp-client.ts:callRemoteTool` which is OAuth-only and `remote-mcp-probe.ts:smokeTestMcp` which only sends `initialize`), runs the full `initialize` handshake via `client.connect()`, then calls `get_brain_identity` (read-scope, non-localOnly) to prove a tool call round-trips. Never throws — every failure maps to `{ ok: false, reason: 'auth' | 'unreachable' | 'timeout' | 'tool_error' | 'unknown', message }` so a wrong/expired token fails at setup, not on the agent's first request. `DEFAULT_PROBE_TIMEOUT_MS = 15_000` shared with `connect.ts`. `serve-http.ts` adds exported pure `skillPublishStatus(publishSkills)` for the startup banner `Skills: published / not published` line + a one-line `gbrain config set mcp.publish_skills true` stderr nudge when publishing is OFF. Docs: `docs/mcp/CODEX.md`, `docs/mcp/PERPLEXITY.md`, `docs/mcp/CLAUDE_CODE.md`, `docs/tutorials/connect-coding-agent.md`. Pinned by `test/connect.test.ts` (pure-helper + render, all four agents) + `test/e2e/connect-bearer.test.ts` (raw-bearer probe + full OAuth chain register→connect→discovery→`/token` mint→`get_brain_identity`, client registered in `beforeAll` before serve takes the PGLite single-writer lock; drives real `claude` + `codex` binaries through `connect --install` with sandboxed `HOME`/`CODEX_HOME`, asserts registration + token never in Codex config, skips when a binary is absent) + `test/e2e/serve-stdio-roundtrip.test.ts` (spawns real `gbrain serve` stdio against a fresh `init --pglite` brain, drives the SDK client through `initialize`→`tools/list`→`tools/call`, asserts the advertised core-tool set and that `capture` is NOT advertised) + `test/serve-skills-publish-nudge.test.ts` (the `test/audit/batch-retry-audit.test.ts` ENOENT case was made hermetic — it had read the real `~/.gbrain/audit`).- `src/commands/upgrade.ts` — self-update CLI. `runPostUpgrade()` enumerates migrations from the TS registry (`src/commands/migrations/index.ts`) and tail-calls `runApplyMigrations(['--yes', '--non-interactive'])` so the mechanical side of every outstanding migration runs unconditionally.- `src/commands/migrations/` — TS migration registry (compiled into the binary; no runtime walk of `skills/migrations/*.md`). `index.ts` lists migrations in semver order. `v0_11_0.ts` = Minions adoption orchestrator (8 phases). `v0_12_0.ts` = Knowledge Graph auto-wire orchestrator (5 phases: schema → config check → backfill links → backfill timeline → verify); `phaseASchema` has a 600s timeout for duplicate-heavy brains. `v0_12_2.ts` = JSONB double-encode repair orchestrator (4 phases: schema → repair-jsonb → verify → record). `v0_14_0.ts` = shell-jobs + autopilot cooperative (pending-host-work ping for skills/migrations/v0.14.0.md). All orchestrators are idempotent and resumable from `partial` status. The RUNNER owns all ledger writes — orchestrators return `OrchestratorResult` and `apply-migrations.ts` persists a canonical `{version, status, phases}` shape (orchestrators no longer call `appendCompletedMigration`). `statusForVersion` prefers `complete` over `partial` (never regresses); 3 consecutive partials → wedged → `--force-retry <version>` writes a `'retry'` reset marker. Schema-only migrations v14 (`pages_updated_at_index`) + v15 (`minion_jobs_max_stalled_default_5` with UPDATE backfill) live in the `MIGRATIONS` array in `src/core/migrate.ts`. `in-process.ts` exports `runMigrateOnlyCore({timeoutMs?})` — single source of truth for "bring schema to head" (`configureGateway` → `createEngine` → `connect` → `initSchema` → `disconnect`, idempotent, 600s `MIGRATE_ONLY_TIMEOUT_MS` guard, throws `MigrateOnlyError` on no-config / timeout); the orchestrators' 9 schema phases AND `init.ts:initMigrateOnly` both delegate to it so schema bring-up can't drift (running in-process removes the spawn that died with `getaddrinfo ENOTFOUND` on Windows + bun + Supabase pooler). `runGbrainSubprocess` is the diagnostic wrapper for the remaining non-schema spawns (extract/repair/stats): captures child stderr (64MB buffer) into the thrown error. `v0_13_1.ts:phaseCGrandfather` is a CHUNKED bulk SQL pass keyed on `pages.id` (globally unique PK, NOT slug — slug uniqueness is `(source_id, slug)`), filters `deleted_at IS NULL` (no tombstones), chunked in `CHUNK_SIZE` batches (DELETE_BATCH_SIZE convention) for bounded lock-hold; the rollback log carries `{id, slug, source_id, pre_frontmatter}` so rollback is unambiguous across sources; idempotent + resumable (each UPDATE flips its rows out of `GRANDFATHER_WHERE`). Pinned by `test/migration-in-process.serial.test.ts` and `test/migrations-v0_13_1-grandfather.test.ts`.-- `src/commands/repair-jsonb.ts` — `gbrain repair-jsonb [--dry-run] [--json]`: rewrites `jsonb_typeof='string'` rows in place across 5 affected columns (pages.frontmatter, raw_data.data, ingest_log.pages_updated, files.metadata, page_versions.frontmatter). Fixes the double-encode bug on Postgres; PGLite no-ops. Idempotent.+- `src/commands/repair-jsonb.ts` — `gbrain repair-jsonb [--dry-run] [--json]`: rewrites `jsonb_typeof='string'` rows in place across 8 affected columns (pages.frontmatter, raw_data.data, ingest_log.pages_updated, files.metadata, page_versions.frontmatter, subagent_messages.content_blocks, subagent_tool_executions.input, subagent_tool_executions.output). The subagent targets are `jsonPayloadOnly`: those columns can legitimately hold jsonb string scalars (persistToolExec binds a tool's pre-serialized string payload as-is), so their damage predicate additionally requires container-shaped content (`^\s*[\[{]`) that `pg_input_is_valid(..., 'jsonb')` (PG16+ floor, same as the IS JSON predicate `updateSourceConfig` relies on) actually parses — a plain-text value that merely starts with `[` or `{` is never flagged or corrupted. Targets whose table doesn't exist on the brain are skipped via `to_regclass` (pre-subagent-era brains), and one target's failure is recorded on stderr while the run continues — earlier repairs are already committed and the v0_12_2 migration orchestrator JSON-parses stdout. `runDoctor`'s `jsonb_integrity` check counts damage with the same predicate over the same target list. Fixes the double-encode bug on Postgres; PGLite no-ops. Idempotent. Pinned by `test/repair-jsonb.test.ts` + `test/doctor.test.ts`.- `src/commands/orphans.ts` — `gbrain orphans [--json] [--count] [--include-pseudo] [--source <id>]`: surfaces pages with zero inbound wikilinks, grouped by domain (auto-generated/raw/pseudo filtered by default). Also exposed as `find_orphans` MCP op. `findOrphans`/`getOrphansData` (the canonical pure fn shared with doctor's `orphan_ratio`) takes `{ sourceId?, sourceIds? }`; `BrainEngine.findOrphanPages(opts?)` (both engines) scopes ONLY the candidate side (`p.source_id = $1` scalar, or `= ANY($1::text[])` federated) while still counting inbound links from ANY source — a page in source X linked FROM source Y is reachable, so NOT an orphan of X (deliberate definition; the stricter intra-source-only reading is rejected). `--source` is an explicit raw-flag parse (NOT `resolveSourceWithTier`, which would scope bare invocations to a default). The `total_linkable` denominator enumerates ALL live pages (scoped) and subtracts every excluded-by-slug page (templates/, scratch/, etc.) so excluded NON-orphan pages with inbound links don't inflate it and suppress warnings. The `find_orphans` MCP op threads `sourceScopeOpts(ctx)` so a source-bound OAuth client doesn't see brain-wide orphans. `gbrain doctor --source <id>` scopes `orphan_ratio` and, under explicit `--source` below 100 entity pages, reports the ratio with a low-scale caveat (thin-client `doctor --source` orphan_ratio remains brain-wide — TODO). Pinned by `test/orphans-source-scope.test.ts` (PGLite) + `test/e2e/engine-parity.test.ts` (Postgres↔PGLite scalar + federated parity). Contributed by @knee5.- `src/commands/salience.ts` — `gbrain salience [--days N] [--limit N] [--kind PREFIX] [--json]`: pages ranked by emotional + activity salience over a recency window. Mirrors orphans.ts shape (pure data fn + JSON formatter + human formatter). Calls `engine.getRecentSalience(opts)`. Score formula: `(emotional_weight × 5) + ln(1 + active_take_count) + 1/(1 + days_since_update)`.- `src/commands/anomalies.ts` — `gbrain anomalies [--since YYYY-MM-DD] [--lookback-days N] [--sigma N] [--json]`: cohort-level activity outliers. Calls `engine.findAnomalies(opts)`. Two cohort kinds: tag, type.@@ -314,6 +314,8 @@ per-release `**vX.Y.Z:**` narration — CI enforces this- `src/core/progress.ts` — Shared bulk-action progress reporter. Writes to stderr. Modes: `auto` (TTY `\r`-rewriting; non-TTY plain lines), `human`, `json` (JSONL), `quiet`. Rate-gated by `minIntervalMs` and `minItems`. `startHeartbeat(reporter, note)` for single long queries. `child()` composes phase paths. Singleton SIGINT/SIGTERM coordinator emits `abort` events for every live phase. EPIPE defense on both sync throws and stream `'error'` events. Zero dependencies. `emitHumanLine` is prefix-aware — inside a `withSourcePrefix(id, ...)` scope from `src/core/console-prefix.ts` it prepends `[id] ` (and TTY-rewrite mode `\r\x1b[2K` carries the prefix inside the clear-to-EOL escape); `emitJson` is intentionally NOT prefixed so NDJSON consumers don't choke on a `[id] {...}` shape.- `src/core/console-prefix.ts` — `AsyncLocalStorage<string>`-backed per-source line-prefix helper. Exports `withSourcePrefix(id, fn)` (runs `fn` with `id` as active prefix; nested wraps replace then restore), `getSourcePrefix()` (read-only accessor; test seam), and `slog(...)` / `serr(...)` (prefix-aware `console.log`/`console.error`). Embedded-newline-safe: a multi-line string under prefix `[foo]` emits `[foo] line1\n[foo] line2`. Outside a wrap, `slog`/`serr` fall through to bare `console.log`/`console.error` so single-source callers see identical output (back-compat invariant). Use `src.id` (slug-validated by `sources add`) NOT `src.name` (free-form) to defeat log-injection through newline/control-character names. Coverage: `src/commands/sync.ts` performSync + callees, `src/commands/embed.ts` runEmbedCore + helpers, `src/core/progress.ts` emitHumanLine.- `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.+- `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. 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`.- `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.diff --git a/docs/architecture/thin-client.md b/docs/architecture/thin-client.mdindex 781e80c25c..91a6f5537e 100644--- a/docs/architecture/thin-client.md+++ b/docs/architecture/thin-client.md@@ -64,7 +64,7 @@ Key files:thin-client routing branches. These commands bypass the operation-layerdispatch in cli.ts (call `engine.foo()` directly), so each gets its own`if (isThinClient(cfg)) { callRemoteTool(...) }` branch that maps CLI flags- to op params. `think` is a special case: the server's `think` op- intentionally disables `--save`/`--take` for remote callers- (operations.ts:1103-1135 trust-boundary gate); thin-client `think` warns- loudly when those flags are set.+ to op params. `think` is a special case: the server's `think` op is+ read-scoped for OAuth/MCP and intentionally disables `--save`/`--take` for+ remote callers in its trust-boundary gate; thin-client `think` warns loudly+ when those flags are set.diff --git a/package.json b/package.jsonindex b034bd26ad..2c48378c6b 100644--- a/package.json+++ b/package.json@@ -37,6 +37,7 @@"build:admin-embedded": "bun run scripts/build-admin-embedded.ts","build:schema": "bash scripts/build-schema.sh","build:llms": "bun run scripts/build-llms.ts",+ "build:flag-registry": "bun run scripts/generate-flag-registry.ts","build:pglite-snapshot": "bun run scripts/build-pglite-snapshot.ts","test": "bash scripts/run-unit-parallel.sh","eval:autocut": "bun test test/search/autocut-eval.test.ts",@@ -148,7 +149,7 @@"bun": ">=1.3.10"},"license": "MIT",- "version": "0.42.75.0",+ "version": "0.42.76.0","overrides": {"@hono/node-server": "^2.0.5","fast-uri": "^3.1.5",diff --git a/scripts/generate-flag-registry.ts b/scripts/generate-flag-registry.tsnew file mode 100644index 0000000000..4b22efd11b--- /dev/null+++ b/scripts/generate-flag-registry.ts@@ -0,0 +1,177 @@+/**+ * #2185 — known-flags registry generator for CLI_ONLY commands.+ *+ * gbrain's CLI_ONLY commands read flags ad hoc (`args.includes('--force')`,+ * per-command parseFlags helpers), so there is no parser to make strict. The+ * pre-dispatch validator in src/cli.ts needs to know each command's legal+ * flags; this script derives them from the source instead of a hand-typed+ * list that would rot.+ *+ * How: parse handleCliOnly's top-level `case 'X': {` blocks out of src/cli.ts,+ * collect every `import('./commands/Y.ts')` inside each block, then scan the+ * case-block text plus each imported module (plus one level of that module's+ * ./relative same-directory imports) for `--flag` string literals — including+ * help text, which deliberately over-includes: accepting a flag the handler+ * ignores is the pre-#2185 status quo for that flag, while missing a real+ * flag would break working invocations on upgrade.+ *+ * Output: src/core/cli-flag-registry.generated.ts (committed; freshness is+ * pinned by test/cli-flag-validation.test.ts the same way build:llms pins the+ * llms bundles). Regenerate: bun run build:flag-registry+ *+ * Hand-tuning lane: EXTRA_FLAGS below, for flags that live deeper than the+ * one-level scan (add with a comment naming the deep module).+ */+import { readFileSync, writeFileSync, existsSync } from 'fs';+import { dirname, resolve as resolvePath, join } from 'path';+import { fileURLToPath } from 'url';++const ROOT = resolvePath(dirname(fileURLToPath(import.meta.url)), '..');++/** Flags that live deeper than the one-level module scan. Keep commented. */+const EXTRA_FLAGS: Record<string, string[]> = {+ // embed's pace knobs resolve inside src/core/pace-mode.ts (two levels deep).+ embed: ['--pace', '--pace-max-concurrency'],+ // sync shares the same pace surface via env/config plus CLI passthrough.+ sync: ['--pace', '--pace-max-concurrency'],+};++/** Universal helper flags every command may see (parsed or short-circuited upstream). */+const UNIVERSAL_FLAGS = ['--help', '--json', '--brain', '--source'];++const FLAG_RE = /--[a-z0-9][a-z0-9-]*/g;++function flagsInText(text: string): Set<string> {+ const out = new Set<string>();+ for (const m of text.matchAll(FLAG_RE)) {+ // Template-literal prefixes (`--bound-${key}` scans as `--bound-`) are+ // not real flags — a trailing hyphen would make the validator accept+ // every typo sharing the prefix.+ if (!m[0].endsWith('-')) out.add(m[0]);+ }+ return out;+}++/** One level of ./relative imports (static or dynamic) from a module's source. */+function relativeImports(src: string, fromDir: string): string[] {+ const paths = new Set<string>();+ for (const m of src.matchAll(/from\s+'(\.\.?\/[^']+\.ts)'/g)) paths.add(m[1]);+ for (const m of src.matchAll(/import\('(\.\.?\/[^']+\.ts)'\)/g)) paths.add(m[1]);+ return [...paths]+ .map(p => resolvePath(fromDir, p))+ .filter(p => existsSync(p));+}++export function buildFlagRegistry(): Record<string, string[]> {+ const cliSource = readFileSync(join(ROOT, 'src/cli.ts'), 'utf-8');++ // CLI_ONLY membership (the single source of truth in src/cli.ts). Strip+ // line comments first — the set literal carries commentary whose quoted+ // words ('Unknown command', 'pages') must not parse as members.+ const onlyMatch = cliSource.match(/const CLI_ONLY = new Set(?:<string>)?\(\[([\s\S]*?)\]\)/);+ if (!onlyMatch) throw new Error('CLI_ONLY set not found in src/cli.ts');+ const onlyBody = onlyMatch[1].replace(/\/\/[^\n]*/g, '');+ const commands = [...onlyBody.matchAll(/'([^']+)'/g)].map(m => m[1]);++ // handleCliOnly body — bounded at the function's closing brace (column 0).+ // Unbounded, the LAST case block absorbed every --flag literal in the rest+ // of cli.ts (printHelp's full flag surface included), handing whichever+ // command sits last in the switch a ~100-flag junk allowlist that made+ // strict validation a no-op for it.+ const fnStart = cliSource.indexOf('async function handleCliOnly');+ if (fnStart < 0) throw new Error('handleCliOnly not found in src/cli.ts');+ const fnTail = cliSource.slice(fnStart);+ const fnEndRel = fnTail.search(/\n\}\n/);+ const fnSrc = fnEndRel > 0 ? fnTail.slice(0, fnEndRel) : fnTail;++ // handleCliOnly dispatches through TWO styles: an `if (command === 'X')`+ // chain (DB-free commands like init/auth/schema) and a switch with+ // `case 'X':` labels. Segment on BOTH marker kinds; the text between a+ // marker and the next marker belongs to that label.+ const markRe = /(?:^\s*if \(command === '([a-z0-9-]+)'\)|^ case '([a-z0-9-]+)':)/gm;+ const marks: Array<{ label: string; start: number }> = [];+ let m: RegExpExecArray | null;+ while ((m = markRe.exec(fnSrc)) !== null) {+ marks.push({ label: (m[1] ?? m[2])!, start: m.index });+ }++ const blocks = new Map<string, string>();+ for (let i = 0; i < marks.length; i++) {+ const end = i + 1 < marks.length ? marks[i + 1].start : fnSrc.length;+ const body = fnSrc.slice(marks[i].start, end);+ // Fall-through labels share the following block.+ blocks.set(marks[i].label, (blocks.get(marks[i].label) ?? '') + body);+ }++ // Safety flags carry destructive-bypass semantics: allowlisting one that+ // the handler never reads recreates the #2185 repro (`post-upgrade+ // --dry-run` accepted, ignored, migrations run for real). Presence isn't+ // enough — upgrade.ts prints a HINT naming another command's --dry-run,+ // which is depth-0 text for post-upgrade. These flags are only legal with+ // CONSUMPTION evidence in the command's own code: the flag as a TIGHT-QUOTED+ // standalone literal (`includes('--dry-run')`, `has('--dry-run')`,+ // `=== '--dry-run'`). Prose bleed embeds the flag inside a longer string, so+ // it never has quotes on both sides of the bare flag.+ const SAFETY_FLAGS = new Set(['--dry-run']);+ const consumes = (text: string, flag: string): boolean =>+ new RegExp(`['"\`]${flag}['"\`]`).test(text);++ const registry: Record<string, string[]> = {};+ for (const command of commands) {+ const block = blocks.get(command) ?? '';+ const flags = new Set<string>(UNIVERSAL_FLAGS);+ const depthZero = new Set<string>();+ let depthZeroText = block;+ for (const f of flagsInText(block)) { flags.add(f); depthZero.add(f); }++ // Modules imported inside the case block, plus one level of each module's+ // own ./relative imports.+ const commandModules = [...block.matchAll(/import\('(\.\/[^']+\.ts)'\)/g)]+ .map(mm => resolvePath(join(ROOT, 'src'), mm[1]))+ .filter(p => existsSync(p));+ for (const modPath of commandModules) {+ const modSrc = readFileSync(modPath, 'utf-8');+ depthZeroText += modSrc;+ for (const f of flagsInText(modSrc)) { flags.add(f); depthZero.add(f); }+ for (const dep of relativeImports(modSrc, dirname(modPath))) {+ for (const f of flagsInText(readFileSync(dep, 'utf-8'))) flags.add(f);+ }+ }++ for (const f of EXTRA_FLAGS[command] ?? []) { flags.add(f); depthZero.add(f); }+ for (const f of SAFETY_FLAGS) {+ if (flags.has(f) && !consumes(depthZeroText, f)) flags.delete(f);+ }+ registry[command] = [...flags].sort();+ }+ return registry;+}++export function renderRegistryModule(registry: Record<string, string[]>): string {+ const entries = Object.keys(registry)+ .sort()+ .map(cmd => ` '${cmd}': [${registry[cmd].map(f => `'${f}'`).join(', ')}],`)+ .join('\n');+ return `// AUTO-GENERATED by scripts/generate-flag-registry.ts — do not edit by hand.+// Regenerate: bun run build:flag-registry+// Freshness + drift pinned by test/cli-flag-validation.test.ts (#2185).+//+// Per-command legal flags for CLI_ONLY commands, derived from each command's+// source (case block + imported modules + one level of relative imports ++// scripts/generate-flag-registry.ts EXTRA_FLAGS). Deliberately over-inclusive+// (help-text mentions count): accepting an ignored flag is the pre-#2185+// status quo; missing a real one breaks working invocations.+export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {+${entries}+};+`;+}++if (import.meta.main) {+ const registry = buildFlagRegistry();+ const outPath = join(ROOT, 'src/core/cli-flag-registry.generated.ts');+ writeFileSync(outPath, renderRegistryModule(registry));+ const n = Object.keys(registry).length;+ const total = Object.values(registry).reduce((a, v) => a + v.length, 0);+ console.log(`wrote ${outPath} (${n} commands, ${total} flag entries)`);+}diff --git a/scripts/run-unit-parallel.sh b/scripts/run-unit-parallel.shindex b65210938e..56e8cb0777 100755--- a/scripts/run-unit-parallel.sh+++ b/scripts/run-unit-parallel.sh@@ -13,9 +13,29 @@## Env overrides:# SHARDS=N same as --shards-# GBRAIN_TEST_SHARD_TIMEOUT per-shard wallclock cap, seconds (default 1500)+# GBRAIN_TEST_SHARD_TIMEOUT per-shard wallclock cap, seconds (default 3000)# GBRAIN_TEST_SHARD_KILL_AFTER grace after TERM before KILL (default 30)# GBRAIN_TEST_MAX_CONCURRENCY passed through to bun test (default 4)+# GBRAIN_TEST_MEM_PER_FILE_MB memory budget per concurrent test file used by+# the adaptive sizing below (default 1536 — a+# PGLite WASM instance reserves ~1-1.5GB)+# GBRAIN_TEST_NO_MEM_ADAPT=1 disable memory-aware concurrency reduction+# GBRAIN_TEST_NO_OOM_FALLBACK=1 disable the serial OOM-rescue pass+#+# Memory safety (two layers; both default-on):+# 1. ADAPTIVE SIZING — before spawning, total concurrency (shards ×+# intra-shard --max-concurrency) is capped to what available memory can+# hold at GBRAIN_TEST_MEM_PER_FILE_MB per concurrent file. Concurrent+# Conductor workspaces running their own suites shrink the budget+# automatically instead of OOMing each other.+# 2. SERIAL PHANTOM RESCUE — two phantom classes are re-run serially+# (--max-concurrency 1) after the parallel pass: (a) failures whose+# shard log carries the PGLite WASM out-of-memory signature, and+# (b) shards killed EXTERNALLY (SIGTERM/SIGKILL well before the shard+# timeout — sibling Conductor workspaces' process cleanup, macOS memory+# jetsam). Phantoms pass serially and the run goes green with an+# oom_rescued note; real failures fail again and stay red. Plain+# assertion failures never match either signature.## Output files (workspace-local; falls back to /tmp if .context/ unwritable):# .context/test-failures.log failure blocks (cleared at start)@@ -38,6 +58,35 @@ detect_cpus() {echo 4}+# ──────────────────────────────────────────────────────────────────────────+# Available-memory detection (MB). macOS: vm_stat free + inactive ++# speculative + purgeable pages (inactive/purgeable are reclaimable on+# pressure, which is exactly the scenario we size for). Linux: MemAvailable.+# Unknown platform → 0, and the caller skips adaptation entirely.+# ──────────────────────────────────────────────────────────────────────────+detect_available_mem_mb() {+ if command -v vm_stat >/dev/null 2>&1; then+ vm_stat 2>/dev/null | awk '+ /page size of/ { psize = $8 }+ /Pages free/ { free = $NF }+ /Pages inactive/ { inactive = $NF }+ /Pages speculative/ { spec = $NF }+ /Pages purgeable/ { purge = $NF }+ END {+ gsub(/\./, "", free); gsub(/\./, "", inactive)+ gsub(/\./, "", spec); gsub(/\./, "", purge)+ if (psize == 0) psize = 16384+ printf "%d\n", (free + inactive + spec + purge) * psize / 1048576+ }'+ return+ fi+ if [ -r /proc/meminfo ]; then+ awk '/MemAvailable/ { printf "%d\n", $2 / 1024; found = 1 } END { if (!found) print 0 }' /proc/meminfo+ return+ fi+ echo 0+}+# ──────────────────────────────────────────────────────────────────────────# Argument parsing. --shards N override wins over $SHARDS; both are clamped.# ──────────────────────────────────────────────────────────────────────────@@ -79,12 +128,60 @@ INTRA_CONC="${MAX_CONCURRENCY_OVERRIDE:-${GBRAIN_TEST_MAX_CONCURRENCY:-4}}"# had completed in 968s. 1500s cap gives ~55% headroom over observed# 4-shard wallclock; real hangs still hit it. Override via# GBRAIN_TEST_SHARD_TIMEOUT=N.-SHARD_TIMEOUT="${GBRAIN_TEST_SHARD_TIMEOUT:-1500}"+# v0.42.74 sizing: 1500 -> 3000. The suite roughly tripled since the 1500s+# cap was set (June: ~3900 tests, 92-migration PGLite replay; now: 11k++# tests, 120-migration replay per PGLite init). At 4 shards, two shards were+# killed at 1500s while making steady per-test progress. 3000s keeps the+# same ~55%-headroom doctrine over observed wallclock; real hangs still die.+SHARD_TIMEOUT="${GBRAIN_TEST_SHARD_TIMEOUT:-3000}"SHARD_KILL_AFTER="${GBRAIN_TEST_SHARD_KILL_AFTER:-30}"if ! printf '%s' "$SHARD_KILL_AFTER" | grep -qE '^[0-9]+$' || [ "$SHARD_KILL_AFTER" -lt 1 ]; thenecho "ERROR: invalid shard kill-after: $SHARD_KILL_AFTER" >&2; exit 2fi+# ──────────────────────────────────────────────────────────────────────────+# Memory-aware concurrency (layer 1). Total concurrent test files =+# N shards × INTRA_CONC; each concurrent file can hold a PGLite WASM+# instance (~1-1.5GB reserved). 4×4 = 16 concurrent instances OOM'd on a+# 128GB machine when other Conductor workspaces ran their suites at the+# same time — every PGLite connect across every shard failed at once+# ("Out of memory" at PGlite.create). Cap total concurrency to what's+# actually available, keeping a 4GB reserve for the OS + bun itself.+# Applies to explicit --shards overrides too (an operator who wants an+# over-committed run sets GBRAIN_TEST_NO_MEM_ADAPT=1).+# ──────────────────────────────────────────────────────────────────────────+MEM_PER_FILE_MB="${GBRAIN_TEST_MEM_PER_FILE_MB:-1536}"+MEM_NOTE=""+if [ "${GBRAIN_TEST_NO_MEM_ADAPT:-0}" != "1" ]; then+ AVAIL_MB=$(detect_available_mem_mb)+ if [ "${AVAIL_MB:-0}" -gt 0 ] 2>/dev/null; then+ BUDGET_MB=$((AVAIL_MB - 4096))+ [ "$BUDGET_MB" -lt "$MEM_PER_FILE_MB" ] && BUDGET_MB="$MEM_PER_FILE_MB"+ MAX_TOTAL=$((BUDGET_MB / MEM_PER_FILE_MB))+ [ "$MAX_TOTAL" -lt 1 ] && MAX_TOTAL=1+ ORIG_N="$N"; ORIG_INTRA="$INTRA_CONC"+ # Shed shards before intra-shard concurrency: fewer bun processes frees+ # more than narrower ones (each process carries its own heap + WASM).+ while [ $((N * INTRA_CONC)) -gt "$MAX_TOTAL" ]; do+ if [ "$N" -gt 1 ]; then N=$((N - 1))+ elif [ "$INTRA_CONC" -gt 1 ]; then INTRA_CONC=$((INTRA_CONC - 1))+ else break+ fi+ done+ if [ "$N" != "$ORIG_N" ] || [ "$INTRA_CONC" != "$ORIG_INTRA" ]; then+ # Fewer shards → more files per shard → each shard legitimately runs+ # longer. Scale the per-shard cap by the shed ratio so adaptation+ # doesn't convert memory safety into false WEDGED verdicts.+ if [ "$N" -lt "$ORIG_N" ]; then+ SHARD_TIMEOUT=$((SHARD_TIMEOUT * ORIG_N / N))+ fi+ MEM_NOTE=" | mem-adapted ${ORIG_N}x${ORIG_INTRA}→${N}x${INTRA_CONC} (avail=${AVAIL_MB}MB, ${MEM_PER_FILE_MB}MB/file, timeout→${SHARD_TIMEOUT}s)"+ else+ MEM_NOTE=" | mem-ok (avail=${AVAIL_MB}MB)"+ fi+ fi+fi+# ──────────────────────────────────────────────────────────────────────────# Output directories. Prefer workspace-local .context/, fall back to /tmp.# ──────────────────────────────────────────────────────────────────────────@@ -100,7 +197,7 @@ elsemkdir -p "$LOG_DIR" || { echo "ERROR: cannot create log dir" >&2; exit 2; }fi# Clear from prior run.-rm -f "$LOG_DIR"/shard-*.log "$LOG_DIR"/shard-*.exit "$LOG_DIR"/shard-*.wedged 2>/dev/null+rm -f "$LOG_DIR"/shard-*.log "$LOG_DIR"/shard-*.exit "$LOG_DIR"/shard-*.wedged "$LOG_DIR"/shard-*.start "$LOG_DIR"/shard-*.end 2>/dev/null: > "$FAILURES_LOG": > "$SUMMARY_FILE"@@ -114,7 +211,7 @@ elif command -v timeout >/dev/null 2>&1; then TIMEOUT_BIN="timeout"fiSTART_TS=$(date +%s)-echo "[unit-parallel] N=$N shards | --max-concurrency=$INTRA_CONC | timeout=${SHARD_TIMEOUT}s | kill-after=${SHARD_KILL_AFTER}s | logs=$LOG_DIR" >&2+echo "[unit-parallel] N=$N shards | --max-concurrency=$INTRA_CONC | timeout=${SHARD_TIMEOUT}s | kill-after=${SHARD_KILL_AFTER}s | logs=$LOG_DIR${MEM_NOTE}" >&2if [ "$DRY_RUN" = "1" ]; thenecho "[unit-parallel] dry-run: would spawn $N shards with the above settings."@@ -133,6 +230,7 @@ SHARD_PIDS=()for i in $(seq 1 "$N"); do(SHARD_LOG="$LOG_DIR/shard-$i.log"+ date +%s > "$LOG_DIR/shard-$i.start"if [ -n "$TIMEOUT_BIN" ]; then"$TIMEOUT_BIN" --signal=TERM --kill-after="${SHARD_KILL_AFTER}s" "${SHARD_TIMEOUT}s" \env SHARD="$i/$N" \@@ -162,6 +260,7 @@ for i in $(seq 1 "$N"); dokill "$cap_pid" 2>/dev/nullwait "$cap_pid" 2>/dev/nullfi+ date +%s > "$LOG_DIR/shard-$i.end"echo "$rc" > "$LOG_DIR/shard-$i.exit"{ [ "$rc" = "124" ] || [ "$rc" = "137" ]; } && echo "WEDGED" > "$LOG_DIR/shard-$i.wedged") &@@ -316,6 +415,40 @@ TOTAL_FAILURES=0TOTAL_PASS=0TOTAL_SKIP=0TOTAL_RC=0++# Layer 2 state (serial OOM rescue). A shard whose log carries the WASM+# out-of-memory signature gets its failing files queued for a serial re-run;+# NON_OOM_FAIL records that at least one failure exists that the rescue lane+# must NOT absolve (plain assertion failures, wedges without the signature).+OOM_RE='Out of memory|WebAssembly\.Memory|RuntimeError: [Aa]borted|Aborted\(\)'+OOM_RESCUE_LIST="$LOG_DIR/oom-rescue-files.txt"+: > "$OOM_RESCUE_LIST"+NON_OOM_FAIL=0+# Set when any shard was killed externally — killed-midrun shards leave lock/+# state residue that can poison the LATER serial pass, so serial failures are+# only rescue-eligible under this flag (or their own OOM signature). A flaky+# serial test in an otherwise-clean run must stay red.+EXTERNAL_KILL_ANY=0++# failing_files_in_log: attribute each `(fail)` block to the test file whose+# `path.test.ts:` header most recently preceded it in bun's output. Under+# GITHUB_ACTIONS the shard wraps each file section as `::group::path.test.ts:`+# — strip that prefix or the rescue pass feeds bun literal `::group::...`+# non-paths that match zero test files (CI-only; local runs have no groups).+failing_files_in_log() {+ local file="$1"+ [ -f "$file" ] || return 0+ awk '+ /^(::group::)?[^ ].*\.test\.ts:$/ {+ current = $0+ sub(/^::group::/, "", current)+ current = substr(current, 1, length(current) - 1)+ next+ }+ /^\(fail\) / && current != "" { print current }+ ' "$file" | sort -u+}+for i in $(seq 1 "$N"); doSHARD_LOG="$LOG_DIR/shard-$i.log"EXIT_FILE="$LOG_DIR/shard-$i.exit"@@ -330,17 +463,74 @@ for i in $(seq 1 "$N"); doTOTAL_FAILURES=$((TOTAL_FAILURES + fail_count))TOTAL_SKIP=$((TOTAL_SKIP + skip_count))+ shard_oom=0+ if [ "$rc" != "0" ] && [ "${GBRAIN_TEST_NO_OOM_FALLBACK:-0}" != "1" ] \+ && [ -f "$SHARD_LOG" ] && grep -qE "$OOM_RE" "$SHARD_LOG"; then+ shard_oom=1+ fi++ # External-kill detection: rc 143 (SIGTERM) / 137 (SIGKILL) with the shard+ # dying before 80% of the shard timeout means something OUTSIDE the runner+ # killed it — sibling Conductor workspaces' process cleanup and macOS+ # memory jetsam both present exactly this way (observed: 3 shards TERM'd ++ # 1 KILL'd at ~700s under a 3000s cap, all mid-progress). A REAL wedge is+ # killed BY the runner at ~SHARD_TIMEOUT and stays red. Externally-killed+ # shards are phantoms: queue for the serial rescue lane like OOM.+ shard_external_kill=0+ if [ "$shard_oom" = "0" ] && [ "${GBRAIN_TEST_NO_OOM_FALLBACK:-0}" != "1" ] \+ && { [ "$rc" = "143" ] || [ "$rc" = "137" ]; }; then+ s_start=$(cat "$LOG_DIR/shard-$i.start" 2>/dev/null) || s_start=""+ s_end=$(cat "$LOG_DIR/shard-$i.end" 2>/dev/null) || s_end=""+ if [ -n "$s_start" ] && [ -n "$s_end" ]; then+ s_elapsed=$((s_end - s_start))+ if [ "$s_elapsed" -lt $((SHARD_TIMEOUT * 80 / 100)) ]; then+ shard_external_kill=1+ EXTERNAL_KILL_ANY=1+ fi+ fi+ fi+if [ -f "$WEDGED_FILE" ]; thenTOTAL_RC=1+ if [ "$shard_external_kill" = "1" ]; then+ SHARD="$i/$N" bash scripts/run-unit-shard.sh --dry-run-list 2>/dev/null >> "$OOM_RESCUE_LIST"+ echo "shard $i/$N: KILLED externally after ${s_elapsed}s (rc=$rc, well before ${SHARD_TIMEOUT}s cap — queued for serial rescue)" >> "$SUMMARY_FILE"+ elif [ "$shard_oom" = "1" ]; then+ # Wedged UNDER memory pressure: we can't attribute failures, so queue+ # the shard's entire file list for the serial rescue pass.+ SHARD="$i/$N" bash scripts/run-unit-shard.sh --dry-run-list 2>/dev/null >> "$OOM_RESCUE_LIST"+ echo "shard $i/$N: WEDGED after ${SHARD_TIMEOUT}s (rc=$rc, OOM signature — queued for serial rescue)" >> "$SUMMARY_FILE"+ else+ NON_OOM_FAIL=1+ echo "shard $i/$N: WEDGED after ${SHARD_TIMEOUT}s (rc=$rc)" >> "$SUMMARY_FILE"+ fi{echo "--- shard $i: WEDGED after ${SHARD_TIMEOUT}s ---"[ -f "$SHARD_LOG" ] && tail -50 "$SHARD_LOG"echo ""} >> "$FAILURES_LOG"- echo "shard $i/$N: WEDGED after ${SHARD_TIMEOUT}s (rc=$rc)" >> "$SUMMARY_FILE"continuefi+ if [ "$rc" != "0" ]; then+ if [ "$shard_oom" = "1" ]; then+ # One scan, reused for both the queue append and the emptiness check.+ shard_failing_files=$(failing_files_in_log "$SHARD_LOG")+ if [ -n "$shard_failing_files" ]; then+ printf '%s\n' "$shard_failing_files" >> "$OOM_RESCUE_LIST"+ else+ # OOM signature but no attributable files (e.g. bun died before any+ # file header) → rescue the whole shard.+ SHARD="$i/$N" bash scripts/run-unit-shard.sh --dry-run-list 2>/dev/null >> "$OOM_RESCUE_LIST"+ fi+ elif [ "$shard_external_kill" = "1" ]; then+ SHARD="$i/$N" bash scripts/run-unit-shard.sh --dry-run-list 2>/dev/null >> "$OOM_RESCUE_LIST"+ echo "shard $i/$N: KILLED externally after ${s_elapsed}s (rc=$rc — queued for serial rescue)" >> "$SUMMARY_FILE"+ else+ NON_OOM_FAIL=1+ fi+ fi+echo "shard $i/$N: pass=$pass_count fail=$fail_count skip=$skip_count rc=$rc" >> "$SUMMARY_FILE"if [ "$rc" != "0" ]; then@@ -395,6 +585,17 @@ if [ "$SERIAL_FILES_COUNT" -gt 0 ]; thencat "$LOG_DIR/serial.log"if [ "$SERIAL_RC" != "0" ]; thenTOTAL_RC=1+ if [ "${GBRAIN_TEST_NO_OOM_FALLBACK:-0}" != "1" ] \+ && { grep -qE "$OOM_RE" "$LOG_DIR/serial.log" || [ "$EXTERNAL_KILL_ANY" = "1" ]; }; then+ # Serial failures are rescue-eligible ONLY with their own OOM signature+ # or when an externally-killed shard ran earlier in this invocation+ # (killed-midrun shards leave lock/state residue that poisons the serial+ # pass). A merely-OOM'd sibling shard is NOT grounds — a flaky serial+ # test must stay red rather than get silently absolved.+ failing_files_in_log "$LOG_DIR/serial.log" >> "$OOM_RESCUE_LIST"+ else+ NON_OOM_FAIL=1+ fis_fail=$(bun_summary_count "fail" "$LOG_DIR/serial.log")TOTAL_FAILURES=$((TOTAL_FAILURES + s_fail))if [ "$s_fail" -gt 0 ]; then@@ -420,6 +621,92 @@ if [ "$SERIAL_FILES_COUNT" -gt 0 ]; thenfifi+# ──────────────────────────────────────────────────────────────────────────+# Layer 2: serial OOM rescue. Re-run every file that failed inside an+# OOM-signature shard, one at a time (1 shard, --max-concurrency 1), after+# the parallel fan-out has fully drained. Phantom failures (the WASM ran out+# of memory because 16 instances were up at once) pass here and the run goes+# green with an oom_rescued note; real failures fail again and stay red.+# ──────────────────────────────────────────────────────────────────────────+OOM_RESCUED=0+OOM_RESCUE_NOTE=""+sort -u "$OOM_RESCUE_LIST" -o "$OOM_RESCUE_LIST" 2>/dev/null+# grep -c exits 1 on zero matches — assign in two steps so an empty rescue+# list yields a single "0" (the grep_count double-output bug, same class).+RESCUE_COUNT=$(grep -c . "$OOM_RESCUE_LIST" 2>/dev/null) || RESCUE_COUNT=0+if [ "$TOTAL_RC" != "0" ] && [ "${RESCUE_COUNT:-0}" -gt 0 ]; then+ echo "════════════ OOM rescue pass ($RESCUE_COUNT files, serial) ════════════"+ echo "[unit-parallel] OOM signature detected — re-running $RESCUE_COUNT failing file(s) at --max-concurrency 1" >&2+ RESCUE_LOG="$LOG_DIR/oom-rescue.log"+ # 60s-per-file floor with the shard cap as a minimum, and 2x the shard cap+ # as a CEILING: a wedged shard queueing its whole file list must not turn+ # `bun run test` into an unbounded multi-hour serial re-run — hitting the+ # ceiling reads as a red rescue, not silence.+ RESCUE_TIMEOUT=$((RESCUE_COUNT * 60))+ [ "$RESCUE_TIMEOUT" -lt "$SHARD_TIMEOUT" ] && RESCUE_TIMEOUT="$SHARD_TIMEOUT"+ [ "$RESCUE_TIMEOUT" -gt $((SHARD_TIMEOUT * 2)) ] && RESCUE_TIMEOUT=$((SHARD_TIMEOUT * 2))+ # Split the queue: *.serial.test.ts files require one bun PROCESS per file+ # (run-serial-tests.sh's isolation contract — top-level mock.module leaks+ # across files in a shared registry); the remainder batches in one process.+ # Both lanes mirror the shard invocation's --timeout=60000 — bun's default+ # 5s per-test timeout would re-fail PGLite phantoms (120-migration replay)+ # and mislabel them 'confirmed real'.+ grep -v '\.serial\.test\.ts$' "$OOM_RESCUE_LIST" > "$LOG_DIR/oom-rescue-batch.txt" || true+ grep '\.serial\.test\.ts$' "$OOM_RESCUE_LIST" > "$LOG_DIR/oom-rescue-serial.txt" || true+ RESCUE_RC=0+ : > "$RESCUE_LOG"+ run_rescue() { # $1 = per-invocation timeout seconds; rest = test-file args+ local t="$1"; shift+ if [ -n "$TIMEOUT_BIN" ]; then+ "$TIMEOUT_BIN" --signal=TERM --kill-after="${SHARD_KILL_AFTER}s" "${t}s" \+ bun test --max-concurrency 1 --timeout=60000 "$@" >> "$RESCUE_LOG" 2>&1+ else+ bun test --max-concurrency 1 --timeout=60000 "$@" >> "$RESCUE_LOG" 2>&1+ fi+ }+ if [ -s "$LOG_DIR/oom-rescue-batch.txt" ]; then+ # shellcheck disable=SC2046+ run_rescue "$RESCUE_TIMEOUT" $(cat "$LOG_DIR/oom-rescue-batch.txt") || RESCUE_RC=1+ fi+ if [ -s "$LOG_DIR/oom-rescue-serial.txt" ]; then+ while IFS= read -r serial_file; do+ [ -n "$serial_file" ] || continue+ run_rescue 300 "$serial_file" || RESCUE_RC=1+ done < "$LOG_DIR/oom-rescue-serial.txt"+ fi+ cat "$RESCUE_LOG"+ r_pass=$(bun_summary_count "pass" "$RESCUE_LOG")+ r_fail=$(bun_summary_count "fail" "$RESCUE_LOG")+ if [ "$RESCUE_RC" = "0" ] && [ "$NON_OOM_FAIL" = "0" ]; then+ # Every failure in the run was OOM-phantom and every rescued file passed+ # serially: the run is green. Adjust the headline numbers so they reflect+ # the rescue verdict, and mark the earlier failure blocks superseded.+ TOTAL_RC=0+ OOM_RESCUED=1+ # Do NOT fold r_pass into TOTAL_PASS — the failing shard's own summary+ # already counted the rescued files' passing tests, so folding would+ # double-count. Rescue results ride in the note instead.+ TOTAL_FAILURES=0+ OOM_RESCUE_NOTE=" | oom_rescued=${RESCUE_COUNT}files(${r_pass}p serial)"+ {+ echo "--- OOM rescue: all $RESCUE_COUNT file(s) passed serially (${r_pass} tests) ---"+ echo "--- failure blocks above were WASM out-of-memory phantoms, superseded ---"+ } >> "$FAILURES_LOG"+ echo "oom-rescue: $RESCUE_COUNT files pass=$r_pass rc=0 (phantom OOM failures superseded)" >> "$SUMMARY_FILE"+ else+ # Real failures confirmed serially (or a non-OOM failure exists anyway).+ OOM_RESCUE_NOTE=" | oom_rescue_failed=${r_fail}real"+ awk '+ /^\(fail\) / { in_block=1; print "--- oom-rescue (serial, confirmed real): " $0; next }+ in_block {+ if (/^\(pass\)/ || /^\(skip\)/ || /^[[:space:]]*$/ || /__bun_test_summary__/) { in_block=0; print ""; next }+ print $0+ }+ ' "$RESCUE_LOG" >> "$FAILURES_LOG"+ echo "oom-rescue: $RESCUE_COUNT files pass=$r_pass fail=$r_fail rc=$RESCUE_RC (real failures confirmed)" >> "$SUMMARY_FILE"+ fi+fi+END_TS=$(date +%s)ELAPSED=$((END_TS - START_TS))@@ -436,10 +723,10 @@ if [ "$TOTAL_RC" != "0" ]; thenecho "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"tail -30 "$FAILURES_LOG"echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"- echo "[unit-parallel] elapsed=${ELAPSED}s | pass=$TOTAL_PASS fail=$TOTAL_FAILURES skip=$TOTAL_SKIP"+ echo "[unit-parallel] elapsed=${ELAPSED}s | pass=$TOTAL_PASS fail=$TOTAL_FAILURES skip=$TOTAL_SKIP${OOM_RESCUE_NOTE}"} >&2exit 1fi-echo "[unit-parallel] elapsed=${ELAPSED}s | pass=$TOTAL_PASS fail=$TOTAL_FAILURES skip=$TOTAL_SKIP" >&2+echo "[unit-parallel] elapsed=${ELAPSED}s | pass=$TOTAL_PASS fail=$TOTAL_FAILURES skip=$TOTAL_SKIP${OOM_RESCUE_NOTE}" >&2exit 0diff --git a/src/cli.ts b/src/cli.tsindex ec60567c95..6bcffdbf4f 100755--- a/src/cli.ts+++ b/src/cli.ts@@ -33,6 +33,7 @@ import { parseGlobalFlags, setCliOptions, getCliOptions } from './core/cli-optioimport type { CliOptions } from './core/cli-options.ts';import { callRemoteTool, RemoteMcpError, unpackToolResult } from './core/mcp-client.ts';import { maybePromptForUpgrade } from './core/thin-client-upgrade-prompt.ts';+import { CLI_FLAG_REGISTRY } from './core/cli-flag-registry.generated.ts';import { VERSION } from './version.ts';// Build CLI name -> operation lookup@@ -54,8 +55,19 @@ export function bigintToStringReplacer(_key: string, value: unknown): unknown {return typeof value === 'bigint' ? value.toString() : value;}+// ENG-2 renderer parity: round-trip a local-engine op's return value so+// renderers see the same shape the routed path produces. Bigint-safe via+// bigintToStringReplacer. Exported for tests (same import-safety contract as+// cliAliases/formatResult). (#2450)+export function normalizeLocalResult(rawResult: unknown): unknown {+ return JSON.parse(JSON.stringify(rawResult, bigintToStringReplacer));+}+// CLI-only commands that bypass the operation layer-export const CLI_ONLY = new Set(['init', 'reinit-pglite', 'pglite-repair', 'upgrade', 'post-upgrade', 'check-update', 'integrations', 'publish', 'check-backlinks', 'lint', 'report', 'import', 'export', 'files', 'embed', 'serve', 'call', 'config', 'doctor', 'migrate', 'eval', 'sync', 'extract', 'extract-conversation-facts', 'enrich', 'features', 'autopilot', 'graph-query', 'jobs', 'agent', 'apply-migrations', 'skillpack-check', 'skillpack', 'resolvers', 'integrity', 'repair-jsonb', 'orphans', 'maintain', 'sources', 'mounts', 'dream', 'check-resolvable', 'routing-eval', 'skillify', 'smoke-test', 'providers', 'storage', 'repos', 'code-def', 'code-refs', 'reindex', 'reindex-code', 'reindex-frontmatter', 'code-callers', 'code-callees', 'reconcile-links', 'frontmatter', 'auth', 'friction', 'claw-test', 'book-mirror', 'takes', 'think', 'salience', 'anomalies', 'calibration', 'transcripts', 'models', 'remote', 'recall', 'forget', 'edges-backfill', 'cache', 'ze-switch', 'retrieval-upgrade', 'founder', 'brainstorm', 'lsd', 'schema', 'capture', 'onboard', 'conversation-parser', 'status', 'connect', 'skillopt', 'quarantine', 'self-upgrade', 'advisor', 'watch', 'reindex-search-vector', 'pages', 'bench', 'backfill']);+export const CLI_ONLY = new Set(['init', 'reinit-pglite', 'pglite-repair', 'upgrade', 'post-upgrade', 'check-update', 'integrations', 'publish', 'check-backlinks', 'lint', 'report', 'import', 'export', 'files', 'embed', 'serve', 'call', 'config', 'doctor', 'migrate', 'eval', 'sync', 'extract', 'extract-conversation-facts', 'enrich', 'features', 'autopilot', 'graph-query', 'jobs', 'agent', 'apply-migrations', 'skillpack-check', 'skillpack', 'resolvers', 'integrity', 'repair-jsonb', 'orphans', 'maintain', 'sources', 'mounts', 'dream', 'check-resolvable', 'routing-eval', 'skillify', 'smoke-test', 'providers', 'storage', 'repos', 'code-def', 'code-refs', 'reindex', 'reindex-code', 'reindex-frontmatter', 'code-callers', 'code-callees', 'reconcile-links', 'frontmatter', 'auth', 'friction', 'claw-test', 'book-mirror', 'takes', 'think', 'salience', 'anomalies', 'calibration', 'transcripts', 'models', 'remote', 'recall', 'forget', 'edges-backfill', 'cache', 'ze-switch', 'retrieval-upgrade', 'founder', 'brainstorm', 'lsd', 'schema', 'capture', 'onboard', 'conversation-parser', 'status', 'connect', 'skillopt', 'quarantine', 'self-upgrade', 'advisor', 'watch', 'reindex-search-vector', 'pages', 'bench', 'backfill',+ // v0.42.58 (#2035 class, caught by the handleCliOnly reachability sweep):+ // full handler at `case 'notability-eval'` but never dispatchable.+ 'notability-eval']);// CLI-only commands whose handlers print their own --help text. These are// excluded from the generic short-circuit so detailed per-command and// per-subcommand usage stays reachable.@@ -328,6 +340,30 @@ async function main() {}}+ // #2185: strict unknown-flag validation — pre-dispatch, pre-engine. A flag+ // no handler consults (the repro: `init --migrate-only --dry-run` applying+ // REAL migrations while the user asked for a rehearsal) fails loud here+ // instead of silently doing the destructive thing. Runs after the --help+ // short-circuit so `gbrain x --help` never errors; runs before any dispatch+ // or engine connect so the error is instant and side-effect-free.+ {+ const unknown = validateCommandFlags(command, subArgs);+ if (unknown) {+ // Message contract shared with init.ts's in-handler check (which this+ // pre-dispatch validator now reaches first): lowercase 'unknown flag'+ // on stderr; --json callers get the structured error on stdout with+ // reason 'invalid_flag' (pinned by test/init-migrate-only.test.ts).+ const message = `unknown flag ${unknown} for 'gbrain ${command}'`;+ // Both --json spellings get the structured envelope (--json=false opts out).+ if (subArgs.some(a => a === '--json' || (a.startsWith('--json=') && a !== '--json=false'))) {+ process.stdout.write(JSON.stringify({ status: 'error', reason: 'invalid_flag', message }) + '\n');+ }+ console.error(`gbrain ${command}: ${message}`);+ console.error(`Run: gbrain ${command} --help`);+ process.exit(1);+ }+ }+// DB-free durability pull (v0.42.44 D2): the harden cron calls// `gbrain sources pull --path <dir>` every ~30 min. It must NOT open PGLite// (a live long-lived session holds the single-writer lock), so handle it@@ -512,8 +548,8 @@ async function main() {// path's return value so renderers see the same shape they'd see on the// routed path. Date → ISO string; bigint → string (postgres.js shape);// Buffer → object. Microsecond-cost; eliminates a whole drift bug class.- const result = JSON.parse(JSON.stringify(rawResult, bigintToStringReplacer));- const output = formatResult(op.name, result);+ const result = normalizeLocalResult(rawResult);+ const output = formatResult(op.name, result, params);if (output) process.stdout.write(output);} catch (e: unknown) {// v0.42.20.0 (codex D4): on error, set exitCode + return so the `finally`@@ -596,7 +632,7 @@ async function runThinClientRouted(signal: sigintController.signal,});const result = unpackToolResult(raw);- const output = formatResult(op.name, result);+ const output = formatResult(op.name, result, params);if (output) process.stdout.write(output);} catch (e: unknown) {if (e instanceof RemoteMcpError) {@@ -814,6 +850,29 @@ export function parseOpArgs(op: Operation, args: string[]): Record<string, unknofor (let i = 0; i < args.length; i++) {const arg = args[i];if (arg.startsWith('--')) {+ // #2185: `--key=value` inline form. Pre-fix this parsed as junk key+ // 'key=value' and consumed the NEXT token as its value, corrupting+ // positional parsing. Recognized here so the strict-flag validator and+ // the parser agree on the idiom.+ const eq = arg.indexOf('=');+ if (eq > 2) {+ const key = arg.slice(2, eq).replace(/-/g, '_');+ // CLI-local booleans: `--json=<v>` / `--dry-run=<v>` must parse as+ // booleans, not fall through to the junk-key path (which would+ // consume the NEXT token as a value and corrupt positional parsing).+ if (key === 'json' || key === 'dry_run') {+ params[key] = arg.slice(eq + 1) !== 'false';+ continue;+ }+ const def = op.params[key];+ if (def) {+ const raw = arg.slice(eq + 1);+ params[key] = def.type === 'boolean' ? raw !== 'false'+ : def.type === 'number' ? Number(raw)+ : raw;+ continue;+ }+ }if (arg.startsWith('--no-')) {const positiveKey = arg.slice(5).replace(/-/g, '_');const positiveDef = op.params[positiveKey];@@ -826,6 +885,14 @@ export function parseOpArgs(op: Operation, args: string[]): Record<string, unknoconst paramDef = op.params[key];if (paramDef?.type === 'boolean') {params[key] = true;+ } else if (key === 'json' || key === 'dry_run') {+ // CLI-local booleans, intentionally NOT on the operation contract+ // exposed over MCP/tools: json is the formatter flag; dry_run feeds+ // makeContext's ctx.dryRun. Both must never consume a value token —+ // pre-fix, `gbrain delete x --dry-run` (trailing) set NOTHING, so+ // ctx.dryRun stayed false and the REAL delete ran despite the+ // rehearsal request (the resurrected #2185 class the red team caught).+ params[key] = true;} else if (i + 1 < args.length) {params[key] = args[++i];if (paramDef?.type === 'number') params[key] = Number(params[key]);@@ -987,6 +1054,112 @@ export function applyThinClientSourceScope(}// Exported for tests (same import-safety contract as applyThinClientSourceScope).+// ─────────────────────────────────────────────────────────────────+// #2185 — strict unknown-flag validation (pre-dispatch, pre-engine).+// A flag no handler consults must fail loud instead of silently doing the+// destructive thing (`init --migrate-only --dry-run` applied REAL migrations+// while the user asked for a rehearsal). Two lanes:+// - op commands: legal flags derive from the operation contract+// (op.params) + the CLI-local formatter flags, mirroring parseOpArgs's+// traversal so values that begin with '--' are never misread.+// - CLI_ONLY commands: legal flags come from the generated+// CLI_FLAG_REGISTRY (scripts/generate-flag-registry.ts scans each+// command's source; freshness + coverage pinned by+// test/cli-flag-validation.test.ts).+// Everything after a literal `--` is passthrough and never validated.+// ─────────────────────────────────────────────────────────────────++// Exempt by contract, not oversight:+// - call: the generic op invoker — arbitrary --param names are its interface.+// - config: `config set <key> <value>` values are arbitrary strings.+// - jobs submit: job payloads carry handler-defined params (shell lane incl.).+function flagValidationExempt(command: string, subArgs: string[]): boolean {+ return command === 'call' || command === 'config'+ || (command === 'jobs' && subArgs[0] === 'submit');+}++/** Returns the first unknown flag (e.g. '--dry-run') or null when clean. */+export function validateCommandFlags(command: string, subArgs: string[]): string | null {+ if (flagValidationExempt(command, subArgs)) return null;+ // Lane order MUST mirror dispatch order (CLI_ONLY first): commands that are+ // BOTH an op and a CLI_ONLY member (think, salience, anomalies) dispatch to+ // handleCliOnly, whose handlers parse flags the op contract doesn't declare+ // (`salience --kind`, `think --with-calibration`) — validating those+ // against op.params rejected documented invocations.+ if (CLI_ONLY.has(command)) {+ const legal = CLI_FLAG_REGISTRY[command];+ // Registry drift fails OPEN at runtime (never brick a command); the+ // drift-guard test fails the build instead.+ if (!legal) return null;+ return findUnknownFlag(subArgs, new Set(legal));+ }+ const op = cliOps.get(command) ?? cliAliases.get(command);+ if (op) return findUnknownOpFlag(op, subArgs);+ return null; // unknown command — the dispatcher's own error handles it+}++/** CLI_ONLY lane: token scan against the generated legal set. */+export function findUnknownFlag(args: string[], legal: ReadonlySet<string>): string | null {+ for (const a of args) {+ if (a === '--') break;+ const m = /^--([a-z0-9][a-z0-9-]*)(?:=.*)?$/i.exec(a);+ if (!m) continue;+ // Casing typo = unknown flag: every handler in the repo is+ // case-sensitive-lowercase, so `--MIGRATE-ONLY` passing validation would+ // just be silently ignored downstream — the exact class this validator+ // exists to kill.+ if (/[A-Z]/.test(m[1])) return `--${m[1]}`;+ const name = `--${m[1]}`;+ if (legal.has(name)) continue;+ // --no-<flag> negation of a known flag is legal.+ if (name.startsWith('--no-') && legal.has(`--${name.slice(5)}`)) continue;+ return name;+ }+ return null;+}++/** Op lane: mirrors parseOpArgs so flag VALUES starting with '--' are skipped. */+export function findUnknownOpFlag(op: Operation, args: string[]): string | null {+ for (let i = 0; i < args.length; i++) {+ const a = args[i];+ if (a === '--') break;+ const m = /^--([a-z0-9][a-z0-9-]*)(?:=(.*))?$/i.exec(a);+ if (!m) continue;+ // Casing typo = unknown flag (see findUnknownFlag).+ if (/[A-Z]/.test(m[1])) return `--${m[1]}`;+ const rawKey = m[1];+ // CLI-local flags consumed OUTSIDE the op contract (never wire params):+ // json/explain — formatter flags; help — short-circuits pre-dispatch;+ // source — makeContext's 6-tier source resolution (deleted before wire);+ // dry-run — makeContext's ctx.dryRun projection.+ // Pre-fix, rejecting these broke documented invocations+ // (`gbrain search "x" --source y`, `gbrain put x --dry-run`).+ if (rawKey === 'json') continue;+ if ((rawKey === 'explain' || rawKey === 'help') && m[2] === undefined) continue;+ if (rawKey === 'source' || rawKey === 'dry-run') {+ // Non-boolean-style CLI-locals consume the next token as their value+ // in parseOpArgs (source does; dry-run is boolean-read) — mirror the+ // parser: source consumes a value when not inline-`=`.+ if (rawKey === 'source' && m[2] === undefined) i++;+ continue;+ }+ if (rawKey.startsWith('no-')) {+ const positive = rawKey.slice(3).replace(/-/g, '_');+ if (op.params[positive]?.type === 'boolean') continue;+ }+ const key = rawKey.replace(/-/g, '_');+ const paramDef = op.params[key];+ if (paramDef) {+ // Non-boolean flags consume the next token as their value unless+ // provided inline via `=` — exactly like parseOpArgs.+ if (paramDef.type !== 'boolean' && m[2] === undefined) i++;+ continue;+ }+ return `--${rawKey}`;+ }+ return null;+}+export async function makeContext(engine: BrainEngine, params: Record<string, unknown>): Promise<OperationContext> {// v0.31.8 (D11): resolve sourceId via the canonical 6-tier chain. Honors// --source / GBRAIN_SOURCE / .gbrain-source / path-match / brain default /@@ -1041,7 +1214,11 @@ export async function makeContext(engine: BrainEngine, params: Record<string, un}// Exported for tests (same import-safety contract as cliAliases/printOpHelp).-export function formatResult(opName: string, result: unknown): string {+export function formatResult(+ opName: string,+ result: unknown,+ params: Record<string, unknown> = {},+): string {switch (opName) {case 'volunteer_context': {const r = result as any;@@ -1080,6 +1257,7 @@ export function formatResult(opName: string, result: unknown): string {case 'search':case 'query': {const results = result as any[];+ if (params.json === true) return JSON.stringify(results, null, 2) + '\n';if (results.length === 0) return 'No results.\n';// v0.40.4 — --explain switches to per-stage attribution formatter.// Reads CliOptions.explain via the module-level singleton.@@ -1166,7 +1344,9 @@ export function formatResult(opName: string, result: unknown): string {).join('\n') + '\n';}default:- return JSON.stringify(result, null, 2) + '\n';+ // bigintToStringReplacer keeps this fallback renderer crash-proof even+ // if a future caller hands it a not-yet-normalized result. (#2450)+ return JSON.stringify(result, bigintToStringReplacer, 2) + '\n';}}diff --git a/src/commands/doctor.ts b/src/commands/doctor.tsindex 4cfe2ad439..d0d994c338 100644--- a/src/commands/doctor.ts+++ b/src/commands/doctor.ts@@ -376,19 +376,38 @@ export async function jsonbIntegrityCheck(progress?: Pick<ProgressReporter, 'heartbeat'>,): Promise<Check> {try {- const targets: Array<{ table: string; col: string; expected: 'object' | 'array' }> = [- { table: 'pages', col: 'frontmatter', expected: 'object' },- { table: 'raw_data', col: 'data', expected: 'object' },- { table: 'ingest_log', col: 'pages_updated', expected: 'array' },- { table: 'files', col: 'metadata', expected: 'object' },- { table: 'page_versions', col: 'frontmatter', expected: 'object' },+ const targets: Array<{ table: string; col: string; expected: 'object' | 'array'; jsonPayloadOnly?: boolean }> = [+ { table: 'pages', col: 'frontmatter', expected: 'object' },+ { table: 'raw_data', col: 'data', expected: 'object' },+ { table: 'ingest_log', col: 'pages_updated', expected: 'array' },+ { table: 'files', col: 'metadata', expected: 'object' },+ { table: 'page_versions', col: 'frontmatter', expected: 'object' },+ // Subagent persistence — second double-encode site (historical damage+ // rows from the pre-v0.42.53.0 positional bind; write paths fixed in+ // #2375). Mirrors repair-jsonb's targets incl. jsonPayloadOnly: these+ // columns can legitimately hold jsonb STRING scalars (persistToolExec+ // binds pre-serialized string payloads as-is), so only JSON-container+ // content counts as damage.+ { table: 'subagent_messages', col: 'content_blocks', expected: 'array', jsonPayloadOnly: true },+ { table: 'subagent_tool_executions', col: 'input', expected: 'object', jsonPayloadOnly: true },+ { table: 'subagent_tool_executions', col: 'output', expected: 'object', jsonPayloadOnly: true },];let totalBad = 0;const breakdown: string[] = [];- for (const { table, col } of targets) {+ for (const { table, col, jsonPayloadOnly } of targets) {progress?.heartbeat(`jsonb_integrity.${table}.${col}`);+ // Skip targets whose table doesn't exist on this brain (subagent_*+ // tables are v0.15+; pre-v0.15 brains naturally lack them).+ const existsRows = await engine.executeRaw<{ exists: boolean }>(+ `SELECT to_regclass($1) IS NOT NULL AS exists`,+ [table],+ );+ if (!existsRows[0]?.exists) continue;+ const damage = jsonPayloadOnly+ ? `jsonb_typeof(${col}) = 'string' AND (${col} #>> '{}') ~ '^[[:space:]]*[\\[{]' AND pg_input_is_valid(${col} #>> '{}', 'jsonb')`+ : `jsonb_typeof(${col}) = 'string'`;const rows = await engine.executeRaw<{ n: number }>(- `SELECT count(*)::int AS n FROM ${table} WHERE jsonb_typeof(${col}) = 'string'`,+ `SELECT count(*)::int AS n FROM ${table} WHERE ${damage}`,);const n = Number(rows[0]?.n ?? 0);if (n > 0) { totalBad += n; breakdown.push(`${table}.${col}=${n}`); }diff --git a/src/commands/repair-jsonb.ts b/src/commands/repair-jsonb.tsindex a9d17652f6..d343c869e9 100644--- a/src/commands/repair-jsonb.ts+++ b/src/commands/repair-jsonb.ts@@ -16,16 +16,27 @@* (it never wrote string-typed JSONB).** Affected columns (audit of src/schema.sql):- * - pages.frontmatter (postgres-engine.ts:107 putPage)- * - raw_data.data (postgres-engine.ts:668 putRawData)- * - ingest_log.pages_updated (postgres-engine.ts:846 logIngest)- * - files.metadata (commands/files.ts:254 file upload)- * - page_versions.frontmatter (downstream of pages.frontmatter via- * INSERT...SELECT FROM pages)+ * - pages.frontmatter (postgres-engine.ts:107 putPage)+ * - raw_data.data (postgres-engine.ts:668 putRawData)+ * - ingest_log.pages_updated (postgres-engine.ts:846 logIngest)+ * - files.metadata (commands/files.ts:254 file upload)+ * - page_versions.frontmatter (downstream of pages.frontmatter via+ * INSERT...SELECT FROM pages)+ * - subagent_messages.content_blocks (subagent.ts:599 persistMessage —+ * v0.16.0+, write path fixed in+ * v0.42.53.0 #2375)+ * - subagent_tool_executions.input (subagent.ts:625/660 persistToolExec+ * Pending/Failed — same wave)+ * - subagent_tool_executions.output (subagent.ts:639 persistToolExecComplete+ * — same wave)*- * Other JSONB columns (minion_jobs.{data,result,progress,stacktrace},- * minion_inbox.payload) were always written via parameterized form ($N::jsonb- * with a string parameter, not interpolation) so they were never affected.+ * The subagent_* writes were broken via a slightly different shape than the+ * v0.12.0 wave: they used `engine.executeRaw` (postgres.js `unsafe`) with+ * `JSON.stringify(value)` + `$N::jsonb` cast. postgres.js's unsafe path+ * binds the resulting string as text, then the `::jsonb` cast wraps it as+ * a jsonb string scalar instead of parsing it. queue.ts and other+ * `executeRaw` callers were not affected because they pass raw objects+ * (postgres.js v3 auto-encodes objects to jsonb).*/import { loadConfig, toEngineConfig } from '../core/config.ts';@@ -39,16 +50,40 @@ interface RepairTarget {column: string;/** Optional secondary key column for logging. */keyCol?: string;+ /**+ * Only unwrap string scalars whose CONTENT is a JSON container ({...} or+ * [...]). The subagent columns can legitimately hold jsonb string scalars+ * (persistToolExec binds `typeof input === 'string' ? input : stringify`,+ * so a tool's pre-serialized plain-text payload lands as a JSON string) —+ * an unconditional unwrap would cast non-JSON text and abort the entire+ * repair run, or corrupt a legitimate value on a second pass.+ */+ jsonPayloadOnly?: boolean;}const TARGETS: RepairTarget[] = [- { table: 'pages', column: 'frontmatter', keyCol: 'slug' },- { table: 'raw_data', column: 'data', keyCol: 'source' },- { table: 'ingest_log', column: 'pages_updated', keyCol: 'source_ref' },- { table: 'files', column: 'metadata', keyCol: 'storage_path' },- { table: 'page_versions', column: 'frontmatter', keyCol: 'snapshot_at' },+ { table: 'pages', column: 'frontmatter', keyCol: 'slug' },+ { table: 'raw_data', column: 'data', keyCol: 'source' },+ { table: 'ingest_log', column: 'pages_updated', keyCol: 'source_ref' },+ { table: 'files', column: 'metadata', keyCol: 'storage_path' },+ { table: 'page_versions', column: 'frontmatter', keyCol: 'snapshot_at' },+ { table: 'subagent_messages', column: 'content_blocks', keyCol: 'job_id', jsonPayloadOnly: true },+ { table: 'subagent_tool_executions', column: 'input', keyCol: 'tool_use_id', jsonPayloadOnly: true },+ { table: 'subagent_tool_executions', column: 'output', keyCol: 'tool_use_id', jsonPayloadOnly: true },];+/** The double-encode predicate for a target (see jsonPayloadOnly). */+function damagePredicate(t: RepairTarget): string {+ const base = `jsonb_typeof(${t.column}) = 'string'`;+ // Container-looking is not enough: '[INFO] fetch complete' matches the+ // shape probe but is NOT valid JSON — the repair cast would throw and+ // abort the run. pg_input_is_valid (PG16+, same floor as the IS JSON+ // predicate updateSourceConfig already relies on) gates on parseability.+ return t.jsonPayloadOnly+ ? `${base} AND (${t.column} #>> '{}') ~ '^[[:space:]]*[\\[{]' AND pg_input_is_valid(${t.column} #>> '{}', 'jsonb')`+ : base;+}+export interface RepairResult {engine: string;per_target: Array<{@@ -113,20 +148,40 @@ export async function repairJsonb(opts: RepairOpts = { dryRun: false }): Promiselet repaired = 0;try {+ // Skip targets whose table doesn't exist yet — relevant for the+ // v0_12_2 migration on pre-v0.15 brains (subagent_* tables hadn't+ // been added yet) and any future schema additions to TARGETS.+ const existsRows = await sql.unsafe(+ `SELECT to_regclass($1) IS NOT NULL AS exists`,+ [t.table],+ ) as Array<{ exists: boolean }>;+ if (!existsRows[0]?.exists) {+ progress.tick(1, `${t.table}.${t.column}=skipped(no-table)`);+ result.per_target.push({ table: t.table, column: t.column, rows_repaired: 0 });+ continue;+ }+if (opts.dryRun) {const rows = await sql.unsafe(- `SELECT count(*)::int AS n FROM ${t.table} WHERE jsonb_typeof(${t.column}) = 'string'`,+ `SELECT count(*)::int AS n FROM ${t.table} WHERE ${damagePredicate(t)}`,);repaired = (rows[0] as unknown as { n: number }).n;} else {const rows = await sql.unsafe(`UPDATE ${t.table}SET ${t.column} = (${t.column} #>> '{}')::jsonb- WHERE jsonb_typeof(${t.column}) = 'string'+ WHERE ${damagePredicate(t)}RETURNING 1`,);repaired = rows.length;}+ } catch (e) {+ // One target's failure (unexpected content shape, permission, etc.)+ // must not abort the remaining targets — earlier repairs are already+ // committed and the v0_12_2 migration orchestrator JSON-parses our+ // stdout. Record, report, continue.+ console.error(`[repair-jsonb] ${t.table}.${t.column} failed: ${(e as Error).message} — continuing with remaining targets`);+ repaired = 0;} finally {stopHb();}diff --git a/src/commands/serve-http.ts b/src/commands/serve-http.tsindex cb4bad1c37..7d79aa1652 100644--- a/src/commands/serve-http.ts+++ b/src/commands/serve-http.ts@@ -30,6 +30,7 @@ import type { OperationContext, AuthInfo } from '../core/operations.ts';import { GBrainOAuthProvider, validateTokenEndpointAuthMethod } from '../core/oauth-provider.ts';import type { SqlQuery } from '../core/oauth-provider.ts';import { hasScope, ALLOWED_SCOPES_LIST, normalizeScopesInput } from '../core/scope.ts';+import { normalizeSourceInput, normalizeFederatedReadInput } from '../core/source-id.ts';import { summarizeMcpParams, dispatchToolCall } from '../mcp/dispatch.ts';import { paramDefToSchema } from '../mcp/tool-defs.ts';import { getBrainHotMemoryMeta } from '../core/facts/meta-hook.ts';@@ -1652,7 +1653,7 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOption// and other malformed inputs// normalizeScopesInput handles all four valid shapes (string, string[],// missing, empty) and rejects the rest with a structured 400.- const { name, tokenTtl, grantTypes, redirectUris, tokenEndpointAuthMethod } = req.body;+ const { name, source, federatedRead, tokenTtl, grantTypes, redirectUris, tokenEndpointAuthMethod } = req.body;const rawScopes = (req.body as Record<string, unknown>).scopes ?? (req.body as Record<string, unknown>).scope;if (!name) { res.status(400).json({ error: 'Name required' }); return; }let scopeString: string;@@ -1684,8 +1685,27 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOption});return;}+ // v0.41.x: honor optional `source` (write source_id) and `federatedRead`+ // (read source set) from the request body, mirroring the CLI's+ // `--source` / `--federated-read` flags. Omitting both preserves the+ // historical behavior (source_id='default', federated_read=[source_id]).+ // Pre-fix this endpoint hardcoded 'default'/undefined, so an admin SPA or+ // a proxy could never mint a client bound to a non-default brain source+ // over HTTP — only the CLI could. Validated here for a structured 400.+ let sourceId: string;+ let federatedReadIds: string[] | undefined;+ try {+ sourceId = normalizeSourceInput(source);+ federatedReadIds = normalizeFederatedReadInput(federatedRead);+ } catch (e) {+ res.status(400).json({+ error: 'invalid_source',+ message: e instanceof Error ? e.message : String(e),+ });+ return;+ }const result = await oauthProvider.registerClientManual(- name, grants, scopeString, uris, 'default', undefined, validatedAuthMethod,+ name, grants, scopeString, uris, sourceId, federatedReadIds, validatedAuthMethod,);// Set per-client TTL if specifiedif (tokenTtl && Number(tokenTtl) > 0) {diff --git a/src/core/cli-flag-registry.generated.ts b/src/core/cli-flag-registry.generated.tsnew file mode 100644index 0000000000..9a6f7c3bb4--- /dev/null+++ b/src/core/cli-flag-registry.generated.ts@@ -0,0 +1,105 @@+// AUTO-GENERATED by scripts/generate-flag-registry.ts — do not edit by hand.+// Regenerate: bun run build:flag-registry+// Freshness + drift pinned by test/cli-flag-validation.test.ts (#2185).+//+// Per-command legal flags for CLI_ONLY commands, derived from each command's+// source (case block + imported modules + one level of relative imports ++// scripts/generate-flag-registry.ts EXTRA_FLAGS). Deliberately over-inclusive+// (help-text mentions count): accepting an ignored flag is the pre-#2185+// status quo; missing a real one breaks working invocations.+export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {+ 'advisor': ['--aliases', '--all', '--apply', '--brain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--skills-dir', '--source', '--stale', '--supersessions', '--thin', '--verbose', '--workspace', '--yes'],+ 'agent': ['--aliases', '--all', '--brain', '--detach', '--fanout-manifest', '--flag', '--flags', '--follow', '--help', '--include-null-signature', '--json', '--max-turns', '--model', '--no-extract', '--no-follow', '--note', '--pattern', '--pending', '--reset', '--resolve', '--since', '--source', '--stale', '--subagent-def', '--supersessions', '--thin', '--timeout-ms', '--tools', '--word'],+ 'anomalies': ['--aliases', '--all', '--brain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--lookback-days', '--mcp-only', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--sigma', '--since', '--source', '--stale', '--supersessions', '--thin', '--timeout'],+ 'apply-migrations': ['--ab', '--all', '--auto-update', '--brain', '--break-lock', '--build-index', '--by-mention', '--compile', '--days', '--dry-run', '--exclusive', '--fast', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--from-meetings', '--from-pages', '--help', '--history', '--host-dir', '--http', '--json', '--lang', '--list', '--locks', '--markdown', '--max-age', '--migrate-only', '--migration', '--mode', '--multimodal', '--no-autopilot-install', '--no-embedding', '--no-extract', '--non-interactive', '--phase', '--priority', '--refresh-unqualified', '--remediate', '--rollback', '--skip-verify', '--source', '--stale', '--undo-wave', '--use-captured-snapshot', '--with-calibration', '--yes'],+ 'auth': ['--aliases', '--all', '--bound-brain', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--enable-dcr', '--enable-dcr-insecure', '--fast', '--federated-read', '--force', '--from-pages', '--grant-types', '--help', '--http', '--include-null-signature', '--json', '--no-embedding', '--no-extract', '--pattern', '--pending', '--redirect-uri', '--reset', '--resolve', '--scopes', '--source', '--stale', '--supersessions', '--takes-holders', '--thin', '--token', '--token-endpoint-auth-method', '--yes'],+ 'autopilot': ['--aliases', '--all', '--auto-fix', '--batch', '--brain', '--break-lock', '--by-type', '--check', '--dimensions', '--dir', '--drain', '--embedding-dimensions', '--embedding-model', '--explain', '--fast', '--ff-only', '--fix', '--force', '--force-break-lock', '--force-retry', '--from-pages', '--full', '--help', '--http', '--include-null-signature', '--inject-bootstrap', '--inline', '--input', '--install', '--interval', '--json', '--markdown', '--max-age', '--max-rss', '--max-usd', '--migrate-only', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--no-extract', '--no-inject', '--no-mutate', '--no-worker', '--non-interactive', '--now', '--once', '--output', '--path', '--pattern', '--pending', '--phase', '--refresh-cache', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--source', '--stale', '--status', '--supersessions', '--swap-only', '--target', '--target-score', '--thin', '--timeout', '--to', '--uninstall', '--unsafe-bypass-dream-guard', '--user', '--version', '--yes'],+ 'backfill': ['--aliases', '--all', '--batch-size', '--brain', '--concurrency', '--dry-run', '--fresh', '--help', '--include-null-signature', '--json', '--keep-index', '--list', '--max-errors', '--max-rows', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--resume', '--source', '--stale', '--supersessions', '--thin'],+ 'bench': ['--baseline', '--brain', '--explain', '--force', '--from', '--help', '--json', '--label', '--lang', '--limit', '--markdown', '--multimodal', '--near-symbol', '--restore-only', '--source', '--stale', '--symbol-kind', '--thin', '--threshold-jaccard', '--threshold-latency-multiplier', '--threshold-top1', '--to', '--tool'],+ 'book-mirror': ['--aliases', '--all', '--allow-empty', '--apply', '--asof', '--author', '--auto', '--background', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--brain-wide-max-cost-usd', '--budget-usd-per-day', '--by-mention', '--chapters-dir', '--content', '--context-file', '--date', '--days', '--dry-run', '--entities', '--explain', '--fast', '--federated', '--file', '--follow', '--force', '--from-pages', '--help', '--http', '--image', '--include-null-signature', '--json', '--kind', '--limit', '--max-turns', '--max-usd', '--mode', '--model', '--multimodal', '--no-confirm', '--no-embedding', '--no-extract', '--no-follow', '--offset', '--path', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--save', '--session', '--since', '--slug', '--slugs', '--source', '--stale', '--stats', '--supersessions', '--thin', '--timeout', '--timeout-ms', '--title', '--trusted-extraction', '--url', '--with-db', '--yes'],+ 'brainstorm': ['--aliases', '--all', '--brain', '--chunker-debug', '--code', '--compile', '--fast', '--fix', '--force', '--force-rechunk', '--force-resume', '--from-pages', '--full', '--help', '--http', '--include-null-signature', '--json', '--judge-model', '--lang', '--limit', '--list-runs', '--markdown', '--max-cost', '--max-far-set', '--max-ideas-per-judge-call', '--model', '--no-embed', '--no-embedding', '--no-extract', '--no-save', '--pattern', '--pending', '--reset', '--resolve', '--resume', '--retry-failed', '--retry-judge', '--save', '--source', '--stale', '--strict-budget', '--supersessions', '--thin', '--timeout', '--yes'],+ 'cache': ['--brain', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--no-embedding', '--source', '--yes'],+ 'calibration': ['--ab', '--aliases', '--all', '--allow-empty', '--apply', '--asof', '--auto', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--by-mention', '--content', '--date', '--days', '--dry-run', '--entities', '--explain', '--fast', '--federated', '--file', '--force', '--from-pages', '--help', '--holder', '--http', '--image', '--include-null-signature', '--json', '--key-prefix', '--kind', '--lang', '--limit', '--markdown', '--max-usd', '--mode', '--multimodal', '--near-symbol', '--no-embedding', '--no-extract', '--no-federated', '--offset', '--path', '--pattern', '--pending', '--phase', '--progress-interval', '--progress-json', '--quiet', '--regenerate', '--repo', '--reset', '--resolve', '--restore-only', '--save', '--scrub-gstack', '--session', '--since', '--slug', '--slugs', '--source', '--stale', '--stats', '--supersessions', '--symbol-kind', '--thin', '--trusted-extraction', '--undo-wave', '--url', '--with-calibration', '--with-db', '--yes'],+ 'call': ['--aliases', '--all', '--all-sources', '--as-context', '--auto-fix', '--background', '--brain', '--by-mention', '--catch-up', '--concurrency', '--confirm-destructive', '--content', '--cost-estimate', '--count', '--days', '--depth', '--dim', '--dir', '--direction', '--enable-dcr', '--enable-dcr-insecure', '--explain', '--fast', '--federated', '--file', '--fix', '--follow', '--force', '--from', '--from-meetings', '--grant-types', '--grep', '--hard-deadline', '--help', '--http', '--image', '--include-expired', '--include-frontmatter', '--include-null-signature', '--infer-dates', '--install', '--interval', '--json', '--key', '--kind', '--lang', '--limit', '--link-source', '--link-type', '--migrate-only', '--missing-path', '--multimodal', '--ner', '--no-embed', '--no-expand', '--no-extract', '--no-federated', '--no-hard-deadline', '--no-retry-connect', '--no-save', '--older-than', '--page', '--param', '--params', '--password', '--path', '--pattern', '--pending', '--pglite', '--port', '--progress-interval', '--progress-json', '--public-url', '--queue', '--quiet', '--reenrich-after', '--refresh-cache', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--restore-only', '--save', '--scopes', '--session', '--sigma', '--since', '--slug', '--slug-prefix', '--source', '--source-id', '--stale', '--status', '--stdin', '--strategy', '--supabase', '--supersessions', '--symbol-kind', '--tag', '--thin', '--timeout', '--to', '--today', '--token', '--token-ttl', '--tools-json', '--type', '--url', '--version', '--watch', '--with-calibration', '--workers', '--yes'],+ 'capture': ['--aliases', '--all', '--allow-empty', '--apply', '--asof', '--auto', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--by-mention', '--content', '--date', '--days', '--depth', '--entities', '--fast', '--federated', '--file', '--force', '--from-pages', '--help', '--http', '--image', '--include-null-signature', '--json', '--kind', '--limit', '--max-usd', '--mcp-only', '--mode', '--multimodal', '--no-embedding', '--no-extract', '--no-federated', '--offset', '--path', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--repo', '--reset', '--resolve', '--restore-only', '--save', '--scopes', '--session', '--since', '--slug', '--slugs', '--source', '--stale', '--stats', '--stdin', '--supersessions', '--thin', '--timeout', '--trusted-extraction', '--type', '--url', '--what', '--where', '--who', '--with-db', '--yes'],+ 'check-backlinks': ['--background', '--brain', '--brain-wide-max-cost-usd', '--dir', '--dry-run', '--explain', '--follow', '--help', '--include-frontmatter', '--json', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--source', '--stale', '--timeout', '--type'],+ 'check-resolvable': ['--brain', '--dry-run', '--fix', '--help', '--json', '--skills-dir', '--source', '--strict', '--verbose'],+ 'check-update': ['--all', '--brain', '--check', '--ff-only', '--help', '--json', '--markdown', '--migrate-only', '--non-interactive', '--refresh-cache', '--source', '--swap-only', '--to', '--version', '--yes'],+ 'claw-test': ['--agent', '--brain', '--dir', '--help', '--json', '--keep-tempdir', '--list-agents', '--live', '--local', '--message', '--no-embed', '--no-embedding', '--path', '--pglite', '--progress-json', '--prompt-file', '--run-id', '--scenario', '--source', '--transcripts'],+ 'code-callees': ['--aliases', '--all', '--all-sources', '--brain', '--chunker-debug', '--clone-dir', '--confirm-destructive', '--federated', '--force', '--help', '--include-null-signature', '--json', '--limit', '--no-extract', '--no-federated', '--no-json', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--thin', '--url', '--url-managed', '--yes'],+ 'code-callers': ['--aliases', '--all', '--all-sources', '--brain', '--chunker-debug', '--clone-dir', '--confirm-destructive', '--federated', '--force', '--help', '--include-null-signature', '--json', '--limit', '--no-extract', '--no-federated', '--no-json', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--thin', '--url', '--url-managed', '--yes'],+ 'code-def': ['--aliases', '--all', '--brain', '--chunker-debug', '--help', '--include-null-signature', '--json', '--lang', '--limit', '--no-extract', '--no-json', '--pattern', '--pending', '--pretty', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--yes'],+ 'code-refs': ['--aliases', '--all', '--brain', '--chunker-debug', '--help', '--include-null-signature', '--json', '--lang', '--limit', '--no-extract', '--no-json', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--yes'],+ 'config': ['--aliases', '--all', '--brain', '--column', '--coverage-override', '--embedding-dimensions', '--embedding-model', '--fast', '--follow', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--markdown', '--model', '--multimodal', '--no-embedding', '--no-extract', '--pattern', '--pending', '--pglite', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--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', '--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', '--abi', '--aliases', '--all', '--allow-shell-jobs', '--auto', '--auto-fix', '--auto-update', '--background', '--batch', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--build-index', '--by-mention', '--by-type', '--check', '--column', '--compile', '--concurrency', '--confidence', '--content-audit', '--count', '--days', '--detach', '--dim', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--exclusive', '--explain', '--fast', '--fix', '--follow', '--force', '--force-break-lock', '--force-retry', '--force-schema', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--grant-types', '--health-interval', '--help', '--history', '--http', '--include-flagged', '--include-frontmatter', '--include-null-signature', '--include-pseudo', '--index-audit', '--input', '--json', '--lang', '--limit', '--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-embed', '--no-embedding', '--no-extract', '--no-mutate', '--oauth-client-secret', '--older-than', '--once', '--overwrite', '--parallel', '--params', '--path', '--pattern', '--pending', '--pglite', '--phase', '--pid-file', '--porcelain', '--priority', '--progress-interval', '--progress-json', '--query', '--queue', '--quiet', '--rebuild-rollup', '--refresh', '--refresh-unqualified', '--regenerate', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--restore-only', '--resume', '--review-lower', '--rollback', '--scope', '--scopes', '--since', '--skills-dir', '--skip-bare-tweet', '--skip-failed', '--skip-urls', '--skip-verify', '--slugs', '--source', '--source-id', '--stale', '--status', '--strategy', '--strict', '--supabase', '--supersessions', '--symbol-kind', '--target', '--target-score', '--thin', '--timeout', '--to', '--top-k', '--type', '--undo-wave', '--unsafe-bypass-dream-guard', '--untracked-files', '--url', '--use-captured-snapshot', '--verbose', '--version', '--window', '--with-calibration', '--workers', '--yes'],+ 'dream': ['--against', '--aliases', '--all', '--anchor', '--asof', '--background', '--batch', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--budget-usd', '--by-type', '--by-type-floor', '--code', '--compile', '--concurrent', '--ctx-size', '--cycles', '--date', '--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', '--from', '--from-db', '--from-pages', '--help', '--http', '--include-null-signature', '--input', '--install', '--json', '--judge-model', '--keyword-only', '--lang', '--limit', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-runtime', '--max-tokens', '--max-usd', '--mcp-only', '--min-recall', '--mode', '--model', '--models', '--multimodal', '--name', '--near-symbol', '--no', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--no-llm', '--no-mutate', '--no-trajectory', '--once', '--output', '--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', '--skip-replay', '--slot-a-model', '--slot-b-model', '--slot-c-model', '--slug', '--slug-prefix', '--source', '--source-id', '--stale', '--supabase', '--supersessions', '--symbol-kind', '--take', '--task', '--thin', '--threshold', '--timeout', '--to', '--top-k', '--undo', '--unsafe-bypass-dream-guard', '--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', '--symbol-kind', '--thin', '--timeout', '--version'],+ 'enrich': ['--aliases', '--all', '--all-sources', '--allow-empty', '--apply', '--asof', '--auto', '--background', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--budget-usd-per-day', '--by-mention', '--clone-dir', '--code', '--concurrency', '--confirm-destructive', '--content', '--date', '--days', '--dry-run', '--embedding-dimensions', '--embedding-model', '--entities', '--explain', '--fast', '--federated', '--file', '--fix', '--follow', '--force', '--force-break-lock', '--from-pages', '--help', '--http', '--image', '--include-null-signature', '--json', '--judge-model', '--kind', '--lang', '--limit', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-runtime', '--max-usd', '--min-context', '--mode', '--model', '--multimodal', '--near-symbol', '--no', '--no-embed', '--no-embedding', '--no-extract', '--offset', '--older-than', '--order', '--path', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--reenrich-after', '--remediate', '--reset', '--resolve', '--restore-only', '--resume', '--save', '--session', '--since', '--slug', '--slugs', '--source', '--source-id', '--stale', '--stats', '--supersessions', '--symbol-kind', '--thin', '--thin-threshold', '--timeout', '--trusted-extraction', '--types', '--url', '--url-managed', '--version', '--with-db', '--workers', '--yes'],+ 'eval': ['--ab-relational', '--against', '--aliases', '--all', '--background', '--baseline', '--batch', '--brain', '--brain-wide-max-cost-usd', '--budget-usd', '--budget-usd-answer', '--budget-usd-retrieval', '--compare', '--compare-limit', '--concurrent', '--config-a', '--config-b', '--corpus', '--cycles', '--days', '--dedup-cosine', '--dedup-max-per-page', '--dedup-type-ratio', '--dimensions', '--distance-min', '--embedding-dimensions', '--embedding-model', '--expand', '--explain', '--fast', '--fixtures', '--follow', '--force', '--from-capture', '--from-db', '--from-pages', '--grounding-min', '--help', '--http', '--include-null-signature', '--input', '--json', '--judge', '--k', '--limit', '--max-pair-chars', '--max-tokens', '--max-usd', '--md', '--metric', '--min-recall', '--mode', '--model', '--models', '--modes', '--multimodal', '--name', '--no', '--no-cache', '--no-embed', '--no-embedding', '--no-expand', '--no-extract', '--no-llm', '--older-than', '--output', '--output-dir', '--parallel', '--pattern', '--pending', '--progress-interval', '--progress-json', '--qrels', '--queries-file', '--query', '--questions', '--quiet', '--receipt-dir', '--refresh-cache', '--remediate', '--reset', '--resolve', '--rrf-k', '--rubric-version', '--runs', '--sampling', '--save', '--seed', '--severity', '--short', '--show-toplevel', '--since', '--skip-replay', '--slot-a-model', '--slot-b-model', '--slot-c-model', '--slug', '--slug-prefix', '--source', '--stale', '--strategy', '--strict', '--suite', '--suites', '--supersessions', '--task', '--thin', '--threshold', '--threshold-expected-top1', '--threshold-first-relevant-hit', '--threshold-jaccard', '--threshold-latency-multiplier', '--threshold-recall-at-k', '--threshold-top1', '--timeout', '--tool', '--top-k', '--top-regressions', '--until', '--usefulness-min', '--verbose', '--version', '--with-code-intel', '--yes'],+ 'export': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--dir', '--explain', '--federated', '--fix', '--follow', '--help', '--include-null-signature', '--json', '--lang', '--markdown', '--multimodal', '--near-symbol', '--no-extract', '--no-federated', '--path', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--repo', '--reset', '--resolve', '--restore-only', '--slug-prefix', '--source', '--stale', '--supersessions', '--symbol-kind', '--thin', '--timeout', '--type'],+ 'extract': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--by-mention', '--catch-up', '--code', '--concurrency', '--dir', '--dry-run', '--explain', '--federated', '--follow', '--from-meetings', '--help', '--include-frontmatter', '--include-null-signature', '--infer-dates', '--json', '--kind', '--lang', '--markdown', '--max-age', '--max-cost-usd', '--multimodal', '--name-status', '--near-symbol', '--ner', '--no-extract', '--no-federated', '--older-than', '--pack', '--path', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--repo', '--reset', '--resolve', '--restore-only', '--run-id', '--since', '--slug', '--source', '--source-id', '--stale', '--strategy', '--supersessions', '--symbol-kind', '--thin', '--timeout', '--type', '--verbose', '--workers', '--yes'],+ 'extract-conversation-facts': ['--aliases', '--all', '--all-sources', '--background', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--by-mention', '--clone-dir', '--code', '--concurrency', '--confirm-destructive', '--dry-run', '--embedding-dimensions', '--embedding-model', '--explain', '--fix', '--follow', '--force', '--force-break-lock', '--help', '--include-null-signature', '--json', '--judge-model', '--lang', '--limit', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-runtime', '--model', '--multimodal', '--near-symbol', '--no', '--no-embed', '--no-embedding', '--no-extract', '--older-than', '--override-disabled', '--path', '--pattern', '--pending', '--pglite', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--restore-only', '--segment-limit', '--session', '--since', '--sleep', '--slug', '--source', '--source-id', '--stale', '--supabase', '--supersessions', '--symbol-kind', '--thin', '--timeout', '--types', '--url', '--url-managed', '--version', '--workers', '--yes'],+ 'features': ['--aliases', '--all', '--auto-fix', '--background', '--batch-size', '--brain', '--by-mention', '--catch-up', '--concurrency', '--dir', '--explain', '--from-meetings', '--help', '--include-frontmatter', '--include-null-signature', '--infer-dates', '--json', '--kind', '--ner', '--no-extract', '--pace', '--pace-max-concurrency', '--pack', '--path', '--pattern', '--pending', '--priority', '--progress-json', '--quiet', '--repo', '--reset', '--resolve', '--run-id', '--since', '--slugs', '--source', '--source-id', '--stale', '--supersessions', '--thin', '--type', '--verbose', '--workers'],+ 'files': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--explain', '--fast', '--follow', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--no-embedding', '--no-extract', '--no-pointer', '--page', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--retry-failed', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--type', '--yes'],+ 'forget': ['--aliases', '--all', '--as-context', '--brain', '--fast', '--federated', '--force', '--from-pages', '--grep', '--help', '--http', '--include-expired', '--include-null-signature', '--json', '--limit', '--mcp-only', '--no-embedding', '--no-extract', '--no-federated', '--path', '--pattern', '--pending', '--reason', '--repo', '--reset', '--resolve', '--restore-only', '--rollup', '--session', '--session-id', '--since', '--since-last-run', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--today', '--watch'],+ 'founder': ['--aliases', '--all', '--brain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--mcp-only', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--since', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--until'],+ 'friction': ['--agent', '--brain', '--help', '--hint', '--json', '--kind', '--message', '--no-redact', '--phase', '--redact', '--run-id', '--severity', '--source', '--transcript-path', '--transcripts'],+ 'frontmatter': ['--aliases', '--all', '--allow-catch-all', '--brain', '--cached', '--diff-filter', '--dry-run', '--exclude-standard', '--fast', '--fix', '--force', '--from-pages', '--get', '--help', '--http', '--include-catch-all', '--include-null-signature', '--json', '--name-only', '--name-status', '--no-embedding', '--no-extract', '--no-verify', '--others', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--strategy', '--supersessions', '--thin', '--timeout', '--uninstall', '--write-back'],+ 'graph-query': ['--aliases', '--all', '--brain', '--depth', '--direction', '--explain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-foreign', '--include-null-signature', '--json', '--lang', '--markdown', '--mcp-only', '--multimodal', '--near-symbol', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--symbol-kind', '--thin', '--timeout', '--type'],+ 'import': ['--aliases', '--all', '--asof', '--background', '--brain', '--brain-wide-max-cost-usd', '--by-mention', '--cached', '--code', '--compile', '--concurrency', '--embedding-dimensions', '--embedding-model', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--fix', '--follow', '--force', '--force-rechunk', '--fresh', '--from-pages', '--full', '--help', '--http', '--include-gitignored', '--include-null-signature', '--json', '--lang', '--markdown', '--max-age', '--multimodal', '--name-status', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--older-than', '--others', '--path', '--pattern', '--pending', '--pglite', '--priority', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--since', '--skip-failed', '--source', '--source-id', '--stale', '--strategy', '--supabase', '--supersessions', '--thin', '--timeout', '--url', '--workers'],+ 'init': ['--all', '--brain', '--chat-model', '--check', '--ctx-size', '--embedding-dimensions', '--embedding-model', '--expansion-model', '--fast', '--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', '--schema-pack', '--scopes', '--skip-embed-check', '--source', '--stale', '--supabase', '--target', '--to', '--touchpoint', '--url', '--version'],+ 'integrations': ['--auto', '--brain', '--dry-run', '--embeddings', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--no-embedding', '--overwrite', '--refresh', '--reranking', '--source', '--target'],+ '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', '--thin', '--timeout', '--type', '--url'],+ 'jobs': ['--abbrev-ref', '--aliases', '--all', '--allow-empty', '--allow-protected', '--allow-shell-jobs', '--apply', '--asof', '--auto', '--auto-with-prompt', '--background', '--backoff-delay', '--backoff-jitter', '--backoff-type', '--batch-size', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--break-lock', '--budget-usd', '--budget-usd-per-day', '--by-mention', '--cached', '--catch-up', '--check', '--cli-path', '--cluster', '--cluster-errors', '--code', '--concurrency', '--confidence', '--confirm-destructive', '--content', '--date', '--days', '--delay', '--detach', '--diff-filter', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--empty', '--entities', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--federated-read', '--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', '--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-gpg-sign', '--no-hard-deadline', '--no-inject', '--no-mutate', '--no-pull', '--no-renames', '--no-schema-pack', '--no-verify', '--no-worker', '--now', '--offset', '--older-than', '--once', '--order', '--others', '--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-ms', '--remediate', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--resume', '--retry-failed', '--review-lower', '--run-id', '--save', '--segment-limit', '--serial', '--session', '--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', '--swap-only', '--symbol-kind', '--target', '--thin', '--thin-threshold', '--timeout', '--timeout-ms', '--trusted-extraction', '--type', '--types', '--uninstall', '--unsafe-bypass-dream-guard', '--url', '--user', '--verbose', '--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', '--thin', '--timeout'],+ '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', '--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'],+ 'migrate': ['--ab', '--aliases', '--all', '--auto-update', '--background', '--batch-size', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--build-index', '--by-mention', '--catch-up', '--compile', '--days', '--dim', '--dry-run', '--embedding-dimensions', '--embedding-model', '--embeddings', '--exclusive', '--explain', '--fast', '--follow', '--force', '--force-retry', '--force-schema', '--from-meetings', '--from-pages', '--help', '--history', '--http', '--ignore-env-override', '--ignore-missing-key', '--include-null-signature', '--json', '--lang', '--locks', '--markdown', '--max-age', '--model', '--multimodal', '--name', '--near-symbol', '--no', '--no-embed', '--no-embedding', '--no-extract', '--non-interactive', '--pace', '--pace-max-concurrency', '--parallel', '--path', '--pattern', '--pending', '--phase', '--prefix', '--priority', '--progress-interval', '--progress-json', '--quiet', '--refresh-unqualified', '--remediate', '--reranking', '--reset', '--resolve', '--restore-only', '--resume', '--rollback', '--skip-verify', '--slugs', '--source', '--stale', '--supersessions', '--symbol-kind', '--thin', '--timeout', '--to', '--undo', '--undo-wave', '--url', '--use-captured-snapshot', '--version', '--with-calibration', '--yes'],+ 'models': ['--aliases', '--all', '--brain', '--embedding-dimensions', '--embedding-model', '--embeddings', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--judge-model', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reranking', '--reset', '--resolve', '--skip', '--source', '--stale', '--supersessions', '--thin', '--undo', '--version'],+ 'mounts': ['--alias', '--brain', '--cache', '--database-path', '--database-url', '--db-path', '--db-url', '--engine', '--explain', '--help', '--id', '--json', '--lang', '--lock', '--markdown', '--mcp-url', '--multimodal', '--near-symbol', '--path', '--restore-only', '--skills-dir', '--source', '--stale', '--symbol-kind', '--thin', '--verbose'],+ 'notability-eval': ['--aliases', '--all', '--brain', '--embedding-dimensions', '--embedding-model', '--help', '--in', '--include-null-signature', '--json', '--model', '--multimodal', '--no', '--no-embed', '--no-extract', '--out', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--skip-llm', '--source', '--stale', '--supersessions', '--target-high', '--target-low', '--target-medium', '--thin', '--version'],+ 'onboard': ['--aliases', '--all', '--allow-empty', '--allow-protected', '--apply', '--asof', '--auto', '--auto-with-prompt', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--by-mention', '--check', '--content', '--date', '--days', '--entities', '--explain', '--federated', '--file', '--from-pages', '--help', '--history', '--http', '--image', '--include-null-signature', '--json', '--kind', '--limit', '--max-usd', '--mode', '--multimodal', '--no-extract', '--offset', '--params', '--path', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--remediation-plan', '--reset', '--resolve', '--resume', '--save', '--session', '--since', '--slug', '--slugs', '--source', '--stale', '--stats', '--supersessions', '--target-score', '--thin', '--trusted-extraction', '--url', '--with-db', '--yes'],+ 'orphans': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--count', '--explain', '--follow', '--help', '--include-null-signature', '--include-pseudo', '--json', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--timeout'],+ 'pages': ['--aliases', '--all', '--brain', '--dry-run', '--help', '--include-null-signature', '--json', '--no-extract', '--older-than', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin'],+ 'pglite-repair': ['--brain', '--break-lock', '--dry-rnu', '--dry-run', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--no-embedding', '--path', '--quiet', '--source', '--yes'],+ 'post-upgrade': ['--all', '--apply-clean-hunks', '--brain', '--check', '--code', '--compile', '--concurrency', '--embedding-dimensions', '--embedding-model', '--fast', '--ff-only', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--format', '--from-pages', '--help', '--host-dir', '--http', '--inject-bootstrap', '--inline', '--install', '--interval', '--json', '--limit', '--list', '--markdown', '--max-rss', '--migrate-only', '--migration', '--mode', '--model', '--multimodal', '--name-only', '--no', '--no-autopilot-install', '--no-embed', '--no-embedding', '--no-inject', '--no-worker', '--non-interactive', '--now', '--quiet', '--repo', '--since', '--skills-dir', '--skip-verify', '--source', '--stale', '--status', '--swap-only', '--target', '--to', '--uninstall', '--user', '--verbose', '--verify', '--version', '--workers', '--yes'],+ 'providers': ['--brain', '--ctx-size', '--embedding-dimensions', '--embedding-model', '--embeddings', '--fast', '--force', '--from-pages', '--help', '--http', '--json', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--reranking', '--source', '--touchpoint', '--version'],+ 'publish': ['--accent', '--bg', '--border', '--brain', '--card-bg', '--code-bg', '--error', '--fg', '--help', '--json', '--link', '--muted', '--out', '--password', '--source', '--title'],+ 'quarantine': ['--aliases', '--all', '--apply', '--brain', '--code', '--compile', '--explain', '--fast', '--fix', '--force', '--force-rechunk', '--from-pages', '--help', '--http', '--include-flagged', '--include-null-signature', '--json', '--lang', '--limit', '--markdown', '--multimodal', '--near-symbol', '--no-embed', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--symbol-kind', '--thin'],+ 'recall': ['--aliases', '--all', '--as-context', '--brain', '--fast', '--federated', '--force', '--from-pages', '--grep', '--help', '--http', '--include-expired', '--include-null-signature', '--json', '--limit', '--mcp-only', '--no-embedding', '--no-extract', '--no-federated', '--path', '--pattern', '--pending', '--reason', '--repo', '--reset', '--resolve', '--restore-only', '--rollup', '--session', '--session-id', '--since', '--since-last-run', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--today', '--watch'],+ 'reconcile-links': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--explain', '--follow', '--help', '--include-frontmatter', '--include-null-signature', '--json', '--name-status', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--source', '--stale', '--strategy', '--supersessions', '--thin', '--timeout', '--type'],+ 'reindex': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--break-lock', '--code', '--compile', '--concurrency', '--cost-estimate', '--dry-run', '--embedding-dimensions', '--embedding-model', '--explain', '--fast', '--fix', '--follow', '--force', '--force-break-lock', '--force-rechunk', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--lang', '--limit', '--markdown', '--max-age', '--max-cost-usd', '--model', '--multimodal', '--no', '--no-embed', '--no-embedding', '--no-extract', '--older-than', '--path', '--pattern', '--pending', '--pglite', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--repo', '--reset', '--resolve', '--source', '--stale', '--supabase', '--supersessions', '--thin', '--timeout', '--version', '--workers', '--yes'],+ 'reindex-code': ['--abi', '--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--chunker-debug', '--code', '--compile', '--concurrency', '--dry-run', '--embedding-dimensions', '--embedding-model', '--explain', '--fix', '--follow', '--force', '--force-rechunk', '--help', '--include-null-signature', '--json', '--judge-model', '--lang', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-runtime', '--model', '--multimodal', '--no', '--no-embed', '--no-extract', '--older-than', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--serial', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--version', '--workers', '--yes'],+ 'reindex-frontmatter': ['--aliases', '--all', '--brain', '--concurrency', '--dry-run', '--force', '--help', '--include-null-signature', '--json', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--slug-prefix', '--source', '--stale', '--supersessions', '--thin', '--workers', '--yes'],+ 'reindex-search-vector': ['--aliases', '--all', '--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--explain', '--follow', '--help', '--include-null-signature', '--json', '--no-extract', '--pattern', '--pending', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--yes'],+ 'reinit-pglite': ['--abbrev-ref', '--all', '--allow-empty', '--brain', '--break-lock', '--chat-model', '--concurrency', '--confirm-destructive', '--diff-filter', '--embedding-dimensions', '--embedding-model', '--empty', '--exclude', '--exclude-standard', '--expansion-model', '--fast', '--force', '--force-break-lock', '--format', '--from-pages', '--full', '--grant-types', '--hard-deadline', '--help', '--http', '--include-gitignored', '--interval', '--is-ancestor', '--issuer-url', '--json', '--key', '--lock', '--max-age', '--max-sources', '--mcp-only', '--mcp-url', '--migrate-only', '--missing-path', '--model', '--name-only', '--name-status', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-gpg-sign', '--no-hard-deadline', '--no-pull', '--no-renames', '--no-schema-pack', '--no-sync', '--no-verify', '--non-interactive', '--oauth-client-id', '--oauth-client-secret', '--parallel', '--path', '--pglite', '--priority', '--quiet', '--repo', '--retry-failed', '--schema-pack', '--scopes', '--serial', '--short', '--show-toplevel', '--skip-embed-check', '--skip-failed', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--strategy', '--supabase', '--target', '--timeout', '--to', '--url', '--version', '--watch', '--workers', '--yes'],+ 'remote': ['--all', '--background', '--brain', '--break-lock', '--by-mention', '--column', '--concurrency', '--content-audit', '--count', '--detach', '--dim', '--dir', '--drain', '--embedding-dimensions', '--embedding-model', '--fast', '--fix', '--force', '--force-retry', '--force-schema', '--from-pages', '--full', '--help', '--http', '--include-flagged', '--index-audit', '--json', '--locks', '--markdown', '--max-cost', '--max-cost-usd', '--max-jobs', '--max-rss', '--max-usd', '--mcp-only', '--migrate-only', '--multimodal', '--nice', '--no-embedding', '--older-than', '--parallel', '--params', '--pglite', '--phase', '--pid-file', '--progress-json', '--query', '--queue', '--rebuild-rollup', '--regenerate', '--remediate', '--remediation-plan', '--reset', '--resume', '--scope', '--scopes', '--since', '--skills-dir', '--skip-failed', '--slugs', '--source', '--source-id', '--stale', '--status', '--supabase', '--target', '--target-score', '--timeout', '--to', '--top-k', '--url', '--window', '--workers', '--yes'],+ 'repair-jsonb': ['--background', '--brain', '--brain-wide-max-cost-usd', '--dry-run', '--explain', '--fast', '--follow', '--force', '--from-pages', '--help', '--http', '--json', '--lang', '--markdown', '--multimodal', '--near-symbol', '--no-embedding', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--restore-only', '--source', '--stale', '--supabase', '--symbol-kind', '--thin', '--timeout', '--url'],+ 'report': ['--brain', '--content', '--dir', '--help', '--json', '--source', '--title', '--type'],+ 'repos': ['--abbrev-ref', '--abort', '--aliases', '--all', '--all-sources', '--brain', '--branch', '--break-lock', '--cached', '--clone-dir', '--compile', '--confirm-destructive', '--detect', '--dry-run', '--explain', '--fast', '--federated', '--file', '--fix', '--force', '--force-break-lock', '--format', '--from-pages', '--full', '--get', '--git-dir', '--github-repo', '--help', '--http', '--include-null-signature', '--include-warns', '--json', '--keep-storage', '--lang', '--local', '--markdown', '--max-age', '--max-cost-usd', '--multimodal', '--name', '--name-only', '--name-status', '--near-symbol', '--no-cron', '--no-embedding', '--no-extract', '--no-federate', '--no-federated', '--no-harden', '--no-verify', '--params', '--pat-file', '--path', '--pattern', '--pending', '--porcelain', '--push-only', '--quiet', '--rebase', '--repo', '--reset', '--resolve', '--restore-only', '--secret', '--set', '--short', '--source', '--source-id', '--stale', '--status', '--strategy', '--supersessions', '--symbol-kind', '--thin', '--unset-all', '--url', '--url-managed', '--yes'],+ 'resolvers': ['--auto', '--backend', '--brain', '--cost', '--help', '--json', '--source'],+ 'retrieval-upgrade': ['--aliases', '--all', '--background', '--batch-size', '--brain', '--brain-wide-max-cost-usd', '--catch-up', '--dim', '--dry-run', '--embedding-dimensions', '--embedding-model', '--embeddings', '--explain', '--fast', '--follow', '--force', '--from-pages', '--help', '--http', '--ignore-env-override', '--ignore-missing-key', '--include-null-signature', '--json', '--markdown', '--model', '--multimodal', '--name', '--no', '--no-embed', '--no-embedding', '--no-extract', '--non-interactive', '--pace', '--pace-max-concurrency', '--parallel', '--pattern', '--pending', '--prefix', '--priority', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--reranking', '--reset', '--resolve', '--resume', '--slugs', '--source', '--stale', '--supersessions', '--thin', '--timeout', '--to', '--undo', '--version', '--yes'],+ 'routing-eval': ['--brain', '--fix', '--help', '--json', '--llm', '--skills-dir', '--source', '--strict', '--verbose'],+ 'salience': ['--aliases', '--all', '--brain', '--days', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--kind', '--limit', '--mcp-only', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--slug-prefix', '--source', '--stale', '--supersessions', '--thin', '--timeout'],+ 'schema': ['--alias', '--aliases', '--all', '--apply', '--as-filing-rules', '--brain', '--dims', '--expert', '--expert-routing', '--extractable', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--inverse', '--json', '--kind', '--no-embedding', '--no-extract', '--pack', '--page-type', '--pattern', '--pending', '--prefix', '--primitive', '--reset', '--resolve', '--schema-pack', '--since', '--source', '--source-id', '--stale', '--supersessions', '--target-type', '--thin', '--to', '--with-db'],+ 'self-upgrade': ['--all', '--brain', '--check', '--check-only', '--ff-only', '--force', '--help', '--json', '--markdown', '--migrate-only', '--non-interactive', '--refresh-cache', '--source', '--swap-only', '--to', '--version', '--yes'],+ 'serve': ['--aliases', '--all', '--bind', '--bound-slug-prefixes', '--brain', '--enable-dcr', '--enable-dcr-insecure', '--federated-read', '--help', '--http', '--include-null-signature', '--json', '--log-full-params', '--no-extract', '--pattern', '--pending', '--port', '--print-admin-token', '--public-url', '--reset', '--resolve', '--source', '--stale', '--stdio-idle-timeout', '--supersessions', '--suppress', '--suppress-bootstrap-token', '--thin', '--token-ttl', '--yes'],+ 'skillify': ['--brain', '--description', '--dry-run', '--force', '--help', '--json', '--mutating', '--recent', '--skills-dir', '--source', '--strict', '--triggers', '--verbose', '--writes-pages', '--writes-to'],+ 'skillopt': ['--aliases', '--all', '--allow-mutate-bundled', '--background', '--batch-size', '--benchmark', '--bootstrap-from-routing', '--bootstrap-from-skill', '--bootstrap-reviewed', '--bootstrap-tasks', '--brain', '--brain-wide-max-cost-usd', '--chunker-debug', '--dry-run', '--epochs', '--follow', '--force', '--held-out', '--help', '--include-null-signature', '--json', '--judge-model', '--lr', '--lr-schedule', '--max-cost-usd', '--max-runtime-min', '--model', '--no-extract', '--no-mutate', '--optimizer-model', '--patch', '--pattern', '--pending', '--reset', '--resolve', '--resume', '--rewrite', '--skills-dir', '--source', '--split', '--stale', '--supersessions', '--target-model', '--target-models', '--thin', '--verbose', '--yes'],+ 'skillpack': ['--all', '--apply-clean-hunks', '--author', '--brain', '--dry-run', '--exit-code', '--fast', '--fix', '--force', '--force-unlock', '--format', '--from', '--from-pages', '--frontmatter', '--full', '--help', '--homepage', '--http', '--json', '--license', '--list', '--minimal', '--name-only', '--no-cache', '--no-embedding', '--no-lint', '--note', '--out', '--overwrite-local', '--push', '--quick', '--quiet', '--refresh', '--repo', '--schema-pack', '--short', '--since', '--skills-dir', '--skip-doctor', '--source', '--strict', '--target', '--tier', '--trust', '--url', '--verbose', '--verify', '--workspace', '--yes'],+ 'skillpack-check': ['--background', '--brain', '--brain-wide-max-cost-usd', '--explain', '--fast', '--follow', '--help', '--json', '--list', '--progress-interval', '--progress-json', '--quiet', '--remediate', '--source', '--stale', '--strict', '--timeout', '--yes'],+ 'smoke-test': ['--brain', '--help', '--json', '--source'],+ 'sources': ['--abbrev-ref', '--abort', '--aliases', '--all', '--all-sources', '--brain', '--branch', '--break-lock', '--cached', '--clone-dir', '--compile', '--confirm-destructive', '--detect', '--dry-run', '--explain', '--fast', '--federated', '--file', '--fix', '--force', '--force-break-lock', '--format', '--from-pages', '--full', '--get', '--git-dir', '--github-repo', '--help', '--http', '--include-null-signature', '--include-warns', '--json', '--keep-storage', '--lang', '--local', '--markdown', '--max-age', '--max-cost-usd', '--multimodal', '--name', '--name-only', '--name-status', '--near-symbol', '--no-cron', '--no-embedding', '--no-extract', '--no-federate', '--no-federated', '--no-harden', '--no-verify', '--params', '--pat-file', '--path', '--pattern', '--pending', '--porcelain', '--push-only', '--quiet', '--rebase', '--repo', '--reset', '--resolve', '--restore-only', '--secret', '--set', '--short', '--source', '--source-id', '--stale', '--status', '--strategy', '--supersessions', '--symbol-kind', '--thin', '--unset-all', '--url', '--url-managed', '--yes'],+ 'status': ['--abbrev-ref', '--abi', '--abort', '--aliases', '--all', '--all-sources', '--allow-empty', '--apply', '--asof', '--auto', '--background', '--batch-size', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--brain-wide-max-cost-usd', '--branch', '--break-lock', '--budget-usd-per-day', '--by-mention', '--cached', '--catch-up', '--clone-dir', '--code', '--column', '--compile', '--concurrency', '--confirm-destructive', '--content', '--content-audit', '--count', '--date', '--days', '--deadline-ms', '--depth', '--detach', '--diff-filter', '--dim', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--empty', '--entities', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--ff-only', '--file', '--fix', '--follow', '--force', '--force-break-lock', '--force-rechunk', '--force-retry', '--force-schema', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--git-path', '--hard-deadline', '--help', '--http', '--image', '--include-flagged', '--include-frontmatter', '--include-gitignored', '--include-null-signature', '--index-audit', '--infer-dates', '--install', '--interval', '--is-ancestor', '--json', '--judge-model', '--kind', '--lang', '--limit', '--lock', '--locks', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-jobs', '--max-rss', '--max-runtime', '--max-sources', '--max-usd', '--mcp-only', '--migrate-only', '--min-context', '--missing-path', '--mode', '--model', '--multimodal', '--name', '--name-only', '--name-status', '--near-symbol', '--ner', '--nice', '--no', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--no-gpg-sign', '--no-hard-deadline', '--no-pull', '--no-recurse-submodules', '--no-renames', '--no-schema-pack', '--no-verify', '--object-format', '--offset', '--older-than', '--order', '--others', '--overwrite', '--pace', '--pace-max-concurrency', '--pack', '--parallel', '--params', '--path', '--pattern', '--pending', '--pglite', '--phase', '--pid-file', '--porcelain', '--prefix', '--priority', '--progress-interval', '--progress-json', '--query', '--queue', '--quiet', '--rebase', '--rebuild-rollup', '--reenrich-after', '--refresh', '--regenerate', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--resume', '--retry-failed', '--run-id', '--save', '--scope', '--scopes', '--section', '--serial', '--session', '--short', '--show-toplevel', '--since', '--skills-dir', '--skip-failed', '--slug', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--stats', '--status', '--stdin', '--strategy', '--supabase', '--supersessions', '--symbol-kind', '--target', '--target-score', '--thin', '--thin-threshold', '--timeout', '--to', '--top-k', '--trusted-extraction', '--type', '--types', '--url', '--url-managed', '--verbose', '--verify', '--version', '--watch', '--what', '--where', '--who', '--window', '--with-db', '--workers', '--yes'],+ 'storage': ['--aliases', '--all', '--brain', '--federated', '--fix', '--help', '--include-null-signature', '--json', '--no-extract', '--no-federated', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--thin', '--to'],+ 'sync': ['--abbrev-ref', '--abi', '--abort', '--aliases', '--all', '--all-sources', '--allow-empty', '--asof', '--auto', '--background', '--batch-size', '--brain', '--brain-wide-max-cost-usd', '--branch', '--break-lock', '--by-mention', '--cached', '--catch-up', '--clone-dir', '--code', '--column', '--compile', '--concurrency', '--confirm-destructive', '--content-audit', '--count', '--depth', '--detach', '--diff-filter', '--dim', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--empty', '--exclude', '--exclude-standard', '--explain', '--fast', '--federated', '--ff-only', '--fix', '--follow', '--force', '--force-break-lock', '--force-rechunk', '--force-retry', '--force-schema', '--format', '--fresh', '--from-meetings', '--from-pages', '--full', '--git-path', '--hard-deadline', '--help', '--http', '--include-flagged', '--include-frontmatter', '--include-gitignored', '--include-null-signature', '--index-audit', '--infer-dates', '--interval', '--is-ancestor', '--json', '--kind', '--lang', '--lock', '--locks', '--markdown', '--max-age', '--max-cost', '--max-cost-usd', '--max-jobs', '--max-rss', '--max-sources', '--max-usd', '--migrate-only', '--missing-path', '--model', '--multimodal', '--name', '--name-only', '--name-status', '--ner', '--nice', '--no-auto-embed', '--no-embed', '--no-embedding', '--no-extract', '--no-federated', '--no-gpg-sign', '--no-hard-deadline', '--no-pull', '--no-recurse-submodules', '--no-renames', '--no-schema-pack', '--no-verify', '--object-format', '--older-than', '--others', '--overwrite', '--pace', '--pace-max-concurrency', '--pack', '--parallel', '--params', '--path', '--pattern', '--pending', '--pglite', '--phase', '--pid-file', '--porcelain', '--prefix', '--priority', '--progress-interval', '--progress-json', '--query', '--queue', '--quiet', '--rebase', '--rebuild-rollup', '--refresh', '--regenerate', '--remediate', '--remediation-plan', '--repo', '--reset', '--resolve', '--respect-gitignore', '--restore-only', '--resume', '--retry-failed', '--run-id', '--save', '--scope', '--serial', '--short', '--show-toplevel', '--since', '--skills-dir', '--skip-failed', '--slug', '--slugs', '--source', '--source-id', '--src-subpath', '--stale', '--status', '--stdin', '--strategy', '--supabase', '--supersessions', '--target', '--target-score', '--thin', '--timeout', '--to', '--top-k', '--type', '--url', '--url-managed', '--verbose', '--verify', '--watch', '--window', '--workers', '--yes'],+ 'takes': ['--aliases', '--all', '--brain', '--bucket-size', '--by', '--claim', '--dir', '--domain', '--dry-run', '--evidence', '--expired', '--fast', '--federated', '--force', '--from-pages', '--help', '--holder', '--http', '--include-covered', '--include-null-signature', '--json', '--kind', '--limit', '--max-pages', '--no-embedding', '--no-extract', '--no-federated', '--outcome', '--path', '--pattern', '--pending', '--quality', '--refresh', '--repo', '--reset', '--resolve', '--restore-only', '--row', '--since', '--slugs', '--sort', '--source', '--source-id', '--stale', '--supersessions', '--thin', '--unit', '--until', '--value', '--weight', '--who', '--yes'],+ 'think': ['--aliases', '--all', '--anchor', '--brain', '--calibration-holder', '--explain', '--fast', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--max-usd', '--mcp-only', '--model', '--no-embedding', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--rounds', '--save', '--since', '--source', '--stale', '--supersessions', '--take', '--thin', '--timeout', '--until', '--with-calibration'],+ 'transcripts': ['--aliases', '--all', '--brain', '--days', '--full', '--help', '--include-null-signature', '--json', '--limit', '--no-extract', '--pattern', '--pending', '--reset', '--resolve', '--source', '--stale', '--supersessions', '--thin', '--timeout'],+ 'upgrade': ['--all', '--apply-clean-hunks', '--brain', '--check', '--code', '--compile', '--concurrency', '--embedding-dimensions', '--embedding-model', '--fast', '--ff-only', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--format', '--from-pages', '--help', '--host-dir', '--http', '--inject-bootstrap', '--inline', '--install', '--interval', '--json', '--limit', '--list', '--markdown', '--max-rss', '--migrate-only', '--migration', '--mode', '--model', '--multimodal', '--name-only', '--no', '--no-autopilot-install', '--no-embed', '--no-embedding', '--no-inject', '--no-worker', '--non-interactive', '--now', '--quiet', '--repo', '--since', '--skills-dir', '--skip-verify', '--source', '--stale', '--status', '--swap-only', '--target', '--to', '--uninstall', '--user', '--verbose', '--verify', '--version', '--workers', '--yes'],+ 'watch': ['--aliases', '--all', '--brain', '--fast', '--federated', '--force', '--from-pages', '--help', '--http', '--include-null-signature', '--json', '--max-pages', '--min-confidence', '--no-embedding', '--no-extract', '--no-federated', '--path', '--pattern', '--pending', '--repo', '--reset', '--resolve', '--restore-only', '--source', '--stale', '--supersessions', '--thin', '--window-turns'],+ 'ze-switch': ['--aliases', '--all', '--brain', '--confirm-reembed', '--dry-run', '--force', '--help', '--ignore-env-override', '--ignore-missing-key', '--include-null-signature', '--json', '--markdown', '--no-extract', '--non-interactive', '--pattern', '--pending', '--reset', '--resolve', '--resume', '--source', '--stale', '--supersessions', '--thin', '--undo', '--yes'],+};diff --git a/src/core/engine.ts b/src/core/engine.tsindex d27739d54d..37357e8853 100644--- a/src/core/engine.ts+++ b/src/core/engine.ts@@ -990,10 +990,13 @@ export interface BrainEngine {*/upsertChunks(slug: string, chunks: ChunkInput[], opts?: { sourceId?: string } & BatchOpts): Promise<void>;/**- * Read every chunk for a page. `opts.sourceId` source-scopes the page- * lookup; without it, multi-source brains return chunks from every- * same-slug source (importCodeFile uses this for incremental embedding- * reuse, which would then attach the wrong source's embeddings).+ * Read every chunk for a page. Scope precedence mirrors getPage (#2555):+ * a federated grant (`sourceIds[]`) wins over scalar `sourceId`; with+ * neither set, the lookup falls back to the `'default'` source (the+ * local-untyped-call default that importCodeFile's incremental embedding+ * reuse relies on). Embedding vectors are never selected — rowToChunk+ * discards them at these call sites, so pulling them was pure egress+ * (#2544).*/getChunks(slug: string, opts?: { sourceId?: string; sourceIds?: string[] }): Promise<Chunk[]>;/**@@ -2104,6 +2107,9 @@ export interface BrainEngine {// Migration supportrunMigration(version: number, sql: string): Promise<void>;+ // Deliberately scalar-only (no sourceIds[] widening): engine-internal with+ // zero remote-reachable callers (verified #2555 review), so the federated+ // read-scope contract doesn't apply. Widen only if an op ever exposes it.getChunksWithEmbeddings(slug: string, opts?: { sourceId?: string }): Promise<Chunk[]>;// Raw SQL (for Minions job queue and other internal modules)diff --git a/src/core/operations.ts b/src/core/operations.tsindex 78f334cc51..797ca52a83 100644--- a/src/core/operations.ts+++ b/src/core/operations.ts@@ -342,7 +342,8 @@ export function normalizeSlugPrefix(prefix: string): string {/*** Write ops a slug-bound client may call: every op that routes through- * `enforceClientSlugFence`, plus `think` (scope `write`, but remote callers+ * `enforceClientSlugFence`, plus `think` (scope `read` for remote callers;+ * it stays on this list because it is `mutating` locally, but remote callers* cannot persist — `save`/`take` are forced false for `remote !== false`).** This list is an ALLOW-list on purpose. The fence used to be enforced op@@ -2274,7 +2275,7 @@ const takes_calibration: Operation = {const think: Operation = {name: 'think',description: 'Multi-hop synthesis across pages + takes + graph. Pulls relevant evidence and produces a cited answer with conflict + gap analysis.',- scope: 'write',+ scope: 'read',params: {question: { type: 'string', required: true, description: 'The question to think about' },anchor: { type: 'string', description: 'Pull the entity subgraph around this slug' },@@ -2285,6 +2286,8 @@ const think: Operation = {since: { type: 'string', description: 'Start of temporal window (YYYY-MM-DD or YYYY-MM)' },until: { type: 'string', description: 'End of temporal window' },},+ // Local CLI can persist with save/take; remote/MCP callers are forced+ // read-only below before runThink/persistSynthesis sees those flags.mutating: true,handler: async (ctx, p) => {const remote = ctx.remote ?? true;@@ -2314,7 +2317,7 @@ const think: Operation = {until: p.until ? String(p.until) : undefined,takesHoldersAllowList: ctx.takesHoldersAllowList,...thinkScope,- remote: ctx.remote === true,+ remote: ctx.remote !== false, // fail-closed: anything not strictly false is untrusted (CLAUDE.md invariant)});// Persist if --save was passed locally@@ -3099,6 +3102,9 @@ const get_chunks: Operation = {slug: { type: 'string', required: true },},handler: async (ctx, p) => {+ // #2555: route through the canonical scope ladder (federated array >+ // scalar floor > nothing) instead of the pre-#2200 scalar-only pattern —+ // a federated grant could read the page via get_page but got [] here.return ctx.engine.getChunks(p.slug as string, sourceScopeOpts(ctx));},scope: 'read',@@ -4151,7 +4157,7 @@ const find_trajectory: Operation = {const points = await ctx.engine.findTrajectory({entitySlug: p.entity_slug,...scope,- remote: ctx.remote === true,+ remote: ctx.remote !== false, // fail-closed: anything not strictly false is untrusted (CLAUDE.md invariant)metric,kind,since,diff --git a/src/core/pglite-engine.ts b/src/core/pglite-engine.tsindex 0727689cc1..42bd46169f 100644--- a/src/core/pglite-engine.ts+++ b/src/core/pglite-engine.ts@@ -751,7 +751,13 @@ export class PGLiteEngine implements BrainEngine {EXISTS (SELECT 1 FROM information_schema.tablesWHERE table_schema='public' AND table_name='timeline_entries') AS timeline_entries_exists,EXISTS (SELECT 1 FROM information_schema.columns- WHERE table_schema='public' AND table_name='timeline_entries' AND column_name='event_page_id') AS timeline_event_page_id_exists+ WHERE table_schema='public' AND table_name='timeline_entries' AND column_name='event_page_id') AS timeline_event_page_id_exists,+ EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema='public' AND table_name='minion_jobs') AS minion_jobs_exists,+ EXISTS (SELECT 1 FROM information_schema.columns+ WHERE table_schema='public' AND table_name='minion_jobs' AND column_name='timeout_at') AS minion_jobs_timeout_at_exists,+ EXISTS (SELECT 1 FROM information_schema.columns+ WHERE table_schema='public' AND table_name='minion_jobs' AND column_name='idempotency_key') AS minion_jobs_idempotency_key_exists`);const probe = rows[0] as {pages_exists: boolean;@@ -796,6 +802,9 @@ export class PGLiteEngine implements BrainEngine {pages_links_extracted_at_exists: boolean;timeline_entries_exists: boolean;timeline_event_page_id_exists: boolean;+ minion_jobs_exists: boolean;+ minion_jobs_timeout_at_exists: boolean;+ minion_jobs_idempotency_key_exists: boolean;};const needsPagesBootstrap = probe.pages_exists && !probe.source_id_exists;@@ -874,6 +883,12 @@ export class PGLiteEngine implements BrainEngine {const needsPagesLinksExtractedAt = probe.pages_exists && !probe.pages_links_extracted_at_exists;// v121: schema-blob indexes reference event_page_id before migrations run.const needsTimelineEventPageId = probe.timeline_entries_exists && !probe.timeline_event_page_id_exists;+ // v7-era (#2626 class sweep): minion_jobs.timeout_at + idempotency_key are+ // migration-added AND referenced by blob indexes (idx_minion_jobs_timeout,+ // uniq_minion_jobs_idempotency) — a pre-v7 minion_jobs wedges blob replay+ // exactly like the v121 incident.+ const needsMinionJobsTimeoutAt = probe.minion_jobs_exists && !probe.minion_jobs_timeout_at_exists;+ const needsMinionJobsIdempotencyKey = probe.minion_jobs_exists && !probe.minion_jobs_idempotency_key_exists;// Fresh installs (no tables yet) and modern brains both no-op.if (!needsPagesBootstrap && !needsLinksBootstrap && !needsChunksBootstrap@@ -886,7 +901,8 @@ export class PGLiteEngine implements BrainEngine {&& !needsContextualRetrievalColumns && !needsPagesGeneration&& !needsPagesEmbeddingSignature&& !needsPagesLinksExtractedAt- && !needsTimelineEventPageId) return;+ && !needsTimelineEventPageId+ && !needsMinionJobsTimeoutAt && !needsMinionJobsIdempotencyKey) return;process.stderr.write(' Pre-v0.21 brain detected, applying forward-reference bootstrap\n');@@ -1141,6 +1157,20 @@ export class PGLiteEngine implements BrainEngine {ALTER TABLE timeline_entries ADD COLUMN IF NOT EXISTS event_page_id INTEGER;`);}++ if (needsMinionJobsTimeoutAt) {+ // v7: blob index idx_minion_jobs_timeout references timeout_at; a+ // pre-v7 minion_jobs wedges blob replay without it (same class as v121).+ await this.db.exec(`+ ALTER TABLE minion_jobs ADD COLUMN IF NOT EXISTS timeout_at TIMESTAMPTZ;+ `);+ }+ if (needsMinionJobsIdempotencyKey) {+ // v7: blob index uniq_minion_jobs_idempotency references idempotency_key.+ await this.db.exec(`+ ALTER TABLE minion_jobs ADD COLUMN IF NOT EXISTS idempotency_key TEXT;+ `);+ }}async withReservedConnection<T>(fn: (conn: ReservedConnection) => Promise<T>): Promise<T> {@@ -2664,8 +2694,14 @@ export class PGLiteEngine implements BrainEngine {async getChunks(slug: string, opts?: { sourceId?: string; sourceIds?: string[] }): Promise<Chunk[]> {const sourceIds = opts?.sourceIds && opts.sourceIds.length > 0 ? opts.sourceIds : undefined;const source = sourceIds ?? opts?.sourceId ?? 'default';+ // #2544: explicit non-vector column list — rowToChunk discards embeddings+ // at this call site, so `cc.*` shipped every vector only to be thrown away.const { rows } = await this.db.query(- `SELECT cc.* FROM content_chunks cc+ `SELECT cc.id, cc.page_id, cc.chunk_index, cc.chunk_text, cc.chunk_source,+ cc.model, cc.token_count, cc.embedded_at, cc.language,+ cc.symbol_name, cc.symbol_type, cc.start_line, cc.end_line,+ cc.parent_symbol_path, cc.doc_comment, cc.symbol_name_qualified, cc.modality+ FROM content_chunks ccJOIN pages p ON p.id = cc.page_idWHERE p.slug = $1 AND ${sourceIds ? 'p.source_id = ANY($2::text[])' : 'p.source_id = $2'}ORDER BY cc.chunk_index`,diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.tsindex c56b6a18b0..e09cce9481 100644--- a/src/core/postgres-engine.ts+++ b/src/core/postgres-engine.ts@@ -624,7 +624,13 @@ export class PostgresEngine implements BrainEngine {EXISTS (SELECT 1 FROM information_schema.tablesWHERE table_schema = current_schema() AND table_name = 'timeline_entries') AS timeline_entries_exists,EXISTS (SELECT 1 FROM information_schema.columns- WHERE table_schema = current_schema() AND table_name = 'timeline_entries' AND column_name = 'event_page_id') AS timeline_event_page_id_exists+ WHERE table_schema = current_schema() AND table_name = 'timeline_entries' AND column_name = 'event_page_id') AS timeline_event_page_id_exists,+ EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = current_schema() AND table_name = 'minion_jobs') AS minion_jobs_exists,+ EXISTS (SELECT 1 FROM information_schema.columns+ WHERE table_schema = current_schema() AND table_name = 'minion_jobs' AND column_name = 'timeout_at') AS minion_jobs_timeout_at_exists,+ EXISTS (SELECT 1 FROM information_schema.columns+ WHERE table_schema = current_schema() AND table_name = 'minion_jobs' AND column_name = 'idempotency_key') AS minion_jobs_idempotency_key_exists`;const probe = probeRows[0]!;@@ -703,6 +709,9 @@ export class PostgresEngine implements BrainEngine {pages_links_extracted_at_exists?: boolean;timeline_entries_exists?: boolean;timeline_event_page_id_exists?: boolean;+ minion_jobs_exists?: boolean;+ minion_jobs_timeout_at_exists?: boolean;+ minion_jobs_idempotency_key_exists?: boolean;};const needsContextualRetrievalColumns = (probe.pages_exists&& (!probeCr.pages_cr_mode_exists || !probeCr.pages_corpus_generation_exists))@@ -725,6 +734,14 @@ export class PostgresEngine implements BrainEngine {// v121: schema-blob indexes reference event_page_id before migrations run.const needsTimelineEventPageId = probeCr.timeline_entries_exists === true&& !probeCr.timeline_event_page_id_exists;+ // v7-era (#2626 class sweep): minion_jobs.timeout_at + idempotency_key are+ // migration-added AND referenced by blob indexes (idx_minion_jobs_timeout,+ // uniq_minion_jobs_idempotency) — a pre-v7 minion_jobs wedges blob replay+ // exactly like the v121 incident.+ const needsMinionJobsTimeoutAt = probeCr.minion_jobs_exists === true+ && !probeCr.minion_jobs_timeout_at_exists;+ const needsMinionJobsIdempotencyKey = probeCr.minion_jobs_exists === true+ && !probeCr.minion_jobs_idempotency_key_exists;if (!needsPagesBootstrap && !needsLinksBootstrap && !needsChunksBootstrap&& !needsPagesDeletedAt && !needsMcpLogBootstrap && !needsSubagentProviderId@@ -736,7 +753,8 @@ export class PostgresEngine implements BrainEngine {&& !needsContextualRetrievalColumns && !needsPagesGeneration&& !needsPagesEmbeddingSignature&& !needsPagesLinksExtractedAt- && !needsTimelineEventPageId) return;+ && !needsTimelineEventPageId+ && !needsMinionJobsTimeoutAt && !needsMinionJobsIdempotencyKey) return;process.stderr.write(' Pre-v0.21 brain detected, applying forward-reference bootstrap\n');@@ -991,6 +1009,20 @@ export class PostgresEngine implements BrainEngine {ALTER TABLE timeline_entries ADD COLUMN IF NOT EXISTS event_page_id INTEGER;`);}++ if (needsMinionJobsTimeoutAt) {+ // v7: blob index idx_minion_jobs_timeout references timeout_at; a+ // pre-v7 minion_jobs wedges blob replay without it (same class as v121).+ await conn.unsafe(`+ ALTER TABLE minion_jobs ADD COLUMN IF NOT EXISTS timeout_at TIMESTAMPTZ;+ `);+ }+ if (needsMinionJobsIdempotencyKey) {+ // v7: blob index uniq_minion_jobs_idempotency references idempotency_key.+ await conn.unsafe(`+ ALTER TABLE minion_jobs ADD COLUMN IF NOT EXISTS idempotency_key TEXT;+ `);+ }}async transaction<T>(fn: (engine: BrainEngine) => Promise<T>): Promise<T> {@@ -2613,8 +2645,15 @@ export class PostgresEngine implements BrainEngine {const scope = sourceIds? tx`p.source_id = ANY(${sourceIds}::text[])`: tx`p.source_id = ${scalarSourceId}`;+ // #2544: explicit non-vector column list — rowToChunk discards+ // embeddings at this call site (includeEmbedding defaults false), so+ // `cc.*` shipped every vector over the wire only to be thrown away.const rows = await tx`- SELECT cc.* FROM content_chunks cc+ SELECT cc.id, cc.page_id, cc.chunk_index, cc.chunk_text, cc.chunk_source,+ cc.model, cc.token_count, cc.embedded_at, cc.language,+ cc.symbol_name, cc.symbol_type, cc.start_line, cc.end_line,+ cc.parent_symbol_path, cc.doc_comment, cc.symbol_name_qualified, cc.modality+ FROM content_chunks ccJOIN pages p ON p.id = cc.page_idWHERE p.slug = ${slug} AND ${scope}ORDER BY cc.chunk_indexdiff --git a/src/core/source-id.ts b/src/core/source-id.tsindex e9a1c414f1..f270e19f4f 100644--- a/src/core/source-id.ts+++ b/src/core/source-id.ts@@ -63,3 +63,43 @@ export function assertValidSourceId(s: unknown): asserts s is string {);}}++/**+ * Normalize the optional `source` field of an `/admin/api/register-client`+ * request body into a write source_id.+ *+ * Mirrors the CLI's `--source` flag. Returns the literal `'default'` when the+ * field is omitted (`undefined`/`null`) so every caller that doesn't send+ * `source` keeps landing on source_id='default' (the pre-source HTTP+ * register-client behavior). A present-but-invalid value throws (via+ * `assertValidSourceId`) so the route can surface a structured 400 instead of+ * failing at INSERT time.+ */+export function normalizeSourceInput(raw: unknown): string {+ if (raw === undefined || raw === null) return 'default';+ assertValidSourceId(raw);+ return raw;+}++/**+ * Normalize the optional `federatedRead` field of an+ * `/admin/api/register-client` request body into a source_id array, or+ * `undefined` when omitted.+ *+ * Mirrors the CLI's `--federated-read` flag. `undefined`/`null` → `undefined`+ * so `registerClientManual` applies its own default (`[sourceId]`, a+ * non-federated client whose read scope equals its write scope). A present+ * value must be a non-empty array whose every element is a valid source_id;+ * anything else throws for a structured 400.+ */+export function normalizeFederatedReadInput(raw: unknown): string[] | undefined {+ if (raw === undefined || raw === null) return undefined;+ if (!Array.isArray(raw) || raw.length === 0) {+ throw new Error(+ `Invalid federatedRead: ${JSON.stringify(raw)}. ` ++ `Must be a non-empty array of source_ids.`,+ );+ }+ for (const s of raw) assertValidSourceId(s);+ return raw as string[];+}diff --git a/test/bootstrap.test.ts b/test/bootstrap.test.tsindex 0b7679fc95..fa308cdc1d 100644--- a/test/bootstrap.test.ts+++ b/test/bootstrap.test.ts@@ -253,4 +253,67 @@ describe('PGLiteEngine#applyForwardReferenceBootstrap', () => {await engine.disconnect();}}, 30000);++ test('wedged-brain recovery: a brain that already FAILED the v0.42.56 upgrade converges on retry', async () => {+ // The loudest #2626-class cohort: operators who upgraded, wedged, and are+ // retrying with a fixed binary. Simulates the failed attempt (the blob's+ // CREATE INDEX crashing on the missing column) and asserts the retry+ // converges to the FULL final shape (column + FK + both partial indexes)+ // with no residue — the failed attempt must not advance the version ledger.+ const engine = new PGLiteEngine();+ await engine.connect({});+ try {+ await engine.initSchema();+ const db = (engine as any).db;++ // Rewind to the pre-v121 shape: schema AND the version counter.+ await db.exec(`+ DROP INDEX IF EXISTS idx_timeline_event_page;+ DROP INDEX IF EXISTS idx_timeline_event_dedup;+ ALTER TABLE timeline_entries DROP CONSTRAINT IF EXISTS timeline_entries_event_page_id_fkey;+ ALTER TABLE timeline_entries DROP COLUMN IF EXISTS event_page_id;+ `);+ await engine.setConfig('version', '120');++ // The failed old-binary attempt: without the bootstrap probe, the blob's+ // CREATE INDEX was the first statement to touch the missing column.+ let wedgeError: Error | null = null;+ try {+ await db.exec(+ `CREATE INDEX IF NOT EXISTS idx_timeline_event_page+ ON timeline_entries(event_page_id) WHERE event_page_id IS NOT NULL`,+ );+ } catch (e) {+ wedgeError = e as Error;+ }+ expect(wedgeError?.message ?? '').toContain('event_page_id');++ // The failed attempt must not have advanced the ledger.+ expect(parseInt((await engine.getConfig('version')) || '1', 10)).toBe(120);++ // Retry with the fixed binary: full initSchema converges to LATEST with+ // the complete final shape.+ await engine.initSchema();+ expect(await engine.getConfig('version')).toBe(String(LATEST_VERSION));+ const { rows: col } = await db.query(`+ SELECT column_name FROM information_schema.columns+ WHERE table_name = 'timeline_entries' AND column_name = 'event_page_id'+ `);+ expect(col).toHaveLength(1);+ const { rows: fk } = await db.query(`+ SELECT conname FROM pg_constraint+ WHERE conname = 'timeline_entries_event_page_id_fkey'+ `);+ expect(fk).toHaveLength(1);+ const { rows: idx } = await db.query(`+ SELECT indexname FROM pg_indexes+ WHERE tablename = 'timeline_entries'+ AND indexname IN ('idx_timeline_event_page', 'idx_timeline_event_dedup')+ `);+ expect(idx).toHaveLength(2);+ } finally {+ await engine.disconnect();+ }+ }, 30000);+});diff --git a/test/cli-flag-validation.test.ts b/test/cli-flag-validation.test.tsnew file mode 100644index 0000000000..f7800f2986--- /dev/null+++ b/test/cli-flag-validation.test.ts@@ -0,0 +1,273 @@+/**+ * #2185 — strict unknown-flag validation.+ *+ * The repro that filed the issue: `gbrain init --migrate-only --dry-run`+ * applied REAL migrations — no handler consults --dry-run, and the ad-hoc+ * `args.includes()` flag style silently ignores anything it doesn't look for.+ * The pre-dispatch validator in src/cli.ts fails loud instead.+ *+ * Four guard classes:+ * 1. sweep — every CLI_ONLY command (minus documented exemptions) and every+ * op command rejects a nonsense flag via the pure validator.+ * 2. acceptance — real flags and passthrough forms stay accepted.+ * 3. drift — every CLI_ONLY member has a registry entry.+ * 4. freshness — the committed generated registry matches a fresh+ * generator run (same doctrine as the llms-bundle freshness test).+ * Plus subprocess smokes for the end-to-end error surface.+ */+import { describe, test, expect } from 'bun:test';+import { spawnSync } from 'child_process';+import {+ validateCommandFlags,+ findUnknownFlag,+ findUnknownOpFlag,+ CLI_ONLY,+} from '../src/cli.ts';+import { CLI_FLAG_REGISTRY } from '../src/core/cli-flag-registry.generated.ts';+import { operations, operationsByName } from '../src/core/operations.ts';+import { buildFlagRegistry } from '../scripts/generate-flag-registry.ts';++const BOGUS = '--definitely-not-a-real-flag-xyz';+const EXEMPT = new Set(['call', 'config']); // jobs is exempt only for `submit`++describe('#2185 sweep — every command rejects a nonsense flag', () => {+ test('every CLI_ONLY command rejects the bogus flag (validator lane)', () => {+ const accepted: string[] = [];+ for (const command of CLI_ONLY) {+ if (EXEMPT.has(command)) continue;+ const verdict = validateCommandFlags(command, [BOGUS]);+ if (verdict !== BOGUS) accepted.push(command);+ }+ expect(accepted).toEqual([]);+ });++ test('every op command rejects the bogus flag (op lane)', () => {+ const accepted: string[] = [];+ for (const op of operations) {+ if (!op.cliHints) continue;+ const verdict = findUnknownOpFlag(op, [BOGUS]);+ if (verdict !== BOGUS) accepted.push(op.name);+ }+ expect(accepted).toEqual([]);+ });++ test('the literal #2185 repro is rejected: init --migrate-only --dry-run', () => {+ expect(validateCommandFlags('init', ['--migrate-only', '--dry-run'])).toBe('--dry-run');+ // And --migrate-only alone stays legal.+ expect(validateCommandFlags('init', ['--migrate-only'])).toBeNull();+ });+});++describe('#2185 acceptance — real usage stays legal', () => {+ test('op flags from the contract are accepted, including values starting with --', () => {+ const search = operationsByName.search;+ expect(findUnknownOpFlag(search, ['needle', '--limit', '5'])).toBeNull();+ // A VALUE that begins with -- is consumed as the value, not validated.+ expect(findUnknownOpFlag(search, ['--query', '--weird-looking-value'])).toBeNull();+ // Inline = form.+ expect(findUnknownOpFlag(search, ['needle', '--limit=5'])).toBeNull();+ // CLI-local formatter flags.+ expect(findUnknownOpFlag(search, ['needle', '--json', '--explain'])).toBeNull();+ });++ test('--no-<flag> negation of a known boolean op param is legal', () => {+ const withBool = operations.find(o =>+ o.cliHints && Object.values(o.params).some(p => p.type === 'boolean'));+ expect(withBool).toBeDefined();+ const boolKey = Object.entries(withBool!.params).find(([, p]) => p.type === 'boolean')![0];+ const flag = `--no-${boolKey.replace(/_/g, '-')}`;+ expect(findUnknownOpFlag(withBool!, [flag])).toBeNull();+ });++ test('everything after a literal -- is passthrough, never validated', () => {+ expect(findUnknownFlag(['--', BOGUS], new Set(['--help']))).toBeNull();+ expect(validateCommandFlags('agent', ['run', '--', BOGUS])).toBeNull();+ expect(findUnknownOpFlag(operationsByName.search, ['--', BOGUS])).toBeNull();+ });++ test('exempt commands accept arbitrary flags by contract', () => {+ expect(validateCommandFlags('call', ['some_op', BOGUS])).toBeNull();+ expect(validateCommandFlags('config', ['set', 'k', BOGUS])).toBeNull();+ expect(validateCommandFlags('jobs', ['submit', 'shell', BOGUS])).toBeNull();+ // ...but non-submit jobs subcommands are validated.+ expect(validateCommandFlags('jobs', ['list', BOGUS])).toBe(BOGUS);+ });++ test('registry-listed CLI_ONLY flags are accepted', () => {+ expect(validateCommandFlags('serve', ['--http', '--port', '4444'])).toBeNull();+ expect(validateCommandFlags('serve', ['--print-admin-token'])).toBeNull();+ expect(validateCommandFlags('embed', ['--stale', '--pace'])).toBeNull();+ expect(validateCommandFlags('sync', ['--full'])).toBeNull();+ });++ // Pre-landing review regression: the validator rejected the CLI-local+ // flags makeContext consumes OUTSIDE the op contract — `gbrain search+ // "x" --source y` and `--dry-run` invocations exited 1 as unknown flags.+ test('op commands accept --source/--dry-run (makeContext CLI-locals, not wire params)', () => {+ const search = operationsByName.search;+ expect(findUnknownOpFlag(search, ['needle', '--source', 'yc-media'])).toBeNull();+ expect(findUnknownOpFlag(search, ['needle', '--source=yc-media'])).toBeNull();+ // --source's VALUE is consumed, never validated as a flag itself.+ expect(findUnknownOpFlag(search, ['--source', '--weird-value', 'needle'])).toBeNull();+ expect(findUnknownOpFlag(search, ['needle', '--dry-run'])).toBeNull();+ // Still strict right next to them.+ expect(findUnknownOpFlag(search, ['needle', '--source', 'y', BOGUS])).toBe(BOGUS);+ });++ test('--json=<v> is coherent between validator and parser (no positional corruption)', async () => {+ const { parseOpArgs } = await import('../src/cli.ts');+ const search = operationsByName.search;+ // Validator accepts any --json form.+ expect(findUnknownOpFlag(search, ['--json=true', 'needle'])).toBeNull();+ // Parser sets the boolean and PRESERVES the positional (pre-fix the+ // junk-key path consumed 'needle' as the value of key 'json=true').+ const p = parseOpArgs(search, ['--json=true', 'needle']);+ expect(p.json).toBe(true);+ expect(p.query).toBe('needle');+ expect((p as Record<string, unknown>)['json=true']).toBeUndefined();+ expect(parseOpArgs(search, ['needle', '--json=false']).json).toBe(false);+ // = forms of bare-only CLI-locals reject loud instead of silently eating tokens.+ expect(findUnknownOpFlag(search, ['--explain=verbose'])).toBe('--explain');+ });+});++describe('#2185 parseOpArgs inline = form (regression rule: changed token consumption)', () => {+ test('string and number params via =, positional untouched', async () => {+ const { parseOpArgs } = await import('../src/cli.ts');+ const search = operationsByName.search;+ const p = parseOpArgs(search, ['needle', '--limit=5']);+ expect(p.query).toBe('needle');+ expect(p.limit).toBe(5);+ });++ test('boolean =false negates; =0 keeps the raw!==false rule (pinned semantics)', async () => {+ const { parseOpArgs } = await import('../src/cli.ts');+ const withBool = operations.find(o =>+ o.cliHints && Object.values(o.params).some(pp => pp.type === 'boolean'))!;+ const key = Object.entries(withBool.params).find(([, pp]) => pp.type === 'boolean')![0];+ const flag = `--${key.replace(/_/g, '-')}`;+ expect(parseOpArgs(withBool, [`${flag}=false`])[key]).toBe(false);+ expect(parseOpArgs(withBool, [`${flag}=0`])[key]).toBe(true);+ });++ test('undeclared =-form key keeps the historical junk-fallthrough (validator rejects it first)', async () => {+ const { parseOpArgs } = await import('../src/cli.ts');+ const search = operationsByName.search;+ // The validator is the strict gate; the parser's legacy behavior for+ // undeclared keys is unchanged — pinned so a refactor can't silently+ // change what unvalidated callers (tests, internal) see.+ expect(findUnknownOpFlag(search, ['--not-a-param=x', 'needle'])).toBe('--not-a-param');+ const p = parseOpArgs(search, ['--not-a-param=x', 'needle']);+ expect(p.query).toBeUndefined(); // consumed by the junk key — validator prevents reaching here+ });+});++describe('#2185 red-team regressions', () => {+ test('dual-lane commands (op AND CLI_ONLY) validate against the DISPATCHED lane', () => {+ // think/salience/anomalies are both ops and CLI_ONLY members; dispatch+ // runs handleCliOnly, whose handlers parse flags the op contract never+ // declares. Pre-fix the validator checked the op lane first and rejected+ // documented invocations.+ expect(validateCommandFlags('salience', ['--kind', 'entity'])).toBeNull();+ expect(validateCommandFlags('think', ['what changed?', '--with-calibration'])).toBeNull();+ expect(validateCommandFlags('salience', [BOGUS])).toBe(BOGUS);+ });++ test('--dry-run is a real CLI-local boolean on op commands (trailing position sets it)', async () => {+ const { parseOpArgs } = await import('../src/cli.ts');+ // An op WITHOUT a declared dry_run param — pre-fix, trailing --dry-run+ // set NOTHING (ctx.dryRun stayed false → the REAL destructive action+ // ran despite the rehearsal request), and leading --dry-run consumed+ // the next token as its value.+ const search = operationsByName.search;+ expect(parseOpArgs(search, ['needle', '--dry-run']).dry_run).toBe(true);+ const leading = parseOpArgs(search, ['--dry-run', 'needle']);+ expect(leading.dry_run).toBe(true);+ expect(leading.query).toBe('needle');+ expect(parseOpArgs(search, ['needle', '--dry-run=false']).dry_run).toBe(false);+ });++ test('uppercase flag typos reject loudly in both lanes (handlers are case-sensitive)', () => {+ expect(findUnknownFlag(['--MIGRATE-ONLY'], new Set(['--migrate-only']))).toBe('--MIGRATE-ONLY');+ expect(findUnknownOpFlag(operationsByName.search, ['--LIMIT', '5'])).toBe('--LIMIT');+ });++ test('safety flags need consumption evidence, not prose bleed (codex P1-A)', () => {+ // upgrade.ts prints a help HINT naming another command's --dry-run; that+ // literal sits at depth 0 for post-upgrade. Pre-fix the generator+ // allowlisted it, recreating the exact #2185 repro the wave exists to+ // kill: `post-upgrade --dry-run` accepted, ignored, migrations run for+ // real. The generator now requires a tight-quoted standalone literal+ // (an args read like has('--dry-run')) before granting a safety flag.+ expect(CLI_FLAG_REGISTRY['post-upgrade']).not.toContain('--dry-run');+ // backfill genuinely consumes it (has('--dry-run') in backfill.ts) —+ // the gate must not strip real consumers.+ expect(CLI_FLAG_REGISTRY['backfill']).toContain('--dry-run');+ });+});++describe('#2185 drift + freshness guards', () => {+ test('every CLI_ONLY member has a registry entry (drift guard)', () => {+ const missing = [...CLI_ONLY].filter(c => !CLI_FLAG_REGISTRY[c]);+ expect(missing).toEqual([]);+ });++ test('committed registry matches a fresh generator run (freshness guard)', () => {+ const fresh = buildFlagRegistry();+ const freshKeys = Object.keys(fresh).sort();+ const committedKeys = Object.keys(CLI_FLAG_REGISTRY).sort();+ expect(committedKeys).toEqual(freshKeys);+ const stale = freshKeys.filter(+ key => JSON.stringify([...CLI_FLAG_REGISTRY[key]]) !== JSON.stringify(fresh[key]),+ );+ // Any listed command means: run `bun run build:flag-registry` and commit.+ expect(stale).toEqual([]);+ });+});++describe('#2185 subprocess smokes — end-to-end error surface', () => {+ const run = (args: string[]) =>+ spawnSync('bun', ['src/cli.ts', ...args], {+ encoding: 'utf-8',+ timeout: 30_000,+ env: { ...process.env, GBRAIN_SKIP_STARTUP_HOOKS: '1' },+ });++ test('init --migrate-only --dry-run fails loud BEFORE any engine work', () => {+ const r = run(['init', '--migrate-only', '--dry-run']);+ expect(r.status).toBe(1);+ expect(r.stderr).toContain("unknown flag --dry-run for 'gbrain init'");+ // Pre-engine: no migration output may appear.+ expect(r.stderr).not.toContain('migration');+ });++ test('typo on an op command fails loud with the command named', () => {+ const r = run(['search', 'needle', '--jsno']);+ expect(r.status).toBe(1);+ expect(r.stderr).toContain("unknown flag --jsno for 'gbrain search'");+ });++ test('--help still short-circuits before validation', () => {+ const r = run(['init', '--help']);+ expect(r.status).toBe(0);+ expect(r.stderr).not.toContain('unknown flag');+ });++ test('global flags are accepted on every command (stripped pre-dispatch)', () => {+ // --quiet is a parseGlobalFlags global: it never reaches the validator.+ // The bogus flag proves validation still ran on what remained.+ const r = run(['init', '--migrate-only', '--quiet', '--definitely-bogus-xyz']);+ expect(r.status).toBe(1);+ expect(r.stderr).toContain("unknown flag --definitely-bogus-xyz for 'gbrain init'");+ expect(r.stderr).not.toContain('--quiet');+ });++ test('op command accepts --source end-to-end (the makeContext CLI-local regression)', () => {+ // Fast path: --help short-circuits AFTER global parse, so a bogus flag+ // alongside --source proves ordering: --source accepted, bogus rejected.+ const r = run(['search', 'needle', '--source', 'nope-source', '--definitely-bogus-xyz']);+ expect(r.status).toBe(1);+ expect(r.stderr).toContain("unknown flag --definitely-bogus-xyz for 'gbrain search'");+ expect(r.stderr).not.toContain('unknown flag --source');+ });+});diff --git a/test/cli-format-search-json.test.ts b/test/cli-format-search-json.test.tsnew file mode 100644index 0000000000..0bdbf88e26--- /dev/null+++ b/test/cli-format-search-json.test.ts@@ -0,0 +1,28 @@+import { describe, expect, test } from 'bun:test';+import { formatResult } from '../src/cli.ts';++describe('formatResult - search/query --json', () => {+ test('search --json renders the raw result array as parseable JSON', () => {+ const out = formatResult('search', [+ {+ slug: 'docs/example',+ score: 0.42,+ chunk_text: 'Example result text',+ },+ ], { json: true });++ expect(JSON.parse(out)).toEqual([+ {+ slug: 'docs/example',+ score: 0.42,+ chunk_text: 'Example result text',+ },+ ]);+ });++ test('query --json keeps empty results machine-readable', () => {+ const out = formatResult('query', [], { json: true });++ expect(JSON.parse(out)).toEqual([]);+ });+});diff --git a/test/cli.test.ts b/test/cli.test.tsindex feafbd71da..06d89da664 100644--- a/test/cli.test.ts+++ b/test/cli.test.ts@@ -49,6 +49,91 @@ describe('CLI structure', () => {test('has formatResult function for CLI output', () => {expect(cliSource).toContain('function formatResult');});++ // #2035-class dispatch-gap guard: every `case '...'` label inside+ // handleCliOnly's top-level dispatch must be a member of CLI_ONLY, else the+ // command is registered but unreachable — 'calibration' shipped exactly this+ // way. Structural, self-updating: a new case without a CLI_ONLY entry fails+ // here at PR time.+ test('every handleCliOnly top-level case label is reachable via CLI_ONLY', () => {+ const onlyMatch = cliSource.match(/const CLI_ONLY = new Set(?:<string>)?\(\[([\s\S]*?)\]\)/);+ expect(onlyMatch).not.toBeNull();+ // Strip line comments before member extraction — the set literal carries+ // commentary whose quoted words must not count as members.+ const onlyBody = onlyMatch![1].replace(/\/\/[^\n]*/g, '');+ const members = new Set([...onlyBody.matchAll(/'([^']+)'/g)].map(m => m[1]));++ const fnStart = cliSource.indexOf('async function handleCliOnly');+ expect(fnStart).toBeGreaterThan(0);+ const fnSrc = cliSource.slice(fnStart);+ // Top-level dispatch labels sit at a fixed indent (6 spaces); nested+ // sub-switches are indented deeper and stay out of this scan.+ const caseLabels = [...fnSrc.matchAll(/^ case '([a-z0-9-]+)':/gm)].map(m => m[1]);+ expect(caseLabels.length).toBeGreaterThan(20);+ // Reachable outside CLI_ONLY, each with a documented route:+ // - 'search': pre-dispatch subcommand gate (modes|stats|tune) in main();+ // the bare command must keep routing to the `search` op for queries.+ // - 'whoknows': currently routes via the find_experts op alias; its+ // handleCliOnly case is dead (adding it to CLI_ONLY would trip the+ // alias-collision guard and silently change output). Tracked follow-up+ // alongside PR #2509 (whoknows --explain).+ const REACHABLE_VIA_OTHER_ROUTE = new Set(['search', 'whoknows']);+ const missing = caseLabels.filter(+ label => !members.has(label) && !REACHABLE_VIA_OTHER_ROUTE.has(label),+ );+ expect(missing).toEqual([]);+ // The search gate itself must exist — losing it re-deadens the dashboards.+ // (master's gate is a superset: modes|stats|tune|diagnose.)+ expect(cliSource).toMatch(/\['modes', 'stats', 'tune'(?:, 'diagnose')?\]\.includes\(subArgs\[0\] \?\? ''\)/);+ });+});++// #2450 — the local-engine output normalizer used a bare JSON.stringify with+// no replacer. A bigint anywhere in an op's return value (e.g. a BIGSERIAL+// primary key read back by the Postgres engine) made JSON.stringify THROW+// "Do not know how to serialize a BigInt", crashing the command before any+// renderer ran. normalizeLocalResult stringifies via bigintToStringReplacer+// (bigint → string, postgres.js wire shape).+describe('BigInt-safe output normalization (#2450)', () => {+ test('bare JSON.stringify throws on a bigint (the pre-fix crash)', () => {+ expect(() => JSON.stringify({ id: 9999999999999999999n })).toThrow(+ /serialize BigInt|serialize a BigInt/i,+ );+ });++ test('normalizeLocalResult serializes bigint → string without throwing', async () => {+ const { normalizeLocalResult } = await import('../src/cli.ts');+ const out = normalizeLocalResult({+ id: 42n,+ nested: { count: 7n },+ arr: [1n, 2n],+ str: 'unchanged',+ num: 3,+ }) as Record<string, unknown>;+ expect(out.id).toBe('42');+ expect((out.nested as Record<string, unknown>).count).toBe('7');+ expect(out.arr).toEqual(['1', '2']);+ expect(out.str).toBe('unchanged');+ expect(out.num).toBe(3);+ });++ test('bigint past Number.MAX_SAFE_INTEGER keeps full precision as a string', async () => {+ const { normalizeLocalResult } = await import('../src/cli.ts');+ const big = 9007199254740993n; // MAX_SAFE_INTEGER + 2+ const out = normalizeLocalResult({ id: big }) as Record<string, unknown>;+ expect(out.id).toBe('9007199254740993');+ });++ test("formatResult's default renderer is bigint-safe", async () => {+ const { formatResult } = await import('../src/cli.ts');+ expect(() => formatResult('__no_such_op__', { id: 5n })).not.toThrow();+ expect(formatResult('__no_such_op__', { id: 5n })).toContain('"5"');+ });++ test('cli.ts no longer uses a replacer-less stringify on the normalize path', () => {+ expect(cliSource).toContain('normalizeLocalResult(rawResult)');+ expect(cliSource).not.toContain('JSON.parse(JSON.stringify(rawResult))');+ });});describe('CLI version', () => {diff --git a/test/doctor.test.ts b/test/doctor.test.tsindex e7614e8a5c..f9fa45380f 100644--- a/test/doctor.test.ts+++ b/test/doctor.test.ts@@ -222,6 +222,58 @@ describe('doctor command', () => {}});+ test('jsonb_integrity: flags double-encoded subagent payloads, ignores legitimate string scalars, skips absent tables', async () => {+ const { PGLiteEngine } = await import('../src/core/pglite-engine.ts');+ const { jsonbIntegrityCheck } = await import('../src/commands/doctor.ts');+ const engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+ try {+ // Seed a minion job to satisfy the FK, then two subagent rows:+ // one DOUBLE-ENCODED (string scalar whose content is a JSON array —+ // the pre-#2375 damage class) and one LEGITIMATE string scalar+ // (persistToolExec binds pre-serialized string payloads as-is).+ await engine.executeRaw(+ `INSERT INTO minion_jobs (id, name, data, status) VALUES (990001, 'doctor-jsonb-test', '{}'::jsonb, 'completed')`,+ );+ await engine.executeRaw(+ `INSERT INTO subagent_messages (job_id, message_idx, role, content_blocks)+ VALUES (990001, 0, 'assistant', to_jsonb('[{"type":"text"}]'::text))`,+ );+ await engine.executeRaw(+ `INSERT INTO subagent_messages (job_id, message_idx, role, content_blocks)+ VALUES (990001, 1, 'assistant', to_jsonb('plain text payload, not JSON'::text))`,+ );+ // Container-LOOKING but invalid JSON — matches the shape probe but+ // pg_input_is_valid must exclude it (repairing it would throw).+ await engine.executeRaw(+ `INSERT INTO subagent_messages (job_id, message_idx, role, content_blocks)+ VALUES (990001, 2, 'assistant', to_jsonb('[INFO] fetch complete'::text))`,+ );++ const damaged = await jsonbIntegrityCheck(engine);+ expect(damaged.status).toBe('warn');+ // Exactly the double-encoded row counts — the legit string scalar doesn't.+ expect(damaged.message).toContain('subagent_messages.content_blocks=1');++ // Cleanup, then prove the ok path again.+ await engine.executeRaw(`DELETE FROM subagent_messages WHERE job_id = 990001`);+ await engine.executeRaw(`DELETE FROM minion_jobs WHERE id = 990001`);+ expect((await jsonbIntegrityCheck(engine)).status).toBe('ok');++ // Absent-table skip lane: rename a target table; the check must skip+ // it without throwing (pre-v0.15 brains lack subagent_* entirely).+ await engine.executeRaw(`ALTER TABLE subagent_tool_executions RENAME TO subagent_tool_executions_bak`);+ try {+ expect((await jsonbIntegrityCheck(engine)).status).toBe('ok');+ } finally {+ await engine.executeRaw(`ALTER TABLE subagent_tool_executions_bak RENAME TO subagent_tool_executions`);+ }+ } finally {+ await engine.disconnect();+ }+ });+test('skill conformance derives a valid host manifest when manifest.json is absent', async () => {const { skillConformanceCheck } = await import('../src/commands/doctor.ts');const skillsDir = join(tmpdir(), `gbrain-doctor-skills-${crypto.randomUUID()}`);diff --git a/test/e2e/engine-parity.test.ts b/test/e2e/engine-parity.test.tsindex 568f742d45..73fbfee903 100644--- a/test/e2e/engine-parity.test.ts+++ b/test/e2e/engine-parity.test.ts@@ -528,6 +528,38 @@ describeBoth('Engine parity — Postgres vs PGLite', () => {}});+ test('#2555 getChunks sourceIds[] parity: federated grant + scalar floor + unset default identical on both engines', async () => {+ for (const eng of [pgEngine, pgliteEngine]) {+ await eng.executeRaw(`INSERT INTO sources (id, name, local_path) VALUES ('gcp-beta', 'gcp-beta', '/tmp/gcp-beta') ON CONFLICT (id) DO NOTHING`);+ await eng.putPage('wiki/gcp-doc', {+ type: 'note', title: 'beta doc', compiled_truth: 'beta body', timeline: '',+ }, { sourceId: 'gcp-beta' });+ await eng.upsertChunks('wiki/gcp-doc', [+ { chunk_index: 0, chunk_text: 'gcp beta chunk', chunk_source: 'compiled_truth' },+ ], { sourceId: 'gcp-beta' });+ await eng.putPage('wiki/gcp-doc', {+ type: 'note', title: 'default decoy', compiled_truth: 'decoy body', timeline: '',+ }, { sourceId: 'default' });+ await eng.upsertChunks('wiki/gcp-doc', [+ { chunk_index: 0, chunk_text: 'gcp default decoy', chunk_source: 'compiled_truth' },+ ], { sourceId: 'default' });+ }++ for (const eng of [pgEngine, pgliteEngine]) {+ // Federated array wins over scalar and reaches the non-default source.+ const federated = await eng.getChunks('wiki/gcp-doc', { sourceId: 'default', sourceIds: ['gcp-beta'] });+ expect(federated.map(c => c.chunk_text)).toEqual(['gcp beta chunk']);+ // Out-of-grant array → empty, never a fall-through to 'default'.+ const outOfGrant = await eng.getChunks('wiki/gcp-doc', { sourceIds: ['gcp-nonexistent'] });+ expect(outOfGrant).toEqual([]);+ // Unset opts keep the historical 'default' floor.+ const unset = await eng.getChunks('wiki/gcp-doc');+ expect(unset.map(c => c.chunk_text)).toEqual(['gcp default decoy']);+ // #2544 trim keeps the Chunk shape (embedding deliberately unselected → null).+ expect(federated[0].embedding).toBeNull();+ }+ });+test('v114 (#1941) listLinkSources parity: same ordered provenance counts on both engines', async () => {const mk = async (eng: BrainEngine) => {for (const s of ['lsp-a', 'lsp-b', 'lsp-c']) {diff --git a/test/e2e/list-all-sources-postgres.test.ts b/test/e2e/list-all-sources-postgres.test.tsindex 0fec4509f7..a0cfa127dc 100644--- a/test/e2e/list-all-sources-postgres.test.ts+++ b/test/e2e/list-all-sources-postgres.test.ts@@ -160,4 +160,34 @@ describeIfDB('Postgres parity — updateSourceConfig', () => {expect(rows[0]?.typeof).toBe('object');expect(rows[0]?.value).toBe('2026-05-22T12:00:00.000Z');});++ test('#2251: mixed-array config (non-object elements) merges instead of throwing, and self-heals to a flat object', async () => {+ await seedSource('mixed');+ // The historical bad shape that permanently blocked last_full_cycle_at+ // writes: a JSONB array holding a non-object element. The bare+ // jsonb_each(elem) threw 'cannot call jsonb_each on a non-object'+ // DURING row production, failing every subsequent updateSourceConfig.+ await engine.executeRaw(+ `UPDATE sources+ SET config = '["stray-string", {"remote_url": "https://kept"}, 42]'::jsonb+ WHERE id = 'mixed'`,+ );++ const ok = await engine.updateSourceConfig('mixed', {+ last_full_cycle_at: '2026-07-09T00:00:00.000Z',+ });+ expect(ok).toBe(true);++ const rows = await engine.executeRaw<{ typeof: string; cycle: string | null; kept: string | null }>(+ `SELECT jsonb_typeof(config) AS typeof,+ config->>'last_full_cycle_at' AS cycle,+ config->>'remote_url' AS kept+ FROM sources WHERE id = 'mixed'`,+ );+ // Self-healed: flat object, patch applied, object elements' keys recovered,+ // non-object stragglers dropped.+ expect(rows[0]?.typeof).toBe('object');+ expect(rows[0]?.cycle).toBe('2026-07-09T00:00:00.000Z');+ expect(rows[0]?.kept).toBe('https://kept');+ });});diff --git a/test/e2e/postgres-bootstrap.test.ts b/test/e2e/postgres-bootstrap.test.tsindex 32b31f32fc..46829885a9 100644--- a/test/e2e/postgres-bootstrap.test.ts+++ b/test/e2e/postgres-bootstrap.test.ts@@ -91,6 +91,70 @@ describe.skipIf(skip)('PostgresEngine forward-reference bootstrap (E2E)', () =>expect(await engine.getConfig('version')).toBe(String(LATEST_VERSION));});+ test('pre-v121 timeline shape converges to full final shape on REAL Postgres (#2626 wedge class)', async () => {+ // The v121 wedge was Postgres-visible in production (blob CREATE INDEX+ // on a column migration v121 hadn't added yet); the PGLite twins live in+ // test/bootstrap.test.ts. Rewind schema AND the version counter to the+ // wedged cohort's true state, then assert full initSchema convergence:+ // column + FK + BOTH partial indexes, ledger at LATEST.+ await engine.initSchema();+ const conn = (engine as any).sql;+ await conn.unsafe(`+ DROP INDEX IF EXISTS idx_timeline_event_dedup;+ DROP INDEX IF EXISTS idx_timeline_event_page;+ ALTER TABLE timeline_entries DROP CONSTRAINT IF EXISTS timeline_entries_event_page_id_fkey;+ ALTER TABLE timeline_entries DROP COLUMN IF EXISTS event_page_id;+ `);+ await engine.setConfig('version', '120');++ await engine.initSchema();++ expect(await engine.getConfig('version')).toBe(String(LATEST_VERSION));+ const col = await conn`+ SELECT column_name FROM information_schema.columns+ WHERE table_schema = current_schema()+ AND table_name = 'timeline_entries' AND column_name = 'event_page_id'+ `;+ expect(col).toHaveLength(1);+ const fk = await conn`+ SELECT conname FROM pg_constraint WHERE conname = 'timeline_entries_event_page_id_fkey'+ `;+ expect(fk).toHaveLength(1);+ const idx = await conn`+ SELECT indexname FROM pg_indexes+ WHERE tablename = 'timeline_entries'+ AND indexname IN ('idx_timeline_event_page', 'idx_timeline_event_dedup')+ `;+ expect(idx).toHaveLength(2);+ }, 60_000);++ test('pre-v7 minion_jobs shape (scanner-sweep wedge class) converges on REAL Postgres', async () => {+ await engine.initSchema();+ const conn = (engine as any).sql;+ await conn.unsafe(`+ DROP INDEX IF EXISTS idx_minion_jobs_timeout;+ DROP INDEX IF EXISTS uniq_minion_jobs_idempotency;+ ALTER TABLE minion_jobs DROP COLUMN IF EXISTS timeout_at;+ ALTER TABLE minion_jobs DROP COLUMN IF EXISTS idempotency_key;+ `);++ await engine.initSchema();++ const cols = await conn`+ SELECT column_name FROM information_schema.columns+ WHERE table_schema = current_schema()+ AND table_name = 'minion_jobs'+ AND column_name IN ('timeout_at', 'idempotency_key')+ `;+ expect(cols).toHaveLength(2);+ const idx = await conn`+ SELECT indexname FROM pg_indexes+ WHERE tablename = 'minion_jobs'+ AND indexname IN ('idx_minion_jobs_timeout', 'uniq_minion_jobs_idempotency')+ `;+ expect(idx).toHaveLength(2);+ }, 60_000);+// Migration v120 — schema-lint hardening (#1647 / #171). Postgres-only// assertions (security_invoker has no surface on embedded PGLite).test('v120: page_links view runs with security_invoker=on (#1647b)', async () => {diff --git a/test/get-page-federated-scope.test.ts b/test/get-page-federated-scope.test.tsindex 6f681fc056..dd3eae1a0a 100644--- a/test/get-page-federated-scope.test.ts+++ b/test/get-page-federated-scope.test.ts@@ -373,3 +373,88 @@ describe('#2200 engine secondary-fetch methods honor sourceIds[]', () => {expect(windowed.map(e => e.summary)).toEqual(['june event']);});});++// ---------------------------------------------------------------------------+// #2555 — get_chunks honors the federated source grant (same class as #1393/+// #2200, chunk read path). Pre-fix the op used the pre-#2200 scalar pattern+// and engine.getChunks had no sourceIds[] support: a federated client that+// could read a page via get_page got [] from get_chunks.+// ---------------------------------------------------------------------------+describe('#2555 get_chunks federated scope', () => {+ const get_chunks = operations.find(o => o.name === 'get_chunks')!;++ beforeEach(async () => {+ await engine.upsertChunks('secret/beta-doc', [+ { chunk_index: 0, chunk_text: 'beta chunk zero', chunk_source: 'compiled_truth' },+ { chunk_index: 1, chunk_text: 'beta chunk one', chunk_source: 'compiled_truth' },+ ], { sourceId: 'beta' });+ // Same-slug decoy chunks in 'default' — the cross-source bleed guard.+ await engine.upsertChunks('secret/beta-doc', [+ { chunk_index: 0, chunk_text: 'default decoy chunk', chunk_source: 'compiled_truth' },+ ], { sourceId: 'default' });+ });++ test('op: federated grant including the page source returns its chunks (the #2555 repro)', async () => {+ const ctx = ctxOf({ remote: true, sourceId: undefined, auth: { token: 't', clientId: 'c', scopes: [], allowedSources: ['alpha', 'beta'] } as any });+ const chunks = await get_chunks.handler(ctx, { slug: 'secret/beta-doc' }) as Array<{ chunk_text: string }>;+ expect(chunks.map(c => c.chunk_text)).toEqual(['beta chunk zero', 'beta chunk one']);+ });++ test('op: grant excluding the page source stays empty — never falls through to default', async () => {+ const ctx = ctxOf({ remote: true, sourceId: undefined, auth: { token: 't', clientId: 'c', scopes: [], allowedSources: ['alpha'] } as any });+ const chunks = await get_chunks.handler(ctx, { slug: 'secret/beta-doc' }) as Array<{ chunk_text: string }>;+ expect(chunks).toEqual([]);+ });++ test('op: no grant + default floor sees only the default decoy, never beta chunks', async () => {+ const ctx = ctxOf({ remote: true, sourceId: 'default', auth: undefined });+ const chunks = await get_chunks.handler(ctx, { slug: 'secret/beta-doc' }) as Array<{ chunk_text: string }>;+ expect(chunks.map(c => c.chunk_text)).toEqual(['default decoy chunk']);+ });++ test('engine: sourceIds[] precedence over scalar; trimmed SELECT keeps the Chunk shape', async () => {+ // array beats scalar: scalar 'default' would return the decoy; array ['beta'] must win.+ const prec = await engine.getChunks('secret/beta-doc', { sourceId: 'default', sourceIds: ['beta'] });+ expect(prec.map(c => c.chunk_text)).toEqual(['beta chunk zero', 'beta chunk one']);+ // #2544 trim: embedding is deliberately not selected (rowToChunk discards+ // it here anyway) and the rest of the Chunk shape survives.+ expect(prec[0].embedding).toBeNull();+ expect(prec[0].chunk_index).toBe(0);+ expect(prec[0].chunk_source).toBe('compiled_truth');+ // Unset opts keep the historical 'default' floor (importCodeFile contract).+ const def = await engine.getChunks('secret/beta-doc');+ expect(def.map(c => c.chunk_text)).toEqual(['default decoy chunk']);+ });++ test('#2544 structural pin: neither engine SELECTs cc.* in getChunks (the trim survives merges)', async () => {+ // The behavioral assertion above is vacuous for the trim itself —+ // rowToChunk hard-nulls embedding regardless of the SELECT. This pin+ // exists because a master merge once silently restored `SELECT cc.*`+ // while the doc comment kept claiming the trim: assert the SELECT shape+ // at the source level for BOTH engines.+ const { readFileSync } = await import('fs');+ for (const enginePath of ['src/core/postgres-engine.ts', 'src/core/pglite-engine.ts']) {+ const src = readFileSync(new URL(`../${enginePath}`, import.meta.url), 'utf-8');+ const start = src.indexOf('async getChunks(slug');+ expect(start).toBeGreaterThan(0);+ // The method's own close (`\n }` at 2-space indent) — an inline+ // `async (tx) =>` callback must not truncate the body, and the NEXT+ // method (e.g. buildStaleChunkWhere's `cc.embedding IS NULL` WHERE+ // predicate) must not leak in. Strip line comments: the pin targets+ // the SQL, not prose that may cite the anti-pattern.+ const end = src.indexOf('\n }\n', start + 10);+ const body = src.slice(start, end).replace(/\/\/[^\n]*/g, '');+ expect(body, `${enginePath} getChunks must not SELECT cc.*`).not.toContain('cc.*');+ // Every non-vector field rowToChunk reads MUST be selected — omitting+ // one silently degrades round-trips (embed.ts getChunks→upsertChunks+ // rewrote image chunks as text when cc.modality was dropped).+ for (const col of ['chunk_text', 'chunk_source', 'model', 'token_count', 'embedded_at',+ 'language', 'symbol_name', 'symbol_type', 'start_line', 'end_line',+ 'parent_symbol_path', 'doc_comment', 'symbol_name_qualified', 'modality']) {+ expect(body, `${enginePath} getChunks must select cc.${col}`).toContain(`cc.${col}`);+ }+ // The vector columns stay unselected.+ expect(body).not.toMatch(/cc\.embedding\b/);+ }+ });+});diff --git a/test/oauth.test.ts b/test/oauth.test.tsindex 91f32ad952..2389bfcf92 100644--- a/test/oauth.test.ts+++ b/test/oauth.test.ts@@ -894,13 +894,23 @@ describe('operation scope annotations', () => {}});- test('mutating operations are write/admin/sources_admin/users_admin/agent scoped', () => {+ test('mutating operations are write/admin/sources_admin/users_admin/agent scoped unless remote-gated', () => {const { operations } = require('../src/core/operations.ts');+ // #2598, same allowlist as test/operations-trust-boundary.test.ts: think+ // is read-scoped for OAuth/MCP because its handler forces save/take off+ // for remote callers before persistence (pinned by+ // test/takes-mcp-allowlist.serial.test.ts); local CLI can still persist.+ const remoteReadOnlyMutatingOps = new Set(['think']);for (const op of operations) {if (op.mutating) {+ if (remoteReadOnlyMutatingOps.has(op.name)) {+ expect(op.scope, `${op.name} remote-gated mutating op should be read-scoped`).toBe('read');+ continue;+ }// v0.28: sources_admin permits sources_add / sources_remove (mutating// sources, not pages); read scope is the only thing too narrow for- // any mutating op. v0.38: 'agent' is a mutating-axis scope for+ // a mutating op unless its remote path forces persistence off before+ // the handler writes. v0.38: 'agent' is a mutating-axis scope for// submit_agent (creates jobs, spends money, but contained by bindings).expect(['write', 'admin', 'sources_admin', 'users_admin', 'agent'],diff --git a/test/operations-trust-boundary.test.ts b/test/operations-trust-boundary.test.tsindex 3940f8a516..828a5085a4 100644--- a/test/operations-trust-boundary.test.ts+++ b/test/operations-trust-boundary.test.ts@@ -86,8 +86,17 @@ describe('operations contract — every op has scope + correct mutability shape''users_admin','agent',]);+ // Remote-gated exception (#2598, same allowlist as test/oauth.test.ts):+ // `think` is read-scoped for OAuth/MCP because its handler forces+ // save/take OFF for remote callers before persistence — pinned by+ // test/takes-mcp-allowlist.serial.test.ts. Local CLI can still persist.+ const REMOTE_READ_ONLY_MUTATING_OPS = new Set(['think']);for (const op of operations) {if (op.mutating === true) {+ if (REMOTE_READ_ONLY_MUTATING_OPS.has(op.name)) {+ expect(op.scope, `remote-gated mutating op "${op.name}" should be read-scoped`).toBe('read');+ continue;+ }expect(WRITE_CLASS_SCOPES.has(op.scope ?? 'read'),`mutating op "${op.name}" has read-tier scope "${op.scope}"; expected one of ${[...WRITE_CLASS_SCOPES].join('/')}`,diff --git a/test/register-client-source-normalize.test.ts b/test/register-client-source-normalize.test.tsnew file mode 100644index 0000000000..efc3b73118--- /dev/null+++ b/test/register-client-source-normalize.test.ts@@ -0,0 +1,96 @@+/**+ * normalizeSourceInput / normalizeFederatedReadInput contract.+ *+ * The `/admin/api/register-client` HTTP endpoint historically hardcoded+ * source_id='default' (and federated_read=[source_id]) — only the CLI+ * (`--source` / `--federated-read`) could bind a client to a non-default+ * brain source. These two normalizers let the HTTP endpoint accept the same+ * inputs from the request body while preserving the historical default when+ * the fields are omitted.+ *+ * Hermetic — pure-function unit tests, no engine, no HTTP.+ */++import { describe, test, expect } from 'bun:test';+import { normalizeSourceInput, normalizeFederatedReadInput } from '../src/core/source-id.ts';++describe('normalizeSourceInput', () => {+ test('undefined → "default" (backward compat)', () => {+ expect(normalizeSourceInput(undefined)).toBe('default');+ });++ test('null → "default" (backward compat)', () => {+ expect(normalizeSourceInput(null)).toBe('default');+ });++ test('valid source_id passes through', () => {+ expect(normalizeSourceInput('mind-agent-brain')).toBe('mind-agent-brain');+ });++ test('single-char source_id is valid', () => {+ expect(normalizeSourceInput('a')).toBe('a');+ });++ test('invalid source_id (underscore) throws', () => {+ expect(() => normalizeSourceInput('mind_agent_brain')).toThrow(/Invalid source_id/);+ });++ test('invalid source_id (uppercase) throws', () => {+ expect(() => normalizeSourceInput('Mind')).toThrow(/Invalid source_id/);+ });++ test('invalid source_id (edge hyphen) throws', () => {+ expect(() => normalizeSourceInput('-mind')).toThrow(/Invalid source_id/);+ });++ test('non-string (number) throws', () => {+ expect(() => normalizeSourceInput(42)).toThrow(/Invalid source_id/);+ });+});++describe('register-client route wiring (structural)', () => {+ test('normalized source + federatedRead reach registerClientManual in the right positions', () => {+ // The unit tests above prove the normalizers; this pins the ROUTE —+ // a transposition of the two new positional args (or a regression to+ // the hardcoded 'default') would pass every unit test and still ship+ // clients bound to the wrong source.+ const { readFileSync } = require('fs');+ const src = readFileSync(new URL('../src/commands/serve-http.ts', import.meta.url), 'utf-8');+ expect(src).toContain('sourceId = normalizeSourceInput(source)');+ expect(src).toContain('federatedReadIds = normalizeFederatedReadInput(federatedRead)');+ expect(src).toMatch(/registerClientManual\(\s*name,\s*grants,\s*scopeString,\s*uris,\s*sourceId,\s*federatedReadIds,\s*validatedAuthMethod/);+ });+});++describe('normalizeFederatedReadInput', () => {+ test('undefined → undefined (let registerClientManual default to [sourceId])', () => {+ expect(normalizeFederatedReadInput(undefined)).toBeUndefined();+ });++ test('null → undefined', () => {+ expect(normalizeFederatedReadInput(null)).toBeUndefined();+ });++ test('valid single-element array passes through', () => {+ expect(normalizeFederatedReadInput(['mind-agent-brain'])).toEqual(['mind-agent-brain']);+ });++ test('valid multi-element array passes through (read across both sources)', () => {+ expect(normalizeFederatedReadInput(['default', 'mind-agent-brain'])).toEqual([+ 'default',+ 'mind-agent-brain',+ ]);+ });++ test('empty array throws (ambiguous — omit the field instead)', () => {+ expect(() => normalizeFederatedReadInput([])).toThrow(/Invalid federatedRead/);+ });++ test('non-array (string) throws', () => {+ expect(() => normalizeFederatedReadInput('default')).toThrow(/Invalid federatedRead/);+ });++ test('array with an invalid source_id element throws', () => {+ expect(() => normalizeFederatedReadInput(['default', 'bad_id'])).toThrow(/Invalid source_id/);+ });+});diff --git a/test/repair-jsonb.test.ts b/test/repair-jsonb.test.tsindex 6b774e4e39..ada85efaec 100644--- a/test/repair-jsonb.test.ts+++ b/test/repair-jsonb.test.ts@@ -19,9 +19,10 @@ describe('repairJsonb — PGLite short-circuit', () => {});expect(result.engine).toBe('pglite');expect(result.total_repaired).toBe(0);- // All 5 columns reported: pages.frontmatter, raw_data.data,- // ingest_log.pages_updated, files.metadata, page_versions.frontmatter.- expect(result.per_target.length).toBe(5);+ // All 8 columns reported: 5 from the v0.12.0 wave + 3 from the v0.16.0+ // subagent_* wave (added when persistMessage / persistToolExec* was+ // identified as a second double-encode site).+ expect(result.per_target.length).toBe(8);for (const t of result.per_target) {expect(t.rows_repaired).toBe(0);}@@ -32,6 +33,9 @@ describe('repairJsonb — PGLite short-circuit', () => {'page_versions.frontmatter','pages.frontmatter','raw_data.data',+ 'subagent_messages.content_blocks',+ 'subagent_tool_executions.input',+ 'subagent_tool_executions.output',]);});});diff --git a/test/schema-bootstrap-coverage.test.ts b/test/schema-bootstrap-coverage.test.tsindex 11419ccbf4..c5c5bf8ce6 100644--- a/test/schema-bootstrap-coverage.test.ts+++ b/test/schema-bootstrap-coverage.test.ts@@ -171,6 +171,13 @@ const REQUIRED_BOOTSTRAP_COVERAGE: ForwardReference[] = [// v121 — referenced by the timeline event lookup and dedup indexes before// the numbered migration can add the column on an existing brain.{ kind: 'column', table: 'timeline_entries', column: 'event_page_id' },+ // v7-era — surfaced by the #2626-class scanner sweep: both columns are+ // migration-added (v7) AND referenced by blob indexes+ // (`idx_minion_jobs_timeout` partial on timeout_at, the partial UNIQUE+ // `uniq_minion_jobs_idempotency` on idempotency_key). A pre-v7 minion_jobs+ // wedges the blob replay exactly like the v121 incident.+ { kind: 'column', table: 'minion_jobs', column: 'timeout_at' },+ { kind: 'column', table: 'minion_jobs', column: 'idempotency_key' },];test('applyForwardReferenceBootstrap covers every forward reference declared in REQUIRED_BOOTSTRAP_COVERAGE', async () => {@@ -261,6 +268,13 @@ test('applyForwardReferenceBootstrap covers every forward reference declared inDROP INDEX IF EXISTS idx_timeline_event_page;ALTER TABLE timeline_entries DROP CONSTRAINT IF EXISTS timeline_entries_event_page_id_fkey;ALTER TABLE timeline_entries DROP COLUMN IF EXISTS event_page_id;++ -- v7 minion_jobs strip (#2626 class sweep): timeout_at + idempotency_key+ -- are migration-added and blob-indexed; strip so bootstrap must re-add.+ DROP INDEX IF EXISTS idx_minion_jobs_timeout;+ DROP INDEX IF EXISTS uniq_minion_jobs_idempotency;+ ALTER TABLE minion_jobs DROP COLUMN IF EXISTS timeout_at;+ ALTER TABLE minion_jobs DROP COLUMN IF EXISTS idempotency_key;`);// Note: we don't strip sources.archived* here because they're inline in the@@ -346,6 +360,14 @@ test('after bootstrap, PGLITE_SCHEMA_SQL replays without crashing on missing forDROP INDEX IF EXISTS idx_timeline_event_page;ALTER TABLE timeline_entries DROP CONSTRAINT IF EXISTS timeline_entries_event_page_id_fkey;ALTER TABLE timeline_entries DROP COLUMN IF EXISTS event_page_id;++ -- v7 minion_jobs strip (#2626 class sweep): the SCHEMA_SQL replay would+ -- crash on idx_minion_jobs_timeout / uniq_minion_jobs_idempotency+ -- without the bootstrap re-adding these migration-added columns.+ DROP INDEX IF EXISTS idx_minion_jobs_timeout;+ DROP INDEX IF EXISTS uniq_minion_jobs_idempotency;+ ALTER TABLE minion_jobs DROP COLUMN IF EXISTS timeout_at;+ ALTER TABLE minion_jobs DROP COLUMN IF EXISTS idempotency_key;`);// Bootstrap, then schema replay. Either step crashing fails the test.@@ -599,16 +621,72 @@ function parseAlterAddColumns(sql: string): Array<{ table: string; column: strinreturn result;}+/**+ * The coverage predicate for blob index-column references, extracted so the+ * v121-regression unit test below can exercise it with synthetic inputs.+ *+ * v0.42.58 (#2626 class): CREATE TABLE presence must NOT count as coverage+ * for a column that ANY migration also adds via ALTER TABLE ADD COLUMN. The+ * migration's existence proves pre-existing tables can lack the column, and+ * on those brains `CREATE TABLE IF NOT EXISTS` no-ops — so the blob's+ * CREATE INDEX crashes initSchema before runMigrations can help. For such+ * columns, only an applyForwardReferenceBootstrap ALTER counts. This is+ * exactly how `timeline_entries.event_page_id` (v121, Life Chronicle) shipped+ * a P0 upgrade wedge past the old predicate: it was in the current CREATE+ * TABLE body, so the check passed while every pre-v121 brain wedged.+ */+function buildIndexRefCoveragePredicate(+ tableColumns: Map<string, Set<string>>,+ bootstrapAdds: Array<{ table: string; column: string }>,+ migrationAddedKeys: Set<string>,+): (table: string, column: string) => boolean {+ return (table: string, column: string): boolean => {+ const inBootstrap = bootstrapAdds.some(a => a.table === table && a.column === column);+ if (inBootstrap) return true;+ // Migration-added columns are forward references by definition —+ // CREATE TABLE presence is exactly the mask that hid the v121 wedge.+ if (migrationAddedKeys.has(`${table}.${column}`)) return false;+ const cols = tableColumns.get(table);+ return Boolean(cols && cols.has(column));+ };+}++test('buildIndexRefCoveragePredicate: CREATE TABLE presence does not mask migration-added columns (v121 regression shape)', () => {+ const tableColumns = new Map([['timeline_entries', new Set(['id', 'event_page_id'])]]);+ const migrationAdded = new Set(['timeline_entries.event_page_id']);++ // The exact pre-fix v121 shape: column in CREATE TABLE, added by migration,+ // NO bootstrap probe → must be UNCOVERED (old predicate said covered).+ const withoutProbe = buildIndexRefCoveragePredicate(tableColumns, [], migrationAdded);+ expect(withoutProbe('timeline_entries', 'event_page_id')).toBe(false);+ // Plain blob-native column (not migration-added) stays covered by CREATE TABLE.+ expect(withoutProbe('timeline_entries', 'id')).toBe(true);++ // With the bootstrap probe present, the same column is covered.+ const withProbe = buildIndexRefCoveragePredicate(+ tableColumns,+ [{ table: 'timeline_entries', column: 'event_page_id' }],+ migrationAdded,+ );+ expect(withProbe('timeline_entries', 'event_page_id')).toBe(true);+});+test('every CREATE INDEX column in PGLITE_SCHEMA_SQL is covered by CREATE TABLE or bootstrap (A2 static check)', async () => {// The structural test that closes the 11-incident wedge class. Static// contract: every column referenced by a CREATE INDEX in PGLITE_SCHEMA_SQL- // must be either (a) declared in the current CREATE TABLE body, or- // (b) added by `applyForwardReferenceBootstrap` in pglite-engine.ts.+ // must be either (a) declared in the current CREATE TABLE body AND not+ // added by any migration (see buildIndexRefCoveragePredicate — migration-+ // added columns are forward references even when the CREATE TABLE body has+ // them), or (b) added by `applyForwardReferenceBootstrap` in+ // pglite-engine.ts.//// Codex outside-voice review caught the 11th wedge: composite-index second// columns (`provider_id` in `(job_id, provider_id)`) are forward references// that earlier extractors missed. This parser walks the full column list// of every index — composite or not — and asserts each one is covered.+ // The 12th wedge (v121 `timeline_entries.event_page_id`, #2626 #2594 #2579+ // #2537 #2536) slipped through because CREATE TABLE presence masked the+ // forward reference; the predicate now cross-references MIGRATIONS.//// Self-updating: when a future migration adds a CREATE INDEX in// PGLITE_SCHEMA_SQL on a column that bootstrap doesn't yet provide, this@@ -616,6 +694,7 @@ test('every CREATE INDEX column in PGLITE_SCHEMA_SQL is covered by CREATE TABLEconst { readFileSync } = await import('fs');const { resolve: resolvePath } = await import('path');const { PGLITE_SCHEMA_SQL } = await import('../src/core/pglite-schema.ts');+ const { extractAddedColumnsFromMigrations } = await import('./helpers/extract-added-columns.ts');const enginePath = resolvePath(process.cwd(), 'src/core/pglite-engine.ts');const engineSrc = readFileSync(enginePath, 'utf-8');@@ -623,21 +702,23 @@ test('every CREATE INDEX column in PGLITE_SCHEMA_SQL is covered by CREATE TABLEconst tableColumns = parseBaseTableColumns(PGLITE_SCHEMA_SQL);const indexRefs = parseIndexColumnReferences(PGLITE_SCHEMA_SQL);const bootstrapAdds = parseAlterAddColumns(engineSrc);+ const migrationAddedKeys = new Set(+ extractAddedColumnsFromMigrations().map(a => `${a.table}.${a.column}`),+ );- // Build the "covered" set: for each (table, column) pair, true iff it's in- // the table's CREATE TABLE columns OR added by an ALTER TABLE in the- // bootstrap function.- const covered = (table: string, column: string): boolean => {- const cols = tableColumns.get(table);- if (cols && cols.has(column)) return true;- return bootstrapAdds.some(a => a.table === table && a.column === column);- };+ const covered = buildIndexRefCoveragePredicate(tableColumns, bootstrapAdds, migrationAddedKeys);// Sanity checks: parser caught the codex case AND bootstrap provides it.expect(indexRefs).toContainEqual({ table: 'subagent_messages', column: 'provider_id' });expect(bootstrapAdds).toContainEqual({ table: 'subagent_messages', column: 'provider_id' });expect(covered('subagent_messages', 'provider_id')).toBe(true);+ // Direct pin of the v121 incident: the column is migration-added, blob-+ // indexed, and MUST be bootstrap-covered.+ expect(migrationAddedKeys.has('timeline_entries.event_page_id')).toBe(true);+ expect(indexRefs.some(r => r.table === 'timeline_entries' && r.column === 'event_page_id')).toBe(true);+ expect(bootstrapAdds).toContainEqual({ table: 'timeline_entries', column: 'event_page_id' });+// The actual contract: every index column reference must be covered.const uncovered: Array<{ table: string; column: string }> = [];for (const ref of indexRefs) {@@ -650,10 +731,13 @@ test('every CREATE INDEX column in PGLITE_SCHEMA_SQL is covered by CREATE TABLEconst list = uncovered.map(u => ` ${u.table}.${u.column}`).join('\n');throw new Error(`PGLITE_SCHEMA_SQL has ${uncovered.length} CREATE INDEX column reference(s) ` +- `that are neither in the table's CREATE TABLE body nor added by ` +- `applyForwardReferenceBootstrap:\n${list}\n\n` ++ `that are not safely covered (in the CREATE TABLE body AND not migration-added, ` ++ `or added by applyForwardReferenceBootstrap):\n${list}\n\n` +`Fix: extend applyForwardReferenceBootstrap in src/core/pglite-engine.ts ` +- `(and the matching Postgres engine) with the missing ALTER TABLE ADD COLUMN.`,+ `(and the matching Postgres engine) with the missing ALTER TABLE ADD COLUMN. ` ++ `A column that is BOTH in the blob's CREATE TABLE AND added by a migration ` ++ `is a forward reference for pre-existing tables — CREATE TABLE presence ` ++ `does not cover it (that mask shipped the v121 upgrade wedge).`,);}}, 30000);diff --git a/test/schema-pack-unify-types-handler.test.ts b/test/schema-pack-unify-types-handler.test.tsindex 80f5a84a4d..2409498416 100644--- a/test/schema-pack-unify-types-handler.test.ts+++ b/test/schema-pack-unify-types-handler.test.ts@@ -156,3 +156,21 @@ describe('runUnifyTypes', () => {});});});++// #1575 — the jobs worker registration must honor the handler's documented+// dry-run default. `apply: data.apply ?? true` made the canonical operator+// invocation (`gbrain jobs submit unify-types --allow-protected --params+// '{"target_pack":...}'`) destructively retype pages by default while+// UnifyTypesOpts.apply documents 'Default false (dry-run)'. Structural pin —+// the worker source must default apply to false.+import { readFileSync } from 'fs';++describe('#1575 unify-types worker dry-run default', () => {+ it('jobs.ts worker registration defaults apply to false, matching the handler contract', () => {+ const jobsSource = readFileSync(new URL('../src/commands/jobs.ts', import.meta.url), 'utf-8');+ const workerBlock = jobsSource.slice(jobsSource.indexOf("worker.register('unify-types'"));+ const registration = workerBlock.slice(0, workerBlock.indexOf('});'));+ expect(registration).toContain('apply: data.apply ?? false');+ expect(registration).not.toContain('apply: data.apply ?? true');+ });+});diff --git a/test/scripts/run-unit-parallel.test.ts b/test/scripts/run-unit-parallel.test.tsindex 3656764a4f..9c7f324554 100644--- a/test/scripts/run-unit-parallel.test.ts+++ b/test/scripts/run-unit-parallel.test.ts@@ -72,7 +72,10 @@ function runWrapper(extraArgs: string[] = []): { code: number; stdout: string; sconst result = spawnSync('bash',[join(TMPROOT, 'scripts', 'run-unit-parallel.sh'), '--shards', '2', ...extraArgs],- { cwd: TMPROOT, encoding: 'utf-8', env: { ...process.env } },+ // Shard-mechanics tests pin explicit --shards behavior with tiny+ // synthetic files; disable mem-adaptation so a RAM-limited runner (CI's+ // ~7GB) can't collapse 2 shards -> 1 and break the shard 1/2 expectations.+ { cwd: TMPROOT, encoding: 'utf-8', env: { ...process.env, GBRAIN_TEST_NO_MEM_ADAPT: '1' } },);return {code: result.status ?? -1,@@ -209,6 +212,8 @@ describe('passing', () => {HOME: process.env.HOME ?? FROOT,TMPDIR: process.env.TMPDIR ?? '/tmp',GBRAIN_TEST_SHARD_TIMEOUT: '300',+ // Same rationale as runWrapper: explicit-shard mechanics under test.+ GBRAIN_TEST_NO_MEM_ADAPT: '1',};});@@ -257,3 +262,141 @@ describe('failing-on-purpose', () => {}});});++describe('run-unit-parallel.sh OOM rescue lane', () => {+ // A fixture that fails WITH the WASM out-of-memory signature on its first+ // run (no sentinel file yet) and passes once the sentinel exists — exactly+ // the phantom-failure shape: dies under parallel memory pressure, passes+ // serially. The runner must (1) detect the signature, (2) re-run the file+ // at --max-concurrency 1, (3) exit 0 with an oom_rescued note.+ let OROOT: string;++ beforeAll(() => {+ OROOT = mkdtempSync(join(tmpdir(), 'gbrain-parallel-oom-'));+ mkdirSync(join(OROOT, 'scripts'), { recursive: true });+ mkdirSync(join(OROOT, 'test'), { recursive: true });+ for (const s of ['run-unit-parallel.sh', 'run-unit-shard.sh', 'run-serial-tests.sh']) {+ copyFileSync(resolve(REPO_ROOT, 'scripts', s), join(OROOT, 'scripts', s));+ chmodSync(join(OROOT, 'scripts', s), 0o755);+ }+ const passing = `import { describe, it, expect } from 'bun:test';+describe('passing', () => {+ it('arithmetic works', () => { expect(1 + 1).toBe(2); });+});`;+ const oomOnce = `import { describe, it, expect } from 'bun:test';+import { existsSync, writeFileSync } from 'fs';+describe('oom-once', () => {+ it('fails with the WASM OOM signature on first run, passes on retry', () => {+ const sentinel = new URL('./oom-sentinel.txt', import.meta.url).pathname;+ if (!existsSync(sentinel)) {+ writeFileSync(sentinel, 'ran-once');+ console.error('Original error: Out of memory');+ throw new Error('Out of memory (simulated PGLite WASM connect failure)');+ }+ expect(1).toBe(1);+ });+});`;+ writeFileSync(join(OROOT, 'test', 'a-pass.test.ts'), passing);+ writeFileSync(join(OROOT, 'test', 'b-oom-once.test.ts'), oomOnce);+ });++ afterAll(() => {+ if (OROOT) rmSync(OROOT, { recursive: true, force: true });+ });++ function runOom(env: Record<string, string> = {}): { code: number; stdout: string; stderr: string } {+ rmSync(join(OROOT, 'test', 'oom-sentinel.txt'), { force: true });+ const result = spawnSync(+ 'bash',+ [join(OROOT, 'scripts', 'run-unit-parallel.sh'), '--shards', '2'],+ { cwd: OROOT, encoding: 'utf-8', env: { ...process.env, GBRAIN_TEST_NO_MEM_ADAPT: '1', ...env } },+ );+ return { code: result.status ?? -1, stdout: result.stdout || '', stderr: result.stderr || '' };+ }++ it('rescues an OOM-signature failure serially and exits 0 with an oom_rescued note', () => {+ const r = runOom();+ expect(r.stdout + r.stderr).toContain('OOM rescue pass');+ expect(r.stderr).toContain('oom_rescued=');+ expect(r.code).toBe(0);+ }, 120_000);++ it('GBRAIN_TEST_NO_OOM_FALLBACK=1 disables the rescue lane (stays red)', () => {+ const r = runOom({ GBRAIN_TEST_NO_OOM_FALLBACK: '1' });+ expect(r.code).not.toBe(0);+ expect(r.stdout + r.stderr).not.toContain('OOM rescue pass');+ }, 120_000);++ it('memory-aware sizing is advertised in the banner (mem-ok or mem-adapted)', () => {+ // The one test that needs adaptation ON — override the harness-wide+ // NO_MEM_ADAPT base (which keeps the shard-mechanics tests deterministic+ // on RAM-limited CI runners).+ const r = runOom({ GBRAIN_TEST_NO_MEM_ADAPT: '0' });+ expect(r.stderr).toMatch(/mem-(ok|adapted)/);+ }, 120_000);++ it('mixed run: a plain assertion failure stays red even when the OOM phantom rescues green', () => {+ // The NON_OOM_FAIL gate — the branch that stops the rescue lane from+ // absolving real failures that happened to share a run with phantoms.+ const realFail = `import { describe, it, expect } from 'bun:test';+describe('real-failure', () => {+ it('expects 1 to equal 2', () => { expect(1).toBe(2); });+});`;+ writeFileSync(join(OROOT, 'test', 'c-real-fail.test.ts'), realFail);+ try {+ const r = runOom();+ expect(r.code).not.toBe(0);+ } finally {+ rmSync(join(OROOT, 'test', 'c-real-fail.test.ts'), { force: true });+ }+ }, 120_000);++ it('a deterministic failure carrying the OOM signature re-fails serially and stays red', () => {+ // The oom_rescue_failed lane: signature match queues the file, but the+ // serial re-run confirms the failure is real — run must stay red.+ const alwaysOom = `import { describe, it } from 'bun:test';+describe('oom-always', () => {+ it('always fails with the signature', () => {+ console.error('Original error: Out of memory');+ throw new Error('Out of memory (deterministic)');+ });+});`;+ writeFileSync(join(OROOT, 'test', 'd-oom-always.test.ts'), alwaysOom);+ try {+ const r = runOom();+ expect(r.code).not.toBe(0);+ expect(r.stderr).toContain('oom_rescue_failed=');+ expect(r.stdout + r.stderr).toContain('oom-rescue (serial, confirmed real)');+ } finally {+ rmSync(join(OROOT, 'test', 'd-oom-always.test.ts'), { force: true });+ }+ }, 120_000);+});++describe('run-unit-parallel.sh external-kill rescue contract', () => {+ // An externally-killed shard (sibling workspace pkill, memory jetsam)+ // presents as rc 143/137 well before the shard timeout. Simulating a+ // mid-run external kill deterministically in a fixture is flaky, so this+ // pins the load-bearing structure instead: the early-death detector, the+ // 80%-of-timeout threshold that separates external kills from real wedges,+ // and the rescue-queue routing for both the wedged and non-wedged branches.+ it('detects early SIGTERM/SIGKILL deaths against the 80% timeout threshold', () => {+ const source = readFileSync(PARALLEL_SH_SRC, 'utf-8');+ expect(source).toContain('[ "$rc" = "143" ] || [ "$rc" = "137" ]');+ expect(source).toContain('$((SHARD_TIMEOUT * 80 / 100))');+ expect(source).toContain('shard_external_kill=1');+ });++ it('routes externally-killed shards into the serial rescue queue, not the red path', () => {+ const source = readFileSync(PARALLEL_SH_SRC, 'utf-8');+ const killBranches = source.split('shard_external_kill" = "1"').length - 1;+ expect(killBranches).toBeGreaterThanOrEqual(2); // wedged + non-wedged branch+ expect(source).toContain('KILLED externally after ${s_elapsed}s');+ });++ it('stamps per-shard start/end epochs so early death is measurable', () => {+ const source = readFileSync(PARALLEL_SH_SRC, 'utf-8');+ expect(source).toContain('date +%s > "$LOG_DIR/shard-$i.start"');+ expect(source).toContain('date +%s > "$LOG_DIR/shard-$i.end"');+ });+});diff --git a/test/takes-mcp-allowlist.serial.test.ts b/test/takes-mcp-allowlist.serial.test.tsindex 01413b814e..6d635258f7 100644--- a/test/takes-mcp-allowlist.serial.test.ts+++ b/test/takes-mcp-allowlist.serial.test.ts@@ -17,6 +17,7 @@ import { withoutAnthropicKey } from './helpers/no-anthropic-key.ts';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { dispatchToolCall } from '../src/mcp/dispatch.ts';import { TAKES_FENCE_BEGIN, TAKES_FENCE_END } from '../src/core/takes-fence.ts';+import { operationsByName } from '../src/core/operations.ts';let engine: PGLiteEngine;let alicePageId: number;@@ -198,6 +199,11 @@ describe('per-token takes-holder allow-list — get_versions body channel', () =});describe('think op — read-only on remote callers (Lane D landed)', () => {+ test('think is read-scoped for MCP while local persistence remains possible', () => {+ expect(operationsByName.think.scope).toBe('read');+ expect(operationsByName.think.mutating).toBe(true);+ });+test('remote save/take is forced read-only via remote_persisted_blocked flag', async () => {// Hermetic no-key: neutralize BOTH env var AND ~/.gbrain config key, else a// configured machine fires a real LLM call and the warning flips to