samagotchi 0.69.0 → 0.70.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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -1
  3. data/docs/configuration.md +3 -2
  4. data/docs/context.md +15 -6
  5. data/docs/hooks.md +1 -1
  6. data/docs/internals/background-tasks.md +1 -1
  7. data/docs/internals/engine.md +10 -5
  8. data/docs/internals/tool-guardrails.md +61 -1
  9. data/docs/plugins.md +24 -19
  10. data/docs/sessions.md +2 -2
  11. data/docs/sub-agent.md +2 -1
  12. data/docs/testing.md +1 -0
  13. data/lib/samagotchi/bridge/turn_accumulator.rb +2 -1
  14. data/lib/samagotchi/bundles/claude-peer/manifest.yml +10 -8
  15. data/lib/samagotchi/bundles/claude-peer/plugin.rb +4 -4
  16. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +6 -6
  17. data/lib/samagotchi/bundles/claude-peer/scripts/pair +3 -3
  18. data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +6 -5
  19. data/lib/samagotchi/bundles/codex-peer/manifest.yml +10 -8
  20. data/lib/samagotchi/bundles/codex-peer/plugin.rb +4 -4
  21. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +6 -6
  22. data/lib/samagotchi/bundles/codex-peer/scripts/pair +3 -3
  23. data/lib/samagotchi/bundles/codex-peer/skill_codex_peer.md +6 -5
  24. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  25. data/lib/samagotchi/config.rb +6 -2
  26. data/lib/samagotchi/context_absorber.rb +35 -21
  27. data/lib/samagotchi/engine.rb +2 -0
  28. data/lib/samagotchi/kernel_loop.rb +10 -0
  29. data/lib/samagotchi/llm_context_stale.rb +2 -2
  30. data/lib/samagotchi/log_subscriber.rb +1 -0
  31. data/lib/samagotchi/peer_sources.rb +1 -1
  32. data/lib/samagotchi/plugin/api.rb +3 -2
  33. data/lib/samagotchi/plugin/peer.rb +35 -16
  34. data/lib/samagotchi/tool_output_cut.rb +312 -0
  35. data/lib/samagotchi/tool_output_files.rb +73 -0
  36. data/lib/samagotchi/tool_runner.rb +46 -12
  37. data/lib/samagotchi/tools/delegate.rb +4 -1
  38. data/lib/samagotchi/tools/delegate_wait.rb +12 -1
  39. data/lib/samagotchi/tools/ref_text.rb +27 -0
  40. data/lib/samagotchi/tools/task_wait.rb +4 -1
  41. data/lib/samagotchi/version.rb +1 -1
  42. data/lib/samagotchi/web/message_parts.rb +4 -2
  43. data/lib/samagotchi/worker.rb +2 -2
  44. data/lib/samagotchi/worker_inbound.rb +3 -0
  45. metadata +4 -1
@@ -0,0 +1,312 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tool_output_files"
4
+
5
+ module Samagotchi
6
+ # How ToolRunner cuts a tool's output that is longer than
7
+ # max_tool_output_chars, by the tool's policy (POLICIES, by its built-in
8
+ # name; any other tool gets :head).
9
+ #
10
+ # :head keeps the first chars and ends with head_line ("[cut: N of M
11
+ # chars; read it in parts]"): read (which has line ranges) and every tool
12
+ # with no policy of its own.
13
+ #
14
+ # :head_tail_file (execute) keeps a short head and a long tail, whole
15
+ # lines, with one gap line between them, and saves the whole output
16
+ # (ToolOutputFiles, through the caller's +save+): a test run's verdict and
17
+ # an error are at the end. The gap line names the saved file and the lines
18
+ # left out of it, so the model reads or rg's it instead of running the
19
+ # command again. execute's stdout and stderr are cut each on its own
20
+ # (stdout gets about two thirds of the room), its exit line always kept.
21
+ # The cut text still fits the cap, and comes from the content alone (no
22
+ # clock): the same output gives the same text.
23
+ #
24
+ # :head_tail_ref (task_wait, delegate, delegate_result) cuts the same
25
+ # way, as one block, and saves nothing: the whole is already in a file (a
26
+ # task's log, the child's reply in its output/ folder), which the tool
27
+ # gave with its text (Tools::RefText) and the gap line names.
28
+ module ToolOutputCut
29
+ POLICIES = { "execute" => :head_tail_file, "task_wait" => :head_tail_ref, "delegate" => :head_tail_ref,
30
+ "delegate_result" => :head_tail_ref }.freeze
31
+
32
+ # What a :head_tail_ref tool's file holds, for its gap line.
33
+ REF_WHAT = { "task_wait" => "the task's whole output", "delegate" => "the child's whole reply",
34
+ "delegate_result" => "the child's whole reply" }.freeze
35
+
36
+ # A head cut's last line (head_line), as LLMContextStale recognizes a
37
+ # partial read by it. Kept in sync with head_line by its spec.
38
+ HEAD_CUT = /\[cut: \d+ of \d+ chars; read it in parts\]\z/
39
+
40
+ # The head's chars at most (execute.cut_head_chars): never more than a
41
+ # quarter of the room; the tail gets the rest.
42
+ HEAD_CHARS = 1000
43
+ # A tail shorter than this isn't worth it: the cap is too small, and
44
+ # the output gets the head cut.
45
+ MIN_TAIL = 200
46
+
47
+ # An execute that didn't finish: stopped, killed at the limit or
48
+ # handed over (its result starts with an "Error:" or a "Still running"
49
+ # line after the lead), or with no exit line last.
50
+ UNFINISHED = /\A(?:ran as: [^\n]*\n)?\[execute\]\n(?:Error: |Still running at the )/
51
+
52
+ # The marks of a text that is already a head-and-tail preview (execute's
53
+ # own above execute.truncate_at_bytes): its saved copy isn't the whole
54
+ # output.
55
+ PREVIEW_MARK = "[TRUNCATED_PREVIEW_HEAD]"
56
+ # execute's blocks (Tools::Execute#call): stdout, stderr, then the exit
57
+ # line last (a hint in brackets may follow it). A killed or handed-over
58
+ # command has none: its output isn't all there is.
59
+ STDOUT_LABEL = "\nstdout:\n"
60
+ STDERR_LABEL = "\nstderr:\n"
61
+ EXIT_LINE = /\nexit: -?\d+[^\n]*(?:\n\([^\n]*\))?\z/
62
+
63
+ # The share of the room stdout gets when both blocks are cut.
64
+ STDOUT_SHARE = Rational(2, 3)
65
+
66
+ # A cut part: [from, length] of the output.
67
+ Block = Data.define(:from, :length) do
68
+ def to = from + length
69
+ end
70
+
71
+ module_function
72
+
73
+ # @param tool [String, nil] the call's tool name
74
+ # @return [Symbol] its policy
75
+ def policy(tool) = POLICIES.fetch(tool.to_s, :head)
76
+
77
+ # +output+ cut to +cap+ chars by +tool+'s policy.
78
+ # @param save [#call, nil] (text, dry:) → ToolOutputFiles::Saved, or a
79
+ # String saying why it wasn't saved ("no session", "Errno::ENOSPC");
80
+ # dry: true only says where it would go. It writes only once the
81
+ # head-and-tail cut is known to fit the cap with that file's gap
82
+ # line: a cut that falls back to the head cut saves nothing.
83
+ # @param head_chars [Integer] execute.cut_head_chars
84
+ # @return [Array(String, Object)] the text the model gets, and what
85
+ # +save+ returned (nil when it wasn't called)
86
+ # @param ref [String, nil] :head_tail_ref: the file the whole text is in
87
+ # @param ref_stderr [String, nil] :head_tail_ref: its stderr's file, if apart
88
+ def apply(output, cap:, tool:, save: nil, ref: nil, ref_stderr: nil, head_chars: HEAD_CHARS)
89
+ kind = policy(tool)
90
+ return [head(output, cap), nil] if kind == :head || cap < MIN_TAIL * 3
91
+
92
+ if kind == :head_tail_ref
93
+ text = head_tail(output, cap: cap, blocks: [Block.new(from: 0, length: output.length)], head_chars: head_chars,
94
+ gap: RefGap.new(output, ref, REF_WHAT.fetch(tool.to_s, "the whole output"),
95
+ stderr: ref_stderr))
96
+ return [text || head(output, cap), nil]
97
+ end
98
+
99
+ blocks = execute_blocks(output)
100
+ planned = save ? save.call(output, dry: true) : "no session"
101
+ text = head_tail(output, cap: cap, blocks: blocks, head_chars: head_chars, gap: FileGap.new(output, planned))
102
+ return [head(output, cap), nil] unless text
103
+ return [text, planned] unless planned.is_a?(ToolOutputFiles::Saved)
104
+
105
+ saved = save.call(output)
106
+ return [text, saved] if saved.is_a?(ToolOutputFiles::Saved)
107
+
108
+ # The write failed: the line says so instead, if that still fits.
109
+ text = head_tail(output, cap: cap, blocks: blocks, head_chars: head_chars, gap: FileGap.new(output, saved))
110
+ [text || head(output, cap), text ? saved : nil]
111
+ end
112
+
113
+ def unfinished?(output) = output.match?(UNFINISHED) || !output.match?(EXIT_LINE)
114
+
115
+ # +output+ as a web row keeps it, at most +cap+ chars (a reload's
116
+ # MessageParts, the Bridge's snapshot): a read's head (its view numbers
117
+ # the lines from the first), any other tool's head and tail with a gap
118
+ # line, as the model's cut keeps a test run's verdict and exit line.
119
+ def preview(output, cap:, tool:)
120
+ return output if output.length <= cap
121
+ return output[0, cap] if tool.to_s == "read"
122
+
123
+ head_tail(output, cap: cap, blocks: [Block.new(from: 0, length: output.length)], head_chars: HEAD_CHARS,
124
+ gap: RefGap.new(output, nil, nil)) || output[0, cap]
125
+ end
126
+
127
+ # +output+ cut to +cap+ chars, the cut line included: the model knows
128
+ # what it didn't get. A cap smaller than the line keeps +cap+ chars and
129
+ # the line goes past it.
130
+ def head(output, cap)
131
+ keep = cap - head_line(cap, output.length).length
132
+ keep = cap unless keep.positive?
133
+ "#{output[0, keep]}#{head_line(keep, output.length)}"
134
+ end
135
+
136
+ def head_line(kept, total)
137
+ "\n[cut: #{kept} of #{total} chars; read it in parts]"
138
+ end
139
+
140
+ # +output+ with each of +blocks+ that doesn't fit its share of the room
141
+ # cut to its head and tail, a gap line between them (the first cut's
142
+ # line +gap+.line, the next ones' +gap+.short). nil when the cap leaves
143
+ # too little room.
144
+ def head_tail(output, cap:, blocks:, head_chars:, gap:)
145
+ fixed = output.length - blocks.sum(&:length)
146
+ # Room for the gap lines (their longest numbers) and a newline each.
147
+ room = cap - fixed - (gap.line_max.length + 2) - (blocks.size > 1 ? gap.short_max.length + 2 : 0)
148
+ shares = shares(blocks.map(&:length), room)
149
+ return nil unless shares
150
+
151
+ text = +""
152
+ at = 0
153
+ first = true
154
+ blocks.zip(shares).each do |block, share|
155
+ text << output[at...block.from]
156
+ at = block.to
157
+ body = output[block.from, block.length]
158
+ if body.length <= share
159
+ text << body
160
+ next
161
+ end
162
+
163
+ shown = cut_block(body, share, head_chars)
164
+ return nil unless shown
165
+
166
+ head_part, tail_part = shown
167
+ left_out = Block.new(from: block.from + head_part.length, length: body.length - head_part.length - tail_part.length)
168
+ text << head_part
169
+ text << "\n" unless head_part.end_with?("\n") || head_part.empty?
170
+ text << (first ? gap.line(left_out) : gap.short(left_out)) << "\n" << tail_part
171
+ first = false
172
+ end
173
+ text << output[at..]
174
+ text.length <= cap ? text : nil
175
+ end
176
+
177
+ # The room each block gets: stdout STDOUT_SHARE when both need cutting,
178
+ # a block that needs less gives the rest to the other. nil when there
179
+ # isn't room for a useful tail.
180
+ def shares(lengths, room)
181
+ return nil if room < MIN_TAIL
182
+ return [room] if lengths.size == 1
183
+
184
+ first = (room * STDOUT_SHARE).floor
185
+ second = room - first
186
+ if lengths[1] < second
187
+ [room - lengths[1], lengths[1]]
188
+ elsif lengths[0] < first
189
+ [lengths[0], room - lengths[0]]
190
+ else
191
+ [first, second]
192
+ end
193
+ end
194
+
195
+ # +body+'s head and tail within +share+ chars, each snapped to whole
196
+ # lines when that keeps at least half of it. nil when the tail would
197
+ # be shorter than MIN_TAIL.
198
+ def cut_block(body, share, head_chars)
199
+ head_size = [head_chars, share / 4].min
200
+ tail_size = share - head_size
201
+ return nil if tail_size < MIN_TAIL
202
+
203
+ head_part = body[0, head_size]
204
+ newline = head_part.rindex("\n")
205
+ head_part = head_part[0, newline + 1] if newline && newline + 1 >= head_size / 2
206
+ tail_part = body[-tail_size..]
207
+ newline = tail_part.index("\n")
208
+ tail_part = tail_part[(newline + 1)..] if newline && tail_part.length - newline - 1 >= tail_size / 2
209
+ [head_part, tail_part]
210
+ end
211
+
212
+ # execute's stdout and stderr bodies, each cut on its own; any other
213
+ # text (no "stdout:"/"stderr:" block: an error, a hand-over) is one.
214
+ def execute_blocks(output)
215
+ ends = output.match(EXIT_LINE)&.begin(0) || output.length
216
+ stdout_at = output.index(STDOUT_LABEL)
217
+ stderr_at = output.rindex(STDERR_LABEL, ends - STDERR_LABEL.length)
218
+ stderr_at = nil if stderr_at && stdout_at && stderr_at < stdout_at
219
+ blocks = []
220
+ if stdout_at
221
+ from = stdout_at + STDOUT_LABEL.length
222
+ blocks << Block.new(from: from, length: [(stderr_at || ends) - from, 0].max)
223
+ end
224
+ blocks << Block.new(from: stderr_at + STDERR_LABEL.length, length: ends - stderr_at - STDERR_LABEL.length) if stderr_at
225
+ blocks.empty? ? [Block.new(from: 0, length: output.length)] : blocks
226
+ end
227
+
228
+ # The gap line of a :head_tail_ref cut: the file the whole text is in.
229
+ class RefGap
230
+ def initialize(output, ref, what, stderr: nil)
231
+ @output = output
232
+ @ref = ref.to_s
233
+ @what = what
234
+ @stderr = stderr.to_s
235
+ end
236
+
237
+ def line(left_out)
238
+ also = @stderr.empty? ? "" : " (its stderr in #{@stderr})"
239
+ where = @ref.empty? ? "" : "; #{@what} is in #{@ref}#{also}: read it with start_line/end_line or rg it"
240
+ "[cut: #{left_out.length} of #{@output.length} chars not shown#{where}]"
241
+ end
242
+
243
+ def short(left_out) = "[cut: #{left_out.length} chars not shown]"
244
+ def line_max = line(Block.new(from: 0, length: @output.length))
245
+ def short_max = short(Block.new(from: 0, length: @output.length))
246
+ end
247
+
248
+ # The gap lines of a :head_tail_file cut: where the whole output is
249
+ # saved, and which of its lines the cut left out.
250
+ class FileGap
251
+ # @param saved [ToolOutputFiles::Saved, String] the saved file, or
252
+ # why there is none
253
+ def initialize(output, saved)
254
+ @output = output
255
+ @saved = saved.is_a?(String) ? nil : saved
256
+ @reason = saved.is_a?(String) ? saved : nil
257
+ end
258
+
259
+ # The first cut's line.
260
+ def line(left_out)
261
+ return "[cut: #{left_out.length} of #{@output.length} chars not shown; #{unsaved_note}]" unless @saved
262
+
263
+ "[cut: #{lines_text(left_out)} of the saved file not shown (#{left_out.length} of #{@output.length} chars); " \
264
+ "#{saved_note}]"
265
+ end
266
+
267
+ # A further cut's line, the file named by the first.
268
+ def short(left_out)
269
+ return "[cut: #{left_out.length} chars not shown]" unless @saved
270
+
271
+ "[cut: #{lines_text(left_out)} of the saved file not shown (#{left_out.length} chars)]"
272
+ end
273
+
274
+ # The longest line and short line it can write (budgeting).
275
+ def line_max = line(widest)
276
+ def short_max = short(widest)
277
+
278
+ private
279
+
280
+ def widest = Block.new(from: 0, length: @output.length)
281
+
282
+ def lines_text(left_out)
283
+ return "lines #{lines_total}-#{lines_total}" if left_out.from.zero? && left_out.length == @output.length
284
+
285
+ first = @output[0, left_out.from].count("\n") + 1
286
+ last = @output[0, left_out.to].count("\n")
287
+ last += 1 unless left_out.to.zero? || @output[left_out.to - 1] == "\n"
288
+ "lines #{first}-#{[last, first].max}"
289
+ end
290
+
291
+ def lines_total = @saved.lines
292
+
293
+ def where = "#{@saved.path} (#{@saved.bytes} bytes, #{@saved.lines} lines)"
294
+
295
+ def saved_note
296
+ if @output.include?(PREVIEW_MARK)
297
+ "this preview is saved in #{where}; the full output wasn't kept: read the preview with " \
298
+ "start_line/end_line or rg it"
299
+ elsif ToolOutputCut.unfinished?(@output)
300
+ "the output it printed is saved in #{where}: read it with start_line/end_line or rg it"
301
+ else
302
+ "the whole output is saved in #{where}: read it with start_line/end_line or rg it; don't rerun the command"
303
+ end
304
+ end
305
+
306
+ def unsaved_note
307
+ "the whole output wasn't saved (#{@reason}); to look past this, rerun with the output sent to a file " \
308
+ "(cmd > /tmp/out.txt 2>&1) and read that"
309
+ end
310
+ end
311
+ end
312
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "fileutils"
5
+ require_relative "atomic_file"
6
+
7
+ module Samagotchi
8
+ # The whole output of a tool call ToolRunner cut (ToolOutputCut's
9
+ # :head_tail_file), saved in the session's folder, tool_output/
10
+ # (sessions/<id>/tool_output/; not output/, the session's reply outbox),
11
+ # so the model reads or rg's it instead of running the command again.
12
+ #
13
+ # The name comes from the content (<tool>-<sha256[0,12]>.txt): the same
14
+ # output gives the same path, so the cut text is the same each time
15
+ # (loop-guard's repeat check, the prompt cache). The files go with the
16
+ # session folder; a session keeps its newest MAX_FILES.
17
+ module ToolOutputFiles
18
+ DIR = "tool_output"
19
+ MAX_FILES = 50
20
+
21
+ # Where a saved output is, and its size.
22
+ Saved = Data.define(:path, :bytes, :lines)
23
+
24
+ module_function
25
+
26
+ # @param session_dir [String] the session's folder (Session.session_dir)
27
+ # @param text [String] the output, saved byte for byte
28
+ # @param tool [String] the tool's name, the file name's start
29
+ # @return [Saved]
30
+ # @raise [SystemCallError] when it can't be written
31
+ def save(session_dir, text, tool:)
32
+ path = planned(session_dir, text, tool: tool).path
33
+ dir = File.dirname(path)
34
+ FileUtils.mkdir_p(dir)
35
+ # Already there (the same output again): newest now, not rewritten.
36
+ # An empty one (cut short) is written again.
37
+ if File.size?(path)
38
+ FileUtils.touch(path)
39
+ else
40
+ AtomicFile.write(path, text)
41
+ end
42
+ prune(dir)
43
+ planned(session_dir, text, tool: tool)
44
+ end
45
+
46
+ # Where #save would put +text+, and its size, with nothing written: the
47
+ # cut checks its gap line fits before anything is saved.
48
+ # @return [Saved]
49
+ def planned(session_dir, text, tool:)
50
+ path = File.join(session_dir, DIR, "#{file_tool(tool)}-#{Digest::SHA256.hexdigest(text)[0, 12]}.txt")
51
+ Saved.new(path: path, bytes: text.bytesize, lines: line_count(text))
52
+ end
53
+
54
+ # The lines a text has, a last line with no newline counted.
55
+ def line_count(text)
56
+ text.count("\n") + (text.empty? || text.end_with?("\n") ? 0 : 1)
57
+ end
58
+
59
+ # A tool name safe in a file name (a plugin's or an MCP tool's).
60
+ def file_tool(tool)
61
+ name = tool.to_s.downcase.gsub(/[^a-z0-9_-]/, "_")
62
+ name.empty? ? "tool" : name
63
+ end
64
+
65
+ # Keeps the newest MAX_FILES files of +dir+, by mtime.
66
+ def prune(dir)
67
+ files = Dir.glob(File.join(dir, "*.txt"))
68
+ return if files.size <= MAX_FILES
69
+
70
+ files.sort_by { |f| [-File.mtime(f).to_f, f] }.drop(MAX_FILES).each { |f| FileUtils.rm_f(f) }
71
+ end
72
+ end
73
+ end
@@ -7,6 +7,9 @@ require_relative "guardrails"
7
7
  require_relative "vision_context"
8
8
  require_relative "log"
9
9
  require_relative "edit_preview"
10
+ require_relative "tool_output_cut"
11
+ require_relative "tool_output_files"
12
+ require_relative "tools/output_guardrails"
10
13
  require_relative "tools/tool_path"
11
14
  require_relative "memory_bundle/index_sync"
12
15
  require_relative "memory_bundle/index_size"
@@ -16,8 +19,8 @@ module Samagotchi
16
19
  # tool_call_completed events, the guardrail gate (before_tool_call hooks
17
20
  # and their veto), dispatch through KernelLoop, the after_tool_call hook
18
21
  # and the output cap. Both loops feed the model `capped_output:` (cut to
19
- # the cap with a "[cut: N of M chars …]" line), the text the event and
20
- # the hook get too.
22
+ # the cap by the tool's ToolOutputCut policy, with a "[cut: …]" line),
23
+ # the text the event and the hook get too.
21
24
  class ToolRunner
22
25
  # More images in one result are left out, each with a line (a tool
23
26
  # that returns many screenshots can't flood the context).
@@ -38,6 +41,7 @@ module Samagotchi
38
41
 
39
42
  # @param call_index [Integer] 1-based position of the call in its batch
40
43
  # @return [Hash] output:, capped_output:, truncated:, activity:,
44
+ # output_file: (the saved whole output of a cut execute result),
41
45
  # images: (refs) when the tool returned images the model gets to see, and
42
46
  # shown_params: the params line the live row showed, and shown_label:
43
47
  # its label ("chrome: screenshot"), only for a tool that isn't built in
@@ -109,7 +113,8 @@ module Samagotchi
109
113
  output = "#{changed}\n#{output}"
110
114
  end
111
115
  truncated = !max_tool_output_chars.nil? && output.length > max_tool_output_chars
112
- capped = truncated ? cut(output, max_tool_output_chars) : output
116
+ capped, saved = truncated ? cut(call, output, max_tool_output_chars, result) : output
117
+ output_file = saved.path if saved.is_a?(ToolOutputFiles::Saved)
113
118
 
114
119
  # The call's outcome as its activity line has it (worked out from the
115
120
  # full output: ok, error, blocked, stopped); a dispatcher that raised
@@ -120,6 +125,7 @@ module Samagotchi
120
125
  completed[:images] = images if images&.any?
121
126
  completed[:diff] = diff if diff
122
127
  completed[:waited_ms] = waited_ms if waited_ms
128
+ completed[:output_file] = output_file if output_file
123
129
  # Also here: a UI that missed the start (a replay gap) builds its row
124
130
  # from this event (its title is in the activity).
125
131
  completed.merge!(fields.slice(*ToolRowFields::EVENT_KEYS))
@@ -135,22 +141,50 @@ module Samagotchi
135
141
  run[:shown_label] = label if label
136
142
  run[:refs] = refs unless refs.empty?
137
143
  run[:diff] = diff if diff
144
+ run[:output_file] = output_file if output_file
138
145
  run
139
146
  end
140
147
 
141
148
  private
142
149
 
143
- # +output+ cut to +cap+ chars, the cut line included: the model knows
144
- # what it didn't get. A cap smaller than the line keeps +cap+ chars and
145
- # the line goes past it.
146
- def cut(output, cap)
147
- keep = cap - cut_line(cap, output.length).length
148
- keep = cap unless keep.positive?
149
- "#{output[0, keep]}#{cut_line(keep, output.length)}"
150
+ # +output+ cut to +cap+ by its tool's policy (ToolOutputCut); an
151
+ # execute result keeps its tail too, its whole output saved in the
152
+ # session's folder.
153
+ # @param result [Hash] the dispatch's: its ref_path is the file the
154
+ # whole text is in (a task's log, a child's reply: Tools::RefText),
155
+ # for task_wait and delegate; ref_stderr_path, a task's stderr log
156
+ # @return [Array(String, Object)] the text, and the saved file (or why
157
+ # there is none) when it was cut that way
158
+ def cut(call, output, cap, result)
159
+ ToolOutputCut.apply(output, cap: cap, tool: call[:name], save: ->(text, dry: false) { save_output(call, text, dry: dry) },
160
+ ref: result[:ref_path], ref_stderr: result[:ref_stderr_path],
161
+ head_chars: Tools::OutputGuardrails.config_positive_int("execute.cut_head_chars",
162
+ ToolOutputCut::HEAD_CHARS))
150
163
  end
151
164
 
152
- def cut_line(kept, total)
153
- "\n[cut: #{kept} of #{total} chars; read it in parts]"
165
+ # The whole output in the session's tool_output folder
166
+ # (ToolOutputFiles), or why it isn't saved: no session (a bare kernel)
167
+ # or the error's class. Never fails the call. +dry+: where it would go,
168
+ # nothing written (ToolOutputFiles.planned).
169
+ # @return [ToolOutputFiles::Saved, String]
170
+ def save_output(call, text, dry: false)
171
+ dir = session_dir
172
+ return "no session" if dir.to_s.empty?
173
+ return ToolOutputFiles.planned(dir, text, tool: call[:name]) if dry
174
+
175
+ ToolOutputFiles.save(dir, text, tool: call[:name])
176
+ rescue SystemCallError => e
177
+ Log.warn(:turn, "tool_output_save_failed", tool: call[:name], error: e.class.name)
178
+ e.class.name
179
+ end
180
+
181
+ # The session's folder (KernelLoop#session_dir), nil for a bare kernel
182
+ # or a lookup that failed.
183
+ def session_dir
184
+ @kernel.respond_to?(:session_dir) ? @kernel.session_dir&.call : nil
185
+ rescue StandardError => e
186
+ Log.warn(:turn, "tool_output_save_failed", error: e.class.name)
187
+ nil
154
188
  end
155
189
 
156
190
  # edit/write only: the file just before the call runs. The row diffs it
@@ -66,7 +66,10 @@ module Samagotchi
66
66
  return child_id if child_id.start_with?("Error:")
67
67
 
68
68
  note = warning ? "Warning: #{warning}\n" : ""
69
- return note + DelegateWait.call(child_id, peers: peers, timeout: timeout) if wait
69
+ if wait
70
+ result = DelegateWait.call(child_id, peers: peers, timeout: timeout)
71
+ return RefText.wrap(note + result, result.respond_to?(:ref_path) ? result.ref_path : nil)
72
+ end
70
73
 
71
74
  started = session.to_s.strip.empty? ? "Started a delegate session" : "Sent the follow-up to delegate #{child_id[0, 8]}"
72
75
  "#{note}session: #{child_id}\nstatus: running\n#{started}; #{running_hint(peers)} " \
@@ -7,6 +7,7 @@ require_relative "output_guardrails"
7
7
  require_relative "peers"
8
8
  require_relative "delegate_relay"
9
9
  require_relative "delegate_cursor"
10
+ require_relative "ref_text"
10
11
 
11
12
  module Samagotchi
12
13
  # Loaded on first use: child_reports requires this file (a circular
@@ -78,7 +79,8 @@ module Samagotchi
78
79
  unless wait.status == :waiting_for_answer && DelegateRelay.relayable?(relay, wait.question)
79
80
  outcomes.concat(relayed_outcomes.delete(key) || [])
80
81
  advance(peers.session_id, child_id, wait, state_dir: sd)
81
- return with_outcomes(finish(wait, child_id, timeout: timeout, reports: reports), outcomes)
82
+ text = with_outcomes(finish(wait, child_id, timeout: timeout, reports: reports), outcomes)
83
+ return RefText.wrap(text, reply_path(child_id, wait, state_dir: sd))
82
84
  end
83
85
 
84
86
  outcome = DelegateRelay.call(child_id, wait.question, relay: relay, state_dir: sd, more: others&.count.to_i)
@@ -104,6 +106,15 @@ module Samagotchi
104
106
  })
105
107
  end
106
108
 
109
+ # The child's reply file a :done wait read (its output/ file), which
110
+ # ToolRunner's cut of a long reply names (ToolOutputCut's
111
+ # :head_tail_ref); nil for any other outcome.
112
+ def reply_path(child_id, wait, state_dir:)
113
+ return nil unless wait.status == :done && wait.file
114
+
115
+ File.join(ReplyWait.reply_dir(child_id, state_dir: state_dir), wait.file)
116
+ end
117
+
107
118
  # The relayed approvals' outcome lines, after the status line.
108
119
  def with_outcomes(text, outcomes)
109
120
  return text if outcomes.empty?
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Samagotchi
4
+ module Tools
5
+ # A tool's text that shows part of a file kept elsewhere in whole: a
6
+ # task's log (task_wait's output tail), a child's reply file (delegate,
7
+ # delegate_result). It is the text itself (a String), like
8
+ # Plugin::ToolResult; #ref_path is the file, which ToolRunner's cut
9
+ # names when the text is longer than max_tool_output_chars
10
+ # (ToolOutputCut's :head_tail_ref), instead of saving a copy;
11
+ # #stderr_path, a handed-over execute's stderr log, the cut names too.
12
+ class RefText < String
13
+ attr_reader :ref_path, :stderr_path
14
+
15
+ def initialize(text, ref_path, stderr_path: nil)
16
+ @ref_path = ref_path
17
+ @stderr_path = stderr_path.to_s.empty? ? nil : stderr_path
18
+ super(text.to_s)
19
+ end
20
+
21
+ # +text+ as a RefText of +ref_path+; +text+ as it is with no path.
22
+ def self.wrap(text, ref_path, stderr_path: nil)
23
+ ref_path.to_s.empty? ? text : new(text, ref_path, stderr_path: stderr_path)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "task_runtime"
4
+ require_relative "ref_text"
4
5
 
5
6
  module Samagotchi
6
7
  module Tools
@@ -121,7 +122,9 @@ module Samagotchi
121
122
  lines << output_tail.rstrip unless output_tail.empty?
122
123
  stderr_tail = stderr_tail(record, tail_lines)
123
124
  lines.concat(["stderr_tail:", stderr_tail.rstrip]) unless stderr_tail.empty?
124
- lines.join("\n")
125
+ # The whole log is at output_path (stderr at stderr_path): a cut of
126
+ # this text names them.
127
+ RefText.wrap(lines.join("\n"), record.output_path, stderr_path: record.stderr_path)
125
128
  end
126
129
  private_class_method :format_response
127
130
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- VERSION = "0.69.0"
4
+ VERSION = "0.70.0"
5
5
  # Sent as the User-Agent of every HTTP request chi makes.
6
6
  USER_AGENT = "chi/#{VERSION}"
7
7
  end
@@ -10,6 +10,7 @@ require_relative "../image_store"
10
10
  require_relative "../llm_context_edit"
11
11
  require_relative "../token_usage"
12
12
  require_relative "../row_refs"
13
+ require_relative "../tool_output_cut"
13
14
 
14
15
  module Samagotchi
15
16
  module Web
@@ -50,7 +51,8 @@ module Samagotchi
50
51
  # so an output that holds the joiner itself stays whole.
51
52
  JOINER_BEFORE_TAG = /\n\n---\n\n(?=\[)/
52
53
  # The row shows 300 chars and the rest on hover; a read of a big file
53
- # would otherwise ride along with every reload.
54
+ # would otherwise ride along with every reload. A longer one keeps its
55
+ # head and tail (ToolOutputCut.preview).
54
56
  OUTPUT_MAX = 2000
55
57
 
56
58
  QWEN_THINK = %r{<think>(.*?)</think>}m
@@ -246,7 +248,7 @@ module Samagotchi
246
248
  part.merge!(ToolRowFields.for(name, call, cwd: cwd))
247
249
  part[:label] = label if label.is_a?(String) && !label.empty?
248
250
  unless output.nil?
249
- part[:output] = output.length > OUTPUT_MAX ? output[0, OUTPUT_MAX] : output
251
+ part[:output] = ToolOutputCut.preview(output, cap: OUTPUT_MAX, tool: name)
250
252
  part[:output_truncated] = true if output.length > OUTPUT_MAX
251
253
  end
252
254
  shots = Array(images).select { |image| image.is_a?(Hash) }
@@ -496,13 +496,13 @@ module Samagotchi
496
496
  # turn from context:<name> whose first message is the wake note already
497
497
  # in the conversation (named by +turn_id+), on the same rules as a
498
498
  # delegate report's wake turn: it counts toward session.max_wakes (a
499
- # peer's answer toward context.peer_turns too), and a failure goes back
499
+ # peer's answer toward context.peer_turns too, unless it agrees), and a failure goes back
500
500
  # to before the turn (the note stays, as a plain one:
501
501
  # WorkerInbound#unmark_wake) and pauses wakes until a human's input.
502
502
  # +context_wake+: a WorkerInbound::ContextWake.
503
503
  def run_context_wake_turn(context_wake)
504
504
  name = context_wake.name
505
- Log.info(:worker, "context_wake_turn", name: name, in_a_row: @wakes.count!(peer: context_wake.peer?))
505
+ Log.info(:worker, "context_wake_turn", name: name, in_a_row: @wakes.count!(peer: context_wake.peer_turn?))
506
506
  @turn_flow.before_prompt_turn
507
507
  run_engine_turn(nil, continue: true, origin: context_wake.origin,
508
508
  id: context_wake.turn_id, max_iterations: IterationLimit.for) do |result, error|
@@ -47,6 +47,9 @@ module Samagotchi
47
47
  def name = delivery.name
48
48
  # For a peer agent's answer (its own budget, WorkerWakes#peer_open?).
49
49
  def peer? = !delivery.peer.nil?
50
+ # Whether the wake counts toward context.peer_turns: a peer's answer
51
+ # that agrees closes the round and counts toward session.max_wakes only.
52
+ def peer_turn? = peer? && !delivery.agreed
50
53
  def origin = { client_id: "#{ClientId::CONTEXT_PREFIX}#{name}" }
51
54
  end
52
55