rubik 0.8.1 → 0.9.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 (148) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +2 -2
  3. data/CHANGELOG.md +207 -43
  4. data/README.md +5 -1
  5. data/bin/rubik +15 -10
  6. data/lib/rubik/adapter/base.rb +19 -0
  7. data/lib/rubik/adapter/generic.rb +0 -14
  8. data/lib/rubik/adapter/node.rb +2 -0
  9. data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
  10. data/lib/rubik/cli/workspace_trust.rb +17 -0
  11. data/lib/rubik/concurrency/worker.rb +143 -0
  12. data/lib/rubik/concurrency.rb +1 -0
  13. data/lib/rubik/config/deep_ops.rb +17 -0
  14. data/lib/rubik/config/defaults.rb +23 -1
  15. data/lib/rubik/config/schema.rb +0 -1
  16. data/lib/rubik/config/store.rb +14 -0
  17. data/lib/rubik/config.rb +7 -3
  18. data/lib/rubik/context/repo_context_reader.rb +27 -8
  19. data/lib/rubik/events.rb +18 -0
  20. data/lib/rubik/kernel/dispatcher.rb +4 -4
  21. data/lib/rubik/kernel/unit.rb +4 -3
  22. data/lib/rubik/log.rb +2 -1
  23. data/lib/rubik/lsp/client.rb +83 -180
  24. data/lib/rubik/lsp/document_store.rb +209 -0
  25. data/lib/rubik/mcp/client.rb +4 -3
  26. data/lib/rubik/mcp/manager.rb +6 -2
  27. data/lib/rubik/mcp/transport/stdio.rb +51 -9
  28. data/lib/rubik/orchestrator/auto_apply.rb +2 -0
  29. data/lib/rubik/orchestrator/budget_manager.rb +18 -2
  30. data/lib/rubik/orchestrator/goal_ledger/deep_ops.rb +41 -0
  31. data/lib/rubik/orchestrator/goal_ledger/drift.rb +2 -1
  32. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +56 -50
  33. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +26 -0
  34. data/lib/rubik/orchestrator/goal_ledger/patch.rb +10 -17
  35. data/lib/rubik/orchestrator/goal_ledger/suspend_lifecycle.rb +9 -41
  36. data/lib/rubik/orchestrator/goal_ledger/validator.rb +6 -20
  37. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +3 -14
  38. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +21 -14
  39. data/lib/rubik/orchestrator/goal_session.rb +505 -0
  40. data/lib/rubik/orchestrator/goal_state.rb +15 -15
  41. data/lib/rubik/orchestrator/loop.rb +62 -63
  42. data/lib/rubik/orchestrator/lsp_feedback.rb +7 -1
  43. data/lib/rubik/orchestrator/subagent/admission_policy.rb +161 -0
  44. data/lib/rubik/orchestrator/subagent/builtin_types.rb +3 -2
  45. data/lib/rubik/orchestrator/subagent/instance.rb +4 -0
  46. data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
  47. data/lib/rubik/orchestrator/subagent/notices.rb +5 -4
  48. data/lib/rubik/orchestrator/subagent/profile_contract.rb +4 -10
  49. data/lib/rubik/orchestrator/subagent/questioner.rb +21 -6
  50. data/lib/rubik/orchestrator/subagent/run_cycle.rb +22 -1
  51. data/lib/rubik/orchestrator/subagent/scoped_registry.rb +34 -2
  52. data/lib/rubik/orchestrator/subagent/step_runner.rb +1 -1
  53. data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
  54. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  55. data/lib/rubik/orchestrator/subagent/type_registry.rb +19 -1
  56. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +6 -1
  57. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +6 -1
  58. data/lib/rubik/orchestrator/turn_executor.rb +2 -1
  59. data/lib/rubik/orchestrator.rb +1 -0
  60. data/lib/rubik/platform/path.rb +0 -1
  61. data/lib/rubik/platform.rb +15 -0
  62. data/lib/rubik/plugin/storage.rb +20 -29
  63. data/lib/rubik/prompt/context_builder.rb +24 -18
  64. data/lib/rubik/prompt/context_compactor.rb +31 -8
  65. data/lib/rubik/prompt/conversation_rows.rb +33 -0
  66. data/lib/rubik/prompt/doctrine.rb +4 -2
  67. data/lib/rubik/prompt/emergency_compactor.rb +5 -4
  68. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  69. data/lib/rubik/repl/commands/agent_execution.rb +15 -467
  70. data/lib/rubik/repl/commands/buffer.rb +5 -1
  71. data/lib/rubik/repl/commands/lsp.rb +1 -1
  72. data/lib/rubik/repl/commands/mcp.rb +5 -0
  73. data/lib/rubik/repl/commands/subagents.rb +4 -4
  74. data/lib/rubik/repl/commands/workspace.rb +22 -2
  75. data/lib/rubik/repl/config_wiring.rb +24 -7
  76. data/lib/rubik/repl/input_execution.rb +1 -1
  77. data/lib/rubik/repl/input_loop.rb +1 -1
  78. data/lib/rubik/repl/lifecycle.rb +31 -2
  79. data/lib/rubik/repl/pruner/harness.rb +52 -67
  80. data/lib/rubik/repl/pruner/rules.rb +3 -1
  81. data/lib/rubik/repl/service_catalog.rb +1 -1
  82. data/lib/rubik/repl/synthesized_command_context.rb +3 -0
  83. data/lib/rubik/repl/turn_recorder.rb +5 -4
  84. data/lib/rubik/repl.rb +1 -0
  85. data/lib/rubik/runtime/context.rb +5 -0
  86. data/lib/rubik/runtime/discovery.rb +55 -0
  87. data/lib/rubik/runtime/manifest_store.rb +116 -0
  88. data/lib/rubik/runtime/overlay_storage.rb +300 -218
  89. data/lib/rubik/runtime/repair_probe.rb +20 -2
  90. data/lib/rubik/runtime/sealed_coeffects.rb +58 -0
  91. data/lib/rubik/runtime/synthesized_command_context.rb +80 -0
  92. data/lib/rubik/runtime/synthesized_tool_context.rb +123 -0
  93. data/lib/rubik/runtime/task_manager.rb +7 -2
  94. data/lib/rubik/runtime.rb +3 -0
  95. data/lib/rubik/security/allowlist.rb +167 -0
  96. data/lib/rubik/security/audit_log.rb +72 -0
  97. data/lib/rubik/security/classifier/classifier.rb +24 -8
  98. data/lib/rubik/security/classifier/commands/biome.yaml +49 -0
  99. data/lib/rubik/security/classifier/commands/bun.yaml +91 -0
  100. data/lib/rubik/security/classifier/commands/bundle.yaml +107 -0
  101. data/lib/rubik/security/classifier/commands/clang-format.yaml +21 -0
  102. data/lib/rubik/security/classifier/commands/clang.yaml +37 -0
  103. data/lib/rubik/security/classifier/commands/cmake.yaml +37 -0
  104. data/lib/rubik/security/classifier/commands/ctest.yaml +30 -0
  105. data/lib/rubik/security/classifier/commands/gcc.yaml +36 -0
  106. data/lib/rubik/security/classifier/commands/gem.yaml +104 -0
  107. data/lib/rubik/security/classifier/commands/gofmt.yaml +15 -0
  108. data/lib/rubik/security/classifier/commands/pnpm.yaml +8 -8
  109. data/lib/rubik/security/classifier/commands/python.yaml +18 -1
  110. data/lib/rubik/security/classifier/commands/python3.yaml +18 -1
  111. data/lib/rubik/security/classifier/commands/rake.yaml +60 -0
  112. data/lib/rubik/security/classifier/commands/rspec.yaml +42 -0
  113. data/lib/rubik/security/classifier/commands/rubocop.yaml +38 -0
  114. data/lib/rubik/security/classifier/commands/ruby.yaml +25 -0
  115. data/lib/rubik/security/classifier/commands/rustc.yaml +34 -0
  116. data/lib/rubik/security/classifier/commands/zig.yaml +77 -0
  117. data/lib/rubik/security/classifier/database.rb +12 -1
  118. data/lib/rubik/security/classifier/matcher.rb +17 -2
  119. data/lib/rubik/security/classifier/models.rb +3 -2
  120. data/lib/rubik/security/classifier.rb +5 -0
  121. data/lib/rubik/security/command_gate.rb +567 -0
  122. data/lib/rubik/security/guard.rb +96 -989
  123. data/lib/rubik/security/path_policy.rb +117 -0
  124. data/lib/rubik/security/synthesis_gate.rb +300 -0
  125. data/lib/rubik/security.rb +5 -0
  126. data/lib/rubik/storage/ranking.rb +113 -0
  127. data/lib/rubik/storage/store.rb +2 -92
  128. data/lib/rubik/storage/transcript_indexer.rb +2 -3
  129. data/lib/rubik/storage/vakanuvis/harness.rb +49 -89
  130. data/lib/rubik/tools/base.rb +24 -7
  131. data/lib/rubik/tools/builtins/files.rb +2 -0
  132. data/lib/rubik/tools/builtins/goal_ledger.rb +9 -9
  133. data/lib/rubik/tools/builtins/grep.rb +2 -3
  134. data/lib/rubik/tools/builtins/lifecycle.rb +91 -88
  135. data/lib/rubik/tools/builtins/synthesis.rb +124 -64
  136. data/lib/rubik/tools/builtins/unsynthesize.rb +38 -0
  137. data/lib/rubik/tools/catalog.rb +3 -0
  138. data/lib/rubik/tools/declaration.rb +6 -2
  139. data/lib/rubik/tools/lifecycle_rollbacks.rb +87 -0
  140. data/lib/rubik/tools/native_search.rb +2 -1
  141. data/lib/rubik/tools/preflight_runner.rb +107 -14
  142. data/lib/rubik/tools/registry.rb +2 -0
  143. data/lib/rubik/tools/truncator.rb +25 -0
  144. data/lib/rubik/tools/uri_resolver.rb +3 -1
  145. data/lib/rubik/uris.rb +5 -1
  146. data/lib/rubik/version.rb +1 -1
  147. data/lib/rubik.rb +1 -0
  148. metadata +35 -1
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../../coeffects"
4
4
  require_relative "../../tools/catalog"
5
+ require_relative "admission_policy"
5
6
 
6
7
  module Rubik
7
8
  module Orchestrator
@@ -23,19 +24,50 @@ module Rubik
23
24
 
24
25
  def build(source_registry:, type_def:, child_ctx:, buffer:, parent_buffer:)
25
26
  return nil unless source_registry
27
+ raise ArgumentError, "ScopedRegistry.build requires a type_def; a nil type_def would grant every tool" unless type_def
26
28
 
27
29
  new_reg = Rubik::Tools::Registry.new(coeffect_ctx: child_ctx)
30
+ child_ctx.provide(Rubik::Coeffects::TOOL_REGISTRY, new_reg) if child_ctx.respond_to?(:provide)
28
31
  filter = type_def.tool_filter
32
+ unless filter
33
+ raise ArgumentError,
34
+ "type '#{type_def.name}' carries no tool_filter; a missing filter would grant every tool in the " \
35
+ "source registry. Set an explicit filter: ProfileContract.tool_filter(grants) for custom types, " \
36
+ "an archetype lambda for built-ins."
37
+ end
29
38
 
30
39
  source_registry.list.each do |tool|
31
40
  next if ROOT_ONLY_TOOLS.include?(tool.name)
32
- next unless filter.nil? || filter.call(tool.name)
33
41
 
34
- new_reg.register(bind_tool_buffer(tool, buffer, ctx: child_ctx, parent_buffer: parent_buffer))
42
+ origin = source_registry.origin(tool.name)
43
+ next unless tool_allowed?(filter, tool, origin, type_def)
44
+
45
+ new_reg.register(
46
+ bind_tool_buffer(tool, buffer, ctx: child_ctx, parent_buffer: parent_buffer),
47
+ origin: origin || :builtin
48
+ )
35
49
  end
36
50
  new_reg
37
51
  end
38
52
 
53
+ # Executor only: multi-arity filters own their whole decision; the
54
+ # shared arity-1 rule (name filter, then origin/risk) is the
55
+ # AdmissionPolicy's to answer.
56
+ def tool_allowed?(filter, tool, origin, type_def)
57
+ unless filter
58
+ raise ArgumentError,
59
+ "tool_allowed? requires a type_def with a tool_filter; a nil filter would grant every tool"
60
+ end
61
+
62
+ if filter.arity != 1
63
+ return filter.call(tool.name, tool, origin) ? true : false
64
+ end
65
+
66
+ return false unless filter.call(tool.name)
67
+
68
+ AdmissionPolicy.allowed?(tool.name, tool, origin, plane: AdmissionPolicy.plane_for(type_def))
69
+ end
70
+
39
71
  # Buffer-bound tools (rebuilt only when a branch buffer splits them
40
72
  # off the parent); everything else is context-rebind territory.
41
73
  # Membership is declared on each tool's Declaration (buffer_bound:)
@@ -180,7 +180,7 @@ module Rubik
180
180
  # status/wait output, and long payloads are bounded.
181
181
  def tool_detail(args)
182
182
  json = Rubik::Platform.safe_json_generate(Rubik::Security::Redactor.mask_value(args))
183
- json.length > 120 ? "#{json[0, 120]}... [truncated]" : json
183
+ Rubik::Tools::Truncator.clip(json, 120, counted: false)
184
184
  end
185
185
 
186
186
  # 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
@@ -34,6 +34,7 @@ module Rubik
34
34
  private :register_builtin
35
35
 
36
36
  def register(type_def, context: nil, replace: false)
37
+ reject_caller_tool_filter(type_def)
37
38
  profile = validate_registered_profile(type_def)
38
39
  apply_profile_contract(type_def, profile)
39
40
  register_dynamic(type_def, context: context || type_def.context || @default_context, replace: replace)
@@ -168,10 +169,27 @@ module Rubik
168
169
  type_def.system_prompt = profile.system_prompt
169
170
  type_def.requires = profile.requires
170
171
  type_def.grants = profile.grants
171
- type_def.tool_filter = ProfileContract.tool_filter(profile.grants)
172
+ type_def.tool_filter = ProfileContract.tool_filter(profile.grants).tap do |filter|
173
+ # Mark the installed filter so re-registering the same def (the
174
+ # registry mutated it) is not mistaken for a caller-provided one.
175
+ type_def.instance_variable_set(:@contract_filter, filter)
176
+ end
172
177
  type_def.enable_write_tools = profile.grants.include?("write")
173
178
  type_def.enable_subagent_tools = profile.grants.include?("subagent_spawn")
174
179
  end
180
+
181
+ # The registry installs the profile-contract filter and never honors
182
+ # a caller-provided one — accepting it (validating its shape, then
183
+ # silently discarding it) would hide a discarded security decision,
184
+ # so it is rejected up front.
185
+ def reject_caller_tool_filter(type_def)
186
+ return unless type_def.tool_filter
187
+ return if type_def.tool_filter.equal?(type_def.instance_variable_get(:@contract_filter))
188
+
189
+ raise ArgumentError,
190
+ "type '#{type_def.name}' declares tool_filter; the registry installs the profile-contract filter " \
191
+ "(ProfileContract.tool_filter(grants)) and never honors a caller filter — express intent via grants."
192
+ end
175
193
  end
176
194
  end
177
195
  end
@@ -23,7 +23,12 @@ module Rubik
23
23
  def tool_registry = @inst.tool_registry
24
24
 
25
25
  def limits
26
- { context_limit: @inst.context_limit, max_output_tokens: @inst.max_output_tokens, config: @inst.config }
26
+ reserved = if @inst.respond_to?(:tool_registry) && @inst.tool_registry&.schemas
27
+ Rubik::Prompt::TokenEstimator.estimate_tool_schemas(@inst.tool_registry.schemas)
28
+ else
29
+ 0
30
+ end
31
+ { context_limit: @inst.context_limit, max_output_tokens: @inst.max_output_tokens, config: @inst.config, reserved_tokens: reserved }
27
32
  end
28
33
 
29
34
  # Subagent policy (ToolCallId doctrine): deterministic
@@ -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
@@ -4,6 +4,7 @@ require_relative "orchestrator/loop"
4
4
  require_relative "orchestrator/goal_state"
5
5
  require_relative "orchestrator/goal_registry"
6
6
  require_relative "orchestrator/goal_ledger"
7
+ require_relative "orchestrator/goal_session"
7
8
  require_relative "orchestrator/subagent/supervisor"
8
9
 
9
10
  module Rubik
@@ -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
@@ -45,6 +45,21 @@ module Rubik
45
45
  value
46
46
  end
47
47
 
48
+ # Child-process spawn hygiene, single-homed here so every subprocess
49
+ # family shares one scrub list: bundler and Ruby runtime options must
50
+ # never leak into a spawned helper's environment, where a foreign
51
+ # BUNDLE_GEMFILE or RUBYOPT would change what the child loads. Values
52
+ # are nil so Ruby's spawn semantics UNSET each variable instead of
53
+ # setting it; explicit per-server env: configuration merges on top and
54
+ # wins. Consumers: LSP client, MCP stdio transport.
55
+ def sanitized_spawn_env
56
+ env = {}
57
+ ENV.each_key do |key|
58
+ env[key] = nil if key.start_with?("BUNDLE_", "BUNDLER_") || ["RUBYOPT", "RUBYLIB"].include?(key)
59
+ end
60
+ env
61
+ end
62
+
48
63
  def host_os
49
64
  RbConfig::CONFIG["host_os"] || ""
50
65
  end
@@ -7,6 +7,7 @@ require "time"
7
7
  # require edilmesi durumunda da çözümlenebilsin diye açık yüklenir.
8
8
  require_relative "../platform"
9
9
  require_relative "../log"
10
+ require_relative "../runtime/manifest_store"
10
11
  require_relative "package_inventory"
11
12
 
12
13
  module Rubik
@@ -36,40 +37,32 @@ module Rubik
36
37
  end
37
38
  end
38
39
 
40
+ # Shared manifest primitives (issue #426) live on Runtime::ManifestStore;
41
+ # the plugin plane delegates path resolution, load/save, hash recording,
42
+ # hash verification (constant-time, one policy) and the trust-mode seam.
43
+ def manifest_store
44
+ @manifest_store ||= Runtime::ManifestStore.new
45
+ end
46
+
39
47
  def manifest_path(dir)
40
- File.join(dir, ".manifest.json")
48
+ manifest_store.manifest_path(dir)
41
49
  end
42
50
 
43
51
  def load_manifest(dir)
44
- path = manifest_path(dir)
45
- return {} unless File.exist?(path)
46
-
47
- JSON.parse(File.read(path))
48
- rescue JSON::ParserError
49
- {}
52
+ manifest_store.load_manifest(dir)
50
53
  end
51
54
 
52
55
  def save_manifest(dir, manifest)
53
- # Atomik yazım (tmp + rename): kilitsiz okuyucuların yarım manifest görmesini
54
- # engeller — OverlayStorage#save_manifest ile aynı düzen (Platform.atomic_write).
55
- Platform.atomic_write(manifest_path(dir), JSON.pretty_generate(manifest))
56
+ manifest_store.save_manifest(dir, manifest)
56
57
  end
57
58
 
58
59
  def record_hash(path, scope: :project)
59
60
  dir = scope.to_sym == :global ? global_plugins_dir : project_plugins_dir
60
- filename = File.basename(path)
61
-
62
- manifest = load_manifest(dir)
63
- manifest[filename] = {
64
- "sha256" => PackageInventory.file_hash(path),
65
- "scope" => scope.to_s,
66
- "created_at" => Time.now.iso8601
67
- }
68
- save_manifest(dir, manifest)
61
+ manifest_store.record_hash(dir, File.basename(path), PackageInventory.file_hash(path), scope: scope)
69
62
  end
70
63
 
71
64
  def verify_integrity(path, scope: :project, content: nil)
72
- return :trusted if ENV["RUBIK_TRUST_OVERLAYS"] == "1"
65
+ return :trusted if manifest_store.trust_mode?
73
66
 
74
67
  dir = scope.to_sym == :global ? global_plugins_dir : project_plugins_dir
75
68
 
@@ -82,15 +75,13 @@ module Rubik
82
75
  current_content = content || (File.exist?(path) ? File.binread(path) : "")
83
76
  current_hash = PackageInventory.content_hash(current_content)
84
77
 
85
- if manifest.key?(basename)
86
- expected_hash = manifest[basename]["sha256"]
87
- if PackageInventory.secure_compare(current_hash, expected_hash)
88
- :verified
89
- else
90
- warn "[SECURITY] Integrity check failed for plugin '#{basename}': file modified since adding."
91
- :rejected
92
- end
93
- else
78
+ case manifest_store.verify_entry(manifest, basename, current_hash)
79
+ when :verified
80
+ :verified
81
+ when :drift
82
+ warn "[SECURITY] Integrity check failed for plugin '#{basename}': file modified since adding."
83
+ :rejected
84
+ when :unknown
94
85
  warn "[SECURITY] Unknown plugin file '#{basename}' not in manifest. Skipping."
95
86
  :rejected
96
87
  end
@@ -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
@@ -69,7 +69,7 @@ module Rubik
69
69
  rescue Errno::EACCES => e
70
70
  root_path = begin
71
71
  ctx.buffer_manager.root_dir
72
- rescue
72
+ rescue StandardError
73
73
  Dir.pwd
74
74
  end
75
75
  ctx.out.puts Rubik::Tools::DiagnosticReporter.render_permission_denied(root_path, operation: "scan", reason: e.message)