clavarium 0.2.436-next.1 → 0.2.438-next.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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.436-next.1",
3
+ "version": "0.2.438-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.436-next.1",
9
+ "version": "0.2.438-next.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.436-next.1",
3
+ "version": "0.2.438-next.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.",
@@ -14,9 +14,9 @@ import { resumeOrFresh } from './tools.mjs';
14
14
  import { TOOLS_PATH, LAUNCH } from './toolbox.mjs';
15
15
  import { availableParallelism } from 'node:os';
16
16
  import { spawn } from 'node:child_process';
17
- import { readFileSync, createReadStream, createWriteStream, mkdtempSync, rmSync } from 'node:fs';
17
+ import { readFileSync, createReadStream, createWriteStream, mkdtempSync, rmSync, statSync, copyFileSync, mkdirSync } from 'node:fs';
18
18
  import { tmpdir } from 'node:os';
19
- import { join } from 'node:path';
19
+ import { join, dirname } from 'node:path';
20
20
 
21
21
  export const SESSION = 'main';
22
22
  export const STOP_DELAY_MS = 60_000;
@@ -938,7 +938,10 @@ export class Conversations {
938
938
  from its machine, a manifest first (clavarium-conversation.json: its tool, its name, its size). Nothing
939
939
  of the tool is known here: any tool that says where its state is can be exported, imported, moved.
940
940
  The machine must run (its home is in it). -> { name: the file's name, stream } */
941
- async exportState(name) {
941
+ /* `toWork` (14-11): the archive written into its own work folder (/work/.clavarium-carry/<toWork>.tgz), that
942
+ the server reads on its side, rather than sent out through the VM manager's output -- which stalled for good
943
+ past ~36 KB on the owner's server. Its output is then empty. */
944
+ async exportState(name, { toWork } = {}) {
942
945
  const c = this.#store.get(name);
943
946
  if (!c) throw new Refusal(404, 'No such conversation.');
944
947
  const tool = this.#tools.find((t) => t.id === c.tool);
@@ -949,13 +952,15 @@ export class Conversations {
949
952
  (a path it has not made yet left out), the cache left out. */
950
953
  /* (Its paths are plain -- letters, digits, . _ / - -- checked by tools.mjs: given as words.) */
951
954
  /* A reading of an earlier try that never ended (14-11: three left, each holding its pipe): ended first. */
952
- const script = 'for p in $(pgrep -f "^tar -czf - --warning=no-file-changed -C /tmp/"); do kill $p 2>/dev/null; done; m=$(mktemp -d) || exit 2; printf "%s" "$1" > "$m/clavarium-conversation.json"; '
955
+ const script = 'for p in $(pgrep -f "^tar -czf .* clavarium-conversation.json"); do kill $p 2>/dev/null; done; m=$(mktemp -d) || exit 2; printf "%s" "$1" > "$m/clavarium-conversation.json"; '
953
956
  + 'ex=""; for a in $2; do ex="$ex --exclude=./$a"; done; ks=""; for a in $3; do if [ -e "$HOME/$a" ]; then ks="$ks ./$a"; fi; done; '
954
- + 'rc=0; tar -czf - --warning=no-file-changed -C "$m" clavarium-conversation.json -C "$HOME" $ex $ks || rc=$?; rm -rf "$m"; '
957
+ + 'out=-; if [ -n "$4" ]; then mkdir -p /work/.clavarium-carry && out="/work/.clavarium-carry/$4.tgz"; fi; '
958
+ + 'rc=0; tar -czf "$out" --warning=no-file-changed -C "$m" clavarium-conversation.json -C "$HOME" $ex $ks || rc=$?; rm -rf "$m"; '
955
959
  /* 1: a file changed while read (its tool writing its log as it is read, 14-11: a live Claude Code): the
956
960
  archive is whole all the same -- what was there, read; only a real error (2) fails. */
957
961
  + '[ "$rc" -le 1 ]';
958
- const argv = this.#vms.execArgv(name, ['sh', '-c', script, 'export', manifest, tool.state.skip.join(' '), tool.state.keep.join(' ')]);
962
+ const id = typeof toWork === 'string' && /^[a-z0-9-]{1,64}$/.test(toWork) ? toWork : '';
963
+ const argv = this.#vms.execArgv(name, ['sh', '-c', script, 'export', manifest, tool.state.skip.join(' '), tool.state.keep.join(' '), id]);
959
964
  const child = spawn(argv[0], argv.slice(1), { stdio: ['ignore', 'pipe', 'pipe'] });
960
965
  /* What it said when it failed: kept short, for the message that says why (a copy, a move). */
961
966
  child.said = '';
@@ -977,19 +982,35 @@ export class Conversations {
977
982
 
978
983
  /* An exported state unpacked into its home (its tool stopped by the caller): its manifest checked, then each
979
984
  part put in place of the old. -> the manifest's { name, at }, or null. */
985
+ /* `input`: a stream, sent through the VM manager's input; or the path of a file here -- then, when it has a work
986
+ folder here, put there and read from it in its machine (14-11: the VM manager's channel failed past a few tens
987
+ of KB on the owner's server, both ways). */
980
988
  async #unpack(name, toolId, input) {
989
+ let viaWork = '';
990
+ let source = input;
991
+ const workDir = typeof input === 'string' && this.#store.get(name)?.work === 'kept' && typeof this.workFolder === 'function' ? this.workFolder(name) : '';
992
+ if (workDir) {
993
+ const id = `in-${Date.now()}`;
994
+ viaWork = join(workDir, '.clavarium-carry', `${id}.tgz`);
995
+ mkdirSync(dirname(viaWork), { recursive: true });
996
+ copyFileSync(input, viaWork);
997
+ source = id;
998
+ } else if (typeof input === 'string') source = createReadStream(input);
999
+ try { return await this.#unpackFrom(name, toolId, source, Boolean(viaWork)); } finally { if (viaWork) rmSync(viaWork, { force: true }); }
1000
+ }
1001
+ async #unpackFrom(name, toolId, input, fromWork) {
981
1002
  /* Unpacked aside first: its manifest read, its tool checked; then moved into the home. */
982
1003
  /* Aside in the home itself (never /tmp, which may be memory): then each part moved in place of the old. */
983
- const script = 'set -e; m=$(mktemp -d "$HOME/.clavarium-import.XXXXXX"); trap \'rm -rf "$m"\' EXIT; tar -xzf - -C "$m"; '
1004
+ const script = 'set -e; m=$(mktemp -d "$HOME/.clavarium-import.XXXXXX"); trap \'rm -rf "$m"\' EXIT; if [ -n "$2" ]; then tar -xzf "/work/.clavarium-carry/$2.tgz" -C "$m"; else tar -xzf - -C "$m"; fi; '
984
1005
  + 'grep -q "\\"kind\\":\\"clavarium-conversation\\"" "$m/clavarium-conversation.json" || { echo "not an exported conversation" >&2; exit 3; }; '
985
1006
  + 'grep -q "\\"tool\\":\\"$1\\"" "$m/clavarium-conversation.json" || { echo "another tool" >&2; exit 4; }; '
986
1007
  + 'cat "$m/clavarium-conversation.json"; rm -f "$m/clavarium-conversation.json"; '
987
1008
  + 'for e in "$m"/.[!.]* "$m"/*; do if [ ! -e "$e" ]; then continue; fi; b=$(basename "$e"); rm -rf "$HOME/$b"; mv "$e" "$HOME/$b"; done';
988
- const argv = this.#vms.execArgv(name, ['sh', '-c', script, 'import', toolId]);
1009
+ const argv = this.#vms.execArgv(name, ['sh', '-c', script, 'import', toolId, fromWork ? input : '']);
989
1010
  const child = spawn(argv[0], argv.slice(1), { stdio: ['pipe', 'pipe', 'pipe'] });
990
1011
  const out = []; const err = [];
991
1012
  child.stdout.on('data', (d) => out.push(d)); child.stderr.on('data', (d) => err.push(d));
992
- input.pipe(child.stdin);
1013
+ if (fromWork) child.stdin.end(); else input.pipe(child.stdin);
993
1014
  child.stdin.on('error', () => { /* it stopped reading: said below */ });
994
1015
  const code = await new Promise((ok) => child.on('close', ok));
995
1016
  if (code === 3) throw new Refusal(400, 'This is not an exported conversation.');
@@ -1065,10 +1086,19 @@ export class Conversations {
1065
1086
  for (let attempt = 0; attempt < 2 && code !== 0; attempt++) {
1066
1087
  if (attempt) await new Promise((ok) => setTimeout(ok, 10_000));
1067
1088
  this.#store.patch(to, { step: `Copying ${tool.label}'s memory` });
1068
- const out = await this.exportState(from);
1089
+ /* Through its work folder when there is one here (14-11): its folders put back first -- a machine
1090
+ started outside an opening may have them loose -- then its archive written there, its size read
1091
+ on this side as it grows. Else, as before, through the VM manager's output. */
1092
+ const workDir = c.work === 'kept' && typeof this.workFolder === 'function' ? this.workFolder(from) : '';
1093
+ const carryId = workDir ? `copy-${to}-${Date.now()}` : '';
1094
+ const inWork = carryId ? join(workDir, '.clavarium-carry', `${carryId}.tgz`) : '';
1095
+ if (inWork) await Promise.race([Promise.resolve().then(() => this.mounted(from, true)).catch(() => {}), new Promise((r) => { setTimeout(r, MOUNTED_MS).unref?.(); })]);
1096
+ const out = await this.exportState(from, inWork ? { toWork: carryId } : {});
1069
1097
  child = out.child;
1070
1098
  let read = 0; let lastAt = Date.now(); let stalled = false;
1071
- out.stream.on('data', (d) => { read += d.length; lastAt = Date.now(); say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read`); });
1099
+ const grew = (n) => { if (n <= read) return; read = n; lastAt = Date.now(); say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read`); };
1100
+ if (inWork) out.stream.resume(); else out.stream.on('data', (d) => grew(read + d.length));
1101
+ const sizing = inWork ? setInterval(() => { try { grew(statSync(inWork).size); } catch { /* not there yet */ } }, 1000) : null;
1072
1102
  /* Nothing comes for a while: said, with how long; for too long, the reading is ended and why said --
1073
1103
  never an endless wait nobody can read (14-11: stuck at "36 KB read"). */
1074
1104
  /* Quiet for 30 s: its machine asked, once, what its reading does -- the file it is on and its state
@@ -1090,14 +1120,18 @@ export class Conversations {
1090
1120
  if (quiet >= 15) say(`Copying ${tool.label}'s memory: ${sizeWords(read)} read, nothing more for ${quiet} s`);
1091
1121
  }, 5000);
1092
1122
  const failed = new Promise((ok) => child.on('error', () => ok(-1)));
1093
- const file = createWriteStream(carried, { mode: 0o600 });
1094
- out.stream.pipe(file);
1095
- code = await Promise.race([new Promise((ok) => child.on('close', ok)), failed]).finally(() => clearInterval(watch));
1123
+ const file = inWork ? null : createWriteStream(carried, { mode: 0o600 });
1124
+ if (file) out.stream.pipe(file);
1125
+ code = await Promise.race([new Promise((ok) => child.on('close', ok)), failed]).finally(() => { clearInterval(watch); if (sizing) clearInterval(sizing); });
1126
+ /* Written in its work folder: taken from there, and the folder left as it was. */
1127
+ if (inWork) {
1128
+ try { if (code === 0 && !stalled) { copyFileSync(inWork, carried); grew(statSync(carried).size); } } finally { rmSync(inWork, { force: true }); }
1129
+ }
1096
1130
  if (stalled) {
1097
1131
  const now = await this.#vms.state(from, { fresh: true }).catch(() => undefined);
1098
1132
  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 ${now ?? 'unreadable'} (see its job's log in Settings)`);
1099
1133
  }
1100
- await new Promise((ok) => (file.writableFinished ? ok() : file.on('finish', ok)));
1134
+ if (file) await new Promise((ok) => (file.writableFinished ? ok() : file.on('finish', ok)));
1101
1135
  if (code !== 255) break;
1102
1136
  }
1103
1137
  if (code !== 0) throw new Error(`its tool's memory could not be read (exit ${code}${firstLine(child.said) ? `: ${firstLine(child.said)}` : ''}).`);
@@ -1120,7 +1154,7 @@ export class Conversations {
1120
1154
  if (started.code !== 0) throw new Error(`its machine did not start: ${firstLine(started.stderr) || `exit ${started.code}`}.`);
1121
1155
  }
1122
1156
  this.#store.patch(to, { step: `Putting ${tool.label}'s memory in the copy` });
1123
- await this.#unpack(to, c.tool, createReadStream(carried));
1157
+ await this.#unpack(to, c.tool, carried);
1124
1158
  }
1125
1159
  this.#store.patch(to, { step: 'Giving it the same accesses' });
1126
1160
  await this.copyRights(from, to);
@@ -1214,7 +1248,7 @@ export class Conversations {
1214
1248
  /* Not opened while its state is put back: building still, to the end. */
1215
1249
  this.#store.patch(name, { phase: 'building', step: `Putting ${tool.label}'s state back` });
1216
1250
  this.drop(name);
1217
- try { await this.#unpack(name, c.tool, createReadStream(carried)); } catch (error) {
1251
+ try { await this.#unpack(name, c.tool, carried); } catch (error) {
1218
1252
  this.#store.patch(name, { phase: 'ready', step: undefined });
1219
1253
  this.ready(name);
1220
1254
  this.renewed(name, `Moved to the shared machine ${machine}, but ${tool.label}'s state did not come along (${error.message}): it starts afresh. Its old machine is kept, stopped.`, true);
@@ -8,7 +8,7 @@
8
8
  import { cp, lstat } from 'node:fs/promises';
9
9
  import { relative, sep } from 'node:path';
10
10
 
11
- export const NOT_COPIED = Object.freeze(['.docket', '.tether', '.binder', '.parley/pulse.json', '.parley/speakers.json', '.parley/restart-asked', '.parley/server.mjs']);
11
+ export const NOT_COPIED = Object.freeze(['.docket', '.tether', '.binder', '.clavarium-carry', '.parley/pulse.json', '.parley/speakers.json', '.parley/restart-asked', '.parley/server.mjs']);
12
12
 
13
13
  /* Whether a path of the work folder (relative, '' for the folder itself) is copied. */
14
14
  export function copied(path) {
package/server/main.mjs CHANGED
@@ -674,6 +674,8 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
674
674
  /* A COPY (conversations.copy): its work folder from its original's, host side -- what is its alone left out
675
675
  (its reminders, its pending asks to the keepers, its devices, its chat's live files) -- then the same accesses. */
676
676
  all.copyWork = (from, to, onProgress) => copyWorkFolder(folders.workFolder(from), folders.workFolder(to), onProgress);
677
+ /* Where a conversation's work folder is on this side: a copy reads its tool's memory through it (14-11). */
678
+ all.workFolder = (name) => folders.workFolder(name);
677
679
  all.copyRights = async (from, to) => {
678
680
  for (const r of folders.of(from)) {
679
681
  if (r.access === 'write') gitKeeper?.beforeWriter(r.name, to, true);