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
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// When the working set's peek and reader ask the item route again: the
|
|
2
|
+
// server's item for a page row (its signature), and which of two answers
|
|
3
|
+
// is the newer. A module of its own (docs/internals/web-frontend.md, "New
|
|
4
|
+
// exports go in new modules"); working_set.js matches rows with it too.
|
|
5
|
+
|
|
6
|
+
// The states the server's items carry (working_set.js STATES).
|
|
7
|
+
export const ITEM_STATES = Object.freeze(["live", "stale", "forgotten", "dropped"]);
|
|
8
|
+
const ELLIPSIS = "…";
|
|
9
|
+
|
|
10
|
+
// The server's items by "kind:key", and its URLs (a cut row key matches one
|
|
11
|
+
// by prefix).
|
|
12
|
+
export function serverIndex(server) {
|
|
13
|
+
const byKey = new Map();
|
|
14
|
+
const urls = [];
|
|
15
|
+
for (const it of server?.items || []) {
|
|
16
|
+
if (!it || !ITEM_STATES.includes(it.state)) continue;
|
|
17
|
+
byKey.set(`${it.kind}:${it.key}`, it);
|
|
18
|
+
if (it.kind === "url") urls.push(it);
|
|
19
|
+
}
|
|
20
|
+
return { byKey, urls };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The server's item for +item+: files by absolute path (a title fallback
|
|
24
|
+
// won't match), memories by name, URLs by the row's key (its params
|
|
25
|
+
// preview: cut at 80 characters with a "…", whose head must start the
|
|
26
|
+
// server's URL). null when the server doesn't know it (a running call).
|
|
27
|
+
export function serverItem(index, item) {
|
|
28
|
+
const exact = index.byKey.get(`${item.kind}:${item.key}`);
|
|
29
|
+
if (exact || item.kind !== "url") return exact || null;
|
|
30
|
+
const key = String(item.key);
|
|
31
|
+
if (!key.endsWith(ELLIPSIS)) return null;
|
|
32
|
+
const head = key.slice(0, -1);
|
|
33
|
+
return index.urls.find((it) => String(it.key).startsWith(head)) || null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The signature of +item+'s server item (count, ops, tokens, state,
|
|
37
|
+
// has_drop, pinned: a new run of it raises count), null when the server
|
|
38
|
+
// doesn't know it (+server+: a snapshot {items, at}, or null). The peek and
|
|
39
|
+
// the reader ask the item route again only when it changed.
|
|
40
|
+
export function serverSig(server, item) {
|
|
41
|
+
const found = item && server ? serverItem(serverIndex(server), item) : null;
|
|
42
|
+
return found ? JSON.stringify([found.count, found.ops, found.tokens, found.state, found.has_drop, found.pinned]) : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Whether the item route's +answer+ is older than the +kept+ one: both
|
|
46
|
+
// from the same running turn (its turn_id) and built from a shorter
|
|
47
|
+
// conversation (at). Its at may go down otherwise (a turn's end keeps
|
|
48
|
+
// less), so a running → idle answer is always taken.
|
|
49
|
+
export function olderAnswer(kept, answer) {
|
|
50
|
+
return !!(kept?.running && answer?.running && kept.turn_id && kept.turn_id === answer.turn_id && Number(answer.at) < Number(kept.at));
|
|
51
|
+
}
|
|
@@ -6,28 +6,35 @@
|
|
|
6
6
|
// One row at a time. The owner's only: working_set_view.js gets none for a
|
|
7
7
|
// participant (the null-control idiom) and draws today's rows.
|
|
8
8
|
//
|
|
9
|
-
// The item's
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
9
|
+
// The item's peek comes from the item route (+getItem+, latest: only the
|
|
10
|
+
// events peekOf picks and the counts; an older worker's whole timeline
|
|
11
|
+
// draws the same); an answer for a row no longer open, or another session,
|
|
12
|
+
// is dropped (latest.js), and so is one older than the drawn one within a
|
|
13
|
+
// running turn (olderAnswer). While a row is open, a new server snapshot
|
|
14
|
+
// asks again (refresh) only when the item's server signature changed
|
|
15
|
+
// (+sigOf+: working_set.js serverSig): mid-turn the route answers the
|
|
16
|
+
// running turn's view as of its last step.
|
|
13
17
|
|
|
14
18
|
import { html, nothing } from "./vendor/lit-html.js";
|
|
15
19
|
import { createLatest } from "./latest.js";
|
|
16
20
|
import { diffCounts, diffTemplate } from "./diff_view.js";
|
|
17
21
|
import { callTemplate, linesTemplate, textTemplate } from "./ws_cards.js";
|
|
18
22
|
import { countsText, eventTag, eventWords, peekOf } from "./ws_timeline.js";
|
|
23
|
+
import { olderAnswer } from "./ws_item_sig.js";
|
|
19
24
|
|
|
20
25
|
// The popover's width while a row is peeked (it is 420 otherwise).
|
|
21
26
|
export const PEEK_WIDTH = 520;
|
|
22
27
|
|
|
23
|
-
// +session()+: the open session's id; +getItem(id, kind, key)+: the item
|
|
24
|
-
// route's answer; +
|
|
25
|
-
//
|
|
26
|
-
|
|
28
|
+
// +session()+: the open session's id; +getItem(id, kind, key, opts)+: the item
|
|
29
|
+
// route's answer; +sigOf(item)+: its server signature (undefined: always ask
|
|
30
|
+
// again); +onChange()+: the peek's content changed (the popover redraws and
|
|
31
|
+
// places itself again).
|
|
32
|
+
export function createPeek({ session, getItem, sigOf = () => undefined, onChange = () => {} }) {
|
|
27
33
|
let openId = null;
|
|
28
34
|
let item = null;
|
|
29
35
|
let data = null;
|
|
30
36
|
let failed = false;
|
|
37
|
+
let askedSig;
|
|
31
38
|
const latest = createLatest(() => `${session()}|${openId}`);
|
|
32
39
|
|
|
33
40
|
function load() {
|
|
@@ -35,10 +42,11 @@ export function createPeek({ session, getItem, onChange = () => {} }) {
|
|
|
35
42
|
if (!id || !item) return;
|
|
36
43
|
const fresh = latest.begin();
|
|
37
44
|
const { kind, key } = item;
|
|
45
|
+
askedSig = sigOf(item);
|
|
38
46
|
Promise.resolve()
|
|
39
|
-
.then(() => getItem(id, kind, key))
|
|
47
|
+
.then(() => getItem(id, kind, key, { latest: true }))
|
|
40
48
|
.then((answer) => {
|
|
41
|
-
if (!fresh()) return;
|
|
49
|
+
if (!fresh() || olderAnswer(data, answer)) return;
|
|
42
50
|
data = answer;
|
|
43
51
|
failed = false;
|
|
44
52
|
onChange();
|
|
@@ -74,11 +82,13 @@ export function createPeek({ session, getItem, onChange = () => {} }) {
|
|
|
74
82
|
isOpen: (id) => openId === id,
|
|
75
83
|
any: () => openId !== null,
|
|
76
84
|
close,
|
|
77
|
-
// A new server snapshot: the open row's timeline asked again
|
|
78
|
-
// drawn one stays until the answer).
|
|
85
|
+
// A new server snapshot: the open row's timeline asked again when its
|
|
86
|
+
// server signature changed (its drawn one stays until the answer).
|
|
79
87
|
refresh(row = item) {
|
|
80
88
|
if (!openId) return;
|
|
81
89
|
item = row || item;
|
|
90
|
+
const sig = sigOf(item);
|
|
91
|
+
if (!failed && sig !== undefined && sig === askedSig) return;
|
|
82
92
|
load();
|
|
83
93
|
},
|
|
84
94
|
// The peek under +row+ (the row's item, overlaid: its runs for the
|
|
@@ -98,7 +108,7 @@ export function createPeek({ session, getItem, onChange = () => {} }) {
|
|
|
98
108
|
if (peek.kind === "diff") body = diffTemplate(event.diff, { fold: 12 });
|
|
99
109
|
else if (peek.kind === "lines") body = linesTemplate(peek.rows, { fold: Infinity });
|
|
100
110
|
else body = textTemplate(peek.rows.map((r) => r.text).join("\n"), { fold: Infinity });
|
|
101
|
-
const words = countsText(events);
|
|
111
|
+
const words = countsText(events, data.counts);
|
|
102
112
|
const foot = html`<div class="ws-peek-f"><button type="button" class="ws-peek-open" data-ws="timeline" data-ws-id=${openId}>Open the timeline${words ? html`<span class="ws-peek-n"> · ${words}</span>` : nothing}</button>${callTemplate(event, row)}</div>`;
|
|
103
113
|
return html`<div class="ws-peek" role="region" aria-label="What chi has">${head}<div class="ws-peek-b">${body}</div>${foot}</div>`;
|
|
104
114
|
},
|
|
@@ -15,9 +15,11 @@
|
|
|
15
15
|
// the owner's: app.js makes this only for a viewer with working_set_items.
|
|
16
16
|
//
|
|
17
17
|
// The timeline comes from the item route (getWorkingSetItem); an answer for
|
|
18
|
-
// an item no longer open, or another session, is dropped (latest.js)
|
|
19
|
-
//
|
|
20
|
-
//
|
|
18
|
+
// an item no longer open, or another session, is dropped (latest.js), and
|
|
19
|
+
// so is one older than the drawn one within a running turn (olderAnswer).
|
|
20
|
+
// While open, a new server snapshot asks again only when the item's server
|
|
21
|
+
// signature changed (working_set.js serverSig): mid-turn the route answers
|
|
22
|
+
// the running turn's view as of its last step. Leaving the
|
|
21
23
|
// session (another one, the start page) resets it: session_view.js
|
|
22
24
|
// forgetSession calls reset(), so #center is never left inert under it.
|
|
23
25
|
|
|
@@ -29,11 +31,13 @@ import { createPeek } from "./ws_peek.js";
|
|
|
29
31
|
import { setTimelineWayIn } from "./ws_way_in.js";
|
|
30
32
|
import { cardTemplate } from "./ws_cards.js";
|
|
31
33
|
import { bandBox, countsText, eventTag, fileStrip, itemPath, linesText, timelineCards } from "./ws_timeline.js";
|
|
34
|
+
import { olderAnswer } from "./ws_item_sig.js";
|
|
32
35
|
import { GLYPHS, leafText, opsText, steerId, steerLine, tokensText } from "./working_set.js";
|
|
33
36
|
|
|
34
|
-
// The item route's answer for session +id+ (+now+: a memory's text now
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
// The item route's answer for session +id+ (+now+: a memory's text now;
|
|
38
|
+
// +latest+: the peek's events and counts only).
|
|
39
|
+
export function getWorkingSetItem(id, kind, key, { now = false, latest = false } = {}) {
|
|
40
|
+
return api(itemPath(id, kind, key, { now, latest }));
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
const PHONE_MAX = 600;
|
|
@@ -68,8 +72,10 @@ export function createWorkingSetPreview({ centerEl, session, state = () => ({}),
|
|
|
68
72
|
let failed = false;
|
|
69
73
|
let tab = "has";
|
|
70
74
|
let openFor = null;
|
|
75
|
+
let askedSig;
|
|
71
76
|
const latest = createLatest(() => `${session()}|${open ? `${open.kind}:${open.key}` : ""}`);
|
|
72
|
-
const
|
|
77
|
+
const sigOf = (it) => workingSet?.serverSig?.(it);
|
|
78
|
+
const peek = createPeek({ session, getItem, sigOf, onChange: () => workingSet?.peekChanged() });
|
|
73
79
|
|
|
74
80
|
const el = doc.createElement("section");
|
|
75
81
|
el.className = "ws-reader";
|
|
@@ -95,10 +101,11 @@ export function createWorkingSetPreview({ centerEl, session, state = () => ({}),
|
|
|
95
101
|
if (!id || !open) return;
|
|
96
102
|
const fresh = latest.begin();
|
|
97
103
|
const { kind, key } = open;
|
|
104
|
+
askedSig = sigOf(current());
|
|
98
105
|
Promise.resolve()
|
|
99
106
|
.then(() => getItem(id, kind, key, { now: kind === "memory" }))
|
|
100
107
|
.then((answer) => {
|
|
101
|
-
if (!fresh()) return;
|
|
108
|
+
if (!fresh() || olderAnswer(data, answer)) return;
|
|
102
109
|
data = answer;
|
|
103
110
|
failed = false;
|
|
104
111
|
draw();
|
|
@@ -295,12 +302,15 @@ export function createWorkingSetPreview({ centerEl, session, state = () => ({}),
|
|
|
295
302
|
workingSet = view;
|
|
296
303
|
if (!view) return;
|
|
297
304
|
setTimelineWayIn(true);
|
|
298
|
-
// A new snapshot: asked again
|
|
299
|
-
//
|
|
305
|
+
// A new snapshot: asked again when the open item's server signature
|
|
306
|
+
// changed, else only drawn (the set beside it); a snapshot of another session: closed, though
|
|
307
|
+
// forgetSession's reset has done it already).
|
|
300
308
|
view.onServer(() => {
|
|
301
309
|
if (el.hidden) return;
|
|
302
|
-
if (session() !== openFor) close({ refocus: false });
|
|
303
|
-
|
|
310
|
+
if (session() !== openFor) return close({ refocus: false });
|
|
311
|
+
const sig = sigOf(current());
|
|
312
|
+
if (failed || sig === undefined || sig !== askedSig) load();
|
|
313
|
+
else draw();
|
|
304
314
|
});
|
|
305
315
|
},
|
|
306
316
|
open: openItem,
|
|
@@ -35,10 +35,12 @@ export function eventState(event) {
|
|
|
35
35
|
return "sent";
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
// The path of the item route for session +id+ (+now+: a memory's text now
|
|
39
|
-
|
|
38
|
+
// The path of the item route for session +id+ (+now+: a memory's text now;
|
|
39
|
+
// +latest+: the peek's events and counts only).
|
|
40
|
+
export function itemPath(id, kind, key, { now = false, latest = false } = {}) {
|
|
40
41
|
const query = new URLSearchParams({ kind, key });
|
|
41
42
|
if (now) query.set("now", "1");
|
|
43
|
+
if (latest) query.set("latest", "1");
|
|
42
44
|
return `/api/sessions/${encodeURIComponent(id)}/working_set/item?${query}`;
|
|
43
45
|
}
|
|
44
46
|
|
|
@@ -132,10 +134,11 @@ export function bandBox(band, length) {
|
|
|
132
134
|
return { left: `${left.toFixed(2)}%`, width: `${Math.min(width, 100 - left).toFixed(2)}%` };
|
|
133
135
|
}
|
|
134
136
|
|
|
135
|
-
// "3 reads, 1 edit" (the peek's link and the reader's head)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const
|
|
137
|
+
// "3 reads, 1 edit" (the peek's link and the reader's head); +counts+: a
|
|
138
|
+
// latest=1 answer's ({read, edit}), whose events are the peek's only.
|
|
139
|
+
export function countsText(events = [], counts = null) {
|
|
140
|
+
const reads = counts ? Number(counts.read) || 0 : events.filter((e) => e.op === "read" || e.op === "memory_read" || e.op === "fetch").length;
|
|
141
|
+
const edits = counts ? Number(counts.edit) || 0 : events.filter((e) => e.op === "edit" || e.op === "write" || e.op === "memory_write").length;
|
|
139
142
|
const word = (n, one) => `${n} ${one}${n === 1 ? "" : "s"}`;
|
|
140
143
|
return [reads ? word(reads, "read") : "", edits ? word(edits, "edit") : ""].filter(Boolean).join(", ");
|
|
141
144
|
}
|
|
@@ -480,7 +480,8 @@ module Samagotchi
|
|
|
480
480
|
# {status: "restarted", session_id, from_version, version} (version nil:
|
|
481
481
|
# the new worker is still starting; its bridge_up follows); 409
|
|
482
482
|
# {error: "held" | "not_running" | "unsupported" | "failed", reason, detail} in words (a stopped
|
|
483
|
-
# worker: held, reason stopped, its pid; a stop marked: held, reason stop_marked
|
|
483
|
+
# worker: held, reason stopped, its pid; a stop marked: held, reason stop_marked;
|
|
484
|
+
# deleting: held, reason deleting); 409
|
|
484
485
|
# owned_by_tui; 404.
|
|
485
486
|
def handle_restart(_req, id)
|
|
486
487
|
result = @manager.restart_session(id, state_dir: @state_dir, client_id: ClientId::WEB_RESTART)
|
|
@@ -498,12 +499,17 @@ module Samagotchi
|
|
|
498
499
|
# POST /api/sessions/:id/archive: hide it (and its delegates) from the
|
|
499
500
|
# lists, keep it for good. A live idle worker is stopped first; a turn
|
|
500
501
|
# running in it or a delegate, a one-turn chi run's session and a scratch one
|
|
501
|
-
# are refused (409).
|
|
502
|
-
|
|
503
|
-
|
|
502
|
+
# are refused (409). Background tasks it or a delegate started that
|
|
503
|
+
# still run: 409 tasks_running, unless the body says {stop_tasks: true}
|
|
504
|
+
# (then stopped first; the 200 names them, #task_fields).
|
|
505
|
+
def handle_archive(req, id)
|
|
506
|
+
body = parse_json(request_body(req, 4096))
|
|
507
|
+
result = @manager.archive_session(id, state_dir: @state_dir, wait: STOP_WAIT_SECONDS,
|
|
508
|
+
running_tasks: running_tasks_policy(body.is_a?(Hash) && body["stop_tasks"]))
|
|
504
509
|
(result.archived + result.discarded).each { |sid| @hub&.touch(sid) }
|
|
505
510
|
json_response(200, { status: "archived", session_id: result.id, archived: result.archived,
|
|
506
|
-
stopped: result.stopped, discarded: result.discarded
|
|
511
|
+
stopped: result.stopped, discarded: result.discarded,
|
|
512
|
+
**task_fields(result.to_h) })
|
|
507
513
|
rescue SessionManager::OwnedByTUI => e
|
|
508
514
|
owned_by_one_shot(id, e)
|
|
509
515
|
rescue SessionManager::ArchiveRefused => e
|
|
@@ -521,30 +527,36 @@ module Samagotchi
|
|
|
521
527
|
# force a live worker is stopped first (the page sends it: its confirm
|
|
522
528
|
# says so); without it a live worker's session is refused, 409
|
|
523
529
|
# worker_running, as chi sessions delete without --force. A one-turn chi run's
|
|
524
|
-
# session is always refused.
|
|
530
|
+
# session is always refused. Background tasks the delete's sessions
|
|
531
|
+
# started that still run: 409 tasks_running, unless ?stop_tasks=true
|
|
532
|
+
# (then stopped first; the 200 names them, #task_fields).
|
|
525
533
|
def handle_delete(req, id)
|
|
526
534
|
result = @manager.delete_session(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
|
|
527
535
|
wait: STOP_WAIT_SECONDS, revive_previous: true, refuse_continuing: true,
|
|
528
|
-
expect: req.GET["expect"]
|
|
536
|
+
expect: req.GET["expect"],
|
|
537
|
+
running_tasks: running_tasks_policy(req.GET["stop_tasks"]))
|
|
529
538
|
touch_deleted(result[:id], result)
|
|
530
539
|
json_response(200, { status: "deleted", session_id: result[:id], stopped: result[:stopped],
|
|
531
540
|
unarchived: Array(result[:unarchived]), moved_back: Array(result[:moved_back]),
|
|
532
|
-
deleted: Array(result[:deleted] || [result[:id]]), left_running: Array(result[:left_running])
|
|
541
|
+
deleted: Array(result[:deleted] || [result[:id]]), left_running: Array(result[:left_running]),
|
|
542
|
+
**task_fields(result) })
|
|
533
543
|
rescue SessionManager::OwnedByTUI => e
|
|
534
544
|
owned_by_one_shot(id, e)
|
|
535
545
|
rescue SessionManager::DeleteRefused => e
|
|
536
546
|
delete_refused(e)
|
|
537
547
|
end
|
|
538
548
|
|
|
539
|
-
# DELETE /api/sessions/:id/chain?force=: every link of the
|
|
540
|
-
# their delegates (SessionManager.delete_chain). Query-only.
|
|
549
|
+
# DELETE /api/sessions/:id/chain?force=&stop_tasks=: every link of the
|
|
550
|
+
# chain with their delegates (SessionManager.delete_chain). Query-only.
|
|
541
551
|
def handle_delete_chain(req, id)
|
|
542
552
|
result = @manager.delete_chain(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
|
|
543
|
-
wait: STOP_WAIT_SECONDS, expect: req.GET["expect"]
|
|
553
|
+
wait: STOP_WAIT_SECONDS, expect: req.GET["expect"],
|
|
554
|
+
running_tasks: running_tasks_policy(req.GET["stop_tasks"]))
|
|
544
555
|
touch_deleted(id, result)
|
|
545
556
|
json_response(200, { status: "deleted", session_id: id, deleted: result[:deleted], left: result[:left],
|
|
546
557
|
left_running: result[:left_running], unarchived: result[:unarchived],
|
|
547
|
-
stopped: result[:stopped], cut_off: result[:cut_off], refusal: result[:refusal]
|
|
558
|
+
stopped: result[:stopped], cut_off: result[:cut_off], refusal: result[:refusal],
|
|
559
|
+
**task_fields(result) })
|
|
548
560
|
rescue SessionManager::OwnedByTUI => e
|
|
549
561
|
owned_by_one_shot(e.session_id, e)
|
|
550
562
|
rescue SessionManager::DeleteRefused => e
|
|
@@ -573,9 +585,34 @@ module Samagotchi
|
|
|
573
585
|
ids.compact.uniq.each { |sid| @hub.touch(sid) }
|
|
574
586
|
end
|
|
575
587
|
|
|
588
|
+
# A user's delete or archive: refused on running tasks (REFUSE), or,
|
|
589
|
+
# asked to (+stop_tasks+: the page's second confirm, --stop-tasks),
|
|
590
|
+
# stopped by "user".
|
|
591
|
+
def running_tasks_policy(stop_tasks)
|
|
592
|
+
policies = SessionManager::RunningTasks::Policy
|
|
593
|
+
truthy_param?(stop_tasks) ? policies.stop(by: "user") : policies::REFUSE
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
# The 200's tasks_stopped, tasks_not_stopped and tasks_ended (each
|
|
597
|
+
# [{session_id, id, command}], empty unless the tasks were stopped).
|
|
598
|
+
def task_fields(result)
|
|
599
|
+
%i[tasks_stopped tasks_not_stopped tasks_ended].to_h { |key| [key, Array(result[key]).map(&:to_wire)] }
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
# The 409 tasks_running's own fields: +op+ ("delete" or "archive"),
|
|
603
|
+
# the tasks ({running, unverified, unreadable}), whether the worker
|
|
604
|
+
# was stopped, a chain's links (the words name them).
|
|
605
|
+
def tasks_running_fields(error, op)
|
|
606
|
+
links = error.respond_to?(:links) ? error.links : []
|
|
607
|
+
{ op: op, tasks: error.tasks.to_wire, worker_stopped: error.worker_stopped, stopped_worker: error.stopped_worker,
|
|
608
|
+
links: (links unless links.empty?) }
|
|
609
|
+
end
|
|
610
|
+
|
|
576
611
|
# 409 with the refusal's reason as its code, and what a remote client
|
|
577
|
-
# raises it again from (ids, the schedule).
|
|
612
|
+
# raises it again from (ids, the schedule; tasks_running's tasks).
|
|
578
613
|
def delete_refused(error)
|
|
614
|
+
return delete_tasks_refused(error) if error.reason == :tasks_running
|
|
615
|
+
|
|
579
616
|
detail = error.reason == :still_stopping ? "#{error.message}; try again in a moment" : error.message
|
|
580
617
|
# A moved delegate that can't go back: whether the page can stop it
|
|
581
618
|
# (its "Stop its worker, then delete"), as the refusal saw it.
|
|
@@ -588,6 +625,11 @@ module Samagotchi
|
|
|
588
625
|
schedule_id: error.schedule_id, stoppable: stoppable)
|
|
589
626
|
end
|
|
590
627
|
|
|
628
|
+
def delete_tasks_refused(error)
|
|
629
|
+
refusal_response(409, "tasks_running", error, session_id: error.session_id, reason: "tasks_running",
|
|
630
|
+
**tasks_running_fields(error, "delete"))
|
|
631
|
+
end
|
|
632
|
+
|
|
591
633
|
# 409 owned_by_tui in the words a refused archive, delete or continue
|
|
592
634
|
# shows (stop, restart, a turn and a command show OwnedByTUI's own).
|
|
593
635
|
def owned_by_one_shot(id, error = nil)
|
|
@@ -604,10 +646,17 @@ module Samagotchi
|
|
|
604
646
|
refusal_response(409, "owned_by_tui", error, session_id: error.session_id, scratch: scratch)
|
|
605
647
|
end
|
|
606
648
|
|
|
607
|
-
# 409 scratch or busy (every
|
|
608
|
-
#
|
|
609
|
-
# reason for a remote client.
|
|
649
|
+
# 409 scratch, tasks_running (#tasks_running_fields) or busy (every
|
|
650
|
+
# other reason: a turn, a queued prompt, an open step-limit question,
|
|
651
|
+
# in it or a delegate, +busy_id+), with the reason for a remote client.
|
|
652
|
+
# tasks_running, not busy: an older chi --remote then says chi web's
|
|
653
|
+
# words, not "a turn is running".
|
|
610
654
|
def archive_refused(error)
|
|
655
|
+
if error.reason == :tasks_running
|
|
656
|
+
return refusal_response(409, "tasks_running", error, session_id: error.session_id, reason: "tasks_running",
|
|
657
|
+
**tasks_running_fields(error, "archive"))
|
|
658
|
+
end
|
|
659
|
+
|
|
611
660
|
refusal_response(409, error.reason == :scratch ? "scratch" : "busy", error, session_id: error.session_id,
|
|
612
661
|
reason: error.reason.to_s, busy_id: error.busy_id)
|
|
613
662
|
end
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "uri"
|
|
3
4
|
require_relative "../working_set"
|
|
4
5
|
require_relative "../llm_context_view"
|
|
5
6
|
require_relative "../session_setting"
|
|
6
7
|
require_relative "../memory_paths"
|
|
8
|
+
require_relative "../worker_link"
|
|
7
9
|
require_relative "participant_view"
|
|
8
10
|
|
|
9
11
|
module Samagotchi
|
|
@@ -43,26 +45,45 @@ module Samagotchi
|
|
|
43
45
|
json_response(200, shown.merge("running" => set[:running] == true))
|
|
44
46
|
end
|
|
45
47
|
|
|
46
|
-
# GET /api/sessions/:id/working_set/item?kind=&key=[&now=1]: what the
|
|
48
|
+
# GET /api/sessions/:id/working_set/item?kind=&key=[&now=1][&latest=1]: what the
|
|
47
49
|
# model has of one item, its timeline (WorkingSet.timeline_of: every
|
|
48
50
|
# run of it, in conversation order), the reader's and the popover
|
|
49
|
-
# peek's. Owner only.
|
|
50
|
-
#
|
|
51
|
-
#
|
|
51
|
+
# peek's. Owner only. A live worker with the feature
|
|
52
|
+
# (WorkingSet::ITEM_FEATURE) answers just the item (the Bridge's GET
|
|
53
|
+
# working_set/item: the running turn's view mid-turn, with at and
|
|
54
|
+
# running), passed on as is: no snapshot, no session file; no answer
|
|
55
|
+
# or a failed one is a 502 (the page asks again). Otherwise, or on a
|
|
56
|
+
# 404 (no such route), it is built from messages: an older
|
|
57
|
+
# worker's snapshot messages (they lag a running turn's events), else
|
|
52
58
|
# the file's. Whole texts: the tool limits bound them (read's
|
|
53
59
|
# truncate_at_bytes, the ToolRunner cap). +now+ (a memory): what its
|
|
54
60
|
# latest read would answer now and the diff (WorkingSet.memory_now).
|
|
61
|
+
# +latest+ (the peek): only the events it draws and the counts
|
|
62
|
+
# (WorkingSet::Timeline#peek_h).
|
|
55
63
|
def handle_working_set_item(req, id)
|
|
56
64
|
kind = req.GET["kind"].to_s
|
|
57
65
|
key = req.GET["key"].to_s
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
return error_response(400, "invalid_item", WorkingSet::INVALID_ITEM) unless WorkingSet::KINDS.include?(kind) && !key.strip.empty?
|
|
67
|
+
|
|
68
|
+
latest = req.GET["latest"] == "1"
|
|
69
|
+
features = WorkerLink.live_features(id, state_dir: default_state_dir)
|
|
70
|
+
if Array(features).include?(WorkingSet::ITEM_FEATURE)
|
|
71
|
+
query = { kind: kind, key: key }
|
|
72
|
+
query[:now] = "1" if req.GET["now"] == "1"
|
|
73
|
+
query[:latest] = "1" if latest
|
|
74
|
+
status, answer = bridge_get(id, "working_set/item?#{URI.encode_www_form(query)}")
|
|
75
|
+
return json_response(200, answer) if status == 200 && answer.is_a?(Hash)
|
|
76
|
+
# No answer, or a failed one: the page marks its ask failed and asks again at the next event, rather than
|
|
77
|
+
# drawing an older worker's lagging messages it would never ask again for. Only a 404 (no such route) falls back.
|
|
78
|
+
unless status == 404
|
|
79
|
+
return error_response(502, "bridge_error", "the session's worker didn't answer the item (#{status || "no reply"})")
|
|
80
|
+
end
|
|
60
81
|
end
|
|
61
82
|
|
|
62
83
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
63
|
-
source = working_set_source(bridge_get_json(id, "snapshot"), session)
|
|
84
|
+
source = working_set_source(features && bridge_get_json(id, "snapshot"), session)
|
|
64
85
|
timeline = WorkingSet.timeline_of(source.messages, kind: kind, key: key, strategy: source.strategy, cwd: source.cwd)
|
|
65
|
-
body = timeline.to_h
|
|
86
|
+
body = latest ? timeline.peek_h : timeline.to_h
|
|
66
87
|
if req.GET["now"] == "1" && kind == WorkingSet::MEMORY
|
|
67
88
|
body[:now] = WorkingSet.memory_now(source.messages, key: key, strategy: source.strategy, cwd: source.cwd,
|
|
68
89
|
root: MemoryPaths.system_dir)&.to_h
|
|
@@ -109,11 +109,19 @@ module Samagotchi
|
|
|
109
109
|
def live? = false
|
|
110
110
|
def worker_owned? = true
|
|
111
111
|
|
|
112
|
+
def short_message(via: ChiInvocation::HERE)
|
|
113
|
+
"its worker runs a newer chi: #{remedy(via)}"
|
|
114
|
+
end
|
|
115
|
+
|
|
112
116
|
def message(session_id, via: ChiInvocation::HERE)
|
|
113
117
|
"session #{session_id}'s worker runs chi #{sidecar.version || "?"} (protocol #{sidecar.protocol}), " \
|
|
114
118
|
"newer than this chi #{Samagotchi::VERSION} (protocol #{PROTOCOL}): restart " \
|
|
115
|
-
"#{via
|
|
119
|
+
"#{remedy(via)}"
|
|
116
120
|
end
|
|
121
|
+
|
|
122
|
+
private
|
|
123
|
+
|
|
124
|
+
def remedy(via) = "restart #{via.remote? ? via.web : "chi web"} or update this chi"
|
|
117
125
|
end
|
|
118
126
|
|
|
119
127
|
module_function
|
|
@@ -37,6 +37,13 @@ module Samagotchi
|
|
|
37
37
|
FILE = "file"
|
|
38
38
|
MEMORY = "memory"
|
|
39
39
|
URL = "url"
|
|
40
|
+
KINDS = [FILE, MEMORY, URL].freeze
|
|
41
|
+
# An item route's refusal of a kind not in KINDS or a blank key (chi
|
|
42
|
+
# web's and the Bridge's alike).
|
|
43
|
+
INVALID_ITEM = "expected kind (file, memory or url) and key"
|
|
44
|
+
# The Bridge feature: GET working_set/item answers one item's timeline
|
|
45
|
+
# from the worker (the running turn's view mid-turn).
|
|
46
|
+
ITEM_FEATURE = "working_set_item"
|
|
40
47
|
LIVE = "live"
|
|
41
48
|
STALE = "stale"
|
|
42
49
|
FORGOTTEN = "forgotten"
|
|
@@ -245,7 +252,26 @@ module Samagotchi
|
|
|
245
252
|
# (the reader shows them newest first).
|
|
246
253
|
Timeline = Data.define(:kind, :key, :events) do
|
|
247
254
|
def to_h = { kind: kind, key: key, events: events.map(&:to_h) }
|
|
255
|
+
|
|
256
|
+
# The peek's answer (the route's latest=1): only the events the
|
|
257
|
+
# page's peek draws (ws_timeline.js peekOf, ported: the latest edit or
|
|
258
|
+
# write with a diff and the newest event after it, else the newest
|
|
259
|
+
# run that isn't one, else the newest), and the counts of its "N reads,
|
|
260
|
+
# M edits" (countsText). peekOf over these picks what it picks over all.
|
|
261
|
+
def peek_h
|
|
262
|
+
edit_at = events.rindex { |e| PEEK_EDITS.include?(e.op) && e.diff }
|
|
263
|
+
shown = if edit_at
|
|
264
|
+
[events[edit_at], events[(edit_at + 1)..].last].compact
|
|
265
|
+
else
|
|
266
|
+
[events.reverse.find { |e| !PEEK_EDITS.include?(e.op) } || events.last].compact
|
|
267
|
+
end
|
|
268
|
+
counts = { read: events.count { |e| COUNT_READS.include?(e.op) }, edit: events.count { |e| COUNT_EDITS.include?(e.op) } }
|
|
269
|
+
{ kind: kind, key: key, events: shown.map(&:to_h), counts: counts, latest: true }
|
|
270
|
+
end
|
|
248
271
|
end
|
|
272
|
+
PEEK_EDITS = %w[edit write].freeze
|
|
273
|
+
COUNT_READS = %w[read memory_read fetch].freeze
|
|
274
|
+
COUNT_EDITS = %w[edit write memory_write].freeze
|
|
249
275
|
|
|
250
276
|
# A stale memory's text now against what the model has: the +text+
|
|
251
277
|
# memory_read would answer now for the names the item's latest read
|
|
@@ -33,7 +33,7 @@ module Samagotchi
|
|
|
33
33
|
WorkingSetSteer::ADD => "added", WorkingSetSteer::UNADD => "unadded" }.freeze
|
|
34
34
|
PIN_OPS = [WorkingSetSteer::PIN, WorkingSetSteer::UNPIN].freeze
|
|
35
35
|
|
|
36
|
-
# @param engine [Engine] #session, #working_set_strategy, #prompt_memories,
|
|
36
|
+
# @param engine [Engine] #session, #working_set_strategy, #working_set_view, #prompt_memories,
|
|
37
37
|
# #clone_messages, #replace_messages
|
|
38
38
|
# @param save [#call] saves the session after a change
|
|
39
39
|
def initialize(engine:, save:)
|
|
@@ -77,14 +77,17 @@ module Samagotchi
|
|
|
77
77
|
[lines.join("\n"), changed, done.positive? ? :ok : :error]
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
# The set
|
|
80
|
+
# The set as the model has it now (Engine#working_set_view: the running
|
|
81
|
+
# turn's as of its last request, else the session's under the next
|
|
82
|
+
# turn's strategy), and the pending ops (mid-turn the turn's copy holds
|
|
83
|
+
# the sent note, so none are).
|
|
81
84
|
def listing
|
|
82
85
|
session = @engine.session
|
|
83
|
-
|
|
86
|
+
view = session && @engine.working_set_view
|
|
87
|
+
return Session::NONE_YET unless view
|
|
84
88
|
|
|
85
|
-
messages =
|
|
86
|
-
items = WorkingSet.build(messages, cwd:
|
|
87
|
-
pins: session.working_set_pins).items
|
|
89
|
+
messages = view.conversation
|
|
90
|
+
items = WorkingSet.build(messages, cwd: view.cwd, strategy: view.strategy, pins: session.working_set_pins).items
|
|
88
91
|
return "working set: empty (the files, memories and URLs the turns read show here)\n#{USAGE}" if items.empty?
|
|
89
92
|
|
|
90
93
|
rows = items.map { |item| [WorkingSetSteer.quote(name(item, session.working_directory)), words(item)] }
|
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.178.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dmitry Dedov
|
|
@@ -699,6 +699,7 @@ files:
|
|
|
699
699
|
- lib/samagotchi/session_manager/lifecycle.rb
|
|
700
700
|
- lib/samagotchi/session_manager/listing.rb
|
|
701
701
|
- lib/samagotchi/session_manager/removal.rb
|
|
702
|
+
- lib/samagotchi/session_manager/running_tasks.rb
|
|
702
703
|
- lib/samagotchi/session_manager/spawn.rb
|
|
703
704
|
- lib/samagotchi/session_manager/worker_command.rb
|
|
704
705
|
- lib/samagotchi/session_metrics.rb
|
|
@@ -1051,6 +1052,7 @@ files:
|
|
|
1051
1052
|
- lib/samagotchi/web/public/revise_stack.js
|
|
1052
1053
|
- lib/samagotchi/web/public/revise_switch.js
|
|
1053
1054
|
- lib/samagotchi/web/public/route.js
|
|
1055
|
+
- lib/samagotchi/web/public/running_tasks.js
|
|
1054
1056
|
- lib/samagotchi/web/public/scope.js
|
|
1055
1057
|
- lib/samagotchi/web/public/scroll.js
|
|
1056
1058
|
- lib/samagotchi/web/public/send_turn.js
|
|
@@ -1146,6 +1148,7 @@ files:
|
|
|
1146
1148
|
- lib/samagotchi/web/public/working_set_rail.js
|
|
1147
1149
|
- lib/samagotchi/web/public/working_set_view.js
|
|
1148
1150
|
- lib/samagotchi/web/public/ws_cards.js
|
|
1151
|
+
- lib/samagotchi/web/public/ws_item_sig.js
|
|
1149
1152
|
- lib/samagotchi/web/public/ws_peek.js
|
|
1150
1153
|
- lib/samagotchi/web/public/ws_preview.css
|
|
1151
1154
|
- lib/samagotchi/web/public/ws_reader.js
|