From fa747145ceaf5483a887f326667f5bf331980ea8 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 1 Dec 2026 11:19:33 +0000 Subject: [PATCH] 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). --- src/journal.ts | 37 ++++++++++++++++------------------- test/workflow-journal.test.ts | 29 +++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/journal.ts b/src/journal.ts index 13b4da8..ff2d9c2 100644 --- a/src/journal.ts +++ b/src/journal.ts @@ -18,7 +18,7 @@ import type { WorkflowJournalItem, WorkflowJournalStep, } 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"; /** @@ -38,23 +38,20 @@ function canonical(value: unknown): string { return JSON.stringify(value); } -function planInputs(plan: WorkflowPlan): unknown { - // Provenance (source, filePath) is where the plan came from, not what it asks - // for; the same plan copied elsewhere keeps its step hashes. - return { - name: plan.name, - description: plan.description, - args: plan.args, - steps: plan.steps.map((step: WorkflowStep) => ({ - id: step.id, - agent: step.agent, - task: step.task, - needs: step.needs, - each: step.each, - model: step.model, - cwd: step.cwd, - })), - }; +/** + * Bumped when the hash payload changes shape, so a future replay layer can tell + * which era a persisted hash came from. Version 2 hashed whole plan steps. + */ +export const WORKFLOW_HASH_PAYLOAD_VERSION = 2; + +/** + * The plan as the replay contract sees it: steps whole, so a new step field is + * hashed by default. Provenance (source, filePath) is where the plan came from, + * not what it asks for; the same plan copied elsewhere keeps its step hashes. + */ +function planHashInput(plan: WorkflowPlan): Omit { + const { source: _source, filePath: _filePath, ...inputs } = plan; + return inputs; } /** @@ -64,7 +61,7 @@ function planInputs(plan: WorkflowPlan): unknown { * identical hashes across runs. */ export function workflowStepInputHash(plan: WorkflowPlan, stepId: string, args: Readonly>): 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")}`; } @@ -80,7 +77,7 @@ export function workflowItemInputHash( args: Readonly>, item: 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")}`; } diff --git a/test/workflow-journal.test.ts b/test/workflow-journal.test.ts index f7ea027..3da5724 100644 --- a/test/workflow-journal.test.ts +++ b/test/workflow-journal.test.ts @@ -4,7 +4,7 @@ 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, WORKFLOW_HASH_PAYLOAD_VERSION, workflowItemInputHash, workflowStepInputHash } from "../src/journal.ts"; import { SubagentManager } from "../src/manager.ts"; import { MAX_TASK_OUTPUT_BYTES, truncateUtf8Head } from "../src/output.ts"; import { @@ -16,7 +16,7 @@ import { WORKFLOW_RUN_ENTRY, } from "../src/persistence.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 { FakeRunner } from "./helpers.ts"; @@ -49,9 +49,16 @@ function makeWorkspace(agentNames = ["scout"]): { cwd: string; ctx: PrepareConte }; } -function makePlan(steps: Array>, args: string[] = []): WorkflowPlan { +function makePlan( + steps: Array>, + 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`; - 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) { @@ -239,6 +246,20 @@ test("step input hashes are stable across identical inputs and change with args 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 () => {