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
@@ -0,0 +1,96 @@
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]} — anahtar reddedildi / key rejected" }
79
+ elsif NO_LISTING_STATUSES.include?(res[:status])
80
+ { ok: true, auth_failed: false, reachable: true, models: [],
81
+ note: "model listesi yayınlanmıyor (HTTP #{res[:status]}) — model adını elle girin / no model listing; enter the name manually" }
82
+ else
83
+ { ok: false, auth_failed: false, reachable: true, models: [], note: "HTTP #{res[:status]}" }
84
+ end
85
+ when :bad_response
86
+ { ok: false, auth_failed: false, reachable: true, models: [], note: res[:error].to_s }
87
+ else # 2xx JSON
88
+ models = yield(res[:json])
89
+ { ok: true, auth_failed: false, reachable: true, models: models.is_a?(Array) ? models : [] }
90
+ end
91
+ rescue StandardError => e
92
+ { ok: false, auth_failed: false, reachable: true, models: [], note: "#{e.class}: #{e.message}" }
93
+ end
94
+ end
95
+ end
96
+ 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