@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,548 @@
|
|
|
1
|
+
/** Load and validate the declarative pipeline (pipeline.json + workflows/sdlc.yaml). */
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import yaml from "js-yaml";
|
|
5
|
+
|
|
6
|
+
export const REASONING_EFFORTS = new Set(["low", "medium", "high", "xhigh", "max", "ultra"]);
|
|
7
|
+
|
|
8
|
+
export class ValidationError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "ValidationError";
|
|
12
|
+
this.exitCode = 2;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function loadEnv(root, envPath = path.join(root, ".env")) {
|
|
17
|
+
if (!fs.existsSync(envPath)) return;
|
|
18
|
+
for (const line of fs.readFileSync(envPath, "utf8").split("\n")) {
|
|
19
|
+
const trimmed = line.trim();
|
|
20
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
21
|
+
const idx = trimmed.indexOf("=");
|
|
22
|
+
if (idx < 1) continue;
|
|
23
|
+
const key = trimmed.slice(0, idx).trim();
|
|
24
|
+
let value = trimmed.slice(idx + 1).trim();
|
|
25
|
+
if (
|
|
26
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
27
|
+
(value.startsWith("'") && value.endsWith("'"))
|
|
28
|
+
) {
|
|
29
|
+
value = value.slice(1, -1);
|
|
30
|
+
}
|
|
31
|
+
if (process.env[key] === undefined) process.env[key] = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function normalizePrompt(text) {
|
|
36
|
+
return String(text || "")
|
|
37
|
+
.replace(/\r\n/g, "\n")
|
|
38
|
+
.trim()
|
|
39
|
+
.split("\n")
|
|
40
|
+
.map((line) => line.replace(/\s+$/, ""))
|
|
41
|
+
.join("\n");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function requireNonEmptyString(value, field) {
|
|
45
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
46
|
+
throw new ValidationError(`${field} must be a non-empty string`);
|
|
47
|
+
}
|
|
48
|
+
return value.trim();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function validateStringArray(value, field) {
|
|
52
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string" || !item.trim())) {
|
|
53
|
+
throw new ValidationError(`${field} must be an array of non-empty strings`);
|
|
54
|
+
}
|
|
55
|
+
return value.map((item) => item.trim());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function validateDockerProjectConfig(root, key, raw, configPath = null) {
|
|
59
|
+
const prefix = `pipeline.docker.projects.${key}`;
|
|
60
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
61
|
+
throw new ValidationError(`${prefix} must be an object`);
|
|
62
|
+
}
|
|
63
|
+
const allowed = new Set([
|
|
64
|
+
"project",
|
|
65
|
+
"match",
|
|
66
|
+
"image",
|
|
67
|
+
"dockerfile",
|
|
68
|
+
"platform",
|
|
69
|
+
"container_prefix",
|
|
70
|
+
"env",
|
|
71
|
+
"volumes",
|
|
72
|
+
"dependencies",
|
|
73
|
+
]);
|
|
74
|
+
const unknown = Object.keys(raw).filter((k) => !allowed.has(k));
|
|
75
|
+
if (unknown.length) {
|
|
76
|
+
throw new ValidationError(`${prefix} has unknown keys: ${unknown.join(", ")}`);
|
|
77
|
+
}
|
|
78
|
+
const project = requireNonEmptyString(raw.project, `${prefix}.project`);
|
|
79
|
+
if (project !== key) {
|
|
80
|
+
throw new ValidationError(`${prefix}.project "${project}" must match key "${key}"`);
|
|
81
|
+
}
|
|
82
|
+
const image = requireNonEmptyString(raw.image, `${prefix}.image`);
|
|
83
|
+
const dockerfile = raw.dockerfile !== undefined
|
|
84
|
+
? requireNonEmptyString(raw.dockerfile, `${prefix}.dockerfile`)
|
|
85
|
+
: undefined;
|
|
86
|
+
const platform = raw.platform !== undefined
|
|
87
|
+
? requireNonEmptyString(raw.platform, `${prefix}.platform`)
|
|
88
|
+
: undefined;
|
|
89
|
+
const containerPrefix = raw.container_prefix !== undefined
|
|
90
|
+
? requireNonEmptyString(raw.container_prefix, `${prefix}.container_prefix`)
|
|
91
|
+
: undefined;
|
|
92
|
+
|
|
93
|
+
if (raw.match !== undefined) {
|
|
94
|
+
if (!raw.match || typeof raw.match !== "object" || Array.isArray(raw.match)) {
|
|
95
|
+
throw new ValidationError(`${prefix}.match must be an object`);
|
|
96
|
+
}
|
|
97
|
+
for (const matchKey of ["repo_names", "package_names", "gradle_root_projects", "files"]) {
|
|
98
|
+
if (raw.match[matchKey] !== undefined) {
|
|
99
|
+
validateStringArray(raw.match[matchKey], `${prefix}.match.${matchKey}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (raw.env !== undefined) {
|
|
104
|
+
if (!raw.env || typeof raw.env !== "object" || Array.isArray(raw.env)) {
|
|
105
|
+
throw new ValidationError(`${prefix}.env must be an object`);
|
|
106
|
+
}
|
|
107
|
+
for (const [envKey, envVal] of Object.entries(raw.env)) {
|
|
108
|
+
if (typeof envVal !== "string") {
|
|
109
|
+
throw new ValidationError(`${prefix}.env.${envKey} must be a string`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (raw.volumes !== undefined) {
|
|
114
|
+
validateStringArray(raw.volumes, `${prefix}.volumes`);
|
|
115
|
+
}
|
|
116
|
+
if (raw.dependencies !== undefined) {
|
|
117
|
+
if (!raw.dependencies || typeof raw.dependencies !== "object" || Array.isArray(raw.dependencies)) {
|
|
118
|
+
throw new ValidationError(`${prefix}.dependencies must be an object`);
|
|
119
|
+
}
|
|
120
|
+
if (raw.dependencies.cli_tools !== undefined) {
|
|
121
|
+
if (!Array.isArray(raw.dependencies.cli_tools)) {
|
|
122
|
+
throw new ValidationError(`${prefix}.dependencies.cli_tools must be an array`);
|
|
123
|
+
}
|
|
124
|
+
for (const [idx, tool] of raw.dependencies.cli_tools.entries()) {
|
|
125
|
+
if (!tool || typeof tool !== "object" || Array.isArray(tool)) {
|
|
126
|
+
throw new ValidationError(`${prefix}.dependencies.cli_tools[${idx}] must be an object`);
|
|
127
|
+
}
|
|
128
|
+
requireNonEmptyString(tool.name, `${prefix}.dependencies.cli_tools[${idx}].name`);
|
|
129
|
+
validateStringArray(tool.verify, `${prefix}.dependencies.cli_tools[${idx}].verify`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
project,
|
|
136
|
+
...(raw.match ? { match: raw.match } : {}),
|
|
137
|
+
image,
|
|
138
|
+
...(dockerfile ? { dockerfile, resolved_dockerfile: path.resolve(root, dockerfile) } : {}),
|
|
139
|
+
...(platform ? { platform } : {}),
|
|
140
|
+
...(containerPrefix ? { container_prefix: containerPrefix } : {}),
|
|
141
|
+
...(raw.env ? { env: raw.env } : {}),
|
|
142
|
+
...(raw.volumes ? { volumes: raw.volumes } : {}),
|
|
143
|
+
...(raw.dependencies ? { dependencies: raw.dependencies } : {}),
|
|
144
|
+
...(configPath ? { config_path: configPath } : {}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function loadDockerProjects(root, docker) {
|
|
149
|
+
if (docker.projects === undefined) return {};
|
|
150
|
+
if (!docker.projects || typeof docker.projects !== "object" || Array.isArray(docker.projects)) {
|
|
151
|
+
throw new ValidationError("pipeline.docker.projects must be an object");
|
|
152
|
+
}
|
|
153
|
+
const resolved = {};
|
|
154
|
+
for (const [key, entry] of Object.entries(docker.projects)) {
|
|
155
|
+
if (!key.trim()) throw new ValidationError("pipeline.docker.projects keys must be non-empty");
|
|
156
|
+
if (typeof entry === "string") {
|
|
157
|
+
const rel = requireNonEmptyString(entry, `pipeline.docker.projects.${key}`);
|
|
158
|
+
const abs = path.isAbsolute(rel) ? rel : path.resolve(root, rel);
|
|
159
|
+
if (!fs.existsSync(abs)) {
|
|
160
|
+
throw new ValidationError(`pipeline.docker.projects.${key} file not found: ${rel}`);
|
|
161
|
+
}
|
|
162
|
+
let parsed;
|
|
163
|
+
try {
|
|
164
|
+
parsed = JSON.parse(fs.readFileSync(abs, "utf8"));
|
|
165
|
+
} catch (err) {
|
|
166
|
+
throw new ValidationError(`pipeline.docker.projects.${key} is not valid JSON: ${err.message}`);
|
|
167
|
+
}
|
|
168
|
+
resolved[key] = validateDockerProjectConfig(root, key, parsed, rel);
|
|
169
|
+
} else {
|
|
170
|
+
resolved[key] = validateDockerProjectConfig(root, key, entry);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return resolved;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function loadPipeline(root = process.cwd()) {
|
|
177
|
+
const pipelinePath = path.join(root, "config", "pipeline.json");
|
|
178
|
+
const workflowPath = path.join(root, "config", "workflows", "sdlc.yaml");
|
|
179
|
+
for (const file of [pipelinePath, workflowPath]) {
|
|
180
|
+
if (!fs.existsSync(file)) throw new ValidationError(`Missing ${path.relative(root, file)}`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let pipeline;
|
|
184
|
+
try {
|
|
185
|
+
pipeline = JSON.parse(fs.readFileSync(pipelinePath, "utf8"));
|
|
186
|
+
} catch (err) {
|
|
187
|
+
throw new ValidationError(`config/pipeline.json is not valid JSON: ${err.message}`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const defaultDocker = {
|
|
191
|
+
enabled: true,
|
|
192
|
+
image: "kandev-task-runtime:bookworm",
|
|
193
|
+
dockerfile: "docker/Dockerfile",
|
|
194
|
+
platform: "linux/amd64",
|
|
195
|
+
container_prefix: "kandev-task",
|
|
196
|
+
worktree_root: "~/.kandev/tasks",
|
|
197
|
+
provision_dependencies: true,
|
|
198
|
+
};
|
|
199
|
+
if (pipeline.docker !== undefined) {
|
|
200
|
+
const docker = pipeline.docker;
|
|
201
|
+
if (!docker || typeof docker !== "object" || Array.isArray(docker)) {
|
|
202
|
+
throw new ValidationError("pipeline.docker must be an object");
|
|
203
|
+
}
|
|
204
|
+
if (docker.enabled !== undefined && typeof docker.enabled !== "boolean") {
|
|
205
|
+
throw new ValidationError("pipeline.docker.enabled must be boolean");
|
|
206
|
+
}
|
|
207
|
+
if (docker.provision_dependencies !== undefined && typeof docker.provision_dependencies !== "boolean") {
|
|
208
|
+
throw new ValidationError("pipeline.docker.provision_dependencies must be boolean");
|
|
209
|
+
}
|
|
210
|
+
for (const key of ["image", "dockerfile", "platform", "container_prefix", "worktree_root", "projects_dir"]) {
|
|
211
|
+
if (docker[key] !== undefined && (typeof docker[key] !== "string" || !docker[key].trim())) {
|
|
212
|
+
throw new ValidationError(`pipeline.docker.${key} must be a non-empty string`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const allowed = new Set([
|
|
216
|
+
"enabled",
|
|
217
|
+
"image",
|
|
218
|
+
"dockerfile",
|
|
219
|
+
"platform",
|
|
220
|
+
"container_prefix",
|
|
221
|
+
"worktree_root",
|
|
222
|
+
"provision_dependencies",
|
|
223
|
+
"projects_dir",
|
|
224
|
+
"projects",
|
|
225
|
+
]);
|
|
226
|
+
const unknown = Object.keys(docker).filter((key) => !allowed.has(key));
|
|
227
|
+
if (unknown.length) throw new ValidationError(`pipeline.docker has unknown keys: ${unknown.join(", ")}`);
|
|
228
|
+
const projects = loadDockerProjects(root, docker);
|
|
229
|
+
pipeline.docker = {
|
|
230
|
+
enabled: docker.enabled ?? defaultDocker.enabled,
|
|
231
|
+
image: docker.image ? docker.image.trim() : defaultDocker.image,
|
|
232
|
+
dockerfile: docker.dockerfile ? docker.dockerfile.trim() : defaultDocker.dockerfile,
|
|
233
|
+
platform: docker.platform ? docker.platform.trim() : defaultDocker.platform,
|
|
234
|
+
container_prefix: docker.container_prefix ? docker.container_prefix.trim() : defaultDocker.container_prefix,
|
|
235
|
+
worktree_root: docker.worktree_root ? docker.worktree_root.trim() : defaultDocker.worktree_root,
|
|
236
|
+
provision_dependencies: docker.provision_dependencies ?? defaultDocker.provision_dependencies,
|
|
237
|
+
...(docker.projects_dir ? { projects_dir: docker.projects_dir.trim() } : {}),
|
|
238
|
+
...(Object.keys(projects).length > 0 ? { projects } : {}),
|
|
239
|
+
};
|
|
240
|
+
} else {
|
|
241
|
+
pipeline.docker = { ...defaultDocker };
|
|
242
|
+
}
|
|
243
|
+
if (pipeline.rework !== undefined) {
|
|
244
|
+
const rework = pipeline.rework;
|
|
245
|
+
if (!rework || typeof rework !== "object" || Array.isArray(rework)) {
|
|
246
|
+
throw new ValidationError("pipeline.rework must be an object");
|
|
247
|
+
}
|
|
248
|
+
if (!Number.isInteger(rework.max_attempts) || rework.max_attempts < 0) {
|
|
249
|
+
throw new ValidationError("pipeline.rework.max_attempts must be a non-negative integer");
|
|
250
|
+
}
|
|
251
|
+
const unknown = Object.keys(rework).filter((key) => key !== "max_attempts");
|
|
252
|
+
if (unknown.length) {
|
|
253
|
+
throw new ValidationError(`pipeline.rework has unknown keys: ${unknown.join(", ")}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (pipeline.resume !== undefined) {
|
|
258
|
+
const resume = pipeline.resume;
|
|
259
|
+
if (!resume || typeof resume !== "object" || Array.isArray(resume)) {
|
|
260
|
+
throw new ValidationError("pipeline.resume must be an object");
|
|
261
|
+
}
|
|
262
|
+
if (typeof resume.enabled !== "boolean") {
|
|
263
|
+
throw new ValidationError("pipeline.resume.enabled must be boolean");
|
|
264
|
+
}
|
|
265
|
+
const integer = (key, min, max) => {
|
|
266
|
+
const value = resume[key];
|
|
267
|
+
if (value === undefined) return;
|
|
268
|
+
if (!Number.isInteger(value) || value < min || (max !== undefined && value > max)) {
|
|
269
|
+
throw new ValidationError(`pipeline.resume.${key} must be an integer between ${min} and ${max ?? "unbounded"}`);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
integer("max_attempts", 1, 10);
|
|
273
|
+
integer("cooldown_ms", 30_000);
|
|
274
|
+
integer("idle_ms", 60_000);
|
|
275
|
+
integer("running_nudge_max", 0, 3);
|
|
276
|
+
integer("scan_interval_ms", 5_000);
|
|
277
|
+
integer("reenter_settle_ms", 0, 10_000);
|
|
278
|
+
const allowed = new Set([
|
|
279
|
+
"enabled",
|
|
280
|
+
"max_attempts",
|
|
281
|
+
"cooldown_ms",
|
|
282
|
+
"idle_ms",
|
|
283
|
+
"stage_idle_ms",
|
|
284
|
+
"running_nudge_max",
|
|
285
|
+
"scan_interval_ms",
|
|
286
|
+
"reenter_settle_ms",
|
|
287
|
+
]);
|
|
288
|
+
const unknown = Object.keys(resume).filter((key) => !allowed.has(key));
|
|
289
|
+
if (unknown.length) throw new ValidationError(`pipeline.resume has unknown keys: ${unknown.join(", ")}`);
|
|
290
|
+
if (resume.stage_idle_ms !== undefined) {
|
|
291
|
+
if (!resume.stage_idle_ms || typeof resume.stage_idle_ms !== "object" || Array.isArray(resume.stage_idle_ms)) {
|
|
292
|
+
throw new ValidationError("pipeline.resume.stage_idle_ms must be an object");
|
|
293
|
+
}
|
|
294
|
+
const floor = resume.idle_ms ?? 900_000;
|
|
295
|
+
for (const [name, value] of Object.entries(resume.stage_idle_ms)) {
|
|
296
|
+
if (!Number.isInteger(value) || value < floor) {
|
|
297
|
+
throw new ValidationError(`pipeline.resume.stage_idle_ms.${name} must be an integer >= ${floor}`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (pipeline.default_profile !== undefined) {
|
|
304
|
+
const dp = pipeline.default_profile;
|
|
305
|
+
if (!dp || typeof dp !== "object" || Array.isArray(dp)) {
|
|
306
|
+
throw new ValidationError("pipeline.default_profile must be an object");
|
|
307
|
+
}
|
|
308
|
+
const allowed = new Set(["name", "model", "reasoning_effort"]);
|
|
309
|
+
const unknown = Object.keys(dp).filter((key) => !allowed.has(key));
|
|
310
|
+
if (unknown.length) {
|
|
311
|
+
throw new ValidationError(`pipeline.default_profile has unknown keys: ${unknown.join(", ")}`);
|
|
312
|
+
}
|
|
313
|
+
for (const key of ["name", "model"]) {
|
|
314
|
+
if (typeof dp[key] !== "string" || !dp[key].trim()) {
|
|
315
|
+
throw new ValidationError(`pipeline.default_profile.${key} must be a non-empty string`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (dp.reasoning_effort !== undefined && !REASONING_EFFORTS.has(dp.reasoning_effort)) {
|
|
319
|
+
throw new ValidationError(
|
|
320
|
+
`pipeline.default_profile.reasoning_effort must be one of ${[...REASONING_EFFORTS].join(", ")}`,
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
let doc;
|
|
326
|
+
try {
|
|
327
|
+
doc = yaml.load(fs.readFileSync(workflowPath, "utf8"));
|
|
328
|
+
} catch (err) {
|
|
329
|
+
throw new ValidationError(`config/workflows/sdlc.yaml is not valid YAML: ${err.message}`);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (!doc || doc.type !== "kandev_workflow") {
|
|
333
|
+
throw new ValidationError("Workflow document must have type: kandev_workflow");
|
|
334
|
+
}
|
|
335
|
+
if (![1, 2, 3].includes(doc.version)) {
|
|
336
|
+
throw new ValidationError(`Unsupported workflow envelope version: ${doc.version}`);
|
|
337
|
+
}
|
|
338
|
+
const workflows = doc.workflows || [];
|
|
339
|
+
if (workflows.length !== 1) {
|
|
340
|
+
throw new ValidationError(`Expected exactly one workflow, found ${workflows.length}`);
|
|
341
|
+
}
|
|
342
|
+
const workflow = workflows[0];
|
|
343
|
+
if (!workflow.name) throw new ValidationError("Workflow name is required");
|
|
344
|
+
if (pipeline.pipeline !== workflow.name) {
|
|
345
|
+
throw new ValidationError(
|
|
346
|
+
`Workflow name "${workflow.name}" does not match pipeline.json "${pipeline.pipeline}"`,
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
if (!Array.isArray(pipeline.stages) || pipeline.stages.length < 1) {
|
|
350
|
+
throw new ValidationError(`Expected at least one stage in pipeline.json, found ${pipeline.stages?.length ?? 0}`);
|
|
351
|
+
}
|
|
352
|
+
for (const stage of pipeline.stages) {
|
|
353
|
+
for (const server of Object.values(stage.mcp_servers || {})) {
|
|
354
|
+
if (server?.env && Object.hasOwn(server.env, "KANDEV_WORKFLOW_ROOT")) {
|
|
355
|
+
server.env.KANDEV_WORKFLOW_ROOT = path.resolve(root);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const tools = stage.mcp_servers?.["agent-tools"];
|
|
359
|
+
if (tools) {
|
|
360
|
+
if (Array.isArray(tools.args)) {
|
|
361
|
+
tools.args = tools.args.map((arg) =>
|
|
362
|
+
arg === "scripts/agent-tools-docker.mjs" ? path.resolve(root, arg) : arg,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
tools.env = {
|
|
366
|
+
...(tools.env || {}),
|
|
367
|
+
KANDEV_DOCKER_ENABLED: String(Boolean(pipeline.docker.enabled)),
|
|
368
|
+
KANDEV_DOCKER_IMAGE: pipeline.docker.image,
|
|
369
|
+
KANDEV_DOCKER_PLATFORM: pipeline.docker.platform,
|
|
370
|
+
};
|
|
371
|
+
if (stage.model) {
|
|
372
|
+
tools.env.KANDEV_STAGE_MODEL = stage.model;
|
|
373
|
+
}
|
|
374
|
+
if (pipeline.agent) {
|
|
375
|
+
tools.env.KANDEV_STAGE_AGENT = pipeline.agent;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (!Array.isArray(workflow.steps) || workflow.steps.length !== pipeline.stages.length + 1) {
|
|
380
|
+
throw new ValidationError(
|
|
381
|
+
`Expected ${pipeline.stages.length} stage steps plus a terminal Done step (${pipeline.stages.length + 1}), found ${workflow.steps?.length ?? 0}`,
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const profileNames = new Set();
|
|
386
|
+
const stepNames = new Set();
|
|
387
|
+
const positions = new Set();
|
|
388
|
+
const stages = [];
|
|
389
|
+
|
|
390
|
+
for (const [index, stage] of pipeline.stages.entries()) {
|
|
391
|
+
if (stage.position !== index) {
|
|
392
|
+
throw new ValidationError(`Stage ${stage.step} has position ${stage.position}, expected ${index}`);
|
|
393
|
+
}
|
|
394
|
+
if (!stage.step) throw new ValidationError(`Stage at position ${index} has no step name`);
|
|
395
|
+
if (stage.human_approval === true) {
|
|
396
|
+
if (stage.profile_name || stage.model || stage.reasoning_effort) {
|
|
397
|
+
throw new ValidationError(`Human approval stage ${stage.step} must not declare a profile or model`);
|
|
398
|
+
}
|
|
399
|
+
if (stage.include_task_prompt === true) {
|
|
400
|
+
throw new ValidationError(`Human approval stage ${stage.step} must not include the task prompt`);
|
|
401
|
+
}
|
|
402
|
+
} else {
|
|
403
|
+
if (!stage.profile_name || profileNames.has(stage.profile_name)) {
|
|
404
|
+
throw new ValidationError(`Duplicate or missing profile_name for stage ${stage.step}`);
|
|
405
|
+
}
|
|
406
|
+
profileNames.add(stage.profile_name);
|
|
407
|
+
if (!stage.model) throw new ValidationError(`Stage ${stage.step} has no model`);
|
|
408
|
+
if (
|
|
409
|
+
stage.reasoning_effort !== undefined &&
|
|
410
|
+
stage.reasoning_effort !== null &&
|
|
411
|
+
!REASONING_EFFORTS.has(stage.reasoning_effort)
|
|
412
|
+
) {
|
|
413
|
+
throw new ValidationError(
|
|
414
|
+
`Stage ${stage.step} reasoning_effort must be one of ${[...REASONING_EFFORTS].join(", ")}`,
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (stage.include_task_prompt !== undefined && typeof stage.include_task_prompt !== "boolean") {
|
|
419
|
+
throw new ValidationError(`Stage ${stage.step} include_task_prompt must be boolean`);
|
|
420
|
+
}
|
|
421
|
+
stages.push(stage);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (pipeline.resume?.stage_idle_ms) {
|
|
425
|
+
const runnable = new Set(
|
|
426
|
+
stages.filter((stage) => stage.human_approval !== true).map((stage) => stage.step),
|
|
427
|
+
);
|
|
428
|
+
const unknown = Object.keys(pipeline.resume.stage_idle_ms).filter((name) => !runnable.has(name));
|
|
429
|
+
if (unknown.length) {
|
|
430
|
+
throw new ValidationError(`pipeline.resume.stage_idle_ms has unknown stages: ${unknown.join(", ")}`);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const byPosition = [...workflow.steps].sort((a, b) => a.position - b.position);
|
|
435
|
+
for (const [index, step] of byPosition.entries()) {
|
|
436
|
+
const isTerminal = index === stages.length;
|
|
437
|
+
if (!step.name || stepNames.has(step.name)) {
|
|
438
|
+
throw new ValidationError(`Duplicate or missing step name at position ${step.position}`);
|
|
439
|
+
}
|
|
440
|
+
stepNames.add(step.name);
|
|
441
|
+
if (positions.has(step.position)) {
|
|
442
|
+
throw new ValidationError(`Duplicate step position ${step.position}`);
|
|
443
|
+
}
|
|
444
|
+
positions.add(step.position);
|
|
445
|
+
if (step.position !== index) {
|
|
446
|
+
throw new ValidationError(`Step "${step.name}" has position ${step.position}, expected ${index}`);
|
|
447
|
+
}
|
|
448
|
+
if (isTerminal) {
|
|
449
|
+
if (step.profile_ref || (step.prompt && step.prompt.trim())) {
|
|
450
|
+
throw new ValidationError(`Terminal step "${step.name}" must not have a profile or prompt`);
|
|
451
|
+
}
|
|
452
|
+
if (step.auto_advance_requires_signal === true) {
|
|
453
|
+
throw new ValidationError(`Terminal step "${step.name}" must not require a completion signal`);
|
|
454
|
+
}
|
|
455
|
+
if (step.is_start_step === true) {
|
|
456
|
+
throw new ValidationError(`Terminal step "${step.name}" must not be the start step`);
|
|
457
|
+
}
|
|
458
|
+
if (step.complete_task_on_enter !== true) {
|
|
459
|
+
throw new ValidationError(`Terminal step "${step.name}" must set complete_task_on_enter: true`);
|
|
460
|
+
}
|
|
461
|
+
if (step.allow_manual_move === true) {
|
|
462
|
+
throw new ValidationError(`Terminal step "${step.name}" must set allow_manual_move: false`);
|
|
463
|
+
}
|
|
464
|
+
if ((step.events?.on_turn_complete || []).length) {
|
|
465
|
+
throw new ValidationError(`Terminal step "${step.name}" must not transition`);
|
|
466
|
+
}
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
const stage = stages[index];
|
|
470
|
+
if (step.name !== stage.step) {
|
|
471
|
+
throw new ValidationError(`Step "${step.name}" does not match pipeline.json step "${stage.step}"`);
|
|
472
|
+
}
|
|
473
|
+
if (stage.human_approval === true) {
|
|
474
|
+
if (step.profile_ref || (step.prompt && step.prompt.trim())) {
|
|
475
|
+
throw new ValidationError(`Human approval step "${step.name}" must not have a profile or prompt`);
|
|
476
|
+
}
|
|
477
|
+
if (step.allow_manual_move !== true) {
|
|
478
|
+
throw new ValidationError(`Human approval step "${step.name}" must allow manual moves`);
|
|
479
|
+
}
|
|
480
|
+
if (step.auto_advance_requires_signal !== false) {
|
|
481
|
+
throw new ValidationError(`Human approval step "${step.name}" must not require an agent signal`);
|
|
482
|
+
}
|
|
483
|
+
if (step.complete_task_on_enter === true || step.is_start_step === true) {
|
|
484
|
+
throw new ValidationError(`Human approval step "${step.name}" must not complete or start the task`);
|
|
485
|
+
}
|
|
486
|
+
if ((step.events?.on_enter || []).length || (step.events?.on_turn_complete || []).length) {
|
|
487
|
+
throw new ValidationError(`Human approval step "${step.name}" must not run or transition an agent`);
|
|
488
|
+
}
|
|
489
|
+
stages[index] = { ...stage, stepDefinition: { ...step, prompt: "" } };
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
if (!step.profile_ref) throw new ValidationError(`Step "${step.name}" has no profile_ref`);
|
|
493
|
+
if (step.profile_ref !== stage.profile_name) {
|
|
494
|
+
throw new ValidationError(
|
|
495
|
+
`Step "${step.name}" profile_ref "${step.profile_ref}" does not match "${stage.profile_name}"`,
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
const includeTaskPrompt = stage.include_task_prompt !== false;
|
|
499
|
+
if (includeTaskPrompt && (!step.prompt || !step.prompt.includes("{{task_prompt}}"))) {
|
|
500
|
+
throw new ValidationError(`Step "${step.name}" prompt must include {{task_prompt}}`);
|
|
501
|
+
}
|
|
502
|
+
if (!includeTaskPrompt && step.prompt && step.prompt.includes("{{task_prompt}}")) {
|
|
503
|
+
throw new ValidationError(`Step "${step.name}" must not include {{task_prompt}}`);
|
|
504
|
+
}
|
|
505
|
+
if (!step.prompt.includes(".agentic/")) {
|
|
506
|
+
throw new ValidationError(`Step "${step.name}" prompt must name an .agentic/ artifact`);
|
|
507
|
+
}
|
|
508
|
+
if (step.auto_advance_requires_signal !== true) {
|
|
509
|
+
throw new ValidationError(`Step "${step.name}" must set auto_advance_requires_signal: true`);
|
|
510
|
+
}
|
|
511
|
+
if ((step.is_start_step === true) !== (index === 0)) {
|
|
512
|
+
throw new ValidationError(`Step "${step.name}" has an invalid is_start_step flag`);
|
|
513
|
+
}
|
|
514
|
+
if (step.complete_task_on_enter === true) {
|
|
515
|
+
throw new ValidationError(`Stage step "${step.name}" must not set complete_task_on_enter`);
|
|
516
|
+
}
|
|
517
|
+
const transitions = step.events?.on_turn_complete || [];
|
|
518
|
+
const moves = transitions.filter((e) => e.type === "move_to_step");
|
|
519
|
+
if (moves.length !== 0) {
|
|
520
|
+
throw new ValidationError(
|
|
521
|
+
`Step "${step.name}" must not transition itself; the external gate moves the task after validating its artifact`,
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
if (Boolean(step.allow_manual_move) !== false) {
|
|
525
|
+
throw new ValidationError(`Step "${step.name}": allow_manual_move must be false`);
|
|
526
|
+
}
|
|
527
|
+
stages[index] = { ...stage, stepDefinition: { ...step, prompt: normalizePrompt(step.prompt) } };
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
return { pipeline, document: doc, workflow: { ...workflow, steps: byPosition }, steps: byPosition, stages };
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** Convert a stage into a stable comparable projection used by sync/verify. */
|
|
534
|
+
export function stepProjection(step) {
|
|
535
|
+
return {
|
|
536
|
+
name: step.name,
|
|
537
|
+
position: step.position,
|
|
538
|
+
color: step.color,
|
|
539
|
+
prompt: normalizePrompt(step.prompt),
|
|
540
|
+
events: step.events || {},
|
|
541
|
+
is_start_step: Boolean(step.is_start_step),
|
|
542
|
+
show_in_command_panel: Boolean(step.show_in_command_panel),
|
|
543
|
+
allow_manual_move: Boolean(step.allow_manual_move),
|
|
544
|
+
auto_advance_requires_signal: Boolean(step.auto_advance_requires_signal),
|
|
545
|
+
cancel_triggers_turn_complete: Boolean(step.cancel_triggers_turn_complete),
|
|
546
|
+
complete_task_on_enter: Boolean(step.complete_task_on_enter),
|
|
547
|
+
};
|
|
548
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** Archive a blocked Planning attempt and ask the gate to re-enter the Gateway. */
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
|
|
6
|
+
const ARCHIVE_NAMES = [
|
|
7
|
+
"doc-review-runs",
|
|
8
|
+
"doc-review.log",
|
|
9
|
+
"doc-review.status.json",
|
|
10
|
+
"planning-blocked.json",
|
|
11
|
+
"spec.json",
|
|
12
|
+
"spec-review.json",
|
|
13
|
+
"speckit-analyze.md",
|
|
14
|
+
"gateway.json",
|
|
15
|
+
"bugfix.json",
|
|
16
|
+
"bugfix-repro.mjs",
|
|
17
|
+
"bugfix-result.json",
|
|
18
|
+
"bugfix-runs",
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
export function replanFile(repoDir) {
|
|
22
|
+
return path.join(path.resolve(repoDir), ".agentic", "replan.json");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function readReplanRequest(repoDir) {
|
|
26
|
+
const root = path.resolve(repoDir);
|
|
27
|
+
try {
|
|
28
|
+
const data = JSON.parse(fs.readFileSync(replanFile(root), "utf8"));
|
|
29
|
+
if (data?.stage !== "planning") return null;
|
|
30
|
+
if (typeof data?.requestedAt !== "string" || Number.isNaN(Date.parse(data.requestedAt))) return null;
|
|
31
|
+
if (typeof data?.archivedTo !== "string") return null;
|
|
32
|
+
const agentic = path.join(root, ".agentic");
|
|
33
|
+
const archive = path.resolve(root, data.archivedTo);
|
|
34
|
+
if (archive !== agentic && !archive.startsWith(`${agentic}${path.sep}`)) return null;
|
|
35
|
+
if (!fs.existsSync(archive)) return null;
|
|
36
|
+
return { ...data, archive };
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function clearReplanRequest(repoDir) {
|
|
43
|
+
const file = replanFile(repoDir);
|
|
44
|
+
if (fs.existsSync(file)) fs.rmSync(file);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function featureDir(repoDir) {
|
|
48
|
+
try {
|
|
49
|
+
const spec = JSON.parse(fs.readFileSync(path.join(repoDir, ".agentic", "spec.json"), "utf8"));
|
|
50
|
+
const value = spec?.feature_dir;
|
|
51
|
+
if (typeof value !== "string" || !value.startsWith("specs/")) return null;
|
|
52
|
+
const normalized = path.posix.normalize(value.replaceAll("\\", "/"));
|
|
53
|
+
if (normalized === ".." || normalized.startsWith("../") || path.posix.isAbsolute(normalized)) return null;
|
|
54
|
+
return normalized;
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function resetPlanningAttempt(repoDir, { task = null, reason = "manual replan", now = new Date() } = {}) {
|
|
61
|
+
const root = path.resolve(repoDir);
|
|
62
|
+
const agentic = path.join(root, ".agentic");
|
|
63
|
+
fs.mkdirSync(agentic, { recursive: true });
|
|
64
|
+
const stamp = now.toISOString().replace(/[:.]/g, "-");
|
|
65
|
+
const base = path.join(agentic, "rework", `planning-replan-${stamp}`);
|
|
66
|
+
let archive = base;
|
|
67
|
+
let suffix = 1;
|
|
68
|
+
while (fs.existsSync(archive)) archive = `${base}-${suffix++}`;
|
|
69
|
+
fs.mkdirSync(archive, { recursive: true });
|
|
70
|
+
const moved = [];
|
|
71
|
+
let archivedFeature = null;
|
|
72
|
+
const feature = featureDir(root);
|
|
73
|
+
if (feature) {
|
|
74
|
+
const source = path.join(root, feature);
|
|
75
|
+
if (fs.existsSync(source)) {
|
|
76
|
+
const tracked = spawnSync("git", ["-C", root, "ls-files", "--", feature], { encoding: "utf8" });
|
|
77
|
+
if (tracked.status === 0 && tracked.stdout.trim()) {
|
|
78
|
+
throw new Error(`feature directory has tracked files: ${feature}`);
|
|
79
|
+
}
|
|
80
|
+
const target = path.join(archive, feature);
|
|
81
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
82
|
+
fs.renameSync(source, target);
|
|
83
|
+
archivedFeature = feature;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const name of ARCHIVE_NAMES) {
|
|
87
|
+
const source = path.join(agentic, name);
|
|
88
|
+
if (!fs.existsSync(source)) continue;
|
|
89
|
+
fs.renameSync(source, path.join(archive, name));
|
|
90
|
+
moved.push(name);
|
|
91
|
+
}
|
|
92
|
+
const marker = {
|
|
93
|
+
stage: "planning",
|
|
94
|
+
task,
|
|
95
|
+
requestedAt: now.toISOString(),
|
|
96
|
+
reason,
|
|
97
|
+
archivedTo: path.relative(root, archive),
|
|
98
|
+
moved,
|
|
99
|
+
archivedFeature,
|
|
100
|
+
};
|
|
101
|
+
fs.writeFileSync(replanFile(root), `${JSON.stringify(marker, null, 2)}\n`);
|
|
102
|
+
return marker;
|
|
103
|
+
}
|