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
@@ -17,6 +17,7 @@ require_relative "note_move"
17
17
  require_relative "peer_continue"
18
18
  require_relative "archive_store"
19
19
  require_relative "continue_intent"
20
+ require_relative "continue_repair_backoff"
20
21
  require_relative "session_transition"
21
22
  require_relative "continued_from"
22
23
  require_relative "recap_store"
@@ -39,7 +40,10 @@ module Samagotchi
39
40
  # worker or open in a one-turn chi run; +ids+ names them), :folder_gone (its
40
41
  # folder isn't there) or :transition_busy (its transition lock not
41
42
  # free within LOCK_WAIT: another continue or change of it runs; its
42
- # words are SessionTransition::Busy's, naming the holder: +detail+).
43
+ # words are SessionTransition::Busy's, naming the holder: +detail+) or
44
+ # :undo_pending (a crashed continue's intent is still in its folder, its
45
+ # repair failing on a system error and backed off: ContinueRepairBackoff;
46
+ # +detail+ is the whole sentence, naming the error).
43
47
  class Refused < StandardError
44
48
  attr_reader :session_id, :reason, :ids
45
49
 
@@ -50,6 +54,12 @@ module Samagotchi
50
54
  super(continue_refused_message(detail))
51
55
  end
52
56
 
57
+ # @param error [String, nil] the error class the repair last failed on
58
+ def self.undo_pending_message(session_id, error)
59
+ cause = error ? " (#{error})" : ""
60
+ "an earlier continue of #{session_id[0, 8]} hasn't been undone yet#{cause}; try again in a minute"
61
+ end
62
+
53
63
  private
54
64
 
55
65
  def continue_refused_message(detail)
@@ -61,6 +71,7 @@ module Samagotchi
61
71
  "#{short} has delegates still open that can't move to the next link: #{detail}; " \
62
72
  "then continue (or wait for them, stop them or archive them)"
63
73
  when :transition_busy then detail || SessionTransition::Busy.new(@session_id).message
74
+ when :undo_pending then detail || self.class.undo_pending_message(@session_id, nil)
64
75
  else "#{short}'s folder #{detail} is gone"
65
76
  end
66
77
  end
@@ -145,7 +156,7 @@ module Samagotchi
145
156
  # Its waiting notes last: the carry above read a pending /objectives
146
157
  # edit from them first. They keep their names, so they still sort
147
158
  # ahead of the link's own notes. A note sent to the previous link
148
- # from here on goes to this one (SessionInbox::Head), and a
159
+ # from here on goes to this one (InboxDelivery::Head), and a
149
160
  # straggler is adopted by its worker (NoteMove.adoption). Given back
150
161
  # once the link is out of the chain, and again for a note an older chi
151
162
  # wrote after its folder went.
@@ -161,30 +172,46 @@ module Samagotchi
161
172
  # and why it isn't the previous link's (nil when it is, or was given).
162
173
  ModelChoice = Data.define(:model, :warning)
163
174
  # What #check! decided before anything changes: the delegates to move
164
- # (ChildMove::Plan) and the new link's model (ModelChoice).
165
- Checked = Data.define(:plan, :choice)
175
+ # (ChildMove::Plan), the new link's model (ModelChoice) and the one
176
+ # listing it planned from (SessionManager.fleet_rows), good only until
177
+ # the next write or wait.
178
+ Checked = Data.define(:plan, :choice, :rows)
166
179
 
167
180
  # Repair an interrupted continue before showing sessions or starting
168
181
  # another continue. An active continue holds its transition lock
169
- # (SessionTransition), so its intent is left alone; after a crash, an
170
- # unsaved successor means the previous link is unarchived.
182
+ # (SessionTransition), so its intent is left alone, unread: the lock is
183
+ # tried before anything else is read, and the fleet is listed only for a
184
+ # repair that needs the descendants (an older intent without
185
+ # archived_ids), once per call. After a crash, an unsaved successor
186
+ # means the previous link is unarchived. A system error in one intent's
187
+ # repair (a full disk, a permission) is logged and backed off
188
+ # (ContinueRepairBackoff), never raised: the next intents are still repaired.
171
189
  def self.repair_interrupted(state_dir:)
172
190
  return unless Dir.exist?(state_dir)
173
191
 
174
192
  paths = Dir.glob(File.join(state_dir, "*", CONTINUE_INTENT))
193
+ ContinueRepairBackoff.keep_only(paths, state_dir)
175
194
  return if paths.empty?
176
195
 
177
- sessions = Session.list(state_dir: state_dir, include_archived: true)
178
- by_id = sessions.to_h { |session| [session.id, session] }
196
+ fleet = nil
197
+ sessions = -> { fleet ||= Session.list(state_dir: state_dir, include_archived: true) }
179
198
  paths.each do |path|
180
- id = File.basename(File.dirname(path))
181
- session = by_id[id]
182
- if session
183
- repair_intent(session, state_dir, path, sessions)
184
- else
185
- quarantine_intent(path, id, message: "session file could not be loaded")
186
- end
199
+ next if ContinueRepairBackoff.waiting?(path)
200
+
201
+ repair_one(path, state_dir, sessions)
202
+ end
203
+ end
204
+
205
+ private_class_method def self.repair_one(path, state_dir, sessions)
206
+ id = File.basename(File.dirname(path))
207
+ if Session.exist?(id, state_dir: state_dir)
208
+ repair_intent(id, state_dir, path, sessions)
209
+ else
210
+ quarantine_intent(path, id, message: "session file could not be loaded")
187
211
  end
212
+ ContinueRepairBackoff.succeeded(path)
213
+ rescue SystemCallError => e
214
+ ContinueRepairBackoff.failed(path, id, e)
188
215
  end
189
216
 
190
217
  # Start the next link of a chain: a new session that continues
@@ -352,10 +379,17 @@ module Samagotchi
352
379
  previous = Session.load(id, state_dir: state_dir)
353
380
  checked = check!(previous, state_dir, model: model)
354
381
  plan = checked.plan
355
- already = [id, *SessionManager.descendant_ids(id, state_dir, except: plan.move)].select do |sid|
382
+ # check!'s listing: nothing was written or waited for since.
383
+ already = [id, *SessionManager.descendant_ids(id, state_dir, except: plan.move, rows: checked.rows)].select do |sid|
356
384
  ArchiveStore.archived?(Session.session_dir(sid, state_dir: state_dir))
357
385
  end
358
386
  intent_path = continue_intent_path(id, state_dir)
387
+ # Under the lock an intent on disk is a crashed continue's whose repair
388
+ # failed (#repair_one): writing ours would lose its archived_ids.
389
+ if File.exist?(intent_path)
390
+ raise Refused.new(id, :undo_pending,
391
+ detail: Refused.undo_pending_message(id, ContinueRepairBackoff.error_of(intent_path)))
392
+ end
359
393
  ContinueIntent.write(intent_path, successor_id: successor_id, previously_archived: already)
360
394
  archive = SessionManager.archive_session(id, state_dir: state_dir, wait: archive_wait, except: plan.move)
361
395
  archived = archive.archived - already
@@ -386,30 +420,42 @@ module Samagotchi
386
420
  File.join(Session.session_dir(id, state_dir: state_dir), CONTINUE_INTENT)
387
421
  end
388
422
 
389
- private_class_method def self.repair_intent(session, state_dir, path, sessions)
423
+ # +sessions+ lists the fleet (memoized by the caller), called only for
424
+ # an older intent's descendants. A non-system error (bad data) sets the
425
+ # intent aside like a corrupt one, so it isn't retried every pass; a
426
+ # SystemCallError (a full disk, a permission) is raised to #repair_one,
427
+ # which backs it off.
428
+ private_class_method def self.repair_intent(id, state_dir, path, sessions)
390
429
  # Not taken (a continue runs, on this thread too): its intent is left.
391
- SessionTransition.try_exclusive(session.id, kind: :repair, state_dir: state_dir) do |token|
430
+ SessionTransition.try_exclusive(id, kind: :repair, state_dir: state_dir) do |token|
392
431
  next unless token
393
432
  next unless File.file?(path)
394
433
 
434
+ session = Session.summary_from_file(Session.session_file(id, state_dir: state_dir))
435
+ next quarantine_intent(path, id, message: "session file could not be loaded") unless session&.id == id
436
+
395
437
  intent = ContinueIntent.read(path)
396
438
  successor_path = Session.session_file(intent.successor_id, state_dir: state_dir) if intent.successor_id
397
439
  successor = JSON.parse(File.read(successor_path)) if successor_path && File.file?(successor_path)
398
- unless successor.is_a?(Hash) && successor["continues"] == session.id
399
- archived = intent.archived_ids || (archived_descendants(session.id, sessions, state_dir) - intent.previously_archived)
400
- undo_failed_continue(session.id, state_dir, moved: [], archived: archived)
440
+ unless successor.is_a?(Hash) && successor["continues"] == id
441
+ archived = intent.archived_ids || (archived_descendants(id, sessions.call, state_dir) - intent.previously_archived)
442
+ undo_failed_continue(id, state_dir, moved: [], archived: archived)
401
443
  # The folder the crashed continue reserved for it (its lock), when
402
444
  # it was never saved.
403
- drop_unsaved(session.id, intent.successor_id, state_dir) if intent.successor_id
445
+ drop_unsaved(id, intent.successor_id, state_dir) if intent.successor_id
404
446
  end
405
447
  FileUtils.rm_f(path)
406
448
  end
407
- rescue JSON::ParserError => e
408
- quarantine_intent(path, session.id, error: e)
409
449
  rescue Errno::ENOENT
410
450
  nil
451
+ rescue SystemCallError
452
+ raise
453
+ rescue StandardError => e
454
+ quarantine_intent(path, id, error: e)
411
455
  end
412
456
 
457
+ # A rename that fails on a system error (an unwritable folder) raises
458
+ # to #repair_one: the intent stays, backed off.
413
459
  private_class_method def self.quarantine_intent(path, session_id, error: nil, message: nil)
414
460
  File.rename(path, "#{path}.bad")
415
461
  Log.warn(:worker, "continue_repair_failed", sid: session_id, path: path,
@@ -508,7 +554,7 @@ module Samagotchi
508
554
  # started, then the rings a moved delegate wrote into it just before
509
555
  # the undo go back to +id+ (CARRY's :link_stopped undos). Then its
510
556
  # session file goes first, so the link is out of the chain (a note to
511
- # +id+ stays there: SessionInbox::Head) and a writer that resolved it
557
+ # +id+ stays there: InboxDelivery::Head) and a writer that resolved it
512
558
  # before finds it gone, then the notes it moved there go back with any
513
559
  # sent to it meanwhile (:link_unlinked: NoteMove.give_back), then the
514
560
  # link's folder goes, and one last look gives back a note an older chi
@@ -661,14 +707,16 @@ module Samagotchi
661
707
  raise Refused.new(previous.id, :continued, ids: [following.id])
662
708
  end
663
709
 
664
- plan = move_plan!(previous.id, state_dir)
710
+ # One listing for the whole plan (every open node, each moved subtree).
711
+ rows = SessionManager.fleet_rows(state_dir: state_dir)
712
+ plan = move_plan!(previous.id, state_dir, rows: rows)
665
713
  # The new link's model, its host checked as SessionManager.spawn_session
666
714
  # checks it: before the archive stops the previous link's worker.
667
715
  choice = model_for(previous, model)
668
716
  dir = previous.working_directory.to_s
669
717
  raise Refused.new(previous.id, :folder_gone, detail: dir) unless File.directory?(dir)
670
718
 
671
- Checked.new(plan: plan, choice: choice)
719
+ Checked.new(plan: plan, choice: choice, rows: rows)
672
720
  end
673
721
 
674
722
  # A child still open below a session (#open_children); why: running,
@@ -678,8 +726,8 @@ module Samagotchi
678
726
  # The delegates a continue of +id+ moves (ChildMove.plan).
679
727
  # @return [ChildMove::Plan]
680
728
  # @raise [Refused] for open ones it can't move
681
- private_class_method def self.move_plan!(id, state_dir)
682
- plan = ChildMove.plan(id, open: open_children(id, state_dir), state_dir: state_dir)
729
+ private_class_method def self.move_plan!(id, state_dir, rows:)
730
+ plan = ChildMove.plan(id, open: open_children(id, state_dir, rows: rows), state_dir: state_dir, rows: rows)
683
731
  return plan if plan.refuse.empty?
684
732
 
685
733
  raise Refused.new(id, :open_children, ids: plan.refuse.map(&:id),
@@ -689,11 +737,14 @@ module Samagotchi
689
737
  # The unarchived children, at any depth (the archive's cascade), of the
690
738
  # session still running, waiting for an answer, with a live worker, or
691
739
  # (a delegate) with a reply its parent wasn't given (ChildrenStatus).
740
+ # One listing for every node (+rows+, a SessionManager.fleet_rows; nil
741
+ # lists once).
692
742
  # @return [Array<OpenChild>]
693
- def self.open_children(id, state_dir)
743
+ def self.open_children(id, state_dir, rows: nil)
694
744
  require_relative "children_status"
695
- nodes = [id, *SessionManager.descendant_ids(id, state_dir)]
696
- nodes.flat_map { |node| ChildrenStatus.of(node, state_dir: state_dir) }.uniq(&:id).filter_map do |child|
745
+ rows ||= SessionManager.fleet_rows(state_dir: state_dir)
746
+ nodes = [id, *SessionManager.descendant_ids(id, state_dir, rows: rows)]
747
+ nodes.flat_map { |node| ChildrenStatus.of(node, state_dir: state_dir, rows: rows) }.uniq(&:id).filter_map do |child|
697
748
  why = if %w[waiting running].include?(child.state) then child.state
698
749
  elsif child.live then "live"
699
750
  elsif child.delegate && child.last_reply && !child.reported then "unreported reply"
@@ -19,6 +19,9 @@ module Samagotchi
19
19
  # (spec/samagotchi/testing/sandbox_spec.rb).
20
20
  autoload :TurnAnchors, File.expand_path("turn_anchors", __dir__)
21
21
  autoload :VerifyAnchor, File.expand_path("verify_anchor", __dir__)
22
+ # The one delivery path (session_inbox_delivery.rb), loaded on first use:
23
+ # the bundle test kit's load budget stays as it was.
24
+ autoload :InboxDelivery, File.expand_path("session_inbox_delivery", __dir__)
22
25
 
23
26
  # A session directory's file inbox and outbox (see SessionManager for the
24
27
  # layout): input/ holds one JSON file per queued turn, notes/ the context
@@ -45,20 +48,6 @@ module Samagotchi
45
48
  # NOTE_MAX_BYTES.
46
49
  class NoteRejected < ArgumentError; end
47
50
 
48
- # The one delivery path (session_inbox_delivery.rb), loaded on first
49
- # use: the bundle test kit's load budget stays as it was.
50
- delivery = File.expand_path("session_inbox_delivery", __dir__)
51
- %i[Dispatch Delivered Head Hold Resolved WakePolicy].each { |name| autoload name, delivery }
52
-
53
- # A delivery of +kind+ to session +target+ (Dispatch.deliver: resolve,
54
- # refuse, the block's write, one presence read, the WakePolicy's wake).
55
- # @return [Delivered]
56
- def self.deliver(kind, target, **, &) = Dispatch.deliver(kind, target, **, &)
57
-
58
- # The wake half of #deliver alone (Dispatch.wake).
59
- # @return [Delivered]
60
- def self.wake(kind, target, **) = Dispatch.wake(kind, target, **)
61
-
62
51
  # Queue a turn's input file: JSON with the prompt and the sender's ids.
63
52
  # +delivery+ is only written when it is "queue": a step-boundary
64
53
  # message is the default an older worker assumes, and a cut is done by
@@ -379,7 +368,7 @@ module Samagotchi
379
368
  # holding it costs nothing (it runs no turn), running it unread could.
380
369
  def self.participant_input?(input_file)
381
370
  data = JSON.parse(File.read(input_file))
382
- !data.is_a?(Hash) || Steer.participant_author?(data["author"])
371
+ !data.is_a?(Hash) || Speaker.participant_author?(data["author"])
383
372
  rescue JSON::ParserError, SystemCallError
384
373
  true
385
374
  end
@@ -393,8 +382,8 @@ module Samagotchi
393
382
  # Another session's question (ask_session) is a turn of its own.
394
383
  return true if ClientId.session?(data["client_id"])
395
384
 
396
- author = Steer.sender_author(data["author"])
397
- running_author ? author != running_author : Steer.participant_author?(data["author"])
385
+ author = Speaker.sender_author(data["author"])
386
+ running_author ? author != running_author : Speaker.participant_author?(data["author"])
398
387
  end
399
388
 
400
389
  # Whether a message waits that a running turn's next boundary merges and
@@ -16,13 +16,24 @@ module Samagotchi
16
16
  # files unloaded until a delivery starts a worker.
17
17
  autoload :SessionManager, File.expand_path("session_manager", __dir__)
18
18
 
19
- # The session inbox's one delivery path (SessionInbox.deliver): which link
19
+ # The session inbox's one delivery path (InboxDelivery.deliver): which link
20
20
  # a delivery goes to (Head), whether it is refused, its write (the kind's
21
21
  # own writer, unchanged), one read of the worker's presence after it, and
22
- # what that means for a worker (WakePolicy). Loaded on first use, from
23
- # session_inbox.rb's autoloads: the bundle test kit's load budget
24
- # (spec/samagotchi/testing/sandbox_spec.rb) stays as it was.
25
- module SessionInbox
22
+ # what that means for a worker (WakePolicy). Its own name, apart from the
23
+ # file queue (SessionInbox) it writes through: the queue reads files, this
24
+ # starts workers. Loaded on first use, from session_inbox.rb's autoload:
25
+ # the bundle test kit's load budget (spec/samagotchi/testing/sandbox_spec.rb)
26
+ # stays as it was.
27
+ module InboxDelivery
28
+ # A delivery of +kind+ to session +target+ (Dispatch.deliver: resolve,
29
+ # refuse, the block's write, one presence read, the WakePolicy's wake).
30
+ # @return [Delivered]
31
+ def self.deliver(kind, target, **, &) = Dispatch.deliver(kind, target, **, &)
32
+
33
+ # The wake half of #deliver alone (Dispatch.wake).
34
+ # @return [Delivered]
35
+ def self.wake(kind, target, **) = Dispatch.wake(kind, target, **)
36
+
26
37
  # Where a delivery for +asked+ goes: +id+, reached +via+ :pinned (the
27
38
  # kind never follows a chain), :same (followed, nothing to follow),
28
39
  # :head (a later link of +asked+'s chain) or :continuing (a send whose
@@ -133,7 +144,8 @@ module Samagotchi
133
144
 
134
145
  # What the delivery means for the session's worker, in this order:
135
146
  # :gone, :owned (a one-turn chi run's), :deleting (a deleter holds the
136
- # lock: the session is going, no worker sees it), :live (a worker holds it: it
147
+ # lock: the session is going, no worker sees it), :starting (a
148
+ # storage migration holds it still: OwnerLock::MIGRATING), :live (a worker holds it: it
137
149
  # sees the delivery), :stopped (the stop marker: the only "stopped",
138
150
  # the file's status never decides), :archived, :starting (a worker is
139
151
  # being started: it reads the store on its first pass), :none (the
@@ -147,6 +159,10 @@ module Samagotchi
147
159
  return :gone unless presence.exists?
148
160
  return :owned if presence.tui_owned?
149
161
  return :deleting if presence.deleting?
162
+ # A storage migration still holding the lock past the wait
163
+ # (WorkerPresence.settled): read as a start pending, the delivery
164
+ # on disk for the next start.
165
+ return :starting if presence.migrating?
150
166
  return :live if presence.owned?
151
167
  return :stopped if presence.stop_marked? && !unstops
152
168
  return :archived if presence.archived? && !starts_archived
@@ -315,7 +331,7 @@ module Samagotchi
315
331
  end
316
332
  end
317
333
 
318
- # SessionInbox.deliver and SessionInbox.wake.
334
+ # InboxDelivery.deliver and InboxDelivery.wake.
319
335
  module Dispatch
320
336
  # What a start each kind makes is logged as (SessionManager.start_for_inbox).
321
337
  START_EVENTS = { ring: :delegate_woke_parent, push: :context_woke_session, peer: :peer_woke_session }.freeze
@@ -336,7 +352,7 @@ module Samagotchi
336
352
  # @param resolved [Resolved, nil] the link, as the caller resolved it
337
353
  # already (Head.resolve for this kind): not walked again
338
354
  # @param owner [#call, nil] (id) the owner lock's holder (a stand-in
339
- # manager's session_owner); nil: SessionManager.session_owner
355
+ # manager's session_owner); nil: OwnerLock.for_session
340
356
  # @param start [#call, nil] (id) starts a worker, returns :woken (or
341
357
  # why not); nil: SessionManager.start_for_inbox
342
358
  # @yieldparam id [String] the session it is written under
@@ -390,15 +406,20 @@ module Samagotchi
390
406
  policy.refusal(presence(id, state_dir, owner), load: -> { Session.load(id, state_dir: state_dir) })
391
407
  end
392
408
 
409
+ # The decision reads the presence once a storage migration's hold is
410
+ # gone (WorkerPresence.settled: a ring, a note, a verdict land, and a
411
+ # ring's wake starts its worker after it).
393
412
  def apply(policy, resolved, state_dir, wake:, owner:, start:)
394
413
  id = resolved.id
395
- outcome = policy.decide(presence(id, state_dir, owner), wake: wake, load: -> { Session.load(id, state_dir: state_dir) })
414
+ found = presence(id, state_dir, owner, settled: true)
415
+ outcome = policy.decide(found, wake: wake, load: -> { Session.load(id, state_dir: state_dir) })
396
416
  if outcome == :start
397
417
  outcome = if start
398
418
  start.call(id)
399
419
  else
400
- SessionManager.start_for_inbox(id, state_dir: state_dir,
401
- event: START_EVENTS.fetch(policy.kind))
420
+ SessionManager.start_for_inbox(
421
+ id, state_dir: state_dir, event: START_EVENTS.fetch(policy.kind)
422
+ )
402
423
  end
403
424
  end
404
425
  status = %i[live woken starting].include?(outcome) ? :queued : :waits
@@ -415,8 +436,10 @@ module Samagotchi
415
436
  SessionInbox.find_new_note_files(Session.session_dir(id, state_dir: state_dir)).size
416
437
  end
417
438
 
418
- def presence(id, state_dir, owner)
419
- reader = owner ? -> { owner.call(id) } : -> { SessionManager.session_owner(id, state_dir: state_dir) }
439
+ def presence(id, state_dir, owner, settled: false)
440
+ reader = owner ? -> { owner.call(id) } : -> { OwnerLock.for_session(id, state_dir: state_dir) }
441
+ return WorkerPresence.settled(id, state_dir: state_dir, owner: reader) if settled
442
+
420
443
  WorkerPresence.read(id, state_dir: state_dir, owner: reader)
421
444
  end
422
445
  end
@@ -11,7 +11,7 @@ module Samagotchi
11
11
  # deleter holds the lock) are refusals. (:off and :scratch are
12
12
  # op-local: a delivery that asks no worker, a scratch session's file.)
13
13
  # A :shared row's delivery while a transition holds the lock
14
- # (SessionInbox::Hold, Delivered#wake) is :busy, or :continued when the
14
+ # (InboxDelivery::Hold, Delivered#wake) is :busy, or :continued when the
15
15
  # holder is a continue naming its new link (a resolution fact, as
16
16
  # continuing is: TRANSITIONING names :busy).
17
17
  DELIVERY_CELLS = { start: :go, live: :go, starting: :wait, stopped: :wait, archived: :wait, none: :wait,
@@ -59,12 +59,12 @@ module Samagotchi
59
59
  }.freeze
60
60
 
61
61
  # {op => {state => cell}}: every operation in every state (STATES order).
62
- # The delivery columns are generated from SessionInbox::WakePolicy::ROWS
62
+ # The delivery columns are generated from InboxDelivery::WakePolicy::ROWS
63
63
  # (each row's #decide on CANONICAL, wake asked, no scratch file), so a
64
64
  # row change moves its column; the literal columns are goldens of what
65
65
  # the deciders do, pinned by spec/session_lifecycle/matrix_spec.rb.
66
66
  ALLOWED = lambda do
67
- deliveries = SessionInbox::WakePolicy::ROWS.to_h do |kind, policy|
67
+ deliveries = InboxDelivery::WakePolicy::ROWS.to_h do |kind, policy|
68
68
  [kind, STATES.to_h { |state| [state, DELIVERY_CELLS.fetch(policy.decide(CANONICAL.fetch(state), wake: true, load: nil))] }.freeze]
69
69
  end
70
70
  literal = LITERAL_OPS.each_with_index.to_h do |op, index|
@@ -77,7 +77,7 @@ module Samagotchi
77
77
  # transition holds the session's lock, in every state (it is a fact
78
78
  # beside the state, not one of STATES: any state can be held): each
79
79
  # delivery whose WakePolicy row holds :shared is refused :busy
80
- # (SessionInbox::Hold, before its own checks); a :none row answers its
80
+ # (InboxDelivery::Hold, before its own checks); a :none row answers its
81
81
  # state's cell. TAKES_LOCK: the operations that take the lock
82
82
  # themselves: a stop, exclusively (stop_session: Busy, nothing marked),
83
83
  # and the ring's column, which is a ring's wake (wake_for_report: shared
@@ -85,7 +85,7 @@ module Samagotchi
85
85
  # none and still goes in). The other literal operations read none
86
86
  # (archive's column is archivable!, its checks only).
87
87
  TAKES_LOCK = { stop: :busy, ring: :busy }.freeze
88
- TRANSITIONING = SessionInbox::WakePolicy::ROWS.filter_map { |kind, policy| [kind, :busy] if policy.shared? }
88
+ TRANSITIONING = InboxDelivery::WakePolicy::ROWS.filter_map { |kind, policy| [kind, :busy] if policy.shared? }
89
89
  .to_h.merge(TAKES_LOCK).freeze
90
90
  end
91
91
  end
@@ -48,7 +48,7 @@ module Samagotchi
48
48
  # started for it. The delete sheet's preview reads it per left node.
49
49
  def running?(id, state_dir)
50
50
  presence = WorkerPresence.read(id, state_dir: state_dir,
51
- owner: SessionManager.session_owner(id, state_dir: state_dir))
51
+ owner: OwnerLock.for_session(id, state_dir: state_dir))
52
52
  presence.owned? || presence.starting?
53
53
  end
54
54
 
@@ -58,7 +58,7 @@ module Samagotchi
58
58
  # from one .session_rows.
59
59
  # @return [Hash{String => Array<String>}]
60
60
  def children_by_parent(state_dir, rows)
61
- (rows || SessionManager.session_rows(state_dir: state_dir, include_tests: true, include_archived: true))
61
+ (rows || SessionManager.fleet_rows(state_dir: state_dir))
62
62
  .each_with_object({}) do |row, by_parent|
63
63
  (by_parent[row.parent_id] ||= []) << row.id unless row.parent_id.to_s.empty?
64
64
  end
@@ -11,7 +11,7 @@ require_relative "../session_chain/links"
11
11
  require_relative "../session_chain/successor"
12
12
  require_relative "../session_inbox"
13
13
  require_relative "../session_transition"
14
- require_relative "../steer"
14
+ require_relative "../speaker"
15
15
  require_relative "../worker_link"
16
16
  require_relative "../worker_presence"
17
17
 
@@ -192,7 +192,7 @@ module Samagotchi
192
192
  # the sender resolves again. Others name the id they mean (the web
193
193
  # composer, schedules, delegates, plugins, participants). Any
194
194
  # sender's turn is delivered under the session's transition lock
195
- # shared (its row's hold, SessionInbox::Hold): while a transition
195
+ # shared (its row's hold, InboxDelivery::Hold): while a transition
196
196
  # holds it, :refused 409 link_continued naming a continue's new link
197
197
  # (successor_id), else 409 transition_busy (DeliverResult::BUSY).
198
198
  # @param manager [#resume_session, #write_turn_input] this class, or a
@@ -215,7 +215,7 @@ module Samagotchi
215
215
  bridge: nil, enqueued_id: nil)
216
216
  raise ArgumentError, "deliver_turn needs the sender's client_id" if client_id.to_s.empty?
217
217
 
218
- follow_guard(session_id, state_dir, follow, SessionInbox::WakePolicy.for_sender(client_id)) do
218
+ follow_guard(session_id, state_dir, follow, InboxDelivery::WakePolicy.for_sender(client_id)) do
219
219
  deliver_turn_now(session_id, prompt: prompt, client_id: client_id, images: images, delivery: delivery,
220
220
  no_interrupt: no_interrupt, author: author, pending_limit: pending_limit,
221
221
  anchors: anchors, state_dir: state_dir, manager: manager, bridge: bridge,
@@ -224,7 +224,7 @@ module Samagotchi
224
224
  end
225
225
 
226
226
  # The delivery under the sender's row's transition hold
227
- # (SessionInbox::Hold.under: SessionTransition.shared, never waited
227
+ # (InboxDelivery::Hold.under: SessionTransition.shared, never waited
228
228
  # for), across all of it: the resume, the Bridge's wait and post, the
229
229
  # file and the second liveness check (a frozen worker's post, up to
230
230
  # BridgeClient::READ_TIMEOUT, keeps a continue of the session waiting
@@ -240,7 +240,7 @@ module Samagotchi
240
240
  private def follow_guard(session_id, state_dir, follow, policy)
241
241
  sd = state_dir || Session.default_state_dir
242
242
  held = ->(successor, busy) { held_refusal(session_id, successor, busy, policy) }
243
- SessionInbox::Hold.under(policy, session_id, sd, refused: held) do
243
+ InboxDelivery::Hold.under(policy, session_id, sd, refused: held) do
244
244
  if follow && Session.valid_id?(session_id) && ArchiveStore.archived?(Session.session_dir(session_id, state_dir: sd)) &&
245
245
  (successor = SessionChain::Successor.of(session_id, state_dir: sd, anchor: false))
246
246
  next continued_refusal(session_id, successor)
@@ -284,11 +284,7 @@ module Samagotchi
284
284
  return refused if refused
285
285
 
286
286
  enqueued_id ||= SessionInbox.new_enqueued_id
287
- # Whether the worker that answered reads an agent: id (nil: no Bridge
288
- # answered): the file fallback below keeps it when it does.
289
- agent_reader = nil
290
287
  if (client = bridge.call)
291
- agent_reader = worker_has?(client, Delivery::AGENT_FEATURE) if ClientId.agent?(client_id)
292
288
  if (missing = anchors.present.find { |anchor| !worker_has?(client, anchor.class::FEATURE) })
293
289
  return DeliverResult.new(status: :refused, code: 409, ack: missing.class::UNAVAILABLE)
294
290
  end
@@ -297,19 +293,15 @@ module Samagotchi
297
293
  return queue_unavailable if Delivery.queue?(delivery) && !worker_has?(client, Delivery::QUEUE_FEATURE)
298
294
  # A participant's turn only to a worker that runs it under the
299
295
  # owner's rules: an older one would run it read-only.
300
- return pair_unavailable if Steer.participant_author?(author) && !worker_has?(client, Delivery::PAIR_FEATURE)
296
+ return pair_unavailable if Speaker.participant_author?(author) && !worker_has?(client, Delivery::PAIR_FEATURE)
301
297
  # Another session's question only to a worker that runs it as a visitor's.
302
298
  return ask_unavailable if ClientId.session?(client_id) && !worker_has?(client, Delivery::ASK_FEATURE)
303
299
  # A participant's Revise only to a worker that checks it at the
304
300
  # stamp (ParticipantRevise): an older one would stamp it unchecked.
305
- if Steer.participant_author?(author) && anchors.revises && !worker_has?(client, ReviseAnchor::PARTICIPANT_FEATURE)
301
+ if Speaker.participant_author?(author) && anchors.revises && !worker_has?(client, ReviseAnchor::PARTICIPANT_FEATURE)
306
302
  return DeliverResult.new(status: :refused, code: 409, ack: ReviseAnchor::PARTICIPANT_UNAVAILABLE)
307
303
  end
308
304
 
309
- # compat shim agent_client_id: a worker from before agent: reads
310
- # an agent's line as chi send's (today's owner row).
311
- client_id = ClientId::CLI_SEND if ClientId.agent?(client_id) && !agent_reader
312
-
313
305
  begin
314
306
  options = { prompt: prompt, client_id: client_id, enqueued_id: enqueued_id }
315
307
  options[:images] = images unless images.empty?
@@ -347,26 +339,11 @@ module Samagotchi
347
339
  # (A one-turn chi run holding it is refused as for anyone, below.)
348
340
  # So is another session's question (ask_session: an older worker runs
349
341
  # it with no header).
350
- if (Steer.participant_author?(author) || ClientId.session?(client_id)) &&
342
+ if (Speaker.participant_author?(author) || ClientId.session?(client_id)) &&
351
343
  (holder = delivery_presence(manager, session_id, state_dir)).owned? && !holder.tui_owned?
352
344
  return exiting_refused
353
345
  end
354
346
 
355
- # compat shim agent_client_id: an agent's file while a worker holds
356
- # the session and no Bridge answered (a 413, a 5xx or a socket error
357
- # from one that did keeps the id it read: agent_reader). Its
358
- # sidecar's features say which worker it is: one without
359
- # agent_sender reads chi send's id instead (today's owner row); one
360
- # it can't tell (exiting, its sidecar gone) is refused, as a
361
- # participant's is, never run as the owner. A worker that starts for
362
- # the file reads the agent's id.
363
- if ClientId.agent?(client_id) && agent_reader.nil? &&
364
- (holder = delivery_presence(manager, session_id, state_dir)).owned? && !holder.tui_owned?
365
- case sidecar_names(session_dir, Delivery::AGENT_FEATURE)
366
- when false then client_id = ClientId::CLI_SEND
367
- when nil then return exiting_refused
368
- end
369
- end
370
347
  # A vouched anchor's file (an ask's reply_for: TurnAnchors::Rule) only
371
348
  # for a worker whose sidecar lists its feature, when no Bridge
372
349
  # answered: a worker that starts for the file may be an older chi
@@ -416,7 +393,7 @@ module Samagotchi
416
393
  end
417
394
 
418
395
  # deliver_turn's two liveness checks, on the sender's row of the session
419
- # inbox (SessionInbox::WakePolicy.for_sender: a person's send un-stops
396
+ # inbox (InboxDelivery::WakePolicy.for_sender: a person's send un-stops
420
397
  # and starts an archived link; an unattended sender's does neither,
421
398
  # and +before+ the write it gets DeliverResult::STOPPED / ARCHIVED,
422
399
  # nothing written; after it, a stop or archive that landed since wins
@@ -442,7 +419,7 @@ module Samagotchi
442
419
  # make (+before+ only) or of a session being deleted, else nil
443
420
  private def start_for_send(manager, session_id, state_dir, client_id, before:, written: nil)
444
421
  presence = delivery_presence(manager, session_id, state_dir)
445
- policy = SessionInbox::WakePolicy.for_sender(client_id)
422
+ policy = InboxDelivery::WakePolicy.for_sender(client_id)
446
423
  outcome = policy.decide(presence)
447
424
  yield if outcome == :owned && block_given?
448
425
  if outcome == :deleting || (outcome == :gone && !before)
@@ -503,9 +480,8 @@ module Samagotchi
503
480
  DeliverResult.new(status: :refused, code: 409, ack: Delivery::QUEUE_UNAVAILABLE)
504
481
  end
505
482
 
506
- # A message held back while an exiting worker holds the session: the
507
- # file's reader is unknown (a participant's, a session's question, an
508
- # agent's the sidecar can't vouch for).
483
+ # A participant's turn or another session's question held back while
484
+ # an exiting worker holds the session.
509
485
  private def exiting_refused
510
486
  DeliverResult.new(status: :timeout, via: :file,
511
487
  ack: { "error" => "worker_timeout",
@@ -513,8 +489,7 @@ module Samagotchi
513
489
  end
514
490
 
515
491
  # Whether the worker whose sidecar is in +session_dir+ names
516
- # +feature+ (an agent: id's reader, Delivery::AGENT_FEATURE; a vouched
517
- # anchor's): true, false, or nil when there is no sidecar to read (a
492
+ # +feature+ (a vouched anchor's): true, false, or nil when there is no sidecar to read (a
518
493
  # worker leaving, starting, or gone).
519
494
  private def sidecar_names(session_dir, feature)
520
495
  link = WorkerLink.resolve(session_dir, unlink: false)