rubik 0.11.0 → 0.13.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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +94 -1
  4. data/bin/rubik +11 -1
  5. data/lib/rubik/buffer/manager.rb +3 -3
  6. data/lib/rubik/cli/connect.rb +51 -0
  7. data/lib/rubik/config/deep_ops.rb +3 -1
  8. data/lib/rubik/config/defaults.rb +9 -8
  9. data/lib/rubik/config/schema.rb +8 -0
  10. data/lib/rubik/config/secret_policy.rb +14 -5
  11. data/lib/rubik/config.rb +1 -1
  12. data/lib/rubik/connect/http.rb +85 -0
  13. data/lib/rubik/connect/models_dev.rb +164 -0
  14. data/lib/rubik/connect/probes.rb +96 -0
  15. data/lib/rubik/connect/prompt_ui.rb +139 -0
  16. data/lib/rubik/connect/wizard.rb +488 -0
  17. data/lib/rubik/connect.rb +7 -0
  18. data/lib/rubik/llm/anthropic.rb +2 -1
  19. data/lib/rubik/llm/client.rb +22 -6
  20. data/lib/rubik/llm/formatter.rb +4 -1
  21. data/lib/rubik/llm/ollama.rb +2 -1
  22. data/lib/rubik/llm/openai_compatible.rb +6 -2
  23. data/lib/rubik/llm/router.rb +6 -1
  24. data/lib/rubik/llm/sse_parser.rb +17 -14
  25. data/lib/rubik/mcp/manager.rb +14 -1
  26. data/lib/rubik/mcp/proxy_tool.rb +2 -2
  27. data/lib/rubik/mcp/transport/stdio.rb +3 -2
  28. data/lib/rubik/orchestrator/goal_state.rb +6 -2
  29. data/lib/rubik/orchestrator/loop.rb +30 -6
  30. data/lib/rubik/orchestrator/provider_resolver.rb +12 -0
  31. data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -0
  32. data/lib/rubik/platform/filesystem.rb +2 -2
  33. data/lib/rubik/repair/loop.rb +1 -1
  34. data/lib/rubik/repl/commands/connect.rb +52 -0
  35. data/lib/rubik/repl/commands.rb +2 -0
  36. data/lib/rubik/repl/input_execution.rb +143 -4
  37. data/lib/rubik/repl/input_loop.rb +1 -0
  38. data/lib/rubik/repl/lifecycle.rb +39 -0
  39. data/lib/rubik/repl/shell.rb +9 -1
  40. data/lib/rubik/runtime/fiber.rb +1 -0
  41. data/lib/rubik/runtime/manager.rb +4 -1
  42. data/lib/rubik/runtime/task_manager.rb +3 -3
  43. data/lib/rubik/sandbox/driver/base.rb +7 -0
  44. data/lib/rubik/sandbox/driver/docker.rb +1 -4
  45. data/lib/rubik/sandbox/driver/podman.rb +1 -0
  46. data/lib/rubik/sandbox/runner.rb +1 -1
  47. data/lib/rubik/security/classifier/classifier.rb +2 -0
  48. data/lib/rubik/storage/store.rb +4 -1
  49. data/lib/rubik/tools/registry.rb +36 -16
  50. data/lib/rubik/ui/capability.rb +6 -4
  51. data/lib/rubik/ui/markdown_stream.rb +1 -0
  52. data/lib/rubik/ui/renderer.rb +4 -2
  53. data/lib/rubik/version.rb +1 -1
  54. metadata +9 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c51c73960cae7dc1fc2f89745c3261cc1d4693bbde7c449175f5c16b8f076bbd
4
- data.tar.gz: 7f570e596aafd7ad9b82d634ef9a38d739e027eb2a7c50b613c2b97be5850c6b
3
+ metadata.gz: c6a9e85889683534d644e9f88c1f076dce8e91e0503a2e40a823149d6d1ca359
4
+ data.tar.gz: 925f5cfb990e06c0f7d1b9ac4911db249853bfde78e9902c20b104b114541f5e
5
5
  SHA512:
6
- metadata.gz: ab509129d3f7f20498535c0b51f48e1a902af9484108eb0c12f3f8c13cc5010e1a359f6474aca6742dd09a16e4dded9645ab41b844afbea5eb01b41e7a09725e
7
- data.tar.gz: 40430100af1ca2a9049ace43864ea82d3dcfad72e005ac1698fe71c5b0a7585e56b32f59a22aea1e7339ea689dd569e346f2f540273ac633e5cfd409c9c1386c
6
+ metadata.gz: 2b8c173c30baa1c4b9ad7b9cfdc9baabd98d8c8191569693a7baba5299d03448d0ed1f6f163e7b9d4ea77c9732fdb4f2ad47a8959304758f9f9cc0f4d6548df5
7
+ data.tar.gz: 409d010dc7e497735871c2e0f84690675f10c0e00750137dea7c1cd6ee93a44c94c861b2d1345f8ac4a005e3801bf76506c2c7f0858681eb0e8f807d2a414292
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,700+ 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,97 @@ 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.13.0] - 2026-11-17
12
+
13
+ The connect-wizard minor: provider binding collapses into an interactive
14
+ `!connect` / `rubik connect` flow that selects the provider from the
15
+ models.dev catalog, validates the key live, and routes the secret through
16
+ the existing SecretPolicy — while a review-and-CodeQL round keeps the
17
+ wizard's URL and key-normalization parsing linear-time and enriches its
18
+ model menus from catalog metadata.
19
+
20
+ ### Added
21
+
22
+ - **Connect wizard (`!connect` / `rubik connect`):** an interactive provider
23
+ setup wizard that collapses provider binding into a few answers — provider
24
+ selection (pinned favorites plus the full models.dev catalog behind a
25
+ type-to-filter submenu), a catalog-prefilled endpoint, a masked API key
26
+ with live validation (auth failures re-ask; unreachable endpoints offer
27
+ save-anyway), model selection from the provider's live list or the catalog
28
+ (context limits written from catalog metadata), and role binding
29
+ (default/fast/advanced). Nothing is written before the confirm step; the
30
+ commit goes through the existing Config verbs so the key lands in the OS
31
+ secret store and global scope is the default. The wizard is offered once on
32
+ first run when boot resolves no LLM client (`connect.first_run_offer`
33
+ knob), and the catalog cache (24h TTL, stale-while-error) lives under
34
+ `~/.rubik/cache/`.
35
+
36
+ ### Fixed
37
+
38
+ - **Wizard review hardening:** the provider-key trimming is linear-time
39
+ (`delete_prefix`/`delete_suffix`, no anchored-alternation regex) to keep
40
+ CodeQL's polynomial-ReDoS check clean; hostless http/https URLs are
41
+ rejected; live model listings are enriched with models.dev catalog
42
+ metadata (context limit, tool-call support); and success notes print even
43
+ when the flow stores no secret (Ollama / env-var / skipped-key).
44
+
45
+ ## [0.12.0] - 2026-11-17
46
+
47
+ The retry-and-resume minor: the LLM transport's hardcoded rate-limit and 5xx
48
+ retry budgets become typed, operator-tunable `llm.*` knobs threaded end to
49
+ end into both agent loops, and an error-interrupted REPL turn keeps its tool
50
+ history and resumes on the same Loop instead of forcing the model to
51
+ re-discover terrain from a summary — while a robustness round protects the
52
+ storage config from redaction, rolls back failed persists, hardens the
53
+ sandbox drivers against catastrophic fallbacks, scrubs the MCP server
54
+ environment, sanitizes terminal-escape output, and fixes goal token
55
+ double-counting.
56
+
57
+ ### Added
58
+
59
+ - **Operator-tunable LLM retry budgets (`llm.max_retries`, `max_429_retries`,
60
+ `backoff_base`, `max_wall_clock`, `rate_limit_max_delay`):** the transport
61
+ layer's hardcoded rate-limit and transient-5xx constants become typed
62
+ defaults tunable globally or per provider (English and Turkish
63
+ configuration guides updated), threaded through the transport and both
64
+ agent loops (root loop and subagent step runner) via a helper that passes
65
+ only explicitly-set values so existing callers keep their built-in
66
+ defaults; the Retry-After delay clamp is configurable end to end with the
67
+ old ceiling preserved as the fallback.
68
+
69
+ ### Changed
70
+
71
+ - **Failed free-text turns merge their tool history into conversation memory
72
+ (#511):** a mid-run LLM error no longer drops the in-turn tool messages —
73
+ the path-independent merge contract is wired into all three failure rescue
74
+ bodies (LLM error, unexpected error, ^C) with the outcome text leading the
75
+ window.
76
+
77
+ ### Fixed
78
+
79
+ - **An error-interrupted free-text turn resumes on the same Loop (#511):** the
80
+ REPL stashes the surviving Loop and an exact-match resume intent
81
+ ("devam et"/"continue"/…) continues from the interrupted step counter with
82
+ a `<turn_resumed>` marker instead of spawning a fresh Loop; resume-intent
83
+ parsing is linear-time (CodeQL polynomial ReDoS) with a length-form slice
84
+ guard.
85
+ - **Fiber retry resurrection, goal token double-count, and resume
86
+ history/stash (#528):** the runtime retry path can no longer resurrect a
87
+ disposed fiber, goal tokens are counted once, and loop history cursors use
88
+ `ObjectSpace::WeakMap` so completed Loops are not retained.
89
+ - **Terminal escapes sanitized, conflict FrozenError, SSE UTF-8, router enum,
90
+ fence tags (#530):** a robustness sweep across ui/buffer/llm/repair.
91
+ - **MCP proxy collisions, server restart on re-add, env scrubbing, and
92
+ registry alias ownership (#527).**
93
+ - **Storage config protected from redaction, failed persists rolled back,
94
+ digit hash lookup and atomic_write fixed (#529):** session-only semantics
95
+ are preserved when a disk persist fails.
96
+ - **Catastrophic fallback regex enforced and sandbox drivers hardened
97
+ (#526):** `/$x`-style patterns are classified as system paths and
98
+ `resolved_path_under?` is reused.
99
+
9
100
  ## [0.11.0] - 2026-11-16
10
101
 
11
102
  The schema-grammar minor: the goal-ledger stops teaching its schema through
@@ -1729,7 +1820,9 @@ installation section.
1729
1820
  - Context: serialized index mutations, snapshot-outside-lock reconcile,
1730
1821
  streaming-first truncation.
1731
1822
 
1732
- [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.11.0...HEAD
1823
+ [Unreleased]: https://github.com/ozgurulukir/rubik/compare/v0.13.0...HEAD
1824
+ [0.13.0]: https://github.com/ozgurulukir/rubik/compare/v0.12.0...v0.13.0
1825
+ [0.12.0]: https://github.com/ozgurulukir/rubik/compare/v0.11.0...v0.12.0
1733
1826
  [0.11.0]: https://github.com/ozgurulukir/rubik/compare/v0.10.0...v0.11.0
1734
1827
  [0.10.0]: https://github.com/ozgurulukir/rubik/compare/v0.9.1...v0.10.0
1735
1828
  [0.9.1]: https://github.com/ozgurulukir/rubik/compare/v0.9.0...v0.9.1
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}"
@@ -183,7 +183,7 @@ module Rubik
183
183
  @buffer.delete(rel_path)
184
184
  @dirty_files.delete(rel_path)
185
185
  @original_hashes.delete(rel_path)
186
- @last_conflicts.delete(rel_path)
186
+ @last_conflicts = @last_conflicts.reject { |c| c == rel_path }.freeze
187
187
  end
188
188
 
189
189
  if from_disk
@@ -210,7 +210,7 @@ module Rubik
210
210
  @buffer.delete(rel_path)
211
211
  @dirty_files.delete(rel_path)
212
212
  @original_hashes.delete(rel_path)
213
- @last_conflicts.delete(rel_path)
213
+ @last_conflicts = @last_conflicts.reject { |c| c == rel_path }.freeze
214
214
  @event_bus&.emit(:buffer_discarded, path: rel_path)
215
215
  rel_path
216
216
  end
@@ -286,7 +286,7 @@ module Rubik
286
286
  rel = path.byteslice((root_size + 1)..-1)
287
287
  rel = rel.tr("\\", "/") if File::ALT_SEPARATOR
288
288
 
289
- if rel.start_with?(".git")
289
+ if rel == ".git" || rel.start_with?(".git/")
290
290
  Find.prune if File.directory?(path)
291
291
  next
292
292
  end
@@ -0,0 +1,51 @@
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 yalnızca kalıcı kapsamlara yazar; oturum-kapsamı için REPL içinde !provider add kullanın."
27
+ output.puts " rubik connect writes persistent scopes only; use !provider add in the REPL for a session-scoped provider."
28
+ return 1
29
+ end
30
+
31
+ scope = argv.include?("--project") || argv.include?("-p") ? :project : :global
32
+ config = Rubik::Config.new(root_dir: root_dir, home_dir: home_dir)
33
+ ui = Rubik::Connect::PromptUI.new(input: input, output: output,
34
+ theme: Rubik::UI::Theme.for(output, config), prompt: prompt)
35
+ wizard = Rubik::Connect::Wizard.new(config: config, ui: ui, io: output, scope: scope,
36
+ probe: probe,
37
+ catalog_loader: -> { Rubik::Connect::ModelsDev.load(home_dir: config.home_dir) })
38
+ result = wizard.run
39
+
40
+ if result[:reason] == :non_interactive
41
+ output.puts "Etkileşimsiz oturum — sihirbaz çalışamaz. 'rubik' ile REPL başlatıp !connect kullanın " \
42
+ "veya !provider add <name> [type] <url> [--key=...] sözdizimini uygulayın."
43
+ output.puts "Non-interactive session — start the REPL and run !connect, or use !provider add flags."
44
+ return 2
45
+ end
46
+ output.puts "Çıkmak için 'rubik' ile REPL'i başlatın / Start the REPL with 'rubik'." if result[:ok]
47
+ result[:ok] ? 0 : 1
48
+ end
49
+ end
50
+ end
51
+ end
@@ -7,7 +7,9 @@ module Rubik
7
7
  "security", "sandbox", "plugins", "mcp", "lsp", "project", "test", "linter", "adapter",
8
8
  # Provider specs carry credentials and endpoints; an untrusted
9
9
  # project must not be able to inject either.
10
- "providers"
10
+ "providers",
11
+ # Storage root/project slug must not be redirected by an untrusted workspace.
12
+ "storage"
11
13
  ].freeze
12
14
 
13
15
  private
@@ -5,19 +5,19 @@ require_relative "../platform"
5
5
  module Rubik
6
6
  class Config
7
7
  module Defaults
8
+ # llm.* transport-resilience fallbacks, reused by both orchestrator seams.
9
+ LLM_RETRY_DEFAULTS = { "max_retries" => 2, "max_429_retries" => 5, "backoff_base" => 0.2,
10
+ "max_wall_clock" => 120, "rate_limit_max_delay" => 60 }.freeze
11
+
8
12
  DEFAULT_CONFIG = {
9
13
  "llm" => {
10
- "stream" => true,
11
- "stream_timeout" => 120,
12
- "open_timeout" => 15,
13
- "default_max_output_tokens" => 4096,
14
- "capture_reasoning" => true,
15
- "auto_continue_on_length" => true,
16
- "max_auto_continues" => 3,
14
+ "stream" => true, "stream_timeout" => 120, "open_timeout" => 15, "default_max_output_tokens" => 4096,
15
+ "capture_reasoning" => true, "auto_continue_on_length" => true, "max_auto_continues" => 3,
17
16
  # Plan 90 bridge (loop.rb): nil = not sent; non-nil reasoning_effort also flips reasoning_model?.
18
17
  "thinking_budget_ratio" => nil, # Anthropic thinking budget share (0.0-0.8) -> opts[:thinking_budget_ratio]
19
18
  "reasoning_effort" => nil, # OpenAI reasoning effort "low"|"medium"|"high" -> opts[:reasoning_effort]
20
- "headers" => {}
19
+ "headers" => {},
20
+ **LLM_RETRY_DEFAULTS
21
21
  },
22
22
  "model" => { "provider" => "ollama", "name" => "qwen2.5-coder:7b", "endpoint" => "http://localhost:11434" },
23
23
  "models" => {
@@ -57,6 +57,7 @@ module Rubik
57
57
  "native" => "auto"
58
58
  },
59
59
  "router" => { "mode" => "auto", "sticky" => true },
60
+ "connect" => { "first_run_offer" => true }, # connect wizard first-run offer (repl/lifecycle.rb)
60
61
  "repair" => { "max_attempts" => 3 },
61
62
  "sandbox" => {
62
63
  "enabled" => false,
@@ -14,6 +14,13 @@ module Rubik
14
14
  "llm.capture_reasoning" => { type: :boolean },
15
15
  "llm.auto_continue_on_length" => { type: :boolean },
16
16
  "llm.max_auto_continues" => { type: :integer, min: 0 },
17
+ # HTTP transport retry budgets (llm/client.rb post_json). 0 disables
18
+ # a budget; backoff_base is seconds; max_wall_clock bounds all retries.
19
+ "llm.max_retries" => { type: :integer, min: 0 },
20
+ "llm.max_429_retries" => { type: :integer, min: 0 },
21
+ "llm.backoff_base" => { type: :float, min: 0.0 },
22
+ "llm.max_wall_clock" => { type: :integer, min: 0 },
23
+ "llm.rate_limit_max_delay" => { type: :integer, min: 0 },
17
24
  "llm.thinking_budget_ratio" => { type: :float, min: 0.0, max: 0.8, nullable: true },
18
25
  "llm.reasoning_effort" => { type: :enum, allowed: ["low", "medium", "high"], nullable: true },
19
26
  "agent.max_steps" => { type: :integer, min: 1 },
@@ -34,6 +41,7 @@ module Rubik
34
41
  "tools.native" => { type: :enum, allowed: ["auto", "on", "off"] },
35
42
  "router.mode" => { type: :enum, allowed: ["auto", "manual", "fast", "advanced"] },
36
43
  "router.sticky" => { type: :boolean },
44
+ "connect.first_run_offer" => { type: :boolean },
37
45
  "repair.max_attempts" => { type: :integer, min: 0 },
38
46
  "sandbox.enabled" => { type: :boolean },
39
47
  # Free-form: the driver registry is dynamically extensible (plugins
@@ -123,11 +123,20 @@ module Rubik
123
123
  # of an api_key path also drops its stored secret(s) — both scopes'
124
124
  # names, so neither keyring entry outlives its reference. Best-effort
125
125
  # — never masks res.
126
- def cleanup_stored_secret(key, res, persist)
127
- return unless secret_key?(key) && res[:ok] && persisting?(persist)
128
-
129
- Rubik::Security::SecretStore.delete(key.to_s)
130
- Rubik::Security::SecretStore.delete("#{key}.project")
126
+ def cleanup_stored_secret(key, res, persist, old_val: nil)
127
+ return unless res[:ok] && persisting?(persist)
128
+
129
+ keys_to_delete = if secret_key?(key)
130
+ [key.to_s]
131
+ elsif old_val.is_a?(Hash)
132
+ collect_secret_leaves(key, old_val).map(&:first)
133
+ else
134
+ []
135
+ end
136
+ keys_to_delete.each do |secret_path|
137
+ Rubik::Security::SecretStore.delete(secret_path)
138
+ Rubik::Security::SecretStore.delete("#{secret_path}.project")
139
+ end
131
140
  rescue Rubik::Security::SecretStore::Error
132
141
  nil
133
142
  end
data/lib/rubik/config.rb CHANGED
@@ -167,7 +167,7 @@ module Rubik
167
167
  notify_change(key, new_val, old_val)
168
168
  @event_bus&.emit(:config_deleted, key: key.to_s, old_val: old_val)
169
169
  end
170
- cleanup_stored_secret(key, res, persist)
170
+ cleanup_stored_secret(key, res, persist, old_val: old_val)
171
171
  res
172
172
  end
173
173
  end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "uri"
6
+
7
+ module Rubik
8
+ module Connect
9
+ # Minimal GET-only JSON transport for the connect wizard: catalog fetch
10
+ # and live endpoint probes. Deliberately probe-grade — short fixed
11
+ # timeouts, no retry budgets, no pooling; the retry-hardened POST path
12
+ # stays in LLM::HTTPClient. Never raises: outcome hashes only, so the
13
+ # wizard can degrade honestly (retry / save-anyway / cancel) instead of
14
+ # unwinding mid-question.
15
+ module Http
16
+ DEFAULT_TIMEOUT = 10
17
+ DEFAULT_OPEN_TIMEOUT = 5
18
+
19
+ module_function
20
+
21
+ # GET `url` and parse the body as JSON.
22
+ # -> {ok: true, status:, json:}
23
+ # | {ok: false, error:, status:, reason:, body:}
24
+ # reason: :invalid_url (non-http(s) scheme), :unreachable (transport),
25
+ # :http_error (non-2xx), :bad_response (2xx non-JSON).
26
+ def get_json(url, headers: {}, timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT, fetcher: nil)
27
+ uri = parse_url(url)
28
+ return uri unless uri.is_a?(URI::Generic) # parse failure outcome
29
+
30
+ response = perform(uri, headers: headers, timeout: timeout, open_timeout: open_timeout, fetcher: fetcher)
31
+ # Duck-typed: real responses and test doubles alike expose code/body;
32
+ # a Hash means the transport itself already failed (outcome hash).
33
+ return response unless response.respond_to?(:code) && response.respond_to?(:body)
34
+
35
+ status = response.code.to_i
36
+ body = response.body.to_s
37
+ return { ok: false, error: "HTTP #{status}", status: status, reason: :http_error, body: body } unless status.between?(200, 299)
38
+
39
+ json = parse_json(body)
40
+ return { ok: false, error: "invalid JSON response", status: status, reason: :bad_response } if json.nil?
41
+
42
+ { ok: true, status: status, json: json }
43
+ end
44
+
45
+ def parse_json(body)
46
+ JSON.parse(body)
47
+ rescue JSON::ParserError
48
+ nil
49
+ end
50
+
51
+ def parse_url(url)
52
+ parsed = begin
53
+ URI.parse(url.to_s)
54
+ rescue URI::Error
55
+ nil
56
+ end
57
+ return invalid_url_outcome if parsed.nil?
58
+ return invalid_url_outcome unless parsed.is_a?(URI::HTTP) || parsed.is_a?(URI::HTTPS)
59
+ return invalid_url_outcome if parsed.host.nil? || parsed.host.empty?
60
+
61
+ parsed
62
+ end
63
+
64
+ def invalid_url_outcome
65
+ { ok: false, error: "only http/https URLs are supported", status: nil, reason: :invalid_url }
66
+ end
67
+
68
+ def perform(uri, headers:, timeout:, open_timeout:, fetcher:)
69
+ if fetcher
70
+ fetcher.call(uri)
71
+ else
72
+ http = Net::HTTP.new(uri.host, uri.port)
73
+ http.use_ssl = uri.is_a?(URI::HTTPS)
74
+ http.open_timeout = open_timeout
75
+ http.read_timeout = timeout
76
+ request = Net::HTTP::Get.new(uri)
77
+ headers.each { |key, value| request[key] = value }
78
+ http.request(request)
79
+ end
80
+ rescue StandardError => e
81
+ { ok: false, error: "#{e.class}: #{e.message}", status: nil, reason: :unreachable }
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require_relative "http"
5
+
6
+ module Rubik
7
+ module Connect
8
+ # models.dev catalog integration: fetch the provider/model directory
9
+ # (a single literal URL — no user input reaches the request, so the
10
+ # fetch carries no SSRF surface), cache it under ~/.rubik/cache with a
11
+ # 24h TTL, degrade to the stale cache when the network fails, and
12
+ # classify providers into the shapes rubik's registry can speak
13
+ # (openai_compatible | anthropic | ollama-local).
14
+ module ModelsDev
15
+ CATALOG_URL = "https://models.dev/catalog.json"
16
+ CACHE_FILE = "models_dev_catalog.json"
17
+ TTL_SECONDS = 24 * 60 * 60
18
+ FETCH_TIMEOUT = 30 # the document is multi-MB JSON
19
+ # Mirrors LLM::Registry's per-model fallback so manual entries behave
20
+ # exactly like !provider add defaults.
21
+ DEFAULT_CONTEXT_LIMIT = 32_768
22
+
23
+ # Everyday providers first; the 300+ catalog tail stays behind the
24
+ # wizard's filter submenu. api is the offline fallback when the
25
+ # catalog is unreachable — online runs prefer the catalog's own api
26
+ # field (so catalog updates win over hardcoded bases).
27
+ PINNED = [
28
+ { id: "openai", label: "OpenAI", type: "openai_compatible", api: "https://api.openai.com/v1", catalog_id: "openai" },
29
+ { id: "anthropic", label: "Anthropic", type: "anthropic", api: "https://api.anthropic.com", catalog_id: "anthropic" },
30
+ { id: "openrouter", label: "OpenRouter", type: "openai_compatible", api: "https://openrouter.ai/api/v1", catalog_id: "openrouter" },
31
+ { id: "deepseek", label: "DeepSeek", type: "openai_compatible", api: "https://api.deepseek.com", catalog_id: "deepseek" },
32
+ { id: "groq", label: "Groq", type: "openai_compatible", api: "https://api.groq.com/openai/v1", catalog_id: "groq" },
33
+ { id: "mistral", label: "Mistral", type: "openai_compatible", api: "https://api.mistral.ai/v1", catalog_id: "mistral" },
34
+ { id: "xai", label: "xAI (Grok)", type: "openai_compatible", api: "https://api.x.ai/v1", catalog_id: "xai" },
35
+ # rubik has no native Gemini client; the documented OpenAI-compatible
36
+ # base speaks Bearer auth and /models — the catalog's native api
37
+ # field for "google" must NOT be used here.
38
+ { id: "gemini", label: "Google (Gemini)", type: "openai_compatible", api: "https://generativelanguage.googleapis.com/v1beta/openai", catalog_id: "google",
39
+ use_catalog_api: false },
40
+ { id: "ollama", label: "Ollama (yerel / local)", type: "ollama", api: "http://localhost:11434", catalog_id: nil }
41
+ ].freeze
42
+
43
+ module_function
44
+
45
+ # -> {catalog: Hash(provider_id => entry), source: :fresh|:cache|:stale|:none}
46
+ # Resolution order: valid cache -> stale-accepted cache (network
47
+ # failed) -> empty (:none). The cache envelope is
48
+ # {"fetched_at" => epoch, "providers" => {...}}; a corrupt file is a
49
+ # miss, never a raise.
50
+ def load(home_dir:, now: -> { Time.now.to_i }, fetcher: nil, ttl: TTL_SECONDS)
51
+ path = cache_path(home_dir)
52
+ cached = read_cache(path)
53
+
54
+ return { catalog: cached[:providers], source: :cache } if cached && (now.call.to_i - cached[:fetched_at]) < ttl
55
+
56
+ fetched = Http.get_json(CATALOG_URL, timeout: FETCH_TIMEOUT, fetcher: fetcher)
57
+ if fetched[:ok] && fetched[:json].is_a?(Hash) && fetched[:json]["providers"].is_a?(Hash)
58
+ write_cache(path, fetched[:json]["providers"], now.call.to_i)
59
+ { catalog: fetched[:json]["providers"], source: :fresh }
60
+ elsif cached
61
+ { catalog: cached[:providers], source: :stale }
62
+ else
63
+ { catalog: {}, source: :none }
64
+ end
65
+ end
66
+
67
+ def cache_path(home_dir)
68
+ File.join(home_dir.to_s, ".rubik", "cache", CACHE_FILE)
69
+ end
70
+
71
+ def read_cache(path)
72
+ parsed = JSON.parse(File.read(path))
73
+ providers = parsed["providers"]
74
+ fetched_at = parsed["fetched_at"]
75
+ return nil unless providers.is_a?(Hash) && fetched_at.is_a?(Numeric)
76
+
77
+ { providers: providers, fetched_at: fetched_at.to_i }
78
+ rescue StandardError
79
+ nil
80
+ end
81
+
82
+ # Atomic replace (tmp + rename) so a crash mid-write can never leave a
83
+ # half-JSON cache behind for the stale path to trip on. The pid
84
+ # suffix keeps two concurrent rubik processes from interleaving one
85
+ # fixed tmp file.
86
+ def write_cache(path, providers, fetched_at)
87
+ dir = File.dirname(path)
88
+ FileUtils.mkdir_p(dir)
89
+ tmp = "#{path}.#{Process.pid}.tmp"
90
+ File.write(tmp, JSON.generate({ "fetched_at" => fetched_at, "providers" => providers }))
91
+ File.rename(tmp, path)
92
+ rescue StandardError
93
+ nil
94
+ end
95
+
96
+ # True when the catalog entry speaks an OpenAI-compatible API rubik
97
+ # can drive: explicit npm package, or an api URL with a compatible
98
+ # path shape (versioned / OpenAI / compatible / PaaS) and not an
99
+ # /anthropic/ path. Entries without any api URL are dropped — the
100
+ # submenu can't prefill a base URL for them (they remain reachable
101
+ # via the custom-endpoint branch).
102
+ def openai_compatible?(entry)
103
+ return false unless entry.is_a?(Hash)
104
+
105
+ npm = entry["npm"].to_s
106
+ return true if ["@ai-sdk/openai-compatible", "@ai-sdk/openai"].include?(npm)
107
+
108
+ api = entry["api"].to_s
109
+ return false if api.empty? || api.include?("/anthropic")
110
+
111
+ api.match?(%r{/v\d+|/openai|/compatible|/paas/})
112
+ end
113
+
114
+ # Catalog entries the submenu offers, sorted by display name.
115
+ # -> [{id:, name:, api:, entry:}]
116
+ def compatible_providers(catalog)
117
+ return [] unless catalog.is_a?(Hash)
118
+
119
+ compatible = catalog.filter_map do |id, entry|
120
+ next unless openai_compatible?(entry)
121
+
122
+ { id: id.to_s, name: entry["name"].to_s.empty? ? id.to_s : entry["name"].to_s,
123
+ api: entry["api"], entry: entry }
124
+ end
125
+ compatible.sort_by { |p| p[:name].downcase }
126
+ end
127
+
128
+ # Models of one catalog provider, best first: widest context on top,
129
+ # tool-capable models ahead of plain ones at equal width.
130
+ # -> [{id:, name:, context_limit:, output_limit:, tool_call:}]
131
+ def models_for(catalog, provider_id)
132
+ entry = catalog.is_a?(Hash) ? catalog[provider_id] : nil
133
+ return [] unless entry.is_a?(Hash) && entry["models"].is_a?(Hash)
134
+
135
+ entries = entry["models"].filter_map do |id, m|
136
+ next unless m.is_a?(Hash)
137
+
138
+ limit = m["limit"].is_a?(Hash) ? m["limit"] : {}
139
+ { id: id.to_s, name: m["name"].to_s.empty? ? id.to_s : m["name"].to_s,
140
+ context_limit: limit["context"].is_a?(Numeric) ? limit["context"].to_i : nil,
141
+ output_limit: limit["output"].is_a?(Numeric) ? limit["output"].to_i : nil,
142
+ tool_call: m["tool_call"] == true }
143
+ end
144
+ entries.sort_by { |m| [m[:context_limit].to_i * -1, m[:name].downcase] }
145
+ end
146
+
147
+ # The pre-highlighted model choice: widest tool-capable context wins;
148
+ # falls back to the first entry. Models are already context-sorted.
149
+ def default_model(models)
150
+ models.find { |m| m[:tool_call] } || models.first
151
+ end
152
+
153
+ # Sanitizes a catalog/display name into a providers.<id> storage key
154
+ # (downcase, alnum/dash/underscore only). Mirrors the downcasing the
155
+ # registry applies when registering provider specs. The trim stays
156
+ # linear (delete_prefix/suffix) — regex-based end-trimming on
157
+ # uncontrolled catalog names triggers CodeQL's polynomial-ReDoS check.
158
+ def provider_key(name)
159
+ key = name.to_s.downcase.gsub(/[^a-z0-9_]+/, "-")
160
+ key.delete_prefix("-").delete_suffix("-")
161
+ end
162
+ end
163
+ end
164
+ end