rubik 0.7.1 → 0.8.1

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 (78) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +205 -1
  4. data/lib/rubik/ast/typescript_extractor.rb +2 -2
  5. data/lib/rubik/config/deep_ops.rb +14 -0
  6. data/lib/rubik/config/defaults.rb +5 -4
  7. data/lib/rubik/config/env_loader.rb +88 -0
  8. data/lib/rubik/config/model_catalog.rb +13 -0
  9. data/lib/rubik/config/pruner.rb +34 -0
  10. data/lib/rubik/config/schema.rb +163 -0
  11. data/lib/rubik/config/scoped.rb +108 -0
  12. data/lib/rubik/config/store.rb +19 -8
  13. data/lib/rubik/config.rb +129 -53
  14. data/lib/rubik/diagnostics/parser.rb +2 -2
  15. data/lib/rubik/kernel/dispatcher.rb +19 -23
  16. data/lib/rubik/kernel/init.rb +22 -18
  17. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +3 -9
  18. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +3 -9
  19. data/lib/rubik/orchestrator/step_executor.rb +12 -59
  20. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +51 -2
  21. data/lib/rubik/orchestrator/subagent/dependency.rb +20 -20
  22. data/lib/rubik/orchestrator/subagent/execution_reads.rb +1 -1
  23. data/lib/rubik/orchestrator/subagent/journal.rb +3 -9
  24. data/lib/rubik/orchestrator/subagent/lifecycle.rb +10 -1
  25. data/lib/rubik/orchestrator/subagent/paging.rb +49 -0
  26. data/lib/rubik/orchestrator/subagent/reaper.rb +1 -3
  27. data/lib/rubik/orchestrator/subagent/redeploy_transaction.rb +99 -0
  28. data/lib/rubik/orchestrator/subagent/result_archive.rb +12 -22
  29. data/lib/rubik/orchestrator/subagent/reuse.rb +37 -31
  30. data/lib/rubik/orchestrator/subagent/scoped_registry.rb +14 -23
  31. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +13 -3
  32. data/lib/rubik/orchestrator/subagent/supervisor.rb +6 -2
  33. data/lib/rubik/orchestrator/subagent/task_ownership.rb +2 -2
  34. data/lib/rubik/orchestrator/subagent/tool_dispatch.rb +12 -49
  35. data/lib/rubik/orchestrator/subagent/transcript_reader.rb +14 -13
  36. data/lib/rubik/orchestrator/turn_executor.rb +9 -4
  37. data/lib/rubik/repl/commands/subagents.rb +3 -2
  38. data/lib/rubik/repl/commands/workspace.rb +14 -8
  39. data/lib/rubik/repl/lifecycle.rb +11 -0
  40. data/lib/rubik/repl/pruner/service.rb +4 -9
  41. data/lib/rubik/repl/service_catalog.rb +2 -1
  42. data/lib/rubik/runtime/context.rb +6 -1
  43. data/lib/rubik/runtime/fiber.rb +17 -5
  44. data/lib/rubik/runtime/manager.rb +19 -12
  45. data/lib/rubik/sandbox/runner.rb +28 -13
  46. data/lib/rubik/storage/layout.rb +14 -3
  47. data/lib/rubik/storage/render.rb +1 -1
  48. data/lib/rubik/storage/vakanuvis/distill_gate.rb +3 -3
  49. data/lib/rubik/storage/vakanuvis/harness.rb +4 -65
  50. data/lib/rubik/storage/vakanuvis/orientation.rb +1 -2
  51. data/lib/rubik/storage/vakanuvis/recall.rb +84 -0
  52. data/lib/rubik/storage/vakanuvis.rb +1 -0
  53. data/lib/rubik/tools/base.rb +65 -0
  54. data/lib/rubik/tools/builtins/define_subagent.rb +6 -0
  55. data/lib/rubik/tools/builtins/dmesg.rb +5 -5
  56. data/lib/rubik/tools/builtins/exec.rb +1 -2
  57. data/lib/rubik/tools/builtins/files.rb +19 -25
  58. data/lib/rubik/tools/builtins/goal_ledger.rb +2 -2
  59. data/lib/rubik/tools/builtins/grep.rb +8 -8
  60. data/lib/rubik/tools/builtins/interactive.rb +7 -3
  61. data/lib/rubik/tools/builtins/lifecycle.rb +2 -1
  62. data/lib/rubik/tools/builtins/lsp.rb +2 -2
  63. data/lib/rubik/tools/builtins/pruner.rb +2 -2
  64. data/lib/rubik/tools/builtins/runtime.rb +1 -1
  65. data/lib/rubik/tools/builtins/send_message.rb +20 -3
  66. data/lib/rubik/tools/builtins/skill.rb +1 -1
  67. data/lib/rubik/tools/builtins/storage.rb +1 -1
  68. data/lib/rubik/tools/builtins/subagent.rb +14 -1
  69. data/lib/rubik/tools/builtins/subagent_lifecycle.rb +5 -3
  70. data/lib/rubik/tools/builtins/symbols.rb +4 -4
  71. data/lib/rubik/tools/builtins/task.rb +1 -1
  72. data/lib/rubik/tools/builtins/vakanuvis.rb +2 -2
  73. data/lib/rubik/tools/catalog.rb +19 -0
  74. data/lib/rubik/tools/declaration.rb +15 -2
  75. data/lib/rubik/tools/result_normalizer.rb +115 -0
  76. data/lib/rubik/tools/truncator.rb +3 -3
  77. data/lib/rubik/version.rb +1 -1
  78. metadata +9 -1
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ class Config
5
+ # Scoped child configuration (AGENTS.md §1.C Unified Context Paradigm).
6
+ # Created via parent_config.extend(overrides = {}).
7
+ # Reads fall back to the parent config unless overridden in the child.
8
+ # Writes modify only the child's local scope, never polluting parent state.
9
+ # Disk persistence from child configs is fail-closed (raises ArgumentError).
10
+ class Scoped < Config
11
+ attr_reader :parent, :overrides
12
+
13
+ # rubocop:disable-next Lint/MissingSuper
14
+ def initialize(parent, overrides = {})
15
+ @parent = parent
16
+ @root_dir = parent.root_dir
17
+ @home_dir = parent.home_dir
18
+ @monitor = Monitor.new
19
+ @warnings = []
20
+ @listeners = []
21
+ @event_bus = parent.event_bus
22
+ @overrides = {}
23
+ return unless overrides.is_a?(Hash)
24
+
25
+ overrides.each do |k, v|
26
+ typed = v.is_a?(Hash) ? deep_stringify_keys(deep_dup(v)) : Schema.coerce(k.to_s, parse_scalar_value(deep_dup(v)))
27
+ Schema.validate_value!(k, typed, strict: true)
28
+ # Hash overrides get the same leaf coercion + validation that
29
+ # dotted-key writes go through, so extend(ui: {verbosity: 'x'})
30
+ # is as strict as set('ui.verbosity', 'x').
31
+ if typed.is_a?(Hash)
32
+ Schema.coerce_nested!(typed, k.to_s)
33
+ section = DEFAULT_CONFIG[k.to_s]
34
+ validate_schema(typed, section, k.to_s) if section.is_a?(Hash) && !section.empty?
35
+ end
36
+ deep_set_key(@overrides, k.to_s, typed)
37
+ end
38
+ end
39
+
40
+ def fetch(key, default = nil)
41
+ @monitor.synchronize do
42
+ keys = key.to_s.split(".")
43
+ val = keys.reduce(@overrides) do |acc, k|
44
+ break nil unless acc.is_a?(Hash) && acc.key?(k)
45
+
46
+ acc[k]
47
+ end
48
+ return val unless val.nil?
49
+
50
+ @parent.fetch(key, default)
51
+ end
52
+ end
53
+
54
+ def [](key)
55
+ fetch(key)
56
+ end
57
+
58
+ def set(key, value, persist: nil)
59
+ raise ArgumentError, "Scoped child configurations cannot persist to disk (scope: #{persist.inspect})" if persisting?(persist)
60
+
61
+ @monitor.synchronize do
62
+ old_val = fetch(key)
63
+ typed_val = parse_scalar_value(value)
64
+ typed_val = Schema.coerce(key, typed_val)
65
+ Schema.validate_value!(key, typed_val, strict: true)
66
+ if typed_val.is_a?(Hash)
67
+ typed_val = deep_dup(typed_val)
68
+ Schema.coerce_nested!(typed_val, key.to_s)
69
+ section = DEFAULT_CONFIG[key.to_s]
70
+ validate_schema(typed_val, section, key.to_s) if section.is_a?(Hash) && !section.empty?
71
+ end
72
+ deep_set_key(@overrides, key, typed_val)
73
+ notify_change(key, typed_val, old_val) if old_val != typed_val
74
+ { ok: true }
75
+ end
76
+ end
77
+
78
+ def delete(key, persist: nil)
79
+ raise ArgumentError, "Scoped child configurations cannot persist to disk" if persisting?(persist)
80
+
81
+ @monitor.synchronize do
82
+ old_val = fetch(key)
83
+ deep_delete(@overrides, key)
84
+ new_val = fetch(key)
85
+ if old_val != new_val
86
+ notify_change(key, new_val, old_val)
87
+ @event_bus&.emit(:config_deleted, key: key.to_s, old_val: old_val)
88
+ end
89
+ { ok: true }
90
+ end
91
+ end
92
+
93
+ def model_spec(role = :default)
94
+ role_str = role.to_s.downcase
95
+ spec = fetch("models.#{role_str}")
96
+ return spec if spec.is_a?(Hash) && spec["provider"] && spec["name"]
97
+
98
+ @parent.model_spec(role)
99
+ end
100
+
101
+ def data
102
+ @monitor.synchronize do
103
+ deep_merge(@parent.data, @overrides)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -3,6 +3,7 @@
3
3
  require "yaml"
4
4
  require "yaml/store"
5
5
  require "fileutils"
6
+ require_relative "pruner"
6
7
 
7
8
  module Rubik
8
9
  class Config
@@ -71,16 +72,17 @@ module Rubik
71
72
  store.transaction do
72
73
  # Read existing data on disk
73
74
  existing = store.roots.to_h { |k| [k, store[k]] }
74
- merged = deep_merge(existing, deep_stringify_keys(@data))
75
+ scope_data = (scope_sym == :project ? @project_layer : @global_layer) || {}
76
+ merged = deep_merge(existing, deep_stringify_keys(scope_data))
75
77
 
76
78
  # Explicitly overwrite model and role specs so stale keys from prior providers are not retained
77
- if @data["models"].is_a?(Hash)
79
+ if scope_data["models"].is_a?(Hash)
78
80
  merged["models"] ||= {}
79
- @data["models"].each do |r_name, r_spec|
81
+ scope_data["models"].each do |r_name, r_spec|
80
82
  merged["models"][r_name.to_s] = deep_dup(r_spec)
81
83
  end
82
84
  end
83
- merged["model"] = deep_dup(@data["model"]) if @data["model"].is_a?(Hash)
85
+ merged["model"] = deep_dup(scope_data["model"]) if scope_data["model"].is_a?(Hash)
84
86
 
85
87
  # Replay nested deletions so validate_schema sees what will be written.
86
88
  # A blocked path means the deletion cannot reach disk and would
@@ -89,11 +91,14 @@ module Rubik
89
91
  unapplied = Array(deletions).select { |path| deep_delete(merged, path) == :blocked }
90
92
  validation_error = "could not apply deletion(s): #{unapplied.join(', ')}" if unapplied.any?
91
93
 
94
+ # Prune redundant defaults against DEFAULT_CONFIG on save
95
+ pruned = Pruner.prune(merged, DEFAULT_CONFIG)
96
+
92
97
  # Schema validation
93
98
  unless validation_error
94
99
  begin
95
100
  validate_schema(@data)
96
- validate_schema(merged)
101
+ validate_schema(pruned)
97
102
  rescue ArgumentError => e
98
103
  validation_error = e.message
99
104
  end
@@ -101,10 +106,16 @@ module Rubik
101
106
  store.abort if validation_error
102
107
 
103
108
  # Remove deleted roots
104
- (store.roots - merged.keys).each { |r| store.delete(r) }
109
+ (store.roots - pruned.keys).each { |r| store.delete(r) }
110
+
111
+ # Save pruned entries
112
+ pruned.each { |k, v| store[k] = v }
105
113
 
106
- # Save merged entries
107
- merged.each { |k, v| store[k] = v }
114
+ if scope_sym == :project
115
+ @project_layer = deep_dup(pruned)
116
+ else
117
+ @global_layer = deep_dup(pruned)
118
+ end
108
119
  end
109
120
 
110
121
  if validation_error
data/lib/rubik/config.rb CHANGED
@@ -1,9 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "monitor"
3
4
  require_relative "config/defaults"
4
5
  require_relative "config/deep_ops"
5
6
  require_relative "config/store"
6
7
  require_relative "config/model_catalog"
8
+ require_relative "config/pruner"
9
+ require_relative "config/schema"
10
+ require_relative "config/env_loader"
11
+ require_relative "config/scoped"
7
12
 
8
13
  module Rubik
9
14
  class Config
@@ -14,25 +19,24 @@ module Rubik
14
19
 
15
20
  DEFAULT_CONFIG = Defaults::DEFAULT_CONFIG
16
21
 
17
- attr_reader :data, :warnings, :root_dir, :home_dir
22
+ attr_reader :data, :warnings, :root_dir, :home_dir,
23
+ :defaults_layer, :global_layer, :project_layer, :env_layer, :session_layer
18
24
  attr_accessor :event_bus
19
25
 
20
26
  def initialize(root_dir: Dir.pwd, home_dir: nil)
27
+ @monitor = Monitor.new
21
28
  @root_dir = File.expand_path(root_dir)
22
- effective_home = home_dir || (if Rubik.const_defined?(:Platform)
23
- Rubik::Platform.home_dir
24
- else
25
- begin
26
- Dir.home
27
- rescue ArgumentError
28
- (ENV["USERPROFILE"] || ENV["HOME"] || ".")
29
- end
30
- end)
29
+ effective_home = home_dir || (Rubik.const_defined?(:Platform) ? Rubik::Platform.home_dir : (ENV["USERPROFILE"] || ENV["HOME"] || "."))
31
30
  @home_dir = File.expand_path(effective_home)
32
31
  @warnings = []
33
32
  @stores = {}
34
33
  @listeners = []
35
34
  @event_bus = nil
35
+ @session_layer = {}
36
+ @defaults_layer = deep_dup(DEFAULT_CONFIG)
37
+ @global_layer = {}
38
+ @project_layer = {}
39
+ @env_layer = {}
36
40
  ensure_rubik_directories
37
41
  @data = load_merged_config
38
42
  end
@@ -40,19 +44,25 @@ module Rubik
40
44
  def on_change(prefix = nil, &block)
41
45
  return proc {} unless block_given?
42
46
 
43
- norm_prefix = prefix&.to_s&.sub(/\.\z/, "")
44
- entry = { prefix: norm_prefix, callback: block }
45
- @listeners << entry
46
- proc { @listeners.delete(entry) }
47
+ @monitor.synchronize do
48
+ norm_prefix = prefix&.to_s&.sub(/\.\z/, "")
49
+ entry = { prefix: norm_prefix, callback: block }
50
+ @listeners << entry
51
+ proc do
52
+ @monitor.synchronize { @listeners.delete(entry) }
53
+ end
54
+ end
47
55
  end
48
56
 
49
57
  # Retrieves a configuration value by dot-separated key path.
50
58
  def fetch(key, default = nil)
51
- keys = key.to_s.split(".")
52
- keys.reduce(@data) do |acc, k|
53
- return default unless acc.is_a?(Hash) && acc.key?(k)
59
+ @monitor.synchronize do
60
+ keys = key.to_s.split(".")
61
+ keys.reduce(@data) do |acc, k|
62
+ return default unless acc.is_a?(Hash) && acc.key?(k)
54
63
 
55
- acc[k]
64
+ acc[k]
65
+ end
56
66
  end
57
67
  end
58
68
 
@@ -66,35 +76,92 @@ module Rubik
66
76
  # {ok: false, error:, reason: :persist_failed} when the write failed —
67
77
  # so callers report honestly instead of assuming success.
68
78
  def set(key, value, persist: nil)
69
- old_val = fetch(key)
70
- typed_val = parse_scalar_value(value)
71
- validate_prompt_grace_value!(key.to_s, typed_val)
72
- validate_schema(typed_val, DEFAULT_CONFIG["ui"], "ui") if key.to_s == "ui" && typed_val.is_a?(Hash)
73
- keys = key.to_s.split(".")
74
- last_key = keys.pop
75
- target = keys.reduce(@data) do |acc, k|
76
- acc[k] ||= {}
77
- acc[k]
79
+ @monitor.synchronize do
80
+ old_val = fetch(key)
81
+ typed_val = parse_scalar_value(value)
82
+ typed_val = Schema.coerce(key, typed_val)
83
+ Schema.validate_value!(key, typed_val, strict: true)
84
+ if typed_val.is_a?(Hash)
85
+ typed_val = deep_dup(typed_val)
86
+ Schema.coerce_nested!(typed_val, key.to_s)
87
+ # Only spec'd sections validate their leaves — open sections
88
+ # (providers, adapter) accept arbitrary child keys silently.
89
+ section = DEFAULT_CONFIG[key.to_s]
90
+ validate_schema(typed_val, section, key.to_s) if section.is_a?(Hash) && !section.empty?
91
+ end
92
+
93
+ deep_set_key(@data, key, typed_val)
94
+
95
+ if persisting?(persist)
96
+ scope_sym = persist.to_s.downcase.to_sym == :project ? :project : :global
97
+ layer = scope_sym == :project ? (@project_layer ||= {}) : (@global_layer ||= {})
98
+ deep_set_key(layer, key, typed_val)
99
+ else
100
+ @session_layer ||= {}
101
+ deep_set_key(@session_layer, key, typed_val)
102
+ end
103
+
104
+ res = persisting?(persist) ? persist_result(save!(persist)) : { ok: true }
105
+ res[:reason] = :persist_failed unless res[:ok]
106
+ notify_change(key, typed_val, old_val) if old_val != typed_val
107
+ res
78
108
  end
79
- target[last_key] = typed_val
109
+ end
80
110
 
81
- res = persisting?(persist) ? persist_result(save!(persist)) : { ok: true }
82
- res[:reason] = :persist_failed unless res[:ok]
83
- notify_change(key, typed_val, old_val) if old_val != typed_val
84
- res
111
+ # Sets a temporary configuration change within a Context lifecycle (AGENTS.md Ω).
112
+ # When ctx is disposed, the inverse restores the prior configuration value.
113
+ def set_revertible(ctx, key, new_val)
114
+ @monitor.synchronize do
115
+ old_val = fetch(key)
116
+ had_key = !old_val.nil?
117
+ set(key, new_val, persist: nil)
118
+ return unless ctx.respond_to?(:on_dispose)
119
+
120
+ ctx.on_dispose do
121
+ if had_key
122
+ set(key, old_val, persist: nil)
123
+ else
124
+ delete(key, persist: nil)
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ # Creates an isolated, scoped child configuration (AGENTS.md Unified Context).
131
+ # Reads fall back to parent; writes modify only the child; persistence is fail-closed.
132
+ def extend(overrides = {})
133
+ Scoped.new(self, overrides)
85
134
  end
86
135
 
87
136
  # Removes the dot-path key from configuration, persisting like set.
88
137
  # Deleting an absent key is a successful no-op. Returns the persist
89
- # outcome (see set); notifies no listener — deletion callers refresh
90
- # the runtime explicitly.
138
+ # outcome (see set); notifies listeners on change.
91
139
  def delete(key, persist: nil)
92
- blocked = deep_delete(@data, key) == :blocked
93
- return { ok: false, error: "cannot delete '#{key}': path traverses a non-hash value" } if blocked
140
+ @monitor.synchronize do
141
+ old_val = fetch(key)
142
+ blocked = deep_delete(@data, key) == :blocked
143
+ return { ok: false, error: "cannot delete '#{key}': path traverses a non-hash value" } if blocked
144
+
145
+ if persisting?(persist)
146
+ scope_sym = persist.to_s.downcase.to_sym == :project ? :project : :global
147
+ layer = scope_sym == :project ? @project_layer : @global_layer
148
+ deep_delete(layer, key) if layer.is_a?(Hash)
149
+ elsif @session_layer.is_a?(Hash)
150
+ deep_delete(@session_layer, key)
151
+ end
152
+
153
+ recompute_data!
154
+
155
+ res = persisting?(persist) ? persist_result(save!(persist, deletions: [key])) : { ok: true }
156
+ res[:reason] = :persist_failed unless res[:ok]
94
157
 
95
- res = persisting?(persist) ? persist_result(save!(persist, deletions: [key])) : { ok: true }
96
- res[:reason] = :persist_failed unless res[:ok]
97
- res
158
+ new_val = fetch(key)
159
+ if old_val != new_val
160
+ notify_change(key, new_val, old_val)
161
+ @event_bus&.emit(:config_deleted, key: key.to_s, old_val: old_val)
162
+ end
163
+ res
164
+ end
98
165
  end
99
166
 
100
167
  # True only for scopes save! writes to disk; nil/false/:session/"session"/
@@ -170,7 +237,8 @@ module Rubik
170
237
 
171
238
  def notify_change(key, new_val, old_val)
172
239
  key_str = key.to_s
173
- @listeners.dup.each do |entry|
240
+ listeners = @monitor.synchronize { @listeners.dup }
241
+ listeners.each do |entry|
174
242
  prefix = entry[:prefix]
175
243
  matches = prefix.nil? || prefix.empty? || key_str == prefix || key_str.start_with?("#{prefix}.")
176
244
  next unless matches
@@ -185,18 +253,13 @@ module Rubik
185
253
  @event_bus&.emit(:config_changed, key: key_str, value: new_val, old_val: old_val)
186
254
  end
187
255
 
188
- def load_merged_config
189
- global_file = global_config_path
190
- project_file = project_config_path
191
-
192
- global_config = load_yaml(global_file)
193
- project_config = load_yaml(project_file)
194
-
195
- merged = deep_merge(deep_dup(DEFAULT_CONFIG), global_config)
196
- merged = deep_merge(merged, project_config)
256
+ def recompute_data!
257
+ merged = deep_dup(@defaults_layer || DEFAULT_CONFIG)
258
+ [@global_layer, @project_layer, @env_layer, @session_layer].each do |layer|
259
+ merged = deep_merge(merged, layer) if layer.is_a?(Hash)
260
+ end
197
261
 
198
- # If global or project config explicitly defines model roles, ensure stale default hosts are not retained
199
- [global_config, project_config].each do |cfg|
262
+ [@global_layer, @project_layer].each do |cfg|
200
263
  next unless cfg.is_a?(Hash)
201
264
 
202
265
  if cfg["models"].is_a?(Hash)
@@ -211,8 +274,21 @@ module Rubik
211
274
  merged["model"] = deep_dup(cfg["model"]) if cfg["model"].is_a?(Hash) && cfg["model"]["provider"] && cfg["model"]["name"]
212
275
  end
213
276
 
214
- validate_schema(merged)
215
- merged
277
+ @data = merged
278
+ end
279
+
280
+ def load_merged_config
281
+ @defaults_layer = deep_dup(DEFAULT_CONFIG)
282
+ @global_layer = load_yaml(global_config_path)
283
+ @global_layer = {} unless @global_layer.is_a?(Hash)
284
+ @project_layer = load_yaml(project_config_path)
285
+ @project_layer = {} unless @project_layer.is_a?(Hash)
286
+ @env_layer = EnvLoader.load_env(ENV, DEFAULT_CONFIG, @warnings)
287
+ @session_layer = {}
288
+
289
+ recompute_data!
290
+ validate_schema(@data)
291
+ @data
216
292
  end
217
293
  end
218
294
  end
@@ -69,7 +69,7 @@ module Rubik
69
69
  text.each_line do |line|
70
70
  if (m = line.match(/^error\[(E\d+)\]:(.*)$/))
71
71
  current_error = { code: m[1], message: m[2].strip, category: :type }
72
- elsif current_error && (m = line.match(/-->\s*([^:\s]+):(\d+):(\d+)/))
72
+ elsif current_error && (m = line.match(/-->\s*+([^:\s]++):(\d++):(\d++)/))
73
73
  entries << Entry.new(
74
74
  file: m[1],
75
75
  line: m[2],
@@ -125,7 +125,7 @@ module Rubik
125
125
  def parse_ts_js(text)
126
126
  entries = []
127
127
  text.each_line do |line|
128
- next unless (m = line.match(%r{^((?:[a-zA-Z]:[\\/])?[^(:\s]+)(?:\((\d+),(\d+)\)|:(\d+):(\d+))(?::\s*-\s*|:\s*|\s*-\s*|\s+)error(?:\s+(TS\d+))?:?(.*)$}i))
128
+ next unless (m = line.match(%r{^((?:[a-zA-Z]:[\\/])?[^(:\s]++)(?:\((\d++),(\d++)\)|:(\d++):(\d++))(?::\s*+-\s*+|:\s*+|\s*+-\s*+|\s++)error(?:\s++(TS\d++))?:?(.*)$}i))
129
129
 
130
130
  file = m[1]
131
131
  line_num = (m[2] || m[4]).to_i
@@ -10,6 +10,7 @@ module Rubik
10
10
  # subscriber error isolation.
11
11
  class Dispatcher
12
12
  STOP_MARKER = :__dispatcher_stop__
13
+ DEFAULT_WARN_QUEUE_DEPTH = 1000
13
14
 
14
15
  Event = Struct.new(:name, :payload, :subsystem, :level, :facility_id, keyword_init: true)
15
16
  Barrier = Struct.new(:queue)
@@ -44,6 +45,18 @@ module Rubik
44
45
  subsystem: subsystem.to_sym, level: level, facility_id: facility_id
45
46
  )
46
47
  )
48
+
49
+ q_size = @queue.size
50
+ if q_size >= DEFAULT_WARN_QUEUE_DEPTH && (q_size % 500).zero?
51
+ msg = "Dispatcher event queue high watermark reached: #{q_size} pending events"
52
+ @logger&.warn(msg)
53
+ begin
54
+ @dmesg&.log(:kernel, :warn, msg, facility_id: "dispatcher", queue_size: q_size)
55
+ rescue StandardError
56
+ nil
57
+ end
58
+ end
59
+
47
60
  true
48
61
  end
49
62
 
@@ -92,29 +105,12 @@ module Rubik
92
105
  end
93
106
  alias dispose stop
94
107
 
95
- def stopped?
96
- @lock.synchronize { @stopped }
97
- end
98
-
99
- def alive?
100
- !stopped? && worker_alive?
101
- end
102
-
103
- def worker_alive?
104
- !@worker_thread.nil? && @worker_thread.alive?
105
- end
106
-
107
- def subscribers(event_name)
108
- @lock.synchronize { @listeners[event_name.to_sym].dup }
109
- end
110
-
111
- def all_subscribers
112
- @lock.synchronize { @listeners.transform_values(&:dup) }
113
- end
114
-
115
- def queue_size
116
- @queue.size
117
- end
108
+ def stopped? = @lock.synchronize { @stopped }
109
+ def alive? = !stopped? && worker_alive?
110
+ def worker_alive? = !@worker_thread.nil? && @worker_thread.alive?
111
+ def subscribers(event_name) = @lock.synchronize { @listeners[event_name.to_sym].dup }
112
+ def all_subscribers = @lock.synchronize { @listeners.transform_values(&:dup) }
113
+ def queue_size = @queue.size
118
114
 
119
115
  private
120
116
 
@@ -88,42 +88,46 @@ module Rubik
88
88
  end
89
89
 
90
90
  def reap_dead_units!
91
- @monitor.synchronize do
91
+ units_to_reap = @monitor.synchronize do
92
92
  reaped = []
93
93
  @units.each_value do |unit|
94
94
  unit.sync_state! if unit.respond_to?(:sync_state!)
95
95
  next unless unit.dead? && unit.state != :reaped
96
96
 
97
- safely_dispose_context(unit)
98
- unit.transition_to!(:reaped, reason: "reaped")
99
97
  reaped << unit
100
98
  end
101
- @dmesg&.log(:kernel, :info, "reaped #{reaped.size} dead units", facility_id: "init") unless reaped.empty?
102
99
  reaped
103
100
  end
101
+
102
+ units_to_reap.each do |unit|
103
+ safely_dispose_context(unit)
104
+ unit.transition_to!(:reaped, reason: "reaped")
105
+ end
106
+
107
+ @dmesg&.log(:kernel, :info, "reaped #{units_to_reap.size} dead units", facility_id: "init") unless units_to_reap.empty?
108
+ units_to_reap
104
109
  end
105
110
 
106
111
  def kill(id, reason = "killed")
107
- @monitor.synchronize do
108
- unit = @units[id.to_s]
109
- return false unless unit
112
+ unit = @monitor.synchronize { @units[id.to_s] }
113
+ return false unless unit
110
114
 
111
- safely_kill_unit(unit, reason)
112
- true
113
- end
115
+ safely_kill_unit(unit, reason)
116
+ true
114
117
  end
115
118
 
116
119
  def kill_all(reason = "killed")
117
- @monitor.synchronize do
118
- # LIFO shutdown: reverse chronological order of registered units
119
- @order.reverse_each do |id|
120
- unit = @units[id]
121
- next unless unit
120
+ # Snapshot in reverse chronological (LIFO) order under the monitor lock,
121
+ # then invoke safely_kill_unit OUTSIDE the lock to prevent blocking
122
+ # supervisor operations during slow/external unit kills (Finding F4).
123
+ units_to_kill = @monitor.synchronize do
124
+ @order.reverse_each.filter_map { |id| @units[id] }
125
+ end
122
126
 
123
- safely_kill_unit(unit, reason)
124
- end
125
- true
127
+ units_to_kill.each do |unit|
128
+ safely_kill_unit(unit, reason)
126
129
  end
130
+ true
127
131
  end
128
132
 
129
133
  def resume_all
@@ -4,6 +4,7 @@ require "time"
4
4
  require "json"
5
5
  require_relative "../../prompt/context_compactor"
6
6
  require_relative "../../prompt/conversation_rows"
7
+ require_relative "../../storage/layout"
7
8
  require_relative "../../uris"
8
9
  require_relative "suspend_lifecycle"
9
10
  require_relative "xml_render"
@@ -98,15 +99,8 @@ module Rubik
98
99
  SuspendLifecycle.load(store: store, storage_id: storage_id, config: config, dmesg: dmesg, event_bus: event_bus)
99
100
  end
100
101
 
101
- def config_get(config, key, default)
102
- return default if config.nil?
103
-
104
- val = if config.is_a?(Hash)
105
- config.dig(*key.to_s.split("."))
106
- elsif config.respond_to?(:fetch)
107
- config.fetch(key, nil)
108
- end
109
- val.nil? ? default : val
102
+ def config_get(config, key, default = nil)
103
+ Rubik::Storage::Layout.config_get(config, key, default)
110
104
  end
111
105
  end
112
106
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../../prompt/conversation_rows"
4
+ require_relative "../../storage/layout"
4
5
 
5
6
  module Rubik
6
7
  module Orchestrator
@@ -52,15 +53,8 @@ module Rubik
52
53
  Directive.new(ledger: ledger, window: [window, 1].max, deps: deps)
53
54
  end
54
55
 
55
- def config_get(config, key, default)
56
- return default if config.nil?
57
-
58
- val = if config.is_a?(Hash)
59
- config.dig(*key.to_s.split("."))
60
- elsif config.respond_to?(:fetch)
61
- config.fetch(key, nil)
62
- end
63
- val.nil? ? default : val
56
+ def config_get(config, key, default = nil)
57
+ Rubik::Storage::Layout.config_get(config, key, default)
64
58
  end
65
59
  end
66
60