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
|
@@ -31,13 +31,16 @@ module Samagotchi
|
|
|
31
31
|
# credential printers, git config writes (reads only), home sweeps
|
|
32
32
|
# and links to them, and a program named by a $var or a glob); a
|
|
33
33
|
# write or edit of git's hooks (GIT_HOOKS);
|
|
34
|
-
# - owner-voiced tools (
|
|
35
|
-
# speaks or acts in the owner's name beyond this turn (a
|
|
36
|
-
# reminder, a note to another session, a
|
|
37
|
-
# forgotten output, the session's loaded tools, a
|
|
38
|
-
# objectives); the tools that read
|
|
39
|
-
#
|
|
40
|
-
#
|
|
34
|
+
# - owner-voiced tools (TOOL_CLASSES' :owner_voiced, and any delegate_*
|
|
35
|
+
# tool): what speaks or acts in the owner's name beyond this turn (a
|
|
36
|
+
# delegate, a reminder, a schedule, a note to another session, a
|
|
37
|
+
# background task, a forgotten output, the session's loaded tools, a
|
|
38
|
+
# move on the owner's objectives); the tools that read the owner's
|
|
39
|
+
# background tasks (:owner_tasks); the readers of the owner's other
|
|
40
|
+
# sessions, reminders and schedules and of the session's attached
|
|
41
|
+
# context (:owner_reader: list_sessions, list_reminders,
|
|
42
|
+
# list_schedules, context_read); and memory_write: a memory reaches
|
|
43
|
+
# the owner's later sessions;
|
|
41
44
|
# - ask_user_question: there is no one on the participant's side to
|
|
42
45
|
# answer it here; the model asks in its reply instead.
|
|
43
46
|
# And it asks the owner, always (Verdict#fresh_ask: no stored approval
|
|
@@ -51,14 +54,36 @@ module Samagotchi
|
|
|
51
54
|
class PairBoundary
|
|
52
55
|
RULE = "pair-boundary"
|
|
53
56
|
SOURCE = "core"
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
# Every built-in tool (ToolNames::NAMES, spec/guardrails/pair_boundary_spec.rb
|
|
58
|
+
# fails on one missing here) and find_tools (Tools::ToolIndex::FIND_NAME)
|
|
59
|
+
# by what a participant's turn may do with it:
|
|
60
|
+
# :owner_voiced denied (OWNER_ONLY): it speaks or acts in the owner's name beyond this turn
|
|
61
|
+
# :owner_tasks denied (OWNER_TASKS): it reads the owner's background tasks (a pair turn
|
|
62
|
+
# starts none: task_create is owner-voiced, and its execute is never handed over)
|
|
63
|
+
# :owner_reader denied (OWNER_READS): it reads the owner's other sessions, reminders,
|
|
64
|
+
# schedules or the session's attached context
|
|
65
|
+
# :memory_write denied (MEMORY_WRITE)
|
|
66
|
+
# :ask denied (ASK)
|
|
67
|
+
# :checked goes on to the path and command checks below
|
|
68
|
+
# A name not here (a plugin's or an MCP server's tool) goes on to them
|
|
69
|
+
# too, but a delegate_* one (OWNER_VOICED_PREFIX). call_tool isn't
|
|
70
|
+
# here: ToolIndex#unwrap renames call_tool(chi/list_sessions) to
|
|
71
|
+
# list_sessions before the guardrails see it.
|
|
72
|
+
TOOL_CLASSES = {
|
|
73
|
+
"execute" => :checked, "read" => :checked, "write" => :checked, "edit" => :checked,
|
|
74
|
+
"memory_read" => :checked, "web_fetch" => :checked,
|
|
75
|
+
"memory_write" => :memory_write,
|
|
76
|
+
"ask_user_question" => :ask,
|
|
77
|
+
"task_wait" => :owner_tasks, "task_get" => :owner_tasks, "task_list" => :owner_tasks,
|
|
78
|
+
"list_sessions" => :owner_reader, "list_reminders" => :owner_reader, "list_schedules" => :owner_reader,
|
|
79
|
+
"context_read" => :owner_reader,
|
|
80
|
+
"delegate" => :owner_voiced, "delegate_result" => :owner_voiced, "register_reminder" => :owner_voiced,
|
|
81
|
+
"cancel_reminder" => :owner_voiced, "send_note" => :owner_voiced, "ask_session" => :owner_voiced,
|
|
82
|
+
"forget_outputs" => :owner_voiced, "task_create" => :owner_voiced, "task_stop" => :owner_voiced,
|
|
83
|
+
"find_tools" => :owner_voiced, "objectives" => :owner_voiced, "schedule_task" => :owner_voiced,
|
|
84
|
+
"cancel_schedule" => :owner_voiced
|
|
85
|
+
}.freeze
|
|
56
86
|
OWNER_VOICED_PREFIX = "delegate_"
|
|
57
|
-
# The background task tools that read the owner's tasks (a pair turn
|
|
58
|
-
# starts none: task_create is owner-voiced, and its execute is never
|
|
59
|
-
# handed over).
|
|
60
|
-
OWNER_TASK_READERS = %w[task_wait task_get task_list].freeze
|
|
61
|
-
ASK_TOOL = "ask_user_question"
|
|
62
87
|
WRITE_TOOLS = %w[write edit].freeze
|
|
63
88
|
|
|
64
89
|
ADVICE = "This is a participant's turn: work with this session's other tools, and say in your reply what " \
|
|
@@ -69,6 +94,12 @@ module Samagotchi
|
|
|
69
94
|
MEMORY_WRITE = "a participant's turn writes no memories: they reach the owner's later sessions; say in your " \
|
|
70
95
|
"reply what to remember"
|
|
71
96
|
ASK = "a participant's turn can't ask the user: ask in your reply"
|
|
97
|
+
OWNER_READS = "a participant's turn can't use %s: the owner's other sessions, reminders, schedules and " \
|
|
98
|
+
"attached context are the owner's to read"
|
|
99
|
+
# A denied kind of TOOL_CLASSES → its refusal (one with %s names the
|
|
100
|
+
# tool).
|
|
101
|
+
KIND_TEXTS = { owner_voiced: OWNER_ONLY, owner_tasks: OWNER_TASKS, owner_reader: OWNER_READS,
|
|
102
|
+
memory_write: MEMORY_WRITE, ask: ASK }.freeze
|
|
72
103
|
OUTSIDE_WRITE = "a participant's turn writes outside the session's project"
|
|
73
104
|
NO_PROJECT = "a participant's turn writes, and this session's folder is the home folder or above it (or none): " \
|
|
74
105
|
"there is no project to keep it in"
|
|
@@ -185,10 +216,10 @@ module Samagotchi
|
|
|
185
216
|
def refusal(verdict)
|
|
186
217
|
call = verdict.call || {}
|
|
187
218
|
tool = call[:name].to_s
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
219
|
+
kind = TOOL_CLASSES.fetch(tool) { :owner_voiced if tool.start_with?(OWNER_VOICED_PREFIX) }
|
|
220
|
+
if (text = KIND_TEXTS[kind])
|
|
221
|
+
return text.include?("%s") ? format(text, tool) : text
|
|
222
|
+
end
|
|
192
223
|
|
|
193
224
|
targets = verdict.targets
|
|
194
225
|
return nil unless targets
|
|
@@ -25,16 +25,27 @@ module Samagotchi
|
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
# Every line that reads as a Hash, oldest first; none with no file
|
|
28
|
+
# Every line that reads as a Hash, oldest first; none with no file, or
|
|
29
|
+
# one that can't be read.
|
|
29
30
|
# @return [Array<Hash>]
|
|
30
31
|
def lines(session_dir)
|
|
32
|
+
lines!(session_dir)
|
|
33
|
+
rescue SystemCallError
|
|
34
|
+
[]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# #lines, but a file that is there and can't be read raises: for a
|
|
38
|
+
# reader that must tell "nothing logged" from "can't tell".
|
|
39
|
+
# @return [Array<Hash>]
|
|
40
|
+
# @raise [SystemCallError] anything but a missing file
|
|
41
|
+
def lines!(session_dir)
|
|
31
42
|
File.readlines(File.join(session_dir, file), chomp: true).filter_map do |raw|
|
|
32
43
|
line = JSON.parse(raw)
|
|
33
44
|
line if line.is_a?(Hash)
|
|
34
45
|
rescue JSON::ParserError
|
|
35
46
|
nil
|
|
36
47
|
end
|
|
37
|
-
rescue
|
|
48
|
+
rescue Errno::ENOENT, Errno::ENOTDIR
|
|
38
49
|
[]
|
|
39
50
|
end
|
|
40
51
|
|
|
@@ -228,6 +228,17 @@ module Samagotchi
|
|
|
228
228
|
# Whether +model+ gets the budget form (it refused adaptive thinking).
|
|
229
229
|
def budget_form?(model) = @refusals.refused?(:thinking_form, model.to_s)
|
|
230
230
|
|
|
231
|
+
# Whether this client's chat requests for +model+ count as carrying a
|
|
232
|
+
# remote host's prompt-cache breakpoints, so its cache has a TTL
|
|
233
|
+
# (cache.ttl; the peer mail's cold hold): a Claude model on a remote
|
|
234
|
+
# host whose dialect sends them (#request_body's cache) and didn't
|
|
235
|
+
# refuse them. A local Messages server (no remote TTL) and a
|
|
236
|
+
# non-Claude model don't.
|
|
237
|
+
def cache_breakpoints?(model)
|
|
238
|
+
@remote && @purpose == "chat" && PromptCache.claude?(model) && @dialect.cache_breakpoints &&
|
|
239
|
+
!@refusals.refused?(:cache_control)
|
|
240
|
+
end
|
|
241
|
+
|
|
231
242
|
# The host's models, every page: GET {base}/v1/models with
|
|
232
243
|
# limit=1000 and after_id paging (has_more/last_id). Anthropic's shape
|
|
233
244
|
# (max_input_tokens, max_tokens, capabilities) and the OpenAI shape a
|
|
@@ -243,6 +243,11 @@ module Samagotchi
|
|
|
243
243
|
provider.is_a?(String) && !provider.strip.empty? ? provider : nil
|
|
244
244
|
end
|
|
245
245
|
|
|
246
|
+
# Whether this client's requests for +model+ carry prompt-cache
|
|
247
|
+
# breakpoints (#cache_breakpoints: a Claude model's chat requests to a
|
|
248
|
+
# remote host), so the host's cache has a TTL (cache.ttl).
|
|
249
|
+
def cache_breakpoints?(model) = @remote && @purpose == "chat" && PromptCache.claude?(model)
|
|
250
|
+
|
|
246
251
|
private
|
|
247
252
|
|
|
248
253
|
def request_body(messages, tools, model, options, session_id)
|
|
@@ -47,6 +47,11 @@ module Samagotchi
|
|
|
47
47
|
CONTROL
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
# How long a host keeps a breakpoint's cache after its last read, in
|
|
51
|
+
# seconds, under cache.ttl (#control): an hour for 1h, else 5 minutes.
|
|
52
|
+
# @return [Integer]
|
|
53
|
+
def ttl_seconds = control[:ttl] == "1h" ? 3600 : 300
|
|
54
|
+
|
|
50
55
|
# Whether +model+ (the bare id a host is asked for) names a Claude model.
|
|
51
56
|
def claude?(model) = model.to_s.match?(/claude/i)
|
|
52
57
|
|
|
@@ -87,7 +87,7 @@ module Samagotchi
|
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
# The changes to this process's env a run gets, as a worker's
|
|
90
|
-
# (SessionManager::
|
|
90
|
+
# (SessionManager::WorkerCommandPart#spawn_options, private in session_manager/worker_command.rb): an installed gem's
|
|
91
91
|
# Bundler changes mapped back, the spawner's keys unset, the XDG dirs
|
|
92
92
|
# absolute. PWD is set to the pages folder too: spawn's chdir doesn't
|
|
93
93
|
# change it, and a script reading ENV["PWD"] would otherwise see the
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
require_relative "analytics_file"
|
|
5
|
+
require_relative "degrade"
|
|
6
|
+
require_relative "peer_exchanges"
|
|
7
|
+
require_relative "peer_mailbox"
|
|
8
|
+
|
|
9
|
+
module Samagotchi
|
|
10
|
+
# The peer mail's cold hold (WorkerInbound::MailPart#mail_hold, reason
|
|
11
|
+
# :cold): a message a peer starts on its own (no +re+ naming a send of
|
|
12
|
+
# chi's), or an agreeing closer (VERDICT OK), doesn't wake chi while its
|
|
13
|
+
# prompt cache is cold: a turn then re-reads the whole conversation at
|
|
14
|
+
# the write price. It waits as a note for the user's next message. An
|
|
15
|
+
# answer chi asked for still wakes it (docs/peers.md, Held reasons).
|
|
16
|
+
#
|
|
17
|
+
# "Cold": the host keeps a TTL'd cache for the session (its requests carry
|
|
18
|
+
# breakpoints: Engine#remote_cache_ttl; a local model has none), the last
|
|
19
|
+
# turn ended longer ago than that TTL (Session#last_turn's ended_at: saved,
|
|
20
|
+
# so a new worker reads it; a ContinueOffer stamp or a failed turn's taken
|
|
21
|
+
# as it is, which is never earlier than the last request), and the
|
|
22
|
+
# conversation is above FLOOR_TOKENS (analytics.json's context.used_tokens),
|
|
23
|
+
# so a small session is never held. Anything unknown is warm: no ended_at
|
|
24
|
+
# (a new link), no count, an unparsable time.
|
|
25
|
+
module PeerCold
|
|
26
|
+
# A conversation this small is never held: its re-read is cheap.
|
|
27
|
+
FLOOR_TOKENS = 20_000
|
|
28
|
+
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
# @param ttl [Integer, nil] seconds the host keeps the cache; nil: no breakpoints
|
|
32
|
+
# @param last_turn [LastTurn, nil] the session's last turn
|
|
33
|
+
# @param used_tokens [Integer, nil] the conversation's size
|
|
34
|
+
# @param now [Time]
|
|
35
|
+
def cold?(ttl:, last_turn:, used_tokens:, now:)
|
|
36
|
+
return false unless ttl.is_a?(Integer) && ttl.positive?
|
|
37
|
+
return false unless used_tokens.is_a?(Integer) && used_tokens > FLOOR_TOKENS
|
|
38
|
+
|
|
39
|
+
ended = ended_at(last_turn)
|
|
40
|
+
!ended.nil? && now - ended > ttl
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The conversation's size the last turn saved (analytics.json), nil
|
|
44
|
+
# when there is none.
|
|
45
|
+
# @return [Integer, nil]
|
|
46
|
+
def used_tokens(session_dir)
|
|
47
|
+
value = AnalyticsFile.read(session_dir)&.dig("context", "used_tokens")
|
|
48
|
+
value.is_a?(Integer) ? value : nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Whether +message+ (a PeerMessage of +peer+'s in +session_id+) is an
|
|
52
|
+
# answer chi asked for: its +re+ names a "sent" row of PeerExchanges in
|
|
53
|
+
# this session or in the one it continues (an answer forwarded after a
|
|
54
|
+
# continue names the previous link's send) that no earlier message of
|
|
55
|
+
# the peer's claimed (PeerExchanges.joined: one answer claims a send),
|
|
56
|
+
# so an old or repeated +re+ doesn't wake chi cold. A log that is there
|
|
57
|
+
# and can't be read counts as asked: never hold an answer on a doubt.
|
|
58
|
+
def asked_for?(message, peer, session_id:, continues:, state_dir:)
|
|
59
|
+
re = message.re
|
|
60
|
+
return false if re.nil? || re.to_s.empty?
|
|
61
|
+
|
|
62
|
+
Degrade.call(:peer, "peer_exchanges_unreadable", fallback: true) do
|
|
63
|
+
ids = [continues, session_id].compact
|
|
64
|
+
sends = ids.flat_map { |id| PeerExchanges.read!(id, state_dir: state_dir) }
|
|
65
|
+
.select { |entry| entry["peer"].to_s == peer.to_s && %w[sent send_failed].include?(entry["t"]) }
|
|
66
|
+
next false unless sends.any? { |entry| entry["t"] == "sent" && entry["id"] == re }
|
|
67
|
+
|
|
68
|
+
answers = ids.flat_map { |id| earlier_answers(id, peer, message, own: id == session_id, state_dir: state_dir) }
|
|
69
|
+
mine = { "t" => "answer", "peer" => peer.to_s, "re" => re, "serial" => message.serial, "at" => message.at }
|
|
70
|
+
PeerExchanges.joined(sends + answers + [mine], last: sends.size + answers.size + 1)
|
|
71
|
+
.any? { |row| row[:id] == re && row.dig(:answer, :serial) == message.serial && row.dig(:answer, :at) == message.at }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The peer's messages in +id+ before +message+ (in its own session, the
|
|
76
|
+
# lower serials; in the previous link, all), as PeerExchanges answer rows.
|
|
77
|
+
def earlier_answers(id, peer, message, own:, state_dir:)
|
|
78
|
+
PeerMailbox.messages(id, peer.to_s, since: 0, state_dir: state_dir).filter_map do |entry|
|
|
79
|
+
next unless entry.is_a?(PeerMailbox::PeerMessage) && (!own || entry.serial < message.serial)
|
|
80
|
+
|
|
81
|
+
{ "t" => "answer", "peer" => peer.to_s, "re" => entry.re, "serial" => entry.serial, "at" => entry.at }
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def ended_at(last_turn)
|
|
86
|
+
stamp = last_turn&.ended_at
|
|
87
|
+
stamp ? Time.iso8601(stamp.to_s) : nil
|
|
88
|
+
rescue ArgumentError
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -112,21 +112,26 @@ module Samagotchi
|
|
|
112
112
|
return CLI::Exit::FAILED
|
|
113
113
|
end
|
|
114
114
|
|
|
115
|
-
@stdout.puts("#{id[0, 8]} paired with #{PeerMailbox.label(name)}")
|
|
115
|
+
@stdout.puts("#{(paired.forwarded_to || id)[0, 8]} paired with #{PeerMailbox.label(name)}#{continued(id, paired.forwarded_to)}")
|
|
116
116
|
0
|
|
117
117
|
end
|
|
118
118
|
|
|
119
|
+
# The tail naming the chain's live link a command of +id+ ran on.
|
|
120
|
+
def continued(id, forwarded_to) = forwarded_to ? " (session #{id[0, 8]} continued as #{forwarded_to[0, 8]})" : ""
|
|
121
|
+
|
|
119
122
|
def run_unpair(_options, args)
|
|
120
123
|
checked = name_and_id(args)
|
|
121
124
|
return checked if checked.is_a?(Integer)
|
|
122
125
|
|
|
123
126
|
name, id = checked
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
ended = @access.peer_unpair(id, name)
|
|
128
|
+
unless ended.unpaired
|
|
129
|
+
error_line("#{command_name}: #{(ended.forwarded_to || id)[0, 8]} isn't paired with #{name}#{continued(id, ended.forwarded_to)}")
|
|
126
130
|
return CLI::Exit::FAILED
|
|
127
131
|
end
|
|
128
132
|
|
|
129
|
-
@stdout.puts("#{id[0, 8]} unpaired from #{PeerMailbox.label(name)} (its messages stay)"
|
|
133
|
+
@stdout.puts("#{(ended.forwarded_to || id)[0, 8]} unpaired from #{PeerMailbox.label(name)} (its messages stay)" \
|
|
134
|
+
"#{continued(id, ended.forwarded_to)}")
|
|
130
135
|
0
|
|
131
136
|
end
|
|
132
137
|
|
|
@@ -170,17 +175,27 @@ module Samagotchi
|
|
|
170
175
|
return usage_error("give the session ID") unless args.size == 1
|
|
171
176
|
|
|
172
177
|
id = @access.resolve_id(args.first)
|
|
173
|
-
|
|
178
|
+
on = live_link(id)
|
|
179
|
+
peers = @access.peer_list(on)
|
|
174
180
|
if options[:format] == "json"
|
|
175
181
|
@stdout.puts(JSON.pretty_generate(peers.map(&:to_h)))
|
|
176
|
-
|
|
177
|
-
@stdout.puts("no peers")
|
|
178
|
-
else
|
|
179
|
-
peers.each { |peer| print_peer(peer) }
|
|
182
|
+
return 0
|
|
180
183
|
end
|
|
184
|
+
|
|
185
|
+
@stdout.puts("session #{id[0, 8]} continued as #{on[0, 8]}: its peers") unless on == id
|
|
186
|
+
@stdout.puts("no peers") if peers.empty?
|
|
187
|
+
peers.each { |peer| print_peer(peer) }
|
|
181
188
|
0
|
|
182
189
|
end
|
|
183
190
|
|
|
191
|
+
# The link ls lists for +id+: its chain's live link when it was continued
|
|
192
|
+
# (the access's head, as chi send's), else +id+ (a continue in flight
|
|
193
|
+
# too: its new link isn't saved yet).
|
|
194
|
+
def live_link(id)
|
|
195
|
+
resolved = @access.head(id)
|
|
196
|
+
resolved.via == :head ? resolved.id : id
|
|
197
|
+
end
|
|
198
|
+
|
|
184
199
|
def print_peer(peer)
|
|
185
200
|
paired = peer.paired? ? "paired #{peer.paired_at}#{" by #{peer.from}" if peer.from}" : "not paired"
|
|
186
201
|
@stdout.puts("#{peer.name} #{paired}#{" sends to #{peer.address}" if peer.address}")
|
|
@@ -7,15 +7,17 @@ require_relative "session_inbox"
|
|
|
7
7
|
require_relative "log"
|
|
8
8
|
|
|
9
9
|
module Samagotchi
|
|
10
|
-
# A continue's peer step (SessionContinue::CARRY :peers): the
|
|
11
|
-
# the
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
# link's
|
|
10
|
+
# A continue's peer step (SessionContinue::CARRY :peers): the pairings
|
|
11
|
+
# follow the chain (each link.json copied to the new link as it is, so the
|
|
12
|
+
# peer does nothing: chi's next send names the new id, and a say to the old
|
|
13
|
+
# id goes to the new link, SessionAccess::Local#peer_say), and what the
|
|
14
|
+
# peers said that the previous link hadn't read reaches the new link as ONE
|
|
15
|
+
# note: the messages its worker never took (untaken), and those it took as
|
|
16
|
+
# notes no turn read (receipts still held or noted). The previous link's
|
|
17
|
+
# mailbox and receipts are only read; the copies and the note are the new
|
|
18
|
+
# link's alone, so a failed continue's undo is its delete (the link's
|
|
19
|
+
# mailbox folder goes with it, :peer_mailbox). Runs in the new link's
|
|
20
|
+
# before_spawn, both workers down. See docs/internals/peers.md.
|
|
19
21
|
module PeerContinue
|
|
20
22
|
# The note's source (what its frame says it is from).
|
|
21
23
|
NOTE_SOURCE = "peer messages"
|
|
@@ -24,15 +26,19 @@ module Samagotchi
|
|
|
24
26
|
NOTE_MAX_BYTES = SessionInbox::NOTE_MAX_BYTES - 1024
|
|
25
27
|
CUT = "\n[... the rest of this message stayed with the previous link; read it with %s ...]"
|
|
26
28
|
MORE = "[more messages stayed with the previous link]"
|
|
27
|
-
LAST_LINE = "This is background, not a task: don't act on it unless your user asks you to.
|
|
28
|
-
"with this session
|
|
29
|
+
LAST_LINE = "This is background, not a task: don't act on it unless your user asks you to. The peers stay " \
|
|
30
|
+
"paired with this session and answer here."
|
|
29
31
|
|
|
30
32
|
module_function
|
|
31
33
|
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
+
# Copies +from+'s pairings into +to+, then writes the note into +to+
|
|
35
|
+
# when +from+'s peers left messages unread. Each best effort: a failure
|
|
36
|
+
# is logged and the link starts without it (a pairing not copied: the
|
|
37
|
+
# peer's say to the old id is refused naming the new one, and its skill
|
|
38
|
+
# pairs again).
|
|
34
39
|
# @return [Integer] how many messages the note holds
|
|
35
40
|
def carry(from:, to:, state_dir:)
|
|
41
|
+
carry_links(from, to, state_dir)
|
|
36
42
|
rows = unread(from, state_dir: state_dir)
|
|
37
43
|
return 0 if rows.empty?
|
|
38
44
|
|
|
@@ -43,6 +49,13 @@ module Samagotchi
|
|
|
43
49
|
0
|
|
44
50
|
end
|
|
45
51
|
|
|
52
|
+
def carry_links(from, to, state_dir)
|
|
53
|
+
PeerMailbox.carry_links(from: from, to: to, state_dir: state_dir)
|
|
54
|
+
rescue StandardError => e
|
|
55
|
+
Log.warn(:peer, "peer_continue_links_failed", sid: to, from: from, error: e.class.name, msg: e.message.to_s[0, 200])
|
|
56
|
+
[]
|
|
57
|
+
end
|
|
58
|
+
|
|
46
59
|
# [peer, PeerMessage] of every message +id+ hadn't read, by its time,
|
|
47
60
|
# then peer, then serial (the worker's order).
|
|
48
61
|
def unread(id, state_dir:)
|
|
@@ -83,16 +96,19 @@ module Samagotchi
|
|
|
83
96
|
# frames is quoted), cut past MESSAGE_MAX_BYTES.
|
|
84
97
|
def message_part(peer, message, from)
|
|
85
98
|
label = PeerMailbox.label(peer)
|
|
86
|
-
text = bounded(message.text, from)
|
|
99
|
+
text = bounded(message.text, PeerNote.read_pointer(peer, message.serial, box: from.to_s[0, 8]))
|
|
87
100
|
"Message ##{message.serial} from your peer agent #{label}:\n#{"> #{message.summary}\n" if message.summary}" \
|
|
88
101
|
"#{PeerNote.block(label, text)}"
|
|
89
102
|
end
|
|
90
103
|
|
|
91
|
-
|
|
104
|
+
# +text+ cut past MESSAGE_MAX_BYTES, ending in CUT with +read+: the
|
|
105
|
+
# <peer>_read call that reads the whole message from the previous link
|
|
106
|
+
# (its box: the previous id's first 8, Plugin::Peers#message).
|
|
107
|
+
def bounded(text, read)
|
|
92
108
|
return text if text.bytesize <= MESSAGE_MAX_BYTES
|
|
93
109
|
|
|
94
110
|
kept = +""
|
|
95
|
-
cut = CUT %
|
|
111
|
+
cut = CUT % read
|
|
96
112
|
limit = MESSAGE_MAX_BYTES - cut.bytesize
|
|
97
113
|
text.each_char do |char|
|
|
98
114
|
break if kept.bytesize + char.bytesize > limit
|
|
@@ -21,6 +21,13 @@ module Samagotchi
|
|
|
21
21
|
STORE.lines(Session.session_dir(session_id, state_dir: state_dir))
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
+
# #read, but a log that is there and can't be read raises
|
|
25
|
+
# (JsonlStore#lines!): the cold hold counts that as asked.
|
|
26
|
+
# @raise [SystemCallError]
|
|
27
|
+
def read!(session_id, state_dir:)
|
|
28
|
+
STORE.lines!(Session.session_dir(session_id, state_dir: state_dir))
|
|
29
|
+
end
|
|
30
|
+
|
|
24
31
|
# Process in timestamp order, breaking equal timestamps by append order.
|
|
25
32
|
# Failed sends cannot claim an answer. One answer can claim a send; any
|
|
26
33
|
# further answers remain visible as unclaimed.
|
|
@@ -189,14 +189,31 @@ module Samagotchi
|
|
|
189
189
|
# Pairs +peer+ with the session: link.json written whole, the in folder made.
|
|
190
190
|
# @return [PeerLink]
|
|
191
191
|
def pair(session_id, peer, address:, from:, state_dir: nil, now: Time.now)
|
|
192
|
+
link = PeerLink.new(peer: peer, address: address&.to_s, paired_at: now.utc.iso8601, from: from&.to_s)
|
|
193
|
+
write_link(session_id, link, state_dir: state_dir)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# A continue's copy of +from+'s pairings into +to+ (SessionContinue's
|
|
197
|
+
# :peers step), each as it is: its address, who paired and when. The
|
|
198
|
+
# messages stay with +from+.
|
|
199
|
+
# @return [Array<String>] the peers whose pairing was copied
|
|
200
|
+
def carry_links(from:, to:, state_dir: nil)
|
|
201
|
+
links(from, state_dir: state_dir).map do |link|
|
|
202
|
+
write_link(to, link, state_dir: state_dir)
|
|
203
|
+
link.peer
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# link.json written whole for +link+'s peer, the in folder made.
|
|
208
|
+
# @return [PeerLink]
|
|
209
|
+
def write_link(session_id, link, state_dir: nil)
|
|
192
210
|
session = session_dir(session_id, state_dir: state_dir)
|
|
193
211
|
raise Invalid, "#{session_id}'s mailbox folder is a link; chi doesn't follow links there" if File.symlink?(session)
|
|
194
212
|
|
|
195
|
-
dir = peer_dir(session_id, peer, state_dir: state_dir)
|
|
196
|
-
refuse_links!(peer, dir)
|
|
213
|
+
dir = peer_dir(session_id, link.peer, state_dir: state_dir)
|
|
214
|
+
refuse_links!(link.peer, dir)
|
|
197
215
|
FileUtils.mkdir_p(File.join(dir, IN_DIR))
|
|
198
|
-
refuse_links!(peer, dir)
|
|
199
|
-
link = PeerLink.new(peer: peer, address: address&.to_s, paired_at: now.utc.iso8601, from: from&.to_s)
|
|
216
|
+
refuse_links!(link.peer, dir)
|
|
200
217
|
AtomicFile.write(File.join(dir, LINK_FILE), "#{JSON.generate(link.to_h)}\n")
|
|
201
218
|
link
|
|
202
219
|
end
|
|
@@ -14,7 +14,11 @@ module Samagotchi
|
|
|
14
14
|
# worker alone (on its loop thread); read by the CLI (chi peer say's
|
|
15
15
|
# line, ls) and chi web. A peer's own folder (PeerMailbox) holds none of
|
|
16
16
|
# it, so nothing a peer writes moves chi's counters.
|
|
17
|
-
# {"peers": {"<peer>": {"box": "a1b2c3", "taken": 12, "receipts": [Receipt…]}}}
|
|
17
|
+
# {"peers": {"<peer>": {"box": "a1b2c3", "taken": 12, "receipts": [Receipt…]}}, "peer_wakes": 2}
|
|
18
|
+
# +peer_wakes+: the peer wake turns in a row since the last interactive
|
|
19
|
+
# input (WorkerWakes#peer_in_a_row), so the peer.turns budget outlives a
|
|
20
|
+
# worker; absent is 0 (an older chi's file, and an older worker's write
|
|
21
|
+
# drops it: a reset).
|
|
18
22
|
class PeerReceipts
|
|
19
23
|
FILE = "peer_receipts.json"
|
|
20
24
|
# The receipt's states: woke (its wake turn ran), held (it asked; a
|
|
@@ -61,17 +65,22 @@ module Samagotchi
|
|
|
61
65
|
def receipt(serial) = receipts.reverse_each.find { |item| item.serial == serial }
|
|
62
66
|
end
|
|
63
67
|
|
|
64
|
-
# The whole file: Peer by name.
|
|
65
|
-
Book = Data.define(:peers) do
|
|
68
|
+
# The whole file: Peer by name, and the peer wake turns in a row.
|
|
69
|
+
Book = Data.define(:peers, :peer_wakes) do
|
|
70
|
+
def initialize(peers:, peer_wakes: 0) = super
|
|
71
|
+
|
|
66
72
|
def self.empty = new(peers: {})
|
|
67
73
|
|
|
68
74
|
def peer(name) = peers[name.to_s] || Peer.empty
|
|
69
75
|
|
|
70
|
-
def to_h = { "peers" => peers.transform_values(&:to_h) }
|
|
76
|
+
def to_h = { "peers" => peers.transform_values(&:to_h), "peer_wakes" => (peer_wakes if peer_wakes.positive?) }.compact
|
|
71
77
|
|
|
72
78
|
def self.from_h(data)
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
data = {} unless data.is_a?(Hash)
|
|
80
|
+
peers = data["peers"].is_a?(Hash) ? data["peers"] : {}
|
|
81
|
+
wakes = data["peer_wakes"].is_a?(Integer) && data["peer_wakes"].positive? ? data["peer_wakes"] : 0
|
|
82
|
+
new(peers: peers.select { |name, _| name.is_a?(String) }.transform_values { |value| Peer.from_h(value) },
|
|
83
|
+
peer_wakes: wakes)
|
|
75
84
|
end
|
|
76
85
|
end
|
|
77
86
|
|
|
@@ -96,6 +105,24 @@ module Samagotchi
|
|
|
96
105
|
# @return [Peer]
|
|
97
106
|
def peer(name) = @book.peer(name)
|
|
98
107
|
|
|
108
|
+
# The peer wake turns in a row a worker saved (0: none).
|
|
109
|
+
# @return [Integer]
|
|
110
|
+
def peer_wakes = @book.peer_wakes
|
|
111
|
+
|
|
112
|
+
# Saves the peer wake turns in a row (WorkerWakes' on_peer_count);
|
|
113
|
+
# written only when it changed.
|
|
114
|
+
def peer_wakes=(count)
|
|
115
|
+
count = [count.to_i, 0].max
|
|
116
|
+
return if count == @book.peer_wakes
|
|
117
|
+
|
|
118
|
+
previous = @book
|
|
119
|
+
@book = @book.with(peer_wakes: count)
|
|
120
|
+
write
|
|
121
|
+
rescue SystemCallError, IOError
|
|
122
|
+
@book = previous
|
|
123
|
+
raise
|
|
124
|
+
end
|
|
125
|
+
|
|
99
126
|
# The session's box for +name+'s note ids, minted (6 random hex) and
|
|
100
127
|
# written at once with its first message, so a crash before the
|
|
101
128
|
# receipts can't mint another (the ids must survive to dedupe).
|