@interncom/diplomatic 0.8.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,13 @@
1
1
  import { Status } from "../../shared/consts";
2
2
  import { IHostConnectionInfo, IHostMetadata } from "../../shared/types";
3
- import type { IHostRow, IHostStore } from "../../types";
3
+ import type { HostStatsUpdate, IHostRow, IHostStore } from "../../types";
4
4
  export declare class IDBHostStore implements IHostStore<URL> {
5
5
  db: IDBDatabase;
6
6
  constructor(db: IDBDatabase);
7
7
  add(info: IHostConnectionInfo<URL>): Promise<void>;
8
8
  private put;
9
9
  touch(label: string, seq: number): Promise<void>;
10
+ recordStats(label: string, u: HostStatsUpdate): Promise<void>;
10
11
  get(label: string): Promise<IHostRow<URL> | undefined>;
11
12
  set(label: string, meta: IHostMetadata): Promise<Status.Success | Status.NotFound | Status.DatabaseError>;
12
13
  del(label: string): Promise<void>;
@@ -1,7 +1,7 @@
1
1
  import { Status } from "../../shared/consts";
2
2
  import { ICrypto } from "../../shared/types";
3
3
  import { EntityID, Hash } from "../../shared/types";
4
- import { ApldState, IMessageStore, IStorableMessage, IStoredMessage } from "../../types";
4
+ import { ApldState, IMessageStore, IStorableMessage, IStoredMessage, ListMsgsOpts } from "../../types";
5
5
  export declare class IDBMessageStore implements IMessageStore {
6
6
  private crypto;
7
7
  db: IDBDatabase;
@@ -10,7 +10,8 @@ export declare class IDBMessageStore implements IMessageStore {
10
10
  del(keys: Iterable<Hash>): Promise<void>;
11
11
  get(key: Hash): Promise<IStoredMessage | undefined>;
12
12
  has(key: Hash): Promise<boolean>;
13
- list(apld?: ApldState): Promise<IStoredMessage[]>;
13
+ list(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
14
+ count(apld?: ApldState): Promise<number>;
14
15
  listKeys(): Promise<Hash[]>;
15
16
  last(eid: EntityID): Promise<IStoredMessage | undefined>;
16
17
  markApplied(keys: Iterable<Hash>): Promise<void>;
@@ -1,10 +1,13 @@
1
1
  import { Status } from "../../shared/consts";
2
2
  import { HostHandle, IHostConnectionInfo, IHostMetadata } from "../../shared/types";
3
- import type { IHostRow, IHostStore } from "../../types";
3
+ import type { HostStatsUpdate, IHostRow, IHostStore } from "../../types";
4
4
  export declare class MemoryHostStore<Handle extends HostHandle> implements IHostStore<Handle> {
5
5
  hosts: Map<string, IHostRow<Handle>>;
6
+ /** Serializes recordStats so concurrent peeks do not drop bag/dupe deltas. */
7
+ private statsChain;
6
8
  add(info: IHostConnectionInfo<Handle>): Promise<void>;
7
9
  touch(label: string, seq: number): Promise<void>;
10
+ recordStats(label: string, u: HostStatsUpdate): Promise<void>;
8
11
  get(label: string): Promise<IHostRow<Handle> | undefined>;
9
12
  set(label: string, meta: IHostMetadata): Promise<Status.Success | Status.NotFound>;
10
13
  del(label: string): Promise<void>;
@@ -1,6 +1,6 @@
1
1
  import { ICrypto } from "../../shared/types";
2
2
  import { EntityID, Hash } from "../../shared/types";
3
- import { ApldState, IMessageStore, IStorableMessage, IStoredMessage, IStoredMessageData } from "../../types";
3
+ import { ApldState, IMessageStore, IStorableMessage, IStoredMessage, IStoredMessageData, ListMsgsOpts } from "../../types";
4
4
  import { Status } from "../../shared/consts";
5
5
  export declare class MemoryMessageStore implements IMessageStore {
6
6
  private crypto;
@@ -10,7 +10,8 @@ export declare class MemoryMessageStore implements IMessageStore {
10
10
  del(keys: Iterable<Hash>): Promise<void>;
11
11
  get(key: Hash): Promise<IStoredMessage | undefined>;
12
12
  has(key: Hash): Promise<boolean>;
13
- list(apld?: ApldState): Promise<IStoredMessage[]>;
13
+ list(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
14
+ count(apld?: ApldState): Promise<number>;
14
15
  listKeys(): Promise<Hash[]>;
15
16
  last(eid: EntityID): Promise<IStoredMessage | undefined>;
16
17
  markApplied(keys: Iterable<Hash>): Promise<void>;
@@ -4,7 +4,7 @@ import { Enclave } from "./shared/crypto/enclave";
4
4
  import { Hash, HostHandle, IBag, ICrypto } from "./shared/types";
5
5
  import { ValStat } from "./shared/valstat";
6
6
  import { ProgressFn } from "./progress";
7
- import { IDownloadMessage, IHostRow, IMsgParts, IStore, type IStoredMessageWrite } from "./types";
7
+ import { IDownloadMessage, IHostRow, IMsgParts, IStore, type IStoredMessageWrite, type ReconcileOpts, type ReconcileResult } from "./types";
8
8
  /** Default soft cap for one push/pull request (~1 MiB). Apps with large
9
9
  * payloads (e.g. media) should raise maxPushBytes / maxPullBytes. */
10
10
  export declare const defaultMaxPushBytes: number;
@@ -82,3 +82,13 @@ export declare function msg2StoredMsgData({ head, body }: IMsgParts): IStoredMes
82
82
  * `scheduleSync` runs full peek/push/pull (or worker handoff).
83
83
  */
84
84
  export declare function handleNotif<Handle extends HostHandle>(bytes: Uint8Array, { conn, store, enclave, host, crypto, onProgress, }: ISyncParams<Handle>, scheduleSync: () => void): Promise<void>;
85
+ /**
86
+ * Full host inventory (PEEK from seq 0): set absolute numBags/numDupes/lastSeq,
87
+ * and optionally enqueue downloads (msgs on host missing locally) and/or
88
+ * uploads (local msgs missing on host). Does not pull/push network beyond
89
+ * peek — caller should sync() to drain queues.
90
+ *
91
+ * lastSeq is set to the max bag seq in the inventory (0 if empty), including
92
+ * rewind when the host has fewer bags than a prior cursor believed.
93
+ */
94
+ export declare function reconcileHost<Handle extends HostHandle>(params: ISyncParams<Handle>, opts?: ReconcileOpts): Promise<ValStat<ReconcileResult>>;
@@ -36,7 +36,39 @@ export interface ISeedStore {
36
36
  }
37
37
  export interface IHostRow<Handle extends HostHandle> extends IHostConnectionInfo<Handle>, Partial<IHostMetadata> {
38
38
  lastSeq: number;
39
+ /**
40
+ * Bags known on this host (full reconcile sets absolute; incremental peek
41
+ * adds newly seen seqs).
42
+ */
43
+ numBags: number;
44
+ /**
45
+ * Extra bags beyond one per msg (`Σ max(0, bags_for_msg − 1)`). Absolute on
46
+ * reconcile; +delta on peek when a new duplicate bag for a known msg is seen.
47
+ */
48
+ numDupes: number;
39
49
  }
50
+ /**
51
+ * Atomic host cursor + bag/dupe stats update (one store transaction).
52
+ * Absolute fields replace; deltas add. `lastSeq` only advances; use
53
+ * `setLastSeq` for a full-inventory absolute cursor (reconcile).
54
+ */
55
+ export type HostStatsUpdate = {
56
+ /** New lastSeq if greater than current (incremental peek/push). */
57
+ lastSeq?: number;
58
+ /** Absolute lastSeq, including rewind (full inventory). */
59
+ setLastSeq?: number;
60
+ /** Absolute bag count (full inventory). */
61
+ numBags?: number;
62
+ /** Absolute dupe count (full inventory). */
63
+ numDupes?: number;
64
+ /** Count of newly peeked bags to add to numBags. */
65
+ bagDelta?: number;
66
+ /**
67
+ * Count of newly seen extra bags for a msg already on the host (duplicate
68
+ * bags for the same msg) to add to numDupes.
69
+ */
70
+ dupeDelta?: number;
71
+ };
40
72
  export interface IHostStore<Handle extends HostHandle> {
41
73
  add: (host: IHostConnectionInfo<Handle>) => Promise<void>;
42
74
  get: (label: string) => Promise<IHostRow<Handle> | undefined>;
@@ -45,7 +77,40 @@ export interface IHostStore<Handle extends HostHandle> {
45
77
  list: () => Promise<Iterable<IHostRow<Handle>>>;
46
78
  wipe: () => Promise<void>;
47
79
  touch: (label: string, seq: number) => Promise<void>;
80
+ /**
81
+ * Apply lastSeq / numBags / numDupes in one transaction so counts do not
82
+ * drift vs the cursor under concurrent peek/push.
83
+ */
84
+ recordStats: (label: string, u: HostStatsUpdate) => Promise<void>;
48
85
  }
86
+ /** Options for {@link IClient.reconcile}. */
87
+ export type ReconcileOpts = {
88
+ /** Enqueue downloads for msgs on host missing locally. Default true. */
89
+ pull?: boolean;
90
+ /** Enqueue uploads for local archive msgs missing on host. Default false. */
91
+ push?: boolean;
92
+ /**
93
+ * After inventory, run {@link IClient.sync} to drain upload/download queues.
94
+ * Default true. Pass false for inventory-only (counts + checksum).
95
+ */
96
+ sync?: boolean;
97
+ };
98
+ /** Internal result of a full host inventory (not the public client API). */
99
+ export type ReconcileResult = {
100
+ /**
101
+ * Set-checksum of distinct msgs on the host (archive keys; same construction
102
+ * as {@link IClient.msgcheck}). Ephemeral — not stored on the host row.
103
+ */
104
+ msgcheck: Hash;
105
+ bagCount: number;
106
+ /** Distinct msgs among bags (dup bags for the same msg count once). */
107
+ uniqueMsgs: number;
108
+ numDupes: number;
109
+ /** Msgs on host not in local archive. */
110
+ missingLocal: number;
111
+ /** Local archive msgs not seen on host. */
112
+ missingHost: number;
113
+ };
49
114
  export interface IUploadQueue {
50
115
  enq: (host: string, hshs: Iterable<Hash>) => Promise<void>;
51
116
  deq: (host: string, hshs: Iterable<Hash>) => Promise<void>;
@@ -147,18 +212,35 @@ export declare function isTerminalApplyFailure(st: Status): boolean;
147
212
  * Clears `err` unless setting {@link APLD_ERROR}.
148
213
  */
149
214
  export declare function setApld(data: IStoredMessageData, apld: ApldState, err?: Status): void;
150
- export declare function toStoredMessage(hash: Hash, data: IStoredMessageData, crypto: ICrypto): Promise<IStoredMessage>;
215
+ /**
216
+ * Options for archive list / client diagnostics.
217
+ * `body` defaults to **true**. Pass `body: false` for heads + apld + err only.
218
+ */
219
+ export type ListMsgsOpts = {
220
+ /** Filter by apply lifecycle; omit for all archive rows. */
221
+ apld?: ApldState;
222
+ /**
223
+ * Include msg bodies and derive `head.hsh` from them.
224
+ * Default `true`. Pass `false` to omit body and `hsh` (cheaper diagnostics);
225
+ * `head.len` still reflects stored size.
226
+ */
227
+ body?: boolean;
228
+ };
229
+ export declare function toStoredMessage(hash: Hash, data: IStoredMessageData, crypto: ICrypto, opts?: {
230
+ body?: boolean;
231
+ }): Promise<IStoredMessage>;
151
232
  export interface IMessageStore {
152
233
  add: (messages: IStorableMessage[]) => Promise<Status[]>;
153
234
  get: (key: Hash) => Promise<IStoredMessage | undefined>;
154
235
  has: (key: Hash) => Promise<boolean>;
155
236
  del: (keys: Iterable<Hash>) => Promise<void>;
156
237
  /**
157
- * List archive rows. Pass {@link ApldState} to filter by apply lifecycle
158
- * (e.g. {@link APLD_PENDING} for the apply queue, {@link APLD_ERROR} for
159
- * diagnostics). Omit for all messages.
238
+ * List archive rows. Filter with `apld` (e.g. {@link APLD_ERROR}).
239
+ * Bodies included by default; pass `body: false` to omit.
160
240
  */
161
- list: (apld?: ApldState) => Promise<IStoredMessage[]>;
241
+ list: (opts?: ListMsgsOpts) => Promise<IStoredMessage[]>;
242
+ /** Count archive rows; optional apld filter (IDB index when available). */
243
+ count: (apld?: ApldState) => Promise<number>;
162
244
  /** Archive keys only (head hashes). Prefer over {@link list} for checksums. */
163
245
  listKeys: () => Promise<Hash[]>;
164
246
  last: (eid: EntityID) => Promise<IStoredMessage | undefined>;
@@ -219,6 +301,24 @@ export interface IClient<Handle extends HostHandle> {
219
301
  * Key encoding (e.g. b64 in IDB) is independent of this definition.
220
302
  */
221
303
  msgcheck(): Promise<Hash>;
304
+ /**
305
+ * List local archive rows for diagnostics (failed apply, pending drain, dump).
306
+ * Bodies included by default; pass `body: false` for lighter listings.
307
+ */
308
+ listMsgs(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
309
+ /**
310
+ * Count archive rows; optional apld filter.
311
+ * Prefer over list+length for badges.
312
+ */
313
+ countMsgs(apld?: ApldState): Promise<number>;
314
+ /**
315
+ * Full host inventory (peek from seq 0): set numBags/numDupes/lastSeq on the
316
+ * host row; optionally enqueue downloads (`pull`) and/or uploads (`push`).
317
+ * Returns ephemeral host msg checksum (distinct msgs on host; same as
318
+ * {@link msgcheck}) for UI comparison with local archive. Call
319
+ * {@link sync} to drain queues. Read counts via {@link hosts}.
320
+ */
321
+ reconcile(hostLabel: string, opts?: ReconcileOpts): Promise<ValStat<Hash>>;
222
322
  wipe(): Promise<void>;
223
323
  import(file: File): Promise<Status>;
224
324
  export(filename: string, extension?: string): Promise<Status>;
@@ -1,8 +1,8 @@
1
1
  import { IClock } from "../shared/clock";
2
2
  import { Status } from "../shared/consts";
3
3
  import type { EntityID, Hash, IDeleteParams, IEntRev, IHostConnectionInfo, IInsertParams, IMessageHead, IStateManager, IUpdateParams, MasterSeed, SerializedContent } from "../shared/types";
4
- import type { ValStat } from "../shared/valstat";
5
- import type { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHostRow, IStateEmitter, IStore } from "../types";
4
+ import { type ValStat } from "../shared/valstat";
5
+ import type { ApldState, IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHostRow, IStateEmitter, IStore, IStoredMessage, ListMsgsOpts, ReconcileOpts } from "../types";
6
6
  /**
7
7
  * Options for attaching to an app-owned sync Worker.
8
8
  *
@@ -116,6 +116,14 @@ export declare class WorkerClient implements IClient<URL> {
116
116
  rebuild(options?: {
117
117
  checkHost?: boolean;
118
118
  }): Promise<Status>;
119
+ /** Shared message IDB on main — no worker RPC. */
120
+ listMsgs(opts?: ListMsgsOpts): Promise<IStoredMessage[]>;
121
+ countMsgs(apld?: ApldState): Promise<number>;
122
+ /**
123
+ * Full host inventory on the worker (network + crypto off main).
124
+ * Returns ephemeral host msg checksum; host row stats in shared IDB.
125
+ */
126
+ reconcile(hostLabel: string, opts?: ReconcileOpts): Promise<ValStat<Hash>>;
119
127
  /** Archive checksum via worker (listKeys + sort + blake3 off main). */
120
128
  msgcheck(): Promise<Hash>;
121
129
  /**
@@ -38,6 +38,13 @@ export type WorkerCmd = {
38
38
  id: number;
39
39
  op: "rebuild";
40
40
  checkHost?: boolean;
41
+ } | {
42
+ id: number;
43
+ op: "reconcile";
44
+ hostLabel: string;
45
+ pull?: boolean;
46
+ push?: boolean;
47
+ sync?: boolean;
41
48
  } | {
42
49
  id: number;
43
50
  op: "msgcheck";