TaskSnapshot, JobSnapshot, the workflow journal record, and their entry envelopes are TypeBox schemas whose Static types are the persisted types. persistence.ts restores with Value.Check, so the hand-written validators (and their drift: validTask skipped step, validJob skipped timings and errors) are gone. A fast-check property holds persist -> restore equal over generated records.
188 lines
5.8 KiB
TypeScript
188 lines
5.8 KiB
TypeScript
/**
|
|
* Round-trip property for the persisted record schemas.
|
|
*
|
|
* The persisted shapes are TypeBox schemas; restore accepts exactly what
|
|
* `Value.Check` accepts. This property puts generated snapshots through
|
|
* persist → restore and requires the value to come back equal, so a schema
|
|
* that drifts from its type (or from what the manager writes) fails here.
|
|
*/
|
|
|
|
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import fc from "fast-check";
|
|
import {
|
|
extractJobSnapshots,
|
|
extractWorkflowJournals,
|
|
persistedJob,
|
|
persistedWorkflowJournal,
|
|
SUBAGENT_JOB_ENTRY,
|
|
WORKFLOW_RUN_ENTRY,
|
|
} from "../src/persistence.ts";
|
|
import type {
|
|
JobSnapshot,
|
|
TaskActivity,
|
|
TaskSnapshot,
|
|
UsageStats,
|
|
WorkflowJournal,
|
|
WorkflowJournalItem,
|
|
WorkflowJournalStep,
|
|
} from "../src/types.ts";
|
|
import type { WorkflowPlan, WorkflowStep } from "../src/workflow-plan.ts";
|
|
|
|
const identifier = fc.stringMatching(/^[A-Za-z_][A-Za-z0-9_-]{0,8}$/);
|
|
const text = fc.string({ maxLength: 40 });
|
|
const nonblank = fc.string({ maxLength: 40 }).filter((value) => /\S/.test(value));
|
|
const finite = fc.integer({ min: 0, max: 1_000_000 });
|
|
const optional = <T>(arb: fc.Arbitrary<T>): fc.Arbitrary<T | undefined> => fc.option(arb, { nil: undefined });
|
|
const status = fc.constantFrom("queued", "running", "succeeded", "failed", "skipped", "cancelled", "interrupted");
|
|
|
|
const usageArb: fc.Arbitrary<UsageStats> = fc.record({
|
|
input: finite,
|
|
output: finite,
|
|
cacheRead: finite,
|
|
cacheWrite: finite,
|
|
cost: finite,
|
|
contextTokens: finite,
|
|
turns: finite,
|
|
});
|
|
|
|
const activityArb: fc.Arbitrary<TaskActivity> = fc.record({
|
|
kind: fc.constantFrom("text", "toolCall", "toolResult"),
|
|
summary: text,
|
|
timestamp: finite,
|
|
isError: optional(fc.boolean()),
|
|
});
|
|
|
|
const taskArb: fc.Arbitrary<TaskSnapshot> = fc.record({
|
|
agent: text,
|
|
agentSource: fc.constantFrom("user", "project", "unknown"),
|
|
task: text,
|
|
cwd: text,
|
|
step: optional(fc.integer({ min: 1, max: 10 })),
|
|
stepId: optional(text),
|
|
item: optional(fc.integer({ min: 1, max: 10 })),
|
|
itemValue: optional(text),
|
|
status,
|
|
exitCode: fc.oneof(fc.integer(), fc.constant(null)),
|
|
output: text,
|
|
outputTruncated: fc.boolean(),
|
|
outputBytesOmitted: finite,
|
|
stderr: text,
|
|
stderrTruncated: fc.boolean(),
|
|
usage: usageArb,
|
|
model: optional(text),
|
|
stopReason: optional(text),
|
|
errorMessage: optional(text),
|
|
terminationSignal: optional(text),
|
|
activity: optional(fc.array(activityArb, { maxLength: 3 })),
|
|
startedAt: optional(finite),
|
|
finishedAt: optional(finite),
|
|
});
|
|
|
|
const jobArb: fc.Arbitrary<JobSnapshot> = fc.record({
|
|
id: text,
|
|
mode: fc.constantFrom("single", "parallel", "chain", "workflow"),
|
|
agentScope: fc.constantFrom("user", "project", "both"),
|
|
projectAgentsDir: fc.oneof(text, fc.constant(null)),
|
|
projectAgentsDirs: fc.array(text, { maxLength: 3 }),
|
|
status: fc.constantFrom("queued", "running", "cancelling", "succeeded", "failed", "cancelled", "interrupted"),
|
|
plannedTaskCount: fc.nat({ max: 100 }),
|
|
results: fc.array(taskArb, { maxLength: 3 }),
|
|
usage: usageArb,
|
|
createdAt: finite,
|
|
startedAt: optional(finite),
|
|
finishedAt: optional(finite),
|
|
errorMessage: optional(text),
|
|
timedOut: optional(fc.boolean()),
|
|
});
|
|
|
|
const stepArb: fc.Arbitrary<WorkflowStep> = fc.record({
|
|
id: identifier,
|
|
agent: nonblank,
|
|
task: nonblank,
|
|
needs: fc.array(identifier, { maxLength: 3 }),
|
|
each: optional(identifier),
|
|
model: optional(nonblank),
|
|
cwd: optional(nonblank),
|
|
});
|
|
|
|
const planArb: fc.Arbitrary<WorkflowPlan> = fc.record({
|
|
name: nonblank,
|
|
description: nonblank,
|
|
args: fc.array(identifier, { maxLength: 3 }),
|
|
steps: fc.array(stepArb, { maxLength: 3 }),
|
|
source: fc.constantFrom("user", "project", "inline"),
|
|
filePath: nonblank,
|
|
});
|
|
|
|
const argsArb = fc
|
|
.uniqueArray(fc.tuple(identifier, text), { maxLength: 3, selector: ([key]) => key })
|
|
.filter((entries) => entries.every(([key]) => key !== "__proto__"))
|
|
.map((entries) => Object.fromEntries(entries) as Record<string, string>);
|
|
|
|
const itemArb: fc.Arbitrary<WorkflowJournalItem> = fc.record({
|
|
item: fc.integer({ min: 1, max: 100 }),
|
|
input: text,
|
|
inputHash: text,
|
|
status,
|
|
attempts: fc.nat({ max: 5 }),
|
|
output: text,
|
|
outputTruncated: fc.boolean(),
|
|
outputBytesOmitted: finite,
|
|
startedAt: optional(finite),
|
|
finishedAt: optional(finite),
|
|
});
|
|
|
|
const journalStepArb: fc.Arbitrary<WorkflowJournalStep> = fc.record({
|
|
id: text,
|
|
inputHash: text,
|
|
status,
|
|
attempts: fc.nat({ max: 5 }),
|
|
output: text,
|
|
outputTruncated: fc.boolean(),
|
|
outputBytesOmitted: finite,
|
|
startedAt: optional(finite),
|
|
finishedAt: optional(finite),
|
|
items: fc.array(itemArb, { maxLength: 3 }),
|
|
});
|
|
|
|
const journalArb: fc.Arbitrary<WorkflowJournal> = fc.record({
|
|
jobId: text,
|
|
status: fc.constantFrom("queued", "running", "cancelling", "succeeded", "failed", "cancelled", "interrupted"),
|
|
plan: planArb,
|
|
args: argsArb,
|
|
steps: fc.array(journalStepArb, { maxLength: 3 }),
|
|
usage: usageArb,
|
|
createdAt: finite,
|
|
startedAt: optional(finite),
|
|
finishedAt: optional(finite),
|
|
errorMessage: optional(text),
|
|
});
|
|
|
|
/** What the session store does to a record: JSON drops absent optionals. */
|
|
function stored<T>(value: T): T {
|
|
return JSON.parse(JSON.stringify(value)) as T;
|
|
}
|
|
|
|
test("property: a persisted job snapshot round-trips through restore", () => {
|
|
fc.assert(
|
|
fc.property(jobArb, (generated) => {
|
|
const job = stored(generated);
|
|
const entry = { type: "custom", customType: SUBAGENT_JOB_ENTRY, data: persistedJob("session-a", job) };
|
|
assert.deepEqual(extractJobSnapshots([entry]), [job]);
|
|
}),
|
|
{ numRuns: 150 },
|
|
);
|
|
});
|
|
|
|
test("property: a persisted workflow journal round-trips through restore", () => {
|
|
fc.assert(
|
|
fc.property(journalArb, (generated) => {
|
|
const journal = stored(generated);
|
|
const entry = { type: "custom", customType: WORKFLOW_RUN_ENTRY, data: persistedWorkflowJournal("session-a", journal) };
|
|
assert.deepEqual(extractWorkflowJournals([entry]), [journal]);
|
|
}),
|
|
{ numRuns: 150 },
|
|
);
|
|
});
|