samagotchi 0.190.0 → 0.191.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -1
- data/docs/architecture.md +8 -7
- data/docs/cli.md +6 -3
- data/docs/context.md +9 -2
- data/docs/guardrails.md +5 -1
- data/docs/internals/attached-context.md +3 -1
- data/docs/internals/peers.md +15 -3
- data/docs/internals/remote-sessions.md +2 -2
- data/docs/internals/session-lifecycle.md +213 -12
- data/docs/peers.md +3 -1
- data/docs/plugins.md +2 -1
- data/docs/scheduling.md +31 -7
- data/docs/sessions.md +11 -11
- data/docs/sub-agent.md +1 -1
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/known-names/manifest.yml +2 -2
- data/lib/samagotchi/bundles/known-names/plugin.rb +17 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/context_command.rb +5 -1
- data/lib/samagotchi/context_sources.rb +28 -0
- data/lib/samagotchi/continue_intent.rb +23 -43
- data/lib/samagotchi/hold_stop.rb +5 -2
- data/lib/samagotchi/participants.rb +13 -5
- data/lib/samagotchi/peer_say.rb +29 -14
- data/lib/samagotchi/plugin/sessions.rb +9 -5
- data/lib/samagotchi/schedule_command.rb +5 -1
- data/lib/samagotchi/schedules/prune_part.rb +138 -38
- data/lib/samagotchi/schedules.rb +29 -6
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/session_access/ask.rb +9 -3
- data/lib/samagotchi/session_access/exchange.rb +6 -2
- data/lib/samagotchi/session_access/local.rb +104 -31
- data/lib/samagotchi/session_access/remote.rb +10 -9
- data/lib/samagotchi/session_access/remote_peers.rb +2 -2
- data/lib/samagotchi/session_access.rb +17 -7
- data/lib/samagotchi/session_chain/delete.rb +100 -47
- data/lib/samagotchi/session_chain/delete_preview.rb +9 -5
- data/lib/samagotchi/session_chain/links.rb +68 -9
- data/lib/samagotchi/session_chain/successor.rb +78 -0
- data/lib/samagotchi/session_continue.rb +108 -40
- data/lib/samagotchi/session_inbox.rb +1 -1
- data/lib/samagotchi/session_inbox_delivery.rb +113 -26
- data/lib/samagotchi/session_lifecycle/allowed.rb +20 -1
- data/lib/samagotchi/session_lifecycle/reading.rb +9 -3
- data/lib/samagotchi/session_lifecycle.rb +13 -2
- data/lib/samagotchi/session_manager/delivery.rb +61 -23
- data/lib/samagotchi/session_manager/emptiness.rb +5 -2
- data/lib/samagotchi/session_manager/lifecycle.rb +50 -14
- data/lib/samagotchi/session_manager/removal.rb +65 -13
- data/lib/samagotchi/session_manager/spawn.rb +14 -6
- data/lib/samagotchi/session_retention.rb +28 -7
- data/lib/samagotchi/session_transition.rb +315 -0
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +10 -2
- data/lib/samagotchi/web/peer_routes.rb +5 -2
- data/lib/samagotchi/web/session_access_routes.rb +5 -2
- data/lib/samagotchi/web/session_control.rb +6 -1
- data/lib/samagotchi/web/session_create.rb +1 -1
- data/lib/samagotchi/worker_boot.rb +28 -6
- metadata +3 -1
data/lib/samagotchi/peer_say.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "peer_mailbox"
|
|
4
4
|
require_relative "peer_receipts"
|
|
5
|
+
require_relative "session_chain/successor"
|
|
6
|
+
require_relative "session_transition"
|
|
5
7
|
|
|
6
8
|
module Samagotchi
|
|
7
9
|
# What chi peer say tells the peer, in chi's own words: the message's
|
|
@@ -12,7 +14,9 @@ module Samagotchi
|
|
|
12
14
|
# #line, so a --remote say prints the remote's line.
|
|
13
15
|
#
|
|
14
16
|
# +status+: :received, or a refusal (nothing written): :gone (no such
|
|
15
|
-
# session), :deleting, :continued (+next_id+
|
|
17
|
+
# session), :deleting, :continued (+next_id+; +in_flight+ while its
|
|
18
|
+
# continue runs: the new link is not saved yet), :busy (another change
|
|
19
|
+
# of the session holds its transition lock), :not_paired, :old_worker
|
|
16
20
|
# (a live worker without the peer mail), :full (UNTAKEN_MAX unread).
|
|
17
21
|
# +serial+: the message's. +wake+: what the say saw at once, when it
|
|
18
22
|
# didn't wait: :running (a turn runs), :starting, :stopped, :archived,
|
|
@@ -22,14 +26,15 @@ module Samagotchi
|
|
|
22
26
|
# without one. +earlier+: [[serial, reason]…] of the peer's other held
|
|
23
27
|
# messages. +turns+: the wakes in a row that a held receipt's reason
|
|
24
28
|
# names (peer turns or max wakes), as configured where the say ran.
|
|
25
|
-
PeerSay = Data.define(:status, :serial, :wake, :receipt, :earlier, :next_id, :turns) do
|
|
26
|
-
def initialize(status:, serial: nil, wake: nil, receipt: nil, earlier: [], next_id: nil, turns: nil
|
|
29
|
+
PeerSay = Data.define(:status, :serial, :wake, :receipt, :earlier, :next_id, :turns, :in_flight) do
|
|
30
|
+
def initialize(status:, serial: nil, wake: nil, receipt: nil, earlier: [], next_id: nil, turns: nil,
|
|
31
|
+
in_flight: false) = super
|
|
27
32
|
|
|
28
33
|
def received? = status == :received
|
|
29
34
|
|
|
30
35
|
# The line for +peer+ (its name): "#12 received · woke chi".
|
|
31
36
|
def line(peer)
|
|
32
|
-
return PeerSay.refusal(status, peer, next_id) unless received?
|
|
37
|
+
return PeerSay.refusal(status, peer, next_id, in_flight: in_flight) unless received?
|
|
33
38
|
|
|
34
39
|
parts = ["##{serial} received", tail(peer)]
|
|
35
40
|
parts << "also held: #{PeerSay.held_list(earlier)}" unless earlier.empty?
|
|
@@ -38,7 +43,7 @@ module Samagotchi
|
|
|
38
43
|
|
|
39
44
|
def to_h
|
|
40
45
|
{ "status" => status.to_s, "serial" => serial, "wake" => wake&.to_s, "receipt" => receipt&.to_h,
|
|
41
|
-
"earlier" => earlier, "next_id" => next_id, "turns" => turns }.compact
|
|
46
|
+
"earlier" => earlier, "next_id" => next_id, "turns" => turns, "in_flight" => (true if in_flight) }.compact
|
|
42
47
|
end
|
|
43
48
|
|
|
44
49
|
# The JSON a chi web answered (#to_h); nil for another shape.
|
|
@@ -50,15 +55,17 @@ module Samagotchi
|
|
|
50
55
|
new(status: data["status"].to_sym, serial: (data["serial"] if data["serial"].is_a?(Integer)),
|
|
51
56
|
wake: (data["wake"].to_sym if PeerSay::WAKES.include?(data["wake"].to_s)),
|
|
52
57
|
receipt: PeerReceipts::Receipt.from_h(data["receipt"]), earlier: earlier,
|
|
53
|
-
next_id: data["next_id"]&.to_s, turns: (data["turns"] if data["turns"].is_a?(Integer))
|
|
58
|
+
next_id: data["next_id"]&.to_s, turns: (data["turns"] if data["turns"].is_a?(Integer)),
|
|
59
|
+
in_flight: data["in_flight"] == true)
|
|
54
60
|
end
|
|
55
61
|
|
|
56
62
|
# The words of a refused say, nothing written.
|
|
57
|
-
def self.refusal(status, peer, next_id)
|
|
63
|
+
def self.refusal(status, peer, next_id, in_flight: false)
|
|
58
64
|
case status
|
|
59
65
|
when :gone then "no such session"
|
|
60
66
|
when :deleting then "this session is being deleted"
|
|
61
|
-
when :continued then
|
|
67
|
+
when :continued then PeerSay.continued(next_id, in_flight, "say it there")
|
|
68
|
+
when :busy then SessionTransition::Busy.new(nil).message
|
|
62
69
|
when :not_paired then "not paired: run the pair script"
|
|
63
70
|
when :old_worker then "this session's chi is older than chi peer: restart the session (chi sessions restart)"
|
|
64
71
|
when :full then "chi has #{PeerMailbox::UNTAKEN_MAX} unread messages from #{peer}"
|
|
@@ -66,6 +73,13 @@ module Samagotchi
|
|
|
66
73
|
end
|
|
67
74
|
end
|
|
68
75
|
|
|
76
|
+
# A continued link's refusal (SessionChain::Continued, "this session"),
|
|
77
|
+
# then what to do: +what+ there, once the new link is saved when its
|
|
78
|
+
# continue still runs.
|
|
79
|
+
def self.continued(next_id, in_flight, what)
|
|
80
|
+
"#{SessionChain::Continued.new(nil, next_id, in_flight: in_flight).message}: #{what}#{" in a moment" if in_flight}"
|
|
81
|
+
end
|
|
82
|
+
|
|
69
83
|
# "#10, #11 (paused)", a run of one reason at a time.
|
|
70
84
|
def self.held_list(earlier)
|
|
71
85
|
earlier.chunk_while { |a, b| a[1] == b[1] }.map do |run|
|
|
@@ -111,20 +125,21 @@ module Samagotchi
|
|
|
111
125
|
PeerSay::POLL_SECONDS = 0.25
|
|
112
126
|
|
|
113
127
|
# What chi peer pair did: :paired (+link+), or a refusal (nothing
|
|
114
|
-
# written): :continued (+next_id+), :
|
|
115
|
-
# the peer mail).
|
|
116
|
-
PeerPaired = Data.define(:status, :link, :next_id) do
|
|
117
|
-
def initialize(status:, link: nil, next_id: nil) = super
|
|
128
|
+
# written): :continued (+next_id+, +in_flight+ as PeerSay's), :busy,
|
|
129
|
+
# :old_worker (a live worker without the peer mail).
|
|
130
|
+
PeerPaired = Data.define(:status, :link, :next_id, :in_flight) do
|
|
131
|
+
def initialize(status:, link: nil, next_id: nil, in_flight: false) = super
|
|
118
132
|
|
|
119
133
|
def refusal
|
|
120
134
|
case status
|
|
121
|
-
when :continued then
|
|
135
|
+
when :continued then PeerSay.continued(next_id, in_flight, "pair with it")
|
|
136
|
+
when :busy then SessionTransition::Busy.new(nil).message
|
|
122
137
|
when :old_worker then PeerSay.refusal(:old_worker, nil, nil)
|
|
123
138
|
end
|
|
124
139
|
end
|
|
125
140
|
end
|
|
126
141
|
|
|
127
|
-
PeerSay::STATUSES = %w[received gone deleting continued not_paired old_worker full].freeze
|
|
142
|
+
PeerSay::STATUSES = %w[received gone deleting continued busy not_paired old_worker full].freeze
|
|
128
143
|
PeerSay::WAKES = %w[running starting stopped archived owned start_failed no_turn not_running deleting].freeze
|
|
129
144
|
# The setting a peer_turns hold names (WorkerWakes::PEER_TURNS_KEY: the
|
|
130
145
|
# CLI loads no worker).
|
|
@@ -100,7 +100,8 @@ module Samagotchi
|
|
|
100
100
|
# @raise [Error] no such session, a one-shot owns it, it wasn't taken
|
|
101
101
|
# (a worker older than queueing refuses a queue; a stopped or
|
|
102
102
|
# archived session: session_stopped / session_archived; one being
|
|
103
|
-
# deleted: session_deleting
|
|
103
|
+
# deleted: session_deleting; one a transition holds: link_continued,
|
|
104
|
+
# transition_busy), or delivery is
|
|
104
105
|
# :cut or unknown
|
|
105
106
|
def send(id, text, delivery: nil)
|
|
106
107
|
raise Error, "nothing to send" if text.to_s.strip.empty?
|
|
@@ -170,10 +171,11 @@ module Samagotchi
|
|
|
170
171
|
|
|
171
172
|
# Stop one of this session's own children as `chi sessions stop` does
|
|
172
173
|
# (by session id; no pid is read here), waiting up to STOP_WAIT s for
|
|
173
|
-
# its worker to let go.
|
|
174
|
+
# its worker to let go. Its transition lock is one try (this worker
|
|
175
|
+
# holds its own owner.lock, so it never waits for one).
|
|
174
176
|
# @return [String] the child's id
|
|
175
|
-
# @raise [Error] not a child of this session, no such session,
|
|
176
|
-
# one-shot owns it
|
|
177
|
+
# @raise [Error] not a child of this session, no such session, a
|
|
178
|
+
# one-shot owns it, or another change holds it (Busy's words)
|
|
177
179
|
def stop(id)
|
|
178
180
|
state_dir = self.state_dir
|
|
179
181
|
sid = resolve(id, state_dir)
|
|
@@ -182,10 +184,12 @@ module Samagotchi
|
|
|
182
184
|
raise Error, "session #{sid[0, 8]} is not a child of this session"
|
|
183
185
|
end
|
|
184
186
|
|
|
185
|
-
SessionManager.stop_session(sid, state_dir: state_dir, wait: STOP_WAIT)
|
|
187
|
+
SessionManager.stop_session(sid, state_dir: state_dir, wait: STOP_WAIT, lock_wait: 0)
|
|
186
188
|
sid
|
|
187
189
|
rescue SessionManager::OwnedByTUI
|
|
188
190
|
raise Error, "session #{sid[0, 8]} is #{OwnerLock::TUI_BUSY}; let it finish"
|
|
191
|
+
rescue SessionTransition::Busy => e
|
|
192
|
+
raise Error, e.message
|
|
189
193
|
end
|
|
190
194
|
|
|
191
195
|
private
|
|
@@ -252,6 +252,8 @@ module Samagotchi
|
|
|
252
252
|
@stdout.puts(" last fired: #{clock(entry.last_fired)} last result: #{entry.last_result || "-"}")
|
|
253
253
|
@stdout.puts(" skips: #{entry.skips} of #{entry.max_skips} failures: #{entry.failures} of #{entry.max_failures}")
|
|
254
254
|
@stdout.puts(" keep_links: #{entry.keep_links} enabled: #{entry.enabled}")
|
|
255
|
+
waiting = Schedules::PrunePart.waiting(entry)
|
|
256
|
+
@stdout.puts(" pruning held at #{waiting.link[0, 8]}: #{waiting.reason} (since #{clock(waiting.at)})") if waiting
|
|
255
257
|
@stdout.puts(" if: #{condition_show(entry)}") unless entry.condition.nil?
|
|
256
258
|
@stdout.puts(" prompt: #{entry.prompt}")
|
|
257
259
|
print_history(entry)
|
|
@@ -336,7 +338,8 @@ module Samagotchi
|
|
|
336
338
|
end
|
|
337
339
|
|
|
338
340
|
# One `list` row: id, name, mode, recurrence, next due, target head, and the
|
|
339
|
-
# notes (skips, why it is disabled, the last result
|
|
341
|
+
# notes (skips, why it is disabled, the last result, the link its prune
|
|
342
|
+
# waits at: Schedules::PrunePart.waiting). +name_width+ is the
|
|
340
343
|
# widest name among the rows printed; without it the name column is just
|
|
341
344
|
# the name. "disabled after N" only when that budget is spent: a one-off
|
|
342
345
|
# whose firing failed (ScheduleDisplay.off_reason "failures") reads
|
|
@@ -347,6 +350,7 @@ module Samagotchi
|
|
|
347
350
|
note += off_note(entry) unless entry.enabled
|
|
348
351
|
note += " if: #{ScheduleDisplay.condition_words(entry, chars: ScheduleDisplay::PROMPT_CHARS)}" unless entry.condition.nil?
|
|
349
352
|
note += " last: #{entry.last_result}" if entry.last_result
|
|
353
|
+
note += " pruning held at #{Schedules::PrunePart.waiting(entry).link[0, 8]}" if Schedules::PrunePart.waiting(entry)
|
|
350
354
|
"#{entry.id} #{ScheduleDisplay.name(entry, width: name_width)} #{entry.mode} #{entry.recurrence_words} " \
|
|
351
355
|
"#{clock(entry.next_due)} #{entry.head[0, 8]}#{note}"
|
|
352
356
|
end
|
|
@@ -1,32 +1,76 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "time"
|
|
3
4
|
require_relative "../archive_store"
|
|
4
5
|
require_relative "../log"
|
|
5
6
|
require_relative "../session"
|
|
6
7
|
require_relative "../session_manager"
|
|
8
|
+
require_relative "../session_transition"
|
|
7
9
|
|
|
8
10
|
module Samagotchi
|
|
9
11
|
class Schedules
|
|
10
12
|
# The chain prune after a firing (docs/scheduling.md, "Keeping the chain
|
|
11
|
-
# short"): the links behind a chain entry's keep_links window go
|
|
13
|
+
# short"): the links behind a chain entry's keep_links window go, oldest
|
|
14
|
+
# first, each under the transition locks of it and the link after it.
|
|
15
|
+
# A link that can't go now holds the prune there (never skipped past:
|
|
16
|
+
# the chain is not deleted around it); the entry records where and why
|
|
17
|
+
# (PRUNE_WAITING, shown by chi schedule list/show), and every tick
|
|
18
|
+
# tries again until it goes.
|
|
12
19
|
module PrunePart
|
|
20
|
+
# The entry's extra key for where its prune waits: {"link", "reason",
|
|
21
|
+
# "at"} (since when it waits at that link). No history row: it is
|
|
22
|
+
# state, cleared once the prune gets past it.
|
|
23
|
+
PRUNE_WAITING = "prune_waiting"
|
|
24
|
+
|
|
25
|
+
# Where an entry's prune waits.
|
|
26
|
+
Waiting = Data.define(:link, :reason, :at)
|
|
27
|
+
|
|
28
|
+
# @return [Waiting, nil]
|
|
29
|
+
def self.waiting(entry)
|
|
30
|
+
value = entry.extra[PRUNE_WAITING]
|
|
31
|
+
return nil unless value.is_a?(Hash) && Session.valid_id?(value["link"])
|
|
32
|
+
|
|
33
|
+
Waiting.new(link: value["link"], reason: value["reason"].to_s, at: value["at"])
|
|
34
|
+
end
|
|
35
|
+
|
|
13
36
|
private
|
|
14
37
|
|
|
38
|
+
# The entries a tick prunes: the chain entries it fired, then every
|
|
39
|
+
# enabled one whose prune waits at a link (#prune tries it again).
|
|
40
|
+
def prunes_due(fired, listed)
|
|
41
|
+
waiting = listed.select do |entry|
|
|
42
|
+
entry.enabled && entry.mode == CHAIN && entry.keep_links.positive? && PrunePart.waiting(entry) &&
|
|
43
|
+
fired.none? { |done| done.id == entry.id }
|
|
44
|
+
end
|
|
45
|
+
fired + waiting
|
|
46
|
+
end
|
|
47
|
+
|
|
15
48
|
# The links behind the kept window, with the delegates that hung under
|
|
16
49
|
# them: archiving a link archives its descendants, and archived sessions
|
|
17
50
|
# are outside the retention sweep, so the delegate pile is the bulk of the
|
|
18
51
|
# archived count. Oldest link first, and within a link its children first
|
|
19
|
-
# (deepest first) and the link last.
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
52
|
+
# (deepest first) and the link last. A link already gone (the one an
|
|
53
|
+
# earlier prune deleted: the walk back from the head ends on its id) is
|
|
54
|
+
# skipped, not a stop, or every prune after the first would stall
|
|
55
|
+
# there. The anchor is never pruned: `last:<anchor>` must keep resolving
|
|
56
|
+
# (SessionChain::Links forwards it over the gap to the head), and it is
|
|
57
|
+
# the id the user remembers. Any other link that can't go now
|
|
58
|
+
# (#prune_hold, or its or the next link's transition lock held) stops
|
|
59
|
+
# the prune below it, recorded (#note_waiting); a schedule failure it
|
|
60
|
+
# is not.
|
|
24
61
|
def prune(entry, links)
|
|
62
|
+
chain = chain_back(entry, links)
|
|
63
|
+
older = chain[entry.keep_links..] || []
|
|
64
|
+
note_waiting(entry, older.empty? ? nil : prune_links(entry, chain, older))
|
|
65
|
+
rescue Session::NotFound => e
|
|
66
|
+
Log.warn(:worker, "prune_missing", id: entry.id, msg: e.message)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The chain from the head back to the anchor, head first.
|
|
70
|
+
def chain_back(entry, links)
|
|
25
71
|
chain = []
|
|
26
72
|
id = entry.head
|
|
27
|
-
|
|
28
|
-
while id && !seen.include?(id)
|
|
29
|
-
seen << id
|
|
73
|
+
while id && !chain.include?(id)
|
|
30
74
|
chain << id
|
|
31
75
|
# The walk stops at the anchor: the links before it are the user's own
|
|
32
76
|
# history, made before this schedule existed, and the anchor itself is
|
|
@@ -38,41 +82,97 @@ module Samagotchi
|
|
|
38
82
|
session = links.session(id) || (File.exist?(Session.session_file(id, state_dir: @state_dir)) ? Session.load(id, state_dir: @state_dir) : nil)
|
|
39
83
|
id = session&.continues
|
|
40
84
|
end
|
|
41
|
-
|
|
42
|
-
|
|
85
|
+
chain
|
|
86
|
+
end
|
|
43
87
|
|
|
88
|
+
# +older+ deleted oldest first, each link under the exclusive
|
|
89
|
+
# transition locks (kind :delete, one try) of it and the link after
|
|
90
|
+
# it, its hold checked again under them.
|
|
91
|
+
# @return [Array(String, String), nil] the link the prune stopped at
|
|
92
|
+
# and why, nil when it got through
|
|
93
|
+
def prune_links(entry, chain, older)
|
|
44
94
|
older.reverse_each do |link|
|
|
45
|
-
next if link == entry.anchor
|
|
46
|
-
break unless ArchiveStore.archived?(Session.session_dir(link, state_dir: @state_dir))
|
|
47
|
-
|
|
48
|
-
tree = SessionManager.delete_tree_ids(link, @state_dir)
|
|
49
|
-
descendants = tree[0...-1]
|
|
50
|
-
# The whole subtree must be ready: a live worker anywhere in it, or a
|
|
51
|
-
# link's descendant that is not archived, stops the walk here rather
|
|
52
|
-
# than half-deleting a tree.
|
|
53
|
-
owned = tree.select { |sid| SessionManager.session_owner(sid, state_dir: @state_dir) }
|
|
54
|
-
unless owned.empty?
|
|
55
|
-
Log.warn(:worker, "prune_waiting", sid: link, id: entry.id, owned: owned.map { |sid| sid[0, 8] })
|
|
56
|
-
break
|
|
57
|
-
end
|
|
95
|
+
next if link == entry.anchor || link_gone?(link)
|
|
58
96
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
97
|
+
reason = prune_hold(link)
|
|
98
|
+
return [link, reason] if reason
|
|
99
|
+
|
|
100
|
+
following = chain[chain.index(link) - 1]
|
|
101
|
+
stopped = SessionTransition.exclusive([link, following], kind: :delete, state_dir: @state_dir, deadline: 0) do
|
|
102
|
+
reason = prune_hold(link)
|
|
103
|
+
next [link, reason] if reason
|
|
104
|
+
|
|
105
|
+
tree = SessionManager.delete_tree_ids(link, @state_dir)
|
|
106
|
+
# Children first (deepest first), the link last: if a delete is
|
|
107
|
+
# refused partway, nothing above it has gone, so the next try
|
|
108
|
+
# finds the same set instead of children under a deleted link.
|
|
109
|
+
SessionManager.delete_ids(tree, state_dir: @state_dir)
|
|
110
|
+
Log.info(:worker, "pruned_link", sid: link, id: entry.id, links: tree.size)
|
|
111
|
+
nil
|
|
63
112
|
end
|
|
113
|
+
return stopped if stopped
|
|
114
|
+
rescue SessionTransition::Busy => e
|
|
115
|
+
return [link, e.message]
|
|
116
|
+
rescue SessionManager::DeleteRefused => e
|
|
117
|
+
return [link, e.session_id == link ? e.message : "#{e.session_id.to_s[0, 8]}: #{e.message}"]
|
|
118
|
+
end
|
|
119
|
+
nil
|
|
120
|
+
end
|
|
64
121
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
122
|
+
# Why +link+ (behind the window) can't go now, nil when it can: not
|
|
123
|
+
# archived (a user brought it back), a schedule's anchor, head or
|
|
124
|
+
# target in its subtree (another entry anchored inside the chain), a
|
|
125
|
+
# live worker in it, or a descendant not archived. The whole subtree
|
|
126
|
+
# must be ready: never half a tree.
|
|
127
|
+
def prune_hold(link)
|
|
128
|
+
return "it is not archived" unless ArchiveStore.archived?(Session.session_dir(link, state_dir: @state_dir))
|
|
129
|
+
|
|
130
|
+
tree = SessionManager.delete_tree_ids(link, @state_dir)
|
|
131
|
+
tree.reverse_each do |sid|
|
|
132
|
+
SessionManager.refuse_protected!(sid, @state_dir)
|
|
133
|
+
rescue SessionManager::DeleteRefused => e
|
|
134
|
+
return sid == link ? e.message : "its delegate #{sid[0, 8]}: #{e.message}"
|
|
71
135
|
end
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
136
|
+
owned = tree.select { |sid| SessionManager.session_owner(sid, state_dir: @state_dir) }
|
|
137
|
+
return "a worker runs in it: #{owned.map { |sid| sid[0, 8] }.join(", ")}" unless owned.empty?
|
|
138
|
+
|
|
139
|
+
open = tree[0...-1].reject { |sid| ArchiveStore.archived?(Session.session_dir(sid, state_dir: @state_dir)) }
|
|
140
|
+
"its delegates are not archived: #{open.map { |sid| sid[0, 8] }.join(", ")}" unless open.empty?
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Record where +entry+'s prune waits (+waiting+: [link, reason]) or
|
|
144
|
+
# that it waits nowhere (nil), under the file's lock, on the stored
|
|
145
|
+
# entry; logged (prune_waiting) once per link it waits at, not every
|
|
146
|
+
# tick. Nothing is written when the entry's record (read with it)
|
|
147
|
+
# says the same.
|
|
148
|
+
def note_waiting(entry, waiting)
|
|
149
|
+
link, reason = waiting
|
|
150
|
+
was = PrunePart.waiting(entry)
|
|
151
|
+
return if was.nil? && link.nil?
|
|
152
|
+
return if was && was.link == link && was.reason == reason
|
|
153
|
+
|
|
154
|
+
logged = false
|
|
155
|
+
write_under_lock do |file|
|
|
156
|
+
position = file.entries.index { |candidate| candidate.is_a?(Entry) && candidate.id == entry.id }
|
|
157
|
+
next unless position
|
|
158
|
+
|
|
159
|
+
stored = file.entries[position]
|
|
160
|
+
before = PrunePart.waiting(stored)
|
|
161
|
+
extra = stored.extra.except(PRUNE_WAITING)
|
|
162
|
+
if link
|
|
163
|
+
at = before&.link == link ? before.at : @clock.call.utc.iso8601
|
|
164
|
+
extra = extra.merge(PRUNE_WAITING => { "link" => link, "reason" => reason, "at" => at })
|
|
165
|
+
logged = before&.link != link
|
|
166
|
+
end
|
|
167
|
+
file.entries[position] = stored.with(extra: extra)
|
|
168
|
+
end
|
|
169
|
+
Log.warn(:worker, "prune_waiting", sid: link, id: entry.id, msg: reason) if logged
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Neither a session file nor a folder: nothing left to delete.
|
|
173
|
+
def link_gone?(link)
|
|
174
|
+
!File.exist?(Session.session_file(link, state_dir: @state_dir)) &&
|
|
175
|
+
!File.directory?(Session.session_dir(link, state_dir: @state_dir))
|
|
76
176
|
end
|
|
77
177
|
end
|
|
78
178
|
end
|
data/lib/samagotchi/schedules.rb
CHANGED
|
@@ -18,6 +18,7 @@ require_relative "delivery"
|
|
|
18
18
|
require_relative "log"
|
|
19
19
|
require_relative "session"
|
|
20
20
|
require_relative "session_chain"
|
|
21
|
+
require_relative "session_chain/successor"
|
|
21
22
|
require_relative "session_inbox"
|
|
22
23
|
require_relative "session_manager"
|
|
23
24
|
require_relative "session_row"
|
|
@@ -80,9 +81,12 @@ module Samagotchi
|
|
|
80
81
|
# A schedule prompt is a chain link's first message or a turn's prompt: the
|
|
81
82
|
# same limit the note inbox uses.
|
|
82
83
|
PROMPT_MAX_BYTES = SessionInbox::NOTE_MAX_BYTES
|
|
83
|
-
# A chain firing can wait 30 s for a recap
|
|
84
|
-
#
|
|
85
|
-
|
|
84
|
+
# A chain firing can wait 30 s for a recap (SessionContinue::RECAP_WAIT),
|
|
85
|
+
# 10 s for the transition locks on each of its 3 tries (LOCK_WAIT,
|
|
86
|
+
# TRIES: a last: continue that meets another's lock follows it) and 5 s
|
|
87
|
+
# for an archive; 5 s more leaves margin before an unfinished turn is
|
|
88
|
+
# called interrupted.
|
|
89
|
+
FIRING_TIMEOUT = 70
|
|
86
90
|
PENDING_MIN_SECONDS = 10 * 60
|
|
87
91
|
PENDING_MAX_SECONDS = 6 * 60 * 60
|
|
88
92
|
# A disabled notice older than this is dropped, not pushed: a chi web that
|
|
@@ -91,8 +95,11 @@ module Samagotchi
|
|
|
91
95
|
# Refusals that mean "not now" rather than "this schedule is broken".
|
|
92
96
|
# (starting: the link's worker holds no lock yet, e.g. another ticker's
|
|
93
97
|
# firing just continued the chain.)
|
|
98
|
+
# (transition_busy: a continue of the chain's end held its transition
|
|
99
|
+
# lock past SessionContinue::LOCK_WAIT, or a same-mode turn met a
|
|
100
|
+
# transition holding its target's lock: DeliverResult::BUSY.)
|
|
94
101
|
SKIP_REASONS = %w[busy queued offer busy_child queued_child offer_child starting starting_child continued
|
|
95
|
-
open_children].freeze
|
|
102
|
+
open_children transition_busy].freeze
|
|
96
103
|
|
|
97
104
|
# The add itself is wrong (no prompt, two recurrence kinds, a scratch
|
|
98
105
|
# target): a usage error, exit 2.
|
|
@@ -256,6 +263,17 @@ module Samagotchi
|
|
|
256
263
|
ambiguous(list.select { |entry| entry.title.to_s.casecmp?(given) }, given).first
|
|
257
264
|
end
|
|
258
265
|
|
|
266
|
+
# anchor → the heads of the schedules anchored there, in the file's order,
|
|
267
|
+
# disabled and fired one-offs too (SessionChain::Links' walk over a pruned
|
|
268
|
+
# anchor's gap: a paused schedule's chain is still one chain). Lenient and
|
|
269
|
+
# quiet: a file that doesn't read forwards nothing.
|
|
270
|
+
# @return [Hash{String => Array<String>}]
|
|
271
|
+
def anchor_heads
|
|
272
|
+
read(quiet: true).entries.grep(Entry).each_with_object({}) do |entry, heads|
|
|
273
|
+
(heads[entry.anchor] ||= []) << entry.head
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
259
277
|
# One match is the answer; several are a usage error naming them, the way
|
|
260
278
|
# Session::AmbiguousId does for session prefixes.
|
|
261
279
|
def ambiguous(entries, given)
|
|
@@ -482,6 +500,7 @@ module Samagotchi
|
|
|
482
500
|
outcomes << outcome
|
|
483
501
|
prune_after << outcome.entry if outcome.status == "fired" && entry.mode == CHAIN && entry.keep_links.positive?
|
|
484
502
|
end
|
|
503
|
+
prune_after = prunes_due(prune_after, started) unless dry_run
|
|
485
504
|
links ||= links_now unless prune_after.empty?
|
|
486
505
|
prune_after.each { |entry| prune(entry, links) }
|
|
487
506
|
Tick.new(locked: false, outcomes: dry_run ? outcomes : mark_disabled(outcomes, started, links: links))
|
|
@@ -913,8 +932,8 @@ module Samagotchi
|
|
|
913
932
|
# listing doesn't have yet; the next tick's walk reaches it. Else nil. A
|
|
914
933
|
# stale intent (no continue holds the lock) is nil: archived it stays.
|
|
915
934
|
def continuing_reason(link)
|
|
916
|
-
successor =
|
|
917
|
-
successor && "
|
|
935
|
+
successor = SessionChain::Successor.in_flight(link, state_dir: @state_dir)
|
|
936
|
+
successor && "#{SessionChain::Continued.for(link, successor).message}; retry at the next tick"
|
|
918
937
|
end
|
|
919
938
|
|
|
920
939
|
# Which chain an entry belongs to, for the one-firing-per-chain-per-tick
|
|
@@ -1015,6 +1034,10 @@ module Samagotchi
|
|
|
1015
1034
|
elsif result.wake_refused?
|
|
1016
1035
|
# Stopped (or archived) since the tick's check: a skip, as the check's.
|
|
1017
1036
|
Outcome.new(id: entry.id, title: entry.title, status: "skipped", detail: result.detail, entry: entry)
|
|
1037
|
+
elsif result.busy? || result.continued?
|
|
1038
|
+
# A transition held the target's lock (a continue naming its new
|
|
1039
|
+
# link, or another change): not now, the next tick tries again.
|
|
1040
|
+
classify(entry, result.continued? ? "continued" : SessionManager::DeliverResult::BUSY, result.detail)
|
|
1018
1041
|
else
|
|
1019
1042
|
Outcome.new(id: entry.id, title: entry.title, status: "failed",
|
|
1020
1043
|
detail: "the turn was not accepted (#{result.status}: #{result.error || result.detail})", entry: entry)
|
|
@@ -820,7 +820,7 @@ module Samagotchi
|
|
|
820
820
|
# A followed send (a head #target resolved) is refused when a continue
|
|
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
|
-
# the first refusal; the old link
|
|
823
|
+
# the first refusal; never to the old link. A busy one goes again, the same link too.
|
|
824
824
|
# @param target [SessionInbox::Resolved] the session (#target)
|
|
825
825
|
# @yieldparam at [SessionInbox::Resolved] each link about to be sent to
|
|
826
826
|
# @yieldreturn [Boolean] false: nothing is sent (its line printed)
|
|
@@ -849,11 +849,11 @@ module Samagotchi
|
|
|
849
849
|
refs = pass.refs
|
|
850
850
|
delivery = pass.delivery
|
|
851
851
|
result = pass.result
|
|
852
|
-
if result.continued?
|
|
852
|
+
if result.continued? || result.busy?
|
|
853
853
|
# Copied in before the refusal: not the old link's to keep.
|
|
854
854
|
@access.discard_images(id, refs)
|
|
855
855
|
deadline ||= monotonic + self.class::CONTINUING_WAIT
|
|
856
|
-
again = follow_again(target, deadline)
|
|
856
|
+
again = follow_again(target, deadline, busy: result.busy?)
|
|
857
857
|
return unsent(again) if again.is_a?(Unsent)
|
|
858
858
|
return deliver(again, prompt, deadline: deadline, &before) if again
|
|
859
859
|
end
|
|
@@ -1030,12 +1030,12 @@ module Samagotchi
|
|
|
1030
1030
|
# (#deliver): Exchange#relocate, until +deadline+. The new link's
|
|
1031
1031
|
# worker is the continue's spawn to start (#delivery_note).
|
|
1032
1032
|
# @return [SessionInbox::Resolved, Unsent, nil] nil: out of time
|
|
1033
|
-
def follow_again(refused, deadline)
|
|
1034
|
-
again = exchange.relocate(refused, deadline) { |look| (@continued_here ||= {})[look.target.id] = true }
|
|
1033
|
+
def follow_again(refused, deadline, busy: false)
|
|
1034
|
+
again = exchange.relocate(refused, deadline, busy: busy) { |look| (@continued_here ||= {})[look.target.id] = true }
|
|
1035
1035
|
return nil unless again
|
|
1036
1036
|
return unsent_of(again) if again.is_a?(SessionAccess::Exchange::Unsent)
|
|
1037
1037
|
|
|
1038
|
-
(@continued_here ||= {})[again.target.id] = true
|
|
1038
|
+
(@continued_here ||= {})[again.target.id] = true unless busy && again.target.id == refused.id
|
|
1039
1039
|
again.target
|
|
1040
1040
|
end
|
|
1041
1041
|
|
|
@@ -132,11 +132,17 @@ module Samagotchi
|
|
|
132
132
|
start ||= @access.prepare_wait(at.id, session: session)
|
|
133
133
|
more = { enqueued_id: enqueued_id, anchors: anchors }.compact
|
|
134
134
|
result = @exchange.attempt(at, prompt, client_id: client_id, queue: queue, **more).result
|
|
135
|
-
if result.continued?
|
|
135
|
+
if result.continued? || result.busy?
|
|
136
136
|
follow_until ||= [@clock.call + continuing_wait, ready_by].min
|
|
137
|
-
again = @exchange.relocate(at, follow_until, bounded: true)
|
|
137
|
+
again = @exchange.relocate(at, follow_until, bounded: true, busy: result.busy?)
|
|
138
138
|
return Refused.new(kind: :canceled, session_id: at.id) if cancelled&.call
|
|
139
|
-
|
|
139
|
+
|
|
140
|
+
unless again
|
|
141
|
+
# Still busy at the deadline: the busy refusal as it came.
|
|
142
|
+
return Refused.new(kind: :undelivered, session_id: at.id, result: result) if result.busy?
|
|
143
|
+
|
|
144
|
+
return Refused.new(kind: :continuing, session_id: at.id, message: result.refusal_text)
|
|
145
|
+
end
|
|
140
146
|
return unsent(again, at.id) if again.is_a?(Exchange::Unsent)
|
|
141
147
|
|
|
142
148
|
at = again.target
|
|
@@ -90,20 +90,24 @@ module Samagotchi
|
|
|
90
90
|
# +refused+'s id (its +asked+) located again after a continue took
|
|
91
91
|
# its link (a DeliverResult#continued?), pausing between looks while
|
|
92
92
|
# it still names that link, until +deadline+ (this clock's seconds).
|
|
93
|
+
# After a busy refusal (DeliverResult#busy?: another transition held
|
|
94
|
+
# the link's lock) the first look after a pause is the answer, the
|
|
95
|
+
# same link again included: the sender tries it again.
|
|
93
96
|
# @param bounded [Boolean] each look's own wait for a continue ends by
|
|
94
97
|
# +deadline+ too (an ask's one deadline); false, it may wait its
|
|
95
98
|
# continuing_wait past it (chi send's)
|
|
99
|
+
# @param busy [Boolean] +refused+ was refused busy
|
|
96
100
|
# @yieldparam located [Located] each look that waited on a continue
|
|
97
101
|
# (#locate's waited), whichever link it named
|
|
98
102
|
# @return [Located, Unsent, nil] nil: out of time
|
|
99
|
-
def relocate(refused, deadline, bounded: false)
|
|
103
|
+
def relocate(refused, deadline, bounded: false, busy: false)
|
|
100
104
|
while @clock.call < deadline && !@cancelled.call
|
|
101
105
|
pause_until(deadline)
|
|
102
106
|
again = locate(refused.asked, deadline: (deadline if bounded))
|
|
103
107
|
return again unless again.is_a?(Located)
|
|
104
108
|
|
|
105
109
|
yield again if block_given? && again.waited
|
|
106
|
-
next if again.target.id == refused.id
|
|
110
|
+
next if !busy && again.target.id == refused.id
|
|
107
111
|
|
|
108
112
|
return again
|
|
109
113
|
end
|