samagotchi 0.79.0 → 0.81.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 +59 -1
- data/README.md +7 -5
- data/docs/architecture.md +9 -1
- data/docs/cli.md +32 -7
- data/docs/configuration.md +10 -4
- data/docs/guardrails.md +26 -3
- data/docs/internals/anthropic.md +3 -1
- data/docs/internals/bundles.md +4 -0
- data/docs/internals/engine.md +13 -3
- data/docs/plugins.md +149 -3
- data/docs/releasing.md +2 -2
- data/docs/sessions.md +9 -1
- data/docs/testing.md +51 -2
- data/lib/samagotchi/bootstrap/config_writer.rb +13 -3
- data/lib/samagotchi/bootstrap/probe.rb +153 -31
- data/lib/samagotchi/bootstrap_command.rb +93 -16
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/guardrails/guardrails/rules.yml +4 -4
- data/lib/samagotchi/bundles/guardrails/manifest.yml +1 -1
- data/lib/samagotchi/bundles/slow-commands/manifest.yml +2 -2
- data/lib/samagotchi/bundles/slow-commands/plugin.rb +2 -1
- data/lib/samagotchi/bundles/system/manifest.yml +2 -2
- data/lib/samagotchi/bundles/system/self_map.md +1 -1
- data/lib/samagotchi/continue_offer.rb +5 -5
- data/lib/samagotchi/engine.rb +68 -39
- data/lib/samagotchi/guardrails/approval.rb +6 -4
- data/lib/samagotchi/guardrails/data_readers.rb +214 -0
- data/lib/samagotchi/guardrails/gate.rb +7 -3
- data/lib/samagotchi/guardrails/rules.rb +4 -2
- data/lib/samagotchi/guardrails/shell_lex.rb +166 -28
- data/lib/samagotchi/guardrails/targets.rb +14 -0
- data/lib/samagotchi/hooks/bundle_loader.rb +31 -16
- data/lib/samagotchi/hooks/registry.rb +44 -3
- data/lib/samagotchi/host_config.rb +8 -8
- data/lib/samagotchi/host_registry.rb +12 -4
- data/lib/samagotchi/idle_client.rb +41 -11
- data/lib/samagotchi/idle_recap.rb +6 -1
- data/lib/samagotchi/idle_target.rb +12 -4
- data/lib/samagotchi/llm/adapter_common.rb +4 -2
- data/lib/samagotchi/llm/anthropic_binding_recovery.rb +3 -0
- data/lib/samagotchi/llm/anthropic_messages.rb +2 -1
- data/lib/samagotchi/llm/http.rb +12 -7
- data/lib/samagotchi/llm/usage.rb +9 -3
- data/lib/samagotchi/model_price.rb +16 -0
- data/lib/samagotchi/plugin/card.rb +44 -0
- data/lib/samagotchi/plugin/context.rb +61 -9
- data/lib/samagotchi/plugin/http.rb +154 -0
- data/lib/samagotchi/plugin/http_transport.rb +77 -0
- data/lib/samagotchi/plugin/loader.rb +29 -10
- data/lib/samagotchi/plugin/service.rb +24 -1
- data/lib/samagotchi/question_answer.rb +29 -0
- data/lib/samagotchi/question_desk.rb +20 -19
- data/lib/samagotchi/recap_setup.rb +2 -1
- data/lib/samagotchi/relay_verifier.rb +1 -1
- data/lib/samagotchi/session_metrics/side_totals.rb +110 -0
- data/lib/samagotchi/session_metrics.rb +46 -12
- data/lib/samagotchi/session_observer.rb +3 -0
- data/lib/samagotchi/side_usage.rb +38 -0
- data/lib/samagotchi/terminal_ui/formatting.rb +3 -0
- data/lib/samagotchi/terminal_ui/model_segments_text.rb +27 -3
- data/lib/samagotchi/testing/boundary.rb +156 -0
- data/lib/samagotchi/testing/bundle.rb +349 -0
- data/lib/samagotchi/testing/fake_executable.rb +42 -0
- data/lib/samagotchi/testing/fake_http.rb +137 -0
- data/lib/samagotchi/testing/helpers.rb +56 -0
- data/lib/samagotchi/testing/host.rb +76 -0
- data/lib/samagotchi/testing/minitest.rb +31 -0
- data/lib/samagotchi/testing/recorder.rb +141 -0
- data/lib/samagotchi/testing/recording_api.rb +27 -0
- data/lib/samagotchi/testing/rspec.rb +31 -0
- data/lib/samagotchi/testing/sandbox.rb +165 -0
- data/lib/samagotchi/testing/sessions.rb +77 -0
- data/lib/samagotchi/testing/verdict.rb +28 -0
- data/lib/samagotchi/testing/waiting.rb +36 -0
- data/lib/samagotchi/testing.rb +26 -0
- data/lib/samagotchi/tools/delegate_relay.rb +7 -6
- data/lib/samagotchi/tools/tool_index.rb +3 -0
- data/lib/samagotchi/turn_state.rb +28 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +4 -1
- data/lib/samagotchi/web/public/app.js +47 -23
- data/lib/samagotchi/web/public/ctx.js +32 -6
- data/lib/samagotchi/web/public/images.js +33 -0
- data/lib/samagotchi/web/public/index.html +71 -38
- data/lib/samagotchi/web/public/info_bar_model.js +1 -1
- data/lib/samagotchi/web/public/level_chip.js +11 -11
- data/lib/samagotchi/web/public/lists_view.js +20 -15
- data/lib/samagotchi/web/public/llm_context_chip.js +15 -15
- data/lib/samagotchi/web/public/model_picker.js +1 -1
- data/lib/samagotchi/web/public/scope.js +3 -3
- data/lib/samagotchi/web/public/stream_handlers.js +7 -0
- data/lib/samagotchi/web/public/strip.js +35 -9
- data/lib/samagotchi/web/public/strip_grip.js +76 -0
- data/lib/samagotchi/web/public/thinking_chip.js +5 -5
- data/lib/samagotchi/web/public/timing.js +6 -1
- data/lib/samagotchi/web/session_summary.rb +2 -0
- metadata +24 -1
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../model_price"
|
|
4
|
+
|
|
5
|
+
module Samagotchi
|
|
6
|
+
class SessionMetrics
|
|
7
|
+
# The sums over a session's side requests (SideUsage: an idle recap, a
|
|
8
|
+
# plugin's ctx.ask_model such as /btw), kept apart from its turns'
|
|
9
|
+
# Totals: the snapshot's and analytics.json's +side+ block, never in
|
|
10
|
+
# +tokens+ or +model_segments+. They have no records: the block itself
|
|
11
|
+
# is saved and loaded (#to_h, .from_saved).
|
|
12
|
+
# +cost_sum+ is nil until a request reports a cost (a reported 0
|
|
13
|
+
# included), +cost_estimate_sum+ 0 until one is estimated from a price
|
|
14
|
+
# (ModelPrice.estimate, the turns' rule). +by_kind+ is a frozen Hash
|
|
15
|
+
# kind => {requests:, cost_sum:, cost_estimate_sum:} in first-seen order.
|
|
16
|
+
class SideTotals < Data.define(:requests, :prompt_sum, :completion_sum, :cached_sum, :cache_write_sum,
|
|
17
|
+
:reasoning_sum, :cost_sum, :cost_estimate_sum, :by_kind)
|
|
18
|
+
# The counts summed as they are (the costs and by_kind apart).
|
|
19
|
+
COUNTS = %i[requests prompt_sum completion_sum cached_sum cache_write_sum reasoning_sum].freeze
|
|
20
|
+
|
|
21
|
+
def self.zero
|
|
22
|
+
new(**COUNTS.to_h { |key| [key, 0] }, cost_sum: nil, cost_estimate_sum: 0, by_kind: {}.freeze)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The block as analytics.json saved it (string keys): a non-Hash is
|
|
26
|
+
# zero totals, a missing or non-numeric count 0, a missing or null
|
|
27
|
+
# cost_sum nil.
|
|
28
|
+
# @param hash [Hash, nil]
|
|
29
|
+
# @return [SideTotals]
|
|
30
|
+
def self.from_saved(hash)
|
|
31
|
+
return zero unless hash.is_a?(Hash)
|
|
32
|
+
|
|
33
|
+
hash = hash.transform_keys(&:to_s)
|
|
34
|
+
kinds = hash["by_kind"].is_a?(Hash) ? hash["by_kind"] : {}
|
|
35
|
+
new(**COUNTS.to_h { |key| [key, count(hash[key.to_s])] }, cost_sum: cost(hash["cost_sum"]),
|
|
36
|
+
cost_estimate_sum: number(hash["cost_estimate_sum"]),
|
|
37
|
+
by_kind: kinds.each_with_object({}) do |(kind, row), rows|
|
|
38
|
+
row = row.is_a?(Hash) ? row.transform_keys(&:to_s) : {}
|
|
39
|
+
rows[kind.to_s] = { requests: count(row["requests"]), cost_sum: cost(row["cost_sum"]),
|
|
40
|
+
cost_estimate_sum: number(row["cost_estimate_sum"]) }.freeze
|
|
41
|
+
end.freeze)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# These totals with one side request counted in.
|
|
45
|
+
# @param usage [SideUsage]
|
|
46
|
+
# @return [SideTotals]
|
|
47
|
+
def add(usage)
|
|
48
|
+
estimate = ModelPrice.estimate(usage.price, reported_cost: usage.cost, prompt_tokens: usage.prompt_tokens,
|
|
49
|
+
cached_tokens: usage.cached_tokens,
|
|
50
|
+
cache_write_tokens: usage.cache_write_tokens,
|
|
51
|
+
completion_tokens: usage.completion_tokens)
|
|
52
|
+
one = { requests: 1, cost_sum: usage.cost&.to_f, cost_estimate_sum: estimate || 0 }
|
|
53
|
+
kind = usage.kind.to_s
|
|
54
|
+
rows = by_kind.merge(kind => sum_row(by_kind[kind], one))
|
|
55
|
+
with(requests: requests + 1, prompt_sum: prompt_sum + usage.prompt_tokens.to_i,
|
|
56
|
+
completion_sum: completion_sum + usage.completion_tokens.to_i,
|
|
57
|
+
cached_sum: cached_sum + usage.cached_tokens.to_i,
|
|
58
|
+
cache_write_sum: cache_write_sum + usage.cache_write_tokens.to_i,
|
|
59
|
+
reasoning_sum: reasoning_sum + usage.reasoning_tokens.to_i,
|
|
60
|
+
cost_sum: add_cost(cost_sum, one[:cost_sum]),
|
|
61
|
+
cost_estimate_sum: cost_estimate_sum + one[:cost_estimate_sum], by_kind: rows.freeze)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# These totals with +other+'s added (a loaded block and the requests
|
|
65
|
+
# counted before the load); the kinds in first-seen order, these first.
|
|
66
|
+
# @param other [SideTotals]
|
|
67
|
+
# @return [SideTotals]
|
|
68
|
+
def merge(other)
|
|
69
|
+
rows = other.by_kind.each_with_object(by_kind.dup) { |(kind, row), all| all[kind] = sum_row(all[kind], row) }
|
|
70
|
+
with(**COUNTS.to_h { |key| [key, public_send(key) + other.public_send(key)] },
|
|
71
|
+
cost_sum: add_cost(cost_sum, other.cost_sum),
|
|
72
|
+
cost_estimate_sum: cost_estimate_sum + other.cost_estimate_sum, by_kind: rows.freeze)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def empty? = requests.zero?
|
|
76
|
+
|
|
77
|
+
# The snapshot's side block (symbol keys; by_kind keyed by kind).
|
|
78
|
+
# @return [Hash]
|
|
79
|
+
def to_h
|
|
80
|
+
counts = COUNTS.to_h { |key| [key, public_send(key)] }
|
|
81
|
+
counts.merge(cost_sum: cost_sum, cost_estimate_sum: cost_estimate_sum, by_kind: by_kind.transform_values(&:dup))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.count(value) = value.is_a?(Numeric) ? value.to_i : 0
|
|
85
|
+
|
|
86
|
+
def self.number(value) = value.is_a?(Numeric) ? value : 0
|
|
87
|
+
|
|
88
|
+
def self.cost(value) = value.is_a?(Numeric) ? value : nil
|
|
89
|
+
|
|
90
|
+
private_class_method :count, :number, :cost
|
|
91
|
+
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
def sum_row(row, one)
|
|
95
|
+
return one.dup.freeze unless row
|
|
96
|
+
|
|
97
|
+
{ requests: row[:requests] + one[:requests], cost_sum: add_cost(row[:cost_sum], one[:cost_sum]),
|
|
98
|
+
cost_estimate_sum: row[:cost_estimate_sum] + one[:cost_estimate_sum] }.freeze
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# nil + nil stays nil (no cost reported yet); a reported one sums.
|
|
102
|
+
def add_cost(left, right)
|
|
103
|
+
return left if right.nil?
|
|
104
|
+
return right if left.nil?
|
|
105
|
+
|
|
106
|
+
left + right
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -12,6 +12,8 @@ require_relative "model_price"
|
|
|
12
12
|
require_relative "session"
|
|
13
13
|
require_relative "session_metrics/totals"
|
|
14
14
|
require_relative "session_metrics/model_segment"
|
|
15
|
+
require_relative "session_metrics/side_totals"
|
|
16
|
+
require_relative "side_usage"
|
|
15
17
|
|
|
16
18
|
module Samagotchi
|
|
17
19
|
# SessionMetrics is a persistent, error-isolated collector of per-session
|
|
@@ -65,11 +67,14 @@ module Samagotchi
|
|
|
65
67
|
# nil when an older file has none.
|
|
66
68
|
# cost_estimate_sum: the estimated cost (hosts.<name>.models prices), 0
|
|
67
69
|
# in an older file.
|
|
70
|
+
# side: the side requests' block (SideTotals#to_h as saved, string
|
|
71
|
+
# keys), nil when the session made none (or an older file).
|
|
68
72
|
SavedSummary = Data.define(:ctx_pct, :prompt_sum, :completion_sum, :cached_sum, :reasoning_sum, :cost_sum,
|
|
69
|
-
:cost_estimate_sum, :memory_index) do
|
|
70
|
-
def initialize(memory_index: nil, cost_estimate_sum: 0, **) = super
|
|
73
|
+
:cost_estimate_sum, :memory_index, :side) do
|
|
74
|
+
def initialize(memory_index: nil, cost_estimate_sum: 0, side: nil, **) = super
|
|
71
75
|
|
|
72
|
-
|
|
76
|
+
# The turns' token totals (the side requests' are #side, apart).
|
|
77
|
+
def tokens = to_h.except(:ctx_pct, :memory_index, :side)
|
|
73
78
|
end
|
|
74
79
|
|
|
75
80
|
# Per-turn transient state, reset on each turn_started.
|
|
@@ -199,7 +204,8 @@ module Samagotchi
|
|
|
199
204
|
completion_sum: count.call("completion_sum"), cached_sum: count.call("cached_sum"),
|
|
200
205
|
reasoning_sum: count.call("reasoning_sum"), cost_sum: count.call("cost_sum"),
|
|
201
206
|
cost_estimate_sum: count.call("cost_estimate_sum"),
|
|
202
|
-
memory_index: data["memory_index"].is_a?(Hash) ? data["memory_index"] : nil
|
|
207
|
+
memory_index: data["memory_index"].is_a?(Hash) ? data["memory_index"] : nil,
|
|
208
|
+
side: data["side"].is_a?(Hash) ? data["side"] : nil)
|
|
203
209
|
rescue TypeError
|
|
204
210
|
nil
|
|
205
211
|
end
|
|
@@ -267,6 +273,10 @@ module Samagotchi
|
|
|
267
273
|
|
|
268
274
|
def initialize(clock: nil, wall_clock: nil)
|
|
269
275
|
@mutex = Monitor.new
|
|
276
|
+
# Serializes #persist (snapshot and write), so two threads' saves land
|
|
277
|
+
# in snapshot order. Taken before @mutex, never under it: the write's
|
|
278
|
+
# IO doesn't hold up the events.
|
|
279
|
+
@persist_mutex = Mutex.new
|
|
270
280
|
@clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
271
281
|
@wall_clock = wall_clock || -> { Time.now }
|
|
272
282
|
@session_id = nil
|
|
@@ -281,6 +291,9 @@ module Samagotchi
|
|
|
281
291
|
# and how many were loaded (a woken collector's live records start empty).
|
|
282
292
|
@persisted_turns = 0
|
|
283
293
|
@loaded_turns = 0
|
|
294
|
+
# The side requests' sums: not in reset_totals, which a load's recount
|
|
295
|
+
# of the turn records calls (side requests have no records).
|
|
296
|
+
@side = SideTotals.zero
|
|
284
297
|
reset_totals
|
|
285
298
|
end
|
|
286
299
|
|
|
@@ -338,6 +351,17 @@ module Samagotchi
|
|
|
338
351
|
end
|
|
339
352
|
end
|
|
340
353
|
|
|
354
|
+
# Count one side request (a recap, a plugin's ctx.ask_model) in the
|
|
355
|
+
# side block, apart from the turns' totals. Safe from any thread.
|
|
356
|
+
# @param usage [SideUsage]
|
|
357
|
+
# @return [Hash] the side block now (SideTotals#to_h)
|
|
358
|
+
def add_side(usage)
|
|
359
|
+
@mutex.synchronize do
|
|
360
|
+
@side = @side.add(usage)
|
|
361
|
+
@side.to_h
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
341
365
|
# Event sink. Safe to call from any thread; errors are isolated by the
|
|
342
366
|
# caller (SessionObserver / TerminalUI) but we still guard internally.
|
|
343
367
|
# @param event [Hash]
|
|
@@ -454,6 +478,15 @@ module Samagotchi
|
|
|
454
478
|
# turn's own end writes over it. Only the file has it.
|
|
455
479
|
# @return [Boolean] true on success
|
|
456
480
|
def persist(state_dir: nil, running_turn: false)
|
|
481
|
+
@persist_mutex.synchronize { persist_now(state_dir, running_turn) }
|
|
482
|
+
rescue StandardError
|
|
483
|
+
false
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
private
|
|
487
|
+
|
|
488
|
+
# #persist's work, under the persist mutex.
|
|
489
|
+
def persist_now(state_dir, running_turn)
|
|
457
490
|
sid, data, turn_count = @mutex.synchronize do
|
|
458
491
|
data = build_snapshot(:all).merge(active_turn: nil, active_tools: [])
|
|
459
492
|
if running_turn && @turn
|
|
@@ -471,12 +504,8 @@ module Samagotchi
|
|
|
471
504
|
AtomicFile.write(AnalyticsFile.path(dir), JSON.pretty_generate(data) + "\n")
|
|
472
505
|
@mutex.synchronize { @persisted_turns = [@persisted_turns, turn_count].max }
|
|
473
506
|
true
|
|
474
|
-
rescue StandardError
|
|
475
|
-
false
|
|
476
507
|
end
|
|
477
508
|
|
|
478
|
-
private
|
|
479
|
-
|
|
480
509
|
# Caller holds the mutex. +records+: :recent (the live snapshot) or :all
|
|
481
510
|
# (analytics.json).
|
|
482
511
|
def build_snapshot(records_mode)
|
|
@@ -502,6 +531,9 @@ module Samagotchi
|
|
|
502
531
|
# The calls the model made under an alias's name (bash run as
|
|
503
532
|
# execute), by its own spelling; only when there are any.
|
|
504
533
|
**(@tool_tallies[:aliased].empty? ? {} : { tool_calls_aliased: @tool_tallies[:aliased].dup }),
|
|
534
|
+
# The side requests (recap, ctx.ask_model), apart from +tokens+;
|
|
535
|
+
# only when there were any.
|
|
536
|
+
**(@side.empty? ? {} : { side: @side.to_h }),
|
|
505
537
|
tool_errors: @tool_tallies[:errors],
|
|
506
538
|
iterations_total: totals.iterations,
|
|
507
539
|
gen_latency_ms: totals.gen_ms.round,
|
|
@@ -791,10 +823,10 @@ module Samagotchi
|
|
|
791
823
|
turn.cost_sum = turn.cost_sum.to_f + turn.gen_cost if turn.gen_cost
|
|
792
824
|
# A reported cost wins; a reported 0 with a price is taken as no report
|
|
793
825
|
# (a gateway that doesn't bill per call); the 0 stays in cost.
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
826
|
+
estimate = ModelPrice.estimate(turn.gen_price, reported_cost: turn.gen_cost, prompt_tokens: turn.gen_prompt_max,
|
|
827
|
+
cached_tokens: turn.gen_cached_max,
|
|
828
|
+
cache_write_tokens: turn.gen_cache_write_max, completion_tokens: completion)
|
|
829
|
+
turn.cost_estimate_sum += estimate if estimate
|
|
798
830
|
turn.last_prefill_tps = turn.gen_prefill_tps.round(1) if turn.gen_prefill_tps
|
|
799
831
|
speed = generation_speed(turn, completion)
|
|
800
832
|
return unless speed
|
|
@@ -1098,6 +1130,8 @@ module Samagotchi
|
|
|
1098
1130
|
end
|
|
1099
1131
|
# The indexes the prompt last held, until this process builds its own.
|
|
1100
1132
|
@memory_index ||= symbolized(prior["memory_index"]) if prior["memory_index"].is_a?(Hash)
|
|
1133
|
+
# The saved side block, with any counted before the load after it.
|
|
1134
|
+
@side = SideTotals.from_saved(prior["side"]).merge(@side)
|
|
1101
1135
|
# The window last seen, until this process's first generation reports.
|
|
1102
1136
|
window = prior["context"].is_a?(Hash) ? prior["context"] : {}
|
|
1103
1137
|
return unless window["window_tokens"] && @context_window_tokens.nil?
|
|
@@ -22,6 +22,9 @@ module Samagotchi
|
|
|
22
22
|
# * recap_ready — an idle session-recap finished generating
|
|
23
23
|
# (`recap:` prose, `generation:` id). Purely additive: it never mutates
|
|
24
24
|
# session.messages; each UI renders it (or not) however it likes.
|
|
25
|
+
# * side_usage — a side request (the recap, a plugin's ctx.ask_model) was
|
|
26
|
+
# counted (Engine#note_side_usage): `kind:` and `side:`, the session's
|
|
27
|
+
# whole side block (SessionMetrics#add_side), in fold order.
|
|
25
28
|
class SessionObserver
|
|
26
29
|
# Handle returned by #subscribe. Holds the observer reference and carries
|
|
27
30
|
# #unsubscribe so a caller can deregister without keeping the registry.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Samagotchi
|
|
4
|
+
# One side request's usage (an idle recap, a plugin's ctx.ask_model such
|
|
5
|
+
# as /btw): what IdleClient reports to its on_usage callback and
|
|
6
|
+
# SessionMetrics#add_side folds into the session's side line, apart from
|
|
7
|
+
# its turns' totals.
|
|
8
|
+
# @!attribute kind [String] "recap", or the asking bundle's name ("btw")
|
|
9
|
+
# @!attribute model [String] the target's model, for the log
|
|
10
|
+
# @!attribute prompt_tokens [Integer] 0 when not reported (and the rest)
|
|
11
|
+
# @!attribute completion_tokens [Integer]
|
|
12
|
+
# @!attribute cached_tokens [Integer]
|
|
13
|
+
# @!attribute cache_write_tokens [Integer]
|
|
14
|
+
# @!attribute reasoning_tokens [Integer]
|
|
15
|
+
# @!attribute cost [Float, nil] the provider's reported cost (OpenRouter's
|
|
16
|
+
# usage.cost), nil when not reported
|
|
17
|
+
# @!attribute price [ModelPrice, nil] the target host's price for the
|
|
18
|
+
# model (IdleTarget#price), what the cost is estimated from
|
|
19
|
+
SideUsage = Data.define(:kind, :model, :prompt_tokens, :completion_tokens, :cached_tokens, :cache_write_tokens,
|
|
20
|
+
:reasoning_tokens, :cost, :price) do
|
|
21
|
+
def initialize(kind:, model: nil, prompt_tokens: 0, completion_tokens: 0, cached_tokens: 0, cache_write_tokens: 0,
|
|
22
|
+
reasoning_tokens: 0, cost: nil, price: nil)
|
|
23
|
+
super
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A response's usage (LLM::Usage; Usage.none's zeros when the server
|
|
27
|
+
# reported no counts: still one request) as a side request's.
|
|
28
|
+
# @param usage [LLM::Usage, nil]
|
|
29
|
+
# @return [SideUsage]
|
|
30
|
+
def self.from_usage(usage, kind:, model:, price:)
|
|
31
|
+
count = ->(name) { usage.respond_to?(name) ? usage.public_send(name).to_i : 0 }
|
|
32
|
+
new(kind: kind.to_s, model: model, prompt_tokens: count.call(:prompt_tokens),
|
|
33
|
+
completion_tokens: count.call(:completion_tokens), cached_tokens: count.call(:cached_tokens),
|
|
34
|
+
cache_write_tokens: count.call(:cache_write_tokens), reasoning_tokens: count.call(:reasoning_tokens),
|
|
35
|
+
cost: usage.respond_to?(:cost) ? usage.cost : nil, price: price)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -594,6 +594,9 @@ module Samagotchi
|
|
|
594
594
|
lines << "speed: #{speed}" if speed
|
|
595
595
|
cost = stats_cost_text(tokens, price_host: snapshot[:price_host], per_model: segments.size >= 2)
|
|
596
596
|
lines << "cost: #{cost}" if cost
|
|
597
|
+
# The side requests, never in the totals above.
|
|
598
|
+
side = side_requests_text(snapshot[:side])
|
|
599
|
+
lines << "side requests: #{side} (not in the totals above)" unless side.empty?
|
|
597
600
|
lines << "gen latency (ms): #{snapshot[:gen_latency_ms]}"
|
|
598
601
|
lines << "cancellations: #{snapshot[:cancellations]}"
|
|
599
602
|
lines << "retries: #{snapshot[:retries]}"
|
|
@@ -5,9 +5,11 @@ require_relative "../memory_bundle/index_size"
|
|
|
5
5
|
|
|
6
6
|
module Samagotchi
|
|
7
7
|
class TerminalUI
|
|
8
|
-
# A session's rows per model in /stats (the snapshot's model_segments)
|
|
9
|
-
#
|
|
10
|
-
#
|
|
8
|
+
# A session's rows per model in /stats (the snapshot's model_segments),
|
|
9
|
+
# and its side requests' line (the snapshot's side block).
|
|
10
|
+
# The four public model_segment_* labels and side_requests_text are the
|
|
11
|
+
# words the web's ctx tooltip shares (ctx.js;
|
|
12
|
+
# spec/shared/labels_matrix.json model_segments, side_requests).
|
|
11
13
|
# Rows come symbol- or string-keyed (the attached TUI's as JSON).
|
|
12
14
|
# Included by Formatting, whose cost_text and speed_text it uses.
|
|
13
15
|
module ModelSegmentsText
|
|
@@ -34,6 +36,28 @@ module Samagotchi
|
|
|
34
36
|
.reject(&:empty?).join(" + ")
|
|
35
37
|
end
|
|
36
38
|
|
|
39
|
+
# "3 (recap 2, btw 1) · 14.2k in / 380 out · $0.0012 + ~$0.0009": the
|
|
40
|
+
# side requests (the snapshot's side block: the recap, /btw and other
|
|
41
|
+
# ctx.ask_model calls), the kinds in first-seen order (one kind: its
|
|
42
|
+
# name alone), then the model rows' tokens and cost words on the
|
|
43
|
+
# block; "" without any (the line is hidden).
|
|
44
|
+
def side_requests_text(side)
|
|
45
|
+
return "" unless side.is_a?(Hash)
|
|
46
|
+
|
|
47
|
+
count = segment_value(side, :requests).to_i
|
|
48
|
+
return "" unless count.positive?
|
|
49
|
+
|
|
50
|
+
kinds = segment_value(side, :by_kind)
|
|
51
|
+
kinds = {} unless kinds.is_a?(Hash)
|
|
52
|
+
names = if kinds.size == 1
|
|
53
|
+
kinds.keys.first.to_s
|
|
54
|
+
else
|
|
55
|
+
kinds.map { |kind, row| "#{kind} #{(row.is_a?(Hash) ? segment_value(row, :requests) : 0).to_i}" }.join(", ")
|
|
56
|
+
end
|
|
57
|
+
head = names.empty? ? count.to_s : "#{count} (#{names})"
|
|
58
|
+
[head, model_segment_tokens_text(side), model_segment_cost_text(side)].reject(&:empty?).join(" · ")
|
|
59
|
+
end
|
|
60
|
+
|
|
37
61
|
# "switch prefill 3.0k uncached prompt ≈$0.0060": what the switch to the
|
|
38
62
|
# row's model prefilled (its cost an estimate from the configured
|
|
39
63
|
# price, left out without one); "" for none.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Samagotchi
|
|
4
|
+
module Testing
|
|
5
|
+
# What a bundle's Ruby may reach of chi (docs/plugins.md, "Stable for
|
|
6
|
+
# bundles"): ALLOWED is the checked copy of that list. #scan finds a
|
|
7
|
+
# bundle's other references to Samagotchi:: and requires of chi's files;
|
|
8
|
+
# EXCEPTIONS is what the shipped bundles reach today anyway, each with
|
|
9
|
+
# why: the APIs to design before that bundle could leave chi's repo
|
|
10
|
+
# (spec/samagotchi/testing/boundary_spec.rb keeps both exact).
|
|
11
|
+
module Boundary
|
|
12
|
+
# A name a bundle may use, and the file that defines it (a require of
|
|
13
|
+
# it is fine too). A name allows itself, its methods (".x") and what
|
|
14
|
+
# it holds ("::X"); one ending in a method or constant allows only that.
|
|
15
|
+
Allowed = Data.define(:name, :file)
|
|
16
|
+
|
|
17
|
+
ALLOWED = [
|
|
18
|
+
Allowed.new(name: "Samagotchi::Plugin::Api", file: "samagotchi/plugin/api"),
|
|
19
|
+
Allowed.new(name: "Samagotchi::Plugin::ToolResult", file: "samagotchi/plugin/tool_result"),
|
|
20
|
+
Allowed.new(name: "Samagotchi::Plugin::ModelError", file: "samagotchi/plugin/context"),
|
|
21
|
+
Allowed.new(name: "Samagotchi::Plugin::ModelCancelled", file: "samagotchi/plugin/context"),
|
|
22
|
+
Allowed.new(name: "Samagotchi::Plugin::HttpError", file: "samagotchi/plugin/http"),
|
|
23
|
+
Allowed.new(name: "Samagotchi::Plugin::HttpCancelled", file: "samagotchi/plugin/http"),
|
|
24
|
+
Allowed.new(name: "Samagotchi::Plugin::Http::Response", file: "samagotchi/plugin/http"),
|
|
25
|
+
Allowed.new(name: "Samagotchi::Plugin::Service::Stopped", file: "samagotchi/plugin/service"),
|
|
26
|
+
Allowed.new(name: "Samagotchi::Plugin::Sessions::Error", file: "samagotchi/plugin/sessions"),
|
|
27
|
+
Allowed.new(name: "Samagotchi::Plugin::AttachedContext::Error", file: "samagotchi/plugin/attached_context"),
|
|
28
|
+
Allowed.new(name: "Samagotchi::Plugin::Peer", file: "samagotchi/plugin/peer"),
|
|
29
|
+
Allowed.new(name: "Samagotchi::Tools::ToolIndex::Tool", file: "samagotchi/tools/tool_index"),
|
|
30
|
+
Allowed.new(name: "Samagotchi::Tools::ToolIndex::Group", file: "samagotchi/tools/tool_index"),
|
|
31
|
+
Allowed.new(name: "Samagotchi::Tools::ToolIndex.schema_note", file: "samagotchi/tools/tool_index"),
|
|
32
|
+
Allowed.new(name: "Samagotchi::Tools::ToolIndex.spec_tokens", file: "samagotchi/tools/tool_index"),
|
|
33
|
+
Allowed.new(name: "Samagotchi::Tools::Execute::NAME", file: "samagotchi/tools/execute"),
|
|
34
|
+
Allowed.new(name: "Samagotchi::Tools::Execute::TIMEOUT_PREFIX", file: "samagotchi/tools/execute"),
|
|
35
|
+
Allowed.new(name: "Samagotchi::Tools::Execute::HANDED_OVER", file: "samagotchi/tools/execute"),
|
|
36
|
+
Allowed.new(name: "Samagotchi::Tools::Execute.timeout_seconds", file: "samagotchi/tools/execute"),
|
|
37
|
+
Allowed.new(name: "Samagotchi::Tools::TaskRuntime::TASKS_DIR", file: "samagotchi/tools/task_runtime"),
|
|
38
|
+
Allowed.new(name: "Samagotchi::Tools::TaskRuntime::EXIT_CODE_FILENAME", file: "samagotchi/tools/task_runtime"),
|
|
39
|
+
Allowed.new(name: "Samagotchi::AtomicFile.write", file: "samagotchi/atomic_file"),
|
|
40
|
+
Allowed.new(name: "Samagotchi::Log.warn", file: "samagotchi/log"),
|
|
41
|
+
Allowed.new(name: "Samagotchi::Log.debug", file: "samagotchi/log"),
|
|
42
|
+
Allowed.new(name: "Samagotchi::Config.get", file: "samagotchi/config"),
|
|
43
|
+
Allowed.new(name: "Samagotchi::VERSION", file: "samagotchi/version"),
|
|
44
|
+
# The kit itself, for a bundle's own specs.
|
|
45
|
+
Allowed.new(name: "Samagotchi::Testing", file: "samagotchi/testing")
|
|
46
|
+
].freeze
|
|
47
|
+
|
|
48
|
+
# The kit's adapters, required by a bundle's specs.
|
|
49
|
+
ALLOWED_REQUIRES = (ALLOWED.map(&:file) + %w[samagotchi/testing/rspec samagotchi/testing/minitest]).uniq.freeze
|
|
50
|
+
|
|
51
|
+
SESSION = "a saved session (context sources are keyed by its id): needs a kit helper that saves one"
|
|
52
|
+
SOURCES = "the session's context sources (the peer's inbox / outbox): needs a kit helper that attaches one"
|
|
53
|
+
REF_LIMITS = "a ref's kinds and limits (event[:refs] checks them too): could be stable constants"
|
|
54
|
+
# The peers' specs pair a session through its context sources.
|
|
55
|
+
PEER_SPEC = {
|
|
56
|
+
'require "samagotchi/context_sources"' => "spec: #{SOURCES}",
|
|
57
|
+
"Samagotchi::Session.new_session" => "spec: #{SESSION}",
|
|
58
|
+
"Samagotchi::ContextSources.session_location" => "spec: #{SOURCES}",
|
|
59
|
+
"Samagotchi::ContextSources::Source.new" => "spec: #{SOURCES}",
|
|
60
|
+
"Samagotchi::ContextSources::Fetched.new" => "spec: an outbox answer as the context poller records it"
|
|
61
|
+
}.freeze
|
|
62
|
+
|
|
63
|
+
# bundle => {reference => why}: what each shipped bundle reaches
|
|
64
|
+
# beyond ALLOWED today, its specs' (why "spec: …") included.
|
|
65
|
+
EXCEPTIONS = {
|
|
66
|
+
"agents-md" => {
|
|
67
|
+
'require "samagotchi/guardrails/context"' => "for GitInfo: a ctx helper for the checkout's files could replace it",
|
|
68
|
+
"Samagotchi::Guardrails::GitInfo.new" => "the work tree's top for the AGENTS.md chain (ctx.repo_root covers the top only)"
|
|
69
|
+
},
|
|
70
|
+
"claude-peer" => PEER_SPEC,
|
|
71
|
+
"codex-peer" => PEER_SPEC,
|
|
72
|
+
"coordinator" => {
|
|
73
|
+
"Samagotchi::MemoryPaths.scope_dir" => "where the coordinator's memories live: needs a ctx memory-path API",
|
|
74
|
+
"Samagotchi::MemoryPaths" => "spec: checks /coordinate end reads no memories (MemoryPaths.scope_dir isn't called)"
|
|
75
|
+
},
|
|
76
|
+
"github-pr" => {
|
|
77
|
+
"Samagotchi::Session.new_session" => "spec: #{SESSION} (ctx.delegate?, ctx.fork?, ctx.context read it)",
|
|
78
|
+
"Samagotchi::ContextSources.session_location" => "spec: reads and removes the session's sources (added_by), " \
|
|
79
|
+
"which ctx.context.list doesn't show",
|
|
80
|
+
'require "samagotchi/memory_bundle/installer"' => "spec: for Installer",
|
|
81
|
+
"Samagotchi::MemoryBundle::Installer.new" => "spec: installs the bundle so its context provider resolves: " \
|
|
82
|
+
"needs a kit install helper",
|
|
83
|
+
'require "samagotchi/context_providers"' => "spec: for ContextProviders",
|
|
84
|
+
"Samagotchi::ContextProviders.resolve" => "spec: the manifest's URL provider through chi's own resolver",
|
|
85
|
+
'require "samagotchi/answer_display"' => "spec: for AnswerDisplay",
|
|
86
|
+
"Samagotchi::AnswerDisplay.new" => "spec: after_turn's present: helper, as chi's fire site builds it: " \
|
|
87
|
+
"the kit could supply after_turn's fire-site keys",
|
|
88
|
+
'require "samagotchi/answer_refs"' => "spec: for AnswerRefs",
|
|
89
|
+
"Samagotchi::AnswerRefs.new" => "spec: after_turn's refs: helper, as chi's fire site builds it",
|
|
90
|
+
"Samagotchi::Log" => "spec: stubs Log to check what ctx.log recorded: a kit log recorder could replace it"
|
|
91
|
+
},
|
|
92
|
+
"loop-guard" => {
|
|
93
|
+
'require "samagotchi/llm/native_tool_normalizer"' => "spec: for NativeToolNormalizer",
|
|
94
|
+
"Samagotchi::LLM::NativeToolNormalizer.normalize" => "spec: replays recorded calls in the shape chi's parser gives",
|
|
95
|
+
"Samagotchi::Hooks::StreamWatch::EVERY_CHARS" => "spec: the stream watch's batch size (a ctx or kit value later)"
|
|
96
|
+
},
|
|
97
|
+
"skills" => {
|
|
98
|
+
"Samagotchi::Tools::MemoryRead.memories_dir" => "lists the skill memories: needs a ctx memory-path API"
|
|
99
|
+
},
|
|
100
|
+
"mcp" => {
|
|
101
|
+
'require "samagotchi/process_group"' => "for ProcessGroup.stop",
|
|
102
|
+
"Samagotchi::ProcessGroup.stop" => "stops a server's process group: needs a ctx process API",
|
|
103
|
+
'require "samagotchi/token_usage"' => "for CHARS_PER_TOKEN",
|
|
104
|
+
"Samagotchi::TokenUsage::CHARS_PER_TOKEN" => "estimates a tool's tokens: could be a stable constant",
|
|
105
|
+
'require "samagotchi/tool_declarations"' => "loaded for Api.tool_spec's schemas"
|
|
106
|
+
},
|
|
107
|
+
"slow-commands" => {
|
|
108
|
+
'require "samagotchi/guardrails/shell_lex"' => "for ShellLex",
|
|
109
|
+
"Samagotchi::Guardrails::ShellLex" => "splits a command into its parts: needs a stable shell-parsing helper",
|
|
110
|
+
"Samagotchi::Tools::Execute.const_defined?" => "reads execute's limits that older chis lack, by reflection",
|
|
111
|
+
"Samagotchi::Tools::Execute.const_get" => "reads execute's limits that older chis lack, by reflection"
|
|
112
|
+
},
|
|
113
|
+
"source-links" => {
|
|
114
|
+
'require "samagotchi/ref"' => "for the Ref / RefAction limits",
|
|
115
|
+
"Samagotchi::Ref::FILE" => REF_LIMITS,
|
|
116
|
+
"Samagotchi::Ref::KIND.match?" => REF_LIMITS,
|
|
117
|
+
"Samagotchi::Ref::MAX_LABEL" => REF_LIMITS,
|
|
118
|
+
"Samagotchi::RefAction::MAX_LABEL" => REF_LIMITS,
|
|
119
|
+
"Samagotchi::RefAction.http_url?" => "which URLs an action may open: could be a stable helper"
|
|
120
|
+
}
|
|
121
|
+
}.freeze
|
|
122
|
+
|
|
123
|
+
# A Samagotchi:: name with what follows it (::X, .method).
|
|
124
|
+
REFERENCE = /\bSamagotchi(?:::[A-Z]\w*)+(?:\.[a-z_]\w*[?!]?)?/
|
|
125
|
+
# A require of one of chi's files.
|
|
126
|
+
REQUIRE = %r{\brequire\s*\(?\s*["'](samagotchi(?:/[\w/]+)?)["']}
|
|
127
|
+
|
|
128
|
+
# One reference outside ALLOWED, in a file of the bundle.
|
|
129
|
+
Finding = Data.define(:reference, :file)
|
|
130
|
+
|
|
131
|
+
module_function
|
|
132
|
+
|
|
133
|
+
# The bundle's references to chi outside ALLOWED (its plugin, hooks,
|
|
134
|
+
# scripts and specs: every .rb under +dir+), one per reference and
|
|
135
|
+
# file, sorted.
|
|
136
|
+
# @param dir [String] the bundle's folder
|
|
137
|
+
# @return [Array<Finding>]
|
|
138
|
+
def scan(dir)
|
|
139
|
+
Dir.glob(File.join(dir, "**", "*.rb")).sort.flat_map do |path|
|
|
140
|
+
text = File.read(path)
|
|
141
|
+
refs = text.scan(REFERENCE).reject { |name| allowed?(name) }
|
|
142
|
+
refs += text.scan(REQUIRE).flatten.reject { |file| ALLOWED_REQUIRES.include?(file) }.map { |file| %(require "#{file}") }
|
|
143
|
+
file = path.delete_prefix("#{File.expand_path(dir)}/")
|
|
144
|
+
refs.uniq.map { |reference| Finding.new(reference: reference, file: file) }
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Whether a bundle may use +name+ ("Samagotchi::X::Y", maybe ".method").
|
|
149
|
+
def allowed?(name)
|
|
150
|
+
ALLOWED.any? do |entry|
|
|
151
|
+
name == entry.name || name.start_with?("#{entry.name}.", "#{entry.name}::")
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|