@dsh-ray/assistant-delivery 0.2.3 → 0.3.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/README.md +15 -2
  2. package/cordis.patch.yml +0 -2
  3. package/lib/agent-runtime.d.ts +15 -0
  4. package/lib/agent-runtime.d.ts.map +1 -1
  5. package/lib/agent-runtime.js +446 -51
  6. package/lib/agent-runtime.js.map +1 -1
  7. package/lib/attention-audit-store.d.ts +2 -1
  8. package/lib/attention-audit-store.d.ts.map +1 -1
  9. package/lib/attention-audit-store.js +3 -0
  10. package/lib/attention-audit-store.js.map +1 -1
  11. package/lib/coordinator.d.ts +21 -1
  12. package/lib/coordinator.d.ts.map +1 -1
  13. package/lib/coordinator.js +255 -68
  14. package/lib/coordinator.js.map +1 -1
  15. package/lib/feedback-command.d.ts +1 -0
  16. package/lib/feedback-command.d.ts.map +1 -1
  17. package/lib/feedback-command.js +1 -1
  18. package/lib/feedback-command.js.map +1 -1
  19. package/lib/index.d.ts +2 -1
  20. package/lib/index.d.ts.map +1 -1
  21. package/lib/index.js +1 -0
  22. package/lib/index.js.map +1 -1
  23. package/lib/interrupted-notices.d.ts +20 -5
  24. package/lib/interrupted-notices.d.ts.map +1 -1
  25. package/lib/interrupted-notices.js +238 -24
  26. package/lib/interrupted-notices.js.map +1 -1
  27. package/lib/learning-command.d.ts +2 -0
  28. package/lib/learning-command.d.ts.map +1 -1
  29. package/lib/learning-command.js +2 -0
  30. package/lib/learning-command.js.map +1 -1
  31. package/lib/native-web-owner.d.ts +12 -0
  32. package/lib/native-web-owner.d.ts.map +1 -1
  33. package/lib/native-web-owner.js +653 -80
  34. package/lib/native-web-owner.js.map +1 -1
  35. package/lib/operator-snapshot.d.ts +1 -1
  36. package/lib/operator-snapshot.d.ts.map +1 -1
  37. package/lib/operator-snapshot.js +6 -5
  38. package/lib/operator-snapshot.js.map +1 -1
  39. package/lib/ray-worker-runtime.d.ts +192 -0
  40. package/lib/ray-worker-runtime.d.ts.map +1 -0
  41. package/lib/ray-worker-runtime.js +1300 -0
  42. package/lib/ray-worker-runtime.js.map +1 -0
  43. package/lib/service.d.ts +286 -3
  44. package/lib/service.d.ts.map +1 -1
  45. package/lib/service.js +1608 -72
  46. package/lib/service.js.map +1 -1
  47. package/lib/session-commands.d.ts +6 -0
  48. package/lib/session-commands.d.ts.map +1 -1
  49. package/lib/session-commands.js +14 -0
  50. package/lib/session-commands.js.map +1 -1
  51. package/lib/session-lease-runtime.d.ts +31 -6
  52. package/lib/session-lease-runtime.d.ts.map +1 -1
  53. package/lib/session-lease-runtime.js +553 -43
  54. package/lib/session-lease-runtime.js.map +1 -1
  55. package/lib/sqlite.d.ts +1 -1
  56. package/lib/sqlite.d.ts.map +1 -1
  57. package/lib/sqlite.js +83 -3
  58. package/lib/sqlite.js.map +1 -1
  59. package/lib/store.d.ts +93 -2
  60. package/lib/store.d.ts.map +1 -1
  61. package/lib/store.js +695 -24
  62. package/lib/store.js.map +1 -1
  63. package/lib/tools.d.ts.map +1 -1
  64. package/lib/tools.js +319 -0
  65. package/lib/tools.js.map +1 -1
  66. package/lib/version-query.d.ts.map +1 -1
  67. package/lib/version-query.js +109 -20
  68. package/lib/version-query.js.map +1 -1
  69. package/lib/version.d.ts +1 -1
  70. package/lib/version.js +1 -1
  71. package/lib/work-item-feedback-store.d.ts +2 -1
  72. package/lib/work-item-feedback-store.d.ts.map +1 -1
  73. package/lib/work-item-feedback-store.js +1 -0
  74. package/lib/work-item-feedback-store.js.map +1 -1
  75. package/lib/work-item-replies-store.d.ts +2 -1
  76. package/lib/work-item-replies-store.d.ts.map +1 -1
  77. package/lib/work-item-replies-store.js +3 -0
  78. package/lib/work-item-replies-store.js.map +1 -1
  79. package/package.json +11 -11
@@ -1,4 +1,4 @@
1
- import { isClassifiedAgentTurnFailureCode } from './interrupted-notices.js';
1
+ import { isClassifiedAgentTurnFailureCode, isReloadInterruptionFailureCode, } from './interrupted-notices.js';
2
2
  import { isFeedbackDispatchRecoveryCode, isFeedbackRetryableFailureCode, isLearningDispatchRecoveryCode, isLearningRetryableFailureCode, isWorkflowDispatchRecoveryCode, permissionDispatchRecoveryFromFailureCode, } from './session-commands.js';
3
3
  import { DeliveryStoreError, } from './store.js';
4
4
  function adapterKey(channel, account) {
@@ -49,6 +49,17 @@ export class DeliveryAdapterRegistryStoppedError extends Error {
49
49
  this.name = 'DeliveryAdapterRegistryStoppedError';
50
50
  }
51
51
  }
52
+ export class ReloadInterruptedError extends Error {
53
+ code;
54
+ constructor(code) {
55
+ super(`assistant-delivery interrupted by reload (${code})`);
56
+ this.code = code;
57
+ this.name = 'ReloadInterruptedError';
58
+ }
59
+ }
60
+ export function isReloadInterruptionSignal(signal) {
61
+ return signal.aborted && signal.reason instanceof ReloadInterruptedError;
62
+ }
52
63
  async function boundedWait(operation, graceMs) {
53
64
  let timeout;
54
65
  try {
@@ -62,31 +73,61 @@ async function boundedWait(operation, graceMs) {
62
73
  clearTimeout(timeout);
63
74
  }
64
75
  }
65
- async function abortAndBoundedDrain(active, leaseMs, reason) {
66
- const snapshot = [...active.entries()];
67
- for (const [, entry] of snapshot)
76
+ async function abortAndBoundedDrain(active, leaseMs, reason, options) {
77
+ const initialSnapshot = [...active.entries()];
78
+ if (initialSnapshot.length === 0)
79
+ return [];
80
+ const graceMs = options?.reloadCode !== undefined && (options.shutdownGraceMs ?? 0) > 0
81
+ ? Math.min(options.shutdownGraceMs, Math.max(1, Math.floor(leaseMs / 2)))
82
+ : 0;
83
+ if (graceMs > 0) {
84
+ let graceTimer;
85
+ try {
86
+ await Promise.race([
87
+ Promise.all(initialSnapshot.map(([, entry]) => entry.promise)),
88
+ new Promise(resolve => {
89
+ graceTimer = setTimeout(resolve, graceMs);
90
+ graceTimer.unref?.();
91
+ }),
92
+ ]);
93
+ }
94
+ finally {
95
+ if (graceTimer !== undefined)
96
+ clearTimeout(graceTimer);
97
+ }
98
+ }
99
+ const remaining = [...active.entries()];
100
+ if (remaining.length === 0)
101
+ return [];
102
+ // Abort stops lease renewal immediately. Give cooperative adapters a bounded
103
+ // unwind window without allowing an uncooperative one to hold service
104
+ // disposal (and the SQLite handle) forever.
105
+ const rawCleanupGraceMs = Math.max(1, Math.floor(leaseMs / 3));
106
+ const cleanupGraceMs = options?.reloadCode !== undefined
107
+ ? Math.max(rawCleanupGraceMs, 2_500)
108
+ : rawCleanupGraceMs;
109
+ options?.beforeAbort?.(remaining, cleanupGraceMs);
110
+ for (const [, entry] of remaining)
68
111
  entry.abort.abort(reason);
69
- if (snapshot.length === 0)
70
- return;
71
- // Abort stops lease renewal immediately. One heartbeat interval gives a
72
- // cooperative adapter time to unwind without allowing an uncooperative one
73
- // to hold service disposal (and the SQLite handle) forever.
74
- const graceMs = Math.max(1, Math.floor(leaseMs / 3));
75
112
  let timeout;
76
113
  try {
77
114
  await Promise.race([
78
- Promise.all(snapshot.map(([, entry]) => entry.promise)),
79
- new Promise(resolve => { timeout = setTimeout(resolve, graceMs); }),
115
+ Promise.all(remaining.map(([, entry]) => entry.promise)),
116
+ new Promise(resolve => {
117
+ timeout = setTimeout(resolve, cleanupGraceMs);
118
+ timeout.unref?.();
119
+ }),
80
120
  ]);
81
121
  }
82
122
  finally {
83
123
  if (timeout !== undefined)
84
124
  clearTimeout(timeout);
85
- for (const [id, entry] of snapshot) {
125
+ for (const [id, entry] of remaining) {
86
126
  if (active.get(id) === entry)
87
127
  active.delete(id);
88
128
  }
89
129
  }
130
+ return remaining;
90
131
  }
91
132
  export class DeliveryAdapterRegistry {
92
133
  context;
@@ -434,6 +475,9 @@ export class InboundCoordinator {
434
475
  now;
435
476
  random;
436
477
  stopping = false;
478
+ stopPromise;
479
+ pendingReloadCode;
480
+ pendingStopEntries;
437
481
  constructor(options) {
438
482
  this.options = options;
439
483
  this.now = options.now ?? Date.now;
@@ -446,26 +490,115 @@ export class InboundCoordinator {
446
490
  const available = this.options.maxConcurrency - this.active.size;
447
491
  if (available <= 0)
448
492
  return;
493
+ const excludeSessionIds = this.options.blockedSessions?.();
449
494
  const claims = this.options.store.claimInbox({ ownerId: this.options.ownerId, leaseMs: this.options.leaseMs,
450
- limit: available, maxAttempts: this.options.maxAttempts });
495
+ limit: available, maxAttempts: this.options.maxAttempts,
496
+ ...(excludeSessionIds !== undefined && excludeSessionIds.size > 0 ? { excludeSessionIds } : {}) });
451
497
  for (const claim of claims) {
452
498
  const abort = new AbortController();
453
499
  const stopHeartbeat = this.startLeaseHeartbeat(claim.record.id, claim.fencingToken, abort);
500
+ const state = {
501
+ inboxId: claim.record.id,
502
+ fencingToken: claim.fencingToken,
503
+ dispatched: false,
504
+ settled: false,
505
+ };
454
506
  let entry;
455
- const promise = this.process(claim.record.id, claim.record.bindingId, claim.record.envelope, claim.fencingToken, abort.signal, permissionDispatchRecoveryFromFailureCode(claim.record.failureCode))
507
+ const promise = this.process(claim.record.id, claim.record.bindingId, claim.record.envelope, claim.fencingToken, abort.signal, state, permissionDispatchRecoveryFromFailureCode(claim.record.failureCode))
456
508
  .catch(() => { })
457
509
  .finally(() => {
458
510
  stopHeartbeat();
459
511
  if (this.active.get(claim.record.id) === entry)
460
512
  this.active.delete(claim.record.id);
461
513
  });
462
- entry = { abort, promise };
514
+ entry = { abort, promise, state };
463
515
  this.active.set(claim.record.id, entry);
464
516
  }
465
517
  }
466
- async stop() {
518
+ hasActive() {
519
+ return this.active.size > 0;
520
+ }
521
+ resume() {
522
+ this.stopping = false;
523
+ }
524
+ settleInbox(state, params) {
525
+ if (state.settled)
526
+ return;
527
+ state.settled = true;
528
+ try {
529
+ this.options.store.finishInbox({
530
+ inboxId: state.inboxId,
531
+ ownerId: this.options.ownerId,
532
+ fencingToken: state.fencingToken,
533
+ ...params,
534
+ });
535
+ }
536
+ catch {
537
+ // Store may already be closed if a late background unwind resolves past teardown.
538
+ }
539
+ }
540
+ renewUnsettledClaims(entries, cleanupGraceMs) {
541
+ const extendLeaseMs = Math.max(this.options.leaseMs, cleanupGraceMs + 2_000);
542
+ for (const [, entry] of entries) {
543
+ if (entry.state === undefined || entry.state.settled)
544
+ continue;
545
+ try {
546
+ this.options.store.renewInboxClaim({
547
+ inboxId: entry.state.inboxId,
548
+ ownerId: this.options.ownerId,
549
+ fencingToken: entry.state.fencingToken,
550
+ leaseMs: extendLeaseMs,
551
+ });
552
+ }
553
+ catch {
554
+ // Store outage or lost fence will be reconciled on recovery.
555
+ }
556
+ }
557
+ }
558
+ async stop(reloadCode, options) {
467
559
  this.stopping = true;
468
- await abortAndBoundedDrain(this.active, this.options.leaseMs, new Error('assistant-delivery is stopping'));
560
+ if (this.stopPromise !== undefined) {
561
+ if (reloadCode !== undefined && this.pendingReloadCode === undefined) {
562
+ this.pendingReloadCode = reloadCode;
563
+ this.renewUnsettledClaims(this.pendingStopEntries ?? [...this.active.entries()], Math.max(Math.floor(this.options.leaseMs / 3), 2_500));
564
+ }
565
+ return await this.stopPromise;
566
+ }
567
+ this.pendingReloadCode = reloadCode;
568
+ const reason = reloadCode !== undefined
569
+ ? new ReloadInterruptedError(reloadCode)
570
+ : new Error('assistant-delivery is stopping');
571
+ const shutdownGraceMs = options?.immediate === true ? 0 : this.options.shutdownGraceMs;
572
+ const stopping = (async () => {
573
+ const remaining = await abortAndBoundedDrain(this.active, this.options.leaseMs, reason, {
574
+ ...(shutdownGraceMs === undefined ? {} : { shutdownGraceMs }),
575
+ ...(reloadCode === undefined ? {} : { reloadCode }),
576
+ beforeAbort: (unsettled, cleanupGraceMs) => {
577
+ this.pendingStopEntries = unsettled;
578
+ if (this.pendingReloadCode === undefined)
579
+ return;
580
+ this.renewUnsettledClaims(unsettled, cleanupGraceMs);
581
+ },
582
+ });
583
+ const effectiveReloadCode = this.pendingReloadCode;
584
+ if (effectiveReloadCode !== undefined) {
585
+ for (const [, entry] of remaining) {
586
+ if (entry.state === undefined || entry.state.settled)
587
+ continue;
588
+ this.settleInbox(entry.state, entry.state.dispatched
589
+ ? { outcome: 'dead_letter', failureCode: effectiveReloadCode }
590
+ : { outcome: 'retry_wait', failureCode: effectiveReloadCode, retryAt: this.now() + this.options.retryBaseMs });
591
+ }
592
+ }
593
+ })().finally(() => {
594
+ if (this.stopPromise === stopping) {
595
+ this.stopPromise = undefined;
596
+ this.pendingReloadCode = undefined;
597
+ this.pendingStopEntries = undefined;
598
+ }
599
+ });
600
+ this.stopPromise = stopping;
601
+ await stopping;
469
602
  }
470
603
  /** Abort and drain exact claims that a durable command fence made terminal. */
471
604
  async cancelUndispatchedClaims(inboxIds, command) {
@@ -512,23 +645,25 @@ export class InboundCoordinator {
512
645
  abort.signal.addEventListener('abort', stop, { once: true });
513
646
  return stop;
514
647
  }
515
- async process(inboxId, bindingId, envelope, fencingToken, signal, permissionDispatchRecovery) {
648
+ async process(inboxId, bindingId, envelope, fencingToken, signal, state, permissionDispatchRecovery) {
516
649
  const binding = bindingId === undefined ? undefined : this.options.store.getBinding(bindingId);
517
650
  if (binding?.status === 'revoked') {
518
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
519
- outcome: 'dead_letter', failureCode: 'binding-revoked' });
651
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: 'binding-revoked' });
520
652
  return;
521
653
  }
522
654
  if (binding === undefined) {
523
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
655
+ this.settleInbox(state, {
524
656
  outcome: 'retry_wait', failureCode: 'binding-unavailable',
525
- retryAt: this.now() + this.options.retryBaseMs });
657
+ retryAt: this.now() + this.options.retryBaseMs,
658
+ });
526
659
  return;
527
660
  }
528
661
  const processor = this.options.processor();
529
662
  if (processor === undefined) {
530
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
531
- outcome: 'retry_wait', failureCode: 'processor-unavailable', retryAt: this.now() + this.options.retryBaseMs });
663
+ this.settleInbox(state, {
664
+ outcome: 'retry_wait', failureCode: 'processor-unavailable',
665
+ retryAt: this.now() + this.options.retryBaseMs,
666
+ });
532
667
  return;
533
668
  }
534
669
  let prepared;
@@ -543,37 +678,58 @@ export class InboundCoordinator {
543
678
  });
544
679
  }
545
680
  catch {
546
- if (signal.aborted)
681
+ if (signal.aborted) {
682
+ if (isReloadInterruptionSignal(signal)) {
683
+ this.settleInbox(state, {
684
+ outcome: 'retry_wait',
685
+ failureCode: signal.reason.code,
686
+ retryAt: this.now() + this.options.retryBaseMs,
687
+ });
688
+ }
547
689
  return;
548
- this.finishInbound(inboxId, fencingToken, {
690
+ }
691
+ this.finishInbound(state, {
549
692
  outcome: 'not-processed', failureCode: 'prepare-threw', retryable: true,
550
693
  });
551
694
  return;
552
695
  }
553
- if (signal.aborted)
696
+ if (signal.aborted) {
697
+ if (isReloadInterruptionSignal(signal)) {
698
+ this.settleInbox(state, {
699
+ outcome: 'retry_wait',
700
+ failureCode: signal.reason.code,
701
+ retryAt: this.now() + this.options.retryBaseMs,
702
+ });
703
+ }
554
704
  return;
705
+ }
555
706
  if (preparation.outcome !== 'prepared') {
556
- this.finishInbound(inboxId, fencingToken, preparation);
707
+ this.finishInbound(state, preparation);
557
708
  return;
558
709
  }
559
710
  prepared = preparation.message;
560
711
  }
561
- if (signal.aborted)
712
+ if (signal.aborted) {
713
+ if (isReloadInterruptionSignal(signal)) {
714
+ this.settleInbox(state, {
715
+ outcome: 'retry_wait',
716
+ failureCode: signal.reason.code,
717
+ retryAt: this.now() + this.options.retryBaseMs,
718
+ });
719
+ }
562
720
  return;
721
+ }
563
722
  const currentBinding = this.options.store.getBinding(binding.id);
564
723
  if (currentBinding === undefined || currentBinding.status !== 'active') {
565
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
566
- outcome: 'dead_letter', failureCode: 'binding-revoked' });
724
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: 'binding-revoked' });
567
725
  return;
568
726
  }
569
727
  if (bindingChangedBeforeDispatch(binding, currentBinding, envelope)) {
570
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
571
- outcome: 'dead_letter', failureCode: 'binding-changed-before-dispatch' });
728
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: 'binding-changed-before-dispatch' });
572
729
  return;
573
730
  }
574
731
  if (!this.options.store.isAuthorizedPrincipal(currentBinding.principal)) {
575
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
576
- outcome: 'dead_letter', failureCode: 'authorization-revoked-before-dispatch' });
732
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: 'authorization-revoked-before-dispatch' });
577
733
  return;
578
734
  }
579
735
  const explicitDispatch = processor.dispatchControl === 'explicit';
@@ -640,6 +796,7 @@ export class InboundCoordinator {
640
796
  throw error;
641
797
  }
642
798
  dispatchMarked = true;
799
+ state.dispatched = true;
643
800
  }
644
801
  : undefined;
645
802
  if (!explicitDispatch) {
@@ -648,12 +805,12 @@ export class InboundCoordinator {
648
805
  inboxId, ownerId: this.options.ownerId, fencingToken, binding: dispatchBinding,
649
806
  });
650
807
  dispatchMarked = true;
808
+ state.dispatched = true;
651
809
  }
652
810
  catch (error) {
653
811
  const failureCode = dispatchAuthorizationFailure(error);
654
812
  if (failureCode !== undefined) {
655
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
656
- outcome: 'dead_letter', failureCode });
813
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode });
657
814
  }
658
815
  // No processor call happened, so lease recovery remains the authority on
659
816
  // whether a stale or failed marker is safe to retry.
@@ -666,42 +823,68 @@ export class InboundCoordinator {
666
823
  }
667
824
  catch {
668
825
  dispatchGateOpen = false;
669
- if (signal.aborted)
826
+ if (signal.aborted) {
827
+ if (isReloadInterruptionSignal(signal)) {
828
+ if (dispatchMarked) {
829
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: signal.reason.code });
830
+ }
831
+ else {
832
+ this.settleInbox(state, {
833
+ outcome: 'retry_wait',
834
+ failureCode: signal.reason.code,
835
+ retryAt: this.now() + this.options.retryBaseMs,
836
+ });
837
+ }
838
+ }
670
839
  return;
840
+ }
671
841
  if (explicitDispatch && !dispatchMarked) {
672
842
  if (dispatchGateBindingFailure !== undefined) {
673
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
674
- outcome: 'dead_letter', failureCode: dispatchGateBindingFailure });
843
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: dispatchGateBindingFailure });
675
844
  return;
676
845
  }
677
- this.finishInbound(inboxId, fencingToken, {
846
+ this.finishInbound(state, {
678
847
  outcome: 'not-processed', failureCode: 'processor-threw-before-dispatch', retryable: true,
679
848
  });
680
849
  return;
681
850
  }
682
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
683
- outcome: 'dead_letter', failureCode: 'processor-ambiguous' });
851
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: 'processor-ambiguous' });
684
852
  return;
685
853
  }
686
854
  dispatchGateOpen = false;
687
- if (signal.aborted)
855
+ if (signal.aborted) {
856
+ if (isReloadInterruptionSignal(signal)) {
857
+ if (dispatchMarked) {
858
+ const failureCode = result.outcome !== 'processed' && isReloadInterruptionFailureCode(result.failureCode)
859
+ ? result.failureCode
860
+ : signal.reason.code;
861
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode });
862
+ }
863
+ else {
864
+ this.settleInbox(state, {
865
+ outcome: 'retry_wait',
866
+ failureCode: signal.reason.code,
867
+ retryAt: this.now() + this.options.retryBaseMs,
868
+ });
869
+ }
870
+ }
688
871
  return;
872
+ }
689
873
  if (dispatchGateBindingFailure !== undefined) {
690
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
691
- outcome: 'dead_letter', failureCode: dispatchGateBindingFailure });
874
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: dispatchGateBindingFailure });
692
875
  return;
693
876
  }
694
877
  if (dispatchGateFailed) {
695
- this.finishInbound(inboxId, fencingToken, {
878
+ this.finishInbound(state, {
696
879
  outcome: 'not-processed', failureCode: 'processor-threw-before-dispatch', retryable: true,
697
880
  });
698
881
  return;
699
882
  }
700
883
  if (dispatchMarked && result.outcome !== 'processed') {
701
884
  if (result.failureCode === 'agent-task-timeout'
702
- || isClassifiedAgentTurnFailureCode(result.failureCode)) {
703
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
704
- outcome: 'dead_letter', failureCode: result.failureCode });
885
+ || isClassifiedAgentTurnFailureCode(result.failureCode)
886
+ || isReloadInterruptionFailureCode(result.failureCode)) {
887
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: result.failureCode });
705
888
  return;
706
889
  }
707
890
  if (permissionDispatchRecoveryFromFailureCode(result.failureCode) !== undefined
@@ -710,40 +893,44 @@ export class InboundCoordinator {
710
893
  || isLearningDispatchRecoveryCode(result.failureCode)
711
894
  || isLearningRetryableFailureCode(result.failureCode)
712
895
  || isWorkflowDispatchRecoveryCode(result.failureCode)) {
713
- this.finishInbound(inboxId, fencingToken, result);
896
+ this.finishInbound(state, result);
714
897
  return;
715
898
  }
716
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
717
- outcome: 'dead_letter', failureCode: 'processor-ambiguous' });
899
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: 'processor-ambiguous' });
718
900
  return;
719
901
  }
720
- this.finishInbound(inboxId, fencingToken, result);
902
+ this.finishInbound(state, result);
721
903
  }
722
- finishInbound(inboxId, fencingToken, result) {
904
+ finishInbound(state, result) {
905
+ if (state.settled)
906
+ return;
723
907
  if (result.outcome === 'processed') {
724
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken, outcome: 'processed' });
908
+ this.settleInbox(state, { outcome: 'processed' });
725
909
  return;
726
910
  }
727
911
  if (result.failureCode === 'agent-task-timeout'
728
- || isClassifiedAgentTurnFailureCode(result.failureCode)) {
729
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
730
- outcome: 'dead_letter', failureCode: result.failureCode });
912
+ || isClassifiedAgentTurnFailureCode(result.failureCode)
913
+ || isReloadInterruptionFailureCode(result.failureCode)) {
914
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: result.failureCode });
731
915
  return;
732
916
  }
733
917
  if (result.failureCode === 'session-lease-busy'
734
- && this.options.store.deferInboxForSessionLease({ inboxId, ownerId: this.options.ownerId,
735
- fencingToken, retryAt: this.now() + Math.max(this.options.retryBaseMs, result.retryAfterMs ?? 0) }))
918
+ && this.options.store.deferInboxForSessionLease({ inboxId: state.inboxId, ownerId: this.options.ownerId,
919
+ fencingToken: state.fencingToken, retryAt: this.now() + Math.max(this.options.retryBaseMs, result.retryAfterMs ?? 0) })) {
920
+ state.settled = true;
736
921
  return;
922
+ }
737
923
  if (!result.retryable) {
738
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
739
- outcome: 'dead_letter', failureCode: result.failureCode });
924
+ this.settleInbox(state, { outcome: 'dead_letter', failureCode: result.failureCode });
740
925
  return;
741
926
  }
742
- const current = this.options.store.getInbox(inboxId);
927
+ const current = this.options.store.getInbox(state.inboxId);
743
928
  const exponential = Math.min(this.options.retryMaxMs, this.options.retryBaseMs * (2 ** Math.max(0, current.attemptCount - 1)) * (1 + this.random()));
744
- this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
745
- outcome: 'retry_wait', failureCode: result.failureCode,
746
- retryAt: this.now() + Math.ceil(Math.max(exponential, result.retryAfterMs ?? 0)) });
929
+ this.settleInbox(state, {
930
+ outcome: 'retry_wait',
931
+ failureCode: result.failureCode,
932
+ retryAt: this.now() + Math.ceil(Math.max(exponential, result.retryAfterMs ?? 0)),
933
+ });
747
934
  }
748
935
  }
749
936
  //# sourceMappingURL=coordinator.js.map