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.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -1
  3. data/docs/architecture.md +3 -3
  4. data/docs/bundles.md +45 -28
  5. data/docs/cli.md +22 -8
  6. data/docs/configuration.md +4 -3
  7. data/docs/context.md +8 -5
  8. data/docs/guardrails.md +9 -4
  9. data/docs/internals/bridge.md +3 -1
  10. data/docs/internals/bundles.md +2 -2
  11. data/docs/internals/pages.md +4 -4
  12. data/docs/internals/participants.md +5 -3
  13. data/docs/internals/peers.md +75 -22
  14. data/docs/internals/prompt-caching.md +2 -1
  15. data/docs/internals/remote-sessions.md +6 -0
  16. data/docs/internals/session-lifecycle.md +3 -2
  17. data/docs/internals/web-frontend.md +91 -19
  18. data/docs/internals/working-set.md +5 -1
  19. data/docs/peers.md +71 -22
  20. data/docs/plugins.md +7 -4
  21. data/docs/releasing.md +30 -2
  22. data/docs/scheduling.md +3 -2
  23. data/docs/sessions.md +1 -1
  24. data/docs/testing.md +35 -3
  25. data/docs/thin-clients.md +27 -11
  26. data/lib/samagotchi/bundles/claude-peer/manifest.yml +2 -2
  27. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +33 -17
  28. data/lib/samagotchi/bundles/codex-peer/manifest.yml +2 -2
  29. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +34 -17
  30. data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
  31. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
  32. data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
  33. data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
  34. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  35. data/lib/samagotchi/compat.rb +8 -1
  36. data/lib/samagotchi/config.rb +4 -0
  37. data/lib/samagotchi/continue_offer.rb +13 -1
  38. data/lib/samagotchi/delivery.rb +2 -2
  39. data/lib/samagotchi/engine/models.rb +17 -0
  40. data/lib/samagotchi/engine/plugins.rb +2 -0
  41. data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
  42. data/lib/samagotchi/jsonl_store.rb +13 -2
  43. data/lib/samagotchi/llm/anthropic_messages.rb +11 -0
  44. data/lib/samagotchi/llm/openai_chat.rb +5 -0
  45. data/lib/samagotchi/llm/prompt_cache.rb +5 -0
  46. data/lib/samagotchi/pages/launch.rb +1 -1
  47. data/lib/samagotchi/peer_cold.rb +92 -0
  48. data/lib/samagotchi/peer_command.rb +24 -9
  49. data/lib/samagotchi/peer_continue.rb +32 -16
  50. data/lib/samagotchi/peer_exchanges.rb +7 -0
  51. data/lib/samagotchi/peer_mailbox.rb +21 -4
  52. data/lib/samagotchi/peer_receipts.rb +33 -6
  53. data/lib/samagotchi/peer_say.rb +34 -16
  54. data/lib/samagotchi/plugin/api.rb +2 -1
  55. data/lib/samagotchi/plugin/context.rb +13 -1
  56. data/lib/samagotchi/plugin/peer.rb +118 -53
  57. data/lib/samagotchi/plugin/peers.rb +13 -6
  58. data/lib/samagotchi/session_access/local.rb +98 -37
  59. data/lib/samagotchi/session_access/remote_peers.rb +3 -3
  60. data/lib/samagotchi/session_chain/links.rb +8 -3
  61. data/lib/samagotchi/session_continue.rb +7 -6
  62. data/lib/samagotchi/session_manager/spawn.rb +6 -1
  63. data/lib/samagotchi/session_stores.rb +16 -11
  64. data/lib/samagotchi/system_one/runs.rb +5 -2
  65. data/lib/samagotchi/testing/boundary.rb +0 -1
  66. data/lib/samagotchi/testing/host.rb +1 -0
  67. data/lib/samagotchi/testing/recorder.rb +3 -0
  68. data/lib/samagotchi/version.rb +1 -1
  69. data/lib/samagotchi/web/peer_routes.rb +10 -5
  70. data/lib/samagotchi/web/public/agent_rows.js +206 -0
  71. data/lib/samagotchi/web/public/agent_runs.js +595 -0
  72. data/lib/samagotchi/web/public/answer_fold.js +41 -44
  73. data/lib/samagotchi/web/public/app.js +3 -3
  74. data/lib/samagotchi/web/public/block_fix.js +1 -1
  75. data/lib/samagotchi/web/public/cards_place.js +6 -4
  76. data/lib/samagotchi/web/public/history_reveal.js +5 -2
  77. data/lib/samagotchi/web/public/index.html +38 -2
  78. data/lib/samagotchi/web/public/page_view.js +1 -1
  79. data/lib/samagotchi/web/public/pages.css +1 -7
  80. data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
  81. data/lib/samagotchi/web/public/peer_note.js +4 -2
  82. data/lib/samagotchi/web/public/peer_row.js +37 -0
  83. data/lib/samagotchi/web/public/quiet_notes.js +12 -1
  84. data/lib/samagotchi/web/public/revise_stack.js +5 -2
  85. data/lib/samagotchi/web/public/send_turn.js +11 -3
  86. data/lib/samagotchi/web/public/turn_block.js +4 -0
  87. data/lib/samagotchi/worker/mail.rb +11 -1
  88. data/lib/samagotchi/worker.rb +7 -5
  89. data/lib/samagotchi/worker_inbound/mail.rb +34 -6
  90. data/lib/samagotchi/worker_inbound.rb +11 -3
  91. data/lib/samagotchi/worker_link.rb +13 -1
  92. data/lib/samagotchi/worker_wakes.rb +33 -5
  93. metadata +5 -2
  94. data/lib/samagotchi/web/public/pages_group.js +0 -67
@@ -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
- unless @access.peer_unpair(id, name)
125
- error_line("#{command_name}: #{id[0, 8]} isn't paired with #{name}")
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
- peers = @access.peer_list(id)
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
- elsif peers.empty?
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 pairing and
11
- # the mailbox stay with the previous link (the peer is told the new id,
12
- # Plugin::Peer#tell_moved, and pairs again), and what the peers said that
13
- # the previous link hadn't read reaches the new link as ONE note: the
14
- # messages its worker never took (untaken), and those it took as notes no
15
- # turn read (receipts still held or noted). Read only: nothing in the
16
- # previous link's mailbox or receipts changes, so a failed continue has
17
- # nothing to undo (the note goes with the failed link). Runs in the new
18
- # link's before_spawn, both workers down. See docs/internals/peers.md.
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. A peer pairs again " \
28
- "with this session to answer here."
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
- # Writes the note into +to+ when +from+'s peers left messages unread.
33
- # Best effort: a failure is logged and the link starts without it.
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
- def bounded(text, from)
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 % "chi peer ls #{from}"
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
- peers = data.is_a?(Hash) && data["peers"].is_a?(Hash) ? data["peers"] : {}
74
- new(peers: peers.select { |name, _| name.is_a?(String) }.transform_values { |value| Peer.from_h(value) })
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).
@@ -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
- PeerSay = Data.define(:status, :serial, :wake, :receipt, :earlier, :next_id, :turns, :in_flight) do
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) }.compact
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, once the new link is saved when its
78
- # continue still runs.
79
- def self.continued(next_id, in_flight, what)
80
- "#{SessionChain::Continued.new(nil, next_id, in_flight: in_flight).message}: #{what}#{" in a moment" if in_flight}"
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
- PeerPaired = Data.define(:status, :link, :next_id, :in_flight) do
131
- def initialize(status:, link: nil, next_id: nil, in_flight: false) = super
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