@grafema/cli 0.2.5-beta → 0.2.7

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 (105) hide show
  1. package/README.md +12 -0
  2. package/dist/cli.js +6 -2
  3. package/dist/cli.js.map +1 -1
  4. package/dist/commands/analyze.d.ts +3 -10
  5. package/dist/commands/analyze.d.ts.map +1 -1
  6. package/dist/commands/analyze.js +5 -347
  7. package/dist/commands/analyze.js.map +1 -1
  8. package/dist/commands/analyzeAction.d.ts +28 -0
  9. package/dist/commands/analyzeAction.d.ts.map +1 -0
  10. package/dist/commands/analyzeAction.js +243 -0
  11. package/dist/commands/analyzeAction.js.map +1 -0
  12. package/dist/commands/check.js +2 -2
  13. package/dist/commands/check.js.map +1 -1
  14. package/dist/commands/context.d.ts +16 -0
  15. package/dist/commands/context.d.ts.map +1 -0
  16. package/dist/commands/context.js +238 -0
  17. package/dist/commands/context.js.map +1 -0
  18. package/dist/commands/doctor/checks.js +1 -1
  19. package/dist/commands/doctor/checks.js.map +1 -1
  20. package/dist/commands/explain.d.ts.map +1 -1
  21. package/dist/commands/explain.js +4 -3
  22. package/dist/commands/explain.js.map +1 -1
  23. package/dist/commands/file.d.ts +15 -0
  24. package/dist/commands/file.d.ts.map +1 -0
  25. package/dist/commands/file.js +144 -0
  26. package/dist/commands/file.js.map +1 -0
  27. package/dist/commands/impact.d.ts.map +1 -1
  28. package/dist/commands/impact.js +2 -3
  29. package/dist/commands/impact.js.map +1 -1
  30. package/dist/commands/init.d.ts.map +1 -1
  31. package/dist/commands/init.js +13 -1
  32. package/dist/commands/init.js.map +1 -1
  33. package/dist/commands/ls.d.ts.map +1 -1
  34. package/dist/commands/ls.js +3 -2
  35. package/dist/commands/ls.js.map +1 -1
  36. package/dist/commands/query.d.ts +8 -0
  37. package/dist/commands/query.d.ts.map +1 -1
  38. package/dist/commands/query.js +158 -51
  39. package/dist/commands/query.js.map +1 -1
  40. package/dist/commands/schema.d.ts.map +1 -1
  41. package/dist/commands/schema.js +3 -2
  42. package/dist/commands/schema.js.map +1 -1
  43. package/dist/commands/server.d.ts.map +1 -1
  44. package/dist/commands/server.js +8 -59
  45. package/dist/commands/server.js.map +1 -1
  46. package/dist/commands/setup-skill.d.ts +17 -0
  47. package/dist/commands/setup-skill.d.ts.map +1 -0
  48. package/dist/commands/setup-skill.js +131 -0
  49. package/dist/commands/setup-skill.js.map +1 -0
  50. package/dist/commands/trace.d.ts.map +1 -1
  51. package/dist/commands/trace.js +20 -10
  52. package/dist/commands/trace.js.map +1 -1
  53. package/dist/plugins/builtinPlugins.d.ts +10 -0
  54. package/dist/plugins/builtinPlugins.d.ts.map +1 -0
  55. package/dist/plugins/builtinPlugins.js +68 -0
  56. package/dist/plugins/builtinPlugins.js.map +1 -0
  57. package/dist/plugins/pluginLoader.d.ts +16 -0
  58. package/dist/plugins/pluginLoader.d.ts.map +1 -0
  59. package/dist/plugins/pluginLoader.js +101 -0
  60. package/dist/plugins/pluginLoader.js.map +1 -0
  61. package/dist/plugins/pluginResolver.js +38 -0
  62. package/dist/utils/codePreview.d.ts +1 -0
  63. package/dist/utils/codePreview.d.ts.map +1 -1
  64. package/dist/utils/codePreview.js +5 -3
  65. package/dist/utils/codePreview.js.map +1 -1
  66. package/dist/utils/formatNode.d.ts +1 -1
  67. package/dist/utils/formatNode.d.ts.map +1 -1
  68. package/dist/utils/formatNode.js +2 -2
  69. package/dist/utils/formatNode.js.map +1 -1
  70. package/dist/utils/pathUtils.d.ts +2 -0
  71. package/dist/utils/pathUtils.d.ts.map +1 -0
  72. package/dist/utils/pathUtils.js +9 -0
  73. package/dist/utils/pathUtils.js.map +1 -0
  74. package/dist/utils/progressRenderer.d.ts +4 -0
  75. package/dist/utils/progressRenderer.d.ts.map +1 -1
  76. package/dist/utils/progressRenderer.js +23 -4
  77. package/dist/utils/progressRenderer.js.map +1 -1
  78. package/package.json +7 -9
  79. package/skills/grafema-codebase-analysis/SKILL.md +295 -0
  80. package/skills/grafema-codebase-analysis/references/node-edge-types.md +123 -0
  81. package/skills/grafema-codebase-analysis/references/query-patterns.md +205 -0
  82. package/src/cli.ts +8 -2
  83. package/src/commands/analyze.ts +5 -435
  84. package/src/commands/analyzeAction.ts +284 -0
  85. package/src/commands/check.ts +2 -2
  86. package/src/commands/context.ts +309 -0
  87. package/src/commands/doctor/checks.ts +1 -1
  88. package/src/commands/explain.ts +4 -3
  89. package/src/commands/explore.tsx +7 -5
  90. package/src/commands/file.ts +179 -0
  91. package/src/commands/impact.ts +2 -3
  92. package/src/commands/init.ts +13 -1
  93. package/src/commands/ls.ts +3 -2
  94. package/src/commands/query.ts +167 -52
  95. package/src/commands/schema.ts +3 -2
  96. package/src/commands/server.ts +8 -64
  97. package/src/commands/setup-skill.ts +162 -0
  98. package/src/commands/trace.ts +18 -9
  99. package/src/plugins/builtinPlugins.ts +108 -0
  100. package/src/plugins/pluginLoader.ts +123 -0
  101. package/src/plugins/pluginResolver.js +38 -0
  102. package/src/utils/codePreview.ts +7 -3
  103. package/src/utils/formatNode.ts +3 -3
  104. package/src/utils/pathUtils.ts +9 -0
  105. package/src/utils/progressRenderer.ts +25 -4
@@ -0,0 +1,295 @@
1
+ ---
2
+ name: grafema-codebase-analysis
3
+ description: >
4
+ Analyze codebases using a graph database instead of reading source files.
5
+ Use when understanding code architecture, finding functions or call patterns,
6
+ tracing data flow, checking dependencies, or answering "where is X used?"
7
+ questions. Grafema builds a queryable code graph from static analysis —
8
+ prefer querying the graph over reading files manually.
9
+ license: Apache-2.0
10
+ compatibility: Requires Grafema MCP server configured (grafema or @grafema/mcp package)
11
+ metadata:
12
+ author: Grafema
13
+ version: "0.2.5"
14
+ ---
15
+
16
+ # Grafema: Graph-Based Codebase Analysis
17
+
18
+ ## Core Principle
19
+
20
+ **Query the graph, not read code.**
21
+
22
+ Grafema builds a graph database from your codebase via static analysis.
23
+ Instead of reading dozens of files to understand how code connects,
24
+ query the graph to get structured, complete answers instantly.
25
+
26
+ ```
27
+ BAD: Read 20 files hoping to find all callers of a function
28
+ GOOD: find_calls({ name: "processPayment" }) -> get all callers in one query
29
+
30
+ BAD: Grep for variable name across files, miss aliased references
31
+ GOOD: trace_dataflow({ source: "userInput", direction: "forward" }) -> complete data flow
32
+
33
+ BAD: Read file by file to understand module dependencies
34
+ GOOD: get_file_overview({ file: "src/api.ts" }) -> structured imports, exports, classes, functions
35
+ ```
36
+
37
+ ### When to Use Grafema
38
+
39
+ - Finding where functions/methods are called
40
+ - Understanding module dependencies and imports
41
+ - Tracing data flow (forward or backward)
42
+ - Getting function details (signature, callers, callees)
43
+ - Checking code invariants with Datalog rules
44
+ - Exploring file structure and entity relationships
45
+
46
+ ### When NOT to Use Grafema
47
+
48
+ - Reading a single specific file (use your editor/Read tool — faster)
49
+ - Editing code (Grafema is read-only analysis)
50
+ - Runtime behavior questions (Grafema is static analysis)
51
+ - Files not yet analyzed (run `analyze_project` first)
52
+
53
+ ## Essential Tools (Tier 1)
54
+
55
+ These 5 tools handle ~80% of queries. Start here.
56
+
57
+ ### find_nodes — Find entities by type, name, or file
58
+
59
+ ```json
60
+ find_nodes({ type: "FUNCTION", name: "validateUser" })
61
+ find_nodes({ type: "CLASS", file: "src/auth.ts" })
62
+ find_nodes({ type: "http:request" })
63
+ find_nodes({ type: "MODULE" })
64
+ ```
65
+
66
+ **Use when:** "Find all X", "What functions are in file Y", "List all routes"
67
+
68
+ **Node types:** MODULE, FUNCTION, METHOD, CLASS, VARIABLE, PARAMETER,
69
+ CALL, PROPERTY_ACCESS, METHOD_CALL, CALL_SITE,
70
+ http:route, http:request, db:query, socketio:emit, socketio:on
71
+
72
+ ### find_calls — Find function/method call sites
73
+
74
+ ```json
75
+ find_calls({ name: "processPayment" })
76
+ find_calls({ name: "query", className: "Database" })
77
+ ```
78
+
79
+ **Use when:** "Where is X called?", "Who calls this function?", "Find all usages"
80
+
81
+ Returns call sites with file locations and whether the target is resolved.
82
+
83
+ ### get_function_details — Complete function info
84
+
85
+ ```json
86
+ get_function_details({ name: "handleRequest" })
87
+ get_function_details({ name: "validate", file: "src/auth.ts" })
88
+ get_function_details({ name: "processOrder", transitive: true })
89
+ ```
90
+
91
+ **Use when:** "What does function X do?", "What does it call?", "Who calls it?"
92
+
93
+ Returns: signature, parameters, what it calls, who calls it.
94
+ Use `transitive: true` to follow call chains (A calls B calls C, max depth 5).
95
+
96
+ ### get_context — Deep context for any node
97
+
98
+ ```json
99
+ get_context({ semanticId: "src/api.ts:handleRequest#fn" })
100
+ get_context({ semanticId: "src/db.ts:Database#class", edgeType: "CALLS" })
101
+ ```
102
+
103
+ **Use when:** "Tell me everything about this entity", "Show me its relationships"
104
+
105
+ Returns: node info, source code, ALL incoming/outgoing edges with code context.
106
+ Use after `find_nodes` to deep-dive into a specific result.
107
+
108
+ ### trace_dataflow — Trace data flow
109
+
110
+ ```json
111
+ trace_dataflow({ source: "userInput", file: "src/handler.ts", direction: "forward" })
112
+ trace_dataflow({ source: "dbResult", file: "src/query.ts", direction: "backward" })
113
+ trace_dataflow({ source: "config", direction: "both", max_depth: 5 })
114
+ ```
115
+
116
+ **Use when:** "Where does this value end up?", "Where does this data come from?",
117
+ "Is user input reaching the database unsanitized?"
118
+
119
+ Directions: `forward` (where does it go?), `backward` (where did it come from?), `both`.
120
+
121
+ ## Decision Tree
122
+
123
+ ```
124
+ START: What do you need?
125
+ |
126
+ |-- "Find entities (functions, classes, routes)"
127
+ | -> find_nodes({ type, name, file })
128
+ |
129
+ |-- "Find who calls function X"
130
+ | -> find_calls({ name: "X" })
131
+ | -> For full details: get_function_details({ name: "X" })
132
+ |
133
+ |-- "Understand a specific entity deeply"
134
+ | -> First: find_nodes to get its semantic ID
135
+ | -> Then: get_context({ semanticId: "..." })
136
+ |
137
+ |-- "Trace data flow"
138
+ | -> trace_dataflow({ source, file, direction })
139
+ |
140
+ |-- "Understand a file's structure"
141
+ | -> get_file_overview({ file: "path/to/file.ts" })
142
+ |
143
+ |-- "Trace an alias/re-export chain"
144
+ | -> trace_alias({ variableName: "alias", file: "path.ts" })
145
+ |
146
+ |-- "Check a code rule/invariant"
147
+ | -> check_invariant({ rule: "violation(X) :- ..." })
148
+ |
149
+ |-- "Custom complex query"
150
+ | -> query_graph({ query: "violation(X) :- ..." })
151
+ | -> See references/query-patterns.md for Datalog syntax
152
+ |
153
+ |-- "Explore unknown codebase"
154
+ | -> get_stats() for high-level overview
155
+ | -> get_schema() for available node/edge types
156
+ | -> find_nodes({ type: "MODULE" }) for module list
157
+ | -> get_file_overview for specific files
158
+ ```
159
+
160
+ ## Common Workflows
161
+
162
+ ### 1. Impact Analysis: "What breaks if I change function X?"
163
+
164
+ ```
165
+ get_function_details({ name: "X", transitive: true })
166
+ -> Check calledBy array for all callers (direct + transitive)
167
+ -> For critical callers: get_context({ semanticId }) for full picture
168
+ ```
169
+
170
+ ### 2. Security Audit: "Does user input reach the database?"
171
+
172
+ ```
173
+ find_nodes({ type: "http:request" })
174
+ -> For each route, trace_dataflow({ source: requestParam, direction: "forward" })
175
+ -> Check if flow reaches db:query nodes
176
+ -> Use find_guards to check for sanitization
177
+ ```
178
+
179
+ ### 3. Onboarding: "How is this codebase structured?"
180
+
181
+ ```
182
+ get_stats() -> Node/edge counts by type
183
+ find_nodes({ type: "MODULE" }) -> All modules
184
+ get_file_overview({ file: "src/index.ts" }) -> Entry point structure
185
+ find_nodes({ type: "http:request" }) -> All API endpoints
186
+ ```
187
+
188
+ ### 4. Dependency Analysis: "What does module X depend on?"
189
+
190
+ ```
191
+ get_file_overview({ file: "src/service.ts" })
192
+ -> Check imports section for dependencies
193
+ -> For each import: get_context for deeper relationships
194
+ ```
195
+
196
+ ### 5. Find Dead Code: "What functions have no callers?"
197
+
198
+ ```
199
+ query_graph({
200
+ query: 'violation(X) :- node(X, "FUNCTION"), \\+ edge(_, X, "CALLS").'
201
+ })
202
+ ```
203
+
204
+ ## Anti-Patterns
205
+
206
+ **Don't read files to find call sites.** Use `find_calls` — it finds ALL callers across
207
+ the entire codebase, including indirect references you'd miss by grepping.
208
+
209
+ **Don't use `query_graph` for simple lookups.** `find_nodes`, `find_calls`, and
210
+ `get_function_details` are optimized for common queries. Reserve Datalog for
211
+ complex patterns (joins, transitive closure, invariant checks).
212
+
213
+ **Don't skip analysis status.** If you just ran `analyze_project`, check
214
+ `get_analysis_status` before querying — partial results are misleading.
215
+
216
+ **Don't request excessive depth.** `get_context` with no filters returns everything.
217
+ Use `edgeType` filter to focus on specific relationships (e.g., `"CALLS,ASSIGNED_FROM"`).
218
+
219
+ **Don't use Grafema for single-file questions.** If you only need to read one file,
220
+ use your editor. Grafema shines for cross-file relationships.
221
+
222
+ ## Advanced Tools (Tier 2)
223
+
224
+ ### query_graph — Custom Datalog queries
225
+
226
+ For complex patterns not covered by high-level tools.
227
+ See [references/query-patterns.md](references/query-patterns.md) for syntax and examples.
228
+
229
+ ```json
230
+ query_graph({
231
+ query: "violation(X) :- node(X, \"CALL\"), attr(X, \"name\", \"eval\").",
232
+ explain: true
233
+ })
234
+ ```
235
+
236
+ Available predicates: `node(Id, Type)`, `edge(Src, Dst, Type)`, `attr(Id, Name, Value)`.
237
+ Must define `violation/1` predicate for results. Use `explain: true` to debug empty results.
238
+
239
+ ### get_file_overview — File-level structure
240
+
241
+ Structured overview of imports, exports, classes, functions, variables with relationships.
242
+ Recommended first step when exploring a specific file before using `get_context`.
243
+
244
+ ### trace_alias — Resolve alias chains
245
+
246
+ For code like `const alias = obj.method; alias()` — traces "alias" back to "obj.method".
247
+
248
+ ### get_schema — Available types
249
+
250
+ Returns all node and edge types in the graph. Use when you need exact type names.
251
+
252
+ ### check_invariant — Code rule checking
253
+
254
+ Check if a Datalog rule has violations. For persistent rules, use `create_guarantee`.
255
+
256
+ ## Specialized Tools (Tier 3)
257
+
258
+ | Tool | Purpose |
259
+ |------|---------|
260
+ | get_stats | Graph statistics (node/edge counts by type) |
261
+ | get_coverage | Analysis coverage for a path |
262
+ | find_guards | Conditional guards protecting a node |
263
+ | create_guarantee | Create persistent code invariant |
264
+ | list_guarantees | List all guarantees |
265
+ | check_guarantees | Check guarantee violations |
266
+ | delete_guarantee | Remove a guarantee |
267
+ | discover_services | Discover services without full analysis |
268
+ | analyze_project | Run/re-run analysis |
269
+ | get_analysis_status | Check analysis progress |
270
+ | read_project_structure | Directory tree |
271
+ | write_config | Update .grafema/config.yaml |
272
+ | get_documentation | Grafema usage docs |
273
+ | report_issue | Report bugs |
274
+
275
+ ## Troubleshooting
276
+
277
+ **Query returns nothing?**
278
+ 1. Check analysis ran: `get_analysis_status`
279
+ 2. Check type names: `get_schema` for available types
280
+ 3. Use `explain: true` in `query_graph` to debug
281
+ 4. Check file paths match (relative to project root)
282
+
283
+ **Need help with Datalog syntax?**
284
+ - See [references/query-patterns.md](references/query-patterns.md)
285
+ - Use `get_documentation({ topic: "queries" })` for inline help
286
+
287
+ **Graph seems incomplete?**
288
+ - Run `get_coverage({ path: "src/" })` to check coverage
289
+ - Re-analyze with `analyze_project({ force: true })`
290
+ - Check `.grafema/config.yaml` for include/exclude patterns
291
+
292
+ ## References
293
+
294
+ - [Node and Edge Types](references/node-edge-types.md) — Complete graph schema
295
+ - [Query Patterns](references/query-patterns.md) — Datalog cookbook with examples
@@ -0,0 +1,123 @@
1
+ # Grafema Graph Schema: Node and Edge Types
2
+
3
+ ## Node Types
4
+
5
+ ### Core Entities
6
+
7
+ | Type | Description | Key Attributes |
8
+ |------|-------------|----------------|
9
+ | `MODULE` | A source file/module | name, file |
10
+ | `FUNCTION` | Function declaration | name, file, line, async |
11
+ | `METHOD` | Class method | name, file, line, className |
12
+ | `CLASS` | Class declaration | name, file, line |
13
+ | `VARIABLE` | Variable declaration | name, file, line, kind (const/let/var) |
14
+ | `PARAMETER` | Function parameter | name, file, line, index |
15
+
16
+ ### Call & Access Nodes
17
+
18
+ | Type | Description | Key Attributes |
19
+ |------|-------------|----------------|
20
+ | `CALL` | Function call expression | name, file, line, resolved |
21
+ | `METHOD_CALL` | Method call expression | name, file, line, object, resolved |
22
+ | `CALL_SITE` | Call site context | file, line |
23
+ | `PROPERTY_ACCESS` | Property access (obj.prop) | name, object, file, line |
24
+
25
+ ### Domain-Specific Nodes
26
+
27
+ | Type | Description | Key Attributes |
28
+ |------|-------------|----------------|
29
+ | `http:route` | HTTP route definition | path, method |
30
+ | `http:request` | HTTP request handler | path, method, file, line |
31
+ | `db:query` | Database query | file, line |
32
+ | `socketio:emit` | Socket.IO emit call | event, file, line |
33
+ | `socketio:on` | Socket.IO event listener | event, file, line |
34
+
35
+ ### Structural Nodes
36
+
37
+ | Type | Description |
38
+ |------|-------------|
39
+ | `SCOPE` | Code scope (function body, if block, loop) |
40
+ | `OBJECT_LITERAL` | Object literal expression |
41
+ | `ARRAY_LITERAL` | Array literal expression |
42
+ | `LITERAL` | Primitive literal value |
43
+ | `IMPORT` | Import declaration |
44
+ | `EXPORT` | Export declaration |
45
+
46
+ ## Edge Types
47
+
48
+ | Type | Direction | Description |
49
+ |------|-----------|-------------|
50
+ | `CONTAINS` | Parent -> Child | Structural containment (module contains function) |
51
+ | `CALLS` | Caller -> Callee | Function/method call relationship |
52
+ | `DEPENDS_ON` | Module -> Module | Module dependency (import) |
53
+ | `ASSIGNED_FROM` | Variable -> Source | Value assignment source |
54
+ | `INSTANCE_OF` | Instance -> Class | Class instantiation |
55
+ | `PASSES_ARGUMENT` | Call -> Value | Argument passing at call site |
56
+ | `HAS_SCOPE` | Function -> Scope | Function's scope chain |
57
+ | `EXTENDS` | Class -> Class | Class inheritance |
58
+ | `IMPLEMENTS` | Class -> Interface | Interface implementation |
59
+ | `RETURNS` | Function -> Type | Return type relationship |
60
+ | `DATAFLOW` | Source -> Sink | Data flow edge |
61
+ | `GUARDED_BY` | Node -> Scope | Conditional guard relationship |
62
+
63
+ ## Common Attribute Names
64
+
65
+ These can be used with `attr(Id, Name, Value)` in Datalog queries:
66
+
67
+ | Attribute | Types | Description |
68
+ |-----------|-------|-------------|
69
+ | `name` | All named nodes | Entity name |
70
+ | `file` | All nodes | Source file path (relative) |
71
+ | `line` | All located nodes | Line number |
72
+ | `column` | All located nodes | Column number |
73
+ | `type` | All nodes | Node type (via `node(Id, Type)`) |
74
+ | `async` | FUNCTION, METHOD | Is async function |
75
+ | `kind` | VARIABLE | const, let, or var |
76
+ | `method` | http:request | HTTP method (GET, POST, etc.) |
77
+ | `path` | http:request, http:route | URL path pattern |
78
+ | `resolved` | CALL, METHOD_CALL | Whether call target is resolved |
79
+ | `object` | METHOD_CALL, PROPERTY_ACCESS | Receiver object name |
80
+ | `className` | METHOD | Owning class name |
81
+ | `event` | socketio:emit, socketio:on | Socket.IO event name |
82
+
83
+ ## Quick Reference: Finding Common Patterns
84
+
85
+ ### Find all functions
86
+ ```
87
+ node(X, "FUNCTION")
88
+ ```
89
+
90
+ ### Find all classes
91
+ ```
92
+ node(X, "CLASS")
93
+ ```
94
+
95
+ ### Find all HTTP endpoints
96
+ ```
97
+ node(X, "http:request")
98
+ ```
99
+
100
+ ### Find calls to a specific function
101
+ ```
102
+ node(X, "CALL"), attr(X, "name", "targetFunction")
103
+ ```
104
+
105
+ ### Find all methods of a class
106
+ ```
107
+ node(C, "CLASS"), attr(C, "name", "MyClass"), edge(C, M, "CONTAINS"), node(M, "METHOD")
108
+ ```
109
+
110
+ ### Find module dependencies
111
+ ```
112
+ edge(A, B, "DEPENDS_ON"), node(A, "MODULE"), node(B, "MODULE")
113
+ ```
114
+
115
+ ### Find data flow from a variable
116
+ ```
117
+ node(X, "VARIABLE"), attr(X, "name", "myVar"), edge(X, Y, "DATAFLOW")
118
+ ```
119
+
120
+ ### Find unresolved calls
121
+ ```
122
+ node(X, "CALL"), attr(X, "resolved", "false")
123
+ ```
@@ -0,0 +1,205 @@
1
+ # Grafema Datalog Query Patterns
2
+
3
+ All queries use `query_graph` tool. Every query must define a `violation/1` predicate —
4
+ matching nodes are returned as results.
5
+
6
+ ## Syntax Quick Reference
7
+
8
+ ```
9
+ violation(X) :- <body>. # Rule: X is a result if body is true
10
+ node(X, "TYPE") # Match node by type
11
+ edge(X, Y, "TYPE") # Match edge: X -> Y of given type
12
+ attr(X, "name", "value") # Match node attribute
13
+ \+ <condition> # Negation: condition is NOT true
14
+ ```
15
+
16
+ ## Basic Patterns
17
+
18
+ ### Find nodes by type
19
+
20
+ ```datalog
21
+ violation(X) :- node(X, "FUNCTION").
22
+ ```
23
+
24
+ ### Find nodes by type and name
25
+
26
+ ```datalog
27
+ violation(X) :- node(X, "FUNCTION"), attr(X, "name", "processPayment").
28
+ ```
29
+
30
+ ### Find nodes by type and file
31
+
32
+ ```datalog
33
+ violation(X) :- node(X, "FUNCTION"), attr(X, "file", "src/api.ts").
34
+ ```
35
+
36
+ ### Find nodes matching multiple criteria
37
+
38
+ ```datalog
39
+ violation(X) :- node(X, "CALL"), attr(X, "name", "eval"), attr(X, "file", "src/handler.ts").
40
+ ```
41
+
42
+ ## Edge Traversal
43
+
44
+ ### One-hop: Find all calls from a function
45
+
46
+ ```datalog
47
+ violation(Call) :-
48
+ node(F, "FUNCTION"), attr(F, "name", "main"),
49
+ edge(F, S, "HAS_SCOPE"), edge(S, Call, "CONTAINS"),
50
+ node(Call, "CALL").
51
+ ```
52
+
53
+ ### One-hop: Find all callers of a function
54
+
55
+ ```datalog
56
+ violation(Caller) :-
57
+ node(Target, "FUNCTION"), attr(Target, "name", "validate"),
58
+ edge(Call, Target, "CALLS"),
59
+ edge(Scope, Call, "CONTAINS"),
60
+ edge(Caller, Scope, "HAS_SCOPE"),
61
+ node(Caller, "FUNCTION").
62
+ ```
63
+
64
+ ### Find module dependencies
65
+
66
+ ```datalog
67
+ violation(Dep) :-
68
+ node(M, "MODULE"), attr(M, "name", "api"),
69
+ edge(M, Dep, "DEPENDS_ON"), node(Dep, "MODULE").
70
+ ```
71
+
72
+ ### Find what a variable is assigned from
73
+
74
+ ```datalog
75
+ violation(Source) :-
76
+ node(V, "VARIABLE"), attr(V, "name", "config"),
77
+ edge(V, Source, "ASSIGNED_FROM").
78
+ ```
79
+
80
+ ## Negation Patterns
81
+
82
+ ### Functions with no callers (potential dead code)
83
+
84
+ ```datalog
85
+ violation(X) :-
86
+ node(X, "FUNCTION"),
87
+ \+ edge(_, X, "CALLS").
88
+ ```
89
+
90
+ ### Modules with no dependents (unused modules)
91
+
92
+ ```datalog
93
+ violation(X) :-
94
+ node(X, "MODULE"),
95
+ \+ edge(_, X, "DEPENDS_ON").
96
+ ```
97
+
98
+ ### Unresolved calls (external/dynamic targets)
99
+
100
+ ```datalog
101
+ violation(X) :-
102
+ node(X, "CALL"),
103
+ attr(X, "resolved", "false").
104
+ ```
105
+
106
+ ## Invariant Patterns
107
+
108
+ ### No eval() usage
109
+
110
+ ```datalog
111
+ violation(X) :- node(X, "CALL"), attr(X, "name", "eval").
112
+ ```
113
+
114
+ ### No direct database queries outside service layer
115
+
116
+ ```datalog
117
+ violation(X) :-
118
+ node(X, "db:query"),
119
+ attr(X, "file", File),
120
+ \+ attr(X, "file", "src/services/").
121
+ ```
122
+
123
+ Note: File matching is exact. For pattern matching, use the `find_nodes` tool instead.
124
+
125
+ ### All HTTP endpoints must have handlers
126
+
127
+ ```datalog
128
+ violation(X) :-
129
+ node(X, "http:request"),
130
+ \+ edge(_, X, "CALLS").
131
+ ```
132
+
133
+ ## Join Patterns
134
+
135
+ ### Find functions that call both X and Y
136
+
137
+ ```datalog
138
+ violation(F) :-
139
+ node(F, "FUNCTION"),
140
+ edge(F, S, "HAS_SCOPE"),
141
+ edge(S, C1, "CONTAINS"), node(C1, "CALL"), attr(C1, "name", "readFile"),
142
+ edge(S, C2, "CONTAINS"), node(C2, "CALL"), attr(C2, "name", "writeFile").
143
+ ```
144
+
145
+ ### Find classes that extend a specific base class
146
+
147
+ ```datalog
148
+ violation(Child) :-
149
+ node(Base, "CLASS"), attr(Base, "name", "BaseService"),
150
+ edge(Child, Base, "EXTENDS"),
151
+ node(Child, "CLASS").
152
+ ```
153
+
154
+ ## Performance Tips
155
+
156
+ 1. **Put most selective filters first.** `attr(X, "name", "specific")` before `node(X, "FUNCTION")`.
157
+
158
+ 2. **Avoid unconstrained joins.** Every variable should be bounded by at least one specific condition.
159
+
160
+ 3. **Use high-level tools when possible.** `find_calls` is faster than writing a Datalog query for the same pattern — it uses optimized indexes.
161
+
162
+ 4. **Use `explain: true` to debug.** If a query returns nothing, add `explain: true` to see step-by-step execution.
163
+
164
+ 5. **Use `limit` and `offset` for large result sets.** Default limit applies, but you can paginate through results.
165
+
166
+ ## Common Mistakes
167
+
168
+ ### Wrong: Unbound variable
169
+
170
+ ```datalog
171
+ # BAD: Y is never constrained
172
+ violation(X) :- node(X, "FUNCTION"), edge(X, Y, "CALLS").
173
+ ```
174
+
175
+ ```datalog
176
+ # GOOD: Constrain Y
177
+ violation(X) :- node(X, "FUNCTION"), edge(X, Y, "CALLS"), node(Y, "FUNCTION").
178
+ ```
179
+
180
+ ### Wrong: Using wrong edge direction
181
+
182
+ ```datalog
183
+ # BAD: CALLS edge goes Caller -> Callee, not reverse
184
+ violation(X) :- node(X, "FUNCTION"), edge(X, Target, "CALLS").
185
+ ```
186
+
187
+ The CALLS edge typically goes from CALL/CALL_SITE node to target FUNCTION,
188
+ not directly from FUNCTION to FUNCTION. Check [node-edge-types.md](node-edge-types.md)
189
+ for correct edge directions.
190
+
191
+ ### Wrong: Missing scope traversal
192
+
193
+ Functions don't directly CONTAIN calls — they have scopes that contain calls:
194
+
195
+ ```datalog
196
+ # BAD: No direct CONTAINS edge from FUNCTION to CALL
197
+ violation(C) :- node(F, "FUNCTION"), edge(F, C, "CONTAINS"), node(C, "CALL").
198
+ ```
199
+
200
+ ```datalog
201
+ # GOOD: Go through HAS_SCOPE
202
+ violation(C) :-
203
+ node(F, "FUNCTION"), edge(F, S, "HAS_SCOPE"),
204
+ edge(S, C, "CONTAINS"), node(C, "CALL").
205
+ ```
package/src/cli.ts CHANGED
@@ -16,7 +16,8 @@ import { lsCommand } from './commands/ls.js';
16
16
  import { getCommand } from './commands/get.js';
17
17
  import { traceCommand } from './commands/trace.js';
18
18
  import { impactCommand } from './commands/impact.js';
19
- import { exploreCommand } from './commands/explore.js';
19
+ import { contextCommand } from './commands/context.js';
20
+
20
21
  import { statsCommand } from './commands/stats.js';
21
22
  import { checkCommand } from './commands/check.js';
22
23
  import { serverCommand } from './commands/server.js';
@@ -24,6 +25,8 @@ import { coverageCommand } from './commands/coverage.js';
24
25
  import { doctorCommand } from './commands/doctor.js';
25
26
  import { schemaCommand } from './commands/schema.js';
26
27
  import { explainCommand } from './commands/explain.js';
28
+ import { fileCommand } from './commands/file.js';
29
+ import { setupSkillCommand } from './commands/setup-skill.js';
27
30
 
28
31
  // Read version from package.json
29
32
  const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -41,12 +44,13 @@ program.addCommand(initCommand);
41
44
  program.addCommand(analyzeCommand);
42
45
  program.addCommand(overviewCommand);
43
46
  program.addCommand(queryCommand);
47
+ program.addCommand(contextCommand);
44
48
  program.addCommand(typesCommand);
45
49
  program.addCommand(lsCommand);
46
50
  program.addCommand(getCommand);
47
51
  program.addCommand(traceCommand);
48
52
  program.addCommand(impactCommand);
49
- program.addCommand(exploreCommand);
53
+
50
54
  program.addCommand(statsCommand); // Keep for backwards compat
51
55
  program.addCommand(coverageCommand);
52
56
  program.addCommand(checkCommand);
@@ -54,5 +58,7 @@ program.addCommand(serverCommand);
54
58
  program.addCommand(doctorCommand);
55
59
  program.addCommand(schemaCommand);
56
60
  program.addCommand(explainCommand);
61
+ program.addCommand(fileCommand);
62
+ program.addCommand(setupSkillCommand);
57
63
 
58
64
  program.parse();