app

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

commit 48f2a830d11eff75680ccfb1b3da02d0b456a92f
parent 4727549eea1c61d928ded372b5d3e5163f81656a
Author: triesap <tyson@radroots.org>
Date:   Tue,  6 Oct 2026 16:22:59 +0000

availability: Add optional NIP-50 sampling (HCP-040)

- Select only capability-qualified sources for normalized public queries.
- Reserve one relevance round and enforce 100 actual deliveries per source.
- Share ingress and lifecycle owners while preserving ordinary fallback.
- Verify source isolation, bypass rejection, local matching, and SDK behavior.

Diffstat:
Mweb/src/lib/catalog/search-run.ts | 45++++++++++++++++++++++++++++++++++++++++++++-
Mweb/src/lib/config/budgets.ts | 7+++++++
Mweb/src/lib/nostr/public-pool.ts | 34+++++++++++++++++++++++++---------
Mweb/src/lib/nostr/request-result.ts | 30++++++++++++++++++++++++------
Mweb/src/lib/nostr/request-scope.ts | 22+++++++++++++++++-----
Aweb/src/lib/nostr/search-query.ts | 18++++++++++++++++++
Aweb/src/lib/nostr/search-sources.ts | 17+++++++++++++++++
Mweb/src/lib/runtime/public-runtime.ts | 14+++++++++++---
Aweb/tests/integration/search-sample.test.ts | 224+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/unit/request-result.test.ts | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/unit/request-scope.test.ts | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/unit/search-query.test.ts | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/unit/search-run.test.ts | 106++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
13 files changed, 701 insertions(+), 28 deletions(-)

diff --git a/web/src/lib/catalog/search-run.ts b/web/src/lib/catalog/search-run.ts @@ -16,6 +16,7 @@ import { beginPublicViewRun, publicViewRunCurrent, publicViewProjectionAvailable, + publicViewNip50Sources, subscribePublicView, disposePublicView, type PublicView @@ -42,6 +43,7 @@ import { type SearchSnapshot } from './search-state.ts'; import type { WallClock } from './clock-policy.ts'; +import { nip50SearchQuery } from '../nostr/search-query.ts'; declare const coordinatorBrand: unique symbol; declare const generationBrand: unique symbol; export type FoodSearchCoordinator = Readonly<{ @@ -67,6 +69,7 @@ interface Session { readonly requests: () => readonly PublicRequest[]; readonly track: (request: PublicRequest) => void; readonly reserveSource: () => void; + readonly reserveSample: () => void; readonly run: () => PublicRun | undefined; readonly bindRun: (run: PublicRun) => void; readonly model: () => HeadResolver | undefined; @@ -122,6 +125,7 @@ function createSession( failed = false, capped = false; let attempts = 0; + let sampleRounds = 0; return { coordinator, token, @@ -157,6 +161,11 @@ function createSession( } attempts++; }, + reserveSample() { + if (sampleRounds >= PUBLIC_SEARCH_RUN_BUDGETS.nip50Rounds) + throw new Error('food_search_sample_round_limit'); + sampleRounds++; + }, run: () => run, bindRun: (value) => { run = value; @@ -282,6 +291,15 @@ export function subscribeFoodSearch( token: FoodSearchRun, filters: readonly PublicFilter[] ): PublicRequest { + if (filters.some((filter) => 'search' in filter)) + throw new Error('food_search_sample_required'); + return subscribeSearchSource(token, filters); +} +function subscribeSearchSource( + token: FoodSearchRun, + filters: readonly PublicFilter[], + sampleSource?: string +): PublicRequest { const session = sessionOf(token), run = foodSearchRequestOwner(token); session.reserveSource(); // Reservation precedes SDK effects and never refunds. @@ -298,7 +316,8 @@ export function subscribeFoodSearch( const head = createPublicHeadCandidate(proof); if (!head || publicHeadSnapshot(head).kind !== 30402) return; if (!session.remember(head)) cancelPublicRun(run); - } + }, + sampleSource ); } catch { if (current(session)) session.fail(); @@ -311,6 +330,30 @@ export function subscribeFoodSearch( } return request; } + +export function sampleFoodSearch( + token: FoodSearchRun +): readonly PublicRequest[] { + const session = sessionOf(token); + requireCurrent(session); + const filters = nip50SearchQuery(session.query); + if (!filters.length) return []; + const sources = publicViewNip50Sources(session.coordinator.view); + if (!sources.length) return []; + session.reserveSample(); // Failed attempts cannot recreate the relevance round. + const samples = new Map<PublicRequest, true>(); + for (const source of sources) { + requireCurrent(session); + try { + samples.set(subscribeSearchSource(token, filters, source), true); + } catch { + // A refused/failed source cannot discard other candidates or the fallback. + requireCurrent(session); + session.fail(); + } + } + return Array.from(samples.keys()); +} export function foodSearchSnapshot( coordinator: FoodSearchCoordinator ): SearchSnapshot<FoodSearchRun> | undefined { diff --git a/web/src/lib/config/budgets.ts b/web/src/lib/config/budgets.ts @@ -49,3 +49,10 @@ export const PUBLIC_SEARCH_RUN_BUDGETS = Object.freeze({ chronologicalWindows: publicChronologicalWindows, primaryScopes: publicRelayLimit + publicChronologicalWindows }); + +// Optional relevance sample: requested count and actual candidate work per source. +// The shared run meter still charges duplicates, rejects and every auxiliary. +export const PUBLIC_NIP50_BUDGETS = Object.freeze({ + requestedPerRelay: 100, + candidatesPerSource: 100 +}); diff --git a/web/src/lib/nostr/public-pool.ts b/web/src/lib/nostr/public-pool.ts @@ -6,6 +6,7 @@ import { type RelayPolicy } from '../config/relays.ts'; import type { PublicFilter, PublicPoolMessage } from './exports.ts'; +import { requireNip50Source } from './search-sources.ts'; declare const ownedPool: unique symbol; export type PublicPool = Readonly<{ readonly [ownedPool]: true }>; @@ -16,7 +17,8 @@ interface Owner { readonly closed: () => boolean; readonly subscribe: ( filters: readonly PublicFilter[], - onMessage: (message: PublicPoolMessage) => void + onMessage: (message: PublicPoolMessage) => void, + sampleSource?: string ) => () => void; readonly close: () => void; } @@ -52,15 +54,21 @@ export function getPublicPool( token, origins, closed: () => closed, - subscribe(filters, onMessage) { + subscribe(filters, onMessage, sampleSource) { if (closed) throw new Error('public_pool_closed'); let release = () => {}; const subscription = sdk - .req([...origins], [...filters], { - waitForAuth: false, - reconnect: false, - resubscribe: false - }) + .req( + sampleSource === undefined + ? [...origins] + : [requireNip50Source(policy, sampleSource)], + [...filters], + { + waitForAuth: false, + reconnect: false, + resubscribe: false + } + ) .subscribe({ next: (message) => { if (!closed) onMessage(message); @@ -120,7 +128,8 @@ export function publicPoolOrigins(token: PublicPool): readonly string[] { export function subscribePublicPool( token: PublicPool, filters: readonly PublicFilter[], - onMessage: (message: PublicPoolMessage) => void + onMessage: (message: PublicPoolMessage) => void, + sampleSource?: string ): () => void { const owner = ownerOf(token); if (owner.closed()) throw new Error('public_pool_closed'); @@ -132,7 +141,14 @@ export function subscribePublicPool( ) ) throw new Error('public_pool_filters_invalid'); - return owner.subscribe(filters, onMessage); + if (sampleSource !== undefined) + requireNip50Source(owner.policy, sampleSource); + if ( + sampleSource === undefined && + filters.some((filter) => 'search' in filter) + ) + throw new Error('public_pool_search_requires_source'); + return owner.subscribe(filters, onMessage, sampleSource); } // Terminal browser-lifetime shutdown: cancel request controls before closing the diff --git a/web/src/lib/nostr/request-result.ts b/web/src/lib/nostr/request-result.ts @@ -1,4 +1,8 @@ -import { PUBLIC_INGRESS_BUDGETS } from '../config/budgets.ts'; +import { + PUBLIC_INGRESS_BUDGETS, + PUBLIC_NIP50_BUDGETS +} from '../config/budgets.ts'; +import { requireNip50Source } from './search-sources.ts'; import { publicRelayTargets, type RelayPolicy } from '../config/relays.ts'; import { createObservationContext, @@ -102,16 +106,21 @@ function sourceOwner(source: string) { export function createPublicRequestResult( policy: RelayPolicy, ingress: PublicIngress, - journal: ObservationJournal + journal: ObservationJournal, + sampleSource?: string ): PublicRequestResult { publicIngressStats(ingress); - const origins = publicRelayTargets(policy, 'read'); + const allOrigins = publicRelayTargets(policy, 'read'); const expected = observationSourceOrigins(journal); if ( - origins.length !== expected.length || - !origins.every((origin, index) => origin === expected[index]) + allOrigins.length !== expected.length || + !allOrigins.every((origin, index) => origin === expected[index]) ) throw new Error('request_observation_policy_changed'); + const origins = + sampleSource === undefined + ? allOrigins + : [requireNip50Source(policy, sampleSource)]; const priorIngress = runIngress.get(journal); if (priorIngress && priorIngress !== ingress) throw new Error('public_request_ingress_changed'); @@ -133,7 +142,16 @@ export function createPublicRequestResult( const admitted = admitPublicEvent(ingress, message.event); if (!source || origin === undefined) return { status: 'source_unknown' }; + const pending = source.state() === 'pending'; source.candidate(); + // Preserve the inclusive100th admitted delivery, then settle this sample + // alone. Global ingress overflow retains its existing run-wide semantics. + if ( + sampleSource !== undefined && + source.snapshot().candidates >= + PUBLIC_NIP50_BUDGETS.candidatesPerSource + ) + source.control('limit'); if (admitted.status === 'limit') { source.control('limit'); return { status: 'limit' }; @@ -142,7 +160,7 @@ export function createPublicRequestResult( source.reject(); return { status: 'rejected' }; } - if (source.state() !== 'pending') { + if (!pending) { source.reject(); return { status: 'inactive' }; } diff --git a/web/src/lib/nostr/request-scope.ts b/web/src/lib/nostr/request-scope.ts @@ -1,5 +1,6 @@ import { PUBLIC_REQUEST_BUDGETS } from '../config/budgets.ts'; import type { RelayPolicy } from '../config/relays.ts'; +import { requireNip50Source } from './search-sources.ts'; import { createPublicIngress, publicIngressStats, @@ -63,7 +64,8 @@ interface RunOwner { readonly open: ( kind: RequestKind, open: (next: (message: PublicPoolMessage) => void) => () => void, - onVerified: (event: VerifiedEnvelope) => void + onVerified: (event: VerifiedEnvelope) => void, + sampleSource?: string ) => PublicRequest; readonly active: () => boolean; readonly snapshot: () => Readonly<{ @@ -206,7 +208,11 @@ export function createPublicRun( }), cancel, dispose, - open(kind, open, onVerified) { + open(kind, open, onVerified, sampleSource) { + if (sampleSource !== undefined) { + if (kind !== 'search') throw new Error('nip50_request_kind'); + requireNip50Source(policy, sampleSource); + } shared.available(); if (!owner.active()) throw new Error('public_run_inactive'); if (!['search', 'head', 'deletion', 'profile'].includes(kind)) @@ -227,7 +233,12 @@ export function createPublicRun( shared.reserve(request, () => finish('cancelled')); let result: PublicRequestResult; try { - result = createPublicRequestResult(policy, ingress, journal); + result = createPublicRequestResult( + policy, + ingress, + journal, + sampleSource + ); } catch { shared.release(request); throw new Error('public_request_prepare_failed'); @@ -357,9 +368,10 @@ export function openPublicRequest( run: PublicRun, kind: RequestKind, open: (next: (message: PublicPoolMessage) => void) => () => void, - onVerified: (event: VerifiedEnvelope) => void + onVerified: (event: VerifiedEnvelope) => void, + sampleSource?: string ): PublicRequest { - return runOf(run).open(kind, open, onVerified); + return runOf(run).open(kind, open, onVerified, sampleSource); } export function closePublicRequest(request: PublicRequest): void { if (!requestOf(request).close()) diff --git a/web/src/lib/nostr/search-query.ts b/web/src/lib/nostr/search-query.ts @@ -0,0 +1,18 @@ +import { PUBLIC_NIP50_BUDGETS } from '../config/budgets.ts'; +import { normalizePublicQuery } from '../catalog/query-input.ts'; +import type { PublicFilter } from './exports.ts'; + +// Relevance sampling supplies no timestamp cursor and no completeness claim. +export function nip50SearchQuery(input: unknown): readonly PublicFilter[] { + const query = normalizePublicQuery(input); + if (!query.ok) throw new Error(query.error); + return query.text === '' + ? [] + : [ + { + kinds: [30402], + search: query.text, + limit: PUBLIC_NIP50_BUDGETS.requestedPerRelay + } + ]; +} diff --git a/web/src/lib/nostr/search-sources.ts b/web/src/lib/nostr/search-sources.ts @@ -0,0 +1,17 @@ +import { readRelayPolicy, type RelayPolicy } from '../config/relays.ts'; + +// Pure destination qualification avoids importing query normalization or its +// query-copy encoder into anonymous transport activation. +export function qualifiedNip50Sources(policy: RelayPolicy): readonly string[] { + return readRelayPolicy(policy) + .public.filter((source) => source.read && source.nip50) + .map((source) => source.origin); +} +export function requireNip50Source( + policy: RelayPolicy, + source: string +): string { + if (!qualifiedNip50Sources(policy).includes(source)) + throw new Error('nip50_source_unqualified'); + return source; +} diff --git a/web/src/lib/runtime/public-runtime.ts b/web/src/lib/runtime/public-runtime.ts @@ -1,5 +1,6 @@ import { deploymentRelayPolicy } from '../config/deployment-relays.ts'; import type { RelayPolicy } from '../config/relays.ts'; +import { qualifiedNip50Sources } from '../nostr/search-sources.ts'; import { getPublicPool, subscribePublicPool, @@ -324,7 +325,8 @@ export function subscribePublicView( run: PublicRun, kind: RequestKind, filters: readonly PublicFilter[], - onVerified: (event: VerifiedEnvelope) => void + onVerified: (event: VerifiedEnvelope) => void, + sampleSource?: string ): PublicRequest { const owner = viewOf(view); if (!owner.current(run)) throw new Error('public_view_run_inactive'); @@ -338,7 +340,8 @@ export function subscribePublicView( subscribePublicPool( owner.runtime.pool, JSON.parse(frozen) as PublicFilter[], - next + next, + sampleSource ), (event) => { if (!owner.current(run)) return; @@ -352,10 +355,15 @@ export function subscribePublicView( owner.current(run) ) onVerified(event); - } + }, + sampleSource ); } +export function publicViewNip50Sources(view: PublicView): readonly string[] { + return qualifiedNip50Sources(viewOf(view).runtime.policy); +} + // Capacity invalidation differs from ordinary teardown: last-known snapshots // remain available on normal close, while a breached working set clears all views. export function publicViewProjectionAvailable(view: PublicView): boolean { diff --git a/web/tests/integration/search-sample.test.ts b/web/tests/integration/search-sample.test.ts @@ -0,0 +1,224 @@ +import test from 'node:test'; +import { + publicRequestScopeSnapshot, + publicRunSnapshot +} from '../../src/lib/nostr/request-scope.ts'; +import assert from 'node:assert/strict'; +import { once } from 'node:events'; +import { readFile } from 'node:fs/promises'; +import { finalizeEvent } from 'applesauce-core/helpers'; +import WebSocket, { WebSocketServer } from 'ws'; +import { validateRelayPolicy } from '../../src/lib/config/relays.ts'; +import { + createPublicRuntimeContext, + mountPublicRuntime, + createPublicView, + closePublicRuntime +} from '../../src/lib/runtime/public-runtime.ts'; +import { + createFoodSearchCoordinator, + beginFoodSearch, + subscribeFoodSearch, + sampleFoodSearch, + foodSearchRequestOwner, + foodSearchSnapshot, + cancelFoodSearch +} from '../../src/lib/catalog/search-run.ts'; + +await test('actual SDK qualified relevance sample closes at100 while ordinary fallback and latest/deletion work share the remaining run', async () => { + const corpus = JSON.parse( + await readFile( + new URL( + '../../../contracts/interop/food_availability/corpus.v1.json', + import.meta.url + ), + 'utf8' + ) + ) as { vectors: { id: string; signed_wires: Record<string, string> }[] }; + const base = JSON.parse( + corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous + ) as { tags: string[][]; content: string; created_at: number }; + const key = crypto.getRandomValues(new Uint8Array(32)); + const previous = finalizeEvent( + { + kind: 30402, + created_at: base.created_at, + tags: base.tags, + content: base.content + }, + key + ); + const current = finalizeEvent( + { + kind: 30402, + created_at: base.created_at + 1, + tags: base.tags.map((t) => + t[0] === 'status' + ? ['status', 'sold'] + : t[0] === 'title' + ? ['title', 'Renamed celery'] + : [...t] + ), + content: base.content + }, + key + ); + const deletion = finalizeEvent( + { + kind: 5, + created_at: base.created_at - 1, + tags: [['e', current.id]], + content: '' + }, + key + ); + key.fill(0); + const origin = 'wss://one.example.org', + server = new WebSocketServer({ + host: '127.0.0.1', + port: 0, + maxPayload: 4096 + }); + const sockets: WebSocket[] = []; + const filters: Record<string, unknown>[] = []; + let sent = () => {}; + const sentBoth = new Promise<void>((resolve) => { + sent = resolve; + }); + server.on('connection', (socket) => { + sockets.push(socket); + socket.on('message', (bytes) => { + const buffer = Array.isArray(bytes) + ? Buffer.concat(bytes) + : Buffer.isBuffer(bytes) + ? bytes + : Buffer.from(bytes); + const frame = JSON.parse(buffer.toString('utf8')) as unknown[]; + if (frame[0] !== 'REQ') return; + const id = String(frame[1]), + query = frame[2] as Record<string, unknown>; + filters.push(query); + const values = + query.search !== undefined + ? Array(101).fill(previous) + : (query.kinds as number[])[0] === 30402 + ? query.authors === undefined + ? [previous] + : [current] + : [deletion]; + for (const value of values) + socket.send(JSON.stringify(['EVENT', id, value])); + socket.send(JSON.stringify(['EOSE', id])); + if (filters.length === 1) sent(); + }); + }); + await once(server, 'listening'); + const address = server.address(); + assert.ok(address && typeof address !== 'string'); + const port = address.port; + const oldWindow = Object.getOwnPropertyDescriptor(globalThis, 'window'), + oldSocket = globalThis.WebSocket; + class FixtureSocket extends WebSocket { + constructor(url: string | URL) { + assert.ok( + [origin, 'wss://two.example.org'].includes( + String(url).replace(/\/$/, '') + ) + ); + super(`ws://127.0.0.1:${port}`); + } + } + Object.defineProperty(globalThis, 'window', { + configurable: true, + value: {} + }); + globalThis.WebSocket = + FixtureSocket as unknown as typeof globalThis.WebSocket; + const context = createPublicRuntimeContext(); + try { + const policy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: [ + { origin, read: true, write: false, nip50: true }, + { + origin: 'wss://two.example.org', + read: true, + write: false, + nip50: false + } + ], + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) + )!; + const runtime = mountPublicRuntime(context, policy)!; + const view = createPublicView(runtime); + const coordinator = createFoodSearchCoordinator(view, { + nowSeconds: () => base.created_at + 10 + }); + const generation = beginFoodSearch(coordinator, 'carrots'); + const samples = sampleFoodSearch(generation); + assert.equal(samples.length, 1); + await sentBoth; + for ( + let n = 0; + n < 100 && publicRequestScopeSnapshot(samples[0]).state === 'active'; + n++ + ) + await new Promise((resolve) => setTimeout(resolve, 5)); + assert.equal( + publicRunSnapshot(foodSearchRequestOwner(generation)).ingress.deliveries, + 100 + ); + subscribeFoodSearch(generation, [{ kinds: [30402], limit: 200 }]); + for ( + let n = 0; + n < 100 && + (foodSearchSnapshot(coordinator)?.run?.activeRequests ?? 0) > 0; + n++ + ) + await new Promise((resolve) => setTimeout(resolve, 5)); + assert.equal(filters.length, 7); + assert.deepEqual(filters[0], { + kinds: [30402], + search: 'carrots', + limit: 100 + }); + const head = filters.find( + (q) => (q.kinds as number[])[0] === 30402 && q.authors !== undefined + )!; + const requests = filters.find((q) => (q.kinds as number[])[0] === 5)!; + assert.deepEqual(head.authors, [previous.pubkey]); + assert.deepEqual(head['#d'], [base.tags.find((t) => t[0] === 'd')![1]]); + assert.deepEqual(requests.authors, [previous.pubkey]); + for (const q of filters.slice(1)) { + assert.equal(q.limit, 200); + assert.equal('q' in q, false); + assert.equal('search' in q, false); + assert.equal('since' in q, false); + assert.equal('#k' in q, false); + } + const snapshot = foodSearchSnapshot(coordinator)!; + assert.equal(snapshot.query, 'carrots'); + assert.equal(snapshot.refresh, 'partial'); + assert.equal(snapshot.rows.length, 0); + assert.equal(snapshot.definitiveAbsence, false); + assert.equal(snapshot.run?.ingress.deliveries, 106); + assert.equal(snapshot.run?.activeRequests, 0); + assert.equal(snapshot.scopes.length, 4); + cancelFoodSearch(generation); + } finally { + closePublicRuntime(context); + key.fill(0); + globalThis.WebSocket = oldSocket; + if (oldWindow) Object.defineProperty(globalThis, 'window', oldWindow); + else delete (globalThis as { window?: unknown }).window; + for (const socket of sockets) socket.terminate(); + await new Promise<void>((resolve, reject) => + server.close((error) => (error ? reject(error) : resolve())) + ); + } +}); diff --git a/web/tests/unit/request-result.test.ts b/web/tests/unit/request-result.test.ts @@ -400,3 +400,61 @@ describe('exact public source observations', () => { ); }); }); + +describe('qualified sample observation scope', () => { + const samplePolicy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: origins.map((origin, index) => ({ + origin, + read: true, + write: false, + nip50: index === 0 + })), + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) + )!; + it('bounds observations to the selected qualified source while charging unexpected deliveries', () => { + const journal = createObservationJournal(samplePolicy), + ingress = createPublicIngress(); + const result = createPublicRequestResult( + samplePolicy, + ingress, + journal, + origins[0] + ); + expect( + handlePublicRequestMessage(result, { + type: 'EVENT', + id: 'sample', + from: origins[1], + event: currentEvent() + }).status + ).toBe('source_unknown'); + expect(publicIngressStats(ingress).deliveries).toBe(1); + expect(publicRequestSnapshot(result).sources).toHaveLength(1); + expect(observationJournalStats(journal).rows).toBe(0); + handlePublicRequestMessage(result, { + type: 'EOSE', + id: 'sample', + from: origins[0] + }); + expect(publicRequestSnapshot(result).coverage).toBe('bounded-eose'); + disposePublicRequestResult(result); + closeObservationJournal(journal); + }); + it('rejects an unqualified source before observation context allocation', () => { + const journal = createObservationJournal(samplePolicy), + ingress = createPublicIngress(), + before = observationJournalStats(journal); + expect(() => + createPublicRequestResult(samplePolicy, ingress, journal, origins[1]) + ).toThrow('nip50_source_unqualified'); + expect(observationJournalStats(journal)).toEqual(before); + expect(publicIngressStats(ingress).deliveries).toBe(0); + closeObservationJournal(journal); + }); +}); diff --git a/web/tests/unit/request-scope.test.ts b/web/tests/unit/request-scope.test.ts @@ -440,3 +440,96 @@ describe('owned public request scopes', () => { closePublicScheduler(f.scheduler); }); }); + +describe('per-source optional sample admission', () => { + const samplePolicy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: [ + { origin, read: true, write: false, nip50: true }, + { + origin: 'wss://two.example.org', + read: true, + write: false, + nip50: false + } + ], + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) + )!; + it('stops only the sample at100 including duplicate and rejected deliveries, leaving fallback alive', () => { + const f = fixture(), + run = createPublicRun(f.scheduler, samplePolicy), + sample = channel(), + fallback = channel(); + let accepted = 0; + const request = openPublicRequest( + run, + 'search', + sample.open, + () => { + accepted++; + }, + origin + ); + const other = openPublicRequest(run, 'search', fallback.open, () => {}); + for (let i = 0; i < 99; i++) + sample.emit({ + type: 'EVENT', + id: 'sample', + from: origin, + event: i % 2 === 0 ? event() : { ...event(), id: '0'.repeat(64) } + }); + expect(publicRequestScopeSnapshot(request).state).toBe('active'); + sample.emit({ type: 'EVENT', id: 'sample', from: origin, event: event() }); + expect(accepted).toBe(51); + expect(publicRequestScopeSnapshot(request).state).toBe('partial'); + expect(publicRequestScopeSnapshot(request).result.sources).toHaveLength(1); + expect(publicRequestScopeSnapshot(request).result.sources[0]).toMatchObject( + { state: 'limit', candidates: 100, rejected: 49 } + ); + expect(publicRunSnapshot(run)).toMatchObject({ + active: true, + activeRequests: 1, + ingress: { deliveries: 100 } + }); + sample.emit({ type: 'EVENT', id: 'sample', from: origin, event: event() }); + expect(publicRunSnapshot(run).ingress.deliveries).toBe(100); + fallback.emit({ + type: 'EVENT', + id: 'fallback', + from: origin, + event: event() + }); + expect(publicRunSnapshot(run).ingress.deliveries).toBe(101); + expect(publicRequestScopeSnapshot(other).state).toBe('active'); + cancelPublicRun(run); + closePublicScheduler(f.scheduler); + }); + it('denies unqualified sample destinations and auxiliary sample misuse before provider acquisition', () => { + const f = fixture(), + run = createPublicRun(f.scheduler, samplePolicy), + provider = channel(); + expect(() => + openPublicRequest( + run, + 'search', + provider.open, + () => {}, + 'wss://two.example.org' + ) + ).toThrow('nip50_source_unqualified'); + expect(() => + openPublicRequest(run, 'head', provider.open, () => {}, origin) + ).toThrow('nip50_request_kind'); + expect(publicRunSnapshot(run)).toMatchObject({ + activeRequests: 0, + ingress: { deliveries: 0 } + }); + cancelPublicRun(run); + closePublicScheduler(f.scheduler); + }); +}); diff --git a/web/tests/unit/search-query.test.ts b/web/tests/unit/search-query.test.ts @@ -0,0 +1,61 @@ +import { describe, it, expect } from 'vitest'; +import { + validateRelayPolicy, + readRelayPolicy +} from '../../src/lib/config/relays.ts'; +import { nip50SearchQuery } from '../../src/lib/nostr/search-query.ts'; +import { qualifiedNip50Sources } from '../../src/lib/nostr/search-sources.ts'; +const policy = validateRelayPolicy( + JSON.stringify({ + schemaVersion: 1, + public: [ + { + origin: 'wss://one.example.org', + read: true, + write: false, + nip50: true + }, + { + origin: 'wss://two.example.org', + read: true, + write: false, + nip50: false + }, + { + origin: 'wss://three.example.org', + read: false, + write: true, + nip50: false + } + ], + inbox: [], + postingEnabled: false, + messagingEnabled: false, + operatorDenylist: [] + }) +)!; +describe('optional NIP50 request preparation', () => { + it('selects only genuine manifest-qualified readable sources', () => { + expect(qualifiedNip50Sources(policy)).toEqual(['wss://one.example.org']); + const detached = readRelayPolicy(policy); + expect(detached.public).toHaveLength(3); + expect(() => qualifiedNip50Sources({} as typeof policy)).toThrow( + 'relay_policy_invalid' + ); + }); + it('normalizes public query copies and requests one finite kind30402 sample without a cursor', () => { + expect(nip50SearchQuery(' CARROTS\n celery ')).toEqual([ + { kinds: [30402], search: 'carrots celery', limit: 100 } + ]); + }); + it('empty queries never request relevance sampling', () => { + expect(nip50SearchQuery(' \n ')).toEqual([]); + }); + it('refuses unsafe query inputs under unchanged byte and term bounds', () => { + expect(() => nip50SearchQuery('x'.repeat(513))).toThrow('query_too_long'); + expect(() => nip50SearchQuery(Array(13).fill('a').join(' '))).toThrow( + 'too_many_terms' + ); + expect(() => nip50SearchQuery({})).toThrow(); + }); +}); diff --git a/web/tests/unit/search-run.test.ts b/web/tests/unit/search-run.test.ts @@ -33,7 +33,7 @@ const corpus = JSON.parse( const base = JSON.parse( corpus.vectors.find((v) => v.id.endsWith('_032'))!.signed_wires.previous ) as { tags: string[][]; content: string; created_at: number }; -async function fixture() { +async function fixture(nip50 = false) { const { RelayPool } = await import('applesauce-relay/pool'); const { validateRelayPolicy } = await import('../../src/lib/config/relays.ts'); @@ -71,7 +71,7 @@ async function fixture() { const policy = validateRelayPolicy( JSON.stringify({ schemaVersion: 1, - public: [{ origin, read: true, write: false, nip50: false }], + public: [{ origin, read: true, write: false, nip50 }], inbox: [], postingEnabled: false, messagingEnabled: false, @@ -80,12 +80,17 @@ async function fixture() { )!; const key = crypto.getRandomValues(new Uint8Array(32)); keys.push(key); - const wire = (title = 'Carrots', d?: string, time = base.created_at) => + const wire = ( + title = 'Carrots', + d?: string, + time = base.created_at, + content = base.content + ) => finalizeEvent( { kind: 30402, created_at: time, - content: base.content, + content, tags: base.tags.map((tag) => tag[0] === 'title' ? ['title', title] @@ -158,6 +163,99 @@ async function fixture() { }; } describe('finite food search coordination (isolated SDK provider, not qualification)', () => { + it('requires the one-round sampler for relevance filters even after a completed sample', async () => { + const f = await fixture(true), + token = f.begin('carrots'); + search.sampleFoodSearch(token); + f.channels[0].emit({ type: 'EOSE', id: 'sample', from: f.origin }); + // A JavaScript caller can supply extra arguments despite the public type. + const unsafeSubscribe = search.subscribeFoodSearch as unknown as ( + run: typeof token, + filters: Parameters<typeof search.subscribeFoodSearch>[1], + source: string + ) => unknown; + expect(() => + unsafeSubscribe( + token, + [{ kinds: [30402], search: 'carrots', limit: 100 }], + f.origin + ) + ).toThrow('food_search_sample_required'); + expect(f.provider).toHaveBeenCalledTimes(1); + }); + it('locally rejects irrelevant or ignored relevance matches and accepts matching fallback evidence', async () => { + const f = await fixture(true), + token = f.begin('carrots'); + search.sampleFoodSearch(token); + f.emit( + 0, + f.wire('Celery', 'irrelevant', base.created_at, 'Celery available') + ); + f.channels[0].emit({ type: 'EOSE', id: 'sample', from: f.origin }); + expect(f.snapshot().rows).toHaveLength(0); + const fallback = search.subscribeFoodSearch(token, [ + { kinds: [30402], limit: 200 } + ]); + expect(requests.publicRequestScopeSnapshot(fallback).state).toBe('active'); + f.emit(f.channels.length - 1, f.wire('Carrots', 'wanted')); + const snapshot = f.snapshot(); + expect(snapshot.rows).toHaveLength(1); + expect(snapshot.rows[0].state.food?.title).toBe('Carrots'); + expect(snapshot.rows[0].lastKnown).toBe(true); + expect(snapshot.definitiveAbsence).toBe(false); + expect('olderCursor' in snapshot).toBe(false); + }); + + it('samples a qualified source once and leaves ordinary fallback available', async () => { + const f = await fixture(true), + token = f.begin(' CARROTS '); + const samples = search.sampleFoodSearch(token); + expect(samples).toHaveLength(1); + expect(f.provider.mock.calls[0][0]).toEqual([f.origin]); + expect(f.provider.mock.calls[0][1]).toEqual([ + { kinds: [30402], search: 'carrots', limit: 100 } + ]); + f.channels[0].emit({ type: 'EOSE', id: 'sample', from: f.origin }); + expect(f.snapshot()).toMatchObject({ + refresh: 'bounded-eose', + definitiveAbsence: false, + rows: [] + }); + expect(() => search.sampleFoodSearch(token)).toThrow( + 'food_search_sample_round_limit' + ); + search.subscribeFoodSearch(token, [{ kinds: [30402], limit: 200 }]); + expect(f.provider.mock.calls[1][1]).toEqual([ + { kinds: [30402], limit: 200 } + ]); + expect(f.snapshot().run?.activeRequests).toBe(1); + }); + it('skips sampling for empty queries and unqualified sources without preventing fallback', async () => { + const f = await fixture(false), + token = f.begin('carrots'); + expect(search.sampleFoodSearch(token)).toEqual([]); + expect(f.provider).not.toHaveBeenCalled(); + const browse = f.begin(''); + expect(search.sampleFoodSearch(browse)).toEqual([]); + search.subscribeFoodSearch(browse, [{ kinds: [30402], limit: 200 }]); + expect(f.provider).toHaveBeenCalledTimes(1); + }); + it('records sample provider failure and still permits ordinary chronological discovery', async () => { + const f = await fixture(true), + token = f.begin('carrots'); + f.provider.mockImplementationOnce(() => { + throw new Error('isolated sample refused'); + }); + expect(search.sampleFoodSearch(token)).toEqual([]); + expect(f.snapshot()).toMatchObject({ + refresh: 'error', + definitiveAbsence: false + }); + search.subscribeFoodSearch(token, [{ kinds: [30402], limit: 200 }]); + expect(f.channels).toHaveLength(1); + expect(f.snapshot().run?.activeRequests).toBe(1); + }); + it('uses one coordinator per genuine view and refuses a changed clock owner', async () => { const f = await fixture(); expect(search.createFoodSearchCoordinator(f.view, f.wallClock)).toBe(