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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4978b3f5f55209ac3558f5d2a82651b0e9f2d56571ebb33cb65a8b488f2689b0
4
- data.tar.gz: 4b430b94ecdc22c7f5b75756f9bd23f0eadf544a0ecf760c8fa5a8c14c99bcc0
3
+ metadata.gz: 228307ab0a21e7b48cf596b995a15c3e5d046dee3cbade03df25663c16b857fd
4
+ data.tar.gz: 5a2d93700eb1c739680875952a0389004ae53493241fa08ac944840a9a46939d
5
5
  SHA512:
6
- metadata.gz: acf9faccdcfdb6a344bfc3e1aa8b7f3c316d42a14b6c65766bf2d9b9c728378a312022c7b4195542abeb62fd7319af72d17cd207ec3b69435cbaa8d807bac94b
7
- data.tar.gz: 88b8c7bd853a932860ad009a06e9f3e1b505e72049d4b09ac911f07fa0ecdaf6320b145dd1f67e2b48488c02fff4dae2b3562806e1b0601bf2e59c3c74233924
6
+ metadata.gz: 3b38148ccb7f93eaf442838fdf82540b5d6899e04843a1d278974dbac9898ee15038cc3f3f39241e708c93a4b37b0c158ae5a2121a1dddfa186da25d5b96222e
7
+ data.tar.gz: 24430386dbb423bc389445bcafe847a6beef46ab9e9829cca0c46b497c69452e86c44a20c1a1047d5a55c7382e0295be503fdc01ca86569186ad11d82d484462
data/ATTRIBUTION.md CHANGED
@@ -121,7 +121,7 @@ SOFTWARE.
121
121
  | **`strings-ansi`** | Piotr Murach | MIT | ANSI-aware string truncation and alignment for REPL formatters. |
122
122
  | **`reline`** | Hiroshi SHIBATA & Ruby Team | Ruby License / BSD-2-Clause | Line-editing, history navigation, autocompletion hooks, and headless terminal adaptation. |
123
123
  | **`prism`** | Kevin Newton & Ruby Core Team | MIT | High-performance Ruby parsing and AST extraction ([`Rubik::AST::RubyExtractor`](lib/rubik/ast/ruby_extractor.rb)). |
124
- | **`minitest`** | Ryan Davis & seattlerb | MIT | Fast, modular test suite powering Rubik's 4,400+ unit, integration, and contract tests. |
124
+ | **`minitest`** | Ryan Davis & seattlerb | MIT | Fast, modular test suite powering Rubik's 4,500+ unit, integration, and contract tests. |
125
125
  | **`rubocop`** | Bozhidar Batsov & contributors | MIT | Static code analysis, style enforcement, and architectural hygiene gates. |
126
126
  | **`benchmark`, `logger`, `pstore`, `reline`, `fiddle`** | Ruby core / rubygems teams | Ruby License / BSD-2-Clause | Ruby stdlib gems pinned explicitly in `rubik.gemspec` as they are unbundled from the default set in Ruby 3.4/3.5. |
127
127
 
data/CHANGELOG.md CHANGED
@@ -6,6 +6,147 @@ commits, 2026-08-23 → 2026-10-09, see `git log`).
6
6
 
7
7
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
8
8
 
9
+ ## [0.10.0] - 2026-11-15
10
+
11
+ The secret-store minor: provider credentials leave the config files for the
12
+ OS secret store (Keychain, Credential Manager, secret-service) behind
13
+ `rubik-secret:` pointers with a project-scope plaintext guard and a `!secrets`
14
+ command, abandoned tool workers get an opt-in grace-then-kill reaper — while
15
+ the extraction wave continues its sweep across the config dialects, the buffer
16
+ apply transaction, the storage search pipeline, the LSP JSON-RPC transport,
17
+ the MCP boot choreography, and the REPL lifecycle, and a robustness round
18
+ closes silent failures and unbounded pipe writes.
19
+
20
+ ### Added
21
+
22
+ - **Secret store for provider credentials (#493):** plaintext
23
+ `providers.<name>.api_key` values are no longer written to config files.
24
+ Writes are routed to the OS secret store (macOS Keychain via `security`,
25
+ Windows Credential Manager via PowerShell PasswordVault, Linux secret-service
26
+ via `secret-tool`) and the config file keeps only a `rubik-secret:` pointer
27
+ that resolves lazily at provider build time; an owner-only (0600)
28
+ `~/.rubik/secrets.yml` file store is the honest fallback when no OS keyring
29
+ is available. The `!secrets` command (`backend | list | has | delete |
30
+ migrate`) reports statuses without printing values and migrates legacy
31
+ plaintext keys in place.
32
+ - **`tools.timeout_kill_after` grace-then-kill reaper (#458):** an abandoned
33
+ tool-call worker (its join budget expired, the result discarded) can now be
34
+ reaped: when the knob is set, the worker is registered with a reaper thread
35
+ and `Thread#kill`ed after the grace period, with the kill logged under
36
+ `:tools`; workers that finish during grace are left to the existing
37
+ late-completion log. Default nil = OFF, byte-identical to the current
38
+ pure-abandonment behavior; invalid values warn and stay off.
39
+
40
+ ### Changed
41
+
42
+ - **Nil-safe config-fetch dialects converge on `Layout.config_get`
43
+ (#438, #489, #490, #491):** three hand-rolled dialects (respond_to? ternaries,
44
+ private fetch_cfg/safe_config copies) collapse onto the existing single home,
45
+ so a dotted knob resolves identically whichever file reads it; the two
46
+ deliberate nil/false-preserving fetches stay untouched per their ruling
47
+ comments.
48
+ - **Project-scoped secrets refused by default (#493):** `!config set
49
+ providers.x.api_key … --project` and project-scoped provider writes holding
50
+ plaintext keys fail with guidance (`--global`, `api_key_env`, or
51
+ `!secrets migrate`) — project config files live inside the repository and
52
+ leak through commits. The schema-backed `security.allow_project_secrets`
53
+ flag restores the legacy behavior, resolved from operator trust only (a
54
+ project config cannot enable it for itself). Project-scope store names carry
55
+ a `.project` suffix so the two scopes can never swap each other's credential;
56
+ `!config get/list`, the `!config set` echo, and `!provider set` no longer
57
+ display secret values; untrusted-workspace filtering now also drops the
58
+ `providers` section.
59
+ - **Truncator kept-window accounting single-homed (#439):** the four copies
60
+ of the six-step byte-budget cut + scrub + omitted-accounting block collapse
61
+ behind `clamp_head`/`clamp_tail`/`window_result` with the two sanctioned
62
+ notice spellings pinned byte-identical.
63
+ - **Traversal and file-not-found disclosures single-homed on BaseTool
64
+ (#440):** `traversal_blocked_message` and `file_not_found_message` own the
65
+ allowed-roots construction and the twin fuzzy-matching blocks that
66
+ list_files/grep_search/read_file/replace_file_content each hand-copied.
67
+ - **Adapter-command execution helpers single-homed on Commands::Base (#441):**
68
+ the pre-execution auto-apply choreography, the adapter fallback selection,
69
+ and the failure-diagnostic rendering that !test/!lint/!format/!sh each
70
+ carried move behind `pre_execution_apply`/`adapter_for`/`render_diagnostic`
71
+ with the pinned message wording byte-identical.
72
+ - **Guard/trust plane split out of OverlayStorage (#465):** path containment
73
+ and trust/bootstrap screening move verbatim into `Runtime::OverlayGuard`
74
+ (127 LOC) with the injected-guard kwarg seam preserved and the Kind adapter
75
+ merged into the single class body; OverlayStorage drops 623 → 558 LOC.
76
+ - **REPL lifecycle's notice routing extracted into NoticeRouter (#469, #485):**
77
+ the MCP boot notices, subagent event routing, and reactive-wakeup half of
78
+ lifecycle.rb move into `REPL::NoticeRouter` with the queue/mutex/token state
79
+ owned there and the safe-notice-print fallback single-homed; the Shell
80
+ public surface is unchanged.
81
+ - **LSP JSON-RPC transport extracted from Client (#470, #484):** process
82
+ spawn/teardown, the framing reader thread, request/response correlation, and
83
+ the sanitized spawn environment move into `LSP::Transport` (~700-LOC Client
84
+ split), with `shutdown` the exact LIFO inverse of spawn; the typed LSP
85
+ protocol API stays on Client.
86
+ - **Buffer apply transaction extracted (#472, #483):** the two-phase apply
87
+ (atomic writes, external-conflict detection, TOCTOU containment re-check,
88
+ structured `ApplyFailedError`) moves into `Buffer::ApplyTransaction`; the
89
+ Manager public API and last_* recording contracts are unchanged.
90
+ - **Storage search pipeline's pure text layer extracted (#473, #482):**
91
+ `Storage::Search` owns tokenization, haystack construction, and the
92
+ token-AND candidate gate; `Store#search`/`#search_ranked` delegate with
93
+ identical scoring, ordering, and tie-breaking.
94
+ - **WorkspaceApproval value object extracted (#474, #487)** and the Loop-side
95
+ goal-resume window concentrated into `Orchestrator::ResumeWindow` (#475,
96
+ #486) — ctx.extend lifecycle, the cross-process history prefix, and the
97
+ goal_resumed marker discipline become testable without running a turn.
98
+ - **MCP boot choreography threaded through a BootRun object (#460):** entries /
99
+ connected / outcomes / boot_epoch stop travelling as mutable side-channel
100
+ parameters; the absence-owes-stop rule becomes the named `owes_stop?`
101
+ predicate; monitor-hold boundaries and epoch gates are verbatim, manager.rb
102
+ drops 639 → 515 lines.
103
+ - **The dual popen3 + reader-thread implementations single-homed (#461)** and
104
+ `execute_free_text` split into named phases on InputExecution (#462) with
105
+ statement order, messages, and history mutation preserved verbatim.
106
+ - **The four unrelated StateError classes unified under `Rubik::StateError`
107
+ (#463):** one rescue target for state-machine violations while keeping the
108
+ module-qualified names, and six bare `raise "string"` sites on public APIs
109
+ typed under `Rubik::Error` with one small class per module.
110
+ - **Direct `$stderr` writes routed through Rubik::Log (#455, #467):** only the
111
+ two sanctioned sites write $stderr directly; overlay bootstrap refusals and
112
+ the synthesis-gate downgrade warn emit via `Log.warn(:security)`, and the
113
+ constructor-default loggers gain `Rubik::Log.forwarder(subsystem)` so they
114
+ respect `RUBIK_LOG_LEVEL` and the single-line format.
115
+ - **`verify_command_safety!` split into named phases (#457):** the 165-LOC
116
+ orchestrator reads as the AST fast-path condition, the fail-closed allowlist
117
+ valve, the three block sites, and the interactive-approval step; decisions,
118
+ messages, and audit events unchanged.
119
+ - **The shared goal-resume choreography extracted from GoalSession (#459)**,
120
+ white-box test coupling reduced across the worst offenders (#464, #476,
121
+ #488, #492), direct unit coverage added for OverlayGuard, BootRun, and
122
+ Platform::Process (#478) and the input_execution phases (#477, #481), and
123
+ raw-sleep cooldown assertions replaced with bounded polling (#456).
124
+
125
+ ### Fixed
126
+
127
+ - **send_input pipe write bounded and the start registration window closed
128
+ (#466):** a child that stops reading stdin can no longer deadlock kill,
129
+ status, logs, and process shutdown — the write happens outside the manager
130
+ monitor with a deadline-bounded `Platform.write_until` loop, and a failure
131
+ between spawn and task registration now rolls back by killing the child.
132
+ - **Review-sweep robustness gaps closed (#468):** the kernel dispatcher's
133
+ stopped-check + queue-push is atomic under the lock (no more stranded events
134
+ wedging sync_drain), the LSP did_* rescues log instead of silently returning
135
+ false, the MCP stdio child-died diagnostic captures the pid before teardown,
136
+ and kill_tree gives TERM a bounded 200ms grace window before KILL escalates.
137
+ - **popen_capture wall clock bounded by a deadline watchdog (#479, #480):**
138
+ the kill now always lands before the join — a deadline breach kills the
139
+ child tree first and raises `Timeout::Error` only after the popen3 block
140
+ returns, instead of racing the unwind.
141
+ - **Silent-failure health sweep closed (#443):** corrupt JSON lines dropped
142
+ during session restore are logged with their line number, the at_exit sweep
143
+ is error-isolated per manager, a failed `docker rm -f` in the ensure warns
144
+ instead of vanishing, and five grep-verified dead methods are deleted.
145
+ - **Late completion of an abandoned tool worker logged (#442):** the worker's
146
+ ensure records an honest warn — which tool, how long the call took, and
147
+ whether it finished with a raise or a discarded result — closing the
148
+ observability gap while the budget still bounds turn latency.
149
+
9
150
  ## [0.9.1] - 2026-11-14
10
151
 
11
152
  The hardening-and-single-homing patch: overlay commands seal their default
@@ -1529,7 +1670,8 @@ installation section.
1529
1670
  - Context: serialized index mutations, snapshot-outside-lock reconcile,
1530
1671
  streaming-first truncation.
1531
1672
 
1532
- [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.9.1...HEAD
1673
+ [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.10.0...HEAD
1674
+ [0.10.0]: https://github.com/ozgurulukir/rubik/compare/v0.9.1...v0.10.0
1533
1675
  [0.9.1]: https://github.com/ozgurulukir/rubik/compare/v0.9.0...v0.9.1
1534
1676
  [0.9.0]: https://github.com/ozgurulukir/rubik/compare/v0.8.1...v0.9.0
1535
1677
  [0.8.1]: https://github.com/ozgurulukir/rubik/compare/v0.8.0...v0.8.1
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "shellwords"
4
4
  require_relative "process"
5
+ require_relative "../storage/layout"
5
6
  require_relative "../platform"
6
7
  require_relative "../runtime/discovery"
7
8
  require_relative "../runtime/diagnostic_engine"
@@ -62,7 +63,7 @@ module Rubik
62
63
  return unavailable_result(reason: reason)
63
64
  end
64
65
 
65
- eff_timeout = (timeout || (@config.respond_to?(:fetch) ? @config.fetch("test.timeout", 30) : 30) || 30).to_i
66
+ eff_timeout = (timeout || Rubik::Storage::Layout.config_get(@config, "test.timeout", 30) || 30).to_i
66
67
  work_dir = test_working_dir(root_dir, path)
67
68
  res = ProcessRunner.run(cmd, timeout: eff_timeout, chdir: work_dir)
68
69
 
@@ -1,79 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "open3"
4
- require "timeout"
5
- require_relative "../log"
3
+ require_relative "../platform"
6
4
 
7
5
  module Rubik
8
6
  module Adapter
7
+ # Adapter-facing wrapper over the single-homed popen3 + drain core,
8
+ # Rubik::Platform::Process#popen_capture (issue #444). Preserves the
9
+ # legacy result contract exactly: no :pid key, and every swallowed IO
10
+ # error keeps its historical Log.debug(:adapter) trace via the observer
11
+ # below (event names mirror the platform core's swallow points).
9
12
  class ProcessRunner
10
- def self.run(cmd, timeout: 15, chdir: Dir.pwd)
11
- start_time = Time.now
12
- stdout_str = ""
13
- stderr_str = ""
14
- exit_code = 127
15
-
16
- pid = nil
17
- begin
18
- Timeout.timeout(timeout) do
19
- Open3.popen3(cmd, chdir: chdir) do |stdin, stdout, stderr, wait_thr|
20
- pid = wait_thr.pid
21
- begin
22
- stdin.close
23
- rescue StandardError => e
24
- Rubik::Log.debug(:adapter, "stdin close failed during process execution: #{e.class}: #{e.message}")
25
- end
26
- t_out = Thread.new do
27
- stdout.read
28
- rescue StandardError => e
29
- Rubik::Log.debug(:adapter, "stdout read failed during process execution: #{e.class}: #{e.message}")
30
- ""
31
- end
32
- t_err = Thread.new do
33
- stderr.read
34
- rescue StandardError => e
35
- Rubik::Log.debug(:adapter, "stderr read failed during process execution: #{e.class}: #{e.message}")
36
- ""
37
- end
38
- begin
39
- stdout_str = t_out.value.to_s
40
- stderr_str = t_err.value.to_s
41
- status = wait_thr.value
42
- exit_code = status.exitstatus || (status.signaled? ? (128 + (status.termsig || 1)) : 1)
43
- ensure
44
- begin
45
- t_out.kill
46
- rescue StandardError => e
47
- Rubik::Log.debug(:adapter, "t_out kill failed: #{e.class}: #{e.message}")
48
- end
49
- begin
50
- t_err.kill
51
- rescue StandardError => e
52
- Rubik::Log.debug(:adapter, "t_err kill failed: #{e.class}: #{e.message}")
53
- end
54
- end
55
- end
56
- end
57
- rescue Timeout::Error
58
- Rubik::Platform.kill_tree(pid) if pid
59
- stderr_str = "Execution timed out after #{timeout} seconds"
60
- exit_code = 124
61
- rescue Errno::ENOENT => e
62
- stderr_str = "Binary not found: #{e.message}"
63
- exit_code = 127
64
- rescue StandardError => e
65
- stderr_str = "Execution error: #{e.message}"
66
- exit_code = 1
13
+ ON_IO_ERROR = lambda do |event, error|
14
+ case event
15
+ when :stdin_close
16
+ Rubik::Log.debug(:adapter, "stdin close failed during process execution: #{error.class}: #{error.message}")
17
+ when :stdout_read
18
+ Rubik::Log.debug(:adapter, "stdout read failed during process execution: #{error.class}: #{error.message}")
19
+ when :stderr_read
20
+ Rubik::Log.debug(:adapter, "stderr read failed during process execution: #{error.class}: #{error.message}")
21
+ when :stdout_kill
22
+ Rubik::Log.debug(:adapter, "t_out kill failed: #{error.class}: #{error.message}")
23
+ when :stderr_kill
24
+ Rubik::Log.debug(:adapter, "t_err kill failed: #{error.class}: #{error.message}")
67
25
  end
26
+ end
27
+
28
+ def self.run(cmd, timeout: 15, chdir: Dir.pwd)
29
+ result = Rubik::Platform.popen_capture(cmd, timeout: timeout, chdir: chdir, on_io_error: ON_IO_ERROR)
68
30
 
69
- duration = Time.now - start_time
70
31
  {
71
- ok: exit_code.zero?,
72
- exit_code: exit_code,
73
- stdout: stdout_str,
74
- stderr: stderr_str,
75
- output: [stdout_str, stderr_str].reject(&:empty?).join("\n"),
76
- duration: duration.round(4)
32
+ ok: result[:ok],
33
+ exit_code: result[:exit_code],
34
+ stdout: result[:stdout],
35
+ stderr: result[:stderr],
36
+ output: result[:output],
37
+ duration: result[:duration]
77
38
  }
78
39
  end
79
40
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "tmpdir"
4
4
  require "fileutils"
5
+ require_relative "../errors"
5
6
  require_relative "../storage/vakanuvis"
6
7
  require_relative "../tools/builtins/storage"
7
8
  require_relative "../tools/builtins/vakanuvis"
@@ -82,6 +83,12 @@ module Rubik
82
83
  MAX_TURNS = 8
83
84
  REPLAY_PROFILES = ["adopter", "wrong-path", "struggler"].freeze
84
85
 
86
+ # Bench-integrity contract (issue #448): a driver returning a
87
+ # non-Response, or toolbelt errors leaking into the measurement plane,
88
+ # is a broken harness — never model behavior. Typed so callers (and
89
+ # rake bench) can distinguish it from ordinary StandardError noise.
90
+ class BenchError < Rubik::Error; end
91
+
85
92
  module_function
86
93
 
87
94
  # ---- mağaza + araç masası ---------------------------------------------
@@ -198,7 +205,7 @@ module Rubik
198
205
  # dönüş, respond_to? zincirlerine girip sahte "araç
199
206
  # benimsemedi / tur sınırına vurdu" veri noktası üretmesin —
200
207
  # dürüstçe patla.
201
- raise "provider returned #{res.inspect}, expected Rubik::LLM::Response" unless res.is_a?(Rubik::LLM::Response)
208
+ raise BenchError, "provider returned #{res.inspect}, expected Rubik::LLM::Response" unless res.is_a?(Rubik::LLM::Response)
202
209
 
203
210
  tool_calls = res.respond_to?(:tool_calls?) && res.tool_calls? ? res.tool_calls : []
204
211
  if tool_calls.empty?
@@ -315,8 +322,8 @@ module Rubik
315
322
  tool_errors = runs.sum { |r| r[:calls].count { |_n, _a, res| TOOL_ERROR_MARKERS.any? { |m| res.to_s.start_with?(m) } } }
316
323
  if tool_errors.positive?
317
324
  first_bad = runs.flat_map { |r| r[:calls] }.find { |_n, _a, res| TOOL_ERROR_MARKERS.any? { |m| res.to_s.start_with?(m) } }
318
- raise "#{driver.label}: #{tool_errors} tool error(s) — the bench toolbelt itself failed; " \
319
- "these numbers would read as model behavior. first: #{first_bad[2].to_s[0, 160]}"
325
+ raise BenchError, "#{driver.label}: #{tool_errors} tool error(s) — the bench toolbelt itself failed; " \
326
+ "these numbers would read as model behavior. first: #{first_bad[2].to_s[0, 160]}"
320
327
  end
321
328
  harness.shutdown
322
329
  metrics_for(driver.label, runs).tap { |m| io&.puts(" [vakanuvis-adoption] #{m.label}: #{m.summary}") }
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../storage/layout"
4
+
3
5
  module Rubik
4
6
  module Buffer
5
7
  # The one home for the apply CONTRACT vocabulary (issue #146):
@@ -19,9 +21,7 @@ module Rubik
19
21
  # documented default (true).
20
22
  def model_can_apply?(shell_config:, manager_config:)
21
23
  cfg = shell_config || manager_config
22
- return true if cfg.nil? || !cfg.respond_to?(:fetch)
23
-
24
- cfg.fetch("buffer.model_can_apply", true) != false
24
+ Rubik::Storage::Layout.config_get(cfg, "buffer.model_can_apply", true) != false
25
25
  end
26
26
 
27
27
  # Partial-apply report lines. `sync` (when given) receives the applied
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require_relative "path_validator"
5
+ require_relative "../platform"
6
+
7
+ module Rubik
8
+ module Buffer
9
+ # The two-phase apply transaction, extracted from Manager (issue #472):
10
+ # per-file atomic writes with external-conflict detection — NOT a
11
+ # multi-file transaction. On a mid-run failure the partial progress is
12
+ # carried by ApplyFailedError: applied entries ARE on disk, the failed
13
+ # entry stays dirty, and pending entries keep their buffer content, so
14
+ # a retry writes only the remainder. External-modification conflicts
15
+ # are skipped (recorded in the run report) unless policy is "overwrite".
16
+ #
17
+ # The transaction owns the loop and its failure semantics; workspace
18
+ # path policy (containment, disk hashes, external-modification config)
19
+ # is delegated to the `disk_policy` adapter, and the overlay hashes are
20
+ # mutated in place so Manager state and the transaction stay the same
21
+ # objects. This makes conflict/failure paths testable without the full
22
+ # Manager dictionary API.
23
+ class ApplyTransaction
24
+ # A single run's outcome: paths that reached disk, externally-modified
25
+ # paths that were skipped, and entries rejected by the path validator.
26
+ Result = Struct.new(:applied, :conflicts, :skipped)
27
+
28
+ # The mutable overlay state (issue #472): the Manager's live
29
+ # buffer / dirty_files / original_hashes hashes, mutated in place.
30
+ Overlay = Struct.new(:buffer, :dirty_files, :original_hashes)
31
+
32
+ attr_reader :overlay
33
+
34
+ # @param overlay [Overlay] live buffer state, mutated in place
35
+ # @param disk_policy [#root_dir, #containment_root, #disk_hash,
36
+ # #external_modification_policy] workspace path policy adapter
37
+ # @param event_bus [Rubik::EventBus, nil] apply lifecycle events
38
+ def initialize(overlay:, disk_policy:, event_bus: nil)
39
+ @overlay = overlay
40
+ @disk_policy = disk_policy
41
+ @event_bus = event_bus
42
+ end
43
+
44
+ # Runs the apply loop over keys already sanitized by the caller (the
45
+ # path policy is Manager's concern). `on_finish` runs in an ensure so
46
+ # the caller records the run report (conflicts/skipped/applied) even
47
+ # when a mid-loop failure raises — Manager's last_conflicts /
48
+ # last_skipped / last_applied depend on it.
49
+ #
50
+ # @return [Result] applied/conflicts/skipped for a completed run
51
+ # @raise [ApplyFailedError] on the first failed write, with the
52
+ # structured partial-progress report attached
53
+ def run(keys_to_apply, effective_policy, &on_finish)
54
+ applied_paths = []
55
+ conflicts = []
56
+ skipped = []
57
+
58
+ begin
59
+ keys_to_apply.each_with_index do |rel_path, index|
60
+ next unless overlay.dirty_files.key?(rel_path)
61
+
62
+ unless validate_and_clean_garbage(rel_path)
63
+ skipped << { path: rel_path, reason: :path_validator_rejected }
64
+ next
65
+ end
66
+
67
+ if external_conflict?(rel_path) && effective_policy != "overwrite"
68
+ conflicts << rel_path
69
+ @event_bus&.emit(:buffer_apply_conflict, paths: conflicts.dup.freeze, path: rel_path)
70
+ next
71
+ end
72
+
73
+ apply_single_entry(rel_path, index: index, keys_to_apply: keys_to_apply,
74
+ applied_paths: applied_paths, conflicts: conflicts, skipped: skipped)
75
+ end
76
+ ensure
77
+ on_finish.call(applied_paths, conflicts, skipped)
78
+ end
79
+
80
+ Result.new(applied_paths, conflicts, skipped)
81
+ end
82
+
83
+ private
84
+
85
+ def validate_and_clean_garbage(rel_path)
86
+ return true if PathValidator.valid?(rel_path)
87
+
88
+ overlay.dirty_files.delete(rel_path)
89
+ overlay.buffer.delete(rel_path)
90
+ overlay.original_hashes.delete(rel_path)
91
+ @event_bus&.emit(:buffer_garbage_skipped, path: rel_path)
92
+ false
93
+ end
94
+
95
+ def external_conflict?(rel_path)
96
+ baseline = overlay.original_hashes[rel_path]
97
+ current_disk = @disk_policy.disk_hash(rel_path)
98
+
99
+ if baseline
100
+ current_disk != baseline
101
+ else
102
+ !current_disk.nil?
103
+ end
104
+ end
105
+
106
+ def apply_single_entry(rel_path, index:, keys_to_apply:, applied_paths:, conflicts:, skipped:)
107
+ full_path = File.expand_path(rel_path, @disk_policy.root_dir)
108
+
109
+ # TOCTOU close: re-verify containment immediately before the
110
+ # atomic write — a symlink planted between write-time sanitize
111
+ # and apply! must not redirect the write outside the workspace.
112
+ # Fail loud: an untrustworthy path is an attack signal or a
113
+ # mid-flight root misconfiguration, not a content problem; the
114
+ # entry stays dirty for a retry after remediation.
115
+ containment_kind, = @disk_policy.containment_root(full_path)
116
+ unless containment_kind
117
+ @event_bus&.emit(:buffer_containment_blocked, path: rel_path)
118
+ raise apply_failure(
119
+ "Write blocked: '#{rel_path}' resolves outside all allowed roots at apply time " \
120
+ "(possible symlink swap since buffering). Re-verify the workspace and retry.",
121
+ kind: :containment_blocked, applied: applied_paths, failed: rel_path,
122
+ pending: pending_after(keys_to_apply, index), conflicts: conflicts, skipped: skipped
123
+ )
124
+ end
125
+
126
+ content = overlay.buffer[rel_path]
127
+
128
+ begin
129
+ Rubik::Platform.atomic_write(full_path, content)
130
+ rescue StandardError => e
131
+ raise apply_failure(
132
+ "Apply failed for '#{rel_path}': #{e.class}: #{e.message}; " \
133
+ "#{applied_paths.empty? ? 'no file was written' : "already written: #{applied_paths.join(', ')}"}; " \
134
+ "#{pending_after(keys_to_apply, index).size} file(s) still dirty",
135
+ kind: :write_failed, applied: applied_paths, failed: rel_path,
136
+ pending: pending_after(keys_to_apply, index), conflicts: conflicts, skipped: skipped
137
+ ), cause: e
138
+ end
139
+
140
+ overlay.original_hashes[rel_path] = Digest::SHA256.hexdigest(content)
141
+ overlay.dirty_files.delete(rel_path)
142
+ overlay.buffer.delete(rel_path)
143
+ applied_paths << rel_path
144
+ end
145
+
146
+ # Build the structured partial-apply error with frozen report copies —
147
+ # a reader must see the state at raise time, not later mutations.
148
+ def apply_failure(message, kind:, applied:, failed:, pending:, conflicts:, skipped: [])
149
+ ApplyFailedError.new(
150
+ message, kind: kind,
151
+ applied: applied.dup.freeze, failed: failed,
152
+ pending: pending.dup.freeze, conflicts: conflicts.dup.freeze,
153
+ skipped: skipped.dup.freeze
154
+ )
155
+ end
156
+
157
+ # This run's remaining dirty entries after the failed index — exactly
158
+ # what a retry will still have to write (garbage-skipped entries are
159
+ # already out of dirty_files when this runs).
160
+ def pending_after(keys, index)
161
+ keys[(index + 1)..].to_a.select { |k| overlay.dirty_files.key?(k) }
162
+ end
163
+ end
164
+ end
165
+ end