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
@@ -7,8 +7,8 @@ require_relative "session_inbox"
7
7
  module Samagotchi
8
8
  # The one rule for putting a context note into a session, shared by
9
9
  # `chi note`, the agent's send_note, `chi broadcast` and /objectives: a
10
- # note delivery (SessionInbox.deliver :note). A note to a link that was
11
- # continued goes to its chain's newest link (SessionInbox::Head); a
10
+ # note delivery (InboxDelivery.deliver :note). A note to a link that was
11
+ # continued goes to its chain's newest link (InboxDelivery::Head); a
12
12
  # session a one-turn chi run owns, or a chi scratch session, refuses it;
13
13
  # any other gets the note in its notes/ folder, which a live worker adds
14
14
  # within seconds and a session with no worker at its next start (a note
@@ -44,8 +44,8 @@ module Samagotchi
44
44
  # @param objectives [Hash, nil] an objectives note's payload
45
45
  # @param pin [Boolean] keep it in +id+, never its chain's newest link
46
46
  # (an /objectives list is the conversation of +id+'s own)
47
- # @param resolved [SessionInbox::Resolved, nil] the link the caller
48
- # resolved already (SessionInbox::Head.resolve(:note, id)): no second walk
47
+ # @param resolved [InboxDelivery::Resolved, nil] the link the caller
48
+ # resolved already (InboxDelivery::Head.resolve(:note, id)): no second walk
49
49
  # @return [Result]
50
50
  # @raise [SessionInbox::NoteRejected] an empty note or one over 16 KiB
51
51
  # @raise [Session::NotFound] no such session, or one being deleted
@@ -53,7 +53,7 @@ module Samagotchi
53
53
  resolved: nil)
54
54
  body = SessionInbox.checked_text(text)
55
55
  path = nil
56
- delivered = SessionInbox.deliver(:note, id, state_dir: state_dir, pin: pin, resolved: resolved) do |at|
56
+ delivered = InboxDelivery.deliver(:note, id, state_dir: state_dir, pin: pin, resolved: resolved) do |at|
57
57
  path = SessionInbox.write_note(at, text: body, source: source, from_session: from_session, from_cwd: from_cwd,
58
58
  objectives: objectives, state_dir: state_dir)
59
59
  false
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "steer"
3
+ require_relative "speaker"
4
4
  require_relative "tool_activity"
5
5
  require_relative "tool_response"
6
6
  require_relative "tool_names"
@@ -59,14 +59,14 @@ module Samagotchi
59
59
  end
60
60
 
61
61
  # The runs of the move at +position+ in +runs+ that are of its own turn:
62
- # after the nearest turn opening (Steer.turn_prompt?, what bounds a turn's
62
+ # after the nearest turn opening (Speaker.turn_prompt?, what bounds a turn's
63
63
  # slice) before the move's entry, up to the move. Mid-turn the fold reads
64
64
  # the loop's own conversation too (Engine#live_conversation), which holds
65
65
  # the running turn's opening. A rollback that drops a turn drops its runs
66
66
  # with its moves.
67
67
  def self.turn_runs(conversation, runs, position)
68
68
  move = runs[position]
69
- start = (move.index - 1).downto(0).find { |i| Steer.turn_prompt?(conversation[i]) } || -1
69
+ start = (move.index - 1).downto(0).find { |i| Speaker.turn_prompt?(conversation[i]) } || -1
70
70
  runs[0...position].select { |run| run.index > start }
71
71
  end
72
72
 
@@ -6,7 +6,7 @@ require_relative "answer_text"
6
6
  require_relative "context_note"
7
7
  require_relative "llm_context_stale"
8
8
  require_relative "redact"
9
- require_relative "steer"
9
+ require_relative "speaker"
10
10
 
11
11
  module Samagotchi
12
12
  autoload :Objectives, File.expand_path("objectives", __dir__)
@@ -76,7 +76,7 @@ module Samagotchi
76
76
  when "model"
77
77
  words = AnswerText.visible(text)
78
78
  "#{OWN_WORDS}\n#{cut(words, OUTPUT_CHARS)}" unless words.empty?
79
- when "user" then "[#{Steer.participant_message?(entry) ? "participant" : "user"}]\n#{cut(text, OUTPUT_CHARS)}"
79
+ when "user" then "[#{Speaker.participant_message?(entry) ? "participant" : "user"}]\n#{cut(text, OUTPUT_CHARS)}"
80
80
  when "system"
81
81
  return nil unless ContextNote.note?(entry) &&
82
82
  Objectives.outside_note?(ContextNote.fetch(entry, :source), ContextNote.fetch(entry, :objectives))
@@ -13,7 +13,7 @@ require_relative "message_time"
13
13
  require_relative "objectives_record"
14
14
  require_relative "owner_prompt"
15
15
  require_relative "session"
16
- require_relative "steer"
16
+ require_relative "speaker"
17
17
  require_relative "tools/objectives_tool"
18
18
  require_relative "visitor"
19
19
 
@@ -130,7 +130,7 @@ module Samagotchi
130
130
  # execute that ran ok.
131
131
  def worked_without_move?(conversation)
132
132
  conversation = Array(conversation)
133
- start = conversation.rindex { |entry| Steer.turn_prompt?(entry) } || -1
133
+ start = conversation.rindex { |entry| Speaker.turn_prompt?(entry) } || -1
134
134
  runs = LLMContextStale.paired_runs(conversation).select { |run| run.index > start }
135
135
  moved = runs.any? do |run|
136
136
  run.call[:name] == Tools::ObjectivesTool::NAME && Tools::ObjectivesTool.recorded?(run.text, Objectives.move_id(run.call))
@@ -3,6 +3,7 @@
3
3
  require "fileutils"
4
4
  require "json"
5
5
  require "time"
6
+ require_relative "session_paths"
6
7
 
7
8
  module Samagotchi
8
9
  # The single-owner lock of a session: whichever process runs the session's
@@ -25,6 +26,15 @@ module Samagotchi
25
26
  DEFAULT_WAIT = 2.0
26
27
  # The kind a deleter holds the lock as while a session's files go.
27
28
  DELETING = "deleting"
29
+ # The kind `chi sessions migrate` holds the lock as while it converts a
30
+ # session's file (StorageMigration): no worker, nothing to signal, and
31
+ # gone in milliseconds. What meets it waits for it (MIGRATING_WAIT:
32
+ # WorkerPresence.settled, a worker taking the lock): the session is
33
+ # never read as being deleted.
34
+ MIGRATING = "migrating"
35
+ # Seconds a delivery, a respawn or a starting worker waits for a
36
+ # migrating holder to let go.
37
+ MIGRATING_WAIT = 5.0
28
38
  RETRY_INTERVAL = 0.05
29
39
  # Let a delete retry past an owner's momentary shared-lock probe.
30
40
  PROBE_WAIT = 2 * RETRY_INTERVAL
@@ -37,7 +47,8 @@ module Samagotchi
37
47
 
38
48
  # Who holds the lock, as .owner reads it from the lock file. Its fields
39
49
  # may be nil for a moment right after acquisition.
40
- # kind: "worker", "tui" or "deleting" (Owner#deleting?); pid as written (an Integer). leaving_at (an
50
+ # kind: "worker", "tui", "deleting" (Owner#deleting?) or "migrating"
51
+ # (Owner#migrating?); pid as written (an Integer). leaving_at (an
41
52
  # ISO 8601 time) and leave_wait (seconds): set once a worker is leaving
42
53
  # (#mark_leaving), its Bridge closed, the lock held for its last work
43
54
  # (the recap on leave) for up to leave_wait.
@@ -53,6 +64,10 @@ module Samagotchi
53
64
  # remove_session_files): no worker, nothing to signal.
54
65
  def deleting? = kind == DELETING
55
66
 
67
+ # A storage migration converting the session's file (StorageMigration):
68
+ # no worker, nothing to signal, gone in a moment.
69
+ def migrating? = kind == MIGRATING
70
+
56
71
  # A worker that closed its Bridge and releases the lock once its
57
72
  # last work is done.
58
73
  def leaving? = worker? && !leaving_at.nil?
@@ -70,7 +85,7 @@ module Samagotchi
70
85
  # #owner probe holds it for a moment). On success the owner's pid and kind
71
86
  # are written into the lock file for #owner to report.
72
87
  # @param session_dir [String]
73
- # @param kind [String] "worker" or "tui"
88
+ # @param kind [String] "worker", "tui", DELETING or MIGRATING
74
89
  # @return [OwnerLock, nil] nil when another owner holds it
75
90
  def self.acquire(session_dir, kind:, wait: DEFAULT_WAIT)
76
91
  FileUtils.mkdir_p(session_dir)
@@ -87,6 +102,17 @@ module Samagotchi
87
102
  new(file, kind: kind)
88
103
  end
89
104
 
105
+ # Session +session_id+'s live owner: the lock holder (.owner) of its
106
+ # dir in +state_dir+. The pid file workers once wrote is never read: a
107
+ # stale one whose pid the OS reused would make a session look owned.
108
+ # +state_dir+ is required: a caller resolves its own default
109
+ # (Session.default_state_dir), so a spec that points it elsewhere
110
+ # points this read there too.
111
+ # @return [Owner, nil]
112
+ def self.for_session(session_id, state_dir:)
113
+ owner(SessionPaths.session_dir(session_id, state_dir: state_dir))
114
+ end
115
+
90
116
  # The current owner, read without disturbing it.
91
117
  # @param session_dir [String]
92
118
  # @return [Owner, nil] while held, nil when free
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "message_time"
4
- require_relative "steer"
4
+ require_relative "speaker"
5
5
 
6
6
  module Samagotchi
7
7
  # The session owner's own words: the one rule for what may act on a
@@ -18,7 +18,7 @@ module Samagotchi
18
18
  # opener (an agent's in a session it doesn't drive) carries its source.
19
19
  def message?(message)
20
20
  field(message, :role).to_s == "user" &&
21
- !Steer.participant_author?(field(message, :author)) &&
21
+ !Speaker.participant_author?(field(message, :author)) &&
22
22
  !(field(message, :kind).to_s.empty? && !field(message, :source).to_s.empty?) &&
23
23
  !%w[input steer note reminder].include?(field(message, :kind).to_s) &&
24
24
  !field(message, :preserve_literals) &&
@@ -28,7 +28,7 @@ module Samagotchi
28
28
  # Whether a turn by +author+ is the owner's own: not a participant's.
29
29
  # The callers ask Visitor.of? beside it, for a visitor's turn (its
30
30
  # origin's client id).
31
- def turn?(author:) = !Steer.participant_author?(author)
31
+ def turn?(author:) = !Speaker.participant_author?(author)
32
32
 
33
33
  def field(hash, key)
34
34
  return nil unless hash.is_a?(Hash)
@@ -27,9 +27,12 @@ module Samagotchi
27
27
  class Unreadable < Error; end
28
28
 
29
29
  # One row of a schedule's history (Schedules::HistoryPart): when, what
30
- # (fired, skipped, failed, missed, enabled, disabled, edited), why, and
31
- # the session it acted on. Times are ISO 8601 UTC strings.
32
- Run = Data.define(:at, :status, :detail, :session_id)
30
+ # (fired, skipped, failed, missed, enabled, disabled, edited), why (the
31
+ # words, +detail+, and as a code, +code+, nil where none is known: an
32
+ # older row's), and the session it acted on. Times are ISO 8601 UTC strings.
33
+ Run = Data.define(:at, :status, :detail, :session_id, :code) do
34
+ def initialize(at:, status:, detail:, session_id:, code: nil) = super
35
+ end
33
36
 
34
37
  # The members a Schedule has: the keys of `chi schedule list --format
35
38
  # json` (ScheduleDisplay.json) a page may rely on. The others (a
@@ -53,7 +56,9 @@ module Samagotchi
53
56
  fields = SCHEDULE_MEMBERS.to_h { |member| [member, json[member.to_s]] }
54
57
  fields[:history] = Array(json["history"]).filter_map do |row|
55
58
  row = row.transform_keys(&:to_s) if row.is_a?(Hash)
56
- row.is_a?(Hash) ? Run.new(at: row["at"], status: row["status"], detail: row["detail"], session_id: row["session_id"]) : nil
59
+ next nil unless row.is_a?(Hash)
60
+
61
+ Run.new(at: row["at"], status: row["status"], detail: row["detail"], session_id: row["session_id"], code: row["code"])
57
62
  end
58
63
  new(**fields)
59
64
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "answer_tail"
4
- require_relative "steer"
4
+ require_relative "speaker"
5
5
  require_relative "verify_anchor"
6
6
 
7
7
  module Samagotchi
@@ -18,7 +18,7 @@ module Samagotchi
18
18
  # A turn's opening as the rule counts it: a prompt or a wake turn's
19
19
  # note, never a stored `!shell` line (the owner's, which a participant
20
20
  # doesn't see: ParticipantView drops it, so it counts for nothing).
21
- def opening?(message) = Steer.turn_prompt?(message) && !Steer.shell_line?(message)
21
+ def opening?(message) = Speaker.turn_prompt?(message) && !Speaker.shell_line?(message)
22
22
 
23
23
  # Why +author+ ("p:<id>") may not revise the turn +turn_id+ in
24
24
  # +messages+, or nil when they may:
@@ -32,7 +32,7 @@ module Samagotchi
32
32
  # @return [Symbol, nil]
33
33
  def refusal(messages, turn_id:, author:, before: nil)
34
34
  list = Array(messages)
35
- author = Steer.sender_author(author)
35
+ author = Speaker.sender_author(author)
36
36
  return :not_own unless author
37
37
 
38
38
  start = list.rindex { |m| opening?(m) && field(m, :turn_id).to_s == turn_id.to_s }
@@ -41,18 +41,18 @@ module Samagotchi
41
41
  list[(start + 1)..].each do |message|
42
42
  next unless opening?(message)
43
43
  break if before && field(message, :turn_id).to_s == before.to_s
44
- return :overtaken unless Steer.sender_author(field(message, :author)) == author
44
+ return :overtaken unless Speaker.sender_author(field(message, :author)) == author
45
45
  end
46
46
  nil
47
47
  end
48
48
 
49
49
  def own_answered?(list, start, author)
50
50
  opening = list[start]
51
- return false unless Steer.sender_author(field(opening, :author)) == author
51
+ return false unless Speaker.sender_author(field(opening, :author)) == author
52
52
  return false if VerifyAnchor.marks?(field(opening, :verify))
53
53
 
54
54
  # Its slice, as AnswerTail.turn_slice bounds one: up to the next prompt.
55
- stop = list.each_index.find { |i| i > start && Steer.turn_prompt?(list[i]) } || list.size
55
+ stop = list.each_index.find { |i| i > start && Speaker.turn_prompt?(list[i]) } || list.size
56
56
  list[start...stop].any? { |message| AnswerTail.answer?(message) }
57
57
  end
58
58
 
@@ -166,10 +166,17 @@ module Samagotchi
166
166
  receipts = current.receipts.reject { |row| items.any? { |item| item.serial == row.serial } } + rows
167
167
  update_peer(name, current.with(taken: [current.taken, taken.to_i].max, receipts: receipts))
168
168
  end
169
- write_changed
170
- rescue SystemCallError, IOError
171
- @book = previous
172
- raise
169
+ # The write is the rollback point, and the announcer (on_change) runs
170
+ # after the rescue: an error from it must not roll the book back behind
171
+ # the file (the next write would then drop these receipts). A write
172
+ # failure still rolls back and raises.
173
+ begin
174
+ write
175
+ rescue SystemCallError, IOError
176
+ @book = previous
177
+ raise
178
+ end
179
+ @on_change&.call
173
180
  end
174
181
 
175
182
  # A turn starts (+turn_id+): every held or noted receipt is read by it.
@@ -54,7 +54,7 @@ module Samagotchi
54
54
  def speaker(message)
55
55
  return "Assistant" unless message["role"] == "user"
56
56
 
57
- Steer.participant_message?(message) ? "Participant" : "User"
57
+ Speaker.participant_message?(message) ? "Participant" : "User"
58
58
  end
59
59
 
60
60
  # String keys, as TranscriptFilter reads a saved session.
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "steer"
3
+ require_relative "speaker"
4
4
 
5
5
  module Samagotchi
6
6
  # Where a saved recap sits among the turns a page draws: after the last
7
- # turn opening it covers (Steer.turn_opening?, the rule its turns_since
7
+ # turn opening it covers (Speaker.turn_opening?, the rule its turns_since
8
8
  # count goes by). Its stamp is that opening's id, as a card's is
9
- # (Steer.opening_stamp: a prompt's turn_id, a wake note's, a /continue no
9
+ # (Speaker.opening_stamp: a prompt's turn_id, a wake note's, a /continue no
10
10
  # reason line's opening_id), so the page finds the bubble by
11
11
  # id, not by counting back (a rollback or a check-in could make the count
12
12
  # land a turn off). Read at read time from recap.json's `covered`: the file
@@ -19,8 +19,8 @@ module Samagotchi
19
19
  # @return [String, nil] the id of the last opening in the covered
20
20
  # messages; nil for none, or one with no id (a line an older chi wrote)
21
21
  def opening_id(messages, covered)
22
- opening = Array(messages).first([covered.to_i, 0].max).reverse_each.find { |message| Steer.turn_opening?(message) }
23
- opening && Steer.opening_stamp(opening)
22
+ opening = Array(messages).first([covered.to_i, 0].max).reverse_each.find { |message| Speaker.turn_opening?(message) }
23
+ opening && Speaker.opening_stamp(opening)
24
24
  end
25
25
  end
26
26
  end
@@ -145,16 +145,16 @@ module Samagotchi
145
145
  next false unless (message[:kind] || message["kind"]).to_s.empty?
146
146
  next false if (message[:turn_id] || message["turn_id"]).to_s.empty?
147
147
 
148
- !Steer.shell_line?(message)
148
+ !Speaker.shell_line?(message)
149
149
  end
150
150
  end
151
151
 
152
152
  # The ReplyFor the latest turn in +messages+ was opened with: its
153
- # opening (Steer.turn_prompt?) is a wake's too, which declares none,
153
+ # opening (Speaker.turn_prompt?) is a wake's too, which declares none,
154
154
  # so a wake turn's question never takes an earlier ask's surface.
155
155
  # @return [ReplyFor, nil]
156
156
  def of_latest_turn(messages)
157
- opening = Array(messages).reverse_each.find { |message| Steer.turn_prompt?(message) }
157
+ opening = Array(messages).reverse_each.find { |message| Speaker.turn_prompt?(message) }
158
158
  opening && from(opening[MESSAGE_KEY] || opening[MESSAGE_KEY.to_s])
159
159
  end
160
160
 
@@ -17,12 +17,12 @@ module Samagotchi
17
17
  @state_dir = state_dir
18
18
  end
19
19
 
20
- # The lock's holder (SessionManager.session_owner).
20
+ # The lock's holder (OwnerLock.for_session).
21
21
  # @return [OwnerLock::Owner, nil]
22
22
  def owner
23
23
  return @owner if defined?(@owner)
24
24
 
25
- @owner = SessionManager.session_owner(@id, state_dir: @state_dir)
25
+ @owner = OwnerLock.for_session(@id, state_dir: @state_dir)
26
26
  end
27
27
 
28
28
  def owner? = !owner.nil?
@@ -4,7 +4,7 @@ require_relative "../session"
4
4
  require_relative "../session_inbox"
5
5
  require_relative "../turn_checkpoint"
6
6
  require_relative "../continue_offer"
7
- require_relative "../steer"
7
+ require_relative "../speaker"
8
8
  require_relative "liveness"
9
9
 
10
10
  module Samagotchi
@@ -96,8 +96,8 @@ module Samagotchi
96
96
  # turn (its author's), so it runs a turn of its own only when it
97
97
  # would wait even there.
98
98
  @own_turn_input ||= !waiting.nil? &&
99
- SessionInbox.turn_end_data?(waiting, running_author: Steer.sender_author(waiting["author"]))
100
- @participant_input ||= !waiting.nil? && Steer.participant_author?(waiting["author"])
99
+ SessionInbox.turn_end_data?(waiting, running_author: Speaker.sender_author(waiting["author"]))
100
+ @participant_input ||= !waiting.nil? && Speaker.participant_author?(waiting["author"])
101
101
  @claimed = waiting.nil?
102
102
  end
103
103
 
@@ -393,7 +393,7 @@ module Samagotchi
393
393
 
394
394
  " disabled after #{entry.skips} #{entry.skips == 1 ? "skip" : "skips"}"
395
395
  when "failures"
396
- return " disabled" unless entry.failures >= entry.max_failures
396
+ return " disabled" unless entry.failures >= entry.max_failures || Schedules::NoticesPart.config_failed?(entry)
397
397
 
398
398
  " disabled after #{entry.failures} #{entry.failures == 1 ? "failure" : "failures"}"
399
399
  else " disabled"
@@ -63,8 +63,9 @@ module Samagotchi
63
63
  # Why an off schedule is off, nil when it isn't (state on or firing):
64
64
  # "owner" turned off by `chi schedule disable` (its last result)
65
65
  # "skips" its skip budget ran out (max_skips)
66
- # "failures" its failure budget ran out (max_failures), or a one-off
67
- # whose firing failed
66
+ # "failures" its failure budget ran out (max_failures), a failure
67
+ # no retry fixes turned it off (Schedules::HistoryPart::CONFIG_FAILURES),
68
+ # or a one-off whose firing failed
68
69
  # "done" a one-off that fired
69
70
  # "missed" a one-off whose moment passed unseen
70
71
  # "off" none of these (an older file's spent one-off)
@@ -75,7 +76,7 @@ module Samagotchi
75
76
  return nil unless state(entry) == "off"
76
77
  return "owner" if entry.last_result == "disabled"
77
78
  return "skips" if entry.skips >= entry.max_skips
78
- return "failures" if entry.failures >= entry.max_failures
79
+ return "failures" if entry.failures >= entry.max_failures || Schedules::NoticesPart.config_failed?(entry)
79
80
  return "off" unless entry.recurrence[:kind] == "date"
80
81
 
81
82
  case entry.history.reverse.find { |row| OUTCOMES.include?(row[:status]) }&.dig(:status)
@@ -28,8 +28,32 @@ module Samagotchi
28
28
  worker_busy: "its worker is starting or leaving"
29
29
  }.freeze
30
30
  # A chain's skip while a terminal or a web tab is open on its target
31
- # (#live_busy_reason): the words MarksPart compares a skip's with.
31
+ # (#live_busy_reason), and its code (MarksPart reads the code; the
32
+ # words only for a row an older chi wrote without one).
32
33
  OPEN_TAB_WORDS = "a terminal or a web tab is open on it"
34
+ OPEN_TAB = "open_tab"
35
+ # The codes of a skip for a stop: the busy cell's, and a same-mode
36
+ # firing's wake refusal (SessionManager::DeliverResult::STOPPED's
37
+ # error) when the stop landed after the check.
38
+ STOP_CODES = ["stopped", SessionManager::DeliverResult::STOPPED["error"]].freeze
39
+
40
+ # The one reading of "stopped" for schedules: every firing on the
41
+ # session skips as stopped (#busy_cell). The stop marker, unless a
42
+ # deleter holds the lock: SessionLifecycle's stopped states
43
+ # (STOPPED_STATES), and with them, while F1 stands (spec/session_lifecycle/
44
+ # matrix_spec.rb's KNOWN_DIFFERENCES), a marked session a one-turn
45
+ # run or a lock still blank holds. #busy_cell, #stopped?
46
+ # (ProtectionPart#stopped_target, #waiting) and the page's marks
47
+ # (Web::SessionSummary) read it.
48
+ # @param presence [WorkerPresence]
49
+ def self.stop_holds?(presence) = presence.stop_marked? && !presence.deleting?
50
+
51
+ # Whether every firing on +session_id+ skips as stopped now
52
+ # (BusyPart.stop_holds?, one presence read).
53
+ # @return [Boolean]
54
+ def stopped?(session_id)
55
+ BusyPart.stop_holds?(WorkerPresence.read(session_id, state_dir: @state_dir))
56
+ end
33
57
 
34
58
  private
35
59
 
@@ -43,7 +67,7 @@ module Samagotchi
43
67
  # SessionManager::DeliverResult::STOPPED, in the same words).
44
68
  def busy_reason(entry, links, target)
45
69
  dir = Session.session_dir(target, state_dir: @state_dir)
46
- owner = SessionManager.session_owner(target, state_dir: @state_dir)
70
+ owner = OwnerLock.for_session(target, state_dir: @state_dir)
47
71
  presence = WorkerPresence.read(target, state_dir: @state_dir, owner: owner, link: :look)
48
72
  # exists: true: #target_of resolved the target already, and a delete
49
73
  # mid-way (its JSON gone, the deleting lock held) must read deleting,
@@ -51,7 +75,7 @@ module Samagotchi
51
75
  # ContinueIntent read this check doesn't use.
52
76
  state = SessionLifecycle::Reading.derive(presence.with(exists: true)).state
53
77
  cell = busy_cell(entry, presence, state)
54
- return [:skip, busy_words(cell, presence.link)] unless cell == :go
78
+ return [:skip, busy_words(cell, presence.link), cell.to_s] unless cell == :go
55
79
  return live_busy_reason(entry, links, target, presence.link) if state == :live
56
80
 
57
81
  file_busy_reason(links.session(target), dir)
@@ -69,7 +93,7 @@ module Samagotchi
69
93
  def busy_cell(entry, presence, state)
70
94
  chain = entry.mode == CHAIN
71
95
  cell = SessionLifecycle.allowed(chain ? :sched_chain : :sched_same, state)
72
- return :stopped if presence.stop_marked? && cell != :deleting
96
+ return :stopped if BusyPart.stop_holds?(presence)
73
97
  return :worker_busy if chain && cell == :go && presence.starting? && !presence.worker_owned?
74
98
 
75
99
  cell
@@ -107,7 +131,7 @@ module Samagotchi
107
131
  # A chain firing archives the session and stops its worker: a
108
132
  # terminal or a web tab on it would be cut off. A queued turn into
109
133
  # an open session is fine, so same mode does not count streams.
110
- return [:skip, OPEN_TAB_WORDS] if entry.mode == CHAIN && activity[:open_streams].to_i.positive?
134
+ return [:skip, OPEN_TAB_WORDS, OPEN_TAB] if entry.mode == CHAIN && activity[:open_streams].to_i.positive?
111
135
 
112
136
  # An idle live worker: free, whatever a starting marker its own
113
137
  # start left (Worker#start clears it just after the lock).
@@ -4,7 +4,11 @@ module Samagotchi
4
4
  class Schedules
5
5
  # The run history (docs/scheduling.md, `chi schedule show`): each entry
6
6
  # keeps its last HISTORY_MAX outcomes in `history`, newest last, rows
7
- # {at:, status:, detail:, session_id:}. One writer per status, each
7
+ # {at:, status:, detail:, session_id:, code:}: detail the words (shown),
8
+ # code the reason as a code (Outcome#code), present only where one is
9
+ # known; a reader classifies by the code, by the words only for a row
10
+ # an older chi wrote without it (docs/architecture.md, "Codes beside
11
+ # words"). One writer per status, each
8
12
  # appending once per event:
9
13
  # skipped, failed — #skipped / #failed (the helpers, never their
10
14
  # callers), and #run_now's refusal of a disabled entry (no counters);
@@ -20,6 +24,13 @@ module Samagotchi
20
24
  # or a turn's reason can be long): with the row's other fields (~120
21
25
  # bytes) an entry's history stays under about 20 × 320 bytes, 6.4 KB.
22
26
  DETAIL_MAX = 200
27
+ # The failed turns' codes (TurnOutcome#code) that no retry fixes: the
28
+ # host refused the key, or doesn't serve the session's model. Such a
29
+ # failure turns a schedule off at once (#failed,
30
+ # NoticesPart#budget_turned_off?), its words naming the model, the host
31
+ # and the fix (NoticesPart#config_failure_words); a flaky one
32
+ # (connection, a 5xx, a timeout) keeps the max_failures budget.
33
+ CONFIG_FAILURES = %w[auth model_not_served].freeze
23
34
 
24
35
  # The rows as JSON hands them back (string keys) or as written (symbol
25
36
  # keys): symbol keys, non-rows dropped.
@@ -30,20 +41,49 @@ module Samagotchi
30
41
 
31
42
  private
32
43
 
44
+ # #skipped for a check's :skip, else #failed.
45
+ def skipped_or_failed(entry, kind, reason, now, code)
46
+ kind == :skip ? skipped(entry, reason, now: now, code: code) : failed(entry, reason, now: now, code: code)
47
+ end
48
+
49
+ # +code+: the skip's reason as a code (Outcome#code), kept on its row.
50
+ def skipped(entry, reason, now: @clock.call, code: nil)
51
+ skips = entry.skips + 1
52
+ within_budget = skips < entry.max_skips
53
+ updated = entry.with(skips: skips, firing_since: nil, firing_token: nil, pending_session_id: nil, pending_turn_id: nil,
54
+ last_result: within_budget ? "skipped" : "disabled: #{reason}",
55
+ enabled: within_budget && (entry.enabled || entry.recurrence[:kind] == "date"))
56
+ recorded(entry, updated, "skipped", reason, now, code: code)
57
+ end
58
+
59
+ # +code+: the failure's reason as a code (Outcome#code), kept on its
60
+ # row; one of CONFIG_FAILURES turns a recurring entry off at once.
61
+ def failed(entry, reason, now: @clock.call, code: nil)
62
+ failures = entry.failures + 1
63
+ within_budget = failures < entry.max_failures && !CONFIG_FAILURES.include?(code.to_s)
64
+ updated = entry.with(failures: failures, firing_since: nil, firing_token: nil, pending_session_id: nil,
65
+ pending_turn_id: nil, last_result: reason,
66
+ enabled: within_budget && (entry.enabled || entry.recurrence[:kind] == "date"))
67
+ recorded(entry, updated, "failed", reason, now, code: code)
68
+ end
69
+
33
70
  # A skip's or a failure's row (+before+ the call, +after+ it), and the
34
71
  # disabled notice when this call turned the entry off by its budget.
35
72
  # Its session: the pending firing's, else the head.
36
- def recorded(before, after, status, reason, now)
37
- off = budget_turned_off?(before, after, status)
38
- noticed = with_disabled_notice(before, after, status, reason, now)
73
+ def recorded(before, after, status, reason, now, code: nil)
74
+ off = budget_turned_off?(before, after, status, code&.to_s)
75
+ noticed = with_disabled_notice(before, after, status, reason, now, code&.to_s)
39
76
  with_history(noticed, status, off ? "disabled: #{reason}" : reason, at: now,
40
- session_id: before.pending_session_id || before.head)
77
+ session_id: before.pending_session_id || before.head,
78
+ code: code)
41
79
  end
42
80
 
43
- # +entry+ with one row more, the oldest dropped past HISTORY_MAX.
44
- def with_history(entry, status, detail, at:, session_id: nil)
81
+ # +entry+ with one row more, the oldest dropped past HISTORY_MAX. A
82
+ # nil +code+ writes no code key (a row as an older chi wrote it).
83
+ def with_history(entry, status, detail, at:, session_id: nil, code: nil)
45
84
  clipped = detail.nil? ? nil : detail.to_s[0, DETAIL_MAX]
46
85
  row = { at: at.utc.iso8601, status: status, detail: clipped, session_id: session_id }
86
+ row[:code] = code.to_s unless code.nil?
47
87
  entry.with(history: (HistoryPart.rows(entry.history) + [row]).last(HISTORY_MAX))
48
88
  end
49
89
  end