rubik 0.9.0 → 0.10.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 +250 -43
- data/README.md +5 -1
- data/lib/rubik/adapter/base.rb +21 -1
- data/lib/rubik/adapter/generic.rb +0 -14
- data/lib/rubik/adapter/node.rb +2 -0
- data/lib/rubik/adapter/process.rb +28 -67
- data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
- data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
- data/lib/rubik/buffer/apply_contract.rb +3 -3
- data/lib/rubik/buffer/apply_transaction.rb +165 -0
- data/lib/rubik/buffer/manager.rb +26 -120
- data/lib/rubik/cli/workspace_trust.rb +16 -0
- data/lib/rubik/config/deep_ops.rb +4 -1
- data/lib/rubik/config/defaults.rb +29 -3
- data/lib/rubik/config/schema.rb +8 -0
- data/lib/rubik/config/secret_policy.rb +197 -0
- data/lib/rubik/config/store.rb +14 -0
- data/lib/rubik/config.rb +11 -0
- data/lib/rubik/diagnostics/collector.rb +0 -9
- data/lib/rubik/errors.rb +15 -0
- data/lib/rubik/event_bus.rb +2 -1
- data/lib/rubik/kernel/dispatcher.rb +16 -11
- data/lib/rubik/kernel/unit.rb +4 -1
- data/lib/rubik/llm/registry.rb +12 -9
- data/lib/rubik/llm/router.rb +2 -5
- data/lib/rubik/log.rb +23 -1
- data/lib/rubik/lsp/client.rb +152 -445
- data/lib/rubik/lsp/document_store.rb +209 -0
- data/lib/rubik/lsp/manager.rb +14 -10
- data/lib/rubik/lsp/transport.rb +365 -0
- data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
- data/lib/rubik/mcp/boot_run.rb +190 -0
- data/lib/rubik/mcp/client.rb +4 -3
- data/lib/rubik/mcp/manager.rb +46 -165
- data/lib/rubik/mcp/transport/stdio.rb +55 -10
- data/lib/rubik/orchestrator/auto_apply.rb +4 -3
- data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
- data/lib/rubik/orchestrator/goal_session.rb +519 -0
- data/lib/rubik/orchestrator/loop.rb +28 -47
- data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
- data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
- data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
- data/lib/rubik/orchestrator/resume_window.rb +96 -0
- data/lib/rubik/orchestrator/shell_deps.rb +16 -0
- data/lib/rubik/orchestrator/stream_manager.rb +2 -1
- data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
- data/lib/rubik/orchestrator/subagent/instance.rb +11 -12
- data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
- data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
- data/lib/rubik/orchestrator/subagent/questioner.rb +24 -7
- data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
- data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
- data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
- data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
- data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
- data/lib/rubik/orchestrator/subagent/step_runner.rb +11 -3
- data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
- data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
- data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
- data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
- data/lib/rubik/orchestrator.rb +1 -0
- data/lib/rubik/platform/io_drain.rb +2 -1
- data/lib/rubik/platform/process.rb +144 -54
- data/lib/rubik/platform.rb +17 -1
- data/lib/rubik/plugin/manager.rb +2 -1
- data/lib/rubik/plugin/storage.rb +20 -29
- data/lib/rubik/repl/commands/agent_context.rb +9 -8
- data/lib/rubik/repl/commands/agent_execution.rb +20 -502
- data/lib/rubik/repl/commands/agent_models.rb +4 -3
- data/lib/rubik/repl/commands/agent_providers.rb +43 -2
- data/lib/rubik/repl/commands/base.rb +47 -2
- data/lib/rubik/repl/commands/buffer.rb +6 -5
- data/lib/rubik/repl/commands/lsp.rb +3 -2
- data/lib/rubik/repl/commands/pruner.rb +2 -1
- data/lib/rubik/repl/commands/secrets.rb +151 -0
- data/lib/rubik/repl/commands/workspace.rb +60 -150
- data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
- data/lib/rubik/repl/config_wiring.rb +3 -3
- data/lib/rubik/repl/hooks.rb +2 -1
- data/lib/rubik/repl/input_execution.rb +158 -76
- data/lib/rubik/repl/input_loop.rb +3 -2
- data/lib/rubik/repl/lifecycle.rb +82 -204
- data/lib/rubik/repl/notice_router.rb +319 -0
- data/lib/rubik/repl/paste_support.rb +3 -1
- data/lib/rubik/repl/presentation.rb +5 -3
- data/lib/rubik/repl/pruner/gate.rb +3 -1
- data/lib/rubik/repl/pruner/harness.rb +2 -1
- data/lib/rubik/repl/shell.rb +4 -3
- data/lib/rubik/repl/turn_recorder.rb +6 -5
- data/lib/rubik/runtime/discovery.rb +3 -2
- data/lib/rubik/runtime/loop_service.rb +4 -3
- data/lib/rubik/runtime/manifest_store.rb +124 -0
- data/lib/rubik/runtime/overlay_guard.rb +127 -0
- data/lib/rubik/runtime/overlay_storage.rb +269 -272
- data/lib/rubik/runtime/task_manager.rb +71 -27
- data/lib/rubik/runtime.rb +1 -0
- data/lib/rubik/sandbox/driver/docker.rb +5 -1
- data/lib/rubik/sandbox/runner.rb +16 -15
- data/lib/rubik/security/allowlist.rb +167 -0
- data/lib/rubik/security/audit_log.rb +72 -0
- data/lib/rubik/security/classifier/classifier.rb +8 -0
- data/lib/rubik/security/command_gate.rb +591 -0
- data/lib/rubik/security/guard.rb +94 -1077
- data/lib/rubik/security/path_policy.rb +117 -0
- data/lib/rubik/security/secret_store.rb +463 -0
- data/lib/rubik/security/synthesis_gate.rb +297 -0
- data/lib/rubik/security.rb +5 -0
- data/lib/rubik/session/manager.rb +7 -1
- data/lib/rubik/skills/manager.rb +2 -3
- data/lib/rubik/storage/ranking.rb +113 -0
- data/lib/rubik/storage/search.rb +49 -0
- data/lib/rubik/storage/store.rb +15 -112
- data/lib/rubik/storage/transcript_indexer.rb +2 -3
- data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
- data/lib/rubik/tools/base.rb +22 -0
- data/lib/rubik/tools/builtins/files.rb +3 -15
- data/lib/rubik/tools/builtins/grep.rb +3 -9
- data/lib/rubik/tools/builtins/lifecycle.rb +96 -92
- data/lib/rubik/tools/builtins/synthesis.rb +3 -0
- data/lib/rubik/tools/lifecycle_rollbacks.rb +100 -0
- data/lib/rubik/tools/native_search.rb +2 -1
- data/lib/rubik/tools/truncator.rb +73 -36
- data/lib/rubik/ui/banner.rb +4 -3
- data/lib/rubik/ui/spinner.rb +3 -1
- data/lib/rubik/ui/statusline.rb +6 -5
- data/lib/rubik/version.rb +1 -1
- data/lib/rubik.rb +1 -2
- metadata +23 -1
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rubik
|
|
4
|
+
module MCP
|
|
5
|
+
# One boot run (issue #453): the mutable state of a single
|
|
6
|
+
# perform_load_from_config invocation — entries / connected / outcomes /
|
|
7
|
+
# boot_epoch — plus the five named phases that consume it, moved
|
|
8
|
+
# verbatim from Manager's former parameter-threaded phase methods. The
|
|
9
|
+
# outcomes ledger is an explicit atom map (index -> :applied | :failed;
|
|
10
|
+
# absence = skipped or never ran — exactly what owes_stop? reads).
|
|
11
|
+
#
|
|
12
|
+
# Friend-class seam: Manager keeps its guarded internals private, so
|
|
13
|
+
# BootRun reaches them through send (the public seams — add_server,
|
|
14
|
+
# connect_server, apply_connected_server, transition_server!, the
|
|
15
|
+
# BootLifecycle claim, and the client() reader — are called directly).
|
|
16
|
+
# Every monitor-hold boundary and epoch gate below is the former inline
|
|
17
|
+
# code relocated, not restructured: the atomic-rollback doctrine lives
|
|
18
|
+
# in run's ensure choreography plus BootLifecycle's epoch machinery.
|
|
19
|
+
class BootRun
|
|
20
|
+
attr_reader :entries, :connected, :outcomes, :boot_epoch
|
|
21
|
+
|
|
22
|
+
def initialize(manager, servers_cfg, mcp_enabled)
|
|
23
|
+
@manager = manager
|
|
24
|
+
@servers_cfg = servers_cfg
|
|
25
|
+
@mcp_enabled = mcp_enabled
|
|
26
|
+
# Boot epoch (review round): :connecting marks carry a per-invocation
|
|
27
|
+
# identity so a wedged older boot can never downgrade a newer mark.
|
|
28
|
+
@boot_epoch = Object.new
|
|
29
|
+
@entries = []
|
|
30
|
+
@connected = []
|
|
31
|
+
@outcomes = {}
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Choreography: register first (phase 0 fails per server without
|
|
35
|
+
# orphaning anything), then ONE ensure spans both post-registration
|
|
36
|
+
# phases — phase 1 deliberately did no context wiring, so stopping
|
|
37
|
+
# the client is the ONLY cleanup an unapplied prepared slot owes (no
|
|
38
|
+
# orphaned child processes, even when an interrupt lands mid-boot).
|
|
39
|
+
# The begin/ensure boundary is verbatim from the former body: a
|
|
40
|
+
# register! failure never triggers the sweeps, exactly as before.
|
|
41
|
+
def run
|
|
42
|
+
register!
|
|
43
|
+
begin
|
|
44
|
+
connect!
|
|
45
|
+
apply!
|
|
46
|
+
ensure
|
|
47
|
+
rollback_unapplied!
|
|
48
|
+
fail_orphans!
|
|
49
|
+
end
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
# Phase 0 (sequential, config order): registration still goes
|
|
56
|
+
# through add_server — subclasses and plugins wrap that seam, and a
|
|
57
|
+
# broken definition must fail exactly as before (logged, skipped,
|
|
58
|
+
# not half-registered). auto_start is deliberately false here: the
|
|
59
|
+
# connect half moved to the fan-out. Ends each auto-start candidate
|
|
60
|
+
# with the phase-0 claim (D5).
|
|
61
|
+
def register!
|
|
62
|
+
@servers_cfg.each do |sname, sdefn|
|
|
63
|
+
next unless sdefn.is_a?(Hash)
|
|
64
|
+
|
|
65
|
+
auto_start = @mcp_enabled && (sdefn.fetch("enabled", true) != false)
|
|
66
|
+
begin
|
|
67
|
+
@manager.add_server(sname, sdefn, auto_start: false, persist: nil)
|
|
68
|
+
rescue StandardError => e
|
|
69
|
+
# ENSURE-LOG (G3 round-1): one broken server config used to
|
|
70
|
+
# vanish behind an ENV["RUBIK_DEBUG"] gate; a misconfigured
|
|
71
|
+
# server must surface without debug mode.
|
|
72
|
+
Rubik::Log.warn(:mcp, "failed to load MCP server '#{sname}' (#{e.class}: #{e.message})")
|
|
73
|
+
@manager.send(:emit_mcp_event, :mcp_server_failed, :err, sname.to_s, error: e.message)
|
|
74
|
+
next
|
|
75
|
+
end
|
|
76
|
+
next unless auto_start
|
|
77
|
+
|
|
78
|
+
name_str = sname.to_s
|
|
79
|
+
# Phase-0 claim (D5): check+mark in ONE monitor hold — a user
|
|
80
|
+
# enable/disable/remove that races the boot reads a definite
|
|
81
|
+
# state, and phase 2's gate below can never miss a window.
|
|
82
|
+
@entries << [name_str, sdefn.transform_keys(&:to_s)] unless @manager.claim_boot_slot!(name_str, @boot_epoch)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Phase 1 (fan-out): pure I/O connects. Slots are captured by SIDE
|
|
87
|
+
# EFFECT inside the item blocks: when an Exception-level error (a
|
|
88
|
+
# boot interrupt) kills .map before it returns, the assignment below
|
|
89
|
+
# never happens — a value-only `connected` would leave the ensure
|
|
90
|
+
# blind to every successfully-started client. Hence the pre-built
|
|
91
|
+
# array owned here rather than a returned value.
|
|
92
|
+
def connect!
|
|
93
|
+
@connected = ::Array.new(@entries.size)
|
|
94
|
+
return if @entries.empty?
|
|
95
|
+
|
|
96
|
+
Rubik::Concurrency.map(@entries.each_with_index.to_a) do |(name_str, parsed_defn), index|
|
|
97
|
+
@connected[index] = { ok: true, **@manager.connect_server(name_str, parsed_defn) }
|
|
98
|
+
rescue StandardError => e
|
|
99
|
+
@connected[index] = { ok: false, error: e }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Phase 2 (sequential, config order): apply state; identical
|
|
104
|
+
# per-server warning on either phase's failure.
|
|
105
|
+
def apply!
|
|
106
|
+
@entries.each_with_index do |(name_str, parsed_defn), index|
|
|
107
|
+
slot = @connected[index]
|
|
108
|
+
next unless slot # never connected (interrupt during phase 1)
|
|
109
|
+
|
|
110
|
+
if slot[:ok]
|
|
111
|
+
begin
|
|
112
|
+
outcome = @manager.send(:boot_apply_gate, name_str, parsed_defn, @boot_epoch,
|
|
113
|
+
client: slot[:client], tools: slot[:tools])
|
|
114
|
+
@outcomes[index] = :applied if outcome == :applied
|
|
115
|
+
if outcome == :applied
|
|
116
|
+
@manager.send(:emit_mcp_event, :mcp_server_loaded, :info, name_str,
|
|
117
|
+
tools: @manager.send(:tool_count, name_str))
|
|
118
|
+
end
|
|
119
|
+
rescue StandardError => e
|
|
120
|
+
# ENSURE-LOG (G3 round-1): one broken server config used to
|
|
121
|
+
# vanish behind an ENV["RUBIK_DEBUG"] gate; a misconfigured
|
|
122
|
+
# server must surface without debug mode. apply's own
|
|
123
|
+
# rollback already set :failed and stopped its client via
|
|
124
|
+
# the context drain; :failed marks the slot so the ensure
|
|
125
|
+
# never double-stops it.
|
|
126
|
+
@outcomes[index] = :failed
|
|
127
|
+
Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{e.class}: #{e.message})")
|
|
128
|
+
@manager.send(:emit_mcp_event, :mcp_server_failed, :err, name_str, error: e.message)
|
|
129
|
+
end
|
|
130
|
+
else
|
|
131
|
+
# Only our own epoch's mark may flip to :failed — a user
|
|
132
|
+
# disable/remove during the handshake, or a newer boot's fresh
|
|
133
|
+
# mark, must win over this boot's connect failure (D5). The
|
|
134
|
+
# epoch gate and flip happen in the same monitor hold inside
|
|
135
|
+
# transition_server!.
|
|
136
|
+
@manager.transition_server!(name_str, :failed, epoch: @boot_epoch)
|
|
137
|
+
error = slot[:error]
|
|
138
|
+
Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{error.class}: #{error.message})")
|
|
139
|
+
@manager.send(:emit_mcp_event, :mcp_server_failed, :err, name_str, error: error.message)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Ensure part 1: stop every prepared client that did not end up
|
|
145
|
+
# registered: a failed apply already rolled its own client back via
|
|
146
|
+
# the context drain, while a never-applied one (interrupt between
|
|
147
|
+
# phases) owes a stop here. A :skipped slot also owes a stop —
|
|
148
|
+
# skipping the APPLY must not orphan its prepared child process (that
|
|
149
|
+
# is the exact leak D10 closed for handshakes). owes_stop? is the
|
|
150
|
+
# structural form of that rule: skipped and never-ran slots owe.
|
|
151
|
+
def rollback_unapplied!
|
|
152
|
+
@entries.each_with_index do |(name_str, _defn), index|
|
|
153
|
+
slot = @connected[index]
|
|
154
|
+
next if !slot.is_a?(Hash) || !slot[:client] || !owes_stop?(index)
|
|
155
|
+
|
|
156
|
+
registered = @manager.client(name_str)
|
|
157
|
+
next if registered.equal?(slot[:client])
|
|
158
|
+
|
|
159
|
+
begin
|
|
160
|
+
slot[:client].stop
|
|
161
|
+
rescue StandardError => e
|
|
162
|
+
Rubik::Log.warn(:mcp, "failed to stop unapplied MCP server '#{name_str}' (#{e.class}: #{e.message})")
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# The named form of the outcomes-ledger absence rule ("absence = owes
|
|
168
|
+
# stop"): an index missing from the ledger means the slot was skipped
|
|
169
|
+
# or never ran, and both owe a stop in the ensure. Delegates the
|
|
170
|
+
# per-outcome rule to BootLifecycle.owes_stop? so the stop-debt rule
|
|
171
|
+
# keeps one home; the ledger only ever records :applied / :failed, so
|
|
172
|
+
# absence is exactly the owes case — behavior-identical to the former
|
|
173
|
+
# inline BootLifecycle.owes_stop?(outcomes[index]) read.
|
|
174
|
+
def owes_stop?(index)
|
|
175
|
+
BootLifecycle.owes_stop?(@outcomes[index])
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Ensure part 2 — terminal state for boot entries that never reached
|
|
179
|
+
# apply (D6): anything still :connecting failed to boot; user-chosen
|
|
180
|
+
# values (:disabled, :active, deleted-by-remove) are left untouched.
|
|
181
|
+
# The epoch check inside transition_server! keeps an older wedged
|
|
182
|
+
# boot's sweep from downgrading a newer boot's fresh mark.
|
|
183
|
+
def fail_orphans!
|
|
184
|
+
@entries.each do |(name_str, _defn)|
|
|
185
|
+
@manager.transition_server!(name_str, :failed, epoch: @boot_epoch)
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
data/lib/rubik/mcp/client.rb
CHANGED
|
@@ -51,10 +51,11 @@ module Rubik
|
|
|
51
51
|
|
|
52
52
|
attr_reader :name, :transport, :server_info, :server_capabilities, :protocol_version, :initialized
|
|
53
53
|
|
|
54
|
-
def initialize(command, args: [], env: {}, name: "unnamed", root_dir: Dir.pwd,
|
|
55
|
-
call_timeout: nil)
|
|
54
|
+
def initialize(command, args: [], env: {}, inherit_env: true, name: "unnamed", root_dir: Dir.pwd,
|
|
55
|
+
transport: nil, guard: nil, call_timeout: nil)
|
|
56
56
|
@name = name.to_s
|
|
57
|
-
@transport = transport || Transport::Stdio.new(command, args: args, env: env,
|
|
57
|
+
@transport = transport || Transport::Stdio.new(command, args: args, env: env, inherit_env: inherit_env,
|
|
58
|
+
root_dir: root_dir, guard: guard)
|
|
58
59
|
# Session-wide tools/call timeout; nil (absent or not a positive
|
|
59
60
|
# number) keeps the per-call DEFAULT_CALL_TIMEOUT fallback.
|
|
60
61
|
@default_call_timeout = positive_number?(call_timeout) ? call_timeout : nil
|
data/lib/rubik/mcp/manager.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require "monitor"
|
|
4
4
|
require_relative "boot_lifecycle"
|
|
5
|
+
require_relative "../storage/layout"
|
|
6
|
+
require_relative "boot_run"
|
|
5
7
|
require_relative "client"
|
|
6
8
|
require_relative "proxy_tool"
|
|
7
9
|
require_relative "errors"
|
|
@@ -59,7 +61,7 @@ module Rubik
|
|
|
59
61
|
def enabled?
|
|
60
62
|
return true unless @config
|
|
61
63
|
|
|
62
|
-
@config
|
|
64
|
+
Rubik::Storage::Layout.config_get(@config, "mcp.enabled", true) != false
|
|
63
65
|
end
|
|
64
66
|
|
|
65
67
|
# Returns {ok:, tools:} — the config persist outcome plus the registered
|
|
@@ -80,6 +82,8 @@ module Rubik
|
|
|
80
82
|
# so `!mcp add` output stays minimal (the knob is hand-written
|
|
81
83
|
# config, not a CLI flag).
|
|
82
84
|
server_hash["timeout"] = parsed_defn["timeout"] if parsed_defn["timeout"]
|
|
85
|
+
# Minimal round-trip rule (#429): only written when explicitly configured.
|
|
86
|
+
server_hash["inherit_env"] = parsed_defn["inherit_env"] if parsed_defn.key?("inherit_env")
|
|
83
87
|
persist_res = @config.set("mcp.servers.#{name_str}", server_hash, persist: persist)
|
|
84
88
|
end
|
|
85
89
|
|
|
@@ -120,6 +124,8 @@ module Rubik
|
|
|
120
124
|
cmd = server_defn["command"] || server_defn[:command]
|
|
121
125
|
args = server_defn["args"] || server_defn[:args] || []
|
|
122
126
|
env = server_defn["env"] || server_defn[:env] || {}
|
|
127
|
+
inherit_env = server_defn.key?("inherit_env") ? server_defn["inherit_env"] : server_defn[:inherit_env] # credential policy (#429): only explicit false opts out
|
|
128
|
+
inherit_env_kwarg = inherit_env.nil? ? {} : { inherit_env: inherit_env } # strict-kwarg DI factories stay compatible
|
|
123
129
|
base_ctx = @context || @runtime_manager&.context
|
|
124
130
|
connect_timeout = resolve_connect_timeout(server_defn, timeout)
|
|
125
131
|
# Config boundary (D2 mirror): validated for BOTH construction
|
|
@@ -129,13 +135,13 @@ module Rubik
|
|
|
129
135
|
|
|
130
136
|
client =
|
|
131
137
|
if @client_factory
|
|
132
|
-
@client_factory.call(cmd, args: args, env: env, name: name_str, root_dir: @root_dir,
|
|
138
|
+
@client_factory.call(cmd, args: args, env: env, **inherit_env_kwarg, name: name_str, root_dir: @root_dir,
|
|
133
139
|
guard: @guard || (base_ctx ? Rubik::Coeffects.get(base_ctx, Rubik::Coeffects::GUARD) : nil))
|
|
134
140
|
else
|
|
135
141
|
Rubik::MCP::Client.new(
|
|
136
142
|
cmd,
|
|
137
143
|
args: args,
|
|
138
|
-
env: env,
|
|
144
|
+
env: env, inherit_env: inherit_env, # nil keeps the transport's inheriting default
|
|
139
145
|
name: name_str,
|
|
140
146
|
root_dir: @root_dir,
|
|
141
147
|
# Resolved live at connect time: the composed kwarg wins, but a
|
|
@@ -334,181 +340,56 @@ module Rubik
|
|
|
334
340
|
|
|
335
341
|
private
|
|
336
342
|
|
|
337
|
-
# Config boot, decomposed into
|
|
338
|
-
#
|
|
339
|
-
#
|
|
340
|
-
#
|
|
341
|
-
#
|
|
342
|
-
#
|
|
343
|
-
#
|
|
343
|
+
# Config boot, decomposed into a BootRun (boot_run.rb): the five named
|
|
344
|
+
# boot phases moved verbatim onto a private run object that owns
|
|
345
|
+
# entries / connected / outcomes / boot_epoch, replacing the former
|
|
346
|
+
# side-channel parameters. N servers' connect handshakes (spawn +
|
|
347
|
+
# initialize + tools/list — the slow I/O) overlap while every
|
|
348
|
+
# observable state change stays sequential in config order (sum → max
|
|
349
|
+
# wall clock). This method only parses the config and opens the run.
|
|
344
350
|
def perform_load_from_config(cfg = nil)
|
|
345
351
|
target_config = cfg || @config
|
|
346
352
|
return unless target_config
|
|
347
353
|
|
|
348
|
-
mcp_enabled =
|
|
349
|
-
servers_cfg =
|
|
354
|
+
mcp_enabled = Rubik::Storage::Layout.config_get(target_config, "mcp.enabled", true) != false
|
|
355
|
+
servers_cfg = Rubik::Storage::Layout.config_get(target_config, "mcp.servers", {})
|
|
350
356
|
return unless servers_cfg.is_a?(Hash)
|
|
351
357
|
|
|
352
|
-
|
|
353
|
-
# per-invocation identity so a wedged older boot's failure branch or
|
|
354
|
-
# ensure sweep can never downgrade a newer boot's fresh mark.
|
|
355
|
-
boot_epoch = Object.new
|
|
356
|
-
entries = register_config_servers(servers_cfg, mcp_enabled, boot_epoch)
|
|
357
|
-
|
|
358
|
-
# Phase 1 deliberately did no context wiring, so stopping the client
|
|
359
|
-
# is the ONLY cleanup an unapplied prepared slot owes — no orphaned
|
|
360
|
-
# child processes, even when an interrupt lands mid-boot. One ensure
|
|
361
|
-
# spans both post-registration phases.
|
|
362
|
-
connected = ::Array.new(entries.size)
|
|
363
|
-
outcomes = {}
|
|
364
|
-
begin
|
|
365
|
-
connect_fanout(entries, connected)
|
|
366
|
-
apply_connected_slots(entries, connected, boot_epoch, outcomes)
|
|
367
|
-
ensure
|
|
368
|
-
stop_unapplied_clients(entries, connected, outcomes)
|
|
369
|
-
fail_orphaned_connecting_marks(entries, boot_epoch)
|
|
370
|
-
end
|
|
371
|
-
nil
|
|
358
|
+
BootRun.new(self, servers_cfg, mcp_enabled).run
|
|
372
359
|
end
|
|
373
360
|
|
|
374
|
-
#
|
|
375
|
-
#
|
|
376
|
-
#
|
|
377
|
-
#
|
|
378
|
-
#
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
# server must surface without debug mode.
|
|
392
|
-
Rubik::Log.warn(:mcp, "failed to load MCP server '#{sname}' (#{e.class}: #{e.message})")
|
|
393
|
-
emit_mcp_event(:mcp_server_failed, :err, sname.to_s, error: e.message)
|
|
394
|
-
next
|
|
395
|
-
end
|
|
396
|
-
next unless auto_start
|
|
397
|
-
|
|
398
|
-
name_str = sname.to_s
|
|
399
|
-
# Phase-0 claim (D5): check+mark in ONE monitor hold — a user
|
|
400
|
-
# enable/disable/remove that races the boot reads a definite
|
|
401
|
-
# state, and phase 2's gate below can never miss a window.
|
|
402
|
-
entries << [name_str, sdefn.transform_keys(&:to_s)] unless claim_boot_slot!(name_str, boot_epoch)
|
|
403
|
-
end
|
|
404
|
-
entries
|
|
405
|
-
end
|
|
406
|
-
|
|
407
|
-
# Phase 1 (fan-out): pure I/O connects. Slots are captured by SIDE
|
|
408
|
-
# EFFECT inside the item blocks: when an Exception-level error (a
|
|
409
|
-
# boot interrupt) kills .map before it returns, the assignment below
|
|
410
|
-
# never happens — a value-only `connected` would leave the ensure
|
|
411
|
-
# blind to every successfully-started client. Hence the pre-built
|
|
412
|
-
# array passed in rather than a returned value.
|
|
413
|
-
def connect_fanout(entries, connected)
|
|
414
|
-
return if entries.empty?
|
|
415
|
-
|
|
416
|
-
Rubik::Concurrency.map(entries.each_with_index.to_a) do |(name_str, parsed_defn), index|
|
|
417
|
-
connected[index] = { ok: true, **connect_server(name_str, parsed_defn) }
|
|
418
|
-
rescue StandardError => e
|
|
419
|
-
connected[index] = { ok: false, error: e }
|
|
420
|
-
end
|
|
421
|
-
end
|
|
422
|
-
|
|
423
|
-
# Phase 2 (sequential, config order): apply state; identical
|
|
424
|
-
# per-server warning on either phase's failure. Outcomes ledger is an
|
|
425
|
-
# explicit atom map (index -> :applied | :failed; absence = skipped or
|
|
426
|
-
# never ran — exactly what owes_stop? reads).
|
|
427
|
-
def apply_connected_slots(entries, connected, boot_epoch, outcomes)
|
|
428
|
-
entries.each_with_index do |(name_str, parsed_defn), index|
|
|
429
|
-
slot = connected[index]
|
|
430
|
-
next unless slot # never connected (interrupt during phase 1)
|
|
431
|
-
|
|
432
|
-
if slot[:ok]
|
|
433
|
-
begin
|
|
434
|
-
outcome = @monitor.synchronize do
|
|
435
|
-
# Single race gate (D5), checked at apply time under the
|
|
436
|
-
# monitor: a disable/remove/enable during boot wins over
|
|
437
|
-
# the boot's own apply, and a stop_all skips it entirely.
|
|
438
|
-
# Epoch-complete gate: this boot may only apply into the
|
|
439
|
-
# slot its own phase-0 claim marked, so a user decision
|
|
440
|
-
# that moved the slot or a newer boot's fresh mark wins.
|
|
441
|
-
# In the synchronous path nothing can interleave, so the
|
|
442
|
-
# guard is provably inert there.
|
|
443
|
-
if @stopped || @statuses[name_str] != :connecting || !@boot_marks[name_str].equal?(boot_epoch)
|
|
444
|
-
:skipped
|
|
445
|
-
else
|
|
446
|
-
apply_connected_server(name_str, parsed_defn, client: slot[:client], tools: slot[:tools])
|
|
447
|
-
# Post-apply revalidation under the boot actor: a no-op
|
|
448
|
-
# when apply already landed :active.
|
|
449
|
-
transition_server!(name_str, :active, epoch: boot_epoch)
|
|
450
|
-
:applied
|
|
451
|
-
end
|
|
452
|
-
end
|
|
453
|
-
outcomes[index] = :applied if outcome == :applied
|
|
454
|
-
emit_mcp_event(:mcp_server_loaded, :info, name_str, tools: (@tools[name_str] || []).size) if outcome == :applied
|
|
455
|
-
rescue StandardError => e
|
|
456
|
-
# ENSURE-LOG (G3 round-1): one broken server config used to
|
|
457
|
-
# vanish behind an ENV["RUBIK_DEBUG"] gate; a misconfigured
|
|
458
|
-
# server must surface without debug mode. apply's own
|
|
459
|
-
# rollback already set :failed and stopped its client via
|
|
460
|
-
# the context drain; :failed marks the slot so the ensure
|
|
461
|
-
# never double-stops it.
|
|
462
|
-
outcomes[index] = :failed
|
|
463
|
-
Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{e.class}: #{e.message})")
|
|
464
|
-
emit_mcp_event(:mcp_server_failed, :err, name_str, error: e.message)
|
|
465
|
-
end
|
|
361
|
+
# BootRun friend seam (boot_run.rb): the D5 race gate, one monitor
|
|
362
|
+
# hold deciding :skipped vs :applied. Moved verbatim — the hold
|
|
363
|
+
# boundary, the @stopped/@statuses/@boot_marks epoch gate and the
|
|
364
|
+
# guarded writes inside are exactly the former inline block; BootRun
|
|
365
|
+
# only receives the decision.
|
|
366
|
+
def boot_apply_gate(name_str, parsed_defn, boot_epoch, client:, tools:)
|
|
367
|
+
@monitor.synchronize do
|
|
368
|
+
# Single race gate (D5), checked at apply time under the
|
|
369
|
+
# monitor: a disable/remove/enable during boot wins over
|
|
370
|
+
# the boot's own apply, and a stop_all skips it entirely.
|
|
371
|
+
# Epoch-complete gate: this boot may only apply into the
|
|
372
|
+
# slot its own phase-0 claim marked, so a user decision
|
|
373
|
+
# that moved the slot or a newer boot's fresh mark wins.
|
|
374
|
+
# In the synchronous path nothing can interleave, so the
|
|
375
|
+
# guard is provably inert there.
|
|
376
|
+
if @stopped || @statuses[name_str] != :connecting || !@boot_marks[name_str].equal?(boot_epoch)
|
|
377
|
+
:skipped
|
|
466
378
|
else
|
|
467
|
-
|
|
468
|
-
#
|
|
469
|
-
#
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
transition_server!(name_str, :failed, epoch: boot_epoch)
|
|
473
|
-
error = slot[:error]
|
|
474
|
-
Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{error.class}: #{error.message})")
|
|
475
|
-
emit_mcp_event(:mcp_server_failed, :err, name_str, error: error.message)
|
|
476
|
-
end
|
|
477
|
-
end
|
|
478
|
-
end
|
|
479
|
-
|
|
480
|
-
# Ensure part 1: stop every prepared client that did not end up
|
|
481
|
-
# registered: a failed apply already rolled its own client back via
|
|
482
|
-
# the context drain, while a never-applied one (interrupt between
|
|
483
|
-
# phases) owes a stop here. A :skipped slot also owes a stop —
|
|
484
|
-
# skipping the APPLY must not orphan its prepared child process (that
|
|
485
|
-
# is the exact leak D10 closed for handshakes). owes_stop? is the
|
|
486
|
-
# structural form of that rule: skipped and never-ran slots owe.
|
|
487
|
-
def stop_unapplied_clients(entries, connected, outcomes)
|
|
488
|
-
entries.each_with_index do |(name_str, _defn), index|
|
|
489
|
-
slot = connected[index]
|
|
490
|
-
next if !slot.is_a?(Hash) || !slot[:client] || !BootLifecycle.owes_stop?(outcomes[index])
|
|
491
|
-
|
|
492
|
-
registered = @monitor.synchronize { @clients[name_str] }
|
|
493
|
-
next if registered.equal?(slot[:client])
|
|
494
|
-
|
|
495
|
-
begin
|
|
496
|
-
slot[:client].stop
|
|
497
|
-
rescue StandardError => e
|
|
498
|
-
Rubik::Log.warn(:mcp, "failed to stop unapplied MCP server '#{name_str}' (#{e.class}: #{e.message})")
|
|
379
|
+
apply_connected_server(name_str, parsed_defn, client: client, tools: tools)
|
|
380
|
+
# Post-apply revalidation under the boot actor: a no-op
|
|
381
|
+
# when apply already landed :active.
|
|
382
|
+
transition_server!(name_str, :active, epoch: boot_epoch)
|
|
383
|
+
:applied
|
|
499
384
|
end
|
|
500
385
|
end
|
|
501
386
|
end
|
|
502
387
|
|
|
503
|
-
#
|
|
504
|
-
#
|
|
505
|
-
#
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
def fail_orphaned_connecting_marks(entries, boot_epoch)
|
|
509
|
-
entries.each do |(name_str, _defn)|
|
|
510
|
-
transition_server!(name_str, :failed, epoch: boot_epoch)
|
|
511
|
-
end
|
|
388
|
+
# BootRun friend seam: the advisory @tools count read for the
|
|
389
|
+
# :mcp_server_loaded event — the same unguarded read the inline phase
|
|
390
|
+
# performed (observability only, never a state decision).
|
|
391
|
+
def tool_count(name_str)
|
|
392
|
+
(@tools[name_str] || []).size
|
|
512
393
|
end
|
|
513
394
|
|
|
514
395
|
public
|
|
@@ -16,12 +16,19 @@ module Rubik
|
|
|
16
16
|
# #stderr_tail for diagnostics.
|
|
17
17
|
STDERR_TAIL_LINES = 20
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
# Credential-shaped variable names stripped from the inherited host
|
|
20
|
+
# environment when a server opts out with `inherit_env: false`. Only
|
|
21
|
+
# the suffix shape is policy — the prefix is the provider's choice
|
|
22
|
+
# (GITHUB_TOKEN, ANTHROPIC_API_KEY, ...), so we match it broadly.
|
|
23
|
+
CREDENTIAL_ENV_PATTERN = /_(API_KEY|TOKEN|SECRET|PASSWORD)\z/i
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
attr_reader :command, :args, :env, :inherit_env, :root_dir, :running, :wait_thr
|
|
26
|
+
|
|
27
|
+
def initialize(command, args: [], env: {}, inherit_env: true, root_dir: Dir.pwd, guard: nil)
|
|
22
28
|
@command = command
|
|
23
29
|
@args = Array(args).map(&:to_s)
|
|
24
30
|
@env = env || {}
|
|
31
|
+
@inherit_env = self.class.coerce_inherit_env(inherit_env)
|
|
25
32
|
@root_dir = File.expand_path(root_dir)
|
|
26
33
|
@guard = guard || Rubik::Security::Guard.new
|
|
27
34
|
@running = false
|
|
@@ -82,14 +89,12 @@ module Rubik
|
|
|
82
89
|
resolved_bin = Rubik::Platform.which(bin)
|
|
83
90
|
cmd_parts[0] = resolved_bin if resolved_bin
|
|
84
91
|
|
|
85
|
-
|
|
86
|
-
|
|
92
|
+
# Match the LSP client's spawn hygiene: the host's bundler/Ruby
|
|
93
|
+
# runtime variables are unset for the child, so an MCP server never
|
|
94
|
+
# inherits a foreign BUNDLE_GEMFILE/RUBYOPT. Explicitly configured
|
|
95
|
+
# env: entries merge on top and win.
|
|
87
96
|
begin
|
|
88
|
-
@stdin, @stdout, @stderr, @wait_thr =
|
|
89
|
-
Open3.popen3(*cmd_parts, chdir: @root_dir)
|
|
90
|
-
else
|
|
91
|
-
Open3.popen3(string_env, *cmd_parts, chdir: @root_dir)
|
|
92
|
-
end
|
|
97
|
+
@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(spawn_env, *cmd_parts, chdir: @root_dir)
|
|
93
98
|
rescue SystemCallError => e
|
|
94
99
|
raise ConnectionError, "Failed to spawn MCP process '#{bin}': #{e.message}"
|
|
95
100
|
end
|
|
@@ -154,6 +159,9 @@ module Rubik
|
|
|
154
159
|
|
|
155
160
|
def close
|
|
156
161
|
child_died = false
|
|
162
|
+
# Capture before @wait_thr is nil'd below: the child-died diagnostic
|
|
163
|
+
# must still report WHICH server process failed to start.
|
|
164
|
+
exited_pid = @wait_thr&.pid
|
|
157
165
|
@mutex.synchronize do
|
|
158
166
|
return if @closed
|
|
159
167
|
|
|
@@ -221,7 +229,7 @@ module Rubik
|
|
|
221
229
|
|
|
222
230
|
if child_died
|
|
223
231
|
hint = stderr_tail.last(5).join(" | ")[0, 512]
|
|
224
|
-
Rubik::Log.warn(:mcp, "MCP child process (pid #{
|
|
232
|
+
Rubik::Log.warn(:mcp, "MCP child process (pid #{exited_pid}) had already exited; recent stderr: #{hint}") unless hint.empty?
|
|
225
233
|
end
|
|
226
234
|
|
|
227
235
|
@mutex.synchronize do
|
|
@@ -251,8 +259,45 @@ module Rubik
|
|
|
251
259
|
@wait_thr&.pid
|
|
252
260
|
end
|
|
253
261
|
|
|
262
|
+
class << self
|
|
263
|
+
# Config coercion for `inherit_env:`: only an explicit false
|
|
264
|
+
# (boolean or the string/nil-safe "false") opts out; every other
|
|
265
|
+
# value — including nil and unknown types — keeps the inheriting
|
|
266
|
+
# default so hand-written config cannot accidentally widen the
|
|
267
|
+
# credential hole or break the spawn.
|
|
268
|
+
def coerce_inherit_env(value)
|
|
269
|
+
![false, "false"].include?(value)
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
254
273
|
private
|
|
255
274
|
|
|
275
|
+
# The environment the child runs in: the host's bundler/Ruby runtime
|
|
276
|
+
# variables are unset (Platform.sanitized_spawn_env — the same scrub
|
|
277
|
+
# list the LSP client applies), then explicitly configured env:
|
|
278
|
+
# entries merge on top and win.
|
|
279
|
+
#
|
|
280
|
+
# Credential policy: by default the child inherits the host env (minus
|
|
281
|
+
# the scrub list) because many MCP servers legitimately need operator
|
|
282
|
+
# credentials (GITHUB_TOKEN etc.). With `inherit_env: false` the child
|
|
283
|
+
# still inherits the host env, but credential-shaped variables
|
|
284
|
+
# (CREDENTIAL_ENV_PATTERN) are additionally unset — spawn override
|
|
285
|
+
# semantics need an explicit nil to remove an inherited variable —
|
|
286
|
+
# leaving the sanitized base plus ONLY the server's explicit env:
|
|
287
|
+
# entries for anything credential-shaped. Explicit env: entries always
|
|
288
|
+
# win in both modes.
|
|
289
|
+
def spawn_env
|
|
290
|
+
base = Rubik::Platform.sanitized_spawn_env
|
|
291
|
+
unless @inherit_env
|
|
292
|
+
ENV.each_key do |key|
|
|
293
|
+
base[key] = nil if key.match?(CREDENTIAL_ENV_PATTERN)
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
base.merge(
|
|
297
|
+
@env.transform_keys(&:to_s).transform_values(&:to_s)
|
|
298
|
+
)
|
|
299
|
+
end
|
|
300
|
+
|
|
256
301
|
def write_message(msg_hash)
|
|
257
302
|
json = JSON.dump(msg_hash)
|
|
258
303
|
@mutex.synchronize do
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "subagent/notices"
|
|
4
|
+
require_relative "../storage/layout"
|
|
4
5
|
|
|
5
6
|
module Rubik
|
|
6
7
|
module Orchestrator
|
|
@@ -58,7 +59,7 @@ module Rubik
|
|
|
58
59
|
# overrides config resolution entirely — the StepExecutor injection
|
|
59
60
|
# seam (its tests pass explicit modes positionally).
|
|
60
61
|
def apply?(config, trigger:, subagent: false, tool: nil, mode: nil)
|
|
61
|
-
return false if
|
|
62
|
+
return false if Rubik::Storage::Layout.config_get(config, "buffer.model_can_apply", true) == false
|
|
62
63
|
|
|
63
64
|
return false if trigger == :pre_execution && !tool.nil? && !test_class_tool?(tool)
|
|
64
65
|
|
|
@@ -146,11 +147,11 @@ module Rubik
|
|
|
146
147
|
# -- one defensive spelling each (replaced eight) -------------------
|
|
147
148
|
|
|
148
149
|
def mode(config)
|
|
149
|
-
|
|
150
|
+
Rubik::Storage::Layout.config_get(config, MODE_KEY, DEFAULT_MODE).to_s.downcase
|
|
150
151
|
end
|
|
151
152
|
|
|
152
153
|
def subagent_flag_on?(config)
|
|
153
|
-
|
|
154
|
+
Rubik::Storage::Layout.config_get(config, FLAG_KEY, DEFAULT_FLAG) == true
|
|
154
155
|
end
|
|
155
156
|
|
|
156
157
|
def test_class_tool?(canonical_name)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../storage/layout"
|
|
4
|
+
|
|
3
5
|
module Rubik
|
|
4
6
|
module Orchestrator
|
|
5
7
|
# Manages auto-continuation when an LLM response is truncated due to
|
|
@@ -131,28 +133,7 @@ module Rubik
|
|
|
131
133
|
private
|
|
132
134
|
|
|
133
135
|
def safe_config(key, default)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if @config.respond_to?(:fetch)
|
|
137
|
-
begin
|
|
138
|
-
val = @config.fetch(key)
|
|
139
|
-
return val unless val.nil?
|
|
140
|
-
rescue KeyError, ArgumentError
|
|
141
|
-
nil
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
if key.include?(".") && @config.respond_to?(:dig)
|
|
146
|
-
val = @config.dig(*key.split("."))
|
|
147
|
-
return val unless val.nil?
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
if @config.respond_to?(:[])
|
|
151
|
-
val = @config[key]
|
|
152
|
-
return val unless val.nil?
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
default
|
|
136
|
+
Rubik::Storage::Layout.config_get(@config, key, default)
|
|
156
137
|
rescue KeyError, TypeError, NoMethodError
|
|
157
138
|
default
|
|
158
139
|
end
|