@securecoms/agent-bridge 0.21.0 → 0.23.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.
@@ -61,6 +61,74 @@ let _runtime = null;
61
61
  // Active channel instances keyed by accountId.
62
62
  const _channels = new Map();
63
63
 
64
+ // ---------------------------------------------------------------------------
65
+ // Inbound delivery contract (P0 containment Task 8).
66
+ //
67
+ // The daemon delivers at-least-once: a `message` frame stays in-flight until
68
+ // we `ack` it, and is re-buffered for the next consumer when the socket drops.
69
+ // So:
70
+ // - a message_id we already handled is acked again and NEVER re-dispatched
71
+ // (bounded `seen` set, same shape as the native runtime's data plane);
72
+ // - on first sight we ask the daemon's content-free reply-receipt ledger
73
+ // (GET /v1/reply-receipt) whether a reply was already published for it —
74
+ // a replacement host must not re-run the model/tools for an answered turn;
75
+ // - a failed turn is NOT acked. We close the socket so the daemon re-buffers
76
+ // the frame for a clean retry (leaving it in-flight on a live socket would
77
+ // strand it until the next reconnect);
78
+ // - a runtime that is not registered yet is the same "retry later" case,
79
+ // never a silent drop;
80
+ // - a message that keeps failing is given up on after MAX_INBOUND_ATTEMPTS
81
+ // handler failures (acked + logged), so one poison message cannot re-run
82
+ // tools forever. Runtime-not-ready and a failed receipt lookup do NOT
83
+ // count toward that cap: neither ran the turn, and giving up on them
84
+ // would be a silent drop of a message nobody processed.
85
+ // ---------------------------------------------------------------------------
86
+
87
+ export const MAX_INBOUND_ATTEMPTS = 3;
88
+ const SEEN_MAX = 5000;
89
+
90
+ /** Thrown by the inbound handler when register() has not injected a runtime. */
91
+ export class RuntimeNotReadyError extends Error {
92
+ constructor() {
93
+ super("OpenClaw runtime not registered yet");
94
+ this.name = "RuntimeNotReadyError";
95
+ }
96
+ }
97
+
98
+ /** Thrown when the reply-receipt lookup cannot answer (never a handler failure). */
99
+ class ReceiptLookupError extends Error {
100
+ constructor(detail) {
101
+ super(`reply receipt lookup failed: ${detail}`);
102
+ this.name = "ReceiptLookupError";
103
+ }
104
+ }
105
+
106
+ /**
107
+ * The agent's OWNER, as attested by the daemon's authorization block. This is
108
+ * the exact predicate the bridge uses for owner-scoped OpenClaw memory
109
+ * (BridgeAgent.mayReadOpenClawMemoryForTurn): audience "owner_dm" with an
110
+ * "active" sender. The block is derived by the daemon from server-authenticated
111
+ * roster data, never from message plaintext. Anything else — including a
112
+ * missing block from an older daemon, an unknown version, or the owner
113
+ * speaking in a group room (the daemon does not classify that as owner) —
114
+ * is NOT the owner. Fail closed.
115
+ */
116
+ export function isOwnerAuthorization(authorization) {
117
+ return (
118
+ authorization !== null &&
119
+ typeof authorization === "object" &&
120
+ authorization.version === 1 &&
121
+ authorization.audience === "owner_dm" &&
122
+ authorization.sender_membership === "active"
123
+ );
124
+ }
125
+
126
+ /** "direct" only for a DM the daemon classified as one; everything else is a group. */
127
+ export function chatTypeFor(authorization) {
128
+ const audience = authorization && typeof authorization === "object" ? authorization.audience : undefined;
129
+ return audience === "owner_dm" || audience === "other_human_dm" ? "direct" : "group";
130
+ }
131
+
64
132
  // ---------------------------------------------------------------------------
65
133
  // Account / identity / connection-file resolution.
66
134
  // ---------------------------------------------------------------------------
@@ -188,6 +256,14 @@ export class LoopbackChannel {
188
256
  this._stopped = false;
189
257
  this._attempt = 0;
190
258
  this._ownIdentity = null;
259
+ // Inbound dedup/retry state (see "Inbound delivery contract" above).
260
+ this._seen = new Set();
261
+ this._inFlight = new Set();
262
+ this._failures = new Map();
263
+ // Consecutive failed turns. While > 0, a WS open does NOT reset the
264
+ // reconnect backoff, so a failing turn is retried with growing delay
265
+ // instead of a ~250 ms redelivery loop.
266
+ this._failStreak = 0;
191
267
  }
192
268
 
193
269
  _setState(s) {
@@ -226,10 +302,7 @@ export class LoopbackChannel {
226
302
  // like the no-token case below, never attempt the gate with an
227
303
  // undefined fileIdentity, never open a WS.
228
304
  this._setState("idle");
229
- this.log(
230
- `[securecoms] no connection file at ~/.securecoms/connections/${this.boundIdentity}.json yet; ` +
231
- "the daemon authors it on startup — ensure the daemon is upgraded and running",
232
- );
305
+ this.log("[SecureComs] no connection file at configured location; waiting for daemon");
233
306
  this._scheduleReconnect();
234
307
  return;
235
308
  }
@@ -248,9 +321,9 @@ export class LoopbackChannel {
248
321
  let h;
249
322
  try {
250
323
  h = await (await fetch(`${this.bridgeUrl}/healthz`)).json();
251
- } catch (e) {
324
+ } catch {
252
325
  this._setState("error");
253
- this.log(`[SecureComs] daemon unreachable at ${this.bridgeUrl}: ${e?.message ?? e}`);
326
+ this.log("[SecureComs] daemon unreachable");
254
327
  this._scheduleReconnect();
255
328
  return;
256
329
  }
@@ -262,14 +335,14 @@ export class LoopbackChannel {
262
335
  });
263
336
  if (!gate.ok) {
264
337
  this._setState("error");
265
- this.log(`[SecureComs] refusing to attach: ${gate.reason}`);
338
+ this.log("[SecureComs] refusing to attach: identity gate failed");
266
339
  this._scheduleReconnect();
267
340
  return;
268
341
  }
269
342
 
270
343
  this.deviceId = h.identity_id;
271
344
  this.sessionCount = h.joined_channels ?? 0;
272
- this.log(`[SecureComs] daemon ok identity=${h.identity_id} joined=${h.joined_channels}`);
345
+ this.log("[SecureComs] daemon identity gate passed");
273
346
  // Echo-suppression anchor — set ONLY after the gate passes.
274
347
  this._ownIdentity = this.boundIdentity;
275
348
  // Backoff is reset on a real WS open (see _connect's "open" handler), NOT
@@ -294,10 +367,11 @@ export class LoopbackChannel {
294
367
 
295
368
  ws.addEventListener("open", () => {
296
369
  this._setState("ready");
297
- this.log(`[SecureComs] connected to daemon WS ${this.wsUrl}`);
370
+ this.log("[SecureComs] connected to daemon WebSocket");
298
371
  // Backoff resets only on a real WS open — a passing gate alone isn't
299
- // enough (see comment in _probeAndGate).
300
- this._attempt = 0;
372
+ // enough (see comment in _probeAndGate) — and not while turns are
373
+ // failing: a close we forced for redelivery must still back off.
374
+ if (this._failStreak === 0) this._attempt = 0;
301
375
  });
302
376
 
303
377
  ws.addEventListener("message", (ev) => {
@@ -306,22 +380,7 @@ export class LoopbackChannel {
306
380
  frame = JSON.parse(typeof ev.data === "string" ? ev.data : ev.data.toString());
307
381
  } catch { return; }
308
382
  if (frame.type !== "message") return;
309
- // Never feed our own sends back into the agent (no echo loop).
310
- if (frame.sender_id && this._ownIdentity && frame.sender_id === this._ownIdentity) {
311
- this._ack(frame.message_id);
312
- return;
313
- }
314
- Promise.resolve(
315
- this.onMessage?.(frame.text ?? "", {
316
- conversationId: frame.channel_id,
317
- messageId: frame.message_id,
318
- senderId: frame.sender_id,
319
- timestamp: frame.ts,
320
- }),
321
- )
322
- .catch((e) => this.log(`[SecureComs] onMessage error: ${e?.message ?? e}`))
323
- // at-least-once: ack after the turn completes so a crash mid-handle replays.
324
- .finally(() => this._ack(frame.message_id));
383
+ void this._onInboundFrame(frame);
325
384
  });
326
385
 
327
386
  ws.addEventListener("close", () => {
@@ -333,15 +392,129 @@ export class LoopbackChannel {
333
392
  this._scheduleReconnect();
334
393
  });
335
394
 
336
- ws.addEventListener("error", (e) => {
337
- this.log(`[SecureComs] WS error: ${e?.message ?? "unknown"}`);
395
+ ws.addEventListener("error", () => {
396
+ this.log("[SecureComs] WebSocket error");
338
397
  });
339
398
  }
340
399
 
400
+ // Acks go to the CURRENT socket, not the one the frame arrived on: if that
401
+ // socket was replaced mid-turn, the daemon re-buffered the frame onto the
402
+ // new one, and only an ack there clears it.
341
403
  _ack(messageId) {
342
404
  try { this._ws?.send(JSON.stringify({ type: "ack", message_id: messageId })); } catch { /* ignore */ }
343
405
  }
344
406
 
407
+ _remember(messageId) {
408
+ if (this._seen.size >= SEEN_MAX) {
409
+ const oldest = this._seen.values().next();
410
+ if (!oldest.done) this._seen.delete(oldest.value);
411
+ }
412
+ this._seen.add(messageId);
413
+ }
414
+
415
+ // Close the current socket so the daemon re-buffers every un-acked frame
416
+ // for redelivery. The close handler re-probes and re-gates as usual.
417
+ _requestRedelivery() {
418
+ try { this._ws?.close(); } catch { /* already closing */ }
419
+ }
420
+
421
+ async _hasReplyReceipt(messageId) {
422
+ let r;
423
+ try {
424
+ r = await fetch(`${this.bridgeUrl}/v1/reply-receipt?in_reply_to=${encodeURIComponent(messageId)}`, {
425
+ headers: { authorization: `Bearer ${this.token}` },
426
+ });
427
+ } catch {
428
+ throw new ReceiptLookupError("daemon unreachable");
429
+ }
430
+ if (!r.ok) throw new ReceiptLookupError(`HTTP ${r.status}`);
431
+ let body;
432
+ try {
433
+ body = await r.json();
434
+ } catch {
435
+ // A 200 with a non-JSON/unparseable body is a daemon-side lookup
436
+ // failure, not a handler failure: it must not count toward
437
+ // MAX_INBOUND_ATTEMPTS (that would eventually ack-and-drop a message
438
+ // that was never actually processed).
439
+ throw new ReceiptLookupError("invalid JSON body");
440
+ }
441
+ return body?.found === true;
442
+ }
443
+
444
+ /**
445
+ * Handle one inbound `message` frame under the at-least-once contract (see
446
+ * "Inbound delivery contract" at the top of this file). Never throws.
447
+ */
448
+ async _onInboundFrame(frame) {
449
+ const messageId = frame.message_id;
450
+ if (typeof messageId !== "string" || messageId === "") {
451
+ this.log("[SecureComs] inbound frame without message_id; ignoring");
452
+ return;
453
+ }
454
+ // Never feed our own sends back into the agent (no echo loop).
455
+ if (frame.sender_id && this._ownIdentity && frame.sender_id === this._ownIdentity) {
456
+ this._remember(messageId);
457
+ this._ack(messageId);
458
+ return;
459
+ }
460
+ if (this._seen.has(messageId)) {
461
+ // Already handled (or reconciled via a reply receipt): ack the
462
+ // redelivered copy, never run the turn twice.
463
+ this._ack(messageId);
464
+ return;
465
+ }
466
+ if (this._inFlight.has(messageId)) {
467
+ // Redelivered onto a replacement socket while the original turn is
468
+ // still running. The original's completion acks on the current socket.
469
+ return;
470
+ }
471
+
472
+ this._inFlight.add(messageId);
473
+ try {
474
+ if (await this._hasReplyReceipt(messageId)) {
475
+ this._remember(messageId);
476
+ this._ack(messageId);
477
+ return;
478
+ }
479
+ await this.onMessage?.(typeof frame.text === "string" ? frame.text : "", {
480
+ conversationId: frame.channel_id,
481
+ messageId,
482
+ senderId: frame.sender_id,
483
+ senderName: frame.sender_name,
484
+ authorization: frame.authorization,
485
+ timestamp: frame.ts,
486
+ });
487
+ this._failures.delete(messageId);
488
+ this._failStreak = 0;
489
+ this._remember(messageId);
490
+ this._ack(messageId);
491
+ } catch (err) {
492
+ this._failStreak++;
493
+ if (err instanceof RuntimeNotReadyError || err instanceof ReceiptLookupError) {
494
+ this.log("[SecureComs] inbound message not processed yet (runtime not ready or reply-receipt lookup failed); leaving unacked for redelivery");
495
+ this._requestRedelivery();
496
+ return;
497
+ }
498
+ const attempts = (this._failures.get(messageId) ?? 0) + 1;
499
+ if (attempts >= MAX_INBOUND_ATTEMPTS) {
500
+ this._failures.delete(messageId);
501
+ this.log("[SecureComs] inbound turn failed MAX_INBOUND_ATTEMPTS times; giving up and acking the message");
502
+ this._remember(messageId);
503
+ this._ack(messageId);
504
+ return;
505
+ }
506
+ if (this._failures.size >= SEEN_MAX) {
507
+ const oldest = this._failures.keys().next();
508
+ if (!oldest.done) this._failures.delete(oldest.value);
509
+ }
510
+ this._failures.set(messageId, attempts);
511
+ this.log("[SecureComs] inbound turn failed; leaving unacked for redelivery");
512
+ this._requestRedelivery();
513
+ } finally {
514
+ this._inFlight.delete(messageId);
515
+ }
516
+ }
517
+
345
518
  /** Send a reply into a specific SecureComs channel via the daemon. */
346
519
  async send(channelId, text, inReplyTo) {
347
520
  const r = await fetch(`${this.bridgeUrl}/v1/send`, {
@@ -390,17 +563,33 @@ export class LoopbackChannel {
390
563
 
391
564
  async function _handleInbound({ text, metadata, channel, account, cfg }) {
392
565
  const core = _runtime;
393
- if (!core) return;
394
-
395
- const conv = metadata.conversationId ?? "default";
396
- const senderId = "securecoms:owner";
566
+ if (!core) throw new RuntimeNotReadyError();
397
567
 
568
+ const conv = metadata.conversationId;
569
+ if (typeof conv !== "string" || conv === "") {
570
+ throw new Error("inbound frame has no channel_id");
571
+ }
572
+ // The REAL sender, as the daemon decrypted and attributed it. Never a
573
+ // hard-coded owner: who sent a message and whether they may run commands
574
+ // are separate facts, and only the daemon's authorization block speaks to
575
+ // the second.
576
+ const senderId = typeof metadata.senderId === "string" && metadata.senderId !== "" ? metadata.senderId : "unknown";
577
+ const senderName = typeof metadata.senderName === "string" && metadata.senderName !== "" ? metadata.senderName : senderId;
578
+ const chatType = chatTypeFor(metadata.authorization);
579
+ const commandAuthorized = isOwnerAuthorization(metadata.authorization);
580
+
581
+ // Route by the CHANNEL, so each SecureComs room/DM is its own conversation.
398
582
  const route = core.channel.routing.resolveAgentRoute({
399
583
  cfg,
400
584
  channel: "securecoms",
401
585
  accountId: account.accountId,
402
- peer: { kind: "direct", id: senderId },
586
+ peer: { kind: chatType, id: conv },
403
587
  });
588
+ // Session isolation is enforced HERE, not delegated to OpenClaw's dmScope:
589
+ // under the default dmScope every direct peer collapses into the agent's
590
+ // main session, which would merge two different humans' DMs (and the
591
+ // owner's) into one history. One session per SecureComs channel, always.
592
+ const sessionKey = `agent:${route.agentId}:securecoms:${chatType}:${conv}`;
404
593
 
405
594
  const storePath = core.channel.session.resolveStorePath(cfg?.session?.store, {
406
595
  agentId: route.agentId,
@@ -409,12 +598,12 @@ async function _handleInbound({ text, metadata, channel, account, cfg }) {
409
598
  const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg);
410
599
  const previousTimestamp = core.channel.session.readSessionUpdatedAt({
411
600
  storePath,
412
- sessionKey: route.sessionKey,
601
+ sessionKey,
413
602
  });
414
603
 
415
604
  const body = core.channel.reply.formatAgentEnvelope({
416
605
  channel: "SecureComs",
417
- from: "Owner",
606
+ from: senderName,
418
607
  timestamp: new Date(metadata.timestamp).getTime(),
419
608
  previousTimestamp,
420
609
  envelope: envelopeOptions,
@@ -425,13 +614,13 @@ async function _handleInbound({ text, metadata, channel, account, cfg }) {
425
614
  Body: body,
426
615
  RawBody: text,
427
616
  CommandBody: text,
428
- From: senderId,
617
+ From: `securecoms:${senderId}`,
429
618
  To: `securecoms:agent:${account.accountId}`,
430
- SessionKey: route.sessionKey,
619
+ SessionKey: sessionKey,
431
620
  AccountId: account.accountId,
432
- ChatType: "direct",
621
+ ChatType: chatType,
433
622
  ConversationLabel: "SecureComs",
434
- SenderName: "Owner",
623
+ SenderName: senderName,
435
624
  SenderId: senderId,
436
625
  Provider: "securecoms",
437
626
  Surface: "securecoms",
@@ -439,14 +628,15 @@ async function _handleInbound({ text, metadata, channel, account, cfg }) {
439
628
  Timestamp: new Date(metadata.timestamp).getTime(),
440
629
  OriginatingChannel: "securecoms",
441
630
  OriginatingTo: `securecoms:agent:${account.accountId}`,
442
- // The owner is cryptographically authenticated by the SecureComs daemon
443
- // (MLS membership + bridge token); authorize command execution.
444
- CommandAuthorized: true,
631
+ // Command execution is the OWNER's alone, as attested by the daemon's
632
+ // authorization block (isOwnerAuthorization). MLS membership proves a
633
+ // sender is in the room, not that they own this agent.
634
+ CommandAuthorized: commandAuthorized,
445
635
  });
446
636
 
447
637
  await core.channel.session.recordInboundSession({
448
638
  storePath,
449
- sessionKey: ctxPayload.SessionKey ?? route.sessionKey,
639
+ sessionKey,
450
640
  ctx: ctxPayload,
451
641
  onRecordError: (err) => core.error?.(`[SecureComs] session record failed: ${String(err)}`),
452
642
  });
@@ -495,7 +685,7 @@ const secureComsPlugin = {
495
685
  aliases: ["sc", "secure-coms"],
496
686
  },
497
687
  capabilities: {
498
- chatTypes: ["direct", "room"],
688
+ chatTypes: ["direct", "group", "room"],
499
689
  },
500
690
  config: {
501
691
  listAccountIds,
@@ -535,15 +725,18 @@ const secureComsPlugin = {
535
725
  boundIdentity: account.boundIdentity,
536
726
  sc: cfg?.channels?.securecoms,
537
727
  log: _log,
728
+ // Throws on failure (including runtime-not-ready) so the channel
729
+ // leaves the frame unacked for redelivery — never a silent drop.
538
730
  onMessage: async (text, metadata) => {
539
731
  if (!_runtime) {
540
- _log?.("[SecureComs] runtime not ready — dropping inbound message");
541
- return;
732
+ _log?.("[SecureComs] runtime not ready — leaving inbound message unacked for redelivery");
733
+ throw new RuntimeNotReadyError();
542
734
  }
543
735
  try {
544
736
  await _handleInbound({ text, metadata, channel, account, cfg });
545
737
  } catch (err) {
546
738
  _log?.(`[SecureComs] inbound dispatch error: ${String(err)}`);
739
+ throw err;
547
740
  }
548
741
  },
549
742
  // Errors are recoverable (probe/gate/backoff retries automatically —
package/dist/bin.d.ts CHANGED
@@ -3,13 +3,9 @@ import { type ResolveNodeDeps } from "./service/resolve-node.js";
3
3
  import { type Harness } from "./service/wire.js";
4
4
  export declare function nodeMajorOk(version: string): boolean;
5
5
  /**
6
- * P0b (daemon-stability): a redacted fingerprint of a bearer token for logs —
7
- * a short prefix plus the total length, never the raw value. Lets an
8
- * operator eyeball "yes, a token was loaded, and it looks like the one I
9
- * expect" without the full secret ever hitting stderr (see the tokenFile
10
- * comment at the top of this file). Mirrors the install-path token hint's
11
- * philosophy of pointing at the token FILE rather than printing the secret
12
- * (see service/install.ts's tokenHint, which returns the bridge-token path).
6
+ * A redacted bearer-token hint for explicit foreground install output: a short
7
+ * prefix plus the total length, never the raw value. Long-running service
8
+ * diagnostics do not call this helper or expose even the hint.
13
9
  */
14
10
  export declare function tokenFingerprint(token: string): string;
15
11
  export declare function parseVerb(argv: string[]): "install" | "uninstall" | "status" | "wire" | "unwire" | null;