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
@@ -5,6 +5,8 @@ require "json"
5
5
  require "time"
6
6
  require "uri"
7
7
  require_relative "../platform"
8
+ require_relative "sse_parser"
9
+ require_relative "connection_pool"
8
10
 
9
11
  module Rubik
10
12
  module LLM
@@ -42,120 +44,6 @@ module Rubik
42
44
  end
43
45
  end
44
46
 
45
- # Buffers raw bytes from a stream and emits complete events.
46
- # Default delimiter is the SSE blank line (\n\n); pass
47
- # `terminator: "\n"` to parse NDJSON streams (one JSON object per line).
48
- # One event is a Hash like:
49
- # { type: "message", data: "json-string" }
50
- # In SSE mode, multi-line `data:` fields are joined with \n. In NDJSON
51
- # mode (`terminator: "\n"`), the `type` field is always "message" and
52
- # the entire line is the `data`. Incomplete events are held in the
53
- # internal buffer until the next feed.
54
- class SSEParser
55
- SSE_EVENT_SEP = /\r\n\r\n|\n\n|\r\n\n|\n\r\n|\r\r/
56
- SSE_TERMINATOR = "\n\n"
57
- NDJSON_TERMINATOR = "\n"
58
-
59
- def initialize(terminator: SSE_TERMINATOR)
60
- @terminator = terminator
61
- @sse_mode = @terminator == SSE_TERMINATOR || @terminator == SSE_EVENT_SEP
62
- @buffer = String.new(encoding: Encoding::UTF_8)
63
- end
64
-
65
- # Feed raw bytes; returns an array of complete events parsed from this
66
- # chunk. Partial events stay in the buffer.
67
- def feed(chunk_bytes)
68
- incoming = chunk_bytes.to_s.dup.force_encoding(Encoding::UTF_8)
69
- incoming = incoming.scrub unless incoming.valid_encoding?
70
- @buffer << incoming
71
- events = []
72
-
73
- if @sse_mode
74
- while (m = @buffer.match(SSE_EVENT_SEP))
75
- # Chunk boundary \r buffering: if match ends on \r at the very end of @buffer,
76
- # wait for next feed to see if \n follows (e.g. \r\r could become \r\r\n).
77
- break if m.end(0) == @buffer.bytesize && @buffer.end_with?("\r")
78
-
79
- event_block = @buffer[0...m.begin(0)]
80
- @buffer = @buffer[m.end(0)..] || String.new(encoding: Encoding::UTF_8)
81
- parsed = parse_event(event_block)
82
- events << parsed if parsed
83
- end
84
- else
85
- while (idx = @buffer.index(@terminator))
86
- event_block = @buffer[0...idx]
87
- @buffer = @buffer[(idx + @terminator.length)..] || String.new(encoding: Encoding::UTF_8)
88
- parsed = parse_event(event_block)
89
- events << parsed if parsed
90
- end
91
- end
92
-
93
- events
94
- end
95
-
96
- # Flush any trailing event (no trailing terminator) and reset.
97
- def flush
98
- return [] if @buffer.empty?
99
-
100
- events = []
101
- if @sse_mode
102
- while (m = @buffer.match(SSE_EVENT_SEP))
103
- event_block = @buffer[0...m.begin(0)]
104
- @buffer = @buffer[m.end(0)..] || String.new(encoding: Encoding::UTF_8)
105
- parsed = parse_event(event_block)
106
- events << parsed if parsed
107
- end
108
- else
109
- while (idx = @buffer.index(@terminator))
110
- event_block = @buffer[0...idx]
111
- @buffer = @buffer[(idx + @terminator.length)..] || String.new(encoding: Encoding::UTF_8)
112
- parsed = parse_event(event_block)
113
- events << parsed if parsed
114
- end
115
- end
116
-
117
- unless @buffer.empty?
118
- remaining = @buffer.dup
119
- @buffer = String.new(encoding: Encoding::UTF_8)
120
- parsed = parse_event(remaining)
121
- events << parsed if parsed
122
- end
123
-
124
- events
125
- end
126
-
127
- private
128
-
129
- def parse_event(block)
130
- return nil if block.nil?
131
- return { type: "message", data: block.to_s.chomp } unless @sse_mode
132
-
133
- lines = block.to_s.split(/\r\n|\r|\n/)
134
- return nil if lines.empty?
135
-
136
- event_type = "message"
137
- data_lines = []
138
- has_field = false
139
-
140
- lines.each do |line|
141
- next if line.empty?
142
- next if line.start_with?(":")
143
-
144
- if (m = line.match(/\Aevent:\s*(.+)\z/))
145
- event_type = m[1]
146
- has_field = true
147
- elsif (m = line.match(/\Adata:\s?(.*)\z/))
148
- data_lines << m[1]
149
- has_field = true
150
- end
151
- end
152
-
153
- return nil unless has_field
154
-
155
- { type: event_type, data: data_lines.join("\n") }
156
- end
157
- end
158
-
159
47
  class HTTPClient
160
48
  # Errors that are safe to retry with an idempotent bounded backoff:
161
49
  # connect-phase failures and transport resets. Net::ReadTimeout is
@@ -372,88 +260,12 @@ module Rubik
372
260
  end
373
261
  end
374
262
 
375
- # Builds the real Net::HTTP transport. open_timeout is bounded LOW and
376
- # independent of the (long) read timeout: Net::HTTP arms a Timeout thread
377
- # around TCP connect on Ruby 3.3; a short arming window shrinks the chance
378
- # that a stale Net::OpenTimeout is injected into unrelated main-thread
379
- # code after an Interrupt escapes the connect region.
380
263
  def self.default_http(uri, timeout:, open_timeout:)
381
- http = Net::HTTP.new(uri.host, uri.port)
382
- http.use_ssl = (uri.scheme == "https")
383
- http.open_timeout = open_timeout
384
- http.read_timeout = timeout
385
- http
264
+ ConnectionPool.default_http(uri, timeout: timeout, open_timeout: open_timeout)
386
265
  end
387
266
 
388
- # ---- Persistent connection pool -------------------------------------
389
- #
390
- # Opening a brand-new TCP(+TLS) connection per LLM round-trip costs
391
- # ~100-300 ms each, and an agent turn issues many round-trips. Sessions
392
- # here persist per (host, port, scheme); a mutex per key serializes
393
- # concurrent use because a single Net::HTTP session is not thread-safe
394
- # (same-endpoint calls wait for the current response to finish).
395
- # Ruby's non-reentrant Mutex would deadlock on same-thread re-entry
396
- # (nested provider call, Enumerator#next fibers) — those transparently
397
- # fall back to a private single-use connection. Transport failures and
398
- # interrupts drop the session so the next attempt dials fresh.
399
- CONNECTION_IDLE_TIMEOUT = 300
400
-
401
- # After any of these the socket's integrity is unknown (or the stream
402
- # was cut mid-read by Ctrl+C): never hand the session back to the pool.
403
- INTEGRITY_LOST_ERRORS = [
404
- *RETRYABLE_ERRORS, Net::ReadTimeout, IOError,
405
- OpenSSL::SSL::SSLError, Net::HTTPBadResponse, Interrupt
406
- ].freeze
407
-
408
- @pool_mutex = Mutex.new
409
- @pool = {}
410
- @pool_locks = {}
411
- @pool_locks_guard = Mutex.new
412
-
413
- def self.with_pooled_connection(uri, timeout:, open_timeout:)
414
- key = [uri.host, uri.port, uri.scheme == "https"]
415
- lock = @pool_locks_guard.synchronize { @pool_locks[key] ||= Mutex.new }
416
-
417
- begin
418
- lock.synchronize do
419
- http = @pool_mutex.synchronize { @pool[key] }
420
- if http&.started?
421
- http.read_timeout = timeout
422
- http.open_timeout = open_timeout
423
- else
424
- http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
425
- http.keep_alive_timeout = CONNECTION_IDLE_TIMEOUT
426
- http.start
427
- @pool_mutex.synchronize { @pool[key] = http }
428
- end
429
-
430
- begin
431
- yield http
432
- rescue *INTEGRITY_LOST_ERRORS
433
- drop_pooled(key, http)
434
- raise
435
- end
436
- end
437
- rescue ThreadError
438
- # Re-entrant call on the same thread (nested LLM call from inside a
439
- # streaming callback, or a second Enumerator#next fiber) — the
440
- # per-key mutex cannot be taken. Serve this call on a private,
441
- # single-use connection instead of deadlocking.
442
- http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
443
- http.start
444
- begin
445
- yield http
446
- ensure
447
- http.finish if http.started?
448
- end
449
- end
450
- end
451
-
452
- def self.drop_pooled(key, http)
453
- @pool_mutex.synchronize { @pool.delete(key) if @pool[key].equal?(http) }
454
- http.finish if http&.started?
455
- rescue StandardError
456
- nil
267
+ def self.with_pooled_connection(uri, timeout:, open_timeout:, &)
268
+ ConnectionPool.with_connection(uri, timeout: timeout, open_timeout: open_timeout, &)
457
269
  end
458
270
 
459
271
  def self.build_post_request(uri, headers, payload, accept: nil)
@@ -490,13 +302,7 @@ module Rubik
490
302
  def self.post_stream_json(url_str, headers: {}, payload: {}, timeout: 120, open_timeout: 15,
491
303
  idle_timeout: nil,
492
304
  terminator: SSEParser::SSE_TERMINATOR, http_factory: nil, &)
493
- begin
494
- uri = URI.parse(url_str.to_s)
495
- rescue URI::InvalidURIError => e
496
- raise ClientError, "Invalid endpoint URL '#{url_str}': #{e.message}"
497
- end
498
-
499
- raise ClientError, "Invalid endpoint URL '#{url_str}'. URL must start with http:// or https://" unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
305
+ uri = parse_and_validate_uri(url_str)
500
306
 
501
307
  effective_timeout = idle_timeout || timeout
502
308
  state = { surfaced: false }
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+
5
+ module Rubik
6
+ module LLM
7
+ # ---- Persistent connection pool ---------------------------------------
8
+ #
9
+ # Opening a brand-new TCP(+TLS) connection per LLM round-trip costs
10
+ # ~100-300 ms each, and an agent turn issues many round-trips. Sessions
11
+ # here persist per (host, port, scheme); a mutex per key serializes
12
+ # concurrent use because a single Net::HTTP session is not thread-safe
13
+ # (same-endpoint calls wait for the current response to finish).
14
+ # Ruby's non-reentrant Mutex would deadlock on same-thread re-entry
15
+ # (nested provider call, Enumerator#next fibers) — those transparently
16
+ # fall back to a private single-use connection. Transport failures and
17
+ # interrupts drop the session so the next attempt dials fresh.
18
+ class ConnectionPool
19
+ CONNECTION_IDLE_TIMEOUT = 300
20
+
21
+ @pool_mutex = Mutex.new
22
+ @pool = {}
23
+ @pool_locks = {}
24
+ @pool_locks_guard = Mutex.new
25
+ @integrity_lost_errors = nil
26
+
27
+ class << self
28
+ # After any of these the socket's integrity is unknown (or the stream
29
+ # was cut mid-read by Ctrl+C): never hand the session back to the pool.
30
+ # Resolved lazily (not a load-time constant) because HTTPClient::
31
+ # RETRYABLE_ERRORS lives in client.rb, which requires this file before
32
+ # defining the HTTPClient class.
33
+ def integrity_lost_errors
34
+ @integrity_lost_errors ||= [
35
+ *HTTPClient::RETRYABLE_ERRORS, Net::ReadTimeout, IOError,
36
+ OpenSSL::SSL::SSLError, Net::HTTPBadResponse, Interrupt
37
+ ].freeze
38
+ end
39
+
40
+ # Yields an HTTP session for (host, port, scheme), reusing the pooled
41
+ # keep-alive session when one exists. Same-thread re-entry falls back
42
+ # to a private single-use connection (see the class comment).
43
+ def with_connection(uri, timeout:, open_timeout:)
44
+ key = [uri.host, uri.port, uri.scheme == "https"]
45
+ lock = @pool_locks_guard.synchronize { @pool_locks[key] ||= Mutex.new }
46
+
47
+ begin
48
+ lock.synchronize do
49
+ http = @pool_mutex.synchronize { @pool[key] }
50
+ if http&.started?
51
+ http.read_timeout = timeout
52
+ http.open_timeout = open_timeout
53
+ else
54
+ http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
55
+ http.keep_alive_timeout = CONNECTION_IDLE_TIMEOUT
56
+ http.start
57
+ @pool_mutex.synchronize { @pool[key] = http }
58
+ end
59
+
60
+ begin
61
+ yield http
62
+ rescue *integrity_lost_errors
63
+ drop(key, http)
64
+ raise
65
+ end
66
+ end
67
+ rescue ThreadError
68
+ # Re-entrant call on the same thread (nested LLM call from inside a
69
+ # streaming callback, or a second Enumerator#next fiber) — the
70
+ # per-key mutex cannot be taken. Serve this call on a private,
71
+ # single-use connection instead of deadlocking.
72
+ http = default_http(uri, timeout: timeout, open_timeout: open_timeout)
73
+ http.start
74
+ begin
75
+ yield http
76
+ ensure
77
+ http.finish if http.started?
78
+ end
79
+ end
80
+ end
81
+
82
+ def drop(key, http)
83
+ @pool_mutex.synchronize { @pool.delete(key) if @pool[key].equal?(http) }
84
+ http.finish if http&.started?
85
+ rescue StandardError
86
+ nil
87
+ end
88
+
89
+ # Builds the real Net::HTTP transport. open_timeout is bounded LOW and
90
+ # independent of the (long) read timeout: Net::HTTP arms a Timeout thread
91
+ # around TCP connect on Ruby 3.3; a short arming window shrinks the chance
92
+ # that a stale Net::OpenTimeout is injected into unrelated main-thread
93
+ # code after an Interrupt escapes the connect region.
94
+ def default_http(uri, timeout:, open_timeout:)
95
+ http = Net::HTTP.new(uri.host, uri.port)
96
+ http.use_ssl = (uri.scheme == "https")
97
+ http.open_timeout = open_timeout
98
+ http.read_timeout = timeout
99
+ http
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../log"
3
4
  require_relative "openai_compatible"
5
+ require_relative "../security/secret_store"
6
+ require_relative "../storage/layout"
4
7
  require_relative "ollama"
5
8
  require_relative "openai"
6
9
  require_relative "anthropic"
@@ -87,7 +90,7 @@ module Rubik
87
90
  )
88
91
  end
89
92
 
90
- custom_providers = config.fetch("providers", {})
93
+ custom_providers = Rubik::Storage::Layout.config_get(config, "providers", {})
91
94
  return unless custom_providers.is_a?(Hash)
92
95
 
93
96
  custom_providers.each do |p_name, p_spec|
@@ -131,13 +134,7 @@ module Rubik
131
134
  def configured_default_max_output_tokens
132
135
  return nil unless @config
133
136
 
134
- val = if @config.respond_to?(:fetch)
135
- begin
136
- @config.fetch("llm.default_max_output_tokens", nil)
137
- rescue KeyError, TypeError, NoMethodError
138
- nil
139
- end
140
- end
137
+ val = Rubik::Storage::Layout.config_get(@config, "llm.default_max_output_tokens", nil)
141
138
  val = @config.dig("llm", "default_max_output_tokens") || @config["llm.default_max_output_tokens"] if val.nil? && @config.is_a?(Hash)
142
139
  val&.to_i if val.to_i.positive?
143
140
  end
@@ -265,7 +262,14 @@ module Rubik
265
262
  def build_provider_instance(p_name, p_spec, headers, default_model, endpoint)
266
263
  type = p_spec["type"].to_s.downcase
267
264
  api_key_env = p_spec["api_key_env"]
268
- api_key = p_spec["api_key"]
265
+ # Config holds at most a "rubik-secret:" pointer at this point
266
+ # (SecretPolicy routed the plaintext into SecretStore on write);
267
+ # resolve it lazily per build so keyring updates apply on refresh.
268
+ api_key = Rubik::Security::SecretStore.resolve(p_spec["api_key"])
269
+ if api_key.nil? && Rubik::Security::SecretStore.reference?(p_spec["api_key"].to_s)
270
+ Rubik::Log.warn(:llm, "[WARN] stored secret '#{p_spec['api_key'].delete_prefix(Rubik::Security::SecretStore::REFERENCE_PREFIX)}' " \
271
+ "could not be retrieved via #{Rubik::Security::SecretStore.backend_name}")
272
+ end
269
273
 
270
274
  case type
271
275
  when "openai_compatible", "openai"
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "registry"
4
+ require_relative "../storage/layout"
4
5
 
5
6
  module Rubik
6
7
  module LLM
@@ -16,7 +17,7 @@ module Rubik
16
17
  end
17
18
 
18
19
  def route(task_type: :auto, prompt: "")
19
- mode = @config.fetch("router.mode", "auto").to_s.downcase
20
+ mode = Rubik::Storage::Layout.config_get(@config, "router.mode", "auto").to_s.downcase
20
21
  role = determine_role(task_type, prompt, mode)
21
22
  spec = @config.model_spec(role)
22
23
 
@@ -66,10 +67,6 @@ module Rubik
66
67
  chosen.stream_chat(messages, opts, &block)
67
68
  end
68
69
 
69
- def available_roles
70
- [:default, :fast, :advanced]
71
- end
72
-
73
70
  private
74
71
 
75
72
  def determine_role(task_type, prompt, mode)
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module LLM
5
+ # Buffers raw bytes from a stream and emits complete events.
6
+ # Default delimiter is the SSE blank line (\n\n); pass
7
+ # `terminator: "\n"` to parse NDJSON streams (one JSON object per line).
8
+ # One event is a Hash like:
9
+ # { type: "message", data: "json-string" }
10
+ # In SSE mode, multi-line `data:` fields are joined with \n. In NDJSON
11
+ # mode (`terminator: "\n"`), the `type` field is always "message" and
12
+ # the entire line is the `data`. Incomplete events are held in the
13
+ # internal buffer until the next feed.
14
+ class SSEParser
15
+ SSE_EVENT_SEP = /\r\n\r\n|\n\n|\r\n\n|\n\r\n|\r\r/
16
+ SSE_TERMINATOR = "\n\n"
17
+ NDJSON_TERMINATOR = "\n"
18
+
19
+ def initialize(terminator: SSE_TERMINATOR)
20
+ @terminator = terminator
21
+ @sse_mode = @terminator == SSE_TERMINATOR || @terminator == SSE_EVENT_SEP
22
+ @buffer = String.new(encoding: Encoding::UTF_8)
23
+ end
24
+
25
+ # Feed raw bytes; returns an array of complete events parsed from this
26
+ # chunk. Partial events stay in the buffer.
27
+ def feed(chunk_bytes)
28
+ incoming = chunk_bytes.to_s.dup.force_encoding(Encoding::UTF_8)
29
+ incoming = incoming.scrub unless incoming.valid_encoding?
30
+ @buffer << incoming
31
+ drain_complete_events(final: false)
32
+ end
33
+
34
+ # Flush any trailing event (no trailing terminator) and reset.
35
+ def flush
36
+ return [] if @buffer.empty?
37
+
38
+ events = drain_complete_events(final: true)
39
+
40
+ unless @buffer.empty?
41
+ remaining = @buffer.dup
42
+ @buffer = String.new(encoding: Encoding::UTF_8)
43
+ parsed = parse_event(remaining)
44
+ events << parsed if parsed
45
+ end
46
+
47
+ events
48
+ end
49
+
50
+ private
51
+
52
+ # The one drain loop, shared by feed and flush. The SSE and NDJSON
53
+ # branches differ only in how they locate the next terminator. The
54
+ # chunk-boundary \r guard applies only while feeding: if the match
55
+ # ends on \r at the very end of the buffer, wait for the next feed
56
+ # to see if \n follows (e.g. \r\r could become \r\r\n). At flush
57
+ # there is no next feed, so the trailing \r is a real separator.
58
+ def drain_complete_events(final:)
59
+ events = []
60
+ if @sse_mode
61
+ while (m = @buffer.match(SSE_EVENT_SEP))
62
+ break if !final && m.end(0) == @buffer.bytesize && @buffer.end_with?("\r")
63
+
64
+ event_block = @buffer[0...m.begin(0)]
65
+ @buffer = @buffer[m.end(0)..] || String.new(encoding: Encoding::UTF_8)
66
+ parsed = parse_event(event_block)
67
+ events << parsed if parsed
68
+ end
69
+ else
70
+ while (idx = @buffer.index(@terminator))
71
+ event_block = @buffer[0...idx]
72
+ @buffer = @buffer[(idx + @terminator.length)..] || String.new(encoding: Encoding::UTF_8)
73
+ parsed = parse_event(event_block)
74
+ events << parsed if parsed
75
+ end
76
+ end
77
+ events
78
+ end
79
+
80
+ def parse_event(block)
81
+ return nil if block.nil?
82
+ return { type: "message", data: block.to_s.chomp } unless @sse_mode
83
+
84
+ lines = block.to_s.split(/\r\n|\r|\n/)
85
+ return nil if lines.empty?
86
+
87
+ event_type = "message"
88
+ data_lines = []
89
+ has_field = false
90
+
91
+ lines.each do |line|
92
+ next if line.empty?
93
+ next if line.start_with?(":")
94
+
95
+ if (m = line.match(/\Aevent:\s*(.+)\z/))
96
+ event_type = m[1]
97
+ has_field = true
98
+ elsif (m = line.match(/\Adata:\s?(.*)\z/))
99
+ data_lines << m[1]
100
+ has_field = true
101
+ end
102
+ end
103
+
104
+ return nil unless has_field
105
+
106
+ { type: event_type, data: data_lines.join("\n") }
107
+ end
108
+ end
109
+ end
110
+ end
data/lib/rubik/log.rb CHANGED
@@ -36,5 +36,26 @@ module Rubik
36
36
  rescue StandardError
37
37
  nil
38
38
  end
39
+
40
+ # Logger-object shim for classes whose dependency-injected logger kwarg
41
+ # historically defaulted to Logger.new($stderr) built at require time.
42
+ # The forwarder routes through Rubik::Log instead, so the configured
43
+ # RUBIK_LOG_LEVEL filter and the single-line format apply to those
44
+ # defaults too. Injecting a real ::Logger instance still works.
45
+ class Forwarder
46
+ def initialize(subsystem)
47
+ @subsystem = subsystem
48
+ end
49
+
50
+ [:debug, :info, :warn, :error].each do |level_name|
51
+ define_method(level_name) do |message|
52
+ Rubik::Log.emit(level_name, @subsystem, message.to_s)
53
+ end
54
+ end
55
+ end
56
+
57
+ def self.forwarder(subsystem)
58
+ Forwarder.new(subsystem)
59
+ end
39
60
  end
40
61
  end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "monitor"
4
+
5
+ module Rubik
6
+ module LSP
7
+ # Per-language circuit breaker for LSP server processes (extracted from
8
+ # Manager, issue #497). After `MAX_CONSECUTIVE_CRASHES` consecutive
9
+ # crashes the circuit opens and automatic restarts are refused for
10
+ # `CIRCUIT_COOLDOWN_SECONDS`; sustained healthy operation
11
+ # (`CIRCUIT_STABILITY_SECONDS` without a crash) retires the crash
12
+ # history. Modeled on Orchestrator::ToolCircuitBreaker.
13
+ #
14
+ # Thread-safety: self-contained behind its own Monitor (a leaf object —
15
+ # callers may hold their own locks around these calls without deadlock).
16
+ class CircuitBreaker
17
+ MAX_CONSECUTIVE_CRASHES = 3
18
+ CIRCUIT_COOLDOWN_SECONDS = 300
19
+ CIRCUIT_STABILITY_SECONDS = 60
20
+
21
+ # `clock` overrides the monotonic time source (test seam); the Manager
22
+ # wires it to its own `monotonic_now` so existing clock stubs keep
23
+ # working.
24
+ def initialize(clock: nil)
25
+ @monitor = Monitor.new
26
+ @clock = clock
27
+ @crash_stats = {}
28
+ @healthy_since = {}
29
+ end
30
+
31
+ # Live per-language crash-history hash ({ count:, last_crash_at:,
32
+ # circuit_open: }); exposed for status reporting and test observability.
33
+ attr_reader :crash_stats
34
+
35
+ def open?(lang)
36
+ lang_str = lang.to_s.downcase
37
+ @monitor.synchronize do
38
+ retire_if_stable!(lang_str)
39
+
40
+ stat = @crash_stats[lang_str]
41
+ return false unless stat && stat[:circuit_open]
42
+
43
+ if stat[:last_crash_at] && (monotonic_now - stat[:last_crash_at] > CIRCUIT_COOLDOWN_SECONDS)
44
+ stat[:circuit_open] = false
45
+ stat[:count] = 0
46
+ false
47
+ else
48
+ true
49
+ end
50
+ end
51
+ end
52
+
53
+ def record_crash(lang)
54
+ lang_str = lang.to_s.downcase
55
+ @monitor.synchronize do
56
+ healthy_since = @healthy_since.delete(lang_str)
57
+ @crash_stats.delete(lang_str) if healthy_since && monotonic_now - healthy_since >= CIRCUIT_STABILITY_SECONDS
58
+ stat = (@crash_stats[lang_str] ||= { count: 0, last_crash_at: nil, circuit_open: false })
59
+ stat[:count] += 1
60
+ stat[:last_crash_at] = monotonic_now
61
+ stat[:circuit_open] = true if stat[:count] >= MAX_CONSECUTIVE_CRASHES
62
+ end
63
+ end
64
+
65
+ def note_healthy_start(lang)
66
+ lang_str = lang.to_s.downcase
67
+ @monitor.synchronize { @healthy_since[lang_str] = monotonic_now }
68
+ end
69
+
70
+ def reset!(lang)
71
+ lang_str = lang.to_s.downcase
72
+ @monitor.synchronize do
73
+ @crash_stats.delete(lang_str)
74
+ @healthy_since[lang_str] = monotonic_now
75
+ end
76
+ end
77
+
78
+ # Drop crash history for a language that has run stably for
79
+ # CIRCUIT_STABILITY_SECONDS. `open?` folds this in automatically.
80
+ def retire_if_stable!(lang)
81
+ lang_str = lang.to_s.downcase
82
+ @monitor.synchronize do
83
+ healthy_since = @healthy_since[lang_str]
84
+ return unless healthy_since && monotonic_now - healthy_since >= CIRCUIT_STABILITY_SECONDS
85
+
86
+ @crash_stats.delete(lang_str)
87
+ @healthy_since.delete(lang_str)
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def monotonic_now
94
+ @clock ? @clock.call : Process.clock_gettime(Process::CLOCK_MONOTONIC)
95
+ end
96
+ end
97
+ end
98
+ end