@cello-protocol/daemon 0.0.120 → 0.0.122
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/dist/content-park.d.ts +1 -1
- package/dist/content-park.d.ts.map +1 -1
- package/dist/content-park.js +64 -12
- package/dist/content-park.js.map +1 -1
- package/dist/daemon.d.ts.map +1 -1
- package/dist/daemon.js +46 -32
- package/dist/daemon.js.map +1 -1
- package/dist/document-delivery-transport.d.ts +78 -0
- package/dist/document-delivery-transport.d.ts.map +1 -0
- package/dist/document-delivery-transport.js +109 -0
- package/dist/document-delivery-transport.js.map +1 -0
- package/dist/document-delivery.d.ts +130 -0
- package/dist/document-delivery.d.ts.map +1 -0
- package/dist/document-delivery.js +246 -0
- package/dist/document-delivery.js.map +1 -0
- package/dist/document-engine.d.ts +134 -0
- package/dist/document-engine.d.ts.map +1 -0
- package/dist/document-engine.js +281 -0
- package/dist/document-engine.js.map +1 -0
- package/dist/document-gate.d.ts +139 -0
- package/dist/document-gate.d.ts.map +1 -0
- package/dist/document-gate.js +465 -0
- package/dist/document-gate.js.map +1 -0
- package/dist/document-handshake.d.ts +88 -0
- package/dist/document-handshake.d.ts.map +1 -0
- package/dist/document-handshake.js +239 -0
- package/dist/document-handshake.js.map +1 -0
- package/dist/document-lifecycle.d.ts +104 -0
- package/dist/document-lifecycle.d.ts.map +1 -0
- package/dist/document-lifecycle.js +363 -0
- package/dist/document-lifecycle.js.map +1 -0
- package/dist/document-notify.d.ts +130 -0
- package/dist/document-notify.d.ts.map +1 -0
- package/dist/document-notify.js +313 -0
- package/dist/document-notify.js.map +1 -0
- package/dist/document-reachability.d.ts +42 -0
- package/dist/document-reachability.d.ts.map +1 -0
- package/dist/document-reachability.js +72 -0
- package/dist/document-reachability.js.map +1 -0
- package/dist/document-rejection.d.ts +224 -0
- package/dist/document-rejection.d.ts.map +1 -0
- package/dist/document-rejection.js +374 -0
- package/dist/document-rejection.js.map +1 -0
- package/dist/document-store.d.ts +269 -0
- package/dist/document-store.d.ts.map +1 -0
- package/dist/document-store.js +752 -0
- package/dist/document-store.js.map +1 -0
- package/dist/document-write-path.d.ts +84 -0
- package/dist/document-write-path.d.ts.map +1 -0
- package/dist/document-write-path.js +412 -0
- package/dist/document-write-path.js.map +1 -0
- package/dist/line-lcs.d.ts +51 -0
- package/dist/line-lcs.d.ts.map +1 -0
- package/dist/line-lcs.js +71 -0
- package/dist/line-lcs.js.map +1 -0
- package/dist/reconnect-drain.d.ts +15 -0
- package/dist/reconnect-drain.d.ts.map +1 -0
- package/dist/reconnect-drain.js +51 -0
- package/dist/reconnect-drain.js.map +1 -0
- package/dist/session-node-manager.d.ts +39 -0
- package/dist/session-node-manager.d.ts.map +1 -1
- package/dist/session-node-manager.js +152 -12
- package/dist/session-node-manager.js.map +1 -1
- package/dist/session-relay-client.d.ts +2 -0
- package/dist/session-relay-client.d.ts.map +1 -1
- package/dist/session-relay-client.js +16 -1
- package/dist/session-relay-client.js.map +1 -1
- package/package.json +6 -5
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOD-DOC-DELIVERY-2 — the transport behind `DocumentDeliveryTransport` (§16.4).
|
|
3
|
+
*
|
|
4
|
+
* DELIVERY-1 built the scheduler and the bookkeeping against an injected seam. This is the seam's
|
|
5
|
+
* implementation: the first NON-HANDLER consumer of the session machinery, which is what makes it
|
|
6
|
+
* interesting — every other caller of `SessionNegotiator` runs because an agent asked for
|
|
7
|
+
* something, and this one runs because a document has a pending envelope and nobody is watching.
|
|
8
|
+
*
|
|
9
|
+
* ── REUSE BEFORE OPEN, AND WHY THAT ORDER ─────────────────────────────────────────────────────
|
|
10
|
+
*
|
|
11
|
+
* §16.4: "the daemon uses the most recent active session with that peer or opens one". Reuse first,
|
|
12
|
+
* because opening is the expensive half — a directory negotiation, a dial, and a seal — and a
|
|
13
|
+
* backlog of pending envelopes for one peer would otherwise pay it per envelope. It is also the
|
|
14
|
+
* behaviour an operator expects when they are mid-conversation about the document: the change lands
|
|
15
|
+
* in the same sealed record as the discussion, without anyone passing a session hint.
|
|
16
|
+
*
|
|
17
|
+
* An explicit `sessionHint` overrides the choice but NOT the validation — a hint naming a session
|
|
18
|
+
* that is not active with this peer is refused rather than silently replaced by the daemon's own
|
|
19
|
+
* pick, because the one reason to pass a hint is to control which sealed record the change lands
|
|
20
|
+
* in, and quietly choosing a different one defeats exactly that.
|
|
21
|
+
*
|
|
22
|
+
* ── WHAT AN ACK IS HERE, AND WHAT IT IS NOT ───────────────────────────────────────────────────
|
|
23
|
+
*
|
|
24
|
+
* `sendContent` reports that the content left, or was PARKED at the relay for an offline peer. That
|
|
25
|
+
* is a transport fact. The DoD's ack is a different one — "the peer's daemon confirms admission (or
|
|
26
|
+
* rejection)" — and it can only come from the peer's inbound document handler, which is its own
|
|
27
|
+
* line. So this adapter returns `admitted: null`: SENT, not acked.
|
|
28
|
+
*
|
|
29
|
+
* That third state is not hedging; both two-valued answers are dishonest here. `true` would mark
|
|
30
|
+
* the envelope acknowledged in the log while the peer may never have applied it, and the log being
|
|
31
|
+
* right about what the peer holds is the entire reason pending is derived from it. `false` would
|
|
32
|
+
* count a send that WORKED as a failure and re-send content already in flight — the
|
|
33
|
+
* permanent-redelivery shape this milestone has already fixed once. The worker records the envelope
|
|
34
|
+
* as delivered, leaves it unacked, and asks again on the capped backoff.
|
|
35
|
+
*/
|
|
36
|
+
import { reachabilityFromDiscovery, DiscoveryUnavailableError } from "./document-reachability.js";
|
|
37
|
+
export function createDocumentDeliveryTransport(deps) {
|
|
38
|
+
return {
|
|
39
|
+
async isPeerReachable(peerAgentId) {
|
|
40
|
+
const outcome = await deps.lookupPeer(peerAgentId, `reach-${peerAgentId.slice(0, 8)}`);
|
|
41
|
+
// Throws on everything that is not an answer about the peer — see document-reachability.ts.
|
|
42
|
+
// The worker treats the throw as `lookup_failed` and keeps it out of the offline-peer count.
|
|
43
|
+
const { reachable, unknownAgent } = reachabilityFromDiscovery(outcome);
|
|
44
|
+
if (unknownAgent) {
|
|
45
|
+
// Distinct from "offline": the address does not resolve at all. Same consequence (do not
|
|
46
|
+
// dial), different thing to tell an operator, so it is said rather than folded in.
|
|
47
|
+
deps.logger.warn("document.delivery.peer_unknown", { peerAgentId });
|
|
48
|
+
}
|
|
49
|
+
return reachable;
|
|
50
|
+
},
|
|
51
|
+
async deliver(input) {
|
|
52
|
+
const { peerAgentId, documentId, envelope, sessionHint, correlationId } = input;
|
|
53
|
+
let sessionId;
|
|
54
|
+
let sessionOpened = false;
|
|
55
|
+
const active = deps.activeSessionsWith(deps.agentName, peerAgentId);
|
|
56
|
+
if (sessionHint !== undefined) {
|
|
57
|
+
if (!active.includes(sessionHint)) {
|
|
58
|
+
// Refused, not replaced. The only reason to pass a hint is to control which sealed
|
|
59
|
+
// record the change lands in; quietly substituting the daemon's own pick would defeat
|
|
60
|
+
// exactly that, and it would do so silently.
|
|
61
|
+
return {
|
|
62
|
+
ok: false,
|
|
63
|
+
reason: "document_session_hint_invalid",
|
|
64
|
+
detail: `session ${sessionHint.slice(0, 16)}… is not an active session with ${peerAgentId}, ` +
|
|
65
|
+
`so the change cannot be placed in that record`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
sessionId = sessionHint;
|
|
69
|
+
}
|
|
70
|
+
else if (active.length > 0) {
|
|
71
|
+
sessionId = active[active.length - 1];
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const opened = await deps.openSession(deps.agentName, peerAgentId, correlationId);
|
|
75
|
+
if (!opened.ok) {
|
|
76
|
+
// The upstream reason verbatim. `document_delivery_threw` is reserved for a genuine
|
|
77
|
+
// programming fault; a dial that was refused should say it was refused.
|
|
78
|
+
return { ok: false, reason: opened.reason, detail: opened.guidance };
|
|
79
|
+
}
|
|
80
|
+
sessionId = opened.sessionId;
|
|
81
|
+
sessionOpened = true;
|
|
82
|
+
}
|
|
83
|
+
const { bytes, hash } = deps.encodeEnvelope(envelope);
|
|
84
|
+
const sent = await deps.sendContent(deps.agentName, sessionId, bytes, hash, correlationId);
|
|
85
|
+
if (!sent.ok) {
|
|
86
|
+
return { ok: false, reason: sent.reason, detail: sent.error };
|
|
87
|
+
}
|
|
88
|
+
deps.logger.info("document.delivery.sent", {
|
|
89
|
+
documentId,
|
|
90
|
+
sessionId,
|
|
91
|
+
sessionOpened,
|
|
92
|
+
parked: sent.delivered === false,
|
|
93
|
+
correlationId,
|
|
94
|
+
});
|
|
95
|
+
// SENT, NOT ACKED — `admitted: null`. The content left (or was parked for an offline peer),
|
|
96
|
+
// which is a transport fact; the DoD's ack is the peer's daemon confirming admission, and
|
|
97
|
+
// that answer comes from the inbound document handler, which is its own line.
|
|
98
|
+
//
|
|
99
|
+
// Neither of the two-valued answers is available honestly. `true` would mark the envelope
|
|
100
|
+
// acknowledged in the log while the peer may never have applied it — and the log being right
|
|
101
|
+
// about what the peer holds is the entire reason pending is derived from it. `false` would
|
|
102
|
+
// count a send that WORKED as a failure and re-send content already in flight, which is the
|
|
103
|
+
// permanent-redelivery shape this milestone already fixed once.
|
|
104
|
+
return { ok: true, sessionId, sessionOpened, admitted: null };
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
export { DiscoveryUnavailableError };
|
|
109
|
+
//# sourceMappingURL=document-delivery-transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-delivery-transport.js","sourceRoot":"","sources":["../src/document-delivery-transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAGH,OAAO,EAAE,yBAAyB,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAkClG,MAAM,UAAU,+BAA+B,CAC7C,IAA2B;IAE3B,OAAO;QACL,KAAK,CAAC,eAAe,CAAC,WAAmB;YACvC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,EAAE,SAAS,WAAW,CAAC,KAAK,CAAC,CAAC,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AyB,EAAE,CAAC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOD-DOC-DELIVERY-1 — daemon-autonomous delivery (§16.4).
|
|
3
|
+
*
|
|
4
|
+
* Publish writes the envelope to the log and returns. Delivery is somebody else's job, and that
|
|
5
|
+
* somebody is this worker: it derives what is pending FROM THE LOG, checks the peer is reachable
|
|
6
|
+
* before dialing, opens or reuses a session itself, delivers, and records the ack — with zero agent
|
|
7
|
+
* attention on either end. Two agents in opposite time zones sync overnight without either agent
|
|
8
|
+
* doing anything.
|
|
9
|
+
*
|
|
10
|
+
* ── PENDING IS DERIVED, NEVER HELD ────────────────────────────────────────────────────────────
|
|
11
|
+
*
|
|
12
|
+
* "Unacknowledged envelopes I authored" is a WHERE clause over the log, and that is the whole
|
|
13
|
+
* definition. A queue in memory does not survive a restart — the daemon is a long-running local
|
|
14
|
+
* process that gets restarted routinely — and a queue in its own table is a second source of truth
|
|
15
|
+
* that can disagree with the log about what was sent. The attempt counter and the next-attempt
|
|
16
|
+
* time live on the envelope row for the same reason: a backoff that resets on restart is not a
|
|
17
|
+
* backoff, and a daemon restarting in a reconnect loop would hammer an unreachable peer at full
|
|
18
|
+
* rate forever.
|
|
19
|
+
*
|
|
20
|
+
* ── WHY LOOKUP BEFORE DIAL ────────────────────────────────────────────────────────────────────
|
|
21
|
+
*
|
|
22
|
+
* §16.4's design is presence-driven push, but there is no presence subscription today (parked,
|
|
23
|
+
* M14-P4). Without one, the honest substitute is: ask the directory whether the peer is reachable,
|
|
24
|
+
* and if it is not, do not burn a dial — schedule a retry on a capped backoff. Dialing an offline
|
|
25
|
+
* peer to find out it is offline is the same information at much higher cost, and it is the cost
|
|
26
|
+
* that would be paid on every pending envelope, on every tick, for as long as the peer is away.
|
|
27
|
+
*/
|
|
28
|
+
import type { DocumentStore, DocumentEnvelopeRow } from "./document-store.js";
|
|
29
|
+
import type { Logger } from "./types.js";
|
|
30
|
+
/**
|
|
31
|
+
* The transport seam. Narrow on purpose (M4 rule: add to an interface only when a failing test or
|
|
32
|
+
* a production behaviour requires it) — the worker's job is scheduling and bookkeeping, and every
|
|
33
|
+
* dial-level concern belongs to the adapter behind this.
|
|
34
|
+
*/
|
|
35
|
+
export interface DocumentDeliveryTransport {
|
|
36
|
+
/**
|
|
37
|
+
* Is the peer reachable right now? `discovery_lookup` today. Returns false rather than throwing
|
|
38
|
+
* for an ordinary "not online"; a throw means the LOOKUP failed, which is a different fact and
|
|
39
|
+
* must not be recorded as the peer being away.
|
|
40
|
+
*/
|
|
41
|
+
isPeerReachable(peerAgentId: string): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Deliver one envelope over a session the transport opens or reuses (§16.4: daemon-chooses by
|
|
44
|
+
* default; `sessionHint` is the one case with audit value — the agent is mid-conversation about
|
|
45
|
+
* the document and wants the discussion and the change in one sealed record).
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* `ok` means the peer's daemon ANSWERED about this envelope — it is not "the peer liked it".
|
|
49
|
+
* `admitted: false` is a rejection (§3.2's `0x05`), and a rejection IS an ack for delivery
|
|
50
|
+
* purposes: the peer has decided, so there is nothing left to retry. Without the distinction the
|
|
51
|
+
* adapter has to map a rejection onto one of two lies — `ok: true` makes the delivery record say
|
|
52
|
+
* the peer admitted content it refused, and `ok: false` redelivers an envelope the peer has
|
|
53
|
+
* already ruled on, forever, re-triggering their gate and their retry counter until the document
|
|
54
|
+
* stalls for reasons the operator cannot see.
|
|
55
|
+
*
|
|
56
|
+
* `sessionOpened` distinguishes a session this delivery opened from one it reused — the audit
|
|
57
|
+
* distinction §16.4 cares about, and unrecoverable after the fact.
|
|
58
|
+
*/
|
|
59
|
+
deliver(input: {
|
|
60
|
+
peerAgentId: string;
|
|
61
|
+
documentId: string;
|
|
62
|
+
envelope: DocumentEnvelopeRow;
|
|
63
|
+
sessionHint?: string;
|
|
64
|
+
correlationId: string;
|
|
65
|
+
}): Promise<{
|
|
66
|
+
ok: true;
|
|
67
|
+
sessionId: string;
|
|
68
|
+
sessionOpened: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* `true` admitted, `false` rejected — both are ACKS, the peer has decided. `null` means the
|
|
71
|
+
* envelope LEFT (or was parked for an offline peer) and no answer has come back yet.
|
|
72
|
+
*
|
|
73
|
+
* The third state is not hedging; both two-valued answers are dishonest for a send whose
|
|
74
|
+
* outcome is unknown. `true` marks the envelope acknowledged in the log while the peer may
|
|
75
|
+
* never have applied it — and the log being right about what the peer holds is the entire
|
|
76
|
+
* reason pending is derived from it. `false` counts a send that WORKED as a failure and
|
|
77
|
+
* re-sends content already in flight.
|
|
78
|
+
*/
|
|
79
|
+
admitted: boolean | null;
|
|
80
|
+
rejectionReason?: string;
|
|
81
|
+
} | {
|
|
82
|
+
ok: false;
|
|
83
|
+
reason: string;
|
|
84
|
+
detail?: string;
|
|
85
|
+
}>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Backoff schedule in ms: ~1s, 5s, 30s, 2m, 10m, then the last entry repeats.
|
|
89
|
+
*
|
|
90
|
+
* The final entry IS the cap — there is no separate ceiling constant. There was one, set to 900s,
|
|
91
|
+
* and it was unreachable: the index clamp meant the schedule never produced a value above 600s, so
|
|
92
|
+
* the exported "cap" was a number the code could not emit and the tests were using it as a synonym
|
|
93
|
+
* for "much later". A documented limit the implementation cannot reach is worse than none.
|
|
94
|
+
*
|
|
95
|
+
* Capped at all because the peer coming back online is the event we are waiting for and it can
|
|
96
|
+
* happen at any moment — an uncapped curve leaves a peer that returned after a long absence waiting
|
|
97
|
+
* hours for a delivery that has been ready the whole time.
|
|
98
|
+
*/
|
|
99
|
+
export declare const DELIVERY_BACKOFF_MS: readonly [1000, 5000, 30000, 120000, 600000];
|
|
100
|
+
export declare const DELIVERY_BACKOFF_CAP_MS: 5000 | 1000 | 30000 | 120000 | 600000;
|
|
101
|
+
/**
|
|
102
|
+
* A document whose peer cannot be resolved is not transient, so it gets the cap immediately rather
|
|
103
|
+
* than climbing to it — but it DOES get scheduled. See the `no_peer` branch.
|
|
104
|
+
*/
|
|
105
|
+
export declare const DELIVERY_UNRESOLVABLE_RETRY_MS: 5000 | 1000 | 30000 | 120000 | 600000;
|
|
106
|
+
export declare function backoffFor(attempts: number): number;
|
|
107
|
+
export interface DeliveryTickResult {
|
|
108
|
+
attempted: number;
|
|
109
|
+
delivered: number;
|
|
110
|
+
/** Sent (or parked) with no answer yet — in flight, neither done nor failed. */
|
|
111
|
+
sent: number;
|
|
112
|
+
/** Answered but refused. Acked all the same — the peer has decided. */
|
|
113
|
+
rejected: number;
|
|
114
|
+
deferred: number;
|
|
115
|
+
failed: number;
|
|
116
|
+
}
|
|
117
|
+
export declare class DocumentDelivery {
|
|
118
|
+
#private;
|
|
119
|
+
constructor(store: DocumentStore, transport: DocumentDeliveryTransport, logger: Logger);
|
|
120
|
+
/**
|
|
121
|
+
* One pass over what is due. Returns counts rather than throwing on a per-envelope failure: one
|
|
122
|
+
* unreachable peer must not stop delivery to every other peer, which is precisely the "works
|
|
123
|
+
* only when all nodes are healthy" shape the project forbids.
|
|
124
|
+
*/
|
|
125
|
+
tick(ownerAgentId: string, peerFor: (documentId: string) => string | null, nowMs: number, opts?: {
|
|
126
|
+
sessionHint?: string;
|
|
127
|
+
correlationId?: string;
|
|
128
|
+
}): Promise<DeliveryTickResult>;
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=document-delivery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-delivery.d.ts","sourceRoot":"","sources":["../src/document-delivery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;GAIG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;;OAIG;IACH,eAAe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EAAE,MAAM,EACpB,OAAO,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,EAC9C,KAAK,EAAE,MAAM,EACb,IAAI,GAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAA;KAAO,GAC1D,OAAO,CAAC,kBAAkB,CAAC;CA4M/B"}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOD-DOC-DELIVERY-1 — daemon-autonomous delivery (§16.4).
|
|
3
|
+
*
|
|
4
|
+
* Publish writes the envelope to the log and returns. Delivery is somebody else's job, and that
|
|
5
|
+
* somebody is this worker: it derives what is pending FROM THE LOG, checks the peer is reachable
|
|
6
|
+
* before dialing, opens or reuses a session itself, delivers, and records the ack — with zero agent
|
|
7
|
+
* attention on either end. Two agents in opposite time zones sync overnight without either agent
|
|
8
|
+
* doing anything.
|
|
9
|
+
*
|
|
10
|
+
* ── PENDING IS DERIVED, NEVER HELD ────────────────────────────────────────────────────────────
|
|
11
|
+
*
|
|
12
|
+
* "Unacknowledged envelopes I authored" is a WHERE clause over the log, and that is the whole
|
|
13
|
+
* definition. A queue in memory does not survive a restart — the daemon is a long-running local
|
|
14
|
+
* process that gets restarted routinely — and a queue in its own table is a second source of truth
|
|
15
|
+
* that can disagree with the log about what was sent. The attempt counter and the next-attempt
|
|
16
|
+
* time live on the envelope row for the same reason: a backoff that resets on restart is not a
|
|
17
|
+
* backoff, and a daemon restarting in a reconnect loop would hammer an unreachable peer at full
|
|
18
|
+
* rate forever.
|
|
19
|
+
*
|
|
20
|
+
* ── WHY LOOKUP BEFORE DIAL ────────────────────────────────────────────────────────────────────
|
|
21
|
+
*
|
|
22
|
+
* §16.4's design is presence-driven push, but there is no presence subscription today (parked,
|
|
23
|
+
* M14-P4). Without one, the honest substitute is: ask the directory whether the peer is reachable,
|
|
24
|
+
* and if it is not, do not burn a dial — schedule a retry on a capped backoff. Dialing an offline
|
|
25
|
+
* peer to find out it is offline is the same information at much higher cost, and it is the cost
|
|
26
|
+
* that would be paid on every pending envelope, on every tick, for as long as the peer is away.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Backoff schedule in ms: ~1s, 5s, 30s, 2m, 10m, then the last entry repeats.
|
|
30
|
+
*
|
|
31
|
+
* The final entry IS the cap — there is no separate ceiling constant. There was one, set to 900s,
|
|
32
|
+
* and it was unreachable: the index clamp meant the schedule never produced a value above 600s, so
|
|
33
|
+
* the exported "cap" was a number the code could not emit and the tests were using it as a synonym
|
|
34
|
+
* for "much later". A documented limit the implementation cannot reach is worse than none.
|
|
35
|
+
*
|
|
36
|
+
* Capped at all because the peer coming back online is the event we are waiting for and it can
|
|
37
|
+
* happen at any moment — an uncapped curve leaves a peer that returned after a long absence waiting
|
|
38
|
+
* hours for a delivery that has been ready the whole time.
|
|
39
|
+
*/
|
|
40
|
+
export const DELIVERY_BACKOFF_MS = [1_000, 5_000, 30_000, 120_000, 600_000];
|
|
41
|
+
export const DELIVERY_BACKOFF_CAP_MS = DELIVERY_BACKOFF_MS[DELIVERY_BACKOFF_MS.length - 1];
|
|
42
|
+
/**
|
|
43
|
+
* A document whose peer cannot be resolved is not transient, so it gets the cap immediately rather
|
|
44
|
+
* than climbing to it — but it DOES get scheduled. See the `no_peer` branch.
|
|
45
|
+
*/
|
|
46
|
+
export const DELIVERY_UNRESOLVABLE_RETRY_MS = DELIVERY_BACKOFF_CAP_MS;
|
|
47
|
+
export function backoffFor(attempts) {
|
|
48
|
+
const idx = Math.min(Math.max(attempts, 0), DELIVERY_BACKOFF_MS.length - 1);
|
|
49
|
+
return DELIVERY_BACKOFF_MS[idx];
|
|
50
|
+
}
|
|
51
|
+
export class DocumentDelivery {
|
|
52
|
+
#store;
|
|
53
|
+
#transport;
|
|
54
|
+
#logger;
|
|
55
|
+
#inFlight = null;
|
|
56
|
+
constructor(store, transport, logger) {
|
|
57
|
+
this.#store = store;
|
|
58
|
+
this.#transport = transport;
|
|
59
|
+
this.#logger = logger;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* One pass over what is due. Returns counts rather than throwing on a per-envelope failure: one
|
|
63
|
+
* unreachable peer must not stop delivery to every other peer, which is precisely the "works
|
|
64
|
+
* only when all nodes are healthy" shape the project forbids.
|
|
65
|
+
*/
|
|
66
|
+
async tick(ownerAgentId, peerFor, nowMs, opts = {}) {
|
|
67
|
+
// NO RE-ENTRY. Nothing marks a row in-flight until its outcome lands, so a `deliver` slower
|
|
68
|
+
// than the tick interval — a dial to a distant peer is exactly that — would have the next tick
|
|
69
|
+
// return the same rows and dial again: two autonomous sessions and two seals for one envelope,
|
|
70
|
+
// and the loser's markAcked returning false, which logs as if the PEER had redelivered an ack.
|
|
71
|
+
if (this.#inFlight)
|
|
72
|
+
return this.#inFlight;
|
|
73
|
+
const run = this.#run(ownerAgentId, peerFor, nowMs, opts);
|
|
74
|
+
this.#inFlight = run;
|
|
75
|
+
try {
|
|
76
|
+
return await run;
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
this.#inFlight = null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async #run(ownerAgentId, peerFor, nowMs, opts) {
|
|
83
|
+
// Minted once per pass and threaded through every event, so an operator can tie a failure back
|
|
84
|
+
// to the lookup that preceded it and the session that carried it. Delivery is precisely the
|
|
85
|
+
// async multi-step flow the convention exists for: lookup, dial, deliver, ack — across ticks
|
|
86
|
+
// and across restarts.
|
|
87
|
+
const correlationId = opts.correlationId ?? `dlv-${ownerAgentId.slice(0, 8)}-${nowMs}`;
|
|
88
|
+
const pending = this.#store.pendingDeliveries(ownerAgentId, nowMs);
|
|
89
|
+
const result = {
|
|
90
|
+
attempted: 0, delivered: 0, sent: 0, rejected: 0, deferred: 0, failed: 0,
|
|
91
|
+
};
|
|
92
|
+
// Group by document so one reachability lookup serves every pending envelope for that peer.
|
|
93
|
+
// Per-envelope lookups would multiply directory traffic by the size of the backlog, which is
|
|
94
|
+
// largest exactly when the peer has been away longest.
|
|
95
|
+
const byDocument = new Map();
|
|
96
|
+
for (const e of pending) {
|
|
97
|
+
const list = byDocument.get(e.documentId);
|
|
98
|
+
if (list)
|
|
99
|
+
list.push(e);
|
|
100
|
+
else
|
|
101
|
+
byDocument.set(e.documentId, [e]);
|
|
102
|
+
}
|
|
103
|
+
for (const [documentId, envelopes] of byDocument) {
|
|
104
|
+
const peerAgentId = peerFor(documentId);
|
|
105
|
+
if (peerAgentId === null) {
|
|
106
|
+
// No peer means the document row is gone or malformed, which is NOT transient — so it goes
|
|
107
|
+
// straight to the cap rather than climbing to it, and crucially it is SCHEDULED. Skipping
|
|
108
|
+
// the schedule made this the one exit that never self-corrects: the rows stayed due on
|
|
109
|
+
// every tick forever, emitting an error line each time, and — because the pending window
|
|
110
|
+
// is ordered and bounded — they filled it and starved every other document. The operator's
|
|
111
|
+
// work on an unrelated document would silently never leave the machine, with the only
|
|
112
|
+
// signal being an error naming a different one.
|
|
113
|
+
this.#logger.error("document.delivery.no_peer", {
|
|
114
|
+
documentId,
|
|
115
|
+
pending: envelopes.length,
|
|
116
|
+
correlationId,
|
|
117
|
+
});
|
|
118
|
+
for (const e of envelopes) {
|
|
119
|
+
this.#store.recordDeliveryAttempt(ownerAgentId, documentId, e.envelopeHash, nowMs + DELIVERY_UNRESOLVABLE_RETRY_MS);
|
|
120
|
+
}
|
|
121
|
+
result.failed += envelopes.length;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
let reachable;
|
|
125
|
+
try {
|
|
126
|
+
reachable = await this.#transport.isPeerReachable(peerAgentId);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
// A FAILED LOOKUP IS NOT AN OFFLINE PEER. Recording it as "away" would report a directory
|
|
130
|
+
// outage to the operator as their collaborator being absent — an error substituted for a
|
|
131
|
+
// different error, and one that sends them to ask the wrong person.
|
|
132
|
+
this.#logger.warn("document.delivery.lookup_failed", {
|
|
133
|
+
documentId,
|
|
134
|
+
peerAgentId,
|
|
135
|
+
correlationId,
|
|
136
|
+
reason: err instanceof Error ? err.message : String(err),
|
|
137
|
+
});
|
|
138
|
+
this.#defer(ownerAgentId, documentId, envelopes, nowMs);
|
|
139
|
+
result.deferred += envelopes.length;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (!reachable) {
|
|
143
|
+
this.#logger.info("document.delivery.peer_unreachable", {
|
|
144
|
+
documentId,
|
|
145
|
+
peerAgentId,
|
|
146
|
+
pending: envelopes.length,
|
|
147
|
+
correlationId,
|
|
148
|
+
});
|
|
149
|
+
this.#defer(ownerAgentId, documentId, envelopes, nowMs);
|
|
150
|
+
result.deferred += envelopes.length;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
for (const envelope of envelopes) {
|
|
154
|
+
result.attempted += 1;
|
|
155
|
+
// CLAIM THE ROW BEFORE DIALING. Scheduling only on the outcome leaves the row due for the
|
|
156
|
+
// whole duration of the dial, and leaves it due FOREVER if the daemon dies mid-dial. The
|
|
157
|
+
// claim is corrected below when the outcome lands.
|
|
158
|
+
const attempts = this.#store.recordDeliveryAttempt(ownerAgentId, documentId, envelope.envelopeHash, nowMs + backoffFor(envelope.attempts ?? 0));
|
|
159
|
+
let outcome;
|
|
160
|
+
try {
|
|
161
|
+
outcome = await this.#transport.deliver({
|
|
162
|
+
peerAgentId,
|
|
163
|
+
documentId,
|
|
164
|
+
envelope,
|
|
165
|
+
sessionHint: opts.sessionHint,
|
|
166
|
+
correlationId,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
// A THROW IS A FAILURE, never a success. Wrapped here rather than left to propagate so
|
|
171
|
+
// one envelope's transport fault does not abandon the rest of the backlog mid-pass.
|
|
172
|
+
outcome = {
|
|
173
|
+
ok: false,
|
|
174
|
+
reason: "document_delivery_threw",
|
|
175
|
+
detail: err instanceof Error ? err.message : String(err),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (outcome.ok && outcome.admitted === null) {
|
|
179
|
+
// IN FLIGHT. The content left, and the peer has not answered. Recorded as DELIVERED so
|
|
180
|
+
// the operator can see it went, and left unacked so the worker keeps asking — on the
|
|
181
|
+
// capped backoff the claim above already scheduled, not at full rate.
|
|
182
|
+
this.#store.markDelivered(ownerAgentId, documentId, envelope.envelopeHash, nowMs);
|
|
183
|
+
this.#logger.info("document.delivery.sent", {
|
|
184
|
+
documentId,
|
|
185
|
+
envelopeHash: envelope.envelopeHash,
|
|
186
|
+
sessionId: outcome.sessionId,
|
|
187
|
+
sessionOpened: outcome.sessionOpened,
|
|
188
|
+
correlationId,
|
|
189
|
+
});
|
|
190
|
+
result.sent += 1;
|
|
191
|
+
}
|
|
192
|
+
else if (outcome.ok) {
|
|
193
|
+
// ACK = the peer's daemon ANSWERED, admitted or not. A rejection is an ack for delivery
|
|
194
|
+
// purposes (§3.2's 0x05): the peer has decided, so there is nothing left to retry, and
|
|
195
|
+
// retrying would re-trigger their gate and their retry counter until the document stalls
|
|
196
|
+
// for reasons the operator cannot see. Supersession is REJECT-1's job, not the worker's.
|
|
197
|
+
const first = this.#store.markAcked(ownerAgentId, documentId, envelope.envelopeHash, nowMs);
|
|
198
|
+
this.#logger.info("document.delivery.session", {
|
|
199
|
+
documentId,
|
|
200
|
+
sessionId: outcome.sessionId,
|
|
201
|
+
opened: outcome.sessionOpened,
|
|
202
|
+
correlationId,
|
|
203
|
+
});
|
|
204
|
+
if (outcome.admitted) {
|
|
205
|
+
this.#logger.info("document.delivery.acked", {
|
|
206
|
+
documentId,
|
|
207
|
+
envelopeHash: envelope.envelopeHash,
|
|
208
|
+
sessionId: outcome.sessionId,
|
|
209
|
+
firstAck: first,
|
|
210
|
+
correlationId,
|
|
211
|
+
});
|
|
212
|
+
result.delivered += 1;
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
this.#logger.warn("document.delivery.rejected", {
|
|
216
|
+
documentId,
|
|
217
|
+
envelopeHash: envelope.envelopeHash,
|
|
218
|
+
sessionId: outcome.sessionId,
|
|
219
|
+
reason: outcome.rejectionReason,
|
|
220
|
+
correlationId,
|
|
221
|
+
});
|
|
222
|
+
result.rejected += 1;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
this.#logger.warn("document.delivery.failed", {
|
|
227
|
+
documentId,
|
|
228
|
+
envelopeHash: envelope.envelopeHash,
|
|
229
|
+
reason: outcome.reason,
|
|
230
|
+
detail: outcome.detail,
|
|
231
|
+
attempts,
|
|
232
|
+
correlationId,
|
|
233
|
+
});
|
|
234
|
+
result.failed += 1;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
#defer(ownerAgentId, documentId, envelopes, nowMs) {
|
|
241
|
+
for (const e of envelopes) {
|
|
242
|
+
this.#store.recordDeliveryAttempt(ownerAgentId, documentId, e.envelopeHash, nowMs + backoffFor(e.attempts ?? 0));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=document-delivery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-delivery.js","sourceRoot":"","sources":["../src/document-delivery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AA8DH;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAU,CAAC;AACrF,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAC,mBAAmB,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAE3F;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,uBAAuB,CAAC;AAEtE,MAAM,UAAU,UAAU,CAAC,QAAgB;IACzC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG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,CAAC,CAAC;gBACrB,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,CACJ,YAAoB,EACpB,UAAkB,EAClB,SAAyC,EACzC,KAAa;QAEb,KAAK,MAAM,CAAC,IAAI,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,MAAM,CAAC,qBAAqB,CAC/B,YAAY,EACZ,UAAU,EACV,CAAC,CAAC,YAAY,EACd,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,QAAQ,IAAI,CAAC,CAAC,CACpC,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOD-DOC-ENGINE-1 — the daemon's Y.Doc lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* The engine owns HOW to apply; the store (DOD-DOC-STORE-1) owns what to replay and in what
|
|
5
|
+
* order. `replay` takes whole envelope rows rather than payload bytes so it can tell a
|
|
6
|
+
* payload-free AUDIT record (a withdrawal or rejection, which is expected and skipped) from a
|
|
7
|
+
* payload-free PURGED update (an operation whose bytes are gone, which must refuse). That is the
|
|
8
|
+
* only thing `kind` is read for — `referencesEnvelopeHash` is pure audit at replay time, never
|
|
9
|
+
* read, because a withdrawal excludes nothing (§16.4; see `replay`).
|
|
10
|
+
*
|
|
11
|
+
* EVERY GUARD BELOW IS A RESPONSE TO A MEASUREMENT, not to a guess. DOD-DOC-FUZZ-1 fuzzed
|
|
12
|
+
* `Y.applyUpdate` and found:
|
|
13
|
+
*
|
|
14
|
+
* - Malformed input THROWS — so a wrapped apply genuinely contains it, and V1 needs no sandbox.
|
|
15
|
+
* - But the dangerous class is what Yjs ACCEPTS. An update whose dependencies the receiver
|
|
16
|
+
* lacks returns success, contributes nothing, and is RETAINED forever in
|
|
17
|
+
* `doc.store.pendingStructs` — a peer streams those until the daemon dies, and a try/catch
|
|
18
|
+
* sees only success. Hence the pending-set check after every apply.
|
|
19
|
+
* - An empty or one-byte update throws a lib0 DECODER error ("Unexpected end of array"), which
|
|
20
|
+
* names Yjs internals rather than a protocol fault. Hence a floor as well as a cap.
|
|
21
|
+
* - Yjs does not bound nesting depth at all, and the size cap bounds it poorly (~16 bytes per
|
|
22
|
+
* level, so roughly 65,000 levels fit in 1 MiB). Structural limits are DOD-DOC-GATE-1's job;
|
|
23
|
+
* the engine's contract is only that a bad update is a typed error, never a crash.
|
|
24
|
+
*
|
|
25
|
+
* ONE TYPED REASON PER FAILURE CLASS. A lib0 string like "Integer out of Range" describes where
|
|
26
|
+
* the decoder gave up, not what the peer did wrong, so it travels as `detail` and never as the
|
|
27
|
+
* reason an operator or a policy log sees.
|
|
28
|
+
*/
|
|
29
|
+
import * as Y from "yjs";
|
|
30
|
+
import type { DocumentEnvelopeRow } from "./document-store.js";
|
|
31
|
+
import type { Logger } from "./types.js";
|
|
32
|
+
export type DocumentUpdateFailure = "document_update_too_large" | "document_update_too_small" | "document_update_malformed" | "document_update_unresolved_dependencies" | "document_snapshot_malformed" | "document_snapshot_incomplete" | "document_envelope_purged";
|
|
33
|
+
export type DocumentUpdateResult = {
|
|
34
|
+
ok: true;
|
|
35
|
+
}
|
|
36
|
+
/** `detail` is the underlying cause — never the reason itself. */
|
|
37
|
+
| {
|
|
38
|
+
ok: false;
|
|
39
|
+
reason: DocumentUpdateFailure;
|
|
40
|
+
detail?: string;
|
|
41
|
+
};
|
|
42
|
+
/** Thrown by the *OrThrow variants and by `replay`, where returning a verdict would let a caller
|
|
43
|
+
* persist a state that was never fully applied. */
|
|
44
|
+
export declare class DocumentUpdateError extends Error {
|
|
45
|
+
readonly reason: DocumentUpdateFailure;
|
|
46
|
+
readonly detail?: string;
|
|
47
|
+
constructor(reason: DocumentUpdateFailure, detail?: string);
|
|
48
|
+
}
|
|
49
|
+
export declare class DocumentEngine {
|
|
50
|
+
#private;
|
|
51
|
+
constructor(logger: Logger);
|
|
52
|
+
/** The pre-parse cap, exposed so callers and tests agree on one number. */
|
|
53
|
+
get maxUpdateBytes(): number;
|
|
54
|
+
/**
|
|
55
|
+
* A fresh live document.
|
|
56
|
+
*
|
|
57
|
+
* Yjs mints its own random clientID and NOTHING here touches it (§14). Deriving it from agent
|
|
58
|
+
* identity, or persisting and restoring one, means two live docs can share it — and
|
|
59
|
+
* DOD-DOC-FUZZ-1 measured that outcome: the colliding writer silently wins, the honest client's
|
|
60
|
+
* update is accepted-and-dropped, and the document becomes a splice of two authors with an
|
|
61
|
+
* empty pending set and no error on any path.
|
|
62
|
+
*/
|
|
63
|
+
createDocument(startingContent?: string): Y.Doc;
|
|
64
|
+
/**
|
|
65
|
+
* Read the single text root a text-typed document uses.
|
|
66
|
+
*
|
|
67
|
+
* Named for the root it reads, not for "the document's content". `DocumentRow.documentType`
|
|
68
|
+
* admits markdown, json and xml; on the latter two the data does not live in this root, and an
|
|
69
|
+
* unnamed `readText` would return "" — indistinguishable from an empty document. Structured
|
|
70
|
+
* types get their own accessors when a unit needs them.
|
|
71
|
+
*/
|
|
72
|
+
readTextRoot(doc: Y.Doc): string;
|
|
73
|
+
insertIntoTextRoot(doc: Y.Doc, index: number, text: string): void;
|
|
74
|
+
/** The full state, or just what a peer holding `sinceStateVector` is missing (§7). */
|
|
75
|
+
encodeState(doc: Y.Doc, sinceStateVector?: Uint8Array): Uint8Array;
|
|
76
|
+
encodeStateVector(doc: Y.Doc): Uint8Array;
|
|
77
|
+
snapshot(doc: Y.Doc): {
|
|
78
|
+
binary: Uint8Array;
|
|
79
|
+
stateVector: Uint8Array;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Materialize a document from a snapshot binary.
|
|
83
|
+
*
|
|
84
|
+
* The restored document mints a FRESH clientID — the binary carries the operations, never an
|
|
85
|
+
* identity to resume under. See `createDocument` for what sharing one costs.
|
|
86
|
+
*/
|
|
87
|
+
restore(binary: Uint8Array): Y.Doc;
|
|
88
|
+
/**
|
|
89
|
+
* Apply one update, with every guard the fuzz pass motivated.
|
|
90
|
+
*
|
|
91
|
+
* Never throws and never leaves the document half-integrated: an update that cannot be fully
|
|
92
|
+
* resolved is applied to a THROWAWAY doc first, so the caller's document is untouched when the
|
|
93
|
+
* answer is no.
|
|
94
|
+
*/
|
|
95
|
+
/**
|
|
96
|
+
* Apply one update to a caller's live document, leaving it untouched if the answer is no.
|
|
97
|
+
*
|
|
98
|
+
* The trial runs on a FRESH scratch document every time. Reusing one across calls is unsound:
|
|
99
|
+
* `Y.applyUpdate` MERGES, it does not reset, so a scratch doc seeded from an empty document
|
|
100
|
+
* still holds the previous trial's operations — and with that residue present, an update whose
|
|
101
|
+
* dependencies the target lacks reports an EMPTY pending set. The one guard that catches the
|
|
102
|
+
* accept class would go green on exactly the input it exists to refuse. (Measured, not
|
|
103
|
+
* reasoned: a shadow holding "AAA" re-seeded from an empty doc still reads "AAA".)
|
|
104
|
+
*/
|
|
105
|
+
applyUpdate(doc: Y.Doc, update: Uint8Array): DocumentUpdateResult;
|
|
106
|
+
/** `applyUpdate` for callers that want the failure to be unmissable. */
|
|
107
|
+
applyUpdateOrThrow(doc: Y.Doc, update: Uint8Array): void;
|
|
108
|
+
/**
|
|
109
|
+
* Fold an ordered envelope log into a materialized state — the `ReplayFn` the store injects.
|
|
110
|
+
*
|
|
111
|
+
* **A WITHDRAWAL EXCLUDES NOTHING.** §16.4 is explicit: withdrawing rolls the change back with
|
|
112
|
+
* a *Yjs undo* and writes a record "beside the original envelope — marked withdrawn, never
|
|
113
|
+
* deleted, so the log stays intact". Rejection resolves the same way, by supersession —
|
|
114
|
+
* "inverses, not erasure" (§3.2). So the undo is itself an ordinary update in the log, and
|
|
115
|
+
* replay simply applies every payload in order.
|
|
116
|
+
*
|
|
117
|
+
* An earlier version of this method excluded the referenced envelope instead, which is
|
|
118
|
+
* unsound in a CRDT log and was measured to be so: Yjs operations are causally chained, so
|
|
119
|
+
* dropping any but the LAST envelope leaves every later one depending on structs that never
|
|
120
|
+
* arrive — the document rebuilds until the next daemon restart and is permanently unopenable
|
|
121
|
+
* after it. It also let ANY sender suppress ANY other sender's content by appending a
|
|
122
|
+
* payload-free row, since nothing upstream checks authorship of a reference. Both problems
|
|
123
|
+
* dissolve when the log is simply replayed, which is what the spec said to do.
|
|
124
|
+
*
|
|
125
|
+
* REFUSES rather than skipping. A payload that will not apply means the log is corrupt, and
|
|
126
|
+
* folding the rest would produce a document that reads as complete while missing operations —
|
|
127
|
+
* the silent divergence the whole two-layer design exists to prevent.
|
|
128
|
+
*/
|
|
129
|
+
replay(envelopes: readonly DocumentEnvelopeRow[]): {
|
|
130
|
+
binary: Uint8Array;
|
|
131
|
+
stateVector: Uint8Array;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=document-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-engine.d.ts","sourceRoot":"","sources":["../src/document-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,MAAM,qBAAqB,GAC7B,2BAA2B,GAC3B,2BAA2B,GAC3B,2BAA2B,GAC3B,yCAAyC,GACzC,6BAA6B,GAC7B,8BAA8B,GAC9B,0BAA0B,CAAC;AAE/B,MAAM,MAAM,oBAAoB,GAC5B;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE;AACd,kEAAkE;GAChE;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,qBAAqB,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAElE;oDACoD;AACpD,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,MAAM,EAAE,qBAAqB,CAAC;IACvC,QAAQ,CAAC,MAAM,C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