@mmerterden/multi-agent-pipeline 20.40.0 → 20.41.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 (76) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +4 -1
  3. package/README.tr.md +4 -1
  4. package/docs/adr/0019-product-tree.md +143 -0
  5. package/docs/adr/README.md +1 -0
  6. package/docs/ecosystem.md +9 -9
  7. package/docs/facts.json +4 -4
  8. package/docs/features.md +6 -0
  9. package/install/claude.mjs +12 -3
  10. package/install/templates/claude-hooks.json +12 -1
  11. package/manifest.json +79 -60
  12. package/package.json +2 -2
  13. package/pipeline/commands/multi-agent/review-analysis/SKILL.md +1 -1
  14. package/pipeline/contract/types/index.d.ts +53 -49
  15. package/pipeline/lib/context-budget.mjs +5 -42
  16. package/pipeline/lib/hook-registration.mjs +5 -0
  17. package/pipeline/lib/md2confluence-v3.py +12 -4
  18. package/pipeline/lib/tiered-budget.mjs +87 -0
  19. package/pipeline/multi-agent-refs/analysis/review.md +23 -1
  20. package/pipeline/multi-agent-refs/features/code-graph.md +11 -1
  21. package/pipeline/multi-agent-refs/features/doctor.md +32 -1
  22. package/pipeline/multi-agent-refs/features/product-tree.md +68 -0
  23. package/pipeline/multi-agent-refs/features/unattended-security.md +88 -1
  24. package/pipeline/multi-agent-refs/features/usage-reporting.md +31 -0
  25. package/pipeline/multi-agent-refs/help/en.md +1 -0
  26. package/pipeline/multi-agent-refs/help/tr.md +1 -0
  27. package/pipeline/multi-agent-refs/knowledge.md +58 -0
  28. package/pipeline/multi-agent-refs/phases/phase-0-init.md +4 -0
  29. package/pipeline/multi-agent-refs/phases/phase-1-plan.md +4 -0
  30. package/pipeline/multi-agent-refs/phases/phase-5-report.md +2 -0
  31. package/pipeline/office/index.html +13 -7
  32. package/pipeline/office/toolkit.json +1 -1
  33. package/pipeline/preferences-template.json +1 -0
  34. package/pipeline/schemas/learnings-ledger.schema.json +35 -2
  35. package/pipeline/schemas/office-archive.schema.json +225 -128
  36. package/pipeline/schemas/prefs.schema.json +83 -0
  37. package/pipeline/schemas/product-tree-overlay.schema.json +57 -0
  38. package/pipeline/schemas/product-tree.schema.json +136 -0
  39. package/pipeline/schemas/toolkit-path-params.json +1 -1
  40. package/pipeline/scripts/_eval-stats.mjs +182 -0
  41. package/pipeline/scripts/_guard_rules.py +130 -1
  42. package/pipeline/scripts/_learnings-promotion.mjs +229 -0
  43. package/pipeline/scripts/_locate.mjs +539 -0
  44. package/pipeline/scripts/_memory-policy.mjs +213 -14
  45. package/pipeline/scripts/_product-tree-extract.mjs +761 -0
  46. package/pipeline/scripts/_product-tree.mjs +923 -0
  47. package/pipeline/scripts/_retrieval.mjs +13 -2
  48. package/pipeline/scripts/_stack-routing.mjs +1 -0
  49. package/pipeline/scripts/_user_guard_rules.py +744 -0
  50. package/pipeline/scripts/agent-guard.py +61 -5
  51. package/pipeline/scripts/agent-guard.sh +8 -1
  52. package/pipeline/scripts/doctor.mjs +157 -1
  53. package/pipeline/scripts/graph-query.mjs +257 -17
  54. package/pipeline/scripts/learnings-ledger.mjs +664 -43
  55. package/pipeline/scripts/note-mcp-failure.mjs +356 -0
  56. package/pipeline/scripts/note-mcp-failure.sh +19 -0
  57. package/pipeline/scripts/note-skill-use.mjs +93 -23
  58. package/pipeline/scripts/office-archive.mjs +23 -10
  59. package/pipeline/scripts/office-memory.mjs +4 -1
  60. package/pipeline/scripts/persona-walk-anchors.mjs +443 -0
  61. package/pipeline/scripts/product-tree-eval.mjs +685 -0
  62. package/pipeline/scripts/product-tree.mjs +544 -0
  63. package/pipeline/skills/.skill-manifest.json +11 -7
  64. package/pipeline/skills/.skills-index.json +12 -1
  65. package/pipeline/skills/shared/README.md +4 -3
  66. package/pipeline/skills/shared/core/google-play-compliance/references/examples.md +1 -1
  67. package/pipeline/skills/shared/core/multi-agent-review-analysis/SKILL.md +1 -1
  68. package/pipeline/skills/shared/external/backlog/SKILL.md +1 -1
  69. package/pipeline/skills/shared/external/localization-reuse-map/SKILL.md +1 -1
  70. package/pipeline/skills/shared/external/spec-persona-walk/SKILL.md +118 -0
  71. package/pipeline/skills/shared/external/spec-persona-walk/evals/evals.json +50 -0
  72. package/pipeline/skills/shared/external/spec-persona-walk/scripts/persona-walk-anchors.mjs +443 -0
  73. package/pipeline/skills/shared/external/storekit/SKILL.md +1 -1
  74. package/pipeline/skills/shared/external/swiftdata/SKILL.md +1 -1
  75. package/pipeline/skills/skill-copies.json +5 -1
  76. package/pipeline/skills/skills-index.md +2 -1
@@ -0,0 +1,761 @@
1
+ /**
2
+ * @file _product-tree-extract.mjs - per-file extraction for the product tree.
3
+ *
4
+ * Reads one source or strings file at a time and returns plain facts:
5
+ * declared views and composables with their spans, navigation references by
6
+ * name, route declarations, programmatic navigation calls, string literals,
7
+ * Retrofit and app router endpoints, and localized strings. Nothing here
8
+ * looks across files; _product-tree.mjs resolves the names against the whole
9
+ * repository.
10
+ *
11
+ * Source is scanned once into two same-length copies, one with comments
12
+ * blanked and strings kept, one with both blanked, so a navigation call
13
+ * written in a comment never counts and a literal is read at the offset its
14
+ * structure was found.
15
+ *
16
+ * Zero dependencies (ADR-0004).
17
+ *
18
+ * @module pipeline/scripts/_product-tree-extract
19
+ */
20
+
21
+ export const TREE_STACKS = ["ios", "android", "web"];
22
+ export const EXTENSIONS = {
23
+ ios: [".swift"],
24
+ android: [".kt", ".java"],
25
+ web: [".tsx", ".jsx", ".ts", ".js", ".mjs"],
26
+ };
27
+ export const QUOTES = { ios: ['"""', '"'], android: ['"""', '"', "'"], web: ["`", '"', "'"] };
28
+
29
+ /** The tree stack a source path belongs to, or null. */
30
+ export function stackOf(path) {
31
+ for (const s of TREE_STACKS) if (EXTENSIONS[s].some((e) => path.endsWith(e))) return s;
32
+ return null;
33
+ }
34
+
35
+ /** Stacks with at least one source file, in TREE_STACKS order. */
36
+ export function detectStacks(paths) {
37
+ const seen = new Set(paths.map(stackOf).filter(Boolean));
38
+ return TREE_STACKS.filter((s) => seen.has(s));
39
+ }
40
+
41
+ /**
42
+ * Scan source once and return two same-length copies: `lit` with comments
43
+ * blanked and strings kept, `code` with both blanked. Structure is read from
44
+ * `code`, literal values from `lit` at the same offsets.
45
+ *
46
+ * @param {string} text
47
+ * @param {string[]} [quotes] string delimiters, longest first
48
+ */
49
+ export function scanSource(text, quotes = ['"""', '"', "'", "`"]) {
50
+ const n = text.length;
51
+ const lit = new Uint16Array(n);
52
+ const code = new Uint16Array(n);
53
+ for (let k = 0; k < n; k++) lit[k] = code[k] = text.charCodeAt(k);
54
+ const SPACE = 32;
55
+ const NL = 10;
56
+ const blank = (arr, from, to) => {
57
+ for (let k = from; k < to && k < n; k++) if (arr[k] !== NL) arr[k] = SPACE;
58
+ };
59
+ const openers = new Set(quotes.map((q) => q.charCodeAt(0)));
60
+ let i = 0;
61
+ while (i < n) {
62
+ const c = lit[i];
63
+ if (c === 47 && i + 1 < n) {
64
+ const d = text.charCodeAt(i + 1);
65
+ if (d === 47) {
66
+ let e = text.indexOf("\n", i);
67
+ if (e === -1) e = n;
68
+ blank(lit, i, e);
69
+ blank(code, i, e);
70
+ i = e;
71
+ continue;
72
+ }
73
+ if (d === 42) {
74
+ let e = text.indexOf("*/", i + 2);
75
+ e = e === -1 ? n : e + 2;
76
+ blank(lit, i, e);
77
+ blank(code, i, e);
78
+ i = e;
79
+ continue;
80
+ }
81
+ }
82
+ if (openers.has(c)) {
83
+ const q = quotes.find((t) => text.startsWith(t, i));
84
+ if (q) {
85
+ let j = i + q.length;
86
+ const single = q.length === 1 && q !== "`";
87
+ while (j < n) {
88
+ const x = text.charCodeAt(j);
89
+ if (x === 92) {
90
+ j += 2;
91
+ continue;
92
+ }
93
+ if (x === q.charCodeAt(0) && (q.length === 1 || text.startsWith(q, j))) {
94
+ j += q.length;
95
+ break;
96
+ }
97
+ if (x === NL && single) break;
98
+ j++;
99
+ }
100
+ blank(code, i, j);
101
+ i = j;
102
+ continue;
103
+ }
104
+ }
105
+ i++;
106
+ }
107
+ const str = (arr) => {
108
+ let out = "";
109
+ for (let k = 0; k < n; k += 8192)
110
+ out += String.fromCharCode.apply(null, arr.subarray(k, k + 8192));
111
+ return out;
112
+ };
113
+ return { lit: str(lit), code: str(code) };
114
+ }
115
+
116
+ /** Comments blanked, string literals kept, offsets preserved. */
117
+ export function stripCommentsKeepStrings(text, quotes) {
118
+ return scanSource(text, quotes).lit;
119
+ }
120
+
121
+ function lineCounter(text) {
122
+ const starts = [0];
123
+ for (let i = 0; i < text.length; i++) if (text[i] === "\n") starts.push(i + 1);
124
+ return (offset) => {
125
+ let lo = 0;
126
+ let hi = starts.length - 1;
127
+ while (lo < hi) {
128
+ const mid = (lo + hi + 1) >> 1;
129
+ if (starts[mid] <= offset) lo = mid;
130
+ else hi = mid - 1;
131
+ }
132
+ return lo + 1;
133
+ };
134
+ }
135
+
136
+ /** Index just past the bracket that closes the one at `open`, or -1. */
137
+ function closeOf(code, open) {
138
+ const o = code[open];
139
+ const c = o === "{" ? "}" : o === "(" ? ")" : o === "<" ? ">" : "]";
140
+ let depth = 0;
141
+ for (let i = open; i < code.length; i++) {
142
+ if (code[i] === o) depth++;
143
+ else if (code[i] === c) {
144
+ depth--;
145
+ if (depth === 0) return i + 1;
146
+ }
147
+ }
148
+ return -1;
149
+ }
150
+
151
+ /**
152
+ * The trailing closure after a call at `from`: skips an argument list in
153
+ * parentheses, then expects `{`. Returns [start, end) or null.
154
+ */
155
+ function closureAfter(code, from) {
156
+ let i = from;
157
+ while (i < code.length && /\s/.test(code[i])) i++;
158
+ if (code[i] === "<") {
159
+ const e = closeOf(code, i);
160
+ if (e < 0) return null;
161
+ i = e;
162
+ while (i < code.length && /\s/.test(code[i])) i++;
163
+ }
164
+ if (code[i] === "(") {
165
+ const e = closeOf(code, i);
166
+ if (e < 0) return null;
167
+ i = e;
168
+ while (i < code.length && /\s/.test(code[i])) i++;
169
+ }
170
+ if (code[i] !== "{") return null;
171
+ const end = closeOf(code, i);
172
+ return end < 0 ? null : [i, end];
173
+ }
174
+
175
+ /** The body block of a function whose parameter list opens at `paren`. */
176
+ function bodyAfterParams(code, paren) {
177
+ const e = closeOf(code, paren);
178
+ if (e < 0) return null;
179
+ for (let i = e; i < code.length && i < e + 200; i++) {
180
+ if (code[i] === "{") {
181
+ const end = closeOf(code, i);
182
+ return end < 0 ? null : [i, end];
183
+ }
184
+ if (code[i] === "=" && code[i + 1] !== ">") return null;
185
+ }
186
+ return null;
187
+ }
188
+
189
+ const CALL_RE = /\b([A-Z][A-Za-z0-9_]*)\s*(?:\(|\{)/g;
190
+
191
+ /** Capitalised calls inside [from, to), in order. */
192
+ function callsIn(code, from, to) {
193
+ const out = [];
194
+ const seg = code.slice(from, to);
195
+ for (const m of seg.matchAll(CALL_RE)) out.push({ name: m[1], offset: from + m.index });
196
+ return out;
197
+ }
198
+
199
+ function innermost(spans, offset) {
200
+ let best = null;
201
+ for (const s of spans) {
202
+ if (s.start <= offset && offset < s.end && (!best || s.start >= best.start)) best = s;
203
+ }
204
+ return best;
205
+ }
206
+
207
+ function snippet(lit, offset) {
208
+ return lit
209
+ .slice(offset, offset + 80)
210
+ .split("\n")[0]
211
+ .trim();
212
+ }
213
+
214
+ // ---------------------------------------------------------------- SwiftUI --
215
+
216
+ /**
217
+ * Per-file SwiftUI facts: declared types with their spans, navigation
218
+ * references by type name, route enums and programmatic calls. Names are
219
+ * resolved against the whole repository later, in resolveIos.
220
+ *
221
+ * @param {string} text
222
+ */
223
+ export function extractSwiftUI(text) {
224
+ const { lit, code } = scanSource(text, QUOTES.ios);
225
+ const lineAt = lineCounter(text);
226
+ const types = [];
227
+ const declRe =
228
+ /\b(struct|class|enum|actor|extension)\s+([A-Z][A-Za-z0-9_]*)(?:\s*<[^{]*?>)?\s*(?::\s*([^{]*))?\{/g;
229
+ for (const m of code.matchAll(declRe)) {
230
+ const open = m.index + m[0].length - 1;
231
+ const end = closeOf(code, open);
232
+ const body = code.slice(open, end < 0 ? code.length : end);
233
+ const conforms = (m[3] || "").split(/[,\s]+/).filter(Boolean);
234
+ types.push({
235
+ kind: m[1],
236
+ name: m[2],
237
+ start: m.index,
238
+ end: end < 0 ? code.length : end,
239
+ line: lineAt(m.index),
240
+ isView: m[1] !== "enum" && conforms.includes("View") && /\bvar\s+body\s*:/.test(body),
241
+ isApp: conforms.includes("App"),
242
+ cases:
243
+ m[1] === "enum"
244
+ ? [...body.matchAll(/\bcase\s+([^\n]+)/g)].flatMap((c) =>
245
+ c[1]
246
+ .split(",")
247
+ .map((x) => x.trim().match(/^([a-z][A-Za-z0-9_]*)/)?.[1])
248
+ .filter(Boolean),
249
+ )
250
+ : [],
251
+ });
252
+ }
253
+ const from = (offset) => innermost(types, offset)?.name ?? null;
254
+ const refs = [];
255
+ const add = (via, offset, target, extra = {}) =>
256
+ refs.push({ via, source: from(offset), target, line: lineAt(offset), ...extra });
257
+
258
+ for (const m of code.matchAll(/\bNavigationLink\s*\(\s*destination\s*:\s*([A-Z]\w*)\s*\(/g)) {
259
+ add("link", m.index, m[1]);
260
+ }
261
+ for (const m of code.matchAll(/\bNavigationLink\s*\{/g)) {
262
+ const block = closureAfter(code, m.index + "NavigationLink".length);
263
+ const first = block && callsIn(code, block[0] + 1, block[1])[0];
264
+ if (first) add("link", m.index, first.name, { firstOnly: true });
265
+ }
266
+ for (const m of code.matchAll(/\bNavigationLink\s*\(\s*value\s*:\s*/g)) {
267
+ const route = routeRefAt(code, m.index + m[0].length);
268
+ if (route) add("link-value", m.index, null, { route });
269
+ }
270
+ for (const m of code.matchAll(/\.navigationDestination\s*\(\s*(for|isPresented|item)\s*:/g)) {
271
+ const block = closureAfter(code, m.index + ".navigationDestination".length);
272
+ if (!block) continue;
273
+ if (m[1] !== "for") {
274
+ const first = callsIn(code, block[0] + 1, block[1])[0];
275
+ if (first) add("destination", m.index, first.name, { firstOnly: true });
276
+ continue;
277
+ }
278
+ const typeName = code.slice(m.index + m[0].length).match(/^\s*([A-Z][\w.]*)\.self/)?.[1];
279
+ if (!typeName) continue;
280
+ const seg = code.slice(block[0] + 1, block[1] - 1);
281
+ const caseRe = /\bcase\s+([^:]+):/g;
282
+ const cases = [...seg.matchAll(caseRe)];
283
+ if (!cases.length) {
284
+ const first = callsIn(code, block[0] + 1, block[1])[0];
285
+ if (first) {
286
+ refs.push({
287
+ via: "route",
288
+ source: from(m.index),
289
+ route: { type: typeName, name: null },
290
+ target: first.name,
291
+ firstOnly: true,
292
+ line: lineAt(m.index),
293
+ });
294
+ }
295
+ continue;
296
+ }
297
+ cases.forEach((c, i) => {
298
+ const segStart = block[0] + 1 + c.index + c[0].length;
299
+ const segEnd = i + 1 < cases.length ? block[0] + 1 + cases[i + 1].index : block[1];
300
+ const first = callsIn(code, segStart, segEnd)[0];
301
+ for (const nm of c[1].matchAll(/\.([a-z][A-Za-z0-9_]*)/g)) {
302
+ if (!first) continue;
303
+ refs.push({
304
+ via: "route",
305
+ source: from(m.index),
306
+ route: { type: typeName, name: nm[1] },
307
+ target: first.name,
308
+ firstOnly: true,
309
+ line: lineAt(block[0] + 1 + c.index),
310
+ });
311
+ }
312
+ });
313
+ }
314
+ for (const m of code.matchAll(/\.(sheet|fullScreenCover|popover)\s*\(/g)) {
315
+ const block = closureAfter(code, m.index + m[0].length - 1);
316
+ const first = block && callsIn(code, block[0] + 1, block[1])[0];
317
+ if (first) add(m[1] === "sheet" ? "sheet" : "cover", m.index, first.name, { firstOnly: true });
318
+ }
319
+ for (const m of code.matchAll(/\b(NavigationStack|NavigationView|NavigationSplitView)\s*[({]/g)) {
320
+ const block = closureAfter(code, m.index + m[1].length);
321
+ const first = block && callsIn(code, block[0] + 1, block[1])[0];
322
+ if (first) add("root", m.index, first.name, { firstOnly: true });
323
+ }
324
+ for (const m of code.matchAll(/\bTabView\s*[({]/g)) {
325
+ const block = closureAfter(code, m.index + "TabView".length);
326
+ if (!block) continue;
327
+ for (const c of callsIn(code, block[0] + 1, block[1]))
328
+ add("tab", c.offset, c.name, { firstOnly: true });
329
+ }
330
+ const entries = [];
331
+ for (const m of code.matchAll(
332
+ /\b(WindowGroup|Window|DocumentGroup|Settings|MenuBarExtra|UtilityWindow)\s*[({]/g,
333
+ )) {
334
+ if (from(m.index) && !types.find((t) => t.name === from(m.index))?.isApp) continue;
335
+ const block = closureAfter(code, m.index + m[1].length);
336
+ const first = block && callsIn(code, block[0] + 1, block[1])[0];
337
+ if (first) entries.push(first.name);
338
+ }
339
+ for (const m of code.matchAll(/\bUIHostingController\s*\(\s*rootView\s*:\s*([A-Z]\w*)\s*\(/g)) {
340
+ add("hosting", m.index, m[1]);
341
+ }
342
+ const programmatic = [];
343
+ const progRe =
344
+ /\b(?:path|navigationPath|navPath|stack|routes)\.append\(\s*|(?<!func\s+)\bnavigate\(\s*(?:to\s*:\s*)?|\.push(?:ViewController)?\(\s*|\.present\(\s*|(?<!func\s+)\bopenWindow\(\s*(?:id\s*:\s*)?/g;
345
+ for (const m of code.matchAll(progRe)) {
346
+ const at = m.index + m[0].length;
347
+ const view = code.slice(at).match(/^([A-Z]\w*)\s*\(/)?.[1] ?? null;
348
+ programmatic.push({
349
+ source: from(m.index),
350
+ route: view ? null : routeRefAt(code, at),
351
+ target: view,
352
+ line: lineAt(m.index),
353
+ text: snippet(lit, m.index),
354
+ });
355
+ }
356
+ return { types, refs, entries, programmatic, literals: literalsOf(lit, QUOTES.ios) };
357
+ }
358
+
359
+ /** `Type.case`, `.case` or `Type.case(...)` at offset, as {type, name}. */
360
+ function routeRefAt(code, at) {
361
+ const m = code.slice(at, at + 120).match(/^(?:([A-Z][\w]*)\s*)?\.\s*([a-z][A-Za-z0-9_]*)\b/);
362
+ return m ? { type: m[1] || null, name: m[2] } : null;
363
+ }
364
+
365
+ // ---------------------------------------------------------------- Compose --
366
+
367
+ /** Per-file Compose facts. */
368
+ export function extractCompose(text) {
369
+ const { lit, code } = scanSource(text, QUOTES.android);
370
+ const lineAt = lineCounter(text);
371
+ const composables = [];
372
+ const funRe =
373
+ /@Composable\s+(?:(?:private|internal|public|inline)\s+)*fun\s+(?:[\w.<>]+\.)?([A-Z][A-Za-z0-9_]*)\s*\(/g;
374
+ for (const m of code.matchAll(funRe)) {
375
+ const body = bodyAfterParams(code, m.index + m[0].length - 1);
376
+ composables.push({
377
+ name: m[1],
378
+ start: m.index,
379
+ end: body ? body[1] : m.index + m[0].length,
380
+ line: lineAt(m.index),
381
+ calls: body ? callsIn(code, body[0] + 1, body[1]).map((c) => c.name) : [],
382
+ });
383
+ }
384
+ const routeTypes = [];
385
+ for (const m of code.matchAll(
386
+ /@Serializable\s+(?:(?:data|internal|private)\s+)*(?:object|class)\s+([A-Z][A-Za-z0-9_]*)/g,
387
+ )) {
388
+ routeTypes.push({ name: m[1], line: lineAt(m.index) });
389
+ }
390
+ const registrations = [];
391
+ const regRe =
392
+ /\b(composable|entry|dialog|bottomSheet)\s*(?:<\s*([A-Z][\w.]*)\s*>|\(\s*(?:route\s*=\s*)?"([^"]*)")/g;
393
+ for (const m of lit.matchAll(regRe)) {
394
+ if (code.slice(m.index, m.index + m[1].length) !== m[1]) continue;
395
+ const head = m.index + m[1].length;
396
+ const block = closureAfter(code, head);
397
+ const first = block && callsIn(code, block[0] + 1, block[1])[0];
398
+ registrations.push({
399
+ route: m[2] ? m[2].split(".").pop() : m[3],
400
+ typed: Boolean(m[2]),
401
+ target: first ? first.name : null,
402
+ start: block ? block[0] : m.index,
403
+ end: block ? block[1] : m.index,
404
+ line: lineAt(m.index),
405
+ });
406
+ }
407
+ const starts = [];
408
+ for (const m of lit.matchAll(/\bstartDestination\s*=\s*(?:"([^"]*)"|([A-Z]\w*))/g)) {
409
+ starts.push(m[1] ?? m[2]);
410
+ }
411
+ const programmatic = [];
412
+ for (const m of lit.matchAll(
413
+ /(?<!fun\s+)\.?\bnavigate\s*\(\s*(?:route\s*=\s*)?|\bbackStack\.add\s*\(\s*/g,
414
+ )) {
415
+ if (code.slice(m.index, m.index + 9) !== lit.slice(m.index, m.index + 9)) continue;
416
+ const at = m.index + m[0].length;
417
+ const str = lit.slice(at).match(/^"([^"]*)"/);
418
+ const typ = code.slice(at).match(/^([A-Z]\w*)/);
419
+ const reg = registrations.find((r) => r.start <= m.index && m.index < r.end);
420
+ const fn = innermost(composables, m.index);
421
+ programmatic.push({
422
+ inRegistration: reg ? reg.route : null,
423
+ source: fn ? fn.name : null,
424
+ routeLiteral: str ? str[1] : null,
425
+ routeType: str ? null : typ ? typ[1] : null,
426
+ line: lineAt(m.index),
427
+ text: snippet(lit, m.index),
428
+ });
429
+ }
430
+ const endpoints = [];
431
+ for (const m of lit.matchAll(
432
+ /@(GET|POST|PUT|DELETE|PATCH|HEAD)\s*\(\s*(?:value\s*=\s*)?"([^"]*)"\s*\)/g,
433
+ )) {
434
+ endpoints.push({ method: m[1], path: m[2], line: lineAt(m.index) });
435
+ }
436
+ const stringRefs = new Set();
437
+ for (const m of code.matchAll(/\bR\.(?:string|plurals)\.([A-Za-z0-9_]+)/g)) stringRefs.add(m[1]);
438
+ return {
439
+ composables,
440
+ routeTypes,
441
+ registrations,
442
+ starts,
443
+ programmatic,
444
+ endpoints,
445
+ stringRefs,
446
+ literals: literalsOf(lit, QUOTES.android),
447
+ };
448
+ }
449
+
450
+ // -------------------------------------------------------------------- Web --
451
+
452
+ const PAGE_EXT = /\.(?:tsx|jsx|ts|js|mdx)$/;
453
+
454
+ /**
455
+ * The route a Next.js app/ or pages/ file serves, or null when it serves
456
+ * none (layouts, components, _app, API handlers).
457
+ *
458
+ * @param {string} path
459
+ * @returns {string|null}
460
+ */
461
+ export function nextRouteFromPath(path) {
462
+ const app = path.match(/(?:^|\/)app\/((?:.*\/)?)page\.(?:tsx|jsx|ts|js|mdx)$/);
463
+ if (app) return "/" + routeSegments(app[1]).join("/");
464
+ const pages = path.match(/(?:^|\/)pages\/(.*)$/);
465
+ if (!pages || !PAGE_EXT.test(pages[1])) return null;
466
+ const segs = pages[1].replace(PAGE_EXT, "").split("/");
467
+ if (segs[0] === "api" || segs.some((s) => s.startsWith("_"))) return null;
468
+ if (segs[segs.length - 1] === "index") segs.pop();
469
+ return "/" + segs.join("/");
470
+ }
471
+
472
+ function routeSegments(dir) {
473
+ return dir
474
+ .split("/")
475
+ .filter(Boolean)
476
+ .filter((s) => !/^\(.*\)$/.test(s) && !s.startsWith("@"));
477
+ }
478
+
479
+ /** The API path an app router route.ts serves, or null. */
480
+ export function nextApiFromPath(path) {
481
+ const app = path.match(/(?:^|\/)app\/((?:.*\/)?)route\.(?:ts|js|tsx|jsx)$/);
482
+ if (app) return "/" + routeSegments(app[1]).join("/");
483
+ const pages = path.match(/(?:^|\/)pages\/(api\/.*)\.(?:ts|js|tsx|jsx)$/);
484
+ if (pages) return "/" + pages[1].replace(/\/index$/, "");
485
+ return null;
486
+ }
487
+
488
+ /** Per-file web facts. */
489
+ export function extractWeb(text) {
490
+ const { lit, code } = scanSource(text, QUOTES.web);
491
+ const lineAt = lineCounter(text);
492
+ const components = [];
493
+ for (const m of code.matchAll(
494
+ /(?:^|[^\w$.])(?:export\s+(?:default\s+)?)?(?:async\s+)?function\s+([A-Z][A-Za-z0-9_]*)\s*\(/g,
495
+ )) {
496
+ components.push({ name: m[1], line: lineAt(m.index) });
497
+ }
498
+ for (const m of code.matchAll(
499
+ /\bconst\s+([A-Z][A-Za-z0-9_]*)\s*(?::[^=\n]+)?=\s*(?:[\w.]+\()?\s*(?:async\s*)?(?:\([^)]*\)|[a-z]\w*)\s*=>/g,
500
+ )) {
501
+ components.push({ name: m[1], line: lineAt(m.index) });
502
+ }
503
+ const defaultExport =
504
+ code.match(/\bexport\s+default\s+(?:async\s+)?function\s+([A-Z][A-Za-z0-9_]*)/)?.[1] ??
505
+ code.match(/\bexport\s+default\s+([A-Z][A-Za-z0-9_]*)\s*;?\s*$/m)?.[1] ??
506
+ null;
507
+ const routes = [];
508
+ for (const m of lit.matchAll(/\bpath\s*:\s*(["'`])([^"'`]*)\1/g)) {
509
+ if (code[m.index] !== "p") continue;
510
+ const tail = code.slice(m.index, m.index + 400);
511
+ const stop = tail.slice(5).search(/\bpath\s*:/);
512
+ const seg = stop >= 0 ? tail.slice(0, stop + 5) : tail;
513
+ const t =
514
+ seg.match(/\belement\s*:\s*<\s*([A-Z]\w*)/)?.[1] ??
515
+ seg.match(/\b[Cc]omponent\s*:\s*([A-Z]\w*)/)?.[1] ??
516
+ null;
517
+ if (t) routes.push({ path: m[2], target: t, line: lineAt(m.index) });
518
+ }
519
+ for (const m of code.matchAll(/<Route\b/g)) {
520
+ const next = code.indexOf("<Route", m.index + 6);
521
+ const end = Math.min(next < 0 ? code.length : next, m.index + 400);
522
+ const tagLit = lit.slice(m.index, end);
523
+ const tagCode = code.slice(m.index, end);
524
+ const p = tagLit.match(/\bpath\s*=\s*\{?\s*(["'`])([^"'`]*)\1/);
525
+ const t =
526
+ tagCode.match(/\belement\s*=\s*\{\s*<\s*([A-Z]\w*)/)?.[1] ??
527
+ tagCode.match(/\b[Cc]omponent\s*=\s*\{\s*([A-Z]\w*)/)?.[1] ??
528
+ null;
529
+ if (p && t) routes.push({ path: p[2], target: t, line: lineAt(m.index) });
530
+ }
531
+ const handlers = [];
532
+ for (const m of code.matchAll(
533
+ /\bexport\s+(?:async\s+function|function|const)\s+(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)\b/g,
534
+ )) {
535
+ handlers.push({ method: m[1], line: lineAt(m.index) });
536
+ }
537
+ const imports = [];
538
+ for (const m of lit.matchAll(/\b(?:from|import)\s*\(?\s*["'](\.{1,2}\/[^"']+)["']/g)) {
539
+ if (code[m.index] === lit[m.index]) imports.push(m[1]);
540
+ }
541
+ const programmatic = [];
542
+ for (const m of lit.matchAll(
543
+ /\b(?:router\.(?:push|replace)|navigate|redirect)\s*\(\s*|<Link\b[^>]*?\bhref\s*=\s*\{?\s*/g,
544
+ )) {
545
+ if (code[m.index] !== lit[m.index]) continue;
546
+ const at = m.index + m[0].length;
547
+ const s = lit.slice(at).match(/^(["'`])([^"'`]*)\1/);
548
+ programmatic.push({
549
+ path: s ? s[2] : null,
550
+ line: lineAt(m.index),
551
+ text: snippet(lit, m.index),
552
+ });
553
+ }
554
+ return {
555
+ components,
556
+ defaultExport,
557
+ routes,
558
+ handlers,
559
+ imports,
560
+ programmatic,
561
+ literals: literalsOf(lit, QUOTES.web),
562
+ };
563
+ }
564
+
565
+ // ---------------------------------------------------------------- strings --
566
+
567
+ const LOCALE_RE = /^[a-z]{2,3}(?:[-_][A-Za-z]{2,4})?$/;
568
+
569
+ /**
570
+ * Text from a file's bytes. Xcode writes .strings files as UTF-16 as often as
571
+ * UTF-8, so a byte-order mark picks the encoding, and a buffer without one
572
+ * whose odd bytes are mostly NUL is read as UTF-16 little-endian.
573
+ *
574
+ * @param {Buffer} buf
575
+ * @returns {string}
576
+ */
577
+ export function decodeText(buf) {
578
+ if (buf.length >= 2 && buf[0] === 0xff && buf[1] === 0xfe) {
579
+ return buf.subarray(2).toString("utf16le");
580
+ }
581
+ if (buf.length >= 2 && buf[0] === 0xfe && buf[1] === 0xff) {
582
+ return Buffer.from(buf.subarray(2)).swap16().toString("utf16le");
583
+ }
584
+ if (buf.length >= 3 && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) {
585
+ return buf.subarray(3).toString("utf8");
586
+ }
587
+ const sample = Math.min(buf.length - (buf.length % 2), 512);
588
+ if (sample >= 4) {
589
+ let oddNul = 0;
590
+ let evenNul = 0;
591
+ for (let i = 0; i < sample; i += 2) {
592
+ if (buf[i] === 0) evenNul++;
593
+ if (buf[i + 1] === 0) oddNul++;
594
+ }
595
+ if (oddNul * 4 >= sample && evenNul === 0) {
596
+ return buf.subarray(0, buf.length - (buf.length % 2)).toString("utf16le");
597
+ }
598
+ }
599
+ return buf.toString("utf8");
600
+ }
601
+
602
+ /** True for a file that holds localized strings. */
603
+ export function isStringsFile(path) {
604
+ if (/\.xcstrings$/.test(path)) return true;
605
+ if (/\.strings$/.test(path)) return true;
606
+ if (/(?:^|\/)res\/values[^/]*\/[^/]*strings[^/]*\.xml$/.test(path)) return true;
607
+ if (!path.endsWith(".json")) return false;
608
+ const parts = path.split("/");
609
+ const name = parts[parts.length - 1].replace(/\.json$/, "");
610
+ const dirs = parts.slice(0, -1).map((d) => d.toLowerCase());
611
+ const i18nDir = dirs.some((d) =>
612
+ ["locales", "locale", "i18n", "lang", "langs", "messages", "translations", "intl"].includes(d),
613
+ );
614
+ return i18nDir && (LOCALE_RE.test(name) || dirs.some((d) => LOCALE_RE.test(d)));
615
+ }
616
+
617
+ /** The locale a strings file is written in. */
618
+ export function localeFromPath(path) {
619
+ const lproj = path.match(/(?:^|\/)([^/]+)\.lproj\//);
620
+ if (lproj) return lproj[1];
621
+ const values = path.match(/(?:^|\/)res\/values(?:-([^/]+))?\//);
622
+ if (values) return values[1] ? values[1].replace(/-r([A-Z]{2})$/, "-$1") : "default";
623
+ const parts = path.split("/");
624
+ const name = parts[parts.length - 1].replace(/\.[^.]+$/, "");
625
+ if (LOCALE_RE.test(name)) return name;
626
+ const dir = parts
627
+ .slice(0, -1)
628
+ .reverse()
629
+ .find((d) => LOCALE_RE.test(d));
630
+ return dir || "default";
631
+ }
632
+
633
+ const unescapeC = (s) =>
634
+ s.replace(/\\(["'\\nt])/g, (_, c) => (c === "n" ? "\n" : c === "t" ? "\t" : c));
635
+
636
+ function decodeXml(s) {
637
+ return s
638
+ .replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, "$1")
639
+ .replace(/<[^>]+>/g, "")
640
+ .replace(/&lt;/g, "<")
641
+ .replace(/&gt;/g, ">")
642
+ .replace(/&quot;/g, '"')
643
+ .replace(/&apos;/g, "'")
644
+ .replace(/&#(\d+);/g, (_, n) => String.fromCharCode(Number(n)))
645
+ .replace(/&amp;/g, "&")
646
+ .replace(/\\(['"@?])/g, "$1")
647
+ .replace(/\\n/g, "\n")
648
+ .trim();
649
+ }
650
+
651
+ /**
652
+ * Rows of {key, value, locale} from any supported strings file.
653
+ *
654
+ * @param {string} text
655
+ * @param {string} path
656
+ * @returns {Array<{key: string, value: string, locale: string}>}
657
+ */
658
+ export function parseStringsFile(text, path) {
659
+ if (path.endsWith(".xcstrings")) return parseXcstrings(text);
660
+ const locale = localeFromPath(path);
661
+ if (path.endsWith(".strings")) {
662
+ const clean = stripCommentsKeepStrings(text, ['"']);
663
+ return [...clean.matchAll(/"((?:[^"\\]|\\.)*)"\s*=\s*"((?:[^"\\]|\\.)*)"\s*;/g)].map((m) => ({
664
+ key: unescapeC(m[1]),
665
+ value: unescapeC(m[2]),
666
+ locale,
667
+ }));
668
+ }
669
+ if (path.endsWith(".xml")) {
670
+ const rows = [];
671
+ const re =
672
+ /<string\s+[^>]*?name="([^"]+)"[^>]*>([\s\S]*?)<\/string>|<plurals\s+[^>]*?name="([^"]+)"[^>]*>([\s\S]*?)<\/plurals>/g;
673
+ for (const m of text.replace(/<!--[\s\S]*?-->/g, "").matchAll(re)) {
674
+ if (m[1]) {
675
+ rows.push({ key: m[1], value: decodeXml(m[2]), locale });
676
+ continue;
677
+ }
678
+ const items = [...m[4].matchAll(/<item\s+quantity="([^"]+)"[^>]*>([\s\S]*?)<\/item>/g)];
679
+ const pick = items.find((x) => x[1] === "other") || items[0];
680
+ if (pick) rows.push({ key: m[3], value: decodeXml(pick[2]), locale });
681
+ }
682
+ return rows;
683
+ }
684
+ let data;
685
+ try {
686
+ data = JSON.parse(text);
687
+ } catch {
688
+ return [];
689
+ }
690
+ const rows = [];
691
+ const walk = (obj, prefix) => {
692
+ for (const [k, v] of Object.entries(obj || {})) {
693
+ const key = prefix ? `${prefix}.${k}` : k;
694
+ if (typeof v === "string") rows.push({ key, value: v, locale });
695
+ else if (v && typeof v === "object" && !Array.isArray(v)) walk(v, key);
696
+ }
697
+ };
698
+ walk(data, "");
699
+ return rows;
700
+ }
701
+
702
+ function parseXcstrings(text) {
703
+ let data;
704
+ try {
705
+ data = JSON.parse(text);
706
+ } catch {
707
+ return [];
708
+ }
709
+ const source = data.sourceLanguage || "en";
710
+ const rows = [];
711
+ for (const [key, entry] of Object.entries(data.strings || {})) {
712
+ const locs = entry?.localizations || {};
713
+ const names = Object.keys(locs);
714
+ for (const loc of names) {
715
+ const l = locs[loc];
716
+ const value =
717
+ l?.stringUnit?.value ??
718
+ l?.variations?.plural?.other?.stringUnit?.value ??
719
+ Object.values(l?.variations?.plural || {})[0]?.stringUnit?.value;
720
+ if (typeof value === "string") rows.push({ key, value, locale: loc });
721
+ }
722
+ const identifierLike = /^[\w.-]+$/.test(key) && /[._]/.test(key);
723
+ if (!names.length && !identifierLike) rows.push({ key, value: key, locale: source });
724
+ }
725
+ return rows;
726
+ }
727
+
728
+ /** Contents of string literals with no interpolation, as a Set. */
729
+ function literalsOf(lit, quotes) {
730
+ const out = new Set();
731
+ const qs = quotes.filter((q) => q.length === 1);
732
+ const re = new RegExp(
733
+ qs.map((q) => `${q === "`" ? "`" : q}((?:[^${q}\\\\\\n]|\\\\.){1,200})${q}`).join("|"),
734
+ "g",
735
+ );
736
+ for (const m of lit.matchAll(re)) {
737
+ const v = m.slice(1).find((g) => g !== undefined);
738
+ if (v !== undefined) out.add(v);
739
+ }
740
+ return out;
741
+ }
742
+
743
+ // -------------------------------------------------------------- endpoints --
744
+
745
+ export const API_LITERAL = /^\/(?:api|v\d+|rest|graphql)(?:\/[^\s"'`?#]*)?$/;
746
+
747
+ /** Normalise an endpoint or route path so templated segments compare equal. */
748
+ export function pathPattern(p) {
749
+ return String(p)
750
+ .replace(/^https?:\/\/[^/]+/, "")
751
+ .replace(/\?.*$/, "")
752
+ .replace(/^\/?/, "/")
753
+ .split("/")
754
+ .map((s) => (/^(?:\{[^}]*\}|\[[^\]]*\]|:\w+|\$\{[^}]*\}|\$\w+|\{\})$/.test(s) ? "*" : s))
755
+ .join("/")
756
+ .replace(/\/+$/, "");
757
+ }
758
+
759
+ export function templateLiteralPath(s) {
760
+ return s.replace(/\$\{[^}]*\}/g, "{}");
761
+ }