samagotchi 0.177.1 → 0.178.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 +27 -1
- data/docs/cli.md +27 -5
- data/docs/guardrails.md +1 -1
- data/docs/internals/background-tasks.md +23 -2
- data/docs/internals/bridge.md +17 -6
- data/docs/internals/remote-sessions.md +6 -1
- data/docs/internals/session-lifecycle.md +17 -8
- data/docs/internals/web-frontend.md +7 -3
- data/docs/internals/working-set.md +31 -5
- data/docs/scheduling.md +8 -1
- data/docs/sessions.md +9 -5
- data/lib/samagotchi/bridge/card_store.rb +2 -19
- data/lib/samagotchi/bridge.rb +38 -6
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/compat.rb +0 -7
- data/lib/samagotchi/engine/events.rb +43 -0
- data/lib/samagotchi/engine.rb +9 -3
- data/lib/samagotchi/event_fanout.rb +56 -0
- data/lib/samagotchi/guardrails/approvals.rb +0 -4
- data/lib/samagotchi/schedules.rb +9 -4
- data/lib/samagotchi/session_access/contract.rb +4 -0
- data/lib/samagotchi/session_access/local.rb +16 -4
- data/lib/samagotchi/session_access/remote.rb +42 -9
- data/lib/samagotchi/session_archive_command.rb +38 -10
- data/lib/samagotchi/session_chain/delete.rb +75 -25
- data/lib/samagotchi/session_delete_command.rb +33 -10
- data/lib/samagotchi/session_lifecycle/allowed.rb +12 -9
- data/lib/samagotchi/session_manager/lifecycle.rb +12 -26
- data/lib/samagotchi/session_manager/removal.rb +109 -25
- data/lib/samagotchi/session_manager/running_tasks.rb +334 -0
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/task_files.rb +37 -4
- data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +7 -1
- data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
- data/lib/samagotchi/terminal_ui/local_link.rb +11 -3
- data/lib/samagotchi/tools/task_runtime.rb +25 -6
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +5 -1
- data/lib/samagotchi/web/public/data.js +19 -7
- data/lib/samagotchi/web/public/delete_sheet.js +26 -7
- data/lib/samagotchi/web/public/running_tasks.js +73 -0
- data/lib/samagotchi/web/public/session_actions.js +57 -8
- data/lib/samagotchi/web/public/stop_sheet.js +3 -2
- data/lib/samagotchi/web/public/working_set.js +3 -28
- data/lib/samagotchi/web/public/working_set_view.js +5 -2
- data/lib/samagotchi/web/public/ws_item_sig.js +51 -0
- data/lib/samagotchi/web/public/ws_peek.js +23 -13
- data/lib/samagotchi/web/public/ws_reader.js +22 -12
- data/lib/samagotchi/web/public/ws_timeline.js +9 -6
- data/lib/samagotchi/web/session_control.rb +65 -16
- data/lib/samagotchi/web/working_set_routes.rb +29 -8
- data/lib/samagotchi/worker_link.rb +9 -1
- data/lib/samagotchi/working_set.rb +26 -0
- data/lib/samagotchi/working_set_command.rb +9 -6
- metadata +4 -1
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require_relative "session"
|
|
4
4
|
require_relative "session_manager"
|
|
5
5
|
require_relative "cli/command"
|
|
6
|
+
require_relative "cli/flags"
|
|
6
7
|
require_relative "session_access/local"
|
|
7
8
|
|
|
8
9
|
module Samagotchi
|
|
@@ -15,18 +16,24 @@ module Samagotchi
|
|
|
15
16
|
|
|
16
17
|
USAGE = {
|
|
17
18
|
"archive" => <<~TEXT,
|
|
18
|
-
Usage: chi sessions archive (ID|PREFIX)...
|
|
19
|
+
Usage: chi sessions archive [--stop-tasks] (ID|PREFIX)...
|
|
19
20
|
Hides each session (and its delegates) from every list and keeps it
|
|
20
21
|
for good: the retention sweep never deletes it. A live worker is
|
|
21
22
|
stopped first; a session running a turn, or in a one-turn chi run, is
|
|
22
|
-
refused
|
|
23
|
-
to one brings it back.
|
|
23
|
+
refused, and so is one whose background tasks still run. chi sessions
|
|
24
|
+
list --archived shows them; a message you send to one brings it back.
|
|
25
|
+
--stop-tasks stop the background tasks it and its delegates started
|
|
26
|
+
(TERM, then KILL), then archive
|
|
24
27
|
TEXT
|
|
25
28
|
"unarchive" => <<~TEXT
|
|
26
29
|
Usage: chi sessions unarchive (ID|PREFIX)...
|
|
27
30
|
Brings archived sessions (and their delegates) back to the lists.
|
|
28
31
|
TEXT
|
|
29
32
|
}.freeze
|
|
33
|
+
# archive's flags (unarchive takes none).
|
|
34
|
+
FLAGS = CLI::Flags.new(help: CLI::Command::HELP_WORDS) do |f|
|
|
35
|
+
f.switch "--stop-tasks"
|
|
36
|
+
end
|
|
30
37
|
|
|
31
38
|
# @param action [String] "archive" or "unarchive"
|
|
32
39
|
# @param argv [Array<String>] the arguments after "sessions <action>"
|
|
@@ -48,10 +55,10 @@ module Samagotchi
|
|
|
48
55
|
@stdout.puts(usage_text)
|
|
49
56
|
return 0
|
|
50
57
|
end
|
|
51
|
-
|
|
52
|
-
return
|
|
58
|
+
ids, stop_tasks = parse
|
|
59
|
+
return ids if ids.is_a?(Integer)
|
|
53
60
|
|
|
54
|
-
if
|
|
61
|
+
if ids.uniq.map { |given| @action == "archive" ? archive(given, stop_tasks: stop_tasks) : unarchive(given) }.all?
|
|
55
62
|
0
|
|
56
63
|
else
|
|
57
64
|
1
|
|
@@ -60,16 +67,36 @@ module Samagotchi
|
|
|
60
67
|
|
|
61
68
|
private
|
|
62
69
|
|
|
63
|
-
|
|
70
|
+
# @return [Array(Array<String>, Boolean), Integer] the ids and
|
|
71
|
+
# --stop-tasks, or the usage exit status
|
|
72
|
+
def parse
|
|
73
|
+
if @action == "archive"
|
|
74
|
+
parsed = parse_flags(FLAGS, @argv, stop_tasks: false)
|
|
75
|
+
return parsed if parsed.is_a?(Integer)
|
|
76
|
+
|
|
77
|
+
args = parsed.args
|
|
78
|
+
stop_tasks = parsed.options[:stop_tasks]
|
|
79
|
+
else
|
|
80
|
+
args = @argv
|
|
81
|
+
stop_tasks = false
|
|
82
|
+
end
|
|
83
|
+
bad = args.find { |arg| arg.start_with?("-") }
|
|
84
|
+
return usage_error(bad ? "unknown option #{bad}" : "give session ids") if bad || args.empty?
|
|
85
|
+
|
|
86
|
+
[args, stop_tasks]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def archive(given, stop_tasks: false)
|
|
64
90
|
id = @access.resolve_id(given)
|
|
65
91
|
short = id[0, 8]
|
|
66
|
-
result = @access.archive(id)
|
|
92
|
+
result = stop_tasks ? @access.archive(id, stop_tasks: true) : @access.archive(id)
|
|
67
93
|
if result.archived.include?(id)
|
|
68
94
|
notes = []
|
|
69
95
|
notes << "stopped its worker" if result.stopped.include?(id)
|
|
96
|
+
notes.concat(SessionManager::RunningTasks.stop_words(result.to_h))
|
|
70
97
|
others = result.archived.size - 1
|
|
71
98
|
notes << "and #{others} delegate#{"s" if others != 1}" if others.positive?
|
|
72
|
-
@stdout.puts("#{short} archived#{" (#{notes.join("
|
|
99
|
+
@stdout.puts("#{short} archived#{" (#{notes.join("; ")})" unless notes.empty?}")
|
|
73
100
|
else
|
|
74
101
|
@stdout.puts("#{short} empty session discarded")
|
|
75
102
|
end
|
|
@@ -79,7 +106,8 @@ module Samagotchi
|
|
|
79
106
|
@stdout.puts("#{short} refused: #{where} #{OwnerLock::TUI_BUSY}; let it finish first")
|
|
80
107
|
false
|
|
81
108
|
rescue SessionManager::ArchiveRefused => e
|
|
82
|
-
|
|
109
|
+
hint = " (#{SessionManager::RunningTasks.hint(nil, "archive", short)})" if e.reason == :tasks_running
|
|
110
|
+
@stdout.puts("#{short} refused: #{e.message}#{hint}")
|
|
83
111
|
false
|
|
84
112
|
rescue ArgumentError => e
|
|
85
113
|
error_line("chi sessions archive: #{e.message}")
|
|
@@ -147,7 +147,11 @@ module Samagotchi
|
|
|
147
147
|
# @raise [SessionManager::DeleteRefused] :continuing, :scheduled,
|
|
148
148
|
# :delegate_unmovable, :worker_running, :still_stopping
|
|
149
149
|
# @raise [SessionManager::OwnedByTUI]
|
|
150
|
-
|
|
150
|
+
# @param running_tasks [SessionManager::RunningTasks::Policy] for the
|
|
151
|
+
# link and the delegates that go with it (check_link!, then each
|
|
152
|
+
# one's removal under its lock)
|
|
153
|
+
def link(id, state_dir:, stop:, wait:, revive_previous:, refuse_continuing:, expect: nil,
|
|
154
|
+
running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
151
155
|
ContinueIntent.hold(id, state_dir: state_dir) do |held|
|
|
152
156
|
raise SessionManager::DeleteRefused.new(id, :continuing) if refuse_continuing && !held
|
|
153
157
|
|
|
@@ -155,11 +159,15 @@ module Samagotchi
|
|
|
155
159
|
unless chained?(id, links)
|
|
156
160
|
raise SessionManager::DeleteRefused.new(id, :plan_changed) unless expect.nil?
|
|
157
161
|
|
|
158
|
-
return plain(SessionManager.delete_resolved(id, state_dir: state_dir, stop: stop, wait: wait
|
|
162
|
+
return plain(SessionManager.delete_resolved(id, state_dir: state_dir, stop: stop, wait: wait,
|
|
163
|
+
running_tasks: running_tasks))
|
|
159
164
|
end
|
|
160
165
|
|
|
161
166
|
plan = link_plan(id, state_dir: state_dir, links: links, undo: revive_previous)
|
|
162
|
-
|
|
167
|
+
unless plan.previous
|
|
168
|
+
return delete_link(plan, state_dir: state_dir, stop: stop, wait: wait, expect: expect,
|
|
169
|
+
running_tasks: running_tasks)
|
|
170
|
+
end
|
|
163
171
|
|
|
164
172
|
# Taken before the link's file goes: after it, a continue of the
|
|
165
173
|
# previous link would no longer be refused. The plan is made again
|
|
@@ -168,7 +176,8 @@ module Samagotchi
|
|
|
168
176
|
raise SessionManager::DeleteRefused.new(plan.previous, :continuing) unless previous_held
|
|
169
177
|
|
|
170
178
|
again = link_plan(id, state_dir: state_dir, links: Links.new(state_dir: state_dir), undo: true)
|
|
171
|
-
return delete_link(again, state_dir: state_dir, stop: stop, wait: wait, expect: expect
|
|
179
|
+
return delete_link(again, state_dir: state_dir, stop: stop, wait: wait, expect: expect,
|
|
180
|
+
running_tasks: running_tasks)
|
|
172
181
|
end
|
|
173
182
|
end
|
|
174
183
|
end
|
|
@@ -184,12 +193,17 @@ module Samagotchi
|
|
|
184
193
|
# The refusals a link's delete makes before anything changes (the
|
|
185
194
|
# sheet's too): a schedule's target, anchor or head, on the link or a
|
|
186
195
|
# delegate that would go; a moved delegate that can't go back; a
|
|
187
|
-
# delegate that would go in a one-turn run
|
|
196
|
+
# delegate that would go in a one-turn run; with REFUSE, a background
|
|
197
|
+
# task the link or such a delegate started that still runs.
|
|
188
198
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
189
|
-
def check_link!(plan, state_dir)
|
|
199
|
+
def check_link!(plan, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
190
200
|
SessionManager.refuse_scheduled!(plan.id, state_dir)
|
|
191
201
|
refuse_unmovable!(plan.id, plan.moved, state_dir)
|
|
192
202
|
precheck!(plan.delegates.delete, state_dir)
|
|
203
|
+
SessionManager::RunningTasks.check!([*plan.delegates.delete, plan.id], state_dir: state_dir,
|
|
204
|
+
policy: running_tasks) do |listing|
|
|
205
|
+
SessionManager::DeleteRefused.new(plan.id, :tasks_running, tasks: listing)
|
|
206
|
+
end
|
|
193
207
|
end
|
|
194
208
|
|
|
195
209
|
# Every refusal first (check_link!), then the link's worker stopped,
|
|
@@ -199,29 +213,52 @@ module Samagotchi
|
|
|
199
213
|
# files. So a refusal (a schedule on a delegate, a one-turn run, a
|
|
200
214
|
# worker that took the link or a delegate after the checks) leaves
|
|
201
215
|
# every session in place.
|
|
202
|
-
def delete_link(plan, state_dir:, stop:, wait:, expect: nil
|
|
216
|
+
def delete_link(plan, state_dir:, stop:, wait:, expect: nil,
|
|
217
|
+
running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
203
218
|
raise SessionManager::DeleteRefused.new(plan.id, :plan_changed) if !expect.nil? && expect != digest(plan)
|
|
204
219
|
|
|
205
220
|
id = plan.id
|
|
206
|
-
check_link!(plan, state_dir)
|
|
221
|
+
check_link!(plan, state_dir, running_tasks: running_tasks)
|
|
207
222
|
stopped = SessionManager.release_for_delete!(id, state_dir: state_dir, stop: stop, wait: wait)
|
|
208
223
|
path = Session.session_file(id, state_dir: state_dir)
|
|
209
224
|
dir = Session.session_dir(id, state_dir: state_dir)
|
|
210
225
|
doomed = [*plan.delegates.delete, id]
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
226
|
+
# REFUSE lists the whole batch once under its locks, before any of it
|
|
227
|
+
# goes; the removals then don't check again (a later id's refusal
|
|
228
|
+
# would leave the earlier ones gone).
|
|
229
|
+
each = SessionManager::RunningTasks.after_check(running_tasks)
|
|
230
|
+
removed = begin
|
|
231
|
+
locked(doomed, state_dir) do
|
|
232
|
+
refuse_tasks!(doomed, plan.id, state_dir, running_tasks)
|
|
233
|
+
remove_locked(plan.delegates.delete, state_dir, running_tasks: each)
|
|
234
|
+
if plan.previous
|
|
235
|
+
SessionContinue.undo_deleted_link(from: plan.previous, link: id, moved: plan.moved, state_dir: state_dir) do
|
|
236
|
+
SessionManager.remove_files(id, path, dir, state_dir: state_dir, purge_runs: false, locked: true,
|
|
237
|
+
running_tasks: each)
|
|
238
|
+
end
|
|
239
|
+
else
|
|
240
|
+
SessionManager.remove_files(id, path, dir, state_dir: state_dir, purge_runs: false, locked: true,
|
|
241
|
+
running_tasks: each)
|
|
216
242
|
end
|
|
217
|
-
else
|
|
218
|
-
SessionManager.remove_files(id, path, dir, state_dir: state_dir, purge_runs: false, locked: true)
|
|
219
243
|
end
|
|
244
|
+
rescue SessionManager::DeleteRefused => e
|
|
245
|
+
raise e unless stopped && e.reason == :tasks_running
|
|
246
|
+
|
|
247
|
+
# The worker stopped was the link's, whichever id refused.
|
|
248
|
+
raise e.with_worker_stopped(id)
|
|
220
249
|
end
|
|
221
250
|
SessionManager.purge_removal_stores(doomed, state_dir: state_dir)
|
|
222
251
|
unarchived = plan.revive.select { |sid| ArchiveStore.unarchive(sid, state_dir: state_dir) }
|
|
223
252
|
{ id: id, removed: removed, stopped: stopped, unarchived: unarchived, moved_back: plan.moved,
|
|
224
|
-
deleted: doomed, left_running: plan.delegates.leave }
|
|
253
|
+
deleted: doomed, left_running: plan.delegates.leave, **running_tasks.result_fields }
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# REFUSE's last check of a batch under its locks (+ids+ all held):
|
|
257
|
+
# DeleteRefused :tasks_running for +id+ before any of them goes.
|
|
258
|
+
def refuse_tasks!(ids, id, state_dir, running_tasks, links: [])
|
|
259
|
+
SessionManager::RunningTasks.refuse_batch!(ids, state_dir: state_dir, policy: running_tasks) do |listing|
|
|
260
|
+
SessionManager::DeleteRefused.new(id, :tasks_running, tasks: listing, links: links)
|
|
261
|
+
end
|
|
225
262
|
end
|
|
226
263
|
|
|
227
264
|
# The refusals of every id a batch would delete, before the first
|
|
@@ -269,10 +306,10 @@ module Samagotchi
|
|
|
269
306
|
# +ids+' files, in order, each locked by the caller (#locked); the
|
|
270
307
|
# session logs are the caller's to purge, once.
|
|
271
308
|
# @return [Array<String>] the paths removed
|
|
272
|
-
def remove_locked(ids, state_dir)
|
|
309
|
+
def remove_locked(ids, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
273
310
|
ids.flat_map do |sid|
|
|
274
311
|
SessionManager.remove_files(sid, Session.session_file(sid, state_dir: state_dir), Session.session_dir(sid, state_dir: state_dir),
|
|
275
|
-
state_dir: state_dir, purge_runs: false, locked: true)
|
|
312
|
+
state_dir: state_dir, purge_runs: false, locked: true, running_tasks: running_tasks)
|
|
276
313
|
end
|
|
277
314
|
end
|
|
278
315
|
|
|
@@ -335,26 +372,32 @@ module Samagotchi
|
|
|
335
372
|
# earlier ones; refusal: the message of a part-way refusal, or nil)
|
|
336
373
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
337
374
|
# nothing was deleted
|
|
338
|
-
|
|
375
|
+
# @param running_tasks [SessionManager::RunningTasks::Policy] for every
|
|
376
|
+
# link and delegate that goes: REFUSE refuses up front (check_chain!)
|
|
377
|
+
# and again under each link's locks; a stop runs link by link
|
|
378
|
+
# @return also tasks_stopped:, tasks_not_stopped:, tasks_ended:
|
|
379
|
+
def chain(id, state_dir:, stop:, wait:, expect: nil, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
339
380
|
links = Links.new(state_dir: state_dir)
|
|
340
381
|
plan = chain_plan(id, state_dir: state_dir, links: links)
|
|
341
382
|
raise SessionManager::DeleteRefused.new(plan.id, :plan_changed) if !expect.nil? && expect != digest(plan)
|
|
342
383
|
|
|
343
|
-
check_chain!(plan, state_dir, stop: stop)
|
|
384
|
+
check_chain!(plan, state_dir, stop: stop, running_tasks: running_tasks)
|
|
344
385
|
hold_all(plan.links, state_dir) do
|
|
345
386
|
stopped = plan.links.select do |link|
|
|
346
387
|
SessionManager.release_for_delete!(link, state_dir: state_dir, stop: stop, wait: wait)
|
|
347
388
|
end
|
|
348
|
-
delete_links(plan, links, state_dir
|
|
389
|
+
delete_links(plan, links, state_dir, running_tasks: running_tasks)
|
|
390
|
+
.merge(stopped: stopped, cut_off: plan.cut_off, **running_tasks.result_fields)
|
|
349
391
|
end
|
|
350
392
|
end
|
|
351
393
|
|
|
352
394
|
# The refusals a chain's delete makes before anything goes (the
|
|
353
395
|
# sheet's too): a schedule on the chain, a link in a one-turn run,
|
|
354
396
|
# without +stop+ a link with a worker, then every delegate that would
|
|
355
|
-
# go (precheck!: a schedule on one, a one-turn run)
|
|
397
|
+
# go (precheck!: a schedule on one, a one-turn run), then with REFUSE
|
|
398
|
+
# a background task any of them started that still runs.
|
|
356
399
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
357
|
-
def check_chain!(plan, state_dir, stop:)
|
|
400
|
+
def check_chain!(plan, state_dir, stop:, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
358
401
|
schedule = Schedules.new(state_dir: state_dir).for_chain(plan.id).first
|
|
359
402
|
raise SessionManager::DeleteRefused.new(plan.id, :chain_scheduled, schedule_id: schedule.id) if schedule
|
|
360
403
|
|
|
@@ -364,6 +407,10 @@ module Samagotchi
|
|
|
364
407
|
raise SessionManager::DeleteRefused.new(link, :worker_running) if !stop && (presence.owned? || presence.starting?)
|
|
365
408
|
end
|
|
366
409
|
precheck!(plan.delegates.values.flat_map(&:delete), state_dir)
|
|
410
|
+
doomed = [*plan.links, *plan.delegates.values.flat_map(&:delete)]
|
|
411
|
+
SessionManager::RunningTasks.check!(doomed, state_dir: state_dir, policy: running_tasks) do |listing|
|
|
412
|
+
SessionManager::DeleteRefused.new(plan.id, :tasks_running, tasks: listing, links: plan.links)
|
|
413
|
+
end
|
|
367
414
|
end
|
|
368
415
|
|
|
369
416
|
# The block inside a shared continue hold of every id.
|
|
@@ -380,7 +427,7 @@ module Samagotchi
|
|
|
380
427
|
|
|
381
428
|
# Each link with its delegates under their locks, newest first; a
|
|
382
429
|
# refused one keeps it and every earlier link whole.
|
|
383
|
-
def delete_links(plan, links, state_dir)
|
|
430
|
+
def delete_links(plan, links, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
384
431
|
done = { id: plan.id, deleted: [], left: [], left_running: [], unarchived: [], refusal: nil }
|
|
385
432
|
newest_first = plan.links.reverse
|
|
386
433
|
record = nil
|
|
@@ -389,7 +436,10 @@ module Samagotchi
|
|
|
389
436
|
batch = [*delegates.delete, link]
|
|
390
437
|
deleted_record = ContinuedFrom.read(link, state_dir: state_dir)
|
|
391
438
|
begin
|
|
392
|
-
locked(batch, state_dir)
|
|
439
|
+
locked(batch, state_dir) do
|
|
440
|
+
refuse_tasks!(batch, link, state_dir, running_tasks)
|
|
441
|
+
remove_locked(batch, state_dir, running_tasks: SessionManager::RunningTasks.after_check(running_tasks))
|
|
442
|
+
end
|
|
393
443
|
rescue SessionManager::DeleteRefused, SessionManager::OwnedByTUI, Session::NotFound => e
|
|
394
444
|
raise if done[:deleted].empty?
|
|
395
445
|
|
|
@@ -14,7 +14,7 @@ module Samagotchi
|
|
|
14
14
|
include CLI::Command
|
|
15
15
|
|
|
16
16
|
USAGE = <<~TEXT
|
|
17
|
-
Usage: chi sessions delete [--force] [--chain] (ID|PREFIX)...
|
|
17
|
+
Usage: chi sessions delete [--force] [--chain] [--stop-tasks] (ID|PREFIX)...
|
|
18
18
|
Deletes each session: its history, notes, images and queued input.
|
|
19
19
|
This can't be undone. Deleting a chain's newest link undoes its
|
|
20
20
|
continue: the previous link comes back to the lists (not started),
|
|
@@ -24,11 +24,16 @@ module Samagotchi
|
|
|
24
24
|
chi run (chi --non-interactive) is always refused
|
|
25
25
|
--chain delete every link of the session's chain, with their
|
|
26
26
|
delegates; running delegates are left running
|
|
27
|
+
--stop-tasks stop the background tasks the deleted sessions started
|
|
28
|
+
(TERM, then KILL), then delete; without it a session
|
|
29
|
+
whose tasks still run is refused. Not --force: a live
|
|
30
|
+
worker is still refused without it
|
|
27
31
|
TEXT
|
|
28
32
|
PREVIEW_LIMIT = 60
|
|
29
33
|
FLAGS = CLI::Flags.new(help: CLI::Command::HELP_WORDS) do |f|
|
|
30
34
|
f.switch "-f", "--force"
|
|
31
35
|
f.switch "--chain"
|
|
36
|
+
f.switch "--stop-tasks"
|
|
32
37
|
end
|
|
33
38
|
|
|
34
39
|
# @param argv [Array<String>] the arguments after "sessions delete"
|
|
@@ -49,7 +54,8 @@ module Samagotchi
|
|
|
49
54
|
return options if options.is_a?(Integer)
|
|
50
55
|
|
|
51
56
|
gone = options[:ids].uniq.map do |given|
|
|
52
|
-
|
|
57
|
+
how = { force: options[:force], stop_tasks: options[:stop_tasks] }
|
|
58
|
+
options[:chain] ? delete_chain(given, **how) : delete(given, **how)
|
|
53
59
|
end
|
|
54
60
|
gone.all? ? 0 : 1
|
|
55
61
|
end
|
|
@@ -61,7 +67,7 @@ module Samagotchi
|
|
|
61
67
|
# @return [Hash, Integer] the options, or the exit status after the
|
|
62
68
|
# help or a usage error
|
|
63
69
|
def parse
|
|
64
|
-
parsed = parse_flags(FLAGS, @argv, force: false, chain: false)
|
|
70
|
+
parsed = parse_flags(FLAGS, @argv, force: false, chain: false, stop_tasks: false)
|
|
65
71
|
return parsed if parsed.is_a?(Integer)
|
|
66
72
|
return usage_error("give session ids") if parsed.args.empty?
|
|
67
73
|
|
|
@@ -69,13 +75,12 @@ module Samagotchi
|
|
|
69
75
|
end
|
|
70
76
|
|
|
71
77
|
# @return [Boolean] whether the session is gone
|
|
72
|
-
def delete(given, force:)
|
|
78
|
+
def delete(given, force:, stop_tasks: false)
|
|
73
79
|
id = @access.resolve_id(given)
|
|
74
80
|
short = id[0, 8]
|
|
75
81
|
preview = preview_of(id)
|
|
76
|
-
result = @access.delete(id, force: force)
|
|
77
|
-
@stdout.puts(["#{short} deleted#{
|
|
78
|
-
preview].compact.join(" "))
|
|
82
|
+
result = stop_tasks ? @access.delete(id, force: force, stop_tasks: true) : @access.delete(id, force: force)
|
|
83
|
+
@stdout.puts(["#{short} deleted#{notes(result)}#{revived(result)}#{delegates(result, id)}", preview].compact.join(" "))
|
|
79
84
|
true
|
|
80
85
|
rescue SessionManager::OwnedByTUI, SessionManager::DeleteRefused => e
|
|
81
86
|
refused(e.session_id && e.session_id != id ? e.session_id[0, 8] : short, e)
|
|
@@ -87,6 +92,14 @@ module Samagotchi
|
|
|
87
92
|
false
|
|
88
93
|
end
|
|
89
94
|
|
|
95
|
+
# " (stopped its worker; stopped 2 background tasks)", or "".
|
|
96
|
+
def notes(result, worker: result[:stopped] == true)
|
|
97
|
+
words = []
|
|
98
|
+
words << "stopped its worker" if worker
|
|
99
|
+
words.concat(SessionManager::RunningTasks.stop_words(result))
|
|
100
|
+
words.empty? ? "" : " (#{words.join("; ")})"
|
|
101
|
+
end
|
|
102
|
+
|
|
90
103
|
# "; the previous link 1a2b3c4d is back in the lists" (D1), with the
|
|
91
104
|
# delegates that went back to it.
|
|
92
105
|
def revived(result)
|
|
@@ -111,10 +124,12 @@ module Samagotchi
|
|
|
111
124
|
|
|
112
125
|
# --chain: one line per link deleted, then a summary.
|
|
113
126
|
# @return [Boolean] whether every link went
|
|
114
|
-
def delete_chain(given, force:)
|
|
127
|
+
def delete_chain(given, force:, stop_tasks: false)
|
|
115
128
|
id = @access.resolve_id(given)
|
|
116
|
-
result = @access.delete_chain(id, force: force)
|
|
117
|
-
result[:deleted].each
|
|
129
|
+
result = stop_tasks ? @access.delete_chain(id, force: force, stop_tasks: true) : @access.delete_chain(id, force: force)
|
|
130
|
+
result[:deleted].each do |sid|
|
|
131
|
+
@stdout.puts("#{sid[0, 8]} deleted#{notes(tasks_of(result, sid), worker: result[:stopped].include?(sid))}")
|
|
132
|
+
end
|
|
118
133
|
@stdout.puts(chain_summary(result))
|
|
119
134
|
result[:left].empty?
|
|
120
135
|
rescue SessionManager::OwnedByTUI, SessionManager::DeleteRefused => e
|
|
@@ -124,6 +139,13 @@ module Samagotchi
|
|
|
124
139
|
false
|
|
125
140
|
end
|
|
126
141
|
|
|
142
|
+
# +result+'s task lists cut to the ones session +sid+ started.
|
|
143
|
+
def tasks_of(result, sid)
|
|
144
|
+
%i[tasks_stopped tasks_not_stopped].to_h do |key|
|
|
145
|
+
[key, Array(result[key]).select { |task| task.session_id == sid }]
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
127
149
|
def chain_summary(result)
|
|
128
150
|
parts = ["#{result[:deleted].size} deleted"]
|
|
129
151
|
parts << "#{result[:left].size} links kept: #{result[:refusal]}" unless result[:left].empty?
|
|
@@ -143,6 +165,7 @@ module Samagotchi
|
|
|
143
165
|
case error.reason
|
|
144
166
|
when :still_stopping then "its worker is still shutting down; try again in a moment"
|
|
145
167
|
when :worker_running then "its worker is running (--force stops it first)"
|
|
168
|
+
when :tasks_running then "#{error.message} (#{SessionManager::RunningTasks.hint(nil, "delete", short)})"
|
|
146
169
|
else error.message
|
|
147
170
|
end
|
|
148
171
|
end
|
|
@@ -23,19 +23,22 @@ module Samagotchi
|
|
|
23
23
|
# What each literal operation answers in each state today, with neutral
|
|
24
24
|
# op-local facts (not scratch, saved status idle, no input/, no question
|
|
25
25
|
# or offer, not SIGSTOPped, features restart and activity, an idle worker
|
|
26
|
-
# with no streams). worker_busy is
|
|
27
|
-
# or leaving"
|
|
28
|
-
# is
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
26
|
+
# with no streams). For schedules, worker_busy is "its worker is starting
|
|
27
|
+
# or leaving", deleting is DeliverResult::DELETING's detail, and
|
|
28
|
+
# worker_newer is WorkerLink::Skew#short_message. Resume's live is "a
|
|
29
|
+
# worker holds it"; restart's fallback is its unresponsive-worker path,
|
|
30
|
+
# its stop_marked a stop marked and its deleting a deleter holds the lock
|
|
31
|
+
# (it reads this table: restart_session's precheck); attach's gone_* are
|
|
32
|
+
# LocalLink's [:gone, why]. absent's delete and schedule cells are
|
|
33
|
+
# unreachable (a gone session is refused before the gate). Resolution (a
|
|
34
|
+
# chain's head, a continue's successor) stays outside: these are the
|
|
35
|
+
# link's own.
|
|
33
36
|
LITERAL = {
|
|
34
37
|
absent: %i[gone gone gone gone go go go wait],
|
|
35
38
|
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
|
|
39
|
+
deleting: %i[live deleting go go worker_running deleting deleting wait],
|
|
37
40
|
stopping: %i[live stop_marked go go worker_running stopped stopped gone_stopped],
|
|
38
|
-
skew: %i[live fallback go go worker_running
|
|
41
|
+
skew: %i[live fallback go go worker_running worker_newer worker_newer gone_worker_newer],
|
|
39
42
|
leaving: %i[live not_running go go worker_running worker_busy worker_busy wait],
|
|
40
43
|
booting_stuck: %i[live fallback go go worker_running worker_busy worker_busy wait],
|
|
41
44
|
booting: %i[live starting go go worker_running worker_busy worker_busy wait],
|
|
@@ -11,10 +11,8 @@ require_relative "../reminder_carry"
|
|
|
11
11
|
require_relative "../session"
|
|
12
12
|
require_relative "../session_inbox"
|
|
13
13
|
require_relative "../session_lifecycle"
|
|
14
|
-
require_relative "../task_files"
|
|
15
|
-
require_relative "../tools/task_record"
|
|
16
|
-
require_relative "../task_transcript"
|
|
17
14
|
require_relative "../worker_link"
|
|
15
|
+
require_relative "running_tasks"
|
|
18
16
|
|
|
19
17
|
module Samagotchi
|
|
20
18
|
class SessionManager
|
|
@@ -25,7 +23,8 @@ module Samagotchi
|
|
|
25
23
|
# :fallback_held (disk-visible work blocks an unresponsive-worker restart),
|
|
26
24
|
# :stopped (its worker, +pid+, is SIGSTOPped: neither the ask nor a TERM
|
|
27
25
|
# reaches it until it is continued), :stop_marked (the session is stopped,
|
|
28
|
-
# or being stopped: `chi sessions stop`, no successor would come),
|
|
26
|
+
# or being stopped: `chi sessions stop`, no successor would come),
|
|
27
|
+
# :deleting (a deleter holds the lock: the session is going), or
|
|
29
28
|
# :failed (the worker answered something else or didn't release).
|
|
30
29
|
class RestartRefused < StandardError
|
|
31
30
|
attr_reader :session_id, :reason, :version, :holds, :pid
|
|
@@ -49,7 +48,7 @@ module Samagotchi
|
|
|
49
48
|
reminders: "it has reminders, which its worker's older chi can't carry over a restart",
|
|
50
49
|
command_running: "a /btw (or another command) is still running",
|
|
51
50
|
relays_open: "a delegate's approval relayed here is open or just answered",
|
|
52
|
-
background_tasks:
|
|
51
|
+
background_tasks: RunningTasks::LEAD,
|
|
53
52
|
scratch: "a chi scratch session's worker ends with its terminal"
|
|
54
53
|
}.merge(WorkerIdleExit::LEAVING_WORDS).freeze
|
|
55
54
|
|
|
@@ -63,6 +62,8 @@ module Samagotchi
|
|
|
63
62
|
"a restart: continue it#{" with `kill -CONT #{pid}`" if pid}#{where}, then restart again"
|
|
64
63
|
when :stop_marked
|
|
65
64
|
"session #{session_id} can't restart: #{DeliverResult::STOPPED["detail"]} (on the newest chi)"
|
|
65
|
+
when :deleting
|
|
66
|
+
"session #{session_id} can't restart: #{DeliverResult::DELETING["detail"]}"
|
|
66
67
|
when :not_running
|
|
67
68
|
"session #{session_id} has no running worker; the next prompt starts one on the newest chi"
|
|
68
69
|
when :unsupported
|
|
@@ -163,7 +164,7 @@ module Samagotchi
|
|
|
163
164
|
Log.info(:worker, "restart_fallback", sid: session_id, why: why, error: error&.class&.name, msg: error&.message)
|
|
164
165
|
dir = Session.session_dir(session_id, state_dir: state_dir)
|
|
165
166
|
session = Session.load(session_id, state_dir: state_dir)
|
|
166
|
-
holds = restart_fallback_holds(session, dir)
|
|
167
|
+
holds = restart_fallback_holds(session, dir, state_dir)
|
|
167
168
|
raise RestartRefused.new(session_id, :fallback_held, holds: holds) unless holds.empty?
|
|
168
169
|
raise RestartRefused.new(session_id, :failed, version: sidecar&.version) unless (owner&.pid || 0).to_i.positive?
|
|
169
170
|
|
|
@@ -195,33 +196,18 @@ module Samagotchi
|
|
|
195
196
|
Restarted.new(session_id: session_id, from_version: sidecar&.version, version: (fresh.version if changed))
|
|
196
197
|
end
|
|
197
198
|
|
|
198
|
-
|
|
199
|
+
# A task that may still run holds it (RunningTasks.of: a live one, one
|
|
200
|
+
# chi can't verify, an unreadable record); a record left marked
|
|
201
|
+
# running by a crashed worker whose pid is gone doesn't.
|
|
202
|
+
private def restart_fallback_holds(session, session_dir, state_dir)
|
|
199
203
|
holds = []
|
|
200
204
|
holds << :turn_running if session.status == Session::STATUS_RUNNING
|
|
201
205
|
holds << :question_pending if session.pending_question
|
|
202
206
|
holds << :input_queued unless SessionInbox.find_new_input_files(session_dir).empty?
|
|
203
|
-
holds << :background_tasks
|
|
207
|
+
holds << :background_tasks unless RunningTasks.of([session.id], state_dir: state_dir).empty?
|
|
204
208
|
holds
|
|
205
209
|
end
|
|
206
210
|
|
|
207
|
-
private def running_created_task?(session)
|
|
208
|
-
root = session.working_directory.to_s
|
|
209
|
-
return false if root.empty?
|
|
210
|
-
|
|
211
|
-
TaskTranscript.created_ids_in(session.messages).any? do |id|
|
|
212
|
-
next false unless TaskFiles::TASK_ID.match?(id)
|
|
213
|
-
|
|
214
|
-
path = File.join(root, TaskFiles::TASKS_DIR, id, TaskFiles::METADATA_FILENAME)
|
|
215
|
-
next false unless File.file?(path)
|
|
216
|
-
|
|
217
|
-
Tools::TaskRecord.from_file(JSON.parse(File.read(path)))&.running?
|
|
218
|
-
rescue JSON::ParserError, SystemCallError => e
|
|
219
|
-
Log.warn(:worker, "restart_task_check_failed", sid: session.id, task_id: id,
|
|
220
|
-
error: e.class.name, msg: e.message)
|
|
221
|
-
true
|
|
222
|
-
end
|
|
223
|
-
end
|
|
224
|
-
|
|
225
211
|
# Stop a session by sending TERM (then CONT, for a stopped one) to its process.
|
|
226
212
|
# With +wait+ (seconds), also wait for the owner to let go of the session,
|
|
227
213
|
# so a resume right after spawns a fresh worker instead of finding the
|