fix: un second doc_subscribe tuait le premier, et les inbox n'étaient lues qu'à la connexion

This commit is contained in:
Sylvain Duchesne
2026-08-17 11:11:04 +02:00
parent 6dfdf2f036
commit 935cce4d7b
14 changed files with 1284 additions and 115 deletions
+79 -6
View File
@@ -125,6 +125,23 @@ export interface FakeWallet {
/** Present only under {@link WalletOptions.unsyncedUntilSubscribed}. */
doc_subscribe?: ReturnType<typeof mock>;
_quads: Quad[];
/**
* A commit made in ANOTHER session, reaching this page now — the broker delivering what
* it was holding. The quads land in the wallet and each document they touch pushes to
* its subscriber, which is what a remote write does here: verified against the real
* broker, a second session's write reached the first session's subscription as a `Patch`
* 12ms after it landed (`e2e/reactivity-doc-subscribe.ts`, CROSS).
*
* It delivers; it does not INVENT. A caller hands it quads the library itself produced
* under the other actor's identity — never a shape a test wrote by hand.
*/
_deliver: (arriving: Quad[]) => void;
/**
* Anchors whose anchored READ throws, as an unreachable repo does. Mutable after boot,
* so a suite builds a healthy world first and breaks only the one call it is about —
* the fault is the broker's, never a reach into the library to make it reject.
*/
_failReadsOn: Set<string>;
}
export interface WalletOptions {
@@ -168,6 +185,38 @@ export function makeWallet(quads: Quad[], options: WalletOptions = {}): FakeWall
const synced = new Set<string>();
const cold = options.unsyncedUntilSubscribed === true;
/**
* The ONE subscriber a document can have.
*
* Not a convenience — it is what the broker does. A branch holds exactly one sender
* (`branch_subscriptions: HashMap<BranchId, Sender<AppResponse>>`) and
* `create_branch_subscription` closes whatever it displaces, so a second
* `doc_subscribe` on a document does not join the first, it EVICTS it — silently, with
* the evicted unsubscribe still callable and no error anywhere. Confirmed against the
* real broker on 2026-08-17: with two subscriptions on one document, a write fired the
* second callback and the first, which had been firing moments before, went quiet.
*
* A Set here would fabricate a world where every subscriber coexists — precisely the
* assumption whose falseness cost this package a view that never re-read and an inbox
* that never notified.
*/
const subscriber = new Map<string, (r: unknown) => void>();
/** See {@link FakeWallet._failReadsOn}. */
const failReadsOn = new Set<string>();
/** A commit on `g` pushes a `Patch` to that document's subscriber — the SESSION THAT
* WROTE IT INCLUDED. Verified against the real broker the same day: a session's own
* `sparqlUpdate` to a document it subscribes to pushed `Patch@69ms`. The engine keys
* its senders by branch and knows nothing about who issued the write. */
const commit = (g: string): void => {
const cb = subscriber.get(g);
if (!cb) return;
setTimeout(() => {
if (subscriber.get(g) === cb) cb({ V0: { Patch: {} } });
}, 0);
};
const doc_create = mock(async () => {
const nuri = `did:ng:o:doc${++minted}`;
// Created here: nothing remote to wait for. This is why the session that wrote the
@@ -180,11 +229,19 @@ export function makeWallet(quads: Quad[], options: WalletOptions = {}): FakeWall
const nuri = a[0] as string;
const onChange = a[2] as (r: unknown) => void;
synced.add(nuri);
subscriber.set(nuri, onChange);
// `TabInfo` first, then the initial `State` — the platform's own order, so a waiter
// that resolved on "the first push of any kind" would return BEFORE the barrier.
setTimeout(() => onChange({ V0: { TabInfo: {} } }), 0);
setTimeout(() => onChange({ V0: { State: {} } }), 0);
return () => {};
// Only while this callback still holds the branch: an evicted subscriber hears nothing.
setTimeout(() => {
if (subscriber.get(nuri) === onChange) onChange({ V0: { TabInfo: {} } });
}, 0);
setTimeout(() => {
if (subscriber.get(nuri) === onChange) onChange({ V0: { State: {} } });
}, 0);
return () => {
if (subscriber.get(nuri) === onChange) subscriber.delete(nuri);
};
});
const sparql_update = mock(async (...a: unknown[]) => {
@@ -199,6 +256,7 @@ export function makeWallet(quads: Quad[], options: WalletOptions = {}): FakeWall
const q = quads[i]!;
if (q.g === anchor && q.s === pattern[1] && q.p === pattern[2]) quads.splice(i, 1);
}
commit(anchor);
}
return undefined;
}
@@ -210,6 +268,7 @@ export function makeWallet(quads: Quad[], options: WalletOptions = {}): FakeWall
? wrapped[2]!
: query.replace(/^[\s\S]*?INSERT\s+DATA\s*\{/i, "").replace(/\}\s*$/, "");
for (const t of parseTriples(body)) quads.push({ g, ...t });
commit(g);
return undefined;
});
@@ -221,6 +280,12 @@ export function makeWallet(quads: Quad[], options: WalletOptions = {}): FakeWall
// The repo the verifier resolves the read against — the anchor when there is one,
// otherwise the graph named in the query.
const target = anchor ?? g;
// The repo this broker cannot answer for. Rejects, as `resolve_target_for_sparql`
// does on a repo the verifier does not have — never 0 rows, which would be the
// altogether different (and silent) cold-start state modelled below.
if (target !== undefined && failReadsOn.has(target)) {
throw new Error(`RepoNotFound: ${target}`);
}
// COLD: present but unsynced. No error, no rows — which is exactly why it is dangerous.
if (cold && target !== undefined && !synced.has(target)) return { results: { bindings: [] } };
const inGraph = quads.filter((q) => q.g === g);
@@ -299,9 +364,17 @@ export function makeWallet(quads: Quad[], options: WalletOptions = {}): FakeWall
return { results: { bindings: [] } };
});
return cold
? { doc_create, doc_subscribe, sparql_update, sparql_query, _quads: quads }
: { doc_create, sparql_update, sparql_query, _quads: quads };
const _deliver = (arriving: Quad[]): void => {
const touched = new Set<string>();
for (const q of arriving) {
quads.push(q);
touched.add(q.g);
}
for (const g of touched) commit(g);
};
const common = { doc_create, sparql_update, sparql_query, _quads: quads, _deliver, _failReadsOn: failReadsOn };
return cold ? { ...common, doc_subscribe } : common;
}
/** Wire the library onto `quads` — what a page load does. */