clavarium 0.2.247-beta.1 → 0.2.248-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.
@@ -15,6 +15,8 @@ const MACHINE_MS = 180_000;
15
15
  const BACK_TRIES = 36;
16
16
  const BACK_EVERY_MS = 5_000;
17
17
  export const TRY_EVERY_MS = 20 * 3600_000;
18
+ /* In the data folder while it runs: the server's watch leaves Multipass alone (vmwatch.mjs, `paused`). */
19
+ export const MULTIPASS_UPDATING = 'multipass-updating';
18
20
 
19
21
  const first = (r) => String(r?.stderr || r?.stdout || '').trim().split('\n').filter(Boolean).pop()?.slice(0, 200) || `exit ${r?.code}`;
20
22
 
@@ -38,7 +40,7 @@ export function runningMachines(text) {
38
40
  return list.filter((m) => m?.state === 'Running' && /^clv-[a-z0-9-]+$/.test(m?.name ?? '')).map((m) => m.name);
39
41
  }
40
42
 
41
- export async function multipassTurn({ run, mode, inWindow, busy, state, save, log = () => {}, now = () => Date.now(), sleep = (ms) => new Promise((r) => { setTimeout(r, ms); }), multipass = '/snap/bin/multipass' }) {
43
+ export async function multipassTurn({ run, mode, inWindow, busy, state, save, log = () => {}, now = () => Date.now(), sleep = (ms) => new Promise((r) => { setTimeout(r, ms); }), multipass = '/snap/bin/multipass', mark = () => {} }) {
42
44
  const sh = (argv, timeoutMs = SNAP_MS) => Promise.resolve(run(argv, { timeoutMs })).catch((e) => ({ code: 1, stderr: e.message }));
43
45
  const list = await sh(['snap', 'list', 'multipass']);
44
46
  if (list.code !== 0) return 'no multipass here';
@@ -83,22 +85,27 @@ export async function multipassTurn({ run, mode, inWindow, busy, state, save, lo
83
85
  const running = seen.code === 0 ? runningMachines(seen.stdout) : null;
84
86
  if (!running) { log(`multipass: its machines could not be read: not updated (${first(seen)})`); return 'not updated'; }
85
87
 
86
- /* Written down BEFORE anything stops: a turn cut here still brings them back. */
87
- save({ multipassRefresh: { triedAt: new Date(now()).toISOString(), machines: running } });
88
- log(`multipass: a new version: ${running.length} machine(s) put to sleep for its update`);
89
- for (const name of running) {
90
- const r = await sh([multipass, 'suspend', name], MACHINE_MS);
91
- if (r.code !== 0) {
92
- log(`multipass: ${name} could not be put to sleep (${first(r)}): not updated, its machines started again`);
93
- save({ multipassRefresh: { ...state().multipassRefresh, machines: await back(running) } });
94
- return 'not updated';
88
+ /* Told to the server first (23-11, on a real server: machines going to sleep keep Multipass busy for minutes, the
89
+ server's watch took it for hung and restarted it in the middle). */
90
+ mark(true);
91
+ try {
92
+ /* Written down BEFORE anything stops: a turn cut here still brings them back. */
93
+ save({ multipassRefresh: { triedAt: new Date(now()).toISOString(), machines: running } });
94
+ log(`multipass: a new version: ${running.length} machine(s) put to sleep for its update`);
95
+ for (const name of running) {
96
+ const r = await sh([multipass, 'suspend', name], MACHINE_MS);
97
+ if (r.code !== 0) {
98
+ log(`multipass: ${name} could not be put to sleep (${first(r)}): not updated, its machines started again`);
99
+ save({ multipassRefresh: { ...state().multipassRefresh, machines: await back(running) } });
100
+ return 'not updated';
101
+ }
95
102
  }
96
- }
97
- /* --ignore-running: a client of Multipass still open (a terminal, a command) would make snap refuse (seen on a real machine); its machine is asleep. */
98
- const done = await sh(['snap', 'refresh', 'multipass', '--ignore-running'], REFRESH_MS);
99
- log(done.code === 0 ? 'multipass: updated' : `multipass: its update failed: ${first(done)}`);
100
- const left = await back(running);
101
- save({ multipassRefresh: { ...state().multipassRefresh, machines: left } });
102
- log(left.length ? `multipass: ${left.length} machine(s) to start again at the next turn` : 'multipass: its machines are running again');
103
- return done.code === 0 ? 'updated' : 'update failed';
103
+ /* --ignore-running: a client of Multipass still open (a terminal, a command) would make snap refuse (seen on a real machine); its machine is asleep. */
104
+ const done = await sh(['snap', 'refresh', 'multipass', '--ignore-running'], REFRESH_MS);
105
+ log(done.code === 0 ? 'multipass: updated' : `multipass: its update failed: ${first(done)}`);
106
+ const left = await back(running);
107
+ save({ multipassRefresh: { ...state().multipassRefresh, machines: left } });
108
+ log(left.length ? `multipass: ${left.length} machine(s) to start again at the next turn` : 'multipass: its machines are running again');
109
+ return done.code === 0 ? 'updated' : 'update failed';
110
+ } finally { mark(false); }
104
111
  }
@@ -56,7 +56,7 @@ import { fileURLToPath } from 'node:url';
56
56
  import { fetchToolArchives, REGISTRY } from './parley-archive.mjs';
57
57
  import { ensureTunnel, tunnelUnit, relayOfUnit, TUNNEL_UNIT, TUNNEL_PROGRAM } from './tunnel.mjs';
58
58
  import { ensureGenericClientId } from './oidc.mjs';
59
- import { multipassTurn } from './multipass-refresh.mjs';
59
+ import { multipassTurn, MULTIPASS_UPDATING } from './multipass-refresh.mjs';
60
60
  /* The updater's run keeps the tunnel up to date: the same functions, from here too. */
61
61
  export { ensureTunnel, tunnelUnit, relayOfUnit, TUNNEL_UNIT, TUNNEL_PROGRAM };
62
62
 
@@ -1128,7 +1128,7 @@ async function main(argv) {
1128
1128
  const file = join(PATHS.stateDir, 'multipass-refresh.json');
1129
1129
  const state = () => { try { return JSON.parse(readFileSync(file, 'utf8')); } catch { return {}; } };
1130
1130
  const save = (change) => { writeFileSync(`${file}.new`, `${JSON.stringify({ ...state(), ...change })}\n`); renameSync(`${file}.new`, file); };
1131
- await multipassTurn({ run: runCommand, mode: conf.MODE, inWindow: inWindow(conf.WINDOW, new Date()), busy: () => u.busy(), state, save, log: (line) => console.error(`updater: ${line}`) });
1131
+ await multipassTurn({ run: runCommand, mode: conf.MODE, inWindow: inWindow(conf.WINDOW, new Date()), busy: () => u.busy(), state, save, log: (line) => console.error(`updater: ${line}`), mark: (on) => { const f = join(PATHS.data, MULTIPASS_UPDATING); if (on) writeFileSync(f, `${new Date().toISOString()}\n`, { mode: 0o644 }); else rmSync(f, { force: true }); } });
1132
1132
  } catch (error) { console.error(`updater: Multipass could not be looked at: ${error.message}`); }
1133
1133
  const r = await u.runOnce(); console.log(typeof r === 'string' ? r : 'done'); return; }
1134
1134
  case 'set': if (rest.length !== 2) throw new Refused('set <mode|channel|window|port> <value>'); return print(u.set(rest[0], rest[1]));
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.247-beta.1",
3
+ "version": "0.2.248-beta.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.247-beta.1",
9
+ "version": "0.2.248-beta.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.247-beta.1",
3
+ "version": "0.2.248-beta.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
package/server/main.mjs CHANGED
@@ -14,7 +14,7 @@ import { Catalogue } from './catalog.mjs';
14
14
  * node server/main.mjs --command "your-tool" --cwd /path/to/work
15
15
  * no VM: one tool on this machine, at /?c=<name>. For development.
16
16
  */
17
- import { readFileSync, readdirSync } from 'node:fs';
17
+ import { readFileSync, readdirSync, statSync } from 'node:fs';
18
18
  import { createHash } from 'node:crypto';
19
19
  import { createServer } from 'node:http';
20
20
  import { readFile } from 'node:fs/promises';
@@ -767,7 +767,7 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
767
767
  may say otherwise: set by hand, or by an earlier version). Not waited for. */
768
768
  void all.reconcileAll?.().catch(() => {});
769
769
  /* The VM manager, watched: said to the pages when it does not answer, restarted once when stuck (vmwatch.mjs). */
770
- const vmWatch = new VmWatch({ run: asHost, store: new Store(`${dataDir}/vm-manager.json`), alert: (payload) => { events.add({ kind: 'server', text: `${payload.title}: ${payload.body ?? ''}`.trim() }); void alerts.tell(payload)?.catch?.(() => {}); } });
770
+ const vmWatch = new VmWatch({ paused: () => { try { return Date.now() - statSync(`${dataDir}/multipass-updating`).mtimeMs < 45 * 60_000; } catch { return false; } }, run: asHost, store: new Store(`${dataDir}/vm-manager.json`), alert: (payload) => { events.add({ kind: 'server', text: `${payload.title}: ${payload.body ?? ''}`.trim() }); void alerts.tell(payload)?.catch?.(() => {}); } });
771
771
  const stopVmWatch = vmWatch.start(vmWatchEveryMs);
772
772
  const platform = { get machines() { return vmWatch.ok; }, answerer: answerer.about(), get operators() { return team ? team.allowed() : [...operators]; }, address: publicOrigin ?? null, version: JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version, get dictation() { return voice.current() ? (voice.model ?? 'set') : null; } };
773
773
  /* Who gets what, and a whole setup in one file: see setup.mjs. */
@@ -40,11 +40,11 @@ export function statesOf(csv) {
40
40
  }
41
41
 
42
42
  export class VmWatch {
43
- #run; #store; #now; #hour; #log; #alert; #stuckMs; #reviveEveryMs; #downSince = null; #reviving = null; #running = new Set(); #wait;
43
+ #run; #store; #now; #hour; #log; #alert; #stuckMs; #reviveEveryMs; #downSince = null; #reviving = null; #running = new Set(); #wait; #paused;
44
44
  ok = true;
45
45
  /* `hour()`: this server's local hour, 0-23 (injected for tests). */
46
- constructor({ run, store, now = () => Date.now(), hour = () => new Date().getHours(), log = (line) => console.error(line), alert = () => {}, stuckMs = STUCK_MS, reviveEveryMs = REVIVE_EVERY_MS, wait = (ms) => new Promise((r) => { setTimeout(r, ms).unref?.(); }) }) {
47
- this.#wait = wait;
46
+ constructor({ run, store, now = () => Date.now(), hour = () => new Date().getHours(), log = (line) => console.error(line), alert = () => {}, stuckMs = STUCK_MS, reviveEveryMs = REVIVE_EVERY_MS, wait = (ms) => new Promise((r) => { setTimeout(r, ms).unref?.(); }), paused = () => false }) {
47
+ this.#wait = wait; this.#paused = paused;
48
48
  this.#run = run; this.#store = store; this.#now = now; this.#hour = hour; this.#log = log; this.#alert = alert; this.#stuckMs = stuckMs; this.#reviveEveryMs = reviveEveryMs;
49
49
  }
50
50
 
@@ -70,6 +70,8 @@ export class VmWatch {
70
70
 
71
71
  /* One look: 'ok', 'down', 'down; restarted already', 'asked', 'down; asked already', 'revived' or 'revive failed'. */
72
72
  async look() {
73
+ /* Its update runs, in the update window (deploy/multipass-refresh.mjs): busy on purpose, it brings its machines back itself. */
74
+ if (this.#paused()) { this.#downSince = null; return 'paused'; }
73
75
  const r = await this.#run(['vm', 'ping'], { timeoutMs: PING_MS }).catch((e) => ({ code: 1, stderr: e.message }));
74
76
  if (r.code === 0) {
75
77
  const was = !this.ok;