@agentunion/fastaun 0.5.13 → 0.5.14

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.
@@ -25,6 +25,7 @@ export declare class MessageDeliveryEngine {
25
25
  constructor(runtime: ClientRuntime);
26
26
  private isPullOperationCurrent;
27
27
  private ensurePullOperationCurrent;
28
+ private isPullScopedSource;
28
29
  private forwardCoordinator;
29
30
  private confirmPlainForwardAck;
30
31
  commitPlainForwardPage(ns: string, afterSeq: number, method: 'message.pull' | 'group.pull', result: unknown, groupId?: string): Promise<{
@@ -91,20 +91,19 @@ export class MessageDeliveryEngine {
91
91
  constructor(runtime) {
92
92
  this.runtime = runtime;
93
93
  }
94
- isPullOperationCurrent() {
95
- const client = this.runtime.client;
96
- const generation = client._pullOperationGeneration;
97
- if (generation === undefined)
98
- return true;
99
- const pipeline = client._rpcPipeline;
100
- return typeof pipeline?.isPullGenerationCurrent === 'function'
101
- ? pipeline.isPullGenerationCurrent(generation)
94
+ isPullOperationCurrent(namespace = '') {
95
+ const pipeline = this.runtime.client._rpcPipeline;
96
+ return typeof pipeline?.isPullNamespaceCurrent === 'function'
97
+ ? pipeline.isPullNamespaceCurrent(namespace)
102
98
  : true;
103
99
  }
104
- ensurePullOperationCurrent() {
105
- if (!this.isPullOperationCurrent())
100
+ ensurePullOperationCurrent(namespace = '') {
101
+ if (!this.isPullOperationCurrent(namespace))
106
102
  throw new Error('pull invalidated');
107
103
  }
104
+ isPullScopedSource(source) {
105
+ return source === 'pull';
106
+ }
108
107
  forwardCoordinator() {
109
108
  const coordinator = this.runtime.client._v2E2EE;
110
109
  if (!coordinator
@@ -120,7 +119,7 @@ export class MessageDeliveryEngine {
120
119
  }
121
120
  async confirmPlainForwardAck(ns, method, ackSeq, groupId = '') {
122
121
  const client = this.runtime.client;
123
- this.ensurePullOperationCurrent();
122
+ this.ensurePullOperationCurrent(ns);
124
123
  const coordinator = this.forwardCoordinator();
125
124
  const generation = this.captureInlineAckGeneration();
126
125
  coordinator.recordForwardAck(ns, ackSeq);
@@ -139,7 +138,7 @@ export class MessageDeliveryEngine {
139
138
  _rpc_background: true,
140
139
  };
141
140
  const result = await client._rpcPipeline.rawCall(method, params, { background: true });
142
- this.ensurePullOperationCurrent();
141
+ this.ensurePullOperationCurrent(ns);
143
142
  const actualAckSeq = this.resolveForwardAckSeq(result, ackSeq);
144
143
  if (actualAckSeq < ackSeq) {
145
144
  throw new Error(`${method} server ACK watermark ${actualAckSeq} is below requested ${ackSeq}`);
@@ -157,7 +156,7 @@ export class MessageDeliveryEngine {
157
156
  throw new Error(`${method} response must be an object`);
158
157
  }
159
158
  const client = this.runtime.client;
160
- this.ensurePullOperationCurrent();
159
+ this.ensurePullOperationCurrent(ns);
161
160
  this.syncStarted(ns);
162
161
  const response = result;
163
162
  const messages = deduplicatePlainForwardPageMessages(response.messages, afterSeq);
@@ -189,20 +188,20 @@ export class MessageDeliveryEngine {
189
188
  let committed = false;
190
189
  try {
191
190
  for (const rawMessage of messages) {
192
- this.ensurePullOperationCurrent();
191
+ this.ensurePullOperationCurrent(ns);
193
192
  const seq = positiveSafeSequenceHint(rawMessage.seq);
194
193
  if (method === 'message.pull') {
195
194
  const appEvent = this.p2pAppEventForMessage(rawMessage);
196
195
  if (await this.publishPulledMessage(appEvent.event, ns, seq, appEvent.payload, false)) {
197
196
  publishedCount += 1;
198
197
  }
199
- this.ensurePullOperationCurrent();
198
+ this.ensurePullOperationCurrent(ns);
200
199
  continue;
201
200
  }
202
201
  const message = normalizeGroupMentionMode(rawMessage);
203
202
  if (this.recallEventFromGroupMessage(message)) {
204
203
  if (await this.publishGroupRecallTombstone(groupId, seq, message, 'pull')) {
205
- this.ensurePullOperationCurrent();
204
+ this.ensurePullOperationCurrent(ns);
206
205
  this.markPublishedSeq(ns, seq);
207
206
  publishedCount += 1;
208
207
  }
@@ -211,11 +210,11 @@ export class MessageDeliveryEngine {
211
210
  this.markPublishedSeq(ns, seq);
212
211
  }
213
212
  else if (await this.publishPulledMessage('group.message_created', ns, seq, message, false)) {
214
- this.ensurePullOperationCurrent();
213
+ this.ensurePullOperationCurrent(ns);
215
214
  publishedCount += 1;
216
215
  }
217
216
  }
218
- this.ensurePullOperationCurrent();
217
+ this.ensurePullOperationCurrent(ns);
219
218
  client._seqTracker.onPullResult(ns, messages, afterSeq, Number.isSafeInteger(result.contiguous_seq)
220
219
  ? result.contiguous_seq
221
220
  : undefined);
@@ -224,21 +223,21 @@ export class MessageDeliveryEngine {
224
223
  client._seqTracker.forceContiguousSeq(ns, commitTarget);
225
224
  }
226
225
  if (client._seqTracker.getContiguousSeq(ns) !== pageContigBefore) {
227
- this.ensurePullOperationCurrent();
226
+ this.ensurePullOperationCurrent(ns);
228
227
  await this.drainOrderedMessages(ns, undefined, false, false, 'pull');
229
- this.ensurePullOperationCurrent();
228
+ this.ensurePullOperationCurrent(ns);
230
229
  await client._commitSeqTrackerState(ns);
231
230
  }
232
231
  committed = true;
233
232
  }
234
233
  catch (exc) {
235
- if (this.isPullOperationCurrent() && !committed && typeof client._seqTracker.restoreNamespaceSnapshot === 'function') {
234
+ if (this.isPullOperationCurrent(ns) && !committed && typeof client._seqTracker.restoreNamespaceSnapshot === 'function') {
236
235
  client._seqTracker.restoreNamespaceSnapshot(ns, pageTrackerSnapshot);
237
236
  this.dropSeqTrackerPending(ns);
238
237
  }
239
238
  throw exc;
240
239
  }
241
- this.ensurePullOperationCurrent();
240
+ this.ensurePullOperationCurrent(ns);
242
241
  const committedAck = client._seqTracker.getContiguousSeq(ns);
243
242
  if (deferredServerCursor > 0 && committedAck >= deferredServerCursor) {
244
243
  coordinator.clearForwardCursor(ns, committedAck);
@@ -269,7 +268,7 @@ export class MessageDeliveryEngine {
269
268
  if (clampedAckSeq < pendingAckSeq) {
270
269
  throw new Error(`${ackMethod} cannot confirm pending Forward watermark ${pendingAckSeq}`);
271
270
  }
272
- this.ensurePullOperationCurrent();
271
+ this.ensurePullOperationCurrent(ns);
273
272
  await this.confirmPlainForwardAck(ns, ackMethod, pendingAckSeq, groupId);
274
273
  }
275
274
  this.syncProgress(ns, {
@@ -654,15 +653,19 @@ export class MessageDeliveryEngine {
654
653
  }
655
654
  async publishOrderedQueueItem(ns, event, seq, payload, source, pullResponse = false, deliveryContext) {
656
655
  const client = this.runtime.client;
657
- this.ensurePullOperationCurrent();
656
+ const pullScoped = this.isPullScopedSource(source);
657
+ if (pullScoped)
658
+ this.ensurePullOperationCurrent(ns);
658
659
  if (event === 'group.changed' && this.isGroupEventNamespace(ns)) {
659
660
  await this.publishOrderedGroupChanged(payload, source);
660
- this.ensurePullOperationCurrent();
661
+ if (pullScoped)
662
+ this.ensurePullOperationCurrent(ns);
661
663
  return;
662
664
  }
663
665
  if (event === 'message.recalled') {
664
666
  await this.publishMessageRecallTombstone(seq, payload, source);
665
- this.ensurePullOperationCurrent();
667
+ if (pullScoped)
668
+ this.ensurePullOperationCurrent(ns);
666
669
  return;
667
670
  }
668
671
  if (pullResponse) {
@@ -675,7 +678,8 @@ export class MessageDeliveryEngine {
675
678
  if (isPromiseLike(published))
676
679
  await published;
677
680
  }
678
- this.ensurePullOperationCurrent();
681
+ if (pullScoped)
682
+ this.ensurePullOperationCurrent(ns);
679
683
  }
680
684
  async publishOrderedGroupChanged(payload, source = 'ordered') {
681
685
  const client = this.runtime.client;
@@ -3489,7 +3493,9 @@ export class MessageDeliveryEngine {
3489
3493
  }
3490
3494
  async drainOrderedMessages(ns, beforeSeq, pullResponse = false, persist = true, source = 'push', context) {
3491
3495
  const client = this.runtime.client;
3492
- this.ensurePullOperationCurrent();
3496
+ const pullScoped = this.isPullScopedSource(source);
3497
+ if (pullScoped)
3498
+ this.ensurePullOperationCurrent(ns);
3493
3499
  const queue = client._pendingOrderedMsgs.get(ns);
3494
3500
  if (!queue || queue.size === 0)
3495
3501
  return;
@@ -3498,7 +3504,8 @@ export class MessageDeliveryEngine {
3498
3504
  const drainContexts = new Map();
3499
3505
  try {
3500
3506
  while (true) {
3501
- this.ensurePullOperationCurrent();
3507
+ if (pullScoped)
3508
+ this.ensurePullOperationCurrent(ns);
3502
3509
  const contig = client._seqTracker.getContiguousSeq(ns);
3503
3510
  const ready = [...queue.keys()]
3504
3511
  .filter((seq) => seq <= contig && (beforeSeq === undefined || seq < beforeSeq))
@@ -3536,7 +3543,8 @@ export class MessageDeliveryEngine {
3536
3543
  }
3537
3544
  }
3538
3545
  await this.publishOrderedQueueItem(ns, item.event, seq, item.payload, itemSource, pullResponse, itemContext);
3539
- this.ensurePullOperationCurrent();
3546
+ if (pullScoped)
3547
+ this.ensurePullOperationCurrent(ns);
3540
3548
  this.markPublishedSeq(ns, seq);
3541
3549
  client._markOrderedSeqDelivered(ns, seq);
3542
3550
  delivered = true;
@@ -3552,7 +3560,8 @@ export class MessageDeliveryEngine {
3552
3560
  if (queue.size === 0) {
3553
3561
  client._pendingOrderedMsgs.delete(ns);
3554
3562
  if (delivered && persist) {
3555
- this.ensurePullOperationCurrent();
3563
+ if (pullScoped)
3564
+ this.ensurePullOperationCurrent(ns);
3556
3565
  await this.saveSeqTrackerState();
3557
3566
  }
3558
3567
  }
@@ -3581,12 +3590,15 @@ export class MessageDeliveryEngine {
3581
3590
  }
3582
3591
  async publishOrderedMessageInternal(event, ns, seq, payload, source, context) {
3583
3592
  const client = this.runtime.client;
3584
- this.ensurePullOperationCurrent();
3593
+ const pullScoped = this.isPullScopedSource(source);
3594
+ if (pullScoped)
3595
+ this.ensurePullOperationCurrent(ns);
3585
3596
  const seqNum = Number(seq);
3586
3597
  if (!Number.isFinite(seqNum) || !Number.isInteger(seqNum) || seqNum <= 0) {
3587
3598
  client._clientLog.debug(`publish ordered direct(no-seq): event=${event}, ns=${ns || '<none>'}, seq=${String(seq)}`);
3588
3599
  await this.publishOrderedQueueItem(ns, event, seqNum, payload, source, false, context);
3589
- this.ensurePullOperationCurrent();
3600
+ if (pullScoped)
3601
+ this.ensurePullOperationCurrent(ns);
3590
3602
  return true;
3591
3603
  }
3592
3604
  if (client._pushedSeqs.get(ns)?.has(seqNum)) {
@@ -3604,7 +3616,8 @@ export class MessageDeliveryEngine {
3604
3616
  return false;
3605
3617
  }
3606
3618
  await this.drainOrderedMessages(ns, seqNum, false, true, source, context);
3607
- this.ensurePullOperationCurrent();
3619
+ if (pullScoped)
3620
+ this.ensurePullOperationCurrent(ns);
3608
3621
  if (client._pushedSeqs.get(ns)?.has(seqNum)) {
3609
3622
  client._clientLog.debug(`publish ordered skipped after-drain duplicate: event=${event}, ns=${ns}, seq=${seqNum}`);
3610
3623
  return false;
@@ -3614,32 +3627,38 @@ export class MessageDeliveryEngine {
3614
3627
  if (queue && queue.size === 0)
3615
3628
  client._pendingOrderedMsgs.delete(ns);
3616
3629
  await this.publishOrderedQueueItem(ns, event, seqNum, payload, source, false, context);
3617
- this.ensurePullOperationCurrent();
3630
+ if (pullScoped)
3631
+ this.ensurePullOperationCurrent(ns);
3618
3632
  this.markPublishedSeq(ns, seqNum);
3619
3633
  client._markOrderedSeqDelivered(ns, seqNum);
3620
3634
  client._clientLog.debug(`publish ordered delivered: event=${event}, ns=${ns}, seq=${seqNum}`);
3621
3635
  await this.drainOrderedMessages(ns, undefined, false, true, source, context);
3622
3636
  if (!client._pendingOrderedMsgs.get(ns)) {
3623
- this.ensurePullOperationCurrent();
3637
+ if (pullScoped)
3638
+ this.ensurePullOperationCurrent(ns);
3624
3639
  await this.saveSeqTrackerState();
3625
3640
  }
3626
3641
  return true;
3627
3642
  }
3628
3643
  async publishPulledMessage(event, ns, seq, payload, persist = true, source = 'pull') {
3629
3644
  const client = this.runtime.client;
3630
- this.ensurePullOperationCurrent();
3645
+ const pullScoped = this.isPullScopedSource(source);
3646
+ if (pullScoped)
3647
+ this.ensurePullOperationCurrent(ns);
3631
3648
  const seqNum = Number(seq);
3632
3649
  if (!Number.isFinite(seqNum) || !Number.isInteger(seqNum) || seqNum <= 0 || !ns) {
3633
3650
  client._clientLog.debug(`publish pulled direct(no-seq): event=${event}, ns=${ns || '<none>'}, seq=${String(seq)}`);
3634
3651
  if (event === 'message.recalled') {
3635
3652
  const published = await this.publishMessageRecallTombstone(seq, payload, source);
3636
- this.ensurePullOperationCurrent();
3653
+ if (pullScoped)
3654
+ this.ensurePullOperationCurrent(ns);
3637
3655
  return published;
3638
3656
  }
3639
3657
  const published = client._withPullResponseProcessing(ns, () => client._publishAppEvent(event, payload, source));
3640
3658
  if (isPromiseLike(published))
3641
3659
  await published;
3642
- this.ensurePullOperationCurrent();
3660
+ if (pullScoped)
3661
+ this.ensurePullOperationCurrent(ns);
3643
3662
  return true;
3644
3663
  }
3645
3664
  const queue = client._pendingOrderedMsgs.get(ns);
@@ -3651,27 +3670,32 @@ export class MessageDeliveryEngine {
3651
3670
  return false;
3652
3671
  }
3653
3672
  await this.drainOrderedMessages(ns, seqNum, true, persist, source);
3654
- this.ensurePullOperationCurrent();
3673
+ if (pullScoped)
3674
+ this.ensurePullOperationCurrent(ns);
3655
3675
  queue?.delete(seqNum);
3656
3676
  if (queue && queue.size === 0)
3657
3677
  client._pendingOrderedMsgs.delete(ns);
3658
3678
  if (event === 'message.recalled') {
3659
3679
  const recallPublished = await this.publishMessageRecallTombstone(seqNum, payload, source);
3660
- this.ensurePullOperationCurrent();
3680
+ if (pullScoped)
3681
+ this.ensurePullOperationCurrent(ns);
3661
3682
  this.markPublishedSeq(ns, seqNum);
3662
3683
  client._clientLog.debug(`publish pulled delivered: event=${event}, ns=${ns}, seq=${seqNum}`);
3663
3684
  await this.drainOrderedMessages(ns, undefined, true, persist, source);
3664
- this.ensurePullOperationCurrent();
3685
+ if (pullScoped)
3686
+ this.ensurePullOperationCurrent(ns);
3665
3687
  return recallPublished;
3666
3688
  }
3667
3689
  const published = client._withPullResponseProcessing(ns, () => client._publishAppEvent(event, payload, source));
3668
3690
  if (isPromiseLike(published))
3669
3691
  await published;
3670
- this.ensurePullOperationCurrent();
3692
+ if (pullScoped)
3693
+ this.ensurePullOperationCurrent(ns);
3671
3694
  this.markPublishedSeq(ns, seqNum);
3672
3695
  client._clientLog.debug(`publish pulled delivered: event=${event}, ns=${ns}, seq=${seqNum}`);
3673
3696
  await this.drainOrderedMessages(ns, undefined, true, persist, source);
3674
- this.ensurePullOperationCurrent();
3697
+ if (pullScoped)
3698
+ this.ensurePullOperationCurrent(ns);
3675
3699
  return true;
3676
3700
  }
3677
3701
  }