@tea-agent/loop-agent 0.12.0 → 0.13.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -2
- package/README.md +30 -2
- package/dist/application/dag/generate-task-dag.js +30 -0
- package/dist/cli/command-definitions.js +10 -3
- package/dist/commands/knowledge.js +129 -31
- package/dist/governance/manifest-types.js +3 -0
- package/dist/task/config-types.js +5 -1
- package/dist/worker/cli.js +96 -1
- package/dist/worker/delivery/package.js +3 -3
- package/dist/worker/feature/decision-loader.js +37 -6
- package/dist/worker/feature/next-action.js +10 -2
- package/dist/worker/feature/ready-plan-projection.js +81 -0
- package/dist/worker/feature/reducer.js +2 -1
- package/dist/worker/feature/review.js +19 -2
- package/dist/worker/feature/run.js +27 -2
- package/dist/worker/follow-up/approve.js +5 -2
- package/dist/worker/follow-up/factory.js +1 -1
- package/dist/worker/observability/read-model.js +246 -41
- package/dist/worker/observe/routes.js +158 -15
- package/dist/worker/observe/spec-evidence.js +281 -0
- package/dist/worker/observe/static/api.js +19 -0
- package/dist/worker/observe/static/app.js +2 -2
- package/dist/worker/observe/static/relations.js +17 -12
- package/dist/worker/observe/static/router.js +8 -0
- package/dist/worker/observe/static/styles.css +12 -0
- package/dist/worker/observe/static/views/batch.js +3 -2
- package/dist/worker/observe/static/views/dag-inspector.js +123 -4
- package/dist/worker/observe/static/views/dashboard.js +8 -5
- package/dist/worker/observe/static/views/feature.js +43 -4
- package/dist/worker/observe/static/views/pool.js +5 -2
- package/dist/worker/observe/static/views/run.js +1 -1
- package/dist/worker/observe/static/views/task.js +69 -15
- package/dist/worker/pool/doctor.js +165 -0
- package/dist/worker/pool/migrate-state.js +303 -0
- package/dist/worker/pool/run-store.js +205 -17
- package/dist/worker/pool/types.js +17 -1
- package/dist/worker/pool/validation.js +100 -15
- package/dist/worker/report/morning-report.js +12 -2
- package/dist/worker/runner/run-ready.js +41 -26
- package/dist/worker/task-graph/ready-planner.js +136 -0
- package/dist/workflows/dag/convergence/controller.js +16 -8
- package/dist/workflows/dag/failure-routing.js +12 -1
- package/dist/workflows/dag/init-hybrid.js +837 -8
- package/dist/workflows/dag/types.js +1 -0
- package/docs/README.md +1 -1
- package/docs/agent-dag-recovery-playbook.md +9 -0
- package/docs/architecture/evolution.md +4 -3
- package/docs/architecture/facts-and-state.md +14 -1
- package/docs/architecture/worker-and-feature.md +6 -2
- package/docs/decisions/README.md +3 -0
- package/docs/design/README.md +8 -0
- package/docs/exec-plans/active/README.md +2 -0
- package/docs/exec-plans/completed/README.md +3 -2
- package/docs/feature-workflow.md +80 -2
- package/docs/loop-agent-harness.md +15 -4
- package/docs/progress/README.md +4 -0
- package/docs/reports/README.md +6 -0
- package/docs/templates/backend-test-dag.json +12 -0
- package/docs/templates/knowledge-graph-bootstrap-dag.json +118 -0
- package/docs/templates/knowledge-sync-dag.json +177 -0
- package/docs/templates/knowledge-sync-draft.schema.json +71 -0
- package/docs/verification-matrix.md +2 -1
- package/package.json +8 -2
- package/scripts/kb-bootstrap-init-skeleton.sh +239 -0
- package/scripts/kb-graph-incremental-prepare.mjs +372 -0
- package/scripts/kb-graph-incremental-prepare.sh +5 -0
- package/scripts/kb-graph-materialize.mjs +105 -0
- package/scripts/kb-graph-materialize.sh +4 -0
- package/scripts/kb-graph-promote.mjs +153 -0
- package/scripts/kb-graph-promote.sh +4 -0
- package/scripts/kb-query.mjs +554 -0
- package/scripts/kb-query.sh +5 -0
- package/skills/agent-worker/SKILL.md +3 -1
- package/skills/agent-worker/references/agent-worker-operator.md +18 -1
- package/skills/frontend-design-review/SKILL.md +26 -24
- package/skills/frontend-implementation/SKILL.md +29 -26
- package/skills/frontend-implementation/references/node-contracts.md +50 -19
- package/skills/frontend-review/SKILL.md +1 -1
- package/skills/loop-agent/references/command-reference.md +1 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Prepare narrowed scope/status for incremental knowledge-graph updates.
|
|
4
|
+
* Reuses knowledge-graph-bootstrap DAG; does not call an LLM.
|
|
5
|
+
*/
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import process from "node:process";
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
|
|
12
|
+
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
|
|
14
|
+
function usage(code = 2) {
|
|
15
|
+
const text = `Usage: node scripts/kb-graph-incremental-prepare.mjs --root <dir> [options]
|
|
16
|
+
|
|
17
|
+
Prepare an incremental graph-update run (same DAG as bootstrap, narrowed scope).
|
|
18
|
+
|
|
19
|
+
--root DIR Repository root (required)
|
|
20
|
+
--feature F-id Seed feature (repeatable)
|
|
21
|
+
--service NAME Seed service (repeatable)
|
|
22
|
+
--domain NAME Seed domain (repeatable)
|
|
23
|
+
--include GLOB include_paths entry (repeatable)
|
|
24
|
+
--reset-staging Clear knowledge/bootstrap/staging/**
|
|
25
|
+
--force-scope Rewrite scope.yaml from seeds (default: merge seeds)
|
|
26
|
+
--product-name NAME Used when creating missing B1 skeleton
|
|
27
|
+
-h, --help
|
|
28
|
+
`;
|
|
29
|
+
if (code === 0) console.log(text);
|
|
30
|
+
else console.error(text);
|
|
31
|
+
process.exit(code);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseArgs(argv) {
|
|
35
|
+
const out = {
|
|
36
|
+
root: null,
|
|
37
|
+
features: [],
|
|
38
|
+
services: [],
|
|
39
|
+
domains: [],
|
|
40
|
+
includes: [],
|
|
41
|
+
resetStaging: false,
|
|
42
|
+
forceScope: false,
|
|
43
|
+
productName: null,
|
|
44
|
+
};
|
|
45
|
+
for (let i = 0; i < argv.length; i++) {
|
|
46
|
+
const a = argv[i];
|
|
47
|
+
const next = () => {
|
|
48
|
+
const v = argv[++i];
|
|
49
|
+
if (v === undefined) throw new Error(`missing value for ${a}`);
|
|
50
|
+
return v;
|
|
51
|
+
};
|
|
52
|
+
switch (a) {
|
|
53
|
+
case "--root":
|
|
54
|
+
out.root = path.resolve(next());
|
|
55
|
+
break;
|
|
56
|
+
case "--feature":
|
|
57
|
+
out.features.push(next());
|
|
58
|
+
break;
|
|
59
|
+
case "--service":
|
|
60
|
+
out.services.push(next());
|
|
61
|
+
break;
|
|
62
|
+
case "--domain":
|
|
63
|
+
out.domains.push(next());
|
|
64
|
+
break;
|
|
65
|
+
case "--include":
|
|
66
|
+
out.includes.push(next());
|
|
67
|
+
break;
|
|
68
|
+
case "--reset-staging":
|
|
69
|
+
out.resetStaging = true;
|
|
70
|
+
break;
|
|
71
|
+
case "--force-scope":
|
|
72
|
+
out.forceScope = true;
|
|
73
|
+
break;
|
|
74
|
+
case "--product-name":
|
|
75
|
+
out.productName = next();
|
|
76
|
+
break;
|
|
77
|
+
case "-h":
|
|
78
|
+
case "--help":
|
|
79
|
+
usage(0);
|
|
80
|
+
break;
|
|
81
|
+
default:
|
|
82
|
+
throw new Error(`unknown argument: ${a}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function ensureSkeleton(root, productName) {
|
|
89
|
+
const scope = path.join(root, "knowledge/bootstrap/scope.yaml");
|
|
90
|
+
const status = path.join(root, "knowledge/bootstrap/status.yaml");
|
|
91
|
+
if (fs.existsSync(scope) && fs.existsSync(status)) return;
|
|
92
|
+
const skeleton = path.join(scriptDir, "kb-bootstrap-init-skeleton.sh");
|
|
93
|
+
const args = ["bash", skeleton, "--root", root];
|
|
94
|
+
if (productName) args.push("--product-name", productName);
|
|
95
|
+
const r = spawnSync(args[0], args.slice(1), { encoding: "utf8" });
|
|
96
|
+
if (r.status !== 0) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`failed to init B1 skeleton: ${r.stderr || r.stdout || r.status}`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function dumpYaml(obj, indent = 0) {
|
|
104
|
+
const sp = " ".repeat(indent);
|
|
105
|
+
if (Array.isArray(obj)) {
|
|
106
|
+
if (obj.length === 0) return "[]";
|
|
107
|
+
return obj.map((item) => `${sp}- ${formatScalar(item)}`).join("\n");
|
|
108
|
+
}
|
|
109
|
+
if (obj && typeof obj === "object") {
|
|
110
|
+
return Object.entries(obj)
|
|
111
|
+
.map(([k, v]) => {
|
|
112
|
+
if (Array.isArray(v)) {
|
|
113
|
+
if (v.length === 0) return `${sp}${k}: []`;
|
|
114
|
+
return `${sp}${k}:\n${v.map((item) => `${sp} - ${formatScalar(item)}`).join("\n")}`;
|
|
115
|
+
}
|
|
116
|
+
if (v && typeof v === "object") {
|
|
117
|
+
return `${sp}${k}:\n${dumpYaml(v, indent + 1)}`;
|
|
118
|
+
}
|
|
119
|
+
return `${sp}${k}: ${formatScalar(v)}`;
|
|
120
|
+
})
|
|
121
|
+
.join("\n");
|
|
122
|
+
}
|
|
123
|
+
return `${sp}${formatScalar(obj)}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function formatScalar(v) {
|
|
127
|
+
if (v === null || v === undefined) return "null";
|
|
128
|
+
if (typeof v === "boolean" || typeof v === "number") return String(v);
|
|
129
|
+
const s = String(v);
|
|
130
|
+
if (/[:#{}[\],&*?|>!%@`]/.test(s) || s === "" || /\s/.test(s)) {
|
|
131
|
+
return JSON.stringify(s);
|
|
132
|
+
}
|
|
133
|
+
return s;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function unique(list) {
|
|
137
|
+
return [...new Set(list.filter(Boolean))];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function buildIncludes(args) {
|
|
141
|
+
if (args.includes.length) return unique(args.includes);
|
|
142
|
+
const includes = ["features/**", "docs/**", "README.md"];
|
|
143
|
+
for (const f of args.features) includes.push(`features/${f}/**`);
|
|
144
|
+
for (const s of args.services) {
|
|
145
|
+
includes.push(
|
|
146
|
+
`services/${s}/**`,
|
|
147
|
+
`apps/${s}/**`,
|
|
148
|
+
`packages/${s}/**`,
|
|
149
|
+
"src/**",
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return unique(includes);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function mergeSeedsIntoScopeText(text, args) {
|
|
156
|
+
let next = text;
|
|
157
|
+
if (!/update_mode:\s*incremental/.test(next)) {
|
|
158
|
+
if (/update_mode:\s*\S+/.test(next)) {
|
|
159
|
+
next = next.replace(/update_mode:\s*\S+/, "update_mode: incremental");
|
|
160
|
+
} else {
|
|
161
|
+
next = next.replace(
|
|
162
|
+
/(schema_version:\s*\d+)/,
|
|
163
|
+
"$1\nupdate_mode: incremental",
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const upsert = (label, items) => {
|
|
168
|
+
if (!items.length) return;
|
|
169
|
+
const re = new RegExp(
|
|
170
|
+
`${label}:\\s*(\\[\\]|\\n(?:\\s+-\\s+[^\\n]+\\n)*)`,
|
|
171
|
+
);
|
|
172
|
+
const m = next.match(re);
|
|
173
|
+
const existing = new Set();
|
|
174
|
+
if (m) {
|
|
175
|
+
for (const line of m[1].split("\n")) {
|
|
176
|
+
const mm = line.match(/-\s+(\S+)/);
|
|
177
|
+
if (mm) existing.add(mm[1].replace(/^["']|["']$/g, ""));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
for (const it of items) existing.add(it);
|
|
181
|
+
const body = [...existing].map((i) => ` - ${i}`).join("\n");
|
|
182
|
+
const block = `${label}:\n${body}\n`;
|
|
183
|
+
if (m) next = next.replace(re, block);
|
|
184
|
+
else next += `\n${block}`;
|
|
185
|
+
};
|
|
186
|
+
upsert("seed_features", args.features);
|
|
187
|
+
upsert("seed_services", args.services);
|
|
188
|
+
upsert("seed_domains", args.domains);
|
|
189
|
+
return next.endsWith("\n") ? next : `${next}\n`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function resetStaging(root, bootstrapId) {
|
|
193
|
+
const staging = path.join(root, "knowledge/bootstrap/staging");
|
|
194
|
+
if (!fs.existsSync(staging)) {
|
|
195
|
+
fs.mkdirSync(staging, { recursive: true });
|
|
196
|
+
}
|
|
197
|
+
for (const ent of fs.readdirSync(staging, { withFileTypes: true })) {
|
|
198
|
+
if (ent.name === ".gitkeep") continue;
|
|
199
|
+
fs.rmSync(path.join(staging, ent.name), { recursive: true, force: true });
|
|
200
|
+
}
|
|
201
|
+
for (const d of [
|
|
202
|
+
"domains",
|
|
203
|
+
"services",
|
|
204
|
+
"modules",
|
|
205
|
+
"features-links.proposed",
|
|
206
|
+
]) {
|
|
207
|
+
fs.mkdirSync(path.join(staging, d), { recursive: true });
|
|
208
|
+
}
|
|
209
|
+
fs.writeFileSync(
|
|
210
|
+
path.join(staging, "notes.md"),
|
|
211
|
+
`# staging cleared for incremental run ${bootstrapId}\n`,
|
|
212
|
+
);
|
|
213
|
+
fs.writeFileSync(
|
|
214
|
+
path.join(staging, "edges.proposed.yaml"),
|
|
215
|
+
"schema_version: 1\nedges: []\n",
|
|
216
|
+
);
|
|
217
|
+
fs.writeFileSync(
|
|
218
|
+
path.join(staging, "coverage-notes.md"),
|
|
219
|
+
"# coverage notes (incremental)\n",
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function main() {
|
|
224
|
+
let args;
|
|
225
|
+
try {
|
|
226
|
+
args = parseArgs(process.argv.slice(2));
|
|
227
|
+
} catch (e) {
|
|
228
|
+
console.error(String(e?.message || e));
|
|
229
|
+
usage(2);
|
|
230
|
+
}
|
|
231
|
+
if (!args.root) {
|
|
232
|
+
console.error("--root is required");
|
|
233
|
+
usage(2);
|
|
234
|
+
}
|
|
235
|
+
if (
|
|
236
|
+
!args.features.length &&
|
|
237
|
+
!args.services.length &&
|
|
238
|
+
!args.domains.length &&
|
|
239
|
+
!args.includes.length
|
|
240
|
+
) {
|
|
241
|
+
console.error(
|
|
242
|
+
"at least one of --feature / --service / --domain / --include is required",
|
|
243
|
+
);
|
|
244
|
+
process.exit(2);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
ensureSkeleton(args.root, args.productName);
|
|
248
|
+
const ts = new Date().toISOString();
|
|
249
|
+
const bootstrapId = `BR-INC-${ts.replace(/[-:TZ.]/g, "").slice(0, 14)}`;
|
|
250
|
+
const scopePath = path.join(args.root, "knowledge/bootstrap/scope.yaml");
|
|
251
|
+
const statusPath = path.join(args.root, "knowledge/bootstrap/status.yaml");
|
|
252
|
+
const backupDir = path.join(
|
|
253
|
+
args.root,
|
|
254
|
+
"knowledge/bootstrap/runs",
|
|
255
|
+
bootstrapId,
|
|
256
|
+
);
|
|
257
|
+
fs.mkdirSync(backupDir, { recursive: true });
|
|
258
|
+
fs.copyFileSync(scopePath, path.join(backupDir, "scope.before.yaml"));
|
|
259
|
+
fs.copyFileSync(statusPath, path.join(backupDir, "status.before.yaml"));
|
|
260
|
+
|
|
261
|
+
const includes = buildIncludes(args);
|
|
262
|
+
const product =
|
|
263
|
+
args.productName || path.basename(args.root);
|
|
264
|
+
|
|
265
|
+
if (args.forceScope || !fs.existsSync(scopePath)) {
|
|
266
|
+
const data = {
|
|
267
|
+
schema_version: 1,
|
|
268
|
+
product_name: product,
|
|
269
|
+
primary_language: "zh-CN",
|
|
270
|
+
repo_kind: "single-service",
|
|
271
|
+
update_mode: "incremental",
|
|
272
|
+
include_paths: includes,
|
|
273
|
+
exclude_paths: ["node_modules/**", "dist/**", ".harness/**"],
|
|
274
|
+
id_namespace: {
|
|
275
|
+
domain_prefix: "DOM",
|
|
276
|
+
service_prefix: "SVC",
|
|
277
|
+
api_prefix: "API",
|
|
278
|
+
},
|
|
279
|
+
seed_domains: args.domains,
|
|
280
|
+
seed_services: args.services,
|
|
281
|
+
seed_features: args.features,
|
|
282
|
+
assumptions: [
|
|
283
|
+
"Incremental update: do not rebuild unrelated domains/services",
|
|
284
|
+
],
|
|
285
|
+
out_of_scope: ["full-repo bootstrap"],
|
|
286
|
+
human_owners: { architect: "", qa: "" },
|
|
287
|
+
};
|
|
288
|
+
fs.writeFileSync(
|
|
289
|
+
scopePath,
|
|
290
|
+
`# Incremental graph scope\n${dumpYaml(data)}\n`,
|
|
291
|
+
);
|
|
292
|
+
} else {
|
|
293
|
+
const merged = mergeSeedsIntoScopeText(
|
|
294
|
+
fs.readFileSync(scopePath, "utf8"),
|
|
295
|
+
args,
|
|
296
|
+
);
|
|
297
|
+
fs.writeFileSync(scopePath, merged);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const status = {
|
|
301
|
+
schema_version: 1,
|
|
302
|
+
bootstrap_id: bootstrapId,
|
|
303
|
+
repo_root: ".",
|
|
304
|
+
phase: "B2",
|
|
305
|
+
update_mode: "incremental",
|
|
306
|
+
updated_at: ts,
|
|
307
|
+
scope_ref: "knowledge/bootstrap/scope.yaml",
|
|
308
|
+
inventory_ref: "knowledge/bootstrap/inventory.json",
|
|
309
|
+
last_run_dir: `knowledge/bootstrap/runs/${bootstrapId}/`,
|
|
310
|
+
stats: {
|
|
311
|
+
entities_proposed: 0,
|
|
312
|
+
entities_asserted: 0,
|
|
313
|
+
edges_proposed: 0,
|
|
314
|
+
edges_asserted: 0,
|
|
315
|
+
features_linked: 0,
|
|
316
|
+
},
|
|
317
|
+
blockers: [],
|
|
318
|
+
next_actions: [
|
|
319
|
+
"Review knowledge/bootstrap/scope.yaml (update_mode: incremental)",
|
|
320
|
+
"Run knowledge-graph-bootstrap DAG (propose limited to scope seeds/includes)",
|
|
321
|
+
"Promote merge-new-only; materialize indexes",
|
|
322
|
+
],
|
|
323
|
+
};
|
|
324
|
+
fs.writeFileSync(
|
|
325
|
+
statusPath,
|
|
326
|
+
`# Incremental bootstrap status\n${dumpYaml(status)}\n`,
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
if (args.resetStaging) resetStaging(args.root, bootstrapId);
|
|
330
|
+
|
|
331
|
+
fs.writeFileSync(
|
|
332
|
+
path.join(backupDir, "README.md"),
|
|
333
|
+
[
|
|
334
|
+
`# Incremental prepare ${bootstrapId}`,
|
|
335
|
+
"",
|
|
336
|
+
`- update_mode: incremental`,
|
|
337
|
+
`- features: ${args.features.join(", ") || "(none)"}`,
|
|
338
|
+
`- services: ${args.services.join(", ") || "(none)"}`,
|
|
339
|
+
`- domains: ${args.domains.join(", ") || "(none)"}`,
|
|
340
|
+
"",
|
|
341
|
+
"Next:",
|
|
342
|
+
"```bash",
|
|
343
|
+
"loop-agent dag run-task <task-id> --profile knowledge-graph-bootstrap",
|
|
344
|
+
`bash scripts/kb-graph-materialize.sh --root ${args.root}`,
|
|
345
|
+
"```",
|
|
346
|
+
"",
|
|
347
|
+
].join("\n"),
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
console.log(
|
|
351
|
+
JSON.stringify(
|
|
352
|
+
{
|
|
353
|
+
ok: true,
|
|
354
|
+
bootstrap_id: bootstrapId,
|
|
355
|
+
update_mode: "incremental",
|
|
356
|
+
scope: "knowledge/bootstrap/scope.yaml",
|
|
357
|
+
status: "knowledge/bootstrap/status.yaml",
|
|
358
|
+
backup: `knowledge/bootstrap/runs/${bootstrapId}/`,
|
|
359
|
+
seeds: {
|
|
360
|
+
features: args.features,
|
|
361
|
+
services: args.services,
|
|
362
|
+
domains: args.domains,
|
|
363
|
+
},
|
|
364
|
+
next: "dag run-task --profile knowledge-graph-bootstrap",
|
|
365
|
+
},
|
|
366
|
+
null,
|
|
367
|
+
2,
|
|
368
|
+
),
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
main();
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Standalone B6-ish materialize: rebuild knowledge/graph/entities-index.yaml
|
|
4
|
+
* and edges.yaml from formal trees + edges.manual.yaml.
|
|
5
|
+
*/
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import process from "node:process";
|
|
9
|
+
|
|
10
|
+
function parseArgs(argv) {
|
|
11
|
+
let root = process.cwd();
|
|
12
|
+
for (let i = 0; i < argv.length; i++) {
|
|
13
|
+
if (argv[i] === "--root") root = path.resolve(argv[++i]);
|
|
14
|
+
else if (argv[i] === "-h" || argv[i] === "--help") {
|
|
15
|
+
console.log("Usage: node scripts/kb-graph-materialize.mjs [--root <dir>]");
|
|
16
|
+
process.exit(0);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return { root };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function walk(dir, acc = []) {
|
|
23
|
+
if (!fs.existsSync(dir)) return acc;
|
|
24
|
+
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
25
|
+
const p = path.join(dir, ent.name);
|
|
26
|
+
if (ent.isDirectory()) walk(p, acc);
|
|
27
|
+
else acc.push(p);
|
|
28
|
+
}
|
|
29
|
+
return acc;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function main() {
|
|
33
|
+
const { root } = parseArgs(process.argv.slice(2));
|
|
34
|
+
const entities = [];
|
|
35
|
+
for (const base of ["knowledge/domains", "knowledge/services", "knowledge/modules"]) {
|
|
36
|
+
for (const f of walk(path.join(root, base))) {
|
|
37
|
+
if (!f.endsWith("meta.yaml") && !f.endsWith("meta.yml")) continue;
|
|
38
|
+
const rel = path.relative(root, f).split(path.sep).join("/");
|
|
39
|
+
const text = fs.readFileSync(f, "utf8");
|
|
40
|
+
const id = (text.match(/^id:\s*(\S+)/m) || [])[1];
|
|
41
|
+
const kind = (text.match(/^kind:\s*(\S+)/m) || [])[1] || "unknown";
|
|
42
|
+
if (id) entities.push({ kind, id, path: rel });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const featRoot = path.join(root, "features");
|
|
46
|
+
if (fs.existsSync(featRoot)) {
|
|
47
|
+
for (const name of fs.readdirSync(featRoot)) {
|
|
48
|
+
if (!/^F-/.test(name)) continue;
|
|
49
|
+
const dir = path.join(featRoot, name);
|
|
50
|
+
if (!fs.statSync(dir).isDirectory()) continue;
|
|
51
|
+
entities.push({ kind: "feature", id: name, path: `features/${name}/` });
|
|
52
|
+
const links = path.join(dir, "knowledge-links.yaml");
|
|
53
|
+
if (fs.existsSync(links)) {
|
|
54
|
+
entities.push({
|
|
55
|
+
kind: "knowledge-links",
|
|
56
|
+
id: `${name}:links`,
|
|
57
|
+
path: `features/${name}/knowledge-links.yaml`,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const graphDir = path.join(root, "knowledge/graph");
|
|
64
|
+
fs.mkdirSync(graphDir, { recursive: true });
|
|
65
|
+
const generatedAt = new Date().toISOString();
|
|
66
|
+
const indexBody = [
|
|
67
|
+
"# generated by kb-graph-materialize",
|
|
68
|
+
"schema_version: 1",
|
|
69
|
+
`generated_at: ${generatedAt}`,
|
|
70
|
+
"entities:",
|
|
71
|
+
...entities.flatMap((e) => [
|
|
72
|
+
` - kind: ${e.kind}`,
|
|
73
|
+
` id: ${e.id}`,
|
|
74
|
+
` path: ${e.path}`,
|
|
75
|
+
]),
|
|
76
|
+
"",
|
|
77
|
+
].join("\n");
|
|
78
|
+
fs.writeFileSync(path.join(graphDir, "entities-index.yaml"), indexBody);
|
|
79
|
+
|
|
80
|
+
const manual = path.join(graphDir, "edges.manual.yaml");
|
|
81
|
+
const edgesOut = path.join(graphDir, "edges.yaml");
|
|
82
|
+
if (fs.existsSync(manual)) {
|
|
83
|
+
fs.copyFileSync(manual, edgesOut);
|
|
84
|
+
} else if (!fs.existsSync(edgesOut)) {
|
|
85
|
+
fs.writeFileSync(
|
|
86
|
+
edgesOut,
|
|
87
|
+
`schema_version: 1\nupdated_at: ${generatedAt}\nedges: []\n`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
console.log(
|
|
92
|
+
JSON.stringify(
|
|
93
|
+
{
|
|
94
|
+
ok: true,
|
|
95
|
+
entities: entities.length,
|
|
96
|
+
entities_index: "knowledge/graph/entities-index.yaml",
|
|
97
|
+
edges: "knowledge/graph/edges.yaml",
|
|
98
|
+
},
|
|
99
|
+
null,
|
|
100
|
+
2,
|
|
101
|
+
),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
main();
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Promote knowledge/bootstrap/staging → formal trees (merge-new-only).
|
|
4
|
+
* Never overwrites existing formal files. Does not call an LLM.
|
|
5
|
+
*/
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import process from "node:process";
|
|
9
|
+
|
|
10
|
+
function parseArgs(argv) {
|
|
11
|
+
const out = {
|
|
12
|
+
root: process.cwd(),
|
|
13
|
+
from: "knowledge/bootstrap/staging",
|
|
14
|
+
dryRun: false,
|
|
15
|
+
json: true,
|
|
16
|
+
};
|
|
17
|
+
for (let i = 0; i < argv.length; i++) {
|
|
18
|
+
const a = argv[i];
|
|
19
|
+
if (a === "--root") out.root = path.resolve(argv[++i]);
|
|
20
|
+
else if (a === "--from") out.from = argv[++i];
|
|
21
|
+
else if (a === "--dry-run") out.dryRun = true;
|
|
22
|
+
else if (a === "--json") out.json = true;
|
|
23
|
+
else if (a === "--markdown") out.json = false;
|
|
24
|
+
else if (a === "-h" || a === "--help") {
|
|
25
|
+
console.log(
|
|
26
|
+
"Usage: node scripts/kb-graph-promote.mjs [--root <dir>] [--from <staging-rel>] [--dry-run] [--json|--markdown]",
|
|
27
|
+
);
|
|
28
|
+
process.exit(0);
|
|
29
|
+
} else {
|
|
30
|
+
throw new Error(`unknown argument: ${a}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function copyDirMergeNew(src, dest, dryRun, stats) {
|
|
37
|
+
if (!fs.existsSync(src)) return;
|
|
38
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
39
|
+
for (const ent of fs.readdirSync(src, { withFileTypes: true })) {
|
|
40
|
+
const s = path.join(src, ent.name);
|
|
41
|
+
const d = path.join(dest, ent.name);
|
|
42
|
+
if (ent.isDirectory()) {
|
|
43
|
+
copyDirMergeNew(s, d, dryRun, stats);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (fs.existsSync(d)) {
|
|
47
|
+
stats.skipped.push(path.relative(stats.root, d).split(path.sep).join("/"));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
stats.copied.push(path.relative(stats.root, d).split(path.sep).join("/"));
|
|
51
|
+
if (!dryRun) {
|
|
52
|
+
fs.mkdirSync(path.dirname(d), { recursive: true });
|
|
53
|
+
fs.copyFileSync(s, d);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function main() {
|
|
59
|
+
let args;
|
|
60
|
+
try {
|
|
61
|
+
args = parseArgs(process.argv.slice(2));
|
|
62
|
+
} catch (e) {
|
|
63
|
+
console.error(String(e?.message || e));
|
|
64
|
+
process.exit(2);
|
|
65
|
+
}
|
|
66
|
+
const root = args.root;
|
|
67
|
+
const staging = path.join(root, args.from);
|
|
68
|
+
if (!fs.existsSync(staging)) {
|
|
69
|
+
const err = {
|
|
70
|
+
ok: false,
|
|
71
|
+
error: `missing staging at ${args.from}`,
|
|
72
|
+
};
|
|
73
|
+
console.log(JSON.stringify(err, null, 2));
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const stats = { root, copied: [], skipped: [] };
|
|
78
|
+
copyDirMergeNew(
|
|
79
|
+
path.join(staging, "domains"),
|
|
80
|
+
path.join(root, "knowledge/domains"),
|
|
81
|
+
args.dryRun,
|
|
82
|
+
stats,
|
|
83
|
+
);
|
|
84
|
+
copyDirMergeNew(
|
|
85
|
+
path.join(staging, "services"),
|
|
86
|
+
path.join(root, "knowledge/services"),
|
|
87
|
+
args.dryRun,
|
|
88
|
+
stats,
|
|
89
|
+
);
|
|
90
|
+
copyDirMergeNew(
|
|
91
|
+
path.join(staging, "modules"),
|
|
92
|
+
path.join(root, "knowledge/modules"),
|
|
93
|
+
args.dryRun,
|
|
94
|
+
stats,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const edgesSrc = path.join(staging, "edges.proposed.yaml");
|
|
98
|
+
const edgesManual = path.join(root, "knowledge/graph/edges.manual.yaml");
|
|
99
|
+
if (fs.existsSync(edgesSrc)) {
|
|
100
|
+
if (fs.existsSync(edgesManual)) {
|
|
101
|
+
stats.skipped.push("knowledge/graph/edges.manual.yaml");
|
|
102
|
+
} else {
|
|
103
|
+
stats.copied.push("knowledge/graph/edges.manual.yaml");
|
|
104
|
+
if (!args.dryRun) {
|
|
105
|
+
fs.mkdirSync(path.dirname(edgesManual), { recursive: true });
|
|
106
|
+
fs.copyFileSync(edgesSrc, edgesManual);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const linksDir = path.join(staging, "features-links.proposed");
|
|
112
|
+
if (fs.existsSync(linksDir)) {
|
|
113
|
+
for (const f of fs.readdirSync(linksDir)) {
|
|
114
|
+
if (!f.endsWith(".yaml") && !f.endsWith(".yml")) continue;
|
|
115
|
+
const id = f.replace(/\.ya?ml$/, "");
|
|
116
|
+
const featureDir = path.join(root, "features", id);
|
|
117
|
+
if (!fs.existsSync(featureDir)) continue;
|
|
118
|
+
const dest = path.join(featureDir, "knowledge-links.yaml");
|
|
119
|
+
const rel = `features/${id}/knowledge-links.yaml`;
|
|
120
|
+
if (fs.existsSync(dest)) {
|
|
121
|
+
stats.skipped.push(rel);
|
|
122
|
+
} else {
|
|
123
|
+
stats.copied.push(rel);
|
|
124
|
+
if (!args.dryRun) {
|
|
125
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
126
|
+
fs.copyFileSync(path.join(linksDir, f), dest);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const result = {
|
|
133
|
+
ok: true,
|
|
134
|
+
dry_run: args.dryRun,
|
|
135
|
+
copied_count: stats.copied.length,
|
|
136
|
+
skipped_count: stats.skipped.length,
|
|
137
|
+
copied: stats.copied,
|
|
138
|
+
skipped_existing: stats.skipped,
|
|
139
|
+
policy: "merge-new-only",
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
if (args.json) {
|
|
143
|
+
console.log(JSON.stringify(result, null, 2));
|
|
144
|
+
} else {
|
|
145
|
+
console.log(`# kb-graph-promote${args.dryRun ? " (dry-run)" : ""}`);
|
|
146
|
+
console.log(`copied: ${result.copied_count}`);
|
|
147
|
+
console.log(`skipped_existing: ${result.skipped_count}`);
|
|
148
|
+
for (const c of stats.copied) console.log(`+ ${c}`);
|
|
149
|
+
for (const s of stats.skipped) console.log(`= ${s}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
main();
|