@rarog/server 1.0.1 → 1.1.0
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/README.md +18 -5
- package/node_modules/@rarog/shared/package.json +1 -1
- package/node_modules/@rarog/shared/src/decode.js +64 -3
- package/node_modules/@rarog/shared/src/protocol/files.js +33 -0
- package/node_modules/@rarog/shared/src/protocol/folders.js +27 -0
- package/node_modules/@rarog/shared/src/protocol/herdr.js +23 -0
- package/node_modules/@rarog/shared/src/protocol/inbox.js +38 -0
- package/node_modules/@rarog/shared/src/protocol/notes.js +7 -1
- package/node_modules/@rarog/shared/src/protocol.js +20 -47
- package/node_modules/@rarog/shared/src/rest.js +106 -12
- package/node_modules/@rarog/shared/src/sentences/herdr.js +0 -4
- package/node_modules/@rarog/shared/src/sentences/index.js +7 -1
- package/node_modules/@rarog/shared/src/sentences/rest.js +21 -0
- package/package.json +3 -3
- package/server/src/auth/admin.js +3 -1
- package/server/src/bootstrap/consent.js +99 -2
- package/server/src/bootstrap/folder-gate.js +55 -0
- package/server/src/bootstrap/http.js +3 -2
- package/server/src/bootstrap/inbox.js +29 -4
- package/server/src/bootstrap/sse.js +2 -1
- package/server/src/cli/doctor-no-hook.js +162 -0
- package/server/src/cli/doctor.js +2 -0
- package/server/src/cli/service-log.js +27 -0
- package/server/src/cli/service.js +5 -2
- package/server/src/cli/templates/launchd.plist +1 -1
- package/server/src/cli/templates/rarog.service +1 -1
- package/server/src/cli/update-channel.js +100 -0
- package/server/src/cli/update-provenance.js +37 -19
- package/server/src/cli/update-registry.js +160 -62
- package/server/src/cli/update.js +13 -3
- package/server/src/companion/turns-settle.js +4 -1
- package/server/src/companion/turns.js +36 -5
- package/server/src/config/files.js +32 -0
- package/server/src/config/flags.js +10 -1
- package/server/src/config/protected-dirs.js +2 -0
- package/server/src/config/roots.js +13 -8
- package/server/src/config/work-area.js +36 -12
- package/server/src/config-env.js +33 -0
- package/server/src/config.js +22 -2
- package/server/src/demo/control.js +9 -1
- package/server/src/demo/files.js +49 -0
- package/server/src/demo/inbox.js +73 -0
- package/server/src/demo/review-routes.js +6 -1
- package/server/src/device-effects.js +3 -1
- package/server/src/drivers/herdr/consent-no-hook-io.js +89 -0
- package/server/src/drivers/herdr/consent-no-hook.js +411 -0
- package/server/src/drivers/herdr/refusals.js +226 -0
- package/server/src/drivers/herdr/returned-prompt.js +172 -0
- package/server/src/drivers/herdr/screen-fallback.js +246 -0
- package/server/src/drivers/herdr-driver.js +193 -295
- package/server/src/files/file-index.js +248 -0
- package/server/src/files/ignored.js +119 -0
- package/server/src/files/listing.js +82 -0
- package/server/src/files/path-rule.js +199 -0
- package/server/src/files/refusals.js +50 -0
- package/server/src/files/send-file.js +115 -0
- package/server/src/files/session-download.js +51 -0
- package/server/src/files/session-files.js +38 -0
- package/server/src/files/session-gate.js +53 -0
- package/server/src/folder-gate/client.js +314 -0
- package/server/src/folder-gate/disk.js +455 -0
- package/server/src/folder-gate/folders.js +23 -0
- package/server/src/folder-gate/gate.js +220 -0
- package/server/src/folder-gate/helper.js +118 -0
- package/server/src/folder-gate/install.js +27 -0
- package/server/src/folder-gate/relay-worker.js +147 -0
- package/server/src/folder-gate/types.js +115 -0
- package/server/src/folder-gate/wire.js +170 -0
- package/server/src/fs-browse.js +55 -23
- package/server/src/fs-mkdir.js +3 -1
- package/server/src/git-status.js +16 -7
- package/server/src/git-watch.js +130 -19
- package/server/src/git-work-tree.js +3 -3
- package/server/src/herdr/audit.js +1 -1
- package/server/src/herdr/client.js +2 -2
- package/server/src/herdr/commands/discover.js +21 -9
- package/server/src/herdr/dialogs/consent-gate.js +28 -8
- package/server/src/herdr/dialogs/decide/sources.js +5 -2
- package/server/src/herdr/dialogs/hook/contract.js +16 -9
- package/server/src/herdr/dialogs/index.js +12 -2
- package/server/src/herdr/dialogs/layer1/cycle.js +43 -7
- package/server/src/herdr/dialogs/layer1/hook-list/hook-list.js +166 -86
- package/server/src/herdr/dialogs/layer1/hook-list/session-facts.js +135 -0
- package/server/src/herdr/dialogs/layer1/screen-key.js +52 -0
- package/server/src/herdr/dialogs/layer1/version-gate.js +13 -5
- package/server/src/herdr/dialogs/no-hook.js +63 -0
- package/server/src/herdr/dialogs/router.js +21 -6
- package/server/src/herdr/dialogs/screen/block.js +10 -0
- package/server/src/herdr/dialogs/screen/parse.js +33 -3
- package/server/src/herdr/dialogs/screen/resolve.js +9 -7
- package/server/src/herdr/dialogs/screen-fallback.js +55 -0
- package/server/src/herdr/dialogs/shadow/alive.js +9 -8
- package/server/src/herdr/jsonl-follow.js +55 -14
- package/server/src/herdr/jsonl-pending.js +25 -1
- package/server/src/herdr/provenance.js +3 -3
- package/server/src/herdr/socket.js +25 -28
- package/server/src/herdr/supervisor.js +13 -10
- package/server/src/herdr/transcript-path.js +36 -0
- package/server/src/herdr/trust.js +11 -8
- package/server/src/herdr/uploads-registry.js +3 -2
- package/server/src/herdr/uploads.js +170 -47
- package/server/src/inbox/card.js +18 -9
- package/server/src/inbox/dialogs.js +40 -12
- package/server/src/inbox/heads.js +55 -0
- package/server/src/inbox/inbox.js +24 -2
- package/server/src/inbox/index.js +3 -1
- package/server/src/index.js +44 -21
- package/server/src/install/repair.js +8 -0
- package/server/src/iroh/listener.js +3 -0
- package/server/src/iroh/log-budget.js +1 -0
- package/server/src/iroh/socket.js +2 -1
- package/server/src/log-timestamps.js +9 -4
- package/server/src/notes/store.js +3 -2
- package/server/src/router.js +20 -2
- package/server/src/routes/devices.js +2 -2
- package/server/src/routes/inbox.js +91 -0
- package/server/src/routes/projects.js +5 -1
- package/server/src/routes/sessions.js +145 -18
- package/server/src/routes/system.js +9 -4
- package/server/src/safe-read.js +86 -1
- package/server/src/sessions/companion-scope.js +16 -7
- package/server/src/sessions/demo-modules.js +16 -0
- package/server/src/sessions/diff-measure.js +30 -0
- package/server/src/sessions/done-retention.js +57 -0
- package/server/src/sessions/launch-gate.js +2 -2
- package/server/src/sessions/manager.js +78 -190
- package/server/src/sessions/project-scan.js +9 -3
- package/server/src/sessions/ring-buffer.js +5 -3
- package/server/src/sessions/seen-marks.js +9 -6
- package/server/src/sessions/session-git.js +31 -2
- package/server/src/sessions/session-scope.js +30 -0
- package/server/src/sessions/session.js +19 -0
- package/server/src/sessions/state-sentences.js +1 -1
- package/server/src/sessions/waiting-line.js +72 -0
- package/server/src/sse.js +143 -10
- package/server/src/term/herdr-backend.js +2 -2
- package/server/src/term/index.js +2 -15
- package/server/src/term/registry.js +4 -3
- package/server/src/term/ws.js +9 -1
- package/server/src/version.js +30 -0
|
@@ -34,18 +34,24 @@
|
|
|
34
34
|
* - **The start sweep reads a registry, not the disk tree.** Every successful write records the real path of its
|
|
35
35
|
* `<cwd>` in `<dataDir>/uploads-dirs.json` (`uploads-registry.ts`), and `sweepAll` cleans only those directories,
|
|
36
36
|
* each only while `realpath` still gives the entry itself. It walks the roots once, when the registry is missing
|
|
37
|
-
* or unreadable, and writes the registry from what it found.
|
|
37
|
+
* or unreadable, and writes the registry from what it found once every part of the walk ran.
|
|
38
|
+
* - **A guarded folder of home waits for the folder gate.** A write in a folder the gate holds closed is refused with
|
|
39
|
+
* the sentence of the folder's state. A write the folder's helper did not answer in time may still land, so its
|
|
40
|
+
* `<cwd>` goes to the registry anyway and the next sweep removes the file. The start sweep does not enter a guarded
|
|
41
|
+
* folder until the gate holds it `open`: that part waits for the change and runs then, only while its directory
|
|
42
|
+
* still resolves to itself. Until it ran, the walk writes no registry, so a restart before then walks again.
|
|
38
43
|
*/
|
|
39
44
|
import { randomBytes } from 'node:crypto';
|
|
40
|
-
import { chmod, lstat, mkdir, readdir, realpath, unlink, writeFile } from 'node:fs/promises';
|
|
41
45
|
import { homedir } from 'node:os';
|
|
42
|
-
import { basename, isAbsolute, join } from 'node:path';
|
|
46
|
+
import { basename, isAbsolute, join, relative } from 'node:path';
|
|
43
47
|
import { FILE_DATA_URL_PATTERN, IMAGE_DATA_URL_PATTERN, UPLOAD_IMAGE_FILE_PATTERN, } from '@rarog/shared';
|
|
44
48
|
import { foreign, say, textOf } from '@rarog/shared/sentences';
|
|
45
49
|
import { HERDR_UPLOAD_TTL_MS } from '../config.js';
|
|
46
50
|
import { inWorkAreaOnDisk } from '../config/work-area.js';
|
|
47
51
|
import { isProtectedPath } from '../config/protected-dirs.js';
|
|
48
|
-
import {
|
|
52
|
+
import { DIRECT_DISK } from '../folder-gate/disk.js';
|
|
53
|
+
import { folderGate } from '../folder-gate/install.js';
|
|
54
|
+
import { FolderGateError } from '../folder-gate/types.js';
|
|
49
55
|
/** Katalog załączników (zdjęć i plików) wewnątrz workspace'u panelu: zapis, odczyt miniatur i sprzątanie. */
|
|
50
56
|
export const UPLOAD_DIR_NAME = '.rarog-uploads';
|
|
51
57
|
/** Po tylu godzinach plik jest śmieciem, nie załącznikiem (definicja: `config.ts`). */
|
|
@@ -279,10 +285,10 @@ function notDirectory(info, dir) {
|
|
|
279
285
|
* tym sprawdzeniem a `chmod` wymagałby ŻYWEGO procesu atakującego na Macu, a wektor, przed którym
|
|
280
286
|
* ta bramka stoi (sklonowane repozytorium z dowiązaniem w drzewie), takiego procesu nie daje.
|
|
281
287
|
*/
|
|
282
|
-
async function assertPlainUploadDir(dir) {
|
|
288
|
+
async function assertPlainUploadDir(disk, dir) {
|
|
283
289
|
let info;
|
|
284
290
|
try {
|
|
285
|
-
info = await lstat
|
|
291
|
+
info = await disk.call('lstat', { path: dir });
|
|
286
292
|
}
|
|
287
293
|
catch {
|
|
288
294
|
return; // wpisu nie ma (albo nie da się go obejrzeć) — resztę powie `mkdir`
|
|
@@ -307,25 +313,25 @@ async function assertPlainUploadDir(dir) {
|
|
|
307
313
|
* {@link EXCLUDE_MAX_BYTES} bez linii — dopisanie; cokolwiek innego (dowiązanie, FIFO, urządzenie, plik
|
|
308
314
|
* ponad sufit) — BEZ zapisu. Zapis po nieudanym odczycie byłby `appendFile`, które na FIFO bez
|
|
309
315
|
* czytelnika blokuje wątek puli libuv. Dopisanie sprawdza typ jeszcze raz na własnym deskryptorze
|
|
310
|
-
* (`
|
|
316
|
+
* (`appendRegular` portu dysku).
|
|
311
317
|
*/
|
|
312
|
-
async function ensureGitExclude(cwd) {
|
|
318
|
+
async function ensureGitExclude(disk, cwd) {
|
|
313
319
|
const gitDir = join(cwd, '.git');
|
|
314
320
|
try {
|
|
315
|
-
const info = await lstat
|
|
321
|
+
const info = await disk.call('lstat', { path: gitDir });
|
|
316
322
|
if (!info.isDirectory())
|
|
317
323
|
return;
|
|
318
324
|
const infoDir = join(gitDir, 'info');
|
|
319
|
-
await mkdir
|
|
325
|
+
await disk.call('mkdir', { path: infoDir }).catch((err) => {
|
|
320
326
|
if (err.code !== 'EEXIST')
|
|
321
327
|
throw err;
|
|
322
328
|
});
|
|
323
|
-
if (!(await lstat
|
|
329
|
+
if (!(await disk.call('lstat', { path: infoDir })).isDirectory())
|
|
324
330
|
return;
|
|
325
331
|
const file = join(infoDir, 'exclude');
|
|
326
332
|
let current = '';
|
|
327
333
|
try {
|
|
328
|
-
const read = await
|
|
334
|
+
const read = await disk.call('readHead', { path: file, maxBytes: EXCLUDE_MAX_BYTES, followSymlink: false });
|
|
329
335
|
if (read === null || read.truncated)
|
|
330
336
|
return;
|
|
331
337
|
current = read.bytes.toString('utf8');
|
|
@@ -338,7 +344,7 @@ async function ensureGitExclude(cwd) {
|
|
|
338
344
|
if (present.has(EXCLUDE_LINE))
|
|
339
345
|
return;
|
|
340
346
|
const prefix = current === '' || current.endsWith('\n') ? '' : '\n';
|
|
341
|
-
await
|
|
347
|
+
await disk.call('appendRegular', { path: file, text: `${prefix}${EXCLUDE_LINE}\n` });
|
|
342
348
|
}
|
|
343
349
|
catch {
|
|
344
350
|
// celowo cicho — patrz komentarz nad funkcją
|
|
@@ -350,29 +356,35 @@ class Uploads {
|
|
|
350
356
|
#maxDepth;
|
|
351
357
|
#home;
|
|
352
358
|
#registry;
|
|
359
|
+
#disk;
|
|
360
|
+
/** Start sweeps that wait for a guarded folder to open, by folder. */
|
|
361
|
+
#waiting = new Map();
|
|
362
|
+
#offGate = null;
|
|
353
363
|
constructor(opts = {}) {
|
|
354
364
|
this.#now = opts.now ?? Date.now;
|
|
355
365
|
this.#ttlMs = opts.ttlMs ?? HERDR_UPLOAD_TTL_MS;
|
|
356
366
|
this.#maxDepth = opts.maxDepth ?? SWEEP_MAX_DEPTH;
|
|
357
367
|
this.#home = opts.home ?? homedir();
|
|
358
368
|
this.#registry = opts.registry;
|
|
369
|
+
this.#disk = opts.disk ?? DIRECT_DISK;
|
|
359
370
|
}
|
|
360
371
|
async write(cwd, images) {
|
|
361
372
|
if (images.length === 0)
|
|
362
373
|
return [];
|
|
363
374
|
const dir = await this.#prepareDir(cwd);
|
|
364
375
|
const paths = [];
|
|
376
|
+
const late = [];
|
|
365
377
|
try {
|
|
366
378
|
for (const image of images) {
|
|
367
379
|
const parsed = parseImageDataUrl(image);
|
|
368
380
|
if (parsed === null) {
|
|
369
381
|
throw new UploadError(say('uploads.not-image'));
|
|
370
382
|
}
|
|
371
|
-
paths.push(await this.#writeOne(dir, uploadName(this.#now(), parsed.ext), parsed.bytes));
|
|
383
|
+
paths.push(await this.#writeOne(dir, uploadName(this.#now(), parsed.ext), parsed.bytes, late));
|
|
372
384
|
}
|
|
373
385
|
}
|
|
374
386
|
finally {
|
|
375
|
-
await this.#remember(cwd, paths);
|
|
387
|
+
await this.#remember(cwd, [...paths, ...late]);
|
|
376
388
|
}
|
|
377
389
|
return paths;
|
|
378
390
|
}
|
|
@@ -381,6 +393,7 @@ class Uploads {
|
|
|
381
393
|
return [];
|
|
382
394
|
const dir = await this.#prepareDir(cwd);
|
|
383
395
|
const paths = [];
|
|
396
|
+
const late = [];
|
|
384
397
|
try {
|
|
385
398
|
for (const file of files) {
|
|
386
399
|
const bytes = parseFileDataUrl(file.dataUrl);
|
|
@@ -388,16 +401,17 @@ class Uploads {
|
|
|
388
401
|
const shown = sanitizeUploadName(file.name);
|
|
389
402
|
throw new UploadError(say('uploads.bad-content', { name: shown }));
|
|
390
403
|
}
|
|
391
|
-
paths.push(await this.#writeOne(dir, uploadFileName(this.#now(), file.name), bytes));
|
|
404
|
+
paths.push(await this.#writeOne(dir, uploadFileName(this.#now(), file.name), bytes, late));
|
|
392
405
|
}
|
|
393
406
|
}
|
|
394
407
|
finally {
|
|
395
|
-
await this.#remember(cwd, paths);
|
|
408
|
+
await this.#remember(cwd, [...paths, ...late]);
|
|
396
409
|
}
|
|
397
410
|
return paths;
|
|
398
411
|
}
|
|
399
412
|
/**
|
|
400
|
-
* Records `cwd` in the registry once a file is on disk
|
|
413
|
+
* Records `cwd` in the registry once a file is on disk or may land there late (a write the folder gate refused
|
|
414
|
+
* after its helper did not answer in time), also when a later file of the same message failed: the
|
|
401
415
|
* start sweep must find every file Rarog left. AFTER the write, never before: a start sweep that saw the directory
|
|
402
416
|
* empty drops it unless it was recorded after the sweep's mark, so a record made before the bytes land could be
|
|
403
417
|
* dropped together with the file it should cover. The entry is the real path of `cwd` (no symlink in it): the
|
|
@@ -407,7 +421,7 @@ class Uploads {
|
|
|
407
421
|
async #remember(cwd, written) {
|
|
408
422
|
if (written.length === 0 || this.#registry === undefined)
|
|
409
423
|
return;
|
|
410
|
-
const real = await
|
|
424
|
+
const real = await this.#disk.call('realpathNative', { path: cwd }).catch(() => null);
|
|
411
425
|
if (real !== null)
|
|
412
426
|
await this.#registry.add(real);
|
|
413
427
|
}
|
|
@@ -420,29 +434,39 @@ class Uploads {
|
|
|
420
434
|
async #prepareDir(cwd) {
|
|
421
435
|
const dir = join(cwd, UPLOAD_DIR_NAME);
|
|
422
436
|
// PRZED `mkdir`, bo `mkdir` na dowiązaniu nie rzuca, a `chmod` niżej idzie za dowiązaniem.
|
|
423
|
-
await assertPlainUploadDir(dir);
|
|
437
|
+
await assertPlainUploadDir(this.#disk, dir);
|
|
424
438
|
try {
|
|
425
|
-
await mkdir
|
|
439
|
+
await this.#disk.call('mkdir', { path: dir, recursive: true, mode: UPLOAD_DIR_MODE });
|
|
426
440
|
// `mkdir` przycina tryb umaskiem, a katalog z załącznikami ma być 0700 NIEZALEŻNIE od
|
|
427
441
|
// ustawień powłoki, z której wystartował Rarog.
|
|
428
|
-
await chmod
|
|
442
|
+
await this.#disk.call('chmod', { path: dir, mode: UPLOAD_DIR_MODE });
|
|
429
443
|
}
|
|
430
444
|
catch (err) {
|
|
445
|
+
if (err instanceof FolderGateError)
|
|
446
|
+
throw new UploadError(err.sentence, { cause: err });
|
|
431
447
|
throw new UploadError(say('uploads.mkdir-failed', { dir, reason: foreign(describe(err)) }));
|
|
432
448
|
}
|
|
433
449
|
// Sprzątamy PRZED zapisem, żeby katalog nie puchł przy użytkowniku, który nigdy nie restartuje
|
|
434
450
|
// Raroga. Błąd sprzątania nie ma prawa zablokować wysyłki.
|
|
435
451
|
await this.sweep(cwd);
|
|
436
|
-
await ensureGitExclude(cwd);
|
|
452
|
+
await ensureGitExclude(this.#disk, cwd);
|
|
437
453
|
return dir;
|
|
438
454
|
}
|
|
439
|
-
|
|
455
|
+
/**
|
|
456
|
+
* One file. A write the folder gate refused is reported with the sentence of the folder's state, and its path goes
|
|
457
|
+
* to `late`: when the folder's helper did not answer in time, the bytes may land after the refusal.
|
|
458
|
+
*/
|
|
459
|
+
async #writeOne(dir, name, bytes, late) {
|
|
440
460
|
const path = join(dir, name);
|
|
441
461
|
try {
|
|
442
462
|
// `wx` — nigdy nie nadpisujemy cudzego pliku, nawet gdyby nazwa się powtórzyła.
|
|
443
|
-
await writeFile
|
|
463
|
+
await this.#disk.call('writeFile', { path, data: bytes, mode: UPLOAD_FILE_MODE, flag: 'wx' });
|
|
444
464
|
}
|
|
445
465
|
catch (err) {
|
|
466
|
+
if (err instanceof FolderGateError) {
|
|
467
|
+
late.push(path);
|
|
468
|
+
throw new UploadError(err.sentence, { cause: err });
|
|
469
|
+
}
|
|
446
470
|
throw new UploadError(say('uploads.write-failed', { path, reason: foreign(describe(err)) }));
|
|
447
471
|
}
|
|
448
472
|
return path;
|
|
@@ -460,7 +484,7 @@ class Uploads {
|
|
|
460
484
|
// (`lstat` widzi dowiązanie; `stat` widziałby cel). Wyścig między tym sprawdzeniem
|
|
461
485
|
// a otwarciem pliku przyjęty świadomie, tak samo jak przy `write` — patrz
|
|
462
486
|
// `assertPlainUploadDir`: wymaga żywego procesu atakującego na Macu.
|
|
463
|
-
if (!(await lstat
|
|
487
|
+
if (!(await this.#disk.call('lstat', { path: dir })).isDirectory())
|
|
464
488
|
return null;
|
|
465
489
|
/*
|
|
466
490
|
* Sam plik czytamy przez JEDEN deskryptor: `O_NOFOLLOW` odmawia
|
|
@@ -469,7 +493,7 @@ class Uploads {
|
|
|
469
493
|
* Przez `safe-read.ts`: `O_NONBLOCK` (FIFO pod nazwą zdjęcia nie zajmuje wątku puli libuv)
|
|
470
494
|
* i sufit {@link UPLOAD_READ_MAX_BYTES} (większy plik to nie zdjęcie Raroga).
|
|
471
495
|
*/
|
|
472
|
-
const read = await
|
|
496
|
+
const read = await this.#disk.call('readHead', { path, maxBytes: UPLOAD_READ_MAX_BYTES, followSymlink: false });
|
|
473
497
|
return read === null || read.truncated ? null : { bytes: read.bytes, mime };
|
|
474
498
|
}
|
|
475
499
|
catch {
|
|
@@ -482,7 +506,7 @@ class Uploads {
|
|
|
482
506
|
async sweepAll(roots) {
|
|
483
507
|
const registry = this.#registry;
|
|
484
508
|
if (registry === undefined) {
|
|
485
|
-
await this.#walkRoots(roots);
|
|
509
|
+
await this.#walkRoots(roots, null);
|
|
486
510
|
return;
|
|
487
511
|
}
|
|
488
512
|
const state = await registry.load();
|
|
@@ -490,7 +514,47 @@ class Uploads {
|
|
|
490
514
|
await this.#sweepRegistered(registry, state.dirs);
|
|
491
515
|
return;
|
|
492
516
|
}
|
|
493
|
-
|
|
517
|
+
// Written only when the whole walk ran, its deferred parts too: a registry written before would leave a subtree
|
|
518
|
+
// nobody swept out of every later start sweep.
|
|
519
|
+
await this.#walkRoots(roots, (found) => registry.rebuild(found));
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* The guarded folder of home `dir` lies in while the folder gate does not hold it `open` (`unknown` too), or `null`:
|
|
523
|
+
* the start sweep does not enter such a folder yet. Text only, against home as given and on disk.
|
|
524
|
+
*/
|
|
525
|
+
#waitingFolderOf(dir, realHome) {
|
|
526
|
+
const gate = folderGate();
|
|
527
|
+
if (!gate.enabled)
|
|
528
|
+
return null;
|
|
529
|
+
const rel = relative(realHome, dir);
|
|
530
|
+
const asGiven = rel !== '' && !rel.startsWith('..') && !isAbsolute(rel) ? join(this.#home, rel) : dir;
|
|
531
|
+
const at = gate.stateOf(dir) ?? gate.stateOf(asGiven);
|
|
532
|
+
return at === null || at.state === 'open' ? null : at.folder;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Runs `job` once the folder gate holds `folder` open. One listener on the gate serves every folder that waits;
|
|
536
|
+
* a job never throws into the gate.
|
|
537
|
+
*/
|
|
538
|
+
#afterOpen(folder, job) {
|
|
539
|
+
const jobs = this.#waiting.get(folder) ?? [];
|
|
540
|
+
jobs.push(job);
|
|
541
|
+
this.#waiting.set(folder, jobs);
|
|
542
|
+
if (this.#offGate !== null)
|
|
543
|
+
return;
|
|
544
|
+
this.#offGate = folderGate().onChange((changed, state) => {
|
|
545
|
+
const ready = state === 'open' ? this.#waiting.get(changed) : undefined;
|
|
546
|
+
if (ready === undefined)
|
|
547
|
+
return;
|
|
548
|
+
this.#waiting.delete(changed);
|
|
549
|
+
if (this.#waiting.size === 0) {
|
|
550
|
+
this.#offGate?.();
|
|
551
|
+
this.#offGate = null;
|
|
552
|
+
}
|
|
553
|
+
void (async () => {
|
|
554
|
+
for (const run of ready)
|
|
555
|
+
await run().catch(() => undefined);
|
|
556
|
+
})();
|
|
557
|
+
});
|
|
494
558
|
}
|
|
495
559
|
/**
|
|
496
560
|
* Sweeps the registered directories whatever the current roots are: the server wrote there itself. A missing
|
|
@@ -500,7 +564,19 @@ class Uploads {
|
|
|
500
564
|
async #sweepRegistered(registry, dirs) {
|
|
501
565
|
const mark = registry.mark();
|
|
502
566
|
const leaving = [];
|
|
567
|
+
const home = await this.#disk.call('realpathNative', { path: this.#home }).catch(() => this.#home);
|
|
503
568
|
for (const cwd of dirs) {
|
|
569
|
+
const folder = this.#waitingFolderOf(cwd, home);
|
|
570
|
+
if (folder !== null) {
|
|
571
|
+
// The entry stays registered until its sweep runs.
|
|
572
|
+
this.#afterOpen(folder, async () => {
|
|
573
|
+
const later = registry.mark();
|
|
574
|
+
const outcome = await this.#sweepEntry(cwd);
|
|
575
|
+
if (outcome === 'gone' || outcome === 'empty')
|
|
576
|
+
await registry.drop([cwd], later);
|
|
577
|
+
});
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
504
580
|
const outcome = await this.#sweepEntry(cwd);
|
|
505
581
|
if (outcome === 'gone' || outcome === 'empty')
|
|
506
582
|
leaving.push(cwd);
|
|
@@ -518,7 +594,7 @@ class Uploads {
|
|
|
518
594
|
async #sweepEntry(cwd) {
|
|
519
595
|
let real;
|
|
520
596
|
try {
|
|
521
|
-
real = await
|
|
597
|
+
real = await this.#disk.call('realpathNative', { path: cwd });
|
|
522
598
|
}
|
|
523
599
|
catch (err) {
|
|
524
600
|
const code = err.code;
|
|
@@ -532,29 +608,62 @@ class Uploads {
|
|
|
532
608
|
return this.#sweepDir(join(cwd, UPLOAD_DIR_NAME));
|
|
533
609
|
}
|
|
534
610
|
/**
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
611
|
+
* Whether a deferred part of the walk may sweep `dir`, a real path when the walk reached it: only while its real path
|
|
612
|
+
* is still `dir` itself. The `lstat` in `#sweepDir` sees only the last name, so a `dir` or a parent replaced by a
|
|
613
|
+
* symlink while the folder was closed would lead the sweep into another tree, where it deletes old files. Otherwise
|
|
614
|
+
* one log line and nothing under `dir` is touched; a `realpath` error other than a missing path keeps `dir` among
|
|
615
|
+
* what the walk found, so the next start tries it again.
|
|
538
616
|
*/
|
|
539
|
-
async #
|
|
617
|
+
async #stillItself(dir, walk) {
|
|
618
|
+
let real;
|
|
619
|
+
try {
|
|
620
|
+
real = await this.#disk.call('realpathNative', { path: dir });
|
|
621
|
+
}
|
|
622
|
+
catch (err) {
|
|
623
|
+
const code = err.code ?? 'error';
|
|
624
|
+
console.warn(`rarog: skipping the deferred cleanup of ${dir} — its real path cannot be read (${code}).`);
|
|
625
|
+
if (code !== 'ENOENT' && code !== 'ENOTDIR')
|
|
626
|
+
walk.found.push(dir);
|
|
627
|
+
return false;
|
|
628
|
+
}
|
|
629
|
+
if (real === dir)
|
|
630
|
+
return true;
|
|
631
|
+
console.warn(`rarog: skipping the deferred cleanup of ${dir} — the path now leads through a symlink; `
|
|
632
|
+
+ 'nothing under it is cleaned.');
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Walk of the roots. Once every part of it ran, the first pass and each subtree deferred until its guarded folder
|
|
637
|
+
* opens, `complete` gets the directories whose `.rarog-uploads` still holds files after the sweep, or could not be
|
|
638
|
+
* read (`unknown`), so the next start tries those again. A folder that does not open in this process, or a part
|
|
639
|
+
* that throws, leaves the walk incomplete and `complete` uncalled. The walk enters no symlinked directory, so every
|
|
640
|
+
* path it gives is already a real path.
|
|
641
|
+
*/
|
|
642
|
+
async #walkRoots(roots, complete) {
|
|
540
643
|
// Home and roots in their on-disk form before the walk: the protected-folder check compares paths as text, so
|
|
541
644
|
// a root that names home in another case (`/users/demo`) or through a symlink would otherwise reach `Desktop`.
|
|
542
645
|
// `fs/promises` `realpath` has the native semantics and returns the stored case on APFS. Only the roots and home
|
|
543
646
|
// are resolved, not every visited folder; a root that does not resolve is skipped, as a missing one always was.
|
|
544
|
-
const home = await
|
|
647
|
+
const home = await this.#disk.call('realpathNative', { path: this.#home }).catch(() => this.#home);
|
|
545
648
|
const resolved = [];
|
|
546
649
|
for (const root of roots) {
|
|
547
650
|
if (!isAbsolute(root))
|
|
548
651
|
continue;
|
|
549
|
-
const real = await
|
|
652
|
+
const real = await this.#disk.call('realpathNative', { path: root }).catch(() => null);
|
|
550
653
|
if (real !== null)
|
|
551
654
|
resolved.push(real);
|
|
552
655
|
}
|
|
553
|
-
const walk = { seen: new Set(), roots: resolved, home, found: [] };
|
|
656
|
+
const walk = { seen: new Set(), roots: resolved, home, found: [], parts: { left: 1, complete } };
|
|
554
657
|
for (const root of resolved) {
|
|
555
658
|
await this.#sweepTree(root, this.#maxDepth, walk);
|
|
556
659
|
}
|
|
557
|
-
|
|
660
|
+
await this.#partRan(walk);
|
|
661
|
+
}
|
|
662
|
+
/** One part of the walk ran; after the last one, `complete` gets what the whole walk found. */
|
|
663
|
+
async #partRan(walk) {
|
|
664
|
+
walk.parts.left -= 1;
|
|
665
|
+
if (walk.parts.left === 0)
|
|
666
|
+
await walk.parts.complete?.(walk.found);
|
|
558
667
|
}
|
|
559
668
|
async #sweepTree(dir, depth, walk) {
|
|
560
669
|
if (walk.seen.has(dir))
|
|
@@ -564,6 +673,18 @@ class Uploads {
|
|
|
564
673
|
// and the walk would wait for an answer nobody gives. A root placed inside such a folder keeps its subtree.
|
|
565
674
|
if (isProtectedPath(dir, walk.roots, walk.home))
|
|
566
675
|
return;
|
|
676
|
+
const folder = this.#waitingFolderOf(dir, walk.home);
|
|
677
|
+
if (folder !== null) {
|
|
678
|
+
// A root inside a guarded folder the gate does not hold open yet: this subtree is a part of the walk that runs
|
|
679
|
+
// once the folder opens, and only while `dir` is still itself.
|
|
680
|
+
walk.parts.left += 1;
|
|
681
|
+
this.#afterOpen(folder, async () => {
|
|
682
|
+
if (await this.#stillItself(dir, walk))
|
|
683
|
+
await this.#sweepTree(dir, depth, { ...walk, seen: new Set() });
|
|
684
|
+
await this.#partRan(walk);
|
|
685
|
+
});
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
567
688
|
const outcome = await this.#sweepDir(join(dir, UPLOAD_DIR_NAME));
|
|
568
689
|
if (outcome === 'kept' || outcome === 'unknown')
|
|
569
690
|
walk.found.push(dir);
|
|
@@ -571,7 +692,7 @@ class Uploads {
|
|
|
571
692
|
return;
|
|
572
693
|
let entries;
|
|
573
694
|
try {
|
|
574
|
-
entries = await readdir
|
|
695
|
+
entries = await this.#disk.call('readdir', { path: dir, encoding: 'utf8' });
|
|
575
696
|
}
|
|
576
697
|
catch {
|
|
577
698
|
return;
|
|
@@ -579,10 +700,11 @@ class Uploads {
|
|
|
579
700
|
for (const entry of entries) {
|
|
580
701
|
if (!entry.isDirectory())
|
|
581
702
|
continue;
|
|
703
|
+
const name = String(entry.name);
|
|
582
704
|
// Katalogi ukryte i `node_modules` odpadają: projektów tam nie ma, a skan kosztuje.
|
|
583
|
-
if (
|
|
705
|
+
if (name.startsWith('.') || name === 'node_modules')
|
|
584
706
|
continue;
|
|
585
|
-
await this.#sweepTree(join(dir,
|
|
707
|
+
await this.#sweepTree(join(dir, name), depth - 1, walk);
|
|
586
708
|
}
|
|
587
709
|
}
|
|
588
710
|
async #sweepDir(dir) {
|
|
@@ -592,7 +714,7 @@ class Uploads {
|
|
|
592
714
|
// (sweep startowy leci przy każdym starcie serwera). `readdir` listuje CEL dowiązania,
|
|
593
715
|
// a `unlink` przez nie przechodzi — dowiązane `.rarog-uploads` oznaczałoby ciche kasowanie
|
|
594
716
|
// wszystkiego starszego niż 48 h w cudzym katalogu.
|
|
595
|
-
info = await lstat
|
|
717
|
+
info = await this.#disk.call('lstat', { path: dir });
|
|
596
718
|
}
|
|
597
719
|
catch (err) {
|
|
598
720
|
const code = err.code;
|
|
@@ -608,9 +730,9 @@ class Uploads {
|
|
|
608
730
|
let names;
|
|
609
731
|
try {
|
|
610
732
|
// `Dirent` z `readdir` nie podąża za dowiązaniem, więc `isFile()` odsiewa dowiązane wpisy.
|
|
611
|
-
names = (await readdir
|
|
733
|
+
names = (await this.#disk.call('readdir', { path: dir, encoding: 'utf8' }))
|
|
612
734
|
.filter((e) => e.isFile())
|
|
613
|
-
.map((e) => e.name);
|
|
735
|
+
.map((e) => String(e.name));
|
|
614
736
|
}
|
|
615
737
|
catch {
|
|
616
738
|
return 'unknown';
|
|
@@ -621,10 +743,11 @@ class Uploads {
|
|
|
621
743
|
const path = join(dir, name);
|
|
622
744
|
try {
|
|
623
745
|
// `lstat` także tutaj — wiek liczymy z tego wpisu, który za chwilę kasujemy, a nie z celu.
|
|
624
|
-
const entry = await lstat
|
|
625
|
-
|
|
746
|
+
const entry = await this.#disk.call('lstat', { path });
|
|
747
|
+
// Milliseconds with their fraction, as the number form of `lstat` gave them; bigint `mtimeMs` drops it.
|
|
748
|
+
if (Number(entry.mtimeNs) / 1e6 >= cutoff)
|
|
626
749
|
continue;
|
|
627
|
-
await unlink
|
|
750
|
+
await this.#disk.call('unlink', { path });
|
|
628
751
|
left -= 1;
|
|
629
752
|
}
|
|
630
753
|
catch {
|
package/server/src/inbox/card.js
CHANGED
|
@@ -57,12 +57,15 @@ export function composeCard(session, shown, requestOf = () => undefined) {
|
|
|
57
57
|
if (kind === null || shown === undefined)
|
|
58
58
|
return null;
|
|
59
59
|
const at = kind === 'result' ? shown.at : session.stateSince;
|
|
60
|
-
const
|
|
60
|
+
const decided = kind === 'decision' ? dialogOf(session, requestOf) : undefined;
|
|
61
|
+
const dialog = decided?.dialog;
|
|
61
62
|
const key = keyOf(shown.turnId, kind, dialog, at);
|
|
63
|
+
// The revision changes the version only: a removal of the card holds while its buttons change.
|
|
64
|
+
const revision = decided?.revision === undefined ? [] : [decided.revision];
|
|
62
65
|
return {
|
|
63
66
|
sessionId: session.id,
|
|
64
67
|
turnId: shown.turnId,
|
|
65
|
-
version: digest([shown.version, kind, key.dialog ?? '', key.dialogId ?? '', key.since ?? '']),
|
|
68
|
+
version: digest([shown.version, kind, key.dialog ?? '', key.dialogId ?? '', key.since ?? '', ...revision]),
|
|
66
69
|
kind,
|
|
67
70
|
state: session.state,
|
|
68
71
|
at,
|
|
@@ -98,21 +101,27 @@ function dialogIdOf(dialog) {
|
|
|
98
101
|
return digest([dialog.kind, 'request', dialog.requestId]);
|
|
99
102
|
return digest([dialog.kind, 'summary', JSON.stringify(dialog.waitingFor ?? null)]);
|
|
100
103
|
}
|
|
104
|
+
/** The dialog of a decision card, and the revision of its request's state and buttons when it has one. */
|
|
101
105
|
function dialogOf(session, requestOf) {
|
|
102
106
|
const waiting = session.waitingFor;
|
|
103
107
|
const kind = waiting?.kind ?? (session.state === 'waiting-question' ? 'question' : undefined);
|
|
104
108
|
if (kind === undefined) {
|
|
105
109
|
return {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
110
|
+
dialog: {
|
|
111
|
+
kind: 'panel',
|
|
112
|
+
...(session.herdrDialog === undefined ? {} : { herdrDialog: session.herdrDialog }),
|
|
113
|
+
...(session.herdrDialogReason === undefined ? {} : { herdrDialogReason: session.herdrDialogReason }),
|
|
114
|
+
},
|
|
109
115
|
};
|
|
110
116
|
}
|
|
111
|
-
const
|
|
117
|
+
const request = requestOf(kind, waiting);
|
|
112
118
|
return {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
dialog: {
|
|
120
|
+
kind,
|
|
121
|
+
...(waiting === undefined ? {} : { waitingFor: waiting }),
|
|
122
|
+
...(request === undefined ? {} : { requestId: request.id }),
|
|
123
|
+
},
|
|
124
|
+
...(request?.revision === undefined ? {} : { revision: request.revision }),
|
|
116
125
|
};
|
|
117
126
|
}
|
|
118
127
|
/** Opaque hash of the parts; the JSON array keeps the parts apart. */
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* The dialog requests each session waits on, folded from the session manager's events (`permission-pending`,
|
|
3
3
|
* `permission-settled`, `permission-resolved`, `question-pending`, `question-resolved`): which request a decision card
|
|
4
4
|
* stands for. The manager keeps its queue of requests to itself and its transcript read marks consent cards as shown
|
|
5
|
-
* on a device, so the Inbox folds the events every device gets. It keeps request ids, states and a digest of
|
|
6
|
-
* session summary's `waitingFor` would say of each request, never their text.
|
|
5
|
+
* on a device, so the Inbox folds the events every device gets. It keeps request ids, states, buttons and a digest of
|
|
6
|
+
* what the session summary's `waitingFor` would say of each request, never their text.
|
|
7
7
|
*/
|
|
8
8
|
import { createHash } from 'node:crypto';
|
|
9
9
|
import { EXIT_PLAN_MODE_TOOL, WAITING_TEXT_MAX } from '@rarog/shared';
|
|
@@ -17,18 +17,31 @@ export class PendingDialogs {
|
|
|
17
17
|
const { request } = event;
|
|
18
18
|
const hook = request.channel !== undefined;
|
|
19
19
|
this.#of(sessionId).permissions.set(request.permId, {
|
|
20
|
-
hook, open: !hook || request.state === 'pending',
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
hook, open: !hook || request.state === 'pending',
|
|
21
|
+
said: {
|
|
22
|
+
tool: request.toolName,
|
|
23
|
+
subject: request.subject ?? (request.toolName === EXIT_PLAN_MODE_TOOL ? 'plan' : 'tool'),
|
|
24
|
+
text: digestOf(permissionLine(request)),
|
|
25
|
+
},
|
|
26
|
+
canAllow: request.canAllow !== false, state: request.state, unavailable: request.unavailable,
|
|
23
27
|
});
|
|
24
28
|
return;
|
|
25
29
|
}
|
|
26
30
|
case 'permission-settled': {
|
|
27
|
-
const
|
|
28
|
-
|
|
31
|
+
const permissions = this.#of(sessionId).permissions;
|
|
32
|
+
const entry = permissions.get(event.permId);
|
|
33
|
+
if (entry === undefined) {
|
|
34
|
+
// Only a consent card changes in place; one the fold never saw pending still holds its place.
|
|
35
|
+
permissions.set(event.permId, {
|
|
36
|
+
hook: true, open: event.state === 'pending', said: undefined, canAllow: event.canAllow, state: event.state,
|
|
37
|
+
unavailable: event.unavailable,
|
|
38
|
+
});
|
|
29
39
|
return;
|
|
40
|
+
}
|
|
30
41
|
entry.open = !entry.hook || event.state === 'pending';
|
|
31
42
|
entry.canAllow = event.canAllow;
|
|
43
|
+
entry.state = event.state;
|
|
44
|
+
entry.unavailable = event.unavailable;
|
|
32
45
|
return;
|
|
33
46
|
}
|
|
34
47
|
case 'permission-resolved':
|
|
@@ -53,7 +66,8 @@ export class PendingDialogs {
|
|
|
53
66
|
* the screen or the transcript, else the first consent card that waits for a person; the first question. It is
|
|
54
67
|
* given only when `waitingFor` says of it what the summary says (tool, subject, text line, question count): the
|
|
55
68
|
* fold lacks a consent card the server recovered at start, before anyone listened, and would otherwise give the id
|
|
56
|
-
* of the card behind it.
|
|
69
|
+
* of the card behind it. A consent card known only from a change cannot be compared, so it gives no id either.
|
|
70
|
+
* `undefined` = the fold has none, or another card than the one `waitingFor` describes.
|
|
57
71
|
*/
|
|
58
72
|
current(sessionId, kind, waitingFor) {
|
|
59
73
|
const requests = this.#sessions.get(sessionId);
|
|
@@ -67,11 +81,25 @@ export class PendingDialogs {
|
|
|
67
81
|
}
|
|
68
82
|
const permissions = [...requests.permissions];
|
|
69
83
|
const [id, permission] = permissions.find(([, p]) => !p.hook) ?? permissions.find(([, p]) => p.hook && p.open) ?? [];
|
|
70
|
-
|
|
84
|
+
const said = permission?.said;
|
|
85
|
+
if (said === undefined || waitingFor.count !== undefined)
|
|
71
86
|
return undefined;
|
|
72
|
-
const
|
|
73
|
-
return
|
|
74
|
-
|
|
87
|
+
const line = permission?.canAllow === true ? said.text : digestOf('');
|
|
88
|
+
return said.tool === waitingFor.tool && said.subject === waitingFor.subject && line === text ? id : undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The {@link current} request with the revision of its state and buttons: for a consent card an opaque digest of
|
|
92
|
+
* `state`, `canAllow` and `unavailable`, which `permission-settled` changes in place; none for a card from the
|
|
93
|
+
* screen or the transcript and for a question, which do not change in place.
|
|
94
|
+
*/
|
|
95
|
+
request(sessionId, kind, waitingFor) {
|
|
96
|
+
const id = this.current(sessionId, kind, waitingFor);
|
|
97
|
+
if (id === undefined)
|
|
98
|
+
return undefined;
|
|
99
|
+
const entry = kind === 'permission' ? this.#sessions.get(sessionId)?.permissions.get(id) : undefined;
|
|
100
|
+
if (entry === undefined || !entry.hook)
|
|
101
|
+
return { id };
|
|
102
|
+
return { id, revision: digestOf(JSON.stringify([entry.state ?? '', entry.canAllow, entry.unavailable ?? ''])) };
|
|
75
103
|
}
|
|
76
104
|
forget(sessionId) {
|
|
77
105
|
this.#sessions.delete(sessionId);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** The head of a card: what the stream says of it, without the text and the dialog details. */
|
|
2
|
+
export function headOf(card) {
|
|
3
|
+
return {
|
|
4
|
+
sessionId: card.sessionId, turnId: card.turnId, version: card.version, kind: card.kind, state: card.state,
|
|
5
|
+
at: card.at, ...(card.dialog === undefined ? {} : { dialog: card.dialog.kind }),
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
/** Newest first, as the Inbox shows the cards; the session id keeps the order stable for equal times. */
|
|
9
|
+
export function byNewest(a, b) {
|
|
10
|
+
if (a.at !== b.at)
|
|
11
|
+
return a.at < b.at ? 1 : -1;
|
|
12
|
+
return a.sessionId < b.sessionId ? -1 : a.sessionId > b.sessionId ? 1 : 0;
|
|
13
|
+
}
|
|
14
|
+
export class InboxHeads {
|
|
15
|
+
#source;
|
|
16
|
+
/** The head last published for each session, with its JSON for the comparison. */
|
|
17
|
+
#published = new Map();
|
|
18
|
+
#listeners = new Set();
|
|
19
|
+
/** Follows the tracker's changes from now on; the heads start empty and fill with its reads. */
|
|
20
|
+
constructor(inbox) {
|
|
21
|
+
this.#source = inbox;
|
|
22
|
+
inbox.onChange((sessionId) => { this.note(sessionId); });
|
|
23
|
+
}
|
|
24
|
+
/** The published heads, newest first: the `inbox-heads` of a stream that opens now. */
|
|
25
|
+
heads() {
|
|
26
|
+
return [...this.#published.values()].map((entry) => entry.head).sort(byNewest);
|
|
27
|
+
}
|
|
28
|
+
/** Listener of the events; returns the function that detaches it. */
|
|
29
|
+
onEvent(listener) {
|
|
30
|
+
this.#listeners.add(listener);
|
|
31
|
+
return () => { this.#listeners.delete(listener); };
|
|
32
|
+
}
|
|
33
|
+
/** Compares the session's visible card with the head published for it and publishes the difference. */
|
|
34
|
+
note(sessionId) {
|
|
35
|
+
const card = this.#source.card(sessionId);
|
|
36
|
+
const previous = this.#published.get(sessionId);
|
|
37
|
+
if (card === null || this.#source.removed(sessionId)) {
|
|
38
|
+
if (previous === undefined)
|
|
39
|
+
return;
|
|
40
|
+
this.#published.delete(sessionId);
|
|
41
|
+
this.#emit({ type: 'inbox-removed', sessionId, turnId: previous.head.turnId });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const head = headOf(card);
|
|
45
|
+
const json = JSON.stringify(head);
|
|
46
|
+
if (previous?.json === json)
|
|
47
|
+
return;
|
|
48
|
+
this.#published.set(sessionId, { head, json });
|
|
49
|
+
this.#emit({ type: 'inbox-head', head });
|
|
50
|
+
}
|
|
51
|
+
#emit(event) {
|
|
52
|
+
for (const listener of this.#listeners)
|
|
53
|
+
listener(event);
|
|
54
|
+
}
|
|
55
|
+
}
|