samagotchi 0.116.0 → 0.118.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 -2
- data/docs/cli.md +28 -1
- data/docs/configuration.md +6 -0
- data/docs/internals/background-tasks.md +1 -1
- data/docs/sessions.md +3 -3
- data/lib/samagotchi/bridge.rb +88 -14
- data/lib/samagotchi/bridge_client.rb +13 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/config.rb +19 -3
- data/lib/samagotchi/hold_stop.rb +89 -0
- data/lib/samagotchi/prompt_warmup.rb +8 -3
- data/lib/samagotchi/session_access/web_bridge_client.rb +9 -4
- data/lib/samagotchi/session_manager.rb +3 -2
- data/lib/samagotchi/task_watch.rb +23 -5
- data/lib/samagotchi/terminal_ui/attached_loop.rb +130 -8
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +2 -0
- data/lib/samagotchi/web/public/app.js +17 -1
- data/lib/samagotchi/web/public/apple-touch-icon.png +0 -0
- data/lib/samagotchi/web/public/composer_size.js +14 -7
- data/lib/samagotchi/web/public/data.js +14 -0
- data/lib/samagotchi/web/public/favicon.svg +1 -1
- data/lib/samagotchi/web/public/icons/chi-180.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-192.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-512.png +0 -0
- data/lib/samagotchi/web/public/index.html +28 -2
- data/lib/samagotchi/web/public/manifest.webmanifest +1 -1
- data/lib/samagotchi/web/public/session_actions.js +39 -6
- data/lib/samagotchi/web/public/stop_sheet.js +126 -0
- data/lib/samagotchi/web/public/tokens.css +2 -2
- data/lib/samagotchi/web/session_access_routes.rb +9 -6
- data/lib/samagotchi/web/session_control.rb +40 -3
- data/lib/samagotchi/worker.rb +57 -5
- data/lib/samagotchi/worker_idle_exit.rb +111 -9
- metadata +3 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// The Stop button's sheet (session_actions.js asks the worker first): when
|
|
2
|
+
// the session's worker still has things the user can stop — reminders, a
|
|
3
|
+
// background task that runs, a delegate with a live worker — Stop asks what
|
|
4
|
+
// to do with them before TERMed-ing the worker. Nothing to stop, or no worker
|
|
5
|
+
// to ask, is today's one tap.
|
|
6
|
+
//
|
|
7
|
+
// "Stop all" runs the worker's stop-all step (POST …/holds/stop, the same
|
|
8
|
+
// HoldStop#stop_all the terminal's /exit ask uses) and then today's Stop;
|
|
9
|
+
// "Stop session only" is today's Stop exactly, so what survives keeps running
|
|
10
|
+
// (docs/sessions.md, "Stopping a worker"). Reminders are listed with a note:
|
|
11
|
+
// they live only in the worker, so they end with the session either way.
|
|
12
|
+
//
|
|
13
|
+
// A phone gets it as a bottom sheet, a wider window as a popover at the Stop
|
|
14
|
+
// button (chip_popover.js). Pure model and template plus createStopSheet, so
|
|
15
|
+
// the specs run them under node --test.
|
|
16
|
+
|
|
17
|
+
import { html, nothing, render } from "./vendor/lit-html.js";
|
|
18
|
+
import { createChipPopover } from "./chip_popover.js";
|
|
19
|
+
|
|
20
|
+
// At most this many of a kind named in the sheet; the rest are one "+N more".
|
|
21
|
+
export const MAX_NAMED = 3;
|
|
22
|
+
|
|
23
|
+
// The Stop button the sheet opens at.
|
|
24
|
+
export const STOP_ANCHOR = "#infoStopBtn";
|
|
25
|
+
|
|
26
|
+
const KIND_WORDS = {
|
|
27
|
+
reminders: ["reminder", "reminders"],
|
|
28
|
+
background_tasks: ["background task", "background tasks"],
|
|
29
|
+
delegates: ["delegate", "delegates"],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// One entry of a hold as the sheet writes it: the command, the reminder's
|
|
33
|
+
// name with its schedule, a delegate's short id and its title. A long command
|
|
34
|
+
// is cut short: the sheet must fit a phone without scrolling.
|
|
35
|
+
function itemWords(item, kind) {
|
|
36
|
+
const label = String(item?.label ?? item?.id ?? "").trim();
|
|
37
|
+
const detail = String(item?.detail ?? "").trim();
|
|
38
|
+
const cut = label.length > 42 ? `${label.slice(0, 42)}…` : label;
|
|
39
|
+
if (kind === "delegates") {
|
|
40
|
+
const id = String(item?.id ?? "").slice(0, 8);
|
|
41
|
+
return `${id} "${cut || "untitled"}"${detail ? ` ${detail}` : ""}`;
|
|
42
|
+
}
|
|
43
|
+
const name = cut || String(item?.id ?? "").slice(0, 8);
|
|
44
|
+
return detail ? `${name} (${detail})` : name;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// What the sheet says from the worker's holds (GET …/holds): null when there
|
|
48
|
+
// is nothing to ask about (no sheet: Stop stays one tap), else its lines, one
|
|
49
|
+
// per kind, and the total.
|
|
50
|
+
// @return {lines: [{kind, count, word, items, more, note}], total} | null
|
|
51
|
+
export function stopSheetModel(holds) {
|
|
52
|
+
const lines = [];
|
|
53
|
+
for (const hold of Array.isArray(holds) ? holds : []) {
|
|
54
|
+
const kind = String(hold?.kind ?? "");
|
|
55
|
+
const items = Array.isArray(hold?.items) ? hold.items : [];
|
|
56
|
+
if (!items.length) continue;
|
|
57
|
+
|
|
58
|
+
const [singular, plural] = KIND_WORDS[kind] ?? [kind || "hold", `${kind || "holds"}s`];
|
|
59
|
+
lines.push({
|
|
60
|
+
kind,
|
|
61
|
+
count: items.length,
|
|
62
|
+
word: items.length === 1 ? singular : plural,
|
|
63
|
+
items: items.slice(0, MAX_NAMED).map((item) => itemWords(item, kind)),
|
|
64
|
+
more: Math.max(0, items.length - MAX_NAMED),
|
|
65
|
+
note: kind === "reminders" ? "they end with the session" : "",
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (!lines.length) return null;
|
|
69
|
+
|
|
70
|
+
return { lines, total: lines.reduce((sum, line) => sum + line.count, 0) };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function lineTemplate(line) {
|
|
74
|
+
return html`<li class="ss-line" data-kind=${line.kind}>
|
|
75
|
+
<b>${line.count} ${line.word}</b>
|
|
76
|
+
${line.note ? html` <span class="ss-note">(${line.note})</span>` : nothing}
|
|
77
|
+
${line.items.length ? html` <span class="ss-items">${line.items.join(" · ")}${line.more ? html` <span class="ss-more">+${line.more} more</span>` : nothing}</span>` : nothing}
|
|
78
|
+
</li>`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// The sheet's content (a lit-html template).
|
|
82
|
+
export function stopSheetTemplate(model) {
|
|
83
|
+
const things = model.total === 1 ? "1 thing" : `${model.total} things`;
|
|
84
|
+
return html`<div class="ctx-pop-head"><strong>This session still has</strong
|
|
85
|
+
><span class="ctx-pop-kind">${things}</span
|
|
86
|
+
><button type="button" class="ctx-pop-close" data-act="close" aria-label="Close">×</button></div
|
|
87
|
+
><ul class="ss-lines">${model.lines.map(lineTemplate)}</ul
|
|
88
|
+
><div class="ss-acts"><button type="button" class="ss-stop-all" data-act="all">Stop all</button
|
|
89
|
+
><button type="button" class="ss-stop-only" data-act="session">Stop session only</button
|
|
90
|
+
><button type="button" class="ss-cancel" data-act="cancel">Cancel</button></div
|
|
91
|
+
><div class="ss-foot">Stopping the session ends its worker; a background task or a delegate runs on its own, so it stays.</div>`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// The sheet at the Stop button. +stopAll(id)+ stops what the worker can and
|
|
95
|
+
// then the session; +stopSession(id)+ is today's Stop; +open(id, holds)+ shows
|
|
96
|
+
// it only when there is something (false otherwise, so the caller can fall
|
|
97
|
+
// back to the plain confirm). @return {open, close, isOpen, pop}
|
|
98
|
+
export function createStopSheet({ doc = globalThis.document, win = globalThis.window,
|
|
99
|
+
anchor = STOP_ANCHOR, el = doc.createElement("span"),
|
|
100
|
+
stopAll = () => {}, stopSession = () => {} }) {
|
|
101
|
+
let model = null;
|
|
102
|
+
let forId = null;
|
|
103
|
+
|
|
104
|
+
const shell = createChipPopover({
|
|
105
|
+
id: "stopSheet", className: "ctx-popover stop-sheet", width: 400, el, anchor, openers: anchor,
|
|
106
|
+
summary: () => model, sheet: true, label: "Stop the session?", doc, win,
|
|
107
|
+
show: (data) => render(stopSheetTemplate(data), shell.pop),
|
|
108
|
+
onClose: () => { model = null; forId = null; },
|
|
109
|
+
onAct: (act) => {
|
|
110
|
+
const id = forId;
|
|
111
|
+
if (act === "all") { shell.close(); stopAll(id); }
|
|
112
|
+
else if (act === "session") { shell.close(); stopSession(id); }
|
|
113
|
+
else if (act === "cancel") shell.close();
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
function open(id, holds) {
|
|
118
|
+
model = stopSheetModel(holds);
|
|
119
|
+
if (!model) return false;
|
|
120
|
+
forId = id;
|
|
121
|
+
shell.open(doc.querySelector(anchor) ?? el);
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return { open, close: () => shell.close(), isOpen: shell.isOpen, pop: shell.pop };
|
|
126
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/* The web UI's colour tokens, shared by the owner's page (index.html) and the participant page (participant.html): the dark :root (the default) and the light set under prefers-color-scheme: light. Everything else reads them through var(). */
|
|
2
|
-
:root{color-scheme:dark;--bg:#0b0d11;--bg-panel:#151821;--bg-inset:#0f1115;--bg-raised:#22262f;--bg-hover:#1b1f27;--border:#1f232b;--border-strong:#2d333f;--text:#e6e8eb;--text-bright:#f0f3f6;--text-2:#c9d1d9;--text-3:#9aa4b2;--text-muted:#8b949e;--text-faint:#6e7681;--accent:#
|
|
2
|
+
:root{color-scheme:dark;--bg:#0b0d11;--bg-panel:#151821;--bg-inset:#0f1115;--bg-raised:#22262f;--bg-hover:#1b1f27;--border:#1f232b;--border-strong:#2d333f;--text:#e6e8eb;--text-bright:#f0f3f6;--text-2:#c9d1d9;--text-3:#9aa4b2;--text-muted:#8b949e;--text-faint:#6e7681;--accent:#5aa9ff;--accent-strong:#0071e3;--accent-bg:#0f2340;--accent-ring:rgba(90,169,255,.18);--primary:#0071e3;--ok:#3fb950;--ok-text:#7ee787;--ok-bg:#1a2e1a;--warn:#d29922;--warn-text:#e3b341;--warn-bright:#f2cc60;--warn-bg:#2b2108;--warn-border:#6b5416;--danger:#f85149;--danger-bg:#da3633;--danger-text:#ff7b72;--danger-bg-dim:#3d1f1f;--think:#8957e5;--think-text:#d2a8ff;--accent-border:#1a3458;--pick-border:#79c0ff;--accent-tint:color-mix(in srgb,var(--accent) 7%,transparent);--narr:color-mix(in srgb,var(--accent) 38%,var(--text-muted));
|
|
3
3
|
--glass-bg:color-mix(in srgb,var(--bg-panel) 70%,transparent);--glass-border:rgba(255,255,255,.08);--glass-highlight:rgba(255,255,255,.05);--glass-shadow:0 8px 30px rgba(0,0,0,.35);
|
|
4
4
|
--diff-add-bg:rgba(63,185,80,.14);--diff-add-text:#aff5b4;--diff-del-bg:rgba(248,81,73,.14);--diff-del-text:#ffc1bc;
|
|
5
5
|
--code-text:#c9d1d9;--code-keyword:#ff7b72;--code-string:#a5d6ff;--code-comment:#8b949e;--code-constant:#79c0ff;--code-func:#d2a8ff;--code-type:#ffa657;
|
|
6
6
|
--card-hover-border:rgba(255,255,255,.14);--on-solid:#fff;--on-warn:#fff;--text-hover:#fff;--badge-bg:rgba(255,255,255,.08);--badge-ring:rgba(255,255,255,.18);--shadow-strong:rgba(0,0,0,.5);--backdrop:rgba(0,0,0,.85);--toast-shadow:0 6px 24px rgba(0,0,0,.25);--ws-stale-opacity:.4;--ws-rule:var(--border-strong);--ws-muted:var(--text-3);--ws-faint:var(--text-muted);--ws-act-border:#444c56;--author-1:#79c0ff;--author-2:#d2a8ff;--author-3:#7ee787;--author-4:#ffa657;--author-5:#ff7b72;--author-6:#f2cc60;--author-owner:var(--text-3);--time-gap:var(--text-faint)}
|
|
7
7
|
/* Light palette: follows the system setting. :not([data-theme="dark"]) leaves room for a later in-page toggle (data-theme on <html>); there is none yet. Each palette sets color-scheme, so native radios, inputs and scrollbars follow it. */
|
|
8
|
-
@media (prefers-color-scheme:light){:root:not([data-theme="dark"]){color-scheme:light;--bg:#f5f6f8;--bg-panel:#ffffff;--bg-inset:#f1f3f6;--bg-raised:#e8ebf0;--bg-hover:#eceff4;--border:#dde1e7;--border-strong:#c3c9d2;--text:#1f2328;--text-bright:#0d1117;--text-2:#2f363d;--text-3:#475160;--text-muted:#57606a;--text-faint:#636c76;--accent:#
|
|
8
|
+
@media (prefers-color-scheme:light){:root:not([data-theme="dark"]){color-scheme:light;--bg:#f5f6f8;--bg-panel:#ffffff;--bg-inset:#f1f3f6;--bg-raised:#e8ebf0;--bg-hover:#eceff4;--border:#dde1e7;--border-strong:#c3c9d2;--text:#1f2328;--text-bright:#0d1117;--text-2:#2f363d;--text-3:#475160;--text-muted:#57606a;--text-faint:#636c76;--accent:#0066cc;--accent-strong:#0058b0;--accent-bg:#e3f0ff;--accent-ring:rgba(0,102,204,.2);--primary:#0071e3;--ok:#1a7f37;--ok-text:#116329;--ok-bg:#dafbe1;--warn:#9a6700;--warn-text:#7d4e00;--warn-bright:#875c00;--warn-bg:#fff6cc;--warn-border:#d4a72c;--danger:#cf222e;--danger-bg:#cf222e;--danger-text:#a40e26;--danger-bg-dim:#ffebe9;--think:#8250df;--think-text:#6639ba;--accent-border:#b3d4f7;--pick-border:#0969da;
|
|
9
9
|
--glass-border:rgba(31,35,40,.10);--glass-highlight:rgba(255,255,255,.7);--glass-shadow:0 8px 30px rgba(31,35,40,.10);
|
|
10
10
|
--diff-add-bg:rgba(26,127,55,.12);--diff-add-text:#116329;--diff-del-bg:rgba(207,34,46,.10);--diff-del-text:#82071e;
|
|
11
11
|
--code-text:#1f2328;--code-keyword:#cf222e;--code-string:#0a3069;--code-comment:#57606a;--code-constant:#0550ae;--code-func:#8250df;--code-type:#953800;
|
|
@@ -304,22 +304,25 @@ module Samagotchi
|
|
|
304
304
|
{ epoch: sidecar.epoch, started_at: sidecar.started_at, version: sidecar.version, features: sidecar.features }
|
|
305
305
|
end
|
|
306
306
|
|
|
307
|
-
# POST /api/sessions/:id/exit {client_id, delete}: a remote attached
|
|
307
|
+
# POST /api/sessions/:id/exit {client_id, delete, stop}: a remote attached
|
|
308
308
|
# TUI's /exit (and the worker's half of /exit --delete and /archive),
|
|
309
309
|
# relayed to the live worker's exit (BridgeClient#request_exit; never
|
|
310
310
|
# wakes one). client_id is passed on only when it is an attached TUI's
|
|
311
311
|
# (tui:…): any other is dropped, and then every stream counts as a UI,
|
|
312
|
-
# which only keeps the worker up.
|
|
313
|
-
#
|
|
314
|
-
#
|
|
315
|
-
# worker_timeout.
|
|
312
|
+
# which only keeps the worker up. stop: true means the ask's "stop them
|
|
313
|
+
# and exit". 200 the Bridge's {status: exiting, session_id, discard,
|
|
314
|
+
# stopped}; 409 held with its {status, reason, tasks, hint, holds};
|
|
315
|
+
# 503 not_live, 501 not_supported (an older worker), 504 worker_timeout.
|
|
316
316
|
def handle_exit(req, id)
|
|
317
317
|
body = body_hash(req) or return refusal(400, "invalid_json", "invalid JSON body")
|
|
318
318
|
@session_class.load(id, state_dir: default_state_dir)
|
|
319
319
|
client_id = body["client_id"] if body["client_id"].is_a?(String) && body["client_id"].start_with?(ClientId::TUI_PREFIX)
|
|
320
320
|
delete = body["delete"] == true
|
|
321
|
+
options = { delete: delete }
|
|
322
|
+
options[:stop] = true if body["stop"] == "all"
|
|
323
|
+
options[:ask] = false if body["ask"] == false
|
|
321
324
|
|
|
322
|
-
request = ->(client) { client.request_exit(client_id: client_id,
|
|
325
|
+
request = ->(client) { client.request_exit(client_id: client_id, **options) }
|
|
323
326
|
with_message(relay(id, bridge_client(id), request, what: "the worker was not asked to exit", cant: "exit from here") do |reply|
|
|
324
327
|
case reply.status
|
|
325
328
|
when 200 then json_response(200, reply.json || { status: "exiting", session_id: id })
|
|
@@ -13,9 +13,10 @@ module Samagotchi
|
|
|
13
13
|
module Web
|
|
14
14
|
# The owner's routes that act on a session's worker: answer or dismiss
|
|
15
15
|
# its question, run a command, send a turn (and its images), cancel, stop
|
|
16
|
-
# a task, dequeue a queued turn,
|
|
17
|
-
#
|
|
18
|
-
#
|
|
16
|
+
# a task, dequeue a queued turn, what it could stop and stop it
|
|
17
|
+
# (handle_session_holds, handle_stop_holds — the page's Stop asks about
|
|
18
|
+
# these first), stop, restart, archive, unarchive and delete
|
|
19
|
+
# (handle_question_answer … handle_delete), with the refusals they answer (owned_by_repl, owned_by_tui, archive_refused). Included in App:
|
|
19
20
|
# its ROUTES name the handlers, and its helpers (json_response,
|
|
20
21
|
# error_response, parse_json, request_body, relay, not_live,
|
|
21
22
|
# bridge_error, reply_detail, worker_timeout, bridge_client,
|
|
@@ -359,6 +360,42 @@ module Samagotchi
|
|
|
359
360
|
end
|
|
360
361
|
end
|
|
361
362
|
|
|
363
|
+
# GET /api/sessions/:id/holds: what this session's worker could stop
|
|
364
|
+
# right now (its Bridge's GET holds: reminders, the background tasks a
|
|
365
|
+
# finish wake watches that still run, the delegate children with a live
|
|
366
|
+
# worker), so the page's Stop can ask about them before TERMed-ing the
|
|
367
|
+
# worker. 200 {session_id, holds: []} — always a list: with no live
|
|
368
|
+
# worker, a worker from before the route, or one that didn't answer
|
|
369
|
+
# there is nothing to ask about, and Stop stays one tap as it was.
|
|
370
|
+
def handle_session_holds(_req, id)
|
|
371
|
+
@session_class.load(id, state_dir: default_state_dir)
|
|
372
|
+
client = bridge_client(id)
|
|
373
|
+
return json_response(200, { session_id: id, holds: [] }) unless client
|
|
374
|
+
|
|
375
|
+
status, json = client.get("holds")
|
|
376
|
+
holds = status == 200 && json.is_a?(Hash) ? json["holds"] : nil
|
|
377
|
+
json_response(200, { session_id: id, holds: holds.is_a?(Array) ? holds : [] })
|
|
378
|
+
rescue SystemCallError, IOError, SocketError
|
|
379
|
+
json_response(200, { session_id: id, holds: [] })
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# POST /api/sessions/:id/holds/stop: the sheet's "Stop all" — the worker
|
|
383
|
+
# stops everything the user can stop (Bridge#handle_holds_stop) before
|
|
384
|
+
# today's Stop TERMs it. 200 {status: "stopped", session_id, stopped:
|
|
385
|
+
# {reminders:, tasks:, delegates:}}; 503 no live worker, 501 a worker
|
|
386
|
+
# from before the route, 504/502 as every relay (the page says which
|
|
387
|
+
# thing it could not stop and stops the session anyway).
|
|
388
|
+
def handle_stop_holds(_req, id)
|
|
389
|
+
@session_class.load(id, state_dir: default_state_dir)
|
|
390
|
+
request = ->(client) { client.stop_holds }
|
|
391
|
+
relay(id, bridge_client(id), request, what: "nothing was stopped", cant: "stop what the session runs") do |reply|
|
|
392
|
+
next unless reply.status == 200
|
|
393
|
+
|
|
394
|
+
stopped = reply.json&.dig("stopped")
|
|
395
|
+
json_response(200, { status: "stopped", session_id: id, stopped: stopped || {} })
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
|
|
362
399
|
# POST /api/sessions/:id/stop {wait?}: 200 {status: "stopped",
|
|
363
400
|
# session_id, released}: released, whether the worker let go of the
|
|
364
401
|
# session within the wait (STOP_WAIT_SECONDS, or the body's seconds up
|
data/lib/samagotchi/worker.rb
CHANGED
|
@@ -13,6 +13,8 @@ require_relative "turn_note"
|
|
|
13
13
|
require_relative "steer"
|
|
14
14
|
require_relative "turn_origin"
|
|
15
15
|
require_relative "worker_idle_exit"
|
|
16
|
+
require_relative "hold_stop"
|
|
17
|
+
require_relative "children_status"
|
|
16
18
|
require_relative "worker_wakes"
|
|
17
19
|
require_relative "worker_inbound"
|
|
18
20
|
require_relative "worker_signals"
|
|
@@ -210,6 +212,11 @@ module Samagotchi
|
|
|
210
212
|
end
|
|
211
213
|
end
|
|
212
214
|
|
|
215
|
+
# The step that stops what the user can stop (the reminders, the watched
|
|
216
|
+
# tasks and the delegate children the exit ask lists), and what the last
|
|
217
|
+
# stop-all exit stopped. /exit's 2 uses it; the web's Stop will too.
|
|
218
|
+
attr_reader :hold_stop, :stopped_holds
|
|
219
|
+
|
|
213
220
|
private
|
|
214
221
|
|
|
215
222
|
# @return [Symbol] what #run returns
|
|
@@ -330,7 +337,13 @@ module Samagotchi
|
|
|
330
337
|
on_command: method(:on_command),
|
|
331
338
|
on_exit_request: method(:exit_request),
|
|
332
339
|
# Decided again as it leaves (a note may still come in).
|
|
333
|
-
exit_discards: method(:empty_session?)
|
|
340
|
+
exit_discards: method(:empty_session?),
|
|
341
|
+
# What a stop-all /exit stopped, for its 200.
|
|
342
|
+
exit_stopped: method(:stopped_holds),
|
|
343
|
+
# chi web's Stop asks about these before TERMed-ing
|
|
344
|
+
# the worker (its own holds routes).
|
|
345
|
+
on_holds: -> { @idle_exit&.stoppable_holds },
|
|
346
|
+
on_holds_stop: -> { @hold_stop&.stop_all })
|
|
334
347
|
# What failed to load and what plugins showed as they loaded, now that
|
|
335
348
|
# a UI can be there (a late one gets them in the snapshot).
|
|
336
349
|
@engine.announce_load_events!
|
|
@@ -346,8 +359,25 @@ module Samagotchi
|
|
|
346
359
|
# finished since the pass looked holds too, until the next pass
|
|
347
360
|
# wakes for it (#run_task_wake).
|
|
348
361
|
watched_tasks: -> { TaskWatch.mode == "wake" ? @task_wakes.watch.holding : [] },
|
|
349
|
-
watched_running: -> { TaskWatch.mode == "wake" ? @task_wakes.watch.running : [] }
|
|
362
|
+
watched_running: -> { TaskWatch.mode == "wake" ? @task_wakes.watch.running : [] },
|
|
363
|
+
# Never a hold (a child is its own worker), but the exit ask lists
|
|
364
|
+
# them so "stop them and exit" stops them too.
|
|
365
|
+
delegates: method(:live_delegates)
|
|
350
366
|
)
|
|
367
|
+
@hold_stop = HoldStop.new(engine: @engine, task_watch: @task_wakes.watch, delegates: method(:live_delegates),
|
|
368
|
+
state_dir: @state_dir)
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# The delegate children of this session that are running a turn or have
|
|
372
|
+
# a live worker. ChildrenStatus.of scans the session files; this runs on
|
|
373
|
+
# an exit request, not every pass.
|
|
374
|
+
# @return [Array<ChildrenStatus::Child>]
|
|
375
|
+
def live_delegates
|
|
376
|
+
ChildrenStatus.of(@session_id, state_dir: @state_dir)
|
|
377
|
+
.select { |child| child.delegate && (child.state == "running" || child.live) }
|
|
378
|
+
rescue StandardError => e
|
|
379
|
+
Log.warn(:worker, "delegate_look_failed", error: e.class.name, msg: e.message)
|
|
380
|
+
[]
|
|
351
381
|
end
|
|
352
382
|
|
|
353
383
|
# What runs beside the loop until it leaves.
|
|
@@ -1418,10 +1448,18 @@ module Samagotchi
|
|
|
1418
1448
|
# +delete+: the session is deleted after (/exit --delete), so no recap.
|
|
1419
1449
|
# +restart+: a new worker takes over (on the newest chi installed), so
|
|
1420
1450
|
# WorkerIdleExit#hold_for_restart's rules, and no recap or discard.
|
|
1451
|
+
# +stop+: the exit ask's "stop them and exit" — stop every hold the user
|
|
1452
|
+
# can stop before leaving; a hold they can't stop refuses the request
|
|
1453
|
+
# (WorkerIdleExit#request_hold checks again, so one that appeared
|
|
1454
|
+
# since the ask is refused too).
|
|
1455
|
+
# +ask+: this client asks the user what to do with what still runs, so the
|
|
1456
|
+
# delegate children are part of the list. An exit that deletes the
|
|
1457
|
+
# session, or one a UI can't ask about (/archive), leaves them out:
|
|
1458
|
+
# they never hold the worker up on their own.
|
|
1421
1459
|
# @return [WorkerIdleExit::Hold, nil] what keeps the worker up (for
|
|
1422
1460
|
# :background_tasks, the watched tasks still running), nil when it
|
|
1423
1461
|
# will leave
|
|
1424
|
-
def exit_request(client_id, delete: false, restart: false)
|
|
1462
|
+
def exit_request(client_id, delete: false, restart: false, stop: false, ask: true)
|
|
1425
1463
|
# The Bridge serves before the idle-exit policy exists.
|
|
1426
1464
|
return WorkerIdleExit::Hold.new(reason: :starting) unless @idle_exit
|
|
1427
1465
|
|
|
@@ -1430,10 +1468,24 @@ module Samagotchi
|
|
|
1430
1468
|
return WorkerIdleExit::Hold.new(reason: hold) if hold
|
|
1431
1469
|
else
|
|
1432
1470
|
# /exit names the tasks it waits for (D3), from the one look that found them.
|
|
1433
|
-
|
|
1434
|
-
|
|
1471
|
+
# The ask covers the delegate children too, but only for a plain /exit: an
|
|
1472
|
+
# exit that deletes the session, or one a UI won't ask about, ignores them.
|
|
1473
|
+
hold = @idle_exit.request_hold(requester: client_id, hint: TASK_STOP_HINT, ask: ask && !delete)
|
|
1474
|
+
if hold
|
|
1475
|
+
# A hold the user can't stop here: no ask, today's refusal.
|
|
1476
|
+
return hold unless hold.ask?
|
|
1477
|
+
# The ask's 409: the list, for the terminal to ask with.
|
|
1478
|
+
return hold unless stop
|
|
1479
|
+
|
|
1480
|
+
@stopped_holds = @hold_stop.stop_all
|
|
1481
|
+
# An unstoppable hold may have arrived while stopping (a child stopped
|
|
1482
|
+
# here is no longer counted).
|
|
1483
|
+
recheck = @idle_exit.request_hold(requester: client_id, hint: TASK_STOP_HINT)
|
|
1484
|
+
return recheck if recheck
|
|
1485
|
+
end
|
|
1435
1486
|
end
|
|
1436
1487
|
|
|
1488
|
+
@stopped_holds = nil unless stop
|
|
1437
1489
|
@exit_requested = true
|
|
1438
1490
|
@exit_requested_by = client_id
|
|
1439
1491
|
@exit_deletes = delete && !restart
|
|
@@ -27,12 +27,27 @@ module Samagotchi
|
|
|
27
27
|
# What keeps the worker up when a client asks it to exit (Worker#exit_request,
|
|
28
28
|
# the Bridge's 409): +reason+, a #hold_for_request (or #hold_for_restart)
|
|
29
29
|
# Symbol, or :starting before this policy exists; for :background_tasks
|
|
30
|
-
# the watched tasks still running ({id:, command:}) and a
|
|
31
|
-
# stopping them
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
# the watched tasks still running ({id:, command:, age_seconds:}) and a
|
|
31
|
+
# +hint+ on stopping them; +holds+ what the user can stop from the exit
|
|
32
|
+
# ask (#StoppableHold), empty when the reason is something they can't.
|
|
33
|
+
Hold = Data.define(:reason, :tasks, :hint, :holds) do
|
|
34
|
+
def initialize(reason:, tasks: nil, hint: nil, holds: []) = super
|
|
35
|
+
|
|
36
|
+
# Held only by things the user can stop: /exit asks what to do with them
|
|
37
|
+
# rather than refusing.
|
|
38
|
+
def ask? = !holds.empty?
|
|
34
39
|
end
|
|
35
40
|
|
|
41
|
+
# One kind of hold the exit ask offers to stop: +kind+ (:reminders,
|
|
42
|
+
# :background_tasks or :delegates) and its +items+ (#HoldItem). The 409's
|
|
43
|
+
# +holds+ is this in JSON: `{kind:, items: [{id:, label:, detail:}]}`.
|
|
44
|
+
StoppableHold = Data.define(:kind, :items)
|
|
45
|
+
|
|
46
|
+
# One entry of a hold: +id+ (a reminder's name, a task id, a child session
|
|
47
|
+
# id), +label+ (the command, the child's title) and +detail+ ("every 20m",
|
|
48
|
+
# "2m", "running a turn").
|
|
49
|
+
HoldItem = Data.define(:id, :label, :detail)
|
|
50
|
+
|
|
36
51
|
# @param engine [Engine] #turn_running?, #last_activity_at, #reminder_store
|
|
37
52
|
# @param bridge [Bridge, nil] #open_streams, #last_client_activity_at; nil
|
|
38
53
|
# when the worker's Bridge failed to start
|
|
@@ -46,10 +61,13 @@ module Samagotchi
|
|
|
46
61
|
# @param watched_running [#call] those of them still running
|
|
47
62
|
# (TaskWatch#running): they hold /exit (one that finished is a note
|
|
48
63
|
# as the worker leaves)
|
|
64
|
+
# @param delegates [#call] the delegate children this session has running
|
|
65
|
+
# or with a live worker (ChildrenStatus::Child): they never hold the
|
|
66
|
+
# exit, but the exit ask lists them so "stop them" stops them too
|
|
49
67
|
# @param clock [#call] monotonic seconds, the Engine's and Bridge's clock
|
|
50
68
|
def initialize(engine:, bridge:, timeout_minutes:, input_pending:, awaiting_continue: -> { false },
|
|
51
69
|
running_tasks: -> { [] }, watched_tasks: -> { [] }, watched_running: -> { [] },
|
|
52
|
-
clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
70
|
+
delegates: -> { [] }, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
53
71
|
@engine = engine
|
|
54
72
|
@bridge = bridge
|
|
55
73
|
@timeout = timeout_minutes.to_f * 60
|
|
@@ -58,6 +76,7 @@ module Samagotchi
|
|
|
58
76
|
@running_tasks = running_tasks
|
|
59
77
|
@watched_tasks = watched_tasks
|
|
60
78
|
@watched_running = watched_running
|
|
79
|
+
@delegates = delegates
|
|
61
80
|
@clock = clock
|
|
62
81
|
@started_at = clock.call
|
|
63
82
|
end
|
|
@@ -96,21 +115,50 @@ module Samagotchi
|
|
|
96
115
|
# :client_connected, :reminders or :background_tasks
|
|
97
116
|
def hold_for_request(requester:, streams: true) = request_hold(requester: requester, streams: streams)&.reason
|
|
98
117
|
|
|
118
|
+
# What the user could stop in this worker right now, whatever else keeps
|
|
119
|
+
# it up: its reminders, the watched background tasks still running, and
|
|
120
|
+
# (for a UI that asks about them, +include_delegates+) the delegate
|
|
121
|
+
# children with a live worker. #request_hold's +holds+ is this, and the
|
|
122
|
+
# web's Stop asks with it before TERMed-ing the worker — a turn running
|
|
123
|
+
# or another UI attached does not hide it, since Stop cancels the turn
|
|
124
|
+
# anyway (docs/sessions.md, "Stopping a worker").
|
|
125
|
+
# @return [Array<StoppableHold>]
|
|
126
|
+
def stoppable_holds(running: @watched_running.call, include_delegates: true)
|
|
127
|
+
holds = []
|
|
128
|
+
reminders = reminder_items
|
|
129
|
+
tasks = running.map do |task|
|
|
130
|
+
HoldItem.new(id: task.id, label: task.command, detail: age_words(task.age_seconds))
|
|
131
|
+
end
|
|
132
|
+
children = include_delegates ? delegate_items : []
|
|
133
|
+
|
|
134
|
+
holds << StoppableHold.new(kind: :reminders, items: reminders) if reminders.any?
|
|
135
|
+
holds << StoppableHold.new(kind: :background_tasks, items: tasks) if tasks.any?
|
|
136
|
+
holds << StoppableHold.new(kind: :delegates, items: children) if children.any?
|
|
137
|
+
holds
|
|
138
|
+
end
|
|
139
|
+
|
|
99
140
|
# #hold_for_request as a Hold: for :background_tasks, the watched tasks
|
|
100
141
|
# still running, read once (TaskWatch#running looks at each task's
|
|
101
|
-
# process), with +hint+.
|
|
142
|
+
# process), with +hint+. +ask+: the client will ask the user about what
|
|
143
|
+
# still runs (an attached terminal's plain `/exit`), so the delegate
|
|
144
|
+
# children this session has running or with a live worker are listed too;
|
|
145
|
+
# they are never a hold on their own, so a request without +ask: true
|
|
146
|
+
# (an exit that deletes the session, the worker's last look before
|
|
147
|
+
# leaving) ignores them.
|
|
102
148
|
# @return [Hold, nil]
|
|
103
|
-
def request_hold(requester:, streams: true, hint: nil)
|
|
149
|
+
def request_hold(requester:, streams: true, hint: nil, ask: false)
|
|
104
150
|
reason = if @engine.turn_running? then :turn_running
|
|
105
151
|
elsif @input_pending.call then :input_queued
|
|
106
152
|
elsif @awaiting_continue.call then :continue_offered
|
|
107
153
|
elsif streams && @bridge && @bridge.open_streams_except(requester).positive? then :client_connected
|
|
108
|
-
elsif @engine.reminder_store&.any? then :reminders
|
|
109
154
|
end
|
|
110
155
|
return Hold.new(reason: reason) if reason
|
|
111
156
|
|
|
112
157
|
running = @watched_running.call
|
|
113
|
-
|
|
158
|
+
holds = stoppable_holds(running: running, include_delegates: ask)
|
|
159
|
+
return nil if holds.empty?
|
|
160
|
+
|
|
161
|
+
Hold.new(reason: holds.first.kind, tasks: running.map(&:to_h), hint: hint, holds: holds)
|
|
114
162
|
end
|
|
115
163
|
|
|
116
164
|
# What keeps the worker from restarting now (a client asked: POST /exit
|
|
@@ -143,5 +191,59 @@ module Samagotchi
|
|
|
143
191
|
last = [@started_at, @engine.last_activity_at, @bridge&.last_client_activity_at].compact.max
|
|
144
192
|
@clock.call - last
|
|
145
193
|
end
|
|
194
|
+
|
|
195
|
+
private
|
|
196
|
+
|
|
197
|
+
# One item per reminder the Engine holds. Every reminder ReminderStore
|
|
198
|
+
# registers is periodic (interval_minutes, 1..1440), so the detail is its
|
|
199
|
+
# schedule — "every 20m", "every 1h" for a whole number of hours — plus
|
|
200
|
+
# ", due now" when its next fire has already come. An entry written
|
|
201
|
+
# without an interval (a store from another chi, a hand-edited one) has no
|
|
202
|
+
# detail: the label alone.
|
|
203
|
+
def reminder_items
|
|
204
|
+
store = @engine.reminder_store
|
|
205
|
+
return [] unless store
|
|
206
|
+
|
|
207
|
+
store.reminders.values.map do |reminder|
|
|
208
|
+
HoldItem.new(id: reminder[:name], label: reminder[:name].to_s, detail: schedule_words(reminder))
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def schedule_words(reminder)
|
|
213
|
+
minutes = Integer(reminder[:interval_minutes], exception: false)
|
|
214
|
+
return nil if minutes.nil? || minutes <= 0
|
|
215
|
+
|
|
216
|
+
words = (minutes % 60).zero? ? "every #{minutes / 60}h" : "every #{minutes}m"
|
|
217
|
+
next_fire = reminder[:next_fire_at]
|
|
218
|
+
words = "#{words}, due now" if next_fire && @clock.call >= next_fire
|
|
219
|
+
words
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# One item per delegate child still running or with a live worker.
|
|
223
|
+
def delegate_items
|
|
224
|
+
@delegates.call.map do |child|
|
|
225
|
+
HoldItem.new(id: child.id, label: child.title.to_s, detail: child_state_words(child))
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# What a child is doing, as the ask says it. #live_delegates takes only
|
|
230
|
+
# ones running a turn or with a worker of their own.
|
|
231
|
+
def child_state_words(child)
|
|
232
|
+
case child.state
|
|
233
|
+
when "running" then "running a turn"
|
|
234
|
+
when "waiting" then "waiting for an answer"
|
|
235
|
+
else "a live worker"
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# How long a task has been running, as the ask words it ("2m", "42s");
|
|
240
|
+
# nil when its record has no start time.
|
|
241
|
+
def age_words(seconds)
|
|
242
|
+
return nil if seconds.nil?
|
|
243
|
+
return "#{seconds / 3600}h" if seconds >= 3600
|
|
244
|
+
return "#{seconds / 60}m" if seconds >= 60
|
|
245
|
+
|
|
246
|
+
"#{seconds}s"
|
|
247
|
+
end
|
|
146
248
|
end
|
|
147
249
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: samagotchi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.118.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dmitry Dedov
|
|
@@ -363,6 +363,7 @@ files:
|
|
|
363
363
|
- lib/samagotchi/guardrails/targets.rb
|
|
364
364
|
- lib/samagotchi/guardrails/verdict.rb
|
|
365
365
|
- lib/samagotchi/guardrails_command.rb
|
|
366
|
+
- lib/samagotchi/hold_stop.rb
|
|
366
367
|
- lib/samagotchi/hook_question.rb
|
|
367
368
|
- lib/samagotchi/hook_routes.rb
|
|
368
369
|
- lib/samagotchi/hooks.rb
|
|
@@ -824,6 +825,7 @@ files:
|
|
|
824
825
|
- lib/samagotchi/web/public/stage_view.js
|
|
825
826
|
- lib/samagotchi/web/public/stage_watch.js
|
|
826
827
|
- lib/samagotchi/web/public/start_page.js
|
|
828
|
+
- lib/samagotchi/web/public/stop_sheet.js
|
|
827
829
|
- lib/samagotchi/web/public/storage.js
|
|
828
830
|
- lib/samagotchi/web/public/stream_handlers.js
|
|
829
831
|
- lib/samagotchi/web/public/strip.js
|