feat(FN-4595): implement cumulative execution timing semantics

Fusion-Task-Id: FN-4595
Fusion-Task-Lineage: 73a3d0ad-5c10-4560-aec5-f9d138deeefd
This commit is contained in:
Fusion
2026-05-15 07:28:34 -07:00
committed by gsxdsm
parent b1f70a20d4
commit 3648d1e1df
18 changed files with 355 additions and 49 deletions

View File

@@ -717,7 +717,7 @@ describe("schema migration", () => {
{ id: "WS-001", mode: "prompt", gateMode: "advisory" },
{ id: "WS-002", mode: "script", gateMode: "advisory" },
]);
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
db.close();
});
@@ -767,7 +767,7 @@ describe("schema migration", () => {
reviewerContextRetryCount: 0,
reviewerFallbackRetryCount: 0,
});
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
db.close();
});
@@ -796,7 +796,7 @@ describe("schema migration", () => {
const columns = db.prepare("PRAGMA table_info(milestones)").all() as Array<{ name: string }>;
expect(columns.map((column) => column.name)).toContain("acceptanceCriteria");
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
db.close();
});
@@ -831,7 +831,7 @@ describe("schema migration", () => {
{ id: "WS-002", mode: "script", enabled: 1, gateMode: "advisory" },
{ id: "WS-003", mode: "prompt", enabled: 0, gateMode: "advisory" },
]);
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
db.close();
});

View File

@@ -290,7 +290,7 @@ describe("Database", () => {
});
it("seeds schema version", () => {
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
});
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(80);
expect(db.getSchemaVersion()).toBe(81);
});
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(80);
expect(db.getSchemaVersion()).toBe(81);
// 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(80);
expect(db.getSchemaVersion()).toBe(81);
// Re-init should not fail
db.init();
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
db.close();
});
@@ -1447,7 +1447,7 @@ describe("schema migrations", () => {
db.init();
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
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(80);
expect(db.getSchemaVersion()).toBe(81);
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(80);
expect(db.getSchemaVersion()).toBe(81);
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(80);
expect(db.getSchemaVersion()).toBe(81);
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(80);
expect(db.getSchemaVersion()).toBe(81);
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(80);
expect(db.getSchemaVersion()).toBe(81);
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(80);
expect(db.getSchemaVersion()).toBe(81);
// 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(80);
expect(localDb.getSchemaVersion()).toBe(81);
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(80);
expect(db.getSchemaVersion()).toBe(81);
expect(db.getLastModified()).toBeGreaterThan(0);
db.close();
@@ -2688,7 +2688,7 @@ describe("migration v77 task token budget columns", () => {
migrated = new Database(fusion);
migrated.init();
expect(migrated.getSchemaVersion()).toBe(80);
expect(migrated.getSchemaVersion()).toBe(81);
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);
@@ -2734,7 +2734,7 @@ describe("migration v67 drops orphan project auth tables", () => {
migrated = new Database(fusion);
migrated.init();
expect(migrated.getSchemaVersion()).toBe(80);
expect(migrated.getSchemaVersion()).toBe(81);
const tables = migrated
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'project_auth_%'")
.all() as Array<{ name: string }>;
@@ -2761,7 +2761,7 @@ describe("migration v67 drops orphan project auth tables", () => {
try {
fresh.init();
expect(fresh.getSchemaVersion()).toBe(80);
expect(fresh.getSchemaVersion()).toBe(81);
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(80);
expect(db1.getSchemaVersion()).toBe(81);
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(80);
expect(db3.getSchemaVersion()).toBe(81);
// 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(80);
expect(db1.getSchemaVersion()).toBe(81);
db1.close();
const db2 = createDatabase(testDir);
expect(() => db2.init()).not.toThrow();
expect(db2.getSchemaVersion()).toBe(80);
expect(db2.getSchemaVersion()).toBe(81);
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(80);
expect(db1.getSchemaVersion()).toBe(81);
// Step 2: Strip lifecycle and cancelledAt columns by recreating the
// table without them. This simulates a DB that was created before the

View File

@@ -2774,7 +2774,7 @@ describe("MissionStore", () => {
describe("Loop State & Validator Run Schema (v31)", () => {
it("schema version is 40 after migration", () => {
expect(db.getSchemaVersion()).toBe(80);
expect(db.getSchemaVersion()).toBe(81);
});
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(80);
expect(db.getSchemaVersion()).toBe(81);
});
});
});

View File

@@ -0,0 +1,88 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { createTaskStoreTestHarness } from "./store-test-helpers.js";
describe("TaskStore execution timing semantics", () => {
const harness = createTaskStoreTestHarness();
let store = harness.store();
beforeEach(async () => {
await harness.beforeEach();
store = harness.store();
});
afterEach(async () => {
await harness.afterEach();
});
it("hydrates legacy tasks without firstExecutionAt and initializes on next in-progress transition", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-05-15T10:00:00.000Z"));
const task = await store.createTask({ description: "legacy timing row" });
await store.moveTask(task.id, "todo");
await store.updateTask(task.id, {
firstExecutionAt: null,
cumulativeActiveMs: null,
executionStartedAt: null,
});
const moved = await store.moveTask(task.id, "in-progress");
expect(moved.firstExecutionAt).toBe("2026-05-15T10:00:00.000Z");
expect(moved.cumulativeActiveMs).toBe(0);
});
it("tracks firstExecutionAt and cumulativeActiveMs across reopen/resume cycles", async () => {
vi.useFakeTimers();
const t0 = new Date("2026-05-15T08:42:00.000Z");
vi.setSystemTime(t0);
const task = await store.createTask({ description: "timing lifecycle" });
await store.moveTask(task.id, "todo");
await store.moveTask(task.id, "in-progress");
vi.setSystemTime(new Date("2026-05-15T08:46:00.000Z"));
await store.moveTask(task.id, "todo", { moveSource: "user" });
vi.setSystemTime(new Date("2026-05-15T13:15:00.000Z"));
const resumed = await store.moveTask(task.id, "in-progress");
vi.setSystemTime(new Date("2026-05-15T13:17:00.000Z"));
const reviewed = await store.moveTask(task.id, "in-review");
expect(resumed.executionStartedAt).toBe("2026-05-15T13:15:00.000Z");
expect(reviewed.firstExecutionAt).toBe("2026-05-15T08:42:00.000Z");
expect(reviewed.cumulativeActiveMs).toBe(6 * 60_000);
});
it("accumulates active segment when preserveResumeState bounce exits in-progress", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-05-15T09:00:00.000Z"));
const task = await store.createTask({ description: "preserve resume timing" });
await store.moveTask(task.id, "todo");
const running = await store.moveTask(task.id, "in-progress");
vi.setSystemTime(new Date("2026-05-15T09:03:00.000Z"));
const bounced = await store.moveTask(task.id, "todo", { preserveResumeState: true });
expect(bounced.executionStartedAt).toBe(running.executionStartedAt);
expect(bounced.cumulativeActiveMs).toBe(3 * 60_000);
});
it("counts only in-progress time for in-progress → in-review → done", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-05-15T11:00:00.000Z"));
const task = await store.createTask({ description: "in review wait excluded" });
await store.moveTask(task.id, "todo");
await store.moveTask(task.id, "in-progress");
vi.setSystemTime(new Date("2026-05-15T11:05:00.000Z"));
await store.moveTask(task.id, "in-review");
vi.setSystemTime(new Date("2026-05-15T11:25:00.000Z"));
const done = await store.moveTask(task.id, "done");
expect(done.cumulativeActiveMs).toBe(5 * 60_000);
});
});

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(80);
expect(db.getSchemaVersion()).toBe(81);
const index = db
.prepare(