diff --git a/scripts/dev-seed.ts b/scripts/dev-seed.ts index bab89506aa..57657d5430 100644 --- a/scripts/dev-seed.ts +++ b/scripts/dev-seed.ts @@ -17,13 +17,11 @@ import { Command, Flag } from "effect/unstable/cli"; import { resolveWorktreeT3Home } from "@t3tools/shared/devHome"; -import { DevSeedError, seedDevDatabase } from "./lib/dev-seed.ts"; +import { DevSeedError, MAX_SEED_THREAD_LIMIT, seedDevDatabase } from "./lib/dev-seed.ts"; import { findRunningServerPid } from "./lib/server-runtime-probe.ts"; const DEFAULT_THREAD_LIMIT = 25; const DEFAULT_ACTIVITY_LIMIT = 200; -/** SQLite's default SQLITE_MAX_VARIABLE_NUMBER; one bound parameter per thread. */ -const MAX_THREAD_LIMIT = 32_766; class DevSeedTargetError extends Schema.TaggedErrorClass()( "DevSeedTargetError", @@ -140,16 +138,15 @@ const devSeedCli = Command.make("dev-seed", { Flag.map(Option.getOrUndefined), ), // Bounded below: SQLite reads a negative LIMIT as "no limit", which would - // quietly turn a capped copy into a full-table one. Bounded above because - // every selected thread becomes one bound parameter in the copy's `IN (...)`, - // and SQLite's compiled-in ceiling is 32766 — past it the seed dies with - // "too many SQL variables" after the target has already been emptied. + // quietly turn a capped copy into a full-table one. The upper bound comes + // from the seeder itself, which binds one variable per selected thread — + // stating it separately here is what previously let the two drift apart. threads: Flag.integer("threads").pipe( Flag.withSchema( - Schema.Int.check(Schema.isGreaterThan(0), Schema.isLessThanOrEqualTo(MAX_THREAD_LIMIT)), + Schema.Int.check(Schema.isGreaterThan(0), Schema.isLessThanOrEqualTo(MAX_SEED_THREAD_LIMIT)), ), Flag.withDescription( - `How many recent threads to copy (default ${String(DEFAULT_THREAD_LIMIT)}, max ${String(MAX_THREAD_LIMIT)}).`, + `How many recent threads to copy (default ${String(DEFAULT_THREAD_LIMIT)}, max ${String(MAX_SEED_THREAD_LIMIT)}).`, ), Flag.withDefault(DEFAULT_THREAD_LIMIT), ), diff --git a/scripts/lib/dev-seed.test.ts b/scripts/lib/dev-seed.test.ts index c335a56288..be6666b0d4 100644 --- a/scripts/lib/dev-seed.test.ts +++ b/scripts/lib/dev-seed.test.ts @@ -5,7 +5,7 @@ import * as NodeOS from "node:os"; import * as NodePath from "node:path"; import * as NodeSqlite from "node:sqlite"; -import { DevSeedError, seedDevDatabase } from "./dev-seed.ts"; +import { DevSeedError, MAX_SEED_THREAD_LIMIT, seedDevDatabase } from "./dev-seed.ts"; const SEEDED_AT = "2026-07-26T00:00:00.000Z"; @@ -511,23 +511,22 @@ describe("seedDevDatabase", () => { }); // Every statement keyed on thread ids must cost exactly one bound variable - // per thread: `--threads` is capped at SQLite's ceiling (32766), so a single - // extra binding makes the documented maximum fail — after the target has - // already been emptied. Runs the real seed, since the point is that no code - // path adds a binding, not that one hand-written statement doesn't. + // per thread: the limit *is* SQLite's ceiling, so a single extra binding + // makes the maximum fail — after the target has already been emptied. Runs + // the real seed, since the point is that no code path adds a binding, not + // that one hand-written statement doesn't. it("seeds at the maximum thread limit without exhausting SQL variables", () => { - const MAX_THREADS = 32_766; withDatabases( ({ source, target }) => { const summary = seedDevDatabase({ sourceDbPath: source, targetDbPath: target, - threadLimit: MAX_THREADS, + threadLimit: MAX_SEED_THREAD_LIMIT, activityLimit: 1, seededAt: SEEDED_AT, }); - assert.equal(summary.threads, MAX_THREADS); + assert.equal(summary.threads, MAX_SEED_THREAD_LIMIT); const [unsettled] = query<{ count: number }>( target, "SELECT COUNT(*) count FROM projection_turns WHERE state IN ('running','pending')", @@ -536,10 +535,38 @@ describe("seedDevDatabase", () => { }, // Threads only: the per-thread tables are what drive the binding count, // and skipping their rows keeps the fixture cheap to build. - { threads: MAX_THREADS, activities: 0, minimalRows: true }, + { threads: MAX_SEED_THREAD_LIMIT, activities: 0, minimalRows: true }, ); }); + // A caller that bypasses the CLI flag would otherwise discover the ceiling + // only after the target had been emptied and the copy rolled back. + it("refuses a thread limit above the ceiling before touching the target", () => { + withDatabases(({ source, target }) => { + assert.throws( + () => + seedDevDatabase({ + sourceDbPath: source, + targetDbPath: target, + threadLimit: MAX_SEED_THREAD_LIMIT + 1, + activityLimit: 10, + seededAt: SEEDED_AT, + }), + DevSeedError, + ); + + // The target's own row is still there: nothing was deleted. + const projects = query<{ project_id: string }>( + target, + "SELECT project_id FROM projection_projects", + ); + assert.deepStrictEqual( + projects.map((row) => row.project_id), + ["stale"], + ); + }); + }); + it("reports a source with nothing to copy", () => { withDatabases( ({ source, target }) => { diff --git a/scripts/lib/dev-seed.ts b/scripts/lib/dev-seed.ts index af74011379..94e253ac12 100644 --- a/scripts/lib/dev-seed.ts +++ b/scripts/lib/dev-seed.ts @@ -21,10 +21,23 @@ import * as NodeSqlite from "node:sqlite"; import { PROJECTION_TABLES_IN_DEPENDENCY_ORDER, PROJECTOR_NAMES } from "./projection-tables.ts"; +/** + * The most threads one call can copy: SQLite's default + * SQLITE_MAX_VARIABLE_NUMBER, because the statements below bind one variable + * per selected thread. + * + * Exported so the CLI's `--threads` bound comes from the code that actually has + * to satisfy it. When the two were stated separately, a statement that bound + * two extra literals put the CLI's own documented maximum over the ceiling — + * and it failed after the target had been emptied. Anything added to those + * statements has to stay free of per-call bindings, or this has to come down. + */ +export const MAX_SEED_THREAD_LIMIT = 32_766; + export interface DevSeedOptions { readonly sourceDbPath: string; readonly targetDbPath: string; - /** How many recent threads to copy. */ + /** How many recent threads to copy. Capped at {@link MAX_SEED_THREAD_LIMIT}. */ readonly threadLimit: number; /** * Newest activities kept per thread. The real table runs to six figures, and @@ -266,6 +279,16 @@ function settleUnfinishedTurns( } export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary { + // Checked before anything is opened, and certainly before the target is + // emptied: overrunning the ceiling mid-copy destroys the target's data and + // rolls back without replacing it. + if (options.threadLimit > MAX_SEED_THREAD_LIMIT) { + throw new DevSeedError( + `cannot copy more than ${String(MAX_SEED_THREAD_LIMIT)} threads in one pass`, + "Each thread costs one SQLite bound variable, and that is the engine's limit.", + ); + } + let source: NodeSqlite.DatabaseSync; try { source = new NodeSqlite.DatabaseSync(options.sourceDbPath, { readOnly: true });