@danielblomma/cortex-mcp 0.4.5 → 0.6.4
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/README.md +38 -42
- package/bin/cortex.mjs +32 -60
- package/package.json +15 -3
- package/scaffold/.context/ontology.cypher +47 -0
- package/scaffold/.githooks/post-commit +14 -0
- package/scaffold/.githooks/post-rewrite +23 -0
- package/scaffold/mcp/package-lock.json +16 -16
- package/scaffold/mcp/package.json +3 -1
- package/scaffold/mcp/src/contextEntities.ts +311 -0
- package/scaffold/mcp/src/defaults.ts +6 -0
- package/scaffold/mcp/src/embed.ts +163 -37
- package/scaffold/mcp/src/frontmatter.ts +39 -0
- package/scaffold/mcp/src/graph.ts +253 -130
- package/scaffold/mcp/src/graphMetrics.ts +12 -0
- package/scaffold/mcp/src/impactPresentation.ts +202 -0
- package/scaffold/mcp/src/impactRanking.ts +237 -0
- package/scaffold/mcp/src/impactResponse.ts +47 -0
- package/scaffold/mcp/src/impactResults.ts +173 -0
- package/scaffold/mcp/src/impactSeed.ts +33 -0
- package/scaffold/mcp/src/impactTraversal.ts +83 -0
- package/scaffold/mcp/src/jsonl.ts +34 -0
- package/scaffold/mcp/src/loadGraph.ts +345 -86
- package/scaffold/mcp/src/paths.ts +17 -1
- package/scaffold/mcp/src/presets.ts +137 -0
- package/scaffold/mcp/src/relatedResponse.ts +30 -0
- package/scaffold/mcp/src/relatedTraversal.ts +101 -0
- package/scaffold/mcp/src/rules.ts +27 -0
- package/scaffold/mcp/src/search.ts +186 -455
- package/scaffold/mcp/src/searchCore.ts +274 -0
- package/scaffold/mcp/src/searchResults.ts +133 -0
- package/scaffold/mcp/src/server.ts +95 -3
- package/scaffold/mcp/src/types.ts +82 -3
- package/scaffold/scripts/context.sh +12 -46
- package/scaffold/scripts/dashboard.mjs +797 -0
- package/scaffold/scripts/dashboard.sh +13 -0
- package/scaffold/scripts/ingest.mjs +2219 -59
- package/scaffold/scripts/install-git-hooks.sh +3 -1
- package/scaffold/scripts/memory-compile.mjs +232 -0
- package/scaffold/scripts/memory-compile.sh +20 -0
- package/scaffold/scripts/memory-lint.mjs +375 -0
- package/scaffold/scripts/memory-lint.sh +20 -0
- package/scaffold/scripts/parsers/config.mjs +178 -0
- package/scaffold/scripts/parsers/cpp.mjs +316 -0
- package/scaffold/scripts/parsers/dotnet/VbNetParser/Program.cs +374 -0
- package/scaffold/scripts/parsers/dotnet/VbNetParser/VbNetParser.csproj +13 -0
- package/scaffold/scripts/parsers/javascript/ast.mjs +61 -0
- package/scaffold/scripts/parsers/javascript/calls.mjs +53 -0
- package/scaffold/scripts/parsers/javascript/chunks.mjs +388 -0
- package/scaffold/scripts/parsers/javascript/imports.mjs +162 -0
- package/scaffold/scripts/parsers/javascript/patterns.mjs +82 -0
- package/scaffold/scripts/parsers/javascript/scope-analysis.mjs +3 -0
- package/scaffold/scripts/parsers/javascript/scope-builder.mjs +305 -0
- package/scaffold/scripts/parsers/javascript/scope-resolver.mjs +82 -0
- package/scaffold/scripts/parsers/javascript.mjs +27 -350
- package/scaffold/scripts/parsers/resources.mjs +166 -0
- package/scaffold/scripts/parsers/sql.mjs +137 -0
- package/scaffold/scripts/parsers/vbnet.mjs +143 -0
- package/scaffold/scripts/status.sh +0 -7
- package/scaffold/scripts/capture-note.sh +0 -55
- package/scaffold/scripts/plan-state-engine.cjs +0 -310
- package/scaffold/scripts/plan-state.sh +0 -71
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { recursive } from "acorn-walk";
|
|
2
|
+
|
|
3
|
+
import { WALK_BASE } from "./ast.mjs";
|
|
4
|
+
import { collectPatternIdentifiers, walkPatternExpressions } from "./patterns.mjs";
|
|
5
|
+
|
|
6
|
+
export function buildScopeGraph(bodyNode) {
|
|
7
|
+
const rootScope = createScope(bodyNode, null, "analysis-root");
|
|
8
|
+
const scopeByNode = new Map();
|
|
9
|
+
const scopeStack = [rootScope];
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
recursive(
|
|
13
|
+
bodyNode,
|
|
14
|
+
null,
|
|
15
|
+
{
|
|
16
|
+
ImportDeclaration() {},
|
|
17
|
+
|
|
18
|
+
VariableDeclaration(node, state, recurse) {
|
|
19
|
+
const targetScope = node.kind === "var" ? nearestVarScope(scopeStack) : currentScope(scopeStack);
|
|
20
|
+
for (const declarator of node.declarations || []) {
|
|
21
|
+
declarePattern(targetScope, declarator.id);
|
|
22
|
+
if (declarator.init) {
|
|
23
|
+
recurse(declarator.init, state);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
FunctionDeclaration(node, state, recurse) {
|
|
29
|
+
const parentScope = currentScope(scopeStack);
|
|
30
|
+
if (node.id) {
|
|
31
|
+
declareIdentifier(parentScope, node.id.name);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const functionScope = createChildScope(scopeByNode, parentScope, node, "function");
|
|
35
|
+
if (node.id) {
|
|
36
|
+
declareIdentifier(functionScope, node.id.name);
|
|
37
|
+
}
|
|
38
|
+
declareTypeParameters(functionScope, node.typeParameters);
|
|
39
|
+
for (const param of node.params || []) {
|
|
40
|
+
declarePattern(functionScope, param);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
withScope(scopeStack, functionScope, () => {
|
|
44
|
+
for (const param of node.params || []) {
|
|
45
|
+
walkPatternExpressions(param, (child) => recurse(child, state));
|
|
46
|
+
}
|
|
47
|
+
if (node.body) {
|
|
48
|
+
recurse(node.body, state);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
FunctionExpression(node, state, recurse) {
|
|
54
|
+
const functionScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "function");
|
|
55
|
+
if (node.id) {
|
|
56
|
+
declareIdentifier(functionScope, node.id.name);
|
|
57
|
+
}
|
|
58
|
+
declareTypeParameters(functionScope, node.typeParameters);
|
|
59
|
+
for (const param of node.params || []) {
|
|
60
|
+
declarePattern(functionScope, param);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
withScope(scopeStack, functionScope, () => {
|
|
64
|
+
for (const param of node.params || []) {
|
|
65
|
+
walkPatternExpressions(param, (child) => recurse(child, state));
|
|
66
|
+
}
|
|
67
|
+
if (node.body) {
|
|
68
|
+
recurse(node.body, state);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
ArrowFunctionExpression(node, state, recurse) {
|
|
74
|
+
const functionScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "function");
|
|
75
|
+
declareTypeParameters(functionScope, node.typeParameters);
|
|
76
|
+
for (const param of node.params || []) {
|
|
77
|
+
declarePattern(functionScope, param);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
withScope(scopeStack, functionScope, () => {
|
|
81
|
+
for (const param of node.params || []) {
|
|
82
|
+
walkPatternExpressions(param, (child) => recurse(child, state));
|
|
83
|
+
}
|
|
84
|
+
if (node.body) {
|
|
85
|
+
recurse(node.body, state);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
ClassDeclaration(node, state, recurse) {
|
|
91
|
+
const parentScope = currentScope(scopeStack);
|
|
92
|
+
if (node.id) {
|
|
93
|
+
declareIdentifier(parentScope, node.id.name);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (node.superClass) {
|
|
97
|
+
recurse(node.superClass, state);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const classScope = createChildScope(scopeByNode, parentScope, node, "class");
|
|
101
|
+
if (node.id) {
|
|
102
|
+
declareIdentifier(classScope, node.id.name);
|
|
103
|
+
}
|
|
104
|
+
declareTypeParameters(classScope, node.typeParameters);
|
|
105
|
+
|
|
106
|
+
withScope(scopeStack, classScope, () => {
|
|
107
|
+
if (node.body) {
|
|
108
|
+
recurse(node.body, state);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
ClassExpression(node, state, recurse) {
|
|
114
|
+
if (node.superClass) {
|
|
115
|
+
recurse(node.superClass, state);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const classScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "class");
|
|
119
|
+
if (node.id) {
|
|
120
|
+
declareIdentifier(classScope, node.id.name);
|
|
121
|
+
}
|
|
122
|
+
declareTypeParameters(classScope, node.typeParameters);
|
|
123
|
+
|
|
124
|
+
withScope(scopeStack, classScope, () => {
|
|
125
|
+
if (node.body) {
|
|
126
|
+
recurse(node.body, state);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
BlockStatement(node, state, recurse) {
|
|
132
|
+
const blockScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "block");
|
|
133
|
+
withScope(scopeStack, blockScope, () => {
|
|
134
|
+
for (const statement of node.body || []) {
|
|
135
|
+
recurse(statement, state);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
SwitchStatement(node, state, recurse) {
|
|
141
|
+
if (node.discriminant) {
|
|
142
|
+
recurse(node.discriminant, state);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const switchScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "block");
|
|
146
|
+
withScope(scopeStack, switchScope, () => {
|
|
147
|
+
for (const caseNode of node.cases || []) {
|
|
148
|
+
recurse(caseNode, state);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
ForStatement(node, state, recurse) {
|
|
154
|
+
const loopScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "block");
|
|
155
|
+
withScope(scopeStack, loopScope, () => {
|
|
156
|
+
if (node.init) {
|
|
157
|
+
recurse(node.init, state);
|
|
158
|
+
}
|
|
159
|
+
if (node.test) {
|
|
160
|
+
recurse(node.test, state);
|
|
161
|
+
}
|
|
162
|
+
if (node.update) {
|
|
163
|
+
recurse(node.update, state);
|
|
164
|
+
}
|
|
165
|
+
if (node.body) {
|
|
166
|
+
recurse(node.body, state);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
ForInStatement(node, state, recurse) {
|
|
172
|
+
const loopScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "block");
|
|
173
|
+
withScope(scopeStack, loopScope, () => {
|
|
174
|
+
recurse(node.left, state);
|
|
175
|
+
recurse(node.right, state);
|
|
176
|
+
if (node.body) {
|
|
177
|
+
recurse(node.body, state);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
ForOfStatement(node, state, recurse) {
|
|
183
|
+
const loopScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "block");
|
|
184
|
+
withScope(scopeStack, loopScope, () => {
|
|
185
|
+
recurse(node.left, state);
|
|
186
|
+
recurse(node.right, state);
|
|
187
|
+
if (node.body) {
|
|
188
|
+
recurse(node.body, state);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
CatchClause(node, state, recurse) {
|
|
194
|
+
const catchScope = createChildScope(scopeByNode, currentScope(scopeStack), node, "catch");
|
|
195
|
+
if (node.param) {
|
|
196
|
+
declarePattern(catchScope, node.param);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
withScope(scopeStack, catchScope, () => {
|
|
200
|
+
if (node.param) {
|
|
201
|
+
walkPatternExpressions(node.param, (child) => recurse(child, state));
|
|
202
|
+
}
|
|
203
|
+
if (node.body) {
|
|
204
|
+
recurse(node.body, state);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
MethodDefinition(node, state, recurse) {
|
|
210
|
+
if (node.computed && node.key) {
|
|
211
|
+
recurse(node.key, state);
|
|
212
|
+
}
|
|
213
|
+
if (node.value) {
|
|
214
|
+
recurse(node.value, state);
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
PropertyDefinition(node, state, recurse) {
|
|
219
|
+
if (node.computed && node.key) {
|
|
220
|
+
recurse(node.key, state);
|
|
221
|
+
}
|
|
222
|
+
if (node.value) {
|
|
223
|
+
recurse(node.value, state);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
WALK_BASE
|
|
228
|
+
);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
// Ignore walk errors for incomplete ASTs.
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return { rootScope, scopeByNode };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function createScope(node, parent, kind) {
|
|
237
|
+
const scope = {
|
|
238
|
+
node,
|
|
239
|
+
parent,
|
|
240
|
+
kind,
|
|
241
|
+
declarations: new Set(),
|
|
242
|
+
children: []
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
if (parent) {
|
|
246
|
+
parent.children.push(scope);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return scope;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function createChildScope(scopeByNode, parentScope, node, kind) {
|
|
253
|
+
const existing = scopeByNode.get(node);
|
|
254
|
+
if (existing) {
|
|
255
|
+
return existing;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const scope = createScope(node, parentScope, kind);
|
|
259
|
+
scopeByNode.set(node, scope);
|
|
260
|
+
return scope;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function currentScope(scopeStack) {
|
|
264
|
+
return scopeStack[scopeStack.length - 1] ?? null;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function nearestVarScope(scopeStack) {
|
|
268
|
+
for (let index = scopeStack.length - 1; index >= 0; index -= 1) {
|
|
269
|
+
const scope = scopeStack[index];
|
|
270
|
+
if (scope.kind === "function" || scope.kind === "analysis-root") {
|
|
271
|
+
return scope;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return currentScope(scopeStack);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function declareIdentifier(scope, name) {
|
|
279
|
+
if (!scope || !name) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
scope.declarations.add(name);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function declarePattern(scope, pattern) {
|
|
287
|
+
collectPatternIdentifiers(pattern, (name) => {
|
|
288
|
+
declareIdentifier(scope, name);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function declareTypeParameters(scope, typeParameters) {
|
|
293
|
+
for (const param of typeParameters?.params || []) {
|
|
294
|
+
declareIdentifier(scope, param.name);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function withScope(scopeStack, scope, visit) {
|
|
299
|
+
scopeStack.push(scope);
|
|
300
|
+
try {
|
|
301
|
+
visit();
|
|
302
|
+
} finally {
|
|
303
|
+
scopeStack.pop();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export function isReferenceIdentifier(node, ancestors) {
|
|
2
|
+
const parent = ancestors[ancestors.length - 2] ?? null;
|
|
3
|
+
const grandparent = ancestors[ancestors.length - 3] ?? null;
|
|
4
|
+
|
|
5
|
+
if (!parent) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
switch (parent.type) {
|
|
10
|
+
case "ImportSpecifier":
|
|
11
|
+
case "ImportDefaultSpecifier":
|
|
12
|
+
case "ImportNamespaceSpecifier":
|
|
13
|
+
case "LabeledStatement":
|
|
14
|
+
case "BreakStatement":
|
|
15
|
+
case "ContinueStatement":
|
|
16
|
+
case "MetaProperty":
|
|
17
|
+
return false;
|
|
18
|
+
case "VariableDeclarator":
|
|
19
|
+
return parent.init === node;
|
|
20
|
+
case "FunctionDeclaration":
|
|
21
|
+
case "FunctionExpression":
|
|
22
|
+
case "ArrowFunctionExpression":
|
|
23
|
+
return !parent.params.includes(node) && parent.id !== node;
|
|
24
|
+
case "ClassDeclaration":
|
|
25
|
+
case "ClassExpression":
|
|
26
|
+
return parent.id !== node;
|
|
27
|
+
case "MemberExpression":
|
|
28
|
+
return parent.object === node || parent.computed;
|
|
29
|
+
case "Property":
|
|
30
|
+
if (grandparent?.type === "ObjectPattern") {
|
|
31
|
+
return parent.computed && parent.key === node;
|
|
32
|
+
}
|
|
33
|
+
if (parent.shorthand && parent.value === node) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return parent.value === node || (parent.computed && parent.key === node);
|
|
37
|
+
case "MethodDefinition":
|
|
38
|
+
case "PropertyDefinition":
|
|
39
|
+
return parent.computed && parent.key === node;
|
|
40
|
+
case "AssignmentPattern":
|
|
41
|
+
return parent.right === node;
|
|
42
|
+
case "ArrayPattern":
|
|
43
|
+
case "ObjectPattern":
|
|
44
|
+
case "RestElement":
|
|
45
|
+
return false;
|
|
46
|
+
case "CatchClause":
|
|
47
|
+
return parent.param !== node;
|
|
48
|
+
case "ExportSpecifier":
|
|
49
|
+
return parent.local === node;
|
|
50
|
+
default:
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function resolveIdentifier(name, ancestors, scopeGraph) {
|
|
56
|
+
let scope = getNearestScope(ancestors, scopeGraph) ?? scopeGraph.rootScope;
|
|
57
|
+
|
|
58
|
+
while (scope) {
|
|
59
|
+
if (scope.declarations.has(name)) {
|
|
60
|
+
return {
|
|
61
|
+
name,
|
|
62
|
+
scope,
|
|
63
|
+
kind: scope.kind
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
scope = scope.parent;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getNearestScope(ancestors, scopeGraph) {
|
|
74
|
+
for (let index = ancestors.length - 2; index >= 0; index -= 1) {
|
|
75
|
+
const scope = scopeGraph.scopeByNode.get(ancestors[index]);
|
|
76
|
+
if (scope) {
|
|
77
|
+
return scope;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return null;
|
|
82
|
+
}
|