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,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "base"
4
+ require_relative "../../storage/layout"
5
+ require_relative "workspace_approval"
4
6
 
5
7
  module Rubik
6
8
  module REPL
@@ -50,7 +52,7 @@ module Rubik
50
52
  end
51
53
 
52
54
  private_class_method def self.handle_allow_cmd_list(ctx)
53
- entries = Array(ctx.config.fetch("security.command_allowlist", []))
55
+ entries = Array(Rubik::Storage::Layout.config_get(ctx.config, "security.command_allowlist", []))
54
56
  ctx.out.puts "=== Allowed Command Prefixes (operator allowlist) ==="
55
57
  if entries.empty?
56
58
  ctx.out.puts " (No command prefixes allowlisted — unknown/dangerous commands stay blocked)"
@@ -59,64 +61,13 @@ module Rubik
59
61
  end
60
62
  end
61
63
 
64
+ # Thin adapter: approval/trust logic lives in CommandPrefixApproval.
62
65
  private_class_method def self.handle_allow_cmd_add(ctx, tokens_to_add, scope)
63
- entry_tokens = tokens_to_add.reject { |a| a == "--force" }
64
- entry = entry_tokens.join(" ").strip
65
- if entry.empty?
66
- ctx.out.puts 'Usage: !allow-cmd add "<command prefix>" [--project|--global] [--force]'
67
- return
68
- end
69
-
70
- tokens = entry.split(/\s+/)
71
- # Fail closed (G3 triage, Priority-1 site): a classifier crash
72
- # must not silently skip the broad-token RCE check — treat the
73
- # token as broad and let the operator pass a narrow prefix.
74
- broad = begin
75
- base = Rubik::Security::Classifier.classify(tokens.first)
76
- base.classification == :dangerous && tokens.size == 1
77
- rescue StandardError => e
78
- Rubik::Log.warn(:security, "classifier failed for '#{tokens.first}' (#{e.class}: #{e.message}); treating as broad")
79
- true
80
- end
81
-
82
- # Fail-closed against unbounded execution: a broad, single-token
83
- # dangerous entry (e.g. "node", "python", "bash") grants
84
- # command execution for EVERY invocation of that binary, so
85
- # `node` → `node -e 'malicious()'` RCE. Such broad tokens are
86
- # blocked with or without --force — the operator should use a
87
- # narrow prefix (e.g. "node .gitnexus/run.cjs").
88
- if broad
89
- ctx.out.puts "Blocked: '#{entry}' is a broad, single-token dangerous command. Even with --force it grants " \
90
- "unbounded command execution (e.g. `#{tokens.first} -e 'malicious(...)'` RCE). " \
91
- "Use a narrow prefix instead (e.g. \"#{tokens.first} <script>\")."
92
- return
93
- end
94
-
95
- # The catastrophic destructive-pattern fallback always runs:
96
- # a dangerous multi-token entry may never add `rm -rf /`-style
97
- # chains to the allowlist, with or without --force.
98
- catastrophic = false
99
- begin
100
- ctx.guard.verify_command_fallback_regex!(entry)
101
- rescue Rubik::Security::BlockedWriteError => e
102
- ctx.out.puts "Blocked: '#{entry}' matches a catastrophic destructive pattern and cannot be allowlisted: #{e.message}"
103
- catastrophic = true
104
- end
105
- return if catastrophic
106
-
107
- current = Array(ctx.config.fetch("security.command_allowlist", [])).dup
108
- if current.include?(entry)
109
- ctx.out.puts "[Rubik::AllowCmd] '#{entry}' is already allowlisted."
110
- return
111
- end
112
- current << entry
113
- res = ctx.config.set("security.command_allowlist", current, persist: scope)
114
- ctx.guard&.log_audit("allowlist_add", command: entry, scope: scope)
115
- return if Base.report_persist_failure(ctx, res, scope)
116
-
117
- ctx.out.puts "[Rubik::AllowCmd] Added command prefix: #{entry} [#{Base.scope_label(scope)}]."
66
+ approval = CommandPrefixApproval.new(config: ctx.config, out: ctx.out, guard: ctx.guard)
67
+ approval.add(tokens_to_add, scope)
118
68
  end
119
69
 
70
+ # Thin adapter: allowlist mutation lives in CommandPrefixApproval.
120
71
  private_class_method def self.handle_allow_cmd_remove(ctx, clean_args, scope)
121
72
  entry = clean_args[1..].join(" ").strip
122
73
  if entry.empty?
@@ -124,17 +75,8 @@ module Rubik
124
75
  return
125
76
  end
126
77
 
127
- current = Array(ctx.config.fetch("security.command_allowlist", [])).dup
128
- unless current.include?(entry)
129
- ctx.out.puts "[Rubik::AllowCmd] '#{entry}' is not in the allowlist."
130
- return
131
- end
132
- current.delete(entry)
133
- res = ctx.config.set("security.command_allowlist", current, persist: scope)
134
- ctx.guard&.log_audit("allowlist_remove", command: entry, scope: scope)
135
- return if Base.report_persist_failure(ctx, res, scope)
136
-
137
- ctx.out.puts "[Rubik::AllowCmd] Removed command prefix: #{entry} [#{Base.scope_label(scope)}]."
78
+ approval = CommandPrefixApproval.new(config: ctx.config, out: ctx.out, guard: ctx.guard)
79
+ approval.remove(entry, scope)
138
80
  end
139
81
 
140
82
  private_class_method def self.register_workspace_command(shell)
@@ -172,7 +114,7 @@ module Rubik
172
114
  allowed_ext = ctx.buffer_manager&.allowed_external_dirs || []
173
115
  rw_allowed_set = ctx.buffer_manager&.rw_allowed_dirs || Set.new
174
116
  glob_rw = ctx.buffer_manager&.allow_external_writes || false
175
- auto_idx = ctx.config.fetch("workspace.auto_index_additional", true)
117
+ auto_idx = Rubik::Storage::Layout.config_get(ctx.config, "workspace.auto_index_additional", true)
176
118
 
177
119
  ctx.out.puts "=== Configured Workspace Roots ==="
178
120
  ctx.out.puts " • [PRIMARY] #{effective_root} (Read-Write)"
@@ -199,80 +141,52 @@ module Rubik
199
141
  ctx.out.puts "Auto-Index Additional Roots : #{auto_idx}"
200
142
  end
201
143
 
144
+ # Thin adapter: trust + mutation + persistence live in
145
+ # WorkspaceApproval (allow external dir, mode flipped from --ro).
202
146
  private_class_method def self.handle_workspace_allow(target_path, ctx, is_ro, scope)
203
147
  if target_path.nil? || target_path.empty?
204
148
  ctx.out.puts "Usage: !workspace allow <path> [--rw|--ro] [--project|--global]"
205
149
  return
206
150
  end
207
151
 
208
- begin
209
- ctx.guard.verify_root_safety!(target_path)
210
- rescue Rubik::Security::BlockedWriteError => e
211
- ctx.out.puts "Security Error: #{e.message}"
212
- return
213
- end
214
-
215
- mode_rw = !is_ro
216
- expanded = ctx.buffer_manager.add_allowed_dir(target_path, rw: mode_rw)
217
- ctx.sandbox_runner&.add_allowed_dir(target_path)
218
-
219
- current_dirs = Array(ctx.config.fetch("workspace.allowed_external_dirs", [])).dup
220
- current_dirs << target_path unless current_dirs.include?(target_path)
221
- res = ctx.config.set("workspace.allowed_external_dirs", current_dirs, persist: scope)
222
- return if Base.report_persist_failure(ctx, res, scope)
223
-
224
- mode_str = mode_rw ? "Read-Write" : "Read-Only"
225
- ctx.out.puts "[Rubik::Allow] Added allowed external directory: #{expanded} (Mode: #{mode_str}) [#{Base.scope_label(scope)}]."
152
+ approval = build_workspace_approval(target_path, ctx, read_write: !is_ro, scope: scope)
153
+ approval.allow_dir! if approval.trusted?
226
154
  end
227
155
 
156
+ # Thin adapter: trust + mutation + persistence live in
157
+ # WorkspaceApproval (additional root).
228
158
  private_class_method def self.handle_workspace_add(target_path, ctx, is_rw, scope)
229
159
  if target_path.nil? || target_path.empty?
230
160
  ctx.out.puts "Usage: !workspace add <path> [--rw] [--project|--global]"
231
161
  return
232
162
  end
233
163
 
234
- begin
235
- ctx.guard.verify_root_safety!(target_path)
236
- rescue Rubik::Security::BlockedWriteError => e
237
- ctx.out.puts "Security Error: #{e.message}"
238
- return
239
- end
240
-
241
- expanded = ctx.buffer_manager.add_root(target_path, rw: is_rw)
242
-
243
- # Sync context index & AST coeffects
244
- auto_idx = ctx.config.fetch("workspace.auto_index_additional", true)
245
- ctx.sync_workspace_index! if auto_idx
246
-
247
- # Persist to config
248
- current_roots = Array(ctx.config.fetch("workspace.additional_roots", [])).dup
249
- current_roots << target_path unless current_roots.include?(target_path)
250
- res = ctx.config.set("workspace.additional_roots", current_roots, persist: scope)
251
- return if Base.report_persist_failure(ctx, res, scope)
252
-
253
- mode_str = is_rw ? "Read-Write" : "Read-Only"
254
- ctx.out.puts "[Rubik::Workspace] Added additional root: #{expanded} (Mode: #{mode_str}) [#{Base.scope_label(scope)}]."
164
+ approval = build_workspace_approval(target_path, ctx, read_write: is_rw, scope: scope)
165
+ approval.add_root! if approval.trusted?
255
166
  end
256
167
 
168
+ # Thin adapter: mutation + persistence live in WorkspaceApproval.
257
169
  private_class_method def self.handle_workspace_remove(target_path, ctx, scope, effective_root)
258
170
  if target_path.nil? || target_path.empty?
259
171
  ctx.out.puts "Usage: !workspace remove <path> [--project|--global]"
260
172
  return
261
173
  end
262
174
 
263
- expanded = ctx.buffer_manager.remove_root(target_path)
264
-
265
- # Sync context index & AST coeffects
266
- auto_idx = ctx.config.fetch("workspace.auto_index_additional", true)
267
- ctx.sync_workspace_index! if auto_idx
268
-
269
- # Persist to config
270
- current_roots = Array(ctx.config.fetch("workspace.additional_roots", [])).dup
271
- current_roots.reject! { |r| File.expand_path(r, effective_root) == expanded || r == target_path }
272
- res = ctx.config.set("workspace.additional_roots", current_roots, persist: scope)
273
- return if Base.report_persist_failure(ctx, res, scope)
175
+ approval = build_workspace_approval(target_path, ctx, read_write: false, scope: scope, effective_root: effective_root)
176
+ approval.remove_root!
177
+ end
274
178
 
275
- ctx.out.puts "[Rubik::Workspace] Removed additional root: #{expanded} [#{Base.scope_label(scope)}]."
179
+ # Explicit dependency wiring (AGENTS.md §5): the value object gets
180
+ # every collaborator injected from ctx — no ambient lookups.
181
+ private_class_method def self.build_workspace_approval(path, ctx, read_write:, scope:, effective_root: nil)
182
+ WorkspaceApproval.new(path: path, read_write: read_write, scope: scope,
183
+ config: ctx.config,
184
+ buffer_manager: ctx.buffer_manager,
185
+ out: ctx.out,
186
+ guard: ctx.guard,
187
+ sandbox_runner: ctx.sandbox_runner,
188
+ index_sync: -> { ctx.sync_workspace_index! },
189
+ effective_root: effective_root)
276
190
  end
277
191
 
278
192
  private_class_method def self.register_allow_command(shell)
@@ -335,48 +249,27 @@ module Rubik
335
249
  end
336
250
  end
337
251
 
252
+ # Thin adapter: mutation + persistence live in WorkspaceApproval.
338
253
  private_class_method def self.handle_allow_remove(target_path, ctx, scope, effective_root)
339
254
  if target_path.nil? || target_path.empty?
340
255
  ctx.out.puts "Usage: !allow remove <path> [--project|--global]"
341
256
  return
342
257
  end
343
258
 
344
- expanded = ctx.buffer_manager&.remove_allowed_dir(target_path)
345
- ctx.sandbox_runner&.remove_allowed_dir(target_path)
346
-
347
- # Persist to config
348
- current_dirs = Array(ctx.config.fetch("workspace.allowed_external_dirs", [])).dup
349
- current_dirs.reject! { |d| File.expand_path(d, effective_root) == expanded || d == target_path }
350
- res = ctx.config.set("workspace.allowed_external_dirs", current_dirs, persist: scope)
351
- return if Base.report_persist_failure(ctx, res, scope)
352
-
353
- ctx.out.puts "[Rubik::Allow] Removed allowed external directory: #{expanded} [#{Base.scope_label(scope)}]."
259
+ approval = build_workspace_approval(target_path, ctx, read_write: false, scope: scope, effective_root: effective_root)
260
+ approval.remove_allowed_dir!
354
261
  end
355
262
 
263
+ # Thin adapter: trust + mutation + persistence live in
264
+ # WorkspaceApproval (allow external dir).
356
265
  private_class_method def self.handle_allow_add(target_path, ctx, scope, mode_rw)
357
266
  if target_path.nil? || target_path.empty?
358
267
  ctx.out.puts "Usage: !allow add <path> [--rw|--ro] [--project|--global]"
359
268
  return
360
269
  end
361
270
 
362
- begin
363
- ctx.guard.verify_root_safety!(target_path)
364
- rescue Rubik::Security::BlockedWriteError => e
365
- ctx.out.puts "Security Error: #{e.message}"
366
- return
367
- end
368
-
369
- expanded = ctx.buffer_manager.add_allowed_dir(target_path, rw: mode_rw)
370
- ctx.sandbox_runner&.add_allowed_dir(target_path)
371
-
372
- # Persist to config (Zero-AST / Zero-Scanner: No IndexService or AST warm)
373
- current_dirs = Array(ctx.config.fetch("workspace.allowed_external_dirs", [])).dup
374
- current_dirs << target_path unless current_dirs.include?(target_path)
375
- res = ctx.config.set("workspace.allowed_external_dirs", current_dirs, persist: scope)
376
- return if Base.report_persist_failure(ctx, res, scope)
377
-
378
- mode_str = mode_rw ? "Read-Write" : "Read-Only"
379
- ctx.out.puts "[Rubik::Allow] Added allowed external directory: #{expanded} (Mode: #{mode_str}) [#{Base.scope_label(scope)}]."
271
+ approval = build_workspace_approval(target_path, ctx, read_write: mode_rw, scope: scope)
272
+ approval.allow_dir! if approval.trusted?
380
273
  end
381
274
 
382
275
  OPERATOR_ONLY_CONFIG_PREFIXES = [
@@ -409,6 +302,18 @@ module Rubik
409
302
  end
410
303
  end
411
304
 
305
+ # Secrets never render: plaintext shows as the redaction mask, while
306
+ # a rubik-secret: pointer prints as-is (it names a location, not a
307
+ # credential). Shared by get/set-echo/list so the three surfaces
308
+ # cannot drift.
309
+ private_class_method def self.display_value(key, val)
310
+ if Rubik::Config::SecretPolicy.secret_key?(key) && !Rubik::Security::SecretStore.reference?(val)
311
+ Rubik::Security::Redactor::MASK_STRING
312
+ else
313
+ val
314
+ end
315
+ end
316
+
412
317
  private_class_method def self.register_config_command(shell)
413
318
  shell.register_command("config", description: "Get, set, or view runtime configuration (set|get|list|path)",
414
319
  usage: USAGE_CONFIG, audience: :both) do |args, ctx|
@@ -429,7 +334,7 @@ module Rubik
429
334
  if val.nil?
430
335
  ctx.out.puts "Config '#{key}' is not set or empty."
431
336
  else
432
- ctx.out.puts "#{key} = #{val}"
337
+ ctx.out.puts "#{key} = #{display_value(key, val)}"
433
338
  end
434
339
  end
435
340
  elsif sub == "set"
@@ -445,7 +350,12 @@ module Rubik
445
350
  res = ctx.config.set(key, val, persist: scope)
446
351
  next if Base.report_persist_failure(ctx, res, scope)
447
352
 
448
- ctx.out.puts "Config '#{key}' updated to '#{val}' [#{Base.scope_label(scope)}]."
353
+ ctx.out.puts "Config '#{key}' updated to '#{display_value(key, val)}' [#{Base.scope_label(scope)}]."
354
+ if (info = res[:secret]) && info[:stored]
355
+ ctx.out.puts " Secret stored via #{info[:backend]} secret store; the config file holds only a reference."
356
+ elsif (info = res[:secret]) && info[:warning]
357
+ ctx.out.puts " Warning: #{info[:warning]}"
358
+ end
449
359
  end
450
360
  elsif sub == "list"
451
361
  filter = clean_args[1]&.downcase
@@ -468,7 +378,7 @@ module Rubik
468
378
  else
469
379
  ctx.out.puts "=== Rubik Configuration (#{matched.size} entries) ==="
470
380
  matched.sort.each do |k, v|
471
- ctx.out.puts " • #{k.ljust(35)} = #{v.inspect}"
381
+ ctx.out.puts " • #{k.ljust(35)} = #{display_value(k, v).inspect}"
472
382
  end
473
383
  end
474
384
  else
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../storage/layout"
4
+
5
+ module Rubik
6
+ module REPL
7
+ module Commands
8
+ # Value object resolving one workspace/directory approval request:
9
+ # (path, read-write mode, persistence scope, trust). All collaborators
10
+ # (config, buffer manager, sandbox runner, security guard, output
11
+ # stream) are injected explicitly — no ambient globals (AGENTS.md §5).
12
+ #
13
+ # The REPL command handlers stay thin adapters: they parse the raw
14
+ # argv into these attributes, print usage lines, and delegate the
15
+ # trust check, the runtime mutation and the config persistence here.
16
+ # Every message spelling is pinned to what the handlers used to emit.
17
+ class WorkspaceApproval
18
+ attr_reader :path, :rw, :scope
19
+
20
+ def initialize(path:, read_write:, scope:, config:, buffer_manager:, out:, guard:, sandbox_runner: nil,
21
+ index_sync: nil, effective_root: nil)
22
+ @path = path
23
+ @rw = read_write
24
+ @scope = scope
25
+ @config = config
26
+ @buffer_manager = buffer_manager
27
+ @out = out
28
+ @guard = guard
29
+ @sandbox_runner = sandbox_runner
30
+ @index_sync = index_sync
31
+ @effective_root = effective_root
32
+ end
33
+
34
+ # Trust resolution (fail-closed): a dangerous system root is refused
35
+ # with the pinned "Security Error:" line before any mutation runs.
36
+ def trusted?
37
+ @guard.verify_root_safety!(@path)
38
+ true
39
+ rescue Rubik::Security::BlockedWriteError => e
40
+ @out.puts "Security Error: #{e.message}"
41
+ false
42
+ end
43
+
44
+ # !workspace add <path> [--rw]: register an additional workspace root
45
+ # on the buffer manager, optionally re-index coeffects, and persist.
46
+ def add_root!
47
+ expanded = @buffer_manager.add_root(@path, rw: @rw)
48
+
49
+ sync_index_if_enabled!
50
+
51
+ current_roots = Array(Rubik::Storage::Layout.config_get(@config, "workspace.additional_roots", [])).dup
52
+ current_roots << @path unless current_roots.include?(@path)
53
+ res = @config.set("workspace.additional_roots", current_roots, persist: @scope)
54
+ return if Base.report_persist_failure(@out, res, @scope)
55
+
56
+ mode_str = @rw ? "Read-Write" : "Read-Only"
57
+ @out.puts "[Rubik::Workspace] Added additional root: #{expanded} (Mode: #{mode_str}) " \
58
+ "[#{Base.scope_label(@scope)}]."
59
+ end
60
+
61
+ # !workspace allow / !allow-dir add <path> [--rw|--ro]: allow an
62
+ # external directory for Buffer I/O and Sandbox (Zero-AST: no index
63
+ # sync) and persist.
64
+ def allow_dir!
65
+ expanded = @buffer_manager.add_allowed_dir(@path, rw: @rw)
66
+ @sandbox_runner&.add_allowed_dir(@path)
67
+
68
+ current_dirs = Array(Rubik::Storage::Layout.config_get(@config, "workspace.allowed_external_dirs", [])).dup
69
+ current_dirs << @path unless current_dirs.include?(@path)
70
+ res = @config.set("workspace.allowed_external_dirs", current_dirs, persist: @scope)
71
+ return if Base.report_persist_failure(@out, res, @scope)
72
+
73
+ mode_str = @rw ? "Read-Write" : "Read-Only"
74
+ @out.puts "[Rubik::Allow] Added allowed external directory: #{expanded} (Mode: #{mode_str}) " \
75
+ "[#{Base.scope_label(@scope)}]."
76
+ end
77
+
78
+ # !workspace remove <path>: unregister an additional root, re-index,
79
+ # and prune persisted entries (matched by resolved path or raw arg).
80
+ def remove_root!
81
+ expanded = @buffer_manager.remove_root(@path)
82
+
83
+ sync_index_if_enabled!
84
+
85
+ current_roots = Array(Rubik::Storage::Layout.config_get(@config, "workspace.additional_roots", [])).dup
86
+ current_roots.reject! { |r| File.expand_path(r, @effective_root) == expanded || r == @path }
87
+ res = @config.set("workspace.additional_roots", current_roots, persist: @scope)
88
+ return if Base.report_persist_failure(@out, res, @scope)
89
+
90
+ @out.puts "[Rubik::Workspace] Removed additional root: #{expanded} [#{Base.scope_label(@scope)}]."
91
+ end
92
+
93
+ # !allow-dir remove <path>: retract an allowed external directory
94
+ # from Buffer and Sandbox, then prune persisted entries.
95
+ def remove_allowed_dir!
96
+ expanded = @buffer_manager&.remove_allowed_dir(@path)
97
+ @sandbox_runner&.remove_allowed_dir(@path)
98
+
99
+ current_dirs = Array(Rubik::Storage::Layout.config_get(@config, "workspace.allowed_external_dirs", [])).dup
100
+ current_dirs.reject! { |d| File.expand_path(d, @effective_root) == expanded || d == @path }
101
+ res = @config.set("workspace.allowed_external_dirs", current_dirs, persist: @scope)
102
+ return if Base.report_persist_failure(@out, res, @scope)
103
+
104
+ @out.puts "[Rubik::Allow] Removed allowed external directory: #{expanded} [#{Base.scope_label(@scope)}]."
105
+ end
106
+
107
+ private
108
+
109
+ # Sync context index & AST coeffects when auto-indexing is on
110
+ # (workspace.auto_index_additional, default true).
111
+ def sync_index_if_enabled!
112
+ auto_idx = Rubik::Storage::Layout.config_get(@config, "workspace.auto_index_additional", true)
113
+ @index_sync&.call if auto_idx
114
+ end
115
+ end
116
+
117
+ # Value object for one operator command-prefix allowlist mutation
118
+ # (!allow-cmd add|remove). Wraps the fail-closed broad-token RCE gate,
119
+ # the catastrophic-pattern veto, duplicate handling and persistence
120
+ # with the exact messages the inline handlers used to emit.
121
+ class CommandPrefixApproval
122
+ def initialize(config:, out:, guard:, classifier: Rubik::Security::Classifier)
123
+ @config = config
124
+ @out = out
125
+ @guard = guard
126
+ @classifier = classifier
127
+ end
128
+
129
+ def add(tokens_to_add, scope)
130
+ entry_tokens = tokens_to_add.reject { |a| a == "--force" }
131
+ entry = entry_tokens.join(" ").strip
132
+ if entry.empty?
133
+ @out.puts 'Usage: !allow-cmd add "<command prefix>" [--project|--global] [--force]'
134
+ return
135
+ end
136
+
137
+ return if broad_token_blocked?(entry)
138
+
139
+ return if catastrophic_pattern_blocked?(entry)
140
+
141
+ current = Array(Rubik::Storage::Layout.config_get(@config, "security.command_allowlist", [])).dup
142
+ if current.include?(entry)
143
+ @out.puts "[Rubik::AllowCmd] '#{entry}' is already allowlisted."
144
+ return
145
+ end
146
+ current << entry
147
+ res = @config.set("security.command_allowlist", current, persist: scope)
148
+ @guard&.log_audit("allowlist_add", command: entry, scope: scope)
149
+ return if Base.report_persist_failure(@out, res, scope)
150
+
151
+ @out.puts "[Rubik::AllowCmd] Added command prefix: #{entry} [#{Base.scope_label(scope)}]."
152
+ end
153
+
154
+ def remove(entry, scope)
155
+ current = Array(Rubik::Storage::Layout.config_get(@config, "security.command_allowlist", [])).dup
156
+ unless current.include?(entry)
157
+ @out.puts "[Rubik::AllowCmd] '#{entry}' is not in the allowlist."
158
+ return
159
+ end
160
+ current.delete(entry)
161
+ res = @config.set("security.command_allowlist", current, persist: scope)
162
+ @guard&.log_audit("allowlist_remove", command: entry, scope: scope)
163
+ return if Base.report_persist_failure(@out, res, scope)
164
+
165
+ @out.puts "[Rubik::AllowCmd] Removed command prefix: #{entry} [#{Base.scope_label(scope)}]."
166
+ end
167
+
168
+ private
169
+
170
+ # Fail closed (G3 triage, Priority-1 site): a classifier crash must
171
+ # not silently skip the broad-token RCE check — treat the token as
172
+ # broad and let the operator pass a narrow prefix.
173
+ def broad_token_blocked?(entry)
174
+ tokens = entry.split(/\s+/)
175
+ broad = begin
176
+ base = @classifier.classify(tokens.first)
177
+ base.classification == :dangerous && tokens.size == 1
178
+ rescue StandardError => e
179
+ Rubik::Log.warn(:security, "classifier failed for '#{tokens.first}' (#{e.class}: #{e.message}); treating as broad")
180
+ true
181
+ end
182
+
183
+ # Fail-closed against unbounded execution: a broad, single-token
184
+ # dangerous entry (e.g. "node", "python", "bash") grants command
185
+ # execution for EVERY invocation of that binary, so `node` →
186
+ # `node -e 'malicious()'` RCE. Such broad tokens are blocked with
187
+ # or without --force — the operator should use a narrow prefix.
188
+ return false unless broad
189
+
190
+ @out.puts "Blocked: '#{entry}' is a broad, single-token dangerous command. Even with --force it grants " \
191
+ "unbounded command execution (e.g. `#{tokens.first} -e 'malicious(...)'` RCE). " \
192
+ "Use a narrow prefix instead (e.g. \"#{tokens.first} <script>\")."
193
+ true
194
+ end
195
+
196
+ # The catastrophic destructive-pattern fallback always runs: a
197
+ # dangerous multi-token entry may never add `rm -rf /`-style chains
198
+ # to the allowlist, with or without --force.
199
+ def catastrophic_pattern_blocked?(entry)
200
+ @guard.verify_command_fallback_regex!(entry)
201
+ false
202
+ rescue Rubik::Security::BlockedWriteError => e
203
+ @out.puts "Blocked: '#{entry}' matches a catastrophic destructive pattern and cannot be allowlisted: #{e.message}"
204
+ true
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end
@@ -92,7 +92,7 @@ module Rubik
92
92
  removed_roots.each { |r| shell.buffer_manager&.remove_root(r) }
93
93
 
94
94
  if (new_roots.any? || removed_roots.any?) &&
95
- shell.config.fetch("workspace.auto_index_additional", true) && shell.context_index
95
+ Rubik::Storage::Layout.config_get(shell.config, "workspace.auto_index_additional", true) && shell.context_index
96
96
  shell.context_index.additional_roots = shell.buffer_manager&.additional_roots
97
97
  shell.context_index.full_scan
98
98
  shell.warm_ast_graph(async: false) if shell.respond_to?(:warm_ast_graph)
@@ -130,8 +130,8 @@ module Rubik
130
130
  return unless ["context.include", "context.exclude"].include?(key)
131
131
  return unless shell.context_index.respond_to?(:update_file_filters)
132
132
 
133
- inc = shell.config.fetch("context.include", [])
134
- exc = shell.config.fetch("context.exclude", [])
133
+ inc = Rubik::Storage::Layout.config_get(shell.config, "context.include", [])
134
+ exc = Rubik::Storage::Layout.config_get(shell.config, "context.exclude", [])
135
135
  shell.context_index.update_file_filters(includes: inc, excludes: exc)
136
136
  shell.warm_ast_graph(async: false) if shell.respond_to?(:warm_ast_graph)
137
137
  end
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "logger"
4
+ require_relative "../log"
4
5
 
5
6
  module Rubik
6
7
  module REPL
7
8
  class Hooks
8
9
  KNOWN_EVENTS = [:file_changed, :test_failed, :before_command, :after_command].freeze
9
10
 
10
- def initialize(logger: Logger.new($stderr))
11
+ def initialize(logger: Rubik::Log.forwarder(:repl_hooks))
11
12
  @hooks = Hash.new { |h, k| h[k] = [] }
12
13
  @logger = logger
13
14
  @mutex = Mutex.new