app

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

local-inventory.ts (3786B)


      1 import type { PublicRecord } from '../contracts/local-records.ts';
      2 import type { PrivateRecord, PrivateTargetReceipt } from './private-records.ts';
      3 export type PublicInventoryRow = Readonly<{
      4   key: string;
      5   family: PublicRecord['family'];
      6   id: string;
      7   revision: number;
      8   logicalBytes: number;
      9   state: 'settled' | 'protected';
     10 }>;
     11 // Conservative local journal classification, not independently proven relay
     12 // evidence. Any unresolved historical receipt keeps the operation protected.
     13 export function publicRecordSettlement(
     14   record: PublicRecord
     15 ): 'settled' | 'protected' {
     16   if (record.family === 'public_draft') return 'settled';
     17   if (
     18     !record.artifact ||
     19     record.receipts.length === 0 ||
     20     record.receipts.some(
     21       (receipt) => receipt.status !== 'accepted' && receipt.status !== 'refused'
     22     )
     23   )
     24     return 'protected';
     25   return record.capture.targets.every((origin) =>
     26     record.receipts.some((receipt) => receipt.origin === origin)
     27   )
     28     ? 'settled'
     29     : 'protected';
     30 }
     31 export function publicInventoryRow(
     32   record: PublicRecord,
     33   logicalBytes: number
     34 ): PublicInventoryRow {
     35   return {
     36     key: record.family + ':' + record.id,
     37     family: record.family,
     38     id: record.id,
     39     revision: record.revision,
     40     logicalBytes,
     41     state: publicRecordSettlement(record)
     42   };
     43 }
     44 export const PUBLIC_CLEANUP_CONSEQUENCES =
     45   'Removing selected records loses their local drafts or recovery receipts. It does not delete remote copies or reverse accepted effects. Pending or uncertain work is protected. Browser-local storage is not a backup.';
     46 
     47 export type PrivateInventoryRow = Readonly<{
     48   id: string;
     49   revision: number;
     50   logicalBytes: number;
     51   state: 'settled' | 'protected';
     52 }>;
     53 // Strict decoded local bookkeeping only, never a new sender, remote receipt or
     54 // replay capability. A later named acceptance resolves only its exact binding.
     55 export function privateRecordSettlement(
     56   record: PrivateRecord
     57 ): 'settled' | 'protected' {
     58   if (
     59     record.family !== 'private_send_operation' ||
     60     !record.peerArtifact ||
     61     !record.deliveryPlan ||
     62     !record.receipts?.length
     63   )
     64     return 'protected';
     65   const facts = record.receipts;
     66   const sameTarget = (a: PrivateTargetReceipt, b: PrivateTargetReceipt) =>
     67     a.role === b.role && a.origin === b.origin && a.eventId === b.eventId;
     68   if (
     69     facts.some(
     70       (fact, index) =>
     71         ['unknown', 'timed_out', 'stopped'].includes(fact.status) &&
     72         !facts.some(
     73           (later, laterIndex) =>
     74             laterIndex > index &&
     75             later.status === 'accepted' &&
     76             sameTarget(fact, later)
     77         )
     78     )
     79   )
     80     return 'protected';
     81   return [
     82     record.deliveryPlan.routes.peer,
     83     record.deliveryPlan.routes.archive
     84   ].every((route) => {
     85     const eventId =
     86       route.role === 'peer'
     87         ? record.peerArtifact!.eventId
     88         : record.self.eventId;
     89     return route.targets.every((origin) =>
     90       facts.some(
     91         (fact) =>
     92           fact.status === 'accepted' &&
     93           fact.role === route.role &&
     94           fact.origin === origin &&
     95           fact.eventId === eventId
     96       )
     97     );
     98   })
     99     ? 'settled'
    100     : 'protected';
    101 }
    102 export function privateInventoryRow(
    103   record: PrivateRecord,
    104   logicalBytes: number
    105 ): PrivateInventoryRow {
    106   return {
    107     id: record.id,
    108     revision: record.revision,
    109     logicalBytes,
    110     state: privateRecordSettlement(record)
    111   };
    112 }
    113 export const PRIVATE_OUTBOX_CLEANUP_CONSEQUENCES =
    114   'Removing selected settled sends loses their local encrypted recovery, delivery status and duplicate-prevention evidence. Other local messages, participants and relays may retain copies. It does not erase remote messages or guarantee that a deliberately repeated message cannot be sent again. Browser-local storage is not a backup.';