rubik 0.13.0 → 0.15.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.
- checksums.yaml +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +122 -1
- data/lib/rubik/adapter/node.rb +4 -1
- data/lib/rubik/ast/c_extractor.rb +1 -1
- data/lib/rubik/ast/fallback_cleaner.rb +6 -1
- data/lib/rubik/ast/graph.rb +38 -57
- data/lib/rubik/ast/graph_extensions.rb +57 -15
- data/lib/rubik/ast/ruby_extractor.rb +16 -6
- data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
- data/lib/rubik/buffer/manager.rb +38 -11
- data/lib/rubik/buffer/snapshot.rb +116 -28
- data/lib/rubik/cli/connect.rb +3 -5
- data/lib/rubik/config/defaults.rb +3 -7
- data/lib/rubik/config/env_guard.rb +43 -0
- data/lib/rubik/config/schema.rb +7 -1
- data/lib/rubik/config/scoped.rb +12 -5
- data/lib/rubik/config/secret_policy.rb +6 -5
- data/lib/rubik/config.rb +1 -1
- data/lib/rubik/connect/models_dev.rb +11 -0
- data/lib/rubik/connect/probes.rb +9 -2
- data/lib/rubik/connect/wizard.rb +117 -57
- data/lib/rubik/context/repo_context_reader.rb +8 -4
- data/lib/rubik/diagnostics/collector.rb +25 -2
- data/lib/rubik/llm/chunk_assembler.rb +12 -1
- data/lib/rubik/llm/client.rb +19 -0
- data/lib/rubik/llm/connection_pool.rb +7 -1
- data/lib/rubik/llm/formatter.rb +22 -2
- data/lib/rubik/llm/provider_key.rb +32 -0
- data/lib/rubik/llm/registry.rb +18 -2
- data/lib/rubik/llm/router.rb +27 -1
- data/lib/rubik/llm/session_header.rb +128 -0
- data/lib/rubik/llm/sse_parser.rb +57 -10
- data/lib/rubik/lsp/client.rb +244 -23
- data/lib/rubik/lsp/document_store.rb +6 -0
- data/lib/rubik/lsp/manager.rb +14 -0
- data/lib/rubik/lsp/transport.rb +34 -7
- data/lib/rubik/mcp/transport/stdio.rb +42 -4
- data/lib/rubik/orchestrator/continuation_manager.rb +68 -7
- data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
- data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
- data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
- data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
- data/lib/rubik/orchestrator/goal_session.rb +14 -14
- data/lib/rubik/orchestrator/goal_wait.rb +15 -9
- data/lib/rubik/orchestrator/loop.rb +39 -39
- data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
- data/lib/rubik/orchestrator/step_executor.rb +17 -2
- data/lib/rubik/orchestrator/subagent/instance.rb +46 -5
- data/lib/rubik/orchestrator/subagent/lifecycle.rb +25 -25
- data/lib/rubik/orchestrator/subagent/messaging.rb +5 -1
- data/lib/rubik/orchestrator/subagent/narration.rb +2 -1
- data/lib/rubik/orchestrator/subagent/notices.rb +7 -3
- data/lib/rubik/orchestrator/subagent/questioner.rb +42 -4
- data/lib/rubik/orchestrator/subagent/reaper.rb +70 -20
- data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
- data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
- data/lib/rubik/orchestrator/subagent/supervisor.rb +21 -3
- data/lib/rubik/orchestrator/tool_call_id.rb +8 -4
- data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +21 -5
- data/lib/rubik/orchestrator/turn_executor/user_channel.rb +40 -5
- data/lib/rubik/orchestrator/turn_executor.rb +24 -8
- data/lib/rubik/platform/filesystem.rb +52 -24
- data/lib/rubik/platform/process.rb +32 -7
- data/lib/rubik/platform/sanitize.rb +17 -18
- data/lib/rubik/platform.rb +27 -0
- data/lib/rubik/plugin/storage.rb +34 -8
- data/lib/rubik/prompt/context_builder.rb +2 -1
- data/lib/rubik/prompt/doctrine.rb +3 -2
- data/lib/rubik/repair/loop.rb +22 -2
- data/lib/rubik/repl/commands/agent_context.rb +1 -1
- data/lib/rubik/repl/commands/agent_execution.rb +20 -15
- data/lib/rubik/repl/commands/base.rb +7 -3
- data/lib/rubik/repl/commands/buffer.rb +21 -2
- data/lib/rubik/repl/commands/connect.rb +0 -1
- data/lib/rubik/repl/commands/lsp.rb +1 -1
- data/lib/rubik/repl/commands/plugins.rb +31 -31
- data/lib/rubik/repl/commands/session.rb +16 -6
- data/lib/rubik/repl/commands/workspace.rb +4 -4
- data/lib/rubik/repl/input_execution.rb +33 -32
- data/lib/rubik/repl/input_loop.rb +9 -4
- data/lib/rubik/repl/lifecycle.rb +1 -2
- data/lib/rubik/repl/notice_router.rb +11 -5
- data/lib/rubik/repl/paste_support.rb +1 -1
- data/lib/rubik/repl/prompt_grace.rb +3 -3
- data/lib/rubik/repl/service_catalog.rb +6 -2
- data/lib/rubik/repl/shell.rb +10 -2
- data/lib/rubik/repl/turn_recorder.rb +18 -4
- data/lib/rubik/runtime/context.rb +33 -27
- data/lib/rubik/runtime/manager.rb +23 -15
- data/lib/rubik/runtime/manifest_store.rb +7 -2
- data/lib/rubik/runtime/task_manager.rb +13 -10
- data/lib/rubik/sandbox/driver/base.rb +34 -0
- data/lib/rubik/sandbox/driver/bubblewrap.rb +60 -8
- data/lib/rubik/sandbox/driver/docker.rb +8 -0
- data/lib/rubik/sandbox/driver/podman.rb +8 -0
- data/lib/rubik/sandbox/driver/process.rb +29 -1
- data/lib/rubik/sandbox/runner.rb +9 -1
- data/lib/rubik/security/classifier/classifier.rb +147 -13
- data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
- data/lib/rubik/security/classifier/commands/git.yaml +27 -1
- data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
- data/lib/rubik/security/classifier/database.rb +2 -2
- data/lib/rubik/security/command_gate.rb +8 -1
- data/lib/rubik/security/path_policy.rb +28 -3
- data/lib/rubik/security/redactor.rb +62 -19
- data/lib/rubik/session/recorder.rb +27 -20
- data/lib/rubik/session/replayer.rb +29 -7
- data/lib/rubik/storage/store.rb +26 -5
- data/lib/rubik/tools/builtins/exec.rb +8 -2
- data/lib/rubik/tools/builtins/files.rb +8 -12
- data/lib/rubik/tools/builtins/grep.rb +3 -2
- data/lib/rubik/tools/builtins/interactive.rb +5 -2
- data/lib/rubik/tools/builtins/lsp.rb +59 -9
- data/lib/rubik/tools/builtins/send_message.rb +7 -2
- data/lib/rubik/tools/builtins/show_file.rb +1 -1
- data/lib/rubik/tools/builtins/synthesis.rb +4 -4
- data/lib/rubik/tools/preflight_runner.rb +4 -1
- data/lib/rubik/tools/result_normalizer.rb +1 -1
- data/lib/rubik/ui/banner.rb +3 -3
- data/lib/rubik/ui/dialog.rb +15 -8
- data/lib/rubik/ui/diff_viewer.rb +4 -4
- data/lib/rubik/ui/frame.rb +13 -3
- data/lib/rubik/ui/markdown_stream.rb +8 -2
- data/lib/rubik/ui/panel.rb +11 -11
- data/lib/rubik/ui/renderer.rb +10 -7
- data/lib/rubik/ui/spinner.rb +1 -1
- data/lib/rubik/ui/statusline.rb +3 -3
- data/lib/rubik/ui/theme.rb +1 -1
- data/lib/rubik/version.rb +1 -1
- metadata +4 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../../log"
|
|
3
4
|
require_relative "../../platform"
|
|
4
5
|
require_relative "../../storage/layout"
|
|
5
6
|
|
|
@@ -13,13 +14,35 @@ module Rubik
|
|
|
13
14
|
# ASLA reap edilmez: thread canlıdır (condvar park), resume hakları
|
|
14
15
|
# bozulmaz.
|
|
15
16
|
module Reaper
|
|
17
|
+
# Bounded transcript-save retry policy (#553): a failed save! returns
|
|
18
|
+
# nil and leaves no file, which previously made archive! return false
|
|
19
|
+
# forever — the instance was unreapable and its history leaked. After
|
|
20
|
+
# this many failed sweeps we reap WITH A WARNING; the save failure is
|
|
21
|
+
# still observable via last_error, and liveness beats a stuck tombstone.
|
|
22
|
+
TRANSCRIPT_ARCHIVE_MAX_RETRIES = 3
|
|
23
|
+
|
|
16
24
|
def archived? = !!@archived
|
|
17
25
|
|
|
18
26
|
# History yalnız transcript diske DURABLY yazıldıysa silinir —
|
|
19
|
-
# dosya yoksa reap sonraki süpürmeye kalır (yarım tasfiye yok).
|
|
20
|
-
|
|
27
|
+
# dosya yoksa reap sonraki süpürmeye kalır (yarım tasfiye yok). After
|
|
28
|
+
# TRANSCRIPT_ARCHIVE_MAX_RETRIES the durability gate is released with
|
|
29
|
+
# a warning so a permanently-failing save cannot strand the instance.
|
|
30
|
+
# `durable:` lets the supervisor sweep pre-probe the transcript OUTSIDE
|
|
31
|
+
# its monitor (issue #595-5) and hand the verdict in; nil probes here
|
|
32
|
+
# (legacy direct callers keep the retry semantics untouched).
|
|
33
|
+
def archive!(durable: nil)
|
|
21
34
|
@monitor.synchronize do
|
|
22
|
-
|
|
35
|
+
exists = durable.nil? ? File.exist?(@transcript_uri.to_s) : durable
|
|
36
|
+
unless exists
|
|
37
|
+
@archive_retries = (@archive_retries || 0) + 1
|
|
38
|
+
if @archive_retries < TRANSCRIPT_ARCHIVE_MAX_RETRIES
|
|
39
|
+
return false # transient: leave it for the next sweep
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
Rubik::Log.warn(:orchestrator,
|
|
43
|
+
"[Reaper] reaping #{@id} without a durable transcript after #{@archive_retries} attempts " \
|
|
44
|
+
"(last_error: #{@last_error.inspect})")
|
|
45
|
+
end
|
|
23
46
|
|
|
24
47
|
@history.replace([])
|
|
25
48
|
@archived = true
|
|
@@ -33,30 +56,54 @@ module Rubik
|
|
|
33
56
|
# bilinçli olarak yalnız @instances'e bakar (reap edilen zaten
|
|
34
57
|
# notice ile duyurulmuş, sonucu transcriptte).
|
|
35
58
|
module ReaperSupervisor
|
|
59
|
+
# Three-pass sweep (issue #595-5): the supervisor monitor is held
|
|
60
|
+
# only for STATE BOOKKEEPING. Pass 1 snapshots the candidates and
|
|
61
|
+
# releases the lock; the transcript durability probes (File.exist?)
|
|
62
|
+
# and the tombstone cleanups run OUTSIDE it, so spawn/send_message/
|
|
63
|
+
# status are never stalled behind FS IO. Pass 2 keeps archive! UNDER
|
|
64
|
+
# the monitor on purpose: the redeploy plane shares this monitor
|
|
65
|
+
# exactly so an archive cannot interleave a redeployable? check
|
|
66
|
+
# (reuse.rb buffer_for_redeploy) — that invariant is not negotiable.
|
|
36
67
|
def reap_finished!(force: false)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
reaped = []
|
|
41
|
-
@instances.each do |id, inst|
|
|
68
|
+
grace = reap_grace_seconds
|
|
69
|
+
candidates = @monitor.synchronize do
|
|
70
|
+
@instances.values.select do |inst|
|
|
42
71
|
next unless inst.respond_to?(:terminal_and_settled?) && inst.terminal_and_settled?
|
|
43
|
-
next unless force || (grace && inst.ended_at && Time.now - inst.ended_at >= grace)
|
|
44
|
-
next unless inst.archive!
|
|
45
72
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
73
|
+
force || (grace && inst.ended_at && Time.now - inst.ended_at >= grace)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
# OUTSIDE the supervisor monitor: per-candidate transcript probe.
|
|
77
|
+
durability = candidates.to_h { |inst| [inst.id, File.exist?(inst.transcript_uri.to_s)] }
|
|
78
|
+
|
|
79
|
+
dropped_steering = []
|
|
80
|
+
reaped_ids = evicted = nil
|
|
81
|
+
@monitor.synchronize do
|
|
82
|
+
reaped_ids = []
|
|
83
|
+
evicted = []
|
|
84
|
+
candidates.each do |inst|
|
|
85
|
+
# Re-checks (redeploy may have revived it between the passes);
|
|
86
|
+
# archive! + tombstone move stay atomic against the redeploy
|
|
87
|
+
# plane by sharing this monitor.
|
|
88
|
+
next unless @instances[inst.id].equal?(inst) && inst.terminal_and_settled?
|
|
89
|
+
next unless inst.archive!(durable: durability[inst.id])
|
|
90
|
+
|
|
91
|
+
dropped_steering << inst.id if inst.respond_to?(:pending_redeploy_messages?) && inst.pending_redeploy_messages?
|
|
92
|
+
@finished[inst.id] = inst
|
|
93
|
+
reaped_ids << inst.id
|
|
49
94
|
end
|
|
50
|
-
|
|
51
|
-
evict_overflow!
|
|
52
|
-
reaped
|
|
95
|
+
reaped_ids.each { |id| @instances.delete(id) }
|
|
96
|
+
evicted = evict_overflow!
|
|
53
97
|
end
|
|
98
|
+
# OUTSIDE the supervisor monitor: tombstone overflow cleanup
|
|
99
|
+
# (context dispose) must not stall live-plane operations.
|
|
100
|
+
evicted.each(&:cleanup)
|
|
54
101
|
# OUTSIDE the supervisor monitor (emit/logging discipline): queued
|
|
55
102
|
# steering that died with the instance instead of a redeploy — the
|
|
56
103
|
# send_message :queued reply discloses this, but the loss must be
|
|
57
104
|
# observable (issue #55 3.2).
|
|
58
105
|
Platform.debug_warn("[Reaper] reaped with undelivered queued steering: #{dropped_steering.join(', ')}") unless dropped_steering.empty?
|
|
59
|
-
|
|
106
|
+
reaped_ids
|
|
60
107
|
end
|
|
61
108
|
|
|
62
109
|
private
|
|
@@ -80,13 +127,16 @@ module Rubik
|
|
|
80
127
|
Rubik::Storage::Layout.config_get(@config, "subagents.tombstone_limit", 25).to_i
|
|
81
128
|
end
|
|
82
129
|
|
|
83
|
-
# FIFO taşma: en eski mezar
|
|
84
|
-
# :disposed'a
|
|
130
|
+
# FIFO taşma: en eski mezar taşları evict edilir ve CALLING SIDE'a
|
|
131
|
+
# döner — cleanup (:disposed'a çekme, context dispose) monitor
|
|
132
|
+
# DIŞINDA çalışır (issue #595-5).
|
|
85
133
|
def evict_overflow!
|
|
134
|
+
evicted = []
|
|
86
135
|
while @finished.size > tombstone_limit
|
|
87
136
|
_id, inst = @finished.shift
|
|
88
|
-
inst
|
|
137
|
+
evicted << inst if inst
|
|
89
138
|
end
|
|
139
|
+
evicted
|
|
90
140
|
end
|
|
91
141
|
end
|
|
92
142
|
end
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
|
-
require_relative "../../config/defaults"
|
|
5
4
|
require_relative "../../errors"
|
|
6
5
|
require_relative "../../prompt"
|
|
7
6
|
require_relative "../../platform"
|
|
@@ -9,6 +8,8 @@ require_relative "../../security/redactor"
|
|
|
9
8
|
require_relative "../../tools/truncator"
|
|
10
9
|
require_relative "../../uris"
|
|
11
10
|
require_relative "../provider_recovery"
|
|
11
|
+
require_relative "../provider_resolver"
|
|
12
|
+
require_relative "../../llm/session_header"
|
|
12
13
|
require_relative "../../storage/layout"
|
|
13
14
|
require_relative "../auto_apply"
|
|
14
15
|
require_relative "../tool_call_id"
|
|
@@ -114,12 +115,11 @@ module Rubik
|
|
|
114
115
|
effective_opts[:open_timeout] ||= ProviderRecovery::DEFAULT_OPEN_TIMEOUT
|
|
115
116
|
effective_opts[:context_limit] ||= @context_limit if @context_limit
|
|
116
117
|
effective_opts[:max_tokens] ||= @max_output_tokens if @max_output_tokens&.positive?
|
|
117
|
-
#
|
|
118
|
-
#
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
end
|
|
118
|
+
# The root loop's resolution seam: provider-scoped llm.* retry knobs plus
|
|
119
|
+
# the session header (identity: the shell coeffect — same conversation).
|
|
120
|
+
resolver = Rubik::Orchestrator::ProviderResolver.new(provider: @provider, config: @config)
|
|
121
|
+
resolver.apply_retry_defaults(effective_opts)
|
|
122
|
+
resolver.apply_session_header!(effective_opts, Rubik::LLM::SessionHeader.session_key_from_context(@context))
|
|
123
123
|
|
|
124
124
|
# Operation telemetry (issue #126): the model wait is observable
|
|
125
125
|
# while it lasts (status shows kind/name/elapsed) and its duration
|
|
@@ -154,10 +154,17 @@ module Rubik
|
|
|
154
154
|
def kill_all(parent_id: nil)
|
|
155
155
|
cancelled = []
|
|
156
156
|
@monitor.synchronize do
|
|
157
|
-
|
|
157
|
+
# Route through kill_locked (not a bare Instance#kill) so the
|
|
158
|
+
# cascade reaches children and the dependency DAG's queued
|
|
159
|
+
# dependents are cancelled to a fixed point (#553) — otherwise a
|
|
160
|
+
# killed predecessor leaves its dependents stuck in @deferred.
|
|
161
|
+
ids = if parent_id
|
|
162
|
+
@instances.values.select { |i| i.parent_id == parent_id.to_s }.map(&:id)
|
|
163
|
+
else
|
|
164
|
+
@instances.keys + @deferred.keys
|
|
165
|
+
end
|
|
166
|
+
ids.each { |id| kill_locked(id.to_s, true, cancelled) }
|
|
158
167
|
@deferred.clear if parent_id.nil?
|
|
159
|
-
targets = parent_id ? @instances.values.select { |i| i.parent_id == parent_id.to_s } : @instances.values
|
|
160
|
-
targets.each(&:kill)
|
|
161
168
|
true
|
|
162
169
|
end
|
|
163
170
|
announce_cancellations(cancelled)
|
|
@@ -184,6 +191,17 @@ module Rubik
|
|
|
184
191
|
end
|
|
185
192
|
end
|
|
186
193
|
|
|
194
|
+
# Dispatched-work view for the goal-completion guard (issue #594):
|
|
195
|
+
# in_flight? units — active? (slot-holding) PLUS :budget_exhausted
|
|
196
|
+
# parked mid-task. Concurrency-slot accounting keeps active_count;
|
|
197
|
+
# the completion guard must not declare a goal achieved while a
|
|
198
|
+
# parked unit still owes a result.
|
|
199
|
+
def dispatched_count
|
|
200
|
+
@monitor.synchronize do
|
|
201
|
+
@instances.values.count(&:in_flight?)
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
187
205
|
def active_count
|
|
188
206
|
@monitor.synchronize do
|
|
189
207
|
@instances.values.count(&:active?)
|
|
@@ -13,10 +13,14 @@ module Rubik
|
|
|
13
13
|
# and a deterministic "call_3_1" WILL recur inside one history —
|
|
14
14
|
# duplicate tool_use ids break strict providers' tool_result
|
|
15
15
|
# pairing. The 4-digit random suffix is load-bearing, not decor.
|
|
16
|
-
# - Subagent StepRunner (deterministic):
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
16
|
+
# - Subagent StepRunner (deterministic): (subagent id, run, step,
|
|
17
|
+
# index) is unique by construction and determinism keeps ids
|
|
18
|
+
# greppable across subagents in transcripts. The run component is
|
|
19
|
+
# load-bearing (issue #593): Redeploy preserves @history and mints
|
|
20
|
+
# a fresh Budget, so step restarts at 0 while the OLD ids stay in
|
|
21
|
+
# the re-sent history — run_seq (incremented by every start_async,
|
|
22
|
+
# spawn and redeploy alike) is what keeps the redeployed run's ids
|
|
23
|
+
# disjoint from the preserved ones.
|
|
20
24
|
# - StepExecutor (unique: true): dead on the production path (the
|
|
21
25
|
# TurnExecutor normalizes ids before dispatch) but live on the
|
|
22
26
|
# executor's own seam — test doubles call it with bare, id-less tool
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "../tool_call_id"
|
|
4
|
+
require_relative "../tool_history"
|
|
4
5
|
|
|
5
6
|
module Rubik
|
|
6
7
|
module Orchestrator
|
|
@@ -32,10 +33,13 @@ module Rubik
|
|
|
32
33
|
end
|
|
33
34
|
|
|
34
35
|
# Subagent policy (ToolCallId doctrine): deterministic
|
|
35
|
-
# (subagent id, step, index)
|
|
36
|
-
#
|
|
36
|
+
# (subagent id, run, step, index). run_seq increments on every
|
|
37
|
+
# start_async — spawn AND redeploy alike — so a redeployed run's
|
|
38
|
+
# ids can never collide with the preserved history it re-sends to
|
|
39
|
+
# the provider (issue #593). Greppable ids still beat
|
|
40
|
+
# collision-proof ones here.
|
|
37
41
|
def mint_call_id(idx)
|
|
38
|
-
ToolCallId.mint(@inst.id, @inst.current_step, idx + 1)
|
|
42
|
+
ToolCallId.mint(@inst.id, @inst.run_seq, @inst.current_step, idx + 1)
|
|
39
43
|
end
|
|
40
44
|
|
|
41
45
|
# ── response shaping ──
|
|
@@ -90,15 +94,27 @@ module Rubik
|
|
|
90
94
|
@inst.history << { role: "assistant", content: final_text.empty? ? nil : final_text, tool_calls: tool_calls }
|
|
91
95
|
end
|
|
92
96
|
|
|
93
|
-
|
|
97
|
+
# The assistant row carrying the finish tool_use was already
|
|
98
|
+
# recorded; it needs a pairing tool-result row before
|
|
99
|
+
# complete_turn! or the preserved history hands strict providers
|
|
100
|
+
# an orphaned tool_use on the next request (issue #593 — mirrors
|
|
101
|
+
# StepExecutor#handle_finish_tool and the user channel's deferred
|
|
102
|
+
# finish path).
|
|
103
|
+
def on_finish(args, fallback_text, call_id = nil)
|
|
94
104
|
message = args["message"] || args[:message] || fallback_text || "Completed"
|
|
95
105
|
@inst.auto_apply_buffer!(:finish) unless @inst.workspace == "branch"
|
|
106
|
+
if call_id
|
|
107
|
+
Rubik::Orchestrator::ToolHistory.write(
|
|
108
|
+
@inst.history, tool_call_id: call_id, tool_name: "finish",
|
|
109
|
+
status: :ok, duration_ms: 0.0, content: message.to_s
|
|
110
|
+
)
|
|
111
|
+
end
|
|
96
112
|
@inst.complete_turn!(message)
|
|
97
113
|
{ ok: true, completed: true }
|
|
98
114
|
end
|
|
99
115
|
|
|
100
116
|
def execute_tool_call(tool_call, text)
|
|
101
|
-
return on_finish(tool_call["args"] || {}, text) if TurnExecutor.finish_call?(@inst.tool_registry, tool_call)
|
|
117
|
+
return on_finish(tool_call["args"] || {}, text, tool_call["id"]) if TurnExecutor.finish_call?(@inst.tool_registry, tool_call)
|
|
102
118
|
|
|
103
119
|
@inst.execute_subagent_tool_call(tool_call)
|
|
104
120
|
nil
|
|
@@ -55,9 +55,25 @@ module Rubik
|
|
|
55
55
|
# accumulated partial text and resets the buffer.
|
|
56
56
|
def final_text(res, text)
|
|
57
57
|
cm = @deps.loop_instance.continuation_manager
|
|
58
|
-
|
|
58
|
+
# `text` is the executor's already-sanitized (and stripped)
|
|
59
|
+
# response. Use it as-is for a normal turn; the stitching branch
|
|
60
|
+
# below sanitizes raw res.text separately (issue #547).
|
|
61
|
+
safe_text = Platform.safe_utf8(text).to_s
|
|
62
|
+
if cm.continuation_buffer.empty?
|
|
63
|
+
# A response without a pending truncation closes the consecutive
|
|
64
|
+
# truncation chain (issue #549). stitch_and_reset covers the other
|
|
65
|
+
# branch; this keeps the buffer itself intact.
|
|
66
|
+
cm.reset_strikes!
|
|
67
|
+
return safe_text
|
|
68
|
+
end
|
|
59
69
|
|
|
60
|
-
|
|
70
|
+
# Stitch the RAW response (sanitized): `text` was already stripped
|
|
71
|
+
# by TurnExecutor#call, which would eat the leading indentation of
|
|
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).
|
|
75
|
+
raw_text = res.respond_to?(:text) ? res.text : text
|
|
76
|
+
cm.stitch_and_reset(Platform.safe_utf8(raw_text).to_s, history: @deps.history)
|
|
61
77
|
end
|
|
62
78
|
|
|
63
79
|
def track_usage(res) = @deps.budget_manager.track_token_usage!(res)
|
|
@@ -66,13 +82,20 @@ module Rubik
|
|
|
66
82
|
# truncation, append the partial thought/text rows plus the
|
|
67
83
|
# targeted nudge, or return the diagnostic terminal when
|
|
68
84
|
# auto-continue is disabled or the ceiling is exhausted.
|
|
69
|
-
def handle_truncation(res,
|
|
85
|
+
def handle_truncation(res, text_resp, tool_calls, finish_reason)
|
|
70
86
|
cm = @deps.loop_instance.continuation_manager
|
|
71
87
|
return nil unless cm.truncation?(finish_reason)
|
|
72
88
|
|
|
89
|
+
# Sanitize the RAW response here rather than reusing the already
|
|
90
|
+
# stripped `text_resp`: the continuation buffer must keep the exact
|
|
91
|
+
# partial text (including its trailing newline) while invalid bytes
|
|
92
|
+
# echoed with finish_reason=length must not raise in
|
|
93
|
+
# ContinuationManager#classify_truncation (issue #547).
|
|
94
|
+
raw_text = res.respond_to?(:text) ? res.text : text_resp
|
|
95
|
+
safe_text = Platform.safe_utf8(raw_text).to_s
|
|
73
96
|
trunc_action = cm.handle_truncation(
|
|
74
97
|
finish_reason: finish_reason,
|
|
75
|
-
text_resp:
|
|
98
|
+
text_resp: safe_text,
|
|
76
99
|
tool_calls: tool_calls,
|
|
77
100
|
res: res,
|
|
78
101
|
max_tokens: @deps.max_output_tokens || @deps.safe_config("llm.default_max_output_tokens", 4096)
|
|
@@ -81,7 +104,8 @@ module Rubik
|
|
|
81
104
|
if trunc_action[:continue]
|
|
82
105
|
nudge_policy.reset!
|
|
83
106
|
if trunc_action[:thought]
|
|
84
|
-
|
|
107
|
+
thought = Platform.safe_utf8(trunc_action[:thought])
|
|
108
|
+
@deps.history << { role: "assistant", content: nil, thought: thought }
|
|
85
109
|
elsif trunc_action[:partial_text]
|
|
86
110
|
@deps.history << { role: "assistant", content: trunc_action[:partial_text] }
|
|
87
111
|
end
|
|
@@ -91,6 +115,17 @@ module Rubik
|
|
|
91
115
|
end
|
|
92
116
|
return nil unless trunc_action[:error]
|
|
93
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
|
+
|
|
94
129
|
@deps.on_step&.call(type: :error, content: trunc_action[:error], step: @deps.step_count, max_steps: @deps.max_steps)
|
|
95
130
|
@deps.loop_instance.turn_result(success: false, error: trunc_action[:error])
|
|
96
131
|
end
|
|
@@ -223,11 +223,14 @@ module Rubik
|
|
|
223
223
|
|
|
224
224
|
def tool_turn(res, text_resp, tool_calls)
|
|
225
225
|
channel.nudge_policy.reset!
|
|
226
|
-
|
|
226
|
+
# One stitched text for the assistant row AND tool dispatch, so a
|
|
227
|
+
# bare finish after truncation sees the partial (issue #547).
|
|
228
|
+
final = channel.final_text(res, text_resp)
|
|
229
|
+
channel.append_tool_call_row(final, tool_calls)
|
|
227
230
|
streamed = res.respond_to?(:streamed) && res.streamed == true
|
|
228
231
|
channel.announce_llm_thought(text_resp, streamed: streamed) if !text_resp.empty? && tool_calls.none? { |tc| self.class.finish_call?(channel.tool_registry, tc) }
|
|
229
232
|
|
|
230
|
-
tool_calls.
|
|
233
|
+
tool_calls.each_with_index do |tc, idx|
|
|
231
234
|
# finish is exempt (pinned by test_orchestrator_stream): its args
|
|
232
235
|
# are optional and the fallback text carries the message, so a
|
|
233
236
|
# malformed finish must still complete the turn gracefully instead
|
|
@@ -236,8 +239,12 @@ module Rubik
|
|
|
236
239
|
channel.malformed_args_row(tc)
|
|
237
240
|
next
|
|
238
241
|
end
|
|
239
|
-
result = channel.execute_tool_call(tc,
|
|
240
|
-
|
|
242
|
+
result = channel.execute_tool_call(tc, final)
|
|
243
|
+
next unless result
|
|
244
|
+
|
|
245
|
+
# Pair calls that follow a terminal one (issue #548).
|
|
246
|
+
tool_calls[(idx + 1)..]&.each { |later| channel.pair_skipped_tool_call(later) }
|
|
247
|
+
return result
|
|
241
248
|
end
|
|
242
249
|
nil
|
|
243
250
|
end
|
|
@@ -335,10 +342,19 @@ module Rubik
|
|
|
335
342
|
raise NotImplementedError, "channel must dispatch tool calls"
|
|
336
343
|
end
|
|
337
344
|
|
|
338
|
-
#
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
345
|
+
# Pairing row for a skipped tool_use id (issue #548).
|
|
346
|
+
def pair_skipped_tool_call(tool_call)
|
|
347
|
+
ToolHistory.write(history,
|
|
348
|
+
tool_call_id: tool_call["id"],
|
|
349
|
+
tool_name: tool_call["tool"].to_s,
|
|
350
|
+
status: :error,
|
|
351
|
+
duration_ms: 0.0,
|
|
352
|
+
content: "Tool call was not executed: the turn completed on an earlier tool call.")
|
|
353
|
+
nil
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
# Canonical error row for unparsable String args (issue #55 3.4):
|
|
357
|
+
# echo the rejected payload, keep the tool_call_id paired.
|
|
342
358
|
def malformed_args_row(tool_call)
|
|
343
359
|
ToolHistory.write(history,
|
|
344
360
|
tool_call_id: tool_call["id"],
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
3
5
|
module Rubik
|
|
4
6
|
module Platform
|
|
5
7
|
# File system operations (atomic writes, newline normalization).
|
|
@@ -12,36 +14,62 @@ module Rubik
|
|
|
12
14
|
def atomic_write(target_path, content)
|
|
13
15
|
dir = File.dirname(target_path)
|
|
14
16
|
FileUtils.mkdir_p(dir) unless File.directory?(dir)
|
|
15
|
-
tmp_file = "#{target_path}.tmp.#{::Process.pid}.#{rand(1_000_000)}"
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
# Preserve the target's permission bits (issue #543): File.open creates
|
|
19
|
+
# a 0644 temp file and rename keeps the temp's mode, so overwriting a
|
|
20
|
+
# 0600 secret would expose it and a 0755 script would lose +x. Capture
|
|
21
|
+
# the mode before writing and carry it onto the temp file before rename.
|
|
22
|
+
existing_mode = begin
|
|
23
|
+
stat = File.stat(target_path)
|
|
24
|
+
stat.mode & 0o7777 if stat.file?
|
|
25
|
+
rescue Errno::ENOENT
|
|
26
|
+
nil
|
|
25
27
|
end
|
|
26
28
|
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
|
|
29
|
+
# O_EXCL refuses a pre-placed path (including a symlink) instead of
|
|
30
|
+
# following it, and the random suffix makes the temp name unpredictable.
|
|
31
|
+
# Together they close the TOCTOU/symlink redirection of issue #543.
|
|
32
|
+
tmp_file = "#{target_path}.tmp.#{::Process.pid}.#{SecureRandom.hex(12)}"
|
|
33
|
+
created = false
|
|
34
|
+
|
|
30
35
|
begin
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
# Mode 0o600 keeps the in-flight temp private until the final chmod.
|
|
37
|
+
File.open(tmp_file, File::WRONLY | File::CREAT | File::EXCL | File::BINARY, 0o600) do |f|
|
|
38
|
+
created = true
|
|
39
|
+
f.write(content)
|
|
40
|
+
f.flush
|
|
41
|
+
begin
|
|
42
|
+
f.fsync
|
|
43
|
+
rescue StandardError, ScriptError
|
|
44
|
+
nil
|
|
45
|
+
end
|
|
37
46
|
end
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
47
|
+
|
|
48
|
+
File.chmod(existing_mode, tmp_file) if existing_mode
|
|
49
|
+
|
|
50
|
+
# Atomic replace: rename tmp_file -> target_path without unlinking first
|
|
51
|
+
# (closing TOCTOU). Retry for Windows transient locks.
|
|
52
|
+
retries = 3
|
|
41
53
|
begin
|
|
42
|
-
|
|
43
|
-
rescue
|
|
44
|
-
|
|
54
|
+
File.rename(tmp_file, target_path)
|
|
55
|
+
rescue SystemCallError => e
|
|
56
|
+
if retries.positive?
|
|
57
|
+
retries -= 1
|
|
58
|
+
sleep 0.02
|
|
59
|
+
retry
|
|
60
|
+
end
|
|
61
|
+
debug_warn("atomic_write failed for #{target_path}: #{e.class}: #{e.message}")
|
|
62
|
+
raise
|
|
63
|
+
end
|
|
64
|
+
ensure
|
|
65
|
+
# Only remove the temp file we created; never delete a pre-placed
|
|
66
|
+
# path (e.g. an attacker's symlink) that made O_EXCL fail.
|
|
67
|
+
if created
|
|
68
|
+
begin
|
|
69
|
+
FileUtils.rm_f(tmp_file)
|
|
70
|
+
rescue StandardError, ScriptError
|
|
71
|
+
nil
|
|
72
|
+
end
|
|
45
73
|
end
|
|
46
74
|
end
|
|
47
75
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
|