rubik 0.9.1 → 0.11.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 (138) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +203 -1
  4. data/lib/rubik/adapter/base.rb +2 -1
  5. data/lib/rubik/adapter/process.rb +28 -67
  6. data/lib/rubik/ast/extractor.rb +2 -1
  7. data/lib/rubik/ast/treesitter_extractor.rb +2 -1
  8. data/lib/rubik/benchmark/vakanuvis_adoption.rb +12 -4
  9. data/lib/rubik/buffer/apply_contract.rb +3 -3
  10. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  11. data/lib/rubik/buffer/manager.rb +26 -120
  12. data/lib/rubik/config/deep_ops.rb +4 -1
  13. data/lib/rubik/config/defaults.rb +6 -3
  14. data/lib/rubik/config/schema.rb +8 -0
  15. data/lib/rubik/config/secret_policy.rb +198 -0
  16. data/lib/rubik/config/store.rb +2 -1
  17. data/lib/rubik/config.rb +13 -2
  18. data/lib/rubik/context/ignore_rules.rb +193 -0
  19. data/lib/rubik/context/scanner.rb +46 -161
  20. data/lib/rubik/diagnostics/collector.rb +0 -9
  21. data/lib/rubik/errors.rb +15 -0
  22. data/lib/rubik/event_bus.rb +2 -1
  23. data/lib/rubik/kernel/dispatcher.rb +12 -7
  24. data/lib/rubik/kernel/unit.rb +4 -1
  25. data/lib/rubik/llm/client.rb +6 -200
  26. data/lib/rubik/llm/connection_pool.rb +104 -0
  27. data/lib/rubik/llm/registry.rb +13 -9
  28. data/lib/rubik/llm/router.rb +2 -5
  29. data/lib/rubik/llm/sse_parser.rb +110 -0
  30. data/lib/rubik/log.rb +21 -0
  31. data/lib/rubik/lsp/circuit_breaker.rb +98 -0
  32. data/lib/rubik/lsp/client.rb +77 -273
  33. data/lib/rubik/lsp/client_provisioner.rb +49 -0
  34. data/lib/rubik/lsp/manager.rb +67 -96
  35. data/lib/rubik/lsp/transport.rb +365 -0
  36. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  37. data/lib/rubik/mcp/boot_run.rb +190 -0
  38. data/lib/rubik/mcp/manager.rb +55 -166
  39. data/lib/rubik/mcp/transport/stdio.rb +4 -1
  40. data/lib/rubik/orchestrator/auto_apply.rb +8 -6
  41. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  42. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +12 -1
  43. data/lib/rubik/orchestrator/goal_ledger/schema_vocabulary.rb +38 -0
  44. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +6 -1
  45. data/lib/rubik/orchestrator/goal_ledger.rb +1 -0
  46. data/lib/rubik/orchestrator/goal_session.rb +53 -39
  47. data/lib/rubik/orchestrator/loop.rb +28 -47
  48. data/lib/rubik/orchestrator/lsp_feedback.rb +1 -1
  49. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  50. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  51. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  52. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  53. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  54. data/lib/rubik/orchestrator/step_executor.rb +2 -1
  55. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  56. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  57. data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
  58. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  59. data/lib/rubik/orchestrator/subagent/notices.rb +2 -2
  60. data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
  61. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  62. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  63. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  64. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  65. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  66. data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
  67. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  68. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  69. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  70. data/lib/rubik/platform/io_drain.rb +5 -2
  71. data/lib/rubik/platform/process.rb +144 -54
  72. data/lib/rubik/platform.rb +2 -1
  73. data/lib/rubik/plugin/manager.rb +2 -1
  74. data/lib/rubik/plugin/storage.rb +8 -8
  75. data/lib/rubik/repl/commands/agent_context.rb +8 -7
  76. data/lib/rubik/repl/commands/agent_execution.rb +13 -32
  77. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  78. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  79. data/lib/rubik/repl/commands/base.rb +47 -2
  80. data/lib/rubik/repl/commands/buffer.rb +5 -4
  81. data/lib/rubik/repl/commands/lsp.rb +2 -1
  82. data/lib/rubik/repl/commands/pruner.rb +2 -1
  83. data/lib/rubik/repl/commands/secrets.rb +151 -0
  84. data/lib/rubik/repl/commands/workspace.rb +60 -150
  85. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  86. data/lib/rubik/repl/config_wiring.rb +8 -6
  87. data/lib/rubik/repl/hooks.rb +2 -1
  88. data/lib/rubik/repl/input_execution.rb +162 -79
  89. data/lib/rubik/repl/input_loop.rb +2 -1
  90. data/lib/rubik/repl/lifecycle.rb +60 -210
  91. data/lib/rubik/repl/notice_router.rb +319 -0
  92. data/lib/rubik/repl/paste_support.rb +3 -1
  93. data/lib/rubik/repl/presentation.rb +5 -3
  94. data/lib/rubik/repl/pruner/gate.rb +3 -1
  95. data/lib/rubik/repl/pruner/harness.rb +2 -1
  96. data/lib/rubik/repl/shell.rb +4 -3
  97. data/lib/rubik/repl/tee_io.rb +2 -1
  98. data/lib/rubik/repl/turn_recorder.rb +6 -5
  99. data/lib/rubik/runtime/ast_service.rb +2 -1
  100. data/lib/rubik/runtime/discovery.rb +3 -2
  101. data/lib/rubik/runtime/loop_service.rb +4 -3
  102. data/lib/rubik/runtime/manifest_store.rb +9 -1
  103. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  104. data/lib/rubik/runtime/overlay_storage.rb +109 -163
  105. data/lib/rubik/runtime/task_manager.rb +64 -25
  106. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  107. data/lib/rubik/sandbox/runner.rb +21 -19
  108. data/lib/rubik/security/audit_log.rb +2 -1
  109. data/lib/rubik/security/command_gate.rb +126 -243
  110. data/lib/rubik/security/guard.rb +7 -3
  111. data/lib/rubik/security/secret_store.rb +492 -0
  112. data/lib/rubik/security/segment_report.rb +161 -0
  113. data/lib/rubik/security/synthesis_gate.rb +8 -11
  114. data/lib/rubik/session/manager.rb +7 -1
  115. data/lib/rubik/session/recorder.rb +2 -1
  116. data/lib/rubik/skills/manager.rb +4 -4
  117. data/lib/rubik/storage/search.rb +49 -0
  118. data/lib/rubik/storage/store.rb +13 -20
  119. data/lib/rubik/storage/transcript_indexer.rb +3 -2
  120. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  121. data/lib/rubik/storage/vakanuvis.rb +2 -1
  122. data/lib/rubik/tools/base.rb +22 -0
  123. data/lib/rubik/tools/builtins/files.rb +3 -15
  124. data/lib/rubik/tools/builtins/goal_ledger.rb +12 -1
  125. data/lib/rubik/tools/builtins/grep.rb +1 -6
  126. data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
  127. data/lib/rubik/tools/builtins/synthesis.rb +3 -3
  128. data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
  129. data/lib/rubik/tools/preflight_runner.rb +2 -1
  130. data/lib/rubik/tools/truncator.rb +48 -36
  131. data/lib/rubik/tools/uri_resolver.rb +3 -2
  132. data/lib/rubik/ui/banner.rb +4 -3
  133. data/lib/rubik/ui/capability.rb +1 -0
  134. data/lib/rubik/ui/spinner.rb +3 -1
  135. data/lib/rubik/ui/statusline.rb +6 -5
  136. data/lib/rubik/version.rb +1 -1
  137. data/lib/rubik.rb +4 -5
  138. metadata +20 -1
@@ -2,12 +2,20 @@
2
2
 
3
3
  require "monitor"
4
4
  require_relative "client"
5
+ require_relative "circuit_breaker"
6
+ require_relative "client_provisioner"
7
+ require_relative "server_command"
8
+ require_relative "../storage/layout"
5
9
  require_relative "../runtime/discovery"
6
10
  require_relative "../runtime/diagnostic_engine"
7
11
 
8
12
  module Rubik
9
13
  module LSP
10
14
  class Manager
15
+ # LSP server ids. Deliberately diverges from
16
+ # Context::Scanner::EXTENSION_MAP (which maps to index language
17
+ # tags); the two tables serve different consumers — keep them
18
+ # separate.
11
19
  EXTENSION_MAP = {
12
20
  ".go" => "go",
13
21
  ".rs" => "rust",
@@ -38,11 +46,16 @@ module Rubik
38
46
  ".bash" => "shellscript"
39
47
  }.freeze
40
48
 
41
- MAX_CONSECUTIVE_CRASHES = 3
42
- CIRCUIT_COOLDOWN_SECONDS = 300
43
- CIRCUIT_STABILITY_SECONDS = 60
49
+ # Circuit-breaker policy lives in CircuitBreaker (issue #497); the
50
+ # constants are re-exposed here for API compatibility.
51
+ MAX_CONSECUTIVE_CRASHES = CircuitBreaker::MAX_CONSECUTIVE_CRASHES
52
+ CIRCUIT_COOLDOWN_SECONDS = CircuitBreaker::CIRCUIT_COOLDOWN_SECONDS
53
+ CIRCUIT_STABILITY_SECONDS = CircuitBreaker::CIRCUIT_STABILITY_SECONDS
44
54
 
45
- attr_reader :config, :root_dir, :clients, :crash_stats
55
+ attr_reader :config, :root_dir, :clients
56
+ # :nodoc: test observability door — read-only view of the per-language
57
+ # disposable-registration tracking (issue #450). No production consumer.
58
+ attr_reader :registered_disposable_langs
46
59
  attr_accessor :runtime_manager, :context
47
60
 
48
61
  def initialize(config:, root_dir: Dir.pwd, runtime_manager: nil, context: nil)
@@ -51,20 +64,25 @@ module Rubik
51
64
  @runtime_manager = runtime_manager
52
65
  @context = context || runtime_manager&.context
53
66
  @clients = {}
54
- @crash_stats = {}
55
- @healthy_since = {}
67
+ @breaker = CircuitBreaker.new(clock: -> { monotonic_now })
68
+ @provisioner = ClientProvisioner.new(config: @config, root_dir: @root_dir)
56
69
  @recovery_errors = {}
57
70
  @registered_disposable_langs = {}
58
71
  @client_inverses = {}
59
72
  @monitor = Monitor.new
60
73
  end
61
74
 
75
+ # Per-language crash history (live hash from the CircuitBreaker).
76
+ def crash_stats
77
+ @breaker.crash_stats
78
+ end
79
+
62
80
  def enabled?
63
- @config.fetch("lsp.enabled", false) == true
81
+ Rubik::Storage::Layout.config_get(@config, "lsp.enabled", false) == true
64
82
  end
65
83
 
66
84
  def auto_diagnostics?
67
- @config.fetch("lsp.auto_diagnostics", true) == true
85
+ Rubik::Storage::Layout.config_get(@config, "lsp.auto_diagnostics", true) == true
68
86
  end
69
87
 
70
88
  def detect_language(file_path)
@@ -77,46 +95,18 @@ module Rubik
77
95
  DOCUMENT_LANGUAGE_ID_MAP[ext] || detect_language(file_path) || "plaintext"
78
96
  end
79
97
 
98
+ # Circuit-breaker state machine (extracted to CircuitBreaker, #497);
99
+ # Manager keeps the public delegating API.
80
100
  def circuit_open?(lang)
81
- @monitor.synchronize do
82
- stat = @crash_stats[lang.to_s.downcase]
83
- healthy_since = @healthy_since[lang.to_s.downcase]
84
- if healthy_since && monotonic_now - healthy_since >= CIRCUIT_STABILITY_SECONDS
85
- @crash_stats.delete(lang.to_s.downcase)
86
- @healthy_since.delete(lang.to_s.downcase)
87
- return false
88
- end
89
-
90
- return false unless stat && stat[:circuit_open]
91
-
92
- if stat[:last_crash_at] && (monotonic_now - stat[:last_crash_at] > CIRCUIT_COOLDOWN_SECONDS)
93
- stat[:circuit_open] = false
94
- stat[:count] = 0
95
- false
96
- else
97
- true
98
- end
99
- end
101
+ @breaker.open?(lang)
100
102
  end
101
103
 
102
104
  def record_crash(lang)
103
- @monitor.synchronize do
104
- lang_str = lang.to_s.downcase
105
- healthy_since = @healthy_since.delete(lang_str)
106
- @crash_stats.delete(lang_str) if healthy_since && monotonic_now - healthy_since >= CIRCUIT_STABILITY_SECONDS
107
- stat = (@crash_stats[lang_str] ||= { count: 0, last_crash_at: nil, circuit_open: false })
108
- stat[:count] += 1
109
- stat[:last_crash_at] = monotonic_now
110
- stat[:circuit_open] = true if stat[:count] >= MAX_CONSECUTIVE_CRASHES
111
- end
105
+ @breaker.record_crash(lang)
112
106
  end
113
107
 
114
108
  def reset_circuit!(lang)
115
- @monitor.synchronize do
116
- lang_str = lang.to_s.downcase
117
- @crash_stats.delete(lang_str)
118
- @healthy_since[lang_str] = monotonic_now
119
- end
109
+ @breaker.reset!(lang)
120
110
  end
121
111
 
122
112
  def ensure_healthy!(language_or_client)
@@ -141,21 +131,10 @@ module Rubik
141
131
  def start_client(lang)
142
132
  lang_str = lang.to_s.downcase
143
133
  @monitor.synchronize do
144
- configured_cmd = @config.fetch("lsp.servers.#{lang_str}") || Client::SERVERS[lang_str]
145
- client = @clients[lang_str] || Client.new(lang_str, root_dir: @root_dir, server_cmd: configured_cmd)
146
-
147
- # 1. Fallback probing if configured command binary is not available
148
- unless client.available?
149
- probed_cmd = Runtime::Discovery.probe_lsp(lang_str, root_dir: @root_dir, custom_cmd: configured_cmd)
150
- client = Client.new(lang_str, root_dir: @root_dir, server_cmd: probed_cmd) if probed_cmd && probed_cmd != client.server_cmd
151
- end
134
+ outcome = @provisioner.provision(lang_str, existing: @clients[lang_str])
135
+ client = outcome[:client]
152
136
 
153
- unless client.available?
154
- failure = Runtime::DiagnosticEngine.diagnose(
155
- cmd: ServerCommand.display(client.server_cmd),
156
- exit_code: 127,
157
- stderr: "Server binary '#{ServerCommand.display(client.server_cmd)}' not found in PATH"
158
- )
137
+ if (failure = outcome[:diagnosis])
159
138
  return {
160
139
  ok: false,
161
140
  error: "Server binary '#{ServerCommand.display(client.server_cmd)}' not found",
@@ -165,7 +144,7 @@ module Rubik
165
144
  end
166
145
 
167
146
  if client.running
168
- { ok: true, message: "LSP server for '#{lang_str}' is already running (PID: #{client.instance_variable_get(:@wait_thr)&.pid})" }
147
+ { ok: true, message: "LSP server for '#{lang_str}' is already running (PID: #{client.server_pid})" }
169
148
  else
170
149
  res = client.start
171
150
  @clients[lang_str] = client
@@ -175,10 +154,7 @@ module Rubik
175
154
  register_client_unlocked(lang_str, client)
176
155
  { ok: true, message: "Started LSP server for '#{lang_str}' (#{ServerCommand.display(client.server_cmd)})" }
177
156
  else
178
- record_crash(lang_str)
179
- stop_client_safely(client)
180
- @clients.delete(lang_str)
181
- unregister_client_unlocked(lang_str)
157
+ restart_failed(lang_str, client, drop_stored: true)
182
158
  failure = Runtime::DiagnosticEngine.diagnose(
183
159
  cmd: ServerCommand.display(client.server_cmd),
184
160
  exit_code: 1,
@@ -350,7 +326,7 @@ module Rubik
350
326
  client_statuses = {}
351
327
  @monitor.synchronize do
352
328
  Client::SERVERS.each do |lang, def_cmd|
353
- cmd = @config.fetch("lsp.servers.#{lang}") || def_cmd
329
+ cmd = Rubik::Storage::Layout.config_get(@config, "lsp.servers.#{lang}", nil) || def_cmd
354
330
  active_c = @clients[lang]
355
331
  avail = active_c ? active_c.available? : Runtime::Discovery.binary_available?(cmd)
356
332
  probed = avail ? cmd : Runtime::Discovery.probe_lsp(lang, root_dir: @root_dir)
@@ -388,19 +364,14 @@ module Rubik
388
364
  # Caller MUST hold @monitor. Dead-client removal and replacement startup
389
365
  # happen in one critical section so concurrent requests share one client.
390
366
  def client_for_unlocked(lang, preferred = nil)
391
- circuit_open?(lang) # also retires crash history after sustained healthy operation
367
+ @breaker.open?(lang) # also retires crash history after sustained healthy operation
392
368
  stored = @clients[lang]
393
369
  client = stored || preferred
394
370
  recovering = false
395
371
 
396
372
  if client && client_dead?(client)
397
373
  recovering = true
398
- record_crash(lang)
399
- stop_client_safely(client)
400
- if stored.equal?(client)
401
- @clients.delete(lang)
402
- unregister_client_unlocked(lang)
403
- end
374
+ restart_failed(lang, client, drop_stored: true, unregister: stored.equal?(client))
404
375
  client = nil
405
376
  end
406
377
 
@@ -408,7 +379,7 @@ module Rubik
408
379
  @recovery_errors[lang] = "LSP server restart for '#{lang}' is blocked by the circuit breaker"
409
380
  return nil
410
381
  end
411
- if recovering && !@config.fetch("lsp.auto_start", true)
382
+ if recovering && !Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true)
412
383
  @recovery_errors[lang] = "LSP server restart for '#{lang}' is disabled"
413
384
  return nil
414
385
  end
@@ -416,51 +387,55 @@ module Rubik
416
387
  return nil unless client || enabled?
417
388
 
418
389
  if client.nil?
419
- configured_cmd = @config.fetch("lsp.servers.#{lang}") || Client::SERVERS[lang]
420
- client = Client.new(lang, root_dir: @root_dir, server_cmd: configured_cmd)
421
- unless client.available?
422
- probed_cmd = Runtime::Discovery.probe_lsp(lang, root_dir: @root_dir, custom_cmd: configured_cmd)
423
- client = Client.new(lang, root_dir: @root_dir, server_cmd: probed_cmd) if probed_cmd
424
- end
390
+ outcome = @provisioner.provision(lang)
391
+ client = outcome[:client]
425
392
 
426
- if client.available? && @config.fetch("lsp.auto_start", true) && !circuit_open?(lang)
393
+ if client.available? && Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true) && !circuit_open?(lang)
427
394
  if client.start
428
- note_healthy_start(lang)
429
- @recovery_errors.delete(lang)
430
- register_client_unlocked(lang, client)
395
+ start_succeeded(lang, client)
431
396
  else
432
- record_crash(lang)
433
- stop_client_safely(client)
434
- unregister_client_unlocked(lang)
435
- @recovery_errors[lang] = "LSP server restart failed for '#{lang}'" if recovering
397
+ restart_failed(lang, client, reason: recovering ? "LSP server restart failed for '#{lang}'" : nil)
436
398
  end
437
399
  elsif recovering
438
400
  @recovery_errors[lang] = if circuit_open?(lang)
439
401
  "LSP server restart for '#{lang}' is blocked by the circuit breaker"
440
- elsif !@config.fetch("lsp.auto_start", true)
402
+ elsif !Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true)
441
403
  "LSP server restart for '#{lang}' is disabled"
442
404
  else
443
405
  "LSP server restart failed for '#{lang}': server unavailable"
444
406
  end
445
407
  end
446
408
  @clients[lang] = client
447
- elsif !client.running && @config.fetch("lsp.auto_start", true) &&
409
+ elsif !client.running && Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true) &&
448
410
  client.available? && !circuit_open?(lang)
449
411
  if client.start
450
- note_healthy_start(lang)
451
- @recovery_errors.delete(lang)
452
- register_client_unlocked(lang, client)
412
+ start_succeeded(lang, client)
453
413
  else
454
- record_crash(lang)
455
- stop_client_safely(client)
456
- unregister_client_unlocked(lang)
457
- @recovery_errors[lang] = "LSP server restart failed for '#{lang}'" if recovering
414
+ restart_failed(lang, client, reason: recovering ? "LSP server restart failed for '#{lang}'" : nil)
458
415
  end
459
416
  end
460
417
 
461
418
  client
462
419
  end
463
420
 
421
+ # Shared failure path for the restart branches: count the crash, tear
422
+ # the client down, drop its registration, and (optionally) record the
423
+ # recovery error shown to later callers. Caller MUST hold @monitor.
424
+ def restart_failed(lang, client, reason: nil, drop_stored: false, unregister: true)
425
+ record_crash(lang)
426
+ stop_client_safely(client)
427
+ @clients.delete(lang) if drop_stored && @clients[lang].equal?(client)
428
+ unregister_client_unlocked(lang) if unregister
429
+ @recovery_errors[lang] = reason if reason
430
+ end
431
+
432
+ # Success path counterpart of restart_failed. Caller MUST hold @monitor.
433
+ def start_succeeded(lang, client)
434
+ @breaker.note_healthy_start(lang)
435
+ @recovery_errors.delete(lang)
436
+ register_client_unlocked(lang, client)
437
+ end
438
+
464
439
  def client_dead?(client)
465
440
  (client.running && client.respond_to?(:alive?) && !client.alive?) ||
466
441
  (client.respond_to?(:crashed?) && client.crashed?)
@@ -503,10 +478,6 @@ module Rubik
503
478
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
504
479
  end
505
480
 
506
- def note_healthy_start(lang)
507
- @healthy_since[lang.to_s.downcase] = monotonic_now
508
- end
509
-
510
481
  def inactive_client_error(file_path, client)
511
482
  lang = client&.language || detect_language(file_path)
512
483
  @monitor.synchronize do
@@ -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