* fix: 8 root-cause fixes from /investigate wave
Consolidated bundle of bug fixes from /investigate on the 8 deferred bugs.
Each fix was designed to go at the structural gap, not the symptom. Codex
verified 20 load-bearing claims on the plan; 12 triggered plan revisions.
Bug 2 — GBRAIN_POOL_SIZE env knob + init finally blocks (no auto-detect).
Covers both the singleton pool (db.ts) and instance pool (import.ts:140).
Bug 3 — Centralize migration ledger writes in apply-migrations runner.
Removed appendCompletedMigration from v0_11_0, v0_12_0, v0_12_2,
v0_13_0, v0_13_1. Added 3-partial wedge cap + --force-retry reset.
'complete wins' preserved; no partial can regress a completed migration.
Bug 5 — v0.14.0 migration registered. src/commands/migrations/v0_14_0.ts
ships Phase A (ALTER minion_jobs.max_stalled SET DEFAULT 3) + Phase B
(pending-host-work ping for shell-jobs adoption).
Bug 6/10 — jsonb_agg(DISTINCT ...) in legacy traverseGraph (both engines).
Presentation-level dedup; schema still preserves provenance rows.
Bug 7 — doctor --fast reads DB URL source via getDbUrlSource() in config.ts.
Precise message: 'Skipping DB checks (--fast mode, URL present from env)'
replaces the misleading 'No database configured'.
Bug 8 — max_stalled default bumped 1→3 in schema-embedded.ts, pglite-schema.ts,
schema.sql (new installs). v0_14_0 Phase A ALTER for existing installs.
autopilot-cycle handler yields to event loop between phases so the
worker's lock-renewal timer fires on huge brains. (Deep AbortSignal
threading through runEmbedCore/runExtractCore/runBacklinksCore/performSync
deferred to v0.15 queue polish.)
Bug 9 — Gate sync.last_commit on no-failures across all three sync paths
(incremental, full via runImport, gbrain import git continuity).
recordSyncFailures() helper + ~/.gbrain/sync-failures.jsonl with
dedup key path+commit+error-hash. New flags: --skip-failed (ack) +
--retry-failed (re-attempt). Doctor surfaces unacknowledged failures.
Bug 11 — brain_score breakdown fields on BrainHealth (embed_coverage_score,
link_density_score, timeline_coverage_score, no_orphans_score,
no_dead_links_score); sum equals brain_score by construction.
dead_links now on the type (resolves featuresTeaserForDoctor drift).
orphan_pages kept as 'islanded' (no inbound AND no outbound) and
docs updated to match — explicit semantic instead of doc drift.
New tests: test/traverse-graph-dedup.test.ts, test/sync-failures.test.ts,
test/brain-score-breakdown.test.ts, test/migration-resume.test.ts,
test/migrations-v0_14_0.test.ts. Extended: migrate, doctor, apply-migrations.
All 1696 unit tests pass locally. postgres-jsonb E2E regression unchanged
(none of these touch the JSONB write surface).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* docs: v0.14.2 CHANGELOG + CLAUDE.md; align migration-flow E2E with runner-owned ledger
CHANGELOG: v0.14.2 entry in the standard release-summary format
(two-line headline + lead + numbers table + "what this means" +
"To take advantage of v0.14.2" self-repair block + itemized
changes grouped by reliability / observability / graph correctness /
new migration / tests / deferred-to-v0.15).
CLAUDE.md: new "Key commands added in v0.14.2" section covers
--skip-failed, --retry-failed, --force-retry, GBRAIN_POOL_SIZE env,
and the new doctor checks (sync_failures, brain_score breakdown).
Migration orchestrator docs updated to describe v0_14_0.ts + the
runner-owned ledger contract from Bug 3.
test/e2e/migration-flow.test.ts: three assertions updated to match
the Bug 3 contract — orchestrators no longer append to completed.jsonl
directly, so direct-orchestrator E2E calls leave the ledger empty.
Preferences assertions remain (that's still the orchestrator's side
of the contract). Runner's ledger write is covered by the unit suite
(test/apply-migrations.test.ts + test/migration-resume.test.ts).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
141 lines
6.0 KiB
TypeScript
141 lines
6.0 KiB
TypeScript
/**
|
|
* Bug 11 — brain_score needs a breakdown + orphan_pages metric is wrong.
|
|
*
|
|
* Assertions:
|
|
* 1. getHealth() returns the new *_score breakdown fields.
|
|
* 2. Breakdown fields sum to brain_score by construction.
|
|
* 3. orphan_pages counts pages with zero INBOUND links, regardless of
|
|
* whether they have outbound links (was: required both).
|
|
* 4. BrainHealth type now carries dead_links.
|
|
*/
|
|
|
|
import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
|
|
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
|
|
|
|
let engine: PGLiteEngine;
|
|
|
|
beforeAll(async () => {
|
|
engine = new PGLiteEngine();
|
|
await engine.connect({});
|
|
await engine.initSchema();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await engine.disconnect();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
for (const t of ['links', 'content_chunks', 'timeline_entries', 'raw_data', 'tags', 'page_versions', 'ingest_log', 'pages']) {
|
|
await (engine as any).db.exec(`DELETE FROM ${t}`);
|
|
}
|
|
});
|
|
|
|
describe('Bug 11 — brain_score breakdown sums to total', () => {
|
|
test('empty brain returns zero score with all breakdown fields present', async () => {
|
|
const h = await engine.getHealth();
|
|
expect(h.brain_score).toBe(0);
|
|
expect(h.embed_coverage_score).toBe(0);
|
|
expect(h.link_density_score).toBe(0);
|
|
expect(h.timeline_coverage_score).toBe(0);
|
|
expect(h.no_orphans_score).toBe(0);
|
|
expect(h.no_dead_links_score).toBe(0);
|
|
// dead_links is now on the type.
|
|
expect(h.dead_links).toBe(0);
|
|
});
|
|
|
|
test('breakdown fields always sum to brain_score', async () => {
|
|
// Seed a small graph — some pages, some links, some embeds.
|
|
for (const slug of ['a', 'b', 'c']) {
|
|
await engine.putPage(slug, { type: 'note', title: slug, compiled_truth: `content of ${slug}`, frontmatter: {} });
|
|
}
|
|
const h = await engine.getHealth();
|
|
const sum =
|
|
h.embed_coverage_score +
|
|
h.link_density_score +
|
|
h.timeline_coverage_score +
|
|
h.no_orphans_score +
|
|
h.no_dead_links_score;
|
|
expect(sum).toBe(h.brain_score);
|
|
});
|
|
|
|
test('brain_score caps at 100', async () => {
|
|
const h = await engine.getHealth();
|
|
expect(h.brain_score).toBeGreaterThanOrEqual(0);
|
|
expect(h.brain_score).toBeLessThanOrEqual(100);
|
|
});
|
|
});
|
|
|
|
describe('Bug 11 — orphan_pages is "no inbound links"', () => {
|
|
test('a page with outbound-only links is NOT an orphan', async () => {
|
|
// Hub page: links out to three others, but nothing links back to it.
|
|
// Previous (buggy) behavior: hub counted as orphan because it had no
|
|
// inbound links (correct) AND the old query also required no outbound.
|
|
await engine.putPage('hub', { type: 'note', title: 'Hub', compiled_truth: 'index', frontmatter: {} });
|
|
await engine.putPage('leaf1', { type: 'note', title: 'L1', compiled_truth: 'x', frontmatter: {} });
|
|
await engine.putPage('leaf2', { type: 'note', title: 'L2', compiled_truth: 'y', frontmatter: {} });
|
|
await engine.putPage('leaf3', { type: 'note', title: 'L3', compiled_truth: 'z', frontmatter: {} });
|
|
|
|
const hubId = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug='hub'`)).rows[0].id;
|
|
for (const target of ['leaf1', 'leaf2', 'leaf3']) {
|
|
const tid = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug=$1`, [target])).rows[0].id;
|
|
await (engine as any).db.query(
|
|
`INSERT INTO links (from_page_id, to_page_id, link_type) VALUES ($1, $2, 'mentions')`,
|
|
[hubId, tid],
|
|
);
|
|
}
|
|
|
|
const h = await engine.getHealth();
|
|
// hub has outbound, no inbound → NOT orphan (under the fixed definition).
|
|
// leaf1/2/3 have inbound from hub → NOT orphan.
|
|
// So orphan_pages should be 0.
|
|
expect(h.orphan_pages).toBe(0);
|
|
});
|
|
|
|
test('a page with no links at all IS an orphan', async () => {
|
|
await engine.putPage('loner', { type: 'note', title: 'Loner', compiled_truth: 'alone', frontmatter: {} });
|
|
const h = await engine.getHealth();
|
|
expect(h.orphan_pages).toBe(1);
|
|
});
|
|
|
|
test('a page with inbound links only is NOT an orphan', async () => {
|
|
await engine.putPage('sink', { type: 'note', title: 'Sink', compiled_truth: 'target', frontmatter: {} });
|
|
await engine.putPage('source', { type: 'note', title: 'Source', compiled_truth: 'origin', frontmatter: {} });
|
|
const sinkId = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug='sink'`)).rows[0].id;
|
|
const srcId = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug='source'`)).rows[0].id;
|
|
await (engine as any).db.query(
|
|
`INSERT INTO links (from_page_id, to_page_id, link_type) VALUES ($1, $2, 'mentions')`,
|
|
[srcId, sinkId],
|
|
);
|
|
|
|
const h = await engine.getHealth();
|
|
// sink has 1 inbound (from source) → not orphan.
|
|
// source has no inbound (but has outbound) → not orphan under new definition.
|
|
expect(h.orphan_pages).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('Bug 11 — doctor renders brain_score breakdown', () => {
|
|
test('doctor source contains brain_score breakdown rendering', async () => {
|
|
const source = await Bun.file(new URL('../src/commands/doctor.ts', import.meta.url)).text();
|
|
expect(source).toContain('brain_score');
|
|
expect(source).toContain('embed_coverage_score');
|
|
expect(source).toContain('link_density_score');
|
|
expect(source).toContain('no_orphans_score');
|
|
expect(source).toContain('no_dead_links_score');
|
|
});
|
|
});
|
|
|
|
describe('Bug 11 — BrainHealth type shape', () => {
|
|
test('type includes dead_links + breakdown scores', async () => {
|
|
const typesSource = await Bun.file(new URL('../src/core/types.ts', import.meta.url)).text();
|
|
expect(typesSource).toContain('dead_links: number');
|
|
expect(typesSource).toContain('embed_coverage_score: number');
|
|
expect(typesSource).toContain('link_density_score: number');
|
|
expect(typesSource).toContain('timeline_coverage_score: number');
|
|
expect(typesSource).toContain('no_orphans_score: number');
|
|
expect(typesSource).toContain('no_dead_links_score: number');
|
|
// The stale "(0-10)" comment must be corrected to 0-100.
|
|
expect(typesSource).toContain('0-100');
|
|
});
|
|
});
|