Repository navigation
Expand file tree
/
Copy pathnode.ts
More file actions
508 lines (463 loc) · 19.4 KB
/
Copy pathnode.ts
File metadata and controls
508 lines (463 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**
* `GET /api/node/<id>` — everything the Symbol view draws, in one round-trip.
*
* The Symbol view is three panes and a strip: callers on the left, the verbatim
* body in the middle with a port per call site, callees on the right anchored
* to those lines, and a blast-radius summary underneath. Splitting that across
* five endpoints would mean five waterfalls before the screen settles, and the
* screen is the product. So this endpoint answers all of it.
*
* Two properties it has to hold, and the reasons they are not obvious:
*
* **No N+1, anywhere.** The engine's own busiest symbol has 545 incoming edges.
* Resolving those one `getNode` at a time is 545 queries and blows the budget on
* its own; so every edge list is resolved with one batched `getNodesByIds`, and
* fan-in for the rail pills comes from one batched `getFanIn`.
*
* **Capped lists that still tell the truth.** 545 callers cannot all be rows,
* but the payload must never suggest there are fewer. Every list carries the
* true `total` beside the `shown` slice, and the ordering is chosen so the
* slice is the useful end: same file first, then production code, then tests.
*/
import type { CodeGraph } from '../../index';
import type { Edge, Node, NodeKind } from '../../types';
import { isTestFile } from '../../search/query-utils';
import { buildHierarchy, type WireOverride } from './hierarchy';
import { notFound } from './respond';
import { findIndexedFile, hasDriftedOnDisk } from './source';
import { annotateWhen } from './when';
import {
BLAST_DEPTH,
CALLER_EDGE_KINDS,
CONTAINER_KINDS,
HUB_THRESHOLD,
MAX_INCOMING_GROUPS,
MAX_OUTGOING_GROUPS,
MAX_OUTLINE_NODES,
MAX_OUTSIDE_INDEX_SAMPLES,
MAX_TEST_FILES,
TEST_CALLER_BUDGET,
TEST_CALLER_HOPS,
TYPE_KINDS,
firstLine,
groupRelations,
toNodeDetail,
toNodeRef,
toPosixPath,
wireList,
type WireNodeRef,
} from './wire';
/** A member row in the focal symbol's outline, with its place in the tree. */
export interface WireMember extends WireNodeRef {
/** The container this member belongs to — the focal node, or one of its children. */
parentId: string;
/** 1 = direct member, 2 = a member of a member (a class's method inside a file). */
depth: number;
/**
* Edges in and out of this member — the outline's `← in → out` columns.
*
* A container's own fan-out is usually zero (a class calls nothing; its
* methods do), so without these an outline of a 700-line class says nothing
* about which member is load-bearing and which is a getter. Edge counts, not
* distinct counterparts: the column is a weight, and it sits beside a
* signature rather than beside a caller list it could contradict.
*/
fanIn: number;
fanOut: number;
/**
* This member redeclares one an ancestor type declares — a name match inside
* a chain the graph already links, not an `overrides` edge (nothing emits
* one). Absent for every member that declares something new.
*/
overrides?: WireOverride;
}
export async function buildNode(cg: CodeGraph, projectRoot: string, nodeId: string): Promise<unknown> {
const node = cg.getNode(nodeId);
if (!node) {
throw notFound(
'No symbol with that id is in this index.',
'Symbol ids change whenever the file is re-indexed — search for the symbol by ' +
'name instead of reusing an id from an older session.'
);
}
const incomingAll = cg.getIncomingEdges(nodeId);
const outgoingAll = cg.getOutgoingEdges(nodeId);
// `contains` is structure, not dependency: upward it is the parent (already in
// `ancestors`), downward it is the members outline. Leaving it in the rails
// would put a symbol's own class in its caller list.
const incoming = incomingAll.filter((e) => e.kind !== 'contains');
const outgoingRest: Edge[] = [];
const containsOut: Edge[] = [];
for (const edge of outgoingAll) {
if (edge.kind === 'contains') containsOut.push(edge);
else outgoingRest.push(edge);
}
const ancestors = cg.getAncestors(nodeId);
// ---------------------------------------------------------------------------
// One batched resolve for every endpoint this payload names.
// ---------------------------------------------------------------------------
const endpointIds = new Set<string>();
for (const edge of incoming) endpointIds.add(edge.source);
for (const edge of outgoingRest) endpointIds.add(edge.target);
for (const edge of containsOut) endpointIds.add(edge.target);
const endpoints = cg.getNodesByIds([...endpointIds]);
// A `references` edge into a type is "uses type X", not "calls X" — the
// header shows those as chips rather than as callee rows. Split at the EDGE
// level so a class that is both instantiated and named as a type appears in
// both places, which is what the source actually says.
const calleeEdges: Edge[] = [];
const typeRefs: Edge[] = [];
for (const edge of outgoingRest) {
const target = endpoints.get(edge.target);
if (edge.kind === 'references' && target && TYPE_KINDS.has(target.kind)) typeRefs.push(edge);
else calleeEdges.push(edge);
}
// ---------------------------------------------------------------------------
// Rails
// ---------------------------------------------------------------------------
const focalFile = toPosixPath(node.filePath);
const incomingGroups = groupRelations(incoming, (e) => e.source, endpoints);
incomingGroups.sort((a, b) => {
// The symbol's own file first ("same file" in the left rail), then
// production code, then tests — so a cap trims the least useful end.
const aSame = a.node.file === focalFile ? 0 : 1;
const bSame = b.node.file === focalFile ? 0 : 1;
if (aSame !== bSame) return aSame - bSame;
if (a.node.test !== b.node.test) return a.node.test ? 1 : -1;
return a.node.file.localeCompare(b.node.file) || firstLine(a) - firstLine(b);
});
const outgoingGroups = groupRelations(calleeEdges, (e) => e.target, endpoints);
// The right rail is line-anchored: rows sit beside the line that calls them.
outgoingGroups.sort((a, b) => firstLine(a) - firstLine(b) || a.node.name.localeCompare(b.node.name));
const typeGroups = groupRelations(typeRefs, (e) => e.target, endpoints);
typeGroups.sort((a, b) => firstLine(a) - firstLine(b) || a.node.name.localeCompare(b.node.name));
const shownIncoming = incomingGroups.slice(0, MAX_INCOMING_GROUPS);
const shownOutgoing = outgoingGroups.slice(0, MAX_OUTGOING_GROUPS);
// Branch conditions per call site: the right rail's sites are all in this
// file; the left rail's are in each caller's own file.
await annotateWhen(cg, projectRoot, [
{ file: focalFile, edges: shownOutgoing.flatMap((r) => r.edges) },
...shownIncoming.map((r) => ({ file: r.node.file, edges: r.edges })),
]);
// Fan-in for the rail pills ("hub · N"), for the rows actually returned —
// one query, not one per row.
const fanInOf = cg.getFanIn([
...shownIncoming.map((r) => r.node.id),
...shownOutgoing.map((r) => r.node.id),
...typeGroups.map((r) => r.node.id),
]);
for (const relation of [...shownIncoming, ...shownOutgoing, ...typeGroups]) {
const count = fanInOf.get(relation.node.id) ?? 0;
relation.fanIn = count;
relation.hub = count >= HUB_THRESHOLD;
}
// ---------------------------------------------------------------------------
// Members outline
// ---------------------------------------------------------------------------
// The type hierarchy, and the override marks it puts on the outline. Gated
// to types inside `buildHierarchy`, so a function costs one kind test.
const hierarchy = buildHierarchy(cg, node);
const members = buildMembers(cg, node, containsOut, endpoints, hierarchy?.overrides);
// ---------------------------------------------------------------------------
// Counts, tests, what leaves the index, blast radius
// ---------------------------------------------------------------------------
const directCallers: Node[] = [];
const seenCaller = new Set<string>();
for (const edge of incoming) {
if (!CALLER_EDGE_KINDS.has(edge.kind) || seenCaller.has(edge.source)) continue;
seenCaller.add(edge.source);
const source = endpoints.get(edge.source);
if (source) directCallers.push(source);
}
const drift = driftFor(cg, projectRoot, node.filePath);
return {
node: toNodeDetail(node),
/** Outermost first: file, then module/class, then the symbol's own parent. */
ancestors: [...ancestors].reverse().map(toNodeRef),
members: wireList(members.items, members.total),
/**
* Ancestors, subtypes and the dispatch fan — `null` for anything that is
* not a type, and for a type with no hierarchy at all.
*/
hierarchy: hierarchy?.wire ?? null,
incoming: wireList(shownIncoming, incomingGroups.length),
outgoing: wireList(shownOutgoing, outgoingGroups.length),
/** `references` edges into a type — the header's "uses types …" chips. */
typesUsed: typeGroups,
counts: {
// Every count below is the length of a list this payload also returns, so
// a badge and the rail beneath it can never disagree.
/** Distinct symbols that reach this one — `incoming.total`. Drives `hub`. */
callers: incomingGroups.length,
/** Distinct symbols this one calls — `outgoing.total`. Types are counted separately. */
callees: outgoingGroups.length,
/** Distinct types this symbol names — `typesUsed.length`. */
typesUsed: typeGroups.length,
/** EDGE counts, which run higher: one caller can call from many lines. */
fanIn: incoming.length,
fanOut: outgoingRest.length,
members: members.total,
hub: incomingGroups.length >= HUB_THRESHOLD,
},
tests: summarizeTestCallers(cg, directCallers),
outsideIndex: summarizeOutsideIndex(cg, nodeId),
blast: summarizeBlast(cg, node, incomingGroups.length),
/** The symbol's file changed on disk since the index — line ranges may be shifted. */
drift,
};
}
// =============================================================================
// Members
// =============================================================================
/**
* The focal symbol's members, in source order, one level of nesting deep.
*
* A file's outline is file → class → method, so direct children alone would
* show a class and nothing inside it. The grandchildren come from ONE batched
* `getOutgoingEdgesFrom` over the container children, never a query per child.
*/
function buildMembers(
cg: CodeGraph,
focal: Node,
containsOut: readonly Edge[],
endpoints: Map<string, Node>,
overrides?: Map<string, WireOverride>
): { items: WireMember[]; total: number } {
// Each member once, and never the symbol itself or a file: a component in a
// Vue / Svelte / Astro file was recorded as containing its script's file node
// and every symbol in it, so the walk one level down met those symbols
// again — and a member listed twice is a repeated key the viewer cannot draw.
const seen = new Set<string>([focal.id]);
const admit = (child: Node | undefined): child is Node =>
!!child && child.kind !== 'file' && !seen.has(child.id) && !!seen.add(child.id);
const direct: Array<{ node: Node; parentId: string; depth: number }> = [];
for (const edge of containsOut) {
const child = endpoints.get(edge.target);
if (admit(child)) direct.push({ node: child, parentId: focal.id, depth: 1 });
}
const containerIds = direct
.filter((entry) => CONTAINER_KINDS.has(entry.node.kind))
.map((entry) => entry.node.id);
const nested: Array<{ node: Node; parentId: string; depth: number }> = [];
if (containerIds.length > 0) {
const grandEdges = cg.getOutgoingEdgesFrom(containerIds, ['contains']);
const grandNodes = cg.getNodesByIds(grandEdges.map((e) => e.target));
for (const edge of grandEdges) {
const child = grandNodes.get(edge.target);
if (admit(child)) nested.push({ node: child, parentId: edge.source, depth: 2 });
}
}
const all = [...direct, ...nested].sort(
(a, b) => a.node.startLine - b.node.startLine || a.node.name.localeCompare(b.node.name)
);
const shown = all.slice(0, MAX_OUTLINE_NODES);
// Two queries for the whole outline, not two per row: a file with 400
// symbols would otherwise be 800 lookups behind one screen.
const memberIds = shown.map((entry) => entry.node.id);
const fanIn = cg.getFanIn(memberIds);
const fanOut = cg.getFanOut(memberIds);
return {
items: shown.map((entry) => {
const member: WireMember = {
...toNodeRef(entry.node),
parentId: entry.parentId,
depth: entry.depth,
fanIn: fanIn.get(entry.node.id) ?? 0,
fanOut: fanOut.get(entry.node.id) ?? 0,
};
const override = overrides?.get(entry.node.id);
if (override) member.overrides = override;
return member;
}),
total: all.length,
};
}
// =============================================================================
// Test coverage
// =============================================================================
export interface WireTestSummary {
/** A test file reaches this symbol within {@link TEST_CALLER_HOPS} caller hops. */
reached: boolean;
/** How many hops away the nearest test was. 1 = a test calls it directly. */
hops: number | null;
fileCount: number;
files: string[];
/**
* The search finished rather than running out of budget. `false` weakens the
* claim from "no test reaches this within 3 hops" to "no test calls this
* directly", which is all that was actually checked.
*/
exhaustive: boolean;
hopsSearched: number;
}
/**
* Which tests reach this symbol — the same question, and the same method,
* behind `codegraph_explore`'s "tests:" line.
*
* Direct test callers first; failing that, walk up to two more caller hops,
* because a helper called only by production code is still tested through
* whatever calls it. The budget bounds a god-symbol, and running out of it is
* reported rather than papered over: claiming "no test reaches this" after an
* incomplete search would be exactly the kind of confident wrong answer the
* viewer exists to avoid.
*/
function summarizeTestCallers(cg: CodeGraph, directCallers: readonly Node[]): WireTestSummary {
const directFiles = [
...new Set(directCallers.map((n) => toPosixPath(n.filePath)).filter(isTestFile)),
];
if (directFiles.length > 0) {
return {
reached: true,
hops: 1,
fileCount: directFiles.length,
files: directFiles.slice(0, MAX_TEST_FILES),
exhaustive: true,
hopsSearched: 1,
};
}
let budget = TEST_CALLER_BUDGET;
const visited = new Set(directCallers.map((n) => n.id));
let frontier: Node[] = [...directCallers];
let hopsSearched = 1;
for (let hop = 2; hop <= TEST_CALLER_HOPS && frontier.length > 0 && budget > 0; hop++) {
hopsSearched = hop;
const next: Node[] = [];
const found = new Set<string>();
for (const current of frontier) {
if (budget-- <= 0) break;
let callers: Array<{ node: Node }>;
try {
callers = cg.getCallers(current.id) as Array<{ node: Node }>;
} catch {
continue;
}
for (const caller of callers) {
const source = caller?.node;
if (!source || visited.has(source.id)) continue;
visited.add(source.id);
const file = toPosixPath(source.filePath);
if (isTestFile(file)) found.add(file);
else next.push(source);
}
}
if (found.size > 0) {
const files = [...found];
return {
reached: true,
hops: hop,
fileCount: files.length,
files: files.slice(0, MAX_TEST_FILES),
exhaustive: true,
hopsSearched: hop,
};
}
frontier = next;
}
return {
reached: false,
hops: null,
fileCount: 0,
files: [],
exhaustive: budget > 0,
hopsSearched,
};
}
// =============================================================================
// References that leave the index
// =============================================================================
/**
* Calls and type mentions from this symbol that never resolved to a node — a
* third-party package, a runtime builtin, a construct extraction doesn't model.
*
* Without this the callee rail would silently be shorter than the body's call
* sites, which reads as "nothing else happens here". Saying "+N calls into
* symbols outside the index" is the honest version of the same screen.
*/
function summarizeOutsideIndex(
cg: CodeGraph,
nodeId: string
): {
total: number;
byKind: Record<string, number>;
samples: Array<{ name: string; kind: string; line: number; col: number }>;
} {
let refs;
try {
refs = cg.getUnresolvedReferencesFrom(nodeId);
} catch {
return { total: 0, byKind: {}, samples: [] };
}
const byKind: Record<string, number> = {};
for (const ref of refs) byKind[ref.referenceKind] = (byKind[ref.referenceKind] ?? 0) + 1;
const samples = [...refs]
.sort((a, b) => a.line - b.line || a.column - b.column)
.slice(0, MAX_OUTSIDE_INDEX_SAMPLES)
.map((ref) => ({
name: ref.referenceName,
kind: ref.referenceKind,
line: ref.line,
col: ref.column,
}));
return { total: refs.length, byKind, samples };
}
// =============================================================================
// Blast radius
// =============================================================================
export interface WireBlastSummary {
/** Distinct symbols that depend on this one directly. */
direct: number;
/** Distinct symbols reached within {@link BLAST_DEPTH} dependency hops. */
withinHops: number;
hops: number;
files: number;
testFiles: number;
routes: number;
/** Up to 40 of the dependent files, most-affected first, for the "what would need re-checking" fold. */
topFiles: Array<{ file: string; symbols: number; test: boolean }>;
}
/**
* What would need re-checking if this symbol changed.
*
* `getImpactRadius` at depth 3 is the engine's own answer to that question —
* incoming dependencies only, `contains` excluded upward so a leaf symbol does
* not explode into its whole class, container members expanded downward so
* callers of a class's methods count against the class.
*/
function summarizeBlast(cg: CodeGraph, node: Node, direct: number): WireBlastSummary | null {
let subgraph;
try {
subgraph = cg.getImpactRadius(node.id, BLAST_DEPTH);
} catch {
return null;
}
const perFile = new Map<string, number>();
let routes = 0;
for (const [id, dependent] of subgraph.nodes) {
if (id === node.id) continue;
const file = toPosixPath(dependent.filePath);
perFile.set(file, (perFile.get(file) ?? 0) + 1);
if (dependent.kind === ('route' as NodeKind)) routes++;
}
const testFiles = [...perFile.keys()].filter(isTestFile).length;
const topFiles = [...perFile.entries()]
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
.slice(0, 40)
.map(([file, symbols]) => ({ file, symbols, test: isTestFile(file) }));
return {
direct,
withinHops: Math.max(0, subgraph.nodes.size - 1),
hops: BLAST_DEPTH,
files: perFile.size,
testFiles,
routes,
topFiles,
};
}
// =============================================================================
// Drift
// =============================================================================
function driftFor(cg: CodeGraph, projectRoot: string, filePath: string): boolean {
const found = findIndexedFile(cg, filePath);
if (!found) return false;
return hasDriftedOnDisk(projectRoot, found.storedPath, found.record);
}