rubik 0.14.0 → 0.15.1

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 (65) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +109 -1
  4. data/lib/rubik/adapter/node.rb +4 -1
  5. data/lib/rubik/ast/fallback_cleaner.rb +6 -1
  6. data/lib/rubik/ast/ruby_extractor.rb +12 -6
  7. data/lib/rubik/buffer/manager.rb +15 -10
  8. data/lib/rubik/buffer/snapshot.rb +103 -35
  9. data/lib/rubik/config/scoped.rb +12 -5
  10. data/lib/rubik/connect/models_dev.rb +4 -2
  11. data/lib/rubik/connect/wizard.rb +9 -1
  12. data/lib/rubik/diagnostics/collector.rb +25 -2
  13. data/lib/rubik/llm/client.rb +19 -0
  14. data/lib/rubik/llm/connection_pool.rb +7 -1
  15. data/lib/rubik/llm/formatter.rb +8 -2
  16. data/lib/rubik/llm/router.rb +8 -0
  17. data/lib/rubik/lsp/client.rb +233 -18
  18. data/lib/rubik/lsp/document_store.rb +6 -0
  19. data/lib/rubik/lsp/manager.rb +14 -0
  20. data/lib/rubik/lsp/transport.rb +17 -0
  21. data/lib/rubik/orchestrator/continuation_manager.rb +54 -2
  22. data/lib/rubik/orchestrator/goal_session.rb +10 -10
  23. data/lib/rubik/orchestrator/goal_wait.rb +15 -9
  24. data/lib/rubik/orchestrator/loop.rb +31 -28
  25. data/lib/rubik/orchestrator/subagent/instance.rb +31 -4
  26. data/lib/rubik/orchestrator/subagent/lifecycle.rb +25 -25
  27. data/lib/rubik/orchestrator/subagent/messaging.rb +5 -1
  28. data/lib/rubik/orchestrator/subagent/narration.rb +2 -1
  29. data/lib/rubik/orchestrator/subagent/notices.rb +6 -2
  30. data/lib/rubik/orchestrator/subagent/questioner.rb +42 -4
  31. data/lib/rubik/orchestrator/subagent/reaper.rb +50 -19
  32. data/lib/rubik/orchestrator/subagent/supervisor.rb +11 -0
  33. data/lib/rubik/orchestrator/tool_call_id.rb +8 -4
  34. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +112 -7
  35. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +15 -2
  36. data/lib/rubik/platform/process.rb +32 -7
  37. data/lib/rubik/platform/sanitize.rb +17 -18
  38. data/lib/rubik/platform.rb +27 -0
  39. data/lib/rubik/plugin/storage.rb +17 -11
  40. data/lib/rubik/prompt/doctrine.rb +3 -2
  41. data/lib/rubik/repl/commands/base.rb +7 -3
  42. data/lib/rubik/repl/commands/buffer.rb +6 -2
  43. data/lib/rubik/repl/commands/session.rb +11 -1
  44. data/lib/rubik/repl/input_execution.rb +3 -3
  45. data/lib/rubik/repl/service_catalog.rb +6 -2
  46. data/lib/rubik/repl/turn_recorder.rb +4 -1
  47. data/lib/rubik/runtime/manifest_store.rb +7 -2
  48. data/lib/rubik/runtime/task_manager.rb +13 -10
  49. data/lib/rubik/sandbox/driver/base.rb +34 -0
  50. data/lib/rubik/sandbox/driver/bubblewrap.rb +2 -0
  51. data/lib/rubik/sandbox/driver/docker.rb +8 -0
  52. data/lib/rubik/sandbox/driver/podman.rb +8 -0
  53. data/lib/rubik/sandbox/driver/process.rb +9 -12
  54. data/lib/rubik/security/path_policy.rb +28 -3
  55. data/lib/rubik/session/recorder.rb +14 -2
  56. data/lib/rubik/storage/store.rb +7 -0
  57. data/lib/rubik/tools/builtins/exec.rb +7 -1
  58. data/lib/rubik/tools/builtins/interactive.rb +5 -2
  59. data/lib/rubik/tools/builtins/lsp.rb +28 -2
  60. data/lib/rubik/tools/builtins/send_message.rb +7 -2
  61. data/lib/rubik/tools/preflight_runner.rb +4 -1
  62. data/lib/rubik/ui/dialog.rb +5 -1
  63. data/lib/rubik/ui/renderer.rb +4 -1
  64. data/lib/rubik/version.rb +1 -1
  65. metadata +1 -1
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../tool_call_id"
4
+ require_relative "../tool_history"
5
+ require_relative "../continuation_manager"
4
6
 
5
7
  module Rubik
6
8
  module Orchestrator
@@ -10,6 +12,14 @@ module Rubik
10
12
  # the channel never holds @monitor and never emits on the bus under
11
13
  # it (the same discipline as the StepRunner methods it replaces).
12
14
  class SubagentChannel < Channel
15
+ # Prefixed to results rebuilt from history after the model went silent.
16
+ SALVAGE_NOTICE = "[Salvaged: the model returned no final report. This is the last assistant " \
17
+ "message from the run; see the transcript for the full work.]"
18
+
19
+ # Output-cap growth bound for truncated turns (issue #610); the
20
+ # operator's own llm.default_max_output_tokens still wins if higher.
21
+ MAX_OUTPUT_TOKENS_CEILING = 16_384
22
+
13
23
  def initialize(instance)
14
24
  super()
15
25
  @inst = instance
@@ -32,10 +42,13 @@ module Rubik
32
42
  end
33
43
 
34
44
  # Subagent policy (ToolCallId doctrine): deterministic
35
- # (subagent id, step, index) — the history lives and dies with the
36
- # run, and greppable ids beat collision-proof ones here.
45
+ # (subagent id, run, step, index). run_seq increments on every
46
+ # start_async — spawn AND redeploy alike — so a redeployed run's
47
+ # ids can never collide with the preserved history it re-sends to
48
+ # the provider (issue #593). Greppable ids still beat
49
+ # collision-proof ones here.
37
50
  def mint_call_id(idx)
38
- ToolCallId.mint(@inst.id, @inst.current_step, idx + 1)
51
+ ToolCallId.mint(@inst.id, @inst.run_seq, @inst.current_step, idx + 1)
39
52
  end
40
53
 
41
54
  # ── response shaping ──
@@ -59,8 +72,37 @@ module Rubik
59
72
  @inst.journal&.record_token_usage(run: @inst.run_seq, usage: usage)
60
73
  end
61
74
 
62
- # Deferred: subagent continuation is future work.
63
- def handle_truncation(_res, _text, _tool_calls, _finish_reason) = nil
75
+ # Stitches the continuation buffer (partial texts from earlier
76
+ # finish_reason=length cycles) in front of the current text, so a
77
+ # long final report cut off mid-way is delivered whole (issue #610).
78
+ def final_text(res, text)
79
+ return text if continuation_manager.continuation_buffer.empty?
80
+
81
+ raw = res.respond_to?(:text) ? res.text : text
82
+ continuation_manager.stitch_and_reset(Platform.safe_utf8(raw).to_s, history: @inst.history)
83
+ end
84
+
85
+ # Same truncation flow as the user channel (issue #610): reasoning
86
+ # that spent the whole output budget gets a targeted continuation
87
+ # nudge instead of the generic empty-response strike. When the
88
+ # auto-continue ceiling is exhausted (or disabled) the partial text
89
+ # already produced is salvaged as the result; with nothing to
90
+ # salvage the turn fails with the actionable diagnostic.
91
+ def handle_truncation(res, text_resp, tool_calls, finish_reason)
92
+ cm = continuation_manager
93
+ return nil unless cm.truncation?(finish_reason)
94
+
95
+ raw_text = res.respond_to?(:text) ? res.text : text_resp
96
+ safe_text = Platform.safe_utf8(raw_text).to_s
97
+ action = cm.handle_truncation(
98
+ finish_reason: finish_reason, text_resp: safe_text, tool_calls: tool_calls, res: res,
99
+ max_tokens: @inst.max_output_tokens || 4096
100
+ )
101
+ return continue_truncated(action) if action[:continue]
102
+ return nil unless action[:error]
103
+
104
+ salvage_or_fail(cm, safe_text, action[:error])
105
+ end
64
106
 
65
107
  # ── announce ──
66
108
  def announce_compaction(reason, layer)
@@ -70,12 +112,27 @@ module Rubik
70
112
  def announce_llm_thought(_text, **) = nil
71
113
 
72
114
  # ── terminals & dispatch ──
115
+ def continuation_manager
116
+ @continuation_manager ||= ContinuationManager.new(config: @inst.config)
117
+ end
118
+
73
119
  # The shared two-strike nudge policy (Channel#empty_response_turn)
74
120
  # now governs empty, tool-less responses: one standard nudge row,
75
121
  # then the terminal below on the second consecutive strike. The
76
122
  # subagent can no longer "complete" with an empty result — the
77
123
  # declared Phase 4 delta.
124
+ #
125
+ # Second consecutive empty strike: the run's work is already in
126
+ # history, so deliver the last assistant text as a clearly marked
127
+ # salvaged result instead of an empty failure (issue #610). With
128
+ # nothing to salvage the turn still fails.
78
129
  def empty_response_failure
130
+ salvaged = last_assistant_text
131
+ if salvaged
132
+ continuation_manager.reset!
133
+ return final_text_result("#{SALVAGE_NOTICE}\n\n#{salvaged}")
134
+ end
135
+
79
136
  @inst.fail_turn!("Empty response from model (2 consecutive)")
80
137
  { ok: false, completed: false }
81
138
  end
@@ -90,19 +147,67 @@ module Rubik
90
147
  @inst.history << { role: "assistant", content: final_text.empty? ? nil : final_text, tool_calls: tool_calls }
91
148
  end
92
149
 
93
- def on_finish(args, fallback_text)
150
+ # The assistant row carrying the finish tool_use was already
151
+ # recorded; it needs a pairing tool-result row before
152
+ # complete_turn! or the preserved history hands strict providers
153
+ # an orphaned tool_use on the next request (issue #593 — mirrors
154
+ # StepExecutor#handle_finish_tool and the user channel's deferred
155
+ # finish path).
156
+ def on_finish(args, fallback_text, call_id = nil)
94
157
  message = args["message"] || args[:message] || fallback_text || "Completed"
95
158
  @inst.auto_apply_buffer!(:finish) unless @inst.workspace == "branch"
159
+ if call_id
160
+ Rubik::Orchestrator::ToolHistory.write(
161
+ @inst.history, tool_call_id: call_id, tool_name: "finish",
162
+ status: :ok, duration_ms: 0.0, content: message.to_s
163
+ )
164
+ end
96
165
  @inst.complete_turn!(message)
97
166
  { ok: true, completed: true }
98
167
  end
99
168
 
100
169
  def execute_tool_call(tool_call, text)
101
- return on_finish(tool_call["args"] || {}, text) if TurnExecutor.finish_call?(@inst.tool_registry, tool_call)
170
+ return on_finish(tool_call["args"] || {}, text, tool_call["id"]) if TurnExecutor.finish_call?(@inst.tool_registry, tool_call)
102
171
 
103
172
  @inst.execute_subagent_tool_call(tool_call)
104
173
  nil
105
174
  end
175
+
176
+ private
177
+
178
+ def last_assistant_text
179
+ row = @inst.history.reverse_each.find do |m|
180
+ m.is_a?(Hash) && m[:role] == "assistant" && !m[:content].to_s.strip.empty?
181
+ end
182
+ row && row[:content].to_s.strip
183
+ end
184
+
185
+ def continue_truncated(action)
186
+ nudge_policy.reset!
187
+ if [:thinking_exhausted, :text_truncated].include?(action[:scenario]) && @inst.respond_to?(:raise_max_output_tokens!)
188
+ @inst.raise_max_output_tokens!(MAX_OUTPUT_TOKENS_CEILING)
189
+ end
190
+ if action[:thought]
191
+ @inst.history << { role: "assistant", content: nil, thought: Platform.safe_utf8(action[:thought]) }
192
+ elsif action[:partial_text]
193
+ @inst.history << { role: "assistant", content: action[:partial_text] }
194
+ end
195
+ @inst.history << { role: "user", content: action[:nudge] }
196
+ :consumed
197
+ end
198
+
199
+ def salvage_or_fail(manager, current_text, error)
200
+ accumulated = manager.continuation_buffer + current_text
201
+ if accumulated.strip.empty?
202
+ manager.reset!
203
+ @inst.fail_turn!(error)
204
+ return { ok: false, completed: false }
205
+ end
206
+
207
+ manager.absorb_partial_rows!(@inst.history)
208
+ manager.reset!
209
+ final_text_result(accumulated.strip)
210
+ end
106
211
  end
107
212
  end
108
213
  end
@@ -69,9 +69,11 @@ module Rubik
69
69
 
70
70
  # Stitch the RAW response (sanitized): `text` was already stripped
71
71
  # by TurnExecutor#call, which would eat the leading indentation of
72
- # the continuation chunk (issue #547).
72
+ # the continuation chunk (issue #547). The history is handed in so
73
+ # the prior partial assistant rows are absorbed — the stitched
74
+ # full text must not duplicate them (issue #595-1).
73
75
  raw_text = res.respond_to?(:text) ? res.text : text
74
- cm.stitch_and_reset(Platform.safe_utf8(raw_text).to_s)
76
+ cm.stitch_and_reset(Platform.safe_utf8(raw_text).to_s, history: @deps.history)
75
77
  end
76
78
 
77
79
  def track_usage(res) = @deps.budget_manager.track_token_usage!(res)
@@ -113,6 +115,17 @@ module Rubik
113
115
  end
114
116
  return nil unless trunc_action[:error]
115
117
 
118
+ # Failure terminal (auto-continue disabled or ceiling exhausted):
119
+ # the generated content must not be dropped from history (issue
120
+ # #595-2). Persist buffer + current partial as ONE assistant row,
121
+ # absorbing any prior partial rows so nothing is duplicated —
122
+ # the same invariant as the stitch path.
123
+ accumulated = cm.continuation_buffer + safe_text
124
+ unless accumulated.strip.empty?
125
+ cm.absorb_partial_rows!(@deps.history)
126
+ @deps.history << { role: "assistant", content: accumulated }
127
+ end
128
+
116
129
  @deps.on_step&.call(type: :error, content: trunc_action[:error], step: @deps.step_count, max_steps: @deps.max_steps)
117
130
  @deps.loop_instance.turn_result(success: false, error: trunc_action[:error])
118
131
  end
@@ -50,6 +50,7 @@ module Rubik
50
50
  pid = nil
51
51
  watchdog = nil
52
52
  deadline_hit = false
53
+ timed_out = false
53
54
 
54
55
  begin
55
56
  Open3.popen3(*, chdir: chdir) do |stdin, stdout, stderr, wait_thr|
@@ -65,12 +66,31 @@ module Rubik
65
66
  # Cancelled in the inner ensure to keep the watchdog from firing
66
67
  # into a recycled pid after normal exit.
67
68
  watchdog = Thread.new(pid, timeout.to_f) do |w_pid, w_deadline|
68
- sleep w_deadline
69
- deadline_hit = true
70
- kill_tree(w_pid)
71
- rescue StandardError
72
- deadline_hit = true
73
- nil
69
+ slept = false
70
+ begin
71
+ sleep w_deadline
72
+ slept = true
73
+ # Race guard (issue #597): only flag a timeout when the kill
74
+ # actually targets a still-live process — a child that completes
75
+ # exactly as the deadline fires keeps its real exit status
76
+ # instead of being relabeled exit 124.
77
+ deadline_hit = true if process_alive?(w_pid)
78
+ kill_tree(w_pid)
79
+ rescue StandardError => e
80
+ # Issue #597-2 follow-up: a kill-path failure at the deadline
81
+ # must never silently drop the timeout — the deadline has
82
+ # fired, so mark it (the caller below raises Timeout::Error
83
+ # and still attempts its own kill) and log the failure.
84
+ # `slept` guards against a spurious mark if the thread itself
85
+ # dies before the deadline. Thread#kill lands as an
86
+ # uncatchable KillError, so a normal-exit cancellation never
87
+ # reaches this rescue.
88
+ if slept
89
+ deadline_hit = true
90
+ Rubik::Log.warn(:platform, "popen_capture deadline kill failed for pid #{w_pid}: " \
91
+ "#{e.class}: #{e.message}")
92
+ end
93
+ end
74
94
  end
75
95
  begin
76
96
  stdin.close
@@ -114,6 +134,7 @@ module Rubik
114
134
  end
115
135
  raise Timeout::Error, "execution expired" if deadline_hit
116
136
  rescue Timeout::Error
137
+ timed_out = true
117
138
  kill_tree(pid) if pid
118
139
  stderr_str = timeout_message || "Execution timed out after #{timeout} seconds"
119
140
  exit_code = 124
@@ -134,7 +155,11 @@ module Rubik
134
155
  stderr: stderr_str,
135
156
  output: [stdout_str, stderr_str].reject(&:empty?).join("\n"),
136
157
  duration: duration.round(4),
137
- pid: pid
158
+ pid: pid,
159
+ # Explicit timeout flag (issue #597): exit code 124 is also a
160
+ # legitimate exit code of timeout(1) and other workloads, so the
161
+ # tool layer must not re-derive `timed_out` from the exit code.
162
+ timed_out: timed_out || deadline_hit
138
163
  }
139
164
  end
140
165
 
@@ -60,21 +60,23 @@ module Rubik
60
60
  end
61
61
 
62
62
  # PERF: Fast-path deep UTF-8 cleaner. Performs zero-allocation Symbol
63
- # validation via Symbol#name and tracks cycle object IDs in a Hash to
64
- # eliminate Set instance allocations (~3.3x speedup, ~70% faster).
63
+ # validation via Symbol#name and tracks cycle object IDs in a path
64
+ # stack: ids are added on descent and removed after the children are
65
+ # visited, so only objects currently on the traversal path (genuine
66
+ # cycles) are cut — a shared object referenced from two sibling places
67
+ # is emitted in full both times (issue #597).
65
68
  def clean_utf8_deep(obj, seen = nil)
69
+ stack = seen.is_a?(Array) ? seen : []
66
70
  case obj
67
71
  when String
68
72
  clean_utf8(obj)
69
73
  when Symbol
70
74
  fast_clean_symbol(obj)
71
75
  when Hash
72
- seen ||= {}
73
76
  obj_id = obj.object_id
74
- return "{...}" if seen.is_a?(Hash) ? seen.key?(obj_id) : seen.include?(obj_id)
75
-
76
- seen.is_a?(Hash) ? (seen[obj_id] = true) : seen.add(obj_id)
77
+ return "{...}" if stack.include?(obj_id)
77
78
 
79
+ stack << obj_id
78
80
  res = {}
79
81
  obj.each do |k, v|
80
82
  cleaned_k = if k.is_a?(Symbol)
@@ -82,27 +84,24 @@ module Rubik
82
84
  elsif k.is_a?(String)
83
85
  clean_utf8(k)
84
86
  else
85
- clean_utf8_deep(k, seen)
87
+ clean_utf8_deep(k, stack)
86
88
  end
87
- res[cleaned_k] = clean_utf8_deep(v, seen)
89
+ res[cleaned_k] = clean_utf8_deep(v, stack)
88
90
  end
91
+ stack.pop
89
92
  res
90
93
  when Array
91
- seen ||= {}
92
94
  obj_id = obj.object_id
93
- return "[...]" if seen.is_a?(Hash) ? seen.key?(obj_id) : seen.include?(obj_id)
94
-
95
- seen.is_a?(Hash) ? (seen[obj_id] = true) : seen.add(obj_id)
95
+ return "[...]" if stack.include?(obj_id)
96
96
 
97
- obj.map { |item| clean_utf8_deep(item, seen) }
97
+ stack << obj_id
98
+ obj.map { |item| clean_utf8_deep(item, stack) }.tap { stack.pop }
98
99
  when Set
99
- seen ||= {}
100
100
  obj_id = obj.object_id
101
- return Set.new(["[...]"]) if seen.is_a?(Hash) ? seen.key?(obj_id) : seen.include?(obj_id)
102
-
103
- seen.is_a?(Hash) ? (seen[obj_id] = true) : seen.add(obj_id)
101
+ return Set.new(["[...]"]) if stack.include?(obj_id)
104
102
 
105
- obj.to_set { |item| clean_utf8_deep(item, seen) }
103
+ stack << obj_id
104
+ obj.to_set { |item| clean_utf8_deep(item, stack) }.tap { stack.pop }
106
105
  else
107
106
  obj
108
107
  end
@@ -61,6 +61,33 @@ module Rubik
61
61
  env
62
62
  end
63
63
 
64
+ # Credential-shaped variable names stripped from child-process
65
+ # environments (issue #586). Matching is by underscore-delimited
66
+ # segment (case-insensitive) so OPENAI_API_KEY, MY_SECRET and
67
+ # AWS_ACCESS_KEY_ID are covered without matching words like
68
+ # "tokenizer". NOTE: integrity hygiene, NOT a confinement boundary.
69
+ CREDENTIAL_ENV_PATTERN = /
70
+ (?:_|\A)(?:KEY|TOKEN|SECRET|PASSWORD|PASSWD|PAT|CREDENTIALS?)(?:_|\z)
71
+ |\A(?:DATABASE|REDIS|MONGO|POSTGRES|MYSQL|AMQP)_URL\z
72
+ |_(?:DATABASE|CONNECTION)_URL\z
73
+ /ix
74
+
75
+ # sanitized_spawn_env plus an explicit nil for every credential-shaped
76
+ # host variable (spawn semantics: nil UNSETs it; benign variables stay
77
+ # out of the hash and remain normally inheritable). Consumers:
78
+ # TaskManager#start (issue #586) and Driver::ProcessDriver#child_env
79
+ # (both now delegate here — single shared CREDENTIAL_ENV_PATTERN).
80
+ # The MCP stdio transport keeps its own narrower pattern on purpose:
81
+ # it matches _REDIS_URL, which this pattern's underscore-URL branch
82
+ # does not, so this pattern is not a safe superset for that path.
83
+ def credential_free_env
84
+ env = sanitized_spawn_env
85
+ ENV.each_key do |key|
86
+ env[key] = nil if key.match?(CREDENTIAL_ENV_PATTERN)
87
+ end
88
+ env
89
+ end
90
+
64
91
  def host_os
65
92
  RbConfig::CONFIG["host_os"] || ""
66
93
  end
@@ -92,6 +92,8 @@ module Rubik
92
92
  # against. Keyed by plugin name (the package root's basename).
93
93
  def record_package(plugin_dir, name, scope: :project)
94
94
  target_dir = scope.to_sym == :global ? global_plugins_dir : project_plugins_dir
95
+ # Fail-closed on a corrupt manifest (issue #596, cf. record_hash).
96
+ manifest_store.refuse_corrupt_manifest!(target_dir, :record, name.to_s)
95
97
  manifest = load_manifest(target_dir)
96
98
  manifest[name.to_s] = {
97
99
  "type" => "directory",
@@ -143,6 +145,9 @@ module Rubik
143
145
 
144
146
  if File.directory?(source_path)
145
147
  plugin_name = File.basename(source_path)
148
+ # Same guard as remove_plugin (issues #554/#596).
149
+ return { ok: false, error: "Invalid plugin name: #{plugin_name.inspect}." } unless safe_plugin_name?(plugin_name, target_dir)
150
+
146
151
  dest_path = File.join(target_dir, plugin_name)
147
152
 
148
153
  if (violation = PackageInventory.violation(source_path))
@@ -159,9 +164,8 @@ module Rubik
159
164
  return { ok: true, name: plugin_name, path: dest_path, type: :directory }
160
165
  end
161
166
 
162
- # Deterministic re-add (also the legacy-manifest re-consent path):
163
- # replace any previous copy so stale helpers cannot survive into
164
- # the newly consented package.
167
+ # Deterministic re-add (also the legacy re-consent path): replace
168
+ # any previous copy so stale helpers cannot survive the re-consent.
165
169
  FileUtils.rm_rf(dest_path)
166
170
  FileUtils.cp_r(source_path, dest_path)
167
171
 
@@ -176,8 +180,12 @@ module Rubik
176
180
 
177
181
  { ok: true, name: plugin_name, path: dest_path, type: :directory }
178
182
  else
183
+ source_name = File.basename(source_path)
179
184
  plugin_name = File.basename(source_path, ".rb")
180
- dest_path = File.join(target_dir, File.basename(source_path))
185
+ # Same guard as remove_plugin (issue #554) before the cp (issue #596).
186
+ return { ok: false, error: "Invalid plugin name: #{plugin_name.inspect}." } unless safe_plugin_name?(source_name, target_dir)
187
+
188
+ dest_path = File.join(target_dir, source_name)
181
189
  FileUtils.cp(source_path, dest_path)
182
190
  record_hash(dest_path, scope: scope)
183
191
 
@@ -224,9 +232,8 @@ module Rubik
224
232
 
225
233
  # A plugin name must resolve to a single path component directly under
226
234
  # target_dir. Reject separators, traversal components (`.`/`..`),
227
- # absolute paths and NUL bytes before any rm_f/rm_rf so a caller-supplied
228
- # name can never escape the plugins directory (issue #554). Any ordinary
229
- # single-component name is allowed, so legitimate plugins keep working.
235
+ # absolute paths and NUL bytes before any rm_f/rm_rf (issue #554).
236
+ # Any ordinary single-component name is allowed, so plugins keep working.
230
237
  def safe_plugin_name?(name, target_dir)
231
238
  component = name.to_s
232
239
  return false if component.empty? || component == "." || component == ".."
@@ -240,8 +247,8 @@ module Rubik
240
247
  end
241
248
 
242
249
  # The package root for a path inside the plugins dir: plugins/<first
243
- # segment> when that segment is a DIRECTORY (directory plugin); nil for
244
- # a single-file plugin. A path outside the plugins dir is not a package.
250
+ # segment> when that segment is a DIRECTORY; nil for a single-file
251
+ # plugin or a path outside the plugins dir.
245
252
  def package_root_for(path, plugins_dir)
246
253
  base = File.expand_path(plugins_dir)
247
254
  prefix = "#{base}#{File::SEPARATOR}"
@@ -259,8 +266,7 @@ module Rubik
259
266
 
260
267
  # Whole-package verification. A legacy (entry-file-only) manifest cannot
261
268
  # vouch for helpers that were never consented to, so it is rejected with
262
- # an explicit re-consent path: re-add the plugin, or opt into trust with
263
- # RUBIK_TRUST_OVERLAYS=1.
269
+ # an explicit re-consent path (re-add, or RUBIK_TRUST_OVERLAYS=1).
264
270
  def verify_package_integrity(package_dir, scope: :project)
265
271
  dir = scope.to_sym == :global ? global_plugins_dir : project_plugins_dir
266
272
  name = File.basename(package_dir)
@@ -201,8 +201,9 @@ module Rubik
201
201
  "dedicated notification-evaluation turn — evaluate the pending notifications, act (extend budget, steer with " \
202
202
  "send_message, retrieve results, wrap up, or report to the user), and conclude your turn again. Subagents can " \
203
203
  "ask YOU questions mid-flight via `ask_question` — a question notification means that subagent is BLOCKED until " \
204
- "you reply: answer it promptly with `send_message(recipient: '<id>', message: '<your answer>')` (it times out " \
205
- "otherwise). Call `#{Rubik::Orchestrator::Subagent::Notices.wait_hint_template}` only when synchronous waiting " \
204
+ "you reply: answer it promptly with `send_message(recipient: '<id>', message: '<your answer>', " \
205
+ "in_reply_to: '<question_id>')` — echo the question_id from the notification, it times out otherwise. " \
206
+ "Call `#{Rubik::Orchestrator::Subagent::Notices.wait_hint_template}` only when synchronous waiting " \
206
207
  "is strictly required. Never poll 'status' repeatedly in a loop."
207
208
  end
208
209
  end
@@ -29,12 +29,16 @@ module Rubik
29
29
 
30
30
  # One honest line for a failed disk write; returns true when it fired
31
31
  # so callers can step past their success report. The in-memory change
32
- # stands — only persistence failed.
32
+ # stands — only persistence failed. `ctx` is the REPL context, but an
33
+ # IO/output stream is also accepted (workspace_approval passes @out
34
+ # directly — issue #598): anything responding to #out is unwrapped,
35
+ # anything else is written to via #puts.
33
36
  def self.report_persist_failure(ctx, res, scope)
34
37
  return false if res.nil? || res[:ok]
35
38
 
36
- ctx.out.puts "Error: could not save to #{scope_label(scope)}: #{res[:error]} " \
37
- "(change kept for this session only)."
39
+ out = ctx.respond_to?(:out) ? ctx.out : ctx
40
+ out.puts "Error: could not save to #{scope_label(scope)}: #{res[:error]} " \
41
+ "(change kept for this session only)."
38
42
  true
39
43
  end
40
44
 
@@ -86,8 +86,12 @@ module Rubik
86
86
  target_path
87
87
  end
88
88
  if ctx.buffer_manager.dirty_files.key?(rel)
89
- full_path = File.expand_path(rel, ctx.buffer_manager.root_dir)
90
- orig_content = File.exist?(full_path) ? File.read(full_path) : ""
89
+ # Route through Manager.read(from_disk: true) so the disk side
90
+ # gets the same read_bytes_as_utf8 encoding as the buffer side
91
+ # (issue #589) — a plain File.read can raise
92
+ # Encoding::CompatibilityError in unified_diff for files with
93
+ # invalid-UTF-8 bytes.
94
+ orig_content = ctx.buffer_manager.read(rel, from_disk: true) || ""
91
95
  new_content = ctx.buffer_manager.read(rel) || ""
92
96
  Rubik::UI::DiffViewer.render(ctx, theme, Rubik::Buffer::Diff.unified_diff(rel, orig_content, new_content))
93
97
  else
@@ -127,6 +127,12 @@ module Rubik
127
127
  FileUtils.mkdir_p(target_dir)
128
128
  target_file = File.join(target_dir, "#{safe_name}.jsonl")
129
129
 
130
+ # Durability barrier before the copy so the copied JSONL can
131
+ # never carry a torn tail from the recorder's append stream
132
+ # (issue #598). Both branches write the same JSONL content; the
133
+ # fallback only fires when no live file exists yet, in which
134
+ # case @events holds the same (recent) events.
135
+ ctx.recorder.flush
130
136
  if ctx.recorder.session_file && File.exist?(ctx.recorder.session_file)
131
137
  FileUtils.cp(ctx.recorder.session_file, target_file)
132
138
  else
@@ -134,7 +140,11 @@ module Rubik
134
140
  ctx.recorder.events.each { |ev| f.puts(JSON.dump(ev)) }
135
141
  end
136
142
  end
137
- ctx.recorder.switch_session_file(target_file, cleanup_empty_current: false)
143
+ # Provenance: the saved copy's parent is the session it was
144
+ # copied FROM (captured before the switch), not the target's
145
+ # own basename (issue #598).
146
+ ctx.recorder.switch_session_file(target_file, cleanup_empty_current: false,
147
+ parent_session_id: ctx.recorder.session_id)
138
148
  ctx.out.puts "[Rubik::Session] Session saved to #{target_file}"
139
149
  end
140
150
 
@@ -212,7 +212,7 @@ module Rubik
212
212
  # context already includes the idle-injected notices. The notice
213
213
  # turn itself drains first, so this clear is a no-op for it.
214
214
  clear_pending_wake_notices! if respond_to?(:clear_pending_wake_notices!)
215
- @recorder&.record_user_turn(text)
215
+ @recorder&.record_user_turn(text) unless @replaying
216
216
  @event_bus.emit(:prompt_received, prompt: text)
217
217
  task
218
218
  end
@@ -270,12 +270,12 @@ module Rubik
270
270
  md_stream.finish { |seg| @output.print seg }
271
271
  @output.flush if @output.respond_to?(:flush)
272
272
  when :tool_call
273
- @recorder&.record_tool_call(step_info[:tool], step_info[:args], call_id: step_info[:call_id])
273
+ @recorder&.record_tool_call(step_info[:tool], step_info[:args], call_id: step_info[:call_id]) unless @replaying
274
274
  formatted = renderer.render(step_info)
275
275
  @output.puts formatted if formatted && !formatted.empty?
276
276
  @output.flush if @output.respond_to?(:flush)
277
277
  when :tool_result
278
- @recorder&.record_tool_result(step_info[:tool], step_info[:output], status: step_info[:status] || :ok, call_id: step_info[:call_id])
278
+ @recorder&.record_tool_result(step_info[:tool], step_info[:output], status: step_info[:status] || :ok, call_id: step_info[:call_id]) unless @replaying
279
279
  formatted = renderer.render(step_info)
280
280
  @output.puts formatted if formatted && !formatted.empty?
281
281
  @output.flush if @output.respond_to?(:flush)
@@ -138,8 +138,12 @@ module Rubik
138
138
  DEFINITION.new(
139
139
  name: :snapshot_store,
140
140
  builder: lambda do |s|
141
- Rubik::Buffer::SnapshotStore.new(storage_dir: File.join(s.home_dir, ".rubik", "snapshots",
142
- File.basename(s.root_dir)))
141
+ # Issue #589: debounced snapshot persistence (undo granularity
142
+ # stays in memory) + on-disk retention cap.
143
+ Rubik::Buffer::SnapshotStore.new(
144
+ storage_dir: File.join(s.home_dir, ".rubik", "snapshots", File.basename(s.root_dir)),
145
+ min_interval: 1.0, max_snapshots: 200
146
+ )
143
147
  end,
144
148
  provide: Rubik::Coeffects::SNAPSHOT_STORE
145
149
  ),
@@ -47,7 +47,10 @@ module Rubik
47
47
  @conversation_history << { role: "user", content: user_text }
48
48
  @conversation_history << { role: "assistant", content: assistant_content.to_s }
49
49
  trim_conversation_window!
50
- @recorder&.record_assistant_response(assistant_content.to_s, tokens: tokens, streamed: streamed)
50
+ # Same one policy as the input record (shell.rb): replayed turns
51
+ # hydrate the conversation window but must not re-record events
52
+ # into the active session JSONL (issue #598).
53
+ @recorder&.record_assistant_response(assistant_content.to_s, tokens: tokens, streamed: streamed) unless @replaying
51
54
  rescue StandardError => e
52
55
  Rubik::Platform.debug_warn("turn outcome recording failed (#{e.class}: #{e.message}); turn lost from conversation memory")
53
56
  nil
@@ -59,8 +59,11 @@ module Rubik
59
59
 
60
60
  # Per-basename hash record: load → merge → save. The caller owns hash
61
61
  # computation (file vs normalized content) and pre-write guards; the
62
- # store owns the entry shape and the manifest mutation.
62
+ # store owns the entry shape and the manifest mutation. Fail-closed on
63
+ # a corrupt manifest — recording would rewrite it with only the new
64
+ # entry and evict the other consented records (issue #596).
63
65
  def record_hash(dir, filename, hash, scope:)
66
+ refuse_corrupt_manifest!(dir, :record, filename)
64
67
  manifest = load_manifest(dir)
65
68
  manifest[filename] = {
66
69
  "sha256" => hash,
@@ -71,8 +74,10 @@ module Rubik
71
74
  end
72
75
 
73
76
  # Symmetric retraction: remove a basename and persist. Returns whether
74
- # the key existed (mirrors Hash#delete's semantics).
77
+ # the key existed (mirrors Hash#delete's semantics). Fail-closed on a
78
+ # corrupt manifest for the same eviction reason as record_hash.
75
79
  def remove_entry(dir, filename)
80
+ refuse_corrupt_manifest!(dir, :delete, filename)
76
81
  manifest = load_manifest(dir)
77
82
  existed = manifest.delete(filename)
78
83
  save_manifest(dir, manifest) if existed