rubik 0.9.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +203 -1
  4. data/lib/rubik/adapter/base.rb +2 -1
  5. data/lib/rubik/adapter/process.rb +28 -67
  6. data/lib/rubik/ast/extractor.rb +2 -1
  7. data/lib/rubik/ast/treesitter_extractor.rb +2 -1
  8. data/lib/rubik/benchmark/vakanuvis_adoption.rb +12 -4
  9. data/lib/rubik/buffer/apply_contract.rb +3 -3
  10. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  11. data/lib/rubik/buffer/manager.rb +26 -120
  12. data/lib/rubik/config/deep_ops.rb +4 -1
  13. data/lib/rubik/config/defaults.rb +6 -3
  14. data/lib/rubik/config/schema.rb +8 -0
  15. data/lib/rubik/config/secret_policy.rb +198 -0
  16. data/lib/rubik/config/store.rb +2 -1
  17. data/lib/rubik/config.rb +13 -2
  18. data/lib/rubik/context/ignore_rules.rb +193 -0
  19. data/lib/rubik/context/scanner.rb +46 -161
  20. data/lib/rubik/diagnostics/collector.rb +0 -9
  21. data/lib/rubik/errors.rb +15 -0
  22. data/lib/rubik/event_bus.rb +2 -1
  23. data/lib/rubik/kernel/dispatcher.rb +12 -7
  24. data/lib/rubik/kernel/unit.rb +4 -1
  25. data/lib/rubik/llm/client.rb +6 -200
  26. data/lib/rubik/llm/connection_pool.rb +104 -0
  27. data/lib/rubik/llm/registry.rb +13 -9
  28. data/lib/rubik/llm/router.rb +2 -5
  29. data/lib/rubik/llm/sse_parser.rb +110 -0
  30. data/lib/rubik/log.rb +21 -0
  31. data/lib/rubik/lsp/circuit_breaker.rb +98 -0
  32. data/lib/rubik/lsp/client.rb +77 -273
  33. data/lib/rubik/lsp/client_provisioner.rb +49 -0
  34. data/lib/rubik/lsp/manager.rb +67 -96
  35. data/lib/rubik/lsp/transport.rb +365 -0
  36. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  37. data/lib/rubik/mcp/boot_run.rb +190 -0
  38. data/lib/rubik/mcp/manager.rb +55 -166
  39. data/lib/rubik/mcp/transport/stdio.rb +4 -1
  40. data/lib/rubik/orchestrator/auto_apply.rb +8 -6
  41. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  42. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +12 -1
  43. data/lib/rubik/orchestrator/goal_ledger/schema_vocabulary.rb +38 -0
  44. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +6 -1
  45. data/lib/rubik/orchestrator/goal_ledger.rb +1 -0
  46. data/lib/rubik/orchestrator/goal_session.rb +53 -39
  47. data/lib/rubik/orchestrator/loop.rb +28 -47
  48. data/lib/rubik/orchestrator/lsp_feedback.rb +1 -1
  49. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  50. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  51. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  52. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  53. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  54. data/lib/rubik/orchestrator/step_executor.rb +2 -1
  55. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  56. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  57. data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
  58. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  59. data/lib/rubik/orchestrator/subagent/notices.rb +2 -2
  60. data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
  61. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  62. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  63. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  64. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  65. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  66. data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
  67. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  68. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  69. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  70. data/lib/rubik/platform/io_drain.rb +5 -2
  71. data/lib/rubik/platform/process.rb +144 -54
  72. data/lib/rubik/platform.rb +2 -1
  73. data/lib/rubik/plugin/manager.rb +2 -1
  74. data/lib/rubik/plugin/storage.rb +8 -8
  75. data/lib/rubik/repl/commands/agent_context.rb +8 -7
  76. data/lib/rubik/repl/commands/agent_execution.rb +13 -32
  77. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  78. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  79. data/lib/rubik/repl/commands/base.rb +47 -2
  80. data/lib/rubik/repl/commands/buffer.rb +5 -4
  81. data/lib/rubik/repl/commands/lsp.rb +2 -1
  82. data/lib/rubik/repl/commands/pruner.rb +2 -1
  83. data/lib/rubik/repl/commands/secrets.rb +151 -0
  84. data/lib/rubik/repl/commands/workspace.rb +60 -150
  85. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  86. data/lib/rubik/repl/config_wiring.rb +8 -6
  87. data/lib/rubik/repl/hooks.rb +2 -1
  88. data/lib/rubik/repl/input_execution.rb +162 -79
  89. data/lib/rubik/repl/input_loop.rb +2 -1
  90. data/lib/rubik/repl/lifecycle.rb +60 -210
  91. data/lib/rubik/repl/notice_router.rb +319 -0
  92. data/lib/rubik/repl/paste_support.rb +3 -1
  93. data/lib/rubik/repl/presentation.rb +5 -3
  94. data/lib/rubik/repl/pruner/gate.rb +3 -1
  95. data/lib/rubik/repl/pruner/harness.rb +2 -1
  96. data/lib/rubik/repl/shell.rb +4 -3
  97. data/lib/rubik/repl/tee_io.rb +2 -1
  98. data/lib/rubik/repl/turn_recorder.rb +6 -5
  99. data/lib/rubik/runtime/ast_service.rb +2 -1
  100. data/lib/rubik/runtime/discovery.rb +3 -2
  101. data/lib/rubik/runtime/loop_service.rb +4 -3
  102. data/lib/rubik/runtime/manifest_store.rb +9 -1
  103. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  104. data/lib/rubik/runtime/overlay_storage.rb +109 -163
  105. data/lib/rubik/runtime/task_manager.rb +64 -25
  106. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  107. data/lib/rubik/sandbox/runner.rb +21 -19
  108. data/lib/rubik/security/audit_log.rb +2 -1
  109. data/lib/rubik/security/command_gate.rb +126 -243
  110. data/lib/rubik/security/guard.rb +7 -3
  111. data/lib/rubik/security/secret_store.rb +492 -0
  112. data/lib/rubik/security/segment_report.rb +161 -0
  113. data/lib/rubik/security/synthesis_gate.rb +8 -11
  114. data/lib/rubik/session/manager.rb +7 -1
  115. data/lib/rubik/session/recorder.rb +2 -1
  116. data/lib/rubik/skills/manager.rb +4 -4
  117. data/lib/rubik/storage/search.rb +49 -0
  118. data/lib/rubik/storage/store.rb +13 -20
  119. data/lib/rubik/storage/transcript_indexer.rb +3 -2
  120. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  121. data/lib/rubik/storage/vakanuvis.rb +2 -1
  122. data/lib/rubik/tools/base.rb +22 -0
  123. data/lib/rubik/tools/builtins/files.rb +3 -15
  124. data/lib/rubik/tools/builtins/goal_ledger.rb +12 -1
  125. data/lib/rubik/tools/builtins/grep.rb +1 -6
  126. data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
  127. data/lib/rubik/tools/builtins/synthesis.rb +3 -3
  128. data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
  129. data/lib/rubik/tools/preflight_runner.rb +2 -1
  130. data/lib/rubik/tools/truncator.rb +48 -36
  131. data/lib/rubik/tools/uri_resolver.rb +3 -2
  132. data/lib/rubik/ui/banner.rb +4 -3
  133. data/lib/rubik/ui/capability.rb +1 -0
  134. data/lib/rubik/ui/spinner.rb +3 -1
  135. data/lib/rubik/ui/statusline.rb +6 -5
  136. data/lib/rubik/version.rb +1 -1
  137. data/lib/rubik.rb +4 -5
  138. metadata +20 -1
@@ -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,198 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../log"
4
+ require_relative "../security/secret_store"
5
+
6
+ module Rubik
7
+ class Config
8
+ # Secret-write policy — the single choke point over Config#set/#delete.
9
+ #
10
+ # Two rules, enforced where every write funnels:
11
+ # 1. Route: a plaintext value at a secret-looking path is stored in
12
+ # the OS keyring (SecretStore) and the config file only ever
13
+ # receives the "rubik-secret:<name>" pointer. Works for both
14
+ # direct paths (providers.x.api_key) and whole-provider Hash
15
+ # writes (!provider set/add rewrite providers.<n> as a Hash).
16
+ # 2. Guard: plaintext secrets are refused for the :project scope —
17
+ # project files sit inside a git worktree and leak through
18
+ # commits, copies, and shares. Escape hatch:
19
+ # security.allow_project_secrets (schema-backed boolean, default
20
+ # false). Secrets already stored as references are pointers, not
21
+ # credentials, and may live anywhere.
22
+ #
23
+ # Interface used by callers:
24
+ # - Config includes the instance helpers (guard_secret_write,
25
+ # warn_plaintext_project_secrets, secret_key?).
26
+ # - REPL surfaces use Rubik::Config::SecretPolicy.secret_key? to
27
+ # mask secret values in display output.
28
+ module SecretPolicy
29
+ # Dot-paths that end in an api_key leaf, e.g.
30
+ # "providers.openai.api_key" or a bare "api_key".
31
+ SECRET_LEAF_PATTERN = /\A(?:[\w-]+\.)*api_key\z/
32
+
33
+ module_function
34
+
35
+ def secret_key?(key)
36
+ key.to_s.match?(SECRET_LEAF_PATTERN)
37
+ end
38
+
39
+ # Store-name convention shared by the write guard and `!secrets
40
+ # migrate`: project-scope secrets get a ".project"-suffixed name so a
41
+ # project-scoped credential can never silently overwrite (or be
42
+ # overwritten by) the global one under a shared pointer.
43
+ def secret_name(path, scope)
44
+ scope.to_s.downcase.to_sym == :project ? "#{path}.project" : path.to_s
45
+ end
46
+
47
+ # Instance-side helpers (included into Config, private there).
48
+
49
+ # Returns nil when the write needs no secret handling; otherwise
50
+ # { block: "<error message>" } -> refuse the write outright
51
+ # { info: {...} } -> write proceeds as-is
52
+ # { typed_val:, info: {...} } -> write proceeds with the
53
+ # Hash rewritten to hold
54
+ # references
55
+ # persist follows Config#set's scope vocabulary (nil/"session" keep
56
+ # the value in memory only — in-process plaintext is out of scope).
57
+ def guard_secret_write(key, typed_val, persist)
58
+ return nil unless persisting?(persist)
59
+
60
+ leaves = collect_secret_leaves(key, typed_val)
61
+ raw = leaves.select { |path, val| secret_leaf?(path, val) }
62
+ return nil if raw.empty?
63
+
64
+ scope = persist.to_s.downcase.to_sym == :project ? :project : :global
65
+ # security.allow_project_secrets=true restores the legacy behavior:
66
+ # project writes act like global ones — the store is still
67
+ # preferred, but a store failure degrades to plaintext + warning
68
+ # instead of refusing the write. The override resolves ONLY from
69
+ # operator domains (global layer / env): a project config can never
70
+ # enable it for itself, or the guard would be self-serve bypassable
71
+ # by any repo-shipped .rubik/config.yml.
72
+ legacy_plaintext_ok = project_secrets_override
73
+ return { block: project_secret_error(leaves) } if scope == :project && !legacy_plaintext_ok
74
+
75
+ rewritten = deep_dup(typed_val)
76
+ stored = []
77
+ raw.each do |path, val|
78
+ store_error = nil
79
+ ref = nil
80
+ begin
81
+ ref = Rubik::Security::SecretStore.set(secret_name(path, scope), val)
82
+ rescue Rubik::Security::SecretStore::Error => e
83
+ store_error = e
84
+ end
85
+
86
+ if store_error
87
+ # gh-style degradation: the write proceeds with plaintext plus a
88
+ # loud warning. Project scope never reaches this fallback with
89
+ # the plaintext guard closed — that refusal happens up front,
90
+ # before the store loop, so a store failure here can only mean
91
+ # the override was open.
92
+ warn_secret_store_fallback(store_error, scope)
93
+ return { info: plaintext_fallback_info }
94
+ end
95
+
96
+ # Leaf paths are full config dot-paths; the rewrite target is the
97
+ # typed_val subtree, so strip the write root ("providers.<n>."
98
+ # for whole-Hash writes). A direct leaf write carries no subtree,
99
+ # so the value itself becomes the pointer.
100
+ if rewritten.is_a?(Hash)
101
+ deep_set_key(rewritten, path.delete_prefix("#{key}."), ref)
102
+ else
103
+ rewritten = ref
104
+ end
105
+ stored << path
106
+ end
107
+ { typed_val: rewritten, info: { stored: true, backend: Rubik::Security::SecretStore.backend_name, names: stored } }
108
+ end
109
+
110
+ # Boot-time honesty: surface plaintext keys already sitting in the
111
+ # project layer (legacy configs, hand-edited files).
112
+ def warn_plaintext_project_secrets
113
+ leaves = collect_secret_leaves("", @project_layer.to_h)
114
+ raw = leaves.select { |path, val| secret_leaf?(path, val) }
115
+ return if raw.empty?
116
+
117
+ @warnings << "Project config #{project_config_path} holds plaintext API key(s) at " \
118
+ "#{raw.map(&:first).join(', ')}. They can leak through commits or shared copies — " \
119
+ "run `!secrets migrate`, move them to --global, or set api_key_env instead."
120
+ end
121
+
122
+ # Keyring hygiene for Config#delete: a successfully persisted delete
123
+ # of an api_key path also drops its stored secret(s) — both scopes'
124
+ # names, so neither keyring entry outlives its reference. Best-effort
125
+ # — never masks res.
126
+ def cleanup_stored_secret(key, res, persist)
127
+ return unless secret_key?(key) && res[:ok] && persisting?(persist)
128
+
129
+ Rubik::Security::SecretStore.delete(key.to_s)
130
+ Rubik::Security::SecretStore.delete("#{key}.project")
131
+ rescue Rubik::Security::SecretStore::Error
132
+ nil
133
+ end
134
+
135
+ private
136
+
137
+ # Operator-trust domains only (env layer, then global layer). The
138
+ # merged fetch is deliberately NOT used: the project layer could
139
+ # otherwise re-enable plaintext-in-project from inside the repo.
140
+ def project_secrets_override
141
+ [@env_layer, @global_layer].any? do |layer|
142
+ layer.is_a?(Hash) && layer.dig("security", "allow_project_secrets") == true
143
+ end
144
+ end
145
+
146
+ # Depth-first walk collecting [dot-path, value] pairs for every
147
+ # api_key leaf inside a whole-Hash write (e.g. providers.<n>).
148
+ def collect_secret_leaves(prefix, obj)
149
+ leaves = []
150
+ case obj
151
+ when Hash
152
+ obj.each do |k, v|
153
+ full = prefix.to_s.empty? ? k.to_s : "#{prefix}.#{k}"
154
+ leaves.concat(collect_secret_leaves(full, v))
155
+ end
156
+ when Array
157
+ obj.each_with_index do |v, i|
158
+ leaves.concat(collect_secret_leaves(prefix.empty? ? i.to_s : "#{prefix}.#{i}", v))
159
+ end
160
+ else
161
+ leaves << [prefix.to_s, obj] if secret_key?(prefix)
162
+ end
163
+ leaves
164
+ end
165
+
166
+ # A raw secret at a secret path: a non-empty plaintext String, or a
167
+ # Numeric (parse_scalar_value coerces "12345" to an Integer before the
168
+ # guard runs — a digit-only api_key must not slip past). References
169
+ # and nils are pointers / clears and always allowed.
170
+ def secret_leaf?(path, val)
171
+ return false unless secret_key?(path)
172
+ return false if val.nil? || Rubik::Security::SecretStore.reference?(val)
173
+
174
+ (val.is_a?(String) && !val.empty?) || val.is_a?(Numeric)
175
+ end
176
+
177
+ def project_secret_error(leaves)
178
+ paths = leaves.map(&:first).join(", ")
179
+ "refusing to store plaintext secret(s) at #{paths} in the project config " \
180
+ "(#{project_config_path}): project files live inside the repository and leak through " \
181
+ "commits or shared copies. Use --global, set providers.<name>.api_key_env to read the key " \
182
+ "from the environment, or run `!secrets migrate`. Override only if you accept the risk: " \
183
+ "security.allow_project_secrets = true."
184
+ end
185
+
186
+ def warn_secret_store_fallback(error, scope)
187
+ msg = "OS secret store unavailable (#{error.message}); plaintext kept in #{scope} config"
188
+ @warnings << msg
189
+ Rubik::Log.warn(:config, "[WARN] #{msg}") if ENV["RUBIK_DEBUG"]
190
+ end
191
+
192
+ def plaintext_fallback_info
193
+ { stored: false, backend: Rubik::Security::SecretStore.backend_name,
194
+ warning: "secret store unavailable; plaintext kept in config" }
195
+ end
196
+ end
197
+ end
198
+ end
@@ -3,6 +3,7 @@
3
3
  require "yaml"
4
4
  require "yaml/store"
5
5
  require "fileutils"
6
+ require_relative "../log"
6
7
  require_relative "pruner"
7
8
 
8
9
  module Rubik
@@ -38,7 +39,7 @@ module Rubik
38
39
  begin
39
40
  FileUtils.mkdir_p(File.dirname(path))
40
41
  rescue SystemCallError => e
41
- warn "[WARN] Failed to create config directory #{File.dirname(path)}: #{e.message}" if ENV["RUBIK_DEBUG"]
42
+ Rubik::Log.warn(:config, "[WARN] Failed to create config directory #{File.dirname(path)}: #{e.message}") if ENV["RUBIK_DEBUG"]
42
43
  end
43
44
  YAML::Store.new(path, true)
44
45
  end
data/lib/rubik/config.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "monitor"
4
+ require_relative "log"
4
5
  require_relative "config/defaults"
5
6
  require_relative "config/deep_ops"
6
7
  require_relative "config/store"
@@ -8,6 +9,7 @@ require_relative "config/model_catalog"
8
9
  require_relative "config/pruner"
9
10
  require_relative "config/schema"
10
11
  require_relative "config/env_loader"
12
+ require_relative "config/secret_policy"
11
13
  require_relative "config/scoped"
12
14
 
13
15
  module Rubik
@@ -16,10 +18,10 @@ module Rubik
16
18
  include DeepOps
17
19
  include Store
18
20
  include ModelCatalog
21
+ include SecretPolicy
19
22
 
20
23
  DEFAULT_CONFIG = Defaults::DEFAULT_CONFIG
21
24
  SENSITIVE_PROJECT_CONFIG_KEYS = DeepOps::SENSITIVE_PROJECT_CONFIG_KEYS
22
-
23
25
  attr_reader :data, :warnings, :root_dir, :home_dir,
24
26
  :defaults_layer, :global_layer, :project_layer, :env_layer, :session_layer
25
27
  attr_accessor :event_bus
@@ -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
@@ -245,7 +254,7 @@ module Rubik
245
254
  begin
246
255
  entry[:callback].call(key_str, new_val, old_val)
247
256
  rescue StandardError => e
248
- warn "[WARN] Config listener failed for '#{key_str}': #{e.message}"
257
+ Rubik::Log.warn(:config, "[WARN] Config listener failed for '#{key_str}': #{e.message}")
249
258
  @event_bus&.emit(:config_listener_error, key: key_str, error: e.message)
250
259
  end
251
260
  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