@ttsc/graph 0.16.4 → 0.16.5

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 (109) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +24 -53
  3. package/lib/TtscGraphApplication.js +46 -0
  4. package/lib/TtscGraphApplication.js.map +1 -0
  5. package/lib/index.js +31 -108
  6. package/lib/index.js.map +1 -1
  7. package/lib/model/TtscGraphMemory.js +185 -0
  8. package/lib/model/TtscGraphMemory.js.map +1 -0
  9. package/lib/model/loadGraph.js +418 -0
  10. package/lib/model/loadGraph.js.map +1 -0
  11. package/lib/reduce.js +101 -0
  12. package/lib/reduce.js.map +1 -0
  13. package/lib/resolveGraphBinary.js +42 -0
  14. package/lib/resolveGraphBinary.js.map +1 -0
  15. package/lib/server/createServer.js +1227 -0
  16. package/lib/server/createServer.js.map +1 -0
  17. package/lib/server/instructions.js +32 -0
  18. package/lib/server/instructions.js.map +1 -0
  19. package/lib/server/resolveHandle.js +25 -0
  20. package/lib/server/resolveHandle.js.map +1 -0
  21. package/lib/server/runExpand.js +177 -0
  22. package/lib/server/runExpand.js.map +1 -0
  23. package/lib/server/runIndex.js +146 -0
  24. package/lib/server/runIndex.js.map +1 -0
  25. package/lib/server/runOverview.js +150 -0
  26. package/lib/server/runOverview.js.map +1 -0
  27. package/lib/server/runQuery.js +147 -0
  28. package/lib/server/runQuery.js.map +1 -0
  29. package/lib/server/runTrace.js +219 -0
  30. package/lib/server/runTrace.js.map +1 -0
  31. package/lib/server/startServer.js +21 -0
  32. package/lib/server/startServer.js.map +1 -0
  33. package/lib/structures/ITtscGraphApplication.js +3 -0
  34. package/lib/structures/ITtscGraphApplication.js.map +1 -0
  35. package/lib/structures/ITtscGraphDecorator.js +3 -0
  36. package/lib/structures/ITtscGraphDecorator.js.map +1 -0
  37. package/lib/structures/ITtscGraphDiagnostic.js +3 -0
  38. package/lib/structures/ITtscGraphDiagnostic.js.map +1 -0
  39. package/lib/structures/ITtscGraphDump.js +3 -0
  40. package/lib/structures/ITtscGraphDump.js.map +1 -0
  41. package/lib/structures/ITtscGraphEdge.js +3 -0
  42. package/lib/structures/ITtscGraphEdge.js.map +1 -0
  43. package/lib/structures/ITtscGraphEvidence.js +3 -0
  44. package/lib/structures/ITtscGraphEvidence.js.map +1 -0
  45. package/lib/structures/ITtscGraphExpand.js +3 -0
  46. package/lib/structures/ITtscGraphExpand.js.map +1 -0
  47. package/lib/structures/ITtscGraphIndex.js +3 -0
  48. package/lib/structures/ITtscGraphIndex.js.map +1 -0
  49. package/lib/structures/ITtscGraphNode.js +3 -0
  50. package/lib/structures/ITtscGraphNode.js.map +1 -0
  51. package/lib/structures/ITtscGraphOverview.js +3 -0
  52. package/lib/structures/ITtscGraphOverview.js.map +1 -0
  53. package/lib/structures/ITtscGraphQuery.js +3 -0
  54. package/lib/structures/ITtscGraphQuery.js.map +1 -0
  55. package/lib/structures/ITtscGraphTrace.js +3 -0
  56. package/lib/structures/ITtscGraphTrace.js.map +1 -0
  57. package/lib/structures/TtscGraphEdgeKind.js +3 -0
  58. package/lib/structures/TtscGraphEdgeKind.js.map +1 -0
  59. package/lib/structures/TtscGraphNodeKind.js +3 -0
  60. package/lib/structures/TtscGraphNodeKind.js.map +1 -0
  61. package/lib/structures/TtscGraphNodeModifier.js +3 -0
  62. package/lib/structures/TtscGraphNodeModifier.js.map +1 -0
  63. package/lib/structures/index.js +37 -0
  64. package/lib/structures/index.js.map +1 -0
  65. package/lib/view.js +140 -0
  66. package/lib/view.js.map +1 -0
  67. package/lib/viewer/index.html +123 -0
  68. package/lib/viewer/viewer.js +4610 -0
  69. package/package.json +17 -4
  70. package/src/TtscGraphApplication.ts +57 -0
  71. package/src/index.ts +40 -126
  72. package/src/model/TtscGraphMemory.ts +204 -0
  73. package/src/model/loadGraph.ts +85 -0
  74. package/src/reduce.ts +165 -0
  75. package/src/resolveGraphBinary.ts +40 -0
  76. package/src/server/createServer.ts +104 -0
  77. package/src/server/instructions.ts +28 -0
  78. package/src/server/resolveHandle.ts +34 -0
  79. package/src/server/runExpand.ts +186 -0
  80. package/src/server/runIndex.ts +181 -0
  81. package/src/server/runOverview.ts +154 -0
  82. package/src/server/runQuery.ts +150 -0
  83. package/src/server/runTrace.ts +233 -0
  84. package/src/server/startServer.ts +25 -0
  85. package/src/structures/ITtscGraphApplication.ts +68 -0
  86. package/src/structures/ITtscGraphDecorator.ts +33 -0
  87. package/src/structures/ITtscGraphDiagnostic.ts +34 -0
  88. package/src/structures/ITtscGraphDump.ts +35 -0
  89. package/src/structures/ITtscGraphEdge.ts +24 -0
  90. package/src/structures/ITtscGraphEvidence.ts +29 -0
  91. package/src/structures/ITtscGraphExpand.ts +85 -0
  92. package/src/structures/ITtscGraphIndex.ts +100 -0
  93. package/src/structures/ITtscGraphNode.ts +64 -0
  94. package/src/structures/ITtscGraphOverview.ts +79 -0
  95. package/src/structures/ITtscGraphQuery.ts +49 -0
  96. package/src/structures/ITtscGraphTrace.ts +97 -0
  97. package/src/structures/TtscGraphEdgeKind.ts +23 -0
  98. package/src/structures/TtscGraphNodeKind.ts +26 -0
  99. package/src/structures/TtscGraphNodeModifier.ts +19 -0
  100. package/src/structures/index.ts +21 -0
  101. package/src/view.ts +155 -0
  102. package/src/viewer/index.html +123 -0
  103. package/src/viewer/main.ts +209 -0
  104. package/lib/bin.d.ts +0 -2
  105. package/lib/diagnostics.d.ts +0 -23
  106. package/lib/diagnostics.js +0 -84
  107. package/lib/diagnostics.js.map +0 -1
  108. package/lib/index.d.ts +0 -29
  109. package/src/diagnostics.ts +0 -93
@@ -0,0 +1,181 @@
1
+ import { TtscGraphMemory } from "../model/TtscGraphMemory";
2
+ import { ITtscGraphEdge } from "../structures/ITtscGraphEdge";
3
+ import { ITtscGraphIndex } from "../structures/ITtscGraphIndex";
4
+ import { ITtscGraphNode } from "../structures/ITtscGraphNode";
5
+ import { resolveGraphHandle } from "./resolveHandle";
6
+ import { signatureOf } from "./runExpand";
7
+ import { runQuery } from "./runQuery";
8
+
9
+ const DEFAULT_LIMIT = 8;
10
+ const MAX_LIMIT = 20;
11
+ const DEFAULT_NEIGHBORS = 4;
12
+ const MAX_NEIGHBORS = 8;
13
+ const MAX_SEEDS = 6;
14
+ const STRUCTURAL_KINDS = new Set<string>(["contains", "exports", "imports"]);
15
+
16
+ /**
17
+ * Build the first source-free index for a code question. The result gives the
18
+ * model stable handles, declaration signatures, and direct graph context. It is
19
+ * deliberately not a source reader; source remains opt-in through
20
+ * graph_expand.
21
+ */
22
+ export function runIndex(
23
+ graph: TtscGraphMemory,
24
+ props: ITtscGraphIndex.IProps,
25
+ ): ITtscGraphIndex {
26
+ const query = props.query.trim();
27
+ const limit = bound(props.limit, DEFAULT_LIMIT, 1, MAX_LIMIT);
28
+ const neighborLimit = bound(
29
+ props.neighbors,
30
+ DEFAULT_NEIGHBORS,
31
+ 0,
32
+ MAX_NEIGHBORS,
33
+ );
34
+
35
+ const queryResult = runQuery(graph, { query, limit });
36
+ const hits = queryResult.hits.map((hit) => ({ ...hit }));
37
+
38
+ const mentions = directMentions(query).map((handle) => {
39
+ const resolved = resolveGraphHandle(graph, handle, 6);
40
+ const mention: ITtscGraphIndex.IMention = { handle };
41
+ if (resolved.node !== undefined)
42
+ mention.node = nodeOf(graph, resolved.node);
43
+ if (resolved.candidates !== undefined) {
44
+ mention.candidates = resolved.candidates.map((node) =>
45
+ nodeOf(graph, node),
46
+ );
47
+ }
48
+ return mention;
49
+ });
50
+
51
+ const seeds: ITtscGraphNode[] = [];
52
+ const seen = new Set<string>();
53
+ const addSeed = (node: ITtscGraphNode | undefined): void => {
54
+ if (node === undefined || seen.has(node.id)) return;
55
+ seen.add(node.id);
56
+ seeds.push(node);
57
+ };
58
+ for (const mention of mentions) {
59
+ if (mention.node !== undefined) addSeed(graph.node(mention.node.id));
60
+ }
61
+ for (const hit of hits) addSeed(graph.node(hit.id));
62
+
63
+ let truncated = seeds.length > MAX_SEEDS;
64
+ const neighborhood: ITtscGraphIndex.INeighborhood[] = [];
65
+ for (const seed of seeds.slice(0, MAX_SEEDS)) {
66
+ const outgoing = refs(graph, graph.outgoing(seed.id), "to", neighborLimit);
67
+ const incoming = refs(
68
+ graph,
69
+ graph.incoming(seed.id),
70
+ "from",
71
+ neighborLimit,
72
+ );
73
+ if (outgoing.truncated || incoming.truncated) truncated = true;
74
+ neighborhood.push({
75
+ ...nodeOf(graph, seed),
76
+ dependsOn: outgoing.items,
77
+ dependedOnBy: incoming.items,
78
+ });
79
+ }
80
+
81
+ return {
82
+ query,
83
+ hits,
84
+ mentions,
85
+ neighborhood,
86
+ next: {
87
+ expand: seeds.slice(0, MAX_SEEDS).map((node) => node.id),
88
+ traceFrom: mentions
89
+ .map((mention) => mention.node?.id)
90
+ .filter((id): id is string => id !== undefined),
91
+ },
92
+ ...(truncated ? { truncated: true } : {}),
93
+ };
94
+ }
95
+
96
+ function nodeOf(
97
+ graph: TtscGraphMemory,
98
+ node: ITtscGraphNode,
99
+ ): ITtscGraphIndex.INode {
100
+ const out: ITtscGraphIndex.INode = {
101
+ id: node.id,
102
+ name: node.qualifiedName ?? node.name,
103
+ kind: node.kind,
104
+ file: node.file,
105
+ };
106
+ if (node.evidence?.startLine !== undefined)
107
+ out.line = node.evidence.startLine;
108
+ const signature = signatureOf(graph.project, node);
109
+ if (signature !== undefined) out.signature = signature;
110
+ return out;
111
+ }
112
+
113
+ function refOf(
114
+ node: ITtscGraphNode,
115
+ edge: ITtscGraphEdge,
116
+ ): ITtscGraphIndex.IReference {
117
+ const out: ITtscGraphIndex.IReference = {
118
+ id: node.id,
119
+ name: node.qualifiedName ?? node.name,
120
+ kind: node.kind,
121
+ file: node.file,
122
+ relation: edge.kind,
123
+ };
124
+ if (node.evidence?.startLine !== undefined)
125
+ out.line = node.evidence.startLine;
126
+ return out;
127
+ }
128
+
129
+ function refs(
130
+ graph: TtscGraphMemory,
131
+ edges: readonly ITtscGraphEdge[],
132
+ end: "to" | "from",
133
+ limit: number,
134
+ ): { items: ITtscGraphIndex.IReference[]; truncated: boolean } {
135
+ const items: ITtscGraphIndex.IReference[] = [];
136
+ const seen = new Set<string>();
137
+ let available = 0;
138
+ for (const edge of edges) {
139
+ if (STRUCTURAL_KINDS.has(edge.kind)) continue;
140
+ const other = graph.node(end === "to" ? edge.to : edge.from);
141
+ if (other === undefined || other.kind === "file") continue;
142
+ const key = `${edge.kind}:${other.id}`;
143
+ if (seen.has(key)) continue;
144
+ seen.add(key);
145
+ available++;
146
+ if (items.length < limit) items.push(refOf(other, edge));
147
+ }
148
+ return { items, truncated: available > items.length };
149
+ }
150
+
151
+ function directMentions(query: string): string[] {
152
+ const handles = new Set<string>();
153
+ for (const match of query.matchAll(/`([^`]+)`/g)) {
154
+ const handle = normalizeHandle(match[1] ?? "");
155
+ if (handle !== undefined) handles.add(handle);
156
+ }
157
+ for (const match of query.matchAll(
158
+ /\b[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+\b/g,
159
+ )) {
160
+ const handle = normalizeHandle(match[0]);
161
+ if (handle !== undefined) handles.add(handle);
162
+ }
163
+ return [...handles];
164
+ }
165
+
166
+ function normalizeHandle(raw: string): string | undefined {
167
+ const value = raw.trim();
168
+ return /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(value)
169
+ ? value
170
+ : undefined;
171
+ }
172
+
173
+ function bound(
174
+ value: number | undefined,
175
+ fallback: number,
176
+ min: number,
177
+ max: number,
178
+ ): number {
179
+ const n = value === undefined || !Number.isFinite(value) ? fallback : value;
180
+ return Math.max(min, Math.min(max, Math.floor(n)));
181
+ }
@@ -0,0 +1,154 @@
1
+ import { TtscGraphMemory } from "../model/TtscGraphMemory";
2
+ import { ITtscGraphOverview } from "../structures/ITtscGraphOverview";
3
+
4
+ /** Edges that express nesting/packaging, not code dependency. */
5
+ const STRUCTURAL_KINDS = new Set<string>(["contains", "exports", "imports"]);
6
+
7
+ /**
8
+ * Project a compact, source-read-free architecture map: counts by kind, folder
9
+ * layering with export density, the highest-dependency symbols (ranked by real
10
+ * fan-in/out, excluding structural edges so nesting does not masquerade as
11
+ * dependency), and the export surface by file. Output is bounded so a model
12
+ * reads structure cheaply.
13
+ */
14
+ export function runOverview(
15
+ graph: TtscGraphMemory,
16
+ props: ITtscGraphOverview.IProps,
17
+ ): ITtscGraphOverview {
18
+ const aspect = props.aspect ?? "all";
19
+ const want = (a: ITtscGraphOverview.IProps["aspect"]): boolean =>
20
+ aspect === "all" || aspect === a;
21
+
22
+ const byKind: Record<string, number> = {};
23
+ let files = 0;
24
+ for (const node of graph.nodes) {
25
+ byKind[node.kind] = (byKind[node.kind] ?? 0) + 1;
26
+ if (node.kind === "file") files++;
27
+ }
28
+
29
+ const result: ITtscGraphOverview = {
30
+ project: graph.project,
31
+ counts: {
32
+ files,
33
+ nodes: graph.nodes.length,
34
+ edges: graph.edges.length,
35
+ byKind,
36
+ },
37
+ };
38
+ if (want("layers")) result.layers = layers(graph);
39
+ if (want("hotspots")) result.hotspots = hotspots(graph);
40
+ if (want("publicApi")) result.publicApi = publicApi(graph);
41
+ return result;
42
+ }
43
+
44
+ /** Folder-level layering: how source and its export surface spread by directory. */
45
+ function layers(graph: TtscGraphMemory): ITtscGraphOverview.ILayer[] {
46
+ const byDir = new Map<string, { files: Set<string>; exported: number }>();
47
+ for (const node of graph.nodes) {
48
+ if (node.external || node.kind === "file") continue;
49
+ const dir = dirname(node.file);
50
+ let entry = byDir.get(dir);
51
+ if (!entry) {
52
+ entry = { files: new Set(), exported: 0 };
53
+ byDir.set(dir, entry);
54
+ }
55
+ entry.files.add(node.file);
56
+ if (node.exported) entry.exported++;
57
+ }
58
+ return [...byDir.entries()]
59
+ .map(([dir, entry]) => ({
60
+ dir,
61
+ files: entry.files.size,
62
+ exported: entry.exported,
63
+ }))
64
+ .sort((a, b) => b.files - a.files)
65
+ .slice(0, 16);
66
+ }
67
+
68
+ /**
69
+ * The symbols at the center of the dependency graph, ranked by real fan-in and
70
+ * fan-out — structural `contains`/`exports`/`imports` edges are excluded so the
71
+ * ranking reflects code dependency, not nesting.
72
+ */
73
+ function hotspots(graph: TtscGraphMemory): ITtscGraphOverview.IHotspot[] {
74
+ const real = (id: string, side: "in" | "out"): number => {
75
+ const edges = side === "in" ? graph.incoming(id) : graph.outgoing(id);
76
+ let n = 0;
77
+ for (const edge of edges) if (!STRUCTURAL_KINDS.has(edge.kind)) n++;
78
+ return n;
79
+ };
80
+ return graph.nodes
81
+ .filter((node) => !node.external && node.kind !== "file")
82
+ .map((node) => ({
83
+ id: node.id,
84
+ name: node.qualifiedName ?? node.name,
85
+ kind: node.kind,
86
+ file: node.file,
87
+ fanIn: real(node.id, "in"),
88
+ fanOut: real(node.id, "out"),
89
+ }))
90
+ .filter((h) => h.fanIn + h.fanOut > 0)
91
+ .sort((a, b) => b.fanIn + b.fanOut - (a.fanIn + a.fanOut))
92
+ .slice(0, 15);
93
+ }
94
+
95
+ /** Declaration kinds that make up a meaningful public API surface. */
96
+ const API_KINDS = new Set<string>([
97
+ "class",
98
+ "interface",
99
+ "function",
100
+ "type",
101
+ "enum",
102
+ ]);
103
+
104
+ /**
105
+ * The exported API surface: the exported symbols a consumer of the project would
106
+ * use, ranked by how depended-on each is (real fan-in/out, structural edges
107
+ * excluded). Ranking by dependency rather than by which file declares the most
108
+ * exports surfaces the load-bearing types (a DataSource, a SelectQueryBuilder)
109
+ * instead of whichever file bundles the most type aliases; test, typings, and
110
+ * generated files are dropped so they cannot crowd the real surface out.
111
+ */
112
+ function publicApi(graph: TtscGraphMemory): ITtscGraphOverview.IPublicApi[] {
113
+ const degree = (id: string): number => {
114
+ let n = 0;
115
+ for (const edge of graph.outgoing(id))
116
+ if (!STRUCTURAL_KINDS.has(edge.kind)) n++;
117
+ for (const edge of graph.incoming(id))
118
+ if (!STRUCTURAL_KINDS.has(edge.kind)) n++;
119
+ return n;
120
+ };
121
+ return graph
122
+ .exported()
123
+ .filter((node) => API_KINDS.has(node.kind) && !isNoiseFile(node.file))
124
+ .map((node) => ({
125
+ name: node.qualifiedName ?? node.name,
126
+ kind: node.kind,
127
+ file: node.file,
128
+ degree: degree(node.id),
129
+ }))
130
+ .sort((a, b) => b.degree - a.degree)
131
+ .slice(0, 30)
132
+ .map(({ name, kind, file }) => ({ name, kind, file }));
133
+ }
134
+
135
+ /**
136
+ * A file whose exports are noise for an architecture overview: a test, a
137
+ * dependency's bundled `.d.ts`/typings, or generated output. The conventions are
138
+ * universal (a `test`/`spec` path, a `typings` file), so excluding them is not
139
+ * framework-specific — it keeps the API surface to authored, used code.
140
+ */
141
+ function isNoiseFile(file: string): boolean {
142
+ return (
143
+ /(^|\/)(test|tests|__tests__|spec|sample|samples)\//.test(file) ||
144
+ /\.(test|spec)\.[cm]?tsx?$/.test(file) ||
145
+ /(^|\/|\.)typings\.[cm]?ts$/.test(file) ||
146
+ /\.d\.[cm]?ts$/.test(file)
147
+ );
148
+ }
149
+
150
+ /** The parent directory of a project-relative path (`.` at the root). */
151
+ function dirname(file: string): string {
152
+ const slash = file.lastIndexOf("/");
153
+ return slash >= 0 ? file.slice(0, slash) : ".";
154
+ }
@@ -0,0 +1,150 @@
1
+ import { TtscGraphMemory } from "../model/TtscGraphMemory";
2
+ import { ITtscGraphNode } from "../structures/ITtscGraphNode";
3
+ import { ITtscGraphQuery } from "../structures/ITtscGraphQuery";
4
+ import { signatureOf } from "./runExpand";
5
+
6
+ // One file should not crowd out the rest of the ranking, so cap hits per file.
7
+ const PER_FILE = 3;
8
+ const DEFAULT_LIMIT = 12;
9
+
10
+ /**
11
+ * Rank the graph's symbols against a natural query. Scoring blends exact and
12
+ * dotted-name matches, CamelCase/subword coverage, file-path terms, a prefix
13
+ * bonus, and dependency centrality, then dampens external, generated, and test
14
+ * nodes and caps per file so the result is a diverse, relevant shortlist rather
15
+ * than one file's roster.
16
+ */
17
+ export function runQuery(
18
+ graph: TtscGraphMemory,
19
+ props: ITtscGraphQuery.IProps,
20
+ ): ITtscGraphQuery {
21
+ const terms = subwords(props.query);
22
+ const queryLc = props.query.trim().toLowerCase();
23
+ if (terms.length === 0) return { hits: [], next: { expand: [] } };
24
+
25
+ const scored: ITtscGraphQuery.IHit[] = [];
26
+ for (const node of graph.nodes) {
27
+ if (node.kind === "file") continue;
28
+ const score = scoreNode(graph, node, queryLc, terms);
29
+ if (score <= 0) continue;
30
+ scored.push({
31
+ id: node.id,
32
+ name: node.qualifiedName ?? node.name,
33
+ kind: node.kind,
34
+ file: node.file,
35
+ line: node.evidence?.startLine,
36
+ score: Math.round(score),
37
+ });
38
+ }
39
+
40
+ scored.sort((a, b) => b.score - a.score);
41
+
42
+ // Diversity: keep at most PER_FILE hits per file while filling up to the limit.
43
+ const limit = Math.max(1, props.limit ?? DEFAULT_LIMIT);
44
+ const perFile = new Map<string, number>();
45
+ const hits: ITtscGraphQuery.IHit[] = [];
46
+ for (const hit of scored) {
47
+ const used = perFile.get(hit.file) ?? 0;
48
+ if (used >= PER_FILE) continue;
49
+ perFile.set(hit.file, used + 1);
50
+ hits.push(hit);
51
+ if (hits.length >= limit) break;
52
+ }
53
+
54
+ // Attach each kept hit's signature — only the shortlist, so the read cost is
55
+ // bounded — so the model can often answer from the query alone, no expand.
56
+ for (const hit of hits) {
57
+ const node = graph.node(hit.id);
58
+ if (node === undefined) continue;
59
+ const sig = signatureOf(graph.project, node);
60
+ if (sig !== undefined) hit.signature = sig;
61
+ }
62
+ return { hits, next: { expand: hits.map((hit) => hit.id) } };
63
+ }
64
+
65
+ /** Score one node against the query; 0 means no match. */
66
+ function scoreNode(
67
+ graph: TtscGraphMemory,
68
+ node: ITtscGraphNode,
69
+ queryLc: string,
70
+ terms: string[],
71
+ ): number {
72
+ const name = node.name.toLowerCase();
73
+ const qualified = (node.qualifiedName ?? node.name).toLowerCase();
74
+ const nameSubs = subwords(node.name);
75
+ const qualifiedSubs = subwords(node.qualifiedName ?? node.name);
76
+ const pathSubs = subwords(node.file);
77
+
78
+ let score = 0;
79
+ if (queryLc === name || queryLc === qualified) {
80
+ score += 100;
81
+ } else if (qualified.includes(".") && queryLc.includes(qualified)) {
82
+ score += 85;
83
+ } else if (queryLc.includes(".") && qualified.includes(queryLc)) {
84
+ score += 60;
85
+ }
86
+
87
+ let covered = 0;
88
+ for (const term of terms) {
89
+ if (nameSubs.includes(term)) {
90
+ score += 12;
91
+ covered++;
92
+ } else if (qualifiedSubs.includes(term)) {
93
+ score += 8;
94
+ covered++;
95
+ } else if (name.includes(term)) {
96
+ score += 5;
97
+ covered++;
98
+ } else if (pathSubs.includes(term)) {
99
+ score += 3;
100
+ }
101
+ }
102
+ // Every query term landed somewhere in the name: a strong whole-query match.
103
+ if (covered === terms.length) score += 10;
104
+ if (name.startsWith(terms[0]!)) score += 4;
105
+
106
+ if (score <= 0) return 0;
107
+
108
+ // Centrality: a symbol the codebase leans on is a likelier target.
109
+ const fan = degree(graph, node.id);
110
+ score += Math.min(8, Math.log2(1 + fan) * 2);
111
+
112
+ // Dampen what is rarely the intended target.
113
+ if (node.external) score *= 0.5;
114
+ if (node.ignored) score *= 0.3;
115
+ if (isTestFile(node.file)) score *= 0.7;
116
+ return score;
117
+ }
118
+
119
+ /** Non-structural in+out degree (code dependency, not nesting). */
120
+ function degree(graph: TtscGraphMemory, id: string): number {
121
+ let n = 0;
122
+ for (const edge of graph.outgoing(id)) if (!isStructural(edge.kind)) n++;
123
+ for (const edge of graph.incoming(id)) if (!isStructural(edge.kind)) n++;
124
+ return n;
125
+ }
126
+
127
+ function isStructural(kind: string): boolean {
128
+ return kind === "contains" || kind === "exports" || kind === "imports";
129
+ }
130
+
131
+ function isTestFile(file: string): boolean {
132
+ return (
133
+ /(^|\/)(test|tests|__tests__|spec)\//.test(file) ||
134
+ /\.(test|spec)\.[cm]?tsx?$/.test(file)
135
+ );
136
+ }
137
+
138
+ /**
139
+ * Split an identifier or phrase into lowercase subword tokens: CamelCase,
140
+ * snake, dotted, and space boundaries all break, so `getHTTPResponse`,
141
+ * `find_by_id`, and `OrderService.create` tokenize the way a query would.
142
+ */
143
+ function subwords(text: string): string[] {
144
+ return text
145
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
146
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
147
+ .split(/[^a-zA-Z0-9]+/)
148
+ .filter((w) => w.length > 0)
149
+ .map((w) => w.toLowerCase());
150
+ }