rubik 0.9.0 → 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.
- checksums.yaml +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +250 -43
- data/README.md +5 -1
- data/lib/rubik/adapter/base.rb +21 -1
- data/lib/rubik/adapter/generic.rb +0 -14
- data/lib/rubik/adapter/node.rb +2 -0
- data/lib/rubik/adapter/process.rb +28 -67
- data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
- data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
- data/lib/rubik/buffer/apply_contract.rb +3 -3
- data/lib/rubik/buffer/apply_transaction.rb +165 -0
- data/lib/rubik/buffer/manager.rb +26 -120
- data/lib/rubik/cli/workspace_trust.rb +16 -0
- data/lib/rubik/config/deep_ops.rb +4 -1
- data/lib/rubik/config/defaults.rb +29 -3
- data/lib/rubik/config/schema.rb +8 -0
- data/lib/rubik/config/secret_policy.rb +197 -0
- data/lib/rubik/config/store.rb +14 -0
- data/lib/rubik/config.rb +11 -0
- data/lib/rubik/diagnostics/collector.rb +0 -9
- data/lib/rubik/errors.rb +15 -0
- data/lib/rubik/event_bus.rb +2 -1
- data/lib/rubik/kernel/dispatcher.rb +16 -11
- data/lib/rubik/kernel/unit.rb +4 -1
- data/lib/rubik/llm/registry.rb +12 -9
- data/lib/rubik/llm/router.rb +2 -5
- data/lib/rubik/log.rb +23 -1
- data/lib/rubik/lsp/client.rb +152 -445
- data/lib/rubik/lsp/document_store.rb +209 -0
- data/lib/rubik/lsp/manager.rb +14 -10
- data/lib/rubik/lsp/transport.rb +365 -0
- data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
- data/lib/rubik/mcp/boot_run.rb +190 -0
- data/lib/rubik/mcp/client.rb +4 -3
- data/lib/rubik/mcp/manager.rb +46 -165
- data/lib/rubik/mcp/transport/stdio.rb +55 -10
- data/lib/rubik/orchestrator/auto_apply.rb +4 -3
- data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
- data/lib/rubik/orchestrator/goal_session.rb +519 -0
- data/lib/rubik/orchestrator/loop.rb +28 -47
- data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
- data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
- data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
- data/lib/rubik/orchestrator/resume_window.rb +96 -0
- data/lib/rubik/orchestrator/shell_deps.rb +16 -0
- data/lib/rubik/orchestrator/stream_manager.rb +2 -1
- data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
- data/lib/rubik/orchestrator/subagent/instance.rb +11 -12
- data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
- data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
- data/lib/rubik/orchestrator/subagent/questioner.rb +24 -7
- data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
- data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
- data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
- data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
- data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
- data/lib/rubik/orchestrator/subagent/step_runner.rb +11 -3
- data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
- data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
- data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
- data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
- data/lib/rubik/orchestrator.rb +1 -0
- data/lib/rubik/platform/io_drain.rb +2 -1
- data/lib/rubik/platform/process.rb +144 -54
- data/lib/rubik/platform.rb +17 -1
- data/lib/rubik/plugin/manager.rb +2 -1
- data/lib/rubik/plugin/storage.rb +20 -29
- data/lib/rubik/repl/commands/agent_context.rb +9 -8
- data/lib/rubik/repl/commands/agent_execution.rb +20 -502
- data/lib/rubik/repl/commands/agent_models.rb +4 -3
- data/lib/rubik/repl/commands/agent_providers.rb +43 -2
- data/lib/rubik/repl/commands/base.rb +47 -2
- data/lib/rubik/repl/commands/buffer.rb +6 -5
- data/lib/rubik/repl/commands/lsp.rb +3 -2
- data/lib/rubik/repl/commands/pruner.rb +2 -1
- data/lib/rubik/repl/commands/secrets.rb +151 -0
- data/lib/rubik/repl/commands/workspace.rb +60 -150
- data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
- data/lib/rubik/repl/config_wiring.rb +3 -3
- data/lib/rubik/repl/hooks.rb +2 -1
- data/lib/rubik/repl/input_execution.rb +158 -76
- data/lib/rubik/repl/input_loop.rb +3 -2
- data/lib/rubik/repl/lifecycle.rb +82 -204
- data/lib/rubik/repl/notice_router.rb +319 -0
- data/lib/rubik/repl/paste_support.rb +3 -1
- data/lib/rubik/repl/presentation.rb +5 -3
- data/lib/rubik/repl/pruner/gate.rb +3 -1
- data/lib/rubik/repl/pruner/harness.rb +2 -1
- data/lib/rubik/repl/shell.rb +4 -3
- data/lib/rubik/repl/turn_recorder.rb +6 -5
- data/lib/rubik/runtime/discovery.rb +3 -2
- data/lib/rubik/runtime/loop_service.rb +4 -3
- data/lib/rubik/runtime/manifest_store.rb +124 -0
- data/lib/rubik/runtime/overlay_guard.rb +127 -0
- data/lib/rubik/runtime/overlay_storage.rb +269 -272
- data/lib/rubik/runtime/task_manager.rb +71 -27
- data/lib/rubik/runtime.rb +1 -0
- data/lib/rubik/sandbox/driver/docker.rb +5 -1
- data/lib/rubik/sandbox/runner.rb +16 -15
- data/lib/rubik/security/allowlist.rb +167 -0
- data/lib/rubik/security/audit_log.rb +72 -0
- data/lib/rubik/security/classifier/classifier.rb +8 -0
- data/lib/rubik/security/command_gate.rb +591 -0
- data/lib/rubik/security/guard.rb +94 -1077
- data/lib/rubik/security/path_policy.rb +117 -0
- data/lib/rubik/security/secret_store.rb +463 -0
- data/lib/rubik/security/synthesis_gate.rb +297 -0
- data/lib/rubik/security.rb +5 -0
- data/lib/rubik/session/manager.rb +7 -1
- data/lib/rubik/skills/manager.rb +2 -3
- data/lib/rubik/storage/ranking.rb +113 -0
- data/lib/rubik/storage/search.rb +49 -0
- data/lib/rubik/storage/store.rb +15 -112
- data/lib/rubik/storage/transcript_indexer.rb +2 -3
- data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
- data/lib/rubik/tools/base.rb +22 -0
- data/lib/rubik/tools/builtins/files.rb +3 -15
- data/lib/rubik/tools/builtins/grep.rb +3 -9
- data/lib/rubik/tools/builtins/lifecycle.rb +96 -92
- data/lib/rubik/tools/builtins/synthesis.rb +3 -0
- data/lib/rubik/tools/lifecycle_rollbacks.rb +100 -0
- data/lib/rubik/tools/native_search.rb +2 -1
- data/lib/rubik/tools/truncator.rb +73 -36
- data/lib/rubik/ui/banner.rb +4 -3
- data/lib/rubik/ui/spinner.rb +3 -1
- data/lib/rubik/ui/statusline.rb +6 -5
- data/lib/rubik/version.rb +1 -1
- data/lib/rubik.rb +1 -2
- metadata +23 -1
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
require_relative "../log"
|
|
7
|
+
require_relative "../storage/layout"
|
|
8
|
+
require_relative "../platform"
|
|
9
|
+
require_relative "goal_state"
|
|
10
|
+
require_relative "goal_registry"
|
|
11
|
+
require_relative "goal_ledger"
|
|
12
|
+
|
|
13
|
+
module Rubik
|
|
14
|
+
module Orchestrator
|
|
15
|
+
# Goal-session domain (issue #404): the ONE home for the !goal run
|
|
16
|
+
# lifecycle — start (execute_goal), the shared turn driver
|
|
17
|
+
# (drive_goal_turn: budget extensions, the single error channel,
|
|
18
|
+
# terminal mapping), suspension plus both resume paths (in-process
|
|
19
|
+
# loop revival and cross-process Σ-window rehydration), the suspended
|
|
20
|
+
# -goal registry plumbing and the `!goal list` surface. Moved verbatim
|
|
21
|
+
# from REPL::Commands::AgentExecution; the command block keeps only
|
|
22
|
+
# argument parsing and delegates here.
|
|
23
|
+
#
|
|
24
|
+
# owns : goal start/suspend/resume/fail/complete choreography, the
|
|
25
|
+
# GoalState + Σ ledger + registry provide discipline, the
|
|
26
|
+
# honest-continuity output strings, the documented ordering
|
|
27
|
+
# contracts (ledger_id-before-suspend!, suspend_stamp_meta
|
|
28
|
+
# schema), the shared resume tail (resume_drive) and the
|
|
29
|
+
# goal-shaped orchestrator-loop stanza (build_goal_orchestrator)
|
|
30
|
+
# leaves : command argument parsing and usage rendering
|
|
31
|
+
# (REPL::Commands::AgentExecution), the Loop machinery
|
|
32
|
+
# itself (Loop/GoalWait), and input_execution's streaming
|
|
33
|
+
# on_step stanza
|
|
34
|
+
module GoalSession
|
|
35
|
+
module_function
|
|
36
|
+
|
|
37
|
+
def initialize_goal_ledger(ctx, goal_text)
|
|
38
|
+
return nil unless Rubik::Orchestrator::GoalLedger::Ledger.enabled?(ctx.config)
|
|
39
|
+
|
|
40
|
+
store = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::STORAGE_STORE)
|
|
41
|
+
dmesg = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::DMESG)
|
|
42
|
+
goal_ledger = Rubik::Orchestrator::GoalLedger::Ledger.create!(
|
|
43
|
+
goal_text: goal_text, config: ctx.config, store: store, dmesg: dmesg,
|
|
44
|
+
event_bus: ctx.event_bus
|
|
45
|
+
)
|
|
46
|
+
ctx.runtime_manager.provide(Rubik::Coeffects::GOAL_LEDGER, goal_ledger)
|
|
47
|
+
goal_ledger
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def execute_goal(ctx, goal_text, custom_steps)
|
|
51
|
+
ctx.recorder&.record_user_turn("!goal #{goal_text}")
|
|
52
|
+
goal_steps = custom_steps || Rubik::Storage::Layout.config_get(ctx.config, "agent.goal_max_steps", 100).to_i
|
|
53
|
+
routed = ctx.router.route(task_type: :goal, prompt: goal_text)
|
|
54
|
+
ctx.runtime_manager.provide(:llm_client, routed[:provider]) if routed[:provider]
|
|
55
|
+
|
|
56
|
+
# Canlı hedef durumu: maestro ve alt ajanlar read_file
|
|
57
|
+
# rubik://goal/state ile aynı anlık görüntüyü okur. Provide swap
|
|
58
|
+
# semantiğine sahiptir — tekrarlanan !goal koşuları Ω'ya inverse
|
|
59
|
+
# yığmaz; session dispose'u tek delete-inverse ile temizler.
|
|
60
|
+
# event_bus: bitiş olayı (GoalState::FINISH_EVENT) Vakanüvis
|
|
61
|
+
# kronikçisinin goal kancasına akar (K3) — notice/wake yok.
|
|
62
|
+
goal_state = Rubik::Orchestrator::GoalState.new(event_bus: ctx.event_bus)
|
|
63
|
+
goal_state.start!(goal_text, goal_steps)
|
|
64
|
+
ctx.runtime_manager.provide(Rubik::Coeffects::GOAL_STATE, goal_state)
|
|
65
|
+
|
|
66
|
+
# Yapılandırılmış Σ defteri (goal-ledger planı A): knob ON iken
|
|
67
|
+
# koşuyla doğar, terminal dallarda finalize olur, provide swap
|
|
68
|
+
# semantiğiyle tekrarlanan !goal koşuları Ω'ya inverse yığmaz.
|
|
69
|
+
# OFF iken hiçbir yüz kurulmaz (araç/URI/prompt bayt-özdeş).
|
|
70
|
+
goal_ledger = initialize_goal_ledger(ctx, goal_text)
|
|
71
|
+
|
|
72
|
+
# Askı kimliği (issue #106): koşu boyunca SABİT — registry
|
|
73
|
+
# girdisinin anahtarı ve Σ damgası meta'sının goal_id'si.
|
|
74
|
+
# (Σ satır id'si her kabulde değişir; kimlik olarak kullanılamaz.)
|
|
75
|
+
goal_id = "goal-#{Process.pid}-#{SecureRandom.hex(4)}"
|
|
76
|
+
|
|
77
|
+
ctx.out.puts "[Rubik::Goal] Starting autonomous goal loop (Budget: #{goal_steps} steps) with #{routed[:role]} model (#{routed[:model]})..."
|
|
78
|
+
ctx.out.puts "[Rubik::Goal] Target: #{goal_text}\n"
|
|
79
|
+
|
|
80
|
+
orchestrator = build_goal_orchestrator(ctx, goal_state: goal_state, routed: routed, steps: goal_steps)
|
|
81
|
+
|
|
82
|
+
# Plan 53 D-8 yardımcısı: nötr çift + kayıtçı eşlemesi tek yerde.
|
|
83
|
+
# Session restore kayıtçı OLAYLARINDAN hidrate eder — eşlenmiş
|
|
84
|
+
# restore için conversation_history VE record_assistant_response
|
|
85
|
+
# birlikte yazılmalı (başarısızlıkta da).
|
|
86
|
+
record_goal_pair = build_goal_record_pair(ctx, goal_text)
|
|
87
|
+
|
|
88
|
+
# Pruner apply seam: hedef döngüsü de aynı sınırda tohumlanır —
|
|
89
|
+
# aşamalı budama planı varsa goal promptundan ÖNCE uygulanır.
|
|
90
|
+
ctx.apply_staged_prune_plan!
|
|
91
|
+
|
|
92
|
+
drive_goal_turn(ctx, orchestrator: orchestrator, goal_state: goal_state,
|
|
93
|
+
goal_ledger: goal_ledger, goal_text: goal_text, goal_id: goal_id,
|
|
94
|
+
record_pair: record_goal_pair,
|
|
95
|
+
start: ->(loop_inst) { loop_inst.run(goal_text, goal_mode: true, conversation_history: ctx.conversation_history) })
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# ------------------------------------------------------------------
|
|
99
|
+
# !goal sürücüsü (issue #106): koşu girişi + bütçe-soru döngüsü +
|
|
100
|
+
# tek hata kanalı + terminal eşlemesi — start ve resume yollarının
|
|
101
|
+
# ORTAK gövdesi. start callable'ı yolu seçer:
|
|
102
|
+
# başlangıç → loop.run (taze koşu)
|
|
103
|
+
# süreç-içi resume → loop.resume! (history prefix nesne-özdeş)
|
|
104
|
+
# çapraz-süreç resume → loop.resume_from (Σ penceresi)
|
|
105
|
+
# Askı dalı (#106): SADECE transient ClientError SUSPENDED'a gider;
|
|
106
|
+
# kalıcı hatalar (moderasyon/401/read-timeout) bugünkü fail_goal
|
|
107
|
+
# çıktısını bayt-baytına korur.
|
|
108
|
+
def drive_goal_turn(ctx, orchestrator:, goal_state:, goal_ledger:,
|
|
109
|
+
goal_text:, goal_id:, record_pair:, start:)
|
|
110
|
+
ctx.active_loop = orchestrator
|
|
111
|
+
begin
|
|
112
|
+
res = start.call(orchestrator)
|
|
113
|
+
res = handle_goal_budget_extensions(ctx, orchestrator: orchestrator, goal_state: goal_state, res: res)
|
|
114
|
+
rescue Interrupt
|
|
115
|
+
# ^C hedefi 'running' bırakmasın; eşlenmiş kayıt otursun. Interrupt
|
|
116
|
+
# yeniden yükselir — kullanıcı mesajını execute_command basar.
|
|
117
|
+
# Resume üstüne ^C = askıyı terk (terminal :stopped damgası).
|
|
118
|
+
goal_state.stop!("Interrupted by user (^C)")
|
|
119
|
+
finalize_goal_ledger(goal_ledger, goal_state, :stopped)
|
|
120
|
+
forget_suspended_goal(ctx, goal_id)
|
|
121
|
+
record_pair.call("[Goal attempt interrupted after #{goal_state.step_count} steps]")
|
|
122
|
+
raise
|
|
123
|
+
rescue Rubik::LLM::ClientError => e
|
|
124
|
+
# Tek hata kanalı (#106 uzantısı): transient transport hatası
|
|
125
|
+
# (RETRYABLE tükenmesi, tükenmiş 5xx, 429) goal'u SUSPENDED'a
|
|
126
|
+
# götürür — loop/counters/Σ korunur, REPL yaşar. Kalıcı hata
|
|
127
|
+
# (#105 çıktısı değişmeden) terminal fail. Yeniden yükseltmez —
|
|
128
|
+
# spinner temizliği dispatch katmanına (execute_command) aittir.
|
|
129
|
+
# next yerine return: rescue sonrası res'siz terminal
|
|
130
|
+
# eşlemesine düşülmesin.
|
|
131
|
+
if e.respond_to?(:transient?) && e.transient?
|
|
132
|
+
suspend_goal_turn(ctx, orchestrator: orchestrator, goal_state: goal_state, goal_ledger: goal_ledger,
|
|
133
|
+
goal_text: goal_text, goal_id: goal_id, record_pair: record_pair, reason: e.message,
|
|
134
|
+
tokens: orchestrator.total_tokens)
|
|
135
|
+
else
|
|
136
|
+
fail_goal_turn(ctx, goal_state: goal_state, goal_ledger: goal_ledger, goal_id: goal_id,
|
|
137
|
+
record_pair: record_pair, reason: e.message)
|
|
138
|
+
end
|
|
139
|
+
return
|
|
140
|
+
rescue StandardError => e
|
|
141
|
+
fail_goal_turn(ctx, goal_state: goal_state, goal_ledger: goal_ledger, goal_id: goal_id,
|
|
142
|
+
record_pair: record_pair, reason: "#{e.class}: #{e.message}")
|
|
143
|
+
return
|
|
144
|
+
ensure
|
|
145
|
+
ctx.active_loop = nil
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
handle_goal_completion(ctx, res: res, goal_state: goal_state, goal_ledger: goal_ledger,
|
|
149
|
+
goal_id: goal_id, record_pair: record_pair)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def finalize_goal_ledger(goal_ledger, goal_state, status)
|
|
153
|
+
return unless goal_ledger
|
|
154
|
+
|
|
155
|
+
goal_ledger.finalize!(status)
|
|
156
|
+
goal_state.ledger_id = goal_ledger.snapshot[:storage_id]
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def fail_goal_turn(ctx, goal_state:, goal_ledger:, goal_id:, record_pair:,
|
|
160
|
+
reason:, tokens: nil, steps: nil)
|
|
161
|
+
sanitized = Rubik::Platform.safe_utf8(reason.to_s)
|
|
162
|
+
steps_used = steps || goal_state.step_count
|
|
163
|
+
goal_state.fail!(sanitized, tokens: tokens)
|
|
164
|
+
finalize_goal_ledger(goal_ledger, goal_state, :failed)
|
|
165
|
+
forget_suspended_goal(ctx, goal_id)
|
|
166
|
+
ctx.out.puts "\n❌ Goal could not be completed: #{sanitized}"
|
|
167
|
+
record_pair.call("[Goal attempt ended after #{steps_used} steps: #{sanitized}]", tokens: tokens)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def suspend_goal_turn(ctx, orchestrator:, goal_state:, goal_ledger:,
|
|
171
|
+
goal_text:, goal_id:, record_pair:, reason:, tokens: nil)
|
|
172
|
+
attempt = suspended_attempt(ctx, goal_id) + 1
|
|
173
|
+
# Ledger kimliği askıdan ÖNCE okunur: suspend! → ledger.suspend!
|
|
174
|
+
# sırası metanın bu turun token'larını içermesi için zorunlu,
|
|
175
|
+
# ama o damga storage_id'yi değiştirdiği için sonradan atanan id
|
|
176
|
+
# SUSPEND_EVENT'e geç kalırdı — ilk askıda kronik satırı slug
|
|
177
|
+
# kimliğine düşer, ledger= bağlantısı kaybolurdu.
|
|
178
|
+
# resume_cross_process ile aynı uçuş-içi atama önceliği.
|
|
179
|
+
goal_state.ledger_id = goal_ledger.snapshot[:storage_id] if goal_ledger
|
|
180
|
+
goal_state.suspend!(reason, tokens: tokens)
|
|
181
|
+
goal_ledger&.suspend!(suspend_stamp_meta(goal_state, goal_id, reason, attempt))
|
|
182
|
+
registry = goal_registry_for(ctx)
|
|
183
|
+
registry.register(Rubik::Orchestrator::GoalRegistry::Entry.new(
|
|
184
|
+
goal_id: goal_id, goal_text: goal_text, loop: orchestrator,
|
|
185
|
+
goal_state: goal_state, ledger: goal_ledger, reason: reason.to_s,
|
|
186
|
+
suspended_at: Time.now, attempt: attempt
|
|
187
|
+
))
|
|
188
|
+
ctx.out.puts "\n⏸️ [Rubik::Goal] Suspended after #{goal_state.step_count} steps (transient provider error): #{reason}"
|
|
189
|
+
ctx.out.puts " Loop, counters and Σ ledger are preserved — resume with `!goal resume`."
|
|
190
|
+
record_pair.call("[Goal suspended after #{goal_state.step_count} steps: #{reason}]", tokens: tokens)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def handle_goal_budget_extensions(ctx, orchestrator:, goal_state:, res:)
|
|
194
|
+
# Bütçe tükenince sor (execute_free_text simetrisi; aynı knob,
|
|
195
|
+
# aynı +20). continue_loop boyunca active_loop DOLU kalmalı:
|
|
196
|
+
# notice yönlendirmesi aktif döngüye bakar (lifecycle.rb
|
|
197
|
+
# route_subagent_notice) — soruya beklerken gelen subagent
|
|
198
|
+
# olayları loop inbox'a girer ve devam adımında boşaltılır.
|
|
199
|
+
auto_extensions = 0
|
|
200
|
+
max_auto = Rubik::Storage::Layout.config_get(ctx.config, "agent.goal_auto_continue_limit", 0).to_i
|
|
201
|
+
# Yapılandırılmış neden (loop.rb terminali): insan-okur string
|
|
202
|
+
# yalnız gösterim içindir — otomasyon prozu parse etmesin.
|
|
203
|
+
while !res[:ok] && res[:reason] == :budget_exhausted
|
|
204
|
+
if auto_extensions < max_auto
|
|
205
|
+
auto_extensions += 1
|
|
206
|
+
ctx.out.puts "\n🤖 [Rubik::Goal] Adım bütçesi doldu (#{res[:error]}). " \
|
|
207
|
+
"Otonom devam hakkı (#{auto_extensions}/#{max_auto}) kullanılarak +20 adım ekleniyor..."
|
|
208
|
+
goal_state.extend!(goal_state.max_steps + 20)
|
|
209
|
+
res = orchestrator.continue_loop(20, auto: true)
|
|
210
|
+
next
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
break unless Rubik::Storage::Layout.config_get(ctx.config, "agent.ask_continue_on_limit", true)
|
|
214
|
+
|
|
215
|
+
ctx.out.puts "\n⚠️ [Rubik::Goal] #{res[:error]}"
|
|
216
|
+
ans = ctx.ask_yes_no(" [Rubik::Goal] +20 adım ile devam edilsin mi?", default: "n")
|
|
217
|
+
break unless ["y", "yes", "evet"].include?(ans)
|
|
218
|
+
|
|
219
|
+
ctx.out.puts " [Rubik::Goal] Adım bütçesi +20 artırılıyor ve hedef sürdürülüyor..."
|
|
220
|
+
goal_state.extend!(goal_state.max_steps + 20)
|
|
221
|
+
res = orchestrator.continue_loop(20)
|
|
222
|
+
end
|
|
223
|
+
res
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def handle_goal_completion(ctx, res:, goal_state:, goal_ledger:,
|
|
227
|
+
goal_id:, record_pair:)
|
|
228
|
+
if res[:ok]
|
|
229
|
+
active = goal_active_subagent_count(ctx)
|
|
230
|
+
if active.positive?
|
|
231
|
+
# Defense behind the loop-level goal guard (GoalWait): a
|
|
232
|
+
# success terminal escaping with live subagents means the
|
|
233
|
+
# pipeline is mid-flight — an honest stop beats a false
|
|
234
|
+
# "Goal Achieved" (field incident 2026-10-05).
|
|
235
|
+
reason = "Goal loop concluded with #{active} active subagent(s) still running — not marked complete"
|
|
236
|
+
goal_state.stop!(reason)
|
|
237
|
+
finalize_goal_ledger(goal_ledger, goal_state, :stopped)
|
|
238
|
+
ctx.out.puts "\n⚠️ [Rubik::Goal] #{reason}."
|
|
239
|
+
record_pair.call("[Goal attempt ended after #{goal_state.step_count} steps: #{reason}]", tokens: res[:tokens])
|
|
240
|
+
else
|
|
241
|
+
goal_state.complete!(res[:output].to_s, tokens: res[:tokens])
|
|
242
|
+
finalize_goal_ledger(goal_ledger, goal_state, :completed)
|
|
243
|
+
forget_suspended_goal(ctx, goal_id)
|
|
244
|
+
ctx.out.puts "\n🎉 Goal Achieved in #{res[:steps]} steps!"
|
|
245
|
+
ctx.out.puts res[:output]
|
|
246
|
+
# Keep the bang syntax out of the LLM context (Plan 53 D-8): the
|
|
247
|
+
# session recorder already stores the literal "!goal ..." for replay.
|
|
248
|
+
record_pair.call(res[:output].to_s, streamed: res[:streamed] == true, tokens: res[:tokens])
|
|
249
|
+
|
|
250
|
+
ctx.out.puts "\nTip: Use `!diff` to review changes and `!apply` to commit to disk." if ctx.buffer_manager.dirty?
|
|
251
|
+
end
|
|
252
|
+
else
|
|
253
|
+
# Başarısızlık eşlemesi yukarıdaki tek hata kanalından akar:
|
|
254
|
+
# GoalState → ledger finalize → kayıtçı çifti, istisna yoluyla
|
|
255
|
+
# aynı sırayla.
|
|
256
|
+
fail_goal_turn(ctx, goal_state: goal_state, goal_ledger: goal_ledger, goal_id: goal_id,
|
|
257
|
+
record_pair: record_pair, reason: res[:error], tokens: res[:tokens], steps: res[:steps])
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# ------------------------------------------------------------------
|
|
262
|
+
# !goal resume (issue #106). İki yol, dönüşüm sırasına göre:
|
|
263
|
+
# 1) Oturum kaydında canlı girdi varsa süreç-içi resume — hayatta
|
|
264
|
+
# kalan Loop örneği canlandırılır (subagent konuşmaları gerçekten
|
|
265
|
+
# süreklidir; süpervizör sürecin yaşıyor).
|
|
266
|
+
# 2) Yoksa çapraz-süreç Σ-only resume — storage'daki "suspended"
|
|
267
|
+
# satırından rehidrasyon. Alt ajan konuşmaları süreçle ölür;
|
|
268
|
+
# çıktı bunu tek cümleyle dürüstçe söyler.
|
|
269
|
+
def resume_suspended_goal(ctx)
|
|
270
|
+
registry = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::GOAL_REGISTRY)
|
|
271
|
+
entry = registry.latest if registry.is_a?(Rubik::Orchestrator::GoalRegistry)
|
|
272
|
+
entry ? resume_in_process(ctx, entry) : resume_cross_process(ctx)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def resume_in_process(ctx, entry)
|
|
276
|
+
ctx.recorder&.record_user_turn("!goal resume")
|
|
277
|
+
# Kaynaklama (issue #454): canlı girdiden; resume! geçişi (SUSPENDED → RUNNING) kaynaklamanın parçası.
|
|
278
|
+
goal_state = entry.goal_state
|
|
279
|
+
goal_ledger = entry.ledger
|
|
280
|
+
goal_state.resume!
|
|
281
|
+
ctx.out.puts "[Rubik::Goal] Resuming '#{entry.goal_text}' (attempt #{entry.attempt + 1}, " \
|
|
282
|
+
"suspended: #{entry.reason})"
|
|
283
|
+
# Dürüst süreklilik: alt ajan konuşmaları süreç-içi resume'da yaşar — çapraz-süreç
|
|
284
|
+
# yoldaki tek cümlelik uyarı burada YOKTUR; ortak kuyruk resume_drive'da.
|
|
285
|
+
resume_drive(ctx, orchestrator: entry.loop, goal_state: goal_state, ledger: goal_ledger,
|
|
286
|
+
goal_text: entry.goal_text, goal_id: entry.goal_id,
|
|
287
|
+
start: lambda { |loop_inst|
|
|
288
|
+
loop_inst.resume!(reason: entry.reason,
|
|
289
|
+
suspended_at: entry.suspended_at,
|
|
290
|
+
attempt: entry.attempt)
|
|
291
|
+
})
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def resume_cross_process(ctx)
|
|
295
|
+
ctx.recorder&.record_user_turn("!goal resume")
|
|
296
|
+
store = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::STORAGE_STORE)
|
|
297
|
+
unless store
|
|
298
|
+
ctx.out.puts "ℹ️ [Rubik::Goal] No suspended goal in this session; storage unavailable — " \
|
|
299
|
+
"cross-process resume honestly refused."
|
|
300
|
+
return
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
row = suspended_rows(store).last
|
|
304
|
+
unless row
|
|
305
|
+
ctx.out.puts "ℹ️ [Rubik::Goal] No suspended goal found (session registry empty, no 'suspended' Σ rows)."
|
|
306
|
+
return
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
dmesg = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::DMESG)
|
|
310
|
+
ledger = Rubik::Orchestrator::GoalLedger::Ledger.load(store: store, storage_id: row.id,
|
|
311
|
+
config: ctx.config, dmesg: dmesg,
|
|
312
|
+
event_bus: ctx.event_bus)
|
|
313
|
+
unless ledger
|
|
314
|
+
ctx.out.puts "⚠️ [Rubik::Goal] Suspended goal row #{row.id} could not be rehydrated — " \
|
|
315
|
+
"cross-process resume refused (corrupt or non-suspended stamp)."
|
|
316
|
+
return
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
meta = ledger.suspend_meta || {}
|
|
320
|
+
goal_text = ledger.snapshot[:objective].to_s
|
|
321
|
+
goal_steps = meta["max_steps"].to_i.positive? ? meta["max_steps"].to_i : Rubik::Storage::Layout.config_get(ctx.config, "agent.goal_max_steps", 100).to_i
|
|
322
|
+
goal_state = Rubik::Orchestrator::GoalState.new(event_bus: ctx.event_bus)
|
|
323
|
+
goal_state.start!(goal_text, goal_steps)
|
|
324
|
+
goal_state.restore_counters!(steps: meta["steps"], max_steps: goal_steps,
|
|
325
|
+
extensions: meta["extensions"], tokens: meta["tokens"])
|
|
326
|
+
goal_state.ledger_id = ledger.snapshot[:storage_id]
|
|
327
|
+
|
|
328
|
+
routed = ctx.router.route(task_type: :goal, prompt: goal_text)
|
|
329
|
+
ctx.runtime_manager.provide(:llm_client, routed[:provider]) if routed[:provider]
|
|
330
|
+
# Kaynaklama (issue #454): döngü build_goal_orchestrator ile yeniden
|
|
331
|
+
# kurulur — canlı Loop sürecle öldü (bilinçli asimetri; süreç-içi
|
|
332
|
+
# yolda entry.loop kullanılır).
|
|
333
|
+
orchestrator = build_goal_orchestrator(ctx, goal_state: goal_state, routed: routed, steps: goal_steps)
|
|
334
|
+
|
|
335
|
+
ctx.out.puts "[Rubik::Goal] Cross-process resume of '#{goal_text}' (attempt #{meta['attempt'].to_i})..."
|
|
336
|
+
# Dürüst süreklilik cümlesi (tasarım sözleşmesi): alt ajan
|
|
337
|
+
# konuşmaları süreçle öldü; disk etkileri ve session kaydı yaşar.
|
|
338
|
+
ctx.out.puts " Subagent conversations did not survive the process restart — their disk " \
|
|
339
|
+
"effects and the session record have."
|
|
340
|
+
resume_drive(ctx, orchestrator: orchestrator, goal_state: goal_state, ledger: ledger,
|
|
341
|
+
goal_text: goal_text,
|
|
342
|
+
goal_id: meta["goal_id"].to_s.empty? ? row.id : meta["goal_id"],
|
|
343
|
+
start: lambda { |loop_inst|
|
|
344
|
+
loop_inst.resume_from(goal_text: goal_text, ledger: ledger,
|
|
345
|
+
reason: meta["reason"].to_s,
|
|
346
|
+
suspended_at: parse_suspended_at(meta["suspended_at"]),
|
|
347
|
+
attempt: meta["attempt"].to_i,
|
|
348
|
+
counters: { steps: meta["steps"], max_steps: goal_steps,
|
|
349
|
+
tokens: meta["tokens"] })
|
|
350
|
+
})
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Resume ortak kuyruğu (issue #454): suspend_stamp_meta'nın resume
|
|
354
|
+
# tarafındaki aynası — coeffect sağlama disiplini (GOAL_STATE +
|
|
355
|
+
# sağlanan GOAL_LEDGER), Σ unsuspend!, nötr kayıtçı çifti, pruner
|
|
356
|
+
# apply sınırı, tek drive_goal_turn stanza'sı: "resume hangi
|
|
357
|
+
# coeffect'leri sağlar?" sorusunun TEK cevabı. Bilinçli asimetri
|
|
358
|
+
# arayan tarafta kalır (erken birleştirme yok): kaynaklama, dürüst
|
|
359
|
+
# süreklilik cümleleri ve döngü — süreç-içi yolda canlı entry.loop,
|
|
360
|
+
# çapraz-süreç yolda build_goal_orchestrator; start callable'ı yolu
|
|
361
|
+
# seçer (loop.resume! vs loop.resume_from).
|
|
362
|
+
def resume_drive(ctx, orchestrator:, goal_state:, ledger:, goal_text:, goal_id:, start:)
|
|
363
|
+
ctx.runtime_manager.provide(Rubik::Coeffects::GOAL_STATE, goal_state)
|
|
364
|
+
ctx.runtime_manager.provide(Rubik::Coeffects::GOAL_LEDGER, ledger) if ledger
|
|
365
|
+
ledger&.unsuspend!
|
|
366
|
+
record_pair = build_goal_record_pair(ctx, goal_text)
|
|
367
|
+
# Pruner apply seam: hedef döngüsü de aynı sınırda tohumlanır —
|
|
368
|
+
# aşamalı budama planı varsa goal promptundan ÖNCE uygulanır.
|
|
369
|
+
ctx.apply_staged_prune_plan!
|
|
370
|
+
drive_goal_turn(ctx, orchestrator: orchestrator, goal_state: goal_state,
|
|
371
|
+
goal_ledger: ledger, goal_text: goal_text, goal_id: goal_id,
|
|
372
|
+
record_pair: record_pair, start: start)
|
|
373
|
+
end
|
|
374
|
+
private_class_method :resume_drive
|
|
375
|
+
|
|
376
|
+
# !goal list (issue #106 Faz 2a): store'daki status == "suspended"
|
|
377
|
+
# Σ satırları — özetler zaten status taşıyor (Store::Summary).
|
|
378
|
+
def list_suspended_goals(ctx)
|
|
379
|
+
ctx.recorder&.record_user_turn("!goal list")
|
|
380
|
+
store = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::STORAGE_STORE)
|
|
381
|
+
unless store
|
|
382
|
+
ctx.out.puts "ℹ️ [Rubik::Goal] Storage unavailable — the suspended-goal list needs goal.ledger storage."
|
|
383
|
+
return
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
rows = suspended_rows(store)
|
|
387
|
+
if rows.empty?
|
|
388
|
+
ctx.out.puts "ℹ️ [Rubik::Goal] No suspended goals."
|
|
389
|
+
return
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
ctx.out.puts "=== Suspended goals ==="
|
|
393
|
+
rows.each do |s|
|
|
394
|
+
ctx.out.puts " #{s.id} #{s.updated_at} #{s.title}"
|
|
395
|
+
end
|
|
396
|
+
ctx.out.puts "Resume the latest with: !goal resume"
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
# Askı satırları: en eski → en yeni sıralı (updated_at; kuiratör
|
|
400
|
+
# list yüzeyiyle aynı alan). Sort anahtarı eşitse row sırası.
|
|
401
|
+
def suspended_rows(store)
|
|
402
|
+
store.list(type: "goal").select { |s| s.status == "suspended" }.sort_by(&:updated_at)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# Σ damgası meta'sı (issue #106): {goal_id, steps, max_steps,
|
|
406
|
+
# extensions, tokens, reason, suspended_at, attempt}. Sembol
|
|
407
|
+
# anahtarlar Ledger#suspend! içinde dizgeleştirilir.
|
|
408
|
+
def suspend_stamp_meta(goal_state, goal_id, reason, attempt)
|
|
409
|
+
{ goal_id: goal_id, steps: goal_state.step_count, max_steps: goal_state.max_steps,
|
|
410
|
+
extensions: goal_state.extensions, tokens: goal_state.tokens.dup,
|
|
411
|
+
reason: reason.to_s.slice(0, 300), suspended_at: Time.now.utc.iso8601, attempt: attempt }
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def suspended_attempt(ctx, goal_id)
|
|
415
|
+
Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::GOAL_REGISTRY)&.fetch(goal_id)&.attempt.to_i
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# Oturum kaydı coeffect'i: ilk askıda provide edilir (provide-swap
|
|
419
|
+
# semantiği — Context#provide anahtar başına TEK delete-inverse
|
|
420
|
+
# tutar; tekrarlanan suspend/resume Ω'yı büyütmez).
|
|
421
|
+
def goal_registry_for(ctx)
|
|
422
|
+
registry = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::GOAL_REGISTRY)
|
|
423
|
+
return registry if registry.is_a?(Rubik::Orchestrator::GoalRegistry)
|
|
424
|
+
|
|
425
|
+
registry = Rubik::Orchestrator::GoalRegistry.new
|
|
426
|
+
ctx.runtime_manager&.provide(Rubik::Coeffects::GOAL_REGISTRY, registry)
|
|
427
|
+
registry
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Terminal yollarda girdi temizliği: kayıt SADECE sağlanmışsa —
|
|
431
|
+
# askı hiç yaşanmamış bir koşunun terminali boş registry provide
|
|
432
|
+
# ETMEMELİ (Ω disiplini).
|
|
433
|
+
def forget_suspended_goal(ctx, goal_id)
|
|
434
|
+
registry = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::GOAL_REGISTRY)
|
|
435
|
+
registry.delete(goal_id) if registry.is_a?(Rubik::Orchestrator::GoalRegistry)
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
# Adım olayı lambda'sı (start + cross-process resume ortak): sayaç
|
|
439
|
+
# akışı, kayıtçı tool çiftleri, render — start yolundaki satır içi
|
|
440
|
+
# gövdenin birebir taşınması.
|
|
441
|
+
def build_goal_on_step(ctx, goal_state)
|
|
442
|
+
lambda { |step_info|
|
|
443
|
+
# Adım sayacı rubik://goal/state'e aksın: olaylar bir adım
|
|
444
|
+
# içinde tekrarlanabilir, GoalState monotoniktir (zararsız).
|
|
445
|
+
goal_state.update_steps(step_info[:step], step_info[:max_steps]) if step_info[:step].is_a?(Integer)
|
|
446
|
+
# Session teşhis edilebilirliği (field incident 2026-10-12):
|
|
447
|
+
# saatler süren !goal koşuları session dosyasına tek satır
|
|
448
|
+
# düşürüyordu — free-text turn simetrisinde tool adımları
|
|
449
|
+
# kayıtçıya akar; render ve LLM geçmişi değişmez.
|
|
450
|
+
case step_info[:type]&.to_sym
|
|
451
|
+
when :tool_call
|
|
452
|
+
ctx.recorder&.record_tool_call(step_info[:tool], step_info[:args], call_id: step_info[:call_id])
|
|
453
|
+
when :tool_result
|
|
454
|
+
ctx.recorder&.record_tool_result(step_info[:tool], step_info[:output], status: step_info[:status] || :ok, call_id: step_info[:call_id])
|
|
455
|
+
end
|
|
456
|
+
ctx.render_step_event(step_info)
|
|
457
|
+
}
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# Plan 53 D-8 yardımcısı: nötr çift + kayıtçı eşlemesi tek yerde.
|
|
461
|
+
# Session restore kayıtçı OLAYLARINDAN hidrate eder — eşlenmiş
|
|
462
|
+
# restore için conversation_history VE record_assistant_response
|
|
463
|
+
# birlikte yazılmalı (başarısızlıkta da).
|
|
464
|
+
def build_goal_record_pair(ctx, goal_text)
|
|
465
|
+
lambda do |assistant_content, streamed: nil, tokens: nil|
|
|
466
|
+
ctx.conversation_history << { role: "user", content: "Goal: #{goal_text}" }
|
|
467
|
+
ctx.conversation_history << { role: "assistant", content: assistant_content }
|
|
468
|
+
ctx.recorder&.record_assistant_response(assistant_content, tokens: tokens, streamed: streamed)
|
|
469
|
+
ctx.trim_conversation_window!
|
|
470
|
+
end
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
# The goal-shaped orchestrator-loop stanza's single home (issue #404).
|
|
474
|
+
# execute_goal (fresh run) and resume_cross_process (Σ-window
|
|
475
|
+
# rehydration) build the SAME shape — orchestrator_loop +
|
|
476
|
+
# warn_at_ratio + the goal on_step lambda — so the construction is
|
|
477
|
+
# folded here once. The plan command (render-only on_step, no
|
|
478
|
+
# warn_at_ratio) and input_execution (streaming on_step) keep their
|
|
479
|
+
# materially different stanzas inline at their own sites.
|
|
480
|
+
def build_goal_orchestrator(ctx, goal_state:, routed:, steps:)
|
|
481
|
+
warn_ratio = Rubik::Storage::Layout.config_get(ctx.config, "agent.warn_at_ratio", 0.75).to_f
|
|
482
|
+
ctx.orchestrator_loop(
|
|
483
|
+
provider: routed[:provider],
|
|
484
|
+
model: routed[:model],
|
|
485
|
+
max_steps: steps,
|
|
486
|
+
warn_at_ratio: warn_ratio,
|
|
487
|
+
context_limit: routed[:context_limit],
|
|
488
|
+
max_output_tokens: routed[:max_output_tokens],
|
|
489
|
+
on_step: build_goal_on_step(ctx, goal_state)
|
|
490
|
+
)
|
|
491
|
+
end
|
|
492
|
+
private_class_method :build_goal_orchestrator
|
|
493
|
+
|
|
494
|
+
# Damga meta'sındaki suspended_at (iso8601) → Time; bozuk değer
|
|
495
|
+
# nil — Loop#append_resume_marker! "unknown" downtime basar.
|
|
496
|
+
def parse_suspended_at(value)
|
|
497
|
+
Time.iso8601(value.to_s)
|
|
498
|
+
rescue ArgumentError, TypeError
|
|
499
|
+
nil
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
# Defense-in-depth behind the loop-level goal guard: if a success
|
|
503
|
+
# terminal escapes GoalWait with live subagent units, the goal must
|
|
504
|
+
# stop honestly instead of celebrating a false completion. Fails
|
|
505
|
+
# CONSERVATIVE: coeffect absence is nil-safe (Coeffects.get never
|
|
506
|
+
# raises for a missing subsystem), so a raise here is real breakage
|
|
507
|
+
# — reporting zero would launder it into exactly the false
|
|
508
|
+
# "Goal Achieved" this backstop exists to refuse.
|
|
509
|
+
def goal_active_subagent_count(ctx)
|
|
510
|
+
supervisor = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::SUBAGENT_SUPERVISOR)
|
|
511
|
+
supervisor&.active_count.to_i
|
|
512
|
+
rescue StandardError => e
|
|
513
|
+
Rubik::Log.warn(:orchestrator, "goal completion defense could not inspect subagents " \
|
|
514
|
+
"(#{e.class}: #{e.message}); refusing completion")
|
|
515
|
+
1
|
|
516
|
+
end
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
end
|
|
@@ -12,6 +12,8 @@ require_relative "provider_resolver"
|
|
|
12
12
|
require_relative "provider_recovery"
|
|
13
13
|
require_relative "continuation_manager"
|
|
14
14
|
require_relative "goal_wait"
|
|
15
|
+
require_relative "resume_window"
|
|
16
|
+
require_relative "../storage/layout"
|
|
15
17
|
require_relative "subagent_notice_inbox"
|
|
16
18
|
require_relative "turn_executor"
|
|
17
19
|
require_relative "turn_executor/user_channel"
|
|
@@ -21,8 +23,8 @@ require_relative "subagent/notices"
|
|
|
21
23
|
module Rubik
|
|
22
24
|
module Orchestrator
|
|
23
25
|
class Loop
|
|
24
|
-
attr_accessor :history, :invalid_tool_count, :last_turn_streamed, :context_warning_emitted
|
|
25
|
-
attr_reader :max_steps, :step_count, :model, :total_tokens, :warn_at_ratio, :runtime_manager, :
|
|
26
|
+
attr_accessor :history, :invalid_tool_count, :last_turn_streamed, :context_warning_emitted, :loop_context
|
|
27
|
+
attr_reader :max_steps, :step_count, :model, :total_tokens, :warn_at_ratio, :runtime_manager, :context_limit, :max_output_tokens, :config
|
|
26
28
|
# Collaborator-facing state (Plan 54 D-5): loop state physically stays on Loop;
|
|
27
29
|
# extracted collaborators reach it through these accessors.
|
|
28
30
|
attr_reader :provider, :on_step, :tool_registry, :shell, :buffer_manager, :lsp_manager, :step_executor,
|
|
@@ -54,7 +56,7 @@ module Rubik
|
|
|
54
56
|
@root_dir = File.expand_path(shell&.buffer_manager&.root_dir || Dir.pwd)
|
|
55
57
|
@runtime_manager = shell&.runtime_manager
|
|
56
58
|
@context_limit = context_limit || 32_768
|
|
57
|
-
@max_output_tokens = (max_output_tokens || (@config
|
|
59
|
+
@max_output_tokens = (max_output_tokens || Rubik::Storage::Layout.config_get(@config, "llm.default_max_output_tokens", 4_096)).to_i
|
|
58
60
|
@loop_context = nil
|
|
59
61
|
@step_count = 0
|
|
60
62
|
@history = []
|
|
@@ -108,6 +110,12 @@ module Rubik
|
|
|
108
110
|
def goal_wait = (@goal_wait ||= GoalWait.new(@shell_deps))
|
|
109
111
|
def continuation_manager = (@continuation_manager ||= ContinuationManager.new(config: @config))
|
|
110
112
|
|
|
113
|
+
# Loop-side resume window collaborator (issue #475): owns the
|
|
114
|
+
# ctx.extend lifecycle, the cross-process history prefix and the
|
|
115
|
+
# goal_resumed marker. Lazy like goal_wait — Class#allocate test
|
|
116
|
+
# doubles skip initialize.
|
|
117
|
+
def resume_window = (@resume_window ||= ResumeWindow.new(@shell_deps))
|
|
118
|
+
|
|
111
119
|
# Agent-turn-executor plan: response processing is shared with the
|
|
112
120
|
# subagent channel through TurnExecutor; Loop supplies the
|
|
113
121
|
# user-channel adapter. Built lazily on first use, NOT in
|
|
@@ -199,18 +207,17 @@ module Rubik
|
|
|
199
207
|
|
|
200
208
|
# Süreç-içi resume (Faz 1): hayatta kalan Loop örneğinde history
|
|
201
209
|
# prefix'i nesne-özdeş kalır, yalnızca goal_resumed imzası eklenir.
|
|
202
|
-
#
|
|
203
|
-
#
|
|
204
|
-
#
|
|
205
|
-
# yoksa her suspend/resume döngüsü ölü bir çocuk sızdırır (§6).
|
|
210
|
+
# Sub-context ve imza düzeni ResumeWindow işbirlikçisindedir (issue
|
|
211
|
+
# #475): resume_window.reopen_context! askılı koşunun ESKİ alt
|
|
212
|
+
# bağlamını LIFO disipliniyle imha eder, sonra taze bir ctx.extend
|
|
213
|
+
# kurar — yoksa her suspend/resume döngüsü ölü bir çocuk sızdırır (§6).
|
|
206
214
|
def resume!(reason:, suspended_at: nil, attempt: 1)
|
|
207
|
-
|
|
208
|
-
@loop_context = @runtime_manager&.context&.extend
|
|
209
|
-
append_resume_marker!(reason: reason, suspended_at: suspended_at, attempt: attempt)
|
|
215
|
+
resume_window.reopen_context!
|
|
210
216
|
begin
|
|
217
|
+
resume_window.stamp_resume_marker!(reason: reason, suspended_at: suspended_at, attempt: attempt)
|
|
211
218
|
execute_steps(opts: resume_request_opts)
|
|
212
219
|
ensure
|
|
213
|
-
|
|
220
|
+
resume_window.close_context!
|
|
214
221
|
end
|
|
215
222
|
end
|
|
216
223
|
|
|
@@ -223,16 +230,16 @@ module Rubik
|
|
|
223
230
|
def resume_from(goal_text:, ledger:, reason:, suspended_at: nil, attempt: 1, counters: {})
|
|
224
231
|
@goal_mode = true
|
|
225
232
|
restore_resume_counters!(counters)
|
|
226
|
-
|
|
233
|
+
resume_window.open_context!
|
|
227
234
|
begin
|
|
228
|
-
|
|
235
|
+
system_prompt = render_system_prompt(mode: :goal, plan_only: false, extra_context: nil)
|
|
236
|
+
resume_window.build_cross_process_prefix!(goal_text: goal_text, ledger: ledger,
|
|
237
|
+
system_prompt: system_prompt)
|
|
229
238
|
@last_prompt_fingerprint = prompt_state_fingerprint(plan_only: false)
|
|
230
|
-
|
|
231
|
-
@history << { role: "user", content: ledger.to_xml }
|
|
232
|
-
append_resume_marker!(reason: reason, suspended_at: suspended_at, attempt: attempt)
|
|
239
|
+
resume_window.stamp_resume_marker!(reason: reason, suspended_at: suspended_at, attempt: attempt)
|
|
233
240
|
execute_steps(opts: resume_request_opts)
|
|
234
241
|
ensure
|
|
235
|
-
|
|
242
|
+
resume_window.close_context!
|
|
236
243
|
end
|
|
237
244
|
end
|
|
238
245
|
|
|
@@ -247,9 +254,7 @@ module Rubik
|
|
|
247
254
|
end
|
|
248
255
|
|
|
249
256
|
def safe_config(key, default)
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
@config.fetch(key, default) || default
|
|
257
|
+
Rubik::Storage::Layout.config_get(@config, key, default)
|
|
253
258
|
rescue KeyError, TypeError, NoMethodError
|
|
254
259
|
default
|
|
255
260
|
end
|
|
@@ -322,33 +327,9 @@ module Rubik
|
|
|
322
327
|
opts
|
|
323
328
|
end
|
|
324
329
|
|
|
325
|
-
# goal_resumed imza satırı
|
|
326
|
-
#
|
|
327
|
-
#
|
|
328
|
-
# okur biçimdedir; bilinemezse "unknown". reason Σ damgasıyla aynı
|
|
329
|
-
# 300-karakter disiplinine tabidir — 5xx gövdeleri history'yi
|
|
330
|
-
# şişirmesin.
|
|
331
|
-
def append_resume_marker!(reason:, suspended_at:, attempt:)
|
|
332
|
-
esc = Rubik::Platform.method(:xml_escape)
|
|
333
|
-
@history << { role: "user", content: "<goal_resumed downtime=\"#{esc.call(format_downtime(suspended_at))}\" " \
|
|
334
|
-
"reason=\"#{esc.call(reason.to_s.slice(0, 300))}\" attempt=\"#{attempt.to_i}\" " \
|
|
335
|
-
"steps_done=\"#{@step_count}\" />" }
|
|
336
|
-
end
|
|
337
|
-
|
|
338
|
-
# saniye → "45s" / "4m 12s" / "1h 03m"; askı zamanı bilinmiyorsa
|
|
339
|
-
# dürüst "unknown" (çapraz-süreç meta bozuksa). Negatif sapma
|
|
340
|
-
# (saat kayması) 0'a kestirilir.
|
|
341
|
-
def format_downtime(suspended_at)
|
|
342
|
-
return "unknown" unless suspended_at.respond_to?(:to_i) && suspended_at.to_i.positive?
|
|
343
|
-
|
|
344
|
-
secs = [(Time.now - suspended_at).to_i, 0].max
|
|
345
|
-
mm, ss = secs.divmod(60)
|
|
346
|
-
hh, mm = mm.divmod(60)
|
|
347
|
-
return "#{hh}h #{mm.to_s.rjust(2, '0')}m" if hh.positive?
|
|
348
|
-
return "#{mm}m #{ss.to_s.rjust(2, '0')}s" if mm.positive?
|
|
349
|
-
|
|
350
|
-
"#{ss}s"
|
|
351
|
-
end
|
|
330
|
+
# goal_resumed imza satırı ve sub-context düzeni ResumeWindow'da
|
|
331
|
+
# (issue #475); Loop yalnızca sayaç geri yükleme ve request-opts
|
|
332
|
+
# sözleşmelerini tutar.
|
|
352
333
|
|
|
353
334
|
def restore_resume_counters!(counters)
|
|
354
335
|
@step_count = counters[:steps].to_i
|