samagotchi 0.24.0 → 0.26.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 (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -1
  3. data/docs/architecture.md +16 -9
  4. data/docs/cli.md +4 -3
  5. data/docs/configuration.md +22 -13
  6. data/docs/hooks.md +1 -1
  7. data/docs/internals/background-tasks.md +1 -0
  8. data/docs/internals/engine.md +478 -0
  9. data/docs/internals/prompt-caching.md +120 -0
  10. data/docs/plugins.md +18 -3
  11. data/docs/testing.md +12 -0
  12. data/lib/samagotchi/bundles/loop-guard/manifest.yml +2 -2
  13. data/lib/samagotchi/bundles/loop-guard/plugin.rb +59 -11
  14. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  15. data/lib/samagotchi/cancellation_controller.rb +8 -0
  16. data/lib/samagotchi/client.rb +53 -3
  17. data/lib/samagotchi/command_steps.rb +233 -0
  18. data/lib/samagotchi/config.rb +14 -4
  19. data/lib/samagotchi/empty_answer_retry.rb +2 -2
  20. data/lib/samagotchi/engine.rb +73 -11
  21. data/lib/samagotchi/guardrails/shell_lex.rb +49 -5
  22. data/lib/samagotchi/idle_client.rb +14 -2
  23. data/lib/samagotchi/kernel_loop.rb +60 -6
  24. data/lib/samagotchi/llm/chat_loop.rb +18 -4
  25. data/lib/samagotchi/llm/errors.rb +26 -1
  26. data/lib/samagotchi/llm/http.rb +5 -4
  27. data/lib/samagotchi/llm/openai_chat.rb +5 -4
  28. data/lib/samagotchi/llm/prompt_cache.rb +34 -9
  29. data/lib/samagotchi/llm/usage.rb +18 -3
  30. data/lib/samagotchi/log_subscriber.rb +2 -1
  31. data/lib/samagotchi/plugin/api.rb +3 -2
  32. data/lib/samagotchi/prompt_warmup.rb +95 -0
  33. data/lib/samagotchi/session_metrics.rb +38 -4
  34. data/lib/samagotchi/system_prompt.rb +30 -9
  35. data/lib/samagotchi/terminal_ui/attached_loop.rb +5 -2
  36. data/lib/samagotchi/terminal_ui/attached_view.rb +6 -2
  37. data/lib/samagotchi/terminal_ui/event_renderer.rb +14 -2
  38. data/lib/samagotchi/terminal_ui/formatting.rb +31 -9
  39. data/lib/samagotchi/terminal_ui.rb +2 -0
  40. data/lib/samagotchi/thinking_tails.rb +104 -0
  41. data/lib/samagotchi/token_usage.rb +18 -6
  42. data/lib/samagotchi/tool_activity.rb +37 -3
  43. data/lib/samagotchi/tool_declarations.rb +14 -0
  44. data/lib/samagotchi/tool_view.rb +40 -4
  45. data/lib/samagotchi/tools/builtins.rb +4 -1
  46. data/lib/samagotchi/tools/execute.rb +2 -1
  47. data/lib/samagotchi/tools/registry.rb +11 -4
  48. data/lib/samagotchi/tools/shell.rb +32 -0
  49. data/lib/samagotchi/tools/task_runtime.rb +3 -3
  50. data/lib/samagotchi/turn_note.rb +26 -2
  51. data/lib/samagotchi/turn_state.rb +6 -0
  52. data/lib/samagotchi/turn_tally.rb +17 -6
  53. data/lib/samagotchi/version.rb +1 -1
  54. data/lib/samagotchi/web/public/command_view.js +74 -3
  55. data/lib/samagotchi/web/public/format.js +11 -1
  56. data/lib/samagotchi/web/public/index.html +45 -13
  57. data/lib/samagotchi/web/public/stage_model.js +5 -1
  58. data/lib/samagotchi/web/public/stage_view.js +1 -1
  59. data/lib/samagotchi/web/public/turn_view.js +3 -3
  60. data/lib/samagotchi/worker.rb +3 -0
  61. metadata +7 -1
@@ -240,7 +240,6 @@ module Samagotchi
240
240
  body = {
241
241
  model: model,
242
242
  messages: cache_breakpoints(Array(messages).map { |message| wire_message(message) }, model),
243
- temperature: 0.0,
244
243
  stream: @stream
245
244
  }
246
245
  body[:stream_options] = { include_usage: true } if @stream
@@ -249,7 +248,9 @@ module Samagotchi
249
248
  body[:tools] = tools
250
249
  body[:tool_choice] = "auto"
251
250
  end
252
- # A configured value replaces chi's own (temperature, max_tokens); nil drops it.
251
+ # No temperature unless configured: each model runs at its provider's
252
+ # default (greedy decoding made thinking models loop). A configured
253
+ # value replaces chi's own (max_tokens); nil drops it.
253
254
  body.merge(options || {}).compact
254
255
  end
255
256
 
@@ -258,13 +259,13 @@ module Samagotchi
258
259
  # would pay for a cache write nothing reads, and a local model named
259
260
  # after Claude must not get Array content.
260
261
  def cache_breakpoints(messages, model)
261
- return messages unless @remote && @purpose == "chat"
262
+ return PromptCache.without_split(messages) unless @remote && @purpose == "chat"
262
263
 
263
264
  PromptCache.mark(messages, model: model)
264
265
  end
265
266
 
266
267
  # The body's fields beyond the conversation and the stream, for the
267
- # log line: "temperature=0.6 presence_penalty=1.5".
268
+ # log line: "temperature=0.6 presence_penalty=1.5", nil for none.
268
269
  def sampling_summary(body)
269
270
  SamplingSettings.log_text(body.except(:model, :messages, :stream, :stream_options, :tools, :tool_choice))
270
271
  end
@@ -11,6 +11,12 @@ module Samagotchi
11
11
  # same prefix plus new messages) reads everything up to it.
12
12
  # Mid and tail system messages (reminders, turn notes) never take the
13
13
  # second mark. Other models' messages pass through untouched.
14
+ #
15
+ # A system message whose `cache_split:` says where its stable part ends
16
+ # (ChatLoop, from SystemPrompt#stable_length) goes as two text parts:
17
+ # the stable part with the mark, the per-session tail (model, location,
18
+ # session) without, so a new session reads the stable part from the
19
+ # cache. `cache_split:` never goes on the wire (#without_split).
14
20
  module PromptCache
15
21
  CONTROL = { type: "ephemeral" }.freeze
16
22
 
@@ -21,16 +27,27 @@ module Samagotchi
21
27
 
22
28
  # +messages+ (wire messages: symbol keys, content a String or an Array
23
29
  # of parts) with the breakpoints set when +model+ is a Claude model;
24
- # the given messages as they are otherwise. The marked messages and
25
- # parts are copies: the caller's are never changed.
30
+ # the given messages as they are otherwise (#without_split). The marked
31
+ # messages and parts are copies: the caller's are never changed.
26
32
  # @return [Array<Hash>]
27
33
  def mark(messages, model:)
28
- return messages unless claude?(model)
34
+ return without_split(messages) unless claude?(model)
29
35
 
30
36
  indexes = [system_index(messages), last_index(messages)].compact.uniq
31
- return messages if indexes.empty?
37
+ messages.each_with_index.map do |message, index|
38
+ next marked(message.except(:cache_split), split: message[:cache_split]) if indexes.include?(index)
32
39
 
33
- messages.each_with_index.map { |message, index| indexes.include?(index) ? marked(message) : message }
40
+ message.key?(:cache_split) ? message.except(:cache_split) : message
41
+ end
42
+ end
43
+
44
+ # +messages+ without the `cache_split:` hints, for a request that sets
45
+ # no breakpoints.
46
+ # @return [Array<Hash>]
47
+ def without_split(messages)
48
+ return messages unless messages.any? { |message| message.key?(:cache_split) }
49
+
50
+ messages.map { |message| message.except(:cache_split) }
34
51
  end
35
52
 
36
53
  # Whether any message in +messages+ carries a breakpoint.
@@ -60,11 +77,19 @@ module Samagotchi
60
77
  end
61
78
  end
62
79
 
63
- # A String becomes one text part; an Array gets the mark on its last
64
- # text part (the last part when none is text).
65
- def marked(message)
80
+ # A String becomes one text part (two at +split+, the mark on the
81
+ # first); an Array gets the mark on its last text part (the last part
82
+ # when none is text).
83
+ def marked(message, split: nil)
66
84
  content = message[:content]
67
- return message.merge(content: [{ type: "text", text: content, cache_control: CONTROL }]) if content.is_a?(String)
85
+ if content.is_a?(String)
86
+ if split.is_a?(Integer) && split.positive? && split < content.length
87
+ return message.merge(content: [{ type: "text", text: content[0, split], cache_control: CONTROL },
88
+ { type: "text", text: content[split..] }])
89
+ end
90
+
91
+ return message.merge(content: [{ type: "text", text: content, cache_control: CONTROL }])
92
+ end
68
93
 
69
94
  target = content.rindex { |part| part.is_a?(Hash) && (part[:type] || part["type"]).to_s == "text" } ||
70
95
  content.rindex { |part| part.is_a?(Hash) }
@@ -5,20 +5,35 @@ require_relative "../token_usage"
5
5
  module Samagotchi
6
6
  module LLM
7
7
  # A response's token counts, never nil: what the server reported
8
- # (:server), else zeros (:none).
9
- Usage = Data.define(:prompt_tokens, :completion_tokens, :source) do
8
+ # (:server), else zeros (:none). +cached_tokens+ is the prompt the server
9
+ # reused from its cache, +cache_write_tokens+ what it wrote to it
10
+ # (OpenRouter reports those for Anthropic models); 0 when not reported.
11
+ Usage = Data.define(:prompt_tokens, :completion_tokens, :source, :cached_tokens, :cache_write_tokens) do
10
12
  # @return [Usage, nil] nil when the payload carries no counts
11
13
  def self.from_payload(payload)
12
14
  counts = TokenUsage.from_payload(payload)
13
15
  return nil unless counts
14
16
 
15
17
  new(prompt_tokens: counts.prompt_tokens.to_i, completion_tokens: counts.completion_tokens.to_i,
16
- source: :server)
18
+ source: :server, cached_tokens: counts.cached_tokens.to_i,
19
+ cache_write_tokens: counts.cache_write_tokens.to_i)
17
20
  end
18
21
 
19
22
  def self.none = new(prompt_tokens: 0, completion_tokens: 0, source: :none)
20
23
 
24
+ def initialize(prompt_tokens:, completion_tokens:, source:, cached_tokens: 0, cache_write_tokens: 0)
25
+ super
26
+ end
27
+
21
28
  def total_tokens = prompt_tokens + completion_tokens
29
+
30
+ # The request's prompt-cache counts for a :generation_completed event
31
+ # and the log ({} when the server reported none).
32
+ def cache_fields
33
+ return {} unless source == :server
34
+
35
+ TokenUsage.cache_fields(prompt: prompt_tokens, cached: cached_tokens, cache_write: cache_write_tokens)
36
+ end
22
37
  end
23
38
  end
24
39
  end
@@ -106,7 +106,8 @@ module Samagotchi
106
106
  requested_model: event[:requested_model],
107
107
  content_length: event[:content_length],
108
108
  thinking_chars: event[:thinking_chars], finish_reason: event[:finish_reason],
109
- stopped_by: event[:stopped_by])
109
+ stopped_by: event[:stopped_by], prompt: event[:prompt_tokens],
110
+ cached: event[:cached_tokens], cache_write: event[:cache_write_tokens])
110
111
  return unless event[:stopped_by]
111
112
 
112
113
  # A plugin (stop_generation) or a steer (Engine#cut_for_steer, by
@@ -264,8 +264,9 @@ module Samagotchi
264
264
 
265
265
  # Make +bundle+'s tools in +registry+ the +specs+: its tools not in
266
266
  # them go; a new one, or one whose schema or label changed, is
267
- # registered (a changed one again, at the end); an unchanged one is
268
- # kept as it is. A name another source has is left out. Called on
267
+ # registered (a changed one again, in its place: Tools::Registry
268
+ # orders bundle tools by bundle and name); an unchanged one is kept
269
+ # as it is. A name another source has is left out. Called on
269
270
  # the turn thread (Engine#apply_staged_tools!).
270
271
  # @return [Hash] {changed: Boolean, skipped: [String] (why)}
271
272
  def self.apply_tools(registry, bundle, specs, context)
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "config"
4
+ require_relative "log"
5
+
6
+ module Samagotchi
7
+ # The turn-end warm-up (cache.warmup). After a turn, the next turn's prompt
8
+ # differs from the last request at its tail: the previous answer's
9
+ # thinking is stripped (Gemma's and Qwen's specs), so a hybrid model on
10
+ # llama.cpp prefills from a restore point well before the end, longer
11
+ # with every turn. The warm-up sends that prompt, up to where the new user
12
+ # turn starts, while the user reads, so the next turn prefills only its
13
+ # own message (measured on Ornith: 0.73 s -> 0.11 s; 417 -> 26 tokens).
14
+ #
15
+ # The slot rule (R4 spike, llama.cpp with its host-memory prompt cache):
16
+ # - the warm-up runs on the slot the turn's last request used, which still
17
+ # holds its state (Client#warm_up, `id_slot`);
18
+ # - the next request is pinned to that slot only while the warm-up still
19
+ # runs, so it queues behind it (0.6 s) instead of starting cold on
20
+ # another slot (12 s);
21
+ # - once the warm-up is done, nothing is pinned: the server finds the
22
+ # state itself, and pinning to a slot it has since cleared skips its
23
+ # prompt cache (12 s again). #take_pin forgets the slot either way.
24
+ class PromptWarmup
25
+ MODES = %w[auto off].freeze
26
+
27
+ # Whether cache.warmup allows a warm-up (auto, the default; off, false,
28
+ # no or 0 turn it off; anything else warns once and is auto).
29
+ def self.enabled?
30
+ value = Config.get("cache.warmup").to_s.strip.downcase
31
+ return false if %w[off false no 0].include?(value)
32
+
33
+ unless value.empty? || value == "auto" || @warned
34
+ @warned = true
35
+ Log.warn(:config, "invalid_value", echo: "Warning: invalid value for cache.warmup: #{value.inspect} (allowed: auto, off) — using auto",
36
+ key: "cache.warmup")
37
+ end
38
+ true
39
+ rescue StandardError
40
+ true
41
+ end
42
+
43
+ def initialize
44
+ @mutex = Mutex.new
45
+ # The pin (#take_pin forgets it) and the last warm-up's thread.
46
+ @state = nil
47
+ @thread = nil
48
+ end
49
+
50
+ # Start a warm-up of +prompt+ on +client+ in its own thread.
51
+ # @param slot [Integer, nil] the last request's slot (nil: unpinned, and
52
+ # nothing to pin the next request to)
53
+ # @return [Thread]
54
+ def start(client:, prompt:, model:, slot:, images: [])
55
+ thread = Thread.new do
56
+ Thread.current.report_on_exception = false
57
+ result = client.warm_up(prompt, model: model, slot: slot, images: images)
58
+ Log.info(:model, "warmup", slot: result&.slot, cached: result&.cache_n, prefilled: result&.prompt_n,
59
+ prefill_ms: result&.prompt_ms, chars: prompt.length)
60
+ result
61
+ rescue StandardError => e
62
+ Log.warn(:model, "warmup_failed", error: e.class.name, msg: e.message.to_s[0, 200])
63
+ nil
64
+ end
65
+ @mutex.synchronize do
66
+ @thread = thread
67
+ @state = { thread: thread, client: client, slot: slot }
68
+ end
69
+ thread
70
+ end
71
+
72
+ # The slot the next request on +client+ must run on: the warm-up's,
73
+ # while it still runs there; nil otherwise (done, failed, unpinned, or
74
+ # another host). Called once per request; the first call forgets the
75
+ # warm-up, so no later request is ever pinned.
76
+ def take_pin(client)
77
+ state = @mutex.synchronize { @state.tap { @state = nil } }
78
+ return nil unless state && state[:slot].is_a?(Integer) && state[:client].equal?(client)
79
+
80
+ state[:thread].alive? ? state[:slot] : nil
81
+ end
82
+
83
+ # Whether a warm-up is running (specs, measurements).
84
+ def running?
85
+ thread = @mutex.synchronize { @thread }
86
+ thread&.alive? || false
87
+ end
88
+
89
+ # Wait up to +timeout+ seconds for the running warm-up (specs).
90
+ def wait(timeout = nil)
91
+ thread = @mutex.synchronize { @thread }
92
+ thread&.join(timeout)
93
+ end
94
+ end
95
+ end
@@ -16,7 +16,7 @@ module Samagotchi
16
16
  # paths and SessionManager background workers.
17
17
  #
18
18
  # Each finished turn leaves a record (status, timings, model, prompt and
19
- # completion tokens, tool calls, iterations, retries); session totals are
19
+ # completion tokens, tool calls, iterations, retries, cuts); session totals are
20
20
  # sums over the records, kept as running counters as records are added, so
21
21
  # a worker that stops and wakes again (a new collector) keeps counting:
22
22
  # #session_id= loads the records already saved.
@@ -78,10 +78,11 @@ module Samagotchi
78
78
  :gen_prompt_max,
79
79
  :gen_open,
80
80
  # The rest of this generation's server report: the largest cached and
81
- # reasoning counts, the last cost, and the server's speeds and decode
81
+ # reasoning counts (and cache writes), the last cost, and the server's speeds and decode
82
82
  # time (llama.cpp's timings); and when its first content, reasoning or
83
83
  # tool call chunk came (an estimated speed runs from there).
84
84
  :gen_cached_max,
85
+ :gen_cache_write_max,
85
86
  :gen_reasoning_max,
86
87
  :gen_cost,
87
88
  :gen_decode_tps,
@@ -102,6 +103,7 @@ module Samagotchi
102
103
  # tokens of the generations with a speed. The last speeds and where the
103
104
  # decode one came from (server / estimate).
104
105
  :cached_sum,
106
+ :cache_write_sum,
105
107
  :reasoning_sum,
106
108
  :cost_sum,
107
109
  :decode_ms_sum,
@@ -110,6 +112,11 @@ module Samagotchi
110
112
  :last_prefill_tps,
111
113
  :tps_source,
112
114
  :retries,
115
+ # Generations a plugin cut (stop_generation: loop-guard's thinking
116
+ # watch), not a steer's cut; and ones that ended at the output cap
117
+ # (finish_reason length). `retries` counts the network's only.
118
+ :cuts,
119
+ :capped,
113
120
  :model,
114
121
  :id,
115
122
  :started_at,
@@ -265,6 +272,7 @@ module Samagotchi
265
272
  end
266
273
  when :generation_completed, :generation_cancelled
267
274
  record_served_model(event)
275
+ count_cut(event)
268
276
  record_generation_completed
269
277
  when :generation_retrying
270
278
  # The retry streams from the start: its counts replace the ones so far.
@@ -364,6 +372,8 @@ module Samagotchi
364
372
  iterations_total: totals[:iterations] + (turn&.iteration_count || 0),
365
373
  gen_latency_ms: (totals[:gen_ms] + (turn&.gen_latency_accum || 0)).round,
366
374
  retries: totals[:retries] + (turn&.retries || 0),
375
+ cuts: totals[:cuts] + (turn&.cuts || 0),
376
+ capped: totals[:capped] + (turn&.capped || 0),
367
377
  started_at: @started_at,
368
378
  last_activity_at: @last_activity_at,
369
379
  # From the session's first start (an earlier process's too), to now.
@@ -386,6 +396,7 @@ module Samagotchi
386
396
  prompt_sum: add.call(:prompt_sum),
387
397
  completion_sum: add.call(:completion_sum),
388
398
  cached_sum: add.call(:cached_sum),
399
+ cache_write_sum: add.call(:cache_write_sum),
389
400
  reasoning_sum: add.call(:reasoning_sum),
390
401
  cost_sum: add.call(:cost_sum),
391
402
  decode_ms_sum: decode_ms.round,
@@ -435,6 +446,7 @@ module Samagotchi
435
446
  gen_prompt_max: 0,
436
447
  gen_open: false,
437
448
  gen_cached_max: 0,
449
+ gen_cache_write_max: 0,
438
450
  gen_reasoning_max: 0,
439
451
  generations: 0,
440
452
  prompt_last: nil,
@@ -443,11 +455,14 @@ module Samagotchi
443
455
  completion_sum: 0,
444
456
  token_sources: [],
445
457
  cached_sum: 0,
458
+ cache_write_sum: 0,
446
459
  reasoning_sum: 0,
447
460
  cost_sum: nil,
448
461
  decode_ms_sum: 0,
449
462
  decode_tokens_sum: 0,
450
463
  retries: 0,
464
+ cuts: 0,
465
+ capped: 0,
451
466
  model: nil,
452
467
  id: event[:turn_id] || SecureRandom.uuid,
453
468
  started_at: now.iso8601(3),
@@ -500,6 +515,15 @@ module Samagotchi
500
515
  end
501
516
  end
502
517
 
518
+ def count_cut(event)
519
+ @mutex.synchronize do
520
+ next unless @turn
521
+
522
+ @turn.cuts += 1 if event[:stopped_by] && event[:stopped_by].to_s != "steer"
523
+ @turn.capped += 1 if event[:finish_reason].to_s == "length"
524
+ end
525
+ end
526
+
503
527
  def record_generation_completed
504
528
  @mutex.synchronize { close_generation if @turn&.gen_open }
505
529
  end
@@ -544,6 +568,7 @@ module Samagotchi
544
568
  # here: it has no speed. Caller holds the mutex.
545
569
  def close_server_report(turn, completion)
546
570
  turn.cached_sum += turn.gen_cached_max
571
+ turn.cache_write_sum += turn.gen_cache_write_max
547
572
  turn.reasoning_sum += turn.gen_reasoning_max
548
573
  turn.cost_sum = turn.cost_sum.to_f + turn.gen_cost if turn.gen_cost
549
574
  turn.last_prefill_tps = turn.gen_prefill_tps.round(1) if turn.gen_prefill_tps
@@ -582,6 +607,7 @@ module Samagotchi
582
607
  @turn.gen_had_server = false
583
608
  @turn.gen_estimate_sum = 0
584
609
  @turn.gen_cached_max = 0
610
+ @turn.gen_cache_write_max = 0
585
611
  @turn.gen_reasoning_max = 0
586
612
  @turn.gen_cost = nil
587
613
  @turn.gen_decode_tps = @turn.gen_prefill_tps = @turn.gen_decode_ms = nil
@@ -605,7 +631,9 @@ module Samagotchi
605
631
  tool_calls: turn.tool_calls,
606
632
  tool_errors: turn.tool_errors,
607
633
  iterations: turn.iteration_count,
608
- retries: turn.retries
634
+ retries: turn.retries,
635
+ cuts: turn.cuts,
636
+ capped: turn.capped
609
637
  }.merge(turn_usage_fields(turn))
610
638
  end
611
639
 
@@ -622,6 +650,7 @@ module Samagotchi
622
650
  prefill_tps: turn.last_prefill_tps,
623
651
  tps_source: turn.tps_source
624
652
  }
653
+ fields[:cache_write_tokens_sum] = turn.cache_write_sum if turn.cache_write_sum.positive?
625
654
  fields.merge!(cost: turn.cost_sum, cost_source: "reported") if turn.cost_sum
626
655
  fields
627
656
  end
@@ -708,6 +737,7 @@ module Samagotchi
708
737
  turn.gen_prompt_max = [turn.gen_prompt_max, usage.prompt_tokens.to_i].max
709
738
  turn.gen_completion_max = [turn.gen_completion_max, usage.completion_tokens.to_i].max
710
739
  turn.gen_cached_max = [turn.gen_cached_max, usage.cached_tokens.to_i].max
740
+ turn.gen_cache_write_max = [turn.gen_cache_write_max, usage.cache_write_tokens.to_i].max
711
741
  turn.gen_reasoning_max = [turn.gen_reasoning_max, usage.reasoning_tokens.to_i].max
712
742
  turn.gen_cost = usage.cost if usage.cost
713
743
  if usage.predicted_per_second && usage.predicted_ms
@@ -857,7 +887,8 @@ module Samagotchi
857
887
  # (#snapshot adds the running turn). Caller holds the mutex.
858
888
  def reset_totals
859
889
  @totals = { cancellations: 0, prompt_sum: 0, completion_sum: 0, token_sources: [], iterations: 0,
860
- gen_ms: 0, retries: 0, by_tool: {}, tool_errors: 0, cached_sum: 0, reasoning_sum: 0,
890
+ gen_ms: 0, retries: 0, cuts: 0, capped: 0, by_tool: {}, tool_errors: 0, cached_sum: 0, cache_write_sum: 0,
891
+ reasoning_sum: 0,
861
892
  cost_sum: 0, decode_ms_sum: 0, decode_tokens_sum: 0, last_decode_tps: nil,
862
893
  last_prefill_tps: nil, tps_source: nil }
863
894
  end
@@ -870,7 +901,10 @@ module Samagotchi
870
901
  totals[:iterations] += number(record[:iterations])
871
902
  totals[:gen_ms] += number(record[:gen_ms])
872
903
  totals[:retries] += number(record[:retries])
904
+ totals[:cuts] += number(record[:cuts])
905
+ totals[:capped] += number(record[:capped])
873
906
  totals[:cached_sum] += number(record[:cached_tokens_sum])
907
+ totals[:cache_write_sum] += number(record[:cache_write_tokens_sum])
874
908
  totals[:reasoning_sum] += number(record[:reasoning_tokens])
875
909
  totals[:cost_sum] += number(record[:cost])
876
910
  totals[:decode_ms_sum] += number(record[:decode_ms])
@@ -14,8 +14,9 @@ module Samagotchi
14
14
  # The system prompt an Engine gives its model: the base prompt (tool
15
15
  # declarations and call syntax for the raw-prompt loop, none for the chat
16
16
  # loop, the shared guidance) and around it the thinking token, rg
17
- # guidance, AGENT.md, where the session runs, the memory indexes, the
18
- # identity memory and the preloaded memories.
17
+ # guidance, the identity memory, the preloaded memories, AGENT.md, the
18
+ # memory indexes and, last, the model, where the session runs and the
19
+ # session (#system_prompt_with_index has the order and why).
19
20
  #
20
21
  # What changes during a session (the profile after a model switch, the
21
22
  # tools a plugin replaces, the attached session, the thinking level) is
@@ -77,8 +78,16 @@ module Samagotchi
77
78
  # indexes and memories again (a model or profile switch, changed tools).
78
79
  def reset!
79
80
  @built = nil
81
+ @stable_lengths = nil
80
82
  end
81
83
 
84
+ # The length of +prompt+'s stable part when it is a chat prompt #build
85
+ # made: everything before the model, location and session lines, which
86
+ # a remote prompt cache can keep for every session (PromptCache). nil
87
+ # for any other text, or a prompt without those lines.
88
+ # @return [Integer, nil]
89
+ def stable_length(prompt) = @stable_lengths&.[](prompt)
90
+
82
91
  # The base prompt (specs, plugins' declarations), with Gemma's tool
83
92
  # declarations at its end as #build has them.
84
93
  def base(chat: false, thinking: nil)
@@ -186,7 +195,7 @@ module Samagotchi
186
195
  6. Use write for full-file rewrites or creating new files.
187
196
 
188
197
  Memory convention:
189
- Project scope: one folder per git repository, shared by its worktrees and subdirectories (path shown above)
198
+ Project scope: one folder per git repository, shared by its worktrees and subdirectories (its path is on the "Project memories folder:" line below)
190
199
  System scope: ~/.config/samagotchi/memories/ (cross-project)
191
200
  memory_read accepts optional scope (project|system).
192
201
  memory_write requires explicit scope and entry name.
@@ -229,16 +238,28 @@ module Samagotchi
229
238
  # @param chat [Boolean] no Gemma thinking token (the chat API's template
230
239
  # decides about thinking)
231
240
  # @param thinking [Symbol, nil] the level (Thinking); nil: the effective model's
241
+ #
242
+ # Ordered for the servers' prompt caches (they reuse only an exact token
243
+ # prefix): what every session shares first, what changes least before
244
+ # what changes more, and the per-session lines last. The global parts
245
+ # (identity, preloads) come before the project's AGENT.md, the memory
246
+ # indexes (a memory write changes them) after both; the model, the
247
+ # location and the session close the prompt (Gemma's tool declarations
248
+ # still follow them).
232
249
  def system_prompt_with_index(base, chat: false, thinking: nil)
233
- project_index = read_memory_index("project")
234
- system_index = read_memory_index("system")
235
- project_description = project_specific_description
236
250
  thinking_token = chat ? "" : Thinking.native(thinking || turn_thinking, profile).system_token
237
251
  memory_sections = [
238
- "Project memories:\n#{project_index}",
239
- "System memories:\n#{system_index}"
252
+ "Project memories:\n#{read_memory_index("project")}",
253
+ "System memories:\n#{read_memory_index("system")}"
240
254
  ].join("\n\n")
241
- prompt = [thinking_token + base, rg_guidance, project_description, project_location, current_model, current_session, memory_sections, system_identity_section, explicit_memory_section].compact.join("\n")
255
+ stable = [thinking_token + base, rg_guidance, system_identity_section, explicit_memory_section,
256
+ project_specific_description, memory_sections]
257
+ stable_text = stable.compact.join("\n")
258
+ volatile = [current_model, project_location, current_session].compact
259
+ return with_tail_declarations(stable_text, chat: chat) if volatile.empty?
260
+
261
+ prompt = "#{stable_text}\n#{volatile.join("\n")}"
262
+ (@stable_lengths ||= {})[prompt] = stable_text.length + 1 if chat
242
263
  with_tail_declarations(prompt, chat: chat)
243
264
  end
244
265
 
@@ -23,6 +23,7 @@ require_relative "../session_manager"
23
23
  require_relative "../session_metrics"
24
24
  require_relative "../guardrails/parent_approvals"
25
25
  require_relative "../tool_activity"
26
+ require_relative "../tool_view"
26
27
  require_relative "../web/message_parts"
27
28
 
28
29
  module Samagotchi
@@ -1109,7 +1110,8 @@ module Samagotchi
1109
1110
  output = part[:output]
1110
1111
  status = output.nil? ? "no result" : ToolActivity.tool_activity_status(output, part[:tool])
1111
1112
  activity = { action: part[:label] || ToolActivity.tool_activity_action(part[:tool]), tool: part[:tool],
1112
- params: part[:params], status: status }
1113
+ params: part[:params], status: status,
1114
+ description: ToolView.description_title(part[:view] || {}) }
1113
1115
  "#{format_tool_activity_line(activity, duration_ms: duration_ms)}" \
1114
1116
  "#{format_tool_image_suffix(part[:images])}#{format_tool_diff_suffix(part[:diff])}"
1115
1117
  end
@@ -1202,8 +1204,9 @@ module Samagotchi
1202
1204
  def replay_tool_completed(event)
1203
1205
  return @renderer.call({ duration_ms: nil }.merge(event)) if event.dig(:activity, :action)
1204
1206
 
1205
- activity = event[:activity] || {}
1207
+ activity = EventRenderer.described(event) || {}
1206
1208
  @screen.commit(snapshot_tool_line({ tool: event[:tool], params: activity[:params], status: activity[:status],
1209
+ description: activity[:description],
1207
1210
  images: event[:images], diff: event[:diff] }))
1208
1211
  end
1209
1212
 
@@ -4,6 +4,8 @@ require "monitor"
4
4
  require_relative "formatting"
5
5
  require_relative "../output_formatter"
6
6
  require_relative "../turn_tally"
7
+ require_relative "../tool_view"
8
+ require_relative "event_renderer"
7
9
  require_relative "thinking_line"
8
10
 
9
11
  module Samagotchi
@@ -111,7 +113,8 @@ module Samagotchi
111
113
  @retry = nil
112
114
  @waiting_since = nil
113
115
  @tool = "running #{event[:tool]}"
114
- @tally.started(key: tally_key(event), tool: event[:tool], params: event[:params])
116
+ @tally.started(key: tally_key(event), tool: event[:tool], params: event[:params],
117
+ description: ToolView.description_title(event[:view] || {}))
115
118
  redraw_status
116
119
  end
117
120
  end
@@ -119,7 +122,8 @@ module Samagotchi
119
122
  def tool_call_feedback_completed(event)
120
123
  @lock.synchronize do
121
124
  @tally.completed(key: tally_key(event), tool: event[:tool],
122
- status: event.dig(:activity, :status), params: event.dig(:activity, :params))
125
+ status: event.dig(:activity, :status), params: event.dig(:activity, :params),
126
+ description: EventRenderer.described(event)&.dig(:description))
123
127
  end
124
128
  end
125
129
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "formatting"
4
+ require_relative "../tool_view"
4
5
 
5
6
  module Samagotchi
6
7
  class TerminalUI
@@ -70,8 +71,8 @@ module Samagotchi
70
71
  @view.tool_call_feedback_completed(event)
71
72
  # A replayed row (a join) carries its duration (nil: unknown).
72
73
  measured = tool_duration_ms(event)
73
- render_streamed_tool_activity(event[:activity], duration_ms: event.fetch(:duration_ms, measured), images: event[:images],
74
- diff: event[:diff])
74
+ render_streamed_tool_activity(self.class.described(event), duration_ms: event.fetch(:duration_ms, measured),
75
+ images: event[:images], diff: event[:diff])
75
76
  when :generation_completed, :generation_cancelled, :tool_dispatch_started
76
77
  @view.generation_feedback_finished
77
78
  when :pending_input_merged
@@ -190,6 +191,17 @@ module Samagotchi
190
191
  Array(event[:steers]).each { |steer| @view.print_line(@view.format_steer_line(source: steer[:source], text: steer[:text])) }
191
192
  end
192
193
 
194
+ # A tool_call_completed's activity with the command's description:
195
+ # its own, else its view's (a snapshot's replay carries the view, not
196
+ # the description). nil for an event without one.
197
+ def self.described(event)
198
+ activity = event[:activity]
199
+ return activity if activity.nil? || activity[:description] || !event[:view].is_a?(Hash)
200
+
201
+ description = ToolView.description_title(event[:view].transform_keys(&:to_sym))
202
+ description ? activity.merge(description: description) : activity
203
+ end
204
+
193
205
  private
194
206
 
195
207
  def render_streamed_tool_activity(activity, duration_ms:, images: nil, diff: nil)