rubik 0.6.0 → 0.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd6a93a2277eebd80ce20dbf33ceba1ba7354d6540a6906740b4d58274736847
4
- data.tar.gz: 2f46b92bbf23cbcea71767c433ffd45dd55b821e6bb8f1b18cb8c2e7310c5be0
3
+ metadata.gz: e6db8019561c617d4b3006fc7caea11b4f780feb2a55bc200fc8ca4825040720
4
+ data.tar.gz: 0acc95f9ae8c299a2d253833c541954d92b1372c6af7c3c1af08ad9a08bb5c53
5
5
  SHA512:
6
- metadata.gz: 258d3631c684bfdd719a3b341197dd02da8fc64789c30645b8df0cd4814fc7546392c371737582adab36ff4d46052828fe8d0fed536a1efb592b2c5dcca34571
7
- data.tar.gz: d9a517f0f4b7026d666d3ac0e2f880805ce40d2341613e5cc75ed72b5b37df3eabf60f07cee9cdf2d8a491b3758b0871edc43b62e501b423bde817b814b1edaf
6
+ metadata.gz: 6d2e351b5907c057bb29fe1b20d84dfef967afb8571116a147ca7e013149e6c5fe00bd5aa8cb2cccd08e4e7679184e02f126e8b2fab2f11cc5ee33f28d837648
7
+ data.tar.gz: b638835c922ad012b30e438f00460704d7f9aaf0d324d9de90267f1d10b6e0dd81b931b4d2491d312825dc0ed2539cd77880bfbddd83765e7e34fdbe27b14dfe
data/CHANGELOG.md CHANGED
@@ -6,6 +6,53 @@ commits, 2026-08-23 → 2026-10-09, see `git log`).
6
6
 
7
7
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
8
8
 
9
+ ## [0.7.0] - 2026-11-03
10
+
11
+ A turn-stall-and-honesty minor: `list_files` and `grep_search` never
12
+ trigger a synchronous full scan again, partial context scans disclose
13
+ themselves instead of serving truncated truth as clean, huge session
14
+ roots get boot-time guidance instead of a silent minutes-long warm
15
+ walk, and a single stalling tool can no longer pin the whole turn —
16
+ the first dispatcher-level call budget lands as an off-by-default knob.
17
+
18
+ ### Added
19
+
20
+ - **Bounded tool execution (`tools.call_timeout`, `tools.call_timeout_overrides`)
21
+ (#355):** an off-by-default dispatcher budget applied at the tool-dispatch seam
22
+ on both orchestrator planes; a fired budget returns an honest error result (the
23
+ call is abandoned, not killed) and the turn continues, with the timeout visible
24
+ via `!dmesg -s tools`. Precedence: an explicit env disable
25
+ (`RUBIK_TOOLS_CALL_TIMEOUT=0`) wins over everything, per-tool overrides refine
26
+ the resolved global, and the env budget beats the config file. Knob unset means
27
+ byte-identical inline execution.
28
+ - **Huge-root guidance (`context.huge_root_notice`, `context.boot_scan_on_huge_root`)
29
+ (#356):** a session root that looks like a huge tree (the user home directory, a
30
+ filesystem root, or a root holding OS data directories) now prints one guidance
31
+ notice at boot and skips the boot-time warm scan; `!context scan` is the explicit
32
+ opt-in, and the knob restores the boot walk. Ordinary repositories boot with no
33
+ new output.
34
+
35
+ ### Fixed
36
+
37
+ - **Partial context scans are disclosed instead of served as truth (#357):** the
38
+ scanner records every dropped-subtree abort into a completeness verdict that
39
+ `Index#status_summary` publishes under the same lock as the files; `!context
40
+ scan` reports `finished PARTIALLY`, `!context status`/`stats` show the flag,
41
+ and `list_files`/`grep_search` append a PARTIAL note to answers served from a
42
+ truncated index (hits, no-matches, and ignored-directory verdicts alike).
43
+ Completed scans produce byte-identical output.
44
+ - **Tools never scan (#358):** an empty context index no longer triggers a
45
+ synchronous full walk inside `list_files`/`grep_search` — in a session rooted
46
+ at a large non-repo tree that walk ran for minutes inside the turn and, the
47
+ Ruby walker holding the GVL, stalled the whole REPL with it. An empty index
48
+ now answers honestly ("not ready, run `!context scan`"), the ignore-rules
49
+ explanation is reserved for indexes that were actually built, grep's
50
+ memory-only dirty-buffer searches disclose their scope on hits and no-hits
51
+ alike, AppData moves to the root-anchored hard-prune set (`.ollama`/`.zcode`
52
+ join the default ignores) with the rg prune globs gaining the any-depth forms
53
+ the Ruby side always applied, and `!context scan` warns on an empty result
54
+ instead of claiming success.
55
+
9
56
  ## [0.6.0] - 2026-11-02
10
57
 
11
58
  A measurement-and-honesty minor: the planned vakanüvis fixes gain their
@@ -1083,7 +1130,8 @@ installation section.
1083
1130
  - Context: serialized index mutations, snapshot-outside-lock reconcile,
1084
1131
  streaming-first truncation.
1085
1132
 
1086
- [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.6.0...HEAD
1133
+ [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.7.0...HEAD
1134
+ [0.7.0]: https://github.com/ozgurulukir/rubik/compare/v0.6.0...v0.7.0
1087
1135
  [0.6.0]: https://github.com/ozgurulukir/rubik/compare/v0.5.0...v0.6.0
1088
1136
  [0.5.0]: https://github.com/ozgurulukir/rubik/compare/v0.4.2...v0.5.0
1089
1137
  [0.4.2]: https://github.com/ozgurulukir/rubik/compare/v0.4.1...v0.4.2
@@ -209,6 +209,9 @@ module Rubik
209
209
  def grep_once(repo_dir, native:)
210
210
  best_of(3) do
211
211
  idx = Rubik::Context::Index.new(root_dir: repo_dir, native: native)
212
+ # Tools never scan; build the index up front (scan cost has its
213
+ # own scan_once benchmark).
214
+ idx.full_scan
212
215
  tool = Rubik::Tools::GrepSearchTool.new(nil, idx, root_dir: repo_dir, config: NativeConfig.new(native))
213
216
  ::Benchmark.realtime { tool.execute({ "query" => "needle_rare_marker", "max_results" => 50 }) }
214
217
  end
@@ -45,6 +45,12 @@ module Rubik
45
45
  "max_output_lines" => 300,
46
46
  "truncation_strategy" => "head_tail",
47
47
  "timeout_seconds" => 30,
48
+ # Dispatcher-level call budget in seconds; nil = off (inline execution,
49
+ # current behavior). RUBIK_TOOLS_CALL_TIMEOUT env overrides the file.
50
+ # A timed-out call is abandoned, not killed — bounds turn latency.
51
+ "call_timeout" => nil,
52
+ # Canonical tool name => seconds; 0/false disables for that tool.
53
+ "call_timeout_overrides" => {},
48
54
  "native" => "auto"
49
55
  },
50
56
  "router" => { "mode" => "auto", "sticky" => true },
@@ -104,6 +110,8 @@ module Rubik
104
110
  "tool_chronicle_max_entries" => 15, # Plan 85 D-2/D-3
105
111
  "merge_turn_tool_history" => true, # Plan 85 D-7
106
112
  "merge_turn_tool_max_entries" => 20, # Plan 85 D-7
113
+ "huge_root_notice" => true, # one guidance notice for likely-huge session roots (home dir, OS data)
114
+ "boot_scan_on_huge_root" => false, # false skips the boot warm walk on strong huge-root signals; !context scan opts in
107
115
  "emergency_truncate_strategy" => "head_tail" # EmergencyCompactor hard-truncate layer: "head_tail" | "head_only" | "tail_only"
108
116
  },
109
117
  "ui" => {
@@ -17,6 +17,12 @@ module Rubik
17
17
  # 17006) and can corrupt it. Scanning itself is not changed — only the
18
18
  # shared-state mutation is made thread-safe.
19
19
  @mutex = Mutex.new
20
+ # Serializes whole scan runs (full_scan / incremental_scan) so the
21
+ # files hash and the completeness verdict pair atomically — two
22
+ # concurrent scans would otherwise let one publish the other's
23
+ # clean verdict with its own aborted files. Held only by scanners,
24
+ # never by readers (status_summary stays lock-cheap).
25
+ @scan_mutex = Mutex.new
20
26
  @scanner = scanner || Scanner.new(
21
27
  root_dir: @root_dir,
22
28
  additional_roots: additional_roots,
@@ -27,6 +33,8 @@ module Rubik
27
33
  @files = {}
28
34
  @languages = []
29
35
  @last_scan_time = nil
36
+ @last_scan_aborted = false
37
+ @last_scan_error = nil
30
38
  end
31
39
 
32
40
  def update_file_filters(includes: nil, excludes: nil)
@@ -44,58 +52,66 @@ module Rubik
44
52
  end
45
53
 
46
54
  def full_scan
47
- start_time = Time.now
48
- files = @scanner.scan
49
- languages = @scanner.detect_languages(files)
50
- @mutex.synchronize do
51
- @files = files
52
- @languages = languages
53
- @last_scan_time = Time.now - start_time
55
+ @scan_mutex.synchronize do
56
+ start_time = Time.now
57
+ files = @scanner.scan
58
+ languages = @scanner.detect_languages(files)
59
+ report = scanner_report
60
+ @mutex.synchronize do
61
+ @files = files
62
+ @languages = languages
63
+ @last_scan_time = Time.now - start_time
64
+ store_scan_outcome!(report)
65
+ end
54
66
  end
55
67
  @files
56
68
  end
57
69
 
58
70
  def incremental_scan
59
- start_time = Time.now
60
- snapshot = @mutex.synchronize { @files }
61
- new_scan = @scanner.scan(snapshot)
62
- old_files = snapshot
63
-
64
- added = []
65
- modified = []
66
- deleted = []
67
- unchanged = []
68
-
69
- new_scan.each do |rel_path, info|
70
- if old_files.key?(rel_path)
71
- old_info = old_files[rel_path]
72
- if old_info[:mtime] != info[:mtime] || old_info[:size] != info[:size] || old_info[:hash] != info[:hash]
73
- modified << rel_path
71
+ @scan_mutex.synchronize do
72
+ start_time = Time.now
73
+ snapshot = @mutex.synchronize { @files }
74
+ new_scan = @scanner.scan(snapshot)
75
+ old_files = snapshot
76
+
77
+ added = []
78
+ modified = []
79
+ deleted = []
80
+ unchanged = []
81
+
82
+ new_scan.each do |rel_path, info|
83
+ if old_files.key?(rel_path)
84
+ old_info = old_files[rel_path]
85
+ if old_info[:mtime] != info[:mtime] || old_info[:size] != info[:size] || old_info[:hash] != info[:hash]
86
+ modified << rel_path
87
+ else
88
+ unchanged << rel_path
89
+ end
74
90
  else
75
- unchanged << rel_path
91
+ added << rel_path
76
92
  end
77
- else
78
- added << rel_path
79
93
  end
80
- end
81
94
 
82
- old_files.each_key do |rel_path|
83
- deleted << rel_path unless new_scan.key?(rel_path)
84
- end
95
+ old_files.each_key do |rel_path|
96
+ deleted << rel_path unless new_scan.key?(rel_path)
97
+ end
85
98
 
86
- languages = @scanner.detect_languages(new_scan)
87
- @mutex.synchronize do
88
- @files = new_scan
89
- @languages = languages
90
- @last_scan_time = Time.now - start_time
91
- end
99
+ languages = @scanner.detect_languages(new_scan)
100
+ report = scanner_report
101
+ @mutex.synchronize do
102
+ @files = new_scan
103
+ @languages = languages
104
+ @last_scan_time = Time.now - start_time
105
+ store_scan_outcome!(report)
106
+ end
92
107
 
93
- {
94
- added: added,
95
- modified: modified,
96
- deleted: deleted,
97
- unchanged: unchanged
98
- }
108
+ {
109
+ added: added,
110
+ modified: modified,
111
+ deleted: deleted,
112
+ unchanged: unchanged
113
+ }
114
+ end
99
115
  end
100
116
 
101
117
  def update_file(rel_path)
@@ -125,10 +141,28 @@ module Rubik
125
141
  {
126
142
  file_count: @files.size,
127
143
  languages: @languages,
128
- last_scan_time_sec: @last_scan_time&.round(4)
144
+ last_scan_time_sec: @last_scan_time&.round(4),
145
+ last_scan_aborted: @last_scan_aborted,
146
+ last_scan_error: @last_scan_error
129
147
  }
130
148
  end
131
149
  end
150
+
151
+ private
152
+
153
+ # Test doubles may not carry last_scan_report; a nil report means a
154
+ # clean scan.
155
+ def scanner_report
156
+ @scanner.respond_to?(:last_scan_report) ? @scanner.last_scan_report : nil
157
+ end
158
+
159
+ # Copies the scanner's completeness verdict under the same lock that
160
+ # publishes the files, so a reader never sees files from an aborted
161
+ # scan without also seeing the flag.
162
+ def store_scan_outcome!(report)
163
+ @last_scan_aborted = report ? report.aborted : false
164
+ @last_scan_error = report&.reasons&.any? ? report.reasons.join("; ") : nil
165
+ end
132
166
  end
133
167
  end
134
168
  end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ module Rubik
6
+ module Context
7
+ # Cheap classifiers for a session root that is likely a huge tree (a
8
+ # home directory, a filesystem root, or OS user-data). Equality and
9
+ # presence probes only — never size probing — so a normal-repo boot
10
+ # pays nothing and prints nothing.
11
+ module RootHeuristics
12
+ # strong: true when a signal reliably implies a full walk can stall
13
+ # the REPL (boot skips the warm scan); gitless alone is a weak
14
+ # aggravator reported in the guidance but never skips the scan.
15
+ Assessment = Struct.new(:reasons, :strong, keyword_init: true)
16
+
17
+ OS_DATA_DIRS = ["AppData", "Library"].freeze
18
+
19
+ module_function
20
+
21
+ def assess(root_dir, home_dir: nil, git_probe: nil)
22
+ root = File.expand_path(root_dir.to_s)
23
+ reasons = []
24
+
25
+ reasons << "root is the user home directory" if home_dir && File.expand_path(home_dir) == root
26
+ reasons << "root is a filesystem root" if filesystem_root?(root)
27
+ os_reason = os_data_dir_reason(root)
28
+ reasons << os_reason if os_reason
29
+
30
+ strong = reasons.any?
31
+
32
+ unless git_work_tree?(root, git_probe)
33
+ reasons << (if strong
34
+ "and is not a git repository (the fallback walker is slow)"
35
+ else
36
+ "root is not a git repository"
37
+ end)
38
+ end
39
+
40
+ Assessment.new(reasons: reasons, strong: strong)
41
+ end
42
+
43
+ # Single home for the guidance wording; reused by boot and !context
44
+ # scan. Empty string when nothing was flagged. A strong verdict also
45
+ # discloses the skipped boot scan so the empty index reads as
46
+ # deliberate, not as a stuck build.
47
+ def guidance(assessment, boot_scan_skipped: false)
48
+ return "" if !assessment || assessment.reasons.empty?
49
+
50
+ tail = boot_scan_skipped ? " The boot-time scan was skipped for this root; run '!context scan' to build the index now." : ""
51
+ "Warning: the session root looks like a huge tree (#{assessment.reasons.join('; ')}). " \
52
+ "Scanning it can be very slow. Consider starting rubik in a project root, " \
53
+ "adding 'context.exclude' entries, or running '!context scan' explicitly to opt in.#{tail}"
54
+ end
55
+
56
+ def filesystem_root?(dir)
57
+ File.dirname(dir) == dir
58
+ end
59
+
60
+ def os_data_dir_reason(root)
61
+ present = begin
62
+ Dir.children(root).any? { |name| OS_DATA_DIRS.include?(name) }
63
+ rescue SystemCallError
64
+ false
65
+ end
66
+ return nil unless present
67
+
68
+ "root contains OS data directories (#{OS_DATA_DIRS.join('/')})"
69
+ end
70
+
71
+ # The .git presence fast path keeps normal repos spawn-free; the probe
72
+ # (one `git rev-parse` spawn) only runs for gitless-looking roots. A
73
+ # failed probe (missing binary, ...) reads as gitless — over-warning
74
+ # by design — but stays discoverable in the log.
75
+ def git_work_tree?(dir, probe)
76
+ return true if File.exist?(File.join(dir, ".git"))
77
+
78
+ probe ||= ->(d) { system_git_probe(d) }
79
+ probe.call(dir)
80
+ rescue StandardError => e
81
+ Rubik::Log.debug(:context, "git probe failed for #{dir} (#{e.class}: #{e.message}); treating as not a repository")
82
+ false
83
+ end
84
+
85
+ def system_git_probe(dir)
86
+ out, _err, status = Open3.capture3("git", "-C", dir, "rev-parse", "--is-inside-work-tree")
87
+ # A bare repo also exits 0 but answers "false" — the verdict is the
88
+ # output, not the exit status.
89
+ status.success? && out.to_s.strip == "true"
90
+ end
91
+ end
92
+ end
93
+ end
@@ -65,7 +65,11 @@ module Rubik
65
65
  "dist/**",
66
66
  "build/**",
67
67
  ".cache/**",
68
- "coverage/**"
68
+ "coverage/**",
69
+ # Multi-GB local model blobs; never project sources.
70
+ ".ollama/**",
71
+ # Agent-harness state: transcripts, sessions, caches.
72
+ ".zcode/**"
69
73
  ].freeze
70
74
 
71
75
  # Subtrees whose ignore pattern is "<path>/**" (optionally with a
@@ -77,10 +81,17 @@ module Rubik
77
81
  # leaked into the index. The walker applies the pattern's plain meaning
78
82
  # (whole subtree ignored), matching real gitignore semantics.
79
83
  PRUNABLE_PATTERN = %r{\A(?:\*\*/)?(.+)/\*\*\z}
80
- # Also hard-rejected inside `ignored?`; pruned here before descending.
81
- HARD_PRUNE_ROOTS = [".git", ".rubik", ".ua", ".turbo", ".sst", ".next", ".nuxt"].freeze
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
82
88
 
83
- attr_reader :root_dir, :additional_roots
89
+ # Outcome of the most recent scan. A partial index served as ground
90
+ # truth is worse than an honest flag: every rescue boundary that drops
91
+ # data marks the report so Index can surface the truncation.
92
+ ScanReport = Struct.new(:aborted, :reasons, keyword_init: true)
93
+
94
+ attr_reader :root_dir, :additional_roots, :last_scan_report
84
95
  attr_accessor :custom_includes, :custom_excludes
85
96
 
86
97
  def initialize(root_dir: Dir.pwd, additional_roots: [], includes: [], excludes: [], native: nil)
@@ -106,6 +117,7 @@ module Rubik
106
117
  # may itself be nil — content never hashed), only stat-changed files
107
118
  # are read and re-hashed. Cold entries start hash-less.
108
119
  def scan(previous = {})
120
+ @last_scan_report = ScanReport.new(aborted: false, reasons: [])
109
121
  files = {}
110
122
 
111
123
  # 1. Scan primary root
@@ -123,6 +135,7 @@ module Rubik
123
135
  files
124
136
  rescue StandardError => e
125
137
  Rubik::Log.warn(:context, "context scan aborted partway (#{e.class}: #{e.message}); returning partial index")
138
+ mark_scan_aborted!("scan aborted partway: #{e.class}: #{e.message}")
126
139
  files || {}
127
140
  end
128
141
 
@@ -155,6 +168,16 @@ module Rubik
155
168
 
156
169
  private
157
170
 
171
+ # Records one dropped-data event. The first few reasons suffice for
172
+ # display; the log keeps the full detail.
173
+ def mark_scan_aborted!(reason)
174
+ @last_scan_report ||= ScanReport.new(aborted: false, reasons: [])
175
+ @last_scan_report.aborted = true
176
+ return if @last_scan_report.reasons.length >= 5
177
+
178
+ @last_scan_report.reasons << reason.to_s[0, 160]
179
+ end
180
+
158
181
  # Stat-dirty model (à la git): a previous entry whose size and mtime
159
182
  # (microsecond precision) both match is NEVER read from disk — its
160
183
  # content hash is reused verbatim, including when it is nil (content
@@ -210,7 +233,9 @@ module Rubik
210
233
  # workspace_xml entry_points listing) stays deterministic.
211
234
  entries = begin
212
235
  Dir.foreach(current).sort - [".", ".."]
213
- rescue SystemCallError
236
+ rescue SystemCallError => e
237
+ Rubik::Log.warn(:context, "directory unreadable: #{current} (#{e.class}: #{e.message})")
238
+ mark_scan_aborted!("directory unreadable: #{current}")
214
239
  []
215
240
  end
216
241
 
@@ -250,6 +275,7 @@ module Rubik
250
275
  out
251
276
  rescue StandardError => e
252
277
  Rubik::Log.warn(:context, "directory walk failed under #{base_dir} (#{e.class}: #{e.message}); keeping partial results")
278
+ mark_scan_aborted!("directory walk failed under #{base_dir}: #{e.class}: #{e.message}")
253
279
  out
254
280
  end
255
281
 
@@ -306,6 +332,7 @@ module Rubik
306
332
  walk_dir(base_dir, "", prefix, ignore_patterns, previous, needles)
307
333
  rescue StandardError => e
308
334
  Rubik::Log.warn(:context, "root scan failed for #{base_dir} (#{e.class}: #{e.message}); root omitted from index")
335
+ mark_scan_aborted!("root scan failed for #{base_dir}: #{e.class}: #{e.message}")
309
336
  {}
310
337
  end
311
338
 
@@ -365,6 +392,7 @@ module Rubik
365
392
  out
366
393
  rescue StandardError => e
367
394
  Rubik::Log.warn(:context, "git ls-files fast path failed for #{base_dir} (#{e.class}: #{e.message}); keeping partial index")
395
+ mark_scan_aborted!("git ls-files fast path failed for #{base_dir}: #{e.class}: #{e.message}")
368
396
  out
369
397
  end
370
398
 
data/lib/rubik/context.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "context/scanner"
4
4
  require_relative "context/index"
5
+ require_relative "context/root_heuristics"
5
6
  require_relative "context/git_info"
6
7
  require_relative "context/repo_context_reader"
7
8
 
@@ -5,6 +5,7 @@ require_relative "auto_apply"
5
5
  require_relative "tool_call_id"
6
6
  require_relative "tool_history"
7
7
  require_relative "tool_circuit_breaker"
8
+ require_relative "tool_timeout"
8
9
  require_relative "maestro_gate"
9
10
  require_relative "lsp_feedback"
10
11
 
@@ -160,11 +161,20 @@ module Rubik
160
161
  "circuit open for '#{tool_instance.name}' — repeated failures; waiting out the cooldown before retrying"
161
162
  end
162
163
 
163
- raw_output = tool_instance.execute(tool_args)
164
+ raw_output = ToolTimeout.call(tool_instance, config: @deps.shell&.config,
165
+ canonical_name: tool_instance.name) do
166
+ tool_instance.execute(tool_args)
167
+ end
164
168
  t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
165
169
  dur = ((t1 - t0) * 1000).round(1)
166
170
 
167
171
  normalize_raw_output(raw_output, tool_name, call_id, dur)
172
+ rescue ToolTimeout::BudgetExceeded => e
173
+ t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
174
+ dur = ((t1 - t0) * 1000).round(1)
175
+ ToolTimeout.emit_timeout_event(tool_name: tool_instance.name, budget_seconds: e.budget_seconds,
176
+ elapsed_seconds: e.elapsed_seconds, context: @deps.runtime_manager&.context)
177
+ ToolTimeout.timeout_result(tool_name: tool_name, call_id: call_id, exceeded: e, duration_ms: dur)
168
178
  rescue StandardError => e
169
179
  t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
170
180
  dur = ((t1 - t0) * 1000).round(1)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../../tools/truncator"
4
4
  require_relative "../../tools/result"
5
+ require_relative "../tool_timeout"
5
6
 
6
7
  module Rubik
7
8
  module Orchestrator
@@ -16,12 +17,20 @@ module Rubik
16
17
  def dispatch_subagent_tool(tool, name, args)
17
18
  return error_result(name, "Tool '#{name}' is not available in subagent toolset.") unless tool
18
19
 
19
- raw = tool.execute(args)
20
20
  cfg = @config.respond_to?(:fetch) ? @config : nil
21
+ raw = Rubik::Orchestrator::ToolTimeout.call(tool, config: cfg,
22
+ canonical_name: tool.respond_to?(:name) ? tool.name : name) do
23
+ tool.execute(args)
24
+ end
21
25
  max_chars = (cfg&.fetch("tools.max_output_chars", Rubik::Tools::Truncator::DEFAULT_MAX_CHARS) || Rubik::Tools::Truncator::DEFAULT_MAX_CHARS).to_i
22
26
  max_lines = (cfg&.fetch("tools.max_output_lines", Rubik::Tools::Truncator::DEFAULT_MAX_LINES) || Rubik::Tools::Truncator::DEFAULT_MAX_LINES).to_i
23
27
  strategy = Rubik::Tools::Truncator.resolve_strategy(cfg&.fetch("tools.truncation_strategy", Rubik::Tools::Truncator::DEFAULT_STRATEGY))
24
28
  normalize_tool_result(raw, name, max_chars: max_chars, max_lines: max_lines, strategy: strategy)
29
+ rescue Rubik::Orchestrator::ToolTimeout::BudgetExceeded => e
30
+ Rubik::Orchestrator::ToolTimeout.emit_timeout_event(tool_name: name, budget_seconds: e.budget_seconds,
31
+ elapsed_seconds: e.elapsed_seconds, context: @context)
32
+ Rubik::Orchestrator::ToolTimeout.timeout_result(tool_name: name, call_id: nil, exceeded: e,
33
+ duration_ms: (e.elapsed_seconds * 1000).round(1))
25
34
  rescue StandardError => e
26
35
  error_result(name, "Tool execution error: #{e.class}: #{e.message}", error: e.message)
27
36
  end
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../coeffects"
4
+ require_relative "../tools/result"
5
+
6
+ module Rubik
7
+ module Orchestrator
8
+ # Bounded tool execution behind `tools.call_timeout`. The dispatch seam
9
+ # runs the call on a worker thread and joins with the resolved budget.
10
+ # Ruby threads cannot be force-killed: an expired call is ABANDONED —
11
+ # it keeps running in the background and its result is discarded — so
12
+ # the budget bounds turn latency, not resource use. Budget off (nil)
13
+ # yields inline: byte-identical to the unwrapped seam.
14
+ module ToolTimeout
15
+ # Raised in the CALLER thread when the join budget expires. The
16
+ # dispatch seam's rescue converts it into an honest error Result so
17
+ # the turn continues.
18
+ class BudgetExceeded < StandardError
19
+ attr_reader :tool_name, :budget_seconds, :elapsed_seconds
20
+
21
+ def initialize(tool_name:, budget_seconds:, elapsed_seconds:)
22
+ @tool_name = tool_name
23
+ @budget_seconds = budget_seconds
24
+ @elapsed_seconds = elapsed_seconds
25
+ super("tool '#{tool_name}' exceeded the #{budget_seconds}s call budget")
26
+ end
27
+ end
28
+
29
+ ENV_KEY = "RUBIK_TOOLS_CALL_TIMEOUT"
30
+ OFF = :off
31
+
32
+ module_function
33
+
34
+ # Budget resolution precedence: an explicit env disable wins, then the
35
+ # per-tool override (off or budget), then the env / config global.
36
+ # Returns nil = off (inline execution).
37
+ def budget_for(config, canonical_name)
38
+ env = resolve_env_budget
39
+ return nil if env == OFF
40
+
41
+ override = resolve_override(config, canonical_name)
42
+ return nil if override == OFF
43
+ return override unless override.nil?
44
+
45
+ env.nil? ? resolve_config_budget(config) : env
46
+ end
47
+
48
+ # The seam entry point. budget nil ⇒ yield inline (no Thread.new — the
49
+ # "knob unset ⇒ byte-identical" contract; the tool is not even asked
50
+ # for its name). Otherwise the yield runs on a named worker thread; an
51
+ # expired join abandons the worker and raises BudgetExceeded in the
52
+ # caller.
53
+ def call(tool, config:, canonical_name: nil)
54
+ budget = budget_for(config, canonical_name)
55
+ return yield if budget.nil?
56
+
57
+ name = canonical_name || tool.name
58
+ result = nil
59
+ error = nil
60
+ # Distinguishes "worker never completed" (killed before the body /
61
+ # hard VM abort — no honest answer exists) from a legitimate nil
62
+ # return. A kill landing mid-body still runs the ensure, which reads
63
+ # as a nil result — the inline-parity residual this seam accepts.
64
+ done = false
65
+ t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
66
+ worker = Thread.new do
67
+ result = yield
68
+ # Re-raised verbatim in the caller below, preserving today's
69
+ # raise-path semantics for ANY exception class, not just
70
+ # StandardError. The re-raise carries the worker-side backtrace;
71
+ # dispatch-site frames are not prepended (the tool's own failure
72
+ # site is what matters for debugging).
73
+ rescue Exception => e # rubocop:disable Lint/RescueException
74
+ error = e
75
+ ensure
76
+ done = true
77
+ end
78
+ worker.name = "rubik-tool-#{name}"
79
+
80
+ unless worker.join(budget)
81
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
82
+ raise BudgetExceeded.new(tool_name: name, budget_seconds: budget, elapsed_seconds: elapsed)
83
+ end
84
+ raise error if error
85
+ raise StandardError, "tool worker for '#{name}' died without completing or raising" unless done
86
+
87
+ result
88
+ end
89
+
90
+ # Honest error result: names the tool and budget, discloses the
91
+ # abandonment, and points at the narrower-retry / override escapes.
92
+ def timeout_result(tool_name:, call_id:, exceeded:, duration_ms:)
93
+ msg = "Tool '#{tool_name}' timed out after #{exceeded.budget_seconds}s. " \
94
+ "The call was abandoned (it may still be running in the background) and its result was discarded. " \
95
+ "Retry with a narrower scope (e.g. add a 'path' or 'extension' filter), or adjust " \
96
+ "'tools.call_timeout_overrides' for this tool."
97
+ Rubik::Tools::Result.new(
98
+ tool_name: tool_name, call_id: call_id, status: :error,
99
+ error: "tool call timeout (budget #{exceeded.budget_seconds}s)", output: msg,
100
+ duration_ms: duration_ms,
101
+ metadata: { "timed_out" => true, "budget_seconds" => exceeded.budget_seconds }
102
+ )
103
+ end
104
+
105
+ # Observability door (mirrors MCP Manager#emit_mcp_event): post —
106
+ # never emit (drain) — so the dispatcher's dmesg door records the
107
+ # timeout under the "tools" subsystem. An unresolved bus, or a post
108
+ # refused by a stopped dispatcher (returns false without raising),
109
+ # falls back to the log; a failing emission never alters the tool
110
+ # result.
111
+ def emit_timeout_event(tool_name:, budget_seconds:, elapsed_seconds:, context:)
112
+ bus = Rubik::Coeffects.get(context, Rubik::Coeffects::EVENTS)
113
+ if bus.respond_to?(:post)
114
+ posted = bus.post(:tool_call_timeout, { tool: tool_name },
115
+ subsystem: :tools, level: :warn,
116
+ budget_seconds: budget_seconds, elapsed_seconds: elapsed_seconds.round(1))
117
+ return if posted
118
+
119
+ Rubik::Log.warn(:tools, "tool '#{tool_name}' exceeded the #{budget_seconds}s call budget " \
120
+ "(event bus refused the record; abandoned after #{elapsed_seconds.round(1)}s)")
121
+ else
122
+ Rubik::Log.warn(:tools, "tool '#{tool_name}' exceeded the #{budget_seconds}s call budget " \
123
+ "(abandoned after #{elapsed_seconds.round(1)}s)")
124
+ end
125
+ rescue StandardError => e
126
+ Rubik::Log.warn(:tools, "failed to emit tool timeout event for '#{tool_name}' " \
127
+ "(budget #{budget_seconds}s: #{e.class}: #{e.message})")
128
+ end
129
+
130
+ # "0" / "off" / "false" / "no" explicitly disable (beats the config
131
+ # file and per-tool overrides); any other non-positive number or
132
+ # garbage warns and falls through to config.
133
+ def resolve_env_budget
134
+ raw = ENV.fetch(ENV_KEY, nil)
135
+ return nil if raw.nil? || raw.strip.empty?
136
+ return OFF if ["off", "false", "no"].include?(raw.strip.downcase)
137
+
138
+ value = parse_seconds(raw)
139
+ if value.nil?
140
+ Rubik::Log.warn(:tools, "ignoring invalid #{ENV_KEY} #{raw.inspect}; falling back to config")
141
+ return nil
142
+ end
143
+ return OFF if value <= 0
144
+
145
+ value
146
+ end
147
+
148
+ # mcp.call_timeout mirror at the config boundary: an invalid value
149
+ # warns and lands on off — the knob must never fail a turn.
150
+ def resolve_config_budget(config)
151
+ raw = config&.fetch("tools.call_timeout", nil)
152
+ return nil if raw.nil?
153
+
154
+ value = parse_seconds(raw)
155
+ return value if value&.positive?
156
+
157
+ Rubik::Log.warn(:tools, "ignoring invalid tools.call_timeout #{raw.inspect} (expected a positive number); treating as off")
158
+ nil
159
+ end
160
+
161
+ # Canonical tool name => seconds. 0/false disables the budget for that
162
+ # tool (the per-tool escape hatch); a positive value replaces the
163
+ # global budget; garbage warns and inherits. Lookup uses key presence,
164
+ # not truthiness — a `false` override must survive the `||` shorthand.
165
+ def resolve_override(config, canonical_name)
166
+ overrides = config&.fetch("tools.call_timeout_overrides", nil)
167
+ return nil unless overrides.is_a?(Hash)
168
+
169
+ key = canonical_name.to_s
170
+ raw = overrides.key?(key) ? overrides[key] : overrides[key.to_sym]
171
+ return nil if raw.nil?
172
+
173
+ value = parse_seconds(raw)
174
+ return OFF if value.nil? && ["off", "false"].include?(raw.to_s.strip.downcase)
175
+ return OFF if value && value <= 0
176
+ return value if value
177
+
178
+ Rubik::Log.warn(:tools, "ignoring invalid call_timeout override for '#{canonical_name}' #{raw.inspect}; using the global budget")
179
+ nil
180
+ end
181
+
182
+ # Non-finite values (Infinity, NaN) are rejected: an infinite budget
183
+ # would silently block the turn forever, the exact failure this knob
184
+ # exists to bound.
185
+ def parse_seconds(raw)
186
+ value = Float(raw)
187
+ value&.finite? ? value : nil
188
+ rescue ArgumentError, TypeError
189
+ nil
190
+ end
191
+ end
192
+ end
193
+ end
@@ -188,6 +188,12 @@ module Rubik
188
188
  @shell.warm_ast_graph(async: async)
189
189
  end
190
190
 
191
+ # Huge-root guidance (#356): boot and !context scan share the
192
+ # assessment and the once-per-session memo on the Shell.
193
+ def maybe_warn_huge_root!(strong_only: true)
194
+ @shell.maybe_warn_huge_root!(strong_only: strong_only)
195
+ end
196
+
191
197
  def build_tool_registry
192
198
  @shell.build_tool_registry
193
199
  end
@@ -49,9 +49,23 @@ module Rubik
49
49
  end
50
50
 
51
51
  private_class_method def self.handle_context_scan(ctx)
52
+ # Explicit opt-in point: weak signals (a plain gitless root) speak here.
53
+ ctx.maybe_warn_huge_root!(strong_only: false)
52
54
  ctx.context_index.full_scan
53
55
  ctx.warm_ast_graph(async: false)
54
- ctx.out.puts "Context index and AST symbol graph updated."
56
+ summary = ctx.context_index.status_summary
57
+ if ctx.context_index.files.empty?
58
+ # Scanner#scan swallows mid-walk aborts into a possibly empty
59
+ # index; claiming success here would loop the model forever.
60
+ ctx.out.puts "Context scan produced NO indexed files — the scan aborted partway (see the log for " \
61
+ "'context scan aborted') or the root has no indexable files. " \
62
+ "Tools will answer 'not ready' until a scan yields files."
63
+ elsif summary[:last_scan_aborted]
64
+ ctx.out.puts "Context scan finished PARTIALLY: #{summary[:file_count]} files indexed, but the scan " \
65
+ "aborted (#{summary[:last_scan_error]}). Results may be incomplete."
66
+ else
67
+ ctx.out.puts "Context index and AST symbol graph updated."
68
+ end
55
69
  rescue Errno::EACCES => e
56
70
  root_path = begin
57
71
  ctx.buffer_manager.root_dir
@@ -122,6 +136,7 @@ module Rubik
122
136
  ctx.out.puts " • Consumed Tokens : #{est_tokens} tokens (#{pct}% of usable capacity)"
123
137
  ctx.out.puts " • Active Turns : #{ctx.conversation_history.size} turn(s)"
124
138
  ctx.out.puts " • Indexed Files : #{summary[:file_count]} files"
139
+ ctx.out.puts " • Index State : PARTIAL — last scan aborted (#{summary[:last_scan_error]})" if summary[:last_scan_aborted]
125
140
  ctx.out.puts " • AST Symbol Count : #{ast_symbols_count} symbols"
126
141
  ctx.out.puts "=================================="
127
142
  guidance = Rubik::Tools::DiagnosticReporter.render_context_status_guidance(
@@ -159,6 +174,7 @@ module Rubik
159
174
  ctx.out.puts "Estimated Prompt Tokens: #{est_tokens} / #{ceiling} tokens #{bar_str}"
160
175
  ctx.out.puts "Compaction Strategy: #{strategy}"
161
176
  ctx.out.puts "Indexed Files: #{summary[:file_count]}, AST Symbols: #{ast_symbols_count}"
177
+ ctx.out.puts "Index State: PARTIAL — last scan aborted (#{summary[:last_scan_error]})" if summary[:last_scan_aborted]
162
178
  ctx.out.puts "============================================="
163
179
  return unless (args && (args.include?("--guidance") || args.include?("-g"))) || est_tokens > (ceiling * 0.8)
164
180
 
@@ -16,7 +16,8 @@ module Rubik
16
16
  Parts = ServiceCatalog.parts_struct
17
17
 
18
18
  def self.build(config:, root_dir: nil, event_bus: nil, buffer_manager: nil, snapshot_store: nil, dmesg: nil, dispatcher: nil, init: nil)
19
- effective_root = File.expand_path(root_dir || buffer_manager&.root_dir || config&.root_dir || Dir.pwd)
19
+ fallback_root = (ENV["RUBIK_TEST"] == "1" && ENV.fetch("RUBIK_TEST_ROOT", nil)) || Dir.pwd
20
+ effective_root = File.expand_path(root_dir || buffer_manager&.root_dir || config&.root_dir || fallback_root)
20
21
  scope = ServiceCatalog::Scope.new(
21
22
  root_dir: effective_root,
22
23
  overrides: {
@@ -141,7 +141,11 @@ module Rubik
141
141
  # default (RUBIK_TEST=1 warms synchronously-off) so the schema entry
142
142
  # never forces async warm in tests.
143
143
  warm_async = Platform.async_knob(@config, "ast.async_warm")
144
- warm_ast_graph(async: warm_async)
144
+ maybe_warn_huge_root!
145
+ # A strong huge-root signal skips the boot warm walk: the index
146
+ # stays empty and tools answer the honest not-ready message until
147
+ # an explicit '!context scan' opts in. Warning only — never a block.
148
+ warm_ast_graph(async: warm_async) unless skip_huge_root_boot_scan?
145
149
  register_default_commands
146
150
  Rubik.current_shell = self
147
151
  Rubik.runtime_manager = @runtime_manager
@@ -159,6 +163,52 @@ module Rubik
159
163
  wire_subagent_events!
160
164
  end
161
165
 
166
+ # Huge-root guidance: classify the session root with cheap probes,
167
+ # print ONE guidance notice per session, and (via skip_huge_root_boot_scan?)
168
+ # gate the boot warm walk. Boot warns on STRONG signals only — a plain
169
+ # gitless root stays silent so ordinary sessions and tests see no new
170
+ # output; the weak gitless aggravator surfaces at the explicit
171
+ # '!context scan' (strong_only: false). Memoized so the scan reuses
172
+ # the assessment without a second git spawn.
173
+ def maybe_warn_huge_root!(strong_only: true)
174
+ return unless @config.nil? || @config.fetch("context.huge_root_notice", true)
175
+ return if @huge_root_notice_shown
176
+
177
+ assessment = huge_root_assessment
178
+ return if strong_only && !assessment.strong
179
+
180
+ guidance = Rubik::Context::RootHeuristics.guidance(assessment, boot_scan_skipped: huge_root_boot_scan_skipped?)
181
+ return if guidance.empty?
182
+
183
+ @huge_root_notice_shown = true
184
+ if @terminal_guard
185
+ @terminal_guard.print_safe_notice(guidance)
186
+ else
187
+ @output&.puts guidance
188
+ end
189
+ end
190
+
191
+ def huge_root_assessment
192
+ @huge_root_assessment ||= Rubik::Context::RootHeuristics.assess(
193
+ @buffer_manager&.root_dir || @config&.root_dir || Dir.pwd,
194
+ home_dir: @config.respond_to?(:home_dir) ? @config.home_dir : nil
195
+ )
196
+ end
197
+
198
+ # Boot scans by default; only a STRONG signal (home dir, filesystem
199
+ # root, OS-data dir) skips the walk, and the knob re-enables it.
200
+ # Gitless roots are weak: guidance only, boot unaffected.
201
+ def skip_huge_root_boot_scan?
202
+ huge_root_boot_scan_skipped?
203
+ end
204
+
205
+ def huge_root_boot_scan_skipped?
206
+ return false unless huge_root_assessment.strong
207
+ return false unless @config.respond_to?(:fetch)
208
+
209
+ !@config.fetch("context.boot_scan_on_huge_root", false)
210
+ end
211
+
162
212
  # Async MCP boot progress notices (mcp-async-boot D8): subscribe BEFORE
163
213
  # the boot triggers so no event is missed, with the unsubscribe inverse
164
214
  # registered in the same breath (wire_subagent_events! pattern). Async
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../base"
4
4
  require_relative "../../coeffects"
5
+ require_relative "../index_messages"
5
6
  require_relative "../uri_resolver"
6
7
  require_relative "../output"
7
8
  require_relative "../diagnostic_reporter"
@@ -152,6 +153,11 @@ module Rubik
152
153
  }
153
154
  }
154
155
  )
156
+ # Honest not-ready answer for the empty-index window: names the cause,
157
+ # the escape hatch, and that ignore-excluded dirs never enter the index.
158
+ INDEX_NOT_READY_MESSAGE = "#{IndexMessages::INDEX_NOT_READY_PREFIX}" \
159
+ "browse with run_command (e.g. `ls`) / read files by absolute path. " \
160
+ "Note: directories excluded by ignore rules (.gitignore / default excludes) never enter the index.".freeze
155
161
  def initialize(context_index = nil, root_dir: Dir.pwd, runtime_context: nil, context: nil)
156
162
  super(DECLARATION)
157
163
  @context_index = context_index
@@ -167,7 +173,7 @@ module Rubik
167
173
  path_filter = args["path"] || args[:path]
168
174
  ext_filter = args["extension"] || args[:extension]
169
175
  idx = context_index
170
- return ToolOutput.error("Context index is not available. Run '!context scan' or restart the session to build it.") unless idx
176
+ return ToolOutput.error(IndexMessages::INDEX_UNAVAILABLE_MESSAGE) unless idx
171
177
 
172
178
  filter, error = BaseTool.normalize_path_filter(path_filter, @root_dir)
173
179
  if error
@@ -176,23 +182,36 @@ module Rubik
176
182
  return ToolOutput.error(DiagnosticReporter.render_traversal_blocked(path_filter, allowed_roots: allowed))
177
183
  end
178
184
 
179
- files = idx.files.empty? ? idx.full_scan : idx.files
185
+ # Tools never scan: an empty index answers honestly instead of
186
+ # triggering a synchronous full_scan inside the tool call.
187
+ return ToolOutput.error(INDEX_NOT_READY_MESSAGE) if idx.files.empty?
188
+
189
+ files = idx.files
190
+ # A no-match/ignored verdict from a PARTIAL index is the dangerous
191
+ # case — the file may exist but the aborted walk never saw it — so
192
+ # every answer below carries the disclosure.
193
+ partial_note = IndexMessages.note_for(idx)
180
194
  # Path-filtered list drives the B9 explanation: an extension miss in
181
195
  # an INDEXED directory must read "no such files here", not "ignored".
182
196
  path_filtered = filter.empty? ? files.keys : files.keys.select { |f| f.start_with?(filter) }
183
197
  file_list = ext_filter && !ext_filter.empty? ? path_filtered.select { |f| f.end_with?(ext_filter) } : path_filtered
184
198
 
185
- return ignored_directory_message(filter, ext_filter) if path_filtered.empty? && existing_but_unindexed?(@root_dir, filter)
199
+ return with_note(ignored_directory_message(filter, ext_filter), partial_note) if path_filtered.empty? && existing_but_unindexed?(@root_dir, filter)
186
200
 
187
- return BaseTool.no_match_message(filter) if file_list.empty?
201
+ return with_note(BaseTool.no_match_message(filter), partial_note) if file_list.empty?
188
202
 
189
- "Found #{file_list.size} file(s):\n#{file_list.first(50).map { |f| " - #{f}" }.join("\n")}"
203
+ listing = "Found #{file_list.size} file(s):\n#{file_list.first(50).map { |f| " - #{f}" }.join("\n")}"
204
+ partial_note ? "#{listing}#{partial_note}" : listing
190
205
  rescue Errno::EACCES => e
191
206
  ToolOutput.error(DiagnosticReporter.render_permission_denied(path_filter || @root_dir, operation: "read", reason: e.message))
192
207
  end
193
208
 
194
209
  private
195
210
 
211
+ def with_note(message, note)
212
+ note ? "#{message}#{note}" : message
213
+ end
214
+
196
215
  # Session review B9: `list_files(path: 'lib')` answered "No matching
197
216
  # files" for a directory that exists — the context index prunes ignore
198
217
  # rules (.gitignore, DEFAULT_IGNORES) silently. Say so and hand the
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../base"
4
4
  require_relative "../../coeffects"
5
+ require_relative "../index_messages"
5
6
  require_relative "../output"
6
7
  require_relative "../native_search"
7
8
  require_relative "../diagnostic_reporter"
@@ -30,6 +31,10 @@ module Rubik
30
31
  required: ["query"]
31
32
  }
32
33
  )
34
+ # Honest not-ready answer: an empty index means "nothing searched
35
+ # yet", not "no matches".
36
+ INDEX_NOT_READY_MESSAGE = "#{IndexMessages::INDEX_NOT_READY_PREFIX}" \
37
+ "search with run_command (e.g. `rg` / `grep`) directly.".freeze
33
38
  def initialize(buffer_manager = nil, context_index = nil, root_dir: Dir.pwd, runtime_context: nil, context: nil, config: nil)
34
39
  super(DECLARATION)
35
40
  @buffer_manager = buffer_manager
@@ -72,28 +77,53 @@ module Rubik
72
77
  regex = regex_for(query, case_sensitive)
73
78
 
74
79
  matches = try_native_search(query, path_filter, ext_filter, case_sensitive, max_results, regex)
75
- matches ||= ruby_search(path_filter, ext_filter, regex, max_results)
76
-
77
- if matches.empty?
78
- "No matching lines found for '#{query}'.#{" (path filter: '#{path_filter}')" unless path_filter.empty?}"
79
- else
80
- lines = ["Found #{matches.size} match(es) for '#{query}':"]
81
- total_chars = 0
82
- capped = false
83
- matches.each do |m|
84
- content = sanitize_line_content(m[:content].to_s)
85
- entry = "#{m[:file]}:#{m[:line]}: #{content}"
86
- if (total_chars + entry.length) > MAX_OUTPUT_CHARS
87
- capped = true
88
- break
89
- end
90
-
91
- lines << entry
92
- total_chars += entry.length
80
+ scope_note = nil
81
+ if matches.nil?
82
+ # Tools never scan: an absent or empty index answers honestly.
83
+ idx = context_index
84
+ return ToolOutput.error(IndexMessages::INDEX_UNAVAILABLE_MESSAGE) if idx.nil?
85
+
86
+ if idx.files.empty?
87
+ buf = buffer_manager
88
+ dirty = buf&.dirty_files ? buf.dirty_files.keys : []
89
+ scoped = dirty.select { |f| pass_path?(f, path_filter) && pass_extension?(f, ext_filter) }
90
+ return ToolOutput.error(INDEX_NOT_READY_MESSAGE) if scoped.empty?
91
+
92
+ # Memory-only search: the disk was not scanned, so hits and
93
+ # no-hits must both disclose that scope.
94
+ hits = ruby_search(path_filter, ext_filter, regex, max_results)
95
+ scope_note = "Searched only the #{scoped.size} in-memory dirty buffer file(s) — the context index is empty, " \
96
+ "the disk was not searched."
97
+ return ToolOutput.error("#{INDEX_NOT_READY_MESSAGE} #{scope_note} No matches there.") if hits.empty?
98
+
99
+ matches = hits
93
100
  end
94
- lines << "... [Grep output capped at #{MAX_OUTPUT_CHARS / 1024}KB to protect context window. Narrow search via 'path' or 'extension']" if capped
95
- lines.join("\n")
96
101
  end
102
+ matches ||= ruby_search(path_filter, ext_filter, regex, max_results)
103
+
104
+ result = if matches.empty?
105
+ "No matching lines found for '#{query}'.#{" (path filter: '#{path_filter}')" unless path_filter.empty?}"
106
+ else
107
+ lines = ["Found #{matches.size} match(es) for '#{query}':"]
108
+ total_chars = 0
109
+ capped = false
110
+ matches.each do |m|
111
+ content = sanitize_line_content(m[:content].to_s)
112
+ entry = "#{m[:file]}:#{m[:line]}: #{content}"
113
+ if (total_chars + entry.length) > MAX_OUTPUT_CHARS
114
+ capped = true
115
+ break
116
+ end
117
+
118
+ lines << entry
119
+ total_chars += entry.length
120
+ end
121
+ lines << "... [Grep output capped at #{MAX_OUTPUT_CHARS / 1024}KB to protect context window. Narrow search via 'path' or 'extension']" if capped
122
+ lines.join("\n")
123
+ end
124
+ note = IndexMessages.note_for(context_index)
125
+ result = "#{result}#{note}" if note
126
+ scope_note ? "#{scope_note}\n#{result}" : result
97
127
  rescue Errno::EACCES => e
98
128
  ToolOutput.error(DiagnosticReporter.render_permission_denied(path_filter.empty? ? @root_dir : path_filter, operation: "read", reason: e.message))
99
129
  rescue Rubik::Buffer::InvalidPathError
@@ -150,18 +180,11 @@ module Rubik
150
180
  (native + memory).sort_by { |m| [m[:file], m[:line]] }.first(max_results)
151
181
  end
152
182
 
153
- # Pure-Ruby search over the context index (or a fresh scan). Reads
154
- # stay on the historical data path — buffer.read for every file when a
155
- # buffer manager exists (it warms the diff baseline), whole-content
156
- # strings otherwise — while the per-file line iteration avoids
157
- # materializing a line array.
183
+ # Pure-Ruby search over the context index; buffer.read when a buffer
184
+ # manager exists, streamed lines otherwise. Callers guarantee a
185
+ # non-nil, non-empty index (execute refuses otherwise).
158
186
  def ruby_search(path_filter, ext_filter, regex, max_results)
159
- idx = context_index
160
- files = if idx
161
- idx.files.empty? ? idx.full_scan : idx.files
162
- else
163
- Rubik::Context::Scanner.new(root_dir: @root_dir, native: @config&.fetch("tools.native")).scan
164
- end
187
+ files = context_index.files
165
188
 
166
189
  all_file_keys = files.keys
167
190
  all_file_keys = (all_file_keys + buffer_manager.dirty_files.keys).uniq if buffer_manager&.dirty_files
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module Tools
5
+ # Shared wording for the tools that consume the context index: one
6
+ # prefix keeps the not-ready phrasing consistent across tools.
7
+ module IndexMessages
8
+ # The index exists but holds no files. Tools append their own
9
+ # escape-hatch tail to this prefix.
10
+ INDEX_NOT_READY_PREFIX = "Context index has no files indexed yet (still building, or the scan failed). " \
11
+ "Run '!context scan' to build it, or "
12
+
13
+ # The index coeffect is absent entirely: nothing to search against.
14
+ INDEX_UNAVAILABLE_MESSAGE = "Context index is not available. Run '!context scan' or restart the session to build it."
15
+
16
+ # Suffix for SUCCESS results served from a PARTIAL index: the model
17
+ # must never mistake a truncated index for ground truth. Returns nil
18
+ # for a clean scan so success output stays unchanged.
19
+ module_function
20
+
21
+ # Reads the completeness verdict off an index's status_summary; nil
22
+ # for a clean scan or an object without the summary seam (test doubles).
23
+ def note_for(idx)
24
+ return nil unless idx.respond_to?(:status_summary)
25
+
26
+ summary = idx.status_summary
27
+ partial_index_note(summary[:last_scan_aborted], summary[:last_scan_error])
28
+ end
29
+
30
+ def partial_index_note(aborted, error)
31
+ return nil unless aborted
32
+
33
+ " Note: the context index is PARTIAL — the last scan aborted" \
34
+ "#{" (#{error})" if error}. Results may be missing files; '!context scan' rebuilds it."
35
+ end
36
+ end
37
+ end
38
+ end
@@ -35,12 +35,21 @@ module Rubik
35
35
  @rg_path = nil
36
36
 
37
37
  class << self
38
- # Rubik prunes these subtrees itself; the target repo's .gitignore
39
- # may not, so the rg-side and Ruby-side file universes stay
40
- # identical. Lazily built: Rubik::Context::Scanner may not be loaded
41
- # at require time (tools load before context in some entry points).
38
+ # Keeps the rg-side and Ruby-side file universes identical: the
39
+ # repo's .gitignore may not prune what Rubik prunes. Lazily built
40
+ # (Scanner may be unloaded at require time). DEFAULT_IGNORES needs
41
+ # both glob forms — a middle-slash rg glob is root-anchored while
42
+ # the Ruby side prunes at any depth; HARD_PRUNE_ROOTS is
43
+ # root-anchored on both sides.
42
44
  def prune_globs
43
- @prune_globs ||= Rubik::Context::Scanner::DEFAULT_IGNORES.flat_map { |p| ["-g", "!#{p}"] }.freeze
45
+ @prune_globs ||= (
46
+ Rubik::Context::Scanner::DEFAULT_IGNORES.flat_map do |p|
47
+ globs = ["-g", "!#{p}"]
48
+ globs += ["-g", "!**/#{p}"] unless p.start_with?("**/")
49
+ globs
50
+ end +
51
+ Rubik::Context::Scanner::HARD_PRUNE_ROOTS.flat_map { |r| ["-g", "!/#{r}/**"] }
52
+ ).freeze
44
53
  end
45
54
 
46
55
  # Resolved rg executable, or nil. RUBIK_RG_PATH beats PATH lookup
data/lib/rubik/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rubik
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubik
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Özgür Ulukır
@@ -296,6 +296,7 @@ files:
296
296
  - lib/rubik/context/git_lister.rb
297
297
  - lib/rubik/context/index.rb
298
298
  - lib/rubik/context/repo_context_reader.rb
299
+ - lib/rubik/context/root_heuristics.rb
299
300
  - lib/rubik/context/scanner.rb
300
301
  - lib/rubik/diagnostics.rb
301
302
  - lib/rubik/diagnostics/collector.rb
@@ -404,6 +405,7 @@ files:
404
405
  - lib/rubik/orchestrator/tool_call_id.rb
405
406
  - lib/rubik/orchestrator/tool_circuit_breaker.rb
406
407
  - lib/rubik/orchestrator/tool_history.rb
408
+ - lib/rubik/orchestrator/tool_timeout.rb
407
409
  - lib/rubik/orchestrator/turn_executor.rb
408
410
  - lib/rubik/orchestrator/turn_executor/subagent_channel.rb
409
411
  - lib/rubik/orchestrator/turn_executor/user_channel.rb
@@ -756,6 +758,7 @@ files:
756
758
  - lib/rubik/tools/declaration.rb
757
759
  - lib/rubik/tools/diagnostic_reporter.rb
758
760
  - lib/rubik/tools/fuzzy_matcher.rb
761
+ - lib/rubik/tools/index_messages.rb
759
762
  - lib/rubik/tools/native_search.rb
760
763
  - lib/rubik/tools/output.rb
761
764
  - lib/rubik/tools/preflight_runner.rb