Project the workflow journal from run state
The journal rebuilt the step/item graph from the flat job record: rows were grouped by stepId, an each-step's coordinator row was picked out of the collision with its item children, and a positional fallback covered rows without step ids. After the seam (#22) the supervisor owns the DAG index, so the record is a projection of that state instead. - WorkflowSupervisor.journal(execution) projects the record from its stepTasks/itemTasks index and the run spec while the run is live. - journal.ts keeps the pure row builders, now over the shared InternalTask record, and builds the record from plan order plus the index; itemsByStep, the byStep collision handling, and the positional fallback are gone. - The manager calls the run's projection at finalize and passes it to onWorkflowJournal; interrupted runs project at shutdown as before. - jobUsage() moves the usage summation into job-state.ts so the snapshot and the journal read one number. Journal tests that hand-built flat snapshots to exercise the reverse-engineering are replaced by run-state-driven ones; a cancelled run's journal is now covered too. Part of #23.
This commit is contained in:
1 parent
a3fc182cb5
commit
8bc08bd08a
5 files changed
+211
-233
No files matched your search
+119
-166
@@ -4,7 +4,8 @@ import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import test, { after } from "node:test";
|
||||
import fc from "fast-check";
|
||||
import { buildWorkflowJournal, workflowItemInputHash, workflowStepInputHash } from "../src/journal.ts";
|
||||
import { buildWorkflowJournal, workflowItemInputHash, workflowStepInputHash, type WorkflowJournalRun } from "../src/journal.ts";
|
||||
import type { InternalTask } from "../src/job-state.ts";
|
||||
import { SubagentManager } from "../src/manager.ts";
|
||||
import { MAX_TASK_OUTPUT_BYTES, truncateUtf8Head } from "../src/output.ts";
|
||||
import {
|
||||
@@ -74,6 +75,35 @@ function serialized(entry: unknown): unknown {
|
||||
return JSON.parse(JSON.stringify(entry));
|
||||
}
|
||||
|
||||
/** One row of a run's DAG index: the shared record the journal projects. */
|
||||
function taskRow(stepId: string, status: TaskStatus, output = "", extra: Partial<TaskSnapshot> = {}): InternalTask {
|
||||
return {
|
||||
snapshot: {
|
||||
agent: "scout",
|
||||
agentSource: "user",
|
||||
task: "check",
|
||||
cwd: "/tmp",
|
||||
stepId,
|
||||
status,
|
||||
exitCode: status === "succeeded" ? 0 : 1,
|
||||
output,
|
||||
outputTruncated: false,
|
||||
outputBytesOmitted: 0,
|
||||
stderr: "",
|
||||
stderrTruncated: false,
|
||||
usage: emptyUsage(),
|
||||
startedAt: 2,
|
||||
finishedAt: 3,
|
||||
...extra,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** The job record's head as the journal carries it, plus the rows its usage sums. */
|
||||
function jobHead(rows: InternalTask[], overrides: Partial<WorkflowJournalRun["execution"]> = {}): WorkflowJournalRun["execution"] {
|
||||
return { id: "job_0009", status: "succeeded", tasks: rows, createdAt: 1, startedAt: 2, finishedAt: 3, ...overrides };
|
||||
}
|
||||
|
||||
test("a finished run persists one journal entry with per-step records and usage", async () => {
|
||||
const { ctx } = makeWorkspace();
|
||||
const plan = makePlan(
|
||||
@@ -172,38 +202,13 @@ test("oversized step outputs are bounded to the async snapshot budget with the t
|
||||
// The same bound an async task snapshot applies to a child's output.
|
||||
const snapshotBound = truncateUtf8Head(oversized, MAX_TASK_OUTPUT_BYTES);
|
||||
|
||||
const job: JobSnapshot = {
|
||||
id: "job_0009",
|
||||
mode: "workflow",
|
||||
agentScope: "user",
|
||||
projectAgentsDir: null,
|
||||
projectAgentsDirs: [],
|
||||
status: "succeeded",
|
||||
plannedTaskCount: 1,
|
||||
usage: emptyUsage(),
|
||||
createdAt: 1,
|
||||
startedAt: 2,
|
||||
finishedAt: 3,
|
||||
results: [{
|
||||
agent: "scout",
|
||||
agentSource: "user",
|
||||
task: "scan",
|
||||
cwd: ctx.cwd,
|
||||
stepId: "scan",
|
||||
status: "succeeded",
|
||||
exitCode: 0,
|
||||
output: oversized,
|
||||
outputTruncated: false,
|
||||
outputBytesOmitted: 0,
|
||||
stderr: "",
|
||||
stderrTruncated: false,
|
||||
usage: emptyUsage(),
|
||||
startedAt: 2,
|
||||
finishedAt: 3,
|
||||
}],
|
||||
};
|
||||
|
||||
const journal = buildWorkflowJournal(job, prepared.workflow);
|
||||
const scan = taskRow("scan", "succeeded", oversized);
|
||||
const journal = buildWorkflowJournal({
|
||||
execution: jobHead([scan]),
|
||||
spec: prepared.workflow,
|
||||
stepTasks: new Map([["scan", scan]]),
|
||||
itemTasks: new Map(),
|
||||
});
|
||||
const record = journal.steps[0];
|
||||
const [kept, note] = record.output.split("\n\n[Output truncated: ");
|
||||
assert.equal(kept, snapshotBound.text);
|
||||
@@ -212,12 +217,18 @@ test("oversized step outputs are bounded to the async snapshot budget with the t
|
||||
assert.equal(record.outputTruncated, true);
|
||||
assert.equal(record.outputBytesOmitted, Buffer.byteLength(oversized, "utf8") - MAX_TASK_OUTPUT_BYTES);
|
||||
|
||||
// An already-bounded snapshot keeps its own omission count instead of
|
||||
// re-marking the text.
|
||||
const bounded = buildWorkflowJournal(
|
||||
{ ...job, results: [{ ...job.results[0], output: snapshotBound.text, outputTruncated: true, outputBytesOmitted: snapshotBound.bytesOmitted }] },
|
||||
prepared.workflow,
|
||||
);
|
||||
// A snapshot the manager already bounded keeps its own omission count
|
||||
// instead of re-marking the text.
|
||||
const boundedRow = taskRow("scan", "succeeded", snapshotBound.text, {
|
||||
outputTruncated: true,
|
||||
outputBytesOmitted: snapshotBound.bytesOmitted,
|
||||
});
|
||||
const bounded = buildWorkflowJournal({
|
||||
execution: jobHead([boundedRow]),
|
||||
spec: prepared.workflow,
|
||||
stepTasks: new Map([["scan", boundedRow]]),
|
||||
itemTasks: new Map(),
|
||||
});
|
||||
assert.equal(bounded.steps[0].output.split("[Output truncated: ").length, 2);
|
||||
assert.equal(bounded.steps[0].outputBytesOmitted, snapshotBound.bytesOmitted);
|
||||
|
||||
@@ -277,37 +288,13 @@ test("journal extraction ignores foreign and malformed entries and keeps the lat
|
||||
const { ctx } = makeWorkspace();
|
||||
const plan = makePlan([{ id: "scan", agent: "scout", task: "scan" }]);
|
||||
const prepared = await prepareWorkflowInvocation(plan, {}, ctx, { agentScope: "project", confirmProjectAgents: false });
|
||||
const job: JobSnapshot = {
|
||||
id: "job_0010",
|
||||
mode: "workflow",
|
||||
agentScope: "user",
|
||||
projectAgentsDir: null,
|
||||
projectAgentsDirs: [],
|
||||
status: "failed",
|
||||
plannedTaskCount: 1,
|
||||
usage: emptyUsage(),
|
||||
createdAt: 1,
|
||||
finishedAt: 2,
|
||||
results: [{
|
||||
agent: "scout",
|
||||
agentSource: "user",
|
||||
task: "scan",
|
||||
cwd: ctx.cwd,
|
||||
stepId: "scan",
|
||||
status: "failed",
|
||||
exitCode: 1,
|
||||
output: "",
|
||||
outputTruncated: false,
|
||||
outputBytesOmitted: 0,
|
||||
stderr: "",
|
||||
stderrTruncated: false,
|
||||
usage: emptyUsage(),
|
||||
errorMessage: "boom",
|
||||
startedAt: 1,
|
||||
finishedAt: 2,
|
||||
}],
|
||||
};
|
||||
const journal = buildWorkflowJournal(job, prepared.workflow);
|
||||
const scan = taskRow("scan", "failed", "", { errorMessage: "boom" });
|
||||
const journal = buildWorkflowJournal({
|
||||
execution: jobHead([scan], { id: "job_0010", status: "failed", startedAt: undefined, finishedAt: 2 }),
|
||||
spec: prepared.workflow,
|
||||
stepTasks: new Map([["scan", scan]]),
|
||||
itemTasks: new Map(),
|
||||
});
|
||||
const entry = serialized({ type: "custom", customType: WORKFLOW_RUN_ENTRY, data: persistedWorkflowJournal("session-a", journal) });
|
||||
const later = serialized({
|
||||
type: "custom",
|
||||
@@ -358,6 +345,32 @@ test("a failed run journals skipped dependents and retains the independent branc
|
||||
assert.equal(journals[0].steps[2].output, "");
|
||||
});
|
||||
|
||||
test("a cancelled run journals once, with every unfinished step settled cancelled", async () => {
|
||||
const { ctx } = makeWorkspace();
|
||||
const plan = makePlan([
|
||||
{ id: "scan", agent: "scout", task: "scan" },
|
||||
{ id: "audit", agent: "scout", task: "audit {steps.scan.output}", needs: ["scan"] },
|
||||
]);
|
||||
const runner = new FakeRunner();
|
||||
const journals: WorkflowJournal[] = [];
|
||||
const manager = new SubagentManager(runner, { onWorkflowJournal: (journal) => journals.push(journal) });
|
||||
const started = manager.startWorkflow(
|
||||
await prepareWorkflowInvocation(plan, {}, ctx, { agentScope: "project", confirmProjectAgents: false }),
|
||||
);
|
||||
|
||||
manager.cancel(started.id);
|
||||
assert.equal((await manager.wait(started.id, 1)).status, "cancelled");
|
||||
assert.equal(journals.length, 1);
|
||||
assert.equal(journals[0].status, "cancelled");
|
||||
assert.deepEqual(
|
||||
journals[0].steps.map((step) => [step.id, step.status, step.attempts]),
|
||||
[
|
||||
["scan", "cancelled", 1],
|
||||
["audit", "cancelled", 0],
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test("an each-step journals per-item rows alongside its aggregate step record", async () => {
|
||||
const { ctx } = makeWorkspace();
|
||||
const plan = makePlan([
|
||||
@@ -365,48 +378,21 @@ test("an each-step journals per-item rows alongside its aggregate step record",
|
||||
{ id: "check", agent: "scout", task: "check {item}", needs: ["list"], each: "{steps.list.output}" },
|
||||
]);
|
||||
const prepared = await prepareWorkflowInvocation(plan, {}, ctx, { agentScope: "project", confirmProjectAgents: false });
|
||||
const snapshot = (stepId: string, status: TaskStatus, output: string, item?: number, itemValue?: string): TaskSnapshot => ({
|
||||
agent: "scout",
|
||||
agentSource: "user",
|
||||
task: "check",
|
||||
cwd: ctx.cwd,
|
||||
stepId,
|
||||
item,
|
||||
itemValue,
|
||||
status,
|
||||
exitCode: status === "succeeded" ? 0 : 1,
|
||||
output,
|
||||
outputTruncated: false,
|
||||
outputBytesOmitted: 0,
|
||||
stderr: "",
|
||||
stderrTruncated: false,
|
||||
usage: emptyUsage(),
|
||||
startedAt: 2,
|
||||
finishedAt: 3,
|
||||
});
|
||||
const job: JobSnapshot = {
|
||||
id: "job_0011",
|
||||
mode: "workflow",
|
||||
agentScope: "user",
|
||||
projectAgentsDir: null,
|
||||
projectAgentsDirs: [],
|
||||
status: "succeeded",
|
||||
plannedTaskCount: 2,
|
||||
usage: emptyUsage(),
|
||||
createdAt: 1,
|
||||
startedAt: 2,
|
||||
finishedAt: 3,
|
||||
results: [
|
||||
snapshot("list", "succeeded", "src/a.ts\nsrc/b.ts"),
|
||||
// The step's own row carries the joined item output and the aggregate
|
||||
// status; item children trail it in `results`.
|
||||
snapshot("check", "succeeded", "aggregate"),
|
||||
snapshot("check", "succeeded", "ok a", 1, "src/a.ts"),
|
||||
snapshot("check", "failed", "boom b", 2, "src/b.ts"),
|
||||
],
|
||||
};
|
||||
const list = taskRow("list", "succeeded", "src/a.ts\nsrc/b.ts");
|
||||
// The coordinator row carries the joined item output and the aggregate
|
||||
// status; the item children sit in the index beside it.
|
||||
const check = taskRow("check", "succeeded", "aggregate");
|
||||
const items = [
|
||||
taskRow("check", "succeeded", "ok a", { item: 1, itemValue: "src/a.ts" }),
|
||||
taskRow("check", "failed", "boom b", { item: 2, itemValue: "src/b.ts" }),
|
||||
];
|
||||
|
||||
const journal = buildWorkflowJournal(job, prepared.workflow);
|
||||
const journal = buildWorkflowJournal({
|
||||
execution: jobHead([list, check, ...items], { id: "job_0011" }),
|
||||
spec: prepared.workflow,
|
||||
stepTasks: new Map([["list", list], ["check", check]]),
|
||||
itemTasks: new Map([["check", items]]),
|
||||
});
|
||||
assert.equal(journal.steps.length, 2, "one journal record per plan step");
|
||||
assert.deepEqual(
|
||||
journal.steps.map((step) => [step.id, step.status, step.output]),
|
||||
@@ -633,69 +619,36 @@ test("oversized item outputs are bounded like step outputs and survive the round
|
||||
const prepared = await prepareWorkflowInvocation(plan, {}, ctx, { agentScope: "project", confirmProjectAgents: false });
|
||||
const oversized = "é".repeat(60 * 1024);
|
||||
const snapshotBound = truncateUtf8Head(oversized, MAX_TASK_OUTPUT_BYTES);
|
||||
const row = (
|
||||
stepId: string,
|
||||
status: TaskStatus,
|
||||
output: string,
|
||||
extra: Partial<TaskSnapshot> = {},
|
||||
): TaskSnapshot => ({
|
||||
agent: "scout",
|
||||
agentSource: "user",
|
||||
task: "check",
|
||||
cwd: ctx.cwd,
|
||||
stepId,
|
||||
status,
|
||||
exitCode: status === "succeeded" ? 0 : 1,
|
||||
output,
|
||||
outputTruncated: false,
|
||||
outputBytesOmitted: 0,
|
||||
stderr: "",
|
||||
stderrTruncated: false,
|
||||
usage: emptyUsage(),
|
||||
startedAt: 2,
|
||||
finishedAt: 3,
|
||||
...extra,
|
||||
const list = taskRow("list", "succeeded", "src/a.ts\nsrc/b.ts");
|
||||
const check = taskRow("check", "succeeded", "aggregate");
|
||||
const first = taskRow("check", "succeeded", oversized, { item: 1, itemValue: "src/a.ts" });
|
||||
const second = taskRow("check", "succeeded", snapshotBound.text, {
|
||||
item: 2,
|
||||
itemValue: "src/b.ts",
|
||||
outputTruncated: true,
|
||||
outputBytesOmitted: snapshotBound.bytesOmitted,
|
||||
});
|
||||
|
||||
const job: JobSnapshot = {
|
||||
id: "job_0013",
|
||||
mode: "workflow",
|
||||
agentScope: "user",
|
||||
projectAgentsDir: null,
|
||||
projectAgentsDirs: [],
|
||||
status: "succeeded",
|
||||
plannedTaskCount: 3,
|
||||
usage: emptyUsage(),
|
||||
createdAt: 1,
|
||||
startedAt: 2,
|
||||
finishedAt: 3,
|
||||
results: [
|
||||
row("list", "succeeded", "src/a.ts\nsrc/b.ts"),
|
||||
row("check", "succeeded", "aggregate"),
|
||||
row("check", "succeeded", oversized, { item: 1, itemValue: "src/a.ts" }),
|
||||
row("check", "succeeded", snapshotBound.text, {
|
||||
item: 2,
|
||||
itemValue: "src/b.ts",
|
||||
outputTruncated: true,
|
||||
outputBytesOmitted: snapshotBound.bytesOmitted,
|
||||
}),
|
||||
],
|
||||
};
|
||||
|
||||
const journal = buildWorkflowJournal(job, prepared.workflow);
|
||||
const [first, second] = journal.steps[1].items;
|
||||
const [kept, note] = first.output.split("\n\n[Output truncated: ");
|
||||
const journal = buildWorkflowJournal({
|
||||
execution: jobHead([list, check, first, second], { id: "job_0013" }),
|
||||
spec: prepared.workflow,
|
||||
stepTasks: new Map([["list", list], ["check", check]]),
|
||||
itemTasks: new Map([["check", [first, second]]]),
|
||||
});
|
||||
const [boundedFirst, boundedSecond] = journal.steps[1].items;
|
||||
const [kept, note] = boundedFirst.output.split("\n\n[Output truncated: ");
|
||||
assert.equal(kept, snapshotBound.text);
|
||||
assert.equal(note, `${snapshotBound.bytesOmitted} bytes omitted]`);
|
||||
assert.equal(Buffer.byteLength(kept, "utf8"), MAX_TASK_OUTPUT_BYTES);
|
||||
assert.equal(first.outputTruncated, true);
|
||||
assert.equal(first.outputBytesOmitted, Buffer.byteLength(oversized, "utf8") - MAX_TASK_OUTPUT_BYTES);
|
||||
assert.equal(boundedFirst.outputTruncated, true);
|
||||
assert.equal(boundedFirst.outputBytesOmitted, Buffer.byteLength(oversized, "utf8") - MAX_TASK_OUTPUT_BYTES);
|
||||
|
||||
// An already-bounded item snapshot keeps its own omission count instead of
|
||||
// An already-bounded item row keeps its own omission count instead of
|
||||
// re-marking the text, exactly like a step row.
|
||||
assert.equal(second.output.split("[Output truncated: ").length, 2);
|
||||
assert.equal(second.outputBytesOmitted, snapshotBound.bytesOmitted);
|
||||
assert.equal(boundedSecond.output.split("[Output truncated: ").length, 2);
|
||||
assert.equal(boundedSecond.outputBytesOmitted, snapshotBound.bytesOmitted);
|
||||
|
||||
const entry = serialized({ type: "custom", customType: WORKFLOW_RUN_ENTRY, data: persistedWorkflowJournal("session-a", journal) });
|
||||
assert.deepEqual(extractWorkflowJournals([entry]), [serialized(journal)]);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user