@koreanhole/kandev 0.0.0-stage → 0.1.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/README.md +367 -2
- package/bin/codex-acp.js +16 -0
- package/bin/kandev-auto-sync.mjs +101 -0
- package/bin/kandev-config.js +53 -0
- package/bin/kandev.js +35 -0
- package/config/config.yaml +6 -0
- package/config/docker/projects/opencodex-cli.json +79 -0
- package/config/docker/projects/samsung-bus-tracker.json +91 -0
- package/config/gateways.json +14 -0
- package/config/pipeline.json +150 -0
- package/config/workflows/sdlc.yaml +1019 -0
- package/docker/Dockerfile +68 -0
- package/docker/Dockerfile.opencodex-cli +44 -0
- package/docker/Dockerfile.samsung-bus-tracker +75 -0
- package/package.json +61 -4
- package/scripts/agent-tools-docker.mjs +223 -0
- package/scripts/baseline.mjs +20 -0
- package/scripts/codex-medium +2 -0
- package/scripts/codex-stage +22 -0
- package/scripts/gate.mjs +909 -0
- package/scripts/gateways/docker.mjs +64 -0
- package/scripts/gateways/speckit.mjs +78 -0
- package/scripts/install-prerequisites.mjs +48 -0
- package/scripts/lib/artifacts.mjs +990 -0
- package/scripts/lib/bugfix.mjs +325 -0
- package/scripts/lib/checksum.mjs +166 -0
- package/scripts/lib/docker-workspace.mjs +1209 -0
- package/scripts/lib/gateways.mjs +79 -0
- package/scripts/lib/kandev.mjs +273 -0
- package/scripts/lib/mcp.mjs +111 -0
- package/scripts/lib/pipeline.mjs +548 -0
- package/scripts/lib/planning-replan.mjs +103 -0
- package/scripts/lib/resume.mjs +207 -0
- package/scripts/lib/review-history.mjs +260 -0
- package/scripts/lib/stage-boundary.mjs +146 -0
- package/scripts/lib/stage-model.mjs +23 -0
- package/scripts/lib/task-worktree.mjs +40 -0
- package/scripts/measure-tokens.mjs +121 -0
- package/scripts/planning-tools.mjs +166 -0
- package/scripts/replan-planning.mjs +82 -0
- package/scripts/review-digest.mjs +240 -0
- package/scripts/run-android-e2e.mjs +116 -0
- package/scripts/run-code-review.mjs +281 -0
- package/scripts/run-detached.mjs +163 -0
- package/scripts/run-doc-review.mjs +155 -0
- package/scripts/run-gateways.mjs +138 -0
- package/scripts/sync.mjs +588 -0
- package/scripts/verify-change.mjs +252 -0
- package/scripts/verify-node.mjs +288 -0
- package/scripts/verify.mjs +442 -0
- package/scripts/wait-status.mjs +88 -0
|
@@ -0,0 +1,990 @@
|
|
|
1
|
+
/** Stage artifact validation shared by the gate daemon and offline checks. */
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { gatewayProblems } from "./gateways.mjs";
|
|
6
|
+
import { reviewSnapshotProblems } from "./review-history.mjs";
|
|
7
|
+
import {
|
|
8
|
+
BUGFIX_RUN_DIR,
|
|
9
|
+
contractProblems,
|
|
10
|
+
readContract,
|
|
11
|
+
readRun,
|
|
12
|
+
runProblems,
|
|
13
|
+
} from "./bugfix.mjs";
|
|
14
|
+
|
|
15
|
+
export const STAGE_ARTIFACTS = [
|
|
16
|
+
{ stage: "gateway", file: "gateway.json" },
|
|
17
|
+
{ stage: "planning", file: "spec.json" },
|
|
18
|
+
{ stage: "implementation", file: "implementation.json" },
|
|
19
|
+
{ stage: "review", file: "review.json" },
|
|
20
|
+
{ stage: "e2e", file: "e2e.json" },
|
|
21
|
+
{ stage: "human-approval", file: null },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export const PLANNING_STAGE = 1;
|
|
25
|
+
const REQUIRED_PLANNING_DOCS = ["spec.md", "plan.md", "tasks.md", "analysis.md", "checklists/requirements.md"];
|
|
26
|
+
|
|
27
|
+
function isObject(value) {
|
|
28
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function nonEmptyArray(value) {
|
|
32
|
+
return Array.isArray(value) && value.length > 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const TEST_PATH_RE = /(^|\/)(tests?|__tests__|androidTest|spec)\/|\.(test|spec)\.[A-Za-z0-9]+$/;
|
|
36
|
+
const SOURCE_PATH_RE = /\.(?:[cm]?js|jsx|tsx?|kt|kts|java|py|rb|go|rs|swift|cs|c|cc|cpp|h|hpp|php|vue|svelte|sql|sh|json|ya?ml|toml)$/i;
|
|
37
|
+
const BUG_MARKER_RE = /\[\s*bug\s*\]|\b(?:bug|bugfix|hotfix|regression)\s*:|\b(?:issue|bug)\s*#\d+|(?:버그|오류|에러|장애|회귀)/i;
|
|
38
|
+
|
|
39
|
+
function readJson(file) {
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function insideRepo(repoDir, rel) {
|
|
48
|
+
if (typeof rel !== "string" || !rel.trim()) return null;
|
|
49
|
+
const repo = fs.realpathSync(repoDir);
|
|
50
|
+
const target = path.resolve(repo, rel);
|
|
51
|
+
if (!target.startsWith(`${repo}${path.sep}`) && target !== repo) return null;
|
|
52
|
+
if (!fs.existsSync(target)) return null;
|
|
53
|
+
const real = fs.realpathSync(target);
|
|
54
|
+
if (!real.startsWith(`${repo}${path.sep}`)) return null;
|
|
55
|
+
return real;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function companionProblems(repoDir, name) {
|
|
59
|
+
const file = path.join(repoDir, ".agentic", name);
|
|
60
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) return [`${name} missing`];
|
|
61
|
+
if (fs.readFileSync(file, "utf8").trim().length === 0) return [`${name} is empty`];
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isValidIsoTimestamp(value) {
|
|
66
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|[+-]\d{2}:\d{2})$/.exec(value || "");
|
|
67
|
+
if (!match) return false;
|
|
68
|
+
const [, year, month, day, hour, minute, second, zone] = match;
|
|
69
|
+
const [y, mo, d, h, mi, s] = [year, month, day, hour, minute, second].map(Number);
|
|
70
|
+
if (zone !== "Z") {
|
|
71
|
+
const [offsetHour, offsetMinute] = zone.slice(1).split(":").map(Number);
|
|
72
|
+
if (offsetHour > 23 || offsetMinute > 59) return false;
|
|
73
|
+
}
|
|
74
|
+
if (!Number.isFinite(Date.parse(value))) return false;
|
|
75
|
+
const probe = new Date(Date.UTC(y, mo - 1, d, h, mi, s));
|
|
76
|
+
return (
|
|
77
|
+
probe.getUTCFullYear() === y &&
|
|
78
|
+
probe.getUTCMonth() === mo - 1 &&
|
|
79
|
+
probe.getUTCDate() === d &&
|
|
80
|
+
probe.getUTCHours() === h &&
|
|
81
|
+
probe.getUTCMinutes() === mi &&
|
|
82
|
+
probe.getUTCSeconds() === s
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function validateEnvelope(data) {
|
|
87
|
+
const problems = [];
|
|
88
|
+
if (typeof data.task !== "string" || !data.task.trim()) problems.push("task must be a non-empty string");
|
|
89
|
+
if (typeof data.timestamp !== "string" || !isValidIsoTimestamp(data.timestamp)) {
|
|
90
|
+
problems.push("timestamp must be a valid ISO-8601 string");
|
|
91
|
+
}
|
|
92
|
+
return problems;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function validRelativePath(value) {
|
|
96
|
+
if (typeof value !== "string" || !value.trim()) return false;
|
|
97
|
+
const normalized = path.posix.normalize(value.replaceAll("\\", "/"));
|
|
98
|
+
return normalized !== ".." && !normalized.startsWith("../") && !path.posix.isAbsolute(normalized);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function validateGateway(data) {
|
|
102
|
+
const problems = [];
|
|
103
|
+
if (data.stage !== "gateway") problems.push("stage must be gateway");
|
|
104
|
+
if (data.verdict !== "PASS") problems.push(`verdict is ${data.verdict ?? "missing"}, expected PASS`);
|
|
105
|
+
problems.push(...gatewayProblems(data));
|
|
106
|
+
if (!Array.isArray(data.application_files_changed)) {
|
|
107
|
+
problems.push("application_files_changed must be an array");
|
|
108
|
+
} else if (data.application_files_changed.length > 0) {
|
|
109
|
+
problems.push(`gateway changed application files: ${data.application_files_changed.slice(0, 5).join(", ")}`);
|
|
110
|
+
}
|
|
111
|
+
if (!Array.isArray(data.preexisting_changes)) {
|
|
112
|
+
problems.push("preexisting_changes must be an array");
|
|
113
|
+
} else if (data.preexisting_changes.length > 0) {
|
|
114
|
+
problems.push(`gateway started with application changes: ${data.preexisting_changes.slice(0, 5).join(", ")}`);
|
|
115
|
+
}
|
|
116
|
+
return problems;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function validateSpec(data) {
|
|
120
|
+
const problems = [];
|
|
121
|
+
if (data.stage !== "planning") problems.push("stage must be planning");
|
|
122
|
+
if (!["feature", "bugfix"].includes(data.change_type)) {
|
|
123
|
+
problems.push("change_type must be feature or bugfix");
|
|
124
|
+
}
|
|
125
|
+
if (typeof data.classification_reason !== "string" || !data.classification_reason.trim()) {
|
|
126
|
+
problems.push("classification_reason must be a non-empty string");
|
|
127
|
+
}
|
|
128
|
+
if (data.change_type === "feature" && BUG_MARKER_RE.test(String(data.task || ""))) {
|
|
129
|
+
problems.push("feature task contains a bug marker; use change_type bugfix");
|
|
130
|
+
}
|
|
131
|
+
if (!["web", "android", "node"].includes(data.platform)) {
|
|
132
|
+
problems.push("platform must be web, android, or node (use node for CLI or package repositories)");
|
|
133
|
+
}
|
|
134
|
+
if (!validRelativePath(data.feature_dir)) problems.push("feature_dir must be a relative path inside the worktree");
|
|
135
|
+
if (!nonEmptyArray(data.documents)) problems.push("documents is empty");
|
|
136
|
+
const documents = new Set();
|
|
137
|
+
for (const [index, file] of (data.documents || []).entries()) {
|
|
138
|
+
if (!validRelativePath(file)) {
|
|
139
|
+
problems.push(`documents[${index}] must be a relative path`);
|
|
140
|
+
} else if (documents.has(file)) {
|
|
141
|
+
problems.push(`documents contains duplicate path ${file}`);
|
|
142
|
+
} else {
|
|
143
|
+
documents.add(file);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (const required of REQUIRED_PLANNING_DOCS) {
|
|
147
|
+
if (!documents.has(required)) problems.push(`documents is missing ${required}`);
|
|
148
|
+
}
|
|
149
|
+
if (!nonEmptyArray(data.functional_requirements)) problems.push("functional_requirements is empty");
|
|
150
|
+
if (!nonEmptyArray(data.acceptance_criteria)) problems.push("acceptance_criteria is empty");
|
|
151
|
+
const frIds = new Set();
|
|
152
|
+
for (const [index, fr] of (data.functional_requirements || []).entries()) {
|
|
153
|
+
if (typeof fr?.id !== "string" || !fr.id.trim()) problems.push(`functional_requirements[${index}] lacks id`);
|
|
154
|
+
else if (frIds.has(fr.id)) problems.push(`duplicate functional requirement id ${fr.id}`);
|
|
155
|
+
else frIds.add(fr.id);
|
|
156
|
+
if (typeof fr?.text !== "string" || !fr.text.trim()) problems.push(`functional_requirements[${index}] lacks text`);
|
|
157
|
+
}
|
|
158
|
+
const acIds = new Set();
|
|
159
|
+
for (const [index, ac] of (data.acceptance_criteria || []).entries()) {
|
|
160
|
+
if (typeof ac?.id !== "string" || !ac.id.trim()) problems.push(`acceptance_criteria[${index}] lacks id`);
|
|
161
|
+
else if (acIds.has(ac.id)) problems.push(`duplicate acceptance criterion id ${ac.id}`);
|
|
162
|
+
else acIds.add(ac.id);
|
|
163
|
+
for (const key of ["given", "when", "then"]) {
|
|
164
|
+
if (typeof ac?.[key] !== "string" || !ac[key].trim()) {
|
|
165
|
+
problems.push(`acceptance_criteria[${index}] lacks ${key}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (!Array.isArray(data.out_of_scope)) problems.push("out_of_scope must be an array");
|
|
170
|
+
if (!Array.isArray(data.application_files_changed)) {
|
|
171
|
+
problems.push("application_files_changed must be an array");
|
|
172
|
+
} else if (data.application_files_changed.length > 0) {
|
|
173
|
+
problems.push(`planning changed application files: ${data.application_files_changed.slice(0, 5).join(", ")}`);
|
|
174
|
+
}
|
|
175
|
+
return problems;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function validateSpecReview(data) {
|
|
179
|
+
const problems = [];
|
|
180
|
+
if (data.stage !== "requirements-review") problems.push("stage must be requirements-review");
|
|
181
|
+
if (!["PASS", "FAIL"].includes(data.verdict)) problems.push("verdict must be PASS or FAIL");
|
|
182
|
+
if (typeof data.agent !== "string" || !data.agent.trim()) problems.push("agent must be a non-empty string");
|
|
183
|
+
for (const key of ["blockers", "majors", "minors"]) {
|
|
184
|
+
if (!Number.isInteger(data[key]) || data[key] < 0) {
|
|
185
|
+
problems.push(`${key} must be a non-negative integer`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (data.verdict !== "PASS") problems.push(`verdict is ${data.verdict ?? "missing"}, expected PASS`);
|
|
189
|
+
if (!Number.isInteger(data.rounds) || data.rounds < 1) problems.push("rounds must be a positive integer");
|
|
190
|
+
|
|
191
|
+
const blockers = Number(data.blockers);
|
|
192
|
+
const blockerIds = data.blocker_ids;
|
|
193
|
+
const hasAcceptedBlockers = data.verdict === "PASS"
|
|
194
|
+
&& Number.isInteger(data.rounds) && data.rounds >= 2
|
|
195
|
+
&& Number.isInteger(blockers) && blockers > 0
|
|
196
|
+
&& Array.isArray(blockerIds)
|
|
197
|
+
&& Array.isArray(data.accepted_blockers) && data.accepted_blockers.length > 0;
|
|
198
|
+
if (blockers > 0 && !hasAcceptedBlockers) {
|
|
199
|
+
problems.push(`blocker findings remain: ${data.blockers}`);
|
|
200
|
+
}
|
|
201
|
+
if (hasAcceptedBlockers) {
|
|
202
|
+
const seenBlockers = new Set();
|
|
203
|
+
for (const [index, id] of blockerIds.entries()) {
|
|
204
|
+
if (typeof id !== "string" || !id.trim()) {
|
|
205
|
+
problems.push(`blocker_ids[${index}] must be a non-empty string`);
|
|
206
|
+
} else if (seenBlockers.has(id)) {
|
|
207
|
+
problems.push(`blocker_ids contains duplicate id ${id}`);
|
|
208
|
+
} else {
|
|
209
|
+
seenBlockers.add(id);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (blockerIds.length !== blockers) {
|
|
213
|
+
problems.push(`blocker_ids has ${blockerIds.length} entries, expected ${blockers}`);
|
|
214
|
+
}
|
|
215
|
+
const acceptedBlockerIds = new Set();
|
|
216
|
+
for (const [index, entry] of data.accepted_blockers.entries()) {
|
|
217
|
+
const id = entry?.id;
|
|
218
|
+
if (typeof id !== "string" || !id.trim()) {
|
|
219
|
+
problems.push(`accepted_blockers[${index}] lacks id`);
|
|
220
|
+
} else if (acceptedBlockerIds.has(id)) {
|
|
221
|
+
problems.push(`accepted_blockers contains duplicate id ${id}`);
|
|
222
|
+
} else {
|
|
223
|
+
acceptedBlockerIds.add(id);
|
|
224
|
+
if (!blockerIds.includes(id)) {
|
|
225
|
+
problems.push(`accepted_blockers id ${id} is not in blocker_ids`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (typeof entry?.reason !== "string" || !entry.reason.trim()) {
|
|
229
|
+
problems.push(`accepted_blockers[${index}] lacks reason`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (const id of blockerIds) {
|
|
233
|
+
if (typeof id === "string" && id.trim() && !acceptedBlockerIds.has(id)) {
|
|
234
|
+
problems.push(`blocker ${id} has no accepted_blockers entry`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
} else if (blockers === 0) {
|
|
238
|
+
if (Array.isArray(blockerIds) && blockerIds.length !== 0) {
|
|
239
|
+
problems.push(`blocker_ids has ${blockerIds.length} entries, expected 0`);
|
|
240
|
+
}
|
|
241
|
+
if (Array.isArray(data.accepted_blockers) && data.accepted_blockers.length !== 0) {
|
|
242
|
+
problems.push("accepted_blockers must be empty when blockers is 0");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const majorIds = data.major_ids;
|
|
247
|
+
if (!Array.isArray(majorIds)) {
|
|
248
|
+
problems.push("major_ids must be an array");
|
|
249
|
+
} else {
|
|
250
|
+
const seen = new Set();
|
|
251
|
+
for (const [index, id] of majorIds.entries()) {
|
|
252
|
+
if (typeof id !== "string" || !id.trim()) {
|
|
253
|
+
problems.push(`major_ids[${index}] must be a non-empty string`);
|
|
254
|
+
} else if (seen.has(id)) {
|
|
255
|
+
problems.push(`major_ids contains duplicate id ${id}`);
|
|
256
|
+
} else {
|
|
257
|
+
seen.add(id);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const history = data.major_history;
|
|
263
|
+
if (!Array.isArray(history)) {
|
|
264
|
+
problems.push("major_history must be an array");
|
|
265
|
+
} else {
|
|
266
|
+
for (const [index, count] of history.entries()) {
|
|
267
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
268
|
+
problems.push(`major_history[${index}] must be a non-negative integer`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const majors = Number(data.majors);
|
|
274
|
+
if (data.verdict === "PASS" && Number.isInteger(majors) && majors > 0) {
|
|
275
|
+
if (Array.isArray(majorIds) && majorIds.length !== majors) {
|
|
276
|
+
problems.push(`major_ids has ${majorIds.length} entries, expected ${majors}`);
|
|
277
|
+
}
|
|
278
|
+
if (!Array.isArray(data.accepted_majors)) {
|
|
279
|
+
problems.push("accepted_majors must be an array when majors > 0");
|
|
280
|
+
} else {
|
|
281
|
+
const acceptedIds = new Set();
|
|
282
|
+
for (const [index, entry] of data.accepted_majors.entries()) {
|
|
283
|
+
const id = entry?.id;
|
|
284
|
+
if (typeof id !== "string" || !id.trim()) {
|
|
285
|
+
problems.push(`accepted_majors[${index}] lacks id`);
|
|
286
|
+
} else if (acceptedIds.has(id)) {
|
|
287
|
+
problems.push(`accepted_majors contains duplicate id ${id}`);
|
|
288
|
+
} else {
|
|
289
|
+
acceptedIds.add(id);
|
|
290
|
+
if (Array.isArray(majorIds) && !majorIds.includes(id)) {
|
|
291
|
+
problems.push(`accepted_majors id ${id} is not in major_ids`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (typeof entry?.reason !== "string" || !entry.reason.trim()) {
|
|
295
|
+
problems.push(`accepted_majors[${index}] lacks reason`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (Array.isArray(majorIds)) {
|
|
299
|
+
for (const id of majorIds) {
|
|
300
|
+
if (typeof id === "string" && id.trim() && !acceptedIds.has(id)) {
|
|
301
|
+
problems.push(`major ${id} has no accepted_majors entry`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
} else if (majors === 0) {
|
|
307
|
+
if (Array.isArray(majorIds) && majorIds.length !== 0) {
|
|
308
|
+
problems.push(`major_ids has ${majorIds.length} entries, expected 0`);
|
|
309
|
+
}
|
|
310
|
+
if (Array.isArray(data.accepted_majors) && data.accepted_majors.length !== 0) {
|
|
311
|
+
problems.push("accepted_majors must be empty when majors is 0");
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (Array.isArray(history)) {
|
|
315
|
+
if (history.length < 1) {
|
|
316
|
+
problems.push("major_history must contain at least one round");
|
|
317
|
+
} else {
|
|
318
|
+
const last = history[history.length - 1];
|
|
319
|
+
if (last !== majors) problems.push(`major_history final entry ${last} does not equal majors ${majors}`);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return problems;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function validatePlanningReview(data) {
|
|
326
|
+
const problems = [];
|
|
327
|
+
if (data.stage !== "planning-review") problems.push("stage must be planning-review");
|
|
328
|
+
problems.push(...validateSpecReview({ ...data, stage: "requirements-review" }));
|
|
329
|
+
|
|
330
|
+
const reviewed = data.reviewed_documents;
|
|
331
|
+
if (!Array.isArray(reviewed) || reviewed.length === 0) {
|
|
332
|
+
problems.push("reviewed_documents must be a non-empty array");
|
|
333
|
+
} else {
|
|
334
|
+
const seen = new Set();
|
|
335
|
+
for (const [index, entry] of reviewed.entries()) {
|
|
336
|
+
const file = entry?.path;
|
|
337
|
+
if (!validRelativePath(file)) problems.push(`reviewed_documents[${index}] lacks a relative path`);
|
|
338
|
+
else if (seen.has(file)) problems.push(`reviewed_documents contains duplicate path ${file}`);
|
|
339
|
+
else seen.add(file);
|
|
340
|
+
if (typeof entry?.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(entry.sha256)) {
|
|
341
|
+
problems.push(`reviewed_documents[${index}] lacks a sha256`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const reviewers = data.reviewers;
|
|
347
|
+
if (!isObject(reviewers)) {
|
|
348
|
+
problems.push("reviewers must be an object");
|
|
349
|
+
return problems;
|
|
350
|
+
}
|
|
351
|
+
const requirement = reviewers.requirement_agent;
|
|
352
|
+
if (!isObject(requirement)) {
|
|
353
|
+
problems.push("reviewers.requirement_agent is missing");
|
|
354
|
+
} else {
|
|
355
|
+
if (requirement.package !== "@koreanhole/requirement-agent@0.1.7") {
|
|
356
|
+
problems.push("reviewers.requirement_agent.package must be @koreanhole/requirement-agent@0.1.7");
|
|
357
|
+
}
|
|
358
|
+
const expectedRoles = ["ambiguity", "testability", "consistency"];
|
|
359
|
+
if (JSON.stringify(requirement.roles) !== JSON.stringify(expectedRoles)) {
|
|
360
|
+
problems.push("reviewers.requirement_agent.roles must be ambiguity,testability,consistency");
|
|
361
|
+
}
|
|
362
|
+
if (typeof requirement.run_id !== "string" || !requirement.run_id.trim()) {
|
|
363
|
+
problems.push("reviewers.requirement_agent.run_id is missing");
|
|
364
|
+
}
|
|
365
|
+
if (requirement.status !== "completed") {
|
|
366
|
+
problems.push("reviewers.requirement_agent.status must be completed");
|
|
367
|
+
}
|
|
368
|
+
for (const key of ["blockers", "majors", "minors"]) {
|
|
369
|
+
if (!Number.isInteger(requirement[key]) || requirement[key] < 0) {
|
|
370
|
+
problems.push(`reviewers.requirement_agent.${key} must be a non-negative integer`);
|
|
371
|
+
} else if (requirement[key] !== data[key]) {
|
|
372
|
+
problems.push(`reviewers.requirement_agent.${key} does not match ${key}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const analyze = reviewers.speckit_analyze;
|
|
378
|
+
if (!isObject(analyze)) {
|
|
379
|
+
problems.push("reviewers.speckit_analyze is missing");
|
|
380
|
+
} else {
|
|
381
|
+
if (analyze.skill !== "$speckit-analyze") problems.push("reviewers.speckit_analyze.skill must be $speckit-analyze");
|
|
382
|
+
if (analyze.report !== ".agentic/speckit-analyze.md") {
|
|
383
|
+
problems.push("reviewers.speckit_analyze.report must be .agentic/speckit-analyze.md");
|
|
384
|
+
}
|
|
385
|
+
if (analyze.status !== "completed") problems.push("reviewers.speckit_analyze.status must be completed");
|
|
386
|
+
if (analyze.verdict !== "PASS") problems.push("reviewers.speckit_analyze.verdict must be PASS");
|
|
387
|
+
for (const key of ["critical", "major", "minor"]) {
|
|
388
|
+
if (!Number.isInteger(analyze[key]) || analyze[key] < 0) {
|
|
389
|
+
problems.push(`reviewers.speckit_analyze.${key} must be a non-negative integer`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (analyze.critical !== 0) problems.push(`reviewers.speckit_analyze.critical must be 0`);
|
|
393
|
+
if (analyze.major !== 0) problems.push(`reviewers.speckit_analyze.major must be 0`);
|
|
394
|
+
}
|
|
395
|
+
return problems;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function fileHash(file) {
|
|
399
|
+
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function planningHandoffProblems(repoDir, spec, review) {
|
|
403
|
+
const problems = [];
|
|
404
|
+
const feature = insideRepo(repoDir, spec.feature_dir);
|
|
405
|
+
if (!feature || !fs.statSync(feature).isDirectory()) {
|
|
406
|
+
return [`feature_dir is not a directory inside the worktree: ${spec.feature_dir}`];
|
|
407
|
+
}
|
|
408
|
+
const realFeature = fs.realpathSync(feature);
|
|
409
|
+
const featureJson = path.join(repoDir, ".specify", "feature.json");
|
|
410
|
+
const featureData = readJson(featureJson);
|
|
411
|
+
if (!isObject(featureData) || typeof featureData.feature_directory !== "string") {
|
|
412
|
+
problems.push(".specify/feature.json is missing or lacks feature_directory");
|
|
413
|
+
} else if (path.posix.normalize(featureData.feature_directory.replaceAll("\\", "/")) !== spec.feature_dir) {
|
|
414
|
+
problems.push(`.specify/feature.json points at ${featureData.feature_directory}, expected ${spec.feature_dir}`);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const documents = new Map((spec.documents || []).map((file) => [file, path.join(realFeature, file)]));
|
|
418
|
+
const realRepo = fs.realpathSync(repoDir);
|
|
419
|
+
for (const required of REQUIRED_PLANNING_DOCS) {
|
|
420
|
+
if (!documents.has(required)) problems.push(`documents is missing ${required}`);
|
|
421
|
+
}
|
|
422
|
+
for (const [file, abs] of documents) {
|
|
423
|
+
const real = insideRepo(repoDir, path.relative(realRepo, abs));
|
|
424
|
+
if (!real || !fs.statSync(real).isFile()) {
|
|
425
|
+
problems.push(`planning document missing: ${file}`);
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
if (!real.startsWith(`${realFeature}${path.sep}`)) {
|
|
429
|
+
problems.push(`planning document escapes feature_dir: ${file}`);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (fs.readFileSync(real, "utf8").trim().length === 0) problems.push(`planning document is empty: ${file}`);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const specFile = documents.get("spec.md");
|
|
436
|
+
if (specFile && fs.existsSync(specFile)) {
|
|
437
|
+
const markdown = fs.readFileSync(specFile, "utf8");
|
|
438
|
+
for (const fr of spec.functional_requirements || []) {
|
|
439
|
+
if (!markdown.includes(fr.id)) problems.push(`FR ${fr.id} is not present in spec.md`);
|
|
440
|
+
}
|
|
441
|
+
for (const ac of spec.acceptance_criteria || []) {
|
|
442
|
+
if (!markdown.includes(ac.id)) problems.push(`AC ${ac.id} is not present in spec.md`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
const tasksFile = documents.get("tasks.md");
|
|
446
|
+
if (tasksFile && fs.existsSync(tasksFile) && !/^\s*-\s*\[[ xX]\]/m.test(fs.readFileSync(tasksFile, "utf8"))) {
|
|
447
|
+
problems.push("tasks.md contains no task checkboxes");
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const checklistDir = path.join(realFeature, "checklists");
|
|
451
|
+
if (!fs.existsSync(checklistDir) || !fs.statSync(checklistDir).isDirectory()) {
|
|
452
|
+
problems.push("checklists directory is missing");
|
|
453
|
+
} else {
|
|
454
|
+
const files = fs.readdirSync(checklistDir).filter((name) => name.endsWith(".md"));
|
|
455
|
+
if (files.length === 0) problems.push("checklists contains no Markdown review");
|
|
456
|
+
let unchecked = 0;
|
|
457
|
+
for (const name of files) {
|
|
458
|
+
const text = fs.readFileSync(path.join(checklistDir, name), "utf8");
|
|
459
|
+
unchecked += (text.match(/^\s*-\s*\[ \]/gm) || []).length;
|
|
460
|
+
}
|
|
461
|
+
if (unchecked > 0) problems.push(`${unchecked} checklist item(s) remain unchecked`);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
problems.push(...reviewSnapshotProblems(repoDir, feature, spec.documents || [], review));
|
|
465
|
+
|
|
466
|
+
const analyzeFile = path.join(repoDir, ".agentic", "speckit-analyze.md");
|
|
467
|
+
if (!fs.existsSync(analyzeFile) || !fs.statSync(analyzeFile).isFile()) {
|
|
468
|
+
problems.push("speckit-analyze.md is missing");
|
|
469
|
+
} else if (fs.readFileSync(analyzeFile, "utf8").trim().length === 0) {
|
|
470
|
+
problems.push("speckit-analyze.md is empty");
|
|
471
|
+
}
|
|
472
|
+
return problems;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function bugfixRunBlockProblems(repoDir, block, { phase, status, scenarioId, requireRegression = false } = {}) {
|
|
476
|
+
const problems = [];
|
|
477
|
+
const expected = `${BUGFIX_RUN_DIR}/${phase}.json`;
|
|
478
|
+
if (!isObject(block)) return [`bugfix.${phase} run block is missing`];
|
|
479
|
+
if (block.phase !== phase) problems.push(`bugfix.${phase} block phase must be ${phase}`);
|
|
480
|
+
if (block.result !== expected) problems.push(`bugfix.${phase} block result must be ${expected}`);
|
|
481
|
+
const loaded = readRun(repoDir, phase);
|
|
482
|
+
problems.push(...loaded.problems.map((problem) => `bugfix.${phase}: ${problem}`));
|
|
483
|
+
if (loaded.data) {
|
|
484
|
+
problems.push(...runProblems(repoDir, loaded.data, {
|
|
485
|
+
phase,
|
|
486
|
+
status,
|
|
487
|
+
scenarioId,
|
|
488
|
+
requireRegression,
|
|
489
|
+
}).map((problem) => `bugfix.${phase}: ${problem}`));
|
|
490
|
+
}
|
|
491
|
+
return problems;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function bugfixRegressionProblems(repoDir, regression, contract) {
|
|
495
|
+
const problems = [];
|
|
496
|
+
if (!isObject(regression)) return ["bugfix.regression_test is missing"];
|
|
497
|
+
if (regression.exit_code !== 0) problems.push("bugfix.regression_test.exit_code is not 0");
|
|
498
|
+
const expected = contract?.regression_test || {};
|
|
499
|
+
if (regression.path !== expected.path) problems.push("bugfix.regression_test.path does not match bugfix.json");
|
|
500
|
+
if (JSON.stringify(regression.command) !== JSON.stringify(expected.command)) {
|
|
501
|
+
problems.push("bugfix.regression_test.command does not match bugfix.json");
|
|
502
|
+
}
|
|
503
|
+
const target = insideRepo(repoDir, regression.path);
|
|
504
|
+
if (!target || !fs.statSync(target).isFile()) {
|
|
505
|
+
problems.push(`bugfix.regression_test.path is missing: ${regression.path}`);
|
|
506
|
+
} else if (regression.sha256 !== fileHash(target)) {
|
|
507
|
+
problems.push("bugfix.regression_test hash changed");
|
|
508
|
+
}
|
|
509
|
+
return problems;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function validateBugfixImplementation(data, spec, repoDir, bugfix) {
|
|
513
|
+
if (spec?.change_type !== "bugfix") return [];
|
|
514
|
+
if (!repoDir) return ["bugfix implementation validation requires repoDir"];
|
|
515
|
+
if (!isObject(data.bugfix)) return ["bugfix block missing"];
|
|
516
|
+
const problems = [];
|
|
517
|
+
const scenarioId = bugfix?.reproduction?.scenario_id;
|
|
518
|
+
problems.push(...bugfixRunBlockProblems(repoDir, data.bugfix.before, {
|
|
519
|
+
phase: "implementation-before",
|
|
520
|
+
status: "failed",
|
|
521
|
+
scenarioId,
|
|
522
|
+
}));
|
|
523
|
+
problems.push(...bugfixRunBlockProblems(repoDir, data.bugfix.after, {
|
|
524
|
+
phase: "implementation-after",
|
|
525
|
+
status: "passed",
|
|
526
|
+
scenarioId,
|
|
527
|
+
requireRegression: true,
|
|
528
|
+
}));
|
|
529
|
+
problems.push(...bugfixRegressionProblems(repoDir, data.bugfix.regression_test, bugfix));
|
|
530
|
+
if (Array.isArray(data.files_changed) && !data.files_changed.includes(bugfix?.regression_test?.path)) {
|
|
531
|
+
problems.push(`files_changed is missing regression test ${bugfix?.regression_test?.path}`);
|
|
532
|
+
}
|
|
533
|
+
return problems;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function validateImplementation(data, spec = null, repoDir = null, bugfix = null) {
|
|
537
|
+
const problems = [];
|
|
538
|
+
if (data.stage !== "implementation") problems.push("stage must be implementation");
|
|
539
|
+
if (data.verdict !== undefined && data.verdict !== "PASS") {
|
|
540
|
+
problems.push(`verdict is ${data.verdict ?? "missing"}, expected PASS when provided`);
|
|
541
|
+
}
|
|
542
|
+
if (!nonEmptyArray(data.files_changed)) problems.push("files_changed is empty");
|
|
543
|
+
for (const key of ["build", "lint"]) {
|
|
544
|
+
const entry = data[key];
|
|
545
|
+
if (!isObject(entry)) {
|
|
546
|
+
problems.push(`${key} result missing`);
|
|
547
|
+
continue;
|
|
548
|
+
}
|
|
549
|
+
if (typeof entry.command !== "string" || !entry.command.trim()) problems.push(`${key}.command missing`);
|
|
550
|
+
if (!Number.isInteger(entry.exit_code)) problems.push(`${key}.exit_code missing`);
|
|
551
|
+
else if (entry.exit_code === 127 && entry.command !== "not-configured") {
|
|
552
|
+
problems.push(`${key}.exit_code 127 is only valid when command is not-configured`);
|
|
553
|
+
} else if (entry.exit_code !== 0 && entry.exit_code !== 127) {
|
|
554
|
+
problems.push(`${key}.exit_code ${entry.exit_code} is not 0 or not-configured (127)`);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
const hasTestFiles = Array.isArray(data.files_changed) && data.files_changed.some((file) =>
|
|
558
|
+
typeof file === "string" && TEST_PATH_RE.test(file),
|
|
559
|
+
);
|
|
560
|
+
const hasSourceFiles = Array.isArray(data.files_changed) && data.files_changed.some((file) =>
|
|
561
|
+
typeof file === "string" && SOURCE_PATH_RE.test(file),
|
|
562
|
+
);
|
|
563
|
+
if (spec?.change_type === "feature" && hasSourceFiles && !hasTestFiles) {
|
|
564
|
+
problems.push("feature changed source files but no permanent test file; add or update a test");
|
|
565
|
+
}
|
|
566
|
+
if (data.test === undefined || data.test === null) {
|
|
567
|
+
if (hasTestFiles) problems.push("test result missing for changed test files");
|
|
568
|
+
} else if (!isObject(data.test)) {
|
|
569
|
+
problems.push("test result missing");
|
|
570
|
+
} else {
|
|
571
|
+
const entry = data.test;
|
|
572
|
+
if (typeof entry.command !== "string" || !entry.command.trim()) problems.push("test.command missing");
|
|
573
|
+
if (!Number.isInteger(entry.exit_code)) {
|
|
574
|
+
problems.push("test.exit_code missing");
|
|
575
|
+
} else if (entry.exit_code === 127 && entry.command !== "not-configured") {
|
|
576
|
+
problems.push("test.exit_code 127 is only valid when command is not-configured");
|
|
577
|
+
} else if (entry.exit_code !== 0 && entry.exit_code !== 127) {
|
|
578
|
+
problems.push(`test.exit_code ${entry.exit_code} is not 0 or not-configured (127)`);
|
|
579
|
+
}
|
|
580
|
+
if (hasTestFiles && entry.command === "not-configured") {
|
|
581
|
+
problems.push("test.command must be configured when test files changed");
|
|
582
|
+
}
|
|
583
|
+
if (hasTestFiles && entry.exit_code !== 0) {
|
|
584
|
+
problems.push(`test.exit_code ${entry.exit_code ?? "missing"} is not 0 for changed test files`);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
problems.push(...validateBugfixImplementation(data, spec, repoDir, bugfix));
|
|
588
|
+
return problems;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function validateReview(data, spec, repoDir) {
|
|
592
|
+
const problems = [];
|
|
593
|
+
if (data.stage !== "review") problems.push("stage must be review");
|
|
594
|
+
if (data.verdict !== "PASS") problems.push(`verdict is ${data.verdict ?? "missing"}, expected PASS`);
|
|
595
|
+
if (typeof data.run_id !== "string" || !data.run_id.trim()) problems.push("run_id must be a non-empty string");
|
|
596
|
+
if (!Array.isArray(data.coverage) || data.coverage.length === 0) {
|
|
597
|
+
problems.push("coverage is empty");
|
|
598
|
+
return problems;
|
|
599
|
+
}
|
|
600
|
+
if (!Array.isArray(data.findings)) problems.push("findings must be an array");
|
|
601
|
+
const classification = data.classification;
|
|
602
|
+
if (!isObject(classification)) {
|
|
603
|
+
problems.push("classification block is missing");
|
|
604
|
+
} else {
|
|
605
|
+
if (!["feature", "bugfix"].includes(classification.change_type)) {
|
|
606
|
+
problems.push("classification.change_type must be feature or bugfix");
|
|
607
|
+
} else if (spec?.change_type && classification.change_type !== spec.change_type) {
|
|
608
|
+
problems.push(`classification ${classification.change_type} does not match spec.json ${spec.change_type}`);
|
|
609
|
+
}
|
|
610
|
+
if (typeof classification.reason !== "string" || !classification.reason.trim()) {
|
|
611
|
+
problems.push("classification.reason must be a non-empty string");
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
for (const finding of Array.isArray(data.findings) ? data.findings : []) {
|
|
615
|
+
if (["blocker", "bug"].includes(finding?.severity)) {
|
|
616
|
+
problems.push(`blocking finding remains: ${finding.severity}`);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
const requirements = new Set();
|
|
620
|
+
for (const [index, entry] of data.coverage.entries()) {
|
|
621
|
+
if (typeof entry?.requirement !== "string" || !entry.requirement.trim()) {
|
|
622
|
+
problems.push(`coverage[${index}] lacks requirement`);
|
|
623
|
+
} else if (requirements.has(entry.requirement)) {
|
|
624
|
+
problems.push(`coverage contains duplicate requirement ${entry.requirement}`);
|
|
625
|
+
} else {
|
|
626
|
+
requirements.add(entry.requirement);
|
|
627
|
+
}
|
|
628
|
+
const evidence = typeof entry?.evidence === "string" ? entry.evidence.trim() : "";
|
|
629
|
+
if (!evidence) {
|
|
630
|
+
problems.push(`coverage ${entry?.requirement ?? index} lacks evidence`);
|
|
631
|
+
} else {
|
|
632
|
+
const match = /^(.*):([1-9][0-9]*)$/.exec(evidence);
|
|
633
|
+
if (!match || !match[1].trim()) {
|
|
634
|
+
problems.push(`coverage ${entry?.requirement ?? index} evidence must be path:line: ${evidence}`);
|
|
635
|
+
} else if (repoDir) {
|
|
636
|
+
const file = insideRepo(repoDir, match[1]);
|
|
637
|
+
if (!file || !fs.statSync(file).isFile()) {
|
|
638
|
+
problems.push(`coverage ${entry?.requirement ?? index} evidence is not a file inside the worktree: ${match[1]}`);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
const covered = new Set(
|
|
644
|
+
data.coverage.filter((entry) => entry?.status === "met").map((entry) => entry.requirement),
|
|
645
|
+
);
|
|
646
|
+
for (const entry of data.coverage) {
|
|
647
|
+
if (entry?.status !== "met") {
|
|
648
|
+
problems.push(`coverage ${entry?.requirement ?? "?"} is ${entry?.status ?? "missing"}, expected met`);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
for (const fr of spec?.functional_requirements || []) {
|
|
652
|
+
if (!covered.has(fr.id)) problems.push(`FR ${fr.id} missing from coverage`);
|
|
653
|
+
}
|
|
654
|
+
for (const ac of spec?.acceptance_criteria || []) {
|
|
655
|
+
if (!covered.has(ac.id)) problems.push(`AC ${ac.id} missing from coverage`);
|
|
656
|
+
}
|
|
657
|
+
if (spec?.change_type === "bugfix") {
|
|
658
|
+
if (!isObject(data.bugfix)) {
|
|
659
|
+
problems.push("bugfix review block is missing");
|
|
660
|
+
} else {
|
|
661
|
+
for (const key of ["root_cause_addressed", "reproduction_unchanged", "regression_test_meaningful"]) {
|
|
662
|
+
if (data.bugfix[key] !== true) problems.push(`bugfix.${key} must be true`);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
return problems;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function validateE2e(data, spec, repoDir, bugfixContract = null) {
|
|
670
|
+
const problems = [];
|
|
671
|
+
if (data.stage !== "e2e") problems.push("stage must be e2e");
|
|
672
|
+
if (data.verdict !== "PASS") problems.push(`verdict is ${data.verdict ?? "missing"}, expected PASS`);
|
|
673
|
+
if (!["web", "android", "node"].includes(data.platform)) {
|
|
674
|
+
problems.push("platform must be web, android, or node (use node for CLI or package repositories)");
|
|
675
|
+
}
|
|
676
|
+
if (spec?.platform && data.platform !== spec.platform) {
|
|
677
|
+
problems.push(`platform ${data.platform ?? "missing"} does not match spec.platform ${spec.platform}`);
|
|
678
|
+
}
|
|
679
|
+
if (!nonEmptyArray(data.scenarios)) {
|
|
680
|
+
problems.push("scenarios is empty");
|
|
681
|
+
if (data.platform === "node") validateNodeE2e(data, repoDir, problems);
|
|
682
|
+
return problems;
|
|
683
|
+
}
|
|
684
|
+
for (const [index, scenario] of (Array.isArray(data.scenarios) ? data.scenarios : []).entries()) {
|
|
685
|
+
if (scenario?.status !== "passed") {
|
|
686
|
+
problems.push(`scenarios[${index}] is ${scenario?.status ?? "missing"}, expected passed`);
|
|
687
|
+
}
|
|
688
|
+
if (typeof scenario?.evidence !== "string" || !scenario.evidence.trim()) {
|
|
689
|
+
problems.push(`scenarios[${index}] lacks evidence`);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
for (const ac of spec?.acceptance_criteria || []) {
|
|
693
|
+
const hit = data.scenarios.some((scenario) => {
|
|
694
|
+
const scenarioId = scenario?.ac_id === undefined ? "" : String(scenario.ac_id).trim();
|
|
695
|
+
if (scenarioId) {
|
|
696
|
+
if (scenarioId === ac.id) return true;
|
|
697
|
+
if (/^AC-\d+/.test(scenarioId)) return false;
|
|
698
|
+
}
|
|
699
|
+
const name = String(scenario?.name || "").trim();
|
|
700
|
+
return name === ac.id || name.startsWith(`${ac.id} `) || name.startsWith(`${ac.id}:`) || name.startsWith(`${ac.id})`);
|
|
701
|
+
});
|
|
702
|
+
if (!hit) problems.push(`AC ${ac.id} has no scenario`);
|
|
703
|
+
}
|
|
704
|
+
if (typeof data.report_path !== "string" || !data.report_path.trim()) {
|
|
705
|
+
problems.push("report_path missing");
|
|
706
|
+
} else if (repoDir) {
|
|
707
|
+
const report = insideRepo(repoDir, data.report_path);
|
|
708
|
+
if (!report || !fs.statSync(report).isFile()) {
|
|
709
|
+
problems.push(`report_path is not a file inside the worktree: ${data.report_path}`);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
for (const [index, scenario] of data.scenarios.entries()) {
|
|
713
|
+
const evidence = scenario?.evidence;
|
|
714
|
+
if (typeof evidence !== "string" || !evidence.trim()) continue;
|
|
715
|
+
const file = evidence.split("#")[0];
|
|
716
|
+
if (!file) {
|
|
717
|
+
problems.push(`scenarios[${index}] evidence is an anchor without a report file`);
|
|
718
|
+
continue;
|
|
719
|
+
}
|
|
720
|
+
if (repoDir) {
|
|
721
|
+
const resolved = insideRepo(repoDir, file);
|
|
722
|
+
if (!resolved || !fs.statSync(resolved).isFile()) {
|
|
723
|
+
problems.push(`scenarios[${index}] evidence is not a file inside the worktree: ${file}`);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
if (spec?.change_type === "bugfix") {
|
|
728
|
+
if (!repoDir) {
|
|
729
|
+
problems.push("bugfix e2e validation requires repoDir");
|
|
730
|
+
return problems;
|
|
731
|
+
}
|
|
732
|
+
const e2eBugfix = data.bugfix;
|
|
733
|
+
if (!isObject(e2eBugfix)) {
|
|
734
|
+
problems.push("bugfix e2e block is missing");
|
|
735
|
+
} else {
|
|
736
|
+
const scenarioId = bugfixContract?.reproduction?.scenario_id;
|
|
737
|
+
problems.push(...bugfixRunBlockProblems(repoDir, e2eBugfix.planning_before, {
|
|
738
|
+
phase: "planning-before",
|
|
739
|
+
status: "failed",
|
|
740
|
+
scenarioId,
|
|
741
|
+
}));
|
|
742
|
+
problems.push(...bugfixRunBlockProblems(repoDir, e2eBugfix.implementation_before, {
|
|
743
|
+
phase: "implementation-before",
|
|
744
|
+
status: "failed",
|
|
745
|
+
scenarioId,
|
|
746
|
+
}));
|
|
747
|
+
problems.push(...bugfixRunBlockProblems(repoDir, e2eBugfix.after, {
|
|
748
|
+
phase: "e2e-after",
|
|
749
|
+
status: "passed",
|
|
750
|
+
scenarioId,
|
|
751
|
+
requireRegression: true,
|
|
752
|
+
}));
|
|
753
|
+
problems.push(...bugfixRegressionProblems(repoDir, e2eBugfix.regression_test, bugfixContract));
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
if (data.platform === "node") validateNodeE2e(data, repoDir, problems);
|
|
757
|
+
return problems;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
function hasCommand(commands, command, exitCode) {
|
|
761
|
+
return commands.some(
|
|
762
|
+
(entry) => isObject(entry) && entry.command === command && entry.exit_code === exitCode,
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function insideAgentic(repoDir, file) {
|
|
767
|
+
if (!repoDir) {
|
|
768
|
+
const normalized = path.normalize(file);
|
|
769
|
+
return normalized.startsWith(`.agentic${path.sep}`) || normalized.startsWith(".agentic/") || normalized.startsWith(".agentic\\");
|
|
770
|
+
}
|
|
771
|
+
const resolved = insideRepo(repoDir, file);
|
|
772
|
+
if (!resolved) return false;
|
|
773
|
+
try {
|
|
774
|
+
const agentic = fs.realpathSync(path.join(repoDir, ".agentic"));
|
|
775
|
+
return resolved.startsWith(`${agentic}${path.sep}`);
|
|
776
|
+
} catch {
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
function validateNodeE2e(data, repoDir, problems) {
|
|
782
|
+
if (!nonEmptyArray(data.commands)) {
|
|
783
|
+
problems.push("commands must contain the Node test and sync dry-run results");
|
|
784
|
+
} else {
|
|
785
|
+
for (const [index, entry] of data.commands.entries()) {
|
|
786
|
+
if (!isObject(entry)) {
|
|
787
|
+
problems.push(`commands[${index}] must be an object`);
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
if (typeof entry.command !== "string" || !entry.command.trim()) {
|
|
791
|
+
problems.push(`commands[${index}].command must be a non-empty string`);
|
|
792
|
+
}
|
|
793
|
+
if (!Number.isInteger(entry.exit_code)) problems.push(`commands[${index}].exit_code must be an integer`);
|
|
794
|
+
}
|
|
795
|
+
if (!hasCommand(data.commands, "npm test", 0)) {
|
|
796
|
+
problems.push("commands must include npm test with exit_code 0");
|
|
797
|
+
}
|
|
798
|
+
const syncPassed = hasCommand(data.commands, "npm run sync:dry", 0);
|
|
799
|
+
const notConfigured = hasCommand(data.commands, "not-configured", 127);
|
|
800
|
+
if (!syncPassed && !notConfigured) {
|
|
801
|
+
problems.push("commands must include npm run sync:dry with exit_code 0 or not-configured with exit_code 127");
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const reportPath = typeof data.report_path === "string" ? data.report_path.trim() : "";
|
|
806
|
+
if (reportPath && !reportPath.split("#")[0].endsWith(".log")) {
|
|
807
|
+
problems.push("node report_path must end with .log");
|
|
808
|
+
}
|
|
809
|
+
if (reportPath && !insideAgentic(repoDir, reportPath.split("#")[0])) {
|
|
810
|
+
problems.push("node report_path must be inside .agentic/");
|
|
811
|
+
}
|
|
812
|
+
if (repoDir && reportPath) {
|
|
813
|
+
const report = insideRepo(repoDir, reportPath.split("#")[0]);
|
|
814
|
+
if (report && (!fs.statSync(report).isFile() || fs.statSync(report).size === 0)) {
|
|
815
|
+
problems.push("node report_path must be a non-empty regular file");
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
for (const [index, scenario] of (Array.isArray(data.scenarios) ? data.scenarios : []).entries()) {
|
|
820
|
+
const evidence = typeof scenario?.evidence === "string" ? scenario.evidence.trim() : "";
|
|
821
|
+
if (!evidence) continue;
|
|
822
|
+
const file = evidence.split("#")[0];
|
|
823
|
+
if (!file.endsWith(".log")) {
|
|
824
|
+
problems.push(`scenarios[${index}] node evidence must be a .log file: ${file}`);
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
if (!insideAgentic(repoDir, file)) {
|
|
828
|
+
problems.push(`scenarios[${index}] evidence must be inside .agentic/: ${file}`);
|
|
829
|
+
}
|
|
830
|
+
if (repoDir) {
|
|
831
|
+
const resolved = insideRepo(repoDir, file);
|
|
832
|
+
if (!resolved || !fs.statSync(resolved).isFile()) {
|
|
833
|
+
problems.push(`scenarios[${index}] evidence is not a file inside the worktree: ${file}`);
|
|
834
|
+
} else if (fs.statSync(resolved).size === 0) {
|
|
835
|
+
problems.push(`scenarios[${index}] evidence is empty: ${file}`);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
const VALIDATORS = {
|
|
842
|
+
gateway: (data) => validateGateway(data),
|
|
843
|
+
planning: (data) => validateSpec(data),
|
|
844
|
+
implementation: (data, spec, repoDir, bugfix) => validateImplementation(data, spec, repoDir, bugfix),
|
|
845
|
+
review: (data, spec, repoDir, bugfix) => validateReview(data, spec, repoDir, bugfix),
|
|
846
|
+
e2e: (data, spec, repoDir, bugfix) => validateE2e(data, spec, repoDir, bugfix),
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
/** Validate a parsed artifact for the given stage index. */
|
|
850
|
+
export function validateArtifact(stageIndex, data, { spec = null, repoDir = null, bugfix = null } = {}) {
|
|
851
|
+
const stage = STAGE_ARTIFACTS[stageIndex];
|
|
852
|
+
if (!stage) return [`no artifact contract for stage ${stageIndex}`];
|
|
853
|
+
if (!isObject(data)) return [`${stage.file} must be a JSON object`];
|
|
854
|
+
let problems;
|
|
855
|
+
try {
|
|
856
|
+
problems = [...validateEnvelope(data), ...VALIDATORS[stage.stage](data, spec, repoDir, bugfix)];
|
|
857
|
+
} catch (err) {
|
|
858
|
+
return [`${stage.file} failed validation: ${err.message}`];
|
|
859
|
+
}
|
|
860
|
+
return problems;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/** Read and validate the artifact for a stage inside a repository worktree. */
|
|
864
|
+
export function readStageArtifact(repoDir, stageIndex, { notBefore = null, requiredGateways = null } = {}) {
|
|
865
|
+
const stage = STAGE_ARTIFACTS[stageIndex];
|
|
866
|
+
if (!stage) return { ok: false, problems: [`no artifact contract for stage ${stageIndex}`] };
|
|
867
|
+
if (!stage.file) return { ok: false, problems: [`${stage.stage} has no generated artifact`] };
|
|
868
|
+
const file = path.join(repoDir, ".agentic", stage.file);
|
|
869
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
|
|
870
|
+
return { ok: false, missing: true, problems: [`${stage.file} missing`] };
|
|
871
|
+
}
|
|
872
|
+
const data = readJson(file);
|
|
873
|
+
if (data === null) return { ok: false, problems: [`${stage.file} is not valid JSON`] };
|
|
874
|
+
|
|
875
|
+
if (stageIndex === 0) {
|
|
876
|
+
const problems = validateArtifact(0, data);
|
|
877
|
+
problems.push(...gatewayProblems(data, requiredGateways));
|
|
878
|
+
if (notBefore && typeof data.timestamp === "string") {
|
|
879
|
+
const created = Date.parse(notBefore);
|
|
880
|
+
if (Number.isFinite(created) && Date.parse(data.timestamp) < created) {
|
|
881
|
+
problems.push(`${stage.file} predates the current task run`);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
const dockerFile = path.join(repoDir, ".agentic", "docker.json");
|
|
885
|
+
if (fs.existsSync(dockerFile)) {
|
|
886
|
+
const dockerData = readJson(dockerFile);
|
|
887
|
+
if (!isObject(dockerData)) {
|
|
888
|
+
problems.push("docker.json is not valid JSON");
|
|
889
|
+
} else if (typeof dockerData.enabled !== "boolean") {
|
|
890
|
+
problems.push("docker.json.enabled must be boolean");
|
|
891
|
+
} else if (dockerData.enabled === true && !dockerData.skipped) {
|
|
892
|
+
if (typeof dockerData.container_id !== "string" || !dockerData.container_id.trim()) {
|
|
893
|
+
problems.push("docker.json enabled but lacks container_id");
|
|
894
|
+
}
|
|
895
|
+
if (dockerData.verified_in_container !== true) {
|
|
896
|
+
problems.push("docker.json: Git worktree not verified in container");
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return { ok: problems.length === 0, data, problems };
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const specFile = path.join(repoDir, ".agentic", "spec.json");
|
|
904
|
+
if (!fs.existsSync(specFile)) {
|
|
905
|
+
return { ok: false, problems: ["spec.json missing; cannot cross-check this artifact"] };
|
|
906
|
+
}
|
|
907
|
+
const spec = readJson(specFile);
|
|
908
|
+
if (!isObject(spec)) {
|
|
909
|
+
return { ok: false, problems: ["spec.json unparseable; cannot cross-check this artifact"] };
|
|
910
|
+
}
|
|
911
|
+
const specProblems = validateArtifact(PLANNING_STAGE, spec);
|
|
912
|
+
if (specProblems.length) {
|
|
913
|
+
return { ok: false, problems: specProblems.map((problem) => `spec.json: ${problem}`) };
|
|
914
|
+
}
|
|
915
|
+
if (notBefore) {
|
|
916
|
+
const created = Date.parse(notBefore);
|
|
917
|
+
if (Number.isFinite(created) && Date.parse(spec.timestamp) < created) {
|
|
918
|
+
return { ok: false, problems: ["spec.json predates the current task run"] };
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
let bugfix = null;
|
|
923
|
+
if (spec.change_type === "bugfix") {
|
|
924
|
+
const contract = readContract(repoDir);
|
|
925
|
+
if (contract.problems.length) {
|
|
926
|
+
return { ok: false, problems: contract.problems.map((problem) => `bugfix: ${problem}`) };
|
|
927
|
+
}
|
|
928
|
+
const contractErrors = contractProblems(repoDir, spec, contract.data);
|
|
929
|
+
if (contractErrors.length) {
|
|
930
|
+
return { ok: false, problems: contractErrors.map((problem) => `bugfix: ${problem}`) };
|
|
931
|
+
}
|
|
932
|
+
bugfix = contract.data;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const reviewFile = path.join(repoDir, ".agentic", "spec-review.json");
|
|
936
|
+
if (!fs.existsSync(reviewFile) || !fs.statSync(reviewFile).isFile()) {
|
|
937
|
+
return { ok: false, missing: true, problems: ["spec-review.json missing"] };
|
|
938
|
+
}
|
|
939
|
+
const review = readJson(reviewFile);
|
|
940
|
+
if (review === null) return { ok: false, problems: ["spec-review.json is not valid JSON"] };
|
|
941
|
+
const reviewProblems = [...validateEnvelope(review), ...validatePlanningReview(review)];
|
|
942
|
+
if (typeof review.task === "string" && review.task !== spec.task) {
|
|
943
|
+
reviewProblems.push("spec-review.json.task does not match spec.json task");
|
|
944
|
+
}
|
|
945
|
+
if (notBefore && typeof review.timestamp === "string") {
|
|
946
|
+
const created = Date.parse(notBefore);
|
|
947
|
+
if (Number.isFinite(created) && Date.parse(review.timestamp) < created) {
|
|
948
|
+
reviewProblems.push("spec-review.json predates the current task run");
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
if (reviewProblems.length) {
|
|
952
|
+
return { ok: false, problems: reviewProblems.map((problem) => `spec-review.json: ${problem}`) };
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
if (stageIndex === PLANNING_STAGE) {
|
|
956
|
+
if (bugfix) {
|
|
957
|
+
const run = readRun(repoDir, "planning-before");
|
|
958
|
+
if (run.problems.length) {
|
|
959
|
+
return { ok: false, problems: run.problems.map((problem) => `bugfix: ${problem}`) };
|
|
960
|
+
}
|
|
961
|
+
const runErrors = runProblems(repoDir, run.data, {
|
|
962
|
+
phase: "planning-before",
|
|
963
|
+
status: "failed",
|
|
964
|
+
scenarioId: bugfix.reproduction.scenario_id,
|
|
965
|
+
});
|
|
966
|
+
if (runErrors.length) {
|
|
967
|
+
return { ok: false, problems: runErrors.map((problem) => `bugfix: ${problem}`) };
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
const handoffProblems = planningHandoffProblems(repoDir, spec, review);
|
|
971
|
+
if (handoffProblems.length) return { ok: false, problems: handoffProblems.map((problem) => `planning: ${problem}`) };
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
let problems;
|
|
975
|
+
try {
|
|
976
|
+
problems = validateArtifact(stageIndex, data, { spec, repoDir, bugfix });
|
|
977
|
+
} catch (err) {
|
|
978
|
+
problems = [`${stage.file} failed validation: ${err.message}`];
|
|
979
|
+
}
|
|
980
|
+
if (notBefore && typeof data.timestamp === "string") {
|
|
981
|
+
const created = Date.parse(notBefore);
|
|
982
|
+
if (Number.isFinite(created) && Date.parse(data.timestamp) < created) {
|
|
983
|
+
problems.push(`${stage.file} predates the current task run`);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
if (spec && data.task !== spec.task) {
|
|
987
|
+
problems.push(`${stage.file}.task does not match spec.json task`);
|
|
988
|
+
}
|
|
989
|
+
return { ok: problems.length === 0, data, problems };
|
|
990
|
+
}
|