rubik 0.9.1 → 0.11.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 (138) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +203 -1
  4. data/lib/rubik/adapter/base.rb +2 -1
  5. data/lib/rubik/adapter/process.rb +28 -67
  6. data/lib/rubik/ast/extractor.rb +2 -1
  7. data/lib/rubik/ast/treesitter_extractor.rb +2 -1
  8. data/lib/rubik/benchmark/vakanuvis_adoption.rb +12 -4
  9. data/lib/rubik/buffer/apply_contract.rb +3 -3
  10. data/lib/rubik/buffer/apply_transaction.rb +165 -0
  11. data/lib/rubik/buffer/manager.rb +26 -120
  12. data/lib/rubik/config/deep_ops.rb +4 -1
  13. data/lib/rubik/config/defaults.rb +6 -3
  14. data/lib/rubik/config/schema.rb +8 -0
  15. data/lib/rubik/config/secret_policy.rb +198 -0
  16. data/lib/rubik/config/store.rb +2 -1
  17. data/lib/rubik/config.rb +13 -2
  18. data/lib/rubik/context/ignore_rules.rb +193 -0
  19. data/lib/rubik/context/scanner.rb +46 -161
  20. data/lib/rubik/diagnostics/collector.rb +0 -9
  21. data/lib/rubik/errors.rb +15 -0
  22. data/lib/rubik/event_bus.rb +2 -1
  23. data/lib/rubik/kernel/dispatcher.rb +12 -7
  24. data/lib/rubik/kernel/unit.rb +4 -1
  25. data/lib/rubik/llm/client.rb +6 -200
  26. data/lib/rubik/llm/connection_pool.rb +104 -0
  27. data/lib/rubik/llm/registry.rb +13 -9
  28. data/lib/rubik/llm/router.rb +2 -5
  29. data/lib/rubik/llm/sse_parser.rb +110 -0
  30. data/lib/rubik/log.rb +21 -0
  31. data/lib/rubik/lsp/circuit_breaker.rb +98 -0
  32. data/lib/rubik/lsp/client.rb +77 -273
  33. data/lib/rubik/lsp/client_provisioner.rb +49 -0
  34. data/lib/rubik/lsp/manager.rb +67 -96
  35. data/lib/rubik/lsp/transport.rb +365 -0
  36. data/lib/rubik/mcp/boot_lifecycle.rb +6 -2
  37. data/lib/rubik/mcp/boot_run.rb +190 -0
  38. data/lib/rubik/mcp/manager.rb +55 -166
  39. data/lib/rubik/mcp/transport/stdio.rb +4 -1
  40. data/lib/rubik/orchestrator/auto_apply.rb +8 -6
  41. data/lib/rubik/orchestrator/continuation_manager.rb +3 -22
  42. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +12 -1
  43. data/lib/rubik/orchestrator/goal_ledger/schema_vocabulary.rb +38 -0
  44. data/lib/rubik/orchestrator/goal_ledger/xml_render.rb +6 -1
  45. data/lib/rubik/orchestrator/goal_ledger.rb +1 -0
  46. data/lib/rubik/orchestrator/goal_session.rb +53 -39
  47. data/lib/rubik/orchestrator/loop.rb +28 -47
  48. data/lib/rubik/orchestrator/lsp_feedback.rb +1 -1
  49. data/lib/rubik/orchestrator/maestro_gate.rb +5 -7
  50. data/lib/rubik/orchestrator/provider_recovery.rb +4 -2
  51. data/lib/rubik/orchestrator/provider_resolver.rb +4 -2
  52. data/lib/rubik/orchestrator/resume_window.rb +96 -0
  53. data/lib/rubik/orchestrator/shell_deps.rb +16 -0
  54. data/lib/rubik/orchestrator/step_executor.rb +2 -1
  55. data/lib/rubik/orchestrator/stream_manager.rb +2 -1
  56. data/lib/rubik/orchestrator/subagent/deferred_spec.rb +4 -1
  57. data/lib/rubik/orchestrator/subagent/instance.rb +7 -12
  58. data/lib/rubik/orchestrator/subagent/lifecycle.rb +3 -1
  59. data/lib/rubik/orchestrator/subagent/notices.rb +2 -2
  60. data/lib/rubik/orchestrator/subagent/questioner.rb +3 -1
  61. data/lib/rubik/orchestrator/subagent/reaper.rb +3 -5
  62. data/lib/rubik/orchestrator/subagent/resilience.rb +4 -2
  63. data/lib/rubik/orchestrator/subagent/result_archive.rb +0 -4
  64. data/lib/rubik/orchestrator/subagent/run_cycle.rb +2 -7
  65. data/lib/rubik/orchestrator/subagent/spawn_transaction.rb +11 -2
  66. data/lib/rubik/orchestrator/subagent/step_runner.rb +10 -2
  67. data/lib/rubik/orchestrator/subagent/transcript_formatter.rb +2 -1
  68. data/lib/rubik/orchestrator/subagent/type_definition.rb +0 -4
  69. data/lib/rubik/orchestrator/tool_timeout.rb +121 -1
  70. data/lib/rubik/platform/io_drain.rb +5 -2
  71. data/lib/rubik/platform/process.rb +144 -54
  72. data/lib/rubik/platform.rb +2 -1
  73. data/lib/rubik/plugin/manager.rb +2 -1
  74. data/lib/rubik/plugin/storage.rb +8 -8
  75. data/lib/rubik/repl/commands/agent_context.rb +8 -7
  76. data/lib/rubik/repl/commands/agent_execution.rb +13 -32
  77. data/lib/rubik/repl/commands/agent_models.rb +4 -3
  78. data/lib/rubik/repl/commands/agent_providers.rb +43 -2
  79. data/lib/rubik/repl/commands/base.rb +47 -2
  80. data/lib/rubik/repl/commands/buffer.rb +5 -4
  81. data/lib/rubik/repl/commands/lsp.rb +2 -1
  82. data/lib/rubik/repl/commands/pruner.rb +2 -1
  83. data/lib/rubik/repl/commands/secrets.rb +151 -0
  84. data/lib/rubik/repl/commands/workspace.rb +60 -150
  85. data/lib/rubik/repl/commands/workspace_approval.rb +209 -0
  86. data/lib/rubik/repl/config_wiring.rb +8 -6
  87. data/lib/rubik/repl/hooks.rb +2 -1
  88. data/lib/rubik/repl/input_execution.rb +162 -79
  89. data/lib/rubik/repl/input_loop.rb +2 -1
  90. data/lib/rubik/repl/lifecycle.rb +60 -210
  91. data/lib/rubik/repl/notice_router.rb +319 -0
  92. data/lib/rubik/repl/paste_support.rb +3 -1
  93. data/lib/rubik/repl/presentation.rb +5 -3
  94. data/lib/rubik/repl/pruner/gate.rb +3 -1
  95. data/lib/rubik/repl/pruner/harness.rb +2 -1
  96. data/lib/rubik/repl/shell.rb +4 -3
  97. data/lib/rubik/repl/tee_io.rb +2 -1
  98. data/lib/rubik/repl/turn_recorder.rb +6 -5
  99. data/lib/rubik/runtime/ast_service.rb +2 -1
  100. data/lib/rubik/runtime/discovery.rb +3 -2
  101. data/lib/rubik/runtime/loop_service.rb +4 -3
  102. data/lib/rubik/runtime/manifest_store.rb +9 -1
  103. data/lib/rubik/runtime/overlay_guard.rb +127 -0
  104. data/lib/rubik/runtime/overlay_storage.rb +109 -163
  105. data/lib/rubik/runtime/task_manager.rb +64 -25
  106. data/lib/rubik/sandbox/driver/docker.rb +5 -1
  107. data/lib/rubik/sandbox/runner.rb +21 -19
  108. data/lib/rubik/security/audit_log.rb +2 -1
  109. data/lib/rubik/security/command_gate.rb +126 -243
  110. data/lib/rubik/security/guard.rb +7 -3
  111. data/lib/rubik/security/secret_store.rb +492 -0
  112. data/lib/rubik/security/segment_report.rb +161 -0
  113. data/lib/rubik/security/synthesis_gate.rb +8 -11
  114. data/lib/rubik/session/manager.rb +7 -1
  115. data/lib/rubik/session/recorder.rb +2 -1
  116. data/lib/rubik/skills/manager.rb +4 -4
  117. data/lib/rubik/storage/search.rb +49 -0
  118. data/lib/rubik/storage/store.rb +13 -20
  119. data/lib/rubik/storage/transcript_indexer.rb +3 -2
  120. data/lib/rubik/storage/vakanuvis/harness.rb +2 -3
  121. data/lib/rubik/storage/vakanuvis.rb +2 -1
  122. data/lib/rubik/tools/base.rb +22 -0
  123. data/lib/rubik/tools/builtins/files.rb +3 -15
  124. data/lib/rubik/tools/builtins/goal_ledger.rb +12 -1
  125. data/lib/rubik/tools/builtins/grep.rb +1 -6
  126. data/lib/rubik/tools/builtins/lifecycle.rb +6 -5
  127. data/lib/rubik/tools/builtins/synthesis.rb +3 -3
  128. data/lib/rubik/tools/lifecycle_rollbacks.rb +14 -1
  129. data/lib/rubik/tools/preflight_runner.rb +2 -1
  130. data/lib/rubik/tools/truncator.rb +48 -36
  131. data/lib/rubik/tools/uri_resolver.rb +3 -2
  132. data/lib/rubik/ui/banner.rb +4 -3
  133. data/lib/rubik/ui/capability.rb +1 -0
  134. data/lib/rubik/ui/spinner.rb +3 -1
  135. data/lib/rubik/ui/statusline.rb +6 -5
  136. data/lib/rubik/version.rb +1 -1
  137. data/lib/rubik.rb +4 -5
  138. metadata +20 -1
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module Context
5
+ # Frozen value object owning the entire "is this path in scope?" dialect
6
+ # of the context index: default ignore globs, the repo's own .gitignore,
7
+ # user excludes/includes, whole-subtree pruning, and the reserved runtime
8
+ # roots. Built once per scan root via .build, it answers three predicates
9
+ # — prune_dir?, ignore_file? / listed_file_ignored?, include_file? — so
10
+ # both listing strategies (Scanner#walk_dir and the git ls-files fast
11
+ # path) stay symmetric by consuming the same rules instead of each
12
+ # re-implementing the filter sequence.
13
+ class IgnoreRules
14
+ DEFAULT_IGNORES = [
15
+ ".rubik/**",
16
+ ".git/**",
17
+ # GitNexus parse cache: thousands of binary index files; indexing
18
+ # them is pure cost and their non-UTF-8 bytes are a grep hazard.
19
+ ".gitnexus/**",
20
+ ".ua/**",
21
+ ".trash*/**",
22
+ "**/.trash*/**",
23
+ ".turbo/**",
24
+ ".sst/**",
25
+ ".next/**",
26
+ ".nuxt/**",
27
+ "node_modules/**",
28
+ "vendor/**",
29
+ "tmp/**",
30
+ "dist/**",
31
+ "build/**",
32
+ ".cache/**",
33
+ "coverage/**",
34
+ # Multi-GB local model blobs; never project sources.
35
+ ".ollama/**",
36
+ # Agent-harness state: transcripts, sessions, caches.
37
+ ".zcode/**"
38
+ ].freeze
39
+
40
+ # Subtrees whose ignore pattern is "<path>/**" (optionally with a
41
+ # "**/" prefix) are pruned whole when the stem's components occur
42
+ # contiguously in a directory's path. NOTE: this is intentionally
43
+ # STRONGER than the legacy per-file filter — Ruby's fnmatch treats a
44
+ # trailing `**` as a single segment, so patterns like "node_modules/**"
45
+ # historically rejected only first-level entries while deeper files
46
+ # leaked into the index. The walker applies the pattern's plain meaning
47
+ # (whole subtree ignored), matching real gitignore semantics.
48
+ PRUNABLE_PATTERN = %r{\A(?:\*\*/)?(.+)/\*\*\z}
49
+ # Also hard-rejected inside ignore_file?; pruned before descending.
50
+ # ROOT-anchored only (comps.first) to match the rg-side `!/X/**`
51
+ # globs — a nested `src/AppData` stays indexed.
52
+ HARD_PRUNE_ROOTS = [".git", ".rubik", ".ua", ".turbo", ".sst", ".next", ".nuxt", "AppData"].freeze
53
+
54
+ attr_reader :custom_includes
55
+
56
+ # `dir` is the scan root whose top-level .gitignore contributes
57
+ # patterns; excludes/includes come from the Scanner's accessors.
58
+ def self.build(dir:, custom_excludes: [], custom_includes: [])
59
+ new(DEFAULT_IGNORES + parse_gitignore(dir) + custom_excludes, custom_includes)
60
+ end
61
+
62
+ # Reads one root's .gitignore: comments and blanks dropped, directory
63
+ # entries ("tmp/") widened to whole-subtree globs ("tmp/**").
64
+ def self.parse_gitignore(dir)
65
+ gitignore_path = File.join(dir, ".gitignore")
66
+ return [] unless File.exist?(gitignore_path)
67
+
68
+ patterns = File.readlines(gitignore_path, chomp: true).map(&:strip).reject do |line|
69
+ line.empty? || line.start_with?("#")
70
+ end
71
+ patterns.map do |pattern|
72
+ pattern.end_with?("/") ? "#{pattern}**" : pattern
73
+ end
74
+ rescue Errno::ENOENT, Errno::EACCES
75
+ []
76
+ end
77
+
78
+ # `patterns` is the merged ignore table; `custom_includes` the user's
79
+ # allow-list. Compilation happens eagerly here so the predicates are
80
+ # allocation-free lookups afterwards.
81
+ def initialize(patterns, custom_includes)
82
+ @patterns = patterns.freeze
83
+ @custom_includes = (custom_includes || []).freeze
84
+ # "<dir>/**" patterns pre-split into needle component arrays.
85
+ # Matching a directory then costs plain array compares — no
86
+ # per-ancestor regex evaluation and no re-splitting of the pattern's
87
+ # stem for every file visited.
88
+ @prunable_needles = @patterns.filter_map do |pattern|
89
+ next unless (m = PRUNABLE_PATTERN.match(pattern))
90
+
91
+ m[1].split("/")
92
+ end.freeze
93
+ @file_regexes = compile(@patterns).freeze
94
+ # The git fast path skips prunable subtree patterns with literal
95
+ # stems: its ancestor loop already pruned every directory segment
96
+ # through prune_dir?, so re-testing those globs per file is pure
97
+ # redundancy. Exotic stems (wildcards) stay in the per-file set.
98
+ @listed_file_regexes = compile(listed_file_patterns).freeze
99
+ freeze
100
+ end
101
+
102
+ # Should descent into a directory stop? `rel_comps` is the directory's
103
+ # path split on "/". Applies the hard-prune roots (root-anchored) and
104
+ # the whole-subtree globs (contiguous component match). The one
105
+ # predicate behind the walker's early pruning and the fast path's
106
+ # ancestor check.
107
+ def prune_dir?(rel_comps)
108
+ return true if HARD_PRUNE_ROOTS.include?(rel_comps.first)
109
+
110
+ @prunable_needles.any? { |needle| contiguous_components?(rel_comps, needle) }
111
+ end
112
+
113
+ # Walker-dialect file filter: reserved roots plus every ignore pattern
114
+ # (prunable ones included, with the documented legacy fnmatch drift).
115
+ def ignore_file?(rel)
116
+ reserved_root?(rel) || @file_regexes.any? { |r| pattern_match?(r, rel) }
117
+ end
118
+
119
+ # Fast-path-dialect file filter: same reserved roots, but prunable
120
+ # literal-stem subtree patterns are withheld because prune_dir? already
121
+ # covered their ancestors (see initialize).
122
+ def listed_file_ignored?(rel)
123
+ reserved_root?(rel) || @listed_file_regexes.any? { |r| pattern_match?(r, rel) }
124
+ end
125
+
126
+ # Include dialect: no include patterns means "everything is included";
127
+ # otherwise a file must match at least one pattern.
128
+ def include_file?(rel)
129
+ return true if @custom_includes.empty?
130
+
131
+ @custom_includes.any? { |pattern| pattern_match?(pattern, rel) }
132
+ end
133
+
134
+ private
135
+
136
+ def listed_file_patterns
137
+ @patterns.reject do |pat|
138
+ m = PRUNABLE_PATTERN.match(pat)
139
+ m && !m[1].include?("*") && !m[1].include?("?") && !m[1].include?("[")
140
+ end
141
+ end
142
+
143
+ def contiguous_components?(haystack, needle)
144
+ return true if haystack == needle
145
+ return false if haystack.size < needle.size
146
+
147
+ (0..(haystack.size - needle.size)).any? do |offset|
148
+ haystack[offset, needle.size] == needle
149
+ end
150
+ end
151
+
152
+ # Runtime-owned roots are never indexed, whatever the ignore tables
153
+ # say — the one predicate behind both listing strategies (the git
154
+ # fast path and the generic walk).
155
+ def reserved_root?(rel)
156
+ rel == ".rubik" || rel.start_with?(".rubik/") || rel == ".git" || rel.start_with?(".git/")
157
+ end
158
+
159
+ # Translates a glob into a Regexp only where the mapping is exact: no
160
+ # character classes, no leading-slash anchoring, and `**` only ever a
161
+ # whole path segment (leading "**/", mid "/**/", trailing "/**").
162
+ # Anything else keeps the pattern string and pattern_match? falls back
163
+ # to File.fnmatch?, whose segment rules the Regexp form cannot express
164
+ # (fnmatch "a**b" never crosses "/" and "**/x" also matches bare "x").
165
+ def compile(ignore_patterns)
166
+ ignore_patterns.map do |pat|
167
+ simple_glob_regex(pat) || pat
168
+ end
169
+ end
170
+
171
+ def simple_glob_regex(pat)
172
+ return nil if pat.empty? || pat.start_with?("/") || pat.include?("[")
173
+ return nil unless pat.split("/").all? { |seg| !seg.include?("*") || seg == "**" || !seg.include?("**") }
174
+
175
+ src = Regexp.escape(pat)
176
+ src = src.sub(%r{\A\\\*\\\*/}, "") # leading "**/": the (?:.*/)? wrapper matches zero dirs too
177
+ src = src.gsub("/\\*\\*/", "/(?:.*/)?") # mid "/**/": zero or more directories
178
+ src = src.gsub("\\*\\*", ".*") # trailing "/**" or lone "**"
179
+ src = src.gsub("\\*", "[^/]*").gsub("\\?", "[^/]")
180
+ Regexp.new("\\A(?:.*/)?#{src}\\z")
181
+ end
182
+
183
+ def pattern_match?(pattern_or_regex, path)
184
+ if pattern_or_regex.is_a?(Regexp)
185
+ pattern_or_regex.match?(path)
186
+ else
187
+ File.fnmatch?(pattern_or_regex, path, File::FNM_PATHNAME | File::FNM_DOTMATCH) ||
188
+ File.fnmatch?("**/#{pattern_or_regex}", path, File::FNM_PATHNAME | File::FNM_DOTMATCH)
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
@@ -4,10 +4,15 @@ require "digest"
4
4
  require "pathname"
5
5
  require_relative "../platform"
6
6
  require_relative "git_lister"
7
+ require_relative "ignore_rules"
7
8
 
8
9
  module Rubik
9
10
  module Context
10
11
  class Scanner
12
+ # Index language tags. Deliberately diverges from
13
+ # LSP::Manager::EXTENSION_MAP (which maps to LSP server ids, e.g.
14
+ # .cpp -> "c"); the two tables serve different consumers — keep
15
+ # them separate.
11
16
  EXTENSION_MAP = {
12
17
  ".rb" => "ruby",
13
18
  ".py" => "python",
@@ -46,45 +51,14 @@ module Rubik
46
51
  "package.json" => "javascript"
47
52
  }.freeze
48
53
 
49
- DEFAULT_IGNORES = [
50
- ".rubik/**",
51
- ".git/**",
52
- # GitNexus parse cache: thousands of binary index files; indexing
53
- # them is pure cost and their non-UTF-8 bytes are a grep hazard.
54
- ".gitnexus/**",
55
- ".ua/**",
56
- ".trash*/**",
57
- "**/.trash*/**",
58
- ".turbo/**",
59
- ".sst/**",
60
- ".next/**",
61
- ".nuxt/**",
62
- "node_modules/**",
63
- "vendor/**",
64
- "tmp/**",
65
- "dist/**",
66
- "build/**",
67
- ".cache/**",
68
- "coverage/**",
69
- # Multi-GB local model blobs; never project sources.
70
- ".ollama/**",
71
- # Agent-harness state: transcripts, sessions, caches.
72
- ".zcode/**"
73
- ].freeze
74
-
75
- # Subtrees whose ignore pattern is "<path>/**" (optionally with a
76
- # "**/" prefix) are pruned whole when the stem's components occur
77
- # contiguously in a directory's path. NOTE: this is intentionally
78
- # STRONGER than the legacy per-file filter — Ruby's fnmatch treats a
79
- # trailing `**` as a single segment, so patterns like "node_modules/**"
80
- # historically rejected only first-level entries while deeper files
81
- # leaked into the index. The walker applies the pattern's plain meaning
82
- # (whole subtree ignored), matching real gitignore semantics.
83
- PRUNABLE_PATTERN = %r{\A(?:\*\*/)?(.+)/\*\*\z}
84
- # Also hard-rejected inside `ignored?`; pruned before descending.
85
- # ROOT-anchored only (comps.first) to match the rg-side `!/X/**`
86
- # globs — a nested `src/AppData` stays indexed.
87
- HARD_PRUNE_ROOTS = [".git", ".rubik", ".ua", ".turbo", ".sst", ".next", ".nuxt", "AppData"].freeze
54
+ # The "is this path in scope?" dialect (default ignores, prunable
55
+ # subtree patterns, hard-prune roots, glob->regex translation, the
56
+ # include dialect and the reserved-root predicate) lives in
57
+ # IgnoreRules; the Scanner is only a consumer of its predicates.
58
+ # Legacy aliases: external consumers (native_search's rg flags, tests)
59
+ # still read the tables off the Scanner.
60
+ DEFAULT_IGNORES = IgnoreRules::DEFAULT_IGNORES
61
+ HARD_PRUNE_ROOTS = IgnoreRules::HARD_PRUNE_ROOTS
88
62
 
89
63
  # Outcome of the most recent scan. A partial index served as ground
90
64
  # truth is worse than an honest flag: every rescue boundary that drops
@@ -121,15 +95,13 @@ module Rubik
121
95
  files = {}
122
96
 
123
97
  # 1. Scan primary root
124
- primary_ignores = DEFAULT_IGNORES + parse_gitignore(@root_dir) + @custom_excludes
125
- files.merge!(scan_root(@root_dir, ".", primary_ignores, previous))
98
+ files.merge!(scan_root(@root_dir, ".", previous))
126
99
 
127
100
  # 2. Scan additional roots
128
101
  @additional_roots.each do |extra_root|
129
102
  next unless File.directory?(extra_root)
130
103
 
131
- extra_ignores = DEFAULT_IGNORES + parse_gitignore(extra_root) + @custom_excludes
132
- files.merge!(scan_root(extra_root, relative_prefix(extra_root), extra_ignores, previous))
104
+ files.merge!(scan_root(extra_root, relative_prefix(extra_root), previous))
133
105
  end
134
106
 
135
107
  files
@@ -224,7 +196,7 @@ module Rubik
224
196
  (stat.mtime.tv_sec * 1_000_000) + stat.mtime.tv_usec
225
197
  end
226
198
 
227
- def walk_dir(base_dir, rel_prefix, key_prefix, ignore_patterns, previous, needles)
199
+ def walk_dir(base_dir, rel_prefix, key_prefix, rules, previous)
228
200
  current = rel_prefix.empty? ? base_dir : "#{base_dir}/#{rel_prefix}"
229
201
  out = {}
230
202
 
@@ -250,9 +222,9 @@ module Rubik
250
222
  end
251
223
 
252
224
  if lst.directory? && !lst.symlink?
253
- next if subtree_ignored?(rel, needles)
225
+ next if prune_dir?(rel, rules)
254
226
 
255
- out.merge!(walk_dir(base_dir, rel, key_prefix, ignore_patterns, previous, needles))
227
+ out.merge!(walk_dir(base_dir, rel, key_prefix, rules, previous))
256
228
  else
257
229
  # Symlinked FILES are still listed (File.stat follows the link),
258
230
  # matching the old Dir.glob behavior; only directory descent is
@@ -264,8 +236,8 @@ module Rubik
264
236
  nil
265
237
  end
266
238
  next unless st&.file?
267
- next if ignored?(rel, ignore_patterns)
268
- next unless included?(rel, @custom_includes)
239
+ next if rules.ignore_file?(rel)
240
+ next unless rules.include_file?(rel)
269
241
 
270
242
  rel_key = key_prefix == "." ? rel : "#{key_prefix}/#{rel}"
271
243
  out[rel_key] = entry_for(rel_key, "#{base_dir}/#{rel}", previous, stat: st)
@@ -279,32 +251,17 @@ module Rubik
279
251
  out
280
252
  end
281
253
 
282
- def subtree_ignored?(dir_rel, needles)
283
- comps = dir_rel.split("/")
284
- return true if HARD_PRUNE_ROOTS.include?(comps.first)
285
-
286
- needles.any? { |needle| contiguous_components?(comps, needle) }
254
+ # The one directory-prune entry point the walker uses; split lives on
255
+ # the Scanner side so IgnoreRules#prune_dir? takes plain components.
256
+ def prune_dir?(rel, rules)
257
+ rules.prune_dir?(rel.split("/"))
287
258
  end
288
259
 
289
- # "<dir>/**" ignore patterns (see PRUNABLE_PATTERN) pre-split into
290
- # needle component arrays, once per scan root. Matching a directory
291
- # then costs plain array compares — no per-ancestor regex evaluation
292
- # and no re-splitting of the pattern's stem for every file visited.
293
- def prunable_needles(ignore_patterns)
294
- ignore_patterns.filter_map do |pattern|
295
- next unless (m = PRUNABLE_PATTERN.match(pattern))
296
-
297
- m[1].split("/")
298
- end
299
- end
300
-
301
- def contiguous_components?(haystack, needle)
302
- return true if haystack == needle
303
- return false if haystack.size < needle.size
304
-
305
- (0..(haystack.size - needle.size)).any? do |offset|
306
- haystack[offset, needle.size] == needle
307
- end
260
+ # One IgnoreRules value object per scan root: the root's .gitignore is
261
+ # merged into the default/exclude tables and everything is compiled
262
+ # once, up front.
263
+ def rules_for(dir)
264
+ IgnoreRules.build(dir: dir, custom_excludes: @custom_excludes, custom_includes: @custom_includes)
308
265
  end
309
266
 
310
267
  # Key prefix for files found under an additional root, mirroring the
@@ -319,17 +276,17 @@ module Rubik
319
276
  # One recursive directory walk with EARLY SUBTREE PRUNING: instead of
320
277
  # enumerating ignored trees (.git/, node_modules/, vendor/ – thousands
321
278
  # of entries on real projects) only to reject each file afterwards,
322
- # descent into a matching subtree never happens. Pruning applies the
323
- # ignore pattern's plain whole-subtree meaning (see PRUNABLE_PATTERN)
324
- # and `ignored?` still filters every file that IS visited. `prefix` is
325
- # the key namespace for this root ("." for the primary project root).
326
- def scan_root(base_dir, prefix, ignore_patterns, previous)
327
- needles = prunable_needles(ignore_patterns)
279
+ # descent into a matching subtree never happens (rules.prune_dir?).
280
+ # `ignored?` semantics survive as rules.ignore_file? filtering every
281
+ # file that IS visited. `prefix` is the key namespace for this root
282
+ # ("." for the primary project root).
283
+ def scan_root(base_dir, prefix, previous)
284
+ rules = rules_for(base_dir)
328
285
  if git_listing? && @custom_includes.empty? && (listed = GitLister.files(base_dir))
329
- return index_listed(base_dir, prefix, listed, ignore_patterns, previous, needles)
286
+ return index_listed(base_dir, prefix, listed, rules, previous)
330
287
  end
331
288
 
332
- walk_dir(base_dir, "", prefix, ignore_patterns, previous, needles)
289
+ walk_dir(base_dir, "", prefix, rules, previous)
333
290
  rescue StandardError => e
334
291
  Rubik::Log.warn(:context, "root scan failed for #{base_dir} (#{e.class}: #{e.message}); root omitted from index")
335
292
  mark_scan_aborted!("root scan failed for #{base_dir}: #{e.class}: #{e.message}")
@@ -341,26 +298,19 @@ module Rubik
341
298
  # the repo's own gitignore: DEFAULT_IGNORES subtrees may be missing
342
299
  # from it, and tracked files ignore gitignore entirely), ordered
343
300
  # identically to walk_dir's depth-first-per-directory sort, and
344
- # ancestor-pruned with the same whole-subtree semantics as the walker.
301
+ # ancestor-pruned with the same whole-subtree semantics as the walker
302
+ # (rules.prune_dir?, memoized per ancestor directory below).
345
303
  # Residual drift the OTHER way: gitignore features the walker's crude
346
304
  # fnmatch translation lacks (negations, anchored patterns) can make the
347
305
  # fast path list fewer files.
348
- def index_listed(base_dir, key_prefix, rel_paths, ignore_patterns, previous, needles)
306
+ def index_listed(base_dir, key_prefix, rel_paths, rules, previous)
349
307
  out = {}
350
308
  # Verdict per ancestor directory, computed on first sight: walker
351
- # order keeps siblings adjacent, so each distinct directory's ignore
309
+ # order keeps siblings adjacent, so each distinct directory's prune
352
310
  # status is evaluated once per scan instead of once per file per
353
- # depth. subtree_ignored? is a pure function of (dir, needles), so
354
- # the memo is exact.
355
- dir_verdicts = Hash.new { |h, dir| h[dir] = subtree_ignored?(dir, needles) }
356
-
357
- # PERF: filter ignore_patterns to exclude prunable subtree patterns that
358
- # dir_verdicts already checked on ancestor directory segments.
359
- file_patterns = ignore_patterns.reject do |pat|
360
- m = PRUNABLE_PATTERN.match(pat)
361
- m && !m[1].include?("*") && !m[1].include?("?") && !m[1].include?("[")
362
- end
363
- file_rules = compile_ignore_regexes(file_patterns)
311
+ # depth. rules.prune_dir? is a pure function of the directory's
312
+ # components, so the memo is exact.
313
+ dir_verdicts = Hash.new { |h, dir| h[dir] = rules.prune_dir?(dir.split("/")) }
364
314
 
365
315
  order_like_walker(rel_paths).each do |rel|
366
316
  # PERF: zero-allocation ancestor check using string slicing instead of Array#split and Array#join
@@ -374,9 +324,8 @@ module Rubik
374
324
  idx += 1
375
325
  end
376
326
  next if ignored_dir
377
- next if rel == ".rubik" || rel.start_with?(".rubik/") || rel == ".git" || rel.start_with?(".git/")
378
- next if file_rules.any? { |r| pattern_match?(r, rel) }
379
- next unless included?(rel, @custom_includes)
327
+ next if rules.listed_file_ignored?(rel)
328
+ next unless rules.include_file?(rel)
380
329
 
381
330
  full = "#{base_dir}/#{rel}"
382
331
  st = begin
@@ -413,70 +362,6 @@ module Rubik
413
362
  def order_like_walker(paths)
414
363
  paths.sort_by! { |p| p.tr("/", "\x00") }
415
364
  end
416
-
417
- def parse_gitignore(dir = @root_dir)
418
- gitignore_path = File.join(dir, ".gitignore")
419
- return [] unless File.exist?(gitignore_path)
420
-
421
- patterns = File.readlines(gitignore_path, chomp: true).map(&:strip).reject do |line|
422
- line.empty? || line.start_with?("#")
423
- end
424
- patterns.map do |pattern|
425
- pattern.end_with?("/") ? "#{pattern}**" : pattern
426
- end
427
- rescue Errno::ENOENT, Errno::EACCES
428
- []
429
- end
430
-
431
- def ignored?(path, ignore_patterns)
432
- return true if path == ".rubik" || path.start_with?(".rubik/")
433
- return true if path == ".git" || path.start_with?(".git/")
434
-
435
- # PERF: precompile simple globs to memoized Regexp objects to avoid
436
- # repeated File.fnmatch? overhead across thousands of files; exotic
437
- # globs keep the fnmatch semantics untouched.
438
- compile_ignore_regexes(ignore_patterns).any? { |r| pattern_match?(r, path) }
439
- end
440
-
441
- # Translates a glob into a Regexp only where the mapping is exact: no
442
- # character classes, no leading-slash anchoring, and `**` only ever a
443
- # whole path segment (leading "**/", mid "/**/", trailing "/**").
444
- # Anything else returns nil and pattern_match? falls back to
445
- # File.fnmatch?, whose segment rules the Regexp form cannot express
446
- # (fnmatch "a**b" never crosses "/" and "**/x" also matches bare "x").
447
- def compile_ignore_regexes(ignore_patterns)
448
- @compiled_ignore_regexes ||= {}
449
- @compiled_ignore_regexes[ignore_patterns] ||= ignore_patterns.map do |pat|
450
- simple_glob_regex(pat) || pat
451
- end
452
- end
453
-
454
- def simple_glob_regex(pat)
455
- return nil if pat.empty? || pat.start_with?("/") || pat.include?("[")
456
- return nil unless pat.split("/").all? { |seg| !seg.include?("*") || seg == "**" || !seg.include?("**") }
457
-
458
- src = Regexp.escape(pat)
459
- src = src.sub(%r{\A\\\*\\\*/}, "") # leading "**/": the (?:.*/)? wrapper matches zero dirs too
460
- src = src.gsub("/\\*\\*/", "/(?:.*/)?") # mid "/**/": zero or more directories
461
- src = src.gsub("\\*\\*", ".*") # trailing "/**" or lone "**"
462
- src = src.gsub("\\*", "[^/]*").gsub("\\?", "[^/]")
463
- Regexp.new("\\A(?:.*/)?#{src}\\z")
464
- end
465
-
466
- def pattern_match?(pattern_or_regex, path)
467
- if pattern_or_regex.is_a?(Regexp)
468
- pattern_or_regex.match?(path)
469
- else
470
- File.fnmatch?(pattern_or_regex, path, File::FNM_PATHNAME | File::FNM_DOTMATCH) ||
471
- File.fnmatch?("**/#{pattern_or_regex}", path, File::FNM_PATHNAME | File::FNM_DOTMATCH)
472
- end
473
- end
474
-
475
- def included?(path, patterns)
476
- return true if patterns.nil? || patterns.empty?
477
-
478
- patterns.any? { |pattern| pattern_match?(pattern, path) }
479
- end
480
365
  end
481
366
  end
482
367
  end
@@ -37,15 +37,6 @@ module Rubik
37
37
  @mutex.synchronize { @entries << entry }
38
38
  end
39
39
 
40
- # Adds multiple Entry objects
41
- def add_entries(entries)
42
- return unless entries.is_a?(Array)
43
-
44
- @mutex.synchronize do
45
- entries.each { |e| @entries << e if e.is_a?(Entry) }
46
- end
47
- end
48
-
49
40
  # Parses CLI compiler/linter output and adds entries
50
41
  def add_parsed(output, lang: :unknown, source: :compiler) # rubocop:disable Lint/UnusedMethodArgument -- cagiran arayüzüyle uyum için source kwarg'i korunuyor
51
42
  parsed = Parser.parse(output, lang)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ # Root of the unified error taxonomy (issue #448): every module-level
5
+ # error class hangs off Rubik::Error so callers can rescue one class.
6
+ # Standalone-requireable — submodule files that define errors require
7
+ # this file instead of the full lib/rubik.rb entry point.
8
+ class Error < StandardError; end
9
+
10
+ # Shared state-machine violation vocabulary (issue #448): the per-module
11
+ # StateError classes (subagent Lifecycle, DeferredSpec, MCP BootLifecycle,
12
+ # kernel Unit) subclass this, so rescuing Rubik::StateError catches any
13
+ # guarded-transition tripwire without knowing the module.
14
+ class StateError < Error; end
15
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "logger"
4
+ require_relative "log"
4
5
  require_relative "kernel/dispatcher"
5
6
 
6
7
  module Rubik
@@ -10,7 +11,7 @@ module Rubik
10
11
  class EventBus
11
12
  attr_reader :dispatcher
12
13
 
13
- def initialize(dispatcher: nil, logger: Logger.new($stderr))
14
+ def initialize(dispatcher: nil, logger: Rubik::Log.forwarder(:event_bus))
14
15
  @logger = logger
15
16
  @dispatcher = dispatcher || Rubik::Kernel::Dispatcher.new(logger: @logger)
16
17
  end
@@ -29,8 +29,6 @@ module Rubik
29
29
  end
30
30
 
31
31
  def post(event_name, payload = {}, subsystem: :kernel, level: :info, facility_id: nil, **kwargs)
32
- return false if stopped?
33
-
34
32
  eff_payload = if kwargs.empty?
35
33
  payload || {}
36
34
  elsif payload.is_a?(Hash)
@@ -39,12 +37,19 @@ module Rubik
39
37
  kwargs
40
38
  end
41
39
 
42
- @queue.push(
43
- Event.new(
44
- name: event_name.to_sym, payload: eff_payload,
45
- subsystem: subsystem.to_sym, level: level, facility_id: facility_id
40
+ # The stopped check and the queue push must be atomic: an unlocked
41
+ # check-then-push let an event land after the worker consumed
42
+ # STOP_MARKER during shutdown, stranding it forever.
43
+ @lock.synchronize do
44
+ return false if @stopped
45
+
46
+ @queue.push(
47
+ Event.new(
48
+ name: event_name.to_sym, payload: eff_payload,
49
+ subsystem: subsystem.to_sym, level: level, facility_id: facility_id
50
+ )
46
51
  )
47
- )
52
+ end
48
53
 
49
54
  q_size = @queue.size
50
55
  if q_size >= DEFAULT_WARN_QUEUE_DEPTH && (q_size % 500).zero?
@@ -1,13 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "monitor"
4
+ require_relative "../errors"
4
5
  require_relative "../events"
5
6
 
6
7
  module Rubik
7
8
  module Kernel
8
9
  # Kernel execution unit representing an asynchronous computation (Plan 104).
9
10
  class Unit
10
- class StateError < StandardError; end
11
+ # Unified taxonomy (issue #448): subclasses Rubik::StateError so a
12
+ # caller can rescue any guarded-transition tripwire with one class.
13
+ class StateError < Rubik::StateError; end
11
14
 
12
15
  VALID_TYPES = [:subagent, :process, :task, :service].freeze
13
16
  VALID_STATES = [:pending, :active, :paused_error, :waiting_budget, :budget_exhausted, :completed, :failed, :reaped].freeze