rubik 0.13.0 → 0.15.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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +122 -1
  4. data/lib/rubik/adapter/node.rb +4 -1
  5. data/lib/rubik/ast/c_extractor.rb +1 -1
  6. data/lib/rubik/ast/fallback_cleaner.rb +6 -1
  7. data/lib/rubik/ast/graph.rb +38 -57
  8. data/lib/rubik/ast/graph_extensions.rb +57 -15
  9. data/lib/rubik/ast/ruby_extractor.rb +16 -6
  10. data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
  11. data/lib/rubik/buffer/manager.rb +38 -11
  12. data/lib/rubik/buffer/snapshot.rb +116 -28
  13. data/lib/rubik/cli/connect.rb +3 -5
  14. data/lib/rubik/config/defaults.rb +3 -7
  15. data/lib/rubik/config/env_guard.rb +43 -0
  16. data/lib/rubik/config/schema.rb +7 -1
  17. data/lib/rubik/config/scoped.rb +12 -5
  18. data/lib/rubik/config/secret_policy.rb +6 -5
  19. data/lib/rubik/config.rb +1 -1
  20. data/lib/rubik/connect/models_dev.rb +11 -0
  21. data/lib/rubik/connect/probes.rb +9 -2
  22. data/lib/rubik/connect/wizard.rb +117 -57
  23. data/lib/rubik/context/repo_context_reader.rb +8 -4
  24. data/lib/rubik/diagnostics/collector.rb +25 -2
  25. data/lib/rubik/llm/chunk_assembler.rb +12 -1
  26. data/lib/rubik/llm/client.rb +19 -0
  27. data/lib/rubik/llm/connection_pool.rb +7 -1
  28. data/lib/rubik/llm/formatter.rb +22 -2
  29. data/lib/rubik/llm/provider_key.rb +32 -0
  30. data/lib/rubik/llm/registry.rb +18 -2
  31. data/lib/rubik/llm/router.rb +27 -1
  32. data/lib/rubik/llm/session_header.rb +128 -0
  33. data/lib/rubik/llm/sse_parser.rb +57 -10
  34. data/lib/rubik/lsp/client.rb +244 -23
  35. data/lib/rubik/lsp/document_store.rb +6 -0
  36. data/lib/rubik/lsp/manager.rb +14 -0
  37. data/lib/rubik/lsp/transport.rb +34 -7
  38. data/lib/rubik/mcp/transport/stdio.rb +42 -4
  39. data/lib/rubik/orchestrator/continuation_manager.rb +68 -7
  40. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
  41. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
  42. data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
  43. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
  44. data/lib/rubik/orchestrator/goal_session.rb +14 -14
  45. data/lib/rubik/orchestrator/goal_wait.rb +15 -9
  46. data/lib/rubik/orchestrator/loop.rb +39 -39
  47. data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
  48. data/lib/rubik/orchestrator/step_executor.rb +17 -2
  49. data/lib/rubik/orchestrator/subagent/instance.rb +46 -5
  50. data/lib/rubik/orchestrator/subagent/lifecycle.rb +25 -25
  51. data/lib/rubik/orchestrator/subagent/messaging.rb +5 -1
  52. data/lib/rubik/orchestrator/subagent/narration.rb +2 -1
  53. data/lib/rubik/orchestrator/subagent/notices.rb +7 -3
  54. data/lib/rubik/orchestrator/subagent/questioner.rb +42 -4
  55. data/lib/rubik/orchestrator/subagent/reaper.rb +70 -20
  56. data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
  57. data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
  58. data/lib/rubik/orchestrator/subagent/supervisor.rb +21 -3
  59. data/lib/rubik/orchestrator/tool_call_id.rb +8 -4
  60. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +21 -5
  61. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +40 -5
  62. data/lib/rubik/orchestrator/turn_executor.rb +24 -8
  63. data/lib/rubik/platform/filesystem.rb +52 -24
  64. data/lib/rubik/platform/process.rb +32 -7
  65. data/lib/rubik/platform/sanitize.rb +17 -18
  66. data/lib/rubik/platform.rb +27 -0
  67. data/lib/rubik/plugin/storage.rb +34 -8
  68. data/lib/rubik/prompt/context_builder.rb +2 -1
  69. data/lib/rubik/prompt/doctrine.rb +3 -2
  70. data/lib/rubik/repair/loop.rb +22 -2
  71. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  72. data/lib/rubik/repl/commands/agent_execution.rb +20 -15
  73. data/lib/rubik/repl/commands/base.rb +7 -3
  74. data/lib/rubik/repl/commands/buffer.rb +21 -2
  75. data/lib/rubik/repl/commands/connect.rb +0 -1
  76. data/lib/rubik/repl/commands/lsp.rb +1 -1
  77. data/lib/rubik/repl/commands/plugins.rb +31 -31
  78. data/lib/rubik/repl/commands/session.rb +16 -6
  79. data/lib/rubik/repl/commands/workspace.rb +4 -4
  80. data/lib/rubik/repl/input_execution.rb +33 -32
  81. data/lib/rubik/repl/input_loop.rb +9 -4
  82. data/lib/rubik/repl/lifecycle.rb +1 -2
  83. data/lib/rubik/repl/notice_router.rb +11 -5
  84. data/lib/rubik/repl/paste_support.rb +1 -1
  85. data/lib/rubik/repl/prompt_grace.rb +3 -3
  86. data/lib/rubik/repl/service_catalog.rb +6 -2
  87. data/lib/rubik/repl/shell.rb +10 -2
  88. data/lib/rubik/repl/turn_recorder.rb +18 -4
  89. data/lib/rubik/runtime/context.rb +33 -27
  90. data/lib/rubik/runtime/manager.rb +23 -15
  91. data/lib/rubik/runtime/manifest_store.rb +7 -2
  92. data/lib/rubik/runtime/task_manager.rb +13 -10
  93. data/lib/rubik/sandbox/driver/base.rb +34 -0
  94. data/lib/rubik/sandbox/driver/bubblewrap.rb +60 -8
  95. data/lib/rubik/sandbox/driver/docker.rb +8 -0
  96. data/lib/rubik/sandbox/driver/podman.rb +8 -0
  97. data/lib/rubik/sandbox/driver/process.rb +29 -1
  98. data/lib/rubik/sandbox/runner.rb +9 -1
  99. data/lib/rubik/security/classifier/classifier.rb +147 -13
  100. data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
  101. data/lib/rubik/security/classifier/commands/git.yaml +27 -1
  102. data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
  103. data/lib/rubik/security/classifier/database.rb +2 -2
  104. data/lib/rubik/security/command_gate.rb +8 -1
  105. data/lib/rubik/security/path_policy.rb +28 -3
  106. data/lib/rubik/security/redactor.rb +62 -19
  107. data/lib/rubik/session/recorder.rb +27 -20
  108. data/lib/rubik/session/replayer.rb +29 -7
  109. data/lib/rubik/storage/store.rb +26 -5
  110. data/lib/rubik/tools/builtins/exec.rb +8 -2
  111. data/lib/rubik/tools/builtins/files.rb +8 -12
  112. data/lib/rubik/tools/builtins/grep.rb +3 -2
  113. data/lib/rubik/tools/builtins/interactive.rb +5 -2
  114. data/lib/rubik/tools/builtins/lsp.rb +59 -9
  115. data/lib/rubik/tools/builtins/send_message.rb +7 -2
  116. data/lib/rubik/tools/builtins/show_file.rb +1 -1
  117. data/lib/rubik/tools/builtins/synthesis.rb +4 -4
  118. data/lib/rubik/tools/preflight_runner.rb +4 -1
  119. data/lib/rubik/tools/result_normalizer.rb +1 -1
  120. data/lib/rubik/ui/banner.rb +3 -3
  121. data/lib/rubik/ui/dialog.rb +15 -8
  122. data/lib/rubik/ui/diff_viewer.rb +4 -4
  123. data/lib/rubik/ui/frame.rb +13 -3
  124. data/lib/rubik/ui/markdown_stream.rb +8 -2
  125. data/lib/rubik/ui/panel.rb +11 -11
  126. data/lib/rubik/ui/renderer.rb +10 -7
  127. data/lib/rubik/ui/spinner.rb +1 -1
  128. data/lib/rubik/ui/statusline.rb +3 -3
  129. data/lib/rubik/ui/theme.rb +1 -1
  130. data/lib/rubik/version.rb +1 -1
  131. metadata +4 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6a9e85889683534d644e9f88c1f076dce8e91e0503a2e40a823149d6d1ca359
4
- data.tar.gz: 925f5cfb990e06c0f7d1b9ac4911db249853bfde78e9902c20b104b114541f5e
3
+ metadata.gz: 9a391892bea3be5bbf6d2102c6800ed7c728c253e4f6f003a7765e6b727b51ef
4
+ data.tar.gz: cf65a2aa159ce1c0ce5765a1aeedda43467a46e918ac7bb4e921e812fbd4edf5
5
5
  SHA512:
6
- metadata.gz: 2b8c173c30baa1c4b9ad7b9cfdc9baabd98d8c8191569693a7baba5299d03448d0ed1f6f163e7b9d4ea77c9732fdb4f2ad47a8959304758f9f9cc0f4d6548df5
7
- data.tar.gz: 409d010dc7e497735871c2e0f84690675f10c0e00750137dea7c1cd6ee93a44c94c861b2d1345f8ac4a005e3801bf76506c2c7f0858681eb0e8f807d2a414292
6
+ metadata.gz: 44fc9d447ea49d37e7c6483a61b69755aa882acaf8b446fd660e6a60d973566be865675dd49f555a2795ce2d6f3e09ceb990da67c514a9f4aebff4b28bf86f14
7
+ data.tar.gz: 5c65c0f28f82e7e8feac9cc4b7f849192c48ed81a4a256f17db687d0eb780783593ba00bc25e21e4f3902d7e47e9bed4df56671508ff3e38bfd0a236980c7e16
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,700+ unit, integration, and contract tests. |
124
+ | **`minitest`** | Ryan Davis & seattlerb | MIT | Fast, modular test suite powering Rubik's 4,950+ 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
@@ -8,6 +8,125 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ## [0.15.0] - 2026-11-20
12
+
13
+ The diagnostics-and-hardening minor: the LSP client learns pull diagnostics
14
+ and spec-compliant incremental sync, the connect wizard's opencode (Go)
15
+ preset becomes zero-prompt — and a broad fix wave closes credential leaks,
16
+ stream-pool poisoning, orchestrator lifecycle bugs, symlink and ANSI escape
17
+ bypasses, and a long tail of platform, buffer, and storage edge cases.
18
+
19
+ ### Added
20
+
21
+ - **LSP pull diagnostics + incremental sync (#583):** the client captures
22
+ `diagnosticProvider` at initialize and issues `textDocument/diagnostic`
23
+ requests for documents whose snapshot is pending, recording `full` results
24
+ through the same version-guarded path as push diagnostics (stale-version
25
+ responses discarded, `unchanged` short-circuited, in-flight pulls deduped
26
+ per document+version). Pull-only servers such as typeprof — the default
27
+ Ruby server — now surface diagnostics instead of reporting "pending"
28
+ forever. Under the negotiated Incremental sync kind, `didChange` sends one
29
+ full-old-document ranged edit computed in UTF-16 code units; Full sync
30
+ keeps the range-less form. `relatedDocuments` entries are now recorded
31
+ too (#585), and the fallback scanner tracks a scope stack so top-level
32
+ defs after a class body index at top level and nested modules yield
33
+ `M::C` prefixes (#600).
34
+
35
+ - **Zero-prompt session header for the opencode (Go) preset:** the connect
36
+ wizard's preset row pre-answers the session-header question — selecting
37
+ `opencode (Go)` applies `x-opencode-session` automatically (one notice
38
+ line instead of two prompts) and the confirm summary still names it.
39
+ The question remains for custom endpoints and catalog picks.
40
+
41
+ ### Changed
42
+
43
+ - Container sandbox drivers (Docker/Podman) now bind-mount each
44
+ `allowed_dirs` entry read-write like Bubblewrap, so commands touching
45
+ allowed external dirs behave identically across drivers (#591).
46
+ - Shared helpers deduplicated: `allowed_dir_mounts` hoisted into
47
+ `Sandbox::Driver::Base`, and process env sanitizing delegates to the single
48
+ `Rubik::Platform.credential_free_env` source (#608).
49
+
50
+ ### Fixed
51
+
52
+ - **Security & sandboxing (#586, #590, #592):** task children spawn with
53
+ credential-shaped host vars explicitly unset and task-log lines redacted
54
+ and byte-bounded at `RingBuffer#push`; shell-plane critical-path writes
55
+ re-resolve AST-derived targets with strict `resolved_path` so a committed
56
+ symlink cannot redirect a write into `.git/`; and ANSI/CSI/OSC escapes are
57
+ stripped from model-controlled tool args and dialog questions (both card
58
+ variants) before they reach the raw terminal.
59
+
60
+ - **LLM transport & adapters (#587, #588):** a streaming callback failure
61
+ now re-types as `StreamCallbackError` so a half-read pooled session is
62
+ discarded instead of poisoning the next request; streaming usage keys are
63
+ read with string spellings after `JSON.parse`; `router.mode fast/advanced`
64
+ map to their forced roles instead of silently behaving as auto; and
65
+ `.ts`/`.tsx` without `tsc`/`npx` returns unavailable instead of a false
66
+ syntax error from `node --check`.
67
+
68
+ - **Orchestrator lifecycle (#593, #594, #595):** subagent tool-call ids
69
+ include a per-run sequence so redeploys no longer re-emit ids the preserved
70
+ history already carries, and the finish tool writes its pairing result
71
+ row; `:budget_exhausted` units now count as dispatched work in the
72
+ goal-completion guard so goals are not declared achieved over unfinished
73
+ work; auto-continue absorbs prior partial rows on stitch; `continue_loop`
74
+ disposes its stale per-run sub-context; `ask_question` pairs answers by
75
+ `in_reply_to` question id so steering cannot pose as an answer; and the
76
+ reaper sweep takes transcript probes out from under the supervisor lock.
77
+
78
+ - **Platform & process (#597):** deep path cleanup tracks a path stack so
79
+ shared non-cyclic objects are no longer elided as `[...]`; the
80
+ `popen_capture` watchdog sets `deadline_hit` only when the kill targets a
81
+ still-live child; an explicit `timed_out` flag replaces exit-code-124
82
+ inference; and preflight kills whole process trees.
83
+
84
+ - **LSP robustness (#601, #605, #609):** the reader rejects non-positive
85
+ `Content-Length` and caps frames at 16 MiB, the diagnostic collector is
86
+ keyed by file with an oldest-first eviction cap, the transport clears
87
+ in-flight pulls on start/stop, and the `lsp.feedback_timeout` knob is
88
+ honored for per-file queries.
89
+
90
+ - **Buffer, plugins, config, storage & session (#589, #596, #598, #599,
91
+ #602):** snapshot persistence is debounced with a retention cap while
92
+ in-memory undo granularity is preserved and `!diff` shares one decoding
93
+ path; `add_plugin` guards derived names and refuses to silently heal a
94
+ corrupt manifest; replayed turns no longer re-record into the active
95
+ session; `Scoped#fetch` lets an explicit `nil` shadow a parent value; and
96
+ `put(ref:)` aliases are validated against `REF_PATTERN`.
97
+
98
+ ## [0.14.0] - 2026-11-19
99
+
100
+ The session-attribution minor: gateway plumbing that needed a stable
101
+ per-conversation identity arrives as a provider-scoped `session_header`
102
+ knob, subagent lanes learn provider-scoped retry knobs, the UI goes
103
+ English — and a large fix wave hardens sandboxing, the security
104
+ classifier, buffers, the AST graph, transports and the goal ledger.
105
+
106
+ ### Added
107
+
108
+ - **Dynamic session header (`providers.<name>.session_header`):** gateways
109
+ that require per-conversation session attribution — opencode's Go gateway
110
+ hard-400s every request with `MissingSessionID` without
111
+ `x-opencode-session` — are now first-class. Setting the knob on a
112
+ provider spec makes every LLM request (root loop, subagents, and repair
113
+ when its caller forwards `session_key`) carry
114
+ `headers[<name>] = <UUID derived from the active session file>`, stable
115
+ per conversation across `!resume` and restarts. A same-named header set
116
+ explicitly in `providers.<name>.headers` or `llm.headers` wins —
117
+ explicit beats automatic.
118
+
119
+ ### Changed
120
+
121
+ - Subagent lanes now honor provider-scoped `providers.<name>.<retry-knob>`
122
+ overrides (`max_retries`, `max_429_retries`, backoff, wall clock, ...)
123
+ through the same `ProviderResolver` seam as the root loop — previously
124
+ only the global `llm.*` spelling reached them.
125
+ - User-facing REPL, CLI and UI messages now render in English. Bilingual
126
+ Turkish/English strings keep only the English half; `Vakanüvis` domain
127
+ vocabulary, Turkish test fixtures and the generated `TOOLS.md` are
128
+ intentionally unchanged. Turkish resume phrases (`devam et`) still work.
129
+
11
130
  ## [0.13.0] - 2026-11-17
12
131
 
13
132
  The connect-wizard minor: provider binding collapses into an interactive
@@ -1820,7 +1939,9 @@ installation section.
1820
1939
  - Context: serialized index mutations, snapshot-outside-lock reconcile,
1821
1940
  streaming-first truncation.
1822
1941
 
1823
- [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.13.0...HEAD
1942
+ [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.15.0...HEAD
1943
+ [0.15.0]: https://github.com/ozgurulukir/rubik/compare/v0.14.0...v0.15.0
1944
+ [0.14.0]: https://github.com/ozgurulukir/rubik/compare/v0.13.0...v0.14.0
1824
1945
  [0.13.0]: https://github.com/ozgurulukir/rubik/compare/v0.12.0...v0.13.0
1825
1946
  [0.12.0]: https://github.com/ozgurulukir/rubik/compare/v0.11.0...v0.12.0
1826
1947
  [0.11.0]: https://github.com/ozgurulukir/rubik/compare/v0.10.0...v0.11.0
@@ -81,7 +81,10 @@ module Rubik
81
81
  elsif Runtime::Discovery.binary_available?("npx")
82
82
  "npx tsc --noEmit #{escaped}"
83
83
  else
84
- "node --check #{escaped}"
84
+ # `node --check` cannot parse TypeScript — falling back to
85
+ # it reported a false syntax error for every TS file
86
+ # (issue #588). Report unavailability like the .vue branch.
87
+ return unavailable_result(reason: "No syntax checker available for TypeScript files. Install tsc or npx.")
85
88
  end
86
89
  when ".vue"
87
90
  if Runtime::Discovery.binary_available?("vue-tsc")
@@ -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,
@@ -117,7 +117,12 @@ module Rubik
117
117
 
118
118
  j = start + 1
119
119
  while j < raw.length
120
- j += 1 if raw[j] == "\\"
120
+ if raw[j] == "\\"
121
+ # Issue #600: skip the escaped pair entirely — an escaped `\/`
122
+ # is content, not the delimiter, and must not end the span.
123
+ j += 2
124
+ next
125
+ end
121
126
  break if raw[j] == "/"
122
127
 
123
128
  j += 1
@@ -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
@@ -168,22 +172,28 @@ module Rubik
168
172
  end
169
173
 
170
174
  def extract_via_fallback(symbols)
171
- current_class_or_module = nil
175
+ # Scope stack of [qualified_name, end_line] pairs (issue #600):
176
+ # entries pop once the scanner moves past their matching `end`, so
177
+ # top-level defs after a class body are no longer misattributed and
178
+ # nested `module M; class C` yields `M::C` prefixes.
179
+ scopes = []
172
180
  i = 0
173
181
 
174
182
  while i < code_lines.size
175
- line = code_lines[i]
176
183
  line_num = i + 1
177
- stripped = line.strip
184
+ scopes.reject! { |(_, end_line)| line_num > end_line }
185
+
186
+ stripped = code_lines[i].strip
178
187
 
179
188
  if stripped.start_with?("class ") || stripped.start_with?("module ")
180
189
  kind = stripped.start_with?("class ") ? :class : :module
181
190
  name = stripped.split(/\s+/)[1].to_s.split("<").first.to_s.strip
182
- current_class_or_module = name
191
+ qualified_name = scopes.empty? ? name : "#{scopes.last[0]}::#{name}"
183
192
  end_line = find_matching_end(i)
193
+ scopes << [qualified_name, end_line]
184
194
 
185
195
  symbols << Symbol.new(
186
- name: name,
196
+ name: qualified_name,
187
197
  kind: kind,
188
198
  file: @file_path,
189
199
  line_start: line_num,
@@ -192,7 +202,7 @@ module Rubik
192
202
  )
193
203
  elsif stripped.start_with?("def ")
194
204
  method_name = stripped.sub(/^def\s+/, "").split(/[(\s;]/).first.strip
195
- full_name = current_class_or_module ? "#{current_class_or_module}##{method_name}" : method_name
205
+ full_name = scopes.empty? ? method_name : "#{scopes.last[0]}##{method_name}"
196
206
  end_line = find_matching_end(i)
197
207
  calls_src = lines[(line_num - 1)..(end_line - 1)]&.join.to_s
198
208
  calls = []
@@ -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,18 +223,31 @@ 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
+ content
235
+ end
236
+
237
+ # Give the advertised !undo / !rollback commands a producer in
238
+ # the live write path (issue #544). Snapshotting is auxiliary:
239
+ # a storage failure must not turn a successful edit into a
240
+ # failed write. The deflate+disk write runs OUTSIDE the monitor
241
+ # (issue #589) so the whole-buffer snapshot no longer serializes
242
+ # concurrent buffer operations behind its IO; the store reads
243
+ # state through the monitor-protected snapshot_state, and its
244
+ # debouncing coalesces bursts into one write per interval.
245
+ begin
246
+ @snapshot_store&.create_snapshot(self)
247
+ rescue StandardError => e
248
+ warn "[Buffer] snapshot recording failed: #{e.class}: #{e.message}"
233
249
  end
250
+ content
234
251
  end
235
252
 
236
253
  # Returns whether the buffer has any unapplied file modifications,
@@ -309,7 +326,7 @@ module Rubik
309
326
 
310
327
  diffs = snapshot.map do |rel_path, new_content|
311
328
  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)) : ""
329
+ orig_content = File.exist?(full_path) ? read_bytes_as_utf8(full_path) : ""
313
330
  Diff.unified_diff(rel_path, orig_content, new_content)
314
331
  end
315
332
 
@@ -420,10 +437,20 @@ module Rubik
420
437
  full_path = File.expand_path(rel_path, @root_dir)
421
438
  return nil unless File.exist?(full_path)
422
439
 
423
- Digest::SHA256.hexdigest(Rubik::Platform.normalize_newlines(Rubik::Platform.read_text(full_path)))
440
+ Digest::SHA256.hexdigest(read_bytes_as_utf8(full_path))
424
441
  rescue Errno::ENOENT, Errno::EACCES
425
442
  nil
426
443
  end
444
+
445
+ private
446
+
447
+ # Platform.read_text uses File.read (text mode), which translates
448
+ # CRLF to LF on Windows and would silently break the raw-byte
449
+ # newline contract (issue #544). File.binread keeps the bytes;
450
+ # safe_utf8 re-tags and scrubs exactly as read_text does.
451
+ def read_bytes_as_utf8(full_path)
452
+ Rubik::Platform.safe_utf8(File.binread(full_path))
453
+ end
427
454
  end
428
455
  end
429
456
  end