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
@@ -5,8 +5,9 @@ require "find"
5
5
  require "digest"
6
6
  require "pathname"
7
7
  require "monitor"
8
+ require_relative "apply_transaction"
9
+ require_relative "../storage/layout"
8
10
  require_relative "diff"
9
- require_relative "path_validator"
10
11
  require_relative "../platform"
11
12
  require_relative "../security/guard"
12
13
 
@@ -57,6 +58,11 @@ module Rubik
57
58
  @last_conflicts = [].freeze
58
59
  @last_skipped = [].freeze
59
60
  @last_applied = nil
61
+ @apply_transaction = ApplyTransaction.new(
62
+ overlay: ApplyTransaction::Overlay.new(@buffer, @dirty_files, @original_hashes),
63
+ disk_policy: self,
64
+ event_bus: event_bus
65
+ )
60
66
  end
61
67
 
62
68
  def add_root(path, rw: false) # rubocop:disable Naming/MethodParameterName -- rw: bilinçli kisa kwarg
@@ -311,45 +317,28 @@ module Rubik
311
317
  end
312
318
 
313
319
  # Applies dirty buffer entries to disk (per-file atomic writes — NOT a
314
- # multi-file transaction). On a mid-run failure the partial progress is
315
- # carried by ApplyFailedError: applied entries ARE on disk, the failed
316
- # entry stays dirty, and pending entries keep their buffer content, so
317
- # a retry writes only the remainder. External-modification conflicts
318
- # are skipped (recorded in last_conflicts) unless policy is "overwrite".
320
+ # multi-file transaction). Delegates the loop to the extracted
321
+ # ApplyTransaction (issue #472); see that class for the failure and
322
+ # conflict semantics. `on_finish` runs even on a mid-run failure, so
323
+ # the run report (last_conflicts / last_skipped / last_applied) is
324
+ # recorded exactly as before.
319
325
  def apply!(target_paths = nil, policy: nil)
320
326
  @monitor.synchronize do
321
- applied_paths = []
322
- conflicts = []
323
- skipped = []
324
- keys_to_apply = determine_keys_to_apply(target_paths)
327
+ keys_to_apply = if target_paths
328
+ Array(target_paths).map { |p| sanitize_path(p, for_write: true) }
329
+ else
330
+ @dirty_files.keys.dup
331
+ end
325
332
  effective_policy = (policy || external_modification_policy).to_s.downcase
326
333
 
327
- begin
328
- keys_to_apply.each_with_index do |rel_path, index|
329
- next unless @dirty_files.key?(rel_path)
330
-
331
- unless validate_and_clean_garbage(rel_path)
332
- skipped << { path: rel_path, reason: :path_validator_rejected }
333
- next
334
- end
335
-
336
- if external_conflict?(rel_path) && effective_policy != "overwrite"
337
- conflicts << rel_path
338
- @event_bus&.emit(:buffer_apply_conflict, paths: conflicts.dup.freeze, path: rel_path)
339
- next
340
- end
341
-
342
- apply_single_entry(rel_path, index: index, keys_to_apply: keys_to_apply,
343
- applied_paths: applied_paths, conflicts: conflicts, skipped: skipped)
344
- end
345
- ensure
334
+ result = @apply_transaction.run(keys_to_apply, effective_policy) do |applied_paths, conflicts, skipped|
346
335
  @last_conflicts = conflicts.dup.freeze
347
336
  @last_skipped = skipped.dup.freeze
348
337
  @last_applied = { at: Time.now.utc, paths: applied_paths.dup.freeze } unless applied_paths.empty?
349
338
  end
350
339
 
351
- @event_bus&.emit(:buffer_applied, paths: applied_paths.dup.freeze) unless applied_paths.empty?
352
- applied_paths
340
+ @event_bus&.emit(:buffer_applied, paths: result.applied.dup.freeze) unless result.applied.empty?
341
+ result.applied
353
342
  end
354
343
  end
355
344
 
@@ -393,76 +382,11 @@ module Rubik
393
382
  end
394
383
  end
395
384
 
396
- private
397
-
398
- def determine_keys_to_apply(target_paths)
399
- if target_paths
400
- Array(target_paths).map { |p| sanitize_path(p, for_write: true) }
401
- else
402
- @dirty_files.keys.dup
403
- end
404
- end
405
-
406
- def validate_and_clean_garbage(rel_path)
407
- return true if PathValidator.valid?(rel_path)
408
-
409
- @dirty_files.delete(rel_path)
410
- @buffer.delete(rel_path)
411
- @original_hashes.delete(rel_path)
412
- @event_bus&.emit(:buffer_garbage_skipped, path: rel_path)
413
- false
414
- end
415
-
416
- def external_conflict?(rel_path)
417
- baseline = @original_hashes[rel_path]
418
- current_disk = disk_hash(rel_path)
419
-
420
- if baseline
421
- current_disk != baseline
422
- else
423
- !current_disk.nil?
424
- end
425
- end
426
-
427
- def apply_single_entry(rel_path, index:, keys_to_apply:, applied_paths:, conflicts:, skipped:)
428
- full_path = File.expand_path(rel_path, @root_dir)
429
-
430
- # TOCTOU close: re-verify containment immediately before the
431
- # atomic write — a symlink planted between write-time sanitize
432
- # and apply! must not redirect the write outside the workspace.
433
- # Fail loud: an untrustworthy path is an attack signal or a
434
- # mid-flight root misconfiguration, not a content problem; the
435
- # entry stays dirty for a retry after remediation.
436
- containment_kind, = containment_root(full_path)
437
- unless containment_kind
438
- @event_bus&.emit(:buffer_containment_blocked, path: rel_path)
439
- raise apply_failure(
440
- "Write blocked: '#{rel_path}' resolves outside all allowed roots at apply time " \
441
- "(possible symlink swap since buffering). Re-verify the workspace and retry.",
442
- kind: :containment_blocked, applied: applied_paths, failed: rel_path,
443
- pending: pending_after(keys_to_apply, index), conflicts: conflicts, skipped: skipped
444
- )
445
- end
446
-
447
- content = @buffer[rel_path]
448
-
449
- begin
450
- Rubik::Platform.atomic_write(full_path, content)
451
- rescue StandardError => e
452
- raise apply_failure(
453
- "Apply failed for '#{rel_path}': #{e.class}: #{e.message}; " \
454
- "#{applied_paths.empty? ? 'no file was written' : "already written: #{applied_paths.join(', ')}"}; " \
455
- "#{pending_after(keys_to_apply, index).size} file(s) still dirty",
456
- kind: :write_failed, applied: applied_paths, failed: rel_path,
457
- pending: pending_after(keys_to_apply, index), conflicts: conflicts, skipped: skipped
458
- ), cause: e
459
- end
460
-
461
- @original_hashes[rel_path] = Digest::SHA256.hexdigest(content)
462
- @dirty_files.delete(rel_path)
463
- @buffer.delete(rel_path)
464
- applied_paths << rel_path
465
- end
385
+ # -- Disk policy adapter for ApplyTransaction (issue #472) ------------
386
+ # Manager serves as the transaction's `disk_policy`: these three are
387
+ # the workspace path policy the apply loop consults. Public because
388
+ # the transaction calls them across the object boundary; not part of
389
+ # the dictionary-facing Manager API.
466
390
 
467
391
  # Single source of boundary containment for both the sanitize_path
468
392
  # entry check and the apply! re-verification. Returns [kind, dir] with
@@ -481,7 +405,7 @@ module Rubik
481
405
 
482
406
  def external_modification_policy
483
407
  raw = if @config.is_a?(Rubik::Config)
484
- @config.fetch("buffer.on_external_modification", "skip")
408
+ Rubik::Storage::Layout.config_get(@config, "buffer.on_external_modification", "skip")
485
409
  elsif @config.respond_to?(:dig)
486
410
  @config.dig("buffer", "on_external_modification") ||
487
411
  (@config["buffer.on_external_modification"] if @config.respond_to?(:[])) ||
@@ -500,24 +424,6 @@ module Rubik
500
424
  rescue Errno::ENOENT, Errno::EACCES
501
425
  nil
502
426
  end
503
-
504
- # Build the structured partial-apply error with frozen report copies —
505
- # a reader must see the state at raise time, not later mutations.
506
- def apply_failure(message, kind:, applied:, failed:, pending:, conflicts:, skipped: [])
507
- ApplyFailedError.new(
508
- message, kind: kind,
509
- applied: applied.dup.freeze, failed: failed,
510
- pending: pending.dup.freeze, conflicts: conflicts.dup.freeze,
511
- skipped: skipped.dup.freeze
512
- )
513
- end
514
-
515
- # This run's remaining dirty entries after the failed index — exactly
516
- # what a retry will still have to write (garbage-skipped entries are
517
- # already out of dirty_files when this runs).
518
- def pending_after(keys, index)
519
- keys[(index + 1)..].to_a.select { |k| @dirty_files.key?(k) }
520
- end
521
427
  end
522
428
  end
523
429
  end
@@ -4,7 +4,10 @@ module Rubik
4
4
  class Config
5
5
  module DeepOps
6
6
  SENSITIVE_PROJECT_CONFIG_KEYS = [
7
- "security", "sandbox", "plugins", "mcp", "lsp", "project", "test", "linter", "adapter"
7
+ "security", "sandbox", "plugins", "mcp", "lsp", "project", "test", "linter", "adapter",
8
+ # Provider specs carry credentials and endpoints; an untrusted
9
+ # project must not be able to inject either.
10
+ "providers"
8
11
  ].freeze
9
12
 
10
13
  private
@@ -49,6 +49,9 @@ module Rubik
49
49
  # current behavior). RUBIK_TOOLS_CALL_TIMEOUT env overrides the file.
50
50
  # A timed-out call is abandoned, not killed — bounds turn latency.
51
51
  "call_timeout" => nil,
52
+ # Grace-then-kill reaper (issue #449): seconds an abandoned worker may
53
+ # run past call_timeout before the reaper kills it; the value IS the grace. nil = off.
54
+ "timeout_kill_after" => nil,
52
55
  # Canonical tool name => seconds; 0/false disables for that tool.
53
56
  "call_timeout_overrides" => {},
54
57
  "native" => "auto"
@@ -67,10 +70,10 @@ module Rubik
67
70
  "prompt_on_startup" => true, "isolation_note" => true # append the 🔍 isolation line to run_command output (sandbox/runner.rb)
68
71
  },
69
72
  "security" => {
70
- # NOTE: no audit_log_path default — Guard.default_audit_log_path (security/guard.rb) is the
71
- # single source so the value resolves at call time; a load-time snapshot would pin require-time HOME.
73
+ # NOTE: no audit_log_path default — Guard.default_audit_log_path is the single source; call-time resolution avoids pinning require-time HOME.
72
74
  "command_allowlist" => [], # operator-managed token-prefix entries (e.g. "node .gitnexus/run.cjs")
73
- "allow_safe_probes" => true # `bash --version`-style version/help probes bypass the blocklist
75
+ "allow_safe_probes" => true, # `bash --version`-style version/help probes bypass the blocklist
76
+ "allow_project_secrets" => false # SecretPolicy: project plaintext refused by default; true = legacy
74
77
  },
75
78
  "buffer" => {
76
79
  "auto_apply" => "manual",
@@ -27,6 +27,10 @@ module Rubik
27
27
  "tools.truncation_strategy" => { type: :enum, allowed: ["head_tail", "head_only", "tail_only"] },
28
28
  # min 0: 0 is a documented disable value (tool_timeout treats <=0 as off).
29
29
  "tools.call_timeout" => { type: :integer, min: 0, nullable: true },
30
+ # Grace-then-kill reaper (issue #449): seconds an abandoned tool worker
31
+ # may keep running past its call_timeout budget before the reaper kills
32
+ # it. nil = off (pure abandonment). Non-positive is treated as off.
33
+ "tools.timeout_kill_after" => { type: :integer, min: 0, nullable: true },
30
34
  "tools.native" => { type: :enum, allowed: ["auto", "on", "off"] },
31
35
  "router.mode" => { type: :enum, allowed: ["auto", "manual", "fast", "advanced"] },
32
36
  "router.sticky" => { type: :boolean },
@@ -41,6 +45,10 @@ module Rubik
41
45
  "sandbox.shell" => { type: :enum, allowed: ["auto", "bash", "powershell"] },
42
46
  "security.allow_safe_probes" => { type: :boolean },
43
47
  "security.command_allowlist" => { type: :array },
48
+ # Escape hatch for SecretPolicy: plaintext provider secrets in the
49
+ # PROJECT config are refused by default (leak vector via commits);
50
+ # this opt-in restores the legacy behavior per workspace.
51
+ "security.allow_project_secrets" => { type: :boolean },
44
52
  "buffer.auto_apply" => { type: :enum, allowed: ["manual", "on_test", "on_finish", "always"] },
45
53
  "buffer.model_can_apply" => { type: :boolean },
46
54
  "buffer.prompt_indicator" => { type: :boolean },
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../security/secret_store"
4
+
5
+ module Rubik
6
+ class Config
7
+ # Secret-write policy — the single choke point over Config#set/#delete.
8
+ #
9
+ # Two rules, enforced where every write funnels:
10
+ # 1. Route: a plaintext value at a secret-looking path is stored in
11
+ # the OS keyring (SecretStore) and the config file only ever
12
+ # receives the "rubik-secret:<name>" pointer. Works for both
13
+ # direct paths (providers.x.api_key) and whole-provider Hash
14
+ # writes (!provider set/add rewrite providers.<n> as a Hash).
15
+ # 2. Guard: plaintext secrets are refused for the :project scope —
16
+ # project files sit inside a git worktree and leak through
17
+ # commits, copies, and shares. Escape hatch:
18
+ # security.allow_project_secrets (schema-backed boolean, default
19
+ # false). Secrets already stored as references are pointers, not
20
+ # credentials, and may live anywhere.
21
+ #
22
+ # Interface used by callers:
23
+ # - Config includes the instance helpers (guard_secret_write,
24
+ # warn_plaintext_project_secrets, secret_key?).
25
+ # - REPL surfaces use Rubik::Config::SecretPolicy.secret_key? to
26
+ # mask secret values in display output.
27
+ module SecretPolicy
28
+ # Dot-paths that end in an api_key leaf, e.g.
29
+ # "providers.openai.api_key" or a bare "api_key".
30
+ SECRET_LEAF_PATTERN = /\A(?:[\w-]+\.)*api_key\z/
31
+
32
+ module_function
33
+
34
+ def secret_key?(key)
35
+ key.to_s.match?(SECRET_LEAF_PATTERN)
36
+ end
37
+
38
+ # Store-name convention shared by the write guard and `!secrets
39
+ # migrate`: project-scope secrets get a ".project"-suffixed name so a
40
+ # project-scoped credential can never silently overwrite (or be
41
+ # overwritten by) the global one under a shared pointer.
42
+ def secret_name(path, scope)
43
+ scope.to_s.downcase.to_sym == :project ? "#{path}.project" : path.to_s
44
+ end
45
+
46
+ # Instance-side helpers (included into Config, private there).
47
+
48
+ # Returns nil when the write needs no secret handling; otherwise
49
+ # { block: "<error message>" } -> refuse the write outright
50
+ # { info: {...} } -> write proceeds as-is
51
+ # { typed_val:, info: {...} } -> write proceeds with the
52
+ # Hash rewritten to hold
53
+ # references
54
+ # persist follows Config#set's scope vocabulary (nil/"session" keep
55
+ # the value in memory only — in-process plaintext is out of scope).
56
+ def guard_secret_write(key, typed_val, persist)
57
+ return nil unless persisting?(persist)
58
+
59
+ leaves = collect_secret_leaves(key, typed_val)
60
+ raw = leaves.select { |path, val| secret_leaf?(path, val) }
61
+ return nil if raw.empty?
62
+
63
+ scope = persist.to_s.downcase.to_sym == :project ? :project : :global
64
+ # security.allow_project_secrets=true restores the legacy behavior:
65
+ # project writes act like global ones — the store is still
66
+ # preferred, but a store failure degrades to plaintext + warning
67
+ # instead of refusing the write. The override resolves ONLY from
68
+ # operator domains (global layer / env): a project config can never
69
+ # enable it for itself, or the guard would be self-serve bypassable
70
+ # by any repo-shipped .rubik/config.yml.
71
+ legacy_plaintext_ok = project_secrets_override
72
+ return { block: project_secret_error(leaves) } if scope == :project && !legacy_plaintext_ok
73
+
74
+ rewritten = deep_dup(typed_val)
75
+ stored = []
76
+ raw.each do |path, val|
77
+ store_error = nil
78
+ ref = nil
79
+ begin
80
+ ref = Rubik::Security::SecretStore.set(secret_name(path, scope), val)
81
+ rescue Rubik::Security::SecretStore::Error => e
82
+ store_error = e
83
+ end
84
+
85
+ if store_error
86
+ # gh-style degradation: the write proceeds with plaintext plus a
87
+ # loud warning — except for :project, which refuses unless the
88
+ # operator explicitly accepted the risk.
89
+ return { block: "#{project_secret_error(leaves)} (secret store failed: #{store_error.message})" } if scope == :project && !legacy_plaintext_ok
90
+
91
+ warn_secret_store_fallback(store_error, scope)
92
+ return { info: plaintext_fallback_info }
93
+ end
94
+
95
+ # Leaf paths are full config dot-paths; the rewrite target is the
96
+ # typed_val subtree, so strip the write root ("providers.<n>."
97
+ # for whole-Hash writes). A direct leaf write carries no subtree,
98
+ # so the value itself becomes the pointer.
99
+ if rewritten.is_a?(Hash)
100
+ deep_set_key(rewritten, path.delete_prefix("#{key}."), ref)
101
+ else
102
+ rewritten = ref
103
+ end
104
+ stored << path
105
+ end
106
+ { typed_val: rewritten, info: { stored: true, backend: Rubik::Security::SecretStore.backend_name, names: stored } }
107
+ end
108
+
109
+ # Boot-time honesty: surface plaintext keys already sitting in the
110
+ # project layer (legacy configs, hand-edited files).
111
+ def warn_plaintext_project_secrets
112
+ leaves = collect_secret_leaves("", @project_layer.to_h)
113
+ raw = leaves.select { |path, val| secret_leaf?(path, val) }
114
+ return if raw.empty?
115
+
116
+ @warnings << "Project config #{project_config_path} holds plaintext API key(s) at " \
117
+ "#{raw.map(&:first).join(', ')}. They can leak through commits or shared copies — " \
118
+ "run `!secrets migrate`, move them to --global, or set api_key_env instead."
119
+ end
120
+
121
+ # Keyring hygiene for Config#delete: a successfully persisted delete
122
+ # of an api_key path also drops its stored secret(s) — both scopes'
123
+ # names, so neither keyring entry outlives its reference. Best-effort
124
+ # — never masks res.
125
+ def cleanup_stored_secret(key, res, persist)
126
+ return unless secret_key?(key) && res[:ok] && persisting?(persist)
127
+
128
+ Rubik::Security::SecretStore.delete(key.to_s)
129
+ Rubik::Security::SecretStore.delete("#{key}.project")
130
+ rescue Rubik::Security::SecretStore::Error
131
+ nil
132
+ end
133
+
134
+ private
135
+
136
+ # Operator-trust domains only (env layer, then global layer). The
137
+ # merged fetch is deliberately NOT used: the project layer could
138
+ # otherwise re-enable plaintext-in-project from inside the repo.
139
+ def project_secrets_override
140
+ [@env_layer, @global_layer].any? do |layer|
141
+ layer.is_a?(Hash) && layer.dig("security", "allow_project_secrets") == true
142
+ end
143
+ end
144
+
145
+ # Depth-first walk collecting [dot-path, value] pairs for every
146
+ # api_key leaf inside a whole-Hash write (e.g. providers.<n>).
147
+ def collect_secret_leaves(prefix, obj)
148
+ leaves = []
149
+ case obj
150
+ when Hash
151
+ obj.each do |k, v|
152
+ full = prefix.to_s.empty? ? k.to_s : "#{prefix}.#{k}"
153
+ leaves.concat(collect_secret_leaves(full, v))
154
+ end
155
+ when Array
156
+ obj.each_with_index do |v, i|
157
+ leaves.concat(collect_secret_leaves(prefix.empty? ? i.to_s : "#{prefix}.#{i}", v))
158
+ end
159
+ else
160
+ leaves << [prefix.to_s, obj] if secret_key?(prefix)
161
+ end
162
+ leaves
163
+ end
164
+
165
+ # A raw secret at a secret path: a non-empty plaintext String, or a
166
+ # Numeric (parse_scalar_value coerces "12345" to an Integer before the
167
+ # guard runs — a digit-only api_key must not slip past). References
168
+ # and nils are pointers / clears and always allowed.
169
+ def secret_leaf?(path, val)
170
+ return false unless secret_key?(path)
171
+ return false if val.nil? || Rubik::Security::SecretStore.reference?(val)
172
+
173
+ (val.is_a?(String) && !val.empty?) || val.is_a?(Numeric)
174
+ end
175
+
176
+ def project_secret_error(leaves)
177
+ paths = leaves.map(&:first).join(", ")
178
+ "refusing to store plaintext secret(s) at #{paths} in the project config " \
179
+ "(#{project_config_path}): project files live inside the repository and leak through " \
180
+ "commits or shared copies. Use --global, set providers.<name>.api_key_env to read the key " \
181
+ "from the environment, or run `!secrets migrate`. Override only if you accept the risk: " \
182
+ "security.allow_project_secrets = true."
183
+ end
184
+
185
+ def warn_secret_store_fallback(error, scope)
186
+ msg = "OS secret store unavailable (#{error.message}); plaintext kept in #{scope} config"
187
+ @warnings << msg
188
+ warn "[WARN] #{msg}" if ENV["RUBIK_DEBUG"]
189
+ end
190
+
191
+ def plaintext_fallback_info
192
+ { stored: false, backend: Rubik::Security::SecretStore.backend_name,
193
+ warning: "secret store unavailable; plaintext kept in config" }
194
+ end
195
+ end
196
+ end
197
+ end
data/lib/rubik/config.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "config/model_catalog"
8
8
  require_relative "config/pruner"
9
9
  require_relative "config/schema"
10
10
  require_relative "config/env_loader"
11
+ require_relative "config/secret_policy"
11
12
  require_relative "config/scoped"
12
13
 
13
14
  module Rubik
@@ -16,6 +17,7 @@ module Rubik
16
17
  include DeepOps
17
18
  include Store
18
19
  include ModelCatalog
20
+ include SecretPolicy
19
21
 
20
22
  DEFAULT_CONFIG = Defaults::DEFAULT_CONFIG
21
23
  SENSITIVE_PROJECT_CONFIG_KEYS = DeepOps::SENSITIVE_PROJECT_CONFIG_KEYS
@@ -89,6 +91,11 @@ module Rubik
89
91
  validate_schema(typed_val, section, key.to_s) if section.is_a?(Hash) && !section.empty?
90
92
  end
91
93
 
94
+ policy = guard_secret_write(key, typed_val, persist)
95
+ return { ok: false, error: policy[:block] } if policy&.key?(:block)
96
+
97
+ typed_val = policy[:typed_val] if policy&.key?(:typed_val)
98
+
92
99
  deep_set_key(@data, key, typed_val)
93
100
 
94
101
  if persisting?(persist)
@@ -102,6 +109,7 @@ module Rubik
102
109
 
103
110
  res = persisting?(persist) ? persist_result(save!(persist)) : { ok: true }
104
111
  res[:reason] = :persist_failed unless res[:ok]
112
+ res[:secret] = policy[:info] if policy&.key?(:info)
105
113
  notify_change(key, typed_val, old_val) if old_val != typed_val
106
114
  res
107
115
  end
@@ -159,6 +167,7 @@ module Rubik
159
167
  notify_change(key, new_val, old_val)
160
168
  @event_bus&.emit(:config_deleted, key: key.to_s, old_val: old_val)
161
169
  end
170
+ cleanup_stored_secret(key, res, persist)
162
171
  res
163
172
  end
164
173
  end
@@ -290,6 +299,8 @@ module Rubik
290
299
  @env_layer = EnvLoader.load_env(ENV, DEFAULT_CONFIG, @warnings)
291
300
  @session_layer = {}
292
301
 
302
+ warn_plaintext_project_secrets
303
+
293
304
  recompute_data!
294
305
  validate_schema(@data)
295
306
  @data
@@ -37,15 +37,6 @@ module Rubik
37
37
  @mutex.synchronize { @entries << entry }
38
38
  end
39
39
 
40
- # Adds multiple Entry objects
41
- def add_entries(entries)
42
- return unless entries.is_a?(Array)
43
-
44
- @mutex.synchronize do
45
- entries.each { |e| @entries << e if e.is_a?(Entry) }
46
- end
47
- end
48
-
49
40
  # Parses CLI compiler/linter output and adds entries
50
41
  def add_parsed(output, lang: :unknown, source: :compiler) # rubocop:disable Lint/UnusedMethodArgument -- cagiran arayüzüyle uyum için source kwarg'i korunuyor
51
42
  parsed = Parser.parse(output, lang)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ # Root of the unified error taxonomy (issue #448): every module-level
5
+ # error class hangs off Rubik::Error so callers can rescue one class.
6
+ # Standalone-requireable — submodule files that define errors require
7
+ # this file instead of the full lib/rubik.rb entry point.
8
+ class Error < StandardError; end
9
+
10
+ # Shared state-machine violation vocabulary (issue #448): the per-module
11
+ # StateError classes (subagent Lifecycle, DeferredSpec, MCP BootLifecycle,
12
+ # kernel Unit) subclass this, so rescuing Rubik::StateError catches any
13
+ # guarded-transition tripwire without knowing the module.
14
+ class StateError < Error; end
15
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "logger"
4
+ require_relative "log"
4
5
  require_relative "kernel/dispatcher"
5
6
 
6
7
  module Rubik
@@ -10,7 +11,7 @@ module Rubik
10
11
  class EventBus
11
12
  attr_reader :dispatcher
12
13
 
13
- def initialize(dispatcher: nil, logger: Logger.new($stderr))
14
+ def initialize(dispatcher: nil, logger: Rubik::Log.forwarder(:event_bus))
14
15
  @logger = logger
15
16
  @dispatcher = dispatcher || Rubik::Kernel::Dispatcher.new(logger: @logger)
16
17
  end
@@ -29,8 +29,6 @@ module Rubik
29
29
  end
30
30
 
31
31
  def post(event_name, payload = {}, subsystem: :kernel, level: :info, facility_id: nil, **kwargs)
32
- return false if stopped?
33
-
34
32
  eff_payload = if kwargs.empty?
35
33
  payload || {}
36
34
  elsif payload.is_a?(Hash)
@@ -39,12 +37,19 @@ module Rubik
39
37
  kwargs
40
38
  end
41
39
 
42
- @queue.push(
43
- Event.new(
44
- name: event_name.to_sym, payload: eff_payload,
45
- subsystem: subsystem.to_sym, level: level, facility_id: facility_id
40
+ # The stopped check and the queue push must be atomic: an unlocked
41
+ # check-then-push let an event land after the worker consumed
42
+ # STOP_MARKER during shutdown, stranding it forever.
43
+ @lock.synchronize do
44
+ return false if @stopped
45
+
46
+ @queue.push(
47
+ Event.new(
48
+ name: event_name.to_sym, payload: eff_payload,
49
+ subsystem: subsystem.to_sym, level: level, facility_id: facility_id
50
+ )
46
51
  )
47
- )
52
+ end
48
53
 
49
54
  q_size = @queue.size
50
55
  if q_size >= DEFAULT_WARN_QUEUE_DEPTH && (q_size % 500).zero?
@@ -1,13 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "monitor"
4
+ require_relative "../errors"
4
5
  require_relative "../events"
5
6
 
6
7
  module Rubik
7
8
  module Kernel
8
9
  # Kernel execution unit representing an asynchronous computation (Plan 104).
9
10
  class Unit
10
- class StateError < StandardError; end
11
+ # Unified taxonomy (issue #448): subclasses Rubik::StateError so a
12
+ # caller can rescue any guarded-transition tripwire with one class.
13
+ class StateError < Rubik::StateError; end
11
14
 
12
15
  VALID_TYPES = [:subagent, :process, :task, :service].freeze
13
16
  VALID_STATES = [:pending, :active, :paused_error, :waiting_budget, :budget_exhausted, :completed, :failed, :reaped].freeze
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "openai_compatible"
4
+ require_relative "../security/secret_store"
5
+ require_relative "../storage/layout"
4
6
  require_relative "ollama"
5
7
  require_relative "openai"
6
8
  require_relative "anthropic"
@@ -87,7 +89,7 @@ module Rubik
87
89
  )
88
90
  end
89
91
 
90
- custom_providers = config.fetch("providers", {})
92
+ custom_providers = Rubik::Storage::Layout.config_get(config, "providers", {})
91
93
  return unless custom_providers.is_a?(Hash)
92
94
 
93
95
  custom_providers.each do |p_name, p_spec|
@@ -131,13 +133,7 @@ module Rubik
131
133
  def configured_default_max_output_tokens
132
134
  return nil unless @config
133
135
 
134
- val = if @config.respond_to?(:fetch)
135
- begin
136
- @config.fetch("llm.default_max_output_tokens", nil)
137
- rescue KeyError, TypeError, NoMethodError
138
- nil
139
- end
140
- end
136
+ val = Rubik::Storage::Layout.config_get(@config, "llm.default_max_output_tokens", nil)
141
137
  val = @config.dig("llm", "default_max_output_tokens") || @config["llm.default_max_output_tokens"] if val.nil? && @config.is_a?(Hash)
142
138
  val&.to_i if val.to_i.positive?
143
139
  end
@@ -265,7 +261,14 @@ module Rubik
265
261
  def build_provider_instance(p_name, p_spec, headers, default_model, endpoint)
266
262
  type = p_spec["type"].to_s.downcase
267
263
  api_key_env = p_spec["api_key_env"]
268
- api_key = p_spec["api_key"]
264
+ # Config holds at most a "rubik-secret:" pointer at this point
265
+ # (SecretPolicy routed the plaintext into SecretStore on write);
266
+ # resolve it lazily per build so keyring updates apply on refresh.
267
+ api_key = Rubik::Security::SecretStore.resolve(p_spec["api_key"])
268
+ if api_key.nil? && Rubik::Security::SecretStore.reference?(p_spec["api_key"].to_s)
269
+ warn "[WARN] stored secret '#{p_spec['api_key'].delete_prefix(Rubik::Security::SecretStore::REFERENCE_PREFIX)}' " \
270
+ "could not be retrieved via #{Rubik::Security::SecretStore.backend_name}"
271
+ end
269
272
 
270
273
  case type
271
274
  when "openai_compatible", "openai"