rubik 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +250 -43
  4. data/README.md +5 -1
  5. data/lib/rubik/adapter/base.rb +21 -1
  6. data/lib/rubik/adapter/generic.rb +0 -14
  7. data/lib/rubik/adapter/node.rb +2 -0
  8. data/lib/rubik/adapter/process.rb +28 -67
  9. data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
  10. data/lib/rubik/benchmark/vakanuvis_adoption.rb +10 -3
  11. data/lib/rubik/buffer/apply_contract.rb +3 -3
  12. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  13. data/lib/rubik/buffer/manager.rb +26 -120
  14. data/lib/rubik/cli/workspace_trust.rb +16 -0
  15. data/lib/rubik/config/deep_ops.rb +4 -1
  16. data/lib/rubik/config/defaults.rb +29 -3
  17. data/lib/rubik/config/schema.rb +8 -0
  18. data/lib/rubik/config/secret_policy.rb +197 -0
  19. data/lib/rubik/config/store.rb +14 -0
  20. data/lib/rubik/config.rb +11 -0
  21. data/lib/rubik/diagnostics/collector.rb +0 -9
  22. data/lib/rubik/errors.rb +15 -0
  23. data/lib/rubik/event_bus.rb +2 -1
  24. data/lib/rubik/kernel/dispatcher.rb +16 -11
  25. data/lib/rubik/kernel/unit.rb +4 -1
  26. data/lib/rubik/llm/registry.rb +12 -9
  27. data/lib/rubik/llm/router.rb +2 -5
  28. data/lib/rubik/log.rb +23 -1
  29. data/lib/rubik/lsp/client.rb +152 -445
  30. data/lib/rubik/lsp/document_store.rb +209 -0
  31. data/lib/rubik/lsp/manager.rb +14 -10
  32. data/lib/rubik/lsp/transport.rb +365 -0
  33. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  34. data/lib/rubik/mcp/boot_run.rb +190 -0
  35. data/lib/rubik/mcp/client.rb +4 -3
  36. data/lib/rubik/mcp/manager.rb +46 -165
  37. data/lib/rubik/mcp/transport/stdio.rb +55 -10
  38. data/lib/rubik/orchestrator/auto_apply.rb +4 -3
  39. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  40. data/lib/rubik/orchestrator/goal_session.rb +519 -0
  41. data/lib/rubik/orchestrator/loop.rb +28 -47
  42. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  43. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  44. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  45. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  46. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  47. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  48. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  49. data/lib/rubik/orchestrator/subagent/instance.rb +11 -12
  50. data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
  51. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  52. data/lib/rubik/orchestrator/subagent/questioner.rb +24 -7
  53. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  54. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  55. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  56. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  57. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  58. data/lib/rubik/orchestrator/subagent/step_runner.rb +11 -3
  59. data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
  60. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  61. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  62. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  63. data/lib/rubik/orchestrator.rb +1 -0
  64. data/lib/rubik/platform/io_drain.rb +2 -1
  65. data/lib/rubik/platform/process.rb +144 -54
  66. data/lib/rubik/platform.rb +17 -1
  67. data/lib/rubik/plugin/manager.rb +2 -1
  68. data/lib/rubik/plugin/storage.rb +20 -29
  69. data/lib/rubik/repl/commands/agent_context.rb +9 -8
  70. data/lib/rubik/repl/commands/agent_execution.rb +20 -502
  71. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  72. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  73. data/lib/rubik/repl/commands/base.rb +47 -2
  74. data/lib/rubik/repl/commands/buffer.rb +6 -5
  75. data/lib/rubik/repl/commands/lsp.rb +3 -2
  76. data/lib/rubik/repl/commands/pruner.rb +2 -1
  77. data/lib/rubik/repl/commands/secrets.rb +151 -0
  78. data/lib/rubik/repl/commands/workspace.rb +60 -150
  79. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  80. data/lib/rubik/repl/config_wiring.rb +3 -3
  81. data/lib/rubik/repl/hooks.rb +2 -1
  82. data/lib/rubik/repl/input_execution.rb +158 -76
  83. data/lib/rubik/repl/input_loop.rb +3 -2
  84. data/lib/rubik/repl/lifecycle.rb +82 -204
  85. data/lib/rubik/repl/notice_router.rb +319 -0
  86. data/lib/rubik/repl/paste_support.rb +3 -1
  87. data/lib/rubik/repl/presentation.rb +5 -3
  88. data/lib/rubik/repl/pruner/gate.rb +3 -1
  89. data/lib/rubik/repl/pruner/harness.rb +2 -1
  90. data/lib/rubik/repl/shell.rb +4 -3
  91. data/lib/rubik/repl/turn_recorder.rb +6 -5
  92. data/lib/rubik/runtime/discovery.rb +3 -2
  93. data/lib/rubik/runtime/loop_service.rb +4 -3
  94. data/lib/rubik/runtime/manifest_store.rb +124 -0
  95. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  96. data/lib/rubik/runtime/overlay_storage.rb +269 -272
  97. data/lib/rubik/runtime/task_manager.rb +71 -27
  98. data/lib/rubik/runtime.rb +1 -0
  99. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  100. data/lib/rubik/sandbox/runner.rb +16 -15
  101. data/lib/rubik/security/allowlist.rb +167 -0
  102. data/lib/rubik/security/audit_log.rb +72 -0
  103. data/lib/rubik/security/classifier/classifier.rb +8 -0
  104. data/lib/rubik/security/command_gate.rb +591 -0
  105. data/lib/rubik/security/guard.rb +94 -1077
  106. data/lib/rubik/security/path_policy.rb +117 -0
  107. data/lib/rubik/security/secret_store.rb +463 -0
  108. data/lib/rubik/security/synthesis_gate.rb +297 -0
  109. data/lib/rubik/security.rb +5 -0
  110. data/lib/rubik/session/manager.rb +7 -1
  111. data/lib/rubik/skills/manager.rb +2 -3
  112. data/lib/rubik/storage/ranking.rb +113 -0
  113. data/lib/rubik/storage/search.rb +49 -0
  114. data/lib/rubik/storage/store.rb +15 -112
  115. data/lib/rubik/storage/transcript_indexer.rb +2 -3
  116. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  117. data/lib/rubik/tools/base.rb +22 -0
  118. data/lib/rubik/tools/builtins/files.rb +3 -15
  119. data/lib/rubik/tools/builtins/grep.rb +3 -9
  120. data/lib/rubik/tools/builtins/lifecycle.rb +96 -92
  121. data/lib/rubik/tools/builtins/synthesis.rb +3 -0
  122. data/lib/rubik/tools/lifecycle_rollbacks.rb +100 -0
  123. data/lib/rubik/tools/native_search.rb +2 -1
  124. data/lib/rubik/tools/truncator.rb +73 -36
  125. data/lib/rubik/ui/banner.rb +4 -3
  126. data/lib/rubik/ui/spinner.rb +3 -1
  127. data/lib/rubik/ui/statusline.rb +6 -5
  128. data/lib/rubik/version.rb +1 -1
  129. data/lib/rubik.rb +1 -2
  130. metadata +23 -1
@@ -0,0 +1,297 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../log"
4
+ require_relative "errors"
5
+
6
+ module Rubik
7
+ module Security
8
+ # The synthesis deny surface, split out of Guard (#403): the capability
9
+ # table (the ONE declaration point), its derived projection constants,
10
+ # the regex layer, the RubyVM AST walk, and the spawn-boundary entry
11
+ # (run_synthesis_gate). Receives the shared AuditLog instance — every
12
+ # refusal is audit-logged there.
13
+ class SynthesisGate
14
+ # Synth-unsafe patterns. These catch RCE-y escapes a synthesis step might
15
+ # smuggle past the classifier (tuned for shell commands, not Ruby):
16
+ # `method(:x).call`, variable send/public_send/__send__, eval/binding.eval,
17
+ # and backticks/%x. The interpreter/eval lines precede the backtick and %x
18
+ # patterns so strings merely *mentioning* backticks aren't mislabelled.
19
+ # Symbol-level denials live in SYNTHESIS_CAPABILITIES below; the two
20
+ # layers are equivalence-locked by test_synthesis_capability_coverage.rb.
21
+ DANGEROUS_SYNTHESIS_PATTERNS = [
22
+ /\b(?:send|public_send|__send__)\s*[( \s]/,
23
+ /\.(?:send|public_send|__send__)\b/,
24
+ /\b(?:method|public_method|singleton_method)\s*(?:\(\s*[:"']|\s+[:"'])/i,
25
+ /\beval\s*[( \s]/,
26
+ /\bbinding\s*\.\s*eval\b/i,
27
+ /\b(?:instance_eval|class_eval|module_eval)\b/,
28
+ /\b(?:instance_variable_get|instance_variable_set)\b/,
29
+ /\b(?:const_set|const_get|remove_const)\b/,
30
+ /(?:^|[;{\n\r])\s*(?:system|exec|spawn)\b|\b(?:system|exec|spawn)\s*\(/,
31
+ /(?:^|[;{\n\r])\s*fork\b|\bfork\s*(?:do|\{|\()/,
32
+ /\bsyscall\s*\(/,
33
+ /\bexit!/,
34
+ /\bIO\s*\.\s*popen\b/,
35
+ /\bOpen3\s*\.\s*(?:capture2|capture2e|capture3|popen2|popen2e|popen3|pipeline)\b/,
36
+ /\bProcess\s*\.\s*(?:kill|fork|daemon|spawn|exec)\b/,
37
+ /\bKernel\s*\.\s*(?:exec|system|fork|spawn|syscall|exit!)\b/,
38
+ # Unmanaged background threads survive context disposal — the exact
39
+ # "Leaked Side Effects" anti-pattern (AGENTS.md §5).
40
+ /\bThread\s*\.\s*(?:new|fork|start)\b/,
41
+ /\bPathname\s*\(/,
42
+ /`[^`]*`/,
43
+ Regexp.new('%x\s*[({\[<|/]', Regexp::IGNORECASE)
44
+ ].freeze
45
+
46
+ # ── Synthesis capability table (Candidate 07) ───────────────────────
47
+ # The ONE declaration point for everything the synthesis AST walk
48
+ # denies (docs/explanation/architecture.md §2 "single declaration point" doctrine; §8.3
49
+ # "the seam is enforced where it is declared"). Each row is a capability
50
+ # a synthesized tool must never exercise; layer fields say WHERE the
51
+ # denial bites:
52
+ # nodes — RubyVM node types refused on sight
53
+ # calls — call names refused on ANY call shape (:fcall/:vcall
54
+ # AND :call/:qcall/:attrasgn)
55
+ # bare_calls — names refused on :fcall/:vcall only; receiver forms
56
+ # stay legal (ctx.require is the coeffect accessor)
57
+ # consts — constants refused as call RECEIVERS only
58
+ # any_const — constants refused on ANY :const/:colon3 reference,
59
+ # anti `X = TCPSocket; X.new` aliasing
60
+ # The FORBIDDEN_SYNTHESIS_* constants below are derived projections of
61
+ # this table, kept under their historical names for the walk's call
62
+ # sites; test_synthesis_capability_coverage.rb locks table <-> constant
63
+ # <-> behavior equivalence so the derivation cannot drift. Note: the
64
+ # :block node (grouped expressions) intentionally has NO row —
65
+ # `(a || b).to_i`-style expressions must stay allowed.
66
+ SynthesisCapability = Struct.new(:name, :reason, :nodes, :calls,
67
+ :bare_calls, :consts, :any_const,
68
+ keyword_init: true)
69
+
70
+ SYNTHESIS_CAPABILITIES = [
71
+ SynthesisCapability.new(
72
+ name: :shell_execution,
73
+ reason: "backtick/%x shell escapes execute outside every Ruby-level guard",
74
+ nodes: [:xstr, :dxstr]
75
+ ),
76
+ SynthesisCapability.new(
77
+ name: :code_definition,
78
+ reason: "method/class definitions mutate the eval target; synthesized payloads are single lambdas",
79
+ nodes: [:defn, :defs, :class, :sclass]
80
+ ),
81
+ SynthesisCapability.new(
82
+ name: :method_table_mutation,
83
+ reason: "alias/undef rewrite the method table behind the guard",
84
+ nodes: [:alias, :undef]
85
+ ),
86
+ SynthesisCapability.new(
87
+ name: :global_state,
88
+ reason: "global variables are process-wide ambient state the host context cannot revert",
89
+ nodes: [:gvar, :gasgn]
90
+ ),
91
+ SynthesisCapability.new(
92
+ name: :dynamic_execution,
93
+ reason: "string eval and Binding turn data into code outside the AST walk",
94
+ calls: [:eval, :instance_eval, :class_eval, :module_eval, :binding,
95
+ :instance_exec, :class_exec, :module_exec, :define_method, :define_singleton_method]
96
+ ),
97
+ SynthesisCapability.new(
98
+ name: :process_control,
99
+ reason: "subprocess spawn and process lifecycle control escape the sandbox and audit trail; " \
100
+ "trap/at_exit install process-wide hooks the host cannot revert — Signal.trap can " \
101
+ "swallow the host REPL's Interrupt/exit handling; exit/exit! can kill the host " \
102
+ "when preflight runs in-process",
103
+ calls: [:system, :exec, :spawn, :fork, :syscall, :exit, :exit!, :trap, :at_exit,
104
+ :popen, :capture2, :capture2e, :capture3, :popen2, :popen2e,
105
+ :popen3, :pipeline, :kill, :daemon],
106
+ consts: [:Process, :Thread, :Signal, :Open3]
107
+ ),
108
+ SynthesisCapability.new(
109
+ name: :reflection,
110
+ reason: "send/method-object families bypass static call-name checks (the method(:x).call escape)",
111
+ calls: [:send, :public_send, :__send__,
112
+ :method, :public_method, :singleton_method,
113
+ :instance_method, :public_instance_method, :bind, :bind_call]
114
+ ),
115
+ SynthesisCapability.new(
116
+ name: :state_introspection,
117
+ reason: "object internals and object-graph walks fish for live objects and sidestep the AST deny surface (the method(:x).call family at runtime)",
118
+ calls: [:instance_variable_get, :instance_variable_set],
119
+ consts: [:ObjectSpace]
120
+ ),
121
+ SynthesisCapability.new(
122
+ name: :namespace_mutation,
123
+ reason: "const_get/const_set/remove_const read and rewrite constant namespaces at runtime",
124
+ calls: [:const_get, :const_set, :remove_const]
125
+ ),
126
+ SynthesisCapability.new(
127
+ name: :filesystem,
128
+ reason: "raw file/IO handles bypass the OutputView and every guard write check",
129
+ calls: [:open, :read, :write, :binread, :binwrite, :unlink, :rm_rf, :rmtree,
130
+ :sysopen, :syswrite, :sysread, :readlines, :foreach, :Pathname],
131
+ consts: [:IO, :File, :Dir, :FileUtils, :STDIN, :STDOUT, :STDERR, :ARGF, :Pathname]
132
+ ),
133
+ SynthesisCapability.new(
134
+ name: :code_loading,
135
+ reason: "require/load execute files outside every guard. :require/:require_relative must NOT live in calls: " \
136
+ "ctx.require('database') is the canonical coeffect accessor (receiver call) and every synthesized " \
137
+ "tool depends on it — the bare forms are the escape. Kernel.require/Kernel.load are already caught " \
138
+ "via :Kernel in runtime_root; obj.load would load arbitrary code or deserialize untrusted state",
139
+ calls: [:load, :autoload],
140
+ bare_calls: [:require, :require_relative, :load, :autoload]
141
+ ),
142
+ SynthesisCapability.new(
143
+ name: :runtime_root,
144
+ reason: "core runtime singletons (Kernel/RubyVM/Class/Module/...) re-open every denied capability",
145
+ consts: [:Kernel, :RubyVM, :Object, :Class, :Module, :BasicObject, :Rubik, :Fiber, :TracePoint, :GC]
146
+ ),
147
+ SynthesisCapability.new(
148
+ name: :ambient_state,
149
+ reason: "ENV reads expose provider keys / ambient config and mutate process-wide state the host cannot revert",
150
+ consts: [:ENV]
151
+ ),
152
+ SynthesisCapability.new(
153
+ name: :network_serialization,
154
+ reason: "sockets and Marshal have no legitimate synthesized-tool use; receiver-only checks leave " \
155
+ "`X = TCPSocket; X.new` (:casgn aliasing) wide open, so their mere appearance is a refusal",
156
+ any_const: [:Socket, :IPSocket, :TCPSocket, :TCPServer, :UDPSocket,
157
+ :UNIXSocket, :UNIXServer, :Net, :Marshal]
158
+ )
159
+ ].map do |cap|
160
+ # Deep-freeze: the ONE declaration point for a security gate must be
161
+ # immutable by construction, not by convention — Struct#freeze alone
162
+ # leaves the member arrays mutable.
163
+ cap.members.each { |member| cap[member].freeze if cap[member].is_a?(Array) }
164
+ cap.freeze
165
+ end.freeze
166
+
167
+ # Fold one capability layer into its denial list. Load-time, pure;
168
+ # test_synthesis_capability_coverage.rb pins this helper as the ONLY
169
+ # fold from table to denial constants.
170
+ def self.synthesis_capability_layer(field)
171
+ SYNTHESIS_CAPABILITIES.flat_map { |cap| cap[field].to_a }.freeze
172
+ end
173
+
174
+ FORBIDDEN_SYNTHESIS_NODES = synthesis_capability_layer(:nodes)
175
+ FORBIDDEN_SYNTHESIS_CALLS = synthesis_capability_layer(:calls)
176
+ FORBIDDEN_SYNTHESIS_BARE_CALLS = synthesis_capability_layer(:bare_calls)
177
+ FORBIDDEN_SYNTHESIS_CONSTS = synthesis_capability_layer(:consts)
178
+ FORBIDDEN_SYNTHESIS_NETWORK_CONSTS = synthesis_capability_layer(:any_const)
179
+
180
+ # The ONE synthesis gate: every site about to eval synthesized code
181
+ # comes through here (synthesis builtins AND PreflightRunner's
182
+ # spawn-boundary re-check). A guard lacking verify_synthesis_safety!
183
+ # is downgraded to the shell-only gate — the duck-typed fallback is a
184
+ # test-double affordance, but skipping the entire AST deny surface is
185
+ # security-observable, so the downgrade is loud. Routed through
186
+ # Rubik::Log (:security, issue #452): Log.emit uses $stderr.puts, not
187
+ # Kernel#warn, so the line survives $VERBOSE=nil (bundler/rake -W0),
188
+ # and Log's error isolation absorbs a broken emit target — the gate
189
+ # then proceeds through verify_command_safety!, whose own failures
190
+ # still fail closed.
191
+ def self.run_synthesis_gate(guard, code)
192
+ if guard.respond_to?(:verify_synthesis_safety!)
193
+ guard.verify_synthesis_safety!(code)
194
+ else
195
+ Rubik::Log.warn(:security, "[Synthesize] guard lacks verify_synthesis_safety!; downgraded to shell-only gate")
196
+ guard.verify_command_safety!(code)
197
+ end
198
+ end
199
+
200
+ def initialize(audit:)
201
+ @audit = audit
202
+ end
203
+
204
+ # Reject synthesized Ruby before it is ever eval'd.
205
+ #
206
+ # Defense-in-depth architecture:
207
+ # 1. Fast regex pattern matching (DANGEROUS_SYNTHESIS_PATTERNS) intercepts known keywords.
208
+ # 2. RubyVM::AbstractSyntaxTree semantic walk checks normalized node types, method calls,
209
+ # and receiver constants against explicit deny rules to block AST-level evasion vectors.
210
+ #
211
+ # Raises Rubik::Security::BlockedWriteError with an actionable message.
212
+ def verify_synthesis_safety!(code)
213
+ code_str = code.to_s
214
+
215
+ matched = DANGEROUS_SYNTHESIS_PATTERNS.find { |p| code_str.match?(p) }
216
+ if matched
217
+ @audit.log_audit("blocked_synthesis", code: code_str, reason: "dangerous_synthesis_pattern")
218
+ raise BlockedWriteError, "Dangerous synthesis code blocked by security guard: #{matched.inspect}"
219
+ end
220
+
221
+ synthesize_safety_walk(code_str)
222
+ end
223
+
224
+ def synthesize_safety_walk(code_str)
225
+ ast = begin
226
+ RubyVM::AbstractSyntaxTree.parse(code_str)
227
+ rescue ArgumentError, SyntaxError
228
+ # Code with syntax errors cannot execute; let synthesis eval safely report
229
+ # a SyntaxError to the user rather than misclassifying a syntax typo as a security breach.
230
+ nil
231
+ end
232
+
233
+ return true if ast.nil?
234
+
235
+ synthesize_safety_check_node(ast)
236
+
237
+ true
238
+ end
239
+
240
+ # Recursively walk the AST, refusing if any disallowed construct is encountered.
241
+ def synthesize_safety_check_node(node)
242
+ return true unless node.respond_to?(:type)
243
+
244
+ type = node.type.to_s.downcase.to_sym
245
+ reject_unsafe(node, "node type :#{type}") if FORBIDDEN_SYNTHESIS_NODES.include?(type)
246
+
247
+ case type
248
+ when :const, :colon3
249
+ # :const covers Net::HTTP / Marshal.dump receivers, `X = Socket`
250
+ # RHS values, and local aliasing `k = Kernel`; :colon3 covers top-level scoped `::TCPSocket`.
251
+ name = node.children[0]
252
+ forbidden_const = FORBIDDEN_SYNTHESIS_NETWORK_CONSTS.include?(name) || FORBIDDEN_SYNTHESIS_CONSTS.include?(name)
253
+ reject_unsafe(node, "reference to forbidden constant :#{name}") if forbidden_const
254
+ when :colon2
255
+ # Scoped constant reference like Rubik::Security::Guard
256
+ name = node.children[1]
257
+ forbidden_const = FORBIDDEN_SYNTHESIS_NETWORK_CONSTS.include?(name) || FORBIDDEN_SYNTHESIS_CONSTS.include?(name)
258
+ reject_unsafe(node, "reference to forbidden constant :#{name}") if forbidden_const
259
+ when :fcall, :vcall
260
+ name = node.children[0]
261
+ reject_unsafe(node, "call to :#{name}") if FORBIDDEN_SYNTHESIS_CALLS.include?(name)
262
+ reject_unsafe(node, "bare call to :#{name}") if FORBIDDEN_SYNTHESIS_BARE_CALLS.include?(name)
263
+ when :call, :qcall, :attrasgn
264
+ recv = node.children[0]
265
+ name = node.children[1]
266
+
267
+ reject_unsafe(node, "call to :#{name}") if FORBIDDEN_SYNTHESIS_CALLS.include?(name)
268
+
269
+ # Receiver const check must accept const node shapes: :const, :colon3, and :colon2
270
+ if recv.respond_to?(:type)
271
+ recv_type = recv.type.to_s.downcase.to_sym
272
+ if [:const, :colon3].include?(recv_type)
273
+ const_name = recv.children[0]
274
+ reject_unsafe(node, "access to #{const_name}.#{name}") if FORBIDDEN_SYNTHESIS_CONSTS.include?(const_name)
275
+ elsif recv_type == :colon2
276
+ const_name = recv.children[1]
277
+ reject_unsafe(node, "access to #{const_name}.#{name}") if FORBIDDEN_SYNTHESIS_CONSTS.include?(const_name)
278
+ end
279
+ end
280
+ end
281
+
282
+ node.children.compact.each do |c|
283
+ synthesize_safety_check_node(c) if c.respond_to?(:type)
284
+ end
285
+ true
286
+ end
287
+
288
+ def reject_unsafe(node, detail = nil)
289
+ kind = node.type.inspect
290
+ reason = detail ? "unsafe_#{detail.to_s.gsub(/\s+/, '_')}" : "unsafe_node_#{kind}"
291
+ @audit.log_audit("blocked_synthesis", code: node.children[0].to_s, reason: reason)
292
+ raise BlockedWriteError,
293
+ "Synthesized code was refused by the security guard: #{detail || "a #{kind} node"} is not permitted in synthesized tools."
294
+ end
295
+ end
296
+ end
297
+ end
@@ -2,6 +2,11 @@
2
2
 
3
3
  require_relative "security/errors"
4
4
  require_relative "security/redactor"
5
+ require_relative "security/audit_log"
6
+ require_relative "security/allowlist"
7
+ require_relative "security/path_policy"
8
+ require_relative "security/synthesis_gate"
9
+ require_relative "security/command_gate"
5
10
  require_relative "security/guard"
6
11
  require_relative "security/classifier"
7
12
 
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "English"
3
4
  require "fileutils"
4
5
  require "json"
6
+ require_relative "../log"
5
7
  require_relative "replayer"
6
8
  require_relative "../llm/message_sanitizer"
7
9
  require_relative "../prompt/context_compactor"
@@ -160,7 +162,11 @@ module Rubik
160
162
 
161
163
  begin
162
164
  event = JSON.parse(line)
163
- rescue JSON::ParserError
165
+ rescue JSON::ParserError => e
166
+ # A corrupt line is dropped from the replayed history — say so
167
+ # instead of losing the row in silence (persistence-path
168
+ # honesty; partial writes after a crash are the normal cause).
169
+ Rubik::Log.warn(:session, "session restore: dropping unparseable line #{$INPUT_LINE_NUMBER} from #{session_file} (#{e.class}: #{e.message.to_s.chars.first(80).join})")
164
170
  next
165
171
  end
166
172
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "monitor"
4
4
  require_relative "frontmatter"
5
+ require_relative "../storage/layout"
5
6
  require_relative "storage"
6
7
  require_relative "registry"
7
8
 
@@ -208,9 +209,7 @@ module Rubik
208
209
  # fetch-with-default without the `|| default` idiom: an explicit
209
210
  # config value of false/nil must survive (see Loop#safe_config hazard).
210
211
  def fetch(key, default)
211
- return default unless @config.respond_to?(:fetch)
212
-
213
- @config.fetch(key, default)
212
+ Rubik::Storage::Layout.config_get(@config, key, default)
214
213
  end
215
214
 
216
215
  # Encoding hygiene for File.read output reaching the model/history
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module Storage
5
+ # BM25-lite relevance ranking (vakanuvis-v2 §4-V8, issue #408): the
6
+ # single home of the scored-search math. Store keeps tokenization, the
7
+ # shared @records_cache and corpus assembly; Ranking owns the candidate
8
+ # gate, corpus statistics (total/avgdl/df), Okapi scoring, the field
9
+ # tier bonus and the final ordering. `rank` is pure over its inputs —
10
+ # same corpus + same tokens + same require_all ⇒ same order, every
11
+ # replay (replay/pin contract below).
12
+ module Ranking
13
+ module_function
14
+
15
+ # BM25-lite (vakanüvis-v2 §4-V8): klasik Okapi değerleri — k1 terim
16
+ # doygunluğu, b uzunluk normalizasyonu ağırlığı.
17
+ BM25_K1 = 1.2
18
+ BM25_B = 0.75
19
+ # Alan kademeli bonus (issue #99 P3): BM25 birincil kalır; başlıkta
20
+ # geçen sorgu terimi etikette geçene, o da yalnız gövdede geçene
21
+ # üstünlük kurar. Terim başına birimdir ve tipik idf katkısının
22
+ # altındadır — maddesel BM25 farklarını ezmez, sadece eşit-yakın
23
+ # skorları alan bilgisiyle kırar.
24
+ TITLE_TIER_BONUS = 1.0
25
+ TAGS_TIER_BONUS = 0.5
26
+
27
+ # One public entry. Store#search_ranked supplies the corpus entries —
28
+ # each `{records:, haystacks:, folded_fields:, has:}` where `has` is
29
+ # the per-token include? matrix produced by the single AND/OR pass —
30
+ # so df comes from that pass alone and tf is scanned only on matching
31
+ # records. Ordering: score desc → updated_at desc → [type, row].
32
+ def rank(tokens:, corpus:, require_all:)
33
+ return [] if corpus.empty?
34
+
35
+ total = corpus.sum { |c| c[:records].size }
36
+ avgdl = corpus.sum { |c| c[:haystacks].sum(&:bytesize) }.fdiv(total)
37
+ dfs = tokens.each_index.map { |ti| corpus.sum { |c| c[:has][ti].count(true) } }
38
+
39
+ gate = ranked_gate(require_all)
40
+ scored = corpus.flat_map do |c|
41
+ c[:records].each_index.select { |i| gate.call(c[:has], i) }.map do |i|
42
+ score = tokens.each_with_index.sum do |token, ti|
43
+ df = dfs[ti]
44
+ next 0.0 if df.zero?
45
+
46
+ # PERF: count_occurrences using String#index avoids Array allocation of String#scan
47
+ tf = count_occurrences(c[:haystacks][i], token)
48
+ next 0.0 if tf.zero?
49
+
50
+ idf = Math.log(((total - df + 0.5) / (df + 0.5)) + 1)
51
+ dl = c[:haystacks][i].bytesize
52
+ idf * (tf * (BM25_K1 + 1)) / (tf + (BM25_K1 * (1 - BM25_B + (BM25_B * dl / avgdl))))
53
+ end
54
+ # PERF: pass cached folded title and tags to avoid String folding & array join allocations per score
55
+ f_title, f_tags = c[:folded_fields][i]
56
+ [score + field_tier_bonus(tokens, f_title, f_tags), c[:records][i]]
57
+ end
58
+ end
59
+ ordered = scored.sort do |(score_a, rec_a), (score_b, rec_b)|
60
+ (score_b <=> score_a).nonzero? ||
61
+ (rec_b["updated_at"].to_s <=> rec_a["updated_at"].to_s).nonzero? ||
62
+ (rec_a["type"].to_s <=> rec_b["type"].to_s).nonzero? ||
63
+ (rec_a["row"].to_i <=> rec_b["row"].to_i)
64
+ end
65
+ ordered.map { |_score, record| record }
66
+ end
67
+
68
+ # Aday eşiği: require_all (AND — default, recall) ya da OR
69
+ # gevşetmesi (fallback — issue #99 P2).
70
+ def ranked_gate(require_all)
71
+ if require_all
72
+ ->(has, i) { has.all? { |row| row[i] } }
73
+ else
74
+ ->(has, i) { has.any? { |row| row[i] } }
75
+ end
76
+ end
77
+
78
+ # PERF: Zero-allocation string occurrence counting via String#index loop.
79
+ # Replaces String#scan(token).size to avoid array allocation per matched term.
80
+ def count_occurrences(haystack, token)
81
+ return 0 if token.empty?
82
+
83
+ count = 0
84
+ offset = 0
85
+ token_len = token.length
86
+ while (idx = haystack.index(token, offset))
87
+ count += 1
88
+ offset = idx + token_len
89
+ end
90
+ count
91
+ end
92
+
93
+ # Terim başına alan izi: katlanmış başlıkta geçen terim +1.0,
94
+ # yalnız katlanmış etiketlerde geçen +0.5, gövde izi 0 — bkz.
95
+ # TITLE_TIER_BONUS/TAGS_TIER_BONUS. Yalnız eşik geçen kayıtlar için
96
+ # hesaplanır (ek tarama yok); başlık/etiket önceden katlanmış
97
+ # dizgeler olarak corpus'tan gelir (Store memoize'ı).
98
+ def field_tier_bonus(tokens, title, tags)
99
+ sum = 0.0
100
+ tokens.each do |token|
101
+ if title.include?(token)
102
+ sum += TITLE_TIER_BONUS
103
+ elsif tags.include?(token)
104
+ sum += TAGS_TIER_BONUS
105
+ end
106
+ end
107
+ sum
108
+ end
109
+
110
+ private_class_method :ranked_gate, :count_occurrences, :field_tier_bonus
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "folding"
4
+
5
+ module Rubik
6
+ module Storage
7
+ # The single home of the search scoring pipeline's pure text layer
8
+ # (issue #473): tokenization, haystack construction, per-record folded
9
+ # fields and the token-AND candidate gate. Every function here is pure
10
+ # over its inputs — no I/O, no cache, no Store state — so the scoring
11
+ # path is unit-testable without the filesystem. Store keeps persistence,
12
+ # the shared @records_cache memoization and corpus assembly; Ranking
13
+ # owns the BM25-lite math; Search owns everything that feeds it.
14
+ module Search
15
+ module_function
16
+
17
+ # Sorgu katlaması + tokenizasyonu (Folding.fold — TR I-sınıfı dahil
18
+ # tek katlama kuralı, folding.rb). Token-AND semantiğinin girdisi:
19
+ # her boşlukla ayrılmış terim, kaydın samanlığında geçmeli. Tek
20
+ # substring araması "ratelimit-lock regression 11/11" gibi doğal çok
21
+ # kelimeli sorguları 0 sonuçla bıraktığı için terimlere bölünür
22
+ # (js-tangram olay raporu 2026-10-04, olay #2); tek terimli sorgular
23
+ # eski davranışın birebir aynısı.
24
+ def tokens(query)
25
+ Folding.fold(query).split
26
+ end
27
+
28
+ # Arama samanlığı: title+body+tags, tek katlama. Kayıt kümesi
29
+ # paylaşımlı/frozen olduğundan sonuç da mutasyona kapalıdır
30
+ # (include?/scan/bytesize mutasyon yapmaz).
31
+ def haystack_of(record)
32
+ Folding.fold("#{record['title']}\n#{record['body']}\n#{Array(record['tags']).join("\n")}")
33
+ end
34
+
35
+ # Sıralama katmanı için kayıt başına katlanmış [başlık, etiket] çifti
36
+ # (Ranking#field_tier_bonus'un alan kademeli bonus girdisi).
37
+ def folded_fields_of(record)
38
+ [Folding.fold(record["title"]), Folding.fold(Array(record["tags"]).join("\n"))]
39
+ end
40
+
41
+ # Token-AND aday üretimi: her terim kaydın samanlığında geçmeli.
42
+ # Kayıt ve samanlık dizileri aynı sırada (each_index hizalı) olmalıdır;
43
+ # eşleşen kayıtların indekslerini döndürür.
44
+ def matching_indices(records, haystacks, tokens)
45
+ records.each_index.select { |i| tokens.all? { |token| haystacks[i].include?(token) } }
46
+ end
47
+ end
48
+ end
49
+ end