@grafema/util 0.3.22 → 0.3.23
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/dist/config/ConfigLoader.d.ts +7 -3
- package/dist/config/ConfigLoader.d.ts.map +1 -1
- package/dist/config/ConfigLoader.js +23 -9
- package/dist/config/ConfigLoader.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/manifest/generator.d.ts +4 -0
- package/dist/manifest/generator.d.ts.map +1 -1
- package/dist/manifest/generator.js +27 -7
- package/dist/manifest/generator.js.map +1 -1
- package/dist/manifest/types.d.ts +1 -1
- package/dist/manifest/types.d.ts.map +1 -1
- package/dist/notation/archetypes.d.ts.map +1 -1
- package/dist/notation/archetypes.js +1 -0
- package/dist/notation/archetypes.js.map +1 -1
- package/dist/queries/findCallsInFunction.d.ts +2 -1
- package/dist/queries/findCallsInFunction.d.ts.map +1 -1
- package/dist/queries/findCallsInFunction.js +11 -5
- package/dist/queries/findCallsInFunction.js.map +1 -1
- package/dist/queries/getShape.d.ts +65 -0
- package/dist/queries/getShape.d.ts.map +1 -0
- package/dist/queries/getShape.js +170 -0
- package/dist/queries/getShape.js.map +1 -0
- package/dist/queries/index.d.ts +3 -0
- package/dist/queries/index.d.ts.map +1 -1
- package/dist/queries/index.js +2 -0
- package/dist/queries/index.js.map +1 -1
- package/dist/queries/traceCallChain.d.ts +77 -0
- package/dist/queries/traceCallChain.d.ts.map +1 -0
- package/dist/queries/traceCallChain.js +235 -0
- package/dist/queries/traceCallChain.js.map +1 -0
- package/dist/queries/types.d.ts +2 -0
- package/dist/queries/types.d.ts.map +1 -1
- package/dist/storage/backends/RFDBServerBackend.d.ts +5 -0
- package/dist/storage/backends/RFDBServerBackend.d.ts.map +1 -1
- package/dist/storage/backends/RFDBServerBackend.js +31 -2
- package/dist/storage/backends/RFDBServerBackend.js.map +1 -1
- package/dist/version.d.ts +22 -0
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +34 -0
- package/dist/version.js.map +1 -1
- package/package.json +3 -3
- package/src/config/ConfigLoader.ts +28 -9
- package/src/index.ts +5 -1
- package/src/manifest/generator.ts +46 -10
- package/src/manifest/types.ts +2 -1
- package/src/notation/archetypes.ts +1 -0
- package/src/queries/findCallsInFunction.ts +15 -7
- package/src/queries/getShape.ts +241 -0
- package/src/queries/index.ts +3 -0
- package/src/queries/traceCallChain.ts +341 -0
- package/src/queries/types.ts +2 -0
- package/src/storage/backends/RFDBServerBackend.ts +29 -2
- package/src/version.ts +39 -0
|
@@ -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
|
+
}
|
package/src/queries/index.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/queries/types.ts
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
import { RFDBClient, type BatchHandle } from '@grafema/rfdb-client';
|
|
22
22
|
import type { ChildProcess } from 'child_process';
|
|
23
23
|
import { join, dirname } from 'path';
|
|
24
|
+
import { createHash } from 'crypto';
|
|
24
25
|
|
|
25
26
|
import type { WireNode, WireEdge, FieldDeclaration, CommitDelta, AttrQuery as RFDBAttrQuery, DatalogExplainResult, ServerStats, CypherResult } from '@grafema/types';
|
|
26
27
|
import type { NodeType, EdgeType } from '@grafema/types';
|
|
@@ -119,11 +120,21 @@ export class RFDBServerBackend {
|
|
|
119
120
|
this.silent = options.silent ?? false;
|
|
120
121
|
this._clientName = options.clientName ?? 'core';
|
|
121
122
|
// Default socket path: next to the database in .grafema folder
|
|
122
|
-
// This ensures each project has its own socket, avoiding conflicts
|
|
123
|
+
// This ensures each project has its own socket, avoiding conflicts.
|
|
124
|
+
// Unix socket paths have a hard limit (SUN_LEN: 104 on macOS, 108 on Linux).
|
|
125
|
+
// If the project path is too deep, fall back to /tmp with a hash to avoid conflicts.
|
|
123
126
|
if (options.socketPath) {
|
|
124
127
|
this.socketPath = options.socketPath;
|
|
125
128
|
} else if (this.dbPath) {
|
|
126
|
-
|
|
129
|
+
const localSocket = join(dirname(this.dbPath), 'rfdb.sock');
|
|
130
|
+
const SUN_LEN = process.platform === 'darwin' ? 104 : 108;
|
|
131
|
+
if (Buffer.byteLength(localSocket) < SUN_LEN) {
|
|
132
|
+
this.socketPath = localSocket;
|
|
133
|
+
} else {
|
|
134
|
+
// Hash the project path to create a unique but short socket path
|
|
135
|
+
const hash = createHash('md5').update(dirname(this.dbPath)).digest('hex').slice(0, 12);
|
|
136
|
+
this.socketPath = join('/tmp', `grafema-${hash}.sock`);
|
|
137
|
+
}
|
|
127
138
|
} else {
|
|
128
139
|
this.socketPath = '/tmp/rfdb.sock'; // fallback, not recommended
|
|
129
140
|
}
|
|
@@ -283,6 +294,22 @@ export class RFDBServerBackend {
|
|
|
283
294
|
this.serverProcess = null;
|
|
284
295
|
}
|
|
285
296
|
|
|
297
|
+
/**
|
|
298
|
+
* Shutdown the RFDB server gracefully (flush + exit).
|
|
299
|
+
* Use before operations that invalidate the socket (e.g. clear + restart).
|
|
300
|
+
*/
|
|
301
|
+
async shutdownServer(): Promise<void> {
|
|
302
|
+
if (!this.client) return;
|
|
303
|
+
try {
|
|
304
|
+
await this.client.shutdown();
|
|
305
|
+
} catch {
|
|
306
|
+
// Server may already be gone
|
|
307
|
+
}
|
|
308
|
+
this.client = null;
|
|
309
|
+
this.connected = false;
|
|
310
|
+
this.serverProcess = null;
|
|
311
|
+
}
|
|
312
|
+
|
|
286
313
|
/**
|
|
287
314
|
* Clear the database
|
|
288
315
|
*/
|
package/src/version.ts
CHANGED
|
@@ -26,3 +26,42 @@ export function getSchemaVersion(version: string): string {
|
|
|
26
26
|
const base = version.split('-')[0];
|
|
27
27
|
return base;
|
|
28
28
|
}
|
|
29
|
+
|
|
30
|
+
export interface ParsedVersion {
|
|
31
|
+
major: number;
|
|
32
|
+
minor: number;
|
|
33
|
+
patch: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parse a version string into structured major.minor.patch components.
|
|
38
|
+
* Strips pre-release tags before parsing.
|
|
39
|
+
*
|
|
40
|
+
* @param version - Version string (e.g., "0.2.5-beta")
|
|
41
|
+
* @returns Parsed version or null if the string is not a valid version
|
|
42
|
+
*/
|
|
43
|
+
export function parseVersion(version: string): ParsedVersion | null {
|
|
44
|
+
const base = getSchemaVersion(version);
|
|
45
|
+
const parts = base.split('.');
|
|
46
|
+
if (parts.length < 2) return null;
|
|
47
|
+
const major = parseInt(parts[0], 10);
|
|
48
|
+
const minor = parseInt(parts[1], 10);
|
|
49
|
+
const patch = parts.length >= 3 ? parseInt(parts[2], 10) : 0;
|
|
50
|
+
if (isNaN(major) || isNaN(minor) || isNaN(patch)) return null;
|
|
51
|
+
return { major, minor, patch };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Check if two versions are compatible (same major.minor).
|
|
56
|
+
* Patch differences are allowed — only major or minor mismatch is incompatible.
|
|
57
|
+
*
|
|
58
|
+
* @param configVersion - Version from the config file
|
|
59
|
+
* @param currentVersion - Currently running Grafema version
|
|
60
|
+
* @returns true if versions share the same major.minor
|
|
61
|
+
*/
|
|
62
|
+
export function isCompatibleVersion(configVersion: string, currentVersion: string): boolean {
|
|
63
|
+
const config = parseVersion(configVersion);
|
|
64
|
+
const current = parseVersion(currentVersion);
|
|
65
|
+
if (!config || !current) return false;
|
|
66
|
+
return config.major === current.major && config.minor === current.minor;
|
|
67
|
+
}
|