-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_process.lua
More file actions
220 lines (186 loc) · 6.58 KB
/
index_process.lua
File metadata and controls
220 lines (186 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
-- Index Process: long-running actor holding the code graph
-- Receives index and query messages from MCP tools
-- Entry kind: process.lua
local logger = require("logger")
local time = require("time")
local index_lib = require("index_lib")
local query_lib = require("query_lib")
local graph_lib = require("graph")
local function reply(dest, topic, data)
local ok, err = process.send(dest, topic, data)
if err then
logger:warn("Failed to reply", {dest = dest, topic = topic, error = tostring(err)})
end
end
local function reply_error(dest, message)
reply(dest, "error", {message = message})
end
---------------------------------------------------------------------------
-- Message handlers
---------------------------------------------------------------------------
local function handle_index(state, payload)
local start = os.clock()
local result, err = index_lib.index_project(
state,
payload.project,
payload.path
)
if err then
reply_error(payload.reply_to, err)
return
end
result.duration = string.format("%.2fs", os.clock() - start)
reply(payload.reply_to, "result", result)
end
local function handle_status(state, payload)
if not state.indexed_at then
reply(payload.reply_to, "result", {
indexed = false,
message = "No project indexed. Run code_index first.",
})
return
end
local s = graph_lib.stats(state)
reply(payload.reply_to, "result", {
indexed = true,
project = state.project,
indexed_at = state.indexed_at,
stats = s,
})
end
local function handle_query_usages(state, payload)
if not state.indexed_at then
reply_error(payload.reply_to, "No project indexed. Run code_index first.")
return
end
local refs, primary = query_lib.find_usages(state, payload.symbol, payload.kind)
reply(payload.reply_to, "result", {
symbol = primary and primary.id or payload.symbol,
symbol_info = primary,
references = refs,
count = #refs,
})
end
local function handle_query_deps(state, payload)
if not state.indexed_at then
reply_error(payload.reply_to, "No project indexed. Run code_index first.")
return
end
local tree = query_lib.build_dep_tree(
state,
payload.file,
payload.direction,
payload.depth
)
reply(payload.reply_to, "result", tree)
end
local function handle_query_callers(state, payload)
if not state.indexed_at then
reply_error(payload.reply_to, "No project indexed. Run code_index first.")
return
end
local tree = query_lib.find_callers(state, payload.symbol, payload.depth)
reply(payload.reply_to, "result", tree)
end
local function handle_query_callees(state, payload)
if not state.indexed_at then
reply_error(payload.reply_to, "No project indexed. Run code_index first.")
return
end
local tree = query_lib.find_callees(state, payload.symbol, payload.depth)
reply(payload.reply_to, "result", tree)
end
local function handle_query_hierarchy(state, payload)
if not state.indexed_at then
reply_error(payload.reply_to, "No project indexed. Run code_index first.")
return
end
local tree = query_lib.build_hierarchy(state, payload.symbol, payload.direction)
reply(payload.reply_to, "result", tree)
end
local function handle_query_impacted(state, payload)
if not state.indexed_at then
reply_error(payload.reply_to, "No project indexed. Run code_index first.")
return
end
local result = query_lib.find_impacted(state, payload.file, payload.depth)
reply(payload.reply_to, "result", result)
end
local function handle_query_symbol(state, payload)
if not state.indexed_at then
reply_error(payload.reply_to, "No project indexed. Run code_index first.")
return
end
local symbols = graph_lib.find_symbol(state, payload.name, payload.kind)
reply(payload.reply_to, "result", {
symbols = symbols,
count = #symbols,
})
end
---------------------------------------------------------------------------
-- Topic router
---------------------------------------------------------------------------
local handlers = {
["index"] = handle_index,
["index:status"] = handle_status,
["query:usages"] = handle_query_usages,
["query:deps"] = handle_query_deps,
["query:callers"] = handle_query_callers,
["query:callees"] = handle_query_callees,
["query:hierarchy"] = handle_query_hierarchy,
["query:impacted"] = handle_query_impacted,
["query:symbol"] = handle_query_symbol,
}
---------------------------------------------------------------------------
-- Main process loop
---------------------------------------------------------------------------
local function main()
local state = graph_lib.new_state()
local ok, reg_err = process.registry.register("code_graph_index")
if reg_err then
logger:error("Failed to register", {error = tostring(reg_err)})
return 1
end
logger:info("code_graph_index started", {pid = process.pid()})
local inbox = process.inbox()
local events = process.events()
while true do
local r = channel.select {
inbox:case_receive(),
events:case_receive()
}
if r.channel == events then
local event = r.value
if event.kind == process.event.CANCEL then
logger:info("code_graph_index shutting down")
return 0
end
else
local msg = r.value
local topic = msg:topic()
local payload = msg:payload():data() or {}
local handler = handlers[topic]
if handler then
-- Wrap in pcall to prevent crashes from bad queries
local success, err = pcall(handler, state, payload)
if not success then
logger:error("Handler error", {
topic = topic,
error = tostring(err),
})
if payload.reply_to then
reply_error(payload.reply_to,
"Internal error: " .. tostring(err))
end
end
else
logger:warn("Unknown topic", {topic = topic})
if payload.reply_to then
reply_error(payload.reply_to,
"Unknown topic: " .. topic)
end
end
end
end
end
return { main = main }