@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
@@ -0,0 +1,272 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * @file graph-export.mjs - the code graph in formats other tools read.
5
+ *
6
+ * Formats:
7
+ * dot Graphviz, one node per vertex, edges labelled with their kind
8
+ * graphml GraphML for yEd, Gephi and similar
9
+ * csv nodes and edges as two CSV blocks separated by a blank line
10
+ * mermaid a flowchart; with --level module it is the architecture diagram
11
+ *
12
+ * Levels:
13
+ * symbol every node (default for dot, graphml, csv)
14
+ * file symbol edges are lifted to the files that hold their ends
15
+ * module files are grouped by their first --dirs path segments (default 2)
16
+ * and edges are summed into one weighted arrow per pair
17
+ *
18
+ * A whole graph is rarely readable, so the view is capped: when it holds more
19
+ * than --max-nodes vertices (default 300) the level is raised one step at a
20
+ * time, symbol to file to module, until it fits, and the header says so.
21
+ *
22
+ * Inputs:
23
+ * --graph <path> Graph file. Default: ~/.claude/knowledge/<cwd name>/code-graph.json
24
+ * --format <f> dot | graphml | csv | mermaid. Required.
25
+ * --level <l> symbol | file | module
26
+ * --dirs N Path segments that name a module. Default 2.
27
+ * --kind K,... Keep only these edge kinds
28
+ * --min-confidence C Drop derived edges below low | medium | high. Default: medium; low
29
+ * edges are leads and appear only when asked for
30
+ * --max-nodes N Cap, see above. Default 300.
31
+ * --out <path> Write here instead of stdout
32
+ *
33
+ * Exit codes: 0 written, 1 graph missing or unreadable, 64 usage.
34
+ *
35
+ * @module pipeline/scripts/graph-export
36
+ */
37
+
38
+ import { writeFileSync } from "node:fs";
39
+ import { resolve } from "node:path";
40
+ import { loadGraph, defaultGraphPath } from "./graph-query.mjs";
41
+ import { layerNotes } from "./_graph-sidecars.mjs";
42
+ import { parseArgs } from "../lib/args.mjs";
43
+ import { exitOnHelp } from "../lib/cli-help.mjs";
44
+
45
+ exitOnHelp(import.meta.url);
46
+
47
+ const RANK = { low: 1, medium: 2, high: 3, certain: 4 };
48
+ const RANK_NAME = ["", "low", "medium", "high", "certain"];
49
+ const LEVELS = ["symbol", "file", "module"];
50
+ const FORMATS = new Set(["dot", "graphml", "csv", "mermaid"]);
51
+
52
+ /**
53
+ * The vertices and weighted edges of the graph at a level.
54
+ *
55
+ * @param {object} graph
56
+ * @param {{level: string, dirs: number, kinds: Set<string>|null, min: number, fixedDirs?: boolean}} opts
57
+ * The module view groups by the first `dirs` directories; unless `fixedDirs`, it takes more
58
+ * until the graph has at least two modules, so a tree that keeps everything under `src/`
59
+ * still draws.
60
+ * @returns {{vertices: Map<string, {id: string, label: string, kind: string}>, edges: Map<string, {from: string, to: string, kind: string, weight: number, rank: number}>}}
61
+ * `rank` is the weakest confidence among the edges folded into one arrow (0 when none carries one).
62
+ */
63
+ export function projection(graph, opts) {
64
+ const view = project(graph, opts);
65
+ if (opts.level !== "module" || opts.fixedDirs) return view;
66
+ let dirs = opts.dirs;
67
+ let current = view;
68
+ while (current.edges.size === 0 && dirs < MAX_MODULE_DIRS) {
69
+ dirs++;
70
+ const deeper = project(graph, { ...opts, dirs });
71
+ if (deeper.edges.size === 0 && deeper.vertices.size <= current.vertices.size) break;
72
+ current = deeper;
73
+ }
74
+ return current;
75
+ }
76
+
77
+ const MAX_MODULE_DIRS = 6;
78
+
79
+ function project(graph, { level, dirs, kinds, min }) {
80
+ const byId = new Map(graph.nodes.map((n) => [n.id, n]));
81
+ const fileOf = new Map();
82
+ for (const e of graph.edges)
83
+ if (e.kind === "defines" && e.from.startsWith("file:")) fileOf.set(e.to, e.from);
84
+ const moduleOf = (path) =>
85
+ path ? path.split("/").slice(0, -1).slice(0, dirs).join("/") || "." : "(external)";
86
+
87
+ const key = (id) => {
88
+ const n = byId.get(id);
89
+ if (!n) return null;
90
+ if (level === "symbol")
91
+ return { id, label: n.owner ? `${n.owner}.${n.name}` : n.name, kind: n.symbolKind || n.kind };
92
+ const fid = n.kind === "file" ? id : fileOf.get(id) || id;
93
+ const fn = byId.get(fid) || n;
94
+ if (level === "file") return { id: fid, label: fn.path || fn.name, kind: fn.kind };
95
+ if (!fn.path) return { id: `${fn.kind}:${fn.name}`, label: fn.name, kind: fn.kind };
96
+ const m = moduleOf(fn.path);
97
+ return { id: `module:${m}`, label: m, kind: "module" };
98
+ };
99
+
100
+ const vertices = new Map();
101
+ const edges = new Map();
102
+ for (const e of graph.edges) {
103
+ if (e.kind === "defines" && level !== "symbol") continue;
104
+ if (kinds && !kinds.has(e.kind)) continue;
105
+ if (e.confidence && (RANK[e.confidence] ?? 4) < min) continue;
106
+ const a = key(e.from);
107
+ const b = key(e.to);
108
+ if (!a || !b || a.id === b.id) continue;
109
+ vertices.set(a.id, a);
110
+ vertices.set(b.id, b);
111
+ const k = `${a.id}\0${b.id}\0${e.kind}`;
112
+ const rank = RANK[e.confidence] ?? 0;
113
+ const have = edges.get(k);
114
+ if (have) {
115
+ have.weight++;
116
+ if (rank && (!have.rank || rank < have.rank)) have.rank = rank;
117
+ } else edges.set(k, { from: a.id, to: b.id, kind: e.kind, weight: 1, rank });
118
+ }
119
+ return { vertices, edges };
120
+ }
121
+
122
+ const MAX_LABEL = 300;
123
+ // eslint-disable-next-line no-control-regex
124
+ const CONTROL = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F\uFFFE\uFFFF]/g;
125
+
126
+ /** A graph name made safe to print anywhere: no control characters, no line breaks, bounded. */
127
+ const clean = (value) => {
128
+ const text = String(value)
129
+ .replace(/[\r\n\t\u2028\u2029]+/g, " ")
130
+ .replace(CONTROL, "");
131
+ const whole = text.toWellFormed ? text.toWellFormed() : text;
132
+ return whole.length > MAX_LABEL ? `${whole.slice(0, MAX_LABEL)}...` : whole;
133
+ };
134
+
135
+ const MERMAID_CODES = { "&": "#amp;", "<": "#lt;", ">": "#gt;", '"': "#quot;", "|": "#124;" };
136
+
137
+ /**
138
+ * Escape a name for each output syntax. A graph is built from a repository, so a
139
+ * file or symbol name is attacker-chosen text: it must not close a quoted
140
+ * string, open a markup tag, start a comment or begin a spreadsheet formula.
141
+ */
142
+ const esc = {
143
+ dot: (s) => clean(s).replace(/\\/g, "\\\\").replace(/"/g, '\\"'),
144
+ xml: (s) =>
145
+ clean(s).replace(
146
+ /[<>&"']/g,
147
+ (c) => ({ "<": "&lt;", ">": "&gt;", "&": "&amp;", '"': "&quot;", "'": "&apos;" })[c],
148
+ ),
149
+ csv: (s) => {
150
+ const raw = String(s);
151
+ let text = clean(raw);
152
+ const formula = /^[\t\r=+\-@]/.test(raw) || /^[=+\-@]/.test(text);
153
+ if (formula) text = `'${text.replace(/^\s+/, "")}`;
154
+ return formula || /[",]/.test(text) || /^\s|\s$/.test(text)
155
+ ? `"${text.replace(/"/g, '""')}"`
156
+ : text;
157
+ },
158
+ mermaid: (s) => clean(s).replace(/[&<>"|]/g, (c) => MERMAID_CODES[c]),
159
+ comment: (s) => clean(s).replace(/--/g, "- -"),
160
+ };
161
+
162
+ /**
163
+ * @param {string} format
164
+ * @param {{vertices: Map<string, object>, edges: Map<string, object>}} view
165
+ * @param {string} note - one line saying what the view is
166
+ * @returns {string}
167
+ */
168
+ export function render(format, view, note) {
169
+ const vs = [...view.vertices.values()];
170
+ const es = [...view.edges.values()];
171
+ const idx = new Map(vs.map((v, i) => [v.id, i]));
172
+ if (format === "dot") {
173
+ const L = [`// ${esc.comment(note)}`, "digraph code {", " rankdir=LR;"];
174
+ for (const v of vs)
175
+ L.push(` n${idx.get(v.id)} [label="${esc.dot(v.label)}", tooltip="${esc.dot(v.kind)}"];`);
176
+ for (const e of es) {
177
+ const w = e.weight > 1 ? ` (${e.weight})` : "";
178
+ const conf = e.rank ? ` ${RANK_NAME[e.rank]}` : "";
179
+ L.push(` n${idx.get(e.from)} -> n${idx.get(e.to)} [label="${esc.dot(e.kind + w + conf)}"];`);
180
+ }
181
+ L.push("}");
182
+ return L.join("\n") + "\n";
183
+ }
184
+ if (format === "graphml") {
185
+ const L = [
186
+ '<?xml version="1.0" encoding="UTF-8"?>',
187
+ `<!-- ${esc.comment(note)} -->`,
188
+ '<graphml xmlns="http://graphml.graphdrawing.org/xmlns">',
189
+ ' <key id="label" for="node" attr.name="label" attr.type="string"/>',
190
+ ' <key id="kind" for="node" attr.name="kind" attr.type="string"/>',
191
+ ' <key id="ekind" for="edge" attr.name="kind" attr.type="string"/>',
192
+ ' <key id="weight" for="edge" attr.name="weight" attr.type="int"/>',
193
+ ' <key id="confidence" for="edge" attr.name="confidence" attr.type="string"/>',
194
+ ' <graph id="G" edgedefault="directed">',
195
+ ];
196
+ for (const v of vs) {
197
+ L.push(
198
+ ` <node id="n${idx.get(v.id)}"><data key="label">${esc.xml(v.label)}</data><data key="kind">${esc.xml(v.kind)}</data></node>`,
199
+ );
200
+ }
201
+ es.forEach((e, i) => {
202
+ L.push(
203
+ ` <edge id="e${i}" source="n${idx.get(e.from)}" target="n${idx.get(e.to)}"><data key="ekind">${esc.xml(e.kind)}</data><data key="weight">${e.weight}</data>${e.rank ? `<data key="confidence">${RANK_NAME[e.rank]}</data>` : ""}</edge>`,
204
+ );
205
+ });
206
+ L.push(" </graph>", "</graphml>");
207
+ return L.join("\n") + "\n";
208
+ }
209
+ if (format === "csv") {
210
+ const L = ["id,label,kind"];
211
+ for (const v of vs) L.push([v.id, v.label, v.kind].map(esc.csv).join(","));
212
+ L.push("", "from,to,kind,weight,confidence");
213
+ for (const e of es)
214
+ L.push(
215
+ [e.from, e.to, e.kind, Number(e.weight), RANK_NAME[e.rank] || ""].map(esc.csv).join(","),
216
+ );
217
+ return L.join("\n") + "\n";
218
+ }
219
+ const L = [`%% ${esc.comment(note)}`, "flowchart LR"];
220
+ for (const v of vs) L.push(` n${idx.get(v.id)}["${esc.mermaid(v.label)}"]`);
221
+ for (const e of es) {
222
+ const w = e.weight > 1 ? ` x${e.weight}` : "";
223
+ const conf = e.rank ? ` ${RANK_NAME[e.rank]}` : "";
224
+ L.push(` n${idx.get(e.from)} -->|${esc.mermaid(e.kind + w + conf)}| n${idx.get(e.to)}`);
225
+ }
226
+ return L.join("\n") + "\n";
227
+ }
228
+
229
+ function main() {
230
+ const { flags } = parseArgs(process.argv.slice(2), { equals: false, terminator: false });
231
+ const format = flags.format;
232
+ if (typeof format !== "string" || !FORMATS.has(format)) {
233
+ process.stderr.write("graph-export: --format dot|graphml|csv|mermaid required\n");
234
+ process.exit(64);
235
+ }
236
+ const path = flags.graph && flags.graph !== true ? resolve(flags.graph) : defaultGraphPath();
237
+ let graph;
238
+ try {
239
+ graph = loadGraph(path);
240
+ } catch (e) {
241
+ process.stderr.write(`graph-export: ${e.message}\n`);
242
+ process.exit(1);
243
+ }
244
+ const requested =
245
+ typeof flags.level === "string" ? flags.level : format === "mermaid" ? "module" : "symbol";
246
+ if (!LEVELS.includes(requested)) {
247
+ process.stderr.write("graph-export: --level symbol|file|module\n");
248
+ process.exit(64);
249
+ }
250
+ const opts = {
251
+ dirs: Number(flags.dirs) > 0 ? Number(flags.dirs) : 2,
252
+ fixedDirs: Number(flags.dirs) > 0,
253
+ kinds: typeof flags.kind === "string" ? new Set(flags.kind.split(",")) : null,
254
+ min: RANK[flags["min-confidence"]] || RANK.medium,
255
+ };
256
+ const cap = Number(flags["max-nodes"]) > 0 ? Number(flags["max-nodes"]) : 300;
257
+ let level = LEVELS.indexOf(requested);
258
+ let view = projection(graph, { ...opts, level: LEVELS[level] });
259
+ while (view.vertices.size > cap && level < LEVELS.length - 1) {
260
+ level++;
261
+ view = projection(graph, { ...opts, level: LEVELS[level] });
262
+ }
263
+ const raised =
264
+ LEVELS[level] !== requested ? ` (raised from ${requested}: more than ${cap} vertices)` : "";
265
+ const note = `${LEVELS[level]} view of ${graph.stack} graph, ${view.vertices.size} vertices, ${view.edges.size} edges${raised}`;
266
+ const text = render(format, view, note);
267
+ for (const n of layerNotes(graph)) process.stderr.write(`graph-export: note: ${n}\n`);
268
+ if (typeof flags.out === "string") writeFileSync(resolve(flags.out), text, "utf8");
269
+ else process.stdout.write(text);
270
+ }
271
+
272
+ if (process.argv[1] && process.argv[1].endsWith("graph-export.mjs")) main();
@@ -9,7 +9,8 @@
9
9
  * and an extension allowed, with no narrower path) and a current code graph
10
10
  * exists for the repository, it adds one line of additionalContext naming
11
11
  * `/multi-agent:graph ask` and the toolkit's graph query tool, which return
12
- * ranked definitions and their neighbours within a token budget.
12
+ * ranked definitions and their neighbours within a token budget, and names up
13
+ * to three nodes of the graph that carry exactly that name.
13
14
  *
14
15
  * "Current" is graphFreshness() from _diff-impact.mjs, the check
15
16
  * graph-affected.mjs reports: the build commit is HEAD and no indexed file
@@ -42,6 +43,7 @@ import { exitOnHelp } from "../lib/cli-help.mjs";
42
43
  exitOnHelp(import.meta.url);
43
44
 
44
45
  const MAX_INPUT = 256 * 1024;
46
+ const MAX_NAMED = 3;
45
47
  const IDENT = /^[A-Za-z_][A-Za-z0-9_]{2,63}$/;
46
48
  const GLOB_IDENT = /^(?:\*\*\/)?\*?([A-Za-z_][A-Za-z0-9_]{2,63})\*?(?:\.[A-Za-z0-9*]+)?$/;
47
49
 
@@ -158,8 +160,19 @@ export function answer(
158
160
  return null;
159
161
  }
160
162
  if (!saveState(session, { hinted: true })) return null;
163
+ const known = (graph.nodes || [])
164
+ .filter((n) => n.name === ident && n.kind !== "module")
165
+ .slice(0, MAX_NAMED);
166
+ const named = known.length
167
+ ? ` The graph holds ${known
168
+ .map(
169
+ (n) =>
170
+ `${n.owner ? `${n.owner}.` : ""}${n.name} (${n.symbolKind || n.kind}) ${n.path || ""}${n.line ? `:${n.line}` : ""}`,
171
+ )
172
+ .join("; ")}.`
173
+ : "";
161
174
  const text =
162
- `A current code graph exists for ${repo.slug}. For a symbol or concept search such as ` +
175
+ `A current code graph exists for ${repo.slug}.${named} For a symbol or concept search such as ` +
163
176
  `\`${ident}\`, \`/multi-agent:graph ask "${ident}"\` or the toolkit tool ` +
164
177
  `mcp__multi-agent__graph_query returns ranked definitions and their neighbours ` +
165
178
  `within a token budget. Grep stays the right tool for an exact text match.`;