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.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +250 -43
  4. data/README.md +5 -1
  5. data/lib/rubik/adapter/base.rb +21 -1
  6. data/lib/rubik/adapter/generic.rb +0 -14
  7. data/lib/rubik/adapter/node.rb +2 -0
  8. data/lib/rubik/adapter/process.rb +28 -67
  9. data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
  10. data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
  11. data/lib/rubik/buffer/apply_contract.rb +3 -3
  12. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  13. data/lib/rubik/buffer/manager.rb +26 -120
  14. data/lib/rubik/cli/workspace_trust.rb +16 -0
  15. data/lib/rubik/config/deep_ops.rb +4 -1
  16. data/lib/rubik/config/defaults.rb +29 -3
  17. data/lib/rubik/config/schema.rb +8 -0
  18. data/lib/rubik/config/secret_policy.rb +197 -0
  19. data/lib/rubik/config/store.rb +14 -0
  20. data/lib/rubik/config.rb +11 -0
  21. data/lib/rubik/diagnostics/collector.rb +0 -9
  22. data/lib/rubik/errors.rb +15 -0
  23. data/lib/rubik/event_bus.rb +2 -1
  24. data/lib/rubik/kernel/dispatcher.rb +16 -11
  25. data/lib/rubik/kernel/unit.rb +4 -1
  26. data/lib/rubik/llm/registry.rb +12 -9
  27. data/lib/rubik/llm/router.rb +2 -5
  28. data/lib/rubik/log.rb +23 -1
  29. data/lib/rubik/lsp/client.rb +152 -445
  30. data/lib/rubik/lsp/document_store.rb +209 -0
  31. data/lib/rubik/lsp/manager.rb +14 -10
  32. data/lib/rubik/lsp/transport.rb +365 -0
  33. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  34. data/lib/rubik/mcp/boot_run.rb +190 -0
  35. data/lib/rubik/mcp/client.rb +4 -3
  36. data/lib/rubik/mcp/manager.rb +46 -165
  37. data/lib/rubik/mcp/transport/stdio.rb +55 -10
  38. data/lib/rubik/orchestrator/auto_apply.rb +4 -3
  39. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  40. data/lib/rubik/orchestrator/goal_session.rb +519 -0
  41. data/lib/rubik/orchestrator/loop.rb +28 -47
  42. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  43. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  44. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  45. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  46. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  47. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  48. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  49. data/lib/rubik/orchestrator/subagent/instance.rb +11 -12
  50. data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
  51. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  52. data/lib/rubik/orchestrator/subagent/questioner.rb +24 -7
  53. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  54. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  55. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  56. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  57. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  58. data/lib/rubik/orchestrator/subagent/step_runner.rb +11 -3
  59. data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
  60. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  61. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  62. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  63. data/lib/rubik/orchestrator.rb +1 -0
  64. data/lib/rubik/platform/io_drain.rb +2 -1
  65. data/lib/rubik/platform/process.rb +144 -54
  66. data/lib/rubik/platform.rb +17 -1
  67. data/lib/rubik/plugin/manager.rb +2 -1
  68. data/lib/rubik/plugin/storage.rb +20 -29
  69. data/lib/rubik/repl/commands/agent_context.rb +9 -8
  70. data/lib/rubik/repl/commands/agent_execution.rb +20 -502
  71. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  72. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  73. data/lib/rubik/repl/commands/base.rb +47 -2
  74. data/lib/rubik/repl/commands/buffer.rb +6 -5
  75. data/lib/rubik/repl/commands/lsp.rb +3 -2
  76. data/lib/rubik/repl/commands/pruner.rb +2 -1
  77. data/lib/rubik/repl/commands/secrets.rb +151 -0
  78. data/lib/rubik/repl/commands/workspace.rb +60 -150
  79. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  80. data/lib/rubik/repl/config_wiring.rb +3 -3
  81. data/lib/rubik/repl/hooks.rb +2 -1
  82. data/lib/rubik/repl/input_execution.rb +158 -76
  83. data/lib/rubik/repl/input_loop.rb +3 -2
  84. data/lib/rubik/repl/lifecycle.rb +82 -204
  85. data/lib/rubik/repl/notice_router.rb +319 -0
  86. data/lib/rubik/repl/paste_support.rb +3 -1
  87. data/lib/rubik/repl/presentation.rb +5 -3
  88. data/lib/rubik/repl/pruner/gate.rb +3 -1
  89. data/lib/rubik/repl/pruner/harness.rb +2 -1
  90. data/lib/rubik/repl/shell.rb +4 -3
  91. data/lib/rubik/repl/turn_recorder.rb +6 -5
  92. data/lib/rubik/runtime/discovery.rb +3 -2
  93. data/lib/rubik/runtime/loop_service.rb +4 -3
  94. data/lib/rubik/runtime/manifest_store.rb +124 -0
  95. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  96. data/lib/rubik/runtime/overlay_storage.rb +269 -272
  97. data/lib/rubik/runtime/task_manager.rb +71 -27
  98. data/lib/rubik/runtime.rb +1 -0
  99. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  100. data/lib/rubik/sandbox/runner.rb +16 -15
  101. data/lib/rubik/security/allowlist.rb +167 -0
  102. data/lib/rubik/security/audit_log.rb +72 -0
  103. data/lib/rubik/security/classifier/classifier.rb +8 -0
  104. data/lib/rubik/security/command_gate.rb +591 -0
  105. data/lib/rubik/security/guard.rb +94 -1077
  106. data/lib/rubik/security/path_policy.rb +117 -0
  107. data/lib/rubik/security/secret_store.rb +463 -0
  108. data/lib/rubik/security/synthesis_gate.rb +297 -0
  109. data/lib/rubik/security.rb +5 -0
  110. data/lib/rubik/session/manager.rb +7 -1
  111. data/lib/rubik/skills/manager.rb +2 -3
  112. data/lib/rubik/storage/ranking.rb +113 -0
  113. data/lib/rubik/storage/search.rb +49 -0
  114. data/lib/rubik/storage/store.rb +15 -112
  115. data/lib/rubik/storage/transcript_indexer.rb +2 -3
  116. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  117. data/lib/rubik/tools/base.rb +22 -0
  118. data/lib/rubik/tools/builtins/files.rb +3 -15
  119. data/lib/rubik/tools/builtins/grep.rb +3 -9
  120. data/lib/rubik/tools/builtins/lifecycle.rb +96 -92
  121. data/lib/rubik/tools/builtins/synthesis.rb +3 -0
  122. data/lib/rubik/tools/lifecycle_rollbacks.rb +100 -0
  123. data/lib/rubik/tools/native_search.rb +2 -1
  124. data/lib/rubik/tools/truncator.rb +73 -36
  125. data/lib/rubik/ui/banner.rb +4 -3
  126. data/lib/rubik/ui/spinner.rb +3 -1
  127. data/lib/rubik/ui/statusline.rb +6 -5
  128. data/lib/rubik/version.rb +1 -1
  129. data/lib/rubik.rb +1 -2
  130. metadata +23 -1
@@ -2,9 +2,12 @@
2
2
 
3
3
  require_relative "../events"
4
4
  require_relative "../log"
5
+ require_relative "../platform"
6
+ require_relative "../storage/layout"
5
7
  require_relative "../orchestrator/subagent/notices"
6
8
  require_relative "../prompt/conversation_rows"
7
9
  require_relative "pending_notice_queue"
10
+ require_relative "notice_router"
8
11
 
9
12
  module Rubik
10
13
  module REPL
@@ -12,14 +15,27 @@ module Rubik
12
15
  # (keeps shell.rb under the decoupling line-count gate). The Shell remains
13
16
  # the coordinator: these methods depend on shell identity, so they live in
14
17
  # an included module rather than in Composition (the pure builder).
18
+ #
19
+ # Issue #469: the notice-routing half (MCP boot notices, subagent event
20
+ # routing, reactive wakeup state) now lives in NoticeRouter; the methods
21
+ # below delegate to a lazily-created router so the public Shell surface
22
+ # (and the bare-Lifecycle test-double harnesses) stay unchanged. The
23
+ # router owns the queue/mutex/token concurrency state.
15
24
  module Lifecycle
16
- # Plan 109 Faz B: notice kinds worth interrupting the idle prompt
17
- # with an autonomous agent-0 turn. Excludes :context_compacted
18
- # (self-healed, guidance says no action needed) and
19
- # :auto_apply_failed (informational; the subagent's own next step
20
- # reports it).
21
- WAKE_KINDS = [:completed, :budget_exhausted, :error_paused, :message, :question, :task].freeze
22
- # Phase 1: assign built subsystems. Entries derive from the
25
+ # Plan 109 Faz B: notice kinds worth interrupting the idle prompt —
26
+ # vocabulary now owned by NoticeRouter; re-exported here because
27
+ # callers (and silence-contract tests) pin Rubik::REPL::Lifecycle::
28
+ # WAKE_KINDS.
29
+ WAKE_KINDS = NoticeRouter::WAKE_KINDS
30
+
31
+ # The notice-routing collaborator (issue #469): lazily created so bare
32
+ # Lifecycle test doubles (which set plain ivars, no constructor) keep
33
+ # working; the router reads host state live via instance_variable_get.
34
+ def notice_router
35
+ @notice_router ||= NoticeRouter.new(self)
36
+ end
37
+
38
+ # Plan 1, Phase 1: assign built subsystems. Entries derive from the
23
39
  # ServiceCatalog (the single declaration point): one loop assigns every
24
40
  # injectable entry's ivar, then a second pass runs the per-entry
25
41
  # after_inject hooks (injected-buffer post-wiring — which runs only on
@@ -56,11 +72,11 @@ module Rubik
56
72
  def load_plugins(parts)
57
73
  return unless @plugin_manager
58
74
 
59
- Array(@config.fetch("plugins.load", [])).each { |g| @plugin_manager.load_plugin_gem(g, runtime_manager: @runtime_manager) }
60
- if @config.fetch("plugins.autoload_dropin", true) && parts.respond_to?(:plugin_storage) && parts.plugin_storage
75
+ Array(Rubik::Storage::Layout.config_get(@config, "plugins.load", [])).each { |g| @plugin_manager.load_plugin_gem(g, runtime_manager: @runtime_manager) }
76
+ if Rubik::Storage::Layout.config_get(@config, "plugins.autoload_dropin", true) && parts.respond_to?(:plugin_storage) && parts.plugin_storage
61
77
  @plugin_manager.load_dropin_plugins(runtime_manager: @runtime_manager, storage: parts.plugin_storage)
62
78
  end
63
- aliases_cfg = @config.fetch("plugins.aliases", {})
79
+ aliases_cfg = Rubik::Storage::Layout.config_get(@config, "plugins.aliases", {})
64
80
  aliases_cfg.each { |s, f| @plugin_manager.register_alias(s, f) } if aliases_cfg.is_a?(Hash)
65
81
  end
66
82
 
@@ -143,6 +159,9 @@ module Rubik
143
159
  # never forces async warm in tests.
144
160
  warm_async = Platform.async_knob(@config, "ast.async_warm")
145
161
  maybe_warn_huge_root!
162
+ # Boot accumulated config problems (unknown keys, corrupt YAML, legacy
163
+ # env vars) surface here — same notices seam, still before the banner.
164
+ maybe_emit_config_warnings!
146
165
  # A strong huge-root signal skips the boot warm walk: the index
147
166
  # stays empty and tools answer the honest not-ready message until
148
167
  # an explicit '!context scan' opts in. Warning only — never a block.
@@ -183,7 +202,7 @@ module Rubik
183
202
  # '!context scan' (strong_only: false). Memoized so the scan reuses
184
203
  # the assessment without a second git spawn.
185
204
  def maybe_warn_huge_root!(strong_only: true)
186
- return unless @config.nil? || @config.fetch("context.huge_root_notice", true)
205
+ return unless @config.nil? || Rubik::Storage::Layout.config_get(@config, "context.huge_root_notice", true)
187
206
  return if @huge_root_notice_shown
188
207
 
189
208
  assessment = huge_root_assessment
@@ -193,11 +212,7 @@ module Rubik
193
212
  return if guidance.empty?
194
213
 
195
214
  @huge_root_notice_shown = true
196
- if @terminal_guard
197
- @terminal_guard.print_safe_notice(guidance)
198
- else
199
- @output&.puts guidance
200
- end
215
+ NoticeRouter.print_safe_notice_line(@terminal_guard, @output, guidance)
201
216
  end
202
217
 
203
218
  def huge_root_assessment
@@ -216,238 +231,100 @@ module Rubik
216
231
 
217
232
  def huge_root_boot_scan_skipped?
218
233
  return false unless huge_root_assessment.strong
219
- return false unless @config.respond_to?(:fetch)
220
234
 
221
- !@config.fetch("context.boot_scan_on_huge_root", false)
235
+ !Rubik::Storage::Layout.config_get(@config, "context.boot_scan_on_huge_root", false)
236
+ end
237
+
238
+ # Boot-time config observability (issue #399): Config#warnings
239
+ # accumulates unknown-key, corrupt-YAML, ~/.rubik and env-deprecation
240
+ # problems during load with no reader; this surfaces them through the
241
+ # notices seam right after the huge-root check, before the banner.
242
+ # Once per REPL process boot — warnings appended later in the session
243
+ # (runtime set()) stay in the array for !config/tests without printing.
244
+ # Rubik::Log.warn mirrors each notice so headless/scripted boots see
245
+ # them on stderr too.
246
+ def maybe_emit_config_warnings!
247
+ warnings = @config&.warnings
248
+ return if warnings.nil? || warnings.empty?
249
+ return if @config_warnings_shown
250
+
251
+ @config_warnings_shown = true
252
+ warnings.each do |w|
253
+ line = "⚠ Config: #{w}"
254
+ Rubik::Log.warn(:config, line)
255
+ NoticeRouter.print_safe_notice_line(@terminal_guard, @output, line)
256
+ end
222
257
  end
223
258
 
224
- # Async MCP boot progress notices (mcp-async-boot D8): subscribe BEFORE
225
- # the boot triggers so no event is missed, with the unsubscribe inverse
226
- # registered in the same breath (wire_subagent_events! pattern). Async
227
- # mode only — a synchronous boot finishes before the first prompt, so
228
- # its failures already surfaced on stderr.
259
+ # --- Notice routing (issue #469): thin delegation to NoticeRouter ----
260
+ # The router owns the queue/mutex/token concurrency state; these
261
+ # delegators keep the historical Shell surface (and the bare-Lifecycle
262
+ # test-double harnesses that include this module) unchanged.
263
+
264
+ # Async MCP boot progress notices (mcp-async-boot D8). See
265
+ # NoticeRouter#wire_mcp_boot_notices!.
229
266
  def wire_mcp_boot_notices!(async:)
230
- return unless async && @event_bus
231
- return if @mcp_notice_tokens # idempotent re-wire
232
-
233
- ctx = @runtime_manager&.context
234
- return unless ctx.respond_to?(:on_dispose)
235
-
236
- @mcp_notice_tokens = [
237
- [:mcp_server_loaded, @event_bus.subscribe(:mcp_server_loaded) { |payload| print_mcp_boot_notice(:loaded, payload) }],
238
- [:mcp_server_failed, @event_bus.subscribe(:mcp_server_failed) { |payload| print_mcp_boot_notice(:failed, payload) }]
239
- ]
240
- ctx.on_dispose do
241
- @mcp_notice_tokens&.each { |(event, token)| @event_bus.unsubscribe(event, token) }
242
- @mcp_notice_tokens = nil
243
- end
267
+ notice_router.wire_mcp_boot_notices!(async: async)
244
268
  end
245
269
 
246
- # Same kworker-thread contract as handle_subagent_event: no shell locks,
247
- # drop silently when the UI is not up yet (an instant boot failure is
248
- # still on stderr and in dmesg). Pure display line — never enters the
249
- # conversation history and never wakes an idle turn.
250
270
  def print_mcp_boot_notice(state, payload)
251
- return unless @running && @output
252
- return unless payload.is_a?(Hash)
253
-
254
- name = payload[:server]
255
- line = if state == :loaded
256
- "mcp: server '#{name}' ready (#{payload[:tools] || 0} tools)"
257
- else
258
- "mcp: server '#{name}' failed (#{payload[:error]})"
259
- end
260
- @terminal_guard&.print_safe_notice(line)
271
+ notice_router.print_mcp_boot_notice(state, payload)
261
272
  end
262
273
 
263
274
  def wire_subagent_events!
264
- return unless @event_bus
265
- return if @subagent_event_tokens # idempotent re-wire (RenderService precedent)
266
-
267
- # Inverse-by-construction: subscribe only where the unsubscribe
268
- # inverse can be registered in the same breath (AGENTS.md §1A).
269
- ctx = @runtime_manager&.context
270
- return unless ctx.respond_to?(:on_dispose)
271
-
272
- @subagent_notices_mutex = Mutex.new
273
- @subagent_notices = []
274
- # Plan 109: the idle-turn wake flag, eagerly initialized BEFORE any
275
- # subscription exists so handlers never race its construction.
276
- @pending_notice_queue = PendingNoticeQueue.new
277
- tokens = Rubik::Orchestrator::Subagent::Notices::EVENTS.keys.map do |event|
278
- [event, @event_bus.subscribe(event) { |payload| handle_subagent_event(event, payload) }]
279
- end
280
- tokens << [Rubik::Events::UNIT_STATE_CHANGED, @event_bus.subscribe(Rubik::Events::UNIT_STATE_CHANGED) { |payload| handle_unit_event(payload) }]
281
- tokens << [Rubik::Events::SUBAGENT_MESSAGE_TO_PARENT, @event_bus.subscribe(Rubik::Events::SUBAGENT_MESSAGE_TO_PARENT) { |payload| handle_parent_message(payload) }]
282
- @subagent_event_tokens = tokens
283
- ctx.on_dispose do
284
- @subagent_event_tokens&.each { |(event, token)| @event_bus.unsubscribe(event, token) }
285
- @subagent_event_tokens = nil
286
- end
275
+ notice_router.wire_subagent_events!
287
276
  end
288
277
 
289
- # ONE routing seam for subagent notices (Candidate 02, unified for
290
- # Plan 109): bus event -> normalized notice -> channel-split
291
- # delivery. Called on the kworker thread (bus dispatch is
292
- # synchronous): Queue pushes, the standby Mutex, and the guard
293
- # monitor only — no shell locks. Model channel (structured hash)
294
- # enters the active loop queue and is rendered to XML at drain
295
- # time; user channel (terminal line) is buffered to stdout while a
296
- # loop is active and flushed at turn end. Between turns the notice
297
- # is injected into @conversation_history, printed, and — when
298
- # wake-worthy — queued to owe agent-0 an autonomous notice turn.
299
278
  def handle_subagent_event(event_name, payload)
300
- return unless @running && @output && !@replaying
301
-
302
- notice = Rubik::Orchestrator::Subagent::Notices.normalize(event_name, payload)
303
- route_subagent_notice(notice) if notice
279
+ notice_router.handle_subagent_event(event_name, payload)
304
280
  end
305
281
 
306
282
  def handle_unit_event(payload)
307
- return unless @running && @output && !@replaying
308
- return unless payload.is_a?(Hash)
309
-
310
- type = payload[:type]&.to_sym
311
- return unless [:process, :task].include?(type)
312
-
313
- to_state = payload[:to_state]&.to_sym
314
- return unless [:completed, :failed].include?(to_state)
315
-
316
- route_subagent_notice({ kind: :task, id: payload[:unit_id], role: "Background Task", state: to_state.to_s, reason: payload[:reason] })
283
+ notice_router.handle_unit_event(payload)
317
284
  end
318
285
 
319
286
  def handle_parent_message(payload)
320
- return unless @running && @output && !@replaying
321
- return unless payload.is_a?(Hash)
322
-
323
- route_subagent_notice({ kind: :message, id: payload[:sender_id] || "subagent",
324
- role: "Subagent", state: "message", reason: payload[:message].to_s })
287
+ notice_router.handle_parent_message(payload)
325
288
  end
326
289
 
327
290
  def route_subagent_notice(notice)
328
- line = Rubik::Orchestrator::Subagent::Notices.user_line(notice)
329
- if @active_loop
330
- @active_loop.enqueue_subagent_notice(notice) if @active_loop.respond_to?(:enqueue_subagent_notice)
331
- @subagent_notices_mutex&.synchronize { @subagent_notices << line }
332
- else
333
- inject_subagent_notice_into_conversation_history!(notice)
334
- if @terminal_guard
335
- @terminal_guard.print_safe_notice(line)
336
- else
337
- @output.puts "\n#{line}"
338
- end
339
- # Wakeloop fix 2026-10-09: an event landing inside an AUTONOMOUS
340
- # turn's idle-path windows (before @active_loop is set, or in the
341
- # ensure tail after it is cleared) must not arm a follow-up wake —
342
- # that is the probabilistic residue of the chained-auto-turn bug.
343
- # History injection and the printed line above still happen (no
344
- # loss); only the synthetic Enter is withheld. A fresh event at a
345
- # genuinely idle prompt (@autonomous_turn false) wakes as designed.
346
- request_reactive_wakeup!(notice) unless @autonomous_turn
347
- end
291
+ notice_router.route_subagent_notice(notice)
348
292
  end
349
293
 
350
- # Plan 109 Faz B: the reactive wake composition — vocabulary policy
351
- # (WAKE_KINDS), kill-switch (subagents.reactive_wakeup config),
352
- # state (PendingNoticeQueue), mechanism (TerminalGuard#safe_wakeup!).
353
- # Every wake-worthy enqueue attempts the kick (no empty->non-empty
354
- # coalescing): a kick refused earlier (user typing / paste
355
- # collection) must retry on the next event, or the wake would wait
356
- # for a manual Enter. TerminalGuard re-verifies emptiness under its
357
- # monitor, so a typing user is never disturbed, and a surplus
358
- # injected Enter degrades to the :empty noop. Runs on the kworker
359
- # thread: queue push and the guard monitor only.
360
294
  def request_reactive_wakeup!(notice)
361
- return unless reactive_wakeup_enabled?
362
- return unless notice.is_a?(Hash) && WAKE_KINDS.include?(notice[:kind])
363
-
364
- pending_notice_queue.enqueue(notice)
365
- @terminal_guard&.safe_wakeup!
295
+ notice_router.request_reactive_wakeup!(notice)
366
296
  end
367
297
 
368
298
  def reactive_wakeup_enabled?
369
- @config.respond_to?(:fetch) ? @config.fetch("subagents.reactive_wakeup", true) != false : true
299
+ notice_router.reactive_wakeup_enabled?
370
300
  end
371
301
 
372
- # Eager init happens in wire_subagent_events! (before any
373
- # subscription exists); the lazy fallback keeps bare Lifecycle test
374
- # doubles working (SubagentNoticeInbox mold).
375
302
  def pending_notice_queue
376
- @pending_notice_queue ||= PendingNoticeQueue.new
303
+ notice_router.pending_notice_queue
377
304
  end
378
305
 
379
306
  def pending_wake_notices?
380
- pending_notice_queue.any?
307
+ notice_router.pending_wake_notices?
381
308
  end
382
309
 
383
310
  def drain_pending_wake_notices!
384
- pending_notice_queue.drain_all
311
+ notice_router.drain_pending_wake_notices!
385
312
  end
386
313
 
387
- # Staleness rule: any real turn's context already includes the
388
- # idle-injected notices in @conversation_history — a surviving
389
- # pending wake would turn the next stray Enter into a redundant
390
- # notice turn.
391
314
  def clear_pending_wake_notices!
392
- pending_notice_queue.clear!
315
+ notice_router.clear_pending_wake_notices!
393
316
  end
394
317
 
395
318
  def inject_subagent_notice_into_conversation_history!(notice)
396
- xml_payload = Rubik::Orchestrator::Subagent::Notices.history_block([notice])
397
- @conversation_history ||= []
398
- Rubik::Prompt::ConversationRows.append_notice!(@conversation_history, xml_payload)
319
+ notice_router.inject_subagent_notice_into_conversation_history!(notice)
399
320
  end
400
321
 
401
322
  def flush_subagent_notices!
402
- return unless @output && @subagent_notices_mutex
403
-
404
- notices = @subagent_notices_mutex.synchronize do
405
- list = @subagent_notices.dup
406
- @subagent_notices.clear
407
- list
408
- end
409
- return if notices.empty?
410
-
411
- if @terminal_guard
412
- notices.each { |m| @terminal_guard.print_safe_notice(m) }
413
- else
414
- @output.puts ""
415
- notices.each { |m| @output.puts m }
416
- end
323
+ notice_router.flush_subagent_notices!
417
324
  end
418
325
 
419
- # Session review B3 (turn-loss fix): Loop's drain runs at the TOP of each
420
- # step, so notices landing during the final step stranded in the loop
421
- # inbox at turn end — the user channel flushed a line, but the model
422
- # channel never entered history and no wake was owed (Plan 109's
423
- # documented "late-notice asymmetry" accepted the loss; a live session
424
- # showed it costs a whole turn). Called from execute_free_text's ensure
425
- # AFTER @active_loop = nil: the notices are injected into
426
- # @conversation_history (visible to the next turn) and wake-worthy kinds
427
- # arm the reactive wake, exactly like the idle-path branch of
428
- # route_subagent_notice. Runs on the turn thread. Per-notice rescue is
429
- # the ensure block's own contract ("cleanup must never mask the
430
- # in-flight exception") — and a raising notice must not strand its
431
- # queue-mates that drain_all already popped.
432
- #
433
- # arm_wake: false keeps the model-channel delivery but does NOT fire a
434
- # synthetic Enter (wakeloop fix 2026-10-09): an INTERRUPTED turn and an
435
- # AUTONOMOUS notice turn settle instead of re-arming — otherwise every
436
- # stranded notice chains another auto-turn (the prompt never returns)
437
- # and the cycle survives Ctrl+C.
438
326
  def deliver_stranded_loop_notices!(loop_instance, arm_wake: true)
439
- return unless loop_instance.respond_to?(:subagent_notice_inbox)
440
-
441
- loop_instance.subagent_notice_inbox.drain_all.each do |notice|
442
- inject_subagent_notice_into_conversation_history!(notice)
443
- request_reactive_wakeup!(notice) if arm_wake
444
- rescue StandardError => e
445
- # Load-bearing visibility (wakeloop fix 2026-10-09): this delivery
446
- # is the fix's "no loss" guarantee, so a dropped notice may not
447
- # vanish behind the ENV-gated debug_warn. Unconditional, non-raising.
448
- Rubik::Log.warn(:repl, "stranded notice delivery failed (#{notice[:kind].inspect}): " \
449
- "#{e.class}: #{e.message}")
450
- end
327
+ notice_router.deliver_stranded_loop_notices!(loop_instance, arm_wake: arm_wake)
451
328
  end
452
329
 
453
330
  def reap_dead_kernel_units!
@@ -460,12 +337,13 @@ module Rubik
460
337
  # Audit 1: ENV["RUBIK_DEBUG"]-gated logging for intentionally swallowed
461
338
  # exceptions (convention from config/store.rb). Never raises.
462
339
  #
463
- # $stderr.puts, not Kernel#warn: warn is silenced when $VERBOSE is nil
464
- # (bundler/rake -W0). Same ruling as Log.emit and Platform.debug_warn.
340
+ # Delegates to Platform.debug_warn (issue #452): the platform channel is
341
+ # the ONE home of RUBIK_DEBUG-gated debug warnings — Log.debug is gated
342
+ # by RUBIK_LOG_LEVEL instead, so reusing the sanctioned platform sink
343
+ # keeps this channel's exact semantics ([WARN] prefix, audible under
344
+ # $VERBOSE=nil, silent unless RUBIK_DEBUG is set, error-isolated).
465
345
  def debug_warn(msg)
466
- $stderr.puts "[WARN] #{msg}" if ENV["RUBIK_DEBUG"] # rubocop:disable Style/StderrPuts
467
- rescue StandardError
468
- nil
346
+ Rubik::Platform.debug_warn(msg)
469
347
  end
470
348
  end
471
349
  end