samagotchi 0.192.0 → 0.193.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 +52 -1
- data/docs/architecture.md +3 -3
- data/docs/bundles.md +45 -28
- data/docs/cli.md +22 -8
- data/docs/configuration.md +4 -3
- data/docs/context.md +8 -5
- data/docs/guardrails.md +9 -4
- data/docs/internals/bridge.md +3 -1
- data/docs/internals/bundles.md +2 -2
- data/docs/internals/pages.md +4 -4
- data/docs/internals/participants.md +5 -3
- data/docs/internals/peers.md +75 -22
- data/docs/internals/prompt-caching.md +2 -1
- data/docs/internals/remote-sessions.md +6 -0
- data/docs/internals/session-lifecycle.md +3 -2
- data/docs/internals/web-frontend.md +91 -19
- data/docs/internals/working-set.md +5 -1
- data/docs/peers.md +71 -22
- data/docs/plugins.md +7 -4
- data/docs/releasing.md +30 -2
- data/docs/scheduling.md +3 -2
- data/docs/sessions.md +1 -1
- data/docs/testing.md +35 -3
- data/docs/thin-clients.md +27 -11
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +2 -2
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +33 -17
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +2 -2
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +34 -17
- data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
- 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/compat.rb +8 -1
- data/lib/samagotchi/config.rb +4 -0
- data/lib/samagotchi/continue_offer.rb +13 -1
- data/lib/samagotchi/delivery.rb +2 -2
- data/lib/samagotchi/engine/models.rb +17 -0
- data/lib/samagotchi/engine/plugins.rb +2 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
- data/lib/samagotchi/jsonl_store.rb +13 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +11 -0
- data/lib/samagotchi/llm/openai_chat.rb +5 -0
- data/lib/samagotchi/llm/prompt_cache.rb +5 -0
- data/lib/samagotchi/pages/launch.rb +1 -1
- data/lib/samagotchi/peer_cold.rb +92 -0
- data/lib/samagotchi/peer_command.rb +24 -9
- data/lib/samagotchi/peer_continue.rb +32 -16
- data/lib/samagotchi/peer_exchanges.rb +7 -0
- data/lib/samagotchi/peer_mailbox.rb +21 -4
- data/lib/samagotchi/peer_receipts.rb +33 -6
- data/lib/samagotchi/peer_say.rb +34 -16
- data/lib/samagotchi/plugin/api.rb +2 -1
- data/lib/samagotchi/plugin/context.rb +13 -1
- data/lib/samagotchi/plugin/peer.rb +118 -53
- data/lib/samagotchi/plugin/peers.rb +13 -6
- data/lib/samagotchi/session_access/local.rb +98 -37
- data/lib/samagotchi/session_access/remote_peers.rb +3 -3
- data/lib/samagotchi/session_chain/links.rb +8 -3
- data/lib/samagotchi/session_continue.rb +7 -6
- data/lib/samagotchi/session_manager/spawn.rb +6 -1
- data/lib/samagotchi/session_stores.rb +16 -11
- data/lib/samagotchi/system_one/runs.rb +5 -2
- data/lib/samagotchi/testing/boundary.rb +0 -1
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +3 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +10 -5
- 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 +3 -3
- data/lib/samagotchi/web/public/block_fix.js +1 -1
- data/lib/samagotchi/web/public/cards_place.js +6 -4
- data/lib/samagotchi/web/public/history_reveal.js +5 -2
- data/lib/samagotchi/web/public/index.html +38 -2
- 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_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/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/turn_block.js +4 -0
- data/lib/samagotchi/worker/mail.rb +11 -1
- data/lib/samagotchi/worker.rb +7 -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 +5 -2
- data/lib/samagotchi/web/public/pages_group.js +0 -67
|
@@ -10,6 +10,7 @@ require "time"
|
|
|
10
10
|
require_relative "../config"
|
|
11
11
|
require_relative "../paths"
|
|
12
12
|
require_relative "../session"
|
|
13
|
+
require_relative "../session_paths"
|
|
13
14
|
require_relative "../peer_exchanges"
|
|
14
15
|
require_relative "../peer_mailbox"
|
|
15
16
|
require_relative "../peer_note"
|
|
@@ -54,11 +55,10 @@ module Samagotchi
|
|
|
54
55
|
# quote. Such a text is titled by its first quoted line, not the quote's
|
|
55
56
|
# `From your thinking:` label.
|
|
56
57
|
#
|
|
57
|
-
# A continue
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
#
|
|
61
|
-
# id; its chi-peer skill knows how it paired.
|
|
58
|
+
# A continue copies the pairing to the new link (PeerContinue): the
|
|
59
|
+
# peer does nothing. chi's next send names the new id in its header, and
|
|
60
|
+
# a say to the old id goes to the new link (SessionAccess::Local
|
|
61
|
+
# #peer_say), its line naming it.
|
|
62
62
|
#
|
|
63
63
|
# A subclass (the bundle's Plugin) sets the strings, as constants: PEER
|
|
64
64
|
# (its mailbox name: "claude"), BUNDLE, NAME (the peer, as the model and
|
|
@@ -86,23 +86,37 @@ module Samagotchi
|
|
|
86
86
|
PAIR_REMOTE_PLACEHOLDER = "__CHI_PEER_PAIR_REMOTE__"
|
|
87
87
|
PAYLOADS_PLACEHOLDER = "__CHI_PEER_PAYLOADS__"
|
|
88
88
|
DIFF_LINES = 20
|
|
89
|
-
# Under the session's data dir: the moved message went (or was tried).
|
|
90
|
-
MOVED_MARKER = "moved-posted"
|
|
91
89
|
# The <peer>_read tool's page, at most (its output's cap, as context_read's).
|
|
92
90
|
READ_DEFAULT_CHARS = 10_000
|
|
93
91
|
READ_MIN_CHARS = 2_000
|
|
94
92
|
# The label line a web quote starts with (annotations.js sourceLabel).
|
|
95
93
|
QUOTE_LABEL = /\AFrom (?:your thinking|your earlier step|the .+ call|my earlier message):\z/
|
|
96
94
|
|
|
95
|
+
# A pairing the model sends to after this many minutes with no
|
|
96
|
+
# exchange is dormant (#quiet_check!); 0 turns the ask off.
|
|
97
|
+
QUIET_KEY = "peer.quiet_minutes"
|
|
98
|
+
QUIET_DEFAULT = 30
|
|
99
|
+
# The dormant send's question's options; Send spends (a parent agent
|
|
100
|
+
# can't answer it: Guardrails::ParentPolicy).
|
|
101
|
+
SEND = "Send"
|
|
102
|
+
DONT_SEND = "Don't send"
|
|
103
|
+
|
|
97
104
|
# A send that didn't happen; the message says why, for the model or the user.
|
|
98
105
|
class SendError < StandardError; end
|
|
99
106
|
|
|
107
|
+
# The time now (#quiet_check!'s clock): specs set it.
|
|
108
|
+
attr_writer :clock
|
|
109
|
+
|
|
100
110
|
def initialize(_settings = {})
|
|
101
111
|
@lock = Mutex.new
|
|
102
112
|
# The running turn's history before it (nil between turns), and the
|
|
103
113
|
# last completed answer this process saw.
|
|
104
114
|
@before_turn = nil
|
|
105
115
|
@last_answer = nil
|
|
116
|
+
# The user said "Don't send" to a dormant send in the running turn:
|
|
117
|
+
# its retries are refused without asking again, until the next turn.
|
|
118
|
+
@declined = false
|
|
119
|
+
@clock = -> { Time.now }
|
|
106
120
|
end
|
|
107
121
|
|
|
108
122
|
# A send's id, from what the caller gave (before the title is cut):
|
|
@@ -147,12 +161,14 @@ module Samagotchi
|
|
|
147
161
|
read_message(ctx, args)
|
|
148
162
|
end
|
|
149
163
|
chi.annotation "Confirm with #{peer::NAME}", command: "#{peer::COMMAND} validate"
|
|
150
|
-
chi.init("Telling #{peer::NAME} the session moved", quiet: true) { |ctx| tell_moved(ctx) }
|
|
151
164
|
# Its messages in the mailbox are this peer's: its notes, its wake
|
|
152
165
|
# budget and lines (peer.turns).
|
|
153
166
|
chi.peer peer::PEER
|
|
154
167
|
chi.on(:before_turn) do |event|
|
|
155
|
-
@lock.synchronize
|
|
168
|
+
@lock.synchronize do
|
|
169
|
+
@before_turn = Array(event[:messages])
|
|
170
|
+
@declined = false
|
|
171
|
+
end
|
|
156
172
|
end
|
|
157
173
|
chi.on(:after_turn) do |event|
|
|
158
174
|
answer = completed_answer(event[:messages]) if event[:status].to_s == "completed"
|
|
@@ -166,7 +182,9 @@ module Samagotchi
|
|
|
166
182
|
# Sends +text+ to the paired peer: the header, the `ref:` line
|
|
167
183
|
# (.send_id of the args as given and +salt+: the tool row's, so the
|
|
168
184
|
# row and the line name the same send), then the text inline or a
|
|
169
|
-
# payload file's path. Records it as the last send, with its id.
|
|
185
|
+
# payload file's path. Records it as the last send, with its id. A
|
|
186
|
+
# model's send to a dormant pairing asks the user first, or is refused
|
|
187
|
+
# (#quiet_check!); the user's own (by: "user", /<peer>) never asks.
|
|
170
188
|
# @return [String] what was sent ("sent to Claude: validate · title")
|
|
171
189
|
# @raise [SendError]
|
|
172
190
|
def deliver(ctx, kind:, title:, text:, by: "model", salt: nil)
|
|
@@ -179,6 +197,7 @@ module Samagotchi
|
|
|
179
197
|
send_id = Peer.send_id(kind, title, text, salt)
|
|
180
198
|
id = ctx.session_id or raise SendError, "this session has no id yet: send after its first turn"
|
|
181
199
|
hint = peer_hint!(ctx)
|
|
200
|
+
quiet_check!(ctx) if by == "model"
|
|
182
201
|
title = one_line(title.strip.empty? ? text.strip.lines.first : title)
|
|
183
202
|
body, where = if text.length <= INLINE_CHARS
|
|
184
203
|
[text, "inline"]
|
|
@@ -197,39 +216,6 @@ module Samagotchi
|
|
|
197
216
|
"sent to #{self.class::NAME}: #{kind} · #{title}#{" (the text in #{where})" unless where == "inline"}"
|
|
198
217
|
end
|
|
199
218
|
|
|
200
|
-
# A continue's new link whose previous link was paired with this peer
|
|
201
|
-
# (ctx.peers.left_behind, with an address): posts the moved message
|
|
202
|
-
# there, once (a marker under the session's data dir: a worker restart
|
|
203
|
-
# doesn't post again). A post that fails is logged and dropped: no
|
|
204
|
-
# retry, and the peer's next say to the old id is refused with the new
|
|
205
|
-
# one (its skill pairs again then).
|
|
206
|
-
# @return [String] what it did (the quiet init task's summary)
|
|
207
|
-
def tell_moved(ctx)
|
|
208
|
-
peer = self.class
|
|
209
|
-
previous = ctx.continues or return "not a continue"
|
|
210
|
-
id = ctx.session_id or return Session::NONE_YET
|
|
211
|
-
address = ctx.peers.left_behind(peer::PEER)&.address.to_s.strip
|
|
212
|
-
return "no pairing with #{peer::NAME} left behind" if address.empty?
|
|
213
|
-
return "told already" unless claim_moved(ctx)
|
|
214
|
-
|
|
215
|
-
check_hint!(address)
|
|
216
|
-
post(address, moved_message(previous, id))
|
|
217
|
-
"told #{peer::NAME} the session moved to #{id[0, 8]}"
|
|
218
|
-
rescue SendError, SystemCallError, ArgumentError => e
|
|
219
|
-
ctx.log.warn("moved_not_posted", error: e.message)
|
|
220
|
-
"couldn't tell #{peer::NAME} the session moved: #{e.message}"
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
# What the peer gets when +previous+ was continued as +id+: the usual
|
|
224
|
-
# header, kind moved, and how to go on. Not a literal pair command:
|
|
225
|
-
# the skill knows how its peer paired (a remote, an ssh socket).
|
|
226
|
-
def moved_message(previous, id)
|
|
227
|
-
short = previous.to_s[0, 8]
|
|
228
|
-
"chi #{short} · moved · continued as #{id}\n" \
|
|
229
|
-
"This chi session was continued as #{id}. Pair with #{id} the way you paired with #{short}, then " \
|
|
230
|
-
"answer there (chi peer say #{self.class::PEER} #{id} …)."
|
|
231
|
-
end
|
|
232
|
-
|
|
233
219
|
# The <peer>_read tool's description: how a long message is read.
|
|
234
220
|
def read_description
|
|
235
221
|
peer = self.class
|
|
@@ -272,16 +258,6 @@ module Samagotchi
|
|
|
272
258
|
|
|
273
259
|
private
|
|
274
260
|
|
|
275
|
-
# Takes this session's moved marker; false when it is there.
|
|
276
|
-
def claim_moved(ctx)
|
|
277
|
-
File.open(File.join(ctx.session_dir, MOVED_MARKER), File::WRONLY | File::CREAT | File::EXCL, 0o600) do |file|
|
|
278
|
-
file.write("#{Time.now.utc.iso8601}\n")
|
|
279
|
-
end
|
|
280
|
-
true
|
|
281
|
-
rescue Errno::EEXIST
|
|
282
|
-
false
|
|
283
|
-
end
|
|
284
|
-
|
|
285
261
|
# The transport: sends +text+ to the peer at +hint+ (the inbox's hint).
|
|
286
262
|
# @raise [SendError]
|
|
287
263
|
def post(_hint, _text) = raise(NotImplementedError, "#{self.class} must define #post")
|
|
@@ -431,6 +407,92 @@ module Samagotchi
|
|
|
431
407
|
address
|
|
432
408
|
end
|
|
433
409
|
|
|
410
|
+
# peer.quiet_minutes, 0 or more (0: off); a value that isn't an
|
|
411
|
+
# integer is the default.
|
|
412
|
+
def quiet_minutes
|
|
413
|
+
value = Integer(Config.get(QUIET_KEY), exception: false)
|
|
414
|
+
value ? [value, 0].max : QUIET_DEFAULT
|
|
415
|
+
rescue StandardError
|
|
416
|
+
QUIET_DEFAULT
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
# How long the pairing has been quiet, in seconds, when that is past
|
|
420
|
+
# peer.quiet_minutes (dormant); nil when it isn't, the setting is 0, or
|
|
421
|
+
# no exchange has a time.
|
|
422
|
+
def dormant_for(ctx)
|
|
423
|
+
quiet = quiet_minutes
|
|
424
|
+
last = last_exchange_at(ctx)
|
|
425
|
+
return nil if quiet.zero? || last.nil?
|
|
426
|
+
|
|
427
|
+
idle = @clock.call - last
|
|
428
|
+
idle > quiet * 60 ? idle : nil
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
# A model's send to a dormant pairing (#dormant_for): the peer's own
|
|
432
|
+
# session may have gone cold too, so the user decides. In a turn they
|
|
433
|
+
# started from the web or a TUI (ctx.turn_interactive?) it asks, Send
|
|
434
|
+
# or Don't send; in any other turn (a schedule's, a delegate's, a wake
|
|
435
|
+
# turn, chi send's) nobody is there to answer, so it is refused at once.
|
|
436
|
+
# A "Don't send" holds for the rest of the turn.
|
|
437
|
+
# @raise [SendError]
|
|
438
|
+
def quiet_check!(ctx)
|
|
439
|
+
idle = dormant_for(ctx) or return
|
|
440
|
+
peer = self.class
|
|
441
|
+
quiet = "#{peer::NAME} and chi have exchanged nothing for #{duration(idle)}: its session may be cold"
|
|
442
|
+
refused = "Don't send to #{peer::NAME} again in this turn: tell your user what you would send, and they " \
|
|
443
|
+
"send it (#{peer::COMMAND} validate or #{peer::COMMAND} context) if they want to."
|
|
444
|
+
raise SendError, "your user chose not to send to #{peer::NAME} now (#{quiet}). #{refused}" if @lock.synchronize { @declined }
|
|
445
|
+
raise SendError, "#{quiet}, and only your user decides whether to send now (#{QUIET_KEY}). #{refused}" unless ctx.turn_interactive?
|
|
446
|
+
|
|
447
|
+
answer = ctx.ask_user(question: "#{peer::NAME} last heard from chi #{duration(idle)} ago: its session may be cold. Send?",
|
|
448
|
+
header: peer::NAME, options: [SEND, DONT_SEND], effects: { SEND => :spend })
|
|
449
|
+
return if answer && (Array(answer[:selected]).first == SEND || Array(answer[:selected_indices]).first&.zero?)
|
|
450
|
+
|
|
451
|
+
@lock.synchronize { @declined = true }
|
|
452
|
+
raise SendError, "your user chose not to send to #{peer::NAME} now (#{quiet}). #{refused}"
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
# The newest time chi and the peer exchanged anything: the pairing,
|
|
456
|
+
# chi's sends to it (PeerExchanges, last-send.json) and its messages,
|
|
457
|
+
# in this session and the one it continues (a continue copies the
|
|
458
|
+
# pairing with its paired_at, and the new link's logs start empty).
|
|
459
|
+
# @return [Time, nil]
|
|
460
|
+
def last_exchange_at(ctx)
|
|
461
|
+
name = self.class::PEER
|
|
462
|
+
stamps = [ctx.peers.link(name)&.paired_at, last_send(ctx)&.dig("at")]
|
|
463
|
+
[ctx.session_id, ctx.continues].compact.each do |id|
|
|
464
|
+
stamps.concat(PeerExchanges.read(id, state_dir: ctx.session_state_dir)
|
|
465
|
+
.filter_map { |entry| entry["at"] if entry["t"] == "sent" && entry["peer"].to_s == name })
|
|
466
|
+
stamps << newest_message(id, name, ctx.session_state_dir)&.at
|
|
467
|
+
end
|
|
468
|
+
stamps.filter_map { |stamp| time_of(stamp) }.max
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
def newest_message(id, name, state_dir)
|
|
472
|
+
PeerMailbox.serials(id, name, state_dir: state_dir).reverse_each do |serial|
|
|
473
|
+
found = PeerMailbox.message(id, name, serial, state_dir: state_dir)
|
|
474
|
+
return found if found
|
|
475
|
+
end
|
|
476
|
+
nil
|
|
477
|
+
rescue PeerMailbox::Invalid, SessionPaths::InvalidId
|
|
478
|
+
nil
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
def time_of(stamp)
|
|
482
|
+
stamp.is_a?(String) ? Time.iso8601(stamp) : nil
|
|
483
|
+
rescue ArgumentError
|
|
484
|
+
nil
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
# +seconds+ in a few words: "45 min", "2 h", "3 d".
|
|
488
|
+
def duration(seconds)
|
|
489
|
+
minutes = (seconds / 60).floor
|
|
490
|
+
if minutes < 60 then "#{minutes} min"
|
|
491
|
+
elsif minutes < 1440 then "#{minutes / 60} h"
|
|
492
|
+
else "#{minutes / 1440} d"
|
|
493
|
+
end
|
|
494
|
+
end
|
|
495
|
+
|
|
434
496
|
# A pairing with no address: the peer can answer, chi can't send.
|
|
435
497
|
def answers_only_line
|
|
436
498
|
peer = self.class
|
|
@@ -458,6 +520,9 @@ module Samagotchi
|
|
|
458
520
|
end
|
|
459
521
|
paired = !link.nil?
|
|
460
522
|
lines << "last send: #{last_send_line(ctx)}" if id && last_send(ctx)
|
|
523
|
+
if link && !address.empty? && (idle = dormant_for(ctx))
|
|
524
|
+
lines << "quiet #{duration(idle)}: a send from the model asks first"
|
|
525
|
+
end
|
|
461
526
|
if link
|
|
462
527
|
last = ctx.peers.last_message(peer::PEER)
|
|
463
528
|
lines << "last message from #{peer::NAME} (its text in the conversation): #{message_line(last)}"
|
|
@@ -23,18 +23,25 @@ module Samagotchi
|
|
|
23
23
|
# @return [PeerMailbox::PeerLink, nil] nil when not paired (or no session yet)
|
|
24
24
|
def link(name) = read_link(@host.session_id.call, name)
|
|
25
25
|
|
|
26
|
-
# The pairing a continue
|
|
27
|
-
#
|
|
28
|
-
# #tell_moved) and pairs again.
|
|
26
|
+
# The previous link's pairing, in a continue's new link: the one the
|
|
27
|
+
# continue copied here (PeerContinue), as the previous link keeps it.
|
|
29
28
|
# @return [PeerMailbox::PeerLink, nil] nil when this session continues none, or it wasn't paired
|
|
30
29
|
def left_behind(name) = read_link(@continues.call, name)
|
|
31
30
|
|
|
32
|
-
# +name+'s message +serial+ (its text, summary, wake, re, at).
|
|
31
|
+
# +name+'s message +serial+ (its text, summary, wake, re, at). +box+
|
|
32
|
+
# names the mailbox: this session's box for +name+ (the note's), or the
|
|
33
|
+
# first 8 of the session this one continues (a continue's note points
|
|
34
|
+
# there for a message it cut: PeerContinue::CUT), so only the link one
|
|
35
|
+
# hop back in this session's own chain; anything else reads nothing.
|
|
33
36
|
# @return [PeerMailbox::PeerMessage, nil] nil when it isn't there (trimmed) or isn't good
|
|
34
37
|
def message(name, serial, box: nil)
|
|
35
38
|
id = @host.session_id.call or return nil
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
previous = @continues.call
|
|
40
|
+
if box && previous && box == previous.to_s[0, 8]
|
|
41
|
+
id = previous
|
|
42
|
+
elsif box && PeerReceipts.read(id, state_dir: state_dir).peer(name.to_s).box != box
|
|
43
|
+
return nil
|
|
44
|
+
end
|
|
38
45
|
|
|
39
46
|
PeerMailbox.message(id, name.to_s, serial, state_dir: state_dir)
|
|
40
47
|
rescue PeerMailbox::Invalid, SessionPaths::InvalidId
|
|
@@ -566,19 +566,87 @@ module Samagotchi
|
|
|
566
566
|
end
|
|
567
567
|
|
|
568
568
|
# --- peers (PeerMailbox: the peer's side; PeerReceipts: the worker's)
|
|
569
|
-
|
|
570
|
-
#
|
|
571
|
-
#
|
|
572
|
-
#
|
|
573
|
-
#
|
|
574
|
-
|
|
575
|
-
#
|
|
569
|
+
#
|
|
570
|
+
# The pairing follows the chain (a continue copies it, PeerContinue):
|
|
571
|
+
# a pair, unpair, list or say of a continued id runs on its chain's
|
|
572
|
+
# live link (#peer_head) whole, every step keyed on that link, and
|
|
573
|
+
# names it (+forwarded_to+).
|
|
574
|
+
|
|
575
|
+
# Pairs +peer+ with session +id+ (link.json), or with its chain's live
|
|
576
|
+
# link when +id+ was continued. Refused, nothing written, when the
|
|
577
|
+
# session is being continued or a live worker of it doesn't take the
|
|
578
|
+
# peer mail (a pairing to it would look done and not work). The
|
|
579
|
+
# pairing is written under a shared hold of the session's transition
|
|
580
|
+
# lock, checked again there (#moved_on): a continue holding it refuses
|
|
581
|
+
# the pair, naming its new link.
|
|
576
582
|
# @return [PeerPaired]
|
|
577
583
|
# @raise [PeerMailbox::Invalid] a bad name
|
|
578
584
|
# @raise [Session::NotFound] no such session
|
|
579
585
|
def peer_pair(id, peer, address:, from:)
|
|
580
586
|
PeerMailbox.check_name!(peer)
|
|
581
587
|
raise Session::NotFound, id unless Session.exist?(id, **dir)
|
|
588
|
+
|
|
589
|
+
head = peer_head(id)
|
|
590
|
+
paired = pair_on(head, peer, address: address, from: from)
|
|
591
|
+
head == id ? paired : paired.with(forwarded_to: head)
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
# Ends the pairing (link.json) of +id+ and, when it was continued, of
|
|
595
|
+
# every link after it to its chain's live link (the one chi sends
|
|
596
|
+
# from; a link between keeps a copy that deleting the live link would
|
|
597
|
+
# bring back with the link it un-archives); the messages stay.
|
|
598
|
+
# Transition hold: none (a removal; refusing it would only make the
|
|
599
|
+
# owner retry).
|
|
600
|
+
# @return [PeerUnpaired]
|
|
601
|
+
def peer_unpair(id, peer)
|
|
602
|
+
path = Session.exist?(id, **dir) ? SessionChain::Links.new(state_dir: state_dir!).to_first_live(id) : [id]
|
|
603
|
+
ended = path.map { |link| PeerMailbox.unpair(link, peer, state_dir: state_dir!) }
|
|
604
|
+
PeerUnpaired.new(unpaired: ended.any?, forwarded_to: (path.last unless path.last == id))
|
|
605
|
+
end
|
|
606
|
+
|
|
607
|
+
# The peers of +id+, or of its chain's live link when it was continued.
|
|
608
|
+
# @return [Array<PeerRoster::Peer>]
|
|
609
|
+
def peer_list(id) = PeerRoster.build(Session.exist?(id, **dir) ? peer_head(id) : id, state_dir: state_dir!)
|
|
610
|
+
|
|
611
|
+
# A message from +peer+ into session +id+, as a delivery
|
|
612
|
+
# (SessionInbox.deliver :peer), run on +id+'s chain's live link when
|
|
613
|
+
# +id+ was continued (#peer_head; the line names it). Refused before
|
|
614
|
+
# anything is written for a gone session, a continued one whose live
|
|
615
|
+
# link isn't paired (an older chi's continue: it names that link), no
|
|
616
|
+
# pairing, a live worker without the peer mail, UNTAKEN_MAX unread;
|
|
617
|
+
# else, under a shared hold of the link's transition lock (checked
|
|
618
|
+
# again there, #moved_on: a continue holding it refuses the say, naming
|
|
619
|
+
# its new link, so no message lands in a mailbox its carry already
|
|
620
|
+
# read; the pairing read again there too, as the continue copies it),
|
|
621
|
+
# written inside the delivery's block (a session being deleted refuses
|
|
622
|
+
# first), which asks for a worker by the message's own +wake+ alone (no
|
|
623
|
+
# context.wake, no window). Then what chi did, at once when it can't
|
|
624
|
+
# answer yet, else the receipt within +wait+ seconds (a look every
|
|
625
|
+
# PeerSay::POLL_SECONDS).
|
|
626
|
+
# @return [PeerSay]
|
|
627
|
+
# @raise [PeerMailbox::Invalid] a bad name, an empty or oversized text
|
|
628
|
+
def peer_say(id, peer, text:, summary:, wake:, re:, wait: 0, sleeper: ->(seconds) { sleep(seconds) },
|
|
629
|
+
clock: -> { monotonic })
|
|
630
|
+
PeerMailbox.check_name!(peer)
|
|
631
|
+
PeerMailbox.checked_text(text)
|
|
632
|
+
return PeerSay.new(status: :gone) unless Session.exist?(id, **dir)
|
|
633
|
+
|
|
634
|
+
head = peer_head(id)
|
|
635
|
+
said = say_on(head, id, peer, text: text, summary: summary, wake: wake, re: re, wait: wait, sleeper: sleeper,
|
|
636
|
+
clock: clock)
|
|
637
|
+
head == id || !said.received? ? said : said.with(forwarded_to: head)
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
private
|
|
641
|
+
|
|
642
|
+
# The link a peer command of +id+ runs on: +id+, or, when it is
|
|
643
|
+
# archived, its chain's first live link from it (SessionChain::Links
|
|
644
|
+
# #first_live, SessionInbox::Head's walk; the chain's end when every
|
|
645
|
+
# link after it is archived). Only an archived id lists the sessions.
|
|
646
|
+
def peer_head(id) = SessionChain::Links.new(state_dir: state_dir!).first_live(id)
|
|
647
|
+
|
|
648
|
+
# #peer_pair on the link +id+ (#peer_head's).
|
|
649
|
+
def pair_on(id, peer, address:, from:)
|
|
582
650
|
if (successor = continued_as(id))
|
|
583
651
|
return PeerPaired.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
|
|
584
652
|
end
|
|
@@ -603,37 +671,17 @@ module Samagotchi
|
|
|
603
671
|
end
|
|
604
672
|
end
|
|
605
673
|
|
|
606
|
-
#
|
|
607
|
-
#
|
|
608
|
-
#
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
# @return [Array<PeerRoster::Peer>]
|
|
612
|
-
def peer_list(id) = PeerRoster.build(id, state_dir: state_dir!)
|
|
613
|
-
|
|
614
|
-
# A message from +peer+ into session +id+, as a delivery
|
|
615
|
-
# (SessionInbox.deliver :peer): refused before anything is written
|
|
616
|
-
# for a gone session, a continued one, no pairing, a live worker
|
|
617
|
-
# without the peer mail, UNTAKEN_MAX unread; else, under a shared hold
|
|
618
|
-
# of the session's transition lock (checked again there, #moved_on: a
|
|
619
|
-
# continue holding it refuses the say, naming its new link, so no
|
|
620
|
-
# message lands in a mailbox its carry already read), written inside
|
|
621
|
-
# the delivery's block (a session being deleted refuses first), which
|
|
622
|
-
# asks for a worker by the message's own +wake+ alone (no
|
|
623
|
-
# context.wake, no window). Then what chi did, at once when it can't
|
|
624
|
-
# answer yet, else the receipt within +wait+ seconds (a look every
|
|
625
|
-
# PeerSay::POLL_SECONDS).
|
|
626
|
-
# @return [PeerSay]
|
|
627
|
-
# @raise [PeerMailbox::Invalid] a bad name, an empty or oversized text
|
|
628
|
-
def peer_say(id, peer, text:, summary:, wake:, re:, wait: 0, sleeper: ->(seconds) { sleep(seconds) },
|
|
629
|
-
clock: -> { monotonic })
|
|
630
|
-
PeerMailbox.check_name!(peer)
|
|
631
|
-
PeerMailbox.checked_text(text)
|
|
632
|
-
return PeerSay.new(status: :gone) unless Session.exist?(id, **dir)
|
|
674
|
+
# #peer_say on the link +id+ (#peer_head's for +asked+). A live link
|
|
675
|
+
# with no pairing of a continued +asked+ (an older chi continued it,
|
|
676
|
+
# copying none) is refused as a continued link, naming it: the peer's
|
|
677
|
+
# skill pairs again there.
|
|
678
|
+
def say_on(id, asked, peer, text:, summary:, wake:, re:, wait:, sleeper:, clock:)
|
|
633
679
|
if (successor = continued_as(id))
|
|
634
680
|
return PeerSay.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
|
|
635
681
|
end
|
|
636
|
-
|
|
682
|
+
|
|
683
|
+
unpaired = id == asked ? PeerSay.new(status: :not_paired) : PeerSay.new(status: :continued, next_id: id)
|
|
684
|
+
return unpaired if id == asked && !PeerMailbox.link(id, peer, state_dir: state_dir!)
|
|
637
685
|
return PeerSay.new(status: :old_worker) if old_peer_worker?(id)
|
|
638
686
|
|
|
639
687
|
message = nil
|
|
@@ -645,6 +693,7 @@ module Samagotchi
|
|
|
645
693
|
when SessionChain::Successor
|
|
646
694
|
return PeerSay.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
647
695
|
end
|
|
696
|
+
return unpaired unless PeerMailbox.link(id, peer, state_dir: state_dir!)
|
|
648
697
|
|
|
649
698
|
SessionInbox.deliver(:peer, id, state_dir: state_dir!) do
|
|
650
699
|
message = PeerMailbox.post(id, peer, text: text, summary: summary, wake: wake, re: re, state_dir: state_dir!,
|
|
@@ -661,14 +710,26 @@ module Samagotchi
|
|
|
661
710
|
start_failed = true
|
|
662
711
|
nil
|
|
663
712
|
end
|
|
664
|
-
return
|
|
713
|
+
return refused_say(id, delivered.wake) if delivered&.refused?
|
|
665
714
|
|
|
666
715
|
at_once = start_failed ? :start_failed : peer_at_once(id, delivered.wake)
|
|
667
716
|
receipt = at_once ? nil : peer_receipt_within(id, peer, message.serial, wait, sleeper, clock)
|
|
668
717
|
peer_said(id, peer, message.serial, at_once, receipt)
|
|
669
718
|
end
|
|
670
719
|
|
|
671
|
-
|
|
720
|
+
# A say the delivery refused, nothing written: a transition's hold
|
|
721
|
+
# (Hold.under: a continue naming its new link, else busy), a delete,
|
|
722
|
+
# or the session gone.
|
|
723
|
+
def refused_say(id, why)
|
|
724
|
+
case why
|
|
725
|
+
when :deleting then PeerSay.new(status: :deleting)
|
|
726
|
+
when :busy then PeerSay.new(status: :busy)
|
|
727
|
+
when :continued
|
|
728
|
+
successor = SessionChain::Successor.of(id, state_dir: state_dir!, in_flight: true)
|
|
729
|
+
successor ? PeerSay.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?) : PeerSay.new(status: :busy)
|
|
730
|
+
else PeerSay.new(status: :gone)
|
|
731
|
+
end
|
|
732
|
+
end
|
|
672
733
|
|
|
673
734
|
# Whether +id+'s live worker doesn't take the peer mail (its Bridge
|
|
674
735
|
# features: a read-only look). No live worker: nil (the next one does).
|
|
@@ -22,7 +22,7 @@ module Samagotchi
|
|
|
22
22
|
case reply.status
|
|
23
23
|
when 200
|
|
24
24
|
link = PeerMailbox::PeerLink.from_h(reply.json["link"]) or raise RemoteFailed, "#{@entry.label}: no link in its answer"
|
|
25
|
-
PeerPaired.new(status: :paired, link: link)
|
|
25
|
+
PeerPaired.new(status: :paired, link: link, forwarded_to: PeerSay.id_or_nil(reply.json["forwarded_to"]))
|
|
26
26
|
when 409
|
|
27
27
|
status = %w[continued busy old_worker].find { |known| known == reply.json["status"] } or raise_refusal(reply)
|
|
28
28
|
PeerPaired.new(status: status.to_sym, next_id: reply.json["next_id"]&.to_s, in_flight: reply.json["in_flight"] == true)
|
|
@@ -30,13 +30,13 @@ module Samagotchi
|
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
# @return [
|
|
33
|
+
# @return [PeerUnpaired] (an older chi web names no +forwarded_to+)
|
|
34
34
|
def peer_unpair(id, peer)
|
|
35
35
|
PeerMailbox.check_name!(peer)
|
|
36
36
|
reply = peer_request(:delete, "#{session_path(id)}/peers/#{peer}")
|
|
37
37
|
raise_refusal(reply) unless reply.status == 200
|
|
38
38
|
|
|
39
|
-
reply.json["unpaired"] == true
|
|
39
|
+
PeerUnpaired.new(unpaired: reply.json["unpaired"] == true, forwarded_to: PeerSay.id_or_nil(reply.json["forwarded_to"]))
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
# @return [Array<PeerRoster::Peer>]
|
|
@@ -84,10 +84,15 @@ module Samagotchi
|
|
|
84
84
|
# From +id+ forward over archived links: +id+ when it is not archived,
|
|
85
85
|
# else the first link after it that is not, or the chain's end (over a
|
|
86
86
|
# pruned anchor's gap too). Only an archived +id+ makes the listing.
|
|
87
|
-
def first_live(id)
|
|
88
|
-
return id unless archived?(id)
|
|
87
|
+
def first_live(id) = to_first_live(id).last
|
|
89
88
|
|
|
90
|
-
|
|
89
|
+
# The links #first_live walks from +id+, +id+ first, the one it
|
|
90
|
+
# reaches last (a peer unpair ends the pairing on each).
|
|
91
|
+
# @return [Array<String>]
|
|
92
|
+
def to_first_live(id)
|
|
93
|
+
return [id] unless archived?(id)
|
|
94
|
+
|
|
95
|
+
walk(id, forward: true) { |link| !archived?(link) }
|
|
91
96
|
end
|
|
92
97
|
|
|
93
98
|
# The link after a pruned anchor's gap: the head of a schedule anchored
|
|
@@ -128,12 +128,13 @@ module Samagotchi
|
|
|
128
128
|
# written into the link only (NoteMove::CONTINUE_SOURCES: never given
|
|
129
129
|
# back), which goes with its delete.
|
|
130
130
|
CarryStep.new(name: :objectives, store: :session_files, carry: :carry_objectives, undo: {}, files: []),
|
|
131
|
-
# The peers'
|
|
132
|
-
#
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
|
|
131
|
+
# The peers' pairings, copied (PeerContinue: the peer does nothing),
|
|
132
|
+
# then the messages it hadn't read, as one note; the messages stay with
|
|
133
|
+
# the previous link (their store rows say so). No undo: the copies are
|
|
134
|
+
# in the link's mailbox folder, removed with its delete (:peer_mailbox),
|
|
135
|
+
# and the note is in the link only, excluded from NoteMove's give-back;
|
|
136
|
+
# the previous link's mailbox and receipts are only read.
|
|
137
|
+
CarryStep.new(name: :peers, store: :peer_mailbox, carry: :carry_peers, undo: {}, files: ["link.json"]),
|
|
137
138
|
# Its waiting notes last: the carry above read a pending /objectives
|
|
138
139
|
# edit from them first. They keep their names, so they still sort
|
|
139
140
|
# ahead of the link's own notes. A note sent to the previous link
|
|
@@ -110,7 +110,12 @@ module Samagotchi
|
|
|
110
110
|
# carry the id.
|
|
111
111
|
unless prompt.to_s.strip.empty?
|
|
112
112
|
session.first_turn_id = SecureRandom.uuid
|
|
113
|
-
|
|
113
|
+
# Who sent it when no person did (a delegating parent, a
|
|
114
|
+
# schedule), and a person's screen (a web tab, a TUI), so its
|
|
115
|
+
# first turn is interactive as their next one is
|
|
116
|
+
# (ctx.turn_interactive?: a dormant peer send asks there); chi
|
|
117
|
+
# send's and no sender's run as the owner's (ClientId::FIRST_PROMPT).
|
|
118
|
+
session.first_turn_client_id = client_id if !ClientId.human?(client_id) || ClientId.interactive?(client_id)
|
|
114
119
|
end
|
|
115
120
|
# With a prompt or without (chi send --new's idle create, its line
|
|
116
121
|
# sent next): its creator's first turn is the driver's too.
|
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
module Samagotchi
|
|
4
4
|
# The files that hold state for a session, including stores outside its
|
|
5
|
-
# sidecar directory. `corpus_dir` is relative to the compatibility corpus
|
|
6
|
-
#
|
|
5
|
+
# sidecar directory. `corpus_dir` is relative to the compatibility corpus;
|
|
6
|
+
# STATE is its root, the state dir beside `sessions/`, where the stores
|
|
7
|
+
# that keep every session's lines sit (SystemOne::RunsStore's
|
|
8
|
+
# system_one/runs/, ContextCandidatesLog's file).
|
|
9
|
+
# A `file` ending in "/" is a folder of files (PeerMailbox's in/, the runs).
|
|
7
10
|
# A Symbol in `continue` names the carry step; a String explains why it stays.
|
|
8
11
|
module SessionStores
|
|
9
12
|
Store = Data.define(:file, :owner, :removal, :cross_version, :continue, :corpus_dir, :corpus_exception) do
|
|
@@ -19,11 +22,12 @@ module Samagotchi
|
|
|
19
22
|
SIDE_CAR = "sessions/compat-session"
|
|
20
23
|
CONTEXT = "context/sessions/compat-session"
|
|
21
24
|
MAILBOX = "mailbox/compat-session/claude"
|
|
22
|
-
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
STATE = "."
|
|
26
|
+
# A continue leaves the messages with the previous link: its :peers
|
|
27
|
+
# step only reads them, for the one note. The pairing (link.json) is
|
|
28
|
+
# carried: copied to the new link, so a say follows the chain.
|
|
29
|
+
MAILBOX_STAYS = "the messages stay with the previous link (its unread ones reach the next one as one note, " \
|
|
30
|
+
"SessionContinue :peers); a say there goes to the chain's live link"
|
|
27
31
|
|
|
28
32
|
STORES = [
|
|
29
33
|
Store.new(file: "archived", owner: "ArchiveStore", removal: :session_files, cross_version: true,
|
|
@@ -38,10 +42,10 @@ module Samagotchi
|
|
|
38
42
|
continue: "a continue ends the previous link's participants", corpus_dir: SIDE_CAR),
|
|
39
43
|
Store.new(file: "participant_prompts.json", owner: "ParticipantPrompts", removal: :session_files, cross_version: true,
|
|
40
44
|
continue: "a continue ends the previous link's participants", corpus_dir: SIDE_CAR),
|
|
41
|
-
Store.new(file: "
|
|
42
|
-
continue: "saved judgments belong to the previous link", corpus_dir:
|
|
45
|
+
Store.new(file: "runs/", owner: "SystemOne::RunsStore", removal: :system_one_runs, cross_version: true,
|
|
46
|
+
continue: "saved judgments belong to the previous link", corpus_dir: "system_one"),
|
|
43
47
|
Store.new(file: "context_candidates.jsonl", owner: "ContextCandidatesLog", removal: :context_candidates_log,
|
|
44
|
-
cross_version:
|
|
48
|
+
cross_version: true, continue: "candidate records belong to the previous link", corpus_dir: STATE),
|
|
45
49
|
Store.new(file: "bridge.json", owner: "WorkerSidecar", removal: :session_files, cross_version: true,
|
|
46
50
|
continue: "the bridge endpoint belongs to the old worker", corpus_dir: SIDE_CAR),
|
|
47
51
|
Store.new(file: "cards.json", owner: "Bridge::CardStore", removal: :session_files, cross_version: true,
|
|
@@ -68,8 +72,9 @@ module Samagotchi
|
|
|
68
72
|
continue: "exchanges stay with the previous link; the new pairing starts empty", corpus_dir: nil,
|
|
69
73
|
corpus_exception: "older readers do not read peer exchange metadata"),
|
|
70
74
|
# A peer's side of the session (PeerMailbox): <state>/mailbox/<id>/<peer>/.
|
|
75
|
+
# The pairing is copied to the new link (SessionContinue :peers).
|
|
71
76
|
Store.new(file: "link.json", owner: "PeerMailbox", removal: :peer_mailbox, cross_version: true,
|
|
72
|
-
continue:
|
|
77
|
+
continue: :peers, corpus_dir: MAILBOX),
|
|
73
78
|
Store.new(file: "in/", owner: "PeerMailbox", removal: :peer_mailbox, cross_version: true,
|
|
74
79
|
continue: MAILBOX_STAYS, corpus_dir: MAILBOX),
|
|
75
80
|
Store.new(file: "lock", owner: "PeerMailbox", removal: :peer_mailbox, cross_version: false,
|
|
@@ -119,8 +119,11 @@ module Samagotchi
|
|
|
119
119
|
end
|
|
120
120
|
|
|
121
121
|
# @param store [#append, #version, nil] nil: nothing is saved
|
|
122
|
-
|
|
122
|
+
# @param clock [#call] the time a line is stamped with (its "at", which
|
|
123
|
+
# names its day file)
|
|
124
|
+
def initialize(store: nil, clock: -> { Time.now })
|
|
123
125
|
@store = store
|
|
126
|
+
@clock = clock
|
|
124
127
|
end
|
|
125
128
|
|
|
126
129
|
def on? = !@store.nil?
|
|
@@ -178,7 +181,7 @@ module Samagotchi
|
|
|
178
181
|
false
|
|
179
182
|
end
|
|
180
183
|
|
|
181
|
-
def now =
|
|
184
|
+
def now = @clock.call.utc.iso8601(3)
|
|
182
185
|
end
|
|
183
186
|
end
|
|
184
187
|
end
|
|
@@ -109,7 +109,6 @@ module Samagotchi
|
|
|
109
109
|
"Samagotchi::ProcessGroup.stop" => "stops a server's process group: needs a ctx process API",
|
|
110
110
|
'require "samagotchi/token_usage"' => "for CHARS_PER_TOKEN",
|
|
111
111
|
"Samagotchi::TokenUsage::CHARS_PER_TOKEN" => "estimates a tool's tokens: could be a stable constant",
|
|
112
|
-
'require "samagotchi/tool_declarations"' => "loaded for Api.tool_spec's schemas",
|
|
113
112
|
"Samagotchi::ProjectEnv" => "checks the worker has a project env (an older chi doesn't)",
|
|
114
113
|
"Samagotchi::ProjectEnv.current" => "starts a server in the session's project env: needs a ctx tools-env API",
|
|
115
114
|
"Samagotchi::ProjectEnv.respond_to?" => "checks the worker has ProjectEnv.spawn_env (an older chi doesn't)",
|