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
@@ -0,0 +1,463 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "fileutils"
5
+ require "open3"
6
+ require "tempfile"
7
+ require "tmpdir"
8
+ require_relative "../platform"
9
+
10
+ module Rubik
11
+ module Security
12
+ # Keeps provider credentials out of plain config files.
13
+ #
14
+ # Interface (class-level, the only surface callers may use):
15
+ # - reference?(value) -> true when value is a stored-secret pointer
16
+ # - reference_for(name) -> the pointer string for a SecretStore.name
17
+ # - resolve(value) -> pointer -> stored value; anything else as-is
18
+ # - set(name, value) -> store secret, return its pointer
19
+ # - fetch(name) -> stored value or nil (a miss never raises)
20
+ # - delete(name) -> idempotent removal
21
+ # - backend_name -> symbol for the active backend
22
+ #
23
+ # A "name" is the config dot-path that owns the secret, e.g.
24
+ # "providers.openai.api_key". Config files never receive the secret
25
+ # itself, only the pointer ("rubik-secret:<name>"), so a config file may
26
+ # be committed, synced, or shared without leaking credentials.
27
+ #
28
+ # Backend chain (first available wins, file fallback always exists):
29
+ # macOS - Keychain generic passwords via the security CLI; the
30
+ # secret travels on stdin (flag-then-stdin mode), never argv
31
+ # Windows - Credential Manager via PowerShell PasswordVault; payloads
32
+ # travel as env vars into FIXED scripts (secret base64)
33
+ # Linux - secret-service D-Bus via secret-tool; secret on stdin
34
+ # always - a ruthlessly simple owner-only 0600 YAML store at
35
+ # ~/.rubik/secrets.yml — plaintext on disk is the honest
36
+ # last resort, surfaced via backend_name
37
+ #
38
+ # Test seam: SecretStore.backend= injects any object responding to
39
+ # read/write/delete, so unit tests never touch a host keyring.
40
+ class SecretStore
41
+ REFERENCE_PREFIX = "rubik-secret:"
42
+ SERVICE = "rubik"
43
+ NAME_PATTERN = /\A\w[\w.-]*\z/
44
+
45
+ class Error < StandardError; end
46
+
47
+ @monitor = Monitor.new
48
+ @backend = nil
49
+
50
+ class << self
51
+ def backend_name
52
+ monitor_sync { backend_class_name(backend) }
53
+ end
54
+
55
+ def available?
56
+ true
57
+ end
58
+
59
+ def reference?(value)
60
+ value.is_a?(String) && value.start_with?(REFERENCE_PREFIX)
61
+ end
62
+
63
+ def reference_for(name)
64
+ "#{REFERENCE_PREFIX}#{validate_name!(name)}"
65
+ end
66
+
67
+ def resolve(value)
68
+ return value unless reference?(value)
69
+
70
+ fetch(value.delete_prefix(REFERENCE_PREFIX))
71
+ rescue Error
72
+ # A hand-edited pointer (empty name, hostile characters) must not
73
+ # crash provider builds or REPL boot — an unresolvable reference
74
+ # resolves to nil, and Registry warns loudly instead.
75
+ nil
76
+ end
77
+
78
+ # Stores the secret and returns its reference. Raises Error when the
79
+ # active OS backend refuses; Config decides the fallback policy.
80
+ def set(name, value)
81
+ validate_name!(name)
82
+ value = value.to_s unless value.is_a?(String)
83
+ raise Error, "secret value must be a non-empty string" unless value.is_a?(String) && !value.empty?
84
+
85
+ backend.write(name, value)
86
+ reference_for(name)
87
+ end
88
+
89
+ def fetch(name)
90
+ validate_name!(name)
91
+ backend.read(name)
92
+ end
93
+
94
+ def delete(name)
95
+ validate_name!(name)
96
+ backend.delete(name)
97
+ true
98
+ end
99
+
100
+ # Test seam: inject a backend object responding to read/write/delete.
101
+ def backend=(backend_obj)
102
+ monitor_sync { @backend = backend_obj }
103
+ end
104
+
105
+ def reset!
106
+ monitor_sync { @backend = nil }
107
+ end
108
+
109
+ private
110
+
111
+ def backend
112
+ monitor_sync { @backend ||= select_backend }
113
+ end
114
+
115
+ def monitor_sync(&)
116
+ @monitor.synchronize(&)
117
+ end
118
+
119
+ def validate_name!(name)
120
+ name_str = name.to_s
121
+ return name_str if name_str.match?(NAME_PATTERN)
122
+
123
+ raise Error, "invalid secret name: #{name_str.inspect} (expected a dot-path like providers.openai.api_key)"
124
+ end
125
+ end
126
+
127
+ # Shared command plumbing. Backends are the internal seam; tests may
128
+ # subclass and override #run to avoid touching a host keyring.
129
+ class CommandBackend
130
+ private
131
+
132
+ # Single spawn point for every backend: receives a fully-formed
133
+ # argument vector (never a shell string) plus optional stdin payload.
134
+ def run(argv, input: nil, env: nil)
135
+ env_hash = env.is_a?(Hash) ? env : {}
136
+ merged = ENV.to_h.merge(env_hash)
137
+ Open3.popen3(merged, *argv) do |pin, pout, perr, waiter|
138
+ pin.write(input.to_s)
139
+ pin.close
140
+ [pout.read, perr.read, waiter.value]
141
+ end
142
+ end
143
+ end
144
+
145
+ class MacosKeychainBackend < CommandBackend
146
+ SECURITY = "/usr/bin/security"
147
+
148
+ def self.available?
149
+ Rubik::Platform.macos? && File.executable?(SECURITY)
150
+ end
151
+
152
+ def write(name, value)
153
+ out, err, st = run(security_write_argv(name), input: value)
154
+ return true if st.success?
155
+
156
+ raise Error, "keychain write failed (#{st.exitstatus}): #{err} #{out}"
157
+ end
158
+
159
+ def read(name)
160
+ out, _err, st = run(security_read_argv(name))
161
+ # 44 = errSecItemNotFound (honest miss); any other nonzero exit is
162
+ # a denied/failed lookup and must not surface as an empty key.
163
+ return nil unless st.success? || st.exitstatus == 44
164
+
165
+ val = out.strip
166
+ val.empty? ? nil : val
167
+ end
168
+
169
+ def delete(name)
170
+ _out, _err, _st = run(security_delete_argv(name))
171
+ true
172
+ end
173
+
174
+ private
175
+
176
+ # The security CLI owns this contract: "-w" with no value asks the
177
+ # tool to read the password from its standard input, so the secret
178
+ # never rides argv or `ps` output.
179
+ def security_write_argv(name)
180
+ [SECURITY, "add-generic-password", "-U", "-s", SERVICE, "-a", name, "-w"]
181
+ end
182
+
183
+ def security_read_argv(name)
184
+ [SECURITY, "find-generic-password", "-s", SERVICE, "-a", name, "-w"]
185
+ end
186
+
187
+ def security_delete_argv(name)
188
+ [SECURITY, "delete-generic-password", "-s", SERVICE, "-a", name]
189
+ end
190
+ end
191
+
192
+ class WindowsCredManBackend < CommandBackend
193
+ PS = "powershell.exe"
194
+
195
+ def self.available?
196
+ Rubik::Platform.windows? && WindowsCredManBackend.find_in_path(PS)
197
+ end
198
+
199
+ def self.find_in_path(binary)
200
+ ENV["PATH"].to_s.split(File::PATH_SEPARATOR).any? do |dir|
201
+ File.executable?(File.join(dir, binary))
202
+ end
203
+ end
204
+
205
+ def write(name, value)
206
+ # The secret travels base64-encoded through the environment into a
207
+ # FIXED script: no caller-controlled text is interpolated into any
208
+ # command string, and the base64 alphabet cannot carry metacharacters.
209
+ payload = {
210
+ "RUBIK_SECRET_NAME" => name,
211
+ "RUBIK_SECRET_VALUE_B64" => [value].pack("m0")
212
+ }
213
+ _out, err, st = run(write_script_argv, env: payload)
214
+ # $ErrorActionPreference = 'Stop' makes the script exit nonzero on
215
+ # any vault failure; ignoring the status would hand back a dangling
216
+ # pointer while reporting success.
217
+ return true if st.success?
218
+
219
+ raise Error, "PasswordVault write failed (#{st.exitstatus}): #{err.to_s.strip}"
220
+ end
221
+
222
+ def read(name)
223
+ out, = run(read_script_argv, env: name_payload(name))
224
+ encoded = out.to_s.strip
225
+ encoded.empty? ? nil : encoded.unpack1("m0").force_encoding(Encoding::UTF_8)
226
+ end
227
+
228
+ def delete(name)
229
+ run(delete_script_argv, env: name_payload(name))
230
+ true
231
+ end
232
+
233
+ # FIXED scripts (frozen at load, no interpolation): WinRT
234
+ # PasswordVault via Windows PowerShell 5.1, present on every
235
+ # supported Windows release. cmdkey cannot read secrets back, so the
236
+ # vault API is the only stdlib-reachable read path.
237
+ WRITE_PS = <<~PS
238
+ $ErrorActionPreference = 'Stop'
239
+ [void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
240
+ $v = New-Object Windows.Security.Credentials.PasswordVault
241
+ $p = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($env:RUBIK_SECRET_VALUE_B64))
242
+ $c = New-Object Windows.Security.Credentials.PasswordCredential('rubik', $env:RUBIK_SECRET_NAME, $p)
243
+ $v.Add($c)
244
+ Write-Output 'ok'
245
+ PS
246
+
247
+ READ_PS = <<~PS
248
+ $ErrorActionPreference = 'Stop'
249
+ [void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
250
+ $v = New-Object Windows.Security.Credentials.PasswordVault
251
+ $c = $v.Retrieve('rubik', $env:RUBIK_SECRET_NAME)
252
+ $c.RetrievePassword()
253
+ Write-Output ([Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($c.Password)))
254
+ PS
255
+
256
+ DELETE_PS = <<~PS
257
+ [void][Windows.Security.Credentials.PasswordVault,Windows.Foundation,ContentType=WindowsRuntime]
258
+ $v = New-Object Windows.Security.Credentials.PasswordVault
259
+ try { $c = $v.Retrieve('rubik', $env:RUBIK_SECRET_NAME); $v.Remove($c) } catch { }
260
+ Write-Output 'ok'
261
+ PS
262
+
263
+ private
264
+
265
+ def name_payload(name)
266
+ { "RUBIK_SECRET_NAME" => name }
267
+ end
268
+
269
+ def write_script_argv
270
+ [PS, "-NoProfile", "-NonInteractive", "-Command", WRITE_PS]
271
+ end
272
+
273
+ def read_script_argv
274
+ [PS, "-NoProfile", "-NonInteractive", "-Command", READ_PS]
275
+ end
276
+
277
+ def delete_script_argv
278
+ [PS, "-NoProfile", "-NonInteractive", "-Command", DELETE_PS]
279
+ end
280
+ end
281
+
282
+ class LinuxSecretToolBackend < CommandBackend
283
+ TOOL = "secret-tool"
284
+
285
+ def self.available?
286
+ return false unless Rubik::Platform.linux? && LinuxSecretToolBackend.find_in_path(TOOL)
287
+
288
+ # Canary probe: headless boxes ship the binary without a running
289
+ # secret service ("Cannot autolaunch D-Bus"), so binary presence
290
+ # alone lies. A missed lookup (exit 1) is a working service; a
291
+ # crashed lookup (other nonzero) is not.
292
+ new.canary_ok?
293
+ end
294
+
295
+ def self.find_in_path(binary)
296
+ ENV["PATH"].to_s.split(File::PATH_SEPARATOR).any? do |dir|
297
+ File.executable?(File.join(dir, binary))
298
+ end
299
+ end
300
+
301
+ def write(name, value)
302
+ _out, err, st = run(store_argv(name), input: value)
303
+ return true if st.success?
304
+
305
+ raise Error, "secret-tool store failed (#{st.exitstatus}): #{err}"
306
+ end
307
+
308
+ def read(name)
309
+ out, = run(lookup_argv(name))
310
+ val = out.to_s.strip
311
+ val.empty? ? nil : val
312
+ end
313
+
314
+ def delete(name)
315
+ run(clear_argv(name))
316
+ true
317
+ end
318
+
319
+ def canary_ok?
320
+ _out, _err, st = run(lookup_argv("rubik-availability-probe"))
321
+ st.exitstatus == 1 || st.success?
322
+ end
323
+
324
+ private
325
+
326
+ # The secret travels on stdin — never argv — per secret-service
327
+ # convention; attrs (service, account) name the lookup key.
328
+ def store_argv(name)
329
+ [TOOL, "store", "--label=rubik", "service", SERVICE, "account", name]
330
+ end
331
+
332
+ def lookup_argv(name)
333
+ [TOOL, "lookup", "service", SERVICE, "account", name]
334
+ end
335
+
336
+ def clear_argv(name)
337
+ [TOOL, "clear", "service", SERVICE, "account", name]
338
+ end
339
+ end
340
+
341
+ # The always-present fallback: owner-only store next to the global
342
+ # config, created 0600 from the first byte (same discipline as
343
+ # Config::Store#ensure_owner_only!), atomic replace on every write.
344
+ class FileBackend
345
+ def initialize(path = default_path)
346
+ @path = path
347
+ end
348
+
349
+ attr_reader :path
350
+
351
+ def write(name, value)
352
+ ensure_owner_only!
353
+ data = read_all
354
+ data[name] = value
355
+ atomic_replace(data)
356
+ end
357
+
358
+ def read(name)
359
+ read_all[name]
360
+ end
361
+
362
+ def delete(name)
363
+ data = read_all
364
+ data.delete(name)
365
+ atomic_replace(data)
366
+ true
367
+ end
368
+
369
+ private
370
+
371
+ def read_all
372
+ return {} unless File.exist?(@path)
373
+
374
+ parsed = YAML.safe_load_file(@path, permitted_classes: [], aliases: false)
375
+ parsed.is_a?(Hash) ? parsed : {}
376
+ rescue Psych::Exception
377
+ {}
378
+ end
379
+
380
+ def atomic_replace(data)
381
+ dir = File.dirname(@path)
382
+ FileUtils.mkdir_p(dir)
383
+ # Tempfile.new (NOT the block form of Tempfile.create, which
384
+ # unlinks the file when the block exits): the temp file must
385
+ # SURVIVE until the rename, and must be CLOSED before it —
386
+ # Windows refuses to rename an open file.
387
+ tmp = Tempfile.new("rubik-secrets-", dir)
388
+ begin
389
+ tmp.write(YAML.dump(data))
390
+ tmp.flush
391
+ tmp.chmod(0o600)
392
+ tmp.close
393
+ File.rename(tmp.path, @path)
394
+ rescue SystemCallError
395
+ # Windows rename can still refuse an existing destination:
396
+ # fall back to a short delete-then-rename window.
397
+ tmp.close
398
+ FileUtils.rm_f(@path)
399
+ File.rename(tmp.path, @path)
400
+ ensure
401
+ tmp.close
402
+ tmp.unlink
403
+ end
404
+ rescue SystemCallError => e
405
+ raise Error, "secret file store write failed: #{e.message}"
406
+ end
407
+
408
+ def ensure_owner_only!
409
+ FileUtils.touch(@path) unless File.exist?(@path)
410
+ File.chmod(0o600, @path)
411
+ rescue SystemCallError
412
+ nil
413
+ end
414
+
415
+ def default_path
416
+ home = if defined?(Rubik::Platform) && Rubik::Platform.respond_to?(:home_dir)
417
+ begin
418
+ Rubik::Platform.home_dir
419
+ rescue StandardError
420
+ nil
421
+ end
422
+ else
423
+ ENV["USERPROFILE"] || ENV.fetch("HOME", nil)
424
+ end
425
+ # No "." fallback: an owner-only secret store must never land in
426
+ # the working tree just because HOME is unset.
427
+ raise Error, "no home directory available for the secret file store (set HOME/USERPROFILE)" if home.nil? || home.to_s.strip.empty?
428
+
429
+ File.join(home.to_s, ".rubik", "secrets.yml")
430
+ end
431
+ end
432
+
433
+ def self.select_backend
434
+ # Test armor (RUBIK_TEST=1, the repo-wide convention): a test run
435
+ # must NEVER touch the host keyring, even through an un-injected
436
+ # path — an interactive-authorization `security` spawn on a headless
437
+ # CI runner hangs forever. Un-injected tests get the owner-only
438
+ # file store in a temp location instead.
439
+ return FileBackend.new(test_file_backend_path) if ENV["RUBIK_TEST"] == "1"
440
+
441
+ [MacosKeychainBackend, WindowsCredManBackend, LinuxSecretToolBackend].each do |klass|
442
+ return klass.new if klass.available?
443
+ end
444
+ FileBackend.new
445
+ end
446
+
447
+ # Process-unique path so parallel test workers cannot observe each
448
+ # other's writes; stays under the OS tempdir, never the worktree.
449
+ def self.test_file_backend_path
450
+ File.join(Dir.tmpdir, "rubik-test-secrets-#{Process.pid}.yml")
451
+ end
452
+
453
+ def self.backend_class_name(backend_obj)
454
+ case backend_obj
455
+ when MacosKeychainBackend then :macos_keychain
456
+ when WindowsCredManBackend then :windows_credman
457
+ when LinuxSecretToolBackend then :linux_secret_service
458
+ else :file
459
+ end
460
+ end
461
+ end
462
+ end
463
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../log"
3
4
  require_relative "errors"
4
5
 
5
6
  module Rubik
@@ -181,21 +182,17 @@ module Rubik
181
182
  # spawn-boundary re-check). A guard lacking verify_synthesis_safety!
182
183
  # is downgraded to the shell-only gate — the duck-typed fallback is a
183
184
  # test-double affordance, but skipping the entire AST deny surface is
184
- # security-observable, so the downgrade is loud. $stderr.puts, not
185
- # Kernel#warn: warn is silenced when $VERBOSE is nil (bundler/rake
186
- # -W0) — same ruling as Log.emit. A failing emit is fail-closed AND
187
- # honest: refusal with the real cause, never a misattributed
188
- # "dangerous operations".
185
+ # security-observable, so the downgrade is loud. Routed through
186
+ # Rubik::Log (:security, issue #452): Log.emit uses $stderr.puts, not
187
+ # Kernel#warn, so the line survives $VERBOSE=nil (bundler/rake -W0),
188
+ # and Log's error isolation absorbs a broken emit target — the gate
189
+ # then proceeds through verify_command_safety!, whose own failures
190
+ # still fail closed.
189
191
  def self.run_synthesis_gate(guard, code)
190
192
  if guard.respond_to?(:verify_synthesis_safety!)
191
193
  guard.verify_synthesis_safety!(code)
192
194
  else
193
- begin
194
- $stderr.puts "[Synthesize] guard lacks verify_synthesis_safety!; downgraded to shell-only gate" # rubocop:disable Style/StderrPuts
195
- rescue StandardError => e
196
- raise BlockedWriteError,
197
- "guard downgrade warn could not be written to stderr (#{e.class}: #{e.message}); refusing to eval"
198
- end
195
+ Rubik::Log.warn(:security, "[Synthesize] guard lacks verify_synthesis_safety!; downgraded to shell-only gate")
199
196
  guard.verify_command_safety!(code)
200
197
  end
201
198
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "English"
3
4
  require "fileutils"
4
5
  require "json"
6
+ require_relative "../log"
5
7
  require_relative "replayer"
6
8
  require_relative "../llm/message_sanitizer"
7
9
  require_relative "../prompt/context_compactor"
@@ -160,7 +162,11 @@ module Rubik
160
162
 
161
163
  begin
162
164
  event = JSON.parse(line)
163
- rescue JSON::ParserError
165
+ rescue JSON::ParserError => e
166
+ # A corrupt line is dropped from the replayed history — say so
167
+ # instead of losing the row in silence (persistence-path
168
+ # honesty; partial writes after a crash are the normal cause).
169
+ Rubik::Log.warn(:session, "session restore: dropping unparseable line #{$INPUT_LINE_NUMBER} from #{session_file} (#{e.class}: #{e.message.to_s.chars.first(80).join})")
164
170
  next
165
171
  end
166
172
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "monitor"
4
4
  require_relative "frontmatter"
5
+ require_relative "../storage/layout"
5
6
  require_relative "storage"
6
7
  require_relative "registry"
7
8
 
@@ -208,9 +209,7 @@ module Rubik
208
209
  # fetch-with-default without the `|| default` idiom: an explicit
209
210
  # config value of false/nil must survive (see Loop#safe_config hazard).
210
211
  def fetch(key, default)
211
- return default unless @config.respond_to?(:fetch)
212
-
213
- @config.fetch(key, default)
212
+ Rubik::Storage::Layout.config_get(@config, key, default)
214
213
  end
215
214
 
216
215
  # Encoding hygiene for File.read output reaching the model/history
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "folding"
4
+
5
+ module Rubik
6
+ module Storage
7
+ # The single home of the search scoring pipeline's pure text layer
8
+ # (issue #473): tokenization, haystack construction, per-record folded
9
+ # fields and the token-AND candidate gate. Every function here is pure
10
+ # over its inputs — no I/O, no cache, no Store state — so the scoring
11
+ # path is unit-testable without the filesystem. Store keeps persistence,
12
+ # the shared @records_cache memoization and corpus assembly; Ranking
13
+ # owns the BM25-lite math; Search owns everything that feeds it.
14
+ module Search
15
+ module_function
16
+
17
+ # Sorgu katlaması + tokenizasyonu (Folding.fold — TR I-sınıfı dahil
18
+ # tek katlama kuralı, folding.rb). Token-AND semantiğinin girdisi:
19
+ # her boşlukla ayrılmış terim, kaydın samanlığında geçmeli. Tek
20
+ # substring araması "ratelimit-lock regression 11/11" gibi doğal çok
21
+ # kelimeli sorguları 0 sonuçla bıraktığı için terimlere bölünür
22
+ # (js-tangram olay raporu 2026-10-04, olay #2); tek terimli sorgular
23
+ # eski davranışın birebir aynısı.
24
+ def tokens(query)
25
+ Folding.fold(query).split
26
+ end
27
+
28
+ # Arama samanlığı: title+body+tags, tek katlama. Kayıt kümesi
29
+ # paylaşımlı/frozen olduğundan sonuç da mutasyona kapalıdır
30
+ # (include?/scan/bytesize mutasyon yapmaz).
31
+ def haystack_of(record)
32
+ Folding.fold("#{record['title']}\n#{record['body']}\n#{Array(record['tags']).join("\n")}")
33
+ end
34
+
35
+ # Sıralama katmanı için kayıt başına katlanmış [başlık, etiket] çifti
36
+ # (Ranking#field_tier_bonus'un alan kademeli bonus girdisi).
37
+ def folded_fields_of(record)
38
+ [Folding.fold(record["title"]), Folding.fold(Array(record["tags"]).join("\n"))]
39
+ end
40
+
41
+ # Token-AND aday üretimi: her terim kaydın samanlığında geçmeli.
42
+ # Kayıt ve samanlık dizileri aynı sırada (each_index hizalı) olmalıdır;
43
+ # eşleşen kayıtların indekslerini döndürür.
44
+ def matching_indices(records, haystacks, tokens)
45
+ records.each_index.select { |i| tokens.all? { |token| haystacks[i].include?(token) } }
46
+ end
47
+ end
48
+ end
49
+ end
@@ -10,6 +10,7 @@ require_relative "../uris"
10
10
  require_relative "layout"
11
11
  require_relative "folding"
12
12
  require_relative "ranking"
13
+ require_relative "search"
13
14
 
14
15
  module Rubik
15
16
  module Storage
@@ -129,16 +130,16 @@ module Rubik
129
130
  # kelimeli sorgular kelimesi kelimesine eşleşmediği için 0 sonuç
130
131
  # dönüyordu (js-tangram olay raporu 2026-10-04, olay #2 — "indeks
131
132
  # bayatlığı" değil, eşleştirme semantiğiydi). Tek terimli sorgular
132
- # eski davranışın birebir aynısı.
133
- tokens = Folding.fold(query).split
133
+ # eski davranışın birebir aynısı. Tokenizasyon ve AND eşiği
134
+ # Search'te (issue #473) — burada yalnız corpus montajı.
135
+ tokens = Search.tokens(query)
134
136
  return [] if tokens.empty?
135
137
 
136
138
  @monitor.synchronize do
137
139
  matched = resolve_types(type).flat_map do |t|
138
140
  records = load_records(t)
139
141
  haystacks = haystacks_for(t, records)
140
- records.each_index.select { |i| tokens.all? { |token| haystacks[i].include?(token) } }
141
- .map { |i| records[i] }
142
+ Search.matching_indices(records, haystacks, tokens).map { |i| records[i] }
142
143
  end
143
144
  matched.sort_by { |r| [r["type"].to_s, r["row"].to_i] }
144
145
  end
@@ -159,7 +160,7 @@ module Rubik
159
160
  # (herhangi bir terimi taşıyan kayıt) — fallback recall içindir;
160
161
  # skorlama iki ducte de aynıdır, default davranış birebir korunur.
161
162
  def search_ranked(query:, type: nil, require_all: true)
162
- tokens = Folding.fold(query).split
163
+ tokens = Search.tokens(query)
163
164
  return [] if tokens.empty?
164
165
 
165
166
  @monitor.synchronize do
@@ -358,31 +359,23 @@ module Rubik
358
359
  # tek katlama kuralı, folding.rb): parse cache girdisinde tembel
359
360
  # memoize edilir — sorgu başına 1 MB'lık dizgi kurulumu tekrarlamaz.
360
361
  # Kayıt kümesi paylaşımlı olduğundan samanlıklar da dondurulmuş
361
- # paylaşılır (include?/scan/bytesize mutasyon yapmaz).
362
+ # paylaşılır (include?/scan/bytesize mutasyon yapmaz). Samanlık
363
+ # kurulumu Search'te (issue #473) — burada yalnız sorgu başına
364
+ # memoize disiplini.
362
365
  def haystacks_for(type, records)
363
366
  entry = @records_cache[type]
364
- return records.map { |r| haystack_of(r) } unless entry
367
+ return records.map { |r| Search.haystack_of(r) } unless entry
365
368
 
366
- entry[:haystacks] ||= records.map { |r| haystack_of(r) }.freeze
369
+ entry[:haystacks] ||= records.map { |r| Search.haystack_of(r) }.freeze
367
370
  end
368
371
 
369
372
  # PERF: Memoize folded title and tags per record in @records_cache
370
373
  # to avoid re-folding and array joining on every search_ranked query.
371
374
  def folded_fields_for(type, records)
372
375
  entry = @records_cache[type]
373
- unless entry
374
- return records.map do |r|
375
- [Folding.fold(r["title"]), Folding.fold(Array(r["tags"]).join("\n"))]
376
- end
377
- end
378
-
379
- entry[:folded_fields] ||= records.map do |r|
380
- [Folding.fold(r["title"]), Folding.fold(Array(r["tags"]).join("\n"))].freeze
381
- end.freeze
382
- end
376
+ return records.map { |r| Search.folded_fields_of(r) } unless entry
383
377
 
384
- def haystack_of(record)
385
- Folding.fold("#{record['title']}\n#{record['body']}\n#{Array(record['tags']).join("\n")}")
378
+ entry[:folded_fields] ||= records.map { |r| Search.folded_fields_of(r).freeze }.freeze
386
379
  end
387
380
 
388
381
  def resolve_types(type)
@@ -3,6 +3,7 @@
3
3
  require "monitor"
4
4
  require_relative "../../concurrency"
5
5
  require_relative "../../events"
6
+ require_relative "../layout"
6
7
 
7
8
  module Rubik
8
9
  module Storage
@@ -103,8 +104,6 @@ module Rubik
103
104
  Recall.recall(@store, query, types: types)
104
105
  end
105
106
 
106
- def auto_digest? = @auto_digest
107
-
108
107
  def touched_topics = @monitor.synchronize { @touched_topics.to_a }
109
108
 
110
109
  # record tool thread'inden, sweep worker'dan koşar — get-then-put
@@ -355,7 +354,7 @@ module Rubik
355
354
  def event_key(payload) = "subagent.#{payload[:storage_id].to_s.empty? ? payload[:id] : payload[:storage_id]}.#{payload[:state]}"
356
355
 
357
356
  def fetch_cfg(key, default)
358
- @config.respond_to?(:fetch) ? @config.fetch(key, default) : default
357
+ Rubik::Storage::Layout.config_get(@config, key, default)
359
358
  end
360
359
  end
361
360
  end