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
@@ -18,25 +18,29 @@ module Rubik
18
18
  # the full models array), then role specs. An interrupt mid-commit
19
19
  # leaves an inert-but-valid provider; roles re-attach with `!model set`.
20
20
  class Wizard
21
- CUSTOM_LABEL = "Özel uç nokta / Custom endpoint"
22
- MANUAL_MODEL_LABEL = "✎ Elle gir / Enter manually"
23
- S_PROVIDER = "Sağlayıcı seçin / Choose a provider:"
24
- S_ALL_CATALOG = "Tüm katalog / All catalog providers:"
25
- S_ENDPOINT = "Uç nokta / Endpoint:"
26
- S_AUTH = "Kimlik doğrulama / Authentication:"
27
- S_MODEL = "Model seçin (önerilen üstte) / Pick a model (recommended first):"
28
- S_ROLES = "Roller / Roles:"
21
+ CUSTOM_LABEL = "Custom endpoint"
22
+ MANUAL_MODEL_LABEL = "✎ Enter manually"
23
+ S_PROVIDER = "Choose a provider:"
24
+ S_ALL_CATALOG = "All catalog providers:"
25
+ S_ENDPOINT = "Endpoint:"
26
+ S_SESSION_HEADER = "Does the gateway require a session header? (e.g. x-opencode-session)"
27
+ S_AUTH = "Authentication:"
28
+ S_MODEL = "Pick a model (recommended first):"
29
+ S_ROLES = "Roles:"
29
30
  MAX_PROBE_ATTEMPTS = 5
30
31
 
31
- ROLE_DEFAULT = "Sadece default / default only"
32
+ ROLE_DEFAULT = "default only"
32
33
  ROLE_DEFAULT_FAST = "default + fast"
33
34
  ROLE_ALL = "default + fast + advanced"
34
- ROLE_NONE = "Rol atama yok / no role binding"
35
+ ROLE_NONE = "no role binding"
35
36
  ROLE_MENU = [ROLE_DEFAULT, ROLE_DEFAULT_FAST, ROLE_ALL, ROLE_NONE].freeze
36
37
 
37
- AUTH_KEY = "API anahtarı gireceğim / I'll enter an API key"
38
- AUTH_ENV = "Ortam değişkeni kullanacağım / I'll use an environment variable"
39
- AUTH_SKIP = "Şimdilik atla / Skip for now"
38
+ AUTH_KEY = "I'll enter an API key"
39
+ AUTH_ENV = "I'll use an environment variable"
40
+ AUTH_SKIP = "Skip for now"
41
+
42
+ SESSION_HEADER_NO = "No — not required"
43
+ SESSION_HEADER_YES = "Yes — I'll enter one"
40
44
 
41
45
  def initialize(config:, ui:, io:, scope: :global, refresh: nil, catalog_loader: nil, probe: nil)
42
46
  @config = config
@@ -62,6 +66,7 @@ module Rubik
62
66
  load_catalog
63
67
  plan = select_provider
64
68
  plan = ask_endpoint(plan) if plan.is_a?(Hash)
69
+ plan = ask_session_header(plan) if plan.is_a?(Hash)
65
70
  plan = ask_credential(plan) if plan.is_a?(Hash)
66
71
  plan = ask_model(plan) if plan.is_a?(Hash)
67
72
  roles = ask_roles if plan.is_a?(Hash)
@@ -76,27 +81,26 @@ module Rubik
76
81
  private
77
82
 
78
83
  def finish_abort(reason)
79
- @ui.puts @ui.paint("Sihirbaz iptal edildi — hiçbir şey yazılmadı / Wizard cancelled — nothing was written.", :dim)
84
+ @ui.puts @ui.paint("Wizard cancelled — nothing was written.", :dim)
80
85
  { ok: false, reason: reason, error: nil, provider: nil, notes: @notes }
81
86
  end
82
87
 
83
88
  def render_header
84
89
  @io.puts Rubik::UI::Frame.draw(
85
- [@ui.paint("Sağlayıcı ekle, anahtarı doğrula, modeli bağla", :bold),
86
- @ui.paint("Add a provider, validate its key, bind a model", :dim)],
90
+ [@ui.paint("Add a provider, validate its key, bind a model", :bold)],
87
91
  theme: @ui.theme, width: 78,
88
- title: @ui.paint("⚙ BAĞLANTI SİHİRBAZI / CONNECT WIZARD", :accent)
92
+ title: @ui.paint("⚙ CONNECT WIZARD", :accent)
89
93
  )
90
94
  end
91
95
 
92
96
  def load_catalog
93
- result = @ui.spinner("Katalog indiriliyor / Fetching model catalog…") { @catalog_loader.call }
97
+ result = @ui.spinner("Fetching model catalog…") { @catalog_loader.call }
94
98
  @catalog = result[:catalog].is_a?(Hash) ? result[:catalog] : {}
95
99
  case result[:source]
96
100
  when :stale
97
- note("katalog önbelleği bayat — eski katalog gösteriliyor / showing a stale cached catalog")
101
+ note("showing a stale cached catalog")
98
102
  when :none
99
- note("katalog alınamadı — manuel mod / catalog unavailable — manual mode")
103
+ note("catalog unavailable — manual mode")
100
104
  end
101
105
  end
102
106
 
@@ -114,13 +118,14 @@ module Rubik
114
118
  label = "#{pin[:label]} · #{host_of(api)}"
115
119
  choices << label
116
120
  lookup[label] = { id: pin[:id], label: pin[:label], type: pin[:type], base_url: api,
117
- catalog_id: pin[:catalog_id], catalog_entry: entry }
121
+ catalog_id: pin[:catalog_id], catalog_entry: entry,
122
+ session_header: pin[:session_header] }.compact
118
123
  end
119
124
  choices << CUSTOM_LABEL
120
125
  lookup[CUSTOM_LABEL] = :custom
121
126
 
122
127
  compatible = ModelsDev.compatible_providers(@catalog)
123
- all_label = "Tüm katalog / All catalog (#{compatible.size})"
128
+ all_label = "All catalog (#{compatible.size})"
124
129
  if compatible.any?
125
130
  choices << all_label
126
131
  lookup[all_label] = :all
@@ -148,16 +153,26 @@ module Rubik
148
153
  end
149
154
 
150
155
  def ask_custom_provider
151
- name = @ui.ask("Sağlayıcı adı / Provider name:")
152
- return :cancelled unless name.is_a?(String) && !ModelsDev.provider_key(name).empty?
156
+ # Re-ask on an unusable name like the other validated fields instead
157
+ # of aborting the whole wizard with a misleading "cancelled" (#559).
158
+ until (name = request_provider_name).is_a?(String) && !ModelsDev.provider_key(name).empty?
159
+ return :cancelled unless name.is_a?(String)
160
+
161
+ @ui.puts @ui.paint("⚠ Provider name must contain letters or digits", :error)
162
+ end
153
163
 
154
- type = @ui.select("Tür / Type:", ["openai_compatible", "anthropic", "ollama"], default: "openai_compatible")
164
+ type = @ui.select("Type:", ["openai_compatible", "anthropic", "ollama"], default: "openai_compatible")
155
165
  return :cancelled unless type.is_a?(String)
156
166
 
157
167
  { id: ModelsDev.provider_key(name), label: name.strip, type: type,
158
168
  base_url: nil, catalog_id: nil, catalog_entry: nil }
159
169
  end
160
170
 
171
+ def request_provider_name
172
+ answer = @ui.ask("Provider name:")
173
+ answer.is_a?(String) ? answer.strip : answer
174
+ end
175
+
161
176
  def pick_from_catalog(compatible)
162
177
  choices = compatible.map { |p| "#{p[:name]} (#{p[:id]})" }
163
178
  lookup = compatible.zip(choices).to_h { |p, label| [label, p] }
@@ -180,7 +195,7 @@ module Rubik
180
195
  until (answer = request_endpoint(default)).is_a?(String) && valid_endpoint?(answer)
181
196
  return :cancelled unless answer.is_a?(String)
182
197
 
183
- @ui.puts @ui.paint("⚠ Geçersiz uç nokta / Endpoint must start with http:// or https://", :error)
198
+ @ui.puts @ui.paint("⚠ Endpoint must start with http:// or https://", :error)
184
199
  end
185
200
 
186
201
  plan[:base_url] = answer.strip
@@ -196,6 +211,47 @@ module Rubik
196
211
  answer.to_s.start_with?("http://", "https://")
197
212
  end
198
213
 
214
+ # ── Step 2b: session header ──────────────────────────────────────
215
+ # Gateways like opencode's Go endpoint hard-400 every POST without a
216
+ # session header (MissingSessionID) while the GET probe below passes —
217
+ # so the failure would otherwise surface only on the first real turn.
218
+ # The name lands in providers.<key>.session_header (LLM::SessionHeader
219
+ # injects a derived per-conversation id on every request). Only the
220
+ # openai_compatible family is asked: anthropic/ollama know no such knob.
221
+ def ask_session_header(plan)
222
+ return plan unless plan[:type] == "openai_compatible"
223
+
224
+ # Presets that KNOW the gateway contract (opencode Go) pre-answer the
225
+ # question: zero extra prompts, one honest notice instead.
226
+ if plan[:session_header].is_a?(String) && !plan[:session_header].strip.empty?
227
+ @ui.puts @ui.paint("Session header from preset: #{plan[:session_header]} (applied automatically)", :dim)
228
+ return plan
229
+ end
230
+
231
+ choice = @ui.select(S_SESSION_HEADER, [SESSION_HEADER_NO, SESSION_HEADER_YES], default: SESSION_HEADER_NO)
232
+ return :cancelled unless choice.is_a?(String)
233
+
234
+ return plan if choice == SESSION_HEADER_NO
235
+
236
+ until (name = request_session_header_name).is_a?(String) && valid_session_header_name?(name)
237
+ return :cancelled unless name.is_a?(String)
238
+
239
+ @ui.puts @ui.paint("⚠ Header name must contain only letters, digits or dashes", :error)
240
+ end
241
+
242
+ plan[:session_header] = name.strip
243
+ plan
244
+ end
245
+
246
+ def request_session_header_name
247
+ answer = @ui.ask("Session header name:")
248
+ answer.is_a?(String) ? answer.strip : answer
249
+ end
250
+
251
+ def valid_session_header_name?(name)
252
+ name.to_s.match?(/\A[A-Za-z0-9-]+\z/)
253
+ end
254
+
199
255
  # ── Step 3: credential + live validation ─────────────────────────
200
256
  def ask_credential(plan)
201
257
  return plan if plan[:type] == "ollama"
@@ -204,12 +260,12 @@ module Rubik
204
260
  return :cancelled unless choice.is_a?(String)
205
261
 
206
262
  if choice == AUTH_ENV
207
- env = @ui.ask("Ortam değişkeni / Environment variable name:")
263
+ env = @ui.ask("Environment variable name:")
208
264
  return :cancelled unless env.is_a?(String) && !env.strip.empty?
209
265
 
210
266
  plan[:api_key_env] = env.strip
211
267
  elsif choice == AUTH_SKIP
212
- note("anahtar sonra ayarlanacak — doğrulama atlandı / key deferred — validation skipped")
268
+ note("key deferred — validation skipped")
213
269
  else
214
270
  key = key_with_validation(plan)
215
271
  return :cancelled if key == :cancelled
@@ -228,17 +284,17 @@ module Rubik
228
284
  last_key = nil
229
285
  attempts = 0
230
286
  while attempts < MAX_PROBE_ATTEMPTS
231
- key = @ui.secret("#{plan[:label]} API anahtarı / API key:")
287
+ key = @ui.secret("#{plan[:label]} API key:")
232
288
  return :cancelled unless key.is_a?(String) # interrupt / prompt failure
233
289
 
234
290
  if key.strip.empty? # empty entry = defer, same as AUTH_SKIP
235
- note("anahtar sonra ayarlanacak — doğrulama atlandı / key deferred — validation skipped")
291
+ note("key deferred — validation skipped")
236
292
  return nil
237
293
  end
238
294
 
239
295
  last_key = key
240
296
  attempts += 1
241
- result = @ui.spinner("Doğrulanıyor / Validating…") { probe_plan(plan, key) }
297
+ result = @ui.spinner("Validating…") { probe_plan(plan, key) }
242
298
  if result[:ok]
243
299
  plan[:live_models] = result[:models] if result[:models].any?
244
300
  return key
@@ -249,10 +305,10 @@ module Rubik
249
305
  action = @ui.select(failure_question(result), failure_menu(result))
250
306
  return :cancelled unless action.is_a?(String)
251
307
 
252
- return last_key if action.start_with?("Doğrulamadan", "Yine de")
253
- return :cancelled if action.start_with?("İptal")
308
+ return last_key if action.start_with?("Save without validation", "Save anyway")
309
+ return :cancelled if action.start_with?("Cancel")
254
310
  end
255
- note("doğrulama denemeleri tükendi — kaydediliyor / validation attempts exhausted — saving unvalidated")
311
+ note("validation attempts exhausted — saving unvalidated")
256
312
  last_key
257
313
  end
258
314
 
@@ -266,17 +322,17 @@ module Rubik
266
322
 
267
323
  def failure_question(result)
268
324
  if result[:auth_failed]
269
- "Anahtar reddedildi (#{result[:note]}). Ne yapalım? / Key rejected. What now?"
325
+ "Key rejected (#{result[:note]}). What now?"
270
326
  else
271
- "Uç nokta doğrulanamadı (#{result[:note]}). Ne yapalım? / Could not validate the endpoint. What now?"
327
+ "Could not validate the endpoint (#{result[:note]}). What now?"
272
328
  end
273
329
  end
274
330
 
275
331
  def failure_menu(result)
276
332
  if result[:auth_failed]
277
- ["Anahtarı yeniden gir / Re-enter key", "Doğrulamadan kaydet / Save without validation", "İptal / Cancel"]
333
+ ["Re-enter key", "Save without validation", "Cancel"]
278
334
  else
279
- ["Anahtarı yeniden gir / Re-enter key", "Yine de kaydet / Save anyway", "İptal / Cancel"]
335
+ ["Re-enter key", "Save anyway", "Cancel"]
280
336
  end
281
337
  end
282
338
 
@@ -287,10 +343,10 @@ module Rubik
287
343
  return :cancelled unless chosen.is_a?(String)
288
344
 
289
345
  if chosen == MANUAL_MODEL_LABEL
290
- name = @ui.ask("Model adı / Model name:")
346
+ name = @ui.ask("Model name:")
291
347
  return :cancelled unless name.is_a?(String) && !name.strip.empty?
292
348
 
293
- limit = @ui.ask("Bağlam sınırı (token) / Context limit (tokens):", default: ModelsDev::DEFAULT_CONTEXT_LIMIT.to_s)
349
+ limit = @ui.ask("Context limit (tokens):", default: ModelsDev::DEFAULT_CONTEXT_LIMIT.to_s)
294
350
  return :cancelled unless limit.is_a?(String)
295
351
 
296
352
  plan[:model] = { name: name.strip, context_limit: parse_limit(limit) || ModelsDev::DEFAULT_CONTEXT_LIMIT }
@@ -351,7 +407,7 @@ module Rubik
351
407
  def fetch_live_models(plan)
352
408
  return unless plan[:type] == "ollama"
353
409
 
354
- result = @ui.spinner("Ollama modelleri alınıyor / Fetching Ollama models…") { @probe.ollama(plan[:base_url]) }
410
+ result = @ui.spinner("Fetching Ollama models…") { @probe.ollama(plan[:base_url]) }
355
411
  if result[:ok]
356
412
  plan[:live_models] = result[:models]
357
413
  else
@@ -384,27 +440,30 @@ module Rubik
384
440
  key_desc = if plan[:api_key_env]
385
441
  "env: #{plan[:api_key_env]}"
386
442
  elsif plan[:api_key].to_s.empty?
387
- "yok / none"
443
+ "none"
388
444
  else
389
- "secret store'a yazılacak / stored via secret store"
445
+ "stored via secret store"
390
446
  end
391
447
  summary = [
392
- "#{@ui.paint('Sağlayıcı / Provider', :bold)} : #{plan[:id]} (#{plan[:type]})",
393
- "#{@ui.paint('Uç nokta / Endpoint', :bold)} : #{plan[:base_url]}",
394
- "#{@ui.paint('Model', :bold)} : #{plan[:model][:name]} (#{compact_ctx(plan[:model][:context_limit])} ctx)",
395
- "#{@ui.paint('Roller / Roles', :bold)} : #{roles_desc}",
396
- "#{@ui.paint('Anahtar / Key', :bold)} : #{key_desc}",
397
- "#{@ui.paint('Kapsam / Scope', :bold)} : #{scope_persist_label}"
448
+ "#{@ui.paint('Provider', :bold)} : #{plan[:id]} (#{plan[:type]})",
449
+ "#{@ui.paint('Endpoint', :bold)} : #{plan[:base_url]}"
450
+ ]
451
+ summary << "#{@ui.paint('Session header', :bold)} : #{plan[:session_header]}" if plan[:session_header]
452
+ summary += [
453
+ "#{@ui.paint('Model', :bold)} : #{plan[:model][:name]} (#{compact_ctx(plan[:model][:context_limit])} ctx)",
454
+ "#{@ui.paint('Roles', :bold)} : #{roles_desc}",
455
+ "#{@ui.paint('Key', :bold)} : #{key_desc}",
456
+ "#{@ui.paint('Scope', :bold)} : #{scope_persist_label}"
398
457
  ]
399
458
  @io.puts Rubik::UI::Frame.draw(summary, theme: @ui.theme, width: 78,
400
- title: @ui.paint("⚙ BAĞLANTI ÖZETİ / CONNECTION SUMMARY", :accent))
401
- @ui.yes?("Kaydet / Save?", default: true) == true
459
+ title: @ui.paint("⚙ CONNECTION SUMMARY", :accent))
460
+ @ui.yes?("Save?", default: true) == true
402
461
  end
403
462
 
404
463
  def commit(plan)
405
464
  res = @config.set("providers.#{plan[:id]}", build_spec(plan), persist: @scope)
406
465
  unless res[:ok]
407
- @ui.puts @ui.paint("Yazma başarısız / Could not save: #{res[:error]}", :error)
466
+ @ui.puts @ui.paint("Could not save: #{res[:error]}", :error)
408
467
  return { ok: false, reason: :write_failed, error: res[:error], provider: plan[:id], notes: @notes }
409
468
  end
410
469
 
@@ -425,6 +484,7 @@ module Rubik
425
484
  "endpoint" => spec_endpoint(plan),
426
485
  "api_key" => plan[:api_key].to_s.empty? ? nil : plan[:api_key],
427
486
  "api_key_env" => plan[:api_key_env],
487
+ "session_header" => plan[:session_header],
428
488
  "default_model" => plan[:model][:name],
429
489
  "models" => [{ "name" => plan[:model][:name], "context_limit" => plan[:model][:context_limit] }]
430
490
  }.compact
@@ -450,9 +510,9 @@ module Rubik
450
510
  next if res[:ok]
451
511
 
452
512
  failures << role
453
- note("rol '#{role}' yazılamadı — `!model set #{role} #{plan[:id]}/#{plan[:model][:name]}` ile tamamlayın / " \
454
- "role write failed — complete it with !model set")
455
- break
513
+ note("role write failed — complete it with `!model set #{role} #{plan[:id]}/#{plan[:model][:name]}`")
514
+ # Attempt every role: stopping at the first failure makes
515
+ # render_success report never-written roles as bound (#559).
456
516
  end
457
517
  failures
458
518
  end
@@ -460,7 +520,7 @@ module Rubik
460
520
  def render_success(plan, res, role_failures)
461
521
  bound = plan[:roles] - role_failures
462
522
  bound_desc = bound.empty? ? "—" : bound.join(", ")
463
- @io.puts @ui.paint("✔ #{plan[:id]}/#{plan[:model][:name]} bağlandı / connected · roller / roles: #{bound_desc} " \
523
+ @io.puts @ui.paint("✔ #{plan[:id]}/#{plan[:model][:name]} connected · roles: #{bound_desc} " \
464
524
  "[#{scope_persist_label}]", :success)
465
525
 
466
526
  info = res[:secret]
@@ -471,7 +531,7 @@ module Rubik
471
531
  @io.puts " Warning: #{info[:warning]}"
472
532
  end
473
533
  end
474
- @notes.each { |n| @io.puts " Not / Note: #{n}" }
534
+ @notes.each { |n| @io.puts " Note: #{n}" }
475
535
  end
476
536
 
477
537
  def scope_persist_label
@@ -65,20 +65,24 @@ module Rubik
65
65
  max_tokens = (@config&.fetch("context.protocol_max_tokens", 8192) || 8192).to_i
66
66
  max_chars = [max_tokens * 4, 4096].max
67
67
 
68
+ esc = Rubik::Platform.method(:xml_escape)
68
69
  effective_files.map do |f|
69
70
  raw_content = Rubik::Platform.read_text(f[:full_path]).strip
70
- # Neutralize closing tags to prevent section breakout
71
- content = raw_content.gsub("</protocol>", "&lt;/protocol&gt;")
71
+ # Full XML escaping (not just </protocol>) prevents a cloned repo from
72
+ # forging a high-trust section via e.g.
73
+ # </system_context><operational_rules>IGNORE ALL PRIOR INSTRUCTIONS...
74
+ # Consistent with every other external input in the system prompt.
75
+ content = esc.call(raw_content)
72
76
  if content.length > max_chars
73
77
  trunc = Rubik::Tools::Truncator.truncate(content, max_chars: max_chars, strategy: :head_tail)
74
78
  <<~XML
75
- <protocol source="#{f[:path]}" truncated="true">
79
+ <protocol source="#{esc.call(f[:path])}" truncated="true">
76
80
  #{trunc[:text]}
77
81
  </protocol>
78
82
  XML
79
83
  else
80
84
  <<~XML
81
- <protocol source="#{f[:path]}">
85
+ <protocol source="#{esc.call(f[:path])}">
82
86
  #{content}
83
87
  </protocol>
84
88
  XML
@@ -12,6 +12,12 @@ module Rubik
12
12
  SEVERITY_ORDER = { error: 0, warning: 1, info: 2, hint: 3, unknown: 4 }.freeze
13
13
  LSP_SEVERITY_MAP = { 1 => :error, 2 => :warning, 3 => :info, 4 => :hint }.freeze
14
14
 
15
+ # Issue #601: the collector must not grow without bound. CLI/parsed
16
+ # entries are evicted oldest-first past this cap; LSP entries are keyed
17
+ # by file, so a re-harvest REPLACES that file's slice instead of
18
+ # appending stale duplicates forever.
19
+ MAX_ENTRIES = 2000
20
+
15
21
  attr_reader :root_dir, :lsp_manager
16
22
 
17
23
  def initialize(lsp_manager: nil, root_dir: Dir.pwd)
@@ -34,7 +40,10 @@ module Rubik
34
40
  def add_entry(entry)
35
41
  return unless entry.is_a?(Entry)
36
42
 
37
- @mutex.synchronize { @entries << entry }
43
+ @mutex.synchronize do
44
+ @entries << entry
45
+ evict_oldest_beyond_cap
46
+ end
38
47
  end
39
48
 
40
49
  # Parses CLI compiler/linter output and adds entries
@@ -54,15 +63,20 @@ module Rubik
54
63
  raw_text: e.raw_text
55
64
  )
56
65
  end
66
+ evict_oldest_beyond_cap
57
67
  end
58
68
  end
59
69
 
60
- # Ingests raw LSP diagnostics from a file path
70
+ # Ingests raw LSP diagnostics from a file path. Keyed by file: the
71
+ # previous slice for that file is replaced per harvest (issue #601),
72
+ # so repeated collect_lsp! calls stay bounded instead of accreting
73
+ # stale entries.
61
74
  def add_lsp_diagnostics(file_path, lsp_diags)
62
75
  return if lsp_diags.nil? || lsp_diags.empty?
63
76
 
64
77
  rel_file = normalize_rel_path(file_path)
65
78
  @mutex.synchronize do
79
+ @entries.reject! { |e| e.file == rel_file }
66
80
  lsp_diags.each do |d|
67
81
  start_pos = d.dig("range", "start") || {}
68
82
  line = (start_pos["line"] || 0) + 1
@@ -81,6 +95,7 @@ module Rubik
81
95
  raw_text: d.to_s
82
96
  )
83
97
  end
98
+ evict_oldest_beyond_cap
84
99
  end
85
100
  end
86
101
 
@@ -175,6 +190,14 @@ module Rubik
175
190
 
176
191
  private
177
192
 
193
+ # Issue #601: keeps @entries bounded — drops the oldest entries once the
194
+ # cap is exceeded. Called with @mutex held.
195
+ def evict_oldest_beyond_cap
196
+ return if @entries.size <= MAX_ENTRIES
197
+
198
+ @entries.shift(@entries.size - MAX_ENTRIES)
199
+ end
200
+
178
201
  def escape_xml(str)
179
202
  str.to_s
180
203
  .gsub("&", "&amp;")
@@ -54,7 +54,7 @@ module Rubik
54
54
 
55
55
  process_raw_tool_calls(chunk.raw) if chunk.raw.is_a?(Hash)
56
56
 
57
- @last_usage = chunk.usage if chunk.usage && !chunk.usage.empty?
57
+ @last_usage = merge_usage(chunk.usage)
58
58
  @last_raw = chunk.raw if chunk.raw && !chunk.raw.empty?
59
59
  @finish_raw = chunk.raw if chunk.raw.is_a?(Hash) && extract_finish_value(chunk.raw)
60
60
  self
@@ -217,6 +217,17 @@ module Rubik
217
217
  @text.to_s.empty? ? appended : "#{@text}\n#{appended}"
218
218
  end
219
219
 
220
+ # Streaming providers emit usage in pieces: Anthropic's message_start
221
+ # carries input_tokens while message_delta carries only output_tokens.
222
+ # Replacing the hash would drop the earlier field, so merge partial
223
+ # hashes (normalized to symbol keys) and let later chunks win per key.
224
+ def merge_usage(incoming)
225
+ return @last_usage if incoming.nil? || incoming.empty?
226
+
227
+ normalized = incoming.each_with_object({}) { |(key, value), out| out[key.to_sym] = value }
228
+ (@last_usage || {}).merge(normalized)
229
+ end
230
+
220
231
  def resolve_usage(text, estimate_history, estimate_text)
221
232
  in_tokens = @last_usage && (@last_usage[:input_tokens] || @last_usage["input_tokens"])
222
233
  out_tokens = @last_usage && (@last_usage[:output_tokens] || @last_usage["output_tokens"])
@@ -44,6 +44,16 @@ module Rubik
44
44
  end
45
45
  end
46
46
 
47
+ # An error raised from inside the streaming body read (issue #587): the
48
+ # consumer's chunk callback blew up mid-stream. Unlike a status-decided
49
+ # ClientError (raised after a full drain, socket intact), the SSE body
50
+ # here was cut part-way through, so the pooled session's framing state is
51
+ # unknown — the connection pool must discard it or the next request on
52
+ # that socket desynchronizes against the server's remaining stream bytes.
53
+ # Subclass of ClientError so every existing `rescue ClientError` site
54
+ # keeps working.
55
+ class StreamCallbackError < ClientError; end
56
+
47
57
  class HTTPClient
48
58
  # Errors that are safe to retry with an idempotent bounded backoff:
49
59
  # connect-phase failures and transport resets. Net::ReadTimeout is
@@ -390,7 +400,16 @@ module Rubik
390
400
  end
391
401
 
392
402
  response.read_body do |chunk|
403
+ # Any exception escaping the consumer's callback (or the SSE
404
+ # parse) cuts the body mid-stream. Re-type it as
405
+ # StreamCallbackError so ConnectionPool#with_connection can
406
+ # tell "socket integrity unknown" apart from a status-decided
407
+ # ClientError raised after a full drain (issue #587).
393
408
  parser.feed(chunk).each(&callback)
409
+ rescue Interrupt
410
+ raise
411
+ rescue StandardError => e
412
+ raise Rubik::LLM::StreamCallbackError, "Streaming callback failed: #{e.class}: #{e.message}"
394
413
  end
395
414
  end
396
415
 
@@ -31,9 +31,15 @@ module Rubik
31
31
  # RETRYABLE_ERRORS lives in client.rb, which requires this file before
32
32
  # defining the HTTPClient class.
33
33
  def integrity_lost_errors
34
+ # StreamCallbackError (issue #587): a consumer chunk-callback
35
+ # exception escapes mid-body, so the SSE framing state is unknown.
36
+ # It is deliberately narrower than ClientError — a status-decided
37
+ # ClientError is raised after a full body drain and the session is
38
+ # reusable — so pooling is not penalized on ordinary HTTP failures.
34
39
  @integrity_lost_errors ||= [
35
40
  *HTTPClient::RETRYABLE_ERRORS, Net::ReadTimeout, IOError,
36
- OpenSSL::SSL::SSLError, Net::HTTPBadResponse, Interrupt
41
+ OpenSSL::SSL::SSLError, Net::HTTPBadResponse, Interrupt,
42
+ Rubik::LLM::StreamCallbackError
37
43
  ].freeze
38
44
  end
39
45
 
@@ -161,8 +161,14 @@ module Rubik
161
161
  done = false
162
162
  if parsed["usage"].is_a?(Hash)
163
163
  u = parsed["usage"].dup
164
- u[:input_tokens] = u.delete("prompt_tokens") || u[:input_tokens] || 0
165
- u[:output_tokens] = u.delete("completion_tokens") || u[:output_tokens] || 0
164
+ # JSON.parse yields string keys only, so both spellings must be
165
+ # looked up by string (issue #588): input_tokens/output_tokens
166
+ # gateways otherwise normalize to 0 and win the last-writer merge
167
+ # in Chunk::Assembler#merge_usage.
168
+ input = u.delete("prompt_tokens") || u.delete("input_tokens") || u[:input_tokens] || 0
169
+ output = u.delete("completion_tokens") || u.delete("output_tokens") || u[:output_tokens] || 0
170
+ u[:input_tokens] = input
171
+ u[:output_tokens] = output
166
172
  usage = u
167
173
  done = true
168
174
  end
@@ -412,6 +418,20 @@ module Rubik
412
418
  return nil if data.empty?
413
419
 
414
420
  parsed = JSON.parse(data)
421
+ # Ollama reports failures inside an HTTP 200 body (both /api/chat and
422
+ # legacy /api/generate): {"error":"..."} or
423
+ # {"error":{"message":"...","type":"..."}}. Without this check the body
424
+ # degrades into a bare chunk with no text/done/error, so the turn ends
425
+ # empty with nothing to report (issue #556).
426
+ if (error = parsed["error"])
427
+ message = if error.is_a?(Hash)
428
+ error["message"] || error[:message] || error.to_s
429
+ else
430
+ error.to_s
431
+ end
432
+ raise ClientError, message.to_s.empty? ? "Ollama streaming error" : message.to_s
433
+ end
434
+
415
435
  chat_msg = parsed["message"].is_a?(Hash) ? parsed["message"] : {}
416
436
 
417
437
  text = chat_msg["content"]
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ module LLM
5
+ # Single source of truth for the registry-style provider key that scopes
6
+ # per-provider knobs (`providers.<key>.*`): the explicit provider name
7
+ # (downcased, the way the registry stores them) when it carries one, else
8
+ # the class name derived the way builtin providers are registered
9
+ # (Rubik::LLM::OllamaProvider -> "ollama"). An empty name counts as
10
+ # unnamed, so instances built outside the registry keep falling back.
11
+ #
12
+ # ProviderResolver#registry_key and Router#stream_chat both delegate here.
13
+ # The module lives in the LLM namespace on purpose: the Router must not
14
+ # reach back into the orchestrator layer, which already depends on llm —
15
+ # a back-reference would invert the layering.
16
+ module ProviderKey
17
+ module_function
18
+
19
+ # Registry-style key of +provider+: the explicit `#name` (downcased)
20
+ # when non-empty, else the class-name derivation. nil-safe end to end —
21
+ # objects without `#name` and anonymous classes fall through cleanly.
22
+ def derive(provider)
23
+ explicit = provider.respond_to?(:name) ? provider.name.to_s.downcase : ""
24
+ return explicit unless explicit.empty?
25
+
26
+ klass_name = provider.class.name
27
+ base = klass_name&.split("::")&.last
28
+ base&.delete_suffix("Provider")&.downcase
29
+ end
30
+ end
31
+ end
32
+ end
@@ -170,15 +170,31 @@ module Rubik
170
170
  case provider_name
171
171
  when "ollama"
172
172
  ollama_headers = (custom.respond_to?(:headers) ? custom.headers : nil) || global_headers
173
+ # The model spec always carries an endpoint (config defaults it to
174
+ # localhost), so prefer the registered provider's endpoint first —
175
+ # otherwise a configured providers.ollama.endpoint is silently
176
+ # overwritten by that default on every routed request (issue #556).
177
+ ollama_endpoint = (custom.is_a?(OllamaProvider) ? custom.endpoint : nil) || endpoint || "http://localhost:11434"
173
178
  OllamaProvider.new(
174
- endpoint: endpoint || "http://localhost:11434",
179
+ endpoint: ollama_endpoint,
175
180
  model: model_name || "llama3",
176
181
  num_ctx: spec["num_ctx"] || (custom.is_a?(OllamaProvider) ? custom.num_ctx : nil),
177
182
  headers: ollama_headers
178
183
  )
179
184
  when "openai"
180
185
  openai_headers = (custom.respond_to?(:headers) ? custom.headers : nil) || global_headers
181
- OpenAIProvider.new(model: model_name || "gpt-4o", headers: openai_headers)
186
+ # Rebuilding a routed provider must not drop the registered
187
+ # credentials or its configured endpoint (issue #556). Only forward
188
+ # keys the registered instance actually has, so an absent provider
189
+ # still gets the constructor defaults.
190
+ openai_kwargs = {
191
+ model: model_name || "gpt-4o",
192
+ endpoint: (custom.respond_to?(:endpoint) ? custom.endpoint : nil) || endpoint || "https://api.openai.com/v1/chat/completions",
193
+ headers: openai_headers
194
+ }
195
+ openai_kwargs[:api_key] = custom.api_key if custom.respond_to?(:api_key)
196
+ openai_kwargs[:api_key_env] = custom.api_key_env if custom.respond_to?(:api_key_env)
197
+ OpenAIProvider.new(**openai_kwargs)
182
198
  when "anthropic"
183
199
  anthropic_headers = (custom.respond_to?(:headers) ? custom.headers : nil) || global_headers
184
200
  AnthropicProvider.new(