rubik 0.9.0 → 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 +250 -43
- data/README.md +5 -1
- data/lib/rubik/adapter/base.rb +21 -1
- data/lib/rubik/adapter/generic.rb +0 -14
- data/lib/rubik/adapter/node.rb +2 -0
- data/lib/rubik/adapter/process.rb +28 -67
- data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
- 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/cli/workspace_trust.rb +16 -0
- data/lib/rubik/config/deep_ops.rb +4 -1
- data/lib/rubik/config/defaults.rb +29 -3
- data/lib/rubik/config/schema.rb +8 -0
- data/lib/rubik/config/secret_policy.rb +197 -0
- data/lib/rubik/config/store.rb +14 -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 +16 -11
- 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 +23 -1
- data/lib/rubik/lsp/client.rb +152 -445
- data/lib/rubik/lsp/document_store.rb +209 -0
- 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/client.rb +4 -3
- data/lib/rubik/mcp/manager.rb +46 -165
- data/lib/rubik/mcp/transport/stdio.rb +55 -10
- 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 +519 -0
- 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 +11 -12
- data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
- data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
- data/lib/rubik/orchestrator/subagent/questioner.rb +24 -7
- 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 +11 -3
- data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
- data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
- data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
- data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
- data/lib/rubik/orchestrator.rb +1 -0
- data/lib/rubik/platform/io_drain.rb +2 -1
- data/lib/rubik/platform/process.rb +144 -54
- data/lib/rubik/platform.rb +17 -1
- data/lib/rubik/plugin/manager.rb +2 -1
- data/lib/rubik/plugin/storage.rb +20 -29
- data/lib/rubik/repl/commands/agent_context.rb +9 -8
- data/lib/rubik/repl/commands/agent_execution.rb +20 -502
- 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 +6 -5
- data/lib/rubik/repl/commands/lsp.rb +3 -2
- 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 +3 -2
- data/lib/rubik/repl/lifecycle.rb +82 -204
- 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 +124 -0
- data/lib/rubik/runtime/overlay_guard.rb +127 -0
- data/lib/rubik/runtime/overlay_storage.rb +269 -272
- data/lib/rubik/runtime/task_manager.rb +71 -27
- data/lib/rubik/runtime.rb +1 -0
- data/lib/rubik/sandbox/driver/docker.rb +5 -1
- data/lib/rubik/sandbox/runner.rb +16 -15
- data/lib/rubik/security/allowlist.rb +167 -0
- data/lib/rubik/security/audit_log.rb +72 -0
- data/lib/rubik/security/classifier/classifier.rb +8 -0
- data/lib/rubik/security/command_gate.rb +591 -0
- data/lib/rubik/security/guard.rb +94 -1077
- data/lib/rubik/security/path_policy.rb +117 -0
- data/lib/rubik/security/secret_store.rb +463 -0
- data/lib/rubik/security/synthesis_gate.rb +297 -0
- data/lib/rubik/security.rb +5 -0
- data/lib/rubik/session/manager.rb +7 -1
- data/lib/rubik/skills/manager.rb +2 -3
- data/lib/rubik/storage/ranking.rb +113 -0
- data/lib/rubik/storage/search.rb +49 -0
- data/lib/rubik/storage/store.rb +15 -112
- data/lib/rubik/storage/transcript_indexer.rb +2 -3
- 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 +3 -9
- data/lib/rubik/tools/builtins/lifecycle.rb +96 -92
- data/lib/rubik/tools/builtins/synthesis.rb +3 -0
- data/lib/rubik/tools/lifecycle_rollbacks.rb +100 -0
- data/lib/rubik/tools/native_search.rb +2 -1
- data/lib/rubik/tools/truncator.rb +73 -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 +23 -1
data/lib/rubik/lsp/client.rb
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "open3"
|
|
4
3
|
require "json"
|
|
5
4
|
require "uri"
|
|
6
5
|
require_relative "server_command"
|
|
6
|
+
require_relative "transport"
|
|
7
|
+
require_relative "document_store"
|
|
7
8
|
require_relative "../platform"
|
|
8
9
|
require_relative "../log"
|
|
9
10
|
|
|
10
11
|
module Rubik
|
|
11
12
|
module LSP
|
|
12
13
|
class Client
|
|
13
|
-
class
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
super(message)
|
|
19
|
-
end
|
|
20
|
-
end
|
|
14
|
+
# Typed request failure. The class itself lives on the Transport (the
|
|
15
|
+
# correlation registry that raises it moved there, issue #470); the
|
|
16
|
+
# constant is aliased here so the public exception surface
|
|
17
|
+
# (Rubik::LSP::Client::RequestError) is unchanged.
|
|
18
|
+
RequestError = Transport::RequestError
|
|
21
19
|
|
|
22
20
|
SERVERS = {
|
|
23
21
|
"go" => "gopls",
|
|
@@ -35,8 +33,12 @@ module Rubik
|
|
|
35
33
|
"vue" => "vue-language-server --stdio"
|
|
36
34
|
}.freeze
|
|
37
35
|
|
|
38
|
-
attr_reader :language, :server_cmd, :root_dir, :
|
|
36
|
+
attr_reader :language, :server_cmd, :root_dir, :server_capabilities,
|
|
39
37
|
:sync_capabilities
|
|
38
|
+
# The JSON-RPC transport delegate (issue #470). Exposed read-only as a
|
|
39
|
+
# seam for focused Transport unit tests and white-box transport
|
|
40
|
+
# injection; the protocol API above remains the supported surface.
|
|
41
|
+
attr_reader :transport
|
|
40
42
|
|
|
41
43
|
def initialize(language, root_dir: Dir.pwd, server_cmd: nil)
|
|
42
44
|
@language = language.to_s.downcase
|
|
@@ -49,27 +51,20 @@ module Rubik
|
|
|
49
51
|
elsif File.basename(@server_argv.first.to_s) == "solargraph" && !@server_argv.include?("stdio")
|
|
50
52
|
@server_argv << "stdio"
|
|
51
53
|
end
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
@
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
@wait_thr = nil
|
|
59
|
-
@reader_thr = nil
|
|
60
|
-
@stderr_drain = nil
|
|
61
|
-
@mutex = Mutex.new
|
|
54
|
+
# The transport owns spawn/reader/correlation (issue #470); it is
|
|
55
|
+
# created first because the DocumentStore shares its mutex/condvar.
|
|
56
|
+
@transport = Transport.new(
|
|
57
|
+
root_dir: @root_dir,
|
|
58
|
+
on_message: ->(msg) { handle_incoming_message(msg) }
|
|
59
|
+
)
|
|
62
60
|
@document_mutex = Mutex.new
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
@
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
@
|
|
70
|
-
@document_contents = {}
|
|
71
|
-
@diagnostic_states = {}
|
|
72
|
-
@version_counter = 0
|
|
61
|
+
# The ONE acquisition rule for document state (see the invariant
|
|
62
|
+
# documented on Rubik::LSP::DocumentStore): hold @document_mutex
|
|
63
|
+
# across every @document_store call; the store takes the transport
|
|
64
|
+
# mutex for the diagnostic hashes. Transport writes (@write_mutex)
|
|
65
|
+
# may only happen inside those document_mutex regions, giving the
|
|
66
|
+
# single legal order document_mutex -> write_mutex -> transport mutex.
|
|
67
|
+
@document_store = DocumentStore.new(@transport.mutex, @transport.cv)
|
|
73
68
|
@server_capabilities = {}
|
|
74
69
|
@sync_capabilities = {
|
|
75
70
|
source: :not_advertised, open_close: false, change: 0,
|
|
@@ -91,23 +86,12 @@ module Rubik
|
|
|
91
86
|
def start
|
|
92
87
|
return false unless available?
|
|
93
88
|
|
|
94
|
-
stop if alive? || @
|
|
89
|
+
stop if alive? || @transport.stale?
|
|
95
90
|
|
|
96
91
|
@document_mutex.synchronize do
|
|
97
|
-
@
|
|
98
|
-
@open_documents.clear
|
|
99
|
-
@document_contents.clear
|
|
100
|
-
@version_counter = 0
|
|
101
|
-
@mutex.synchronize do
|
|
102
|
-
@diagnostics.clear
|
|
103
|
-
@diagnostic_states.clear
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
@mutex.synchronize do
|
|
107
|
-
@responses.clear
|
|
108
|
-
@pending_requests.clear
|
|
109
|
-
@reader_failure = nil
|
|
92
|
+
@document_store.reset
|
|
110
93
|
end
|
|
94
|
+
@transport.reset_correlation
|
|
111
95
|
|
|
112
96
|
cmd_parts = @server_argv.dup
|
|
113
97
|
bin = cmd_parts.first
|
|
@@ -116,22 +100,7 @@ module Rubik
|
|
|
116
100
|
|
|
117
101
|
cmd_parts[0] = resolved_bin if resolved_bin
|
|
118
102
|
|
|
119
|
-
@
|
|
120
|
-
# Binary mode: LSP framing is byte-exact. Text-mode pipes on Windows
|
|
121
|
-
# translate CRLF→LF on read, so Content-Length counts and read(len)
|
|
122
|
-
# disagree and the JSON body arrives truncated (the reader's old
|
|
123
|
-
# silent rescue-then-break hid this — no real server was ever
|
|
124
|
-
# spawned in tests).
|
|
125
|
-
@stdin.binmode
|
|
126
|
-
@stdout.binmode
|
|
127
|
-
@running = false
|
|
128
|
-
@crashed = false
|
|
129
|
-
@reader_alive = false
|
|
130
|
-
|
|
131
|
-
start_reader_thread
|
|
132
|
-
# An unread stderr pipe fills its OS buffer (~64KB) and blocks the
|
|
133
|
-
# server itself mid-write; drain continuously, discard content.
|
|
134
|
-
@stderr_drain = Rubik::Platform::IoDrain.start(@stderr)
|
|
103
|
+
@transport.spawn(cmd_parts)
|
|
135
104
|
|
|
136
105
|
root_uri = Rubik::Platform.to_file_uri(@root_dir)
|
|
137
106
|
init_res = send_request("initialize", {
|
|
@@ -171,117 +140,66 @@ module Rubik
|
|
|
171
140
|
stop
|
|
172
141
|
return false
|
|
173
142
|
end
|
|
174
|
-
@
|
|
143
|
+
@transport.mark_running
|
|
175
144
|
init_res
|
|
176
|
-
rescue StandardError
|
|
145
|
+
rescue StandardError => e
|
|
146
|
+
# The handshake failure cause (bad server binary, protocol mismatch,
|
|
147
|
+
# JSON parse error) is the only diagnostic an operator gets — never
|
|
148
|
+
# discard it silently (log.rb doctrine: Log, not raw warn, in lib/).
|
|
149
|
+
Rubik::Log.error(:lsp, "handshake failed: #{e.class}: #{e.message}")
|
|
177
150
|
stop
|
|
178
151
|
false
|
|
179
152
|
end
|
|
180
153
|
|
|
181
154
|
def alive?
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if @wait_thr
|
|
186
|
-
return false unless @wait_thr.alive?
|
|
187
|
-
return false if @stdin&.closed? || @stdout&.closed?
|
|
188
|
-
end
|
|
155
|
+
@transport.alive?
|
|
156
|
+
end
|
|
189
157
|
|
|
190
|
-
|
|
158
|
+
def running
|
|
159
|
+
@transport.running
|
|
191
160
|
end
|
|
192
161
|
|
|
193
162
|
def crashed?
|
|
194
|
-
|
|
163
|
+
@transport.crashed?
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# PID of the spawned language server process (nil when not running).
|
|
167
|
+
def server_pid
|
|
168
|
+
@transport.server_pid
|
|
195
169
|
end
|
|
196
170
|
|
|
197
171
|
def reconnect!
|
|
198
|
-
stop if
|
|
172
|
+
stop if running
|
|
199
173
|
start
|
|
200
174
|
end
|
|
201
175
|
|
|
202
176
|
def stop
|
|
203
|
-
return unless
|
|
177
|
+
return unless running || @transport.resources?
|
|
204
178
|
|
|
205
179
|
begin
|
|
206
|
-
send_request("shutdown", {}, timeout: 2) if
|
|
207
|
-
send_notification("exit", {}) if
|
|
180
|
+
send_request("shutdown", {}, timeout: 2) if running
|
|
181
|
+
send_notification("exit", {}) if running
|
|
208
182
|
rescue StandardError
|
|
209
183
|
# ignore
|
|
210
184
|
end
|
|
211
185
|
|
|
212
|
-
@
|
|
213
|
-
@crashed = false
|
|
214
|
-
@reader_alive = false
|
|
215
|
-
# Drain first: its thread may be blocked in gets on @stderr.
|
|
216
|
-
@stderr_drain&.stop
|
|
217
|
-
@stderr_drain = nil
|
|
218
|
-
begin
|
|
219
|
-
@reader_thr&.kill
|
|
220
|
-
rescue StandardError
|
|
221
|
-
nil
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
[@stdin, @stdout, @stderr].each do |io|
|
|
225
|
-
io.close
|
|
226
|
-
rescue StandardError
|
|
227
|
-
nil
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
begin
|
|
231
|
-
@reader_thr&.join(0.5)
|
|
232
|
-
@reader_thr = nil
|
|
233
|
-
rescue StandardError
|
|
234
|
-
nil
|
|
235
|
-
end
|
|
236
|
-
return unless @wait_thr&.alive?
|
|
237
|
-
|
|
238
|
-
Rubik::Platform.kill_tree(@wait_thr.pid)
|
|
239
|
-
begin
|
|
240
|
-
@wait_thr.join
|
|
241
|
-
rescue
|
|
242
|
-
nil
|
|
243
|
-
end
|
|
186
|
+
@transport.shutdown
|
|
244
187
|
end
|
|
245
188
|
alias dispose stop
|
|
246
189
|
|
|
247
190
|
def definition(file_path, line, character)
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
res = send_request("textDocument/definition", {
|
|
251
|
-
textDocument: { uri: document_uri(file_path) },
|
|
252
|
-
position: { line: line, character: character }
|
|
253
|
-
})
|
|
254
|
-
|
|
255
|
-
{ ok: true, result: res }
|
|
256
|
-
rescue StandardError => e
|
|
257
|
-
{ ok: false, error: e.message }
|
|
191
|
+
position_request("textDocument/definition", file_path, line, character)
|
|
258
192
|
end
|
|
259
193
|
|
|
260
194
|
def references(file_path, line, character)
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
position: { line: line, character: character },
|
|
266
|
-
context: { includeDeclaration: true }
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
{ ok: true, result: res }
|
|
270
|
-
rescue StandardError => e
|
|
271
|
-
{ ok: false, error: e.message }
|
|
195
|
+
position_request(
|
|
196
|
+
"textDocument/references", file_path, line, character,
|
|
197
|
+
context: { includeDeclaration: true }
|
|
198
|
+
)
|
|
272
199
|
end
|
|
273
200
|
|
|
274
201
|
def hover(file_path, line, character)
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
res = send_request("textDocument/hover", {
|
|
278
|
-
textDocument: { uri: document_uri(file_path) },
|
|
279
|
-
position: { line: line, character: character }
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
{ ok: true, result: res }
|
|
283
|
-
rescue StandardError => e
|
|
284
|
-
{ ok: false, error: e.message }
|
|
202
|
+
position_request("textDocument/hover", file_path, line, character)
|
|
285
203
|
end
|
|
286
204
|
|
|
287
205
|
def did_open(file_path, content, language_id = @language)
|
|
@@ -289,38 +207,27 @@ module Rubik
|
|
|
289
207
|
|
|
290
208
|
abs_path = normalized_document_path(file_path)
|
|
291
209
|
@document_mutex.synchronize do
|
|
292
|
-
if @
|
|
293
|
-
return true if @
|
|
210
|
+
if @document_store.open?(abs_path)
|
|
211
|
+
return true if @document_store.content_for(abs_path) == content
|
|
294
212
|
|
|
295
213
|
return send_document_change_unlocked(abs_path, content)
|
|
296
214
|
end
|
|
297
215
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
216
|
+
@document_store.commit(abs_path) do
|
|
217
|
+
version = @document_store.next_version
|
|
218
|
+
@document_store.stage_version(abs_path, content, version)
|
|
219
|
+
send_notification("textDocument/didOpen", {
|
|
220
|
+
textDocument: {
|
|
221
|
+
uri: document_uri(abs_path),
|
|
222
|
+
languageId: language_id.to_s,
|
|
223
|
+
version: version,
|
|
224
|
+
text: content
|
|
225
|
+
}
|
|
226
|
+
})
|
|
305
227
|
end
|
|
306
|
-
sent = send_notification("textDocument/didOpen", {
|
|
307
|
-
textDocument: {
|
|
308
|
-
uri: document_uri(abs_path),
|
|
309
|
-
languageId: language_id.to_s,
|
|
310
|
-
version: version,
|
|
311
|
-
text: content
|
|
312
|
-
}
|
|
313
|
-
})
|
|
314
|
-
unless sent
|
|
315
|
-
@open_documents.delete(abs_path)
|
|
316
|
-
@document_contents.delete(abs_path)
|
|
317
|
-
@mutex.synchronize do
|
|
318
|
-
old_state ? @diagnostic_states[abs_path] = old_state : @diagnostic_states.delete(abs_path)
|
|
319
|
-
end
|
|
320
|
-
end
|
|
321
|
-
sent
|
|
322
228
|
end
|
|
323
|
-
rescue StandardError
|
|
229
|
+
rescue StandardError => e
|
|
230
|
+
Rubik::Log.warn(:lsp, "did_open failed for #{file_path}: #{e.class}: #{e.message}")
|
|
324
231
|
false
|
|
325
232
|
end
|
|
326
233
|
|
|
@@ -329,38 +236,27 @@ module Rubik
|
|
|
329
236
|
|
|
330
237
|
abs_path = normalized_document_path(file_path)
|
|
331
238
|
@document_mutex.synchronize { send_document_change_unlocked(abs_path, content) }
|
|
332
|
-
rescue StandardError
|
|
239
|
+
rescue StandardError => e
|
|
240
|
+
Rubik::Log.warn(:lsp, "did_change failed for #{file_path}: #{e.class}: #{e.message}")
|
|
333
241
|
false
|
|
334
242
|
end
|
|
335
243
|
|
|
336
244
|
def did_close(file_path)
|
|
337
|
-
return false unless
|
|
245
|
+
return false unless running && @sync_capabilities[:open_close]
|
|
338
246
|
|
|
339
247
|
abs_path = normalized_document_path(file_path)
|
|
340
248
|
@document_mutex.synchronize do
|
|
341
|
-
|
|
342
|
-
return false unless version
|
|
249
|
+
return false unless @document_store.open?(abs_path)
|
|
343
250
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
251
|
+
@document_store.commit(abs_path) do
|
|
252
|
+
@document_store.evict(abs_path)
|
|
253
|
+
send_notification("textDocument/didClose", {
|
|
254
|
+
textDocument: { uri: document_uri(abs_path) }
|
|
255
|
+
})
|
|
347
256
|
end
|
|
348
|
-
@open_documents.delete(abs_path)
|
|
349
|
-
@document_contents.delete(abs_path)
|
|
350
|
-
sent = send_notification("textDocument/didClose", {
|
|
351
|
-
textDocument: { uri: document_uri(abs_path) }
|
|
352
|
-
})
|
|
353
|
-
unless sent
|
|
354
|
-
@open_documents[abs_path] = version
|
|
355
|
-
@document_contents[abs_path] = content
|
|
356
|
-
@mutex.synchronize do
|
|
357
|
-
@diagnostic_states[abs_path] = state if state
|
|
358
|
-
@diagnostics[abs_path] = diagnostics if diagnostics
|
|
359
|
-
end
|
|
360
|
-
end
|
|
361
|
-
sent
|
|
362
257
|
end
|
|
363
|
-
rescue StandardError
|
|
258
|
+
rescue StandardError => e
|
|
259
|
+
Rubik::Log.warn(:lsp, "did_close failed for #{file_path}: #{e.class}: #{e.message}")
|
|
364
260
|
false
|
|
365
261
|
end
|
|
366
262
|
|
|
@@ -372,7 +268,8 @@ module Rubik
|
|
|
372
268
|
params = { textDocument: { uri: document_uri(abs_path) } }
|
|
373
269
|
params[:text] = content if @sync_capabilities[:include_text] && !content.nil?
|
|
374
270
|
send_notification("textDocument/didSave", params)
|
|
375
|
-
rescue StandardError
|
|
271
|
+
rescue StandardError => e
|
|
272
|
+
Rubik::Log.warn(:lsp, "did_save failed for #{file_path}: #{e.class}: #{e.message}")
|
|
376
273
|
false
|
|
377
274
|
end
|
|
378
275
|
|
|
@@ -380,7 +277,7 @@ module Rubik
|
|
|
380
277
|
return false if file_path.nil? || file_path.to_s.strip.empty?
|
|
381
278
|
|
|
382
279
|
abs_path = normalized_document_path(file_path)
|
|
383
|
-
@document_mutex.synchronize { @
|
|
280
|
+
@document_mutex.synchronize { @document_store.open?(abs_path) }
|
|
384
281
|
rescue ArgumentError
|
|
385
282
|
false
|
|
386
283
|
end
|
|
@@ -390,111 +287,67 @@ module Rubik
|
|
|
390
287
|
end
|
|
391
288
|
|
|
392
289
|
def all_diagnostics
|
|
393
|
-
@
|
|
394
|
-
|
|
395
|
-
|
|
290
|
+
@document_store.all_diagnostics
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def diagnostics
|
|
294
|
+
@document_store.all_diagnostics
|
|
396
295
|
end
|
|
397
296
|
|
|
398
297
|
def diagnostics_snapshot_for(file_path)
|
|
399
298
|
abs_path = normalized_document_path(file_path)
|
|
400
|
-
@
|
|
299
|
+
@document_store.diagnostic_snapshot(abs_path)
|
|
401
300
|
rescue ArgumentError, TypeError
|
|
402
301
|
{ state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
|
|
403
302
|
end
|
|
404
303
|
|
|
405
304
|
def wait_for_diagnostics(file_path, timeout: 0.2)
|
|
406
305
|
abs_path = normalized_document_path(file_path)
|
|
407
|
-
|
|
408
|
-
@mutex.synchronize do
|
|
409
|
-
while @diagnostic_states[abs_path]&.dig(:state) == :pending
|
|
410
|
-
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
411
|
-
break if remaining <= 0
|
|
412
|
-
|
|
413
|
-
@cv.wait(@mutex, remaining)
|
|
414
|
-
end
|
|
415
|
-
diagnostics_snapshot_unlocked(abs_path)
|
|
416
|
-
end
|
|
306
|
+
@document_store.await_diagnostics(abs_path, timeout)
|
|
417
307
|
rescue ArgumentError, TypeError
|
|
418
308
|
{ state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
|
|
419
309
|
end
|
|
420
310
|
|
|
421
311
|
def all_diagnostic_snapshots
|
|
422
|
-
@
|
|
423
|
-
@diagnostic_states.each_with_object({}) do |(path, state), result|
|
|
424
|
-
result[path] = {
|
|
425
|
-
state: state[:state],
|
|
426
|
-
diagnostics: (@diagnostics[path] || []).dup,
|
|
427
|
-
document_version: state[:document_version],
|
|
428
|
-
published_version: state[:published_version]
|
|
429
|
-
}
|
|
430
|
-
end
|
|
431
|
-
end
|
|
312
|
+
@document_store.all_diagnostic_snapshots
|
|
432
313
|
end
|
|
433
314
|
|
|
434
315
|
private
|
|
435
316
|
|
|
317
|
+
# ---- Delegates to the transport (issue #470). These thin shims keep
|
|
318
|
+
# ---- the Client's internal call sites and per-instance test stubs
|
|
319
|
+
# ---- working unchanged; the implementations live on Transport.
|
|
320
|
+
def send_request(method, params, timeout: 5)
|
|
321
|
+
@transport.send_request(method, params, timeout: timeout)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def wait_for_response(req_id, timeout: 5)
|
|
325
|
+
@transport.wait_for_response(req_id, timeout: timeout)
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def send_notification(method, params)
|
|
329
|
+
@transport.send_notification(method, params)
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def write_message(msg_hash)
|
|
333
|
+
@transport.write_message(msg_hash)
|
|
334
|
+
end
|
|
335
|
+
|
|
436
336
|
def start_reader_thread
|
|
437
|
-
@
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
loop do
|
|
443
|
-
header_line = @stdout.gets
|
|
444
|
-
break if header_line.nil?
|
|
445
|
-
|
|
446
|
-
stripped = header_line.strip
|
|
447
|
-
break if stripped.empty?
|
|
448
|
-
|
|
449
|
-
content_length = stripped.split(":", 2).last.strip.to_i if stripped.downcase.start_with?("content-length:")
|
|
450
|
-
end
|
|
451
|
-
|
|
452
|
-
break if content_length.nil?
|
|
453
|
-
|
|
454
|
-
body = @stdout.read(content_length)
|
|
455
|
-
break unless body
|
|
456
|
-
|
|
457
|
-
begin
|
|
458
|
-
parsed = JSON.parse(body)
|
|
459
|
-
handle_incoming_message(parsed)
|
|
460
|
-
rescue StandardError => e
|
|
461
|
-
Rubik::Log.warn(:lsp, "Failed to handle message: #{e.class}: #{e.message}")
|
|
462
|
-
end
|
|
463
|
-
rescue IOError, Errno::EPIPE, Errno::ECONNRESET
|
|
464
|
-
break
|
|
465
|
-
rescue StandardError => e
|
|
466
|
-
Rubik::Log.warn(:lsp, "Reader loop error: #{e.class}: #{e.message}")
|
|
467
|
-
next
|
|
468
|
-
end
|
|
469
|
-
end
|
|
470
|
-
ensure
|
|
471
|
-
@mutex.synchronize do
|
|
472
|
-
@crashed = true if @running
|
|
473
|
-
@reader_alive = false
|
|
474
|
-
@running = false
|
|
475
|
-
@reader_failure ||= "LSP response reader stopped before completing the request"
|
|
476
|
-
@cv.broadcast
|
|
477
|
-
end
|
|
478
|
-
end
|
|
337
|
+
@transport.start_reader_thread
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def sanitized_spawn_env
|
|
341
|
+
@transport.sanitized_spawn_env
|
|
479
342
|
end
|
|
480
343
|
|
|
344
|
+
# ---- Incoming message routing (protocol concern, stays on Client).
|
|
345
|
+
|
|
481
346
|
def handle_incoming_message(msg)
|
|
482
347
|
if msg.key?("method") && msg.key?("id")
|
|
483
348
|
handle_server_request(msg)
|
|
484
349
|
elsif msg.key?("id")
|
|
485
|
-
|
|
486
|
-
@mutex.synchronize do
|
|
487
|
-
# Discard late response if request is neither pending nor already tracked
|
|
488
|
-
if @pending_requests.key?(req_id)
|
|
489
|
-
@responses[req_id] = msg
|
|
490
|
-
@cv.broadcast
|
|
491
|
-
end
|
|
492
|
-
# Prune stale responses if cap reached
|
|
493
|
-
if @responses.size > 100
|
|
494
|
-
oldest_keys = @responses.keys.take(@responses.size - 100)
|
|
495
|
-
oldest_keys.each { |k| @responses.delete(k) }
|
|
496
|
-
end
|
|
497
|
-
end
|
|
350
|
+
@transport.correlate_response(msg)
|
|
498
351
|
elsif msg["method"] == "textDocument/publishDiagnostics"
|
|
499
352
|
params = msg["params"] || {}
|
|
500
353
|
uri = params["uri"].to_s
|
|
@@ -504,95 +357,9 @@ module Rubik
|
|
|
504
357
|
published_version = params["version"]
|
|
505
358
|
|
|
506
359
|
@document_mutex.synchronize do
|
|
507
|
-
|
|
508
|
-
next if published_version.is_a?(Numeric) && current_version && published_version < current_version
|
|
509
|
-
|
|
510
|
-
@mutex.synchronize do
|
|
511
|
-
@diagnostics[norm_path] = diags
|
|
512
|
-
@diagnostic_states[norm_path] = {
|
|
513
|
-
state: published_version.is_a?(Numeric) ? :fresh : :unversioned,
|
|
514
|
-
document_version: current_version,
|
|
515
|
-
published_version: published_version
|
|
516
|
-
}
|
|
517
|
-
@cv.broadcast
|
|
518
|
-
end
|
|
519
|
-
end
|
|
520
|
-
end
|
|
521
|
-
end
|
|
522
|
-
|
|
523
|
-
def send_request(method, params, timeout: 5)
|
|
524
|
-
req_id = @mutex.synchronize do
|
|
525
|
-
@req_id += 1
|
|
526
|
-
@pending_requests[@req_id] = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
527
|
-
@req_id
|
|
528
|
-
end
|
|
529
|
-
msg = {
|
|
530
|
-
jsonrpc: "2.0",
|
|
531
|
-
id: req_id,
|
|
532
|
-
method: method,
|
|
533
|
-
params: params
|
|
534
|
-
}
|
|
535
|
-
unless write_message(msg)
|
|
536
|
-
@mutex.synchronize do
|
|
537
|
-
@pending_requests.delete(req_id)
|
|
538
|
-
@responses.delete(req_id)
|
|
360
|
+
@document_store.record_diagnostics(norm_path, diags, published_version)
|
|
539
361
|
end
|
|
540
|
-
raise RequestError.new(:transport_failure, @reader_failure || "LSP request write failed")
|
|
541
362
|
end
|
|
542
|
-
wait_for_response(req_id, timeout: timeout)
|
|
543
|
-
end
|
|
544
|
-
|
|
545
|
-
def wait_for_response(req_id, timeout: 5)
|
|
546
|
-
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
547
|
-
timed_out = false
|
|
548
|
-
reader_failure = nil
|
|
549
|
-
|
|
550
|
-
@mutex.synchronize do
|
|
551
|
-
loop do
|
|
552
|
-
if @responses.key?(req_id)
|
|
553
|
-
response = @responses.delete(req_id)
|
|
554
|
-
@pending_requests.delete(req_id)
|
|
555
|
-
if response.key?("error")
|
|
556
|
-
error = response["error"] || {}
|
|
557
|
-
raise RequestError.new(:server_error, error["message"] || "LSP server returned an error")
|
|
558
|
-
end
|
|
559
|
-
return response["result"] if response.key?("result")
|
|
560
|
-
|
|
561
|
-
raise RequestError.new(:invalid_response, "LSP response did not contain a result")
|
|
562
|
-
end
|
|
563
|
-
|
|
564
|
-
if @reader_failure
|
|
565
|
-
reader_failure = @reader_failure
|
|
566
|
-
@responses.delete(req_id)
|
|
567
|
-
@pending_requests.delete(req_id)
|
|
568
|
-
break
|
|
569
|
-
end
|
|
570
|
-
|
|
571
|
-
remaining = timeout - (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time)
|
|
572
|
-
if remaining <= 0
|
|
573
|
-
@responses.delete(req_id)
|
|
574
|
-
@pending_requests.delete(req_id)
|
|
575
|
-
timed_out = true
|
|
576
|
-
break
|
|
577
|
-
end
|
|
578
|
-
|
|
579
|
-
@cv.wait(@mutex, remaining)
|
|
580
|
-
end
|
|
581
|
-
end
|
|
582
|
-
|
|
583
|
-
if timed_out
|
|
584
|
-
# W9: send $/cancelRequest notification (safely ignored if unsupported)
|
|
585
|
-
begin
|
|
586
|
-
send_notification("$/cancelRequest", { id: req_id })
|
|
587
|
-
rescue StandardError
|
|
588
|
-
nil
|
|
589
|
-
end
|
|
590
|
-
raise RequestError.new(:timeout, "LSP request timed out after #{timeout} seconds")
|
|
591
|
-
end
|
|
592
|
-
|
|
593
|
-
raise RequestError.new(:reader_failure, reader_failure) if reader_failure
|
|
594
|
-
|
|
595
|
-
nil
|
|
596
363
|
end
|
|
597
364
|
|
|
598
365
|
def normalized_document_path(file_path)
|
|
@@ -626,8 +393,22 @@ module Rubik
|
|
|
626
393
|
[prefix, *segments].compact.reject(&:empty?).join("/").downcase
|
|
627
394
|
end
|
|
628
395
|
|
|
629
|
-
|
|
630
|
-
|
|
396
|
+
# The collapsed positional query (issue #406): definition, references
|
|
397
|
+
# and hover differ only in the LSP method string (references adds the
|
|
398
|
+
# includeDeclaration context). Adding typeDefinition/implementation is
|
|
399
|
+
# a one-line public wrapper around this helper.
|
|
400
|
+
def position_request(method, file_path, line, character, extra_params = {})
|
|
401
|
+
return { ok: false, error: "LSP server not running" } unless alive?
|
|
402
|
+
|
|
403
|
+
params = {
|
|
404
|
+
textDocument: { uri: document_uri(file_path) },
|
|
405
|
+
position: { line: line, character: character }
|
|
406
|
+
}.merge(extra_params)
|
|
407
|
+
res = send_request(method, params)
|
|
408
|
+
|
|
409
|
+
{ ok: true, result: res }
|
|
410
|
+
rescue StandardError => e
|
|
411
|
+
{ ok: false, error: e.message }
|
|
631
412
|
end
|
|
632
413
|
|
|
633
414
|
def resolved_binary(bin)
|
|
@@ -641,55 +422,24 @@ module Rubik
|
|
|
641
422
|
Rubik::Platform.which(bin)
|
|
642
423
|
end
|
|
643
424
|
|
|
644
|
-
def pending_diagnostics(version)
|
|
645
|
-
{
|
|
646
|
-
state: :pending,
|
|
647
|
-
document_version: version,
|
|
648
|
-
published_version: nil
|
|
649
|
-
}
|
|
650
|
-
end
|
|
651
|
-
|
|
652
|
-
def diagnostics_snapshot_unlocked(abs_path)
|
|
653
|
-
state = @diagnostic_states[abs_path]
|
|
654
|
-
{
|
|
655
|
-
state: state ? state[:state] : :unavailable,
|
|
656
|
-
diagnostics: (@diagnostics[abs_path] || []).dup,
|
|
657
|
-
document_version: state && state[:document_version],
|
|
658
|
-
published_version: state && state[:published_version]
|
|
659
|
-
}
|
|
660
|
-
end
|
|
661
|
-
|
|
662
425
|
def send_document_change_unlocked(abs_path, content)
|
|
663
426
|
return false unless @sync_capabilities[:change].to_i.positive?
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
version: version
|
|
680
|
-
},
|
|
681
|
-
# A text-only event is a full-document replacement,
|
|
682
|
-
# which is valid for both Full and Incremental sync.
|
|
683
|
-
contentChanges: [{ text: content }]
|
|
684
|
-
})
|
|
685
|
-
unless sent
|
|
686
|
-
@open_documents[abs_path] = previous_version
|
|
687
|
-
@document_contents[abs_path] = previous_content
|
|
688
|
-
@mutex.synchronize do
|
|
689
|
-
previous_state ? @diagnostic_states[abs_path] = previous_state : @diagnostic_states.delete(abs_path)
|
|
690
|
-
end
|
|
427
|
+
return false unless @document_store.open?(abs_path)
|
|
428
|
+
return true if @document_store.content_for(abs_path) == content
|
|
429
|
+
|
|
430
|
+
@document_store.commit(abs_path) do
|
|
431
|
+
version = @document_store.next_version
|
|
432
|
+
@document_store.stage_version(abs_path, content, version)
|
|
433
|
+
send_notification("textDocument/didChange", {
|
|
434
|
+
textDocument: {
|
|
435
|
+
uri: document_uri(abs_path),
|
|
436
|
+
version: version
|
|
437
|
+
},
|
|
438
|
+
# A text-only event is a full-document replacement,
|
|
439
|
+
# which is valid for both Full and Incremental sync.
|
|
440
|
+
contentChanges: [{ text: content }]
|
|
441
|
+
})
|
|
691
442
|
end
|
|
692
|
-
sent
|
|
693
443
|
end
|
|
694
444
|
|
|
695
445
|
def normalize_sync_capabilities(value)
|
|
@@ -750,49 +500,6 @@ module Rubik
|
|
|
750
500
|
})
|
|
751
501
|
end
|
|
752
502
|
end
|
|
753
|
-
|
|
754
|
-
def send_notification(method, params)
|
|
755
|
-
msg = {
|
|
756
|
-
jsonrpc: "2.0",
|
|
757
|
-
method: method,
|
|
758
|
-
params: params
|
|
759
|
-
}
|
|
760
|
-
write_message(msg)
|
|
761
|
-
end
|
|
762
|
-
|
|
763
|
-
def write_message(msg_hash)
|
|
764
|
-
json = JSON.dump(msg_hash)
|
|
765
|
-
header = "Content-Length: #{json.bytesize}\r\n\r\n"
|
|
766
|
-
bytes = header + json
|
|
767
|
-
@write_mutex.synchronize do
|
|
768
|
-
offset = 0
|
|
769
|
-
while offset < bytes.bytesize
|
|
770
|
-
written = @stdin.write(bytes.byteslice(offset, bytes.bytesize - offset))
|
|
771
|
-
raise IOError, "LSP transport made no write progress" unless written&.positive?
|
|
772
|
-
|
|
773
|
-
offset += written
|
|
774
|
-
end
|
|
775
|
-
@stdin.flush
|
|
776
|
-
end
|
|
777
|
-
true
|
|
778
|
-
rescue IOError, SystemCallError, NoMethodError => e
|
|
779
|
-
message = "LSP transport write failed: #{e.class}: #{e.message}"
|
|
780
|
-
@mutex.synchronize do
|
|
781
|
-
@reader_failure ||= message
|
|
782
|
-
@running = false
|
|
783
|
-
@crashed = true if @wait_thr
|
|
784
|
-
@cv.broadcast
|
|
785
|
-
end
|
|
786
|
-
false
|
|
787
|
-
end
|
|
788
|
-
|
|
789
|
-
def sanitized_spawn_env
|
|
790
|
-
env = {}
|
|
791
|
-
ENV.each_key do |k|
|
|
792
|
-
env[k] = nil if k.start_with?("BUNDLE_", "BUNDLER_") || ["RUBYOPT", "RUBYLIB"].include?(k)
|
|
793
|
-
end
|
|
794
|
-
env
|
|
795
|
-
end
|
|
796
503
|
end
|
|
797
504
|
end
|
|
798
505
|
end
|