@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,325 @@
|
|
|
1
|
+
/** Bugfix reproduction contract and run evidence shared by the gate and Stage 3. */
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
export const BUGFIX_FILE = ".agentic/bugfix.json";
|
|
7
|
+
export const BUGFIX_RUN_DIR = ".agentic/bugfix-runs";
|
|
8
|
+
export const BUGFIX_PHASES = Object.freeze([
|
|
9
|
+
"planning-before",
|
|
10
|
+
"implementation-before",
|
|
11
|
+
"implementation-after",
|
|
12
|
+
"e2e-after",
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
const FRAMEWORKS = new Set(["node-test", "junit", "playwright", "other"]);
|
|
16
|
+
|
|
17
|
+
function isObject(value) {
|
|
18
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function nonEmptyString(value) {
|
|
22
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function validTimestamp(value) {
|
|
26
|
+
return nonEmptyString(value) && Number.isFinite(Date.parse(value));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function readJson(file) {
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function sha256File(file) {
|
|
38
|
+
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function validRelativePath(value) {
|
|
42
|
+
if (!nonEmptyString(value)) return false;
|
|
43
|
+
const normalized = path.posix.normalize(value.replaceAll("\\", "/"));
|
|
44
|
+
return normalized !== ".." && !normalized.startsWith("../") && !path.posix.isAbsolute(normalized);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function repoRoot(repoDir) {
|
|
48
|
+
return path.resolve(repoDir);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Resolve a relative path inside the worktree, optionally requiring an existing file. */
|
|
52
|
+
export function resolveInside(repoDir, rel, { mustExist = false, file = false } = {}) {
|
|
53
|
+
if (!validRelativePath(rel)) return null;
|
|
54
|
+
const root = repoRoot(repoDir);
|
|
55
|
+
const target = path.resolve(root, rel);
|
|
56
|
+
if (target !== root && !target.startsWith(`${root}${path.sep}`)) return null;
|
|
57
|
+
if (!mustExist) return target;
|
|
58
|
+
if (!fs.existsSync(target)) return null;
|
|
59
|
+
const real = fs.realpathSync(target);
|
|
60
|
+
const realRoot = fs.realpathSync(root);
|
|
61
|
+
if (real !== realRoot && !real.startsWith(`${realRoot}${path.sep}`)) return null;
|
|
62
|
+
if (file && !fs.statSync(real).isFile()) return null;
|
|
63
|
+
return real;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function insideAgentic(repoDir, rel, options = {}) {
|
|
67
|
+
const target = resolveInside(repoDir, rel, options);
|
|
68
|
+
if (!target) return null;
|
|
69
|
+
const root = repoRoot(repoDir);
|
|
70
|
+
const agentic = options.mustExist && fs.existsSync(path.join(root, ".agentic"))
|
|
71
|
+
? fs.realpathSync(path.join(root, ".agentic"))
|
|
72
|
+
: path.join(root, ".agentic");
|
|
73
|
+
const compare = options.mustExist ? target : path.resolve(target);
|
|
74
|
+
return compare === agentic || compare.startsWith(`${agentic}${path.sep}`) ? target : null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function sourcePathProblems(repoDir, rel, label) {
|
|
78
|
+
if (!validRelativePath(rel)) return [`${label} must be a relative path`];
|
|
79
|
+
const normalized = path.posix.normalize(rel.replaceAll("\\", "/"));
|
|
80
|
+
if (normalized.startsWith(".agentic/") || normalized.startsWith("specs/") || normalized.startsWith(".specify/")) {
|
|
81
|
+
return [`${label} must point at an application or configuration file`];
|
|
82
|
+
}
|
|
83
|
+
if (!resolveInside(repoDir, rel, { mustExist: true, file: true })) {
|
|
84
|
+
return [`${label} is not an existing file inside the worktree: ${rel}`];
|
|
85
|
+
}
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Validate `.agentic/bugfix.json` against the planning spec and repository state. */
|
|
90
|
+
export function contractProblems(repoDir, spec, data) {
|
|
91
|
+
if (!isObject(data)) return ["bugfix.json must be an object"];
|
|
92
|
+
const problems = [];
|
|
93
|
+
if (data.stage !== "bugfix") problems.push("bugfix.json stage must be bugfix");
|
|
94
|
+
if (data.verdict !== "PASS") problems.push("bugfix.json verdict must be PASS");
|
|
95
|
+
if (data.task !== spec?.task) problems.push("bugfix.json task does not match spec.json task");
|
|
96
|
+
if (!validTimestamp(data.timestamp)) problems.push("bugfix.json timestamp must be ISO-8601");
|
|
97
|
+
|
|
98
|
+
const issue = data.issue;
|
|
99
|
+
if (!isObject(issue)) {
|
|
100
|
+
problems.push("bugfix.json issue is missing");
|
|
101
|
+
} else {
|
|
102
|
+
for (const key of ["summary", "source", "observed", "expected"]) {
|
|
103
|
+
if (!nonEmptyString(issue[key])) problems.push(`bugfix.json issue.${key} is missing`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const reproduction = data.reproduction;
|
|
108
|
+
let scenarioId = null;
|
|
109
|
+
if (!isObject(reproduction)) {
|
|
110
|
+
problems.push("bugfix.json reproduction is missing");
|
|
111
|
+
} else {
|
|
112
|
+
scenarioId = nonEmptyString(reproduction.scenario_id) ? reproduction.scenario_id : null;
|
|
113
|
+
if (!scenarioId) problems.push("bugfix.json reproduction.scenario_id is missing");
|
|
114
|
+
if (!validRelativePath(reproduction.driver) || !insideAgentic(repoDir, reproduction.driver, { mustExist: true, file: true })) {
|
|
115
|
+
problems.push("bugfix.json reproduction.driver must be an existing file inside .agentic/");
|
|
116
|
+
}
|
|
117
|
+
if (!Array.isArray(reproduction.command) || reproduction.command.length === 0
|
|
118
|
+
|| reproduction.command.some((part) => !nonEmptyString(part))) {
|
|
119
|
+
problems.push("bugfix.json reproduction.command must be a non-empty argv array");
|
|
120
|
+
}
|
|
121
|
+
if (reproduction.expected_before !== "failed") problems.push("bugfix.json reproduction.expected_before must be failed");
|
|
122
|
+
if (reproduction.expected_after !== "passed") problems.push("bugfix.json reproduction.expected_after must be passed");
|
|
123
|
+
if (reproduction.timeout_ms !== undefined
|
|
124
|
+
&& (!Number.isInteger(reproduction.timeout_ms) || reproduction.timeout_ms <= 0)) {
|
|
125
|
+
problems.push("bugfix.json reproduction.timeout_ms must be a positive integer");
|
|
126
|
+
}
|
|
127
|
+
if (!validRelativePath(reproduction.result_path) || !insideAgentic(repoDir, reproduction.result_path)) {
|
|
128
|
+
problems.push("bugfix.json reproduction.result_path must be inside .agentic/");
|
|
129
|
+
}
|
|
130
|
+
const files = reproduction.files;
|
|
131
|
+
if (!Array.isArray(files) || files.length === 0) {
|
|
132
|
+
problems.push("bugfix.json reproduction.files is empty");
|
|
133
|
+
} else {
|
|
134
|
+
const seen = new Set();
|
|
135
|
+
for (const [index, file] of files.entries()) {
|
|
136
|
+
if (!isObject(file)) {
|
|
137
|
+
problems.push(`bugfix.json reproduction.files[${index}] must be an object`);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (seen.has(file.path)) problems.push(`bugfix.json reproduction.files contains duplicate ${file.path}`);
|
|
141
|
+
else seen.add(file.path);
|
|
142
|
+
const target = insideAgentic(repoDir, file.path, { mustExist: true, file: true });
|
|
143
|
+
if (!target) problems.push(`bugfix.json reproduction file is missing inside .agentic/: ${file.path}`);
|
|
144
|
+
else if (!/^[0-9a-f]{64}$/.test(String(file.sha256 || ""))) {
|
|
145
|
+
problems.push(`bugfix.json reproduction file lacks sha256: ${file.path}`);
|
|
146
|
+
} else if (sha256File(target) !== file.sha256) {
|
|
147
|
+
problems.push(`bugfix.json reproduction file hash changed: ${file.path}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!files.some((file) => file?.path === reproduction.driver)) {
|
|
151
|
+
problems.push("bugfix.json reproduction.driver must be listed in reproduction.files");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const rootCause = data.root_cause;
|
|
157
|
+
if (!isObject(rootCause)) {
|
|
158
|
+
problems.push("bugfix.json root_cause is missing");
|
|
159
|
+
} else {
|
|
160
|
+
if (!nonEmptyString(rootCause.summary)) problems.push("bugfix.json root_cause.summary is missing");
|
|
161
|
+
if (!Array.isArray(rootCause.causal_chain) || rootCause.causal_chain.length < 2
|
|
162
|
+
|| rootCause.causal_chain.some((step) => !nonEmptyString(step))) {
|
|
163
|
+
problems.push("bugfix.json root_cause.causal_chain must contain at least two steps");
|
|
164
|
+
}
|
|
165
|
+
if (!Array.isArray(rootCause.evidence) || rootCause.evidence.length === 0) {
|
|
166
|
+
problems.push("bugfix.json root_cause.evidence is empty");
|
|
167
|
+
} else {
|
|
168
|
+
for (const [index, evidence] of rootCause.evidence.entries()) {
|
|
169
|
+
if (!isObject(evidence)) {
|
|
170
|
+
problems.push(`bugfix.json root_cause.evidence[${index}] must be an object`);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
problems.push(...sourcePathProblems(repoDir, evidence.path, `root_cause.evidence[${index}].path`));
|
|
174
|
+
if (!Number.isInteger(evidence.line) || evidence.line < 1) {
|
|
175
|
+
problems.push(`bugfix.json root_cause.evidence[${index}].line must be a positive integer`);
|
|
176
|
+
}
|
|
177
|
+
if (!nonEmptyString(evidence.note)) {
|
|
178
|
+
problems.push(`bugfix.json root_cause.evidence[${index}].note is missing`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (!Array.isArray(data.fix_scope) || data.fix_scope.length === 0
|
|
185
|
+
|| data.fix_scope.some((entry) => !nonEmptyString(entry))) {
|
|
186
|
+
problems.push("bugfix.json fix_scope must be a non-empty string array");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const regression = data.regression_test;
|
|
190
|
+
if (!isObject(regression)) {
|
|
191
|
+
problems.push("bugfix.json regression_test is missing");
|
|
192
|
+
} else {
|
|
193
|
+
if (!validRelativePath(regression.path)
|
|
194
|
+
|| path.posix.normalize(regression.path.replaceAll("\\", "/")).startsWith(".agentic/")) {
|
|
195
|
+
problems.push("bugfix.json regression_test.path must be a relative repository path outside .agentic/");
|
|
196
|
+
}
|
|
197
|
+
if (!Array.isArray(regression.command) || regression.command.length === 0
|
|
198
|
+
|| regression.command.some((part) => !nonEmptyString(part))) {
|
|
199
|
+
problems.push("bugfix.json regression_test.command must be a non-empty argv array");
|
|
200
|
+
}
|
|
201
|
+
if (!FRAMEWORKS.has(regression.framework)) {
|
|
202
|
+
problems.push("bugfix.json regression_test.framework is unsupported");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return problems;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function readContract(repoDir) {
|
|
210
|
+
const file = path.join(repoRoot(repoDir), BUGFIX_FILE);
|
|
211
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
|
|
212
|
+
return { data: null, problems: ["bugfix.json missing"] };
|
|
213
|
+
}
|
|
214
|
+
const data = readJson(file);
|
|
215
|
+
return data === null
|
|
216
|
+
? { data: null, problems: ["bugfix.json is not valid JSON"] }
|
|
217
|
+
: { data, problems: [] };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function readRun(repoDir, phase) {
|
|
221
|
+
const file = path.join(repoRoot(repoDir), BUGFIX_RUN_DIR, `${phase}.json`);
|
|
222
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
|
|
223
|
+
return { data: null, problems: [`${phase} run result missing`] };
|
|
224
|
+
}
|
|
225
|
+
const data = readJson(file);
|
|
226
|
+
return data === null
|
|
227
|
+
? { data: null, problems: [`${phase} run result is not valid JSON`] }
|
|
228
|
+
: { data, problems: [] };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Validate one phase result and its copied driver result. */
|
|
232
|
+
export function runProblems(repoDir, data, { phase, status, scenarioId, requireRegression = false } = {}) {
|
|
233
|
+
if (!isObject(data)) return [`${phase} run result must be an object`];
|
|
234
|
+
const problems = [];
|
|
235
|
+
if (data.phase !== phase) problems.push(`${phase} run result phase is ${data.phase ?? "missing"}`);
|
|
236
|
+
if (data.scenario_id !== scenarioId) problems.push(`${phase} run result scenario_id does not match ${scenarioId}`);
|
|
237
|
+
if (data.status !== status) problems.push(`${phase} run result status is ${data.status ?? "missing"}, expected ${status}`);
|
|
238
|
+
if (!Number.isInteger(data.exit_code)) {
|
|
239
|
+
problems.push(`${phase} run result exit_code is missing`);
|
|
240
|
+
} else if (status === "failed" && data.exit_code === 0) {
|
|
241
|
+
problems.push(`${phase} run result failed with exit_code 0`);
|
|
242
|
+
} else if (status === "passed" && data.exit_code !== 0) {
|
|
243
|
+
problems.push(`${phase} run result passed with exit_code ${data.exit_code}`);
|
|
244
|
+
}
|
|
245
|
+
if (!nonEmptyString(data.evidence)) problems.push(`${phase} run result evidence is missing`);
|
|
246
|
+
if (!validTimestamp(data.timestamp)) problems.push(`${phase} run result timestamp must be ISO-8601`);
|
|
247
|
+
|
|
248
|
+
const log = insideAgentic(repoDir, data.log, { mustExist: true, file: true });
|
|
249
|
+
if (!log) problems.push(`${phase} run log is missing inside .agentic/`);
|
|
250
|
+
else if (fs.statSync(log).size === 0) problems.push(`${phase} run log is empty`);
|
|
251
|
+
|
|
252
|
+
const result = insideAgentic(repoDir, data.result_path, { mustExist: true, file: true });
|
|
253
|
+
if (!result) {
|
|
254
|
+
problems.push(`${phase} driver result is missing inside .agentic/`);
|
|
255
|
+
} else {
|
|
256
|
+
const driver = readJson(result);
|
|
257
|
+
if (!isObject(driver) || driver.scenario !== scenarioId) {
|
|
258
|
+
problems.push(`${phase} driver result scenario does not match ${scenarioId}`);
|
|
259
|
+
} else if (driver.status !== status) {
|
|
260
|
+
problems.push(`${phase} driver result status is ${driver.status ?? "missing"}, expected ${status}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (requireRegression) {
|
|
265
|
+
const regression = data.regression_test;
|
|
266
|
+
if (!isObject(regression)) {
|
|
267
|
+
problems.push(`${phase} regression_test result is missing`);
|
|
268
|
+
} else {
|
|
269
|
+
if (!validRelativePath(regression.path)) problems.push(`${phase} regression_test.path is invalid`);
|
|
270
|
+
if (!Array.isArray(regression.command) || regression.command.length === 0) {
|
|
271
|
+
problems.push(`${phase} regression_test.command is missing`);
|
|
272
|
+
}
|
|
273
|
+
if (regression.exit_code !== 0) problems.push(`${phase} regression_test.exit_code is not 0`);
|
|
274
|
+
const target = resolveInside(repoDir, regression.path, { mustExist: true, file: true });
|
|
275
|
+
if (!target) problems.push(`${phase} regression_test.path is missing: ${regression.path}`);
|
|
276
|
+
else if (!/^[0-9a-f]{64}$/.test(String(regression.sha256 || ""))) {
|
|
277
|
+
problems.push(`${phase} regression_test.sha256 is missing`);
|
|
278
|
+
} else if (sha256File(target) !== regression.sha256) {
|
|
279
|
+
problems.push(`${phase} regression_test hash changed`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return problems;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Hash the planning contract and reproduction files for the stage boundary. */
|
|
288
|
+
export function pinsFor(repoDir, spec) {
|
|
289
|
+
const root = repoRoot(repoDir);
|
|
290
|
+
const specPath = ".agentic/spec.json";
|
|
291
|
+
const pins = {
|
|
292
|
+
spec: { path: specPath, sha256: sha256File(path.join(root, specPath)) },
|
|
293
|
+
bugfix: null,
|
|
294
|
+
reproduction: [],
|
|
295
|
+
};
|
|
296
|
+
if (spec?.change_type !== "bugfix") return pins;
|
|
297
|
+
const bugfixPath = path.join(root, BUGFIX_FILE);
|
|
298
|
+
pins.bugfix = { path: BUGFIX_FILE, sha256: sha256File(bugfixPath) };
|
|
299
|
+
const contract = readJson(bugfixPath);
|
|
300
|
+
pins.reproduction = (contract?.reproduction?.files || []).map((file) => ({
|
|
301
|
+
path: file.path,
|
|
302
|
+
sha256: file.sha256,
|
|
303
|
+
}));
|
|
304
|
+
return pins;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function pinProblems(repoDir, pins) {
|
|
308
|
+
if (!pins) return [];
|
|
309
|
+
const problems = [];
|
|
310
|
+
const check = (pin, label) => {
|
|
311
|
+
if (!pin) return;
|
|
312
|
+
const target = resolveInside(repoDir, pin.path, { mustExist: true, file: true });
|
|
313
|
+
if (!target) {
|
|
314
|
+
problems.push(`${label} pinned file is missing: ${pin.path}`);
|
|
315
|
+
} else if (sha256File(target) !== pin.sha256) {
|
|
316
|
+
problems.push(`${label} pinned file changed: ${pin.path}`);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
check(pins.spec, "spec.json");
|
|
320
|
+
check(pins.bugfix, "bugfix.json");
|
|
321
|
+
for (const pin of Array.isArray(pins.reproduction) ? pins.reproduction : []) {
|
|
322
|
+
check(pin, "bugfix reproduction");
|
|
323
|
+
}
|
|
324
|
+
return problems;
|
|
325
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/** Fingerprint the installed Kandev package/runtime and application directory. */
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
const HOME_EXCLUDES = [
|
|
8
|
+
"logs",
|
|
9
|
+
"tasks",
|
|
10
|
+
"sessions",
|
|
11
|
+
"tmp",
|
|
12
|
+
"plugins",
|
|
13
|
+
"supervisor",
|
|
14
|
+
"agentctl-journals",
|
|
15
|
+
"data",
|
|
16
|
+
"repos",
|
|
17
|
+
"worktrees",
|
|
18
|
+
"backups",
|
|
19
|
+
"artifacts",
|
|
20
|
+
"cache",
|
|
21
|
+
".kandev-backend.lock",
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export const BASELINE_VERSION = 2;
|
|
25
|
+
|
|
26
|
+
export const GATE_CODE_FILES = [
|
|
27
|
+
"scripts/gate.mjs",
|
|
28
|
+
"scripts/lib/artifacts.mjs",
|
|
29
|
+
"scripts/lib/bugfix.mjs",
|
|
30
|
+
"scripts/lib/checksum.mjs",
|
|
31
|
+
"scripts/lib/planning-replan.mjs",
|
|
32
|
+
"scripts/lib/resume.mjs",
|
|
33
|
+
"scripts/lib/stage-boundary.mjs",
|
|
34
|
+
"scripts/lib/task-worktree.mjs",
|
|
35
|
+
"config/pipeline.json",
|
|
36
|
+
"config/workflows/sdlc.yaml",
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
/** Hash the code that changes gate decisions, so a stale daemon is visible. */
|
|
40
|
+
export function gateCodeHash(root = process.cwd()) {
|
|
41
|
+
const hash = crypto.createHash("sha256");
|
|
42
|
+
for (const file of GATE_CODE_FILES) {
|
|
43
|
+
hash.update(file);
|
|
44
|
+
hash.update("\0");
|
|
45
|
+
hash.update(fs.readFileSync(path.join(path.resolve(root), file)));
|
|
46
|
+
hash.update("\0");
|
|
47
|
+
}
|
|
48
|
+
return hash.digest("hex");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isExcluded(rel, excludes) {
|
|
52
|
+
return excludes.some((entry) => rel === entry || rel.startsWith(`${entry}/`));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function walk(root, rel, excludes, out, prefix = "") {
|
|
56
|
+
const abs = path.join(root, rel);
|
|
57
|
+
let entries;
|
|
58
|
+
try {
|
|
59
|
+
entries = fs.readdirSync(abs, { withFileTypes: true });
|
|
60
|
+
} catch {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
64
|
+
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
65
|
+
if (isExcluded(childRel, excludes)) continue;
|
|
66
|
+
if (entry.isSymbolicLink()) {
|
|
67
|
+
out[`${prefix}${childRel}`] = `link:${fs.readlinkSync(path.join(root, childRel))}`;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (entry.isDirectory()) {
|
|
71
|
+
walk(root, childRel, excludes, out, prefix);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (!entry.isFile()) continue;
|
|
75
|
+
const hash = crypto.createHash("sha256");
|
|
76
|
+
hash.update(fs.readFileSync(path.join(root, childRel)));
|
|
77
|
+
out[`${prefix}${childRel}`] = hash.digest("hex");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function packageRootCandidates() {
|
|
82
|
+
const candidates = [];
|
|
83
|
+
if (process.env.KANDEV_PACKAGE_DIR) candidates.push(process.env.KANDEV_PACKAGE_DIR);
|
|
84
|
+
candidates.push(
|
|
85
|
+
path.join(os.homedir(), ".npm-global", "lib", "node_modules", "kandev"),
|
|
86
|
+
path.join(path.dirname(process.execPath), "..", "lib", "node_modules", "kandev"),
|
|
87
|
+
"/usr/local/lib/node_modules/kandev",
|
|
88
|
+
"/usr/lib/node_modules/kandev",
|
|
89
|
+
);
|
|
90
|
+
return candidates.map((candidate) => path.resolve(candidate));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isKandevPackage(dir) {
|
|
94
|
+
const manifest = path.join(dir, "package.json");
|
|
95
|
+
if (!fs.existsSync(manifest)) return false;
|
|
96
|
+
try {
|
|
97
|
+
return JSON.parse(fs.readFileSync(manifest, "utf8")).name === "kandev";
|
|
98
|
+
} catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Resolve both the Kandev package root and the native runtime bundle. */
|
|
104
|
+
export function resolveInstallRoots() {
|
|
105
|
+
const packageRoot = packageRootCandidates().find(isKandevPackage);
|
|
106
|
+
if (!packageRoot) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"Kandev package root not found; set KANDEV_PACKAGE_DIR or install kandev under ~/.npm-global",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
if (process.env.KANDEV_BUNDLE_DIR) {
|
|
112
|
+
const bundleDir = path.resolve(process.env.KANDEV_BUNDLE_DIR);
|
|
113
|
+
if (!fs.existsSync(bundleDir)) throw new Error(`KANDEV_BUNDLE_DIR does not exist: ${bundleDir}`);
|
|
114
|
+
return { packageRoot, bundleDir };
|
|
115
|
+
}
|
|
116
|
+
const platform = `@kdlbs/runtime-${process.platform}-${process.arch}`;
|
|
117
|
+
const bundleDir = path.join(packageRoot, "node_modules", platform);
|
|
118
|
+
if (!fs.existsSync(bundleDir)) {
|
|
119
|
+
throw new Error(`Kandev runtime bundle missing: ${bundleDir}`);
|
|
120
|
+
}
|
|
121
|
+
return { packageRoot, bundleDir };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function fingerprint({
|
|
125
|
+
roots = resolveInstallRoots(),
|
|
126
|
+
homeDir = process.env.KANDEV_HOME || path.join(os.homedir(), ".kandev"),
|
|
127
|
+
} = {}) {
|
|
128
|
+
const { packageRoot, bundleDir } = roots;
|
|
129
|
+
const files = {};
|
|
130
|
+
walk(packageRoot, "", [], files, "package/");
|
|
131
|
+
walk(bundleDir, "", [], files, "bundle/");
|
|
132
|
+
if (fs.existsSync(homeDir)) {
|
|
133
|
+
walk(homeDir, "", HOME_EXCLUDES, files, "home/");
|
|
134
|
+
}
|
|
135
|
+
return { roots: { packageRoot, bundleDir, homeDir }, files, count: Object.keys(files).length };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function baselinePath(root = process.cwd()) {
|
|
139
|
+
return path.join(root, ".cache", "install-baseline.json");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function readBaseline(root = process.cwd()) {
|
|
143
|
+
const file = baselinePath(root);
|
|
144
|
+
if (!fs.existsSync(file)) return null;
|
|
145
|
+
const data = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
146
|
+
if (data.version !== BASELINE_VERSION) return { version: data.version, stale: true };
|
|
147
|
+
return data;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function writeBaseline(snapshot, root = process.cwd()) {
|
|
151
|
+
const file = baselinePath(root);
|
|
152
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
153
|
+
fs.writeFileSync(file, JSON.stringify({ version: BASELINE_VERSION, ...snapshot }, null, 2));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function compareFingerprint(before, after) {
|
|
157
|
+
const changes = [];
|
|
158
|
+
for (const [file, hash] of Object.entries(before.files)) {
|
|
159
|
+
if (!(file in after.files)) changes.push(`removed: ${file}`);
|
|
160
|
+
else if (after.files[file] !== hash) changes.push(`modified: ${file}`);
|
|
161
|
+
}
|
|
162
|
+
for (const file of Object.keys(after.files)) {
|
|
163
|
+
if (!(file in before.files)) changes.push(`added: ${file}`);
|
|
164
|
+
}
|
|
165
|
+
return changes;
|
|
166
|
+
}
|