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.
- checksums.yaml +4 -4
- data/ATTRIBUTION.md +1 -1
- data/CHANGELOG.md +71 -1
- data/bin/rubik +11 -1
- data/lib/rubik/ast/c_extractor.rb +1 -1
- data/lib/rubik/ast/graph.rb +38 -57
- data/lib/rubik/ast/graph_extensions.rb +57 -15
- data/lib/rubik/ast/ruby_extractor.rb +4 -0
- data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
- data/lib/rubik/buffer/manager.rb +33 -11
- data/lib/rubik/buffer/snapshot.rb +23 -3
- data/lib/rubik/cli/connect.rb +49 -0
- data/lib/rubik/config/defaults.rb +4 -7
- data/lib/rubik/config/env_guard.rb +43 -0
- data/lib/rubik/config/schema.rb +8 -1
- data/lib/rubik/config/secret_policy.rb +6 -5
- data/lib/rubik/config.rb +1 -1
- data/lib/rubik/connect/http.rb +85 -0
- data/lib/rubik/connect/models_dev.rb +173 -0
- data/lib/rubik/connect/probes.rb +103 -0
- data/lib/rubik/connect/prompt_ui.rb +139 -0
- data/lib/rubik/connect/wizard.rb +540 -0
- data/lib/rubik/connect.rb +7 -0
- data/lib/rubik/context/repo_context_reader.rb +8 -4
- data/lib/rubik/llm/chunk_assembler.rb +12 -1
- data/lib/rubik/llm/formatter.rb +14 -0
- data/lib/rubik/llm/openai_compatible.rb +4 -1
- data/lib/rubik/llm/provider_key.rb +32 -0
- data/lib/rubik/llm/registry.rb +18 -2
- data/lib/rubik/llm/router.rb +19 -1
- data/lib/rubik/llm/session_header.rb +128 -0
- data/lib/rubik/llm/sse_parser.rb +57 -10
- data/lib/rubik/lsp/client.rb +11 -5
- data/lib/rubik/lsp/transport.rb +17 -7
- data/lib/rubik/mcp/transport/stdio.rb +42 -4
- data/lib/rubik/orchestrator/continuation_manager.rb +14 -5
- data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
- data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
- data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
- data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
- data/lib/rubik/orchestrator/goal_session.rb +4 -4
- data/lib/rubik/orchestrator/loop.rb +8 -11
- data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
- data/lib/rubik/orchestrator/step_executor.rb +17 -2
- data/lib/rubik/orchestrator/subagent/instance.rb +24 -1
- data/lib/rubik/orchestrator/subagent/notices.rb +1 -1
- data/lib/rubik/orchestrator/subagent/reaper.rb +21 -2
- data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
- data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
- data/lib/rubik/orchestrator/subagent/supervisor.rb +10 -3
- data/lib/rubik/orchestrator/turn_executor/user_channel.rb +27 -5
- data/lib/rubik/orchestrator/turn_executor.rb +24 -8
- data/lib/rubik/platform/filesystem.rb +52 -24
- data/lib/rubik/plugin/storage.rb +20 -0
- data/lib/rubik/prompt/context_builder.rb +2 -1
- data/lib/rubik/repair/loop.rb +22 -2
- data/lib/rubik/repl/commands/agent_context.rb +1 -1
- data/lib/rubik/repl/commands/agent_execution.rb +20 -15
- data/lib/rubik/repl/commands/buffer.rb +15 -0
- data/lib/rubik/repl/commands/connect.rb +51 -0
- data/lib/rubik/repl/commands/lsp.rb +1 -1
- data/lib/rubik/repl/commands/plugins.rb +31 -31
- data/lib/rubik/repl/commands/session.rb +5 -5
- data/lib/rubik/repl/commands/workspace.rb +4 -4
- data/lib/rubik/repl/commands.rb +2 -0
- data/lib/rubik/repl/input_execution.rb +30 -29
- data/lib/rubik/repl/input_loop.rb +10 -4
- data/lib/rubik/repl/lifecycle.rb +38 -0
- data/lib/rubik/repl/notice_router.rb +11 -5
- data/lib/rubik/repl/paste_support.rb +1 -1
- data/lib/rubik/repl/prompt_grace.rb +3 -3
- data/lib/rubik/repl/shell.rb +10 -2
- data/lib/rubik/repl/turn_recorder.rb +14 -3
- data/lib/rubik/runtime/context.rb +33 -27
- data/lib/rubik/runtime/manager.rb +23 -15
- data/lib/rubik/sandbox/driver/bubblewrap.rb +58 -8
- data/lib/rubik/sandbox/driver/process.rb +32 -1
- data/lib/rubik/sandbox/runner.rb +9 -1
- data/lib/rubik/security/classifier/classifier.rb +147 -13
- data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
- data/lib/rubik/security/classifier/commands/git.yaml +27 -1
- data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
- data/lib/rubik/security/classifier/database.rb +2 -2
- data/lib/rubik/security/command_gate.rb +8 -1
- data/lib/rubik/security/redactor.rb +62 -19
- data/lib/rubik/session/recorder.rb +13 -18
- data/lib/rubik/session/replayer.rb +29 -7
- data/lib/rubik/storage/store.rb +19 -5
- data/lib/rubik/tools/builtins/exec.rb +1 -1
- data/lib/rubik/tools/builtins/files.rb +8 -12
- data/lib/rubik/tools/builtins/grep.rb +3 -2
- data/lib/rubik/tools/builtins/lsp.rb +31 -7
- data/lib/rubik/tools/builtins/show_file.rb +1 -1
- data/lib/rubik/tools/builtins/synthesis.rb +4 -4
- data/lib/rubik/tools/result_normalizer.rb +1 -1
- data/lib/rubik/ui/banner.rb +3 -3
- data/lib/rubik/ui/dialog.rb +10 -7
- data/lib/rubik/ui/diff_viewer.rb +4 -4
- data/lib/rubik/ui/frame.rb +13 -3
- data/lib/rubik/ui/markdown_stream.rb +8 -2
- data/lib/rubik/ui/panel.rb +11 -11
- data/lib/rubik/ui/renderer.rb +6 -6
- data/lib/rubik/ui/spinner.rb +1 -1
- data/lib/rubik/ui/statusline.rb +3 -3
- data/lib/rubik/ui/theme.rb +1 -1
- data/lib/rubik/version.rb +1 -1
- metadata +12 -1
data/lib/rubik/llm/registry.rb
CHANGED
|
@@ -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:
|
|
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
|
-
|
|
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(
|
data/lib/rubik/llm/router.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "provider_key"
|
|
3
4
|
require_relative "registry"
|
|
5
|
+
require_relative "session_header"
|
|
4
6
|
require_relative "../storage/layout"
|
|
5
7
|
|
|
6
8
|
module Rubik
|
|
@@ -44,7 +46,13 @@ module Rubik
|
|
|
44
46
|
# Streaming variant. Resolves a provider via `#route` (or accepts an
|
|
45
47
|
# explicit `provider:` opt) and delegates to its `#stream_chat`.
|
|
46
48
|
# Yields Chunks. Without a block, returns an Enumerator.
|
|
47
|
-
|
|
49
|
+
#
|
|
50
|
+
# `session_key:` is opt-in (issue #534): when given, the resolved
|
|
51
|
+
# provider's per-provider session header knob
|
|
52
|
+
# (`providers.<key>.session_header`) is applied with the same
|
|
53
|
+
# SessionHeader.apply! semantics as the orchestrator lanes. Without
|
|
54
|
+
# it, behavior is unchanged.
|
|
55
|
+
def stream_chat(messages, task_type: :auto, prompt: "", provider: nil, session_key: nil, **opts, &block)
|
|
48
56
|
chosen_route = if provider
|
|
49
57
|
resolved = provider.respond_to?(:stream_chat) ? provider : @registry.get(provider)
|
|
50
58
|
{ provider: resolved }
|
|
@@ -67,6 +75,16 @@ module Rubik
|
|
|
67
75
|
)
|
|
68
76
|
end
|
|
69
77
|
|
|
78
|
+
# Opt-in per-provider session-header injection for external callers
|
|
79
|
+
# (issue #534): same SessionHeader.apply! semantics as the orchestrator
|
|
80
|
+
# lanes — knob lookup, explicit-beats-automatic, and the key-absent-when-
|
|
81
|
+
# unset discipline all live there. Without the kwarg this is a no-op, so
|
|
82
|
+
# the pass-through stays byte-identical for existing callers.
|
|
83
|
+
if session_key
|
|
84
|
+
Rubik::LLM::SessionHeader.apply!(opts, provider_key: Rubik::LLM::ProviderKey.derive(chosen),
|
|
85
|
+
config: @config, session_key: session_key)
|
|
86
|
+
end
|
|
87
|
+
|
|
70
88
|
return to_enum(:stream_chat, messages, task_type: task_type, prompt: prompt, provider: chosen, **opts) unless block # rubocop:disable Lint/ToEnumArguments -- provider: chosen bilinçli; enumerator routing anlik görüntüsünü tasir
|
|
71
89
|
|
|
72
90
|
chosen.stream_chat(messages, opts, &block)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require_relative "../coeffects"
|
|
5
|
+
require_relative "../log"
|
|
6
|
+
require_relative "../platform"
|
|
7
|
+
require_relative "../storage/layout"
|
|
8
|
+
|
|
9
|
+
module Rubik
|
|
10
|
+
module LLM
|
|
11
|
+
# Per-provider dynamic session header (providers.<name>.session_header):
|
|
12
|
+
# gateways like opencode's Go endpoint hard-400 every POST without a
|
|
13
|
+
# session header (MissingSessionID) and ask for ONE stable id per
|
|
14
|
+
# conversation carried on EVERY request — including auxiliary lanes
|
|
15
|
+
# (subagents, repair), where dropping it is the classic mid-conversation
|
|
16
|
+
# failure mode. The id is a UUID derived deterministically from the
|
|
17
|
+
# session recorder identity (Recorder#session_id), so resume
|
|
18
|
+
# (switch_session_file re-targets the same file) and restarts keep the
|
|
19
|
+
# same identity while distinct conversations stay distinct. Pure
|
|
20
|
+
# functions; knob and identity are re-read per request (config
|
|
21
|
+
# reactivity preserved) and the module holds no state. Lanes reach it
|
|
22
|
+
# through ProviderResolver#apply_session_header!, so the module itself
|
|
23
|
+
# stays lane-agnostic.
|
|
24
|
+
module SessionHeader
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
# Domain-separation salt: the same basename must never collide across
|
|
28
|
+
# purposes or deployments.
|
|
29
|
+
DERIVATION_SALT = "rubik.llm.session_header/v1"
|
|
30
|
+
|
|
31
|
+
# Conversation identity string -> RFC 4122-shaped UUID (SHA-256, first
|
|
32
|
+
# 16 bytes stamped with version-5 / RFC-variant bits). One-way: leaks
|
|
33
|
+
# nothing about the file name. nil for a blank identity.
|
|
34
|
+
def uuid_for(session_key)
|
|
35
|
+
key = session_key.to_s
|
|
36
|
+
return nil if key.empty?
|
|
37
|
+
|
|
38
|
+
bytes = Digest::SHA256.digest("#{DERIVATION_SALT}:#{key}").unpack("C16")
|
|
39
|
+
bytes[6] = (bytes[6] & 0x0F) | 0x50 # version 5
|
|
40
|
+
bytes[8] = (bytes[8] & 0x3F) | 0x80 # RFC 4122 variant
|
|
41
|
+
hex = bytes.pack("C*").unpack1("H*")
|
|
42
|
+
[hex[0, 8], hex[8, 4], hex[12, 4], hex[16, 4], hex[20, 12]].join("-")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Conversation identity: the session recorder file basename without
|
|
46
|
+
# extension. Recorder#session_id is the SSOT for that spelling; the
|
|
47
|
+
# basename fallback covers plain test doubles. nil when no
|
|
48
|
+
# recorder-backed identity exists; callers then SKIP injection.
|
|
49
|
+
def session_key_from_recorder(recorder)
|
|
50
|
+
return recorder.session_id if recorder.respond_to?(:session_id)
|
|
51
|
+
|
|
52
|
+
return nil unless recorder.respond_to?(:session_file)
|
|
53
|
+
|
|
54
|
+
file = recorder.session_file
|
|
55
|
+
return nil if file.nil? || file.to_s.empty?
|
|
56
|
+
|
|
57
|
+
File.basename(file.to_s, ".jsonl")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Identity from a runtime context: the SHELL coeffect (parent-chain
|
|
61
|
+
# walk) is THE conversation anchor — the root loop reads it through
|
|
62
|
+
# its runtime manager, subagents through their child context, so both
|
|
63
|
+
# lanes share one conversation id. nil-safe end to end.
|
|
64
|
+
def session_key_from_context(ctx)
|
|
65
|
+
shell = Rubik::Coeffects.get(ctx, Rubik::Coeffects::SHELL)
|
|
66
|
+
return nil unless shell.respond_to?(:recorder)
|
|
67
|
+
|
|
68
|
+
session_key_from_recorder(shell.recorder)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Knob: providers.<key>.session_header — the header NAME to inject.
|
|
72
|
+
# Deliberately provider-scoped ONLY (no llm.session_header global
|
|
73
|
+
# spelling): a global would silently route every provider through one
|
|
74
|
+
# header name. Same two-arg Layout read as ProviderResolver#llm_config
|
|
75
|
+
# (safe_config's false-coercion / KeyError traps do not apply here).
|
|
76
|
+
def header_name(config, provider_key)
|
|
77
|
+
return nil unless provider_key.is_a?(String) && !provider_key.empty?
|
|
78
|
+
|
|
79
|
+
name = Rubik::Storage::Layout.config_get(config, "providers.#{provider_key}.session_header", nil)
|
|
80
|
+
name.is_a?(String) && !name.strip.empty? ? name.strip : nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Header names the operator configured EXPLICITLY (downcased) — both
|
|
84
|
+
# scopes the Registry bakes into the provider's @headers layer:
|
|
85
|
+
# providers.<key>.headers and the global llm.headers.
|
|
86
|
+
def explicit_header_names(config, provider_key)
|
|
87
|
+
names = []
|
|
88
|
+
if provider_key.is_a?(String) && !provider_key.empty?
|
|
89
|
+
scoped = Rubik::Storage::Layout.config_get(config, "providers.#{provider_key}.headers", nil)
|
|
90
|
+
names += scoped.keys if scoped.is_a?(Hash)
|
|
91
|
+
end
|
|
92
|
+
global = Rubik::Storage::Layout.config_get(config, "llm.headers", nil)
|
|
93
|
+
names += global.keys if global.is_a?(Hash)
|
|
94
|
+
names.map { |k| k.to_s.downcase }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# THE injection core. Mutates +opts+ in place and returns it. The
|
|
98
|
+
# :headers key stays ABSENT unless a header is actually added — the
|
|
99
|
+
# "key absent when unset" discipline of the reasoning_effort bridge.
|
|
100
|
+
# Explicit beats automatic: if the operator already set the same header
|
|
101
|
+
# name — in providers.<key>.headers, global llm.headers, or per-call
|
|
102
|
+
# opts[:headers] (adapters merge opts LAST, so blind injection would
|
|
103
|
+
# clobber their value) — auto-injection skips for that request.
|
|
104
|
+
def apply!(opts, provider_key:, config:, session_key:)
|
|
105
|
+
name = header_name(config, provider_key)
|
|
106
|
+
return opts if name.nil?
|
|
107
|
+
|
|
108
|
+
key = session_key.to_s
|
|
109
|
+
if key.empty?
|
|
110
|
+
# A set knob with an unresolvable identity is a STANDING
|
|
111
|
+
# misconfiguration — the gateway will hard-400 mid-conversation,
|
|
112
|
+
# so this must not stay silent (silence-forbidden doctrine).
|
|
113
|
+
Rubik::Log.warn(:llm, "[SessionHeader] providers.#{provider_key}.session_header=#{name} is set, " \
|
|
114
|
+
"but no session identity is available on this lane; header not injected")
|
|
115
|
+
return opts
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
return opts if explicit_header_names(config, provider_key).include?(name.downcase)
|
|
119
|
+
|
|
120
|
+
existing = opts[:headers].is_a?(Hash) ? opts[:headers] : {}
|
|
121
|
+
return opts if existing.any? { |k, _| k.to_s.downcase == name.downcase }
|
|
122
|
+
|
|
123
|
+
opts[:headers] = existing.merge(name => uuid_for(key))
|
|
124
|
+
opts
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
data/lib/rubik/llm/sse_parser.rb
CHANGED
|
@@ -22,28 +22,46 @@ module Rubik
|
|
|
22
22
|
@terminator_bin = terminator.is_a?(String) ? terminator.b : terminator
|
|
23
23
|
@sse_mode = @terminator == SSE_TERMINATOR || @terminator == SSE_EVENT_SEP
|
|
24
24
|
@buffer = String.new(encoding: Encoding::ASCII_8BIT)
|
|
25
|
+
# Full-body fallback (issue #556): a provider that ignores `stream:
|
|
26
|
+
# true` answers with one plain JSON body carrying no SSE fields.
|
|
27
|
+
# Remember that body until flush so the turn is not silently empty.
|
|
28
|
+
@raw_body = String.new(encoding: Encoding::ASCII_8BIT)
|
|
29
|
+
@any_event = false
|
|
25
30
|
end
|
|
26
31
|
|
|
27
32
|
# Feed raw bytes; returns an array of complete events parsed from this
|
|
28
33
|
# chunk. Partial events stay in the buffer.
|
|
29
34
|
def feed(chunk_bytes)
|
|
30
|
-
|
|
35
|
+
bytes = chunk_bytes.to_s.b
|
|
36
|
+
@buffer << bytes
|
|
37
|
+
# Stop remembering once a real event surfaced — from then on this is
|
|
38
|
+
# a genuine SSE stream and the fallback must never fire.
|
|
39
|
+
@raw_body << bytes if @sse_mode && !@any_event
|
|
31
40
|
drain_complete_events(final: false)
|
|
32
41
|
end
|
|
33
42
|
|
|
34
43
|
# Flush any trailing event (no trailing terminator) and reset.
|
|
35
44
|
def flush
|
|
36
|
-
|
|
45
|
+
events = []
|
|
46
|
+
unless @buffer.empty? && @raw_body.empty?
|
|
47
|
+
events = drain_complete_events(final: true)
|
|
37
48
|
|
|
38
|
-
|
|
49
|
+
unless @buffer.empty?
|
|
50
|
+
remaining = @buffer.dup
|
|
51
|
+
parsed = parse_event(remaining)
|
|
52
|
+
events << parsed if parsed
|
|
53
|
+
@any_event = true if parsed
|
|
54
|
+
end
|
|
39
55
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
events << parsed if parsed
|
|
56
|
+
# No SSE event ever surfaced but the body carried real content: emit
|
|
57
|
+
# the whole body as one message event so formatters can parse it and
|
|
58
|
+
# the turn reports text instead of an empty response.
|
|
59
|
+
events << { type: "message", data: full_body_data(@raw_body) } if @sse_mode && !@any_event && substantive_body?(@raw_body)
|
|
45
60
|
end
|
|
46
61
|
|
|
62
|
+
@buffer = String.new(encoding: Encoding::ASCII_8BIT)
|
|
63
|
+
@raw_body = String.new(encoding: Encoding::ASCII_8BIT)
|
|
64
|
+
@any_event = false
|
|
47
65
|
events
|
|
48
66
|
end
|
|
49
67
|
|
|
@@ -64,19 +82,48 @@ module Rubik
|
|
|
64
82
|
event_block = @buffer.byteslice(0, m.begin(0))
|
|
65
83
|
@buffer = @buffer.byteslice(m.end(0)..-1) || String.new(encoding: Encoding::ASCII_8BIT)
|
|
66
84
|
parsed = parse_event(event_block)
|
|
67
|
-
|
|
85
|
+
next unless parsed
|
|
86
|
+
|
|
87
|
+
events << parsed
|
|
88
|
+
@any_event = true
|
|
68
89
|
end
|
|
69
90
|
else
|
|
70
91
|
while (idx = @buffer.index(@terminator_bin))
|
|
71
92
|
event_block = @buffer.byteslice(0, idx)
|
|
72
93
|
@buffer = @buffer.byteslice((idx + @terminator_bin.bytesize)..-1) || String.new(encoding: Encoding::ASCII_8BIT)
|
|
73
94
|
parsed = parse_event(event_block)
|
|
74
|
-
|
|
95
|
+
next unless parsed
|
|
96
|
+
|
|
97
|
+
events << parsed
|
|
98
|
+
@any_event = true
|
|
75
99
|
end
|
|
76
100
|
end
|
|
77
101
|
events
|
|
78
102
|
end
|
|
79
103
|
|
|
104
|
+
# True when the raw body holds at least one line that is neither blank
|
|
105
|
+
# nor an SSE comment. Pure keepalive/comment bodies must not trigger the
|
|
106
|
+
# full-body fallback (they are not a provider answer).
|
|
107
|
+
def substantive_body?(raw)
|
|
108
|
+
utf8 = decode_utf8(raw)
|
|
109
|
+
utf8.each_line do |line|
|
|
110
|
+
stripped = line.strip
|
|
111
|
+
next if stripped.empty? || stripped.start_with?(":")
|
|
112
|
+
|
|
113
|
+
return true
|
|
114
|
+
end
|
|
115
|
+
false
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def full_body_data(raw)
|
|
119
|
+
decode_utf8(raw).sub(/\s+\z/, "")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def decode_utf8(bytes)
|
|
123
|
+
text = bytes.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
124
|
+
text.valid_encoding? ? text : text.scrub
|
|
125
|
+
end
|
|
126
|
+
|
|
80
127
|
def parse_event(block)
|
|
81
128
|
return nil if block.nil?
|
|
82
129
|
|
data/lib/rubik/lsp/client.rb
CHANGED
|
@@ -177,13 +177,19 @@ module Rubik
|
|
|
177
177
|
return unless running || @transport.resources?
|
|
178
178
|
|
|
179
179
|
begin
|
|
180
|
-
|
|
181
|
-
|
|
180
|
+
if running
|
|
181
|
+
send_request("shutdown", {}, timeout: 2)
|
|
182
|
+
send_notification("exit", {})
|
|
183
|
+
end
|
|
182
184
|
rescue StandardError
|
|
183
|
-
#
|
|
185
|
+
# A failed/wedged shutdown write is not a reason to leave the server
|
|
186
|
+
# alive — bounded writes make this a deadline rather than a hang.
|
|
187
|
+
ensure
|
|
188
|
+
# Always retract the transport, even if an Interrupt/non-StandardError
|
|
189
|
+
# unwinds the graceful shutdown: kill_tree is the only thing that can
|
|
190
|
+
# unblock a writer parked on a full stdin pipe.
|
|
191
|
+
@transport.shutdown
|
|
184
192
|
end
|
|
185
|
-
|
|
186
|
-
@transport.shutdown
|
|
187
193
|
end
|
|
188
194
|
alias dispose stop
|
|
189
195
|
|
data/lib/rubik/lsp/transport.rb
CHANGED
|
@@ -32,6 +32,12 @@ module Rubik
|
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
# Deadline for a single framed write. A server that stops draining
|
|
36
|
+
# stdin must not park the writer forever on @write_mutex — Client#stop
|
|
37
|
+
# sends shutdown through this mutex, so a stalled write would otherwise
|
|
38
|
+
# prevent the kill_tree that unblocks the pipe.
|
|
39
|
+
WRITE_TIMEOUT = 5.0
|
|
40
|
+
|
|
35
41
|
attr_reader :mutex, :cv, :running
|
|
36
42
|
|
|
37
43
|
def initialize(root_dir:, on_message:)
|
|
@@ -257,13 +263,11 @@ module Rubik
|
|
|
257
263
|
header = "Content-Length: #{json.bytesize}\r\n\r\n"
|
|
258
264
|
bytes = header + json
|
|
259
265
|
@write_mutex.synchronize do
|
|
260
|
-
|
|
261
|
-
while
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
offset += written
|
|
266
|
-
end
|
|
266
|
+
# Deadline-bounded write: the old @stdin.write loop could park here
|
|
267
|
+
# forever while holding @write_mutex. Client#stop sends shutdown
|
|
268
|
+
# through this mutex, so a stalled write prevented it from ever
|
|
269
|
+
# reaching the kill_tree that unblocks the pipe.
|
|
270
|
+
Rubik::Platform.write_until(@stdin, bytes, timeout: write_timeout)
|
|
267
271
|
@stdin.flush
|
|
268
272
|
end
|
|
269
273
|
true
|
|
@@ -278,6 +282,12 @@ module Rubik
|
|
|
278
282
|
false
|
|
279
283
|
end
|
|
280
284
|
|
|
285
|
+
# Single source for the write deadline; overridable in tests so a
|
|
286
|
+
# non-draining pipe can be exercised without waiting the full 5s.
|
|
287
|
+
def write_timeout
|
|
288
|
+
WRITE_TIMEOUT
|
|
289
|
+
end
|
|
290
|
+
|
|
281
291
|
# Correlate an incoming response with the pending-request registry.
|
|
282
292
|
# Server-request ids and late responses are discarded here exactly as
|
|
283
293
|
# before the extraction (issue #206).
|
|
@@ -16,6 +16,12 @@ module Rubik
|
|
|
16
16
|
# #stderr_tail for diagnostics.
|
|
17
17
|
STDERR_TAIL_LINES = 20
|
|
18
18
|
|
|
19
|
+
# Upper bound on a single stdin write. A server that stops draining
|
|
20
|
+
# stdin lets the OS pipe buffer fill; without a deadline the writer
|
|
21
|
+
# parks forever. Platform.write_until converts that stall into an
|
|
22
|
+
# Errno::EPIPE after this many seconds instead of a deadlock.
|
|
23
|
+
STDIN_WRITE_TIMEOUT = 5.0
|
|
24
|
+
|
|
19
25
|
# Credential-shaped variable names stripped from the inherited host
|
|
20
26
|
# environment when a server opts out with `inherit_env: false`. Only
|
|
21
27
|
# the suffix shape is policy — the prefix is the provider's choice
|
|
@@ -43,6 +49,10 @@ module Rubik
|
|
|
43
49
|
@stderr_mutex = Mutex.new
|
|
44
50
|
@stderr_tail = []
|
|
45
51
|
@mutex = Mutex.new
|
|
52
|
+
# Writes are serialized on their OWN mutex, never @mutex: @mutex is
|
|
53
|
+
# also taken by close/wait_for_response/the reader, so a stalled
|
|
54
|
+
# write holding it would wedge teardown and response delivery.
|
|
55
|
+
@write_mutex = Mutex.new
|
|
46
56
|
@cv = ConditionVariable.new
|
|
47
57
|
@responses = {}
|
|
48
58
|
@closed = false
|
|
@@ -159,6 +169,9 @@ module Rubik
|
|
|
159
169
|
end
|
|
160
170
|
|
|
161
171
|
def close
|
|
172
|
+
# Never take @write_mutex here: a writer may be stalled in
|
|
173
|
+
# write_until, and killing the child (below) is precisely what
|
|
174
|
+
# unblocks it. Waiting for the write lock would deadlock teardown.
|
|
162
175
|
child_died = false
|
|
163
176
|
# Capture before @wait_thr is nil'd below: the child-died diagnostic
|
|
164
177
|
# must still report WHICH server process failed to start.
|
|
@@ -301,17 +314,33 @@ module Rubik
|
|
|
301
314
|
|
|
302
315
|
def write_message(msg_hash)
|
|
303
316
|
json = JSON.dump(msg_hash)
|
|
304
|
-
@mutex.
|
|
317
|
+
# Resolve/validate the pipe under @mutex, then write OUTSIDE it.
|
|
318
|
+
# @mutex is shared with close, wait_for_response and the reader;
|
|
319
|
+
# a large payload to a server not draining stdin would block inside
|
|
320
|
+
# puts while holding it — close could never reach kill_tree, timeouts
|
|
321
|
+
# could not start, and responses could not be stored (bidirectional
|
|
322
|
+
# deadlock). A dedicated write mutex keeps writes serialized while
|
|
323
|
+
# write_until bounds the stall with a deadline.
|
|
324
|
+
stdin = @mutex.synchronize do
|
|
305
325
|
raise ConnectionError, "Process stdin is closed" if @stdin.nil? || @stdin.closed?
|
|
306
326
|
|
|
307
|
-
@stdin
|
|
308
|
-
|
|
327
|
+
@stdin
|
|
328
|
+
end
|
|
329
|
+
@write_mutex.synchronize do
|
|
330
|
+
Rubik::Platform.write_until(stdin, "#{json}\n", timeout: stdin_write_timeout)
|
|
331
|
+
stdin.flush
|
|
309
332
|
end
|
|
310
|
-
rescue IOError, Errno::EPIPE => e
|
|
333
|
+
rescue IOError, Errno::EPIPE, Errno::EINTR => e
|
|
311
334
|
close
|
|
312
335
|
raise ConnectionError, "Failed to write to MCP server: #{e.message}"
|
|
313
336
|
end
|
|
314
337
|
|
|
338
|
+
# Single source for the write deadline; overridable in tests so a
|
|
339
|
+
# non-draining pipe can be exercised without waiting the full 5s.
|
|
340
|
+
def stdin_write_timeout
|
|
341
|
+
STDIN_WRITE_TIMEOUT
|
|
342
|
+
end
|
|
343
|
+
|
|
315
344
|
def wait_for_response(req_id, timeout: 10)
|
|
316
345
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
317
346
|
response = @mutex.synchronize do
|
|
@@ -416,6 +445,15 @@ module Rubik
|
|
|
416
445
|
def handle_incoming_message(msg)
|
|
417
446
|
return unless msg.is_a?(Hash) && msg.key?("id")
|
|
418
447
|
|
|
448
|
+
# A server-initiated request/notification (e.g. a "ping" carrying an
|
|
449
|
+
# id) is NOT a reply to one of our pending requests. Mirror the LSP
|
|
450
|
+
# transport's dispatch guard: only id-bearing messages WITHOUT a
|
|
451
|
+
# method can be responses, and only ones carrying result/error can
|
|
452
|
+
# satisfy a waiter. Otherwise a colliding id stores a nil-result
|
|
453
|
+
# "response" and the real reply is later discarded as a late dup.
|
|
454
|
+
return if msg.key?("method")
|
|
455
|
+
return unless msg.key?("result") || msg.key?("error")
|
|
456
|
+
|
|
419
457
|
@mutex.synchronize do
|
|
420
458
|
return unless @pending_requests.include?(msg["id"])
|
|
421
459
|
|
|
@@ -66,16 +66,16 @@ module Rubik
|
|
|
66
66
|
|
|
67
67
|
unless enabled?
|
|
68
68
|
rec = (max_tokens.to_i * 2).positive? ? max_tokens.to_i * 2 : 8192
|
|
69
|
-
diag = "⚠️ Model
|
|
70
|
-
"
|
|
69
|
+
diag = "⚠️ Model output reached the maximum token limit (max_output_tokens: #{max_tokens}) and was cut off before completion.\n" \
|
|
70
|
+
"Solution: To increase the output token budget, you can run the following command:\n !config set llm.default_max_output_tokens #{rec}"
|
|
71
71
|
return { continue: false, error: diag }
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
if ceiling_reached?
|
|
75
75
|
rec = (max_tokens.to_i * 2).positive? ? max_tokens.to_i * 2 : 8192
|
|
76
|
-
diag = "⚠️ Model
|
|
77
|
-
"
|
|
78
|
-
"
|
|
76
|
+
diag = "⚠️ Model output reached the maximum token limit (max_output_tokens: #{max_tokens}) and " \
|
|
77
|
+
"the #{@max_auto_continues} automatic continue attempts were exhausted, so it was stopped.\n" \
|
|
78
|
+
"Solution: To increase the output token budget, you can run the following command:\n !config set llm.default_max_output_tokens #{rec}"
|
|
79
79
|
return { continue: false, error: diag }
|
|
80
80
|
end
|
|
81
81
|
|
|
@@ -130,6 +130,15 @@ module Rubik
|
|
|
130
130
|
@continuation_buffer = +""
|
|
131
131
|
end
|
|
132
132
|
|
|
133
|
+
# A successful turn closes the *consecutive* truncation chain, so the
|
|
134
|
+
# ceiling must start over (issue #549). The partial text buffer is NOT
|
|
135
|
+
# cleared here: stitch_and_reset owns that, and dropping it on a tool
|
|
136
|
+
# turn would lose the already-generated answer. Only the strike count
|
|
137
|
+
# is reset.
|
|
138
|
+
def reset_strikes!
|
|
139
|
+
@auto_continue_count = 0
|
|
140
|
+
end
|
|
141
|
+
|
|
133
142
|
private
|
|
134
143
|
|
|
135
144
|
def safe_config(key, default)
|
|
@@ -30,6 +30,9 @@ module Rubik
|
|
|
30
30
|
class Ledger
|
|
31
31
|
Result = Struct.new(:status, :reasons, :rev, :uri, keyword_init: true)
|
|
32
32
|
|
|
33
|
+
# Revision chain bound (issue #551): newest N superseded rows kept.
|
|
34
|
+
MAX_RETAINED_REVISIONS = 16
|
|
35
|
+
|
|
33
36
|
# Askı yaşam döngüsü (issue #106): suspend!/unsuspend! damgaları +
|
|
34
37
|
# restore_from_row! + suspend_meta — instance-method include ile
|
|
35
38
|
# private yardımcılara (row_title, canonical_json, final_result,
|
|
@@ -118,8 +121,7 @@ module Rubik
|
|
|
118
121
|
return nil unless row.is_a?(Hash) && row["status"] == "suspended"
|
|
119
122
|
|
|
120
123
|
doc = JSON.parse(row["body"].to_s)
|
|
121
|
-
return nil unless
|
|
122
|
-
doc["objective"] && SuspendLifecycle::SIGMA_COLLECTIONS.all? { |key| doc[key].is_a?(Array) }
|
|
124
|
+
return nil unless Validator.sigma_doc_well_formed?(doc, schema: PRODUCTION_SCHEMA)
|
|
123
125
|
|
|
124
126
|
ledger = new(goal_text: doc["objective"].to_s,
|
|
125
127
|
max_retries: max_patch_retries(config),
|
|
@@ -297,6 +299,7 @@ module Rubik
|
|
|
297
299
|
if row
|
|
298
300
|
@storage_id = row["id"]
|
|
299
301
|
@last_links = links
|
|
302
|
+
prune_superseded_rows!
|
|
300
303
|
end
|
|
301
304
|
log(:patch_accepted, "goal ledger rev #{@doc['rev']} applied (#{row ? "stored #{@storage_id}" : 'memory-only'})")
|
|
302
305
|
Result.new(status: :applied, rev: @doc["rev"], uri: ledger_uri)
|
|
@@ -307,6 +310,19 @@ module Rubik
|
|
|
307
310
|
Result.new(status: :applied, rev: @doc["rev"], uri: ledger_uri)
|
|
308
311
|
end
|
|
309
312
|
|
|
313
|
+
# Keep newest N superseded rows plus the active row and the newest
|
|
314
|
+
# suspended row (the `.last` resume target). Stale suspended rows from
|
|
315
|
+
# dead processes are bounded too. A pruning error never rolls back.
|
|
316
|
+
def prune_superseded_rows!
|
|
317
|
+
rows = @store.list(type: "goal").select { |s| s.tags.include?("goal-ledger") }.sort_by(&:row)
|
|
318
|
+
suspended = rows.select { |s| s.status == "suspended" }.sort_by(&:updated_at)
|
|
319
|
+
keep = [@storage_id, suspended.last&.id].compact
|
|
320
|
+
keep += rows.reject { |s| keep.include?(s.id) }.last(MAX_RETAINED_REVISIONS).map(&:id)
|
|
321
|
+
rows.each { |s| @store.delete(type: "goal", id: s.id) unless keep.include?(s.id) }
|
|
322
|
+
rescue StandardError => e
|
|
323
|
+
degrade("revision row pruning", e)
|
|
324
|
+
end
|
|
325
|
+
|
|
310
326
|
def exhausted_result
|
|
311
327
|
log(:patch_exhausted, "patch budget exhausted after #{@rejections} consecutive rejections; " \
|
|
312
328
|
"ledger unchanged at rev #{@doc['rev']}")
|
|
@@ -77,10 +77,13 @@ module Rubik
|
|
|
77
77
|
before = Prompt::TokenEstimator.estimate_history(rows)
|
|
78
78
|
units = Prompt::ContextCompactor.group_atomic_units(rows)
|
|
79
79
|
anchor_idx = anchor_unit_index(units)
|
|
80
|
+
# Her apply! turunda TEK kez hesaplanır (O(n)): eskiden banked?
|
|
81
|
+
# her birim için assistant birimlerini yeniden tarardı (O(n²)).
|
|
82
|
+
recency_start = recency_start_index(units)
|
|
80
83
|
count = 0
|
|
81
84
|
rev_now = @ledger.rev
|
|
82
85
|
units.each_with_index do |unit, idx|
|
|
83
|
-
next unless banked?(idx, anchor_idx,
|
|
86
|
+
next unless banked?(idx, anchor_idx, recency_start)
|
|
84
87
|
next if Prompt::ContextCompactor.unit_pins_goal?(unit) # <user_task> taşıyan birim asla emilmez
|
|
85
88
|
|
|
86
89
|
unit.each do |row|
|
|
@@ -114,14 +117,20 @@ module Rubik
|
|
|
114
117
|
# Recency: son K assistant-taşıyan birimin İLKİnden itibaren her şey
|
|
115
118
|
# (ve anchor'u ve sonrası) raw kalır; banked = idx < anchor VE
|
|
116
119
|
# idx < recency_start.
|
|
117
|
-
def banked?(idx, anchor_idx,
|
|
120
|
+
def banked?(idx, anchor_idx, recency_start)
|
|
118
121
|
return false if anchor_idx.nil? || idx >= anchor_idx
|
|
119
122
|
|
|
120
|
-
assistant_units = units.each_index.select { |i| units[i].any? { |r| assistant_row?(r) } }
|
|
121
|
-
recency_start = assistant_units[[assistant_units.size - @window, 0].max] || units.size
|
|
122
123
|
idx < recency_start
|
|
123
124
|
end
|
|
124
125
|
|
|
126
|
+
# Son K assistant-taşıyan birimin İLKİ — tur boyunca sabit, bu yüzden
|
|
127
|
+
# apply! içinde bir kez hoist edilir. assistant_units listesi artık
|
|
128
|
+
# birim başına değil, tur başına kurulur (issue #551, O(n²) → O(n)).
|
|
129
|
+
def recency_start_index(units)
|
|
130
|
+
assistant_units = units.each_index.select { |i| units[i].any? { |r| assistant_row?(r) } }
|
|
131
|
+
assistant_units[[assistant_units.size - @window, 0].max] || units.size
|
|
132
|
+
end
|
|
133
|
+
|
|
125
134
|
def assistant_row?(row)
|
|
126
135
|
Prompt::ConversationRows.row?(row) && Prompt::ConversationRows.role_of(row) == "assistant"
|
|
127
136
|
end
|