@rljson/fs-agent 0.2.0 → 0.2.2

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.
@@ -4,7 +4,8 @@ import { ATOMIC_TMP_PREFIX } from './fs-atomic-write.ts';
4
4
  import { FsBlobAdapter } from './fs-blob-adapter.ts';
5
5
  import { StoreFsTreeOptions } from './fs-db-adapter.ts';
6
6
  import { FsSignal } from './fs-signals.ts';
7
- import { AntiEntropyStatus } from './fs-receive.ts';
7
+ import { FsHistoryEntry, FsHistoryOptions, FsHistoryPage } from './fs-history.ts';
8
+ import { AntiEntropyStatus, Relation } from './fs-receive.ts';
8
9
  import { FsScanner, FsTree } from './fs-scanner.ts';
9
10
  import { Connector, Db } from '@rljson/db';
10
11
  /**
@@ -245,6 +246,11 @@ export declare const STAMP_ASK_MAX_MS = 30000;
245
246
  * intentional.
246
247
  */
247
248
  export declare const MASS_DELETE_MAX_RATIO = 0.3;
249
+ /**
250
+ * How many passes {@link FsAgent.restoreRevision} makes over a folder that
251
+ * holds a file still being written, before it gives up and changes nothing.
252
+ */
253
+ export declare const RESTORE_SETTLE_PASSES = 10;
248
254
  /**
249
255
  * A restore that could not put the folder into the state the tree describes.
250
256
  *
@@ -481,13 +487,6 @@ export declare class FsAgent {
481
487
  * Gets the current timeout configuration
482
488
  */
483
489
  get timeouts(): Required<TimeoutConfig>;
484
- /**
485
- * Whether this node and the hub agree, and what the anti-entropy has done
486
- * about it when they did not. `null` until {@link syncFromDb} runs.
487
- *
488
- * A divergence that lasts is the one thing a lost message leaves behind, and
489
- * until now nothing reported it: this is what a diagnostics view should show.
490
- */
491
490
  /**
492
491
  * Everything that happened to this folder that a person might care about,
493
492
  * oldest first — see {@link FsSignal}.
@@ -523,6 +522,13 @@ export declare class FsAgent {
523
522
  * @returns Unsubscribes.
524
523
  */
525
524
  onSignal(listener: (signal: FsSignal) => void): () => void;
525
+ /**
526
+ * Whether this node and the hub agree, and what the anti-entropy has done
527
+ * about it when they did not. `null` until {@link syncFromDb} runs.
528
+ *
529
+ * A divergence that lasts is the one thing a lost message leaves behind, and
530
+ * until now nothing reported it: this is what a diagnostics view should show.
531
+ */
526
532
  get antiEntropyStatus(): AntiEntropyStatus | null;
527
533
  /**
528
534
  * Records the ref this folder is now at, so a restart can still say what it
@@ -569,6 +575,8 @@ export declare class FsAgent {
569
575
  * what to emit about.
570
576
  * @param recovered - Paths moved aside because the history removed them.
571
577
  * @param conflicted - Paths edited here while away and changed elsewhere.
578
+ * @param copies - Where each moved file went, `path → name`; a path the
579
+ * join could not move has no entry.
572
580
  */
573
581
  private _signalJoinOutcome;
574
582
  /**
@@ -585,7 +593,14 @@ export declare class FsAgent {
585
593
  * never worth failing a construction over.
586
594
  */
587
595
  private _restoreSignals;
588
- /** Writes the signal log, best effort. */
596
+ /**
597
+ * Writes the signal log, best effort.
598
+ *
599
+ * Through a staging file, because a host reads this file while the agent
600
+ * writes it, and a crash between truncate and write left an empty file —
601
+ * which the next start could not parse, so the whole history was dropped and
602
+ * the next signal overwrote it.
603
+ */
589
604
  private _persistSignals;
590
605
  /**
591
606
  * Appends a sync error entry to the error log file in the sync folder.
@@ -924,6 +939,9 @@ export declare class FsAgent {
924
939
  * nothing else will ever tell anybody it stopped working.
925
940
  * @param path - The file to move, relative to the root, `/`-separated.
926
941
  * @param name - Where to move it, same form.
942
+ * @param copies - Where each file that WAS moved went, `path → name`: what
943
+ * the join signal tells a host. A file that could not be moved is left out,
944
+ * because its own version is gone once the restore overwrites it.
927
945
  */
928
946
  private _setAside;
929
947
  /** The chain entry this node stands on, persisted across restarts. */
@@ -932,6 +950,12 @@ export declare class FsAgent {
932
950
  private _ownContent;
933
951
  /** Paths the watcher saw deleted since the own head was authored. */
934
952
  private readonly _watched;
953
+ /**
954
+ * What the last refusal held back and is still gone. A refusal holds its
955
+ * files until they are back — see `planAuthoring`. In memory only: after a
956
+ * restart the floors judge what is gone afresh, as before.
957
+ */
958
+ private _heldBack;
935
959
  /** Every chain entry's stamp, as this node learned it. */
936
960
  private _stamps?;
937
961
  /** The route this agent syncs, once a sync started. */
@@ -965,6 +989,74 @@ export declare class FsAgent {
965
989
  private _partiallyApplied;
966
990
  /** The chain entry this node stands on, if any. */
967
991
  get ownHead(): string | undefined;
992
+ /**
993
+ * The entry this folder stands on, with its stamp: what a host shows as the
994
+ * folder's current revision.
995
+ * @returns The entry, or `undefined` while no sync runs or the folder has no
996
+ * head yet.
997
+ */
998
+ headInfo(): Promise<FsHistoryEntry | undefined>;
999
+ /**
1000
+ * One page of the folder's history, newest first: every edit, merge and
1001
+ * root below the own head, whichever node made it — see `fs-history.ts` for
1002
+ * the order.
1003
+ *
1004
+ * A host whose agent runs in another process reads the same pages from the
1005
+ * store with `readFsHistory`.
1006
+ * @param options - Where to start (default: the own head), or the cursor of
1007
+ * the previous page, and how many entries.
1008
+ * @returns The page; empty while no sync runs or the folder has no head.
1009
+ */
1010
+ history(options?: FsHistoryOptions): Promise<FsHistoryPage>;
1011
+ /**
1012
+ * How this folder's head stands to another — the hub's, a peer's: the same
1013
+ * (`same`), behind it, ahead of it, forked from it, or not decidable from
1014
+ * what this node can read (`incomplete`).
1015
+ *
1016
+ * Two different heads can hold the same files — after two nodes merged the
1017
+ * same fork, say. Whether they do is a question for the tree refs, not this.
1018
+ * @param head - The other head.
1019
+ * @returns The relation, or `undefined` while no sync runs or the folder has
1020
+ * no head.
1021
+ */
1022
+ relationTo(head: string): Promise<Relation | undefined>;
1023
+ /**
1024
+ * Puts the folder back to a revision of its history, EXACTLY, and makes that
1025
+ * the newest edit, so every node follows.
1026
+ *
1027
+ * Exactly: what the revision holds is written, and what it lacks is
1028
+ * deleted. An older `restore` or `loadFromDb` of the same tree only adds and
1029
+ * overwrites, so files made after the revision stay and the folder ends up
1030
+ * in a state no revision ever had.
1031
+ *
1032
+ * Nothing is lost by it. Whatever the folder holds beyond its head is
1033
+ * recorded first, and the revision being left stays in the history, from
1034
+ * where it can be restored in turn.
1035
+ *
1036
+ * Rejects while no sync runs or the folder has no head; with a
1037
+ * `MassDeleteRefusedError`, changing nothing, when the revision would delete
1038
+ * a large part of the folder — the guard every prune has; and with a
1039
+ * `RestoreIncompleteError` when a file could not be written, the rest of the
1040
+ * revision in place.
1041
+ * @param head - The revision: a chain entry from {@link history}.
1042
+ * @returns `restored`; `unknown` when this node cannot read the entry or its
1043
+ * tree; `busy` while a file in the folder is still being written — nothing
1044
+ * was changed in either case, and a busy folder can be asked again.
1045
+ */
1046
+ restoreRevision(head: string): Promise<'restored' | 'unknown' | 'busy'>;
1047
+ /**
1048
+ * Records what the folder holds before something writes over it, waiting
1049
+ * out a file the scan will not read yet.
1050
+ *
1051
+ * A pass leaves out a file it has never read and one written to while it
1052
+ * ran, and reads them once they settle — right for a sync, which only ever
1053
+ * adds a later version. A restore overwrites and prunes, and a file no entry
1054
+ * holds yet is somebody's work with no copy anywhere: measured, a file
1055
+ * saved just before a restore was deleted by it.
1056
+ * @returns Whether everything was recorded; `false` while a file is still
1057
+ * being written after {@link RESTORE_SETTLE_PASSES} passes.
1058
+ */
1059
+ private _recordFolder;
968
1060
  /** When this agent last finished something that may write to the folder. */
969
1061
  private _wroteAt;
970
1062
  /**