@grafema/cli 0.2.5-beta → 0.2.6-beta

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 (100) 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/package.json +7 -9
  75. package/skills/grafema-codebase-analysis/SKILL.md +295 -0
  76. package/skills/grafema-codebase-analysis/references/node-edge-types.md +123 -0
  77. package/skills/grafema-codebase-analysis/references/query-patterns.md +205 -0
  78. package/src/cli.ts +8 -2
  79. package/src/commands/analyze.ts +5 -435
  80. package/src/commands/analyzeAction.ts +284 -0
  81. package/src/commands/check.ts +2 -2
  82. package/src/commands/context.ts +309 -0
  83. package/src/commands/doctor/checks.ts +1 -1
  84. package/src/commands/explain.ts +4 -3
  85. package/src/commands/explore.tsx +7 -5
  86. package/src/commands/file.ts +179 -0
  87. package/src/commands/impact.ts +2 -3
  88. package/src/commands/init.ts +13 -1
  89. package/src/commands/ls.ts +3 -2
  90. package/src/commands/query.ts +167 -52
  91. package/src/commands/schema.ts +3 -2
  92. package/src/commands/server.ts +8 -64
  93. package/src/commands/setup-skill.ts +162 -0
  94. package/src/commands/trace.ts +18 -9
  95. package/src/plugins/builtinPlugins.ts +108 -0
  96. package/src/plugins/pluginLoader.ts +123 -0
  97. package/src/plugins/pluginResolver.js +38 -0
  98. package/src/utils/codePreview.ts +7 -3
  99. package/src/utils/formatNode.ts +3 -3
  100. package/src/utils/pathUtils.ts +9 -0
@@ -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();