samagotchi 0.193.0 → 0.194.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -1
  3. data/docs/bundle-authoring.md +1 -1
  4. data/docs/bundles.md +37 -12
  5. data/docs/cli.md +26 -10
  6. data/docs/configuration.md +71 -5
  7. data/docs/desktop.md +3 -0
  8. data/docs/guardrails.md +11 -1
  9. data/docs/hooks.md +1 -0
  10. data/docs/internals/engine.md +46 -3
  11. data/docs/internals/panes.md +66 -0
  12. data/docs/internals/peers.md +101 -5
  13. data/docs/internals/prompt-caching.md +4 -0
  14. data/docs/internals/session-lifecycle.md +9 -2
  15. data/docs/internals/web-frontend.md +16 -4
  16. data/docs/peers.md +79 -5
  17. data/docs/plugins.md +42 -4
  18. data/docs/sessions.md +15 -6
  19. data/docs/sub-agent.md +6 -4
  20. data/docs/system-one.md +4 -0
  21. data/docs/testing.md +3 -0
  22. data/lib/samagotchi/bridge/card_store.rb +1 -1
  23. data/lib/samagotchi/broadcast/triage_model.rb +17 -0
  24. data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
  25. data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
  26. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +28 -7
  27. data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
  28. data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
  29. data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
  30. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +23 -2
  31. data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
  32. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  33. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  34. data/lib/samagotchi/chi_invocation.rb +7 -0
  35. data/lib/samagotchi/client.rb +18 -0
  36. data/lib/samagotchi/codex_rollout.rb +93 -0
  37. data/lib/samagotchi/config.rb +15 -6
  38. data/lib/samagotchi/effective_model.rb +28 -2
  39. data/lib/samagotchi/engine/cards.rb +9 -4
  40. data/lib/samagotchi/engine/hooks.rb +8 -0
  41. data/lib/samagotchi/engine/models.rb +180 -7
  42. data/lib/samagotchi/engine/side_requests.rb +72 -13
  43. data/lib/samagotchi/engine/toolkit.rb +1 -1
  44. data/lib/samagotchi/engine/turn_helpers.rb +1 -1
  45. data/lib/samagotchi/engine.rb +11 -8
  46. data/lib/samagotchi/engine_snapshots.rb +16 -3
  47. data/lib/samagotchi/events.rb +1 -0
  48. data/lib/samagotchi/hooks.rb +3 -1
  49. data/lib/samagotchi/host_registry.rb +146 -16
  50. data/lib/samagotchi/idle_client.rb +13 -2
  51. data/lib/samagotchi/idle_recap.rb +33 -3
  52. data/lib/samagotchi/idle_target.rb +27 -7
  53. data/lib/samagotchi/llm/adapter_common.rb +4 -2
  54. data/lib/samagotchi/llm/anthropic_messages.rb +3 -2
  55. data/lib/samagotchi/llm/errors.rb +35 -4
  56. data/lib/samagotchi/llm/model_listing.rb +19 -0
  57. data/lib/samagotchi/llm/openai_chat.rb +3 -2
  58. data/lib/samagotchi/model_binding.rb +46 -4
  59. data/lib/samagotchi/model_catalog.rb +68 -8
  60. data/lib/samagotchi/model_profile.rb +35 -0
  61. data/lib/samagotchi/models_command.rb +8 -4
  62. data/lib/samagotchi/panes/kitty.rb +122 -0
  63. data/lib/samagotchi/panes.rb +185 -0
  64. data/lib/samagotchi/peer_announce.rb +245 -0
  65. data/lib/samagotchi/peer_command.rb +97 -11
  66. data/lib/samagotchi/peer_continue.rb +10 -0
  67. data/lib/samagotchi/peer_mailbox.rb +30 -7
  68. data/lib/samagotchi/peer_pair_here.rb +149 -0
  69. data/lib/samagotchi/peer_presence.rb +434 -0
  70. data/lib/samagotchi/peer_say.rb +17 -10
  71. data/lib/samagotchi/peer_wait_expiry.rb +47 -0
  72. data/lib/samagotchi/plugin/api.rb +12 -1
  73. data/lib/samagotchi/plugin/card.rb +68 -11
  74. data/lib/samagotchi/plugin/context.rb +20 -1
  75. data/lib/samagotchi/plugin/panes.rb +54 -0
  76. data/lib/samagotchi/plugin/peer.rb +52 -10
  77. data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
  78. data/lib/samagotchi/plugin/peer_start.rb +301 -0
  79. data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
  80. data/lib/samagotchi/plugin/peers.rb +76 -3
  81. data/lib/samagotchi/recap_setup.rb +40 -3
  82. data/lib/samagotchi/self_report.rb +25 -6
  83. data/lib/samagotchi/send_command.rb +6 -6
  84. data/lib/samagotchi/served_lookup.rb +225 -0
  85. data/lib/samagotchi/served_ref.rb +41 -0
  86. data/lib/samagotchi/session_access/contract.rb +6 -0
  87. data/lib/samagotchi/session_access/local.rb +54 -6
  88. data/lib/samagotchi/session_access/remote.rb +4 -4
  89. data/lib/samagotchi/session_access/remote_peers.rb +7 -4
  90. data/lib/samagotchi/session_commands.rb +40 -8
  91. data/lib/samagotchi/session_continue.rb +6 -2
  92. data/lib/samagotchi/session_lifecycle/allowed.rb +4 -2
  93. data/lib/samagotchi/session_manager/delivery.rb +5 -0
  94. data/lib/samagotchi/session_manager/lifecycle.rb +58 -7
  95. data/lib/samagotchi/session_manager/removal.rb +3 -0
  96. data/lib/samagotchi/session_model_settings.rb +7 -3
  97. data/lib/samagotchi/session_stores.rb +9 -0
  98. data/lib/samagotchi/sessions_command.rb +16 -3
  99. data/lib/samagotchi/system_one/http_backend.rb +111 -10
  100. data/lib/samagotchi/system_prompt.rb +11 -4
  101. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -1
  102. data/lib/samagotchi/terminal_ui/formatting.rb +26 -12
  103. data/lib/samagotchi/terminal_ui/status_row.rb +20 -3
  104. data/lib/samagotchi/terminal_ui.rb +1 -1
  105. data/lib/samagotchi/testing/bundle.rb +6 -2
  106. data/lib/samagotchi/turn_endings.rb +6 -2
  107. data/lib/samagotchi/user_typed.rb +29 -0
  108. data/lib/samagotchi/version.rb +1 -1
  109. data/lib/samagotchi/web/app.rb +8 -20
  110. data/lib/samagotchi/web/participant_review.rb +1 -1
  111. data/lib/samagotchi/web/participant_view.rb +1 -1
  112. data/lib/samagotchi/web/peer_routes.rb +33 -13
  113. data/lib/samagotchi/web/public/app.js +1 -1
  114. data/lib/samagotchi/web/public/card.js +26 -10
  115. data/lib/samagotchi/web/public/context_chips.js +24 -2
  116. data/lib/samagotchi/web/public/copy.js +8 -2
  117. data/lib/samagotchi/web/public/data.js +2 -1
  118. data/lib/samagotchi/web/public/format.js +9 -1
  119. data/lib/samagotchi/web/public/index.html +18 -0
  120. data/lib/samagotchi/web/public/info_bar_model.js +1 -1
  121. data/lib/samagotchi/web/public/model_pick.js +18 -5
  122. data/lib/samagotchi/web/public/model_picker.js +1 -1
  123. data/lib/samagotchi/web/public/peer_chip.js +15 -5
  124. data/lib/samagotchi/web/public/peer_data.js +4 -2
  125. data/lib/samagotchi/web/public/peer_wait.js +36 -0
  126. data/lib/samagotchi/web/public/served_ref.js +9 -0
  127. data/lib/samagotchi/web/public/session_fields.js +1 -0
  128. data/lib/samagotchi/web/public/session_state.js +4 -0
  129. data/lib/samagotchi/web/public/stage_missing.js +23 -6
  130. data/lib/samagotchi/web/public/stream_handlers.js +16 -0
  131. data/lib/samagotchi/web/public/version_notices.js +7 -1
  132. data/lib/samagotchi/web/session_control.rb +19 -10
  133. data/lib/samagotchi/web/session_show.rb +20 -0
  134. data/lib/samagotchi/web/update_ops.rb +2 -2
  135. data/lib/samagotchi/worker/command_queue.rb +3 -0
  136. data/lib/samagotchi/worker/mail.rb +14 -1
  137. data/lib/samagotchi/worker.rb +1 -0
  138. metadata +18 -1
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "chi_invocation"
3
4
  require_relative "peer_mailbox"
4
5
  require_relative "peer_receipts"
5
6
  require_relative "session_chain/successor"
@@ -35,11 +36,13 @@ module Samagotchi
35
36
 
36
37
  def received? = status == :received
37
38
 
38
- # The line for +peer+ (its name): "#12 received · woke chi".
39
- def line(peer)
39
+ # The line for +peer+ (its name): "#12 received · woke chi". +id+: the
40
+ # session said to, and +via+ how its commands are spelled where the
41
+ # line is drawn: a stopped session's line names its chi --resume.
42
+ def line(peer, id: nil, via: ChiInvocation::HERE)
40
43
  return PeerSay.refusal(status, peer, next_id, in_flight: in_flight) unless received?
41
44
 
42
- parts = ["##{serial} received", tail(peer)]
45
+ parts = ["##{serial} received", tail(peer, forwarded_to || id, via)]
43
46
  parts << "also held: #{PeerSay.held_list(earlier)}" unless earlier.empty?
44
47
  parts << "session continued as #{forwarded_to[0, 8]}: answer there" if forwarded_to
45
48
  parts.join(" · ")
@@ -98,7 +101,8 @@ module Samagotchi
98
101
 
99
102
  private
100
103
 
101
- def tail(peer)
104
+ def tail(peer, id, via)
105
+ return "#{PeerSay::AT_ONCE.fetch(wake)} (#{via.resume(id[0, 8])})" if wake == :stopped && id
102
106
  return PeerSay::AT_ONCE.fetch(wake) if wake
103
107
  return "no answer from chi yet" unless receipt
104
108
 
@@ -149,11 +153,12 @@ module Samagotchi
149
153
  end
150
154
  end
151
155
 
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
156
+ # What chi peer unpair did: whether there was a pairing (+unpaired+),
157
+ # whether a wait for the peer ended (+waited+: /claude pair's mark,
158
+ # PeerPresence) and, when the id was continued, the chain's live link it
159
+ # ended (+forwarded_to+).
160
+ PeerUnpaired = Data.define(:unpaired, :forwarded_to, :waited) do
161
+ def initialize(unpaired:, forwarded_to: nil, waited: false) = super
157
162
  end
158
163
 
159
164
  PeerSay::STATUSES = %w[received gone deleting continued busy not_paired old_worker full].freeze
@@ -165,7 +170,9 @@ module Samagotchi
165
170
  PeerSay::AT_ONCE = {
166
171
  running: "chi is in a turn: it reads this when the turn ends",
167
172
  starting: "chi is starting",
168
- stopped: "chi is stopped: it reads this at its next start",
173
+ # Only its owner's message starts it again (a peer's never does): the
174
+ # line adds its chi --resume when it knows the session.
175
+ stopped: "chi is stopped: it reads this when its user starts it",
169
176
  archived: "this session is archived: it reads this if you reopen it",
170
177
  owned: "a one-turn chi run has this session: it reads this at its next start",
171
178
  start_failed: "chi's worker isn't running and couldn't be started from here: it reads this at its next start",
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "peer_mailbox"
4
+ require_relative "peer_presence"
5
+
6
+ module Samagotchi
7
+ # The worker's check for a wait that ran out (/claude pair's mark,
8
+ # PeerPresence; owner decision 7): at the worker's start and on each idle
9
+ # pass (between turns, Worker#idle_pass), this session's marks past their
10
+ # 10 minutes are removed and, once per mark, +on_ended+ gets the peer: the
11
+ # Engine fires :peer_wait_ended, and the peer's bundle says "No Claude
12
+ # Code session came in 10 min; /claude pair to wait again." (ctx.notify:
13
+ # a :hook_notice kept in cards.json; never a message, no turn). A restart
14
+ # within or after the 10 minutes is the same check. Only this worker
15
+ # removes a timed-out mark (its unpair and its delete too): every other
16
+ # reader treats it as absent, so the line isn't lost. A mark of a session
17
+ # paired meanwhile, or of a peer no loaded bundle declares, goes without
18
+ # a word.
19
+ class PeerWaitExpiry
20
+ # The folder is read at most this often (the pass runs on every wake).
21
+ EVERY_SECONDS = 5
22
+
23
+ # @param registry [PeerRegistry] the peers the loaded bundles declared
24
+ # @param on_ended [#call] gets the peer's name
25
+ def initialize(session_id:, state_dir:, registry:, on_ended:)
26
+ @session_id = session_id
27
+ @state_dir = state_dir
28
+ @registry = registry
29
+ @on_ended = on_ended
30
+ @looked_at = nil
31
+ end
32
+
33
+ def call(now: Time.now)
34
+ return if @looked_at && now - @looked_at < EVERY_SECONDS
35
+
36
+ @looked_at = now
37
+ PeerPresence.session_marks(@session_id, state_dir: @state_dir).each do |peer, mark|
38
+ next unless mark.stale?(now)
39
+ next unless PeerPresence.stop_waiting(peer, @session_id, state_dir: @state_dir)
40
+ next if PeerMailbox.link(@session_id, peer, state_dir: @state_dir)
41
+ next unless @registry&.declared?(peer)
42
+
43
+ @on_ended.call(peer)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -63,7 +63,18 @@ module Samagotchi
63
63
  "ctx.card(between_turns:)" => "0.183.0",
64
64
  "chi.peer" => "0.188.0",
65
65
  "ctx.peers" => "0.188.0",
66
- "ctx.turn_interactive?" => "0.193.0"
66
+ "ctx.turn_interactive?" => "0.193.0",
67
+ "ctx.card(rows:)" => "0.194.0",
68
+ "ctx.peers.pair" => "0.194.0",
69
+ "ctx.peers.unpair" => "0.194.0",
70
+ "ctx.peers.here" => "0.194.0",
71
+ "ctx.peers.wait" => "0.194.0",
72
+ "ctx.peers.waiting" => "0.194.0",
73
+ "ctx.peers.claim" => "0.194.0",
74
+ "ctx.peers.release" => "0.194.0",
75
+ "ctx.peers.stop_waiting" => "0.194.0",
76
+ "ctx.panes" => "0.194.0",
77
+ "ctx.user_typed" => "0.194.0"
67
78
  }.freeze
68
79
 
69
80
  # chi.on's on_error: values. :log (the default) logs a raise and goes
@@ -10,39 +10,96 @@ module Samagotchi
10
10
  module Card
11
11
  LEVELS = %i[info warn].freeze
12
12
  MAX_ACTIONS = 6
13
+ # Rows (a list to pick from: claude-peer's Claude Code sessions): each
14
+ # a title and a sub line, with up to ROW_ACTIONS buttons of its own,
15
+ # the actions that name it (row: its index). They count apart from
16
+ # the card's own MAX_ACTIONS.
17
+ MAX_ROWS = 8
18
+ ROW_ACTIONS = 3
19
+ ROW_TITLE_CHARS = 200
20
+ ROW_SUB_CHARS = 300
13
21
  # An authorize address can be longer than a ref's (RefAction::MAX_URL).
14
22
  MAX_URL = 4_096
15
23
 
16
24
  module_function
17
25
 
18
26
  # @return [Hash] {type: :card, id:, source:, title:, body:, level:,
19
- # actions: [{label:, command:} | {label:, url:}]}; a new id when none
20
- # is given. A +url+ action opens that http(s) address in a new tab
21
- # (the web) or prints it (the TUI); it never sends to the session.
22
- # @raise [ArgumentError] a card without a title, a bad level or action
23
- def build(source:, title:, body:, actions:, level:, id:)
27
+ # actions: [{label:, command:} | {label:, url:}], rows: (only when
28
+ # given) [{title:, sub:}]}; a new id when none is given. A +url+
29
+ # action opens that http(s) address in a new tab (the web) or prints
30
+ # it (the TUI); it never sends to the session. An action with
31
+ # +row:+ (an index into +rows+) is that row's button; +primary:+
32
+ # true draws it solid in the web.
33
+ # @raise [ArgumentError] a card without a title, a bad level, action or row
34
+ def build(source:, title:, body:, actions:, level:, id:, rows: nil)
24
35
  title = title.to_s.strip
25
36
  raise ArgumentError, "a card needs a title" if title.empty?
26
37
 
27
38
  level = level.to_s.to_sym
28
39
  raise ArgumentError, "card level must be one of #{LEVELS.join(", ")}" unless LEVELS.include?(level)
29
40
 
30
- actions = Array(actions)
31
- raise ArgumentError, "a card has at most #{MAX_ACTIONS} actions" if actions.size > MAX_ACTIONS
41
+ rows = rows.nil? ? nil : build_rows(rows)
42
+ actions = Array(actions).map { |action| build_action(action, rows: rows || []) }
43
+ own = actions.count { |action| !action.key?(:row) }
44
+ raise ArgumentError, "a card has at most #{MAX_ACTIONS} actions" if own > MAX_ACTIONS
45
+ if actions.filter_map { |action| action[:row] }.tally.values.any? { |count| count > ROW_ACTIONS }
46
+ raise ArgumentError, "a card has at most #{ROW_ACTIONS} actions in a row"
47
+ end
32
48
 
33
- actions = actions.map { |action| build_action(action) }
34
49
  id = id.to_s.strip
35
- { type: :card, id: id.empty? ? SecureRandom.hex(4) : id, source: source.to_s, title: title, body: body.to_s,
36
- level: level, actions: actions }
50
+ card = { type: :card, id: id.empty? ? SecureRandom.hex(4) : id, source: source.to_s, title: title, body: body.to_s,
51
+ level: level, actions: actions }
52
+ rows ? card.merge(rows: rows) : card
53
+ end
54
+
55
+ # @raise [ArgumentError] not an Array of {title:, sub:} Hashes, more
56
+ # than MAX_ROWS, a row with neither
57
+ def build_rows(rows)
58
+ raise ArgumentError, "a card's rows are an Array" unless rows.is_a?(Array)
59
+ raise ArgumentError, "a card has at most #{MAX_ROWS} rows" if rows.size > MAX_ROWS
60
+
61
+ rows.map do |row|
62
+ raise ArgumentError, "a card row is a Hash {title:, sub:}" unless row.is_a?(Hash)
63
+
64
+ row = row.transform_keys(&:to_sym)
65
+ title = one_line(row[:title], ROW_TITLE_CHARS)
66
+ sub = one_line(row[:sub], ROW_SUB_CHARS)
67
+ raise ArgumentError, "a row needs a title or a sub line" if title.empty? && sub.empty?
68
+
69
+ { title: title, sub: sub }
70
+ end
37
71
  end
38
72
 
73
+ # Control characters out (a row's text may come from a file any local
74
+ # process writes, and the TUI prints it), whitespace collapsed.
75
+ def one_line(text, max) = text.to_s.gsub(/[[:cntrl:]]/, " ").gsub(/\s+/, " ").strip[0, max]
76
+
39
77
  # @raise [ArgumentError] not a Hash, both or neither of command / url,
40
78
  # a bad command, a url that isn't http(s) with a host (no userinfo,
41
79
  # at most MAX_URL characters), a url action without a label
42
- def build_action(action)
80
+ def build_action(action, rows: [])
43
81
  raise ArgumentError, "a card action is a Hash {label:, command:} or {label:, url:}" unless action.is_a?(Hash)
44
82
 
45
83
  action = action.transform_keys(&:to_sym)
84
+ marks = action_marks(action, rows)
85
+ plain_action(action).merge(marks)
86
+ end
87
+
88
+ # An action's +row+ (checked against +rows+, ROW_ACTIONS a row) and
89
+ # +primary+, as given.
90
+ def action_marks(action, rows)
91
+ marks = {}
92
+ if action.key?(:row) && !action[:row].nil?
93
+ row = action[:row]
94
+ raise ArgumentError, "a card action's row #{row.inspect} isn't one of the card's rows" unless row.is_a?(Integer) && row.between?(0, rows.size - 1)
95
+
96
+ marks[:row] = row
97
+ end
98
+ marks[:primary] = true if action[:primary] == true
99
+ marks
100
+ end
101
+
102
+ def plain_action(action)
46
103
  label = action[:label].to_s.strip
47
104
  has_command = action.key?(:command) && !action[:command].nil?
48
105
  has_url = action.key?(:url) && !action[:url].nil?
@@ -10,6 +10,7 @@ require_relative "../hook_question"
10
10
  require_relative "../idle_client"
11
11
  require_relative "../tool_ids"
12
12
  require_relative "../cache_confirm"
13
+ require_relative "../user_typed"
13
14
  require_relative "service"
14
15
  require_relative "side_question"
15
16
 
@@ -31,6 +32,7 @@ module Samagotchi
31
32
  autoload :Sessions, File.expand_path("sessions", __dir__)
32
33
  autoload :AttachedContext, File.expand_path("attached_context", __dir__)
33
34
  autoload :Peers, File.expand_path("peers", __dir__)
35
+ autoload :Panes, File.expand_path("panes", __dir__)
34
36
  # ctx.http's file needs only json and uri (net/http loads with the first
35
37
  # real request); autoloaded so a bundle can name HttpError at load time.
36
38
  autoload :Http, File.expand_path("http", __dir__)
@@ -295,6 +297,13 @@ module Samagotchi
295
297
  @host.cache_confirm&.call(line.to_s, changed ? true : false)
296
298
  end
297
299
 
300
+ # Whether the command running on this thread was typed by a person at
301
+ # a UI (the web, a card's button there, an attached TUI): never chi
302
+ # send (the model's own shell can run it), a tool call, a delegate or
303
+ # a participant. What acts as the user on their screen, or writes
304
+ # another agent's settings, runs only then (UserTyped).
305
+ def user_typed? = UserTyped.typed?
306
+
298
307
  # Whether the session is a `chi scratch` one (deleted when it ends).
299
308
  def scratch? = !!@host.scratch&.call
300
309
 
@@ -429,11 +438,15 @@ module Samagotchi
429
438
  # @param id [String, nil] an earlier card's id to replace it
430
439
  # @param between_turns [Boolean] never a running turn's: shown outside
431
440
  # the turn's block while one runs (from a thread of the plugin's own)
441
+ # @param rows [Array<Hash>, nil] {title:, sub:}: a list to pick from,
442
+ # each row's buttons the actions with its index as +row:+ (up to 3
443
+ # a row, apart from the card's own 6)
432
444
  # @return [String] the card's id
433
445
  # @raise [ArgumentError] no title, a bad level or action
434
- def card(title:, body: "", actions: [], level: :info, id: nil, between_turns: false)
446
+ def card(title:, body: "", actions: [], level: :info, id: nil, between_turns: false, rows: nil)
435
447
  shown = { source: @bundle, title: title, body: body, actions: actions, level: level, id: id }
436
448
  shown[:between_turns] = true if between_turns
449
+ shown[:rows] = rows unless rows.nil?
437
450
  @host.card.call(**shown)
438
451
  end
439
452
 
@@ -588,6 +601,12 @@ module Samagotchi
588
601
  @context ||= AttachedContext.new(@host, bundle: @bundle)
589
602
  end
590
603
 
604
+ # The panes where agents' own UIs run (Samagotchi::Panes: kitty
605
+ # windows), read now from the config. #show moves the user's screen,
606
+ # so it runs only in a command they typed (#user_typed?).
607
+ # @return [Panes]
608
+ def panes = Panes.new
609
+
591
610
  # This session's peers: a pairing and a peer's messages (Peers).
592
611
  # @return [Peers]
593
612
  def peers
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../panes"
4
+ require_relative "../user_typed"
5
+
6
+ module Samagotchi
7
+ module Plugin
8
+ # ctx.panes (docs/plugins.md, Panes): the kitty windows agents run in
9
+ # (Samagotchi::Panes, its config read at each call). #find a pid's
10
+ # window and its title; #alive? whether a window #open made is still
11
+ # there. #show brings one to the front, #open opens a background tab
12
+ # and #paste types into one: they act as the user on their screen, so
13
+ # only in a command a person typed at a UI (UserTyped: never a tool
14
+ # call or chi send).
15
+ class Panes
16
+ # Show, open or paste from anything but a command the user typed.
17
+ class NotTyped < StandardError; end
18
+
19
+ # @return [Samagotchi::Panes::Pane, nil] the window +pid+ runs in
20
+ def find(pid:) = Samagotchi::Panes.from_config.find(pid: pid)
21
+
22
+ # @return [Boolean] whether kitty showed it (false: the window is gone)
23
+ # @raise [NotTyped] not in a command the user typed
24
+ def show(pane)
25
+ raise NotTyped, "Show brings a window to the front, so it runs only when you tap it or type it" unless UserTyped.typed?
26
+
27
+ Samagotchi::Panes.from_config.show(pane)
28
+ end
29
+
30
+ # A background kitty tab running +command+ (argv) in +cwd+, titled
31
+ # +title+.
32
+ # @return [Samagotchi::Panes::PaneRef]
33
+ # @raise [NotTyped] not in a command the user typed
34
+ # @raise [Samagotchi::Panes::Error] no kitty answers (Unavailable), or it didn't open it
35
+ def open(command:, cwd:, title:)
36
+ raise NotTyped, "it opens a kitty tab on your screen, so it runs only when you tap it or type it" unless UserTyped.typed?
37
+
38
+ Samagotchi::Panes.from_config.open(command: command, cwd: cwd, title: title)
39
+ end
40
+
41
+ # Whether +ref+'s window is still there.
42
+ def alive?(ref) = Samagotchi::Panes.from_config.alive?(ref)
43
+
44
+ # Pastes +text+ into +ref+'s window, then Enter when +enter+.
45
+ # @return [Boolean] whether kitty took it (false: the window is gone)
46
+ # @raise [NotTyped] not in a command the user typed
47
+ def paste(ref, text, enter: true)
48
+ raise NotTyped, "it types into a window on your screen, so it runs only when you tap it or type it" unless UserTyped.typed?
49
+
50
+ Samagotchi::Panes.from_config.paste(ref, text, enter: enter)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -14,6 +14,9 @@ require_relative "../session_paths"
14
14
  require_relative "../peer_exchanges"
15
15
  require_relative "../peer_mailbox"
16
16
  require_relative "../peer_note"
17
+ require_relative "peer_pairing"
18
+ require_relative "peer_start"
19
+ require_relative "peer_start_hook"
17
20
 
18
21
  module Samagotchi
19
22
  module Plugin
@@ -71,6 +74,10 @@ module Samagotchi
71
74
  # below that raise NotImplementedError. Its constants are read as
72
75
  # self.class::X: a bare name here would find only this class's.
73
76
  class Peer
77
+ include PeerPairing
78
+ include PeerStart
79
+ include PeerStartHook
80
+
74
81
  KINDS = %w[validate context question].freeze
75
82
  # A part up to this long goes inline: the peer reads it with no file
76
83
  # access.
@@ -152,8 +159,9 @@ module Samagotchi
152
159
  rescue SendError => e
153
160
  "Error: #{e.message}"
154
161
  end
155
- chi.command peer::COMMAND, "the paired #{peer::APP} session: status; validate|context <text>, last: send to " \
156
- "it; install-skill: install #{peer::NAME}'s chi-peer skill", anytime: true do |args, ctx|
162
+ chi.command peer::COMMAND, "the paired #{peer::APP} session: status; pair, unpair: pair with one here or " \
163
+ "wait for one; start: open one in kitty; validate|context <text>, last: send to it; install-skill: install " \
164
+ "#{peer::NAME}'s chi-peer skill", anytime: true do |args, ctx|
157
165
  run_command(args.to_s.strip, ctx)
158
166
  end
159
167
  chi.tool peer::READ_TOOL, read_description, params: READ_PARAMS, label: "read #{peer::NAME}'s message",
@@ -164,6 +172,13 @@ module Samagotchi
164
172
  # Its messages in the mailbox are this peer's: its notes, its wake
165
173
  # budget and lines (peer.turns).
166
174
  chi.peer peer::PEER
175
+ # A /<peer> pair wait that ran out (the worker's PeerWaitExpiry):
176
+ # one line, between turns.
177
+ chi.on(:peer_wait_ended) do |event, ctx|
178
+ next unless event[:peer].to_s == peer::PEER
179
+
180
+ ctx.notify("No #{peer::APP} session came in #{event[:minutes] || 10} min; #{peer::COMMAND} pair to wait again.")
181
+ end
167
182
  chi.on(:before_turn) do |event|
168
183
  @lock.synchronize do
169
184
  @before_turn = Array(event[:messages])
@@ -278,7 +293,7 @@ module Samagotchi
278
293
  def setup_paragraph(_ctx) = raise(NotImplementedError, "#{self.class} must define #setup_paragraph")
279
294
 
280
295
  # The installed card's next step, after the skill's path.
281
- def installed_line(short) = "In #{self.class::APP}, say \"pair with chi #{short}\"."
296
+ def installed_line(_short) = "In #{self.class::APP}, say \"pair with chi\" (or type #{self.class::COMMAND} pair here first)."
282
297
 
283
298
  # More placeholders the installed skill fills in: {placeholder => text}.
284
299
  def skill_substitutions(_ctx) = {}
@@ -296,10 +311,16 @@ module Samagotchi
296
311
  when "validate", "context" then rest.strip.empty? ? self.class::USAGE : send_text(ctx, verb, rest)
297
312
  when "last" then rest.empty? ? send_last(ctx) : self.class::USAGE
298
313
  when "install-skill" then install_skill(ctx, yes: rest == "--yes")
299
- else self.class::USAGE
314
+ when "install-hook" then install_hook(ctx, yes: rest.strip == "--yes")
315
+ when "pair", "show", "unpair" then pairing_verb(verb, rest.strip, ctx)
316
+ when "start" then rest.strip.empty? ? start_here(ctx) : self.class::USAGE
317
+ else extra_command(verb, rest.strip, ctx) || self.class::USAGE
300
318
  end
301
319
  end
302
320
 
321
+ # A subclass's own verb (claude-peer's install-hook); nil: not one.
322
+ def extra_command(_verb, _rest, _ctx) = nil
323
+
303
324
  def send_text(ctx, kind, text)
304
325
  "#{deliver(ctx, kind: kind, title: text_title(text), text: text, by: "user", salt: command_salt)}; #{self.class::NAME}'s answer comes as a " \
305
326
  "message with its text"
@@ -500,11 +521,15 @@ module Samagotchi
500
521
  "again from #{peer::NAME} with an address chi can reach"
501
522
  end
502
523
 
503
- # The status card; nil, so the command adds no text.
524
+ # The status card; nil, so the command adds no text. Unpaired, a
525
+ # bundle whose peer lists itself shows the sessions here, that none
526
+ # is, or the wait (PeerPairing#unpaired_card).
504
527
  def status(ctx)
505
528
  peer = self.class
506
529
  id = ctx.session_id
507
530
  link = id && ctx.peers.link(peer::PEER)
531
+ return nil if id && !link && !paired_the_old_way?(ctx) && unpaired_card(ctx)
532
+
508
533
  address = link&.address.to_s.strip
509
534
  lines = if link && !address.empty?
510
535
  paired_lines(address)
@@ -515,10 +540,12 @@ module Samagotchi
515
540
  ["**paired the old way: pair again** (install the skill first: Install skill below, then tell " \
516
541
  "#{peer::NAME} \"pair with chi #{id[0, 8]}\")."]
517
542
  else
543
+ id_words = id ? "; #{peer::NAME} needs the id, #{id[0, 8]}, only when several chi sessions run in its project" : ""
518
544
  ["**not paired.** In #{peer::APP}, install the chi-peer skill (Install skill below), then tell " \
519
- "#{peer::NAME} \"pair with chi #{id.to_s[0, 8]}\"."]
545
+ "#{peer::NAME} \"pair with chi\" (#{peer::COMMAND} pair waits for it#{id_words})."]
520
546
  end
521
547
  paired = !link.nil?
548
+ rows, row_actions = started_rows(ctx, link) if link
522
549
  lines << "last send: #{last_send_line(ctx)}" if id && last_send(ctx)
523
550
  if link && !address.empty? && (idle = dormant_for(ctx))
524
551
  lines << "quiet #{duration(idle)}: a send from the model asks first"
@@ -528,10 +555,17 @@ module Samagotchi
528
555
  lines << "last message from #{peer::NAME} (its text in the conversation): #{message_line(last)}"
529
556
  end
530
557
  lines << "skill: **differs from this bundle's**: #{peer::COMMAND} install-skill (Replace), then pair again" if skill_differs?(ctx)
558
+ lines << hook_note if hook_note
531
559
  lines << transport_line
532
- ctx.card(id: peer::STATUS_CARD, title: "#{peer::BUNDLE}: #{paired ? "paired" : "not paired"}",
533
- body: lines.join("\n\n"), actions: [{ label: "Refresh", command: peer::COMMAND },
534
- { label: "Install skill", command: "#{peer::COMMAND} install-skill" }])
560
+ actions = Array(row_actions) + [{ label: "Refresh", command: peer::COMMAND }]
561
+ actions << start_action if !link && id && start_prompt
562
+ actions << { label: "Install skill", command: "#{peer::COMMAND} install-skill" }
563
+ state = if link && link_gone?(link) then "ended"
564
+ elsif paired then "paired"
565
+ else "not paired"
566
+ end
567
+ ctx.card(id: peer::STATUS_CARD, title: "#{peer::BUNDLE}: #{state}",
568
+ body: lines.join("\n\n"), actions: actions, rows: rows)
535
569
  nil
536
570
  end
537
571
 
@@ -604,13 +638,21 @@ module Samagotchi
604
638
  elsif current == text then "Installed and up to date."
605
639
  else "Installed, and different from this bundle's (Replace overwrites it):\n\n```diff\n#{diff(target, text)}\n```"
606
640
  end
607
- body = ["#{peer::APP}'s side: it pairs (`pair with chi <id>`), answers validates and replies into chi.",
641
+ body = ["#{peer::APP}'s side: it pairs (`pair with chi`), answers validates and replies into chi.",
608
642
  "target: `#{target}`", state, setup_paragraph(ctx)]
609
643
  actions = if current == text
610
644
  []
611
645
  else
612
646
  [{ label: current ? "Replace" : "Install", command: "#{peer::COMMAND} install-skill --yes" }]
613
647
  end
648
+ if start_hook?
649
+ hooked = hook_state == :current
650
+ body << (hooked ? "start hook: added (`#{hook_file}`)" : hook_paragraph)
651
+ # Its button opens the hook's card, the merged file as a diff, whose
652
+ # own Add writes it (the plan: the diff before the write).
653
+ body << "Add the start hook shows the merged file first; its card's Add writes it." unless hooked
654
+ actions << { label: "Add the start hook…", command: "#{peer::COMMAND} install-hook" } unless hooked
655
+ end
614
656
  ctx.card(id: peer::SKILL_CARD, title: "#{peer::NAME}'s chi-peer skill", body: body.join("\n\n"), actions: actions)
615
657
  nil
616
658
  end