@rljson/fs-agent 0.1.0 → 0.2.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.
@@ -28,6 +28,17 @@ export declare const atomicTmpPath: (filePath: string) => string;
28
28
  * @param content - Bytes to write.
29
29
  */
30
30
  export declare const atomicWriteFile: (filePath: string, content: Buffer | string) => Promise<void>;
31
+ /**
32
+ * {@link atomicWriteFile}, synchronously.
33
+ *
34
+ * For the agent's own bookkeeping files — the signal log and the state file —
35
+ * which are written from places that cannot wait, and which a host reads while
36
+ * the agent runs. Written in place, a crash between the truncate and the write
37
+ * left an empty file, and the next start could not read the history it held.
38
+ * @param filePath - Destination path.
39
+ * @param content - Bytes to write.
40
+ */
41
+ export declare const atomicWriteFileSync: (filePath: string, content: Buffer | string) => void;
31
42
  /**
32
43
  * Writes a stream to `filePath` through a staging file, one chunk at a time.
33
44
  *
@@ -9,7 +9,7 @@ export declare const FS_EDIT_ACTION = "putFsTree";
9
9
  * catch-up, and left unbounded it pins a core on a long chain.
10
10
  *
11
11
  * Exhausting it is NOT the same as failing to read a row — see
12
- * {@link FsEditChain.classify}. Conflating the two made any node more than this
12
+ * {@link FsEditChain.relate}. Conflating the two made any node more than this
13
13
  * many pushes into its own history stop repairing, permanently and silently.
14
14
  */
15
15
  export declare const DEFAULT_MAX_WALK = 500;
@@ -36,13 +36,17 @@ export interface FsAppendOptions {
36
36
  /** Relative paths removed. Default: none. */
37
37
  removed?: readonly string[];
38
38
  /**
39
- * What this entry was made from.
40
- *
41
- * Defaults to the current head. Passed explicitly only for a MERGE entry,
42
- * which has two parents — the one shape `MultiEditManager` refuses and the
39
+ * The entry's `timeId`, when it has to be a function of its content: two
40
+ * nodes that merge the same two heads into the same tree write the same
41
+ * entry. Minted otherwise; a root is identified by its tree.
42
+ */
43
+ timeId?: string;
44
+ /**
45
+ * What this entry was made from: none for a root, the own head for an edit,
46
+ * both heads for a merge — the one shape `MultiEditManager` refuses and the
43
47
  * reason these rows are written by hand.
44
48
  */
45
- previous?: readonly string[];
49
+ previous: readonly string[];
46
50
  }
47
51
  /** One entry of the chain, as read back. */
48
52
  export interface FsChainEntry {
@@ -78,37 +82,22 @@ export declare const createFsChainTables: (db: Db, treeKey: string) => Promise<v
78
82
  * The append-only chain for one folder.
79
83
  *
80
84
  * Append-only and never merged: each node keeps its own lineage, exactly as
81
- * mongo does. Nothing here rewrites or prunes an entry — that is WP5's job.
85
+ * mongo does. Nothing here rewrites or prunes an entry, and nothing collects
86
+ * one either — the chain grows for the life of the folder. See
87
+ * `README.public.md`, "Known constraints".
82
88
  */
83
89
  export declare class FsEditChain {
84
90
  private readonly _db;
85
91
  private readonly _treeKey;
86
- private _head;
87
92
  /** The last own-lineage walk, keyed by the head it was computed for. */
88
93
  private _lineageCache;
89
- private _ready;
90
94
  constructor(_db: Db, _treeKey: string);
91
- /** The entry this node's lineage currently ends at. */
92
- get head(): string | undefined;
93
- /** Whether {@link init} has completed. */
94
- get ready(): boolean;
95
95
  /**
96
- * Creates the tables and continues this node's lineage where it left off.
97
- *
98
- * A process that came back and started a fresh root would orphan everything
99
- * it wrote before, so the tip is read from the store rather than assumed
100
- * absent.
96
+ * Creates the chain's tables if absent. Where this node stands is not the
97
+ * chain's business: a table several nodes write has no tip of its own, and
98
+ * the agent keeps its head in its state file.
101
99
  */
102
100
  init(): Promise<void>;
103
- /**
104
- * Re-reads the tip, for a node waiting to learn the network's state.
105
- *
106
- * `init` reads it once. A joining node has none of its own and is waiting
107
- * for the fleet's to replicate, so it has to be able to look again — the
108
- * rows arrive after the connection, not before it.
109
- * @returns The tip now, or `undefined` while nothing has arrived.
110
- */
111
- refreshHead(): Promise<string | undefined>;
112
101
  /**
113
102
  * Appends one entry.
114
103
  * @param opts - What the change was; see {@link FsAppendOptions}.
@@ -123,78 +112,38 @@ export declare class FsEditChain {
123
112
  */
124
113
  entry(head: string): Promise<FsChainEntry | undefined>;
125
114
  /**
126
- * The newest entry that produced a given tree ref.
127
- *
128
- * **The migration bridge.** A build that predates the `~H~` announcement
129
- * sends a plain tree ref, and a receiver holding one cannot find its chain
130
- * row by hash — `dataRef` is the field, but finding a row BY a field is a
131
- * query rather than a content read. Measured across a real relay
132
- * (`test/fs-chain-crosses-the-wire.spec.ts`): the query is served, so an
133
- * older peer's push can still carry ancestry.
134
- *
135
- * **Ambiguous by nature, which is why it is the fallback and not the
136
- * primary.** A tree ref is a content hash, so a folder that returns to an
137
- * earlier state produces a SECOND entry with the same `dataRef` — §2.1,
138
- * exactly the ambiguity the chain exists to remove. The newest by `timeId`
139
- * is the right pick: it is the entry that most recently produced this
140
- * content, and the one whose ancestry describes how the folder got here now.
141
- *
142
- * Never called on the apply path. A query is a peer read, and awaiting one
143
- * before scheduling an apply is how a late joiner's bootstrap was lost.
144
- * @param treeRef - The state announced.
145
- * @returns The newest entry producing it, or `undefined` if none is found.
146
- */
147
- /**
148
- * The OLDEST entry that produced a given tree ref.
149
- *
150
- * **How a fleet agrees on one name for one state.** Every node authors an
151
- * entry for the state it starts in, and those states are usually identical —
152
- * peers already in sync hold the same bytes, so they derive the same tree
153
- * ref. Each node then sits on its own lineage root and `classify` answers
154
- * `fork` to every announcement forever.
155
- *
156
- * The oldest entry is the one to converge on because the choice is the same
157
- * for everybody who can see the rows, and because adoption that only ever
158
- * moves BACKWARDS in time is monotone: it cannot oscillate as rows arrive.
159
- * Whoever reached this content first described it first.
160
- *
161
- * The counterpart of {@link entryForTreeRef}, which answers the opposite
162
- * question — *"how did this folder get here NOW"* — and therefore wants the
163
- * newest.
164
- * @param treeRef - The state to name.
165
- * @returns The oldest entry producing it, or `undefined` if none is found.
115
+ * How two heads stand, and for a fork the ancestor to merge against.
116
+ *
117
+ * `same` — the one head. `behind` — theirs descends from ours. `ahead` —
118
+ * ours descends from theirs. A descent found is proof, whatever else in
119
+ * either history cannot be read. `fork` — neither descends from the other:
120
+ * `base` is a common ancestor no other common ancestor descends from, the
121
+ * smallest ref of them when there are several, so every node merging the
122
+ * same two heads merges against the same base. `undefined` when the two
123
+ * share no history within the walk. `incomplete` — no descent was found and
124
+ * an entry could not be read, so a fork cannot be told from a lag.
125
+ *
126
+ * A walk that exhausts `maxWalk` with everything readable answers `fork`:
127
+ * less precise than the truth, never destructive.
128
+ * @param ours - This node's head.
129
+ * @param theirs - The head that arrived.
130
+ * @param maxWalk - Give up past this many entries per side.
131
+ * @returns The relation, and the merge base of a fork.
166
132
  */
167
- oldestEntryForTreeRef(treeRef: string): Promise<FsChainEntry | undefined>;
168
- entryForTreeRef(treeRef: string): Promise<FsChainEntry | undefined>;
133
+ relate(ours: string, theirs: string, maxWalk?: number): Promise<{
134
+ verdict: 'same' | 'behind' | 'ahead' | 'incomplete';
135
+ } | {
136
+ verdict: 'fork';
137
+ base: string | undefined;
138
+ }>;
169
139
  /**
170
- * Where two heads stand relative to each other.
171
- *
172
- * `behind` — theirs descends from ours, so we are the one missing work.
173
- * `ahead` — ours descends from theirs; they are missing ours.
174
- * `fork` — neither descends from the other. Both sides have work.
175
- * `incomplete` — an entry could not be RESOLVED, so nothing may be concluded.
176
- *
177
- * **A hole and the walk bound are not the same truncation**, and treating
178
- * them alike was a latent permanent failure. A hole means an entry exists and
179
- * this node cannot read it: the ref it was looking for may be inside the part
180
- * it could not reach, so the honest answer is "I do not know". Exhausting the
181
- * walk bound means the opposite — everything asked for WAS readable, and no
182
- * relation was found within {@link DEFAULT_MAX_WALK} entries of history.
183
- *
184
- * Conflating them meant any node more than `DEFAULT_MAX_WALK` pushes into its
185
- * own history answered `incomplete` forever, which the decision turns into
186
- * `blocked`, which never repairs. A long-lived node would simply stop
187
- * healing, silently. Measured on the growth run that was meant to be about
188
- * storage.
189
- *
190
- * A bounded walk with no relation therefore answers `fork`: both sides keep
191
- * their work and reconciliation is additive. Less precise than the truth and
192
- * never destructive, which is the right direction to be wrong in.
193
- * @param ourHead - This node's head, or `undefined` when it has none.
194
- * @param theirHead - The head that arrived.
195
- * @returns How the two histories stand.
140
+ * The `previous` of every entry reachable from `head`, by generation.
141
+ * @param head - Where to start.
142
+ * @param maxWalk - Give up past this many entries.
143
+ * @returns Each entry read, mapped to its parents, and whether an entry
144
+ * could not be read.
196
145
  */
197
- classify(ourHead: string | undefined, theirHead: string): Promise<'behind' | 'ahead' | 'fork' | 'incomplete'>;
146
+ private _parentsOf;
198
147
  /**
199
148
  * Every entry reachable from `head`, itself included.
200
149
  *
@@ -205,7 +154,7 @@ export declare class FsEditChain {
205
154
  *
206
155
  * `bounded` — the walk ran out of budget with everything it asked for
207
156
  * readable. A different statement, and a usable one: no relation within this
208
- * many entries of history. See {@link classify}.
157
+ * many entries of history. See {@link relate}.
209
158
  * @param head - Where to start.
210
159
  * @param maxWalk - Give up past this many entries.
211
160
  * @returns The refs reached, whether anything was unreadable, and whether
@@ -298,42 +247,17 @@ export declare class FsEditChain {
298
247
  * @param maxWalk - Give up past this many entries. A walk this deep is a
299
248
  * cold replay rather than a catch-up, and left unbounded it pins a core on
300
249
  * a long chain.
301
- * **Both halves of the net delta are returned**, and the second one is not
302
- * bookkeeping. A receiver holds a TOMBSTONE for every path it has deleted,
303
- * including the ones a peer told it to delete, and that tombstone refuses
304
- * the path if anyone writes it again. Only a peer that STATES it created
305
- * the path may lift it — a tree merely containing the path states nothing,
306
- * since every tree that predates the deletion contains it too. Measured: a
307
- * file created, deleted and created again at the same path never reached the
308
- * second node, in three runs of three, because nothing could clear the
309
- * tombstone the node had set on its peer's authority.
310
- * @returns The net removals, the net changes, the newest `timeId` seen, and
311
- * whether the walk resolved completely.
250
+ * **Both halves of the net delta are returned**: a path deleted and created
251
+ * again inside the walked range is a change, not a removal, and only the
252
+ * second half can say so.
253
+ * @returns The net removals, the net changes, and whether the walk resolved
254
+ * completely.
312
255
  */
313
256
  collectRemovals(head: string, ourHead: string | undefined, maxWalk?: number): Promise<{
314
257
  removed: string[];
315
258
  changed: string[];
316
- timeId?: string;
317
259
  complete: boolean;
318
260
  }>;
319
- /**
320
- * The newest entry nothing else descends from.
321
- *
322
- * **A single slot, and that is a known limit.** Every node keeps its own
323
- * lineage and chains are never merged, so once the walk starts pulling peers'
324
- * rows (WP3) this table holds several tips and "the newest" can be somebody
325
- * else's. `@rljson/mongo-agent` had exactly this bug — lost updates
326
- * root-caused to a single `_lastApplied` slot rather than per-node lineages —
327
- * and fixed it by tracking lineages. Nothing reads this chain yet, so the
328
- * limit is not reachable; WP3 must replace it rather than build on it.
329
- *
330
- * "Newest" is by `timeId`, which is `<millis>:<nanoid>` — so two entries
331
- * minted in the same millisecond are separated by a random tail. That order
332
- * is arbitrary in wall-clock terms and IDENTICAL on every node, which is the
333
- * property that matters; nothing may depend on it meaning "later in time".
334
- * @returns The tip's ref, or `undefined` on an empty chain.
335
- */
336
- private _tip;
337
261
  /**
338
262
  * Pulls the generated ref out of an insert result.
339
263
  * @param result - What `db.add*` returned.
@@ -342,144 +266,6 @@ export declare class FsEditChain {
342
266
  */
343
267
  private _refOf;
344
268
  }
345
- /**
346
- * Orders two `timeId`s (`<millis>:<nanoid>`).
347
- *
348
- * Compares the millisecond part numerically and breaks ties on the random
349
- * suffix, so the order is TOTAL and IDENTICAL on every node — which is the
350
- * property that makes convergence a guarantee rather than a matter of who
351
- * spoke last. It is not "later in wall-clock time": two ids minted in the same
352
- * millisecond are separated by a random tail, and nothing may read the result
353
- * as a timestamp comparison.
354
- *
355
- * A missing or malformed id orders as "not comparable" (`0`), so a caller
356
- * cannot accidentally treat "unknown" as "older".
357
- *
358
- * Same semantics as `@rljson/mongo-agent`'s `compareTimeId`, restated here
359
- * rather than depended on: fs must not take a dependency on mongo-agent, which
360
- * already depends on fs-agent.
361
- * @param a - The first timeId, or `undefined`.
362
- * @param b - The second timeId, or `undefined`.
363
- * @returns `-1` if `a` is older, `1` if newer, `0` when equal or not
364
- * comparable.
365
- */
366
- export declare const compareTimeId: (a: string | undefined, b: string | undefined) => number;
367
- /** What {@link planRemovals} is asked. */
368
- export interface RemovalQuestion {
369
- /** Relative paths the peer says it deleted. */
370
- removed: readonly string[];
371
- /** The `timeId` of the edit carrying them. */
372
- timeId: string;
373
- /**
374
- * `path → timeId` of the newest LOCAL edit that touched each path.
375
- *
376
- * A path absent from this map has no local claim, and a removal for it is
377
- * applied — "unknown" must never read as "older".
378
- */
379
- localTimeIds: ReadonlyMap<string, string>;
380
- /**
381
- * `path → timeId` of the last edit the CHAIN knows for that path.
382
- *
383
- * Consulted only where {@link localTimeIds} has nothing, which is the case
384
- * that matters: a node holding a file it RECEIVED rather than authored has
385
- * no claim on it — a receiver adopts and never authors, deliberately — so
386
- * without this the ordering question had no answer and an old removal won
387
- * by default.
388
- *
389
- * Optional, because the chain is best effort. Absent, the behaviour is
390
- * exactly what it was: only this node's own claims can outrank a removal.
391
- */
392
- chainTimeIds?: ReadonlyMap<string, string>;
393
- /** Relative paths this node currently holds. */
394
- held: ReadonlySet<string>;
395
- /**
396
- * Paths this node HOLDS but has never told anyone about — local work made
397
- * since its last announcement.
398
- *
399
- * **A removal cannot be about a file its author has never seen.** A peer's
400
- * stated removal travels with the chain and is re-collected by every later
401
- * walk, so it keeps arriving after it was first applied. If this node
402
- * re-creates the path in the meantime, that creation has no `localTimeIds`
403
- * entry yet — claims are recorded by the PUSH, which has not happened — so
404
- * the recency rule above cannot see it and the removal deletes a file the
405
- * user has just made.
406
- *
407
- * Measured as `I7b`: three nodes, a deletion everyone applied, then a
408
- * different node re-creates the path. The re-created file was removed again
409
- * before its author could announce it, and the fleet ended up split, with
410
- * the deleter holding the file and the author who re-made it without it.
411
- * Found by the random-churn fuzzer at roughly one run in eight; the named
412
- * reproduction is deterministic in 33 seconds.
413
- *
414
- * Absent means "ask the rules below", so an omitted set changes nothing.
415
- */
416
- unannounced?: ReadonlySet<string>;
417
- /**
418
- * How many paths the SAME edit claims.
419
- *
420
- * It counts towards what the folder would be left with, because a rename
421
- * drops every old name and claims every new one — a folder that is renamed
422
- * does not shrink, and reading the removals alone makes one look like a
423
- * wipe. Default 0, which is what a pure deletion is.
424
- */
425
- claims?: number;
426
- /** Below this many removals, nothing is bounded. */
427
- minFiles: number;
428
- /**
429
- * Above `minFiles`, the largest fraction of `held` one removal may take.
430
- */
431
- maxRatio: number;
432
- }
433
- /** What {@link planRemovals} decided about a peer's deletions. */
434
- export interface RemovalPlan {
435
- /** Paths to delete here. */
436
- apply: string[];
437
- /**
438
- * Paths refused because this node has NEWER work on them.
439
- *
440
- * A removal is a statement about a state. A node that has since re-created
441
- * the path has moved past that state, and applying the removal would undo
442
- * newer work on the authority of an older edit.
443
- */
444
- staler: string[];
445
- /**
446
- * Whether the whole set was refused for being too large.
447
- *
448
- * The mass-delete circuit breaker, applied here for the same reason
449
- * `@rljson/mongo-agent` applies it to tombstone application: an explicit
450
- * removal list bypasses the ancestry check by design, so it must not also
451
- * bypass the bound on how much one message may destroy.
452
- */
453
- blocked: boolean;
454
- }
455
- /**
456
- * Which of a peer's deletions to apply here.
457
- *
458
- * **Why an explicit list is needed at all.** A tree records what a folder
459
- * holds, so a deletion reaches a peer only as an absence — and an absence is
460
- * indistinguishable from a state that merely predates the file. fs infers the
461
- * difference from ancestry (`senderSawMyState`), which works only while the
462
- * ancestor can be resolved. A partitioned node's ancestor cannot, and that is
463
- * the measured data loss: the deleted file comes back on the node that deleted
464
- * it. A removal carried in the chain is a FACT rather than an inference, and it
465
- * is the authorisation ancestry could not supply.
466
- *
467
- * **Which is exactly why it is bounded twice.** It deletes without asking the
468
- * ancestry rule, so recency and volume are the only guards left:
469
- *
470
- * - **recency**, by `timeId`. Minted once by the node that made the edit, so
471
- * every node orders the same pair the same way. A removal older than this
472
- * node's own newest edit to that path is refused.
473
- * - **volume**, by the mass-delete circuit breaker. Below
474
- * `minFiles` nothing is bounded — emptying a three-file folder is an
475
- * ordinary edit — and above it a removal of more than `maxRatio` of what
476
- * this node holds is refused wholesale.
477
- * @param opts - The removal and this node's state; see
478
- * {@link RemovalQuestion}.
479
- * @returns What to delete, what was refused as stale, and whether the whole
480
- * set was blocked.
481
- */
482
- export declare const planRemovals: (opts: RemovalQuestion) => RemovalPlan;
483
269
  /**
484
270
  * Above this many files, a removal covering the WHOLE folder is refused.
485
271
  *
@@ -570,13 +356,13 @@ export interface JoinPlan {
570
356
  * **The chain applies first; the filesystem only then.** A joining node used to
571
357
  * author a lineage root from whatever it happened to hold and push it as the
572
358
  * network's newest claim. That is one defect wearing two faces: every node got
573
- * its own lineage root, so `classify` answered `fork` to every announcement
359
+ * its own lineage root, so `relate` answered `fork` to every announcement
574
360
  * ever made; and a node restored from a backup pushed deleted files back to the
575
361
  * whole fleet.
576
362
  *
577
363
  * Every question here is answered against the chain. The folder is consulted
578
364
  * only for what it holds — never for what that means.
579
365
  * @param q - The head, the folder, and what the history says was removed.
580
- * @returns The per-path plan; every path appears in at most one bucket.
366
+ * @returns The per-path plan; every path appears in at most one list.
581
367
  */
582
368
  export declare const planJoin: (q: JoinQuestion) => JoinPlan;
@@ -0,0 +1,83 @@
1
+ import { Db } from '@rljson/db';
2
+ import { RefStamp } from '@rljson/rljson';
3
+ import { FsChainEntry } from './fs-edit-chain.ts';
4
+ /** One entry of the history, as a person is shown it. */
5
+ export interface FsHistoryEntry extends FsChainEntry {
6
+ /**
7
+ * `edit` — a change one node made; `merge` — two lineages joined, `previous`
8
+ * names both; `root` — where a lineage began.
9
+ */
10
+ kind: 'edit' | 'merge' | 'root';
11
+ /**
12
+ * When the authoring node made the edit, epoch ms by THAT node's clock.
13
+ * Absent for a merge and a root: both are derived from their content so
14
+ * every node writes the same entry, and carry no time.
15
+ */
16
+ at?: number;
17
+ /**
18
+ * The announcement that first carried the entry to the fleet, as this node
19
+ * learned it — the order no clock decides. Absent until this node learned
20
+ * it, which for its own newest edit is a round trip to the hub.
21
+ */
22
+ stamp?: RefStamp;
23
+ }
24
+ /** What {@link walkFsHistory} lists. */
25
+ export interface FsHistoryOptions {
26
+ /** The entry to start from. The agent defaults it to its own head. */
27
+ from?: string;
28
+ /**
29
+ * Where the previous page stopped: its {@link FsHistoryPage.cursor}. Wins
30
+ * over `from`.
31
+ */
32
+ cursor?: string;
33
+ /**
34
+ * How many entries one page holds. Default {@link FS_HISTORY_PAGE}, at most
35
+ * {@link DEFAULT_MAX_WALK}.
36
+ */
37
+ limit?: number;
38
+ }
39
+ /** One page of a history. */
40
+ export interface FsHistoryPage {
41
+ /** Newest first. */
42
+ entries: FsHistoryEntry[];
43
+ /**
44
+ * Pass back as `cursor` for the next page. Absent when nothing is left to
45
+ * list. Opaque: its shape may change in any release.
46
+ */
47
+ cursor?: string;
48
+ /**
49
+ * Entries this page reached as a `previous` and could not read — history
50
+ * this node does not hold. Nothing older than them can be listed, and a host
51
+ * should say so rather than show the list as complete.
52
+ */
53
+ missing: string[];
54
+ }
55
+ /** How many entries a page holds when the caller does not say. */
56
+ export declare const FS_HISTORY_PAGE = 50;
57
+ /** How a walk reads the chain: one entry, and the stamp it has. */
58
+ export interface FsHistorySource {
59
+ /** One entry, or `undefined` when this node cannot read it. */
60
+ entry(head: string): Promise<FsChainEntry | undefined>;
61
+ /** The entry's stamp, or `undefined` while this node does not know it. */
62
+ stamp(head: string): RefStamp | undefined;
63
+ }
64
+ /**
65
+ * One page of a chain's history, newest first. See the file header for the
66
+ * order.
67
+ * @param source - How to read the chain.
68
+ * @param options - Where to start, or continue, and how much to list.
69
+ * @returns The page.
70
+ */
71
+ export declare const walkFsHistory: (source: FsHistorySource, options: FsHistoryOptions) => Promise<FsHistoryPage>;
72
+ /**
73
+ * The history of a chain in a database this process does not own — the store
74
+ * of an fs-agent running in another process, opened read-only by its host.
75
+ *
76
+ * **Writes nothing.** No table is created: the stamps are read when their
77
+ * table is there and are absent when it is not.
78
+ * @param db - The database holding the chain.
79
+ * @param treeKey - The route's trees table key.
80
+ * @param options - Where to start, or continue, and how much to list.
81
+ * @returns The page.
82
+ */
83
+ export declare const readFsHistory: (db: Db, treeKey: string, options: FsHistoryOptions) => Promise<FsHistoryPage>;
@@ -0,0 +1,109 @@
1
+ import { RefStamp } from '@rljson/rljson';
2
+ /** `path → blobId`, with {@link DIR_MARKER} for a directory. */
3
+ export type ContentMap = ReadonlyMap<string, string>;
4
+ /** The value a directory has in a {@link ContentMap}. */
5
+ export declare const DIR_MARKER = "<dir>";
6
+ /** Who introduced one side's version of a path, and when it reached the fleet. */
7
+ export interface Introducer {
8
+ /** The chain entry that introduced the version. */
9
+ entry: string;
10
+ /** Its stamp, or `undefined` while nobody has told this node. */
11
+ stamp: RefStamp | undefined;
12
+ /** The node that authored the entry. Names a conflicted copy. */
13
+ nodeId: string;
14
+ }
15
+ /** One side of a merge. */
16
+ export interface MergeSide {
17
+ /** The side's tree. */
18
+ content: ContentMap;
19
+ /** Who introduced this side's version of `path`. */
20
+ introducer: (path: string) => Introducer;
21
+ }
22
+ /** A conflict the merge settled, and the signal it owes. */
23
+ export interface MergeConflict {
24
+ /** `conflict/arbitrary` for C3 and C4, `collision/type` for C5. */
25
+ kind: 'conflict/arbitrary' | 'collision/type';
26
+ /** The contested path. */
27
+ path: string;
28
+ /** Where the version that lost the name was kept, when one was. */
29
+ copyPath?: string;
30
+ }
31
+ /** What {@link merge3} decided. */
32
+ export interface MergeOutcome {
33
+ /** The merged tree, conflicted copies included. */
34
+ merged: Map<string, string>;
35
+ /** Every conflict the merge settled — one signal each. */
36
+ conflicts: MergeConflict[];
37
+ /**
38
+ * Paths whose C3 verdict needs a stamp this node does not know yet. When
39
+ * non-empty the caller must not apply `merged`: it defers and signals
40
+ * `repair/blocked`, and merges again once the stamp has arrived.
41
+ */
42
+ unknownStamps: string[];
43
+ }
44
+ /**
45
+ * Merges two concurrent versions of a folder against their common ancestor.
46
+ *
47
+ * Per path, with `a` the ancestor's value and `b`/`c` the two sides':
48
+ *
49
+ * | rows | situation | outcome |
50
+ * | --- | --- | --- |
51
+ * | C1/C2 | one side changed, or both changed it the same way | that value |
52
+ * | C3 | both changed one file differently | both kept; the version whose introducing entry has the higher stamp keeps the name |
53
+ * | C4 | one side deleted, the other edited | the edit stays; the deletion is reported, not applied |
54
+ * | C5 | a directory on one side, a file on the other | both kept: the directory keeps the path, the file becomes a conflicted copy |
55
+ *
56
+ * Nothing compares two machines' clocks, and no version is ever dropped.
57
+ * @param base - The common ancestor's tree.
58
+ * @param ours - This node's side.
59
+ * @param theirs - The other side.
60
+ * @returns The merged tree, the conflicts it settled, and the paths it could
61
+ * not decide yet.
62
+ */
63
+ export declare const merge3: (base: ContentMap, ours: MergeSide, theirs: MergeSide) => MergeOutcome;
64
+ /**
65
+ * The name a version that lost a contested path is kept under:
66
+ * `<stem> (conflicted copy <hub>-<n> <nodeId>)<ext>`, leaving out what is not
67
+ * known — the stamp, the node — and numbered when taken.
68
+ *
69
+ * A function of the loser's author and stamp only, so every node that settles
70
+ * the same conflict picks the same name.
71
+ * @param path - The contested path.
72
+ * @param nodeId - The node that introduced the losing version.
73
+ * @param stamp - Its stamp, when the name should carry it.
74
+ * @param taken - Paths already in use; the chosen name is added.
75
+ * @returns The copy's path.
76
+ */
77
+ export declare const copyName: (path: string, nodeId: string, stamp: RefStamp | undefined, taken: Set<string>) => string;
78
+ /**
79
+ * The name a joining folder's own version of a path is set aside under when
80
+ * the network holds a different one, numbered when taken:
81
+ *
82
+ * `<stem> (conflicted copy <UTC time> local)<ext>`
83
+ *
84
+ * No stamp can order it — the version never reached the fleet — so the name
85
+ * says when it was set aside and that it is this machine's.
86
+ * @param path - The path.
87
+ * @param at - When it was set aside, epoch ms.
88
+ * @param taken - Paths already in use; the chosen name is added.
89
+ * @returns The copy's path.
90
+ */
91
+ export declare const joinCopyName: (path: string, at: number, taken: Set<string>) => string;
92
+ /**
93
+ * The name a file is set aside under when the history DELETED its path:
94
+ * `<stem> (recovered)<ext>`, numbered when taken.
95
+ *
96
+ * **Not a conflict copy, and the difference is who is told.** A conflict copy
97
+ * is live work on a live path and is announced, because the network needs it.
98
+ * A recovered file is content the fleet deliberately removed — a folder
99
+ * restored from last month's backup, or a client that was away while a
100
+ * directory was deleted — and announcing it would push every one of those
101
+ * deletions back to every node, under names nobody deleted. So it is kept
102
+ * where its owner can see it and nothing is said about it. No time and no
103
+ * identity: the path is simply gone from the history, and one name per path is
104
+ * what a user can act on.
105
+ * @param path - The path the history removed.
106
+ * @param taken - Paths already in use; the chosen name is added.
107
+ * @returns The set-aside path.
108
+ */
109
+ export declare const recoveredName: (path: string, taken: Set<string>) => string;