samagotchi 0.24.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +54 -1
- data/docs/architecture.md +16 -9
- data/docs/cli.md +4 -3
- data/docs/configuration.md +21 -13
- data/docs/hooks.md +1 -1
- data/docs/internals/background-tasks.md +1 -0
- data/docs/internals/prompt-caching.md +94 -0
- data/docs/plugins.md +16 -2
- data/docs/testing.md +12 -0
- data/lib/samagotchi/bundles/loop-guard/manifest.yml +2 -2
- data/lib/samagotchi/bundles/loop-guard/plugin.rb +43 -7
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/command_steps.rb +233 -0
- data/lib/samagotchi/config.rb +5 -2
- data/lib/samagotchi/empty_answer_retry.rb +2 -2
- data/lib/samagotchi/engine.rb +23 -4
- data/lib/samagotchi/guardrails/shell_lex.rb +49 -5
- data/lib/samagotchi/idle_client.rb +14 -2
- data/lib/samagotchi/kernel_loop.rb +21 -3
- data/lib/samagotchi/llm/chat_loop.rb +17 -3
- data/lib/samagotchi/llm/errors.rb +26 -1
- data/lib/samagotchi/llm/openai_chat.rb +5 -4
- data/lib/samagotchi/llm/prompt_cache.rb +34 -9
- data/lib/samagotchi/llm/usage.rb +18 -3
- data/lib/samagotchi/log_subscriber.rb +2 -1
- data/lib/samagotchi/plugin/api.rb +3 -2
- data/lib/samagotchi/session_metrics.rb +38 -4
- data/lib/samagotchi/system_prompt.rb +30 -9
- data/lib/samagotchi/terminal_ui/attached_loop.rb +5 -2
- data/lib/samagotchi/terminal_ui/attached_view.rb +6 -2
- data/lib/samagotchi/terminal_ui/event_renderer.rb +14 -2
- data/lib/samagotchi/terminal_ui/formatting.rb +31 -9
- data/lib/samagotchi/thinking_tails.rb +103 -0
- data/lib/samagotchi/token_usage.rb +18 -6
- data/lib/samagotchi/tool_activity.rb +37 -3
- data/lib/samagotchi/tool_declarations.rb +14 -0
- data/lib/samagotchi/tool_view.rb +40 -4
- data/lib/samagotchi/tools/builtins.rb +4 -1
- data/lib/samagotchi/tools/execute.rb +2 -1
- data/lib/samagotchi/tools/registry.rb +11 -4
- data/lib/samagotchi/tools/shell.rb +32 -0
- data/lib/samagotchi/tools/task_runtime.rb +3 -3
- data/lib/samagotchi/turn_tally.rb +17 -6
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/public/command_view.js +71 -3
- data/lib/samagotchi/web/public/format.js +11 -1
- data/lib/samagotchi/web/public/index.html +25 -0
- data/lib/samagotchi/web/public/stage_model.js +5 -1
- data/lib/samagotchi/web/public/stage_view.js +1 -1
- data/lib/samagotchi/web/public/turn_view.js +3 -3
- metadata +5 -1
|
@@ -25,6 +25,9 @@ module Samagotchi
|
|
|
25
25
|
# CreditsHeld 402 "… in-flight requests": credit reserved by other
|
|
26
26
|
# requests; a RateLimited, retried after 20 s
|
|
27
27
|
# OutOfCredits any other 402; never retried
|
|
28
|
+
# OverBudget 402 metadata.reason weight_exceeds_budget: the
|
|
29
|
+
# request alone exceeds the key's budget; an
|
|
30
|
+
# OutOfCredits, never retried
|
|
28
31
|
# ServerError 5xx or a server's error event mid-stream
|
|
29
32
|
# AuthError 401/403, or an API key variable that is not set
|
|
30
33
|
# BadRequest other 4xx (a context overflow is one, whatever status;
|
|
@@ -105,6 +108,15 @@ module Samagotchi
|
|
|
105
108
|
def summary = "out of credits on host #{host}: #{detail}; add credits, then send again"
|
|
106
109
|
end
|
|
107
110
|
|
|
111
|
+
# A 402 whose error.metadata.reason is weight_exceeds_budget (OpenRouter):
|
|
112
|
+
# this one request's credit reservation is larger than the key's whole
|
|
113
|
+
# budget, so no amount of waiting lets it through. Never retried, even
|
|
114
|
+
# when the message reads like the in-flight one.
|
|
115
|
+
class OverBudget < OutOfCredits
|
|
116
|
+
def summary = "request too large for the credit budget on host #{host}: #{detail}; " \
|
|
117
|
+
"lower max_tokens (default.max_tokens) or raise the key's credit limit"
|
|
118
|
+
end
|
|
119
|
+
|
|
108
120
|
class ServerError < ProviderError
|
|
109
121
|
def kind = :server
|
|
110
122
|
|
|
@@ -261,6 +273,9 @@ module Samagotchi
|
|
|
261
273
|
REASONING_REFUSED_RE = /reasoning|thinking/i
|
|
262
274
|
# A 402 about credit held by in-flight requests (see CreditsHeld).
|
|
263
275
|
CREDITS_HELD_RE = /in-flight requests/i
|
|
276
|
+
# A 402's error.metadata.reason when the request can never fit the
|
|
277
|
+
# key's budget (see OverBudget).
|
|
278
|
+
OVER_BUDGET_REASON = "weight_exceeds_budget"
|
|
264
279
|
# Seconds to wait before retrying a CreditsHeld without a Retry-After.
|
|
265
280
|
CREDITS_SETTLE_DELAY = 20.0
|
|
266
281
|
RETRYABLE_SERVER_STATUSES = [500, 502, 503, 504, 529].freeze
|
|
@@ -285,7 +300,9 @@ module Samagotchi
|
|
|
285
300
|
when 429 then RateLimited.new(text, retry_after: parse_retry_after(retry_after), **options)
|
|
286
301
|
when 408 then ServerError.new(text, retryable: true, **options)
|
|
287
302
|
when 402
|
|
288
|
-
if
|
|
303
|
+
if error_reason(body) == OVER_BUDGET_REASON
|
|
304
|
+
OverBudget.new(text, **options)
|
|
305
|
+
elsif CREDITS_HELD_RE.match?(message) || CREDITS_HELD_RE.match?(body.to_s)
|
|
289
306
|
CreditsHeld.new(text, retry_after: parse_retry_after(retry_after) || CREDITS_SETTLE_DELAY, **options)
|
|
290
307
|
else
|
|
291
308
|
OutOfCredits.new(text, **options)
|
|
@@ -356,6 +373,14 @@ module Samagotchi
|
|
|
356
373
|
message.empty? ? "(empty body)" : message[0, 500]
|
|
357
374
|
end
|
|
358
375
|
|
|
376
|
+
# error.metadata.reason of a JSON error body (OpenRouter), or nil.
|
|
377
|
+
def error_reason(body)
|
|
378
|
+
parsed = parse_json(body.to_s)
|
|
379
|
+
error = parsed["error"] if parsed.is_a?(Hash)
|
|
380
|
+
metadata = error["metadata"] if error.is_a?(Hash)
|
|
381
|
+
metadata["reason"] if metadata.is_a?(Hash)
|
|
382
|
+
end
|
|
383
|
+
|
|
359
384
|
# +message+ followed by the upstream provider's name and words from
|
|
360
385
|
# +metadata+ (raw may be text or a JSON error body of its own).
|
|
361
386
|
def with_upstream(message, metadata)
|
|
@@ -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
|
-
#
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
64
|
-
# text part (the last part
|
|
65
|
-
|
|
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
|
-
|
|
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) }
|
data/lib/samagotchi/llm/usage.rb
CHANGED
|
@@ -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
|
-
|
|
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,
|
|
268
|
-
#
|
|
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)
|
|
@@ -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,
|
|
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,
|
|
18
|
-
#
|
|
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
|
|
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#{
|
|
239
|
-
"System memories:\n#{
|
|
252
|
+
"Project memories:\n#{read_memory_index("project")}",
|
|
253
|
+
"System memories:\n#{read_memory_index("system")}"
|
|
240
254
|
].join("\n\n")
|
|
241
|
-
|
|
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
|
|
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
|
|
74
|
-
|
|
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)
|
|
@@ -11,11 +11,21 @@ module Samagotchi
|
|
|
11
11
|
# $stdout is a colour terminal.
|
|
12
12
|
module Formatting
|
|
13
13
|
def format_tool_activity_line(activity, duration_ms: nil)
|
|
14
|
-
params = activity[:params].to_s.strip
|
|
15
|
-
params_suffix = params.empty? ? "" : " #{paint(params, 90)}"
|
|
16
14
|
status = activity[:status].to_s
|
|
17
15
|
elapsed_suffix = duration_ms.nil? ? "" : " (#{format_elapsed_duration(duration_ms)})"
|
|
18
|
-
"#{paint("tool>", 36)} #{activity[:action]} (#{activity[:tool]}#{
|
|
16
|
+
"#{paint("tool>", 36)} #{activity[:action]} (#{activity[:tool]}#{tool_params_suffix(activity)}): " \
|
|
17
|
+
"#{paint(status, status_color(status))}#{elapsed_suffix}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# What a tool line says the call did: the model's description of a
|
|
21
|
+
# command (": List the specs") in place of its cut params, else the
|
|
22
|
+
# params, dim.
|
|
23
|
+
def tool_params_suffix(activity)
|
|
24
|
+
description = activity[:description].to_s.strip
|
|
25
|
+
return ": #{description}" unless description.empty?
|
|
26
|
+
|
|
27
|
+
params = activity[:params].to_s.strip
|
|
28
|
+
params.empty? ? "" : " #{paint(params, 90)}"
|
|
19
29
|
end
|
|
20
30
|
|
|
21
31
|
# Green ok, yellow stopped (a wait the user's Stop ended), red the rest.
|
|
@@ -30,12 +40,22 @@ module Samagotchi
|
|
|
30
40
|
|
|
31
41
|
STEER_PREVIEW = 80
|
|
32
42
|
|
|
43
|
+
# Who a steer names as its sender: a plugin's own label, chi's sender
|
|
44
|
+
# ids in words (the web's format.js steerSender agrees;
|
|
45
|
+
# spec/shared/labels_matrix.json). "": no one named.
|
|
46
|
+
STEER_SENDERS = { "parent_agent" => "parent agent", "chi_send" => "chi send", "plugin_send" => "plugin" }.freeze
|
|
47
|
+
|
|
48
|
+
def steer_sender(source)
|
|
49
|
+
STEER_SENDERS.fetch(source.to_s, source.to_s)
|
|
50
|
+
end
|
|
51
|
+
|
|
33
52
|
# "check-in> nudged: <text>", dim, one line cut: a plugin's steer
|
|
34
53
|
# (Steer) in the running turn or the join's last exchange.
|
|
35
54
|
def format_steer_line(source:, text:)
|
|
36
55
|
first = text.to_s.strip.split("\n").first.to_s
|
|
37
56
|
first = "#{first[0, STEER_PREVIEW - 1]}…" if first.length > STEER_PREVIEW || text.to_s.strip.include?("\n")
|
|
38
|
-
|
|
57
|
+
sender = steer_sender(source)
|
|
58
|
+
paint("#{sender.empty? ? "plugin" : sender}> nudged: #{first}", 90)
|
|
39
59
|
end
|
|
40
60
|
|
|
41
61
|
# "↻ empty answer, asking again (1/1)": the loop retries an empty answer;
|
|
@@ -119,10 +139,8 @@ module Samagotchi
|
|
|
119
139
|
|
|
120
140
|
# A tool call from the snapshot: it has no action text, only the tool.
|
|
121
141
|
def snapshot_tool_line(part)
|
|
122
|
-
params = part[:params].to_s.strip
|
|
123
|
-
params_suffix = params.empty? ? "" : " #{paint(params, 90)}"
|
|
124
142
|
status = part[:status].to_s
|
|
125
|
-
"#{paint("tool>", 36)} #{part[:tool]}#{
|
|
143
|
+
"#{paint("tool>", 36)} #{part[:tool]}#{tool_params_suffix(part)}: #{paint(status, status_color(status))}" \
|
|
126
144
|
"#{format_tool_image_suffix(part[:images])}#{format_tool_diff_suffix(part[:diff])}"
|
|
127
145
|
end
|
|
128
146
|
|
|
@@ -385,6 +403,9 @@ module Samagotchi
|
|
|
385
403
|
lines << "gen latency (ms): #{snapshot[:gen_latency_ms]}"
|
|
386
404
|
lines << "cancellations: #{snapshot[:cancellations]}"
|
|
387
405
|
lines << "retries: #{snapshot[:retries]}"
|
|
406
|
+
# A looped session: loop-guard's cuts, or thinking that ran to the cap.
|
|
407
|
+
lines << "thinking cuts: #{snapshot[:cuts].to_i}"
|
|
408
|
+
lines << "output cap hits: #{snapshot[:capped].to_i}"
|
|
388
409
|
context = snapshot[:context] || {}
|
|
389
410
|
if context[:used_tokens]
|
|
390
411
|
pct = context[:window_tokens].to_i.positive? ? format(" (%.1f%%)", context[:used_tokens] * 100.0 / context[:window_tokens]) : ""
|
|
@@ -400,14 +421,15 @@ module Samagotchi
|
|
|
400
421
|
lines.join("\n")
|
|
401
422
|
end
|
|
402
423
|
|
|
403
|
-
# ", cached 4864 (93%), reasoning 212" for the tokens
|
|
404
|
-
# server didn't report are left out.
|
|
424
|
+
# ", cached 4864 (93%), cache writes 312, reasoning 212" for the tokens
|
|
425
|
+
# line; the counts a server didn't report are left out.
|
|
405
426
|
def token_breakdown_text(tokens)
|
|
406
427
|
cached = tokens[:cached_sum].to_i
|
|
407
428
|
reasoning = tokens[:reasoning_sum].to_i
|
|
408
429
|
prompt = tokens[:prompt_sum].to_i
|
|
409
430
|
text = +""
|
|
410
431
|
text << ", cached #{cached} (#{(cached * 100.0 / prompt).round}%)" if cached.positive? && prompt.positive?
|
|
432
|
+
text << ", cache writes #{tokens[:cache_write_sum].to_i}" if tokens[:cache_write_sum].to_i.positive?
|
|
411
433
|
text << ", reasoning #{reasoning}" if reasoning.positive?
|
|
412
434
|
text
|
|
413
435
|
end
|