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
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fileutils"
3
4
  require_relative "atomic_file"
4
5
  require_relative "memory_paths"
5
6
  require_relative "paths"
@@ -11,52 +12,62 @@ module Samagotchi
11
12
  # MemoryOrphans), at every session start while it lasts.
12
13
  #
13
14
  # The claim: an id-keyed memories folder's `.root` names the folder that
14
- # last started a session with it (one line; a dotfile, so no *.md glob
15
- # lists it). Written only here, at session start, never by the key
16
- # resolver: a read-only command or a memory call must not write.
17
- ProjectIdClash = Data.define(:root, :other, :id) do
15
+ # owns it (one line; a dotfile, so no *.md glob lists it). A new folder is
16
+ # claimed before use; init also claims folders it moves or copies.
17
+ ProjectIdClash = Data.define(:root, :other, :id, :folder) do
18
+ def initialize(root:, other:, id:, folder: File.join(MemoryPaths.projects_dir, id))
19
+ super
20
+ end
21
+
18
22
  # The one text for the condition.
19
23
  def message
20
- "#{Paths.tilde(root)} and #{Paths.tilde(other)} share chi project id #{id} (a copied folder?): they share " \
21
- "memories. Run `chi project init --new-id` in the copy to give it its own."
24
+ if other.empty?
25
+ "#{folder} already exists and is not claimed by #{Paths.tilde(root)}; run `chi project init --new-id` " \
26
+ "to give this project its own id"
27
+ else
28
+ "#{Paths.tilde(root)} and #{Paths.tilde(other)} share chi project id #{id} (a copied folder?): " \
29
+ "#{folder} is already in use. Run `chi project init --new-id` in the copy to give it its own."
30
+ end
22
31
  end
23
32
  end
24
33
 
25
34
  class ProjectIdClash
26
- CLAIM = ".root"
35
+ CLAIM = MemoryPaths::CLAIM
27
36
 
28
- # +cwd+'s project, when a marked one with an id: a clash when the claim
29
- # names another folder that still has a marker with this id; otherwise
30
- # the claim is set to this root (when it changes; a moved folder just
31
- # takes it over). Only once the id folder exists: nothing is made for a
32
- # project with no memories yet.
37
+ # +cwd+'s project, when a marked one with an id: a clash when the id
38
+ # folder is already taken; otherwise the folder is claimed for this root
39
+ # (a moved folder just takes it over).
33
40
  # @return [ProjectIdClash, nil]
34
41
  def self.check(env: ENV, cwd: Dir.pwd, home: MemoryPaths.default_home)
35
42
  found = MemoryPaths.lookup(cwd, home: home)
36
43
  return nil unless found.id
37
44
 
38
45
  dir = File.join(MemoryPaths.projects_dir(env: env), found.id)
39
- return nil unless File.directory?(dir)
46
+ unless File.exist?(dir) || File.symlink?(dir)
47
+ FileUtils.mkdir_p(File.dirname(dir))
48
+ created = begin
49
+ Dir.mkdir(dir)
50
+ true
51
+ rescue Errno::EEXIST
52
+ false
53
+ end
54
+ if created
55
+ AtomicFile.write(File.join(dir, CLAIM), "#{found.root}\n")
56
+ return nil
57
+ end
58
+ end
40
59
 
41
60
  claim = File.join(dir, CLAIM)
42
61
  other = File.file?(claim) ? File.read(claim, 4096).to_s.lines.first.to_s.strip : ""
43
- return nil if other == found.root
44
- return new(root: found.root, other: other, id: found.id) if same_project?(other, found.id, home)
62
+ conflict = MemoryPaths.project_id_conflict(root: found.root, id: found.id, env: env, home: home)
63
+ return new(root: found.root, other: other, id: found.id, folder: dir) if conflict
45
64
 
46
- AtomicFile.write(claim, "#{found.root}\n")
65
+ AtomicFile.write(claim, "#{found.root}\n") unless other == found.root
47
66
  nil
48
67
  rescue SystemCallError
68
+ # Session start goes on: a memories folder chi can't make or claim
69
+ # is no clash to warn about.
49
70
  nil
50
71
  end
51
-
52
- # Whether +other+ still is a marked folder with +id+ (not gone, not
53
- # given another id).
54
- def self.same_project?(other, id, home)
55
- return false if other.empty? || !File.directory?(other)
56
-
57
- found = MemoryPaths.lookup(other, home: home)
58
- found.marker? && found.root == File.realpath(other) && found.id == id
59
- end
60
- private_class_method :same_project?
61
72
  end
62
73
  end
@@ -126,16 +126,31 @@ module Samagotchi
126
126
  end
127
127
 
128
128
  root = found.root
129
- old_key = MemoryPaths.key_for(root: root, id: found.id)
129
+ conflict = nil
130
+ old_key = begin
131
+ MemoryPaths.key_for(root: root, id: found.id, env: env, home: home)
132
+ rescue MemoryPaths::ProjectIdConflict => e
133
+ conflict = e
134
+ nil
135
+ end
130
136
  id = ProjectId.generate(File.basename(root))
131
137
  lines = ["#{Paths.tilde(root)} has a chi project id of its own now: #{id} (was #{found.id || "none: keyed by its path"})"]
132
- places(env: env, state_dir: state_dir || SessionPaths.default_state_dir(env: env)).each do |place|
133
- from = place.path(old_key)
134
- next unless File.directory?(from)
138
+ if conflict
139
+ lines << "did not copy #{Paths.tilde(conflict.folder)} because it is already used by another project"
140
+ else
141
+ project_places = places(env: env, state_dir: state_dir || SessionPaths.default_state_dir(env: env))
142
+ project_places.each do |place|
143
+ from = place.path(old_key)
144
+ next unless File.directory?(from)
135
145
 
136
- to = place.path(id)
137
- copy_folder(File.realpath(from), to)
138
- lines << "copied #{Paths.tilde(from)} to #{Paths.tilde(to)}"
146
+ to = place.path(id)
147
+ copy_folder(File.realpath(from), to)
148
+ lines << "copied #{Paths.tilde(from)} to #{Paths.tilde(to)}"
149
+ end
150
+ memory_folder = project_places.first.path(id)
151
+ if File.directory?(memory_folder)
152
+ AtomicFile.write(File.join(memory_folder, MemoryPaths::CLAIM), "#{root}\n")
153
+ end
139
154
  end
140
155
  write_id(root, id)
141
156
  Outcome.new(kind: :new_id, reason: nil, root: root, top: root, nested_in: nil, message: lines.join("\n"), id: id)
@@ -181,6 +196,8 @@ module Samagotchi
181
196
  "with it when the folder moves or is renamed"]
182
197
  lines << "#{problem}: replaced" if problem
183
198
  list.each { |place| lines.concat(move_folder(place.path(path_key), place.path(id))) }
199
+ memory_folder = list.first.path(id)
200
+ AtomicFile.write(File.join(memory_folder, MemoryPaths::CLAIM), "#{root}\n") if File.directory?(memory_folder)
184
201
  write_id(root, id)
185
202
  if orphans
186
203
  keys = orphans.candidates.map { |candidate| "#{Paths.tilde(candidate)} (#{MemoryOrphans.key_note(candidate)})" }
@@ -131,7 +131,8 @@ module Samagotchi
131
131
  # (#follow_into). Without one (a 0.156 worker, a turn with no id, a
132
132
  # merge from before the update) the merge is inferred
133
133
  # (ReplyWait.merged_elsewhere?) and waited for as the next reply past
134
- # the cursor, as without turn:, never as a turn that will not come.
134
+ # the cursor, as without turn:, never as a turn that will not come
135
+ # (compat shim merge_inference: Compat::SHIMS).
135
136
  def follow_merge(session, merge)
136
137
  return unless may_follow?
137
138
 
@@ -12,6 +12,7 @@ require_relative "worker_presence"
12
12
  require_relative "client_id"
13
13
  require_relative "command_intake"
14
14
  require_relative "config"
15
+ require_relative "continue_intent"
15
16
  require_relative "continue_offer"
16
17
  require_relative "delivery"
17
18
  require_relative "log"
@@ -199,13 +200,13 @@ module Samagotchi
199
200
  end
200
201
 
201
202
  def protected_session_ids
202
- index = Index.new(@state_dir)
203
- entries.flat_map { |entry| [entry.anchor, entry.head, target_of(entry, index).first] }.compact.uniq
203
+ links = links_now
204
+ entries.flat_map { |entry| [entry.anchor, entry.head, target_of(entry, links).first] }.compact.uniq
204
205
  end
205
206
 
206
207
  def protecting(session_id)
207
- index = Index.new(@state_dir)
208
- entries.find { |entry| target_of(entry, index).first == session_id }
208
+ links = links_now
209
+ entries.find { |entry| target_of(entry, links).first == session_id }
209
210
  end
210
211
 
211
212
  # @param id_or_name [String] an id, a unique prefix of one, or a title
@@ -303,11 +304,11 @@ module Samagotchi
303
304
  # its anchor or its head is one of them.
304
305
  # @return [Array<String>]
305
306
  def chain_of(session_id)
306
- index = Index.new(@state_dir)
307
- head = index.latest(session_id)
307
+ links = links_now
308
+ head = links.latest(session_id)
308
309
  ids = [head]
309
310
  id = head
310
- while (previous = index.continues(id)) && !ids.include?(previous)
311
+ while (previous = links.continues(id)) && !ids.include?(previous)
311
312
  ids << previous
312
313
  id = previous
313
314
  end
@@ -348,7 +349,7 @@ module Samagotchi
348
349
  outcomes = []
349
350
  pending = []
350
351
  prune_after = []
351
- index = nil
352
+ links = nil
352
353
 
353
354
  FileUtils.mkdir_p(@store_dir)
354
355
  FileUtils.mkdir_p(@state_dir)
@@ -407,18 +408,19 @@ module Samagotchi
407
408
  next
408
409
  end
409
410
 
410
- # One session listing for the whole tick: a chain walk re-globbing
411
- # every session per call is the tick's dominant cost.
412
- index = Index.new(@state_dir)
411
+ # One session listing for the whole tick (one SessionChain::Links): a
412
+ # chain walk re-globbing every session per call is the tick's
413
+ # dominant cost.
414
+ links = links_now
413
415
  # One firing per chain per tick. A second chain entry of the same chain
414
416
  # would continue the link the first one just made: that link has no
415
- # worker lock yet and is not in this tick's index, so the busy check
417
+ # worker lock yet and is not in this tick's listing, so the busy check
416
418
  # cannot see it, and its prompt would be archived before it ever ran.
417
- fired_chains = resolved_chains.to_h { |entry| [chain_key(index, entry), true] }
418
- pending_chains = in_flight.select { |entry| entry.mode == CHAIN }.map { |entry| chain_key(index, entry) }
419
+ fired_chains = resolved_chains.to_h { |entry| [chain_key(links, entry), true] }
420
+ pending_chains = in_flight.select { |entry| entry.mode == CHAIN }.map { |entry| chain_key(links, entry) }
419
421
  due.each do |entry|
420
422
  position = updated.index { |candidate| candidate.is_a?(Entry) && candidate.id == entry.id }
421
- verdict = check(entry, index)
423
+ verdict = check(entry, links)
422
424
  if verdict
423
425
  kind, reason = verdict
424
426
  unless dry_run
@@ -428,20 +430,20 @@ module Samagotchi
428
430
  detail: reason, entry: entry)
429
431
  next
430
432
  end
431
- if entry.mode == CHAIN && fired_chains[chain_key(index, entry)]
433
+ if entry.mode == CHAIN && fired_chains[chain_key(links, entry)]
432
434
  reason = "the chain just fired; retry at the next tick"
433
435
  updated[position] = skipped(entry, reason) unless dry_run
434
436
  outcomes << Outcome.new(id: entry.id, title: entry.title, status: "skipped", detail: reason, entry: entry)
435
437
  next
436
438
  end
437
- if entry.mode == CHAIN && pending_chains.include?(chain_key(index, entry))
439
+ if entry.mode == CHAIN && pending_chains.include?(chain_key(links, entry))
438
440
  reason = "a schedule turn is still pending in this chain"
439
441
  updated[position] = skipped(entry, reason) unless dry_run
440
442
  outcomes << Outcome.new(id: entry.id, title: entry.title, status: "skipped", detail: reason, entry: entry)
441
443
  next
442
444
  end
443
445
  if dry_run
444
- fired_chains[chain_key(index, entry)] = true if entry.mode == CHAIN
446
+ fired_chains[chain_key(links, entry)] = true if entry.mode == CHAIN
445
447
  outcomes << Outcome.new(id: entry.id, title: entry.title, status: "dry", detail: entry.recurrence_words, entry: entry)
446
448
  next
447
449
  end
@@ -453,18 +455,18 @@ module Samagotchi
453
455
  advanced = advance(entry, now)
454
456
  updated[position] = advanced
455
457
  pending << advanced
456
- fired_chains[chain_key(index, entry)] = true if entry.mode == CHAIN
458
+ fired_chains[chain_key(links, entry)] = true if entry.mode == CHAIN
457
459
  end
458
460
  write(file: file.with(entries: updated, attributes: file.attributes.merge(last_tick_at: now.utc.iso8601))) unless dry_run
459
461
  end
460
462
 
461
463
  pending.each do |entry|
462
- outcome = fire_and_record(entry, index)
464
+ outcome = fire_and_record(entry, links)
463
465
  outcomes << outcome
464
466
  prune_after << outcome.entry if outcome.status == "fired" && entry.mode == CHAIN && entry.keep_links.positive?
465
467
  end
466
- index ||= Index.new(@state_dir) unless prune_after.empty?
467
- prune_after.each { |entry| prune(entry, index) }
468
+ links ||= links_now unless prune_after.empty?
469
+ prune_after.each { |entry| prune(entry, links) }
468
470
  Tick.new(locked: false, outcomes: outcomes)
469
471
  end
470
472
 
@@ -479,7 +481,7 @@ module Samagotchi
479
481
  entry = lookup(id_or_name)
480
482
  return Outcome.new(id: id_or_name.to_s, status: "failed", detail: "no such schedule") if entry.nil?
481
483
 
482
- index = Index.new(@state_dir)
484
+ links = links_now
483
485
  reserved = nil
484
486
  outcome = nil
485
487
  write_under_lock do |file|
@@ -501,7 +503,7 @@ module Samagotchi
501
503
  next
502
504
  end
503
505
 
504
- verdict = check(current, index)
506
+ verdict = check(current, links)
505
507
  if verdict
506
508
  kind, reason = verdict
507
509
  status = kind == :skip ? "skipped" : "failed"
@@ -518,7 +520,7 @@ module Samagotchi
518
520
  return outcome if outcome
519
521
  return Outcome.new(id: entry.id, title: entry.title, status: "failed", detail: "no such schedule") unless reserved
520
522
 
521
- fire_and_record(reserved, index)
523
+ fire_and_record(reserved, links)
522
524
  end
523
525
 
524
526
  # Minutes between the hub's ticks when schedule.auto_tick is on.
@@ -578,50 +580,6 @@ module Samagotchi
578
580
  end
579
581
  end
580
582
 
581
- # The chain a tick walks: the sessions once, with their predecessors,
582
- # their successors and whether they are archived.
583
- class Index
584
- def initialize(state_dir)
585
- @state_dir = state_dir
586
- sessions = Session.list(state_dir: state_dir, include_archived: true, sort: "created_at", order: "asc")
587
- @by_id = sessions.to_h { |session| [session.id, session] }
588
- @archived = @by_id.to_h { |id, _session| [id, ArchiveStore.archived?(Session.session_dir(id, state_dir: state_dir))] }
589
- @nexts = {}
590
- sessions.each { |session| @nexts[session.continues] ||= session.id if session.continues }
591
- end
592
-
593
- def session(id) = @by_id[id]
594
-
595
- def gone?(id) = @by_id[id].nil?
596
-
597
- def archived?(id) = @archived[id]
598
-
599
- attr_reader :state_dir
600
-
601
- # The chain's latest link from +id+ on, in this one listing.
602
- def latest(id)
603
- seen = [id]
604
- while (following = @nexts[seen.last]) && !seen.include?(following)
605
- seen << following
606
- end
607
- seen.last
608
- end
609
-
610
- # A link's predecessor, or nil (a pruned or never-existing one).
611
- def continues(id) = @by_id[id]&.continues
612
-
613
- # A chain's root: +id+ back through `continues` until a link with no
614
- # predecessor. Entries of one chain share this root whichever link each
615
- # of them was added against.
616
- def root(id)
617
- seen = [id]
618
- while (previous = @by_id[seen.last]&.continues) && !seen.include?(previous)
619
- seen << previous
620
- end
621
- seen.last
622
- end
623
- end
624
-
625
583
  # The length of an interval in seconds; nil for something that is not an
626
584
  # interval, and nil for zero so a `0m` schedule can never fire every tick.
627
585
  def self.interval_seconds(interval)
@@ -677,6 +635,11 @@ module Samagotchi
677
635
 
678
636
  private
679
637
 
638
+ # The chains as they are now, over one session listing (SessionChain::Links):
639
+ # one per tick, passed wherever a target is resolved, so the check, the
640
+ # one-firing-per-chain guard and the firing see the same chain.
641
+ def links_now = SessionChain::Links.new(state_dir: @state_dir)
642
+
680
643
  # @param strict [Boolean] a file that is not readable JSON raises rather
681
644
  # than reading as empty: a write starting from an empty list would rewrite
682
645
  # the file and lose every schedule on it. Listing stays lenient.
@@ -942,50 +905,77 @@ module Samagotchi
942
905
  end
943
906
  end
944
907
 
945
- # :skip (busy now, try the next tick) or :fail (the target is gone or
946
- # archived); nil when the entry may fire.
947
- def check(entry, index)
948
- target, reason = target_of(entry, index)
949
- return [:fail, reason] if target.nil?
908
+ # :skip (busy now, or being continued: try the next tick) or :fail (the
909
+ # target is gone or archived); nil when the entry may fire. The
910
+ # continue's skip comes before the busy check: its successor holds no
911
+ # worker lock yet, and the old link's busy signals say nothing.
912
+ def check(entry, links)
913
+ target, reason, kind = target_of(entry, links)
914
+ return [kind || :fail, reason] if target.nil?
950
915
 
951
- busy_reason(entry, index, target)
916
+ busy_reason(entry, links, target)
952
917
  end
953
918
 
954
919
  # The session the entry would act on: its head, followed through every
955
920
  # continue made since it was last written — a chain firing's new link or a
956
- # human `chi send --continues`/`chi resume` (SessionChain.latest, the same
957
- # walk SessionContinue.run does for `last:`). Both modes use the walk: a
921
+ # person's `chi send --new --continues` or web `continue →` (SessionChain::Links#latest,
922
+ # the walk SessionContinue.run does for `last:`). The chain's end, not the
923
+ # send rule's first live link (Links#first_live): a chain continues only
924
+ # its end, and a schedule never un-archives a retired chain. Both modes use the walk: a
958
925
  # chain continues that link, a same-mode entry queues a turn in it. The
959
926
  # anchor alone is not enough for either: the first continue archives it, so
960
927
  # a same-mode entry read literally would fail "archived" from then on, and
961
928
  # the chain would continue a link that is no longer the end. The anchor is
962
929
  # the fallback when the head is gone (pruned or deleted by hand): the walk
963
- # from it still finds the chain's end.
964
- def target_of(entry, index)
965
- head = index.latest(index.gone?(entry.head) ? entry.anchor : entry.head)
966
- if index.gone?(head)
930
+ # from it still finds the chain's end. An archived end fails (a retired
931
+ # chain stops its schedules), unless a continue of it is in flight
932
+ # (#continuing_reason): then it is a skip, the third member :skip.
933
+ # @return [Array] [target, nil] or [nil, reason] or [nil, reason, :skip]
934
+ def target_of(entry, links)
935
+ head = links.latest(links.gone?(entry.head) ? entry.anchor : entry.head)
936
+ if links.gone?(head)
967
937
  return [nil, entry.mode == CHAIN ? "the chain is gone" : "the session it was made against is gone"]
968
938
  end
969
939
 
970
- return [nil, "archived"] if index.archived?(head)
940
+ if links.archived?(head)
941
+ continuing = continuing_reason(head)
942
+ return continuing ? [nil, continuing, :skip] : [nil, "archived"]
943
+ end
971
944
 
972
945
  [head, nil]
973
946
  end
974
947
 
948
+ # Why an archived +link+ is "not now": a continue of it in flight (a
949
+ # person's `chi send --new --continues` or web `continue →`; ContinueIntent.in_flight:
950
+ # the intent written, the lock held) archived it for a successor this
951
+ # listing doesn't have yet; the next tick's walk reaches it. Else nil. A
952
+ # stale intent (no continue holds the lock) is nil: archived it stays.
953
+ def continuing_reason(link)
954
+ successor = ContinueIntent.in_flight(link, state_dir: @state_dir)
955
+ successor && "being continued as #{successor[0, 8]}; retry at the next tick"
956
+ end
957
+
975
958
  # Which chain an entry belongs to, for the one-firing-per-chain-per-tick
976
959
  # guard: the root of the chain its resolved target is in. Every entry of a
977
- # chain has the same root, whichever link it was added against.
978
- def chain_key(index, entry)
979
- target, _reason = target_of(entry, index)
980
- index.root(target)
960
+ # chain has the same root, whichever link it was added against. One whose
961
+ # target doesn't resolve (gone, archived, being continued) gets a key of
962
+ # its own: it shares no chain with anyone this tick.
963
+ def chain_key(links, entry)
964
+ target, _reason = target_of(entry, links)
965
+ target ? links.root(target) : "unresolved:#{entry.id}"
981
966
  end
982
967
 
983
968
  # Two paths (FR7): a live worker is asked through its Bridge; with no
984
- # worker the files say what the tick can know.
985
- def busy_reason(entry, index, target)
969
+ # worker the files say what the tick can know. A session its owner
970
+ # stopped is a skip first, in both modes: a schedule restarts nothing
971
+ # the owner stopped (deliver_turn would refuse a same-mode firing too,
972
+ # SessionManager::DeliverResult::STOPPED, in the same words).
973
+ def busy_reason(entry, links, target)
986
974
  dir = Session.session_dir(target, state_dir: @state_dir)
987
975
  owner = SessionManager.session_owner(target, state_dir: @state_dir)
988
976
  presence = WorkerPresence.read(target, state_dir: @state_dir, owner: owner, link: :look)
977
+ return [:skip, SessionManager::DeliverResult::STOPPED["detail"]] if presence.stop_marked?
978
+
989
979
  link = presence.link
990
980
 
991
981
  if presence.worker_owned?
@@ -1001,7 +991,7 @@ module Samagotchi
1001
991
  # queues a turn, so for it the files are enough.
1002
992
  return [:skip, "its worker is too old to ask: chi sessions restart #{target[0, 8]}"] if entry.mode == CHAIN
1003
993
 
1004
- return file_busy_reason(index.session(target), dir)
994
+ return file_busy_reason(links.session(target), dir)
1005
995
  end
1006
996
  return [:skip, "its worker is not answering"] if activity.nil?
1007
997
 
@@ -1022,7 +1012,7 @@ module Samagotchi
1022
1012
  # A queued turn is fine: the starting worker takes it.
1023
1013
  return [:skip, "its worker is starting or leaving"] if entry.mode == CHAIN && presence.starting?
1024
1014
 
1025
- file_busy_reason(index.session(target), dir)
1015
+ file_busy_reason(links.session(target), dir)
1026
1016
  end
1027
1017
 
1028
1018
  # The worker's activity snapshot, :old when it has no /activity route, or
@@ -1056,14 +1046,20 @@ module Samagotchi
1056
1046
  nil
1057
1047
  end
1058
1048
 
1059
- def fire_and_record(entry, index)
1060
- outcome = fire(entry, index)
1049
+ # A firing whose target no longer resolves (#target_of: gone, archived,
1050
+ # or being continued since the tick's check): what #check would say.
1051
+ def unresolved(entry, reason, kind)
1052
+ Outcome.new(id: entry.id, title: entry.title, status: kind == :skip ? "skipped" : "failed", detail: reason, entry: entry)
1053
+ end
1054
+
1055
+ def fire_and_record(entry, links)
1056
+ outcome = fire(entry, links)
1061
1057
  record(entry, outcome)
1062
1058
  outcome
1063
1059
  end
1064
1060
 
1065
- def fire(entry, index)
1066
- entry.mode == CHAIN ? fire_chain(entry, index) : fire_same(entry, index)
1061
+ def fire(entry, links)
1062
+ entry.mode == CHAIN ? fire_chain(entry, links) : fire_same(entry, links)
1067
1063
  rescue StandardError => e
1068
1064
  # run_locked re-raises whatever spawn_continuation throws after undoing
1069
1065
  # the move: a spawn failure or an ENOSPC is a failure, not a skip.
@@ -1071,9 +1067,9 @@ module Samagotchi
1071
1067
  Outcome.new(id: entry.id, title: entry.title, status: "failed", detail: "#{e.class}: #{e.message}", entry: entry)
1072
1068
  end
1073
1069
 
1074
- def fire_chain(entry, index)
1075
- head, _reason = target_of(entry, index)
1076
- return Outcome.new(id: entry.id, title: entry.title, status: "failed", detail: "the chain is gone", entry: entry) if head.nil?
1070
+ def fire_chain(entry, links)
1071
+ head, reason, kind = target_of(entry, links)
1072
+ return unresolved(entry, reason, kind) if head.nil?
1077
1073
  # One stored before #add refused it: its link would refuse the line,
1078
1074
  # so it fails here, with no link started and nothing archived.
1079
1075
  if CommandIntake.shell_line?(entry.prompt)
@@ -1099,14 +1095,23 @@ module Samagotchi
1099
1095
  classify(entry, e.reason, e.message)
1100
1096
  end
1101
1097
 
1102
- def fire_same(entry, index)
1103
- target, reason = target_of(entry, index)
1104
- return Outcome.new(id: entry.id, title: entry.title, status: "failed", detail: reason, entry: entry) if target.nil?
1098
+ def fire_same(entry, links, walked_again: false)
1099
+ target, reason, kind = target_of(entry, links)
1100
+ return unresolved(entry, reason, kind) if target.nil?
1105
1101
 
1106
1102
  # Checked again, against the files: a continue made between the tick's
1107
1103
  # listing and this moment archives the target it just resolved to.
1104
+ # One in flight is a skip; one that ended since saved its successor
1105
+ # before it let its intent go, so a fresh listing (one walk again,
1106
+ # checked as the tick checks) finds where the chain went.
1108
1107
  dir = Session.session_dir(target, state_dir: @state_dir)
1109
- return Outcome.new(id: entry.id, title: entry.title, status: "failed", detail: "archived", entry: entry) if ArchiveStore.archived?(dir)
1108
+ if ArchiveStore.archived?(dir)
1109
+ continuing = continuing_reason(target)
1110
+ return fire_same_again(entry) if continuing.nil? && !walked_again
1111
+
1112
+ return Outcome.new(id: entry.id, title: entry.title, status: continuing ? "skipped" : "failed",
1113
+ detail: continuing || "archived", entry: entry)
1114
+ end
1110
1115
  # One stored before #add refused it: the Bridge would refuse it, but a
1111
1116
  # delivery that falls back to the input file is accepted.
1112
1117
  if CommandIntake.shell_line?(entry.prompt)
@@ -1126,12 +1131,25 @@ module Samagotchi
1126
1131
  # one fired into, so the walk survives a link deleted in between.
1127
1132
  Outcome.new(id: entry.id, title: entry.title, status: "pending", detail: "queued a turn",
1128
1133
  entry: entry.with(head: target, pending_session_id: target, pending_turn_id: result.enqueued_id))
1134
+ elsif result.wake_refused?
1135
+ # Stopped (or archived) since the tick's check: a skip, as the check's.
1136
+ Outcome.new(id: entry.id, title: entry.title, status: "skipped", detail: result.detail, entry: entry)
1129
1137
  else
1130
1138
  Outcome.new(id: entry.id, title: entry.title, status: "failed",
1131
1139
  detail: "the turn was not accepted (#{result.status}: #{result.error || result.detail})", entry: entry)
1132
1140
  end
1133
1141
  end
1134
1142
 
1143
+ # #fire_same over a fresh listing: its target and busy checks as the
1144
+ # tick's (#check), then the firing; an end still archived fails.
1145
+ def fire_same_again(entry)
1146
+ links = links_now
1147
+ verdict = check(entry, links)
1148
+ return unresolved(entry, verdict[1], verdict[0]) if verdict
1149
+
1150
+ fire_same(entry, links, walked_again: true)
1151
+ end
1152
+
1135
1153
  def classify(entry, reason, message)
1136
1154
  status = SKIP_REASONS.include?(reason.to_s) ? "skipped" : "failed"
1137
1155
  Outcome.new(id: entry.id, title: entry.title, status: status, detail: message, entry: entry)
@@ -1188,13 +1206,13 @@ module Samagotchi
1188
1206
  # still unarchived: nothing is deleted in that case, and the next firing
1189
1207
  # retries (a schedule failure it is not). The anchor is never pruned:
1190
1208
  # `last:<anchor>` must keep resolving, and it is the id the user remembers.
1191
- def prune(entry, index)
1192
- links = []
1209
+ def prune(entry, links)
1210
+ chain = []
1193
1211
  id = entry.head
1194
1212
  seen = []
1195
1213
  while id && !seen.include?(id)
1196
1214
  seen << id
1197
- links << id
1215
+ chain << id
1198
1216
  # The walk stops at the anchor: the links before it are the user's own
1199
1217
  # history, made before this schedule existed, and the anchor itself is
1200
1218
  # the id `last:<anchor>` and the user remember.
@@ -1202,10 +1220,10 @@ module Samagotchi
1202
1220
 
1203
1221
  # The link a firing just made is not in the tick's listing; every
1204
1222
  # older one is.
1205
- session = index.session(id) || (File.exist?(Session.session_file(id, state_dir: @state_dir)) ? Session.load(id, state_dir: @state_dir) : nil)
1223
+ session = links.session(id) || (File.exist?(Session.session_file(id, state_dir: @state_dir)) ? Session.load(id, state_dir: @state_dir) : nil)
1206
1224
  id = session&.continues
1207
1225
  end
1208
- older = links[entry.keep_links..] || []
1226
+ older = chain[entry.keep_links..] || []
1209
1227
  return if older.empty?
1210
1228
 
1211
1229
  older.reverse_each do |link|