samagotchi 0.184.0 → 0.186.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 (119) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +68 -1
  3. data/docs/architecture.md +1 -1
  4. data/docs/bundles.md +62 -35
  5. data/docs/cli.md +10 -1
  6. data/docs/configuration.md +19 -1
  7. data/docs/context.md +76 -10
  8. data/docs/guardrails.md +20 -2
  9. data/docs/hooks.md +1 -0
  10. data/docs/internals/block-renderers.md +4 -2
  11. data/docs/internals/bridge.md +7 -2
  12. data/docs/internals/engine.md +15 -1
  13. data/docs/internals/llm-context-forget.md +6 -0
  14. data/docs/internals/participants.md +4 -1
  15. data/docs/internals/prompt-caching.md +14 -0
  16. data/docs/internals/remote-sessions.md +10 -1
  17. data/docs/internals/session-lifecycle.md +1 -1
  18. data/docs/internals/web-frontend.md +23 -6
  19. data/docs/mcp-server.md +6 -2
  20. data/docs/scheduling.md +5 -0
  21. data/docs/sessions.md +12 -2
  22. data/docs/sub-agent.md +48 -3
  23. data/docs/thin-clients.md +76 -4
  24. data/lib/samagotchi/agent_caller.rb +97 -0
  25. data/lib/samagotchi/bridge.rb +7 -7
  26. data/lib/samagotchi/bundles/claude-peer/manifest.yml +14 -8
  27. data/lib/samagotchi/bundles/claude-peer/plugin.rb +3 -3
  28. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +49 -30
  29. data/lib/samagotchi/bundles/claude-peer/scripts/pair +2 -2
  30. data/lib/samagotchi/bundles/claude-peer/scripts/pair-remote +123 -0
  31. data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +11 -3
  32. data/lib/samagotchi/bundles/codex-peer/manifest.yml +10 -8
  33. data/lib/samagotchi/bundles/codex-peer/plugin.rb +3 -3
  34. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +29 -8
  35. data/lib/samagotchi/bundles/codex-peer/scripts/pair +1 -1
  36. data/lib/samagotchi/bundles/codex-peer/skill_codex_peer.md +11 -3
  37. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  38. data/lib/samagotchi/bundles/mcp/http_client.rb +19 -2
  39. data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
  40. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  41. data/lib/samagotchi/bundles/system/self_map.md +2 -0
  42. data/lib/samagotchi/client_id.rb +61 -1
  43. data/lib/samagotchi/command_intake.rb +16 -8
  44. data/lib/samagotchi/config.rb +3 -1
  45. data/lib/samagotchi/context_absorber.rb +215 -46
  46. data/lib/samagotchi/delivery.rb +15 -0
  47. data/lib/samagotchi/driver_command.rb +68 -0
  48. data/lib/samagotchi/engine/block_fix.rb +2 -2
  49. data/lib/samagotchi/engine.rb +20 -5
  50. data/lib/samagotchi/engine_peers.rb +6 -0
  51. data/lib/samagotchi/guardrails/pair_boundary.rb +1 -1
  52. data/lib/samagotchi/llm/chat_loop.rb +16 -2
  53. data/lib/samagotchi/mcp_command.rb +7 -2
  54. data/lib/samagotchi/mcp_server/tools.rb +9 -3
  55. data/lib/samagotchi/objectives_track.rb +2 -1
  56. data/lib/samagotchi/owner_prompt.rb +5 -1
  57. data/lib/samagotchi/parent_report.rb +9 -5
  58. data/lib/samagotchi/plain_reply.rb +75 -0
  59. data/lib/samagotchi/plugin/peer.rb +10 -4
  60. data/lib/samagotchi/reply_for.rb +254 -0
  61. data/lib/samagotchi/reply_wait.rb +12 -3
  62. data/lib/samagotchi/schedules.rb +10 -7
  63. data/lib/samagotchi/send_command/parent_gate.rb +3 -1
  64. data/lib/samagotchi/send_command.rb +45 -8
  65. data/lib/samagotchi/session.rb +9 -1
  66. data/lib/samagotchi/session_access/ask.rb +10 -4
  67. data/lib/samagotchi/session_access/contract.rb +2 -0
  68. data/lib/samagotchi/session_access/exchange/token.rb +42 -7
  69. data/lib/samagotchi/session_access/exchange.rb +5 -2
  70. data/lib/samagotchi/session_access/local.rb +6 -3
  71. data/lib/samagotchi/session_access/remote.rb +23 -3
  72. data/lib/samagotchi/session_access.rb +4 -2
  73. data/lib/samagotchi/session_command_catalog.rb +8 -0
  74. data/lib/samagotchi/session_commands.rb +9 -0
  75. data/lib/samagotchi/session_continue.rb +4 -1
  76. data/lib/samagotchi/session_inbox.rb +2 -0
  77. data/lib/samagotchi/session_inbox_delivery.rb +4 -2
  78. data/lib/samagotchi/session_manager/delivery.rb +80 -5
  79. data/lib/samagotchi/session_manager/spawn.rb +7 -1
  80. data/lib/samagotchi/steer.rb +119 -22
  81. data/lib/samagotchi/terminal_ui/formatting.rb +12 -2
  82. data/lib/samagotchi/tool_activity.rb +4 -0
  83. data/lib/samagotchi/tool_declarations.rb +22 -1
  84. data/lib/samagotchi/tool_names.rb +4 -0
  85. data/lib/samagotchi/toolkit.rb +3 -2
  86. data/lib/samagotchi/tools/ask_session.rb +208 -0
  87. data/lib/samagotchi/tools/builtin_calls.rb +1 -0
  88. data/lib/samagotchi/tools/builtins.rb +5 -0
  89. data/lib/samagotchi/turn_anchors.rb +65 -17
  90. data/lib/samagotchi/turn_checkpoint.rb +21 -2
  91. data/lib/samagotchi/turn_stages/context_judge.rb +3 -1
  92. data/lib/samagotchi/turn_stages/objectives.rb +4 -2
  93. data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +3 -0
  94. data/lib/samagotchi/version.rb +1 -1
  95. data/lib/samagotchi/visitor.rb +37 -0
  96. data/lib/samagotchi/web/app.rb +1 -1
  97. data/lib/samagotchi/web/ask_routes.rb +68 -21
  98. data/lib/samagotchi/web/message_display.rb +15 -2
  99. data/lib/samagotchi/web/participant_access.rb +1 -1
  100. data/lib/samagotchi/web/public/app.js +1 -1
  101. data/lib/samagotchi/web/public/block_fix.js +11 -3
  102. data/lib/samagotchi/web/public/history_shape.js +3 -1
  103. data/lib/samagotchi/web/public/index.html +8 -0
  104. data/lib/samagotchi/web/public/peer_links.js +27 -4
  105. data/lib/samagotchi/web/public/peer_note.js +19 -3
  106. data/lib/samagotchi/web/public/peer_text.js +62 -0
  107. data/lib/samagotchi/web/public/picture_regions.js +2 -2
  108. data/lib/samagotchi/web/public/turn_events.js +19 -0
  109. data/lib/samagotchi/web/public/working_set_view.js +11 -2
  110. data/lib/samagotchi/web/session_access_routes.rb +30 -2
  111. data/lib/samagotchi/web/session_control.rb +22 -3
  112. data/lib/samagotchi/web/session_create.rb +11 -7
  113. data/lib/samagotchi/worker/command_queue.rb +5 -2
  114. data/lib/samagotchi/worker/senders.rb +79 -0
  115. data/lib/samagotchi/worker.rb +22 -18
  116. data/lib/samagotchi/worker_inbound.rb +12 -4
  117. data/lib/samagotchi/worker_participant_gate.rb +1 -3
  118. data/lib/samagotchi/worker_wakes.rb +12 -0
  119. metadata +10 -1
@@ -5,6 +5,8 @@ require_relative "../session_access/local"
5
5
  require_relative "../session_inbox"
6
6
  require_relative "../reply_wait"
7
7
  require_relative "../pending_question"
8
+ require_relative "../plain_reply"
9
+ require_relative "../reply_for"
8
10
  require_relative "../context_sources"
9
11
  require_relative "../peer_exchanges"
10
12
  require_relative "../context_command"
@@ -183,6 +185,25 @@ module Samagotchi
183
185
  question: busy.question&.to_file, parent_strategy: (strategy_json(busy.strategy) if busy.strategy) }.compact
184
186
  end
185
187
 
188
+ # A question +reply_for+'s surface may not answer (ReplyFor#answers?):
189
+ # needs: "detail", the words to say in its text, the question's id and
190
+ # kind only (no options, no approval's facts); {} for any other.
191
+ def needs_detail_fields(reply_for, question)
192
+ return {} if reply_for.nil? || question.nil? || reply_for.answers?(question)
193
+
194
+ { needs: "detail", text: reply_for.needs_detail_text(question), question: question.to_file.slice("id", "kind") }
195
+ end
196
+
197
+ # The question as +reply_for+'s surface gets it: a plain one's text and
198
+ # header made plain (PlainReply), its options byte-exact (they are
199
+ # answered by label); the saved question is never touched.
200
+ def question_json(question, reply_for)
201
+ json = question&.to_file
202
+ return json unless json && reply_for&.plain?
203
+
204
+ json.merge(%w[question header].to_h { |key| [key, PlainReply.strip(json[key])] }.compact)
205
+ end
206
+
186
207
  def strategy_json(strategy) = { name: strategy.name, allowed: strategy.allowed, origin: strategy.origin.to_s }
187
208
 
188
209
  def wait_start_json(id, start)
@@ -249,11 +270,18 @@ module Samagotchi
249
270
 
250
271
  RESULT_FIELDS = %i[text file outcome limit error_kind retryable cancel_reason stopped_by kept_steps turn_id origin].freeze
251
272
 
252
- def wait_result_json(result, strategy)
273
+ # +reply_for+: the asking surface's (ReplyFor), passed in by the route
274
+ # (an ask's from its request, a token's from the token, an answer's
275
+ # from the pending turn's saved opener), never looked up after the
276
+ # turn: a plain one gets the reply and its question's words made plain
277
+ # (PlainReply, #question_json), here only.
278
+ def wait_result_json(result, strategy, reply_for: nil)
253
279
  json = { status: result.status.to_s }
254
280
  RESULT_FIELDS.each { |key| json[key] = result[key] }
255
281
  json[:text] = json[:text]&.dup&.force_encoding(Encoding::UTF_8)&.scrub
256
- json[:question] = result.question&.to_file
282
+ json[:text] = PlainReply.strip(json[:text]) if reply_for&.plain?
283
+ json[:question] = question_json(result.question, reply_for)
284
+ json.merge!(needs_detail_fields(reply_for, result.question)) if result.status == :waiting_for_answer
257
285
  json[:parent_strategy] = strategy_json(strategy)
258
286
  json
259
287
  end
@@ -5,6 +5,7 @@ require_relative "../client_id"
5
5
  require_relative "../delivery"
6
6
  require_relative "../image_store"
7
7
  require_relative "../prompt_history"
8
+ require_relative "../reply_for"
8
9
  require_relative "../session"
9
10
  require_relative "../session_command_catalog"
10
11
  require_relative "../session_manager"
@@ -57,6 +58,19 @@ module Samagotchi
57
58
  qid = body["id"]
58
59
  return error_response(400, "missing_fields", "id is required") if qid.to_s.strip.empty?
59
60
 
61
+ # reply_for: the answering surface's (a thin client's, ReplyFor). One
62
+ # without detail may not answer an approval, a spending question or
63
+ # a user-only option (ReplyFor#answers?): 403, nothing relayed.
64
+ begin
65
+ answering = ReplyFor.parse(body["reply_for"])
66
+ rescue ReplyFor::Invalid => e
67
+ return error_response(400, "invalid_answer", e.message)
68
+ end
69
+ pending = session.pending_question
70
+ if answering && pending && pending.id.to_s == qid.to_s && !answering.answers?(pending)
71
+ return json_response(403, ReplyFor::NEEDS_DETAIL)
72
+ end
73
+
60
74
  # wait: a thin client's answer that also waits for what comes next
61
75
  # (AskRoutes#answered_wait), its start read before the answer goes in.
62
76
  wait, error = answer_wait_option(body)
@@ -65,6 +79,11 @@ module Samagotchi
65
79
  start = answer_wait_start(id, session, qid) if wait
66
80
  return session_not_found(id) if wait && !start
67
81
 
82
+ # The pending turn's surface, from its saved opener (the running
83
+ # turn's checkpoint copy until its first boundary), read before the
84
+ # answer goes in: its reply is shaped for it (#answered_wait).
85
+ reply_for = ReplyFor.of_latest_turn(session.messages) if wait
86
+
68
87
  # client_id: only chi answer's (a parent agent's answer, which the
69
88
  # worker holds to its guardrails.parent_answers) is passed on; any
70
89
  # other is dropped, so a forward can only tighten (an answer never
@@ -93,7 +112,7 @@ module Samagotchi
93
112
  detail: reply_detail(reply, "the worker refused that option") })
94
113
  end
95
114
  end
96
- relayed.is_a?(Symbol) ? answered_wait(id, qid, start, wait, answered: relayed == :answered) : relayed
115
+ relayed.is_a?(Symbol) ? answered_wait(id, qid, start, wait, answered: relayed == :answered, reply_for: reply_for) : relayed
97
116
  end
98
117
 
99
118
  # A request to the worker sent once more when the worker dropped it as
@@ -235,7 +254,7 @@ module Samagotchi
235
254
  result = SessionManager.deliver_turn(id, prompt: prompt.to_s, client_id: client_id, images: images,
236
255
  delivery: delivery, no_interrupt: body["no_interrupt"] == true,
237
256
  author: principal(req).author,
238
- anchors: TurnAnchors.from(body),
257
+ anchors: TurnAnchors.from_turn_route(body),
239
258
  follow: body["follow"] == true,
240
259
  state_dir: @state_dir, manager: @manager,
241
260
  bridge: -> { live_bridge_client(id) })
@@ -266,7 +285,7 @@ module Samagotchi
266
285
  # @return [Array, nil] the response, or nil to go on
267
286
  def turn_refusal(id, body, prompt, images)
268
287
  vision = body["check_vision"] == true && !images.empty?
269
- return nil unless vision || App::CLI_SEND_IDS.include?(body["client_id"])
288
+ return nil unless vision || ClientId.parent_line?(body["client_id"])
270
289
 
271
290
  session = @session_class.load(id, state_dir: default_state_dir)
272
291
  refused = parent_gate(prompt, client_id: body["client_id"], values: [session.parent_answers])
@@ -34,10 +34,6 @@ module Samagotchi
34
34
  # What a continue takes from the previous link: refused beside it.
35
35
  CONTINUE_TAKES = %w[dir llm_context thinking settings toolkit tools no_tools parent_answers].freeze
36
36
 
37
- # chi send's client ids: its lines are checked as chi send checks them
38
- # (#parent_gate).
39
- CLI_SEND_IDS = [ClientId::CLI_SEND, ClientId::CLI_ANSWER].freeze
40
-
41
37
  private
42
38
 
43
39
  def handle_create(req)
@@ -91,11 +87,19 @@ module Samagotchi
91
87
  parent_answers, error = request_parent_answers(body["parent_answers"])
92
88
  return error if error
93
89
 
90
+ # driver: the agent (agent:<name>) the owner names its driver (chi
91
+ # send --new --driver); none: its creator when that is an agent.
92
+ driver = body["driver"]
93
+ unless driver.nil? || (ClientId.agent?(driver) && ClientId.agent(ClientId.agent_name(driver)) == driver)
94
+ return error_response(400, "invalid_driver", "driver is an agent's id: agent:<name>")
95
+ end
96
+
94
97
  model = (body["model"] || body["model_name"]).to_s.strip
95
98
  refused = create_refusal(body, idle ? body["preview"] : prompt, values: [parent_answers], model: model)
96
99
  return refused if refused
97
100
 
98
101
  folder = { working_directory: dir || start_dir }
102
+ folder[:driver] = driver if driver
99
103
  # model: the model the session starts on (the start page's picker,
100
104
  # spelled as GET /api/models lists it); blank means the default.
101
105
  folder[:model_name] = model unless model.empty?
@@ -178,7 +182,7 @@ module Samagotchi
178
182
  # for #create_refusal; nil when the previous link isn't there
179
183
  # (continue_from says so).
180
184
  def continue_refusal(given, body, line, model:)
181
- return nil unless body["check_vision"] == true || CLI_SEND_IDS.include?(body["client_id"])
185
+ return nil unless body["check_vision"] == true || ClientId.parent_line?(body["client_id"])
182
186
 
183
187
  previous = Session.load(SessionChain.resolve(given, state_dir: default_state_dir), state_dir: default_state_dir)
184
188
  create_refusal(body, line, values: [previous.parent_answers], model: model, previous: previous)
@@ -220,9 +224,9 @@ module Samagotchi
220
224
  # @return [Array, nil] the response, or nil to go on
221
225
  def parent_gate(line, client_id:, values:)
222
226
  line = line.to_s
223
- return nil unless CLI_SEND_IDS.include?(client_id)
227
+ return nil unless ClientId.parent_line?(client_id)
224
228
 
225
- if client_id == ClientId::CLI_ANSWER && TurnFlow.continue_yes?(line) &&
229
+ if (client_id == ClientId::CLI_ANSWER || ClientId.agent?(client_id)) && TurnFlow.continue_yes?(line) &&
226
230
  (refusal = SendCommand::ParentGate.continue_refusal_for(values))
227
231
  return error_response(403, "parent_continue_refused", refusal)
228
232
  end
@@ -218,9 +218,12 @@ module Samagotchi
218
218
  def confirm?(command) = ClientId.interactive?(command.client_id) && !command.card
219
219
 
220
220
  # Whether +command+ is a parent agent's (chi answer's marker, which chi
221
- # send and a parent's attach give a /continue yes too): held to
221
+ # send and a parent's attach give a /continue yes too, or an agent's
222
+ # id that reached the worker unmapped): held to
222
223
  # guardrails.parent_answers (SessionCommands#continue_answer).
223
- def as_parent?(command) = command.client_id.to_s == ClientId::CLI_ANSWER
224
+ def as_parent?(command)
225
+ command.client_id.to_s == ClientId::CLI_ANSWER || ClientId.agent?(command.client_id)
226
+ end
224
227
 
225
228
  # Whether +command+ is a user's at a UI (a web tab, a chi TUI, a card's
226
229
  # click), not a parent agent's: only such a line loosens
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../archive_store"
4
+ require_relative "../child_reports"
5
+ require_relative "../client_id"
6
+ require_relative "../log"
7
+ require_relative "../turn_checkpoint"
8
+ require_relative "../visitor"
9
+
10
+ module Samagotchi
11
+ class Worker
12
+ # Who a turn's input came from and what it does to the session: the
13
+ # owner's input (#user_input: un-archive, wake reset, held reports), the
14
+ # driver's (the same but the un-archive), a visitor's count toward
15
+ # session.max_wakes and its refusal (#count_visitor, #refuse_visitor),
16
+ # and the one seat for refusing a queued turn (#refuse_queued_turn).
17
+ # Reads @session, @session_id, @state_dir, @session_dir, @wakes,
18
+ # @child_reports and @event_log.
19
+ module SendersPart
20
+ private
21
+
22
+ # A visitor's turn (an agent's that doesn't drive the session: Visitor)
23
+ # counts toward session.max_wakes, the budget wake turns use, before
24
+ # anything else runs for it (no start grace, context.wake or pause
25
+ # gate: its sender asked for it).
26
+ # @return [Boolean] false when the budget is spent: refuse it
27
+ def count_visitor
28
+ return false if @wakes.spent?
29
+
30
+ @wakes.count!
31
+ true
32
+ end
33
+
34
+ # The owner's input (ClientId.owner?) un-archives the session; a
35
+ # delegate's, a plugin's, a reminder's or a participant's doesn't.
36
+ # It also resets the wakes (budget and pause); input typed in the web or
37
+ # a TUI the peer wakes too (chi send doesn't: a peer can run it).
38
+ def user_input(client_id)
39
+ # The session's driver (an agent: Visitor.driver?) resets the wake
40
+ # budget and releases held reports as chi send's input did, but never
41
+ # brings back an archived session.
42
+ if Visitor.driver?(client_id, @session)
43
+ @wakes.human_input!(interactive: false)
44
+ @child_reports&.release_holds
45
+ return
46
+ end
47
+ return unless ClientId.owner?(client_id)
48
+
49
+ ArchiveStore.user_input(@session_id, state_dir: @state_dir)
50
+ @wakes.human_input!(interactive: ClientId.interactive?(client_id))
51
+ @child_reports&.release_holds
52
+ end
53
+
54
+ # session.max_wakes spent: a visitor's queued turn is refused (its
55
+ # sender's wait reads ParentReport::REFUSED_LINES' max_wakes); the
56
+ # owner hears it once, until their next message (WorkerWakes#notice_visitor!).
57
+ def refuse_visitor(origin)
58
+ Log.info(:worker, "visitor_turn_refused", client_id: origin.client_id, reason: Visitor::CAP_REASON)
59
+ refuse_queued_turn(origin, Visitor::CAP_REASON)
60
+ return unless @wakes.notice_visitor!
61
+
62
+ # An agent's name, or another session's question by its id8.
63
+ name = ClientId.agent_name(origin.client_id) || "session #{ClientId.session_name(origin.client_id)}"
64
+ @event_log.announce(type: :hook_notice, hook: "agent", level: :info, between_turns: true,
65
+ text: "#{name}'s message was not run: #{@wakes.max} turns ran in a row with no message " \
66
+ "from you (#{ChildRing::MAX_WAKES_KEY}); agents' messages run again after your next one")
67
+ nil
68
+ end
69
+
70
+ # A queued turn that may not start (a participant's, WorkerParticipantGate#refuse):
71
+ # settled as refused for +reason+ (a code) and dropped from every UI's
72
+ # queue; it never runs (TurnCheckpoint.refuse_queued).
73
+ # @return [nil]
74
+ def refuse_queued_turn(origin, reason, author: nil)
75
+ TurnCheckpoint.refuse_queued(@session_dir, @event_log, origin, reason, author: author)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -26,6 +26,7 @@ require_relative "reminder_carry"
26
26
  require_relative "log"
27
27
  require_relative "turn_flow"
28
28
  require_relative "turn_checkpoint"
29
+ require_relative "reply_for"
29
30
  require_relative "continue_offer"
30
31
  require_relative "iteration_limit"
31
32
  require_relative "session_commands"
@@ -49,6 +50,7 @@ require_relative "worker/scratch"
49
50
  require_relative "worker/child_side"
50
51
  require_relative "worker/command_queue"
51
52
  require_relative "worker/block_fixes"
53
+ require_relative "worker/senders"
52
54
 
53
55
  module Samagotchi
54
56
  # The loop of a background session worker, once it owns the session (see
@@ -100,6 +102,7 @@ module Samagotchi
100
102
  include ChildSidePart
101
103
  include CommandQueuePart
102
104
  include BlockFixesPart
105
+ include SendersPart
103
106
 
104
107
  FALLBACK_TICK_SECONDS = 5
105
108
  # A worker just started runs no wake turn for delegate reports this
@@ -347,10 +350,12 @@ module Samagotchi
347
350
  # This session's own delegate children's news (rings in children/).
348
351
  @child_reports = ChildReports.new(session_id: @session_id, state_dir: @state_dir,
349
352
  predecessor: @session.continues)
350
- # Its attached context's notes (ContextSources).
353
+ # Its attached context's notes (ContextSources); a peer's text goes in
354
+ # its note up to the tool output cap, as a task's does (WorkerTaskWakes).
351
355
  @context_absorber = ContextAbsorber.new(session_id: @session_id, state_dir: @state_dir,
352
356
  project_root: @session.project_root, project_id: @session.project_id,
353
- peer_sources: @engine.peer_sources, peer_turns: -> { @wakes.peer_turns })
357
+ peer_sources: @engine.peer_sources, peer_turns: -> { @wakes.peer_turns },
358
+ cap: -> { ToolOutputCut.max_chars(nil) })
354
359
  # What comes in between turns, and how each kind is saved.
355
360
  @inbound = WorkerInbound.new(session: @session, state_dir: @state_dir, session_dir: @session_dir, engine: @engine,
356
361
  event_log: @event_log, turn_state: @turn_state, context_absorber: @context_absorber, wakes: @wakes,
@@ -690,10 +695,16 @@ module Samagotchi
690
695
  return @participant_gate.refuse(origin, reason)
691
696
  end
692
697
 
698
+ visitor = Visitor.of?(origin.client_id, @session)
699
+ return refuse_visitor(origin) if visitor && !count_visitor
700
+
693
701
  # A candidate the prompt mentions is attached and fetched first, so
694
702
  # the absorb below brings its note into this turn.
695
- ContextCandidates.before_turn(session: @session, prompt: prompt.text, author: origin.author, state_dir: @state_dir,
696
- cancelled: -> { stopped_on_disk? })
703
+ # Not for a visitor's prompt: only the owner's attaches (OwnerPrompt).
704
+ unless visitor
705
+ ContextCandidates.before_turn(session: @session, prompt: prompt.text, author: origin.author, state_dir: @state_dir,
706
+ cancelled: -> { stopped_on_disk? })
707
+ end
697
708
  # A first turn sees the context attached before it, and any turn the
698
709
  # background tasks that finished since the last look.
699
710
  absorb_context(before_turn: true)
@@ -701,8 +712,12 @@ module Samagotchi
701
712
  user_input(origin.client_id)
702
713
  @continue_offer.drop(origin)
703
714
  @turn_flow.before_prompt_turn
715
+ # A visitor's opener is saved with its sender, which gives it its
716
+ # header line (Steer::OPENER_HEADERS); the driver's and the owner's
717
+ # keep their bytes.
718
+ source = visitor ? { opener_source: origin.client_id } : {}
704
719
  run_engine_turn(prompt.text, origin: origin, max_iterations: max_iterations(prompt.no_interrupt),
705
- images: prompt.images, **prompt.anchors.kwargs) do |result, error|
720
+ images: prompt.images, **source, **prompt.anchors.kwargs) do |result, error|
706
721
  if error
707
722
  # The Engine announced :turn_failed (with the error's one line).
708
723
  restore_failed_turn([[prompt.text, origin, prompt.images, prompt.anchors], *@merged_this_turn], error: error)
@@ -748,7 +763,8 @@ module Samagotchi
748
763
  @turn_author = origin.participant_author
749
764
  restores = @turn_flow.restores
750
765
  begin
751
- @checkpoint.begin_turn(prompt: prompt, turn_id: turn_args[:id], enqueued_id: origin.enqueued_id, author: turn_author_fields(origin))
766
+ @checkpoint.begin_turn(prompt: prompt, turn_id: turn_args[:id], enqueued_id: origin.enqueued_id, author: turn_author_fields(origin),
767
+ source: turn_args[:opener_source], reply: ReplyFor.opener_fields(@session.messages, turn_args[:reply_for]))
752
768
  @session.status = Session::STATUS_RUNNING
753
769
  # A continue that moved this session to the chain's new link wrote
754
770
  # the new parent beside it (Session::PARENT_FILE): this turn's
@@ -957,18 +973,6 @@ module Samagotchi
957
973
 
958
974
  def monotonic_now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
959
975
 
960
- # The owner's input (ClientId.owner?) un-archives the session; a
961
- # delegate's, a plugin's, a reminder's or a participant's doesn't.
962
- # It also resets the wakes (budget and pause); input typed in the web or
963
- # a TUI the peer wakes too (chi send doesn't: a peer can run it).
964
- def user_input(client_id)
965
- return unless ClientId.owner?(client_id)
966
-
967
- ArchiveStore.user_input(@session_id, state_dir: @state_dir)
968
- @wakes.human_input!(interactive: ClientId.interactive?(client_id))
969
- @child_reports&.release_holds
970
- end
971
-
972
976
  def stopped_on_disk?
973
977
  SessionManager.stopped_on_disk?(@session_id, state_dir: @state_dir)
974
978
  end
@@ -61,8 +61,10 @@ module Samagotchi
61
61
  # that agrees closes the round and counts toward session.max_wakes only.
62
62
  def peer_turn? = peer? && !delivery.agreed
63
63
  def origin = TurnOrigin.new(client_id: "#{ClientId::CONTEXT_PREFIX}#{name}")
64
- # The plain note #unmark_wake puts back.
65
- def note = delivery.note
64
+ # The plain note #unmark_wake puts back: with the wake note's text
65
+ # when only that held it (ContextAbsorber::Delivery#wake_plain), so a
66
+ # failed wake turn keeps the text it marked read.
67
+ def note = delivery.wake_plain || delivery.note
66
68
  end
67
69
 
68
70
  # A task finish wake turn to run (TaskWatch, TaskFinishNote): its wake
@@ -332,7 +334,10 @@ module Samagotchi
332
334
  what: "command", reason: "not_permitted")
333
335
  return true
334
336
  end
335
- return false unless CommandIntake.allowed?(origin.client_id) || CommandIntake.shell_line?(text)
337
+ # A ! line from a sender that may not run one (an agent's too:
338
+ # CommandIntake.refused?, asked first) is queued for its refusal
339
+ # (Worker::CommandQueue#command_result).
340
+ return false unless CommandIntake.refused?(text, origin.client_id) || CommandIntake.allowed?(origin.client_id)
336
341
 
337
342
  @queue_command.call(text, origin.client_id, after_file, origin.enqueued_id)
338
343
  true
@@ -404,7 +409,10 @@ module Samagotchi
404
409
 
405
410
  # +batch+ with +waking+'s subscription recording the wake (the 10-minute window).
406
411
  def woken(batch, waking)
407
- stamped = waking.with(subscription: waking.subscription.with(wakes_at: Time.now.iso8601))
412
+ # The wake note went in: its own read mark when it holds a text the
413
+ # plain note doesn't (ContextAbsorber::Delivery#wake_subscription).
414
+ subscription = waking.wake_subscription || waking.subscription
415
+ stamped = waking.with(subscription: subscription.with(wakes_at: Time.now.iso8601))
408
416
  batch.with(deliveries: batch.deliveries.map { |delivery| delivery == waking ? stamped : delivery })
409
417
  end
410
418
  end
@@ -98,11 +98,9 @@ module Samagotchi
98
98
  # @param origin [TurnOrigin] a participant's
99
99
  # @return [nil]
100
100
  def refuse(origin, reason)
101
- TurnCheckpoint.record_refused(@session_dir, origin.enqueued_id, reason: reason, author: origin.participant_author)
102
101
  Participants.log(source_id, :turn, state_dir: @state_dir, participant: origin.participant_id,
103
102
  enqueued_id: origin.enqueued_id, status: "refused", reason: reason)
104
- @event_log.announce(type: :turn_dequeued, enqueued_id: origin.enqueued_id, client_id: origin.client_id)
105
- nil
103
+ TurnCheckpoint.refuse_queued(@session_dir, @event_log, origin, reason, author: origin.participant_author)
106
104
  end
107
105
 
108
106
  # A participant invited, revoked or expired since the last look: the
@@ -36,6 +36,7 @@ module Samagotchi
36
36
  @peer_in_a_row = 0
37
37
  @paused = false
38
38
  @budget_noticed = false
39
+ @visitor_noticed = false
39
40
  @peer_noticed = false
40
41
  @handed = nil
41
42
  end
@@ -145,6 +146,7 @@ module Samagotchi
145
146
  @in_a_row = 0
146
147
  @paused = false
147
148
  @budget_noticed = false
149
+ @visitor_noticed = false
148
150
  return unless interactive
149
151
 
150
152
  @peer_in_a_row = 0
@@ -159,6 +161,16 @@ module Samagotchi
159
161
  @budget_noticed = true
160
162
  end
161
163
 
164
+ # @return [Boolean] true the first time a visitor's turn (an agent's
165
+ # that doesn't drive the session) is refused since the last human
166
+ # input: the UIs hear it once. Apart from #notice_budget!, which a
167
+ # delegate's or a task's notice may have spent this stretch.
168
+ def notice_visitor!
169
+ return false if @visitor_noticed
170
+
171
+ @visitor_noticed = true
172
+ end
173
+
162
174
  # @return [Boolean] true the first time a peer's answer is held since
163
175
  # the last interactive input: the UIs hear it once
164
176
  def notice_peer_budget!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samagotchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.184.0
4
+ version: 0.186.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -185,6 +185,7 @@ files:
185
185
  - docs/testing.md
186
186
  - docs/thin-clients.md
187
187
  - lib/samagotchi.rb
188
+ - lib/samagotchi/agent_caller.rb
188
189
  - lib/samagotchi/analytics_file.rb
189
190
  - lib/samagotchi/answer_command.rb
190
191
  - lib/samagotchi/answer_display.rb
@@ -254,6 +255,7 @@ files:
254
255
  - lib/samagotchi/bundles/claude-peer/plugin.rb
255
256
  - lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md
256
257
  - lib/samagotchi/bundles/claude-peer/scripts/pair
258
+ - lib/samagotchi/bundles/claude-peer/scripts/pair-remote
257
259
  - lib/samagotchi/bundles/claude-peer/skill_claude_peer.md
258
260
  - lib/samagotchi/bundles/codex-peer/manifest.yml
259
261
  - lib/samagotchi/bundles/codex-peer/plugin.rb
@@ -379,6 +381,7 @@ files:
379
381
  - lib/samagotchi/desktop/macos/Sessions.swift
380
382
  - lib/samagotchi/desktop/macos/Tmux.swift
381
383
  - lib/samagotchi/desktop_command.rb
384
+ - lib/samagotchi/driver_command.rb
382
385
  - lib/samagotchi/edit_preview.rb
383
386
  - lib/samagotchi/effective_model.rb
384
387
  - lib/samagotchi/empty_answer_error.rb
@@ -618,6 +621,7 @@ files:
618
621
  - lib/samagotchi/peer_exchanges.rb
619
622
  - lib/samagotchi/peer_sources.rb
620
623
  - lib/samagotchi/pending_question.rb
624
+ - lib/samagotchi/plain_reply.rb
621
625
  - lib/samagotchi/plugin/api.rb
622
626
  - lib/samagotchi/plugin/attached_context.rb
623
627
  - lib/samagotchi/plugin/card.rb
@@ -683,6 +687,7 @@ files:
683
687
  - lib/samagotchi/remote_attach_command.rb
684
688
  - lib/samagotchi/remote_command.rb
685
689
  - lib/samagotchi/remotes.rb
690
+ - lib/samagotchi/reply_for.rb
686
691
  - lib/samagotchi/reply_wait.rb
687
692
  - lib/samagotchi/reply_wait/liveness.rb
688
693
  - lib/samagotchi/reply_wait/poll.rb
@@ -875,6 +880,7 @@ files:
875
880
  - lib/samagotchi/toolkit_report_command.rb
876
881
  - lib/samagotchi/toolkit_suggest.rb
877
882
  - lib/samagotchi/tools/args.rb
883
+ - lib/samagotchi/tools/ask_session.rb
878
884
  - lib/samagotchi/tools/ask_user_question.rb
879
885
  - lib/samagotchi/tools/builtin_calls.rb
880
886
  - lib/samagotchi/tools/builtins.rb
@@ -943,6 +949,7 @@ files:
943
949
  - lib/samagotchi/version.rb
944
950
  - lib/samagotchi/vision_context.rb
945
951
  - lib/samagotchi/vision_support.rb
952
+ - lib/samagotchi/visitor.rb
946
953
  - lib/samagotchi/waiting_steer.rb
947
954
  - lib/samagotchi/web/app.rb
948
955
  - lib/samagotchi/web/ask_routes.rb
@@ -1083,6 +1090,7 @@ files:
1083
1090
  - lib/samagotchi/web/public/peer_exchange.js
1084
1091
  - lib/samagotchi/web/public/peer_links.js
1085
1092
  - lib/samagotchi/web/public/peer_note.js
1093
+ - lib/samagotchi/web/public/peer_text.js
1086
1094
  - lib/samagotchi/web/public/picture_regions.js
1087
1095
  - lib/samagotchi/web/public/picture_viewer.js
1088
1096
  - lib/samagotchi/web/public/plain_line.js
@@ -1249,6 +1257,7 @@ files:
1249
1257
  - lib/samagotchi/worker/command_queue.rb
1250
1258
  - lib/samagotchi/worker/leave.rb
1251
1259
  - lib/samagotchi/worker/scratch.rb
1260
+ - lib/samagotchi/worker/senders.rb
1252
1261
  - lib/samagotchi/worker/signal_grace.rb
1253
1262
  - lib/samagotchi/worker/startup.rb
1254
1263
  - lib/samagotchi/worker/wake_turns.rb