rubik 0.12.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +71 -1
  4. data/bin/rubik +11 -1
  5. data/lib/rubik/ast/c_extractor.rb +1 -1
  6. data/lib/rubik/ast/graph.rb +38 -57
  7. data/lib/rubik/ast/graph_extensions.rb +57 -15
  8. data/lib/rubik/ast/ruby_extractor.rb +4 -0
  9. data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
  10. data/lib/rubik/buffer/manager.rb +33 -11
  11. data/lib/rubik/buffer/snapshot.rb +23 -3
  12. data/lib/rubik/cli/connect.rb +49 -0
  13. data/lib/rubik/config/defaults.rb +4 -7
  14. data/lib/rubik/config/env_guard.rb +43 -0
  15. data/lib/rubik/config/schema.rb +8 -1
  16. data/lib/rubik/config/secret_policy.rb +6 -5
  17. data/lib/rubik/config.rb +1 -1
  18. data/lib/rubik/connect/http.rb +85 -0
  19. data/lib/rubik/connect/models_dev.rb +173 -0
  20. data/lib/rubik/connect/probes.rb +103 -0
  21. data/lib/rubik/connect/prompt_ui.rb +139 -0
  22. data/lib/rubik/connect/wizard.rb +540 -0
  23. data/lib/rubik/connect.rb +7 -0
  24. data/lib/rubik/context/repo_context_reader.rb +8 -4
  25. data/lib/rubik/llm/chunk_assembler.rb +12 -1
  26. data/lib/rubik/llm/formatter.rb +14 -0
  27. data/lib/rubik/llm/openai_compatible.rb +4 -1
  28. data/lib/rubik/llm/provider_key.rb +32 -0
  29. data/lib/rubik/llm/registry.rb +18 -2
  30. data/lib/rubik/llm/router.rb +19 -1
  31. data/lib/rubik/llm/session_header.rb +128 -0
  32. data/lib/rubik/llm/sse_parser.rb +57 -10
  33. data/lib/rubik/lsp/client.rb +11 -5
  34. data/lib/rubik/lsp/transport.rb +17 -7
  35. data/lib/rubik/mcp/transport/stdio.rb +42 -4
  36. data/lib/rubik/orchestrator/continuation_manager.rb +14 -5
  37. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
  38. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
  39. data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
  40. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
  41. data/lib/rubik/orchestrator/goal_session.rb +4 -4
  42. data/lib/rubik/orchestrator/loop.rb +8 -11
  43. data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
  44. data/lib/rubik/orchestrator/step_executor.rb +17 -2
  45. data/lib/rubik/orchestrator/subagent/instance.rb +24 -1
  46. data/lib/rubik/orchestrator/subagent/notices.rb +1 -1
  47. data/lib/rubik/orchestrator/subagent/reaper.rb +21 -2
  48. data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
  49. data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
  50. data/lib/rubik/orchestrator/subagent/supervisor.rb +10 -3
  51. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +27 -5
  52. data/lib/rubik/orchestrator/turn_executor.rb +24 -8
  53. data/lib/rubik/platform/filesystem.rb +52 -24
  54. data/lib/rubik/plugin/storage.rb +20 -0
  55. data/lib/rubik/prompt/context_builder.rb +2 -1
  56. data/lib/rubik/repair/loop.rb +22 -2
  57. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  58. data/lib/rubik/repl/commands/agent_execution.rb +20 -15
  59. data/lib/rubik/repl/commands/buffer.rb +15 -0
  60. data/lib/rubik/repl/commands/connect.rb +51 -0
  61. data/lib/rubik/repl/commands/lsp.rb +1 -1
  62. data/lib/rubik/repl/commands/plugins.rb +31 -31
  63. data/lib/rubik/repl/commands/session.rb +5 -5
  64. data/lib/rubik/repl/commands/workspace.rb +4 -4
  65. data/lib/rubik/repl/commands.rb +2 -0
  66. data/lib/rubik/repl/input_execution.rb +30 -29
  67. data/lib/rubik/repl/input_loop.rb +10 -4
  68. data/lib/rubik/repl/lifecycle.rb +38 -0
  69. data/lib/rubik/repl/notice_router.rb +11 -5
  70. data/lib/rubik/repl/paste_support.rb +1 -1
  71. data/lib/rubik/repl/prompt_grace.rb +3 -3
  72. data/lib/rubik/repl/shell.rb +10 -2
  73. data/lib/rubik/repl/turn_recorder.rb +14 -3
  74. data/lib/rubik/runtime/context.rb +33 -27
  75. data/lib/rubik/runtime/manager.rb +23 -15
  76. data/lib/rubik/sandbox/driver/bubblewrap.rb +58 -8
  77. data/lib/rubik/sandbox/driver/process.rb +32 -1
  78. data/lib/rubik/sandbox/runner.rb +9 -1
  79. data/lib/rubik/security/classifier/classifier.rb +147 -13
  80. data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
  81. data/lib/rubik/security/classifier/commands/git.yaml +27 -1
  82. data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
  83. data/lib/rubik/security/classifier/database.rb +2 -2
  84. data/lib/rubik/security/command_gate.rb +8 -1
  85. data/lib/rubik/security/redactor.rb +62 -19
  86. data/lib/rubik/session/recorder.rb +13 -18
  87. data/lib/rubik/session/replayer.rb +29 -7
  88. data/lib/rubik/storage/store.rb +19 -5
  89. data/lib/rubik/tools/builtins/exec.rb +1 -1
  90. data/lib/rubik/tools/builtins/files.rb +8 -12
  91. data/lib/rubik/tools/builtins/grep.rb +3 -2
  92. data/lib/rubik/tools/builtins/lsp.rb +31 -7
  93. data/lib/rubik/tools/builtins/show_file.rb +1 -1
  94. data/lib/rubik/tools/builtins/synthesis.rb +4 -4
  95. data/lib/rubik/tools/result_normalizer.rb +1 -1
  96. data/lib/rubik/ui/banner.rb +3 -3
  97. data/lib/rubik/ui/dialog.rb +10 -7
  98. data/lib/rubik/ui/diff_viewer.rb +4 -4
  99. data/lib/rubik/ui/frame.rb +13 -3
  100. data/lib/rubik/ui/markdown_stream.rb +8 -2
  101. data/lib/rubik/ui/panel.rb +11 -11
  102. data/lib/rubik/ui/renderer.rb +6 -6
  103. data/lib/rubik/ui/spinner.rb +1 -1
  104. data/lib/rubik/ui/statusline.rb +3 -3
  105. data/lib/rubik/ui/theme.rb +1 -1
  106. data/lib/rubik/version.rb +1 -1
  107. metadata +12 -1
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rubik
4
+ class Config
5
+ # Issue #561: typed-value validation shared by explicit writes and the
6
+ # boot-time env layer, plus the fail-soft env guard. Kept out of
7
+ # defaults.rb so the schema/default tables stay inside the file-size
8
+ # hygiene limit.
9
+ module EnvGuard
10
+ private
11
+
12
+ # Explicit writes (Config#set / Scoped#set) keep strict validation;
13
+ # boot-time env validation passes strict: false so a malformed RUBIK_*
14
+ # variable is dropped with a warning instead of crashing Config.new.
15
+ def validate_typed_value!(key, value, strict: true)
16
+ return true unless Schema::DEFINITIONS.key?(key.to_s)
17
+
18
+ Schema.validate_value!(key, value, strict: strict)
19
+ end
20
+
21
+ # Walk the env layer's typed leaves and remove any schema-backed value
22
+ # that fails its spec, recording a warning instead of raising. Mutates
23
+ # and returns the layer so the merged config never sees the rejected
24
+ # value (and cannot resurrect it on a later set).
25
+ def validate_env_layer!(layer, warnings, prefix = "")
26
+ return layer unless layer.is_a?(Hash)
27
+
28
+ layer.each_key do |key|
29
+ full = prefix.empty? ? key.to_s : "#{prefix}.#{key}"
30
+ val = layer[key]
31
+ if val.is_a?(Hash)
32
+ validate_env_layer!(val, warnings, full)
33
+ elsif Schema::DEFINITIONS.key?(full) && !validate_typed_value!(full, val, strict: false)
34
+ warnings << "[CONFIG] Ignoring invalid environment value '#{full}=#{val.inspect}'; " \
35
+ "using the configured default instead."
36
+ layer.delete(key)
37
+ end
38
+ end
39
+ layer
40
+ end
41
+ end
42
+ end
43
+ end
@@ -41,6 +41,7 @@ module Rubik
41
41
  "tools.native" => { type: :enum, allowed: ["auto", "on", "off"] },
42
42
  "router.mode" => { type: :enum, allowed: ["auto", "manual", "fast", "advanced"] },
43
43
  "router.sticky" => { type: :boolean },
44
+ "connect.first_run_offer" => { type: :boolean },
44
45
  "repair.max_attempts" => { type: :integer, min: 0 },
45
46
  "sandbox.enabled" => { type: :boolean },
46
47
  # Free-form: the driver registry is dynamically extensible (plugins
@@ -165,7 +166,13 @@ module Rubik
165
166
  when :integer
166
167
  val.strip =~ /^-?\d+$/ ? val.strip.to_i : val
167
168
  when :float
168
- val.strip =~ /^-?\d+(\.\d+)?$/ ? val.strip.to_f : val
169
+ # Accept leading-dot (.2), trailing-dot (2.) and exponent (1e-3,
170
+ # -2.5E+2) forms that Ruby's Float() understands.
171
+ val.strip =~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/ ? val.strip.to_f : val
172
+ when :array
173
+ # Comma-separated env values (e.g. "ls,cat") become a typed list;
174
+ # blank entries are dropped so trailing commas stay harmless.
175
+ val.strip.split(",").map(&:strip).reject(&:empty?)
169
176
  when :enum
170
177
  val.strip.downcase
171
178
  else
@@ -120,9 +120,10 @@ module Rubik
120
120
  end
121
121
 
122
122
  # Keyring hygiene for Config#delete: a successfully persisted delete
123
- # of an api_key path also drops its stored secret(s) — both scopes'
124
- # names, so neither keyring entry outlives its reference. Best-effort
125
- # — never masks res.
123
+ # of an api_key path also drops the stored secret for the scope being
124
+ # removed. The sibling scope's keyring entry is deliberately left
125
+ # intact — its config pointer still lives in the other layer and must
126
+ # keep resolving. Best-effort — never masks res.
126
127
  def cleanup_stored_secret(key, res, persist, old_val: nil)
127
128
  return unless res[:ok] && persisting?(persist)
128
129
 
@@ -133,9 +134,9 @@ module Rubik
133
134
  else
134
135
  []
135
136
  end
137
+ scope = persist.to_s.downcase.to_sym == :project ? :project : :global
136
138
  keys_to_delete.each do |secret_path|
137
- Rubik::Security::SecretStore.delete(secret_path)
138
- Rubik::Security::SecretStore.delete("#{secret_path}.project")
139
+ Rubik::Security::SecretStore.delete(secret_name(secret_path, scope))
139
140
  end
140
141
  rescue Rubik::Security::SecretStore::Error
141
142
  nil
data/lib/rubik/config.rb CHANGED
@@ -296,7 +296,7 @@ module Rubik
296
296
  @project_layer = filter_untrusted_project_config(@project_layer)
297
297
  end
298
298
 
299
- @env_layer = EnvLoader.load_env(ENV, DEFAULT_CONFIG, @warnings)
299
+ @env_layer = validate_env_layer!(EnvLoader.load_env(ENV, DEFAULT_CONFIG, @warnings), @warnings)
300
300
  @session_layer = {}
301
301
 
302
302
  warn_plaintext_project_secrets
@@ -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,173 @@
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
+ # The models.dev catalog's own api for "opencode" is the CONSOLE
41
+ # endpoint (https://opencode.ai/inference/openai/v1), not the Go
42
+ # gateway — hence use_catalog_api: false (same pattern as Google).
43
+ # The /v1 tail is REQUIRED: normalize_endpoint only appends
44
+ # /chat/completions to a versioned tail, and a bare /openai tail
45
+ # would spell /openai/chat/completions (a 404 there). POSTs hard-400
46
+ # with MissingSessionID until providers.<key>.session_header carries
47
+ # x-opencode-session — the wizard collects it (ask_session_header).
48
+ { id: "opencode", label: "opencode (Go)", type: "openai_compatible", api: "https://opencode.ai/inference/go/openai/v1", catalog_id: "opencode", use_catalog_api: false },
49
+ { id: "ollama", label: "Ollama (yerel / local)", type: "ollama", api: "http://localhost:11434", catalog_id: nil }
50
+ ].freeze
51
+
52
+ module_function
53
+
54
+ # -> {catalog: Hash(provider_id => entry), source: :fresh|:cache|:stale|:none}
55
+ # Resolution order: valid cache -> stale-accepted cache (network
56
+ # failed) -> empty (:none). The cache envelope is
57
+ # {"fetched_at" => epoch, "providers" => {...}}; a corrupt file is a
58
+ # miss, never a raise.
59
+ def load(home_dir:, now: -> { Time.now.to_i }, fetcher: nil, ttl: TTL_SECONDS)
60
+ path = cache_path(home_dir)
61
+ cached = read_cache(path)
62
+
63
+ return { catalog: cached[:providers], source: :cache } if cached && (now.call.to_i - cached[:fetched_at]) < ttl
64
+
65
+ fetched = Http.get_json(CATALOG_URL, timeout: FETCH_TIMEOUT, fetcher: fetcher)
66
+ if fetched[:ok] && fetched[:json].is_a?(Hash) && fetched[:json]["providers"].is_a?(Hash)
67
+ write_cache(path, fetched[:json]["providers"], now.call.to_i)
68
+ { catalog: fetched[:json]["providers"], source: :fresh }
69
+ elsif cached
70
+ { catalog: cached[:providers], source: :stale }
71
+ else
72
+ { catalog: {}, source: :none }
73
+ end
74
+ end
75
+
76
+ def cache_path(home_dir)
77
+ File.join(home_dir.to_s, ".rubik", "cache", CACHE_FILE)
78
+ end
79
+
80
+ def read_cache(path)
81
+ parsed = JSON.parse(File.read(path))
82
+ providers = parsed["providers"]
83
+ fetched_at = parsed["fetched_at"]
84
+ return nil unless providers.is_a?(Hash) && fetched_at.is_a?(Numeric)
85
+
86
+ { providers: providers, fetched_at: fetched_at.to_i }
87
+ rescue StandardError
88
+ nil
89
+ end
90
+
91
+ # Atomic replace (tmp + rename) so a crash mid-write can never leave a
92
+ # half-JSON cache behind for the stale path to trip on. The pid
93
+ # suffix keeps two concurrent rubik processes from interleaving one
94
+ # fixed tmp file.
95
+ def write_cache(path, providers, fetched_at)
96
+ dir = File.dirname(path)
97
+ FileUtils.mkdir_p(dir)
98
+ tmp = "#{path}.#{Process.pid}.tmp"
99
+ File.write(tmp, JSON.generate({ "fetched_at" => fetched_at, "providers" => providers }))
100
+ File.rename(tmp, path)
101
+ rescue StandardError
102
+ nil
103
+ end
104
+
105
+ # True when the catalog entry speaks an OpenAI-compatible API rubik
106
+ # can drive: explicit npm package, or an api URL with a compatible
107
+ # path shape (versioned / OpenAI / compatible / PaaS) and not an
108
+ # /anthropic/ path. Entries without any api URL are dropped — the
109
+ # submenu can't prefill a base URL for them (they remain reachable
110
+ # via the custom-endpoint branch).
111
+ def openai_compatible?(entry)
112
+ return false unless entry.is_a?(Hash)
113
+
114
+ npm = entry["npm"].to_s
115
+ return true if ["@ai-sdk/openai-compatible", "@ai-sdk/openai"].include?(npm)
116
+
117
+ api = entry["api"].to_s
118
+ return false if api.empty? || api.include?("/anthropic")
119
+
120
+ api.match?(%r{/v\d+|/openai|/compatible|/paas/})
121
+ end
122
+
123
+ # Catalog entries the submenu offers, sorted by display name.
124
+ # -> [{id:, name:, api:, entry:}]
125
+ def compatible_providers(catalog)
126
+ return [] unless catalog.is_a?(Hash)
127
+
128
+ compatible = catalog.filter_map do |id, entry|
129
+ next unless openai_compatible?(entry)
130
+
131
+ { id: id.to_s, name: entry["name"].to_s.empty? ? id.to_s : entry["name"].to_s,
132
+ api: entry["api"], entry: entry }
133
+ end
134
+ compatible.sort_by { |p| p[:name].downcase }
135
+ end
136
+
137
+ # Models of one catalog provider, best first: widest context on top,
138
+ # tool-capable models ahead of plain ones at equal width.
139
+ # -> [{id:, name:, context_limit:, output_limit:, tool_call:}]
140
+ def models_for(catalog, provider_id)
141
+ entry = catalog.is_a?(Hash) ? catalog[provider_id] : nil
142
+ return [] unless entry.is_a?(Hash) && entry["models"].is_a?(Hash)
143
+
144
+ entries = entry["models"].filter_map do |id, m|
145
+ next unless m.is_a?(Hash)
146
+
147
+ limit = m["limit"].is_a?(Hash) ? m["limit"] : {}
148
+ { id: id.to_s, name: m["name"].to_s.empty? ? id.to_s : m["name"].to_s,
149
+ context_limit: limit["context"].is_a?(Numeric) ? limit["context"].to_i : nil,
150
+ output_limit: limit["output"].is_a?(Numeric) ? limit["output"].to_i : nil,
151
+ tool_call: m["tool_call"] == true }
152
+ end
153
+ entries.sort_by { |m| [m[:context_limit].to_i * -1, m[:name].downcase] }
154
+ end
155
+
156
+ # The pre-highlighted model choice: widest tool-capable context wins;
157
+ # falls back to the first entry. Models are already context-sorted.
158
+ def default_model(models)
159
+ models.find { |m| m[:tool_call] } || models.first
160
+ end
161
+
162
+ # Sanitizes a catalog/display name into a providers.<id> storage key
163
+ # (downcase, alnum/dash/underscore only). Mirrors the downcasing the
164
+ # registry applies when registering provider specs. The trim stays
165
+ # linear (delete_prefix/suffix) — regex-based end-trimming on
166
+ # uncontrolled catalog names triggers CodeQL's polynomial-ReDoS check.
167
+ def provider_key(name)
168
+ key = name.to_s.downcase.gsub(/[^a-z0-9_]+/, "-")
169
+ key.delete_prefix("-").delete_suffix("-")
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "http"
4
+
5
+ module Rubik
6
+ module Connect
7
+ # Live endpoint validation for the connect wizard: one cheap GET per
8
+ # provider family, with the API key supplied — so a typo'd credential
9
+ # fails here (with a re-ask loop) instead of on the first real turn.
10
+ # No retries: the wizard owns the retry/save-anyway/cancel menu.
11
+ module Probes
12
+ TIMEOUT = 8
13
+ OLLAMA_TIMEOUT = 4
14
+ AUTH_STATUSES = [401, 403].freeze
15
+ NO_LISTING_STATUSES = [404, 405].freeze
16
+
17
+ module_function
18
+
19
+ # Outcome shape shared by all probes:
20
+ # ok: true -> endpoint answered and (when possible) listed models
21
+ # auth_failed: key was explicitly rejected (401/403)
22
+ # reachable: the host answered at all (network + TLS succeeded)
23
+ # models: live model ids when the endpoint lists them
24
+ # note: honest detail for the wizard's report
25
+ # The api_key never appears in `note`.
26
+
27
+ def openai_compatible(base_url, api_key: nil, timeout: TIMEOUT, fetcher: nil)
28
+ res = Http.get_json(models_url(base_url), headers: bearer_headers(api_key), timeout: timeout, fetcher: fetcher)
29
+ classify(res) { |json| model_ids(json) }
30
+ end
31
+
32
+ # Anthropic exposes a free GET /v1/models on the fixed API base (the
33
+ # provider class has no endpoint parameter, so a custom gateway base
34
+ # cannot be probed here — custom gateways belong to the
35
+ # openai_compatible branch anyway).
36
+ def anthropic(api_key:, timeout: TIMEOUT, fetcher: nil)
37
+ res = Http.get_json("https://api.anthropic.com/v1/models",
38
+ headers: { "x-api-key" => api_key.to_s, "anthropic-version" => "2023-06-01" },
39
+ timeout: timeout, fetcher: fetcher)
40
+ classify(res) { |json| model_ids(json) }
41
+ end
42
+
43
+ # Ollama's /api/tags doubles as the liveness check AND the installed
44
+ # model list — the wizard's model menu is built from it.
45
+ def ollama(host, timeout: OLLAMA_TIMEOUT, fetcher: nil)
46
+ res = Http.get_json("#{host.to_s.delete_suffix('/')}/api/tags", timeout: timeout, fetcher: fetcher)
47
+ classify(res) do |json|
48
+ json["models"].filter_map { |m| m["name"] if m.is_a?(Hash) }
49
+ end
50
+ end
51
+
52
+ # REST root for the model listing: OpenAI-compatible bases end in a
53
+ # version segment or the Gemini-compat "/openai" tail; those take
54
+ # /models directly, bare hosts get the conventional /v1/models.
55
+ def models_url(base_url)
56
+ base = base_url.to_s.delete_suffix("/")
57
+ if base.match?(%r{/v\d+\z}) || base.end_with?("/openai")
58
+ "#{base}/models"
59
+ else
60
+ "#{base}/v1/models"
61
+ end
62
+ end
63
+
64
+ def bearer_headers(api_key)
65
+ api_key.to_s.empty? ? {} : { "Authorization" => "Bearer #{api_key}" }
66
+ end
67
+
68
+ def model_ids(json)
69
+ Array(json["data"]).filter_map { |m| m["id"] if m.is_a?(Hash) }
70
+ end
71
+
72
+ def classify(res)
73
+ case res[:reason]
74
+ when :invalid_url, :unreachable
75
+ { ok: false, auth_failed: false, reachable: false, models: [], note: res[:error].to_s }
76
+ when :http_error
77
+ if AUTH_STATUSES.include?(res[:status])
78
+ { ok: false, auth_failed: true, reachable: true, models: [], note: "HTTP #{res[:status]} — key rejected" }
79
+ elsif NO_LISTING_STATUSES.include?(res[:status])
80
+ { ok: true, auth_failed: false, reachable: true, models: [],
81
+ note: "no model listing (HTTP #{res[:status]}) — enter the name manually" }
82
+ elsif res[:status] == 400
83
+ # Honest scope: the 400 was observed on THIS GET probe; the
84
+ # session-header part is a "commonly means" hint, not a settled
85
+ # diagnosis — the wizard's failure menu owns the next move.
86
+ { ok: false, auth_failed: false, reachable: true, models: [],
87
+ note: "HTTP 400 — request-shape rejection; commonly means a gateway rejecting requests missing a session header " \
88
+ "(e.g. opencode: x-opencode-session) — not an auth failure" }
89
+ else
90
+ { ok: false, auth_failed: false, reachable: true, models: [], note: "HTTP #{res[:status]}" }
91
+ end
92
+ when :bad_response
93
+ { ok: false, auth_failed: false, reachable: true, models: [], note: res[:error].to_s }
94
+ else # 2xx JSON
95
+ models = yield(res[:json])
96
+ { ok: true, auth_failed: false, reachable: true, models: models.is_a?(Array) ? models : [] }
97
+ end
98
+ rescue StandardError => e
99
+ { ok: false, auth_failed: false, reachable: true, models: [], note: "#{e.class}: #{e.message}" }
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tty-prompt"
4
+ require_relative "../ui/capability"
5
+ require_relative "../ui/theme"
6
+ require_relative "../repl/interrupt_handler"
7
+
8
+ module Rubik
9
+ module Connect
10
+ # Prompt port for the connect wizard: the exact TTY::Prompt operations
11
+ # the flow needs (select / ask / secret / yes?), with UI::Dialog's
12
+ # sentinel contract (:cancelled / :non_interactive) and the same
13
+ # injectable-prompt test seam. Lives outside UI::Dialog because the
14
+ # wizard runs shell-less (rubik connect boots no Shell) and needs
15
+ # typed prompts (masked key entry, yes/no) Dialog does not offer.
16
+ class PromptUI
17
+ CANCELLED = :cancelled
18
+ NON_INTERACTIVE = :non_interactive
19
+
20
+ attr_reader :theme
21
+
22
+ def initialize(input:, output:, theme: nil, prompt: nil)
23
+ @input = input
24
+ @output = output
25
+ @theme = theme || Rubik::UI::Theme.for(output, nil)
26
+ # Injected prompt => forced-interactive (tests); same seam as Dialog.
27
+ @prompt = prompt
28
+ end
29
+
30
+ def interactive?
31
+ return true if @prompt
32
+
33
+ Rubik::UI::Capability.interactive?(input: @input, output: @output)
34
+ end
35
+
36
+ # Single-choice menu; `filter: true` lets users type to narrow long
37
+ # lists (the 300+ catalog tail depends on it). Returns the chosen
38
+ # String or a sentinel.
39
+ def select(question, choices, default: nil)
40
+ raw do |p|
41
+ # tty-prompt select defaults are 1-based; 0 raises out of range.
42
+ default_index = choices.index(default).to_i + 1
43
+ p.select(question, choices, filter: true, per_page: 10, cycle: true,
44
+ default: default_index)
45
+ end
46
+ end
47
+
48
+ # Free-text question; empty input accepts `default` when given.
49
+ def ask(question, default: nil)
50
+ raw do |p|
51
+ if default.nil?
52
+ p.ask(question)
53
+ else
54
+ # default rides both channels: the option (consumed by injected
55
+ # test doubles) and the TTY modifier block (real prompts) —
56
+ # TTY treats them as equivalent settings, idempotent to double-apply.
57
+ p.ask(question, default: default.to_s) { |q| q.default(default.to_s) }
58
+ end
59
+ end
60
+ end
61
+
62
+ # Masked credential entry (no echo). Real TTY::Prompt exposes #mask;
63
+ # injected test doubles implement the wizard-level #secret.
64
+ def secret(question)
65
+ raw do |p|
66
+ if p.respond_to?(:mask)
67
+ p.mask(question)
68
+ else
69
+ p.secret(question)
70
+ end
71
+ end
72
+ end
73
+
74
+ def yes?(question, default: true)
75
+ raw { |p| p.yes?(question, default: default) { |q| q.default(default) } }
76
+ end
77
+
78
+ # Plain line output for notices/reports; never a sentinel source.
79
+ def puts(line)
80
+ @output.puts line
81
+ end
82
+
83
+ # Theme-aware text styling for wizard output (frames, success/error
84
+ # lines); degrades to the raw text off color-capable terminals.
85
+ def paint(text, style)
86
+ @theme.paint(text, style)
87
+ end
88
+
89
+ # Wraps a blocking fetch in a live spinner on capable terminals and
90
+ # stays silent elsewhere; always yields.
91
+ def spinner(message)
92
+ if Rubik::UI::Capability.spinner?(@output)
93
+ spin_out = Rubik::UI::Capability.unwrap_tee(@output) || $stdout
94
+ spin = TTY::Spinner.new("[:spinner] #{message}", format: :spin, hide_cursor: true, clear: true,
95
+ output: spin_out)
96
+ spin.auto_spin
97
+ begin
98
+ yield
99
+ ensure
100
+ spin.stop
101
+ end
102
+ else
103
+ yield
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ # The one sentinel funnel: every prompt operation degrades to
110
+ # :non_interactive off a live TTY and to :cancelled on interrupt or
111
+ # prompt failure, restoring the console either way (Dialog
112
+ # discipline). A swallowed non-interrupt error is never silent —
113
+ # debug_warn names it so a prompt-layer bug cannot pose as a user
114
+ # cancellation.
115
+ def raw
116
+ return NON_INTERACTIVE unless interactive?
117
+
118
+ yield(@prompt || build_prompt)
119
+ rescue Interrupt
120
+ CANCELLED
121
+ rescue StandardError => e
122
+ Rubik::Platform.debug_warn("connect prompt failed (#{e.class}: #{e.message}) — treated as cancel")
123
+ CANCELLED
124
+ ensure
125
+ restore_console
126
+ end
127
+
128
+ def build_prompt
129
+ TTY::Prompt.new(input: @input, output: Rubik::UI::Capability.unwrap_tee(@output) || $stdout)
130
+ end
131
+
132
+ def restore_console
133
+ Rubik::REPL::InterruptHandler.restore_console_after_interrupt
134
+ rescue StandardError
135
+ nil
136
+ end
137
+ end
138
+ end
139
+ end