@mmerterden/multi-agent-pipeline 23.3.0 → 23.5.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.
Files changed (239) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/README.md +27 -8
  3. package/README.tr.md +28 -9
  4. package/SECURITY.md +16 -0
  5. package/docs/adr/0008-installer-modularization-and-secret-leak-defense.md +1 -1
  6. package/docs/adr/0010-own-code-graph.md +299 -8
  7. package/docs/architecture.md +3 -3
  8. package/docs/ecosystem.md +10 -10
  9. package/docs/engineering.md +4 -4
  10. package/docs/facts.json +8 -8
  11. package/docs/features.md +5 -5
  12. package/docs/performance.md +17 -17
  13. package/install/_dev-only-files.mjs +9 -0
  14. package/install/_platform-filter.mjs +2 -55
  15. package/install/claude.mjs +28 -2
  16. package/install/codex.mjs +1 -3
  17. package/install/copilot.mjs +4 -8
  18. package/install/templates/claude-hooks.json +32 -1
  19. package/manifest.json +236 -167
  20. package/package.json +11 -3
  21. package/pipeline/commands/multi-agent/SKILL.md +1 -1
  22. package/pipeline/commands/multi-agent/design-check/SKILL.md +3 -2
  23. package/pipeline/commands/multi-agent/diff-explain/SKILL.md +3 -3
  24. package/pipeline/commands/multi-agent/graph/SKILL.md +57 -24
  25. package/pipeline/commands/multi-agent/pair/SKILL.md +92 -0
  26. package/pipeline/commands/multi-agent/prune-prompts/SKILL.md +1 -1
  27. package/pipeline/commands/multi-agent/search/SKILL.md +7 -1
  28. package/pipeline/commands/multi-agent/status/SKILL.md +4 -0
  29. package/pipeline/commands/multi-agent/sync/SKILL.md +3 -3
  30. package/pipeline/contract/CHANGELOG.md +63 -0
  31. package/pipeline/contract/README.md +1 -0
  32. package/pipeline/contract/build.mjs +13 -1
  33. package/pipeline/contract/fixtures/error-invalid-request.json +1 -1
  34. package/pipeline/contract/fixtures/error-unauthorized.json +1 -1
  35. package/pipeline/contract/fixtures/runs-awaiting-question.json +4 -1
  36. package/pipeline/contract/fixtures/runs-empty.json +1 -1
  37. package/pipeline/contract/fixtures/runs-failed.json +4 -1
  38. package/pipeline/contract/fixtures/runs-old-schema.json +3 -0
  39. package/pipeline/contract/fixtures/runs-pr-opened-redacted.json +4 -1
  40. package/pipeline/contract/fixtures/runs-pr-opened.json +4 -1
  41. package/pipeline/contract/fixtures/runs-running.json +4 -1
  42. package/pipeline/contract/manifest.json +41 -15
  43. package/pipeline/contract/types/index.d.ts +81 -3
  44. package/pipeline/lib/detach-self.mjs +95 -0
  45. package/pipeline/lib/public-mcp-servers.mjs +33 -0
  46. package/pipeline/lib/rotated-log.mjs +93 -0
  47. package/pipeline/lib/stack-detect.sh +1 -1
  48. package/pipeline/lib/tls-trust.mjs +29 -0
  49. package/pipeline/lib/usage-lifecycle.mjs +9 -11
  50. package/pipeline/lib/usage-names.generated.mjs +613 -0
  51. package/pipeline/lib/usage-run-key.mjs +235 -0
  52. package/pipeline/lib/usage-vocab.mjs +210 -0
  53. package/pipeline/multi-agent-refs/analysis/intake.md +2 -2
  54. package/pipeline/multi-agent-refs/analysis/render.md +1 -1
  55. package/pipeline/multi-agent-refs/channels/generator.md +4 -5
  56. package/pipeline/multi-agent-refs/cross-cli-contract.md +3 -3
  57. package/pipeline/multi-agent-refs/design-check/init-inventory.md +11 -1
  58. package/pipeline/multi-agent-refs/design-check/scope-and-ledger.md +1 -1
  59. package/pipeline/multi-agent-refs/features/code-graph.md +146 -11
  60. package/pipeline/multi-agent-refs/features/code-intelligence.md +16 -14
  61. package/pipeline/multi-agent-refs/features/dev-critic.md +7 -7
  62. package/pipeline/multi-agent-refs/features/model-fallback.md +6 -4
  63. package/pipeline/multi-agent-refs/features/remote-control.md +253 -0
  64. package/pipeline/multi-agent-refs/features/review-delta.md +1 -1
  65. package/pipeline/multi-agent-refs/features/review-file-set.md +3 -3
  66. package/pipeline/multi-agent-refs/features/review-multi-repo.md +1 -1
  67. package/pipeline/multi-agent-refs/features/session-brief.md +42 -0
  68. package/pipeline/multi-agent-refs/features/skill-conformance.md +3 -3
  69. package/pipeline/multi-agent-refs/features/usage-reporting.md +255 -27
  70. package/pipeline/multi-agent-refs/help/en.md +3 -2
  71. package/pipeline/multi-agent-refs/help/tr.md +3 -2
  72. package/pipeline/multi-agent-refs/knowledge.md +1 -1
  73. package/pipeline/multi-agent-refs/orchestrator/dispatcher.md +2 -2
  74. package/pipeline/multi-agent-refs/phases/operations.md +2 -2
  75. package/pipeline/multi-agent-refs/phases/phase-0-init.md +13 -12
  76. package/pipeline/multi-agent-refs/phases/phase-1-plan.md +4 -4
  77. package/pipeline/multi-agent-refs/phases/phase-3-review.md +2 -0
  78. package/pipeline/multi-agent-refs/phases/phase-5-report.md +1 -1
  79. package/pipeline/multi-agent-refs/platform-parity.md +2 -2
  80. package/pipeline/multi-agent-refs/wiki-capture.md +3 -17
  81. package/pipeline/office/index.html +1309 -236
  82. package/pipeline/office/toolkit.json +1 -1
  83. package/pipeline/rules/outside-the-pipeline.md +1 -1
  84. package/pipeline/schemas/activity.schema.json +23 -0
  85. package/pipeline/schemas/agent-state.schema.json +6 -1
  86. package/pipeline/schemas/code-graph-calls.schema.json +254 -0
  87. package/pipeline/schemas/code-graph-docs.schema.json +223 -0
  88. package/pipeline/schemas/code-graph-infra.schema.json +227 -0
  89. package/pipeline/schemas/code-graph.schema.json +2 -2
  90. package/pipeline/schemas/examples/design-check-config.json +27 -0
  91. package/pipeline/schemas/learning-curve.schema.json +67 -0
  92. package/pipeline/schemas/mcp-health.schema.json +96 -0
  93. package/pipeline/schemas/office-jobs.json +69 -2
  94. package/pipeline/schemas/office-jobs.schema.json +8 -1
  95. package/pipeline/schemas/phases.json +5 -0
  96. package/pipeline/schemas/phases.schema.json +8 -0
  97. package/pipeline/schemas/prefs.schema.json +46 -12
  98. package/pipeline/schemas/runs-index.schema.json +17 -0
  99. package/pipeline/schemas/toolkit-path-params.json +78 -8
  100. package/pipeline/scripts/README.md +1 -0
  101. package/pipeline/scripts/_code-graph.mjs +184 -103
  102. package/pipeline/scripts/_contract-core.mjs +1 -1
  103. package/pipeline/scripts/_contract-routes-office.mjs +33 -0
  104. package/pipeline/scripts/_diff-impact.mjs +21 -2
  105. package/pipeline/scripts/_graph-sidecars.mjs +372 -0
  106. package/pipeline/scripts/_http-post.mjs +103 -115
  107. package/pipeline/scripts/_safe-read.mjs +57 -0
  108. package/pipeline/scripts/_skill-practices.mjs +26 -0
  109. package/pipeline/scripts/_strip-code.mjs +380 -0
  110. package/pipeline/scripts/activity-snapshot.mjs +13 -1
  111. package/pipeline/scripts/aggregate-metrics.mjs +3 -5
  112. package/pipeline/scripts/build-stack-plugins.mjs +1 -47
  113. package/pipeline/scripts/call-graph/index.mjs +78 -0
  114. package/pipeline/scripts/call-graph/lang/cpp.mjs +308 -0
  115. package/pipeline/scripts/call-graph/lang/csharp.mjs +130 -0
  116. package/pipeline/scripts/call-graph/lang/go.mjs +303 -0
  117. package/pipeline/scripts/call-graph/lang/java.mjs +370 -0
  118. package/pipeline/scripts/call-graph/lang/kotlin.mjs +397 -0
  119. package/pipeline/scripts/call-graph/lang/python.mjs +399 -0
  120. package/pipeline/scripts/call-graph/lang/rust.mjs +345 -0
  121. package/pipeline/scripts/call-graph/lang/shared.mjs +230 -0
  122. package/pipeline/scripts/call-graph/lang/swift.mjs +432 -0
  123. package/pipeline/scripts/call-graph/lang/typescript.mjs +692 -0
  124. package/pipeline/scripts/call-graph/lex.mjs +396 -0
  125. package/pipeline/scripts/call-graph/platform.mjs +86 -0
  126. package/pipeline/scripts/call-graph/profiles.mjs +89 -0
  127. package/pipeline/scripts/call-graph/resolve.mjs +1125 -0
  128. package/pipeline/scripts/call-graph/scan.mjs +267 -0
  129. package/pipeline/scripts/code-graph-rules/android.json +7 -2
  130. package/pipeline/scripts/code-graph-rules/cpp.json +99 -0
  131. package/pipeline/scripts/code-graph-rules/csharp.json +102 -0
  132. package/pipeline/scripts/code-graph-rules/go.json +2 -1
  133. package/pipeline/scripts/code-graph-rules/ios.json +3 -1
  134. package/pipeline/scripts/code-graph-rules/java.json +76 -0
  135. package/pipeline/scripts/code-graph-rules/node.json +7 -2
  136. package/pipeline/scripts/code-graph-rules/python.json +6 -2
  137. package/pipeline/scripts/code-graph-rules/rust.json +105 -0
  138. package/pipeline/scripts/contract-server.mjs +3 -0
  139. package/pipeline/scripts/design-check-config.mjs +205 -0
  140. package/pipeline/scripts/feedback-send.mjs +26 -26
  141. package/pipeline/scripts/gate-ledger.mjs +2 -1
  142. package/pipeline/scripts/gen-facts.mjs +1 -2
  143. package/pipeline/scripts/gen-usage-names.mjs +124 -0
  144. package/pipeline/scripts/graph-affected.mjs +109 -20
  145. package/pipeline/scripts/graph-build.mjs +95 -15
  146. package/pipeline/scripts/graph-export.mjs +272 -0
  147. package/pipeline/scripts/graph-hint.mjs +15 -2
  148. package/pipeline/scripts/graph-query.mjs +305 -26
  149. package/pipeline/scripts/graph-report.mjs +187 -1
  150. package/pipeline/scripts/health-report.mjs +3 -4
  151. package/pipeline/scripts/infra/docs.mjs +317 -0
  152. package/pipeline/scripts/infra/extract.mjs +998 -0
  153. package/pipeline/scripts/infra/graph.mjs +245 -0
  154. package/pipeline/scripts/infra/yaml-lite.mjs +311 -0
  155. package/pipeline/scripts/job-launch.mjs +67 -2
  156. package/pipeline/scripts/learning-curve.mjs +288 -178
  157. package/pipeline/scripts/log-metric.sh +10 -1
  158. package/pipeline/scripts/mcp-health.mjs +309 -0
  159. package/pipeline/scripts/note-agent-event.mjs +3 -4
  160. package/pipeline/scripts/note-command-use.mjs +333 -0
  161. package/pipeline/scripts/note-command-use.sh +126 -0
  162. package/pipeline/scripts/note-mcp-call.sh +89 -0
  163. package/pipeline/scripts/note-mcp-failure.mjs +272 -1
  164. package/pipeline/scripts/note-session.sh +62 -3
  165. package/pipeline/scripts/note-skill-use.mjs +58 -2
  166. package/pipeline/scripts/office-build.mjs +115 -1
  167. package/pipeline/scripts/office-memory.mjs +14 -5
  168. package/pipeline/scripts/pair.mjs +333 -0
  169. package/pipeline/scripts/pre-push-check.sh +1 -1
  170. package/pipeline/scripts/remote/cbor.mjs +128 -0
  171. package/pipeline/scripts/remote/contract.mjs +98 -0
  172. package/pipeline/scripts/remote/handler.mjs +330 -0
  173. package/pipeline/scripts/remote/handles.mjs +57 -0
  174. package/pipeline/scripts/remote/lifecycle.mjs +186 -0
  175. package/pipeline/scripts/remote/listener.mjs +204 -0
  176. package/pipeline/scripts/remote/lockout.mjs +103 -0
  177. package/pipeline/scripts/remote/nonces.mjs +61 -0
  178. package/pipeline/scripts/remote/pairing.mjs +519 -0
  179. package/pipeline/scripts/remote/qr-encode.mjs +459 -0
  180. package/pipeline/scripts/remote/store.mjs +191 -0
  181. package/pipeline/scripts/remote/transport.mjs +211 -0
  182. package/pipeline/scripts/remote/verbs.json +70 -0
  183. package/pipeline/scripts/remote/verbs.mjs +217 -0
  184. package/pipeline/scripts/remote/webauthn.mjs +169 -0
  185. package/pipeline/scripts/remote/wire.mjs +364 -0
  186. package/pipeline/scripts/remote-listener.mjs +83 -0
  187. package/pipeline/scripts/route-learn.mjs +4 -4
  188. package/pipeline/scripts/run-kill.mjs +1 -1
  189. package/pipeline/scripts/run-write.mjs +13 -2
  190. package/pipeline/scripts/runs-index.mjs +30 -3
  191. package/pipeline/scripts/search-logs.sh +9 -0
  192. package/pipeline/scripts/session-brief.mjs +458 -0
  193. package/pipeline/scripts/session-brief.sh +130 -0
  194. package/pipeline/scripts/skill-usage.mjs +16 -5
  195. package/pipeline/scripts/test-gap-rules/cpp.json +25 -0
  196. package/pipeline/scripts/test-gap-rules/csharp.json +20 -0
  197. package/pipeline/scripts/test-gap-rules/java.json +20 -0
  198. package/pipeline/scripts/test-gap-rules/rust.json +14 -0
  199. package/pipeline/scripts/token-budget-report.mjs +3 -2
  200. package/pipeline/scripts/uninstall.mjs +29 -0
  201. package/pipeline/scripts/usage-access.mjs +16 -31
  202. package/pipeline/scripts/usage-health.mjs +31 -62
  203. package/pipeline/scripts/usage-register-beacon.mjs +11 -22
  204. package/pipeline/scripts/usage-register.mjs +39 -29
  205. package/pipeline/scripts/usage-report.mjs +579 -178
  206. package/pipeline/scripts/validate-code-graph.mjs +361 -111
  207. package/pipeline/scripts/write-state.mjs +39 -0
  208. package/pipeline/skills/.skill-manifest.json +21 -17
  209. package/pipeline/skills/.skills-index.json +13 -2
  210. package/pipeline/skills/shared/README.md +5 -4
  211. package/pipeline/skills/shared/core/multi-agent-design-check/SKILL.md +1 -1
  212. package/pipeline/skills/shared/core/multi-agent-diff-explain/SKILL.md +3 -3
  213. package/pipeline/skills/shared/core/multi-agent-graph/SKILL.md +55 -21
  214. package/pipeline/skills/shared/core/multi-agent-pair/SKILL.md +41 -0
  215. package/pipeline/skills/shared/core/multi-agent-prune-prompts/SKILL.md +1 -1
  216. package/pipeline/skills/shared/core/multi-agent-sync/SKILL.md +3 -3
  217. package/pipeline/skills/shared/external/app-store-optimization/SKILL.md +2 -3
  218. package/pipeline/skills/shared/external/app-store-optimization/references/keyword-research-methodology.md +2 -2
  219. package/pipeline/skills/shared/external/app-store-optimization/references/product-page-variants.md +2 -2
  220. package/pipeline/skills/shared/external/app-store-review/SKILL.md +11 -4
  221. package/pipeline/skills/shared/external/build-settings-audit/SKILL.md +1 -1
  222. package/pipeline/skills/shared/external/build-time-benchmark/SKILL.md +5 -5
  223. package/pipeline/skills/shared/external/build-time-fixes/SKILL.md +5 -5
  224. package/pipeline/skills/shared/external/build-time-optimizer/SKILL.md +6 -6
  225. package/pipeline/skills/shared/external/free-llm-apis/references/providers.md +1 -0
  226. package/pipeline/skills/shared/external/ios-coding-standard/references/STANDARD.md +2 -2
  227. package/pipeline/skills/shared/external/ios-haptics/references/core-haptics.md +1 -1
  228. package/pipeline/skills/shared/external/localization-reuse-map/references/publish-and-snapshot.md +1 -1
  229. package/pipeline/skills/shared/external/package-build-audit/SKILL.md +3 -3
  230. package/pipeline/skills/shared/external/reverse-engineer-own-app/SKILL.md +2 -3
  231. package/pipeline/skills/shared/external/swift-compile-hotspots/SKILL.md +2 -2
  232. package/pipeline/skills/shared/external/weatherkit/SKILL.md +5 -6
  233. package/pipeline/skills/skills-index.md +3 -2
  234. package/docs/FIGMA_PIPELINE.md +0 -137
  235. package/pipeline/lib/identifier-scan.mjs +0 -156
  236. package/pipeline/scripts/_renamed-skills.mjs +0 -164
  237. package/pipeline/scripts/publish-staged.mjs +0 -324
  238. package/pipeline/skills/renamed.json +0 -30
  239. package/pipeline/skills/shared/external/ios-coding-standard/scripts/check_structure.py +0 -765
@@ -1,19 +1,27 @@
1
1
  #!/usr/bin/env node
2
2
  // validate-code-graph.mjs - schema validator for graph-build.mjs output.
3
3
  //
4
- // Zero deps (ADR-0004). Reads JSON from path or STDIN, validates against
5
- // pipeline/schemas/code-graph.schema.json.
4
+ // Zero deps (ADR-0004). Reads JSON from a path or STDIN and validates it against
5
+ // pipeline/schemas/code-graph.schema.json (the main graph) or, for a sidecar,
6
+ // the code-graph-<name>.schema.json beside it. Given the path of a main graph it
7
+ // also validates every sidecar next to it (code-graph.calls.json, .docs.json,
8
+ // .infra.json), with their edge endpoints resolved against the main graph.
6
9
  //
7
- // Edge endpoints are checked against the node set, not just for type: a graph
8
- // whose edges point at ids no node declares still parses as valid JSON, but
9
- // every traversal built on it silently drops those hops.
10
+ // The main graph is held to schema 1.2.0: the node kinds, edge kinds and fields
11
+ // that version defines and nothing else, so a reader that only knows 1.2.0 can
12
+ // always read it. Edge endpoints are checked against the node set, not just for
13
+ // type: a graph whose edges point at ids no node declares still parses as valid
14
+ // JSON, but every traversal built on it silently drops those hops. Counts in
15
+ // `stats` and each node's `degree` are recomputed and compared.
10
16
  //
11
17
  // Exit codes:
12
18
  // 0 - valid
13
19
  // 1 - invalid
14
20
  // 64 - usage error
15
21
 
16
- import { readFileSync } from "node:fs";
22
+ import { existsSync, readFileSync } from "node:fs";
23
+ import { basename } from "node:path";
24
+ import { CALL_VIAS, SIDECAR_NAMES, SIDECAR_SPECS, sidecarPath } from "./_graph-sidecars.mjs";
17
25
 
18
26
  // Optional: this script has no other dependency and also runs copied on its
19
27
  // own, without lib/ beside it; there it simply has no --help.
@@ -22,10 +30,42 @@ await import("../lib/cli-help.mjs").then(
22
30
  () => {},
23
31
  );
24
32
 
25
- const ALLOWED_STACK = new Set(["ios", "android", "node", "python", "go"]);
26
- const ALLOWED_NODE_KIND = new Set(["file", "symbol", "module"]);
27
- const ALLOWED_EDGE_KIND = new Set(["defines", "imports", "references", "inherits"]);
33
+ const ALLOWED_STACK = new Set([
34
+ "ios",
35
+ "android",
36
+ "node",
37
+ "python",
38
+ "go",
39
+ "rust",
40
+ "java",
41
+ "csharp",
42
+ "cpp",
43
+ ]);
44
+ const MAIN_VERSIONS = ["1.0.0", "1.1.0", "1.2.0"];
45
+ const MAIN_NODE_KINDS = new Set(["file", "symbol", "module"]);
46
+ const MAIN_EDGE_KINDS = new Set(["defines", "imports", "references", "inherits"]);
47
+ const MAIN_NODE_FIELDS = new Set([
48
+ "id",
49
+ "kind",
50
+ "name",
51
+ "path",
52
+ "line",
53
+ "startLine",
54
+ "endLine",
55
+ "symbolKind",
56
+ "nested",
57
+ "isTest",
58
+ "degree",
59
+ ]);
60
+ const SIDECAR_NODE_FIELDS = new Set([...MAIN_NODE_FIELDS, "owner"]);
61
+ const MAIN_EDGE_FIELDS = new Set(["from", "to", "kind"]);
62
+ const SIDECAR_EDGE_FIELDS = new Set(["from", "to", "kind", "via", "confidence"]);
63
+ const CONFIDENCE = new Set(["certain", "high", "medium", "low"]);
64
+ const CALL_CONFIDENCE = new Set(["high", "medium", "low"]);
28
65
  const MAX_REPORTED_DANGLING = 5;
66
+ const MAX_ERRORS = 60;
67
+ const MAX_NAME = 4096;
68
+ const VIA_TOKEN = /^[a-z][a-z0-9_-]{0,39}$/;
29
69
 
30
70
  function readInput() {
31
71
  const arg = process.argv[2];
@@ -45,107 +85,75 @@ function readInput() {
45
85
  return Promise.resolve(readFileSync(arg, "utf-8"));
46
86
  }
47
87
 
48
- function validateStats(obj, errors) {
49
- if (typeof obj.stats !== "object" || obj.stats === null || Array.isArray(obj.stats)) {
50
- errors.push("stats must be an object");
51
- return;
52
- }
53
- for (const key of ["files", "nodes", "edges"]) {
54
- if (!Number.isInteger(obj.stats[key]) || obj.stats[key] < 0) {
55
- errors.push(`stats.${key} must be a non-negative integer`);
56
- }
57
- }
58
- if (Array.isArray(obj.nodes) && obj.stats.nodes !== obj.nodes.length) {
59
- errors.push(
60
- `stats.nodes (${obj.stats.nodes}) does not match nodes.length (${obj.nodes.length})`,
61
- );
62
- }
63
- if (Array.isArray(obj.edges) && obj.stats.edges !== obj.edges.length) {
64
- errors.push(
65
- `stats.edges (${obj.stats.edges}) does not match edges.length (${obj.edges.length})`,
66
- );
67
- }
68
- }
88
+ const isInt = (v) => Number.isInteger(v) && v >= 0;
89
+ const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
69
90
 
70
- function validateNodes(obj, errors) {
71
- if (!Array.isArray(obj.nodes)) {
91
+ function checkNodes(nodes, { kinds, fields, errors }) {
92
+ if (!Array.isArray(nodes)) {
72
93
  errors.push("nodes must be an array");
73
- return new Set();
94
+ return new Map();
74
95
  }
75
- const ids = new Set();
76
- obj.nodes.forEach((n, i) => {
96
+ const byId = new Map();
97
+ nodes.forEach((n, i) => {
77
98
  const p = `nodes[${i}]`;
78
- if (typeof n !== "object" || n === null) {
99
+ if (!isObject(n)) {
79
100
  errors.push(`${p} must be an object`);
80
101
  return;
81
102
  }
82
- if (typeof n.id !== "string" || n.id.length === 0) errors.push(`${p}.id must be a string`);
83
- else if (ids.has(n.id)) errors.push(`${p}.id is duplicated: ${n.id}`);
84
- else ids.add(n.id);
85
- if (!ALLOWED_NODE_KIND.has(n.kind))
86
- errors.push(`${p}.kind must be one of ${[...ALLOWED_NODE_KIND].join(",")}`);
87
- if (typeof n.name !== "string") errors.push(`${p}.name must be a string`);
88
- if (!Number.isInteger(n.degree) || n.degree < 0)
89
- errors.push(`${p}.degree must be a non-negative integer`);
103
+ if (typeof n.id !== "string" || n.id.length === 0 || n.id.length > MAX_NAME)
104
+ errors.push(`${p}.id must be a string of 1 to ${MAX_NAME} characters`);
105
+ else if (byId.has(n.id)) errors.push(`${p}.id is duplicated: ${n.id.slice(0, 80)}`);
106
+ else byId.set(n.id, n);
107
+ if (!kinds.has(n.kind)) errors.push(`${p}.kind must be one of ${[...kinds].join(",")}`);
108
+ if (typeof n.name !== "string" || n.name.length > MAX_NAME)
109
+ errors.push(`${p}.name must be a string of at most ${MAX_NAME} characters`);
110
+ if (!isInt(n.degree)) errors.push(`${p}.degree must be a non-negative integer`);
111
+ for (const k of Object.keys(n))
112
+ if (!fields.has(k)) errors.push(`${p} has a field this schema does not define: ${k}`);
90
113
  });
91
- return ids;
114
+ return byId;
92
115
  }
93
116
 
94
- function validateEdges(obj, ids, errors) {
95
- if (!Array.isArray(obj.edges)) {
96
- errors.push("edges must be an array");
97
- return;
98
- }
99
- const dangling = [];
100
- obj.edges.forEach((e, i) => {
101
- const p = `edges[${i}]`;
102
- if (typeof e !== "object" || e === null) {
103
- errors.push(`${p} must be an object`);
104
- return;
105
- }
106
- if (typeof e.from !== "string") errors.push(`${p}.from must be a string`);
107
- if (typeof e.to !== "string") errors.push(`${p}.to must be a string`);
108
- if (!ALLOWED_EDGE_KIND.has(e.kind))
109
- errors.push(`${p}.kind must be one of ${[...ALLOWED_EDGE_KIND].join(",")}`);
110
- if (ids.size) {
111
- if (typeof e.from === "string" && !ids.has(e.from)) dangling.push(`${p}.from -> ${e.from}`);
112
- if (typeof e.to === "string" && !ids.has(e.to)) dangling.push(`${p}.to -> ${e.to}`);
113
- }
114
- });
115
- if (dangling.length) {
116
- errors.push(
117
- `${dangling.length} edge endpoint(s) reference no node: ` +
118
- dangling.slice(0, MAX_REPORTED_DANGLING).join("; ") +
119
- (dangling.length > MAX_REPORTED_DANGLING ? "; ..." : ""),
120
- );
121
- }
122
- }
123
-
124
- function validateManifest(obj, errors) {
125
- if (obj.manifest === undefined) return;
126
- if (typeof obj.manifest !== "object" || obj.manifest === null || Array.isArray(obj.manifest)) {
117
+ function checkManifest(manifest, errors) {
118
+ if (manifest === undefined) return;
119
+ if (!isObject(manifest)) {
127
120
  errors.push("manifest must be an object");
128
121
  return;
129
122
  }
130
- for (const [path, entry] of Object.entries(obj.manifest)) {
131
- if (typeof entry !== "object" || entry === null) {
123
+ for (const [path, entry] of Object.entries(manifest)) {
124
+ if (!isObject(entry)) {
132
125
  errors.push(`manifest["${path}"] must be an object`);
133
126
  continue;
134
127
  }
135
- if (!Number.isInteger(entry.size) || entry.size < 0)
136
- errors.push(`manifest["${path}"].size must be a non-negative integer`);
137
- if (!Number.isInteger(entry.mtimeMs) || entry.mtimeMs < 0)
128
+ if (!isInt(entry.size)) errors.push(`manifest["${path}"].size must be a non-negative integer`);
129
+ if (!isInt(entry.mtimeMs))
138
130
  errors.push(`manifest["${path}"].mtimeMs must be a non-negative integer`);
139
131
  }
140
132
  }
141
133
 
142
- export function validate(obj) {
143
- const errors = [];
144
- if (typeof obj !== "object" || obj === null || Array.isArray(obj))
145
- return ["root must be an object"];
134
+ function dangling(edges, resolves, errors) {
135
+ const found = [];
136
+ edges.forEach((e, i) => {
137
+ if (typeof e.from === "string" && !resolves(e.from))
138
+ found.push(`edges[${i}].from -> ${e.from}`);
139
+ if (typeof e.to === "string" && !resolves(e.to)) found.push(`edges[${i}].to -> ${e.to}`);
140
+ });
141
+ if (found.length) {
142
+ errors.push(
143
+ `${found.length} edge endpoint(s) reference no node: ` +
144
+ found
145
+ .slice(0, MAX_REPORTED_DANGLING)
146
+ .map((s) => s.slice(0, 120))
147
+ .join("; ") +
148
+ (found.length > MAX_REPORTED_DANGLING ? "; ..." : ""),
149
+ );
150
+ }
151
+ }
146
152
 
147
- if (!["1.0.0", "1.1.0", "1.2.0"].includes(obj.schemaVersion))
148
- errors.push(`schemaVersion must be "1.0.0", "1.1.0" or "1.2.0"`);
153
+ function validateMain(obj) {
154
+ const errors = [];
155
+ if (!MAIN_VERSIONS.includes(obj.schemaVersion))
156
+ errors.push(`schemaVersion must be one of ${MAIN_VERSIONS.join(", ")}`);
149
157
  if (!ALLOWED_STACK.has(obj.stack))
150
158
  errors.push(`stack must be one of ${[...ALLOWED_STACK].join(",")}`);
151
159
  if (typeof obj.root !== "string" || obj.root.length === 0)
@@ -155,31 +163,273 @@ export function validate(obj) {
155
163
  if (obj.baseCommit !== undefined && obj.baseCommit !== null && typeof obj.baseCommit !== "string")
156
164
  errors.push("baseCommit must be a string or null");
157
165
 
158
- validateStats(obj, errors);
159
- const ids = validateNodes(obj, errors);
160
- validateEdges(obj, ids, errors);
161
- validateManifest(obj, errors);
166
+ const known = new Set([
167
+ "schemaVersion",
168
+ "stack",
169
+ "root",
170
+ "baseCommit",
171
+ "generatedAt",
172
+ "stats",
173
+ "nodes",
174
+ "edges",
175
+ "manifest",
176
+ ]);
177
+ for (const k of Object.keys(obj))
178
+ if (!known.has(k)) errors.push(`top-level field ${k} is not part of schema 1.2.0`);
179
+
180
+ const byId = checkNodes(obj.nodes, { kinds: MAIN_NODE_KINDS, fields: MAIN_NODE_FIELDS, errors });
181
+ const version = MAIN_VERSIONS.indexOf(obj.schemaVersion);
182
+ if (Array.isArray(obj.nodes) && version === 0)
183
+ obj.nodes.forEach((n, i) => {
184
+ if (isObject(n) && n.endLine !== undefined)
185
+ errors.push(`nodes[${i}].endLine needs schema 1.1.0 or newer`);
186
+ });
187
+
188
+ const degree = new Map();
189
+ if (!Array.isArray(obj.edges)) errors.push("edges must be an array");
190
+ else {
191
+ obj.edges.forEach((e, i) => {
192
+ const p = `edges[${i}]`;
193
+ if (!isObject(e)) {
194
+ errors.push(`${p} must be an object`);
195
+ return;
196
+ }
197
+ if (typeof e.from !== "string") errors.push(`${p}.from must be a string`);
198
+ if (typeof e.to !== "string") errors.push(`${p}.to must be a string`);
199
+ if (!MAIN_EDGE_KINDS.has(e.kind))
200
+ errors.push(`${p}.kind must be one of ${[...MAIN_EDGE_KINDS].join(",")}`);
201
+ else if (e.kind === "inherits" && version < 2)
202
+ errors.push(`${p}.kind inherits needs schema 1.2.0`);
203
+ for (const k of Object.keys(e))
204
+ if (!MAIN_EDGE_FIELDS.has(k))
205
+ errors.push(`${p} has a field this schema does not define: ${k}`);
206
+ degree.set(e.from, (degree.get(e.from) || 0) + 1);
207
+ degree.set(e.to, (degree.get(e.to) || 0) + 1);
208
+ });
209
+ if (byId.size) dangling(obj.edges, (id) => byId.has(id), errors);
210
+ let wrong = 0;
211
+ for (const [id, n] of byId) if (n.degree !== (degree.get(id) || 0)) wrong++;
212
+ if (wrong) errors.push(`${wrong} node(s) carry a degree that does not match the edges`);
213
+ }
214
+
215
+ if (!isObject(obj.stats)) errors.push("stats must be an object");
216
+ else {
217
+ for (const key of Object.keys(obj.stats))
218
+ if (!["files", "nodes", "edges"].includes(key))
219
+ errors.push(`stats.${key} is not part of schema 1.2.0`);
220
+ for (const key of ["files", "nodes", "edges"])
221
+ if (!isInt(obj.stats[key])) errors.push(`stats.${key} must be a non-negative integer`);
222
+ if (Array.isArray(obj.nodes) && obj.stats.nodes !== obj.nodes.length)
223
+ errors.push(
224
+ `stats.nodes (${obj.stats.nodes}) does not match nodes.length (${obj.nodes.length})`,
225
+ );
226
+ if (Array.isArray(obj.edges) && obj.stats.edges !== obj.edges.length)
227
+ errors.push(
228
+ `stats.edges (${obj.stats.edges}) does not match edges.length (${obj.edges.length})`,
229
+ );
230
+ if (Array.isArray(obj.nodes)) {
231
+ const files = obj.nodes.filter((n) => isObject(n) && n.kind === "file").length;
232
+ if (obj.stats.files !== files)
233
+ errors.push(`stats.files (${obj.stats.files}) does not match the ${files} file node(s)`);
234
+ }
235
+ }
236
+ checkManifest(obj.manifest, errors);
237
+ return errors;
238
+ }
239
+
240
+ /**
241
+ * @param {object} obj - a parsed sidecar
242
+ * @param {Map<string, object>|null} mainById - the main graph's nodes by id, when known
243
+ */
244
+ function validateSidecar(obj, mainById, main = null) {
245
+ const errors = [];
246
+ const name = obj.sidecar;
247
+ const spec = SIDECAR_SPECS[name];
248
+ if (!spec) return [`sidecar must be one of ${SIDECAR_NAMES.join(", ")}`];
249
+ if (obj.schemaVersion !== spec.version)
250
+ errors.push(`schemaVersion must be ${spec.version} for a ${name} sidecar`);
251
+ if (!isObject(obj.parent) || typeof obj.parent.generatedAt !== "string")
252
+ errors.push("parent.generatedAt must name the main graph build");
253
+ else if (isObject(main)) {
254
+ const same = [
255
+ ["generatedAt", main.generatedAt],
256
+ ["baseCommit", main.baseCommit ?? null],
257
+ ["stack", main.stack],
258
+ ["nodes", Array.isArray(main.nodes) ? main.nodes.length : undefined],
259
+ ["edges", Array.isArray(main.edges) ? main.edges.length : undefined],
260
+ ];
261
+ for (const [key, want] of same)
262
+ if (want !== undefined && (obj.parent[key] ?? null) !== want)
263
+ errors.push(
264
+ `parent.${key} (${String(obj.parent[key]).slice(0, 40)}) does not match the main graph (${String(want).slice(0, 40)}): the sidecar was built against another graph`,
265
+ );
266
+ }
267
+ if (typeof obj.generatedAt !== "string" || Number.isNaN(Date.parse(obj.generatedAt)))
268
+ errors.push("generatedAt must be an ISO 8601 timestamp");
269
+ if (obj.truncated !== null && obj.truncated !== undefined) {
270
+ if (!isObject(obj.truncated) || !isInt(obj.truncated.edges) || !isInt(obj.truncated.nodes))
271
+ errors.push("truncated must be null or {edges, nodes, reason}");
272
+ } else if (obj.truncated === undefined) errors.push("truncated must be present (null when not)");
273
+
274
+ const kinds = new Set(spec.nodeKinds);
275
+ const byId = checkNodes(obj.nodes, { kinds, fields: SIDECAR_NODE_FIELDS, errors });
276
+ const edgeKinds = new Set(spec.edgeKinds);
277
+ const kindOf = (id) => (byId.get(id) || mainById?.get(id))?.kind;
278
+ if (!Array.isArray(obj.edges)) errors.push("edges must be an array");
279
+ else {
280
+ obj.edges.forEach((e, i) => {
281
+ const p = `edges[${i}]`;
282
+ if (!isObject(e)) {
283
+ errors.push(`${p} must be an object`);
284
+ return;
285
+ }
286
+ if (typeof e.from !== "string") errors.push(`${p}.from must be a string`);
287
+ if (typeof e.to !== "string") errors.push(`${p}.to must be a string`);
288
+ if (!edgeKinds.has(e.kind))
289
+ errors.push(`${p}.kind must be one of ${[...edgeKinds].join(",")} in a ${name} sidecar`);
290
+ for (const k of Object.keys(e))
291
+ if (!SIDECAR_EDGE_FIELDS.has(k))
292
+ errors.push(`${p} has a field this schema does not define: ${k}`);
293
+ if (e.via !== undefined) {
294
+ const ok =
295
+ e.kind === "calls"
296
+ ? CALL_VIAS.has(e.via)
297
+ : typeof e.via === "string" && VIA_TOKEN.test(e.via);
298
+ if (!ok) errors.push(`${p}.via is not a known derivation: ${String(e.via).slice(0, 40)}`);
299
+ }
300
+ const allowed = name === "calls" ? CALL_CONFIDENCE : CONFIDENCE;
301
+ if (e.confidence !== undefined && !allowed.has(e.confidence))
302
+ errors.push(`${p}.confidence must be one of ${[...allowed].join(",")}`);
303
+ if (e.kind === "calls") {
304
+ if (e.via === undefined) errors.push(`${p} is a calls edge without via`);
305
+ if (e.confidence === undefined) errors.push(`${p} is a calls edge without confidence`);
306
+ const from = kindOf(e.from);
307
+ const to = kindOf(e.to);
308
+ if (from !== undefined && from !== "symbol" && from !== "file")
309
+ errors.push(`${p} is a calls edge from a ${from} node`);
310
+ if (to !== undefined && to !== "symbol")
311
+ errors.push(`${p} is a calls edge to a ${to} node`);
312
+ }
313
+ });
314
+ dangling(obj.edges, (id) => byId.has(id) || !mainById || mainById.has(id), errors);
315
+ }
162
316
 
317
+ if (isObject(obj.annotations)) {
318
+ for (const [id, extra] of Object.entries(obj.annotations)) {
319
+ if (!isObject(extra) || Object.keys(extra).some((k) => k !== "owner"))
320
+ errors.push(`annotations["${id.slice(0, 80)}"] may only carry an owner`);
321
+ if (mainById && !mainById.has(id))
322
+ errors.push(`annotations name a node the main graph lacks: ${id.slice(0, 80)}`);
323
+ }
324
+ } else if (obj.annotations !== undefined) errors.push("annotations must be an object");
325
+
326
+ if (!isObject(obj.stats)) errors.push("stats must be an object");
327
+ else {
328
+ if (Array.isArray(obj.nodes) && obj.stats.nodes !== obj.nodes.length)
329
+ errors.push(
330
+ `stats.nodes (${obj.stats.nodes}) does not match nodes.length (${obj.nodes.length})`,
331
+ );
332
+ if (Array.isArray(obj.edges) && obj.stats.edges !== obj.edges.length)
333
+ errors.push(
334
+ `stats.edges (${obj.stats.edges}) does not match edges.length (${obj.edges.length})`,
335
+ );
336
+ const count = (list, pred) => (Array.isArray(list) ? list.filter(pred).length : 0);
337
+ const expect = {
338
+ calls: () => count(obj.edges, (e) => isObject(e) && e.kind === "calls"),
339
+ docs: () => count(obj.nodes, (n) => isObject(n) && n.kind === "doc"),
340
+ rationale: () => count(obj.nodes, (n) => isObject(n) && n.kind === "rationale"),
341
+ infra: () => count(obj.nodes, (n) => isObject(n) && n.kind === "infra"),
342
+ env: () => count(obj.nodes, (n) => isObject(n) && n.kind === "env"),
343
+ config: () => count(obj.nodes, (n) => isObject(n) && n.kind === "config"),
344
+ };
345
+ const owns = {
346
+ calls: ["calls"],
347
+ docs: ["docs", "rationale"],
348
+ infra: ["infra", "env", "config"],
349
+ };
350
+ for (const key of owns[name]) {
351
+ if (obj.truncated) continue;
352
+ if (obj.stats[key] !== expect[key]())
353
+ errors.push(
354
+ `stats.${key} (${obj.stats[key]}) does not match the ${expect[key]()} in the file`,
355
+ );
356
+ }
357
+ }
358
+ if (name !== "calls") {
359
+ if (!isObject(obj.manifest))
360
+ errors.push("manifest must be present for a docs or infra sidecar");
361
+ else checkManifest(obj.manifest, errors);
362
+ }
163
363
  return errors;
164
364
  }
165
365
 
166
- (async () => {
167
- const raw = await readInput();
168
- let obj;
169
- try {
170
- obj = JSON.parse(raw);
171
- } catch (e) {
172
- console.log(JSON.stringify({ ok: false, errors: [`JSON parse error: ${e.message}`] }));
173
- process.exitCode = 1;
174
- return;
366
+ /**
367
+ * @param {object} obj - a parsed main graph or sidecar
368
+ * @param {{main?: object}} [opts] - the parsed main graph, to resolve a sidecar's endpoints
369
+ * @returns {string[]} errors, at most MAX_ERRORS and a count of the rest
370
+ */
371
+ export function validate(obj, opts = {}) {
372
+ if (!isObject(obj)) return ["root must be an object"];
373
+ let errors;
374
+ if (obj.sidecar !== undefined) {
375
+ const mainById =
376
+ opts.main && Array.isArray(opts.main.nodes)
377
+ ? new Map(opts.main.nodes.filter(isObject).map((n) => [n.id, n]))
378
+ : null;
379
+ errors = validateSidecar(obj, mainById, opts.main || null);
380
+ } else errors = validateMain(obj);
381
+ if (errors.length > MAX_ERRORS) {
382
+ const rest = errors.length - MAX_ERRORS;
383
+ errors = [...errors.slice(0, MAX_ERRORS), `... and ${rest} more`];
175
384
  }
176
- const errors = validate(obj);
177
- if (errors.length === 0) {
178
- console.log(JSON.stringify({ ok: true, errors: [] }));
179
- return;
385
+ return errors;
386
+ }
387
+
388
+ /** The main graph and each sidecar beside it, validated together. */
389
+ export function validateSet(graphPath) {
390
+ const main = JSON.parse(readFileSync(graphPath, "utf-8"));
391
+ const errors = validate(main);
392
+ for (const name of SIDECAR_NAMES) {
393
+ const p = sidecarPath(graphPath, name);
394
+ if (!existsSync(p)) continue;
395
+ let side;
396
+ try {
397
+ side = JSON.parse(readFileSync(p, "utf-8"));
398
+ } catch (e) {
399
+ errors.push(`${basename(p)}: JSON parse error: ${e.message}`);
400
+ continue;
401
+ }
402
+ for (const err of validate(side, { main })) errors.push(`${basename(p)}: ${err}`);
180
403
  }
181
- // `errors` is one entry per violation and has no bound, so process.exit(1)
182
- // here truncated the report exactly when there was most of it to read.
183
- console.log(JSON.stringify({ ok: false, errors }));
184
- process.exitCode = 1;
185
- })();
404
+ return errors;
405
+ }
406
+
407
+ const SELF = import.meta.url.endsWith(basename(process.argv[1] || ""));
408
+ if (SELF) {
409
+ (async () => {
410
+ const arg = process.argv[2];
411
+ let errors;
412
+ try {
413
+ if (arg && arg !== "-" && existsSync(arg) && !/\.(calls|docs|infra)\.json$/.test(arg)) {
414
+ errors = validateSet(arg);
415
+ } else {
416
+ const obj = JSON.parse(await readInput());
417
+ let main = null;
418
+ const m = arg && arg.match(/^(.*)\.(calls|docs|infra)\.json$/);
419
+ if (m && existsSync(`${m[1]}.json`))
420
+ main = JSON.parse(readFileSync(`${m[1]}.json`, "utf-8"));
421
+ errors = validate(obj, { main });
422
+ }
423
+ } catch (e) {
424
+ console.log(JSON.stringify({ ok: false, errors: [`JSON parse error: ${e.message}`] }));
425
+ process.exitCode = 1;
426
+ return;
427
+ }
428
+ if (errors.length === 0) {
429
+ console.log(JSON.stringify({ ok: true, errors: [] }));
430
+ return;
431
+ }
432
+ console.log(JSON.stringify({ ok: false, errors }));
433
+ process.exitCode = 1;
434
+ })();
435
+ }
@@ -368,6 +368,44 @@ export function clearPassedWait(next, current, payload) {
368
368
  next.waitingFor = null;
369
369
  }
370
370
 
371
+ const WAIT_STATUSES = new Set(["awaiting_input", "awaiting-user-test-main-checkout"]);
372
+
373
+ /** The wait a state records: its `waitingFor` step, else its waiting status, else null. */
374
+ function waitOf(state) {
375
+ if (typeof state?.waitingFor === "string" && state.waitingFor) return state.waitingFor;
376
+ return WAIT_STATUSES.has(state?.status) ? state.status : null;
377
+ }
378
+
379
+ /**
380
+ * Record when a run began to wait for a person, as `waitingSince`. Entering a
381
+ * wait, or moving to a different one, stamps `now`; a write that leaves the
382
+ * same wait in place keeps the earlier stamp, so a rewrite of the file (a
383
+ * migration, a copy, a restore) does not restart the clock; ending the wait
384
+ * nulls the field where it was present. A `waitingSince` the caller sets
385
+ * itself is kept.
386
+ *
387
+ * @param {object} next the document as it would be written
388
+ * @param {object} current the document on disk
389
+ * @param {string} [now]
390
+ */
391
+ export function stampWaitingSince(next, current, now = new Date().toISOString()) {
392
+ if (!next || typeof next !== "object" || Array.isArray(next)) return;
393
+ const wait = waitOf(next);
394
+ if (!wait) {
395
+ if (next.waitingSince != null) next.waitingSince = null;
396
+ return;
397
+ }
398
+ const sameWait = waitOf(current) === wait;
399
+ const callerSet =
400
+ typeof next.waitingSince === "string" && next.waitingSince !== current?.waitingSince;
401
+ if (callerSet) return;
402
+ if (sameWait && typeof current?.waitingSince === "string") {
403
+ next.waitingSince = current.waitingSince;
404
+ return;
405
+ }
406
+ next.waitingSince = now;
407
+ }
408
+
371
409
  /**
372
410
  * The refusal message for a write that moves the run into `complete` without
373
411
  * the evidence lib/run-completion.mjs asks for, or null.
@@ -626,6 +664,7 @@ async function main() {
626
664
  }
627
665
 
628
666
  next.rev = priorRev + 1;
667
+ stampWaitingSince(next, current);
629
668
  stampHostSession(next, payload, fileExistedBefore);
630
669
 
631
670
  const unknown = undeclaredKeys(payload, replace ? {} : current);