feat(FEAT-002): add startValidatorRun and completeValidatorRun methods
- Add validator-run:started and validator-run:completed events to MissionStoreEvents - Add rowToValidatorRun converter method - Add generateValidatorRunId method - Implement startValidatorRun: creates run with status='running', sets startedAt, increments feature validatorAttemptCount, updates lastValidatorRunId, sets loopState to 'validating', emits event - Implement completeValidatorRun: handles passed/failed/blocked/error transitions with correct loop state updates and durationMs computation - Add tests for all validator run methods covering VAL-DM-015 through VAL-DM-020 Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
@@ -2638,6 +2638,201 @@ describe("MissionStore", () => {
|
||||
expect(indexNames).toContain("idxFixLineageSourceFeatureId");
|
||||
});
|
||||
});
|
||||
|
||||
describe("validator run methods", () => {
|
||||
it("startValidatorRun creates run with status running (VAL-DM-015)", () => {
|
||||
const mission = store.createMission({ title: "Validator Run Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id, "task_completion");
|
||||
|
||||
expect(run).toBeDefined();
|
||||
expect(run.status).toBe("running");
|
||||
expect(run.featureId).toBe(feature.id);
|
||||
expect(run.milestoneId).toBe(milestone.id);
|
||||
expect(run.sliceId).toBe(slice.id);
|
||||
expect(run.triggerType).toBe("task_completion");
|
||||
expect(run.startedAt).toBeDefined();
|
||||
expect(run.completedAt).toBeUndefined();
|
||||
|
||||
// Verify feature was updated
|
||||
const updatedFeature = store.getFeature(feature.id);
|
||||
expect(updatedFeature!.validatorAttemptCount).toBe(1);
|
||||
expect(updatedFeature!.lastValidatorRunId).toBe(run.id);
|
||||
expect(updatedFeature!.loopState).toBe("validating");
|
||||
});
|
||||
|
||||
it("startValidatorRun increments validatorAttemptCount (VAL-DM-015)", () => {
|
||||
const mission = store.createMission({ title: "Validator Run Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
// Start first run
|
||||
const run1 = store.startValidatorRun(feature.id);
|
||||
expect(run1.validatorAttempt).toBe(1);
|
||||
|
||||
// Start second run
|
||||
const run2 = store.startValidatorRun(feature.id);
|
||||
expect(run2.validatorAttempt).toBe(2);
|
||||
|
||||
// Verify feature has correct count
|
||||
const updatedFeature = store.getFeature(feature.id);
|
||||
expect(updatedFeature!.validatorAttemptCount).toBe(2);
|
||||
expect(updatedFeature!.lastValidatorRunId).toBe(run2.id);
|
||||
});
|
||||
|
||||
it("completeValidatorRun transitions to passed (VAL-DM-016)", () => {
|
||||
const mission = store.createMission({ title: "Complete Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
const completedRun = store.completeValidatorRun(run.id, "passed", "All assertions passed");
|
||||
|
||||
expect(completedRun.status).toBe("passed");
|
||||
expect(completedRun.completedAt).toBeDefined();
|
||||
expect(completedRun.summary).toBe("All assertions passed");
|
||||
|
||||
// Verify feature state
|
||||
const updatedFeature = store.getFeature(feature.id);
|
||||
expect(updatedFeature!.loopState).toBe("passed");
|
||||
expect(updatedFeature!.lastValidatorStatus).toBe("passed");
|
||||
});
|
||||
|
||||
it("completeValidatorRun transitions to failed (VAL-DM-017)", () => {
|
||||
const mission = store.createMission({ title: "Complete Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
const completedRun = store.completeValidatorRun(run.id, "failed", "Assertions failed");
|
||||
|
||||
expect(completedRun.status).toBe("failed");
|
||||
|
||||
// Verify feature state
|
||||
const updatedFeature = store.getFeature(feature.id);
|
||||
expect(updatedFeature!.loopState).toBe("needs_fix");
|
||||
expect(updatedFeature!.lastValidatorStatus).toBe("failed");
|
||||
});
|
||||
|
||||
it("completeValidatorRun transitions to blocked (VAL-DM-018)", () => {
|
||||
const mission = store.createMission({ title: "Complete Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
const completedRun = store.completeValidatorRun(run.id, "blocked", undefined, "External dependency unavailable");
|
||||
|
||||
expect(completedRun.status).toBe("blocked");
|
||||
expect(completedRun.blockedReason).toBe("External dependency unavailable");
|
||||
|
||||
// Verify feature state
|
||||
const updatedFeature = store.getFeature(feature.id);
|
||||
expect(updatedFeature!.loopState).toBe("blocked");
|
||||
expect(updatedFeature!.lastValidatorStatus).toBe("blocked");
|
||||
});
|
||||
|
||||
it("completeValidatorRun transitions to error (VAL-DM-019)", () => {
|
||||
const mission = store.createMission({ title: "Complete Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
const completedRun = store.completeValidatorRun(run.id, "error", "AI session failed");
|
||||
|
||||
expect(completedRun.status).toBe("error");
|
||||
|
||||
// Verify feature stays in validating state on error
|
||||
const updatedFeature = store.getFeature(feature.id);
|
||||
expect(updatedFeature!.loopState).toBe("validating");
|
||||
expect(updatedFeature!.lastValidatorStatus).toBe("error");
|
||||
});
|
||||
|
||||
it("completeValidatorRun computes durationMs (VAL-DM-020)", () => {
|
||||
const mission = store.createMission({ title: "Duration Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
// Use vi.useFakeTimers to control time
|
||||
const startTime = new Date(run.startedAt).getTime();
|
||||
const expectedDuration = 5000; // 5 seconds
|
||||
|
||||
// Advance timers
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(startTime + expectedDuration);
|
||||
|
||||
const completedRun = store.completeValidatorRun(run.id, "passed");
|
||||
|
||||
vi.useRealTimers();
|
||||
|
||||
// durationMs should be computed correctly
|
||||
const completedTime = new Date(completedRun.completedAt!).getTime();
|
||||
const actualDuration = completedTime - startTime;
|
||||
expect(actualDuration).toBe(expectedDuration);
|
||||
});
|
||||
|
||||
it("getValidatorRun returns run by id", () => {
|
||||
const mission = store.createMission({ title: "Get Run Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
const retrieved = store.getValidatorRun(run.id);
|
||||
expect(retrieved).toBeDefined();
|
||||
expect(retrieved!.id).toBe(run.id);
|
||||
expect(retrieved!.status).toBe("running");
|
||||
});
|
||||
|
||||
it("startValidatorRun emits validator-run:started event", () => {
|
||||
const mission = store.createMission({ title: "Event Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const eventListener = vi.fn();
|
||||
store.on("validator-run:started", eventListener);
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
expect(eventListener).toHaveBeenCalledWith(run);
|
||||
|
||||
store.off("validator-run:started", eventListener);
|
||||
});
|
||||
|
||||
it("completeValidatorRun emits validator-run:completed event", () => {
|
||||
const mission = store.createMission({ title: "Event Test" });
|
||||
const milestone = store.addMilestone(mission.id, { title: "MS" });
|
||||
const slice = store.addSlice(milestone.id, { title: "SL" });
|
||||
const feature = store.addFeature(slice.id, { title: "Test Feature" });
|
||||
|
||||
const run = store.startValidatorRun(feature.id);
|
||||
|
||||
const eventListener = vi.fn();
|
||||
store.on("validator-run:completed", eventListener);
|
||||
|
||||
const completedRun = store.completeValidatorRun(run.id, "passed", "Success");
|
||||
|
||||
expect(eventListener).toHaveBeenCalledWith(completedRun, "passed", expect.any(Number));
|
||||
|
||||
store.off("validator-run:completed", eventListener);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// vi import for vitest mocking
|
||||
|
||||
@@ -19,6 +19,7 @@ import type {
|
||||
Milestone,
|
||||
Slice,
|
||||
MissionFeature,
|
||||
MissionValidatorRun,
|
||||
MissionCreateInput,
|
||||
MilestoneCreateInput,
|
||||
SliceCreateInput,
|
||||
@@ -40,6 +41,8 @@ import type {
|
||||
ContractAssertionCreateInput,
|
||||
ContractAssertionUpdateInput,
|
||||
MilestoneValidationState,
|
||||
ValidatorRunStatus,
|
||||
FeatureLoopState,
|
||||
} from "./mission-types.js";
|
||||
|
||||
// ── Mission Summary Type ─────────────────────────────────────────────
|
||||
@@ -103,6 +106,10 @@ export interface MissionStoreEvents {
|
||||
"assertion:unlinked": [{ featureId: string; assertionId: string }];
|
||||
/** Emitted when a milestone's validation state is recomputed */
|
||||
"milestone:validation:updated": [{ milestoneId: string; state: MilestoneValidationState; rollup: MilestoneValidationRollup }];
|
||||
/** Emitted when a validator run is started */
|
||||
"validator-run:started": [MissionValidatorRun];
|
||||
/** Emitted when a validator run is completed (run, final status, durationMs) */
|
||||
"validator-run:completed": [MissionValidatorRun, ValidatorRunStatus, number];
|
||||
}
|
||||
|
||||
// ── MissionStore Class ──────────────────────────────────────────────
|
||||
@@ -259,6 +266,28 @@ export class MissionStore extends EventEmitter<MissionStoreEvents> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a database row to a MissionValidatorRun object.
|
||||
*/
|
||||
private rowToValidatorRun(row: any): MissionValidatorRun {
|
||||
return {
|
||||
id: row.id,
|
||||
featureId: row.featureId,
|
||||
milestoneId: row.milestoneId,
|
||||
sliceId: row.sliceId,
|
||||
status: row.status as ValidatorRunStatus,
|
||||
triggerType: row.triggerType || undefined,
|
||||
implementationAttempt: row.implementationAttempt ?? 0,
|
||||
validatorAttempt: row.validatorAttempt ?? 0,
|
||||
summary: row.summary || undefined,
|
||||
blockedReason: row.blockedReason || undefined,
|
||||
startedAt: row.startedAt,
|
||||
completedAt: row.completedAt || undefined,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Mission CRUD Operations ────────────────────────────────────────
|
||||
|
||||
/**
|
||||
@@ -1694,6 +1723,199 @@ export class MissionStore extends EventEmitter<MissionStoreEvents> {
|
||||
return this.rowToFeature(row);
|
||||
}
|
||||
|
||||
// ── Validator Run Operations ────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Start a new validator run for a feature.
|
||||
* Creates a run with status='running', sets startedAt, increments the feature's
|
||||
* validatorAttemptCount, updates lastValidatorRunId, and emits validator-run:started event.
|
||||
*
|
||||
* @param featureId - Feature ID to start validation for
|
||||
* @param triggerType - What triggered this run (e.g., 'task_completion', 'manual', 'scheduled')
|
||||
* @returns The created validator run
|
||||
* @throws Error if feature not found
|
||||
*/
|
||||
startValidatorRun(featureId: string, triggerType?: string): MissionValidatorRun {
|
||||
const feature = this.getFeature(featureId);
|
||||
if (!feature) {
|
||||
throw new Error(`Feature ${featureId} not found`);
|
||||
}
|
||||
|
||||
// Resolve the hierarchy to get milestoneId and sliceId
|
||||
const slice = this.getSlice(feature.sliceId);
|
||||
if (!slice) {
|
||||
throw new Error(`Slice ${feature.sliceId} not found`);
|
||||
}
|
||||
|
||||
const milestone = this.getMilestone(slice.milestoneId);
|
||||
if (!milestone) {
|
||||
throw new Error(`Milestone ${slice.milestoneId} not found`);
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const id = this.generateValidatorRunId();
|
||||
|
||||
// Increment validatorAttemptCount on the feature
|
||||
const newValidatorAttemptCount = (feature.validatorAttemptCount ?? 0) + 1;
|
||||
|
||||
const run: MissionValidatorRun = {
|
||||
id,
|
||||
featureId,
|
||||
milestoneId: milestone.id,
|
||||
sliceId: slice.id,
|
||||
status: "running",
|
||||
triggerType,
|
||||
implementationAttempt: feature.implementationAttemptCount ?? 0,
|
||||
validatorAttempt: newValidatorAttemptCount,
|
||||
startedAt: now,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
|
||||
this.db.transaction(() => {
|
||||
// Insert the validator run
|
||||
this.db.prepare(`
|
||||
INSERT INTO mission_validator_runs (id, featureId, milestoneId, sliceId, status, triggerType, implementationAttempt, validatorAttempt, startedAt, createdAt, updatedAt)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
run.id,
|
||||
run.featureId,
|
||||
run.milestoneId,
|
||||
run.sliceId,
|
||||
run.status,
|
||||
run.triggerType ?? null,
|
||||
run.implementationAttempt,
|
||||
run.validatorAttempt,
|
||||
run.startedAt,
|
||||
run.createdAt,
|
||||
run.updatedAt,
|
||||
);
|
||||
|
||||
// Update the feature: increment validatorAttemptCount and set lastValidatorRunId
|
||||
this.updateFeature(featureId, {
|
||||
validatorAttemptCount: newValidatorAttemptCount,
|
||||
lastValidatorRunId: run.id,
|
||||
loopState: "validating",
|
||||
});
|
||||
});
|
||||
|
||||
this.db.bumpLastModified();
|
||||
this.emit("validator-run:started", run);
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete a validator run with the given result.
|
||||
* Sets run status, completedAt, durationMs and updates feature loop state based on result.
|
||||
*
|
||||
* Result transitions:
|
||||
* - 'passed': run status='passed', feature loopState='passed', lastValidatorStatus='passed'
|
||||
* - 'failed': run status='failed', feature loopState='needs_fix', lastValidatorStatus='failed'
|
||||
* - 'blocked': run status='blocked', feature loopState='blocked', lastValidatorStatus='blocked'
|
||||
* - 'error': run status='error', feature loopState stays 'validating', lastValidatorStatus='error'
|
||||
*
|
||||
* @param runId - Validator run ID to complete
|
||||
* @param result - The completion result status
|
||||
* @param summary - Optional summary of the validation run
|
||||
* @param blockedReason - Optional reason if result is 'blocked'
|
||||
* @returns The completed validator run
|
||||
* @throws Error if run not found
|
||||
*/
|
||||
completeValidatorRun(
|
||||
runId: string,
|
||||
result: "passed" | "failed" | "blocked" | "error",
|
||||
summary?: string,
|
||||
blockedReason?: string,
|
||||
): MissionValidatorRun {
|
||||
const run = this.getValidatorRun(runId);
|
||||
if (!run) {
|
||||
throw new Error(`Validator run ${runId} not found`);
|
||||
}
|
||||
|
||||
if (run.status !== "running") {
|
||||
throw new Error(`Validator run ${runId} is not in 'running' status`);
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const completedAt = now;
|
||||
|
||||
// Compute durationMs as non-negative integer
|
||||
const startedAtMs = new Date(run.startedAt).getTime();
|
||||
const completedAtMs = new Date(completedAt).getTime();
|
||||
const durationMs = Math.max(0, completedAtMs - startedAtMs);
|
||||
|
||||
// Determine feature loop state and lastValidatorStatus based on result
|
||||
let featureLoopState: FeatureLoopState;
|
||||
let featureLastValidatorStatus: ValidatorRunStatus;
|
||||
|
||||
switch (result) {
|
||||
case "passed":
|
||||
featureLoopState = "passed";
|
||||
featureLastValidatorStatus = "passed";
|
||||
break;
|
||||
case "failed":
|
||||
featureLoopState = "needs_fix";
|
||||
featureLastValidatorStatus = "failed";
|
||||
break;
|
||||
case "blocked":
|
||||
featureLoopState = "blocked";
|
||||
featureLastValidatorStatus = "blocked";
|
||||
break;
|
||||
case "error":
|
||||
featureLoopState = "validating"; // stays validating on error
|
||||
featureLastValidatorStatus = "error";
|
||||
break;
|
||||
}
|
||||
|
||||
this.db.transaction(() => {
|
||||
// Update the validator run
|
||||
this.db.prepare(`
|
||||
UPDATE mission_validator_runs SET
|
||||
status = ?,
|
||||
summary = ?,
|
||||
blockedReason = ?,
|
||||
completedAt = ?,
|
||||
updatedAt = ?
|
||||
WHERE id = ?
|
||||
`).run(
|
||||
result,
|
||||
summary ?? null,
|
||||
blockedReason ?? null,
|
||||
completedAt,
|
||||
now,
|
||||
runId,
|
||||
);
|
||||
|
||||
// Update the feature's loop state and lastValidatorStatus
|
||||
this.updateFeature(run.featureId, {
|
||||
loopState: featureLoopState,
|
||||
lastValidatorStatus: featureLastValidatorStatus,
|
||||
});
|
||||
});
|
||||
|
||||
this.db.bumpLastModified();
|
||||
|
||||
// Re-read the run to get the updated state
|
||||
const updatedRun = this.getValidatorRun(runId)!;
|
||||
|
||||
this.emit("validator-run:completed", updatedRun, result, durationMs);
|
||||
|
||||
return updatedRun;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a validator run by ID.
|
||||
*
|
||||
* @param id - Validator run ID
|
||||
* @returns The validator run, or undefined if not found
|
||||
*/
|
||||
getValidatorRun(id: string): MissionValidatorRun | undefined {
|
||||
const row = this.db.prepare("SELECT * FROM mission_validator_runs WHERE id = ?").get(id);
|
||||
if (!row) return undefined;
|
||||
return this.rowToValidatorRun(row);
|
||||
}
|
||||
|
||||
// ── Contract Assertion Operations ─────────────────────────────────
|
||||
|
||||
/**
|
||||
@@ -2516,4 +2738,10 @@ export class MissionStore extends EventEmitter<MissionStoreEvents> {
|
||||
const random = Math.random().toString(36).substring(2, 6).toUpperCase();
|
||||
return `CA-${timestamp.toString(36).toUpperCase()}-${random}`;
|
||||
}
|
||||
|
||||
private generateValidatorRunId(): string {
|
||||
const timestamp = Date.now();
|
||||
const random = Math.random().toString(36).substring(2, 6).toUpperCase();
|
||||
return `VR-${timestamp.toString(36).toUpperCase()}-${random}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user