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,6 +2,7 @@
2
2
 
3
3
  require_relative "../tools/catalog"
4
4
  require_relative "tool_history"
5
+ require_relative "../storage/layout"
5
6
 
6
7
  module Rubik
7
8
  module Orchestrator
@@ -29,14 +30,11 @@ module Rubik
29
30
  !["start"].include?((tool_args["action"] || tool_args[:action]).to_s.downcase.strip)
30
31
 
31
32
  # Kapıyı çalışma anında config'den oku (!config set ile operatör
32
- # aç/kapa mümkün). Gerçek Config dotted fetch'i çözer
33
- # ("subagents.maestro_gate"); düz Hash (test doubles) nested tutar —
34
- # dig fallback'i iki şekli de kapsar (dotted-key config gotchası).
33
+ # aç/kapa mümkün). config_get (Storage::Layout) gerçek Config'in
34
+ # dotted fetch'ini ve düz Hash'in nested (dig) şeklini tek evde
35
+ # çözer.
35
36
  cfg = @deps.shell&.config
36
- return true unless cfg.respond_to?(:fetch)
37
-
38
- raw = cfg.fetch("subagents.maestro_gate", nil)
39
- raw = cfg.dig("subagents", "maestro_gate") if raw.nil? && cfg.respond_to?(:dig)
37
+ raw = Rubik::Storage::Layout.config_get(cfg, "subagents.maestro_gate", nil)
40
38
  raw != false
41
39
  end
42
40
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../storage/layout"
4
+
3
5
  module Rubik
4
6
  module Orchestrator
5
7
  # Deep module (Candidate 03): the ONE home for provider-call recovery
@@ -70,14 +72,14 @@ module Rubik
70
72
  # compaction call site: nil config, fetch-less objects, and null YAML
71
73
  # sections all collapse to {}. One spelling replaces five.
72
74
  def compaction_config(config)
73
- cfg = config.respond_to?(:fetch) ? config.fetch(COMPACTION_CONFIG_KEY, {}) : {}
75
+ cfg = Rubik::Storage::Layout.config_get(config, COMPACTION_CONFIG_KEY, {})
74
76
  cfg.is_a?(Hash) ? cfg : {}
75
77
  end
76
78
 
77
79
  # ── implementation details (not interface) ────────────────────────
78
80
 
79
81
  def auto_recover?(config)
80
- config.respond_to?(:fetch) ? config.fetch(AUTO_RECOVER_KEY, true) : true
82
+ Rubik::Storage::Layout.config_get(config, AUTO_RECOVER_KEY, true)
81
83
  end
82
84
 
83
85
  def overflow_error?(err)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../storage/layout"
4
+
3
5
  module Rubik
4
6
  module Orchestrator
5
7
  # Provider-scoped configuration resolution for the orchestrator loop
@@ -50,7 +52,7 @@ module Rubik
50
52
  def llm_config(key, default)
51
53
  prov_key = registry_key
52
54
  if prov_key.is_a?(String) && !prov_key.empty? && @config.respond_to?(:fetch)
53
- val = @config.fetch("providers.#{prov_key}.#{key}", nil)
55
+ val = Rubik::Storage::Layout.config_get(@config, "providers.#{prov_key}.#{key}", nil)
54
56
  return val unless val.nil?
55
57
  end
56
58
 
@@ -60,7 +62,7 @@ module Rubik
60
62
  # KeyError instead of falling through.
61
63
  return default unless @config.respond_to?(:fetch)
62
64
 
63
- @config.fetch("llm.#{key}", default)
65
+ Rubik::Storage::Layout.config_get(@config, "llm.#{key}", default)
64
66
  rescue StandardError
65
67
  default
66
68
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../prompt"
4
+ require_relative "shell_deps"
5
+
6
+ module Rubik
7
+ module Orchestrator
8
+ # Loop-side resume window (issue #475, the Loop mirror of GoalSession's
9
+ # resume drive from #459). One collaborator owns the three concerns the
10
+ # resume path used to scatter across the run loop:
11
+ #
12
+ # 1. ctx.extend lifecycle — the §6 discipline: an in-process resume
13
+ # disposes the (already run()-disposed) old per-run sub-context
14
+ # BEFORE extending a fresh one, so repeated suspend/resume cycles do
15
+ # not leak dead children; the ensure-side close disposes exactly once.
16
+ # 2. Cross-process history prefix construction — the Σ-only window
17
+ # [system prompt] → [task] → [Σ row], shaped so the first resumed
18
+ # step's ledger refresh repositions (not duplicates) the Σ row.
19
+ # 3. goal_resumed marker stamping — downtime formatting and the
20
+ # 300-character reason discipline, testable without running a turn.
21
+ #
22
+ # System-prompt rendering and state fingerprinting stay on Loop (shared
23
+ # with run()'s prompt view, candidate 2 Phase 2); their VALUES are passed
24
+ # in, so this collaborator renders nothing itself.
25
+ class ResumeWindow
26
+ def initialize(deps)
27
+ @deps = ShellDeps.wrap(deps)
28
+ end
29
+
30
+ # ── ctx.extend lifecycle (§6) ─────────────────────────────────────
31
+ # In-process resume: the suspended run's sub-context is dead already
32
+ # (run()'s ensure disposed it) — dispose again defensively, then
33
+ # extend a fresh child for the resumed window.
34
+ def reopen_context!
35
+ @deps.reopen_run_context!
36
+ end
37
+
38
+ # Cross-process resume: a fresh Loop has no prior sub-context — just
39
+ # extend. Disposing first would be a no-op on nil, but the distinct
40
+ # entry point keeps the two resume paths' invariants explicit.
41
+ def open_context!
42
+ @deps.open_run_context!
43
+ end
44
+
45
+ # ensure-side dispose of the window's sub-context.
46
+ def close_context!
47
+ @deps.dispose_run_context!
48
+ end
49
+
50
+ # ── cross-process history prefix ──────────────────────────────────
51
+ # Builds [system-prompt row] → [task row] → [Σ row] on the loop's
52
+ # history. The system prompt CONTENT is supplied by the caller (Loop
53
+ # renders via its shared prompt view); this method owns the ordering
54
+ # and the Σ-row placement contract.
55
+ def build_cross_process_prefix!(goal_text:, ledger:, system_prompt:)
56
+ history << { role: "system", content: system_prompt }
57
+ history << { role: "user", content: Rubik::Prompt::ContextBuilder.format_user_task(goal_text) }
58
+ history << { role: "user", content: ledger.to_xml }
59
+ end
60
+
61
+ # ── goal_resumed marker ───────────────────────────────────────────
62
+ # Signature row (last link of the cross-process window): role "user"
63
+ # (EmergencyCompactor hard tier does not touch it), single-tag quoting
64
+ # discipline (ledger_row?/drift_row? mold). downtime is human-readable,
65
+ # "unknown" when unknowable. reason obeys the same 300-character
66
+ # discipline as the Σ stamp — 5xx bodies must not bloat history.
67
+ def stamp_resume_marker!(reason:, suspended_at:, attempt:)
68
+ esc = Rubik::Platform.method(:xml_escape)
69
+ history << { role: "user", content: "<goal_resumed downtime=\"#{esc.call(format_downtime(suspended_at))}\" " \
70
+ "reason=\"#{esc.call(reason.to_s.slice(0, 300))}\" attempt=\"#{attempt.to_i}\" " \
71
+ "steps_done=\"#{@deps.step_count}\" />" }
72
+ end
73
+
74
+ private
75
+
76
+ def history
77
+ @deps.history
78
+ end
79
+
80
+ # seconds → "45s" / "4m 12s" / "1h 03m"; honest "unknown" when the
81
+ # suspend time is unavailable (corrupt cross-process meta). Negative
82
+ # drift (clock skew) clamps to 0.
83
+ def format_downtime(suspended_at)
84
+ return "unknown" unless suspended_at.respond_to?(:to_i) && suspended_at.to_i.positive?
85
+
86
+ secs = [(Time.now - suspended_at).to_i, 0].max
87
+ mm, ss = secs.divmod(60)
88
+ hh, mm = mm.divmod(60)
89
+ return "#{hh}h #{mm.to_s.rjust(2, '0')}m" if hh.positive?
90
+ return "#{mm}m #{ss.to_s.rjust(2, '0')}s" if mm.positive?
91
+
92
+ "#{ss}s"
93
+ end
94
+ end
95
+ end
96
+ end
@@ -51,6 +51,22 @@ module Rubik
51
51
  @loop.history = new_history
52
52
  end
53
53
 
54
+ # ResumeWindow lifecycle commands (issue #475): Loop owns the
55
+ # @loop_context ivar; named commands instead of ivar reach-through,
56
+ # per the writes-policy note in the class comment.
57
+ def reopen_run_context!
58
+ dispose_run_context!
59
+ open_run_context!
60
+ end
61
+
62
+ def open_run_context!
63
+ @loop.loop_context = @loop.runtime_manager&.context&.extend
64
+ end
65
+
66
+ def dispose_run_context!
67
+ @loop.loop_context&.dispose
68
+ end
69
+
54
70
  def reset_invalid_tools!
55
71
  @loop.invalid_tool_count = 0
56
72
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../llm/chunk_assembler"
4
4
  require_relative "provider_recovery"
5
+ require_relative "../storage/layout"
5
6
 
6
7
  module Rubik
7
8
  module Orchestrator
@@ -88,7 +89,7 @@ module Rubik
88
89
  capture_reasoning = if @deps.respond_to?(:safe_config)
89
90
  @deps.safe_config("llm.capture_reasoning", true) != false
90
91
  elsif @deps.respond_to?(:config) && @deps.config.respond_to?(:fetch)
91
- @deps.config.fetch("llm.capture_reasoning", true) != false
92
+ Rubik::Storage::Layout.config_get(@deps.config, "llm.capture_reasoning", true) != false
92
93
  else
93
94
  true
94
95
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../errors"
3
4
  require_relative "budget"
4
5
  require_relative "notices"
5
6
  require_relative "conversation_id"
@@ -19,7 +20,9 @@ module Rubik
19
20
  # this spec — nil for explicitly detached work. A cancelled owner makes
20
21
  # the claim loops drop the spec before it ever starts.
21
22
  class DeferredSpec
22
- class StateError < StandardError; end
23
+ # Unified taxonomy (issue #448): subclasses Rubik::StateError so a
24
+ # caller can rescue any guarded-transition tripwire with one class.
25
+ class StateError < Rubik::StateError; end
23
26
 
24
27
  # The spec machine's every legal from->to edge (Lifecycle family
25
28
  # doctrine: one guarded transition per state machine, fail-loud on
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "monitor"
4
+ require_relative "../../storage/layout"
4
5
  require_relative "transcript_formatter"
5
6
  require_relative "start_gate"
6
7
  require_relative "storage_id"
@@ -73,21 +74,15 @@ module Rubik
73
74
  @tool_registry = tool_registry
74
75
  @provider = provider
75
76
  @buffer_manager = buffer_manager
76
- max_total_steps = if config.is_a?(Hash)
77
- config["subagents.max_total_steps"] || config.dig("subagents", "max_total_steps") ||
78
- Budget::DEFAULT_MAX_TOTAL_STEPS
79
- elsif config.respond_to?(:fetch)
80
- config.fetch("subagents.max_total_steps", Budget::DEFAULT_MAX_TOTAL_STEPS)
81
- else
82
- Budget::DEFAULT_MAX_TOTAL_STEPS
83
- end
77
+ max_total_steps = Rubik::Storage::Layout.config_get(config, "subagents.max_total_steps",
78
+ Budget::DEFAULT_MAX_TOTAL_STEPS)
84
79
  @budget = Budget.new(max_steps: max_steps, max_total_steps: max_total_steps)
85
80
  @root_dir = root_dir
86
81
  @config = config
87
82
  @event_bus = event_bus
88
83
  @provider_gate = provider_gate
89
- @context_limit = (context_limit || (@config.respond_to?(:fetch) ? @config.fetch("context.max_tokens", 32_768) : 32_768) || 32_768).to_i
90
- @max_output_tokens = (@config.respond_to?(:fetch) ? @config.fetch("llm.default_max_output_tokens", 4_096) : 4_096).to_i
84
+ @context_limit = (context_limit || Rubik::Storage::Layout.config_get(@config, "context.max_tokens", 32_768) || 32_768).to_i
85
+ @max_output_tokens = Rubik::Storage::Layout.config_get(@config, "llm.default_max_output_tokens", 4_096).to_i
91
86
 
92
87
  @state = :pending
93
88
  @history = []
@@ -165,6 +160,10 @@ module Rubik
165
160
  grant_budget!(additional_steps, note: ->(extra, total) { Budget.message_grant_note(extra, total) })
166
161
  end
167
162
  end
163
+ # Broadcast OUTSIDE the critical section (budget-pause discipline):
164
+ # wakes a questioner waiting on the answer. Spurious wakeups are
165
+ # harmless — the waiter rechecks @inbox and its state guards.
166
+ notify_answer!
168
167
  end
169
168
 
170
169
  def resume!(additional_steps: nil)
@@ -254,7 +253,7 @@ module Rubik
254
253
  end
255
254
 
256
255
  def pause_and_await_budget_expansion!(wait_timeout = nil)
257
- timeout = wait_timeout || (@config.respond_to?(:fetch) ? @config.fetch("subagents.budget_wait_timeout", 180) : 180) || 180
256
+ timeout = wait_timeout || Rubik::Storage::Layout.config_get(@config, "subagents.budget_wait_timeout", 180) || 180
258
257
  payload = nil
259
258
  @monitor.synchronize do
260
259
  return false unless executing?
@@ -295,7 +294,7 @@ module Rubik
295
294
 
296
295
  def pause_on_error!(error, wait_timeout = nil)
297
296
  timeout = wait_timeout ||
298
- (@config.respond_to?(:fetch) && @config.fetch("subagents.error_wait_timeout", nil)) ||
297
+ Rubik::Storage::Layout.config_get(@config, "subagents.error_wait_timeout", nil) ||
299
298
  Resilience::DEFAULT_ERROR_WAIT_TIMEOUT
300
299
  payload = nil
301
300
  @monitor.synchronize do
@@ -7,6 +7,7 @@ require "fileutils"
7
7
  require_relative "../../platform"
8
8
  require_relative "../../security/redactor"
9
9
  require_relative "../../storage/layout"
10
+ require_relative "../../tools/truncator"
10
11
 
11
12
  module Rubik
12
13
  module Orchestrator
@@ -304,7 +305,7 @@ module Rubik
304
305
 
305
306
  def clip(text, limit)
306
307
  value = Rubik::Security::Redactor.redact(text.to_s)
307
- value.length > limit ? "#{value[0, limit]}... [truncated #{value.length - limit} chars]" : value
308
+ Rubik::Tools::Truncator.clip(value, limit)
308
309
  end
309
310
 
310
311
  def config_get(config, key, default = nil)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../errors"
4
+
3
5
  module Rubik
4
6
  module Orchestrator
5
7
  module Subagent
@@ -12,7 +14,7 @@ module Rubik
12
14
  # public constants — adding a state means editing THIS file (plus the
13
15
  # kernel STATE_MAP, held by contract test).
14
16
  module Lifecycle
15
- class StateError < StandardError; end
17
+ class StateError < Rubik::StateError; end
16
18
 
17
19
  # Every legal from->to edge. A transition not listed here is a bug
18
20
  # — fail loud with both endpoints in the message. :disposed is
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../storage/layout"
4
+
3
5
  module Rubik
4
6
  module Orchestrator
5
7
  module Subagent
@@ -10,8 +12,10 @@ module Rubik
10
12
  # inbox for the maestro's send_message reply. The worker thread is the ONLY
11
13
  # inbox drainer (drain_inbox_into_history runs between steps) and it is
12
14
  # blocked inside the tool here, so the pop cannot race a drain.
13
- # Polling (not a cond var) follows the Supervisor#wait_for precedent:
14
- # kill hard-kills the thread, so perfect signaling is not required.
15
+ # The answer wait is a ConditionVariable guarded by the same @monitor
16
+ # that guards @inbox (mirroring Supervisor's @wait_cond idiom); the
17
+ # emitter (Instance#post_message) broadcasts OUTSIDE the critical
18
+ # section. Kill hard-kills the thread, so no signal is needed there.
15
19
  module Questioner
16
20
  DEFAULT_QUESTION_TIMEOUT = 300
17
21
 
@@ -37,15 +41,21 @@ module Rubik
37
41
  answer = nil
38
42
  begin
39
43
  # Emit OUTSIDE the monitor (budget-pause discipline): synchronous
40
- # bus dispatch finishes before the poll starts, so the answer can
44
+ # bus dispatch finishes before the wait starts, so the answer can
41
45
  # never land before we begin waiting for it.
42
46
  Notices.emit(@event_bus, Notices::QUESTION_EVENT, payload)
43
47
  deadline = Time.now + payload[:timeout]
48
+ @monitor.synchronize { @answer_cond ||= @monitor.new_cond }
44
49
  until answer
45
- @monitor.synchronize { answer = @inbox.pop(true)&.text unless @inbox.empty? }
50
+ @monitor.synchronize do
51
+ answer = @inbox.pop(true)&.text unless @inbox.empty?
52
+ # Sleep on the cond var INSIDE the monitor (wait atomically
53
+ # releases it), bounded by the deadline. The emitter's
54
+ # broadcast outside the critical section cannot be lost:
55
+ # a push can only interleave while we are in wait.
56
+ @answer_cond.wait([deadline - Time.now, 0].max) unless answer
57
+ end
46
58
  break if Time.now >= deadline || !(awaiting_human? || executing?)
47
-
48
- sleep 0.1
49
59
  end
50
60
  ensure
51
61
  # Guarded restore (also covers a raise or Thread#kill mid-wait):
@@ -71,9 +81,16 @@ module Rubik
71
81
  def question_timeout(override = nil)
72
82
  return override.to_f if override&.to_f&.positive?
73
83
 
74
- (@config.respond_to?(:fetch) ? @config.fetch("subagents.question_timeout", DEFAULT_QUESTION_TIMEOUT) : DEFAULT_QUESTION_TIMEOUT) ||
84
+ Rubik::Storage::Layout.config_get(@config, "subagents.question_timeout", DEFAULT_QUESTION_TIMEOUT) ||
75
85
  DEFAULT_QUESTION_TIMEOUT
76
86
  end
87
+
88
+ # Emitter-side signal (mirror of Supervisor#notify_wait!): the
89
+ # broadcast happens outside the caller's critical section, and a
90
+ # nil cond just means nobody is waiting on an answer.
91
+ def notify_answer!
92
+ @monitor.synchronize { @answer_cond&.broadcast }
93
+ end
77
94
  end
78
95
  end
79
96
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../../platform"
4
+ require_relative "../../storage/layout"
4
5
 
5
6
  module Rubik
6
7
  module Orchestrator
@@ -71,15 +72,12 @@ module Rubik
71
72
  end
72
73
 
73
74
  def reap_grace_seconds
74
- raw = @config.respond_to?(:fetch) ? @config.fetch("subagents.reap_finished_after", 300) : 300
75
- raw = 300 if raw.nil?
75
+ raw = Rubik::Storage::Layout.config_get(@config, "subagents.reap_finished_after", 300)
76
76
  raw.to_f <= 0 ? nil : raw.to_f
77
77
  end
78
78
 
79
79
  def tombstone_limit
80
- raw = @config.respond_to?(:fetch) ? @config.fetch("subagents.tombstone_limit", 25) : 25
81
- raw = 25 if raw.nil?
82
- raw.to_i
80
+ Rubik::Storage::Layout.config_get(@config, "subagents.tombstone_limit", 25).to_i
83
81
  end
84
82
 
85
83
  # FIFO taşma: en eski mezar taşı evict edilir ve cleanup ile
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../storage/layout"
4
+
3
5
  module Rubik
4
6
  module Orchestrator
5
7
  module Subagent
@@ -41,10 +43,10 @@ module Rubik
41
43
 
42
44
  def retry_transient(config: nil, gate: nil, max_retries: nil, backoff_base: nil, on_retry: nil, &block)
43
45
  retries_limit = max_retries ||
44
- (config.respond_to?(:fetch) && config.fetch("subagents.network_retry_count", nil)) ||
46
+ Rubik::Storage::Layout.config_get(config, "subagents.network_retry_count", nil) ||
45
47
  DEFAULT_NETWORK_RETRIES
46
48
  base_delay = backoff_base ||
47
- (config.respond_to?(:fetch) && config.fetch("subagents.network_backoff_base", nil)) ||
49
+ Rubik::Storage::Layout.config_get(config, "subagents.network_backoff_base", nil) ||
48
50
  DEFAULT_BACKOFF_BASE
49
51
 
50
52
  attempt = 0
@@ -116,10 +116,6 @@ module Rubik
116
116
  end
117
117
  end
118
118
 
119
- def latest_run(storage_id)
120
- @monitor.synchronize { (@entries[storage_id.to_s] || {}).keys.max }
121
- end
122
-
123
119
  # THE page rendering (tool action + rubik:// read share it, so the two
124
120
  # access paths cannot drift). The continuation is the paginated API
125
121
  # call — the reference itself always identifies the exact run.
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "transcript_formatter"
4
4
  require_relative "narration"
5
+ require_relative "../../storage/layout"
5
6
 
6
7
  module Rubik
7
8
  module Orchestrator
@@ -81,13 +82,7 @@ module Rubik
81
82
  effective = @system_prompt && !@system_prompt.strip.empty? ? "#{base}\n\nCustom Instructions:\n#{@system_prompt}" : base
82
83
 
83
84
  if @type_name == "self"
84
- shell_pref = if @config.respond_to?(:fetch)
85
- @config.fetch("sandbox.shell", "auto")
86
- elsif @config.is_a?(Hash)
87
- @config["sandbox.shell"] || "auto"
88
- else
89
- "auto"
90
- end
85
+ shell_pref = Rubik::Storage::Layout.config_get(@config, "sandbox.shell", "auto")
91
86
  if defined?(Rubik::Sandbox::ShellResolver)
92
87
  env_line = Rubik::Sandbox::ShellResolver.environment_line(shell_pref)
93
88
  effective = "#{effective}\n\n#{env_line}" if env_line && !env_line.empty?
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../errors"
4
+ require_relative "../../storage/layout"
5
+
3
6
  module Rubik
4
7
  module Orchestrator
5
8
  module Subagent
@@ -17,6 +20,12 @@ module Rubik
17
20
  Rubik::Coeffects::TASK_CANCELLATION
18
21
  ].freeze
19
22
 
23
+ # Start-gate release failure (issue #448): the worker start gate
24
+ # could not be released after a deferred activation — the spawn
25
+ # transaction rolls the instance back. Typed under Rubik::Error so
26
+ # callers can distinguish it from ordinary RuntimeError noise.
27
+ class StartGateReleaseError < Rubik::Error; end
28
+
20
29
  def spawn(role:, prompt:, type_name: "research", system_prompt: nil,
21
30
  parent_id: ConversationId::ROOT_ID, workspace: "inherit", model: "inherit",
22
31
  max_steps: nil, provider: nil, depends_on: nil, reserved_id: nil, owner: nil)
@@ -114,7 +123,7 @@ module Rubik
114
123
  prov, effective_model = resolve_provider_and_model(model)
115
124
  prov = provider if provider
116
125
 
117
- context_limit = @config.respond_to?(:fetch) ? @config.fetch("context.max_tokens", 32_768) : 32_768
126
+ context_limit = Rubik::Storage::Layout.config_get(@config, "context.max_tokens", 32_768)
118
127
  effective_steps = (max_steps || @default_max_steps).to_i
119
128
  child_config = if @config.is_a?(Rubik::Config)
120
129
  @config.extend(
@@ -141,7 +150,7 @@ module Rubik
141
150
  def activate_and_release_subagent!(inst:, child_ctx:, owner:, activation_init:)
142
151
  activate_instance!(inst, child_ctx: child_ctx, owner_token: owner,
143
152
  init_supervisor: activation_init, defer_start: true)
144
- raise "Subagent worker start gate could not be released." unless inst.release_start!
153
+ raise StartGateReleaseError, "Subagent worker start gate could not be released." unless inst.release_start!
145
154
  end
146
155
 
147
156
  def resolve_profile_snapshot(type_name:, role:, system_prompt:)
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require_relative "../../errors"
4
5
  require_relative "../../prompt"
5
6
  require_relative "../../platform"
6
7
  require_relative "../../security/redactor"
7
8
  require_relative "../../tools/truncator"
8
9
  require_relative "../../uris"
9
10
  require_relative "../provider_recovery"
11
+ require_relative "../../storage/layout"
10
12
  require_relative "../auto_apply"
11
13
  require_relative "../tool_call_id"
12
14
  require_relative "../tool_history"
@@ -30,6 +32,12 @@ module Rubik
30
32
  # spine (agent-turn-executor plan); this module keeps the invocation path, budget
31
33
  # mechanics, and the subagent dispatch.
32
34
  module StepRunner
35
+ # Provider contract violation (issue #448): the configured provider
36
+ # supports neither #complete nor #chat — a wiring bug, not a
37
+ # transient network condition. Typed under Rubik::Error so callers
38
+ # can distinguish it from ordinary RuntimeError noise.
39
+ class ProviderContractError < Rubik::Error; end
40
+
33
41
  include ContextCompaction
34
42
  include ToolDispatch
35
43
 
@@ -96,7 +104,7 @@ module Rubik
96
104
  end
97
105
 
98
106
  def auto_pause_on_error?
99
- @config.respond_to?(:fetch) ? @config.fetch("subagents.auto_pause_on_network_error", true) != false : true
107
+ Rubik::Storage::Layout.config_get(@config, "subagents.auto_pause_on_network_error", true) != false
100
108
  end
101
109
 
102
110
  def invoke_provider(opts)
@@ -114,7 +122,7 @@ module Rubik
114
122
  return @provider.complete(@history, effective_opts) if @provider.respond_to?(:complete)
115
123
  return @provider.chat(@history, **effective_opts) if @provider.respond_to?(:chat)
116
124
 
117
- raise "Provider #{@provider.class} does not support complete or chat"
125
+ raise ProviderContractError, "Provider #{@provider.class} does not support complete or chat"
118
126
  ensure
119
127
  @operations.finish(OperationTracker::MODEL)
120
128
  end
@@ -180,7 +188,7 @@ module Rubik
180
188
  # status/wait output, and long payloads are bounded.
181
189
  def tool_detail(args)
182
190
  json = Rubik::Platform.safe_json_generate(Rubik::Security::Redactor.mask_value(args))
183
- json.length > 120 ? "#{json[0, 120]}... [truncated]" : json
191
+ Rubik::Tools::Truncator.clip(json, 120, counted: false)
184
192
  end
185
193
 
186
194
  # The subagent's own spelling of the shared auto-apply policy (AutoApply
@@ -54,6 +54,10 @@ module Rubik
54
54
  @ended_at = Time.now
55
55
  @budget_cond.broadcast
56
56
  @error_cond.broadcast
57
+ # A questioner waiting on the answer cond must observe the state
58
+ # flip now, not at its deadline (the old 100 ms poll did this
59
+ # implicitly via its state-guard tick).
60
+ notify_answer!
57
61
  ensure
58
62
  @monitor.exit
59
63
  end
@@ -5,6 +5,7 @@ require "json"
5
5
  require "digest"
6
6
  require_relative "../../security/redactor"
7
7
  require_relative "../../storage/layout"
8
+ require_relative "../../tools/truncator"
8
9
 
9
10
  module Rubik
10
11
  module Orchestrator
@@ -139,7 +140,7 @@ module Rubik
139
140
  when "tool"
140
141
  t_name = entry[:tool_name] || entry["tool_name"]
141
142
  text = content.to_s
142
- truncated = text.length > 2000 ? "#{text.slice(0, 2000)}\n... [truncated #{text.length - 2000} chars]" : text
143
+ truncated = Rubik::Tools::Truncator.clip(text, 2000, separator: "\n")
143
144
  "[TOOL RESULT: #{t_name}]\n#{truncated}\n---"
144
145
  end
145
146
  end
@@ -45,10 +45,6 @@ module Rubik
45
45
  end
46
46
  alias unregister undo
47
47
 
48
- def to_type_def
49
- @type_def
50
- end
51
-
52
48
  def handle
53
49
  self
54
50
  end