sdtk-code-kit 0.3.0 → 0.3.1

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.
Files changed (56) hide show
  1. package/README.md +204 -204
  2. package/assets/manifest/toolkit-bundle.manifest.json +147 -147
  3. package/assets/manifest/toolkit-bundle.sha256.txt +5 -5
  4. package/bin/sdtk-code.js +14 -14
  5. package/package.json +44 -44
  6. package/src/commands/agent-team.js +425 -174
  7. package/src/commands/build.js +53 -53
  8. package/src/commands/doctor.js +51 -51
  9. package/src/commands/generate.js +11 -11
  10. package/src/commands/guardrails.js +215 -215
  11. package/src/commands/help.js +264 -257
  12. package/src/commands/init.js +98 -98
  13. package/src/commands/plan.js +53 -53
  14. package/src/commands/readiness.js +77 -77
  15. package/src/commands/resume.js +61 -61
  16. package/src/commands/runtime.js +224 -224
  17. package/src/commands/ship.js +59 -59
  18. package/src/commands/sleep.js +132 -132
  19. package/src/commands/start.js +89 -89
  20. package/src/commands/status.js +77 -77
  21. package/src/commands/update.js +11 -11
  22. package/src/commands/verify.js +71 -71
  23. package/src/index.js +112 -112
  24. package/src/lib/agent-team-author.js +521 -0
  25. package/src/lib/agent-team-evidence.js +302 -0
  26. package/src/lib/agent-team-execute.js +892 -0
  27. package/src/lib/agent-team-export.js +411 -409
  28. package/src/lib/agent-team-plan.js +514 -514
  29. package/src/lib/agent-team-readiness.js +161 -0
  30. package/src/lib/agent-team-report.js +165 -0
  31. package/src/lib/args.js +107 -107
  32. package/src/lib/errors.js +41 -41
  33. package/src/lib/model-provider.js +219 -0
  34. package/src/lib/powershell.js +65 -65
  35. package/src/lib/project-target-guard.js +53 -53
  36. package/src/lib/scope.js +72 -72
  37. package/src/lib/ship-readiness.js +281 -281
  38. package/src/lib/sleep-plan.js +175 -175
  39. package/src/lib/sleep-report.js +229 -229
  40. package/src/lib/toolkit-payload.js +72 -72
  41. package/src/lib/trust-events.js +252 -185
  42. package/src/lib/trust-file-scope.js +154 -154
  43. package/src/lib/trust-policy.js +212 -212
  44. package/src/lib/trust-redact.js +58 -35
  45. package/src/lib/trust-trace.js +179 -179
  46. package/src/lib/update.js +344 -344
  47. package/src/lib/workflow-artifact.js +503 -503
  48. package/src/lib/workflow-build.js +127 -127
  49. package/src/lib/workflow-doctor.js +83 -83
  50. package/src/lib/workflow-handoff.js +1029 -1029
  51. package/src/lib/workflow-intake.js +101 -101
  52. package/src/lib/workflow-plan.js +193 -193
  53. package/src/lib/workflow-review-packet.js +650 -650
  54. package/src/lib/workflow-ship.js +213 -213
  55. package/src/lib/workflow-state.js +438 -438
  56. package/src/lib/workflow-verify.js +393 -393
@@ -1,393 +1,393 @@
1
- "use strict";
2
-
3
- const fs = require("fs");
4
- const path = require("path");
5
- const { ValidationError } = require("./errors");
6
- const { createOrUpdateWorkflowArtifact, loadWorkflowArtifactState } = require("./workflow-artifact");
7
- const { createReviewPacket } = require("./workflow-review-packet");
8
-
9
- const VALID_REVIEW_STATUS = ["pending", "pass", "partial", "fail"];
10
- const VALID_EVIDENCE_RESULT = ["pass", "partial", "fail"];
11
- const VALID_TRUTH_SYNC_STATUS = ["touched", "untouched"];
12
- const GLOB_PATTERN_RE = /[*?[\]{}]/;
13
-
14
- function normalizeEntries(entries) {
15
- return (entries || []).map((entry) => entry.trim()).filter(Boolean);
16
- }
17
-
18
- function validateChoice(value, choices, flagName) {
19
- if (!choices.includes(value)) {
20
- throw new ValidationError(
21
- `Invalid value for --${flagName}: "${value}". Must be one of: ${choices.join(", ")}`
22
- );
23
- }
24
- return value;
25
- }
26
-
27
- function normalizeRepoRelativePath(rawValue, label) {
28
- if (!rawValue || !String(rawValue).trim()) {
29
- throw new ValidationError(`${label} must be a non-empty repo-relative path.`);
30
- }
31
-
32
- const normalized = String(rawValue).trim().replace(/\\/g, "/").replace(/^\.\//, "");
33
- if (normalized.startsWith("/") || /^[A-Za-z]:/.test(normalized)) {
34
- throw new ValidationError(`${label} must be repo-relative, not absolute: ${rawValue}`);
35
- }
36
- if (
37
- normalized === "." ||
38
- normalized.startsWith("../") ||
39
- normalized.split("/").includes("..")
40
- ) {
41
- throw new ValidationError(`${label} must stay within the repository boundary: ${rawValue}`);
42
- }
43
-
44
- return normalized;
45
- }
46
-
47
- function rejectGlobPattern(rawValue, label) {
48
- if (GLOB_PATTERN_RE.test(String(rawValue))) {
49
- throw new ValidationError(`${label} must be an exact repo-relative path, not a glob pattern: ${rawValue}`);
50
- }
51
- }
52
-
53
- function parseEvidenceEntries(entries, featureKey, projectPath) {
54
- const normalized = normalizeEntries(entries);
55
- if (normalized.length === 0) {
56
- throw new ValidationError(
57
- 'Verification requires at least one "--evidence" entry in the format "check|summary|pass|docs/dev/evidence/FEATURE_KEY/file.txt".'
58
- );
59
- }
60
-
61
- const expectedPrefix = `docs/dev/evidence/${featureKey}/`;
62
- return normalized.map((entry) => {
63
- const parts = entry.split("|").map((part) => part.trim());
64
- if (parts.length !== 4 || parts.some((part) => !part)) {
65
- throw new ValidationError(
66
- 'Each "--evidence" entry must use exactly four pipe-separated fields: "check|summary|pass|docs/dev/evidence/FEATURE_KEY/file.txt".'
67
- );
68
- }
69
-
70
- const [checkName, summary, result, rawOutputRef] = parts;
71
- validateChoice(result, VALID_EVIDENCE_RESULT, "evidence");
72
-
73
- const normalizedRef = rawOutputRef.replace(/\\/g, "/");
74
- rejectGlobPattern(normalizedRef, "evidence raw-output reference");
75
- if (!normalizedRef.startsWith(expectedPrefix)) {
76
- throw new ValidationError(
77
- `Evidence raw-output reference must stay under "${expectedPrefix}". Received: ${rawOutputRef}`
78
- );
79
- }
80
- if (!fs.existsSync(path.join(projectPath, normalizedRef.replace(/\//g, path.sep)))) {
81
- throw new ValidationError(
82
- `Evidence raw-output reference must exist before verify can claim the check ran. Missing: ${rawOutputRef}`
83
- );
84
- }
85
-
86
- return {
87
- checkName,
88
- summary,
89
- result,
90
- rawOutputRef: normalizedRef,
91
- };
92
- });
93
- }
94
-
95
- function parseChangedFileEntries(entries) {
96
- return normalizeEntries(entries).map((entry) => {
97
- const parts = entry.split("|").map((part) => part.trim());
98
- if (parts.length !== 2 || parts.some((part) => !part)) {
99
- throw new ValidationError(
100
- 'Each "--changed-file" entry must use exactly two pipe-separated fields: "repo/path|rationale".'
101
- );
102
- }
103
-
104
- const normalizedPath = normalizeRepoRelativePath(parts[0], "changed-file path");
105
- rejectGlobPattern(normalizedPath, "changed-file path");
106
-
107
- return {
108
- path: normalizedPath,
109
- rationale: parts[1],
110
- };
111
- });
112
- }
113
-
114
- function parseExcludeFileEntries(entries) {
115
- return normalizeEntries(entries).map((entry) => {
116
- const parts = entry.split("|").map((part) => part.trim());
117
- if (parts.length !== 2 || parts.some((part) => !part)) {
118
- throw new ValidationError(
119
- 'Each "--exclude-file" entry must use exactly two pipe-separated fields: "repo/path|reason".'
120
- );
121
- }
122
-
123
- const normalizedPath = normalizeRepoRelativePath(parts[0], "exclude-file path");
124
- rejectGlobPattern(normalizedPath, "exclude-file path");
125
-
126
- return {
127
- path: normalizedPath,
128
- reason: parts[1],
129
- };
130
- });
131
- }
132
-
133
- function parseTruthSyncTargetEntries(entries) {
134
- return normalizeEntries(entries).map((entry) => {
135
- const parts = entry.split("|").map((part) => part.trim());
136
- if (parts.length !== 3 || parts.some((part) => !part)) {
137
- throw new ValidationError(
138
- 'Each "--truth-sync-target" entry must use exactly three pipe-separated fields: "path-or-surface|touched|rationale".'
139
- );
140
- }
141
-
142
- const [target, status, rationale] = parts;
143
- validateChoice(status, VALID_TRUTH_SYNC_STATUS, "truth-sync-target");
144
- return {
145
- target,
146
- status,
147
- rationale,
148
- };
149
- });
150
- }
151
-
152
- function parseCommitBoundaryPaths(entries, flagName) {
153
- return normalizeEntries(entries).map((entry) => {
154
- const normalizedPath = normalizeRepoRelativePath(entry, `${flagName} path`);
155
- rejectGlobPattern(normalizedPath, `${flagName} path`);
156
- return normalizedPath;
157
- });
158
- }
159
-
160
- function parseCaveatEntries(entries) {
161
- return normalizeEntries(entries).map((entry) => {
162
- const parts = entry.split("|").map((part) => part.trim());
163
- if (parts.length !== 4 || parts.some((part) => !part)) {
164
- throw new ValidationError(
165
- 'Each "--caveat" entry must use exactly four pipe-separated fields: "ID|statement|impact|owner".'
166
- );
167
- }
168
-
169
- return {
170
- id: parts[0],
171
- statement: parts[1],
172
- impact: parts[2],
173
- owner: parts[3],
174
- };
175
- });
176
- }
177
-
178
- function resolveEvidenceGate(evidenceEntries) {
179
- if (evidenceEntries.some((entry) => entry.result === "fail")) {
180
- return "fail";
181
- }
182
- if (evidenceEntries.some((entry) => entry.result === "partial")) {
183
- return "partial";
184
- }
185
- return "pass";
186
- }
187
-
188
- function resolveReviewStatus(value, flagName) {
189
- if (!value) {
190
- return "pending";
191
- }
192
- return validateChoice(value, VALID_REVIEW_STATUS, flagName);
193
- }
194
-
195
- function validateVerifyState(state) {
196
- if (state.intakeOutcome === "BLOCKED_MISSING_INPUTS") {
197
- throw new ValidationError(
198
- 'Workflow artifact is blocked by missing upstream inputs. Re-run "sdtk-code start" after the handoff inputs are ready.'
199
- );
200
- }
201
-
202
- if (state.currentPhase === "build") {
203
- if (state.phaseStatus !== "completed") {
204
- throw new ValidationError(
205
- 'Workflow must complete "build" before "verify". Run "sdtk-code build --complete" first.'
206
- );
207
- }
208
- return;
209
- }
210
-
211
- if (state.currentPhase !== "verify") {
212
- throw new ValidationError(
213
- `Cannot run "verify" from current phase "${state.currentPhase}". Re-run the next legal workflow step instead.`
214
- );
215
- }
216
- }
217
-
218
- function resolveVerifyPhaseStatus(flags, state, evidenceGate, specGateStatus, qualityGateStatus) {
219
- if (flags.complete && flags.blocked) {
220
- throw new ValidationError('Use either "--complete" or "--blocked", not both.');
221
- }
222
-
223
- if (flags.complete) {
224
- if (evidenceGate !== "pass" || specGateStatus !== "pass" || qualityGateStatus !== "pass") {
225
- throw new ValidationError(
226
- 'Verify can be marked complete only when evidence, spec/compliance, and quality gates all pass.'
227
- );
228
- }
229
- return "completed";
230
- }
231
-
232
- if (
233
- flags.blocked ||
234
- evidenceGate === "fail" ||
235
- specGateStatus === "fail" ||
236
- qualityGateStatus === "fail"
237
- ) {
238
- return "blocked";
239
- }
240
-
241
- if (
242
- state.currentPhase === "verify" &&
243
- state.phaseStatus === "completed" &&
244
- evidenceGate === "pass" &&
245
- specGateStatus === "pass" &&
246
- qualityGateStatus === "pass"
247
- ) {
248
- return "completed";
249
- }
250
-
251
- return "in_progress";
252
- }
253
-
254
- function getSuggestedEngineSkills(phaseStatus, evidenceGate, specGateStatus, qualityGateStatus) {
255
- if (phaseStatus === "completed") {
256
- return ["code-ship", "code-finish"];
257
- }
258
-
259
- const hasFailure =
260
- evidenceGate === "fail" || specGateStatus === "fail" || qualityGateStatus === "fail";
261
- const skills = ["code-verify", "code-review"];
262
- if (hasFailure || phaseStatus === "blocked") {
263
- skills.unshift("code-debug");
264
- }
265
- return skills;
266
- }
267
-
268
- function evaluateProvenanceConsistency(reviewPacket) {
269
- const warnings = [];
270
- if (!reviewPacket || !reviewPacket.hasReferenceDisclosure) {
271
- warnings.push("review packet did not render the required Reference Disclosure section");
272
- return warnings;
273
- }
274
-
275
- if (reviewPacket.provenanceMode === "missing") {
276
- warnings.push("workflow plan is missing the required Provenance section or declaration");
277
- }
278
-
279
- if (
280
- reviewPacket.provenanceMode === "external" &&
281
- reviewPacket.referenceDisclosureMode !== "external"
282
- ) {
283
- warnings.push("workflow plan reports external references, but review packet disclosure mode is not external");
284
- }
285
-
286
- if (
287
- reviewPacket.provenanceMode === "no_reference" &&
288
- reviewPacket.referenceDisclosureMode !== "no_reference"
289
- ) {
290
- warnings.push("workflow plan reports no-reference declaration, but review packet disclosure does not mirror it");
291
- }
292
-
293
- return warnings;
294
- }
295
-
296
- function runVerify(projectPath, featureKey, flags) {
297
- const state = loadWorkflowArtifactState(projectPath, featureKey);
298
- validateVerifyState(state);
299
-
300
- const evidenceEntries = parseEvidenceEntries(flags.evidence, featureKey, projectPath);
301
- const evidenceGate = resolveEvidenceGate(evidenceEntries);
302
- const specGateStatus = resolveReviewStatus(flags["spec-status"], "spec-status");
303
- const qualityGateStatus = resolveReviewStatus(flags["quality-status"], "quality-status");
304
- const phaseStatus = resolveVerifyPhaseStatus(
305
- flags,
306
- state,
307
- evidenceGate,
308
- specGateStatus,
309
- qualityGateStatus
310
- );
311
- const nextPhase = phaseStatus === "completed" ? "ship" : "verify";
312
- const blockingState =
313
- phaseStatus === "blocked"
314
- ? "blocked_verify_issue"
315
- : phaseStatus === "completed"
316
- ? "clear"
317
- : "review_in_progress";
318
- const suggestedEngineSkills = getSuggestedEngineSkills(
319
- phaseStatus,
320
- evidenceGate,
321
- specGateStatus,
322
- qualityGateStatus
323
- );
324
- const specNotes = normalizeEntries(flags["spec-note"]);
325
- const qualityNotes = normalizeEntries(flags["quality-note"]);
326
- const debugNotes = normalizeEntries(flags["debug-note"]);
327
- const reviewPacket = createReviewPacket({
328
- projectPath,
329
- featureKey,
330
- workflowText: state.text,
331
- evidenceEntries,
332
- specGateStatus,
333
- qualityGateStatus,
334
- specNotes,
335
- qualityNotes,
336
- debugNotes,
337
- batchSummary: flags["batch-summary"] || "",
338
- changedFiles: parseChangedFileEntries(flags["changed-file"]),
339
- excludedFiles: parseExcludeFileEntries(flags["exclude-file"]),
340
- truthSyncTargets: parseTruthSyncTargetEntries(flags["truth-sync-target"]),
341
- commitInclude: parseCommitBoundaryPaths(flags["commit-include"], "commit-include"),
342
- commitExclude: parseCommitBoundaryPaths(flags["commit-exclude"], "commit-exclude"),
343
- caveats: parseCaveatEntries(flags.caveat),
344
- reviewerGuidance: normalizeEntries(flags["reviewer-guidance"]),
345
- });
346
- const normalizedReviewPacketPath = path
347
- .relative(projectPath, reviewPacket.reviewPacketPath)
348
- .replace(/\\/g, "/");
349
- const provenanceWarnings = evaluateProvenanceConsistency(reviewPacket);
350
- const verifyDebugNotes = [
351
- ...debugNotes,
352
- ...provenanceWarnings.map((warning) => `Provenance warning: ${warning}`),
353
- ];
354
-
355
- const artifactPath = createOrUpdateWorkflowArtifact({
356
- projectPath,
357
- featureKey,
358
- lane: state.lane,
359
- currentPhase: "verify",
360
- phaseStatus,
361
- intakeOutcome: state.intakeOutcome,
362
- nextPhase,
363
- blockingState,
364
- suggestedEngineSkills,
365
- managedBlocks: ["METADATA", "VERIFY", "STATUS"],
366
- evidenceEntries,
367
- evidenceGate,
368
- specGateStatus,
369
- qualityGateStatus,
370
- specNotes,
371
- qualityNotes,
372
- verifyDebugNotes,
373
- reviewPacketPath: normalizedReviewPacketPath,
374
- });
375
-
376
- return {
377
- artifactPath,
378
- lane: state.lane,
379
- phaseStatus,
380
- nextPhase,
381
- suggestedEngineSkills,
382
- evidenceCount: evidenceEntries.length,
383
- evidenceGate,
384
- specGateStatus,
385
- qualityGateStatus,
386
- reviewPacketPath: normalizedReviewPacketPath,
387
- provenanceWarnings,
388
- };
389
- }
390
-
391
- module.exports = {
392
- runVerify,
393
- };
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { ValidationError } = require("./errors");
6
+ const { createOrUpdateWorkflowArtifact, loadWorkflowArtifactState } = require("./workflow-artifact");
7
+ const { createReviewPacket } = require("./workflow-review-packet");
8
+
9
+ const VALID_REVIEW_STATUS = ["pending", "pass", "partial", "fail"];
10
+ const VALID_EVIDENCE_RESULT = ["pass", "partial", "fail"];
11
+ const VALID_TRUTH_SYNC_STATUS = ["touched", "untouched"];
12
+ const GLOB_PATTERN_RE = /[*?[\]{}]/;
13
+
14
+ function normalizeEntries(entries) {
15
+ return (entries || []).map((entry) => entry.trim()).filter(Boolean);
16
+ }
17
+
18
+ function validateChoice(value, choices, flagName) {
19
+ if (!choices.includes(value)) {
20
+ throw new ValidationError(
21
+ `Invalid value for --${flagName}: "${value}". Must be one of: ${choices.join(", ")}`
22
+ );
23
+ }
24
+ return value;
25
+ }
26
+
27
+ function normalizeRepoRelativePath(rawValue, label) {
28
+ if (!rawValue || !String(rawValue).trim()) {
29
+ throw new ValidationError(`${label} must be a non-empty repo-relative path.`);
30
+ }
31
+
32
+ const normalized = String(rawValue).trim().replace(/\\/g, "/").replace(/^\.\//, "");
33
+ if (normalized.startsWith("/") || /^[A-Za-z]:/.test(normalized)) {
34
+ throw new ValidationError(`${label} must be repo-relative, not absolute: ${rawValue}`);
35
+ }
36
+ if (
37
+ normalized === "." ||
38
+ normalized.startsWith("../") ||
39
+ normalized.split("/").includes("..")
40
+ ) {
41
+ throw new ValidationError(`${label} must stay within the repository boundary: ${rawValue}`);
42
+ }
43
+
44
+ return normalized;
45
+ }
46
+
47
+ function rejectGlobPattern(rawValue, label) {
48
+ if (GLOB_PATTERN_RE.test(String(rawValue))) {
49
+ throw new ValidationError(`${label} must be an exact repo-relative path, not a glob pattern: ${rawValue}`);
50
+ }
51
+ }
52
+
53
+ function parseEvidenceEntries(entries, featureKey, projectPath) {
54
+ const normalized = normalizeEntries(entries);
55
+ if (normalized.length === 0) {
56
+ throw new ValidationError(
57
+ 'Verification requires at least one "--evidence" entry in the format "check|summary|pass|docs/dev/evidence/FEATURE_KEY/file.txt".'
58
+ );
59
+ }
60
+
61
+ const expectedPrefix = `docs/dev/evidence/${featureKey}/`;
62
+ return normalized.map((entry) => {
63
+ const parts = entry.split("|").map((part) => part.trim());
64
+ if (parts.length !== 4 || parts.some((part) => !part)) {
65
+ throw new ValidationError(
66
+ 'Each "--evidence" entry must use exactly four pipe-separated fields: "check|summary|pass|docs/dev/evidence/FEATURE_KEY/file.txt".'
67
+ );
68
+ }
69
+
70
+ const [checkName, summary, result, rawOutputRef] = parts;
71
+ validateChoice(result, VALID_EVIDENCE_RESULT, "evidence");
72
+
73
+ const normalizedRef = rawOutputRef.replace(/\\/g, "/");
74
+ rejectGlobPattern(normalizedRef, "evidence raw-output reference");
75
+ if (!normalizedRef.startsWith(expectedPrefix)) {
76
+ throw new ValidationError(
77
+ `Evidence raw-output reference must stay under "${expectedPrefix}". Received: ${rawOutputRef}`
78
+ );
79
+ }
80
+ if (!fs.existsSync(path.join(projectPath, normalizedRef.replace(/\//g, path.sep)))) {
81
+ throw new ValidationError(
82
+ `Evidence raw-output reference must exist before verify can claim the check ran. Missing: ${rawOutputRef}`
83
+ );
84
+ }
85
+
86
+ return {
87
+ checkName,
88
+ summary,
89
+ result,
90
+ rawOutputRef: normalizedRef,
91
+ };
92
+ });
93
+ }
94
+
95
+ function parseChangedFileEntries(entries) {
96
+ return normalizeEntries(entries).map((entry) => {
97
+ const parts = entry.split("|").map((part) => part.trim());
98
+ if (parts.length !== 2 || parts.some((part) => !part)) {
99
+ throw new ValidationError(
100
+ 'Each "--changed-file" entry must use exactly two pipe-separated fields: "repo/path|rationale".'
101
+ );
102
+ }
103
+
104
+ const normalizedPath = normalizeRepoRelativePath(parts[0], "changed-file path");
105
+ rejectGlobPattern(normalizedPath, "changed-file path");
106
+
107
+ return {
108
+ path: normalizedPath,
109
+ rationale: parts[1],
110
+ };
111
+ });
112
+ }
113
+
114
+ function parseExcludeFileEntries(entries) {
115
+ return normalizeEntries(entries).map((entry) => {
116
+ const parts = entry.split("|").map((part) => part.trim());
117
+ if (parts.length !== 2 || parts.some((part) => !part)) {
118
+ throw new ValidationError(
119
+ 'Each "--exclude-file" entry must use exactly two pipe-separated fields: "repo/path|reason".'
120
+ );
121
+ }
122
+
123
+ const normalizedPath = normalizeRepoRelativePath(parts[0], "exclude-file path");
124
+ rejectGlobPattern(normalizedPath, "exclude-file path");
125
+
126
+ return {
127
+ path: normalizedPath,
128
+ reason: parts[1],
129
+ };
130
+ });
131
+ }
132
+
133
+ function parseTruthSyncTargetEntries(entries) {
134
+ return normalizeEntries(entries).map((entry) => {
135
+ const parts = entry.split("|").map((part) => part.trim());
136
+ if (parts.length !== 3 || parts.some((part) => !part)) {
137
+ throw new ValidationError(
138
+ 'Each "--truth-sync-target" entry must use exactly three pipe-separated fields: "path-or-surface|touched|rationale".'
139
+ );
140
+ }
141
+
142
+ const [target, status, rationale] = parts;
143
+ validateChoice(status, VALID_TRUTH_SYNC_STATUS, "truth-sync-target");
144
+ return {
145
+ target,
146
+ status,
147
+ rationale,
148
+ };
149
+ });
150
+ }
151
+
152
+ function parseCommitBoundaryPaths(entries, flagName) {
153
+ return normalizeEntries(entries).map((entry) => {
154
+ const normalizedPath = normalizeRepoRelativePath(entry, `${flagName} path`);
155
+ rejectGlobPattern(normalizedPath, `${flagName} path`);
156
+ return normalizedPath;
157
+ });
158
+ }
159
+
160
+ function parseCaveatEntries(entries) {
161
+ return normalizeEntries(entries).map((entry) => {
162
+ const parts = entry.split("|").map((part) => part.trim());
163
+ if (parts.length !== 4 || parts.some((part) => !part)) {
164
+ throw new ValidationError(
165
+ 'Each "--caveat" entry must use exactly four pipe-separated fields: "ID|statement|impact|owner".'
166
+ );
167
+ }
168
+
169
+ return {
170
+ id: parts[0],
171
+ statement: parts[1],
172
+ impact: parts[2],
173
+ owner: parts[3],
174
+ };
175
+ });
176
+ }
177
+
178
+ function resolveEvidenceGate(evidenceEntries) {
179
+ if (evidenceEntries.some((entry) => entry.result === "fail")) {
180
+ return "fail";
181
+ }
182
+ if (evidenceEntries.some((entry) => entry.result === "partial")) {
183
+ return "partial";
184
+ }
185
+ return "pass";
186
+ }
187
+
188
+ function resolveReviewStatus(value, flagName) {
189
+ if (!value) {
190
+ return "pending";
191
+ }
192
+ return validateChoice(value, VALID_REVIEW_STATUS, flagName);
193
+ }
194
+
195
+ function validateVerifyState(state) {
196
+ if (state.intakeOutcome === "BLOCKED_MISSING_INPUTS") {
197
+ throw new ValidationError(
198
+ 'Workflow artifact is blocked by missing upstream inputs. Re-run "sdtk-code start" after the handoff inputs are ready.'
199
+ );
200
+ }
201
+
202
+ if (state.currentPhase === "build") {
203
+ if (state.phaseStatus !== "completed") {
204
+ throw new ValidationError(
205
+ 'Workflow must complete "build" before "verify". Run "sdtk-code build --complete" first.'
206
+ );
207
+ }
208
+ return;
209
+ }
210
+
211
+ if (state.currentPhase !== "verify") {
212
+ throw new ValidationError(
213
+ `Cannot run "verify" from current phase "${state.currentPhase}". Re-run the next legal workflow step instead.`
214
+ );
215
+ }
216
+ }
217
+
218
+ function resolveVerifyPhaseStatus(flags, state, evidenceGate, specGateStatus, qualityGateStatus) {
219
+ if (flags.complete && flags.blocked) {
220
+ throw new ValidationError('Use either "--complete" or "--blocked", not both.');
221
+ }
222
+
223
+ if (flags.complete) {
224
+ if (evidenceGate !== "pass" || specGateStatus !== "pass" || qualityGateStatus !== "pass") {
225
+ throw new ValidationError(
226
+ 'Verify can be marked complete only when evidence, spec/compliance, and quality gates all pass.'
227
+ );
228
+ }
229
+ return "completed";
230
+ }
231
+
232
+ if (
233
+ flags.blocked ||
234
+ evidenceGate === "fail" ||
235
+ specGateStatus === "fail" ||
236
+ qualityGateStatus === "fail"
237
+ ) {
238
+ return "blocked";
239
+ }
240
+
241
+ if (
242
+ state.currentPhase === "verify" &&
243
+ state.phaseStatus === "completed" &&
244
+ evidenceGate === "pass" &&
245
+ specGateStatus === "pass" &&
246
+ qualityGateStatus === "pass"
247
+ ) {
248
+ return "completed";
249
+ }
250
+
251
+ return "in_progress";
252
+ }
253
+
254
+ function getSuggestedEngineSkills(phaseStatus, evidenceGate, specGateStatus, qualityGateStatus) {
255
+ if (phaseStatus === "completed") {
256
+ return ["code-ship", "code-finish"];
257
+ }
258
+
259
+ const hasFailure =
260
+ evidenceGate === "fail" || specGateStatus === "fail" || qualityGateStatus === "fail";
261
+ const skills = ["code-verify", "code-review"];
262
+ if (hasFailure || phaseStatus === "blocked") {
263
+ skills.unshift("code-debug");
264
+ }
265
+ return skills;
266
+ }
267
+
268
+ function evaluateProvenanceConsistency(reviewPacket) {
269
+ const warnings = [];
270
+ if (!reviewPacket || !reviewPacket.hasReferenceDisclosure) {
271
+ warnings.push("review packet did not render the required Reference Disclosure section");
272
+ return warnings;
273
+ }
274
+
275
+ if (reviewPacket.provenanceMode === "missing") {
276
+ warnings.push("workflow plan is missing the required Provenance section or declaration");
277
+ }
278
+
279
+ if (
280
+ reviewPacket.provenanceMode === "external" &&
281
+ reviewPacket.referenceDisclosureMode !== "external"
282
+ ) {
283
+ warnings.push("workflow plan reports external references, but review packet disclosure mode is not external");
284
+ }
285
+
286
+ if (
287
+ reviewPacket.provenanceMode === "no_reference" &&
288
+ reviewPacket.referenceDisclosureMode !== "no_reference"
289
+ ) {
290
+ warnings.push("workflow plan reports no-reference declaration, but review packet disclosure does not mirror it");
291
+ }
292
+
293
+ return warnings;
294
+ }
295
+
296
+ function runVerify(projectPath, featureKey, flags) {
297
+ const state = loadWorkflowArtifactState(projectPath, featureKey);
298
+ validateVerifyState(state);
299
+
300
+ const evidenceEntries = parseEvidenceEntries(flags.evidence, featureKey, projectPath);
301
+ const evidenceGate = resolveEvidenceGate(evidenceEntries);
302
+ const specGateStatus = resolveReviewStatus(flags["spec-status"], "spec-status");
303
+ const qualityGateStatus = resolveReviewStatus(flags["quality-status"], "quality-status");
304
+ const phaseStatus = resolveVerifyPhaseStatus(
305
+ flags,
306
+ state,
307
+ evidenceGate,
308
+ specGateStatus,
309
+ qualityGateStatus
310
+ );
311
+ const nextPhase = phaseStatus === "completed" ? "ship" : "verify";
312
+ const blockingState =
313
+ phaseStatus === "blocked"
314
+ ? "blocked_verify_issue"
315
+ : phaseStatus === "completed"
316
+ ? "clear"
317
+ : "review_in_progress";
318
+ const suggestedEngineSkills = getSuggestedEngineSkills(
319
+ phaseStatus,
320
+ evidenceGate,
321
+ specGateStatus,
322
+ qualityGateStatus
323
+ );
324
+ const specNotes = normalizeEntries(flags["spec-note"]);
325
+ const qualityNotes = normalizeEntries(flags["quality-note"]);
326
+ const debugNotes = normalizeEntries(flags["debug-note"]);
327
+ const reviewPacket = createReviewPacket({
328
+ projectPath,
329
+ featureKey,
330
+ workflowText: state.text,
331
+ evidenceEntries,
332
+ specGateStatus,
333
+ qualityGateStatus,
334
+ specNotes,
335
+ qualityNotes,
336
+ debugNotes,
337
+ batchSummary: flags["batch-summary"] || "",
338
+ changedFiles: parseChangedFileEntries(flags["changed-file"]),
339
+ excludedFiles: parseExcludeFileEntries(flags["exclude-file"]),
340
+ truthSyncTargets: parseTruthSyncTargetEntries(flags["truth-sync-target"]),
341
+ commitInclude: parseCommitBoundaryPaths(flags["commit-include"], "commit-include"),
342
+ commitExclude: parseCommitBoundaryPaths(flags["commit-exclude"], "commit-exclude"),
343
+ caveats: parseCaveatEntries(flags.caveat),
344
+ reviewerGuidance: normalizeEntries(flags["reviewer-guidance"]),
345
+ });
346
+ const normalizedReviewPacketPath = path
347
+ .relative(projectPath, reviewPacket.reviewPacketPath)
348
+ .replace(/\\/g, "/");
349
+ const provenanceWarnings = evaluateProvenanceConsistency(reviewPacket);
350
+ const verifyDebugNotes = [
351
+ ...debugNotes,
352
+ ...provenanceWarnings.map((warning) => `Provenance warning: ${warning}`),
353
+ ];
354
+
355
+ const artifactPath = createOrUpdateWorkflowArtifact({
356
+ projectPath,
357
+ featureKey,
358
+ lane: state.lane,
359
+ currentPhase: "verify",
360
+ phaseStatus,
361
+ intakeOutcome: state.intakeOutcome,
362
+ nextPhase,
363
+ blockingState,
364
+ suggestedEngineSkills,
365
+ managedBlocks: ["METADATA", "VERIFY", "STATUS"],
366
+ evidenceEntries,
367
+ evidenceGate,
368
+ specGateStatus,
369
+ qualityGateStatus,
370
+ specNotes,
371
+ qualityNotes,
372
+ verifyDebugNotes,
373
+ reviewPacketPath: normalizedReviewPacketPath,
374
+ });
375
+
376
+ return {
377
+ artifactPath,
378
+ lane: state.lane,
379
+ phaseStatus,
380
+ nextPhase,
381
+ suggestedEngineSkills,
382
+ evidenceCount: evidenceEntries.length,
383
+ evidenceGate,
384
+ specGateStatus,
385
+ qualityGateStatus,
386
+ reviewPacketPath: normalizedReviewPacketPath,
387
+ provenanceWarnings,
388
+ };
389
+ }
390
+
391
+ module.exports = {
392
+ runVerify,
393
+ };