samagotchi 0.191.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 +91 -1
- data/docs/architecture.md +8 -7
- data/docs/bundles.md +46 -29
- data/docs/cli.md +37 -15
- data/docs/configuration.md +10 -4
- 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/inline-widgets.md +2 -1
- data/docs/internals/pages.md +4 -4
- data/docs/internals/participants.md +6 -8
- data/docs/internals/peers.md +75 -22
- data/docs/internals/prompt-caching.md +2 -1
- data/docs/internals/remote-sessions.md +9 -0
- data/docs/internals/session-lifecycle.md +36 -21
- data/docs/internals/web-frontend.md +195 -25
- 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 +10 -3
- data/docs/sessions.md +3 -3
- data/docs/testing.md +36 -5
- data/docs/thin-clients.md +114 -12
- data/lib/samagotchi/bridge.rb +2 -3
- 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 +4 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +7 -405
- data/lib/samagotchi/bundles/mcp/server_config.rb +158 -0
- data/lib/samagotchi/bundles/mcp/sign_in.rb +261 -0
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/client_id.rb +2 -19
- data/lib/samagotchi/compat.rb +6 -5
- data/lib/samagotchi/config.rb +7 -0
- data/lib/samagotchi/continue_offer.rb +13 -1
- data/lib/samagotchi/delivery.rb +3 -7
- 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/schedule_command.rb +1 -0
- data/lib/samagotchi/schedules/prune_part.rb +32 -0
- data/lib/samagotchi/schedules.rb +14 -9
- data/lib/samagotchi/session.rb +7 -0
- data/lib/samagotchi/session_access/local.rb +98 -37
- data/lib/samagotchi/session_access/remote.rb +1 -1
- 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 +15 -7
- data/lib/samagotchi/session_lifecycle.rb +4 -0
- data/lib/samagotchi/session_manager/delivery.rb +0 -6
- data/lib/samagotchi/session_manager/lifecycle.rb +30 -17
- data/lib/samagotchi/session_manager/listing.rb +2 -1
- data/lib/samagotchi/session_manager/removal.rb +7 -5
- data/lib/samagotchi/session_manager/spawn.rb +8 -4
- data/lib/samagotchi/session_manager.rb +4 -0
- data/lib/samagotchi/session_row.rb +2 -0
- data/lib/samagotchi/session_stores.rb +16 -11
- data/lib/samagotchi/session_transition.rb +61 -15
- 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/tools/cancel_schedule.rb +10 -6
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +3 -1
- data/lib/samagotchi/web/ask_create.rb +138 -0
- data/lib/samagotchi/web/ask_routes.rb +56 -9
- data/lib/samagotchi/web/last_ask.rb +78 -0
- 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/answer_refs.js +3 -9
- data/lib/samagotchi/web/public/answer_write.js +28 -0
- data/lib/samagotchi/web/public/app.js +75 -99
- data/lib/samagotchi/web/public/block_fix.js +30 -12
- data/lib/samagotchi/web/public/cards_place.js +6 -4
- data/lib/samagotchi/web/public/chains.js +3 -1
- data/lib/samagotchi/web/public/command_list.js +5 -3
- data/lib/samagotchi/web/public/composer.js +9 -6
- data/lib/samagotchi/web/public/composer_mount.js +18 -11
- data/lib/samagotchi/web/public/draft_keys.js +10 -0
- data/lib/samagotchi/web/public/drafts.js +278 -0
- data/lib/samagotchi/web/public/history_reveal.js +5 -2
- data/lib/samagotchi/web/public/index.html +38 -2
- data/lib/samagotchi/web/public/owner_claim.js +7 -12
- 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/prompt_history.js +6 -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 +62 -23
- data/lib/samagotchi/web/public/session_actions.js +80 -16
- data/lib/samagotchi/web/public/session_prefs.js +9 -3
- data/lib/samagotchi/web/public/session_view.js +7 -2
- data/lib/samagotchi/web/public/turn_block.js +4 -0
- data/lib/samagotchi/web/public/turn_events.js +0 -2
- data/lib/samagotchi/web/public/unqueue.js +22 -9
- data/lib/samagotchi/web/public/viewer_self.js +1 -6
- data/lib/samagotchi/web/public/views.js +12 -1
- data/lib/samagotchi/web/session_access_routes.rb +6 -3
- data/lib/samagotchi/web/session_control.rb +4 -2
- data/lib/samagotchi/web/session_create.rb +50 -33
- data/lib/samagotchi/web/session_hub.rb +52 -36
- data/lib/samagotchi/web/session_summary.rb +45 -7
- data/lib/samagotchi/web/start_dir.rb +6 -4
- 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_presence.rb +22 -6
- data/lib/samagotchi/worker_wakes.rb +33 -5
- metadata +12 -2
- data/lib/samagotchi/web/public/pages_group.js +0 -67
data/lib/samagotchi/peer_say.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require_relative "peer_mailbox"
|
|
4
4
|
require_relative "peer_receipts"
|
|
5
5
|
require_relative "session_chain/successor"
|
|
6
|
+
require_relative "session_paths"
|
|
6
7
|
require_relative "session_transition"
|
|
7
8
|
|
|
8
9
|
module Samagotchi
|
|
@@ -26,9 +27,11 @@ module Samagotchi
|
|
|
26
27
|
# without one. +earlier+: [[serial, reason]…] of the peer's other held
|
|
27
28
|
# messages. +turns+: the wakes in a row that a held receipt's reason
|
|
28
29
|
# names (peer turns or max wakes), as configured where the say ran.
|
|
29
|
-
|
|
30
|
+
# +forwarded_to+: the link it went to when the id said to was continued
|
|
31
|
+
# (the chain's live link, which has the pairing), nil when it went there.
|
|
32
|
+
PeerSay = Data.define(:status, :serial, :wake, :receipt, :earlier, :next_id, :turns, :in_flight, :forwarded_to) do
|
|
30
33
|
def initialize(status:, serial: nil, wake: nil, receipt: nil, earlier: [], next_id: nil, turns: nil,
|
|
31
|
-
in_flight: false) = super
|
|
34
|
+
in_flight: false, forwarded_to: nil) = super
|
|
32
35
|
|
|
33
36
|
def received? = status == :received
|
|
34
37
|
|
|
@@ -38,12 +41,14 @@ module Samagotchi
|
|
|
38
41
|
|
|
39
42
|
parts = ["##{serial} received", tail(peer)]
|
|
40
43
|
parts << "also held: #{PeerSay.held_list(earlier)}" unless earlier.empty?
|
|
44
|
+
parts << "session continued as #{forwarded_to[0, 8]}: answer there" if forwarded_to
|
|
41
45
|
parts.join(" · ")
|
|
42
46
|
end
|
|
43
47
|
|
|
44
48
|
def to_h
|
|
45
49
|
{ "status" => status.to_s, "serial" => serial, "wake" => wake&.to_s, "receipt" => receipt&.to_h,
|
|
46
|
-
"earlier" => earlier, "next_id" => next_id, "turns" => turns, "in_flight" => (true if in_flight)
|
|
50
|
+
"earlier" => earlier, "next_id" => next_id, "turns" => turns, "in_flight" => (true if in_flight),
|
|
51
|
+
"forwarded_to" => forwarded_to }.compact
|
|
47
52
|
end
|
|
48
53
|
|
|
49
54
|
# The JSON a chi web answered (#to_h); nil for another shape.
|
|
@@ -56,15 +61,18 @@ module Samagotchi
|
|
|
56
61
|
wake: (data["wake"].to_sym if PeerSay::WAKES.include?(data["wake"].to_s)),
|
|
57
62
|
receipt: PeerReceipts::Receipt.from_h(data["receipt"]), earlier: earlier,
|
|
58
63
|
next_id: data["next_id"]&.to_s, turns: (data["turns"] if data["turns"].is_a?(Integer)),
|
|
59
|
-
in_flight: data["in_flight"] == true)
|
|
64
|
+
in_flight: data["in_flight"] == true, forwarded_to: PeerSay.id_or_nil(data["forwarded_to"]))
|
|
60
65
|
end
|
|
61
66
|
|
|
67
|
+
# A session id a chi web named, else nil.
|
|
68
|
+
def self.id_or_nil(value) = value.is_a?(String) && SessionPaths.valid_id?(value) ? value : nil
|
|
69
|
+
|
|
62
70
|
# The words of a refused say, nothing written.
|
|
63
71
|
def self.refusal(status, peer, next_id, in_flight: false)
|
|
64
72
|
case status
|
|
65
73
|
when :gone then "no such session"
|
|
66
74
|
when :deleting then "this session is being deleted"
|
|
67
|
-
when :continued then PeerSay.continued(next_id, in_flight, "say it there")
|
|
75
|
+
when :continued then PeerSay.continued(next_id, in_flight, "say it there", "say it again")
|
|
68
76
|
when :busy then SessionTransition::Busy.new(nil).message
|
|
69
77
|
when :not_paired then "not paired: run the pair script"
|
|
70
78
|
when :old_worker then "this session's chi is older than chi peer: restart the session (chi sessions restart)"
|
|
@@ -74,10 +82,11 @@ module Samagotchi
|
|
|
74
82
|
end
|
|
75
83
|
|
|
76
84
|
# A continued link's refusal (SessionChain::Continued, "this session"),
|
|
77
|
-
# then what to do: +what+ there,
|
|
78
|
-
#
|
|
79
|
-
|
|
80
|
-
|
|
85
|
+
# then what to do: +what+ there, or, while its continue still runs,
|
|
86
|
+
# +again+ in a moment (the same command: once the new link is saved, a
|
|
87
|
+
# say or pair of this id goes there).
|
|
88
|
+
def self.continued(next_id, in_flight, what, again)
|
|
89
|
+
"#{SessionChain::Continued.new(nil, next_id, in_flight: in_flight).message}: #{in_flight ? "#{again} in a moment" : what}"
|
|
81
90
|
end
|
|
82
91
|
|
|
83
92
|
# "#10, #11 (paused)", a run of one reason at a time.
|
|
@@ -126,19 +135,27 @@ module Samagotchi
|
|
|
126
135
|
|
|
127
136
|
# What chi peer pair did: :paired (+link+), or a refusal (nothing
|
|
128
137
|
# written): :continued (+next_id+, +in_flight+ as PeerSay's), :busy,
|
|
129
|
-
# :old_worker (a live worker without the peer mail).
|
|
130
|
-
|
|
131
|
-
|
|
138
|
+
# :old_worker (a live worker without the peer mail). +forwarded_to+ as
|
|
139
|
+
# PeerSay's: the chain's live link it paired, when the id was continued.
|
|
140
|
+
PeerPaired = Data.define(:status, :link, :next_id, :in_flight, :forwarded_to) do
|
|
141
|
+
def initialize(status:, link: nil, next_id: nil, in_flight: false, forwarded_to: nil) = super
|
|
132
142
|
|
|
133
143
|
def refusal
|
|
134
144
|
case status
|
|
135
|
-
when :continued then PeerSay.continued(next_id, in_flight, "pair with it")
|
|
145
|
+
when :continued then PeerSay.continued(next_id, in_flight, "pair with it", "pair again")
|
|
136
146
|
when :busy then SessionTransition::Busy.new(nil).message
|
|
137
147
|
when :old_worker then PeerSay.refusal(:old_worker, nil, nil)
|
|
138
148
|
end
|
|
139
149
|
end
|
|
140
150
|
end
|
|
141
151
|
|
|
152
|
+
# What chi peer unpair did: whether there was a pairing (+unpaired+) and,
|
|
153
|
+
# when the id was continued, the chain's live link it ended
|
|
154
|
+
# (+forwarded_to+).
|
|
155
|
+
PeerUnpaired = Data.define(:unpaired, :forwarded_to) do
|
|
156
|
+
def initialize(unpaired:, forwarded_to: nil) = super
|
|
157
|
+
end
|
|
158
|
+
|
|
142
159
|
PeerSay::STATUSES = %w[received gone deleting continued busy not_paired old_worker full].freeze
|
|
143
160
|
PeerSay::WAKES = %w[running starting stopped archived owned start_failed no_turn not_running deleting].freeze
|
|
144
161
|
# The setting a peer_turns hold names (WorkerWakes::PEER_TURNS_KEY: the
|
|
@@ -164,13 +181,14 @@ module Samagotchi
|
|
|
164
181
|
"queued" => "a message is queued ahead of it",
|
|
165
182
|
"exit" => "chi is exiting",
|
|
166
183
|
"leaving" => "chi's worker is leaving",
|
|
167
|
-
"before_turn" => "a turn is starting"
|
|
184
|
+
"before_turn" => "a turn is starting",
|
|
185
|
+
"cold" => "chi's cache is cold"
|
|
168
186
|
}.freeze
|
|
169
187
|
# And until when it waits.
|
|
170
188
|
PeerSay::HELD_UNTIL = {
|
|
171
189
|
"off" => "waits for the user's next message", "paused" => "waits for the user's next message",
|
|
172
190
|
"continue" => "waits for the user's next message", "max_wakes" => "waits for the user's next message",
|
|
173
|
-
"peer_turns" => "waits for the user's next message",
|
|
191
|
+
"peer_turns" => "waits for the user's next message", "cold" => "waits for the user's next message",
|
|
174
192
|
"queued" => "chi reads it in its next turn", "before_turn" => "chi reads it in that turn",
|
|
175
193
|
"exit" => "it reads this at its next start", "leaving" => "it reads this at its next start"
|
|
176
194
|
}.freeze
|
|
@@ -178,6 +196,6 @@ module Samagotchi
|
|
|
178
196
|
PeerSay::SHORT_WORDS = {
|
|
179
197
|
"off" => "off", "paused" => "paused", "continue" => "continue offer", "max_wakes" => "max wakes",
|
|
180
198
|
"peer_turns" => "peer turns", "queued" => "queued", "exit" => "exit", "leaving" => "leaving",
|
|
181
|
-
"before_turn" => "before a turn"
|
|
199
|
+
"before_turn" => "before a turn", "cold" => "cold"
|
|
182
200
|
}.freeze
|
|
183
201
|
end
|
|
@@ -62,7 +62,8 @@ module Samagotchi
|
|
|
62
62
|
"ctx.sign_in" => "0.183.0",
|
|
63
63
|
"ctx.card(between_turns:)" => "0.183.0",
|
|
64
64
|
"chi.peer" => "0.188.0",
|
|
65
|
-
"ctx.peers" => "0.188.0"
|
|
65
|
+
"ctx.peers" => "0.188.0",
|
|
66
|
+
"ctx.turn_interactive?" => "0.193.0"
|
|
66
67
|
}.freeze
|
|
67
68
|
|
|
68
69
|
# chi.on's on_error: values. :log (the default) logs a raise and goes
|
|
@@ -85,12 +85,14 @@ module Samagotchi
|
|
|
85
85
|
# +sign_in+ takes (bundle, title:, button:, timeout:, port:, path:, cancel:) and
|
|
86
86
|
# the block that builds the address, and returns the SignIn flow
|
|
87
87
|
# (Engine#start_sign_in; the test kit's opens no port).
|
|
88
|
+
# +turn_interactive+ returns whether the running turn came from a screen
|
|
89
|
+
# that can answer a question (ClientId.interactive?: a web tab, a TUI).
|
|
88
90
|
Host = Struct.new(:session_id, :cwd, :messages, :messages_partial, :notify, :ask_user, :cancelled, :card,
|
|
89
91
|
:ask_model, :model_name, :model_key, :state_dir, :scratch, :setup, :prompt_files,
|
|
90
92
|
:steer, :stop_turn, :stop_generation, :frontend, :toolkit, :declared_tools, :use_toolkit, :load_tools,
|
|
91
93
|
:cancel_controller, :cost, :usage, :turn_author, :cache_confirm, :model_names,
|
|
92
94
|
:candidate_proposed, :judgments, :turn_running, :current_cancel, :secrets_store,
|
|
93
|
-
:sign_in, keyword_init: true)
|
|
95
|
+
:sign_in, :turn_interactive, keyword_init: true)
|
|
94
96
|
|
|
95
97
|
# ctx.ask_model failed: the model couldn't be reached, timed out, or
|
|
96
98
|
# sent nothing usable. The message says why, for the user.
|
|
@@ -230,6 +232,16 @@ module Samagotchi
|
|
|
230
232
|
# power whatever the author (docs/hooks.md).
|
|
231
233
|
def turn_author = @host.turn_author&.call
|
|
232
234
|
|
|
235
|
+
# @return [Boolean] whether the running turn was sent from a screen
|
|
236
|
+
# that shows a question and can answer it now: the owner's web tab or
|
|
237
|
+
# TUI (ClientId.interactive?; their first prompt and a Continue tap on
|
|
238
|
+
# such a turn too), and a thin client's POST …/ask (web:unnamed),
|
|
239
|
+
# which answers through …/answer. False between turns and for every other
|
|
240
|
+
# turn: chi send's, a schedule's, a delegate's, a wake turn (a peer's,
|
|
241
|
+
# a task's, attached context's), a participant's, a one-shot run. A
|
|
242
|
+
# ctx.ask_user in such a turn would wait with nobody there to answer.
|
|
243
|
+
def turn_interactive? = @host.turn_interactive&.call == true
|
|
244
|
+
|
|
233
245
|
# @return [String, nil] the name of the session's toolkit ("full" for
|
|
234
246
|
# one from before toolkits; nil with no session)
|
|
235
247
|
def toolkit = @host.toolkit&.call
|
|
@@ -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
|
|
@@ -9,6 +9,21 @@ require_relative "../session_transition"
|
|
|
9
9
|
|
|
10
10
|
module Samagotchi
|
|
11
11
|
class Schedules
|
|
12
|
+
# What #cancel removed: +entry+ (an Entry, or the raw Hash of one that
|
|
13
|
+
# doesn't read) and +newest+, the chain's newest link, set only
|
|
14
|
+
# when this cancel took away the last bridge over a pruned anchor's gap
|
|
15
|
+
# (PrunePart#cancel_strands). It owns the line that says so; callers only
|
|
16
|
+
# add context.
|
|
17
|
+
Cancelled = Data.define(:entry, :newest) do
|
|
18
|
+
def initialize(entry:, newest: nil) = super
|
|
19
|
+
|
|
20
|
+
def id = entry.is_a?(Hash) ? entry["id"] : entry.id
|
|
21
|
+
|
|
22
|
+
def warning
|
|
23
|
+
newest && "#{entry.anchor[0, 8]}'s chain was pruned; its newest link is #{newest[0, 8]} — send there from now on."
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
12
27
|
# The chain prune after a firing (docs/scheduling.md, "Keeping the chain
|
|
13
28
|
# short"): the links behind a chain entry's keep_links window go, oldest
|
|
14
29
|
# first, each under the transition locks of it and the link after it.
|
|
@@ -169,6 +184,23 @@ module Samagotchi
|
|
|
169
184
|
Log.warn(:worker, "prune_waiting", sid: link, id: entry.id, msg: reason) if logged
|
|
170
185
|
end
|
|
171
186
|
|
|
187
|
+
# The chain's newest link when removing +removed+ from +entries+ (the
|
|
188
|
+
# file's list, under its lock) takes away the last bridge over its
|
|
189
|
+
# anchor's gap (SessionChain::Links#anchor_forward), else nil. A disabled
|
|
190
|
+
# entry still bridges, so only a cancel ends one. The heads are given,
|
|
191
|
+
# not read: the file still holds the entry.
|
|
192
|
+
# @return [String, nil]
|
|
193
|
+
def cancel_strands(entries, removed)
|
|
194
|
+
return nil if removed.head == removed.anchor
|
|
195
|
+
|
|
196
|
+
before = SessionChain::Links.new(state_dir: @state_dir, anchor_heads: heads_by_anchor(entries))
|
|
197
|
+
return nil unless before.anchor_forward(removed.anchor)
|
|
198
|
+
|
|
199
|
+
rest = entries.reject { |entry| entry.equal?(removed) }
|
|
200
|
+
after = SessionChain::Links.new(state_dir: @state_dir, anchor_heads: heads_by_anchor(rest))
|
|
201
|
+
after.anchor_forward(removed.anchor) ? nil : before.latest(removed.anchor)
|
|
202
|
+
end
|
|
203
|
+
|
|
172
204
|
# Neither a session file nor a folder: nothing left to delete.
|
|
173
205
|
def link_gone?(link)
|
|
174
206
|
!File.exist?(Session.session_file(link, state_dir: @state_dir)) &&
|
data/lib/samagotchi/schedules.rb
CHANGED
|
@@ -268,11 +268,7 @@ module Samagotchi
|
|
|
268
268
|
# anchor's gap: a paused schedule's chain is still one chain). Lenient and
|
|
269
269
|
# quiet: a file that doesn't read forwards nothing.
|
|
270
270
|
# @return [Hash{String => Array<String>}]
|
|
271
|
-
def anchor_heads
|
|
272
|
-
read(quiet: true).entries.grep(Entry).each_with_object({}) do |entry, heads|
|
|
273
|
-
(heads[entry.anchor] ||= []) << entry.head
|
|
274
|
-
end
|
|
275
|
-
end
|
|
271
|
+
def anchor_heads = heads_by_anchor(read(quiet: true).entries)
|
|
276
272
|
|
|
277
273
|
# One match is the answer; several are a usage error naming them, the way
|
|
278
274
|
# Session::AmbiguousId does for session prefixes.
|
|
@@ -336,8 +332,11 @@ module Samagotchi
|
|
|
336
332
|
entry
|
|
337
333
|
end
|
|
338
334
|
|
|
339
|
-
# @return [
|
|
340
|
-
def cancel(id_or_name)
|
|
335
|
+
# @return [Cancelled, nil] nil when no schedule matches
|
|
336
|
+
def cancel(id_or_name)
|
|
337
|
+
change(id_or_name, delete: true) || cancel_unreadable(id_or_name)&.then { |record| Cancelled.new(entry: record.raw) }
|
|
338
|
+
end
|
|
339
|
+
|
|
341
340
|
def enable(id_or_name) = change(id_or_name, enabled: true)
|
|
342
341
|
def disable(id_or_name) = change(id_or_name, enabled: false)
|
|
343
342
|
|
|
@@ -574,6 +573,11 @@ module Samagotchi
|
|
|
574
573
|
# one-firing-per-chain guard and the firing see the same chain.
|
|
575
574
|
def links_now = SessionChain::Links.new(state_dir: @state_dir)
|
|
576
575
|
|
|
576
|
+
# #anchor_heads of +entries+ (the readable ones), for PrunePart#cancel_strands too.
|
|
577
|
+
def heads_by_anchor(entries)
|
|
578
|
+
entries.grep(Entry).each_with_object({}) { |entry, heads| (heads[entry.anchor] ||= []) << entry.head }
|
|
579
|
+
end
|
|
580
|
+
|
|
577
581
|
# @param strict [Boolean] a file that is not readable JSON raises rather
|
|
578
582
|
# than reading as empty: a write starting from an empty list would rewrite
|
|
579
583
|
# the file and lose every schedule on it. #entries stays lenient.
|
|
@@ -625,7 +629,7 @@ module Samagotchi
|
|
|
625
629
|
# an ambiguous one raises Usage rather than taking the first match. nil
|
|
626
630
|
# too when the entry is gone by the time the lock is held (another cancel
|
|
627
631
|
# landed in between): the caller says "no schedule", not "ok".
|
|
628
|
-
# @return [Entry, nil] the entry as changed (
|
|
632
|
+
# @return [Entry, Cancelled, nil] the entry as changed (Cancelled, for a delete)
|
|
629
633
|
def change(id_or_name, enabled: nil, delete: false)
|
|
630
634
|
entry = lookup(id_or_name)
|
|
631
635
|
return nil if entry.nil?
|
|
@@ -636,7 +640,8 @@ module Samagotchi
|
|
|
636
640
|
next if position.nil?
|
|
637
641
|
|
|
638
642
|
if delete
|
|
639
|
-
|
|
643
|
+
removed = file.entries[position]
|
|
644
|
+
result = Cancelled.new(entry: removed, newest: cancel_strands(file.entries, removed))
|
|
640
645
|
file.entries[position] = nil
|
|
641
646
|
file.entries.compact!
|
|
642
647
|
else
|
data/lib/samagotchi/session.rb
CHANGED
|
@@ -465,6 +465,11 @@ module Samagotchi
|
|
|
465
465
|
# not saved.
|
|
466
466
|
attr_accessor :model_warning
|
|
467
467
|
|
|
468
|
+
# The status the session file held when loaded, before the stop marker's
|
|
469
|
+
# overlay (Session.load and .summary_from_file set it; nil for a session
|
|
470
|
+
# built in memory); not saved.
|
|
471
|
+
attr_accessor :loaded_status
|
|
472
|
+
|
|
468
473
|
# The delegates a continue moved to this new link (ChildMove;
|
|
469
474
|
# SessionContinue.run sets it); not saved.
|
|
470
475
|
attr_accessor :moved_children
|
|
@@ -653,6 +658,7 @@ module Samagotchi
|
|
|
653
658
|
|
|
654
659
|
session = from_h(JSON.parse(File.read(path)))
|
|
655
660
|
session.persisted!
|
|
661
|
+
session.loaded_status = session.status
|
|
656
662
|
session.status = STATUS_STOPPED if stopped_marker?(session.id, state_dir: state_dir)
|
|
657
663
|
session.parent_id = parent_override(session.id, state_dir: state_dir) || session.parent_id
|
|
658
664
|
session
|
|
@@ -782,6 +788,7 @@ module Samagotchi
|
|
|
782
788
|
|
|
783
789
|
session = from_h(data, messages: false)
|
|
784
790
|
session.persisted!
|
|
791
|
+
session.loaded_status = session.status
|
|
785
792
|
session.status = STATUS_STOPPED if stopped_marker?(session.id, state_dir: File.dirname(path))
|
|
786
793
|
session.parent_id = parent_override(session.id, state_dir: File.dirname(path)) || session.parent_id
|
|
787
794
|
session
|