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
@@ -9,13 +9,15 @@ module Samagotchi
9
9
  # What a session's page says about the schedules waiting on it (chi web's
10
10
  # notice strip under the session bar, the card's ⏸ schedule badge):
11
11
  # .session_marks, every enabled schedule by its resolved target
12
- # (#target_of, as the tick and #waiting_on resolve it), which the session
13
- # hub caches; .shown_marks, the ones the page shows for one session,
14
- # classified so the page never matches words.
12
+ # (ProtectionPart#waiting_by_target, the reader the Stop sheet's
13
+ # #waiting_on reads too), which the session hub caches; .shown_marks,
14
+ # the ones the page shows for one session, classified so the page never
15
+ # matches words. A skip is classified by its row's code
16
+ # (HistoryPart), by its words only for a row an older chi wrote.
15
17
  module MarksPart
16
18
  # A mark's kinds: the session is stopped (the page's own sentence), a
17
- # chain skipped for an open terminal or tab (BusyPart::OPEN_TAB_WORDS),
18
- # or another skip, its words as recorded.
19
+ # chain skipped for an open terminal or tab (BusyPart::OPEN_TAB), or
20
+ # another skip, its words as recorded.
19
21
  KINDS = %w[stopped open_tab other].freeze
20
22
  # The history rows that are outcomes of a firing or of the tick that
21
23
  # fires it (HistoryPart writes them; the owner's enabled, disabled and
@@ -27,10 +29,13 @@ module Samagotchi
27
29
 
28
30
  module ClassMethods
29
31
  # The session hub's read: one lenient, quiet read of the store and one
30
- # session listing (none when no schedule is enabled). It never
32
+ # session listing (none when no schedule is enabled), or the hub's
33
+ # own rows given (+sessions+: no listing, no archive read). It never
31
34
  # raises: a store that doesn't read is no marks and no log line (as
32
35
  # .waiting_on), a chain walk that fails one warn line and no marks.
33
36
  # @param state_dir [String, nil] the sessions' directory
37
+ # @param sessions [Array<SessionChain::Links::Row>, nil] every session
38
+ # (archived ones too) as the caller holds them; nil lists the dir
34
39
  # @return [Hash{String => Array<Hash>}] session id → its marks, the
35
40
  # earliest due first: {id:, title:, skips:, max_skips:, reason:
36
41
  # (the newest skipped row's words while skips > 0, else nil),
@@ -39,10 +44,11 @@ module Samagotchi
39
44
  # row (OUTCOME_STATUSES) is a skip — or it has none yet — 0 after
40
45
  # a failure, a fire or a miss, which supersede them (the counters
41
46
  # go on counting for max_skips). A schedule whose firing is in
42
- # flight (ScheduleDisplay's "firing") is running, not waiting:
43
- # none, though its skips reset only when it settles.
44
- def session_marks(state_dir: nil)
45
- new(state_dir: state_dir).session_marks
47
+ # flight (ScheduleDisplay's "firing") is marked as the Stop sheet
48
+ # names it (#waiting_on), with skips 0: it is running, so the page
49
+ # shows it only while its session is stopped.
50
+ def session_marks(state_dir: nil, sessions: nil)
51
+ new(state_dir: state_dir).session_marks(sessions: sessions)
46
52
  rescue StandardError => e
47
53
  Log.warn(:worker, "schedule_marks_unreadable", error: e.class.name, msg: e.message)
48
54
  {}
@@ -53,7 +59,9 @@ module Samagotchi
53
59
  # is a skip, but not for a stop that is over (the next tick runs it).
54
60
  # Without the volatile fields, so a summary built twice compares equal.
55
61
  # @param marks [Array<Hash>, nil] .session_marks' for the session
56
- # @param stopped [Boolean] the session's status is stopped
62
+ # @param stopped [Boolean] every firing on the session skips as
63
+ # stopped now (BusyPart.stop_holds?: the stop marker, read fresh by
64
+ # the caller, Web::SessionSummary)
57
65
  # @return [Array<Hash>] {id:, title:, skips:, max_skips:, kind:
58
66
  # (KINDS), reason: (kind "other" only, else nil)}
59
67
  def shown_marks(marks, stopped:)
@@ -70,16 +78,12 @@ module Samagotchi
70
78
  end
71
79
  end
72
80
 
81
+ # @param sessions [Array<SessionChain::Links::Row>, nil] .session_marks'
73
82
  # @return [Hash{String => Array<Hash>}] .session_marks' answer
74
- def session_marks
75
- list = entries(quiet: true).reject { |entry| entry.firing_since || entry.pending_session_id }
76
- return {} if list.empty?
77
-
78
- links = links_now
79
- list.each_with_object({}) do |entry, marks|
80
- target = target_of(entry, links).first
81
- (marks[target] ||= []) << mark_of(entry) if target
82
- end
83
+ def session_marks(sessions: nil)
84
+ list = entries(quiet: true)
85
+ links = sessions && !list.empty? ? links_now(sessions: sessions) : nil
86
+ waiting_by_target(list, links: links).transform_values { |waiting| waiting.map { |entry| mark_of(entry) } }
83
87
  end
84
88
 
85
89
  private
@@ -90,12 +94,31 @@ module Samagotchi
90
94
  # stands as the skips it is, a failure, a fire or a miss supersedes
91
95
  # them (0 the page shows; the counters go on counting for
92
96
  # max_skips). An entry with no outcome row yet never attempted a
93
- # firing: its skips stand.
97
+ # firing: its skips stand. A firing in flight is running: 0.
94
98
  outcome = newest_outcome(rows)
95
- skips = outcome.nil? || outcome == "skipped" ? entry.skips.to_i : 0
96
- reason = skips.positive? ? newest_skip_words(rows) : nil
97
- { id: entry.id, title: entry.title, skips: skips, max_skips: entry.max_skips, reason: reason,
98
- open_tab: reason == BusyPart::OPEN_TAB_WORDS, stopped_skip: reason == BusyPart::BUSY_WORDS[:stopped] }
99
+ in_flight = entry.firing_since || entry.pending_session_id
100
+ skips = !in_flight && (outcome.nil? || outcome == "skipped") ? entry.skips.to_i : 0
101
+ skip = skips.positive? ? newest_skip(rows) : nil
102
+ { id: entry.id, title: entry.title, skips: skips, max_skips: entry.max_skips, reason: skip&.dig(:detail),
103
+ open_tab: skip_kind(skip) == :open_tab, stopped_skip: skip_kind(skip) == :stopped }
104
+ end
105
+
106
+ # What a skipped row was for: :stopped, :open_tab or :other, by its
107
+ # code (BusyPart::STOP_CODES, BusyPart::OPEN_TAB); a row an older chi
108
+ # wrote without one by its words (BUSY_WORDS[:stopped], OPEN_TAB_WORDS).
109
+ # nil for no row.
110
+ def skip_kind(row)
111
+ return nil if row.nil?
112
+
113
+ code = row[:code]
114
+ if code
115
+ return :stopped if BusyPart::STOP_CODES.include?(code)
116
+
117
+ return code == BusyPart::OPEN_TAB ? :open_tab : :other
118
+ end
119
+ return :stopped if row[:detail] == BusyPart::BUSY_WORDS[:stopped]
120
+
121
+ row[:detail] == BusyPart::OPEN_TAB_WORDS ? :open_tab : :other
99
122
  end
100
123
 
101
124
  # The newest outcome row's status (OUTCOME_STATUSES), the owner's rows
@@ -105,10 +128,10 @@ module Samagotchi
105
128
  rows.reverse.find { |row| OUTCOME_STATUSES.include?(row[:status]) }&.dig(:status)
106
129
  end
107
130
 
108
- # The newest skipped row's words (the newest row may be an edit or a
109
- # missed occurrence).
110
- def newest_skip_words(rows)
111
- rows.reverse.find { |row| row[:status] == "skipped" }&.dig(:detail)
131
+ # The newest skipped row (the newest row may be an edit or a missed
132
+ # occurrence).
133
+ def newest_skip(rows)
134
+ rows.reverse.find { |row| row[:status] == "skipped" }
112
135
  end
113
136
  end
114
137
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "fileutils"
4
4
  require "time"
5
+ require_relative "history_part"
6
+ require_relative "marks_part"
5
7
 
6
8
  module Samagotchi
7
9
  class Schedules
@@ -11,6 +13,14 @@ module Samagotchi
11
13
  # #skipped / #failed, whatever ran them (a cron tick, a run-now, chi
12
14
  # web's own tick); claimed by SessionHub#publish_disabled.
13
15
  module NoticesPart
16
+ # The newest outcome row of +entry+ is a failure HistoryPart::CONFIG_FAILURES names
17
+ # (ScheduleDisplay.off_reason: an entry it turned off is off for its
18
+ # failures, whatever the count).
19
+ def self.config_failed?(entry)
20
+ row = HistoryPart.rows(entry.history).reverse.find { |run| MarksPart::OUTCOME_STATUSES.include?(run[:status]) }
21
+ row && row[:status] == "failed" && HistoryPart::CONFIG_FAILURES.include?(row[:code]) ? true : false
22
+ end
23
+
14
24
  # The disabled notices whatever ticker set them (#skipped / #failed: a
15
25
  # cron `chi schedule tick`, a `run-now`, chi web's own tick), each cleared
16
26
  # here and returned once, for chi web's hub to push (SessionHub
@@ -64,6 +74,20 @@ module Samagotchi
64
74
 
65
75
  private
66
76
 
77
+ # A failed firing's words when its turn failed for its configuration
78
+ # (HistoryPart::CONFIG_FAILURES): which host, which model, and the fix; nil for any
79
+ # other outcome (TurnOutcome#detail's words then).
80
+ def config_failure_words(result, session)
81
+ host = result.turn&.error_host.to_s
82
+ host = "its host" if host.empty?
83
+ case result.code
84
+ when "model_not_served"
85
+ "host #{host} doesn't serve #{session.model_name}: /model #{host}:served or edit the session's model"
86
+ when "auth"
87
+ "host #{host} refused the key for #{session.model_name}: fix the key, then enable"
88
+ end
89
+ end
90
+
67
91
  # The skips and failures that turned off a recurring entry that was on when
68
92
  # the pass began (+before+), as the file has it now: one diff covers every
69
93
  # path that records one. A `date` one-off is spent, not disabled; an entry
@@ -93,20 +117,23 @@ module Samagotchi
93
117
  # at its budget. A `date` one-off is spent, not disabled; an entry off
94
118
  # within budget is an owner's disable. #skipped / #failed set the
95
119
  # disabled notice on it, #mark_disabled the CLI's `(disabled)`.
96
- def budget_turned_off?(before, after, status)
120
+ # A failure HistoryPart::CONFIG_FAILURES names (+code+: the failure's; by default
121
+ # +after+'s newest row's, #mark_disabled reading it back) turns it off
122
+ # whatever the count.
123
+ def budget_turned_off?(before, after, status, code = HistoryPart.rows(after.history).last&.dig(:code))
97
124
  return false unless before.enabled && before.recurrence[:kind] != "date" && !after.enabled
98
125
 
99
126
  case status
100
127
  when "skipped" then after.skips >= after.max_skips
101
- when "failed" then after.failures >= after.max_failures
128
+ when "failed" then after.failures >= after.max_failures || HistoryPart::CONFIG_FAILURES.include?(code)
102
129
  else false
103
130
  end
104
131
  end
105
132
 
106
133
  # The notice a chi web pushes for a budget turn-off (#claim_disabled_notices),
107
134
  # when this +status+ call turned +before+ off; +after+ unchanged otherwise.
108
- def with_disabled_notice(before, after, status, reason, now)
109
- return after unless budget_turned_off?(before, after, status)
135
+ def with_disabled_notice(before, after, status, reason, now, code = nil)
136
+ return after unless budget_turned_off?(before, after, status, code)
110
137
 
111
138
  count = status == "skipped" ? after.skips : after.failures
112
139
  after.with(disabled_notice: { kind: status, count: count, reason: reason, at: now.utc.iso8601 })
@@ -16,10 +16,23 @@ module Samagotchi
16
16
  # their own rules; chi schedule cancel ID drops such an entry,
17
17
  # #cancel_unreadable). A missing file holds nothing.
18
18
  #
19
- # Its lenient sibling, #waiting_on: the schedules a stop of a session
20
- # leaves waiting (the same target predicate, #targeting, over the
21
- # lenient read), which a stop only says and never refuses on.
19
+ # Its lenient sibling, #waiting: the schedules waiting on a session
20
+ # (the same target predicate, #targeting, over the lenient read) and
21
+ # whether it is stopped (BusyPart.stop_holds?), the one reader of
22
+ # "is a schedule waiting here, and why": the Stop sheet's #waiting_on,
23
+ # #stopped_target (add, enable, schedule_task) and the page's marks
24
+ # (MarksPart.session_marks, through #waiting_by_target) all read it. A
25
+ # firing in flight is waiting too: the Stop sheet and the page's bar
26
+ # both name it.
22
27
  module ProtectionPart
28
+ # The schedules waiting on one session: +entries+ the enabled ones
29
+ # whose resolved target it is, the earliest due first, a firing in
30
+ # flight among them; +stopped+ every firing on it skips as stopped
31
+ # (BusyPart.stop_holds?).
32
+ Waiting = Data.define(:session_id, :entries, :stopped) do
33
+ def stopped? = stopped
34
+ end
35
+
23
36
  def self.included(base) = base.extend(ClassMethods)
24
37
 
25
38
  module ClassMethods
@@ -44,11 +57,18 @@ module Samagotchi
44
57
  # tap logs nothing about the store); no enabled schedule lists no
45
58
  # session.
46
59
  # @return [Array<Entry>]
47
- def waiting_on(session_id) = targeting(session_id, entries(quiet: true))
60
+ def waiting_on(session_id) = waiting(session_id).entries
61
+
62
+ # The schedules waiting on +session_id+ and whether it is stopped
63
+ # (Waiting), from a lenient, quiet read.
64
+ # @return [Waiting]
65
+ def waiting(session_id)
66
+ Waiting.new(session_id: session_id, entries: targeting(session_id, entries(quiet: true)), stopped: stopped?(session_id))
67
+ end
48
68
 
49
69
  # The session the entry would act on when it is stopped now: the same
50
- # resolution the tick uses (#target_of), read from the marker
51
- # (WorkerPresence#stop_marked?: a stop is a marker, not the saved status).
70
+ # resolution the tick uses (#target_of), stopped as the tick reads it
71
+ # (#stopped?: the marker, not the saved status, BusyPart.stop_holds?).
52
72
  # nil when the entry does not resolve, the target is not stopped, or the
53
73
  # walk or the presence cannot be read: this is a line a caller prints,
54
74
  # never a check, so it never raises.
@@ -56,11 +76,7 @@ module Samagotchi
56
76
  def stopped_target(entry)
57
77
  Degrade.call(:worker, "schedules_stopped_target_unreadable") do
58
78
  target = target_of(entry, links_now).first
59
- if target.nil?
60
- nil
61
- else
62
- WorkerPresence.read(target, state_dir: @state_dir).stop_marked? ? target : nil
63
- end
79
+ target && stopped?(target) ? target : nil
64
80
  end
65
81
  end
66
82
 
@@ -82,6 +98,24 @@ module Samagotchi
82
98
  entry && SessionProtection::Hold.new(session_id: session_id, reason: :scheduled, ref: entry.id)
83
99
  end
84
100
 
101
+ # #hold of each of +ids+, over one strict read of the file and, only
102
+ # when some id is no schedule's anchor or head, one session listing (a
103
+ # Links given this file's own anchor heads): a batch delete's check.
104
+ # Fail-closed as #hold: a store that doesn't read raises for the whole
105
+ # batch. No ids, no read.
106
+ # @return [Hash{String => SessionProtection::Hold}] the held ids only
107
+ def holds(ids)
108
+ return {} if ids.empty?
109
+
110
+ list = protection_entries
111
+ by_target = nil
112
+ ids.each_with_object({}) do |sid, held|
113
+ entry = list.find { |candidate| candidate.anchor == sid || candidate.head == sid }
114
+ entry ||= (by_target ||= waiting_by_target(list, links: batch_links(list)))[sid]&.first
115
+ held[sid] = SessionProtection::Hold.new(session_id: sid, reason: :scheduled, ref: entry.id) if entry
116
+ end
117
+ end
118
+
85
119
  # The first enabled schedule on the chain +session_id+ belongs to (as
86
120
  # #for_chain: its anchor or head a link of it), which a chain's delete
87
121
  # would take.
@@ -106,6 +140,30 @@ module Samagotchi
106
140
 
107
141
  def protecting(session_id, list) = targeting(session_id, list).first
108
142
 
143
+ # Each target of +list+ → its entries (in +list+'s order), over one
144
+ # session listing (none when +list+ is empty); an entry that doesn't
145
+ # resolve is in none. #targeting's predicate, for every session at
146
+ # once (MarksPart.session_marks, #holds).
147
+ # @return [Hash{String => Array<Entry>}]
148
+ def waiting_by_target(list, links: nil)
149
+ return {} if list.empty?
150
+
151
+ links ||= links_now
152
+ list.each_with_object({}) do |entry, by_target|
153
+ target = target_of(entry, links).first
154
+ (by_target[target] ||= []) << entry if target
155
+ end
156
+ end
157
+
158
+ # #holds' one walker: none built for an empty +list+, and given the
159
+ # file's anchor heads (#anchor_heads, a lenient read, disabled entries
160
+ # too, as Links would read them) so it builds no second Schedules.
161
+ def batch_links(list)
162
+ return nil if list.empty?
163
+
164
+ SessionChain::Links.new(state_dir: @state_dir, anchor_heads: anchor_heads)
165
+ end
166
+
109
167
  # The entries of +list+ whose resolved target is +session_id+, over one
110
168
  # session listing (none when +list+ is empty).
111
169
  def targeting(session_id, list)
@@ -143,12 +143,13 @@ module Samagotchi
143
143
  return "it is not archived" unless ArchiveStore.archived?(Session.session_dir(link, state_dir: @state_dir))
144
144
 
145
145
  tree = SessionManager.delete_tree_ids(link, @state_dir)
146
+ protection = SessionManager.protection_check(tree, @state_dir)
146
147
  tree.reverse_each do |sid|
147
- SessionManager.refuse_protected!(sid, @state_dir)
148
+ protection.call(sid)
148
149
  rescue SessionManager::DeleteRefused => e
149
150
  return sid == link ? e.message : "its delegate #{sid[0, 8]}: #{e.message}"
150
151
  end
151
- owned = tree.select { |sid| SessionManager.session_owner(sid, state_dir: @state_dir) }
152
+ owned = tree.select { |sid| OwnerLock.for_session(sid, state_dir: @state_dir) }
152
153
  return "a worker runs in it: #{owned.map { |sid| sid[0, 8] }.join(", ")}" unless owned.empty?
153
154
 
154
155
  open = tree[0...-1].reject { |sid| ArchiveStore.archived?(Session.session_dir(sid, state_dir: @state_dir)) }
@@ -99,9 +99,9 @@ module Samagotchi
99
99
  # firing just continued the chain.)
100
100
  # (transition_busy: a continue of the chain's end held its transition
101
101
  # lock past SessionContinue::LOCK_WAIT, or a same-mode turn met a
102
- # transition holding its target's lock: DeliverResult::BUSY.)
102
+ # transition holding its target's lock: DeliverResult::BUSY; undo_pending: a crashed continue's undo failed.)
103
103
  SKIP_REASONS = %w[busy queued offer busy_child queued_child offer_child starting starting_child continued
104
- open_children transition_busy].freeze
104
+ open_children transition_busy undo_pending].freeze
105
105
 
106
106
  # The add itself is wrong (no prompt, two recurrence kinds, a scratch
107
107
  # target): a usage error, exit 2.
@@ -206,10 +206,14 @@ module Samagotchi
206
206
 
207
207
  # What one entry did in a tick: "fired", "skipped", "missed", "failed",
208
208
  # "not-needed" (its condition said no: ConditionPart) or "dry" (a
209
- # --dry-run's "would fire"). +detail+ is the reason. +disabled+:
210
- # this skip or failure used up a recurring entry's budget (#mark_disabled).
211
- Outcome = Data.define(:id, :title, :status, :detail, :entry, :disabled, :session_id) do
212
- def initialize(id:, status:, title: nil, detail: nil, entry: nil, disabled: false, session_id: nil) = super
209
+ # --dry-run's "would fire"). +detail+ is the reason, words for display;
210
+ # +code+ the reason as a code where one is known (HistoryPart: a busy
211
+ # cell, open_tab, a wake refusal's error, a refusal's reason, a failed
212
+ # turn's error kind), which the history row keeps beside the words.
213
+ # +disabled+: this skip or failure used up a recurring entry's budget
214
+ # (#mark_disabled).
215
+ Outcome = Data.define(:id, :title, :status, :detail, :entry, :disabled, :session_id, :code) do
216
+ def initialize(id:, status:, title: nil, detail: nil, entry: nil, disabled: false, session_id: nil, code: nil) = super
213
217
  end
214
218
 
215
219
  # A whole tick.
@@ -464,12 +468,10 @@ module Samagotchi
464
468
  end
465
469
  verdict = check(entry, links)
466
470
  if verdict
467
- kind, reason = verdict
468
- unless dry_run
469
- updated[position] = kind == :skip ? skipped(entry, reason, now: now) : failed(entry, reason, now: now)
470
- end
471
+ kind, reason, code = verdict
472
+ updated[position] = skipped_or_failed(entry, kind, reason, now, code) unless dry_run
471
473
  outcomes << Outcome.new(id: entry.id, title: entry.title, status: kind == :skip ? "skipped" : "failed",
472
- detail: reason, entry: entry)
474
+ detail: reason, entry: entry, code: code)
473
475
  next
474
476
  end
475
477
  if entry.mode == CHAIN && fired_chains[chain_key(links, entry)]
@@ -550,12 +552,12 @@ module Samagotchi
550
552
 
551
553
  verdict = check(current, links)
552
554
  if verdict
553
- kind, reason = verdict
555
+ kind, reason, code = verdict
554
556
  status = kind == :skip ? "skipped" : "failed"
555
- updated = status == "skipped" ? skipped(current, reason, now: now) : failed(current, reason, now: now) if current.enabled
556
- updated ||= with_history(current.with(last_result: reason), status, reason, at: now, session_id: current.head)
557
+ updated = skipped_or_failed(current, kind, reason, now, code) if current.enabled
558
+ updated ||= with_history(current.with(last_result: reason), status, reason, at: now, session_id: current.head, code: code)
557
559
  file.entries[position] = updated
558
- outcome = Outcome.new(id: current.id, title: current.title, status: status, detail: reason, entry: updated)
560
+ outcome = Outcome.new(id: current.id, title: current.title, status: status, detail: reason, entry: updated, code: code)
559
561
  next
560
562
  end
561
563
 
@@ -579,8 +581,8 @@ module Samagotchi
579
581
 
580
582
  # The chains as they are now, over one session listing (SessionChain::Links):
581
583
  # one per tick, passed wherever a target is resolved, so the check, the
582
- # one-firing-per-chain guard and the firing see the same chain.
583
- def links_now = SessionChain::Links.new(state_dir: @state_dir)
584
+ # one-firing-per-chain guard and the firing see the same chain (+sessions+: MarksPart's rows).
585
+ def links_now(sessions: nil) = SessionChain::Links.new(state_dir: @state_dir, sessions: sessions)
584
586
 
585
587
  # #anchor_heads of +entries+ (the readable ones), for PrunePart#cancel_strands too.
586
588
  def heads_by_anchor(entries)
@@ -786,24 +788,6 @@ module Samagotchi
786
788
  enabled: one_off ? false : entry.enabled)
787
789
  end
788
790
 
789
- def skipped(entry, reason, now: @clock.call)
790
- skips = entry.skips + 1
791
- within_budget = skips < entry.max_skips
792
- updated = entry.with(skips: skips, firing_since: nil, firing_token: nil, pending_session_id: nil, pending_turn_id: nil,
793
- last_result: within_budget ? "skipped" : "disabled: #{reason}",
794
- enabled: within_budget && (entry.enabled || entry.recurrence[:kind] == "date"))
795
- recorded(entry, updated, "skipped", reason, now)
796
- end
797
-
798
- def failed(entry, reason, now: @clock.call)
799
- failures = entry.failures + 1
800
- within_budget = failures < entry.max_failures
801
- updated = entry.with(failures: failures, firing_since: nil, firing_token: nil, pending_session_id: nil,
802
- pending_turn_id: nil, last_result: reason,
803
- enabled: within_budget && (entry.enabled || entry.recurrence[:kind] == "date"))
804
- recorded(entry, updated, "failed", reason, now)
805
- end
806
-
807
791
  def pending_detail(entry)
808
792
  session = Session.load(entry.pending_session_id, state_dir: @state_dir)
809
793
  question = session.pending_question
@@ -834,7 +818,7 @@ module Samagotchi
834
818
 
835
819
  if result.turn
836
820
  return Outcome.new(id: entry.id, title: entry.title, status: result.completed? ? "fired" : "failed",
837
- detail: result.detail, entry: entry)
821
+ detail: config_failure_words(result, session) || result.detail, entry: entry, code: result.code)
838
822
  end
839
823
 
840
824
  return Outcome.new(id: entry.id, title: entry.title, status: "failed", detail: "no outcome", entry: entry) if
@@ -895,13 +879,14 @@ module Samagotchi
895
879
  last_result: outcome.detail, failures: 0, skips: 0)
896
880
  with_history(settled, "fired", outcome.detail, at: now, session_id: entry.pending_session_id)
897
881
  else
898
- updated = failed(entry, outcome.detail, now: now).with(due_was: nil)
882
+ updated = failed(entry, outcome.detail, now: now, code: outcome.code).with(due_was: nil)
899
883
  entry.recurrence[:kind] == "date" ? updated.with(enabled: false) : updated
900
884
  end
901
885
  end
902
886
 
903
- # :skip (busy now, or being continued: try the next tick) or :fail (the
904
- # target is gone or archived); nil when the entry may fire. The
887
+ # [:skip, words, code] (busy now, or being continued: try the next
888
+ # tick; code: BusyPart's, nil where none) or [:fail, words] (the target
889
+ # is gone or archived); nil when the entry may fire. The
905
890
  # continue's skip comes before the busy check: its successor holds no
906
891
  # worker lock yet, and the old link's busy signals say nothing.
907
892
  def check(entry, links)
@@ -962,8 +947,9 @@ module Samagotchi
962
947
 
963
948
  # A firing whose target no longer resolves (#target_of: gone, archived,
964
949
  # or being continued since the tick's check): what #check would say.
965
- def unresolved(entry, reason, kind)
966
- Outcome.new(id: entry.id, title: entry.title, status: kind == :skip ? "skipped" : "failed", detail: reason, entry: entry)
950
+ def unresolved(entry, reason, kind, code = nil)
951
+ Outcome.new(id: entry.id, title: entry.title, status: kind == :skip ? "skipped" : "failed", detail: reason, entry: entry,
952
+ code: code)
967
953
  end
968
954
 
969
955
  def fire_and_record(entry, links, now: @clock.call)
@@ -1046,8 +1032,10 @@ module Samagotchi
1046
1032
  Outcome.new(id: entry.id, title: entry.title, status: "pending", detail: "queued a turn",
1047
1033
  entry: entry.with(head: target, pending_session_id: target, pending_turn_id: result.enqueued_id))
1048
1034
  elsif result.wake_refused?
1049
- # Stopped (or archived) since the tick's check: a skip, as the check's.
1050
- Outcome.new(id: entry.id, title: entry.title, status: "skipped", detail: result.detail, entry: entry)
1035
+ # Stopped (or archived) since the tick's check: a skip, as the
1036
+ # check's, its code the refusal's (DeliverResult::WAKE_REFUSALS).
1037
+ Outcome.new(id: entry.id, title: entry.title, status: "skipped", detail: result.detail, entry: entry,
1038
+ code: result.error)
1051
1039
  elsif result.busy? || result.continued?
1052
1040
  # A transition held the target's lock (a continue naming its new
1053
1041
  # link, or another change): not now, the next tick tries again.
@@ -1063,14 +1051,16 @@ module Samagotchi
1063
1051
  def fire_same_again(entry)
1064
1052
  links = links_now
1065
1053
  verdict = check(entry, links)
1066
- return unresolved(entry, verdict[1], verdict[0]) if verdict
1054
+ return unresolved(entry, verdict[1], verdict[0], verdict[2]) if verdict
1067
1055
 
1068
1056
  fire_same(entry, links, walked_again: true)
1069
1057
  end
1070
1058
 
1059
+ # A refusal's +reason+ (an exception's, a DeliverResult's code) is the
1060
+ # outcome's code, its +message+ the words.
1071
1061
  def classify(entry, reason, message)
1072
1062
  status = SKIP_REASONS.include?(reason.to_s) ? "skipped" : "failed"
1073
- Outcome.new(id: entry.id, title: entry.title, status: status, detail: message, entry: entry)
1063
+ Outcome.new(id: entry.id, title: entry.title, status: status, detail: message, entry: entry, code: reason&.to_s)
1074
1064
  end
1075
1065
 
1076
1066
  # A run-now refused before anything was tried: an enabled schedule moves
@@ -1103,9 +1093,9 @@ module Samagotchi
1103
1093
  failures: 0, skips: 0, last_fired: @clock.call.utc.iso8601)
1104
1094
  with_history(fired, "fired", outcome.detail, at: now, session_id: outcome.entry.head)
1105
1095
  when "skipped"
1106
- skipped(stored, outcome.detail, now: now).with(firing_since: nil)
1096
+ skipped(stored, outcome.detail, now: now, code: outcome.code).with(firing_since: nil)
1107
1097
  else
1108
- failed(stored, outcome.detail, now: now)
1098
+ failed(stored, outcome.detail, now: now, code: outcome.code)
1109
1099
  end
1110
1100
 
1111
1101
  # A skip or a failure is "not now", not "late": the occurrence the tick
@@ -48,7 +48,7 @@ module Samagotchi
48
48
  # A cut's outcomes in the Bridge's ack (SteerCut#cut_for_steer).
49
49
  CUT_OUTCOMES = %w[now waits off].freeze
50
50
  # How long a send to a link being continued waits for the new link to
51
- # be saved (SessionInbox::Head's :continuing; the spawn is quick), and
51
+ # be saved (InboxDelivery::Head's :continuing; the spawn is quick), and
52
52
  # how often it asks again; then it is refused.
53
53
  CONTINUING_WAIT = 10.0
54
54
  CONTINUING_POLL = 0.2
@@ -354,7 +354,7 @@ module Samagotchi
354
354
  # a link named after it: seen is where each one went.
355
355
  seen = {}
356
356
  results = targets(options).map do |target|
357
- next unsent(target) unless target.is_a?(SessionInbox::Resolved)
357
+ next unsent(target) unless target.is_a?(InboxDelivery::Resolved)
358
358
  next true if seen[target.id]
359
359
 
360
360
  seen[target.id] = true
@@ -699,7 +699,7 @@ module Samagotchi
699
699
  requested_turn = options[:turn]&.to_s
700
700
  id = target.id
701
701
  if requested_turn && target.asked != id && @access.prepare_wait(target.asked, turn: requested_turn).known_turn
702
- @wait_target = target = SessionInbox::Resolved.new(id: target.asked, asked: target.asked, via: :same)
702
+ @wait_target = target = InboxDelivery::Resolved.new(id: target.asked, asked: target.asked, via: :same)
703
703
  @session_id = id = target.id
704
704
  end
705
705
  start = @access.prepare_wait(id, turn: requested_turn)
@@ -791,7 +791,7 @@ module Samagotchi
791
791
  @stderr.puts("#{target.label} refused: #{local_refusal(entry.name, target.id)}")
792
792
  else
793
793
  targets(options).each do |target|
794
- next unsent(target) unless target.is_a?(SessionInbox::Resolved)
794
+ next unsent(target) unless target.is_a?(InboxDelivery::Resolved)
795
795
 
796
796
  @info.puts("#{target.label} refused: #{local_refusal(entry.name, target.id)}")
797
797
  end
@@ -821,8 +821,8 @@ module Samagotchi
821
821
  # took its link since (DeliverResult#continued?): it resolves again
822
822
  # (#follow_again) and goes to the new link, until CONTINUING_WAIT after
823
823
  # the first refusal; never to the old link. A busy one goes again, the same link too.
824
- # @param target [SessionInbox::Resolved] the session (#target)
825
- # @yieldparam at [SessionInbox::Resolved] each link about to be sent to
824
+ # @param target [InboxDelivery::Resolved] the session (#target)
825
+ # @yieldparam at [InboxDelivery::Resolved] each link about to be sent to
826
826
  # @yieldreturn [Boolean] false: nothing is sent (its line printed)
827
827
  # @return [Boolean] whether the message was queued (@sent_to: where)
828
828
  def deliver(target, prompt, deadline: nil, &before)
@@ -957,23 +957,23 @@ module Samagotchi
957
957
  # The sessions the ids name, each once and in the order given, resolved
958
958
  # once up front (#target): what ParentGate checks, what the message
959
959
  # goes to, what --wait waits on.
960
- # @return [Array<SessionInbox::Resolved, Unsent>]
960
+ # @return [Array<InboxDelivery::Resolved, Unsent>]
961
961
  def targets(options)
962
962
  @targets ||= options[:ids].uniq.map { |given| target(given) }
963
963
  end
964
964
 
965
965
  # The targets' ids, for ParentGate (one that names none is skipped:
966
966
  # its line comes where it is sent).
967
- def resolved_ids(options) = targets(options).grep(SessionInbox::Resolved).map(&:id)
967
+ def resolved_ids(options) = targets(options).grep(InboxDelivery::Resolved).map(&:id)
968
968
 
969
969
  # --wait's one target, kept for the JSON lines (@session_id: its id;
970
970
  # asked_extra); nil after its line (an Unsent is kept too: a continue
971
971
  # in flight's refusal names the id asked).
972
- # @return [SessionInbox::Resolved, nil]
972
+ # @return [InboxDelivery::Resolved, nil]
973
973
  def wait_target(options)
974
974
  target = targets(options).first
975
975
  @wait_target = target
976
- unless target.is_a?(SessionInbox::Resolved)
976
+ unless target.is_a?(InboxDelivery::Resolved)
977
977
  unsent(target)
978
978
  return nil
979
979
  end
@@ -1007,7 +1007,7 @@ module Samagotchi
1007
1007
  # continue of it in flight (:continuing: the new link not saved yet)
1008
1008
  # is asked again for up to CONTINUING_WAIT, then refused; the old link
1009
1009
  # is never sent to (a send would un-archive it beside the new one).
1010
- # @return [SessionInbox::Resolved, Unsent]
1010
+ # @return [InboxDelivery::Resolved, Unsent]
1011
1011
  def target(given)
1012
1012
  found = exchange.locate(given)
1013
1013
  return unsent_of(found) if found.is_a?(SessionAccess::Exchange::Unsent)
@@ -1029,7 +1029,7 @@ module Samagotchi
1029
1029
  # +refused+'s id resolved again after a continue took its link
1030
1030
  # (#deliver): Exchange#relocate, until +deadline+. The new link's
1031
1031
  # worker is the continue's spawn to start (#delivery_note).
1032
- # @return [SessionInbox::Resolved, Unsent, nil] nil: out of time
1032
+ # @return [InboxDelivery::Resolved, Unsent, nil] nil: out of time
1033
1033
  def follow_again(refused, deadline, busy: false)
1034
1034
  again = exchange.relocate(refused, deadline, busy: busy) { |look| (@continued_here ||= {})[look.target.id] = true }
1035
1035
  return nil unless again