samagotchi 0.183.0 → 0.185.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 (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +84 -1
  3. data/docs/architecture.md +12 -11
  4. data/docs/bundles.md +63 -36
  5. data/docs/cli.md +10 -1
  6. data/docs/configuration.md +3 -2
  7. data/docs/context.md +76 -10
  8. data/docs/guardrails.md +7 -2
  9. data/docs/hooks.md +1 -0
  10. data/docs/internals/block-renderers.md +4 -2
  11. data/docs/internals/bridge.md +7 -3
  12. data/docs/internals/bundles.md +6 -2
  13. data/docs/internals/engine.md +2 -1
  14. data/docs/internals/llm-context-forget.md +6 -0
  15. data/docs/internals/participants.md +5 -2
  16. data/docs/internals/prompt-caching.md +8 -1
  17. data/docs/internals/remote-sessions.md +22 -4
  18. data/docs/internals/session-lifecycle.md +1 -1
  19. data/docs/internals/web-frontend.md +37 -15
  20. data/docs/internals/web-push.md +11 -0
  21. data/docs/internals/working-set.md +238 -6
  22. data/docs/mcp-server.md +11 -3
  23. data/docs/memory.md +2 -1
  24. data/docs/plugins.md +6 -6
  25. data/docs/scheduling.md +19 -0
  26. data/docs/sessions.md +13 -3
  27. data/docs/sub-agent.md +48 -3
  28. data/docs/thin-clients.md +38 -12
  29. data/lib/samagotchi/agent_caller.rb +97 -0
  30. data/lib/samagotchi/bridge.rb +7 -7
  31. data/lib/samagotchi/bridge_client.rb +12 -4
  32. data/lib/samagotchi/bundle_command/check.rb +20 -2
  33. data/lib/samagotchi/bundle_manifest.rb +5 -1
  34. data/lib/samagotchi/bundles/claude-peer/manifest.yml +14 -8
  35. data/lib/samagotchi/bundles/claude-peer/plugin.rb +3 -3
  36. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +49 -30
  37. data/lib/samagotchi/bundles/claude-peer/scripts/pair +2 -2
  38. data/lib/samagotchi/bundles/claude-peer/scripts/pair-remote +123 -0
  39. data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +11 -3
  40. data/lib/samagotchi/bundles/codex-peer/manifest.yml +10 -8
  41. data/lib/samagotchi/bundles/codex-peer/plugin.rb +3 -3
  42. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +29 -8
  43. data/lib/samagotchi/bundles/codex-peer/scripts/pair +1 -1
  44. data/lib/samagotchi/bundles/codex-peer/skill_codex_peer.md +11 -3
  45. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  46. data/lib/samagotchi/bundles/mcp/http_client.rb +96 -54
  47. data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
  48. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  49. data/lib/samagotchi/bundles/system/self_map.md +9 -2
  50. data/lib/samagotchi/client_id.rb +61 -1
  51. data/lib/samagotchi/command_intake.rb +16 -8
  52. data/lib/samagotchi/context_absorber.rb +212 -46
  53. data/lib/samagotchi/delivery.rb +15 -0
  54. data/lib/samagotchi/driver_command.rb +68 -0
  55. data/lib/samagotchi/engine/block_fix.rb +2 -2
  56. data/lib/samagotchi/engine/session.rb +14 -6
  57. data/lib/samagotchi/engine/turn_helpers.rb +4 -3
  58. data/lib/samagotchi/engine.rb +13 -5
  59. data/lib/samagotchi/engine_peers.rb +6 -0
  60. data/lib/samagotchi/guardrails/pair_boundary.rb +1 -1
  61. data/lib/samagotchi/mcp_command.rb +7 -2
  62. data/lib/samagotchi/mcp_server/http.rb +3 -3
  63. data/lib/samagotchi/mcp_server/protocol.rb +23 -4
  64. data/lib/samagotchi/mcp_server/stdio.rb +1 -0
  65. data/lib/samagotchi/mcp_server/tools.rb +11 -4
  66. data/lib/samagotchi/model_notes.rb +34 -12
  67. data/lib/samagotchi/objectives_track.rb +2 -1
  68. data/lib/samagotchi/owner_prompt.rb +5 -1
  69. data/lib/samagotchi/parent_report.rb +9 -5
  70. data/lib/samagotchi/plugin/peer.rb +10 -4
  71. data/lib/samagotchi/prompt_files.rb +11 -4
  72. data/lib/samagotchi/prompt_layout.rb +88 -0
  73. data/lib/samagotchi/prompt_part.rb +82 -20
  74. data/lib/samagotchi/reply_wait.rb +16 -4
  75. data/lib/samagotchi/schedule_command.rb +18 -4
  76. data/lib/samagotchi/schedules.rb +49 -14
  77. data/lib/samagotchi/send_command/parent_gate.rb +3 -1
  78. data/lib/samagotchi/send_command.rb +45 -8
  79. data/lib/samagotchi/session.rb +9 -1
  80. data/lib/samagotchi/session_access/ask.rb +58 -14
  81. data/lib/samagotchi/session_access/contract.rb +2 -0
  82. data/lib/samagotchi/session_access/exchange.rb +5 -2
  83. data/lib/samagotchi/session_access/local.rb +13 -3
  84. data/lib/samagotchi/session_access/remote.rb +18 -2
  85. data/lib/samagotchi/session_access.rb +4 -2
  86. data/lib/samagotchi/session_command_catalog.rb +8 -0
  87. data/lib/samagotchi/session_commands.rb +9 -0
  88. data/lib/samagotchi/session_continue.rb +4 -1
  89. data/lib/samagotchi/session_inbox.rb +17 -0
  90. data/lib/samagotchi/session_inbox_delivery.rb +4 -2
  91. data/lib/samagotchi/session_manager/delivery.rb +58 -9
  92. data/lib/samagotchi/session_manager/spawn.rb +7 -1
  93. data/lib/samagotchi/steer.rb +95 -20
  94. data/lib/samagotchi/system_prompt.rb +196 -161
  95. data/lib/samagotchi/terminal_ui/formatting.rb +12 -2
  96. data/lib/samagotchi/tool_activity.rb +4 -0
  97. data/lib/samagotchi/tool_declarations.rb +22 -1
  98. data/lib/samagotchi/tool_names.rb +4 -0
  99. data/lib/samagotchi/toolkit.rb +3 -2
  100. data/lib/samagotchi/tools/ask_session.rb +208 -0
  101. data/lib/samagotchi/tools/builtin_calls.rb +1 -0
  102. data/lib/samagotchi/tools/builtins.rb +5 -0
  103. data/lib/samagotchi/tools/memory.rb +11 -3
  104. data/lib/samagotchi/turn_checkpoint.rb +19 -2
  105. data/lib/samagotchi/turn_stages/context_judge.rb +3 -1
  106. data/lib/samagotchi/turn_stages/objectives.rb +4 -2
  107. data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +3 -0
  108. data/lib/samagotchi/turn_stages/warmup.rb +2 -2
  109. data/lib/samagotchi/version.rb +1 -1
  110. data/lib/samagotchi/visitor.rb +37 -0
  111. data/lib/samagotchi/web/app.rb +2 -3
  112. data/lib/samagotchi/web/ask_routes.rb +95 -23
  113. data/lib/samagotchi/web/mcp_route.rb +7 -4
  114. data/lib/samagotchi/web/message_display.rb +15 -2
  115. data/lib/samagotchi/web/public/app.js +1 -1
  116. data/lib/samagotchi/web/public/block_fix.js +11 -3
  117. data/lib/samagotchi/web/public/delete_sheet.js +20 -18
  118. data/lib/samagotchi/web/public/history_shape.js +3 -1
  119. data/lib/samagotchi/web/public/index.html +8 -0
  120. data/lib/samagotchi/web/public/peer_links.js +27 -4
  121. data/lib/samagotchi/web/public/peer_note.js +19 -3
  122. data/lib/samagotchi/web/public/peer_text.js +45 -0
  123. data/lib/samagotchi/web/public/picture_regions.js +2 -2
  124. data/lib/samagotchi/web/public/removal_sheet_model.js +3 -4
  125. data/lib/samagotchi/web/public/running_tasks.js +0 -16
  126. data/lib/samagotchi/web/public/session_actions.js +0 -9
  127. data/lib/samagotchi/web/public/turn_events.js +19 -0
  128. data/lib/samagotchi/web/public/working_set_view.js +178 -23
  129. data/lib/samagotchi/web/public/ws_part_peek.js +277 -0
  130. data/lib/samagotchi/web/public/ws_peek.js +3 -1
  131. data/lib/samagotchi/web/public/ws_preview.css +30 -0
  132. data/lib/samagotchi/web/public/ws_reader.js +7 -3
  133. data/lib/samagotchi/web/public/ws_sections.js +37 -7
  134. data/lib/samagotchi/web/push/notifier.rb +33 -6
  135. data/lib/samagotchi/web/server.rb +1 -1
  136. data/lib/samagotchi/web/session_control.rb +1 -1
  137. data/lib/samagotchi/web/session_create.rb +11 -7
  138. data/lib/samagotchi/web/session_hub.rb +26 -4
  139. data/lib/samagotchi/web/working_set_routes.rb +93 -5
  140. data/lib/samagotchi/worker/command_queue.rb +5 -2
  141. data/lib/samagotchi/worker/senders.rb +79 -0
  142. data/lib/samagotchi/worker.rb +21 -18
  143. data/lib/samagotchi/worker_inbound.rb +12 -4
  144. data/lib/samagotchi/worker_link.rb +2 -2
  145. data/lib/samagotchi/worker_participant_gate.rb +1 -3
  146. data/lib/samagotchi/worker_wakes.rb +12 -0
  147. data/lib/samagotchi/working_set.rb +162 -90
  148. data/lib/samagotchi/working_set_locator.rb +221 -0
  149. data/lib/samagotchi/working_set_own_changes.rb +221 -0
  150. data/lib/samagotchi/working_set_part.rb +281 -0
  151. metadata +13 -1
@@ -235,8 +235,9 @@ module Samagotchi
235
235
  Kit.new(name: LEAN, tools: LEAN_TOOLS, groups: :all),
236
236
  # A delegate child: it reports to its parent (send_note; the
237
237
  # delegated-by line names it) and leaves memory writes to it, and
238
- # starts no children of its own (removed: not even through call_tool).
239
- Kit.new(name: CHILD, base: LEAN, add: %w[send_note], remove: %w[memory_write delegate delegate_result])
238
+ # starts no children of its own, nor asks other sessions (ask_session)
239
+ # (removed: not even through call_tool).
240
+ Kit.new(name: CHILD, base: LEAN, add: %w[send_note], remove: %w[memory_write delegate delegate_result ask_session])
240
241
  ].to_h { |kit| [kit.name, kit] }.freeze
241
242
 
242
243
  # What a toolkit declares, the Registry's filter: every registered tool,
@@ -0,0 +1,208 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+ require "digest"
5
+ require_relative "../tool_names"
6
+ require_relative "peers"
7
+
8
+ module Samagotchi
9
+ module Tools
10
+ # Ask another chi session a question and get its answer as the result:
11
+ # the question goes in as that session's own turn (queued behind a
12
+ # running one), from session:<this id8>, a visitor's turn there
13
+ # (Visitor: one header line, counted toward its session.max_wakes). One
14
+ # call waits up to CAP seconds (SessionAccess::Ask, as chi_ask does); a
15
+ # turn still running then gives a token, which the next call (token:)
16
+ # waits on. It never answers a question that session asks its user: the
17
+ # asker has no answer tool, so it says so and its model tells its user.
18
+ class AskSession
19
+ NAME = ToolNames::AskSession::NAME
20
+ # The wait's slice and the whole call's cap: chi_ask's (McpServer::Tools).
21
+ SLICE = 25
22
+ CAP = 35
23
+ WAITS_ON_QUESTION = "waits on a question for its user: not answered. Tell your user; it waits in chi web " \
24
+ "(or chi --attach ID)"
25
+
26
+ # A wrapped Exchange whose deliveries never go to the asker: a
27
+ # continue that moves the target onto this session (Ask relocates the
28
+ # head) is refused, nothing sent.
29
+ class NotSelf < SimpleDelegator
30
+ def initialize(exchange, asker)
31
+ super(exchange)
32
+ @asker = asker
33
+ end
34
+
35
+ def attempt(target, prompt, **)
36
+ return __getobj__.attempt(target, prompt, **) unless target.id == @asker
37
+
38
+ refused = SessionManager::DeliverResult.new(
39
+ status: :refused, ack: { "error" => "self", "detail" => "#{target.id[0, 8]} is this session now (continued into it)" }
40
+ )
41
+ SessionAccess::Exchange::Attempted.new(target: target, session: nil, owner: nil, running: false, delivery: nil,
42
+ refs: [], result: refused)
43
+ end
44
+ end
45
+
46
+ def self.name = NAME
47
+
48
+ # @param question [String] the question (up to 16 KiB)
49
+ # @param session [String, nil] the session asked: an id or a prefix
50
+ # @param token [String, nil] a running answer's token, to wait on it
51
+ # @param peers [Peers, nil]
52
+ # @return [String]
53
+ def self.call(question, session: nil, token: nil, peers: nil, access: nil, slice: SLICE, cap: CAP)
54
+ return "Error: this session's id is not known here" unless peers&.session_id
55
+
56
+ load_backend
57
+ access ||= SessionAccess::Local.new(state_dir: peers.state_dir)
58
+ new(peers, access, slice: slice, cap: cap)
59
+ .run(question, session: session, token: token)
60
+ end
61
+
62
+ # Loaded on the first call, not with the tools: the backend (Local
63
+ # brings chi web's probes) is too big a load for the core's start and
64
+ # the bundle test kit's budget (core_without_web_spec, sandbox_spec).
65
+ def self.load_backend
66
+ require_relative "../client_id"
67
+ require_relative "../parent_report"
68
+ require_relative "../session"
69
+ require_relative "../session_inbox"
70
+ require_relative "../session_manager"
71
+ require_relative "../session_access/local"
72
+ require_relative "../session_access/ask"
73
+ require_relative "../session_access/exchange"
74
+ require_relative "../session_access/exchange/token"
75
+ require_relative "../reply_wait"
76
+ require_relative "../turn_checkpoint"
77
+ end
78
+
79
+ def initialize(peers, access, slice:, cap:)
80
+ @peers = peers
81
+ @access = access
82
+ @slice = slice
83
+ @cap = cap
84
+ @cancelled = -> { peers.cancelled? }
85
+ end
86
+
87
+ def run(question, session:, token:)
88
+ return wait(token) unless token.to_s.strip.empty?
89
+ return "Error: give the session to ask (list_sessions shows them), or the token of a running answer" if session.to_s.strip.empty?
90
+
91
+ ask(session.to_s.strip, SessionInbox.checked_text(question, noun: "question"))
92
+ rescue SessionInbox::NoteRejected, SessionAccess::Exchange::Token::Invalid => e
93
+ "Error: #{e.message}"
94
+ rescue SessionManager::ScratchSession => e
95
+ "Error: #{e.message}"
96
+ rescue SessionManager::OwnedByTUI
97
+ "Error: a one-turn chi run holds that session now; it can't take a question"
98
+ rescue Session::NotFound => e
99
+ "Error: #{e.message} (list_sessions shows them)"
100
+ rescue ArgumentError => e # AmbiguousId, a file that won't load
101
+ "Error: #{e.message}"
102
+ end
103
+
104
+ private
105
+
106
+ def ask(given, question)
107
+ if (refused = refused_before(given, question))
108
+ return refused
109
+ end
110
+
111
+ head = SessionInbox::Head.resolve(:send, Session.resolve!(given, state_dir: @peers.state_dir), state_dir: @peers.state_dir)
112
+ asker = SessionAccess::Ask.new(@access, exchange: exchange)
113
+ reply = asker.call(given, prompt: question, client_id: ClientId.session(@peers.session_id), queue: true,
114
+ timeout: @slice, cap: @cap, cancelled: @cancelled,
115
+ request_id: request_id(given, question, head.id))
116
+ words(reply)
117
+ end
118
+
119
+ # The two checks send_note makes (itself, a link continued into it),
120
+ # and a line a session would run as a command (a /word a session
121
+ # command, a ! line refused there anyway).
122
+ def refused_before(given, question)
123
+ id = Session.resolve!(given, state_dir: @peers.state_dir)
124
+ Session.load(id, state_dir: @peers.state_dir)
125
+ return "Error: #{id[0, 8]} is this session; ask_session is for other sessions" if id == @peers.session_id
126
+
127
+ head = SessionInbox::Head.resolve(:send, id, state_dir: @peers.state_dir)
128
+ return "Error: #{id[0, 8]} was continued into this session; ask_session is for other sessions" if head.id == @peers.session_id
129
+ return "Error: a question can't start with ! or /: ask it in words" if question.start_with?("!", "/")
130
+
131
+ nil
132
+ end
133
+
134
+ def wait(token)
135
+ id, target = SessionAccess::Exchange::Token.load(token.to_s.strip)
136
+ return "Error: that token is this session's; ask_session waits on other sessions" if id == @peers.session_id
137
+
138
+ result = exchange.wait(id, target, timeout: @slice, cancelled: @cancelled)
139
+ next_token = SessionAccess::Exchange::Token.dump(id, target) if result.status == :timeout
140
+ outcome(result, id, next_token)
141
+ end
142
+
143
+ def exchange
144
+ @exchange ||= NotSelf.new(SessionAccess::Exchange.new(@access, cancelled: @cancelled), @peers.session_id)
145
+ end
146
+
147
+ # The same question to the same session within one turn waits on the
148
+ # first one's turn while that turn still runs or is queued (a retry
149
+ # after a timeout asks once): the asker, its turn, the target as given
150
+ # and the question. Once that turn has ended (answered, refused,
151
+ # canceled: ReplyWait.ended_turn? on the session asked, +head_id+), the
152
+ # same words ask again under the next key (-2, -3, …), never replaying
153
+ # an old answer (Ask#call rebinds to any turn of its key it knows).
154
+ def request_id(given, question, head_id)
155
+ turn = @peers.respond_to?(:turn_id) ? @peers.turn_id : nil
156
+ digest = Digest::SHA256.hexdigest([given, question].join("\0"))[0, 16]
157
+ base = "#{@peers.session_id[0, 8]}-#{turn || "none"}-#{digest}"
158
+ session = Session.load(head_id, state_dir: @peers.state_dir)
159
+ dir = Session.session_dir(head_id, state_dir: @peers.state_dir)
160
+ (1..).each do |n|
161
+ key = n == 1 ? base : "#{base}-#{n}"
162
+ enqueued = SessionInbox.enqueued_id_for("ask:#{key}")
163
+ return key unless ReplyWait.ended_turn?(session, session_dir: dir, turn: enqueued)
164
+ end
165
+ end
166
+
167
+ def words(reply)
168
+ case reply
169
+ when SessionAccess::Ask::Refused then refused(reply)
170
+ when SessionAccess::Ask::Ran then "Error: session #{reply.session_id[0, 8]} ran it as a session command; no answer comes"
171
+ else outcome(reply.result, reply.session_id, reply.token)
172
+ end
173
+ end
174
+
175
+ def outcome(result, id, token)
176
+ short = id.to_s[0, 8]
177
+ case result.status
178
+ when :done
179
+ text = result.text.to_s
180
+ "Session #{short} answered:\n#{text.strip.empty? ? "(an empty reply)" : text}"
181
+ when :timeout
182
+ "Session #{short} is still answering. Call ask_session again with token: #{token} (no session or question) " \
183
+ "to keep waiting."
184
+ when :waiting_for_answer then "Session #{short} #{WAITS_ON_QUESTION.sub("ID", short)}"
185
+ when :no_reply then "Error: session #{short}: #{ParentReport.no_reply_line(result)}"
186
+ when :worker_gone then "Error: session #{short}: its worker is gone; ask again to start a fresh one"
187
+ when :stopped then "Error: session #{short} was stopped"
188
+ when :canceled then "Error: the wait was stopped (this turn was canceled)"
189
+ else "Error: session #{short}: #{result.status} #{result.text}".strip
190
+ end
191
+ end
192
+
193
+ def refused(refused)
194
+ short = refused.session_id.to_s[0, 8]
195
+ case refused.kind
196
+ when :busy
197
+ return "Session #{short} #{WAITS_ON_QUESTION.sub("ID", short)}" if refused.busy&.error == "waiting"
198
+
199
+ "Error: session #{short} is busy (#{refused.busy&.error}); ask again later"
200
+ when :unknown then "Error: #{refused.error&.message || refused.message} (list_sessions shows them)"
201
+ when :undelivered then "Error: session #{short}: #{refused.result.refusal_text}"
202
+ when :canceled then "Error: the question was not asked (this turn was canceled)"
203
+ else "Error: #{refused.message}"
204
+ end
205
+ end
206
+ end
207
+ end
208
+ end
@@ -51,6 +51,7 @@ module Samagotchi
51
51
  "cancel_schedule" => { content: "name", fallback: :prefix_or_raw },
52
52
  "list_sessions" => { fallback: :prefix, fallback_key: "cwd" },
53
53
  "send_note" => { content: "text" },
54
+ "ask_session" => { content: "question" },
54
55
  "context_read" => { content: "name", fallback: :prefix_or_raw },
55
56
  "delegate" => { content: "task", fallback: :prefix_or_raw },
56
57
  "ask_user_question" => { content: "question", also: %w[question], verbatim: %w[options], options: true,
@@ -23,6 +23,7 @@ require_relative "list_schedules"
23
23
  require_relative "cancel_schedule"
24
24
  require_relative "list_sessions"
25
25
  require_relative "send_note"
26
+ require_relative "ask_session"
26
27
  require_relative "context_read"
27
28
  require_relative "delegate"
28
29
  require_relative "delegate_result"
@@ -62,6 +63,7 @@ module Samagotchi
62
63
  CancelSchedule,
63
64
  ListSessions,
64
65
  SendNote,
66
+ AskSession,
65
67
  ContextRead,
66
68
  Delegate,
67
69
  DelegateResult,
@@ -122,6 +124,9 @@ module Samagotchi
122
124
  end,
123
125
  ListSessions::NAME => ->(call, kctx) { ListSessions.call(call[:content], peers: kctx.peers, cwd: call[:cwd]) },
124
126
  SendNote::NAME => ->(call, kctx) { SendNote.call(call[:content], session: call[:session], peers: kctx.peers) },
127
+ AskSession::NAME => lambda do |call, kctx|
128
+ AskSession.call(call[:content], session: call[:session], token: call[:token], peers: kctx.peers)
129
+ end,
125
130
  ContextRead::NAME => lambda do |call, kctx|
126
131
  ContextRead.call(call[:content], offset: call[:offset], limit: call[:limit], peers: kctx.peers)
127
132
  end,
@@ -105,6 +105,16 @@ module Samagotchi
105
105
  end
106
106
  private_class_method :prompt_memory_name
107
107
 
108
+ # The text a Resolved entry reads as: its file, its overlay appended
109
+ # (#append_overlay). memory_read's body of one name and the prompt's
110
+ # identity and preloads (SystemPrompt, through #call); the working
111
+ # set's re-fetch of a prompt part resolves with the saved keys and
112
+ # composes here too, so the two never differ. +read+: the file reader.
113
+ def self.compose(resolved, read: ->(path) { File.read(path, encoding: "UTF-8") })
114
+ body = read.call(resolved.path)
115
+ resolved.overlay_path ? append_overlay(body, read.call(resolved.overlay_path), resolved.overlay_key) : body
116
+ end
117
+
108
118
  def self.overlay_prefix(key) = "#{SEPARATOR}Model-specific guidance (#{key}):\n"
109
119
 
110
120
  def self.append_overlay(body, overlay_text, key) = "#{body}#{overlay_prefix(key)}#{overlay_text}"
@@ -166,9 +176,7 @@ module Samagotchi
166
176
  next
167
177
  end
168
178
 
169
- body = File.read(resolved.path, encoding: "UTF-8")
170
- body = append_overlay(body, File.read(resolved.overlay_path, encoding: "UTF-8"), resolved.overlay_key) if resolved.overlay_path
171
- results << body
179
+ results << compose(resolved)
172
180
  reads[name] = { path: resolved.path, overlay_path: resolved.overlay_path, requested_scope: scope,
173
181
  model_key: model_key, fallback_model_key: fallback_model_key, project_dir: project_dir }
174
182
  end
@@ -122,6 +122,20 @@ module Samagotchi
122
122
  Log.warn(:worker, "command_outcome_write_failed", error: e.class.name, msg: e.message, enqueued_id: id)
123
123
  end
124
124
 
125
+ # A queued turn that may not start (Worker#refuse_queued_turn, a
126
+ # participant's through WorkerParticipantGate#refuse): its settled record
127
+ # (.record_refused) ends its sender's wait, and :turn_dequeued drops its
128
+ # bubble from every UI's queue. It never runs.
129
+ # @param event_log [#announce] the worker's
130
+ # @param origin [TurnOrigin] the queued turn's
131
+ # @param reason [String] a code (ParentReport::REFUSED_LINES)
132
+ # @return [nil]
133
+ def self.refuse_queued(session_dir, event_log, origin, reason, author: nil)
134
+ record_refused(session_dir, origin.enqueued_id, reason: reason, author: author)
135
+ event_log.announce(type: :turn_dequeued, enqueued_id: origin.enqueued_id, client_id: origin.client_id)
136
+ nil
137
+ end
138
+
125
139
  # A participant's queued turn the worker refused (WorkerParticipantGate#refuse:
126
140
  # +reason+ revoked or cap_reached) ends its sender's wait: no turn ran,
127
141
  # canceled for that reason (ParentReport reads it as refused). Its
@@ -184,7 +198,10 @@ module Samagotchi
184
198
  # @param author [Hash] a participant's prompt's author fields
185
199
  # (Engine#author_fields), so a kill before the first boundary leaves
186
200
  # it theirs; {} for the owner's
187
- def begin_turn(prompt: nil, turn_id: nil, enqueued_id: nil, author: {})
201
+ # +source+: a visitor's opener's sender (Steer::OPENER_HEADERS), saved
202
+ # on it as the Engine saves it (a turn killed before its first boundary
203
+ # keeps its header); nil for the owner's and the driver's.
204
+ def begin_turn(prompt: nil, turn_id: nil, enqueued_id: nil, author: {}, source: nil)
188
205
  @boundaries = 0
189
206
  @enqueued_id = enqueued_id.to_s.empty? ? nil : enqueued_id.to_s
190
207
  @author = author[:author]
@@ -195,7 +212,7 @@ module Samagotchi
195
212
  # stamp (Engine's Turn#wall), which the turn's first boundary saves.
196
213
  unless prompt.to_s.empty?
197
214
  messages << { role: "user", content: prompt, **(turn_id ? { turn_id: turn_id } : {}), **author,
198
- MessageTime::KEY => MessageTime.iso(MessageTime.now) }
215
+ **(source ? { source: source } : {}), MessageTime::KEY => MessageTime.iso(MessageTime.now) }
199
216
  end
200
217
  @session.turn_checkpoint = { messages: messages, marker: InterruptedTurn.new(started_at: @started_at, at: @started_at, iteration: 0,
201
218
  enqueued_id: @enqueued_id, author: @author) }
@@ -11,6 +11,7 @@ require_relative "../owner_prompt"
11
11
  require_relative "../session"
12
12
  require_relative "../side_judgments"
13
13
  require_relative "../system_one/client"
14
+ require_relative "../visitor"
14
15
 
15
16
  module Samagotchi
16
17
  module TurnStages
@@ -54,7 +55,8 @@ module Samagotchi
54
55
  # @param recorder [SystemOne::Recorder] the asks' runs (built for this call)
55
56
  def start(turn, recorder:)
56
57
  session = turn.session
57
- return unless turn.prompt.is_a?(String) && OwnerPrompt.turn?(author: turn.origin.author)
58
+ return unless turn.prompt.is_a?(String) && OwnerPrompt.turn?(author: turn.origin.author) &&
59
+ !Visitor.of?(turn.origin.client_id, session)
58
60
 
59
61
  pass(session, ContextCandidates.owner_texts(session.messages) + [turn.prompt], phase: "turn",
60
62
  state_dir: turn.context.state_dir, recorder: recorder)
@@ -8,6 +8,7 @@ require_relative "../objectives_audit"
8
8
  require_relative "../objectives_track"
9
9
  require_relative "../owner_prompt"
10
10
  require_relative "../system_one/client"
11
+ require_relative "../visitor"
11
12
 
12
13
  module Samagotchi
13
14
  module TurnStages
@@ -41,7 +42,8 @@ module Samagotchi
41
42
  # reminder's or a delegate's prompt isn't one.
42
43
  # @param recorder [SystemOne::Recorder] the audit's runs (built for this call)
43
44
  def incoming(turn, recorder:)
44
- return unless turn.prompt.is_a?(String) && turn.origin.human? && OwnerPrompt.turn?(author: turn.origin.author)
45
+ return unless turn.prompt.is_a?(String) && turn.origin.human? && OwnerPrompt.turn?(author: turn.origin.author) &&
46
+ !Visitor.of?(turn.origin.client_id, turn.session)
45
47
 
46
48
  audit(turn.context.state_dir, recorder).incoming(turn.session, turn.prompt)
47
49
  end
@@ -51,7 +53,7 @@ module Samagotchi
51
53
  # @param recorder [SystemOne::Recorder] the audit's runs (built for this
52
54
  # call: after a before_turn hook's model switch, the new model)
53
55
  def outgoing(turn, result, recorder:)
54
- return unless OwnerPrompt.turn?(author: turn.origin.author)
56
+ return unless OwnerPrompt.turn?(author: turn.origin.author) && !Visitor.of?(turn.origin.client_id, turn.session)
55
57
 
56
58
  audit(turn.context.state_dir, recorder).outgoing(turn.session, result.text)
57
59
  end
@@ -7,6 +7,7 @@ require_relative "../system_one/client"
7
7
  require_relative "../toolkit"
8
8
  require_relative "../toolkit_suggest"
9
9
  require_relative "../tools/tool_index"
10
+ require_relative "../visitor"
10
11
 
11
12
  module Samagotchi
12
13
  # The steps of a turn that keep state across its preparation, each a
@@ -162,6 +163,8 @@ module Samagotchi
162
163
  # read from disk) and the log (read whole) only when it gets there.
163
164
  kits = nil
164
165
  candidates = nil
166
+ # A visitor's prompt (Visitor) is not the owner's, as a participant's isn't.
167
+ return if Visitor.of?(turn.origin.client_id, session)
165
168
  return unless ToolkitSuggest.eligible?(
166
169
  session: session, prompt: turn.prompt, author: turn.origin.author,
167
170
  candidate_count: lambda {
@@ -19,8 +19,8 @@ module Samagotchi
19
19
  # @param turn_state [TurnState] whether steers are carried to the next turn
20
20
  # @param model_settings [SessionModelSettings] the effective model now, the next turn's LLM context strategy
21
21
  # @param reminder_queue [ReminderQueue] whether a reminder is due
22
- # @param system_prompt [#call] (model) the system prompt (Engine#system_prompt): called last, only
23
- # once every check passed, as building it has side effects (the memory index metric, the prompt notes)
22
+ # @param system_prompt [#call] (model) the system prompt (Engine#system_prompt with record: false: a
23
+ # warm-up records no parts, notes or memory index): called last, only once every check passed
24
24
  def initialize(prompt_warmup:, kernel:, turn_state:, model_settings:, reminder_queue:, system_prompt:)
25
25
  @prompt_warmup = prompt_warmup
26
26
  @kernel = kernel
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- VERSION = "0.183.0"
4
+ VERSION = "0.185.0"
5
5
  # Sent as the User-Agent of every HTTP request chi makes.
6
6
  USER_AGENT = "chi/#{VERSION}"
7
7
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "client_id"
4
+
5
+ module Samagotchi
6
+ # Whether a turn's sender is a visitor in a session: an agent's
7
+ # (agent:<name>) that is not the session's driver (Session#driver), or
8
+ # another chi session's question (session:<id8>, ask_session). A
9
+ # visitor's turn opens with one header line naming it (Steer::OPENER_HEADERS),
10
+ # counts toward session.max_wakes (Worker#run_prompt) and is not the
11
+ # owner's prompt (OwnerPrompt). The driver's turns are as chi send's
12
+ # were. No Engine: the Worker and the turn stages both ask it.
13
+ module Visitor
14
+ # The refused visitor turn's cancel_reason (TurnCheckpoint.record_refused):
15
+ # a code, whose sentence is ParentReport::REFUSED_LINES'.
16
+ CAP_REASON = "max_wakes"
17
+
18
+ module_function
19
+
20
+ # @param session [Session, nil] the session the turn runs in
21
+ def of?(client_id, session)
22
+ return true if ClientId.session?(client_id)
23
+
24
+ ClientId.agent?(client_id) && client_id.to_s != driver_of(session)
25
+ end
26
+
27
+ # Whether +client_id+ is the session's driver.
28
+ def driver?(client_id, session)
29
+ ClientId.agent?(client_id) && client_id.to_s == driver_of(session)
30
+ end
31
+
32
+ def driver_of(session)
33
+ session.respond_to?(:driver) ? session.driver.to_s : ""
34
+ end
35
+ private_class_method :driver_of
36
+ end
37
+ end
@@ -342,9 +342,8 @@ module Samagotchi
342
342
  ["POST", %r{\A/api/sessions/([^/]+)/context\z}, :handle_context_add, :owner, :session],
343
343
  ["POST", %r{\A/api/sessions/([^/]+)/context/([^/]+)/push\z}, :handle_context_push, :owner, :session],
344
344
  ["DELETE", %r{\A/api/sessions/([^/]+)/context/([^/]+)\z}, :handle_context_delete, :owner, :session],
345
- ["GET", %r{\A/api/sessions/([^/]+)/working_set\z}, :handle_working_set, :participant, :session],
345
+ *WorkingSetRoutes::ROUTES,
346
346
  ["GET", %r{\A/api/sessions/([^/]+)/prompt_history\z}, :handle_prompt_history, :participant, :session],
347
- ["GET", %r{\A/api/sessions/([^/]+)/working_set/item\z}, :handle_working_set_item, :owner, :session],
348
347
  ["GET", %r{\A/api/sessions/([^/]+)/participants\z}, :handle_participants_list, :owner, :session],
349
348
  ["POST", %r{\A/api/sessions/([^/]+)/participants\z}, :handle_participants_invite, :owner, :session],
350
349
  ["DELETE", %r{\A/api/sessions/([^/]+)/participants\z}, :handle_participants_revoke, :owner, :session],
@@ -1127,7 +1126,7 @@ module Samagotchi
1127
1126
  # delegates: chi --remote sessions delete --dry-run asks for this first.
1128
1127
  DELETE_PLAN_FEATURE = "delete_plan_all"
1129
1128
  INFO_FEATURES = ["dir", "newest_workers", SessionAccessRoutes::FEATURE, SessionAccessRoutes::ATTACH_FEATURE,
1130
- BOUND_FEATURE, CHAIN_DELETE_FEATURE, STOP_TASKS_FEATURE, DELETE_PLAN_FEATURE, ASK_FEATURE, McpRoute::FEATURE].freeze
1129
+ BOUND_FEATURE, CHAIN_DELETE_FEATURE, STOP_TASKS_FEATURE, DELETE_PLAN_FEATURE, ASK_FEATURE, McpRoute::FEATURE, Delivery::AGENT_FEATURE].freeze
1131
1130
 
1132
1131
  # The models a new session can start on, spelled as chi spells them
1133
1132
  # elsewhere: bare for the default host (what default.model holds), and