rubik 0.15.1 → 0.15.3

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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +67 -2
  4. data/lib/rubik/ast/cache_store.rb +22 -4
  5. data/lib/rubik/ast/extractor.rb +1 -4
  6. data/lib/rubik/ast/ruby_extractor.rb +23 -1
  7. data/lib/rubik/ast/symbol.rb +9 -4
  8. data/lib/rubik/ast/treesitter/python_walker.rb +5 -4
  9. data/lib/rubik/ast/treesitter_extractor.rb +10 -3
  10. data/lib/rubik/buffer/diff.rb +34 -2
  11. data/lib/rubik/buffer/manager.rb +18 -15
  12. data/lib/rubik/buffer/path_validator.rb +18 -2
  13. data/lib/rubik/buffer/snapshot.rb +144 -9
  14. data/lib/rubik/concurrency/timers.rb +7 -0
  15. data/lib/rubik/config/scoped.rb +18 -7
  16. data/lib/rubik/config/secret_policy.rb +22 -1
  17. data/lib/rubik/config.rb +24 -26
  18. data/lib/rubik/context/ignore_rules.rb +7 -2
  19. data/lib/rubik/diagnostics/parser.rb +19 -8
  20. data/lib/rubik/kernel/dispatcher.rb +11 -5
  21. data/lib/rubik/llm/client.rb +24 -3
  22. data/lib/rubik/llm/connection_pool.rb +51 -30
  23. data/lib/rubik/llm/message_sanitizer.rb +20 -1
  24. data/lib/rubik/llm/sse_parser.rb +10 -1
  25. data/lib/rubik/lsp/client.rb +9 -2
  26. data/lib/rubik/lsp/manager.rb +43 -40
  27. data/lib/rubik/lsp/transport.rb +61 -1
  28. data/lib/rubik/mcp/transport/stdio.rb +143 -131
  29. data/lib/rubik/orchestrator/activity_log.rb +3 -2
  30. data/lib/rubik/orchestrator/goal_session.rb +14 -14
  31. data/lib/rubik/orchestrator/loop.rb +11 -12
  32. data/lib/rubik/orchestrator/subagent/completion.rb +18 -8
  33. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +5 -1
  34. data/lib/rubik/platform/process.rb +74 -19
  35. data/lib/rubik/platform.rb +16 -16
  36. data/lib/rubik/plugin/manager.rb +5 -6
  37. data/lib/rubik/repl/service_catalog.rb +1 -1
  38. data/lib/rubik/repl/terminal_guard.rb +29 -23
  39. data/lib/rubik/repl/turn_recorder.rb +35 -11
  40. data/lib/rubik/sandbox/driver/base.rb +26 -3
  41. data/lib/rubik/sandbox/driver/bubblewrap.rb +6 -11
  42. data/lib/rubik/sandbox/driver/docker.rb +1 -11
  43. data/lib/rubik/sandbox/driver/podman.rb +26 -18
  44. data/lib/rubik/sandbox/driver/process.rb +16 -35
  45. data/lib/rubik/security/blocked_guidance.rb +4 -2
  46. data/lib/rubik/security/command_gate.rb +27 -14
  47. data/lib/rubik/security/errors.rb +13 -0
  48. data/lib/rubik/security/guard.rb +11 -2
  49. data/lib/rubik/security/path_policy.rb +9 -6
  50. data/lib/rubik/storage/store.rb +31 -11
  51. data/lib/rubik/tools/builtins/exec.rb +5 -1
  52. data/lib/rubik/tools/builtins/files.rb +2 -0
  53. data/lib/rubik/tools/uri_resolver.rb +23 -3
  54. data/lib/rubik/version.rb +1 -1
  55. metadata +1 -1
@@ -22,6 +22,8 @@ module Rubik
22
22
  @pool = {}
23
23
  @pool_locks = {}
24
24
  @pool_locks_guard = Mutex.new
25
+ @pool_owners = {}
26
+ @pool_owners_guard = Mutex.new
25
27
  @integrity_lost_errors = nil
26
28
 
27
29
  class << self
@@ -46,42 +48,45 @@ module Rubik
46
48
  # Yields an HTTP session for (host, port, scheme), reusing the pooled
47
49
  # keep-alive session when one exists. Same-thread re-entry falls back
48
50
  # to a private single-use connection (see the class comment).
49
- def with_connection(uri, timeout:, open_timeout:)
51
+ def with_connection(uri, timeout:, open_timeout:, &block)
50
52
  key = [uri.host, uri.port, uri.scheme == "https"]
51
53
  lock = @pool_locks_guard.synchronize { @pool_locks[key] ||= Mutex.new }
52
54
 
53
- begin
54
- lock.synchronize do
55
- http = @pool_mutex.synchronize { @pool[key] }
56
- if http&.started?
57
- http.read_timeout = timeout
58
- http.open_timeout = open_timeout
59
- else
60
- http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
61
- http.keep_alive_timeout = CONNECTION_IDLE_TIMEOUT
62
- http.start
63
- @pool_mutex.synchronize { @pool[key] = http }
64
- end
55
+ # Only a genuine same-thread re-entry takes the private-connection
56
+ # path. A ThreadError raised by the caller's callback is an ordinary
57
+ # application error: it must propagate, never re-issue the request.
58
+ return with_private_connection(uri, timeout: timeout, open_timeout: open_timeout, &block) if owned_by_current_thread?(key)
59
+
60
+ unless lock.try_lock
61
+ # Held by another thread — re-check for a same-thread re-entry that
62
+ # landed between the first check and try_lock, then queue normally.
63
+ return with_private_connection(uri, timeout: timeout, open_timeout: open_timeout, &block) if owned_by_current_thread?(key)
64
+
65
+ lock.lock
66
+ end
65
67
 
66
- begin
67
- yield http
68
- rescue *integrity_lost_errors
69
- drop(key, http)
70
- raise
71
- end
68
+ begin
69
+ @pool_owners_guard.synchronize { @pool_owners[key] = Thread.current }
70
+ http = @pool_mutex.synchronize { @pool[key] }
71
+ if http&.started?
72
+ http.read_timeout = timeout
73
+ http.open_timeout = open_timeout
74
+ else
75
+ http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
76
+ http.keep_alive_timeout = CONNECTION_IDLE_TIMEOUT
77
+ http.start
78
+ @pool_mutex.synchronize { @pool[key] = http }
72
79
  end
73
- rescue ThreadError
74
- # Re-entrant call on the same thread (nested LLM call from inside a
75
- # streaming callback, or a second Enumerator#next fiber) — the
76
- # per-key mutex cannot be taken. Serve this call on a private,
77
- # single-use connection instead of deadlocking.
78
- http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
79
- http.start
80
- begin
81
- yield http
82
- ensure
83
- http.finish if http.started?
80
+
81
+ block.call(http)
82
+ rescue *integrity_lost_errors
83
+ drop(key, http)
84
+ raise
85
+ ensure
86
+ @pool_owners_guard.synchronize do
87
+ @pool_owners.delete(key) if @pool_owners[key].equal?(Thread.current)
84
88
  end
89
+ lock.unlock
85
90
  end
86
91
  end
87
92
 
@@ -104,6 +109,22 @@ module Rubik
104
109
  http.read_timeout = timeout
105
110
  http
106
111
  end
112
+
113
+ private
114
+
115
+ def owned_by_current_thread?(key)
116
+ @pool_owners_guard.synchronize { @pool_owners[key].equal?(Thread.current) }
117
+ end
118
+
119
+ def with_private_connection(uri, timeout:, open_timeout:, &block)
120
+ http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
121
+ http.start
122
+ begin
123
+ block.call(http)
124
+ ensure
125
+ http.finish if http.started?
126
+ end
127
+ end
107
128
  end
108
129
  end
109
130
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../prompt/conversation_rows"
4
+ require_relative "../security/redactor"
4
5
 
5
6
  module Rubik
6
7
  module LLM
@@ -18,6 +19,11 @@ module Rubik
18
19
  # * role:"tool" messages are never merged — each carries its own
19
20
  # tool_call_id; strict providers coalesce them at format time.
20
21
  module MessageSanitizer
22
+ # Issue #634: a rejected message used to be logged with msg.inspect,
23
+ # dumping full prompts and tool arguments into debug logs. Only a short
24
+ # bounded preview is emitted now.
25
+ REJECTED_PREVIEW_CHARS = 80
26
+
21
27
  module_function
22
28
 
23
29
  def normalize(messages)
@@ -26,7 +32,7 @@ module Rubik
26
32
  merged = []
27
33
  messages.each do |msg|
28
34
  unless Prompt::ConversationRows.row?(msg)
29
- Rubik::Platform.debug_warn("MessageSanitizer: dropping non-hash message #{msg.inspect}")
35
+ Rubik::Platform.debug_warn(rejection_detail(msg))
30
36
  next
31
37
  end
32
38
 
@@ -74,6 +80,19 @@ module Rubik
74
80
  nil
75
81
  end
76
82
 
83
+ def rejection_detail(msg)
84
+ preview = begin
85
+ text = Rubik::Security::Redactor.redact(msg.inspect)
86
+ # A custom inspect can embed newlines/ANSI; do not let it forge a
87
+ # log line through the debug warning below.
88
+ text = text.gsub(/[[:cntrl:]]/, "?")
89
+ text.length > REJECTED_PREVIEW_CHARS ? "#{text[0, REJECTED_PREVIEW_CHARS]}…" : text
90
+ rescue StandardError
91
+ "<inspect failed>"
92
+ end
93
+ "MessageSanitizer: dropping non-hash message (class=#{msg.class}) #{preview}"
94
+ end
95
+
77
96
  def join_content(first, second)
78
97
  first_str = first.to_s
79
98
  second_str = second.to_s
@@ -17,6 +17,12 @@ module Rubik
17
17
  SSE_TERMINATOR = "\n\n"
18
18
  NDJSON_TERMINATOR = "\n"
19
19
 
20
+ # Issue #634: a keepalive-only stream never produces an event, so the
21
+ # full-body fallback buffer used to grow for the whole connection. Only
22
+ # the tail is retained; once a real SSE event surfaces the fallback is
23
+ # disabled, so the cap never affects genuine event parsing.
24
+ MAX_RAW_BODY_BYTES = 64 * 1024
25
+
20
26
  def initialize(terminator: SSE_TERMINATOR)
21
27
  @terminator = terminator
22
28
  @terminator_bin = terminator.is_a?(String) ? terminator.b : terminator
@@ -36,7 +42,10 @@ module Rubik
36
42
  @buffer << bytes
37
43
  # Stop remembering once a real event surfaced — from then on this is
38
44
  # a genuine SSE stream and the fallback must never fire.
39
- @raw_body << bytes if @sse_mode && !@any_event
45
+ if @sse_mode && !@any_event
46
+ @raw_body << bytes
47
+ @raw_body = @raw_body.byteslice(-MAX_RAW_BODY_BYTES, MAX_RAW_BODY_BYTES) if @raw_body.bytesize > MAX_RAW_BODY_BYTES
48
+ end
40
49
  drain_complete_events(final: false)
41
50
  end
42
51
 
@@ -40,6 +40,13 @@ module Rubik
40
40
  # lsp_diagnostics tool from ever hanging on a wedged server.
41
41
  DIAGNOSTIC_PULL_TIMEOUT = 5
42
42
 
43
+ # The snapshot shape returned when no running client holds the
44
+ # document — one home shared by Client rescue paths and the Manager's
45
+ # inactive fallbacks, so the contract gains a key once, not 4×.
46
+ UNAVAILABLE_SNAPSHOT = {
47
+ state: :unavailable, diagnostics: [].freeze, document_version: nil, published_version: nil
48
+ }.freeze
49
+
43
50
  attr_reader :language, :server_cmd, :root_dir, :server_capabilities,
44
51
  :sync_capabilities
45
52
  # The JSON-RPC transport delegate (issue #470). Exposed read-only as a
@@ -329,7 +336,7 @@ module Rubik
329
336
  refresh_diagnostics_via_pull(abs_path) if pull_diagnostics? && snapshot[:state] == :pending
330
337
  snapshot
331
338
  rescue ArgumentError, TypeError
332
- { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
339
+ UNAVAILABLE_SNAPSHOT
333
340
  end
334
341
 
335
342
  def wait_for_diagnostics(file_path, timeout: 0.2)
@@ -343,7 +350,7 @@ module Rubik
343
350
  @document_store.diagnostic_snapshot(abs_path)[:state] == :pending
344
351
  @document_store.await_diagnostics(abs_path, timeout)
345
352
  rescue ArgumentError, TypeError
346
- { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil }
353
+ UNAVAILABLE_SNAPSHOT
347
354
  end
348
355
 
349
356
  def all_diagnostic_snapshots
@@ -197,33 +197,30 @@ module Rubik
197
197
  def sync_buffer_file(file_path, content)
198
198
  return false unless enabled?
199
199
 
200
- client = client_for(file_path)
201
- return false unless client&.running
202
-
203
- # Try did_change, or did_open if not yet opened
204
- if client.document_open?(file_path)
205
- client.did_change(file_path, content)
206
- else
207
- client.did_open(file_path, content, document_language_id(file_path))
200
+ with_running_client(file_path, ->(_client) { false }) do |client|
201
+ # Try did_change, or did_open if not yet opened
202
+ if client.document_open?(file_path)
203
+ client.did_change(file_path, content)
204
+ else
205
+ client.did_open(file_path, content, document_language_id(file_path))
206
+ end
208
207
  end
209
208
  end
210
209
 
211
210
  def did_save_file(file_path, content = nil)
212
211
  return false unless enabled?
213
212
 
214
- client = client_for(file_path)
215
- return false unless client&.running
216
-
217
- client.did_save(file_path, content)
213
+ with_running_client(file_path, ->(_client) { false }) do |client|
214
+ client.did_save(file_path, content)
215
+ end
218
216
  end
219
217
 
220
218
  def did_close_file(file_path)
221
219
  return false unless enabled?
222
220
 
223
- client = client_for(file_path)
224
- return false unless client&.running
225
-
226
- client.did_close(file_path)
221
+ with_running_client(file_path, ->(_client) { false }) do |client|
222
+ client.did_close(file_path)
223
+ end
227
224
  end
228
225
 
229
226
  def sync_buffer_file_if_needed(file_path, buffer_manager)
@@ -257,24 +254,21 @@ module Rubik
257
254
  end
258
255
 
259
256
  def diagnostics_for(file_path)
260
- client = client_for(file_path)
261
- return [] unless client&.running
262
-
263
- client.diagnostics_for(file_path)
257
+ with_running_client(file_path, ->(_client) { [] }) do |client|
258
+ client.diagnostics_for(file_path)
259
+ end
264
260
  end
265
261
 
266
262
  def diagnostics_snapshot_for(file_path)
267
- client = client_for(file_path)
268
- return { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil } unless client&.running
269
-
270
- client.diagnostics_snapshot_for(file_path)
263
+ with_running_client(file_path, ->(_client) { Client::UNAVAILABLE_SNAPSHOT }) do |client|
264
+ client.diagnostics_snapshot_for(file_path)
265
+ end
271
266
  end
272
267
 
273
268
  def wait_for_diagnostics(file_path, timeout: 0.2)
274
- client = client_for(file_path)
275
- return { state: :unavailable, diagnostics: [], document_version: nil, published_version: nil } unless client&.running
276
-
277
- client.wait_for_diagnostics(file_path, timeout: timeout)
269
+ with_running_client(file_path, ->(_client) { Client::UNAVAILABLE_SNAPSHOT }) do |client|
270
+ client.wait_for_diagnostics(file_path, timeout: timeout)
271
+ end
278
272
  end
279
273
 
280
274
  def all_diagnostics
@@ -316,24 +310,21 @@ module Rubik
316
310
  end
317
311
 
318
312
  def hover(file_path, line, character)
319
- client = client_for(file_path)
320
- return { ok: false, error: inactive_client_error(file_path, client) } unless client&.running
321
-
322
- client.hover(file_path, line, character)
313
+ with_running_client(file_path, ->(client) { { ok: false, error: inactive_client_error(file_path, client) } }) do |client|
314
+ client.hover(file_path, line, character)
315
+ end
323
316
  end
324
317
 
325
318
  def definition(file_path, line, character)
326
- client = client_for(file_path)
327
- return { ok: false, error: inactive_client_error(file_path, client) } unless client&.running
328
-
329
- client.definition(file_path, line, character)
319
+ with_running_client(file_path, ->(client) { { ok: false, error: inactive_client_error(file_path, client) } }) do |client|
320
+ client.definition(file_path, line, character)
321
+ end
330
322
  end
331
323
 
332
324
  def references(file_path, line, character)
333
- client = client_for(file_path)
334
- return { ok: false, error: inactive_client_error(file_path, client) } unless client&.running
335
-
336
- client.references(file_path, line, character)
325
+ with_running_client(file_path, ->(client) { { ok: false, error: inactive_client_error(file_path, client) } }) do |client|
326
+ client.references(file_path, line, character)
327
+ end
337
328
  end
338
329
 
339
330
  def status_summary
@@ -498,6 +489,18 @@ module Rubik
498
489
  @recovery_errors[lang] || "LSP not active for '#{file_path}'"
499
490
  end
500
491
  end
492
+
493
+ # One running-client funnel for the file-scoped delegation methods:
494
+ # resolves the client and yields it while running; otherwise calls the
495
+ # fallback lambda with the (possibly nil) client. The fallback owns the
496
+ # per-method inactive shape (false, [], the unavailable snapshot, the
497
+ # hover-error hash) so observable outputs stay byte-identical.
498
+ def with_running_client(file_path, fallback)
499
+ client = client_for(file_path)
500
+ return fallback.call(client) unless client&.running
501
+
502
+ yield client
503
+ end
501
504
  end
502
505
  end
503
506
  end
@@ -44,6 +44,16 @@ module Rubik
44
44
  # bytes. 16 MiB is far above any legitimate LSP payload.
45
45
  MAX_FRAME_SIZE = 16 * 1024 * 1024
46
46
 
47
+ # Deadline for reading a frame BODY after a valid Content-Length header.
48
+ # Issue #631: a server that sent the header then stalled mid-body used to
49
+ # park the reader forever on @stdout.read while alive? still reported a
50
+ # healthy server.
51
+ BODY_READ_TIMEOUT = 5.0
52
+
53
+ # Max bytes pulled from the pipe per readpartial call while draining a
54
+ # frame body. Bounds allocations without penalizing large frames.
55
+ BODY_READ_CHUNK = 64 * 1024
56
+
47
57
  attr_reader :mutex, :cv, :running
48
58
 
49
59
  def initialize(root_dir:, on_message:)
@@ -348,7 +358,7 @@ module Rubik
348
358
  break
349
359
  end
350
360
 
351
- body = @stdout.read(content_length)
361
+ body = read_body_with_deadline(content_length)
352
362
  break unless body
353
363
 
354
364
  begin
@@ -377,6 +387,56 @@ module Rubik
377
387
 
378
388
  private
379
389
 
390
+ # Deadline-bounded exact read of a frame body. IO.select bounds the wait
391
+ # for readability; readpartial is non-blocking once data is ready. On
392
+ # timeout or EOF after a partial read we record @reader_failure and
393
+ # return nil, so the caller breaks the loop and the ensure block marks
394
+ # the reader dead — the circuit breaker can then trip (issue #631).
395
+ def read_body_with_deadline(length)
396
+ body = String.new(encoding: Encoding::ASCII_8BIT)
397
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + body_read_timeout
398
+
399
+ while body.bytesize < length
400
+ remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
401
+ # IO.select is deliberate: the reader runs on its own Thread, not a
402
+ # Fiber scheduler, so the scheduler-compatibility cop does not apply.
403
+ if remaining <= 0 || IO.select([@stdout], nil, nil, remaining).nil? # rubocop:disable Lint/IncompatibleIoSelectWithFiberScheduler
404
+ record_reader_failure("LSP response body read timed out after #{body_read_timeout}s " \
405
+ "(#{body.bytesize}/#{length} bytes)")
406
+ return nil
407
+ end
408
+
409
+ begin
410
+ chunk = @stdout.readpartial([length - body.bytesize, BODY_READ_CHUNK].min)
411
+ rescue EOFError
412
+ record_reader_failure("LSP server closed connection mid-body (#{body.bytesize}/#{length} bytes)")
413
+ return nil
414
+ end
415
+ if chunk.nil? || chunk.empty?
416
+ record_reader_failure("LSP server closed connection mid-body (#{body.bytesize}/#{length} bytes)")
417
+ return nil
418
+ end
419
+
420
+ body << chunk
421
+ end
422
+
423
+ body.bytesize == length ? body : nil
424
+ end
425
+
426
+ def record_reader_failure(message)
427
+ @mutex.synchronize do
428
+ next if @reader_failure
429
+
430
+ @reader_failure = message
431
+ end
432
+ end
433
+
434
+ # Single source for the body deadline; overridable in tests so a stalled
435
+ # pipe can be exercised without waiting the full 5s.
436
+ def body_read_timeout
437
+ BODY_READ_TIMEOUT
438
+ end
439
+
380
440
  # Responses are correlated inside the transport; every other message
381
441
  # (server requests, notifications such as publishDiagnostics) is
382
442
  # protocol and goes to the Client via the on_message callback.