@securecoms/agent-bridge 0.27.0 → 0.28.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.
Files changed (49) hide show
  1. package/README.md +41 -0
  2. package/dist/bin.d.ts +1 -1
  3. package/dist/bin.js +3396 -380
  4. package/dist/bridge-agent.d.ts +182 -1
  5. package/dist/config.d.ts +7 -0
  6. package/dist/embedder-manifest.json +52 -0
  7. package/dist/embedder-worker.js +2898 -0
  8. package/dist/embedder.js +1574 -0
  9. package/dist/index.js +3201 -306
  10. package/dist/memory/consumer-recall.d.ts +21 -0
  11. package/dist/memory/embedder/client.d.ts +103 -0
  12. package/dist/memory/embedder/download.d.ts +28 -0
  13. package/dist/memory/embedder/embedder-main.d.ts +1 -0
  14. package/dist/memory/embedder/embedder-paths.d.ts +14 -0
  15. package/dist/memory/embedder/engine.d.ts +21 -0
  16. package/dist/memory/embedder/gc.d.ts +2 -0
  17. package/dist/memory/embedder/handshake.d.ts +7 -0
  18. package/dist/memory/embedder/heartbeats.d.ts +4 -0
  19. package/dist/memory/embedder/import-bundle.d.ts +6 -0
  20. package/dist/memory/embedder/inner-source.d.ts +2 -0
  21. package/dist/memory/embedder/layout.d.ts +36 -0
  22. package/dist/memory/embedder/locks.d.ts +17 -0
  23. package/dist/memory/embedder/manifest.d.ts +30 -0
  24. package/dist/memory/embedder/no-dial.d.ts +3 -0
  25. package/dist/memory/embedder/protocol.d.ts +55 -0
  26. package/dist/memory/embedder/run-dir.d.ts +11 -0
  27. package/dist/memory/embedder/scheduler.d.ts +24 -0
  28. package/dist/memory/embedder/server.d.ts +46 -0
  29. package/dist/memory/embedder/session.d.ts +17 -0
  30. package/dist/memory/embedder/spawn.d.ts +40 -0
  31. package/dist/memory/embedder/tokenizer.d.ts +41 -0
  32. package/dist/memory/embedder/types.d.ts +32 -0
  33. package/dist/memory/embedder/windows.d.ts +46 -0
  34. package/dist/memory/embedder/worker-body.d.ts +44 -0
  35. package/dist/memory/embedder/worker.d.ts +1 -0
  36. package/dist/memory/memory-config.d.ts +13 -0
  37. package/dist/memory/note-frames.d.ts +5 -2
  38. package/dist/memory/notes-book.d.ts +21 -3
  39. package/dist/memory/provider.d.ts +2 -0
  40. package/dist/memory/recall/call-heartbeat.d.ts +35 -0
  41. package/dist/memory/recall/membership-purge.d.ts +82 -0
  42. package/dist/memory/recall/rank.d.ts +15 -0
  43. package/dist/memory/recall/recall-book.d.ts +140 -0
  44. package/dist/memory/recall/reembed.d.ts +35 -0
  45. package/dist/memory-taps.d.ts +3 -2
  46. package/dist/ort-wasm-simd-threaded.wasm +0 -0
  47. package/dist/version.d.ts +1 -1
  48. package/dist/ws-server.d.ts +13 -1
  49. package/package.json +2 -1
@@ -33,6 +33,8 @@ import { type OwnerAuthorityStore, type OwnerPin } from "@agentvault/connect-cli
33
33
  import type { BridgeConfig } from "./config.js";
34
34
  import { LatencyRecorder } from "./latency.js";
35
35
  import type { AttachmentMeta, VaultRefEntry } from "./attachments.js";
36
+ import { type Embedder } from "./memory/embedder/client.js";
37
+ import { type RecallItem, type RecallTapCapture } from "./memory/recall/recall-book.js";
36
38
  import { type MemoryNoteResultFrame } from "./memory/note-frames.js";
37
39
  import { type ChatMessage } from "./voice/fast-prompt.js";
38
40
  import { VOICE_CALL_START_MARKER } from "./voice/prompt-compiler.js";
@@ -154,6 +156,12 @@ export interface MemoryNoteContext {
154
156
  token: number | undefined;
155
157
  /** Per-socket request ids still being decided (M-2). At most MAX_PENDING_NOTE_REQUESTS (review m-3). */
156
158
  pendingRequests: Set<string>;
159
+ /**
160
+ * 7b spec §9.1 (H5): the recall ids shown with THIS socket's delivery of `messageId` that carries
161
+ * `token`, else undefined. Task 18 fix round 1 (M2): the caller passes `ctx.token`, the token captured
162
+ * when the frame ARRIVED, never the token read after an await.
163
+ */
164
+ shownRecall(messageId: string, token: number | undefined): ReadonlySet<number> | undefined;
157
165
  }
158
166
  /** Review m-3: pending memory_note requests per socket; a frame beyond this refuses `invalid` with no echo. */
159
167
  export declare const MAX_PENDING_NOTE_REQUESTS = 8;
@@ -314,6 +322,26 @@ export interface BridgeAgentDeps {
314
322
  * it undefined and gets homedir().
315
323
  */
316
324
  home?: string;
325
+ /**
326
+ * 7b Task 15: the embedder. Tests inject a fake. Undefined in the production construction (`hostMachine`, no
327
+ * `home`) builds the real EmbedderClient; undefined anywhere else gets INERT_EMBEDDER, so no test reaches a real one
328
+ * (Task 19 fix M2: a test that injects neither no longer builds a real client on the developer's homedir()).
329
+ */
330
+ embedder?: Embedder;
331
+ /**
332
+ * 7b Task 19 fix M2: true only in the production construction (bin.ts). This agent owns machine-level state under
333
+ * homedir(): the call heartbeat (~/.securecoms/run/voice-call), the model marker and GC (~/.securecoms/models) and
334
+ * the one-second maintenance timer that drives them. A test that injects `home` gets the same, under its temp home.
335
+ * A construction with neither (most direct test constructions) touches none of those paths.
336
+ *
337
+ * Library callers: omit it and semantic recall stays inert (INERT_EMBEDDER: the report says `preparing` or
338
+ * `off`, no model is downloaded or loaded, no call heartbeat is written and other bridges' background work is
339
+ * never paused by this one). Pass `true` only for the one process per agent that should own those paths, as
340
+ * bin.ts does. host-machine.arch.test.ts pins bin.ts as the only production construction.
341
+ */
342
+ hostMachine?: boolean;
343
+ /** 7b Task 15 (E1 delta M5): injected platform, default process.platform. */
344
+ platform?: NodeJS.Platform;
317
345
  /** Injectable so tests can assert recorded stages; defaults to a fresh one. */
318
346
  latency?: LatencyRecorder;
319
347
  /**
@@ -1126,6 +1154,57 @@ export declare class BridgeAgent {
1126
1154
  /** The ONE follow-up cycle every trigger during the in-flight cycle shares. */
1127
1155
  private ingestFollowUp;
1128
1156
  private runtimeConfigTimer?;
1157
+ /** Semantic memory 7b (spec §5.4, audit r2 H-A): the hourly left-channel purge. */
1158
+ private membershipSweepTimer?;
1159
+ /** 7b Task 15 (spec §5.3): the hourly recall prune, modelled on the membership sweep. */
1160
+ private recallPruneTimer?;
1161
+ private recallPruneWork;
1162
+ private readonly platform;
1163
+ private readonly embedder;
1164
+ /** 7b Task 16: the recall index (the single writer, I8: reached only from tapRecall). */
1165
+ private readonly recall;
1166
+ /** 7b Task 19 (spec §8, H1, ruling P-1): this identity's call heartbeat, and every identity's for the pause. */
1167
+ private readonly heartbeat;
1168
+ /** 7b Task 19 (spec §8): lazy re-embed of pending rows (never an insert; I8 untouched). */
1169
+ private readonly reembed;
1170
+ /** 7b Task 19: the one-second maintenance timer (heartbeat refresh, re-embed, daily marker and GC). */
1171
+ private recallMaintenanceTimer?;
1172
+ private recallMaintenanceWork;
1173
+ private maintenanceTickN;
1174
+ /** The previous re-embed tick did work: poll again next second instead of waiting for the minute. */
1175
+ private reembedHot;
1176
+ private lastModelUpkeepAt;
1177
+ /** Fix L3: the marker is touched on the first tick with recall on, then daily, independent of the GC cadence. */
1178
+ private lastMarkerTouchAt;
1179
+ /** Fix M2: BridgeAgentDeps.hostMachine, or an injected home (see there). */
1180
+ private readonly machinePaths;
1181
+ /** Spec §19 LOW (Federal pre-flight notice): logged once per process. */
1182
+ private modelDownloadNoticeLogged;
1183
+ /**
1184
+ * 7b Task 16 (spec §4 M1, fix H-1): turns restored from runtime_inbox at
1185
+ * boot AND turns the boot drain ingested before the boot refetch settled;
1186
+ * tapped once by reindexRestoredTurns after it.
1187
+ */
1188
+ private readonly deferredRecallIds;
1189
+ /** 7b fix H-2: recall epochs captured at receipt in the ingest cycle, consumed by stageSealed (cleared at cycle end). */
1190
+ private readonly recallReceipt;
1191
+ /** 7b fix H-2: the persisted receipt epochs of each staged turn (like ingestGeneration; deleted at ack). */
1192
+ private readonly recallStageEpochs;
1193
+ /** 7b Task 16: in-flight fire-and-forget index taps; close() waits for them (bounded). */
1194
+ private readonly recallTapWork;
1195
+ /**
1196
+ * Final-review item 2: conversations whose `audience` skip was already logged in this process. A room (or any
1197
+ * conversation the owner-DM gate refuses) refuses every turn the same way, so the line is written once per
1198
+ * conversation per process, not once per inbound message. Bounded: cleared when it reaches 4096 entries.
1199
+ */
1200
+ private readonly recallAudienceLogged;
1201
+ /** Off until a payload arrives (spec §5.3: before the first payload of a process, recall neither indexes nor reads nor purges). */
1202
+ private recallConfig;
1203
+ /** The membership sweep now running, if any; close() waits for it (bounded). */
1204
+ private membershipSweepWork;
1205
+ /** Task 13 fix round LOW 3: when each channel last delivered an inbound row to this process. */
1206
+ private readonly lastInboundAt;
1207
+ private readonly membershipSweeper;
1129
1208
  private readonly listeners;
1130
1209
  private startedAt;
1131
1210
  private readonly store;
@@ -1173,6 +1252,8 @@ export declare class BridgeAgent {
1173
1252
  * closes the socket but is not a downgrade.
1174
1253
  */
1175
1254
  private notesConsumerSeen;
1255
+ /** 7b spec §7.2 Q4: a runtime socket (no harness claim) declared memory_recall at least once in this process (Task 19 reports it). */
1256
+ private recallConsumerSeen;
1176
1257
  /** Review M-1: the refetch now running, and the ONE follow-up every caller during it shares. */
1177
1258
  private refetchInFlight;
1178
1259
  private refetchFollowUp;
@@ -1812,6 +1893,17 @@ export declare class BridgeAgent {
1812
1893
  * A timeout rejects with the SDK's `FetchDeadlineError`.
1813
1894
  */
1814
1895
  private fetchBounded;
1896
+ /** H-A rule 4 (audit r1 C2): GET /api/v1/channels/{id}; only 404 is "gone" (404, not 403, for non-members). */
1897
+ private probeChannelMembership;
1898
+ /** §5.4: one conversation's turns, facts, notes and recall, inside the memory mutex, with every in-memory copy. */
1899
+ private purgeConversation;
1900
+ /**
1901
+ * 7b Task 17: the purged conversation's recall cache and pending map
1902
+ * entries go too, inside the memory mutex. The purge already moved the
1903
+ * purge and content epochs inside its transaction, so `warm` and
1904
+ * `deliverable` refuse even before this drop runs (M-A3, M-A4).
1905
+ */
1906
+ private onConversationPurged;
1815
1907
  /** M6 (Task 11): idempotent get-or-create resolve of the enclave's hidden
1816
1908
  * vault channel id — used as the fallback when a caller omits
1817
1909
  * vaultChannelId. Mirrors connect-web's filesafe-api.ts resolveVaultChannel
@@ -1875,6 +1967,7 @@ export declare class BridgeAgent {
1875
1967
  recordMemory(input: import("@agentvault/connect-mls-store").MemoryRecordInput): Promise<{
1876
1968
  seq: number;
1877
1969
  deduped: boolean;
1970
+ dropped?: true;
1878
1971
  }>;
1879
1972
  /**
1880
1973
  * OpenClaw collapses text history and workspace facts into the owner's
@@ -1906,6 +1999,82 @@ export declare class BridgeAgent {
1906
1999
  * this point keeps the last applied state.
1907
2000
  */
1908
2001
  private applyPayloadMemory;
2002
+ /** Spec §5.3 (H3): mode state lives in the store; level-triggered after every refetch. Never throws. */
2003
+ private applyRecallModeFromPayload;
2004
+ /** A retention change or a prune removed rows: drop the vector caches. Called only inside notes.exclusive. */
2005
+ private onRecallRetentionChangedLocked;
2006
+ /** Markdown mode deleted every row: drop the caches and the per-message map. Called only inside notes.exclusive. */
2007
+ private onRecallPurgedAllLocked;
2008
+ /**
2009
+ * 7b spec §7.1, §7.2: Q1 (config half) and Q2 here, then the 150 ms bound
2010
+ * inside RecallBook.prepare. Called beside prepareNotesForTurn, before the
2011
+ * turn is staged. The receipt epochs (Task 16 fix H-2) narrow the capture:
2012
+ * the in-cycle receipt, else the persisted staging marks; marks that exist
2013
+ * but lack an epoch give no recall (fail closed). Never throws.
2014
+ */
2015
+ private prepareRecallForTurn;
2016
+ /** ws flush (synchronous). Q1 to Q3 and Q6 again at delivery, on the ORIGINAL message; Q4 lives in withConsumerRecall. */
2017
+ recallForDelivery(m: BridgeMessage): readonly RecallItem[] | undefined;
2018
+ /** ws-server: a runtime socket with no harness claim declared memory_recall. */
2019
+ noteRecallConsumer(): void;
2020
+ /** @internal test seam */
2021
+ recallWarm(c: string): Promise<void>;
2022
+ /** @internal test seam */
2023
+ recallCacheWarm(c: string): boolean;
2024
+ /** @internal test seam */
2025
+ recallDeliverable(id: string, c: string): readonly RecallItem[] | undefined;
2026
+ /** I1 and Q1's config half (spec §4, §7.2): provider, payload semantic, kill switch, store enabled. */
2027
+ private recallProviderOn;
2028
+ /**
2029
+ * Spec §4: the tap's capture, synchronous, taken before the tap's first
2030
+ * await (fact 5: the ingest stamp is deleted at ack). The purge and mode
2031
+ * epochs are the ones persisted at staging, captured at RECEIPT (Task 14,
2032
+ * audit r2 M-A; fix H-2), so a purge or a mode change after receipt, even
2033
+ * across a restart, refuses the insert. A missing value is -1, which no
2034
+ * real epoch equals: the tap skips it (`purged` / `epoch`), fail closed.
2035
+ */
2036
+ captureRecallTap(messageId: string, channelId: string): RecallTapCapture;
2037
+ /** One content-free skip line (reason class only). */
2038
+ private logRecallSkip;
2039
+ /**
2040
+ * The single writer (I8). Gates I1 to I5 here, in spec §4 table order; I6
2041
+ * (capture half) here, and I6, I7, M-A and I9 again inside the store
2042
+ * transaction. A skip logs only its reason class. Never throws.
2043
+ */
2044
+ private tapRecall;
2045
+ /** Fire-and-forget tap from the ingest cycle; tracked so close() and tests can wait for it. */
2046
+ private spawnRecallTap;
2047
+ /** @internal test seam: resolves once every in-flight ingest tap has settled. */
2048
+ settleRecallTaps(): Promise<void>;
2049
+ /**
2050
+ * Spec §4 M1: after the boot refetch settles, tap the deferred turns: those
2051
+ * restored from runtime_inbox (a crash between staging and the tap) and
2052
+ * those the boot drain ingested before the recall config was known (fix
2053
+ * H-1). I9 absorbs a first run that committed. The authorization is the
2054
+ * frozen mark; I1 is re-read here; the capture carries the persisted
2055
+ * receipt epochs (fix H-2). A turn no longer held logs `stamp_missing`.
2056
+ */
2057
+ private reindexRestoredTurns;
2058
+ /**
2059
+ * Spec §5.3 hourly prune; audit r1 H5: inside the memory mutex, and a prune
2060
+ * that deleted rows drops the vector caches. Never throws.
2061
+ * @internal test seam
2062
+ */
2063
+ runRecallPruneTick(): Promise<void>;
2064
+ /**
2065
+ * 7b Task 19, the one-second maintenance tick minus the heartbeat refresh (the timer does that). Never throws.
2066
+ * - Once a day (and on the first tick): touch this identity's model marker while recall is on and the files are
2067
+ * on disk, then GC model trees this manifest does not name and no marker has used for 30 days (spec §6.4 item 6).
2068
+ * - Re-embed (spec §8) while recall is on: every minute, and every second while the previous tick did work.
2069
+ * @internal test seam
2070
+ */
2071
+ runRecallMaintenanceTick(n: number): Promise<void>;
2072
+ /**
2073
+ * Spec §9.2 (Task 19): capability versus state, plus the H6 hold count and the newest completed sweep time
2074
+ * (Task 13 carry). Pure reads: never starts, connects or spawns the embedder (plan audit r1 LOW).
2075
+ */
2076
+ private reportedRecall;
2077
+ private reportedRecallState;
1909
2078
  /**
1910
2079
  * Review M-5: the report key only for a value the backend accepts
1911
2080
  * (0..MAX_CLEAR_GENERATION); otherwise the key is omitted, so one bad
@@ -2475,7 +2644,12 @@ export declare class BridgeAgent {
2475
2644
  * which Date.parse would read as LOCAL time, so a bare timestamp is UTC.
2476
2645
  */
2477
2646
  private recordRecvLag;
2647
+ /**
2648
+ * 7b Task 16 carry: the cycle's receipt epochs are dropped in a `finally`,
2649
+ * so a cycle that throws leaves no stale entry for a later staging to read.
2650
+ */
2478
2651
  private ingestCycle;
2652
+ private ingestCycleBody;
2479
2653
  drain(channelId: string | undefined, waitSeconds: number): Promise<BridgeMessage[]>;
2480
2654
  /** Notify-only SSE signal: fired after a buffer push. Listener pulls via takeBuffered. */
2481
2655
  subscribe(listener: () => void): () => void;
@@ -2588,7 +2762,12 @@ export declare class BridgeAgent {
2588
2762
  isRuntimeMessageRetired(messageId: string): boolean;
2589
2763
  /** Complete the durable handoff only after the runtime explicitly acks. */
2590
2764
  ackRuntimeMessages(messageIds: string[]): void;
2591
- /** The sealed runtime_inbox payload: the message plus its ingest stamp (never on the message object). */
2765
+ /**
2766
+ * The sealed runtime_inbox payload: the message plus its ingest stamp and receipt recall epochs (never on the message object).
2767
+ * Task 18 fix round 1: any bridge-private key already on the message is stripped first (the same splitStagedKeys), so
2768
+ * only the bridge's own marks are persisted. A message whose W9 budget is durably spent (agent_memory_w9_spent: an ack
2769
+ * then a backend redelivery) is staged with W9_KEY = W9_MAX, so the budget stays spent.
2770
+ */
2592
2771
  private stagedPayload;
2593
2772
  /**
2594
2773
  * Seal `message` into runtime_inbox with the current ingest stamp and record
@@ -2601,6 +2780,8 @@ export declare class BridgeAgent {
2601
2780
  * valid stamp gets none, so its write refuses `cleared`.
2602
2781
  */
2603
2782
  private stageSealed;
2783
+ /** In-memory mirror of the persisted marks (staging and restore). */
2784
+ private applyStagedMarks;
2604
2785
  private restoreRuntimeInbox;
2605
2786
  /**
2606
2787
  * Seal a runtime frame before the backend queue id may be acknowledged.
package/dist/config.d.ts CHANGED
@@ -38,6 +38,13 @@ export interface BridgeConfig {
38
38
  * a backend voice-settings refetch replaces `voice` wholesale.
39
39
  */
40
40
  voiceFastPathMode?: FastPathMode;
41
+ /**
42
+ * Spec §6.6 (M6): credential `memory.model_source`. "import_only" stops every model
43
+ * network connection; files arrive only through `models import <dir>`. Local only:
44
+ * the backend cannot set it (it never writes the credential). Anything but the exact
45
+ * string "import_only" is "download".
46
+ */
47
+ modelSource: "download" | "import_only";
41
48
  }
42
49
  export declare const REQUIRED: readonly ["api_key", "identity_id", "device_id", "signing_private_key", "enclave_id", "api_base"];
43
50
  export declare function loadConfig(opts: {
@@ -0,0 +1,52 @@
1
+ {
2
+ "manifest": {
3
+ "modelId": "minilm-l6-v2-int8",
4
+ "dims": 384,
5
+ "maxTokens": 256,
6
+ "pooling": "mean",
7
+ "normalize": true,
8
+ "similarityThreshold": 0.19,
9
+ "onnxInputs": [
10
+ "input_ids",
11
+ "attention_mask",
12
+ "token_type_ids"
13
+ ],
14
+ "onnxOutput": "last_hidden_state",
15
+ "wasmSha256": "ec8580a9d7b9476ceee52e10a7f94124e4dc71a019d666ed6d4726697c109a4d",
16
+ "wasmSize": 13961845,
17
+ "files": [
18
+ {
19
+ "name": "model.onnx",
20
+ "urlPath": "/models/embeddings/minilm-l6-v2-int8/afdb6f1a0e45b715d0bb9b11772f032c399babd23bfc31fed1c170afc848bdb1/model.onnx",
21
+ "sha256": "afdb6f1a0e45b715d0bb9b11772f032c399babd23bfc31fed1c170afc848bdb1",
22
+ "size": 22972370
23
+ },
24
+ {
25
+ "name": "vocab.txt",
26
+ "urlPath": "/models/embeddings/minilm-l6-v2-int8/07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3/vocab.txt",
27
+ "sha256": "07eced375cec144d27c900241f3e339478dec958f92fddbc551f295c992038a3",
28
+ "size": 231508
29
+ },
30
+ {
31
+ "name": "LICENSE",
32
+ "urlPath": "/models/embeddings/minilm-l6-v2-int8/d83f8f68a86e308e30d1ddf3d1b9984500f0c39ab8a1c7d274ab2799c21155b4/LICENSE",
33
+ "sha256": "d83f8f68a86e308e30d1ddf3d1b9984500f0c39ab8a1c7d274ab2799c21155b4",
34
+ "size": 9934
35
+ },
36
+ {
37
+ "name": "NOTICE",
38
+ "urlPath": "/models/embeddings/minilm-l6-v2-int8/99446995c7800bd9ce033f24c477b00daa50f06da85e441e903f4ce1e32c7507/NOTICE",
39
+ "sha256": "99446995c7800bd9ce033f24c477b00daa50f06da85e441e903f4ce1e32c7507",
40
+ "size": 320
41
+ }
42
+ ]
43
+ },
44
+ "tokenizer": {
45
+ "lowercase": true,
46
+ "stripAccents": true,
47
+ "unkToken": "[UNK]",
48
+ "unknownDropped": false,
49
+ "addSpecialTokens": true,
50
+ "maxInputCharsPerWord": 100
51
+ }
52
+ }