rubik 0.9.0 → 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.
- checksums.yaml +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +108 -43
- data/README.md +5 -1
- data/lib/rubik/adapter/base.rb +19 -0
- data/lib/rubik/adapter/generic.rb +0 -14
- data/lib/rubik/adapter/node.rb +2 -0
- data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
- data/lib/rubik/cli/workspace_trust.rb +16 -0
- data/lib/rubik/config/defaults.rb +23 -0
- data/lib/rubik/config/store.rb +14 -0
- data/lib/rubik/kernel/dispatcher.rb +4 -4
- data/lib/rubik/log.rb +2 -1
- data/lib/rubik/lsp/client.rb +83 -180
- data/lib/rubik/lsp/document_store.rb +209 -0
- data/lib/rubik/mcp/client.rb +4 -3
- data/lib/rubik/mcp/manager.rb +6 -2
- data/lib/rubik/mcp/transport/stdio.rb +51 -9
- data/lib/rubik/orchestrator/goal_session.rb +505 -0
- data/lib/rubik/orchestrator/subagent/instance.rb +4 -0
- data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
- data/lib/rubik/orchestrator/subagent/questioner.rb +21 -6
- data/lib/rubik/orchestrator/subagent/step_runner.rb +1 -1
- data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
- data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
- data/lib/rubik/orchestrator.rb +1 -0
- data/lib/rubik/platform.rb +15 -0
- data/lib/rubik/plugin/storage.rb +20 -29
- data/lib/rubik/repl/commands/agent_context.rb +1 -1
- data/lib/rubik/repl/commands/agent_execution.rb +7 -470
- data/lib/rubik/repl/commands/buffer.rb +1 -1
- data/lib/rubik/repl/commands/lsp.rb +1 -1
- data/lib/rubik/repl/input_loop.rb +1 -1
- data/lib/rubik/repl/lifecycle.rb +28 -0
- data/lib/rubik/runtime/manifest_store.rb +116 -0
- data/lib/rubik/runtime/overlay_storage.rb +247 -196
- data/lib/rubik/runtime/task_manager.rb +7 -2
- data/lib/rubik/runtime.rb +1 -0
- data/lib/rubik/security/allowlist.rb +167 -0
- data/lib/rubik/security/audit_log.rb +72 -0
- data/lib/rubik/security/classifier/classifier.rb +8 -0
- data/lib/rubik/security/command_gate.rb +567 -0
- data/lib/rubik/security/guard.rb +88 -1075
- data/lib/rubik/security/path_policy.rb +117 -0
- data/lib/rubik/security/synthesis_gate.rb +300 -0
- data/lib/rubik/security.rb +5 -0
- data/lib/rubik/storage/ranking.rb +113 -0
- data/lib/rubik/storage/store.rb +2 -92
- data/lib/rubik/storage/transcript_indexer.rb +2 -3
- data/lib/rubik/tools/builtins/grep.rb +2 -3
- data/lib/rubik/tools/builtins/lifecycle.rb +90 -87
- data/lib/rubik/tools/builtins/synthesis.rb +3 -0
- data/lib/rubik/tools/lifecycle_rollbacks.rb +87 -0
- data/lib/rubik/tools/native_search.rb +2 -1
- data/lib/rubik/tools/truncator.rb +25 -0
- data/lib/rubik/version.rb +1 -1
- metadata +11 -1
|
@@ -7,6 +7,7 @@ require "time"
|
|
|
7
7
|
# save_manifest atomik yazımı Platform.atomic_write kullanır; dosyanın tek başına
|
|
8
8
|
# require edilmesi durumunda da çözümlenebilsin diye açık yüklenir.
|
|
9
9
|
require_relative "../platform"
|
|
10
|
+
require_relative "../plugin/package_inventory"
|
|
10
11
|
require_relative "synthesized_command_context"
|
|
11
12
|
require_relative "synthesized_tool_context"
|
|
12
13
|
|
|
@@ -27,6 +28,15 @@ module Rubik
|
|
|
27
28
|
ensure_directories
|
|
28
29
|
end
|
|
29
30
|
|
|
31
|
+
# Shared manifest primitives (issue #426) live on ManifestStore; this
|
|
32
|
+
# store wires the overlay-dir validation + containment checks into it.
|
|
33
|
+
def manifest_store
|
|
34
|
+
@manifest_store ||= ManifestStore.new(validate_path: lambda { |path, dir|
|
|
35
|
+
ensure_valid_overlay_dir!(dir)
|
|
36
|
+
ensure_contained_path!(path, dir)
|
|
37
|
+
})
|
|
38
|
+
end
|
|
39
|
+
|
|
30
40
|
def project_commands_dir
|
|
31
41
|
File.join(@root_dir, ".rubik", "commands")
|
|
32
42
|
end
|
|
@@ -52,27 +62,15 @@ module Rubik
|
|
|
52
62
|
end
|
|
53
63
|
|
|
54
64
|
def manifest_path(dir)
|
|
55
|
-
|
|
56
|
-
path = File.join(dir, ".manifest.json")
|
|
57
|
-
ensure_contained_path!(path, dir)
|
|
58
|
-
path
|
|
65
|
+
manifest_store.manifest_path(dir)
|
|
59
66
|
end
|
|
60
67
|
|
|
61
68
|
def load_manifest(dir)
|
|
62
|
-
|
|
63
|
-
return {} unless File.exist?(path)
|
|
64
|
-
|
|
65
|
-
JSON.parse(File.read(path))
|
|
66
|
-
rescue JSON::ParserError, ArgumentError
|
|
67
|
-
{}
|
|
69
|
+
manifest_store.load_manifest(dir)
|
|
68
70
|
end
|
|
69
71
|
|
|
70
72
|
def save_manifest(dir, manifest)
|
|
71
|
-
|
|
72
|
-
# yarım manifest görmesini engeller; yarım okuma, kayıtlı overlay'lerin "unknown"
|
|
73
|
-
# sayılıp kalıcı olarak reddedilmesine yol açabiliyordu. Platform.atomic_write,
|
|
74
|
-
# Windows için unlink/rename yedeğiyle bu düzeni sağlar.
|
|
75
|
-
Platform.atomic_write(manifest_path(dir), JSON.pretty_generate(manifest))
|
|
73
|
+
manifest_store.save_manifest(dir, manifest)
|
|
76
74
|
end
|
|
77
75
|
|
|
78
76
|
def normalize_content(raw)
|
|
@@ -80,23 +78,12 @@ module Rubik
|
|
|
80
78
|
end
|
|
81
79
|
|
|
82
80
|
def record_hash(dir, filename, scope)
|
|
83
|
-
|
|
84
|
-
if manifest_state(dir) == :corrupt
|
|
85
|
-
# Bozuk manifesti sessizce "heal" etmek (yalnızca yeni kaydı yazmak) diğer
|
|
86
|
-
# kayıtlı overlay'leri tahliye eder; bunun yerine kaydı reddet.
|
|
87
|
-
raise "cannot record '#{filename}': .manifest.json in #{dir} is corrupt; delete it and re-record overlays"
|
|
88
|
-
end
|
|
81
|
+
refuse_corrupt_manifest!(dir, :record, filename)
|
|
89
82
|
|
|
90
83
|
path = File.join(dir, filename)
|
|
91
84
|
ensure_contained_path!(path, dir)
|
|
92
85
|
hash = Digest::SHA256.hexdigest(normalize_content(File.binread(path)))
|
|
93
|
-
|
|
94
|
-
manifest[filename] = {
|
|
95
|
-
"sha256" => hash,
|
|
96
|
-
"scope" => scope.to_s,
|
|
97
|
-
"created_at" => Time.now.iso8601
|
|
98
|
-
}
|
|
99
|
-
save_manifest(dir, manifest)
|
|
86
|
+
manifest_store.record_hash(dir, filename, hash, scope: scope)
|
|
100
87
|
end
|
|
101
88
|
|
|
102
89
|
def verify_integrity(file_path, dir, bootstrap_eligible: false)
|
|
@@ -112,86 +99,53 @@ module Rubik
|
|
|
112
99
|
current_content = File.exist?(file_path) ? File.binread(file_path) : ""
|
|
113
100
|
current_hash = Digest::SHA256.hexdigest(normalize_content(current_content))
|
|
114
101
|
|
|
115
|
-
|
|
116
|
-
|
|
102
|
+
case manifest_store.verify_entry(manifest, basename, current_hash)
|
|
103
|
+
when :verified
|
|
104
|
+
:verified
|
|
105
|
+
when :drift
|
|
106
|
+
# Constant-time comparison policy is single-homed in ManifestStore
|
|
107
|
+
# (issue #426), same as plugin verification.
|
|
117
108
|
expected_hash = manifest[basename]["sha256"]
|
|
118
|
-
|
|
119
|
-
|
|
109
|
+
warn "[SECURITY] Integrity check failed for overlay '#{basename}': file modified " \
|
|
110
|
+
"since synthesis. Skipping. (expected: #{expected_hash}, got: #{current_hash})"
|
|
111
|
+
:rejected
|
|
112
|
+
when :unknown
|
|
113
|
+
if bootstrap_eligible && sentinel_content?(current_content)
|
|
114
|
+
# Bootstrap (tek seferlik): yalnızca manifest dosyasının hiç var olmadığı ilk taramada,
|
|
115
|
+
# sentinel başlıklı dosya kabul edilip hash'i manifest'e yazılır; uygunluk kararını
|
|
116
|
+
# verified_dir_entries taramanın başında verir. Manifest varken bootstrap asla çalışmaz.
|
|
117
|
+
# Kabul ÖNCE synthesis guard'ından geçer (Aday 07 ertelemesi): klonlanan depodaki
|
|
118
|
+
# dosya bu ortamda sentezlenmemişti; reddedilen dosya manifest'e yazılmaz.
|
|
119
|
+
verdict = screen_bootstrap_content(current_content, basename)
|
|
120
|
+
return verdict unless verdict == :ok
|
|
121
|
+
|
|
122
|
+
scope_match = current_content.match(/# Scope:\s*(.*)/)
|
|
123
|
+
scope = scope_match ? scope_match[1].strip : "unknown"
|
|
124
|
+
|
|
125
|
+
manifest[basename] = {
|
|
126
|
+
"sha256" => current_hash,
|
|
127
|
+
"scope" => scope,
|
|
128
|
+
"created_at" => Time.now.iso8601
|
|
129
|
+
}
|
|
130
|
+
save_manifest(dir, manifest)
|
|
131
|
+
:bootstrapped
|
|
120
132
|
else
|
|
121
|
-
warn "[SECURITY]
|
|
122
|
-
"since synthesis. Skipping. (expected: #{expected_hash}, got: #{current_hash})"
|
|
133
|
+
warn "[SECURITY] Unknown overlay file '#{basename}' not in manifest. Skipping."
|
|
123
134
|
:rejected
|
|
124
135
|
end
|
|
125
|
-
elsif bootstrap_eligible && sentinel_content?(current_content)
|
|
126
|
-
# Bootstrap (tek seferlik): yalnızca manifest dosyasının hiç var olmadığı ilk taramada,
|
|
127
|
-
# sentinel başlıklı dosya kabul edilip hash'i manifest'e yazılır; uygunluk kararını
|
|
128
|
-
# verified_dir_entries taramanın başında verir. Manifest varken bootstrap asla çalışmaz.
|
|
129
|
-
# Kabul ÖNCE synthesis guard'ından geçer (Aday 07 ertelemesi): klonlanan depodaki
|
|
130
|
-
# dosya bu ortamda sentezlenmemişti; reddedilen dosya manifest'e yazılmaz.
|
|
131
|
-
verdict = screen_bootstrap_content(current_content, basename)
|
|
132
|
-
return verdict unless verdict == :ok
|
|
133
|
-
|
|
134
|
-
scope_match = current_content.match(/# Scope:\s*(.*)/)
|
|
135
|
-
scope = scope_match ? scope_match[1].strip : "unknown"
|
|
136
|
-
|
|
137
|
-
manifest[basename] = {
|
|
138
|
-
"sha256" => current_hash,
|
|
139
|
-
"scope" => scope,
|
|
140
|
-
"created_at" => Time.now.iso8601
|
|
141
|
-
}
|
|
142
|
-
save_manifest(dir, manifest)
|
|
143
|
-
:bootstrapped
|
|
144
|
-
else
|
|
145
|
-
warn "[SECURITY] Unknown overlay file '#{basename}' not in manifest. Skipping."
|
|
146
|
-
:rejected
|
|
147
136
|
end
|
|
148
137
|
end
|
|
149
138
|
|
|
139
|
+
# Issue #407: the command/tool quadruplets collapse onto shared skeletons
|
|
140
|
+
# (save / delete / load_entries in the private section) behind the Kind
|
|
141
|
+
# adapter at the bottom of this file. Public surface unchanged: these
|
|
142
|
+
# delegators keep the exact pre-refactor signatures and per-kind defaults.
|
|
150
143
|
def save_command(name, code, scope: :project, description: "Custom command")
|
|
151
|
-
|
|
152
|
-
target_dir = scope.to_sym == :global ? global_commands_dir : project_commands_dir
|
|
153
|
-
FileUtils.mkdir_p(target_dir)
|
|
154
|
-
|
|
155
|
-
filename = "#{clean_name.gsub(':', '_')}.rb"
|
|
156
|
-
file_path = File.join(target_dir, filename)
|
|
157
|
-
ensure_contained_path!(file_path, target_dir)
|
|
158
|
-
|
|
159
|
-
header = <<~RUBY
|
|
160
|
-
# frozen_string_literal: true
|
|
161
|
-
#{COMMAND_SENTINEL} !#{clean_name}
|
|
162
|
-
# Scope: #{scope}
|
|
163
|
-
# Description: #{description}
|
|
164
|
-
|
|
165
|
-
RUBY
|
|
166
|
-
|
|
167
|
-
File.write(file_path, "#{header}#{code.strip}\n")
|
|
168
|
-
record_hash(target_dir, filename, scope)
|
|
169
|
-
{ ok: true, path: file_path, name: clean_name, scope: scope }
|
|
170
|
-
rescue StandardError => e
|
|
171
|
-
{ ok: false, error: e.message }
|
|
144
|
+
save(COMMAND_KIND, name, code, scope: scope, description: description)
|
|
172
145
|
end
|
|
173
146
|
|
|
174
|
-
# Delete a synthesized command from disk and manifest. Symmetric to
|
|
175
|
-
# save_command: same clean_name normalization, scope→dir selection and
|
|
176
|
-
# { ok:, error: } shape. Fail-closed on a corrupt manifest — never wipe
|
|
177
|
-
# it with {} (mirror the record_hash guard on the save path).
|
|
178
147
|
def delete_command(name, scope: :project)
|
|
179
|
-
|
|
180
|
-
target_dir = scope.to_sym == :global ? global_commands_dir : project_commands_dir
|
|
181
|
-
filename = "#{clean_name.gsub(':', '_')}.rb"
|
|
182
|
-
file_path = File.join(target_dir, filename)
|
|
183
|
-
ensure_contained_path!(file_path, target_dir)
|
|
184
|
-
|
|
185
|
-
raise "cannot delete '#{filename}': .manifest.json in #{target_dir} is corrupt; delete it and re-record overlays" if manifest_state(target_dir) == :corrupt
|
|
186
|
-
|
|
187
|
-
manifest = load_manifest(target_dir)
|
|
188
|
-
manifest.delete(filename)
|
|
189
|
-
save_manifest(target_dir, manifest)
|
|
190
|
-
|
|
191
|
-
FileUtils.rm_f(file_path)
|
|
192
|
-
{ ok: true, name: clean_name, scope: scope, path: file_path }
|
|
193
|
-
rescue StandardError => e
|
|
194
|
-
{ ok: false, error: e.message }
|
|
148
|
+
delete(COMMAND_KIND, name, scope: scope)
|
|
195
149
|
end
|
|
196
150
|
|
|
197
151
|
def list_commands
|
|
@@ -201,34 +155,31 @@ module Rubik
|
|
|
201
155
|
end
|
|
202
156
|
|
|
203
157
|
def load_all(shell, _manager = nil)
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
file_path = cmd_meta[:path]
|
|
209
|
-
cmd_name = cmd_meta[:name]
|
|
210
|
-
target_dir = cmd_meta[:scope].to_sym == :global ? global_commands_dir : project_commands_dir
|
|
211
|
-
|
|
212
|
-
begin
|
|
213
|
-
ensure_contained_path!(file_path, target_dir)
|
|
214
|
-
code = File.read(file_path)
|
|
215
|
-
eval_command(shell, cmd_name, code, cmd_meta[:scope])
|
|
216
|
-
loaded << cmd_name
|
|
217
|
-
rescue StandardError => e
|
|
218
|
-
warn "[OVERLAY] Failed to load custom command '#{cmd_name}' from #{file_path}: #{e.class}: #{e.message}"
|
|
219
|
-
end
|
|
158
|
+
load_entries(COMMAND_KIND, list_commands) do |name, code, scope|
|
|
159
|
+
eval_command(shell, name, code, scope)
|
|
160
|
+
true
|
|
220
161
|
end
|
|
221
|
-
loaded
|
|
222
162
|
end
|
|
223
163
|
|
|
224
164
|
def eval_command(shell, cmd_name, code, scope)
|
|
225
|
-
|
|
226
|
-
description = desc_match ? desc_match[1].strip : "Custom #{scope} command"
|
|
165
|
+
description = overlay_description(code, scope, "command")
|
|
227
166
|
|
|
228
167
|
proc_handler = if code.include?("->") || code.include?("lambda") || code.include?("proc")
|
|
229
|
-
|
|
168
|
+
overlay_eval(code)
|
|
230
169
|
else
|
|
231
|
-
|
|
170
|
+
# Sealed default (issue #398): a bare body (no proc marker)
|
|
171
|
+
# used to be wrapped into the legacy 3-arity raw-shell shape;
|
|
172
|
+
# it is now wrapped into a SEALED 2-arity handler so default
|
|
173
|
+
# saves land on the SynthesizedCommandContext deny-list path
|
|
174
|
+
# below. The body's `output` channel is preserved — bound per
|
|
175
|
+
# invocation to the sealed context's output view, inside the
|
|
176
|
+
# wrapper's own scope (lambda-local, no shared binding
|
|
177
|
+
# mutation, clean_eval_binding isolation unchanged). `shell`
|
|
178
|
+
# is deliberately NOT bound: bare bodies referencing it now
|
|
179
|
+
# raise NameError — the intended hardening; the documented
|
|
180
|
+
# migration is authoring an explicit 3-arity proc (the
|
|
181
|
+
# operator-trusted escape hatch in the dispatch else branch).
|
|
182
|
+
overlay_eval("->(args, ctx) {\noutput = ctx.respond_to?(:out) ? ctx.out : nil\n#{code}\n}")
|
|
232
183
|
end
|
|
233
184
|
|
|
234
185
|
registered = shell.register_command(cmd_name, description: "#{description} [#{scope}]", custom: true) do |args, ctx|
|
|
@@ -257,6 +208,18 @@ module Rubik
|
|
|
257
208
|
end
|
|
258
209
|
proc_handler.call(args, sealed_ctx)
|
|
259
210
|
else
|
|
211
|
+
# Trust model (issue #398): this branch serves EXPLICITLY authored
|
|
212
|
+
# 3-arity handlers only — bare-body saves are sealed by the default
|
|
213
|
+
# wrap above. Such a handler receives the RAW interactive
|
|
214
|
+
# CommandContext/shell (`guard`, `config`, `sandbox_runner`,
|
|
215
|
+
# `buffer_manager`, etc. reachable), so the SealedCoeffects
|
|
216
|
+
# deny-list does NOT apply here. That is the operator-trusted
|
|
217
|
+
# bootstrap contract, anchored to the same trust vocabulary as the
|
|
218
|
+
# eval sites above: guard-checked at synthesis (or bootstrap
|
|
219
|
+
# acceptance) AND disk hash verified against the manifest
|
|
220
|
+
# (verify_integrity), with RUBIK_TRUST_OVERLAYS=1 as the blanket
|
|
221
|
+
# operator override. Authoring a 3-arity proc remains the
|
|
222
|
+
# deliberate escape hatch — it must stay an explicit act.
|
|
260
223
|
out_ctx = if ctx
|
|
261
224
|
ctx
|
|
262
225
|
elsif shell.respond_to?(:command_context)
|
|
@@ -274,55 +237,11 @@ module Rubik
|
|
|
274
237
|
end
|
|
275
238
|
|
|
276
239
|
def save_tool(name, code, scope: :project, description: "Custom native tool", params_schema: nil, risk: :execute)
|
|
277
|
-
|
|
278
|
-
target_dir = scope.to_sym == :global ? global_tools_dir : project_tools_dir
|
|
279
|
-
FileUtils.mkdir_p(target_dir)
|
|
280
|
-
|
|
281
|
-
filename = "#{clean_name}.rb"
|
|
282
|
-
file_path = File.join(target_dir, filename)
|
|
283
|
-
ensure_contained_path!(file_path, target_dir)
|
|
284
|
-
|
|
285
|
-
schema_json = params_schema ? JSON.dump(params_schema) : "{}"
|
|
286
|
-
header = <<~RUBY
|
|
287
|
-
# frozen_string_literal: true
|
|
288
|
-
#{TOOL_SENTINEL} #{clean_name}
|
|
289
|
-
# Scope: #{scope}
|
|
290
|
-
# Risk: #{risk}
|
|
291
|
-
# Description: #{description}
|
|
292
|
-
# Schema: #{schema_json}
|
|
293
|
-
|
|
294
|
-
RUBY
|
|
295
|
-
|
|
296
|
-
File.write(file_path, "#{header}#{code.strip}\n")
|
|
297
|
-
record_hash(target_dir, filename, scope)
|
|
298
|
-
{ ok: true, path: file_path, name: clean_name, scope: scope }
|
|
299
|
-
rescue StandardError => e
|
|
300
|
-
# Audit 1: surfaced to the caller as ok:false and logged under RUBIK_DEBUG.
|
|
301
|
-
warn "[WARN] [OVERLAY] Failed to save tool '#{name}' to #{file_path}: #{e.class}: #{e.message}" if ENV["RUBIK_DEBUG"]
|
|
302
|
-
{ ok: false, error: e.message }
|
|
240
|
+
save(TOOL_KIND, name, code, scope: scope, description: description, params_schema: params_schema, risk: risk)
|
|
303
241
|
end
|
|
304
242
|
|
|
305
|
-
# Delete a synthesized tool from disk and manifest. Symmetric to
|
|
306
|
-
# save_tool: same clean_name normalization, scope→dir selection and
|
|
307
|
-
# { ok:, error: } shape. Fail-closed on a corrupt manifest — never wipe
|
|
308
|
-
# it with {} (mirror the record_hash guard on the save path).
|
|
309
243
|
def delete_tool(name, scope: :project)
|
|
310
|
-
|
|
311
|
-
target_dir = scope.to_sym == :global ? global_tools_dir : project_tools_dir
|
|
312
|
-
filename = "#{clean_name}.rb"
|
|
313
|
-
file_path = File.join(target_dir, filename)
|
|
314
|
-
ensure_contained_path!(file_path, target_dir)
|
|
315
|
-
|
|
316
|
-
raise "cannot delete '#{filename}': .manifest.json in #{target_dir} is corrupt; delete it and re-record overlays" if manifest_state(target_dir) == :corrupt
|
|
317
|
-
|
|
318
|
-
manifest = load_manifest(target_dir)
|
|
319
|
-
manifest.delete(filename)
|
|
320
|
-
save_manifest(target_dir, manifest)
|
|
321
|
-
|
|
322
|
-
FileUtils.rm_f(file_path)
|
|
323
|
-
{ ok: true, name: clean_name, scope: scope, path: file_path }
|
|
324
|
-
rescue StandardError => e
|
|
325
|
-
{ ok: false, error: e.message }
|
|
244
|
+
delete(TOOL_KIND, name, scope: scope)
|
|
326
245
|
end
|
|
327
246
|
|
|
328
247
|
def list_tools
|
|
@@ -331,31 +250,17 @@ module Rubik
|
|
|
331
250
|
end
|
|
332
251
|
|
|
333
252
|
def load_tools(tool_registry, runtime_manager: nil, runtime_context: nil)
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
next
|
|
253
|
+
load_entries(TOOL_KIND, list_tools) do |name, code, scope|
|
|
254
|
+
tool_inst = eval_tool(name, code, scope, runtime_manager: runtime_manager, runtime_context: runtime_context)
|
|
255
|
+
next false unless tool_inst
|
|
337
256
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
ensure_contained_path!(file_path, target_dir)
|
|
341
|
-
|
|
342
|
-
code = File.read(t_meta[:path])
|
|
343
|
-
tool_inst = eval_tool(t_meta[:name], code, t_meta[:scope], runtime_manager: runtime_manager, runtime_context: runtime_context)
|
|
344
|
-
if tool_inst
|
|
345
|
-
tool_registry.register(tool_inst, origin: :overlay)
|
|
346
|
-
loaded << t_meta[:name]
|
|
347
|
-
else
|
|
348
|
-
warn "[OVERLAY] Custom tool '#{t_meta[:name]}' from #{t_meta[:path]} did not evaluate to a usable tool."
|
|
349
|
-
end
|
|
350
|
-
rescue StandardError => e
|
|
351
|
-
warn "[OVERLAY] Failed to load custom tool '#{t_meta[:name]}' from #{t_meta[:path]}: #{e.class}: #{e.message}"
|
|
257
|
+
tool_registry.register(tool_inst, origin: :overlay)
|
|
258
|
+
true
|
|
352
259
|
end
|
|
353
|
-
loaded
|
|
354
260
|
end
|
|
355
261
|
|
|
356
262
|
def eval_tool(name, code, scope, runtime_manager: nil, runtime_context: nil)
|
|
357
|
-
|
|
358
|
-
description = desc_match ? desc_match[1].strip : "Custom #{scope} tool"
|
|
263
|
+
description = overlay_description(code, scope, "tool")
|
|
359
264
|
|
|
360
265
|
schema_match = code.match(/# Schema:\s*(.*)/)
|
|
361
266
|
schema = if schema_match
|
|
@@ -380,7 +285,7 @@ module Rubik
|
|
|
380
285
|
# dropping the tool at load.
|
|
381
286
|
risk = :execute unless [:read, :write, :execute].include?(risk)
|
|
382
287
|
|
|
383
|
-
evaluated =
|
|
288
|
+
evaluated = overlay_eval(code)
|
|
384
289
|
if evaluated.is_a?(Class) && evaluated < Rubik::Tools::BaseTool
|
|
385
290
|
evaluated.new
|
|
386
291
|
elsif evaluated.respond_to?(:call)
|
|
@@ -410,6 +315,111 @@ module Rubik
|
|
|
410
315
|
|
|
411
316
|
private
|
|
412
317
|
|
|
318
|
+
# Single home of the fail-closed corrupt-manifest contract (issue #407,
|
|
319
|
+
# single-homed in ManifestStore by issue #426): record_hash and the
|
|
320
|
+
# delete skeleton refuse through this one raise. `verb` reproduces the
|
|
321
|
+
# per-site messages ("cannot record '...'" vs "cannot delete '...'");
|
|
322
|
+
# the rest of the template is verbatim shared.
|
|
323
|
+
def refuse_corrupt_manifest!(dir, verb, filename)
|
|
324
|
+
manifest_store.refuse_corrupt_manifest!(dir, verb, filename)
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
# Shared save skeleton (issue #407): mkdir → containment check → header →
|
|
328
|
+
# File.write → record_hash → { ok:, ... } → rescue { ok:false, error: }.
|
|
329
|
+
# Header bytes are load-bearing twice over (eval_* parse them back with
|
|
330
|
+
# regexes; the integrity manifest hashes the whole file), so emission must
|
|
331
|
+
# stay byte-identical (pinned by the header-bytes tests).
|
|
332
|
+
def save(kind, name, code, scope:, description:, params_schema: nil, risk: :execute)
|
|
333
|
+
clean_name = kind.normalize_name(name)
|
|
334
|
+
target_dir = kind_dir(kind, scope)
|
|
335
|
+
FileUtils.mkdir_p(target_dir)
|
|
336
|
+
|
|
337
|
+
filename = kind.filename_for(clean_name)
|
|
338
|
+
file_path = File.join(target_dir, filename)
|
|
339
|
+
ensure_contained_path!(file_path, target_dir)
|
|
340
|
+
|
|
341
|
+
header = kind.header_lines(name: clean_name, scope: scope, description: description,
|
|
342
|
+
risk: risk, schema: params_schema ? JSON.dump(params_schema) : "{}")
|
|
343
|
+
File.write(file_path, "#{header.join("\n")}\n#{code.strip}\n")
|
|
344
|
+
record_hash(target_dir, filename, scope)
|
|
345
|
+
{ ok: true, path: file_path, name: clean_name, scope: scope }
|
|
346
|
+
rescue StandardError => e
|
|
347
|
+
# Audit 1: surfaced to the caller as ok:false; tool kind logs under RUBIK_DEBUG.
|
|
348
|
+
warn "[WARN] [OVERLAY] Failed to save #{kind.label} '#{name}' to #{file_path}: #{e.class}: #{e.message}" if kind.debug_warn && ENV["RUBIK_DEBUG"]
|
|
349
|
+
{ ok: false, error: e.message }
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# Shared delete skeleton (issue #407). Symmetric to save for both kinds:
|
|
353
|
+
# same clean_name normalization, scope→dir selection and { ok:, error: }
|
|
354
|
+
# shape. Fail-closed on a corrupt manifest — never wipe it with {}
|
|
355
|
+
# (single-homed in refuse_corrupt_manifest! above).
|
|
356
|
+
def delete(kind, name, scope:)
|
|
357
|
+
clean_name = kind.normalize_name(name)
|
|
358
|
+
target_dir = kind_dir(kind, scope)
|
|
359
|
+
filename = kind.filename_for(clean_name)
|
|
360
|
+
file_path = File.join(target_dir, filename)
|
|
361
|
+
ensure_contained_path!(file_path, target_dir)
|
|
362
|
+
|
|
363
|
+
refuse_corrupt_manifest!(target_dir, :delete, filename)
|
|
364
|
+
|
|
365
|
+
manifest = load_manifest(target_dir)
|
|
366
|
+
manifest.delete(filename)
|
|
367
|
+
save_manifest(target_dir, manifest)
|
|
368
|
+
|
|
369
|
+
FileUtils.rm_f(file_path)
|
|
370
|
+
{ ok: true, name: clean_name, scope: scope, path: file_path }
|
|
371
|
+
rescue StandardError => e
|
|
372
|
+
{ ok: false, error: e.message }
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# Shared load skeleton (issue #407): iterate the verified listing → skip
|
|
376
|
+
# rejected entries → containment check → read → evaluate via the block →
|
|
377
|
+
# collect → rescue+warn. The block decides what "registration" means per
|
|
378
|
+
# kind; a falsy result collects nothing and, for the tool kind
|
|
379
|
+
# (warn_on_unusable), warns "did not evaluate to a usable tool". Warn
|
|
380
|
+
# wording stays byte-identical to the pre-refactor messages.
|
|
381
|
+
def load_entries(kind, entries)
|
|
382
|
+
loaded = []
|
|
383
|
+
entries.each do |meta|
|
|
384
|
+
next if meta[:integrity] == :rejected
|
|
385
|
+
|
|
386
|
+
name = meta[:name]
|
|
387
|
+
file_path = meta[:path]
|
|
388
|
+
target_dir = kind_dir(kind, meta[:scope])
|
|
389
|
+
|
|
390
|
+
ensure_contained_path!(file_path, target_dir)
|
|
391
|
+
code = File.read(file_path)
|
|
392
|
+
if yield(name, code, meta[:scope])
|
|
393
|
+
loaded << name
|
|
394
|
+
elsif kind.warn_on_unusable
|
|
395
|
+
warn "[OVERLAY] Custom #{kind.label} '#{name}' from #{file_path} did not evaluate to a usable tool."
|
|
396
|
+
end
|
|
397
|
+
rescue StandardError => e
|
|
398
|
+
warn "[OVERLAY] Failed to load custom #{kind.label} '#{name}' from #{file_path}: #{e.class}: #{e.message}"
|
|
399
|
+
end
|
|
400
|
+
loaded
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# Shared header-field readback (issue #407): both eval paths recover the
|
|
404
|
+
# Description line with the same regex and default text.
|
|
405
|
+
def overlay_description(code, scope, kind_label)
|
|
406
|
+
desc_match = code.match(/# Description:\s*(.*)/)
|
|
407
|
+
desc_match ? desc_match[1].strip : "Custom #{scope} #{kind_label}"
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# Single home of the overlay eval sink and its rubocop disable: the code
|
|
411
|
+
# is guard-checked at synthesis (or bootstrap acceptance) and its disk
|
|
412
|
+
# hash verified against the manifest before this runs.
|
|
413
|
+
def overlay_eval(code)
|
|
414
|
+
eval(code, clean_eval_binding, __FILE__, __LINE__) # rubocop:disable Security/Eval -- overlay çekirdeği; sentez anında (veya bootstrap kabulünde) guard kontrolünden geçti VE disk hash'i manifestle doğrulandı (verify_integrity)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
# Kind-scoped directory selection: anything but :global lands in the
|
|
418
|
+
# project tree (pre-refactor behavior for both kinds).
|
|
419
|
+
def kind_dir(kind, scope)
|
|
420
|
+
scope.to_sym == :global ? public_send(kind.global_dir) : public_send(kind.project_dir)
|
|
421
|
+
end
|
|
422
|
+
|
|
413
423
|
def ensure_valid_overlay_dir!(dir)
|
|
414
424
|
allowed = [
|
|
415
425
|
[project_commands_dir, @root_dir],
|
|
@@ -485,21 +495,16 @@ module Rubik
|
|
|
485
495
|
|
|
486
496
|
# Ortak kaçış kapısı predicate'i: iki güvenlik kapısının (verify_integrity,
|
|
487
497
|
# verified_dir_entries) kopya ENV kontrolüyle sürünerek ayrışmasını engeller.
|
|
498
|
+
# Tek seam ManifestStore'da yaşar (issue #426); plugin tarafı da aynı kapıdan geçer.
|
|
488
499
|
def trust_mode?
|
|
489
|
-
|
|
500
|
+
manifest_store.trust_mode?
|
|
490
501
|
end
|
|
491
502
|
|
|
492
|
-
# Üç durumlu manifest
|
|
493
|
-
#
|
|
494
|
-
#
|
|
503
|
+
# Üç durumlu manifest yoklaması ve bozuk-manifest reddi ManifestStore'da
|
|
504
|
+
# tek home'da (issue #426): kayıt yolu (record_hash) ve delete iskeleti
|
|
505
|
+
# bozuk manifestte {} davranışına değil, fail-closed redde bağlı.
|
|
495
506
|
def manifest_state(dir)
|
|
496
|
-
|
|
497
|
-
return :missing unless File.exist?(path)
|
|
498
|
-
|
|
499
|
-
parsed = JSON.parse(File.read(path))
|
|
500
|
-
parsed.is_a?(Hash) ? :valid : :corrupt
|
|
501
|
-
rescue JSON::ParserError
|
|
502
|
-
:corrupt
|
|
507
|
+
manifest_store.manifest_state(dir)
|
|
503
508
|
end
|
|
504
509
|
|
|
505
510
|
# Dizin taraması başına bootstrap uygunluğu: manifest dosyası hiç yokken sentinel
|
|
@@ -565,5 +570,51 @@ module Rubik
|
|
|
565
570
|
Object.new.instance_eval { binding }
|
|
566
571
|
end
|
|
567
572
|
end
|
|
573
|
+
|
|
574
|
+
# ── Private Kind adapter (issue #407) ──────────────────────────────────────
|
|
575
|
+
# Defined at the bottom so the frozen instances below can reference the
|
|
576
|
+
# sentinel constants declared in the class body above. Per-kind asymmetries
|
|
577
|
+
# it parameterizes: name normalization (name_regex/strip_bang/colon_ok —
|
|
578
|
+
# commands keep ':' in the name but substitute it in the filename), scope→
|
|
579
|
+
# dir selection (global_dir/project_dir; anything but :global is project),
|
|
580
|
+
# byte-exact header emission (sentinel/bang/metadata_lines), and the warn
|
|
581
|
+
# asymmetries (debug_warn on save failure, warn_on_unusable on load).
|
|
582
|
+
class OverlayStorage
|
|
583
|
+
Kind = Struct.new(:label, :sentinel, :bang, :name_regex, :strip_bang, :colon_ok, :debug_warn,
|
|
584
|
+
:warn_on_unusable, :metadata_lines, :global_dir, :project_dir, keyword_init: true) do
|
|
585
|
+
def normalize_name(raw)
|
|
586
|
+
name = raw.to_s.downcase
|
|
587
|
+
name = name.delete_prefix("!") if strip_bang
|
|
588
|
+
name.gsub(name_regex, "_")
|
|
589
|
+
end
|
|
590
|
+
|
|
591
|
+
def filename_for(clean_name)
|
|
592
|
+
"#{colon_ok ? clean_name.gsub(':', '_') : clean_name}.rb"
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
# Byte-exact header template: frozen-string line, sentinel line, Scope,
|
|
596
|
+
# tool-only Risk/Schema lines around Description, trailing blank line.
|
|
597
|
+
# eval_* parse these lines back and the integrity manifest hashes the
|
|
598
|
+
# whole file, so every byte is contract (pinned by the header-bytes tests).
|
|
599
|
+
def header_lines(name:, scope:, description:, risk:, schema:)
|
|
600
|
+
lines = ["# frozen_string_literal: true",
|
|
601
|
+
"#{sentinel} #{bang}#{name}",
|
|
602
|
+
"# Scope: #{scope}"]
|
|
603
|
+
lines << "# Risk: #{risk}" if metadata_lines
|
|
604
|
+
lines << "# Description: #{description}"
|
|
605
|
+
lines << "# Schema: #{schema}" if metadata_lines
|
|
606
|
+
lines << ""
|
|
607
|
+
lines
|
|
608
|
+
end
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
COMMAND_KIND = Kind.new(label: "command", sentinel: COMMAND_SENTINEL, bang: "!", name_regex: /[^a-z0-9_:]/,
|
|
612
|
+
strip_bang: true, colon_ok: true, debug_warn: false, warn_on_unusable: false,
|
|
613
|
+
metadata_lines: false, global_dir: :global_commands_dir, project_dir: :project_commands_dir).freeze
|
|
614
|
+
|
|
615
|
+
TOOL_KIND = Kind.new(label: "tool", sentinel: TOOL_SENTINEL, bang: "", name_regex: /[^a-z0-9_]/,
|
|
616
|
+
strip_bang: false, colon_ok: false, debug_warn: true, warn_on_unusable: true,
|
|
617
|
+
metadata_lines: true, global_dir: :global_tools_dir, project_dir: :project_tools_dir).freeze
|
|
618
|
+
end
|
|
568
619
|
end
|
|
569
620
|
end
|
|
@@ -4,6 +4,7 @@ require "open3"
|
|
|
4
4
|
require "monitor"
|
|
5
5
|
require "time"
|
|
6
6
|
require_relative "../platform"
|
|
7
|
+
require_relative "../security/guard" unless defined?(Rubik::Security::Guard)
|
|
7
8
|
|
|
8
9
|
module Rubik
|
|
9
10
|
module Runtime
|
|
@@ -98,7 +99,11 @@ module Rubik
|
|
|
98
99
|
@tasks = {}
|
|
99
100
|
@task_counter = 0
|
|
100
101
|
@monitor = Monitor.new
|
|
101
|
-
|
|
102
|
+
# Fail-closed fallback (house pattern): a bare construction must never
|
|
103
|
+
# leave the popen3 sink unguarded — a fresh Guard still blocks
|
|
104
|
+
# dangerous/unclassifiable commands, it only lacks the operator
|
|
105
|
+
# allowlist of the injected session guard.
|
|
106
|
+
@guard = guard || Rubik::Security::Guard.new
|
|
102
107
|
@context = context
|
|
103
108
|
@root_dir = File.expand_path(root_dir)
|
|
104
109
|
@max_buffer_lines = max_buffer_lines
|
|
@@ -116,7 +121,7 @@ module Rubik
|
|
|
116
121
|
cmd_str = command.to_s.strip
|
|
117
122
|
raise ArgumentError, "Command cannot be empty" if cmd_str.empty?
|
|
118
123
|
|
|
119
|
-
@guard
|
|
124
|
+
@guard.verify_command_safety!(cmd_str)
|
|
120
125
|
|
|
121
126
|
# Each task carries its OWN child context, never the shared runtime
|
|
122
127
|
# root: Kernel::Init#reap_dead_units! disposes a dead unit's context
|
data/lib/rubik/runtime.rb
CHANGED
|
@@ -7,6 +7,7 @@ require_relative "runtime/fiber"
|
|
|
7
7
|
require_relative "runtime/manager"
|
|
8
8
|
require_relative "runtime/service_definition"
|
|
9
9
|
require_relative "runtime/service_registration"
|
|
10
|
+
require_relative "runtime/manifest_store"
|
|
10
11
|
require_relative "runtime/overlay_storage"
|
|
11
12
|
require_relative "runtime/discovery"
|
|
12
13
|
require_relative "runtime/diagnostic_engine"
|