@dsh-ray/assistant-delivery 0.0.1-seed.0 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +610 -1
- package/bin/dsh-restart-supervisor.js +102 -0
- package/cordis.patch.yml +29 -0
- package/lib/acceptance.d.ts +54 -0
- package/lib/acceptance.d.ts.map +1 -0
- package/lib/acceptance.js +2 -0
- package/lib/acceptance.js.map +1 -0
- package/lib/agent-runtime.d.ts +247 -0
- package/lib/agent-runtime.d.ts.map +1 -0
- package/lib/agent-runtime.js +4458 -0
- package/lib/agent-runtime.js.map +1 -0
- package/lib/approval-status-context.d.ts +8 -0
- package/lib/approval-status-context.d.ts.map +1 -0
- package/lib/approval-status-context.js +55 -0
- package/lib/approval-status-context.js.map +1 -0
- package/lib/attention-audit-store.d.ts +54 -0
- package/lib/attention-audit-store.d.ts.map +1 -0
- package/lib/attention-audit-store.js +213 -0
- package/lib/attention-audit-store.js.map +1 -0
- package/lib/attention-enforcement.d.ts +75 -0
- package/lib/attention-enforcement.d.ts.map +1 -0
- package/lib/attention-enforcement.js +84 -0
- package/lib/attention-enforcement.js.map +1 -0
- package/lib/attention.d.ts +208 -0
- package/lib/attention.d.ts.map +1 -0
- package/lib/attention.js +288 -0
- package/lib/attention.js.map +1 -0
- package/lib/canonical.d.ts +28 -0
- package/lib/canonical.d.ts.map +1 -0
- package/lib/canonical.js +190 -0
- package/lib/canonical.js.map +1 -0
- package/lib/coordinator.d.ts +134 -0
- package/lib/coordinator.d.ts.map +1 -0
- package/lib/coordinator.js +746 -0
- package/lib/coordinator.js.map +1 -0
- package/lib/feedback-command.d.ts +105 -0
- package/lib/feedback-command.d.ts.map +1 -0
- package/lib/feedback-command.js +547 -0
- package/lib/feedback-command.js.map +1 -0
- package/lib/foreground-observation.d.ts +66 -0
- package/lib/foreground-observation.d.ts.map +1 -0
- package/lib/foreground-observation.js +13 -0
- package/lib/foreground-observation.js.map +1 -0
- package/lib/foreground-termination.d.ts +31 -0
- package/lib/foreground-termination.d.ts.map +1 -0
- package/lib/foreground-termination.js +42 -0
- package/lib/foreground-termination.js.map +1 -0
- package/lib/goal-wake-types.d.ts +93 -0
- package/lib/goal-wake-types.d.ts.map +1 -0
- package/lib/goal-wake-types.js +70 -0
- package/lib/goal-wake-types.js.map +1 -0
- package/lib/in-process-reload-runtime.d.ts +120 -0
- package/lib/in-process-reload-runtime.d.ts.map +1 -0
- package/lib/in-process-reload-runtime.js +1280 -0
- package/lib/in-process-reload-runtime.js.map +1 -0
- package/lib/inbound-images.d.ts +38 -0
- package/lib/inbound-images.d.ts.map +1 -0
- package/lib/inbound-images.js +269 -0
- package/lib/inbound-images.js.map +1 -0
- package/lib/index.d.ts +41 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +25 -0
- package/lib/index.js.map +1 -0
- package/lib/learning-command.d.ts +40 -0
- package/lib/learning-command.d.ts.map +1 -0
- package/lib/learning-command.js +57 -0
- package/lib/learning-command.js.map +1 -0
- package/lib/native-termination.d.ts +12 -0
- package/lib/native-termination.d.ts.map +1 -0
- package/lib/native-termination.js +57 -0
- package/lib/native-termination.js.map +1 -0
- package/lib/native-web-owner.d.ts +97 -0
- package/lib/native-web-owner.d.ts.map +1 -0
- package/lib/native-web-owner.js +338 -0
- package/lib/native-web-owner.js.map +1 -0
- package/lib/operator-snapshot.d.ts +107 -0
- package/lib/operator-snapshot.d.ts.map +1 -0
- package/lib/operator-snapshot.js +1013 -0
- package/lib/operator-snapshot.js.map +1 -0
- package/lib/operator.d.ts +25 -0
- package/lib/operator.d.ts.map +1 -0
- package/lib/operator.js +48 -0
- package/lib/operator.js.map +1 -0
- package/lib/progress-attention.d.ts +55 -0
- package/lib/progress-attention.d.ts.map +1 -0
- package/lib/progress-attention.js +128 -0
- package/lib/progress-attention.js.map +1 -0
- package/lib/reply-replay.d.ts +52 -0
- package/lib/reply-replay.d.ts.map +1 -0
- package/lib/reply-replay.js +122 -0
- package/lib/reply-replay.js.map +1 -0
- package/lib/restart-native.d.ts +47 -0
- package/lib/restart-native.d.ts.map +1 -0
- package/lib/restart-native.js +170 -0
- package/lib/restart-native.js.map +1 -0
- package/lib/restart-runtime.d.ts +38 -0
- package/lib/restart-runtime.d.ts.map +1 -0
- package/lib/restart-runtime.js +335 -0
- package/lib/restart-runtime.js.map +1 -0
- package/lib/restart-store.d.ts +132 -0
- package/lib/restart-store.d.ts.map +1 -0
- package/lib/restart-store.js +757 -0
- package/lib/restart-store.js.map +1 -0
- package/lib/restart-supervisor.d.ts +129 -0
- package/lib/restart-supervisor.d.ts.map +1 -0
- package/lib/restart-supervisor.js +615 -0
- package/lib/restart-supervisor.js.map +1 -0
- package/lib/service.d.ts +1279 -0
- package/lib/service.d.ts.map +1 -0
- package/lib/service.js +8694 -0
- package/lib/service.js.map +1 -0
- package/lib/session-commands.d.ts +52 -0
- package/lib/session-commands.d.ts.map +1 -0
- package/lib/session-commands.js +180 -0
- package/lib/session-commands.js.map +1 -0
- package/lib/session-lease-runtime.d.ts +70 -0
- package/lib/session-lease-runtime.d.ts.map +1 -0
- package/lib/session-lease-runtime.js +395 -0
- package/lib/session-lease-runtime.js.map +1 -0
- package/lib/session-lease-types.d.ts +27 -0
- package/lib/session-lease-types.d.ts.map +1 -0
- package/lib/session-lease-types.js +2 -0
- package/lib/session-lease-types.js.map +1 -0
- package/lib/sqlite.d.ts +8 -0
- package/lib/sqlite.d.ts.map +1 -0
- package/lib/sqlite.js +1665 -0
- package/lib/sqlite.js.map +1 -0
- package/lib/store.d.ts +1267 -0
- package/lib/store.d.ts.map +1 -0
- package/lib/store.js +8039 -0
- package/lib/store.js.map +1 -0
- package/lib/tools.d.ts +4 -0
- package/lib/tools.d.ts.map +1 -0
- package/lib/tools.js +73 -0
- package/lib/tools.js.map +1 -0
- package/lib/types.d.ts +1091 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +10 -0
- package/lib/types.js.map +1 -0
- package/lib/version-query.d.ts +77 -0
- package/lib/version-query.d.ts.map +1 -0
- package/lib/version-query.js +2208 -0
- package/lib/version-query.js.map +1 -0
- package/lib/version.d.ts +2 -0
- package/lib/version.d.ts.map +1 -0
- package/lib/version.js +2 -0
- package/lib/version.js.map +1 -0
- package/lib/work-item-engineering-context.d.ts +127 -0
- package/lib/work-item-engineering-context.d.ts.map +1 -0
- package/lib/work-item-engineering-context.js +151 -0
- package/lib/work-item-engineering-context.js.map +1 -0
- package/lib/work-item-feedback-schema.d.ts +2 -0
- package/lib/work-item-feedback-schema.d.ts.map +1 -0
- package/lib/work-item-feedback-schema.js +29 -0
- package/lib/work-item-feedback-schema.js.map +1 -0
- package/lib/work-item-feedback-store.d.ts +29 -0
- package/lib/work-item-feedback-store.d.ts.map +1 -0
- package/lib/work-item-feedback-store.js +111 -0
- package/lib/work-item-feedback-store.js.map +1 -0
- package/lib/work-item-feedback.d.ts +157 -0
- package/lib/work-item-feedback.d.ts.map +1 -0
- package/lib/work-item-feedback.js +2 -0
- package/lib/work-item-feedback.js.map +1 -0
- package/lib/work-item-replies-schema.d.ts +3 -0
- package/lib/work-item-replies-schema.d.ts.map +1 -0
- package/lib/work-item-replies-schema.js +45 -0
- package/lib/work-item-replies-schema.js.map +1 -0
- package/lib/work-item-replies-store.d.ts +43 -0
- package/lib/work-item-replies-store.d.ts.map +1 -0
- package/lib/work-item-replies-store.js +203 -0
- package/lib/work-item-replies-store.js.map +1 -0
- package/lib/work-item-replies.d.ts +93 -0
- package/lib/work-item-replies.d.ts.map +1 -0
- package/lib/work-item-replies.js +15 -0
- package/lib/work-item-replies.js.map +1 -0
- package/lib/workflow-auto-producer.d.ts +37 -0
- package/lib/workflow-auto-producer.d.ts.map +1 -0
- package/lib/workflow-auto-producer.js +58 -0
- package/lib/workflow-auto-producer.js.map +1 -0
- package/lib/workflow-command.d.ts +16 -0
- package/lib/workflow-command.d.ts.map +1 -0
- package/lib/workflow-command.js +52 -0
- package/lib/workflow-command.js.map +1 -0
- package/package.json +174 -4
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
import { isFeedbackDispatchRecoveryCode, isFeedbackRetryableFailureCode, isLearningDispatchRecoveryCode, isLearningRetryableFailureCode, isWorkflowDispatchRecoveryCode, permissionDispatchRecoveryFromFailureCode, } from './session-commands.js';
|
|
2
|
+
import { DeliveryStoreError, } from './store.js';
|
|
3
|
+
function adapterKey(channel, account) {
|
|
4
|
+
return JSON.stringify([channel, account]);
|
|
5
|
+
}
|
|
6
|
+
function samePrincipal(left, right) {
|
|
7
|
+
return left.channel === right.channel
|
|
8
|
+
&& left.account === right.account
|
|
9
|
+
&& left.tenant === right.tenant
|
|
10
|
+
&& left.user === right.user;
|
|
11
|
+
}
|
|
12
|
+
function sameConversation(left, right) {
|
|
13
|
+
return left.channel === right.channel
|
|
14
|
+
&& left.account === right.account
|
|
15
|
+
&& left.tenant === right.tenant
|
|
16
|
+
&& left.kind === right.kind
|
|
17
|
+
&& left.chat === right.chat
|
|
18
|
+
&& left.thread === right.thread;
|
|
19
|
+
}
|
|
20
|
+
function bindingChangedBeforeDispatch(claimed, current, envelope) {
|
|
21
|
+
return current.id !== claimed.id
|
|
22
|
+
|| current.version !== claimed.version
|
|
23
|
+
|| current.sessionId !== claimed.sessionId
|
|
24
|
+
|| current.generation !== claimed.generation
|
|
25
|
+
|| !samePrincipal(current.principal, claimed.principal)
|
|
26
|
+
|| !samePrincipal(current.principal, envelope.principal)
|
|
27
|
+
|| !sameConversation(current.conversation, claimed.conversation)
|
|
28
|
+
|| !sameConversation(current.conversation, envelope.conversation);
|
|
29
|
+
}
|
|
30
|
+
function dispatchAuthorizationFailure(error) {
|
|
31
|
+
if (!(error instanceof DeliveryStoreError))
|
|
32
|
+
return undefined;
|
|
33
|
+
if (error.code === 'unauthorized-principal')
|
|
34
|
+
return 'authorization-revoked-before-dispatch';
|
|
35
|
+
if (error.code === 'invalid-binding')
|
|
36
|
+
return 'binding-changed-before-dispatch';
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const adapterDisposeGraceMs = 5_000;
|
|
40
|
+
// A registry that is (or becomes) stopped rejects registration with this typed
|
|
41
|
+
// error. It means the whole plugin tree is tearing down — often because an
|
|
42
|
+
// earlier plugin failed to load (e.g. the web server hit EADDRINUSE) — so a
|
|
43
|
+
// channel adapter that was mid-registration must not surface it as its own
|
|
44
|
+
// fatal cause and mask the real one.
|
|
45
|
+
export class DeliveryAdapterRegistryStoppedError extends Error {
|
|
46
|
+
constructor() {
|
|
47
|
+
super('assistant-delivery adapter registry is stopped');
|
|
48
|
+
this.name = 'DeliveryAdapterRegistryStoppedError';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function boundedWait(operation, graceMs) {
|
|
52
|
+
let timeout;
|
|
53
|
+
try {
|
|
54
|
+
return await Promise.race([
|
|
55
|
+
operation,
|
|
56
|
+
new Promise(resolve => { timeout = setTimeout(() => resolve(undefined), graceMs); }),
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
if (timeout !== undefined)
|
|
61
|
+
clearTimeout(timeout);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function abortAndBoundedDrain(active, leaseMs, reason) {
|
|
65
|
+
const snapshot = [...active.entries()];
|
|
66
|
+
for (const [, entry] of snapshot)
|
|
67
|
+
entry.abort.abort(reason);
|
|
68
|
+
if (snapshot.length === 0)
|
|
69
|
+
return;
|
|
70
|
+
// Abort stops lease renewal immediately. One heartbeat interval gives a
|
|
71
|
+
// cooperative adapter time to unwind without allowing an uncooperative one
|
|
72
|
+
// to hold service disposal (and the SQLite handle) forever.
|
|
73
|
+
const graceMs = Math.max(1, Math.floor(leaseMs / 3));
|
|
74
|
+
let timeout;
|
|
75
|
+
try {
|
|
76
|
+
await Promise.race([
|
|
77
|
+
Promise.all(snapshot.map(([, entry]) => entry.promise)),
|
|
78
|
+
new Promise(resolve => { timeout = setTimeout(resolve, graceMs); }),
|
|
79
|
+
]);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (timeout !== undefined)
|
|
83
|
+
clearTimeout(timeout);
|
|
84
|
+
for (const [id, entry] of snapshot) {
|
|
85
|
+
if (active.get(id) === entry)
|
|
86
|
+
active.delete(id);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export class DeliveryAdapterRegistry {
|
|
91
|
+
context;
|
|
92
|
+
adapters = new Map();
|
|
93
|
+
starting = new Map();
|
|
94
|
+
stopSignal;
|
|
95
|
+
resolveStop;
|
|
96
|
+
stopped = false;
|
|
97
|
+
constructor(context) {
|
|
98
|
+
this.context = context;
|
|
99
|
+
this.stopSignal = new Promise(resolve => { this.resolveStop = resolve; });
|
|
100
|
+
}
|
|
101
|
+
async register(adapter) {
|
|
102
|
+
if (this.stopped)
|
|
103
|
+
throw new DeliveryAdapterRegistryStoppedError();
|
|
104
|
+
const key = adapterKey(adapter.channel, adapter.account);
|
|
105
|
+
if (this.adapters.has(key) || this.starting.has(key)) {
|
|
106
|
+
throw new Error(`delivery adapter ${adapter.channel}/${adapter.account} is already registered`);
|
|
107
|
+
}
|
|
108
|
+
const result = Promise.resolve()
|
|
109
|
+
.then(() => adapter.start(this.context))
|
|
110
|
+
.then(dispose => ({ status: 'started', ...(dispose === undefined ? {} : { dispose }) }), error => ({ status: 'failed', error }));
|
|
111
|
+
const entry = { result };
|
|
112
|
+
this.starting.set(key, entry);
|
|
113
|
+
let dispose;
|
|
114
|
+
try {
|
|
115
|
+
const outcome = await Promise.race([
|
|
116
|
+
result,
|
|
117
|
+
this.stopSignal.then(() => ({ status: 'stopped' })),
|
|
118
|
+
]);
|
|
119
|
+
if (outcome.status === 'stopped') {
|
|
120
|
+
void this.cleanupStarting(entry);
|
|
121
|
+
throw new DeliveryAdapterRegistryStoppedError();
|
|
122
|
+
}
|
|
123
|
+
if (outcome.status === 'failed')
|
|
124
|
+
throw outcome.error;
|
|
125
|
+
dispose = outcome.dispose;
|
|
126
|
+
if (this.stopped) {
|
|
127
|
+
await this.cleanupStarting(entry);
|
|
128
|
+
throw new DeliveryAdapterRegistryStoppedError();
|
|
129
|
+
}
|
|
130
|
+
if (this.starting.get(key) === entry)
|
|
131
|
+
this.starting.delete(key);
|
|
132
|
+
this.adapters.set(key, { adapter, ...(dispose === undefined ? {} : { dispose }) });
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
if (this.starting.get(key) === entry)
|
|
136
|
+
this.starting.delete(key);
|
|
137
|
+
}
|
|
138
|
+
let active = true;
|
|
139
|
+
return async () => {
|
|
140
|
+
if (!active)
|
|
141
|
+
return;
|
|
142
|
+
active = false;
|
|
143
|
+
const current = this.adapters.get(key);
|
|
144
|
+
if (current?.adapter !== adapter)
|
|
145
|
+
return;
|
|
146
|
+
this.adapters.delete(key);
|
|
147
|
+
await boundedWait(Promise.resolve().then(() => current.dispose?.()), adapterDisposeGraceMs);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
get(channel, account) {
|
|
151
|
+
return this.adapters.get(adapterKey(channel, account))?.adapter;
|
|
152
|
+
}
|
|
153
|
+
isStarting(channel, account) {
|
|
154
|
+
return this.starting.has(adapterKey(channel, account));
|
|
155
|
+
}
|
|
156
|
+
isStopped() {
|
|
157
|
+
return this.stopped;
|
|
158
|
+
}
|
|
159
|
+
reconcilableUnknownRoutes() {
|
|
160
|
+
const routes = [];
|
|
161
|
+
for (const { adapter } of this.adapters.values()) {
|
|
162
|
+
if (adapter.capabilities.reconcileUnknownSend === true && adapter.reconcileUnknownSend !== undefined) {
|
|
163
|
+
routes.push({ channel: adapter.channel, account: adapter.account });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return routes;
|
|
167
|
+
}
|
|
168
|
+
size() { return this.adapters.size; }
|
|
169
|
+
async stop() {
|
|
170
|
+
if (this.stopped)
|
|
171
|
+
return;
|
|
172
|
+
this.stopped = true;
|
|
173
|
+
this.resolveStop();
|
|
174
|
+
const current = [...this.adapters.values()];
|
|
175
|
+
const pending = [...this.starting.values()];
|
|
176
|
+
this.adapters.clear();
|
|
177
|
+
await boundedWait(Promise.allSettled([
|
|
178
|
+
...current.map(entry => Promise.resolve().then(() => entry.dispose?.())),
|
|
179
|
+
...pending.map(entry => this.cleanupStarting(entry)),
|
|
180
|
+
]), adapterDisposeGraceMs);
|
|
181
|
+
}
|
|
182
|
+
cleanupStarting(entry) {
|
|
183
|
+
entry.cleanup ??= entry.result.then(async (outcome) => {
|
|
184
|
+
if (outcome.status !== 'started')
|
|
185
|
+
return;
|
|
186
|
+
const dispose = outcome.dispose;
|
|
187
|
+
if (dispose === undefined)
|
|
188
|
+
return;
|
|
189
|
+
await boundedWait(Promise.resolve().then(() => dispose()), adapterDisposeGraceMs)
|
|
190
|
+
.catch(() => undefined);
|
|
191
|
+
});
|
|
192
|
+
return entry.cleanup;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
export class DeliveryCoordinator {
|
|
196
|
+
options;
|
|
197
|
+
active = new Map();
|
|
198
|
+
now;
|
|
199
|
+
random;
|
|
200
|
+
timer;
|
|
201
|
+
stopping = false;
|
|
202
|
+
constructor(options) {
|
|
203
|
+
this.options = options;
|
|
204
|
+
this.now = options.now ?? Date.now;
|
|
205
|
+
this.random = options.random ?? Math.random;
|
|
206
|
+
}
|
|
207
|
+
async tick() {
|
|
208
|
+
if (this.stopping)
|
|
209
|
+
return;
|
|
210
|
+
this.options.store.recoverOutbox({ maxAttempts: this.options.maxAttempts });
|
|
211
|
+
const available = this.options.maxConcurrency - this.active.size;
|
|
212
|
+
if (available <= 0)
|
|
213
|
+
return;
|
|
214
|
+
const claims = this.options.store.claimOutbox({
|
|
215
|
+
ownerId: this.options.ownerId,
|
|
216
|
+
leaseMs: this.options.leaseMs,
|
|
217
|
+
limit: available,
|
|
218
|
+
maxAttempts: this.options.maxAttempts,
|
|
219
|
+
unknownReconcileRoutes: this.options.registry.reconcilableUnknownRoutes(),
|
|
220
|
+
excludeIds: [...this.active.keys()],
|
|
221
|
+
...(this.options.ownerRouteGuard === undefined ? {} : { ownerRouteGuard: this.options.ownerRouteGuard }),
|
|
222
|
+
});
|
|
223
|
+
for (const claim of claims) {
|
|
224
|
+
const abort = new AbortController();
|
|
225
|
+
const stopHeartbeat = this.startLeaseHeartbeat(claim.record.id, claim.fencingToken, abort);
|
|
226
|
+
let entry;
|
|
227
|
+
const promise = this.process(claim.record, claim.fencingToken, claim.mode, abort.signal)
|
|
228
|
+
.catch(() => { })
|
|
229
|
+
.finally(() => {
|
|
230
|
+
stopHeartbeat();
|
|
231
|
+
if (this.active.get(claim.record.id) === entry)
|
|
232
|
+
this.active.delete(claim.record.id);
|
|
233
|
+
});
|
|
234
|
+
entry = { abort, promise };
|
|
235
|
+
this.active.set(claim.record.id, entry);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
start() {
|
|
239
|
+
if (this.timer !== undefined)
|
|
240
|
+
return;
|
|
241
|
+
this.stopping = false;
|
|
242
|
+
const interval = this.options.tickIntervalMs ?? 1_000;
|
|
243
|
+
this.timer = setInterval(() => void this.tick().catch(() => { }), interval);
|
|
244
|
+
this.timer.unref?.();
|
|
245
|
+
void this.tick().catch(() => { });
|
|
246
|
+
}
|
|
247
|
+
async stop() {
|
|
248
|
+
this.stopping = true;
|
|
249
|
+
if (this.timer !== undefined)
|
|
250
|
+
clearInterval(this.timer);
|
|
251
|
+
this.timer = undefined;
|
|
252
|
+
await abortAndBoundedDrain(this.active, this.options.leaseMs, new Error('assistant-delivery is stopping'));
|
|
253
|
+
}
|
|
254
|
+
async whenIdle() {
|
|
255
|
+
while (this.active.size > 0)
|
|
256
|
+
await Promise.all([...this.active.values()].map(entry => entry.promise));
|
|
257
|
+
}
|
|
258
|
+
startLeaseHeartbeat(outboxId, fencingToken, abort) {
|
|
259
|
+
let active = true;
|
|
260
|
+
const interval = Math.max(1, Math.floor(this.options.leaseMs / 3));
|
|
261
|
+
const heartbeat = setInterval(() => {
|
|
262
|
+
if (!active || abort.signal.aborted)
|
|
263
|
+
return;
|
|
264
|
+
try {
|
|
265
|
+
if (this.options.store.renewOutboxClaim({ outboxId, ownerId: this.options.ownerId, fencingToken,
|
|
266
|
+
leaseMs: this.options.leaseMs }))
|
|
267
|
+
return;
|
|
268
|
+
abort.abort(new Error('assistant-delivery outbox lease was lost'));
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
abort.abort(error);
|
|
272
|
+
}
|
|
273
|
+
}, interval);
|
|
274
|
+
heartbeat.unref?.();
|
|
275
|
+
const stop = () => {
|
|
276
|
+
if (!active)
|
|
277
|
+
return;
|
|
278
|
+
active = false;
|
|
279
|
+
clearInterval(heartbeat);
|
|
280
|
+
abort.signal.removeEventListener('abort', stop);
|
|
281
|
+
};
|
|
282
|
+
abort.signal.addEventListener('abort', stop, { once: true });
|
|
283
|
+
return stop;
|
|
284
|
+
}
|
|
285
|
+
async process(record, fencingToken, mode, signal) {
|
|
286
|
+
const adapter = this.options.registry.get(record.intent.target.conversation.channel, record.intent.target.conversation.account);
|
|
287
|
+
if (adapter === undefined) {
|
|
288
|
+
this.finish(record, fencingToken, mode === 'reconcile'
|
|
289
|
+
? { outcome: 'unknown', failureCode: 'adapter-unavailable' }
|
|
290
|
+
: { outcome: 'not-sent', failureCode: 'adapter-unavailable', retryable: true });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (!adapter.capabilities.formats.includes(record.intent.format ?? 'plain')) {
|
|
294
|
+
this.finish(record, fencingToken, { outcome: 'not-sent', failureCode: 'unsupported-format', retryable: false });
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
// WP19 stage 2b: the inline body of a converted notice is only a summary; never send it without its attachment.
|
|
298
|
+
// Reconcile never resends, so an unknown send is still reconciled; a reconciled not-sent row lands here next.
|
|
299
|
+
if (mode === 'send' && record.intent.attachment !== undefined && adapter.capabilities.textAttachments !== true) {
|
|
300
|
+
this.finish(record, fencingToken, { outcome: 'not-sent', failureCode: 'attachment-unsupported', retryable: false });
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (mode === 'reconcile') {
|
|
304
|
+
if (!adapter.capabilities.reconcileUnknownSend || adapter.reconcileUnknownSend === undefined) {
|
|
305
|
+
this.finish(record, fencingToken, { outcome: 'unknown', failureCode: 'reconcile-unsupported' });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
try {
|
|
309
|
+
const result = await adapter.reconcileUnknownSend(record, signal);
|
|
310
|
+
if (signal.aborted)
|
|
311
|
+
return;
|
|
312
|
+
this.finishReconcile(record, fencingToken, result);
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
if (signal.aborted)
|
|
316
|
+
return;
|
|
317
|
+
this.finish(record, fencingToken, { outcome: 'unknown', failureCode: 'reconcile-threw' });
|
|
318
|
+
}
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const ownerRoute = this.options.ownerRouteGuard === undefined
|
|
322
|
+
? { kind: 'not-route' }
|
|
323
|
+
: this.options.store.validateClaimedOwnerRoute(record, this.options.ownerRouteGuard);
|
|
324
|
+
if (ownerRoute.kind === 'denied') {
|
|
325
|
+
this.finish(record, fencingToken, {
|
|
326
|
+
outcome: 'not-sent', failureCode: ownerRoute.failureCode, retryable: false,
|
|
327
|
+
});
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (ownerRoute.kind === 'deferred') {
|
|
331
|
+
this.finish(record, fencingToken, {
|
|
332
|
+
outcome: 'not-sent', failureCode: ownerRoute.failureCode, retryable: true,
|
|
333
|
+
});
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (ownerRoute.kind === 'not-route') {
|
|
337
|
+
const binding = this.options.store.getBinding(record.intent.bindingId);
|
|
338
|
+
if (binding?.status !== 'active') {
|
|
339
|
+
this.finish(record, fencingToken, { outcome: 'not-sent', failureCode: 'binding-revoked', retryable: false });
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (!sameConversation(binding.conversation, record.intent.target.conversation)
|
|
343
|
+
|| !samePrincipal(binding.principal, record.intent.target.principal)) {
|
|
344
|
+
this.finish(record, fencingToken, {
|
|
345
|
+
outcome: 'not-sent', failureCode: 'binding-target-mismatch', retryable: false,
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
let authorized;
|
|
350
|
+
try {
|
|
351
|
+
authorized = this.options.store.isAuthorizedPrincipal(binding.principal);
|
|
352
|
+
}
|
|
353
|
+
catch {
|
|
354
|
+
this.finish(record, fencingToken, {
|
|
355
|
+
outcome: 'not-sent', failureCode: 'principal-authorization-check-failed', retryable: true,
|
|
356
|
+
});
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (!authorized) {
|
|
360
|
+
this.finish(record, fencingToken, { outcome: 'not-sent', failureCode: 'principal-revoked', retryable: false });
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const notice = this.options.notificationGuard?.(record);
|
|
365
|
+
if (notice !== undefined) {
|
|
366
|
+
this.finish(record, fencingToken, notice);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
try {
|
|
370
|
+
const result = await adapter.send(record.intent, signal);
|
|
371
|
+
if (signal.aborted)
|
|
372
|
+
return;
|
|
373
|
+
this.finish(record, fencingToken, result);
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
if (signal.aborted)
|
|
377
|
+
return;
|
|
378
|
+
this.finish(record, fencingToken, { outcome: 'unknown', failureCode: 'adapter-threw' });
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
finish(record, fencingToken, result) {
|
|
382
|
+
if (result.outcome === 'accepted') {
|
|
383
|
+
this.options.store.finishOutbox({ outboxId: record.id, ownerId: this.options.ownerId, fencingToken,
|
|
384
|
+
outcome: 'accepted', providerMessageId: result.providerMessageId });
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (result.outcome === 'unknown') {
|
|
388
|
+
this.options.store.finishOutbox({ outboxId: record.id, ownerId: this.options.ownerId, fencingToken,
|
|
389
|
+
outcome: 'unknown_after_send', failureCode: result.failureCode,
|
|
390
|
+
...(result.providerMessageId === undefined ? {} : { providerMessageId: result.providerMessageId }) });
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (!result.retryable) {
|
|
394
|
+
this.options.store.finishOutbox({ outboxId: record.id, ownerId: this.options.ownerId, fencingToken,
|
|
395
|
+
outcome: 'dead', failureCode: result.failureCode });
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const exponential = Math.min(this.options.retryMaxMs, this.options.retryBaseMs * (2 ** Math.max(0, record.attemptCount - 1)) * (1 + this.random()));
|
|
399
|
+
const delay = Math.max(exponential, result.retryAfterMs ?? 0);
|
|
400
|
+
this.options.store.finishOutbox({ outboxId: record.id, ownerId: this.options.ownerId, fencingToken,
|
|
401
|
+
outcome: 'retry_wait', failureCode: result.failureCode, retryAt: this.now() + Math.ceil(delay) });
|
|
402
|
+
}
|
|
403
|
+
finishReconcile(record, fencingToken, result) {
|
|
404
|
+
if (result.outcome === 'unknown') {
|
|
405
|
+
this.finish(record, fencingToken, { outcome: 'unknown', failureCode: 'reconcile-inconclusive' });
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (result.outcome === 'not-sent') {
|
|
409
|
+
const ownerRoute = record.intent.metadata?.['dsh.route.authority'] !== undefined;
|
|
410
|
+
this.finish(record, fencingToken, {
|
|
411
|
+
outcome: 'not-sent',
|
|
412
|
+
failureCode: ownerRoute ? 'owner-route-reconciled-not-sent' : 'reconciled-not-sent',
|
|
413
|
+
retryable: !ownerRoute,
|
|
414
|
+
});
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
this.options.store.finishOutbox({ outboxId: record.id, ownerId: this.options.ownerId, fencingToken,
|
|
418
|
+
outcome: 'accepted', providerMessageId: result.providerMessageId });
|
|
419
|
+
if (result.outcome !== 'accepted') {
|
|
420
|
+
this.options.store.recordReceipt({
|
|
421
|
+
channel: record.intent.target.conversation.channel,
|
|
422
|
+
account: record.intent.target.conversation.account,
|
|
423
|
+
providerMessageId: result.providerMessageId,
|
|
424
|
+
status: result.outcome,
|
|
425
|
+
occurredAt: this.now(),
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
export class InboundCoordinator {
|
|
431
|
+
options;
|
|
432
|
+
active = new Map();
|
|
433
|
+
now;
|
|
434
|
+
random;
|
|
435
|
+
stopping = false;
|
|
436
|
+
constructor(options) {
|
|
437
|
+
this.options = options;
|
|
438
|
+
this.now = options.now ?? Date.now;
|
|
439
|
+
this.random = options.random ?? Math.random;
|
|
440
|
+
}
|
|
441
|
+
async tick() {
|
|
442
|
+
if (this.stopping)
|
|
443
|
+
return;
|
|
444
|
+
this.options.store.recoverInbox({ maxAttempts: this.options.maxAttempts });
|
|
445
|
+
const available = this.options.maxConcurrency - this.active.size;
|
|
446
|
+
if (available <= 0)
|
|
447
|
+
return;
|
|
448
|
+
const claims = this.options.store.claimInbox({ ownerId: this.options.ownerId, leaseMs: this.options.leaseMs,
|
|
449
|
+
limit: available, maxAttempts: this.options.maxAttempts });
|
|
450
|
+
for (const claim of claims) {
|
|
451
|
+
const abort = new AbortController();
|
|
452
|
+
const stopHeartbeat = this.startLeaseHeartbeat(claim.record.id, claim.fencingToken, abort);
|
|
453
|
+
let entry;
|
|
454
|
+
const promise = this.process(claim.record.id, claim.record.bindingId, claim.record.envelope, claim.fencingToken, abort.signal, permissionDispatchRecoveryFromFailureCode(claim.record.failureCode))
|
|
455
|
+
.catch(() => { })
|
|
456
|
+
.finally(() => {
|
|
457
|
+
stopHeartbeat();
|
|
458
|
+
if (this.active.get(claim.record.id) === entry)
|
|
459
|
+
this.active.delete(claim.record.id);
|
|
460
|
+
});
|
|
461
|
+
entry = { abort, promise };
|
|
462
|
+
this.active.set(claim.record.id, entry);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
async stop() {
|
|
466
|
+
this.stopping = true;
|
|
467
|
+
await abortAndBoundedDrain(this.active, this.options.leaseMs, new Error('assistant-delivery is stopping'));
|
|
468
|
+
}
|
|
469
|
+
/** Abort and drain exact claims that a durable command fence made terminal. */
|
|
470
|
+
async cancelUndispatchedClaims(inboxIds, command) {
|
|
471
|
+
const operations = [...new Set(inboxIds)]
|
|
472
|
+
.map(inboxId => this.active.get(inboxId))
|
|
473
|
+
.filter((entry) => entry !== undefined);
|
|
474
|
+
if (operations.length === 0)
|
|
475
|
+
return;
|
|
476
|
+
const reason = new Error(`assistant-delivery: inbox preparation cancelled by /${command}`);
|
|
477
|
+
for (const operation of operations) {
|
|
478
|
+
if (!operation.abort.signal.aborted)
|
|
479
|
+
operation.abort.abort(reason);
|
|
480
|
+
}
|
|
481
|
+
await Promise.all(operations.map(operation => operation.promise));
|
|
482
|
+
}
|
|
483
|
+
async whenIdle() {
|
|
484
|
+
while (this.active.size > 0)
|
|
485
|
+
await Promise.all([...this.active.values()].map(entry => entry.promise));
|
|
486
|
+
}
|
|
487
|
+
startLeaseHeartbeat(inboxId, fencingToken, abort) {
|
|
488
|
+
let active = true;
|
|
489
|
+
const interval = Math.max(1, Math.floor(this.options.leaseMs / 3));
|
|
490
|
+
const heartbeat = setInterval(() => {
|
|
491
|
+
if (!active || abort.signal.aborted)
|
|
492
|
+
return;
|
|
493
|
+
try {
|
|
494
|
+
if (this.options.store.renewInboxClaim({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
495
|
+
leaseMs: this.options.leaseMs }))
|
|
496
|
+
return;
|
|
497
|
+
abort.abort(new Error('assistant-delivery inbox lease was lost'));
|
|
498
|
+
}
|
|
499
|
+
catch (error) {
|
|
500
|
+
abort.abort(error);
|
|
501
|
+
}
|
|
502
|
+
}, interval);
|
|
503
|
+
heartbeat.unref?.();
|
|
504
|
+
const stop = () => {
|
|
505
|
+
if (!active)
|
|
506
|
+
return;
|
|
507
|
+
active = false;
|
|
508
|
+
clearInterval(heartbeat);
|
|
509
|
+
abort.signal.removeEventListener('abort', stop);
|
|
510
|
+
};
|
|
511
|
+
abort.signal.addEventListener('abort', stop, { once: true });
|
|
512
|
+
return stop;
|
|
513
|
+
}
|
|
514
|
+
async process(inboxId, bindingId, envelope, fencingToken, signal, permissionDispatchRecovery) {
|
|
515
|
+
const binding = bindingId === undefined ? undefined : this.options.store.getBinding(bindingId);
|
|
516
|
+
if (binding?.status === 'revoked') {
|
|
517
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
518
|
+
outcome: 'dead_letter', failureCode: 'binding-revoked' });
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
if (binding === undefined) {
|
|
522
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
523
|
+
outcome: 'retry_wait', failureCode: 'binding-unavailable',
|
|
524
|
+
retryAt: this.now() + this.options.retryBaseMs });
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const processor = this.options.processor();
|
|
528
|
+
if (processor === undefined) {
|
|
529
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
530
|
+
outcome: 'retry_wait', failureCode: 'processor-unavailable', retryAt: this.now() + this.options.retryBaseMs });
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
let prepared;
|
|
534
|
+
if (processor.prepare !== undefined) {
|
|
535
|
+
let preparation;
|
|
536
|
+
try {
|
|
537
|
+
preparation = await processor.prepare(binding, envelope, signal, {
|
|
538
|
+
inboxId,
|
|
539
|
+
ownerId: this.options.ownerId,
|
|
540
|
+
fencingToken,
|
|
541
|
+
...(permissionDispatchRecovery === undefined ? {} : { permissionDispatchRecovery }),
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
catch {
|
|
545
|
+
if (signal.aborted)
|
|
546
|
+
return;
|
|
547
|
+
this.finishInbound(inboxId, fencingToken, {
|
|
548
|
+
outcome: 'not-processed', failureCode: 'prepare-threw', retryable: true,
|
|
549
|
+
});
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
if (signal.aborted)
|
|
553
|
+
return;
|
|
554
|
+
if (preparation.outcome !== 'prepared') {
|
|
555
|
+
this.finishInbound(inboxId, fencingToken, preparation);
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
prepared = preparation.message;
|
|
559
|
+
}
|
|
560
|
+
if (signal.aborted)
|
|
561
|
+
return;
|
|
562
|
+
const currentBinding = this.options.store.getBinding(binding.id);
|
|
563
|
+
if (currentBinding === undefined || currentBinding.status !== 'active') {
|
|
564
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
565
|
+
outcome: 'dead_letter', failureCode: 'binding-revoked' });
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
if (bindingChangedBeforeDispatch(binding, currentBinding, envelope)) {
|
|
569
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
570
|
+
outcome: 'dead_letter', failureCode: 'binding-changed-before-dispatch' });
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
if (!this.options.store.isAuthorizedPrincipal(currentBinding.principal)) {
|
|
574
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
575
|
+
outcome: 'dead_letter', failureCode: 'authorization-revoked-before-dispatch' });
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const explicitDispatch = processor.dispatchControl === 'explicit';
|
|
579
|
+
const dispatchBinding = {
|
|
580
|
+
id: currentBinding.id,
|
|
581
|
+
version: currentBinding.version,
|
|
582
|
+
sessionId: currentBinding.sessionId,
|
|
583
|
+
generation: currentBinding.generation,
|
|
584
|
+
conversation: { ...currentBinding.conversation },
|
|
585
|
+
principal: { ...currentBinding.principal },
|
|
586
|
+
};
|
|
587
|
+
let dispatchMarked = false;
|
|
588
|
+
let dispatchGateFailed = false;
|
|
589
|
+
let dispatchGateBindingFailure;
|
|
590
|
+
let dispatchGateOpen = explicitDispatch;
|
|
591
|
+
const markDispatching = explicitDispatch
|
|
592
|
+
? () => {
|
|
593
|
+
if (!dispatchGateOpen || dispatchGateFailed) {
|
|
594
|
+
throw new Error('assistant-delivery inbound dispatch gate is closed');
|
|
595
|
+
}
|
|
596
|
+
if (signal.aborted)
|
|
597
|
+
throw signal.reason;
|
|
598
|
+
if (dispatchMarked)
|
|
599
|
+
return;
|
|
600
|
+
let latestBinding;
|
|
601
|
+
try {
|
|
602
|
+
latestBinding = this.options.store.getBinding(currentBinding.id);
|
|
603
|
+
}
|
|
604
|
+
catch (error) {
|
|
605
|
+
dispatchGateFailed = true;
|
|
606
|
+
throw error;
|
|
607
|
+
}
|
|
608
|
+
let authorized;
|
|
609
|
+
try {
|
|
610
|
+
authorized = this.options.store.isAuthorizedPrincipal(dispatchBinding.principal);
|
|
611
|
+
}
|
|
612
|
+
catch (error) {
|
|
613
|
+
dispatchGateFailed = true;
|
|
614
|
+
throw error;
|
|
615
|
+
}
|
|
616
|
+
if (!authorized) {
|
|
617
|
+
dispatchGateFailed = true;
|
|
618
|
+
dispatchGateBindingFailure = 'authorization-revoked-before-dispatch';
|
|
619
|
+
throw new Error('assistant-delivery inbound authorization was revoked before dispatch');
|
|
620
|
+
}
|
|
621
|
+
if (latestBinding === undefined || latestBinding.status !== 'active') {
|
|
622
|
+
dispatchGateFailed = true;
|
|
623
|
+
dispatchGateBindingFailure = 'binding-revoked';
|
|
624
|
+
throw new Error('assistant-delivery inbound binding was revoked before dispatch');
|
|
625
|
+
}
|
|
626
|
+
if (bindingChangedBeforeDispatch(currentBinding, latestBinding, envelope)) {
|
|
627
|
+
dispatchGateFailed = true;
|
|
628
|
+
dispatchGateBindingFailure = 'binding-changed-before-dispatch';
|
|
629
|
+
throw new Error('assistant-delivery inbound binding changed before dispatch');
|
|
630
|
+
}
|
|
631
|
+
try {
|
|
632
|
+
this.options.store.markInboxDispatching({
|
|
633
|
+
inboxId, ownerId: this.options.ownerId, fencingToken, binding: dispatchBinding,
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
catch (error) {
|
|
637
|
+
dispatchGateFailed = true;
|
|
638
|
+
dispatchGateBindingFailure = dispatchAuthorizationFailure(error) ?? dispatchGateBindingFailure;
|
|
639
|
+
throw error;
|
|
640
|
+
}
|
|
641
|
+
dispatchMarked = true;
|
|
642
|
+
}
|
|
643
|
+
: undefined;
|
|
644
|
+
if (!explicitDispatch) {
|
|
645
|
+
try {
|
|
646
|
+
this.options.store.markInboxDispatching({
|
|
647
|
+
inboxId, ownerId: this.options.ownerId, fencingToken, binding: dispatchBinding,
|
|
648
|
+
});
|
|
649
|
+
dispatchMarked = true;
|
|
650
|
+
}
|
|
651
|
+
catch (error) {
|
|
652
|
+
const failureCode = dispatchAuthorizationFailure(error);
|
|
653
|
+
if (failureCode !== undefined) {
|
|
654
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
655
|
+
outcome: 'dead_letter', failureCode });
|
|
656
|
+
}
|
|
657
|
+
// No processor call happened, so lease recovery remains the authority on
|
|
658
|
+
// whether a stale or failed marker is safe to retry.
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
let result;
|
|
663
|
+
try {
|
|
664
|
+
result = await processor.process(currentBinding, envelope, signal, prepared, markDispatching);
|
|
665
|
+
}
|
|
666
|
+
catch {
|
|
667
|
+
dispatchGateOpen = false;
|
|
668
|
+
if (signal.aborted)
|
|
669
|
+
return;
|
|
670
|
+
if (explicitDispatch && !dispatchMarked) {
|
|
671
|
+
if (dispatchGateBindingFailure !== undefined) {
|
|
672
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
673
|
+
outcome: 'dead_letter', failureCode: dispatchGateBindingFailure });
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
this.finishInbound(inboxId, fencingToken, {
|
|
677
|
+
outcome: 'not-processed', failureCode: 'processor-threw-before-dispatch', retryable: true,
|
|
678
|
+
});
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
682
|
+
outcome: 'dead_letter', failureCode: 'processor-ambiguous' });
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
dispatchGateOpen = false;
|
|
686
|
+
if (signal.aborted)
|
|
687
|
+
return;
|
|
688
|
+
if (dispatchGateBindingFailure !== undefined) {
|
|
689
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
690
|
+
outcome: 'dead_letter', failureCode: dispatchGateBindingFailure });
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if (dispatchGateFailed) {
|
|
694
|
+
this.finishInbound(inboxId, fencingToken, {
|
|
695
|
+
outcome: 'not-processed', failureCode: 'processor-threw-before-dispatch', retryable: true,
|
|
696
|
+
});
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
if (dispatchMarked && result.outcome !== 'processed') {
|
|
700
|
+
if (result.failureCode === 'agent-task-timeout') {
|
|
701
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
702
|
+
outcome: 'dead_letter', failureCode: 'agent-task-timeout' });
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
if (permissionDispatchRecoveryFromFailureCode(result.failureCode) !== undefined
|
|
706
|
+
|| isFeedbackDispatchRecoveryCode(result.failureCode)
|
|
707
|
+
|| isFeedbackRetryableFailureCode(result.failureCode)
|
|
708
|
+
|| isLearningDispatchRecoveryCode(result.failureCode)
|
|
709
|
+
|| isLearningRetryableFailureCode(result.failureCode)
|
|
710
|
+
|| isWorkflowDispatchRecoveryCode(result.failureCode)) {
|
|
711
|
+
this.finishInbound(inboxId, fencingToken, result);
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
715
|
+
outcome: 'dead_letter', failureCode: 'processor-ambiguous' });
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
this.finishInbound(inboxId, fencingToken, result);
|
|
719
|
+
}
|
|
720
|
+
finishInbound(inboxId, fencingToken, result) {
|
|
721
|
+
if (result.outcome === 'processed') {
|
|
722
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken, outcome: 'processed' });
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
if (result.failureCode === 'agent-task-timeout') {
|
|
726
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
727
|
+
outcome: 'dead_letter', failureCode: 'agent-task-timeout' });
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
if (result.failureCode === 'session-lease-busy'
|
|
731
|
+
&& this.options.store.deferInboxForSessionLease({ inboxId, ownerId: this.options.ownerId,
|
|
732
|
+
fencingToken, retryAt: this.now() + Math.max(this.options.retryBaseMs, result.retryAfterMs ?? 0) }))
|
|
733
|
+
return;
|
|
734
|
+
if (!result.retryable) {
|
|
735
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
736
|
+
outcome: 'dead_letter', failureCode: result.failureCode });
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
const current = this.options.store.getInbox(inboxId);
|
|
740
|
+
const exponential = Math.min(this.options.retryMaxMs, this.options.retryBaseMs * (2 ** Math.max(0, current.attemptCount - 1)) * (1 + this.random()));
|
|
741
|
+
this.options.store.finishInbox({ inboxId, ownerId: this.options.ownerId, fencingToken,
|
|
742
|
+
outcome: 'retry_wait', failureCode: result.failureCode,
|
|
743
|
+
retryAt: this.now() + Math.ceil(Math.max(exponential, result.retryAfterMs ?? 0)) });
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
//# sourceMappingURL=coordinator.js.map
|