samagotchi 0.177.0 → 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 +44 -1
- data/docs/architecture.md +5 -4
- data/docs/cli.md +31 -6
- data/docs/guardrails.md +1 -1
- data/docs/internals/background-tasks.md +23 -2
- data/docs/internals/bridge.md +17 -6
- data/docs/internals/bundles.md +4 -3
- data/docs/internals/participants.md +8 -5
- data/docs/internals/remote-sessions.md +6 -1
- data/docs/internals/session-lifecycle.md +176 -0
- data/docs/internals/web-frontend.md +14 -6
- data/docs/internals/working-set.md +33 -6
- data/docs/plugins.md +1 -1
- data/docs/releasing.md +3 -2
- data/docs/scheduling.md +8 -1
- data/docs/sessions.md +15 -9
- data/docs/testing.md +4 -2
- data/lib/samagotchi/bridge/card_store.rb +2 -19
- data/lib/samagotchi/bridge.rb +38 -6
- data/lib/samagotchi/bundle_command/check.rb +1 -1
- 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/session.rb +7 -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/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 +12 -5
- 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 +98 -25
- data/lib/samagotchi/session_chain/delete_preview.rb +2 -2
- data/lib/samagotchi/session_delete_command.rb +33 -10
- data/lib/samagotchi/session_inbox.rb +29 -0
- data/lib/samagotchi/session_inbox_delivery.rb +13 -5
- data/lib/samagotchi/session_lifecycle/allowed.rb +69 -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 +33 -39
- data/lib/samagotchi/session_manager/removal.rb +117 -25
- data/lib/samagotchi/session_manager/running_tasks.rb +334 -0
- 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/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/tool_names.rb +1 -1
- data/lib/samagotchi/tools/memory.rb +29 -0
- data/lib/samagotchi/tools/task_runtime.rb +25 -6
- 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/app.rb +5 -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 +19 -3
- data/lib/samagotchi/web/public/delete_sheet.js +67 -11
- 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_sections.js +1 -0
- data/lib/samagotchi/web/public/ws_timeline.js +9 -6
- data/lib/samagotchi/web/session_control.rb +66 -16
- data/lib/samagotchi/web/session_summary.rb +7 -0
- data/lib/samagotchi/web/working_set_routes.rb +29 -8
- data/lib/samagotchi/worker_boot.rb +9 -6
- data/lib/samagotchi/worker_link.rb +9 -1
- data/lib/samagotchi/worker_presence.rb +4 -0
- data/lib/samagotchi/working_set.rb +26 -0
- data/lib/samagotchi/working_set_command.rb +10 -7
- data/lib/samagotchi/working_set_steer.rb +3 -10
- metadata +9 -1
|
@@ -318,6 +318,8 @@ module Samagotchi
|
|
|
318
318
|
BOUND_FEATURE = "bound_grants"
|
|
319
319
|
# Web::App::CHAIN_DELETE_FEATURE: DELETE …/chain.
|
|
320
320
|
CHAIN_DELETE_FEATURE = "chain_delete"
|
|
321
|
+
# Web::App::STOP_TASKS_FEATURE: delete and archive take stop_tasks.
|
|
322
|
+
STOP_TASKS_FEATURE = "stop_tasks"
|
|
321
323
|
|
|
322
324
|
# The header that chi web names the signed-in user in (GET
|
|
323
325
|
# /api/info's proxy), nil when it has no such proxy, or is a chi
|
|
@@ -478,12 +480,23 @@ module Samagotchi
|
|
|
478
480
|
end
|
|
479
481
|
|
|
480
482
|
# @return [SessionManager::ArchiveResult] the reply's lists coerced to arrays
|
|
481
|
-
|
|
482
|
-
|
|
483
|
+
# @raise [RemoteTooOld] +stop_tasks+ and a chi web without STOP_TASKS_FEATURE
|
|
484
|
+
def archive(id, stop_tasks: false)
|
|
485
|
+
require_feature!(STOP_TASKS_FEATURE) if stop_tasks
|
|
486
|
+
reply = request(:post, "#{session_path(id)}/archive", body: ({ stop_tasks: true } if stop_tasks))
|
|
483
487
|
raise_refusal(reply) unless reply.status == 200
|
|
484
488
|
|
|
485
489
|
SessionManager::ArchiveResult.new(id: reply.json["session_id"] || id, archived: Array(reply.json["archived"]),
|
|
486
|
-
stopped: Array(reply.json["stopped"]), discarded: Array(reply.json["discarded"])
|
|
490
|
+
stopped: Array(reply.json["stopped"]), discarded: Array(reply.json["discarded"]),
|
|
491
|
+
**task_fields(reply.json))
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# The 200's tasks_stopped, tasks_not_stopped and tasks_ended as
|
|
495
|
+
# RunningTasks::RunningTask lists ([] from a chi web without them).
|
|
496
|
+
private def task_fields(json)
|
|
497
|
+
%w[tasks_stopped tasks_not_stopped tasks_ended].to_h do |key|
|
|
498
|
+
[key.to_sym, Array(json[key]).map { |task| SessionManager::RunningTasks::RunningTask.from_wire(task) }]
|
|
499
|
+
end
|
|
487
500
|
end
|
|
488
501
|
|
|
489
502
|
# @return [Hash] {id:, unarchived:}
|
|
@@ -498,26 +511,31 @@ module Samagotchi
|
|
|
498
511
|
# A chi web before chain_delete answers without unarchived,
|
|
499
512
|
# moved_back, deleted and left_running (no revive, no undo, no D7
|
|
500
513
|
# there): each reads as [].
|
|
501
|
-
|
|
502
|
-
|
|
514
|
+
# @raise [RemoteTooOld] +stop_tasks+ and a chi web without STOP_TASKS_FEATURE
|
|
515
|
+
def delete(id, force:, stop_tasks: false)
|
|
516
|
+
require_feature!(STOP_TASKS_FEATURE) if stop_tasks
|
|
517
|
+
reply = request(:delete, session_path(id), query: { force: (true if force), stop_tasks: (true if stop_tasks) })
|
|
503
518
|
raise_refusal(reply) unless reply.status == 200
|
|
504
519
|
|
|
505
520
|
{ id: reply.json["session_id"] || id, stopped: reply.json["stopped"] == true,
|
|
506
521
|
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"])
|
|
522
|
+
deleted: ids_of(reply.json["deleted"]), left_running: ids_of(reply.json["left_running"]),
|
|
523
|
+
**task_fields(reply.json) }
|
|
508
524
|
end
|
|
509
525
|
|
|
510
526
|
# DELETE …/chain there (SessionManager.delete_chain's result).
|
|
511
527
|
# @raise [RemoteTooOld] a chi web without it (an older one would answer 404)
|
|
512
|
-
def delete_chain(id, force:)
|
|
528
|
+
def delete_chain(id, force:, stop_tasks: false)
|
|
513
529
|
require_feature!(CHAIN_DELETE_FEATURE)
|
|
514
|
-
|
|
530
|
+
require_feature!(STOP_TASKS_FEATURE) if stop_tasks
|
|
531
|
+
reply = request(:delete, "#{session_path(id)}/chain", query: { force: (true if force), stop_tasks: (true if stop_tasks) })
|
|
515
532
|
raise_refusal(reply) unless reply.status == 200
|
|
516
533
|
|
|
517
534
|
json = reply.json
|
|
518
535
|
{ id: json["session_id"] || id, deleted: ids_of(json["deleted"]), left: ids_of(json["left"]),
|
|
519
536
|
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"]
|
|
537
|
+
stopped: ids_of(json["stopped"]), cut_off: json["cut_off"] == true, refusal: json["refusal"],
|
|
538
|
+
**task_fields(json) }
|
|
521
539
|
end
|
|
522
540
|
|
|
523
541
|
private def ids_of(value) = Array(value).select { |id| Session.valid_id?(id) }
|
|
@@ -786,6 +804,7 @@ module Samagotchi
|
|
|
786
804
|
when "worker_running", "still_stopping", "scheduled", "continuing", "delegate_unmovable", "chain_scheduled"
|
|
787
805
|
raise SessionManager::DeleteRefused.new(sid, reason || error.to_sym, message: message, ids: ids_of(json["ids"]),
|
|
788
806
|
schedule_id: json["schedule_id"])
|
|
807
|
+
when "tasks_running" then raise tasks_refusal(json, sid)
|
|
789
808
|
when "busy", "scratch"
|
|
790
809
|
raise SessionManager::ArchiveRefused.new(sid, reason, busy_id: json["busy_id"]) if reason
|
|
791
810
|
when "continued", "open_children", "folder_gone"
|
|
@@ -814,6 +833,20 @@ module Samagotchi
|
|
|
814
833
|
"#{": #{message}" if message}"
|
|
815
834
|
end
|
|
816
835
|
|
|
836
|
+
# A 409 tasks_running as the refusal Local's callee raises, by its op;
|
|
837
|
+
# the words are made here from the listing (one place for them).
|
|
838
|
+
def tasks_refusal(json, sid)
|
|
839
|
+
tasks = SessionManager::RunningTasks::Listing.from_wire(json["tasks"])
|
|
840
|
+
# Whose worker was stopped (an older chi web sends only the Boolean).
|
|
841
|
+
stopped = Session.valid_id?(json["stopped_worker"]) ? json["stopped_worker"] : json["worker_stopped"] == true
|
|
842
|
+
if json["op"] == "archive"
|
|
843
|
+
SessionManager::ArchiveRefused.new(sid, :tasks_running, tasks: tasks, worker_stopped: stopped)
|
|
844
|
+
else
|
|
845
|
+
SessionManager::DeleteRefused.new(sid, :tasks_running, tasks: tasks, worker_stopped: stopped,
|
|
846
|
+
links: ids_of(json["links"]))
|
|
847
|
+
end
|
|
848
|
+
end
|
|
849
|
+
|
|
817
850
|
def create_body(spec)
|
|
818
851
|
body = { model: spec.model_name, client_id: spec.client_id, history: false }
|
|
819
852
|
if spec.preview || spec.idle
|
|
@@ -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}")
|
|
@@ -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,27 @@ module Samagotchi
|
|
|
130
147
|
# @raise [SessionManager::DeleteRefused] :continuing, :scheduled,
|
|
131
148
|
# :delegate_unmovable, :worker_running, :still_stopping
|
|
132
149
|
# @raise [SessionManager::OwnedByTUI]
|
|
133
|
-
|
|
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)
|
|
134
155
|
ContinueIntent.hold(id, state_dir: state_dir) do |held|
|
|
135
156
|
raise SessionManager::DeleteRefused.new(id, :continuing) if refuse_continuing && !held
|
|
136
157
|
|
|
137
158
|
links = Links.new(state_dir: state_dir)
|
|
138
159
|
unless chained?(id, links)
|
|
139
|
-
|
|
160
|
+
raise SessionManager::DeleteRefused.new(id, :plan_changed) unless expect.nil?
|
|
161
|
+
|
|
162
|
+
return plain(SessionManager.delete_resolved(id, state_dir: state_dir, stop: stop, wait: wait,
|
|
163
|
+
running_tasks: running_tasks))
|
|
140
164
|
end
|
|
141
165
|
|
|
142
166
|
plan = link_plan(id, state_dir: state_dir, links: links, undo: revive_previous)
|
|
143
|
-
|
|
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
|
|
144
171
|
|
|
145
172
|
# Taken before the link's file goes: after it, a continue of the
|
|
146
173
|
# previous link would no longer be refused. The plan is made again
|
|
@@ -149,7 +176,8 @@ module Samagotchi
|
|
|
149
176
|
raise SessionManager::DeleteRefused.new(plan.previous, :continuing) unless previous_held
|
|
150
177
|
|
|
151
178
|
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
|
|
179
|
+
return delete_link(again, state_dir: state_dir, stop: stop, wait: wait, expect: expect,
|
|
180
|
+
running_tasks: running_tasks)
|
|
153
181
|
end
|
|
154
182
|
end
|
|
155
183
|
end
|
|
@@ -165,12 +193,17 @@ module Samagotchi
|
|
|
165
193
|
# The refusals a link's delete makes before anything changes (the
|
|
166
194
|
# sheet's too): a schedule's target, anchor or head, on the link or a
|
|
167
195
|
# delegate that would go; a moved delegate that can't go back; a
|
|
168
|
-
# 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.
|
|
169
198
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
170
|
-
def check_link!(plan, state_dir)
|
|
199
|
+
def check_link!(plan, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
171
200
|
SessionManager.refuse_scheduled!(plan.id, state_dir)
|
|
172
201
|
refuse_unmovable!(plan.id, plan.moved, state_dir)
|
|
173
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
|
|
174
207
|
end
|
|
175
208
|
|
|
176
209
|
# Every refusal first (check_link!), then the link's worker stopped,
|
|
@@ -180,27 +213,52 @@ module Samagotchi
|
|
|
180
213
|
# files. So a refusal (a schedule on a delegate, a one-turn run, a
|
|
181
214
|
# worker that took the link or a delegate after the checks) leaves
|
|
182
215
|
# every session in place.
|
|
183
|
-
def delete_link(plan, state_dir:, stop:, wait:
|
|
216
|
+
def delete_link(plan, state_dir:, stop:, wait:, expect: nil,
|
|
217
|
+
running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
218
|
+
raise SessionManager::DeleteRefused.new(plan.id, :plan_changed) if !expect.nil? && expect != digest(plan)
|
|
219
|
+
|
|
184
220
|
id = plan.id
|
|
185
|
-
check_link!(plan, state_dir)
|
|
221
|
+
check_link!(plan, state_dir, running_tasks: running_tasks)
|
|
186
222
|
stopped = SessionManager.release_for_delete!(id, state_dir: state_dir, stop: stop, wait: wait)
|
|
187
223
|
path = Session.session_file(id, state_dir: state_dir)
|
|
188
224
|
dir = Session.session_dir(id, state_dir: state_dir)
|
|
189
225
|
doomed = [*plan.delegates.delete, id]
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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)
|
|
195
242
|
end
|
|
196
|
-
else
|
|
197
|
-
SessionManager.remove_files(id, path, dir, state_dir: state_dir, purge_runs: false, locked: true)
|
|
198
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)
|
|
199
249
|
end
|
|
200
250
|
SessionManager.purge_removal_stores(doomed, state_dir: state_dir)
|
|
201
251
|
unarchived = plan.revive.select { |sid| ArchiveStore.unarchive(sid, state_dir: state_dir) }
|
|
202
252
|
{ id: id, removed: removed, stopped: stopped, unarchived: unarchived, moved_back: plan.moved,
|
|
203
|
-
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
|
|
204
262
|
end
|
|
205
263
|
|
|
206
264
|
# The refusals of every id a batch would delete, before the first
|
|
@@ -248,10 +306,10 @@ module Samagotchi
|
|
|
248
306
|
# +ids+' files, in order, each locked by the caller (#locked); the
|
|
249
307
|
# session logs are the caller's to purge, once.
|
|
250
308
|
# @return [Array<String>] the paths removed
|
|
251
|
-
def remove_locked(ids, state_dir)
|
|
309
|
+
def remove_locked(ids, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
252
310
|
ids.flat_map do |sid|
|
|
253
311
|
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)
|
|
312
|
+
state_dir: state_dir, purge_runs: false, locked: true, running_tasks: running_tasks)
|
|
255
313
|
end
|
|
256
314
|
end
|
|
257
315
|
|
|
@@ -314,24 +372,32 @@ module Samagotchi
|
|
|
314
372
|
# earlier ones; refusal: the message of a part-way refusal, or nil)
|
|
315
373
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
316
374
|
# nothing was deleted
|
|
317
|
-
|
|
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)
|
|
318
380
|
links = Links.new(state_dir: state_dir)
|
|
319
381
|
plan = chain_plan(id, state_dir: state_dir, links: links)
|
|
320
|
-
|
|
382
|
+
raise SessionManager::DeleteRefused.new(plan.id, :plan_changed) if !expect.nil? && expect != digest(plan)
|
|
383
|
+
|
|
384
|
+
check_chain!(plan, state_dir, stop: stop, running_tasks: running_tasks)
|
|
321
385
|
hold_all(plan.links, state_dir) do
|
|
322
386
|
stopped = plan.links.select do |link|
|
|
323
387
|
SessionManager.release_for_delete!(link, state_dir: state_dir, stop: stop, wait: wait)
|
|
324
388
|
end
|
|
325
|
-
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)
|
|
326
391
|
end
|
|
327
392
|
end
|
|
328
393
|
|
|
329
394
|
# The refusals a chain's delete makes before anything goes (the
|
|
330
395
|
# sheet's too): a schedule on the chain, a link in a one-turn run,
|
|
331
396
|
# without +stop+ a link with a worker, then every delegate that would
|
|
332
|
-
# 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.
|
|
333
399
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
334
|
-
def check_chain!(plan, state_dir, stop:)
|
|
400
|
+
def check_chain!(plan, state_dir, stop:, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
335
401
|
schedule = Schedules.new(state_dir: state_dir).for_chain(plan.id).first
|
|
336
402
|
raise SessionManager::DeleteRefused.new(plan.id, :chain_scheduled, schedule_id: schedule.id) if schedule
|
|
337
403
|
|
|
@@ -341,6 +407,10 @@ module Samagotchi
|
|
|
341
407
|
raise SessionManager::DeleteRefused.new(link, :worker_running) if !stop && (presence.owned? || presence.starting?)
|
|
342
408
|
end
|
|
343
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
|
|
344
414
|
end
|
|
345
415
|
|
|
346
416
|
# The block inside a shared continue hold of every id.
|
|
@@ -357,7 +427,7 @@ module Samagotchi
|
|
|
357
427
|
|
|
358
428
|
# Each link with its delegates under their locks, newest first; a
|
|
359
429
|
# refused one keeps it and every earlier link whole.
|
|
360
|
-
def delete_links(plan, links, state_dir)
|
|
430
|
+
def delete_links(plan, links, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
361
431
|
done = { id: plan.id, deleted: [], left: [], left_running: [], unarchived: [], refusal: nil }
|
|
362
432
|
newest_first = plan.links.reverse
|
|
363
433
|
record = nil
|
|
@@ -366,7 +436,10 @@ module Samagotchi
|
|
|
366
436
|
batch = [*delegates.delete, link]
|
|
367
437
|
deleted_record = ContinuedFrom.read(link, state_dir: state_dir)
|
|
368
438
|
begin
|
|
369
|
-
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
|
|
370
443
|
rescue SessionManager::DeleteRefused, SessionManager::OwnedByTUI, Session::NotFound => e
|
|
371
444
|
raise if done[:deleted].empty?
|
|
372
445
|
|
|
@@ -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
|
|
@@ -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
|
|
@@ -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")
|