@agentvault/agentvault 0.23.52 → 0.23.53

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.
package/dist/index.js CHANGED
@@ -64526,11 +64526,11 @@ var init_mls_kp_pool = __esm({
64526
64526
 
64527
64527
  // src/client-version.ts
64528
64528
  function ownIdentity() {
64529
- const v2 = true ? "0.23.52" : FALLBACK;
64529
+ const v2 = true ? "0.23.53" : FALLBACK;
64530
64530
  return `${PACKAGE}@${v2}`;
64531
64531
  }
64532
64532
  function buildSha() {
64533
- const raw = true ? "d7143c3" : "";
64533
+ const raw = true ? "3f2a933" : "";
64534
64534
  const cleaned = (raw ?? "").trim();
64535
64535
  return !cleaned || cleaned === "unknown" ? null : cleaned;
64536
64536
  }
@@ -65442,6 +65442,122 @@ var init_decision_reply = __esm({
65442
65442
  }
65443
65443
  });
65444
65444
 
65445
+ // src/welcome-kp-backoff.ts
65446
+ var KP_BACKOFF_INITIAL_MS, KP_BACKOFF_MAX_MS, KP_RACE_RETRY_MS, KP_RACE_RETRY_LIMIT, consoleLogger, short, WelcomeKeyPackageBackoff;
65447
+ var init_welcome_kp_backoff = __esm({
65448
+ "src/welcome-kp-backoff.ts"() {
65449
+ "use strict";
65450
+ KP_BACKOFF_INITIAL_MS = 10 * 6e4;
65451
+ KP_BACKOFF_MAX_MS = 60 * 6e4;
65452
+ KP_RACE_RETRY_MS = 6e4;
65453
+ KP_RACE_RETRY_LIMIT = 3;
65454
+ consoleLogger = {
65455
+ warn: (m2) => console.warn(m2),
65456
+ info: (m2) => console.log(m2)
65457
+ };
65458
+ short = (id) => id.slice(0, 8);
65459
+ WelcomeKeyPackageBackoff = class {
65460
+ constructor(log = consoleLogger) {
65461
+ this.log = log;
65462
+ }
65463
+ log;
65464
+ entries = /* @__PURE__ */ new Map();
65465
+ pendingSig = /* @__PURE__ */ new Map();
65466
+ failing = /* @__PURE__ */ new Set();
65467
+ key(groupId, deviceId) {
65468
+ return `${groupId}:${deviceId}`;
65469
+ }
65470
+ /**
65471
+ * Record this pull's pending list for a group. Returns true when the set of
65472
+ * requests differs from the previous pull's — the caller logs its "N pending"
65473
+ * line only then. Backoffs for devices whose request is gone are dropped.
65474
+ */
65475
+ observePending(groupId, pending) {
65476
+ const sig = pending.map((r2) => r2.id).sort().join(",");
65477
+ const changed = this.pendingSig.get(groupId) !== sig;
65478
+ if (pending.length === 0) this.pendingSig.delete(groupId);
65479
+ else this.pendingSig.set(groupId, sig);
65480
+ const live = new Set(pending.map((r2) => r2.requesting_device_id));
65481
+ const prefix = `${groupId}:`;
65482
+ for (const k2 of [...this.entries.keys()]) {
65483
+ if (k2.startsWith(prefix) && !live.has(k2.slice(prefix.length))) this.entries.delete(k2);
65484
+ }
65485
+ return changed && pending.length > 0;
65486
+ }
65487
+ /** Should this pull ask the batch for this device's KeyPackage? */
65488
+ shouldAsk(groupId, deviceId, available, label, now = Date.now()) {
65489
+ const k2 = this.key(groupId, deviceId);
65490
+ const entry = this.entries.get(k2);
65491
+ if (available === false) {
65492
+ if (!entry) {
65493
+ this.entries.set(k2, { untilMs: now + KP_BACKOFF_INITIAL_MS, delayMs: KP_BACKOFF_INITIAL_MS, lastAvailable: false, raceRetries: 0 });
65494
+ this.log.warn(
65495
+ `[SecureChannel] No KeyPackage for ${short(deviceId)} in ${label} \u2014 waiting for the device to come back online; not asking again until it publishes one`
65496
+ );
65497
+ } else {
65498
+ entry.lastAvailable = false;
65499
+ }
65500
+ return false;
65501
+ }
65502
+ if (!entry) return true;
65503
+ if (available === true && entry.lastAvailable === false) return true;
65504
+ return now >= entry.untilMs;
65505
+ }
65506
+ /** The outcome of an ask. `found` = the batch returned a KeyPackage. */
65507
+ recordKeyPackage(groupId, deviceId, found, available, label, now = Date.now()) {
65508
+ const k2 = this.key(groupId, deviceId);
65509
+ const entry = this.entries.get(k2);
65510
+ if (found) {
65511
+ if (entry) {
65512
+ this.entries.delete(k2);
65513
+ this.log.info(`[SecureChannel] KeyPackage for ${short(deviceId)} is available again in ${label} \u2014 admitting`);
65514
+ }
65515
+ return;
65516
+ }
65517
+ const raced = available === true && (entry?.raceRetries ?? 0) < KP_RACE_RETRY_LIMIT;
65518
+ if (!entry) {
65519
+ if (raced) {
65520
+ this.entries.set(k2, { untilMs: now + KP_RACE_RETRY_MS, delayMs: 0, lastAvailable: available, raceRetries: 1 });
65521
+ this.log.warn(
65522
+ `[SecureChannel] No KeyPackage for ${short(deviceId)} in ${label} although the server listed one (claimed elsewhere?) \u2014 retrying in ${KP_RACE_RETRY_MS / 1e3} s`
65523
+ );
65524
+ return;
65525
+ }
65526
+ this.entries.set(k2, { untilMs: now + KP_BACKOFF_INITIAL_MS, delayMs: KP_BACKOFF_INITIAL_MS, lastAvailable: available, raceRetries: 0 });
65527
+ this.log.warn(
65528
+ `[SecureChannel] No KeyPackage for ${short(deviceId)} in ${label}, skipping \u2014 not asking again for ${KP_BACKOFF_INITIAL_MS / 6e4} min (the device has to come back online)`
65529
+ );
65530
+ return;
65531
+ }
65532
+ entry.lastAvailable = available;
65533
+ if (raced) {
65534
+ entry.raceRetries += 1;
65535
+ entry.untilMs = now + KP_RACE_RETRY_MS;
65536
+ return;
65537
+ }
65538
+ entry.raceRetries = available === true ? entry.raceRetries : 0;
65539
+ entry.delayMs = entry.delayMs === 0 ? KP_BACKOFF_INITIAL_MS : Math.min(entry.delayMs * 2, KP_BACKOFF_MAX_MS);
65540
+ entry.untilMs = now + entry.delayMs;
65541
+ }
65542
+ /** A non-OK response for `scope`: warn once until it recovers. */
65543
+ noteFetchFailure(scope, status) {
65544
+ if (this.failing.has(scope)) return;
65545
+ this.failing.add(scope);
65546
+ this.log.warn(`[SecureChannel] ${scope} failed (${status}) \u2014 will retry each pull; logging once until it recovers`);
65547
+ }
65548
+ noteFetchOk(scope) {
65549
+ if (!this.failing.delete(scope)) return;
65550
+ this.log.info(`[SecureChannel] ${scope} recovered`);
65551
+ }
65552
+ /** Test/diagnostic view. */
65553
+ backoffFor(groupId, deviceId) {
65554
+ const e = this.entries.get(this.key(groupId, deviceId));
65555
+ return e ? { untilMs: e.untilMs, delayMs: e.delayMs } : void 0;
65556
+ }
65557
+ };
65558
+ }
65559
+ });
65560
+
65445
65561
  // src/history-backup-client.ts
65446
65562
  var history_backup_client_exports = {};
65447
65563
  __export(history_backup_client_exports, {
@@ -66538,6 +66654,7 @@ var init_channel = __esm({
66538
66654
  init_history_backup_scheduler();
66539
66655
  await init_history_recovery_code();
66540
66656
  init_decision_reply();
66657
+ init_welcome_kp_backoff();
66541
66658
  ROOM_AGENT_TYPES = /* @__PURE__ */ new Set([
66542
66659
  "message",
66543
66660
  "text",
@@ -66648,6 +66765,8 @@ var init_channel = __esm({
66648
66765
  _mlsGroups = /* @__PURE__ */ new Map();
66649
66766
  /** `${mlsGroupId}:${deviceId}` -> last re-admit ms (#1171). */
66650
66767
  _roomFulfilCooldown = /* @__PURE__ */ new Map();
66768
+ /** #1428: per (group, device) KeyPackage backoff for the pending-Welcome pulls. */
66769
+ _welcomeKpBackoff = new WelcomeKeyPackageBackoff();
66651
66770
  /** Maps conversationId -> conversation_group_id for shared 1:1 MLS groups */
66652
66771
  _sessionGroupIds = /* @__PURE__ */ new Map();
66653
66772
  /** Consecutive MLS decrypt failure count per group key for epoch recovery. */
@@ -68734,7 +68853,7 @@ ${text}`;
68734
68853
  */
68735
68854
  sendActivitySpan(spanData) {
68736
68855
  if (!this._ws || this._ws.readyState !== WebSocket.OPEN) return;
68737
- const pluginVersion = true ? "0.23.52" : "0.0.0-dev";
68856
+ const pluginVersion = true ? "0.23.53" : "0.0.0-dev";
68738
68857
  const agentName = this.config.agentName ?? "Agent";
68739
68858
  const resource = {
68740
68859
  "service.name": "agentvault-agent",
@@ -70911,7 +71030,7 @@ ${text}`;
70911
71030
  agentVersion: this.config.agentVersion ?? "0.0.0",
70912
71031
  // __AV_VERSION__ is injected by esbuild from package.json at build time.
70913
71032
  // Falls back to "0.0.0-dev" in non-bundled contexts (tests).
70914
- pluginVersion: true ? "0.23.52" : "0.0.0-dev"
71033
+ pluginVersion: true ? "0.23.53" : "0.0.0-dev"
70915
71034
  });
70916
71035
  this._telemetryReporter.startAutoFlush(3e4);
70917
71036
  }
@@ -71239,7 +71358,7 @@ ${text}`;
71239
71358
  agentVersion: this.config.agentVersion ?? "0.0.0",
71240
71359
  // __AV_VERSION__ is injected by esbuild from package.json at build time.
71241
71360
  // Falls back to "0.0.0-dev" in non-bundled contexts (tests).
71242
- pluginVersion: true ? "0.23.52" : "0.0.0-dev"
71361
+ pluginVersion: true ? "0.23.53" : "0.0.0-dev"
71243
71362
  });
71244
71363
  this._telemetryReporter.startAutoFlush(3e4);
71245
71364
  }
@@ -73175,7 +73294,7 @@ ${baseText}`;
73175
73294
  return 0;
73176
73295
  }
73177
73296
  const snapshot = mlsGroup.exportState();
73178
- const preHead = await this._readGroupHead(mlsGroupId);
73297
+ const preHead = opts.preHead ?? await this._readGroupHead(mlsGroupId);
73179
73298
  if (preHead.ok && preHead.epoch !== Number(mlsGroup.epoch)) {
73180
73299
  console.warn(
73181
73300
  `[SecureChannel] ${label}: server head is at epoch ${preHead.epoch}, we are at ${Number(mlsGroup.epoch)} \u2014 NOT committing (it would be refused, #1184); ${targets.length} request(s) stay pending`
@@ -73362,6 +73481,40 @@ ${baseText}`;
73362
73481
  _wsIsOpen() {
73363
73482
  return this._ws?.readyState === 1;
73364
73483
  }
73484
+ /**
73485
+ * #1429 review: the cheap checks `_fulfilWelcomeRequests` makes, run BEFORE a
73486
+ * pending-Welcome site claims KeyPackages — the order #1225 S4 gave the A2A
73487
+ * path. `GET /key-packages/batch` CONSUMES; the room, shared-1:1 and conv
73488
+ * sites used to claim one per pending device and only then find the socket
73489
+ * closed, another commit in flight, or the server head elsewhere — burning a
73490
+ * package per device per failed pull (and the shared/conv sites have no
73491
+ * cooldown). `_fulfilWelcomeRequests` keeps its own checks: this is a gate on
73492
+ * spending, not a replacement for the guard at the point of commit.
73493
+ */
73494
+ /**
73495
+ * Returns the head it read (handed on to `_fulfilWelcomeRequests` as its
73496
+ * pre-flight, so a pull still reads the head once before committing), or
73497
+ * `null` when nothing should be claimed this pull.
73498
+ */
73499
+ async _welcomeCommitPreflight(mlsGroupId, mlsGroup, label) {
73500
+ if (!this._wsIsOpen()) {
73501
+ console.warn(`[SecureChannel] ${label}: socket is not OPEN \u2014 not claiming KeyPackages; requests stay pending for the next pull`);
73502
+ return null;
73503
+ }
73504
+ if (this._commitInFlight.has(mlsGroupId)) {
73505
+ console.warn(`[SecureChannel] ${label}: another commit for this group is in flight \u2014 not claiming KeyPackages this pull`);
73506
+ return null;
73507
+ }
73508
+ const head = await this._readGroupHead(mlsGroupId);
73509
+ if (head.ok && head.epoch !== Number(mlsGroup.epoch)) {
73510
+ console.warn(
73511
+ `[SecureChannel] ${label}: server head is at epoch ${head.epoch}, we are at ${Number(mlsGroup.epoch)} \u2014 not claiming KeyPackages (the commit would be refused, #1184); requests stay pending`
73512
+ );
73513
+ await this._catchUpAfterPreflightSkip(mlsGroupId, head.epoch, Number(mlsGroup.epoch), label);
73514
+ return null;
73515
+ }
73516
+ return head;
73517
+ }
73365
73518
  /** SHA-256 hex of a commit's bytes — its identity on the server (#1175/#1177). */
73366
73519
  /**
73367
73520
  * #1225: send ONE commit over the socket and wait for the server's verdict,
@@ -74387,14 +74540,27 @@ ${baseText}`;
74387
74540
  const roomState = this._persisted?.rooms?.[roomId];
74388
74541
  if (!roomState?.mlsGroupId) continue;
74389
74542
  try {
74543
+ const roomLabel = `room ${roomId.slice(0, 8)}`;
74390
74544
  const pendingRes = await fetch(
74391
74545
  `${this.config.apiUrl}/api/v1/mls/groups/${roomState.mlsGroupId}/pending-welcomes`,
74392
74546
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74393
74547
  );
74394
- if (!pendingRes.ok) continue;
74548
+ if (!pendingRes.ok) {
74549
+ this._welcomeKpBackoff.noteFetchFailure(`pending-welcomes for ${roomLabel}`, pendingRes.status);
74550
+ continue;
74551
+ }
74552
+ this._welcomeKpBackoff.noteFetchOk(`pending-welcomes for ${roomLabel}`);
74395
74553
  let pending = await pendingRes.json();
74554
+ if (this._welcomeKpBackoff.observePending(roomState.mlsGroupId, pending)) {
74555
+ console.log(`[SecureChannel] ${pending.length} pending Welcome requests for room ${roomId.slice(0, 8)}`);
74556
+ }
74557
+ if (pending.length === 0) continue;
74558
+ const nowKp = Date.now();
74559
+ const roomMlsGroupId = roomState.mlsGroupId;
74560
+ pending = pending.filter(
74561
+ (r2) => this._welcomeKpBackoff.shouldAsk(roomMlsGroupId, r2.requesting_device_id, r2.key_package_available, roomLabel, nowKp)
74562
+ );
74396
74563
  if (pending.length === 0) continue;
74397
- console.log(`[SecureChannel] ${pending.length} pending Welcome requests for room ${roomId.slice(0, 8)}`);
74398
74564
  const nowMs = Date.now();
74399
74565
  const fresh = pending.filter((r2) => {
74400
74566
  const key = `${roomState.mlsGroupId}:${r2.requesting_device_id}`;
@@ -74408,6 +74574,8 @@ ${baseText}`;
74408
74574
  return true;
74409
74575
  });
74410
74576
  if (fresh.length === 0) continue;
74577
+ const roomPreHead = await this._welcomeCommitPreflight(roomState.mlsGroupId, mlsGroup, roomLabel);
74578
+ if (!roomPreHead) continue;
74411
74579
  for (const r2 of fresh) {
74412
74580
  this._roomFulfilCooldown.set(`${roomState.mlsGroupId}:${r2.requesting_device_id}`, nowMs);
74413
74581
  }
@@ -74417,13 +74585,23 @@ ${baseText}`;
74417
74585
  `${this.config.apiUrl}/api/v1/mls/key-packages/batch?device_ids=${deviceIds.join(",")}`,
74418
74586
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74419
74587
  );
74420
- if (!kpRes.ok) continue;
74588
+ if (!kpRes.ok) {
74589
+ this._welcomeKpBackoff.noteFetchFailure(`KeyPackage batch for ${roomLabel}`, kpRes.status);
74590
+ continue;
74591
+ }
74592
+ this._welcomeKpBackoff.noteFetchOk(`KeyPackage batch for ${roomLabel}`);
74421
74593
  const kpData = await kpRes.json();
74422
74594
  const roomTargets = [];
74423
74595
  for (const req of pending) {
74424
74596
  const kpHex = kpData[req.requesting_device_id];
74597
+ this._welcomeKpBackoff.recordKeyPackage(
74598
+ roomState.mlsGroupId,
74599
+ req.requesting_device_id,
74600
+ Boolean(kpHex),
74601
+ req.key_package_available,
74602
+ roomLabel
74603
+ );
74425
74604
  if (!kpHex) {
74426
- console.warn(`[SecureChannel] No KeyPackage for ${req.requesting_device_id.slice(0, 8)}, skipping`);
74427
74605
  continue;
74428
74606
  }
74429
74607
  const kpBytes = new Uint8Array(Buffer.from(kpHex, "hex"));
@@ -74441,8 +74619,10 @@ ${baseText}`;
74441
74619
  label: `room ${roomId.slice(0, 8)}`,
74442
74620
  welcomeFields: { room_id: roomId },
74443
74621
  targets: roomTargets,
74444
- memberDeviceIds: (roomState.members ?? []).map((m2) => m2.deviceId)
74622
+ memberDeviceIds: (roomState.members ?? []).map((m2) => m2.deviceId),
74445
74623
  // #1226
74624
+ preHead: roomPreHead
74625
+ // #1429
74446
74626
  });
74447
74627
  if (done > 0) {
74448
74628
  console.log(
@@ -74460,28 +74640,49 @@ ${baseText}`;
74460
74640
  const mlsGroup = this._mlsGroups.get(`1to1-group:${gid}`);
74461
74641
  if (!mlsGroup?.isInitialized) continue;
74462
74642
  try {
74643
+ const sharedLabel = `shared group ${gid.slice(0, 8)}`;
74463
74644
  const pendingRes = await fetch(
74464
74645
  `${this.config.apiUrl}/api/v1/mls/groups/${entry.mlsGroupId}/pending-welcomes`,
74465
74646
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74466
74647
  );
74467
- if (!pendingRes.ok) continue;
74468
- const pending = await pendingRes.json();
74648
+ if (!pendingRes.ok) {
74649
+ this._welcomeKpBackoff.noteFetchFailure(`pending-welcomes for ${sharedLabel}`, pendingRes.status);
74650
+ continue;
74651
+ }
74652
+ this._welcomeKpBackoff.noteFetchOk(`pending-welcomes for ${sharedLabel}`);
74653
+ const allPending = await pendingRes.json();
74654
+ if (this._welcomeKpBackoff.observePending(entry.mlsGroupId, allPending)) {
74655
+ console.log(`[SecureChannel] ${allPending.length} pending Welcome requests for shared group ${gid.slice(0, 8)}`);
74656
+ }
74657
+ const nowKp = Date.now();
74658
+ const pending = allPending.filter(
74659
+ (r2) => this._welcomeKpBackoff.shouldAsk(entry.mlsGroupId, r2.requesting_device_id, r2.key_package_available, sharedLabel, nowKp)
74660
+ );
74469
74661
  if (pending.length === 0) continue;
74470
- console.log(`[SecureChannel] ${pending.length} pending Welcome requests for shared group ${gid.slice(0, 8)}`);
74662
+ const sharedPreHead = await this._welcomeCommitPreflight(entry.mlsGroupId, mlsGroup, sharedLabel);
74663
+ if (!sharedPreHead) continue;
74471
74664
  const deviceIds = pending.map((r2) => r2.requesting_device_id);
74472
74665
  const kpRes = await fetch(
74473
74666
  `${this.config.apiUrl}/api/v1/mls/key-packages/batch?device_ids=${deviceIds.join(",")}`,
74474
74667
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74475
74668
  );
74476
- if (!kpRes.ok) continue;
74669
+ if (!kpRes.ok) {
74670
+ this._welcomeKpBackoff.noteFetchFailure(`KeyPackage batch for ${sharedLabel}`, kpRes.status);
74671
+ continue;
74672
+ }
74673
+ this._welcomeKpBackoff.noteFetchOk(`KeyPackage batch for ${sharedLabel}`);
74477
74674
  const kpData = await kpRes.json();
74478
74675
  const validReqs = [];
74479
74676
  for (const req of pending) {
74480
74677
  const kpHex = kpData[req.requesting_device_id];
74481
- if (!kpHex) {
74482
- console.warn(`[SecureChannel] No KeyPackage for ${req.requesting_device_id.slice(0, 8)}, skipping`);
74483
- continue;
74484
- }
74678
+ this._welcomeKpBackoff.recordKeyPackage(
74679
+ entry.mlsGroupId,
74680
+ req.requesting_device_id,
74681
+ Boolean(kpHex),
74682
+ req.key_package_available,
74683
+ sharedLabel
74684
+ );
74685
+ if (!kpHex) continue;
74485
74686
  const kpBytes = new Uint8Array(Buffer.from(kpHex, "hex"));
74486
74687
  validReqs.push({ req, kp: MLSGroupManager.deserializeKeyPackage(kpBytes) });
74487
74688
  }
@@ -74498,8 +74699,10 @@ ${baseText}`;
74498
74699
  label: `shared group ${gid.slice(0, 8)}`,
74499
74700
  welcomeFields: { conversation_group_id: gid },
74500
74701
  targets: sharedTargets,
74501
- memberDeviceIds: entry.memberDeviceIds ?? []
74702
+ memberDeviceIds: entry.memberDeviceIds ?? [],
74502
74703
  // #1226
74704
+ preHead: sharedPreHead
74705
+ // #1429
74503
74706
  });
74504
74707
  if (done > 0) {
74505
74708
  console.log(`[SecureChannel] Batched Welcome for ${done}/${validReqs.length} devices in shared group ${gid.slice(0, 8)} (epoch=${mlsGroup.epoch})`);
@@ -74533,23 +74736,49 @@ ${baseText}`;
74533
74736
  const mlsGroup = this._mlsGroups.get(`conv:${convId}`);
74534
74737
  if (!mlsGroup?.isInitialized) continue;
74535
74738
  try {
74739
+ const convLabel = `conv ${convId.slice(0, 8)}`;
74536
74740
  const pendingRes = await fetch(
74537
74741
  `${this.config.apiUrl}/api/v1/mls/groups/${convEntry.mlsGroupId}/pending-welcomes`,
74538
74742
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74539
74743
  );
74540
- if (!pendingRes.ok) continue;
74744
+ if (!pendingRes.ok) {
74745
+ this._welcomeKpBackoff.noteFetchFailure(`pending-welcomes for ${convLabel}`, pendingRes.status);
74746
+ continue;
74747
+ }
74748
+ this._welcomeKpBackoff.noteFetchOk(`pending-welcomes for ${convLabel}`);
74541
74749
  const pending = await pendingRes.json();
74750
+ if (this._welcomeKpBackoff.observePending(convEntry.mlsGroupId, pending)) {
74751
+ console.log(`[SecureChannel] ${pending.length} pending Welcome requests for conv ${convId.slice(0, 8)}`);
74752
+ }
74542
74753
  if (pending.length === 0) continue;
74543
- console.log(`[SecureChannel] ${pending.length} pending Welcome requests for conv ${convId.slice(0, 8)}`);
74544
74754
  for (const req of pending) {
74545
74755
  try {
74756
+ if (!this._welcomeKpBackoff.shouldAsk(
74757
+ convEntry.mlsGroupId,
74758
+ req.requesting_device_id,
74759
+ req.key_package_available,
74760
+ convLabel
74761
+ )) continue;
74762
+ const convPreHead = await this._welcomeCommitPreflight(convEntry.mlsGroupId, mlsGroup, convLabel);
74763
+ if (!convPreHead) break;
74546
74764
  const kpRes = await fetch(
74547
74765
  `${this.config.apiUrl}/api/v1/mls/key-packages/batch?device_ids=${req.requesting_device_id}`,
74548
74766
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74549
74767
  );
74550
- if (!kpRes.ok) continue;
74768
+ if (!kpRes.ok) {
74769
+ this._welcomeKpBackoff.noteFetchFailure(`KeyPackage batch for ${convLabel}`, kpRes.status);
74770
+ continue;
74771
+ }
74772
+ this._welcomeKpBackoff.noteFetchOk(`KeyPackage batch for ${convLabel}`);
74551
74773
  const kpData = await kpRes.json();
74552
74774
  const kpHex = kpData[req.requesting_device_id];
74775
+ this._welcomeKpBackoff.recordKeyPackage(
74776
+ convEntry.mlsGroupId,
74777
+ req.requesting_device_id,
74778
+ Boolean(kpHex),
74779
+ req.key_package_available,
74780
+ convLabel
74781
+ );
74553
74782
  if (!kpHex) continue;
74554
74783
  const kpBytes = new Uint8Array(Buffer.from(kpHex, "hex"));
74555
74784
  const memberKp = MLSGroupManager.deserializeKeyPackage(kpBytes);
@@ -74566,7 +74795,9 @@ ${baseText}`;
74566
74795
  // #1226: a 1:1 roster is [self, owner] — the web's shape too.
74567
74796
  memberDeviceIds: [this._persisted?.sessions?.[convId]?.ownerDeviceId].filter(
74568
74797
  (d2) => typeof d2 === "string"
74569
- )
74798
+ ),
74799
+ preHead: convPreHead
74800
+ // #1429
74570
74801
  });
74571
74802
  if (done > 0) {
74572
74803
  console.log(`[SecureChannel] Fulfilled Welcome for ${req.requesting_device_id.slice(0, 8)} in conv ${convId.slice(0, 8)}`);
@@ -74614,15 +74845,28 @@ ${baseText}`;
74614
74845
  if (!mlsGroup?.isInitialized) continue;
74615
74846
  if (chState.role !== "creator") continue;
74616
74847
  try {
74848
+ const a2aPendingLabel = `A2A ${chId.slice(0, 8)}`;
74617
74849
  const pendingRes = await fetch(
74618
74850
  `${this.config.apiUrl}/api/v1/mls/groups/${chState.mlsGroupId}/pending-welcomes`,
74619
74851
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74620
74852
  );
74621
- if (!pendingRes.ok) continue;
74853
+ if (!pendingRes.ok) {
74854
+ this._welcomeKpBackoff.noteFetchFailure(`pending-welcomes for ${a2aPendingLabel}`, pendingRes.status);
74855
+ continue;
74856
+ }
74857
+ this._welcomeKpBackoff.noteFetchOk(`pending-welcomes for ${a2aPendingLabel}`);
74622
74858
  const pending = await pendingRes.json();
74859
+ if (this._welcomeKpBackoff.observePending(chState.mlsGroupId, pending)) {
74860
+ console.log(`[SecureChannel] ${pending.length} pending A2A Welcome requests for channel ${chId.slice(0, 8)}`);
74861
+ }
74623
74862
  if (pending.length === 0) continue;
74624
- console.log(`[SecureChannel] ${pending.length} pending A2A Welcome requests for channel ${chId.slice(0, 8)}`);
74625
74863
  for (const req of pending) {
74864
+ if (!this._welcomeKpBackoff.shouldAsk(
74865
+ chState.mlsGroupId,
74866
+ req.requesting_device_id,
74867
+ req.key_package_available,
74868
+ a2aPendingLabel
74869
+ )) continue;
74626
74870
  try {
74627
74871
  const next = await this._fulfilA2AWelcome(chId, chState, mlsGroup, req);
74628
74872
  if (next === "stop") break;
@@ -74934,9 +75178,20 @@ ${baseText}`;
74934
75178
  `${this.config.apiUrl}/api/v1/mls/key-packages/batch?device_ids=${req.requesting_device_id}`,
74935
75179
  { headers: { Authorization: `Bearer ${this._deviceJwt}` } }
74936
75180
  );
74937
- if (!kpRes.ok) return "next";
75181
+ if (!kpRes.ok) {
75182
+ this._welcomeKpBackoff.noteFetchFailure(`KeyPackage batch for ${a2aLabel}`, kpRes.status);
75183
+ return "next";
75184
+ }
75185
+ this._welcomeKpBackoff.noteFetchOk(`KeyPackage batch for ${a2aLabel}`);
74938
75186
  const kpData = await kpRes.json();
74939
75187
  const kpHex = kpData[req.requesting_device_id];
75188
+ this._welcomeKpBackoff.recordKeyPackage(
75189
+ chState.mlsGroupId,
75190
+ req.requesting_device_id,
75191
+ Boolean(kpHex),
75192
+ req.key_package_available,
75193
+ a2aLabel
75194
+ );
74940
75195
  if (!kpHex) return "next";
74941
75196
  const kpBytes = new Uint8Array(Buffer.from(kpHex, "hex"));
74942
75197
  const memberKp = MLSGroupManager.deserializeKeyPackage(kpBytes);
@@ -103197,7 +103452,7 @@ var init_index = __esm({
103197
103452
  await init_skill_telemetry();
103198
103453
  await init_policy_enforcer();
103199
103454
  init_room_protocol();
103200
- VERSION = true ? "0.23.52" : "0.0.0-dev";
103455
+ VERSION = true ? "0.23.53" : "0.0.0-dev";
103201
103456
  }
103202
103457
  });
103203
103458
  await init_index();