rubik 0.15.2 → 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.
- checksums.yaml +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +40 -3
- data/lib/rubik/ast/cache_store.rb +22 -4
- data/lib/rubik/ast/extractor.rb +1 -4
- data/lib/rubik/ast/ruby_extractor.rb +23 -1
- data/lib/rubik/ast/symbol.rb +9 -4
- data/lib/rubik/ast/treesitter/python_walker.rb +5 -4
- data/lib/rubik/ast/treesitter_extractor.rb +10 -3
- data/lib/rubik/buffer/diff.rb +34 -2
- data/lib/rubik/buffer/manager.rb +18 -15
- data/lib/rubik/buffer/path_validator.rb +18 -2
- data/lib/rubik/buffer/snapshot.rb +144 -9
- data/lib/rubik/concurrency/timers.rb +7 -0
- data/lib/rubik/config/scoped.rb +18 -7
- data/lib/rubik/config/secret_policy.rb +22 -1
- data/lib/rubik/config.rb +24 -26
- data/lib/rubik/context/ignore_rules.rb +7 -2
- data/lib/rubik/diagnostics/parser.rb +19 -8
- data/lib/rubik/kernel/dispatcher.rb +11 -5
- data/lib/rubik/llm/client.rb +24 -3
- data/lib/rubik/llm/connection_pool.rb +51 -30
- data/lib/rubik/llm/message_sanitizer.rb +20 -1
- data/lib/rubik/llm/sse_parser.rb +10 -1
- data/lib/rubik/lsp/transport.rb +61 -1
- data/lib/rubik/mcp/transport/stdio.rb +139 -122
- data/lib/rubik/orchestrator/activity_log.rb +3 -2
- data/lib/rubik/orchestrator/subagent/completion.rb +18 -8
- data/lib/rubik/platform/process.rb +74 -19
- data/lib/rubik/plugin/manager.rb +5 -6
- data/lib/rubik/repl/service_catalog.rb +1 -1
- data/lib/rubik/repl/terminal_guard.rb +29 -23
- data/lib/rubik/repl/turn_recorder.rb +35 -11
- data/lib/rubik/sandbox/driver/process.rb +11 -2
- data/lib/rubik/security/blocked_guidance.rb +4 -2
- data/lib/rubik/security/command_gate.rb +27 -14
- data/lib/rubik/security/errors.rb +13 -0
- data/lib/rubik/security/guard.rb +11 -2
- data/lib/rubik/security/path_policy.rb +9 -6
- data/lib/rubik/storage/store.rb +31 -11
- data/lib/rubik/tools/builtins/exec.rb +5 -1
- data/lib/rubik/tools/builtins/files.rb +2 -0
- data/lib/rubik/tools/uri_resolver.rb +23 -3
- data/lib/rubik/version.rb +1 -1
- 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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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(
|
|
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
|
data/lib/rubik/llm/sse_parser.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
|
data/lib/rubik/lsp/transport.rb
CHANGED
|
@@ -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 =
|
|
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.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "open3"
|
|
4
|
+
require "monitor"
|
|
4
5
|
require "json"
|
|
5
6
|
require "shellwords"
|
|
6
7
|
require_relative "../errors"
|
|
@@ -42,6 +43,9 @@ module Rubik
|
|
|
42
43
|
@stderr_mutex = Mutex.new
|
|
43
44
|
@stderr_tail = []
|
|
44
45
|
@mutex = Mutex.new
|
|
46
|
+
# Serializes the whole check-spawn-assign lifecycle; close takes the
|
|
47
|
+
# same monitor so a concurrent teardown cannot race an open.
|
|
48
|
+
@lifecycle_monitor = Monitor.new
|
|
45
49
|
# Writes are serialized on their OWN mutex, never @mutex: @mutex is
|
|
46
50
|
# also taken by close/wait_for_response/the reader, so a stalled
|
|
47
51
|
# write holding it would wedge teardown and response delivery.
|
|
@@ -50,72 +54,83 @@ module Rubik
|
|
|
50
54
|
@responses = {}
|
|
51
55
|
@closed = false
|
|
52
56
|
@generation = 0
|
|
57
|
+
@child_identity = nil
|
|
53
58
|
@pending_requests = Set.new
|
|
54
59
|
end
|
|
55
60
|
|
|
56
61
|
def open
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
@lifecycle_monitor.synchronize do
|
|
63
|
+
return if running?
|
|
64
|
+
|
|
65
|
+
# A previous incarnation whose child died on its own (the reader's
|
|
66
|
+
# ensure cleared @running) still holds pipe FDs and an unreaped
|
|
67
|
+
# wait thread; re-spawning over them leaks both. close is
|
|
68
|
+
# idempotent and a no-op when nothing lingers.
|
|
69
|
+
close if @wait_thr || @reader_thr || @stderr_thr
|
|
70
|
+
|
|
71
|
+
cmd_parts = if @args.empty? && @command.is_a?(String)
|
|
72
|
+
begin
|
|
73
|
+
Shellwords.split(@command)
|
|
74
|
+
rescue ArgumentError => e
|
|
75
|
+
raise ConnectionError, "Invalid command syntax: #{e.message}"
|
|
76
|
+
end
|
|
77
|
+
else
|
|
78
|
+
[@command.to_s] + @args
|
|
70
79
|
end
|
|
71
|
-
else
|
|
72
|
-
[@command.to_s] + @args
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
bin = cmd_parts.first
|
|
76
|
-
raise ConnectionError, "MCP command cannot be empty" if bin.nil? || bin.empty?
|
|
77
|
-
|
|
78
|
-
# Red-Team Defense: Verify command safety against destructive operations.
|
|
79
|
-
# Route the MCP spawn command through the same Guard#verify_command_safety!
|
|
80
|
-
# call that RunCommandTool uses — the narrow DANGEROUS_COMMAND_PATTERNS
|
|
81
|
-
# subset below was a bypass: it misses many dangerous commands the
|
|
82
|
-
# classifier flags. Fails closed on any dangerous/unmanaged command.
|
|
83
|
-
full_cmd = Shellwords.shelljoin(cmd_parts)
|
|
84
|
-
begin
|
|
85
|
-
@guard.verify_command_safety!(full_cmd)
|
|
86
|
-
rescue Rubik::Security::BlockedWriteError => e
|
|
87
|
-
# No manual audit here: verify_command_safety! already logged
|
|
88
|
-
# "blocked_command" with the TRUE classifier reason before raising
|
|
89
|
-
# — a second hardcoded entry would double-log and mislabel it.
|
|
90
|
-
raise ConnectionError, "Security blocked MCP process: #{e.message}"
|
|
91
|
-
end
|
|
92
80
|
|
|
93
|
-
|
|
94
|
-
|
|
81
|
+
bin = cmd_parts.first
|
|
82
|
+
raise ConnectionError, "MCP command cannot be empty" if bin.nil? || bin.empty?
|
|
95
83
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
84
|
+
# Red-Team Defense: Verify command safety against destructive operations.
|
|
85
|
+
# Route the MCP spawn command through the same Guard#verify_command_safety!
|
|
86
|
+
# call that RunCommandTool uses — the narrow DANGEROUS_COMMAND_PATTERNS
|
|
87
|
+
# subset below was a bypass: it misses many dangerous commands the
|
|
88
|
+
# classifier flags. Fails closed on any dangerous/unmanaged command.
|
|
89
|
+
full_cmd = Shellwords.shelljoin(cmd_parts)
|
|
90
|
+
begin
|
|
91
|
+
@guard.verify_command_safety!(full_cmd)
|
|
92
|
+
rescue Rubik::Security::BlockedWriteError => e
|
|
93
|
+
# No manual audit here: verify_command_safety! already logged
|
|
94
|
+
# "blocked_command" with the TRUE classifier reason before raising
|
|
95
|
+
# — a second hardcoded entry would double-log and mislabel it.
|
|
96
|
+
raise ConnectionError, "Security blocked MCP process: #{e.message}"
|
|
97
|
+
end
|
|
105
98
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
99
|
+
resolved_bin = Rubik::Platform.which(bin)
|
|
100
|
+
cmd_parts[0] = resolved_bin if resolved_bin
|
|
101
|
+
|
|
102
|
+
# Match the LSP client's spawn hygiene: the host's bundler/Ruby
|
|
103
|
+
# runtime variables are unset for the child, so an MCP server never
|
|
104
|
+
# inherits a foreign BUNDLE_GEMFILE/RUBYOPT. Explicitly configured
|
|
105
|
+
# env: entries merge on top and win.
|
|
106
|
+
begin
|
|
107
|
+
@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(spawn_env, *cmd_parts, chdir: @root_dir)
|
|
108
|
+
rescue SystemCallError => e
|
|
109
|
+
raise ConnectionError, "Failed to spawn MCP process '#{bin}': #{e.message}"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Snapshot the child's identity at spawn so teardown cannot signal a
|
|
113
|
+
# recycled PID (the guard in Platform.kill_tree needs it).
|
|
114
|
+
@child_identity = begin
|
|
115
|
+
Rubik::Platform.send(:process_identity, @wait_thr.pid)
|
|
116
|
+
rescue StandardError
|
|
117
|
+
nil
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
@mutex.synchronize do
|
|
121
|
+
@running = true
|
|
122
|
+
@closed = false
|
|
123
|
+
@generation += 1
|
|
124
|
+
@req_id = 0
|
|
125
|
+
@responses.clear
|
|
126
|
+
@pending_requests.clear
|
|
127
|
+
end
|
|
128
|
+
@stderr_mutex.synchronize { @stderr_tail.clear }
|
|
115
129
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
130
|
+
start_stderr_thread
|
|
131
|
+
start_reader_thread
|
|
132
|
+
self
|
|
133
|
+
end
|
|
119
134
|
end
|
|
120
135
|
alias start open
|
|
121
136
|
|
|
@@ -162,87 +177,89 @@ module Rubik
|
|
|
162
177
|
end
|
|
163
178
|
|
|
164
179
|
def close
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
child_died = !@running
|
|
176
|
-
@closed = true
|
|
177
|
-
@running = false
|
|
178
|
-
end
|
|
180
|
+
@lifecycle_monitor.synchronize do
|
|
181
|
+
# Never take @write_mutex here: a writer may be stalled in
|
|
182
|
+
# write_until, and killing the child (below) is precisely what
|
|
183
|
+
# unblocks it. Waiting for the write lock would deadlock teardown.
|
|
184
|
+
child_died = false
|
|
185
|
+
# Capture before @wait_thr is nil'd below: the child-died diagnostic
|
|
186
|
+
# must still report WHICH server process failed to start.
|
|
187
|
+
exited_pid = @wait_thr&.pid
|
|
188
|
+
@mutex.synchronize do
|
|
189
|
+
return if @closed
|
|
179
190
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
end
|
|
191
|
+
child_died = !@running
|
|
192
|
+
@closed = true
|
|
193
|
+
@running = false
|
|
194
|
+
end
|
|
185
195
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
196
|
+
begin
|
|
197
|
+
@reader_thr&.kill
|
|
198
|
+
rescue StandardError
|
|
199
|
+
nil
|
|
200
|
+
end
|
|
191
201
|
|
|
192
|
-
if @wait_thr&.alive?
|
|
193
|
-
pid_to_kill = @wait_thr.pid
|
|
194
|
-
Rubik::Platform.kill_tree(pid_to_kill)
|
|
195
202
|
begin
|
|
196
|
-
@
|
|
203
|
+
@stderr_thr&.kill
|
|
197
204
|
rescue StandardError
|
|
198
205
|
nil
|
|
199
206
|
end
|
|
200
207
|
|
|
201
|
-
if @wait_thr
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
+
if @wait_thr&.alive?
|
|
209
|
+
pid_to_kill = @wait_thr.pid
|
|
210
|
+
Rubik::Platform.kill_tree(pid_to_kill, identity: @child_identity)
|
|
211
|
+
begin
|
|
212
|
+
@wait_thr.join(0.5)
|
|
213
|
+
rescue StandardError
|
|
214
|
+
nil
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
if @wait_thr.alive?
|
|
218
|
+
abandoned_waiter = @wait_thr
|
|
219
|
+
Thread.new do
|
|
220
|
+
begin
|
|
221
|
+
abandoned_waiter.join(2.5)
|
|
222
|
+
rescue StandardError
|
|
223
|
+
nil
|
|
224
|
+
end
|
|
225
|
+
Rubik::Log.warn(:mcp, "abandoned child process pid #{pid_to_kill} did not exit within reaper window") if abandoned_waiter.alive?
|
|
208
226
|
end
|
|
209
|
-
Rubik::Log.warn(:mcp, "abandoned child process pid #{pid_to_kill} did not exit within reaper window") if abandoned_waiter.alive?
|
|
210
227
|
end
|
|
211
228
|
end
|
|
212
|
-
|
|
213
|
-
@wait_thr = nil
|
|
229
|
+
@wait_thr = nil
|
|
214
230
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
231
|
+
[@stdin, @stdout, @stderr].each do |io|
|
|
232
|
+
io&.close unless io&.closed?
|
|
233
|
+
rescue StandardError
|
|
234
|
+
nil
|
|
235
|
+
end
|
|
220
236
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
237
|
+
begin
|
|
238
|
+
@reader_thr&.join(0.5)
|
|
239
|
+
rescue StandardError
|
|
240
|
+
nil
|
|
241
|
+
ensure
|
|
242
|
+
@reader_thr = nil
|
|
243
|
+
end
|
|
228
244
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
245
|
+
begin
|
|
246
|
+
@stderr_thr&.join(0.5)
|
|
247
|
+
rescue StandardError
|
|
248
|
+
nil
|
|
249
|
+
ensure
|
|
250
|
+
@stderr_thr = nil
|
|
251
|
+
end
|
|
236
252
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
253
|
+
if child_died
|
|
254
|
+
hint = stderr_tail.last(5).join(" | ")[0, 512]
|
|
255
|
+
Rubik::Log.warn(:mcp, "MCP child process (pid #{exited_pid}) had already exited; recent stderr: #{hint}") unless hint.empty?
|
|
256
|
+
end
|
|
241
257
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
258
|
+
@mutex.synchronize do
|
|
259
|
+
@responses.clear
|
|
260
|
+
@pending_requests.clear
|
|
261
|
+
@cv.broadcast
|
|
262
|
+
end
|
|
246
263
|
end
|
|
247
264
|
end
|
|
248
265
|
alias stop close
|
|
@@ -47,11 +47,12 @@ module Rubik
|
|
|
47
47
|
def chronological_summary(max_entries: 20)
|
|
48
48
|
return "" if @entries.empty?
|
|
49
49
|
|
|
50
|
-
lines = @entries.
|
|
50
|
+
lines = @entries.last(max_entries).map.with_index(1) do |entry, i|
|
|
51
51
|
status_tag = entry[:status] == :error ? " [FAILED]" : ""
|
|
52
52
|
" #{i}. #{entry[:tool]}#{status_tag}"
|
|
53
53
|
end
|
|
54
|
-
|
|
54
|
+
dropped = @entries.size - max_entries
|
|
55
|
+
lines.unshift(" ... (#{dropped} earlier calls omitted)") if dropped.positive?
|
|
55
56
|
lines.join("\n")
|
|
56
57
|
end
|
|
57
58
|
|
|
@@ -14,18 +14,28 @@ module Rubik
|
|
|
14
14
|
# (zararsız sıralama).
|
|
15
15
|
module CompletionSupervisor
|
|
16
16
|
def handle_subagent_completion(inst)
|
|
17
|
-
merge_branch_buffer(inst) if inst.workspace == "branch" && inst.completed?
|
|
18
|
-
auto_apply_subagent_buffer(inst) if inst.completed?
|
|
19
|
-
retain_result(inst)
|
|
20
|
-
Rubik::Storage::TranscriptIndexer.index(storage_store, inst)
|
|
21
|
-
reap_orphans
|
|
22
|
-
release_ready!
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
guard_completion_step("branch buffer merge", inst) { merge_branch_buffer(inst) if inst.workspace == "branch" && inst.completed? }
|
|
18
|
+
guard_completion_step("auto-apply", inst) { auto_apply_subagent_buffer(inst) if inst.completed? }
|
|
19
|
+
guard_completion_step("result retention", inst) { retain_result(inst) }
|
|
20
|
+
guard_completion_step("transcript indexing", inst) { Rubik::Storage::TranscriptIndexer.index(storage_store, inst) }
|
|
21
|
+
guard_completion_step("orphan reaping", inst) { reap_orphans }
|
|
22
|
+
guard_completion_step("ready release", inst) { release_ready! }
|
|
23
|
+
ensure
|
|
24
|
+
# The terminal contract (issue #619): never strand a waiter.
|
|
25
|
+
guard_completion_step("completion notice", inst) { emit_completion_notice(inst) }
|
|
26
|
+
guard_completion_step("waiter wake-up", inst) { notify_wait! }
|
|
25
27
|
end
|
|
26
28
|
|
|
27
29
|
private
|
|
28
30
|
|
|
31
|
+
# One failing step degrades to a warning; the rest of the choreography still runs.
|
|
32
|
+
def guard_completion_step(label, inst)
|
|
33
|
+
yield
|
|
34
|
+
rescue StandardError => e
|
|
35
|
+
Rubik::Log.warn(:orchestrator, "completion #{label} failed for #{inst.id}: #{e.class}: #{e.message}")
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
|
|
29
39
|
# One terminal outcome per execution (issue #125): an operator kill
|
|
30
40
|
# already announced the cancellation from the kill path — the settle
|
|
31
41
|
# notice would be a duplicate. Natural settles announce here, with the
|