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
@@ -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 = {}
@@ -79,10 +79,10 @@ module Rubik
79
79
  when :verified
80
80
  :verified
81
81
  when :drift
82
- warn "[SECURITY] Integrity check failed for plugin '#{basename}': file modified since adding."
82
+ Rubik::Log.warn(:plugin, "[SECURITY] Integrity check failed for plugin '#{basename}': file modified since adding.")
83
83
  :rejected
84
84
  when :unknown
85
- warn "[SECURITY] Unknown plugin file '#{basename}' not in manifest. Skipping."
85
+ Rubik::Log.warn(:plugin, "[SECURITY] Unknown plugin file '#{basename}' not in manifest. Skipping.")
86
86
  :rejected
87
87
  end
88
88
  end
@@ -247,8 +247,8 @@ module Rubik
247
247
  entry = load_manifest(dir)[name]
248
248
 
249
249
  unless entry.is_a?(Hash) && entry["type"] == "directory" && entry["files"].is_a?(Hash)
250
- warn "[SECURITY] Directory plugin '#{name}' has no package inventory in the manifest " \
251
- "(legacy manifest). Skipping; re-add it with '!plugin add' or set RUBIK_TRUST_OVERLAYS=1 to trust it."
250
+ Rubik::Log.warn(:plugin, "[SECURITY] Directory plugin '#{name}' has no package inventory in the manifest " \
251
+ "(legacy manifest). Skipping; re-add it with '!plugin add' or set RUBIK_TRUST_OVERLAYS=1 to trust it.")
252
252
  return :rejected
253
253
  end
254
254
 
@@ -256,16 +256,16 @@ module Rubik
256
256
  begin
257
257
  current = PackageInventory.inventory(package_dir)
258
258
  rescue PackageInventory::Violation => e
259
- warn "[SECURITY] Integrity check failed for plugin package '#{name}': #{e.message}. " \
260
- "Re-add it with '!plugin add' or set RUBIK_TRUST_OVERLAYS=1 to trust it."
259
+ Rubik::Log.warn(:plugin, "[SECURITY] Integrity check failed for plugin package '#{name}': #{e.message}. " \
260
+ "Re-add it with '!plugin add' or set RUBIK_TRUST_OVERLAYS=1 to trust it.")
261
261
  return :rejected
262
262
  end
263
263
 
264
264
  drift = PackageInventory.drift(entry["files"], current)
265
265
  return :verified if drift.empty?
266
266
 
267
- warn "[SECURITY] Integrity check failed for plugin package '#{name}': #{drift}. " \
268
- "Re-add it with '!plugin add' or set RUBIK_TRUST_OVERLAYS=1 to trust it."
267
+ Rubik::Log.warn(:plugin, "[SECURITY] Integrity check failed for plugin package '#{name}': #{drift}. " \
268
+ "Re-add it with '!plugin add' or set RUBIK_TRUST_OVERLAYS=1 to trust it.")
269
269
  :rejected
270
270
  end
271
271
  end
@@ -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)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "base"
4
4
  require_relative "../../llm"
5
+ require_relative "secrets"
5
6
 
6
7
  module Rubik
7
8
  module REPL
@@ -19,6 +20,7 @@ module Rubik
19
20
  def self.register(shell)
20
21
  register_provider_command(shell)
21
22
  register_providers_command(shell)
23
+ register_secrets_command(shell)
22
24
  end
23
25
 
24
26
  private_class_method def self.register_provider_command(shell)
@@ -111,6 +113,7 @@ module Rubik
111
113
 
112
114
  ctx.out.puts "Successfully added provider '#{p_name}' (#{p_type} -> #{normalized_endpoint}) " \
113
115
  "with model '#{m_name}' [#{Base.scope_label(scope)}]."
116
+ render_secret_stored_line(res, ctx) if api_key
114
117
  end
115
118
 
116
119
  private_class_method def self.handle_add_provider_model(clean_args, ctx, scope)
@@ -163,7 +166,11 @@ module Rubik
163
166
  return
164
167
  end
165
168
 
166
- p_spec = ctx.config.provider_spec(p_name)
169
+ # provider_spec returns the @data-resident Hash: dup before
170
+ # mutating, so a refused write (e.g. :project plaintext secret)
171
+ # never leaves plaintext behind in runtime memory or poisons the
172
+ # next whole-Hash write of an unrelated field.
173
+ p_spec = ctx.config.provider_spec(p_name)&.dup
167
174
  if p_spec.nil?
168
175
  ctx.out.puts "Provider '#{p_name}' not found."
169
176
  return
@@ -194,7 +201,22 @@ module Rubik
194
201
  ctx.refresh_llm_registry!
195
202
  return if Base.report_persist_failure(ctx, res, scope)
196
203
 
197
- ctx.out.puts "Updated provider '#{p_name}' field '#{field}' to '#{val}' [#{Base.scope_label(scope)}]."
204
+ shown_val = ["key", "api_key"].include?(field) ? Rubik::Security::Redactor::MASK_STRING : val
205
+ ctx.out.puts "Updated provider '#{p_name}' field '#{field}' to '#{shown_val}' [#{Base.scope_label(scope)}]."
206
+ render_secret_stored_line(res, ctx) if ["key", "api_key", "key-env", "key_env", "api_key_env"].include?(field)
207
+ end
208
+
209
+ # One honest line about where the credential went: the config file
210
+ # holds only a rubik-secret: pointer when the store took the value.
211
+ private_class_method def self.render_secret_stored_line(res, ctx)
212
+ info = res[:secret]
213
+ return if info.nil?
214
+
215
+ if info[:stored]
216
+ ctx.out.puts " API key stored via #{info[:backend]} secret store; the config file holds only a reference."
217
+ elsif info[:warning]
218
+ ctx.out.puts " Warning: #{info[:warning]}"
219
+ end
198
220
  end
199
221
 
200
222
  private_class_method def self.handle_remove_provider(clean_args, ctx, scope)
@@ -207,6 +229,14 @@ module Rubik
207
229
  res = ctx.config.remove_provider(p_name, persist: scope)
208
230
  ctx.refresh_llm_registry!
209
231
  if res[:ok]
232
+ # Best-effort hygiene: drop the provider's stored secret in both
233
+ # scope slots (the write guard suffixes project-scope names).
234
+ begin
235
+ Rubik::Security::SecretStore.delete("providers.#{p_name}.api_key")
236
+ Rubik::Security::SecretStore.delete("providers.#{p_name}.api_key.project")
237
+ rescue Rubik::Security::SecretStore::Error
238
+ nil
239
+ end
210
240
  ctx.out.puts "Removed provider '#{p_name}' [#{Base.scope_label(scope)}]."
211
241
  elsif res[:reason] == :unknown_provider
212
242
  ctx.out.puts "Provider '#{p_name}' not found."
@@ -241,6 +271,17 @@ module Rubik
241
271
  end
242
272
  end
243
273
 
274
+ # !secrets rides this module's registration plumbing (the command
275
+ # domain is provider credentials); its handlers live in Secrets.
276
+ private_class_method def self.register_secrets_command(shell)
277
+ shell.register_command("secrets",
278
+ description: "Manage stored provider credentials " \
279
+ "(backend | list | has <name> | delete <name> | migrate)",
280
+ usage: Rubik::REPL::Commands::Secrets::USAGE_SECRETS) do |args, ctx|
281
+ Secrets.dispatch(args, ctx)
282
+ end
283
+ end
284
+
244
285
  # Shared rendering for !provider list (human) and !providers (agent):
245
286
  # same catalog, same pixels — only the audience differs. Pinned
246
287
  # byte-for-byte by TestProviderCatalogRender.
@@ -46,8 +46,53 @@ module Rubik
46
46
  Base.report_persist_failure(ctx, res, scope)
47
47
  end
48
48
 
49
- def scope_label(scope)
50
- Base.scope_label(scope)
49
+ # Pre-execution buffer auto-apply choreography shared by the !test
50
+ # and !sh commands: when the buffer is dirty, apply-and-sync if the
51
+ # buffer.auto_apply policy allows, else print the honest unapplied
52
+ # warning. label/subject parameterize the two message spellings so
53
+ # each command keeps its pinned wording. Orchestrator::AutoApply
54
+ # stays the policy's single home — this only mediates the REPL ctx
55
+ # wiring.
56
+ def self.pre_execution_apply(ctx, label, subject)
57
+ return unless ctx.buffer_manager&.dirty?
58
+
59
+ if Rubik::Orchestrator::AutoApply.apply?(ctx.config, trigger: :pre_execution)
60
+ applied = Rubik::Orchestrator::AutoApply.apply_and_sync(ctx.buffer_manager, ctx)
61
+ ctx.out.puts Rubik::Orchestrator::AutoApply.applied_line(applied.size, label)
62
+ else
63
+ ctx.out.puts Rubik::Orchestrator::AutoApply.unapplied_warning(subject)
64
+ end
65
+ end
66
+
67
+ # Adapter resolution shared by the adapter-backed commands
68
+ # (!test/!lint/!format): the command argument names the language
69
+ # when given, else the project's primary language is detected;
70
+ # "ruby" is the honest fallback when neither resolves.
71
+ def self.adapter_for(ctx, lang_arg = nil)
72
+ lang = lang_arg || Rubik::Runtime::Discovery.detect_primary_language(Dir.pwd) || "ruby"
73
+ ctx.adapter_registry.get(lang) || ctx.adapter_registry.get("ruby")
74
+ end
75
+
76
+ # Shared failure-diagnostic line for the adapter-backed commands:
77
+ # prints the category + suggestion when the run failed with a
78
+ # diagnostic. `tip` appends an optional command-specific hint line.
79
+ def self.render_diagnostic(ctx, res, tip: nil)
80
+ return unless !res[:ok] && res[:diagnostic]
81
+
82
+ ctx.out.puts "\n🔍 [Diagnostic] #{res[:diagnostic].category.to_s.upcase}: #{res[:diagnostic].suggestion}"
83
+ ctx.out.puts tip if tip
84
+ end
85
+
86
+ def pre_execution_apply(ctx, label, subject)
87
+ Base.pre_execution_apply(ctx, label, subject)
88
+ end
89
+
90
+ def adapter_for(ctx, lang_arg = nil)
91
+ Base.adapter_for(ctx, lang_arg)
92
+ end
93
+
94
+ def render_diagnostic(ctx, res, tip: nil)
95
+ Base.render_diagnostic(ctx, res, tip: tip)
51
96
  end
52
97
  end
53
98
  end