rubik 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +117 -1
- data/lib/rubik/ast/extractor.rb +2 -1
- data/lib/rubik/ast/treesitter_extractor.rb +2 -1
- data/lib/rubik/benchmark/vakanuvis_adoption.rb +2 -1
- data/lib/rubik/buffer/manager.rb +3 -3
- data/lib/rubik/config/deep_ops.rb +3 -1
- data/lib/rubik/config/defaults.rb +8 -8
- data/lib/rubik/config/schema.rb +7 -0
- data/lib/rubik/config/secret_policy.rb +20 -10
- data/lib/rubik/config/store.rb +2 -1
- data/lib/rubik/config.rb +3 -3
- data/lib/rubik/context/ignore_rules.rb +193 -0
- data/lib/rubik/context/scanner.rb +46 -161
- data/lib/rubik/llm/anthropic.rb +2 -1
- data/lib/rubik/llm/client.rb +28 -206
- data/lib/rubik/llm/connection_pool.rb +104 -0
- data/lib/rubik/llm/formatter.rb +4 -1
- data/lib/rubik/llm/ollama.rb +2 -1
- data/lib/rubik/llm/openai_compatible.rb +2 -1
- data/lib/rubik/llm/registry.rb +3 -2
- data/lib/rubik/llm/router.rb +6 -1
- data/lib/rubik/llm/sse_parser.rb +113 -0
- data/lib/rubik/lsp/circuit_breaker.rb +98 -0
- data/lib/rubik/lsp/client_provisioner.rb +49 -0
- data/lib/rubik/lsp/manager.rb +55 -88
- data/lib/rubik/mcp/manager.rb +39 -14
- data/lib/rubik/mcp/proxy_tool.rb +2 -2
- data/lib/rubik/mcp/transport/stdio.rb +3 -2
- data/lib/rubik/orchestrator/auto_apply.rb +4 -3
- data/lib/rubik/orchestrator/goal_ledger/ledger.rb +12 -1
- data/lib/rubik/orchestrator/goal_ledger/schema_vocabulary.rb +38 -0
- data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +6 -1
- data/lib/rubik/orchestrator/goal_ledger.rb +1 -0
- data/lib/rubik/orchestrator/goal_state.rb +6 -2
- data/lib/rubik/orchestrator/loop.rb +30 -6
- data/lib/rubik/orchestrator/lsp_feedback.rb +1 -1
- data/lib/rubik/orchestrator/provider_resolver.rb +12 -0
- data/lib/rubik/orchestrator/step_executor.rb +2 -1
- data/lib/rubik/orchestrator/subagent/notices.rb +2 -2
- data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -0
- data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
- data/lib/rubik/platform/filesystem.rb +2 -2
- data/lib/rubik/platform/io_drain.rb +3 -1
- data/lib/rubik/plugin/storage.rb +8 -8
- data/lib/rubik/repair/loop.rb +1 -1
- data/lib/rubik/repl/config_wiring.rb +5 -3
- data/lib/rubik/repl/input_execution.rb +147 -7
- data/lib/rubik/repl/lifecycle.rb +1 -1
- data/lib/rubik/repl/shell.rb +9 -1
- data/lib/rubik/repl/tee_io.rb +2 -1
- data/lib/rubik/runtime/ast_service.rb +2 -1
- data/lib/rubik/runtime/fiber.rb +1 -0
- data/lib/rubik/runtime/manager.rb +4 -1
- data/lib/rubik/runtime/overlay_storage.rb +17 -17
- data/lib/rubik/runtime/task_manager.rb +4 -4
- data/lib/rubik/sandbox/driver/base.rb +7 -0
- data/lib/rubik/sandbox/driver/docker.rb +1 -4
- data/lib/rubik/sandbox/driver/podman.rb +1 -0
- data/lib/rubik/sandbox/runner.rb +6 -5
- data/lib/rubik/security/audit_log.rb +2 -1
- data/lib/rubik/security/classifier/classifier.rb +2 -0
- data/lib/rubik/security/command_gate.rb +11 -152
- data/lib/rubik/security/guard.rb +1 -1
- data/lib/rubik/security/secret_store.rb +32 -3
- data/lib/rubik/security/segment_report.rb +161 -0
- data/lib/rubik/session/recorder.rb +2 -1
- data/lib/rubik/skills/manager.rb +2 -1
- data/lib/rubik/storage/store.rb +4 -1
- data/lib/rubik/storage/transcript_indexer.rb +3 -2
- data/lib/rubik/storage/vakanuvis.rb +2 -1
- data/lib/rubik/tools/builtins/goal_ledger.rb +12 -1
- data/lib/rubik/tools/builtins/synthesis.rb +3 -3
- data/lib/rubik/tools/preflight_runner.rb +2 -1
- data/lib/rubik/tools/registry.rb +36 -16
- data/lib/rubik/tools/uri_resolver.rb +3 -2
- data/lib/rubik/ui/capability.rb +7 -4
- data/lib/rubik/ui/markdown_stream.rb +1 -0
- data/lib/rubik/ui/renderer.rb +4 -2
- data/lib/rubik/version.rb +1 -1
- data/lib/rubik.rb +3 -3
- metadata +8 -1
|
@@ -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
|
data/lib/rubik/llm/formatter.rb
CHANGED
|
@@ -326,7 +326,10 @@ module Rubik
|
|
|
326
326
|
chunk = Chunk.new(usage: usage, raw: parsed)
|
|
327
327
|
when "message_stop"
|
|
328
328
|
chunk = Chunk.new(done: true, raw: parsed)
|
|
329
|
-
when "message_start"
|
|
329
|
+
when "message_start"
|
|
330
|
+
usage = parsed.dig("message", "usage")
|
|
331
|
+
chunk = Chunk.new(usage: usage, raw: parsed)
|
|
332
|
+
when "content_block_start", "content_block_stop"
|
|
330
333
|
chunk = Chunk.new(raw: parsed)
|
|
331
334
|
end
|
|
332
335
|
|
data/lib/rubik/llm/ollama.rb
CHANGED
data/lib/rubik/llm/registry.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../log"
|
|
3
4
|
require_relative "openai_compatible"
|
|
4
5
|
require_relative "../security/secret_store"
|
|
5
6
|
require_relative "../storage/layout"
|
|
@@ -266,8 +267,8 @@ module Rubik
|
|
|
266
267
|
# resolve it lazily per build so keyring updates apply on refresh.
|
|
267
268
|
api_key = Rubik::Security::SecretStore.resolve(p_spec["api_key"])
|
|
268
269
|
if api_key.nil? && Rubik::Security::SecretStore.reference?(p_spec["api_key"].to_s)
|
|
269
|
-
warn "[WARN] stored secret '#{p_spec['api_key'].delete_prefix(Rubik::Security::SecretStore::REFERENCE_PREFIX)}' " \
|
|
270
|
-
|
|
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}")
|
|
271
272
|
end
|
|
272
273
|
|
|
273
274
|
case type
|
data/lib/rubik/llm/router.rb
CHANGED
|
@@ -45,7 +45,12 @@ module Rubik
|
|
|
45
45
|
# explicit `provider:` opt) and delegates to its `#stream_chat`.
|
|
46
46
|
# Yields Chunks. Without a block, returns an Enumerator.
|
|
47
47
|
def stream_chat(messages, task_type: :auto, prompt: "", provider: nil, **opts, &block)
|
|
48
|
-
chosen_route =
|
|
48
|
+
chosen_route = if provider
|
|
49
|
+
resolved = provider.respond_to?(:stream_chat) ? provider : @registry.get(provider)
|
|
50
|
+
{ provider: resolved }
|
|
51
|
+
else
|
|
52
|
+
route(task_type: task_type, prompt: prompt)
|
|
53
|
+
end
|
|
49
54
|
chosen = chosen_route[:provider]
|
|
50
55
|
raise ClientError, "No provider available for streaming" unless chosen
|
|
51
56
|
|
|
@@ -0,0 +1,113 @@
|
|
|
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_EVENT_SEP_BIN = Regexp.new(SSE_EVENT_SEP.source.b, Regexp::NOENCODING)
|
|
17
|
+
SSE_TERMINATOR = "\n\n"
|
|
18
|
+
NDJSON_TERMINATOR = "\n"
|
|
19
|
+
|
|
20
|
+
def initialize(terminator: SSE_TERMINATOR)
|
|
21
|
+
@terminator = terminator
|
|
22
|
+
@terminator_bin = terminator.is_a?(String) ? terminator.b : terminator
|
|
23
|
+
@sse_mode = @terminator == SSE_TERMINATOR || @terminator == SSE_EVENT_SEP
|
|
24
|
+
@buffer = String.new(encoding: Encoding::ASCII_8BIT)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Feed raw bytes; returns an array of complete events parsed from this
|
|
28
|
+
# chunk. Partial events stay in the buffer.
|
|
29
|
+
def feed(chunk_bytes)
|
|
30
|
+
@buffer << chunk_bytes.to_s.b
|
|
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::ASCII_8BIT)
|
|
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_BIN))
|
|
62
|
+
break if !final && m.end(0) == @buffer.bytesize && @buffer.end_with?("\r".b)
|
|
63
|
+
|
|
64
|
+
event_block = @buffer.byteslice(0, m.begin(0))
|
|
65
|
+
@buffer = @buffer.byteslice(m.end(0)..-1) || String.new(encoding: Encoding::ASCII_8BIT)
|
|
66
|
+
parsed = parse_event(event_block)
|
|
67
|
+
events << parsed if parsed
|
|
68
|
+
end
|
|
69
|
+
else
|
|
70
|
+
while (idx = @buffer.index(@terminator_bin))
|
|
71
|
+
event_block = @buffer.byteslice(0, idx)
|
|
72
|
+
@buffer = @buffer.byteslice((idx + @terminator_bin.bytesize)..-1) || String.new(encoding: Encoding::ASCII_8BIT)
|
|
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
|
+
|
|
83
|
+
utf8 = block.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
84
|
+
utf8 = utf8.scrub unless utf8.valid_encoding?
|
|
85
|
+
return { type: "message", data: utf8.chomp } unless @sse_mode
|
|
86
|
+
|
|
87
|
+
lines = utf8.split(/\r\n|\r|\n/)
|
|
88
|
+
return nil if lines.empty?
|
|
89
|
+
|
|
90
|
+
event_type = "message"
|
|
91
|
+
data_lines = []
|
|
92
|
+
has_field = false
|
|
93
|
+
|
|
94
|
+
lines.each do |line|
|
|
95
|
+
next if line.empty?
|
|
96
|
+
next if line.start_with?(":")
|
|
97
|
+
|
|
98
|
+
if (m = line.match(/\Aevent:\s*(.+)\z/))
|
|
99
|
+
event_type = m[1]
|
|
100
|
+
has_field = true
|
|
101
|
+
elsif (m = line.match(/\Adata:\s?(.*)\z/))
|
|
102
|
+
data_lines << m[1]
|
|
103
|
+
has_field = true
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
return nil unless has_field
|
|
108
|
+
|
|
109
|
+
{ type: event_type, data: data_lines.join("\n") }
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
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
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "client"
|
|
4
|
+
require_relative "server_command"
|
|
5
|
+
require_relative "../storage/layout"
|
|
6
|
+
require_relative "../runtime/discovery"
|
|
7
|
+
require_relative "../runtime/diagnostic_engine"
|
|
8
|
+
|
|
9
|
+
module Rubik
|
|
10
|
+
module LSP
|
|
11
|
+
# Client provisioning policy for LSP servers (extracted from Manager,
|
|
12
|
+
# issue #497): resolve the configured server command, build a Client,
|
|
13
|
+
# fall back to Runtime::Discovery probing when the configured binary is
|
|
14
|
+
# unavailable, and diagnose the failure when nothing usable is found.
|
|
15
|
+
#
|
|
16
|
+
# Plain, lock-free collaborator — the Manager owns all locking and
|
|
17
|
+
# invokes `provision` while holding its monitor.
|
|
18
|
+
class ClientProvisioner
|
|
19
|
+
def initialize(config:, root_dir:)
|
|
20
|
+
@config = config
|
|
21
|
+
@root_dir = root_dir
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Resolve a client for `lang`. Returns { client:, diagnosis: } —
|
|
25
|
+
# `client` is always a Client instance (usable or not); `diagnosis` is
|
|
26
|
+
# a Runtime::ExecutionFailure when no server binary could be found,
|
|
27
|
+
# nil when the client is available.
|
|
28
|
+
def provision(lang, existing: nil)
|
|
29
|
+
configured_cmd = Rubik::Storage::Layout.config_get(@config, "lsp.servers.#{lang}", nil) || Client::SERVERS[lang]
|
|
30
|
+
client = existing || Client.new(lang, root_dir: @root_dir, server_cmd: configured_cmd)
|
|
31
|
+
|
|
32
|
+
# Fallback probing if the configured command binary is not available
|
|
33
|
+
unless client.available?
|
|
34
|
+
probed_cmd = Runtime::Discovery.probe_lsp(lang, root_dir: @root_dir, custom_cmd: configured_cmd)
|
|
35
|
+
client = Client.new(lang, root_dir: @root_dir, server_cmd: probed_cmd) if probed_cmd && !probed_cmd.eql?(client.server_cmd)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
return { client: client, diagnosis: nil } if client.available?
|
|
39
|
+
|
|
40
|
+
failure = Runtime::DiagnosticEngine.diagnose(
|
|
41
|
+
cmd: ServerCommand.display(client.server_cmd),
|
|
42
|
+
exit_code: 127,
|
|
43
|
+
stderr: "Server binary '#{ServerCommand.display(client.server_cmd)}' not found in PATH"
|
|
44
|
+
)
|
|
45
|
+
{ client: client, diagnosis: failure }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/rubik/lsp/manager.rb
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
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"
|
|
5
8
|
require_relative "../storage/layout"
|
|
6
9
|
require_relative "../runtime/discovery"
|
|
7
10
|
require_relative "../runtime/diagnostic_engine"
|
|
@@ -9,6 +12,10 @@ require_relative "../runtime/diagnostic_engine"
|
|
|
9
12
|
module Rubik
|
|
10
13
|
module LSP
|
|
11
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.
|
|
12
19
|
EXTENSION_MAP = {
|
|
13
20
|
".go" => "go",
|
|
14
21
|
".rs" => "rust",
|
|
@@ -39,11 +46,13 @@ module Rubik
|
|
|
39
46
|
".bash" => "shellscript"
|
|
40
47
|
}.freeze
|
|
41
48
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
|
45
54
|
|
|
46
|
-
attr_reader :config, :root_dir, :clients
|
|
55
|
+
attr_reader :config, :root_dir, :clients
|
|
47
56
|
# :nodoc: test observability door — read-only view of the per-language
|
|
48
57
|
# disposable-registration tracking (issue #450). No production consumer.
|
|
49
58
|
attr_reader :registered_disposable_langs
|
|
@@ -55,14 +64,19 @@ module Rubik
|
|
|
55
64
|
@runtime_manager = runtime_manager
|
|
56
65
|
@context = context || runtime_manager&.context
|
|
57
66
|
@clients = {}
|
|
58
|
-
@
|
|
59
|
-
@
|
|
67
|
+
@breaker = CircuitBreaker.new(clock: -> { monotonic_now })
|
|
68
|
+
@provisioner = ClientProvisioner.new(config: @config, root_dir: @root_dir)
|
|
60
69
|
@recovery_errors = {}
|
|
61
70
|
@registered_disposable_langs = {}
|
|
62
71
|
@client_inverses = {}
|
|
63
72
|
@monitor = Monitor.new
|
|
64
73
|
end
|
|
65
74
|
|
|
75
|
+
# Per-language crash history (live hash from the CircuitBreaker).
|
|
76
|
+
def crash_stats
|
|
77
|
+
@breaker.crash_stats
|
|
78
|
+
end
|
|
79
|
+
|
|
66
80
|
def enabled?
|
|
67
81
|
Rubik::Storage::Layout.config_get(@config, "lsp.enabled", false) == true
|
|
68
82
|
end
|
|
@@ -81,46 +95,18 @@ module Rubik
|
|
|
81
95
|
DOCUMENT_LANGUAGE_ID_MAP[ext] || detect_language(file_path) || "plaintext"
|
|
82
96
|
end
|
|
83
97
|
|
|
98
|
+
# Circuit-breaker state machine (extracted to CircuitBreaker, #497);
|
|
99
|
+
# Manager keeps the public delegating API.
|
|
84
100
|
def circuit_open?(lang)
|
|
85
|
-
@
|
|
86
|
-
stat = @crash_stats[lang.to_s.downcase]
|
|
87
|
-
healthy_since = @healthy_since[lang.to_s.downcase]
|
|
88
|
-
if healthy_since && monotonic_now - healthy_since >= CIRCUIT_STABILITY_SECONDS
|
|
89
|
-
@crash_stats.delete(lang.to_s.downcase)
|
|
90
|
-
@healthy_since.delete(lang.to_s.downcase)
|
|
91
|
-
return false
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
return false unless stat && stat[:circuit_open]
|
|
95
|
-
|
|
96
|
-
if stat[:last_crash_at] && (monotonic_now - stat[:last_crash_at] > CIRCUIT_COOLDOWN_SECONDS)
|
|
97
|
-
stat[:circuit_open] = false
|
|
98
|
-
stat[:count] = 0
|
|
99
|
-
false
|
|
100
|
-
else
|
|
101
|
-
true
|
|
102
|
-
end
|
|
103
|
-
end
|
|
101
|
+
@breaker.open?(lang)
|
|
104
102
|
end
|
|
105
103
|
|
|
106
104
|
def record_crash(lang)
|
|
107
|
-
@
|
|
108
|
-
lang_str = lang.to_s.downcase
|
|
109
|
-
healthy_since = @healthy_since.delete(lang_str)
|
|
110
|
-
@crash_stats.delete(lang_str) if healthy_since && monotonic_now - healthy_since >= CIRCUIT_STABILITY_SECONDS
|
|
111
|
-
stat = (@crash_stats[lang_str] ||= { count: 0, last_crash_at: nil, circuit_open: false })
|
|
112
|
-
stat[:count] += 1
|
|
113
|
-
stat[:last_crash_at] = monotonic_now
|
|
114
|
-
stat[:circuit_open] = true if stat[:count] >= MAX_CONSECUTIVE_CRASHES
|
|
115
|
-
end
|
|
105
|
+
@breaker.record_crash(lang)
|
|
116
106
|
end
|
|
117
107
|
|
|
118
108
|
def reset_circuit!(lang)
|
|
119
|
-
@
|
|
120
|
-
lang_str = lang.to_s.downcase
|
|
121
|
-
@crash_stats.delete(lang_str)
|
|
122
|
-
@healthy_since[lang_str] = monotonic_now
|
|
123
|
-
end
|
|
109
|
+
@breaker.reset!(lang)
|
|
124
110
|
end
|
|
125
111
|
|
|
126
112
|
def ensure_healthy!(language_or_client)
|
|
@@ -145,21 +131,10 @@ module Rubik
|
|
|
145
131
|
def start_client(lang)
|
|
146
132
|
lang_str = lang.to_s.downcase
|
|
147
133
|
@monitor.synchronize do
|
|
148
|
-
|
|
149
|
-
client =
|
|
150
|
-
|
|
151
|
-
# 1. Fallback probing if configured command binary is not available
|
|
152
|
-
unless client.available?
|
|
153
|
-
probed_cmd = Runtime::Discovery.probe_lsp(lang_str, root_dir: @root_dir, custom_cmd: configured_cmd)
|
|
154
|
-
client = Client.new(lang_str, root_dir: @root_dir, server_cmd: probed_cmd) if probed_cmd && probed_cmd != client.server_cmd
|
|
155
|
-
end
|
|
134
|
+
outcome = @provisioner.provision(lang_str, existing: @clients[lang_str])
|
|
135
|
+
client = outcome[:client]
|
|
156
136
|
|
|
157
|
-
|
|
158
|
-
failure = Runtime::DiagnosticEngine.diagnose(
|
|
159
|
-
cmd: ServerCommand.display(client.server_cmd),
|
|
160
|
-
exit_code: 127,
|
|
161
|
-
stderr: "Server binary '#{ServerCommand.display(client.server_cmd)}' not found in PATH"
|
|
162
|
-
)
|
|
137
|
+
if (failure = outcome[:diagnosis])
|
|
163
138
|
return {
|
|
164
139
|
ok: false,
|
|
165
140
|
error: "Server binary '#{ServerCommand.display(client.server_cmd)}' not found",
|
|
@@ -179,10 +154,7 @@ module Rubik
|
|
|
179
154
|
register_client_unlocked(lang_str, client)
|
|
180
155
|
{ ok: true, message: "Started LSP server for '#{lang_str}' (#{ServerCommand.display(client.server_cmd)})" }
|
|
181
156
|
else
|
|
182
|
-
|
|
183
|
-
stop_client_safely(client)
|
|
184
|
-
@clients.delete(lang_str)
|
|
185
|
-
unregister_client_unlocked(lang_str)
|
|
157
|
+
restart_failed(lang_str, client, drop_stored: true)
|
|
186
158
|
failure = Runtime::DiagnosticEngine.diagnose(
|
|
187
159
|
cmd: ServerCommand.display(client.server_cmd),
|
|
188
160
|
exit_code: 1,
|
|
@@ -392,19 +364,14 @@ module Rubik
|
|
|
392
364
|
# Caller MUST hold @monitor. Dead-client removal and replacement startup
|
|
393
365
|
# happen in one critical section so concurrent requests share one client.
|
|
394
366
|
def client_for_unlocked(lang, preferred = nil)
|
|
395
|
-
|
|
367
|
+
@breaker.open?(lang) # also retires crash history after sustained healthy operation
|
|
396
368
|
stored = @clients[lang]
|
|
397
369
|
client = stored || preferred
|
|
398
370
|
recovering = false
|
|
399
371
|
|
|
400
372
|
if client && client_dead?(client)
|
|
401
373
|
recovering = true
|
|
402
|
-
|
|
403
|
-
stop_client_safely(client)
|
|
404
|
-
if stored.equal?(client)
|
|
405
|
-
@clients.delete(lang)
|
|
406
|
-
unregister_client_unlocked(lang)
|
|
407
|
-
end
|
|
374
|
+
restart_failed(lang, client, drop_stored: true, unregister: stored.equal?(client))
|
|
408
375
|
client = nil
|
|
409
376
|
end
|
|
410
377
|
|
|
@@ -420,23 +387,14 @@ module Rubik
|
|
|
420
387
|
return nil unless client || enabled?
|
|
421
388
|
|
|
422
389
|
if client.nil?
|
|
423
|
-
|
|
424
|
-
client =
|
|
425
|
-
unless client.available?
|
|
426
|
-
probed_cmd = Runtime::Discovery.probe_lsp(lang, root_dir: @root_dir, custom_cmd: configured_cmd)
|
|
427
|
-
client = Client.new(lang, root_dir: @root_dir, server_cmd: probed_cmd) if probed_cmd
|
|
428
|
-
end
|
|
390
|
+
outcome = @provisioner.provision(lang)
|
|
391
|
+
client = outcome[:client]
|
|
429
392
|
|
|
430
393
|
if client.available? && Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true) && !circuit_open?(lang)
|
|
431
394
|
if client.start
|
|
432
|
-
|
|
433
|
-
@recovery_errors.delete(lang)
|
|
434
|
-
register_client_unlocked(lang, client)
|
|
395
|
+
start_succeeded(lang, client)
|
|
435
396
|
else
|
|
436
|
-
|
|
437
|
-
stop_client_safely(client)
|
|
438
|
-
unregister_client_unlocked(lang)
|
|
439
|
-
@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)
|
|
440
398
|
end
|
|
441
399
|
elsif recovering
|
|
442
400
|
@recovery_errors[lang] = if circuit_open?(lang)
|
|
@@ -451,20 +409,33 @@ module Rubik
|
|
|
451
409
|
elsif !client.running && Rubik::Storage::Layout.config_get(@config, "lsp.auto_start", true) &&
|
|
452
410
|
client.available? && !circuit_open?(lang)
|
|
453
411
|
if client.start
|
|
454
|
-
|
|
455
|
-
@recovery_errors.delete(lang)
|
|
456
|
-
register_client_unlocked(lang, client)
|
|
412
|
+
start_succeeded(lang, client)
|
|
457
413
|
else
|
|
458
|
-
|
|
459
|
-
stop_client_safely(client)
|
|
460
|
-
unregister_client_unlocked(lang)
|
|
461
|
-
@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)
|
|
462
415
|
end
|
|
463
416
|
end
|
|
464
417
|
|
|
465
418
|
client
|
|
466
419
|
end
|
|
467
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
|
+
|
|
468
439
|
def client_dead?(client)
|
|
469
440
|
(client.running && client.respond_to?(:alive?) && !client.alive?) ||
|
|
470
441
|
(client.respond_to?(:crashed?) && client.crashed?)
|
|
@@ -507,10 +478,6 @@ module Rubik
|
|
|
507
478
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
508
479
|
end
|
|
509
480
|
|
|
510
|
-
def note_healthy_start(lang)
|
|
511
|
-
@healthy_since[lang.to_s.downcase] = monotonic_now
|
|
512
|
-
end
|
|
513
|
-
|
|
514
481
|
def inactive_client_error(file_path, client)
|
|
515
482
|
lang = client&.language || detect_language(file_path)
|
|
516
483
|
@monitor.synchronize do
|