rubik 0.9.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +203 -1
  4. data/lib/rubik/adapter/base.rb +2 -1
  5. data/lib/rubik/adapter/process.rb +28 -67
  6. data/lib/rubik/ast/extractor.rb +2 -1
  7. data/lib/rubik/ast/treesitter_extractor.rb +2 -1
  8. data/lib/rubik/benchmark/vakanuvis_adoption.rb +12 -4
  9. data/lib/rubik/buffer/apply_contract.rb +3 -3
  10. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  11. data/lib/rubik/buffer/manager.rb +26 -120
  12. data/lib/rubik/config/deep_ops.rb +4 -1
  13. data/lib/rubik/config/defaults.rb +6 -3
  14. data/lib/rubik/config/schema.rb +8 -0
  15. data/lib/rubik/config/secret_policy.rb +198 -0
  16. data/lib/rubik/config/store.rb +2 -1
  17. data/lib/rubik/config.rb +13 -2
  18. data/lib/rubik/context/ignore_rules.rb +193 -0
  19. data/lib/rubik/context/scanner.rb +46 -161
  20. data/lib/rubik/diagnostics/collector.rb +0 -9
  21. data/lib/rubik/errors.rb +15 -0
  22. data/lib/rubik/event_bus.rb +2 -1
  23. data/lib/rubik/kernel/dispatcher.rb +12 -7
  24. data/lib/rubik/kernel/unit.rb +4 -1
  25. data/lib/rubik/llm/client.rb +6 -200
  26. data/lib/rubik/llm/connection_pool.rb +104 -0
  27. data/lib/rubik/llm/registry.rb +13 -9
  28. data/lib/rubik/llm/router.rb +2 -5
  29. data/lib/rubik/llm/sse_parser.rb +110 -0
  30. data/lib/rubik/log.rb +21 -0
  31. data/lib/rubik/lsp/circuit_breaker.rb +98 -0
  32. data/lib/rubik/lsp/client.rb +77 -273
  33. data/lib/rubik/lsp/client_provisioner.rb +49 -0
  34. data/lib/rubik/lsp/manager.rb +67 -96
  35. data/lib/rubik/lsp/transport.rb +365 -0
  36. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  37. data/lib/rubik/mcp/boot_run.rb +190 -0
  38. data/lib/rubik/mcp/manager.rb +55 -166
  39. data/lib/rubik/mcp/transport/stdio.rb +4 -1
  40. data/lib/rubik/orchestrator/auto_apply.rb +8 -6
  41. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  42. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +12 -1
  43. data/lib/rubik/orchestrator/goal_ledger/schema_vocabulary.rb +38 -0
  44. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +6 -1
  45. data/lib/rubik/orchestrator/goal_ledger.rb +1 -0
  46. data/lib/rubik/orchestrator/goal_session.rb +53 -39
  47. data/lib/rubik/orchestrator/loop.rb +28 -47
  48. data/lib/rubik/orchestrator/lsp_feedback.rb +1 -1
  49. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  50. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  51. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  52. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  53. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  54. data/lib/rubik/orchestrator/step_executor.rb +2 -1
  55. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  56. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  57. data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
  58. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  59. data/lib/rubik/orchestrator/subagent/notices.rb +2 -2
  60. data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
  61. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  62. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  63. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  64. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  65. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  66. data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
  67. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  68. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  69. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  70. data/lib/rubik/platform/io_drain.rb +5 -2
  71. data/lib/rubik/platform/process.rb +144 -54
  72. data/lib/rubik/platform.rb +2 -1
  73. data/lib/rubik/plugin/manager.rb +2 -1
  74. data/lib/rubik/plugin/storage.rb +8 -8
  75. data/lib/rubik/repl/commands/agent_context.rb +8 -7
  76. data/lib/rubik/repl/commands/agent_execution.rb +13 -32
  77. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  78. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  79. data/lib/rubik/repl/commands/base.rb +47 -2
  80. data/lib/rubik/repl/commands/buffer.rb +5 -4
  81. data/lib/rubik/repl/commands/lsp.rb +2 -1
  82. data/lib/rubik/repl/commands/pruner.rb +2 -1
  83. data/lib/rubik/repl/commands/secrets.rb +151 -0
  84. data/lib/rubik/repl/commands/workspace.rb +60 -150
  85. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  86. data/lib/rubik/repl/config_wiring.rb +8 -6
  87. data/lib/rubik/repl/hooks.rb +2 -1
  88. data/lib/rubik/repl/input_execution.rb +162 -79
  89. data/lib/rubik/repl/input_loop.rb +2 -1
  90. data/lib/rubik/repl/lifecycle.rb +60 -210
  91. data/lib/rubik/repl/notice_router.rb +319 -0
  92. data/lib/rubik/repl/paste_support.rb +3 -1
  93. data/lib/rubik/repl/presentation.rb +5 -3
  94. data/lib/rubik/repl/pruner/gate.rb +3 -1
  95. data/lib/rubik/repl/pruner/harness.rb +2 -1
  96. data/lib/rubik/repl/shell.rb +4 -3
  97. data/lib/rubik/repl/tee_io.rb +2 -1
  98. data/lib/rubik/repl/turn_recorder.rb +6 -5
  99. data/lib/rubik/runtime/ast_service.rb +2 -1
  100. data/lib/rubik/runtime/discovery.rb +3 -2
  101. data/lib/rubik/runtime/loop_service.rb +4 -3
  102. data/lib/rubik/runtime/manifest_store.rb +9 -1
  103. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  104. data/lib/rubik/runtime/overlay_storage.rb +109 -163
  105. data/lib/rubik/runtime/task_manager.rb +64 -25
  106. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  107. data/lib/rubik/sandbox/runner.rb +21 -19
  108. data/lib/rubik/security/audit_log.rb +2 -1
  109. data/lib/rubik/security/command_gate.rb +126 -243
  110. data/lib/rubik/security/guard.rb +7 -3
  111. data/lib/rubik/security/secret_store.rb +492 -0
  112. data/lib/rubik/security/segment_report.rb +161 -0
  113. data/lib/rubik/security/synthesis_gate.rb +8 -11
  114. data/lib/rubik/session/manager.rb +7 -1
  115. data/lib/rubik/session/recorder.rb +2 -1
  116. data/lib/rubik/skills/manager.rb +4 -4
  117. data/lib/rubik/storage/search.rb +49 -0
  118. data/lib/rubik/storage/store.rb +13 -20
  119. data/lib/rubik/storage/transcript_indexer.rb +3 -2
  120. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  121. data/lib/rubik/storage/vakanuvis.rb +2 -1
  122. data/lib/rubik/tools/base.rb +22 -0
  123. data/lib/rubik/tools/builtins/files.rb +3 -15
  124. data/lib/rubik/tools/builtins/goal_ledger.rb +12 -1
  125. data/lib/rubik/tools/builtins/grep.rb +1 -6
  126. data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
  127. data/lib/rubik/tools/builtins/synthesis.rb +3 -3
  128. data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
  129. data/lib/rubik/tools/preflight_runner.rb +2 -1
  130. data/lib/rubik/tools/truncator.rb +48 -36
  131. data/lib/rubik/tools/uri_resolver.rb +3 -2
  132. data/lib/rubik/ui/banner.rb +4 -3
  133. data/lib/rubik/ui/capability.rb +1 -0
  134. data/lib/rubik/ui/spinner.rb +3 -1
  135. data/lib/rubik/ui/statusline.rb +6 -5
  136. data/lib/rubik/version.rb +1 -1
  137. data/lib/rubik.rb +4 -5
  138. metadata +20 -1
@@ -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
@@ -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.fetch("mcp.enabled", true) != false
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
@@ -134,7 +136,7 @@ module Rubik
134
136
  client =
135
137
  if @client_factory
136
138
  @client_factory.call(cmd, args: args, env: env, **inherit_env_kwarg, name: name_str, root_dir: @root_dir,
137
- guard: @guard || (base_ctx ? Rubik::Coeffects.get(base_ctx, Rubik::Coeffects::GUARD) : nil))
139
+ guard: resolve_guard(base_ctx))
138
140
  else
139
141
  Rubik::MCP::Client.new(
140
142
  cmd,
@@ -146,7 +148,7 @@ module Rubik
146
148
  # Manager built before wire_runtime_context still picks up the
147
149
  # session guard once the context provides it (construction-time
148
150
  # snapshots of coeffects go stale — this matches base_ctx above).
149
- guard: @guard || (base_ctx ? Rubik::Coeffects.get(base_ctx, Rubik::Coeffects::GUARD) : nil),
151
+ guard: resolve_guard(base_ctx),
150
152
  call_timeout: call_timeout
151
153
  )
152
154
  end
@@ -167,6 +169,24 @@ module Rubik
167
169
  end
168
170
  end
169
171
 
172
+ # The single boot race gate (D5), checked at apply time under the
173
+ # monitor: a disable/remove/enable during boot wins over the boot's
174
+ # own apply, and a stop_all skips it entirely. Epoch-complete: this
175
+ # boot may only apply into the slot its own phase-0 claim marked,
176
+ # so a user decision that moved the slot or a newer boot's fresh
177
+ # mark wins. Callers hold @monitor.
178
+ def boot_gate_open?(name_str, epoch)
179
+ !@stopped && @statuses[name_str] == :connecting && @boot_marks[name_str].equal?(epoch)
180
+ end
181
+
182
+ # Resolved live at connect time: the composed kwarg wins, but a
183
+ # Manager built before wire_runtime_context still picks up the
184
+ # session guard once the context provides it (construction-time
185
+ # snapshots of coeffects go stale — matches base_ctx).
186
+ def resolve_guard(base_ctx)
187
+ @guard || (base_ctx ? Rubik::Coeffects.get(base_ctx, Rubik::Coeffects::GUARD) : nil)
188
+ end
189
+
170
190
  # Apply phase of enable_server: the post-connect half, extracted
171
191
  # verbatim — context wiring FIRST (so any later failure cleans up via
172
192
  # the child drain), proxy registration, registry flips, and the
@@ -245,7 +265,7 @@ module Rubik
245
265
  end
246
266
 
247
267
  @monitor.synchronize do
248
- if @stopped || @statuses[name_str] != :connecting || !@boot_marks[name_str].equal?(epoch)
268
+ unless boot_gate_open?(name_str, epoch)
249
269
  safely_stop_rolled_back_client(connected[:client], name_str)
250
270
  return @clients[name_str]
251
271
  end
@@ -338,181 +358,50 @@ module Rubik
338
358
 
339
359
  private
340
360
 
341
- # Config boot, decomposed into the named phases over the concurrency
342
- # boundary so N servers' connect handshakes (spawn + initialize +
343
- # tools/list — the slow I/O) overlap while every observable state
344
- # change stays sequential in config order. Boot wall clock goes
345
- # sum(per-server) → max(per-server); one hung server no longer delays
346
- # the rest. Pure orchestration below: each phase is a private method,
347
- # moved verbatim from the former single body (zero logic change).
361
+ # Config boot, decomposed into a BootRun (boot_run.rb): the five named
362
+ # boot phases moved verbatim onto a private run object that owns
363
+ # entries / connected / outcomes / boot_epoch, replacing the former
364
+ # side-channel parameters. N servers' connect handshakes (spawn +
365
+ # initialize + tools/list — the slow I/O) overlap while every
366
+ # observable state change stays sequential in config order (sum → max
367
+ # wall clock). This method only parses the config and opens the run.
348
368
  def perform_load_from_config(cfg = nil)
349
369
  target_config = cfg || @config
350
370
  return unless target_config
351
371
 
352
- mcp_enabled = target_config.fetch("mcp.enabled", true) != false
353
- servers_cfg = target_config.fetch("mcp.servers", {})
372
+ mcp_enabled = Rubik::Storage::Layout.config_get(target_config, "mcp.enabled", true) != false
373
+ servers_cfg = Rubik::Storage::Layout.config_get(target_config, "mcp.servers", {})
354
374
  return unless servers_cfg.is_a?(Hash)
355
375
 
356
- # Boot epoch (review round): :connecting marks are tagged with a
357
- # per-invocation identity so a wedged older boot's failure branch or
358
- # ensure sweep can never downgrade a newer boot's fresh mark.
359
- boot_epoch = Object.new
360
- entries = register_config_servers(servers_cfg, mcp_enabled, boot_epoch)
361
-
362
- # Phase 1 deliberately did no context wiring, so stopping the client
363
- # is the ONLY cleanup an unapplied prepared slot owes — no orphaned
364
- # child processes, even when an interrupt lands mid-boot. One ensure
365
- # spans both post-registration phases.
366
- connected = ::Array.new(entries.size)
367
- outcomes = {}
368
- begin
369
- connect_fanout(entries, connected)
370
- apply_connected_slots(entries, connected, boot_epoch, outcomes)
371
- ensure
372
- stop_unapplied_clients(entries, connected, outcomes)
373
- fail_orphaned_connecting_marks(entries, boot_epoch)
374
- end
375
- nil
376
- end
377
-
378
- # Phase 0 (sequential, config order): registration still goes
379
- # through add_server — subclasses and plugins wrap that seam, and a
380
- # broken definition must fail exactly as before (logged, skipped,
381
- # not half-registered). auto_start is deliberately false here: the
382
- # connect half moved to the fan-out. Ends each auto-start candidate
383
- # with the phase-0 claim (D5) and returns the entries to boot.
384
- def register_config_servers(servers_cfg, mcp_enabled, boot_epoch)
385
- entries = []
386
- servers_cfg.each do |sname, sdefn|
387
- next unless sdefn.is_a?(Hash)
388
-
389
- auto_start = mcp_enabled && (sdefn.fetch("enabled", true) != false)
390
- begin
391
- add_server(sname, sdefn, auto_start: false, persist: nil)
392
- rescue StandardError => e
393
- # ENSURE-LOG (G3 round-1): one broken server config used to
394
- # vanish behind an ENV["RUBIK_DEBUG"] gate; a misconfigured
395
- # server must surface without debug mode.
396
- Rubik::Log.warn(:mcp, "failed to load MCP server '#{sname}' (#{e.class}: #{e.message})")
397
- emit_mcp_event(:mcp_server_failed, :err, sname.to_s, error: e.message)
398
- next
399
- end
400
- next unless auto_start
401
-
402
- name_str = sname.to_s
403
- # Phase-0 claim (D5): check+mark in ONE monitor hold — a user
404
- # enable/disable/remove that races the boot reads a definite
405
- # state, and phase 2's gate below can never miss a window.
406
- entries << [name_str, sdefn.transform_keys(&:to_s)] unless claim_boot_slot!(name_str, boot_epoch)
407
- end
408
- entries
376
+ BootRun.new(self, servers_cfg, mcp_enabled).run
409
377
  end
410
378
 
411
- # Phase 1 (fan-out): pure I/O connects. Slots are captured by SIDE
412
- # EFFECT inside the item blocks: when an Exception-level error (a
413
- # boot interrupt) kills .map before it returns, the assignment below
414
- # never happens — a value-only `connected` would leave the ensure
415
- # blind to every successfully-started client. Hence the pre-built
416
- # array passed in rather than a returned value.
417
- def connect_fanout(entries, connected)
418
- return if entries.empty?
419
-
420
- Rubik::Concurrency.map(entries.each_with_index.to_a) do |(name_str, parsed_defn), index|
421
- connected[index] = { ok: true, **connect_server(name_str, parsed_defn) }
422
- rescue StandardError => e
423
- connected[index] = { ok: false, error: e }
424
- end
425
- end
426
-
427
- # Phase 2 (sequential, config order): apply state; identical
428
- # per-server warning on either phase's failure. Outcomes ledger is an
429
- # explicit atom map (index -> :applied | :failed; absence = skipped or
430
- # never ran — exactly what owes_stop? reads).
431
- def apply_connected_slots(entries, connected, boot_epoch, outcomes)
432
- entries.each_with_index do |(name_str, parsed_defn), index|
433
- slot = connected[index]
434
- next unless slot # never connected (interrupt during phase 1)
435
-
436
- if slot[:ok]
437
- begin
438
- outcome = @monitor.synchronize do
439
- # Single race gate (D5), checked at apply time under the
440
- # monitor: a disable/remove/enable during boot wins over
441
- # the boot's own apply, and a stop_all skips it entirely.
442
- # Epoch-complete gate: this boot may only apply into the
443
- # slot its own phase-0 claim marked, so a user decision
444
- # that moved the slot or a newer boot's fresh mark wins.
445
- # In the synchronous path nothing can interleave, so the
446
- # guard is provably inert there.
447
- if @stopped || @statuses[name_str] != :connecting || !@boot_marks[name_str].equal?(boot_epoch)
448
- :skipped
449
- else
450
- apply_connected_server(name_str, parsed_defn, client: slot[:client], tools: slot[:tools])
451
- # Post-apply revalidation under the boot actor: a no-op
452
- # when apply already landed :active.
453
- transition_server!(name_str, :active, epoch: boot_epoch)
454
- :applied
455
- end
456
- end
457
- outcomes[index] = :applied if outcome == :applied
458
- emit_mcp_event(:mcp_server_loaded, :info, name_str, tools: (@tools[name_str] || []).size) if outcome == :applied
459
- rescue StandardError => e
460
- # ENSURE-LOG (G3 round-1): one broken server config used to
461
- # vanish behind an ENV["RUBIK_DEBUG"] gate; a misconfigured
462
- # server must surface without debug mode. apply's own
463
- # rollback already set :failed and stopped its client via
464
- # the context drain; :failed marks the slot so the ensure
465
- # never double-stops it.
466
- outcomes[index] = :failed
467
- Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{e.class}: #{e.message})")
468
- emit_mcp_event(:mcp_server_failed, :err, name_str, error: e.message)
469
- end
379
+ # BootRun friend seam (boot_run.rb): the D5 race gate, one monitor
380
+ # hold deciding :skipped vs :applied. Moved verbatim — the hold
381
+ # boundary, the @stopped/@statuses/@boot_marks epoch gate and the
382
+ # guarded writes inside are exactly the former inline block; BootRun
383
+ # only receives the decision.
384
+ def boot_apply_gate(name_str, parsed_defn, boot_epoch, client:, tools:)
385
+ @monitor.synchronize do
386
+ # In the synchronous path nothing can interleave, so the
387
+ # gate is provably inert there.
388
+ if boot_gate_open?(name_str, boot_epoch)
389
+ apply_connected_server(name_str, parsed_defn, client: client, tools: tools)
390
+ # Post-apply revalidation under the boot actor: a no-op
391
+ # when apply already landed :active.
392
+ transition_server!(name_str, :active, epoch: boot_epoch)
393
+ :applied
470
394
  else
471
- # Only our own epoch's mark may flip to :failed — a user
472
- # disable/remove during the handshake, or a newer boot's fresh
473
- # mark, must win over this boot's connect failure (D5). The
474
- # epoch gate and flip happen in the same monitor hold inside
475
- # transition_server!.
476
- transition_server!(name_str, :failed, epoch: boot_epoch)
477
- error = slot[:error]
478
- Rubik::Log.warn(:mcp, "failed to load MCP server '#{name_str}' (#{error.class}: #{error.message})")
479
- emit_mcp_event(:mcp_server_failed, :err, name_str, error: error.message)
480
- end
481
- end
482
- end
483
-
484
- # Ensure part 1: stop every prepared client that did not end up
485
- # registered: a failed apply already rolled its own client back via
486
- # the context drain, while a never-applied one (interrupt between
487
- # phases) owes a stop here. A :skipped slot also owes a stop —
488
- # skipping the APPLY must not orphan its prepared child process (that
489
- # is the exact leak D10 closed for handshakes). owes_stop? is the
490
- # structural form of that rule: skipped and never-ran slots owe.
491
- def stop_unapplied_clients(entries, connected, outcomes)
492
- entries.each_with_index do |(name_str, _defn), index|
493
- slot = connected[index]
494
- next if !slot.is_a?(Hash) || !slot[:client] || !BootLifecycle.owes_stop?(outcomes[index])
495
-
496
- registered = @monitor.synchronize { @clients[name_str] }
497
- next if registered.equal?(slot[:client])
498
-
499
- begin
500
- slot[:client].stop
501
- rescue StandardError => e
502
- Rubik::Log.warn(:mcp, "failed to stop unapplied MCP server '#{name_str}' (#{e.class}: #{e.message})")
395
+ :skipped
503
396
  end
504
397
  end
505
398
  end
506
399
 
507
- # Ensure part 2 — terminal state for boot entries that never reached
508
- # apply (D6): anything still :connecting failed to boot; user-chosen
509
- # values (:disabled, :active, deleted-by-remove) are left untouched.
510
- # The epoch check inside transition_server! keeps an older wedged
511
- # boot's sweep from downgrading a newer boot's fresh mark.
512
- def fail_orphaned_connecting_marks(entries, boot_epoch)
513
- entries.each do |(name_str, _defn)|
514
- transition_server!(name_str, :failed, epoch: boot_epoch)
515
- end
400
+ # BootRun friend seam: the advisory @tools count read for the
401
+ # :mcp_server_loaded event — the same unguarded read the inline phase
402
+ # performed (observability only, never a state decision).
403
+ def tool_count(name_str)
404
+ (@tools[name_str] || []).size
516
405
  end
517
406
 
518
407
  public
@@ -159,6 +159,9 @@ module Rubik
159
159
 
160
160
  def close
161
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
162
165
  @mutex.synchronize do
163
166
  return if @closed
164
167
 
@@ -226,7 +229,7 @@ module Rubik
226
229
 
227
230
  if child_died
228
231
  hint = stderr_tail.last(5).join(" | ")[0, 512]
229
- Rubik::Log.warn(:mcp, "MCP child process (pid #{pid}) had already exited; recent stderr: #{hint}") unless hint.empty?
232
+ Rubik::Log.warn(:mcp, "MCP child process (pid #{exited_pid}) had already exited; recent stderr: #{hint}") unless hint.empty?
230
233
  end
231
234
 
232
235
  @mutex.synchronize do
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../log"
3
4
  require_relative "subagent/notices"
5
+ require_relative "../storage/layout"
4
6
 
5
7
  module Rubik
6
8
  module Orchestrator
@@ -58,7 +60,7 @@ module Rubik
58
60
  # overrides config resolution entirely — the StepExecutor injection
59
61
  # seam (its tests pass explicit modes positionally).
60
62
  def apply?(config, trigger:, subagent: false, tool: nil, mode: nil)
61
- return false if config.respond_to?(:fetch) && config.fetch("buffer.model_can_apply", true) == false
63
+ return false if Rubik::Storage::Layout.config_get(config, "buffer.model_can_apply", true) == false
62
64
 
63
65
  return false if trigger == :pre_execution && !tool.nil? && !test_class_tool?(tool)
64
66
 
@@ -108,11 +110,11 @@ module Rubik
108
110
  Subagent::Notices.emit(event_bus, Subagent::Notices::AUTO_APPLY_FAILED_EVENT,
109
111
  { id: id, role: role, state: state.to_s, reason: reason })
110
112
  else
111
- warn("[AutoApply] subagent '#{id}' auto-apply (#{trigger}) failed: #{reason}")
113
+ Rubik::Log.warn(:orchestrator, "[AutoApply] subagent '#{id}' auto-apply (#{trigger}) failed: #{reason}")
112
114
  end
113
115
  rescue StandardError => e
114
- warn("[AutoApply] subagent '#{id}' auto-apply (#{trigger}) failed: #{reason} " \
115
- "(notice emit also failed: #{e.class}: #{e.message})")
116
+ Rubik::Log.warn(:orchestrator, "[AutoApply] subagent '#{id}' auto-apply (#{trigger}) failed: #{reason} " \
117
+ "(notice emit also failed: #{e.class}: #{e.message})")
116
118
  end
117
119
  end
118
120
 
@@ -146,11 +148,11 @@ module Rubik
146
148
  # -- one defensive spelling each (replaced eight) -------------------
147
149
 
148
150
  def mode(config)
149
- config.respond_to?(:fetch) ? config.fetch(MODE_KEY, DEFAULT_MODE).to_s.downcase : DEFAULT_MODE
151
+ Rubik::Storage::Layout.config_get(config, MODE_KEY, DEFAULT_MODE).to_s.downcase
150
152
  end
151
153
 
152
154
  def subagent_flag_on?(config)
153
- config.respond_to?(:fetch) ? config.fetch(FLAG_KEY, DEFAULT_FLAG) == true : DEFAULT_FLAG
155
+ Rubik::Storage::Layout.config_get(config, FLAG_KEY, DEFAULT_FLAG) == true
154
156
  end
155
157
 
156
158
  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
- return default unless @config
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
@@ -87,6 +87,13 @@ module Rubik
87
87
  config_get(config, "goal.ledger.max_patch_retries", DEFAULT_MAX_RETRIES).to_i
88
88
  end
89
89
 
90
+ # Model-yüzü sözlük: SchemaVocabulary'ün PRODUCTION_SCHEMA'dan
91
+ # türettiği tek satır (frozen şema → memoize güvenli). Tool
92
+ # description'ı ile tükenme feedback'i bu tek türevi paylaşır.
93
+ def schema_vocabulary
94
+ @schema_vocabulary ||= SchemaVocabulary.render(PRODUCTION_SCHEMA)
95
+ end
96
+
90
97
  def create!(goal_text:, config: nil, store: nil, dmesg: nil, event_bus: nil)
91
98
  ledger = new(goal_text: goal_text.to_s, max_retries: max_patch_retries(config),
92
99
  store: store, dmesg: dmesg, event_bus: event_bus)
@@ -142,8 +149,12 @@ module Rubik
142
149
  # Ret sayacı ardışıktır; başarı sıfırlar; bütçe aşımında :exhausted
143
150
  # döner ve ESKİ Σ korunur (goal loop asla brick olmaz).
144
151
  def update(patch_or_text)
145
- verdict = Validator.validate(patch_or_text, schema: PRODUCTION_SCHEMA, current_doc: @doc)
146
152
  @monitor.synchronize do
153
+ # Validation reads @doc; keep it inside the monitor so a
154
+ # concurrent reader/writer cannot slip between validate and
155
+ # apply! (validation is pure — no IO — so holding the lock is
156
+ # free of deadlock risk).
157
+ verdict = Validator.validate(patch_or_text, schema: PRODUCTION_SCHEMA, current_doc: @doc)
147
158
  if verdict[0] == :ok
148
159
  apply!(verdict[1])
149
160
  elsif @rejections >= @max_retries
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module Orchestrator
5
+ module GoalLedger
6
+ # Model-yüzü sözlük türetme yüzeyi. SSOT disiplini: Σ şemasının tek
7
+ # gerçek kaynağı Ledger::PRODUCTION_SCHEMA'dır; enum/required/optional
8
+ # bilgisi buradan TEK noktadan türetilir ve üç statik teaching yüzeyine
9
+ # dağıtılır (tool description — yüklenme anı, Σ XML sayfası — her adım
10
+ # başı reinject, tükenme feedback'i). Elle yazılan enum/alan kopyası
11
+ # YOKTUR: şema değişirse sözlük değişir. Durum-bağımsızdır — rev/drift
12
+ # ile hiçbir ilişkisi olmadığı için bayatlayamaz (drift felsefesi:
13
+ # bayat teşhis bilgi zehridir; sözlük teşhis değil, gramerdir) ve tek
14
+ # sabit satır olduğu için pinli Σ satırını şişirmez.
15
+ module SchemaVocabulary
16
+ module_function
17
+
18
+ # Tek satırlık, deterministik sözlük: item-section'lar için alan
19
+ # mühürleri (* required, ? optional) ve enum dağarcığı, skalar
20
+ # listeler için tip. Hash ekleme sırası korunur → deterministik.
21
+ def render(schema)
22
+ sections = schema.sections.map do |name, section|
23
+ if section.scalar
24
+ "#{name}:string-list"
25
+ else
26
+ fields = section.item.fields.map do |fname, spec|
27
+ "#{fname}#{spec.optional ? '?' : '*'}#{":#{spec.enum.join('|')}" if spec.enum}"
28
+ end
29
+ "#{name}(#{fields.join(',')})"
30
+ end
31
+ end
32
+ "#{sections.join(' ')} — * required, ? optional; append items via /<section>/-, " \
33
+ "one op per item; wholesale section writes are rejected"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end