clavarium 0.2.200-beta.1 → 0.2.201-beta.1
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/conversations.mjs +39 -15
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.201-beta.1",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "clavarium",
|
|
9
|
-
"version": "0.2.
|
|
9
|
+
"version": "0.2.201-beta.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/conversations.mjs
CHANGED
|
@@ -21,6 +21,8 @@ import { join } from 'node:path';
|
|
|
21
21
|
export const SESSION = 'main';
|
|
22
22
|
export const STOP_DELAY_MS = 60_000;
|
|
23
23
|
const STEP_MS = 15 * 60_000;
|
|
24
|
+
/* A copy reading its original's memory: nothing for this long, and it is ended (and why said). */
|
|
25
|
+
export const COPY_QUIET_MS = Number(process.env.CLAVARIUM_COPY_QUIET_MS) || 3 * 60_000;
|
|
24
26
|
/* A size in words: 840 KB, 12 MB, 1.4 GB. */
|
|
25
27
|
const sizeWords = (bytes) => (bytes >= 1024 ** 3 ? `${(bytes / 1024 ** 3).toFixed(1)} GB` : bytes >= 1024 ** 2 ? `${Math.round(bytes / 1024 ** 2)} MB` : `${Math.round(bytes / 1024)} KB`);
|
|
26
28
|
/* The door of a conversation in a shared machine (#221), put in the machine by the server. */
|
|
@@ -317,7 +319,8 @@ export class Conversations {
|
|
|
317
319
|
return there.map((x) => x.machineMb).find(Number.isInteger) ?? Math.max(...there.map((x) => Number(x.memoryMb) || 0));
|
|
318
320
|
}
|
|
319
321
|
|
|
320
|
-
|
|
322
|
+
/* `after`: a promise its build waits for (a copy reads its original first); resolving false, no build. */
|
|
323
|
+
create({ name: given, title: read, tool, memoryMb, cpus, diskGb, workOnDisk, group, machine, machineMb, after }) {
|
|
321
324
|
const { name, title } = this.#naming({ name: given, title: read });
|
|
322
325
|
/* IN A SHARED MACHINE (#221), named: its work is always kept here (mounted into its place there). */
|
|
323
326
|
if (machine !== undefined && (typeof machine !== 'string' || !isConversationName(machine))) throw new Refusal(400, 'A machine name is 1 to 32 lower-case letters, digits or hyphens; it starts with a letter.');
|
|
@@ -351,7 +354,7 @@ export class Conversations {
|
|
|
351
354
|
const { order, groups } = this.#order();
|
|
352
355
|
this.#store.put(conversation);
|
|
353
356
|
this.#keepOrder({ order: [name, ...order], groups: group && !groups.includes(group) ? [...groups, group] : groups });
|
|
354
|
-
this.built = this.#build(name, chosen, memory);
|
|
357
|
+
this.built = after === undefined ? this.#build(name, chosen, memory) : Promise.resolve(after).then((go) => (go === false || !this.#store.get(name) ? undefined : this.#build(name, chosen, memory)));
|
|
355
358
|
return conversation;
|
|
356
359
|
}
|
|
357
360
|
|
|
@@ -1010,30 +1013,26 @@ export class Conversations {
|
|
|
1010
1013
|
const memory = shared ? Math.max(tool.memoryMb, Math.min(Number(c.memoryMb) || tool.memoryMb, Number(size) || SHARED_MB)) : Number(c.memoryMb) || tool.memoryMb;
|
|
1011
1014
|
const label = typeof title === 'string' && title.trim() ? title.trim() : `${c.title ?? c.name} (copy)`;
|
|
1012
1015
|
await this.admit({ title: label, tool: c.tool, memoryMb: memory, ...(shared ? { machine, machineMb } : {}) });
|
|
1013
|
-
|
|
1016
|
+
/* Its tool's memory is read FIRST, its original then put back to sleep, and only then its machine built: the
|
|
1017
|
+
two never run at once (14-11: a new 4 GB machine and the woken original together starved the server). */
|
|
1018
|
+
let open; const gate = new Promise((ok) => { open = ok; });
|
|
1019
|
+
const made = this.create({ title: label, tool: c.tool, memoryMb: memory, ...(shared ? { machine, machineMb } : { cpus: c.cpus, diskGb: c.diskGb }), ...(c.group ? { group: c.group } : {}), after: gate });
|
|
1014
1020
|
/* Before its build can end (it awaits first): held, never usable half made. */
|
|
1015
1021
|
this.#holding.add(made.name);
|
|
1016
1022
|
this.#store.patch(made.name, { copyOf: name });
|
|
1017
1023
|
const building = this.built;
|
|
1018
|
-
this.copied = this.#copying(name, made.name, c, tool, building);
|
|
1024
|
+
this.copied = this.#copying(name, made.name, c, tool, building, open);
|
|
1019
1025
|
return this.#store.get(made.name);
|
|
1020
1026
|
}
|
|
1021
1027
|
|
|
1022
|
-
async #copying(from, to, c, tool, building) {
|
|
1028
|
+
async #copying(from, to, c, tool, building, open = () => {}) {
|
|
1023
1029
|
const dir = mkdtempSync(join(tmpdir(), 'clv-copy-'));
|
|
1024
1030
|
const carried = join(dir, 'state.tar.gz');
|
|
1025
1031
|
let woke = false;
|
|
1026
1032
|
try {
|
|
1027
|
-
await building;
|
|
1028
|
-
const built = this.#store.get(to);
|
|
1029
|
-
if (!built) return undefined; /* erased meanwhile */
|
|
1030
|
-
if (built.phase === 'failed') throw new Error(built.error ?? 'its machine could not be made.');
|
|
1031
1033
|
/* Said as it goes, once a second at most: a long copy is seen moving (13-11, "on ne sait pas si ça marche"). */
|
|
1032
1034
|
let said = 0;
|
|
1033
1035
|
const say = (step) => { const now = Date.now(); if (now - said < 1000) return; said = now; if (this.#store.get(to)) this.#store.patch(to, { step }); };
|
|
1034
|
-
this.#store.patch(to, { step: 'Copying its work' });
|
|
1035
|
-
const work = await this.copyWork(from, to, (p) => say(`Copying its work: ${p.files} file${p.files === 1 ? '' : 's'}, ${sizeWords(p.bytes)}`));
|
|
1036
|
-
if (work?.files !== undefined) this.#store.patch(to, { step: `Copying its work: ${work.files} file${work.files === 1 ? '' : 's'}, ${sizeWords(work.bytes)}, done` });
|
|
1037
1036
|
if (tool.state) {
|
|
1038
1037
|
this.#store.patch(to, { step: `Copying ${tool.label}'s memory` });
|
|
1039
1038
|
const state = await this.#vms.state(from, { fresh: true }).catch(() => undefined);
|
|
@@ -1057,16 +1056,40 @@ export class Conversations {
|
|
|
1057
1056
|
this.#store.patch(to, { step: `Copying ${tool.label}'s memory` });
|
|
1058
1057
|
const out = await this.exportState(from);
|
|
1059
1058
|
child = out.child;
|
|
1060
|
-
let read = 0;
|
|
1061
|
-
out.stream.on('data', (d) => { read += d.length; say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read`); });
|
|
1059
|
+
let read = 0; let lastAt = Date.now(); let stalled = false;
|
|
1060
|
+
out.stream.on('data', (d) => { read += d.length; lastAt = Date.now(); say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read`); });
|
|
1061
|
+
/* Nothing comes for a while: said, with how long; for too long, the reading is ended and why said --
|
|
1062
|
+
never an endless wait nobody can read (14-11: stuck at "36 KB read"). */
|
|
1063
|
+
const watch = setInterval(() => {
|
|
1064
|
+
const quiet = Math.round((Date.now() - lastAt) / 1000);
|
|
1065
|
+
if (quiet >= COPY_QUIET_MS / 1000) { stalled = true; child.kill('SIGKILL'); return; }
|
|
1066
|
+
if (quiet >= 15) say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read, nothing more for ${quiet} s`);
|
|
1067
|
+
}, 5000);
|
|
1062
1068
|
const failed = new Promise((ok) => child.on('error', () => ok(-1)));
|
|
1063
1069
|
const file = createWriteStream(carried, { mode: 0o600 });
|
|
1064
1070
|
out.stream.pipe(file);
|
|
1065
|
-
code = await Promise.race([new Promise((ok) => child.on('close', ok)), failed]);
|
|
1071
|
+
code = await Promise.race([new Promise((ok) => child.on('close', ok)), failed]).finally(() => clearInterval(watch));
|
|
1072
|
+
if (stalled) {
|
|
1073
|
+
const now = await this.#vms.state(from, { fresh: true }).catch(() => undefined);
|
|
1074
|
+
throw new Error(`its original stopped sending its tool's memory (${sizeWords(read)} read, then nothing for ${COPY_QUIET_MS / 60_000} min; its machine is ${now ?? 'in a state that could not be read'})`);
|
|
1075
|
+
}
|
|
1066
1076
|
await new Promise((ok) => (file.writableFinished ? ok() : file.on('finish', ok)));
|
|
1067
1077
|
if (code !== 255) break;
|
|
1068
1078
|
}
|
|
1069
1079
|
if (code !== 0) throw new Error(`its tool's memory could not be read (exit ${code}${firstLine(child.said) ? `: ${firstLine(child.said)}` : ''}).`);
|
|
1080
|
+
/* Read: its original back to sleep at once, before the copy's machine is made. */
|
|
1081
|
+
if (woke) { await this.sleep(from).catch(() => {}); woke = false; }
|
|
1082
|
+
this.#borrowed.delete(from);
|
|
1083
|
+
}
|
|
1084
|
+
open(true);
|
|
1085
|
+
await building;
|
|
1086
|
+
const built = this.#store.get(to);
|
|
1087
|
+
if (!built) return undefined; /* erased meanwhile */
|
|
1088
|
+
if (built.phase === 'failed') throw new Error(built.error ?? 'its machine could not be made.');
|
|
1089
|
+
this.#store.patch(to, { step: 'Copying its work' });
|
|
1090
|
+
const work = await this.copyWork(from, to, (p) => say(`Copying its work: ${p.files} file${p.files === 1 ? '' : 's'}, ${sizeWords(p.bytes)}`));
|
|
1091
|
+
if (work?.files !== undefined) this.#store.patch(to, { step: `Copying its work: ${work.files} file${work.files === 1 ? '' : 's'}, ${sizeWords(work.bytes)}, done` });
|
|
1092
|
+
if (tool.state) {
|
|
1070
1093
|
if ((await this.#vms.state(to, { fresh: true }).catch(() => undefined)) !== 'Running') {
|
|
1071
1094
|
const started = await this.#vms.start(to);
|
|
1072
1095
|
if (started.code !== 0) throw new Error(`its machine did not start: ${firstLine(started.stderr) || `exit ${started.code}`}.`);
|
|
@@ -1082,6 +1105,7 @@ export class Conversations {
|
|
|
1082
1105
|
this.ready(to);
|
|
1083
1106
|
return undefined;
|
|
1084
1107
|
} catch (error) {
|
|
1108
|
+
open(false);
|
|
1085
1109
|
this.#holding.delete(to);
|
|
1086
1110
|
if (this.#store.get(to)) this.#store.patch(to, { phase: 'failed', step: undefined, progress: undefined, buildingSince: undefined, error: `Not copied: ${String(error?.message ?? error).replace(/\.?$/, '.')} Its original is untouched; erase this copy.` });
|
|
1087
1111
|
return undefined;
|