rubik 0.9.1 → 0.11.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 (138) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +203 -1
  4. data/lib/rubik/adapter/base.rb +2 -1
  5. data/lib/rubik/adapter/process.rb +28 -67
  6. data/lib/rubik/ast/extractor.rb +2 -1
  7. data/lib/rubik/ast/treesitter_extractor.rb +2 -1
  8. data/lib/rubik/benchmark/vakanuvis_adoption.rb +12 -4
  9. data/lib/rubik/buffer/apply_contract.rb +3 -3
  10. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  11. data/lib/rubik/buffer/manager.rb +26 -120
  12. data/lib/rubik/config/deep_ops.rb +4 -1
  13. data/lib/rubik/config/defaults.rb +6 -3
  14. data/lib/rubik/config/schema.rb +8 -0
  15. data/lib/rubik/config/secret_policy.rb +198 -0
  16. data/lib/rubik/config/store.rb +2 -1
  17. data/lib/rubik/config.rb +13 -2
  18. data/lib/rubik/context/ignore_rules.rb +193 -0
  19. data/lib/rubik/context/scanner.rb +46 -161
  20. data/lib/rubik/diagnostics/collector.rb +0 -9
  21. data/lib/rubik/errors.rb +15 -0
  22. data/lib/rubik/event_bus.rb +2 -1
  23. data/lib/rubik/kernel/dispatcher.rb +12 -7
  24. data/lib/rubik/kernel/unit.rb +4 -1
  25. data/lib/rubik/llm/client.rb +6 -200
  26. data/lib/rubik/llm/connection_pool.rb +104 -0
  27. data/lib/rubik/llm/registry.rb +13 -9
  28. data/lib/rubik/llm/router.rb +2 -5
  29. data/lib/rubik/llm/sse_parser.rb +110 -0
  30. data/lib/rubik/log.rb +21 -0
  31. data/lib/rubik/lsp/circuit_breaker.rb +98 -0
  32. data/lib/rubik/lsp/client.rb +77 -273
  33. data/lib/rubik/lsp/client_provisioner.rb +49 -0
  34. data/lib/rubik/lsp/manager.rb +67 -96
  35. data/lib/rubik/lsp/transport.rb +365 -0
  36. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  37. data/lib/rubik/mcp/boot_run.rb +190 -0
  38. data/lib/rubik/mcp/manager.rb +55 -166
  39. data/lib/rubik/mcp/transport/stdio.rb +4 -1
  40. data/lib/rubik/orchestrator/auto_apply.rb +8 -6
  41. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  42. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +12 -1
  43. data/lib/rubik/orchestrator/goal_ledger/schema_vocabulary.rb +38 -0
  44. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +6 -1
  45. data/lib/rubik/orchestrator/goal_ledger.rb +1 -0
  46. data/lib/rubik/orchestrator/goal_session.rb +53 -39
  47. data/lib/rubik/orchestrator/loop.rb +28 -47
  48. data/lib/rubik/orchestrator/lsp_feedback.rb +1 -1
  49. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  50. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  51. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  52. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  53. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  54. data/lib/rubik/orchestrator/step_executor.rb +2 -1
  55. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  56. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  57. data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
  58. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  59. data/lib/rubik/orchestrator/subagent/notices.rb +2 -2
  60. data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
  61. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  62. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  63. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  64. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  65. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  66. data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
  67. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  68. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  69. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  70. data/lib/rubik/platform/io_drain.rb +5 -2
  71. data/lib/rubik/platform/process.rb +144 -54
  72. data/lib/rubik/platform.rb +2 -1
  73. data/lib/rubik/plugin/manager.rb +2 -1
  74. data/lib/rubik/plugin/storage.rb +8 -8
  75. data/lib/rubik/repl/commands/agent_context.rb +8 -7
  76. data/lib/rubik/repl/commands/agent_execution.rb +13 -32
  77. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  78. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  79. data/lib/rubik/repl/commands/base.rb +47 -2
  80. data/lib/rubik/repl/commands/buffer.rb +5 -4
  81. data/lib/rubik/repl/commands/lsp.rb +2 -1
  82. data/lib/rubik/repl/commands/pruner.rb +2 -1
  83. data/lib/rubik/repl/commands/secrets.rb +151 -0
  84. data/lib/rubik/repl/commands/workspace.rb +60 -150
  85. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  86. data/lib/rubik/repl/config_wiring.rb +8 -6
  87. data/lib/rubik/repl/hooks.rb +2 -1
  88. data/lib/rubik/repl/input_execution.rb +162 -79
  89. data/lib/rubik/repl/input_loop.rb +2 -1
  90. data/lib/rubik/repl/lifecycle.rb +60 -210
  91. data/lib/rubik/repl/notice_router.rb +319 -0
  92. data/lib/rubik/repl/paste_support.rb +3 -1
  93. data/lib/rubik/repl/presentation.rb +5 -3
  94. data/lib/rubik/repl/pruner/gate.rb +3 -1
  95. data/lib/rubik/repl/pruner/harness.rb +2 -1
  96. data/lib/rubik/repl/shell.rb +4 -3
  97. data/lib/rubik/repl/tee_io.rb +2 -1
  98. data/lib/rubik/repl/turn_recorder.rb +6 -5
  99. data/lib/rubik/runtime/ast_service.rb +2 -1
  100. data/lib/rubik/runtime/discovery.rb +3 -2
  101. data/lib/rubik/runtime/loop_service.rb +4 -3
  102. data/lib/rubik/runtime/manifest_store.rb +9 -1
  103. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  104. data/lib/rubik/runtime/overlay_storage.rb +109 -163
  105. data/lib/rubik/runtime/task_manager.rb +64 -25
  106. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  107. data/lib/rubik/sandbox/runner.rb +21 -19
  108. data/lib/rubik/security/audit_log.rb +2 -1
  109. data/lib/rubik/security/command_gate.rb +126 -243
  110. data/lib/rubik/security/guard.rb +7 -3
  111. data/lib/rubik/security/secret_store.rb +492 -0
  112. data/lib/rubik/security/segment_report.rb +161 -0
  113. data/lib/rubik/security/synthesis_gate.rb +8 -11
  114. data/lib/rubik/session/manager.rb +7 -1
  115. data/lib/rubik/session/recorder.rb +2 -1
  116. data/lib/rubik/skills/manager.rb +4 -4
  117. data/lib/rubik/storage/search.rb +49 -0
  118. data/lib/rubik/storage/store.rb +13 -20
  119. data/lib/rubik/storage/transcript_indexer.rb +3 -2
  120. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  121. data/lib/rubik/storage/vakanuvis.rb +2 -1
  122. data/lib/rubik/tools/base.rb +22 -0
  123. data/lib/rubik/tools/builtins/files.rb +3 -15
  124. data/lib/rubik/tools/builtins/goal_ledger.rb +12 -1
  125. data/lib/rubik/tools/builtins/grep.rb +1 -6
  126. data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
  127. data/lib/rubik/tools/builtins/synthesis.rb +3 -3
  128. data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
  129. data/lib/rubik/tools/preflight_runner.rb +2 -1
  130. data/lib/rubik/tools/truncator.rb +48 -36
  131. data/lib/rubik/tools/uri_resolver.rb +3 -2
  132. data/lib/rubik/ui/banner.rb +4 -3
  133. data/lib/rubik/ui/capability.rb +1 -0
  134. data/lib/rubik/ui/spinner.rb +3 -1
  135. data/lib/rubik/ui/statusline.rb +6 -5
  136. data/lib/rubik/version.rb +1 -1
  137. data/lib/rubik.rb +4 -5
  138. metadata +20 -1
@@ -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 = []
@@ -258,7 +253,7 @@ module Rubik
258
253
  end
259
254
 
260
255
  def pause_and_await_budget_expansion!(wait_timeout = nil)
261
- 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
262
257
  payload = nil
263
258
  @monitor.synchronize do
264
259
  return false unless executing?
@@ -299,7 +294,7 @@ module Rubik
299
294
 
300
295
  def pause_on_error!(error, wait_timeout = nil)
301
296
  timeout = wait_timeout ||
302
- (@config.respond_to?(:fetch) && @config.fetch("subagents.error_wait_timeout", nil)) ||
297
+ Rubik::Storage::Layout.config_get(@config, "subagents.error_wait_timeout", nil) ||
303
298
  Resilience::DEFAULT_ERROR_WAIT_TIMEOUT
304
299
  payload = nil
305
300
  @monitor.synchronize do
@@ -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,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../log"
3
4
  require_relative "../../events"
4
5
  require_relative "../../platform"
5
6
  require_relative "budget"
@@ -69,7 +70,7 @@ module Rubik
69
70
  rescue StandardError => e
70
71
  raise if fallback.nil?
71
72
 
72
- warn("#{fallback} (#{e.class}: #{e.message})")
73
+ Rubik::Log.warn(:orchestrator, "#{fallback} (#{e.class}: #{e.message})")
73
74
  end
74
75
 
75
76
  def normalize(event_name, payload)
@@ -191,7 +192,6 @@ module Rubik
191
192
  end
192
193
 
193
194
  DEFAULT_WAIT_TIMEOUT = 30
194
-
195
195
  def render_ids(ids) = "[#{Array(ids).map { |id| "'#{id}'" }.join(', ')}]"
196
196
 
197
197
  def wait_hint(ids, timeout: DEFAULT_WAIT_TIMEOUT) = "manage_subagents(action: 'wait', conversation_ids: #{render_ids(ids)}, timeout: #{timeout})"
@@ -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
@@ -79,7 +81,7 @@ module Rubik
79
81
  def question_timeout(override = nil)
80
82
  return override.to_f if override&.to_f&.positive?
81
83
 
82
- (@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) ||
83
85
  DEFAULT_QUESTION_TIMEOUT
84
86
  end
85
87
 
@@ -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
@@ -3,6 +3,7 @@
3
3
  require "fileutils"
4
4
  require "json"
5
5
  require "digest"
6
+ require_relative "../../log"
6
7
  require_relative "../../security/redactor"
7
8
  require_relative "../../storage/layout"
8
9
  require_relative "../../tools/truncator"
@@ -56,7 +57,7 @@ module Rubik
56
57
  File.write(file_path, text)
57
58
  file_path
58
59
  rescue StandardError => e
59
- warn("[Subagent transcript] Failed to save #{file_path}: #{e.class}: #{e.message}")
60
+ Rubik::Log.warn(:orchestrator, "[Subagent transcript] Failed to save #{file_path}: #{e.class}: #{e.message}")
60
61
  nil
61
62
  end
62
63
 
@@ -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
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "monitor"
4
+
3
5
  require_relative "../coeffects"
4
6
  require_relative "../tools/result"
5
7
 
@@ -11,6 +13,11 @@ module Rubik
11
13
  # it keeps running in the background and its result is discarded — so
12
14
  # the budget bounds turn latency, not resource use. Budget off (nil)
13
15
  # yields inline: byte-identical to the unwrapped seam.
16
+ #
17
+ # Opt-in bound on the leak itself (issue #449): with `tools.timeout_kill_after`
18
+ # set, abandoned workers are tracked in a Monitor-synchronized registry and a
19
+ # reaper thread force-kills any worker still running once the grace elapses.
20
+ # Knob unset ⇒ pure abandonment, unchanged behavior.
14
21
  module ToolTimeout
15
22
  # Raised in the CALLER thread when the join budget expires. The
16
23
  # dispatch seam's rescue converts it into an honest error Result so
@@ -27,8 +34,17 @@ module Rubik
27
34
  end
28
35
 
29
36
  ENV_KEY = "RUBIK_TOOLS_CALL_TIMEOUT"
37
+ KILL_AFTER_CONFIG_KEY = "tools.timeout_kill_after"
30
38
  OFF = :off
31
39
 
40
+ # Grace-then-kill registry (issue #449): abandoned workers whose
41
+ # operator opted into the kill policy via `tools.timeout_kill_after`.
42
+ # Keyed by the worker thread; at most one reaper thread services the
43
+ # whole registry, exiting whenever it drains.
44
+ REGISTRY_MONITOR = Monitor.new
45
+ @registry = {} # worker Thread => {thread:, deadline:, tool_name:, budget:, grace:}
46
+ @reaper = nil
47
+
32
48
  module_function
33
49
 
34
50
  # Budget resolution precedence: an explicit env disable wins, then the
@@ -62,6 +78,12 @@ module Rubik
62
78
  # return. A kill landing mid-body still runs the ensure, which reads
63
79
  # as a nil result — the inline-parity residual this seam accepts.
64
80
  done = false
81
+ # Set by the caller when the join budget expires; read from the
82
+ # worker's ensure to log the late completion (see below). A worker
83
+ # finishing inside the join-expiry→flag window is not logged — the
84
+ # residual is bounded by the join granularity and costs nothing but
85
+ # one missing warn line.
86
+ abandoned = false
65
87
  t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
66
88
  worker = Thread.new do
67
89
  result = yield
@@ -69,16 +91,29 @@ module Rubik
69
91
  # raise-path semantics for ANY exception class, not just
70
92
  # StandardError. The re-raise carries the worker-side backtrace;
71
93
  # dispatch-site frames are not prepended (the tool's own failure
72
- # site is what matters for debugging).
94
+ # site is what production debugging cares about).
73
95
  rescue Exception => e # rubocop:disable Lint/RescueException
74
96
  error = e
75
97
  ensure
98
+ if abandoned
99
+ late = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
100
+ Rubik::Log.warn(:tools,
101
+ "tool '#{name}' call was abandoned at the #{budget}s budget but finished late " \
102
+ "after #{late.round(1)}s; its outcome (#{error ? "raise #{error.class}" : 'a result'}) was discarded")
103
+ end
76
104
  done = true
77
105
  end
78
106
  worker.name = "rubik-tool-#{name}"
79
107
 
80
108
  unless worker.join(budget)
109
+ abandoned = true
81
110
  elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
111
+ # Opt-in grace-then-kill (issue #449): with `tools.timeout_kill_after`
112
+ # set, the abandoned worker is handed to the reaper, which force-kills
113
+ # it once the grace elapses. Knob unset ⇒ pure abandonment (the
114
+ # late-completion warn below remains the only trail).
115
+ grace = kill_after_for(config)
116
+ register_for_kill(worker, tool_name: name, budget: budget, grace: grace) if grace
82
117
  raise BudgetExceeded.new(tool_name: name, budget_seconds: budget, elapsed_seconds: elapsed)
83
118
  end
84
119
  raise error if error
@@ -158,6 +193,91 @@ module Rubik
158
193
  nil
159
194
  end
160
195
 
196
+ # Grace-then-kill reaper knob at the config boundary (issue #449): the
197
+ # seconds an abandoned worker may keep running past its budget before
198
+ # the reaper force-kills it. The value IS the grace period — directly
199
+ # operator-set (e.g. `tools.timeout_kill_after: 30`), not derived from
200
+ # the budget. nil/absent = off, byte-identical to the pure-abandonment
201
+ # behavior. Mirrors resolve_config_budget: an invalid or non-positive
202
+ # value warns and lands on off — the knob must never fail a turn.
203
+ def kill_after_for(config)
204
+ raw = config&.fetch(KILL_AFTER_CONFIG_KEY, nil)
205
+ return nil if raw.nil?
206
+
207
+ value = parse_seconds(raw)
208
+ return value if value&.positive?
209
+
210
+ Rubik::Log.warn(:tools, "ignoring invalid #{KILL_AFTER_CONFIG_KEY} #{raw.inspect} (expected a positive number); treating as off")
211
+ nil
212
+ end
213
+
214
+ # Hands an abandoned worker to the grace-then-kill reaper and ensures
215
+ # exactly one reaper thread is parked on the registry.
216
+ def register_for_kill(thread, tool_name:, budget:, grace:)
217
+ REGISTRY_MONITOR.synchronize do
218
+ @registry[thread] = { thread: thread, deadline: Time.now + grace,
219
+ tool_name: tool_name, budget: budget, grace: grace }
220
+ @reaper = spawn_reaper if @reaper.nil? || !@reaper.alive?
221
+ end
222
+ end
223
+
224
+ # Sleeps until the earliest deadline, reaps, repeats; exits as soon as
225
+ # the registry drains so no reaper thread lingers between timeouts.
226
+ def spawn_reaper
227
+ Thread.new do
228
+ Thread.current.name = "rubik-tool-reaper"
229
+ loop do
230
+ deadline = REGISTRY_MONITOR.synchronize do
231
+ earliest = @registry.values.map { |entry| entry[:deadline] }.min
232
+ # Announce the exit under the lock so the next registration
233
+ # spawns a fresh reaper instead of trusting a dying thread.
234
+ @reaper = nil if earliest.nil?
235
+ earliest
236
+ end
237
+ break if deadline.nil?
238
+
239
+ delay = deadline - Time.now
240
+ sleep(delay) if delay.positive?
241
+ reap_expired
242
+ end
243
+ end
244
+ end
245
+
246
+ # Removes every entry whose grace has elapsed: dead workers need no
247
+ # kill (the abandoned-worker ensure already logged their late
248
+ # completion), still-running ones are force-killed and the kill is
249
+ # logged under the orchestrator's "tools" tag.
250
+ def reap_expired
251
+ expired = REGISTRY_MONITOR.synchronize do
252
+ now = Time.now
253
+ due = @registry.select { |_thread, entry| entry[:deadline] <= now }
254
+ due.each_key { |thread| @registry.delete(thread) }
255
+ due.values
256
+ end
257
+ expired.each do |entry|
258
+ next unless entry[:thread].alive?
259
+
260
+ begin
261
+ entry[:thread].kill
262
+ rescue StandardError
263
+ nil
264
+ end
265
+ Rubik::Log.warn(:tools, "tool '#{entry[:tool_name]}' worker was still running past its " \
266
+ "#{entry[:budget]}s budget after the #{entry[:grace]}s grace " \
267
+ "(tools.timeout_kill_after); killed it")
268
+ end
269
+ end
270
+
271
+ # Test/observability doors: a snapshot of the pending kill registry and
272
+ # whether a reaper thread is currently parked on it.
273
+ def pending_kills
274
+ REGISTRY_MONITOR.synchronize { @registry.values.map(&:dup) }
275
+ end
276
+
277
+ def reaper_alive?
278
+ REGISTRY_MONITOR.synchronize { @reaper.is_a?(Thread) && @reaper.alive? }
279
+ end
280
+
161
281
  # Canonical tool name => seconds. 0/false disables the budget for that
162
282
  # tool (the per-tool escape hatch); a positive value replaces the
163
283
  # global budget; garbage warns and inherits. Lookup uses key presence,
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../log"
4
+
3
5
  module Rubik
4
6
  module Platform
5
7
  # Continuously drains a child-process IO (stderr) so a chatty child
6
8
  # never fills the OS pipe buffer (~64KB) and deadlocks writing
7
9
  # diagnostics while the parent waits on stdout. Discards content; the
8
- # optional on_line observer is a future debug-forwarding hook.
10
+ # optional on_line observer forwards each drained line when given
11
+ # (implemented and test-covered; currently unused in production).
9
12
  # Lifecycle contract mirrors the existing reader threads (MCP stdio,
10
13
  # LSP stdout): kill + bounded join, never raises from #stop.
11
14
  class IoDrain
@@ -33,7 +36,7 @@ module Rubik
33
36
  # time it is the watchdog dying while the child may still flood
34
37
  # the pipe — the exact deadlock this class exists to prevent —
35
38
  # so it must leave a trace.
36
- warn "[rubik] stderr drain died unexpectedly (#{e.class}: #{e.message})" unless @stopped
39
+ Rubik::Log.warn(:platform, "[rubik] stderr drain died unexpectedly (#{e.class}: #{e.message})") unless @stopped
37
40
  end
38
41
  self
39
42
  end