app

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

search-run.ts (20029B)


      1 import {
      2   orderFoodRows,
      3   listingView,
      4   type ListingView
      5 } from './listing-view.ts';
      6 import {
      7   createPublisherViews,
      8   readPublisherPage,
      9   publisherSnapshot,
     10   type PublisherViews
     11 } from './publishers.ts';
     12 import {
     13   PUBLIC_QUERY_BUDGETS,
     14   PUBLIC_SEARCH_BUDGETS,
     15   PUBLIC_SEARCH_RUN_BUDGETS
     16 } from '../config/budgets.ts';
     17 import type { PublicFilter } from '../nostr/exports.ts';
     18 import { headResolutionQueries } from '../nostr/product-queries.ts';
     19 import {
     20   publicRunSnapshot,
     21   publicRunObservations,
     22   publicRequestScopeSnapshot,
     23   closePublicRequest,
     24   cancelPublicRun,
     25   type PublicRun,
     26   type PublicRequest
     27 } from '../nostr/request-scope.ts';
     28 import {
     29   beginPublicViewRun,
     30   publicViewRunCurrent,
     31   publicViewProjectionAvailable,
     32   publicViewNip50Sources,
     33   publicViewPublisherAllowed,
     34   subscribePublicView,
     35   disposePublicView,
     36   type PublicView
     37 } from '../runtime/public-runtime.ts';
     38 import {
     39   createPublicHeadCandidate,
     40   publicHeadKey,
     41   publicHeadSnapshot,
     42   selectPublicHead,
     43   type PublicHead
     44 } from './heads.ts';
     45 import { normalizePublicQuery } from './query-input.ts';
     46 import {
     47   createPublicViewHeadResolver,
     48   resolveHeads,
     49   resolveKnownHeads,
     50   headResolutionSnapshot,
     51   type HeadResolver
     52 } from './resolve-head.ts';
     53 import { searchResolvedFood } from './search-match.ts';
     54 import {
     55   searchRefreshState,
     56   type SearchScope,
     57   type SearchSnapshot
     58 } from './search-state.ts';
     59 import type { WallClock } from './clock-policy.ts';
     60 import { nip50SearchQuery } from '../nostr/search-query.ts';
     61 import {
     62   chronologicalSearchQuery,
     63   requireChronologicalSearchQuery
     64 } from '../nostr/chronological-query.ts';
     65 import {
     66   createChronologicalContinuation,
     67   localSearchPage,
     68   type ChronologicalContinuation,
     69   type ChronologicalWindow
     70 } from './continuation.ts';
     71 declare const coordinatorBrand: unique symbol;
     72 declare const generationBrand: unique symbol;
     73 export type FoodSearchCoordinator = Readonly<{
     74   readonly [coordinatorBrand]: true;
     75 }>;
     76 export type FoodSearchRun = Readonly<{ readonly [generationBrand]: true }>;
     77 interface Coordinator {
     78   readonly view: PublicView;
     79   readonly publishers: PublisherViews;
     80   readonly clock: WallClock;
     81   readonly current: () => FoodSearchRun | undefined;
     82   readonly reserve: (token: FoodSearchRun) => void;
     83   readonly closed: () => boolean;
     84   readonly close: () => void;
     85 }
     86 interface Session {
     87   readonly coordinator: Coordinator;
     88   readonly token: FoodSearchRun;
     89   readonly query: string;
     90   readonly heads: () => readonly PublicHead[];
     91   readonly pending: () => readonly PublicHead[];
     92   readonly resolved: (heads: readonly PublicHead[]) => void;
     93   readonly remember: (head: PublicHead) => boolean;
     94   readonly requests: () => readonly PublicRequest[];
     95   readonly track: (request: PublicRequest) => void;
     96   readonly reserveSource: () => void;
     97   readonly reserveSample: () => void;
     98   readonly continuation: () => ChronologicalContinuation;
     99   readonly bindContinuation: (value: ChronologicalContinuation) => void;
    100   readonly chronological: () => readonly (readonly [
    101     PublicRequest,
    102     ChronologicalWindow
    103   ])[];
    104   readonly trackChronological: (
    105     request: PublicRequest,
    106     window: ChronologicalWindow
    107   ) => void;
    108   readonly pages: () => number;
    109   readonly showMore: () => void;
    110   readonly run: () => PublicRun | undefined;
    111   readonly bindRun: (run: PublicRun) => void;
    112   readonly model: () => HeadResolver | undefined;
    113   readonly bindModel: (model: HeadResolver) => void;
    114   readonly flags: () => Readonly<{
    115     cancelled: boolean;
    116     failed: boolean;
    117     capped: boolean;
    118   }>;
    119   readonly cap: () => void;
    120   readonly fail: () => void;
    121   readonly cancel: () => void;
    122 }
    123 const coordinators = new WeakMap<FoodSearchCoordinator, Coordinator>();
    124 const viewCoordinators = new WeakMap<
    125   PublicView,
    126   Readonly<{ clock: WallClock; token: FoodSearchCoordinator }>
    127 >();
    128 const sessions = new WeakMap<FoodSearchRun, Session>();
    129 function coordinatorOf(token: FoodSearchCoordinator): Coordinator {
    130   const owner = coordinators.get(token);
    131   if (!owner) throw new Error('food_search_coordinator_invalid');
    132   return owner;
    133 }
    134 function sessionOf(token: FoodSearchRun): Session {
    135   const session = sessions.get(token);
    136   if (!session) throw new Error('food_search_invalid');
    137   return session;
    138 }
    139 function current(session: Session): boolean {
    140   return (
    141     !session.coordinator.closed() &&
    142     session.coordinator.current() === session.token
    143   );
    144 }
    145 function requireCurrent(session: Session): void {
    146   if (!current(session)) throw new Error('food_search_superseded');
    147 }
    148 // All mutation stays in lexical plain-model owners; callers get opaque handles
    149 // and detached snapshots, never map-returned mutable records or SDK objects.
    150 function createSession(
    151   coordinator: Coordinator,
    152   token: FoodSearchRun,
    153   query: string,
    154   seed: readonly PublicHead[],
    155   fallback: HeadResolver | undefined
    156 ): Session {
    157   const heads = new Map(seed.map((head) => [publicHeadKey(head), head]));
    158   const resolved = new Map<string, true>();
    159   const requests = new Map<PublicRequest, true>();
    160   let run: PublicRun | undefined,
    161     model = fallback;
    162   let cancelled = false,
    163     failed = false,
    164     capped = false;
    165   let attempts = 0;
    166   let sampleRounds = 0;
    167   let chronological = createChronologicalContinuation(),
    168     pages = 1;
    169   const windows = new Map<PublicRequest, ChronologicalWindow>();
    170   return {
    171     coordinator,
    172     token,
    173     query,
    174     heads: () => Array.from(heads.values()),
    175     pending: () =>
    176       Array.from(heads.values()).filter(
    177         (head) => !resolved.has(publicHeadKey(head))
    178       ),
    179     resolved: (values) => {
    180       for (const head of values) resolved.set(publicHeadKey(head), true);
    181     },
    182     remember(head) {
    183       const key = publicHeadKey(head);
    184       if (
    185         !heads.has(key) &&
    186         heads.size >= PUBLIC_QUERY_BUDGETS.coordinatesPerRun
    187       ) {
    188         capped = true;
    189         return false;
    190       }
    191       heads.set(key, selectPublicHead(heads.get(key), head).head);
    192       return true;
    193     },
    194     requests: () => Array.from(requests.keys()),
    195     track: (request) => {
    196       requests.set(request, true);
    197     },
    198     reserveSource() {
    199       if (attempts >= PUBLIC_SEARCH_RUN_BUDGETS.primaryScopes) {
    200         capped = true;
    201         throw new Error('food_search_primary_limit');
    202       }
    203       attempts++;
    204     },
    205     reserveSample() {
    206       if (sampleRounds >= PUBLIC_SEARCH_RUN_BUDGETS.nip50Rounds)
    207         throw new Error('food_search_sample_round_limit');
    208       sampleRounds++;
    209     },
    210     continuation: () => chronological,
    211     bindContinuation: (value) => {
    212       chronological = value;
    213     },
    214     chronological: () => Array.from(windows.entries()),
    215     trackChronological: (request, window) => {
    216       windows.set(request, window);
    217     },
    218     pages: () => pages,
    219     showMore: () => {
    220       pages = Math.min(
    221         Math.ceil(
    222           PUBLIC_QUERY_BUDGETS.coordinatesPerRun /
    223             PUBLIC_SEARCH_BUDGETS.pageRows
    224         ),
    225         pages + 1
    226       );
    227     },
    228     run: () => run,
    229     bindRun: (value) => {
    230       run = value;
    231     },
    232     model: () => model,
    233     bindModel: (value) => {
    234       model = value;
    235     },
    236     flags: () => ({ cancelled, failed, capped }),
    237     cap: () => {
    238       capped = true;
    239     },
    240     fail: () => {
    241       failed = true;
    242     },
    243     cancel() {
    244       cancelled = true;
    245       if (run) cancelPublicRun(run);
    246     }
    247   };
    248 }
    249 export function createFoodSearchCoordinator(
    250   view: PublicView,
    251   clock: WallClock
    252 ): FoodSearchCoordinator {
    253   publicViewProjectionAvailable(view);
    254   if (typeof clock.nowSeconds !== 'function')
    255     throw new Error('food_search_clock_invalid');
    256   const existing = viewCoordinators.get(view);
    257   if (existing) {
    258     if (existing.clock !== clock) throw new Error('food_search_clock_changed');
    259     return existing.token;
    260   }
    261   const token = Object.freeze({}) as FoodSearchCoordinator;
    262   let generation: FoodSearchRun | undefined,
    263     closed = false;
    264   coordinators.set(token, {
    265     view,
    266     publishers: createPublisherViews(view, clock),
    267     clock,
    268     current: () => generation,
    269     reserve: (value) => {
    270       generation = value;
    271     },
    272     closed: () => closed,
    273     close() {
    274       closed = true;
    275       // Preserve failed cleanup controls; repeated disposal retries the view.
    276       disposePublicView(view);
    277       generation = undefined;
    278     }
    279   });
    280   viewCoordinators.set(view, { clock, token });
    281   return token;
    282 }
    283 export function beginFoodSearch(
    284   coordinator: FoodSearchCoordinator,
    285   input: unknown,
    286   known: readonly PublicHead[] = []
    287 ): FoodSearchRun {
    288   const owner = coordinatorOf(coordinator);
    289   if (owner.closed()) throw new Error('food_search_closed');
    290   const query = normalizePublicQuery(input);
    291   if (!query.ok) throw new Error(query.error);
    292   // Complete seed validation precedes clocks, cache and run effects.
    293   headResolutionQueries(known);
    294   const previousToken = owner.current();
    295   const previous =
    296     previousToken === undefined ? undefined : sessionOf(previousToken);
    297   const heads = new Map(
    298     previous?.heads().map((head) => [publicHeadKey(head), head])
    299   );
    300   for (const head of known) {
    301     const key = publicHeadKey(head);
    302     heads.set(key, selectPublicHead(heads.get(key), head).head);
    303   }
    304   if (heads.size > PUBLIC_QUERY_BUDGETS.coordinatesPerRun)
    305     throw new Error('head_resolver_coordinate_limit');
    306   const token = Object.freeze({}) as FoodSearchRun;
    307   const seed = Array.from(heads.values());
    308   const session = createSession(
    309     owner,
    310     token,
    311     query.text,
    312     seed,
    313     previous?.model()
    314   );
    315   sessions.set(token, session);
    316   owner.reserve(token); // Reserve before previous settlement or injected clocks.
    317   try {
    318     const run = beginPublicViewRun(owner.view);
    319     session.bindRun(run);
    320     requireCurrent(session);
    321     const model = createPublicViewHeadResolver(owner.view, run, owner.clock);
    322     session.bindModel(model);
    323     requireCurrent(session);
    324     session.resolved(seed);
    325     const admitted = seed.filter((head) =>
    326       publicViewPublisherAllowed(owner.view, publicHeadSnapshot(head).pubkey)
    327     );
    328     if (admitted.length) resolveHeads(model, admitted);
    329     requireCurrent(session);
    330     return token;
    331   } catch {
    332     if (!current(session)) throw new Error('food_search_superseded');
    333     session.fail();
    334     const run = session.run();
    335     if (run) {
    336       try {
    337         cancelPublicRun(run);
    338       } catch {
    339         /* Still owned, retryable. */
    340       }
    341     }
    342     throw new Error('food_search_start_failed');
    343   }
    344 }
    345 export function foodSearchRequestOwner(token: FoodSearchRun): PublicRun {
    346   const session = sessionOf(token);
    347   requireCurrent(session);
    348   const run = session.run();
    349   if (!run || !publicViewRunCurrent(session.coordinator.view, run))
    350     throw new Error('food_search_inactive');
    351   requireCurrent(session);
    352   return run;
    353 }
    354 // Filter strategies remain separate; primary sources and all auxiliaries share
    355 // one genuine anonymous run rather than creating transport or budget owners.
    356 export function subscribeFoodSearch(
    357   token: FoodSearchRun,
    358   filters: readonly PublicFilter[]
    359 ): PublicRequest {
    360   if (filters.some((filter) => 'search' in filter))
    361     throw new Error('food_search_sample_required');
    362   const session = sessionOf(token);
    363   requireCurrent(session);
    364   refreshChronological(session);
    365   requireCurrent(session);
    366   const selected = requireChronologicalSearchQuery(
    367     filters,
    368     session.continuation().snapshot().until
    369   );
    370   let window: ChronologicalWindow;
    371   try {
    372     window = session.continuation().begin();
    373   } catch (error) {
    374     if (
    375       error instanceof Error &&
    376       error.message === 'food_search_chronological_window_limit'
    377     )
    378       session.cap();
    379     throw error;
    380   }
    381   try {
    382     const request = subscribeSearchSource(token, selected, undefined, window);
    383     session.trackChronological(request, window);
    384     return request;
    385   } catch (error) {
    386     window.finish(false, 'partial');
    387     throw error;
    388   }
    389 }
    390 function subscribeSearchSource(
    391   token: FoodSearchRun,
    392   filters: readonly PublicFilter[],
    393   sampleSource?: string,
    394   chronologicalWindow?: ChronologicalWindow
    395 ): PublicRequest {
    396   const session = sessionOf(token),
    397     run = foodSearchRequestOwner(token);
    398   session.reserveSource(); // Reservation precedes SDK effects and never refunds.
    399   let request: PublicRequest;
    400   try {
    401     request = subscribePublicView(
    402       session.coordinator.view,
    403       run,
    404       'search',
    405       filters,
    406       (proof) => {
    407         // Admission already includes a valid inclusive tail that stopped ingress.
    408         if (!current(session)) return;
    409         const head = createPublicHeadCandidate(proof);
    410         if (!head || publicHeadSnapshot(head).kind !== 30402) return;
    411         chronologicalWindow?.observe(head);
    412         if (!session.remember(head)) cancelPublicRun(run);
    413       },
    414       sampleSource
    415     );
    416   } catch {
    417     if (current(session)) session.fail();
    418     throw new Error('food_search_request_failed');
    419   }
    420   session.track(request);
    421   if (!current(session)) {
    422     closePublicRequest(request);
    423     throw new Error('food_search_superseded');
    424   }
    425   return request;
    426 }
    427 
    428 export function sampleFoodSearch(
    429   token: FoodSearchRun
    430 ): readonly PublicRequest[] {
    431   const session = sessionOf(token);
    432   requireCurrent(session);
    433   const filters = nip50SearchQuery(session.query);
    434   if (!filters.length) return [];
    435   const sources = publicViewNip50Sources(session.coordinator.view);
    436   if (!sources.length) return [];
    437   session.reserveSample(); // Failed attempts cannot recreate the relevance round.
    438   const samples = new Map<PublicRequest, true>();
    439   for (const source of sources) {
    440     requireCurrent(session);
    441     try {
    442       samples.set(subscribeSearchSource(token, filters, source), true);
    443     } catch {
    444       // A refused/failed source cannot discard other candidates or the fallback.
    445       requireCurrent(session);
    446       session.fail();
    447     }
    448   }
    449   return Array.from(samples.keys());
    450 }
    451 function refreshChronological(session: Session): void {
    452   for (const [request, window] of session.chronological()) {
    453     const scope = publicRequestScopeSnapshot(request);
    454     if (scope.state === 'active') continue;
    455     window.finish(
    456       scope.result.sources.some(
    457         (source) => source.candidates >= PUBLIC_QUERY_BUDGETS.requestedPerRelay
    458       ),
    459       scope.state === 'eose' && scope.result.coverage === 'bounded-eose'
    460         ? 'bounded-eose'
    461         : 'partial',
    462       scope.result.sources.map((source) => ({
    463         source: source.source,
    464         saturated: source.candidates >= PUBLIC_QUERY_BUDGETS.requestedPerRelay,
    465         eventIds: publicRunObservations(session.run()!, request)
    466           .filter((row) => row.source === source.source && row.kind === 30402)
    467           .map((row) => row.eventId)
    468       }))
    469     );
    470   }
    471 }
    472 export function chronologicalFoodSearch(token: FoodSearchRun): PublicRequest {
    473   const session = sessionOf(token);
    474   requireCurrent(session);
    475   refreshChronological(session);
    476   requireCurrent(session);
    477   return subscribeFoodSearch(
    478     token,
    479     chronologicalSearchQuery(session.continuation().snapshot().until)
    480   );
    481 }
    482 export function showMoreFoodSearch(token: FoodSearchRun): void {
    483   const session = sessionOf(token);
    484   requireCurrent(session);
    485   session.showMore();
    486 }
    487 export function searchOlderFoodSearch(
    488   coordinator: FoodSearchCoordinator
    489 ): FoodSearchRun {
    490   const owner = coordinatorOf(coordinator),
    491     previous = owner.current();
    492   if (!previous || owner.closed()) throw new Error('food_search_inactive');
    493   const session = sessionOf(previous);
    494   const snapshot = foodSearchSnapshot(coordinator);
    495   requireCurrent(session);
    496   if (!snapshot?.available) throw new Error('food_search_unavailable');
    497   const next = session.continuation().restart();
    498   const token = beginFoodSearch(coordinator, session.query);
    499   const successor = sessionOf(token);
    500   requireCurrent(successor);
    501   successor.bindContinuation(next);
    502   chronologicalFoodSearch(token);
    503   return token;
    504 }
    505 export function foodSearchSnapshot(
    506   coordinator: FoodSearchCoordinator
    507 ): SearchSnapshot<FoodSearchRun> | undefined {
    508   const owner = coordinatorOf(coordinator),
    509     token = owner.current();
    510   if (owner.closed() || token === undefined) return undefined;
    511   const session = sessionOf(token),
    512     model = session.model(),
    513     ownedRun = session.run();
    514   if (ownedRun && model) {
    515     const active = publicViewRunCurrent(owner.view, ownedRun);
    516     if (!current(session)) return undefined;
    517     const pending = session
    518       .pending()
    519       .filter((head) =>
    520         publicViewPublisherAllowed(owner.view, publicHeadSnapshot(head).pubkey)
    521       );
    522     if (pending.length) {
    523       try {
    524         if (active) resolveHeads(model, pending);
    525         else resolveKnownHeads(model, pending);
    526         session.resolved(pending);
    527       } catch {
    528         session.fail();
    529       }
    530     }
    531   }
    532   refreshChronological(session);
    533   const raw = model ? headResolutionSnapshot(model) : [];
    534   const matched = model ? searchResolvedFood(model, session.query) : undefined;
    535   const scopes = new Map<object, SearchScope>();
    536   for (const request of session.requests()) {
    537     const scope = publicRequestScopeSnapshot(request);
    538     scopes.set(scope.result.context, scope);
    539   }
    540   for (const row of raw)
    541     for (const scope of [row.headSources, row.deletionSources])
    542       if (scope) scopes.set(scope.result.context, scope);
    543   const available =
    544     publicViewProjectionAvailable(owner.view) &&
    545     (matched?.ok ? matched.available : true);
    546   const run = ownedRun ? publicRunSnapshot(ownedRun) : undefined;
    547   // Model/run clocks may synchronously supersede or dispose this generation.
    548   if (!current(session)) return undefined;
    549   const sources = Array.from(scopes.values()),
    550     flags = session.flags();
    551   const refresh = searchRefreshState(
    552     available,
    553     run,
    554     sources,
    555     flags.cancelled,
    556     flags.failed,
    557     flags.capped
    558   );
    559   const continuation = session.continuation().snapshot(),
    560     page = localSearchPage(
    561       available && matched?.ok
    562         ? orderFoodRows(
    563             matched.rows.filter((row) =>
    564               publicViewPublisherAllowed(owner.view, row.state.head.pubkey)
    565             )
    566           )
    567         : [],
    568       session.pages()
    569     );
    570   const listings = new Map<string, ListingView>();
    571   for (const row of page.rows) {
    572     const value = listingView(
    573       row,
    574       publisherSnapshot(owner.publishers, row.state.head.pubkey)
    575     );
    576     if (value) listings.set(value.eventId, value);
    577   }
    578   if (!current(session)) return undefined;
    579   return {
    580     generation: token,
    581     query: session.query,
    582     refresh:
    583       continuation.gap && refresh === 'bounded-eose' ? 'partial' : refresh,
    584     available,
    585     rows: page.rows,
    586     listings: Array.from(listings.values()),
    587     hasMore: page.hasMore,
    588     continuation,
    589     scopes: sources,
    590     run,
    591     definitiveAbsence: false
    592   };
    593 }
    594 // Explicit auxiliary work for the current local page. Show more itself stays
    595 // local; snapshots never schedule profile transport or prompt a signer.
    596 export function resolveFoodSearchPublishers(
    597   token: FoodSearchRun
    598 ): readonly PublicRequest[] {
    599   const session = sessionOf(token);
    600   requireCurrent(session);
    601   const snapshot = foodSearchSnapshot(
    602     viewCoordinators.get(session.coordinator.view)!.token
    603   );
    604   requireCurrent(session);
    605   if (!snapshot?.available) throw new Error('food_search_unavailable');
    606   const run = foodSearchRequestOwner(token);
    607   const page = snapshot.rows.slice(
    608     (session.pages() - 1) * PUBLIC_SEARCH_BUDGETS.pageRows,
    609     session.pages() * PUBLIC_SEARCH_BUDGETS.pageRows
    610   );
    611   try {
    612     const requests = readPublisherPage(
    613       session.coordinator.publishers,
    614       run,
    615       page.map((row) => row.state.head.pubkey),
    616       session.pages()
    617     );
    618     for (const request of requests) session.track(request);
    619     requireCurrent(session);
    620     return requests;
    621   } catch (error) {
    622     if (current(session)) session.fail();
    623     throw error;
    624   }
    625 }
    626 export function cancelFoodSearch(token: FoodSearchRun): void {
    627   const session = sessionOf(token);
    628   requireCurrent(session);
    629   session.cancel();
    630 }
    631 export function closeFoodSearchCoordinator(
    632   coordinator: FoodSearchCoordinator
    633 ): void {
    634   coordinatorOf(coordinator).close();
    635 }