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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +108 -43
  4. data/README.md +5 -1
  5. data/lib/rubik/adapter/base.rb +19 -0
  6. data/lib/rubik/adapter/generic.rb +0 -14
  7. data/lib/rubik/adapter/node.rb +2 -0
  8. data/lib/rubik/ast/treesitter/zig_walker.rb +15 -2
  9. data/lib/rubik/cli/workspace_trust.rb +16 -0
  10. data/lib/rubik/config/defaults.rb +23 -0
  11. data/lib/rubik/config/store.rb +14 -0
  12. data/lib/rubik/kernel/dispatcher.rb +4 -4
  13. data/lib/rubik/log.rb +2 -1
  14. data/lib/rubik/lsp/client.rb +83 -180
  15. data/lib/rubik/lsp/document_store.rb +209 -0
  16. data/lib/rubik/mcp/client.rb +4 -3
  17. data/lib/rubik/mcp/manager.rb +6 -2
  18. data/lib/rubik/mcp/transport/stdio.rb +51 -9
  19. data/lib/rubik/orchestrator/goal_session.rb +505 -0
  20. data/lib/rubik/orchestrator/subagent/instance.rb +4 -0
  21. data/lib/rubik/orchestrator/subagent/journal.rb +2 -1
  22. data/lib/rubik/orchestrator/subagent/questioner.rb +21 -6
  23. data/lib/rubik/orchestrator/subagent/step_runner.rb +1 -1
  24. data/lib/rubik/orchestrator/subagent/teardown.rb +4 -0
  25. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  26. data/lib/rubik/orchestrator.rb +1 -0
  27. data/lib/rubik/platform.rb +15 -0
  28. data/lib/rubik/plugin/storage.rb +20 -29
  29. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  30. data/lib/rubik/repl/commands/agent_execution.rb +7 -470
  31. data/lib/rubik/repl/commands/buffer.rb +1 -1
  32. data/lib/rubik/repl/commands/lsp.rb +1 -1
  33. data/lib/rubik/repl/input_loop.rb +1 -1
  34. data/lib/rubik/repl/lifecycle.rb +28 -0
  35. data/lib/rubik/runtime/manifest_store.rb +116 -0
  36. data/lib/rubik/runtime/overlay_storage.rb +247 -196
  37. data/lib/rubik/runtime/task_manager.rb +7 -2
  38. data/lib/rubik/runtime.rb +1 -0
  39. data/lib/rubik/security/allowlist.rb +167 -0
  40. data/lib/rubik/security/audit_log.rb +72 -0
  41. data/lib/rubik/security/classifier/classifier.rb +8 -0
  42. data/lib/rubik/security/command_gate.rb +567 -0
  43. data/lib/rubik/security/guard.rb +88 -1075
  44. data/lib/rubik/security/path_policy.rb +117 -0
  45. data/lib/rubik/security/synthesis_gate.rb +300 -0
  46. data/lib/rubik/security.rb +5 -0
  47. data/lib/rubik/storage/ranking.rb +113 -0
  48. data/lib/rubik/storage/store.rb +2 -92
  49. data/lib/rubik/storage/transcript_indexer.rb +2 -3
  50. data/lib/rubik/tools/builtins/grep.rb +2 -3
  51. data/lib/rubik/tools/builtins/lifecycle.rb +90 -87
  52. data/lib/rubik/tools/builtins/synthesis.rb +3 -0
  53. data/lib/rubik/tools/lifecycle_rollbacks.rb +87 -0
  54. data/lib/rubik/tools/native_search.rb +2 -1
  55. data/lib/rubik/tools/truncator.rb +25 -0
  56. data/lib/rubik/version.rb +1 -1
  57. metadata +11 -1
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "shellwords"
4
+ require_relative "../platform"
5
+ require_relative "classifier"
6
+
7
+ module Rubik
8
+ module Security
9
+ # Operator-controlled command allowlist (Plan: komut-allowlist-güvenlik-kapısı),
10
+ # split out of Guard (#403) as the allowlist leaf: state (@command_allowlist),
11
+ # the AST gate, the raw Shellwords gate, and every token-prefix matcher.
12
+ # Entries are token-prefixes: "node" allows every node invocation (broad);
13
+ # "node .gitnexus/run.cjs" allows only that script (narrow — preferred).
14
+ # Applied AFTER classifier verdict, BEFORE the raise, and the catastrophic
15
+ # fallback regex still runs afterwards (defense-in-depth is never skipped).
16
+ class Allowlist
17
+ # Shell metacharacters that make a raw (AST-less) token-prefix allowlist
18
+ # match dishonest: they can join, redirect, substitute, or line-split
19
+ # the string, so "the first tokens match" would not mean "the whole
20
+ # simple command matches". The classifier-crash rescue only accepts a
21
+ # single simple command; anything else fails closed.
22
+ RAW_ALLOWLIST_UNSAFE_PATTERN = /[;&|<>`$(){}\[\]\\\n\r]/
23
+
24
+ def initialize(entries = [])
25
+ @command_allowlist = normalize_allowlist(entries)
26
+ end
27
+
28
+ def command_allowlist
29
+ @command_allowlist.dup
30
+ end
31
+
32
+ def allowed_commands=(entries)
33
+ @command_allowlist = normalize_allowlist(entries)
34
+ end
35
+ alias set_allowed_commands allowed_commands=
36
+
37
+ def command_allowlisted?(ast, cmd_str: nil)
38
+ _ = cmd_str
39
+ return false if @command_allowlist.empty?
40
+ return false unless ast.respond_to?(:commands)
41
+
42
+ ast_commands = ast.commands.to_a
43
+ return false if ast_commands.empty?
44
+
45
+ # EVERY dangerous segment (recursively incl. pipeline inner commands)
46
+ # must match an entry — a single allowlisted prefix cannot rescue
47
+ # `allowlisted-cmd && rm -rf /` chains.
48
+ ast_commands.all? { |c| command_matches_allowlist?(c) }
49
+ end
50
+
51
+ # Token-prefix allowlist check for an input whose AST does not exist
52
+ # (classifier crashed). Only a single simple command qualifies: with
53
+ # shell metacharacters present the string may hold several segments
54
+ # and the gate's "every segment must match" contract cannot be
55
+ # honored — fail closed instead of over-promising the prefix match.
56
+ # Quoting is handled by Shellwords; an unparseable string (unbalanced
57
+ # quote) fails closed.
58
+ def command_allowlisted_raw?(cmd_str)
59
+ return false if @command_allowlist.empty?
60
+ return false if cmd_str.match?(RAW_ALLOWLIST_UNSAFE_PATTERN)
61
+
62
+ tokens = nil
63
+ begin
64
+ tokens = Shellwords.split(cmd_str)
65
+ rescue ArgumentError
66
+ return false
67
+ end
68
+ return false if tokens.empty?
69
+ return false if Rubik::Security::Classifier::Engine.builtin_dangerous?(tokens[0])
70
+
71
+ allowlist_match_argv?(tokens)
72
+ end
73
+
74
+ private
75
+
76
+ def normalize_allowlist(entries)
77
+ Array(entries).filter_map do |e|
78
+ tokens = e.to_s.strip.split(/\s+/).reject(&:empty?)
79
+ tokens unless tokens.empty?
80
+ end
81
+ end
82
+
83
+ def canonical_binary_path(bin)
84
+ return nil if bin.nil? || bin.to_s.empty?
85
+
86
+ str = bin.to_s
87
+ if str.include?("/") || str.include?("\\") || File.exist?(str)
88
+ Rubik::Platform.normalize_path(File.expand_path(str))
89
+ else
90
+ resolved = Rubik::Platform.which(str)
91
+ resolved ? Rubik::Platform.normalize_path(resolved) : nil
92
+ end
93
+ end
94
+
95
+ def binary_allowlist_match?(arg, token)
96
+ return true if arg == token
97
+ # Windows exe-alias fast path (#103): on a windows host "node.exe"
98
+ # IS the allowlisted "node" — a lexical alias, no filesystem probe.
99
+ # The canonical-path probe below resolves through Platform.which +
100
+ # File.file?, a second host seam the windows? stub cannot cover (a
101
+ # linux host has no node.exe on disk, so the probe fails and the
102
+ # allowlist would dead-close). On a posix host the alias is FALSE:
103
+ # node.exe is a different binary than node there.
104
+ return true if windows_executable_alias?(arg, token)
105
+
106
+ c_arg = canonical_binary_path(arg)
107
+ c_token = canonical_binary_path(token)
108
+
109
+ return true if c_arg && c_token && Rubik::Platform.path_equal?(c_arg, c_token)
110
+
111
+ false
112
+ end
113
+
114
+ # True when arg carries an executable extension from PATHEXT and its
115
+ # stem equals the token (case-insensitive — windows path semantics).
116
+ # Bare stems only: a path-form arg ("C:/x/node.exe") keeps going
117
+ # through the canonical-path probe, which resolves both sides.
118
+ def windows_executable_alias?(arg, token)
119
+ return false unless Rubik::Platform.windows?
120
+
121
+ ext = ::File.extname(arg)
122
+ return false if ext.empty? || !Rubik::Platform.pathext.include?(ext.downcase)
123
+
124
+ arg.delete_suffix(ext).casecmp?(token)
125
+ end
126
+
127
+ def command_matches_allowlist?(result)
128
+ # Missing argv (e.g. empty invocation) can never be allowlisted.
129
+ argv = result.respond_to?(:argv) ? result.argv : nil
130
+ return false if argv.nil? || argv.empty?
131
+
132
+ # Dangerous shell builtins (`source`, `.`) are never rescuable by
133
+ # allowlist: they execute arbitrary file content by design.
134
+ return false if Rubik::Security::Classifier::Engine.builtin_dangerous?(argv[0])
135
+
136
+ # W6: If this command wraps inner commands, EVERY inner command must also match allowlist
137
+ if result.respond_to?(:inner_commands) && result.inner_commands && !result.inner_commands.empty? && !result.inner_commands.all? do |inner|
138
+ command_matches_allowlist?(inner)
139
+ end
140
+ return false
141
+ end
142
+
143
+ allowlist_match_argv?(argv)
144
+ end
145
+
146
+ # The single token-prefix matching home shared by the AST path
147
+ # (#command_matches_allowlist?) and the raw Shellwords path
148
+ # (#command_allowlisted_raw?) — two callers, one contract: an entry
149
+ # matches when its tokens are a prefix of argv (position 0 through the
150
+ # binary canonical-path probe, the rest by equality).
151
+ def allowlist_match_argv?(argv)
152
+ @command_allowlist.any? do |entry|
153
+ next false if entry.length > argv.length
154
+
155
+ entry.each_with_index.all? do |token, i|
156
+ arg = argv[i]
157
+ if i.zero?
158
+ binary_allowlist_match?(arg, token)
159
+ else
160
+ arg == token
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "time"
5
+ require "fileutils"
6
+ require_relative "../platform"
7
+ require_relative "redactor"
8
+
9
+ module Rubik
10
+ module Security
11
+ # The shared audit leaf of the Guard split (#403): owns the audit
12
+ # destination (@audit_log_path), secret masking, and the JSONL append.
13
+ # Every other security module (PathPolicy, CommandGate, SynthesisGate)
14
+ # receives an instance of this class and routes its log_audit calls
15
+ # through it — the audit trail has exactly one home.
16
+ class AuditLog
17
+ MASK_PATTERNS = Rubik::Security::Redactor::PATTERNS
18
+
19
+ attr_reader :audit_log_path
20
+
21
+ # Default audit location, resolved at call time (not a load-time
22
+ # constant) so Platform.home_dir stubs in tests and runtime HOME
23
+ # changes are honored. Single source shared by the constructor
24
+ # default and Guard.for_config.
25
+ def self.default_audit_log_path
26
+ File.join(Rubik::Platform.home_dir, ".rubik", "audit.log")
27
+ end
28
+
29
+ def initialize(audit_log_path = AuditLog.default_audit_log_path)
30
+ @audit_log_path = audit_log_path
31
+ end
32
+
33
+ # Live-reconfigurable audit destination (ConfigWiring applies config
34
+ # changes in place — zero restart). Same blank-means-disabled semantics
35
+ # as for_config; non-string values pass through and surface loudly in
36
+ # log_audit's unconditional warn.
37
+ def audit_log_path=(val)
38
+ @audit_log_path = val.is_a?(String) && val.strip.empty? ? nil : val
39
+ end
40
+
41
+ def mask_text(text)
42
+ Rubik::Security::Redactor.redact(text)
43
+ end
44
+
45
+ def log_audit(action, details = {})
46
+ return if @audit_log_path.nil? || @audit_log_path.to_s.strip.empty?
47
+
48
+ # Nil-safety: a nil details hash (or nil value) must never crash the
49
+ # command path — audit failure is already swallowed below.
50
+ masked_details = (details || {}).transform_values { |v| v.nil? ? "" : mask_text(v.to_s) }
51
+
52
+ entry = {
53
+ ts: Time.now.iso8601,
54
+ action: action,
55
+ details: masked_details
56
+ }
57
+
58
+ dir = File.dirname(@audit_log_path)
59
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
60
+
61
+ File.open(@audit_log_path, "a") do |f|
62
+ f.puts(JSON.dump(entry))
63
+ end
64
+ rescue StandardError => e
65
+ # Unconditional: an audit trail failing silently is indistinguishable
66
+ # from an operator's intentional disable — the one warn in the guard
67
+ # that must not hide behind RUBIK_DEBUG.
68
+ warn "[WARN] Failed to write audit log #{@audit_log_path}: #{e.message}"
69
+ end
70
+ end
71
+ end
72
+ end
@@ -37,6 +37,14 @@ module Rubik
37
37
  new(database: database, shell: shell).classify(expression)
38
38
  end
39
39
 
40
+ # Public interface predicate over the Matcher's builtin vocabulary.
41
+ # External gates (e.g. Guard's allowlist) depend on this instead of
42
+ # reaching into Matcher::BUILTIN_DANGEROUS_CMDS — a non-interface
43
+ # constant of another module.
44
+ def self.builtin_dangerous?(binary)
45
+ Matcher::BUILTIN_DANGEROUS_CMDS.include?(binary.to_s)
46
+ end
47
+
40
48
  def initialize(database: nil, shell: nil)
41
49
  @database = database || self.class.default_database
42
50
  @shell = shell