|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { Database } from "../../shared/sqlite"; |
| 6 | +import { createTestTempDirFromPath } from "../../shared/test-temp-dir"; |
| 7 | +import { LATEST_MIGRATION_VERSION, MIGRATIONS, runMigrations } from "./migrations"; |
| 8 | +import { |
| 9 | + getSchemaFenceRejection, |
| 10 | + initializeDatabase, |
| 11 | + LATEST_SUPPORTED_VERSION, |
| 12 | + openDatabase, |
| 13 | +} from "./storage-db"; |
| 14 | +import { |
| 15 | + commitHostRunnerPass, |
| 16 | + createHostRunnerState, |
| 17 | + loadHostRunnerRecord, |
| 18 | +} from "./storage-host-runner"; |
| 19 | +import { SESSION_SCOPED_TABLES } from "./storage-session-tables"; |
| 20 | + |
| 21 | +const tables = ["host_runner_entries", "host_runner_ids", "host_runner_views", "host_runner_state"]; |
| 22 | + |
| 23 | +function schema(db: Database): unknown[] { |
| 24 | + return db |
| 25 | + .prepare( |
| 26 | + "SELECT name, sql FROM sqlite_master WHERE name LIKE 'host_runner_%' ORDER BY name", |
| 27 | + ) |
| 28 | + .all(); |
| 29 | +} |
| 30 | + |
| 31 | +describe("migration 97", () => { |
| 32 | + test("populated v96 upgrades losslessly and converges with fresh schema, idempotently", () => { |
| 33 | + const db = new Database(":memory:"); |
| 34 | + const fresh = new Database(":memory:"); |
| 35 | + try { |
| 36 | + initializeDatabase(db); |
| 37 | + for (const table of tables) db.exec(`DROP TABLE ${table}`); |
| 38 | + db.exec( |
| 39 | + "CREATE TABLE schema_migrations(version INTEGER PRIMARY KEY, applied_at INTEGER NOT NULL, description TEXT)", |
| 40 | + ); |
| 41 | + for (const migration of MIGRATIONS.filter((m) => m.version <= 96)) { |
| 42 | + migration.up(db); |
| 43 | + db.prepare("INSERT INTO schema_migrations(version, applied_at) VALUES (?, 0)").run( |
| 44 | + migration.version, |
| 45 | + ); |
| 46 | + } |
| 47 | + db.prepare( |
| 48 | + "INSERT INTO session_meta(session_id, harness, counter) VALUES ('legacy', 'opencode', 42)", |
| 49 | + ).run(); |
| 50 | + const before = db.prepare("SELECT * FROM session_meta WHERE session_id='legacy'").get(); |
| 51 | + runMigrations(db); |
| 52 | + expect( |
| 53 | + db.prepare("SELECT * FROM session_meta WHERE session_id='legacy'").get(), |
| 54 | + ).toEqual(before); |
| 55 | + expect(LATEST_SUPPORTED_VERSION).toBe(97); |
| 56 | + expect(LATEST_MIGRATION_VERSION).toBe(97); |
| 57 | + initializeDatabase(fresh); |
| 58 | + runMigrations(fresh); |
| 59 | + expect(schema(db)).toEqual(schema(fresh)); |
| 60 | + const key = { session_id: "s", harness: "opencode2" }; |
| 61 | + const state = { |
| 62 | + ...createHostRunnerState("lineage"), |
| 63 | + ordinal_divergence: 2, |
| 64 | + bootstrap_refused_json: '{"code":"misconfigured","config":{"model":"x"}}', |
| 65 | + }; |
| 66 | + commitHostRunnerPass(db, key, { state, elided: ["marker"] }); |
| 67 | + const record = loadHostRunnerRecord(db, key); |
| 68 | + MIGRATIONS.find((m) => m.version === 97)!.up(db); |
| 69 | + runMigrations(db); |
| 70 | + expect(loadHostRunnerRecord(db, key)).toEqual(record); |
| 71 | + for (const table of tables) { |
| 72 | + expect( |
| 73 | + SESSION_SCOPED_TABLES.find((definition) => definition.table === table) |
| 74 | + ?.harnessScoped, |
| 75 | + ).toBe(true); |
| 76 | + } |
| 77 | + } finally { |
| 78 | + db.close(); |
| 79 | + fresh.close(); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + test("startup heals missing race, divergence and bootstrap refusal columns", () => { |
| 84 | + const db = new Database(":memory:"); |
| 85 | + try { |
| 86 | + initializeDatabase(db); |
| 87 | + db.exec( |
| 88 | + "ALTER TABLE host_runner_entries DROP COLUMN race; ALTER TABLE host_runner_state DROP COLUMN ordinal_divergence; ALTER TABLE host_runner_state DROP COLUMN bootstrap_refused_json", |
| 89 | + ); |
| 90 | + initializeDatabase(db); |
| 91 | + const columns = (table: string) => |
| 92 | + (db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[]).map( |
| 93 | + (c) => c.name, |
| 94 | + ); |
| 95 | + expect(columns("host_runner_entries")).toContain("race"); |
| 96 | + expect(columns("host_runner_state")).toContain("ordinal_divergence"); |
| 97 | + expect(columns("host_runner_state")).toContain("bootstrap_refused_json"); |
| 98 | + } finally { |
| 99 | + db.close(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + test("a v96 host refuses the migrated runner store at the normal open fence without changing it", () => { |
| 104 | + const directory = createTestTempDirFromPath(join(tmpdir(), "host-runner-fence-")); |
| 105 | + const path = join(directory, "context.db"); |
| 106 | + const db = new Database(path); |
| 107 | + try { |
| 108 | + initializeDatabase(db); |
| 109 | + runMigrations(db); |
| 110 | + const before = schema(db); |
| 111 | + expect(openDatabase({ dbPath: path, latestSupportedVersion: 96 })).toBeNull(); |
| 112 | + expect(getSchemaFenceRejection()).toEqual({ |
| 113 | + persistedVersion: 97, |
| 114 | + supportedVersion: 96, |
| 115 | + }); |
| 116 | + expect(schema(db)).toEqual(before); |
| 117 | + } finally { |
| 118 | + db.close(); |
| 119 | + rmSync(directory, { recursive: true, force: true }); |
| 120 | + } |
| 121 | + }); |
| 122 | +}); |
0 commit comments