app

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

late-results.ts (2970B)


      1 import {
      2   approvedSigningIdentity,
      3   bindApprovedResponse,
      4   type ApprovedPublicSigning
      5 } from '../nostr/approved-signing.ts';
      6 import {
      7   capturedArtifactSnapshot,
      8   type CapturedArtifact
      9 } from '../persistence/artifact-records.ts';
     10 import {
     11   publicRecordWire,
     12   type PublicRecordHandle
     13 } from '../persistence/records.ts';
     14 
     15 declare const lateBrand: unique symbol;
     16 export type LatePublicArtifact = Readonly<{ [lateBrand]: true }>;
     17 type Saved = Readonly<{
     18   owner: string;
     19   id: string;
     20   revision: number;
     21   recordWire: string;
     22   session: symbol;
     23   hash: string;
     24   artifact: CapturedArtifact;
     25 }>;
     26 const results = new WeakMap<LatePublicArtifact, Saved>();
     27 // Pure exact public binding, not proof of SDK admission or current authority.
     28 // The real adapter retains raw responses inside an admitted signature callback.
     29 // Its same-job verified artifact may be reclassified if the identity continuation
     30 // loses freshness. Provider objects and mutable verification caches never survive.
     31 export function bindLatePublicResponse(
     32   approval: ApprovedPublicSigning,
     33   response: unknown,
     34   session: unknown
     35 ): LatePublicArtifact | undefined {
     36   const identity = approvedSigningIdentity(approval);
     37   if (!identity || typeof session !== 'symbol') return undefined;
     38   const artifact = bindApprovedResponse(approval, response),
     39     view = artifact && capturedArtifactSnapshot(artifact);
     40   if (!artifact || !view) return undefined;
     41   const token = Object.freeze({}) as LatePublicArtifact;
     42   results.set(token, {
     43     owner: identity.owner,
     44     id: identity.id,
     45     revision: identity.revision,
     46     recordWire: identity.recordWire,
     47     session,
     48     hash: view.hash,
     49     artifact
     50   });
     51   return token;
     52 }
     53 // Internal original-authority metadata. This does not revive a session or lease.
     54 export function latePublicResultIdentity(token: LatePublicArtifact) {
     55   const saved = results.get(token);
     56   return saved
     57     ? {
     58         owner: saved.owner,
     59         id: saved.id,
     60         revision: saved.revision,
     61         recordWire: saved.recordWire,
     62         session: saved.session
     63       }
     64     : undefined;
     65 }
     66 export function latePublicResultSnapshot(token: LatePublicArtifact) {
     67   const saved = results.get(token);
     68   return saved
     69     ? {
     70         owner: saved.owner,
     71         id: saved.id,
     72         revision: saved.revision,
     73         hash: saved.hash
     74       }
     75     : undefined;
     76 }
     77 // Namespace-scoped mechanical review only. The session owner separately checks
     78 // observed author; fresh resume/readiness/IDB CAS and publication remain distinct.
     79 export function reviewLatePublicArtifact(
     80   token: LatePublicArtifact,
     81   original: PublicRecordHandle,
     82   owner: unknown,
     83   id: unknown,
     84   review: unknown
     85 ): CapturedArtifact | undefined {
     86   const saved = results.get(token);
     87   if (
     88     !saved ||
     89     review !== 'review_original_late_artifact' ||
     90     owner !== saved.owner ||
     91     id !== saved.id ||
     92     publicRecordWire(original, owner, id) !== saved.recordWire
     93   )
     94     return undefined;
     95   return saved.artifact;
     96 }