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.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +250 -43
  4. data/README.md +5 -1
  5. data/lib/rubik/adapter/base.rb +21 -1
  6. data/lib/rubik/adapter/generic.rb +0 -14
  7. data/lib/rubik/adapter/node.rb +2 -0
  8. data/lib/rubik/adapter/process.rb +28 -67
  9. data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
  10. data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
  11. data/lib/rubik/buffer/apply_contract.rb +3 -3
  12. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  13. data/lib/rubik/buffer/manager.rb +26 -120
  14. data/lib/rubik/cli/workspace_trust.rb +16 -0
  15. data/lib/rubik/config/deep_ops.rb +4 -1
  16. data/lib/rubik/config/defaults.rb +29 -3
  17. data/lib/rubik/config/schema.rb +8 -0
  18. data/lib/rubik/config/secret_policy.rb +197 -0
  19. data/lib/rubik/config/store.rb +14 -0
  20. data/lib/rubik/config.rb +11 -0
  21. data/lib/rubik/diagnostics/collector.rb +0 -9
  22. data/lib/rubik/errors.rb +15 -0
  23. data/lib/rubik/event_bus.rb +2 -1
  24. data/lib/rubik/kernel/dispatcher.rb +16 -11
  25. data/lib/rubik/kernel/unit.rb +4 -1
  26. data/lib/rubik/llm/registry.rb +12 -9
  27. data/lib/rubik/llm/router.rb +2 -5
  28. data/lib/rubik/log.rb +23 -1
  29. data/lib/rubik/lsp/client.rb +152 -445
  30. data/lib/rubik/lsp/document_store.rb +209 -0
  31. data/lib/rubik/lsp/manager.rb +14 -10
  32. data/lib/rubik/lsp/transport.rb +365 -0
  33. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  34. data/lib/rubik/mcp/boot_run.rb +190 -0
  35. data/lib/rubik/mcp/client.rb +4 -3
  36. data/lib/rubik/mcp/manager.rb +46 -165
  37. data/lib/rubik/mcp/transport/stdio.rb +55 -10
  38. data/lib/rubik/orchestrator/auto_apply.rb +4 -3
  39. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  40. data/lib/rubik/orchestrator/goal_session.rb +519 -0
  41. data/lib/rubik/orchestrator/loop.rb +28 -47
  42. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  43. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  44. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  45. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  46. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  47. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  48. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  49. data/lib/rubik/orchestrator/subagent/instance.rb +11 -12
  50. data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
  51. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  52. data/lib/rubik/orchestrator/subagent/questioner.rb +24 -7
  53. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  54. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  55. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  56. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  57. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  58. data/lib/rubik/orchestrator/subagent/step_runner.rb +11 -3
  59. data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
  60. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  61. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  62. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  63. data/lib/rubik/orchestrator.rb +1 -0
  64. data/lib/rubik/platform/io_drain.rb +2 -1
  65. data/lib/rubik/platform/process.rb +144 -54
  66. data/lib/rubik/platform.rb +17 -1
  67. data/lib/rubik/plugin/manager.rb +2 -1
  68. data/lib/rubik/plugin/storage.rb +20 -29
  69. data/lib/rubik/repl/commands/agent_context.rb +9 -8
  70. data/lib/rubik/repl/commands/agent_execution.rb +20 -502
  71. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  72. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  73. data/lib/rubik/repl/commands/base.rb +47 -2
  74. data/lib/rubik/repl/commands/buffer.rb +6 -5
  75. data/lib/rubik/repl/commands/lsp.rb +3 -2
  76. data/lib/rubik/repl/commands/pruner.rb +2 -1
  77. data/lib/rubik/repl/commands/secrets.rb +151 -0
  78. data/lib/rubik/repl/commands/workspace.rb +60 -150
  79. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  80. data/lib/rubik/repl/config_wiring.rb +3 -3
  81. data/lib/rubik/repl/hooks.rb +2 -1
  82. data/lib/rubik/repl/input_execution.rb +158 -76
  83. data/lib/rubik/repl/input_loop.rb +3 -2
  84. data/lib/rubik/repl/lifecycle.rb +82 -204
  85. data/lib/rubik/repl/notice_router.rb +319 -0
  86. data/lib/rubik/repl/paste_support.rb +3 -1
  87. data/lib/rubik/repl/presentation.rb +5 -3
  88. data/lib/rubik/repl/pruner/gate.rb +3 -1
  89. data/lib/rubik/repl/pruner/harness.rb +2 -1
  90. data/lib/rubik/repl/shell.rb +4 -3
  91. data/lib/rubik/repl/turn_recorder.rb +6 -5
  92. data/lib/rubik/runtime/discovery.rb +3 -2
  93. data/lib/rubik/runtime/loop_service.rb +4 -3
  94. data/lib/rubik/runtime/manifest_store.rb +124 -0
  95. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  96. data/lib/rubik/runtime/overlay_storage.rb +269 -272
  97. data/lib/rubik/runtime/task_manager.rb +71 -27
  98. data/lib/rubik/runtime.rb +1 -0
  99. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  100. data/lib/rubik/sandbox/runner.rb +16 -15
  101. data/lib/rubik/security/allowlist.rb +167 -0
  102. data/lib/rubik/security/audit_log.rb +72 -0
  103. data/lib/rubik/security/classifier/classifier.rb +8 -0
  104. data/lib/rubik/security/command_gate.rb +591 -0
  105. data/lib/rubik/security/guard.rb +94 -1077
  106. data/lib/rubik/security/path_policy.rb +117 -0
  107. data/lib/rubik/security/secret_store.rb +463 -0
  108. data/lib/rubik/security/synthesis_gate.rb +297 -0
  109. data/lib/rubik/security.rb +5 -0
  110. data/lib/rubik/session/manager.rb +7 -1
  111. data/lib/rubik/skills/manager.rb +2 -3
  112. data/lib/rubik/storage/ranking.rb +113 -0
  113. data/lib/rubik/storage/search.rb +49 -0
  114. data/lib/rubik/storage/store.rb +15 -112
  115. data/lib/rubik/storage/transcript_indexer.rb +2 -3
  116. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  117. data/lib/rubik/tools/base.rb +22 -0
  118. data/lib/rubik/tools/builtins/files.rb +3 -15
  119. data/lib/rubik/tools/builtins/grep.rb +3 -9
  120. data/lib/rubik/tools/builtins/lifecycle.rb +96 -92
  121. data/lib/rubik/tools/builtins/synthesis.rb +3 -0
  122. data/lib/rubik/tools/lifecycle_rollbacks.rb +100 -0
  123. data/lib/rubik/tools/native_search.rb +2 -1
  124. data/lib/rubik/tools/truncator.rb +73 -36
  125. data/lib/rubik/ui/banner.rb +4 -3
  126. data/lib/rubik/ui/spinner.rb +3 -1
  127. data/lib/rubik/ui/statusline.rb +6 -5
  128. data/lib/rubik/version.rb +1 -1
  129. data/lib/rubik.rb +1 -2
  130. metadata +23 -1
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module LSP
5
+ # Owns the four per-document state hashes of LSP::Client (issue #406):
6
+ #
7
+ # open_documents path -> open version
8
+ # document_contents path -> full text of the open document
9
+ # diagnostic_states path -> { state:, document_version:, published_version: }
10
+ # diagnostics path -> last published diagnostics array
11
+ #
12
+ # ## Lock discipline — the single home of the LSP lock-order invariant
13
+ #
14
+ # This class creates no locks of its own. The owning Client must hold its
15
+ # document mutex across EVERY call into this store — that is the only
16
+ # synchronization for open_documents / document_contents. The diagnostic
17
+ # hashes share the client's protocol mutex and its condition variable,
18
+ # injected here as diagnostic_mutex / diagnostics_cv: this store acquires
19
+ # the diagnostics mutex for every diagnostic read/write and broadcasts the
20
+ # condition variable under that same hold, so waiters can never miss a
21
+ # wakeup (Client#wait_for_diagnostics waits on the same pair).
22
+ #
23
+ # The resulting global acquisition order — and the only legal one — is:
24
+ #
25
+ # document_mutex -> write_mutex -> diagnostics mutex
26
+ #
27
+ # write_mutex sits between the two because transport writes happen inside
28
+ # document transitions and write_message's failure path takes the
29
+ # diagnostics mutex. No call site may re-derive this order: document state
30
+ # flows exclusively through this store.
31
+ class DocumentStore
32
+ # Per-path pre-transition state. A nil field means "path absent before
33
+ # the transition", so the restore deletes instead of writing.
34
+ Snapshot = Struct.new(:version, :content, :state, :diagnostics)
35
+
36
+ def initialize(diagnostic_mutex, diagnostics_cv)
37
+ @diagnostic_mutex = diagnostic_mutex
38
+ @diagnostics_cv = diagnostics_cv
39
+ @open_documents = {}
40
+ @document_contents = {}
41
+ @diagnostic_states = {}
42
+ @diagnostics = {}
43
+ @version_counter = 0
44
+ end
45
+
46
+ # Start-of-session reset (Client#start): drop every document and
47
+ # diagnostic trace and restart the version counter at zero.
48
+ def reset
49
+ @open_documents.clear
50
+ @document_contents.clear
51
+ @version_counter = 0
52
+ @diagnostic_mutex.synchronize do
53
+ @diagnostic_states.clear
54
+ @diagnostics.clear
55
+ end
56
+ end
57
+
58
+ def open?(path)
59
+ @open_documents.key?(path)
60
+ end
61
+
62
+ def content_for(path)
63
+ @document_contents[path]
64
+ end
65
+
66
+ def next_version
67
+ @version_counter += 1
68
+ end
69
+
70
+ # Forward mutation shared by didOpen (new document) and didChange (new
71
+ # version of an open document): publish the version/content pair and
72
+ # invalidate the diagnostic snapshot for the new version.
73
+ def stage_version(path, content, version)
74
+ @open_documents[path] = version
75
+ @document_contents[path] = content
76
+ @diagnostic_mutex.synchronize do
77
+ @diagnostic_states[path] = pending_diagnostics(version)
78
+ end
79
+ end
80
+
81
+ # Forward mutation of didClose: remove every trace of the document.
82
+ def evict(path)
83
+ @open_documents.delete(path)
84
+ @document_contents.delete(path)
85
+ @diagnostic_mutex.synchronize do
86
+ @diagnostic_states.delete(path)
87
+ @diagnostics.delete(path)
88
+ end
89
+ end
90
+
91
+ # The ONE transactional helper for document transitions (issue #406):
92
+ # capture the per-path snapshot, yield to the transport send, and on a
93
+ # falsy result restore the snapshot in place — failed sends never leave
94
+ # mutated document state behind. Returns the block's result. Must be
95
+ # called with the caller's document mutex held.
96
+ def commit(path)
97
+ before = snapshot(path)
98
+ sent = yield
99
+ restore(path, before) unless sent
100
+ sent
101
+ end
102
+
103
+ # Records a publishDiagnostics payload. Returns false when the payload
104
+ # is stale relative to the currently open version (the caller discards
105
+ # the message), true otherwise; waiters wake under the same
106
+ # diagnostics-mutex hold that installs the new state.
107
+ def record_diagnostics(path, published, version)
108
+ current = @open_documents[path]
109
+ return false if version.is_a?(Numeric) && current && version < current
110
+
111
+ @diagnostic_mutex.synchronize do
112
+ @diagnostics[path] = published
113
+ @diagnostic_states[path] = {
114
+ state: version.is_a?(Numeric) ? :fresh : :unversioned,
115
+ document_version: current,
116
+ published_version: version
117
+ }
118
+ @diagnostics_cv.broadcast
119
+ end
120
+ true
121
+ end
122
+
123
+ def all_diagnostics
124
+ @diagnostic_mutex.synchronize { @diagnostics.dup }
125
+ end
126
+
127
+ def diagnostic_snapshot(path)
128
+ @diagnostic_mutex.synchronize { build_snapshot(path) }
129
+ end
130
+
131
+ # Blocks while the document's diagnostic state is :pending, waking on
132
+ # each publish/broadcast, then returns the final snapshot.
133
+ def await_diagnostics(path, timeout)
134
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
135
+ @diagnostic_mutex.synchronize do
136
+ while @diagnostic_states[path]&.dig(:state) == :pending
137
+ remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
138
+ break if remaining <= 0
139
+
140
+ @diagnostics_cv.wait(@diagnostic_mutex, remaining)
141
+ end
142
+ build_snapshot(path)
143
+ end
144
+ end
145
+
146
+ def all_diagnostic_snapshots
147
+ @diagnostic_mutex.synchronize do
148
+ @diagnostic_states.each_with_object({}) do |(path, state), result|
149
+ result[path] = {
150
+ state: state[:state],
151
+ diagnostics: (@diagnostics[path] || []).dup,
152
+ document_version: state[:document_version],
153
+ published_version: state[:published_version]
154
+ }
155
+ end
156
+ end
157
+ end
158
+
159
+ private
160
+
161
+ # Reads the per-path state across all four hashes. open_documents /
162
+ # document_contents are read directly (the caller holds the document
163
+ # mutex); the diagnostic pair shares one diagnostics-mutex hold.
164
+ def snapshot(path)
165
+ state, published = @diagnostic_mutex.synchronize do
166
+ [@diagnostic_states[path], @diagnostics[path]]
167
+ end
168
+ Snapshot.new(@open_documents[path], @document_contents[path], state, published)
169
+ end
170
+
171
+ # Writes the captured pre-transition state back. nil fields mean the
172
+ # path was absent before, so the entry is deleted again.
173
+ def restore(path, before)
174
+ restore_entry(@open_documents, path, before.version)
175
+ restore_entry(@document_contents, path, before.content)
176
+ @diagnostic_mutex.synchronize do
177
+ restore_entry(@diagnostic_states, path, before.state)
178
+ restore_entry(@diagnostics, path, before.diagnostics)
179
+ end
180
+ end
181
+
182
+ def restore_entry(hash, path, value)
183
+ if value
184
+ hash[path] = value
185
+ else
186
+ hash.delete(path)
187
+ end
188
+ end
189
+
190
+ def build_snapshot(path)
191
+ state = @diagnostic_states[path]
192
+ {
193
+ state: state ? state[:state] : :unavailable,
194
+ diagnostics: (@diagnostics[path] || []).dup,
195
+ document_version: state && state[:document_version],
196
+ published_version: state && state[:published_version]
197
+ }
198
+ end
199
+
200
+ def pending_diagnostics(version)
201
+ {
202
+ state: :pending,
203
+ document_version: version,
204
+ published_version: nil
205
+ }
206
+ end
207
+ end
208
+ end
209
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "monitor"
4
4
  require_relative "client"
5
+ require_relative "../storage/layout"
5
6
  require_relative "../runtime/discovery"
6
7
  require_relative "../runtime/diagnostic_engine"
7
8
 
@@ -43,6 +44,9 @@ module Rubik
43
44
  CIRCUIT_STABILITY_SECONDS = 60
44
45
 
45
46
  attr_reader :config, :root_dir, :clients, :crash_stats
47
+ # :nodoc: test observability door — read-only view of the per-language
48
+ # disposable-registration tracking (issue #450). No production consumer.
49
+ attr_reader :registered_disposable_langs
46
50
  attr_accessor :runtime_manager, :context
47
51
 
48
52
  def initialize(config:, root_dir: Dir.pwd, runtime_manager: nil, context: nil)
@@ -60,11 +64,11 @@ module Rubik
60
64
  end
61
65
 
62
66
  def enabled?
63
- @config.fetch("lsp.enabled", false) == true
67
+ Rubik::Storage::Layout.config_get(@config, "lsp.enabled", false) == true
64
68
  end
65
69
 
66
70
  def auto_diagnostics?
67
- @config.fetch("lsp.auto_diagnostics", true) == true
71
+ Rubik::Storage::Layout.config_get(@config, "lsp.auto_diagnostics", true) == true
68
72
  end
69
73
 
70
74
  def detect_language(file_path)
@@ -141,7 +145,7 @@ module Rubik
141
145
  def start_client(lang)
142
146
  lang_str = lang.to_s.downcase
143
147
  @monitor.synchronize do
144
- configured_cmd = @config.fetch("lsp.servers.#{lang_str}") || Client::SERVERS[lang_str]
148
+ configured_cmd = Rubik::Storage::Layout.config_get(@config, "lsp.servers.#{lang_str}", nil) || Client::SERVERS[lang_str]
145
149
  client = @clients[lang_str] || Client.new(lang_str, root_dir: @root_dir, server_cmd: configured_cmd)
146
150
 
147
151
  # 1. Fallback probing if configured command binary is not available
@@ -165,7 +169,7 @@ module Rubik
165
169
  end
166
170
 
167
171
  if client.running
168
- { ok: true, message: "LSP server for '#{lang_str}' is already running (PID: #{client.instance_variable_get(:@wait_thr)&.pid})" }
172
+ { ok: true, message: "LSP server for '#{lang_str}' is already running (PID: #{client.server_pid})" }
169
173
  else
170
174
  res = client.start
171
175
  @clients[lang_str] = client
@@ -350,7 +354,7 @@ module Rubik
350
354
  client_statuses = {}
351
355
  @monitor.synchronize do
352
356
  Client::SERVERS.each do |lang, def_cmd|
353
- cmd = @config.fetch("lsp.servers.#{lang}") || def_cmd
357
+ cmd = Rubik::Storage::Layout.config_get(@config, "lsp.servers.#{lang}", nil) || def_cmd
354
358
  active_c = @clients[lang]
355
359
  avail = active_c ? active_c.available? : Runtime::Discovery.binary_available?(cmd)
356
360
  probed = avail ? cmd : Runtime::Discovery.probe_lsp(lang, root_dir: @root_dir)
@@ -408,7 +412,7 @@ module Rubik
408
412
  @recovery_errors[lang] = "LSP server restart for '#{lang}' is blocked by the circuit breaker"
409
413
  return nil
410
414
  end
411
- if recovering && !@config.fetch("lsp.auto_start", true)
415
+ if recovering && !Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true)
412
416
  @recovery_errors[lang] = "LSP server restart for '#{lang}' is disabled"
413
417
  return nil
414
418
  end
@@ -416,14 +420,14 @@ module Rubik
416
420
  return nil unless client || enabled?
417
421
 
418
422
  if client.nil?
419
- configured_cmd = @config.fetch("lsp.servers.#{lang}") || Client::SERVERS[lang]
423
+ configured_cmd = Rubik::Storage::Layout.config_get(@config, "lsp.servers.#{lang}", nil) || Client::SERVERS[lang]
420
424
  client = Client.new(lang, root_dir: @root_dir, server_cmd: configured_cmd)
421
425
  unless client.available?
422
426
  probed_cmd = Runtime::Discovery.probe_lsp(lang, root_dir: @root_dir, custom_cmd: configured_cmd)
423
427
  client = Client.new(lang, root_dir: @root_dir, server_cmd: probed_cmd) if probed_cmd
424
428
  end
425
429
 
426
- if client.available? && @config.fetch("lsp.auto_start", true) && !circuit_open?(lang)
430
+ if client.available? && Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true) && !circuit_open?(lang)
427
431
  if client.start
428
432
  note_healthy_start(lang)
429
433
  @recovery_errors.delete(lang)
@@ -437,14 +441,14 @@ module Rubik
437
441
  elsif recovering
438
442
  @recovery_errors[lang] = if circuit_open?(lang)
439
443
  "LSP server restart for '#{lang}' is blocked by the circuit breaker"
440
- elsif !@config.fetch("lsp.auto_start", true)
444
+ elsif !Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true)
441
445
  "LSP server restart for '#{lang}' is disabled"
442
446
  else
443
447
  "LSP server restart failed for '#{lang}': server unavailable"
444
448
  end
445
449
  end
446
450
  @clients[lang] = client
447
- elsif !client.running && @config.fetch("lsp.auto_start", true) &&
451
+ elsif !client.running && Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true) &&
448
452
  client.available? && !circuit_open?(lang)
449
453
  if client.start
450
454
  note_healthy_start(lang)
@@ -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
- class StateError < StandardError; end
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