samagotchi 0.173.0 → 0.174.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 +31 -1
- data/docs/architecture.md +2 -2
- data/docs/cli.md +9 -2
- data/docs/internals/background-tasks.md +0 -3
- data/docs/internals/participants.md +3 -1
- data/docs/internals/remote-sessions.md +8 -0
- data/docs/internals/web-frontend.md +24 -3
- data/docs/plugins.md +1 -3
- data/docs/scheduling.md +9 -3
- data/docs/sessions.md +3 -1
- data/docs/testing.md +6 -0
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/child_move.rb +11 -4
- data/lib/samagotchi/continued_from.rb +51 -0
- data/lib/samagotchi/engine.rb +1 -1
- data/lib/samagotchi/note_move.rb +11 -4
- data/lib/samagotchi/schedules.rb +13 -16
- data/lib/samagotchi/session_access/contract.rb +1 -0
- data/lib/samagotchi/session_access/local.rb +9 -1
- data/lib/samagotchi/session_access/remote.rb +26 -3
- data/lib/samagotchi/session_chain/delete.rb +405 -0
- data/lib/samagotchi/session_chain/delete_preview.rb +96 -0
- data/lib/samagotchi/session_chain/links.rb +38 -0
- data/lib/samagotchi/session_command_catalog.rb +2 -2
- data/lib/samagotchi/session_commands.rb +3 -13
- data/lib/samagotchi/session_continue.rb +51 -13
- data/lib/samagotchi/session_delete_command.rb +82 -17
- data/lib/samagotchi/session_manager/removal.rb +137 -31
- data/lib/samagotchi/session_stores.rb +2 -0
- data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +2 -2
- data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
- data/lib/samagotchi/terminal_ui/exit_lines.rb +16 -0
- data/lib/samagotchi/terminal_ui/local_link.rb +7 -1
- data/lib/samagotchi/terminal_ui.rb +3 -3
- data/lib/samagotchi/testing/boundary.rb +0 -5
- data/lib/samagotchi/testing/bundle.rb +1 -1
- data/lib/samagotchi/tools/task_runtime.rb +0 -5
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +7 -1
- data/lib/samagotchi/web/public/app.js +13 -3
- data/lib/samagotchi/web/public/chains.js +3 -3
- data/lib/samagotchi/web/public/data.js +12 -0
- data/lib/samagotchi/web/public/delete_sheet.js +344 -0
- data/lib/samagotchi/web/public/format.js +1 -1
- data/lib/samagotchi/web/public/index.html +27 -0
- data/lib/samagotchi/web/public/info_bar.js +3 -2
- data/lib/samagotchi/web/public/lists_view.js +2 -2
- data/lib/samagotchi/web/public/more_sheet.js +42 -5
- data/lib/samagotchi/web/public/session_actions.js +50 -6
- data/lib/samagotchi/web/server.rb +13 -13
- data/lib/samagotchi/web/session_control.rb +58 -5
- metadata +5 -1
|
@@ -316,6 +316,8 @@ module Samagotchi
|
|
|
316
316
|
|
|
317
317
|
# Web::App::BOUND_FEATURE: POST …/participants takes an identity.
|
|
318
318
|
BOUND_FEATURE = "bound_grants"
|
|
319
|
+
# Web::App::CHAIN_DELETE_FEATURE: DELETE …/chain.
|
|
320
|
+
CHAIN_DELETE_FEATURE = "chain_delete"
|
|
319
321
|
|
|
320
322
|
# The header that chi web names the signed-in user in (GET
|
|
321
323
|
# /api/info's proxy), nil when it has no such proxy, or is a chi
|
|
@@ -493,13 +495,33 @@ module Samagotchi
|
|
|
493
495
|
end
|
|
494
496
|
|
|
495
497
|
# @return [Hash] {id:, stopped:}
|
|
498
|
+
# A chi web before chain_delete answers without unarchived,
|
|
499
|
+
# moved_back, deleted and left_running (no revive, no undo, no D7
|
|
500
|
+
# there): each reads as [].
|
|
496
501
|
def delete(id, force:)
|
|
497
502
|
reply = request(:delete, session_path(id), query: { force: (true if force) })
|
|
498
503
|
raise_refusal(reply) unless reply.status == 200
|
|
499
504
|
|
|
500
|
-
{ id: reply.json["session_id"] || id, stopped: reply.json["stopped"] == true
|
|
505
|
+
{ id: reply.json["session_id"] || id, stopped: reply.json["stopped"] == true,
|
|
506
|
+
unarchived: ids_of(reply.json["unarchived"]), moved_back: ids_of(reply.json["moved_back"]),
|
|
507
|
+
deleted: ids_of(reply.json["deleted"]), left_running: ids_of(reply.json["left_running"]) }
|
|
501
508
|
end
|
|
502
509
|
|
|
510
|
+
# DELETE …/chain there (SessionManager.delete_chain's result).
|
|
511
|
+
# @raise [RemoteTooOld] a chi web without it (an older one would answer 404)
|
|
512
|
+
def delete_chain(id, force:)
|
|
513
|
+
require_feature!(CHAIN_DELETE_FEATURE)
|
|
514
|
+
reply = request(:delete, "#{session_path(id)}/chain", query: { force: (true if force) })
|
|
515
|
+
raise_refusal(reply) unless reply.status == 200
|
|
516
|
+
|
|
517
|
+
json = reply.json
|
|
518
|
+
{ id: json["session_id"] || id, deleted: ids_of(json["deleted"]), left: ids_of(json["left"]),
|
|
519
|
+
left_running: ids_of(json["left_running"]), unarchived: ids_of(json["unarchived"]),
|
|
520
|
+
stopped: ids_of(json["stopped"]), cut_off: json["cut_off"] == true, refusal: json["refusal"] }
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
private def ids_of(value) = Array(value).select { |id| Session.valid_id?(id) }
|
|
524
|
+
|
|
503
525
|
# --- context (a session's own sources: a project's are local only)
|
|
504
526
|
|
|
505
527
|
# @return [ContextAdded]
|
|
@@ -761,8 +783,9 @@ module Samagotchi
|
|
|
761
783
|
raise SessionManager::ScratchSession, sid if json["scratch"] == true
|
|
762
784
|
|
|
763
785
|
raise SessionManager::OwnedByTUI, sid
|
|
764
|
-
when "worker_running", "still_stopping", "scheduled"
|
|
765
|
-
raise SessionManager::DeleteRefused.new(sid, reason, message: message)
|
|
786
|
+
when "worker_running", "still_stopping", "scheduled", "continuing", "delegate_unmovable", "chain_scheduled"
|
|
787
|
+
raise SessionManager::DeleteRefused.new(sid, reason || error.to_sym, message: message, ids: ids_of(json["ids"]),
|
|
788
|
+
schedule_id: json["schedule_id"])
|
|
766
789
|
when "busy", "scratch"
|
|
767
790
|
raise SessionManager::ArchiveRefused.new(sid, reason, busy_id: json["busy_id"]) if reason
|
|
768
791
|
when "continued", "open_children", "folder_gone"
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "links"
|
|
5
|
+
require_relative "../archive_store"
|
|
6
|
+
require_relative "../child_move"
|
|
7
|
+
require_relative "../continue_intent"
|
|
8
|
+
require_relative "../continued_from"
|
|
9
|
+
require_relative "../note_move"
|
|
10
|
+
require_relative "../owner_lock"
|
|
11
|
+
require_relative "../session"
|
|
12
|
+
require_relative "../starting_marker"
|
|
13
|
+
require_relative "../worker_presence"
|
|
14
|
+
|
|
15
|
+
module Samagotchi
|
|
16
|
+
# Loaded on first use: session_manager loads this file from its delete
|
|
17
|
+
# (a require cycle otherwise), and session_continue requires session_manager.
|
|
18
|
+
autoload :SessionManager, File.expand_path("../session_manager", __dir__)
|
|
19
|
+
autoload :SessionContinue, File.expand_path("../session_continue", __dir__)
|
|
20
|
+
autoload :Schedules, File.expand_path("../schedules", __dir__)
|
|
21
|
+
|
|
22
|
+
module SessionChain
|
|
23
|
+
# A user's delete of a link of a session chain (the CLI, the web, the
|
|
24
|
+
# TUI's /exit --delete: SessionManager.delete_session's revive_previous:
|
|
25
|
+
# and refuse_continuing:), and of the whole chain (.chain:
|
|
26
|
+
# SessionManager.delete_chain). A session in no chain is deleted as
|
|
27
|
+
# before (SessionManager.delete_resolved). The scratch cleanup, the
|
|
28
|
+
# empty-session discard, the failed-continue rollback and the prune
|
|
29
|
+
# never come here.
|
|
30
|
+
#
|
|
31
|
+
# - The chain's newest link (not archived itself, its previous link
|
|
32
|
+
# archived, nobody continuing it) is undone (D2/D5): the delegates its
|
|
33
|
+
# continue moved go back to the previous link, with the notes waiting
|
|
34
|
+
# in it, and the previous link and what the continue archived with it
|
|
35
|
+
# come back to the lists (ContinuedFrom's record; without one, the
|
|
36
|
+
# previous link alone). The previous link is not started.
|
|
37
|
+
# - Any other link (a middle one splits the chain) is deleted alone.
|
|
38
|
+
# - Either way the link's own delegates follow DELEGATES (D7).
|
|
39
|
+
module Delete
|
|
40
|
+
# What DELEGATES does with a link's delegates: +delete+ (deepest
|
|
41
|
+
# first, before the link) and +leave+ (running, with their whole
|
|
42
|
+
# subtrees: they go on without a parent, their files untouched).
|
|
43
|
+
Plan = Data.define(:delete, :leave)
|
|
44
|
+
|
|
45
|
+
# D7, hard-coded for the chain deletes for now (one place, so that a
|
|
46
|
+
# policy for every delete can move it into SessionManager later): a
|
|
47
|
+
# delegate is running when a worker owns it or is being started for
|
|
48
|
+
# it (a one-turn chi run too, which can't be stopped from here). A
|
|
49
|
+
# saved status running with no worker is a crashed run: it goes. A running one is left with its whole subtree;
|
|
50
|
+
# every other node under the link (no running node between it and the
|
|
51
|
+
# link) is deleted.
|
|
52
|
+
class DelegateRule
|
|
53
|
+
# @param except [Array<String>] the link's children left out with
|
|
54
|
+
# their subtrees (the ones the undo gives back)
|
|
55
|
+
# @return [Plan]
|
|
56
|
+
def plan(link, state_dir:, except: [])
|
|
57
|
+
delete = []
|
|
58
|
+
leave = []
|
|
59
|
+
queue = [link]
|
|
60
|
+
until queue.empty?
|
|
61
|
+
SessionManager.children_of(queue.shift, state_dir: state_dir).each do |row|
|
|
62
|
+
next if row.id == link || except.include?(row.id) || delete.include?(row.id) || leave.include?(row.id)
|
|
63
|
+
|
|
64
|
+
if running?(row, state_dir)
|
|
65
|
+
leave.concat([row.id, *SessionManager.descendant_ids(row.id, state_dir)] - leave)
|
|
66
|
+
else
|
|
67
|
+
delete << row.id
|
|
68
|
+
queue << row.id
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
Plan.new(delete: delete.reverse, leave: leave)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def running?(row, state_dir)
|
|
78
|
+
presence = WorkerPresence.read(row.id, state_dir: state_dir,
|
|
79
|
+
owner: SessionManager.session_owner(row.id, state_dir: state_dir))
|
|
80
|
+
presence.owned? || presence.starting?
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
DELEGATES = DelegateRule.new.freeze
|
|
85
|
+
|
|
86
|
+
module_function
|
|
87
|
+
|
|
88
|
+
# What deleting the link +id+ alone does, read from one listing: the
|
|
89
|
+
# one plan the delete runs (.link) and the web's sheet shows (.preview).
|
|
90
|
+
# +previous+: the link whose continue is undone (D2/D5), nil when
|
|
91
|
+
# none is; +revive+: what comes back to the lists; +moved+: the
|
|
92
|
+
# delegates that go back to it; +delegates+: DELEGATES' Plan for the
|
|
93
|
+
# rest of its delegates; +notes+: the waiting notes that go back.
|
|
94
|
+
LinkPlan = Data.define(:id, :previous, :revive, :moved, :delegates, :notes)
|
|
95
|
+
|
|
96
|
+
# @param undo [Boolean] the caller undoes a newest link's continue
|
|
97
|
+
# (revive_previous)
|
|
98
|
+
# @return [LinkPlan]
|
|
99
|
+
def link_plan(id, state_dir:, links:, undo: true)
|
|
100
|
+
previous = links.continues(id)
|
|
101
|
+
previous = nil unless undo && previous && revivable?(id, previous, links)
|
|
102
|
+
moved = previous ? moved_ids(id, previous, state_dir) : []
|
|
103
|
+
LinkPlan.new(id: id, previous: previous, revive: previous ? revive_ids(id, previous, state_dir) : [],
|
|
104
|
+
moved: moved, delegates: DELEGATES.plan(id, state_dir: state_dir, except: moved),
|
|
105
|
+
notes: previous ? NoteMove.returnable(from: id, moved: [], state_dir: state_dir).size : 0)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# What deleting every link of +id+'s chain does: the links on its
|
|
109
|
+
# branch (root to end, past a gap only the part after it), each
|
|
110
|
+
# link's DELEGATES plan, and whether a gap cut earlier links off.
|
|
111
|
+
ChainPlan = Data.define(:id, :links, :delegates, :cut_off)
|
|
112
|
+
|
|
113
|
+
# @return [ChainPlan]
|
|
114
|
+
def chain_plan(id, state_dir:, links:)
|
|
115
|
+
ids = links.live_links(id)
|
|
116
|
+
ChainPlan.new(id: id, links: ids, delegates: ids.to_h { |link| [link, DELEGATES.plan(link, state_dir: state_dir)] },
|
|
117
|
+
cut_off: links.gone?(links.chain(id).first))
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Whether +id+ is in a chain of 2 or more links that still exist (D1):
|
|
121
|
+
# a link alone past a gap (its predecessor deleted) is deleted as a
|
|
122
|
+
# session in no chain is.
|
|
123
|
+
def chained?(id, links) = links.live_links(id).size > 1
|
|
124
|
+
|
|
125
|
+
# Delete the link +id+ (a full id that exists), inside a shared hold
|
|
126
|
+
# of its continue lock (and the previous link's when it would come
|
|
127
|
+
# back), so no continue of either starts meanwhile.
|
|
128
|
+
# @return [Hash] {id:, removed:, stopped:, unarchived:, moved_back:,
|
|
129
|
+
# deleted: (its delegates deleted and it), left_running:}
|
|
130
|
+
# @raise [SessionManager::DeleteRefused] :continuing, :scheduled,
|
|
131
|
+
# :delegate_unmovable, :worker_running, :still_stopping
|
|
132
|
+
# @raise [SessionManager::OwnedByTUI]
|
|
133
|
+
def link(id, state_dir:, stop:, wait:, revive_previous:, refuse_continuing:)
|
|
134
|
+
ContinueIntent.hold(id, state_dir: state_dir) do |held|
|
|
135
|
+
raise SessionManager::DeleteRefused.new(id, :continuing) if refuse_continuing && !held
|
|
136
|
+
|
|
137
|
+
links = Links.new(state_dir: state_dir)
|
|
138
|
+
unless chained?(id, links)
|
|
139
|
+
return plain(SessionManager.delete_resolved(id, state_dir: state_dir, stop: stop, wait: wait))
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
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
|
|
144
|
+
|
|
145
|
+
# Taken before the link's file goes: after it, a continue of the
|
|
146
|
+
# previous link would no longer be refused. The plan is made again
|
|
147
|
+
# under it.
|
|
148
|
+
ContinueIntent.hold(plan.previous, state_dir: state_dir) do |previous_held|
|
|
149
|
+
raise SessionManager::DeleteRefused.new(plan.previous, :continuing) unless previous_held
|
|
150
|
+
|
|
151
|
+
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)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# D2's conditions: +id+ is the chain's end and not archived itself,
|
|
158
|
+
# and the link before it is there and archived.
|
|
159
|
+
def revivable?(id, previous, links)
|
|
160
|
+
!links.gone?(previous) && links.next_of(id).nil? && !links.archived?(id) && links.archived?(previous)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def plain(result) = result.merge(unarchived: [], moved_back: [], deleted: [result[:id]], left_running: [])
|
|
164
|
+
|
|
165
|
+
# The refusals a link's delete makes before anything changes (the
|
|
166
|
+
# sheet's too): a schedule's target, anchor or head, on the link or a
|
|
167
|
+
# delegate that would go; a moved delegate that can't go back; a
|
|
168
|
+
# delegate that would go in a one-turn run.
|
|
169
|
+
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
170
|
+
def check_link!(plan, state_dir)
|
|
171
|
+
SessionManager.refuse_scheduled!(plan.id, state_dir)
|
|
172
|
+
refuse_unmovable!(plan.id, plan.moved, state_dir)
|
|
173
|
+
precheck!(plan.delegates.delete, state_dir)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Every refusal first (check_link!), then the link's worker stopped,
|
|
177
|
+
# then the owner lock (kind deleting, no wait) on the link and every
|
|
178
|
+
# D7 delegate that goes, and only under those locks the delegates'
|
|
179
|
+
# files, then either the undo (+plan.previous+: D5) or the link's
|
|
180
|
+
# files. So a refusal (a schedule on a delegate, a one-turn run, a
|
|
181
|
+
# worker that took the link or a delegate after the checks) leaves
|
|
182
|
+
# every session in place.
|
|
183
|
+
def delete_link(plan, state_dir:, stop:, wait:)
|
|
184
|
+
id = plan.id
|
|
185
|
+
check_link!(plan, state_dir)
|
|
186
|
+
stopped = SessionManager.release_for_delete!(id, state_dir: state_dir, stop: stop, wait: wait)
|
|
187
|
+
path = Session.session_file(id, state_dir: state_dir)
|
|
188
|
+
dir = Session.session_dir(id, state_dir: state_dir)
|
|
189
|
+
doomed = [*plan.delegates.delete, id]
|
|
190
|
+
removed = locked(doomed, state_dir) do
|
|
191
|
+
remove_locked(plan.delegates.delete, state_dir)
|
|
192
|
+
if plan.previous
|
|
193
|
+
SessionContinue.undo_deleted_link(from: plan.previous, link: id, moved: plan.moved, state_dir: state_dir) do
|
|
194
|
+
SessionManager.remove_files(id, path, dir, state_dir: state_dir, purge_runs: false, locked: true)
|
|
195
|
+
end
|
|
196
|
+
else
|
|
197
|
+
SessionManager.remove_files(id, path, dir, state_dir: state_dir, purge_runs: false, locked: true)
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
SessionManager.purge_removal_stores(doomed, state_dir: state_dir)
|
|
201
|
+
unarchived = plan.revive.select { |sid| ArchiveStore.unarchive(sid, state_dir: state_dir) }
|
|
202
|
+
{ id: id, removed: removed, stopped: stopped, unarchived: unarchived, moved_back: plan.moved,
|
|
203
|
+
deleted: doomed, left_running: plan.delegates.leave }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# The refusals of every id a batch would delete, before the first
|
|
207
|
+
# goes: a schedule's target, anchor or head (the refusal names that
|
|
208
|
+
# delegate), a one-turn run.
|
|
209
|
+
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
210
|
+
def precheck!(ids, state_dir)
|
|
211
|
+
ids.each do |sid|
|
|
212
|
+
begin
|
|
213
|
+
SessionManager.refuse_scheduled!(sid, state_dir)
|
|
214
|
+
rescue SessionManager::DeleteRefused => e
|
|
215
|
+
raise SessionManager::DeleteRefused.new(
|
|
216
|
+
sid, :scheduled, schedule_id: e.schedule_id,
|
|
217
|
+
message: "its delegate #{sid[0, 8]} is the target of an enabled schedule; " \
|
|
218
|
+
"cancel it with chi schedule cancel #{e.schedule_id}"
|
|
219
|
+
)
|
|
220
|
+
end
|
|
221
|
+
raise SessionManager::OwnedByTUI, sid if SessionManager.session_owner(sid, state_dir: state_dir)&.tui?
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# The block under the owner lock (kind deleting, taken without
|
|
226
|
+
# waiting) of every id with a folder, the starting marker read again
|
|
227
|
+
# under each, held until the block returns: a worker that holds one,
|
|
228
|
+
# or a start claimed meanwhile, refuses before anything goes, and
|
|
229
|
+
# none takes one midway.
|
|
230
|
+
# @raise [SessionManager::DeleteRefused] :worker_running
|
|
231
|
+
def locked(ids, state_dir)
|
|
232
|
+
locks = []
|
|
233
|
+
ids.each do |sid|
|
|
234
|
+
dir = Session.session_dir(sid, state_dir: state_dir)
|
|
235
|
+
next unless Dir.exist?(dir)
|
|
236
|
+
|
|
237
|
+
lock = OwnerLock.acquire(dir, kind: OwnerLock::DELETING, wait: 0)
|
|
238
|
+
raise SessionManager::DeleteRefused.new(sid, :worker_running) unless lock
|
|
239
|
+
|
|
240
|
+
locks << lock
|
|
241
|
+
raise SessionManager::DeleteRefused.new(sid, :worker_running) if StartingMarker.fresh?(sid, state_dir: state_dir)
|
|
242
|
+
end
|
|
243
|
+
yield
|
|
244
|
+
ensure
|
|
245
|
+
locks.each(&:release)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# +ids+' files, in order, each locked by the caller (#locked); the
|
|
249
|
+
# session logs are the caller's to purge, once.
|
|
250
|
+
# @return [Array<String>] the paths removed
|
|
251
|
+
def remove_locked(ids, state_dir)
|
|
252
|
+
ids.flat_map do |sid|
|
|
253
|
+
SessionManager.remove_files(sid, Session.session_file(sid, state_dir: state_dir), Session.session_dir(sid, state_dir: state_dir),
|
|
254
|
+
state_dir: state_dir, purge_runs: false, locked: true)
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# The delegates the continue +previous+ → +id+ moved that are still
|
|
259
|
+
# +id+'s: by ContinuedFrom's record (a nested undo needs it: an earlier
|
|
260
|
+
# one rewrote parent.json's from), else +id+'s children whose
|
|
261
|
+
# parent.json says they came from +previous+.
|
|
262
|
+
# @return [Array<String>]
|
|
263
|
+
def moved_ids(id, previous, state_dir)
|
|
264
|
+
children = SessionManager.children_of(id, state_dir: state_dir).map(&:id)
|
|
265
|
+
record = ContinuedFrom.read(id, state_dir: state_dir)
|
|
266
|
+
return record.moved_ids & children if record&.previous_id == previous
|
|
267
|
+
|
|
268
|
+
children.select { |child| moved_from(child, state_dir) == previous }
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# What comes back to the lists: what the continue archived, else the
|
|
272
|
+
# previous link alone (its archived delegates stay archived).
|
|
273
|
+
def revive_ids(id, previous, state_dir)
|
|
274
|
+
record = ContinuedFrom.read(id, state_dir: state_dir)
|
|
275
|
+
record&.previous_id == previous ? (record.archived_ids | [previous]) : [previous]
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# parent.json's +from+, nil without one.
|
|
279
|
+
def moved_from(child, state_dir)
|
|
280
|
+
data = JSON.parse(File.read(File.join(Session.session_dir(child, state_dir: state_dir), Session::PARENT_FILE)))
|
|
281
|
+
data.is_a?(Hash) ? data["from"] : nil
|
|
282
|
+
rescue JSON::ParserError, SystemCallError
|
|
283
|
+
nil
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# Whether the page can stop the workers of +ids+ (moved delegates that
|
|
287
|
+
# can't go back): none is in a one-turn chi run, which can't be stopped
|
|
288
|
+
# from here. The 409 and the preview say it.
|
|
289
|
+
def stoppable?(ids, state_dir)
|
|
290
|
+
ids.none? { |sid| SessionManager.session_owner(sid, state_dir: state_dir)&.tui? }
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def refuse_unmovable!(id, moved, state_dir)
|
|
294
|
+
stuck = moved.filter_map { |child| (why = ChildMove.unmovable(child, state_dir: state_dir)) && [child, why] }
|
|
295
|
+
return if stuck.empty?
|
|
296
|
+
|
|
297
|
+
raise SessionManager::DeleteRefused.new(id, :delegate_unmovable, ids: stuck.map(&:first),
|
|
298
|
+
detail: stuck.map { |c, why| "#{c[0, 8]} #{why}" }.join("; "))
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
# Delete every link connected to +id+ on its branch (past a gap, only
|
|
302
|
+
# the part after it is reached), newest first, each with its D7
|
|
303
|
+
# delegates. Refused before anything goes when a schedule is on the
|
|
304
|
+
# chain or on a delegate that would go, a link or such a delegate is in
|
|
305
|
+
# a one-turn chi run, a continue of a link is under way, a link has a
|
|
306
|
+
# worker (without +stop+), or a stopped link's worker outlives the
|
|
307
|
+
# wait: every link is checked and stopped before the first is
|
|
308
|
+
# deleted. Each link then goes under the owner locks of it and its
|
|
309
|
+
# delegates (#locked); a link refused there (a worker took it after
|
|
310
|
+
# the stop) leaves it and the earlier links whole, its new end back in
|
|
311
|
+
# the lists as D2 says (no undo).
|
|
312
|
+
# @return [Hash] {id:, deleted:, left:, left_running:, unarchived:,
|
|
313
|
+
# stopped:, cut_off:, refusal:} (cut_off: a deleted link cuts off
|
|
314
|
+
# earlier ones; refusal: the message of a part-way refusal, or nil)
|
|
315
|
+
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
316
|
+
# nothing was deleted
|
|
317
|
+
def chain(id, state_dir:, stop:, wait:)
|
|
318
|
+
links = Links.new(state_dir: state_dir)
|
|
319
|
+
plan = chain_plan(id, state_dir: state_dir, links: links)
|
|
320
|
+
check_chain!(plan, state_dir, stop: stop)
|
|
321
|
+
hold_all(plan.links, state_dir) do
|
|
322
|
+
stopped = plan.links.select do |link|
|
|
323
|
+
SessionManager.release_for_delete!(link, state_dir: state_dir, stop: stop, wait: wait)
|
|
324
|
+
end
|
|
325
|
+
delete_links(plan, links, state_dir).merge(stopped: stopped, cut_off: plan.cut_off)
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# The refusals a chain's delete makes before anything goes (the
|
|
330
|
+
# sheet's too): a schedule on the chain, a link in a one-turn run,
|
|
331
|
+
# without +stop+ a link with a worker, then every delegate that would
|
|
332
|
+
# go (precheck!: a schedule on one, a one-turn run).
|
|
333
|
+
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
334
|
+
def check_chain!(plan, state_dir, stop:)
|
|
335
|
+
schedule = Schedules.new(state_dir: state_dir).for_chain(plan.id).first
|
|
336
|
+
raise SessionManager::DeleteRefused.new(plan.id, :chain_scheduled, schedule_id: schedule.id) if schedule
|
|
337
|
+
|
|
338
|
+
plan.links.each do |link|
|
|
339
|
+
presence = WorkerPresence.read(link, state_dir: state_dir, owner: SessionManager.session_owner(link, state_dir: state_dir))
|
|
340
|
+
raise SessionManager::OwnedByTUI, link if presence.tui_owned?
|
|
341
|
+
raise SessionManager::DeleteRefused.new(link, :worker_running) if !stop && (presence.owned? || presence.starting?)
|
|
342
|
+
end
|
|
343
|
+
precheck!(plan.delegates.values.flat_map(&:delete), state_dir)
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# The block inside a shared continue hold of every id.
|
|
347
|
+
# @raise [SessionManager::DeleteRefused] :continuing, the first one a continue holds
|
|
348
|
+
def hold_all(ids, state_dir, &block)
|
|
349
|
+
return yield if ids.empty?
|
|
350
|
+
|
|
351
|
+
ContinueIntent.hold(ids.first, state_dir: state_dir) do |held|
|
|
352
|
+
raise SessionManager::DeleteRefused.new(ids.first, :continuing) unless held
|
|
353
|
+
|
|
354
|
+
hold_all(ids.drop(1), state_dir, &block)
|
|
355
|
+
end
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
# Each link with its delegates under their locks, newest first; a
|
|
359
|
+
# refused one keeps it and every earlier link whole.
|
|
360
|
+
def delete_links(plan, links, state_dir)
|
|
361
|
+
done = { id: plan.id, deleted: [], left: [], left_running: [], unarchived: [], refusal: nil }
|
|
362
|
+
newest_first = plan.links.reverse
|
|
363
|
+
record = nil
|
|
364
|
+
newest_first.each_with_index do |link, at|
|
|
365
|
+
delegates = plan.delegates.fetch(link)
|
|
366
|
+
batch = [*delegates.delete, link]
|
|
367
|
+
deleted_record = ContinuedFrom.read(link, state_dir: state_dir)
|
|
368
|
+
begin
|
|
369
|
+
locked(batch, state_dir) { remove_locked(batch, state_dir) }
|
|
370
|
+
rescue SessionManager::DeleteRefused, SessionManager::OwnedByTUI, Session::NotFound => e
|
|
371
|
+
raise if done[:deleted].empty?
|
|
372
|
+
|
|
373
|
+
done[:left] = newest_first[at..].reverse
|
|
374
|
+
done[:refusal] = e.message
|
|
375
|
+
done[:unarchived] = revive_left_end(newest_first[at - 1], record, link, links, state_dir)
|
|
376
|
+
break
|
|
377
|
+
ensure
|
|
378
|
+
SessionManager.purge_removal_stores(batch.reject { |sid| on_disk?(sid, state_dir) }, state_dir: state_dir)
|
|
379
|
+
end
|
|
380
|
+
# The record of the link just deleted, read before it went: the
|
|
381
|
+
# next link's revive takes what its continue archived.
|
|
382
|
+
record = deleted_record
|
|
383
|
+
done[:deleted].concat(batch)
|
|
384
|
+
done[:left_running].concat(delegates.leave)
|
|
385
|
+
end
|
|
386
|
+
done
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
# D2 on what a part-way refusal left: its end comes back to the lists
|
|
390
|
+
# (with what the deleted successor's continue archived) unless that
|
|
391
|
+
# successor was archived itself. The successor's record was read
|
|
392
|
+
# before it went.
|
|
393
|
+
def revive_left_end(successor, record, left_end, links, state_dir)
|
|
394
|
+
return [] if links.archived?(successor) || !links.archived?(left_end)
|
|
395
|
+
|
|
396
|
+
ids = record&.previous_id == left_end ? (record.archived_ids | [left_end]) : [left_end]
|
|
397
|
+
ids.select { |sid| ArchiveStore.unarchive(sid, state_dir: state_dir) }
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
def on_disk?(id, state_dir)
|
|
401
|
+
File.exist?(Session.session_file(id, state_dir: state_dir)) || Dir.exist?(Session.session_dir(id, state_dir: state_dir))
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "delete"
|
|
4
|
+
|
|
5
|
+
module Samagotchi
|
|
6
|
+
module SessionChain
|
|
7
|
+
module Delete
|
|
8
|
+
# What the web's delete sheet shows for a chained session (GET
|
|
9
|
+
# /api/sessions/:id/delete_plan): the same LinkPlan and ChainPlan the
|
|
10
|
+
# delete runs, and the refusals the delete's own checks raise now
|
|
11
|
+
# (check_link!, check_chain!, the continue holds, a one-turn run). Made
|
|
12
|
+
# from one listing each time the sheet opens. It takes and releases
|
|
13
|
+
# each link's continue lock (shared, as the delete does), creating its
|
|
14
|
+
# empty file where there was none; it writes nothing else. A stale preview is fine:
|
|
15
|
+
# the delete builds its plan again and refuses as it does.
|
|
16
|
+
module Preview
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# @return [Hash] {chained: false} for a session in no chain, else
|
|
20
|
+
# {chained: true, session_id, position, size, cut_off, link: {...},
|
|
21
|
+
# chain: {...}}; each refusal {reason, message, schedule_id, ids,
|
|
22
|
+
# stoppable} or nil
|
|
23
|
+
# @param state_dir [String, nil] nil: Session.default_state_dir (chi
|
|
24
|
+
# web's, as SessionManager's methods take it)
|
|
25
|
+
def call(id, state_dir:)
|
|
26
|
+
state_dir ||= Session.default_state_dir
|
|
27
|
+
links = Links.new(state_dir: state_dir)
|
|
28
|
+
return { chained: false, session_id: id } unless Delete.chained?(id, links)
|
|
29
|
+
|
|
30
|
+
this = Delete.link_plan(id, state_dir: state_dir, links: links)
|
|
31
|
+
whole = Delete.chain_plan(id, state_dir: state_dir, links: links)
|
|
32
|
+
at = whole.links.index(id) || 0
|
|
33
|
+
{ chained: true, session_id: id, position: at + 1, size: whole.links.size, cut_off: whole.cut_off,
|
|
34
|
+
link: link_json(this, links, whole, at, state_dir), chain: chain_json(whole, state_dir) }
|
|
35
|
+
end
|
|
36
|
+
|
|
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,
|
|
39
|
+
deletes: plan.delegates.delete.size, left_running: plan.delegates.leave.size,
|
|
40
|
+
earlier: at, next_id: links.next_of(plan.id), later: whole.links.size - at - 1,
|
|
41
|
+
refusal: refusal(state_dir) { link_checks(plan, state_dir) } }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The sessions that go back with the undo: each moved delegate and
|
|
45
|
+
# its own subtree (they come back with it).
|
|
46
|
+
def moved_count(plan, state_dir)
|
|
47
|
+
plan.moved.sum { |child| 1 + SessionManager.descendant_ids(child, state_dir).size }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def chain_json(plan, state_dir)
|
|
51
|
+
{ links: plan.links.size, deletes: plan.delegates.values.sum { |d| d.delete.size },
|
|
52
|
+
left_running: plan.delegates.values.sum { |d| d.leave.size },
|
|
53
|
+
refusal: refusal(state_dir) { chain_checks(plan, state_dir) } }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The link delete's refusals in its order: a continue of it (or of
|
|
57
|
+
# the previous link it undoes), a one-turn run, then check_link!.
|
|
58
|
+
def link_checks(plan, state_dir)
|
|
59
|
+
[plan.id, plan.previous].compact.each { |sid| refuse_continuing!(sid, state_dir) }
|
|
60
|
+
tui!(plan.id, state_dir)
|
|
61
|
+
Delete.check_link!(plan, state_dir)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The web always stops the links' workers (force), so a worker is no
|
|
65
|
+
# refusal here.
|
|
66
|
+
def chain_checks(plan, state_dir)
|
|
67
|
+
Delete.check_chain!(plan, state_dir, stop: true)
|
|
68
|
+
plan.links.each { |sid| refuse_continuing!(sid, state_dir) }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def refuse_continuing!(id, state_dir)
|
|
72
|
+
free = ContinueIntent.hold(id, state_dir: state_dir) { |held| held }
|
|
73
|
+
raise SessionManager::DeleteRefused.new(id, :continuing) unless free
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def tui!(id, state_dir)
|
|
77
|
+
presence = WorkerPresence.read(id, state_dir: state_dir, owner: SessionManager.session_owner(id, state_dir: state_dir))
|
|
78
|
+
raise SessionManager::OwnedByTUI, id if presence.tui_owned?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# The block's refusal as the sheet reads it, nil when there is none.
|
|
82
|
+
# +stoppable+: a moved delegate that can't go back is on an older
|
|
83
|
+
# chi's worker the page can stop (Delete.stoppable?).
|
|
84
|
+
def refusal(state_dir)
|
|
85
|
+
yield
|
|
86
|
+
nil
|
|
87
|
+
rescue SessionManager::OwnedByTUI => e
|
|
88
|
+
{ reason: "owned_by_tui", message: e.message, ids: [e.session_id] }
|
|
89
|
+
rescue SessionManager::DeleteRefused => e
|
|
90
|
+
{ reason: e.reason.to_s, message: e.message, schedule_id: e.schedule_id, ids: e.ids,
|
|
91
|
+
stoppable: e.reason == :delegate_unmovable ? Delete.stoppable?(e.ids, state_dir) : nil }.compact
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "../archive_store"
|
|
4
|
+
require_relative "../log"
|
|
4
5
|
require_relative "../session"
|
|
5
6
|
|
|
6
7
|
module Samagotchi
|
|
@@ -23,6 +24,8 @@ module Samagotchi
|
|
|
23
24
|
# - #latest: the chain's end (SessionChain.latest, `last:<id>`, Schedules)
|
|
24
25
|
# - #first_live: the first link not archived, or the end (Head: send, note)
|
|
25
26
|
# - #root: back to the first link (Schedules' one firing per chain)
|
|
27
|
+
# - #chain / #live_links: every link of +id+'s branch, root to end
|
|
28
|
+
# (Schedules#chain_of, the chain delete)
|
|
26
29
|
#
|
|
27
30
|
# The listing is a snapshot: one Links per question, or per tick.
|
|
28
31
|
class Links
|
|
@@ -80,6 +83,25 @@ module Samagotchi
|
|
|
80
83
|
seen.last
|
|
81
84
|
end
|
|
82
85
|
|
|
86
|
+
# Every link of +id+'s chain, root to end: from #latest(id) back
|
|
87
|
+
# through `continues`, reversed, so the walk stays on +id+'s own branch
|
|
88
|
+
# of a fork. A gone predecessor ends it on its id (as #root does: a
|
|
89
|
+
# schedule's anchor or head may name a deleted root); a loop ends where
|
|
90
|
+
# it would repeat.
|
|
91
|
+
# @return [Array<String>]
|
|
92
|
+
def chain(id)
|
|
93
|
+
ids = [latest(id)]
|
|
94
|
+
while (previous = continues(ids.last)) && !ids.include?(previous)
|
|
95
|
+
ids << previous
|
|
96
|
+
end
|
|
97
|
+
log_forks(ids)
|
|
98
|
+
ids.reverse
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# #chain without the gone ids: the links that still have a file.
|
|
102
|
+
# @return [Array<String>]
|
|
103
|
+
def live_links(id) = chain(id).reject { |link| gone?(link) }
|
|
104
|
+
|
|
83
105
|
private
|
|
84
106
|
|
|
85
107
|
# +id+ and the links after it, in order, until none follows, a link
|
|
@@ -104,6 +126,22 @@ module Samagotchi
|
|
|
104
126
|
nexts[session.continues] ||= session.id if session.continues
|
|
105
127
|
end
|
|
106
128
|
end
|
|
129
|
+
|
|
130
|
+
# continues id → the sessions that continue it, from the one listing.
|
|
131
|
+
def successors
|
|
132
|
+
@successors ||= by_id.each_value.select(&:continues).group_by(&:continues)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# A link continued twice (a hand edit, an old race): #chain follows
|
|
136
|
+
# +ids+' own branch and the other keeps a gap. Logged, not handled.
|
|
137
|
+
def log_forks(ids)
|
|
138
|
+
ids.each do |link|
|
|
139
|
+
others = Array(successors[link]).map(&:id) - ids
|
|
140
|
+
next if others.empty?
|
|
141
|
+
|
|
142
|
+
Log.debug(:worker, "chain_fork", sid: link[0, 8], others: others.map { |sid| sid[0, 8] })
|
|
143
|
+
end
|
|
144
|
+
end
|
|
107
145
|
end
|
|
108
146
|
end
|
|
109
147
|
end
|
|
@@ -19,8 +19,8 @@ module Samagotchi
|
|
|
19
19
|
# delete_on_exit?…). No Engine, no tool: the attached TUI, chi web,
|
|
20
20
|
# chi send and the prompt history require this file, not
|
|
21
21
|
# session_commands (which loads tools/execute and tools/builtins for
|
|
22
|
-
# !cmd and /context).
|
|
23
|
-
#
|
|
22
|
+
# !cmd and /context). Every caller of the class methods names this module
|
|
23
|
+
# directly; SessionCommands only includes it, for its constants.
|
|
24
24
|
module SessionCommandCatalog
|
|
25
25
|
MODEL_COMMAND = "/model"
|
|
26
26
|
MODELS_COMMAND = "/models"
|
|
@@ -69,16 +69,6 @@ module Samagotchi
|
|
|
69
69
|
# first (nothing changed): the line that applies it
|
|
70
70
|
Result = Struct.new(:status, :output, :changed, :model_name, :resume, :shell, :decision, :confirm, keyword_init: true)
|
|
71
71
|
|
|
72
|
-
# The catalogue's class methods (SessionCommandCatalog), for the
|
|
73
|
-
# worker-side callers that name SessionCommands.
|
|
74
|
-
def self.register_builtins(registry) = SessionCommandCatalog.register_builtins(registry)
|
|
75
|
-
def self.delete_on_exit?(line) = SessionCommandCatalog.delete_on_exit?(line)
|
|
76
|
-
def self.builtin_registry = SessionCommandCatalog.builtin_registry
|
|
77
|
-
def self.ui_only_entry(text) = SessionCommandCatalog.ui_only_entry(text)
|
|
78
|
-
def self.ui_only_refusal(name, id, runner:, remote: nil) = SessionCommandCatalog.ui_only_refusal(name, id, runner:, remote:)
|
|
79
|
-
def self.builtin_name(line) = SessionCommandCatalog.builtin_name(line)
|
|
80
|
-
def self.display_name(entry) = SessionCommandCatalog.display_name(entry)
|
|
81
|
-
|
|
82
72
|
# @return [String, nil] the model /model clear goes back to; nil when none
|
|
83
73
|
attr_reader :default_model
|
|
84
74
|
|
|
@@ -88,7 +78,7 @@ module Samagotchi
|
|
|
88
78
|
# @param save [#call] saves a session (a worker passes its state dir)
|
|
89
79
|
# @param registry [Commands::Registry] the commands #run looks lines up in
|
|
90
80
|
def initialize(engine:, turn_flow:, default_model:, save: ->(session) { session.save },
|
|
91
|
-
registry:
|
|
81
|
+
registry: SessionCommandCatalog.builtin_registry)
|
|
92
82
|
@engine = engine
|
|
93
83
|
@turn_flow = turn_flow
|
|
94
84
|
@default_model = default_model
|
|
@@ -107,13 +97,13 @@ module Samagotchi
|
|
|
107
97
|
# A name with a UI's entry and the session's (/recap): listed once,
|
|
108
98
|
# as the session's (registered first).
|
|
109
99
|
entries = @registry.entries.uniq(&:name).sort_by { |entry| [help_group(entry), entry.name] }
|
|
110
|
-
width = entries.map { |entry|
|
|
100
|
+
width = entries.map { |entry| SessionCommandCatalog.display_name(entry).length }.max
|
|
111
101
|
lines = entries.map do |entry|
|
|
112
102
|
notes = []
|
|
113
103
|
notes << entry.source unless entry.source == "core"
|
|
114
104
|
notes << MID_TURN_NOTES[entry.mid_turn_label] if MID_TURN_NOTES[entry.mid_turn_label]
|
|
115
105
|
notes << "#{ui_words(entry.uis)} only" if entry.local
|
|
116
|
-
line = " #{
|
|
106
|
+
line = " #{SessionCommandCatalog.display_name(entry).ljust(width)} #{entry.description}"
|
|
117
107
|
notes.empty? ? line : "#{line} (#{notes.join("; ")})"
|
|
118
108
|
end
|
|
119
109
|
"commands:\n#{lines.join("\n")}"
|