rubik 0.9.1 → 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.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +143 -1
  4. data/lib/rubik/adapter/base.rb +2 -1
  5. data/lib/rubik/adapter/process.rb +28 -67
  6. data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
  7. data/lib/rubik/buffer/apply_contract.rb +3 -3
  8. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  9. data/lib/rubik/buffer/manager.rb +26 -120
  10. data/lib/rubik/config/deep_ops.rb +4 -1
  11. data/lib/rubik/config/defaults.rb +6 -3
  12. data/lib/rubik/config/schema.rb +8 -0
  13. data/lib/rubik/config/secret_policy.rb +197 -0
  14. data/lib/rubik/config.rb +11 -0
  15. data/lib/rubik/diagnostics/collector.rb +0 -9
  16. data/lib/rubik/errors.rb +15 -0
  17. data/lib/rubik/event_bus.rb +2 -1
  18. data/lib/rubik/kernel/dispatcher.rb +12 -7
  19. data/lib/rubik/kernel/unit.rb +4 -1
  20. data/lib/rubik/llm/registry.rb +12 -9
  21. data/lib/rubik/llm/router.rb +2 -5
  22. data/lib/rubik/log.rb +21 -0
  23. data/lib/rubik/lsp/client.rb +77 -273
  24. data/lib/rubik/lsp/manager.rb +14 -10
  25. data/lib/rubik/lsp/transport.rb +365 -0
  26. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  27. data/lib/rubik/mcp/boot_run.rb +190 -0
  28. data/lib/rubik/mcp/manager.rb +40 -163
  29. data/lib/rubik/mcp/transport/stdio.rb +4 -1
  30. data/lib/rubik/orchestrator/auto_apply.rb +4 -3
  31. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  32. data/lib/rubik/orchestrator/goal_session.rb +53 -39
  33. data/lib/rubik/orchestrator/loop.rb +28 -47
  34. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  35. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  36. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  37. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  38. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  39. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  40. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  41. data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
  42. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  43. data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
  44. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  45. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  46. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  47. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  48. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  49. data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
  50. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  51. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  52. data/lib/rubik/platform/io_drain.rb +2 -1
  53. data/lib/rubik/platform/process.rb +144 -54
  54. data/lib/rubik/platform.rb +2 -1
  55. data/lib/rubik/plugin/manager.rb +2 -1
  56. data/lib/rubik/repl/commands/agent_context.rb +8 -7
  57. data/lib/rubik/repl/commands/agent_execution.rb +13 -32
  58. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  59. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  60. data/lib/rubik/repl/commands/base.rb +47 -2
  61. data/lib/rubik/repl/commands/buffer.rb +5 -4
  62. data/lib/rubik/repl/commands/lsp.rb +2 -1
  63. data/lib/rubik/repl/commands/pruner.rb +2 -1
  64. data/lib/rubik/repl/commands/secrets.rb +151 -0
  65. data/lib/rubik/repl/commands/workspace.rb +60 -150
  66. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  67. data/lib/rubik/repl/config_wiring.rb +3 -3
  68. data/lib/rubik/repl/hooks.rb +2 -1
  69. data/lib/rubik/repl/input_execution.rb +158 -76
  70. data/lib/rubik/repl/input_loop.rb +2 -1
  71. data/lib/rubik/repl/lifecycle.rb +59 -209
  72. data/lib/rubik/repl/notice_router.rb +319 -0
  73. data/lib/rubik/repl/paste_support.rb +3 -1
  74. data/lib/rubik/repl/presentation.rb +5 -3
  75. data/lib/rubik/repl/pruner/gate.rb +3 -1
  76. data/lib/rubik/repl/pruner/harness.rb +2 -1
  77. data/lib/rubik/repl/shell.rb +4 -3
  78. data/lib/rubik/repl/turn_recorder.rb +6 -5
  79. data/lib/rubik/runtime/discovery.rb +3 -2
  80. data/lib/rubik/runtime/loop_service.rb +4 -3
  81. data/lib/rubik/runtime/manifest_store.rb +9 -1
  82. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  83. data/lib/rubik/runtime/overlay_storage.rb +92 -146
  84. data/lib/rubik/runtime/task_manager.rb +64 -25
  85. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  86. data/lib/rubik/sandbox/runner.rb +16 -15
  87. data/lib/rubik/security/command_gate.rb +117 -93
  88. data/lib/rubik/security/guard.rb +6 -2
  89. data/lib/rubik/security/secret_store.rb +463 -0
  90. data/lib/rubik/security/synthesis_gate.rb +8 -11
  91. data/lib/rubik/session/manager.rb +7 -1
  92. data/lib/rubik/skills/manager.rb +2 -3
  93. data/lib/rubik/storage/search.rb +49 -0
  94. data/lib/rubik/storage/store.rb +13 -20
  95. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  96. data/lib/rubik/tools/base.rb +22 -0
  97. data/lib/rubik/tools/builtins/files.rb +3 -15
  98. data/lib/rubik/tools/builtins/grep.rb +1 -6
  99. data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
  100. data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
  101. data/lib/rubik/tools/truncator.rb +48 -36
  102. data/lib/rubik/ui/banner.rb +4 -3
  103. data/lib/rubik/ui/spinner.rb +3 -1
  104. data/lib/rubik/ui/statusline.rb +6 -5
  105. data/lib/rubik/version.rb +1 -1
  106. data/lib/rubik.rb +1 -2
  107. metadata +13 -1
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "monitor"
4
+
3
5
  require_relative "../coeffects"
4
6
  require_relative "../tools/result"
5
7
 
@@ -11,6 +13,11 @@ module Rubik
11
13
  # it keeps running in the background and its result is discarded — so
12
14
  # the budget bounds turn latency, not resource use. Budget off (nil)
13
15
  # yields inline: byte-identical to the unwrapped seam.
16
+ #
17
+ # Opt-in bound on the leak itself (issue #449): with `tools.timeout_kill_after`
18
+ # set, abandoned workers are tracked in a Monitor-synchronized registry and a
19
+ # reaper thread force-kills any worker still running once the grace elapses.
20
+ # Knob unset ⇒ pure abandonment, unchanged behavior.
14
21
  module ToolTimeout
15
22
  # Raised in the CALLER thread when the join budget expires. The
16
23
  # dispatch seam's rescue converts it into an honest error Result so
@@ -27,8 +34,17 @@ module Rubik
27
34
  end
28
35
 
29
36
  ENV_KEY = "RUBIK_TOOLS_CALL_TIMEOUT"
37
+ KILL_AFTER_CONFIG_KEY = "tools.timeout_kill_after"
30
38
  OFF = :off
31
39
 
40
+ # Grace-then-kill registry (issue #449): abandoned workers whose
41
+ # operator opted into the kill policy via `tools.timeout_kill_after`.
42
+ # Keyed by the worker thread; at most one reaper thread services the
43
+ # whole registry, exiting whenever it drains.
44
+ REGISTRY_MONITOR = Monitor.new
45
+ @registry = {} # worker Thread => {thread:, deadline:, tool_name:, budget:, grace:}
46
+ @reaper = nil
47
+
32
48
  module_function
33
49
 
34
50
  # Budget resolution precedence: an explicit env disable wins, then the
@@ -62,6 +78,12 @@ module Rubik
62
78
  # return. A kill landing mid-body still runs the ensure, which reads
63
79
  # as a nil result — the inline-parity residual this seam accepts.
64
80
  done = false
81
+ # Set by the caller when the join budget expires; read from the
82
+ # worker's ensure to log the late completion (see below). A worker
83
+ # finishing inside the join-expiry→flag window is not logged — the
84
+ # residual is bounded by the join granularity and costs nothing but
85
+ # one missing warn line.
86
+ abandoned = false
65
87
  t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
66
88
  worker = Thread.new do
67
89
  result = yield
@@ -69,16 +91,29 @@ module Rubik
69
91
  # raise-path semantics for ANY exception class, not just
70
92
  # StandardError. The re-raise carries the worker-side backtrace;
71
93
  # dispatch-site frames are not prepended (the tool's own failure
72
- # site is what matters for debugging).
94
+ # site is what production debugging cares about).
73
95
  rescue Exception => e # rubocop:disable Lint/RescueException
74
96
  error = e
75
97
  ensure
98
+ if abandoned
99
+ late = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
100
+ Rubik::Log.warn(:tools,
101
+ "tool '#{name}' call was abandoned at the #{budget}s budget but finished late " \
102
+ "after #{late.round(1)}s; its outcome (#{error ? "raise #{error.class}" : 'a result'}) was discarded")
103
+ end
76
104
  done = true
77
105
  end
78
106
  worker.name = "rubik-tool-#{name}"
79
107
 
80
108
  unless worker.join(budget)
109
+ abandoned = true
81
110
  elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
111
+ # Opt-in grace-then-kill (issue #449): with `tools.timeout_kill_after`
112
+ # set, the abandoned worker is handed to the reaper, which force-kills
113
+ # it once the grace elapses. Knob unset ⇒ pure abandonment (the
114
+ # late-completion warn below remains the only trail).
115
+ grace = kill_after_for(config)
116
+ register_for_kill(worker, tool_name: name, budget: budget, grace: grace) if grace
82
117
  raise BudgetExceeded.new(tool_name: name, budget_seconds: budget, elapsed_seconds: elapsed)
83
118
  end
84
119
  raise error if error
@@ -158,6 +193,91 @@ module Rubik
158
193
  nil
159
194
  end
160
195
 
196
+ # Grace-then-kill reaper knob at the config boundary (issue #449): the
197
+ # seconds an abandoned worker may keep running past its budget before
198
+ # the reaper force-kills it. The value IS the grace period — directly
199
+ # operator-set (e.g. `tools.timeout_kill_after: 30`), not derived from
200
+ # the budget. nil/absent = off, byte-identical to the pure-abandonment
201
+ # behavior. Mirrors resolve_config_budget: an invalid or non-positive
202
+ # value warns and lands on off — the knob must never fail a turn.
203
+ def kill_after_for(config)
204
+ raw = config&.fetch(KILL_AFTER_CONFIG_KEY, nil)
205
+ return nil if raw.nil?
206
+
207
+ value = parse_seconds(raw)
208
+ return value if value&.positive?
209
+
210
+ Rubik::Log.warn(:tools, "ignoring invalid #{KILL_AFTER_CONFIG_KEY} #{raw.inspect} (expected a positive number); treating as off")
211
+ nil
212
+ end
213
+
214
+ # Hands an abandoned worker to the grace-then-kill reaper and ensures
215
+ # exactly one reaper thread is parked on the registry.
216
+ def register_for_kill(thread, tool_name:, budget:, grace:)
217
+ REGISTRY_MONITOR.synchronize do
218
+ @registry[thread] = { thread: thread, deadline: Time.now + grace,
219
+ tool_name: tool_name, budget: budget, grace: grace }
220
+ @reaper = spawn_reaper if @reaper.nil? || !@reaper.alive?
221
+ end
222
+ end
223
+
224
+ # Sleeps until the earliest deadline, reaps, repeats; exits as soon as
225
+ # the registry drains so no reaper thread lingers between timeouts.
226
+ def spawn_reaper
227
+ Thread.new do
228
+ Thread.current.name = "rubik-tool-reaper"
229
+ loop do
230
+ deadline = REGISTRY_MONITOR.synchronize do
231
+ earliest = @registry.values.map { |entry| entry[:deadline] }.min
232
+ # Announce the exit under the lock so the next registration
233
+ # spawns a fresh reaper instead of trusting a dying thread.
234
+ @reaper = nil if earliest.nil?
235
+ earliest
236
+ end
237
+ break if deadline.nil?
238
+
239
+ delay = deadline - Time.now
240
+ sleep(delay) if delay.positive?
241
+ reap_expired
242
+ end
243
+ end
244
+ end
245
+
246
+ # Removes every entry whose grace has elapsed: dead workers need no
247
+ # kill (the abandoned-worker ensure already logged their late
248
+ # completion), still-running ones are force-killed and the kill is
249
+ # logged under the orchestrator's "tools" tag.
250
+ def reap_expired
251
+ expired = REGISTRY_MONITOR.synchronize do
252
+ now = Time.now
253
+ due = @registry.select { |_thread, entry| entry[:deadline] <= now }
254
+ due.each_key { |thread| @registry.delete(thread) }
255
+ due.values
256
+ end
257
+ expired.each do |entry|
258
+ next unless entry[:thread].alive?
259
+
260
+ begin
261
+ entry[:thread].kill
262
+ rescue StandardError
263
+ nil
264
+ end
265
+ Rubik::Log.warn(:tools, "tool '#{entry[:tool_name]}' worker was still running past its " \
266
+ "#{entry[:budget]}s budget after the #{entry[:grace]}s grace " \
267
+ "(tools.timeout_kill_after); killed it")
268
+ end
269
+ end
270
+
271
+ # Test/observability doors: a snapshot of the pending kill registry and
272
+ # whether a reaper thread is currently parked on it.
273
+ def pending_kills
274
+ REGISTRY_MONITOR.synchronize { @registry.values.map(&:dup) }
275
+ end
276
+
277
+ def reaper_alive?
278
+ REGISTRY_MONITOR.synchronize { @reaper.is_a?(Thread) && @reaper.alive? }
279
+ end
280
+
161
281
  # Canonical tool name => seconds. 0/false disables the budget for that
162
282
  # tool (the per-tool escape hatch); a positive value replaces the
163
283
  # global budget; garbage warns and inherits. Lookup uses key presence,
@@ -5,7 +5,8 @@ module Rubik
5
5
  # Continuously drains a child-process IO (stderr) so a chatty child
6
6
  # never fills the OS pipe buffer (~64KB) and deadlocks writing
7
7
  # diagnostics while the parent waits on stdout. Discards content; the
8
- # optional on_line observer is a future debug-forwarding hook.
8
+ # optional on_line observer forwards each drained line when given
9
+ # (implemented and test-covered; currently unused in production).
9
10
  # Lifecycle contract mirrors the existing reader threads (MCP stdio,
10
11
  # LSP stdout): kill + bounded join, never raises from #stop.
11
12
  class IoDrain
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "open3"
4
4
  require "timeout"
5
+ require_relative "../log"
5
6
 
6
7
  module Rubik
7
8
  module Platform
@@ -12,53 +13,106 @@ module Rubik
12
13
  base.extend self
13
14
  end
14
15
 
15
- # Execute a command via Open3.popen3 with timeout, concurrent stream reads,
16
- # signal exit status mapping, and automatic process-tree termination on timeout.
17
- def popen_capture(*, timeout: 15, chdir: Dir.pwd, timeout_message: nil, error_message: nil)
16
+ # Default swallowed-IO-error observer for the popen3 drain core
17
+ # (issue #444): reader-thread deaths are now visible via Log.debug
18
+ # (adopting the adapter variant's log-on-swallow behavior) instead of
19
+ # being swallowed silently, while stdin-close and reader-thread kill
20
+ # failures stay silent teardown noise, as before. Log is error-isolated,
21
+ # so the observer itself can never raise.
22
+ DEFAULT_ON_IO_ERROR = lambda do |event, error|
23
+ case event
24
+ when :stdout_read
25
+ Rubik::Log.debug(:platform, "stdout read failed during process execution: #{error.class}: #{error.message}")
26
+ when :stderr_read
27
+ Rubik::Log.debug(:platform, "stderr read failed during process execution: #{error.class}: #{error.message}")
28
+ end
29
+ end
30
+
31
+ # Single-homed popen3 + dual reader-thread drain core (issue #444):
32
+ # executes a command via Open3.popen3 with timeout, concurrent
33
+ # stdout/stderr drain (prevents the OS pipe buffer from filling and
34
+ # deadlocking the child), signal exit status mapping, and automatic
35
+ # process-tree termination on timeout. Consumers: sandbox drivers and
36
+ # Rubik::Adapter::ProcessRunner, which overrides on_io_error to keep
37
+ # its legacy Log.debug(:adapter) trace of every swallowed IO error.
38
+ #
39
+ # on_io_error is invoked as (event, exception) for each IO error the
40
+ # core must swallow to keep draining: :stdin_close, :stdout_read,
41
+ # :stderr_read, :stdout_kill, :stderr_kill. For long-lived
42
+ # line-oriented drains (LSP stderr) use Platform::IoDrain instead;
43
+ # this core is the synchronous capture-to-string variant.
44
+ def popen_capture(*, timeout: 15, chdir: Dir.pwd, timeout_message: nil, error_message: nil, on_io_error: nil)
45
+ on_io_error ||= DEFAULT_ON_IO_ERROR
18
46
  start_time = Time.now
19
47
  stdout_str = ""
20
48
  stderr_str = ""
21
49
  exit_code = 127
22
50
  pid = nil
51
+ watchdog = nil
52
+ deadline_hit = false
23
53
 
24
54
  begin
25
- Timeout.timeout(timeout) do
26
- Open3.popen3(*, chdir: chdir) do |stdin, stdout, stderr, wait_thr|
27
- pid = wait_thr.pid if wait_thr.respond_to?(:pid)
55
+ Open3.popen3(*, chdir: chdir) do |stdin, stdout, stderr, wait_thr|
56
+ pid = wait_thr.pid if wait_thr.respond_to?(:pid)
57
+ # Deadline watchdog (issue #479): kill the tree from a separate
58
+ # thread the moment the deadline fires, so a hung child never
59
+ # outlives `timeout`. The kill must NOT ride Timeout.timeout's
60
+ # timer thread: its unwind races the kill, and if the unwind wins,
61
+ # popen3's ensure joins the still-running child and the timeout
62
+ # merely relabels the result instead of bounding the wall clock.
63
+ # Instead the watchdog kills first and sets deadline_hit, and the
64
+ # Timeout::Error is raised only after the block has returned.
65
+ # Cancelled in the inner ensure to keep the watchdog from firing
66
+ # into a recycled pid after normal exit.
67
+ watchdog = Thread.new(pid, timeout.to_f) do |w_pid, w_deadline|
68
+ sleep w_deadline
69
+ deadline_hit = true
70
+ kill_tree(w_pid)
71
+ rescue StandardError
72
+ deadline_hit = true
73
+ nil
74
+ end
75
+ begin
76
+ stdin.close
77
+ rescue StandardError => e
78
+ on_io_error.call(:stdin_close, e)
79
+ end
80
+ t_out = Thread.new do
81
+ stdout.read
82
+ rescue StandardError => e
83
+ on_io_error.call(:stdout_read, e)
84
+ ""
85
+ end
86
+ t_err = Thread.new do
87
+ stderr.read
88
+ rescue StandardError => e
89
+ on_io_error.call(:stderr_read, e)
90
+ ""
91
+ end
92
+ begin
93
+ stdout_str = t_out.value.to_s
94
+ stderr_str = t_err.value.to_s
95
+ status = wait_thr.value
96
+ exit_code = status.exitstatus || (status.signaled? ? (128 + (status.termsig || 1)) : 1)
97
+ ensure
28
98
  begin
29
- stdin.close
99
+ watchdog&.kill
30
100
  rescue StandardError
31
101
  nil
32
102
  end
33
- t_out = Thread.new do
34
- stdout.read
35
- rescue StandardError
36
- ""
37
- end
38
- t_err = Thread.new do
39
- stderr.read
40
- rescue StandardError
41
- ""
103
+ begin
104
+ t_out.kill
105
+ rescue StandardError => e
106
+ on_io_error.call(:stdout_kill, e)
42
107
  end
43
108
  begin
44
- stdout_str = t_out.value.to_s
45
- stderr_str = t_err.value.to_s
46
- status = wait_thr.value
47
- exit_code = status.exitstatus || (status.signaled? ? (128 + (status.termsig || 1)) : 1)
48
- ensure
49
- begin
50
- t_out.kill
51
- rescue StandardError
52
- nil
53
- end
54
- begin
55
- t_err.kill
56
- rescue StandardError
57
- nil
58
- end
109
+ t_err.kill
110
+ rescue StandardError => e
111
+ on_io_error.call(:stderr_kill, e)
59
112
  end
60
113
  end
61
114
  end
115
+ raise Timeout::Error, "execution expired" if deadline_hit
62
116
  rescue Timeout::Error
63
117
  kill_tree(pid) if pid
64
118
  stderr_str = timeout_message || "Execution timed out after #{timeout} seconds"
@@ -97,33 +151,31 @@ module Rubik
97
151
  end
98
152
  else
99
153
  # On Unix/Linux, collect descendant PIDs so child processes are not
100
- # orphaned when the parent process is killed.
101
- descendants = descendant_pids(pid)
102
- descendants.each do |cpid|
103
- begin
104
- ::Process.kill("TERM", cpid)
105
- rescue StandardError
106
- nil
107
- end
108
- begin
109
- ::Process.kill("KILL", cpid)
110
- rescue StandardError
111
- nil
112
- end
113
- end
114
- begin
115
- ::Process.kill("TERM", pid)
116
- rescue StandardError
117
- nil
118
- end
119
- begin
120
- ::Process.kill("KILL", pid)
121
- rescue StandardError
122
- nil
154
+ # orphaned when the parent process is killed. TERM gets a short
155
+ # grace window before KILL escalates: back-to-back TERM→KILL gave
156
+ # graceful shutdown no scheduling window, so lock/file/socket
157
+ # holders were always hard-killed.
158
+ targets = (descendant_pids(pid) + [pid]).uniq
159
+ signal_all(targets, "TERM")
160
+
161
+ # Escalate to KILL for whatever is still alive once the grace
162
+ # window lapses (or the loop observes all targets gone early).
163
+ # ::Process, not Process: this file defines Rubik::Platform::Process,
164
+ # which would otherwise shadow the top-level constant.
165
+ deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + KILL_TREE_GRACE_SECONDS
166
+ still_alive = targets
167
+ until ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) >= deadline
168
+ still_alive = targets.select { |tpid| process_alive?(tpid) }
169
+ break if still_alive.empty?
170
+
171
+ sleep 0.01
123
172
  end
173
+ signal_all(still_alive, "KILL")
124
174
  end
125
175
  end
126
176
 
177
+ KILL_TREE_GRACE_SECONDS = 0.2
178
+
127
179
  # Collect all descendant process IDs recursively (leaves first).
128
180
  def descendant_pids(pid)
129
181
  return [] unless pid&.positive?
@@ -145,8 +197,46 @@ module Rubik
145
197
  []
146
198
  end
147
199
 
200
+ # Deadline-bounded write to a (possibly blocking) pipe: converts a
201
+ # never-draining reader — a child that stopped reading stdin — into an
202
+ # Errno::EPIPE raise after `timeout` seconds instead of parking the
203
+ # caller forever on a full OS pipe buffer. Uses write_nonblock + wait,
204
+ # so the fd's blocking flag is irrelevant. Exposed via Rubik::Platform.
205
+ def write_until(io, text, timeout: 5.0)
206
+ deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + timeout.to_f
207
+ text = text.to_s
208
+ until text.empty?
209
+ written = io.write_nonblock(text, exception: false)
210
+ case written
211
+ when :wait_writable
212
+ remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
213
+ raise Errno::EPIPE, "pipe write exceeded #{timeout}s deadline" if remaining <= 0
214
+
215
+ io.wait_writable(remaining) or raise Errno::EPIPE, "pipe write exceeded #{timeout}s deadline"
216
+ when Integer
217
+ text = text.byteslice(written..)
218
+ end
219
+ end
220
+ nil
221
+ end
222
+
148
223
  private
149
224
 
225
+ def signal_all(pids, sig)
226
+ pids.each do |tpid|
227
+ ::Process.kill(sig, tpid)
228
+ rescue StandardError
229
+ nil
230
+ end
231
+ end
232
+
233
+ def process_alive?(pid)
234
+ ::Process.kill(0, pid)
235
+ true
236
+ rescue StandardError
237
+ false
238
+ end
239
+
150
240
  def direct_child_pids(pid)
151
241
  task_dir = "/proc/#{pid}/task"
152
242
  if File.directory?(task_dir)
@@ -7,6 +7,7 @@ require "cgi"
7
7
  require "json"
8
8
 
9
9
  require_relative "platform/path"
10
+ require_relative "storage/layout"
10
11
  require_relative "platform/process"
11
12
  require_relative "platform/filesystem"
12
13
  require_relative "platform/sanitize"
@@ -40,7 +41,7 @@ module Rubik
40
41
  # resolves false so tests warm/boot synchronously. Consumers:
41
42
  # repl/lifecycle.rb#warm_up! (ast.async_warm, mcp.async_load).
42
43
  def async_knob(config, key)
43
- value = config.fetch(key, nil)
44
+ value = Rubik::Storage::Layout.config_get(config, key, nil)
44
45
  value = ENV["RUBIK_TEST"] != "1" if value.nil?
45
46
  value
46
47
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "logger"
4
+ require_relative "../log"
4
5
  require_relative "requires_dag"
5
6
 
6
7
  module Rubik
@@ -8,7 +9,7 @@ module Rubik
8
9
  class Manager
9
10
  attr_reader :registered_plugins, :aliases
10
11
 
11
- def initialize(logger: Logger.new($stderr))
12
+ def initialize(logger: Rubik::Log.forwarder(:plugin_manager))
12
13
  @logger = logger
13
14
  @registered_plugins = []
14
15
  @aliases = {}
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "base"
4
+ require_relative "../../storage/layout"
4
5
  require_relative "../step_formatter"
5
6
  require_relative "../../prompt"
6
7
  require_relative "../../tools/diagnostic_reporter"
@@ -102,7 +103,7 @@ module Rubik
102
103
  spec = ctx.current_model_spec
103
104
  context_limit = ctx.llm_registry.context_limit_for(spec["provider"], spec["name"])
104
105
  max_output = ctx.llm_registry.max_output_tokens_for(spec["provider"], spec["name"])
105
- context_cfg = ctx.config.fetch("context", {})
106
+ context_cfg = Rubik::Storage::Layout.config_get(ctx.config, "context", {})
106
107
 
107
108
  ctx.conversation_history = Rubik::Prompt::ContextCompactor.compact_history(
108
109
  ctx.conversation_history,
@@ -153,7 +154,7 @@ module Rubik
153
154
  context_limit = ctx.llm_registry.context_limit_for(p_name, m_name)
154
155
  max_output = ctx.llm_registry.max_output_tokens_for(p_name, m_name)
155
156
  ceiling = ctx.llm_registry.effective_input_limit_for(p_name, m_name)
156
- context_cfg = ctx.config.fetch("context", {})
157
+ context_cfg = Rubik::Storage::Layout.config_get(ctx.config, "context", {})
157
158
  strategy = context_cfg["compaction_strategy"] || "hybrid"
158
159
 
159
160
  est_tokens = Rubik::Prompt::TokenEstimator.estimate_history(ctx.conversation_history)
@@ -191,11 +192,11 @@ module Rubik
191
192
  scope, clean_args = Base.parse_scope(args)
192
193
 
193
194
  if clean_args.empty? || clean_args.first.downcase == "status"
194
- cur_max = ctx.config.fetch("agent.max_steps", 40)
195
- goal_max = ctx.config.fetch("agent.goal_max_steps", 100)
196
- plan_max = ctx.config.fetch("agent.plan_max_steps", 15)
197
- warn_r = ctx.config.fetch("agent.warn_at_ratio", 0.75)
198
- ask_c = ctx.config.fetch("agent.ask_continue_on_limit", true)
195
+ cur_max = Rubik::Storage::Layout.config_get(ctx.config, "agent.max_steps", 40)
196
+ goal_max = Rubik::Storage::Layout.config_get(ctx.config, "agent.goal_max_steps", 100)
197
+ plan_max = Rubik::Storage::Layout.config_get(ctx.config, "agent.plan_max_steps", 15)
198
+ warn_r = Rubik::Storage::Layout.config_get(ctx.config, "agent.warn_at_ratio", 0.75)
199
+ ask_c = Rubik::Storage::Layout.config_get(ctx.config, "agent.ask_continue_on_limit", true)
199
200
 
200
201
  ctx.out.puts "=== Agentic Step Budget Configuration ==="
201
202
  ctx.out.puts " • Default Task Max Steps : #{cur_max}"
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "base"
4
+ require_relative "../../storage/layout"
4
5
  require_relative "../../runtime"
5
6
  require_relative "../../sandbox"
6
7
  require_relative "../../orchestrator/auto_apply"
@@ -39,7 +40,7 @@ module Rubik
39
40
  next
40
41
  end
41
42
 
42
- plan_steps = ctx.config.fetch("agent.plan_max_steps", 15).to_i
43
+ plan_steps = Rubik::Storage::Layout.config_get(ctx.config, "agent.plan_max_steps", 15).to_i
43
44
  routed = ctx.router.route(task_type: :plan, prompt: task)
44
45
  ctx.runtime_manager.provide(:llm_client, routed[:provider]) if routed[:provider]
45
46
  orchestrator = ctx.orchestrator_loop(
@@ -129,33 +130,21 @@ module Rubik
129
130
 
130
131
  private_class_method def self.register_test_command(shell)
131
132
  shell.register_command("test", description: "Run test suite via active language adapter") do |args, ctx|
132
- if ctx.buffer_manager&.dirty?
133
- if Rubik::Orchestrator::AutoApply.apply?(ctx.config, trigger: :pre_execution)
134
- applied = Rubik::Orchestrator::AutoApply.apply_and_sync(ctx.buffer_manager, ctx)
135
- ctx.out.puts Rubik::Orchestrator::AutoApply.applied_line(applied.size, "test run")
136
- else
137
- ctx.out.puts Rubik::Orchestrator::AutoApply.unapplied_warning("Tests")
138
- end
139
- end
140
- lang = args.first || Rubik::Runtime::Discovery.detect_primary_language(Dir.pwd) || "ruby"
141
- adapter = ctx.adapter_registry.get(lang) || ctx.adapter_registry.get("ruby")
133
+ Base.pre_execution_apply(ctx, "test run", "Tests")
134
+ adapter = Base.adapter_for(ctx, args.first)
142
135
  res = adapter.run_tests(Dir.pwd)
143
136
  ctx.out.puts res[:output]
144
- if !res[:ok] && res[:diagnostic]
145
- ctx.out.puts "\n🔍 [Diagnostic] #{res[:diagnostic].category.to_s.upcase}: #{res[:diagnostic].suggestion}"
146
- ctx.out.puts "Tip: Use `repair_runtime_service` (or `fix_service`) to adapt test runners dynamically."
147
- end
137
+ Base.render_diagnostic(ctx, res, tip: "Tip: Use `repair_runtime_service` (or `fix_service`) to adapt test runners dynamically.")
148
138
  end
149
139
  end
150
140
 
151
141
  private_class_method def self.register_lint_command(shell)
152
142
  shell.register_command("lint", description: "Run linter/syntax check", audience: :both) do |args, ctx|
153
143
  target = args.first || "lib/rubik.rb"
154
- lang = Rubik::Runtime::Discovery.detect_primary_language(Dir.pwd) || "ruby"
155
- adapter = ctx.adapter_registry.get(lang) || ctx.adapter_registry.get("ruby")
144
+ adapter = Base.adapter_for(ctx)
156
145
  res = adapter.check_syntax(target)
157
146
  ctx.out.puts res[:output]
158
- ctx.out.puts "\n🔍 [Diagnostic] #{res[:diagnostic].category.to_s.upcase}: #{res[:diagnostic].suggestion}" if !res[:ok] && res[:diagnostic]
147
+ Base.render_diagnostic(ctx, res)
159
148
  end
160
149
  end
161
150
 
@@ -171,25 +160,17 @@ module Rubik
171
160
  next
172
161
  end
173
162
  end
174
- lang = Rubik::Runtime::Discovery.detect_primary_language(Dir.pwd) || "ruby"
175
- adapter = ctx.adapter_registry.get(lang) || ctx.adapter_registry.get("ruby")
163
+ adapter = Base.adapter_for(ctx)
176
164
  res = adapter.format_code(target)
177
165
  ctx.out.puts res[:output]
178
- ctx.out.puts "\n🔍 [Diagnostic] #{res[:diagnostic].category.to_s.upcase}: #{res[:diagnostic].suggestion}" if !res[:ok] && res[:diagnostic]
166
+ Base.render_diagnostic(ctx, res)
179
167
  end
180
168
  end
181
169
  end
182
170
 
183
171
  private_class_method def self.register_sh_command(shell)
184
172
  shell.register_command("sh", description: "Execute shell command via sandbox runner") do |args, ctx|
185
- if ctx.buffer_manager&.dirty?
186
- if Rubik::Orchestrator::AutoApply.apply?(ctx.config, trigger: :pre_execution)
187
- applied = Rubik::Orchestrator::AutoApply.apply_and_sync(ctx.buffer_manager, ctx)
188
- ctx.out.puts Rubik::Orchestrator::AutoApply.applied_line(applied.size, "command execution")
189
- else
190
- ctx.out.puts Rubik::Orchestrator::AutoApply.unapplied_warning("Commands")
191
- end
192
- end
173
+ Base.pre_execution_apply(ctx, "command execution", "Commands")
193
174
 
194
175
  cmd = args.join(" ")
195
176
  ctx.guard.verify_command_safety!(cmd)
@@ -208,10 +189,10 @@ module Rubik
208
189
  registered_keys = registered_map.keys.uniq
209
190
 
210
191
  if clean_args.empty? || subcmd == "status"
211
- enabled = ctx.config.fetch("sandbox.enabled", false)
192
+ enabled = Rubik::Storage::Layout.config_get(ctx.config, "sandbox.enabled", false)
212
193
  state_str = enabled ? "Açık (enabled)" : "Kapalı (disabled - direct safe execution)"
213
- driver_name = ctx.sandbox_runner&.driver&.name || ctx.config.fetch("sandbox.driver", "bwrap")
214
- net_str = ctx.config.fetch("sandbox.allow_network", false) ? "allowed" : "blocked"
194
+ driver_name = ctx.sandbox_runner&.driver&.name || Rubik::Storage::Layout.config_get(ctx.config, "sandbox.driver", "bwrap")
195
+ net_str = Rubik::Storage::Layout.config_get(ctx.config, "sandbox.allow_network", false) ? "allowed" : "blocked"
215
196
 
216
197
  ctx.out.puts "=== Sandbox Durumu ==="
217
198
  ctx.out.puts "Durum: #{state_str}"
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "base"
4
+ require_relative "../../storage/layout"
4
5
  require_relative "../../llm"
5
6
 
6
7
  module Rubik
@@ -17,7 +18,7 @@ module Rubik
17
18
  # Extract registered providers from registry and config
18
19
  registered_providers = []
19
20
  registered_providers += ctx.llm_registry.list if ctx.llm_registry
20
- registered_providers += ctx.config.fetch("providers", {}).keys if ctx.config
21
+ registered_providers += Rubik::Storage::Layout.config_get(ctx.config, "providers", {}).keys if ctx.config
21
22
  registered_providers = registered_providers.map { |p| p.to_s.downcase }.uniq
22
23
 
23
24
  # 1. Explicit provider prefix with colon: e.g. "openrouter:anthropic/claude-3.5-sonnet"
@@ -73,7 +74,7 @@ module Rubik
73
74
 
74
75
  registered_providers = []
75
76
  registered_providers += ctx.llm_registry.list if ctx.llm_registry
76
- registered_providers += ctx.config.fetch("providers", {}).keys if ctx.config
77
+ registered_providers += Rubik::Storage::Layout.config_get(ctx.config, "providers", {}).keys if ctx.config
77
78
  registered_providers = registered_providers.map { |p| p.to_s.downcase }.uniq
78
79
 
79
80
  # 1. If 2 or more space-separated arguments: e.g. ["openrouter", "deepseek/deepseek-v4-flash"]
@@ -111,7 +112,7 @@ module Rubik
111
112
  end
112
113
 
113
114
  private_class_method def self.handle_list_models(ctx)
114
- mode = ctx.config.fetch("router.mode", "auto")
115
+ mode = Rubik::Storage::Layout.config_get(ctx.config, "router.mode", "auto")
115
116
  def_spec = ctx.config.model_spec(:default)
116
117
  fast_spec = ctx.config.model_spec(:fast)
117
118
  adv_spec = ctx.config.model_spec(:advanced)