mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-28 14:59:47 +00:00
* fix(initSchema): narrow pre-schema bootstrap + v24 PGLite no-op Closes a 2-year-old wedge cycle that hit users 10+ times across 6 schema versions (#239, #243, #266, #357, #366, #374, #375, #378, #395, #396). Bug class: gbrain ships an embedded schema blob (PGLITE_SCHEMA_SQL + SCHEMA_SQL) that runs before numbered migrations on every initSchema(). The blob references columns that newer migrations introduce. On any brain older than the migration that adds those columns, the blob crashes before the migration can run. Fix: PGLiteEngine.initSchema() and PostgresEngine.initSchema() now call a new private applyForwardReferenceBootstrap() before the schema blob. The bootstrap probes for missing forward-referenced state and adds only what's needed (sources table + pages.source_id, links.link_source + links.origin_page_id, content_chunks.symbol_name + content_chunks.language). Fresh installs and modern brains both no-op. A CI guard test/schema-bootstrap-coverage.test.ts enforces that the bootstrap covers every forward reference in PGLITE_SCHEMA_SQL. Future migrations that add column-with-index in the schema blob must extend the bootstrap; the test fails loudly otherwise. Migration v24 (rls_backfill_missing_tables) now no-ops on PGLite via sqlFor.pglite: '' since PGLite has no RLS engine and is single-tenant. Closes #395. The plan went through CEO + Eng + Codex review. Codex caught a critical bug in the original "run all migrations early" approach: it would crash on v24 trying to ALTER subagent tables that the schema blob hadn't created yet. The narrow bootstrap shape resolves that. Wave incorporates community PRs #398 (@vinsew), #399 (@jdcastro2), #402 (@schnubb-web). Co-Authored-By: vinsew <yiyangchaishu@gmail.com> Co-Authored-By: Julián David Castro <juliancastro@Mac-mini-de-Julian.local> Co-Authored-By: schnubb-web <info@mia-mai.de> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(test): bump beforeAll timeout on minions-shell-pglite for parallel-load flake Default 5s beforeAll timeout occasionally trips under the parallel test runner when many test files initialize PGLite concurrently. The same pattern is documented as a P0 TODO for v0.21 Code Cathedral tests; this is the one instance the upgrade-hardening wave directly exposed (CPU pressure from new bootstrap test files). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: bump version and changelog (v0.21.1) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * docs: update project documentation for v0.21.1 - CLAUDE.md: PGLite + Postgres engine entries note new applyForwardReferenceBootstrap() in initSchema(), v24 sqlFor.pglite no-op, and the new bootstrap test files (test/bootstrap.test.ts, test/schema-bootstrap-coverage.test.ts, test/e2e/postgres-bootstrap.test.ts). - CHANGELOG.md: voice polish on the v0.21.1 headline (drop stray ## prefixes so the bold two-line headline renders as bold prose, not h2 sub-headers that break the version-entry hierarchy). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * chore: correct version slot from v0.22.5 to v0.21.6 Slot allocation correction. v0.21.6 is the actual landing slot for this wave on the v0.21.x patch line. VERSION, package.json, CHANGELOG.md (header + table + take-advantage section), CLAUDE.md (engine entries, migrate.ts entry, test descriptions) all updated together. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: correct version slot to v0.22.7 VERSION, package.json, CHANGELOG.md (header + table + take-advantage section), CLAUDE.md (engine entries, migrate.ts entry, test descriptions) all updated together. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: regenerate llms.txt + llms-full.txt for v0.22.7 CLAUDE.md changed (engine entries describe the bootstrap, migrate.ts entry describes the v24 PGLite no-op). The build:llms regen-drift guard caught the staleness in CI. Running `bun run build:llms` propagates the same content into the AI-consumable bundles. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: change version slot from v0.22.7 to v0.22.6-hotfix.1 PR #483 (fix/mcp-registration-auth) claimed v0.22.7. Moved this wave to v0.22.6-hotfix.1 to avoid the collision. Note: semver-orders BEFORE 0.22.6 (pre-release suffix), so the hotfix tag is informational, not ordering-correct. Acceptable here because the wave's content predates master's 0.22.6 and is being landed as a parallel hotfix slot. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: change version slot to v0.22.6.1 4-digit hotfix slot under master's v0.22.6. bun + bun:test accept the format; the build-llms regen-drift guard and bootstrap tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: vinsew <yiyangchaishu@gmail.com> Co-authored-by: Julián David Castro <juliancastro@Mac-mini-de-Julian.local> Co-authored-by: schnubb-web <info@mia-mai.de> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
150 lines
6.8 KiB
TypeScript
150 lines
6.8 KiB
TypeScript
/**
|
|
* CI guard: PGLITE_SCHEMA_SQL must not forward-reference state that
|
|
* `applyForwardReferenceBootstrap` doesn't know how to create.
|
|
*
|
|
* Background: gbrain ships an "embedded latest schema" blob
|
|
* (`pglite-schema.ts`) for fast bootstraps, alongside a numbered migration
|
|
* chain (`migrate.ts`) for incremental upgrades. Across 2 years and 6 schema
|
|
* versions, every release that added a column-with-index in the schema blob
|
|
* without a corresponding bootstrap addition has triggered the same wedge
|
|
* incident class (#239, #243, #266, #266, #357, #366, #374, #375, #378,
|
|
* #395, #396).
|
|
*
|
|
* The bootstrap is the structural fix. This test enforces the contract:
|
|
* for every "forward reference" the schema blob makes (FK or indexed column
|
|
* defined later than its reference site, or any column that older brains
|
|
* lack), the bootstrap MUST add enough state so that running the schema
|
|
* blob is replay-safe on a brain that lacks every member of
|
|
* `REQUIRED_BOOTSTRAP_COVERAGE`.
|
|
*
|
|
* **When you add a new schema-blob forward reference:**
|
|
* 1. Extend `applyForwardReferenceBootstrap` in pglite-engine.ts +
|
|
* postgres-engine.ts to add the new state.
|
|
* 2. Add an entry to `REQUIRED_BOOTSTRAP_COVERAGE` below.
|
|
* 3. This test will pass.
|
|
*
|
|
* If you add a forward reference but skip step 1, this test fails. If you
|
|
* skip step 2, this test passes but the bootstrap silently drifts behind
|
|
* the schema. The eng-review polish notes recommended layered coverage
|
|
* (per-engine integration tests in `test/bootstrap.test.ts` +
|
|
* `test/e2e/postgres-bootstrap.test.ts`) to catch step 2 oversights.
|
|
*/
|
|
|
|
import { test, expect } from 'bun:test';
|
|
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
|
|
|
|
// Forward-reference targets that PGLITE_SCHEMA_SQL requires.
|
|
// When you add a new one, extend this list AND the bootstrap.
|
|
type ForwardReference =
|
|
| { kind: 'table'; name: string }
|
|
| { kind: 'column'; table: string; column: string };
|
|
|
|
const REQUIRED_BOOTSTRAP_COVERAGE: ForwardReference[] = [
|
|
// Forward-referenced by `pages.source_id REFERENCES sources(id)` and the
|
|
// `INSERT INTO sources (id, name, config) VALUES ('default', ...)` seed.
|
|
{ kind: 'table', name: 'sources' },
|
|
// Forward-referenced by `CREATE INDEX idx_pages_source_id ON pages(source_id)`.
|
|
{ kind: 'column', table: 'pages', column: 'source_id' },
|
|
// Forward-referenced by `CREATE INDEX idx_links_source ON links(link_source)`.
|
|
{ kind: 'column', table: 'links', column: 'link_source' },
|
|
// Forward-referenced by `CREATE INDEX idx_links_origin ON links(origin_page_id)`.
|
|
{ kind: 'column', table: 'links', column: 'origin_page_id' },
|
|
// v0.19+ — forward-referenced by `CREATE INDEX idx_chunks_symbol_name
|
|
// ON content_chunks(symbol_name) WHERE symbol_name IS NOT NULL`.
|
|
{ kind: 'column', table: 'content_chunks', column: 'symbol_name' },
|
|
// v0.19+ — forward-referenced by `CREATE INDEX idx_chunks_language
|
|
// ON content_chunks(language) WHERE language IS NOT NULL`.
|
|
{ kind: 'column', table: 'content_chunks', column: 'language' },
|
|
];
|
|
|
|
test('applyForwardReferenceBootstrap covers every forward reference declared in REQUIRED_BOOTSTRAP_COVERAGE', async () => {
|
|
const engine = new PGLiteEngine();
|
|
await engine.connect({});
|
|
try {
|
|
await engine.initSchema();
|
|
const db = (engine as any).db;
|
|
|
|
// Strip every required forward-reference target so the brain looks like
|
|
// it pre-dates the migrations that introduced these objects. Drop columns
|
|
// before the table-level constraints that depend on them.
|
|
await db.exec(`
|
|
ALTER TABLE pages DROP CONSTRAINT IF EXISTS pages_source_slug_key;
|
|
ALTER TABLE pages ADD CONSTRAINT pages_slug_key UNIQUE (slug);
|
|
DROP INDEX IF EXISTS idx_pages_source_id;
|
|
ALTER TABLE pages DROP COLUMN IF EXISTS source_id;
|
|
DROP TABLE IF EXISTS sources CASCADE;
|
|
|
|
DROP INDEX IF EXISTS idx_links_source;
|
|
DROP INDEX IF EXISTS idx_links_origin;
|
|
ALTER TABLE links DROP CONSTRAINT IF EXISTS links_from_to_type_source_origin_unique;
|
|
ALTER TABLE links DROP COLUMN IF EXISTS link_source;
|
|
ALTER TABLE links DROP COLUMN IF EXISTS origin_page_id;
|
|
|
|
DROP INDEX IF EXISTS idx_chunks_symbol_name;
|
|
DROP INDEX IF EXISTS idx_chunks_language;
|
|
ALTER TABLE content_chunks DROP COLUMN IF EXISTS symbol_name;
|
|
ALTER TABLE content_chunks DROP COLUMN IF EXISTS language;
|
|
`);
|
|
|
|
// Run bootstrap in isolation (NOT initSchema). This is what we're testing.
|
|
await (engine as any).applyForwardReferenceBootstrap();
|
|
|
|
// Assert every required forward-reference target now satisfies the
|
|
// schema-blob's expectations.
|
|
for (const ref of REQUIRED_BOOTSTRAP_COVERAGE) {
|
|
if (ref.kind === 'table') {
|
|
const { rows } = await db.query(
|
|
`SELECT 1 FROM information_schema.tables
|
|
WHERE table_schema = 'public' AND table_name = $1`,
|
|
[ref.name],
|
|
);
|
|
expect(rows.length).toBeGreaterThan(0);
|
|
} else {
|
|
const { rows } = await db.query(
|
|
`SELECT 1 FROM information_schema.columns
|
|
WHERE table_schema = 'public' AND table_name = $1 AND column_name = $2`,
|
|
[ref.table, ref.column],
|
|
);
|
|
expect(rows.length).toBeGreaterThan(0);
|
|
}
|
|
}
|
|
} finally {
|
|
await engine.disconnect();
|
|
}
|
|
}, 30000);
|
|
|
|
test('after bootstrap, PGLITE_SCHEMA_SQL replays without crashing on missing forward references', async () => {
|
|
// End-to-end contract: bootstrap → SCHEMA_SQL must succeed even on a brain
|
|
// that lacks every forward-referenced target. This catches the case where
|
|
// REQUIRED_BOOTSTRAP_COVERAGE drifts behind PGLITE_SCHEMA_SQL — if the
|
|
// schema blob added a new index on a column the bootstrap doesn't create,
|
|
// the SCHEMA_SQL exec below would crash even though the per-target asserts
|
|
// above pass.
|
|
const engine = new PGLiteEngine();
|
|
await engine.connect({});
|
|
try {
|
|
await engine.initSchema();
|
|
const db = (engine as any).db;
|
|
|
|
await db.exec(`
|
|
ALTER TABLE pages DROP CONSTRAINT IF EXISTS pages_source_slug_key;
|
|
ALTER TABLE pages ADD CONSTRAINT pages_slug_key UNIQUE (slug);
|
|
DROP INDEX IF EXISTS idx_pages_source_id;
|
|
ALTER TABLE pages DROP COLUMN IF EXISTS source_id;
|
|
DROP TABLE IF EXISTS sources CASCADE;
|
|
DROP INDEX IF EXISTS idx_links_source;
|
|
DROP INDEX IF EXISTS idx_links_origin;
|
|
ALTER TABLE links DROP CONSTRAINT IF EXISTS links_from_to_type_source_origin_unique;
|
|
ALTER TABLE links DROP COLUMN IF EXISTS link_source;
|
|
ALTER TABLE links DROP COLUMN IF EXISTS origin_page_id;
|
|
`);
|
|
|
|
// Bootstrap, then schema replay. Either step crashing fails the test.
|
|
const { PGLITE_SCHEMA_SQL } = await import('../src/core/pglite-schema.ts');
|
|
await (engine as any).applyForwardReferenceBootstrap();
|
|
await db.exec(PGLITE_SCHEMA_SQL);
|
|
} finally {
|
|
await engine.disconnect();
|
|
}
|
|
}, 30000);
|