app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

inbox-probe-exclusion.spec.ts (45386B)


      1 import { test, expect, type Page } from '@playwright/test';
      2 import { build } from 'vite';
      3 import { once } from 'node:events';
      4 import { fileURLToPath } from 'node:url';
      5 import { WebSocketServer, type WebSocket } from 'ws';
      6 import { createStaticHarness } from '../integration/harness/static.ts';
      7 import type * as Fixture from './harness/inbox-probe-exclusion.ts';
      8 declare global {
      9   interface Window {
     10     qx: typeof Fixture;
     11     qxFixture: Awaited<ReturnType<typeof Fixture.makeExclusionFixture>>;
     12     qxChild: Awaited<ReturnType<typeof Fixture.openRecoveryContext>>;
     13     qxHold: ReturnType<typeof Fixture.holdExclusionNativeCompletion>;
     14     qxPending: Promise<string>;
     15     qxCancellation: AbortController;
     16   }
     17 }
     18 let server: Awaited<ReturnType<typeof createStaticHarness>>,
     19   sockets: WebSocketServer,
     20   endpoint: string,
     21   bundle: string,
     22   head = '';
     23 const physical = new Set<WebSocket>(),
     24   archived = new Map<string, unknown>(),
     25   physicalOrigins = new WeakMap<WebSocket, string>(),
     26   live = new Map<WebSocket, string>();
     27 let receiveFrames: { id: string; created_at: number }[] = [];
     28 let receiveLane: 'none' | 'history' | 'live' | 'both' = 'none';
     29 const requests: {
     30   origin: string;
     31   filter: { kinds?: number[]; ids?: string[]; limit?: number; until?: number };
     32 }[] = [];
     33 const origins = [
     34   'wss://inbox.example.org',
     35   'wss://peer.example.org',
     36   'wss://archive.example.org'
     37 ];
     38 test.beforeAll(async () => {
     39   server = await createStaticHarness();
     40   sockets = new WebSocketServer({
     41     host: '127.0.0.1',
     42     port: 0,
     43     maxPayload: 65536
     44   });
     45   await once(sockets, 'listening');
     46   const address = sockets.address();
     47   if (!address || typeof address === 'string')
     48     throw Error('actual loopback endpoint');
     49   endpoint = 'ws://127.0.0.1:' + address.port;
     50   sockets.on('connection', (socket, request) => {
     51     physical.add(socket);
     52     socket.on('close', () => {
     53       physical.delete(socket);
     54       live.delete(socket);
     55     });
     56     const origin = new URL(request.url!, 'http://127.0.0.1').searchParams.get(
     57       'origin'
     58     )!;
     59     physicalOrigins.set(socket, origin);
     60     socket.on('message', (bytes) => {
     61       const raw = Buffer.isBuffer(bytes)
     62         ? bytes
     63         : Array.isArray(bytes)
     64           ? Buffer.concat(bytes)
     65           : Buffer.from(bytes);
     66       const frame = JSON.parse(raw.toString()) as unknown[];
     67       if (frame[0] === 'REQ') {
     68         const filter = frame[2] as {
     69           kinds?: number[];
     70           ids?: string[];
     71           limit?: number;
     72           until?: number;
     73         };
     74         if (requests.length >= 10000)
     75           throw Error('bounded fixture request log');
     76         requests.push({ origin, filter });
     77         if (filter.kinds?.includes(10050) && head)
     78           socket.send(JSON.stringify(['EVENT', frame[1], JSON.parse(head)]));
     79         if (filter.kinds?.includes(1059)) {
     80           if (filter.ids) {
     81             const event = archived.get(JSON.stringify([origin, filter.ids[0]]));
     82             if (event) socket.send(JSON.stringify(['EVENT', frame[1], event]));
     83           } else {
     84             if (filter.limit === 0) live.set(socket, String(frame[1]));
     85             if (
     86               receiveLane === 'both' ||
     87               (receiveLane === 'history' && filter.limit !== 0) ||
     88               (receiveLane === 'live' && filter.limit === 0)
     89             ) {
     90               for (const event of receiveFrames)
     91                 if (
     92                   filter.until === undefined ||
     93                   event.created_at <= filter.until
     94                 )
     95                   socket.send(JSON.stringify(['EVENT', frame[1], event]));
     96             }
     97           }
     98         }
     99         socket.send(JSON.stringify(['EOSE', frame[1]]));
    100       } else if (frame[0] === 'EVENT') {
    101         const event = frame[1] as { id: string };
    102         archived.set(JSON.stringify([origin, event.id]), event);
    103         socket.send(
    104           JSON.stringify(['OK', event.id, true, 'controlled named acceptance'])
    105         );
    106       }
    107     });
    108   });
    109   const result = await build({
    110     configFile: false,
    111     logLevel: 'silent',
    112     build: {
    113       write: false,
    114       minify: false,
    115       lib: {
    116         entry: fileURLToPath(
    117           new URL('./harness/inbox-probe-exclusion.ts', import.meta.url)
    118         ),
    119         name: 'qx',
    120         formats: ['iife']
    121       }
    122     }
    123   });
    124   const output = Array.isArray(result) ? result[0] : result;
    125   if (!('output' in output)) throw Error('actual compiled exclusion harness');
    126   const chunks = output.output.filter((item) => item.type === 'chunk');
    127   expect(chunks).toHaveLength(1);
    128   bundle = chunks[0].code;
    129 });
    130 test.afterAll(async () => {
    131   for (const socket of sockets.clients) socket.terminate();
    132   await new Promise<void>((resolve) => sockets.close(() => resolve()));
    133   await server.close();
    134 });
    135 test.beforeEach(async ({ page }) => {
    136   await expect.poll(() => physical.size).toBe(0);
    137   head = '';
    138   archived.clear();
    139   live.clear();
    140   requests.length = 0;
    141   receiveFrames = [];
    142   receiveLane = 'none';
    143   await page.addInitScript(
    144     ({ endpoint, origins }) => {
    145       const Native = window.WebSocket;
    146       window.WebSocket = class extends Native {
    147         constructor(url: string | URL, protocols?: string | string[]) {
    148           const origin = new URL(String(url)).origin;
    149           if (!['wss://discovery.example.org', ...origins].includes(origin))
    150             throw Error('unapproved fixture origin');
    151           super(endpoint + '/?origin=' + encodeURIComponent(origin), protocols);
    152         }
    153       };
    154     },
    155     { endpoint, origins }
    156   );
    157   await page.goto(server.url + '/search');
    158   await page.addScriptTag({ content: bundle });
    159   head = await page.evaluate(async (origins) => {
    160     window.qxFixture = await window.qx.makeExclusionFixture();
    161     return window.qxFixture.host.preferenceWire(origins);
    162   }, origins);
    163   await page.evaluate(async () => {
    164     await window.qxFixture.open();
    165     if ((await window.qxFixture.prepareOriginal()).status !== 'prepared')
    166       throw Error('actual original S3/R1 preparation');
    167   });
    168 });
    169 test.afterEach(async ({ page }) => {
    170   if (page.isClosed()) return;
    171   const closed = await page.evaluate(() => {
    172     try {
    173       window.qxFixture.close();
    174       return true;
    175     } catch (error) {
    176       if (
    177         error instanceof Error &&
    178         [
    179           'private_inbox_cleanup_required',
    180           'private_history_cleanup_required',
    181           'private_auth_cleanup_required'
    182         ].includes(error.message)
    183       )
    184         return false;
    185       throw error;
    186     }
    187   });
    188   if (!closed) {
    189     await expect
    190       .poll(
    191         () =>
    192           Array.from(physical).filter(
    193             (socket) =>
    194               physicalOrigins.get(socket) !== 'wss://discovery.example.org'
    195           ).length
    196       )
    197       .toBe(0);
    198     await page.evaluate(() => window.qxFixture.close());
    199   }
    200 });
    201 test('original actual admitted journal excludes exact self ciphertext before received retention', async ({
    202   page
    203 }) => {
    204   const result = await page.evaluate(async () => {
    205     const f = window.qxFixture,
    206       before = f.host.counts();
    207     const status = await f.retain();
    208     return {
    209       status,
    210       before,
    211       after: f.host.counts(),
    212       received: (await f.receivedRows()).length
    213     };
    214   });
    215   expect(result.status).toBe('probe_excluded');
    216   expect(result.received).toBe(0);
    217   expect(result.after).toEqual(result.before);
    218 });
    219 test('original actual C7 loss during native observation uses ordinary owner-current retention', async ({
    220   page
    221 }) => {
    222   const result = await page.evaluate(async () => {
    223     const f = window.qxFixture;
    224     const fault = f.nativeFault('read_loss', () => f.stop());
    225     try {
    226       const status = await f.retain();
    227       return {
    228         status,
    229         native: fault.counts(),
    230         received: (await f.receivedRows()).length
    231       };
    232     } finally {
    233       fault.restore();
    234     }
    235   });
    236   expect(result.native.readonly).toBe(1);
    237   expect(result.status).toBe('retained');
    238   expect(result.received).toBe(1);
    239 });
    240 
    241 for (const recovered of [false, true]) {
    242   const label = recovered ? 'REC' : 'original';
    243   test(`${label} actual readonly exclusion performs zero native mutations`, async ({
    244     page
    245   }) => {
    246     const result = await page.evaluate(async (recovered) => {
    247       const f = window.qxFixture;
    248       if (recovered && (await f.qualify(true)).status !== 'saved')
    249         throw Error('actual REC');
    250       const wire = await f.selfWire(),
    251         before = f.host.counts(),
    252         watch = window.qx.watchExclusionNative();
    253       try {
    254         const status = await f.retain(wire);
    255         return {
    256           status,
    257           native: watch.counts(),
    258           before,
    259           after: f.host.counts()
    260         };
    261       } finally {
    262         watch.restore();
    263       }
    264     }, recovered);
    265     expect(result.status).toBe('probe_excluded');
    266     expect(result.native.readonly).toBeGreaterThan(0);
    267     expect(result.native).toMatchObject({ readwrite: 0, puts: 0, deletes: 0 });
    268     expect(result.after).toEqual(result.before);
    269   });
    270   test(`${label} held actual owner WebLock refuses before native or provider work`, async ({
    271     page
    272   }) => {
    273     const result = await page.evaluate(async (recovered) => {
    274       const f = window.qxFixture;
    275       if (recovered && (await f.qualify(true)).status !== 'saved')
    276         throw Error('actual REC');
    277       const wire = await f.selfWire(),
    278         before = f.host.counts(),
    279         watch = window.qx.watchExclusionNative();
    280       try {
    281         return await navigator.locks.request(
    282           'harvestcircle:owner:' + f.host.owner,
    283           async () => ({
    284             status: await f.retain(wire),
    285             native: watch.counts(),
    286             before,
    287             after: f.host.counts()
    288           })
    289         );
    290       } finally {
    291         watch.restore();
    292       }
    293     }, recovered);
    294     expect(result.status).toBe('busy');
    295     expect(result.native).toEqual({
    296       readonly: 0,
    297       readwrite: 0,
    298       puts: 0,
    299       deletes: 0
    300     });
    301     expect(result.after).toEqual(result.before);
    302   });
    303   for (const loss of ['cancel', 'session', 'C7', 'head'] as const)
    304     test(`${label} actual ${loss} loss while native completion is held cannot exclude`, async ({
    305       page
    306     }) => {
    307       await page.evaluate(async (recovered) => {
    308         const f = window.qxFixture;
    309         if (recovered && (await f.qualify(true)).status !== 'saved')
    310           throw Error('actual REC');
    311       }, recovered);
    312       await page.evaluate(async () => {
    313         const wire = await window.qxFixture.selfWire();
    314         window.qxCancellation = new AbortController();
    315         window.qxHold = window.qx.holdExclusionNativeCompletion();
    316         window.qxPending = window.qxFixture.retain(
    317           wire,
    318           window.qxCancellation.signal
    319         );
    320       });
    321       await expect
    322         .poll(() => page.evaluate(() => window.qxHold.waiting()))
    323         .toBe(true);
    324       const result = await page.evaluate(async (loss) => {
    325         const f = window.qxFixture;
    326         try {
    327           if (loss === 'cancel') window.qxCancellation.abort();
    328           else if (loss === 'session')
    329             window.qx.closePrivateSession(f.actors().session);
    330           else if (loss === 'head') f.actors().churnHead();
    331           else if (loss === 'C7') f.stop();
    332         } finally {
    333           window.qxHold.release();
    334         }
    335         return {
    336           status: await window.qxPending,
    337           received: (await f.receivedRows()).length
    338         };
    339       }, loss);
    340       expect(result.status).toBe(
    341         loss === 'C7' || loss === 'head' ? 'retained' : 'stopped'
    342       );
    343       expect(result.received).toBe(loss === 'C7' || loss === 'head' ? 1 : 0);
    344     });
    345   test(`${label} copied signed id with changed ciphertext fails ordinary admission`, async ({
    346     page
    347   }) => {
    348     const result = await page.evaluate(async (recovered) => {
    349       const f = window.qxFixture;
    350       if (recovered && (await f.qualify(true)).status !== 'saved')
    351         throw Error('actual REC');
    352       const event = JSON.parse(await f.selfWire()) as { content: string };
    353       event.content =
    354         event.content.slice(0, -1) + (event.content.endsWith('A') ? 'B' : 'A');
    355       const before = f.host.counts(),
    356         status = await f.retain(JSON.stringify(event));
    357       return {
    358         status,
    359         before,
    360         after: f.host.counts(),
    361         received: (await f.receivedRows()).length
    362       };
    363     }, recovered);
    364     expect(result.status).toBe('invalid');
    365     expect(result.received).toBe(0);
    366     expect(result.after).toEqual(result.before);
    367   });
    368   test(`${label} copied envelope and different genuine repository session and owner cannot borrow exclusion`, async ({
    369     page
    370   }) => {
    371     const result = await page.evaluate(async (recovered) => {
    372       const q = window.qx,
    373         f = window.qxFixture;
    374       if (recovered && (await f.qualify(true)).status !== 'saved')
    375         throw Error('actual REC');
    376       const wire = await f.selfWire(),
    377         a = f.actors(),
    378         opened = await q.openBrowserDatabase();
    379       if (opened.state !== 'ready') throw Error('actual database');
    380       const other = q.createPrivateStorageRepository(
    381         opened.owner,
    382         f.host.owner
    383       )!;
    384       const identity = q.createIdentitySession();
    385       await q.connectIdentity(identity);
    386       await q.probeIdentityMessaging(identity, 'reviewed_self_copy');
    387       const session = await q.createPrivateSession(
    388         identity,
    389         'reviewed_private_session'
    390       );
    391       if (!session || session === a.session || other === f.repository)
    392         throw Error('actual distinct source actors');
    393       try {
    394         const envelope = q.admitInboxEnvelope(
    395           wire,
    396           f.host.owner,
    397           originsForBrowser(),
    398           Date.now()
    399         )!;
    400         const before = f.host.counts();
    401         const copied = await q.retainInboxEnvelope(f.repository, a.session, {
    402           ...envelope
    403         });
    404         const foreign = await q.retainInboxEnvelope(
    405           f.host.foreignRepository(),
    406           a.session,
    407           envelope
    408         );
    409         const otherRepository = await q.retainInboxEnvelope(
    410           other,
    411           a.session,
    412           envelope
    413         );
    414         const otherSession = await q.retainInboxEnvelope(
    415           f.repository,
    416           session,
    417           envelope
    418         );
    419         return {
    420           copied: copied.status,
    421           foreign: foreign.status,
    422           otherRepository: otherRepository.status,
    423           otherSession: otherSession.status,
    424           before,
    425           after: f.host.counts()
    426         };
    427       } finally {
    428         q.closePrivateSession(session);
    429         q.disconnectIdentity(identity);
    430         q.closeBrowserDatabase(opened.owner);
    431       }
    432       function originsForBrowser() {
    433         return 'wss://inbox.example.org';
    434       }
    435     }, recovered);
    436     expect(result).toMatchObject({
    437       copied: 'invalid',
    438       foreign: 'invalid',
    439       otherRepository: 'retained',
    440       otherSession: 'duplicate'
    441     });
    442     expect(result.after).toEqual(result.before);
    443   });
    444   test(`${label} only a new genuine source admission restores exclusion after observed absence`, async ({
    445     page
    446   }) => {
    447     const result = await page.evaluate(async (recovered) => {
    448       const f = window.qxFixture;
    449       if (recovered && (await f.qualify(true)).status !== 'saved')
    450         throw Error('actual REC');
    451       const wire = await f.selfWire(),
    452         row = await f.rowWire();
    453       await f.removeFixtureRow();
    454       const missing = await f.retain(wire);
    455       await f.replaceRow(row);
    456       const restored = await f.retain(wire);
    457       await f.freshSession();
    458       const recovery = await f.recover();
    459       if (!recovery || recovery.status !== 'recovered')
    460         throw Error('actual fresh recovery');
    461       const adopted = await f.adopt();
    462       if (!('acknowledgement' in adopted)) throw Error('actual fresh adoption');
    463       return {
    464         missing,
    465         restored,
    466         readmitted: await f.retain(wire),
    467         received: (await f.receivedRows()).length
    468       };
    469     }, recovered);
    470     expect(result).toEqual({
    471       missing: 'retained',
    472       restored: 'duplicate',
    473       readmitted: 'probe_excluded',
    474       received: 1
    475     });
    476   });
    477   for (const lane of ['finite', 'sync'] as const)
    478     test(`${label} genuine ${lane} pending queue stays bounded while actual native completion is held`, async ({
    479       page
    480     }) => {
    481       await qualify(page, recovered);
    482       const wire = await page.evaluate(() => window.qxFixture.selfWire());
    483       receiveFrames = Array.from(
    484         { length: 501 },
    485         () => JSON.parse(wire) as (typeof receiveFrames)[number]
    486       );
    487       receiveLane = lane === 'finite' ? 'history' : 'live';
    488       await page.evaluate(() => {
    489         window.qxHold = window.qx.holdExclusionNativeCompletion();
    490       });
    491       try {
    492         expect(
    493           await page.evaluate(
    494             (lane) => window.qxFixture.startReceive(lane),
    495             lane
    496           )
    497         ).toBe(true);
    498         await expect
    499           .poll(() => page.evaluate(() => window.qxHold.waiting()))
    500           .toBe(true);
    501         await expect
    502           .poll(() =>
    503             page.evaluate(() => window.qxFixture.receiveSnapshot()?.reason)
    504           )
    505           .toBe('budget');
    506         const held = await page.evaluate(() =>
    507           window.qxFixture.receiveSnapshot()
    508         );
    509         expect(held!.pending).toBeGreaterThan(0);
    510         expect(held!.pending).toBeLessThanOrEqual(500);
    511         expect(held!.excludedProbes).toBe(0);
    512       } finally {
    513         await page.evaluate(() => window.qxHold.release());
    514       }
    515       await expect
    516         .poll(() =>
    517           page.evaluate(() => window.qxFixture.receiveSnapshot()?.pending)
    518         )
    519         .toBe(0);
    520       expect(
    521         await page.evaluate(
    522           async () => (await window.qxFixture.receivedRows()).length
    523         )
    524       ).toBe(0);
    525     });
    526   test(`${label} repeated full inclusive probe page saturates and actual Stop clears transient cursors`, async ({
    527     page
    528   }) => {
    529     await qualify(page, recovered);
    530     expect(
    531       await page.evaluate(() => window.qxFixture.startReceive('sync'))
    532     ).toBe(true);
    533     await liveReady(page);
    534     const wire = await page.evaluate(() => window.qxFixture.selfWire());
    535     receiveFrames = Array.from(
    536       { length: 200 },
    537       () => JSON.parse(wire) as (typeof receiveFrames)[number]
    538     );
    539     receiveLane = 'history';
    540     expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
    541     expect(
    542       (await page.evaluate(() => window.qxFixture.olderSnapshot()))!.sources[0]
    543         .state
    544     ).toBe('capped');
    545     expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
    546     expect(
    547       (await page.evaluate(() => window.qxFixture.olderSnapshot()))!.sources[0]
    548         .state
    549     ).toBe('saturated');
    550     const count = requests.length;
    551     expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
    552     // Shared500 cap may leave later sources partial; the saturated FIRST
    553     // source itself cannot be requested a third time.
    554     expect(
    555       requests
    556         .slice(count)
    557         .filter((row) => row.origin === origins[0] && row.filter.limit !== 0)
    558     ).toHaveLength(0);
    559     await page.evaluate(() => {
    560       try {
    561         window.qxFixture.stopReceive();
    562       } catch (error) {
    563         if (
    564           !(error instanceof Error) ||
    565           ![
    566             'private_history_cleanup_required',
    567             'private_inbox_cleanup_required'
    568           ].includes(error.message)
    569         )
    570           throw error;
    571       }
    572     });
    573     expect(
    574       (await page.evaluate(() => window.qxFixture.olderSnapshot()))!.sources
    575     ).toEqual([]);
    576     expect(await page.evaluate(() => window.qxFixture.older())).toBe(false);
    577     expect(
    578       await page.evaluate(
    579         async () => (await window.qxFixture.receivedRows()).length
    580       )
    581     ).toBe(0);
    582   });
    583   test(`${label} observed generation loss clears transient excluded cursor metadata`, async ({
    584     page
    585   }) => {
    586     await qualify(page, recovered);
    587     expect(
    588       await page.evaluate(() => window.qxFixture.startReceive('sync'))
    589     ).toBe(true);
    590     await liveReady(page);
    591     const wire = await page.evaluate(() => window.qxFixture.selfWire());
    592     receiveFrames = [JSON.parse(wire) as (typeof receiveFrames)[number]];
    593     receiveLane = 'history';
    594     expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
    595     expect(
    596       (await page.evaluate(() => window.qxFixture.olderSnapshot()))!.sources
    597         .length
    598     ).toBeGreaterThan(0);
    599     await page.evaluate(() => window.qxFixture.actors().churnHead());
    600     expect(await page.evaluate(() => window.qxFixture.older())).toBe(false);
    601     expect(
    602       (await page.evaluate(() => window.qxFixture.olderSnapshot()))!.sources
    603     ).toEqual([]);
    604   });
    605   test(`${label} same current direct probe remains excluded on repeated delivery`, async ({
    606     page
    607   }) => {
    608     const result = await page.evaluate(async (recovered) => {
    609       const f = window.qxFixture;
    610       if (recovered && (await f.qualify(true)).status !== 'saved')
    611         throw Error('actual REC source');
    612       const before = f.host.counts(),
    613         first = await f.retain(),
    614         second = await f.retain();
    615       return {
    616         first,
    617         second,
    618         before,
    619         after: f.host.counts(),
    620         received: (await f.receivedRows()).length
    621       };
    622     }, recovered);
    623     expect(result.first).toBe('probe_excluded');
    624     expect(result.second).toBe('probe_excluded');
    625     expect(result.received).toBe(0);
    626     expect(result.after).toEqual(result.before);
    627   });
    628   test(`${label} fresh actual session without adoption receives stored probe through ordinary decrypt`, async ({
    629     page
    630   }) => {
    631     const result = await page.evaluate(async (recovered) => {
    632       const f = window.qxFixture;
    633       if (recovered && (await f.qualify(true)).status !== 'saved')
    634         throw Error('actual REC source');
    635       const wire = await f.selfWire();
    636       await f.freshSession();
    637       const before = f.host.counts(),
    638         status = await f.retain(wire),
    639         afterRetain = f.host.counts(),
    640         decrypted = await f.decryptStored();
    641       return { status, before, afterRetain, decrypted };
    642     }, recovered);
    643     expect(result.status).toBe('retained');
    644     expect(result.afterRetain).toEqual(result.before);
    645     expect(result.decrypted.snapshot).toMatchObject({
    646       attempted: 1,
    647       invalid: 1,
    648       admitted: 0
    649     });
    650     expect(
    651       result.decrypted.after.decrypts - result.decrypted.before.decrypts
    652     ).toBe(2);
    653   });
    654   test(`${label} actual browser reload loses suppression admission while keeping native journal`, async ({
    655     page
    656   }) => {
    657     const setup = await page.evaluate(() => {
    658       const f = window.qxFixture;
    659       return {
    660         owner: f.host.owner,
    661         policyWire: JSON.stringify(window.qx.readRelayPolicy(f.host.policy)),
    662         operatorWire: window.qx.inboxOperatorQualificationWire(
    663           f.host.qualification,
    664           f.host.policy
    665         )!
    666       };
    667     });
    668     await page.evaluate((url) => {
    669       const child = document.createElement('iframe');
    670       child.id = 'exclusion-realm';
    671       child.src = url;
    672       document.body.append(child);
    673     }, server.url + '/search');
    674     const frame = await (
    675       await page.locator('#exclusion-realm').elementHandle()
    676     ).contentFrame();
    677     if (!frame) throw Error('actual same-origin child');
    678     await frame.waitForLoadState();
    679     await frame.addScriptTag({ content: bundle });
    680     const original = await frame.evaluate(
    681       async ({ setup, recovered }) => {
    682         window.qx.installHostedProvider();
    683         const policy = window.qx.validateRelayPolicy(setup.policyWire)!,
    684           operator = window.qx.validateInboxOperatorQualification(
    685             setup.operatorWire
    686           )!;
    687         const id = crypto.randomUUID();
    688         const context = await window.qx.openRecoveryContext(
    689           setup.owner,
    690           policy,
    691           operator,
    692           id
    693         );
    694         const record = await window.qx.prepareHostedOriginal(context);
    695         if (recovered) {
    696           const result = await context.recover(
    697             context.capture('reviewed_inbox_probe_recovery', record.id)
    698           );
    699           if (!('proof' in result)) throw Error('actual child recovery');
    700           const adopted = await window.qx.adoptRecoveredInboxProbe(
    701             context.repository,
    702             context.session,
    703             context.access,
    704             policy,
    705             operator,
    706             result.proof,
    707             'reviewed_original_inbox_probe_retry'
    708           );
    709           if (!('acknowledgement' in adopted))
    710             throw Error('actual child adoption');
    711         }
    712         const wire = record.self!.wire;
    713         const before = await window.qx.retainInRecoveryContext(context, wire);
    714         context.close();
    715         return { id: record.id, wire, before };
    716       },
    717       { setup, recovered }
    718     );
    719     expect(original.before).toBe('probe_excluded');
    720     await frame.goto(server.url + '/search');
    721     await frame.addScriptTag({ content: bundle });
    722     const before = await page.evaluate(() => window.qxFixture.host.counts());
    723     const result = await frame.evaluate(
    724       async ({ setup, original }) => {
    725         window.qx.installHostedProvider();
    726         window.qxChild = await window.qx.openRecoveryContext(
    727           setup.owner,
    728           window.qx.validateRelayPolicy(setup.policyWire)!,
    729           window.qx.validateInboxOperatorQualification(setup.operatorWire)!,
    730           original.id
    731         );
    732         const status = await window.qx.retainInRecoveryContext(
    733           window.qxChild,
    734           original.wire
    735         );
    736         return { status };
    737       },
    738       { setup, original }
    739     );
    740     const afterRetain = await page.evaluate(() =>
    741       window.qxFixture.host.counts()
    742     );
    743     const decrypted = await frame.evaluate(() =>
    744       window.qx.decryptInRecoveryContext(window.qxChild)
    745     );
    746     const after = await page.evaluate(() => window.qxFixture.host.counts());
    747     await frame.evaluate(() => window.qxChild.close());
    748     expect(
    749       await page.evaluate(
    750         async () => (await window.qxFixture.host.stored()).length
    751       )
    752     ).toBe(2);
    753     expect(result.status).toBe('retained');
    754     // Opening the actual fresh identity performs its existing capability probe.
    755     expect(afterRetain.decrypts - before.decrypts).toBe(1);
    756     expect(afterRetain.signs).toBe(before.signs);
    757     expect(decrypted.snapshot).toMatchObject({
    758       attempted: 1,
    759       invalid: 1,
    760       admitted: 0
    761     });
    762     expect(after.decrypts - afterRetain.decrypts).toBe(2);
    763   });
    764   for (const lane of ['finite', 'sync'] as const)
    765     test(`${label} genuine ${lane} probe flood exhausts original delivery budget before exclusion`, async ({
    766       page
    767     }) => {
    768       await qualify(page, recovered);
    769       const wire = await page.evaluate(() => window.qxFixture.selfWire()),
    770         event = JSON.parse(wire) as (typeof receiveFrames)[number];
    771       receiveFrames = Array.from({ length: 501 }, () => event);
    772       receiveLane = 'history';
    773       const before = await page.evaluate(() => window.qxFixture.host.counts());
    774       expect(
    775         await page.evaluate((lane) => window.qxFixture.startReceive(lane), lane)
    776       ).toBe(true);
    777       await expect
    778         .poll(() =>
    779           page.evaluate(() => window.qxFixture.receiveSnapshot()?.reason)
    780         )
    781         .toBe('budget');
    782       await expect
    783         .poll(() =>
    784           page.evaluate(() => window.qxFixture.receiveSnapshot()?.pending)
    785         )
    786         .toBe(0);
    787       const result = await page.evaluate(async () => ({
    788         snapshot: window.qxFixture.receiveSnapshot(),
    789         received: (await window.qxFixture.receivedRows()).length,
    790         after: window.qxFixture.host.counts()
    791       }));
    792       expect(result.snapshot!.candidates).toBeGreaterThan(0);
    793       expect(result.snapshot!.candidates).toBeLessThanOrEqual(501);
    794       expect(result.snapshot!.bytes).toBe(
    795         result.snapshot!.candidates * Buffer.byteLength(wire)
    796       );
    797       expect(result.snapshot!.excludedProbes).toBeLessThanOrEqual(
    798         result.snapshot!.candidates
    799       );
    800       expect(result.received).toBe(0);
    801       expect(result.after).toEqual({
    802         ...before,
    803         keys: before.keys + (lane === 'sync' ? 2 : 1)
    804       });
    805     });
    806   for (const order of [
    807     'ordinary-first',
    808     'probe-first',
    809     'between-pages'
    810   ] as const)
    811     test(`${label} mixed 1999-row ${order} preserves bounded cursor facts and honest native retention`, async ({
    812       page
    813     }) => {
    814       await qualify(page, recovered);
    815       const nativeUntil = await page.evaluate(() =>
    816         window.qxFixture.seedReceived()
    817       );
    818       expect(
    819         await page.evaluate(() => window.qxFixture.startReceive('sync'))
    820       ).toBe(true);
    821       await liveReady(page);
    822       const wire = await page.evaluate(() => window.qxFixture.selfWire()),
    823         probe = JSON.parse(wire) as (typeof receiveFrames)[number];
    824       const normal = JSON.parse(
    825         await page.evaluate(
    826           ({ recovered, time }) =>
    827             window.qxFixture.ordinaryWire(recovered, time),
    828           { recovered, time: probe.created_at - 1 }
    829         )
    830       ) as (typeof receiveFrames)[number];
    831       receiveFrames = order === 'ordinary-first' ? [normal, probe] : [probe];
    832       if (order === 'probe-first') receiveFrames.push(normal);
    833       receiveLane = 'history';
    834       expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
    835       if (order === 'between-pages') {
    836         expect(
    837           (await page.evaluate(() => window.qxFixture.olderSnapshot()))!.reason
    838         ).toBeNull();
    839         expect(
    840           await page.evaluate(
    841             (wire) => window.qxFixture.retain(wire),
    842             JSON.stringify(normal)
    843           )
    844         ).toBe('retained');
    845         receiveFrames = [];
    846         expect(await page.evaluate(() => window.qxFixture.older())).toBe(false);
    847       }
    848       expect(
    849         await page.evaluate(() => window.qxFixture.olderSnapshot())
    850       ).toMatchObject({
    851         state: 'needs_action',
    852         reason: 'capacity',
    853         historyComplete: false
    854       });
    855       expect(
    856         await page.evaluate(
    857           async () => (await window.qxFixture.receivedRows()).length
    858         )
    859       ).toBe(2000);
    860       await page.evaluate(
    861         (id) => window.qxFixture.deleteReceived(id),
    862         normal.id
    863       );
    864       receiveFrames = [];
    865       expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
    866       const after = await page.evaluate(() => window.qxFixture.olderSnapshot());
    867       if (order === 'ordinary-first') {
    868         expect(after!.sources[0].seen).not.toContain(probe.id);
    869         expect(after!.sources[0].until).toBe(nativeUntil);
    870       } else {
    871         expect(after!.sources[0].seen).toContain(probe.id);
    872         expect(after!.sources[0].until).toBe(probe.created_at);
    873       }
    874       expect(after!.historyComplete).toBe(false);
    875     });
    876 }
    877 
    878 async function qualify(page: Page, recovered: boolean) {
    879   expect(
    880     await page.evaluate(
    881       async (recovered) => (await window.qxFixture.qualify(recovered)).status,
    882       recovered
    883     )
    884   ).toBe('saved');
    885   expect(
    886     await page.evaluate(
    887       async () => (await window.qxFixture.qualifyAccess()).status
    888     )
    889   ).toBe('qualified');
    890 }
    891 async function liveReady(page: Page) {
    892   await expect
    893     .poll(() =>
    894       page.evaluate(() => {
    895         const snapshot = window.qxFixture.receiveSnapshot();
    896         return snapshot && 'backfill' in snapshot
    897           ? {
    898               state: snapshot.state,
    899               backfill: snapshot.backfill,
    900               pending: snapshot.pending
    901             }
    902           : undefined;
    903       })
    904     )
    905     .toEqual({ state: 'live', backfill: 'complete', pending: 0 });
    906 }
    907 for (const recovered of [false, true]) {
    908   const label = recovered ? 'REC' : 'original';
    909   for (const variation of ['extra', 'order'] as const)
    910     test(`${label} same signed event with ${variation} wire variation follows ordinary envelope admission`, async ({
    911       page
    912     }) => {
    913       const result = await page.evaluate(
    914         async ({ recovered, variation }) => {
    915           const f = window.qxFixture;
    916           if (recovered && (await f.qualify(true)).status !== 'saved')
    917             throw Error('actual REC source');
    918           const event = JSON.parse(await f.selfWire()) as Record<
    919             string,
    920             unknown
    921           >;
    922           const changed =
    923             variation === 'extra'
    924               ? { ...event, diagnostic: 'not source authority' }
    925               : Object.fromEntries(Object.entries(event).reverse());
    926           const before = f.host.counts();
    927           const status = await f.retain(JSON.stringify(changed));
    928           return {
    929             status,
    930             before,
    931             after: f.host.counts(),
    932             received: (await f.receivedRows()).length
    933           };
    934         },
    935         { recovered, variation }
    936       );
    937       expect(result.status).toBe(
    938         variation === 'extra' ? 'invalid' : 'retained'
    939       );
    940       expect(result.received).toBe(variation === 'extra' ? 0 : 1);
    941       expect(result.after).toEqual(result.before);
    942     });
    943   test(`${label} observed missing journal then handwritten exact restoration cannot revive exclusion`, async ({
    944     page
    945   }) => {
    946     const result = await page.evaluate(async (recovered) => {
    947       const f = window.qxFixture;
    948       if (recovered && (await f.qualify(true)).status !== 'saved')
    949         throw Error('actual REC source');
    950       const wire = await f.selfWire(),
    951         row = await f.rowWire();
    952       await f.removeFixtureRow();
    953       const first = await f.retain(wire);
    954       await f.replaceRow(row);
    955       const second = await f.retain(wire);
    956       return { first, second, received: (await f.receivedRows()).length };
    957     }, recovered);
    958     expect(result).toEqual({
    959       first: 'retained',
    960       second: 'duplicate',
    961       received: 1
    962     });
    963   });
    964   for (const mode of ['changed', 'alias', 'abort', 'corrupt'] as const)
    965     test(`${label} actual ${mode} native custody cannot exclude`, async ({
    966       page
    967     }) => {
    968       const result = await page.evaluate(
    969         async ({ recovered, mode }) => {
    970           const f = window.qxFixture;
    971           if (recovered && (await f.qualify(true)).status !== 'saved')
    972             throw Error('actual REC source');
    973           const wire = await f.selfWire(),
    974             row = await f.rowWire();
    975           if (mode === 'changed') {
    976             const parsed = JSON.parse(row) as { revision: number };
    977             parsed.revision++;
    978             await f.replaceRow(JSON.stringify(parsed));
    979           } else if (mode === 'alias')
    980             await f.replaceRow(row, crypto.randomUUID());
    981           else if (mode === 'corrupt')
    982             await f.replaceRow('{malformed own journal');
    983           const fault =
    984             mode === 'abort' ? f.nativeFault('initial_read_abort') : undefined;
    985           try {
    986             const status = await f.retain(wire);
    987             return {
    988               status,
    989               received: (await f.receivedRows()).length,
    990               corruptPreserved:
    991                 mode !== 'corrupt' ||
    992                 (await f.host.stored()).find((row) => row.id === f.command())
    993                   ?.wire === '{malformed own journal',
    994               reads: fault?.counts().readonly
    995             };
    996           } finally {
    997             fault?.restore();
    998           }
    999         },
   1000         { recovered, mode }
   1001       );
   1002       expect(result.status).toBe('retained');
   1003       expect(result.received).toBe(1);
   1004       expect(result.corruptPreserved).toBe(true);
   1005       if (mode === 'abort') expect(result.reads).toBe(1);
   1006     });
   1007   test(`${label} foreign native namespace is preserved and does not poison genuine exclusion`, async ({
   1008     page
   1009   }) => {
   1010     const result = await page.evaluate(async (recovered) => {
   1011       const f = window.qxFixture;
   1012       if (recovered && (await f.qualify(true)).status !== 'saved')
   1013         throw Error('actual REC source');
   1014       const owner =
   1015         'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5';
   1016       const id = crypto.randomUUID(),
   1017         wire = await f.rowWire();
   1018       await f.replaceRow(wire, id, owner);
   1019       const before = (await f.host.stored()).find((row) => row.id === id)!;
   1020       const status = await f.retain();
   1021       const after = (await f.host.stored()).find((row) => row.id === id)!;
   1022       return {
   1023         status,
   1024         preserved: JSON.stringify(before) === JSON.stringify(after)
   1025       };
   1026     }, recovered);
   1027     expect(result).toEqual({ status: 'probe_excluded', preserved: true });
   1028   });
   1029   for (const lane of ['finite', 'history', 'live'] as const)
   1030     test(`${label} genuine qualified ${lane} receive charges exact probe deliveries without retention or prompts`, async ({
   1031       page
   1032     }) => {
   1033       await qualify(page, recovered);
   1034       const wire = await page.evaluate(() => window.qxFixture.selfWire());
   1035       receiveFrames = [JSON.parse(wire) as (typeof receiveFrames)[number]];
   1036       receiveLane = lane === 'live' ? 'live' : 'history';
   1037       const before = await page.evaluate(() => window.qxFixture.host.counts());
   1038       expect(
   1039         await page.evaluate(
   1040           async (lane) =>
   1041             window.qxFixture.startReceive(
   1042               lane === 'finite' ? 'finite' : 'sync'
   1043             ),
   1044           lane
   1045         )
   1046       ).toBe(true);
   1047       await expect
   1048         .poll(() =>
   1049           page.evaluate(
   1050             () => window.qxFixture.receiveSnapshot()?.excludedProbes
   1051           )
   1052         )
   1053         .toBe(lane === 'history' ? 6 : 3);
   1054       if (lane !== 'finite') await liveReady(page);
   1055       const result = await page.evaluate(async () => ({
   1056         snapshot: window.qxFixture.receiveSnapshot(),
   1057         received: (await window.qxFixture.receivedRows()).length,
   1058         after: window.qxFixture.host.counts()
   1059       }));
   1060       expect(result.snapshot).toMatchObject({
   1061         candidates: lane === 'history' ? 6 : 3,
   1062         excludedProbes: lane === 'history' ? 6 : 3,
   1063         retained: 0,
   1064         duplicates: 0,
   1065         rejected: 0,
   1066         pending: 0
   1067       });
   1068       expect(result.snapshot!.bytes).toBe(
   1069         (lane === 'history' ? 6 : 3) * Buffer.byteLength(wire)
   1070       );
   1071       expect(result.received).toBe(0);
   1072       const settled = await page.evaluate(() => window.qxFixture.host.counts());
   1073       // Existing live admission plus its two bounded history windows each
   1074       // recheck the extension owner; no decryption or signing is added.
   1075       expect(result.after).toEqual({
   1076         ...before,
   1077         keys: before.keys + (lane === 'finite' ? 1 : 3)
   1078       });
   1079       expect(settled).toEqual(result.after);
   1080     });
   1081   for (const mixed of [false, true])
   1082     test(`${label} real load-older ${mixed ? 'mixed' : 'probe-only'} page preserves signed cursor progress and actual costs`, async ({
   1083       page
   1084     }) => {
   1085       await qualify(page, recovered);
   1086       expect(
   1087         await page.evaluate(() => window.qxFixture.startReceive('sync'))
   1088       ).toBe(true);
   1089       await liveReady(page);
   1090       const wire = await page.evaluate(() => window.qxFixture.selfWire()),
   1091         probe = JSON.parse(wire) as (typeof receiveFrames)[number];
   1092       let ordinary: (typeof receiveFrames)[number] | undefined;
   1093       if (mixed) {
   1094         const normal = await page.evaluate(
   1095           ({ recovered, time }) =>
   1096             window.qxFixture.ordinaryWire(recovered, time),
   1097           { recovered, time: probe.created_at - 1 }
   1098         );
   1099         ordinary = JSON.parse(normal) as (typeof receiveFrames)[number];
   1100       }
   1101       receiveFrames = ordinary ? [probe, ordinary, probe] : [probe, probe];
   1102       receiveLane = 'history';
   1103       const before = await page.evaluate(() => window.qxFixture.host.counts());
   1104       expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
   1105       const state = await page.evaluate(async () => ({
   1106         older: window.qxFixture.olderSnapshot(),
   1107         received: (await window.qxFixture.receivedRows()).length,
   1108         after: window.qxFixture.host.counts()
   1109       }));
   1110       expect(state.older!.historyComplete).toBe(false);
   1111       expect(state.older!.reason).toBeNull();
   1112       for (const cursor of state.older!.sources) {
   1113         expect(cursor.until).toBe(ordinary?.created_at ?? probe.created_at);
   1114         expect(cursor.seen).toContain(probe.id);
   1115         if (ordinary) expect(cursor.seen).toContain(ordinary.id);
   1116         // Existing cursor policy preserves duplicate delivery costs: unique
   1117         // signed facts are fewer than actual deliveries, so coverage is partial.
   1118         expect(cursor.partial).toBe(true);
   1119       }
   1120       expect(state.received).toBe(mixed ? 1 : 0);
   1121       expect(state.after).toEqual({ ...before, keys: before.keys + 3 });
   1122       receiveFrames = [];
   1123       expect(await page.evaluate(() => window.qxFixture.older())).toBe(true);
   1124       const next = await page.evaluate(() => window.qxFixture.olderSnapshot());
   1125       for (const cursor of next!.sources) {
   1126         expect(cursor.until).toBe(ordinary?.created_at ?? probe.created_at);
   1127         expect(cursor.seen).toContain(probe.id);
   1128       }
   1129       expect(
   1130         requests.filter(
   1131           (row) =>
   1132             row.filter.kinds?.includes(1059) &&
   1133             !row.filter.ids &&
   1134             row.filter.limit !== 0
   1135         ).length
   1136       ).toBe(12);
   1137     });
   1138   test(`${label} successful reviewed removal returns self ciphertext to normal explicit decrypt cost and self-pair rejection`, async ({
   1139     page
   1140   }) => {
   1141     await qualify(page, recovered);
   1142     const result = await page.evaluate(async () => {
   1143       const f = window.qxFixture,
   1144         wire = await f.selfWire();
   1145       if (!(await f.select()) || (await f.remove()).status !== 'removed')
   1146         throw Error('actual successful reviewed cleanup');
   1147       if (f.qualifyRemoved().status !== 'qualified')
   1148         throw Error('actual removed source qualification');
   1149       const before = f.host.counts(),
   1150         status = await f.retain(wire),
   1151         retained = (await f.receivedRows()).length;
   1152       const afterRetain = f.host.counts(),
   1153         decrypted = await f.decryptStored();
   1154       return { status, retained, before, afterRetain, decrypted };
   1155     });
   1156     expect(result.status).toBe('retained');
   1157     expect(result.retained).toBe(1);
   1158     expect(result.afterRetain).toEqual(result.before);
   1159     expect(result.decrypted.snapshot).toMatchObject({
   1160       attempted: 1,
   1161       invalid: 1,
   1162       admitted: 0
   1163     });
   1164     expect(result.decrypted.rooms).toBe(0);
   1165     expect(
   1166       result.decrypted.after.decrypts - result.decrypted.before.decrypts
   1167     ).toBe(2);
   1168   });
   1169   test(`${label} genuine two-party message with identical diagnostic body remains a normal conversation`, async ({
   1170     page
   1171   }) => {
   1172     const result = await page.evaluate(async (recovered) => {
   1173       const f = window.qxFixture;
   1174       if (recovered && (await f.qualify(true)).status !== 'saved')
   1175         throw Error('actual REC source');
   1176       const wire = f.ordinaryWire(recovered),
   1177         status = await f.retain(wire),
   1178         decrypted = await f.decryptStored();
   1179       return { status, decrypted };
   1180     }, recovered);
   1181     expect(result.status).toBe('retained');
   1182     expect(result.decrypted.snapshot).toMatchObject({
   1183       attempted: 1,
   1184       invalid: 0,
   1185       admitted: 1
   1186     });
   1187     expect(result.decrypted.rooms).toBe(1);
   1188     expect(
   1189       result.decrypted.after.decrypts - result.decrypted.before.decrypts
   1190     ).toBe(2);
   1191   });
   1192   test(`${label} genuine native-observed delivery proof is one-use and cannot survive final source loss`, async ({
   1193     page
   1194   }) => {
   1195     if (recovered)
   1196       expect(
   1197         await page.evaluate(
   1198           async () => (await window.qxFixture.qualify(true)).status
   1199         )
   1200       ).toBe('saved');
   1201     const result = await page.evaluate(async () => {
   1202       const f = window.qxFixture,
   1203         a = f.actors(),
   1204         wire = await f.selfWire(),
   1205         event = JSON.parse(wire) as { id: string };
   1206       return await navigator.locks.request(
   1207         'harvestcircle:owner:' + f.host.owner,
   1208         { mode: 'exclusive', ifAvailable: true },
   1209         async (lock) => {
   1210           if (!lock) throw Error('actual owner WebLock');
   1211           const first = await window.qx.observeInboxProbeDelivery(
   1212             f.repository,
   1213             a.session,
   1214             event.id,
   1215             wire
   1216           );
   1217           if (typeof first === 'string')
   1218             throw Error('actual native-observed source proof');
   1219           const copy = window.qx.consumeInboxProbeDelivery(
   1220             { ...first },
   1221             f.repository,
   1222             a.session,
   1223             event.id,
   1224             wire
   1225           );
   1226           const current = window.qx.consumeInboxProbeDelivery(
   1227             first,
   1228             f.repository,
   1229             a.session,
   1230             event.id,
   1231             wire
   1232           );
   1233           const repeat = window.qx.consumeInboxProbeDelivery(
   1234             first,
   1235             f.repository,
   1236             a.session,
   1237             event.id,
   1238             wire
   1239           );
   1240           const later = await window.qx.observeInboxProbeDelivery(
   1241             f.repository,
   1242             a.session,
   1243             event.id,
   1244             wire
   1245           );
   1246           if (typeof later === 'string')
   1247             throw Error('actual second native source proof');
   1248           f.stop();
   1249           const lost = window.qx.consumeInboxProbeDelivery(
   1250             later,
   1251             f.repository,
   1252             a.session,
   1253             event.id,
   1254             wire
   1255           );
   1256           return { copy, current, repeat, lost };
   1257         }
   1258       );
   1259     });
   1260     expect(result).toEqual({
   1261       copy: false,
   1262       current: true,
   1263       repeat: false,
   1264       lost: false
   1265     });
   1266   });
   1267 }