rubik 0.13.0 → 0.15.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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/ATTRIBUTION.md +1 -1
  3. data/CHANGELOG.md +122 -1
  4. data/lib/rubik/adapter/node.rb +4 -1
  5. data/lib/rubik/ast/c_extractor.rb +1 -1
  6. data/lib/rubik/ast/fallback_cleaner.rb +6 -1
  7. data/lib/rubik/ast/graph.rb +38 -57
  8. data/lib/rubik/ast/graph_extensions.rb +57 -15
  9. data/lib/rubik/ast/ruby_extractor.rb +16 -6
  10. data/lib/rubik/ast/treesitter/c_walker.rb +11 -3
  11. data/lib/rubik/buffer/manager.rb +38 -11
  12. data/lib/rubik/buffer/snapshot.rb +116 -28
  13. data/lib/rubik/cli/connect.rb +3 -5
  14. data/lib/rubik/config/defaults.rb +3 -7
  15. data/lib/rubik/config/env_guard.rb +43 -0
  16. data/lib/rubik/config/schema.rb +7 -1
  17. data/lib/rubik/config/scoped.rb +12 -5
  18. data/lib/rubik/config/secret_policy.rb +6 -5
  19. data/lib/rubik/config.rb +1 -1
  20. data/lib/rubik/connect/models_dev.rb +11 -0
  21. data/lib/rubik/connect/probes.rb +9 -2
  22. data/lib/rubik/connect/wizard.rb +117 -57
  23. data/lib/rubik/context/repo_context_reader.rb +8 -4
  24. data/lib/rubik/diagnostics/collector.rb +25 -2
  25. data/lib/rubik/llm/chunk_assembler.rb +12 -1
  26. data/lib/rubik/llm/client.rb +19 -0
  27. data/lib/rubik/llm/connection_pool.rb +7 -1
  28. data/lib/rubik/llm/formatter.rb +22 -2
  29. data/lib/rubik/llm/provider_key.rb +32 -0
  30. data/lib/rubik/llm/registry.rb +18 -2
  31. data/lib/rubik/llm/router.rb +27 -1
  32. data/lib/rubik/llm/session_header.rb +128 -0
  33. data/lib/rubik/llm/sse_parser.rb +57 -10
  34. data/lib/rubik/lsp/client.rb +244 -23
  35. data/lib/rubik/lsp/document_store.rb +6 -0
  36. data/lib/rubik/lsp/manager.rb +14 -0
  37. data/lib/rubik/lsp/transport.rb +34 -7
  38. data/lib/rubik/mcp/transport/stdio.rb +42 -4
  39. data/lib/rubik/orchestrator/continuation_manager.rb +68 -7
  40. data/lib/rubik/orchestrator/goal_ledger/ledger.rb +18 -2
  41. data/lib/rubik/orchestrator/goal_ledger/observation_projection.rb +13 -4
  42. data/lib/rubik/orchestrator/goal_ledger/validator.rb +46 -7
  43. data/lib/rubik/orchestrator/goal_ledger/verifier.rb +8 -1
  44. data/lib/rubik/orchestrator/goal_session.rb +14 -14
  45. data/lib/rubik/orchestrator/goal_wait.rb +15 -9
  46. data/lib/rubik/orchestrator/loop.rb +39 -39
  47. data/lib/rubik/orchestrator/provider_resolver.rb +29 -6
  48. data/lib/rubik/orchestrator/step_executor.rb +17 -2
  49. data/lib/rubik/orchestrator/subagent/instance.rb +46 -5
  50. data/lib/rubik/orchestrator/subagent/lifecycle.rb +25 -25
  51. data/lib/rubik/orchestrator/subagent/messaging.rb +5 -1
  52. data/lib/rubik/orchestrator/subagent/narration.rb +2 -1
  53. data/lib/rubik/orchestrator/subagent/notices.rb +7 -3
  54. data/lib/rubik/orchestrator/subagent/questioner.rb +42 -4
  55. data/lib/rubik/orchestrator/subagent/reaper.rb +70 -20
  56. data/lib/rubik/orchestrator/subagent/start_gate.rb +1 -1
  57. data/lib/rubik/orchestrator/subagent/step_runner.rb +7 -7
  58. data/lib/rubik/orchestrator/subagent/supervisor.rb +21 -3
  59. data/lib/rubik/orchestrator/tool_call_id.rb +8 -4
  60. data/lib/rubik/orchestrator/turn_executor/subagent_channel.rb +21 -5
  61. data/lib/rubik/orchestrator/turn_executor/user_channel.rb +40 -5
  62. data/lib/rubik/orchestrator/turn_executor.rb +24 -8
  63. data/lib/rubik/platform/filesystem.rb +52 -24
  64. data/lib/rubik/platform/process.rb +32 -7
  65. data/lib/rubik/platform/sanitize.rb +17 -18
  66. data/lib/rubik/platform.rb +27 -0
  67. data/lib/rubik/plugin/storage.rb +34 -8
  68. data/lib/rubik/prompt/context_builder.rb +2 -1
  69. data/lib/rubik/prompt/doctrine.rb +3 -2
  70. data/lib/rubik/repair/loop.rb +22 -2
  71. data/lib/rubik/repl/commands/agent_context.rb +1 -1
  72. data/lib/rubik/repl/commands/agent_execution.rb +20 -15
  73. data/lib/rubik/repl/commands/base.rb +7 -3
  74. data/lib/rubik/repl/commands/buffer.rb +21 -2
  75. data/lib/rubik/repl/commands/connect.rb +0 -1
  76. data/lib/rubik/repl/commands/lsp.rb +1 -1
  77. data/lib/rubik/repl/commands/plugins.rb +31 -31
  78. data/lib/rubik/repl/commands/session.rb +16 -6
  79. data/lib/rubik/repl/commands/workspace.rb +4 -4
  80. data/lib/rubik/repl/input_execution.rb +33 -32
  81. data/lib/rubik/repl/input_loop.rb +9 -4
  82. data/lib/rubik/repl/lifecycle.rb +1 -2
  83. data/lib/rubik/repl/notice_router.rb +11 -5
  84. data/lib/rubik/repl/paste_support.rb +1 -1
  85. data/lib/rubik/repl/prompt_grace.rb +3 -3
  86. data/lib/rubik/repl/service_catalog.rb +6 -2
  87. data/lib/rubik/repl/shell.rb +10 -2
  88. data/lib/rubik/repl/turn_recorder.rb +18 -4
  89. data/lib/rubik/runtime/context.rb +33 -27
  90. data/lib/rubik/runtime/manager.rb +23 -15
  91. data/lib/rubik/runtime/manifest_store.rb +7 -2
  92. data/lib/rubik/runtime/task_manager.rb +13 -10
  93. data/lib/rubik/sandbox/driver/base.rb +34 -0
  94. data/lib/rubik/sandbox/driver/bubblewrap.rb +60 -8
  95. data/lib/rubik/sandbox/driver/docker.rb +8 -0
  96. data/lib/rubik/sandbox/driver/podman.rb +8 -0
  97. data/lib/rubik/sandbox/driver/process.rb +29 -1
  98. data/lib/rubik/sandbox/runner.rb +9 -1
  99. data/lib/rubik/security/classifier/classifier.rb +147 -13
  100. data/lib/rubik/security/classifier/commands/docker.yaml +1 -1
  101. data/lib/rubik/security/classifier/commands/git.yaml +27 -1
  102. data/lib/rubik/security/classifier/commands/sed.yaml +3 -3
  103. data/lib/rubik/security/classifier/database.rb +2 -2
  104. data/lib/rubik/security/command_gate.rb +8 -1
  105. data/lib/rubik/security/path_policy.rb +28 -3
  106. data/lib/rubik/security/redactor.rb +62 -19
  107. data/lib/rubik/session/recorder.rb +27 -20
  108. data/lib/rubik/session/replayer.rb +29 -7
  109. data/lib/rubik/storage/store.rb +26 -5
  110. data/lib/rubik/tools/builtins/exec.rb +8 -2
  111. data/lib/rubik/tools/builtins/files.rb +8 -12
  112. data/lib/rubik/tools/builtins/grep.rb +3 -2
  113. data/lib/rubik/tools/builtins/interactive.rb +5 -2
  114. data/lib/rubik/tools/builtins/lsp.rb +59 -9
  115. data/lib/rubik/tools/builtins/send_message.rb +7 -2
  116. data/lib/rubik/tools/builtins/show_file.rb +1 -1
  117. data/lib/rubik/tools/builtins/synthesis.rb +4 -4
  118. data/lib/rubik/tools/preflight_runner.rb +4 -1
  119. data/lib/rubik/tools/result_normalizer.rb +1 -1
  120. data/lib/rubik/ui/banner.rb +3 -3
  121. data/lib/rubik/ui/dialog.rb +15 -8
  122. data/lib/rubik/ui/diff_viewer.rb +4 -4
  123. data/lib/rubik/ui/frame.rb +13 -3
  124. data/lib/rubik/ui/markdown_stream.rb +8 -2
  125. data/lib/rubik/ui/panel.rb +11 -11
  126. data/lib/rubik/ui/renderer.rb +10 -7
  127. data/lib/rubik/ui/spinner.rb +1 -1
  128. data/lib/rubik/ui/statusline.rb +3 -3
  129. data/lib/rubik/ui/theme.rb +1 -1
  130. data/lib/rubik/version.rb +1 -1
  131. metadata +4 -1
@@ -32,6 +32,18 @@ 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
+
41
+ # Upper bound on a single framed message the reader accepts (issue #601).
42
+ # A wedged/corrupt server emitting a garbage oversized Content-Length
43
+ # would otherwise park the reader thread on a blocking read of that many
44
+ # bytes. 16 MiB is far above any legitimate LSP payload.
45
+ MAX_FRAME_SIZE = 16 * 1024 * 1024
46
+
35
47
  attr_reader :mutex, :cv, :running
36
48
 
37
49
  def initialize(root_dir:, on_message:)
@@ -257,13 +269,11 @@ module Rubik
257
269
  header = "Content-Length: #{json.bytesize}\r\n\r\n"
258
270
  bytes = header + json
259
271
  @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
272
+ # Deadline-bounded write: the old @stdin.write loop could park here
273
+ # forever while holding @write_mutex. Client#stop sends shutdown
274
+ # through this mutex, so a stalled write prevented it from ever
275
+ # reaching the kill_tree that unblocks the pipe.
276
+ Rubik::Platform.write_until(@stdin, bytes, timeout: write_timeout)
267
277
  @stdin.flush
268
278
  end
269
279
  true
@@ -278,6 +288,12 @@ module Rubik
278
288
  false
279
289
  end
280
290
 
291
+ # Single source for the write deadline; overridable in tests so a
292
+ # non-draining pipe can be exercised without waiting the full 5s.
293
+ def write_timeout
294
+ WRITE_TIMEOUT
295
+ end
296
+
281
297
  # Correlate an incoming response with the pending-request registry.
282
298
  # Server-request ids and late responses are discarded here exactly as
283
299
  # before the extraction (issue #206).
@@ -321,6 +337,17 @@ module Rubik
321
337
 
322
338
  break if content_length.nil?
323
339
 
340
+ # Issue #601: a non-positive or oversized wire-supplied
341
+ # Content-Length means corrupt framing — fail the reader
342
+ # instead of parking on an unbounded blocking read.
343
+ if content_length <= 0 || content_length > MAX_FRAME_SIZE
344
+ @mutex.synchronize do
345
+ @reader_failure = "LSP server sent invalid Content-Length: #{content_length} " \
346
+ "(must be > 0 and <= #{MAX_FRAME_SIZE})"
347
+ end
348
+ break
349
+ end
350
+
324
351
  body = @stdout.read(content_length)
325
352
  break unless body
326
353
 
@@ -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
 
@@ -16,6 +16,16 @@ module Rubik
16
16
  class ContinuationManager
17
17
  TRUNCATION_REASONS = ["length", "max_tokens"].freeze
18
18
 
19
+ # The continuation nudges handle_truncation appends right after each
20
+ # partial row; they anchor absorb_partial_rows! to the genuine partial
21
+ # positions (issue #595-1 follow-up). Keep in sync with the nudge
22
+ # strings inside handle_truncation.
23
+ NUDGE_PREFIXES = [
24
+ "Your thinking process reached the output token limit",
25
+ "Your previous response was cut off",
26
+ "Your tool call was cut off"
27
+ ].freeze
28
+
19
29
  attr_reader :auto_continue_count, :continuation_buffer, :max_auto_continues
20
30
 
21
31
  def initialize(config: nil, max_auto_continues: nil)
@@ -23,6 +33,10 @@ module Rubik
23
33
  @max_auto_continues = (max_auto_continues || safe_config("llm.max_auto_continues", 3)).to_i
24
34
  @auto_continue_count = 0
25
35
  @continuation_buffer = +""
36
+ # The exact partial texts appended to history per text-truncation
37
+ # cycle (issue #595-1): the stitch/failure paths absorb their
38
+ # assistant rows so the history carries the full text ONCE.
39
+ @partial_texts = []
26
40
  end
27
41
 
28
42
  def enabled?
@@ -66,16 +80,16 @@ module Rubik
66
80
 
67
81
  unless enabled?
68
82
  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}"
83
+ diag = "⚠️ Model output reached the maximum token limit (max_output_tokens: #{max_tokens}) and was cut off before completion.\n" \
84
+ "Solution: To increase the output token budget, you can run the following command:\n !config set llm.default_max_output_tokens #{rec}"
71
85
  return { continue: false, error: diag }
72
86
  end
73
87
 
74
88
  if ceiling_reached?
75
89
  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}"
90
+ diag = "⚠️ Model output reached the maximum token limit (max_output_tokens: #{max_tokens}) and " \
91
+ "the #{@max_auto_continues} automatic continue attempts were exhausted, so it was stopped.\n" \
92
+ "Solution: To increase the output token budget, you can run the following command:\n !config set llm.default_max_output_tokens #{rec}"
79
93
  return { continue: false, error: diag }
80
94
  end
81
95
 
@@ -94,6 +108,7 @@ module Rubik
94
108
  }
95
109
  when :text_truncated
96
110
  @continuation_buffer << text_resp.to_s
111
+ @partial_texts << text_resp.to_s
97
112
  {
98
113
  continue: true,
99
114
  scenario: :text_truncated,
@@ -114,8 +129,12 @@ module Rubik
114
129
  end
115
130
 
116
131
  # Stitches any accumulated continuation buffer with the final response
117
- # text and resets the continuation state.
118
- def stitch_and_reset(final_text)
132
+ # text and resets the continuation state. When `history` is given, the
133
+ # partial assistant rows appended per continuation cycle are absorbed
134
+ # FIRST (issue #595-1): the stitched full text replaces them instead
135
+ # of duplicating every partial.
136
+ def stitch_and_reset(final_text, history: nil)
137
+ absorb_partial_rows!(history) if history
119
138
  stitched = if @continuation_buffer.empty?
120
139
  final_text.to_s
121
140
  else
@@ -125,9 +144,51 @@ module Rubik
125
144
  stitched
126
145
  end
127
146
 
147
+ # Absorbs the partial assistant rows recorded across continuation
148
+ # cycles out of the given history (issue #595-1/#595-2). Anchoring
149
+ # (#595-1 follow-up): a partial row is always the assistant row
150
+ # immediately preceding its own continuation nudge, so matching is
151
+ # restricted to that position — content equality alone would also
152
+ # delete an unrelated earlier assistant row that coincidentally
153
+ # repeats a partial verbatim.
154
+ def absorb_partial_rows!(history)
155
+ @partial_texts.each do |partial|
156
+ history.each_index do |i|
157
+ row = history[i]
158
+ next unless row.is_a?(Hash)
159
+ next unless continuation_nudge_row?(history[i + 1])
160
+
161
+ role = row[:role] || row["role"]
162
+ tool_calls = row[:tool_calls] || row["tool_calls"]
163
+ content = row[:content] || row["content"]
164
+ history[i] = nil if role == "assistant" && tool_calls.nil? && content == partial
165
+ end
166
+ history.compact!
167
+ end
168
+ end
169
+
170
+ # The continuation nudges appended right after each partial row are
171
+ # the anchor markers (handle_truncation is the only writer).
172
+ def continuation_nudge_row?(row)
173
+ return false unless row.is_a?(Hash)
174
+
175
+ content = (row[:content] || row["content"]).to_s
176
+ (row[:role] || row["role"]) == "user" && NUDGE_PREFIXES.any? { |prefix| content.start_with?(prefix) }
177
+ end
178
+
128
179
  def reset!
129
180
  @auto_continue_count = 0
130
181
  @continuation_buffer = +""
182
+ @partial_texts = []
183
+ end
184
+
185
+ # A successful turn closes the *consecutive* truncation chain, so the
186
+ # ceiling must start over (issue #549). The partial text buffer is NOT
187
+ # cleared here: stitch_and_reset owns that, and dropping it on a tool
188
+ # turn would lose the already-generated answer. Only the strike count
189
+ # is reset.
190
+ def reset_strikes!
191
+ @auto_continue_count = 0
131
192
  end
132
193
 
133
194
  private
@@ -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
@@ -274,7 +274,7 @@ module Rubik
274
274
 
275
275
  def resume_in_process(ctx, entry)
276
276
  ctx.recorder&.record_user_turn("!goal resume")
277
- # Kaynaklama (issue #454): canlı girdiden; resume! geçişi (SUSPENDED → RUNNING) kaynaklamanın parçası.
277
+ # Kaynaklama (issue #454): resume! geçişi (SUSPENDED → RUNNING) kaynaklamanın parçası.
278
278
  goal_state = entry.goal_state
279
279
  goal_ledger = entry.ledger
280
280
  goal_state.resume!
@@ -491,23 +491,23 @@ module Rubik
491
491
  end
492
492
  private_class_method :build_goal_orchestrator
493
493
 
494
- # Damga meta'sındaki suspended_at (iso8601) → Time; bozuk değer
495
- # nil — Loop#append_resume_marker! "unknown" downtime basar.
494
+ # suspended_at (iso8601) → Time; bozuk değer nil — marker "unknown" basar.
496
495
  def parse_suspended_at(value)
497
496
  Time.iso8601(value.to_s)
498
497
  rescue ArgumentError, TypeError
499
498
  nil
500
499
  end
501
500
 
502
- # Defense-in-depth behind the loop-level goal guard: if a success
503
- # terminal escapes GoalWait with live subagent units, the goal must
504
- # stop honestly instead of celebrating a false completion. Fails
505
- # CONSERVATIVE: coeffect absence is nil-safe (Coeffects.get never
506
- # raises for a missing subsystem), so a raise here is real breakage
507
- # — reporting zero would launder it into exactly the false
508
- # "Goal Achieved" this backstop exists to refuse.
501
+ # Defense-in-depth behind the loop-level goal guard: a success terminal
502
+ # that escapes GoalWait over live units must stop honestly, not celebrate.
503
+ # Fails CONSERVATIVE: coeffect absence is nil-safe, so a raise is real
504
+ # breakage — zero would launder it into the false "Goal Achieved".
509
505
  def goal_active_subagent_count(ctx)
510
506
  supervisor = Rubik::Coeffects.get(ctx.runtime_manager&.context, Rubik::Coeffects::SUBAGENT_SUPERVISOR)
507
+ # Issue #594: dispatched_count covers :budget_exhausted units parked
508
+ # mid-task; doubles predating it still answer through active_count.
509
+ return supervisor.dispatched_count.to_i if supervisor.respond_to?(:dispatched_count)
510
+
511
511
  supervisor&.active_count.to_i
512
512
  rescue StandardError => e
513
513
  Rubik::Log.warn(:orchestrator, "goal completion defense could not inspect subagents " \
@@ -21,16 +21,22 @@ module Rubik
21
21
  @deps = deps
22
22
  end
23
23
 
24
- # Live dispatched work: any active unit (running, paused on error or
25
- # budget, or blocked on an ask_question answer) counts — none of them
26
- # may be abandoned by a completion. A nil supervisor (no subagent
27
- # system, bare test loops) means nothing is pending.
24
+ # Live dispatched work: any in-flight unit (running, paused on error
25
+ # or budget, parked at :budget_exhausted, or blocked on an
26
+ # ask_question answer) counts — none of them may be abandoned by a
27
+ # completion (issue #594: :budget_exhausted is parked mid-task, not
28
+ # finished). A nil supervisor (no subagent system, bare test loops)
29
+ # means nothing is pending. Test-double supervisors that predate
30
+ # dispatched_count still answer through active_count.
28
31
  def pending?
29
- active_count.positive?
32
+ dispatched_count.positive?
30
33
  end
31
34
 
32
- def active_count
33
- supervisor&.active_count.to_i
35
+ def dispatched_count
36
+ sup = supervisor
37
+ return sup.dispatched_count.to_i if sup.respond_to?(:dispatched_count)
38
+
39
+ sup&.active_count.to_i
34
40
  end
35
41
 
36
42
  # Called by the user channel INSTEAD of building the success
@@ -43,7 +49,7 @@ module Rubik
43
49
  return inject_notices! unless inbox.empty?
44
50
 
45
51
  timeout = wait_timeout
46
- @deps.on_step&.call(type: :goal_wait, via: via, active: active_count, timeout: timeout,
52
+ @deps.on_step&.call(type: :goal_wait, via: via, active: dispatched_count, timeout: timeout,
47
53
  step: @deps.step_count, max_steps: @deps.max_steps)
48
54
  # Notices arriving while the model was concluding sit in the inbox;
49
55
  # wait_for blocks until the whole dispatched set settles (budget-
@@ -98,7 +104,7 @@ module Rubik
98
104
  # falsely completing.
99
105
  def timeout_nudge!
100
106
  append_user_message!(
101
- "<goal_wait_timeout active=\"#{active_count}\" timeout=\"#{wait_timeout}\">#{active_count} dispatched " \
107
+ "<goal_wait_timeout active=\"#{dispatched_count}\" timeout=\"#{wait_timeout}\">#{dispatched_count} dispatched " \
102
108
  "subagent(s) are still running. The goal is NOT complete while they run. Conclude your turn again to " \
103
109
  "keep waiting, or inspect and steer them (`manage_subagents` action 'status' | 'wait' | 'kill'). " \
104
110
  "Call finish only once every dispatched subagent has settled.</goal_wait_timeout>"