rubik 0.9.1 → 0.10.0
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.
- checksums.yaml +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +143 -1
- data/lib/rubik/adapter/base.rb +2 -1
- data/lib/rubik/adapter/process.rb +28 -67
- data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
- data/lib/rubik/buffer/apply_contract.rb +3 -3
- data/lib/rubik/buffer/apply_transaction.rb +165 -0
- data/lib/rubik/buffer/manager.rb +26 -120
- data/lib/rubik/config/deep_ops.rb +4 -1
- data/lib/rubik/config/defaults.rb +6 -3
- data/lib/rubik/config/schema.rb +8 -0
- data/lib/rubik/config/secret_policy.rb +197 -0
- data/lib/rubik/config.rb +11 -0
- data/lib/rubik/diagnostics/collector.rb +0 -9
- data/lib/rubik/errors.rb +15 -0
- data/lib/rubik/event_bus.rb +2 -1
- data/lib/rubik/kernel/dispatcher.rb +12 -7
- data/lib/rubik/kernel/unit.rb +4 -1
- data/lib/rubik/llm/registry.rb +12 -9
- data/lib/rubik/llm/router.rb +2 -5
- data/lib/rubik/log.rb +21 -0
- data/lib/rubik/lsp/client.rb +77 -273
- data/lib/rubik/lsp/manager.rb +14 -10
- data/lib/rubik/lsp/transport.rb +365 -0
- data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
- data/lib/rubik/mcp/boot_run.rb +190 -0
- data/lib/rubik/mcp/manager.rb +40 -163
- data/lib/rubik/mcp/transport/stdio.rb +4 -1
- data/lib/rubik/orchestrator/auto_apply.rb +4 -3
- data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
- data/lib/rubik/orchestrator/goal_session.rb +53 -39
- data/lib/rubik/orchestrator/loop.rb +28 -47
- data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
- data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
- data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
- data/lib/rubik/orchestrator/resume_window.rb +96 -0
- data/lib/rubik/orchestrator/shell_deps.rb +16 -0
- data/lib/rubik/orchestrator/stream_manager.rb +2 -1
- data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
- data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
- data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
- data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
- data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
- data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
- data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
- data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
- data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
- data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
- data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
- data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
- data/lib/rubik/platform/io_drain.rb +2 -1
- data/lib/rubik/platform/process.rb +144 -54
- data/lib/rubik/platform.rb +2 -1
- data/lib/rubik/plugin/manager.rb +2 -1
- data/lib/rubik/repl/commands/agent_context.rb +8 -7
- data/lib/rubik/repl/commands/agent_execution.rb +13 -32
- data/lib/rubik/repl/commands/agent_models.rb +4 -3
- data/lib/rubik/repl/commands/agent_providers.rb +43 -2
- data/lib/rubik/repl/commands/base.rb +47 -2
- data/lib/rubik/repl/commands/buffer.rb +5 -4
- data/lib/rubik/repl/commands/lsp.rb +2 -1
- data/lib/rubik/repl/commands/pruner.rb +2 -1
- data/lib/rubik/repl/commands/secrets.rb +151 -0
- data/lib/rubik/repl/commands/workspace.rb +60 -150
- data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
- data/lib/rubik/repl/config_wiring.rb +3 -3
- data/lib/rubik/repl/hooks.rb +2 -1
- data/lib/rubik/repl/input_execution.rb +158 -76
- data/lib/rubik/repl/input_loop.rb +2 -1
- data/lib/rubik/repl/lifecycle.rb +59 -209
- data/lib/rubik/repl/notice_router.rb +319 -0
- data/lib/rubik/repl/paste_support.rb +3 -1
- data/lib/rubik/repl/presentation.rb +5 -3
- data/lib/rubik/repl/pruner/gate.rb +3 -1
- data/lib/rubik/repl/pruner/harness.rb +2 -1
- data/lib/rubik/repl/shell.rb +4 -3
- data/lib/rubik/repl/turn_recorder.rb +6 -5
- data/lib/rubik/runtime/discovery.rb +3 -2
- data/lib/rubik/runtime/loop_service.rb +4 -3
- data/lib/rubik/runtime/manifest_store.rb +9 -1
- data/lib/rubik/runtime/overlay_guard.rb +127 -0
- data/lib/rubik/runtime/overlay_storage.rb +92 -146
- data/lib/rubik/runtime/task_manager.rb +64 -25
- data/lib/rubik/sandbox/driver/docker.rb +5 -1
- data/lib/rubik/sandbox/runner.rb +16 -15
- data/lib/rubik/security/command_gate.rb +117 -93
- data/lib/rubik/security/guard.rb +6 -2
- data/lib/rubik/security/secret_store.rb +463 -0
- data/lib/rubik/security/synthesis_gate.rb +8 -11
- data/lib/rubik/session/manager.rb +7 -1
- data/lib/rubik/skills/manager.rb +2 -3
- data/lib/rubik/storage/search.rb +49 -0
- data/lib/rubik/storage/store.rb +13 -20
- data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
- data/lib/rubik/tools/base.rb +22 -0
- data/lib/rubik/tools/builtins/files.rb +3 -15
- data/lib/rubik/tools/builtins/grep.rb +1 -6
- data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
- data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
- data/lib/rubik/tools/truncator.rb +48 -36
- data/lib/rubik/ui/banner.rb +4 -3
- data/lib/rubik/ui/spinner.rb +3 -1
- data/lib/rubik/ui/statusline.rb +6 -5
- data/lib/rubik/version.rb +1 -1
- data/lib/rubik.rb +1 -2
- metadata +13 -1
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "json"
|
|
5
|
+
require_relative "../platform"
|
|
6
|
+
require_relative "../log"
|
|
7
|
+
|
|
8
|
+
module Rubik
|
|
9
|
+
module LSP
|
|
10
|
+
# JSON-RPC-over-stdio transport for LSP::Client (issue #470). Owns the
|
|
11
|
+
# generic plumbing that has nothing to do with the LSP protocol:
|
|
12
|
+
#
|
|
13
|
+
# * process spawn/teardown (Open3 popen3, stderr drain, kill tree),
|
|
14
|
+
# * the reader thread and its byte-exact Content-Length framing,
|
|
15
|
+
# * the request/response correlation registry (@responses /
|
|
16
|
+
# @pending_requests / @reader_failure) guarded by @mutex + @cv,
|
|
17
|
+
# * thread-safe framed writes (@write_mutex).
|
|
18
|
+
#
|
|
19
|
+
# Protocol concerns (the initialize handshake, document sync, diagnostics
|
|
20
|
+
# routing, server-request handling) stay in Client, which delegates the
|
|
21
|
+
# plumbing here and receives every non-response incoming message through
|
|
22
|
+
# the on_message callback. stop (shutdown) remains the exact inverse of
|
|
23
|
+
# start (spawn): every resource the transport opens is retracted in LIFO
|
|
24
|
+
# order, and a failing inverse never aborts the unwind.
|
|
25
|
+
class Transport
|
|
26
|
+
class RequestError < StandardError
|
|
27
|
+
attr_reader :reason
|
|
28
|
+
|
|
29
|
+
def initialize(reason, message)
|
|
30
|
+
@reason = reason
|
|
31
|
+
super(message)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
attr_reader :mutex, :cv, :running
|
|
36
|
+
|
|
37
|
+
def initialize(root_dir:, on_message:)
|
|
38
|
+
@root_dir = root_dir
|
|
39
|
+
@on_message = on_message
|
|
40
|
+
@running = false
|
|
41
|
+
@crashed = false
|
|
42
|
+
@req_id = 0
|
|
43
|
+
@stdin = nil
|
|
44
|
+
@stdout = nil
|
|
45
|
+
@stderr = nil
|
|
46
|
+
@wait_thr = nil
|
|
47
|
+
@reader_thr = nil
|
|
48
|
+
@stderr_drain = nil
|
|
49
|
+
@reader_alive = false
|
|
50
|
+
@mutex = Mutex.new
|
|
51
|
+
@write_mutex = Mutex.new
|
|
52
|
+
@cv = ConditionVariable.new
|
|
53
|
+
@responses = {}
|
|
54
|
+
@pending_requests = {}
|
|
55
|
+
@reader_failure = nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Spawn the server process for +argv+ (already resolved to an absolute
|
|
59
|
+
# binary by the caller), install the reader thread and the stderr
|
|
60
|
+
# drain. Returns true; all failures raise and are handled by the
|
|
61
|
+
# caller's handshake rescue.
|
|
62
|
+
def spawn(argv)
|
|
63
|
+
@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(sanitized_spawn_env, *argv, chdir: @root_dir)
|
|
64
|
+
# Binary mode: LSP framing is byte-exact. Text-mode pipes on Windows
|
|
65
|
+
# translate CRLF→LF on read, so Content-Length counts and read(len)
|
|
66
|
+
# disagree and the JSON body arrives truncated (the reader's old
|
|
67
|
+
# silent rescue-then-break hid this — no real server was ever
|
|
68
|
+
# spawned in tests).
|
|
69
|
+
@stdin.binmode
|
|
70
|
+
@stdout.binmode
|
|
71
|
+
@running = false
|
|
72
|
+
@crashed = false
|
|
73
|
+
@reader_alive = false
|
|
74
|
+
|
|
75
|
+
start_reader_thread
|
|
76
|
+
# An unread stderr pipe fills its OS buffer (~64KB) and blocks the
|
|
77
|
+
# server itself mid-write; drain continuously, discard content.
|
|
78
|
+
@stderr_drain = Rubik::Platform::IoDrain.start(@stderr)
|
|
79
|
+
true
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Called by Client after a successful initialize handshake; only then
|
|
83
|
+
# does alive? report true (mirrors the pre-extraction @running = true).
|
|
84
|
+
def mark_running
|
|
85
|
+
@running = true
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def crashed?
|
|
89
|
+
@crashed == true
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def alive?
|
|
93
|
+
return false unless @running
|
|
94
|
+
return false unless @reader_alive
|
|
95
|
+
|
|
96
|
+
if @wait_thr
|
|
97
|
+
return false unless @wait_thr.alive?
|
|
98
|
+
return false if @stdin&.closed? || @stdout&.closed?
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
true
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# True when a previous spawn left live plumbing that stop must retract
|
|
105
|
+
# before a new spawn (the `@wait_thr&.alive? || @reader_alive` half of
|
|
106
|
+
# Client#start's stop-if condition).
|
|
107
|
+
def stale?
|
|
108
|
+
@wait_thr&.alive? || @reader_alive
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# True when any teardown-able resource exists (the stop guard).
|
|
112
|
+
def resources?
|
|
113
|
+
@wait_thr || @reader_thr
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# PID of the spawned server process, nil when nothing was spawned.
|
|
117
|
+
def server_pid
|
|
118
|
+
@wait_thr&.pid
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Clear correlation state before a (re)start. Also rewinds the request
|
|
122
|
+
# id counter, mirroring the pre-extraction start() behavior.
|
|
123
|
+
def reset_correlation
|
|
124
|
+
@req_id = 0
|
|
125
|
+
@mutex.synchronize do
|
|
126
|
+
@responses.clear
|
|
127
|
+
@pending_requests.clear
|
|
128
|
+
@reader_failure = nil
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Full teardown: retract every resource in LIFO order — stderr drain,
|
|
133
|
+
# reader thread, pipes, then the server process tree. A failing
|
|
134
|
+
# inverse must never abort the unwind (half-unwound transport state is
|
|
135
|
+
# worse than a failed close).
|
|
136
|
+
def shutdown
|
|
137
|
+
@running = false
|
|
138
|
+
@crashed = false
|
|
139
|
+
@reader_alive = false
|
|
140
|
+
# Drain first: its thread may be blocked in gets on @stderr.
|
|
141
|
+
@stderr_drain&.stop
|
|
142
|
+
@stderr_drain = nil
|
|
143
|
+
begin
|
|
144
|
+
@reader_thr&.kill
|
|
145
|
+
rescue StandardError
|
|
146
|
+
nil
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
[@stdin, @stdout, @stderr].each do |io|
|
|
150
|
+
io.close
|
|
151
|
+
rescue StandardError
|
|
152
|
+
nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
begin
|
|
156
|
+
@reader_thr&.join(0.5)
|
|
157
|
+
@reader_thr = nil
|
|
158
|
+
rescue StandardError
|
|
159
|
+
nil
|
|
160
|
+
end
|
|
161
|
+
return unless @wait_thr&.alive?
|
|
162
|
+
|
|
163
|
+
Rubik::Platform.kill_tree(@wait_thr.pid)
|
|
164
|
+
begin
|
|
165
|
+
@wait_thr.join
|
|
166
|
+
rescue StandardError
|
|
167
|
+
nil
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def send_request(method, params, timeout: 5)
|
|
172
|
+
req_id = @mutex.synchronize do
|
|
173
|
+
@req_id += 1
|
|
174
|
+
@pending_requests[@req_id] = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
175
|
+
@req_id
|
|
176
|
+
end
|
|
177
|
+
msg = {
|
|
178
|
+
jsonrpc: "2.0",
|
|
179
|
+
id: req_id,
|
|
180
|
+
method: method,
|
|
181
|
+
params: params
|
|
182
|
+
}
|
|
183
|
+
unless write_message(msg)
|
|
184
|
+
@mutex.synchronize do
|
|
185
|
+
@pending_requests.delete(req_id)
|
|
186
|
+
@responses.delete(req_id)
|
|
187
|
+
end
|
|
188
|
+
raise RequestError.new(:transport_failure, @reader_failure || "LSP request write failed")
|
|
189
|
+
end
|
|
190
|
+
wait_for_response(req_id, timeout: timeout)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def wait_for_response(req_id, timeout: 5)
|
|
194
|
+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
195
|
+
timed_out = false
|
|
196
|
+
reader_failure = nil
|
|
197
|
+
|
|
198
|
+
@mutex.synchronize do
|
|
199
|
+
loop do
|
|
200
|
+
if @responses.key?(req_id)
|
|
201
|
+
response = @responses.delete(req_id)
|
|
202
|
+
@pending_requests.delete(req_id)
|
|
203
|
+
if response.key?("error")
|
|
204
|
+
error = response["error"] || {}
|
|
205
|
+
raise RequestError.new(:server_error, error["message"] || "LSP server returned an error")
|
|
206
|
+
end
|
|
207
|
+
return response["result"] if response.key?("result")
|
|
208
|
+
|
|
209
|
+
raise RequestError.new(:invalid_response, "LSP response did not contain a result")
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
if @reader_failure
|
|
213
|
+
reader_failure = @reader_failure
|
|
214
|
+
@responses.delete(req_id)
|
|
215
|
+
@pending_requests.delete(req_id)
|
|
216
|
+
break
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
remaining = timeout - (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time)
|
|
220
|
+
if remaining <= 0
|
|
221
|
+
@responses.delete(req_id)
|
|
222
|
+
@pending_requests.delete(req_id)
|
|
223
|
+
timed_out = true
|
|
224
|
+
break
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
@cv.wait(@mutex, remaining)
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
if timed_out
|
|
232
|
+
# W9: send $/cancelRequest notification (safely ignored if unsupported)
|
|
233
|
+
begin
|
|
234
|
+
send_notification("$/cancelRequest", { id: req_id })
|
|
235
|
+
rescue StandardError
|
|
236
|
+
nil
|
|
237
|
+
end
|
|
238
|
+
raise RequestError.new(:timeout, "LSP request timed out after #{timeout} seconds")
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
raise RequestError.new(:reader_failure, reader_failure) if reader_failure
|
|
242
|
+
|
|
243
|
+
nil
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def send_notification(method, params)
|
|
247
|
+
msg = {
|
|
248
|
+
jsonrpc: "2.0",
|
|
249
|
+
method: method,
|
|
250
|
+
params: params
|
|
251
|
+
}
|
|
252
|
+
write_message(msg)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def write_message(msg_hash)
|
|
256
|
+
json = JSON.dump(msg_hash)
|
|
257
|
+
header = "Content-Length: #{json.bytesize}\r\n\r\n"
|
|
258
|
+
bytes = header + json
|
|
259
|
+
@write_mutex.synchronize do
|
|
260
|
+
offset = 0
|
|
261
|
+
while offset < bytes.bytesize
|
|
262
|
+
written = @stdin.write(bytes.byteslice(offset, bytes.bytesize - offset))
|
|
263
|
+
raise IOError, "LSP transport made no write progress" unless written&.positive?
|
|
264
|
+
|
|
265
|
+
offset += written
|
|
266
|
+
end
|
|
267
|
+
@stdin.flush
|
|
268
|
+
end
|
|
269
|
+
true
|
|
270
|
+
rescue IOError, SystemCallError, NoMethodError => e
|
|
271
|
+
message = "LSP transport write failed: #{e.class}: #{e.message}"
|
|
272
|
+
@mutex.synchronize do
|
|
273
|
+
@reader_failure ||= message
|
|
274
|
+
@running = false
|
|
275
|
+
@crashed = true if @wait_thr
|
|
276
|
+
@cv.broadcast
|
|
277
|
+
end
|
|
278
|
+
false
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Correlate an incoming response with the pending-request registry.
|
|
282
|
+
# Server-request ids and late responses are discarded here exactly as
|
|
283
|
+
# before the extraction (issue #206).
|
|
284
|
+
def correlate_response(msg)
|
|
285
|
+
req_id = msg["id"]
|
|
286
|
+
@mutex.synchronize do
|
|
287
|
+
# Discard late response if request is neither pending nor already tracked
|
|
288
|
+
if @pending_requests.key?(req_id)
|
|
289
|
+
@responses[req_id] = msg
|
|
290
|
+
@cv.broadcast
|
|
291
|
+
end
|
|
292
|
+
# Prune stale responses if cap reached
|
|
293
|
+
if @responses.size > 100
|
|
294
|
+
oldest_keys = @responses.keys.take(@responses.size - 100)
|
|
295
|
+
oldest_keys.each { |k| @responses.delete(k) }
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def sanitized_spawn_env
|
|
301
|
+
Rubik::Platform.sanitized_spawn_env
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Starts (or restarts) the framing reader thread. Public because the
|
|
305
|
+
# spawn phase and the test seam both drive it directly.
|
|
306
|
+
def start_reader_thread
|
|
307
|
+
@reader_alive = true
|
|
308
|
+
@reader_thr = Thread.new do
|
|
309
|
+
while (@running || @reader_alive) && @stdout && !@stdout.closed?
|
|
310
|
+
begin
|
|
311
|
+
content_length = nil
|
|
312
|
+
loop do
|
|
313
|
+
header_line = @stdout.gets
|
|
314
|
+
break if header_line.nil?
|
|
315
|
+
|
|
316
|
+
stripped = header_line.strip
|
|
317
|
+
break if stripped.empty?
|
|
318
|
+
|
|
319
|
+
content_length = stripped.split(":", 2).last.strip.to_i if stripped.downcase.start_with?("content-length:")
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
break if content_length.nil?
|
|
323
|
+
|
|
324
|
+
body = @stdout.read(content_length)
|
|
325
|
+
break unless body
|
|
326
|
+
|
|
327
|
+
begin
|
|
328
|
+
parsed = JSON.parse(body)
|
|
329
|
+
dispatch(parsed)
|
|
330
|
+
rescue StandardError => e
|
|
331
|
+
Rubik::Log.warn(:lsp, "Failed to handle message: #{e.class}: #{e.message}")
|
|
332
|
+
end
|
|
333
|
+
rescue IOError, Errno::EPIPE, Errno::ECONNRESET
|
|
334
|
+
break
|
|
335
|
+
rescue StandardError => e
|
|
336
|
+
Rubik::Log.warn(:lsp, "Reader loop error: #{e.class}: #{e.message}")
|
|
337
|
+
next
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
ensure
|
|
341
|
+
@mutex.synchronize do
|
|
342
|
+
@crashed = true if @running
|
|
343
|
+
@reader_alive = false
|
|
344
|
+
@running = false
|
|
345
|
+
@reader_failure ||= "LSP response reader stopped before completing the request"
|
|
346
|
+
@cv.broadcast
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
private
|
|
352
|
+
|
|
353
|
+
# Responses are correlated inside the transport; every other message
|
|
354
|
+
# (server requests, notifications such as publishDiagnostics) is
|
|
355
|
+
# protocol and goes to the Client via the on_message callback.
|
|
356
|
+
def dispatch(msg)
|
|
357
|
+
if msg.key?("id") && !msg.key?("method")
|
|
358
|
+
correlate_response(msg)
|
|
359
|
+
else
|
|
360
|
+
@on_message.call(msg)
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
end
|
|
365
|
+
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
|
|
3
5
|
module Rubik
|
|
4
6
|
module MCP
|
|
5
7
|
# Boot state-machine vocabulary for Rubik::MCP::Manager (included there).
|
|
@@ -15,8 +17,10 @@ module Rubik
|
|
|
15
17
|
module BootLifecycle
|
|
16
18
|
# Raised by transition_server! on an edge outside TRANSITIONS. The
|
|
17
19
|
# table is derived from the actual write sites, so this firing means a
|
|
18
|
-
# new writer skipped the guarded seam (fail-loud tripwire).
|
|
19
|
-
|
|
20
|
+
# new writer skipped the guarded seam (fail-loud tripwire). Unified
|
|
21
|
+
# taxonomy (issue #448): subclasses Rubik::StateError so a caller can
|
|
22
|
+
# rescue any guarded-transition tripwire with one class.
|
|
23
|
+
class StateError < Rubik::StateError; end
|
|
20
24
|
|
|
21
25
|
STATES = [:disabled, :connecting, :active, :failed].freeze
|
|
22
26
|
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rubik
|
|
4
|
+
module MCP
|
|
5
|
+
# One boot run (issue #453): the mutable state of a single
|
|
6
|
+
# perform_load_from_config invocation — entries / connected / outcomes /
|
|
7
|
+
# boot_epoch — plus the five named phases that consume it, moved
|
|
8
|
+
# verbatim from Manager's former parameter-threaded phase methods. The
|
|
9
|
+
# outcomes ledger is an explicit atom map (index -> :applied | :failed;
|
|
10
|
+
# absence = skipped or never ran — exactly what owes_stop? reads).
|
|
11
|
+
#
|
|
12
|
+
# Friend-class seam: Manager keeps its guarded internals private, so
|
|
13
|
+
# BootRun reaches them through send (the public seams — add_server,
|
|
14
|
+
# connect_server, apply_connected_server, transition_server!, the
|
|
15
|
+
# BootLifecycle claim, and the client() reader — are called directly).
|
|
16
|
+
# Every monitor-hold boundary and epoch gate below is the former inline
|
|
17
|
+
# code relocated, not restructured: the atomic-rollback doctrine lives
|
|
18
|
+
# in run's ensure choreography plus BootLifecycle's epoch machinery.
|
|
19
|
+
class BootRun
|
|
20
|
+
attr_reader :entries, :connected, :outcomes, :boot_epoch
|
|
21
|
+
|
|
22
|
+
def initialize(manager, servers_cfg, mcp_enabled)
|
|
23
|
+
@manager = manager
|
|
24
|
+
@servers_cfg = servers_cfg
|
|
25
|
+
@mcp_enabled = mcp_enabled
|
|
26
|
+
# Boot epoch (review round): :connecting marks carry a per-invocation
|
|
27
|
+
# identity so a wedged older boot can never downgrade a newer mark.
|
|
28
|
+
@boot_epoch = Object.new
|
|
29
|
+
@entries = []
|
|
30
|
+
@connected = []
|
|
31
|
+
@outcomes = {}
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Choreography: register first (phase 0 fails per server without
|
|
35
|
+
# orphaning anything), then ONE ensure spans both post-registration
|
|
36
|
+
# phases — phase 1 deliberately did no context wiring, so stopping
|
|
37
|
+
# the client is the ONLY cleanup an unapplied prepared slot owes (no
|
|
38
|
+
# orphaned child processes, even when an interrupt lands mid-boot).
|
|
39
|
+
# The begin/ensure boundary is verbatim from the former body: a
|
|
40
|
+
# register! failure never triggers the sweeps, exactly as before.
|
|
41
|
+
def run
|
|
42
|
+
register!
|
|
43
|
+
begin
|
|
44
|
+
connect!
|
|
45
|
+
apply!
|
|
46
|
+
ensure
|
|
47
|
+
rollback_unapplied!
|
|
48
|
+
fail_orphans!
|
|
49
|
+
end
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
# Phase 0 (sequential, config order): registration still goes
|
|
56
|
+
# through add_server — subclasses and plugins wrap that seam, and a
|
|
57
|
+
# broken definition must fail exactly as before (logged, skipped,
|
|
58
|
+
# not half-registered). auto_start is deliberately false here: the
|
|
59
|
+
# connect half moved to the fan-out. Ends each auto-start candidate
|
|
60
|
+
# with the phase-0 claim (D5).
|
|
61
|
+
def register!
|
|
62
|
+
@servers_cfg.each do |sname, sdefn|
|
|
63
|
+
next unless sdefn.is_a?(Hash)
|
|
64
|
+
|
|
65
|
+
auto_start = @mcp_enabled && (sdefn.fetch("enabled", true) != false)
|
|
66
|
+
begin
|
|
67
|
+
@manager.add_server(sname, sdefn, auto_start: false, persist: nil)
|
|
68
|
+
rescue StandardError => e
|
|
69
|
+
# ENSURE-LOG (G3 round-1): one broken server config used to
|
|
70
|
+
# vanish behind an ENV["RUBIK_DEBUG"] gate; a misconfigured
|
|
71
|
+
# server must surface without debug mode.
|
|
72
|
+
Rubik::Log.warn(:mcp, "failed to load MCP server '#{sname}' (#{e.class}: #{e.message})")
|
|
73
|
+
@manager.send(:emit_mcp_event, :mcp_server_failed, :err, sname.to_s, error: e.message)
|
|
74
|
+
next
|
|
75
|
+
end
|
|
76
|
+
next unless auto_start
|
|
77
|
+
|
|
78
|
+
name_str = sname.to_s
|
|
79
|
+
# Phase-0 claim (D5): check+mark in ONE monitor hold — a user
|
|
80
|
+
# enable/disable/remove that races the boot reads a definite
|
|
81
|
+
# state, and phase 2's gate below can never miss a window.
|
|
82
|
+
@entries << [name_str, sdefn.transform_keys(&:to_s)] unless @manager.claim_boot_slot!(name_str, @boot_epoch)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Phase 1 (fan-out): pure I/O connects. Slots are captured by SIDE
|
|
87
|
+
# EFFECT inside the item blocks: when an Exception-level error (a
|
|
88
|
+
# boot interrupt) kills .map before it returns, the assignment below
|
|
89
|
+
# never happens — a value-only `connected` would leave the ensure
|
|
90
|
+
# blind to every successfully-started client. Hence the pre-built
|
|
91
|
+
# array owned here rather than a returned value.
|
|
92
|
+
def connect!
|
|
93
|
+
@connected = ::Array.new(@entries.size)
|
|
94
|
+
return if @entries.empty?
|
|
95
|
+
|
|
96
|
+
Rubik::Concurrency.map(@entries.each_with_index.to_a) do |(name_str, parsed_defn), index|
|
|
97
|
+
@connected[index] = { ok: true, **@manager.connect_server(name_str, parsed_defn) }
|
|
98
|
+
rescue StandardError => e
|
|
99
|
+
@connected[index] = { ok: false, error: e }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Phase 2 (sequential, config order): apply state; identical
|
|
104
|
+
# per-server warning on either phase's failure.
|
|
105
|
+
def apply!
|
|
106
|
+
@entries.each_with_index do |(name_str, parsed_defn), index|
|
|
107
|
+
slot = @connected[index]
|
|
108
|
+
next unless slot # never connected (interrupt during phase 1)
|
|
109
|
+
|
|
110
|
+
if slot[:ok]
|
|
111
|
+
begin
|
|
112
|
+
outcome = @manager.send(:boot_apply_gate, name_str, parsed_defn, @boot_epoch,
|
|
113
|
+
client: slot[:client], tools: slot[:tools])
|
|
114
|
+
@outcomes[index] = :applied if outcome == :applied
|
|
115
|
+
if outcome == :applied
|
|
116
|
+
@manager.send(:emit_mcp_event, :mcp_server_loaded, :info, name_str,
|
|
117
|
+
tools: @manager.send(:tool_count, name_str))
|
|
118
|
+
end
|
|
119
|
+
rescue StandardError => e
|
|
120
|
+
# ENSURE-LOG (G3 round-1): one broken server config used to
|
|
121
|
+
# vanish behind an ENV["RUBIK_DEBUG"] gate; a misconfigured
|
|
122
|
+
# server must surface without debug mode. apply's own
|
|
123
|
+
# rollback already set :failed and stopped its client via
|
|
124
|
+
# the context drain; :failed marks the slot so the ensure
|
|
125
|
+
# never double-stops it.
|
|
126
|
+
@outcomes[index] = :failed
|
|
127
|
+
Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{e.class}: #{e.message})")
|
|
128
|
+
@manager.send(:emit_mcp_event, :mcp_server_failed, :err, name_str, error: e.message)
|
|
129
|
+
end
|
|
130
|
+
else
|
|
131
|
+
# Only our own epoch's mark may flip to :failed — a user
|
|
132
|
+
# disable/remove during the handshake, or a newer boot's fresh
|
|
133
|
+
# mark, must win over this boot's connect failure (D5). The
|
|
134
|
+
# epoch gate and flip happen in the same monitor hold inside
|
|
135
|
+
# transition_server!.
|
|
136
|
+
@manager.transition_server!(name_str, :failed, epoch: @boot_epoch)
|
|
137
|
+
error = slot[:error]
|
|
138
|
+
Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{error.class}: #{error.message})")
|
|
139
|
+
@manager.send(:emit_mcp_event, :mcp_server_failed, :err, name_str, error: error.message)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Ensure part 1: stop every prepared client that did not end up
|
|
145
|
+
# registered: a failed apply already rolled its own client back via
|
|
146
|
+
# the context drain, while a never-applied one (interrupt between
|
|
147
|
+
# phases) owes a stop here. A :skipped slot also owes a stop —
|
|
148
|
+
# skipping the APPLY must not orphan its prepared child process (that
|
|
149
|
+
# is the exact leak D10 closed for handshakes). owes_stop? is the
|
|
150
|
+
# structural form of that rule: skipped and never-ran slots owe.
|
|
151
|
+
def rollback_unapplied!
|
|
152
|
+
@entries.each_with_index do |(name_str, _defn), index|
|
|
153
|
+
slot = @connected[index]
|
|
154
|
+
next if !slot.is_a?(Hash) || !slot[:client] || !owes_stop?(index)
|
|
155
|
+
|
|
156
|
+
registered = @manager.client(name_str)
|
|
157
|
+
next if registered.equal?(slot[:client])
|
|
158
|
+
|
|
159
|
+
begin
|
|
160
|
+
slot[:client].stop
|
|
161
|
+
rescue StandardError => e
|
|
162
|
+
Rubik::Log.warn(:mcp, "failed to stop unapplied MCP server '#{name_str}' (#{e.class}: #{e.message})")
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# The named form of the outcomes-ledger absence rule ("absence = owes
|
|
168
|
+
# stop"): an index missing from the ledger means the slot was skipped
|
|
169
|
+
# or never ran, and both owe a stop in the ensure. Delegates the
|
|
170
|
+
# per-outcome rule to BootLifecycle.owes_stop? so the stop-debt rule
|
|
171
|
+
# keeps one home; the ledger only ever records :applied / :failed, so
|
|
172
|
+
# absence is exactly the owes case — behavior-identical to the former
|
|
173
|
+
# inline BootLifecycle.owes_stop?(outcomes[index]) read.
|
|
174
|
+
def owes_stop?(index)
|
|
175
|
+
BootLifecycle.owes_stop?(@outcomes[index])
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Ensure part 2 — terminal state for boot entries that never reached
|
|
179
|
+
# apply (D6): anything still :connecting failed to boot; user-chosen
|
|
180
|
+
# values (:disabled, :active, deleted-by-remove) are left untouched.
|
|
181
|
+
# The epoch check inside transition_server! keeps an older wedged
|
|
182
|
+
# boot's sweep from downgrading a newer boot's fresh mark.
|
|
183
|
+
def fail_orphans!
|
|
184
|
+
@entries.each do |(name_str, _defn)|
|
|
185
|
+
@manager.transition_server!(name_str, :failed, epoch: @boot_epoch)
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|