rubik 0.13.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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +34 -1
  4. data/lib/rubik/ast/c_extractor.rb +1 -1
  5. data/lib/rubik/ast/graph.rb +38 -57
  6. data/lib/rubik/ast/graph_extensions.rb +57 -15
  7. data/lib/rubik/ast/ruby_extractor.rb +4 -0
  8. data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
  9. data/lib/rubik/buffer/manager.rb +33 -11
  10. data/lib/rubik/buffer/snapshot.rb +23 -3
  11. data/lib/rubik/cli/connect.rb +3 -5
  12. data/lib/rubik/config/defaults.rb +3 -7
  13. data/lib/rubik/config/env_guard.rb +43 -0
  14. data/lib/rubik/config/schema.rb +7 -1
  15. data/lib/rubik/config/secret_policy.rb +6 -5
  16. data/lib/rubik/config.rb +1 -1
  17. data/lib/rubik/connect/models_dev.rb +9 -0
  18. data/lib/rubik/connect/probes.rb +9 -2
  19. data/lib/rubik/connect/wizard.rb +108 -56
  20. data/lib/rubik/context/repo_context_reader.rb +8 -4
  21. data/lib/rubik/llm/chunk_assembler.rb +12 -1
  22. data/lib/rubik/llm/formatter.rb +14 -0
  23. data/lib/rubik/llm/provider_key.rb +32 -0
  24. data/lib/rubik/llm/registry.rb +18 -2
  25. data/lib/rubik/llm/router.rb +19 -1
  26. data/lib/rubik/llm/session_header.rb +128 -0
  27. data/lib/rubik/llm/sse_parser.rb +57 -10
  28. data/lib/rubik/lsp/client.rb +11 -5
  29. data/lib/rubik/lsp/transport.rb +17 -7
  30. data/lib/rubik/mcp/transport/stdio.rb +42 -4
  31. data/lib/rubik/orchestrator/continuation_manager.rb +14 -5
  32. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
  33. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
  34. data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
  35. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
  36. data/lib/rubik/orchestrator/goal_session.rb +4 -4
  37. data/lib/rubik/orchestrator/loop.rb +8 -11
  38. data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
  39. data/lib/rubik/orchestrator/step_executor.rb +17 -2
  40. data/lib/rubik/orchestrator/subagent/instance.rb +24 -1
  41. data/lib/rubik/orchestrator/subagent/notices.rb +1 -1
  42. data/lib/rubik/orchestrator/subagent/reaper.rb +21 -2
  43. data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
  44. data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
  45. data/lib/rubik/orchestrator/subagent/supervisor.rb +10 -3
  46. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +27 -5
  47. data/lib/rubik/orchestrator/turn_executor.rb +24 -8
  48. data/lib/rubik/platform/filesystem.rb +52 -24
  49. data/lib/rubik/plugin/storage.rb +20 -0
  50. data/lib/rubik/prompt/context_builder.rb +2 -1
  51. data/lib/rubik/repair/loop.rb +22 -2
  52. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  53. data/lib/rubik/repl/commands/agent_execution.rb +20 -15
  54. data/lib/rubik/repl/commands/buffer.rb +15 -0
  55. data/lib/rubik/repl/commands/connect.rb +0 -1
  56. data/lib/rubik/repl/commands/lsp.rb +1 -1
  57. data/lib/rubik/repl/commands/plugins.rb +31 -31
  58. data/lib/rubik/repl/commands/session.rb +5 -5
  59. data/lib/rubik/repl/commands/workspace.rb +4 -4
  60. data/lib/rubik/repl/input_execution.rb +30 -29
  61. data/lib/rubik/repl/input_loop.rb +9 -4
  62. data/lib/rubik/repl/lifecycle.rb +1 -2
  63. data/lib/rubik/repl/notice_router.rb +11 -5
  64. data/lib/rubik/repl/paste_support.rb +1 -1
  65. data/lib/rubik/repl/prompt_grace.rb +3 -3
  66. data/lib/rubik/repl/shell.rb +10 -2
  67. data/lib/rubik/repl/turn_recorder.rb +14 -3
  68. data/lib/rubik/runtime/context.rb +33 -27
  69. data/lib/rubik/runtime/manager.rb +23 -15
  70. data/lib/rubik/sandbox/driver/bubblewrap.rb +58 -8
  71. data/lib/rubik/sandbox/driver/process.rb +32 -1
  72. data/lib/rubik/sandbox/runner.rb +9 -1
  73. data/lib/rubik/security/classifier/classifier.rb +147 -13
  74. data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
  75. data/lib/rubik/security/classifier/commands/git.yaml +27 -1
  76. data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
  77. data/lib/rubik/security/classifier/database.rb +2 -2
  78. data/lib/rubik/security/command_gate.rb +8 -1
  79. data/lib/rubik/security/redactor.rb +62 -19
  80. data/lib/rubik/session/recorder.rb +13 -18
  81. data/lib/rubik/session/replayer.rb +29 -7
  82. data/lib/rubik/storage/store.rb +19 -5
  83. data/lib/rubik/tools/builtins/exec.rb +1 -1
  84. data/lib/rubik/tools/builtins/files.rb +8 -12
  85. data/lib/rubik/tools/builtins/grep.rb +3 -2
  86. data/lib/rubik/tools/builtins/lsp.rb +31 -7
  87. data/lib/rubik/tools/builtins/show_file.rb +1 -1
  88. data/lib/rubik/tools/builtins/synthesis.rb +4 -4
  89. data/lib/rubik/tools/result_normalizer.rb +1 -1
  90. data/lib/rubik/ui/banner.rb +3 -3
  91. data/lib/rubik/ui/dialog.rb +10 -7
  92. data/lib/rubik/ui/diff_viewer.rb +4 -4
  93. data/lib/rubik/ui/frame.rb +13 -3
  94. data/lib/rubik/ui/markdown_stream.rb +8 -2
  95. data/lib/rubik/ui/panel.rb +11 -11
  96. data/lib/rubik/ui/renderer.rb +6 -6
  97. data/lib/rubik/ui/spinner.rb +1 -1
  98. data/lib/rubik/ui/statusline.rb +3 -3
  99. data/lib/rubik/ui/theme.rb +1 -1
  100. data/lib/rubik/version.rb +1 -1
  101. metadata +4 -1
@@ -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
@@ -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
- @buffer << chunk_bytes.to_s.b
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
- return [] if @buffer.empty?
45
+ events = []
46
+ unless @buffer.empty? && @raw_body.empty?
47
+ events = drain_complete_events(final: true)
37
48
 
38
- events = drain_complete_events(final: true)
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
- unless @buffer.empty?
41
- remaining = @buffer.dup
42
- @buffer = String.new(encoding: Encoding::ASCII_8BIT)
43
- parsed = parse_event(remaining)
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
- events << parsed if parsed
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
- events << parsed if parsed
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
 
@@ -177,13 +177,19 @@ module Rubik
177
177
  return unless running || @transport.resources?
178
178
 
179
179
  begin
180
- send_request("shutdown", {}, timeout: 2) if running
181
- send_notification("exit", {}) if running
180
+ if running
181
+ send_request("shutdown", {}, timeout: 2)
182
+ send_notification("exit", {})
183
+ end
182
184
  rescue StandardError
183
- # ignore
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
 
@@ -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
- offset = 0
261
- while offset < bytes.bytesize
262
- written = @stdin.write(bytes.byteslice(offset, bytes.bytesize - offset))
263
- raise IOError, "LSP transport made no write progress" unless written&.positive?
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.synchronize do
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.puts(json)
308
- @stdin.flush
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 çıktısı maksimum token sınırına (max_output_tokens: #{max_tokens}) ulaştığı için tamamlanamadan kesildi.\n" \
70
- "Çözüm: Çıktı token bütçesini artırmak için şu komutu çalıştırabilirsiniz:\n !config set llm.default_max_output_tokens #{rec}"
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 çıktısı maksimum token sınırına (max_output_tokens: #{max_tokens}) ulaştığı için " \
77
- "ve #{@max_auto_continues} otomatik devam denemesi tükendiği için durduruldu.\n" \
78
- "Çözüm: Çıktı token bütçesini artırmak için şu komutu çalıştırabilirsiniz:\n !config set llm.default_max_output_tokens #{rec}"
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 doc.is_a?(Hash) && doc["rev"].is_a?(Integer) &&
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, units)
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, units)
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
@@ -20,8 +20,27 @@ module Rubik
20
20
  # kalır ve validate içinde implicit self ile erişilir.
21
21
  extend DeepOps
22
22
 
23
+ # Σ belgesi cap'lidir (üstteki sözleşme): alan uzunlukları ve patch
24
+ # boyutu zaten sınırlıydı; koleksiyon başına öğe sayısı da sınırlanır.
25
+ # Yoksa pinli tek Σ satırı (refresh_in_history!) her adımda sınırsız
26
+ # büyür ve hiçbir compaction tier'ı onu küçültemez. SSOT burasıdır —
27
+ # Ledger.load da aynı tavanı okur.
28
+ MAX_ITEMS_PER_COLLECTION = 200
29
+
23
30
  module_function
24
31
 
32
+ # Σ gövde şekli kapısı (Ledger.load): koleksiyonlar dizi, madde
33
+ # listeleri Hash, skalar liste String olmalı; tavan SSOT burada.
34
+ def sigma_doc_well_formed?(doc, schema:)
35
+ return false unless doc.is_a?(Hash) && doc["rev"].is_a?(Integer) && doc["objective"]
36
+
37
+ schema.sections.all? do |name, spec|
38
+ coll = doc[name]
39
+ coll.is_a?(Array) && coll.size <= MAX_ITEMS_PER_COLLECTION &&
40
+ coll.all?(spec.item ? Hash : String)
41
+ end
42
+ end
43
+
25
44
  # patch_or_text: String (önce parse edilir) | GoalLedger::Patch
26
45
  # -> [:ok, new_doc, new_rev] | [:rejected, [reason, ...]]
27
46
  # Model girdisinde ASLA raise etmez (her başarısızlık veridir);
@@ -139,7 +158,7 @@ module Rubik
139
158
  return [err] if err
140
159
 
141
160
  coll << op[:value]
142
- return []
161
+ return collection_cap_rejection(op, name, coll)
143
162
  end
144
163
 
145
164
  idx = index_token(idx_tok, name, coll, op, insertable: op[:op] == :add)
@@ -148,14 +167,17 @@ module Rubik
148
167
  err = op[:op] == :remove ? nil : check_value(spec, op[:value], name, op)
149
168
  return [err] if err
150
169
 
151
- if op[:op] == :add
170
+ case op[:op]
171
+ when :add
152
172
  coll.insert(idx, op[:value])
153
- elsif op[:op] == :replace
173
+ collection_cap_rejection(op, name, coll)
174
+ when :replace
154
175
  coll[idx] = op[:value]
176
+ []
155
177
  else
156
178
  coll.delete_at(idx)
179
+ []
157
180
  end
158
- []
159
181
  end
160
182
 
161
183
  # ---- item listesi (tasks/findings/...) ----
@@ -179,7 +201,7 @@ module Rubik
179
201
  return errs unless errs.empty?
180
202
 
181
203
  coll << op[:value]
182
- return dup_id_rejections(coll, spec, name, op)
204
+ return dup_id_rejections(coll, spec, name, op) + collection_cap_rejection(op, name, coll)
183
205
  end
184
206
 
185
207
  idx = index_token(idx_tok, name, coll, op, insertable: op[:op] == :add)
@@ -193,8 +215,13 @@ module Rubik
193
215
  errs = validate_item_object(spec, op[:value], name, op)
194
216
  return errs unless errs.empty?
195
217
 
196
- op[:op] == :add ? coll.insert(idx, op[:value]) : coll[idx] = op[:value]
197
- dup_id_rejections(coll, spec, name, op)
218
+ if op[:op] == :add
219
+ coll.insert(idx, op[:value])
220
+ dup_id_rejections(coll, spec, name, op) + collection_cap_rejection(op, name, coll)
221
+ else
222
+ coll[idx] = op[:value]
223
+ dup_id_rejections(coll, spec, name, op)
224
+ end
198
225
  end
199
226
 
200
227
  def field_op(op, name, spec, coll, idx_tok)
@@ -327,6 +354,18 @@ module Rubik
327
354
  "use a numeric index for replace/remove")
328
355
  end
329
356
 
357
+ # Koleksiyon başına öğe tavanı: yalnız BÜYÜTEN op'lar reddedilir;
358
+ # replace/remove her zaman geçer (mevcut fazla öğeler temizlenebilir).
359
+ # Working copy zaten mutasyona uğramış olsa da ret tüm patch'i çöpe
360
+ # atar — current_doc dokunulmaz (atomiklik korunur).
361
+ def collection_cap_rejection(op, name, coll)
362
+ return [] if coll.size <= MAX_ITEMS_PER_COLLECTION
363
+
364
+ [rejection(:schema, op[:index], op[:path],
365
+ "#{name} is at its cap of #{MAX_ITEMS_PER_COLLECTION} items",
366
+ "remove an existing #{name} item before adding another")]
367
+ end
368
+
330
369
  def dup_id_rejections(coll, spec, name, op)
331
370
  return [] unless spec.id_field
332
371
 
@@ -93,9 +93,16 @@ module Rubik
93
93
  # Kesilmiş okumada needle yokluğu KANITLANAMAZ → unknown (tri-state
94
94
  # dürüstlüğünün kalbi); tam okumada yokluk gerçek yokluktur → failed.
95
95
  def needle_read(spec, expanded)
96
- size = File.size(expanded)
96
+ # Missing path is a proven false (same contract as file_exists) — never
97
+ # let Errno::ENOENT fall through to the generic :unknown rescue.
98
+ return [:failed, "file_contains #{spec[:path]}: missing"] unless File.exist?(expanded)
97
99
  return [:failed, "file_contains #{spec[:path]}: path is a directory"] if File.directory?(expanded)
100
+ # A FIFO/socket/device reports File.size == 0 and File.open would block
101
+ # forever waiting for a writer (the verify path is model-authored).
102
+ # Reject non-regular files up front so they report :unknown, never hang.
103
+ return [:unknown, "file_contains #{spec[:path]}: not a regular file"] unless File.file?(expanded)
98
104
 
105
+ size = File.size(expanded)
99
106
  body = File.open(expanded, "rb") { |f| f.read(VerifySpec::READ_CAP_BYTES) }.to_s
100
107
  body = Rubik::Platform.safe_utf8(body)
101
108
  return [:ok, "file_contains"] if body.include?(spec[:needle])
@@ -203,8 +203,8 @@ module Rubik
203
203
  while !res[:ok] && res[:reason] == :budget_exhausted
204
204
  if auto_extensions < max_auto
205
205
  auto_extensions += 1
206
- ctx.out.puts "\n🤖 [Rubik::Goal] Adım bütçesi doldu (#{res[:error]}). " \
207
- "Otonom devam hakkı (#{auto_extensions}/#{max_auto}) kullanılarak +20 adım ekleniyor..."
206
+ ctx.out.puts "\n🤖 [Rubik::Goal] Step budget exhausted (#{res[:error]}). " \
207
+ "Using autonomous continue allowance (#{auto_extensions}/#{max_auto}), +20 steps are being added..."
208
208
  goal_state.extend!(goal_state.max_steps + 20)
209
209
  res = orchestrator.continue_loop(20, auto: true)
210
210
  next
@@ -213,10 +213,10 @@ module Rubik
213
213
  break unless Rubik::Storage::Layout.config_get(ctx.config, "agent.ask_continue_on_limit", true)
214
214
 
215
215
  ctx.out.puts "\n⚠️ [Rubik::Goal] #{res[:error]}"
216
- ans = ctx.ask_yes_no(" [Rubik::Goal] +20 adım ile devam edilsin mi?", default: "n")
216
+ ans = ctx.ask_yes_no(" [Rubik::Goal] Continue with +20 steps?", default: "n")
217
217
  break unless ["y", "yes", "evet"].include?(ans)
218
218
 
219
- ctx.out.puts " [Rubik::Goal] Adım bütçesi +20 artırılıyor ve hedef sürdürülüyor..."
219
+ ctx.out.puts " [Rubik::Goal] Increasing step budget by +20 and resuming the goal..."
220
220
  goal_state.extend!(goal_state.max_steps + 20)
221
221
  res = orchestrator.continue_loop(20)
222
222
  end