Hash whole plan steps under payload version 2

workflowStepInputHash and workflowItemInputHash feed the canonical
serializer the plan with provenance stripped, so every step field is
hashed by default and a new one cannot vanish from the replay contract.
The payload marker moves 1 -> 2 to separate hash eras; nothing replays
today (ADR 0004).
This commit is contained in:
alex committed 2026-12-01 11:19:33 +00:00
1 parent 1fcc6130c7
commit fa747145ce
2 files changed
+42 -24

No files matched your search

+17 -20
View File
@@ -18,7 +18,7 @@ import type {
WorkflowJournalItem, WorkflowJournalItem,
WorkflowJournalStep, WorkflowJournalStep,
} from "./types.ts"; } from "./types.ts";
import type { WorkflowPlan, WorkflowStep } from "./workflow-plan.ts"; import type { WorkflowPlan } from "./workflow-plan.ts";
import type { WorkflowRunSpec } from "./workflow-run.ts"; import type { WorkflowRunSpec } from "./workflow-run.ts";
/** /**
@@ -38,23 +38,20 @@ function canonical(value: unknown): string {
return JSON.stringify(value); return JSON.stringify(value);
} }
function planInputs(plan: WorkflowPlan): unknown { /**
// Provenance (source, filePath) is where the plan came from, not what it asks * Bumped when the hash payload changes shape, so a future replay layer can tell
// for; the same plan copied elsewhere keeps its step hashes. * which era a persisted hash came from. Version 2 hashed whole plan steps.
return { */
name: plan.name, export const WORKFLOW_HASH_PAYLOAD_VERSION = 2;
description: plan.description,
args: plan.args, /**
steps: plan.steps.map((step: WorkflowStep) => ({ * The plan as the replay contract sees it: steps whole, so a new step field is
id: step.id, * hashed by default. Provenance (source, filePath) is where the plan came from,
agent: step.agent, * not what it asks for; the same plan copied elsewhere keeps its step hashes.
task: step.task, */
needs: step.needs, function planHashInput(plan: WorkflowPlan): Omit<WorkflowPlan, "source" | "filePath"> {
each: step.each, const { source: _source, filePath: _filePath, ...inputs } = plan;
model: step.model, return inputs;
cwd: step.cwd,
})),
};
} }
/** /**
@@ -64,7 +61,7 @@ function planInputs(plan: WorkflowPlan): unknown {
* identical hashes across runs. * identical hashes across runs.
*/ */
export function workflowStepInputHash(plan: WorkflowPlan, stepId: string, args: Readonly<Record<string, string>>): string { export function workflowStepInputHash(plan: WorkflowPlan, stepId: string, args: Readonly<Record<string, string>>): string {
const payload = canonical({ version: 1, plan: planInputs(plan), step: stepId, args }); const payload = canonical({ version: WORKFLOW_HASH_PAYLOAD_VERSION, plan: planHashInput(plan), step: stepId, args });
return `sha256:${createHash("sha256").update(payload, "utf8").digest("hex")}`; return `sha256:${createHash("sha256").update(payload, "utf8").digest("hex")}`;
} }
@@ -80,7 +77,7 @@ export function workflowItemInputHash(
args: Readonly<Record<string, string>>, args: Readonly<Record<string, string>>,
item: string, item: string,
): string { ): string {
const payload = canonical({ version: 1, plan: planInputs(plan), step: stepId, item, args }); const payload = canonical({ version: WORKFLOW_HASH_PAYLOAD_VERSION, plan: planHashInput(plan), step: stepId, item, args });
return `sha256:${createHash("sha256").update(payload, "utf8").digest("hex")}`; return `sha256:${createHash("sha256").update(payload, "utf8").digest("hex")}`;
} }
+25 -4
View File
@@ -4,7 +4,7 @@ import * as os from "node:os";
import * as path from "node:path"; import * as path from "node:path";
import test, { after } from "node:test"; import test, { after } from "node:test";
import fc from "fast-check"; import fc from "fast-check";
import { buildWorkflowJournal, workflowItemInputHash, workflowStepInputHash } from "../src/journal.ts"; import { buildWorkflowJournal, WORKFLOW_HASH_PAYLOAD_VERSION, workflowItemInputHash, workflowStepInputHash } from "../src/journal.ts";
import { SubagentManager } from "../src/manager.ts"; import { SubagentManager } from "../src/manager.ts";
import { MAX_TASK_OUTPUT_BYTES, truncateUtf8Head } from "../src/output.ts"; import { MAX_TASK_OUTPUT_BYTES, truncateUtf8Head } from "../src/output.ts";
import { import {
@@ -16,7 +16,7 @@ import {
WORKFLOW_RUN_ENTRY, WORKFLOW_RUN_ENTRY,
} from "../src/persistence.ts"; } from "../src/persistence.ts";
import { prepareWorkflowInvocation, type PrepareContext } from "../src/prepare.ts"; import { prepareWorkflowInvocation, type PrepareContext } from "../src/prepare.ts";
import { parseWorkflow, type WorkflowPlan } from "../src/workflow-plan.ts"; import { parseWorkflow, type WorkflowPlan, type WorkflowSource } from "../src/workflow-plan.ts";
import { emptyUsage, type JobSnapshot, type TaskSnapshot, type TaskStatus, type WorkflowJournal, type WorkflowJournalItem, type WorkflowJournalStep } from "../src/types.ts"; import { emptyUsage, type JobSnapshot, type TaskSnapshot, type TaskStatus, type WorkflowJournal, type WorkflowJournalItem, type WorkflowJournalStep } from "../src/types.ts";
import { FakeRunner } from "./helpers.ts"; import { FakeRunner } from "./helpers.ts";
@@ -49,9 +49,16 @@ function makeWorkspace(agentNames = ["scout"]): { cwd: string; ctx: PrepareConte
}; };
} }
function makePlan(steps: Array<Record<string, unknown>>, args: string[] = []): WorkflowPlan { function makePlan(
steps: Array<Record<string, unknown>>,
args: string[] = [],
provenance: { source?: WorkflowSource; filePath?: string } = {},
): WorkflowPlan {
const content = `---\n${JSON.stringify({ name: "test-workflow", description: "Test workflow", args, steps }, null, 2)}\n---\n\nNotes.\n`; const content = `---\n${JSON.stringify({ name: "test-workflow", description: "Test workflow", args, steps }, null, 2)}\n---\n\nNotes.\n`;
return parseWorkflow(content, { source: "user", filePath: "/tmp/test-workflow.md" }); return parseWorkflow(content, {
source: provenance.source ?? "user",
filePath: provenance.filePath ?? "/tmp/test-workflow.md",
});
} }
function pending(runner: FakeRunner, stepId: string) { function pending(runner: FakeRunner, stepId: string) {
@@ -239,6 +246,20 @@ test("step input hashes are stable across identical inputs and change with args
workflowStepInputHash(first, "scan", { target: "src" }), workflowStepInputHash(first, "scan", { target: "src" }),
); );
assert.notEqual(workflowStepInputHash(first, "ghost", { target: "src" }), workflowStepInputHash(first, "scan", { target: "src" })); assert.notEqual(workflowStepInputHash(first, "ghost", { target: "src" }), workflowStepInputHash(first, "scan", { target: "src" }));
// Whole steps are hashed: a step field beyond the brief still moves the hash.
const withCwd = makePlan([{ id: "scan", agent: "scout", task: "scan {args.target}", cwd: "src" }], ["target"]);
assert.notEqual(workflowStepInputHash(withCwd, "scan", { target: "src" }), workflowStepInputHash(first, "scan", { target: "src" }));
// Provenance is not an input: the same plan copied elsewhere hashes alike.
const elsewhere = makePlan(steps, ["target"], { source: "project", filePath: "/repo/.pi/workflows/test-workflow.md" });
assert.equal(workflowStepInputHash(elsewhere, "scan", { target: "src" }), workflowStepInputHash(first, "scan", { target: "src" }));
});
test("the input-hash payload carries the replay-contract version", () => {
// Nothing replays today (ADR 0004 journal-only); the marker only separates
// hash eras, so moving it is a deliberate migration, never a refactor.
assert.equal(WORKFLOW_HASH_PAYLOAD_VERSION, 2);
}); });
test("journals hash the step inputs, not the outputs, so identical runs agree", async () => { test("journals hash the step inputs, not the outputs, so identical runs agree", async () => {