rubik 0.12.0 → 0.14.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 (107) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +71 -1
  4. data/bin/rubik +11 -1
  5. data/lib/rubik/ast/c_extractor.rb +1 -1
  6. data/lib/rubik/ast/graph.rb +38 -57
  7. data/lib/rubik/ast/graph_extensions.rb +57 -15
  8. data/lib/rubik/ast/ruby_extractor.rb +4 -0
  9. data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
  10. data/lib/rubik/buffer/manager.rb +33 -11
  11. data/lib/rubik/buffer/snapshot.rb +23 -3
  12. data/lib/rubik/cli/connect.rb +49 -0
  13. data/lib/rubik/config/defaults.rb +4 -7
  14. data/lib/rubik/config/env_guard.rb +43 -0
  15. data/lib/rubik/config/schema.rb +8 -1
  16. data/lib/rubik/config/secret_policy.rb +6 -5
  17. data/lib/rubik/config.rb +1 -1
  18. data/lib/rubik/connect/http.rb +85 -0
  19. data/lib/rubik/connect/models_dev.rb +173 -0
  20. data/lib/rubik/connect/probes.rb +103 -0
  21. data/lib/rubik/connect/prompt_ui.rb +139 -0
  22. data/lib/rubik/connect/wizard.rb +540 -0
  23. data/lib/rubik/connect.rb +7 -0
  24. data/lib/rubik/context/repo_context_reader.rb +8 -4
  25. data/lib/rubik/llm/chunk_assembler.rb +12 -1
  26. data/lib/rubik/llm/formatter.rb +14 -0
  27. data/lib/rubik/llm/openai_compatible.rb +4 -1
  28. data/lib/rubik/llm/provider_key.rb +32 -0
  29. data/lib/rubik/llm/registry.rb +18 -2
  30. data/lib/rubik/llm/router.rb +19 -1
  31. data/lib/rubik/llm/session_header.rb +128 -0
  32. data/lib/rubik/llm/sse_parser.rb +57 -10
  33. data/lib/rubik/lsp/client.rb +11 -5
  34. data/lib/rubik/lsp/transport.rb +17 -7
  35. data/lib/rubik/mcp/transport/stdio.rb +42 -4
  36. data/lib/rubik/orchestrator/continuation_manager.rb +14 -5
  37. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
  38. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
  39. data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
  40. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
  41. data/lib/rubik/orchestrator/goal_session.rb +4 -4
  42. data/lib/rubik/orchestrator/loop.rb +8 -11
  43. data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
  44. data/lib/rubik/orchestrator/step_executor.rb +17 -2
  45. data/lib/rubik/orchestrator/subagent/instance.rb +24 -1
  46. data/lib/rubik/orchestrator/subagent/notices.rb +1 -1
  47. data/lib/rubik/orchestrator/subagent/reaper.rb +21 -2
  48. data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
  49. data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
  50. data/lib/rubik/orchestrator/subagent/supervisor.rb +10 -3
  51. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +27 -5
  52. data/lib/rubik/orchestrator/turn_executor.rb +24 -8
  53. data/lib/rubik/platform/filesystem.rb +52 -24
  54. data/lib/rubik/plugin/storage.rb +20 -0
  55. data/lib/rubik/prompt/context_builder.rb +2 -1
  56. data/lib/rubik/repair/loop.rb +22 -2
  57. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  58. data/lib/rubik/repl/commands/agent_execution.rb +20 -15
  59. data/lib/rubik/repl/commands/buffer.rb +15 -0
  60. data/lib/rubik/repl/commands/connect.rb +51 -0
  61. data/lib/rubik/repl/commands/lsp.rb +1 -1
  62. data/lib/rubik/repl/commands/plugins.rb +31 -31
  63. data/lib/rubik/repl/commands/session.rb +5 -5
  64. data/lib/rubik/repl/commands/workspace.rb +4 -4
  65. data/lib/rubik/repl/commands.rb +2 -0
  66. data/lib/rubik/repl/input_execution.rb +30 -29
  67. data/lib/rubik/repl/input_loop.rb +10 -4
  68. data/lib/rubik/repl/lifecycle.rb +38 -0
  69. data/lib/rubik/repl/notice_router.rb +11 -5
  70. data/lib/rubik/repl/paste_support.rb +1 -1
  71. data/lib/rubik/repl/prompt_grace.rb +3 -3
  72. data/lib/rubik/repl/shell.rb +10 -2
  73. data/lib/rubik/repl/turn_recorder.rb +14 -3
  74. data/lib/rubik/runtime/context.rb +33 -27
  75. data/lib/rubik/runtime/manager.rb +23 -15
  76. data/lib/rubik/sandbox/driver/bubblewrap.rb +58 -8
  77. data/lib/rubik/sandbox/driver/process.rb +32 -1
  78. data/lib/rubik/sandbox/runner.rb +9 -1
  79. data/lib/rubik/security/classifier/classifier.rb +147 -13
  80. data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
  81. data/lib/rubik/security/classifier/commands/git.yaml +27 -1
  82. data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
  83. data/lib/rubik/security/classifier/database.rb +2 -2
  84. data/lib/rubik/security/command_gate.rb +8 -1
  85. data/lib/rubik/security/redactor.rb +62 -19
  86. data/lib/rubik/session/recorder.rb +13 -18
  87. data/lib/rubik/session/replayer.rb +29 -7
  88. data/lib/rubik/storage/store.rb +19 -5
  89. data/lib/rubik/tools/builtins/exec.rb +1 -1
  90. data/lib/rubik/tools/builtins/files.rb +8 -12
  91. data/lib/rubik/tools/builtins/grep.rb +3 -2
  92. data/lib/rubik/tools/builtins/lsp.rb +31 -7
  93. data/lib/rubik/tools/builtins/show_file.rb +1 -1
  94. data/lib/rubik/tools/builtins/synthesis.rb +4 -4
  95. data/lib/rubik/tools/result_normalizer.rb +1 -1
  96. data/lib/rubik/ui/banner.rb +3 -3
  97. data/lib/rubik/ui/dialog.rb +10 -7
  98. data/lib/rubik/ui/diff_viewer.rb +4 -4
  99. data/lib/rubik/ui/frame.rb +13 -3
  100. data/lib/rubik/ui/markdown_stream.rb +8 -2
  101. data/lib/rubik/ui/panel.rb +11 -11
  102. data/lib/rubik/ui/renderer.rb +6 -6
  103. data/lib/rubik/ui/spinner.rb +1 -1
  104. data/lib/rubik/ui/statusline.rb +3 -3
  105. data/lib/rubik/ui/theme.rb +1 -1
  106. data/lib/rubik/version.rb +1 -1
  107. metadata +12 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f668c26bd4967d506b511a23d21f1a9afe91cc0c94b6d007b39e4dd1d4102e7e
4
- data.tar.gz: b4d12df2cc31b33e86a97c5a1a2f050f42bbc612169eb54d36a96d238f9b5445
3
+ metadata.gz: a649ff3579ecb32b3d69eae6e53f48a8eb44f9c54a44d847860ed1889284a265
4
+ data.tar.gz: a2a9c1a7ab5ed4d5d769c513fa904ebdbc285fef8f00b1fc9e6dd14c127dbf27
5
5
  SHA512:
6
- metadata.gz: 4072b8c92a9f9990227c1b72996a48942fd1008a849bf9e6d6ff74c80f1448dbd10d5409158e85e1c7c5ae54463f69b0b9a68b07a542a87cb51a837e8bee4751
7
- data.tar.gz: 5a9679b4f592d999e9602af4758327347f7bf5a84eee8e0b70bb5591830eab73a65dfcc1cf7bb78ae171b2e4b32bea9ef52c8ce6f704d9806db9d440e5cef9fc
6
+ metadata.gz: e4679732a909f8bb62ecc0f3b43e3489c86220b717e501896355054235b217f6b4148e70795715a3dcb6eb9b53323ca9ec3d29bbc9d0fad524092d28cd157352
7
+ data.tar.gz: 95f47090b296dbf632c3a941ef8e34ce2cc849ca7a0c95e3b24751e1087a16a5dd1fbd3cef1d5dc8c6e492c0461d4f2a349b6fdfbeebe6c69c3186608a734e20
data/ATTRIBUTION.md CHANGED
@@ -121,7 +121,7 @@ SOFTWARE.
121
121
  | **`strings-ansi`** | Piotr Murach | MIT | ANSI-aware string truncation and alignment for REPL formatters. |
122
122
  | **`reline`** | Hiroshi SHIBATA & Ruby Team | Ruby License / BSD-2-Clause | Line-editing, history navigation, autocompletion hooks, and headless terminal adaptation. |
123
123
  | **`prism`** | Kevin Newton & Ruby Core Team | MIT | High-performance Ruby parsing and AST extraction ([`Rubik::AST::RubyExtractor`](lib/rubik/ast/ruby_extractor.rb)). |
124
- | **`minitest`** | Ryan Davis & seattlerb | MIT | Fast, modular test suite powering Rubik's 4,600+ unit, integration, and contract tests. |
124
+ | **`minitest`** | Ryan Davis & seattlerb | MIT | Fast, modular test suite powering Rubik's 4,900+ unit, integration, and contract tests. |
125
125
  | **`rubocop`** | Bozhidar Batsov & contributors | MIT | Static code analysis, style enforcement, and architectural hygiene gates. |
126
126
  | **`benchmark`, `logger`, `pstore`, `reline`, `fiddle`** | Ruby core / rubygems teams | Ruby License / BSD-2-Clause | Ruby stdlib gems pinned explicitly in `rubik.gemspec` as they are unbundled from the default set in Ruby 3.4/3.5. |
127
127
 
data/CHANGELOG.md CHANGED
@@ -6,6 +6,74 @@ 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
+ ## [Unreleased]
10
+
11
+ ## [0.14.0] - 2026-11-19
12
+
13
+ The session-attribution minor: gateway plumbing that needed a stable
14
+ per-conversation identity arrives as a provider-scoped `session_header`
15
+ knob, subagent lanes learn provider-scoped retry knobs, the UI goes
16
+ English — and a large fix wave hardens sandboxing, the security
17
+ classifier, buffers, the AST graph, transports and the goal ledger.
18
+
19
+ ### Added
20
+
21
+ - **Dynamic session header (`providers.<name>.session_header`):** gateways
22
+ that require per-conversation session attribution — opencode's Go gateway
23
+ hard-400s every request with `MissingSessionID` without
24
+ `x-opencode-session` — are now first-class. Setting the knob on a
25
+ provider spec makes every LLM request (root loop, subagents, and repair
26
+ when its caller forwards `session_key`) carry
27
+ `headers[<name>] = <UUID derived from the active session file>`, stable
28
+ per conversation across `!resume` and restarts. A same-named header set
29
+ explicitly in `providers.<name>.headers` or `llm.headers` wins —
30
+ explicit beats automatic.
31
+
32
+ ### Changed
33
+
34
+ - Subagent lanes now honor provider-scoped `providers.<name>.<retry-knob>`
35
+ overrides (`max_retries`, `max_429_retries`, backoff, wall clock, ...)
36
+ through the same `ProviderResolver` seam as the root loop — previously
37
+ only the global `llm.*` spelling reached them.
38
+ - User-facing REPL, CLI and UI messages now render in English. Bilingual
39
+ Turkish/English strings keep only the English half; `Vakanüvis` domain
40
+ vocabulary, Turkish test fixtures and the generated `TOOLS.md` are
41
+ intentionally unchanged. Turkish resume phrases (`devam et`) still work.
42
+
43
+ ## [0.13.0] - 2026-11-17
44
+
45
+ The connect-wizard minor: provider binding collapses into an interactive
46
+ `!connect` / `rubik connect` flow that selects the provider from the
47
+ models.dev catalog, validates the key live, and routes the secret through
48
+ the existing SecretPolicy — while a review-and-CodeQL round keeps the
49
+ wizard's URL and key-normalization parsing linear-time and enriches its
50
+ model menus from catalog metadata.
51
+
52
+ ### Added
53
+
54
+ - **Connect wizard (`!connect` / `rubik connect`):** an interactive provider
55
+ setup wizard that collapses provider binding into a few answers — provider
56
+ selection (pinned favorites plus the full models.dev catalog behind a
57
+ type-to-filter submenu), a catalog-prefilled endpoint, a masked API key
58
+ with live validation (auth failures re-ask; unreachable endpoints offer
59
+ save-anyway), model selection from the provider's live list or the catalog
60
+ (context limits written from catalog metadata), and role binding
61
+ (default/fast/advanced). Nothing is written before the confirm step; the
62
+ commit goes through the existing Config verbs so the key lands in the OS
63
+ secret store and global scope is the default. The wizard is offered once on
64
+ first run when boot resolves no LLM client (`connect.first_run_offer`
65
+ knob), and the catalog cache (24h TTL, stale-while-error) lives under
66
+ `~/.rubik/cache/`.
67
+
68
+ ### Fixed
69
+
70
+ - **Wizard review hardening:** the provider-key trimming is linear-time
71
+ (`delete_prefix`/`delete_suffix`, no anchored-alternation regex) to keep
72
+ CodeQL's polynomial-ReDoS check clean; hostless http/https URLs are
73
+ rejected; live model listings are enriched with models.dev catalog
74
+ metadata (context limit, tool-call support); and success notes print even
75
+ when the flow stores no secret (Ollama / env-var / skipped-key).
76
+
9
77
  ## [0.12.0] - 2026-11-17
10
78
 
11
79
  The retry-and-resume minor: the LLM transport's hardcoded rate-limit and 5xx
@@ -1784,7 +1852,9 @@ installation section.
1784
1852
  - Context: serialized index mutations, snapshot-outside-lock reconcile,
1785
1853
  streaming-first truncation.
1786
1854
 
1787
- [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.12.0...HEAD
1855
+ [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.14.0...HEAD
1856
+ [0.14.0]: https://github.com/ozgurulukir/rubik/compare/v0.13.0...v0.14.0
1857
+ [0.13.0]: https://github.com/ozgurulukir/rubik/compare/v0.12.0...v0.13.0
1788
1858
  [0.12.0]: https://github.com/ozgurulukir/rubik/compare/v0.11.0...v0.12.0
1789
1859
  [0.11.0]: https://github.com/ozgurulukir/rubik/compare/v0.10.0...v0.11.0
1790
1860
  [0.10.0]: https://github.com/ozgurulukir/rubik/compare/v0.9.1...v0.10.0
data/bin/rubik CHANGED
@@ -18,8 +18,18 @@ trap("INT") do
18
18
  end
19
19
 
20
20
  options = {}
21
+
22
+ # `rubik connect`: standalone provider setup wizard — runs and exits before
23
+ # the trust gate and Shell boot (same run-and-exit shape as --list-sessions);
24
+ # the wizard writes global-scope config only, --project stays an explicit flag.
25
+ if ARGV.first == "connect"
26
+ ARGV.shift
27
+ require "rubik/cli/connect"
28
+ exit(Rubik::CLI::Connect.run(argv: ARGV))
29
+ end
30
+
21
31
  OptionParser.new do |opts|
22
- opts.banner = "Usage: rubik [options]"
32
+ opts.banner = "Usage: rubik [options] | rubik connect"
23
33
 
24
34
  opts.on("-v", "--version", "Show version") do
25
35
  puts "rubik version #{Rubik::VERSION}"
@@ -40,7 +40,7 @@ module Rubik
40
40
  signature: stripped
41
41
  )
42
42
  elsif (match = stripped.match(/^(?:typedef\s+)?(struct|union|enum)\s+([A-Za-z0-9_]+)?\s*\{?/))
43
- name = match[2] || "anonymous_#{kind}_#{line_num}"
43
+ name = match[2] || "anonymous_#{match[1]}_#{line_num}"
44
44
  end_line = stripped.include?(";") && !stripped.include?("{") ? line_num : find_closing_brace(i)
45
45
  symbols << Symbol.new(
46
46
  name: name,
@@ -144,84 +144,65 @@ module Rubik
144
144
  end
145
145
 
146
146
  def smart_context_for(symbol_name)
147
- @mutex.synchronize do
147
+ target, dependencies = @mutex.synchronize do
148
148
  candidates = _symbol_candidate_pairs_unlocked(symbol_name)
149
- return "" unless candidates.length == 1
149
+ next nil unless candidates.length == 1
150
150
 
151
151
  target = candidates.first.last
152
- context_blocks = []
153
- context_blocks << "# Target symbol: #{target.name} (#{target.file}:#{target.line_start}-#{target.line_end})"
154
- context_blocks << target.body if target.body
155
-
152
+ target_scope = _symbol_scope(target.name)
153
+ deps = []
156
154
  target.calls.each do |called_name|
157
155
  dep_candidates = _symbol_candidate_pairs_unlocked(called_name).map(&:last)
158
- target_scope = _symbol_scope(target.name)
159
156
  matching = dep_candidates.find { |s| s.file && target.file && Rubik::Platform.path_equal?(s.file, target.file) && _symbol_scope(s.name) == target_scope } ||
160
157
  dep_candidates.find { |s| s.file && target.file && Rubik::Platform.path_equal?(s.file, target.file) } ||
161
158
  (dep_candidates.first if dep_candidates.length == 1)
162
- if matching
163
- context_blocks << "# Dependency signature: #{matching.name}"
164
- context_blocks << matching.signature
165
- end
159
+ deps << matching if matching
166
160
  end
167
-
168
- context_blocks.join("\n\n")
161
+ [target, deps]
162
+ end
163
+ return "" unless target
164
+
165
+ # Symbol#body can fall back to File.readlines; keep that disk I/O
166
+ # outside the graph lock so it cannot serialize concurrent readers.
167
+ body = target.body
168
+ context_blocks = []
169
+ context_blocks << "# Target symbol: #{target.name} (#{target.file}:#{target.line_start}-#{target.line_end})"
170
+ context_blocks << body if body
171
+
172
+ dependencies.each do |matching|
173
+ context_blocks << "# Dependency signature: #{matching.name}"
174
+ context_blocks << matching.signature
169
175
  end
176
+
177
+ context_blocks.join("\n\n")
170
178
  end
171
179
 
172
180
  def outline_for(file_path)
173
- @mutex.synchronize do
174
- syms = _get_file_symbols_unlocked(file_path)
175
- syms = _index_file_unlocked(file_path) if syms.empty? && File.exist?(file_path)
181
+ # Snapshot under the lock, but do the parse/source read outside it:
182
+ # the extractor reads from disk and is the slowest path through here.
183
+ syms = @mutex.synchronize { _get_file_symbols_unlocked(file_path).dup }
184
+ if syms.empty?
185
+ return "No symbols found in '#{file_path}'." unless File.exist?(file_path)
186
+
187
+ result = Extractor.extract_result(file_path, nil)
188
+ # Re-acquire only to merge the parse into the in-memory maps.
189
+ syms = @mutex.synchronize { _merge_extraction_result_unlocked(file_path, result).dup }
190
+ end
176
191
 
177
- return "No symbols found in '#{file_path}'." if syms.empty?
192
+ return "No symbols found in '#{file_path}'." if syms.empty?
178
193
 
179
- lines = ["=== Outline for #{file_path} (#{syms.size} symbols) ==="]
180
- syms.sort_by(&:line_start).each do |sym|
181
- indent = [:class, :struct, :module, :interface, :type].include?(sym.kind) ? "" : " "
182
- lines << "#{indent}• [#{sym.kind.to_s.upcase}] #{sym.name} (lines #{sym.line_start}-#{sym.line_end}) -> #{sym.signature}"
183
- end
184
- lines.join("\n")
194
+ lines = ["=== Outline for #{file_path} (#{syms.size} symbols) ==="]
195
+ syms.sort_by(&:line_start).each do |sym|
196
+ indent = [:class, :struct, :module, :interface, :type].include?(sym.kind) ? "" : " "
197
+ lines << "#{indent}• [#{sym.kind.to_s.upcase}] #{sym.name} (lines #{sym.line_start}-#{sym.line_end}) -> #{sym.signature}"
185
198
  end
199
+ lines.join("\n")
186
200
  end
187
201
 
188
202
  private
189
203
 
190
204
  def _index_file_unlocked(file_path, content = nil)
191
- result = Extractor.extract_result(file_path, content)
192
- @extraction_status[file_path] = result.status
193
-
194
- # A failed extraction must not silently DELETE the file's previously
195
- # indexed symbols (issue #147): stale-but-known beats unknown, and
196
- # the log line makes the downgrade visible. The best-effort symbols
197
- # are only indexed when there is nothing to protect.
198
- if result.status == :failed
199
- existing = _get_file_symbols_unlocked(file_path)
200
- unless existing.empty?
201
- Rubik::Log.warn(:ast, "extraction failed for #{file_path} (#{result.detail}); keeping previously indexed symbols")
202
- return existing
203
- end
204
- end
205
-
206
- _remove_file_unlocked(file_path)
207
-
208
- ext = File.extname(file_path).downcase
209
- lang = CacheStore::LANGUAGE_MAP[ext] || "code"
210
-
211
- extracted = result.symbols
212
- if result.status == :failed
213
- # Truthful accounting: the fallback symbols ARE indexed when
214
- # nothing pre-existing needed protection — even when there are
215
- # none, the log must not claim otherwise.
216
- Rubik::Log.warn(:ast, "extraction failed for #{file_path} (#{result.detail}); indexing #{extracted.length} best-effort fallback symbols")
217
- end
218
- extracted.each do |sym|
219
- @symbols[sym.name] = sym
220
- q_key = "#{lang}:#{file_path}##{sym.name}"
221
- @qualified_symbols[q_key] = sym
222
- @file_symbols[file_path] << sym
223
- end
224
- extracted
205
+ _merge_extraction_result_unlocked(file_path, Extractor.extract_result(file_path, content))
225
206
  end
226
207
 
227
208
  def _remove_file_unlocked(file_path, clear_status: false)
@@ -191,27 +191,69 @@ module Rubik
191
191
  end
192
192
 
193
193
  def symbol_definition(symbol_name)
194
- @mutex.synchronize do
194
+ target = @mutex.synchronize do
195
195
  candidates = _symbol_candidate_pairs_unlocked(symbol_name)
196
- return nil unless candidates.length == 1
197
-
198
- target = candidates.first.last
199
- {
200
- name: target.name,
201
- kind: target.kind,
202
- file: target.file,
203
- line_start: target.line_start,
204
- line_end: target.line_end,
205
- signature: target.signature,
206
- body: target.body,
207
- docstring: target.docstring,
208
- calls: target.calls
209
- }
196
+ next nil unless candidates.length == 1
197
+
198
+ candidates.first.last
210
199
  end
200
+ return nil unless target
201
+
202
+ # Symbol#body can fall back to File.readlines; read it outside the
203
+ # graph lock so it cannot serialize concurrent readers (issue #560).
204
+ {
205
+ name: target.name,
206
+ kind: target.kind,
207
+ file: target.file,
208
+ line_start: target.line_start,
209
+ line_end: target.line_end,
210
+ signature: target.signature,
211
+ body: target.body,
212
+ docstring: target.docstring,
213
+ calls: target.calls
214
+ }
211
215
  end
212
216
 
213
217
  private
214
218
 
219
+ # Caller must hold @mutex. outline_for does the (disk-reading) extraction
220
+ # outside the lock and only merges the finished result through here.
221
+ def _merge_extraction_result_unlocked(file_path, result)
222
+ @extraction_status[file_path] = result.status
223
+
224
+ # A failed extraction must not silently DELETE the file's previously
225
+ # indexed symbols (issue #147): stale-but-known beats unknown, and
226
+ # the log line makes the downgrade visible. The best-effort symbols
227
+ # are only indexed when there is nothing to protect.
228
+ if result.status == :failed
229
+ existing = _get_file_symbols_unlocked(file_path)
230
+ unless existing.empty?
231
+ Rubik::Log.warn(:ast, "extraction failed for #{file_path} (#{result.detail}); keeping previously indexed symbols")
232
+ return existing
233
+ end
234
+ end
235
+
236
+ _remove_file_unlocked(file_path)
237
+
238
+ ext = File.extname(file_path).downcase
239
+ lang = CacheStore::LANGUAGE_MAP[ext] || "code"
240
+
241
+ extracted = result.symbols
242
+ if result.status == :failed
243
+ # Truthful accounting: the fallback symbols ARE indexed when
244
+ # nothing pre-existing needed protection — even when there are
245
+ # none, the log must not claim otherwise.
246
+ Rubik::Log.warn(:ast, "extraction failed for #{file_path} (#{result.detail}); indexing #{extracted.length} best-effort fallback symbols")
247
+ end
248
+ extracted.each do |sym|
249
+ @symbols[sym.name] = sym
250
+ q_key = "#{lang}:#{file_path}##{sym.name}"
251
+ @qualified_symbols[q_key] = sym
252
+ @file_symbols[file_path] << sym
253
+ end
254
+ extracted
255
+ end
256
+
215
257
  def _search_rank(name, query)
216
258
  return 0 if name == query
217
259
  return 1 if name.start_with?(query)
@@ -121,6 +121,10 @@ module Rubik
121
121
  body_loader: body_loader_for(line_start, line_end)
122
122
  )
123
123
 
124
+ # Nested defs/classes live under the DefNode; without this walk the
125
+ # Prism path drops symbols the fallback scanner still finds.
126
+ node.compact_child_nodes.each { |child| walk_node(child, current_scope, symbols) }
127
+
124
128
  when Prism::CallNode
125
129
  name = node.name.to_s
126
130
  # PERF: Fast Set lookup avoids Array allocation on every CallNode
@@ -29,6 +29,13 @@ module Rubik
29
29
  "union_specifier" => :union
30
30
  }.freeze
31
31
 
32
+ # A typedef's kind follows its underlying type, not a hardcoded :struct.
33
+ TYPEDEF_KINDS = {
34
+ "struct_specifier" => :struct,
35
+ "union_specifier" => :union,
36
+ "enum_specifier" => :enum
37
+ }.freeze
38
+
32
39
  DECLARATOR_WRAPPERS = [
33
40
  "function_declarator", "pointer_declarator", "reference_declarator", "parenthesized_declarator"
34
41
  ].freeze
@@ -87,22 +94,23 @@ module Rubik
87
94
  end
88
95
 
89
96
  def walk_typedef(node, symbols, scope)
97
+ type_node = node.child_by_field_name("type")
90
98
  declarator = node.child_by_field_name("declarator")
91
99
  name = if declarator
92
100
  text_for(declarator).strip
93
101
  else
94
- type_node = node.child_by_field_name("type")
95
102
  text_for(type_node&.child_by_field_name("name")).strip
96
103
  end
97
104
  return if name.empty?
98
105
 
106
+ kind = TYPEDEF_KINDS.fetch(type_node&.kind, :type)
99
107
  qualified = qualify(scope, name)
100
108
  sig = clean_signature(node)
101
109
  symbols << build_symbol(
102
110
  name: qualified,
103
- kind: :struct,
111
+ kind: kind,
104
112
  node: node,
105
- signature: sig.empty? ? "typedef struct #{qualified}" : sig
113
+ signature: sig.empty? ? "typedef #{kind} #{qualified}" : sig
106
114
  )
107
115
  end
108
116
 
@@ -38,7 +38,7 @@ module Rubik
38
38
  class Manager
39
39
  attr_reader :root_dir, :dirty_files, :additional_roots, :rw_roots, :allowed_external_dirs, :rw_allowed_dirs,
40
40
  :guard, :last_conflicts, :last_skipped
41
- attr_accessor :allow_external_writes, :config
41
+ attr_accessor :allow_external_writes, :config, :snapshot_store
42
42
 
43
43
  def initialize(root_dir: Dir.pwd, additional_roots: [], allowed_external_dirs: [], allow_external_writes: false,
44
44
  event_bus: nil, guard: nil, config: nil)
@@ -170,7 +170,11 @@ module Rubik
170
170
  rel_or_exp
171
171
  end
172
172
 
173
- # Reads a file from the buffer (if dirty) or from disk, normalizing newlines.
173
+ # Reads a file from the buffer (if dirty) or from disk. Raw bytes —
174
+ # including CRLF line endings — are preserved end-to-end: a read/edit/
175
+ # write cycle on a CRLF file must not rewrite every line ending to LF
176
+ # (issue #544). Newline normalization is confined to conflict-detection
177
+ # hashing, never to the content that is buffered or applied to disk.
174
178
  #
175
179
  # @param path [String] file path relative to the root directory
176
180
  # @param reload [Boolean] force reload from disk, discarding in-memory buffer if present
@@ -190,7 +194,7 @@ module Rubik
190
194
  full_path = File.expand_path(rel_path, @root_dir)
191
195
  return nil unless File.exist?(full_path)
192
196
 
193
- return Rubik::Platform.normalize_newlines(Rubik::Platform.read_text(full_path))
197
+ return read_bytes_as_utf8(full_path)
194
198
  elsif @buffer.key?(rel_path)
195
199
  return @buffer[rel_path]
196
200
  end
@@ -198,7 +202,7 @@ module Rubik
198
202
  full_path = File.expand_path(rel_path, @root_dir)
199
203
  return nil unless File.exist?(full_path)
200
204
 
201
- content = Rubik::Platform.normalize_newlines(Rubik::Platform.read_text(full_path))
205
+ content = read_bytes_as_utf8(full_path)
202
206
  @original_hashes[rel_path] = Digest::SHA256.hexdigest(content)
203
207
  content
204
208
  end
@@ -219,17 +223,25 @@ module Rubik
219
223
  def write(path, content, reset_baseline: false)
220
224
  @monitor.synchronize do
221
225
  rel_path = sanitize_path(path, for_write: true)
222
- normalized_content = Rubik::Platform.normalize_newlines(content)
223
-
224
226
  if reset_baseline || !@original_hashes.key?(rel_path)
225
227
  full_path = File.expand_path(rel_path, @root_dir)
226
- @original_hashes[rel_path] = (Digest::SHA256.hexdigest(Rubik::Platform.normalize_newlines(Rubik::Platform.read_text(full_path))) if File.exist?(full_path))
228
+ @original_hashes[rel_path] = (Digest::SHA256.hexdigest(read_bytes_as_utf8(full_path)) if File.exist?(full_path))
227
229
  end
228
230
 
229
- @buffer[rel_path] = normalized_content
231
+ @buffer[rel_path] = content
230
232
  @dirty_files[rel_path] = true
231
233
  @event_bus&.emit(:buffer_changed, path: rel_path)
232
- normalized_content
234
+
235
+ # Give the advertised !undo / !rollback commands a producer in
236
+ # the live write path (issue #544). Snapshotting is auxiliary:
237
+ # a storage failure must not turn a successful edit into a
238
+ # failed write.
239
+ begin
240
+ @snapshot_store&.create_snapshot(self)
241
+ rescue StandardError => e
242
+ warn "[Buffer] snapshot recording failed: #{e.class}: #{e.message}"
243
+ end
244
+ content
233
245
  end
234
246
  end
235
247
 
@@ -309,7 +321,7 @@ module Rubik
309
321
 
310
322
  diffs = snapshot.map do |rel_path, new_content|
311
323
  full_path = File.expand_path(rel_path, @root_dir)
312
- orig_content = File.exist?(full_path) ? Rubik::Platform.normalize_newlines(Rubik::Platform.read_text(full_path)) : ""
324
+ orig_content = File.exist?(full_path) ? read_bytes_as_utf8(full_path) : ""
313
325
  Diff.unified_diff(rel_path, orig_content, new_content)
314
326
  end
315
327
 
@@ -420,10 +432,20 @@ module Rubik
420
432
  full_path = File.expand_path(rel_path, @root_dir)
421
433
  return nil unless File.exist?(full_path)
422
434
 
423
- Digest::SHA256.hexdigest(Rubik::Platform.normalize_newlines(Rubik::Platform.read_text(full_path)))
435
+ Digest::SHA256.hexdigest(read_bytes_as_utf8(full_path))
424
436
  rescue Errno::ENOENT, Errno::EACCES
425
437
  nil
426
438
  end
439
+
440
+ private
441
+
442
+ # Platform.read_text uses File.read (text mode), which translates
443
+ # CRLF to LF on Windows and would silently break the raw-byte
444
+ # newline contract (issue #544). File.binread keeps the bytes;
445
+ # safe_utf8 re-tags and scrubs exactly as read_text does.
446
+ def read_bytes_as_utf8(full_path)
447
+ Rubik::Platform.safe_utf8(File.binread(full_path))
448
+ end
427
449
  end
428
450
  end
429
451
  end
@@ -4,6 +4,7 @@ require "zlib"
4
4
  require "json"
5
5
  require "fileutils"
6
6
  require "securerandom"
7
+ require_relative "../platform"
7
8
 
8
9
  module Rubik
9
10
  module Buffer
@@ -45,13 +46,26 @@ module Rubik
45
46
  class SnapshotStore
46
47
  attr_reader :storage_dir, :snapshots
47
48
 
48
- def initialize(storage_dir: File.join(ENV.fetch("HOME", nil), ".rubik", "snapshots", "default"))
49
+ def initialize(storage_dir: File.join(Rubik::Platform.home_dir, ".rubik", "snapshots", "default"))
49
50
  @storage_dir = storage_dir
50
51
  @snapshots = []
51
52
  @current_index = -1
53
+ # The state undo-to-beginning restores. Defaults to a clean
54
+ # buffer; consumers that snapshot a pre-existing dirty buffer
55
+ # (e.g. Repair::Loop) record that state explicitly so an aborted
56
+ # operation restores the pending edits instead of wiping them
57
+ # (issue #544).
58
+ @baseline = {}
52
59
  FileUtils.mkdir_p(@storage_dir)
53
60
  end
54
61
 
62
+ # Records the state that a full undo should restore. Call once,
63
+ # before the first snapshot, when the buffer already holds edits
64
+ # that must survive an aborted operation.
65
+ def record_baseline(buffer_manager)
66
+ @baseline = buffer_manager.snapshot_state
67
+ end
68
+
55
69
  def create_snapshot(buffer_manager)
56
70
  return nil unless buffer_manager.dirty?
57
71
 
@@ -63,7 +77,13 @@ module Rubik
63
77
  file_path = File.join(@storage_dir, "#{snapshot.id}.zdeflate")
64
78
  File.binwrite(file_path, snapshot.compress)
65
79
 
66
- @snapshots = @snapshots[0..@current_index] if @current_index < @snapshots.size - 1
80
+ # Clamp the redo truncation: with @current_index == -1 the range
81
+ # [0..-1] is the WHOLE array, so a fresh snapshot after undoing
82
+ # to empty would be appended after stale snapshots and a later
83
+ # undo would resurrect the discarded edit (issue #544).
84
+ if @current_index < @snapshots.size - 1
85
+ @snapshots = @current_index.negative? ? [] : @snapshots[0..@current_index]
86
+ end
67
87
 
68
88
  @snapshots << snapshot
69
89
  @current_index = @snapshots.size - 1
@@ -74,7 +94,7 @@ module Rubik
74
94
  return false if @current_index.negative?
75
95
 
76
96
  if @current_index.zero?
77
- buffer_manager.restore_snapshot_state({})
97
+ buffer_manager.restore_snapshot_state(@baseline)
78
98
  @current_index = -1
79
99
  return true
80
100
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../connect"
4
+
5
+ module Rubik
6
+ module CLI
7
+ # `rubik connect` — the standalone connect-wizard runner. Boots no
8
+ # Shell and no workspace trust gate (same run-and-exit shape as
9
+ # --list-sessions): the wizard writes global-scope config only, and a
10
+ # project-scope choice stays an explicit --project flag (plaintext
11
+ # project secrets are still refused by SecretPolicy).
12
+ module Connect
13
+ module_function
14
+
15
+ USAGE = "Usage: rubik connect [--project|--global]"
16
+
17
+ # -> exit code: 0 connected, 1 cancelled/failed, 2 non-interactive.
18
+ # prompt:/probe: are test seams (scripted prompt, stubbed probes).
19
+ def run(argv:, input: $stdin, output: $stdout, root_dir: Dir.pwd, home_dir: nil, prompt: nil, probe: nil)
20
+ if argv.intersect?(["--help", "-h"])
21
+ output.puts USAGE
22
+ return 0
23
+ end
24
+ if argv.intersect?(["--session", "-s"])
25
+ output.puts USAGE
26
+ output.puts " rubik connect writes persistent scopes only; use !provider add in the REPL for a session-scoped provider."
27
+ return 1
28
+ end
29
+
30
+ scope = argv.include?("--project") || argv.include?("-p") ? :project : :global
31
+ config = Rubik::Config.new(root_dir: root_dir, home_dir: home_dir)
32
+ ui = Rubik::Connect::PromptUI.new(input: input, output: output,
33
+ theme: Rubik::UI::Theme.for(output, config), prompt: prompt)
34
+ wizard = Rubik::Connect::Wizard.new(config: config, ui: ui, io: output, scope: scope,
35
+ probe: probe,
36
+ catalog_loader: -> { Rubik::Connect::ModelsDev.load(home_dir: config.home_dir) })
37
+ result = wizard.run
38
+
39
+ if result[:reason] == :non_interactive
40
+ output.puts "Non-interactive session — the wizard cannot run. Start the REPL with 'rubik' and use !connect, " \
41
+ "or apply the !provider add <name> [type] <url> [--key=...] syntax."
42
+ return 2
43
+ end
44
+ output.puts "Start the REPL with 'rubik'." if result[:ok]
45
+ result[:ok] ? 0 : 1
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../platform"
4
+ require_relative "env_guard"
4
5
 
5
6
  module Rubik
6
7
  class Config
7
8
  module Defaults
9
+ include EnvGuard
10
+
8
11
  # llm.* transport-resilience fallbacks, reused by both orchestrator seams.
9
12
  LLM_RETRY_DEFAULTS = { "max_retries" => 2, "max_429_retries" => 5, "backoff_base" => 0.2,
10
13
  "max_wall_clock" => 120, "rate_limit_max_delay" => 60 }.freeze
@@ -57,6 +60,7 @@ module Rubik
57
60
  "native" => "auto"
58
61
  },
59
62
  "router" => { "mode" => "auto", "sticky" => true },
63
+ "connect" => { "first_run_offer" => true }, # connect wizard first-run offer (repl/lifecycle.rb)
60
64
  "repair" => { "max_attempts" => 3 },
61
65
  "sandbox" => {
62
66
  "enabled" => false,
@@ -230,13 +234,6 @@ module Rubik
230
234
 
231
235
  private
232
236
 
233
- # Schema-backed keys must satisfy their spec wherever the merged tree is validated.
234
- def validate_typed_value!(key, value)
235
- return unless Schema::DEFINITIONS.key?(key.to_s)
236
-
237
- Schema.validate_value!(key, value, strict: true)
238
- end
239
-
240
237
  def validate_schema(config, schema = DEFAULT_CONFIG, prefix = "", strict: false)
241
238
  raise ArgumentError, "Configuration must be a Hash, got #{config.class}" unless config.is_a?(Hash)
242
239