@open-e2ee/signal-protocol-sdk 9.2.0 → 9.4.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 (79) hide show
  1. package/CHANGELOG.md +111 -0
  2. package/README.md +2 -2
  3. package/SECURITY.md +2 -2
  4. package/client/README.md +11 -14
  5. package/dist/client/client.d.ts +79 -8
  6. package/dist/client/client.d.ts.map +1 -1
  7. package/dist/client/client.js +246 -50
  8. package/dist/client/client.js.map +1 -1
  9. package/dist/client/config.d.ts +4 -6
  10. package/dist/client/config.d.ts.map +1 -1
  11. package/dist/client/config.js.map +1 -1
  12. package/dist/client/event-hooks.d.ts +1 -1
  13. package/dist/client/group-outbox.d.ts +2 -4
  14. package/dist/client/group-outbox.d.ts.map +1 -1
  15. package/dist/client/group-outbox.js +7 -1
  16. package/dist/client/group-outbox.js.map +1 -1
  17. package/dist/client/hosted-anonymous-delivery.d.ts.map +1 -1
  18. package/dist/client/hosted-anonymous-delivery.js +2 -11
  19. package/dist/client/hosted-anonymous-delivery.js.map +1 -1
  20. package/dist/client/hosted-mailbox-subscription.d.ts.map +1 -1
  21. package/dist/client/hosted-mailbox-subscription.js +136 -15
  22. package/dist/client/hosted-mailbox-subscription.js.map +1 -1
  23. package/dist/client/hosted-presence-frames.js +1 -1
  24. package/dist/client/hosted-profile-keys.d.ts +8 -1
  25. package/dist/client/hosted-profile-keys.d.ts.map +1 -1
  26. package/dist/client/hosted-profile-keys.js +64 -3
  27. package/dist/client/hosted-profile-keys.js.map +1 -1
  28. package/dist/client/hosted-transport.d.ts +1 -1
  29. package/dist/client/hosted-transport.d.ts.map +1 -1
  30. package/dist/client/hosted-transport.js.map +1 -1
  31. package/dist/client/hosted.d.ts +9 -0
  32. package/dist/client/hosted.d.ts.map +1 -1
  33. package/dist/client/hosted.js +4 -1
  34. package/dist/client/hosted.js.map +1 -1
  35. package/dist/client/messages.d.ts.map +1 -1
  36. package/dist/client/messages.js +12 -2
  37. package/dist/client/messages.js.map +1 -1
  38. package/dist/client/relay-acceptance.d.ts +21 -0
  39. package/dist/client/relay-acceptance.d.ts.map +1 -0
  40. package/dist/client/relay-acceptance.js +22 -0
  41. package/dist/client/relay-acceptance.js.map +1 -0
  42. package/dist/client/relay-request-deadline.d.ts +9 -9
  43. package/dist/client/relay-request-deadline.d.ts.map +1 -1
  44. package/dist/client/relay-request-deadline.js +9 -9
  45. package/dist/client/relay-request-deadline.js.map +1 -1
  46. package/dist/client/relay-subscription.d.ts +31 -1
  47. package/dist/client/relay-subscription.d.ts.map +1 -1
  48. package/dist/client/relay-subscription.js +52 -20
  49. package/dist/client/relay-subscription.js.map +1 -1
  50. package/dist/client/relay-work.d.ts +7 -6
  51. package/dist/client/relay-work.d.ts.map +1 -1
  52. package/dist/client/relay-work.js +34 -9
  53. package/dist/client/relay-work.js.map +1 -1
  54. package/dist/client/retry.d.ts +3 -2
  55. package/dist/client/retry.d.ts.map +1 -1
  56. package/dist/client/retry.js +6 -4
  57. package/dist/client/retry.js.map +1 -1
  58. package/dist/client/signal-service-cipher.d.ts.map +1 -1
  59. package/dist/client/signal-service-cipher.js +2 -0
  60. package/dist/client/signal-service-cipher.js.map +1 -1
  61. package/dist/client/state.d.ts +5 -4
  62. package/dist/client/state.d.ts.map +1 -1
  63. package/dist/client/state.js +14 -12
  64. package/dist/client/state.js.map +1 -1
  65. package/dist/client/types.d.ts +20 -3
  66. package/dist/client/types.d.ts.map +1 -1
  67. package/dist/client/types.js.map +1 -1
  68. package/dist/local/store/reliability.d.ts +2 -0
  69. package/dist/local/store/reliability.d.ts.map +1 -1
  70. package/dist/local/store/reliability.js.map +1 -1
  71. package/dist/remote/relay/types.d.ts +11 -4
  72. package/dist/remote/relay/types.d.ts.map +1 -1
  73. package/dist/types/api.d.ts +35 -8
  74. package/dist/types/api.d.ts.map +1 -1
  75. package/docs/ASSURANCE.md +4 -4
  76. package/docs/DEVIATIONS.md +17 -16
  77. package/docs/DOCUMENTATION_STANDARDS.md +4 -4
  78. package/docs/GETTING_STARTED.md +6 -0
  79. package/package.json +1 -1
@@ -202,6 +202,22 @@ export class DefaultSignalProtocolClient {
202
202
  * @see SignalProtocolClientState for state details
203
203
  */
204
204
  state = new SignalProtocolClientState();
205
+ /**
206
+ * The delivery receipt sends in progress, from a batch timer, the batch
207
+ * bound, stopRelaySubscription(), or stop(): the relay work item of each
208
+ * send, and the promise that settles when the send finishes. While stop()
209
+ * holds the batches, only stop() starts a send. stop() waits until its
210
+ * receipt deadline for the sends that are in progress when it starts,
211
+ * except a send that an earlier stop() stopped, and for the sends that it
212
+ * starts. It does not stop them before then.
213
+ */
214
+ deliveryReceiptSends = new Map();
215
+ /**
216
+ * The stop() in progress. It is set before a step of stop() runs, so a
217
+ * stop() call that overlaps it, also from a step, waits for it, and a
218
+ * subscription start is refused.
219
+ */
220
+ stopping;
205
221
  /**
206
222
  * Get retry rate limiting state for retry operations
207
223
  */
@@ -596,9 +612,54 @@ export class DefaultSignalProtocolClient {
596
612
  forcePreKeyRotation: () => this.regeneratePreKeysWithFreshIds(),
597
613
  handleDeliveryReceipt: (envelope, receipt) => this.handleDeliveryReceipt(envelope, receipt, ctx),
598
614
  handleTypingIndicator: (envelope, typing) => this.handleTypingIndicator(envelope, typing, ctx),
599
- sendDeliveryReceipt: (userId, timestamps) => this.state.relayWork.run((work) => this.sendDeliveryReceipt(userId, timestamps, this.relayWorkContext(work))),
615
+ sendDeliveryReceipt: (userId, timestamps) => this.sendDeliveryReceiptAsRelayWork(userId, timestamps),
600
616
  };
601
617
  }
618
+ /**
619
+ * Send one delivery receipt batch as relay work of its own. The context
620
+ * carries the stop signal of the tracker, which also aborts at a stop
621
+ * after the work ends, so a retry timer of the send sees that stop. The
622
+ * send is in deliveryReceiptSends while it runs.
623
+ */
624
+ sendDeliveryReceiptAsRelayWork(userId, timestamps) {
625
+ return this.state.relayWork.run(async (work) => {
626
+ const send = this.sendDeliveryReceipt(userId, timestamps, {
627
+ ...this.relayWorkContext(work),
628
+ stopSignal: this.state.relayWork.stopSignal,
629
+ });
630
+ this.deliveryReceiptSends.set(work, send);
631
+ try {
632
+ await send;
633
+ }
634
+ finally {
635
+ this.deliveryReceiptSends.delete(work);
636
+ }
637
+ });
638
+ }
639
+ /**
640
+ * Start the send of every batched delivery receipt, once each. The batches
641
+ * leave the accumulator, so no batch timer sends them again. The returned
642
+ * promises settle when the sends finish. They never reject.
643
+ */
644
+ flushPendingDeliveryReceipts() {
645
+ return RelaySubscriptionOps.flushPendingReceipts(this.relaySubscriptionState.receiptAccumulator, (userId, timestamps) => this.sendDeliveryReceiptAsRelayWork(userId, timestamps), this.logger);
646
+ }
647
+ /**
648
+ * Wait for the receipt sends that stop() waits for, with their app hooks,
649
+ * for no more than the given time.
650
+ */
651
+ async waitForStopReceiptSends(sends, ms) {
652
+ let timer;
653
+ const bound = new Promise((resolve) => {
654
+ timer = setTimeout(resolve, Math.max(0, ms));
655
+ });
656
+ try {
657
+ await Promise.race([Promise.allSettled(sends), bound]);
658
+ }
659
+ finally {
660
+ clearTimeout(timer);
661
+ }
662
+ }
602
663
  /** Client context whose hooks call the app through the given relay work. */
603
664
  relayWorkContext(work) {
604
665
  return { ...this.ctx, hooks: work.wrapHooks(this.hooks) };
@@ -1599,6 +1660,8 @@ export class DefaultSignalProtocolClient {
1599
1660
  *
1600
1661
  * Callers can run this manually after registering hooks via registerHook().
1601
1662
  * Called automatically by create() when relay + hook configured.
1663
+ * A call while stop() runs is refused and logged; start again after stop()
1664
+ * resolves.
1602
1665
  *
1603
1666
  * @see SignalProtocolClient.startRelaySubscription
1604
1667
  */
@@ -1615,6 +1678,13 @@ export class DefaultSignalProtocolClient {
1615
1678
  });
1616
1679
  return;
1617
1680
  }
1681
+ // A start while stop() runs would outlive stop()
1682
+ if (this.stopping) {
1683
+ this.logger.debug('Relay subscription not started: stop() in progress', {
1684
+ category: 'E2EE',
1685
+ });
1686
+ return;
1687
+ }
1618
1688
  // Avoid duplicate subscriptions
1619
1689
  if (this.relayUnsubscribe) {
1620
1690
  this.logger.debug('Relay subscription already active', {
@@ -1627,18 +1697,23 @@ export class DefaultSignalProtocolClient {
1627
1697
  data: { userId: this.userId, deviceId: this.deviceId },
1628
1698
  });
1629
1699
  this.relayUnsubscribe = this.relay.subscribe(this.userId, this.deviceId, (envelope) => {
1630
- this.state.relayWork
1631
- .run((work) => {
1700
+ const handled = this.state.relayWork.run((work) => {
1632
1701
  const ctx = this.relayWorkContext(work);
1633
1702
  return RelaySubscriptionOps.handleRelayMessage({ ...ctx, cipher: this.cipher, stopSignal: work.stopSignal }, envelope, this.relaySubscriptionState, this.relaySubscriptionCallbacks(ctx), this.relaySubscriptionConfig);
1634
- })
1635
- .catch((error) => {
1636
- // The relay does not wait for the handler. It delivers the envelope again.
1703
+ });
1704
+ handled.catch((error) => {
1705
+ // The relay delivers an envelope that it did not acknowledge again.
1637
1706
  this.logger.error('Failed to handle relay envelope', {
1638
1707
  category: 'E2EE',
1639
1708
  data: { envelopeId: envelope.id, error: error.message },
1640
1709
  });
1641
1710
  });
1711
+ // A relay that waits for the work, as the hosted transport does, ends
1712
+ // its batch and accepts an ephemeral message only after the work
1713
+ // settles, and pulls again after the work fails. A relay that does not
1714
+ // wait ignores the promise, and the handler above already holds its
1715
+ // failure.
1716
+ return handled;
1642
1717
  }, {
1643
1718
  // Batching callbacks for notification coalescing
1644
1719
  onBatchStart: () => this.contentAdapter.setRelayBatching(true),
@@ -1657,19 +1732,11 @@ export class DefaultSignalProtocolClient {
1657
1732
  * @see SignalProtocolClient.stopRelaySubscription
1658
1733
  */
1659
1734
  stopRelaySubscription() {
1660
- // Flush any pending batched delivery receipts before unsubscribing
1661
- const { receiptAccumulator } = this.relaySubscriptionState;
1662
- for (const [userId, timestamps] of receiptAccumulator.pending) {
1663
- if (timestamps.length > 0) {
1664
- this.state.relayWork
1665
- .run((work) => this.sendDeliveryReceipt(userId, timestamps, this.relayWorkContext(work)))
1666
- .catch((_error) => {
1667
- this.logger.warn('Failed to send delivery receipt', {
1668
- category: 'E2EE',
1669
- data: { userId },
1670
- });
1671
- });
1672
- }
1735
+ // Send the batched delivery receipts before unsubscribing. During the
1736
+ // hold, stop() sends the batches that wait at each of its flushes and
1737
+ // drops a batch made after its last flush.
1738
+ if (!this.relaySubscriptionState.receiptAccumulator.held) {
1739
+ this.flushPendingDeliveryReceipts();
1673
1740
  }
1674
1741
  if (this.relayUnsubscribe) {
1675
1742
  this.relayUnsubscribe();
@@ -1689,6 +1756,8 @@ export class DefaultSignalProtocolClient {
1689
1756
  *
1690
1757
  * Automatically started if relay.subscribeRetryRequests is available.
1691
1758
  * Call this manually if you need to restart the subscription.
1759
+ * A call while stop() runs is refused and logged; start again after stop()
1760
+ * resolves.
1692
1761
  */
1693
1762
  startRetryRequestSubscription() {
1694
1763
  if (!this.relay || !this.relay.subscribeRetryRequests) {
@@ -1697,6 +1766,13 @@ export class DefaultSignalProtocolClient {
1697
1766
  });
1698
1767
  return;
1699
1768
  }
1769
+ // A start while stop() runs would outlive stop()
1770
+ if (this.stopping) {
1771
+ this.logger.debug('Retry request subscription not started: stop() in progress', {
1772
+ category: 'E2EE',
1773
+ });
1774
+ return;
1775
+ }
1700
1776
  // Avoid duplicate subscriptions
1701
1777
  if (this.retryUnsubscribe) {
1702
1778
  this.logger.debug('Retry request subscription already active', {
@@ -1752,7 +1828,7 @@ export class DefaultSignalProtocolClient {
1752
1828
  * @param ctx - Client context, whose hooks run through the relay work of the send
1753
1829
  *
1754
1830
  */
1755
- async sendDeliveryReceipt(recipientUserId, timestamps, ctx = this.ctx) {
1831
+ async sendDeliveryReceipt(recipientUserId, timestamps, ctx = this.receiptContext()) {
1756
1832
  return this.sendReceipt(recipientUserId, timestamps, ReceiptType.DELIVERY, ctx);
1757
1833
  }
1758
1834
  /**
@@ -1876,12 +1952,23 @@ export class DefaultSignalProtocolClient {
1876
1952
  async sendTypingIndicator(recipientUserId, recipientDeviceId, conversationId, action, groupId) {
1877
1953
  return MessageOps.sendTypingIndicator(this.ctx, recipientUserId, recipientDeviceId, conversationId, action, groupId);
1878
1954
  }
1955
+ /**
1956
+ * Client context of a receipt send outside relay work, such as a read
1957
+ * receipt that the app sends. It carries the stop signal of the tracker,
1958
+ * so the next stop() clears the retry timer of the send. stop() does not
1959
+ * wait for the send.
1960
+ */
1961
+ receiptContext() {
1962
+ return { ...this.ctx, stopSignal: this.state.relayWork.stopSignal };
1963
+ }
1879
1964
  /**
1880
1965
  * Internal method to send receipt (delivery or read)
1881
1966
  *
1882
- * Delegates to MessageOps.sendReceipt for implementation.
1967
+ * Delegates to MessageOps.sendReceipt for implementation. The context
1968
+ * carries the stop signal of the tracker, so the next stop() clears the
1969
+ * retry timer of the send.
1883
1970
  */
1884
- async sendReceipt(recipientUserId, timestamps, type, ctx = this.ctx) {
1971
+ async sendReceipt(recipientUserId, timestamps, type, ctx = this.receiptContext()) {
1885
1972
  return MessageOps.sendReceipt(ctx, recipientUserId, timestamps, type);
1886
1973
  }
1887
1974
  /**
@@ -1904,13 +1991,35 @@ export class DefaultSignalProtocolClient {
1904
1991
  * Stop the Signal Protocol client and clean up resources
1905
1992
  *
1906
1993
  * Call this when the user logs out or the app shuts down.
1907
- * Unsubscribes from relay server, waits for the SDK work of the
1908
- * deliveries, retry requests, and receipt sends in progress, and cleans up
1909
- * any pending operations.
1910
- *
1911
- * It does not wait for app hooks, so a hook can await stop(). When a hook
1912
- * returns after stop(), the SDK drops the work after the hook and writes
1913
- * nothing more. The relay delivers the envelope again after the next start.
1994
+ * It starts the send of each batched delivery receipt, once each, and
1995
+ * unsubscribes from the relay server. Then it waits for delivery receipt
1996
+ * sends, with their app hooks, until one receipt deadline 5 s after stop()
1997
+ * starts. It waits for each receipt send that is in progress when stop()
1998
+ * starts, except a send that an earlier stop() stopped, and for the sends
1999
+ * that it starts. A delivery that finishes during the wait batches its
2000
+ * receipt. At the end of each wait, stop() sends the batches that wait and
2001
+ * waits for those sends until the same deadline. It does this again while
2002
+ * the deadline is not past. stop() holds the receipt batches from its
2003
+ * first flush until it clears its tracking state. During the hold,
2004
+ * stopRelaySubscription() does not send them, and stop() drops a receipt
2005
+ * batched after its last flush. At the deadline it stops each receipt
2006
+ * send that is left: a send that waits in an app hook stops, and that
2007
+ * receipt is not sent. A send that is in its relay call at the deadline
2008
+ * is waited for, and that receipt can still be sent. It also
2009
+ * waits for the SDK work of the deliveries and retry requests in progress.
2010
+ * Then it cleans up any pending operations.
2011
+ *
2012
+ * Apart from the receipt wait, it does not wait for app hooks, so a hook
2013
+ * can await stop(). A hook of a delivery receipt send that awaits stop()
2014
+ * holds stop() until the receipt deadline. stop() does not wait for read
2015
+ * and viewed receipt sends. When a hook returns after stop(), the SDK drops
2016
+ * the work after the hook and writes nothing more. The relay delivers the
2017
+ * envelope again after the next start.
2018
+ *
2019
+ * A call to startRelaySubscription() or startRetryRequestSubscription()
2020
+ * while stop() runs is refused and logged; start again after stop()
2021
+ * resolves. A stop() call that overlaps a running stop() waits for it and
2022
+ * settles as it settles.
1914
2023
  *
1915
2024
  * @example
1916
2025
  * ```typescript
@@ -1919,28 +2028,115 @@ export class DefaultSignalProtocolClient {
1919
2028
  * ```
1920
2029
  */
1921
2030
  async stop() {
1922
- // 1. Stop relay subscription
1923
- if (this.relayUnsubscribe) {
1924
- this.relayUnsubscribe();
1925
- this.relayUnsubscribe = undefined;
1926
- this.logger.debug('Relay subscription stopped', { category: 'E2EE' });
2031
+ if (this.stopping) {
2032
+ // An overlapping call waits for the running stop() and settles as it
2033
+ // settles. No subscription can start during the run
2034
+ await this.stopping;
2035
+ return;
1927
2036
  }
1928
- // 2. Stop retry request subscription
1929
- if (this.retryUnsubscribe) {
1930
- this.retryUnsubscribe();
2037
+ this.stopping = this.stopOnce();
2038
+ try {
2039
+ await this.stopping;
2040
+ }
2041
+ finally {
2042
+ this.stopping = undefined;
2043
+ }
2044
+ }
2045
+ /** The steps of one stop(). A stop() call that overlaps it waits for it. */
2046
+ async stopOnce() {
2047
+ // stop() sets this.stopping before step 1 runs. So a stop() call from a
2048
+ // step, such as from a relay connection state listener in step 2, waits
2049
+ // for this run and does not start a second run. A subscription start
2050
+ // from a step is refused
2051
+ await Promise.resolve();
2052
+ // 1. Start the receipt deadline, 5 s from now. Take the receipt sends in
2053
+ // progress, except a send that an earlier stop() stopped, and start the
2054
+ // send of the batched delivery receipts, once each, as relay work. Until
2055
+ // the end of step 7, a delivery that finishes batches its receipt with no
2056
+ // timer, and stopRelaySubscription() does not send it. Step 6 sends the
2057
+ // batches that wait at the end of each wait, again while the deadline is
2058
+ // not past. A receipt batched after its last flush is dropped in step 7
2059
+ const receiptDeadline = Date.now() + RelaySubscriptionOps.STOP_RECEIPT_FLUSH_MS;
2060
+ const receiptSends = [
2061
+ ...[...this.deliveryReceiptSends]
2062
+ .filter(([work]) => !work.stopSignal.aborted)
2063
+ .map(([, send]) => send),
2064
+ ...this.flushPendingDeliveryReceipts(),
2065
+ ];
2066
+ const receiptAccumulator = this.relaySubscriptionState.receiptAccumulator;
2067
+ receiptAccumulator.held = true;
2068
+ try {
2069
+ // 2. Stop relay subscription. Clear it before the call, so a call that
2070
+ // the unsubscribe makes does not unsubscribe it again. If the call
2071
+ // throws, put it back, so the next stop() calls it again
2072
+ const relayUnsubscribe = this.relayUnsubscribe;
2073
+ this.relayUnsubscribe = undefined;
2074
+ if (relayUnsubscribe) {
2075
+ try {
2076
+ relayUnsubscribe();
2077
+ }
2078
+ catch (error) {
2079
+ this.relayUnsubscribe ??= relayUnsubscribe;
2080
+ throw error;
2081
+ }
2082
+ this.logger.debug('Relay subscription stopped', { category: 'E2EE' });
2083
+ }
2084
+ // 3. Stop retry request subscription. Clear it before the call, and put
2085
+ // it back if the call throws, as step 2 does
2086
+ const retryUnsubscribe = this.retryUnsubscribe;
1931
2087
  this.retryUnsubscribe = undefined;
1932
- this.logger.debug('Retry request subscription stopped', {
1933
- category: 'E2EE',
1934
- });
1935
- }
1936
- // 3. Stop the relay work in progress and wait for its SDK work. Work
1937
- // that waits in an app hook is not awaited. It writes nothing after
1938
- // the hook, and the relay delivers it again after the next start
1939
- await this.state.relayWork.settle();
1940
- // 4. Clear internal tracking state via state manager. This cancels the
1941
- // receipt timers, so no receipt starts after the wait
1942
- this.state.clearForStop();
1943
- // 5. Cleanup expired Sesame sessions
2088
+ if (retryUnsubscribe) {
2089
+ try {
2090
+ retryUnsubscribe();
2091
+ }
2092
+ catch (error) {
2093
+ this.retryUnsubscribe ??= retryUnsubscribe;
2094
+ throw error;
2095
+ }
2096
+ this.logger.debug('Retry request subscription stopped', {
2097
+ category: 'E2EE',
2098
+ });
2099
+ }
2100
+ // 4. Stop all relay work except the delivery receipt sends, then wait
2101
+ // for the receipt sends of step 1, with their app hooks, until the
2102
+ // receipt deadline. A delivery or retry request whose app hook returns
2103
+ // during the wait writes nothing more, and the relay delivers it again
2104
+ // after the next start. A receipt send is not stopped here, because the
2105
+ // relay has the acknowledgment of its messages and does not deliver
2106
+ // them again
2107
+ if (receiptSends.length > 0) {
2108
+ this.state.relayWork.stopAllExcept(new Set(this.deliveryReceiptSends.keys()));
2109
+ await this.waitForStopReceiptSends(receiptSends, receiptDeadline - Date.now());
2110
+ }
2111
+ // 5. Stop the relay work in progress and wait for its SDK work. Work
2112
+ // that waits in an app hook is not awaited. It writes nothing after
2113
+ // the hook, and the relay delivers it again after the next start. So a
2114
+ // receipt send that waits in an app hook after the deadline stops, and
2115
+ // that receipt is not sent. A receipt send that is still in its relay
2116
+ // call is waited for, and that receipt can still be sent
2117
+ await this.state.relayWork.settle();
2118
+ // 6. A delivery can finish during steps 4, 5, and 6, and the relay does
2119
+ // not deliver it again. Send the receipts that such deliveries batched,
2120
+ // wait for them until the same receipt deadline, and stop what is left
2121
+ // as step 5 does. Do this again while a batch waits and the deadline
2122
+ // is not past
2123
+ do {
2124
+ const lateReceiptSends = this.flushPendingDeliveryReceipts();
2125
+ if (lateReceiptSends.length === 0)
2126
+ break;
2127
+ await this.waitForStopReceiptSends(lateReceiptSends, receiptDeadline - Date.now());
2128
+ await this.state.relayWork.settle();
2129
+ } while (Date.now() < receiptDeadline);
2130
+ // 7. Clear internal tracking state via state manager. This drops the
2131
+ // receipts batched after the last flush of step 6, which is the last
2132
+ // flush before the deadline or the first flush after step 5. Then the
2133
+ // hold ends, and new batches set their timers again
2134
+ this.state.clearForStop();
2135
+ }
2136
+ finally {
2137
+ receiptAccumulator.held = false;
2138
+ }
2139
+ // 8. Cleanup expired Sesame sessions
1944
2140
  try {
1945
2141
  await this.sesameManager.cleanupExpiredSessions();
1946
2142
  }
@@ -1950,7 +2146,7 @@ export class DefaultSignalProtocolClient {
1950
2146
  data: { error: error.message },
1951
2147
  });
1952
2148
  }
1953
- // 6. Cleanup expired message records (SESAME spec §6.2)
2149
+ // 9. Cleanup expired message records (SESAME spec §6.2)
1954
2150
  try {
1955
2151
  const deleted = await this._storage.deleteExpiredMessageRecords(MESSAGE_RECORD_TTL_MS);
1956
2152
  if (deleted > 0) {