rubik 0.8.0 → 0.9.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 (142) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +2 -2
  3. data/CHANGELOG.md +164 -1
  4. data/bin/rubik +15 -10
  5. data/lib/rubik/cli/workspace_trust.rb +1 -0
  6. data/lib/rubik/concurrency/worker.rb +143 -0
  7. data/lib/rubik/concurrency.rb +1 -0
  8. data/lib/rubik/config/deep_ops.rb +17 -0
  9. data/lib/rubik/config/defaults.rb +0 -1
  10. data/lib/rubik/config/schema.rb +0 -1
  11. data/lib/rubik/config.rb +7 -3
  12. data/lib/rubik/context/repo_context_reader.rb +27 -8
  13. data/lib/rubik/events.rb +18 -0
  14. data/lib/rubik/kernel/dispatcher.rb +19 -23
  15. data/lib/rubik/kernel/init.rb +22 -18
  16. data/lib/rubik/kernel/unit.rb +4 -3
  17. data/lib/rubik/orchestrator/auto_apply.rb +2 -0
  18. data/lib/rubik/orchestrator/budget_manager.rb +18 -2
  19. data/lib/rubik/orchestrator/goal_ledger/deep_ops.rb +41 -0
  20. data/lib/rubik/orchestrator/goal_ledger/drift.rb +2 -1
  21. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +59 -59
  22. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +29 -9
  23. data/lib/rubik/orchestrator/goal_ledger/patch.rb +10 -17
  24. data/lib/rubik/orchestrator/goal_ledger/suspend_lifecycle.rb +9 -41
  25. data/lib/rubik/orchestrator/goal_ledger/validator.rb +6 -20
  26. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +3 -14
  27. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +21 -14
  28. data/lib/rubik/orchestrator/goal_state.rb +15 -15
  29. data/lib/rubik/orchestrator/loop.rb +62 -63
  30. data/lib/rubik/orchestrator/lsp_feedback.rb +7 -1
  31. data/lib/rubik/orchestrator/subagent/admission_policy.rb +161 -0
  32. data/lib/rubik/orchestrator/subagent/builtin_types.rb +3 -2
  33. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +51 -2
  34. data/lib/rubik/orchestrator/subagent/dependency.rb +20 -20
  35. data/lib/rubik/orchestrator/subagent/execution_reads.rb +1 -1
  36. data/lib/rubik/orchestrator/subagent/journal.rb +3 -9
  37. data/lib/rubik/orchestrator/subagent/lifecycle.rb +10 -1
  38. data/lib/rubik/orchestrator/subagent/notices.rb +5 -4
  39. data/lib/rubik/orchestrator/subagent/paging.rb +49 -0
  40. data/lib/rubik/orchestrator/subagent/profile_contract.rb +4 -10
  41. data/lib/rubik/orchestrator/subagent/reaper.rb +1 -3
  42. data/lib/rubik/orchestrator/subagent/redeploy_transaction.rb +99 -0
  43. data/lib/rubik/orchestrator/subagent/result_archive.rb +12 -22
  44. data/lib/rubik/orchestrator/subagent/reuse.rb +37 -31
  45. data/lib/rubik/orchestrator/subagent/run_cycle.rb +22 -1
  46. data/lib/rubik/orchestrator/subagent/scoped_registry.rb +34 -2
  47. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +1 -1
  48. data/lib/rubik/orchestrator/subagent/supervisor.rb +6 -2
  49. data/lib/rubik/orchestrator/subagent/task_ownership.rb +2 -2
  50. data/lib/rubik/orchestrator/subagent/transcript_reader.rb +14 -13
  51. data/lib/rubik/orchestrator/subagent/type_registry.rb +19 -1
  52. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +6 -1
  53. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +6 -1
  54. data/lib/rubik/orchestrator/turn_executor.rb +2 -1
  55. data/lib/rubik/platform/path.rb +0 -1
  56. data/lib/rubik/prompt/context_builder.rb +24 -18
  57. data/lib/rubik/prompt/context_compactor.rb +31 -8
  58. data/lib/rubik/prompt/conversation_rows.rb +33 -0
  59. data/lib/rubik/prompt/doctrine.rb +4 -2
  60. data/lib/rubik/prompt/emergency_compactor.rb +5 -4
  61. data/lib/rubik/repl/commands/agent_execution.rb +13 -2
  62. data/lib/rubik/repl/commands/buffer.rb +4 -0
  63. data/lib/rubik/repl/commands/mcp.rb +5 -0
  64. data/lib/rubik/repl/commands/subagents.rb +7 -6
  65. data/lib/rubik/repl/commands/workspace.rb +22 -2
  66. data/lib/rubik/repl/config_wiring.rb +24 -7
  67. data/lib/rubik/repl/input_execution.rb +1 -1
  68. data/lib/rubik/repl/lifecycle.rb +3 -2
  69. data/lib/rubik/repl/pruner/harness.rb +52 -67
  70. data/lib/rubik/repl/pruner/rules.rb +3 -1
  71. data/lib/rubik/repl/pruner/service.rb +4 -9
  72. data/lib/rubik/repl/service_catalog.rb +1 -1
  73. data/lib/rubik/repl/synthesized_command_context.rb +3 -0
  74. data/lib/rubik/repl/turn_recorder.rb +5 -4
  75. data/lib/rubik/repl.rb +1 -0
  76. data/lib/rubik/runtime/context.rb +11 -1
  77. data/lib/rubik/runtime/discovery.rb +55 -0
  78. data/lib/rubik/runtime/fiber.rb +17 -5
  79. data/lib/rubik/runtime/manager.rb +19 -12
  80. data/lib/rubik/runtime/overlay_storage.rb +54 -23
  81. data/lib/rubik/runtime/repair_probe.rb +20 -2
  82. data/lib/rubik/runtime/sealed_coeffects.rb +58 -0
  83. data/lib/rubik/runtime/synthesized_command_context.rb +80 -0
  84. data/lib/rubik/runtime/synthesized_tool_context.rb +123 -0
  85. data/lib/rubik/runtime.rb +2 -0
  86. data/lib/rubik/security/classifier/classifier.rb +16 -8
  87. data/lib/rubik/security/classifier/commands/biome.yaml +49 -0
  88. data/lib/rubik/security/classifier/commands/bun.yaml +91 -0
  89. data/lib/rubik/security/classifier/commands/bundle.yaml +107 -0
  90. data/lib/rubik/security/classifier/commands/clang-format.yaml +21 -0
  91. data/lib/rubik/security/classifier/commands/clang.yaml +37 -0
  92. data/lib/rubik/security/classifier/commands/cmake.yaml +37 -0
  93. data/lib/rubik/security/classifier/commands/ctest.yaml +30 -0
  94. data/lib/rubik/security/classifier/commands/gcc.yaml +36 -0
  95. data/lib/rubik/security/classifier/commands/gem.yaml +104 -0
  96. data/lib/rubik/security/classifier/commands/gofmt.yaml +15 -0
  97. data/lib/rubik/security/classifier/commands/pnpm.yaml +8 -8
  98. data/lib/rubik/security/classifier/commands/python.yaml +18 -1
  99. data/lib/rubik/security/classifier/commands/python3.yaml +18 -1
  100. data/lib/rubik/security/classifier/commands/rake.yaml +60 -0
  101. data/lib/rubik/security/classifier/commands/rspec.yaml +42 -0
  102. data/lib/rubik/security/classifier/commands/rubocop.yaml +38 -0
  103. data/lib/rubik/security/classifier/commands/ruby.yaml +25 -0
  104. data/lib/rubik/security/classifier/commands/rustc.yaml +34 -0
  105. data/lib/rubik/security/classifier/commands/zig.yaml +77 -0
  106. data/lib/rubik/security/classifier/database.rb +12 -1
  107. data/lib/rubik/security/classifier/matcher.rb +17 -2
  108. data/lib/rubik/security/classifier/models.rb +3 -2
  109. data/lib/rubik/security/classifier.rb +5 -0
  110. data/lib/rubik/security/guard.rb +133 -39
  111. data/lib/rubik/storage/layout.rb +14 -3
  112. data/lib/rubik/storage/vakanuvis/harness.rb +49 -89
  113. data/lib/rubik/tools/base.rb +55 -7
  114. data/lib/rubik/tools/builtins/dmesg.rb +1 -1
  115. data/lib/rubik/tools/builtins/exec.rb +1 -2
  116. data/lib/rubik/tools/builtins/files.rb +3 -1
  117. data/lib/rubik/tools/builtins/goal_ledger.rb +10 -10
  118. data/lib/rubik/tools/builtins/grep.rb +2 -2
  119. data/lib/rubik/tools/builtins/interactive.rb +1 -3
  120. data/lib/rubik/tools/builtins/lifecycle.rb +1 -1
  121. data/lib/rubik/tools/builtins/lsp.rb +2 -2
  122. data/lib/rubik/tools/builtins/pruner.rb +1 -1
  123. data/lib/rubik/tools/builtins/runtime.rb +1 -1
  124. data/lib/rubik/tools/builtins/send_message.rb +14 -3
  125. data/lib/rubik/tools/builtins/skill.rb +1 -1
  126. data/lib/rubik/tools/builtins/storage.rb +1 -1
  127. data/lib/rubik/tools/builtins/subagent.rb +2 -1
  128. data/lib/rubik/tools/builtins/subagent_lifecycle.rb +5 -3
  129. data/lib/rubik/tools/builtins/symbols.rb +4 -4
  130. data/lib/rubik/tools/builtins/synthesis.rb +121 -64
  131. data/lib/rubik/tools/builtins/task.rb +1 -1
  132. data/lib/rubik/tools/builtins/unsynthesize.rb +38 -0
  133. data/lib/rubik/tools/builtins/vakanuvis.rb +1 -1
  134. data/lib/rubik/tools/catalog.rb +3 -0
  135. data/lib/rubik/tools/declaration.rb +6 -2
  136. data/lib/rubik/tools/preflight_runner.rb +107 -14
  137. data/lib/rubik/tools/registry.rb +2 -0
  138. data/lib/rubik/tools/uri_resolver.rb +3 -1
  139. data/lib/rubik/uris.rb +5 -1
  140. data/lib/rubik/version.rb +1 -1
  141. data/lib/rubik.rb +1 -0
  142. metadata +27 -1
@@ -0,0 +1,77 @@
1
+ # $schema: ../../../schemas/command.schema.json
2
+ command: zig
3
+ description: "General-purpose programming language and toolchain"
4
+ classification: LOCAL_EFFECTS
5
+ risk: LOW
6
+ strict: false
7
+ global_options:
8
+ --help: {overrides: READONLY}
9
+ -h: {overrides: READONLY, aliases: [--help]}
10
+ subcommands:
11
+ version:
12
+ classification: READONLY
13
+ strict: false
14
+ env:
15
+ classification: READONLY
16
+ strict: false
17
+ help:
18
+ classification: READONLY
19
+ strict: false
20
+ zen:
21
+ classification: READONLY
22
+ strict: false
23
+ targets:
24
+ classification: READONLY
25
+ strict: false
26
+ ast-check:
27
+ classification: READONLY
28
+ strict: false
29
+ test:
30
+ classification: LOCAL_EFFECTS
31
+ risk: LOW
32
+ strict: false
33
+ build:
34
+ classification: LOCAL_EFFECTS
35
+ risk: LOW
36
+ strict: false
37
+ subcommands:
38
+ test:
39
+ classification: LOCAL_EFFECTS
40
+ risk: LOW
41
+ strict: false
42
+ build-exe:
43
+ classification: LOCAL_EFFECTS
44
+ strict: false
45
+ options:
46
+ -fno-emit-bin: {overrides: READONLY}
47
+ build-lib:
48
+ classification: LOCAL_EFFECTS
49
+ strict: false
50
+ options:
51
+ -fno-emit-bin: {overrides: READONLY}
52
+ build-obj:
53
+ classification: LOCAL_EFFECTS
54
+ strict: false
55
+ options:
56
+ -fno-emit-bin: {overrides: READONLY}
57
+ fmt:
58
+ classification: LOCAL_EFFECTS
59
+ strict: false
60
+ options:
61
+ --check: {overrides: READONLY}
62
+ --stdin: {}
63
+ run:
64
+ classification: DANGEROUS
65
+ strict: false
66
+ init:
67
+ classification: LOCAL_EFFECTS
68
+ strict: false
69
+ init-exe:
70
+ classification: LOCAL_EFFECTS
71
+ strict: false
72
+ init-lib:
73
+ classification: LOCAL_EFFECTS
74
+ strict: false
75
+ fetch:
76
+ classification: LOCAL_EFFECTS
77
+ strict: false
@@ -12,7 +12,7 @@ module Rubik
12
12
  include MonitorMixin
13
13
 
14
14
  ALIAS_FORBIDDEN_KEYS = [
15
- "classification", "risk", "subcommands", "options", "global_options", "subcommand_mode", "delegates_to", "strict"
15
+ "classification", "bare_classification", "risk", "subcommands", "options", "global_options", "subcommand_mode", "delegates_to", "strict"
16
16
  ].freeze
17
17
 
18
18
  attr_reader :builtin_dir, :user_dir
@@ -39,6 +39,16 @@ module Rubik
39
39
  index_dir(@user_dir) if @user_dir && File.directory?(@user_dir)
40
40
  end
41
41
 
42
+ def reload!
43
+ synchronize do
44
+ @files.clear
45
+ @cache.clear
46
+ index_dir(@builtin_dir) if @builtin_dir && File.directory?(@builtin_dir)
47
+ index_dir(@user_dir) if @user_dir && File.directory?(@user_dir)
48
+ end
49
+ self
50
+ end
51
+
42
52
  def [](key, shell: nil)
43
53
  get(key, nil, shell: shell)
44
54
  end
@@ -127,6 +137,7 @@ module Rubik
127
137
  description: data["description"],
128
138
  aliases: parse_aliases(data["aliases"], command_name),
129
139
  classification: Classification.normalize(data["classification"]),
140
+ bare_classification: Classification.normalize(data["bare_classification"]),
130
141
  risk: Risk.normalize(data["risk"]),
131
142
  global_options: parse_options(data["global_options"]),
132
143
  subcommands: parse_subcommands(data["subcommands"]),
@@ -12,6 +12,14 @@ module Rubik
12
12
  BUILTIN_DIR_CMDS = ["cd", "pushd", "popd"].freeze
13
13
  BUILTIN_READONLY_CMDS = ["[", "[[", "test"].freeze
14
14
  BUILTIN_DANGEROUS_CMDS = ["source", "."].freeze
15
+ STDIN_TARGET_TOKENS = ["-", "/dev/stdin", "/dev/fd/0"].freeze
16
+
17
+ def self.real_script_target?(positionals)
18
+ effective = positionals.reject { |t| t == "--" }
19
+ return false if effective.empty?
20
+
21
+ !STDIN_TARGET_TOKENS.include?(effective.first)
22
+ end
15
23
 
16
24
  def self.match_command(invocation, database, parse_warnings = nil, shell: nil)
17
25
  argv = invocation.argv
@@ -113,7 +121,12 @@ module Rubik
113
121
  matched_def, command_def, all_overrides, all_risk_overrides,
114
122
  unknown_opts, matched_defs, rem_pos, matched_rule
115
123
  )
116
- base_class = matched_def.classification || Classification::READONLY
124
+ has_target = Matcher.real_script_target?(rem_pos)
125
+ base_class = if matched_def.bare_classification && !has_target && all_overrides.empty?
126
+ matched_def.bare_classification
127
+ else
128
+ matched_def.classification || Classification::READONLY
129
+ end
117
130
  base_risk = matched_def.risk || Delegation.default_risk(base_class)
118
131
  risk_set = !matched_def.risk.nil?
119
132
 
@@ -204,8 +217,10 @@ module Rubik
204
217
  cmd_inners = 0
205
218
 
206
219
  if matched_def.delegates_to
220
+ delegation_pos = rem_pos.dup
221
+ delegation_pos.shift if matched_def.delegates_to.mode == :rest_are_argv && delegation_pos.first == "--"
207
222
  res = Delegation.handle_delegation(
208
- rem_pos, matched_def.delegates_to, database, argv,
223
+ delegation_pos, matched_def.delegates_to, database, argv,
209
224
  matched_def, parse_warnings
210
225
  ) { |inv, db, w| match_command(inv, db, w, shell: shell) }
211
226
  cmd_inners = res.size
@@ -112,11 +112,12 @@ module Rubik
112
112
  end
113
113
 
114
114
  CommandDef = Data.define(
115
- :command, :description, :alias_of, :aliases, :classification, :risk,
115
+ :command, :description, :alias_of, :aliases, :classification, :bare_classification, :risk,
116
116
  :global_options, :subcommands, :options, :strict,
117
117
  :subcommand_mode, :delegates_to
118
118
  ) do
119
- def initialize(command:, description: nil, alias_of: nil, aliases: [], classification: nil, risk: nil,
119
+ def initialize(command:, description: nil, alias_of: nil, aliases: [], classification: nil,
120
+ bare_classification: nil, risk: nil,
120
121
  global_options: {}, subcommands: {}, options: {}, strict: true,
121
122
  subcommand_mode: :hierarchical, delegates_to: nil)
122
123
  super
@@ -20,6 +20,11 @@ module Rubik
20
20
  def self.database
21
21
  Engine.default_database
22
22
  end
23
+
24
+ def self.system_path?(path)
25
+ @default_engine ||= Engine.new
26
+ @default_engine.system_path?(path)
27
+ end
23
28
  end
24
29
  end
25
30
  end
@@ -14,6 +14,8 @@ module Rubik
14
14
  class Guard
15
15
  DEFAULT_CRITICAL_PATTERNS = [
16
16
  ".git/**",
17
+ ".rubik/**",
18
+ "**/.rubik/**",
17
19
  ".env*",
18
20
  "secrets.*",
19
21
  "*.pem",
@@ -95,6 +97,7 @@ module Rubik
95
97
  # Unmanaged background threads survive context disposal — the exact
96
98
  # "Leaked Side Effects" anti-pattern (AGENTS.md §5).
97
99
  /\bThread\s*\.\s*(?:new|fork|start)\b/,
100
+ /\bPathname\s*\(/,
98
101
  /`[^`]*`/,
99
102
  Regexp.new('%x\s*[({\[<|/]', Regexp::IGNORECASE)
100
103
  ].freeze
@@ -117,6 +120,7 @@ module Rubik
117
120
  RAW_ALLOWLIST_UNSAFE_PATTERN = /[;&|<>`$(){}\[\]\\\n\r]/
118
121
 
119
122
  attr_reader :audit_log_path
123
+ attr_accessor :shell, :root_dir
120
124
 
121
125
  # Live-reconfigurable audit destination (ConfigWiring applies config
122
126
  # changes in place — zero restart). Same blank-means-disabled semantics
@@ -144,7 +148,7 @@ module Rubik
144
148
  # suppresses on nil/empty. (The previous composition call site's
145
149
  # `fetch(...) || default` silently re-enabled auditing an operator had
146
150
  # turned off.)
147
- def self.for_config(config)
151
+ def self.for_config(config, root_dir: nil, shell: nil)
148
152
  return new if config.nil? || !config.respond_to?(:fetch)
149
153
 
150
154
  raw_audit = config.fetch("security.audit_log_path", default_audit_log_path)
@@ -160,20 +164,49 @@ module Rubik
160
164
  warn "[Guard] security.audit_log_path has non-path value #{raw_audit.inspect}; auditing disabled"
161
165
  nil
162
166
  end
167
+
168
+ effective_root = root_dir || (config.respond_to?(:root_dir) ? config.root_dir : nil)
169
+
163
170
  new(
164
171
  audit_log_path: audit_path,
165
172
  command_allowlist: Array(config.fetch("security.command_allowlist", [])),
166
173
  # Stringy "false" from persisted config must disable probes too.
167
- allow_safe_probes: config.fetch("security.allow_safe_probes", true).to_s.downcase != "false"
174
+ allow_safe_probes: config.fetch("security.allow_safe_probes", true).to_s.downcase != "false",
175
+ root_dir: effective_root,
176
+ shell: shell
168
177
  )
169
178
  end
170
179
 
171
180
  def initialize(custom_patterns: [], command_allowlist: [], allow_safe_probes: true,
172
- audit_log_path: Guard.default_audit_log_path)
181
+ audit_log_path: Guard.default_audit_log_path, root_dir: nil, shell: nil)
173
182
  @critical_patterns = (DEFAULT_CRITICAL_PATTERNS + custom_patterns).uniq
174
183
  @command_allowlist = normalize_allowlist(command_allowlist)
175
184
  @allow_safe_probes = allow_safe_probes == true
176
185
  @audit_log_path = audit_log_path
186
+ @root_dir = root_dir
187
+ @shell = shell
188
+ end
189
+
190
+ def workspace_trust_candidate_allowed?(cmd_str, ast = nil)
191
+ return false if cmd_str.to_s.match?(RAW_ALLOWLIST_UNSAFE_PATTERN)
192
+
193
+ ast ||= begin
194
+ Rubik::Security::Classifier.classify(cmd_str.to_s)
195
+ rescue StandardError
196
+ nil
197
+ end
198
+ return false unless ast
199
+ return false if ast.respond_to?(:parse_warnings) && ast.parse_warnings.to_a.any?
200
+ return false unless ast.respond_to?(:commands) && ast.commands.to_a.size == 1
201
+
202
+ cmd = ast.commands.first
203
+ return false if cmd.respond_to?(:inner_commands) && cmd.inner_commands && !cmd.inner_commands.empty?
204
+
205
+ effective_root = @root_dir || Dir.pwd
206
+ return false unless defined?(Rubik::CLI::WorkspaceTrust) && Rubik::CLI::WorkspaceTrust.trusted?(effective_root)
207
+ return false unless defined?(Rubik::Runtime::Discovery)
208
+
209
+ Rubik::Runtime::Discovery.recognized_candidate?(cmd_str, root_dir: effective_root)
177
210
  end
178
211
 
179
212
  # security.allow_safe_probes — operator-managed toggle (live-reconfigurable
@@ -390,17 +423,42 @@ module Rubik
390
423
  end
391
424
  end
392
425
 
393
- # allow_unknown: the ONLY sanctioned true is the synthesis pre-check —
394
- # Ruby source ("->(args) { … }") legitimately classifies UNKNOWN through
395
- # the shell classifier, and synthesis carries its own deny layers
396
- # (DANGEROUS_SYNTHESIS_PATTERNS + AST walk). Shell-gate callers must keep
397
- # the default: an UNKNOWN verdict means an unvetted binary, and a binary
398
- # the database cannot classify is exactly the one that must not run
399
- # without operator approval.
426
+ def check_critical_path_writes!(ast, cmd_str)
427
+ ast_commands = ast.respond_to?(:commands) ? ast.commands.to_a : []
428
+ write_targets = ast_commands.flat_map do |c|
429
+ targets = Array(c.respond_to?(:write_paths) ? c.write_paths : nil)
430
+ cmd_name = c.respond_to?(:command) ? c.command.to_a.first.to_s : ""
431
+ targets.concat(c.positionals.to_a) if ["cp", "mv", "tee", "touch", "ln", "install"].include?(cmd_name) && c.respond_to?(:positionals)
432
+ targets
433
+ end.compact.reject(&:empty?)
434
+
435
+ write_targets.concat(ast.write_paths.to_a) if ast.respond_to?(:write_paths)
436
+
437
+ write_targets.uniq.each do |wp|
438
+ next unless critical_path?(wp)
439
+
440
+ log_audit("blocked_command", command: cmd_str, reason: "critical_path_write", path: wp)
441
+ raise BlockedWriteError.new(
442
+ "Command write to critical path blocked: #{wp} in `#{cmd_str}`",
443
+ kind: :critical_path_write,
444
+ blocked_command: cmd_str,
445
+ path: wp
446
+ )
447
+ end
448
+ end
449
+
450
+ # allow_unknown: synthesis verification no longer routes through the
451
+ # shell gate (it has its own deny layers: DANGEROUS_SYNTHESIS_PATTERNS
452
+ # + AST walk), so no production caller passes true today. The flag
453
+ # remains a tested seam for future in-gate verifiers of non-shell
454
+ # source; shell-gate callers must keep the default: an UNKNOWN verdict
455
+ # means an unvetted binary, and a binary the database cannot classify
456
+ # is exactly the one that must not run without operator approval.
400
457
  def verify_command_safety!(cmd, shell: nil, allow_unknown: false)
401
458
  cmd_str = cmd.to_s
402
459
  begin
403
460
  ast = Rubik::Security::Classifier.classify(cmd_str, shell: shell)
461
+ check_critical_path_writes!(ast, cmd_str)
404
462
  if ast.classification == :dangerous
405
463
  # Safe-probe exemption (highest-priority friction fix): run the
406
464
  # catastrophic fallback regex first — it never loosens.
@@ -480,6 +538,36 @@ module Rubik
480
538
  verify_command_fallback_regex!(cmd_str)
481
539
  return true
482
540
  end
541
+
542
+ # Workspace Trust Discovery Recognition Gate:
543
+ if workspace_trust_candidate_allowed?(cmd_str, ast)
544
+ log_audit("allowed_command", command: cmd_str, reason: "workspace_trust_discovery")
545
+ verify_command_fallback_regex!(cmd_str)
546
+ return true
547
+ end
548
+
549
+ # Interactive operator approval gate (Antigravity-style single keypress)
550
+ effective_shell = shell || @shell
551
+ if effective_shell.respond_to?(:interactive_input?) && effective_shell.interactive_input?
552
+ # Defense-in-depth: Un-bypassable catastrophic fallback regex MUST pass before prompt
553
+ verify_command_fallback_regex!(cmd_str)
554
+
555
+ theme = (effective_shell.respond_to?(:ui_theme) && effective_shell.ui_theme) ||
556
+ (defined?(Rubik::UI::Theme) ? Rubik::UI::Theme.new : nil)
557
+
558
+ question = "Allow unclassified command execution: '#{cmd_str}'?"
559
+ answer = if defined?(Rubik::UI::Approvals)
560
+ Rubik::UI::Approvals.keypress_yes_no(effective_shell, question, theme: theme, default: "n")
561
+ else
562
+ "n"
563
+ end
564
+
565
+ if ["y", "yes"].include?(answer.to_s.downcase)
566
+ log_audit("allowed_command", command: cmd_str, reason: "operator_interactive_approval")
567
+ return true
568
+ end
569
+ end
570
+
483
571
  log_audit("blocked_command", command: cmd_str, reason: "unknown_command")
484
572
  raise BlockedWriteError.new(
485
573
  "Unknown command execution blocked (fail-closed): #{cmd_str}. The binary is not in the " \
@@ -531,9 +619,9 @@ module Rubik
531
619
  # security-observable downgrade nobody should need RUBIK_DEBUG to
532
620
  # notice. The shell gate fails closed — an exception must never
533
621
  # mint execution permission; the operator allowlist is the one
534
- # sanctioned valve. The synthesis seam (allow_unknown) keeps
535
- # flowing into its own deny layers (DANGEROUS_SYNTHESIS_PATTERNS
536
- # + RubyVM walk), which run after this gate.
622
+ # sanctioned valve. The allow_unknown seam (no production caller
623
+ # today) keeps flowing into its own deny layers
624
+ # (DANGEROUS_SYNTHESIS_PATTERNS + RubyVM walk) after this gate.
537
625
  if allow_unknown
538
626
  warn "[WARN] AST classification error on non-shell input, continuing to synthesis deny layers: #{e.message}"
539
627
  log_audit("classifier_error", command: cmd_str, reason: "#{e.class}: #{e.message}",
@@ -854,14 +942,16 @@ module Rubik
854
942
  SynthesisCapability.new(
855
943
  name: :dynamic_execution,
856
944
  reason: "string eval and Binding turn data into code outside the AST walk",
857
- calls: [:eval, :instance_eval, :class_eval, :module_eval, :binding]
945
+ calls: [:eval, :instance_eval, :class_eval, :module_eval, :binding,
946
+ :instance_exec, :class_exec, :module_exec, :define_method, :define_singleton_method]
858
947
  ),
859
948
  SynthesisCapability.new(
860
949
  name: :process_control,
861
950
  reason: "subprocess spawn and process lifecycle control escape the sandbox and audit trail; " \
862
951
  "trap/at_exit install process-wide hooks the host cannot revert — Signal.trap can " \
863
- "swallow the host REPL's Interrupt/exit handling",
864
- calls: [:system, :exec, :spawn, :fork, :syscall, :exit!, :trap, :at_exit,
952
+ "swallow the host REPL's Interrupt/exit handling; exit/exit! can kill the host " \
953
+ "when preflight runs in-process",
954
+ calls: [:system, :exec, :spawn, :fork, :syscall, :exit, :exit!, :trap, :at_exit,
865
955
  :popen, :capture2, :capture2e, :capture3, :popen2, :popen2e,
866
956
  :popen3, :pipeline, :kill, :daemon],
867
957
  consts: [:Process, :Thread, :Signal, :Open3]
@@ -870,7 +960,8 @@ module Rubik
870
960
  name: :reflection,
871
961
  reason: "send/method-object families bypass static call-name checks (the method(:x).call escape)",
872
962
  calls: [:send, :public_send, :__send__,
873
- :method, :public_method, :singleton_method]
963
+ :method, :public_method, :singleton_method,
964
+ :instance_method, :public_instance_method, :bind, :bind_call]
874
965
  ),
875
966
  SynthesisCapability.new(
876
967
  name: :state_introspection,
@@ -886,8 +977,9 @@ module Rubik
886
977
  SynthesisCapability.new(
887
978
  name: :filesystem,
888
979
  reason: "raw file/IO handles bypass the OutputView and every guard write check",
889
- calls: [:open, :read, :write, :binread, :binwrite, :delete, :unlink, :rm_rf],
890
- consts: [:IO, :File, :Dir, :FileUtils, :STDIN, :STDOUT, :STDERR, :ARGF]
980
+ calls: [:open, :read, :write, :binread, :binwrite, :unlink, :rm_rf, :rmtree,
981
+ :sysopen, :syswrite, :sysread, :readlines, :foreach, :Pathname],
982
+ consts: [:IO, :File, :Dir, :FileUtils, :STDIN, :STDOUT, :STDERR, :ARGF, :Pathname]
891
983
  ),
892
984
  SynthesisCapability.new(
893
985
  name: :code_loading,
@@ -901,7 +993,7 @@ module Rubik
901
993
  SynthesisCapability.new(
902
994
  name: :runtime_root,
903
995
  reason: "core runtime singletons (Kernel/RubyVM/Class/Module/...) re-open every denied capability",
904
- consts: [:Kernel, :RubyVM, :Object, :Class, :Module, :BasicObject]
996
+ consts: [:Kernel, :RubyVM, :Object, :Class, :Module, :BasicObject, :Rubik, :Fiber, :TracePoint, :GC]
905
997
  ),
906
998
  SynthesisCapability.new(
907
999
  name: :ambient_state,
@@ -963,20 +1055,13 @@ module Rubik
963
1055
  # Reject synthesized Ruby before it is ever eval'd.
964
1056
  #
965
1057
  # Defense-in-depth architecture:
966
- # 1. verify_command_safety! checks shell command patterns.
967
- # 2. Fast regex pattern matching (DANGEROUS_SYNTHESIS_PATTERNS) intercepts known keywords.
968
- # 3. RubyVM::AbstractSyntaxTree semantic walk checks normalized node types, method calls,
1058
+ # 1. Fast regex pattern matching (DANGEROUS_SYNTHESIS_PATTERNS) intercepts known keywords.
1059
+ # 2. RubyVM::AbstractSyntaxTree semantic walk checks normalized node types, method calls,
969
1060
  # and receiver constants against explicit deny rules to block AST-level evasion vectors.
970
1061
  #
971
1062
  # Raises Rubik::Security::BlockedWriteError with an actionable message.
972
1063
  def verify_synthesis_safety!(code)
973
1064
  code_str = code.to_s
974
- # allow_unknown: synthesized Ruby is not shell — its UNKNOWN verdicts
975
- # are "not shell-shaped" noise (see verify_command_safety! comment).
976
- # The shell-classifier layer here still blocks dangerous shapes and
977
- # still runs the catastrophic regex; synthesis-specific escapes are
978
- # denied by DANGEROUS_SYNTHESIS_PATTERNS and the AST walk below.
979
- verify_command_safety!(code_str, allow_unknown: true)
980
1065
 
981
1066
  matched = DANGEROUS_SYNTHESIS_PATTERNS.find { |p| code_str.match?(p) }
982
1067
  if matched
@@ -1012,11 +1097,16 @@ module Rubik
1012
1097
 
1013
1098
  case type
1014
1099
  when :const, :colon3
1015
- # :const covers Net::HTTP / Marshal.dump receivers and `X = Socket`
1016
- # RHS values; :colon3 covers top-level scoped `::TCPSocket` — the
1017
- # one-character bypass (children[0] is the name symbol in both).
1100
+ # :const covers Net::HTTP / Marshal.dump receivers, `X = Socket`
1101
+ # RHS values, and local aliasing `k = Kernel`; :colon3 covers top-level scoped `::TCPSocket`.
1018
1102
  name = node.children[0]
1019
- reject_unsafe(node, "reference to forbidden constant :#{name}") if FORBIDDEN_SYNTHESIS_NETWORK_CONSTS.include?(name)
1103
+ forbidden_const = FORBIDDEN_SYNTHESIS_NETWORK_CONSTS.include?(name) || FORBIDDEN_SYNTHESIS_CONSTS.include?(name)
1104
+ reject_unsafe(node, "reference to forbidden constant :#{name}") if forbidden_const
1105
+ when :colon2
1106
+ # Scoped constant reference like Rubik::Security::Guard
1107
+ name = node.children[1]
1108
+ forbidden_const = FORBIDDEN_SYNTHESIS_NETWORK_CONSTS.include?(name) || FORBIDDEN_SYNTHESIS_CONSTS.include?(name)
1109
+ reject_unsafe(node, "reference to forbidden constant :#{name}") if forbidden_const
1020
1110
  when :fcall, :vcall
1021
1111
  name = node.children[0]
1022
1112
  reject_unsafe(node, "call to :#{name}") if FORBIDDEN_SYNTHESIS_CALLS.include?(name)
@@ -1027,12 +1117,16 @@ module Rubik
1027
1117
 
1028
1118
  reject_unsafe(node, "call to :#{name}") if FORBIDDEN_SYNTHESIS_CALLS.include?(name)
1029
1119
 
1030
- # Receiver const check must accept BOTH const node shapes: :const
1031
- # (Net::HTTP) and :colon3 (::ENV, ::Thread.new, ::Kernel.require —
1032
- # top-level scoping would otherwise re-open every denylist entry).
1033
- if recv.respond_to?(:type) && [:const, :colon3].include?(recv.type.to_s.downcase.to_sym)
1034
- const_name = recv.children[0]
1035
- reject_unsafe(node, "access to #{const_name}.#{name}") if FORBIDDEN_SYNTHESIS_CONSTS.include?(const_name)
1120
+ # Receiver const check must accept const node shapes: :const, :colon3, and :colon2
1121
+ if recv.respond_to?(:type)
1122
+ recv_type = recv.type.to_s.downcase.to_sym
1123
+ if [:const, :colon3].include?(recv_type)
1124
+ const_name = recv.children[0]
1125
+ reject_unsafe(node, "access to #{const_name}.#{name}") if FORBIDDEN_SYNTHESIS_CONSTS.include?(const_name)
1126
+ elsif recv_type == :colon2
1127
+ const_name = recv.children[1]
1128
+ reject_unsafe(node, "access to #{const_name}.#{name}") if FORBIDDEN_SYNTHESIS_CONSTS.include?(const_name)
1129
+ end
1036
1130
  end
1037
1131
  end
1038
1132
 
@@ -16,13 +16,24 @@ module Rubik
16
16
  DEFAULT_ROOT = "~/.rubik/rubikstorage"
17
17
 
18
18
  # Noktalı anahtar okuması her iki config şeklinde aynı çalışsın:
19
- # Rubik::Config#fetch noktalı yolları böler, düz Hash#fetch BÜTÜN
20
- # dizgiyi tek anahtar sayar — Hash için dig, diğerleri için fetch.
19
+ # Rubik::Config#fetch noktalı yolları böler, düz Hash için hem düz
20
+ # anahtar (config[key]) hem de iç içe Hash (dig) desteklenir —
21
+ # Hash için flat || dig, diğerleri için fetch, nil durumunda default.
21
22
  def config_get(config, key, default = nil)
22
23
  return default if config.nil?
23
24
 
24
25
  val = if config.is_a?(Hash)
25
- config.dig(*key.to_s.split("."))
26
+ if config.key?(key)
27
+ config[key]
28
+ elsif config.key?(key.to_s)
29
+ config[key.to_s]
30
+ else
31
+ begin
32
+ config.dig(*key.to_s.split("."))
33
+ rescue TypeError
34
+ nil
35
+ end
36
+ end
26
37
  elsif config.respond_to?(:fetch)
27
38
  config.fetch(key, nil)
28
39
  end