@grafema/util 0.3.22 → 0.3.24

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 (60) hide show
  1. package/dist/config/ConfigLoader.d.ts +7 -3
  2. package/dist/config/ConfigLoader.d.ts.map +1 -1
  3. package/dist/config/ConfigLoader.js +23 -9
  4. package/dist/config/ConfigLoader.js.map +1 -1
  5. package/dist/index.d.ts +5 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +3 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/manifest/generator.d.ts +4 -0
  10. package/dist/manifest/generator.d.ts.map +1 -1
  11. package/dist/manifest/generator.js +27 -7
  12. package/dist/manifest/generator.js.map +1 -1
  13. package/dist/manifest/types.d.ts +1 -1
  14. package/dist/manifest/types.d.ts.map +1 -1
  15. package/dist/notation/archetypes.d.ts.map +1 -1
  16. package/dist/notation/archetypes.js +1 -0
  17. package/dist/notation/archetypes.js.map +1 -1
  18. package/dist/queries/findCallsInFunction.d.ts +2 -1
  19. package/dist/queries/findCallsInFunction.d.ts.map +1 -1
  20. package/dist/queries/findCallsInFunction.js +11 -5
  21. package/dist/queries/findCallsInFunction.js.map +1 -1
  22. package/dist/queries/getShape.d.ts +65 -0
  23. package/dist/queries/getShape.d.ts.map +1 -0
  24. package/dist/queries/getShape.js +170 -0
  25. package/dist/queries/getShape.js.map +1 -0
  26. package/dist/queries/index.d.ts +3 -0
  27. package/dist/queries/index.d.ts.map +1 -1
  28. package/dist/queries/index.js +2 -0
  29. package/dist/queries/index.js.map +1 -1
  30. package/dist/queries/traceCallChain.d.ts +77 -0
  31. package/dist/queries/traceCallChain.d.ts.map +1 -0
  32. package/dist/queries/traceCallChain.js +235 -0
  33. package/dist/queries/traceCallChain.js.map +1 -0
  34. package/dist/queries/types.d.ts +2 -0
  35. package/dist/queries/types.d.ts.map +1 -1
  36. package/dist/storage/backends/RFDBServerBackend.d.ts +5 -0
  37. package/dist/storage/backends/RFDBServerBackend.d.ts.map +1 -1
  38. package/dist/storage/backends/RFDBServerBackend.js +31 -2
  39. package/dist/storage/backends/RFDBServerBackend.js.map +1 -1
  40. package/dist/utils/lazyDownload.d.ts.map +1 -1
  41. package/dist/utils/lazyDownload.js +4 -0
  42. package/dist/utils/lazyDownload.js.map +1 -1
  43. package/dist/version.d.ts +22 -0
  44. package/dist/version.d.ts.map +1 -1
  45. package/dist/version.js +34 -0
  46. package/dist/version.js.map +1 -1
  47. package/package.json +3 -3
  48. package/src/config/ConfigLoader.ts +28 -9
  49. package/src/index.ts +5 -1
  50. package/src/manifest/generator.ts +46 -10
  51. package/src/manifest/types.ts +2 -1
  52. package/src/notation/archetypes.ts +1 -0
  53. package/src/queries/findCallsInFunction.ts +15 -7
  54. package/src/queries/getShape.ts +241 -0
  55. package/src/queries/index.ts +3 -0
  56. package/src/queries/traceCallChain.ts +341 -0
  57. package/src/queries/types.ts +2 -0
  58. package/src/storage/backends/RFDBServerBackend.ts +29 -2
  59. package/src/utils/lazyDownload.ts +4 -0
  60. package/src/version.ts +39 -0
@@ -39,7 +39,8 @@ import type { CallInfo, FindCallsOptions } from './types.js';
39
39
  interface GraphBackend {
40
40
  getNode(id: string): Promise<{
41
41
  id: string;
42
- type: string;
42
+ type?: string;
43
+ nodeType?: string;
43
44
  name?: string;
44
45
  file?: string;
45
46
  line?: number;
@@ -51,6 +52,11 @@ interface GraphBackend {
51
52
  ): Promise<Array<{ src: string; dst: string; type: string }>>;
52
53
  }
53
54
 
55
+ /** Normalize node type field (RFDB uses nodeType, some backends use type) */
56
+ function getNodeType(node: { type?: string; nodeType?: string }): string {
57
+ return node.nodeType ?? node.type ?? 'UNKNOWN';
58
+ }
59
+
54
60
  /**
55
61
  * Maximum BFS depth for downward scope traversal.
56
62
  *
@@ -111,7 +117,7 @@ export async function findCallsInFunction(
111
117
  const child = await backend.getNode(edge.dst);
112
118
  if (!child) continue;
113
119
 
114
- if (child.type === 'CALL' || child.type === 'METHOD_CALL') {
120
+ if (getNodeType(child) === 'CALL' || getNodeType(child) === 'METHOD_CALL') {
115
121
  const callInfo = await buildCallInfo(backend, child, 0);
116
122
  calls.push(callInfo);
117
123
 
@@ -128,7 +134,7 @@ export async function findCallsInFunction(
128
134
  }
129
135
 
130
136
  // Continue into nested scopes, but NOT into nested functions/classes
131
- if (child.type === 'SCOPE') {
137
+ if (getNodeType(child) === 'SCOPE') {
132
138
  queue.push({ id: child.id, depth: depth + 1 });
133
139
  }
134
140
  }
@@ -169,12 +175,13 @@ export async function findCallsInFunction(
169
175
  */
170
176
  async function buildCallInfo(
171
177
  backend: GraphBackend,
172
- callNode: { id: string; type: string; name?: string; file?: string; line?: number; object?: string },
178
+ callNode: { id: string; type?: string; nodeType?: string; name?: string; file?: string; line?: number; object?: string },
173
179
  depth: number
174
180
  ): Promise<CallInfo> {
175
- // Check for CALLS edge (resolved target)
176
- const callsEdges = await backend.getOutgoingEdges(callNode.id, ['CALLS']);
181
+ // Check for CALLS or CALLS_REMOTE edge (resolved target)
182
+ const callsEdges = await backend.getOutgoingEdges(callNode.id, ['CALLS', 'CALLS_REMOTE']);
177
183
  const isResolved = callsEdges.length > 0;
184
+ const isRemote = callsEdges.length > 0 && callsEdges[0].type === 'CALLS_REMOTE';
178
185
 
179
186
  let target = undefined;
180
187
  if (isResolved) {
@@ -192,13 +199,14 @@ async function buildCallInfo(
192
199
  return {
193
200
  id: callNode.id,
194
201
  name: callNode.name ?? '<unknown>',
195
- type: callNode.type as 'CALL' | 'METHOD_CALL',
202
+ type: getNodeType(callNode) as 'CALL' | 'METHOD_CALL',
196
203
  object: callNode.object,
197
204
  resolved: isResolved,
198
205
  target,
199
206
  file: callNode.file,
200
207
  line: callNode.line,
201
208
  depth,
209
+ remote: isRemote,
202
210
  };
203
211
  }
204
212
 
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Shape Query Engine
3
+ *
4
+ * Computes the "shape" of a CLASS, INTERFACE, or typed VARIABLE:
5
+ * the set of methods and properties it has, including inherited ones.
6
+ *
7
+ * Shape = { members: ShapeMember[], extends: string[], implementedBy: string[] }
8
+ *
9
+ * For CLASS: own HAS_METHOD + HAS_PROPERTY + walk EXTENDS chain for inherited
10
+ * For INTERFACE: own HAS_METHOD + HAS_PROPERTY (METHOD_SIGNATURE + PROPERTY_SIGNATURE)
11
+ * For VARIABLE: follow INSTANCE_OF → CLASS/INTERFACE → shape
12
+ *
13
+ * @module queries/getShape
14
+ */
15
+
16
+ export interface ShapeMember {
17
+ name: string;
18
+ kind: 'method' | 'property' | 'method_signature' | 'property_signature';
19
+ from: string; // CLASS/INTERFACE name that defines this member
20
+ file?: string;
21
+ line?: number;
22
+ nodeId: string;
23
+ }
24
+
25
+ export interface ShapeResult {
26
+ name: string;
27
+ nodeId: string;
28
+ nodeType: string;
29
+ file?: string;
30
+ members: ShapeMember[];
31
+ extends: string[];
32
+ implements: string[];
33
+ implementedBy: string[];
34
+ confidence: 'high' | 'medium' | 'low';
35
+ }
36
+
37
+ interface GraphBackend {
38
+ getNode(id: string): Promise<{
39
+ id: string | number;
40
+ type?: string;
41
+ nodeType?: string;
42
+ name?: string;
43
+ file?: string;
44
+ line?: number;
45
+ metadata?: string | Record<string, unknown>;
46
+ semanticId?: string;
47
+ } | null>;
48
+ getOutgoingEdges(
49
+ nodeId: string,
50
+ edgeTypes?: string[] | null
51
+ ): Promise<Array<{ src: string; dst: string; type: string; metadata?: string }>>;
52
+ getIncomingEdges(
53
+ nodeId: string,
54
+ edgeTypes?: string[] | null
55
+ ): Promise<Array<{ src: string; dst: string; type: string }>>;
56
+ }
57
+
58
+ function nodeType(node: { type?: string; nodeType?: string }): string {
59
+ return node.nodeType ?? node.type ?? 'UNKNOWN';
60
+ }
61
+
62
+ function parseMeta(node: { metadata?: string | Record<string, unknown> }): Record<string, unknown> {
63
+ if (!node.metadata) return {};
64
+ if (typeof node.metadata === 'string') {
65
+ try { return JSON.parse(node.metadata); } catch { return {}; }
66
+ }
67
+ return node.metadata;
68
+ }
69
+
70
+ /** Index mapping class/interface name → node ID for EXTENDS chain walking */
71
+ export type ClassIndex = Map<string, string>;
72
+
73
+ /**
74
+ * Get the shape of a CLASS, INTERFACE, or VARIABLE.
75
+ * @param classIndex — optional name→ID index for resolving EXTENDS/IMPLEMENTS targets
76
+ */
77
+ export async function getShape(
78
+ backend: GraphBackend,
79
+ targetId: string,
80
+ classIndex?: ClassIndex,
81
+ ): Promise<ShapeResult | null> {
82
+ const node = await backend.getNode(targetId);
83
+ if (!node) return null;
84
+
85
+ const nType = nodeType(node);
86
+ const nId = String(node.id);
87
+
88
+ if (nType === 'CLASS' || nType === 'INTERFACE') {
89
+ return getClassShape(backend, nId, node.name ?? '<unknown>', nType, node.file, classIndex);
90
+ }
91
+
92
+ if (nType === 'VARIABLE' || nType === 'CONSTANT' || nType === 'PARAMETER') {
93
+ // Follow INSTANCE_OF to find the class/interface
94
+ const edges = await backend.getOutgoingEdges(nId);
95
+ const instanceOf = edges.find(e => e.type === 'INSTANCE_OF');
96
+ if (!instanceOf) return null;
97
+
98
+ const classNode = await backend.getNode(instanceOf.dst);
99
+ if (!classNode) return null;
100
+
101
+ const shape = await getClassShape(
102
+ backend,
103
+ String(classNode.id),
104
+ classNode.name ?? '<unknown>',
105
+ nodeType(classNode),
106
+ classNode.file,
107
+ classIndex,
108
+ );
109
+ if (shape) {
110
+ shape.confidence = 'medium'; // inferred, not direct
111
+ }
112
+ return shape;
113
+ }
114
+
115
+ return null;
116
+ }
117
+
118
+ /**
119
+ * Get shape for a CLASS or INTERFACE, including inherited members.
120
+ */
121
+ async function getClassShape(
122
+ backend: GraphBackend,
123
+ classId: string,
124
+ className: string,
125
+ classType: string,
126
+ classFile?: string,
127
+ classIndex?: ClassIndex,
128
+ ): Promise<ShapeResult> {
129
+ const members: ShapeMember[] = [];
130
+ const extendsChain: string[] = [];
131
+ const implementsList: string[] = [];
132
+ const implementedBy: string[] = [];
133
+ const visited = new Set<string>();
134
+
135
+ // Collect own + inherited members via BFS through EXTENDS
136
+ const queue: Array<{ id: string; name: string }> = [{ id: classId, name: className }];
137
+
138
+ while (queue.length > 0) {
139
+ const current = queue.shift()!;
140
+ if (visited.has(current.id)) continue;
141
+ visited.add(current.id);
142
+
143
+ // Collect HAS_METHOD edges
144
+ const edges = await backend.getOutgoingEdges(current.id);
145
+ for (const edge of edges) {
146
+ if (edge.type === 'HAS_METHOD') {
147
+ const member = await backend.getNode(edge.dst);
148
+ if (member) {
149
+ members.push({
150
+ name: member.name ?? '<unknown>',
151
+ kind: nodeType(member) === 'METHOD_SIGNATURE' ? 'method_signature' : 'method',
152
+ from: current.name,
153
+ file: member.file,
154
+ line: member.line,
155
+ nodeId: String(member.id),
156
+ });
157
+ }
158
+ }
159
+ if (edge.type === 'HAS_PROPERTY') {
160
+ const member = await backend.getNode(edge.dst);
161
+ if (member) {
162
+ const mType = nodeType(member);
163
+ // Skip if it's actually a METHOD_SIGNATURE that was incorrectly HAS_PROPERTY
164
+ // (this handles legacy data before the fix)
165
+ const kind = mType === 'PROPERTY_SIGNATURE' ? 'property_signature'
166
+ : mType === 'METHOD_SIGNATURE' ? 'method_signature'
167
+ : 'property';
168
+ members.push({
169
+ name: member.name ?? '<unknown>',
170
+ kind,
171
+ from: current.name,
172
+ file: member.file,
173
+ line: member.line,
174
+ nodeId: String(member.id),
175
+ });
176
+ }
177
+ }
178
+ }
179
+
180
+ // Follow EXTENDS chain
181
+ const meta = await getNodeMeta(backend, current.id);
182
+ const superClass = meta.superClass as string | undefined;
183
+ if (superClass && current.id !== classId) {
184
+ extendsChain.push(superClass);
185
+ } else if (superClass && current.id === classId) {
186
+ extendsChain.push(superClass);
187
+ }
188
+ if (superClass) {
189
+ // Find the superclass node
190
+ const superNode = await findClassByName(backend, superClass, classIndex);
191
+ if (superNode) {
192
+ queue.push({ id: String(superNode.id), name: superNode.name ?? superClass });
193
+ }
194
+ }
195
+
196
+ // Collect implements
197
+ const implementsStr = meta.implements as string | undefined;
198
+ if (implementsStr && current.id === classId) {
199
+ implementsList.push(...implementsStr.split(',').map(s => s.trim()).filter(Boolean));
200
+ // Also collect members from implemented interfaces
201
+ for (const ifaceName of implementsList) {
202
+ const ifaceNode = await findClassByName(backend, ifaceName, classIndex);
203
+ if (ifaceNode) {
204
+ queue.push({ id: String(ifaceNode.id), name: ifaceNode.name ?? ifaceName });
205
+ }
206
+ }
207
+ }
208
+ }
209
+
210
+ // Find implementedBy (classes that have INSTANCE_OF → this class, or metadata implements)
211
+ // Skip for now — expensive reverse lookup
212
+
213
+ return {
214
+ name: className,
215
+ nodeId: classId,
216
+ nodeType: classType,
217
+ file: classFile,
218
+ members,
219
+ extends: extendsChain,
220
+ implements: implementsList,
221
+ implementedBy,
222
+ confidence: 'high',
223
+ };
224
+ }
225
+
226
+ async function getNodeMeta(backend: GraphBackend, nodeId: string): Promise<Record<string, unknown>> {
227
+ const node = await backend.getNode(nodeId);
228
+ if (!node) return {};
229
+ return parseMeta(node);
230
+ }
231
+
232
+ async function findClassByName(
233
+ _backend: GraphBackend,
234
+ name: string,
235
+ classIndex?: ClassIndex,
236
+ ): Promise<{ id: string; name: string } | null> {
237
+ if (!classIndex) return null;
238
+ const id = classIndex.get(name);
239
+ if (!id) return null;
240
+ return { id, name };
241
+ }
@@ -11,6 +11,9 @@ export { findCallsInFunction } from './findCallsInFunction.js';
11
11
  export { findContainingFunction } from './findContainingFunction.js';
12
12
  export { traceValues, aggregateValues, NONDETERMINISTIC_PATTERNS, NONDETERMINISTIC_OBJECTS } from './traceValues.js';
13
13
  export { traceDataflow, traceForwardBFS, traceBackwardBFS } from './traceDataflow.js';
14
+ export { traceCallChain } from './traceCallChain.js';
15
+ export { getShape } from './getShape.js';
16
+ export type { ShapeResult, ShapeMember, ClassIndex } from './getShape.js';
14
17
  export {
15
18
  buildNodeContext,
16
19
  getNodeDisplayName,
@@ -0,0 +1,341 @@
1
+ /**
2
+ * Transitive call-chain traversal.
3
+ *
4
+ * Forward: from a function, follow CALLS/CALLS_REMOTE edges transitively
5
+ * to show what the function eventually calls (including cross-language hops).
6
+ *
7
+ * Backward: from a function, follow incoming CALLS/CALLS_REMOTE edges
8
+ * to show who eventually calls this function.
9
+ *
10
+ * Uses findCallsInFunction for forward traversal (handles both Layout A
11
+ * and Layout B edge patterns). Backward traversal uses incoming edge BFS.
12
+ *
13
+ * @module queries/traceCallChain
14
+ */
15
+
16
+ import { findCallsInFunction } from './findCallsInFunction.js';
17
+ import type { CallInfo } from './types.js';
18
+
19
+ /**
20
+ * Graph backend interface.
21
+ * Accepts nodes with either `type` or `nodeType` field (RFDB uses `nodeType`).
22
+ */
23
+ interface GraphBackend {
24
+ getNode(id: string): Promise<{
25
+ id: string;
26
+ type?: string;
27
+ nodeType?: string;
28
+ name?: string;
29
+ file?: string;
30
+ line?: number;
31
+ endLine?: number;
32
+ } | null>;
33
+ getOutgoingEdges(
34
+ nodeId: string,
35
+ edgeTypes: string[] | null
36
+ ): Promise<Array<{ src: string; dst: string; type: string }>>;
37
+ getIncomingEdges(
38
+ nodeId: string,
39
+ edgeTypes: string[] | null
40
+ ): Promise<Array<{ src: string; dst: string; type: string }>>;
41
+ }
42
+
43
+ /** Normalize node type field (RFDB uses nodeType, some backends use type) */
44
+ function nodeType(node: { type?: string; nodeType?: string }): string {
45
+ return node.nodeType ?? node.type ?? 'UNKNOWN';
46
+ }
47
+
48
+ export interface CallChainHop {
49
+ /** The function/method being called */
50
+ name: string;
51
+ /** Semantic ID */
52
+ id: string;
53
+ /** File path */
54
+ file?: string;
55
+ /** Line number */
56
+ line?: number;
57
+ /** Depth in chain (0 = direct) */
58
+ depth: number;
59
+ /** Crosses process/language boundary */
60
+ remote: boolean;
61
+ /** Call was resolved to a target */
62
+ resolved: boolean;
63
+ }
64
+
65
+ export interface TraceCallChainResult {
66
+ direction: 'forward' | 'backward';
67
+ startNode: { id: string; name: string; file?: string; line?: number };
68
+ chain: CallChainHop[];
69
+ totalFound: number;
70
+ }
71
+
72
+ export interface TraceCallChainOptions {
73
+ direction?: 'forward' | 'backward' | 'both';
74
+ maxDepth?: number;
75
+ limit?: number;
76
+ }
77
+
78
+ const CALL_EDGE_TYPES = ['CALLS', 'CALLS_REMOTE'];
79
+ const FUNCTION_TYPES = new Set(['FUNCTION', 'METHOD', 'CONSTRUCTOR', 'LAMBDA']);
80
+
81
+ /**
82
+ * Trace call chain from a function forward or backward.
83
+ */
84
+ export async function traceCallChain(
85
+ backend: GraphBackend,
86
+ startId: string,
87
+ options: TraceCallChainOptions = {},
88
+ ): Promise<TraceCallChainResult[]> {
89
+ const {
90
+ direction = 'forward',
91
+ maxDepth = 10,
92
+ limit = 100,
93
+ } = options;
94
+
95
+ const startNode = await backend.getNode(startId);
96
+ if (!startNode) return [];
97
+
98
+ const start = {
99
+ id: startNode.id,
100
+ name: startNode.name ?? '<unknown>',
101
+ file: startNode.file,
102
+ line: startNode.line,
103
+ };
104
+
105
+ const results: TraceCallChainResult[] = [];
106
+
107
+ if (direction === 'forward' || direction === 'both') {
108
+ const chain = await traceForward(backend, startId, maxDepth, limit);
109
+ results.push({ direction: 'forward', startNode: start, chain, totalFound: chain.length });
110
+ }
111
+
112
+ if (direction === 'backward' || direction === 'both') {
113
+ const chain = await traceBackward(backend, startId, maxDepth, limit);
114
+ results.push({ direction: 'backward', startNode: start, chain, totalFound: chain.length });
115
+ }
116
+
117
+ return results;
118
+ }
119
+
120
+ /**
121
+ * Forward: what does this function call (transitively)?
122
+ * Uses findCallsInFunction with transitive mode + follows CALLS_REMOTE.
123
+ * Falls back to line-range matching when METHOD nodes have no outgoing edges.
124
+ */
125
+ async function traceForward(
126
+ backend: GraphBackend,
127
+ startId: string,
128
+ maxDepth: number,
129
+ limit: number,
130
+ ): Promise<CallChainHop[]> {
131
+ let calls = await findCallsInFunction(backend, startId, {
132
+ transitive: true,
133
+ transitiveDepth: maxDepth,
134
+ });
135
+
136
+ // Fallback: if no calls found via scope chain, try line-range matching
137
+ // (METHOD nodes often have no outgoing edges — calls are found by position)
138
+ if (calls.length === 0) {
139
+ calls = await findCallsByLineRange(backend, startId, maxDepth);
140
+ }
141
+
142
+ // Also check if any resolved target METHOD/FUNCTION has CALLS_REMOTE outgoing
143
+ const chain: CallChainHop[] = [];
144
+ const visited = new Set<string>();
145
+
146
+ for (const call of calls) {
147
+ if (chain.length >= limit) break;
148
+
149
+ chain.push({
150
+ name: call.name,
151
+ id: call.target?.id ?? call.id,
152
+ file: call.target?.file ?? call.file,
153
+ line: call.target?.line ?? call.line,
154
+ depth: call.depth ?? 0,
155
+ remote: call.remote ?? false,
156
+ resolved: call.resolved,
157
+ });
158
+
159
+ // If the target has CALLS_REMOTE outgoing, follow it
160
+ if (call.target && !visited.has(call.target.id)) {
161
+ visited.add(call.target.id);
162
+ const remoteEdges = await backend.getOutgoingEdges(call.target.id, ['CALLS_REMOTE']);
163
+ for (const re of remoteEdges) {
164
+ if (chain.length >= limit) break;
165
+ const remoteTarget = await backend.getNode(re.dst);
166
+ if (!remoteTarget) continue;
167
+
168
+ chain.push({
169
+ name: remoteTarget.name ?? '<unknown>',
170
+ id: remoteTarget.id,
171
+ file: remoteTarget.file,
172
+ line: remoteTarget.line,
173
+ depth: (call.depth ?? 0) + 1,
174
+ remote: true,
175
+ resolved: true,
176
+ });
177
+
178
+ // Recurse into the remote target's calls
179
+ if (!visited.has(remoteTarget.id)) {
180
+ visited.add(remoteTarget.id);
181
+ const remoteCalls = await findCallsInFunction(backend, remoteTarget.id, {
182
+ transitive: true,
183
+ transitiveDepth: Math.max(1, maxDepth - (call.depth ?? 0) - 1),
184
+ });
185
+ for (const rc of remoteCalls) {
186
+ if (chain.length >= limit) break;
187
+ chain.push({
188
+ name: rc.name,
189
+ id: rc.target?.id ?? rc.id,
190
+ file: rc.target?.file ?? rc.file,
191
+ line: rc.target?.line ?? rc.line,
192
+ depth: (call.depth ?? 0) + 2 + (rc.depth ?? 0),
193
+ remote: rc.remote ?? false,
194
+ resolved: rc.resolved,
195
+ });
196
+ }
197
+ }
198
+ }
199
+ }
200
+ }
201
+
202
+ return chain;
203
+ }
204
+
205
+ /**
206
+ * Backward: who calls this function (transitively)?
207
+ * BFS over incoming CALLS/CALLS_REMOTE edges, resolving containing functions.
208
+ */
209
+ async function traceBackward(
210
+ backend: GraphBackend,
211
+ startId: string,
212
+ maxDepth: number,
213
+ limit: number,
214
+ ): Promise<CallChainHop[]> {
215
+ const chain: CallChainHop[] = [];
216
+ const visited = new Set<string>();
217
+ visited.add(startId);
218
+
219
+ interface QueueItem { functionId: string; depth: number; remote: boolean }
220
+ const queue: QueueItem[] = [{ functionId: startId, depth: 0, remote: false }];
221
+
222
+ while (queue.length > 0 && chain.length < limit) {
223
+ const { functionId, depth } = queue.shift()!;
224
+ if (depth > maxDepth) continue;
225
+
226
+ // Find all CALL/METHOD_CALL nodes that have CALLS/CALLS_REMOTE → this function
227
+ const incomingCalls = await backend.getIncomingEdges(functionId, CALL_EDGE_TYPES);
228
+
229
+ for (const edge of incomingCalls) {
230
+ if (chain.length >= limit) break;
231
+
232
+ const callNode = await backend.getNode(edge.src);
233
+ if (!callNode) continue;
234
+
235
+ const isRemote = edge.type === 'CALLS_REMOTE';
236
+
237
+ // Find the containing function of this CALL node
238
+ const containingFn = await findContainingFunction(backend, callNode.id);
239
+ if (!containingFn) {
240
+ // Can't determine caller — add the call node itself
241
+ chain.push({
242
+ name: callNode.name ?? '<unknown>',
243
+ id: callNode.id,
244
+ file: callNode.file,
245
+ line: callNode.line,
246
+ depth,
247
+ remote: isRemote,
248
+ resolved: true,
249
+ });
250
+ continue;
251
+ }
252
+
253
+ if (visited.has(containingFn.id)) continue;
254
+ visited.add(containingFn.id);
255
+
256
+ chain.push({
257
+ name: containingFn.name ?? '<unknown>',
258
+ id: containingFn.id,
259
+ file: containingFn.file,
260
+ line: containingFn.line,
261
+ depth,
262
+ remote: isRemote,
263
+ resolved: true,
264
+ });
265
+
266
+ // Continue BFS from the containing function
267
+ queue.push({ functionId: containingFn.id, depth: depth + 1, remote: isRemote });
268
+ }
269
+ }
270
+
271
+ return chain;
272
+ }
273
+
274
+ /**
275
+ * Walk up CONTAINS edges to find the enclosing FUNCTION/METHOD.
276
+ */
277
+ async function findContainingFunction(
278
+ backend: GraphBackend,
279
+ nodeId: string,
280
+ ): Promise<{ id: string; name?: string; file?: string; line?: number } | null> {
281
+ let currentId = nodeId;
282
+ const seen = new Set<string>();
283
+
284
+ for (let i = 0; i < 10; i++) {
285
+ if (seen.has(currentId)) return null;
286
+ seen.add(currentId);
287
+
288
+ const incoming = await backend.getIncomingEdges(currentId, ['CONTAINS', 'HAS_SCOPE']);
289
+ if (incoming.length === 0) return null;
290
+
291
+ for (const edge of incoming) {
292
+ const parent = await backend.getNode(edge.src);
293
+ if (!parent) continue;
294
+ if (FUNCTION_TYPES.has(nodeType(parent))) {
295
+ return { id: parent.id, name: parent.name, file: parent.file, line: parent.line };
296
+ }
297
+ // Continue climbing from the parent
298
+ currentId = parent.id;
299
+ break;
300
+ }
301
+ }
302
+
303
+ return null;
304
+ }
305
+
306
+ /**
307
+ * Line-range fallback: find CALL nodes in the same file within the function's line range.
308
+ * Used when METHOD nodes have no outgoing edges (common for JS analyzer methods).
309
+ */
310
+ async function findCallsByLineRange(
311
+ backend: GraphBackend,
312
+ functionId: string,
313
+ _maxDepth: number,
314
+ ): Promise<CallInfo[]> {
315
+ const fn = await backend.getNode(functionId);
316
+ if (!fn || !fn.file || !fn.line) return [];
317
+
318
+ // Query all CALL/METHOD_CALL nodes in the same file
319
+ // We use getIncomingEdges on the MODULE to find CONTAINS → CALL pattern
320
+ // But simpler: iterate all outgoing CALLS_REMOTE from this node directly
321
+ const directRemote = await backend.getOutgoingEdges(functionId, ['CALLS_REMOTE']);
322
+ const calls: CallInfo[] = [];
323
+
324
+ for (const edge of directRemote) {
325
+ const target = await backend.getNode(edge.dst);
326
+ if (!target) continue;
327
+ calls.push({
328
+ id: functionId,
329
+ name: target.name ?? '<unknown>',
330
+ type: 'CALL',
331
+ resolved: true,
332
+ target: { id: target.id, name: target.name ?? '<unknown>', file: target.file, line: target.line },
333
+ file: fn.file,
334
+ line: fn.line,
335
+ depth: 0,
336
+ remote: true,
337
+ });
338
+ }
339
+
340
+ return calls;
341
+ }
@@ -34,6 +34,8 @@ export interface CallInfo {
34
34
  line?: number;
35
35
  /** Depth in transitive call chain (0 = direct call) */
36
36
  depth?: number;
37
+ /** Whether this call crosses a process/language boundary (CALLS_REMOTE) */
38
+ remote?: boolean;
37
39
  }
38
40
 
39
41
  /**