Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
v0.41.1.0 feat: eval-loop wave — gbrain bench publish + gbrain eval gate close the LOOP (#1352)
Garry Tanbf52e1049b1f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c0fb2160c6..9efac22926 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,95 @@All notable changes to GBrain will be documented in this file.+## [0.41.1.0] - 2026-05-24++**Your CI can now fail a PR when search retrieval gets worse.** Before this release, gbrain shipped the pieces you'd need to measure retrieval quality (capture, replay, nightly probe, cross-modal runner) but nothing connected them into a loop. You could see a quality drop on your screen but nothing automatically caught it. v0.41 closes the loop end-to-end. You publish a baseline once — a snapshot of how your brain performs on a set of real queries — and then `gbrain eval gate` runs against that baseline on every PR. If results get materially worse OR if the brain stops finding pages it used to find, the command exits non-zero and CI turns red. The wave also wires the nightly quality probe into the autopilot daemon (opt-in via config) so a brain you've left running notices its own degradation.++Two ways to fail a gate: a **regression gate** that compares current retrieval to a baseline you captured (catches "did my refactor break search?") and a **correctness gate** that runs your queries against a list of known-right answers (catches "is my search actually any good?"). Most people will start with the regression gate because it's cheaper — `gbrain bench publish` turns your last 200 real queries into a baseline file in a few seconds. The correctness gate (qrels-based, recall@K + first-relevant-hit-rate) is the more honest signal: jaccard alone measures consistency with old retrieval, not correctness, so a better embedding model would FAIL a jaccard-only gate. Both gates are source-id-aware (`source_id::slug` compares) so federated brains can't false-pass via wrong-source hits — the canonical gbrain pitfall closed structurally before it could enter the eval surface.++## To take advantage of v0.41.1.0++`gbrain upgrade` handles the binary update. Then choose one of three paths depending on how you use gbrain:++**Path A — Your own personal regression gate (5 minutes):**++```bash+# 1. Capture a few hundred real queries (one-time; uses queries already in eval_candidates)+gbrain eval export --limit 200 --tool query > /tmp/captured.ndjson++# 2. Publish them as a baseline+mkdir -p ~/.gbrain/baselines+gbrain bench publish --from /tmp/captured.ndjson --to ~/.gbrain/baselines/personal.baseline.ndjson --label "personal-$(date +%Y%m%d)"++# 3. Gate against it (run this in pre-commit, in CI, or just by hand)+gbrain eval gate --baseline ~/.gbrain/baselines/personal.baseline.ndjson+```++Eval capture is OFF by default — set `GBRAIN_CONTRIBUTOR_MODE=1` in your shell or run `gbrain config set eval.capture true` if you want capture to start running (the v0.42 wave flips this default once the privacy hardening lands).++**Path B — Add the qrels correctness gate (more honest signal):**++```bash+# Use the bundled placeholder fixture or write your own qrels.json+gbrain eval gate --qrels test/fixtures/eval-baselines/qrels-search.json++# Both gates required (most useful in CI):+gbrain eval gate --baseline ~/.gbrain/baselines/personal.baseline.ndjson --qrels ~/.gbrain/qrels/personal.qrels.json+```++The qrels file is a JSON object with `{schema_version: 1, queries: [{query, relevant_slugs, first_relevant_slug}]}`. For multi-source / federated brains, use the explicit shape: `{query, relevant: [{source_id, slug}], expected_top1: {source_id, slug}}`.++**Path C — Let your autopilot run the nightly probe:**++```bash+# Opt in (default is off — protects API spend on fresh installs)+gbrain config set autopilot.nightly_quality_probe.enabled true+gbrain config set autopilot.nightly_quality_probe.max_usd 5++# Next autopilot tick, the probe fires once per 24h and writes an event+# to ~/.gbrain/audit/quality-probe-YYYY-Www.jsonl. `gbrain doctor` surfaces+# the outcome.+```++If any step fails or numbers look wrong, please file an issue with `gbrain doctor` output and `tail -20 ~/.gbrain/audit/bench-publish-*.jsonl` so we can see what the wave's audit trail recorded.++### Itemized changes++**New CLI verbs:**+- `gbrain bench publish --from <captured.ndjson> --to <X.baseline.ndjson> [--label STRING] [--force] [--threshold-jaccard FLOAT] [--threshold-top1 FLOAT] [--threshold-latency-multiplier FLOAT] [--json]` — turn captured eval rows into a baseline file. Stamps a stable `query_hash` on every row, embeds `_kind: 'baseline_metadata'` discriminator + thresholds + `source_hash` in the first line, sorts rows deterministically. Strict posture: empty input → exit 1; duplicate `(tool_name, source_ids, query_hash)` → exit 1 with first 5 dupes + paste-ready dedup hint; `--to` exists → exit 2 unless `--force`. Audit JSONL at `~/.gbrain/audit/bench-publish-YYYY-Www.jsonl`.+- `gbrain eval gate [--baseline <X.baseline.ndjson>] [--qrels <Y.qrels.json>] [--threshold-jaccard FLOAT] [--threshold-top1 FLOAT] [--threshold-latency-multiplier FLOAT] [--threshold-recall-at-k FLOAT] [--threshold-first-relevant-hit FLOAT] [--threshold-expected-top1 FLOAT] [-k INT] [--json]` — two-gate dispatch. Regression gate replays baseline queries in-process via `replayCore` (NOT spawn subprocess — avoids gbrain-version-drift for source-tree CI). Correctness gate runs each qrels query via bare `hybridSearch` (deterministic; matches existing eval harness pattern at `src/core/search/eval.ts:242`). Both gates carry source_id through `source_id::slug` compare keys. Verdict requires BOTH to pass when both flags set. Latency math `(baseline + delta) / baseline <= multiplier` (corrected from the original `delta / baseline` shape that would have let 2.5x slowdowns pass at multiplier=2.0). Exit codes: 0 PASS, 1 FAIL (regression OR in-process throw — D3 fail-closed), 2 USAGE.++**New core modules:**+- `src/core/bench/baseline-file.ts` — single source of truth for the `.baseline.ndjson` file shape. Exports `BaselineMetadata`, `BaselineFile`, `BaselineRow`, `BaselineThresholds` types + pure `parseBaselineFile()` / `serializeBaselineFile()` / `computeSourceHash()` / `normalizeQueryForHash()` / `computeQueryHash()` helpers. Body rows stamped with `schema_version: 1` so existing `eval replay` parser accepts them unchanged. ~190 LOC.+- `src/core/bench/qrels-file.ts` — pure parser + math for the `.qrels.json` shape. Accepts BOTH the existing fixture shape (slug-only `relevant_slugs` + `first_relevant_slug`, auto-promoted to `source_id='default'`) AND the federated shape (explicit `relevant: [{source_id, slug}]` + `expected_top1`). `computeRecallAtK` / `computeFirstRelevantHit` / `computeExpectedTop1Hit` pure functions; compare keys are `${source_id}::${slug}` strings everywhere. ~210 LOC.+- `src/core/bench/correctness-gate.ts` — orchestrator that runs every qrels query via bare `hybridSearch` and computes aggregate metrics. Per-query throws recorded as `errored: true` and flagged as a gate failure (any qrels-query exception flips verdict to fail). Injectable `searchFn` test seam. ~140 LOC.+- `src/core/cycle/nightly-probe-adapters.ts` — bridges the autopilot's object-shape `NightlyProbeDeps` to the existing argv-shape `runEvalLongMemEval` + `runEvalCrossModal` CLI functions. Cross-modal adapter threads `--output summaryPath` explicitly (regression for codex-caught argv bug — without it the summary lands at the default receipt path). In-process invocation (not subprocess) avoids gbrain-version-drift. ~125 LOC.++**Autopilot wiring (opt-in, off by default):**+- `src/commands/autopilot.ts` — tick body now invokes `runNightlyQualityProbe` when `cfg.autopilot.nightly_quality_probe.enabled === true`. The phase's internal 24h rate-limit (via `shouldRunNightly` reading the audit JSONL) is the single source of truth — no scheduler-side precheck. Probe call wrapped in try/catch that logs to stderr and DOES NOT bump `consecutiveErrors` (probe failure is informational, never crashes the loop). Default `max_usd` cap = 5.++**Modified existing modules:**+- `src/commands/eval-replay.ts:parseNdjson` — one-line addition: skip lines where `_kind === 'baseline_metadata'`. Without this, the metadata header would be counted as a fake captured row and pollute counts.+- `src/commands/eval-replay.ts` — new exported `replayCore(engine, opts): Promise<{summary, results}>` programmatic entrypoint. The existing CLI `runEvalReplay` now wraps it. Lets `eval gate` call replay in-process rather than spawning a subprocess. `ReplaySummary` interface also exported so eval-gate can type its envelope.+- `src/commands/eval.ts` — added `'gate'` to the sub-subcommand dispatch.++**Tests (8 new files, ~65 cases):**+- `test/bench/baseline-file.test.ts` (9 cases) — round-trip, threshold defaults, source_hash determinism, schema version reject, malformed metadata reject, empty file reject, missing query_hash reject.+- `test/bench/qrels-file.test.ts` (19 cases) — legacy fixture shape parsing, federated shape with explicit source_id, recall@K math edge cases, multi-source guard via `source_id::slug` compare keys.+- `test/bench/correctness-gate.test.ts` (6 cases) — per-query iteration, throw-fails-gate (Finding 2D), missing-page-as-miss, empty retrieved → 0, wrong-source-no-credit (eng-D5 regression), expected_top1 denominator math.+- `test/bench-publish.test.ts` (10 cases) — buildBaselineFromInput happy + edge paths, strict dedupe posture, multi-source NOT a dupe, deterministic serialize, round-trip stability.+- `test/eval-replay-metadata-skip.test.ts` (2 cases) — IRON-RULE: metadata header skipped from row counts; malformed rows still rejected (validator live).+- `test/eval-gate.test.ts` (10 cases) — usage errors, baseline-only path, qrels-only path, JSON envelope shape, latency math (corrected formula pinned, OLD formula's bug documented).+- `test/cycle/nightly-probe-adapters.test.ts` (6 cases) — receipt parsing, missing file throws, malformed JSON throws, argv shape regression (pins `--output`), shape contract pinned.+- `test/autopilot-nightly-probe-wiring.test.ts` (8 cases) — source-shape regression for the autopilot wiring: feature flag check, NO scheduler-side rate-limit (D10 simplification), try/catch doesn't bump consecutiveErrors, DI shape, in-process via gateway.isAvailable (not subprocess), max_usd default = 5.+- `test/e2e/eval-loop.test.ts` (4 cases) — full PGLite in-memory LOOP: self-gate passes, perturbed-row gate fails, D3 fail-closed on malformed baseline, byte-stable round-trip.++**v0.42+ follow-ups filed in TODOS.md:**+- Capture-default flip + scrubber hardening (deferred from this wave per D1 — needs v0.41's destination to ship first).+- `gbrain bench publish --suggest-thresholds` once 30 days of CI data accumulates.+- `gbrain bench diff` + `gbrain bench list` companion verbs.++Plan + 13 CEO-review decisions + 10 eng-review decisions + 2 codex outside-voice rounds (15 + 9 findings, all absorbed) at `~/.claude/plans/system-instruction-you-are-working-rustling-peacock.md`. Strategic frame: the v0.41 wave closes the eval LOOP end-to-end so retrieval regressions on master burn a publicly-visible score in BrainBench-Real (sibling `gbrain-evals` repo).## [0.41.0.0] - 2026-05-24**Your 100-job subagent batch now actually completes.** A real user ran `gbrain jobs work --concurrency 10` against an Azure-hosted Anthropic endpoint, submitted 100 background jobs, and watched every single one dead-letter with `rate lease "anthropic:messages" full (8/8)`. The default cap of 8 starved 2 workers; every starved job got marked as a failure, hit `max_attempts = 3` after 3 lease-full bounces, and dead-lettered. This release turns minions from "a CLI you drive" into "a fleet you supervise" — submit a batch, walk away, come back to completed work.diff --git a/CLAUDE.md b/CLAUDE.mdindex 6900fb2cce..52d77e4df3 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -91,7 +91,10 @@ strict behavior when unset.- `src/core/cross-modal-eval/receipt-write.ts` (v0.27.x) — wraps `fs.writeFileSync` with `mkdirSync({recursive:true})` ahead of every write (T5 correction; `gbrainPath()` does NOT auto-mkdir).- `src/commands/eval-export.ts` (v0.25.0) — streams `eval_candidates` rows as NDJSON to stdout with `schema_version: 1` prefix on every line. EPIPE-safe, progress heartbeats on stderr, stable id-desc tiebreaker so `--since` windows never dupe/miss rows.- `src/commands/eval-prune.ts` (v0.25.0) — explicit retention cleanup. Requires `--older-than DUR`. `--dry-run` reports would-delete count.-- `src/commands/eval-replay.ts` (v0.25.0) — contributor-facing replay tool. Reads NDJSON from `gbrain eval export`, re-runs each captured `query` / `search` op against the current brain, computes set-Jaccard@k between captured + current `retrieved_slugs`, top-1 stability rate, and latency Δ. Stable JSON shape (`schema_version: 1`) for CI gating; human mode prints a regression table. Pure Bun, zero new deps. The dev-loop half of BrainBench-Real that closes the gap between "data captured" and "data used to gate a PR." See `docs/eval-bench.md` for the workflow.+- `src/commands/eval-replay.ts` (v0.25.0, extended v0.41.0.0) — contributor-facing replay tool. Reads NDJSON from `gbrain eval export`, re-runs each captured `query` / `search` op against the current brain, computes set-Jaccard@k between captured + current `retrieved_slugs`, top-1 stability rate, and latency Δ. Stable JSON shape (`schema_version: 1`) for CI gating; human mode prints a regression table. Pure Bun, zero new deps. The dev-loop half of BrainBench-Real that closes the gap between "data captured" and "data used to gate a PR." See `docs/eval-bench.md` for the workflow. **v0.41.0.0 changes:** `parseNdjson` now skips lines where `_kind === 'baseline_metadata'` so `gbrain bench publish`-written baselines parse cleanly without the metadata header polluting row counts. New exported `replayCore(engine, opts): Promise<{summary, results}>` programmatic entrypoint + exported `ReplaySummary` type so `gbrain eval gate` can call replay in-process (NOT spawn subprocess — avoids gbrain-version-drift for source-tree CI runs). The existing CLI `runEvalReplay` now wraps `replayCore`.+- `src/core/bench/baseline-file.ts` + `src/core/bench/qrels-file.ts` + `src/core/bench/correctness-gate.ts` + `src/commands/bench-publish.ts` + `src/commands/eval-gate.ts` (v0.41.0.0) — the eval-loop wave. Two new CLI verbs: `gbrain bench publish --from <captured.ndjson> --to <X.baseline.ndjson>` writes a baseline file from `gbrain eval export` output (stamps stable `query_hash` on every row; metadata header carries `_kind: 'baseline_metadata'` + thresholds + `source_hash` + `baseline_mean_latency_ms`; deterministic sort by `(tool_name, query_hash)`; D4 strict posture — empty=fail, dupes=fail with paste-ready hint, `--to` exists=refuse without `--force`); `gbrain eval gate [--baseline X] [--qrels Y]` is the two-gate dispatcher (regression gate via in-process `replayCore`, correctness gate via bare `hybridSearch` for determinism per eng-D6, both must pass when both flags set, exit 0 PASS / 1 FAIL / 2 USAGE). Source-id-aware: `bench publish` dedup key is `(tool_name, source_ids, query_hash)`; qrels compare keys are `${source_id}::${slug}` everywhere; closes the multi-source-bug-class (canonical gbrain pitfall) at the file-shape layer. Latency math: `(baseline + delta) / baseline <= multiplier` (the original `delta / baseline` formula would have let 2.5x slowdowns pass at multiplier=2.0). D3 fail-closed: ANY in-process throw flips verdict to fail with named breach in `breaches[]` — never silently exit 0. `.qrels.json` shape preserves the existing 12-row `test/fixtures/eval-baselines/qrels-search.json` fixture (slug-only `relevant_slugs` + `first_relevant_slug` auto-promote to `source_id='default'`) AND supports the federated shape (explicit `relevant: [{source_id, slug}]` + `expected_top1`). `correctness-gate.ts` runs each qrels query via bare `hybridSearch`; per-query throw recorded as `errored: true` and flagged as gate failure (Finding 2D). Audit JSONL for bench-publish at `~/.gbrain/audit/bench-publish-YYYY-Www.jsonl`. Plan + 23 decisions + 2 codex outside-voice rounds at `~/.claude/plans/system-instruction-you-are-working-rustling-peacock.md`. Pinned by 65 cases across `test/bench/baseline-file.test.ts` (9) + `test/bench/qrels-file.test.ts` (19) + `test/bench/correctness-gate.test.ts` (6) + `test/bench-publish.test.ts` (10) + `test/eval-gate.test.ts` (10) + `test/eval-replay-metadata-skip.test.ts` (2) + `test/cycle/nightly-probe-adapters.test.ts` (6) + `test/autopilot-nightly-probe-wiring.test.ts` (8) + `test/e2e/eval-loop.test.ts` (4).+- `src/core/cycle/nightly-probe-adapters.ts` (v0.41.0.0) — bridges the autopilot's object-shape `NightlyProbeDeps` to the existing argv-shape `runEvalLongMemEval` + `runEvalCrossModal` CLI functions. Cross-modal adapter argv MUST include `--output summaryPath` (codex round-2 #1 — without it the summary lands at the default receipt path and the adapter would read nothing from `summaryPath`). In-process invocation (NOT subprocess) — avoids gbrain-version-drift class for source-tree CI runs. ~125 LOC. Pinned by `test/cycle/nightly-probe-adapters.test.ts` (6 cases including argv-shape regression for the `--output` fix).+- `src/commands/autopilot.ts` extension (v0.41.0.0) — tick body invokes `runNightlyQualityProbe` when `cfg.autopilot.nightly_quality_probe.enabled === true` (default OFF — opt-in to protect API spend on fresh installs). Per eng-D10 (codex round-1 #11): NO scheduler-side rate-limit check — `runNightlyQualityProbe`'s internal `shouldRunNightly` (reading the audit JSONL) is the single source of truth. Probe call wrapped in try/catch that logs via `logError` and DOES NOT bump `consecutiveErrors` (probe failure is informational, never crashes the loop). Default `max_usd` cap = 5. Source-shape pinned by `test/autopilot-nightly-probe-wiring.test.ts` (8 regression assertions).- `test/eval-replay-gate.test.ts` + `test/fixtures/eval-baselines/qrels-search.json` (v0.40.1.0 Track D / T5, per D8 reshape) — hermetic retrieval qrels gate that runs in the standard PR unit-shard CI matrix (`.github/workflows/test.yml`, NOT the fixed-file E2E workflow). Structural replacement for the original "replay against captured `eval_candidates` baseline" design (deferred to `v0.41+: contributor-mode CI capture` in TODOS.md; Codex outside-voice caught three fatal flaws — select-e2e.ts is local-only, eval-export bypasses op-layer capture on PGLite-seed tests, replay re-embeds via gateway which needs an API key CI doesn't have). Uses the canonical PGLite block from CLAUDE.md test-isolation rules (R3+R4) and the basis-vector embedding pattern from `test/e2e/search-quality.test.ts:23-28` for fully hermetic retrieval. The qrels fixture (12 queries) is hand-curated with PLACEHOLDER names only (alice-example, widget-co-example, etc. — per Codex #9 + CLAUDE.md privacy rule) and embeds each query at a deterministic basis dimension so retrieval is reproducible. Each query lists `relevant_slugs[]` + `first_relevant_slug`; for each, the test computes `top1_match_rate` (top-1 == first_relevant) and `recall@10` (fraction of relevant_slugs in top-10), asserting both meet floors (defaults `>= 0.80` and `>= 0.85`). Env-overridable floors via `GBRAIN_REPLAY_GATE_TOP1_FLOOR` / `GBRAIN_REPLAY_GATE_RECALL_FLOOR` (with `withEnv()` per CLAUDE.md R1). Refresh discipline (per D4): when ranking changes intentionally move expected slugs, edit `qrels-search.json` directly and include a `Why:` line in the commit body so future maintainers can read the audit trail. Without `Why:`, the gate degrades to rubber-stamp within months. Pinned by 5 cases in `test/eval-replay-gate.test.ts` including a privacy-grep regression guard against real-name reintroduction.- `src/core/cycle/nightly-quality-probe.ts` + `src/core/audit-quality-probe.ts` + `test/fixtures/longmemeval-nightly.jsonl` + `test/nightly-quality-probe.test.ts` (v0.40.1.0 Track D / T6+T7+T8, per D12) — opt-in nightly cross-modal quality probe. The phase runs `gbrain eval longmemeval --by-type` against the committed 10-question placeholder fixture, pipes the output through `gbrain eval cross-modal --batch --max-usd 5 --yes`, and writes one event per run to `~/.gbrain/audit/quality-probe-YYYY-Www.jsonl` (ISO-week-rotated, mirrors `audit-slug-fallback.ts`; honors `GBRAIN_AUDIT_DIR`). Default DISABLED — opt-in via `gbrain config set autopilot.nightly_quality_probe.enabled true` (prevents surprise API spend on `gbrain init`). 24h rate limit (the pure `shouldRunNightly(now, recentEvents, windowMs?)` function) skips with audit row `outcome: rate_limited` when a recent run exists. Embedding-key short-circuit: longmemeval needs `gateway.embedQuery()`, so the phase exits early with `outcome: no_embedding_key` + stderr warn when no provider is configured. Full DI surface via `NightlyProbeDeps` (`isEnabled`, `hasEmbeddingProvider`, `resolveMaxUsd`, `resolveRepoRoot`, `runLongMemEval`, `runCrossModalBatch`, `now`) so the unit test stubs every external effect — no PGLite, no real LLM calls, no env mutation outside `withEnv()`. Cost ceiling: $5/run × 30 nights ≈ $150/month worst-case; expected real cost ~$0.35/night × 30 ≈ $10.50/month. **Autopilot scheduler wiring deferred to v0.41+ follow-up (filed in TODOS.md)** — the phase is callable in isolation today; the cycle-loop dispatcher hasn't been wired to invoke it on the 24h cadence yet. New `nightly_quality_probe_health` doctor check (in `src/commands/doctor.ts` right after `slug_fallback_audit`) reads the last 7 days of audit events: SKIPPED when feature flag is off (with paste-ready enable command); OK when enabled + all PASS; WARN on any FAIL / ERROR / BUDGET_EXCEEDED in the window with per-outcome counts. Pinned by 14 cases in `test/nightly-quality-probe.test.ts` (rate-limit pure-function unit tests + DI-stubbed end-to-end phase tests across every outcome branch).- `src/commands/eval-trajectory.ts` + `src/commands/founder-scorecard.ts` + `src/core/trajectory.ts` (v0.35.7) — temporal trajectory + founder scorecard. The wave that turns the v0.35.3.1 date-aware contradiction probe into a useful temporal substrate. `gbrain eval trajectory <entity>` shows the chronological typed-claim history (mrr/arr/team_size/etc) with regressions auto-flagged inline; `gbrain founder scorecard <entity>` rolls up claim_accuracy / consistency / growth_trajectory / red_flags into one JSON. Pure-function math lives in `trajectory.ts`: `detectRegressions(points, threshold)` walks consecutive metric-value pairs per metric (10% drop default, env override `GBRAIN_TRAJECTORY_REGRESSION_THRESHOLD`); `computeDriftScore(points)` returns `1 - mean(cosine(emb[i], emb[i-1]))` over existing embeddings (null when <3 embedded points). Backed by `BrainEngine.findTrajectory(opts)` — both Postgres and PGLite, single SQL query, deterministic `ORDER BY valid_from ASC, id ASC` (R3). Source-scoped via the v0.34.1.0 `sourceId` scalar / `sourceIds` array dual pattern (D-CDX-6); visibility-filtered for remote callers (D-CDX-1) — `recall`-equivalent posture. MCP op `find_trajectory` (read scope, NOT localOnly) registered after `find_experts`. Migration v67 adds four optional typed-claim columns (`claim_metric`, `claim_value`, `claim_unit`, `claim_period`) + a partial index on `(entity_slug, claim_metric, valid_from) WHERE claim_metric IS NOT NULL`. Fence widens from 10 to 14 cells when any row has typed data; renderer stays at 10 cells when none do (no churn diff on existing fences). Metric labels normalize to lowercase snake_case via `normalizeMetricLabel` (15-entry seed map for common founder metrics). The `consolidate` cycle phase gains semantic upsert keyed on `(page_id, claim, since_date)` — fixes the pre-existing F4 duplicate-takes bug where re-running the full cycle after `extract_facts` cleared `consolidated_at` would silently append duplicate takes via `MAX(row_num)+1`. Also writes chronological `valid_until` on each cluster's older facts. The `extract_facts` cycle phase batch-embeds via `gateway.embed()` before insert AND threads `pages.effective_date` as the `pageEffectiveDate` fallback for `valid_from` (precedence chain: fence-row > pageEffectiveDate > now()). The contradiction probe MUST NOT write `valid_until` — R1+R8 grep guard at `test/eval-contradictions/no-valid-until-write.test.ts` pins this. Codex outside-voice round caught F1 (v66 collision → v67), F2 (Haiku lives in `facts/extract.ts` not `extract-facts.ts` cycle phase), F3 (cycle didn't embed before insert), F4 (idempotency bug), F5+F6 (missed `fence-write.ts` caller + no Page object there → pageEffectiveDate is OPTIONAL), F7 (privacy regression — visibility filter added), F8 (ParsedFact needed typed-field extension for markdown system-of-record), F9 (dual scalar+federated sourceId). Plan: `~/.claude/plans/system-instruction-you-are-working-curious-jellyfish.md`. Tests: 258 across 12 files. **v0.40.2.0 (migration v89 `facts_event_type_column`):** `facts` table gains a nullable `event_type TEXT` column so the typed-claim substrate can carry event-shaped rows (`event_type='meeting'`, `'job_change'`, `'location_change'`) alongside metric-shaped rows. `TrajectoryPoint.event_type: string | null` projected by both PGLite and Postgres `findTrajectory` paths. `TrajectoryOpts.kind?: 'metric' | 'event' | 'all'` filter added (default `'all'`). Existing callers (`founder-scorecard`, `eval-trajectory`) pass `kind: 'metric'` explicitly for call-site clarity — no behavior change since both already defensively skipped NULL-metric rows in their per-metric math. Back-compat pinned by `test/regressions/v0_40_2_0-trajectory-backcompat.test.ts` (4 cases: byte-identical `computeFounderScorecard` + `computeTrajectoryStats` output with and without event rows). Engine parity in `test/engine-parity-event-type.test.ts` (6 cases). Plan: `~/.claude/plans/system-instruction-you-are-working-crystalline-owl.md`.diff --git a/TODOS.md b/TODOS.mdindex 60849ca369..14e9fbedf1 100644--- a/TODOS.md+++ b/TODOS.md@@ -170,12 +170,17 @@ signal. Plan file: `~/.claude/plans/system-instruction-you-are-working-dazzling-Three strategic decisions landed and the 7 verified-absent items theanalysis surfaced were approved for filing.-### D1 — v0.41 Eval-loop wave (NEXT, P0)+### D1 — v0.41 Eval-loop wave (LANDED v0.41.0.0, scope reshaped)-The eval/quality-gate cluster has all the substrate (eval_candidates table,-`eval export/replay`, cross-modal runner, nightly probe, audit JSONL) but-the LOOP is barely live. Three blocking moves turn "gbrain has eval infra"-into "gbrain is self-improving."+**Status:** Shipped in v0.41.0.0 (2026-05-24). CEO+Eng review reshaped the+original 3-item slice: items 1 + 3 (autopilot wiring + `gbrain eval gate`)+shipped as planned + EXPANDED with a correctness gate (qrels-based recall@K++ first-relevant-hit-rate) and a `gbrain bench publish` verb that closes the+LOOP by giving captured data a destination. Item 2 (capture-default flip)+deferred to v0.42 because the flip is a one-way door and shouldn't ship+before the destination exists.++The original 3 items as filed (kept for traceability):- [ ] **P0 — `gbrain eval gate <baseline.ndjson>` for CI.** The single mostload-bearing missing item across all 12 clusters. Fails the build on@@ -319,6 +324,48 @@ cleanup can move each into the relevant area section.doesn't. (Embedding cluster.)---+## v0.41 Eval-loop wave follow-ups (v0.42+)++Filed during v0.41 CEO + Eng review (D11-D13). All three landed via codex+outside-voice triage on the reshaped plan.++- [ ] **v0.42 P1: capture-default flip + scrubber hardening.** Flip+ `eval.capture` default from OFF to ON. Harden `src/core/eval-capture-scrub.ts`+ with AWS access key (`AKIA[0-9A-Z]{16}`), GitHub PAT (`ghp_[A-Za-z0-9]{36}`),+ and generic API-key-suffix patterns. Add first-run stderr banner with+ `gbrain eval capture off` opt-out hint and persistent+ `eval.capture_acknowledged` config flag (banner fires once per acked-false).+ Two new CLI verbs: `gbrain eval capture on|off|status` + `acknowledge`.+ Dependency: v0.41 LOOP (this wave) has shipped + been used for at least+ a month so the destination story is real. Filed during v0.41 CEO review+ per D11 after the original wave plan was reshaped by codex outside-voice+ to defer this item.++- [ ] **v0.42-v0.43 P2: `gbrain bench publish --suggest-thresholds`.**+ Reads the last 30 days of `eval gate` JSON outputs (from gbrain-evals+ CI artifacts or `~/.gbrain/audit/bench-publish-*.jsonl`), computes p10+ of each metric across passes, suggests those as thresholds. Starting-+ guess thresholds in v0.41 (regression: jaccard 0.85 / top1 0.80 /+ latency_multiplier 2.0; correctness: recall@10 0.70 /+ first_relevant_hit_rate 0.60 / expected_top1 0.50) are either too tight+ or too loose; data informs the heuristic. Dependency: 30+ days of gate+ runs accumulating. Filed during v0.41 CEO review per D12.++- [ ] **v0.42+ P3: `gbrain bench diff` + `gbrain bench list`.**+ `bench diff <a.baseline.ndjson> <b.baseline.ndjson>` — visual diff of+ two baselines showing which queries changed top-1 retrieval, which+ lost relevant_slugs, which gained. `bench list [--dir <path>]` — lists+ baselines with metadata (label, published_at, row_count, source_hash);+ defaults to `~/.gbrain/baselines/` + `gbrain-evals/baselines/` if both+ exist. Trivial; ship when there's >1 baseline to look at. Filed during+ v0.41 CEO review per D13.++- [ ] **v0.42+: ship the coordinated `gbrain-evals/baselines/v0.41-launch.baseline.ndjson`+ + `gbrain-evals/qrels/v0.41-launch.qrels.json` (hermetic-synthetic per D9).**+ Generate locally via `gbrain bench publish --from <hermetic-test-corpus>` then+ commit to the sibling gbrain-evals repo. Gives `gbrain eval gate` a canonical+ baseline target so users don't have to bootstrap their own immediately.+## v0.40.7.0 Schema Cathedral v3 follow-ups (v0.40.7+)These were filed when v0.40.7.0 closed PR #1321's design as a productiondiff --git a/VERSION b/VERSIONindex 7ee8e6a088..3e8954862c 100644--- a/VERSION+++ b/VERSION@@ -1 +1 @@-0.41.0.0\ No newline at end of file+0.41.1.0\ No newline at end of filediff --git a/docs/eval-bench.md b/docs/eval-bench.mdindex 8d35ef62d1..85cf9f8ab9 100644--- a/docs/eval-bench.md+++ b/docs/eval-bench.md@@ -8,6 +8,112 @@ For the **NDJSON wire format** consumed by gbrain-evals, see[`eval-capture.md`](./eval-capture.md). This doc is the human dev loopthat lives on top of that format.+## v0.41 update — the LOOP is now real++Before v0.41, you could capture eval rows and replay them but nothing+stitched them into a gate. `gbrain bench publish` + `gbrain eval gate`+close the loop. Two gates:++- **Regression gate** (`--baseline X.baseline.ndjson`): replays a baseline+ you captured against your current brain. Catches: "did my refactor break+ search?" Compares jaccard / top-1 stability / latency multiplier.+- **Correctness gate** (`--qrels Y.qrels.json`): runs known-right queries+ against your current brain via bare `hybridSearch`. Catches: "is my+ retrieval actually any good?" Computes recall@K, first-relevant-hit-rate,+ expected_top1-hit-rate.++Both can be passed together; both must pass for verdict `pass`. At least+one is required.++### The full LOOP for your own brain++```bash+# 1. Capture (one-time; uses queries already in eval_candidates)+gbrain eval export --limit 200 --tool query > /tmp/captured.ndjson++# 2. Publish a baseline+mkdir -p ~/.gbrain/baselines+gbrain bench publish --from /tmp/captured.ndjson --to ~/.gbrain/baselines/personal.baseline.ndjson --label "personal-$(date +%Y%m%d)"++# 3. Gate against it+gbrain eval gate --baseline ~/.gbrain/baselines/personal.baseline.ndjson+```++### Privacy posture (D9)++**Public baselines in `gbrain-evals` are hermetic-synthetic ONLY.** Real+user captures stay local in `~/.gbrain/baselines/`. The boundary is+enforced at the file source, not by post-hoc scrubbing. If you publish a+baseline to `gbrain-evals`, generate it from a fixture-seeded test brain+(placeholder names like `alice-example`, `widget-co-example`) — never+from a real user's `eval_candidates` table.++### Deterministic-pipeline disclosure++`gbrain eval gate --qrels` uses bare `hybridSearch` (not the production+`query` op handler). This is deliberate: gates need to be deterministic in+CI. Production retrieval differs via the query cache, salience freshness,+expansion, etc. The gate measures retrieval quality with a fixed pipeline;+your users may see different results when the cache is warm.++### `.qrels.json` shape++Two equivalent representations per entry:++```json+{+ "schema_version": 1,+ "queries": [+ {+ "query_id": "q1",+ "query": "fintech founder",+ "relevant_slugs": ["people/alice-example"],+ "first_relevant_slug": "people/alice-example"+ }+ ]+}+```++For federated / multi-source brains, use the explicit shape (no defaults+to `source_id='default'`):++```json+{+ "query_id": "q2",+ "query": "anything",+ "relevant": [+ {"source_id": "host", "slug": "people/alice"},+ {"source_id": "team-a", "slug": "people/alice"}+ ],+ "expected_top1": {"source_id": "host", "slug": "people/alice"}+}+```++Without `source_id`, a hit from the wrong source could false-pass the+gate. The compare everywhere is `${source_id}::${slug}` strings.++### Example GitHub Actions workflow++```yaml+name: gbrain-eval-gate+on: [pull_request]+jobs:+ gate:+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4+ - uses: oven-sh/setup-bun@v2+ - run: bun install+ - run: |+ # Run both gates; CI fails on any breach.+ gbrain eval gate \+ --baseline gbrain-evals/baselines/v0.41-launch.baseline.ndjson \+ --qrels gbrain-evals/qrels/v0.41-launch.qrels.json \+ --json | tee /tmp/gate.json+```++---+## Prerequisite: turn on contributor modeCapture is **off by default** for production users (privacy-positive — nodiff --git a/llms-full.txt b/llms-full.txtindex f68b11dcbc..05c16ec801 100644--- a/llms-full.txt+++ b/llms-full.txt@@ -233,7 +233,10 @@ strict behavior when unset.- `src/core/cross-modal-eval/receipt-write.ts` (v0.27.x) — wraps `fs.writeFileSync` with `mkdirSync({recursive:true})` ahead of every write (T5 correction; `gbrainPath()` does NOT auto-mkdir).- `src/commands/eval-export.ts` (v0.25.0) — streams `eval_candidates` rows as NDJSON to stdout with `schema_version: 1` prefix on every line. EPIPE-safe, progress heartbeats on stderr, stable id-desc tiebreaker so `--since` windows never dupe/miss rows.- `src/commands/eval-prune.ts` (v0.25.0) — explicit retention cleanup. Requires `--older-than DUR`. `--dry-run` reports would-delete count.-- `src/commands/eval-replay.ts` (v0.25.0) — contributor-facing replay tool. Reads NDJSON from `gbrain eval export`, re-runs each captured `query` / `search` op against the current brain, computes set-Jaccard@k between captured + current `retrieved_slugs`, top-1 stability rate, and latency Δ. Stable JSON shape (`schema_version: 1`) for CI gating; human mode prints a regression table. Pure Bun, zero new deps. The dev-loop half of BrainBench-Real that closes the gap between "data captured" and "data used to gate a PR." See `docs/eval-bench.md` for the workflow.+- `src/commands/eval-replay.ts` (v0.25.0, extended v0.41.0.0) — contributor-facing replay tool. Reads NDJSON from `gbrain eval export`, re-runs each captured `query` / `search` op against the current brain, computes set-Jaccard@k between captured + current `retrieved_slugs`, top-1 stability rate, and latency Δ. Stable JSON shape (`schema_version: 1`) for CI gating; human mode prints a regression table. Pure Bun, zero new deps. The dev-loop half of BrainBench-Real that closes the gap between "data captured" and "data used to gate a PR." See `docs/eval-bench.md` for the workflow. **v0.41.0.0 changes:** `parseNdjson` now skips lines where `_kind === 'baseline_metadata'` so `gbrain bench publish`-written baselines parse cleanly without the metadata header polluting row counts. New exported `replayCore(engine, opts): Promise<{summary, results}>` programmatic entrypoint + exported `ReplaySummary` type so `gbrain eval gate` can call replay in-process (NOT spawn subprocess — avoids gbrain-version-drift for source-tree CI runs). The existing CLI `runEvalReplay` now wraps `replayCore`.+- `src/core/bench/baseline-file.ts` + `src/core/bench/qrels-file.ts` + `src/core/bench/correctness-gate.ts` + `src/commands/bench-publish.ts` + `src/commands/eval-gate.ts` (v0.41.0.0) — the eval-loop wave. Two new CLI verbs: `gbrain bench publish --from <captured.ndjson> --to <X.baseline.ndjson>` writes a baseline file from `gbrain eval export` output (stamps stable `query_hash` on every row; metadata header carries `_kind: 'baseline_metadata'` + thresholds + `source_hash` + `baseline_mean_latency_ms`; deterministic sort by `(tool_name, query_hash)`; D4 strict posture — empty=fail, dupes=fail with paste-ready hint, `--to` exists=refuse without `--force`); `gbrain eval gate [--baseline X] [--qrels Y]` is the two-gate dispatcher (regression gate via in-process `replayCore`, correctness gate via bare `hybridSearch` for determinism per eng-D6, both must pass when both flags set, exit 0 PASS / 1 FAIL / 2 USAGE). Source-id-aware: `bench publish` dedup key is `(tool_name, source_ids, query_hash)`; qrels compare keys are `${source_id}::${slug}` everywhere; closes the multi-source-bug-class (canonical gbrain pitfall) at the file-shape layer. Latency math: `(baseline + delta) / baseline <= multiplier` (the original `delta / baseline` formula would have let 2.5x slowdowns pass at multiplier=2.0). D3 fail-closed: ANY in-process throw flips verdict to fail with named breach in `breaches[]` — never silently exit 0. `.qrels.json` shape preserves the existing 12-row `test/fixtures/eval-baselines/qrels-search.json` fixture (slug-only `relevant_slugs` + `first_relevant_slug` auto-promote to `source_id='default'`) AND supports the federated shape (explicit `relevant: [{source_id, slug}]` + `expected_top1`). `correctness-gate.ts` runs each qrels query via bare `hybridSearch`; per-query throw recorded as `errored: true` and flagged as gate failure (Finding 2D). Audit JSONL for bench-publish at `~/.gbrain/audit/bench-publish-YYYY-Www.jsonl`. Plan + 23 decisions + 2 codex outside-voice rounds at `~/.claude/plans/system-instruction-you-are-working-rustling-peacock.md`. Pinned by 65 cases across `test/bench/baseline-file.test.ts` (9) + `test/bench/qrels-file.test.ts` (19) + `test/bench/correctness-gate.test.ts` (6) + `test/bench-publish.test.ts` (10) + `test/eval-gate.test.ts` (10) + `test/eval-replay-metadata-skip.test.ts` (2) + `test/cycle/nightly-probe-adapters.test.ts` (6) + `test/autopilot-nightly-probe-wiring.test.ts` (8) + `test/e2e/eval-loop.test.ts` (4).+- `src/core/cycle/nightly-probe-adapters.ts` (v0.41.0.0) — bridges the autopilot's object-shape `NightlyProbeDeps` to the existing argv-shape `runEvalLongMemEval` + `runEvalCrossModal` CLI functions. Cross-modal adapter argv MUST include `--output summaryPath` (codex round-2 #1 — without it the summary lands at the default receipt path and the adapter would read nothing from `summaryPath`). In-process invocation (NOT subprocess) — avoids gbrain-version-drift class for source-tree CI runs. ~125 LOC. Pinned by `test/cycle/nightly-probe-adapters.test.ts` (6 cases including argv-shape regression for the `--output` fix).+- `src/commands/autopilot.ts` extension (v0.41.0.0) — tick body invokes `runNightlyQualityProbe` when `cfg.autopilot.nightly_quality_probe.enabled === true` (default OFF — opt-in to protect API spend on fresh installs). Per eng-D10 (codex round-1 #11): NO scheduler-side rate-limit check — `runNightlyQualityProbe`'s internal `shouldRunNightly` (reading the audit JSONL) is the single source of truth. Probe call wrapped in try/catch that logs via `logError` and DOES NOT bump `consecutiveErrors` (probe failure is informational, never crashes the loop). Default `max_usd` cap = 5. Source-shape pinned by `test/autopilot-nightly-probe-wiring.test.ts` (8 regression assertions).- `test/eval-replay-gate.test.ts` + `test/fixtures/eval-baselines/qrels-search.json` (v0.40.1.0 Track D / T5, per D8 reshape) — hermetic retrieval qrels gate that runs in the standard PR unit-shard CI matrix (`.github/workflows/test.yml`, NOT the fixed-file E2E workflow). Structural replacement for the original "replay against captured `eval_candidates` baseline" design (deferred to `v0.41+: contributor-mode CI capture` in TODOS.md; Codex outside-voice caught three fatal flaws — select-e2e.ts is local-only, eval-export bypasses op-layer capture on PGLite-seed tests, replay re-embeds via gateway which needs an API key CI doesn't have). Uses the canonical PGLite block from CLAUDE.md test-isolation rules (R3+R4) and the basis-vector embedding pattern from `test/e2e/search-quality.test.ts:23-28` for fully hermetic retrieval. The qrels fixture (12 queries) is hand-curated with PLACEHOLDER names only (alice-example, widget-co-example, etc. — per Codex #9 + CLAUDE.md privacy rule) and embeds each query at a deterministic basis dimension so retrieval is reproducible. Each query lists `relevant_slugs[]` + `first_relevant_slug`; for each, the test computes `top1_match_rate` (top-1 == first_relevant) and `recall@10` (fraction of relevant_slugs in top-10), asserting both meet floors (defaults `>= 0.80` and `>= 0.85`). Env-overridable floors via `GBRAIN_REPLAY_GATE_TOP1_FLOOR` / `GBRAIN_REPLAY_GATE_RECALL_FLOOR` (with `withEnv()` per CLAUDE.md R1). Refresh discipline (per D4): when ranking changes intentionally move expected slugs, edit `qrels-search.json` directly and include a `Why:` line in the commit body so future maintainers can read the audit trail. Without `Why:`, the gate degrades to rubber-stamp within months. Pinned by 5 cases in `test/eval-replay-gate.test.ts` including a privacy-grep regression guard against real-name reintroduction.- `src/core/cycle/nightly-quality-probe.ts` + `src/core/audit-quality-probe.ts` + `test/fixtures/longmemeval-nightly.jsonl` + `test/nightly-quality-probe.test.ts` (v0.40.1.0 Track D / T6+T7+T8, per D12) — opt-in nightly cross-modal quality probe. The phase runs `gbrain eval longmemeval --by-type` against the committed 10-question placeholder fixture, pipes the output through `gbrain eval cross-modal --batch --max-usd 5 --yes`, and writes one event per run to `~/.gbrain/audit/quality-probe-YYYY-Www.jsonl` (ISO-week-rotated, mirrors `audit-slug-fallback.ts`; honors `GBRAIN_AUDIT_DIR`). Default DISABLED — opt-in via `gbrain config set autopilot.nightly_quality_probe.enabled true` (prevents surprise API spend on `gbrain init`). 24h rate limit (the pure `shouldRunNightly(now, recentEvents, windowMs?)` function) skips with audit row `outcome: rate_limited` when a recent run exists. Embedding-key short-circuit: longmemeval needs `gateway.embedQuery()`, so the phase exits early with `outcome: no_embedding_key` + stderr warn when no provider is configured. Full DI surface via `NightlyProbeDeps` (`isEnabled`, `hasEmbeddingProvider`, `resolveMaxUsd`, `resolveRepoRoot`, `runLongMemEval`, `runCrossModalBatch`, `now`) so the unit test stubs every external effect — no PGLite, no real LLM calls, no env mutation outside `withEnv()`. Cost ceiling: $5/run × 30 nights ≈ $150/month worst-case; expected real cost ~$0.35/night × 30 ≈ $10.50/month. **Autopilot scheduler wiring deferred to v0.41+ follow-up (filed in TODOS.md)** — the phase is callable in isolation today; the cycle-loop dispatcher hasn't been wired to invoke it on the 24h cadence yet. New `nightly_quality_probe_health` doctor check (in `src/commands/doctor.ts` right after `slug_fallback_audit`) reads the last 7 days of audit events: SKIPPED when feature flag is off (with paste-ready enable command); OK when enabled + all PASS; WARN on any FAIL / ERROR / BUDGET_EXCEEDED in the window with per-outcome counts. Pinned by 14 cases in `test/nightly-quality-probe.test.ts` (rate-limit pure-function unit tests + DI-stubbed end-to-end phase tests across every outcome branch).- `src/commands/eval-trajectory.ts` + `src/commands/founder-scorecard.ts` + `src/core/trajectory.ts` (v0.35.7) — temporal trajectory + founder scorecard. The wave that turns the v0.35.3.1 date-aware contradiction probe into a useful temporal substrate. `gbrain eval trajectory <entity>` shows the chronological typed-claim history (mrr/arr/team_size/etc) with regressions auto-flagged inline; `gbrain founder scorecard <entity>` rolls up claim_accuracy / consistency / growth_trajectory / red_flags into one JSON. Pure-function math lives in `trajectory.ts`: `detectRegressions(points, threshold)` walks consecutive metric-value pairs per metric (10% drop default, env override `GBRAIN_TRAJECTORY_REGRESSION_THRESHOLD`); `computeDriftScore(points)` returns `1 - mean(cosine(emb[i], emb[i-1]))` over existing embeddings (null when <3 embedded points). Backed by `BrainEngine.findTrajectory(opts)` — both Postgres and PGLite, single SQL query, deterministic `ORDER BY valid_from ASC, id ASC` (R3). Source-scoped via the v0.34.1.0 `sourceId` scalar / `sourceIds` array dual pattern (D-CDX-6); visibility-filtered for remote callers (D-CDX-1) — `recall`-equivalent posture. MCP op `find_trajectory` (read scope, NOT localOnly) registered after `find_experts`. Migration v67 adds four optional typed-claim columns (`claim_metric`, `claim_value`, `claim_unit`, `claim_period`) + a partial index on `(entity_slug, claim_metric, valid_from) WHERE claim_metric IS NOT NULL`. Fence widens from 10 to 14 cells when any row has typed data; renderer stays at 10 cells when none do (no churn diff on existing fences). Metric labels normalize to lowercase snake_case via `normalizeMetricLabel` (15-entry seed map for common founder metrics). The `consolidate` cycle phase gains semantic upsert keyed on `(page_id, claim, since_date)` — fixes the pre-existing F4 duplicate-takes bug where re-running the full cycle after `extract_facts` cleared `consolidated_at` would silently append duplicate takes via `MAX(row_num)+1`. Also writes chronological `valid_until` on each cluster's older facts. The `extract_facts` cycle phase batch-embeds via `gateway.embed()` before insert AND threads `pages.effective_date` as the `pageEffectiveDate` fallback for `valid_from` (precedence chain: fence-row > pageEffectiveDate > now()). The contradiction probe MUST NOT write `valid_until` — R1+R8 grep guard at `test/eval-contradictions/no-valid-until-write.test.ts` pins this. Codex outside-voice round caught F1 (v66 collision → v67), F2 (Haiku lives in `facts/extract.ts` not `extract-facts.ts` cycle phase), F3 (cycle didn't embed before insert), F4 (idempotency bug), F5+F6 (missed `fence-write.ts` caller + no Page object there → pageEffectiveDate is OPTIONAL), F7 (privacy regression — visibility filter added), F8 (ParsedFact needed typed-field extension for markdown system-of-record), F9 (dual scalar+federated sourceId). Plan: `~/.claude/plans/system-instruction-you-are-working-curious-jellyfish.md`. Tests: 258 across 12 files. **v0.40.2.0 (migration v89 `facts_event_type_column`):** `facts` table gains a nullable `event_type TEXT` column so the typed-claim substrate can carry event-shaped rows (`event_type='meeting'`, `'job_change'`, `'location_change'`) alongside metric-shaped rows. `TrajectoryPoint.event_type: string | null` projected by both PGLite and Postgres `findTrajectory` paths. `TrajectoryOpts.kind?: 'metric' | 'event' | 'all'` filter added (default `'all'`). Existing callers (`founder-scorecard`, `eval-trajectory`) pass `kind: 'metric'` explicitly for call-site clarity — no behavior change since both already defensively skipped NULL-metric rows in their per-metric math. Back-compat pinned by `test/regressions/v0_40_2_0-trajectory-backcompat.test.ts` (4 cases: byte-identical `computeFounderScorecard` + `computeTrajectoryStats` output with and without event rows). Engine parity in `test/engine-parity-event-type.test.ts` (6 cases). Plan: `~/.claude/plans/system-instruction-you-are-working-crystalline-owl.md`.diff --git a/package.json b/package.jsonindex 9962924599..bf66f075b7 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "gbrain",- "version": "0.41.0.0",+ "version": "0.41.1.0","description": "Postgres-native personal knowledge brain with hybrid RAG search","type": "module","main": "src/core/index.ts",diff --git a/src/commands/autopilot.ts b/src/commands/autopilot.tsindex 4dbd38bf34..0c2c2b66e4 100644--- a/src/commands/autopilot.ts+++ b/src/commands/autopilot.ts@@ -645,6 +645,36 @@ export async function runAutopilot(engine: BrainEngine, args: string[]) {}}+ // 4.5 — Nightly quality probe (v0.41).+ // Per D10: trust the phase's internal 24h rate-limit (via shouldRunNightly+ // reading the audit JSONL). No scheduler-side precheck — one source of+ // truth for the rate-limit. Feature flag gates the probe entirely.+ // Wrapped in try/catch — a probe failure NEVER crashes the autopilot+ // loop. Probe runs even when cycleOk=false (probe may surface signal+ // explaining why the cycle is failing).+ try {+ const probeEnabled = cfg?.autopilot?.nightly_quality_probe?.enabled === true;+ if (probeEnabled) {+ const { runNightlyQualityProbe } = await import('../core/cycle/nightly-quality-probe.ts');+ const { runLongMemEvalForProbe, runCrossModalBatchForProbe } = await import('../core/cycle/nightly-probe-adapters.ts');+ const { isAvailable } = await import('../core/ai/gateway.ts');+ const maxUsd = Number(cfg?.autopilot?.nightly_quality_probe?.max_usd ?? 5);+ await runNightlyQualityProbe({+ isEnabled: () => true, // already gated above; phase re-checks for defense-in-depth+ hasEmbeddingProvider: () => isAvailable('embedding'),+ resolveMaxUsd: () => maxUsd,+ resolveRepoRoot: () => repoPath ?? gbrainHomePath('.'),+ runLongMemEval: runLongMemEvalForProbe,+ runCrossModalBatch: runCrossModalBatchForProbe,+ now: () => new Date(),+ });+ }+ } catch (e) {+ logError('autopilot.nightly_probe', e);+ // Intentional: do NOT bump consecutiveErrors. Probe failure is+ // informational; autopilot loop continues.+ }+// Wait for next cycleawait new Promise(r => setTimeout(r, interval * 1000));}diff --git a/src/commands/bench-publish.ts b/src/commands/bench-publish.tsnew file mode 100644index 0000000000..dfd9899645--- /dev/null+++ b/src/commands/bench-publish.ts@@ -0,0 +1,384 @@+/**+ * gbrain bench publish — turn a captured-run NDJSON into a baseline file (v0.41).+ *+ * The LOOP-closing verb. Without `bench publish`, the eval gate has nothing+ * to gate against except hand-curated fixtures. This verb takes the output+ * of `gbrain eval export` and writes a `*.baseline.ndjson` file with:+ * - line 1: metadata header (label, thresholds, source_hash, baseline_mean_latency_ms)+ * - lines 2..N: raw captured rows with `query_hash` stamped on each+ *+ * Strict posture (per CEO D4 + eng D5):+ * - Empty input → exit 1 with "no rows to publish (empty input)".+ * - Duplicate (tool_name, source_ids, query_hash) rows → exit 1 listing the+ * first 5 dupes + paste-ready dedup hint. source_ids in the dedup key+ * per eng-D5 (multi-source brains).+ * - --to path exists → exit 2 (USAGE) unless --force.+ *+ * NOT streaming. Real baselines are <10K rows (codex round-1 #8 — streaming+ * + sort + hash all together is impossible). In-memory read + sort + write.+ *+ * Usage:+ * gbrain bench publish --from <captured.ndjson> --to <X.baseline.ndjson>+ * [--threshold-jaccard FLOAT] [--threshold-top1 FLOAT]+ * [--threshold-latency-multiplier FLOAT] [--label STRING]+ * [--force] [--json]+ */++import { existsSync, readFileSync, writeFileSync } from 'node:fs';+import { resolve as resolvePath } from 'node:path';+import type { EvalCandidateInput } from '../core/types.ts';+import {+ BASELINE_FILE_SCHEMA_VERSION,+ DEFAULT_THRESHOLDS,+ computeQueryHash,+ computeSourceHash,+ serializeBaselineFile,+ type BaselineFile,+ type BaselineMetadata,+ type BaselineRow,+ type BaselineThresholds,+} from '../core/bench/baseline-file.ts';+import { createAuditWriter } from '../core/audit/audit-writer.ts';++interface PublishOpts {+ help?: boolean;+ from?: string;+ to?: string;+ label?: string;+ force?: boolean;+ json?: boolean;+ thresholds: Partial<BaselineThresholds>;+}++interface BenchPublishAuditEvent {+ ts: string;+ label: string;+ source_hash: string;+ row_count: number;+ output_path: string;+ thresholds: BaselineThresholds;+ success: boolean;+ failure_reason?: string;+}++const auditWriter = createAuditWriter<BenchPublishAuditEvent>({+ featureName: 'bench-publish',+ errorLabel: 'bench-publish-audit',+ errorTrailer: '; continuing',+});++function parseArgs(args: string[]): PublishOpts {+ const opts: PublishOpts = { thresholds: {} };+ for (let i = 0; i < args.length; i++) {+ const arg = args[i]!;+ const next = args[i + 1];+ switch (arg) {+ case '--help':+ case '-h':+ opts.help = true;+ break;+ case '--from':+ opts.from = next;+ i++;+ break;+ case '--to':+ opts.to = next;+ i++;+ break;+ case '--label':+ opts.label = next;+ i++;+ break;+ case '--force':+ opts.force = true;+ break;+ case '--json':+ opts.json = true;+ break;+ case '--threshold-jaccard':+ opts.thresholds.jaccard = Number(next);+ i++;+ break;+ case '--threshold-top1':+ opts.thresholds.top1 = Number(next);+ i++;+ break;+ case '--threshold-latency-multiplier':+ opts.thresholds.latency_multiplier = Number(next);+ i++;+ break;+ default:+ // Unknown arg; will fail below.+ break;+ }+ }+ return opts;+}++function printHelp(): void {+ console.log(`gbrain bench publish — write a baseline file from captured queries++Usage:+ gbrain bench publish --from <captured.ndjson> --to <X.baseline.ndjson> [flags]++Required:+ --from FILE Input NDJSON from \`gbrain eval export\`+ --to FILE Output baseline path (recommend .baseline.ndjson extension)++Optional:+ --label STRING Human-readable label (default: derived from --to filename)+ --force Overwrite --to if it exists+ --json Print JSON envelope to stdout+ --threshold-jaccard FLOAT Embedded jaccard threshold (default: ${DEFAULT_THRESHOLDS.jaccard})+ --threshold-top1 FLOAT Embedded top-1 threshold (default: ${DEFAULT_THRESHOLDS.top1})+ --threshold-latency-multiplier FLOAT+ Embedded latency multiplier (default: ${DEFAULT_THRESHOLDS.latency_multiplier})+ -h, --help Show this help++Exit codes:+ 0 Baseline written+ 1 Input validation failure (empty, duplicates, parse error, write failure)+ 2 Usage error (missing flag, --to exists without --force)++Examples:+ # Publish your own personal baseline+ gbrain eval export --limit 200 --tool query > /tmp/captured.ndjson+ gbrain bench publish --from /tmp/captured.ndjson --to ~/.gbrain/baselines/personal.baseline.ndjson --label "personal-2026-05"++ # Gate against it later+ gbrain eval gate --baseline ~/.gbrain/baselines/personal.baseline.ndjson+`);+}++/** Parse one input NDJSON line into a candidate row. Throws with line context on failure. */+function parseInputRow(line: string, lineNo: number): EvalCandidateInput {+ let parsed: unknown;+ try {+ parsed = JSON.parse(line);+ } catch (err) {+ throw new Error(`Line ${lineNo}: malformed JSON: ${(err as Error).message}`);+ }+ if (typeof parsed !== 'object' || parsed === null) {+ throw new Error(`Line ${lineNo} is not a JSON object`);+ }+ const row = parsed as Partial<EvalCandidateInput> & Record<string, unknown>;+ // schema_version is optional on input (some test/synthetic inputs omit it).+ // Strict shape check on the actual fields we use.+ if (row.tool_name !== 'query' && row.tool_name !== 'search') {+ throw new Error(`Line ${lineNo} missing or invalid tool_name (must be 'query' or 'search')`);+ }+ if (typeof row.query !== 'string') {+ throw new Error(`Line ${lineNo} missing query`);+ }+ if (!Array.isArray(row.retrieved_slugs)) {+ throw new Error(`Line ${lineNo} missing retrieved_slugs`);+ }+ if (!Array.isArray(row.source_ids)) {+ throw new Error(`Line ${lineNo} missing source_ids`);+ }+ if (typeof row.latency_ms !== 'number') {+ throw new Error(`Line ${lineNo} missing latency_ms`);+ }+ return row as EvalCandidateInput;+}++/** Per eng-D5: dedup key includes source_ids so multi-source brains don't collapse. */+function dedupKey(row: BaselineRow): string {+ const sources = [...row.source_ids].sort().join(',');+ return `${row.tool_name}|${sources}|${row.query_hash}`;+}++/**+ * Programmatic entrypoint. Returns the BaselineFile structure without+ * writing to disk. Throws on validation failures with paste-ready messages.+ */+export function buildBaselineFromInput(+ input: EvalCandidateInput[],+ opts: { label: string; thresholds?: Partial<BaselineThresholds>; publishedAt?: Date },+): BaselineFile {+ if (input.length === 0) {+ throw new Error('no rows to publish (empty input)');+ }++ const rows: BaselineRow[] = input.map(r => ({+ ...r,+ query_hash: computeQueryHash(r.query),+ }));++ // Dedup check before anything else (eng-D5 strict posture).+ const seen = new Map<string, BaselineRow[]>();+ for (const row of rows) {+ const key = dedupKey(row);+ if (!seen.has(key)) seen.set(key, []);+ seen.get(key)!.push(row);+ }+ const dupes = [...seen.entries()].filter(([, rs]) => rs.length > 1);+ if (dupes.length > 0) {+ const first5 = dupes.slice(0, 5).map(([key, rs]) => {+ const sample = rs[0]!;+ return ` ${key} (query="${sample.query.slice(0, 60)}", ${rs.length} copies)`;+ }).join('\n');+ throw new Error(+ `Found ${dupes.length} duplicate (tool_name, source_ids, query_hash) key(s). First ${Math.min(5, dupes.length)}:\n${first5}\n\n` ++ `Hint: dedupe your captured rows before publishing, e.g.\n` ++ ` jq -c -s 'group_by(.tool_name + "|" + (.source_ids|sort|join(",")) + "|" + .query) | map(.[0]) | .[]' /tmp/captured.ndjson > /tmp/deduped.ndjson`,+ );+ }++ const thresholds: BaselineThresholds = {+ jaccard: opts.thresholds?.jaccard ?? DEFAULT_THRESHOLDS.jaccard,+ top1: opts.thresholds?.top1 ?? DEFAULT_THRESHOLDS.top1,+ latency_multiplier: opts.thresholds?.latency_multiplier ?? DEFAULT_THRESHOLDS.latency_multiplier,+ };++ const baselineMeanLatencyMs = rows.reduce((s, r) => s + r.latency_ms, 0) / rows.length;++ const metadata: BaselineMetadata = {+ schema_version: BASELINE_FILE_SCHEMA_VERSION,+ _kind: 'baseline_metadata',+ label: opts.label,+ published_at: (opts.publishedAt ?? new Date()).toISOString(),+ source_hash: computeSourceHash(rows),+ thresholds,+ row_count: rows.length,+ baseline_mean_latency_ms: baselineMeanLatencyMs,+ };++ return { metadata, rows };+}++/** Derive a default label from the output path basename (sans extension). */+function deriveLabel(toPath: string): string {+ const base = toPath.split('/').pop() ?? toPath;+ return base.replace(/\.baseline\.ndjson$/, '').replace(/\.ndjson$/, '');+}++/** Read + parse the input NDJSON. Throws with line context on failure. */+function readInputNdjson(path: string): EvalCandidateInput[] {+ const content = readFileSync(path, 'utf-8');+ const lines = content.split('\n');+ const rows: EvalCandidateInput[] = [];+ for (let i = 0; i < lines.length; i++) {+ const line = lines[i]!.trim();+ if (!line) continue;+ rows.push(parseInputRow(line, i + 1));+ }+ return rows;+}++export async function runBenchPublish(args: string[]): Promise<void> {+ const opts = parseArgs(args);+ if (opts.help) {+ printHelp();+ return;+ }++ // USAGE checks (exit 2)+ if (!opts.from) {+ console.error('Error: --from FILE is required\n');+ printHelp();+ process.exit(2);+ }+ if (!opts.to) {+ console.error('Error: --to FILE is required\n');+ printHelp();+ process.exit(2);+ }+ if (!existsSync(opts.from)) {+ console.error(`Error: --from file not found: ${opts.from}`);+ process.exit(1);+ }+ if (existsSync(opts.to) && !opts.force) {+ console.error(`Error: --to path already exists: ${opts.to}\nUse --force to overwrite.`);+ process.exit(2);+ }++ const label = opts.label ?? deriveLabel(opts.to);+ const outputPath = resolvePath(opts.to);++ let input: EvalCandidateInput[];+ try {+ input = readInputNdjson(opts.from);+ } catch (err) {+ const msg = (err as Error).message;+ console.error(`Error: ${msg}`);+ auditWriter.log({+ label,+ source_hash: '',+ row_count: 0,+ output_path: outputPath,+ thresholds: { ...DEFAULT_THRESHOLDS, ...opts.thresholds },+ success: false,+ failure_reason: msg,+ });+ process.exit(1);+ }++ let file: BaselineFile;+ try {+ file = buildBaselineFromInput(input, {+ label,+ thresholds: opts.thresholds,+ });+ } catch (err) {+ const msg = (err as Error).message;+ console.error(`Error: ${msg}`);+ auditWriter.log({+ label,+ source_hash: '',+ row_count: input.length,+ output_path: outputPath,+ thresholds: { ...DEFAULT_THRESHOLDS, ...opts.thresholds },+ success: false,+ failure_reason: msg,+ });+ process.exit(1);+ }++ const serialized = serializeBaselineFile(file);++ try {+ writeFileSync(outputPath, serialized);+ } catch (err) {+ const msg = (err as Error).message;+ console.error(`Error: could not write baseline to ${outputPath}: ${msg}`);+ auditWriter.log({+ label,+ source_hash: file.metadata.source_hash,+ row_count: file.rows.length,+ output_path: outputPath,+ thresholds: file.metadata.thresholds,+ success: false,+ failure_reason: msg,+ });+ process.exit(1);+ }++ auditWriter.log({+ label,+ source_hash: file.metadata.source_hash,+ row_count: file.rows.length,+ output_path: outputPath,+ thresholds: file.metadata.thresholds,+ success: true,+ });++ if (opts.json) {+ console.log(JSON.stringify({+ schema_version: 1,+ output_path: outputPath,+ label: file.metadata.label,+ source_hash: file.metadata.source_hash,+ row_count: file.rows.length,+ baseline_mean_latency_ms: file.metadata.baseline_mean_latency_ms,+ thresholds: file.metadata.thresholds,+ }, null, 2));+ } else {+ console.log(`Wrote ${outputPath}`);+ console.log(` Label: ${file.metadata.label}`);+ console.log(` Rows: ${file.rows.length}`);+ console.log(` Hash: ${file.metadata.source_hash.slice(0, 16)}…`);+ console.log(` Latency: ${file.metadata.baseline_mean_latency_ms.toFixed(0)}ms baseline mean`);+ console.log(` Gate: jaccard>=${file.metadata.thresholds.jaccard} top1>=${file.metadata.thresholds.top1} latency<=${file.metadata.thresholds.latency_multiplier}x`);+ }+}diff --git a/src/commands/eval-gate.ts b/src/commands/eval-gate.tsnew file mode 100644index 0000000000..fda0b35f6c--- /dev/null+++ b/src/commands/eval-gate.ts@@ -0,0 +1,491 @@+/**+ * gbrain eval gate — fail CI on retrieval regressions OR correctness drops (v0.41).+ *+ * Two gating paths (per CEO D8 + eng D6/D7):+ *+ * - Regression gate (--baseline X.baseline.ndjson): replays baseline+ * queries against current brain; computes jaccard / top-1 stability /+ * latency multiplier; catches REGRESSIONS during refactors.+ *+ * - Correctness gate (--qrels Y.qrels.json): runs qrels queries against+ * current brain via bare hybridSearch; computes recall@K /+ * first_relevant_hit_rate / expected_top1_hit_rate; catches retrieval+ * QUALITY drops against known-right answers.+ *+ * Both can be passed together; both must pass for verdict `pass`. At least+ * one must be set (usage error otherwise).+ *+ * Fail-closed posture (D3): any in-process throw from replay or+ * correctness-gate flips verdict to `fail` with a named breach. Per+ * codex round-2 #7, replay runs in-process (NOT spawn subprocess) to avoid+ * the gbrain-version-drift bug class for source-tree CI runs.+ *+ * Latency math CORRECTED (codex round-2 #2): the gate uses+ * `(baseline_mean_latency_ms + mean_latency_delta_ms) / baseline_mean_latency_ms <= multiplier`.+ * The earlier formula (`delta / baseline <= multiplier`) would let a 2.5x+ * slowdown pass at multiplier=2.0.+ *+ * Exit codes: 0 PASS, 1 FAIL (regression OR throw), 2 USAGE.+ */++import { existsSync, readFileSync } from 'node:fs';+import type { BrainEngine } from '../core/engine.ts';+import {+ parseBaselineFile,+ type BaselineFile,+ type BaselineThresholds,+} from '../core/bench/baseline-file.ts';+import {+ DEFAULT_QRELS_THRESHOLDS,+ parseQrelsFile,+ type QrelsFile,+} from '../core/bench/qrels-file.ts';+import { runCorrectnessGate, type CorrectnessResult } from '../core/bench/correctness-gate.ts';+import { replayCore, type ReplaySummary } from './eval-replay.ts';++interface GateOpts {+ help?: boolean;+ baseline?: string;+ qrels?: string;+ k?: number;+ json?: boolean;+ thresholdJaccard?: number;+ thresholdTop1?: number;+ thresholdLatencyMultiplier?: number;+ thresholdRecallAtK?: number;+ thresholdFirstRelevantHit?: number;+ thresholdExpectedTop1?: number;+}++interface Breach {+ metric: string;+ observed?: number;+ threshold?: number;+ reason?: string;+ error_tail?: string;+}++interface GateResult {+ schema_version: 1;+ verdict: 'pass' | 'fail';+ regression_gate: {+ ran: boolean;+ baseline_path?: string;+ summary?: ReplaySummary;+ thresholds?: BaselineThresholds;+ latency_skipped?: boolean;+ breaches?: Breach[];+ };+ correctness_gate: {+ ran: boolean;+ qrels_path?: string;+ summary?: CorrectnessResult['summary'];+ thresholds?: {+ recall_at_k: number;+ first_relevant_hit: number;+ expected_top1: number;+ };+ breaches?: Breach[];+ };+}++function parseArgs(args: string[]): GateOpts {+ const opts: GateOpts = {};+ for (let i = 0; i < args.length; i++) {+ const arg = args[i]!;+ const next = args[i + 1];+ switch (arg) {+ case '--help':+ case '-h':+ opts.help = true;+ break;+ case '--baseline':+ opts.baseline = next;+ i++;+ break;+ case '--qrels':+ opts.qrels = next;+ i++;+ break;+ case '--json':+ opts.json = true;+ break;+ case '-k':+ case '--k':+ opts.k = Number(next);+ i++;+ break;+ case '--threshold-jaccard':+ opts.thresholdJaccard = Number(next);+ i++;+ break;+ case '--threshold-top1':+ opts.thresholdTop1 = Number(next);+ i++;+ break;+ case '--threshold-latency-multiplier':+ opts.thresholdLatencyMultiplier = Number(next);+ i++;+ break;+ case '--threshold-recall-at-k':+ opts.thresholdRecallAtK = Number(next);+ i++;+ break;+ case '--threshold-first-relevant-hit':+ opts.thresholdFirstRelevantHit = Number(next);+ i++;+ break;+ case '--threshold-expected-top1':+ opts.thresholdExpectedTop1 = Number(next);+ i++;+ break;+ default:+ break;+ }+ }+ return opts;+}++function printHelp(): void {+ console.log(`gbrain eval gate — fail CI on retrieval regressions or correctness drops++Usage:+ gbrain eval gate --baseline X.baseline.ndjson [flags] # regression-only+ gbrain eval gate --qrels Y.qrels.json [flags] # correctness-only+ gbrain eval gate --baseline X --qrels Y [flags] # both required++Required (at least one):+ --baseline FILE Baseline NDJSON from \`gbrain bench publish\`+ --qrels FILE Qrels JSON (\`{schema_version, queries: [...]}\` shape)++Thresholds (override baseline metadata; CLI > embedded > defaults):+ --threshold-jaccard FLOAT Regression: mean Jaccard floor (default ${0.85})+ --threshold-top1 FLOAT Regression: top-1 stability floor (default ${0.80})+ --threshold-latency-multiplier FLOAT+ Regression: current/baseline latency cap (default ${2.0}x)+ --threshold-recall-at-k FLOAT Correctness: mean recall@k floor (default ${DEFAULT_QRELS_THRESHOLDS.recall_at_k})+ --threshold-first-relevant-hit FLOAT+ Correctness: first-relevant-hit-rate floor (default ${DEFAULT_QRELS_THRESHOLDS.first_relevant_hit})+ --threshold-expected-top1 FLOAT Correctness: expected_top1-hit-rate floor (default ${DEFAULT_QRELS_THRESHOLDS.expected_top1})+ -k, --k N Top-K for recall@K (default ${DEFAULT_QRELS_THRESHOLDS.k})++Output:+ --json Print JSON envelope to stdout+ -h, --help Show this help++Exit codes:+ 0 All requested gates passed+ 1 At least one breach (regression or correctness) OR in-process throw+ 2 Usage error (no flags, file missing, malformed JSON)+`);+}++function isFinitePos(n: number): boolean {+ return Number.isFinite(n) && n > 0;+}++function runRegressionGate(+ engine: BrainEngine,+ baselinePath: string,+ cliOverrides: Pick<GateOpts, 'thresholdJaccard' | 'thresholdTop1' | 'thresholdLatencyMultiplier'>,+): Promise<GateResult['regression_gate']> {+ return (async () => {+ let baselineFile: BaselineFile;+ try {+ const content = readFileSync(baselinePath, 'utf-8');+ baselineFile = parseBaselineFile(content);+ } catch (err) {+ // USAGE-style failure (file missing or malformed). Surface as a gate+ // breach since the gate ran in PASS posture and now can't proceed.+ return {+ ran: true,+ baseline_path: baselinePath,+ breaches: [{+ metric: 'baseline_parse',+ reason: 'baseline_unreadable',+ error_tail: (err as Error).message,+ }],+ };+ }++ // Threshold precedence: CLI > embedded > defaults (defaults built into baseline at publish time).+ const thresholds: BaselineThresholds = {+ jaccard: cliOverrides.thresholdJaccard ?? baselineFile.metadata.thresholds.jaccard,+ top1: cliOverrides.thresholdTop1 ?? baselineFile.metadata.thresholds.top1,+ latency_multiplier: cliOverrides.thresholdLatencyMultiplier ?? baselineFile.metadata.thresholds.latency_multiplier,+ };++ let summary: ReplaySummary;+ try {+ const out = await replayCore(engine, { against: baselinePath });+ summary = out.summary;+ } catch (err) {+ // D3 fail-closed on in-process throw (codex round-2 #7).+ return {+ ran: true,+ baseline_path: baselinePath,+ thresholds,+ breaches: [{+ metric: 'replay_in_process',+ reason: 'replay_threw',+ error_tail: (err as Error).message,+ }],+ };+ }++ const breaches: Breach[] = [];+ if (summary.mean_jaccard < thresholds.jaccard) {+ breaches.push({+ metric: 'mean_jaccard',+ observed: summary.mean_jaccard,+ threshold: thresholds.jaccard,+ });+ }+ if (summary.top1_stability_rate < thresholds.top1) {+ breaches.push({+ metric: 'top1_stability_rate',+ observed: summary.top1_stability_rate,+ threshold: thresholds.top1,+ });+ }++ // Corrected latency math (codex round-2 #2):+ // ratio = (baseline + delta) / baseline; must be <= multiplier.+ // Skip the check when baseline_mean_latency_ms <= 0 (synthetic baselines).+ const baselineMean = baselineFile.metadata.baseline_mean_latency_ms;+ let latencySkipped = false;+ if (isFinitePos(baselineMean)) {+ const ratio = (baselineMean + summary.mean_latency_delta_ms) / baselineMean;+ if (ratio > thresholds.latency_multiplier) {+ breaches.push({+ metric: 'latency_ratio',+ observed: ratio,+ threshold: thresholds.latency_multiplier,+ });+ }+ } else {+ latencySkipped = true;+ console.error(+ `[eval gate] WARN: baseline_mean_latency_ms is ${baselineMean}; skipping latency check.`,+ );+ }++ return {+ ran: true,+ baseline_path: baselinePath,+ summary,+ thresholds,+ ...(latencySkipped ? { latency_skipped: true } : {}),+ ...(breaches.length > 0 ? { breaches } : {}),+ };+ })();+}++function runCorrectnessGateDispatch(+ engine: BrainEngine,+ qrelsPath: string,+ k: number,+ cliOverrides: Pick<GateOpts, 'thresholdRecallAtK' | 'thresholdFirstRelevantHit' | 'thresholdExpectedTop1'>,+): Promise<GateResult['correctness_gate']> {+ return (async () => {+ let qrelsFile: QrelsFile;+ try {+ const content = readFileSync(qrelsPath, 'utf-8');+ qrelsFile = parseQrelsFile(content);+ } catch (err) {+ return {+ ran: true,+ qrels_path: qrelsPath,+ breaches: [{+ metric: 'qrels_parse',+ reason: 'qrels_unreadable',+ error_tail: (err as Error).message,+ }],+ };+ }++ const thresholds = {+ recall_at_k: cliOverrides.thresholdRecallAtK ?? DEFAULT_QRELS_THRESHOLDS.recall_at_k,+ first_relevant_hit: cliOverrides.thresholdFirstRelevantHit ?? DEFAULT_QRELS_THRESHOLDS.first_relevant_hit,+ expected_top1: cliOverrides.thresholdExpectedTop1 ?? DEFAULT_QRELS_THRESHOLDS.expected_top1,+ };++ let result: CorrectnessResult;+ try {+ result = await runCorrectnessGate(engine, qrelsFile, { k });+ } catch (err) {+ return {+ ran: true,+ qrels_path: qrelsPath,+ thresholds,+ breaches: [{+ metric: 'correctness_gate',+ reason: 'orchestrator_threw',+ error_tail: (err as Error).message,+ }],+ };+ }++ const breaches: Breach[] = [];+ if (result.summary.queries_errored > 0) {+ // Per-query throws are gate failures (Finding 2D).+ const erroredQueries = result.per_query.filter(p => p.errored).slice(0, 5);+ breaches.push({+ metric: 'queries_errored',+ observed: result.summary.queries_errored,+ threshold: 0,+ reason: 'one_or_more_qrels_queries_threw',+ error_tail: erroredQueries.map(p => `${p.query_id}: ${p.error_message}`).join(' | '),+ });+ }+ if (result.summary.mean_recall_at_k < thresholds.recall_at_k) {+ breaches.push({+ metric: 'mean_recall_at_k',+ observed: result.summary.mean_recall_at_k,+ threshold: thresholds.recall_at_k,+ });+ }+ if (result.summary.first_relevant_hit_rate < thresholds.first_relevant_hit) {+ breaches.push({+ metric: 'first_relevant_hit_rate',+ observed: result.summary.first_relevant_hit_rate,+ threshold: thresholds.first_relevant_hit,+ });+ }+ // Only enforce expected_top1 floor when at least one query had it set.+ if (result.summary.expected_top1_denominator > 0 &&+ result.summary.expected_top1_hit_rate < thresholds.expected_top1) {+ breaches.push({+ metric: 'expected_top1_hit_rate',+ observed: result.summary.expected_top1_hit_rate,+ threshold: thresholds.expected_top1,+ });+ }++ return {+ ran: true,+ qrels_path: qrelsPath,+ summary: result.summary,+ thresholds,+ ...(breaches.length > 0 ? { breaches } : {}),+ };+ })();+}++function printHumanOutput(result: GateResult): void {+ const overall = result.verdict === 'pass' ? '✅ PASS' : '❌ FAIL';+ console.log(`Verdict: ${overall}`);+ console.log('');++ if (result.regression_gate.ran) {+ console.log('Regression gate (--baseline)');+ const r = result.regression_gate;+ if (r.summary) {+ console.log(` mean_jaccard: ${r.summary.mean_jaccard.toFixed(3)} (floor ${r.thresholds?.jaccard ?? '?'})`);+ console.log(` top1_stability: ${(r.summary.top1_stability_rate * 100).toFixed(1)}% (floor ${(((r.thresholds?.top1 ?? 0)) * 100).toFixed(0)}%)`);+ if (!r.latency_skipped) {+ console.log(` mean_latency_delta: ${r.summary.mean_latency_delta_ms >= 0 ? '+' : ''}${r.summary.mean_latency_delta_ms.toFixed(0)}ms`);+ } else {+ console.log(` latency: SKIPPED (baseline_mean_latency_ms <= 0)`);+ }+ }+ if (r.breaches && r.breaches.length > 0) {+ console.log(` BREACHES:`);+ for (const b of r.breaches) {+ const obs = b.observed !== undefined ? ` observed=${b.observed.toFixed(3)}` : '';+ const thr = b.threshold !== undefined ? ` threshold=${b.threshold.toFixed(3)}` : '';+ const reason = b.reason ? ` reason=${b.reason}` : '';+ console.log(` - ${b.metric}${obs}${thr}${reason}`);+ if (b.error_tail) console.log(` ${b.error_tail.slice(0, 200)}`);+ }+ }+ console.log('');+ }++ if (result.correctness_gate.ran) {+ console.log('Correctness gate (--qrels)');+ const c = result.correctness_gate;+ if (c.summary) {+ console.log(` queries_run: ${c.summary.queries_run}/${c.summary.queries_total} (${c.summary.queries_errored} errored)`);+ console.log(` mean_recall@${c.summary.k}: ${c.summary.mean_recall_at_k.toFixed(3)} (floor ${c.thresholds?.recall_at_k ?? '?'})`);+ console.log(` first_relevant_hit: ${(c.summary.first_relevant_hit_rate * 100).toFixed(1)}% (floor ${(((c.thresholds?.first_relevant_hit ?? 0)) * 100).toFixed(0)}%)`);+ if (c.summary.expected_top1_denominator > 0) {+ console.log(` expected_top1_hit: ${(c.summary.expected_top1_hit_rate * 100).toFixed(1)}% over ${c.summary.expected_top1_denominator} queries (floor ${(((c.thresholds?.expected_top1 ?? 0)) * 100).toFixed(0)}%)`);+ }+ }+ if (c.breaches && c.breaches.length > 0) {+ console.log(` BREACHES:`);+ for (const b of c.breaches) {+ const obs = b.observed !== undefined ? ` observed=${typeof b.observed === 'number' ? b.observed.toFixed(3) : b.observed}` : '';+ const thr = b.threshold !== undefined ? ` threshold=${typeof b.threshold === 'number' ? b.threshold.toFixed(3) : b.threshold}` : '';+ const reason = b.reason ? ` reason=${b.reason}` : '';+ console.log(` - ${b.metric}${obs}${thr}${reason}`);+ if (b.error_tail) console.log(` ${b.error_tail.slice(0, 200)}`);+ }+ }+ }+}++export async function runEvalGate(engine: BrainEngine, args: string[]): Promise<void> {+ const opts = parseArgs(args);+ if (opts.help) {+ printHelp();+ return;+ }++ if (!opts.baseline && !opts.qrels) {+ console.error('Error: at least one of --baseline or --qrels must be set\n');+ printHelp();+ process.exit(2);+ }++ if (opts.baseline && !existsSync(opts.baseline)) {+ console.error(`Error: baseline file not found: ${opts.baseline}`);+ process.exit(2);+ }+ if (opts.qrels && !existsSync(opts.qrels)) {+ console.error(`Error: qrels file not found: ${opts.qrels}`);+ process.exit(2);+ }++ const result: GateResult = {+ schema_version: 1,+ verdict: 'pass',+ regression_gate: { ran: false },+ correctness_gate: { ran: false },+ };++ if (opts.baseline) {+ result.regression_gate = await runRegressionGate(engine, opts.baseline, {+ thresholdJaccard: opts.thresholdJaccard,+ thresholdTop1: opts.thresholdTop1,+ thresholdLatencyMultiplier: opts.thresholdLatencyMultiplier,+ });+ if (result.regression_gate.breaches && result.regression_gate.breaches.length > 0) {+ result.verdict = 'fail';+ }+ }++ if (opts.qrels) {+ const k = opts.k ?? DEFAULT_QRELS_THRESHOLDS.k;+ result.correctness_gate = await runCorrectnessGateDispatch(engine, opts.qrels, k, {+ thresholdRecallAtK: opts.thresholdRecallAtK,+ thresholdFirstRelevantHit: opts.thresholdFirstRelevantHit,+ thresholdExpectedTop1: opts.thresholdExpectedTop1,+ });+ if (result.correctness_gate.breaches && result.correctness_gate.breaches.length > 0) {+ result.verdict = 'fail';+ }+ }++ if (opts.json) {+ console.log(JSON.stringify(result, null, 2));+ } else {+ printHumanOutput(result);+ }++ if (result.verdict === 'fail') process.exit(1);+}++// Exported for tests + e2e LOOP test+export type { GateResult, Breach };diff --git a/src/commands/eval-replay.ts b/src/commands/eval-replay.tsindex f47bb6313e..22f0ccbff9 100644--- a/src/commands/eval-replay.ts+++ b/src/commands/eval-replay.ts@@ -188,6 +188,10 @@ interface CapturedRow {/*** Parse NDJSON. One object per non-blank line. Single bad line throws — it's* a corrupt export and silently dropping rows would mask real bugs.+ *+ * v0.41 (codex round-1 #3): SKIPS the `_kind: 'baseline_metadata'` header line+ * that `gbrain bench publish` writes. The header carries metadata (label,+ * thresholds, source_hash, etc) that must NOT be counted as a captured row.*/function parseNdjson(content: string): CapturedRow[] {const lines = content.split('\n');@@ -195,12 +199,14 @@ function parseNdjson(content: string): CapturedRow[] {for (let i = 0; i < lines.length; i++) {const line = lines[i]!.trim();if (!line) continue;- let row: CapturedRow;+ let row: CapturedRow & { _kind?: string };try {row = JSON.parse(line);} catch (err) {throw new Error(`NDJSON parse error on line ${i + 1}: ${(err as Error).message}`);}+ // v0.41: drop baseline metadata header before it can pollute counts.+ if (row._kind === 'baseline_metadata') continue;if (typeof row.schema_version !== 'number') {throw new Error(`Line ${i + 1} missing schema_version — not from \`gbrain eval export\`?`);}@@ -298,7 +304,7 @@ async function replayRow(engine: BrainEngine, row: CapturedRow, opts: ReplayOpts};}-interface ReplaySummary {+export interface ReplaySummary {rows_total: number;rows_replayed: number;rows_skipped: number;@@ -378,45 +384,32 @@ function printHumanSummary(summary: ReplaySummary, results: RowResult[], topRegr}}-export async function runEvalReplay(engine: BrainEngine, args: string[]): Promise<void> {- const opts = parseArgs(args);- if (opts.help) {- printHelp();- return;- }+/**+ * Programmatic entrypoint. Throws on error (no process.exit), returns the+ * computed summary + per-row results.+ *+ * v0.41 (codex round-2 #7): exposed so `gbrain eval gate --baseline` can+ * call replay in-process rather than spawning a subprocess. Subprocess+ * spawning would run the INSTALLED gbrain (drift risk for source-tree runs).+ */+export async function replayCore(+ engine: BrainEngine,+ opts: ReplayOpts,+): Promise<{ summary: ReplaySummary; results: RowResult[] }> {if (!opts.against) {- console.error('Error: --against FILE.ndjson is required\n');- printHelp();- process.exit(1);+ throw new Error('replayCore: opts.against (path to NDJSON) is required');}if (!existsSync(opts.against)) {- console.error(`Error: file not found: ${opts.against}`);- process.exit(1);- }-- let rows: CapturedRow[];- try {- const content = readFileSync(opts.against, 'utf-8');- rows = parseNdjson(content);- } catch (err) {- console.error(`Error: ${(err as Error).message}`);- process.exit(1);+ throw new Error(`File not found: ${opts.against}`);}-+ const content = readFileSync(opts.against, 'utf-8');+ const rows = parseNdjson(content);if (rows.length === 0) {- console.error(`Error: ${opts.against} is empty (no NDJSON rows)`);- process.exit(1);+ throw new Error(`${opts.against} is empty (no NDJSON rows)`);}const capped = opts.limit && opts.limit > 0 ? rows.slice(0, opts.limit) : rows;- if (!opts.json) {- console.error(- `Replaying ${capped.length}${capped.length < rows.length ? ` of ${rows.length}` : ''} captured queries${opts.mode ? ` under mode=${opts.mode}` : ''}${opts.compareLimit ? ` (compare-limit=${opts.compareLimit})` : ''}…`,- );- }- // v0.32.3: thread --mode into the engine's config so hybridSearch resolves- // it through the standard chain. Set once before the replay loop runs.if (opts.mode) {try { await engine.setConfig('search.mode', opts.mode); } catch { /* swallow */ }}@@ -441,12 +434,47 @@ export async function runEvalReplay(engine: BrainEngine, args: string[]): Promis}const r = await replayRow(engine, row, opts);results.push(r);- if (!opts.json && results.length % 25 === 0) {- process.stderr.write(` ...${results.length}/${capped.length}\n`);- }}- const summary = summarize(results);+ return { summary: summarize(results), results };+}++export async function runEvalReplay(engine: BrainEngine, args: string[]): Promise<void> {+ const opts = parseArgs(args);+ if (opts.help) {+ printHelp();+ return;+ }+ if (!opts.against) {+ console.error('Error: --against FILE.ndjson is required\n');+ printHelp();+ process.exit(1);+ }++ if (!opts.json) {+ // Pre-flight: count rows so the "Replaying X of Y" line is accurate.+ // The programmatic path skips this nicety.+ try {+ const content = readFileSync(opts.against!, 'utf-8');+ const allRows = parseNdjson(content);+ const cappedCount = opts.limit && opts.limit > 0 ? Math.min(allRows.length, opts.limit) : allRows.length;+ console.error(+ `Replaying ${cappedCount}${cappedCount < allRows.length ? ` of ${allRows.length}` : ''} captured queries${opts.mode ? ` under mode=${opts.mode}` : ''}${opts.compareLimit ? ` (compare-limit=${opts.compareLimit})` : ''}…`,+ );+ } catch { /* swallow; replayCore will throw with the same message */ }+ }++ let summary: ReplaySummary;+ let results: RowResult[];+ try {+ const out = await replayCore(engine, opts);+ summary = out.summary;+ results = out.results;+ } catch (err) {+ console.error(`Error: ${(err as Error).message}`);+ process.exit(1);+ }+if (opts.json) {console.log(JSON.stringify({schema_version: 1,diff --git a/src/commands/eval.ts b/src/commands/eval.tsindex 8227e2e700..2c8c7fabd7 100644--- a/src/commands/eval.ts+++ b/src/commands/eval.ts@@ -37,6 +37,13 @@ export async function runEvalCommand(engine: BrainEngine, args: string[]): Promiconst { runEvalReplay } = await import('./eval-replay.ts');return runEvalReplay(engine, args.slice(1));}+ if (sub === 'gate') {+ // v0.41 — eval gate. Two paths (regression via --baseline, correctness+ // via --qrels). Needs an engine: correctness gate runs live retrieval;+ // regression gate calls replayCore in-process (codex round-2 #7).+ const { runEvalGate } = await import('./eval-gate.ts');+ return runEvalGate(engine, args.slice(1));+ }if (sub === 'cross-modal') {// No-DB sub-subcommand. The cli.ts dispatcher routes the user-facing// path before connectEngine, so this branch only fires when callersdiff --git a/src/core/bench/baseline-file.ts b/src/core/bench/baseline-file.tsnew file mode 100644index 0000000000..9d02bca8b7--- /dev/null+++ b/src/core/bench/baseline-file.ts@@ -0,0 +1,193 @@+/**+ * Baseline file format for `gbrain bench publish` + `gbrain eval gate --baseline`.+ *+ * Wire shape: NDJSON. First line is metadata (`_kind: 'baseline_metadata'`).+ * Subsequent lines are raw captured rows mirroring `EvalCandidateInput`.+ *+ * The `_kind` discriminator lets `gbrain eval replay` skip the metadata line+ * cleanly (codex round-1 #3 — without the discriminator the header counts as+ * a fake data row and pollutes counts).+ *+ * Source-id-aware: row-level `source_ids: string[]` + `query_hash` form the+ * dedup key in `bench publish` (codex round-2 #6 — slug-only would collapse+ * the same query across federated sources).+ *+ * `baseline_mean_latency_ms` is the absolute baseline latency. The gate's+ * latency check is `(baseline + delta) / baseline <= multiplier`, which+ * requires the absolute baseline — `eval replay` only emits the delta+ * (codex round-2 #2).+ */++import { createHash } from 'node:crypto';+import type { EvalCandidateInput } from '../types.ts';++/** Stable on-disk format version. Bump when adding new required fields. */+export const BASELINE_FILE_SCHEMA_VERSION = 1 as const;++/** Default thresholds when neither baseline metadata nor CLI flags set them. */+export const DEFAULT_THRESHOLDS: BaselineThresholds = {+ jaccard: 0.85,+ top1: 0.80,+ latency_multiplier: 2.0,+};++export interface BaselineThresholds {+ jaccard: number;+ top1: number;+ latency_multiplier: number;+}++export interface BaselineMetadata {+ schema_version: typeof BASELINE_FILE_SCHEMA_VERSION;+ _kind: 'baseline_metadata';+ label: string;+ published_at: string; // ISO 8601+ source_hash: string; // sha256 of sorted row hashes+ thresholds: BaselineThresholds;+ row_count: number;+ baseline_mean_latency_ms: number;+}++/** A baseline row mirrors EvalCandidateInput + adds a stable query_hash. */+export interface BaselineRow extends EvalCandidateInput {+ /** sha256(normalizeQueryForHash(query)).slice(0,16). Stamped at publish time. */+ query_hash: string;+}++export interface BaselineFile {+ metadata: BaselineMetadata;+ rows: BaselineRow[];+}++/**+ * Lowercase + collapse whitespace. Used as the input to `computeQueryHash`+ * so that " Hello World " and "hello world" hash to the same value.+ */+export function normalizeQueryForHash(query: string): string {+ return query.toLowerCase().replace(/\s+/g, ' ').trim();+}++/** 16-hex-char sha256 prefix of the normalized query. Stable across runs. */+export function computeQueryHash(query: string): string {+ return createHash('sha256').update(normalizeQueryForHash(query)).digest('hex').slice(0, 16);+}++/** sha256 of the sorted concatenation of every row's query_hash. */+export function computeSourceHash(rows: BaselineRow[]): string {+ const hashes = rows.map(r => r.query_hash).sort();+ return createHash('sha256').update(hashes.join('\n')).digest('hex');+}++export class BaselineParseError extends Error {+ constructor(message: string, public readonly lineNumber?: number) {+ super(message);+ this.name = 'BaselineParseError';+ }+}++/**+ * Parse a baseline NDJSON file. First non-empty line MUST be metadata.+ * Subsequent lines are rows.+ *+ * Throws `BaselineParseError` with paste-ready line + message on any failure.+ */+export function parseBaselineFile(content: string): BaselineFile {+ const lines = content.split('\n');+ let metadata: BaselineMetadata | null = null;+ const rows: BaselineRow[] = [];++ for (let i = 0; i < lines.length; i++) {+ const line = lines[i]!.trim();+ if (!line) continue;++ let parsed: unknown;+ try {+ parsed = JSON.parse(line);+ } catch (err) {+ throw new BaselineParseError(+ `Malformed JSON on line ${i + 1}: ${err instanceof Error ? err.message : String(err)}`,+ i + 1,+ );+ }++ if (typeof parsed !== 'object' || parsed === null) {+ throw new BaselineParseError(`Line ${i + 1} is not a JSON object`, i + 1);+ }++ if (metadata === null) {+ // First non-empty line MUST be metadata.+ const meta = parsed as Partial<BaselineMetadata>;+ if (meta._kind !== 'baseline_metadata') {+ throw new BaselineParseError(+ `First line must have "_kind": "baseline_metadata" (got ${JSON.stringify(meta._kind)})`,+ i + 1,+ );+ }+ if (meta.schema_version !== BASELINE_FILE_SCHEMA_VERSION) {+ throw new BaselineParseError(+ `Unsupported schema_version ${meta.schema_version} (this gbrain build expects ${BASELINE_FILE_SCHEMA_VERSION})`,+ i + 1,+ );+ }+ if (+ typeof meta.label !== 'string' ||+ typeof meta.published_at !== 'string' ||+ typeof meta.source_hash !== 'string' ||+ typeof meta.row_count !== 'number' ||+ typeof meta.baseline_mean_latency_ms !== 'number' ||+ !meta.thresholds ||+ typeof meta.thresholds.jaccard !== 'number' ||+ typeof meta.thresholds.top1 !== 'number' ||+ typeof meta.thresholds.latency_multiplier !== 'number'+ ) {+ throw new BaselineParseError(+ `Metadata header missing required fields (label/published_at/source_hash/row_count/baseline_mean_latency_ms/thresholds)`,+ i + 1,+ );+ }+ metadata = meta as BaselineMetadata;+ } else {+ const row = parsed as Partial<BaselineRow>;+ if (typeof row.tool_name !== 'string' || (row.tool_name !== 'query' && row.tool_name !== 'search')) {+ throw new BaselineParseError(`Row on line ${i + 1} missing or invalid tool_name`, i + 1);+ }+ if (typeof row.query !== 'string') {+ throw new BaselineParseError(`Row on line ${i + 1} missing query`, i + 1);+ }+ if (typeof row.query_hash !== 'string') {+ throw new BaselineParseError(`Row on line ${i + 1} missing query_hash (was it written by gbrain bench publish?)`, i + 1);+ }+ if (!Array.isArray(row.retrieved_slugs) || !Array.isArray(row.source_ids)) {+ throw new BaselineParseError(`Row on line ${i + 1} missing retrieved_slugs or source_ids`, i + 1);+ }+ rows.push(row as BaselineRow);+ }+ }++ if (metadata === null) {+ throw new BaselineParseError('Empty file or no metadata header found');+ }+ return { metadata, rows };+}++/**+ * Serialize a BaselineFile to NDJSON. Output is byte-deterministic given+ * the same input: rows sort by (tool_name, query_hash) before write.+ *+ * Each body row is stamped with `schema_version: 1` (same envelope as+ * `gbrain eval export`) so the existing `eval replay` parser accepts the+ * row unchanged. Without this stamp, replay's "Line N missing schema_version"+ * validator would reject every baseline body row.+ */+export function serializeBaselineFile(file: BaselineFile): string {+ const sortedRows = [...file.rows].sort((a, b) => {+ if (a.tool_name !== b.tool_name) return a.tool_name < b.tool_name ? -1 : 1;+ if (a.query_hash !== b.query_hash) return a.query_hash < b.query_hash ? -1 : 1;+ return 0;+ });+ const lines = [+ JSON.stringify(file.metadata),+ ...sortedRows.map(r => JSON.stringify({ schema_version: 1, ...r })),+ ];+ return lines.join('\n') + '\n';+}diff --git a/src/core/bench/correctness-gate.ts b/src/core/bench/correctness-gate.tsnew file mode 100644index 0000000000..017d82b37a--- /dev/null+++ b/src/core/bench/correctness-gate.ts@@ -0,0 +1,173 @@+/**+ * Correctness gate orchestrator (v0.41).+ *+ * Runs every qrels query against the brain via bare `hybridSearch` (per+ * eng-D6 — determinism over production-mirroring; matches the existing+ * eval harness pattern at src/core/search/eval.ts:242), then computes:+ * - mean_recall_at_k+ * - first_relevant_hit_rate+ * - expected_top1_hit_rate (when any entries set expected_top1)+ *+ * Compares are on `${source_id}::${slug}` strings (per eng-D5) so multi-+ * source brains don't false-pass via wrong-source hits.+ *+ * Failure mode (Finding 2D): a per-query exception (timeout, brain error,+ * etc.) flips verdict to `fail` with a per-query breach naming the+ * exception. The lenient alternative — drop the failing query from the+ * aggregate — would silently inflate scores by hiding hard queries.+ */++import type { BrainEngine } from '../engine.ts';+import { hybridSearch } from '../search/hybrid.ts';+import {+ computeExpectedTop1Hit,+ computeFirstRelevantHit,+ computeRecallAtK,+ DEFAULT_QRELS_THRESHOLDS,+ refKey,+ type QrelsFile,+ type QrelsEntry,+} from './qrels-file.ts';++export interface CorrectnessGateOpts {+ /** Top-K for recall@K. Defaults to DEFAULT_QRELS_THRESHOLDS.k (10). */+ k?: number;+ /**+ * Pluggable search function for tests. Default uses bare `hybridSearch`.+ * Tests inject a stub to drive deterministic per-query behavior without+ * a real brain.+ */+ searchFn?: (engine: BrainEngine, query: string, opts: { limit: number }) => Promise<Array<{ source_id?: string; slug: string }>>;+}++export interface PerQueryResult {+ query_id: string;+ query: string;+ recall_at_k: number;+ first_relevant_hit: 0 | 1;+ expected_top1_hit?: 0 | 1;+ retrieved_count: number;+ /** When the query throws, recorded as a per-query failure. */+ errored?: true;+ error_message?: string;+}++export interface CorrectnessSummary {+ k: number;+ queries_total: number;+ queries_run: number; // queries_total - queries_errored+ queries_errored: number;+ mean_recall_at_k: number;+ first_relevant_hit_rate: number;+ /** Denominator = queries with expected_top1 SET (not total queries). */+ expected_top1_hit_rate: number;+ expected_top1_denominator: number;+}++export interface CorrectnessResult {+ summary: CorrectnessSummary;+ per_query: PerQueryResult[];+}++/** Build the canonical `${source_id}::${slug}` set for a SearchResult-like array. */+function toRefKeySet(results: Array<{ source_id?: string; slug: string }>): string[] {+ return results.map(r => `${r.source_id ?? 'default'}::${r.slug}`);+}++async function runOneQuery(+ engine: BrainEngine,+ entry: QrelsEntry,+ k: number,+ searchFn: NonNullable<CorrectnessGateOpts['searchFn']>,+): Promise<PerQueryResult> {+ let retrieved: string[];+ try {+ const raw = await searchFn(engine, entry.query, { limit: k });+ retrieved = toRefKeySet(raw);+ } catch (err) {+ return {+ query_id: entry.query_id,+ query: entry.query,+ recall_at_k: 0,+ first_relevant_hit: 0,+ retrieved_count: 0,+ errored: true,+ error_message: (err as Error).message,+ };+ }++ const relevant = entry.relevant.map(refKey);+ const recall = computeRecallAtK(retrieved, relevant, k);+ const firstRelevant = computeFirstRelevantHit(retrieved, relevant);++ const out: PerQueryResult = {+ query_id: entry.query_id,+ query: entry.query,+ recall_at_k: recall,+ first_relevant_hit: firstRelevant,+ retrieved_count: retrieved.length,+ };++ if (entry.expected_top1) {+ out.expected_top1_hit = computeExpectedTop1Hit(retrieved, refKey(entry.expected_top1));+ }++ return out;+}++/**+ * Run the correctness gate against a brain. Returns per-query results ++ * aggregate summary. Does NOT throw on per-query failures (they're recorded+ * as `errored: true` in the per-query list and surface as a non-zero+ * `queries_errored` in the summary). DOES throw if the entire qrels file+ * is empty (caller bug; parseQrelsFile already rejects this shape).+ */+export async function runCorrectnessGate(+ engine: BrainEngine,+ qrels: QrelsFile,+ opts: CorrectnessGateOpts = {},+): Promise<CorrectnessResult> {+ if (qrels.queries.length === 0) {+ throw new Error('runCorrectnessGate: qrels file has no queries');+ }+ const k = opts.k ?? DEFAULT_QRELS_THRESHOLDS.k;+ const searchFn = opts.searchFn ?? (async (e, q, o) => {+ const results = await hybridSearch(e, q, { limit: o.limit });+ return results.map(r => ({ source_id: r.source_id, slug: r.slug }));+ });++ const perQuery: PerQueryResult[] = [];+ for (const entry of qrels.queries) {+ perQuery.push(await runOneQuery(engine, entry, k, searchFn));+ }++ const errored = perQuery.filter(p => p.errored).length;+ const run = perQuery.length - errored;+ const nonErrored = perQuery.filter(p => !p.errored);++ const meanRecall = nonErrored.length === 0+ ? 0+ : nonErrored.reduce((s, p) => s + p.recall_at_k, 0) / nonErrored.length;+ const firstRelevantRate = nonErrored.length === 0+ ? 0+ : nonErrored.reduce((s, p) => s + p.first_relevant_hit, 0) / nonErrored.length;++ const withExpectedTop1 = nonErrored.filter(p => p.expected_top1_hit !== undefined);+ const expectedTop1Rate = withExpectedTop1.length === 0+ ? 0+ : withExpectedTop1.reduce((s, p) => s + (p.expected_top1_hit ?? 0), 0) / withExpectedTop1.length;++ return {+ summary: {+ k,+ queries_total: perQuery.length,+ queries_run: run,+ queries_errored: errored,+ mean_recall_at_k: meanRecall,+ first_relevant_hit_rate: firstRelevantRate,+ expected_top1_hit_rate: expectedTop1Rate,+ expected_top1_denominator: withExpectedTop1.length,+ },+ per_query: perQuery,+ };+}diff --git a/src/core/bench/qrels-file.ts b/src/core/bench/qrels-file.tsnew file mode 100644index 0000000000..e162e7dda0--- /dev/null+++ b/src/core/bench/qrels-file.ts@@ -0,0 +1,230 @@+/**+ * Qrels file format for `gbrain eval gate --qrels`.+ *+ * Wire shape: JSON OBJECT (NOT bare array — preserves the existing fixture+ * shape at `test/fixtures/eval-baselines/qrels-search.json` per eng-D7).+ *+ * ```json+ * {+ * "schema_version": 1,+ * "_description": "...",+ * "queries": [+ * { "query_id": "q1", "query": "...", "relevant_slugs": ["slug-a"], "first_relevant_slug": "slug-a" },+ * { "query_id": "q2", "query": "...", "relevant": [{"source_id":"foo","slug":"slug-b"}], "expected_top1": {"source_id":"foo","slug":"slug-b"} }+ * ]+ * }+ * ```+ *+ * Two equivalent representations PER ENTRY:+ * - Legacy slug-only (`relevant_slugs` + `first_relevant_slug`): auto-promoted+ * to source_id 'default' for back-compat with the existing 12-row fixture.+ * - Federated (`relevant` + `expected_top1`): explicit `{source_id, slug}`+ * pairs per eng-D5 (codex round-2 #6 — slug-only false-passes on multi-source brains).+ *+ * Compares are on `${source_id}::${slug}` strings everywhere.+ */++export const QRELS_FILE_SCHEMA_VERSION = 1 as const;++/** Defaults when neither qrels-file nor CLI flags set them. */+export const DEFAULT_QRELS_THRESHOLDS = {+ recall_at_k: 0.70,+ first_relevant_hit: 0.60,+ /** Lower default because exact top-1 is harder than any-relevant top-1. */+ expected_top1: 0.50,+ /** k for recall@k unless overridden by CLI. */+ k: 10,+} as const;++export interface SourceSlugRef {+ source_id: string;+ slug: string;+}++export interface QrelsEntry {+ query_id: string;+ query: string;+ /** Normalized to {source_id, slug} pairs. Plain strings auto-promote to source_id='default'. */+ relevant: SourceSlugRef[];+ /** If set, retrieved[0] MUST equal this exact pair (strict top-1 metric). */+ expected_top1?: SourceSlugRef;+ label?: string;+ /** Pass-through fields from the existing fixture shape (kept for forward compat). */+ embedding_dim?: number;+}++export interface QrelsFile {+ schema_version: typeof QRELS_FILE_SCHEMA_VERSION;+ queries: QrelsEntry[];+ _description?: string;+}++export class QrelsParseError extends Error {+ constructor(message: string, public readonly entryIndex?: number) {+ super(message);+ this.name = 'QrelsParseError';+ }+}++/** Build the canonical `${source_id}::${slug}` compare key. */+export function makeRef(source_id: string, slug: string): string {+ return `${source_id}::${slug}`;+}++/** Build the canonical compare key from a SourceSlugRef. */+export function refKey(ref: SourceSlugRef): string {+ return makeRef(ref.source_id, ref.slug);+}++/**+ * Parse a qrels JSON file. Accepts both the legacy slug-only shape+ * (`relevant_slugs` + `first_relevant_slug`, source_id auto-defaults) and+ * the federated shape (`relevant` + `expected_top1` with explicit source_id).+ */+export function parseQrelsFile(content: string): QrelsFile {+ let parsed: unknown;+ try {+ parsed = JSON.parse(content);+ } catch (err) {+ throw new QrelsParseError(`Malformed JSON: ${err instanceof Error ? err.message : String(err)}`);+ }++ if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {+ throw new QrelsParseError(+ 'Qrels file must be a JSON object (got array or non-object). Expected shape: {"schema_version":1,"queries":[...]}',+ );+ }++ const file = parsed as Partial<QrelsFile> & { queries?: unknown };+ if (file.schema_version !== QRELS_FILE_SCHEMA_VERSION) {+ throw new QrelsParseError(+ `Unsupported schema_version ${file.schema_version} (this gbrain build expects ${QRELS_FILE_SCHEMA_VERSION})`,+ );+ }+ if (!Array.isArray(file.queries)) {+ throw new QrelsParseError('Qrels file missing "queries" array');+ }+ if (file.queries.length === 0) {+ throw new QrelsParseError('Qrels file has empty "queries" array — at least one entry required');+ }++ const queries: QrelsEntry[] = [];+ for (let i = 0; i < file.queries.length; i++) {+ const raw = file.queries[i];+ if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {+ throw new QrelsParseError(`Entry ${i} is not a JSON object`, i);+ }+ const entry = raw as unknown as Record<string, unknown>;++ const query_id = typeof entry.query_id === 'string' ? entry.query_id : `entry-${i}`;+ if (typeof entry.query !== 'string' || entry.query.trim() === '') {+ throw new QrelsParseError(`Entry ${i} (${query_id}) missing or empty "query"`, i);+ }++ // Normalize relevant: prefer federated `relevant`, fall back to legacy `relevant_slugs`.+ let relevant: SourceSlugRef[];+ if (Array.isArray(entry.relevant)) {+ relevant = entry.relevant.map((r, j) => {+ if (typeof r !== 'object' || r === null) {+ throw new QrelsParseError(`Entry ${i} (${query_id}) relevant[${j}] is not an object`, i);+ }+ const ref = r as Record<string, unknown>;+ if (typeof ref.source_id !== 'string' || typeof ref.slug !== 'string') {+ throw new QrelsParseError(+ `Entry ${i} (${query_id}) relevant[${j}] missing source_id or slug`,+ i,+ );+ }+ return { source_id: ref.source_id, slug: ref.slug };+ });+ } else if (Array.isArray(entry.relevant_slugs)) {+ relevant = entry.relevant_slugs.map((slug, j) => {+ if (typeof slug !== 'string') {+ throw new QrelsParseError(+ `Entry ${i} (${query_id}) relevant_slugs[${j}] is not a string`,+ i,+ );+ }+ return { source_id: 'default', slug };+ });+ } else {+ throw new QrelsParseError(+ `Entry ${i} (${query_id}) missing "relevant" or "relevant_slugs"`,+ i,+ );+ }+ if (relevant.length === 0) {+ throw new QrelsParseError(`Entry ${i} (${query_id}) has empty relevant set`, i);+ }++ // Normalize expected_top1: prefer federated `expected_top1`, fall back to legacy `first_relevant_slug`.+ let expected_top1: SourceSlugRef | undefined;+ if (entry.expected_top1 !== undefined) {+ const e = entry.expected_top1;+ if (typeof e !== 'object' || e === null) {+ throw new QrelsParseError(`Entry ${i} (${query_id}) expected_top1 is not an object`, i);+ }+ const ref = e as Record<string, unknown>;+ if (typeof ref.source_id !== 'string' || typeof ref.slug !== 'string') {+ throw new QrelsParseError(+ `Entry ${i} (${query_id}) expected_top1 missing source_id or slug`,+ i,+ );+ }+ expected_top1 = { source_id: ref.source_id, slug: ref.slug };+ } else if (typeof entry.first_relevant_slug === 'string') {+ expected_top1 = { source_id: 'default', slug: entry.first_relevant_slug };+ }++ const out: QrelsEntry = { query_id, query: entry.query, relevant };+ if (expected_top1) out.expected_top1 = expected_top1;+ if (typeof entry.label === 'string') out.label = entry.label;+ if (typeof entry.embedding_dim === 'number') out.embedding_dim = entry.embedding_dim;+ // The cast is safe: we built `out` from validated fields. The pass-through+ // shape may carry additional unknown keys we want to surface to consumers+ // (back-compat with the existing fixture's `query_id`, `embedding_dim`).+ queries.push(out);+ }++ return {+ schema_version: QRELS_FILE_SCHEMA_VERSION,+ queries,+ ...(typeof file._description === 'string' ? { _description: file._description } : {}),+ };+}++/**+ * recall_at_k = |intersect(retrieved[:k], relevant)| / |relevant|.+ * Both `retrieved` and `relevant` are arrays of `${source_id}::${slug}` keys.+ * Returns a number in [0, 1].+ */+export function computeRecallAtK(retrieved: string[], relevant: string[], k: number): number {+ if (relevant.length === 0) return 0;+ const relevantSet = new Set(relevant);+ const topK = retrieved.slice(0, k);+ let hits = 0;+ for (const r of topK) {+ if (relevantSet.has(r)) hits++;+ }+ return hits / relevant.length;+}++/**+ * first_relevant_hit = 1 if retrieved[0] in relevant else 0.+ * Empty retrieved → 0.+ */+export function computeFirstRelevantHit(retrieved: string[], relevant: string[]): 0 | 1 {+ if (retrieved.length === 0) return 0;+ const relevantSet = new Set(relevant);+ return relevantSet.has(retrieved[0]!) ? 1 : 0;+}++/**+ * expected_top1_hit = 1 if retrieved[0] === expected_top1 else 0.+ * Caller must check `expected_top1 !== undefined` before invoking.+ * Empty retrieved → 0.+ */+export function computeExpectedTop1Hit(retrieved: string[], expected_top1: string): 0 | 1 {+ if (retrieved.length === 0) return 0;+ return retrieved[0] === expected_top1 ? 1 : 0;+}diff --git a/src/core/config.ts b/src/core/config.tsindex 8d5f790c5f..623a3614d6 100644--- a/src/core/config.ts+++ b/src/core/config.ts@@ -80,6 +80,23 @@ export interface GBrainConfig {* those are different stores). Edit `~/.gbrain/config.json` directly.* All fields default to ON — capture and scrubbing both opt-out.*/+ /**+ * v0.41 — autopilot daemon configuration. Currently houses the nightly+ * quality probe feature flag (default OFF — opt-in to protect API spend+ * on fresh installs). Flag is gated INSIDE the autopilot tick body;+ * absence means "do not run nightly probe."+ */+ autopilot?: {+ nightly_quality_probe?: {+ /** Enable the nightly probe in the autopilot loop. Defaults to false. */+ enabled?: boolean;+ /**+ * Cost cap (USD) per probe invocation. Defaults to 5.+ * Worst case: 5 × 30 nights ≈ $150/month per brain.+ */+ max_usd?: number;+ };+ };eval?: {/** false disables capture entirely. Defaults to true. */capture?: boolean;diff --git a/src/core/cycle/nightly-probe-adapters.ts b/src/core/cycle/nightly-probe-adapters.tsnew file mode 100644index 0000000000..2ce34e1af5--- /dev/null+++ b/src/core/cycle/nightly-probe-adapters.ts@@ -0,0 +1,136 @@+/**+ * Bridge between `NightlyProbeDeps` (object-shape) and the existing CLI+ * functions (argv-shape) for `runEvalLongMemEval` + `runEvalCrossModal`.+ *+ * Per eng-D2: the existing CLI functions take argv arrays, not the object+ * shape the nightly-probe phase expects. The adapter converts; the CLI+ * functions stay unchanged.+ *+ * Per codex round-2 #1: `runEvalCrossModal --batch` only writes the summary+ * to `--output` (or its own default path). The adapter MUST pass+ * `--output summaryPath` so the file lands where the caller expects.+ *+ * Per codex round-2 #12: in-process invocation avoids the gbrain-version-+ * drift bug class. The adapter calls the CLI functions directly (not via+ * subprocess), so the workspace gbrain runs — not whatever's installed.+ */++import { readFileSync, existsSync } from 'node:fs';++/** Arguments accepted by the longmemeval adapter. */+export interface LongMemEvalProbeArgs {+ fixturePath: string;+ outputPath: string;+}++/** Arguments accepted by the cross-modal adapter. */+export interface CrossModalProbeArgs {+ batchPath: string;+ summaryPath: string;+ maxUsd: number;+}++/** Cross-modal batch summary shape (matches `runEvalCrossModal --batch --json`'s envelope). */+export interface CrossModalBatchSummary {+ pass_count: number;+ fail_count: number;+ inconclusive_count: number;+ error_count: number;+ est_cost_usd: number;+ verdict: string;+}++/**+ * Adapter for `runEvalLongMemEval`. Builds the argv shape the CLI expects+ * and calls it in-process.+ *+ * The CLI's first positional arg is `<dataset.jsonl>` (fixturePath).+ * `--output PATH` writes per-question rows.+ *+ * The CLI calls `process.exit(1)` on errors. The adapter doesn't trap+ * exit — the caller (nightly-quality-probe phase) wraps in try/catch and+ * treats any exit-style failure as a probe failure that doesn't crash+ * autopilot.+ */+export async function runLongMemEvalForProbe(args: LongMemEvalProbeArgs): Promise<void> {+ const { runEvalLongMemEval } = await import('../../commands/eval-longmemeval.ts');+ await runEvalLongMemEval([args.fixturePath, '--output', args.outputPath]);+}++/**+ * Adapter for `runEvalCrossModal --batch`. Threads `--output` so the+ * summary lands at the caller-controlled path (codex round-2 #1 fix),+ * then reads + parses the summary from that path.+ *+ * Returns `{ exitCode, summary }` shape so the caller can both surface the+ * verdict and decide what to do with non-zero exit codes (cost overrun,+ * gate failure, etc).+ *+ * Throws if `summaryPath` is missing after the run (caller misconfigured+ * the batch input) or unparseable (cross-modal wrote garbage). Both+ * cases are paste-ready in the error message.+ */+export async function runCrossModalBatchForProbe(+ args: CrossModalProbeArgs,+): Promise<{ exitCode: number; summary: CrossModalBatchSummary }> {+ const { runEvalCrossModal } = await import('../../commands/eval-cross-modal.ts');+ const exitCode = await runEvalCrossModal([+ '--batch',+ args.batchPath,+ '--output',+ args.summaryPath,+ '--max-usd',+ String(args.maxUsd),+ '--yes',+ '--json',+ ]);++ if (!existsSync(args.summaryPath)) {+ throw new Error(+ `nightly-probe-adapter: cross-modal --batch finished (exit ${exitCode}) but ` ++ `summary file is missing at ${args.summaryPath}. ` ++ `Hint: confirm the batch input JSONL is valid and writable.`,+ );+ }++ let raw: string;+ try {+ raw = readFileSync(args.summaryPath, 'utf-8');+ } catch (err) {+ throw new Error(+ `nightly-probe-adapter: could not read cross-modal summary at ${args.summaryPath}: ` ++ `${(err as Error).message}`,+ );+ }++ let parsed: unknown;+ try {+ parsed = JSON.parse(raw);+ } catch (err) {+ throw new Error(+ `nightly-probe-adapter: cross-modal summary at ${args.summaryPath} is malformed JSON: ` ++ `${(err as Error).message}. First 200 chars: ${raw.slice(0, 200)}`,+ );+ }++ if (typeof parsed !== 'object' || parsed === null) {+ throw new Error(+ `nightly-probe-adapter: cross-modal summary at ${args.summaryPath} is not a JSON object`,+ );+ }++ // Cross-modal --batch --json wraps the summary as a top-level object;+ // pick the fields we care about and pass through. Tolerate the shape+ // being slightly larger (e.g. per-question receipts inline).+ const obj = parsed as Record<string, unknown>;+ const summary: CrossModalBatchSummary = {+ pass_count: Number(obj.pass_count ?? 0),+ fail_count: Number(obj.fail_count ?? 0),+ inconclusive_count: Number(obj.inconclusive_count ?? 0),+ error_count: Number(obj.error_count ?? 0),+ est_cost_usd: Number(obj.est_cost_usd ?? 0),+ verdict: typeof obj.verdict === 'string' ? obj.verdict : 'unknown',+ };++ return { exitCode, summary };+}diff --git a/test/autopilot-nightly-probe-wiring.test.ts b/test/autopilot-nightly-probe-wiring.test.tsnew file mode 100644index 0000000000..207beba8e7--- /dev/null+++ b/test/autopilot-nightly-probe-wiring.test.ts@@ -0,0 +1,75 @@+/**+ * Source-shape regression tests for the v0.41 autopilot wiring of+ * `runNightlyQualityProbe`.+ *+ * The autopilot loop is hard to drive end-to-end without spinning a real+ * daemon (database, queue, gateway, etc). These tests pin the structural+ * shape of the wiring — the feature flag check, the try/catch, the DI+ * shape passed to runNightlyQualityProbe — so future refactors can't+ * silently strip the protections without a CI signal.+ *+ * The pure decision logic lives in shouldRunNightly (already pinned by+ * tests in nightly-quality-probe.test.ts).+ */++import { describe, test, expect } from 'bun:test';+import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';++const AUTOPILOT_SRC = resolve('src/commands/autopilot.ts');+const SOURCE = readFileSync(AUTOPILOT_SRC, 'utf-8');++describe('autopilot wiring: nightly quality probe', () => {+ test('imports runNightlyQualityProbe from the phase module', () => {+ expect(SOURCE).toContain(`runNightlyQualityProbe`);+ expect(SOURCE).toContain(`nightly-quality-probe`);+ });++ test('uses the eng-D2 adapter module (not direct subprocess of eval-longmemeval/cross-modal)', () => {+ expect(SOURCE).toContain(`nightly-probe-adapters`);+ expect(SOURCE).toContain(`runLongMemEvalForProbe`);+ expect(SOURCE).toContain(`runCrossModalBatchForProbe`);+ });++ test('feature flag gate present: cfg.autopilot.nightly_quality_probe.enabled', () => {+ // Per D10: the scheduler ONLY checks the feature flag. The 24h rate-limit+ // lives inside runNightlyQualityProbe itself (no scheduler-side precheck).+ expect(SOURCE).toContain(`nightly_quality_probe?.enabled === true`);+ });++ test('NO scheduler-side rate-limit check (D10 simplification)', () => {+ // Codex round-1 #11 caught: scheduler-side rate-limit duplicates phase-internal logic.+ // The wiring code MUST NOT call shouldRunNightly directly OR read recent events+ // before invoking the phase.+ expect(SOURCE).not.toContain(`shouldRunNightly(`);+ expect(SOURCE).not.toContain(`readRecentQualityProbeEvents(`);+ });++ test('probe call wrapped in try/catch that does NOT bump consecutiveErrors', () => {+ // The try/catch around the probe must log the error but never crash the loop.+ // We verify the structural pattern: the probe call is inside a try block,+ // the catch block calls logError, and consecutiveErrors is not bumped inside the catch.+ expect(SOURCE).toMatch(/try\s*\{\s*[^}]*nightly_quality_probe/);+ expect(SOURCE).toMatch(/catch[\s\S]*?autopilot\.nightly_probe[\s\S]*?do NOT bump consecutiveErrors/);+ });++ test('DI shape: isEnabled / hasEmbeddingProvider / resolveMaxUsd / resolveRepoRoot / runLongMemEval / runCrossModalBatch / now', () => {+ // The exact 7 fields of NightlyProbeDeps.+ expect(SOURCE).toContain(`isEnabled:`);+ expect(SOURCE).toContain(`hasEmbeddingProvider:`);+ expect(SOURCE).toContain(`resolveMaxUsd:`);+ expect(SOURCE).toContain(`resolveRepoRoot:`);+ expect(SOURCE).toContain(`runLongMemEval:`);+ expect(SOURCE).toContain(`runCrossModalBatch:`);+ expect(SOURCE).toContain(`now:`);+ });++ test('hasEmbeddingProvider reads from gateway.isAvailable("embedding") (codex round-2 #12 — in-process, not subprocess)', () => {+ expect(SOURCE).toContain(`isAvailable('embedding')`);+ expect(SOURCE).toContain(`gateway`);+ });++ test('max_usd default = 5 when config unset (matches plan default per D10)', () => {+ expect(SOURCE).toMatch(/max_usd\s*\?\?\s*5/);+ });+});diff --git a/test/bench-publish.test.ts b/test/bench-publish.test.tsnew file mode 100644index 0000000000..57e08d92b2--- /dev/null+++ b/test/bench-publish.test.ts@@ -0,0 +1,135 @@+import { describe, test, expect } from 'bun:test';+import { writeFileSync, readFileSync, mkdtempSync, rmSync, existsSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++import { buildBaselineFromInput } from '../src/commands/bench-publish.ts';+import {+ parseBaselineFile,+ serializeBaselineFile,+ computeQueryHash,+ DEFAULT_THRESHOLDS,+} from '../src/core/bench/baseline-file.ts';+import type { EvalCandidateInput } from '../src/core/types.ts';++function makeInput(query: string, opts: { source_ids?: string[]; latency_ms?: number; tool_name?: 'query' | 'search' } = {}): EvalCandidateInput {+ return {+ tool_name: opts.tool_name ?? 'query',+ query,+ retrieved_slugs: [`slug-for-${query.slice(0, 10)}`],+ retrieved_chunk_ids: [1],+ source_ids: opts.source_ids ?? ['default'],+ expand_enabled: false,+ detail: 'medium',+ detail_resolved: 'medium',+ vector_enabled: true,+ expansion_applied: false,+ latency_ms: opts.latency_ms ?? 100,+ remote: false,+ job_id: null,+ subagent_id: null,+ };+}++describe('bench-publish: buildBaselineFromInput', () => {+ test('happy path: input rows → BaselineFile with metadata + query_hash stamped', () => {+ const input = [makeInput('hello world'), makeInput('lorem ipsum')];+ const file = buildBaselineFromInput(input, { label: 'test-1' });++ expect(file.metadata.label).toBe('test-1');+ expect(file.metadata._kind).toBe('baseline_metadata');+ expect(file.metadata.row_count).toBe(2);+ expect(file.metadata.baseline_mean_latency_ms).toBe(100);+ expect(file.rows).toHaveLength(2);+ expect(file.rows[0]!.query_hash).toBe(computeQueryHash(file.rows[0]!.query));+ });++ test('threshold CLI overrides win over defaults', () => {+ const input = [makeInput('x')];+ const file = buildBaselineFromInput(input, {+ label: 'x',+ thresholds: { jaccard: 0.9 },+ });+ expect(file.metadata.thresholds.jaccard).toBe(0.9);+ expect(file.metadata.thresholds.top1).toBe(DEFAULT_THRESHOLDS.top1); // unchanged+ });++ test('baseline_mean_latency_ms computed from input rows', () => {+ const input = [makeInput('a', { latency_ms: 100 }), makeInput('b', { latency_ms: 300 })];+ const file = buildBaselineFromInput(input, { label: 'x' });+ expect(file.metadata.baseline_mean_latency_ms).toBe(200);+ });++ test('strict: empty input → throws "no rows to publish"', () => {+ expect(() => buildBaselineFromInput([], { label: 'x' })).toThrow(/no rows to publish/);+ });++ test('strict: duplicate (tool_name, source_ids, query_hash) → throws with first 5 listed', () => {+ const input = [makeInput('same query'), makeInput('same query')];+ expect(() => buildBaselineFromInput(input, { label: 'x' })).toThrow(/duplicate/i);+ });++ test('multi-source: SAME query against DIFFERENT source_ids is NOT a dupe (eng-D5)', () => {+ const input = [+ makeInput('same query', { source_ids: ['source-a'] }),+ makeInput('same query', { source_ids: ['source-b'] }),+ ];+ // Should NOT throw — different source_ids → different dedup key.+ const file = buildBaselineFromInput(input, { label: 'x' });+ expect(file.rows).toHaveLength(2);+ });++ test('round-trip: serialize → parse preserves all fields byte-stable', () => {+ const input = [makeInput('foo'), makeInput('bar'), makeInput('baz')];+ const file = buildBaselineFromInput(input, {+ label: 'roundtrip',+ publishedAt: new Date('2026-05-24T00:00:00Z'),+ });+ const serialized = serializeBaselineFile(file);+ const parsed = parseBaselineFile(serialized);+ expect(parsed.metadata.label).toBe('roundtrip');+ expect(parsed.metadata.published_at).toBe('2026-05-24T00:00:00.000Z');+ expect(parsed.rows).toHaveLength(3);+ // Deterministic serialize: same input → byte-identical output.+ expect(serializeBaselineFile(file)).toBe(serialized);+ });++ test('source_hash stable across publish runs of same input', () => {+ const input = [makeInput('alpha'), makeInput('beta')];+ const f1 = buildBaselineFromInput(input, { label: 'x' });+ const f2 = buildBaselineFromInput(input, { label: 'x' });+ expect(f1.metadata.source_hash).toBe(f2.metadata.source_hash);+ });+});++describe('bench-publish: CLI lifecycle (smoke)', () => {+ test('CLI writes a baseline file end-to-end', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'bench-publish-test-'));+ const fromPath = join(dir, 'captured.ndjson');+ const toPath = join(dir, 'out.baseline.ndjson');++ const rows = [makeInput('foo'), makeInput('bar')];+ writeFileSync(fromPath, rows.map(r => JSON.stringify({ schema_version: 1, ...r })).join('\n') + '\n');++ // Import and run programmatically (avoids subprocess; we want assertions on file content).+ const { runBenchPublish } = await import('../src/commands/bench-publish.ts');+ // runBenchPublish is process.exit-based; can't call directly here without+ // catching the exit. Use buildBaselineFromInput + serializeBaselineFile+ // for the assertion path (covered above). This smoke verifies CLI args+ // parse without throwing.+ const args = ['--from', fromPath, '--to', toPath, '--label', 'smoke-test', '--json'];+ void args; // CLI smoke covered in e2e LOOP test.+ void runBenchPublish;++ // Sanity: the helper functions produce a file the CLI would write.+ const file = buildBaselineFromInput(rows, { label: 'smoke-test' });+ writeFileSync(toPath, serializeBaselineFile(file));+ expect(existsSync(toPath)).toBe(true);+ const content = readFileSync(toPath, 'utf-8');+ const firstLine = JSON.parse(content.split('\n')[0]!);+ expect(firstLine._kind).toBe('baseline_metadata');+ expect(firstLine.label).toBe('smoke-test');++ rmSync(dir, { recursive: true, force: true });+ });+});diff --git a/test/bench/baseline-file.test.ts b/test/bench/baseline-file.test.tsnew file mode 100644index 0000000000..f7504c12f9--- /dev/null+++ b/test/bench/baseline-file.test.ts@@ -0,0 +1,132 @@+import { describe, test, expect } from 'bun:test';+import {+ BASELINE_FILE_SCHEMA_VERSION,+ DEFAULT_THRESHOLDS,+ BaselineParseError,+ computeQueryHash,+ computeSourceHash,+ normalizeQueryForHash,+ parseBaselineFile,+ serializeBaselineFile,+ type BaselineFile,+ type BaselineRow,+} from '../../src/core/bench/baseline-file.ts';++function makeRow(query: string, idx: number): BaselineRow {+ return {+ tool_name: 'query',+ query,+ query_hash: computeQueryHash(query),+ retrieved_slugs: [`slug-${idx}`],+ retrieved_chunk_ids: [idx],+ source_ids: ['default'],+ expand_enabled: false,+ detail: 'medium',+ detail_resolved: 'medium',+ vector_enabled: true,+ expansion_applied: false,+ latency_ms: 100 + idx,+ remote: false,+ job_id: null,+ subagent_id: null,+ };+}++function makeFile(rows: BaselineRow[]): BaselineFile {+ return {+ metadata: {+ schema_version: BASELINE_FILE_SCHEMA_VERSION,+ _kind: 'baseline_metadata',+ label: 'test-label',+ published_at: '2026-05-24T00:00:00Z',+ source_hash: computeSourceHash(rows),+ thresholds: { ...DEFAULT_THRESHOLDS },+ row_count: rows.length,+ baseline_mean_latency_ms:+ rows.length === 0 ? 0 : rows.reduce((s, r) => s + r.latency_ms, 0) / rows.length,+ },+ rows,+ };+}++describe('baseline-file', () => {+ test('round-trip parse/serialize preserves metadata and rows', () => {+ const rows = [makeRow('hello world', 0), makeRow('lorem ipsum', 1)];+ const file = makeFile(rows);+ const serialized = serializeBaselineFile(file);+ const parsed = parseBaselineFile(serialized);++ expect(parsed.metadata.label).toBe('test-label');+ expect(parsed.metadata.schema_version).toBe(BASELINE_FILE_SCHEMA_VERSION);+ expect(parsed.metadata._kind).toBe('baseline_metadata');+ expect(parsed.rows).toHaveLength(2);+ expect(parsed.rows.map(r => r.query).sort()).toEqual(['hello world', 'lorem ipsum']);+ });++ test('threshold defaults match the documented values', () => {+ expect(DEFAULT_THRESHOLDS.jaccard).toBe(0.85);+ expect(DEFAULT_THRESHOLDS.top1).toBe(0.80);+ expect(DEFAULT_THRESHOLDS.latency_multiplier).toBe(2.0);+ });++ test('source_hash is deterministic across runs of the same input', () => {+ const rows1 = [makeRow('alpha', 0), makeRow('beta', 1), makeRow('gamma', 2)];+ const rows2 = [makeRow('beta', 1), makeRow('gamma', 2), makeRow('alpha', 0)]; // different order+ expect(computeSourceHash(rows1)).toBe(computeSourceHash(rows2));+ });++ test('reject metadata header missing required fields', () => {+ const bad = JSON.stringify({ schema_version: 1, _kind: 'baseline_metadata', label: 'x' }) + '\n';+ expect(() => parseBaselineFile(bad)).toThrow(BaselineParseError);+ });++ test('serializeBaselineFile produces byte-identical output on same input', () => {+ const rows = [makeRow('foo', 0), makeRow('bar', 1), makeRow('baz', 2)];+ const file = makeFile(rows);+ expect(serializeBaselineFile(file)).toBe(serializeBaselineFile(file));+ });++ test('normalizeQueryForHash is idempotent across whitespace and case', () => {+ expect(normalizeQueryForHash(' Hello World ')).toBe('hello world');+ expect(computeQueryHash(' Hello World ')).toBe(computeQueryHash('hello world'));+ });++ test('reject schema_version mismatch with a clear message', () => {+ const bad = JSON.stringify({+ schema_version: 999,+ _kind: 'baseline_metadata',+ label: 'x',+ published_at: 'z',+ source_hash: 'h',+ row_count: 0,+ baseline_mean_latency_ms: 0,+ thresholds: { jaccard: 0.85, top1: 0.8, latency_multiplier: 2.0 },+ }) + '\n';+ expect(() => parseBaselineFile(bad)).toThrow(/schema_version/);+ });++ test('reject empty file with no metadata', () => {+ expect(() => parseBaselineFile('')).toThrow(BaselineParseError);+ expect(() => parseBaselineFile('\n\n\n')).toThrow(BaselineParseError);+ });++ test('reject row missing query_hash (was it written by bench publish?)', () => {+ const meta = JSON.stringify({+ schema_version: 1,+ _kind: 'baseline_metadata',+ label: 'x',+ published_at: '2026-01-01T00:00:00Z',+ source_hash: 'h',+ row_count: 1,+ baseline_mean_latency_ms: 100,+ thresholds: { jaccard: 0.85, top1: 0.8, latency_multiplier: 2.0 },+ });+ const row = JSON.stringify({+ tool_name: 'query',+ query: 'hi',+ retrieved_slugs: ['x'],+ source_ids: ['default'],+ });+ expect(() => parseBaselineFile(meta + '\n' + row + '\n')).toThrow(/query_hash/);+ });+});diff --git a/test/bench/correctness-gate.test.ts b/test/bench/correctness-gate.test.tsnew file mode 100644index 0000000000..56b2dc307c--- /dev/null+++ b/test/bench/correctness-gate.test.ts@@ -0,0 +1,148 @@+import { describe, test, expect } from 'bun:test';+import { runCorrectnessGate } from '../../src/core/bench/correctness-gate.ts';+import type { QrelsFile } from '../../src/core/bench/qrels-file.ts';+import type { BrainEngine } from '../../src/core/engine.ts';++// The correctness gate's only engine touchpoint is `searchFn`, which is+// injectable. Tests pass a fake engine + a deterministic search stub.+const fakeEngine = {} as unknown as BrainEngine;++function makeQrels(queries: QrelsFile['queries']): QrelsFile {+ return { schema_version: 1, queries };+}++describe('correctness-gate: per-query iteration + aggregate math', () => {+ test('perfect retrieval → mean_recall=1, first_relevant=1, expected_top1=1', async () => {+ const qrels = makeQrels([+ {+ query_id: 'q1',+ query: 'x',+ relevant: [{ source_id: 'default', slug: 'a' }, { source_id: 'default', slug: 'b' }],+ expected_top1: { source_id: 'default', slug: 'a' },+ },+ ]);+ const result = await runCorrectnessGate(fakeEngine, qrels, {+ k: 10,+ searchFn: async () => [+ { source_id: 'default', slug: 'a' },+ { source_id: 'default', slug: 'b' },+ ],+ });+ expect(result.summary.mean_recall_at_k).toBe(1);+ expect(result.summary.first_relevant_hit_rate).toBe(1);+ expect(result.summary.expected_top1_hit_rate).toBe(1);+ expect(result.summary.queries_errored).toBe(0);+ });++ test('per-query throw → errored=true; query NOT counted in aggregates; gate flagged', async () => {+ // Finding 2D: a query throw flips verdict to fail. The orchestrator records+ // the throw as a per-query failure; the caller (eval-gate.ts) treats+ // any queries_errored > 0 as a gate failure.+ const qrels = makeQrels([+ {+ query_id: 'q-throws',+ query: 'x',+ relevant: [{ source_id: 'default', slug: 'a' }],+ },+ {+ query_id: 'q-works',+ query: 'y',+ relevant: [{ source_id: 'default', slug: 'b' }],+ },+ ]);+ let called = 0;+ const result = await runCorrectnessGate(fakeEngine, qrels, {+ k: 10,+ searchFn: async () => {+ called++;+ if (called === 1) throw new Error('simulated brain timeout');+ return [{ source_id: 'default', slug: 'b' }];+ },+ });+ expect(result.summary.queries_total).toBe(2);+ expect(result.summary.queries_run).toBe(1);+ expect(result.summary.queries_errored).toBe(1);+ // Aggregate computed on non-errored only.+ expect(result.summary.mean_recall_at_k).toBe(1);+ // Errored query surfaced in per_query list with error_message.+ const errored = result.per_query.find(p => p.errored);+ expect(errored?.error_message).toMatch(/timeout/);+ });++ test('missing brain page (slug not in retrieved) counted as miss', async () => {+ const qrels = makeQrels([+ {+ query_id: 'q1',+ query: 'x',+ relevant: [{ source_id: 'default', slug: 'a' }, { source_id: 'default', slug: 'b' }],+ },+ ]);+ const result = await runCorrectnessGate(fakeEngine, qrels, {+ searchFn: async () => [{ source_id: 'default', slug: 'a' }], // only 'a' retrieved+ });+ expect(result.summary.mean_recall_at_k).toBe(0.5); // 1 of 2 relevant+ expect(result.summary.first_relevant_hit_rate).toBe(1); // top-1 was 'a' which is relevant+ });++ test('empty retrieved list → recall=0 / first_relevant=0 / expected_top1=0', async () => {+ const qrels = makeQrels([+ {+ query_id: 'q1',+ query: 'x',+ relevant: [{ source_id: 'default', slug: 'a' }],+ expected_top1: { source_id: 'default', slug: 'a' },+ },+ ]);+ const result = await runCorrectnessGate(fakeEngine, qrels, {+ searchFn: async () => [],+ });+ expect(result.summary.mean_recall_at_k).toBe(0);+ expect(result.summary.first_relevant_hit_rate).toBe(0);+ expect(result.summary.expected_top1_hit_rate).toBe(0);+ expect(result.summary.queries_errored).toBe(0); // empty result != error+ });++ test('multi-source: wrong-source hit does NOT count as relevant (eng-D5 regression)', async () => {+ // Same slug "people/alice" in two sources; qrels says we want host's+ // version specifically. Retrieval returns team-a's version. That's NOT+ // a hit — the eng-D5 fix is structurally enforced via source_id::slug+ // compare keys.+ const qrels = makeQrels([+ {+ query_id: 'q1',+ query: 'x',+ relevant: [{ source_id: 'host', slug: 'people/alice' }],+ expected_top1: { source_id: 'host', slug: 'people/alice' },+ },+ ]);+ const result = await runCorrectnessGate(fakeEngine, qrels, {+ searchFn: async () => [{ source_id: 'team-a', slug: 'people/alice' }],+ });+ expect(result.summary.mean_recall_at_k).toBe(0);+ expect(result.summary.first_relevant_hit_rate).toBe(0);+ expect(result.summary.expected_top1_hit_rate).toBe(0);+ });++ test('expected_top1_hit_rate denominator = queries WITH expected_top1 only', async () => {+ const qrels = makeQrels([+ {+ query_id: 'q1',+ query: 'a',+ relevant: [{ source_id: 'default', slug: 'a' }],+ expected_top1: { source_id: 'default', slug: 'a' },+ },+ {+ query_id: 'q2',+ query: 'b',+ relevant: [{ source_id: 'default', slug: 'b' }],+ // no expected_top1 set+ },+ ]);+ const result = await runCorrectnessGate(fakeEngine, qrels, {+ searchFn: async (_e, q) => [{ source_id: 'default', slug: q }],+ });+ // 1 of 1 query with expected_top1 matched (q1).+ expect(result.summary.expected_top1_denominator).toBe(1);+ expect(result.summary.expected_top1_hit_rate).toBe(1);+ });+});diff --git a/test/bench/qrels-file.test.ts b/test/bench/qrels-file.test.tsnew file mode 100644index 0000000000..a8fbff1669--- /dev/null+++ b/test/bench/qrels-file.test.ts@@ -0,0 +1,144 @@+import { describe, test, expect } from 'bun:test';+import {+ QRELS_FILE_SCHEMA_VERSION,+ DEFAULT_QRELS_THRESHOLDS,+ QrelsParseError,+ makeRef,+ refKey,+ parseQrelsFile,+ computeRecallAtK,+ computeFirstRelevantHit,+ computeExpectedTop1Hit,+} from '../../src/core/bench/qrels-file.ts';++describe('qrels-file: parser', () => {+ test('parses the existing legacy fixture shape (relevant_slugs + first_relevant_slug)', () => {+ const legacy = {+ schema_version: 1,+ queries: [+ {+ query_id: 'q1',+ query: 'fintech founder',+ relevant_slugs: ['people/alice', 'companies/widget-co'],+ first_relevant_slug: 'people/alice',+ },+ ],+ };+ const parsed = parseQrelsFile(JSON.stringify(legacy));+ expect(parsed.queries).toHaveLength(1);+ expect(parsed.queries[0]!.query).toBe('fintech founder');+ // Legacy slugs promote to source_id='default'.+ expect(parsed.queries[0]!.relevant).toEqual([+ { source_id: 'default', slug: 'people/alice' },+ { source_id: 'default', slug: 'companies/widget-co' },+ ]);+ expect(parsed.queries[0]!.expected_top1).toEqual({ source_id: 'default', slug: 'people/alice' });+ });++ test('parses the federated shape (relevant + expected_top1 with explicit source_id)', () => {+ const federated = {+ schema_version: 1,+ queries: [+ {+ query_id: 'q1',+ query: 'fintech founder',+ relevant: [+ { source_id: 'host', slug: 'people/alice' },+ { source_id: 'team-a', slug: 'people/alice' },+ ],+ expected_top1: { source_id: 'host', slug: 'people/alice' },+ },+ ],+ };+ const parsed = parseQrelsFile(JSON.stringify(federated));+ expect(parsed.queries[0]!.relevant).toHaveLength(2);+ // Multi-source: same slug, different source_id, both treated as distinct.+ expect(refKey(parsed.queries[0]!.relevant[0]!)).toBe('host::people/alice');+ expect(refKey(parsed.queries[0]!.relevant[1]!)).toBe('team-a::people/alice');+ });++ test('rejects bare JSON array (must be object with schema_version)', () => {+ expect(() => parseQrelsFile('[]')).toThrow(QrelsParseError);+ });++ test('rejects missing queries field', () => {+ expect(() => parseQrelsFile(JSON.stringify({ schema_version: 1 }))).toThrow(/queries/);+ });++ test('rejects empty queries array', () => {+ expect(() => parseQrelsFile(JSON.stringify({ schema_version: 1, queries: [] }))).toThrow(+ /empty/,+ );+ });++ test('rejects entry with empty relevant set', () => {+ expect(() =>+ parseQrelsFile(+ JSON.stringify({+ schema_version: 1,+ queries: [{ query_id: 'q1', query: 'x', relevant_slugs: [] }],+ }),+ ),+ ).toThrow(/empty relevant/);+ });+});++describe('qrels-file: math', () => {+ test('computeRecallAtK perfect = 1.0', () => {+ expect(computeRecallAtK(['a', 'b', 'c'], ['a', 'b', 'c'], 10)).toBe(1.0);+ });++ test('computeRecallAtK zero = 0.0', () => {+ expect(computeRecallAtK(['x', 'y', 'z'], ['a', 'b'], 10)).toBe(0);+ });++ test('computeRecallAtK partial', () => {+ expect(computeRecallAtK(['a', 'x'], ['a', 'b'], 10)).toBe(0.5);+ });++ test('computeRecallAtK k smaller than retrieved truncates', () => {+ // Only top-1 is 'a'; relevant is 'a' + 'b'; k=1 → 1/2 = 0.5.+ expect(computeRecallAtK(['a', 'b', 'c'], ['a', 'b'], 1)).toBe(0.5);+ });++ test('computeRecallAtK empty relevant set returns 0 (defensive)', () => {+ expect(computeRecallAtK(['a'], [], 10)).toBe(0);+ });++ test('computeFirstRelevantHit retrieved[0] in relevant', () => {+ expect(computeFirstRelevantHit(['a', 'b'], ['a', 'c'])).toBe(1);+ });++ test('computeFirstRelevantHit retrieved[0] not in relevant', () => {+ expect(computeFirstRelevantHit(['x', 'a'], ['a', 'b'])).toBe(0);+ });++ test('computeFirstRelevantHit empty retrieved = 0', () => {+ expect(computeFirstRelevantHit([], ['a'])).toBe(0);+ });++ test('computeExpectedTop1Hit exact match = 1', () => {+ expect(computeExpectedTop1Hit(['default::a', 'default::b'], 'default::a')).toBe(1);+ });++ test('computeExpectedTop1Hit different source_id = 0 (multi-source guard)', () => {+ // Same slug, different source → NOT a hit (eng-D5 regression guard).+ expect(computeExpectedTop1Hit(['team-a::people/alice'], 'host::people/alice')).toBe(0);+ });++ test('makeRef/refKey format', () => {+ expect(makeRef('host', 'people/alice')).toBe('host::people/alice');+ expect(refKey({ source_id: 'host', slug: 'people/alice' })).toBe('host::people/alice');+ });++ test('DEFAULT_QRELS_THRESHOLDS shape and values', () => {+ expect(DEFAULT_QRELS_THRESHOLDS.recall_at_k).toBe(0.70);+ expect(DEFAULT_QRELS_THRESHOLDS.first_relevant_hit).toBe(0.60);+ expect(DEFAULT_QRELS_THRESHOLDS.expected_top1).toBe(0.50);+ expect(DEFAULT_QRELS_THRESHOLDS.k).toBe(10);+ });++ test('schema version constant matches parser expectation', () => {+ expect(QRELS_FILE_SCHEMA_VERSION).toBe(1);+ });+});diff --git a/test/cycle/nightly-probe-adapters.test.ts b/test/cycle/nightly-probe-adapters.test.tsnew file mode 100644index 0000000000..d6ba5ebb3a--- /dev/null+++ b/test/cycle/nightly-probe-adapters.test.ts@@ -0,0 +1,142 @@+/**+ * Unit tests for `src/core/cycle/nightly-probe-adapters.ts`.+ *+ * The adapters bridge object-shape `NightlyProbeDeps` arguments to the+ * existing argv-array CLI functions. Tests pin:+ * - argv shape passed to each underlying CLI function (codex round-2 #1)+ * - receipt file parsing happy path+ * - missing receipt file → throws with paste-ready hint+ * - malformed receipt JSON → throws with the bad content prefix+ * - exit-code passthrough+ */++import { describe, test, expect } from 'bun:test';+import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++import {+ runCrossModalBatchForProbe,+} from '../../src/core/cycle/nightly-probe-adapters.ts';++// We can't easily mock the actual CLI functions without `mock.module`+// (which would force this file to `*.serial.test.ts`). Instead, we test+// the adapter's pure file-handling logic by mocking the imported function+// via `__setCrossModalForTests` ... but the adapter file doesn't expose+// one. So we test the contract that the cross-modal adapter REJECTS+// missing/malformed receipts deterministically.++describe('nightly-probe-adapters: cross-modal receipt parsing', () => {+ test('missing summary file → throws with paste-ready hint', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'nightly-adapter-'));+ const summaryPath = join(dir, 'never-written.json');++ // We can't actually run runEvalCrossModal here without a real LLM key.+ // The adapter calls the CLI then reads the file. We exercise the+ // "missing file" branch by pointing at a non-existent path with a+ // batch input that the CLI will likely error on quickly — but we+ // expect to land in the "summary missing" throw, NOT in cross-modal's+ // actual execution. Use a non-existent batch path so cross-modal+ // exits 1 fast.+ const batchPath = join(dir, 'nonexistent-batch.jsonl');++ let threw: unknown;+ try {+ await runCrossModalBatchForProbe({+ batchPath,+ summaryPath,+ maxUsd: 0.01,+ });+ } catch (err) {+ threw = err;+ }++ // EITHER the adapter throws our specific "summary file missing" error,+ // OR cross-modal throws first on the nonexistent batch path. Both are+ // legitimate failure modes; the adapter must end up throwing SOME error.+ expect(threw).toBeDefined();+ rmSync(dir, { recursive: true, force: true });+ });++ test('malformed summary JSON → throws with content prefix', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'nightly-adapter-'));+ const summaryPath = join(dir, 'bad-summary.json');++ // Pre-write malformed JSON so the adapter's parse-error path fires+ // when (if) cross-modal completes and the adapter reads the file.+ writeFileSync(summaryPath, '{not valid json');++ // Same caveat as above — we can't exercise the full cross-modal path+ // without an API key, but we can verify the adapter's behavior when+ // the receipt file exists but is bad. The cross-modal CLI may overwrite+ // our content; that's OK — the test pins that the adapter throws on+ // failure rather than returning garbage. Use nonexistent batch input.+ const batchPath = join(dir, 'nonexistent-batch.jsonl');++ let threw: unknown;+ try {+ await runCrossModalBatchForProbe({+ batchPath,+ summaryPath,+ maxUsd: 0.01,+ });+ } catch (err) {+ threw = err;+ }+ expect(threw).toBeDefined();+ rmSync(dir, { recursive: true, force: true });+ });+});++describe('nightly-probe-adapters: argv shape regression (codex round-2 #1)', () => {+ test('adapter argv shape includes --output explicitly (regression for codex finding)', () => {+ // This is a static-source-shape assertion that the adapter file+ // includes the `--output` flag in its argv construction. The regression+ // codex caught was an adapter that omitted --output, so the summary+ // landed at the default cross-modal receipt path and the adapter+ // would read nothing from `summaryPath`. This assertion pins the fix+ // in the adapter source so future refactors can't silently drop it.+ const path = require('node:path').resolve('src/core/cycle/nightly-probe-adapters.ts');+ const fs = require('node:fs');+ const source = fs.readFileSync(path, 'utf-8');++ // Both adapters' argv arrays must include these markers:+ expect(source).toContain(`'--output'`); // both adapters thread an output path+ expect(source).toContain(`args.summaryPath`); // cross-modal reads from caller-controlled path+ expect(source).toContain(`'--batch'`);+ expect(source).toContain(`'--max-usd'`);+ expect(source).toContain(`'--yes'`);+ expect(source).toContain(`'--json'`); // cross-modal needs --json for the summary envelope+ });++ test('runLongMemEvalForProbe builds argv with --output for output path', () => {+ const path = require('node:path').resolve('src/core/cycle/nightly-probe-adapters.ts');+ const fs = require('node:fs');+ const source = fs.readFileSync(path, 'utf-8');+ // longmemeval adapter: first positional arg is fixturePath, then --output outputPath.+ expect(source).toMatch(/runEvalLongMemEval\(\[args\.fixturePath, '--output', args\.outputPath\]\)/);+ });+});++describe('nightly-probe-adapters: contract regression', () => {+ test('returns the documented shape: {exitCode, summary}', () => {+ // Static type-shape check via source inspection — if the return shape+ // ever drifts, this regression catches it.+ const path = require('node:path').resolve('src/core/cycle/nightly-probe-adapters.ts');+ const fs = require('node:fs');+ const source = fs.readFileSync(path, 'utf-8');+ expect(source).toMatch(/Promise<\{ exitCode: number; summary: CrossModalBatchSummary \}>/);+ });++ test('CrossModalBatchSummary shape includes the 6 expected fields', () => {+ const path = require('node:path').resolve('src/core/cycle/nightly-probe-adapters.ts');+ const fs = require('node:fs');+ const source = fs.readFileSync(path, 'utf-8');+ expect(source).toContain('pass_count');+ expect(source).toContain('fail_count');+ expect(source).toContain('inconclusive_count');+ expect(source).toContain('error_count');+ expect(source).toContain('est_cost_usd');+ expect(source).toContain('verdict');+ });+});diff --git a/test/e2e/eval-loop.test.ts b/test/e2e/eval-loop.test.tsnew file mode 100644index 0000000000..9f1055872e--- /dev/null+++ b/test/e2e/eval-loop.test.ts@@ -0,0 +1,274 @@+/**+ * e2e LOOP test (v0.41 / eng-D5).+ *+ * Exercises the full capture → export → publish → gate chain in one+ * process against PGLite in-memory. Hermetic — no LLM calls, no Docker,+ * no DATABASE_URL required.+ *+ * Strategy: use `tool_name: 'search'` (bare keyword) for the captured+ * rows so replay calls `engine.searchKeyword()` which doesn't need+ * embeddings. Basis-vector embeddings are seeded for future vector tests+ * but the LOOP-correctness assertions ride the keyword path.+ *+ * 4 cases per the plan:+ * 1. self-gate passes (regression-only path)+ * 2. perturbed-row gate fails with named breach (regression-only path)+ * 3. D3 fail-closed: malformed baseline → exit 1 with breach (not silent pass)+ * 4. Round-trip: published baseline parses back byte-identically+ */++import { describe, test, expect, beforeAll, afterAll } from 'bun:test';+import { mkdtempSync, writeFileSync, readFileSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++import { PGLiteEngine } from '../../src/core/pglite-engine.ts';+import type { ChunkInput, EvalCandidateInput } from '../../src/core/types.ts';+import { buildBaselineFromInput } from '../../src/commands/bench-publish.ts';+import {+ parseBaselineFile,+ serializeBaselineFile,+} from '../../src/core/bench/baseline-file.ts';+import { runEvalGate } from '../../src/commands/eval-gate.ts';++let engine: PGLiteEngine;++function basisEmbedding(idx: number, dim = 1536): Float32Array {+ const emb = new Float32Array(dim);+ emb[idx % dim] = 1.0;+ return emb;+}++// Capture a search via the real engine.searchKeyword + build an+// EvalCandidateInput row that mirrors what `gbrain eval export` would write.+async function captureSearch(query: string, latency_ms = 100): Promise<EvalCandidateInput> {+ const t0 = Date.now();+ const results = await engine.searchKeyword(query);+ const observed = Date.now() - t0;+ return {+ tool_name: 'search',+ query,+ retrieved_slugs: results.map(r => r.slug),+ retrieved_chunk_ids: results.map(r => r.chunk_id),+ source_ids: ['default'],+ expand_enabled: null,+ detail: null,+ detail_resolved: null,+ vector_enabled: false,+ expansion_applied: false,+ latency_ms: latency_ms || observed,+ remote: false,+ job_id: null,+ subagent_id: null,+ };+}++// process.exit hijacker for tests that exercise the CLI dispatcher.+function withExitCapture<T>(fn: () => Promise<T>): Promise<{ exitCode: number | null; result?: T }> {+ const realExit = process.exit;+ let captured: number | null = null;+ process.exit = ((code?: number) => {+ captured = code ?? 0;+ throw new Error('__test_exit__');+ }) as typeof process.exit;+ return (async () => {+ try {+ const result = await fn();+ return { exitCode: captured, result };+ } catch (e) {+ if (e instanceof Error && e.message === '__test_exit__') {+ return { exitCode: captured };+ }+ throw e;+ } finally {+ process.exit = realExit;+ }+ })();+}++beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();++ // Seed 4 placeholder pages with distinguishable content. Pages chosen+ // so each captured query hits a unique single page → top1 is stable.+ await engine.putPage('people/alice-example', {+ type: 'person',+ title: 'Alice Example',+ compiled_truth: 'Alice is a fintech founder building payments infrastructure for emerging markets.',+ timeline: '2026-01-15: Met Alice at example meetup.',+ });+ await engine.upsertChunks('people/alice-example', [+ {+ chunk_index: 0,+ chunk_text: 'Alice is a fintech founder building payments infrastructure for emerging markets.',+ chunk_source: 'compiled_truth',+ embedding: basisEmbedding(0),+ token_count: 15,+ },+ ]);++ await engine.putPage('people/bob-example', {+ type: 'person',+ title: 'Bob Example',+ compiled_truth: 'Bob is an AI safety researcher working on alignment.',+ timeline: '2026-02-10: Bob shared alignment paper.',+ });+ await engine.upsertChunks('people/bob-example', [+ {+ chunk_index: 0,+ chunk_text: 'Bob is an AI safety researcher working on alignment.',+ chunk_source: 'compiled_truth',+ embedding: basisEmbedding(1),+ token_count: 12,+ },+ ]);++ await engine.putPage('companies/widget-co-example', {+ type: 'company',+ title: 'Widget Co Example',+ compiled_truth: 'Widget Co manufactures industrial widgets for healthcare verticals.',+ timeline: '2026-03-01: Widget Co announced Series A.',+ });+ await engine.upsertChunks('companies/widget-co-example', [+ {+ chunk_index: 0,+ chunk_text: 'Widget Co manufactures industrial widgets for healthcare verticals.',+ chunk_source: 'compiled_truth',+ embedding: basisEmbedding(2),+ token_count: 12,+ },+ ]);+}, 60_000);++afterAll(async () => {+ await engine.disconnect();+});++describe('e2e LOOP: capture → publish → gate against self', () => {+ test('case 1: self-gate against just-published baseline returns PASS', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-loop-'));+ try {+ // Step 1: capture three search queries against the live brain.+ const captured = await Promise.all([+ captureSearch('fintech'),+ captureSearch('alignment'),+ captureSearch('widgets'),+ ]);+ // Sanity: keyword search returned at least one row for each (else+ // the test corpus isn't seeded properly and the LOOP can't loop).+ for (const c of captured) {+ expect(c.retrieved_slugs.length).toBeGreaterThan(0);+ }++ // Step 2: publish a baseline from the captured rows.+ const baselineFile = buildBaselineFromInput(captured, {+ label: 'e2e-loop-self-test',+ });+ const baselinePath = join(dir, 'self.baseline.ndjson');+ writeFileSync(baselinePath, serializeBaselineFile(baselineFile));++ // Step 3: gate against the baseline. Since the brain hasn't changed,+ // self-replay should produce identical retrieval → jaccard=1.0,+ // top1=1.0 → verdict PASS → exit 0.+ const out = await withExitCapture(() =>+ runEvalGate(engine, ['--baseline', baselinePath, '--json']),+ );+ // Exit 0 is the unwrapped happy path (no process.exit call).+ // Some assertion environments will have null exitCode meaning "did not+ // call process.exit", which is the success path.+ expect(out.exitCode).toBe(null);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ }, 30_000);+});++describe('e2e LOOP: perturbed baseline → gate FAILS with named breach', () => {+ test('case 2: perturbed retrieved_slugs → jaccard drops → exit 1', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-loop-'));+ try {+ const captured = await Promise.all([+ captureSearch('fintech'),+ captureSearch('alignment'),+ captureSearch('widgets'),+ ]);++ // Perturb: invent slugs that the brain WILL NOT return. Replay will+ // see zero overlap → low jaccard → breach.+ const perturbed = captured.map(c => ({+ ...c,+ retrieved_slugs: ['fake/slug-not-in-brain-1', 'fake/slug-not-in-brain-2'],+ }));++ const baselineFile = buildBaselineFromInput(perturbed, {+ label: 'e2e-loop-perturbed',+ });+ const baselinePath = join(dir, 'perturbed.baseline.ndjson');+ writeFileSync(baselinePath, serializeBaselineFile(baselineFile));++ const out = await withExitCapture(() =>+ runEvalGate(engine, ['--baseline', baselinePath]),+ );+ // Perturbed baseline → current retrieval doesn't match → jaccard well+ // below 0.85 → exit 1 with breach.+ expect(out.exitCode).toBe(1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ }, 30_000);+});++describe('e2e LOOP: D3 fail-closed on malformed baseline (NOT silent pass)', () => {+ test('case 3: malformed baseline → exit 1 with parse breach (D3 IRON-RULE)', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-loop-'));+ try {+ const baselinePath = join(dir, 'bad.baseline.ndjson');+ writeFileSync(baselinePath, '{this is not valid JSON\n');++ const out = await withExitCapture(() =>+ runEvalGate(engine, ['--baseline', baselinePath]),+ );+ // The IRON-RULE: malformed input MUST exit 1 (treat as gate fail).+ // The pre-D3 bug would have silently exited 0.+ expect(out.exitCode).toBe(1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });+});++describe('e2e LOOP: round-trip byte stability', () => {+ test('case 4: published baseline parses back byte-identically', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-loop-'));+ try {+ const captured = await Promise.all([+ captureSearch('fintech'),+ captureSearch('alignment'),+ ]);++ const baselineFile = buildBaselineFromInput(captured, {+ label: 'roundtrip',+ publishedAt: new Date('2026-05-24T00:00:00Z'),+ });+ const baselinePath = join(dir, 'roundtrip.baseline.ndjson');+ const serialized = serializeBaselineFile(baselineFile);+ writeFileSync(baselinePath, serialized);++ // Read back from disk, parse, re-serialize: must be byte-identical.+ const onDisk = readFileSync(baselinePath, 'utf-8');+ const parsed = parseBaselineFile(onDisk);+ const reserialized = serializeBaselineFile(parsed);+ expect(reserialized).toBe(onDisk);++ // Also: metadata fields preserved exactly.+ expect(parsed.metadata.label).toBe('roundtrip');+ expect(parsed.metadata.published_at).toBe('2026-05-24T00:00:00.000Z');+ expect(parsed.metadata._kind).toBe('baseline_metadata');+ expect(parsed.rows.length).toBe(captured.length);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ }, 30_000);+});diff --git a/test/eval-gate.test.ts b/test/eval-gate.test.tsnew file mode 100644index 0000000000..bbf05ea9d9--- /dev/null+++ b/test/eval-gate.test.ts@@ -0,0 +1,259 @@+/**+ * Unit tests for `gbrain eval gate` (v0.41).+ *+ * Pure-logic tests against `runEvalGate` driven through a PGLite engine.+ * The dispatcher's full integration path (capture → publish → gate) is+ * covered by `test/e2e/eval-loop.test.ts`. This file pins:+ * - usage errors (no flags, files missing, malformed inputs)+ * - exit-code matrix+ * - threshold precedence (CLI > embedded > defaults)+ * - regression-only / correctness-only / both-required paths+ * - JSON envelope shape+ * - latency math (corrected per codex round-2 #2)+ * - D3 fail-closed on subprocess (in-process throw, in our case)+ */++import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';+import { writeFileSync, mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { resetPgliteState } from './helpers/reset-pglite.ts';+import { runEvalGate } from '../src/commands/eval-gate.ts';+import {+ BASELINE_FILE_SCHEMA_VERSION,+ DEFAULT_THRESHOLDS,+ computeQueryHash,+ computeSourceHash,+ serializeBaselineFile,+ type BaselineFile,+ type BaselineRow,+} from '../src/core/bench/baseline-file.ts';++let engine: PGLiteEngine;++beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+});++afterAll(async () => {+ await engine.disconnect();+});++beforeEach(async () => {+ await resetPgliteState(engine);+});++function makeRow(query: string, slugs: string[], latency_ms = 100): BaselineRow {+ return {+ tool_name: 'query',+ query,+ query_hash: computeQueryHash(query),+ retrieved_slugs: slugs,+ retrieved_chunk_ids: slugs.map((_, i) => i),+ source_ids: ['default'],+ expand_enabled: false,+ detail: 'medium',+ detail_resolved: 'medium',+ vector_enabled: true,+ expansion_applied: false,+ latency_ms,+ remote: false,+ job_id: null,+ subagent_id: null,+ };+}++function writeBaselineFile(+ dir: string,+ rows: BaselineRow[],+ opts: { label?: string; thresholds?: BaselineFile['metadata']['thresholds'] } = {},+): string {+ const path = join(dir, 'test.baseline.ndjson');+ const file: BaselineFile = {+ metadata: {+ schema_version: BASELINE_FILE_SCHEMA_VERSION,+ _kind: 'baseline_metadata',+ label: opts.label ?? 'unit-test',+ published_at: '2026-05-24T00:00:00Z',+ source_hash: computeSourceHash(rows),+ thresholds: opts.thresholds ?? { ...DEFAULT_THRESHOLDS },+ row_count: rows.length,+ baseline_mean_latency_ms:+ rows.length === 0 ? 0 : rows.reduce((s, r) => s + r.latency_ms, 0) / rows.length,+ },+ rows,+ };+ writeFileSync(path, serializeBaselineFile(file));+ return path;+}++function writeQrelsFile(dir: string, queries: unknown[]): string {+ const path = join(dir, 'test.qrels.json');+ writeFileSync(path, JSON.stringify({ schema_version: 1, queries }, null, 2));+ return path;+}++// process.exit hijacker — capture exit code without actually exiting.+function withExitCapture<T>(fn: () => Promise<T>): Promise<{ exitCode: number | null; result?: T; threw?: unknown }> {+ const realExit = process.exit;+ let captured: number | null = null;+ process.exit = ((code?: number) => {+ captured = code ?? 0;+ throw new Error('__test_exit__');+ }) as typeof process.exit;+ return (async () => {+ try {+ const result = await fn();+ return { exitCode: captured, result };+ } catch (e) {+ if (e instanceof Error && e.message === '__test_exit__') {+ return { exitCode: captured };+ }+ return { exitCode: captured, threw: e };+ } finally {+ process.exit = realExit;+ }+ })();+}++describe('eval gate: usage errors', () => {+ test('no flags → exit 2 with usage error', async () => {+ const out = await withExitCapture(() => runEvalGate(engine, []));+ expect(out.exitCode).toBe(2);+ });++ test('--baseline file missing → exit 2', async () => {+ const out = await withExitCapture(() =>+ runEvalGate(engine, ['--baseline', '/tmp/does-not-exist-12345.ndjson']),+ );+ expect(out.exitCode).toBe(2);+ });++ test('--qrels file missing → exit 2', async () => {+ const out = await withExitCapture(() =>+ runEvalGate(engine, ['--qrels', '/tmp/does-not-exist-12345.json']),+ );+ expect(out.exitCode).toBe(2);+ });+});++describe('eval gate: regression-only path', () => {+ test('malformed baseline → surfaces as breach (verdict fail, exit 1)', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-gate-test-'));+ const baseline = join(dir, 'bad.baseline.ndjson');+ writeFileSync(baseline, 'not json\n');+ try {+ const out = await withExitCapture(() => runEvalGate(engine, ['--baseline', baseline]));+ expect(out.exitCode).toBe(1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });++ test('empty-brain replay against baseline → throws (replay rejects empty) → fail-closed breach', async () => {+ // Synthetic baseline with 1 row; the brain is empty, so replay throws+ // when it tries to hybridSearch (no embedding key). D3 fail-closed says+ // any throw becomes a breach.+ const dir = mkdtempSync(join(tmpdir(), 'eval-gate-test-'));+ const baseline = writeBaselineFile(dir, [makeRow('foo', ['a', 'b'])]);+ try {+ const out = await withExitCapture(() =>+ runEvalGate(engine, ['--baseline', baseline, '--json']),+ );+ // Empty-brain replay yields 0 metrics → 0.0 jaccard, 0.0 top1 →+ // both below the 0.85 / 0.80 thresholds → fail → exit 1.+ expect(out.exitCode).toBe(1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });+});++describe('eval gate: correctness-only path', () => {+ test('empty-brain qrels gate → 0 recall → exit 1 with breach', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-gate-test-'));+ const qrels = writeQrelsFile(dir, [+ { query_id: 'q1', query: 'nonexistent', relevant_slugs: ['nonexistent/page'] },+ ]);+ try {+ const out = await withExitCapture(() => runEvalGate(engine, ['--qrels', qrels]));+ expect(out.exitCode).toBe(1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });++ test('malformed qrels → surfaces as breach (exit 1)', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-gate-test-'));+ const qrels = join(dir, 'bad.qrels.json');+ writeFileSync(qrels, '{not valid');+ try {+ const out = await withExitCapture(() => runEvalGate(engine, ['--qrels', qrels]));+ expect(out.exitCode).toBe(1);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });+});++describe('eval gate: JSON envelope shape', () => {+ test('--json prints stable schema_version 1 envelope with both sections', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-gate-test-'));+ const qrels = writeQrelsFile(dir, [+ { query_id: 'q1', query: 'x', relevant_slugs: ['nonexistent'] },+ ]);+ try {+ // Capture stdout+ const realLog = console.log;+ let captured = '';+ console.log = (msg: string) => { captured += msg + '\n'; };+ try {+ await withExitCapture(() => runEvalGate(engine, ['--qrels', qrels, '--json']));+ } finally {+ console.log = realLog;+ }+ const envelope = JSON.parse(captured.trim());+ expect(envelope.schema_version).toBe(1);+ expect(envelope.verdict).toMatch(/pass|fail/);+ expect(envelope.regression_gate).toBeDefined();+ expect(envelope.correctness_gate).toBeDefined();+ expect(envelope.regression_gate.ran).toBe(false);+ expect(envelope.correctness_gate.ran).toBe(true);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });+});++describe('eval gate: latency math (corrected per codex round-2 #2)', () => {+ test('formula: (baseline + delta) / baseline <= multiplier', () => {+ // Direct math check (not via runEvalGate to avoid the brain round-trip).+ // baseline=100ms, current=250ms → delta=+150ms → ratio = 250/100 = 2.5x+ // multiplier=2.0 → 2.5 > 2.0 → BREACH.+ const baseline = 100;+ const delta = 150;+ const multiplier = 2.0;+ const ratio = (baseline + delta) / baseline;+ expect(ratio).toBe(2.5);+ expect(ratio > multiplier).toBe(true); // SHOULD breach++ // The OLD (wrong) formula would have been delta / baseline = 1.5,+ // which is < 2.0 → would have PASSED a 2.5x slowdown. Regression test.+ const oldFormula = delta / baseline;+ expect(oldFormula).toBe(1.5);+ expect(oldFormula > multiplier).toBe(false); // OLD formula's bug — pinned for documentation.+ });++ test('baseline_mean_latency_ms = 0 → latency check skipped (not crash)', async () => {+ // This is hard to test through runEvalGate without a real brain that+ // returns matching slugs. The integration is covered by e2e/eval-loop.+ // Here we pin that the conditional is correct.+ const baselineMean = 0;+ const isFinitePos = Number.isFinite(baselineMean) && baselineMean > 0;+ expect(isFinitePos).toBe(false);+ });+});diff --git a/test/eval-replay-metadata-skip.test.ts b/test/eval-replay-metadata-skip.test.tsnew file mode 100644index 0000000000..7151159582--- /dev/null+++ b/test/eval-replay-metadata-skip.test.ts@@ -0,0 +1,106 @@+/**+ * v0.41 IRON-RULE regression: `eval replay` MUST skip the+ * `_kind: 'baseline_metadata'` header line that `gbrain bench publish`+ * writes. Codex round-1 #3 caught that without the skip, the header+ * would be parsed as a fake captured row and pollute counts.+ *+ * This test verifies the skip works by feeding `parseNdjson` (via the+ * replayCore entrypoint's file-read path) a synthetic baseline file and+ * asserting:+ * 1. The metadata line is dropped (rows_total reflects ONLY captured rows)+ * 2. The body rows parse correctly+ * 3. parseNdjson rejects a bare captured row WITHOUT schema_version+ * (i.e. the discriminator + schema_version validators are both live)+ */++import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';+import { writeFileSync, mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';++import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { resetPgliteState } from './helpers/reset-pglite.ts';+import { replayCore } from '../src/commands/eval-replay.ts';++let engine: PGLiteEngine;++beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+});++afterAll(async () => {+ await engine.disconnect();+});++beforeEach(async () => {+ await resetPgliteState(engine);+});++describe('eval-replay metadata-skip regression (v0.41)', () => {+ test('parseNdjson skips _kind:baseline_metadata header line', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-replay-meta-skip-'));+ const baselinePath = join(dir, 'baseline.ndjson');++ // Synthetic baseline file with metadata header + 2 captured rows.+ const metadataLine = JSON.stringify({+ schema_version: 1,+ _kind: 'baseline_metadata',+ label: 'test',+ published_at: '2026-05-24T00:00:00Z',+ source_hash: 'fake-hash',+ thresholds: { jaccard: 0.85, top1: 0.8, latency_multiplier: 2.0 },+ row_count: 2,+ baseline_mean_latency_ms: 100,+ });+ const row1 = JSON.stringify({+ id: 1,+ schema_version: 1,+ tool_name: 'query',+ query: '', // empty query → skipped, doesn't touch the engine+ retrieved_slugs: ['a'],+ latency_ms: 100,+ });+ const row2 = JSON.stringify({+ id: 2,+ schema_version: 1,+ tool_name: 'query',+ query: '',+ retrieved_slugs: ['b'],+ latency_ms: 100,+ });+ writeFileSync(baselinePath, `${metadataLine}\n${row1}\n${row2}\n`);++ try {+ const { summary } = await replayCore(engine, { against: baselinePath });+ // KEY ASSERTION: rows_total = 2 (metadata header NOT counted).+ // Without the skip, this would be 3 and parser would throw on the+ // metadata line because it has no schema_version of the type expected.+ expect(summary.rows_total).toBe(2);+ expect(summary.rows_skipped).toBe(2); // both empty-query rows skipped+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });++ test('parseNdjson still rejects malformed rows (validator live, not silently dropping everything)', async () => {+ const dir = mkdtempSync(join(tmpdir(), 'eval-replay-meta-skip-'));+ const baselinePath = join(dir, 'bad.ndjson');++ const metadataLine = JSON.stringify({+ schema_version: 1,+ _kind: 'baseline_metadata',+ label: 'test',+ });+ // Row missing schema_version entirely → should still throw.+ const badRow = JSON.stringify({ id: 1, tool_name: 'query', query: 'x' });+ writeFileSync(baselinePath, `${metadataLine}\n${badRow}\n`);++ try {+ await expect(replayCore(engine, { against: baselinePath })).rejects.toThrow(/schema_version/);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });+});