@getchomp/mcp 0.0.0-stage → 0.2.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +15 -2
- package/dist/conventionsReference.d.ts +84 -0
- package/dist/conventionsReference.js +313 -0
- package/dist/designSystem/astScanner.d.ts +153 -0
- package/dist/designSystem/astScanner.js +2014 -0
- package/dist/designSystem/baseline.d.ts +65 -0
- package/dist/designSystem/baseline.js +136 -0
- package/dist/designSystem/color.d.ts +35 -0
- package/dist/designSystem/color.js +265 -0
- package/dist/designSystem/config.d.ts +183 -0
- package/dist/designSystem/config.js +469 -0
- package/dist/designSystem/contrast.d.ts +55 -0
- package/dist/designSystem/contrast.js +377 -0
- package/dist/designSystem/cssParser.d.ts +63 -0
- package/dist/designSystem/cssParser.js +469 -0
- package/dist/designSystem/merges.d.ts +57 -0
- package/dist/designSystem/merges.js +94 -0
- package/dist/designSystem/preprocessors.d.ts +46 -0
- package/dist/designSystem/preprocessors.js +1108 -0
- package/dist/designSystem/presentation.d.ts +45 -0
- package/dist/designSystem/presentation.js +796 -0
- package/dist/designSystem/styleVectors.d.ts +27 -0
- package/dist/designSystem/styleVectors.js +484 -0
- package/dist/designSystem/tokens.d.ts +39 -0
- package/dist/designSystem/tokens.js +409 -0
- package/dist/designSystem/topology.d.ts +64 -0
- package/dist/designSystem/topology.js +254 -0
- package/dist/designSystem/types.d.ts +857 -0
- package/dist/designSystem/types.js +113 -0
- package/dist/designSystem/typography.d.ts +25 -0
- package/dist/designSystem/typography.js +244 -0
- package/dist/designSystem/uiShapes.d.ts +74 -0
- package/dist/designSystem/uiShapes.js +285 -0
- package/dist/designSystem/valueClusters.d.ts +76 -0
- package/dist/designSystem/valueClusters.js +281 -0
- package/dist/designSystemPlugin.d.ts +321 -0
- package/dist/designSystemPlugin.js +4730 -0
- package/dist/enrichNodesPlugin.d.ts +160 -0
- package/dist/enrichNodesPlugin.js +957 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +1298 -0
- package/dist/intelligence.d.ts +502 -0
- package/dist/intelligence.js +3058 -0
- package/dist/pluginRunner.d.ts +364 -0
- package/dist/pluginRunner.js +1096 -0
- package/dist/plugins/design-system/index.d.ts +14 -0
- package/dist/plugins/design-system/index.js +241 -0
- package/dist/plugins/enrich-nodes/index.d.ts +388 -0
- package/dist/plugins/enrich-nodes/index.js +697 -0
- package/dist/plugins/index.d.ts +15 -0
- package/dist/plugins/index.js +20 -0
- package/dist/plugins/local-shims/index.d.ts +121 -0
- package/dist/plugins/local-shims/index.js +237 -0
- package/dist/shimsPlugin.d.ts +134 -0
- package/dist/shimsPlugin.js +1392 -0
- package/package.json +39 -3
|
@@ -0,0 +1,1392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* packages/mcp/src/shimsPlugin.ts
|
|
3
|
+
*
|
|
4
|
+
* User-Defined Local Graph Shims Plugin (`L1_SHIMS` / `source: 'local-shims'`)
|
|
5
|
+
* Enforces [PAT-DB-03], [PAT-DB-05], [PAT-VIS-15], [PAT-VIS-36], [PAT-MCP-09], [PAT-MCP-10].
|
|
6
|
+
*
|
|
7
|
+
* Sits cleanly as a Layer 1 plugin on top of Chomp's deterministic Layer 0 AST graph:
|
|
8
|
+
* - Layer 0 (`nodes` / `edges` in SQLite) remains 100% pure AST-evidenced facts.
|
|
9
|
+
* - Local shims live in `.chomp/shims.json` (and optional `chomp.config.json#shims`) and are
|
|
10
|
+
* evaluated by `localShimsPlugin` into `L1_SHIMS` `entity_overlays` carrying:
|
|
11
|
+
* 1. Structured `shimmedEdges` + `syntheticConnectors` consumed by `applyShimOverlaysToGraph`
|
|
12
|
+
* when `overlayLayers.L1_SHIMS` is enabled (Eye icon in HUD).
|
|
13
|
+
* 2. Full `PluginPresentation` v1 (`summary`, `metrics`, `views`, `prompt`, `sections`)
|
|
14
|
+
* for the HUD card, Package Inspector, and Node Inspector.
|
|
15
|
+
*/
|
|
16
|
+
import { createHash } from "node:crypto";
|
|
17
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { dirname, resolve } from "node:path";
|
|
19
|
+
import { CONNECTIVITY_TARGET_TYPES, PLUGIN_PRESENTATION_PAYLOAD_KEY, PLUGIN_PRESENTATION_VERSION, SHIM_OVERLAY_LAYER, applyShimOverlaysToGraph, capForPresentation, classifyCallEdgeResolution, computeActionableUnresolvedCallsByNode, stableEntityId, } from "@getchomp/core";
|
|
20
|
+
import { computeFileOverlayHash, getChompPlugin, loadPluginSettings, resolveRepoName, runChompPlugin, } from "./pluginRunner.js";
|
|
21
|
+
import { packageMatchesFilter } from "./intelligence.js";
|
|
22
|
+
export const LOCAL_SHIMS_PLUGIN_NAME = "local-shims";
|
|
23
|
+
export const LOCAL_SHIMS_FILE_REL = ".chomp/plugins/local-shims/shims.json";
|
|
24
|
+
export const LEGACY_LOCAL_SHIMS_FILE_REL = ".chomp/shims.json";
|
|
25
|
+
function isObj(v) {
|
|
26
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
27
|
+
}
|
|
28
|
+
function primaryEvidence(ev) {
|
|
29
|
+
if (!ev)
|
|
30
|
+
return undefined;
|
|
31
|
+
return Array.isArray(ev) ? ev[0] : ev;
|
|
32
|
+
}
|
|
33
|
+
function stripNodePrefix(name) {
|
|
34
|
+
const idx = name.indexOf(":");
|
|
35
|
+
return idx >= 0 ? name.slice(idx + 1).trim() : name.trim();
|
|
36
|
+
}
|
|
37
|
+
function normalizeCallToken(raw) {
|
|
38
|
+
return raw
|
|
39
|
+
.replace(/^Call:\s*/i, "")
|
|
40
|
+
.replace(/\(\)\s*$/, "")
|
|
41
|
+
.trim();
|
|
42
|
+
}
|
|
43
|
+
export function deriveShimId(input) {
|
|
44
|
+
if (typeof input.id === "string" && input.id.trim()) {
|
|
45
|
+
return input.id.trim();
|
|
46
|
+
}
|
|
47
|
+
const key = JSON.stringify({
|
|
48
|
+
src: input.sourceId ?? input.sourceName ?? "",
|
|
49
|
+
srcFile: input.sourceFile ?? "",
|
|
50
|
+
call: input.callName ?? "",
|
|
51
|
+
sk: input.shapeKey ?? "",
|
|
52
|
+
tgt: input.targetId ?? input.targetName ?? input.targetConnector?.name ?? "",
|
|
53
|
+
tgtFile: input.targetFile ?? "",
|
|
54
|
+
et: input.entityType ?? "CALL",
|
|
55
|
+
});
|
|
56
|
+
return `shim-${createHash("sha256").update(key).digest("hex").slice(0, 10)}`;
|
|
57
|
+
}
|
|
58
|
+
function normalizeShimEntry(raw, origin) {
|
|
59
|
+
if (!isObj(raw))
|
|
60
|
+
return null;
|
|
61
|
+
const input = {
|
|
62
|
+
...(typeof raw.id === "string" && raw.id.trim() ? { id: raw.id.trim() } : {}),
|
|
63
|
+
...(typeof raw.sourceId === "string" && raw.sourceId.trim() ? { sourceId: raw.sourceId.trim() } : {}),
|
|
64
|
+
...(typeof raw.sourceName === "string" && raw.sourceName.trim()
|
|
65
|
+
? { sourceName: raw.sourceName.trim() }
|
|
66
|
+
: {}),
|
|
67
|
+
...(typeof raw.sourceFile === "string" && raw.sourceFile.trim()
|
|
68
|
+
? { sourceFile: raw.sourceFile.trim().replace(/\\/g, "/") }
|
|
69
|
+
: {}),
|
|
70
|
+
...(typeof raw.callName === "string" && raw.callName.trim() ? { callName: raw.callName.trim() } : {}),
|
|
71
|
+
...(typeof raw.shapeKey === "string" && raw.shapeKey.trim() ? { shapeKey: raw.shapeKey.trim() } : {}),
|
|
72
|
+
...(typeof raw.targetId === "string" && raw.targetId.trim() ? { targetId: raw.targetId.trim() } : {}),
|
|
73
|
+
...(typeof raw.targetName === "string" && raw.targetName.trim()
|
|
74
|
+
? { targetName: raw.targetName.trim() }
|
|
75
|
+
: {}),
|
|
76
|
+
...(typeof raw.targetFile === "string" && raw.targetFile.trim()
|
|
77
|
+
? { targetFile: raw.targetFile.trim().replace(/\\/g, "/") }
|
|
78
|
+
: {}),
|
|
79
|
+
...(isObj(raw.targetConnector) &&
|
|
80
|
+
typeof raw.targetConnector.name === "string" &&
|
|
81
|
+
raw.targetConnector.name.trim()
|
|
82
|
+
? {
|
|
83
|
+
targetConnector: {
|
|
84
|
+
name: raw.targetConnector.name.trim(),
|
|
85
|
+
...(typeof raw.targetConnector.entityType === "string" &&
|
|
86
|
+
raw.targetConnector.entityType.trim()
|
|
87
|
+
? { entityType: raw.targetConnector.entityType.trim() }
|
|
88
|
+
: {}),
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
: {}),
|
|
92
|
+
...(typeof raw.entityType === "string" && raw.entityType.trim()
|
|
93
|
+
? { entityType: raw.entityType.trim() }
|
|
94
|
+
: {}),
|
|
95
|
+
...(typeof raw.edgeName === "string" && raw.edgeName.trim() ? { edgeName: raw.edgeName.trim() } : {}),
|
|
96
|
+
...(typeof raw.label === "string" && raw.label.trim() ? { label: raw.label.trim() } : {}),
|
|
97
|
+
...(typeof raw.rationale === "string" && raw.rationale.trim()
|
|
98
|
+
? { rationale: raw.rationale.trim() }
|
|
99
|
+
: {}),
|
|
100
|
+
...(typeof raw.enabled === "boolean" ? { enabled: raw.enabled } : {}),
|
|
101
|
+
};
|
|
102
|
+
if (!input.sourceId && !input.sourceName)
|
|
103
|
+
return null;
|
|
104
|
+
if (!input.targetId && !input.targetName && !input.targetConnector)
|
|
105
|
+
return null;
|
|
106
|
+
return {
|
|
107
|
+
...input,
|
|
108
|
+
id: deriveShimId(input),
|
|
109
|
+
origin,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export function loadLocalShimsFromTexts(shimsJsonText, chompConfigText) {
|
|
113
|
+
const shims = [];
|
|
114
|
+
const parseErrors = [];
|
|
115
|
+
const seenIds = new Set();
|
|
116
|
+
const parseArray = (rawList, origin) => {
|
|
117
|
+
for (const item of rawList) {
|
|
118
|
+
const parsed = normalizeShimEntry(item, origin);
|
|
119
|
+
if (!parsed) {
|
|
120
|
+
parseErrors.push(`${origin}: skipped malformed shim entry (requires sourceId|sourceName and targetId|targetName|targetConnector)`);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (seenIds.has(parsed.id))
|
|
124
|
+
continue;
|
|
125
|
+
seenIds.add(parsed.id);
|
|
126
|
+
shims.push(parsed);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
if (shimsJsonText !== null && shimsJsonText.trim()) {
|
|
130
|
+
try {
|
|
131
|
+
const parsed = JSON.parse(shimsJsonText);
|
|
132
|
+
const list = Array.isArray(parsed) ? parsed : isObj(parsed) && Array.isArray(parsed.shims) ? parsed.shims : null;
|
|
133
|
+
if (list)
|
|
134
|
+
parseArray(list, "shims.json");
|
|
135
|
+
else
|
|
136
|
+
parseErrors.push(`${LOCAL_SHIMS_FILE_REL}: expected an object with a "shims" array`);
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
parseErrors.push(`${LOCAL_SHIMS_FILE_REL}: invalid JSON (${err?.message ?? String(err)})`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (chompConfigText !== null && chompConfigText.trim()) {
|
|
143
|
+
try {
|
|
144
|
+
const parsed = JSON.parse(chompConfigText);
|
|
145
|
+
if (isObj(parsed) && Array.isArray(parsed.shims)) {
|
|
146
|
+
parseArray(parsed.shims, "chomp.config.json");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// Malformed chomp.config.json is reported elsewhere if needed; ignore silently here.
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { shims, parseErrors };
|
|
154
|
+
}
|
|
155
|
+
function buildOwningDuMap(nodes) {
|
|
156
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
157
|
+
const result = new Map();
|
|
158
|
+
for (const n of nodes) {
|
|
159
|
+
if (n.entityType === "DEPLOYMENT_UNIT") {
|
|
160
|
+
result.set(n.id, n);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
let cur = n;
|
|
164
|
+
let guard = 0;
|
|
165
|
+
while (cur?.parentBoundaryId && guard++ < 8) {
|
|
166
|
+
const parent = byId.get(cur.parentBoundaryId);
|
|
167
|
+
if (!parent)
|
|
168
|
+
break;
|
|
169
|
+
if (parent.entityType === "DEPLOYMENT_UNIT") {
|
|
170
|
+
result.set(n.id, parent);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
cur = parent;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
function matchNodeInGraph(nodes, id, name, fileFilter, allowOpenConnectors) {
|
|
179
|
+
const candidates = nodes.filter((n) => {
|
|
180
|
+
if ((n.scope ?? "USER") !== "USER")
|
|
181
|
+
return false;
|
|
182
|
+
if (n.entityType === "DEPLOYMENT_UNIT")
|
|
183
|
+
return false;
|
|
184
|
+
if (!allowOpenConnectors && n.type === "OPEN_CONNECTOR")
|
|
185
|
+
return false;
|
|
186
|
+
return true;
|
|
187
|
+
});
|
|
188
|
+
if (id) {
|
|
189
|
+
const exact = candidates.find((n) => n.id === id);
|
|
190
|
+
return exact ? { node: exact } : { error: `No USER-scope node found with id '${id}'` };
|
|
191
|
+
}
|
|
192
|
+
if (!name) {
|
|
193
|
+
return { error: "Missing node id or name" };
|
|
194
|
+
}
|
|
195
|
+
const normFile = fileFilter?.replace(/\\/g, "/").toLowerCase();
|
|
196
|
+
const byFile = normFile
|
|
197
|
+
? candidates.filter((n) => {
|
|
198
|
+
const evs = Array.isArray(n.evidence) ? n.evidence : [n.evidence];
|
|
199
|
+
return evs.some((ev) => ev?.filePath?.replace(/\\/g, "/").toLowerCase().includes(normFile));
|
|
200
|
+
})
|
|
201
|
+
: candidates;
|
|
202
|
+
const targetLower = name.toLowerCase();
|
|
203
|
+
const strippedTargetLower = stripNodePrefix(name).toLowerCase();
|
|
204
|
+
const exactMatches = byFile.filter((n) => n.name.toLowerCase() === targetLower ||
|
|
205
|
+
stripNodePrefix(n.name).toLowerCase() === strippedTargetLower);
|
|
206
|
+
if (exactMatches.length === 1)
|
|
207
|
+
return { node: exactMatches[0] };
|
|
208
|
+
if (exactMatches.length > 1) {
|
|
209
|
+
const files = exactMatches
|
|
210
|
+
.slice(0, 4)
|
|
211
|
+
.map((n) => primaryEvidence(n.evidence)?.filePath ?? n.id)
|
|
212
|
+
.join(", ");
|
|
213
|
+
return {
|
|
214
|
+
error: `Ambiguous node name '${name}' matched ${exactMatches.length} nodes (${files}); specify exact id or file filter`,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const partialMatches = byFile.filter((n) => stripNodePrefix(n.name).toLowerCase().includes(strippedTargetLower));
|
|
218
|
+
if (partialMatches.length === 1)
|
|
219
|
+
return { node: partialMatches[0] };
|
|
220
|
+
if (partialMatches.length > 1) {
|
|
221
|
+
return {
|
|
222
|
+
error: `Ambiguous partial name '${name}' matched ${partialMatches.length} nodes; pass exact sourceId/targetId from chomp_get_nodes`,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
return { error: `Could not resolve '${name}'${fileFilter ? ` in '${fileFilter}'` : ""} in graph` };
|
|
226
|
+
}
|
|
227
|
+
export function evaluateShimsAgainstGraph(graph, shims) {
|
|
228
|
+
const owningDuByNodeId = buildOwningDuMap(graph.nodes);
|
|
229
|
+
const resolved = [];
|
|
230
|
+
const unmatched = [];
|
|
231
|
+
for (const shim of shims) {
|
|
232
|
+
if (shim.enabled === false)
|
|
233
|
+
continue;
|
|
234
|
+
const srcRes = matchNodeInGraph(graph.nodes, shim.sourceId, shim.sourceName, shim.sourceFile, false);
|
|
235
|
+
if (!srcRes.node) {
|
|
236
|
+
unmatched.push({
|
|
237
|
+
shim,
|
|
238
|
+
reason: `Source unmatched: ${srcRes.error ?? "unknown source"}`,
|
|
239
|
+
});
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const sourceNode = srcRes.node;
|
|
243
|
+
const sourceDu = owningDuByNodeId.get(sourceNode.id);
|
|
244
|
+
let targetNode;
|
|
245
|
+
let syntheticConnector;
|
|
246
|
+
if (shim.targetConnector) {
|
|
247
|
+
const connName = shim.targetConnector.name.trim();
|
|
248
|
+
const connEntityType = shim.targetConnector.entityType?.trim() || "EXTERNAL_SDK_CALL";
|
|
249
|
+
const existingConn = graph.nodes.find((n) => n.type === "OPEN_CONNECTOR" &&
|
|
250
|
+
(n.name.toLowerCase() === connName.toLowerCase() ||
|
|
251
|
+
stripNodePrefix(n.name).toLowerCase() === stripNodePrefix(connName).toLowerCase()));
|
|
252
|
+
if (existingConn) {
|
|
253
|
+
targetNode = existingConn;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
const duScope = sourceDu ? stripNodePrefix(sourceDu.name) : "repo";
|
|
257
|
+
const srcEv = primaryEvidence(sourceNode.evidence) ?? { filePath: shim.sourceFile ?? "unknown" };
|
|
258
|
+
const connId = stableEntityId(`shim-connector:${duScope}`, "OPEN_CONNECTOR", connName);
|
|
259
|
+
syntheticConnector = {
|
|
260
|
+
id: connId,
|
|
261
|
+
name: connName,
|
|
262
|
+
type: "OPEN_CONNECTOR",
|
|
263
|
+
entityType: connEntityType,
|
|
264
|
+
patternId: "shim.open-connector",
|
|
265
|
+
scope: "USER",
|
|
266
|
+
evidence: srcEv,
|
|
267
|
+
metadata: {
|
|
268
|
+
source: "USER_SHIM",
|
|
269
|
+
shimId: shim.id,
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
targetNode = syntheticConnector;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
const tgtRes = matchNodeInGraph(graph.nodes, shim.targetId, shim.targetName, shim.targetFile, true);
|
|
277
|
+
if (!tgtRes.node) {
|
|
278
|
+
unmatched.push({
|
|
279
|
+
shim,
|
|
280
|
+
reason: `Target unmatched: ${tgtRes.error ?? "unknown target"}`,
|
|
281
|
+
packageId: sourceDu?.id,
|
|
282
|
+
});
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
targetNode = tgtRes.node;
|
|
286
|
+
}
|
|
287
|
+
if (sourceNode.id === targetNode.id) {
|
|
288
|
+
unmatched.push({
|
|
289
|
+
shim,
|
|
290
|
+
reason: `Self-loop shim rejected: source and target both resolved to '${sourceNode.name}'`,
|
|
291
|
+
packageId: sourceDu?.id,
|
|
292
|
+
});
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
// Match unresolved L0 call edges originating from `sourceNode.id`.
|
|
296
|
+
const unresolvedOnSource = graph.edges.filter((e) => e.sourceId === sourceNode.id && !e.targetId && (e.scope ?? "USER") === "USER");
|
|
297
|
+
let matchedEdges = unresolvedOnSource;
|
|
298
|
+
if (shim.callName || shim.shapeKey) {
|
|
299
|
+
const normCall = shim.callName ? normalizeCallToken(shim.callName).toLowerCase() : null;
|
|
300
|
+
matchedEdges = unresolvedOnSource.filter((e) => {
|
|
301
|
+
const edgeCall = normalizeCallToken(e.name).toLowerCase();
|
|
302
|
+
const callOk = normCall
|
|
303
|
+
? edgeCall === normCall || edgeCall.endsWith(`.${normCall}`) || edgeCall.includes(normCall)
|
|
304
|
+
: true;
|
|
305
|
+
const shapeOk = shim.shapeKey ? e.metadata?.shapeKey === shim.shapeKey : true;
|
|
306
|
+
return callOk && shapeOk;
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
else if (unresolvedOnSource.length > 1) {
|
|
310
|
+
// If multiple unresolved calls exist and no callName/shapeKey filter was supplied, try matching target symbol name;
|
|
311
|
+
// otherwise emit a companion shimmed edge without guessing which unresolved call site it belonged to.
|
|
312
|
+
const tgtSym = stripNodePrefix(targetNode.name).split(".").pop()?.toLowerCase() ?? "";
|
|
313
|
+
const bySym = tgtSym
|
|
314
|
+
? unresolvedOnSource.filter((e) => normalizeCallToken(e.name).toLowerCase().includes(tgtSym))
|
|
315
|
+
: [];
|
|
316
|
+
matchedEdges = bySym.length === 1 ? bySym : [];
|
|
317
|
+
}
|
|
318
|
+
const chosenEdgeEv = matchedEdges[0]?.evidence ?? sourceNode.evidence;
|
|
319
|
+
const entityType = shim.entityType?.trim() || "CALL";
|
|
320
|
+
const edgeName = shim.edgeName?.trim() ||
|
|
321
|
+
(targetNode.type === "OPEN_CONNECTOR"
|
|
322
|
+
? `Call: ${targetNode.name}`
|
|
323
|
+
: `Call: ${stripNodePrefix(targetNode.name)}()`);
|
|
324
|
+
const rawCallForLabel = shim.callName
|
|
325
|
+
? normalizeCallToken(shim.callName)
|
|
326
|
+
: matchedEdges[0]?.name
|
|
327
|
+
? normalizeCallToken(matchedEdges[0].name)
|
|
328
|
+
: "";
|
|
329
|
+
const derivedShimLabel = shim.label?.trim() ||
|
|
330
|
+
(rawCallForLabel.includes(".")
|
|
331
|
+
? rawCallForLabel.slice(0, rawCallForLabel.lastIndexOf(".")).trim()
|
|
332
|
+
: "") ||
|
|
333
|
+
"Custom adapter";
|
|
334
|
+
if (syntheticConnector) {
|
|
335
|
+
syntheticConnector.metadata = {
|
|
336
|
+
...(syntheticConnector.metadata ?? {}),
|
|
337
|
+
shimLabel: derivedShimLabel,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
const edgeEntry = {
|
|
341
|
+
shimId: shim.id,
|
|
342
|
+
sourceId: sourceNode.id,
|
|
343
|
+
targetId: targetNode.id,
|
|
344
|
+
name: edgeName,
|
|
345
|
+
entityType,
|
|
346
|
+
...(matchedEdges.length > 0 ? { resolvedEdgeIds: matchedEdges.map((e) => e.id) } : {}),
|
|
347
|
+
shimLabel: derivedShimLabel,
|
|
348
|
+
...(shim.rationale ? { rationale: shim.rationale } : {}),
|
|
349
|
+
evidence: chosenEdgeEv,
|
|
350
|
+
};
|
|
351
|
+
resolved.push({
|
|
352
|
+
shim,
|
|
353
|
+
sourceNode,
|
|
354
|
+
targetNode,
|
|
355
|
+
...(syntheticConnector ? { syntheticConnector } : {}),
|
|
356
|
+
resolvedEdgeIds: matchedEdges.map((e) => e.id),
|
|
357
|
+
edgeEntry,
|
|
358
|
+
packageId: sourceDu?.id,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
return { resolved, unmatched, owningDuByNodeId };
|
|
362
|
+
}
|
|
363
|
+
function computeActionableUnresolvedMap(graph) {
|
|
364
|
+
const nodeById = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
365
|
+
const outEdgeMap = new Map();
|
|
366
|
+
for (const e of graph.edges) {
|
|
367
|
+
if (!e.targetId || e.sourceId === e.targetId)
|
|
368
|
+
continue;
|
|
369
|
+
const list = outEdgeMap.get(e.sourceId) ?? [];
|
|
370
|
+
list.push(e.targetId);
|
|
371
|
+
outEdgeMap.set(e.sourceId, list);
|
|
372
|
+
}
|
|
373
|
+
return computeActionableUnresolvedCallsByNode(graph, nodeById, outEdgeMap);
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Collects unhandled patterns and unresolved external package/spec calls eligible for custom adapters.
|
|
377
|
+
* Deliberately excludes normal `DYNAMIC_BOUNDARY` parameter/local callbacks so custom adapters remain
|
|
378
|
+
* a rare escape hatch rather than flagging routine dynamic TypeScript code.
|
|
379
|
+
*/
|
|
380
|
+
function computeShimmableUnresolvedMap(graph, actionableByNode) {
|
|
381
|
+
const combined = new Map();
|
|
382
|
+
const seenKeys = new Set();
|
|
383
|
+
for (const [nodeId, calls] of actionableByNode.entries()) {
|
|
384
|
+
const list = [];
|
|
385
|
+
for (const c of calls) {
|
|
386
|
+
const clean = normalizeCallToken(c.name);
|
|
387
|
+
const dedupeKey = `${nodeId}::${clean}::${c.shapeKey ?? ""}`;
|
|
388
|
+
if (seenKeys.has(dedupeKey))
|
|
389
|
+
continue;
|
|
390
|
+
seenKeys.add(dedupeKey);
|
|
391
|
+
list.push({ name: c.name, ...(c.shapeKey ? { shapeKey: c.shapeKey } : {}) });
|
|
392
|
+
}
|
|
393
|
+
if (list.length > 0)
|
|
394
|
+
combined.set(nodeId, list);
|
|
395
|
+
}
|
|
396
|
+
for (const edge of graph.edges) {
|
|
397
|
+
if (edge.targetId || !edge.sourceId)
|
|
398
|
+
continue;
|
|
399
|
+
if ((edge.scope ?? "USER") !== "USER" || edge.metadata?.crossScopeFiltered)
|
|
400
|
+
continue;
|
|
401
|
+
if (!edge.entityType || !CONNECTIVITY_TARGET_TYPES.has(edge.entityType))
|
|
402
|
+
continue;
|
|
403
|
+
const importSpec = typeof edge.metadata?.importSpecifier === "string" ? edge.metadata.importSpecifier.trim() : "";
|
|
404
|
+
const isExternalImportSpec = Boolean(importSpec) &&
|
|
405
|
+
!importSpec.startsWith(".") &&
|
|
406
|
+
!importSpec.startsWith("/") &&
|
|
407
|
+
!importSpec.startsWith("#") &&
|
|
408
|
+
!importSpec.startsWith("node:");
|
|
409
|
+
const isExternalUnhandled = edge.metadata?.unresolvedReason === "external-package" ||
|
|
410
|
+
(isExternalImportSpec && classifyCallEdgeResolution(edge) === "KNOWABLE_GAP");
|
|
411
|
+
if (!isExternalUnhandled)
|
|
412
|
+
continue;
|
|
413
|
+
const clean = normalizeCallToken(edge.name);
|
|
414
|
+
if (!clean)
|
|
415
|
+
continue;
|
|
416
|
+
const shapeKey = typeof edge.metadata?.shapeKey === "string" ? edge.metadata.shapeKey : undefined;
|
|
417
|
+
const dedupeKey = `${edge.sourceId}::${clean}::${shapeKey ?? ""}`;
|
|
418
|
+
if (seenKeys.has(dedupeKey))
|
|
419
|
+
continue;
|
|
420
|
+
seenKeys.add(dedupeKey);
|
|
421
|
+
const list = combined.get(edge.sourceId) ?? [];
|
|
422
|
+
list.push({
|
|
423
|
+
name: edge.entityType === "RENDERS" ? clean : `${clean}()`,
|
|
424
|
+
...(shapeKey ? { shapeKey } : {}),
|
|
425
|
+
});
|
|
426
|
+
combined.set(edge.sourceId, list);
|
|
427
|
+
}
|
|
428
|
+
return combined;
|
|
429
|
+
}
|
|
430
|
+
const CUSTOM_ADAPTERS_HUD_DESCRIPTION = "Bridges unresolved calls and external APIs onto the map.";
|
|
431
|
+
function humanizeUnhandledPattern(shapeKey) {
|
|
432
|
+
const sk = shapeKey ?? "";
|
|
433
|
+
if (sk.includes("ElementAccessExpression"))
|
|
434
|
+
return "Dynamic key lookup";
|
|
435
|
+
if (sk.includes("/Param/"))
|
|
436
|
+
return "Callback parameter";
|
|
437
|
+
if (sk.includes("/Import/"))
|
|
438
|
+
return "Imported registry or package call";
|
|
439
|
+
if (sk.includes("/This/"))
|
|
440
|
+
return "Class member call";
|
|
441
|
+
if (sk.includes("/CallResult/") || sk.includes("/AwaitResult/"))
|
|
442
|
+
return "Factory return call";
|
|
443
|
+
if (sk.includes("/LocalVar/"))
|
|
444
|
+
return "Variable dispatch";
|
|
445
|
+
return "Unresolved call pattern";
|
|
446
|
+
}
|
|
447
|
+
function summarizeUnhandledPatternItems(hotspotNodes) {
|
|
448
|
+
const counts = new Map();
|
|
449
|
+
for (const h of hotspotNodes) {
|
|
450
|
+
for (const c of h.calls) {
|
|
451
|
+
const label = humanizeUnhandledPattern(c.shapeKey);
|
|
452
|
+
counts.set(label, (counts.get(label) ?? 0) + 1);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return [...counts.entries()]
|
|
456
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
457
|
+
.slice(0, 5)
|
|
458
|
+
.map(([label, count]) => ({ label, value: count }));
|
|
459
|
+
}
|
|
460
|
+
function formatCustomAdaptersHeadline(activeCount, unhandledCount, brokenCount) {
|
|
461
|
+
if (brokenCount > 0) {
|
|
462
|
+
return `${brokenCount} broken bridge${brokenCount === 1 ? "" : "s"}${unhandledCount > 0 ? ` · ${unhandledCount} unresolved` : ""}`;
|
|
463
|
+
}
|
|
464
|
+
if (activeCount > 0 && unhandledCount > 0) {
|
|
465
|
+
return `${activeCount} active · ${unhandledCount} unresolved`;
|
|
466
|
+
}
|
|
467
|
+
if (activeCount > 0) {
|
|
468
|
+
return `${activeCount} bridge${activeCount === 1 ? "" : "s"} active`;
|
|
469
|
+
}
|
|
470
|
+
if (unhandledCount > 0) {
|
|
471
|
+
return `${unhandledCount} unresolved pattern${unhandledCount === 1 ? "" : "s"}`;
|
|
472
|
+
}
|
|
473
|
+
return "No unresolved patterns";
|
|
474
|
+
}
|
|
475
|
+
function buildShimPromptAction(pkgLabel, unresolvedCount, brokenCount) {
|
|
476
|
+
const scopeClause = pkgLabel ? ` for package "${pkgLabel}"` : "";
|
|
477
|
+
if (brokenCount > 0) {
|
|
478
|
+
return {
|
|
479
|
+
label: "Copy repair prompt",
|
|
480
|
+
text: `Use \`chomp_local_shims\` (action: "status")${scopeClause} to inspect the ${brokenCount} broken bridge(s) in \`${LOCAL_SHIMS_FILE_REL}\`, update stale source/target IDs via \`chomp_get_nodes\`, and verify with \`chomp_get_diff\`.`,
|
|
481
|
+
hint: "Fix broken source or target IDs in shims.json.",
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
if (unresolvedCount > 0) {
|
|
485
|
+
return {
|
|
486
|
+
label: "Copy agent prompt",
|
|
487
|
+
text: `Use \`chomp_local_shims\` (action: "suggest"${pkgLabel ? `, packageName: "${pkgLabel}"` : ""}) to inspect ${unresolvedCount} unresolved pattern(s)${scopeClause}, add \`chomp.config.json#conventions\` rules for repeatable syntax or upsert bridges via \`chomp_local_shims\` (action: "upsert"), and verify with \`chomp_get_diff\`.`,
|
|
488
|
+
hint: "Bridge unresolved patterns or external APIs.",
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
return undefined;
|
|
492
|
+
}
|
|
493
|
+
function buildNodeShimPresentation(node, outbound, inbound, remainingCalls) {
|
|
494
|
+
const sections = [];
|
|
495
|
+
if (outbound.length > 0) {
|
|
496
|
+
const rows = outbound.map((rec) => {
|
|
497
|
+
const ev = primaryEvidence(rec.edgeEntry.evidence);
|
|
498
|
+
return {
|
|
499
|
+
cells: {
|
|
500
|
+
target: stripNodePrefix(rec.targetNode.name),
|
|
501
|
+
type: rec.edgeEntry.entityType,
|
|
502
|
+
resolvedCall: rec.shim.callName ?? (rec.resolvedEdgeIds.length > 0 ? `${rec.resolvedEdgeIds.length} call(s)` : "Direct bridge"),
|
|
503
|
+
rationale: rec.shim.rationale ?? "Custom bridge",
|
|
504
|
+
},
|
|
505
|
+
tone: "good",
|
|
506
|
+
entityId: rec.targetNode.id,
|
|
507
|
+
...(ev?.filePath
|
|
508
|
+
? {
|
|
509
|
+
evidence: [
|
|
510
|
+
{
|
|
511
|
+
filePath: ev.filePath,
|
|
512
|
+
...(ev.lineNumber ? { lineNumber: ev.lineNumber } : {}),
|
|
513
|
+
...(ev.snippet ? { snippet: ev.snippet } : {}),
|
|
514
|
+
},
|
|
515
|
+
],
|
|
516
|
+
}
|
|
517
|
+
: {}),
|
|
518
|
+
};
|
|
519
|
+
});
|
|
520
|
+
sections.push({
|
|
521
|
+
id: "outbound-shims",
|
|
522
|
+
title: "Outbound bridges",
|
|
523
|
+
badge: { text: String(outbound.length), tone: "good" },
|
|
524
|
+
priority: 10,
|
|
525
|
+
blocks: [
|
|
526
|
+
{
|
|
527
|
+
kind: "table",
|
|
528
|
+
columns: [
|
|
529
|
+
{ id: "target", label: "Target" },
|
|
530
|
+
{ id: "type", label: "Connection", mono: true },
|
|
531
|
+
{ id: "resolvedCall", label: "Call site", mono: true },
|
|
532
|
+
{ id: "rationale", label: "Note" },
|
|
533
|
+
],
|
|
534
|
+
rows,
|
|
535
|
+
},
|
|
536
|
+
],
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
if (inbound.length > 0) {
|
|
540
|
+
const items = inbound.map((rec) => ({
|
|
541
|
+
label: stripNodePrefix(rec.sourceNode.name),
|
|
542
|
+
detail: rec.shim.rationale
|
|
543
|
+
? `${rec.edgeEntry.entityType} · ${rec.shim.rationale}`
|
|
544
|
+
: `${rec.edgeEntry.entityType} (${rec.shim.id})`,
|
|
545
|
+
tone: "info",
|
|
546
|
+
entityId: rec.sourceNode.id,
|
|
547
|
+
}));
|
|
548
|
+
sections.push({
|
|
549
|
+
id: "inbound-shims",
|
|
550
|
+
title: "Inbound bridges",
|
|
551
|
+
badge: { text: String(inbound.length), tone: "info" },
|
|
552
|
+
priority: 20,
|
|
553
|
+
blocks: [{ kind: "list", items }],
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
if (remainingCalls.length > 0) {
|
|
557
|
+
sections.push({
|
|
558
|
+
id: "remaining-unresolved",
|
|
559
|
+
title: "Unresolved patterns",
|
|
560
|
+
badge: { text: String(remainingCalls.length), tone: "warn" },
|
|
561
|
+
priority: 30,
|
|
562
|
+
blocks: [
|
|
563
|
+
{
|
|
564
|
+
kind: "list",
|
|
565
|
+
items: remainingCalls.map((c) => ({
|
|
566
|
+
label: c.name,
|
|
567
|
+
detail: [c.reason, c.shapeKey].filter(Boolean).join(" · "),
|
|
568
|
+
tone: "warn",
|
|
569
|
+
...(c.filePath
|
|
570
|
+
? {
|
|
571
|
+
evidence: [
|
|
572
|
+
{
|
|
573
|
+
filePath: c.filePath,
|
|
574
|
+
...(c.lineNumber ? { lineNumber: c.lineNumber } : {}),
|
|
575
|
+
...(c.snippet ? { snippet: c.snippet } : {}),
|
|
576
|
+
},
|
|
577
|
+
],
|
|
578
|
+
}
|
|
579
|
+
: {}),
|
|
580
|
+
})),
|
|
581
|
+
},
|
|
582
|
+
],
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
const totalActive = outbound.length + inbound.length;
|
|
586
|
+
return {
|
|
587
|
+
version: PLUGIN_PRESENTATION_VERSION,
|
|
588
|
+
summary: {
|
|
589
|
+
status: remainingCalls.length > 0 ? "warn" : "ok",
|
|
590
|
+
badge: {
|
|
591
|
+
text: totalActive > 0
|
|
592
|
+
? `${totalActive} active`
|
|
593
|
+
: `${remainingCalls.length} unresolved`,
|
|
594
|
+
tone: remainingCalls.length > 0 ? "warn" : "good",
|
|
595
|
+
},
|
|
596
|
+
headline: outbound.length > 0
|
|
597
|
+
? `${outbound.length} outbound bridge${outbound.length === 1 ? "" : "s"}${remainingCalls.length > 0 ? ` · ${remainingCalls.length} unresolved` : ""}`
|
|
598
|
+
: inbound.length > 0
|
|
599
|
+
? `${inbound.length} inbound bridge${inbound.length === 1 ? "" : "s"}`
|
|
600
|
+
: `${remainingCalls.length} unresolved pattern${remainingCalls.length === 1 ? "" : "s"}`,
|
|
601
|
+
issueCount: remainingCalls.length,
|
|
602
|
+
},
|
|
603
|
+
sections,
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
export const localShimsPlugin = {
|
|
607
|
+
name: LOCAL_SHIMS_PLUGIN_NAME,
|
|
608
|
+
displayName: "Bridges",
|
|
609
|
+
version: "1.0.0",
|
|
610
|
+
order: 10,
|
|
611
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
612
|
+
description: CUSTOM_ADAPTERS_HUD_DESCRIPTION,
|
|
613
|
+
accepts: (filePath) => filePath === LOCAL_SHIMS_FILE_REL ||
|
|
614
|
+
filePath === LEGACY_LOCAL_SHIMS_FILE_REL ||
|
|
615
|
+
filePath === "chomp.config.json",
|
|
616
|
+
extract: (ctx) => {
|
|
617
|
+
const includeConfigShims = ctx.settings.includeConfigShims !== false;
|
|
618
|
+
const showHotspotLens = ctx.settings.showUnresolvedHotspotLens !== false;
|
|
619
|
+
const shimsText = ctx.readPluginFile("shims.json") ?? ctx.readFile(LEGACY_LOCAL_SHIMS_FILE_REL);
|
|
620
|
+
const configText = includeConfigShims ? ctx.readFile("chomp.config.json") : null;
|
|
621
|
+
const { shims, parseErrors } = loadLocalShimsFromTexts(shimsText, configText);
|
|
622
|
+
const inputDeps = [];
|
|
623
|
+
const shimsDep = ctx.hashPluginFile("shims.json") ?? ctx.hashFile(LEGACY_LOCAL_SHIMS_FILE_REL);
|
|
624
|
+
if (shimsDep)
|
|
625
|
+
inputDeps.push(shimsDep);
|
|
626
|
+
if (includeConfigShims) {
|
|
627
|
+
const configDep = ctx.hashFile("chomp.config.json");
|
|
628
|
+
if (configDep)
|
|
629
|
+
inputDeps.push(configDep);
|
|
630
|
+
}
|
|
631
|
+
const { resolved, unmatched, owningDuByNodeId } = evaluateShimsAgainstGraph(ctx.graph, shims);
|
|
632
|
+
const rawUnresolvedByNode = computeActionableUnresolvedMap(ctx.graph);
|
|
633
|
+
// Group resolved shims by sourceNode and targetNode
|
|
634
|
+
const outboundByNodeId = new Map();
|
|
635
|
+
const inboundByNodeId = new Map();
|
|
636
|
+
for (const rec of resolved) {
|
|
637
|
+
const outList = outboundByNodeId.get(rec.sourceNode.id) ?? [];
|
|
638
|
+
outList.push(rec);
|
|
639
|
+
outboundByNodeId.set(rec.sourceNode.id, outList);
|
|
640
|
+
if (!rec.syntheticConnector) {
|
|
641
|
+
const inList = inboundByNodeId.get(rec.targetNode.id) ?? [];
|
|
642
|
+
inList.push(rec);
|
|
643
|
+
inboundByNodeId.set(rec.targetNode.id, inList);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
// Project shims onto a working copy so we can measure exact post-shim unresolved counts!
|
|
647
|
+
const provisionalOverlays = [...outboundByNodeId.entries()].map(([entityId, recs]) => ({
|
|
648
|
+
entityId,
|
|
649
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
650
|
+
payload: {
|
|
651
|
+
shimmedEdges: recs.map((r) => r.edgeEntry),
|
|
652
|
+
syntheticConnectors: recs
|
|
653
|
+
.map((r) => r.syntheticConnector)
|
|
654
|
+
.filter((c) => Boolean(c)),
|
|
655
|
+
},
|
|
656
|
+
}));
|
|
657
|
+
const rawShimmableByNode = computeShimmableUnresolvedMap(ctx.graph, rawUnresolvedByNode);
|
|
658
|
+
const projectedGraph = applyShimOverlaysToGraph(ctx.graph, provisionalOverlays, { enabled: true });
|
|
659
|
+
const postUnresolvedByNode = computeActionableUnresolvedMap(projectedGraph);
|
|
660
|
+
const postShimmableByNode = computeShimmableUnresolvedMap(projectedGraph, postUnresolvedByNode);
|
|
661
|
+
const overlays = [];
|
|
662
|
+
const touchedNodeIds = new Set([
|
|
663
|
+
...outboundByNodeId.keys(),
|
|
664
|
+
...inboundByNodeId.keys(),
|
|
665
|
+
...postUnresolvedByNode.keys(),
|
|
666
|
+
]);
|
|
667
|
+
const nodeById = new Map(ctx.graph.nodes.map((n) => [n.id, n]));
|
|
668
|
+
for (const nodeId of touchedNodeIds) {
|
|
669
|
+
const node = nodeById.get(nodeId);
|
|
670
|
+
if (!node)
|
|
671
|
+
continue;
|
|
672
|
+
const outbound = outboundByNodeId.get(nodeId) ?? [];
|
|
673
|
+
const inbound = inboundByNodeId.get(nodeId) ?? [];
|
|
674
|
+
const remainingCalls = postUnresolvedByNode.get(nodeId) ?? [];
|
|
675
|
+
const syntheticConnectors = outbound
|
|
676
|
+
.map((r) => r.syntheticConnector)
|
|
677
|
+
.filter((c) => Boolean(c));
|
|
678
|
+
const presentation = buildNodeShimPresentation(node, outbound, inbound, remainingCalls);
|
|
679
|
+
const evItems = outbound
|
|
680
|
+
.map((r) => primaryEvidence(r.edgeEntry.evidence))
|
|
681
|
+
.filter((ev) => Boolean(ev?.filePath))
|
|
682
|
+
.slice(0, 5)
|
|
683
|
+
.map((ev) => ctx.evidence(ev.filePath, ev.lineNumber, ev.snippet, "user-shim"));
|
|
684
|
+
overlays.push({
|
|
685
|
+
entityId: nodeId,
|
|
686
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
687
|
+
source: LOCAL_SHIMS_PLUGIN_NAME,
|
|
688
|
+
derivation: "INFERRED",
|
|
689
|
+
...(inputDeps.length > 0 ? { inputs: inputDeps } : {}),
|
|
690
|
+
...(evItems.length > 0 ? { evidence: evItems } : {}),
|
|
691
|
+
payload: {
|
|
692
|
+
...(outbound.length > 0 ? { shimmedEdges: outbound.map((r) => r.edgeEntry) } : {}),
|
|
693
|
+
...(syntheticConnectors.length > 0 ? { syntheticConnectors } : {}),
|
|
694
|
+
shimIds: [...outbound.map((r) => r.shim.id), ...inbound.map((r) => r.shim.id)],
|
|
695
|
+
[PLUGIN_PRESENTATION_PAYLOAD_KEY]: presentation,
|
|
696
|
+
},
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
// Build per-DEPLOYMENT_UNIT package overlays so HUD & PackageInspector render cleanly
|
|
700
|
+
const deploymentUnits = ctx.graph.nodes.filter((n) => n.entityType === "DEPLOYMENT_UNIT" && (n.scope ?? "USER") === "USER");
|
|
701
|
+
const totalShimmableRepoGaps = [...postShimmableByNode.values()].reduce((acc, l) => acc + l.length, 0);
|
|
702
|
+
const totalRepoBroken = unmatched.length + parseErrors.length;
|
|
703
|
+
const repoHeadline = formatCustomAdaptersHeadline(resolved.length, totalShimmableRepoGaps, totalRepoBroken);
|
|
704
|
+
const repoPrompt = buildShimPromptAction("", totalShimmableRepoGaps, totalRepoBroken);
|
|
705
|
+
for (const du of deploymentUnits) {
|
|
706
|
+
const pkgLabel = stripNodePrefix(du.name);
|
|
707
|
+
const pkgKey = pkgLabel.replace(/^@[^/]+\//, "");
|
|
708
|
+
const pkgResolved = resolved.filter((r) => r.packageId === du.id);
|
|
709
|
+
const pkgUnmatched = unmatched.filter((u) => !u.packageId || u.packageId === du.id);
|
|
710
|
+
let rawGapCount = 0;
|
|
711
|
+
let remainingGapCount = 0;
|
|
712
|
+
let shimmableCount = 0;
|
|
713
|
+
const hotspotNodes = [];
|
|
714
|
+
const shimmableNodes = [];
|
|
715
|
+
for (const n of ctx.graph.nodes) {
|
|
716
|
+
if ((n.scope ?? "USER") !== "USER")
|
|
717
|
+
continue;
|
|
718
|
+
const ownerDu = owningDuByNodeId.get(n.id);
|
|
719
|
+
if (ownerDu?.id !== du.id)
|
|
720
|
+
continue;
|
|
721
|
+
const rawCalls = rawUnresolvedByNode.get(n.id) ?? [];
|
|
722
|
+
const remCalls = postUnresolvedByNode.get(n.id) ?? [];
|
|
723
|
+
const shimmableCalls = postShimmableByNode.get(n.id) ?? [];
|
|
724
|
+
rawGapCount += rawCalls.length;
|
|
725
|
+
remainingGapCount += remCalls.length;
|
|
726
|
+
shimmableCount += shimmableCalls.length;
|
|
727
|
+
if (remCalls.length > 0) {
|
|
728
|
+
hotspotNodes.push({ node: n, calls: remCalls });
|
|
729
|
+
}
|
|
730
|
+
if (shimmableCalls.length > 0) {
|
|
731
|
+
shimmableNodes.push({ calls: shimmableCalls });
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
hotspotNodes.sort((a, b) => b.calls.length - a.calls.length || a.node.name.localeCompare(b.node.name));
|
|
735
|
+
const resolvedGapCount = Math.max(0, rawGapCount - remainingGapCount);
|
|
736
|
+
const brokenCount = pkgUnmatched.length + parseErrors.length;
|
|
737
|
+
const issueCount = brokenCount + shimmableCount;
|
|
738
|
+
const unhandledPatternItems = summarizeUnhandledPatternItems(shimmableNodes);
|
|
739
|
+
let status = "ok";
|
|
740
|
+
let badgeText = "0 needed";
|
|
741
|
+
let badgeTone = "good";
|
|
742
|
+
if (brokenCount > 0) {
|
|
743
|
+
status = "warn";
|
|
744
|
+
badgeText = `${brokenCount} broken`;
|
|
745
|
+
badgeTone = "warn";
|
|
746
|
+
}
|
|
747
|
+
else if (pkgResolved.length > 0 && shimmableCount === 0) {
|
|
748
|
+
status = "ok";
|
|
749
|
+
badgeText = `${pkgResolved.length} active`;
|
|
750
|
+
badgeTone = "good";
|
|
751
|
+
}
|
|
752
|
+
else if (pkgResolved.length > 0 && shimmableCount > 0) {
|
|
753
|
+
status = "warn";
|
|
754
|
+
badgeText = `${pkgResolved.length} active · ${shimmableCount} unresolved`;
|
|
755
|
+
badgeTone = "warn";
|
|
756
|
+
}
|
|
757
|
+
else if (shimmableCount > 0) {
|
|
758
|
+
status = "warn";
|
|
759
|
+
badgeText = `${shimmableCount} unresolved`;
|
|
760
|
+
badgeTone = "warn";
|
|
761
|
+
}
|
|
762
|
+
const headline = formatCustomAdaptersHeadline(pkgResolved.length, shimmableCount, brokenCount);
|
|
763
|
+
const views = [];
|
|
764
|
+
if (pkgResolved.length > 0) {
|
|
765
|
+
const highlightIds = [];
|
|
766
|
+
const annotations = {};
|
|
767
|
+
for (const rec of pkgResolved) {
|
|
768
|
+
if (!highlightIds.includes(rec.sourceNode.id))
|
|
769
|
+
highlightIds.push(rec.sourceNode.id);
|
|
770
|
+
if (!highlightIds.includes(rec.targetNode.id)) {
|
|
771
|
+
highlightIds.push(rec.targetNode.id);
|
|
772
|
+
}
|
|
773
|
+
annotations[rec.sourceNode.id] =
|
|
774
|
+
`Bridged -> ${stripNodePrefix(rec.targetNode.name)}${rec.shim.rationale ? `: ${rec.shim.rationale}` : ""}`;
|
|
775
|
+
}
|
|
776
|
+
views.push({
|
|
777
|
+
id: "active-shimmed-links",
|
|
778
|
+
title: "Active bridges",
|
|
779
|
+
description: `Nodes connected via ${LOCAL_SHIMS_FILE_REL} in ${pkgLabel}`,
|
|
780
|
+
badge: { text: String(pkgResolved.length), tone: "good" },
|
|
781
|
+
highlightNodeIds: highlightIds,
|
|
782
|
+
viewLevel: "package",
|
|
783
|
+
packageName: pkgKey,
|
|
784
|
+
nodeAnnotations: annotations,
|
|
785
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
if (hotspotNodes.length > 0 && showHotspotLens) {
|
|
789
|
+
const annotations = {};
|
|
790
|
+
for (const h of hotspotNodes.slice(0, 40)) {
|
|
791
|
+
annotations[h.node.id] = `${h.calls.length} unresolved pattern(s): ${h.calls
|
|
792
|
+
.slice(0, 3)
|
|
793
|
+
.map((c) => c.name)
|
|
794
|
+
.join(", ")}`;
|
|
795
|
+
}
|
|
796
|
+
views.push({
|
|
797
|
+
id: "unresolved-call-hotspots",
|
|
798
|
+
title: "Unresolved patterns",
|
|
799
|
+
description: `Nodes in ${pkgLabel} with unresolved call patterns`,
|
|
800
|
+
badge: { text: String(shimmableCount), tone: "warn" },
|
|
801
|
+
highlightNodeIds: hotspotNodes.slice(0, 40).map((h) => h.node.id),
|
|
802
|
+
viewLevel: "package",
|
|
803
|
+
packageName: pkgKey,
|
|
804
|
+
nodeAnnotations: annotations,
|
|
805
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
const sections = [
|
|
809
|
+
{
|
|
810
|
+
id: "shims-overview",
|
|
811
|
+
title: "Bridges",
|
|
812
|
+
priority: 10,
|
|
813
|
+
blocks: [
|
|
814
|
+
{
|
|
815
|
+
kind: "metrics",
|
|
816
|
+
items: [
|
|
817
|
+
{
|
|
818
|
+
id: "active-shims",
|
|
819
|
+
label: "Active bridges",
|
|
820
|
+
value: pkgResolved.length,
|
|
821
|
+
unit: "count",
|
|
822
|
+
tone: pkgResolved.length > 0 ? "good" : "neutral",
|
|
823
|
+
hint: "Bridges active in this package.",
|
|
824
|
+
emptyText: "No bridges configured.",
|
|
825
|
+
},
|
|
826
|
+
{
|
|
827
|
+
id: "bridged-gaps",
|
|
828
|
+
label: "Bridged",
|
|
829
|
+
value: resolvedGapCount,
|
|
830
|
+
unit: "count",
|
|
831
|
+
tone: resolvedGapCount > 0 ? "good" : "neutral",
|
|
832
|
+
hint: "Unresolved patterns connected by a bridge.",
|
|
833
|
+
emptyText: "None bridged.",
|
|
834
|
+
},
|
|
835
|
+
{
|
|
836
|
+
id: "open-gaps",
|
|
837
|
+
label: "Unresolved",
|
|
838
|
+
value: shimmableCount,
|
|
839
|
+
unit: "count",
|
|
840
|
+
tone: shimmableCount > 0 ? "warn" : "good",
|
|
841
|
+
hint: "Unresolved patterns or external APIs in this package.",
|
|
842
|
+
emptyText: "No unresolved patterns.",
|
|
843
|
+
},
|
|
844
|
+
...(brokenCount > 0
|
|
845
|
+
? [
|
|
846
|
+
{
|
|
847
|
+
id: "broken-shims",
|
|
848
|
+
label: "Broken",
|
|
849
|
+
value: brokenCount,
|
|
850
|
+
unit: "count",
|
|
851
|
+
tone: "bad",
|
|
852
|
+
hint: "Bridges whose source or target no longer exists.",
|
|
853
|
+
},
|
|
854
|
+
]
|
|
855
|
+
: []),
|
|
856
|
+
],
|
|
857
|
+
},
|
|
858
|
+
],
|
|
859
|
+
},
|
|
860
|
+
];
|
|
861
|
+
if (pkgResolved.length > 0) {
|
|
862
|
+
const { items: cappedRows, totalCount } = capForPresentation(pkgResolved, 50);
|
|
863
|
+
sections.push({
|
|
864
|
+
id: "active-shims-table",
|
|
865
|
+
title: "Active bridges",
|
|
866
|
+
badge: { text: String(pkgResolved.length), tone: "good" },
|
|
867
|
+
priority: 20,
|
|
868
|
+
blocks: [
|
|
869
|
+
{
|
|
870
|
+
kind: "table",
|
|
871
|
+
columns: [
|
|
872
|
+
{ id: "source", label: "Source" },
|
|
873
|
+
{ id: "target", label: "Target" },
|
|
874
|
+
{ id: "type", label: "Connection", mono: true },
|
|
875
|
+
{ id: "origin", label: "Origin", mono: true },
|
|
876
|
+
],
|
|
877
|
+
rows: cappedRows.map((rec) => ({
|
|
878
|
+
cells: {
|
|
879
|
+
source: stripNodePrefix(rec.sourceNode.name),
|
|
880
|
+
target: stripNodePrefix(rec.targetNode.name),
|
|
881
|
+
type: rec.edgeEntry.entityType,
|
|
882
|
+
origin: rec.shim.origin ?? "shims.json",
|
|
883
|
+
},
|
|
884
|
+
tone: "good",
|
|
885
|
+
entityId: rec.sourceNode.id,
|
|
886
|
+
})),
|
|
887
|
+
maxVisible: 10,
|
|
888
|
+
totalCount,
|
|
889
|
+
},
|
|
890
|
+
],
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
if (brokenCount > 0) {
|
|
894
|
+
const items = [
|
|
895
|
+
...parseErrors.map((msg) => ({ label: "Manifest parse issue", detail: msg, tone: "bad" })),
|
|
896
|
+
...pkgUnmatched.map((u) => ({
|
|
897
|
+
label: u.shim.id,
|
|
898
|
+
detail: u.reason,
|
|
899
|
+
tone: "warn",
|
|
900
|
+
})),
|
|
901
|
+
];
|
|
902
|
+
sections.push({
|
|
903
|
+
id: "broken-shims-list",
|
|
904
|
+
title: "Broken bridges",
|
|
905
|
+
badge: { text: String(brokenCount), tone: "warn" },
|
|
906
|
+
priority: 25,
|
|
907
|
+
blocks: [{ kind: "list", items }],
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
if (hotspotNodes.length > 0 && showHotspotLens) {
|
|
911
|
+
const { items: cappedHotspots, totalCount } = capForPresentation(hotspotNodes, 30);
|
|
912
|
+
sections.push({
|
|
913
|
+
id: "unresolved-hotspots-table",
|
|
914
|
+
title: "Unresolved patterns",
|
|
915
|
+
badge: { text: String(shimmableCount), tone: "warn" },
|
|
916
|
+
priority: 30,
|
|
917
|
+
blocks: [
|
|
918
|
+
{
|
|
919
|
+
kind: "table",
|
|
920
|
+
columns: [
|
|
921
|
+
{ id: "contract", label: "Node" },
|
|
922
|
+
{ id: "gaps", label: "Count", align: "right" },
|
|
923
|
+
{ id: "examples", label: "Calls", mono: true },
|
|
924
|
+
],
|
|
925
|
+
rows: cappedHotspots.map((h) => {
|
|
926
|
+
const ev = primaryEvidence(h.node.evidence);
|
|
927
|
+
return {
|
|
928
|
+
cells: {
|
|
929
|
+
contract: stripNodePrefix(h.node.name),
|
|
930
|
+
gaps: h.calls.length,
|
|
931
|
+
examples: h.calls
|
|
932
|
+
.slice(0, 2)
|
|
933
|
+
.map((c) => c.name)
|
|
934
|
+
.join(", "),
|
|
935
|
+
},
|
|
936
|
+
tone: "warn",
|
|
937
|
+
entityId: h.node.id,
|
|
938
|
+
...(ev?.filePath
|
|
939
|
+
? {
|
|
940
|
+
evidence: [
|
|
941
|
+
{
|
|
942
|
+
filePath: ev.filePath,
|
|
943
|
+
...(ev.lineNumber ? { lineNumber: ev.lineNumber } : {}),
|
|
944
|
+
},
|
|
945
|
+
],
|
|
946
|
+
}
|
|
947
|
+
: {}),
|
|
948
|
+
};
|
|
949
|
+
}),
|
|
950
|
+
maxVisible: 8,
|
|
951
|
+
totalCount,
|
|
952
|
+
},
|
|
953
|
+
],
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
const pkgPrompt = buildShimPromptAction(pkgLabel, shimmableCount, brokenCount);
|
|
957
|
+
const pkgPresentation = {
|
|
958
|
+
version: PLUGIN_PRESENTATION_VERSION,
|
|
959
|
+
summary: {
|
|
960
|
+
status,
|
|
961
|
+
badge: { text: badgeText, tone: badgeTone },
|
|
962
|
+
headline,
|
|
963
|
+
repoHeadline,
|
|
964
|
+
description: CUSTOM_ADAPTERS_HUD_DESCRIPTION,
|
|
965
|
+
issueCount,
|
|
966
|
+
metrics: [
|
|
967
|
+
{
|
|
968
|
+
id: "active-shims",
|
|
969
|
+
label: "Active",
|
|
970
|
+
value: pkgResolved.length,
|
|
971
|
+
unit: "count",
|
|
972
|
+
tone: pkgResolved.length > 0 ? "good" : "neutral",
|
|
973
|
+
},
|
|
974
|
+
{
|
|
975
|
+
id: "bridged-gaps",
|
|
976
|
+
label: "Bridged",
|
|
977
|
+
value: resolvedGapCount,
|
|
978
|
+
unit: "count",
|
|
979
|
+
tone: resolvedGapCount > 0 ? "good" : "neutral",
|
|
980
|
+
},
|
|
981
|
+
{
|
|
982
|
+
id: "open-gaps",
|
|
983
|
+
label: "Unresolved",
|
|
984
|
+
value: shimmableCount,
|
|
985
|
+
unit: "count",
|
|
986
|
+
tone: shimmableCount > 0 ? "warn" : "good",
|
|
987
|
+
},
|
|
988
|
+
],
|
|
989
|
+
...(unhandledPatternItems.length > 0 ? { items: unhandledPatternItems } : {}),
|
|
990
|
+
...(views.length > 0 ? { views } : {}),
|
|
991
|
+
...(pkgPrompt ? { prompt: pkgPrompt } : {}),
|
|
992
|
+
...(repoPrompt ? { repoPrompt } : {}),
|
|
993
|
+
},
|
|
994
|
+
sections,
|
|
995
|
+
};
|
|
996
|
+
overlays.push({
|
|
997
|
+
entityId: du.id,
|
|
998
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
999
|
+
source: LOCAL_SHIMS_PLUGIN_NAME,
|
|
1000
|
+
derivation: "INFERRED",
|
|
1001
|
+
...(inputDeps.length > 0 ? { inputs: inputDeps } : {}),
|
|
1002
|
+
payload: {
|
|
1003
|
+
packageLabel: pkgLabel,
|
|
1004
|
+
packageKey: pkgKey,
|
|
1005
|
+
activeShimCount: pkgResolved.length,
|
|
1006
|
+
bridgedGapCount: resolvedGapCount,
|
|
1007
|
+
remainingUnresolvedCount: shimmableCount,
|
|
1008
|
+
brokenShimCount: brokenCount,
|
|
1009
|
+
[PLUGIN_PRESENTATION_PAYLOAD_KEY]: pkgPresentation,
|
|
1010
|
+
},
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
const totalRawGaps = [...rawShimmableByNode.values()].reduce((acc, l) => acc + l.length, 0);
|
|
1014
|
+
return {
|
|
1015
|
+
overlays,
|
|
1016
|
+
summary: {
|
|
1017
|
+
configuredShims: shims.length,
|
|
1018
|
+
activeShims: resolved.length,
|
|
1019
|
+
unmatchedShims: unmatched.length,
|
|
1020
|
+
parseErrors,
|
|
1021
|
+
rawUnresolvedCallCount: totalRawGaps,
|
|
1022
|
+
bridgedCallCount: Math.max(0, totalRawGaps - totalShimmableRepoGaps),
|
|
1023
|
+
remainingUnresolvedCallCount: totalShimmableRepoGaps,
|
|
1024
|
+
},
|
|
1025
|
+
};
|
|
1026
|
+
},
|
|
1027
|
+
};
|
|
1028
|
+
function readShimsManifestFile(repoPath) {
|
|
1029
|
+
const primaryPath = resolve(repoPath, LOCAL_SHIMS_FILE_REL);
|
|
1030
|
+
const legacyPath = resolve(repoPath, LEGACY_LOCAL_SHIMS_FILE_REL);
|
|
1031
|
+
const manifestPath = existsSync(primaryPath)
|
|
1032
|
+
? primaryPath
|
|
1033
|
+
: existsSync(legacyPath)
|
|
1034
|
+
? legacyPath
|
|
1035
|
+
: primaryPath;
|
|
1036
|
+
if (!existsSync(manifestPath)) {
|
|
1037
|
+
return { path: primaryPath, manifest: { version: 1, shims: [] } };
|
|
1038
|
+
}
|
|
1039
|
+
try {
|
|
1040
|
+
const raw = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
1041
|
+
const list = Array.isArray(raw)
|
|
1042
|
+
? raw
|
|
1043
|
+
: isObj(raw) && Array.isArray(raw.shims)
|
|
1044
|
+
? raw.shims
|
|
1045
|
+
: [];
|
|
1046
|
+
return { path: primaryPath, manifest: { version: 1, shims: list } };
|
|
1047
|
+
}
|
|
1048
|
+
catch {
|
|
1049
|
+
return { path: primaryPath, manifest: { version: 1, shims: [] } };
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
function writeShimsManifestFile(repoPath, shims) {
|
|
1053
|
+
const manifestPath = resolve(repoPath, LOCAL_SHIMS_FILE_REL);
|
|
1054
|
+
mkdirSync(dirname(manifestPath), { recursive: true });
|
|
1055
|
+
const normalized = shims.map((s) => ({ ...s, id: deriveShimId(s) }));
|
|
1056
|
+
writeFileSync(manifestPath, `${JSON.stringify({ version: 1, shims: normalized }, null, 2)}\n`, "utf8");
|
|
1057
|
+
return manifestPath;
|
|
1058
|
+
}
|
|
1059
|
+
export async function manageLocalShims(options) {
|
|
1060
|
+
const action = options.action ?? "status";
|
|
1061
|
+
const repoPath = resolve(options.repoPath);
|
|
1062
|
+
const repoName = options.repoName ?? resolveRepoName(repoPath);
|
|
1063
|
+
const repoId = options.repoId ?? repoName.toLowerCase().replace(/[^a-z0-9_-]/g, "_");
|
|
1064
|
+
const settings = loadPluginSettings(repoPath, LOCAL_SHIMS_PLUGIN_NAME);
|
|
1065
|
+
const includeConfigShims = settings.includeConfigShims !== false;
|
|
1066
|
+
const configPath = resolve(repoPath, "chomp.config.json");
|
|
1067
|
+
const readConfigText = () => includeConfigShims && existsSync(configPath) ? readFileSync(configPath, "utf8") : null;
|
|
1068
|
+
const readManifestText = () => {
|
|
1069
|
+
const primary = resolve(repoPath, LOCAL_SHIMS_FILE_REL);
|
|
1070
|
+
if (existsSync(primary))
|
|
1071
|
+
return readFileSync(primary, "utf8");
|
|
1072
|
+
const legacy = resolve(repoPath, LEGACY_LOCAL_SHIMS_FILE_REL);
|
|
1073
|
+
return existsSync(legacy) ? readFileSync(legacy, "utf8") : null;
|
|
1074
|
+
};
|
|
1075
|
+
if (action === "suggest") {
|
|
1076
|
+
const owningDuByNodeId = buildOwningDuMap(options.graph.nodes);
|
|
1077
|
+
const unresolvedByNode = computeActionableUnresolvedMap(options.graph);
|
|
1078
|
+
const pkgFilter = options.packageName?.trim();
|
|
1079
|
+
const includeDynamic = options.includeDynamic === true;
|
|
1080
|
+
const combinedByNode = new Map();
|
|
1081
|
+
const seenKeys = new Set();
|
|
1082
|
+
for (const [nodeId, calls] of unresolvedByNode.entries()) {
|
|
1083
|
+
const list = [];
|
|
1084
|
+
for (const c of calls) {
|
|
1085
|
+
const clean = normalizeCallToken(c.name);
|
|
1086
|
+
const dedupeKey = `${nodeId}::${clean}::${c.shapeKey ?? ""}`;
|
|
1087
|
+
if (seenKeys.has(dedupeKey))
|
|
1088
|
+
continue;
|
|
1089
|
+
seenKeys.add(dedupeKey);
|
|
1090
|
+
list.push({
|
|
1091
|
+
name: c.name,
|
|
1092
|
+
...(c.shapeKey ? { shapeKey: c.shapeKey } : {}),
|
|
1093
|
+
resolutionClass: "KNOWABLE_GAP",
|
|
1094
|
+
reason: c.reason,
|
|
1095
|
+
...(c.snippet ? { snippet: c.snippet } : {}),
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
if (list.length > 0)
|
|
1099
|
+
combinedByNode.set(nodeId, list);
|
|
1100
|
+
}
|
|
1101
|
+
for (const edge of options.graph.edges) {
|
|
1102
|
+
if (edge.targetId || !edge.sourceId)
|
|
1103
|
+
continue;
|
|
1104
|
+
if ((edge.scope ?? "USER") !== "USER" || edge.metadata?.crossScopeFiltered)
|
|
1105
|
+
continue;
|
|
1106
|
+
if (!edge.entityType || !CONNECTIVITY_TARGET_TYPES.has(edge.entityType))
|
|
1107
|
+
continue;
|
|
1108
|
+
const cls = classifyCallEdgeResolution(edge);
|
|
1109
|
+
if (cls === "PLATFORM_PRIMITIVE" || cls === "RESOLVED")
|
|
1110
|
+
continue;
|
|
1111
|
+
const importSpec = typeof edge.metadata?.importSpecifier === "string" ? edge.metadata.importSpecifier.trim() : "";
|
|
1112
|
+
const isExternalImportSpec = Boolean(importSpec) &&
|
|
1113
|
+
!importSpec.startsWith(".") &&
|
|
1114
|
+
!importSpec.startsWith("/") &&
|
|
1115
|
+
!importSpec.startsWith("#") &&
|
|
1116
|
+
!importSpec.startsWith("node:");
|
|
1117
|
+
const isExternalUnhandled = edge.metadata?.unresolvedReason === "external-package" ||
|
|
1118
|
+
(isExternalImportSpec && cls === "KNOWABLE_GAP");
|
|
1119
|
+
if (!includeDynamic && !isExternalUnhandled)
|
|
1120
|
+
continue;
|
|
1121
|
+
const clean = normalizeCallToken(edge.name);
|
|
1122
|
+
if (!clean)
|
|
1123
|
+
continue;
|
|
1124
|
+
const shapeKey = typeof edge.metadata?.shapeKey === "string" ? edge.metadata.shapeKey : undefined;
|
|
1125
|
+
const dedupeKey = `${edge.sourceId}::${clean}::${shapeKey ?? ""}`;
|
|
1126
|
+
if (seenKeys.has(dedupeKey))
|
|
1127
|
+
continue;
|
|
1128
|
+
seenKeys.add(dedupeKey);
|
|
1129
|
+
const ev = primaryEvidence(edge.evidence);
|
|
1130
|
+
const list = combinedByNode.get(edge.sourceId) ?? [];
|
|
1131
|
+
list.push({
|
|
1132
|
+
name: edge.entityType === "RENDERS" ? clean : `${clean}()`,
|
|
1133
|
+
...(shapeKey ? { shapeKey } : {}),
|
|
1134
|
+
resolutionClass: cls,
|
|
1135
|
+
reason: isExternalUnhandled
|
|
1136
|
+
? "external-package"
|
|
1137
|
+
: cls === "DYNAMIC_BOUNDARY"
|
|
1138
|
+
? "dynamic-boundary"
|
|
1139
|
+
: "unresolved-target",
|
|
1140
|
+
...(ev?.snippet ? { snippet: ev.snippet } : {}),
|
|
1141
|
+
});
|
|
1142
|
+
combinedByNode.set(edge.sourceId, list);
|
|
1143
|
+
}
|
|
1144
|
+
const callableTargets = options.graph.nodes.filter((n) => (n.scope ?? "USER") === "USER" &&
|
|
1145
|
+
(n.type === "CONTRACT" || n.type === "OPEN_CONNECTOR" || n.entityType === "CLASS") &&
|
|
1146
|
+
n.entityType !== "DEPLOYMENT_UNIT");
|
|
1147
|
+
const rawSuggestions = [];
|
|
1148
|
+
for (const [nodeId, calls] of combinedByNode.entries()) {
|
|
1149
|
+
const srcNode = options.graph.nodes.find((n) => n.id === nodeId);
|
|
1150
|
+
if (!srcNode)
|
|
1151
|
+
continue;
|
|
1152
|
+
const du = owningDuByNodeId.get(nodeId);
|
|
1153
|
+
const pkgLabel = du ? stripNodePrefix(du.name) : undefined;
|
|
1154
|
+
const srcEv = primaryEvidence(srcNode.evidence);
|
|
1155
|
+
if (pkgFilter && !packageMatchesFilter(pkgLabel ?? "", srcEv?.filePath, pkgFilter)) {
|
|
1156
|
+
continue;
|
|
1157
|
+
}
|
|
1158
|
+
for (const call of calls) {
|
|
1159
|
+
const cleanCall = normalizeCallToken(call.name);
|
|
1160
|
+
const leafToken = cleanCall.split(".").pop()?.toLowerCase() ?? cleanCall.toLowerCase();
|
|
1161
|
+
const candidateTargets = leafToken.length >= 3
|
|
1162
|
+
? callableTargets
|
|
1163
|
+
.filter((t) => {
|
|
1164
|
+
if (t.id === srcNode.id)
|
|
1165
|
+
return false;
|
|
1166
|
+
const stripped = stripNodePrefix(t.name).toLowerCase();
|
|
1167
|
+
const methodTail = stripped.split(".").pop() ?? stripped;
|
|
1168
|
+
return methodTail === leafToken || stripped === leafToken;
|
|
1169
|
+
})
|
|
1170
|
+
.slice(0, 5)
|
|
1171
|
+
: [];
|
|
1172
|
+
const topTarget = candidateTargets[0];
|
|
1173
|
+
rawSuggestions.push({
|
|
1174
|
+
suggestedShim: {
|
|
1175
|
+
sourceId: srcNode.id,
|
|
1176
|
+
sourceName: srcNode.name,
|
|
1177
|
+
...(srcEv?.filePath ? { sourceFile: srcEv.filePath } : {}),
|
|
1178
|
+
callName: cleanCall,
|
|
1179
|
+
...(call.shapeKey ? { shapeKey: call.shapeKey } : {}),
|
|
1180
|
+
...(topTarget
|
|
1181
|
+
? { targetId: topTarget.id, targetName: topTarget.name }
|
|
1182
|
+
: {
|
|
1183
|
+
targetConnector: {
|
|
1184
|
+
name: `External Call: ${cleanCall}`,
|
|
1185
|
+
entityType: "EXTERNAL_SDK_CALL",
|
|
1186
|
+
},
|
|
1187
|
+
}),
|
|
1188
|
+
entityType: "CALL",
|
|
1189
|
+
rationale: topTarget
|
|
1190
|
+
? `Resolves dynamic/indirect call '${call.name}' to '${topTarget.name}'`
|
|
1191
|
+
: `Fill in targetId/targetName (via chomp_get_nodes) or targetConnector for '${call.name}'`,
|
|
1192
|
+
},
|
|
1193
|
+
sourceNode: {
|
|
1194
|
+
id: srcNode.id,
|
|
1195
|
+
name: srcNode.name,
|
|
1196
|
+
entityType: srcNode.entityType,
|
|
1197
|
+
...(pkgLabel ? { packageName: pkgLabel } : {}),
|
|
1198
|
+
...(srcEv?.filePath ? { filePath: srcEv.filePath } : {}),
|
|
1199
|
+
...(srcEv?.lineNumber ? { lineNumber: srcEv.lineNumber } : {}),
|
|
1200
|
+
},
|
|
1201
|
+
unresolvedCall: {
|
|
1202
|
+
name: call.name,
|
|
1203
|
+
...(call.shapeKey ? { shapeKey: call.shapeKey } : {}),
|
|
1204
|
+
resolutionClass: call.resolutionClass,
|
|
1205
|
+
reason: call.reason,
|
|
1206
|
+
...(call.snippet ? { snippet: call.snippet } : {}),
|
|
1207
|
+
},
|
|
1208
|
+
candidateTargets: candidateTargets.map((t) => {
|
|
1209
|
+
const tDu = owningDuByNodeId.get(t.id);
|
|
1210
|
+
const tEv = primaryEvidence(t.evidence);
|
|
1211
|
+
return {
|
|
1212
|
+
id: t.id,
|
|
1213
|
+
name: t.name,
|
|
1214
|
+
entityType: t.entityType,
|
|
1215
|
+
...(tDu ? { packageName: stripNodePrefix(tDu.name) } : {}),
|
|
1216
|
+
...(tEv?.filePath ? { filePath: tEv.filePath } : {}),
|
|
1217
|
+
};
|
|
1218
|
+
}),
|
|
1219
|
+
guidance: "If this call pattern repeats across multiple files (e.g. a custom router, wrapper, or ORM helper), call `chomp_get_conventions` and declare a `chomp.config.json#conventions` rule instead of per-call shims. Use `chomp_local_shims` (`action: 'upsert'`) for true runtime DI, dynamic dispatch, or one-off external exits.",
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
// Prioritize high-signal intra-repo candidate matches first, then KNOWABLE_GAP, then DYNAMIC_BOUNDARY.
|
|
1224
|
+
rawSuggestions.sort((a, b) => {
|
|
1225
|
+
const aHasTarget = a.candidateTargets.length > 0 ? 1 : 0;
|
|
1226
|
+
const bHasTarget = b.candidateTargets.length > 0 ? 1 : 0;
|
|
1227
|
+
if (bHasTarget !== aHasTarget)
|
|
1228
|
+
return bHasTarget - aHasTarget;
|
|
1229
|
+
const aKnowable = a.unresolvedCall.resolutionClass === "KNOWABLE_GAP" ? 1 : 0;
|
|
1230
|
+
const bKnowable = b.unresolvedCall.resolutionClass === "KNOWABLE_GAP" ? 1 : 0;
|
|
1231
|
+
return bKnowable - aKnowable;
|
|
1232
|
+
});
|
|
1233
|
+
const suggestions = rawSuggestions.slice(0, 40);
|
|
1234
|
+
return {
|
|
1235
|
+
action: "suggest",
|
|
1236
|
+
manifestPath: LOCAL_SHIMS_FILE_REL,
|
|
1237
|
+
totalSuggestions: suggestions.length,
|
|
1238
|
+
suggestions,
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
if (action === "validate") {
|
|
1242
|
+
const incoming = (options.shims ?? [])
|
|
1243
|
+
.map((s) => normalizeShimEntry(s, "shims.json"))
|
|
1244
|
+
.filter((s) => s !== null);
|
|
1245
|
+
const { resolved, unmatched } = evaluateShimsAgainstGraph(options.graph, incoming);
|
|
1246
|
+
return {
|
|
1247
|
+
action: "validate",
|
|
1248
|
+
valid: unmatched.length === 0 && incoming.length === (options.shims?.length ?? 0),
|
|
1249
|
+
submittedCount: options.shims?.length ?? 0,
|
|
1250
|
+
resolvedCount: resolved.length,
|
|
1251
|
+
unmatchedCount: unmatched.length,
|
|
1252
|
+
resolved: resolved.map((r) => ({
|
|
1253
|
+
id: r.shim.id,
|
|
1254
|
+
sourceId: r.sourceNode.id,
|
|
1255
|
+
sourceName: r.sourceNode.name,
|
|
1256
|
+
targetId: r.targetNode.id,
|
|
1257
|
+
targetName: r.targetNode.name,
|
|
1258
|
+
resolvedEdgeCount: r.resolvedEdgeIds.length,
|
|
1259
|
+
syntheticConnector: Boolean(r.syntheticConnector),
|
|
1260
|
+
})),
|
|
1261
|
+
unmatched: unmatched.map((u) => ({ id: u.shim.id, reason: u.reason })),
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
if (action === "upsert") {
|
|
1265
|
+
const incoming = options.shims ?? [];
|
|
1266
|
+
if (incoming.length === 0) {
|
|
1267
|
+
throw new Error("`shims` array must contain at least one shim entry when action is 'upsert'.");
|
|
1268
|
+
}
|
|
1269
|
+
const { manifest } = readShimsManifestFile(repoPath);
|
|
1270
|
+
const byId = new Map();
|
|
1271
|
+
for (const existing of manifest.shims) {
|
|
1272
|
+
byId.set(deriveShimId(existing), { ...existing, id: deriveShimId(existing) });
|
|
1273
|
+
}
|
|
1274
|
+
for (const next of incoming) {
|
|
1275
|
+
const normalized = normalizeShimEntry(next, "shims.json");
|
|
1276
|
+
if (!normalized) {
|
|
1277
|
+
throw new Error("Invalid shim entry: every shim must specify (`sourceId` or `sourceName`) and (`targetId`, `targetName`, or `targetConnector`).");
|
|
1278
|
+
}
|
|
1279
|
+
const { origin: _drop, ...clean } = normalized;
|
|
1280
|
+
byId.set(normalized.id, clean);
|
|
1281
|
+
}
|
|
1282
|
+
const updatedList = [...byId.values()];
|
|
1283
|
+
const writtenPath = writeShimsManifestFile(repoPath, updatedList);
|
|
1284
|
+
const pluginRun = await runChompPlugin(getChompPlugin(LOCAL_SHIMS_PLUGIN_NAME) ?? localShimsPlugin, {
|
|
1285
|
+
repoPath,
|
|
1286
|
+
repoId,
|
|
1287
|
+
repoName,
|
|
1288
|
+
storage: options.storage,
|
|
1289
|
+
graph: options.graph,
|
|
1290
|
+
persist: Boolean(options.storage),
|
|
1291
|
+
});
|
|
1292
|
+
return {
|
|
1293
|
+
action: "upsert",
|
|
1294
|
+
manifestPath: LOCAL_SHIMS_FILE_REL,
|
|
1295
|
+
absoluteManifestPath: writtenPath,
|
|
1296
|
+
manifestFileHash: computeFileOverlayHash(repoPath, LOCAL_SHIMS_FILE_REL)?.hash ?? null,
|
|
1297
|
+
upsertedCount: incoming.length,
|
|
1298
|
+
totalManifestShims: updatedList.length,
|
|
1299
|
+
pluginRun: pluginRun.summary,
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
if (action === "remove") {
|
|
1303
|
+
const idsToRemove = new Set((options.shimIds ?? []).map((id) => id.trim()).filter(Boolean));
|
|
1304
|
+
if (!options.clearAll && idsToRemove.size === 0) {
|
|
1305
|
+
throw new Error("Pass `shimIds` (or `clearAll: true`) when action is 'remove'.");
|
|
1306
|
+
}
|
|
1307
|
+
const { manifest } = readShimsManifestFile(repoPath);
|
|
1308
|
+
const beforeCount = manifest.shims.length;
|
|
1309
|
+
const remaining = options.clearAll
|
|
1310
|
+
? []
|
|
1311
|
+
: manifest.shims.filter((s) => !idsToRemove.has(deriveShimId(s)));
|
|
1312
|
+
writeShimsManifestFile(repoPath, remaining);
|
|
1313
|
+
const pluginRun = await runChompPlugin(getChompPlugin(LOCAL_SHIMS_PLUGIN_NAME) ?? localShimsPlugin, {
|
|
1314
|
+
repoPath,
|
|
1315
|
+
repoId,
|
|
1316
|
+
repoName,
|
|
1317
|
+
storage: options.storage,
|
|
1318
|
+
graph: options.graph,
|
|
1319
|
+
persist: Boolean(options.storage),
|
|
1320
|
+
});
|
|
1321
|
+
return {
|
|
1322
|
+
action: "remove",
|
|
1323
|
+
manifestPath: LOCAL_SHIMS_FILE_REL,
|
|
1324
|
+
removedCount: beforeCount - remaining.length,
|
|
1325
|
+
remainingManifestShims: remaining.length,
|
|
1326
|
+
pluginRun: pluginRun.summary,
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
if (action === "run") {
|
|
1330
|
+
const pluginRun = await runChompPlugin(getChompPlugin(LOCAL_SHIMS_PLUGIN_NAME) ?? localShimsPlugin, {
|
|
1331
|
+
repoPath,
|
|
1332
|
+
repoId,
|
|
1333
|
+
repoName,
|
|
1334
|
+
storage: options.storage,
|
|
1335
|
+
graph: options.graph,
|
|
1336
|
+
persist: Boolean(options.storage),
|
|
1337
|
+
});
|
|
1338
|
+
return {
|
|
1339
|
+
action: "run",
|
|
1340
|
+
manifestPath: LOCAL_SHIMS_FILE_REL,
|
|
1341
|
+
overlayCount: pluginRun.overlayCount,
|
|
1342
|
+
summary: pluginRun.summary,
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
// Default: action === "status"
|
|
1346
|
+
const { shims, parseErrors } = loadLocalShimsFromTexts(readManifestText(), readConfigText());
|
|
1347
|
+
const { resolved, unmatched } = evaluateShimsAgainstGraph(options.graph, shims);
|
|
1348
|
+
const rawUnresolved = computeActionableUnresolvedMap(options.graph);
|
|
1349
|
+
const provisionalOverlays = resolved.map((r) => ({
|
|
1350
|
+
entityId: r.sourceNode.id,
|
|
1351
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
1352
|
+
payload: {
|
|
1353
|
+
shimmedEdges: [r.edgeEntry],
|
|
1354
|
+
...(r.syntheticConnector ? { syntheticConnectors: [r.syntheticConnector] } : {}),
|
|
1355
|
+
},
|
|
1356
|
+
}));
|
|
1357
|
+
const projectedGraph = applyShimOverlaysToGraph(options.graph, provisionalOverlays, { enabled: true });
|
|
1358
|
+
const postUnresolved = computeActionableUnresolvedMap(projectedGraph);
|
|
1359
|
+
const rawGapCount = [...rawUnresolved.values()].reduce((acc, l) => acc + l.length, 0);
|
|
1360
|
+
const remainingGapCount = [...postUnresolved.values()].reduce((acc, l) => acc + l.length, 0);
|
|
1361
|
+
return {
|
|
1362
|
+
action: "status",
|
|
1363
|
+
layer: SHIM_OVERLAY_LAYER,
|
|
1364
|
+
pluginName: LOCAL_SHIMS_PLUGIN_NAME,
|
|
1365
|
+
manifestPath: LOCAL_SHIMS_FILE_REL,
|
|
1366
|
+
manifestExists: existsSync(resolve(repoPath, LOCAL_SHIMS_FILE_REL)) ||
|
|
1367
|
+
existsSync(resolve(repoPath, LEGACY_LOCAL_SHIMS_FILE_REL)),
|
|
1368
|
+
configuredShimCount: shims.length,
|
|
1369
|
+
activeShimCount: resolved.length,
|
|
1370
|
+
unmatchedShimCount: unmatched.length,
|
|
1371
|
+
parseErrors,
|
|
1372
|
+
rawUnresolvedCallCount: rawGapCount,
|
|
1373
|
+
bridgedCallCount: Math.max(0, rawGapCount - remainingGapCount),
|
|
1374
|
+
remainingUnresolvedCallCount: remainingGapCount,
|
|
1375
|
+
activeShims: resolved.map((r) => ({
|
|
1376
|
+
id: r.shim.id,
|
|
1377
|
+
origin: r.shim.origin,
|
|
1378
|
+
sourceId: r.sourceNode.id,
|
|
1379
|
+
sourceName: r.sourceNode.name,
|
|
1380
|
+
targetId: r.targetNode.id,
|
|
1381
|
+
targetName: r.targetNode.name,
|
|
1382
|
+
entityType: r.edgeEntry.entityType,
|
|
1383
|
+
resolvedEdgeCount: r.resolvedEdgeIds.length,
|
|
1384
|
+
rationale: r.shim.rationale ?? null,
|
|
1385
|
+
})),
|
|
1386
|
+
unmatchedShims: unmatched.map((u) => ({
|
|
1387
|
+
id: u.shim.id,
|
|
1388
|
+
origin: u.shim.origin,
|
|
1389
|
+
reason: u.reason,
|
|
1390
|
+
})),
|
|
1391
|
+
};
|
|
1392
|
+
}
|