samagotchi 0.181.0 → 0.183.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 (200) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +93 -1
  3. data/bin/chi +4 -1
  4. data/docs/architecture.md +17 -7
  5. data/docs/bundle-authoring.md +1 -1
  6. data/docs/bundles.md +103 -4
  7. data/docs/cli.md +58 -10
  8. data/docs/configuration.md +3 -1
  9. data/docs/guardrails.md +16 -1
  10. data/docs/internals/block-renderers.md +172 -3
  11. data/docs/internals/bridge.md +23 -5
  12. data/docs/internals/bundles.md +21 -4
  13. data/docs/internals/engine.md +29 -8
  14. data/docs/internals/inline-widgets.md +3 -1
  15. data/docs/internals/prompt-caching.md +9 -1
  16. data/docs/internals/remote-sessions.md +38 -7
  17. data/docs/internals/web-frontend.md +116 -26
  18. data/docs/internals/working-set.md +1 -1
  19. data/docs/mcp-server.md +80 -0
  20. data/docs/plugins.md +308 -21
  21. data/docs/sessions.md +7 -4
  22. data/docs/sub-agent.md +9 -0
  23. data/docs/testing.md +3 -1
  24. data/docs/thin-clients.md +146 -0
  25. data/lib/samagotchi/answer_display.rb +3 -2
  26. data/lib/samagotchi/block_fix/stamp.rb +49 -0
  27. data/lib/samagotchi/block_fix.rb +220 -0
  28. data/lib/samagotchi/bridge/block_fix_route.rb +30 -0
  29. data/lib/samagotchi/bridge/pending_card.rb +4 -3
  30. data/lib/samagotchi/bridge/sse_writer.rb +16 -15
  31. data/lib/samagotchi/bridge/turn_drawings.rb +19 -0
  32. data/lib/samagotchi/bridge.rb +69 -69
  33. data/lib/samagotchi/bridge_client.rb +9 -0
  34. data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
  35. data/lib/samagotchi/bundle_command/check.rb +36 -19
  36. data/lib/samagotchi/bundle_command.rb +50 -21
  37. data/lib/samagotchi/bundle_manifest.rb +101 -4
  38. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  39. data/lib/samagotchi/bundles/mcp/client.rb +260 -0
  40. data/lib/samagotchi/bundles/mcp/content.rb +107 -0
  41. data/lib/samagotchi/bundles/mcp/http_client.rb +407 -0
  42. data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
  43. data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
  44. data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
  45. data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
  46. data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
  47. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  48. data/lib/samagotchi/cli/parent_wait.rb +5 -3
  49. data/lib/samagotchi/command_intake.rb +7 -0
  50. data/lib/samagotchi/commands/registry.rb +8 -3
  51. data/lib/samagotchi/config.rb +3 -0
  52. data/lib/samagotchi/continue_intent.rb +8 -0
  53. data/lib/samagotchi/continue_offer.rb +24 -15
  54. data/lib/samagotchi/engine/block_fix.rb +332 -0
  55. data/lib/samagotchi/engine/cards.rb +14 -5
  56. data/lib/samagotchi/engine/events.rb +10 -15
  57. data/lib/samagotchi/engine/models.rb +1 -1
  58. data/lib/samagotchi/engine/plugins.rb +3 -0
  59. data/lib/samagotchi/engine/recap.rb +1 -1
  60. data/lib/samagotchi/engine/side_requests.rb +8 -6
  61. data/lib/samagotchi/engine/sign_in.rb +142 -0
  62. data/lib/samagotchi/engine/turn_end.rb +3 -3
  63. data/lib/samagotchi/engine.rb +47 -27
  64. data/lib/samagotchi/engine_snapshots.rb +1 -1
  65. data/lib/samagotchi/events.rb +1 -0
  66. data/lib/samagotchi/guardrail_wiring.rb +3 -1
  67. data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
  68. data/lib/samagotchi/guardrails/verdict.rb +3 -2
  69. data/lib/samagotchi/hold_stop.rb +4 -4
  70. data/lib/samagotchi/idle_client.rb +10 -6
  71. data/lib/samagotchi/idle_recap.rb +19 -11
  72. data/lib/samagotchi/idle_reminders.rb +7 -5
  73. data/lib/samagotchi/idle_scheduler.rb +7 -10
  74. data/lib/samagotchi/inline_widgets.rb +12 -0
  75. data/lib/samagotchi/installed_bundles/dir.rb +37 -0
  76. data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
  77. data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
  78. data/lib/samagotchi/installed_bundles/record.rb +3 -2
  79. data/lib/samagotchi/installed_bundles/standing.rb +3 -1
  80. data/lib/samagotchi/kernel_loop.rb +7 -5
  81. data/lib/samagotchi/llm/chat_loop.rb +2 -0
  82. data/lib/samagotchi/llm/http.rb +96 -5
  83. data/lib/samagotchi/markdown_parser.rb +28 -0
  84. data/lib/samagotchi/mcp_command.rb +110 -0
  85. data/lib/samagotchi/mcp_server/http.rb +112 -0
  86. data/lib/samagotchi/mcp_server/protocol.rb +244 -0
  87. data/lib/samagotchi/mcp_server/stdio.rb +92 -0
  88. data/lib/samagotchi/mcp_server/text.rb +155 -0
  89. data/lib/samagotchi/mcp_server/tools.rb +245 -0
  90. data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
  91. data/lib/samagotchi/memory_bundle/builder.rb +20 -2
  92. data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
  93. data/lib/samagotchi/memory_bundle/installer.rb +16 -3
  94. data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
  95. data/lib/samagotchi/memory_bundle/shas.rb +6 -4
  96. data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
  97. data/lib/samagotchi/memory_bundle/status.rb +15 -6
  98. data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
  99. data/lib/samagotchi/plugin/api.rb +6 -1
  100. data/lib/samagotchi/plugin/card.rb +36 -11
  101. data/lib/samagotchi/plugin/context.rb +63 -6
  102. data/lib/samagotchi/plugin/http.rb +40 -15
  103. data/lib/samagotchi/plugin/http_errors.rb +15 -0
  104. data/lib/samagotchi/plugin/http_stream.rb +86 -0
  105. data/lib/samagotchi/plugin/http_transport.rb +58 -16
  106. data/lib/samagotchi/plugin/loader.rb +18 -11
  107. data/lib/samagotchi/plugin/secrets.rb +266 -0
  108. data/lib/samagotchi/plugin/sign_in.rb +392 -0
  109. data/lib/samagotchi/plugin/sse_reader.rb +103 -0
  110. data/lib/samagotchi/queued_command.rb +28 -4
  111. data/lib/samagotchi/recap_setup.rb +13 -4
  112. data/lib/samagotchi/ref.rb +3 -2
  113. data/lib/samagotchi/relay_verifier.rb +8 -7
  114. data/lib/samagotchi/relay_watcher.rb +4 -4
  115. data/lib/samagotchi/run_options.rb +2 -1
  116. data/lib/samagotchi/send_command.rb +54 -41
  117. data/lib/samagotchi/session_access/ask.rb +244 -0
  118. data/lib/samagotchi/session_access/contract.rb +8 -0
  119. data/lib/samagotchi/session_access/delete_plan.rb +77 -0
  120. data/lib/samagotchi/session_access/exchange/token.rb +101 -0
  121. data/lib/samagotchi/session_access/exchange.rb +173 -0
  122. data/lib/samagotchi/session_access/local.rb +38 -6
  123. data/lib/samagotchi/session_access/remote.rb +23 -2
  124. data/lib/samagotchi/session_access.rb +1 -0
  125. data/lib/samagotchi/session_chain/delete.rb +39 -81
  126. data/lib/samagotchi/session_chain/delete_preview.rb +55 -19
  127. data/lib/samagotchi/session_command_catalog.rb +10 -0
  128. data/lib/samagotchi/session_commands.rb +17 -0
  129. data/lib/samagotchi/session_config.rb +4 -4
  130. data/lib/samagotchi/session_delete_command.rb +129 -3
  131. data/lib/samagotchi/session_manager/delegate_rule.rb +86 -0
  132. data/lib/samagotchi/session_manager/listing.rb +4 -4
  133. data/lib/samagotchi/session_manager/removal.rb +38 -12
  134. data/lib/samagotchi/session_manager/running_tasks.rb +50 -23
  135. data/lib/samagotchi/session_manager.rb +1 -0
  136. data/lib/samagotchi/session_observer.rb +27 -0
  137. data/lib/samagotchi/steer_cut.rb +4 -3
  138. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
  139. data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
  140. data/lib/samagotchi/testing/boundary.rb +4 -0
  141. data/lib/samagotchi/testing/bundle.rb +48 -2
  142. data/lib/samagotchi/testing/fake_http.rb +60 -15
  143. data/lib/samagotchi/testing/host.rb +4 -1
  144. data/lib/samagotchi/testing/recorder.rb +45 -5
  145. data/lib/samagotchi/tools/tool_index.rb +14 -3
  146. data/lib/samagotchi/version.rb +1 -1
  147. data/lib/samagotchi/web/app.rb +20 -6
  148. data/lib/samagotchi/web/ask_routes.rb +292 -0
  149. data/lib/samagotchi/web/block_fix_routes.rb +39 -0
  150. data/lib/samagotchi/web/markdown_renderer.rb +24 -9
  151. data/lib/samagotchi/web/mcp_route.rb +88 -0
  152. data/lib/samagotchi/web/message_display.rb +6 -1
  153. data/lib/samagotchi/web/participant_view.rb +1 -1
  154. data/lib/samagotchi/web/public/app.js +7 -8
  155. data/lib/samagotchi/web/public/block_fix.js +270 -0
  156. data/lib/samagotchi/web/public/block_fix_note.js +104 -0
  157. data/lib/samagotchi/web/public/block_frame.js +77 -13
  158. data/lib/samagotchi/web/public/block_host.js +45 -9
  159. data/lib/samagotchi/web/public/block_regions.js +126 -0
  160. data/lib/samagotchi/web/public/block_renderers.css +12 -0
  161. data/lib/samagotchi/web/public/block_watch.js +16 -2
  162. data/lib/samagotchi/web/public/card.js +22 -9
  163. data/lib/samagotchi/web/public/card_actions.js +21 -0
  164. data/lib/samagotchi/web/public/card_kinds.js +1 -1
  165. data/lib/samagotchi/web/public/card_registry.js +3 -2
  166. data/lib/samagotchi/web/public/data.js +15 -12
  167. data/lib/samagotchi/web/public/delete_sheet.js +222 -162
  168. data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
  169. data/lib/samagotchi/web/public/format.js +3 -0
  170. data/lib/samagotchi/web/public/index.html +52 -7
  171. data/lib/samagotchi/web/public/inline_widgets.js +1 -1
  172. data/lib/samagotchi/web/public/more_sheet.js +6 -4
  173. data/lib/samagotchi/web/public/picture_regions.js +243 -0
  174. data/lib/samagotchi/web/public/picture_viewer.js +113 -12
  175. data/lib/samagotchi/web/public/refs.js +5 -3
  176. data/lib/samagotchi/web/public/region_hit.js +144 -0
  177. data/lib/samagotchi/web/public/removal_sheet_model.js +275 -0
  178. data/lib/samagotchi/web/public/running_tasks.js +6 -4
  179. data/lib/samagotchi/web/public/session_actions.js +105 -100
  180. data/lib/samagotchi/web/public/stream_handlers.js +5 -1
  181. data/lib/samagotchi/web/public/turn_block.js +5 -2
  182. data/lib/samagotchi/web/server.rb +6 -4
  183. data/lib/samagotchi/web/session_access_routes.rb +14 -11
  184. data/lib/samagotchi/web/session_control.rb +84 -30
  185. data/lib/samagotchi/web/session_create.rb +2 -2
  186. data/lib/samagotchi/worker/block_fixes.rb +22 -0
  187. data/lib/samagotchi/worker/child_side.rb +3 -2
  188. data/lib/samagotchi/worker/command_queue.rb +13 -13
  189. data/lib/samagotchi/worker/leave.rb +3 -3
  190. data/lib/samagotchi/worker/scratch.rb +6 -6
  191. data/lib/samagotchi/worker/signal_grace.rb +6 -5
  192. data/lib/samagotchi/worker/startup.rb +3 -3
  193. data/lib/samagotchi/worker/wake_turns.rb +4 -4
  194. data/lib/samagotchi/worker.rb +42 -24
  195. data/lib/samagotchi/worker_exit_claim.rb +1 -1
  196. data/lib/samagotchi/worker_idle_exit.rb +18 -9
  197. data/lib/samagotchi/worker_inbound.rb +12 -7
  198. data/lib/samagotchi/worker_participant_gate.rb +9 -7
  199. data/lib/samagotchi/worker_task_wakes.rb +7 -5
  200. metadata +42 -1
@@ -125,7 +125,7 @@ module Samagotchi
125
125
  record_history(body, prompt) if !idle && PromptHistory.shell_line?(prompt.to_s.strip)
126
126
  json_response(201, session_to_json(session).merge(bridge_port: port, model_warning: session.model_warning,
127
127
  parent_answers_warning: Guardrails::ParentPolicy.capped_note(parent_answers),
128
- first_turn_id: session.first_turn_id))
128
+ first_turn_id: session.first_turn_id, **create_token(session)))
129
129
  end
130
130
 
131
131
  # POST /api/sessions with continues: <id, prefix or last:id>: the next
@@ -171,7 +171,7 @@ module Samagotchi
171
171
  model_warning: session.model_warning,
172
172
  opener: session.opener,
173
173
  moved_context: session.moved_context&.to_h,
174
- first_turn_id: session.first_turn_id))
174
+ first_turn_id: session.first_turn_id, **create_token(session)))
175
175
  end
176
176
 
177
177
  # The previous link's parent_answers and the model the new one runs,
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../degrade"
4
+
5
+ module Samagotchi
6
+ class Worker
7
+ # A failed block's fix (Engine::BlockFixPart) lands on the loop, between
8
+ # turns: in each pass that runs the queued commands and absorbs the
9
+ # notes, after the commands and before the notes and the next prompt, so
10
+ # a fix ready as a turn ends is in the conversation the next queued turn
11
+ # sends; one that isn't lands when that turn ends. Reads @engine.
12
+ module BlockFixesPart
13
+ private
14
+
15
+ # The fixes waiting, written into the conversation and saved.
16
+ def apply_block_fixes
17
+ applied = Degrade.call(:worker, "block_fix_apply_failed", fallback: 0) { @engine.apply_block_fixes }
18
+ save_or_log(:block_fix) { save_session } if applied.positive?
19
+ end
20
+ end
21
+ end
22
+ end
@@ -19,7 +19,8 @@ module Samagotchi
19
19
  # Verify fresh hand-off. Writes @owes_parent, @asked_parent and
20
20
  # @reporting_turn (also written by #run_engine_turn, with @initial_turn
21
21
  # from #run_initial_prompt; #initialize sets them). Reads @session,
22
- # @state_dir, @session_dir, @engine, @child_reports and @initial_turn.
22
+ # @state_dir, @session_dir, @engine, @event_log, @child_reports and
23
+ # @initial_turn.
23
24
  module ChildSidePart
24
25
  private
25
26
 
@@ -44,7 +45,7 @@ module Samagotchi
44
45
  return unless save_or_log(:fresh_verdicts) { save_session }
45
46
 
46
47
  FileUtils.rm_f(files)
47
- added.each { |record| @engine.announce(type: :verdict_recorded, verdict: record) }
48
+ added.each { |record| @event_log.announce(type: :verdict_recorded, verdict: record) }
48
49
  rescue StandardError => e
49
50
  Log.warn(:worker, "fresh_verdicts_failed", error: e.class.name, msg: e.message)
50
51
  end
@@ -15,12 +15,12 @@ module Samagotchi
15
15
  # any queued prompt, and refuses or drops what arrives mid-turn. Includes
16
16
  # the "what waits" predicates #input_pending?, #held_for_offer? and
17
17
  # #nothing_queued?. LOCK RULE: the queue is touched with the event log held
18
- # (@engine.synchronize_events): #on_command on a Bridge thread, the pops
18
+ # (@event_log.synchronize): #on_command on a Bridge thread, the pops
19
19
  # and re-pushes in #next_ready_command, #refuse_queued_commands and
20
20
  # #drop_queued_commands. The `empty?` peeks (those, #input_pending?,
21
21
  # #nothing_queued?, #start's next_turn_waiting lambda) are benign by
22
22
  # design. Writes @command_queue's contents (created in #initialize).
23
- # Reads @engine, @session_dir, @commands (built in #start),
23
+ # Reads @engine, @event_log, @turn_state, @session_dir, @commands (built in #start),
24
24
  # @continue_offer, @turn_end_seq, @turn_end_type (#start's observer),
25
25
  # @turn_flow and @waker.
26
26
  module CommandQueuePart
@@ -72,7 +72,7 @@ module Samagotchi
72
72
  def next_ready_command
73
73
  return nil if @command_queue.empty?
74
74
 
75
- @engine.synchronize_events do
75
+ @event_log.synchronize do
76
76
  commands = drain_commands
77
77
  first = commands.first
78
78
  ready = first && ready_command?(first) ? commands.shift : nil
@@ -111,7 +111,7 @@ module Samagotchi
111
111
  def refuse_queued_commands(mid_turn: false)
112
112
  return if @command_queue.empty?
113
113
 
114
- @engine.synchronize_events do
114
+ @event_log.synchronize do
115
115
  drain_commands.each do |command|
116
116
  during = mid_turn || command.after_seq.to_i < @turn_end_seq
117
117
  case (during ? turn_answer(command, ended: !mid_turn) : :keep)
@@ -144,7 +144,7 @@ module Samagotchi
144
144
  def waiting_for_turn_end(command)
145
145
  return command unless command.mid_turn == :loop && @engine.command_registry.mid_turn(command.line) == :queue
146
146
 
147
- @engine.announce(type: :command_queued, **command.event_fields, waits: "turn_end")
147
+ @event_log.announce(type: :command_queued, **command.event_fields, waits: "turn_end")
148
148
  command.with(mid_turn: :queue)
149
149
  end
150
150
 
@@ -164,7 +164,7 @@ module Samagotchi
164
164
  def drop_queued_commands
165
165
  return if @command_queue.nil? || @command_queue.empty? || @engine.nil?
166
166
 
167
- @engine.synchronize_events do
167
+ @event_log.synchronize do
168
168
  drain_commands.each { |command| drop_command(command, "dropped: the session's worker stopped before it ran") }
169
169
  end
170
170
  rescue StandardError => e
@@ -181,11 +181,11 @@ module Samagotchi
181
181
  awaiting = @continue_offer.awaiting?
182
182
  result, shown = run_command_line(command)
183
183
  resolved = @continue_offer.resolved?(awaiting, result)
184
- @engine.synchronize_events do
184
+ @event_log.synchronize do
185
185
  @continue_offer.announce_resolved(result, command) if resolved
186
186
  announce_command(command, status: result.status.to_s, output: result.output, changed: Array(result.changed),
187
187
  confirm: result.confirm)
188
- shown.each { |event| @engine.announce(event) }
188
+ shown.each { |event| @event_log.announce(event) }
189
189
  end
190
190
  save_or_log(:command) { save_session } unless Array(result.changed).empty?
191
191
  user_input(command.client_id) if resolved
@@ -199,7 +199,7 @@ module Samagotchi
199
199
  def run_command_line(command)
200
200
  @engine.holding_announcements do
201
201
  command_result(command) do
202
- @commands.run(command.line, confirm: confirm?(command), as_parent: as_parent?(command), user_ui: user_ui?(command))
202
+ @commands.run(command.run_line, confirm: confirm?(command), as_parent: as_parent?(command), user_ui: user_ui?(command))
203
203
  end
204
204
  end
205
205
  end
@@ -258,7 +258,7 @@ module Samagotchi
258
258
  # :refuse while a turn runs, :loop idle
259
259
  def on_command(command, after_file: nil)
260
260
  registry = @engine.command_registry
261
- policy = if @engine.turn_running? then registry.mid_turn(command.line)
261
+ policy = if @turn_state.running? then registry.mid_turn(command.line)
262
262
  else registry.anytime?(command.line) ? :anytime : :loop
263
263
  end
264
264
  if policy == :anytime
@@ -268,7 +268,7 @@ module Samagotchi
268
268
 
269
269
  after_file ||= newest_input_file if policy == :queue
270
270
  # The count says which turn ends came before it.
271
- @command_queue << command.with(after_seq: @engine.event_count, mid_turn: policy, after_file: after_file)
271
+ @command_queue << command.with(after_seq: @event_log.event_count, mid_turn: policy, after_file: after_file)
272
272
  @waker.wake
273
273
  policy
274
274
  end
@@ -294,7 +294,7 @@ module Samagotchi
294
294
  @engine.spawn_anytime do
295
295
  result = command_result(command) do
296
296
  @engine.running_anytime do
297
- @commands.run(command.line, confirm: confirm?(command), as_parent: as_parent?(command), user_ui: user_ui?(command))
297
+ @commands.run(command.run_line, confirm: confirm?(command), as_parent: as_parent?(command), user_ui: user_ui?(command))
298
298
  end
299
299
  end
300
300
  # Saved before its command_ran: a client that reads the file on the reply finds it.
@@ -331,7 +331,7 @@ module Samagotchi
331
331
  SessionSetting::ALL.each { |setting| event[setting.key] = setting.summary(@engine) if setting.changed_by?(changed) }
332
332
  event[:confirm] = confirm.to_h if confirm
333
333
  event[:output_truncated] = true if text.length > COMMAND_OUTPUT_LIMIT
334
- @engine.announce(event)
334
+ @event_log.announce(event)
335
335
  end
336
336
 
337
337
  # +text+ as its :command_ran carries it: over the limit, its head to a
@@ -13,7 +13,7 @@ module Samagotchi
13
13
  # carried reminders, the recap, the discard). Writes @marked_leaving
14
14
  # (#mark_leaving) and @discard (#left), and the exit claim through its own
15
15
  # methods; reads @exit_claim, @idle_exit, @hold_stop, @session, @session_id,
16
- # @session_dir, @state_dir, @default_model, @engine, @bridge, @owner_lock,
16
+ # @session_dir, @state_dir, @default_model, @engine, @event_log, @bridge, @owner_lock,
17
17
  # @waker and @task_wakes.
18
18
  module LeavePart
19
19
  # /exit held by background tasks a finish wake waits for: how to end them.
@@ -109,7 +109,7 @@ module Samagotchi
109
109
  # decided under the lock), false when something came in since the request
110
110
  def leave_on_request
111
111
  stop_bridge_after do
112
- @engine.synchronize_events do
112
+ @event_log.synchronize do
113
113
  # One read: the kind and the asker of the same exit.
114
114
  taken = @exit_claim.taken
115
115
  restart = taken&.restart?
@@ -143,7 +143,7 @@ module Samagotchi
143
143
  # @return [Boolean] false when something came in since #due?
144
144
  def leave_idle
145
145
  stop_bridge_after do
146
- @engine.synchronize_events do
146
+ @event_log.synchronize do
147
147
  next false unless @idle_exit.due?
148
148
 
149
149
  @bridge&.close_intake
@@ -14,7 +14,7 @@ module Samagotchi
14
14
  # SCRATCH_JOIN_GRACE and SCRATCH_GONE_AFTER. Writes @unattended_since
15
15
  # (the scratch thread and the loop, no lock: a monotonic stamp, benign).
16
16
  # Reads @poll_interval, @session, @bridge, @started_at (#start), @engine,
17
- # @session_id and @waker. #scratch? and @scratch_watch (#start_background,
17
+ # @turn_state, @question_desk, @event_log, @session_id and @waker. #scratch? and @scratch_watch (#start_background,
18
18
  # #teardown) stay in Worker.
19
19
  module ScratchPart
20
20
  # How long a scratch worker waits for its terminal's first stream
@@ -46,11 +46,11 @@ module Samagotchi
46
46
  end
47
47
 
48
48
  def cancel_unattended_turn
49
- return unless @engine.turn_running? && scratch_unattended?
49
+ return unless @turn_state.running? && scratch_unattended?
50
50
 
51
51
  Log.info(:worker, "scratch_turn_cancelled", sid: @session_id)
52
- @engine.cancel_question(SessionManager::STOP_CANCEL_REASON) if @engine.pending_question
53
- @engine.cancel_current_turn!(SessionManager::STOP_CANCEL_REASON.to_sym)
52
+ @question_desk.cancel(SessionManager::STOP_CANCEL_REASON) if @question_desk.pending
53
+ @turn_state.cancel!(SessionManager::STOP_CANCEL_REASON.to_sym)
54
54
  @waker.wake
55
55
  end
56
56
 
@@ -83,9 +83,9 @@ module Samagotchi
83
83
  # @return [Boolean] false when a client came back since
84
84
  def leave_scratch
85
85
  stop_bridge_after do
86
- @engine.synchronize_events do
86
+ @event_log.synchronize do
87
87
  # A turn still running is the scratch watch's to cancel first.
88
- next false unless scratch_unattended? && !@engine.turn_running?
88
+ next false unless scratch_unattended? && !@turn_state.running?
89
89
 
90
90
  @bridge&.close_intake
91
91
  @engine.stop_idle(join: false)
@@ -11,7 +11,8 @@ module Samagotchi
11
11
  # (under @signal_mutex). Worker writes the rest it reads: @run_thread
12
12
  # (#run), @loop_started (#run_loop), @left_loop (#run's ensure),
13
13
  # @turn_ending (the turn observer) and the grace ivars and @signal_mutex
14
- # (#initialize). Reads those, @signals, @engine, @waker, @session_id and
14
+ # (#initialize). Reads those, @signals, @turn_state (nil-guarded: a
15
+ # signal may come before #start set it), @waker, @session_id and
15
16
  # @state_dir.
16
17
  module SignalGracePart
17
18
  # How long a signalled worker's turn has to end (it is canceled at once,
@@ -48,7 +49,7 @@ module Samagotchi
48
49
  # for it (the loop's first look leaves). A signal after the loop left
49
50
  # (the teardown, the caller's last steps) only brings the hard exit.
50
51
  def signalled(name, count)
51
- Log.info(:worker, "signal", signal: name, count: count, turn_running: @engine&.turn_running? || false)
52
+ Log.info(:worker, "signal", signal: name, count: count, turn_running: @turn_state&.running? || false)
52
53
  return signal_hard_exit if @left_loop
53
54
 
54
55
  @waker.wake
@@ -58,9 +59,9 @@ module Samagotchi
58
59
  hooks_deadline = deadline + @signal_hard_exit
59
60
  ended = false
60
61
  until @left_loop
61
- running = @engine&.turn_running?
62
+ running = @turn_state&.running?
62
63
  ended ||= running && @turn_ending
63
- @engine.cancel_current_turn!(:signal) if running && !ended
64
+ @turn_state.cancel!(:signal) if running && !ended
64
65
  break if @loop_started && Process.clock_gettime(Process::CLOCK_MONOTONIC) >= (ended ? hooks_deadline : deadline)
65
66
 
66
67
  sleep(SIGNAL_RETRY)
@@ -85,7 +86,7 @@ module Samagotchi
85
86
  expired = @signal_mutex.synchronize { !@grace_expired && !@left_loop && (@grace_expired = true) }
86
87
  return unless expired
87
88
 
88
- Log.warn(:worker, "signal_grace_expired", signal: name, turn_running: @engine&.turn_running? || false)
89
+ Log.warn(:worker, "signal_grace_expired", signal: name, turn_running: @turn_state&.running? || false)
89
90
  save_or_log(:signal) { save_session }
90
91
  # Not once the loop has left (it may have, during the save).
91
92
  @signal_mutex.synchronize { @run_thread&.raise(SignalException.new(name)) unless @left_loop }
@@ -14,7 +14,7 @@ module Samagotchi
14
14
  # #drop_dead_question. Called by #start and #serve only; no state of its
15
15
  # own. Writes @session (the interrupted turn's end, the stale status, a
16
16
  # dead question) and saves it. Reads @session, @session_dir, @session_id,
17
- # @state_dir and @engine.
17
+ # @state_dir, @engine and @event_log.
18
18
  module StartupPart
19
19
  private
20
20
 
@@ -71,8 +71,8 @@ module Samagotchi
71
71
  value = @session.parent_answers
72
72
  return unless Guardrails::ParentPolicy.unknown_level?(value)
73
73
 
74
- @engine.announce({ type: :guardrail_warning, label: "parent_answers",
75
- message: "parent_answers #{value.inspect} in session #{@session_id} isn't hard/standard/soft; hard applies" })
74
+ @event_log.announce({ type: :guardrail_warning, label: "parent_answers",
75
+ message: "parent_answers #{value.inspect} in session #{@session_id} isn't hard/standard/soft; hard applies" })
76
76
  end
77
77
 
78
78
  # A question saved by a worker that died while it waited (an
@@ -17,7 +17,7 @@ module Samagotchi
17
17
  # the idle loop's sleep (#idle_wait). Glue over WorkerWakes, WorkerTaskWakes
18
18
  # and WorkerInbound: it writes no ivar of its own. Reads @child_reports,
19
19
  # @wakes, @turn_flow, @inbound, @continue_offer, @task_wakes, @engine,
20
- # @session_dir and @poll_interval.
20
+ # @event_log, @session_dir and @poll_interval.
21
21
  module WakeTurnsPart
22
22
  # A reminder turn's origin: chi's own (system:reminder). Its run gets
23
23
  # an id of its own (#wake_origin); this constant has none.
@@ -79,9 +79,9 @@ module Samagotchi
79
79
  return unless @wakes.notice_budget!
80
80
 
81
81
  count = SessionInbox.find_ring_files(@session_dir).map { |f| SessionInbox.read_ring(f)&.dig(:child_id) }.uniq.size
82
- @engine.announce(type: :hook_notice, hook: "delegate", level: :info, between_turns: true,
83
- text: "#{count} delegate report#{"s" if count != 1} waiting; #{count == 1 ? "it joins" : "they join"} " \
84
- "your next message (#{@wakes.max} turn#{"s" if @wakes.max != 1} ran for reports in a row, #{ChildRing::MAX_WAKES_KEY})")
82
+ @event_log.announce(type: :hook_notice, hook: "delegate", level: :info, between_turns: true,
83
+ text: "#{count} delegate report#{"s" if count != 1} waiting; #{count == 1 ? "it joins" : "they join"} " \
84
+ "your next message (#{@wakes.max} turn#{"s" if @wakes.max != 1} ran for reports in a row, #{ChildRing::MAX_WAKES_KEY})")
85
85
  end
86
86
 
87
87
  # A wake turn (a delegate report's, an attached context change's, a
@@ -48,6 +48,7 @@ require_relative "worker/signal_grace"
48
48
  require_relative "worker/scratch"
49
49
  require_relative "worker/child_side"
50
50
  require_relative "worker/command_queue"
51
+ require_relative "worker/block_fixes"
51
52
 
52
53
  module Samagotchi
53
54
  # The loop of a background session worker, once it owns the session (see
@@ -82,13 +83,14 @@ module Samagotchi
82
83
  # the loop's thread, as Ruby's default would (the teardown still runs).
83
84
  #
84
85
  # This file keeps the loop, the wiring, the prompt turns and the steering
85
- # drain. Seven concerns are parts, lib/samagotchi/worker/<concern>.rb
86
+ # drain. Eight concerns are parts, lib/samagotchi/worker/<concern>.rb
86
87
  # holding Worker::<Concern>Part, included here (they share Worker's ivars):
87
88
  # LeavePart (exit and leave), WakeTurnsPart (the turns nobody typed),
88
89
  # StartupPart (the start-up settle), SignalGracePart (a signal's grace),
89
90
  # ScratchPart (a scratch worker whose terminal left), ChildSidePart (a
90
- # delegate child's rings, Verify fresh verdicts) and CommandQueuePart
91
- # (session commands between turns).
91
+ # delegate child's rings, Verify fresh verdicts), CommandQueuePart
92
+ # (session commands between turns) and BlockFixesPart (a failed block's
93
+ # fix, between turns).
92
94
  class Worker
93
95
  include LeavePart
94
96
  include WakeTurnsPart
@@ -97,6 +99,7 @@ module Samagotchi
97
99
  include ScratchPart
98
100
  include ChildSidePart
99
101
  include CommandQueuePart
102
+ include BlockFixesPart
100
103
 
101
104
  FALLBACK_TICK_SECONDS = 5
102
105
  # A worker just started runs no wake turn for delegate reports this
@@ -273,9 +276,16 @@ module Samagotchi
273
276
  # runs from the leave) and restored into it after.
274
277
  carried = take_carried_reminders
275
278
  @engine = build_engine
279
+ # The Engine's event log, turn state, question desk and steer cut,
280
+ # handed to the Bridge and the collaborators (read here, not in
281
+ # #start_bridge, whose rescue would hide a missing one).
282
+ @event_log = @engine.event_log
283
+ @turn_state = @engine.turn_state
284
+ @question_desk = @engine.question_desk
285
+ @steer_cut = @engine.steer_cut
276
286
  restore_carried_reminders(carried)
277
287
  @config_restart_unsubscribe = ConfigFile.watch_restart_settings!(process: :worker) do |message|
278
- @engine.announce(type: :hook_notice, hook: "config", text: message, level: :warn, between_turns: true)
288
+ @event_log.announce(type: :hook_notice, hook: "config", text: message, level: :warn, between_turns: true)
279
289
  end
280
290
  prune_tombstones
281
291
  settle_interrupted_turn
@@ -286,7 +296,7 @@ module Samagotchi
286
296
  @engine.session = @session
287
297
  # May this participant act (loop thread only): a turn as it starts,
288
298
  # the drain, the mid-turn stop, the Continue.
289
- @participant_gate = WorkerParticipantGate.new(engine: @engine, session: @session, session_id: @session_id,
299
+ @participant_gate = WorkerParticipantGate.new(engine: @engine, event_log: @event_log, session: @session, session_id: @session_id,
290
300
  state_dir: @state_dir, session_dir: @session_dir,
291
301
  hook: Engine::PARTICIPANTS_HOOK)
292
302
  # Who is in the review as the worker starts: no notice for them
@@ -301,7 +311,8 @@ module Samagotchi
301
311
  @turn_flow = TurnFlow.new(engine: @engine)
302
312
  # Its question's answer is queued as the matching /continue (the
303
313
  # Bridge exists by the time anyone answers).
304
- @continue_offer = ContinueOffer.new(engine: @engine, turn_flow: @turn_flow, run_turn: method(:run_engine_turn),
314
+ @continue_offer = ContinueOffer.new(engine: @engine, event_log: @event_log, turn_state: @turn_state,
315
+ question_desk: @question_desk, turn_flow: @turn_flow, run_turn: method(:run_engine_turn),
305
316
  max_iterations: method(:max_iterations),
306
317
  participant_refused: @participant_gate.method(:refusal),
307
318
  save: -> { save_or_log(:command) { save_session } },
@@ -316,11 +327,11 @@ module Samagotchi
316
327
  @turn_end_seq = 0
317
328
  # The background tasks this session started and hasn't seen finish:
318
329
  # one that finishes unseen wakes the idle session (#run_task_wake).
319
- @task_wakes = WorkerTaskWakes.new(engine: @engine, session: @session, wakes: @wakes,
330
+ @task_wakes = WorkerTaskWakes.new(engine: @engine, event_log: @event_log, session: @session, wakes: @wakes,
320
331
  awaiting_continue: -> { @turn_flow.awaiting_continue? },
321
332
  idle: -> { nothing_queued? && !@exit_claim.taken? },
322
333
  save: -> { save_or_log(:task_notes) { save_session } })
323
- @engine.subscribe(observer: lambda { |event|
334
+ @event_log.subscribe(observer: lambda { |event|
324
335
  if Events::TURN_END.include?(event[:type])
325
336
  @turn_end_seq = event[:event_seq]
326
337
  @turn_end_type = event[:type]
@@ -342,7 +353,7 @@ module Samagotchi
342
353
  peer_sources: @engine.peer_sources, peer_turns: -> { @wakes.peer_turns })
343
354
  # What comes in between turns, and how each kind is saved.
344
355
  @inbound = WorkerInbound.new(session: @session, state_dir: @state_dir, session_dir: @session_dir, engine: @engine,
345
- context_absorber: @context_absorber, wakes: @wakes,
356
+ event_log: @event_log, turn_state: @turn_state, context_absorber: @context_absorber, wakes: @wakes,
346
357
  awaiting_continue: -> { @turn_flow.awaiting_continue? },
347
358
  stopped: -> { stopped_on_disk? },
348
359
  queue_command: lambda { |line, client_id, after_file, enqueued_id, author: nil|
@@ -366,7 +377,8 @@ module Samagotchi
366
377
  # The Bridge (UIs and clients reach the worker from here on), then the
367
378
  # idle-exit policy.
368
379
  def serve
369
- @bridge = start_bridge(engine: @engine, state_dir: @state_dir, session_id: @session_id,
380
+ @bridge = start_bridge(engine: @engine, event_log: @event_log, turn_state: @turn_state, question_desk: @question_desk,
381
+ steer_cut: @steer_cut, state_dir: @state_dir, session_id: @session_id,
370
382
  on_input: -> { @waker.wake },
371
383
  on_command: method(:on_command),
372
384
  on_exit_request: method(:exit_request),
@@ -385,7 +397,7 @@ module Samagotchi
385
397
  @engine.announce_load_events!
386
398
  announce_session_warnings
387
399
  @idle_exit = WorkerIdleExit.new(
388
- engine: @engine, bridge: @bridge,
400
+ engine: @engine, turn_state: @turn_state, question_desk: @question_desk, bridge: @bridge,
389
401
  timeout_minutes: @idle_exit_minutes || config_idle_exit_minutes,
390
402
  input_pending: method(:input_pending?),
391
403
  awaiting_continue: -> { @turn_flow.awaiting_continue? },
@@ -400,8 +412,8 @@ module Samagotchi
400
412
  # them so "stop them and exit" stops them too.
401
413
  delegates: method(:live_delegates)
402
414
  )
403
- @hold_stop = HoldStop.new(engine: @engine, task_watch: @task_wakes.watch, delegates: method(:live_delegates),
404
- state_dir: @state_dir)
415
+ @hold_stop = HoldStop.new(reminder_store: @engine.reminder_store, task_watch: @task_wakes.watch,
416
+ delegates: method(:live_delegates), state_dir: @state_dir)
405
417
  end
406
418
 
407
419
  # Start the in-process Bridge transport for this worker. The bridge is
@@ -414,11 +426,13 @@ module Samagotchi
414
426
  # (the worker degrades: turns still flow through the input-dir loop, but
415
427
  # there is no live SSE or in-process cancel/answer).
416
428
  # @param on_input [#call, nil] called after the Bridge queues a turn
417
- def start_bridge(engine:, state_dir:, session_id:, on_input: nil, on_command: nil, on_exit_request: nil,
418
- exit_discards: nil, exit_stopped: nil, on_holds: nil, on_holds_stop: nil, on_activity: nil)
429
+ def start_bridge(engine:, event_log:, turn_state:, question_desk:, steer_cut:, state_dir:, session_id:, on_input: nil,
430
+ on_command: nil, on_exit_request: nil, exit_discards: nil, exit_stopped: nil, on_holds: nil,
431
+ on_holds_stop: nil, on_activity: nil)
419
432
  require_relative "bridge"
420
433
  Samagotchi::Bridge.new(
421
- engine: engine, state_dir: state_dir, session_id: session_id, input_format: SessionInbox::INPUT_FORMAT,
434
+ engine: engine, event_log: event_log, turn_state: turn_state, question_desk: question_desk, steer_cut: steer_cut,
435
+ state_dir: state_dir, session_id: session_id, input_format: SessionInbox::INPUT_FORMAT,
422
436
  on_input: on_input, on_command: on_command, on_exit_request: on_exit_request, exit_discards: exit_discards,
423
437
  exit_stopped: exit_stopped, on_holds: on_holds, on_holds_stop: on_holds_stop, on_activity: on_activity
424
438
  ).start
@@ -438,7 +452,7 @@ module Samagotchi
438
452
  # @return [Hash]
439
453
  def activity_snapshot
440
454
  {
441
- turn_running: @engine.turn_running?,
455
+ turn_running: @turn_state.running?,
442
456
  input_pending: input_pending?,
443
457
  awaiting_continue: @turn_flow.awaiting_continue?,
444
458
  open_streams: @bridge&.open_streams.to_i,
@@ -482,6 +496,7 @@ module Samagotchi
482
496
  # before a new prompt still answers the offer).
483
497
  return if run_queued_commands
484
498
 
499
+ apply_block_fixes
485
500
  # Between turns, so the next turn (the first one too) sees them.
486
501
  # A change that asks to wake may start a turn here (C4), but not
487
502
  # ahead of an exit or restart a client asked for: it is a note then.
@@ -514,7 +529,7 @@ module Samagotchi
514
529
  return if run_wake_turn
515
530
  return if run_task_wake
516
531
  return if run_due_reminders
517
- return left(:scratch_left) if scratch_unattended? && !@engine.turn_running? && leave_scratch
532
+ return left(:scratch_left) if scratch_unattended? && !@turn_state.running? && leave_scratch
518
533
  return left(:idle_exit) if @idle_exit.due? && leave_idle
519
534
 
520
535
  leaving = @exit_claim.taken? && leave_on_request
@@ -543,6 +558,7 @@ module Samagotchi
543
558
  # Commands that came before this file (D1: a queued /model X sent
544
559
  # before this prompt, a command an earlier file carried) run first.
545
560
  run_queued_commands
561
+ apply_block_fixes
546
562
  @inbound.absorb_notes
547
563
  if held ? SessionInbox.participant_input?(input_file) : held_for_offer?(input_file)
548
564
  held = true
@@ -623,6 +639,8 @@ module Samagotchi
623
639
  engine.interface = :worker
624
640
  # A pair turn stops mid-turn at its participant's cap.
625
641
  engine.participant_stop = ->(author) { @participant_gate.turn_stop(author) }
642
+ # A block fix's result wakes the loop, which applies it between turns.
643
+ engine.block_fix_ready = -> { waker.wake }
626
644
  engine.guardrail_state_dir = @state_dir if @state_dir
627
645
  engine.session_state_dir = @state_dir if @state_dir
628
646
  engine
@@ -792,14 +810,14 @@ module Samagotchi
792
810
  # conversation as a turn note
793
811
  # Not saved here: run_engine_turn saves after its block.
794
812
  def restore_failed_turn(prompts, error: nil)
795
- @engine.synchronize_events do
813
+ @event_log.synchronize do
796
814
  next if @turn_flow.prompt_turn_failed(error: error) == :kept
797
815
 
798
816
  prompts.each do |prompt, origin, images, anchors|
799
817
  restored = { type: :prompt_restored, prompt: prompt, origin: origin.wire }
800
818
  restored[:images] = images unless Array(images).empty?
801
819
  restored.merge!(anchors.to_h) if anchors
802
- @engine.announce(restored)
820
+ @event_log.announce(restored)
803
821
  end
804
822
  end
805
823
  end
@@ -834,7 +852,7 @@ module Samagotchi
834
852
  begin
835
853
  input = SessionInbox.read_input(claimed_file)
836
854
  enqueued_id = input.origin.enqueued_id
837
- if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@engine.claim_enqueued_id(enqueued_id)
855
+ if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@turn_state.claim_enqueued_id(enqueued_id)
838
856
  Log.info(:worker, "duplicate_input_dropped", enqueued_id: enqueued_id)
839
857
  next
840
858
  end
@@ -852,7 +870,7 @@ module Samagotchi
852
870
  SessionInbox.mark_merged(@session_dir, enqueued_id, into: @turn_enqueued_id) unless prompt.empty?
853
871
  prompt.empty? ? nil : [prompt, input.origin]
854
872
  ensure
855
- @engine.finish_enqueued_id(enqueued_id)
873
+ @turn_state.finish_enqueued_id(enqueued_id)
856
874
  end
857
875
  ensure
858
876
  FileUtils.rm_f(claimed_file)
@@ -863,8 +881,8 @@ module Samagotchi
863
881
  # (their rings stay for the next turn), so not in @merged_this_turn.
864
882
  reports, mark = take_child_reports
865
883
  unless merged.empty? && reports.empty?
866
- @engine.announce(type: :input_merged, count: merged.size + reports.size,
867
- origins: (merged.map(&:last) + reports.map(&:origin)).filter_map(&:wire))
884
+ @event_log.announce(type: :input_merged, count: merged.size + reports.size,
885
+ origins: (merged.map(&:last) + reports.map(&:origin)).filter_map(&:wire))
868
886
  @merged_this_turn.concat(merged)
869
887
  end
870
888
  # Each line keeps its sender (Steer.source_for_client): a chi send or
@@ -12,7 +12,7 @@ module Samagotchi
12
12
  # Threads: safe from any thread. Its fields change only under its own
13
13
  # mutex, and the taken exit is one value replaced whole (#taken), so a
14
14
  # reader never sees a kind with another request's flags. Today every
15
- # writer also holds the event lock (Engine#synchronize_events: the Bridge's
15
+ # writer also holds the event lock (SessionObserver#synchronize: the Bridge's
16
16
  # /exit, the loop's leave); nothing here depends on it. Lock order: the
17
17
  # event lock, then this one.
18
18
  class WorkerExitClaim
@@ -66,7 +66,12 @@ module Samagotchi
66
66
  # "2m", "running a turn").
67
67
  HoldItem = Data.define(:id, :label, :detail)
68
68
 
69
- # @param engine [Engine] #turn_running?, #last_activity_at, #reminder_store
69
+ # @param engine [Engine] #reminder_store, #sign_in_waiting?,
70
+ # #anytime_running?, #relay_desk
71
+ # @param turn_state [TurnState] the Engine's (Engine#turn_state):
72
+ # #running?, #last_activity_at
73
+ # @param question_desk [QuestionDesk] the Engine's
74
+ # (Engine#question_desk): #pending
70
75
  # @param bridge [Bridge, nil] #open_streams, #last_client_activity_at; nil
71
76
  # when the worker's Bridge failed to start
72
77
  # @param timeout_minutes [Numeric, nil] 0 or nil: never exit
@@ -83,10 +88,12 @@ module Samagotchi
83
88
  # or with a live worker (ChildrenStatus::Child): they never hold the
84
89
  # exit, but the exit ask lists them so "stop them" stops them too
85
90
  # @param clock [#call] monotonic seconds, the Engine's and Bridge's clock
86
- def initialize(engine:, bridge:, timeout_minutes:, input_pending:, awaiting_continue: -> { false },
91
+ def initialize(engine:, turn_state:, question_desk:, bridge:, timeout_minutes:, input_pending:, awaiting_continue: -> { false },
87
92
  running_tasks: -> { [] }, watched_tasks: -> { [] }, watched_running: -> { [] },
88
93
  delegates: -> { [] }, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
89
94
  @engine = engine
95
+ @turn_state = turn_state
96
+ @question_desk = question_desk
90
97
  @bridge = bridge
91
98
  @timeout = timeout_minutes.to_f * 60
92
99
  @input_pending = input_pending
@@ -106,16 +113,18 @@ module Samagotchi
106
113
 
107
114
  # What keeps the worker up, or nil when nothing does.
108
115
  # @return [Symbol, nil] :disabled, :turn_running, :input_queued,
109
- # :continue_offered, :client_connected, :reminders, :background_tasks
110
- # or :recent_activity
116
+ # :continue_offered, :client_connected, :reminders, :background_tasks,
117
+ # :sign_in_waiting (ctx.sign_in's flow, until its own timeout) or
118
+ # :recent_activity
111
119
  def hold
112
120
  return :disabled unless @timeout.positive?
113
- return :turn_running if @engine.turn_running?
121
+ return :turn_running if @turn_state.running?
114
122
  return :input_queued if @input_pending.call
115
123
  return :continue_offered if @awaiting_continue.call
116
124
  return :client_connected if @bridge && @bridge.open_streams.positive?
117
125
  return :reminders if @engine.reminder_store&.any?
118
126
  return :background_tasks if @watched_tasks.call.any?
127
+ return :sign_in_waiting if @engine.sign_in_waiting?
119
128
  return :recent_activity if idle_seconds < @timeout
120
129
 
121
130
  nil
@@ -165,7 +174,7 @@ module Samagotchi
165
174
  # leaving) ignores them.
166
175
  # @return [Hold, nil]
167
176
  def request_hold(requester:, streams: true, hint: nil, ask: false)
168
- reason = if @engine.turn_running? then :turn_running
177
+ reason = if @turn_state.running? then :turn_running
169
178
  elsif @input_pending.call then :input_queued
170
179
  elsif @awaiting_continue.call then :continue_offered
171
180
  elsif streams && @bridge && @bridge.open_streams_except(requester).positive? then :client_connected
@@ -193,10 +202,10 @@ module Samagotchi
193
202
  # :question_pending, :command_running, :relays_open or
194
203
  # :background_tasks
195
204
  def hold_for_restart
196
- return :turn_running if @engine.turn_running?
205
+ return :turn_running if @turn_state.running?
197
206
  return :input_queued if @input_pending.call
198
207
  return :continue_offered if @awaiting_continue.call
199
- return :question_pending if @engine.pending_question
208
+ return :question_pending if @question_desk.pending
200
209
  return :command_running if @engine.anytime_running?
201
210
  return :relays_open if @engine.relay_desk.active?
202
211
  return :background_tasks if @running_tasks.call.any?
@@ -206,7 +215,7 @@ module Samagotchi
206
215
 
207
216
  # @return [Float] seconds since the latest activity of any kind
208
217
  def idle_seconds
209
- last = [@started_at, @engine.last_activity_at, @bridge&.last_client_activity_at].compact.max
218
+ last = [@started_at, @turn_state.last_activity_at, @bridge&.last_client_activity_at].compact.max
210
219
  @clock.call - last
211
220
  end
212
221