rubik 0.8.1 → 0.9.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 (107) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +2 -2
  3. data/CHANGELOG.md +100 -1
  4. data/bin/rubik +15 -10
  5. data/lib/rubik/cli/workspace_trust.rb +1 -0
  6. data/lib/rubik/concurrency/worker.rb +143 -0
  7. data/lib/rubik/concurrency.rb +1 -0
  8. data/lib/rubik/config/deep_ops.rb +17 -0
  9. data/lib/rubik/config/defaults.rb +0 -1
  10. data/lib/rubik/config/schema.rb +0 -1
  11. data/lib/rubik/config.rb +7 -3
  12. data/lib/rubik/context/repo_context_reader.rb +27 -8
  13. data/lib/rubik/events.rb +18 -0
  14. data/lib/rubik/kernel/unit.rb +4 -3
  15. data/lib/rubik/orchestrator/auto_apply.rb +2 -0
  16. data/lib/rubik/orchestrator/budget_manager.rb +18 -2
  17. data/lib/rubik/orchestrator/goal_ledger/deep_ops.rb +41 -0
  18. data/lib/rubik/orchestrator/goal_ledger/drift.rb +2 -1
  19. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +56 -50
  20. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +26 -0
  21. data/lib/rubik/orchestrator/goal_ledger/patch.rb +10 -17
  22. data/lib/rubik/orchestrator/goal_ledger/suspend_lifecycle.rb +9 -41
  23. data/lib/rubik/orchestrator/goal_ledger/validator.rb +6 -20
  24. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +3 -14
  25. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +21 -14
  26. data/lib/rubik/orchestrator/goal_state.rb +15 -15
  27. data/lib/rubik/orchestrator/loop.rb +62 -63
  28. data/lib/rubik/orchestrator/lsp_feedback.rb +7 -1
  29. data/lib/rubik/orchestrator/subagent/admission_policy.rb +161 -0
  30. data/lib/rubik/orchestrator/subagent/builtin_types.rb +3 -2
  31. data/lib/rubik/orchestrator/subagent/notices.rb +5 -4
  32. data/lib/rubik/orchestrator/subagent/profile_contract.rb +4 -10
  33. data/lib/rubik/orchestrator/subagent/run_cycle.rb +22 -1
  34. data/lib/rubik/orchestrator/subagent/scoped_registry.rb +34 -2
  35. data/lib/rubik/orchestrator/subagent/type_registry.rb +19 -1
  36. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +6 -1
  37. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +6 -1
  38. data/lib/rubik/orchestrator/turn_executor.rb +2 -1
  39. data/lib/rubik/platform/path.rb +0 -1
  40. data/lib/rubik/prompt/context_builder.rb +24 -18
  41. data/lib/rubik/prompt/context_compactor.rb +31 -8
  42. data/lib/rubik/prompt/conversation_rows.rb +33 -0
  43. data/lib/rubik/prompt/doctrine.rb +4 -2
  44. data/lib/rubik/prompt/emergency_compactor.rb +5 -4
  45. data/lib/rubik/repl/commands/agent_execution.rb +13 -2
  46. data/lib/rubik/repl/commands/buffer.rb +4 -0
  47. data/lib/rubik/repl/commands/mcp.rb +5 -0
  48. data/lib/rubik/repl/commands/subagents.rb +4 -4
  49. data/lib/rubik/repl/commands/workspace.rb +22 -2
  50. data/lib/rubik/repl/config_wiring.rb +24 -7
  51. data/lib/rubik/repl/input_execution.rb +1 -1
  52. data/lib/rubik/repl/lifecycle.rb +3 -2
  53. data/lib/rubik/repl/pruner/harness.rb +52 -67
  54. data/lib/rubik/repl/pruner/rules.rb +3 -1
  55. data/lib/rubik/repl/service_catalog.rb +1 -1
  56. data/lib/rubik/repl/synthesized_command_context.rb +3 -0
  57. data/lib/rubik/repl/turn_recorder.rb +5 -4
  58. data/lib/rubik/repl.rb +1 -0
  59. data/lib/rubik/runtime/context.rb +5 -0
  60. data/lib/rubik/runtime/discovery.rb +55 -0
  61. data/lib/rubik/runtime/overlay_storage.rb +54 -23
  62. data/lib/rubik/runtime/repair_probe.rb +20 -2
  63. data/lib/rubik/runtime/sealed_coeffects.rb +58 -0
  64. data/lib/rubik/runtime/synthesized_command_context.rb +80 -0
  65. data/lib/rubik/runtime/synthesized_tool_context.rb +123 -0
  66. data/lib/rubik/runtime.rb +2 -0
  67. data/lib/rubik/security/classifier/classifier.rb +16 -8
  68. data/lib/rubik/security/classifier/commands/biome.yaml +49 -0
  69. data/lib/rubik/security/classifier/commands/bun.yaml +91 -0
  70. data/lib/rubik/security/classifier/commands/bundle.yaml +107 -0
  71. data/lib/rubik/security/classifier/commands/clang-format.yaml +21 -0
  72. data/lib/rubik/security/classifier/commands/clang.yaml +37 -0
  73. data/lib/rubik/security/classifier/commands/cmake.yaml +37 -0
  74. data/lib/rubik/security/classifier/commands/ctest.yaml +30 -0
  75. data/lib/rubik/security/classifier/commands/gcc.yaml +36 -0
  76. data/lib/rubik/security/classifier/commands/gem.yaml +104 -0
  77. data/lib/rubik/security/classifier/commands/gofmt.yaml +15 -0
  78. data/lib/rubik/security/classifier/commands/pnpm.yaml +8 -8
  79. data/lib/rubik/security/classifier/commands/python.yaml +18 -1
  80. data/lib/rubik/security/classifier/commands/python3.yaml +18 -1
  81. data/lib/rubik/security/classifier/commands/rake.yaml +60 -0
  82. data/lib/rubik/security/classifier/commands/rspec.yaml +42 -0
  83. data/lib/rubik/security/classifier/commands/rubocop.yaml +38 -0
  84. data/lib/rubik/security/classifier/commands/ruby.yaml +25 -0
  85. data/lib/rubik/security/classifier/commands/rustc.yaml +34 -0
  86. data/lib/rubik/security/classifier/commands/zig.yaml +77 -0
  87. data/lib/rubik/security/classifier/database.rb +12 -1
  88. data/lib/rubik/security/classifier/matcher.rb +17 -2
  89. data/lib/rubik/security/classifier/models.rb +3 -2
  90. data/lib/rubik/security/classifier.rb +5 -0
  91. data/lib/rubik/security/guard.rb +133 -39
  92. data/lib/rubik/storage/vakanuvis/harness.rb +49 -89
  93. data/lib/rubik/tools/base.rb +24 -7
  94. data/lib/rubik/tools/builtins/files.rb +2 -0
  95. data/lib/rubik/tools/builtins/goal_ledger.rb +9 -9
  96. data/lib/rubik/tools/builtins/lifecycle.rb +1 -1
  97. data/lib/rubik/tools/builtins/synthesis.rb +121 -64
  98. data/lib/rubik/tools/builtins/unsynthesize.rb +38 -0
  99. data/lib/rubik/tools/catalog.rb +3 -0
  100. data/lib/rubik/tools/declaration.rb +6 -2
  101. data/lib/rubik/tools/preflight_runner.rb +107 -14
  102. data/lib/rubik/tools/registry.rb +2 -0
  103. data/lib/rubik/tools/uri_resolver.rb +3 -1
  104. data/lib/rubik/uris.rb +5 -1
  105. data/lib/rubik/version.rb +1 -1
  106. data/lib/rubik.rb +1 -0
  107. metadata +25 -1
@@ -36,7 +36,12 @@ module Rubik
36
36
  end
37
37
 
38
38
  def limits
39
- { context_limit: @deps.context_limit, max_output_tokens: @deps.max_output_tokens, config: @deps.config }
39
+ reserved = if @deps.respond_to?(:tool_registry) && @deps.tool_registry&.schemas
40
+ Rubik::Prompt::TokenEstimator.estimate_tool_schemas(@deps.tool_registry.schemas)
41
+ else
42
+ 0
43
+ end
44
+ { context_limit: @deps.context_limit, max_output_tokens: @deps.max_output_tokens, config: @deps.config, reserved_tokens: reserved }
40
45
  end
41
46
 
42
47
  # Loop policy (ToolCallId doctrine): unique-by-suffix ids — the
@@ -122,7 +122,8 @@ module Rubik
122
122
  layer = Rubik::Prompt::EmergencyCompactor.compact!(
123
123
  channel.history, context_limit: limits[:context_limit], max_output_tokens: limits[:max_output_tokens],
124
124
  config: ProviderRecovery.compaction_config(limits[:config]),
125
- absorb: channel.absorb_directive
125
+ absorb: channel.absorb_directive,
126
+ reserved_tokens: limits[:reserved_tokens] || 0
126
127
  )
127
128
  channel.announce_compaction(reason, layer)
128
129
  layer
@@ -214,7 +214,6 @@ module Rubik
214
214
  roots << ENV["CommonProgramFiles"] if ENV["CommonProgramFiles"]
215
215
  roots << ENV["USERPROFILE"] if ENV["USERPROFILE"]
216
216
  roots << "#{ENV['SystemDrive']}/" if ENV["SystemDrive"]
217
- roots << ENV["SystemDrive"] if ENV["SystemDrive"]
218
217
  end
219
218
  roots << home_dir
220
219
  roots.compact.map { |r| normalize_path(r) }.uniq
@@ -20,8 +20,8 @@ module Rubik
20
20
  Rubik::Context::RepoContextReader.new(root_dir: root_dir).find_protocol_files
21
21
  end
22
22
 
23
- def self.build_protocol_xml(root_dir = Dir.pwd)
24
- Rubik::Context::RepoContextReader.new(root_dir: root_dir).read_protocol_xml
23
+ def self.build_protocol_xml(root_dir = Dir.pwd, config: nil)
24
+ Rubik::Context::RepoContextReader.new(root_dir: root_dir, config: config).read_protocol_xml
25
25
  end
26
26
 
27
27
  def self.build_workspace_xml(context_index = nil, root_dir = Dir.pwd)
@@ -165,9 +165,10 @@ module Rubik
165
165
  custom = commands.select { |_k, v| v.is_a?(Hash) && v[:custom] }
166
166
  return "" if custom.empty?
167
167
 
168
+ esc = Rubik::Platform.method(:xml_escape)
168
169
  cmd_tags = custom.sort.map do |name, meta|
169
170
  desc = (meta.is_a?(Hash) ? meta[:description] : meta) || ""
170
- " <command name=\"!#{name}\">#{desc}</command>"
171
+ " <command name=\"!#{esc.call(name.to_s)}\">#{esc.call(desc.to_s)}</command>"
171
172
  end.join("\n")
172
173
  "<custom_commands>\n#{cmd_tags}\n</custom_commands>"
173
174
  end
@@ -198,13 +199,7 @@ module Rubik
198
199
  esc = Rubik::Platform.method(:xml_escape)
199
200
  tags = types.map do |t|
200
201
  name = esc.call(t.name.to_s)
201
- tools_mode = if t.name.to_s == "self"
202
- "full_clone"
203
- elsif t.enable_write_tools
204
- "read_write_test"
205
- else
206
- "read_only"
207
- end
202
+ tools_mode = Rubik::Orchestrator::Subagent::AdmissionPolicy.capability_label(t)
208
203
  workspace = t.name.to_s == "coder" ? "branch" : "inherit"
209
204
  desc = esc.call(t.description.to_s)
210
205
  " <subagent name=\"#{name}\" tools=\"#{tools_mode}\" default_workspace=\"#{workspace}\">#{desc}</subagent>"
@@ -243,7 +238,7 @@ module Rubik
243
238
  end
244
239
  private_class_method :prompt_visible_subagent_types
245
240
 
246
- def self.build_lsp_xml(lsp_manager = nil)
241
+ def self.build_lsp_xml(lsp_manager = nil, max_diagnostics: 50)
247
242
  return "" unless lsp_manager&.enabled?
248
243
 
249
244
  summary = lsp_manager.status_summary
@@ -251,7 +246,7 @@ module Rubik
251
246
 
252
247
  all_diags = lsp_manager.all_diagnostics
253
248
  total_errors = 0
254
- diag_tags = []
249
+ parsed_diags = []
255
250
 
256
251
  all_diags.each do |fpath, diags|
257
252
  diags.each do |d|
@@ -259,7 +254,10 @@ module Rubik
259
254
  total_errors += 1 if sev == "error"
260
255
  start_line = (d.dig("range", "start", "line") || 0) + 1
261
256
  msg = Rubik::Platform.xml_escape(d["message"])
262
- diag_tags << " <diagnostic file=\"#{Rubik::Platform.xml_escape(fpath)}\" line=\"#{start_line}\" severity=\"#{sev}\">#{msg}</diagnostic>"
257
+ parsed_diags << {
258
+ severity: sev,
259
+ tag: " <diagnostic file=\"#{Rubik::Platform.xml_escape(fpath)}\" line=\"#{start_line}\" severity=\"#{sev}\">#{msg}</diagnostic>"
260
+ }
263
261
  end
264
262
  end
265
263
 
@@ -274,14 +272,21 @@ module Rubik
274
272
  lsp_diagnostics/lsp_hover will return nothing — use run_command or
275
273
  read tools instead.
276
274
  XML
277
- elsif diag_tags.empty?
275
+ elsif parsed_diags.empty?
278
276
  <<~XML.strip
279
277
  <lsp_status enabled="true" active_servers="#{Rubik::Platform.xml_escape(active_servers.join(', '))}" clean="true" />
280
278
  XML
281
279
  else
280
+ # Prioritize errors before warnings when capping
281
+ sorted_diags = parsed_diags.sort_by { |d| d[:severity] == "error" ? 0 : 1 }
282
+ shown_diags = sorted_diags.first(max_diagnostics).map { |d| d[:tag] }
283
+ omitted_count = parsed_diags.size - shown_diags.size
284
+ omitted_notice = omitted_count.positive? ? "\n <!-- #{omitted_count} additional diagnostics omitted for brevity -->" : ""
285
+ shown_attr = omitted_count.positive? ? " shown=\"#{shown_diags.size}\"" : ""
286
+
282
287
  <<~XML.strip
283
- <lsp_diagnostics active_servers="#{Rubik::Platform.xml_escape(active_servers.join(', '))}" total_issues="#{diag_tags.size}" errors="#{total_errors}">
284
- #{diag_tags.join("\n")}
288
+ <lsp_diagnostics active_servers="#{Rubik::Platform.xml_escape(active_servers.join(', '))}" total_issues="#{parsed_diags.size}" errors="#{total_errors}"#{shown_attr}>
289
+ #{shown_diags.join("\n")}#{omitted_notice}
285
290
  </lsp_diagnostics>
286
291
  XML
287
292
  end
@@ -303,7 +308,7 @@ module Rubik
303
308
  # ShellResolver the sandbox driver spawns through.
304
309
  sections << Rubik::Sandbox::ShellResolver.environment_line(svc.config&.fetch("sandbox.shell", "auto") || "auto")
305
310
 
306
- protocol_xml = build_protocol_xml(svc.root_dir)
311
+ protocol_xml = build_protocol_xml(svc.root_dir, config: svc.config)
307
312
  sections << protocol_xml unless protocol_xml.empty?
308
313
 
309
314
  workspace_xml = build_workspace_xml(svc.context_index, svc.root_dir)
@@ -403,7 +408,8 @@ module Rubik
403
408
 
404
409
  def self.detect_git_branch(root_dir)
405
410
  Rubik::Context::RepoContextReader.new(root_dir: root_dir).read_git_branch
406
- rescue StandardError
411
+ rescue StandardError => e
412
+ Rubik::Log.warn(:context, "detect_git_branch failed: #{e.class}: #{e.message}")
407
413
  nil
408
414
  end
409
415
  end
@@ -13,21 +13,23 @@ module Rubik
13
13
 
14
14
  module_function
15
15
 
16
- def effective_input_ceiling(context_limit:, max_output_tokens: 4096, safety_margin: 1024)
17
- [1024, context_limit.to_i - max_output_tokens.to_i - safety_margin.to_i].max
16
+ def effective_input_ceiling(context_limit:, max_output_tokens: 4096, safety_margin: 1024, reserved_tokens: 0)
17
+ [1024, context_limit.to_i - max_output_tokens.to_i - safety_margin.to_i - reserved_tokens.to_i].max
18
18
  end
19
19
 
20
- def needs_compaction?(history, context_limit:, max_output_tokens: 4096, safety_margin: 1024, max_tokens_ratio: 0.85, config: {})
20
+ def needs_compaction?(history, context_limit:, max_output_tokens: 4096, safety_margin: 1024, max_tokens_ratio: 0.85, config: {}, reserved_tokens: 0)
21
21
  return false if history.nil? || history.empty?
22
22
 
23
23
  ratio = (config["max_tokens_ratio"] || config[:max_tokens_ratio] || max_tokens_ratio || 0.85).to_f
24
24
  margin = (config["safety_margin_tokens"] || config[:safety_margin_tokens] || safety_margin || 1024).to_i
25
25
  max_out = (config["max_output_tokens"] || config[:max_output_tokens] || max_output_tokens || 4096).to_i
26
+ reserved = (config["reserved_tokens"] || config[:reserved_tokens] || reserved_tokens || 0).to_i
26
27
 
27
28
  ceiling = effective_input_ceiling(
28
29
  context_limit: context_limit,
29
30
  max_output_tokens: max_out,
30
- safety_margin: margin
31
+ safety_margin: margin,
32
+ reserved_tokens: reserved
31
33
  )
32
34
  threshold = (ceiling * ratio).to_i
33
35
  total_tokens = TokenEstimator.estimate_history(history)
@@ -38,7 +40,7 @@ module Rubik
38
40
  # iken tier atlanır ve davranış bit-bit aynıdır; verilirse dup
39
41
  # kopyaya #apply!(rows) uygulanır (prompt katmanı goal bilmez;
40
42
  # directive'i orchestrator/goal_ledger ailesi kurar).
41
- def compact_history(history, context_limit:, max_output_tokens: 4096, safety_margin: 1024, config: {}, force: false, absorb: nil)
43
+ def compact_history(history, context_limit:, max_output_tokens: 4096, safety_margin: 1024, config: {}, force: false, absorb: nil, reserved_tokens: 0)
42
44
  return [] if history.nil? || history.empty?
43
45
 
44
46
  strategy = (config["compaction_strategy"] || config[:compaction_strategy] || "hybrid").to_s
@@ -49,11 +51,14 @@ module Rubik
49
51
  preserve_first_n = (config["preserve_first_n"] || config[:preserve_first_n] || 2).to_i
50
52
  preserve_last_n = (config["preserve_last_n"] || config[:preserve_last_n] || 6).to_i
51
53
  safety_margin = (config["safety_margin_tokens"] || config[:safety_margin_tokens] || safety_margin || 1024).to_i
54
+ max_out = (config["max_output_tokens"] || config[:max_output_tokens] || max_output_tokens || 4096).to_i
55
+ reserved = (config["reserved_tokens"] || config[:reserved_tokens] || reserved_tokens || 0).to_i
52
56
 
53
57
  ceiling = effective_input_ceiling(
54
58
  context_limit: context_limit,
55
- max_output_tokens: max_output_tokens,
56
- safety_margin: safety_margin
59
+ max_output_tokens: max_out,
60
+ safety_margin: safety_margin,
61
+ reserved_tokens: reserved
57
62
  )
58
63
  threshold = (ceiling * max_tokens_ratio).to_i
59
64
 
@@ -173,6 +178,14 @@ module Rubik
173
178
  role == "user" && content.include?("<user_task>") && content.include?("</user_task>")
174
179
  end
175
180
 
181
+ # Σ satırı gramerinin tek evi: etiket adları hem render (XmlRender,
182
+ # DriftNote) hem tanıma (ledger_message?/drift_message?) hem de pruner
183
+ # enjeksiyon-kaçışı yüzeylerince tüketilir — önek yalnız burada doğar,
184
+ # herkes aynı sabitten okur. Ayrı sabitler: `<goal_ledger ` ve
185
+ # `<ledger_drift ` satırları farklı pin/pin-dışı kontratlara sahiptir.
186
+ GOAL_LEDGER_TAG = "goal_ledger"
187
+ LEDGER_DRIFT_TAG = "ledger_drift"
188
+
176
189
  # Yapılandırılmış goal ledger satırı mı? (goal-ledger planı A) —
177
190
  # goal_message? ile aynı alıntı-tek-etiket disiplini: açılış
178
191
  # `<goal_ledger ` + kapanış `</goal_ledger>` BİRLİKTE aranır; kullanıcı
@@ -183,7 +196,17 @@ module Rubik
183
196
 
184
197
  role = ConversationRows.role_of(msg)
185
198
  content = ConversationRows.content_of(msg).to_s
186
- role == "user" && content.start_with?("<goal_ledger ") && content.include?("</goal_ledger>")
199
+ role == "user" && content.start_with?("<#{GOAL_LEDGER_TAG} ") && content.include?("</#{GOAL_LEDGER_TAG}>")
200
+ end
201
+
202
+ # Drift yeniden-enjeksiyon taşıyıcısı (DriftNote) — ledger_message?
203
+ # ile aynı alıntı-tek-etiket disiplini; etiket adı LEDGER_DRIFT_TAG.
204
+ def drift_message?(msg)
205
+ return false unless ConversationRows.row?(msg)
206
+
207
+ role = ConversationRows.role_of(msg)
208
+ content = ConversationRows.content_of(msg).to_s
209
+ role == "user" && content.start_with?("<#{LEDGER_DRIFT_TAG} ") && content.include?("</#{LEDGER_DRIFT_TAG}>")
187
210
  end
188
211
 
189
212
  # İsim korunur (pruner rules.rb:39 çağrısı yük taşıyor): pin ailesi
@@ -43,6 +43,39 @@ module Rubik
43
43
  # match), preserving current behavior bit-for-bit.
44
44
  def user_row?(row) = row[:role] == "user" || row["role"] == "user"
45
45
 
46
+ # Dialogue turn sanitizer for prior history and session restoration.
47
+ # Defends against unauthorized role injection (e.g. injected "system" role)
48
+ # and unescaped structural pin tags (<user_task>, <protocol>).
49
+ def sanitize_dialogue_turn(turn)
50
+ return nil unless row?(turn)
51
+
52
+ role = role_of(turn).strip.downcase
53
+ return nil unless ["user", "assistant", "tool"].include?(role)
54
+
55
+ raw_content = content_of(turn)
56
+ tool_calls = value_of(turn, :tool_calls)
57
+ tool_call_id = value_of(turn, :tool_call_id)
58
+ tool_name = value_of(turn, :tool_name)
59
+
60
+ empty_content = raw_content.nil? || raw_content.to_s.strip.empty?
61
+ no_tools = (tool_calls.nil? || tool_calls.empty?) && tool_call_id.nil?
62
+ return nil if empty_content && no_tools
63
+
64
+ content = raw_content.to_s
65
+ if role == "user"
66
+ content = content.gsub("<user_task>", "&lt;user_task&gt;")
67
+ .gsub("</user_task>", "&lt;/user_task&gt;")
68
+ .gsub("<protocol>", "&lt;protocol&gt;")
69
+ .gsub("</protocol>", "&lt;/protocol&gt;")
70
+ end
71
+
72
+ sanitized = { role: role, content: content }
73
+ sanitized[:tool_calls] = tool_calls if tool_calls && !tool_calls.empty?
74
+ sanitized[:tool_call_id] = tool_call_id.to_s if tool_call_id
75
+ sanitized[:tool_name] = tool_name.to_s if tool_name
76
+ sanitized
77
+ end
78
+
46
79
  # The notice-merge policy, previously duplicated in
47
80
  # Orchestrator::Loop#merge_notice_block_into_history! and
48
81
  # Lifecycle#inject_subagent_notice_into_conversation_history!.
@@ -62,9 +62,11 @@ module Rubik
62
62
  HDR
63
63
  when :goal
64
64
  ledger_active = goal_ledger && tool_active?("update_goal_ledger", active_tools)
65
- ledger_line = " If a `<goal_ledger>` row is present, keep its state current: read `rubik://goal/ledger` and " \
65
+ ledger_line = " If a `<goal_ledger>` row is present, keep its state current: read `rubik://goal/ledger` (or `.json`) and " \
66
66
  "apply JSON patches with `update_goal_ledger(patch: ...)` against the reported rev after each " \
67
- "meaningful step (task status changes, new findings/decisions)."
67
+ "meaningful step (task status changes, new findings/decisions). Tasks and environment notes can carry " \
68
+ "declarative `verify` specifications ({\"type\": \"file_exists\"|\"file_contains\"|\"marker\", \"path\": ...}) " \
69
+ "that the harness autonomously re-verifies against the filesystem at step boundaries."
68
70
  <<~HDR
69
71
  You are Rubik (maestro), an autonomous AI coding SUPERVISOR in AUTONOMOUS GOAL mode.
70
72
  You own the goal in <user_task> and MUST drive it to verified completion as a pure orchestrator — you conduct, subagents perform. You may perform lightweight read-only inspection (`list_files`, `grep_search`, `read_file`) to orient and shape assignments, while all broader research, code changes, and verification run in subagent contexts, never in yours:
@@ -31,19 +31,20 @@ module Rubik
31
31
  # :hard_truncate); the nil/empty guards return the input unchanged
32
32
  # (pinned by tests). The return value is observational: callers forward
33
33
  # it on the :emergency_compact on_step event rather than branch on it.
34
- def compact!(history, context_limit: nil, max_output_tokens: nil, config: {}, absorb: nil)
34
+ def compact!(history, context_limit: nil, max_output_tokens: nil, config: {}, absorb: nil, reserved_tokens: 0)
35
35
  return history if history.nil? || history.empty?
36
36
 
37
37
  config = {} unless config.is_a?(Hash) # a null `context:` YAML section makes fetch return nil
38
- max_out = max_output_tokens || 4096
38
+ max_out = (config["max_output_tokens"] || config[:max_output_tokens] || max_output_tokens || 4096).to_i
39
+ reserved = (config["reserved_tokens"] || config[:reserved_tokens] || reserved_tokens || 0).to_i
39
40
  if context_limit&.positive?
40
41
  structural = Rubik::Prompt::ContextCompactor.compact_history(
41
42
  history, context_limit: context_limit,
42
- max_output_tokens: max_out, config: config, force: true, absorb: absorb
43
+ max_output_tokens: max_out, config: config, force: true, absorb: absorb, reserved_tokens: reserved
43
44
  )
44
45
  history.replace(structural)
45
46
  still_over = Rubik::Prompt::ContextCompactor.needs_compaction?(
46
- history, context_limit: context_limit, max_output_tokens: max_out, config: config
47
+ history, context_limit: context_limit, max_output_tokens: max_out, config: config, reserved_tokens: reserved
47
48
  )
48
49
  return :structural unless still_over
49
50
  end
@@ -231,7 +231,8 @@ module Rubik
231
231
  # eşlemesine düşülmesin.
232
232
  if e.respond_to?(:transient?) && e.transient?
233
233
  suspend_goal_turn(ctx, orchestrator: orchestrator, goal_state: goal_state, goal_ledger: goal_ledger,
234
- goal_text: goal_text, goal_id: goal_id, record_pair: record_pair, reason: e.message)
234
+ goal_text: goal_text, goal_id: goal_id, record_pair: record_pair, reason: e.message,
235
+ tokens: orchestrator.total_tokens)
235
236
  else
236
237
  fail_goal_turn(ctx, goal_state: goal_state, goal_ledger: goal_ledger, goal_id: goal_id,
237
238
  record_pair: record_pair, reason: e.message)
@@ -298,7 +299,9 @@ module Rubik
298
299
  # olayları loop inbox'a girer ve devam adımında boşaltılır.
299
300
  auto_extensions = 0
300
301
  max_auto = ctx.config.fetch("agent.goal_auto_continue_limit", 0).to_i
301
- while !res[:ok] && res[:error]&.include?("Max steps budget exceeded")
302
+ # Yapılandırılmış neden (loop.rb terminali): insan-okur string
303
+ # yalnız gösterim içindir — otomasyon prozu parse etmesin.
304
+ while !res[:ok] && res[:reason] == :budget_exhausted
302
305
  if auto_extensions < max_auto
303
306
  auto_extensions += 1
304
307
  ctx.out.puts "\n🤖 [Rubik::Goal] Adım bütçesi doldu (#{res[:error]}). " \
@@ -623,6 +626,14 @@ module Rubik
623
626
  shell.register_command("format", description: "Format target file", audience: :both) do |args, ctx|
624
627
  target = args.first
625
628
  if target
629
+ if ctx.agent_caller
630
+ begin
631
+ ctx.guard.verify_path_write!(target)
632
+ rescue Rubik::Security::BlockedWriteError => e
633
+ ctx.out.puts "Security Error: formatting critical path blocked: #{e.message}"
634
+ next
635
+ end
636
+ end
626
637
  lang = Rubik::Runtime::Discovery.detect_primary_language(Dir.pwd) || "ruby"
627
638
  adapter = ctx.adapter_registry.get(lang) || ctx.adapter_registry.get("ruby")
628
639
  res = adapter.format_code(target)
@@ -154,6 +154,10 @@ module Rubik
154
154
  ctx.out.puts " • Prompt Indicator : #{ctx.config.fetch('buffer.prompt_indicator', true)}"
155
155
  ctx.out.puts " • Reminder Interval: #{ctx.config.fetch('buffer.reminder_interval', 3)} turns"
156
156
  elsif ["manual", "on_test", "on_finish", "always"].include?(sub)
157
+ if ctx.agent_caller
158
+ ctx.out.puts "Security Error: buffer autoapply policy is operator-only and cannot be altered by autonomous agents."
159
+ return
160
+ end
157
161
  res = ctx.config.set("buffer.auto_apply", sub, persist: scope)
158
162
  return if Base.report_persist_failure(ctx, res, scope)
159
163
 
@@ -21,6 +21,11 @@ module Rubik
21
21
  scope, clean_args = Base.parse_scope(args)
22
22
  sub = clean_args.first&.downcase || "status"
23
23
 
24
+ if ctx.agent_caller && !["status", "list", "ping", "tools"].include?(sub)
25
+ ctx.out.puts "Security Error: MCP server management ('#{sub}') is operator-only and cannot be altered by autonomous agents."
26
+ next
27
+ end
28
+
24
29
  case sub
25
30
  when "status"
26
31
  status_servers(ctx, manager)
@@ -3,6 +3,7 @@
3
3
  require_relative "base"
4
4
  require_relative "../../orchestrator/subagent/budget"
5
5
  require_relative "../../orchestrator/subagent/notices"
6
+ require_relative "../../orchestrator/subagent/admission_policy"
6
7
 
7
8
  module Rubik
8
9
  module REPL
@@ -105,10 +106,9 @@ module Rubik
105
106
  types = sup.type_registry.list
106
107
  ctx.out.puts "=== Available Subagent Archetypes (#{types.size}) ==="
107
108
  types.each do |t|
108
- flags = []
109
- flags << "write" if t.enable_write_tools
110
- flags << "nesting" if t.enable_subagent_tools
111
- flag_str = flags.empty? ? "read-only" : flags.join(", ")
109
+ label = Rubik::Orchestrator::Subagent::AdmissionPolicy.capability_label(t)
110
+ # Same capability vocabulary the prompt's tools= attribute uses.
111
+ flag_str = t.enable_subagent_tools ? "#{label}, nesting" : label
112
112
  ctx.out.puts " • #{t.name.ljust(12)} [#{flag_str}]: #{t.description}"
113
113
  end
114
114
  end
@@ -384,11 +384,31 @@ module Rubik
384
384
  "sandbox.",
385
385
  "subagents.",
386
386
  "plugins.",
387
- "lsp.servers.",
387
+ "lsp.",
388
+ "network.",
389
+ "mcp.",
390
+ "test.",
391
+ "linter.",
392
+ "project.test_command",
393
+ "adapter.",
394
+ "skills.paths",
395
+ "storage.root",
396
+ "buffer.model_can_apply",
397
+ "buffer.auto_apply",
388
398
  "workspace.allow_external_writes",
399
+ "workspace.allowed_external_dirs",
400
+ "workspace.additional_roots",
389
401
  "tools.circuit_breaker."
390
402
  ].freeze
391
403
 
404
+ def self.operator_only_config_key?(key)
405
+ k = key.to_s.strip
406
+ OPERATOR_ONLY_CONFIG_PREFIXES.any? do |p|
407
+ prefix = p.chomp(".")
408
+ k == prefix || k.start_with?("#{prefix}.")
409
+ end
410
+ end
411
+
392
412
  private_class_method def self.register_config_command(shell)
393
413
  shell.register_command("config", description: "Get, set, or view runtime configuration (set|get|list|path)",
394
414
  usage: USAGE_CONFIG, audience: :both) do |args, ctx|
@@ -417,7 +437,7 @@ module Rubik
417
437
  val = clean_args[2..]&.join(" ")
418
438
  if key.nil? || key.empty? || val.nil? || val.empty?
419
439
  ctx.out.puts "Usage: !config set <key> <val> [--project|--global]"
420
- elsif ctx.agent_caller && OPERATOR_ONLY_CONFIG_PREFIXES.any? { |p| key.start_with?(p) }
440
+ elsif ctx.agent_caller && operator_only_config_key?(key)
421
441
  # Security boundary: agents cannot alter security, sandbox, subagent governance,
422
442
  # plugins (arbitrary code execution), LSP binary commands, or external write policies.
423
443
  ctx.out.puts "Security Error: configuration key '#{key}' is operator-only and cannot be altered by autonomous agents."
@@ -56,12 +56,11 @@ module Rubik
56
56
  shell.guard.audit_log_path = val if shell.guard.respond_to?(:audit_log_path=)
57
57
  end
58
58
 
59
- cleanups << config.on_change("network.allow") do |_key, val, _old|
60
- config.set("sandbox.allow_network", val, persist: false) if config.fetch("sandbox.allow_network", nil) != val
61
- end
62
-
63
- cleanups << config.on_change("sandbox.allow_network") do |_key, val, _old|
64
- config.set("network.allow", val, persist: false) if config.fetch("network.allow", nil) != val
59
+ # network.allow is retired: the runner reads sandbox.allow_network
60
+ # only, so the mirror pushed writes into a key nothing reads back.
61
+ # The incantation stays accepted; it warns instead of mirroring.
62
+ cleanups << config.on_change("network.allow") do |_key, _val, _old|
63
+ warn "[ConfigWiring] [DEPRECATION] Config key 'network.allow' is deprecated and has no effect. Use 'sandbox.allow_network' instead."
65
64
  end
66
65
 
67
66
  cleanups
@@ -79,7 +78,17 @@ module Rubik
79
78
  new_roots = exp_roots - current_roots
80
79
  removed_roots = current_roots - exp_roots
81
80
 
82
- new_roots.each { |r| shell.buffer_manager&.add_root(r) }
81
+ new_roots.each do |r|
82
+ if shell.guard
83
+ begin
84
+ shell.guard.verify_root_safety!(r)
85
+ rescue Rubik::Security::BlockedWriteError => e
86
+ warn "[ConfigWiring] #{e.message}. Skipping additional root #{r}."
87
+ next
88
+ end
89
+ end
90
+ shell.buffer_manager&.add_root(r)
91
+ end
83
92
  removed_roots.each { |r| shell.buffer_manager&.remove_root(r) }
84
93
 
85
94
  if (new_roots.any? || removed_roots.any?) &&
@@ -97,6 +106,14 @@ module Rubik
97
106
  removed_dirs = current_dirs - exp_dirs
98
107
 
99
108
  new_dirs.each do |d|
109
+ if shell.guard
110
+ begin
111
+ shell.guard.verify_root_safety!(d)
112
+ rescue Rubik::Security::BlockedWriteError => e
113
+ warn "[ConfigWiring] #{e.message}. Skipping allowed directory #{d}."
114
+ next
115
+ end
116
+ end
100
117
  shell.buffer_manager.add_allowed_dir(d) if shell.buffer_manager.respond_to?(:add_allowed_dir)
101
118
  shell.sandbox_runner&.add_allowed_dir(d)
102
119
  end
@@ -231,7 +231,7 @@ module Rubik
231
231
  @active_loop = orchestrator
232
232
  res = orchestrator.run(text, conversation_history: @conversation_history)
233
233
 
234
- while !res[:ok] && res[:error]&.include?("Max steps budget exceeded") && @config.fetch("agent.ask_continue_on_limit", true)
234
+ while !res[:ok] && res[:reason] == :budget_exhausted && @config.fetch("agent.ask_continue_on_limit", true)
235
235
  @output.puts "\n⚠️ [Rubik::Agent] #{res[:error]}"
236
236
  ans = ask_yes_no(" [Rubik::Agent] +20 adım ile devam edilsin mi?", default: "n")
237
237
  break unless ["y", "yes", "evet"].include?(ans)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../events"
3
4
  require_relative "../log"
4
5
  require_relative "../orchestrator/subagent/notices"
5
6
  require_relative "../prompt/conversation_rows"
@@ -276,8 +277,8 @@ module Rubik
276
277
  tokens = Rubik::Orchestrator::Subagent::Notices::EVENTS.keys.map do |event|
277
278
  [event, @event_bus.subscribe(event) { |payload| handle_subagent_event(event, payload) }]
278
279
  end
279
- tokens << [:unit_state_changed, @event_bus.subscribe(:unit_state_changed) { |payload| handle_unit_event(payload) }]
280
- tokens << [:subagent_message_to_parent, @event_bus.subscribe(:subagent_message_to_parent) { |payload| handle_parent_message(payload) }]
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) }]
281
282
  @subagent_event_tokens = tokens
282
283
  ctx.on_dispose do
283
284
  @subagent_event_tokens&.each { |(event, token)| @event_bus.unsubscribe(event, token) }