@interncom/diplomatic 0.7.0 → 0.8.0

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.
@@ -2,7 +2,7 @@ import DiplomaticClientAPI from "./shared/client";
2
2
  import { IClock } from "./shared/clock";
3
3
  import { Status } from "./shared/consts";
4
4
  import { EncodedMessage } from "./shared/message";
5
- import { EntityID, HostHandle, ICrypto, IDeleteParams, IEntRev, IHostConnectionInfo, IInsertParams, IMessageHead, IStateManager, ITransport, IUpdateParams, MasterSeed } from "./shared/types";
5
+ import { EntityID, Hash, HostHandle, ICrypto, IDeleteParams, IEntRev, IHostConnectionInfo, IInsertParams, IMessageHead, IStateManager, ITransport, IUpdateParams, MasterSeed } from "./shared/types";
6
6
  import { ValStat } from "./shared/valstat";
7
7
  import { IClient, IDiplomaticClientState, IDiplomaticClientXferState, IHostRow, IStateEmitter, IStore } from "./types";
8
8
  export declare class SyncClient<Handle extends HostHandle> implements IClient<Handle> {
@@ -132,6 +132,36 @@ export declare class SyncClient<Handle extends HostHandle> implements IClient<Ha
132
132
  */
133
133
  sync(): Promise<Status>;
134
134
  private doSync;
135
+ /**
136
+ * Rebuild application state from the local message archive.
137
+ *
138
+ * By default, inventories each linked host from seq 0 (full header list),
139
+ * enqueues and pulls any bags missing from the archive, then clears EntDB
140
+ * (or other app state) and re-executes every archived msg. Use after a
141
+ * schema/applier change when derived ents diverge from the durable archive.
142
+ *
143
+ * Pass `{ checkHost: false }` to skip the host inventory (offline / local-only).
144
+ * Does not wipe the message archive, seed, or hosts.
145
+ */
146
+ rebuild(options?: {
147
+ checkHost?: boolean;
148
+ }): Promise<Status>;
149
+ /**
150
+ * Checksum of the local msg archive (set of head hashes).
151
+ * listKeys → raw bytes → sort → blake3(concat); empty → blake3(∅).
152
+ * Store key encoding is irrelevant; we always sort decoded (binary) hashes.
153
+ */
154
+ msgcheck(): Promise<Hash>;
155
+ /**
156
+ * Peek each host from seq 0 and pull any bags not already in the archive.
157
+ * Leaves open msgs as APLD_PENDING; does not exec (rebuild replays everything).
158
+ */
159
+ private repull;
160
+ /**
161
+ * Clear app state and re-exec every archived msg (HLC newest-first batches).
162
+ * Caller must hold applyChain.
163
+ */
164
+ private replay;
135
165
  wipe(): Promise<void>;
136
166
  import: (file: File, options?: {
137
167
  onProgress?: (index: number, total: number, status: Status) => void;
@@ -1,5 +1,5 @@
1
1
  import { Status } from "../shared/consts";
2
- import { EntityID, IOp } from "../shared/types";
2
+ import { EntityID, Hash, ICrypto, IOp } from "../shared/types";
3
3
  import { ValStat } from "../shared/valstat";
4
4
  import { EntitiesQuery, IEntDB, IEntity } from "./entdb";
5
5
  export type EntChangeListener = (types: Set<string>) => void;
@@ -59,4 +59,6 @@ export declare class CachedEntDB implements IEntDB {
59
59
  countEntities({ type }: {
60
60
  type: string;
61
61
  }): Promise<ValStat<number>>;
62
+ /** Durable authority — mem may be only partially warm. */
63
+ checksum(crypto: ICrypto): Promise<ValStat<Hash>>;
62
64
  }
@@ -1,6 +1,6 @@
1
1
  import { Status } from "../shared/consts";
2
2
  import { ValStat } from "../shared/valstat";
3
- import { EntityID, GroupID, IEntRev, IMessageHead, IMsgEntBody, IOp } from "../shared/types";
3
+ import { EntityID, GroupID, Hash, ICrypto, IEntRev, IMessageHead, IMsgEntBody, IOp } from "../shared/types";
4
4
  export { entStateManager } from "./manager.ts";
5
5
  export interface IEntity<T = unknown> extends Omit<IMsgEntBody<T>, "body"> {
6
6
  eid: EntityID;
@@ -49,6 +49,11 @@ export interface IEntDB {
49
49
  countEntities({ type }: {
50
50
  type: string;
51
51
  }): Promise<ValStat<number>>;
52
+ /**
53
+ * Frontier checksum of live rows: eid + updatedAt + ctr per ent
54
+ * (see encodeEntRev / checksumEntRevs). Not a content hash of bodies.
55
+ */
56
+ checksum(crypto: ICrypto): Promise<ValStat<Hash>>;
52
57
  }
53
58
  /** Prior rev from a loaded entity (typical update/delete input). */
54
59
  export declare function revFromEntity(ent: IEntity): IEntRev;
@@ -1,5 +1,5 @@
1
1
  import { Status } from "../shared/consts";
2
- import { EntityID, GroupID, IOp } from "../shared/types";
2
+ import { EntityID, GroupID, Hash, ICrypto, IOp } from "../shared/types";
3
3
  import { ValStat } from "../shared/valstat.ts";
4
4
  import { EntitiesQuery, IEntDB, IEntity } from "./entdb";
5
5
  export declare const entityTableName = "entities";
@@ -9,9 +9,21 @@ export declare const typeGroupIndexName = "entity_type_group_id";
9
9
  export declare const typeParentIndexName = "entity_type_parent_id";
10
10
  /** multiEntry index on tags[]; query by tag then filter by type. */
11
11
  export declare const tagsIndexName = "entity_tags";
12
+ /** Shared by main thread and sync worker — must stay in lockstep. */
13
+ export declare const ENT_IDB_NAME = "db";
14
+ /** v12: multiEntry tags index (entity_tags). */
15
+ export declare const ENT_IDB_VERSION = 12;
12
16
  export declare class EntIDB implements IEntDB {
13
17
  db: IDBDatabase | undefined;
18
+ /** In-flight open; coalesces concurrent ensureDb / peer upgrade reopen. */
19
+ private opening;
14
20
  init(): Promise<void>;
21
+ /**
22
+ * Open (or reopen) the shared EntDB IDB connection.
23
+ * Main + worker both hold connections; on versionchange we must close so the
24
+ * peer's upgrade is not blocked forever (classic multi-connection hang).
25
+ */
26
+ private ensureDb;
15
27
  apply: (ops: IOp[]) => Promise<{
16
28
  stats: Status[];
17
29
  types: Set<string>;
@@ -28,6 +40,7 @@ export declare class EntIDB implements IEntDB {
28
40
  countEntities({ type }: {
29
41
  type: string;
30
42
  }): Promise<ValStat<number>>;
43
+ checksum(crypto: ICrypto): Promise<ValStat<Hash>>;
31
44
  }
32
45
  /** Durable IndexedDB EntDB. Internal-only. Apps use {@link openEntDB} instead. */
33
46
  export declare function openEntIDB(): Promise<EntIDB>;
@@ -1,6 +1,6 @@
1
1
  import { IEntDB, IEntity } from "./entdb";
2
2
  import { Status } from "../shared/consts";
3
- import { EntityID, GroupID, IOp } from "../shared/types";
3
+ import { EntityID, GroupID, Hash, ICrypto, IOp } from "../shared/types";
4
4
  import { ValStat } from "../shared/valstat.ts";
5
5
  interface IDateRange {
6
6
  start: Date;
@@ -53,5 +53,6 @@ export declare class EntDBMemory implements IEntDB {
53
53
  countEntities({ type }: {
54
54
  type: string;
55
55
  }): Promise<ValStat<number>>;
56
+ checksum(crypto: ICrypto): Promise<ValStat<Hash>>;
56
57
  }
57
58
  export {};
@@ -22,6 +22,7 @@ import type { ApldState, Applier, IClient, IDiplomaticClientState, IHostRow, ISt
22
22
  import { APLD_APPLIED, APLD_ERROR, APLD_PENDING, apldFromStored, isApldState, isPendingApply, isTerminalApplyFailure, setApld } from "./types";
23
23
  import type { SyncProgressEvent } from "./progress";
24
24
  import { defaultPeekProgressEvery, idleProgress, shouldEmitItemProgress } from "./progress";
25
+ import { checksumEntRevs, checksumSet, cmpBytes, encodeEntRev } from "./shared/checksum";
25
26
  import { openDiplomaticClient, type OpenDiplomaticClientMainOptions, type OpenDiplomaticClientOptions, type OpenDiplomaticClientWorkerOptions, type OpenedDiplomaticClient } from "./openClient";
26
27
  import { WorkerClient } from "./worker/client";
27
28
  import type { WorkerClientOptions } from "./worker/client";
@@ -29,5 +30,5 @@ export declare function genWebClient(stateMgr: IStateManager, url: URL): Promise
29
30
  client: SyncClient<URL>;
30
31
  setSeed: (seedHex: string) => Promise<void>;
31
32
  }>;
32
- export { APLD_APPLIED, APLD_ERROR, APLD_PENDING, apldFromStored, b64tob, btob64, btoh, CachedEntDB, Clock, crypto, Decoder, defaultPeekProgressEvery, eidCodec, Encoder, EntDBMemory, EntIDB, EntitiesQuery, EntityID, entStateManager, genSingletonEID, GroupID, hostHTTPTransport, htob, HTTPTransport, IDBStore, idleProgress, IEntDB, IEntity, isApldState, isPendingApply, isTerminalApplyFailure, IStore, MasterSeed, MemoryStore, normalizeTags, nullEntDB, nullStateManager, openDiplomaticClient, openEntDB, openIDBStore, revFromEntity, revFromHead, setApld, shouldEmitItemProgress, SingletonStateManager, StateManager, Status, SyncClient, TypedEventEmitter, useClient, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, WorkerClient, };
33
+ export { APLD_APPLIED, APLD_ERROR, APLD_PENDING, apldFromStored, b64tob, btob64, btoh, CachedEntDB, checksumEntRevs, checksumSet, Clock, cmpBytes, crypto, Decoder, defaultPeekProgressEvery, eidCodec, encodeEntRev, Encoder, EntDBMemory, EntIDB, EntitiesQuery, EntityID, entStateManager, genSingletonEID, GroupID, hostHTTPTransport, htob, HTTPTransport, IDBStore, idleProgress, IEntDB, IEntity, isApldState, isPendingApply, isTerminalApplyFailure, IStore, MasterSeed, MemoryStore, normalizeTags, nullEntDB, nullStateManager, openDiplomaticClient, openEntDB, openIDBStore, revFromEntity, revFromHead, setApld, shouldEmitItemProgress, SingletonStateManager, StateManager, Status, SyncClient, TypedEventEmitter, useClient, useClientState, useClientXferState, useStateWatcher, useStateWatcherSuspense, useSyncOnResume, WorkerClient, };
33
34
  export type { ApldState, Applier, CachedEntDBOptions, EntDBMemoryOptions, HostHandle, IClient, ICrypto, IDeleteParams, IDiplomaticClientState, IEntRev, IHostConnectionInfo, IHostRow, IMessage, IMutateOp, IOp, IStateManager, IStoredMessage, IStoredMessageData, IStoredMessageWrite, ITransport, IUpdateParams, OpenDiplomaticClientMainOptions, OpenDiplomaticClientOptions, OpenDiplomaticClientWorkerOptions, OpenedDiplomaticClient, OpenEntDBOptions, SyncProgressEvent, WorkerClientOptions, };