clavarium 0.2.24-next.3 → 0.2.24-next.4
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.
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/server/health.mjs +3 -1
- package/server/healthdocket.mjs +106 -0
- package/server/main.mjs +6 -0
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.24-next.
|
|
3
|
+
"version": "0.2.24-next.4",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "clavarium",
|
|
9
|
-
"version": "0.2.24-next.
|
|
9
|
+
"version": "0.2.24-next.4",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/health.mjs
CHANGED
|
@@ -128,6 +128,8 @@ export class Health {
|
|
|
128
128
|
#vms; #conversations; #resources; #records = new Map(); #running = new Map(); #quiet = new Map(); #settleMs; #probeMs; #lookMs; #seen;
|
|
129
129
|
/* Set by the server: told each judgment (the chat of a conversation says what is wrong: talk.mjs). */
|
|
130
130
|
judged = () => {};
|
|
131
|
+
/* Set by the server: told a conversation is forgotten (what else is kept of its machine goes too). */
|
|
132
|
+
forgotten = () => {};
|
|
131
133
|
|
|
132
134
|
/* `settleMs`: how long a missing folder is given before it is mounted
|
|
133
135
|
again (a machine that has just started may still be mounting it).
|
|
@@ -148,7 +150,7 @@ export class Health {
|
|
|
148
150
|
this.#seen?.put({ name, keys, at: new Date().toISOString() });
|
|
149
151
|
return this.of(name);
|
|
150
152
|
}
|
|
151
|
-
forget(name) { this.#records.delete(name); this.#quiet.delete(name); this.#seen?.remove?.(name); }
|
|
153
|
+
forget(name) { this.#records.delete(name); this.#quiet.delete(name); this.#seen?.remove?.(name); try { this.forgotten(name); } catch { /* told */ } }
|
|
152
154
|
|
|
153
155
|
/* The look under way (it may be mounting a folder again), or null. */
|
|
154
156
|
busy(name) { return this.#running.get(name) ?? null; }
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/* A MACHINE SHORT OF ROOM, TOLD TO ITS AGENT THROUGH ITS DOCKET (dockets.mjs),
|
|
2
|
+
* where its chat is on -- nowhere else. Its owner is told as before (talk.mjs,
|
|
3
|
+
* healthreact.mjs); this is on top: one event per threshold crossed, which the
|
|
4
|
+
* agent handles and acknowledges (docket done / failed: said in its chat).
|
|
5
|
+
*
|
|
6
|
+
* disk (/ and /work, each) at DISK_LEVELS of its size used
|
|
7
|
+
* memory at MEMORY_LEVELS used, at SUSTAINED looks in a row
|
|
8
|
+
* (health.mjs looks every two minutes: a peak is not news)
|
|
9
|
+
* out of memory the whole machine (not a program at its own limit),
|
|
10
|
+
* each time it happens again, OOM_QUIET_MS apart at least
|
|
11
|
+
*
|
|
12
|
+
* NO REPEAT: a level is posted once; again only when it got worse (the next
|
|
13
|
+
* level) or after it went below its REARM (a few points under it) and came
|
|
14
|
+
* back. What was posted is kept in a store, so a restart of the server does
|
|
15
|
+
* not post it again.
|
|
16
|
+
*
|
|
17
|
+
* Not where its owner chose its own reaction (healthreact.mjs): its agent is told
|
|
18
|
+
* one way, not two.
|
|
19
|
+
*
|
|
20
|
+
* Only a machine that runs and answered is judged: one that does not answer
|
|
21
|
+
* cannot read its docket, and what it has is not known (nothing changes).
|
|
22
|
+
*
|
|
23
|
+
* judged(name, record) told each judgment by health.mjs
|
|
24
|
+
* forget(name)
|
|
25
|
+
*/
|
|
26
|
+
export const DISK_LEVELS = [85, 95];
|
|
27
|
+
export const MEMORY_LEVELS = [90, 97];
|
|
28
|
+
/* Below this many points under a level, it may be posted again. */
|
|
29
|
+
export const REARM = 5;
|
|
30
|
+
export const SUSTAINED = 2;
|
|
31
|
+
export const OOM_QUIET_MS = 60 * 60_000;
|
|
32
|
+
export const KIND = 'machine-health';
|
|
33
|
+
|
|
34
|
+
const ACK = 'then say what you did (docket done ID "what")';
|
|
35
|
+
|
|
36
|
+
/* The highest level reached by `percent`, 0 when none. */
|
|
37
|
+
const reached = (percent, levels) => levels.reduce((n, l, i) => (percent >= l ? i + 1 : n), 0);
|
|
38
|
+
/* What is kept of `before` once at `percent`: a level stays posted until it is REARM under it. */
|
|
39
|
+
const kept = (before, percent, levels) => { let n = before; while (n > 0 && percent < levels[n - 1] - REARM) n -= 1; return n; };
|
|
40
|
+
|
|
41
|
+
export function diskText(path, percent) {
|
|
42
|
+
return path === '/'
|
|
43
|
+
? `Disk of your machine at ${percent}% (/): free space -- caches, temporary files, build outputs -- ${ACK}.`
|
|
44
|
+
: `Work folder of your machine at ${percent}% (${path}): free space without losing work -- caches, build outputs -- ${ACK}.`;
|
|
45
|
+
}
|
|
46
|
+
export function memoryText(percent) {
|
|
47
|
+
return `Memory of your machine at ${percent}% for several minutes: stop what is no longer needed -- servers, watchers, builds left running -- ${ACK}.`;
|
|
48
|
+
}
|
|
49
|
+
export function oomText(times) {
|
|
50
|
+
return `Your machine ran out of memory${times > 1 ? ` (${times} times since it started)` : ''}: a program was stopped. Run less at once, ${ACK}.`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class HealthDocket {
|
|
54
|
+
#store; #dockets; #now;
|
|
55
|
+
#reacting;
|
|
56
|
+
/* `dockets`: { on(name), post(name, {kind, text, data}) } (dockets.mjs). `reacting(name)`: its owner
|
|
57
|
+
turned on its own reaction to a machine short of room (healthreact.mjs). */
|
|
58
|
+
constructor({ store, dockets, reacting = () => false, now = () => Date.now() }) { this.#store = store; this.#dockets = dockets; this.#reacting = reacting; this.#now = now; }
|
|
59
|
+
|
|
60
|
+
forget(name) { this.#store.remove?.(name); }
|
|
61
|
+
|
|
62
|
+
judged(name, record) {
|
|
63
|
+
if (!record || record.state !== 'Running' || record.answering !== true) return [];
|
|
64
|
+
if (!this.#dockets.on(name) || this.#reacting(name)) return [];
|
|
65
|
+
const before = this.#store.get(name) ?? {};
|
|
66
|
+
const r = { name, disks: { ...(before.disks ?? {}) }, memory: { ...(before.memory ?? {}) }, ...(before.oom ? { oom: { ...before.oom } } : {}) };
|
|
67
|
+
const posted = [];
|
|
68
|
+
/* Posted, or not (its folder cannot be written): then tried again at the next look. */
|
|
69
|
+
const post = (text, data) => {
|
|
70
|
+
try { posted.push(this.#dockets.post(name, { kind: KIND, text, data })); return true; } catch { return false; }
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
for (const [path, d] of Object.entries(record.disks ?? {})) {
|
|
74
|
+
if (!(d.sizeMb > 0)) continue;
|
|
75
|
+
const percent = Math.round(100 * (d.sizeMb - d.availableMb) / d.sizeMb);
|
|
76
|
+
const was = kept(r.disks[path] ?? 0, percent, DISK_LEVELS);
|
|
77
|
+
const now = reached(percent, DISK_LEVELS);
|
|
78
|
+
r.disks[path] = now > was && post(diskText(path, percent), { health: 'disk', path, percent, level: DISK_LEVELS[now - 1] }) ? now : was;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const m = record.memory;
|
|
82
|
+
if (m && m.totalMb > 0) {
|
|
83
|
+
const percent = Math.round(100 * (m.totalMb - m.availableMb) / m.totalMb);
|
|
84
|
+
const now = reached(percent, MEMORY_LEVELS);
|
|
85
|
+
/* Looks in a row at a level or above. */
|
|
86
|
+
const high = now > 0 ? (r.memory.high ?? 0) + 1 : 0;
|
|
87
|
+
const was = kept(r.memory.level ?? 0, percent, MEMORY_LEVELS);
|
|
88
|
+
const sent = now > was && high >= SUSTAINED && post(memoryText(percent), { health: 'memory', percent, level: MEMORY_LEVELS[now - 1] });
|
|
89
|
+
r.memory = { level: sent ? now : was, high };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* The whole machine out of memory (the kernel's own words: health.mjs). Within the quiet time,
|
|
93
|
+
the count kept is not raised: told once it is over. A count lower than the one kept: the
|
|
94
|
+
machine started again, the count with it. */
|
|
95
|
+
const times = record.oomKinds?.machine;
|
|
96
|
+
if (Number.isInteger(times)) {
|
|
97
|
+
const last = r.oom ?? { count: 0, at: 0 };
|
|
98
|
+
const sent = times > last.count && this.#now() - last.at >= OOM_QUIET_MS && post(oomText(times), { health: 'oom', times });
|
|
99
|
+
r.oom = sent ? { count: times, at: this.#now() } : { count: Math.min(times, last.count), at: last.at };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const state = (x) => JSON.stringify([x.disks ?? {}, x.memory ?? {}, x.oom ?? null]);
|
|
103
|
+
if (state(r) !== state(before)) this.#store.put(r);
|
|
104
|
+
return posted;
|
|
105
|
+
}
|
|
106
|
+
}
|
package/server/main.mjs
CHANGED
|
@@ -61,6 +61,7 @@ import { Updates } from './updates.mjs';
|
|
|
61
61
|
import { Certificate } from './certificate.mjs';
|
|
62
62
|
import { Operators } from './operators.mjs';
|
|
63
63
|
import { HealthReact } from './healthreact.mjs';
|
|
64
|
+
import { HealthDocket } from './healthdocket.mjs';
|
|
64
65
|
import { MachinesMove } from './machinesmove.mjs';
|
|
65
66
|
import { Wifi } from './wifi.mjs';
|
|
66
67
|
import { Binders } from './binders.mjs';
|
|
@@ -438,10 +439,15 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
|
|
|
438
439
|
/* And what its owner chose that it does on top (healthreact.mjs): its agent told, or a command run. */
|
|
439
440
|
const reacting = new HealthReact({ store: new Store(`${dataDir}/health-react.json`), say: (name, line) => wakeups.say(name, line), exec: (name, argv, options) => vms.exec(name, argv, options),
|
|
440
441
|
isAgent: (name) => all.list().find((c) => c.name === name)?.ai === true, event: (name, line) => talk.event(name, line) });
|
|
442
|
+
/* Where its chat is on, its agent is given an event when its disk or memory crosses a level (healthdocket.mjs);
|
|
443
|
+
not where its owner turned on its own reaction (above): one way of telling it, not two. */
|
|
444
|
+
const healthDocket = new HealthDocket({ store: new Store(`${dataDir}/health-docket.json`), dockets, reacting: (name) => reacting.settings(name).on });
|
|
445
|
+
health.forgotten = (name) => healthDocket.forget(name);
|
|
441
446
|
/* A machine's problems worth keeping in What happened: said once, until they went away. */
|
|
442
447
|
const problemsSaid = new Map();
|
|
443
448
|
health.judged = (name, record) => {
|
|
444
449
|
talk.machine(name, record); void reacting.judged(name, record).catch(() => {});
|
|
450
|
+
try { healthDocket.judged(name, record); } catch { /* its docket: nothing more */ }
|
|
445
451
|
const now = (record?.problems ?? []).filter((x) => x.level === 'bad' || (x.level === 'warn' && ['memory', 'disk'].includes(x.kind)));
|
|
446
452
|
const before = problemsSaid.get(name) ?? new Set();
|
|
447
453
|
for (const x of now) if (!before.has(x.key)) events.add({ kind: 'machine', text: `${name}: ${x.text}`, conversation: name });
|