samagotchi 0.201.0 → 0.202.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 (175) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -1
  3. data/docs/architecture.md +47 -7
  4. data/docs/cli.md +20 -1
  5. data/docs/configuration.md +2 -1
  6. data/docs/internals/bridge.md +4 -4
  7. data/docs/internals/engine.md +9 -6
  8. data/docs/internals/knowledge.md +26 -6
  9. data/docs/internals/objectives.md +2 -2
  10. data/docs/internals/pages.md +4 -2
  11. data/docs/internals/participants.md +1 -1
  12. data/docs/internals/peers.md +9 -7
  13. data/docs/internals/remote-sessions.md +6 -6
  14. data/docs/internals/session-lifecycle.md +37 -15
  15. data/docs/internals/web-frontend.md +18 -7
  16. data/docs/pages.md +6 -3
  17. data/docs/plugins.md +2 -2
  18. data/docs/scheduling.md +40 -12
  19. data/docs/sessions.md +48 -6
  20. data/docs/sub-agent.md +1 -2
  21. data/docs/testing.md +92 -8
  22. data/lib/samagotchi/answer_display.rb +3 -2
  23. data/lib/samagotchi/answer_tail.rb +4 -4
  24. data/lib/samagotchi/bridge/card_places.rb +5 -5
  25. data/lib/samagotchi/bridge/turn_accumulator.rb +5 -5
  26. data/lib/samagotchi/bridge/turn_drawings/synth.rb +5 -5
  27. data/lib/samagotchi/bridge/turn_drawings.rb +2 -2
  28. data/lib/samagotchi/bridge/turn_ranges.rb +15 -8
  29. data/lib/samagotchi/bridge.rb +5 -5
  30. data/lib/samagotchi/broadcast_command.rb +3 -3
  31. data/lib/samagotchi/bundle_command/try/trace.rb +18 -6
  32. data/lib/samagotchi/bundles/knowledge/embedder.rb +2 -0
  33. data/lib/samagotchi/bundles/knowledge/github.rb +19 -0
  34. data/lib/samagotchi/bundles/knowledge/manifest.yml +6 -6
  35. data/lib/samagotchi/bundles/knowledge/plugin.rb +26 -54
  36. data/lib/samagotchi/bundles/knowledge/refresh.rb +89 -14
  37. data/lib/samagotchi/bundles/knowledge/store.rb +36 -2
  38. data/lib/samagotchi/bundles/schedules/manifest.yml +2 -2
  39. data/lib/samagotchi/bundles/schedules/scripts/schedules.rb +24 -3
  40. data/lib/samagotchi/bundles/system/config_modification_protocol.md +1 -1
  41. data/lib/samagotchi/bundles/system/manifest.yml +3 -3
  42. data/lib/samagotchi/bundles/system/skill_pages.md +1 -1
  43. data/lib/samagotchi/chi_invocation.rb +1 -1
  44. data/lib/samagotchi/child_move.rb +6 -3
  45. data/lib/samagotchi/child_reports.rb +3 -3
  46. data/lib/samagotchi/children_status.rb +4 -2
  47. data/lib/samagotchi/client_id.rb +2 -2
  48. data/lib/samagotchi/compat.rb +4 -6
  49. data/lib/samagotchi/config.rb +3 -0
  50. data/lib/samagotchi/context_note.rb +1 -1
  51. data/lib/samagotchi/continue_intent.rb +1 -1
  52. data/lib/samagotchi/continue_offer.rb +2 -2
  53. data/lib/samagotchi/continue_repair_backoff.rb +71 -0
  54. data/lib/samagotchi/delivery.rb +1 -4
  55. data/lib/samagotchi/engine/participants.rb +1 -1
  56. data/lib/samagotchi/engine/recap.rb +1 -1
  57. data/lib/samagotchi/engine/turn_end.rb +1 -1
  58. data/lib/samagotchi/grants.rb +2 -2
  59. data/lib/samagotchi/guardrail_wiring.rb +1 -1
  60. data/lib/samagotchi/guardrails/approval.rb +3 -3
  61. data/lib/samagotchi/guardrails/pair_boundary.rb +3 -3
  62. data/lib/samagotchi/guardrails/participant_refusal.rb +2 -2
  63. data/lib/samagotchi/idle_recap.rb +6 -6
  64. data/lib/samagotchi/kernel_loop.rb +1 -1
  65. data/lib/samagotchi/last_turn.rb +9 -6
  66. data/lib/samagotchi/llm/chat_loop.rb +5 -2
  67. data/lib/samagotchi/mcp_command.rb +2 -2
  68. data/lib/samagotchi/memory_usage.rb +7 -6
  69. data/lib/samagotchi/note_command.rb +1 -1
  70. data/lib/samagotchi/note_delivery.rb +5 -5
  71. data/lib/samagotchi/objectives_backing.rb +3 -3
  72. data/lib/samagotchi/objectives_record.rb +2 -2
  73. data/lib/samagotchi/objectives_track.rb +2 -2
  74. data/lib/samagotchi/owner_lock.rb +28 -2
  75. data/lib/samagotchi/owner_prompt.rb +3 -3
  76. data/lib/samagotchi/pages/runtime.rb +9 -4
  77. data/lib/samagotchi/participant_revise.rb +6 -6
  78. data/lib/samagotchi/peer_receipts.rb +11 -4
  79. data/lib/samagotchi/plugin/side_question.rb +1 -1
  80. data/lib/samagotchi/recap_place.rb +5 -5
  81. data/lib/samagotchi/reply_for.rb +3 -3
  82. data/lib/samagotchi/reply_wait/liveness.rb +2 -2
  83. data/lib/samagotchi/reply_wait/poll.rb +3 -3
  84. data/lib/samagotchi/schedule_command.rb +1 -1
  85. data/lib/samagotchi/schedule_display.rb +4 -3
  86. data/lib/samagotchi/schedules/busy_part.rb +29 -5
  87. data/lib/samagotchi/schedules/history_part.rb +47 -7
  88. data/lib/samagotchi/schedules/marks_part.rb +52 -29
  89. data/lib/samagotchi/schedules/notices_part.rb +31 -4
  90. data/lib/samagotchi/schedules/protection_part.rb +69 -11
  91. data/lib/samagotchi/schedules/prune_part.rb +3 -2
  92. data/lib/samagotchi/schedules.rb +37 -47
  93. data/lib/samagotchi/send_command.rb +12 -12
  94. data/lib/samagotchi/session.rb +71 -59
  95. data/lib/samagotchi/session_access/contract.rb +7 -0
  96. data/lib/samagotchi/session_access/exchange.rb +1 -1
  97. data/lib/samagotchi/session_access/local.rb +9 -9
  98. data/lib/samagotchi/session_access/remote.rb +20 -24
  99. data/lib/samagotchi/session_access.rb +1 -1
  100. data/lib/samagotchi/session_chain/delete.rb +15 -8
  101. data/lib/samagotchi/session_chain/delete_preview.rb +3 -3
  102. data/lib/samagotchi/session_chain/links.rb +18 -4
  103. data/lib/samagotchi/session_chain/successor.rb +1 -1
  104. data/lib/samagotchi/session_chain.rb +3 -3
  105. data/lib/samagotchi/session_continue.rb +83 -32
  106. data/lib/samagotchi/session_inbox.rb +6 -17
  107. data/lib/samagotchi/session_inbox_delivery.rb +36 -13
  108. data/lib/samagotchi/session_lifecycle/allowed.rb +5 -5
  109. data/lib/samagotchi/session_manager/delegate_rule.rb +2 -2
  110. data/lib/samagotchi/session_manager/delivery.rb +13 -38
  111. data/lib/samagotchi/session_manager/emptiness.rb +35 -15
  112. data/lib/samagotchi/session_manager/lifecycle.rb +3 -2
  113. data/lib/samagotchi/session_manager/listing.rb +35 -10
  114. data/lib/samagotchi/session_manager/removal.rb +16 -0
  115. data/lib/samagotchi/session_manager/spawn.rb +9 -6
  116. data/lib/samagotchi/session_manager.rb +6 -4
  117. data/lib/samagotchi/session_messages.rb +602 -0
  118. data/lib/samagotchi/session_metrics.rb +7 -4
  119. data/lib/samagotchi/session_participants.rb +4 -4
  120. data/lib/samagotchi/session_protection.rb +16 -2
  121. data/lib/samagotchi/session_retention.rb +26 -12
  122. data/lib/samagotchi/session_row.rb +1 -1
  123. data/lib/samagotchi/session_stats.rb +17 -8
  124. data/lib/samagotchi/session_stores.rb +5 -0
  125. data/lib/samagotchi/session_transition.rb +3 -2
  126. data/lib/samagotchi/sessions_command.rb +55 -3
  127. data/lib/samagotchi/steer.rb +16 -50
  128. data/lib/samagotchi/storage_migration.rb +234 -0
  129. data/lib/samagotchi/terminal_ui/attached_loop.rb +3 -3
  130. data/lib/samagotchi/terminal_ui/formatting.rb +5 -5
  131. data/lib/samagotchi/terminal_ui/join_replay.rb +3 -3
  132. data/lib/samagotchi/terminal_ui/question_prompt.rb +2 -2
  133. data/lib/samagotchi/toolkit_report_command.rb +1 -1
  134. data/lib/samagotchi/tools/ask_session.rb +2 -2
  135. data/lib/samagotchi/tools/send_note.rb +1 -1
  136. data/lib/samagotchi/turn_context.rb +3 -3
  137. data/lib/samagotchi/turn_endings.rb +17 -0
  138. data/lib/samagotchi/turn_excerpts.rb +7 -4
  139. data/lib/samagotchi/turn_flow.rb +1 -1
  140. data/lib/samagotchi/turn_note.rb +30 -2
  141. data/lib/samagotchi/turn_origin.rb +7 -7
  142. data/lib/samagotchi/turn_outcome.rb +11 -0
  143. data/lib/samagotchi/turn_stages/verify_anchors.rb +4 -4
  144. data/lib/samagotchi/verify_fresh.rb +6 -6
  145. data/lib/samagotchi/verify_fresh_origin.rb +4 -4
  146. data/lib/samagotchi/version.rb +1 -1
  147. data/lib/samagotchi/web/app.rb +4 -3
  148. data/lib/samagotchi/web/last_ask.rb +2 -2
  149. data/lib/samagotchi/web/message_display.rb +12 -12
  150. data/lib/samagotchi/web/page_routes.rb +9 -3
  151. data/lib/samagotchi/web/participant_view.rb +4 -4
  152. data/lib/samagotchi/web/participants_admin.rb +1 -1
  153. data/lib/samagotchi/web/public/bubbles.js +1 -1
  154. data/lib/samagotchi/web/public/composer_fold.js +0 -3
  155. data/lib/samagotchi/web/public/composer_grip.js +0 -2
  156. data/lib/samagotchi/web/public/page_view.js +29 -4
  157. data/lib/samagotchi/web/public/participant_model.js +1 -1
  158. data/lib/samagotchi/web/public/peer_wait.js +2 -4
  159. data/lib/samagotchi/web/public/schedule_marks.js +2 -5
  160. data/lib/samagotchi/web/public/schedule_name.js +14 -0
  161. data/lib/samagotchi/web/public/session_actions.js +5 -0
  162. data/lib/samagotchi/web/public/start_page.js +1 -1
  163. data/lib/samagotchi/web/public/stop_sheet.js +2 -4
  164. data/lib/samagotchi/web/public/working_set_rail.js +0 -3
  165. data/lib/samagotchi/web/push/attention.rb +1 -1
  166. data/lib/samagotchi/web/session_create.rb +1 -1
  167. data/lib/samagotchi/web/session_hub.rb +68 -20
  168. data/lib/samagotchi/web/session_summary.rb +18 -14
  169. data/lib/samagotchi/web/verify_fresh_routes.rb +4 -4
  170. data/lib/samagotchi/worker/turn_drain.rb +158 -0
  171. data/lib/samagotchi/worker.rb +8 -144
  172. data/lib/samagotchi/worker_boot.rb +21 -5
  173. data/lib/samagotchi/worker_participant_gate.rb +5 -5
  174. data/lib/samagotchi/worker_presence.rb +24 -1
  175. metadata +6 -1
@@ -10,6 +10,7 @@ require_relative "../session_continue"
10
10
  require_relative "../schedules"
11
11
  require_relative "../bridge_client"
12
12
  require_relative "../worker_link"
13
+ require_relative "../worker_presence"
13
14
  require_relative "../config"
14
15
  require_relative "../log"
15
16
  require_relative "../installed_versions"
@@ -68,7 +69,27 @@ module Samagotchi
68
69
  # whether anything changed: the file's [mtime, size], the session
69
70
  # dir's mtime, and the live stamp {pid, kind, bridge_started_at} so
70
71
  # a change of worker is a change even when `owner` reads the same.
71
- Entry = Struct.new(:session, :summary, :stamp, :dir_stamp, :owner, :live, keyword_init: true)
72
+ # summary_of is the Session the summary was last built from: another
73
+ # than `session` when that build raised (.chain_row).
74
+ Entry = Struct.new(:session, :summary, :stamp, :dir_stamp, :owner, :live, :summary_of, keyword_init: true)
75
+
76
+ # One entry's row for the schedule marks' chain walk
77
+ # (Schedules.session_marks(sessions:)): the id inside its file, as a
78
+ # session listing keys it, and the archive flag its summary read; a
79
+ # summary older than the session (its last build raised) has the
80
+ # marker read again. nil for an entry with no summary (never shown,
81
+ # gone for the marks too).
82
+ # @return [SessionChain::Links::Row, nil]
83
+ def self.chain_row(entry, state_dir:)
84
+ session = entry.summary && entry.session
85
+ return nil unless session
86
+
87
+ archived = if entry.summary_of.equal?(session) then entry.summary[:archived]
88
+ else ArchiveStore.archived?(Session.session_dir(session.id, state_dir: state_dir))
89
+ end
90
+ SessionChain::Links::Row.new(id: session.id, continues: session.continues, archived: archived,
91
+ created_at: session.created_at)
92
+ end
72
93
 
73
94
  # Returned by #subscribe.
74
95
  class Subscription
@@ -131,12 +152,19 @@ module Samagotchi
131
152
  # #recompute_schedule_marks): session id → its marks. Read again when
132
153
  # schedules.json's stamp changed or the disabled-notice claim came due
133
154
  # (#claim_disabled_if_due: stale), and when the sessions folder did
134
- # (#scan_files) at most once every MARKS_FOLDER_INTERVAL.
155
+ # (#scan_files) or a session's archived or continues flag (#refresh)
156
+ # at most once every MARKS_FOLDER_INTERVAL.
135
157
  @schedule_marks = {}
136
158
  @schedules_stamp = UNSEEN
137
159
  @schedule_marks_stale = true
138
160
  @schedule_marks_folder_changed = false
139
161
  @schedule_marks_read_at = nil
162
+ # The marks reads in the order they were decided (#schedule_marks_due?
163
+ # hands out the next), and the newest swapped in: two scans at once
164
+ # (the tick thread's, AutoUpdate#finish's) never swap an older read
165
+ # in after a newer one.
166
+ @schedule_marks_reads = 0
167
+ @schedule_marks_swapped = 0
140
168
  @sweep_thread = nil
141
169
  @wakeup = Queue.new # stop wakes the tick thread out of its sleep
142
170
  @thread = nil
@@ -175,7 +203,7 @@ module Samagotchi
175
203
  # gives the retention sweep its chance (it used to run on
176
204
  # GET /api/sessions, which the page no longer calls).
177
205
  def scan(tick: true)
178
- full, marks_due = @monitor.synchronize do
206
+ full, marks_read, rows = @monitor.synchronize do
179
207
  full = full_probe_due?
180
208
  SessionContinue.repair_interrupted(state_dir: @state_dir)
181
209
  scan_files
@@ -184,10 +212,12 @@ module Samagotchi
184
212
  if full
185
213
  check_installed
186
214
  end
187
- [full, schedule_marks_due?]
215
+ marks_read = schedule_marks_due? ? (@schedule_marks_reads += 1) : nil
216
+ # The chain rows as of this scan's refreshes, taken with the decision.
217
+ [full, marks_read, marks_read && chain_rows]
188
218
  end
189
- # Outside the projection's lock: a session listing (tens of ms).
190
- recompute_schedule_marks if marks_due
219
+ # Outside the projection's lock: schedules.json and the chain walks.
220
+ recompute_schedule_marks(marks_read, rows) if marks_read
191
221
  sweep_if_due if full
192
222
  # Outside the projection's lock: a firing can wait for a recap.
193
223
  schedule_tick if tick
@@ -325,7 +355,7 @@ module Samagotchi
325
355
  entries = @sessions.values.reject { |e| e.summary.nil? }
326
356
  entries = entries.select { |e| e.summary[:project_root] == project_root } if project_root
327
357
  key = sort.to_s == "created_at" ? :created_at : :updated_at
328
- sorted = entries.sort_by { |e| sort_time(e.summary[key]) }
358
+ sorted = entries.sort_by { |e| Session.sort_time(e.summary[key]) }
329
359
  sorted.reverse! unless order.to_s == "asc"
330
360
  sorted.map(&:summary)
331
361
  end
@@ -523,7 +553,7 @@ module Samagotchi
523
553
  # Whether the schedule marks are to be read again (under the lock; the
524
554
  # read itself is #recompute_schedule_marks', outside it): schedules.json's
525
555
  # stamp changed or the cache is stale, read now; the sessions folder
526
- # changed, read once MARKS_FOLDER_INTERVAL has passed since the last
556
+ # (or a session's chain flags) changed, read once MARKS_FOLDER_INTERVAL has passed since the last
527
557
  # read (the change stays noted until then). A scan where nothing
528
558
  # changed costs one stat.
529
559
  def schedule_marks_due?
@@ -541,12 +571,18 @@ module Samagotchi
541
571
  end
542
572
 
543
573
  # The schedules waiting on each session (Schedules.session_marks: one
544
- # read of schedules.json and one session listing), read outside the
574
+ # read of schedules.json, the chains walked over +rows+, the
575
+ # projection's, #chain_rows: no session listing), read outside the
545
576
  # lock; the lock is taken only to swap the cache in and re-emit the
546
- # sessions whose marks changed.
547
- def recompute_schedule_marks
548
- marks = Samagotchi::Schedules.session_marks(state_dir: @state_dir)
577
+ # sessions whose marks changed. +read+ is the read's number (#scan):
578
+ # one older than the newest swapped in is dropped (what changed since
579
+ # its decision is noted for a later read).
580
+ def recompute_schedule_marks(read, rows)
581
+ marks = Samagotchi::Schedules.session_marks(state_dir: @state_dir, sessions: rows)
549
582
  @monitor.synchronize do
583
+ next if read <= @schedule_marks_swapped
584
+
585
+ @schedule_marks_swapped = read
550
586
  next if marks == @schedule_marks
551
587
 
552
588
  @schedule_marks = marks
@@ -554,6 +590,12 @@ module Samagotchi
554
590
  end
555
591
  end
556
592
 
593
+ # Every shown session's chain row (.chain_row), under the lock: frozen
594
+ # values, nothing shared with a later refresh.
595
+ def chain_rows
596
+ @sessions.each_value.filter_map { |entry| self.class.chain_row(entry, state_dir: @state_dir) }
597
+ end
598
+
557
599
  def schedules_stamp
558
600
  stat = File.stat(File.join(File.dirname(File.expand_path(@state_dir)), Samagotchi::Schedules::FILE))
559
601
  [stat.mtime, stat.size]
@@ -564,13 +606,16 @@ module Samagotchi
564
606
  # A change of the cached marks alone changes no session's stamps, so
565
607
  # #refresh didn't rebuild those summaries: each whose shown marks
566
608
  # differ gets them merged in and its `session` event again (as
567
- # #refresh_worker_compat), built the way SessionSummary.build builds them.
609
+ # #refresh_worker_compat), built the way SessionSummary.build builds them
610
+ # (its stop read again for a session with marks: one presence read).
568
611
  def refresh_schedule_marks
569
612
  @sessions.each do |id, entry|
570
613
  summary = entry.summary
571
614
  next if summary.nil?
572
615
 
573
- marks = SessionSummary.schedule_marks_shown(@schedule_marks[id], summary[:status])
616
+ cached = @schedule_marks[id]
617
+ stopped = !Array(cached).empty? && Samagotchi::Schedules::BusyPart.stop_holds?(WorkerPresence.read(id, state_dir: @state_dir))
618
+ marks = SessionSummary.schedule_marks_shown(cached, stopped)
574
619
  next if summary[:schedule_marks] == marks
575
620
 
576
621
  entry.summary = summary.merge(schedule_marks: marks)
@@ -664,11 +709,16 @@ module Samagotchi
664
709
  entry.live = live
665
710
  summary = SessionSummary.build(entry.session, owner: owner, session_dir: dir, registry: hub_registry,
666
711
  compat_version: @installed || VERSION, schedule_marks: @schedule_marks[id])
712
+ entry.summary_of = entry.session
667
713
  # A new worker (pid) or a new Bridge for the same session is an
668
714
  # event for the page even when the summary reads the same: it
669
715
  # reconnects its stream on it.
670
716
  return if summary == entry.summary && !live_changed
671
717
 
718
+ # Archived, unarchived or continuing another link: only the session's
719
+ # own folder or file changed (#scan_files may see nothing), and a
720
+ # schedule's target may be another link now. #touch too.
721
+ @schedule_marks_folder_changed = true if chain_moved?(entry.summary, summary)
672
722
  entry.summary = summary
673
723
  emit("session", session: summary)
674
724
  end
@@ -681,6 +731,10 @@ module Samagotchi
681
731
  # refresh of a card.
682
732
  def hub_registry = HostRegistry.current
683
733
 
734
+ def chain_moved?(before, after)
735
+ before && (before[:archived] != after[:archived] || before[:continues] != after[:continues])
736
+ end
737
+
684
738
  def drop(id)
685
739
  entry = @sessions.delete(id)
686
740
  emit("session_gone", id: id) if entry&.summary
@@ -721,12 +775,6 @@ module Samagotchi
721
775
  Log.warn(:web, "hub_subscriber_failed", error: e.class.name, msg: e.message)
722
776
  end
723
777
  end
724
-
725
- def sort_time(value)
726
- Time.iso8601(value.to_s)
727
- rescue ArgumentError
728
- Time.at(0)
729
- end
730
778
  end
731
779
  end
732
780
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../session"
4
- require_relative "../steer"
4
+ require_relative "../speaker"
5
5
  require_relative "../participants"
6
6
  require_relative "../verify_fresh"
7
7
  require_relative "../recap_store"
@@ -33,9 +33,11 @@ module Samagotchi
33
33
  archived project_root bridge_up worker_version worker_features worker_compat pending_question pending_card
34
34
  last_turn verdicts verify_fresh last_verdict schedule_marks].freeze
35
35
 
36
- # The hub's answer for a session: its lifecycle state and whether its
37
- # Bridge is up for it (the state, not the link alone).
38
- Shown = Data.define(:state, :up)
36
+ # The hub's answer for a session: its lifecycle state, whether its
37
+ # Bridge is up for it (the state, not the link alone), and whether
38
+ # every schedule firing on it skips as stopped (Schedules::BusyPart.
39
+ # stop_holds?, the page's schedule marks).
40
+ Shown = Data.define(:state, :up, :schedules_stopped)
39
41
 
40
42
  module_function
41
43
 
@@ -51,7 +53,7 @@ module Samagotchi
51
53
  link ||= WorkerLink.resolve(session_dir, unlink: false, owner: owner)
52
54
  presence = WorkerPresence.read(session.id, state_dir: File.dirname(session_dir), owner: owner, link: link)
53
55
  state = SessionLifecycle::Reading.derive(presence).state
54
- Shown.new(state: state, up: state == :live)
56
+ Shown.new(state: state, up: state == :live, schedules_stopped: Schedules::BusyPart.stop_holds?(presence))
55
57
  end
56
58
 
57
59
  # @param session [Session] a saved session (messages may be empty)
@@ -159,17 +161,19 @@ module Samagotchi
159
161
  last_verdict: last_verdict(session, session_dir),
160
162
  # The schedules waiting on it the page says (the notice strip under
161
163
  # the session bar, the card's ⏸ schedule badge): every one while it
162
- # is stopped, else those whose newest outcome is a skip
163
- # (Schedules.shown_marks).
164
- schedule_marks: schedule_marks_shown(schedule_marks, status)
164
+ # is stopped as the schedules read it (Schedules::BusyPart.stop_holds?:
165
+ # the marker, under a one-turn run too), else those whose newest
166
+ # outcome is a skip (Schedules.shown_marks).
167
+ schedule_marks: schedule_marks_shown(schedule_marks, shown.schedules_stopped)
165
168
  }
166
169
  WEB_KEYS.to_h { |key| [key, values.fetch(key)] }
167
170
  end
168
171
 
169
- # The page's marks of a session from the hub's cached ones and its
170
- # status: what #build puts in and the hub's re-emit merges in alike.
171
- def schedule_marks_shown(marks, status)
172
- Schedules.shown_marks(marks, stopped: status == Session::STATUS_STOPPED)
172
+ # The page's marks of a session from the hub's cached ones and whether
173
+ # its schedules skip as stopped (Shown#schedules_stopped): what #build
174
+ # puts in and the hub's re-emit merges in alike.
175
+ def schedule_marks_shown(marks, stopped)
176
+ Schedules.shown_marks(marks, stopped: stopped)
173
177
  end
174
178
 
175
179
  def last_verdict(session, session_dir)
@@ -178,10 +182,10 @@ module Samagotchi
178
182
 
179
183
  verdict = raw.to_h { |key, value| [key.to_sym, value] }
180
184
  author = verdict[:by]
181
- return verdict unless Steer.participant_author?(author)
185
+ return verdict unless Speaker.participant_author?(author)
182
186
 
183
187
  participant = Participants.read(session.id, state_dir: File.dirname(session_dir))
184
- .find { |entry| entry.id == Steer.participant_id(author) }
188
+ .find { |entry| entry.id == Speaker.participant_id(author) }
185
189
  participant ? verdict.merge(by_name: participant.name) : verdict
186
190
  end
187
191
  private_class_method :last_verdict
@@ -5,7 +5,7 @@ require_relative "../worker_link"
5
5
  require_relative "../participant_messages"
6
6
  require_relative "../participants"
7
7
  require_relative "../session"
8
- require_relative "../steer"
8
+ require_relative "../speaker"
9
9
  require_relative "../verify_fresh"
10
10
 
11
11
  module Samagotchi
@@ -37,7 +37,7 @@ module Samagotchi
37
37
  return verify_fresh_refused(clicker, of, *refused)
38
38
  end
39
39
 
40
- by = Steer.sender_author(field_of(VerifyFresh.original(session.verdicts, of), :by))
40
+ by = Speaker.sender_author(field_of(VerifyFresh.original(session.verdicts, of), :by))
41
41
  runner = by ? verdict_author_grant(id, by) : clicker
42
42
  return author_link_ended(clicker, id, of, by) if by && runner.nil?
43
43
  if runner && runner != clicker && (refused = participant_cap_refusal(runner) ||
@@ -60,14 +60,14 @@ module Samagotchi
60
60
  # The live grant of the participant +author+ ("p:<id>") in session
61
61
  # +id+, nil when it is gone, revoked or expired.
62
62
  def verdict_author_grant(id, author)
63
- grant = @grants.find(Steer.participant_id(author))
63
+ grant = @grants.find(Speaker.participant_id(author))
64
64
  grant if grant && grant.session_id == id && grant.active?(@grants.now)
65
65
  end
66
66
 
67
67
  # 409 author_link_ended: the verdict's participant can't be charged or
68
68
  # bounded any more, so no child runs for it.
69
69
  def author_link_ended(clicker, id, of, author)
70
- pid = Steer.participant_id(author)
70
+ pid = Speaker.participant_id(author)
71
71
  name = Participants.read(id, state_dir: default_state_dir).find { |entry| entry.id == pid }&.name
72
72
  name = pid if name.to_s.strip.empty?
73
73
  detail = format(AUTHOR_LINK_ENDED, name: name)
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ require_relative "../log"
6
+ require_relative "../session_inbox"
7
+ require_relative "../steer"
8
+
9
+ module Samagotchi
10
+ class Worker
11
+ # The mid-turn steering drain: #pending_input_drain — the drain
12
+ # #run_engine_turn hands to Engine#run_turn (pending_input:), claiming
13
+ # the input files that arrive while a turn runs and merging them at
14
+ # each iteration boundary — with #input_waiting? (the input_waiting:
15
+ # probe), #turn_author_fields, #merged_line, #take_child_reports and
16
+ # #resolve_relays. Writes @pending_input_drain (memoized) and
17
+ # @merged_this_turn (concat; #run_engine_turn resets it per turn).
18
+ # Reads @session, @session_dir, @turn_author, @turn_enqueued_id,
19
+ # @turn_state, @participant_gate, @event_log, @engine, @checkpoint,
20
+ # @turn_id, @wakes and @child_reports.
21
+ module TurnDrainPart
22
+ private
23
+
24
+ # Shared mid-turn steering drain: claims any input files that arrive
25
+ # while a turn is running and hands them to the agentic loop so
26
+ # follow-ups merge at the next iteration boundary instead of waiting
27
+ # for the turn to end. claim_input_file is atomic (rename), so a file
28
+ # consumed mid-turn simply fails the outer loop's later claim with
29
+ # ENOENT → nil. No double-processing risk.
30
+ #
31
+ # Runs on the turn thread; it announces who sent the merged input so
32
+ # every live UI can attribute it.
33
+ def pending_input_drain
34
+ @pending_input_drain ||= lambda do
35
+ # An iteration boundary: tell whoever sent a command now that it waits.
36
+ refuse_queued_commands(mid_turn: true)
37
+ # A queued line or one with images runs as its own turn after this
38
+ # one (steering merges text only): skipped here, not a barrier, so a
39
+ # line sent after it still goes in at this boundary. Only the files
40
+ # claimed below reach @merged_this_turn and the checkpoint.
41
+ # A participant's turn takes only their own lines: the owner's and
42
+ # anyone else's (the Bridge queues them, this covers a file it didn't
43
+ # see) run as their own turns after it, never inside its boundary;
44
+ # and the owner's turn takes no participant's (SessionInbox.turn_end_data?).
45
+ files = SessionInbox.find_new_input_files(@session_dir).sort
46
+ .reject { |input_file| SessionInbox.waits_for_turn_end?(input_file, running_author: @turn_author) }
47
+ merged = files.filter_map do |input_file|
48
+ claimed_file = SessionInbox.claim_input_file(input_file)
49
+ next unless claimed_file
50
+
51
+ begin
52
+ input = SessionInbox.read_input(claimed_file)
53
+ enqueued_id = input.origin.enqueued_id
54
+ if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@turn_state.claim_enqueued_id(enqueued_id)
55
+ Log.info(:worker, "duplicate_input_dropped", enqueued_id: enqueued_id)
56
+ next
57
+ end
58
+ begin
59
+ # A participant's line gets the checks their queued turn gets at
60
+ # its start (WorkerParticipantGate#refusal: the link, the cap): refused,
61
+ # it is taken off as that turn would be, and never merged.
62
+ if (author = input.origin.participant_author) && (reason = @participant_gate.refusal(author))
63
+ @participant_gate.refuse(input.origin, reason)
64
+ next
65
+ end
66
+ prompt = input.prompt.to_s.strip
67
+ # Which turn took it, on disk before its file goes (a wait for
68
+ # it reads this, not an inference: ReplyWait::Poll#follow_merge).
69
+ SessionInbox.mark_merged(@session_dir, enqueued_id, into: @turn_enqueued_id) unless prompt.empty?
70
+ prompt.empty? ? nil : [prompt, input.origin]
71
+ ensure
72
+ @turn_state.finish_enqueued_id(enqueued_id)
73
+ end
74
+ ensure
75
+ FileUtils.rm_f(claimed_file)
76
+ end
77
+ end
78
+ merged.each { |_prompt, origin| user_input(origin.client_id) }
79
+ # Delegate children's reports: never handed back on a failure
80
+ # (their rings stay for the next turn), so not in @merged_this_turn.
81
+ reports, mark = take_child_reports
82
+ unless merged.empty? && reports.empty?
83
+ @event_log.announce(type: :input_merged, count: merged.size + reports.size,
84
+ origins: (merged.map(&:last) + reports.map(&:origin)).filter_map(&:wire))
85
+ @merged_this_turn.concat(merged)
86
+ end
87
+ # Each line keeps its sender (Steer.source_for_client): a chi send or
88
+ # a delegating parent's line is saved and shown to the model as theirs,
89
+ # a participant's with its author (Engine#author_fields).
90
+ lines = merged.map { |prompt, origin| merged_line(prompt, origin) } + reports.map(&:line)
91
+ # A wake turn starts with its first line, whoever sent it.
92
+ lines[0] = lines[0].with(mark: mark) if mark && lines.any?
93
+ # The turn so far, with what this boundary merges (its input files
94
+ # are gone now), on disk at most every TurnCheckpoint::INTERVAL;
95
+ # its metrics so far with it, so a turn a KILL ends keeps them.
96
+ @engine.checkpoint_metrics if @checkpoint.boundary(@engine.live_conversation, merging: lines, turn_id: @turn_id)
97
+ lines
98
+ end
99
+ end
100
+
101
+ # Whether a person's message waits that the next boundary merges
102
+ # (SessionInbox.steer_waiting?): a running task_wait ends on it. In a
103
+ # participant's turn only their own.
104
+ def input_waiting?
105
+ SessionInbox.steer_waiting?(@session_dir, running_author: @turn_author)
106
+ end
107
+
108
+ # A participant's author fields (Engine#author_fields), {} for the owner.
109
+ def turn_author_fields(origin)
110
+ origin.participant? ? @engine.author_fields(origin.author, session: @session) : {}
111
+ end
112
+
113
+ def merged_line(prompt, origin)
114
+ author = turn_author_fields(origin)
115
+ Steer::Line.new(text: prompt, source: Steer.source_for_client(origin.client_id),
116
+ author: author[:author], author_name: author[:author_name])
117
+ end
118
+
119
+ # The reports for rings that came in since the turn last looked. A
120
+ # failure to read them must not take the turn's steering with it.
121
+ # A wake turn's first call gets the reports it was started for (their
122
+ # message is the turn's start: turn_start, turn_id, so a reload shows
123
+ # the turn as its own).
124
+ # @return [Array(Array<ChildReports::Report>, Hash|nil)] the reports
125
+ # and the mark of their message
126
+ # A report whose question goes to the parent's user is relayed here
127
+ # (ChildReports#resolve), before the boundary announces or merges
128
+ # anything; one whose card a Stop closed is held back until a human's
129
+ # input (no wake turn re-offers it at once; other wakes go on).
130
+ # +resolve+ false: a wake turn's take before the turn starts.
131
+ def take_child_reports(resolve: true)
132
+ if (reports = @wakes.take_handed)
133
+ return [resolve_relays(reports), { turn_start: true, turn_id: @turn_id }.compact]
134
+ end
135
+
136
+ taken = @child_reports.take
137
+ [resolve ? resolve_relays(taken) : taken, nil]
138
+ rescue StandardError => e
139
+ Log.warn(:worker, "delegate_reports_failed", error: e.class.name, msg: e.message)
140
+ [[], nil]
141
+ end
142
+
143
+ def resolve_relays(reports)
144
+ return reports if reports.none?(&:relay)
145
+
146
+ relay = @engine.relay_peer
147
+ stopped = false
148
+ reports.filter_map do |report|
149
+ next report unless report.relay
150
+ # The turn stopped on an earlier card: no more cards in it.
151
+ next @child_reports.hold_back(report) if stopped
152
+
153
+ @child_reports.resolve(report, relay: relay).tap { |resolved| stopped = resolved.nil? }
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -9,20 +9,15 @@ require_relative "engine"
9
9
  require_relative "events"
10
10
  require_relative "session"
11
11
  require_relative "session_inbox"
12
- require_relative "turn_note"
13
- require_relative "steer"
14
12
  require_relative "turn_origin"
15
13
  require_relative "worker_idle_exit"
16
14
  require_relative "degrade"
17
15
  require_relative "hold_stop"
18
- require_relative "children_status"
19
16
  require_relative "worker_wakes"
20
17
  require_relative "worker_inbound"
21
18
  require_relative "worker_signals"
22
19
  require_relative "worker_task_wakes"
23
20
  require_relative "session_manager"
24
- require_relative "archive_store"
25
- require_relative "reminder_carry"
26
21
  require_relative "log"
27
22
  require_relative "turn_flow"
28
23
  require_relative "turn_checkpoint"
@@ -30,7 +25,6 @@ require_relative "reply_for"
30
25
  require_relative "continue_offer"
31
26
  require_relative "iteration_limit"
32
27
  require_relative "session_commands"
33
- require_relative "queued_command"
34
28
  require_relative "model_profile"
35
29
  require_relative "child_reports"
36
30
  require_relative "child_move"
@@ -52,6 +46,7 @@ require_relative "worker/command_queue"
52
46
  require_relative "worker/block_fixes"
53
47
  require_relative "worker/senders"
54
48
  require_relative "worker/mail"
49
+ require_relative "worker/turn_drain"
55
50
 
56
51
  module Samagotchi
57
52
  # The loop of a background session worker, once it owns the session (see
@@ -85,15 +80,16 @@ module Samagotchi
85
80
  # signal is cut: the session is saved as it is and the signal raised in
86
81
  # the loop's thread, as Ruby's default would (the teardown still runs).
87
82
  #
88
- # This file keeps the loop, the wiring, the prompt turns and the steering
89
- # drain. Eight concerns are parts, lib/samagotchi/worker/<concern>.rb
90
- # holding Worker::<Concern>Part, included here (they share Worker's ivars):
83
+ # This file keeps the loop, the wiring and the prompt turns. Eleven
84
+ # concerns are parts, lib/samagotchi/worker/<concern>.rb holding
85
+ # Worker::<Concern>Part, included here (they share Worker's ivars):
91
86
  # LeavePart (exit and leave), WakeTurnsPart (the turns nobody typed),
92
87
  # StartupPart (the start-up settle), SignalGracePart (a signal's grace),
93
88
  # ScratchPart (a scratch worker whose terminal left), ChildSidePart (a
94
89
  # delegate child's rings, Verify fresh verdicts), CommandQueuePart
95
- # (session commands between turns) and BlockFixesPart (a failed block's
96
- # fix, between turns).
90
+ # (session commands between turns), BlockFixesPart (a failed block's fix,
91
+ # between turns), SendersPart (who a turn's input came from), MailPart
92
+ # (peer mail) and TurnDrainPart (the mid-turn steering drain).
97
93
  class Worker
98
94
  include LeavePart
99
95
  include WakeTurnsPart
@@ -105,6 +101,7 @@ module Samagotchi
105
101
  include BlockFixesPart
106
102
  include SendersPart
107
103
  include MailPart
104
+ include TurnDrainPart
108
105
 
109
106
  FALLBACK_TICK_SECONDS = 5
110
107
  # A worker just started runs no wake turn for delegate reports this
@@ -849,139 +846,6 @@ module Samagotchi
849
846
  end
850
847
  end
851
848
 
852
- # Shared mid-turn steering drain: claims any input files that arrive
853
- # while a turn is running and hands them to the agentic loop so
854
- # follow-ups merge at the next iteration boundary instead of waiting
855
- # for the turn to end. claim_input_file is atomic (rename), so a file
856
- # consumed mid-turn simply fails the outer loop's later claim with
857
- # ENOENT → nil. No double-processing risk.
858
- #
859
- # Runs on the turn thread; it announces who sent the merged input so
860
- # every live UI can attribute it.
861
- def pending_input_drain
862
- @pending_input_drain ||= lambda do
863
- # An iteration boundary: tell whoever sent a command now that it waits.
864
- refuse_queued_commands(mid_turn: true)
865
- # A queued line or one with images runs as its own turn after this
866
- # one (steering merges text only): skipped here, not a barrier, so a
867
- # line sent after it still goes in at this boundary. Only the files
868
- # claimed below reach @merged_this_turn and the checkpoint.
869
- # A participant's turn takes only their own lines: the owner's and
870
- # anyone else's (the Bridge queues them, this covers a file it didn't
871
- # see) run as their own turns after it, never inside its boundary;
872
- # and the owner's turn takes no participant's (SessionInbox.turn_end_data?).
873
- files = SessionInbox.find_new_input_files(@session_dir).sort
874
- .reject { |input_file| SessionInbox.waits_for_turn_end?(input_file, running_author: @turn_author) }
875
- merged = files.filter_map do |input_file|
876
- claimed_file = SessionInbox.claim_input_file(input_file)
877
- next unless claimed_file
878
-
879
- begin
880
- input = SessionInbox.read_input(claimed_file)
881
- enqueued_id = input.origin.enqueued_id
882
- if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@turn_state.claim_enqueued_id(enqueued_id)
883
- Log.info(:worker, "duplicate_input_dropped", enqueued_id: enqueued_id)
884
- next
885
- end
886
- begin
887
- # A participant's line gets the checks their queued turn gets at
888
- # its start (WorkerParticipantGate#refusal: the link, the cap): refused,
889
- # it is taken off as that turn would be, and never merged.
890
- if (author = input.origin.participant_author) && (reason = @participant_gate.refusal(author))
891
- @participant_gate.refuse(input.origin, reason)
892
- next
893
- end
894
- prompt = input.prompt.to_s.strip
895
- # Which turn took it, on disk before its file goes (a wait for
896
- # it reads this, not an inference: ReplyWait::Poll#follow_merge).
897
- SessionInbox.mark_merged(@session_dir, enqueued_id, into: @turn_enqueued_id) unless prompt.empty?
898
- prompt.empty? ? nil : [prompt, input.origin]
899
- ensure
900
- @turn_state.finish_enqueued_id(enqueued_id)
901
- end
902
- ensure
903
- FileUtils.rm_f(claimed_file)
904
- end
905
- end
906
- merged.each { |_prompt, origin| user_input(origin.client_id) }
907
- # Delegate children's reports: never handed back on a failure
908
- # (their rings stay for the next turn), so not in @merged_this_turn.
909
- reports, mark = take_child_reports
910
- unless merged.empty? && reports.empty?
911
- @event_log.announce(type: :input_merged, count: merged.size + reports.size,
912
- origins: (merged.map(&:last) + reports.map(&:origin)).filter_map(&:wire))
913
- @merged_this_turn.concat(merged)
914
- end
915
- # Each line keeps its sender (Steer.source_for_client): a chi send or
916
- # a delegating parent's line is saved and shown to the model as theirs,
917
- # a participant's with its author (Engine#author_fields).
918
- lines = merged.map { |prompt, origin| merged_line(prompt, origin) } + reports.map(&:line)
919
- # A wake turn starts with its first line, whoever sent it.
920
- lines[0] = lines[0].with(mark: mark) if mark && lines.any?
921
- # The turn so far, with what this boundary merges (its input files
922
- # are gone now), on disk at most every TurnCheckpoint::INTERVAL;
923
- # its metrics so far with it, so a turn a KILL ends keeps them.
924
- @engine.checkpoint_metrics if @checkpoint.boundary(@engine.live_conversation, merging: lines, turn_id: @turn_id)
925
- lines
926
- end
927
- end
928
-
929
- # Whether a person's message waits that the next boundary merges
930
- # (SessionInbox.steer_waiting?): a running task_wait ends on it. In a
931
- # participant's turn only their own.
932
- def input_waiting?
933
- SessionInbox.steer_waiting?(@session_dir, running_author: @turn_author)
934
- end
935
-
936
- # A participant's author fields (Engine#author_fields), {} for the owner.
937
- def turn_author_fields(origin)
938
- origin.participant? ? @engine.author_fields(origin.author, session: @session) : {}
939
- end
940
-
941
- def merged_line(prompt, origin)
942
- author = turn_author_fields(origin)
943
- Steer::Line.new(text: prompt, source: Steer.source_for_client(origin.client_id),
944
- author: author[:author], author_name: author[:author_name])
945
- end
946
-
947
- # The reports for rings that came in since the turn last looked. A
948
- # failure to read them must not take the turn's steering with it.
949
- # A wake turn's first call gets the reports it was started for (their
950
- # message is the turn's start: turn_start, turn_id, so a reload shows
951
- # the turn as its own).
952
- # @return [Array(Array<ChildReports::Report>, Hash|nil)] the reports
953
- # and the mark of their message
954
- # A report whose question goes to the parent's user is relayed here
955
- # (ChildReports#resolve), before the boundary announces or merges
956
- # anything; one whose card a Stop closed is held back until a human's
957
- # input (no wake turn re-offers it at once; other wakes go on).
958
- # +resolve+ false: a wake turn's take before the turn starts.
959
- def take_child_reports(resolve: true)
960
- if (reports = @wakes.take_handed)
961
- return [resolve_relays(reports), { turn_start: true, turn_id: @turn_id }.compact]
962
- end
963
-
964
- taken = @child_reports.take
965
- [resolve ? resolve_relays(taken) : taken, nil]
966
- rescue StandardError => e
967
- Log.warn(:worker, "delegate_reports_failed", error: e.class.name, msg: e.message)
968
- [[], nil]
969
- end
970
-
971
- def resolve_relays(reports)
972
- return reports if reports.none?(&:relay)
973
-
974
- relay = @engine.relay_peer
975
- stopped = false
976
- reports.filter_map do |report|
977
- next report unless report.relay
978
- # The turn stopped on an earlier card: no more cards in it.
979
- next @child_reports.hold_back(report) if stopped
980
-
981
- @child_reports.resolve(report, relay: relay).tap { |resolved| stopped = resolved.nil? }
982
- end
983
- end
984
-
985
849
  def monotonic_now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
986
850
 
987
851
  def stopped_on_disk?