samagotchi 0.177.0 → 0.177.1
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 +18 -1
- data/docs/architecture.md +5 -4
- data/docs/cli.md +4 -1
- data/docs/internals/bundles.md +4 -3
- data/docs/internals/participants.md +8 -5
- data/docs/internals/session-lifecycle.md +167 -0
- data/docs/internals/web-frontend.md +7 -3
- data/docs/internals/working-set.md +2 -1
- data/docs/plugins.md +1 -1
- data/docs/releasing.md +3 -2
- data/docs/sessions.md +7 -5
- data/docs/testing.md +4 -2
- data/lib/samagotchi/bundle_command/check.rb +1 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/compat.rb +1 -1
- data/lib/samagotchi/engine/session.rb +7 -0
- data/lib/samagotchi/memory_bundle/shipped_update.rb +1 -1
- data/lib/samagotchi/note_delivery.rb +9 -4
- data/lib/samagotchi/plugin/sessions.rb +2 -1
- data/lib/samagotchi/revise_anchor.rb +2 -0
- data/lib/samagotchi/schedules.rb +3 -1
- data/lib/samagotchi/session_chain/delete.rb +28 -5
- data/lib/samagotchi/session_chain/delete_preview.rb +2 -2
- data/lib/samagotchi/session_inbox.rb +29 -0
- data/lib/samagotchi/session_inbox_delivery.rb +13 -5
- data/lib/samagotchi/session_lifecycle/allowed.rb +66 -0
- data/lib/samagotchi/session_lifecycle/canonical.rb +49 -0
- data/lib/samagotchi/session_lifecycle/reading.rb +65 -0
- data/lib/samagotchi/session_lifecycle.rb +59 -0
- data/lib/samagotchi/session_manager/delivery.rb +29 -6
- data/lib/samagotchi/session_manager/lifecycle.rb +22 -14
- data/lib/samagotchi/session_manager/removal.rb +16 -8
- data/lib/samagotchi/session_manager/spawn.rb +2 -2
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/system_prompt.rb +22 -2
- data/lib/samagotchi/tool_names.rb +1 -1
- data/lib/samagotchi/tools/memory.rb +29 -0
- data/lib/samagotchi/turn_anchors.rb +5 -0
- data/lib/samagotchi/verify_anchor.rb +2 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/participant_show.rb +3 -4
- data/lib/samagotchi/web/participant_view.rb +4 -6
- data/lib/samagotchi/web/public/data.js +6 -2
- data/lib/samagotchi/web/public/delete_sheet.js +42 -5
- data/lib/samagotchi/web/public/ws_sections.js +1 -0
- data/lib/samagotchi/web/session_control.rb +4 -3
- data/lib/samagotchi/web/session_summary.rb +7 -0
- data/lib/samagotchi/worker_boot.rb +9 -6
- data/lib/samagotchi/worker_presence.rb +4 -0
- data/lib/samagotchi/working_set_command.rb +2 -2
- data/lib/samagotchi/working_set_steer.rb +3 -10
- metadata +6 -1
|
@@ -147,7 +147,7 @@ module Samagotchi
|
|
|
147
147
|
end
|
|
148
148
|
|
|
149
149
|
# A source with hooks/*.rb (listed or not) can't be installed: the run
|
|
150
|
-
# stops there, as after a manifest error (
|
|
150
|
+
# stops there, as after a manifest error (Manifest::HooksRetired.refuse_source_files).
|
|
151
151
|
def hooks_retired?
|
|
152
152
|
files = MemoryBundle::Manifest::HooksRetired.files_in(@dir)
|
|
153
153
|
return false if files.empty?
|
data/lib/samagotchi/compat.rb
CHANGED
|
@@ -18,7 +18,7 @@ module Samagotchi
|
|
|
18
18
|
MIN_AGE_DAYS = 14
|
|
19
19
|
|
|
20
20
|
SHIMS = [
|
|
21
|
-
Shim.new(id: :cards_v1_seed, added_in: "0.151.0", added_on: "2026-11-05", remove_after: "0.
|
|
21
|
+
Shim.new(id: :cards_v1_seed, added_in: "0.151.0", added_on: "2026-11-05", remove_after: "0.178.0",
|
|
22
22
|
files: ["lib/samagotchi/bridge/card_store.rb"],
|
|
23
23
|
what: "CardStore#seed_v1 reads a cards.json from before the turn log; its comment says a retention " \
|
|
24
24
|
"period after the release that wrote version 2 (0.151.0, 2026-11-05)"),
|
|
@@ -29,6 +29,13 @@ module Samagotchi
|
|
|
29
29
|
names.map { |name| name.to_s.strip }.reject(&:empty?).uniq.sort
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# @return [Array<Array(String, String, nil)>] prompt memory names and scopes
|
|
33
|
+
def prompt_memories
|
|
34
|
+
memories = Array(@prompt_builder.prompt_memories) + Array(prompt_notes).map { |note| [note.name, note.scope] }
|
|
35
|
+
memories.map { |name, scope| [name.to_s.strip, scope] }.reject { |name, _| name.empty? }.uniq
|
|
36
|
+
.sort_by { |name, scope| [name, scope.to_s] }
|
|
37
|
+
end
|
|
38
|
+
|
|
32
39
|
# @return [Array<String>] the names the session preloads (config baseline
|
|
33
40
|
# + --memory, minus mutes), known before the prompt is built, unlike
|
|
34
41
|
# #activated_memory_names
|
|
@@ -203,7 +203,7 @@ module Samagotchi
|
|
|
203
203
|
return skip(row, "needs chi #{manifest.requires_chi}", keep_to: true)
|
|
204
204
|
end
|
|
205
205
|
|
|
206
|
-
# The upgrade would refuse it (
|
|
206
|
+
# The upgrade would refuse it (Manifest::HooksRetired.refuse_source_files).
|
|
207
207
|
hooks = Manifest::HooksRetired.files_in(row.source_dir)
|
|
208
208
|
return skip(row, Manifest::HooksRetired.for_files(hooks).message, keep_to: true) unless hooks.empty?
|
|
209
209
|
return skip(row, "project scope: chi bundle upgrade #{ship.source} in the project") if row.scope == "project"
|
|
@@ -48,16 +48,21 @@ module Samagotchi
|
|
|
48
48
|
# resolved already (SessionInbox::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
|
-
# @raise [Session::NotFound] no such session
|
|
51
|
+
# @raise [Session::NotFound] no such session, or one being deleted
|
|
52
52
|
def deliver(id, text:, source:, state_dir:, from_session: nil, from_cwd: nil, objectives: nil, pin: false,
|
|
53
53
|
resolved: nil)
|
|
54
54
|
body = SessionInbox.checked_text(text)
|
|
55
|
+
path = nil
|
|
55
56
|
delivered = SessionInbox.deliver(:note, id, state_dir: state_dir, pin: pin, resolved: resolved) do |at|
|
|
56
|
-
SessionInbox.write_note(at, text: body, source: source, from_session: from_session, from_cwd: from_cwd,
|
|
57
|
-
|
|
57
|
+
path = SessionInbox.write_note(at, text: body, source: source, from_session: from_session, from_cwd: from_cwd,
|
|
58
|
+
objectives: objectives, state_dir: state_dir)
|
|
58
59
|
false
|
|
59
60
|
end
|
|
60
|
-
|
|
61
|
+
# A session being deleted is a gone one; a delete that took its lock
|
|
62
|
+
# after the write gets the note back (unless a worker read it already).
|
|
63
|
+
raise Session::NotFound, id if delivered.wake == :gone ||
|
|
64
|
+
(delivered.wake == :deleting &&
|
|
65
|
+
(path.nil? || SessionInbox.withdraw(path, delivered.id, state_dir: state_dir)))
|
|
61
66
|
|
|
62
67
|
Result.new(id: delivered.id, asked: delivered.asked, status: delivered.status, queued: delivered.queued || 0,
|
|
63
68
|
scratch: delivered.refused? && delivered.wake == :scratch)
|
|
@@ -99,7 +99,8 @@ module Samagotchi
|
|
|
99
99
|
# @return [String] the id of the session it went to
|
|
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
|
-
# archived session: session_stopped / session_archived
|
|
102
|
+
# archived session: session_stopped / session_archived; one being
|
|
103
|
+
# deleted: session_deleting), or delivery is
|
|
103
104
|
# :cut or unknown
|
|
104
105
|
def send(id, text, delivery: nil)
|
|
105
106
|
raise Error, "nothing to send" if text.to_s.strip.empty?
|
data/lib/samagotchi/schedules.rb
CHANGED
|
@@ -1002,7 +1002,9 @@ module Samagotchi
|
|
|
1002
1002
|
# an open session is fine, so same mode does not count streams.
|
|
1003
1003
|
return [:skip, "a terminal or a web tab is open on it"] if entry.mode == CHAIN && activity[:open_streams].to_i.positive?
|
|
1004
1004
|
|
|
1005
|
-
|
|
1005
|
+
# An idle live worker: free, whatever a starting marker its own
|
|
1006
|
+
# start left (Worker#start clears it just after the lock).
|
|
1007
|
+
return nil
|
|
1006
1008
|
end
|
|
1007
1009
|
# A worker being started for it holds no lock yet (another ticker's
|
|
1008
1010
|
# firing just continued the chain to it, a resume): a chain firing
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "digest"
|
|
3
4
|
require "json"
|
|
4
5
|
require_relative "links"
|
|
5
6
|
require_relative "../archive_store"
|
|
@@ -117,6 +118,22 @@ module Samagotchi
|
|
|
117
118
|
cut_off: links.gone?(links.chain(id).first))
|
|
118
119
|
end
|
|
119
120
|
|
|
121
|
+
# The ids each delete choice shows, encoded as stable, sorted sets.
|
|
122
|
+
def digest(plan)
|
|
123
|
+
canonical = case plan
|
|
124
|
+
when LinkPlan
|
|
125
|
+
[[plan.id], [plan.previous].compact, plan.revive, plan.moved,
|
|
126
|
+
plan.delegates.delete, plan.delegates.leave].map(&:sort)
|
|
127
|
+
when ChainPlan
|
|
128
|
+
[plan.links.sort, *plan.delegates.sort.map do |link, delegates|
|
|
129
|
+
[link, delegates.delete.sort, delegates.leave.sort]
|
|
130
|
+
end]
|
|
131
|
+
else
|
|
132
|
+
raise ArgumentError, "unknown delete plan: #{plan.class}"
|
|
133
|
+
end
|
|
134
|
+
Digest::SHA256.hexdigest(JSON.generate(canonical))[0, 16]
|
|
135
|
+
end
|
|
136
|
+
|
|
120
137
|
# Whether +id+ is in a chain of 2 or more links that still exist (D1):
|
|
121
138
|
# a link alone past a gap (its predecessor deleted) is deleted as a
|
|
122
139
|
# session in no chain is.
|
|
@@ -130,17 +147,19 @@ module Samagotchi
|
|
|
130
147
|
# @raise [SessionManager::DeleteRefused] :continuing, :scheduled,
|
|
131
148
|
# :delegate_unmovable, :worker_running, :still_stopping
|
|
132
149
|
# @raise [SessionManager::OwnedByTUI]
|
|
133
|
-
def link(id, state_dir:, stop:, wait:, revive_previous:, refuse_continuing:)
|
|
150
|
+
def link(id, state_dir:, stop:, wait:, revive_previous:, refuse_continuing:, expect: nil)
|
|
134
151
|
ContinueIntent.hold(id, state_dir: state_dir) do |held|
|
|
135
152
|
raise SessionManager::DeleteRefused.new(id, :continuing) if refuse_continuing && !held
|
|
136
153
|
|
|
137
154
|
links = Links.new(state_dir: state_dir)
|
|
138
155
|
unless chained?(id, links)
|
|
156
|
+
raise SessionManager::DeleteRefused.new(id, :plan_changed) unless expect.nil?
|
|
157
|
+
|
|
139
158
|
return plain(SessionManager.delete_resolved(id, state_dir: state_dir, stop: stop, wait: wait))
|
|
140
159
|
end
|
|
141
160
|
|
|
142
161
|
plan = link_plan(id, state_dir: state_dir, links: links, undo: revive_previous)
|
|
143
|
-
return delete_link(plan, state_dir: state_dir, stop: stop, wait: wait) unless plan.previous
|
|
162
|
+
return delete_link(plan, state_dir: state_dir, stop: stop, wait: wait, expect: expect) unless plan.previous
|
|
144
163
|
|
|
145
164
|
# Taken before the link's file goes: after it, a continue of the
|
|
146
165
|
# previous link would no longer be refused. The plan is made again
|
|
@@ -149,7 +168,7 @@ module Samagotchi
|
|
|
149
168
|
raise SessionManager::DeleteRefused.new(plan.previous, :continuing) unless previous_held
|
|
150
169
|
|
|
151
170
|
again = link_plan(id, state_dir: state_dir, links: Links.new(state_dir: state_dir), undo: true)
|
|
152
|
-
return delete_link(again, state_dir: state_dir, stop: stop, wait: wait)
|
|
171
|
+
return delete_link(again, state_dir: state_dir, stop: stop, wait: wait, expect: expect)
|
|
153
172
|
end
|
|
154
173
|
end
|
|
155
174
|
end
|
|
@@ -180,7 +199,9 @@ module Samagotchi
|
|
|
180
199
|
# files. So a refusal (a schedule on a delegate, a one-turn run, a
|
|
181
200
|
# worker that took the link or a delegate after the checks) leaves
|
|
182
201
|
# every session in place.
|
|
183
|
-
def delete_link(plan, state_dir:, stop:, wait:)
|
|
202
|
+
def delete_link(plan, state_dir:, stop:, wait:, expect: nil)
|
|
203
|
+
raise SessionManager::DeleteRefused.new(plan.id, :plan_changed) if !expect.nil? && expect != digest(plan)
|
|
204
|
+
|
|
184
205
|
id = plan.id
|
|
185
206
|
check_link!(plan, state_dir)
|
|
186
207
|
stopped = SessionManager.release_for_delete!(id, state_dir: state_dir, stop: stop, wait: wait)
|
|
@@ -314,9 +335,11 @@ module Samagotchi
|
|
|
314
335
|
# earlier ones; refusal: the message of a part-way refusal, or nil)
|
|
315
336
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
316
337
|
# nothing was deleted
|
|
317
|
-
def chain(id, state_dir:, stop:, wait:)
|
|
338
|
+
def chain(id, state_dir:, stop:, wait:, expect: nil)
|
|
318
339
|
links = Links.new(state_dir: state_dir)
|
|
319
340
|
plan = chain_plan(id, state_dir: state_dir, links: links)
|
|
341
|
+
raise SessionManager::DeleteRefused.new(plan.id, :plan_changed) if !expect.nil? && expect != digest(plan)
|
|
342
|
+
|
|
320
343
|
check_chain!(plan, state_dir, stop: stop)
|
|
321
344
|
hold_all(plan.links, state_dir) do
|
|
322
345
|
stopped = plan.links.select do |link|
|
|
@@ -35,7 +35,7 @@ module Samagotchi
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def link_json(plan, links, whole, at, state_dir)
|
|
38
|
-
{ previous_id: plan.previous, moved: moved_count(plan, state_dir), notes: plan.notes, unarchived: plan.revive.size,
|
|
38
|
+
{ digest: Delete.digest(plan), previous_id: plan.previous, moved: moved_count(plan, state_dir), notes: plan.notes, unarchived: plan.revive.size,
|
|
39
39
|
deletes: plan.delegates.delete.size, left_running: plan.delegates.leave.size,
|
|
40
40
|
earlier: at, next_id: links.next_of(plan.id), later: whole.links.size - at - 1,
|
|
41
41
|
refusal: refusal(state_dir) { link_checks(plan, state_dir) } }
|
|
@@ -48,7 +48,7 @@ module Samagotchi
|
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
def chain_json(plan, state_dir)
|
|
51
|
-
{ links: plan.links.size, deletes: plan.delegates.values.sum { |d| d.delete.size },
|
|
51
|
+
{ digest: Delete.digest(plan), links: plan.links.size, deletes: plan.delegates.values.sum { |d| d.delete.size },
|
|
52
52
|
left_running: plan.delegates.values.sum { |d| d.leave.size },
|
|
53
53
|
refusal: refusal(state_dir) { chain_checks(plan, state_dir) } }
|
|
54
54
|
end
|
|
@@ -94,6 +94,35 @@ module Samagotchi
|
|
|
94
94
|
false
|
|
95
95
|
end
|
|
96
96
|
|
|
97
|
+
# Take back a file a delivery wrote (an input, a note) once it read
|
|
98
|
+
# the session as being deleted (WakePolicy's :deleting, or gone) after
|
|
99
|
+
# its write: the delete took the lock between its check and its write.
|
|
100
|
+
# @return [Boolean] true when nobody has it: removed now, or gone with
|
|
101
|
+
# the session (its folder, if the write made it again, is removed when
|
|
102
|
+
# empty); false when a worker claimed it meanwhile (renamed to
|
|
103
|
+
# .processing: the delete gave up and the file is delivered after all)
|
|
104
|
+
def self.withdraw(path, session_id, state_dir:)
|
|
105
|
+
removed = begin
|
|
106
|
+
File.delete(path)
|
|
107
|
+
true
|
|
108
|
+
rescue Errno::ENOENT
|
|
109
|
+
false
|
|
110
|
+
end
|
|
111
|
+
return removed if Session.exist?(session_id, state_dir: state_dir)
|
|
112
|
+
|
|
113
|
+
remove_left_folder(File.dirname(path), session_id, state_dir)
|
|
114
|
+
true
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# A gone session's folder that a late write made again (mkdir_p),
|
|
118
|
+
# inbox folder first, both only while empty.
|
|
119
|
+
def self.remove_left_folder(inbox_dir, session_id, state_dir)
|
|
120
|
+
[inbox_dir, Session.session_dir(session_id, state_dir: state_dir)].each { |dir| Dir.rmdir(dir) }
|
|
121
|
+
rescue SystemCallError
|
|
122
|
+
nil
|
|
123
|
+
end
|
|
124
|
+
private_class_method :remove_left_folder
|
|
125
|
+
|
|
97
126
|
# Queue a context note for a session: background text its worker adds
|
|
98
127
|
# to the conversation between turns. It never starts a turn.
|
|
99
128
|
# @param source [String] where it came from ("cli", "slack", "session")
|
|
@@ -54,8 +54,11 @@ module Samagotchi
|
|
|
54
54
|
# worker sees it: one runs, is starting or was started) or :waits (no
|
|
55
55
|
# worker comes: it waits for the next start)
|
|
56
56
|
# @!attribute wake [Symbol] WakePolicy#decide's outcome, :woken for a
|
|
57
|
-
# start made, or the refusal (:gone, :
|
|
58
|
-
# :scratch)
|
|
57
|
+
# start made, or the refusal (:gone, :deleting: a deleter holds its
|
|
58
|
+
# lock, :owned: a one-turn chi run's, :scratch). :deleting after the
|
|
59
|
+
# write (status :waits) is a delete that took the lock meanwhile: the
|
|
60
|
+
# file goes with the session (a caller that reports the delivery takes
|
|
61
|
+
# it back, SessionInbox.withdraw)
|
|
59
62
|
# @!attribute queued [Integer, nil] notes waiting (a note kind's), nil else
|
|
60
63
|
Delivered = Data.define(:kind, :id, :asked, :via, :status, :wake, :queued) do
|
|
61
64
|
def initialize(kind:, id:, asked:, via:, status:, wake:, queued: nil) = super
|
|
@@ -93,19 +96,22 @@ module Samagotchi
|
|
|
93
96
|
def self.for_sender(client_id) = self.for(ClientId.wake_kind(client_id))
|
|
94
97
|
|
|
95
98
|
# Why the delivery is refused before anything is written, else nil:
|
|
96
|
-
# :gone (no session), :
|
|
99
|
+
# :gone (no session), :deleting (a deleter holds its lock: what is
|
|
100
|
+
# written goes with it), :owned (a one-turn chi run's), :scratch.
|
|
97
101
|
# @param load [#call, nil] the session, read only when the scratch
|
|
98
102
|
# flag decides (nil: not a scratch session)
|
|
99
103
|
# @return [Symbol, nil]
|
|
100
104
|
def refusal(presence, load: nil)
|
|
101
105
|
return :gone unless presence.exists?
|
|
106
|
+
return :deleting if presence.deleting?
|
|
102
107
|
return :owned if refuse_tui && presence.tui_owned?
|
|
103
108
|
|
|
104
109
|
:scratch if refuse_scratch && scratch?(load)
|
|
105
110
|
end
|
|
106
111
|
|
|
107
112
|
# What the delivery means for the session's worker, in this order:
|
|
108
|
-
# :gone, :owned (a one-turn chi run's), :
|
|
113
|
+
# :gone, :owned (a one-turn chi run's), :deleting (a deleter holds the
|
|
114
|
+
# lock: the session is going, no worker sees it), :live (a worker holds it: it
|
|
109
115
|
# sees the delivery), :stopped (the stop marker: the only "stopped",
|
|
110
116
|
# the file's status never decides), :archived, :starting (a worker is
|
|
111
117
|
# being started: it reads the store on its first pass), :none (the
|
|
@@ -118,6 +124,7 @@ module Samagotchi
|
|
|
118
124
|
def decide(presence, wake: true, load: nil)
|
|
119
125
|
return :gone unless presence.exists?
|
|
120
126
|
return :owned if presence.tui_owned?
|
|
127
|
+
return :deleting if presence.deleting?
|
|
121
128
|
return :live if presence.owned?
|
|
122
129
|
return :stopped if presence.stop_marked? && !unstops
|
|
123
130
|
return :archived if presence.archived? && !starts_archived
|
|
@@ -279,9 +286,10 @@ module Samagotchi
|
|
|
279
286
|
start: start)
|
|
280
287
|
end
|
|
281
288
|
|
|
289
|
+
# Every kind reads the presence: a session being deleted refuses
|
|
290
|
+
# them all (WakePolicy#refusal).
|
|
282
291
|
def refusal(policy, id, state_dir, owner)
|
|
283
292
|
return :gone unless Session.exist?(id, state_dir: state_dir)
|
|
284
|
-
return nil unless policy.refuse_tui || policy.refuse_scratch
|
|
285
293
|
|
|
286
294
|
policy.refusal(presence(id, state_dir, owner), load: -> { Session.load(id, state_dir: state_dir) })
|
|
287
295
|
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../session_inbox"
|
|
4
|
+
require_relative "../session_lifecycle"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
module SessionLifecycle
|
|
8
|
+
# What a delivery's WakePolicy#decide outcome is as a cell: a worker
|
|
9
|
+
# sees it (:live) or one is started (:start): go; it waits on disk for
|
|
10
|
+
# whoever runs the session next: wait; :gone, :owned and :deleting (a
|
|
11
|
+
# deleter holds the lock) are refusals. (:off and :scratch are
|
|
12
|
+
# op-local: a delivery that asks no worker, a scratch session's file.)
|
|
13
|
+
DELIVERY_CELLS = { start: :go, live: :go, starting: :wait, stopped: :wait, archived: :wait, none: :wait,
|
|
14
|
+
gone: :gone, owned: :owned, deleting: :deleting }.freeze
|
|
15
|
+
|
|
16
|
+
# The operations that are not inbox deliveries, in the order of LITERAL's
|
|
17
|
+
# rows: resume_session, restart_session, stop_session, archive
|
|
18
|
+
# (archivable!), delete (release_for_delete!, stop: false), a schedule's
|
|
19
|
+
# busy check in chain and in same mode (Schedules#busy_reason), and a
|
|
20
|
+
# terminal attaching (TerminalUI::LocalLink#live_worker).
|
|
21
|
+
LITERAL_OPS = %i[resume restart stop archive delete sched_chain sched_same attach].freeze
|
|
22
|
+
|
|
23
|
+
# What each literal operation answers in each state today, with neutral
|
|
24
|
+
# op-local facts (not scratch, saved status idle, no input/, no question
|
|
25
|
+
# or offer, not SIGSTOPped, features restart and activity, an idle worker
|
|
26
|
+
# with no streams). worker_busy is a schedule's "its worker is starting
|
|
27
|
+
# or leaving"; resume's live is "a worker holds it"; restart's fallback
|
|
28
|
+
# is its unresponsive-worker path, its stop_marked a stop marked (it
|
|
29
|
+
# reads this table: restart_session's precheck); attach's gone_* are LocalLink's
|
|
30
|
+
# [:gone, why]. absent's delete and schedule cells are unreachable (a
|
|
31
|
+
# gone session is refused before the gate). Resolution (a chain's head,
|
|
32
|
+
# a continue's successor) stays outside: these are the link's own.
|
|
33
|
+
LITERAL = {
|
|
34
|
+
absent: %i[gone gone gone gone go go go wait],
|
|
35
|
+
tui_owned: %i[owned_by_tui owned_by_tui owned_by_tui owned_by_tui owned_by_tui go go wait],
|
|
36
|
+
deleting: %i[live starting go go worker_running worker_busy worker_busy wait],
|
|
37
|
+
stopping: %i[live stop_marked go go worker_running stopped stopped gone_stopped],
|
|
38
|
+
skew: %i[live fallback go go worker_running worker_busy worker_busy gone_worker_newer],
|
|
39
|
+
leaving: %i[live not_running go go worker_running worker_busy worker_busy wait],
|
|
40
|
+
booting_stuck: %i[live fallback go go worker_running worker_busy worker_busy wait],
|
|
41
|
+
booting: %i[live starting go go worker_running worker_busy worker_busy wait],
|
|
42
|
+
live: %i[live go go go worker_running go go go],
|
|
43
|
+
stopped_starting: %i[go stop_marked go starting worker_running stopped stopped gone_stopped],
|
|
44
|
+
stopped: %i[go stop_marked go go go stopped stopped gone_stopped],
|
|
45
|
+
archived_starting: %i[starting not_running go starting worker_running worker_busy go wait],
|
|
46
|
+
starting: %i[starting not_running go starting worker_running worker_busy go wait],
|
|
47
|
+
archived: %i[go not_running go go go go go wait],
|
|
48
|
+
idle: %i[go not_running go go go go go wait]
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
# {op => {state => cell}}: every operation in every state (STATES order).
|
|
52
|
+
# The delivery columns are generated from SessionInbox::WakePolicy::ROWS
|
|
53
|
+
# (each row's #decide on CANONICAL, wake asked, no scratch file), so a
|
|
54
|
+
# row change moves its column; the literal columns are goldens of what
|
|
55
|
+
# the deciders do, pinned by spec/session_lifecycle/matrix_spec.rb.
|
|
56
|
+
ALLOWED = lambda do
|
|
57
|
+
deliveries = SessionInbox::WakePolicy::ROWS.to_h do |kind, policy|
|
|
58
|
+
[kind, STATES.to_h { |state| [state, DELIVERY_CELLS.fetch(policy.decide(CANONICAL.fetch(state), wake: true, load: nil))] }.freeze]
|
|
59
|
+
end
|
|
60
|
+
literal = LITERAL_OPS.each_with_index.to_h do |op, index|
|
|
61
|
+
[op, STATES.to_h { |state| [state, LITERAL.fetch(state).fetch(index)] }.freeze]
|
|
62
|
+
end
|
|
63
|
+
deliveries.merge(literal).freeze
|
|
64
|
+
end.call
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../owner_lock"
|
|
4
|
+
require_relative "../worker_link"
|
|
5
|
+
require_relative "../worker_presence"
|
|
6
|
+
require_relative "../worker_sidecar"
|
|
7
|
+
require_relative "../session_lifecycle"
|
|
8
|
+
|
|
9
|
+
module Samagotchi
|
|
10
|
+
module SessionLifecycle
|
|
11
|
+
# The fewest facts that make each state (STATES order), as a
|
|
12
|
+
# WorkerPresence with its link: what ALLOWED's delivery columns are
|
|
13
|
+
# decided on. Each derives back to its own state (Reading.derive). A
|
|
14
|
+
# compound state takes the decided facts: stopping is a worker whose
|
|
15
|
+
# Bridge answers, with a stop; stopped_starting a start marked before
|
|
16
|
+
# the stop (one marked after it holds a resume off: a row fact the
|
|
17
|
+
# characterization matrix keeps).
|
|
18
|
+
CANONICAL = lambda do
|
|
19
|
+
worker = OwnerLock::Owner.new(pid: 1, kind: "worker")
|
|
20
|
+
sidecar = WorkerSidecar.new(port: 1, pid: 1)
|
|
21
|
+
live = WorkerLink::Live.new(sidecar: sidecar, owner: worker)
|
|
22
|
+
at = lambda do |owner: nil, link: WorkerLink::None.new(owner: owner), exists: true, stop: false, start: false, archived: false|
|
|
23
|
+
WorkerPresence.new(session_id: "canonical", exists: exists, owner: owner, stop_marked: stop,
|
|
24
|
+
starting_marked: start, archived: archived, link: link)
|
|
25
|
+
end
|
|
26
|
+
tui = OwnerLock::Owner.new(pid: 1, kind: "tui")
|
|
27
|
+
deleting = OwnerLock::Owner.new(pid: 1, kind: OwnerLock::DELETING)
|
|
28
|
+
leaving = worker.with(leaving_at: "2020-01-01T00:00:00Z", leave_wait: 1.0)
|
|
29
|
+
{
|
|
30
|
+
absent: at.call(exists: false),
|
|
31
|
+
tui_owned: at.call(owner: tui),
|
|
32
|
+
deleting: at.call(owner: deleting, link: WorkerLink::Starting.new(owner: deleting)),
|
|
33
|
+
stopping: at.call(owner: worker, link: live, stop: true),
|
|
34
|
+
skew: at.call(owner: worker, link: WorkerLink::Skew.new(sidecar: sidecar.with(protocol: WorkerLink::PROTOCOL + 1),
|
|
35
|
+
owner: worker, reason: :protocol)),
|
|
36
|
+
leaving: at.call(owner: leaving, link: WorkerLink::Leaving.new(owner: leaving)),
|
|
37
|
+
booting_stuck: at.call(owner: worker, link: WorkerLink::Starting.new(owner: worker, stuck: true)),
|
|
38
|
+
booting: at.call(owner: worker, link: WorkerLink::Starting.new(owner: worker)),
|
|
39
|
+
live: at.call(owner: worker, link: live),
|
|
40
|
+
stopped_starting: at.call(stop: true, start: true),
|
|
41
|
+
stopped: at.call(stop: true),
|
|
42
|
+
archived_starting: at.call(archived: true, start: true),
|
|
43
|
+
starting: at.call(start: true),
|
|
44
|
+
archived: at.call(archived: true),
|
|
45
|
+
idle: at.call
|
|
46
|
+
}.freeze
|
|
47
|
+
end.call
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../worker_link"
|
|
4
|
+
require_relative "../worker_presence"
|
|
5
|
+
require_relative "../session_lifecycle"
|
|
6
|
+
|
|
7
|
+
module Samagotchi
|
|
8
|
+
module SessionLifecycle
|
|
9
|
+
# One read of a session's lifecycle: the state (one of STATES), the
|
|
10
|
+
# facts it was derived from, and the successor of a continue in flight.
|
|
11
|
+
# @!attribute state [Symbol] one of STATES
|
|
12
|
+
# @!attribute presence [WorkerPresence] read with its link (:look)
|
|
13
|
+
# @!attribute continuing [String, nil] ContinueIntent.in_flight's
|
|
14
|
+
# successor: a fact, not a state (only a send's or a schedule's link
|
|
15
|
+
# resolution reads it)
|
|
16
|
+
Reading = Data.define(:state, :presence, :continuing) do
|
|
17
|
+
# @param presence [WorkerPresence] with its link read
|
|
18
|
+
# @param continuing [String, nil]
|
|
19
|
+
# @return [Reading]
|
|
20
|
+
def self.derive(presence, continuing: nil)
|
|
21
|
+
new(state: state_of(presence), presence: presence, continuing: continuing)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The first of STATES whose facts hold. A lock held by someone that is
|
|
25
|
+
# neither a one-turn run nor a deleter, without a worker's link (a lock
|
|
26
|
+
# record still blank: WorkerLink reads it Starting), is booting.
|
|
27
|
+
# @return [Symbol]
|
|
28
|
+
def self.state_of(presence)
|
|
29
|
+
return :absent unless presence.exists?
|
|
30
|
+
|
|
31
|
+
owner = presence.owner
|
|
32
|
+
return :tui_owned if owner&.tui?
|
|
33
|
+
return :deleting if owner&.deleting?
|
|
34
|
+
return :stopping if owner&.worker? && presence.stop_marked?
|
|
35
|
+
return link_state(presence.link) if owner
|
|
36
|
+
|
|
37
|
+
unowned_state(presence)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.link_state(link)
|
|
41
|
+
case link
|
|
42
|
+
when WorkerLink::Skew then :skew
|
|
43
|
+
when WorkerLink::Leaving then :leaving
|
|
44
|
+
when WorkerLink::Live then :live
|
|
45
|
+
when WorkerLink::Starting then link.stuck ? :booting_stuck : :booting
|
|
46
|
+
else :booting
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
private_class_method :link_state
|
|
50
|
+
|
|
51
|
+
def self.unowned_state(presence)
|
|
52
|
+
stop = presence.stop_marked?
|
|
53
|
+
start = presence.starting?
|
|
54
|
+
return start ? :stopped_starting : :stopped if stop
|
|
55
|
+
return start ? :archived_starting : :archived if presence.archived?
|
|
56
|
+
|
|
57
|
+
start ? :starting : :idle
|
|
58
|
+
end
|
|
59
|
+
private_class_method :unowned_state
|
|
60
|
+
|
|
61
|
+
def facts = presence
|
|
62
|
+
def continuing? = !continuing.nil?
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "session"
|
|
4
|
+
require_relative "worker_presence"
|
|
5
|
+
require_relative "continue_intent"
|
|
6
|
+
|
|
7
|
+
module Samagotchi
|
|
8
|
+
# A session's lifecycle state, derived from what is on disk now: one read of
|
|
9
|
+
# the liveness facts (WorkerPresence, with the WorkerLink looked at, never
|
|
10
|
+
# unlinked) and of a continue in flight (ContinueIntent.in_flight), named
|
|
11
|
+
# by the first of STATES whose facts hold (Reading.derive). Nothing is
|
|
12
|
+
# stored: see docs/internals/session-lifecycle.md (Derived states).
|
|
13
|
+
#
|
|
14
|
+
# A read only: it writes, clears and unlinks nothing (the marker writers
|
|
15
|
+
# are pinned in spec/session_lifecycle_writers_spec.rb). One decider
|
|
16
|
+
# reads it (restart_session's precheck, ALLOWED[:restart]); ALLOWED is what
|
|
17
|
+
# each operation does in each state, pinned against the deciders by
|
|
18
|
+
# spec/session_lifecycle/matrix_spec.rb.
|
|
19
|
+
module SessionLifecycle
|
|
20
|
+
# In precedence order: the first whose facts hold names the session.
|
|
21
|
+
# The compound states (stopped_starting, archived_starting) are there
|
|
22
|
+
# because the deciders disagree on which of two facts wins.
|
|
23
|
+
STATES = %i[absent tui_owned deleting stopping skew leaving booting_stuck booting live
|
|
24
|
+
stopped_starting stopped archived_starting starting archived idle].freeze
|
|
25
|
+
|
|
26
|
+
autoload :Reading, File.expand_path("session_lifecycle/reading", __dir__)
|
|
27
|
+
autoload :CANONICAL, File.expand_path("session_lifecycle/canonical", __dir__)
|
|
28
|
+
autoload :ALLOWED, File.expand_path("session_lifecycle/allowed", __dir__)
|
|
29
|
+
|
|
30
|
+
# The cell words that are no refusal, per operation: a resume that finds
|
|
31
|
+
# a worker there or one being started waits for it; a restart's
|
|
32
|
+
# fallback (an unresponsive worker's path) goes.
|
|
33
|
+
VERDICT_WORDS = { resume: { live: :wait, starting: :wait }, restart: { fallback: :go } }.freeze
|
|
34
|
+
|
|
35
|
+
module_function
|
|
36
|
+
|
|
37
|
+
# @return [Symbol] the cell of +op+ in +state+ (ALLOWED)
|
|
38
|
+
def allowed(op, state) = ALLOWED.fetch(op).fetch(state)
|
|
39
|
+
|
|
40
|
+
# A cell as :go, :wait or :refused: :go and :wait are themselves, an
|
|
41
|
+
# operation's own words (VERDICT_WORDS) theirs, every other a refusal.
|
|
42
|
+
# @return [Symbol]
|
|
43
|
+
def verdict(op, cell)
|
|
44
|
+
return cell if %i[go wait].include?(cell)
|
|
45
|
+
|
|
46
|
+
VERDICT_WORDS.fetch(op, {}).fetch(cell, :refused)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Read +session_id+'s state now.
|
|
50
|
+
# @param state_dir [String, nil] nil: Session.default_state_dir
|
|
51
|
+
# @param owner [OwnerLock::Owner, nil, :read, #call] as WorkerPresence.read takes it
|
|
52
|
+
# @return [Reading]
|
|
53
|
+
def read(session_id, state_dir: nil, owner: :read)
|
|
54
|
+
sd = state_dir || Session.default_state_dir
|
|
55
|
+
presence = WorkerPresence.read(session_id, state_dir: sd, owner: owner, link: :look)
|
|
56
|
+
Reading.derive(presence, continuing: ContinueIntent.in_flight(session_id, state_dir: sd))
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -58,7 +58,7 @@ module Samagotchi
|
|
|
58
58
|
# tool's follow-up says it after "Error: session <id8>".
|
|
59
59
|
# @return [String]
|
|
60
60
|
def refusal_text
|
|
61
|
-
refused = error == Delivery::QUEUE_UNAVAILABLE["error"] || continued? || wake_refused? ||
|
|
61
|
+
refused = error == Delivery::QUEUE_UNAVAILABLE["error"] || continued? || wake_refused? || deleting? ||
|
|
62
62
|
DeliverResult::REMOTE_REFUSALS.include?(error)
|
|
63
63
|
"#{refused ? "refused" : "failed"}: #{detail || "could not queue it"}"
|
|
64
64
|
end
|
|
@@ -76,6 +76,9 @@ module Samagotchi
|
|
|
76
76
|
# (DeliverResult::STOPPED / ARCHIVED: only a person's message does).
|
|
77
77
|
def wake_refused? = DeliverResult::WAKE_REFUSALS.include?(error)
|
|
78
78
|
|
|
79
|
+
# A session being deleted (DeliverResult::DELETING): nothing went in.
|
|
80
|
+
def deleting? = error == DeliverResult::DELETING["error"]
|
|
81
|
+
|
|
79
82
|
# A --cut's outcome ("now", "waits", "off"), nil when nothing was cut.
|
|
80
83
|
def cut = ack["cut"]
|
|
81
84
|
|
|
@@ -101,6 +104,11 @@ module Samagotchi
|
|
|
101
104
|
DeliverResult::ARCHIVED = { "error" => "session_archived",
|
|
102
105
|
"detail" => "the session is archived; only its owner's message brings it back" }.freeze
|
|
103
106
|
DeliverResult::WAKE_REFUSALS = [DeliverResult::STOPPED["error"], DeliverResult::ARCHIVED["error"]].freeze
|
|
107
|
+
# Any sender's turn to a session a deleter holds (its owner lock kind
|
|
108
|
+
# deleting: a delete, an empty session's discard), or that went while
|
|
109
|
+
# the input was written: refused (a 409 from chi web), its file taken
|
|
110
|
+
# back (SessionInbox.withdraw), never accepted and then removed.
|
|
111
|
+
DeliverResult::DELETING = { "error" => "session_deleting", "detail" => "the session is being deleted" }.freeze
|
|
104
112
|
|
|
105
113
|
# The unclaimed input files +author+ wrote.
|
|
106
114
|
PARTICIPANT_QUEUE_LOCK = "participants.lock"
|
|
@@ -140,7 +148,8 @@ module Samagotchi
|
|
|
140
148
|
# a web tab, a TUI) un-stops a stopped session and starts an archived
|
|
141
149
|
# link; a schedule's, a plugin's, a delegate's, a participant's or an
|
|
142
150
|
# unknown one's is refused there (:refused 409, DeliverResult::STOPPED
|
|
143
|
-
# / ARCHIVED, nothing written)
|
|
151
|
+
# / ARCHIVED, nothing written); anyone's to a session being deleted
|
|
152
|
+
# is refused (:refused 409, DeliverResult::DELETING)
|
|
144
153
|
# @param images [Array<Hash>] refs ({file:, name:}) already in images/
|
|
145
154
|
# @param delivery [String, nil] what the message asks the running turn
|
|
146
155
|
# for: "next_step" (default), "cut" or "queue"
|
|
@@ -314,10 +323,14 @@ module Samagotchi
|
|
|
314
323
|
# A worker that idle-exited since the resume never reads it either:
|
|
315
324
|
# wake a new one. (The exiting worker also looks for input it left;
|
|
316
325
|
# one the first resume spawned that is still starting is left be.)
|
|
317
|
-
start_for_send(manager, session_id, state_dir, client_id, before: false) do
|
|
326
|
+
refused = start_for_send(manager, session_id, state_dir, client_id, before: false, written: path) do
|
|
318
327
|
FileUtils.rm_f(path) if path.is_a?(String)
|
|
319
328
|
raise OwnedByTUI, session_id
|
|
320
329
|
end
|
|
330
|
+
# A delete that took the lock between the check and the write: the
|
|
331
|
+
# file is taken back (unless a worker claimed it already).
|
|
332
|
+
return refused if refused && SessionInbox.withdraw(path, session_id, state_dir: state_dir || Session.default_state_dir)
|
|
333
|
+
|
|
321
334
|
DeliverResult.new(status: :accepted, via: :file,
|
|
322
335
|
ack: { "status" => "accepted", "enqueued_id" => enqueued_id, "session_id" => session_id })
|
|
323
336
|
end
|
|
@@ -341,14 +354,24 @@ module Samagotchi
|
|
|
341
354
|
# held for its last work) has its input served by its own respawn
|
|
342
355
|
# (WorkerBoot's leave): a stop that landed before this send is
|
|
343
356
|
# cleared, as resume_session clears it (a person's send un-stops); one
|
|
344
|
-
# that lands after it still wins.
|
|
357
|
+
# that lands after it still wins. A deleter holding the lock refuses
|
|
358
|
+
# any sender, before the write and after it (DeliverResult::DELETING),
|
|
359
|
+
# and so does a session gone after the write (+written+, the file's
|
|
360
|
+
# path: a stand-in manager's write makes none).
|
|
345
361
|
# @return [DeliverResult, nil] the refusal of a wake the sender may not
|
|
346
|
-
# make (+before+ only), else nil
|
|
347
|
-
private def start_for_send(manager, session_id, state_dir, client_id, before:)
|
|
362
|
+
# make (+before+ only) or of a session being deleted, else nil
|
|
363
|
+
private def start_for_send(manager, session_id, state_dir, client_id, before:, written: nil)
|
|
348
364
|
presence = delivery_presence(manager, session_id, state_dir)
|
|
349
365
|
policy = SessionInbox::WakePolicy.for_sender(client_id)
|
|
350
366
|
outcome = policy.decide(presence)
|
|
351
367
|
yield if outcome == :owned && block_given?
|
|
368
|
+
if outcome == :deleting || (outcome == :gone && !before)
|
|
369
|
+
if before || written.is_a?(String)
|
|
370
|
+
Log.info(:worker, "send_deleting_refused", sid: session_id[0, 8], kind: policy.kind, before: before)
|
|
371
|
+
return DeliverResult.new(status: :refused, code: 409, ack: DeliverResult::DELETING)
|
|
372
|
+
end
|
|
373
|
+
return nil if outcome == :deleting
|
|
374
|
+
end
|
|
352
375
|
if %i[stopped archived].include?(outcome)
|
|
353
376
|
return nil unless before
|
|
354
377
|
|