samagotchi 0.192.0 → 0.194.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 +128 -1
- data/docs/architecture.md +3 -3
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +82 -40
- data/docs/cli.md +47 -17
- data/docs/configuration.md +75 -8
- data/docs/context.md +8 -5
- data/docs/desktop.md +3 -0
- data/docs/guardrails.md +20 -5
- data/docs/hooks.md +1 -0
- data/docs/internals/bridge.md +3 -1
- data/docs/internals/bundles.md +2 -2
- data/docs/internals/engine.md +46 -3
- data/docs/internals/pages.md +4 -4
- data/docs/internals/panes.md +66 -0
- data/docs/internals/participants.md +5 -3
- data/docs/internals/peers.md +176 -27
- data/docs/internals/prompt-caching.md +6 -1
- data/docs/internals/remote-sessions.md +6 -0
- data/docs/internals/session-lifecycle.md +12 -4
- data/docs/internals/web-frontend.md +106 -22
- data/docs/internals/working-set.md +5 -1
- data/docs/peers.md +150 -27
- data/docs/plugins.md +48 -7
- data/docs/releasing.md +30 -2
- data/docs/scheduling.md +3 -2
- data/docs/sessions.md +16 -7
- data/docs/sub-agent.md +6 -4
- data/docs/system-one.md +4 -0
- data/docs/testing.md +38 -3
- data/docs/thin-clients.md +27 -11
- data/lib/samagotchi/bridge/card_store.rb +1 -1
- data/lib/samagotchi/broadcast/triage_model.rb +17 -0
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +61 -24
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +57 -19
- data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
- data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/chi_invocation.rb +7 -0
- data/lib/samagotchi/client.rb +18 -0
- data/lib/samagotchi/codex_rollout.rb +93 -0
- data/lib/samagotchi/compat.rb +8 -1
- data/lib/samagotchi/config.rb +19 -6
- data/lib/samagotchi/continue_offer.rb +13 -1
- data/lib/samagotchi/delivery.rb +2 -2
- data/lib/samagotchi/effective_model.rb +28 -2
- data/lib/samagotchi/engine/cards.rb +9 -4
- data/lib/samagotchi/engine/hooks.rb +8 -0
- data/lib/samagotchi/engine/models.rb +197 -7
- data/lib/samagotchi/engine/plugins.rb +2 -0
- data/lib/samagotchi/engine/side_requests.rb +72 -13
- data/lib/samagotchi/engine/toolkit.rb +1 -1
- data/lib/samagotchi/engine/turn_helpers.rb +1 -1
- data/lib/samagotchi/engine.rb +11 -8
- data/lib/samagotchi/engine_snapshots.rb +16 -3
- data/lib/samagotchi/events.rb +1 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
- data/lib/samagotchi/hooks.rb +3 -1
- data/lib/samagotchi/host_registry.rb +146 -16
- data/lib/samagotchi/idle_client.rb +13 -2
- data/lib/samagotchi/idle_recap.rb +33 -3
- data/lib/samagotchi/idle_target.rb +27 -7
- data/lib/samagotchi/jsonl_store.rb +13 -2
- data/lib/samagotchi/llm/adapter_common.rb +4 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +14 -2
- data/lib/samagotchi/llm/errors.rb +35 -4
- data/lib/samagotchi/llm/model_listing.rb +19 -0
- data/lib/samagotchi/llm/openai_chat.rb +8 -2
- data/lib/samagotchi/llm/prompt_cache.rb +5 -0
- data/lib/samagotchi/model_binding.rb +46 -4
- data/lib/samagotchi/model_catalog.rb +68 -8
- data/lib/samagotchi/model_profile.rb +35 -0
- data/lib/samagotchi/models_command.rb +8 -4
- data/lib/samagotchi/pages/launch.rb +1 -1
- data/lib/samagotchi/panes/kitty.rb +122 -0
- data/lib/samagotchi/panes.rb +185 -0
- data/lib/samagotchi/peer_announce.rb +245 -0
- data/lib/samagotchi/peer_cold.rb +92 -0
- data/lib/samagotchi/peer_command.rb +121 -20
- data/lib/samagotchi/peer_continue.rb +42 -16
- data/lib/samagotchi/peer_exchanges.rb +7 -0
- data/lib/samagotchi/peer_mailbox.rb +50 -10
- data/lib/samagotchi/peer_pair_here.rb +149 -0
- data/lib/samagotchi/peer_presence.rb +434 -0
- data/lib/samagotchi/peer_receipts.rb +33 -6
- data/lib/samagotchi/peer_say.rb +46 -21
- data/lib/samagotchi/peer_wait_expiry.rb +47 -0
- data/lib/samagotchi/plugin/api.rb +13 -1
- data/lib/samagotchi/plugin/card.rb +68 -11
- data/lib/samagotchi/plugin/context.rb +33 -2
- data/lib/samagotchi/plugin/panes.rb +54 -0
- data/lib/samagotchi/plugin/peer.rb +170 -63
- data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
- data/lib/samagotchi/plugin/peer_start.rb +301 -0
- data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
- data/lib/samagotchi/plugin/peers.rb +89 -9
- data/lib/samagotchi/recap_setup.rb +40 -3
- data/lib/samagotchi/self_report.rb +25 -6
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/served_lookup.rb +225 -0
- data/lib/samagotchi/served_ref.rb +41 -0
- data/lib/samagotchi/session_access/contract.rb +6 -0
- data/lib/samagotchi/session_access/local.rb +149 -40
- data/lib/samagotchi/session_access/remote.rb +4 -4
- data/lib/samagotchi/session_access/remote_peers.rb +8 -5
- data/lib/samagotchi/session_chain/links.rb +8 -3
- data/lib/samagotchi/session_commands.rb +40 -8
- data/lib/samagotchi/session_continue.rb +11 -6
- data/lib/samagotchi/session_lifecycle/allowed.rb +4 -2
- data/lib/samagotchi/session_manager/delivery.rb +5 -0
- data/lib/samagotchi/session_manager/lifecycle.rb +58 -7
- data/lib/samagotchi/session_manager/removal.rb +3 -0
- data/lib/samagotchi/session_manager/spawn.rb +6 -1
- data/lib/samagotchi/session_model_settings.rb +7 -3
- data/lib/samagotchi/session_stores.rb +25 -11
- data/lib/samagotchi/sessions_command.rb +16 -3
- data/lib/samagotchi/system_one/http_backend.rb +111 -10
- data/lib/samagotchi/system_one/runs.rb +5 -2
- data/lib/samagotchi/system_prompt.rb +11 -4
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +26 -12
- data/lib/samagotchi/terminal_ui/status_row.rb +20 -3
- data/lib/samagotchi/terminal_ui.rb +1 -1
- data/lib/samagotchi/testing/boundary.rb +0 -1
- data/lib/samagotchi/testing/bundle.rb +6 -2
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +3 -0
- data/lib/samagotchi/turn_endings.rb +6 -2
- data/lib/samagotchi/user_typed.rb +29 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +8 -20
- data/lib/samagotchi/web/participant_review.rb +1 -1
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +40 -15
- data/lib/samagotchi/web/public/agent_rows.js +206 -0
- data/lib/samagotchi/web/public/agent_runs.js +595 -0
- data/lib/samagotchi/web/public/answer_fold.js +41 -44
- data/lib/samagotchi/web/public/app.js +4 -4
- data/lib/samagotchi/web/public/block_fix.js +1 -1
- data/lib/samagotchi/web/public/card.js +26 -10
- data/lib/samagotchi/web/public/cards_place.js +6 -4
- data/lib/samagotchi/web/public/context_chips.js +24 -2
- data/lib/samagotchi/web/public/copy.js +8 -2
- data/lib/samagotchi/web/public/data.js +2 -1
- data/lib/samagotchi/web/public/format.js +9 -1
- data/lib/samagotchi/web/public/history_reveal.js +5 -2
- data/lib/samagotchi/web/public/index.html +56 -2
- data/lib/samagotchi/web/public/info_bar_model.js +1 -1
- data/lib/samagotchi/web/public/model_pick.js +18 -5
- data/lib/samagotchi/web/public/model_picker.js +1 -1
- data/lib/samagotchi/web/public/page_view.js +1 -1
- data/lib/samagotchi/web/public/pages.css +1 -7
- data/lib/samagotchi/web/public/peer_chip.js +15 -5
- data/lib/samagotchi/web/public/peer_data.js +4 -2
- data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
- data/lib/samagotchi/web/public/peer_note.js +4 -2
- data/lib/samagotchi/web/public/peer_row.js +37 -0
- data/lib/samagotchi/web/public/peer_wait.js +36 -0
- data/lib/samagotchi/web/public/quiet_notes.js +12 -1
- data/lib/samagotchi/web/public/revise_stack.js +5 -2
- data/lib/samagotchi/web/public/send_turn.js +11 -3
- data/lib/samagotchi/web/public/served_ref.js +9 -0
- data/lib/samagotchi/web/public/session_fields.js +1 -0
- data/lib/samagotchi/web/public/session_state.js +4 -0
- data/lib/samagotchi/web/public/stage_missing.js +23 -6
- data/lib/samagotchi/web/public/stream_handlers.js +16 -0
- data/lib/samagotchi/web/public/turn_block.js +4 -0
- data/lib/samagotchi/web/public/version_notices.js +7 -1
- data/lib/samagotchi/web/session_control.rb +19 -10
- data/lib/samagotchi/web/session_show.rb +20 -0
- data/lib/samagotchi/web/update_ops.rb +2 -2
- data/lib/samagotchi/worker/command_queue.rb +3 -0
- data/lib/samagotchi/worker/mail.rb +24 -1
- data/lib/samagotchi/worker.rb +8 -5
- data/lib/samagotchi/worker_inbound/mail.rb +34 -6
- data/lib/samagotchi/worker_inbound.rb +11 -3
- data/lib/samagotchi/worker_link.rb +13 -1
- data/lib/samagotchi/worker_wakes.rb +33 -5
- metadata +22 -2
- data/lib/samagotchi/web/public/pages_group.js +0 -67
|
@@ -35,7 +35,9 @@ module Samagotchi
|
|
|
35
35
|
# @param on_usage [#call] (usage) records a recap request's SideUsage
|
|
36
36
|
# (Engine#note_side_usage)
|
|
37
37
|
# @param host_registry [HostRegistry]
|
|
38
|
-
# @param session_target [#call]
|
|
38
|
+
# @param session_target [#call] (max_age: seconds, optional; nil: no
|
|
39
|
+
# listing) → the session's current model as a recap target (a served
|
|
40
|
+
# one listed unless that is max_age old)
|
|
39
41
|
# @param session_id [#call] → String, nil
|
|
40
42
|
# @param state_dir [#call] → String
|
|
41
43
|
# @param session_config [#call] → the session's own core settings
|
|
@@ -63,8 +65,20 @@ module Samagotchi
|
|
|
63
65
|
model = string_config(kwarg_config, :model) || registry_string("recap.model")
|
|
64
66
|
|
|
65
67
|
settings = { model: model, host_ref: host_ref, base_url: base_url, host_registry: host_registry }
|
|
68
|
+
retarget = nil
|
|
66
69
|
target = if base_url.nil? && host_ref.nil? && model.nil?
|
|
67
|
-
|
|
70
|
+
# A not-served retry lists the session's served host again.
|
|
71
|
+
retarget = -> { session_target.call(max_age: 0) }
|
|
72
|
+
# The attempt (on the idle scheduler's thread) lists nothing:
|
|
73
|
+
# the id last known, and the retry lists after a swap.
|
|
74
|
+
-> { session_target.call(max_age: nil) }
|
|
75
|
+
elsif base_url.nil? && IdleTarget.served_model?(model, host_registry)
|
|
76
|
+
# recap.model: …served: checked once (the warnings), then
|
|
77
|
+
# listed and resolved at each attempt.
|
|
78
|
+
return nil unless fixed_target(settings)
|
|
79
|
+
|
|
80
|
+
retarget = -> { served_target(settings, list: true) }
|
|
81
|
+
-> { served_target(settings, list: false) }
|
|
68
82
|
elsif !IdleTarget.host_named?(**settings)
|
|
69
83
|
# A model naming no host goes where a bare --model goes
|
|
70
84
|
# (HostRegistry#host_for_model: the default host unless
|
|
@@ -88,10 +102,33 @@ module Samagotchi
|
|
|
88
102
|
sentences: recap_sentences(string_config(kwarg_config, :sentences) || registry_string("recap.sentences", session: session)),
|
|
89
103
|
store: RecapStore.new(session_id_lookup: -> { session_id.call }, state_dir_lookup: -> { state_dir.call }),
|
|
90
104
|
session_id: -> { session_id.call },
|
|
91
|
-
on_usage: on_usage
|
|
105
|
+
on_usage: on_usage,
|
|
106
|
+
retarget: retarget
|
|
92
107
|
)
|
|
93
108
|
end
|
|
94
109
|
|
|
110
|
+
# A served recap model's target, resolved (IdleTarget.resolve) from the
|
|
111
|
+
# id last known; with +list+ (the not-served retry, on the summarize
|
|
112
|
+
# thread) its host is listed first. An attempt never lists: it runs on
|
|
113
|
+
# the idle scheduler's thread, which never waits on a request.
|
|
114
|
+
# @raise [IdleClient::SummarizeError] the host lists none or several
|
|
115
|
+
# models (no recap this time)
|
|
116
|
+
def self.served_target(settings, list:)
|
|
117
|
+
registry = settings[:host_registry]
|
|
118
|
+
refusal = if list
|
|
119
|
+
registry.refresh_served!(settings[:model], max_age: 0, host: settings[:host_ref])&.refusal
|
|
120
|
+
else
|
|
121
|
+
registry.cached_served_refusal(settings[:model], host: settings[:host_ref])
|
|
122
|
+
end
|
|
123
|
+
if refusal
|
|
124
|
+
Log.info(:recap, "served_refused", msg: refusal)
|
|
125
|
+
raise IdleClient::SummarizeError, refusal
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
IdleTarget.resolve(**settings)
|
|
129
|
+
end
|
|
130
|
+
private_class_method :served_target
|
|
131
|
+
|
|
95
132
|
# The recap's pinned host and model (IdleTarget.resolve), or nil after
|
|
96
133
|
# a warning when they can't be resolved (recap stays off).
|
|
97
134
|
# @return [IdleTarget, nil]
|
|
@@ -209,13 +209,30 @@ module Samagotchi
|
|
|
209
209
|
# "splash:x (this session abcd1234; default main:y)", "main:y (this
|
|
210
210
|
# session abcd1234, the default)", or outside a session "main:y (default)".
|
|
211
211
|
def model_label(model, default, env)
|
|
212
|
-
|
|
212
|
+
shown = served_label(model, env)
|
|
213
|
+
return "#{shown} (default)" unless session_model(env)
|
|
213
214
|
|
|
214
215
|
id = env[PARENT_SESSION_ENV].to_s
|
|
215
216
|
session = id.empty? || id == "chi" ? "this session" : "this session #{id[0, 8]}"
|
|
216
|
-
return "#{
|
|
217
|
+
return "#{shown} (#{session}, the default)" if default && same_model?(model, default, env)
|
|
217
218
|
|
|
218
|
-
"#{
|
|
219
|
+
"#{shown} (#{session}; default #{default || "not configured"})"
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# A served model (main:served) with what its host serves now, from one
|
|
223
|
+
# bounded listing ("main:served → q8"), or why it can't run ("main:served
|
|
224
|
+
# (host main lists 3 models (a, b, c))"); any other model as it is. The
|
|
225
|
+
# listing goes first, so the rows after it (model key, host, served
|
|
226
|
+
# model) read the id it found.
|
|
227
|
+
def served_label(model, env)
|
|
228
|
+
now = registry(env).refresh_served!(model, max_age: 0)
|
|
229
|
+
return model unless now
|
|
230
|
+
return "#{model} → #{now.id}" if now.id
|
|
231
|
+
return "#{model} (#{now.refusal.split(";").first})" if now.refusal
|
|
232
|
+
|
|
233
|
+
"#{model} (unknown: #{now.error&.message.to_s[0, 80]})"
|
|
234
|
+
rescue StandardError => e
|
|
235
|
+
"#{model} (unknown: #{e.class})"
|
|
219
236
|
end
|
|
220
237
|
|
|
221
238
|
def same_model?(one, other, env)
|
|
@@ -230,7 +247,7 @@ module Samagotchi
|
|
|
230
247
|
# engine keys memory_write current_model_only): which `<name>.<key>.md`
|
|
231
248
|
# overlays are this model's.
|
|
232
249
|
def model_key_for(model, env)
|
|
233
|
-
ModelOverlay.key_for(registry(env).
|
|
250
|
+
ModelOverlay.key_for(registry(env).sent_model_id(model)) || "-"
|
|
234
251
|
rescue StandardError
|
|
235
252
|
ModelOverlay.key_for(model) || "-"
|
|
236
253
|
end
|
|
@@ -241,7 +258,7 @@ module Samagotchi
|
|
|
241
258
|
# without any. Read from the memories dirs now: a session's prompt
|
|
242
259
|
# takes them at its next build (/stats names the ones it carries).
|
|
243
260
|
def model_notes_for(model, env)
|
|
244
|
-
bare = registry(env).
|
|
261
|
+
bare = registry(env).sent_model_id(model)
|
|
245
262
|
key = ModelOverlay.key_for(bare)
|
|
246
263
|
notes = ModelNotes.for(name: bare, key: key, fallback_key: fallback_model_key(model, key, env),
|
|
247
264
|
muted: session_muted(env), env: env, warn: false)
|
|
@@ -258,7 +275,9 @@ module Samagotchi
|
|
|
258
275
|
def fallback_model_key(model, key, env)
|
|
259
276
|
Degrade.call(:model, "fallback_key_unresolved") do
|
|
260
277
|
typed = session_typed_model(env) || model
|
|
261
|
-
|
|
278
|
+
typed_name = registry(env).parse_qualified_model(typed).last
|
|
279
|
+
# A typed `served` keys nothing of its own (ModelBinding#sync_model_key!).
|
|
280
|
+
typed_key = ServedRef.word?(typed_name) ? nil : ModelOverlay.key_for(typed_name)
|
|
262
281
|
typed_key == key ? nil : typed_key
|
|
263
282
|
end
|
|
264
283
|
end
|
|
@@ -63,11 +63,11 @@ module Samagotchi
|
|
|
63
63
|
chi send --wait [--timeout S] [--format json] [-m TEXT] [--image PATH]... (--new | ID)
|
|
64
64
|
chi send --queue --wait [--timeout S] [--format json] [-m TEXT] ID
|
|
65
65
|
chi send --wait [--timeout S] [--format json] [--turn T] ID
|
|
66
|
-
Sends a message to each session, as if typed in it: a turn starts,
|
|
67
|
-
|
|
68
|
-
worker
|
|
69
|
-
chain gets it (OLD → NEW (continued)), and the old
|
|
70
|
-
archived; one being continued
|
|
66
|
+
Sends a message to each session, as if typed in it: a turn starts, or a
|
|
67
|
+
running one takes it at its next step. Yours starts a stopped session's
|
|
68
|
+
worker (an agent's doesn't: see --as). A session that was continued: the
|
|
69
|
+
newest link of its chain gets it (OLD → NEW (continued)), and the old
|
|
70
|
+
link stays archived; one being continued now waits a moment for its new
|
|
71
71
|
link. A chain's first id is a stable address for it.
|
|
72
72
|
A session command (/model x, !cmd) runs as the command; --wait
|
|
73
73
|
then has no reply to wait for (exit 0, status command).
|
|
@@ -858,7 +858,7 @@ module Samagotchi
|
|
|
858
858
|
return deliver(again, prompt, deadline: deadline, &before) if again
|
|
859
859
|
end
|
|
860
860
|
unless result.accepted?
|
|
861
|
-
@info.puts("#{short} #{result.refusal_text}")
|
|
861
|
+
@info.puts("#{short} #{result.refusal_text}#{"; ask your user to start it: #{@access.invocation.start_stopped(id)}" if result.stopped?}")
|
|
862
862
|
return false
|
|
863
863
|
end
|
|
864
864
|
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "failure_line"
|
|
4
|
+
require_relative "log"
|
|
5
|
+
require_relative "model_list_store"
|
|
6
|
+
require_relative "served_ref"
|
|
7
|
+
|
|
8
|
+
module Samagotchi
|
|
9
|
+
# What each host serves now, for the model value `served` (ServedRef): the
|
|
10
|
+
# one id the host's own listing has. One per HostRegistry, keyed by host
|
|
11
|
+
# name, under a mutex. Two reads:
|
|
12
|
+
#
|
|
13
|
+
# - #cached: no request, never raises. This lookup's last listing, else
|
|
14
|
+
# the host adapter's fresh model list (LLM::ModelListing#models_if_fresh),
|
|
15
|
+
# else the saved list (ModelListStore) when it has exactly one id; nil
|
|
16
|
+
# when nothing is known (the readers then send the word `served`). What
|
|
17
|
+
# every Bridge read, snapshot and session= sees.
|
|
18
|
+
# - #refresh!: lists the host (HostRegistry#probe_model_ids: short
|
|
19
|
+
# timeouts, no retry) when the last listing is older than +max_age+,
|
|
20
|
+
# primes the adapter's list, and saves to ModelListStore when the ids
|
|
21
|
+
# changed or the saved entry is a day old. What a turn's start calls.
|
|
22
|
+
#
|
|
23
|
+
# Declared ids (hosts.<name>.models) never count.
|
|
24
|
+
class ServedLookup
|
|
25
|
+
# A turn that can't run on `served`: the host lists none or several
|
|
26
|
+
# models. Its message is the line the turn fails with.
|
|
27
|
+
class Refused < StandardError
|
|
28
|
+
include UserError
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# A refresh's answer: id, the one model the host serves (nil when
|
|
32
|
+
# unknown or refused); refusal, the line a turn fails with when the host
|
|
33
|
+
# lists none or several; error, the listing failure (the last known id
|
|
34
|
+
# is kept with it).
|
|
35
|
+
Resolution = Data.define(:id, :refusal, :error) do
|
|
36
|
+
def initialize(id: nil, refusal: nil, error: nil) = super
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Seconds a saved-list read (hit or miss) is kept: chi web's polls don't
|
|
40
|
+
# re-read model_lists.json per session per poll.
|
|
41
|
+
SAVED_TTL = 10
|
|
42
|
+
# Seconds a failed listing is remembered for a caller that takes an old
|
|
43
|
+
# listing (max_age > 0: System 1, side asks): a host that is down fails
|
|
44
|
+
# those fast, not one bounded listing each. A turn's start (max_age 0)
|
|
45
|
+
# always lists.
|
|
46
|
+
FAILED_TTL = 30
|
|
47
|
+
# Seconds after which an unchanged listing still rewrites the saved
|
|
48
|
+
# entry (its 7-day TTL runs from its at).
|
|
49
|
+
SAVE_REFRESH = 24 * 60 * 60
|
|
50
|
+
# The most ids a refusal names.
|
|
51
|
+
NAMES_SHOWN = 5
|
|
52
|
+
|
|
53
|
+
# The line a turn fails with when +ids+ isn't exactly one id, else nil.
|
|
54
|
+
# @return [String, nil]
|
|
55
|
+
def self.refusal(host, ids)
|
|
56
|
+
return "host #{host} lists no model; served needs exactly one" if ids.empty?
|
|
57
|
+
return nil if ids.size == 1
|
|
58
|
+
|
|
59
|
+
names = ids.first(NAMES_SHOWN).join(", ")
|
|
60
|
+
names += ", …" if ids.size > NAMES_SHOWN
|
|
61
|
+
"host #{host} lists #{ids.size} models (#{names}); served needs exactly one: pick one with /model"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The ids a listing names (LLM::ModelInfo list): stripped, no blanks,
|
|
65
|
+
# each once. What "exactly one" counts, here and on the pickers.
|
|
66
|
+
# @return [Array<String>]
|
|
67
|
+
def self.ids_of(models) = Array(models).map { |model| model.id.to_s.strip }.reject(&:empty?).uniq
|
|
68
|
+
|
|
69
|
+
# The one id +models+ names, nil for none or several: the id a served
|
|
70
|
+
# ref on that host resolves to (the pickers' served row).
|
|
71
|
+
# @return [String, nil]
|
|
72
|
+
def self.one_id(models)
|
|
73
|
+
ids = ids_of(models)
|
|
74
|
+
ids.size == 1 ? ids.first : nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @param lister [#call] (entry, timeout: seconds when capped,
|
|
78
|
+
# cancel_controller: when given) → the
|
|
79
|
+
# LLM::ModelInfo list the host lists now; raises on failure
|
|
80
|
+
# @param adapter [#call] (entry) → the host's chat adapter, or nil (no request)
|
|
81
|
+
# @param env [Hash] what ModelListStore is read and written with
|
|
82
|
+
# @param clock [#call] monotonic seconds
|
|
83
|
+
# @param now [#call] epoch seconds (the saved list's age)
|
|
84
|
+
def initialize(lister:, adapter:, env: ENV, clock: nil, now: nil)
|
|
85
|
+
@lister = lister
|
|
86
|
+
@adapter = adapter
|
|
87
|
+
@env = env
|
|
88
|
+
@clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
89
|
+
@now = now || -> { Time.now.to_i }
|
|
90
|
+
@mutex = Mutex.new
|
|
91
|
+
# host name => { ids:, at: } (this process's listings)
|
|
92
|
+
@listed = {}
|
|
93
|
+
# host name => [id or nil, at] (saved-list reads)
|
|
94
|
+
@saved = {}
|
|
95
|
+
# host name => [error, at] (the last listing that failed)
|
|
96
|
+
@failed = {}
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The id +entry+'s host serves as far as known, with no request; nil
|
|
100
|
+
# when nothing is known or the last listing was refused.
|
|
101
|
+
# @return [String, nil]
|
|
102
|
+
def cached(entry)
|
|
103
|
+
listed = @mutex.synchronize { @listed[entry.name] }
|
|
104
|
+
return single(listed[:ids]) if listed
|
|
105
|
+
|
|
106
|
+
fresh = fresh_adapter_ids(entry)
|
|
107
|
+
return single(fresh) if fresh
|
|
108
|
+
|
|
109
|
+
saved_id(entry)
|
|
110
|
+
rescue StandardError => e
|
|
111
|
+
Log.debug(:model, "served_cached_failed", host: entry&.name, error: e.class.name, msg: e.message.to_s[0, 200])
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Lists +entry+'s host unless this lookup listed it less than +max_age+
|
|
116
|
+
# seconds ago (nil or 0: always).
|
|
117
|
+
# A listing that answered is the answer, whatever saving it does
|
|
118
|
+
# (#save logs its own failure). Two listings at once: the one started
|
|
119
|
+
# later is kept, an older one never overwrites it.
|
|
120
|
+
# +timeout+ (seconds), when given, caps the listing's own timeouts
|
|
121
|
+
# (System 1's ask spends its deadline on it).
|
|
122
|
+
# @return [Resolution]
|
|
123
|
+
# +cancel_controller+, when given, cuts the listing (a judgment's Stop).
|
|
124
|
+
def refresh!(entry, max_age: 0, timeout: nil, cancel_controller: nil)
|
|
125
|
+
listed, failed = @mutex.synchronize { [@listed[entry.name], @failed[entry.name]] }
|
|
126
|
+
old_ok = max_age.to_f.positive?
|
|
127
|
+
return resolution(entry, listed[:ids]) if listed && old_ok && (@clock.call - listed[:at]) < max_age
|
|
128
|
+
return Resolution.new(id: previous_id(listed) || cached(entry), error: failed.first) if failed && old_ok && (@clock.call - failed.last) < FAILED_TTL
|
|
129
|
+
|
|
130
|
+
started = @clock.call
|
|
131
|
+
begin
|
|
132
|
+
options = { timeout: timeout, cancel_controller: cancel_controller }.compact
|
|
133
|
+
models = Array(options.empty? ? @lister.call(entry) : @lister.call(entry, **options))
|
|
134
|
+
rescue StandardError => e
|
|
135
|
+
Log.warn(:model, "served_list_failed", host: entry.name, error: e.class.name, msg: e.message.to_s[0, 200])
|
|
136
|
+
# A Stop says nothing about the host.
|
|
137
|
+
@mutex.synchronize { @failed[entry.name] = [e, @clock.call] } unless e.class.name.end_with?("RequestCancelled")
|
|
138
|
+
return Resolution.new(id: previous_id(listed) || cached(entry), error: e)
|
|
139
|
+
end
|
|
140
|
+
@mutex.synchronize { @failed.delete(entry.name) }
|
|
141
|
+
ids = self.class.ids_of(models)
|
|
142
|
+
kept = record(entry, ids, started)
|
|
143
|
+
return resolution(entry, kept) unless kept.equal?(ids)
|
|
144
|
+
|
|
145
|
+
Log.info(:model, "served_listed", host: entry.name, ids: ids.first(NAMES_SHOWN).join(","), count: ids.size) if ids != listed&.fetch(:ids, nil)
|
|
146
|
+
save(entry, models, ids)
|
|
147
|
+
resolution(entry, ids)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The line a turn would fail with because this lookup's last listing of
|
|
151
|
+
# +entry+'s host named none or several models, else nil (nothing listed
|
|
152
|
+
# yet, or one id). No request: what System 1's #target reads.
|
|
153
|
+
# @return [String, nil]
|
|
154
|
+
def cached_refusal(entry)
|
|
155
|
+
listed = @mutex.synchronize { @listed[entry.name] }
|
|
156
|
+
listed && self.class.refusal(entry.name, listed[:ids])
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Forget +entry+'s listing: the next #cached reads the adapter or the
|
|
160
|
+
# saved list, the next #refresh! lists.
|
|
161
|
+
def invalidate!(entry)
|
|
162
|
+
@mutex.synchronize do
|
|
163
|
+
@listed.delete(entry.name)
|
|
164
|
+
@saved.delete(entry.name)
|
|
165
|
+
@failed.delete(entry.name)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
private
|
|
170
|
+
|
|
171
|
+
def single(ids) = ids&.size == 1 ? ids.first : nil
|
|
172
|
+
|
|
173
|
+
# Keeps +ids+ as +entry+'s listing unless one started after +started+ is
|
|
174
|
+
# there already. @return [Array<String>] the ids kept (+ids+ itself when
|
|
175
|
+
# these were)
|
|
176
|
+
def record(entry, ids, started)
|
|
177
|
+
@mutex.synchronize do
|
|
178
|
+
current = @listed[entry.name]
|
|
179
|
+
next current[:ids] if current && current[:at] > started
|
|
180
|
+
|
|
181
|
+
@listed[entry.name] = { ids: ids, at: started }
|
|
182
|
+
ids
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def previous_id(listed) = listed && single(listed[:ids])
|
|
187
|
+
|
|
188
|
+
def resolution(entry, ids)
|
|
189
|
+
refusal = self.class.refusal(entry.name, ids)
|
|
190
|
+
Resolution.new(id: refusal ? nil : ids.first, refusal: refusal)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# The adapter's list when it is fresh, else nil (never a GET).
|
|
194
|
+
def fresh_adapter_ids(entry)
|
|
195
|
+
adapter = @adapter.call(entry)
|
|
196
|
+
models = adapter.respond_to?(:models_if_fresh) ? adapter.models_if_fresh : nil
|
|
197
|
+
models&.map { |model| model.id.to_s }&.reject(&:empty?)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def saved_id(entry)
|
|
201
|
+
kept = @mutex.synchronize { @saved[entry.name] }
|
|
202
|
+
return kept.first if kept && (@clock.call - kept.last) < SAVED_TTL
|
|
203
|
+
|
|
204
|
+
list = ModelListStore.find(entry.name, env: @env)
|
|
205
|
+
id = list && !list.stale?(now: @now.call) ? single(list.ids) : nil
|
|
206
|
+
@mutex.synchronize { @saved[entry.name] = [id, @clock.call] }
|
|
207
|
+
id
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# The adapter's list follows (the window and vision lookups see the new
|
|
211
|
+
# id at once), and the saved list when the ids changed or it is a day
|
|
212
|
+
# old. A host that lists nothing keeps its saved ids (ModelListStore).
|
|
213
|
+
def save(entry, models, ids)
|
|
214
|
+
adapter = @adapter.call(entry)
|
|
215
|
+
adapter.store_models!(models) if adapter.respond_to?(:store_models!)
|
|
216
|
+
saved = ModelListStore.find(entry.name, env: @env)
|
|
217
|
+
return if saved && saved.ids == ids && saved.age(now: @now.call) < SAVE_REFRESH
|
|
218
|
+
|
|
219
|
+
ModelListStore.save(entry.name, ids, at: @now.call, env: @env)
|
|
220
|
+
@mutex.synchronize { @saved.delete(entry.name) }
|
|
221
|
+
rescue StandardError => e
|
|
222
|
+
Log.warn(:model, "served_save_failed", host: entry.name, error: e.class.name, msg: e.message.to_s[0, 200])
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "model_ref"
|
|
4
|
+
|
|
5
|
+
module Samagotchi
|
|
6
|
+
# The model value `served`: "whatever this host serves now" (main:served;
|
|
7
|
+
# a bare `served` is the default host's). ModelRef stays pure, so `served`
|
|
8
|
+
# is just an id there; callers ask this module whether a ref is one.
|
|
9
|
+
# ServedLookup resolves it from the host's own /v1/models (the one id it
|
|
10
|
+
# lists), never from hosts.<name>.models. The word is reserved (any case):
|
|
11
|
+
# an alias named `served` is ignored (ConfigFile.model_aliases).
|
|
12
|
+
module ServedRef
|
|
13
|
+
WORD = "served"
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# Whether +ref+ (a ModelRef) is a served ref: its id, the alias applied,
|
|
18
|
+
# is `served` in any case.
|
|
19
|
+
def served?(ref) = !ref.nil? && word?(ref.id)
|
|
20
|
+
|
|
21
|
+
# Whether +name+ is the word itself.
|
|
22
|
+
def word?(name) = name.to_s.strip.casecmp?(WORD)
|
|
23
|
+
|
|
24
|
+
# Whether +ref+, a stored ref (ModelRef#ref: its alias applied, "host:id"
|
|
25
|
+
# when it names a host), is a served one: `served` or `<host>:served`.
|
|
26
|
+
# No hosts needed: what the UIs ask of a session's model_name.
|
|
27
|
+
def stored?(ref)
|
|
28
|
+
text = ref.to_s.strip
|
|
29
|
+
word?(text) || text.match?(/\A[^:]+:#{WORD}\z/i)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Whether +name+ is written as a served ref (`served`, `main:served`)
|
|
33
|
+
# before any alias: what HostRegistry#lookup_names leaves out (an alias
|
|
34
|
+
# of main:served stays: a models: entry keyed by it applies).
|
|
35
|
+
# @param hosts [Hash] configured hosts by name
|
|
36
|
+
def literal?(name, hosts:)
|
|
37
|
+
_, id = ModelRef.split(name, hosts: hosts)
|
|
38
|
+
word?(id)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -156,6 +156,12 @@ module Samagotchi
|
|
|
156
156
|
"peer pair" => SUPPORTED,
|
|
157
157
|
"peer pair --address" => SUPPORTED,
|
|
158
158
|
"peer pair --from" => SUPPORTED,
|
|
159
|
+
"peer pair --peer-session" => SUPPORTED,
|
|
160
|
+
# No id: this machine's marks and live sessions (PeerPairHere); the
|
|
161
|
+
# pair script says "pair with an id over a remote" itself.
|
|
162
|
+
"peer pair --here" => LOCAL_ONLY,
|
|
163
|
+
# The peers' start hooks (Claude Code's, Codex's) pair on this machine (PeerPresence).
|
|
164
|
+
"peer announce" => LOCAL_ONLY,
|
|
159
165
|
"peer unpair" => SUPPORTED,
|
|
160
166
|
"peer say" => SUPPORTED,
|
|
161
167
|
"peer say --wake" => SUPPORTED,
|