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
@@ -7,6 +7,8 @@ require "time"
7
7
  # save_manifest atomik yazımı Platform.atomic_write kullanır; dosyanın tek başına
8
8
  # require edilmesi durumunda da çözümlenebilsin diye açık yüklenir.
9
9
  require_relative "../platform"
10
+ require_relative "synthesized_command_context"
11
+ require_relative "synthesized_tool_context"
10
12
 
11
13
  module Rubik
12
14
  module Runtime
@@ -16,10 +18,12 @@ module Rubik
16
18
  COMMAND_SENTINEL = "# Rubik Custom Command:"
17
19
  TOOL_SENTINEL = "# Rubik Custom Tool:"
18
20
 
19
- def initialize(root_dir: Dir.pwd, home_dir: ENV.fetch("HOME", nil), guard: nil)
21
+ def initialize(root_dir: Dir.pwd, home_dir: ENV.fetch("HOME", nil), guard: nil, runtime_manager: nil, runtime_context: nil)
20
22
  @root_dir = File.expand_path(root_dir)
21
23
  @home_dir = File.expand_path(home_dir || ENV["HOME"] || ".")
22
24
  @guard = guard
25
+ @runtime_manager = runtime_manager
26
+ @runtime_context = runtime_context
23
27
  ensure_directories
24
28
  end
25
29
 
@@ -228,27 +232,39 @@ module Rubik
228
232
  end
229
233
 
230
234
  registered = shell.register_command(cmd_name, description: "#{description} [#{scope}]", custom: true) do |args, ctx|
231
- # Arity contract mirrored from SynthesizeCommandTool: 2-arity
232
- # handlers receive the per-dispatch CommandContext (its IO
233
- # methods delegate to the active capture stream), 3-arity keeps
234
- # the raw shell for trusted persisted code. Direct invocation
235
- # outside dispatch has no ctx: use the shell's factory, or its
236
- # raw output for shell doubles predating the contract. The warn
237
- # keeps a ctx-forgetting dispatch site visible instead of
238
- # silently auto-healed.
239
- out_ctx = if ctx
240
- ctx
241
- elsif shell.respond_to?(:command_context)
242
- warn "[rubik] overlay command invoked without a dispatch CommandContext" unless ENV["RUBIK_TEST"]
243
- shell.command_context
244
- else
245
- shell.output
246
- end
247
235
  if proc_handler.arity == 1
248
236
  proc_handler.call(args)
249
237
  elsif proc_handler.arity == 2
250
- proc_handler.call(args, out_ctx)
238
+ warn "[rubik] overlay command invoked without a dispatch CommandContext" unless ctx || ENV["RUBIK_TEST"]
239
+ sealed_ctx = if ctx.is_a?(Rubik::REPL::SynthesizedCommandContext)
240
+ ctx
241
+ else
242
+ output_io = if ctx.respond_to?(:out)
243
+ ctx.out
244
+ elsif ctx
245
+ ctx
246
+ elsif shell.respond_to?(:output)
247
+ shell.output
248
+ else
249
+ $stdout
250
+ end
251
+ runtime_ctx = if shell.respond_to?(:runtime_manager)
252
+ shell.runtime_manager&.context
253
+ elsif ctx.respond_to?(:runtime_manager)
254
+ ctx.runtime_manager&.context
255
+ end
256
+ Rubik::REPL::SynthesizedCommandContext.new(output: output_io, coeffects: runtime_ctx || {})
257
+ end
258
+ proc_handler.call(args, sealed_ctx)
251
259
  else
260
+ out_ctx = if ctx
261
+ ctx
262
+ elsif shell.respond_to?(:command_context)
263
+ warn "[rubik] overlay command invoked without a dispatch CommandContext" unless ENV["RUBIK_TEST"]
264
+ shell.command_context
265
+ else
266
+ shell.output
267
+ end
252
268
  proc_handler.call(args, out_ctx.respond_to?(:out) ? out_ctx.out : out_ctx, shell)
253
269
  end
254
270
  end
@@ -314,7 +330,7 @@ module Rubik
314
330
  verified_dir_entries(project_tools_dir, :project)
315
331
  end
316
332
 
317
- def load_tools(tool_registry)
333
+ def load_tools(tool_registry, runtime_manager: nil, runtime_context: nil)
318
334
  loaded = []
319
335
  list_tools.each do |t_meta|
320
336
  next if t_meta[:integrity] == :rejected
@@ -324,7 +340,7 @@ module Rubik
324
340
  ensure_contained_path!(file_path, target_dir)
325
341
 
326
342
  code = File.read(t_meta[:path])
327
- tool_inst = eval_tool(t_meta[:name], code, t_meta[:scope])
343
+ tool_inst = eval_tool(t_meta[:name], code, t_meta[:scope], runtime_manager: runtime_manager, runtime_context: runtime_context)
328
344
  if tool_inst
329
345
  tool_registry.register(tool_inst, origin: :overlay)
330
346
  loaded << t_meta[:name]
@@ -337,7 +353,7 @@ module Rubik
337
353
  loaded
338
354
  end
339
355
 
340
- def eval_tool(name, code, scope)
356
+ def eval_tool(name, code, scope, runtime_manager: nil, runtime_context: nil)
341
357
  desc_match = code.match(/# Description:\s*(.*)/)
342
358
  description = desc_match ? desc_match[1].strip : "Custom #{scope} tool"
343
359
 
@@ -346,6 +362,10 @@ module Rubik
346
362
  begin
347
363
  JSON.parse(schema_match[1])
348
364
  rescue JSON::ParserError
365
+ # A malformed schema silently claiming "no arguments"
366
+ # hides real overlay bugs; keep loading (the tool is
367
+ # hash-verified) but say so.
368
+ warn "[rubik] overlay tool '#{name}' has a malformed '# Schema:' header; treating as no-arg schema"
349
369
  {}
350
370
  end
351
371
  else
@@ -364,14 +384,25 @@ module Rubik
364
384
  if evaluated.is_a?(Class) && evaluated < Rubik::Tools::BaseTool
365
385
  evaluated.new
366
386
  elsif evaluated.respond_to?(:call)
387
+ runtime_mgr = runtime_manager || @runtime_manager
388
+ runtime_ctx = runtime_context || @runtime_context || (runtime_mgr&.context if runtime_mgr.respond_to?(:context)) || Rubik::Runtime::Context.new
367
389
  Class.new(Rubik::Tools::BaseTool) do
368
390
  define_method(:initialize) do
369
391
  super(name: name, description: "#{description} [#{scope}]", risk: risk, params_schema: schema)
370
392
  @handler = evaluated
393
+ @runtime_context = runtime_ctx
371
394
  end
372
395
 
373
- define_method(:execute) do |args = {}|
374
- @handler.call(args)
396
+ define_method(:execute) do |args = {}, ctx = nil|
397
+ if @handler.respond_to?(:arity) && @handler.arity.zero?
398
+ @handler.call
399
+ elsif Rubik::Runtime::SynthesizedToolContext.accepts_context?(@handler)
400
+ effective_ctx = ctx || @runtime_context || Rubik::Runtime::Context.new
401
+ sealed_ctx = Rubik::Runtime::SynthesizedToolContext.wrap(effective_ctx)
402
+ @handler.call(args, sealed_ctx)
403
+ else
404
+ @handler.call(args)
405
+ end
375
406
  end
376
407
  end.new
377
408
  end
@@ -11,8 +11,11 @@ module Rubik
11
11
  module Runtime
12
12
  # Functional checks own their resources through the caller's probe context.
13
13
  class RepairProbe
14
- def initialize(root_dir:)
14
+ ALLOWED_PROBE_FLAGS = ["--help", "-h", "help", "--version", "-v", "-V", "version", "--status"].freeze
15
+
16
+ def initialize(root_dir:, guard: nil)
15
17
  @root_dir = root_dir
18
+ @guard = guard
16
19
  end
17
20
 
18
21
  PROBE_TIMEOUT = 5
@@ -26,6 +29,15 @@ module Rubik
26
29
  return probe_failure(proposed_cmd, 127, "Availability check failed: executable not found in PATH")
27
30
  end
28
31
 
32
+ if @guard
33
+ cmd_to_check = proposed_cmd.is_a?(Hash) ? [proposed_cmd[:executable], *Array(proposed_cmd[:args])].join(" ") : proposed_cmd.to_s
34
+ begin
35
+ @guard.verify_command_safety!(cmd_to_check)
36
+ rescue Rubik::Security::BlockedWriteError => e
37
+ return probe_failure(proposed_cmd, 1, "Proposed command blocked by security guard: #{e.message}")
38
+ end
39
+ end
40
+
29
41
  if target == "lsp"
30
42
  client = Rubik::LSP::Client.new(lang, root_dir: @root_dir, server_cmd: proposed_cmd)
31
43
  sub_ctx.on_dispose { client.stop }
@@ -45,8 +57,14 @@ module Rubik
45
57
  end
46
58
 
47
59
  probe_args = ["--help"]
60
+ else
61
+ return probe_failure(proposed_cmd, 1, "probe_args must be a non-empty array of strings") unless probe_args.is_a?(Array) && !probe_args.empty? && probe_args.all?(String)
62
+
63
+ disallowed = probe_args.reject { |arg| ALLOWED_PROBE_FLAGS.include?(arg) }
64
+ if disallowed.any?
65
+ return probe_failure(proposed_cmd, 1, "Disallowed probe_args: only safe diagnostic flags #{ALLOWED_PROBE_FLAGS.inspect} are allowed (got: #{disallowed.inspect})")
66
+ end
48
67
  end
49
- return probe_failure(proposed_cmd, 1, "probe_args must be a non-empty array of strings") unless probe_args.is_a?(Array) && !probe_args.empty? && probe_args.all?(String)
50
68
 
51
69
  argv.concat(probe_args)
52
70
  # Array-form spawn never interprets shell operators. File-backed output
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../coeffects"
4
+ require_relative "../log"
5
+
6
+ module Rubik
7
+ module Runtime
8
+ # Shared capability gate for sealed execution contexts (synthesized tools
9
+ # and commands). The Guard vets the synthesized code itself; this
10
+ # deny-list is the gate on what that code may resolve through the sealed
11
+ # context's coeffect/require path.
12
+ module SealedCoeffects
13
+ # Coeffects that must NEVER be leaked to synthesized code. Canonical
14
+ # keys (Rubik::Coeffects); legacy alias spellings canonicalize to their
15
+ # canonical form before the check, so aliases of a blocked service are
16
+ # denied without being restated here. App-domain coeffects (e.g.
17
+ # storage) stay reachable by design.
18
+ BLOCKED_COEFFECTS = [
19
+ "guard",
20
+ "config",
21
+ "sandbox",
22
+ "sandbox_runner",
23
+ "shell",
24
+ "buffer_manager",
25
+ "runtime_manager",
26
+ "task_manager",
27
+ "subagent_supervisor",
28
+ "init",
29
+ "dispatcher",
30
+ "commands",
31
+ "tool_registry",
32
+ "adapter_registry",
33
+ "mcp_manager",
34
+ "llm_client",
35
+ "router",
36
+ "plugin_manager",
37
+ "plugin_storage",
38
+ "lsp_manager",
39
+ "skill_manager"
40
+ ].freeze
41
+
42
+ private
43
+
44
+ # A legacy alias of a blocked service (e.g. "tools" for
45
+ # "tool_registry") must not slip through under the unblocked spelling.
46
+ # Denials log at warn: audible at the default level, since a blocked
47
+ # probe is a security event. Silence toward synthesized code (the
48
+ # command path and get/has?) is deliberate — the log is the operator
49
+ # record; the error class stays distinguishable host-side only.
50
+ def blocked?(key)
51
+ canonical = Rubik::Coeffects.canonical(key)
52
+ blocked = BLOCKED_COEFFECTS.include?(key) || BLOCKED_COEFFECTS.include?(canonical)
53
+ Rubik::Log.warn(:runtime, "sealed context denied coeffect '#{key}'") if blocked
54
+ blocked
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sealed_coeffects"
4
+
5
+ module Rubik
6
+ module Runtime
7
+ # Sealed execution context for synthesized and overlay commands (Pillar 2, Feature 11).
8
+ #
9
+ # Unlike the interactive REPL `CommandContext` (which exposes `guard`, `config`,
10
+ # `sandbox_runner`, `buffer_manager`, `shell`, and session mutations),
11
+ # `SynthesizedCommandContext` exposes ONLY:
12
+ # - Output methods: `out` (wrapped stream), `puts`, `print`, `flush` (and alias `output`)
13
+ # - Safe mediated coeffects: `coeffect(name)`
14
+ #
15
+ # All access to internal subsystems (.guard, .config, .sandbox_runner, .shell,
16
+ # .buffer_manager, .commands, etc.) is blocked; calling them raises NoMethodError.
17
+ class SynthesizedCommandContext
18
+ include SealedCoeffects
19
+
20
+ attr_reader :out
21
+ alias output out
22
+
23
+ def self.wrap(ctx, shell: nil, runtime_manager: nil)
24
+ return ctx if ctx.is_a?(self)
25
+
26
+ output_io = if ctx.respond_to?(:out) then ctx.out
27
+ elsif ctx then ctx
28
+ elsif shell.respond_to?(:output) then shell.output
29
+ else $stdout
30
+ end
31
+ runtime_ctx = if runtime_manager&.context then runtime_manager.context
32
+ elsif shell.respond_to?(:runtime_manager) then shell.runtime_manager&.context
33
+ elsif ctx.respond_to?(:runtime_manager) then ctx.runtime_manager&.context
34
+ end
35
+ new(output: output_io, coeffects: runtime_ctx || {})
36
+ end
37
+
38
+ def initialize(output: $stdout, coeffects: {})
39
+ @out = output || $stdout
40
+ @coeffects = coeffects || {}
41
+ end
42
+
43
+ # IO conveniences delegating to @out
44
+ def puts(*)
45
+ @out.puts(*)
46
+ end
47
+
48
+ def print(*)
49
+ @out.print(*)
50
+ end
51
+
52
+ def flush
53
+ @out.flush if @out.respond_to?(:flush)
54
+ end
55
+
56
+ # Safe mediated coeffect accessor.
57
+ # Returns nil if the coeffect is blocked or not satisfied.
58
+ def coeffect(name)
59
+ return nil if name.nil?
60
+
61
+ key = name.to_s.downcase.strip
62
+ return nil if blocked?(key)
63
+
64
+ if @coeffects.is_a?(Hash)
65
+ @coeffects[key] || @coeffects[name] || @coeffects[name.to_s] || @coeffects[name.to_sym]
66
+ elsif @coeffects.respond_to?(:get)
67
+ @coeffects.get(key)
68
+ elsif @coeffects.respond_to?(:resolve)
69
+ @coeffects.resolve(key)
70
+ elsif @coeffects.respond_to?(:[])
71
+ @coeffects[key] || @coeffects[name]
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ module REPL
78
+ SynthesizedCommandContext = Rubik::Runtime::SynthesizedCommandContext
79
+ end
80
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../coeffects"
4
+ require_relative "context"
5
+ require_relative "sealed_coeffects"
6
+
7
+ module Rubik
8
+ module Runtime
9
+ # Sealed read-only execution context for synthesized and overlay tools (Pillar 2 / Pillar 3).
10
+ #
11
+ # Modeled after `Rubik::REPL::SynthesizedCommandContext`. Exposes ONLY read-only coeffect
12
+ # accessors (`require`, `get`, `has?`, `satisfied?`, `coeffect`).
13
+ #
14
+ # All mutating/lifecycle/parent methods (`dispose`, `provide`, `unprovide`, `parent`,
15
+ # `extend`, `register_disposable`, `on_dispose`, etc.) are blocked with NoMethodError.
16
+ class SynthesizedToolContext
17
+ include SealedCoeffects
18
+
19
+ # Positional parameter kinds accepted as evidence of a 2-arity handler.
20
+ POSITIONAL_KINDS = [:req, :opt, :rest].freeze
21
+
22
+ # Remove Object#extend so calling ctx.extend raises NoMethodError
23
+ undef_method :extend
24
+
25
+ class << self
26
+ # Idempotently wrap an existing context in a sealed SynthesizedToolContext.
27
+ def wrap(context)
28
+ return context if context.is_a?(self)
29
+
30
+ new(context)
31
+ end
32
+
33
+ # Determines whether a tool handler expects or permits context as its 2nd argument.
34
+ #
35
+ # Evaluates positional parameter count (:req, :opt, :rest), ignoring keyword
36
+ # and block parameters.
37
+ def accepts_context?(handler)
38
+ return false unless handler.respond_to?(:call)
39
+
40
+ params = if handler.respond_to?(:parameters)
41
+ handler.parameters
42
+ elsif handler.respond_to?(:method)
43
+ begin
44
+ handler.method(:call).parameters
45
+ rescue StandardError
46
+ []
47
+ end
48
+ else
49
+ []
50
+ end
51
+
52
+ if params.empty?
53
+ return handler.arity == 2 if handler.respond_to?(:arity)
54
+
55
+ return false
56
+ end
57
+
58
+ # params is an Array of [type, name] pairs (Method#parameters), so a
59
+ # Hash-style slice would be wrong here — filter by positional kinds.
60
+ pos_params = params.select { |param| POSITIONAL_KINDS.include?(param[0]) }
61
+ req_count = pos_params.count { |param| param[0] == :req }
62
+
63
+ 2.between?(req_count, pos_params.size)
64
+ end
65
+ end
66
+
67
+ def initialize(context = nil)
68
+ @context = context
69
+ end
70
+
71
+ # Safe read-only coeffect dependency resolution.
72
+ def require(name)
73
+ raise CoeffectMissingError, "Required coeffect '#{name}' is not satisfied in the context." if name.nil?
74
+
75
+ key = name.to_s.downcase.strip
76
+ raise CoeffectBlockedError, "Required coeffect '#{key}' is denied by sealed-context policy." if blocked?(key)
77
+
78
+ val = get(name)
79
+ return val unless val.nil?
80
+
81
+ raise CoeffectMissingError, "Required coeffect '#{name}' is not satisfied in the context."
82
+ end
83
+
84
+ # Safe mediated coeffect reader.
85
+ def get(name, default = nil)
86
+ return default if name.nil?
87
+
88
+ key = name.to_s.downcase.strip
89
+ return default if blocked?(key)
90
+
91
+ val = if @context.respond_to?(:get)
92
+ res = @context.get(key)
93
+ res = @context.get(name) if res.nil? && name.to_s != key
94
+ res
95
+ elsif @context.is_a?(Hash)
96
+ @context[key] || @context[name] || @context[name.to_s] || @context[name.to_sym]
97
+ elsif @context.respond_to?(:resolve)
98
+ @context.resolve(key)
99
+ elsif @context.respond_to?(:[])
100
+ @context[key] || @context[name]
101
+ end
102
+
103
+ val.nil? ? default : val
104
+ end
105
+
106
+ def has?(name)
107
+ return false if name.nil?
108
+
109
+ # get denies blocked keys (returns the default), so a single gate is
110
+ # enough and the denial logs once.
111
+ !get(name).nil?
112
+ end
113
+
114
+ def satisfied?(dependencies)
115
+ Array(dependencies).all? { |dep| has?(dep) }
116
+ end
117
+
118
+ def coeffect(name)
119
+ get(name)
120
+ end
121
+ end
122
+ end
123
+ end
data/lib/rubik/runtime.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "runtime/context"
4
+ require_relative "runtime/synthesized_command_context"
5
+ require_relative "runtime/synthesized_tool_context"
4
6
  require_relative "runtime/fiber"
5
7
  require_relative "runtime/manager"
6
8
  require_relative "runtime/service_definition"
@@ -29,6 +29,10 @@ module Rubik
29
29
  @default_database ||= CommandDatabase.new
30
30
  end
31
31
 
32
+ def self.reset_database!
33
+ @default_database = nil
34
+ end
35
+
32
36
  def self.classify_expression(expression, database: nil, shell: nil)
33
37
  new(database: database, shell: shell).classify(expression)
34
38
  end
@@ -349,11 +353,14 @@ module Rubik
349
353
 
350
354
  p = p[1..-2] if (p.start_with?('"') && p.end_with?('"')) || (p.start_with?("'") && p.end_with?("'"))
351
355
  return false if p.empty? || p.start_with?("-")
352
- return false if !p.include?("..") && temp_path?(p)
356
+ return false if !p.include?("..") && (temp_path?(p) || DEV_PATHS.any? { |dev| p == dev || p.start_with?(dev) })
357
+
358
+ # Bare drive prefix without separator is NOT a drive root or system path
359
+ return false if p =~ %r{\A[a-zA-Z]:(?![\\/])}
353
360
 
354
361
  return true if p =~ %r{\A/(?:\*|\.+)?/?\z}
355
- return true if p =~ %r{\A[a-zA-Z]:(?:[\\/]|\*|\.+)?\z}i
356
- return true if HOME_TOKENS.include?(p)
362
+ return true if p =~ %r{\A[a-zA-Z]:[\\/](?:\*|\.+)?\z}i
363
+ return true if HOME_TOKENS.include?(p) || p.start_with?("~/", "$HOME/", "${HOME}/")
357
364
 
358
365
  if p =~ %r{\A([a-zA-Z]):[/\\](.*)\z}
359
366
  parts = []
@@ -361,11 +368,11 @@ module Rubik
361
368
  win_norm = "#{Regexp.last_match(1).downcase}:/#{parts.join('/')}".downcase
362
369
  return false if win_norm.include?("/appdata/local/temp/") || win_norm.end_with?("/appdata/local/temp")
363
370
 
364
- return true if win_norm =~ %r{\A[a-z]:/(?:windows|program files|program files \(x86\)|system32)(?:/|\z)} ||
365
- win_norm =~ %r{\A[a-z]:/users/?(?:\*|\.+)?/?\z} ||
366
- win_norm =~ %r{\A[a-z]:/?(?:\*|\.+)?/?\z}
367
-
368
- return false
371
+ if win_norm =~ %r{\A[a-z]:/(?:windows|program files|program files \(x86\)|system32)(?:/|\z)} ||
372
+ win_norm =~ %r{\A[a-z]:/users/?(?:\*|\.+)?/?\z} ||
373
+ win_norm =~ %r{\A[a-z]:/?(?:\*|\.+)?/?\z}
374
+ return true
375
+ end
369
376
  end
370
377
 
371
378
  expanded = safe_expand_path(p)
@@ -382,6 +389,7 @@ module Rubik
382
389
 
383
390
  false
384
391
  end
392
+ public :system_path?
385
393
 
386
394
  def safe_expand_path(path)
387
395
  File.expand_path(path)
@@ -0,0 +1,49 @@
1
+ # $schema: ../../../schemas/command.schema.json
2
+ command: biome
3
+ description: "Toolchain for the web (formatter, linter, etc.)"
4
+ classification: READONLY
5
+ risk: LOW
6
+ strict: false
7
+ global_options:
8
+ --version: {overrides: READONLY}
9
+ -v: {overrides: READONLY, aliases: [--version]}
10
+ --help: {overrides: READONLY}
11
+ -h: {overrides: READONLY, aliases: [--help]}
12
+ --config-path: {takes_value: true}
13
+ subcommands:
14
+ format:
15
+ classification: READONLY
16
+ strict: false
17
+ options:
18
+ --write: {overrides: LOCAL_EFFECTS}
19
+ --stdin-file-path: {takes_value: true}
20
+ lint:
21
+ classification: READONLY
22
+ strict: false
23
+ options:
24
+ --write: {overrides: LOCAL_EFFECTS}
25
+ check:
26
+ classification: READONLY
27
+ strict: false
28
+ options:
29
+ --write: {overrides: LOCAL_EFFECTS}
30
+ --apply: {overrides: LOCAL_EFFECTS, aliases: [--write]}
31
+ --apply-unsafe: {overrides: LOCAL_EFFECTS}
32
+ ci:
33
+ classification: READONLY
34
+ strict: false
35
+ version:
36
+ classification: READONLY
37
+ strict: false
38
+ explain:
39
+ classification: READONLY
40
+ strict: false
41
+ rage:
42
+ classification: READONLY
43
+ strict: false
44
+ init:
45
+ classification: LOCAL_EFFECTS
46
+ strict: false
47
+ migrate:
48
+ classification: LOCAL_EFFECTS
49
+ strict: false
@@ -0,0 +1,91 @@
1
+ # $schema: ../../../schemas/command.schema.json
2
+ command: bun
3
+ description: "All-in-one JavaScript and TypeScript toolkit and runtime"
4
+ classification: DANGEROUS
5
+ strict: false
6
+ global_options:
7
+ --version: {overrides: READONLY}
8
+ -v: {overrides: READONLY, aliases: [--version]}
9
+ --help: {overrides: READONLY}
10
+ -h: {overrides: READONLY, aliases: [--help]}
11
+ --cwd: {takes_value: true, captures_directory: true}
12
+ subcommands:
13
+ test:
14
+ classification: LOCAL_EFFECTS
15
+ risk: LOW
16
+ strict: false
17
+ options:
18
+ --bail: {}
19
+ -b: {aliases: [--bail]}
20
+ --timeout: {takes_value: true}
21
+ -t: {takes_value: true, aliases: [--timeout]}
22
+ --rerun-each: {takes_value: true}
23
+ --only: {}
24
+ --todo: {}
25
+ --watch: {}
26
+ --coverage: {}
27
+ --update-snapshots: {}
28
+ -u: {aliases: [--update-snapshots]}
29
+ install:
30
+ classification: LOCAL_EFFECTS
31
+ risk: LOW
32
+ strict: false
33
+ aliases: [i]
34
+ add:
35
+ classification: LOCAL_EFFECTS
36
+ risk: LOW
37
+ strict: false
38
+ aliases: [a]
39
+ remove:
40
+ classification: LOCAL_EFFECTS
41
+ risk: LOW
42
+ strict: false
43
+ aliases: [rm]
44
+ update:
45
+ classification: LOCAL_EFFECTS
46
+ risk: LOW
47
+ strict: false
48
+ link:
49
+ classification: LOCAL_EFFECTS
50
+ strict: false
51
+ unlink:
52
+ classification: LOCAL_EFFECTS
53
+ strict: false
54
+ build:
55
+ classification: LOCAL_EFFECTS
56
+ strict: false
57
+ init:
58
+ classification: LOCAL_EFFECTS
59
+ strict: false
60
+ upgrade:
61
+ classification: LOCAL_EFFECTS
62
+ strict: false
63
+ pm:
64
+ classification: LOCAL_EFFECTS
65
+ strict: false
66
+ subcommands:
67
+ ls:
68
+ classification: READONLY
69
+ strict: false
70
+ bin:
71
+ classification: READONLY
72
+ strict: false
73
+ cache:
74
+ classification: LOCAL_EFFECTS
75
+ strict: false
76
+ subcommands:
77
+ rm:
78
+ classification: LOCAL_EFFECTS
79
+ strict: false
80
+ run:
81
+ classification: LOCAL_EFFECTS
82
+ strict: false
83
+ x:
84
+ classification: DANGEROUS
85
+ strict: false
86
+ create:
87
+ classification: DANGEROUS
88
+ strict: false
89
+ repl:
90
+ classification: DANGEROUS
91
+ strict: false