feat(FN-4396): complete Step 3 — migrate and persist token budget fields

Fusion-Task-Id: FN-4396
Fusion-Task-Lineage: 3839210b-b443-410d-a476-aa81128c739a
This commit is contained in:
Fusion
2026-05-14 07:42:39 -07:00
committed by gsxdsm
parent 9c298d0596
commit f1dd57ff91
9 changed files with 159 additions and 40 deletions

View File

@@ -717,7 +717,7 @@ describe("schema migration", () => {
{ id: "WS-001", mode: "prompt", gateMode: "advisory" },
{ id: "WS-002", mode: "script", gateMode: "gate" },
]);
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
db.close();
});

View File

@@ -290,7 +290,7 @@ describe("Database", () => {
});
it("seeds schema version", () => {
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
});
it("includes tokenUsageCacheWriteTokens on freshly initialized tasks table", () => {
@@ -318,7 +318,7 @@ describe("Database", () => {
it("is idempotent - calling init() twice does not fail", () => {
expect(() => db.init()).not.toThrow();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
});
it("does not overwrite existing config on re-init", () => {
// Update the config
@@ -1383,7 +1383,7 @@ describe("schema migrations", () => {
db.init();
// Verify version bumped to 29 (includes v1→v2 through v26→v29)
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
// Verify new columns exist and existing data is intact
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
@@ -1408,11 +1408,11 @@ describe("schema migrations", () => {
const db = new Database(fusionDir);
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
// Re-init should not fail
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
db.close();
});
@@ -1447,7 +1447,7 @@ describe("schema migrations", () => {
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
expect(cols.map((col) => col.name)).toContain("priority");
@@ -1488,7 +1488,7 @@ describe("schema migrations", () => {
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
const colNames = cols.map((col) => col.name);
@@ -1560,7 +1560,7 @@ describe("schema migrations", () => {
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
const colNames = cols.map((col) => col.name);
@@ -1800,7 +1800,7 @@ describe("schema migrations", () => {
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
const cols = db.prepare("PRAGMA table_info(chat_messages)").all() as Array<{ name: string }>;
expect(cols.map((col) => col.name)).toContain("attachments");
@@ -1874,7 +1874,7 @@ describe("schema migrations", () => {
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'agentRatings'").all() as Array<{ name: string }>;
expect(tables).toEqual([{ name: "agentRatings" }]);
@@ -1898,7 +1898,7 @@ describe("schema migrations", () => {
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'mission_events'").all() as Array<{ name: string }>;
expect(tables).toEqual([{ name: "mission_events" }]);
@@ -2002,7 +2002,7 @@ describe("schema migrations", () => {
db.init();
// Verify version bumped to 29
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
// Verify new columns exist and existing data is intact
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
@@ -2221,7 +2221,7 @@ describe("schema migrations", () => {
localDb.init();
expect(localDb.getSchemaVersion()).toBe(76);
expect(localDb.getSchemaVersion()).toBe(77);
const columns = localDb.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
expect(columns.map((column) => column.name)).toContain("tokenUsageCacheWriteTokens");
@@ -2532,7 +2532,7 @@ describe("createDatabase factory", () => {
const db = createDatabase(fusionDir);
db.init();
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
expect(db.getLastModified()).toBeGreaterThan(0);
db.close();
@@ -2646,6 +2646,72 @@ describe("TaskStore — verification cache", () => {
});
});
describe("migration v77 task token budget columns", () => {
it("includes task token budget columns on fresh init", () => {
const temp = makeTmpDir();
const fusion = join(temp, ".fusion");
const fresh = new Database(fusion);
try {
fresh.init();
const rows = fresh
.prepare("PRAGMA table_info(tasks)")
.all() as Array<{ name: string }>;
const names = new Set(rows.map((row) => row.name));
expect(names.has("tokenBudgetSoftAlertedAt")).toBe(true);
expect(names.has("tokenBudgetHardAlertedAt")).toBe(true);
expect(names.has("tokenBudgetOverride")).toBe(true);
} finally {
try {
fresh.close();
} catch {
// already closed
}
removeTrackedTmpDirSync(temp);
}
});
it("adds task token budget columns during migration without dropping existing rows", () => {
const temp = makeTmpDir();
const fusion = join(temp, ".fusion");
const localDb = new Database(fusion);
let migrated: Database | undefined;
try {
localDb.init();
localDb.prepare("INSERT INTO tasks (id, description, \"column\", createdAt, updatedAt) VALUES (?, ?, ?, ?, ?)")
.run("FN-MIGRATE", "migration row", "todo", "2026-01-01T00:00:00.000Z", "2026-01-01T00:00:00.000Z");
localDb.prepare("UPDATE __meta SET value = '76' WHERE key = 'schemaVersion'").run();
localDb.exec("ALTER TABLE tasks DROP COLUMN tokenBudgetSoftAlertedAt");
localDb.exec("ALTER TABLE tasks DROP COLUMN tokenBudgetHardAlertedAt");
localDb.exec("ALTER TABLE tasks DROP COLUMN tokenBudgetOverride");
localDb.close();
migrated = new Database(fusion);
migrated.init();
expect(migrated.getSchemaVersion()).toBe(77);
const rows = migrated.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
const names = new Set(rows.map((row) => row.name));
expect(names.has("tokenBudgetSoftAlertedAt")).toBe(true);
expect(names.has("tokenBudgetHardAlertedAt")).toBe(true);
expect(names.has("tokenBudgetOverride")).toBe(true);
const task = migrated.prepare("SELECT id FROM tasks WHERE id = ?").get("FN-MIGRATE") as { id: string } | undefined;
expect(task?.id).toBe("FN-MIGRATE");
} finally {
try {
migrated?.close();
} catch {
// already closed
}
try {
localDb.close();
} catch {
// already closed
}
removeTrackedTmpDirSync(temp);
}
});
});
describe("migration v67 drops orphan project auth tables", () => {
it("drops project_auth_* tables left over from the removed pluggable auth feature", () => {
const temp = makeTmpDir();
@@ -2668,7 +2734,7 @@ describe("migration v67 drops orphan project auth tables", () => {
migrated = new Database(fusion);
migrated.init();
expect(migrated.getSchemaVersion()).toBe(76);
expect(migrated.getSchemaVersion()).toBe(77);
const tables = migrated
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'project_auth_%'")
.all() as Array<{ name: string }>;
@@ -2695,7 +2761,7 @@ describe("migration v67 drops orphan project auth tables", () => {
try {
fresh.init();
expect(fresh.getSchemaVersion()).toBe(76);
expect(fresh.getSchemaVersion()).toBe(77);
const tables = fresh
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'project_auth_%'")
.all() as Array<{ name: string }>;

View File

@@ -886,7 +886,7 @@ describe("Migration: pre-33 DB upgrade", () => {
// Step 1: Create a fresh database at v33 (runs all migrations up to 33)
const db1 = createDatabase(legacyDir);
db1.init();
expect(db1.getSchemaVersion()).toBe(76);
expect(db1.getSchemaVersion()).toBe(77);
db1.close();
// Step 2: Manually downgrade to version 32 and drop insight tables
@@ -921,7 +921,7 @@ describe("Migration: pre-33 DB upgrade", () => {
expect(tableNamesBefore).not.toContain("project_insight_runs");
// Now run init — this triggers the v32→v33 migration
db3.init();
expect(db3.getSchemaVersion()).toBe(76);
expect(db3.getSchemaVersion()).toBe(77);
// Step 4: Verify insight tables exist after migration
const tablesAfter = db3.prepare(
@@ -952,12 +952,12 @@ describe("Migration: pre-33 DB upgrade", () => {
try {
const db1 = createDatabase(testDir);
db1.init();
expect(db1.getSchemaVersion()).toBe(76);
expect(db1.getSchemaVersion()).toBe(77);
db1.close();
const db2 = createDatabase(testDir);
expect(() => db2.init()).not.toThrow();
expect(db2.getSchemaVersion()).toBe(76);
expect(db2.getSchemaVersion()).toBe(77);
db2.close();
} finally {
rmSync(testDir, { recursive: true, force: true });
@@ -971,7 +971,7 @@ describe("Migration: pre-33 DB upgrade", () => {
// Step 1: Create a fresh DB and run migrations
const db1 = createDatabase(compatDir);
db1.init();
expect(db1.getSchemaVersion()).toBe(76);
expect(db1.getSchemaVersion()).toBe(77);
// Step 2: Strip lifecycle and cancelledAt columns by recreating the
// table without them. This simulates a DB that was created before the

View File

@@ -2639,7 +2639,7 @@ describe("MissionStore", () => {
describe("Loop State & Validator Run Schema (v31)", () => {
it("schema version is 40 after migration", () => {
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
});
it("mission_features table has loop state columns", () => {

View File

@@ -584,7 +584,7 @@ describe("Run Audit", () => {
});
it("schema version is bumped to 40", () => {
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
});
});
});

View File

@@ -141,6 +141,39 @@ describe("TaskStore", () => {
expect(detail.tokenUsage).toEqual(tokenUsage);
});
it("round-trips token budget alert sentinels and overrides", async () => {
const created = await harness.store().createTask({
description: "Token budget fields",
});
(harness.store() as any).db.prepare(`
UPDATE tasks
SET tokenBudgetSoftAlertedAt = ?,
tokenBudgetHardAlertedAt = ?,
tokenBudgetOverride = ?
WHERE id = ?
`).run(
"2026-05-14T01:00:00.000Z",
"2026-05-14T01:05:00.000Z",
JSON.stringify({
soft: 1_000_000,
hard: 2_000_000,
raisedAt: "2026-05-14T01:06:00.000Z",
reason: "manual override",
}),
created.id,
);
const reloaded = await harness.store().getTask(created.id);
expect(reloaded.tokenBudgetSoftAlertedAt).toBe("2026-05-14T01:00:00.000Z");
expect(reloaded.tokenBudgetHardAlertedAt).toBe("2026-05-14T01:05:00.000Z");
expect(reloaded.tokenBudgetOverride).toEqual({
soft: 1_000_000,
hard: 2_000_000,
raisedAt: "2026-05-14T01:06:00.000Z",
reason: "manual override",
});
});
it("clears token usage via null update and keeps it absent after reload", async () => {
// Cross-instance persistence test — see counterpart above.
harness.store().close();

View File

@@ -51,7 +51,7 @@ describe("TaskStore task documents", () => {
expect(tableNames.has("task_documents")).toBe(true);
expect(tableNames.has("task_document_revisions")).toBe(true);
expect(db.getSchemaVersion()).toBe(76);
expect(db.getSchemaVersion()).toBe(77);
const index = db
.prepare(