weavatrix 0.2.13 → 0.2.15

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 (145) hide show
  1. package/README.md +99 -39
  2. package/SECURITY.md +2 -2
  3. package/docs/releases/v0.2.15.md +37 -0
  4. package/package.json +2 -2
  5. package/skill/SKILL.md +57 -38
  6. package/src/analysis/architecture/contract-graph.js +119 -0
  7. package/src/analysis/architecture/contract-schema.js +110 -0
  8. package/src/analysis/architecture/contract-storage.js +35 -0
  9. package/src/analysis/architecture/contract-verification.js +168 -0
  10. package/src/analysis/architecture-contract.js +16 -343
  11. package/src/analysis/change-classification/diff-parser.js +103 -0
  12. package/src/analysis/change-classification/options.js +40 -0
  13. package/src/analysis/change-classification/symbol-classifier.js +177 -0
  14. package/src/analysis/change-classification.js +120 -519
  15. package/src/analysis/dead-code-review/policy.js +23 -0
  16. package/src/analysis/dead-code-review.js +9 -19
  17. package/src/analysis/dep-check.js +14 -71
  18. package/src/analysis/dep-rules.js +10 -303
  19. package/src/analysis/dependency/conventions.js +16 -0
  20. package/src/analysis/dependency/scoped-dependencies.js +45 -0
  21. package/src/analysis/dependency/source-references.js +21 -0
  22. package/src/analysis/duplicates.tokenize.js +12 -2
  23. package/src/analysis/endpoints/common.js +62 -0
  24. package/src/analysis/endpoints/extract.js +107 -0
  25. package/src/analysis/endpoints/inventory.js +84 -0
  26. package/src/analysis/endpoints/mounts.js +129 -0
  27. package/src/analysis/endpoints-java.js +80 -3
  28. package/src/analysis/endpoints.js +3 -448
  29. package/src/analysis/git-history/analytics.js +177 -0
  30. package/src/analysis/git-history/collector.js +109 -0
  31. package/src/analysis/git-history/options.js +68 -0
  32. package/src/analysis/git-history.js +128 -577
  33. package/src/analysis/health-capabilities.js +82 -0
  34. package/src/analysis/http-contracts/analysis.js +169 -0
  35. package/src/analysis/http-contracts/client-call-detection.js +81 -0
  36. package/src/analysis/http-contracts/client-call-parser.js +255 -0
  37. package/src/analysis/http-contracts/graph-context.js +143 -0
  38. package/src/analysis/http-contracts/matching.js +61 -0
  39. package/src/analysis/http-contracts/shared.js +86 -0
  40. package/src/analysis/http-contracts.js +6 -822
  41. package/src/analysis/internal-audit/dependency-health.js +111 -0
  42. package/src/analysis/internal-audit/python-manifests.js +65 -0
  43. package/src/analysis/internal-audit/repo-files.js +55 -0
  44. package/src/analysis/internal-audit/supply-chain.js +132 -0
  45. package/src/analysis/internal-audit.collect.js +5 -106
  46. package/src/analysis/internal-audit.reach.js +20 -0
  47. package/src/analysis/internal-audit.run.js +114 -200
  48. package/src/analysis/jvm-dependency-evidence.js +69 -0
  49. package/src/analysis/source-correctness/retry-patterns.js +93 -0
  50. package/src/analysis/source-correctness.js +225 -0
  51. package/src/analysis/structure/dependency-graph.js +156 -0
  52. package/src/analysis/structure/findings.js +142 -0
  53. package/src/analysis/task-retrieval.js +3 -14
  54. package/src/graph/builder/go-receiver-resolution.js +112 -0
  55. package/src/graph/builder/js/queries.js +48 -0
  56. package/src/graph/builder/lang-go.js +89 -4
  57. package/src/graph/builder/lang-js.js +4 -44
  58. package/src/graph/builder/pass2-resolution.js +68 -0
  59. package/src/graph/freshness-probe.js +2 -1
  60. package/src/graph/incremental-refresh.js +3 -2
  61. package/src/graph/internal-builder.build.js +22 -183
  62. package/src/graph/internal-builder.pass2.js +161 -0
  63. package/src/graph/internal-builder.resolvers.js +2 -105
  64. package/src/graph/resolvers/rust.js +117 -0
  65. package/src/mcp/actions/advisories.mjs +18 -0
  66. package/src/mcp/actions/graph-lifecycle.mjs +148 -0
  67. package/src/mcp/actions/graph-sync.mjs +195 -0
  68. package/src/mcp/actions/hosted-architecture.mjs +57 -0
  69. package/src/mcp/architecture-bootstrap.mjs +168 -0
  70. package/src/mcp/architecture-starter.mjs +234 -0
  71. package/src/mcp/catalog.mjs +20 -6
  72. package/src/mcp/evidence/bun-lock-graph.mjs +209 -0
  73. package/src/mcp/evidence/duplicate-member-order.mjs +8 -0
  74. package/src/mcp/evidence/package-graph-common.mjs +115 -0
  75. package/src/mcp/evidence/package-lock-graph.mjs +92 -0
  76. package/src/mcp/evidence-snapshot.duplicates.mjs +3 -7
  77. package/src/mcp/evidence-snapshot.package-graph.mjs +5 -474
  78. package/src/mcp/git-output.mjs +10 -0
  79. package/src/mcp/graph/context-core.mjs +111 -0
  80. package/src/mcp/graph/context-seeds.mjs +197 -0
  81. package/src/mcp/graph/context-state.mjs +86 -0
  82. package/src/mcp/graph/reverse-reach.mjs +42 -0
  83. package/src/mcp/graph/tools-core.mjs +143 -0
  84. package/src/mcp/graph/tools-query.mjs +223 -0
  85. package/src/mcp/graph-context.mjs +4 -496
  86. package/src/mcp/health/audit-format.mjs +172 -0
  87. package/src/mcp/health/audit.mjs +171 -0
  88. package/src/mcp/health/dead-code.mjs +110 -0
  89. package/src/mcp/health/duplicates.mjs +83 -0
  90. package/src/mcp/health/endpoints.mjs +42 -0
  91. package/src/mcp/health/structure.mjs +219 -0
  92. package/src/mcp/runtime-version.mjs +32 -0
  93. package/src/mcp/server/auto-refresh.mjs +66 -0
  94. package/src/mcp/server/runtime-config.mjs +43 -0
  95. package/src/mcp/server/shutdown.mjs +40 -0
  96. package/src/mcp/sync/evidence-architecture.mjs +54 -0
  97. package/src/mcp/sync/evidence-common.mjs +88 -0
  98. package/src/mcp/sync/evidence-duplicates.mjs +97 -0
  99. package/src/mcp/sync/evidence-health.mjs +56 -0
  100. package/src/mcp/sync/evidence-packages.mjs +95 -0
  101. package/src/mcp/sync/payload-common.mjs +97 -0
  102. package/src/mcp/sync/payload-v2.mjs +53 -0
  103. package/src/mcp/sync/payload-v3.mjs +79 -0
  104. package/src/mcp/sync-evidence.mjs +7 -402
  105. package/src/mcp/sync-payload.mjs +10 -244
  106. package/src/mcp/tools-actions.mjs +18 -414
  107. package/src/mcp/tools-architecture.mjs +18 -70
  108. package/src/mcp/tools-context.mjs +56 -15
  109. package/src/mcp/tools-endpoints.mjs +4 -1
  110. package/src/mcp/tools-graph.mjs +17 -331
  111. package/src/mcp/tools-health.mjs +24 -705
  112. package/src/mcp/tools-impact-change.mjs +2 -38
  113. package/src/mcp/tools-impact.mjs +2 -43
  114. package/src/mcp-server.mjs +35 -146
  115. package/src/path-classification.js +14 -0
  116. package/src/precision/lsp-client/constants.js +12 -0
  117. package/src/precision/lsp-client/environment.js +20 -0
  118. package/src/precision/lsp-client/errors.js +15 -0
  119. package/src/precision/lsp-client/lifecycle.js +127 -0
  120. package/src/precision/lsp-client/message-parser.js +81 -0
  121. package/src/precision/lsp-client/protocol.js +212 -0
  122. package/src/precision/lsp-client/registry.js +58 -0
  123. package/src/precision/lsp-client/repo-uri.js +60 -0
  124. package/src/precision/lsp-client/stdio-client.js +151 -0
  125. package/src/precision/lsp-client.js +10 -682
  126. package/src/precision/lsp-overlay/build.js +238 -0
  127. package/src/precision/lsp-overlay/contract.js +61 -0
  128. package/src/precision/lsp-overlay/reference-results.js +108 -0
  129. package/src/precision/lsp-overlay/semantic-inputs.js +62 -0
  130. package/src/precision/lsp-overlay/source-session.js +134 -0
  131. package/src/precision/lsp-overlay/store.js +150 -0
  132. package/src/precision/lsp-overlay/target-index.js +147 -0
  133. package/src/precision/lsp-overlay/target-query.js +55 -0
  134. package/src/precision/lsp-overlay.js +11 -868
  135. package/src/precision/typescript-lsp-provider.js +12 -682
  136. package/src/precision/typescript-provider/client.js +69 -0
  137. package/src/precision/typescript-provider/discovery.js +124 -0
  138. package/src/precision/typescript-provider/project-host.js +249 -0
  139. package/src/precision/typescript-provider/project-safety.js +161 -0
  140. package/src/precision/typescript-provider/reference-usage.js +53 -0
  141. package/src/security/malware-heuristics.sweep.js +1 -1
  142. package/src/security/registry-sig.classify.js +2 -1
  143. package/src/security/registry-sig.rules.js +3 -3
  144. package/src/version.js +7 -0
  145. package/docs/releases/v0.2.13.md +0 -48
@@ -149,11 +149,17 @@ export function extractLargeStrings(text, { py = false, cs = false, minLines = 6
149
149
 
150
150
  // one raw token stream; the two modes differ only in identifier canonicalization
151
151
  export function tokenize(text) {
152
- const raw = text.match(/[A-Za-z_$][\w$]*|\d+(?:\.\d+)?|[^\s\w]/g) || [];
152
+ const raw = text.match(/[A-Za-z_$][\w$]*|(?:0[xX][\dA-Fa-f_]+|0[bB][01_]+|0[oO][0-7_]+|\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d[\d_]*)?)n?|[^\s\w]/g) || [];
153
153
  const strict = [];
154
154
  const renamed = [];
155
155
  for (const t of raw) {
156
- if (/^\d/.test(t)) { strict.push("N"); renamed.push("N"); continue; }
156
+ if (/^\d/.test(t)) {
157
+ // Numeric constants are behavior, not renameable identifiers. Preserve
158
+ // their value so same-shape limit/config tables with different numbers do
159
+ // not become perfect clones; a small constant edit still leaves the rest
160
+ // of a genuine copied body highly similar.
161
+ strict.push(`N:${t}`); renamed.push(`N:${t}`); continue;
162
+ }
157
163
  if (/^[A-Za-z_$]/.test(t) && !KEYWORDS.has(t)) { strict.push(t); renamed.push("I"); continue; }
158
164
  strict.push(t); renamed.push(t);
159
165
  }
@@ -178,5 +184,9 @@ export function fingerprints(toks) {
178
184
  for (let j = i; j < Math.min(i + W, hashes.length); j++) if (hashes[j] < min) min = hashes[j];
179
185
  fp.add(min);
180
186
  }
187
+ // Winnowing can skip every changed literal in a repetitive policy table.
188
+ // Exact numeric anchors keep behavior-defining limits in the similarity
189
+ // denominator while leaving ordinary copied bodies tolerant of a small edit.
190
+ for (const token of toks) if (token.startsWith("N:")) fp.add(`numeric:${token}`);
181
191
  return fp;
182
192
  }
@@ -0,0 +1,62 @@
1
+ export const MAX_ENDPOINT_FILES = 3_000;
2
+ export const MAX_ENDPOINTS = 2_000;
3
+ export const HTTP_METHODS = new Set(["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "TRACE", "CONNECT", "ALL", "ANY"]);
4
+
5
+ export function lineAt(text, index) {
6
+ let line = 1;
7
+ for (let i = 0; i < index && i < text.length; i++) if (text[i] === "\n") line++;
8
+ return line;
9
+ }
10
+
11
+ // Preserve offsets and literals while hiding comments from regex extractors.
12
+ export function maskComments(text, { hashComments = false } = {}) {
13
+ const chars = String(text || "").split("");
14
+ let quote = "", escaped = false, lineComment = false, blockComment = false;
15
+ for (let i = 0; i < chars.length; i++) {
16
+ const ch = chars[i], next = chars[i + 1];
17
+ if (lineComment) {
18
+ if (ch === "\n" || ch === "\r") lineComment = false;
19
+ else chars[i] = " ";
20
+ continue;
21
+ }
22
+ if (blockComment) {
23
+ if (ch === "*" && next === "/") { chars[i] = chars[i + 1] = " "; i++; blockComment = false; }
24
+ else if (ch !== "\n" && ch !== "\r") chars[i] = " ";
25
+ continue;
26
+ }
27
+ if (quote) {
28
+ if (escaped) escaped = false;
29
+ else if (ch === "\\") escaped = true;
30
+ else if (ch === quote) quote = "";
31
+ continue;
32
+ }
33
+ if (ch === '"' || ch === "'" || ch === "`") { quote = ch; continue; }
34
+ if (ch === "/" && next === "/") { chars[i] = chars[i + 1] = " "; i++; lineComment = true; continue; }
35
+ if (ch === "/" && next === "*") { chars[i] = chars[i + 1] = " "; i++; blockComment = true; continue; }
36
+ if (hashComments && ch === "#") { chars[i] = " "; lineComment = true; }
37
+ }
38
+ return chars.join("");
39
+ }
40
+
41
+ export function handlerName(expr) {
42
+ const source = String(expr || "").trim();
43
+ if (!source || /=>/.test(source) || /^\s*(async\s+)?function\b/.test(source) || /^\s*(?:async\s+)?(?:move\s+)?\|[^|]*\|/.test(source)) return "";
44
+ const turbofish = /(?:^|::)([A-Za-z_][\w]*)\s*::<[\s\S]*>\s*$/.exec(source);
45
+ if (turbofish) return turbofish[1];
46
+ const identifiers = source.match(/[A-Za-z_$][\w$]*/g);
47
+ if (!identifiers) return "";
48
+ const skipped = new Set(["async", "function", "await", "req", "res", "ctx", "request", "response", "next", "return"]);
49
+ for (let i = identifiers.length - 1; i >= 0; i--) if (!skipped.has(identifiers[i])) return identifiers[i];
50
+ return "";
51
+ }
52
+
53
+ export function handlerReference(expr) {
54
+ const source = String(expr || "").trim();
55
+ if (!source || /=>/.test(source) || /^\s*(async\s+)?function\b/.test(source)) return "";
56
+ const refs = [...source.matchAll(/([A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)+)/g)];
57
+ return refs.length ? refs.at(-1)[1].replace(/\s+/g, "") : "";
58
+ }
59
+
60
+ export const looksLikePath = (path) => typeof path === "string" && /^\/[\w\-./:{}*$?]*$/.test(path) && !path.includes("://");
61
+ export const cleanPath = (path) => String(path || "").replace(/\/+$/, "") || "/";
62
+ export const normalizedFile = (file) => String(file || "").replace(/\\/g, "/").replace(/^\.\//, "");
@@ -0,0 +1,107 @@
1
+ import { extractRustEndpoints } from "../endpoints-rust.js";
2
+ import { extractSpringEndpoints } from "../endpoints-java.js";
3
+ import { HTTP_METHODS, cleanPath, handlerName, handlerReference, lineAt, looksLikePath, maskComments } from "./common.js";
4
+
5
+ const HTTP_CLIENT_CALLER = /^(axios|https?|fetch|ky|got|superagent|needle|undici|xhr|\$http|http[Cc]lient|api[Cc]lient|rest[Cc]lient)$/;
6
+ const OPENAPI_BLOCK = /\boperationId\b|\bresponses\s*:|\brequestBody\b|\bschemaRef\b|\boperation\s*\(|\bsummary\s*:/;
7
+
8
+ export function nextRoutePath(file) {
9
+ const parts = String(file || "").replace(/\\/g, "/").split("/").filter(Boolean);
10
+ if (!/^route\.[cm]?[jt]s$/i.test(parts.at(-1) || "")) return "";
11
+ const appAt = parts.lastIndexOf("app");
12
+ if (appAt < 0) return "";
13
+ const route = [];
14
+ for (let segment of parts.slice(appAt + 1, -1)) {
15
+ if (!segment || /^\([^)]*\)$/.test(segment) || segment.startsWith("@")) continue;
16
+ segment = segment.replace(/^\((?:\.{1,3})\)/, "");
17
+ let match;
18
+ if ((match = /^\[\[\.\.\.([^\]]+)\]\]$/.exec(segment))) segment = `*${match[1]}?`;
19
+ else if ((match = /^\[\.\.\.([^\]]+)\]$/.exec(segment))) segment = `*${match[1]}`;
20
+ else if ((match = /^\[([^\]]+)\]$/.exec(segment))) segment = `:${match[1]}`;
21
+ if (segment) route.push(segment);
22
+ }
23
+ return `/${route.join("/")}`;
24
+ }
25
+
26
+ export function extractEndpointsFromText(text, file) {
27
+ const out = [];
28
+ const py = /\.py$/i.test(file), rust = /\.rs$/i.test(file), java = /\.java$/i.test(file);
29
+ const scanText = maskComments(text, { hashComments: py });
30
+ const add = (method, path, expr, index) => {
31
+ const normalizedPath = cleanPath(path);
32
+ const normalizedMethod = String(method || "ANY").toUpperCase();
33
+ if (!looksLikePath(normalizedPath) || !HTTP_METHODS.has(normalizedMethod)) return;
34
+ const handler = handlerName(expr);
35
+ const handlerRef = handlerReference(expr);
36
+ out.push({ method: normalizedMethod, path: normalizedPath, handler, ...(handlerRef ? { handlerRef } : {}), file, line: lineAt(text, index) });
37
+ };
38
+
39
+ const nextPath = nextRoutePath(file);
40
+ if (nextPath) {
41
+ const seen = new Set();
42
+ const direct = /\bexport\s+(?:(?:async|declare)\s+)*(?:function\s+|(?:const|let|var)\s+)(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b/g;
43
+ let match;
44
+ while ((match = direct.exec(scanText))) {
45
+ const method = match[1].toUpperCase();
46
+ if (!seen.has(method)) { seen.add(method); add(method, nextPath, method, match.index); }
47
+ }
48
+ const lists = /\bexport\s*\{([^}]+)\}/g;
49
+ while ((match = lists.exec(scanText))) {
50
+ for (const item of match[1].split(",")) {
51
+ const methodMatch = /^\s*([A-Za-z_$][\w$]*)(?:\s+as\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS))?\s*$/.exec(item);
52
+ if (!methodMatch || (!methodMatch[2] && !HTTP_METHODS.has(methodMatch[1]))) continue;
53
+ const method = String(methodMatch[2] || methodMatch[1]).toUpperCase();
54
+ if (HTTP_METHODS.has(method) && !seen.has(method)) { seen.add(method); add(method, nextPath, methodMatch[1], match.index); }
55
+ }
56
+ }
57
+ }
58
+
59
+ if (rust) extractRustEndpoints(scanText, add);
60
+ if (java) {
61
+ out.push(...extractSpringEndpoints(scanText, file));
62
+ return out;
63
+ }
64
+
65
+ const objKey = /(["'`])(\/[^"'`]*)\1\s*:\s*(\{)?/g;
66
+ let match;
67
+ while ((match = objKey.exec(scanText))) {
68
+ const path = match[2], keyIndex = match.index;
69
+ if (match[3]) {
70
+ let cursor = objKey.lastIndex, depth = 1;
71
+ const start = cursor;
72
+ while (cursor < scanText.length && depth > 0) {
73
+ if (scanText[cursor] === "{") depth++;
74
+ else if (scanText[cursor] === "}") depth--;
75
+ cursor++;
76
+ }
77
+ const body = scanText.slice(start, cursor - 1);
78
+ objKey.lastIndex = cursor;
79
+ if (OPENAPI_BLOCK.test(body)) continue;
80
+ const methodEntry = /\b(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b\s*:\s*([^,\n}]+)/gi;
81
+ let entry;
82
+ while ((entry = methodEntry.exec(body))) add(entry[1], path, entry[2], keyIndex);
83
+ } else {
84
+ const expression = /^([^,\n}]+)/.exec(scanText.slice(objKey.lastIndex, objKey.lastIndex + 200));
85
+ if (expression && !/^\s*(?:\{|["'`\[]|[-+]?\d|true\b|false\b|null\b|undefined\b)/i.test(expression[1])) add("ANY", path, expression[1], keyIndex);
86
+ }
87
+ }
88
+
89
+ const call = /(?<!@)\b([\w$]+)\s*\.\s*(get|post|put|patch|delete|head|options|all)\s*\(\s*(["'`])(\/[^"'`]*)\3\s*(?:,\s*([\s\S]{0,160}?))?\)/gi;
90
+ while ((match = call.exec(scanText))) {
91
+ const handler = String(match[5] || "").trim();
92
+ if (!HTTP_CLIENT_CALLER.test(match[1]) && handler && handler[0] !== "{") add(match[2], match[4], match[5], match.index);
93
+ }
94
+
95
+ const go = /\.\s*(?:HandleFunc|Handle)\s*\(\s*(["'`])(\/[^"'`]*)\1\s*,\s*([\s\S]{0,120}?)\)/g;
96
+ while ((match = go.exec(scanText))) add("ANY", match[2], match[3], match.index);
97
+
98
+ if (py || /\.(?:[cm]?[jt]sx?)$/i.test(file)) {
99
+ const decorator = /@[\w$]*\.?\s*(get|post|put|patch|delete|head|options)\s*\(\s*(["'`])(\/[^"'`]*)\2/gi;
100
+ while ((match = decorator.exec(scanText))) {
101
+ const after = scanText.slice(decorator.lastIndex, decorator.lastIndex + 200);
102
+ const fn = /\b(?:def|async\s+def|function|const|export\s+function)\s+([A-Za-z_$][\w$]*)/.exec(after);
103
+ add(match[1], match[3], fn ? fn[1] : "", match.index);
104
+ }
105
+ }
106
+ return out;
107
+ }
@@ -0,0 +1,84 @@
1
+ import { safeRead } from "../../util.js";
2
+ import { createRepoBoundary } from "../../repo-path.js";
3
+ import { MAX_ENDPOINT_FILES, MAX_ENDPOINTS, normalizedFile } from "./common.js";
4
+ import { extractEndpointsFromText, nextRoutePath } from "./extract.js";
5
+ import { joinEndpointPath, mountedBasePaths } from "./mounts.js";
6
+
7
+ const normParamKey = (path) => String(path).replace(/\{([^/}]+)\}/g, ":$1");
8
+ const sortEndpoints = (left, right) => left.path.localeCompare(right.path) || left.method.localeCompare(right.method);
9
+
10
+ function preferEndpoint(candidate, current) {
11
+ if (Boolean(candidate.handler) !== Boolean(current.handler)) return Boolean(candidate.handler);
12
+ return Number(candidate.path.includes("{")) < Number(current.path.includes("{"));
13
+ }
14
+
15
+ export function analyzeEndpointInventory(repoPath, codeFiles) {
16
+ const files = (codeFiles || []).slice(0, MAX_ENDPOINT_FILES);
17
+ const byKey = new Map();
18
+ const declarations = new Map();
19
+ const boundary = createRepoBoundary(repoPath);
20
+ const sources = new Map();
21
+ const eligibleFiles = [];
22
+ for (const file of files) {
23
+ const relative = normalizedFile(file.path || file);
24
+ if (!/\.(?:[cm]?[jt]sx?|py|go|rs|java)$/i.test(relative)) continue;
25
+ const resolved = boundary.resolve(relative);
26
+ if (!resolved.ok) continue;
27
+ sources.set(relative, safeRead(resolved.path) || "");
28
+ eligibleFiles.push(relative);
29
+ }
30
+ const mountAnalysis = mountedBasePaths(eligibleFiles, sources);
31
+ let truncated = false;
32
+ for (const relative of eligibleFiles) {
33
+ const text = sources.get(relative);
34
+ const routeSignal = /["'`]\/|\.(?:get|post|put|patch|delete)\s*\(|HandleFunc|@\w*\.?(?:get|post|put|patch|delete)|@(?:[\w$]+\.)*(?:Request|Get|Post|Put|Patch|Delete)Mapping\b/i;
35
+ if (!text || (!nextRoutePath(relative) && !routeSignal.test(text))) continue;
36
+ for (const declaration of extractEndpointsFromText(text, relative)) {
37
+ const declarationKey = `${declaration.file}\0${declaration.line}\0${declaration.method}\0${normParamKey(declaration.path)}`;
38
+ if (!declarations.has(declarationKey)) declarations.set(declarationKey, declaration);
39
+ const bases = mountAnalysis.paths.get(relative) || [{ path: "", chain: [] }];
40
+ for (const base of bases) {
41
+ const path = base.path ? joinEndpointPath(base.path, declaration.path) : declaration.path;
42
+ const endpoint = {
43
+ ...declaration,
44
+ declaredPath: declaration.path,
45
+ path,
46
+ mountState: base.chain.length ? "COMPOSED_STATIC" : "DECLARED_LOCAL",
47
+ confidence: base.chain.length ? "high" : "medium",
48
+ mountChain: base.chain,
49
+ ...(base.path ? { localPath: declaration.path } : {}),
50
+ };
51
+ const key = `${endpoint.method} ${normParamKey(endpoint.path)}`;
52
+ const current = byKey.get(key);
53
+ if (!current || preferEndpoint(endpoint, current)) byKey.set(key, endpoint);
54
+ if (byKey.size >= MAX_ENDPOINTS) { truncated = true; break; }
55
+ }
56
+ if (truncated) break;
57
+ }
58
+ if (truncated) break;
59
+ }
60
+ const endpoints = [...byKey.values()].sort(sortEndpoints);
61
+ const composed = endpoints.filter((endpoint) => endpoint.mountChain.length).length;
62
+ return {
63
+ endpoints,
64
+ declarations: [...declarations.values()].sort(sortEndpoints),
65
+ mounts: mountAnalysis.mounts,
66
+ stats: {
67
+ scannedFiles: eligibleFiles.length,
68
+ declaredRoutes: declarations.size,
69
+ emittedRoutes: endpoints.length,
70
+ reachableRoutes: composed,
71
+ reachableStaticRoutes: composed,
72
+ composedRoutes: composed,
73
+ localRoutes: endpoints.length - composed,
74
+ localDeclarations: endpoints.length - composed,
75
+ staticMounts: mountAnalysis.mounts.length,
76
+ truncated,
77
+ maxEndpoints: MAX_ENDPOINTS,
78
+ },
79
+ };
80
+ }
81
+
82
+ export function detectEndpoints(repoPath, codeFiles) {
83
+ return analyzeEndpointInventory(repoPath, codeFiles).endpoints;
84
+ }
@@ -0,0 +1,129 @@
1
+ import { posix } from "node:path";
2
+ import { cleanPath, lineAt, maskComments } from "./common.js";
3
+
4
+ const JS_ROUTE_EXTENSIONS = [".js", ".ts", ".tsx", ".jsx", ".cjs", ".mjs"];
5
+
6
+ function importBindings(text) {
7
+ const bindings = new Map();
8
+ const add = (name, specifier) => {
9
+ if (/^[A-Za-z_$][\w$]*$/.test(name || "") && typeof specifier === "string") bindings.set(name, specifier);
10
+ };
11
+ let match;
12
+ const direct = /\bimport\s+([A-Za-z_$][\w$]*)\s*(?:,\s*\{[^}]*\})?\s+from\s*(["'`])([^"'`]+)\2/g;
13
+ while ((match = direct.exec(text))) add(match[1], match[3]);
14
+ const namespace = /\bimport\s+\*\s+as\s+([A-Za-z_$][\w$]*)\s+from\s*(["'`])([^"'`]+)\2/g;
15
+ while ((match = namespace.exec(text))) add(match[1], match[3]);
16
+ const named = /\bimport\s*\{([^}]+)\}\s*from\s*(["'`])([^"'`]+)\2/g;
17
+ while ((match = named.exec(text))) {
18
+ for (const item of match[1].split(",")) {
19
+ const binding = /^\s*[A-Za-z_$][\w$]*(?:\s+as\s+([A-Za-z_$][\w$]*))?\s*$/.exec(item);
20
+ if (binding) add(binding[1] || item.trim(), match[3]);
21
+ }
22
+ }
23
+ const commonJs = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*require\s*\(\s*(["'`])([^"'`]+)\2\s*\)/g;
24
+ while ((match = commonJs.exec(text))) add(match[1], match[3]);
25
+ return bindings;
26
+ }
27
+
28
+ function resolveImportedFile(importer, specifier, availableFiles) {
29
+ if (!specifier?.startsWith(".")) return null;
30
+ const base = posix.normalize(posix.join(posix.dirname(importer), specifier));
31
+ const candidates = [base];
32
+ if (!JS_ROUTE_EXTENSIONS.some((extension) => base.endsWith(extension))) {
33
+ for (const extension of JS_ROUTE_EXTENSIONS) candidates.push(`${base}${extension}`);
34
+ for (const extension of JS_ROUTE_EXTENSIONS) candidates.push(`${base}/index${extension}`);
35
+ }
36
+ return candidates.find((candidate) => availableFiles.has(candidate)) || null;
37
+ }
38
+
39
+ function callArguments(text, openParen) {
40
+ let quote = "", escaped = false, depth = 0, start = openParen + 1;
41
+ const args = [];
42
+ for (let index = openParen; index < text.length; index++) {
43
+ const char = text[index];
44
+ if (quote) {
45
+ if (escaped) escaped = false;
46
+ else if (char === "\\") escaped = true;
47
+ else if (char === quote) quote = "";
48
+ continue;
49
+ }
50
+ if (char === '"' || char === "'" || char === "`") { quote = char; continue; }
51
+ if (char === "(") { depth++; continue; }
52
+ if (char === ")") {
53
+ depth--;
54
+ if (depth === 0) {
55
+ args.push(text.slice(start, index).trim());
56
+ return { args, end: index + 1 };
57
+ }
58
+ continue;
59
+ }
60
+ if (char === "," && depth === 1) {
61
+ args.push(text.slice(start, index).trim());
62
+ start = index + 1;
63
+ }
64
+ }
65
+ return null;
66
+ }
67
+
68
+ function routerMounts(text, file, availableFiles) {
69
+ const scanText = maskComments(text);
70
+ const bindings = importBindings(scanText);
71
+ const mounts = [];
72
+ const useCall = /\b[A-Za-z_$][\w$]*\s*\.\s*use\s*\(/g;
73
+ let match;
74
+ while ((match = useCall.exec(scanText))) {
75
+ const parsed = callArguments(scanText, scanText.indexOf("(", match.index));
76
+ if (!parsed) continue;
77
+ useCall.lastIndex = parsed.end;
78
+ const args = parsed.args.filter(Boolean);
79
+ if (!args.length) continue;
80
+ const literal = /^(["'`])(\/[^"'`]*)\1$/.exec(args[0]);
81
+ const mountPath = literal ? cleanPath(literal[2]) : "/";
82
+ const identifier = /^([A-Za-z_$][\w$]*)(?:\.[A-Za-z_$][\w$]*)?$/.exec(args.at(-1) || "")?.[1];
83
+ const child = resolveImportedFile(file, identifier ? bindings.get(identifier) : null, availableFiles);
84
+ if (child && child !== file) mounts.push({ parent: file, child, path: mountPath, line: lineAt(text, match.index) });
85
+ }
86
+ return mounts;
87
+ }
88
+
89
+ export function joinEndpointPath(base, route) {
90
+ const left = cleanPath(base || "/");
91
+ const right = cleanPath(route || "/");
92
+ if (left === "/") return right;
93
+ if (right === "/") return left;
94
+ return cleanPath(`${left}/${right.replace(/^\/+/, "")}`.replace(/\/{2,}/g, "/"));
95
+ }
96
+
97
+ export function mountedBasePaths(files, sources) {
98
+ const incoming = new Map();
99
+ const mounts = [];
100
+ const available = new Set(files);
101
+ for (const file of files) {
102
+ for (const mount of routerMounts(sources.get(file) || "", file, available)) {
103
+ mounts.push(mount);
104
+ if (!incoming.has(mount.child)) incoming.set(mount.child, []);
105
+ incoming.get(mount.child).push(mount);
106
+ }
107
+ }
108
+ const cache = new Map();
109
+ const resolve = (file, stack = new Set()) => {
110
+ if (cache.has(file)) return cache.get(file);
111
+ if (stack.has(file)) return [];
112
+ const parents = incoming.get(file) || [];
113
+ if (!parents.length) return [{ path: "", chain: [] }];
114
+ const paths = [];
115
+ for (const mount of parents) {
116
+ for (const base of resolve(mount.parent, new Set(stack).add(file))) {
117
+ const path = joinEndpointPath(base.path, mount.path);
118
+ const key = `${path}\0${base.chain.map((item) => `${item.file}:${item.line}:${item.path}`).join("|")}\0${mount.parent}:${mount.line}:${mount.path}`;
119
+ if (!paths.some((item) => item.key === key)) paths.push({ key, path, chain: [...base.chain, { file: mount.parent, line: mount.line, path: mount.path, child: mount.child }] });
120
+ if (paths.length >= 32) break;
121
+ }
122
+ if (paths.length >= 32) break;
123
+ }
124
+ const result = paths.length ? paths.map(({ key: _key, ...item }) => item) : [{ path: "", chain: [] }];
125
+ cache.set(file, result);
126
+ return result;
127
+ };
128
+ return { paths: new Map(files.map((file) => [file, resolve(file)])), mounts };
129
+ }
@@ -4,6 +4,7 @@
4
4
  import { maskJavaNonCode } from "./java-source.js";
5
5
 
6
6
  const SPRING_MAPPING = /@(?:org\.springframework\.web\.bind\.annotation\.)?(RequestMapping|GetMapping|PostMapping|PutMapping|PatchMapping|DeleteMapping)\b/g;
7
+ const SPRING_CONDITION = /@(?:org\.springframework\.boot\.autoconfigure\.condition\.)?(ConditionalOnExpression|ConditionalOnProperty)\b/g;
7
8
  const REQUEST_METHOD = /\bRequestMethod\s*\.\s*(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|TRACE)\b/g;
8
9
 
9
10
  const lineAt = (text, index) => {
@@ -119,6 +120,58 @@ function mappingMethods(name, args) {
119
120
  return methods.length ? methods : ["ANY"];
120
121
  }
121
122
 
123
+ function namedInitializer(args, name) {
124
+ const source = String(args || "");
125
+ const match = new RegExp(`\\b${name}\\s*=`).exec(source);
126
+ return match ? initializer(source, source.indexOf("=", match.index)) : "";
127
+ }
128
+
129
+ function namedStrings(args, name) {
130
+ return stringLiterals(namedInitializer(args, name));
131
+ }
132
+
133
+ function namedBoolean(args, name, fallback) {
134
+ const value = namedInitializer(args, name).trim();
135
+ if (/^true\b/i.test(value)) return true;
136
+ if (/^false\b/i.test(value)) return false;
137
+ return fallback;
138
+ }
139
+
140
+ function positionalStrings(args) {
141
+ const source = String(args || "");
142
+ const start = source.trimStart()[0];
143
+ return start === '"' || start === "{" ? stringLiterals(initializer(source, -1)) : [];
144
+ }
145
+
146
+ function expressionDefaultActive(expression) {
147
+ const value = String(expression || "").trim();
148
+ if (/^(?:true|false)$/i.test(value)) return value.toLowerCase() === "true";
149
+ const match = /^\$\{[^{}:]+:(true|false)\}$/i.exec(value);
150
+ return match ? match[1].toLowerCase() === "true" : null;
151
+ }
152
+
153
+ function conditionMetadata(annotation, args) {
154
+ if (annotation === "ConditionalOnExpression") {
155
+ const expression = namedStrings(args, "value")[0] || stringLiterals(args)[0] || "";
156
+ return {
157
+ type: annotation,
158
+ expression,
159
+ defaultActive: expressionDefaultActive(expression),
160
+ };
161
+ }
162
+ const namedProperties = [...namedStrings(args, "name"), ...namedStrings(args, "value")];
163
+ const properties = [...new Set(namedProperties.length ? namedProperties : positionalStrings(args))];
164
+ const matchIfMissing = namedBoolean(args, "matchIfMissing", false);
165
+ return {
166
+ type: annotation,
167
+ prefix: namedStrings(args, "prefix")[0] || "",
168
+ properties,
169
+ havingValue: namedStrings(args, "havingValue")[0] || "",
170
+ matchIfMissing,
171
+ defaultActive: matchIfMissing,
172
+ };
173
+ }
174
+
122
175
  function joinPaths(prefix, path) {
123
176
  const left = String(prefix || "").trim().replace(/^\/+|\/+$/g, "");
124
177
  const right = String(path || "").trim().replace(/^\/+|\/+$/g, "");
@@ -135,10 +188,10 @@ function declarationAfter(text, start) {
135
188
  if (classMatch) {
136
189
  const open = bounded.indexOf("{");
137
190
  const bodyOpen = open < 0 ? -1 : declarationStart + open;
138
- return { kind: "class", name: classMatch[2], bodyOpen, bodyClose: bodyOpen < 0 ? text.length : balancedEnd(text, bodyOpen, "{", "}") };
191
+ return { kind: "class", name: classMatch[2], start: declarationStart, bodyOpen, bodyClose: bodyOpen < 0 ? text.length : balancedEnd(text, bodyOpen, "{", "}") };
139
192
  }
140
193
  const methodMatch = /\b([A-Za-z_$][\w$]*)\s*\(/.exec(head);
141
- return methodMatch ? { kind: "method", name: methodMatch[1] } : { kind: "unknown", name: "" };
194
+ return methodMatch ? { kind: "method", name: methodMatch[1], start: declarationStart } : { kind: "unknown", name: "", start: declarationStart };
142
195
  }
143
196
 
144
197
  export function extractSpringEndpoints(text, file) {
@@ -160,6 +213,19 @@ export function extractSpringEndpoints(text, file) {
160
213
  });
161
214
  SPRING_MAPPING.lastIndex = Math.max(SPRING_MAPPING.lastIndex, invocation.end);
162
215
  }
216
+ const conditions = [];
217
+ SPRING_CONDITION.lastIndex = 0;
218
+ while ((match = SPRING_CONDITION.exec(code))) {
219
+ const invocation = annotationInvocation(text, SPRING_CONDITION.lastIndex);
220
+ const declaration = declarationAfter(code, invocation.end);
221
+ conditions.push({
222
+ index: match.index,
223
+ line: lineAt(text, match.index),
224
+ declaration,
225
+ ...conditionMetadata(match[1], invocation.args),
226
+ });
227
+ SPRING_CONDITION.lastIndex = Math.max(SPRING_CONDITION.lastIndex, invocation.end);
228
+ }
163
229
 
164
230
  const classMappings = mappings
165
231
  .filter((item) => item.declaration.kind === "class")
@@ -176,9 +242,20 @@ export function extractSpringEndpoints(text, file) {
176
242
  const prefixes = owner?.paths?.length ? owner.paths : [""];
177
243
  const paths = mappingPaths(item.args);
178
244
  if (!paths.length) continue; // literal path was requested but could not be resolved
245
+ const activationConditions = conditions.filter((condition) => condition.declaration.start === item.declaration.start
246
+ || (owner && condition.declaration.start === owner.declaration.start));
247
+ const activation = activationConditions.length
248
+ ? {
249
+ conditional: true,
250
+ defaultActive: activationConditions.some((condition) => condition.defaultActive === false)
251
+ ? false
252
+ : activationConditions.every((condition) => condition.defaultActive === true) ? true : null,
253
+ conditions: activationConditions.map(({ declaration: _declaration, index: _index, ...condition }) => condition),
254
+ }
255
+ : {};
179
256
  for (const method of mappingMethods(item.annotation, item.args)) {
180
257
  for (const prefix of prefixes) for (const path of paths) {
181
- out.push({ method, path: joinPaths(prefix, path), handler: item.declaration.name, file, line: item.line });
258
+ out.push({ method, path: joinPaths(prefix, path), handler: item.declaration.name, file, line: item.line, ...activation });
182
259
  }
183
260
  }
184
261
  }