@kontourai/flow-agents 3.6.0 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/build/src/builder-flow-run-adapter.d.ts +22 -2
- package/build/src/builder-flow-run-adapter.js +93 -28
- package/build/src/builder-flow-runtime.d.ts +8 -3
- package/build/src/builder-flow-runtime.js +308 -47
- package/build/src/cli/assignment-provider.d.ts +4 -7
- package/build/src/cli/assignment-provider.js +67 -13
- package/build/src/cli/builder-run.js +14 -18
- package/build/src/cli/workflow-sidecar.d.ts +28 -4
- package/build/src/cli/workflow-sidecar.js +801 -97
- package/build/src/cli/workflow.js +290 -42
- package/build/src/flow-kit/validate.d.ts +17 -0
- package/build/src/flow-kit/validate.js +340 -2
- package/build/src/index.js +5 -5
- package/build/src/lib/observed-command.d.ts +7 -0
- package/build/src/lib/observed-command.js +100 -0
- package/build/src/tools/generate-context-map.js +5 -3
- package/context/scripts/hooks/lib/kit-catalog.js +1 -1
- package/context/scripts/hooks/stop-goal-fit.js +78 -9
- package/docs/context-map.md +22 -20
- package/docs/developer-architecture.md +1 -1
- package/docs/public-workflow-cli.md +76 -7
- package/docs/skills-map.md +51 -27
- package/docs/spec/builder-flow-runtime.md +41 -40
- package/docs/workflow-usage-guide.md +109 -42
- package/evals/fixtures/hook-influence/cases.json +2 -2
- package/evals/integration/test_builder_entry_enforcement.sh +52 -41
- package/evals/integration/test_builder_step_producers.sh +297 -6
- package/evals/integration/test_bundle_install.sh +212 -63
- package/evals/integration/test_critique_supersession_roundtrip.sh +21 -8
- package/evals/integration/test_current_json_per_actor.sh +12 -0
- package/evals/integration/test_dual_emit_flow_step.sh +62 -43
- package/evals/integration/test_flowdef_session_activation.sh +145 -25
- package/evals/integration/test_flowdef_session_history_preservation.sh +23 -21
- package/evals/integration/test_gate_lockdown.sh +3 -5
- package/evals/integration/test_goal_fit_hook.sh +60 -2
- package/evals/integration/test_liveness_verdict.sh +14 -17
- package/evals/integration/test_phase_map_and_gate_claim.sh +63 -38
- package/evals/integration/test_public_workflow_cli.sh +325 -11
- package/evals/integration/test_pull_work_liveness_preflight.sh +22 -66
- package/evals/integration/test_sidecar_field_preservation.sh +36 -11
- package/evals/integration/test_workflow_sidecar_writer.sh +277 -44
- package/evals/integration/test_workflow_steering_hook.sh +15 -38
- package/evals/run.sh +2 -0
- package/evals/static/test_builder_skill_coherence.sh +247 -0
- package/evals/static/test_library_exports.sh +5 -2
- package/evals/static/test_workflow_skills.sh +13 -325
- package/kits/builder/flows/build.flow.json +22 -0
- package/kits/builder/flows/shape.flow.json +9 -9
- package/kits/builder/kit.json +70 -16
- package/kits/builder/skills/builder-shape/SKILL.md +75 -75
- package/kits/builder/skills/continue-work/SKILL.md +45 -106
- package/kits/builder/skills/deliver/SKILL.md +96 -442
- package/kits/builder/skills/design-probe/SKILL.md +40 -139
- package/kits/builder/skills/evidence-gate/SKILL.md +59 -201
- package/kits/builder/skills/execute-plan/SKILL.md +54 -125
- package/kits/builder/skills/fix-bug/SKILL.md +42 -132
- package/kits/builder/skills/gate-review/SKILL.md +60 -211
- package/kits/builder/skills/idea-to-backlog/SKILL.md +63 -244
- package/kits/builder/skills/learning-review/SKILL.md +63 -170
- package/kits/builder/skills/pickup-probe/SKILL.md +54 -111
- package/kits/builder/skills/plan-work/SKILL.md +51 -185
- package/kits/builder/skills/pull-work/SKILL.md +136 -485
- package/kits/builder/skills/release-readiness/SKILL.md +66 -107
- package/kits/builder/skills/review-work/SKILL.md +89 -176
- package/kits/builder/skills/tdd-workflow/SKILL.md +53 -147
- package/kits/builder/skills/verify-work/SKILL.md +101 -113
- package/package.json +2 -2
- package/scripts/hooks/lib/kit-catalog.js +1 -1
- package/scripts/hooks/stop-goal-fit.js +78 -9
- package/src/builder-flow-run-adapter.ts +118 -32
- package/src/builder-flow-runtime.ts +331 -61
- package/src/cli/assignment-provider-lock.test.mjs +83 -0
- package/src/cli/assignment-provider.ts +62 -13
- package/src/cli/builder-flow-run-adapter.test.mjs +4 -2
- package/src/cli/builder-flow-runtime.test.mjs +194 -8
- package/src/cli/builder-run.ts +3 -9
- package/src/cli/kit-metadata-security.test.mjs +232 -6
- package/src/cli/public-api.test.mjs +15 -0
- package/src/cli/workflow-sidecar-execution-proof.test.mjs +90 -0
- package/src/cli/workflow-sidecar.ts +724 -97
- package/src/cli/workflow.ts +277 -40
- package/src/flow-kit/validate.ts +320 -2
- package/src/index.ts +6 -5
- package/src/lib/observed-command.ts +96 -0
- package/src/tools/generate-context-map.ts +5 -3
package/src/cli/workflow.ts
CHANGED
|
@@ -3,13 +3,17 @@ import * as path from "node:path";
|
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import { isDeepStrictEqual } from "node:util";
|
|
6
|
-
import {
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { validateDefinition } from "@kontourai/flow";
|
|
8
|
+
import { loadBuilderFlowRun } from "../builder-flow-run-adapter.js";
|
|
9
|
+
import { inspectBuilderFlowSession, recoverBuilderFlowSession, syncBuilderFlowSession } from "../builder-flow-runtime.js";
|
|
7
10
|
import { flowAgentsPackageRoot, flowAgentsPackageVersion } from "../lib/package-version.js";
|
|
8
11
|
import { pinnedFlowAgentsCommand } from "../lib/pinned-cli-command.js";
|
|
9
12
|
import { defaultArtifactRootForRead, flowAgentsArtifactRoot } from "../lib/local-artifact-root.js";
|
|
10
|
-
import { flagBool, flagString, parseArgs } from "../lib/args.js";
|
|
13
|
+
import { flagBool, flagList, flagString, parseArgs } from "../lib/args.js";
|
|
11
14
|
import { main as builderRun } from "./builder-run.js";
|
|
12
|
-
import { currentWorkflowSessionDir,
|
|
15
|
+
import { currentWorkflowSessionDir, isMeaningfulTestCommand, mainFromPublicWorkflow, WORKFLOW_WRITER_CONTRACT_VERSION } from "./workflow-sidecar.js";
|
|
16
|
+
import { resolveCurrentAssignmentActor, withSubjectLock } from "./assignment-provider.js";
|
|
13
17
|
|
|
14
18
|
type JsonRecord = Record<string, unknown>;
|
|
15
19
|
|
|
@@ -18,7 +22,7 @@ const PACKAGE_ROOT = flowAgentsPackageRoot();
|
|
|
18
22
|
const REQUIRE = createRequire(import.meta.url);
|
|
19
23
|
const PACKAGE_METADATA = readJsonFile(path.join(PACKAGE_ROOT, "package.json"), "Flow Agents package metadata");
|
|
20
24
|
const CLI_VERSION = flowAgentsPackageVersion();
|
|
21
|
-
const PUBLIC_VERBS = ["start", "status", "evidence", "pause", "resume", "release", "cancel", "archive", "doctor"] as const;
|
|
25
|
+
const PUBLIC_VERBS = ["start", "status", "evidence", "critique", "pause", "resume", "release", "cancel", "archive", "doctor"] as const;
|
|
22
26
|
|
|
23
27
|
function usage(): void {
|
|
24
28
|
console.log(`Usage: flow-agents workflow <verb> [options]
|
|
@@ -27,6 +31,7 @@ Public workflow verbs:
|
|
|
27
31
|
start Start or resume a workflow for a Work Item.
|
|
28
32
|
status Show the current canonical run and projected next action.
|
|
29
33
|
evidence Record evidence for the current Flow gate and synchronize it.
|
|
34
|
+
critique Record review critique directly into the current trust bundle.
|
|
30
35
|
pause Pause the current run as its assignment actor.
|
|
31
36
|
resume Resume the current paused run as its assignment actor.
|
|
32
37
|
release Release the current assignment without canceling the run.
|
|
@@ -55,6 +60,7 @@ export async function main(argv: string[]): Promise<number> {
|
|
|
55
60
|
const sessionDir = resolveSessionDir(parsed.flags);
|
|
56
61
|
if (verb === "status") return status(sessionDir, flagBool(parsed.flags, "json"));
|
|
57
62
|
if (verb === "evidence") return evidence(sessionDir, argv.slice(1), flagBool(parsed.flags, "json"));
|
|
63
|
+
if (verb === "critique") return critique(sessionDir, argv.slice(1), flagBool(parsed.flags, "json"));
|
|
58
64
|
|
|
59
65
|
const forwarded = stripPublicFlags(argv.slice(1), new Set(["artifact-root", "session-dir", "json"]));
|
|
60
66
|
if (verb === "release" && !flagString(parsed.flags, "reason")) throw new Error("workflow release requires --reason <text>");
|
|
@@ -63,14 +69,30 @@ export async function main(argv: string[]): Promise<number> {
|
|
|
63
69
|
|
|
64
70
|
async function start(argv: string[]): Promise<number> {
|
|
65
71
|
const parsed = parseArgs(argv);
|
|
66
|
-
assertOnlyFlags(parsed.flags, new Set(["flow", "work-item", "task-slug", "artifact-root", "source-request", "summary", "title", "criterion"]), "workflow start");
|
|
72
|
+
assertOnlyFlags(parsed.flags, new Set(["flow", "work-item", "task-slug", "artifact-root", "source-request", "summary", "title", "criterion", "assignment-provider", "effective-state-json"]), "workflow start");
|
|
67
73
|
const flow = flagString(parsed.flags, "flow", "builder.build")!;
|
|
68
|
-
if (flow !== "builder.build") throw new Error("workflow start
|
|
74
|
+
if (flow !== "builder.build" && flow !== "builder.shape") throw new Error("workflow start supports only --flow builder.build or builder.shape");
|
|
69
75
|
const workItem = flagString(parsed.flags, "work-item");
|
|
70
|
-
if (!workItem) throw new Error("workflow start requires --work-item <owner/repo#id>");
|
|
71
76
|
const artifactRoot = path.resolve(flagString(parsed.flags, "artifact-root", flowAgentsArtifactRoot())!);
|
|
72
77
|
const taskSlug = flagString(parsed.flags, "task-slug");
|
|
73
|
-
if (
|
|
78
|
+
if (flow === "builder.shape") {
|
|
79
|
+
if (!taskSlug || !isSafeSlug(taskSlug)) throw new Error("workflow start --flow builder.shape requires an explicit safe --task-slug");
|
|
80
|
+
if (workItem) throw new Error("workflow start --flow builder.shape creates a local Work Item; omit --work-item");
|
|
81
|
+
} else if (!workItem) {
|
|
82
|
+
throw new Error("workflow start requires --work-item <provider-ref>");
|
|
83
|
+
}
|
|
84
|
+
const assignmentProvider = flagString(parsed.flags, "assignment-provider", flow === "builder.shape" || workItem?.startsWith("local:") ? "local-file" : undefined);
|
|
85
|
+
const effectiveStateJson = flagString(parsed.flags, "effective-state-json");
|
|
86
|
+
if (flow === "builder.build" && workItem && !workItem.startsWith("local:") && !assignmentProvider) {
|
|
87
|
+
throw new Error("workflow start requires --assignment-provider <kind> for a provider-backed Work Item; provider identity is never inferred from its reference");
|
|
88
|
+
}
|
|
89
|
+
if (assignmentProvider !== "local-file" && !effectiveStateJson) {
|
|
90
|
+
throw new Error(`workflow start requires --effective-state-json <path> for assignment provider ${assignmentProvider}`);
|
|
91
|
+
}
|
|
92
|
+
if (assignmentProvider === "local-file" && effectiveStateJson) {
|
|
93
|
+
throw new Error("workflow start --effective-state-json is only valid for a non-local assignment provider");
|
|
94
|
+
}
|
|
95
|
+
if (workItem?.startsWith("local:")) {
|
|
74
96
|
const localSlug = workItem.slice("local:".length);
|
|
75
97
|
if (!taskSlug || taskSlug !== localSlug || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(localSlug)) {
|
|
76
98
|
throw new Error("local Work Item retries require the exact existing --task-slug binding");
|
|
@@ -80,29 +102,56 @@ async function start(argv: string[]): Promise<number> {
|
|
|
80
102
|
if (localRecord.id !== taskSlug || (localRecord.source_provider as JsonRecord | undefined)?.kind !== "local") {
|
|
81
103
|
throw new Error("local Work Item retry does not match the existing session binding");
|
|
82
104
|
}
|
|
83
|
-
} else if (taskSlug) {
|
|
105
|
+
} else if (taskSlug && flow !== "builder.shape") {
|
|
84
106
|
throw new Error("--task-slug is reserved for an existing local Work Item retry");
|
|
85
107
|
}
|
|
86
|
-
const
|
|
87
|
-
|
|
108
|
+
const existingSlug = taskSlug ?? (workItem ? workItemSlug(workItem) : null);
|
|
109
|
+
if (existingSlug && fs.existsSync(path.join(artifactRoot, existingSlug, "state.json"))) {
|
|
110
|
+
try {
|
|
111
|
+
const existing = await loadBuilderFlowRun({ cwd: path.dirname(path.dirname(artifactRoot)), runId: existingSlug });
|
|
112
|
+
if (existing.definitionId !== flow) {
|
|
113
|
+
throw new Error(`workflow start cannot resume ${existing.definitionId} as ${flow}; local shape sessions are not build retries. Create or claim a provider Work Item for the builder.build handoff.`);
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if ((error as { code?: string }).code !== "flow.run_location.not_found") throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (flow === "builder.build" && workItem && !workItem.startsWith("local:")) {
|
|
120
|
+
const slug = workItemSlug(workItem);
|
|
121
|
+
const report = path.join(artifactRoot, slug, `${slug}--pull-work.md`);
|
|
122
|
+
try {
|
|
123
|
+
const stat = fs.lstatSync(report);
|
|
124
|
+
if (stat.isSymbolicLink() || !stat.isFile() || !fs.readFileSync(report, "utf8").includes(workItem)) {
|
|
125
|
+
throw new Error("invalid");
|
|
126
|
+
}
|
|
127
|
+
} catch {
|
|
128
|
+
throw new Error(`workflow start requires concrete pull-work selection evidence at ${report} naming ${workItem} before it can produce selected-work`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const sourceRequest = flagString(parsed.flags, "source-request", `Start ${flow} for ${workItem ?? `local:${taskSlug}`}`)!;
|
|
132
|
+
const summary = flagString(parsed.flags, "summary", `Deliver ${workItem ?? `local:${taskSlug}`} through ${flow}.`)!;
|
|
88
133
|
const forwarded = keepFlags(argv, new Set(["title", "criterion"]));
|
|
89
|
-
return
|
|
134
|
+
return mainFromPublicWorkflow([
|
|
90
135
|
"ensure-session",
|
|
91
136
|
"--artifact-root", artifactRoot,
|
|
92
137
|
"--flow-id", flow,
|
|
93
|
-
"--work-item", workItem,
|
|
138
|
+
...(workItem ? ["--work-item", workItem] : []),
|
|
94
139
|
...(taskSlug ? ["--task-slug", taskSlug] : []),
|
|
140
|
+
...(assignmentProvider ? ["--assignment-provider", assignmentProvider] : []),
|
|
141
|
+
...(effectiveStateJson ? ["--effective-state-json", path.resolve(effectiveStateJson)] : []),
|
|
95
142
|
"--source-request", sourceRequest,
|
|
96
143
|
"--summary", summary,
|
|
97
144
|
...forwarded,
|
|
98
145
|
]);
|
|
99
146
|
}
|
|
100
147
|
|
|
148
|
+
function workItemSlug(workItem: string): string {
|
|
149
|
+
return workItem.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
150
|
+
}
|
|
151
|
+
|
|
101
152
|
async function status(sessionDir: string, json: boolean): Promise<number> {
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
const projectRoot = path.dirname(path.dirname(path.dirname(sessionDir)));
|
|
105
|
-
const result = await loadBuilderBuildRun({ cwd: projectRoot, runId: slug });
|
|
153
|
+
const inspected = await inspectBuilderFlowSession({ sessionDir });
|
|
154
|
+
const result = inspected.run;
|
|
106
155
|
const report = {
|
|
107
156
|
run_id: result.runId,
|
|
108
157
|
definition_id: result.definitionId,
|
|
@@ -110,7 +159,7 @@ async function status(sessionDir: string, json: boolean): Promise<number> {
|
|
|
110
159
|
status: result.state.status,
|
|
111
160
|
current_step: result.state.current_step,
|
|
112
161
|
session_dir: sessionDir,
|
|
113
|
-
next_action:
|
|
162
|
+
next_action: inspected.projection.next_action ?? null,
|
|
114
163
|
};
|
|
115
164
|
if (json) console.log(JSON.stringify(report));
|
|
116
165
|
else {
|
|
@@ -124,27 +173,107 @@ async function status(sessionDir: string, json: boolean): Promise<number> {
|
|
|
124
173
|
|
|
125
174
|
async function evidence(sessionDir: string, argv: string[], json: boolean): Promise<number> {
|
|
126
175
|
const parsed = parseArgs(argv);
|
|
127
|
-
assertOnlyFlags(parsed.flags, new Set(["artifact-root", "session-dir", "json", "expectation", "status", "summary", "evidence-ref-json", "accepted-gap-reason", "waived-by"]), "workflow evidence");
|
|
176
|
+
assertOnlyFlags(parsed.flags, new Set(["artifact-root", "session-dir", "json", "expectation", "status", "summary", "route-reason", "evidence-ref-json", "criterion-json", "accepted-gap-reason", "waived-by", "command"]), "workflow evidence");
|
|
128
177
|
if (!flagString(parsed.flags, "expectation")) throw new Error("workflow evidence requires --expectation <gate-expectation-id>");
|
|
129
178
|
if (!flagString(parsed.flags, "status")) throw new Error("workflow evidence requires --status <pass|fail|not_verified>");
|
|
130
179
|
if (!flagString(parsed.flags, "summary")) throw new Error("workflow evidence requires --summary <text>");
|
|
131
180
|
const forwarded = stripPublicFlags(argv, new Set(["artifact-root", "session-dir", "json"]));
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
181
|
+
const { slug, projectRoot } = readBoundSession(sessionDir);
|
|
182
|
+
const commands = flagList(parsed.flags, "command");
|
|
183
|
+
if (Object.hasOwn(parsed.flags, "command") && commands.length === 0) {
|
|
184
|
+
throw new Error("workflow evidence --command requires a shell command value");
|
|
185
|
+
}
|
|
186
|
+
const requiresTestEvidence = flagString(parsed.flags, "expectation") === "tests-evidence" && flagString(parsed.flags, "status") === "pass";
|
|
187
|
+
// Argument and command-shape rejection must be read-only. Recovery below may
|
|
188
|
+
// repair stale projections, so it runs only after every command is accepted.
|
|
189
|
+
assertRunnableEvidenceCommands(commands, projectRoot, requiresTestEvidence);
|
|
190
|
+
const report = await withSubjectLock(path.dirname(sessionDir), slug, async () => {
|
|
191
|
+
// Validate the owner after the lock is held, then keep the lock through command
|
|
192
|
+
// execution, evidence recording, and postcondition capture so assignment and
|
|
193
|
+
// session state cannot change mid-invocation.
|
|
194
|
+
const caller = assertMatchingAssignmentActor(sessionDir, slug);
|
|
195
|
+
const repaired = await recoverBuilderFlowSession({ sessionDir });
|
|
196
|
+
const beforeEvidence = manifestEvidenceDigests(repaired.run.manifest);
|
|
197
|
+
await mainFromPublicWorkflow([
|
|
198
|
+
"record-gate-claim",
|
|
199
|
+
sessionDir,
|
|
200
|
+
...forwarded,
|
|
201
|
+
"--actor",
|
|
202
|
+
caller.actorKey,
|
|
203
|
+
]);
|
|
204
|
+
|
|
205
|
+
await syncBuilderFlowSession({ sessionDir });
|
|
206
|
+
|
|
207
|
+
const digest = fileSha256(path.join(sessionDir, "trust.bundle"));
|
|
208
|
+
const run = await loadBuilderFlowRun({ cwd: repaired.projectRoot, runId: slug });
|
|
209
|
+
const afterEvidence = manifestEvidenceDigests(run.manifest);
|
|
210
|
+
const newEvidence = afterEvidence.filter((value) => !beforeEvidence.includes(value));
|
|
211
|
+
if (newEvidence.length !== 1 || newEvidence[0] !== digest) {
|
|
212
|
+
throw new Error("workflow evidence did not attach exactly this invocation's resulting trust.bundle digest");
|
|
213
|
+
}
|
|
214
|
+
const updatedSidecar = readBoundSession(sessionDir).sidecar;
|
|
215
|
+
return immutableReport({
|
|
216
|
+
run_id: run.runId,
|
|
217
|
+
status: run.state.status,
|
|
218
|
+
current_step: run.state.current_step,
|
|
219
|
+
attached: true,
|
|
220
|
+
next_action: updatedSidecar.next_action ?? null,
|
|
221
|
+
});
|
|
222
|
+
});
|
|
143
223
|
if (json) console.log(JSON.stringify(report));
|
|
144
224
|
else console.log(`Recorded evidence; canonical run is ${report.status} at ${report.current_step}.`);
|
|
145
225
|
return 0;
|
|
146
226
|
}
|
|
147
227
|
|
|
228
|
+
async function critique(sessionDir: string, argv: string[], json: boolean): Promise<number> {
|
|
229
|
+
const parsed = parseArgs(argv);
|
|
230
|
+
assertOnlyFlags(parsed.flags, new Set(["artifact-root", "session-dir", "json", "id", "verdict", "summary", "artifact-ref", "finding-json", "lane-json", "timestamp"]), "workflow critique");
|
|
231
|
+
if (!flagString(parsed.flags, "summary")) throw new Error("workflow critique requires --summary <text>");
|
|
232
|
+
if (Object.hasOwn(parsed.flags, "reviewer")) throw new Error("workflow critique derives reviewer identity from the authenticated assignment actor; --reviewer is not accepted");
|
|
233
|
+
const { slug, projectRoot } = readBoundSession(sessionDir);
|
|
234
|
+
const forwarded = stripPublicFlags(argv, new Set(["artifact-root", "session-dir", "json"]));
|
|
235
|
+
const report = await withSubjectLock(path.dirname(sessionDir), slug, async () => {
|
|
236
|
+
const caller = assertDistinctReviewActor(sessionDir, slug);
|
|
237
|
+
const current = await loadBuilderFlowRun({ cwd: projectRoot, runId: slug });
|
|
238
|
+
if (current.definitionId !== "builder.build" || current.state.current_step !== "verify") throw new Error("workflow critique is allowed only for the canonical builder.build verify step");
|
|
239
|
+
const beforeManifest = JSON.parse(JSON.stringify(current.manifest)) as JsonRecord;
|
|
240
|
+
const beforeTrustBundle = optionalFileDigest(path.join(sessionDir, "trust.bundle"));
|
|
241
|
+
const legacySidecars = ["critique.json", "evidence.json"].map((name) => ({ name, digest: optionalFileDigest(path.join(sessionDir, name)) }));
|
|
242
|
+
await mainFromPublicWorkflow(["record-critique", sessionDir, ...forwarded, "--reviewer", caller.actorKey]);
|
|
243
|
+
const result = await recoverBuilderFlowSession({ sessionDir });
|
|
244
|
+
const digest = fileSha256(path.join(sessionDir, "trust.bundle"));
|
|
245
|
+
if (!isDeepStrictEqual(result.run.manifest, beforeManifest)) {
|
|
246
|
+
throw new Error("workflow critique must not attach or otherwise mutate the Flow manifest");
|
|
247
|
+
}
|
|
248
|
+
if (beforeTrustBundle === digest) {
|
|
249
|
+
throw new Error("workflow critique did not change trust.bundle");
|
|
250
|
+
}
|
|
251
|
+
if (legacySidecars.some(({ name, digest: legacyDigest }) => optionalFileDigest(path.join(sessionDir, name)) !== legacyDigest)) {
|
|
252
|
+
throw new Error("workflow critique must persist only through trust.bundle");
|
|
253
|
+
}
|
|
254
|
+
return immutableReport({ run_id: slug, recorded: true });
|
|
255
|
+
});
|
|
256
|
+
if (json) console.log(JSON.stringify(report));
|
|
257
|
+
else console.log("Recorded critique in the trust bundle.");
|
|
258
|
+
return 0;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function assertRunnableEvidenceCommands(commands: string[], projectRoot: string, requiresTestEvidence: boolean): void {
|
|
262
|
+
const helperPath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../scripts/hooks/lib/runnable-command.js");
|
|
263
|
+
const { isRunnableCommandText } = REQUIRE(helperPath) as { isRunnableCommandText: (text: string) => boolean };
|
|
264
|
+
if (commands.length > 1 && new Set(commands).size !== commands.length) {
|
|
265
|
+
throw new Error("workflow evidence --command values must be unique because observations are matched by exact command text");
|
|
266
|
+
}
|
|
267
|
+
for (const command of commands) {
|
|
268
|
+
if (!isRunnableCommandText(command)) {
|
|
269
|
+
throw new Error(`workflow evidence --command ${JSON.stringify(command)} is not a runnable shell command — prose belongs in --summary, which is never executed.`);
|
|
270
|
+
}
|
|
271
|
+
if (requiresTestEvidence && !isMeaningfulTestCommand(command, projectRoot)) {
|
|
272
|
+
throw new Error("workflow evidence tests-evidence command must resolve through a non-vacuous package script or a known test/check/verify/eval runner or project-local test path; shell wrappers, no-ops, version/help commands, and arbitrary node -e commands are not evidence");
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
148
277
|
function resolveSessionDir(flags: ReturnType<typeof parseArgs>["flags"]): string {
|
|
149
278
|
const explicit = flagString(flags, "session-dir");
|
|
150
279
|
if (explicit) return validateCanonicalSessionDir(path.resolve(explicit));
|
|
@@ -167,11 +296,16 @@ function doctor(argv: string[]): number {
|
|
|
167
296
|
const install = readOptionalJson(installFile);
|
|
168
297
|
const state = readCurrentState(artifactRoot);
|
|
169
298
|
const packageKit = readOptionalJson(path.join(packageRoot, "kits", "builder", "kit.json"));
|
|
170
|
-
const packageFlow = readOptionalJson(path.join(packageRoot, "kits", "builder", "flows", "build.flow.json"));
|
|
171
299
|
const installedKitFile = path.join(projectRoot, "kits", "builder", "kit.json");
|
|
172
|
-
const installedFlowFile = path.join(projectRoot, "kits", "builder", "flows", "build.flow.json");
|
|
173
300
|
const installedKit = readOptionalJson(installedKitFile);
|
|
174
|
-
const
|
|
301
|
+
const definitions = [
|
|
302
|
+
{ id: "builder.build", file: "build.flow.json" },
|
|
303
|
+
{ id: "builder.shape", file: "shape.flow.json" },
|
|
304
|
+
].map(({ id, file }) => {
|
|
305
|
+
const packageFile = path.join(packageRoot, "kits", "builder", "flows", file);
|
|
306
|
+
const installedFile = path.join(projectRoot, "kits", "builder", "flows", file);
|
|
307
|
+
return { id, packageFile, installedFile, packageDefinition: readOptionalJson(packageFile), installedDefinition: readOptionalJson(installedFile) };
|
|
308
|
+
});
|
|
175
309
|
const resolvedFlowPackage = readOptionalJson(resolveDependencyPackageJson("@kontourai/flow"));
|
|
176
310
|
const installedVersion = typeof install?.version === "string" ? install.version : null;
|
|
177
311
|
const staleInstall = installedVersion !== null && installedVersion !== cliVersion;
|
|
@@ -187,18 +321,35 @@ function doctor(argv: string[]): number {
|
|
|
187
321
|
if (!installIntegrity.ok) warnings.push(`Installed hook/writer assets failed integrity verification: ${installIntegrity.problems.join("; ")}. Run: ${remediation}`);
|
|
188
322
|
if (localDependency?.version && localDependency.version !== cliVersion) warnings.push(`Repository-local Flow Agents ${String(localDependency.version)} differs from executing CLI ${cliVersion}; keep automation explicitly versioned.`);
|
|
189
323
|
if (activeKitIds.includes("builder") && !installedKit) warnings.push(`Activated Builder Kit is missing at ${installedKitFile}. Run: ${remediation}`);
|
|
190
|
-
|
|
324
|
+
for (const definition of definitions) {
|
|
325
|
+
if (activeKitIds.includes("builder") && !definition.installedDefinition) {
|
|
326
|
+
warnings.push(`Activated ${definition.id} definition is missing at ${definition.installedFile}. Run: ${remediation}`);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (!definition.installedDefinition) continue;
|
|
330
|
+
const validationError = validateFlowDefinition(definition.id, definition.installedDefinition);
|
|
331
|
+
if (validationError) warnings.push(`Installed ${definition.id} definition is invalid: ${validationError}. Run: ${remediation}`);
|
|
332
|
+
if (definition.installedDefinition.id !== definition.packageDefinition?.id) {
|
|
333
|
+
warnings.push(`Installed ${definition.id} definition id ${String(definition.installedDefinition.id)} differs from CLI definition ${String(definition.packageDefinition?.id)}. Run: ${remediation}`);
|
|
334
|
+
}
|
|
335
|
+
if (definition.installedDefinition.version !== definition.packageDefinition?.version) {
|
|
336
|
+
warnings.push(`Installed ${definition.id} version ${String(definition.installedDefinition.version)} differs from CLI version ${String(definition.packageDefinition?.version)}. Run: ${remediation}`);
|
|
337
|
+
} else if (fileSha256(definition.installedFile) !== fileSha256(definition.packageFile)) {
|
|
338
|
+
warnings.push(`Installed ${definition.id} content differs from Flow Agents ${cliVersion}. Run: ${remediation}`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
191
341
|
if (installedKit && installedKit.schema_version !== packageKit?.schema_version) {
|
|
192
342
|
warnings.push(`Installed Builder Kit schema ${String(installedKit.schema_version)} differs from CLI schema ${String(packageKit?.schema_version)}. Run: ${remediation}`);
|
|
193
343
|
}
|
|
194
344
|
if (installedKit && fileSha256(installedKitFile) !== fileSha256(path.join(packageRoot, "kits", "builder", "kit.json"))) {
|
|
195
345
|
warnings.push(`Installed Builder Kit content differs from Flow Agents ${cliVersion}. Run: ${remediation}`);
|
|
196
346
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
347
|
+
const currentRun = state?.flow_run && typeof state.flow_run === "object" ? state.flow_run as JsonRecord : null;
|
|
348
|
+
const currentDefinition = definitions.find((definition) => definition.id === currentRun?.definition_id);
|
|
349
|
+
if (currentRun?.definition_id && !currentDefinition) {
|
|
350
|
+
warnings.push(`Current run uses unsupported Flow definition ${String(currentRun.definition_id)}; recover or migrate before mutation.`);
|
|
351
|
+
} else if (currentRun && currentDefinition && currentRun.definition_version !== currentDefinition.packageDefinition?.version) {
|
|
352
|
+
warnings.push(`Current run uses ${currentDefinition.id}@${String(currentRun.definition_version)} while CLI resolves ${currentDefinition.id}@${String(currentDefinition.packageDefinition?.version)}; recover or migrate before mutation.`);
|
|
202
353
|
}
|
|
203
354
|
if (state && state.schema_version !== "1.0") warnings.push(`Artifact schema ${String(state.schema_version)} is unsupported; recreate or migrate the session with CLI ${cliVersion}.`);
|
|
204
355
|
const trustBundleSchema = readCurrentTrustBundleSchema(artifactRoot);
|
|
@@ -234,7 +385,13 @@ function doctor(argv: string[]): number {
|
|
|
234
385
|
source: installedKit ? installedKitFile : path.join(packageRoot, "kits", "builder", "kit.json"),
|
|
235
386
|
},
|
|
236
387
|
flow_runtime: { package_version: resolvedFlowVersion, expected_range: expectedFlowRange, compatible: resolvedFlowVersion ? flowVersionCompatible(resolvedFlowVersion, expectedFlowRange) : false },
|
|
237
|
-
definition: { id:
|
|
388
|
+
definition: { id: currentRun?.definition_id ?? definitions[0]!.packageDefinition?.id ?? null, version: currentRun?.definition_version ?? definitions[0]!.packageDefinition?.version ?? null },
|
|
389
|
+
definitions: definitions.map((definition) => ({
|
|
390
|
+
id: definition.id,
|
|
391
|
+
resolved_version: definition.packageDefinition?.version ?? null,
|
|
392
|
+
installed_version: definition.installedDefinition?.version ?? null,
|
|
393
|
+
installed_valid: definition.installedDefinition ? validateFlowDefinition(definition.id, definition.installedDefinition) === null : false,
|
|
394
|
+
})),
|
|
238
395
|
artifact: {
|
|
239
396
|
state_schema_version: state?.schema_version ?? null,
|
|
240
397
|
trust_bundle_schema_version: trustBundleSchema,
|
|
@@ -288,6 +445,86 @@ function readOptionalJson(file: string): JsonRecord | null {
|
|
|
288
445
|
}
|
|
289
446
|
}
|
|
290
447
|
|
|
448
|
+
function validateFlowDefinition(expectedId: string, definition: JsonRecord): string | null {
|
|
449
|
+
if (definition.id !== expectedId) return `expected id ${expectedId}, received ${String(definition.id)}`;
|
|
450
|
+
try {
|
|
451
|
+
validateDefinition(definition as never);
|
|
452
|
+
return null;
|
|
453
|
+
} catch (error) {
|
|
454
|
+
return error instanceof Error ? error.message : String(error);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function isSafeSlug(value: string): boolean {
|
|
459
|
+
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function readBoundSession(sessionDir: string): { sidecar: JsonRecord; slug: string; projectRoot: string } {
|
|
463
|
+
const slug = path.basename(sessionDir);
|
|
464
|
+
if (!isSafeSlug(slug)) throw new Error("workflow session basename must be a safe task slug");
|
|
465
|
+
const sidecar = readJsonFile(path.join(sessionDir, "state.json"), "workflow state");
|
|
466
|
+
if (sidecar.task_slug !== slug) throw new Error("workflow state task_slug must exactly match the safe session basename");
|
|
467
|
+
return { sidecar, slug, projectRoot: path.dirname(path.dirname(path.dirname(sessionDir))) };
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function readAssignment(sessionDir: string, slug: string): JsonRecord {
|
|
471
|
+
const artifactRoot = path.dirname(sessionDir);
|
|
472
|
+
const assignmentRoot = path.join(artifactRoot, "assignment");
|
|
473
|
+
const stat = fs.lstatSync(assignmentRoot);
|
|
474
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) throw new Error("workflow assignment directory must be a non-symlink directory");
|
|
475
|
+
const file = path.join(assignmentRoot, `${slug}.json`);
|
|
476
|
+
const fileStat = fs.lstatSync(file);
|
|
477
|
+
if (fileStat.isSymbolicLink() || !fileStat.isFile()) throw new Error("workflow assignment must be a non-symlink regular file");
|
|
478
|
+
return readJsonFile(file, "workflow assignment");
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function assertMatchingAssignmentActor(sessionDir: string, slug: string): ReturnType<typeof resolveCurrentAssignmentActor> {
|
|
482
|
+
const assignment = readActiveAssignment(sessionDir, slug);
|
|
483
|
+
const caller = resolveCurrentAssignmentActor();
|
|
484
|
+
const normalizeActor = (value: unknown): JsonRecord | null => value && typeof value === "object" ? { ...(value as JsonRecord), human: (value as JsonRecord).human ?? null } : null;
|
|
485
|
+
if (assignment.actor_key !== caller.actorKey || !isDeepStrictEqual(normalizeActor(assignment.actor), normalizeActor(caller.actor))) {
|
|
486
|
+
throw new Error("workflow mutation requires the session's active, matching assignment actor");
|
|
487
|
+
}
|
|
488
|
+
return caller;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function readActiveAssignment(sessionDir: string, slug: string): JsonRecord {
|
|
492
|
+
const assignment = readAssignment(sessionDir, slug);
|
|
493
|
+
if (assignment.status !== "claimed" || assignment.artifact_dir !== slug || typeof assignment.actor_key !== "string" || !assignment.actor_key || !assignment.actor || typeof assignment.actor !== "object" || Array.isArray(assignment.actor)) {
|
|
494
|
+
throw new Error("workflow mutation requires the session's active implementation assignment");
|
|
495
|
+
}
|
|
496
|
+
return assignment;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function assertDistinctReviewActor(sessionDir: string, slug: string): ReturnType<typeof resolveCurrentAssignmentActor> {
|
|
500
|
+
const assignment = readActiveAssignment(sessionDir, slug);
|
|
501
|
+
const caller = resolveCurrentAssignmentActor();
|
|
502
|
+
if (assignment.actor_key === caller.actorKey) {
|
|
503
|
+
throw new Error("workflow critique requires a reviewer identity distinct from the active implementation assignment actor");
|
|
504
|
+
}
|
|
505
|
+
return caller;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function immutableReport<T>(value: T): T {
|
|
509
|
+
if (!value || typeof value !== "object") return value;
|
|
510
|
+
for (const nested of Object.values(value as Record<string, unknown>)) immutableReport(nested);
|
|
511
|
+
return Object.freeze(value);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function manifestEvidenceDigests(manifest: JsonRecord): string[] {
|
|
515
|
+
const evidence = Array.isArray(manifest.evidence) ? manifest.evidence : [];
|
|
516
|
+
return evidence
|
|
517
|
+
.map((entry) => entry && typeof entry === "object" ? (entry as JsonRecord).sha256 : null)
|
|
518
|
+
.filter((digest): digest is string => typeof digest === "string");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function optionalFileDigest(file: string): string | null {
|
|
522
|
+
try { return fileSha256(file); } catch (error) {
|
|
523
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
|
|
524
|
+
throw error;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
291
528
|
function stripPublicFlags(argv: string[], removed: Set<string>): string[] {
|
|
292
529
|
const result: string[] = [];
|
|
293
530
|
for (let index = 0; index < argv.length; index += 1) {
|