samagotchi 0.164.0 → 0.165.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 (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -1
  3. data/docs/architecture.md +7 -5
  4. data/docs/cli.md +20 -13
  5. data/docs/configuration.md +5 -1
  6. data/docs/guardrails.md +13 -16
  7. data/docs/internals/block-renderers.md +1 -1
  8. data/docs/internals/bridge.md +4 -3
  9. data/docs/internals/bundles.md +4 -2
  10. data/docs/internals/inline-widgets.md +8 -1
  11. data/docs/internals/web-frontend.md +36 -9
  12. data/docs/memory.md +12 -7
  13. data/docs/plugins.md +6 -4
  14. data/docs/releasing.md +6 -2
  15. data/docs/scheduling.md +25 -3
  16. data/docs/sessions.md +8 -6
  17. data/docs/testing.md +28 -4
  18. data/lib/samagotchi/block_renderers.rb +3 -6
  19. data/lib/samagotchi/bridge/card_store.rb +2 -1
  20. data/lib/samagotchi/bridge/turn_accumulator.rb +7 -8
  21. data/lib/samagotchi/bridge.rb +45 -3
  22. data/lib/samagotchi/bundle_command/installed_check.rb +2 -1
  23. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  24. data/lib/samagotchi/chi_home.rb +44 -2
  25. data/lib/samagotchi/client_id.rb +34 -3
  26. data/lib/samagotchi/compat.rb +36 -0
  27. data/lib/samagotchi/context_command.rb +3 -0
  28. data/lib/samagotchi/context_providers.rb +5 -6
  29. data/lib/samagotchi/context_sources.rb +2 -2
  30. data/lib/samagotchi/continue_intent.rb +50 -5
  31. data/lib/samagotchi/events.rb +4 -4
  32. data/lib/samagotchi/extension_load.rb +2 -1
  33. data/lib/samagotchi/guardrails/child_boundary.rb +7 -27
  34. data/lib/samagotchi/guardrails/rules.rb +11 -9
  35. data/lib/samagotchi/guardrails/targets.rb +3 -4
  36. data/lib/samagotchi/inline_widgets.rb +16 -1
  37. data/lib/samagotchi/memory_bundle/provenance.rb +46 -4
  38. data/lib/samagotchi/memory_bundle/status.rb +2 -1
  39. data/lib/samagotchi/memory_command.rb +3 -0
  40. data/lib/samagotchi/memory_orphans.rb +4 -7
  41. data/lib/samagotchi/memory_paths.rb +64 -7
  42. data/lib/samagotchi/memory_usage.rb +1 -1
  43. data/lib/samagotchi/plugin/sessions.rb +4 -2
  44. data/lib/samagotchi/project_command.rb +4 -1
  45. data/lib/samagotchi/project_id_clash.rb +37 -26
  46. data/lib/samagotchi/project_init.rb +24 -7
  47. data/lib/samagotchi/reply_wait/poll.rb +2 -1
  48. data/lib/samagotchi/schedules.rb +124 -106
  49. data/lib/samagotchi/send_command.rb +71 -18
  50. data/lib/samagotchi/session.rb +3 -2
  51. data/lib/samagotchi/session_access/local.rb +19 -2
  52. data/lib/samagotchi/session_access/remote.rb +15 -4
  53. data/lib/samagotchi/session_chain/links.rb +109 -0
  54. data/lib/samagotchi/session_chain.rb +7 -19
  55. data/lib/samagotchi/session_continue.rb +138 -54
  56. data/lib/samagotchi/session_inbox_delivery.rb +57 -38
  57. data/lib/samagotchi/session_manager/delivery.rb +104 -18
  58. data/lib/samagotchi/session_manager/removal.rb +13 -7
  59. data/lib/samagotchi/session_manager.rb +1 -0
  60. data/lib/samagotchi/session_retention.rb +20 -10
  61. data/lib/samagotchi/session_setup.rb +31 -1
  62. data/lib/samagotchi/state_format.rb +12 -8
  63. data/lib/samagotchi/terminal_ui/attach_launcher.rb +6 -7
  64. data/lib/samagotchi/terminal_ui.rb +20 -9
  65. data/lib/samagotchi/tools/delegate.rb +2 -1
  66. data/lib/samagotchi/verify_fresh.rb +4 -1
  67. data/lib/samagotchi/version.rb +1 -1
  68. data/lib/samagotchi/web/public/activity.js +2 -2
  69. data/lib/samagotchi/web/public/app.js +13 -13
  70. data/lib/samagotchi/web/public/bar_fold.js +293 -0
  71. data/lib/samagotchi/web/public/card.js +1 -1
  72. data/lib/samagotchi/web/public/composer_mode.js +5 -7
  73. data/lib/samagotchi/web/public/index.html +132 -65
  74. data/lib/samagotchi/web/public/info_bar.js +28 -16
  75. data/lib/samagotchi/web/public/info_bar_model.js +70 -1
  76. data/lib/samagotchi/web/public/more_sheet.js +98 -40
  77. data/lib/samagotchi/web/public/stage_view.js +3 -1
  78. data/lib/samagotchi/web/public/stream_handlers.js +1 -1
  79. data/lib/samagotchi/web/public/turn_events.js +0 -122
  80. data/lib/samagotchi/web/public/turn_model.js +1 -1
  81. data/lib/samagotchi/web/public/widget_picks.js +18 -3
  82. data/lib/samagotchi/web/session_control.rb +15 -2
  83. data/lib/samagotchi/web/session_create.rb +4 -2
  84. data/lib/samagotchi/web/update_check.rb +27 -8
  85. metadata +3 -1
@@ -68,6 +68,67 @@ module Samagotchi
68
68
  # moved the chain's end while it waited for the lock.
69
69
  TRIES = 3
70
70
 
71
+ # One thing a continue carries from the previous link to the new one.
72
+ # +carry+ names the method run in spawn_session's before_spawn (once the
73
+ # link and its chain note are saved, before its worker spawns): it takes
74
+ # a CarryContext and returns what the step carried, kept in #run's
75
+ # +carried+ under +name+. +undo+ maps an UNDO_PHASES phase to the method
76
+ # (taking an UndoContext) that gives it back when the continue fails;
77
+ # empty when there is nothing to undo (CARRY's comment says why).
78
+ # +store+ is the SessionManager::SESSION_STATE_STORES store it carries.
79
+ CarryStep = Data.define(:name, :store, :carry, :undo)
80
+ # What a carry step works on: the previous link (a Session), the new
81
+ # link's id, the delegates moving (ChildMove::Plan#move).
82
+ CarryContext = Data.define(:previous, :to, :state_dir, :move)
83
+ # What an undo works on: the previous link's id, the failed link (nil in
84
+ # :moved_back, which looks it up itself), the delegates the plan moved,
85
+ # and what the forward carry kept (#run's +carried+, by step name).
86
+ UndoContext = Data.define(:from, :link, :state_dir, :moved, :carried)
87
+
88
+ # When a failed continue undoes each step, in time order: :moved_back
89
+ # while the failed link's worker may still run (before
90
+ # #discard_failed_link), :link_stopped once its worker is stopped,
91
+ # :link_unlinked once its session file is gone (out of the chain: a note
92
+ # to the previous link stays there), :link_deleted once its folder is
93
+ # gone. Within a phase, the steps undo in reverse CARRY order.
94
+ UNDO_PHASES = %i[moved_back link_stopped link_unlinked link_deleted].freeze
95
+
96
+ # What a continue carries, in order (the forward carry runs them first
97
+ # to last, in the new link's before_spawn). A new per-session store a
98
+ # continue should carry is added here, with its undo.
99
+ CARRY = [
100
+ # Its open delegates (ChildMove): given back to the previous link
101
+ # first, then the rings one rang the link with just before the undo.
102
+ CarryStep.new(name: :children, store: :session_files, carry: :carry_children,
103
+ undo: { moved_back: :undo_move, link_stopped: :return_rings }),
104
+ # Its movable context too, with a note saying what moved and stayed.
105
+ # No undo: ContextSources.move copies (the previous link keeps its
106
+ # sources), and the link's copies go with its delete (remove_files).
107
+ CarryStep.new(name: :context, store: :context_sources, carry: :carry_context, undo: {}),
108
+ # Its objectives: the list reads as the latest word. No undo: a note
109
+ # written into the link only (NoteMove::CONTINUE_SOURCES: never given
110
+ # back), which goes with its delete.
111
+ CarryStep.new(name: :objectives, store: :session_files, carry: :carry_objectives, undo: {}),
112
+ # Its waiting notes last: the carry above read a pending /objectives
113
+ # edit from them first. They keep their names, so they still sort
114
+ # ahead of the link's own notes. A note sent to the previous link
115
+ # from here on goes to this one (SessionInbox::Head), and a
116
+ # straggler is adopted by its worker (NoteMove.adoption). Given back
117
+ # once the link is out of the chain, and again for a note an older chi
118
+ # wrote after its folder went.
119
+ CarryStep.new(name: :notes, store: :session_files, carry: :carry_notes,
120
+ undo: { link_unlinked: :give_back_notes, link_deleted: :give_back_strays })
121
+ ].freeze
122
+
123
+ # The SessionManager::SESSION_STATE_STORES a continue deliberately
124
+ # leaves with the previous link, and why: every store is in CARRY or here.
125
+ NOT_CARRIED = {
126
+ plugin_session_state: "a bundle's per-session state (ctx.session_dir) stays with the archived link",
127
+ participants: "the archive ends the previous link's review (D8); a continue copies no grants",
128
+ system_one_runs: "System 1's saved runs record the previous link's own turns",
129
+ context_candidates_log: "the candidates log records the previous link's own turns"
130
+ }.freeze
131
+
71
132
  # The model a new link runs on (#model_for): the name to spawn it with,
72
133
  # and why it isn't the previous link's (nil when it is, or was given).
73
134
  ModelChoice = Data.define(:model, :warning)
@@ -174,24 +235,22 @@ module Samagotchi
174
235
  end
175
236
  successor_id = SecureRandom.uuid
176
237
  intent_path = continue_intent_path(id, state_dir)
177
- intent_data = { successor_id: successor_id, previously_archived: already }
178
- AtomicFile.write(intent_path, "#{JSON.generate(intent_data)}\n", perm: 0o600)
238
+ ContinueIntent.write(intent_path, successor_id: successor_id, previously_archived: already)
179
239
  archive = SessionManager.archive_session(id, state_dir: state_dir, wait: archive_wait, except: plan.move)
180
240
  archived = archive.archived - already
181
- intent_data[:archived_ids] = archived
182
- AtomicFile.write(intent_path, "#{JSON.generate(intent_data)}\n", perm: 0o600)
183
- # The notes the new link took from this one (NoteMove.move).
184
- notes = []
241
+ ContinueIntent.write(intent_path, successor_id: successor_id, previously_archived: already, archived_ids: archived)
242
+ # What each CARRY step took to the new link, by name.
243
+ carried = {}
185
244
  begin
186
245
  link = spawn_continuation(previous, prompt: prompt, preview: preview, choice: checked.choice, state_dir: state_dir,
187
- move: plan.move, client_id: client_id, notes: notes, session_id: successor_id)
246
+ move: plan.move, client_id: client_id, carried: carried, session_id: successor_id)
188
247
  rescue StandardError, Interrupt
189
248
  # Nothing started: the delegates go back, what this archived goes
190
249
  # back to the lists, and a new link saved before its worker failed
191
250
  # to spawn goes (left, it would hold the chain: "continued already",
192
251
  # last: to it), its notes given back first.
193
- undo_failed_continue(id, state_dir, moved: plan.move, archived: archived) do
194
- discard_failed_link(id, state_dir, notes: notes)
252
+ undo_failed_continue(id, state_dir, moved: plan.move, archived: archived, carried: carried) do
253
+ discard_failed_link(id, state_dir, moved: plan.move, carried: carried)
195
254
  end
196
255
  FileUtils.rm_f(intent_path)
197
256
  raise
@@ -211,18 +270,11 @@ module Samagotchi
211
270
  next unless lock.flock(File::LOCK_EX | File::LOCK_NB)
212
271
  next unless File.file?(path)
213
272
 
214
- intent = JSON.parse(File.read(path))
215
- intent = {} unless intent.is_a?(Hash)
216
- successor_id = intent["successor_id"]
217
- successor_path = Session.session_file(successor_id, state_dir: state_dir) if Session.valid_id?(successor_id)
273
+ intent = ContinueIntent.read(path)
274
+ successor_path = Session.session_file(intent.successor_id, state_dir: state_dir) if intent.successor_id
218
275
  successor = JSON.parse(File.read(successor_path)) if successor_path && File.file?(successor_path)
219
276
  unless successor.is_a?(Hash) && successor["continues"] == session.id
220
- previously_archived = Array(intent["previously_archived"]).select { |id| Session.valid_id?(id) }
221
- archived = if intent["archived_ids"].is_a?(Array)
222
- intent["archived_ids"].select { |id| Session.valid_id?(id) }
223
- else
224
- archived_descendants(session.id, sessions, state_dir) - previously_archived
225
- end
277
+ archived = intent.archived_ids || (archived_descendants(session.id, sessions, state_dir) - intent.previously_archived)
226
278
  undo_failed_continue(session.id, state_dir, moved: [], archived: archived)
227
279
  end
228
280
  FileUtils.rm_f(path)
@@ -259,8 +311,8 @@ module Samagotchi
259
311
  end
260
312
  end
261
313
 
262
- private_class_method def self.undo_failed_continue(id, state_dir, moved:, archived:)
263
- undo_move(id, state_dir, moved: moved)
314
+ private_class_method def self.undo_failed_continue(id, state_dir, moved:, archived:, carried: {})
315
+ undo_carry(:moved_back, UndoContext.new(from: id, link: nil, state_dir: state_dir, moved: moved, carried: carried))
264
316
  begin
265
317
  yield if block_given?
266
318
  ensure
@@ -269,41 +321,81 @@ module Samagotchi
269
321
  end
270
322
  end
271
323
 
324
+ # The CARRY steps' undos for +phase+ (UNDO_PHASES), in reverse CARRY order.
325
+ private_class_method def self.undo_carry(phase, ctx)
326
+ CARRY.reverse_each do |step|
327
+ method = step.undo[phase]
328
+ send(method, ctx) if method
329
+ end
330
+ end
331
+
332
+ # CARRY's forward steps (CarryContext).
333
+ private_class_method def self.carry_children(ctx)
334
+ ChildMove.apply(ctx.move, from: ctx.previous.id, to: ctx.to, state_dir: ctx.state_dir)
335
+ end
336
+
337
+ private_class_method def self.carry_context(ctx)
338
+ ContextMove.call(from: ctx.previous.id, to: ctx.to, mode: :continue, state_dir: ctx.state_dir)
339
+ end
340
+
341
+ private_class_method def self.carry_objectives(ctx)
342
+ Objectives.carry(ctx.previous, to: ctx.to, state_dir: ctx.state_dir)
343
+ end
344
+
345
+ # @return [Array<String>] the names of the notes moved to the link
346
+ private_class_method def self.carry_notes(ctx)
347
+ NoteMove.move(from: ctx.previous.id, to: ctx.to, state_dir: ctx.state_dir)
348
+ end
349
+
272
350
  # The delegates a failed #run moved, or began to (its
273
- # intent names them), go back to +id+.
274
- private_class_method def self.undo_move(id, state_dir, moved:)
351
+ # intent names them), go back to the previous link.
352
+ private_class_method def self.undo_move(ctx)
353
+ id = ctx.from
354
+ state_dir = ctx.state_dir
275
355
  link = SessionChain.next_of(id, state_dir: state_dir) or return
276
- ids = ChildMove.read_intent(link, state_dir: state_dir)&.ids || moved
356
+ ids = ChildMove.read_intent(link, state_dir: state_dir)&.ids || ctx.moved
277
357
  ChildMove.apply(ids, from: link, to: id, state_dir: state_dir, undo: true)
278
358
  ChildMove.clear_intent(link, state_dir: state_dir)
279
359
  rescue StandardError => e
280
360
  Log.warn(:worker, "continue_undo_move_failed", sid: id, error: e.class.name, msg: e.message)
281
361
  end
282
362
 
363
+ # The rings a moved delegate wrote into the failed link just before the
364
+ # undo go back to the previous link.
365
+ private_class_method def self.return_rings(ctx)
366
+ ChildMove.return_rings(from: ctx.link, to: ctx.from, state_dir: ctx.state_dir)
367
+ end
368
+
369
+ # The notes the link took (CARRY's :notes), with any sent to it meanwhile.
370
+ private_class_method def self.give_back_notes(ctx)
371
+ NoteMove.give_back(from: ctx.link, to: ctx.from, moved: Array(ctx.carried[:notes]), state_dir: ctx.state_dir)
372
+ end
373
+
283
374
  # The link a failed #run saved (no other continues +id+:
284
375
  # check! made sure under the lock): its worker stopped if one
285
376
  # started, then the rings a moved delegate wrote into it just before
286
- # the undo go back to +id+. Then its session file goes first, so the
287
- # link is out of the chain (a note to +id+ stays there:
288
- # SessionInbox::Head) and a writer that resolved it before finds it gone,
289
- # then the notes it moved there (+notes+) go back with any sent to it
290
- # meanwhile (NoteMove.give_back), then the link's folder goes, and one
291
- # last look gives back a note an older chi wrote after the delete
292
- # (its write_note recreates the folder). A worker that outlives the
293
- # stop is raised (SessionManager::DeleteRefused): the link stays, and
294
- # the caller hears it.
295
- private_class_method def self.discard_failed_link(id, state_dir, notes:)
377
+ # the undo go back to +id+ (CARRY's :link_stopped undos). Then its
378
+ # session file goes first, so the link is out of the chain (a note to
379
+ # +id+ stays there: SessionInbox::Head) and a writer that resolved it
380
+ # before finds it gone, then the notes it moved there go back with any
381
+ # sent to it meanwhile (:link_unlinked: NoteMove.give_back), then the
382
+ # link's folder goes, and one last look gives back a note an older chi
383
+ # wrote after the delete (:link_deleted; its write_note recreates the
384
+ # folder). A worker that outlives the stop is raised
385
+ # (SessionManager::DeleteRefused): the link stays, and the caller hears it.
386
+ private_class_method def self.discard_failed_link(id, state_dir, moved:, carried:)
296
387
  orphan = SessionChain.next_of(id, state_dir: state_dir) or return
297
388
  if SessionManager.refuse_tui!(orphan, state_dir: state_dir) &&
298
389
  !SessionManager.stop_session(orphan, state_dir: state_dir, wait: SessionManager::STOP_WAIT)
299
390
  raise SessionManager::DeleteRefused.new(orphan, :still_stopping)
300
391
  end
301
392
 
302
- ChildMove.return_rings(from: orphan, to: id, state_dir: state_dir)
393
+ ctx = UndoContext.new(from: id, link: orphan, state_dir: state_dir, moved: moved, carried: carried)
394
+ undo_carry(:link_stopped, ctx)
303
395
  FileUtils.rm_f(Session.session_file(orphan, state_dir: state_dir))
304
- NoteMove.give_back(from: orphan, to: id, moved: notes, state_dir: state_dir)
396
+ undo_carry(:link_unlinked, ctx)
305
397
  SessionManager.delete_session(orphan, state_dir: state_dir, allow_scheduled: true)
306
- give_back_strays(orphan, id, state_dir)
398
+ undo_carry(:link_deleted, ctx)
307
399
  rescue SessionManager::DeleteRefused
308
400
  raise
309
401
  rescue StandardError => e
@@ -312,11 +404,13 @@ module Samagotchi
312
404
 
313
405
  # A failed link's folder an older chi's note brought back after its
314
406
  # delete: the note goes back to +id+, the folder goes again.
315
- private_class_method def self.give_back_strays(orphan, id, state_dir)
407
+ private_class_method def self.give_back_strays(ctx)
408
+ orphan = ctx.link
409
+ state_dir = ctx.state_dir
316
410
  dir = Session.session_dir(orphan, state_dir: state_dir)
317
411
  return unless Dir.exist?(dir) && !Session.exist?(orphan, state_dir: state_dir)
318
412
 
319
- NoteMove.give_back(from: orphan, to: id, moved: [], state_dir: state_dir)
413
+ NoteMove.give_back(from: orphan, to: ctx.from, moved: [], state_dir: state_dir)
320
414
  FileUtils.rm_rf(dir)
321
415
  end
322
416
 
@@ -333,25 +427,15 @@ module Samagotchi
333
427
  # The new link, holding off any worker but its own while +move+ goes to
334
428
  # it (spawn_session marks it starting before its file is saved:
335
429
  # StartingMarker.fresh?).
336
- # @param notes [Array<String>] gets the names of the notes moved to it
337
- private_class_method def self.spawn_continuation(previous, prompt:, preview:, choice:, state_dir:, move:, notes:,
430
+ # @param carried [Hash] gets what each CARRY step took to it, by name
431
+ private_class_method def self.spawn_continuation(previous, prompt:, preview:, choice:, state_dir:, move:, carried:,
338
432
  session_id:, client_id: nil)
339
433
  recap = RecapStore.read(Session.session_dir(previous.id, state_dir: state_dir))
340
434
  start = prompt.to_s.strip.empty? ? nil : prompt
341
435
  preview = previous.first_preview if preview.to_s.strip.empty? && start.nil?
342
- moved_context = nil
343
436
  before_spawn = lambda do |link|
344
- ChildMove.apply(move, from: previous.id, to: link.id, state_dir: state_dir)
345
- # Its movable context too, with a note saying what moved and stayed.
346
- moved_context = ContextMove.call(from: previous.id, to: link.id, mode: :continue, state_dir: state_dir)
347
- # Its objectives: the list reads as the latest word.
348
- Objectives.carry(previous, to: link.id, state_dir: state_dir)
349
- # Its waiting notes last: the carry above read a pending /objectives
350
- # edit from them first. They keep their names, so they still sort
351
- # ahead of the link's own notes. A note sent to the previous link
352
- # from here on goes to this one (SessionInbox::Head), and a
353
- # straggler is adopted by its worker (NoteMove.adoption).
354
- notes.concat(NoteMove.move(from: previous.id, to: link.id, state_dir: state_dir))
437
+ ctx = CarryContext.new(previous: previous, to: link.id, state_dir: state_dir, move: move)
438
+ CARRY.each { |step| carried[step.name] = send(step.carry, ctx) }
355
439
  end
356
440
  link = SessionManager.spawn_session(
357
441
  prompt: start, preview: preview, title: previous.title, working_directory: previous.working_directory,
@@ -361,7 +445,7 @@ module Samagotchi
361
445
  session_id: session_id
362
446
  )
363
447
  link.moved_children = move
364
- link.moved_context = moved_context
448
+ link.moved_context = carried[:context]
365
449
  link.opener = SessionChain.opener_for(previous)
366
450
  # The fallback's warning first: spawn_session set its own.
367
451
  warnings = [choice.warning, link.model_warning].compact
@@ -3,8 +3,10 @@
3
3
  require_relative "session"
4
4
  require_relative "session_inbox"
5
5
  require_relative "archive_store"
6
+ require_relative "client_id"
6
7
  require_relative "worker_presence"
7
8
  require_relative "continue_intent"
9
+ require_relative "session_chain/links"
8
10
 
9
11
  module Samagotchi
10
12
  # Loaded on first use: this autoload keeps SessionManager and its ~80
@@ -64,8 +66,10 @@ module Samagotchi
64
66
  # What a delivery of one kind may do to a session's worker: one frozen
65
67
  # row per kind (ROWS), decided on WorkerPresence's predicates alone, in
66
68
  # one order for every kind (#decide).
67
- # @!attribute kind [Symbol] :send, :note, :push, :ring, :fresh_verdict or
68
- # :leave (WorkerBoot's respawn, not a delivery)
69
+ # @!attribute kind [Symbol] :send, :note, :push, :ring, :fresh_verdict,
70
+ # :leave (WorkerBoot's respawn, not a delivery), or a turn delivery's
71
+ # unattended sender (deliver_turn, #for_sender): :schedule, :plugin,
72
+ # :delegate, :participant, :automatic
69
73
  # @!attribute resolves [Symbol] which link it goes to (Head.resolve):
70
74
  # :pinned the id named; :head the chain's newest link from it; :reader
71
75
  # the link that reads the named one's file (a ring: the file stays with
@@ -73,9 +77,9 @@ module Samagotchi
73
77
  # rings, so the wake goes one hop at most)
74
78
  # @!attribute starts [Boolean] it may start a worker
75
79
  # @!attribute unstops [Boolean] it starts a stopped session (a resume
76
- # clears the stop): send only
80
+ # clears the stop): send only (a person's)
77
81
  # @!attribute starts_archived [Boolean] it starts an archived link
78
- # (input from a person un-archives it): send only
82
+ # (input from a person un-archives it): send (and leave) only
79
83
  # @!attribute refuse_tui [Boolean] a one-turn chi run's session refuses it
80
84
  # @!attribute refuse_scratch [Boolean] a chi scratch session refuses it
81
85
  WakePolicy = Data.define(:kind, :resolves, :starts, :unstops, :starts_archived, :refuse_tui, :refuse_scratch) do
@@ -83,6 +87,11 @@ module Samagotchi
83
87
  # @raise [ArgumentError] no such kind
84
88
  def self.for(kind) = WakePolicy::ROWS.fetch(kind) { raise ArgumentError, "no inbox kind #{kind.inspect}" }
85
89
 
90
+ # The row a turn delivery from +client_id+ reads: its kind by the
91
+ # sender (ClientId.wake_kind), never a caller's flag.
92
+ # @return [WakePolicy]
93
+ def self.for_sender(client_id) = self.for(ClientId.wake_kind(client_id))
94
+
86
95
  # Why the delivery is refused before anything is written, else nil:
87
96
  # :gone (no session), :owned (a one-turn chi run's), :scratch.
88
97
  # @param load [#call, nil] the session, read only when the scratch
@@ -126,12 +135,15 @@ module Samagotchi
126
135
  end
127
136
 
128
137
  # A send follows a chain as a note does: input for a continued link
129
- # goes to its chain's newest link (chi send resolves it once up front,
130
- # SessionAccess#head; SessionManager.deliver_turn resolves nothing).
138
+ # goes to its chain's newest link (chi send resolves it up front,
139
+ # SessionAccess#head; SessionManager.deliver_turn resolves nothing, but
140
+ # refuses that followed send when a continue took the archived link
141
+ # since, and chi send resolves again: deliver_turn's follow:).
131
142
  # What stays on the id it names doesn't ask Head: the web composer (a
132
143
  # person on that page means that session), a participant's grant (one
133
- # session), delegate follow-ups, Plugin::Sessions and Schedules (their
134
- # own rule).
144
+ # session), delegate follow-ups, Plugin::Sessions and Schedules (the
145
+ # chain's end, SessionChain::Links#latest: a firing continues the end,
146
+ # and never un-archives a retired chain).
135
147
  WakePolicy::ROWS = {
136
148
  send: WakePolicy.new(kind: :send, resolves: :head, starts: true, unstops: true, starts_archived: true,
137
149
  refuse_tui: true, refuse_scratch: true),
@@ -148,7 +160,16 @@ module Samagotchi
148
160
  # lock's release). A stop wins; an archived session's input still
149
161
  # starts it, as a send's would have.
150
162
  leave: WakePolicy.new(kind: :leave, resolves: :pinned, starts: true, unstops: false, starts_archived: true,
151
- refuse_tui: false, refuse_scratch: false)
163
+ refuse_tui: false, refuse_scratch: false),
164
+ # A turn delivery's unattended senders (deliver_turn reads the row by
165
+ # the sender, ClientId.wake_kind): they start an idle session as a send
166
+ # does, but a stop and an archive are the owner's (the sender is
167
+ # refused, SessionManager::DeliverResult::STOPPED / ARCHIVED). They
168
+ # stay on the id they name. :automatic is any sender chi doesn't name.
169
+ **%i[schedule plugin delegate participant automatic].to_h do |kind|
170
+ [kind, WakePolicy.new(kind: kind, resolves: :pinned, starts: true, unstops: false, starts_archived: false,
171
+ refuse_tui: true, refuse_scratch: true)]
172
+ end
152
173
  }.freeze
153
174
 
154
175
  # Which link of a chain a delivery goes to.
@@ -162,10 +183,11 @@ module Samagotchi
162
183
  # file could make ends where it started); a :reader kind goes one hop
163
184
  # at most. Then, for a kind that would un-archive the link it reaches
164
185
  # (a send: WakePolicy#starts_archived), a continue of that link in
165
- # flight (ContinueIntent.in_flight: its successor not saved yet) gives
166
- # :continuing, the successor's id. A note needs no such check: written
167
- # to the link before NoteMove.move it moves, after it the new link's
168
- # worker adopts it (NoteMove.adoption).
186
+ # flight (ContinueIntent.in_flight) gives the successor's id: via
187
+ # :continuing while it is not saved yet, :head once it is
188
+ # (#continuing). A note needs no such check: written to the link
189
+ # before NoteMove.move it moves, after it the new link's worker adopts
190
+ # it (NoteMove.adoption).
169
191
  # @return [Resolved]
170
192
  def resolve(kind, id, state_dir:)
171
193
  policy = WakePolicy.for(kind)
@@ -175,38 +197,35 @@ module Samagotchi
175
197
  (continuing(resolved, state_dir) if policy.starts_archived) || resolved
176
198
  end
177
199
 
200
+ # The chain walk is SessionChain::Links': #first_live for :head (the
201
+ # send/note rule: the first live link, not the chain's end that a
202
+ # schedule fires at, Links#latest), its successor alone for :reader.
178
203
  def walk(how, id, state_dir)
179
- same = Resolved.new(id: id, asked: id, via: :same)
180
- return same unless archived?(id, state_dir)
181
-
182
- nexts = successors(state_dir)
183
- seen = [id]
184
- while (following = nexts[seen.last]) && !seen.include?(following)
185
- seen << following
186
- break if how == :reader || !archived?(following, state_dir)
187
- end
188
- seen.size == 1 ? same : Resolved.new(id: seen.last, asked: id, via: :head)
204
+ links = SessionChain::Links.new(state_dir: state_dir)
205
+ reached = how == :reader ? reader(links, id) : links.first_live(id)
206
+ reached == id ? Resolved.new(id: id, asked: id, via: :same) : Resolved.new(id: reached, asked: id, via: :head)
189
207
  end
190
208
 
191
- # +resolved+'s link being continued, its successor not saved yet: the
192
- # successor, via :continuing; else nil (none in flight, or saved: the
193
- # walk reached it).
194
- def continuing(resolved, state_dir)
195
- successor = ContinueIntent.in_flight(resolved.id, state_dir: state_dir)
196
- return nil if successor.nil? || Session.exist?(successor, state_dir: state_dir)
209
+ # The link that reads an archived +id+'s file: its successor, one hop.
210
+ def reader(links, id)
211
+ return id unless links.archived?(id)
197
212
 
198
- Resolved.new(id: successor, asked: resolved.asked, via: :continuing)
213
+ links.next_of(id) || id
199
214
  end
200
215
 
201
- def archived?(id, state_dir)
202
- Session.valid_id?(id) && ArchiveStore.archived?(Session.session_dir(id, state_dir: state_dir))
203
- end
216
+ # +resolved+'s link being continued: its successor, via :continuing
217
+ # while it is not saved yet, via :head once it is (saved after the
218
+ # walk's listing: the walk still named the old link, which a send
219
+ # would un-archive beside it); else nil (none in flight). A continue
220
+ # that ends after this look is deliver_turn's to catch (a followed
221
+ # send to an archived link with a successor is refused, and chi send
222
+ # resolves again).
223
+ def continuing(resolved, state_dir)
224
+ successor = ContinueIntent.in_flight(resolved.id, state_dir: state_dir)
225
+ return nil if successor.nil?
204
226
 
205
- # id → the session that continues it (SessionChain.next_of's: the
206
- # earliest one, archived ones too).
207
- def successors(state_dir)
208
- Session.list(state_dir: state_dir, sort: "created_at", order: "asc", include_archived: true)
209
- .each_with_object({}) { |s, nexts| nexts[s.continues] ||= s.id if s.continues }
227
+ via = Session.exist?(successor, state_dir: state_dir) ? :head : :continuing
228
+ Resolved.new(id: successor, asked: resolved.asked, via: via)
210
229
  end
211
230
  end
212
231