@agentvault/grok-bridge 0.4.5 → 0.4.7

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 (2) hide show
  1. package/dist/index.js +111 -55
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -64605,11 +64605,11 @@ var init_mls_kp_pool = __esm2({
64605
64605
  }
64606
64606
  });
64607
64607
  function ownIdentity() {
64608
- const v2 = true ? "0.23.32" : FALLBACK;
64608
+ const v2 = true ? "0.23.34" : FALLBACK;
64609
64609
  return `${PACKAGE}@${v2}`;
64610
64610
  }
64611
64611
  function buildSha() {
64612
- const raw = true ? "564f8b1-dirty" : "";
64612
+ const raw = true ? "6b305ca-dirty" : "";
64613
64613
  const cleaned = (raw ?? "").trim();
64614
64614
  return !cleaned || cleaned === "unknown" ? null : cleaned;
64615
64615
  }
@@ -65524,6 +65524,7 @@ var OUTBOUND_DEDUPE_MAX;
65524
65524
  var OUTBOUND_DEDUPE_TYPES;
65525
65525
  var RETRY_CANDIDATE_TTL_MS;
65526
65526
  var RETRY_CANDIDATE_MAX;
65527
+ var ROOM_FULFIL_COOLDOWN_MS;
65527
65528
  var SecureChannel;
65528
65529
  var init_channel = __esm2({
65529
65530
  async "src/channel.ts"() {
@@ -65569,6 +65570,7 @@ var init_channel = __esm2({
65569
65570
  OUTBOUND_DEDUPE_TYPES = /* @__PURE__ */ new Set(["text", "status_alert", "artifact", "attachment"]);
65570
65571
  RETRY_CANDIDATE_TTL_MS = 6e4;
65571
65572
  RETRY_CANDIDATE_MAX = 32;
65573
+ ROOM_FULFIL_COOLDOWN_MS = 6e4;
65572
65574
  SecureChannel = class _SecureChannel extends EventEmitter {
65573
65575
  constructor(config2) {
65574
65576
  super();
@@ -65638,6 +65640,8 @@ var init_channel = __esm2({
65638
65640
  _drDeliveryPulling = false;
65639
65641
  /** MLS group managers per room/conversation (roomId or conv:conversationId or 1to1-group:groupId -> MLSGroupManager) */
65640
65642
  _mlsGroups = /* @__PURE__ */ new Map();
65643
+ /** `${mlsGroupId}:${deviceId}` -> last re-admit ms (#1171). */
65644
+ _roomFulfilCooldown = /* @__PURE__ */ new Map();
65641
65645
  /** Maps conversationId -> conversation_group_id for shared 1:1 MLS groups */
65642
65646
  _sessionGroupIds = /* @__PURE__ */ new Map();
65643
65647
  /** Consecutive MLS decrypt failure count per group key for epoch recovery. */
@@ -66803,7 +66807,7 @@ var init_channel = __esm2({
66803
66807
  */
66804
66808
  sendActivitySpan(spanData) {
66805
66809
  if (!this._ws || this._ws.readyState !== WebSocket.OPEN) return;
66806
- const pluginVersion = true ? "0.23.32" : "0.0.0-dev";
66810
+ const pluginVersion = true ? "0.23.34" : "0.0.0-dev";
66807
66811
  const agentName = this.config.agentName ?? "Agent";
66808
66812
  const resource = {
66809
66813
  "service.name": "agentvault-agent",
@@ -68741,7 +68745,7 @@ var init_channel = __esm2({
68741
68745
  agentVersion: this.config.agentVersion ?? "0.0.0",
68742
68746
  // __AV_VERSION__ is injected by esbuild from package.json at build time.
68743
68747
  // Falls back to "0.0.0-dev" in non-bundled contexts (tests).
68744
- pluginVersion: true ? "0.23.32" : "0.0.0-dev"
68748
+ pluginVersion: true ? "0.23.34" : "0.0.0-dev"
68745
68749
  });
68746
68750
  this._telemetryReporter.startAutoFlush(3e4);
68747
68751
  }
@@ -69065,7 +69069,7 @@ var init_channel = __esm2({
69065
69069
  agentVersion: this.config.agentVersion ?? "0.0.0",
69066
69070
  // __AV_VERSION__ is injected by esbuild from package.json at build time.
69067
69071
  // Falls back to "0.0.0-dev" in non-bundled contexts (tests).
69068
- pluginVersion: true ? "0.23.32" : "0.0.0-dev"
69072
+ pluginVersion: true ? "0.23.34" : "0.0.0-dev"
69069
69073
  });
69070
69074
  this._telemetryReporter.startAutoFlush(3e4);
69071
69075
  }
@@ -69469,6 +69473,7 @@ var init_channel = __esm2({
69469
69473
  try {
69470
69474
  const cipherBytes = new Uint8Array(Buffer.from(data.payload, "hex"));
69471
69475
  const result = await mgr.decrypt(cipherBytes);
69476
+ if (mgrKey) this._mlsDecryptFailCounts.delete(mgrKey);
69472
69477
  const mlsGroupId = (convGroupId ? this._persisted?.mlsGroups?.[convGroupId]?.mlsGroupId : null) ?? this._persisted?.mlsConversations?.[convId]?.mlsGroupId ?? groupId;
69473
69478
  if (mlsGroupId) {
69474
69479
  await saveMlsState(this.config.dataDir, mlsGroupId, JSON.stringify(mgr.exportState()));
@@ -69573,6 +69578,10 @@ var init_channel = __esm2({
69573
69578
  this.config.onMessage(text, metadata);
69574
69579
  }
69575
69580
  } catch (decryptErr) {
69581
+ if (classifyCommitFailure(data.epoch, mgr?.epoch) === "already-applied") {
69582
+ console.debug(`[SecureChannel] MLS 1:1 message at epoch ${String(data.epoch)} predates our epoch ${String(mgr?.epoch)} \u2014 cannot decrypt, ignoring`);
69583
+ return;
69584
+ }
69576
69585
  const failKey = mgrKey || `unknown:${groupId}`;
69577
69586
  const count = (this._mlsDecryptFailCounts.get(failKey) ?? 0) + 1;
69578
69587
  this._mlsDecryptFailCounts.set(failKey, count);
@@ -70414,6 +70423,7 @@ ${baseText}`;
70414
70423
  try {
70415
70424
  const ciphertext = Buffer.from(data.payload, "hex");
70416
70425
  const result = await mlsGroup.decrypt(new Uint8Array(ciphertext));
70426
+ this._mlsDecryptFailCounts.delete(resolvedRoomId);
70417
70427
  await saveMlsState(this.config.dataDir, groupId, JSON.stringify(mlsGroup.exportState()));
70418
70428
  if (result.isHandshake || !result.plaintext) {
70419
70429
  return;
@@ -70503,6 +70513,10 @@ ${baseText}`;
70503
70513
  console.error("[SecureChannel] onMessage callback error (MLS):", err);
70504
70514
  });
70505
70515
  } catch (err) {
70516
+ if (classifyCommitFailure(data.epoch, mlsGroup.epoch) === "already-applied") {
70517
+ console.debug(`[SecureChannel] MLS room message at epoch ${String(data.epoch)} predates our epoch ${String(mlsGroup.epoch)} for ${resolvedRoomId.slice(0, 8)} \u2014 cannot decrypt, ignoring`);
70518
+ return;
70519
+ }
70506
70520
  const failKey = resolvedRoomId;
70507
70521
  const count = (this._mlsDecryptFailCounts.get(failKey) ?? 0) + 1;
70508
70522
  this._mlsDecryptFailCounts.set(failKey, count);
@@ -70754,13 +70768,7 @@ ${baseText}`;
70754
70768
  console.warn(`[SecureChannel] MLS epoch recovery for ${groupKey} (${mlsGroupId.slice(0, 8)})`);
70755
70769
  this._mlsGroups.delete(groupKey);
70756
70770
  this._mlsDecryptFailCounts.delete(groupKey);
70757
- try {
70758
- const { unlink: unlink2 } = await import("fs/promises");
70759
- const path2 = `${this.config.dataDir}/.mls-state/${mlsGroupId}.json`;
70760
- await unlink2(path2).catch(() => {
70761
- });
70762
- } catch {
70763
- }
70771
+ await deleteMlsState(this.config.dataDir, mlsGroupId);
70764
70772
  try {
70765
70773
  const mgr = new MLSGroupManager();
70766
70774
  const identity = new TextEncoder().encode(this._deviceId);
@@ -70835,19 +70843,26 @@ ${baseText}`;
70835
70843
  return 0;
70836
70844
  }
70837
70845
  const snapshot = mlsGroup.exportState();
70838
- const { commit, welcome } = await mlsGroup.addMembers(targets.map((t2) => t2.keyPackage));
70839
- const commitSent = await this._sendFrameConfirmed(
70840
- {
70841
- event: "mls_commit",
70842
- data: {
70843
- group_id: mlsGroupId,
70844
- epoch: Number(mlsGroup.epoch),
70845
- payload: Buffer.from(commit).toString("hex")
70846
- }
70847
- },
70848
- `${label} commit`
70849
- );
70850
- if (!commitSent) {
70846
+ const { commits, welcome } = await mlsGroup.reAdmitMembers(targets.map((t2) => t2.keyPackage));
70847
+ let sentCommits = 0;
70848
+ for (const c2 of commits) {
70849
+ const ok = await this._sendFrameConfirmed(
70850
+ {
70851
+ event: "mls_commit",
70852
+ data: {
70853
+ group_id: mlsGroupId,
70854
+ // The epoch this commit PRODUCED. After two commits the manager
70855
+ // only knows the last one, which is why the result carries them.
70856
+ epoch: Number(c2.epoch),
70857
+ payload: Buffer.from(c2.commit).toString("hex")
70858
+ }
70859
+ },
70860
+ `${label} commit ${sentCommits + 1}/${commits.length}`
70861
+ );
70862
+ if (!ok) break;
70863
+ sentCommits++;
70864
+ }
70865
+ if (sentCommits === 0) {
70851
70866
  mlsGroup.importState(snapshot);
70852
70867
  console.warn(
70853
70868
  `[SecureChannel] ${label}: commit was NOT written \u2014 rolled back to epoch ${Number(mlsGroup.epoch)}, nothing fulfilled, requests stay pending`
@@ -70855,6 +70870,12 @@ ${baseText}`;
70855
70870
  return 0;
70856
70871
  }
70857
70872
  await saveMlsState(this.config.dataDir, mlsGroupId, JSON.stringify(mlsGroup.exportState()));
70873
+ if (sentCommits < commits.length) {
70874
+ console.warn(
70875
+ `[SecureChannel] ${label}: only ${sentCommits}/${commits.length} commits reached the wire \u2014 requests stay PENDING for the next pull`
70876
+ );
70877
+ return 0;
70878
+ }
70858
70879
  if (!welcome) {
70859
70880
  console.warn(
70860
70881
  `[SecureChannel] ${label}: addMembers produced NO Welcome \u2014 ${targets.length} request(s) left pending rather than marked fulfilled`
@@ -71535,6 +71556,11 @@ ${baseText}`;
71535
71556
  nackedIds.push(msg.queue_id);
71536
71557
  if (msg.group_id && (msg.message_type === "commit" || msg.message_type === "application")) {
71537
71558
  const failKey = msg.conversation_group_id ? `1to1-group:${msg.conversation_group_id}` : `group:${msg.group_id}`;
71559
+ const localMgr = msg.room_id && this._mlsGroups.get(msg.room_id) || this._mlsGroups.get(failKey);
71560
+ if (classifyCommitFailure(msg.epoch, localMgr?.epoch) === "already-applied") {
71561
+ console.debug(`[SecureChannel] Delivery ${msg.message_type} at epoch ${String(msg.epoch)} predates our epoch ${String(localMgr?.epoch)} \u2014 not counting toward recovery`);
71562
+ continue;
71563
+ }
71538
71564
  const count = (this._mlsDecryptFailCounts.get(failKey) ?? 0) + 1;
71539
71565
  this._mlsDecryptFailCounts.set(failKey, count);
71540
71566
  if (count >= _SecureChannel.MAX_MLS_DECRYPT_FAILS) {
@@ -71583,38 +71609,63 @@ ${baseText}`;
71583
71609
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
71584
71610
  );
71585
71611
  if (!pendingRes.ok) continue;
71586
- const pending = await pendingRes.json();
71612
+ let pending = await pendingRes.json();
71587
71613
  if (pending.length === 0) continue;
71588
71614
  console.log(`[SecureChannel] ${pending.length} pending Welcome requests for room ${roomId.slice(0, 8)}`);
71615
+ const nowMs = Date.now();
71616
+ const fresh = pending.filter((r2) => {
71617
+ const key = `${roomState.mlsGroupId}:${r2.requesting_device_id}`;
71618
+ const last = this._roomFulfilCooldown.get(key) ?? 0;
71619
+ if (nowMs - last < ROOM_FULFIL_COOLDOWN_MS) {
71620
+ console.log(
71621
+ `[SecureChannel] skipping ${r2.requesting_device_id.slice(0, 8)} in room ${roomId.slice(0, 8)} \u2014 re-admitted ${Math.round((nowMs - last) / 1e3)}s ago`
71622
+ );
71623
+ return false;
71624
+ }
71625
+ return true;
71626
+ });
71627
+ if (fresh.length === 0) continue;
71628
+ for (const r2 of fresh) {
71629
+ this._roomFulfilCooldown.set(`${roomState.mlsGroupId}:${r2.requesting_device_id}`, nowMs);
71630
+ }
71631
+ pending = fresh;
71632
+ const deviceIds = pending.map((r2) => r2.requesting_device_id);
71633
+ const kpRes = await fetch(
71634
+ `${this.config.apiUrl}/api/v1/mls/key-packages/batch?device_ids=${deviceIds.join(",")}`,
71635
+ { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
71636
+ );
71637
+ if (!kpRes.ok) continue;
71638
+ const kpData = await kpRes.json();
71639
+ const roomTargets = [];
71589
71640
  for (const req of pending) {
71590
- try {
71591
- const kpRes = await fetch(
71592
- `${this.config.apiUrl}/api/v1/mls/key-packages/batch?device_ids=${req.requesting_device_id}`,
71593
- { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
71641
+ const kpHex = kpData[req.requesting_device_id];
71642
+ if (!kpHex) {
71643
+ console.warn(`[SecureChannel] No KeyPackage for ${req.requesting_device_id.slice(0, 8)}, skipping`);
71644
+ continue;
71645
+ }
71646
+ const kpBytes = new Uint8Array(Buffer.from(kpHex, "hex"));
71647
+ roomTargets.push({
71648
+ requestId: req.id,
71649
+ deviceId: req.requesting_device_id,
71650
+ keyPackage: MLSGroupManager.deserializeKeyPackage(kpBytes)
71651
+ });
71652
+ }
71653
+ if (roomTargets.length === 0) continue;
71654
+ try {
71655
+ const done = await this._fulfilWelcomeRequests({
71656
+ mlsGroup,
71657
+ mlsGroupId: roomState.mlsGroupId,
71658
+ label: `room ${roomId.slice(0, 8)}`,
71659
+ welcomeFields: { room_id: roomId },
71660
+ targets: roomTargets
71661
+ });
71662
+ if (done > 0) {
71663
+ console.log(
71664
+ `[SecureChannel] Batched Welcome for ${done}/${roomTargets.length} device(s) in room ${roomId.slice(0, 8)} (epoch=${mlsGroup.epoch})`
71594
71665
  );
71595
- if (!kpRes.ok) continue;
71596
- const kpData = await kpRes.json();
71597
- const kpHex = kpData[req.requesting_device_id];
71598
- if (!kpHex) continue;
71599
- const kpBytes = new Uint8Array(Buffer.from(kpHex, "hex"));
71600
- const memberKp = MLSGroupManager.deserializeKeyPackage(kpBytes);
71601
- const done = await this._fulfilWelcomeRequests({
71602
- mlsGroup,
71603
- mlsGroupId: roomState.mlsGroupId,
71604
- label: `room ${roomId.slice(0, 8)}`,
71605
- welcomeFields: { room_id: roomId },
71606
- targets: [{
71607
- requestId: req.id,
71608
- deviceId: req.requesting_device_id,
71609
- keyPackage: memberKp
71610
- }]
71611
- });
71612
- if (done > 0) {
71613
- console.log(`[SecureChannel] Fulfilled Welcome for ${req.requesting_device_id.slice(0, 8)} in room ${roomId.slice(0, 8)}`);
71614
- }
71615
- } catch (fulfillErr) {
71616
- console.warn(`[SecureChannel] Welcome fulfill failed for ${req.requesting_device_id.slice(0, 8)}:`, fulfillErr);
71617
71666
  }
71667
+ } catch (fulfillErr) {
71668
+ console.warn(`[SecureChannel] Batched Welcome failed for room ${roomId.slice(0, 8)}:`, fulfillErr);
71618
71669
  }
71619
71670
  } catch {
71620
71671
  }
@@ -72187,6 +72238,7 @@ ${baseText}`;
72187
72238
  try {
72188
72239
  const ciphertext = new Uint8Array(Buffer.from(data.payload, "hex"));
72189
72240
  const result = await mlsGroup.decrypt(ciphertext);
72241
+ this._mlsDecryptFailCounts.delete(`a2a:${a2aChannelId}`);
72190
72242
  await saveMlsState(this.config.dataDir, groupId, JSON.stringify(mlsGroup.exportState()));
72191
72243
  if (result.isHandshake || !result.plaintext) {
72192
72244
  return;
@@ -72207,6 +72259,10 @@ ${baseText}`;
72207
72259
  this.config.onA2AMessage(a2aMsg);
72208
72260
  }
72209
72261
  } catch (err) {
72262
+ if (classifyCommitFailure(data.epoch, mlsGroup?.epoch) === "already-applied") {
72263
+ console.debug(`[SecureChannel] MLS A2A message at epoch ${String(data.epoch)} predates our epoch ${String(mlsGroup?.epoch)} for ${a2aChannelId.slice(0, 8)} \u2014 cannot decrypt, ignoring`);
72264
+ return;
72265
+ }
72210
72266
  const failKey = `a2a:${a2aChannelId}`;
72211
72267
  const count = (this._mlsDecryptFailCounts.get(failKey) ?? 0) + 1;
72212
72268
  this._mlsDecryptFailCounts.set(failKey, count);
@@ -99425,7 +99481,7 @@ var init_index = __esm2({
99425
99481
  await init_skill_telemetry();
99426
99482
  await init_policy_enforcer();
99427
99483
  init_room_protocol();
99428
- VERSION = true ? "0.23.32" : "0.0.0-dev";
99484
+ VERSION = true ? "0.23.34" : "0.0.0-dev";
99429
99485
  }
99430
99486
  });
99431
99487
  await init_index();
@@ -100351,7 +100407,7 @@ async function main() {
100351
100407
  const info = parseInfoFlags(process.argv.slice(2));
100352
100408
  if (info.showVersion || info.showHelp) {
100353
100409
  const version2 = buildClientVersion2(
100354
- true ? "0.4.5" : void 0
100410
+ true ? "0.4.7" : void 0
100355
100411
  );
100356
100412
  if (info.showVersion) printLine(version2);
100357
100413
  if (info.showHelp) {
@@ -100404,7 +100460,7 @@ async function main() {
100404
100460
  agentName: cfg.agentName,
100405
100461
  platform: "node",
100406
100462
  clientVersion: buildClientVersion2(
100407
- true ? "0.4.5" : void 0
100463
+ true ? "0.4.7" : void 0
100408
100464
  )
100409
100465
  });
100410
100466
  channelRef = channel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentvault/grok-bridge",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "type": "module",
5
5
  "description": "AgentVault Grok Bridge — daemon bridging a Grok agent into E2E-encrypted AgentVault direct messages and rooms.",
6
6
  "main": "dist/index.js",