clavarium 0.2.226-beta.1 → 0.2.227-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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.226-beta.1",
3
+ "version": "0.2.227-beta.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.226-beta.1",
9
+ "version": "0.2.227-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.226-beta.1",
3
+ "version": "0.2.227-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
@@ -562,7 +562,9 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
562
562
  ...(rereadAfterMs === undefined ? {} : { rereadAfterMs }),
563
563
  /* A line typed down the terminal already open when there is one, and
564
564
  what is already known of a machine used instead of asking (talk.mjs). */
565
- terminal: { open: (name) => bridge?.open(name) ?? false, type: (name, line, ms) => bridge?.type(name, line, ms) ?? false, droppedAt: (name) => bridge?.droppedAt(name) },
565
+ /* `settled` (17-11): how long its terminal has been open and quiet -- without it, the line after a restart
566
+ was typed REREAD_AFTER_MS after, into a tool maybe still loading (talk.mjs, READY). */
567
+ terminal: { open: (name) => bridge?.open(name) ?? false, type: (name, line, ms) => bridge?.type(name, line, ms) ?? false, droppedAt: (name) => bridge?.droppedAt(name), settled: (name) => bridge?.settled(name) ?? null },
566
568
  health: { of: (name) => health?.of(name) ?? null },
567
569
  /* A secret pasted in a message is lifted out before the message is kept:
568
570
  it goes to the short-lived vault and the message keeps only a marker
package/server/talk.mjs CHANGED
@@ -17,7 +17,8 @@
17
17
  * until the tool has read, never while somebody
18
18
  * types there, never into a machine that does not
19
19
  * run (the message waits, and the line is typed
20
- * when it runs again).
20
+ * when it runs again); still unread a while
21
+ * after, typed again by itself (RENUDGE_MS).
21
22
  * agent-asked, waiting the owner's devices are alerted.
22
23
  * agent-showed a file under /work or in an attached folder is
23
24
  * opened from the page; any other is said to be
@@ -116,15 +117,27 @@ export const HEALTH_KNOWN_MS = 150_000;
116
117
  export const enterCommand = () => ['tmux', '-L', SOCKET, 'send-keys', '-t', PANE, 'Enter'];
117
118
  /* Escape, alone: what the Esc key of the page's key row sends -- a tool's current turn stopped (an API key that may act, apikeys.mjs). */
118
119
  export const escapeCommand = () => ['tmux', '-L', SOCKET, 'send-keys', '-t', PANE, 'Escape'];
120
+ /* A PANE SCROLLED BACK IS IN COPY MODE (`mouse on`: the wheel of any screen puts it there, and it stays
121
+ there when that screen goes away): the keys of a line typed then are copy-mode commands, the line never
122
+ reaches the tool and its Enter only leaves the mode (tried with tmux 3.4). The mode is left first, in
123
+ the same command -- nothing is done to a pane that is not in a mode (no shell: -F reads a format). */
124
+ const LEAVE_MODE = ['if-shell', '-F', '-t', PANE, '#{pane_in_mode}', `send-keys -t ${PANE} -X cancel`, ';'];
119
125
  export function typeCommand(line) {
120
126
  const text = line.endsWith(';') ? `${line.slice(0, -1)}\\;` : line;
121
- return ['tmux', '-L', SOCKET, 'send-keys', '-t', PANE, '-l', '--', text, ';', 'run-shell', '-d', String(ENTER_AFTER_MS / 1000), ';', 'send-keys', '-t', PANE, 'Enter'];
127
+ return ['tmux', '-L', SOCKET, ...LEAVE_MODE, 'send-keys', '-t', PANE, '-l', '--', text, ';', 'run-shell', '-d', String(ENTER_AFTER_MS / 1000), ';', 'send-keys', '-t', PANE, 'Enter'];
122
128
  }
129
+ /* Where the line is in typeCommand's argument vector. */
130
+ export const TYPED_AT = 3 + LEAVE_MODE.length + 5;
123
131
  /* Added to Parley's invitation, this server's own sentence: a branch can be
124
132
  checked out in one folder only, and /tmp is emptied when the machine
125
133
  restarts, with the work left there. Typed in a terminal: no quote, no
126
134
  backquote, nothing a shell would run. */
127
135
  export const BRANCHES = 'Check out your branches in the main folder of the repository or under /work, never in /tmp: a branch can be checked out in one folder only, and /tmp is emptied when the machine restarts. The checks before sending run in a copy made at exactly the commit to send (CLAVARIUM_CHECK_COPY, CLAVARIUM_CHECK_HEAD): never write a check that needs the main folder on a branch or at that commit.';
136
+ /* THE LINE TYPED AGAIN BY ITSELF (17-11): one line lost (a pane in copy mode, a tool drawing a modal)
137
+ silenced every later one, the tool "told" till it read. A message still unread this long after its
138
+ line was typed has it typed again, these long after the last, then every last one of them -- till the
139
+ tool reads (its read count moves). Kept in its record: a restart of this server goes on. */
140
+ export const RENUDGE_MS = Object.freeze([60_000, 180_000, 600_000]);
128
141
  /* After a restart, the tool is given this long to start before REREAD is typed. */
129
142
  export const REREAD_AFTER_MS = 10_000;
130
143
  /* READY, NOT JUST STARTED. The tool starts when its terminal is opened, which
@@ -222,8 +235,8 @@ export const windowOf = (choice) => { const [kind, n] = String(choice).split(':'
222
235
  const PART = /^[A-Za-z0-9._~-]{1,200}$/;
223
236
  export class Talk {
224
237
  #store; #words; #vms; #conversations; #resources; #toolbox; #warm; #alert; #spoke; #inRoom; #listening = new Map(); #every; #now;
225
- #typedAt = new Map(); #nextTry = new Map(); #trying = new Map(); #timer; #lastLang = 'en'; #rereadAfter;
226
- #terminal; #health; #ranAt = new Map(); #kept; #screen; #dropSecret;
238
+ #typedAt = new Map(); #nextTry = new Map(); #trying = new Map(); #ticking = new Map(); #timer; #lastLang = 'en'; #rereadAfter;
239
+ #terminal; #health; #ranAt = new Map(); #kept; #screen; #dropSecret; #log;
227
240
 
228
241
  /* `alert({ title, body, url })`: told when the tool asks, or waits.
229
242
  `words`: a store for the owner's choice of words (one record). */
@@ -233,7 +246,8 @@ export class Talk {
233
246
  /* `keptDir`: where each conversation's thread is kept (its own folder),
234
247
  in this server's data -- never in the work folder, which its machine
235
248
  writes. Without it, the bridge folder is all there is. */
236
- constructor({ store, words, vms, conversations, resources, toolbox = null, warm = async () => {}, alert = () => {}, spoke = () => {}, inRoom = () => {}, listenEveryMs = 2000, now = () => Date.now(), rereadAfterMs = REREAD_AFTER_MS, terminal = null, health = null, keptDir = null, screen = null, dropSecret = null }) {
249
+ constructor({ store, words, vms, conversations, resources, toolbox = null, warm = async () => {}, alert = () => {}, spoke = () => {}, inRoom = () => {}, listenEveryMs = 2000, now = () => Date.now(), rereadAfterMs = REREAD_AFTER_MS, terminal = null, health = null, keptDir = null, screen = null, dropSecret = null, log = (line) => console.log(line) }) {
250
+ this.#log = log;
237
251
  this.#terminal = terminal; this.#health = health; this.#kept = keptDir; this.#screen = screen; this.#dropSecret = dropSecret;
238
252
  this.#store = store; this.#words = words; this.#vms = vms; this.#conversations = conversations; this.#resources = resources; this.#toolbox = toolbox; this.#warm = warm; this.#alert = alert; this.#spoke = spoke; this.#inRoom = inRoom; this.#every = listenEveryMs; this.#now = now; this.#rereadAfter = rereadAfterMs;
239
253
  for (const s of store.all()) if (s.on === true) this.#listen(s.name);
@@ -476,17 +490,22 @@ export class Talk {
476
490
  return this.of(name);
477
491
  }
478
492
 
479
- /* Types a line and Enter in the tool's terminal. Through the terminal the
480
- bridge keeps open into the same tmux pane when there is one (nothing
481
- sent into the machine: the bytes go down a channel already open), or
482
- else one command in the machine. */
493
+ /* Types a line and Enter in the tool's terminal. BY ONE COMMAND IN THE MACHINE FIRST (17-11): it
494
+ leaves copy mode before it types (typeCommand), which the terminal the bridge keeps open cannot --
495
+ bytes down it are copy-mode keys while the pane is scrolled back, and the line was lost, said
496
+ typed. That terminal, into the same tmux pane, when the command fails. Each line said in the log,
497
+ and each one lost, with why. */
483
498
  async #type(name, line) {
499
+ const what = line === WAKE ? 'wake' : line === URGENT ? 'urgent' : line.length > 60 ? `${line.slice(0, 60)}...` : line;
484
500
  await this.#warm(name);
501
+ const ran = await this.#vms.exec(name, typeCommand(line), { timeoutMs: 30_000 }).catch((error) => ({ code: -1, stderr: error?.message }));
502
+ if (ran.code === 0) { this.#ranAt.set(name, this.#now()); this.#log(`talk: ${name}: typed (${what})`); return true; }
503
+ const why = `exit ${ran.code}${ran.stderr ? `: ${String(ran.stderr).trim().slice(0, 200)}` : ''}`;
485
504
  const down = this.#terminal?.open(name) ? await Promise.resolve(this.#terminal.type(name, line, ENTER_AFTER_MS)).catch(() => false) : false;
486
- if (down === true) { this.#ranAt.set(name, this.#now()); return true; }
487
505
  /* The line went, its Enter did not (the channel closed meanwhile): the Enter alone, never the line twice. */
488
- const ok = (await this.#vms.exec(name, down === 'enter' ? enterCommand() : typeCommand(line), { timeoutMs: 30_000 })).code === 0;
506
+ const ok = down === true || (down === 'enter' && (await this.#vms.exec(name, enterCommand(), { timeoutMs: 30_000 }).catch(() => ({ code: -1 }))).code === 0);
489
507
  if (ok) this.#ranAt.set(name, this.#now());
508
+ this.#log(ok ? `talk: ${name}: typed down its terminal (${what}), the command in the machine failed (${why})` : `talk: ${name}: NOT typed (${what}): ${why}; ${this.#terminal?.open(name) ? 'its terminal refused too' : 'no terminal open'}`);
490
509
  return ok;
491
510
  }
492
511
 
@@ -749,10 +768,10 @@ export class Talk {
749
768
  this.#store.put({ ...this.#store.get(name), reread: false, renewedText: undefined });
750
769
  return 'told';
751
770
  }
752
- if (!(await this.#type(name, this.rereadLine()).catch(() => false))) return later(RETRY_MS);
771
+ if (!(await this.#type(name, this.rereadLine()).catch((error) => { this.#log(`talk: ${name}: NOT typed: ${error?.message}`); return false; }))) return later(RETRY_MS);
753
772
  /* Reading the whole thread, it reads what waits too: that line is not typed after it. */
754
773
  const waits = this.#parley(name).unread() > 0;
755
- this.#store.put({ ...this.#store.get(name), reread: false, ...(waits ? { pending: false, woke: true, why: null, again: false } : {}), typedAt: new Date(this.#now()).toISOString() });
774
+ this.#store.put({ ...this.#store.get(name), reread: false, ...(waits ? { pending: false, woke: true, why: null, again: false, toldRead: this.#readCount(name), renudged: 0 } : {}), typedAt: new Date(this.#now()).toISOString() });
756
775
  return 'typed';
757
776
  }
758
777
 
@@ -760,6 +779,12 @@ export class Talk {
760
779
  /* Somebody typed in its terminal: nothing is typed over them. */
761
780
  typed(name) { this.#typedAt.set(name, this.#now()); }
762
781
 
782
+ /* How far the tool has read (Parley's read.json: messages read, archived ones counted). Grows only. */
783
+ #readCount(name) { try { return Math.max(0, Number(JSON.parse(readFileSync(`${this.#dir(name)}/read.json`, 'utf8')).lines) || 0); } catch { return 0; } }
784
+ /* TOLD, THEN READ, THEN WRITTEN TO AGAIN before `woke` was put back (the tick's, every second -- or
785
+ never, while a tick waited on another conversation): the line typed was used, the next is owed. */
786
+ #readSince(name, r) { return Number.isInteger(r?.toldRead) && this.#readCount(name) > r.toldRead; }
787
+
763
788
  #want(name) {
764
789
  const r = this.#store.get(name);
765
790
  if (r?.on === true && r.woke !== true) this.#store.put({ ...r, pending: true });
@@ -769,27 +794,28 @@ export class Talk {
769
794
  'typed', 'read' (nothing waits), 'told' (typed already, not read yet),
770
795
  'asleep', 'typing', 'failed' -- the last three wait, and are tried
771
796
  again. `again`: the owner asked to remind it, typed before or not. */
772
- #wake(name, again = false, urgent = false) {
797
+ #wake(name, again = false, urgent = false, renudge = false) {
773
798
  const going = this.#trying.get(name);
774
799
  if (going) return going;
775
- const run = this.#tryWake(name, again, urgent).finally(() => this.#trying.delete(name));
800
+ const run = this.#tryWake(name, again, urgent, renudge).finally(() => this.#trying.delete(name));
776
801
  this.#trying.set(name, run);
777
802
  return run;
778
803
  }
779
- async #tryWake(name, again, urgent = false) {
804
+ async #tryWake(name, again, urgent = false, renudge = false) {
780
805
  const r = this.#store.get(name);
781
806
  if (r?.on !== true) return 'read';
782
- const waits = (why, ms) => { this.#store.put({ ...this.#store.get(name), pending: true, why, again }); this.#nextTry.set(name, this.#now() + ms); return why; };
783
- if (this.#parley(name).unread() === 0) { if (r.pending || r.woke) this.#store.put({ ...r, pending: false, woke: false, why: null, again: false }); return 'read'; }
807
+ const waits = (why, ms) => { this.#store.put({ ...this.#store.get(name), pending: true, why, again, renudge }); this.#nextTry.set(name, this.#now() + ms); return why; };
808
+ if (this.#parley(name).unread() === 0) { if (r.pending || r.woke) this.#store.put({ ...r, pending: false, woke: false, why: null, again: false, renudge: false }); return 'read'; }
784
809
  /* Its machine has just restarted: the line that will be typed tells it to read everything. */
785
810
  if (r.reread === true) { this.#store.put({ ...r, pending: true, why: 'restarting', again }); return 'restarting'; }
786
- if (r.woke === true && !again) { if (r.pending) this.#store.put({ ...r, pending: false, why: null }); return 'told'; }
811
+ if (r.woke === true && !again && !this.#readSince(name, r)) { if (r.pending) this.#store.put({ ...r, pending: false, why: null }); return 'told'; }
787
812
  if (!this.#conversations.awake().includes(name)) return waits('asleep', RETRY_MS);
788
813
  const quiet = this.#now() - (this.#typedAt.get(name) ?? 0);
789
814
  if (quiet < TYPING_QUIET_MS) return waits('typing', TYPING_QUIET_MS - quiet + 100);
790
815
  if (!(await this.#runs(name))) return waits('asleep', RETRY_MS);
791
- if (!(await this.#type(name, urgent ? URGENT : WAKE).catch(() => false))) return waits('failed', RETRY_MS);
792
- this.#store.put({ ...this.#store.get(name), pending: false, woke: true, why: null, again: false, typedAt: new Date(this.#now()).toISOString() });
816
+ if (renudge) this.#log(`talk: ${name}: still unread ${Math.round((this.#now() - Date.parse(r.typedAt ?? '')) / 1000)} s after its line: typed again (${(r.renudged ?? 0) + 1})`);
817
+ if (!(await this.#type(name, urgent ? URGENT : WAKE).catch((error) => { this.#log(`talk: ${name}: NOT typed: ${error?.message}`); return false; }))) return waits('failed', RETRY_MS);
818
+ this.#store.put({ ...this.#store.get(name), pending: false, woke: true, why: null, again: false, renudge: false, typedAt: new Date(this.#now()).toISOString(), toldRead: this.#readCount(name), renudged: renudge ? (r.renudged ?? 0) + 1 : 0 });
793
819
  return 'typed';
794
820
  }
795
821
 
@@ -801,16 +827,36 @@ export class Talk {
801
827
  return { ...this.of(name), told };
802
828
  }
803
829
 
804
- async #tick() {
830
+ /* NO CONVERSATION WAITS ON ANOTHER: each one's work is started, never awaited in turn -- a machine
831
+ that does not answer (its terminal opening, its state asked) held every conversation after it in
832
+ the store, ticks piling up behind, and their `woke` never put back: their next line was never
833
+ typed. One run at a time for each (#wake's own, and this one's for the rest). */
834
+ #tick() {
805
835
  for (const r of this.#store.all()) {
806
- if (r.on === true) this.#reapForgets(r);
807
- if (r.on === true && r.reread === true) { await this.#tryReread(r.name).catch(() => {}); continue; }
808
- if (r.on !== true || (!r.pending && !r.woke)) continue;
836
+ if (this.#ticking.has(r.name)) continue;
837
+ const run = this.#tickOne(r).catch(() => {}).finally(() => this.#ticking.delete(r.name));
838
+ this.#ticking.set(r.name, run);
839
+ }
840
+ }
841
+ async #tickOne(r) {
842
+ if (r.on === true) this.#reapForgets(r);
843
+ if (r.on === true && r.reread === true) { await this.#tryReread(r.name); return; }
844
+ if (r.on !== true || (!r.pending && !r.woke)) return;
845
+ if (r.woke && !r.pending) {
846
+ let unread;
847
+ try { unread = this.#parley(r.name).unread(); } catch { return; /* its folder went */ }
809
848
  /* It has read: what waited is done, and the next message wakes it again. */
810
- if (r.woke && !r.pending) { try { if (this.#parley(r.name).unread() === 0) this.#store.put({ ...r, woke: false }); } catch { /* its folder went */ } continue; }
811
- if (this.#now() < (this.#nextTry.get(r.name) ?? 0)) continue;
812
- await this.#wake(r.name, r.again === true).catch(() => {});
849
+ if (unread === 0) { this.#store.put({ ...r, woke: false }); return; }
850
+ /* It read since, and more was written: the line is owed now. */
851
+ if (this.#readSince(r.name, r)) { await this.#wake(r.name); return; }
852
+ /* Still unread long after: typed again, RENUDGE_MS apart, the last of them for ever after. */
853
+ const after = RENUDGE_MS[Math.min(r.renudged ?? 0, RENUDGE_MS.length - 1)];
854
+ const at = Date.parse(r.typedAt ?? '');
855
+ if (Number.isFinite(at) && this.#now() - at >= after && this.#now() >= (this.#nextTry.get(r.name) ?? 0)) await this.#wake(r.name, true, false, true);
856
+ return;
813
857
  }
858
+ if (this.#now() < (this.#nextTry.get(r.name) ?? 0)) return;
859
+ await this.#wake(r.name, r.again === true, false, r.renudge === true);
814
860
  }
815
861
 
816
862
  /* The agent asked to forget short-lived secrets (parley forget -> forget.jsonl beside its thread): the