@task-master/cadence 0.0.0-stage → 0.1.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/dist/index.js ADDED
@@ -0,0 +1,1882 @@
1
+ // src/config.ts
2
+ var DEFAULTS = {
3
+ /** How long a publisher may hold an outbox entry before reclamation. */
4
+ outboxClaimTtlMs: 6e4,
5
+ /** How long a worker may hold a submission claim before reconciliation resolves it. */
6
+ submissionClaimTtlMs: 6e5,
7
+ /** No observable progress for this long makes a run a candidate for termination. */
8
+ noProgressDeadlineMs: 36e5
9
+ };
10
+ function defaultIntegrationPolicy(overrides = {}) {
11
+ return {
12
+ defaultSince: /* @__PURE__ */ new Date("2024-01-01T00:00:00.000Z"),
13
+ overlapBufferMs: 3e5,
14
+ maxCatchUpWindowMs: 6048e5,
15
+ failureThreshold: 3,
16
+ emptyStreakThreshold: 10,
17
+ backoffBaseMs: 6e4,
18
+ backoffMaxMs: 36e5,
19
+ maxAttempts: 5,
20
+ partialPolicy: "advance_with_repair",
21
+ ...overrides
22
+ };
23
+ }
24
+ function defaultTrafficPolicy(overrides = {}) {
25
+ return {
26
+ version: 1,
27
+ timezone: "America/New_York",
28
+ enforcement: "kickoff_only",
29
+ rules: [],
30
+ minRemainingWindowMs: 9e5,
31
+ resumeJitterMs: 9e5,
32
+ ...overrides
33
+ };
34
+ }
35
+
36
+ // src/db/collections.ts
37
+ import { MongoClient } from "mongodb";
38
+ async function connect(uri, dbName) {
39
+ const client = new MongoClient(uri);
40
+ await client.connect();
41
+ const db = client.db(dbName);
42
+ const c = {
43
+ client,
44
+ db,
45
+ integrations: db.collection("integration"),
46
+ units: db.collection("unit"),
47
+ requests: db.collection("request"),
48
+ runs: db.collection("run"),
49
+ outbox: db.collection("dispatchOutbox"),
50
+ coverage: db.collection("coverage"),
51
+ repairs: db.collection("repairItem")
52
+ };
53
+ await ensureIndexes(c);
54
+ return c;
55
+ }
56
+ async function ensureIndexes(c) {
57
+ await c.units.createIndex(
58
+ { integrationId: 1, adminState: 1, executionState: 1, scheduledNextEligibleAt: 1 },
59
+ { name: "due_units" }
60
+ );
61
+ await c.requests.createIndex(
62
+ { dedupFingerprint: 1 },
63
+ {
64
+ name: "open_request_dedup",
65
+ unique: true,
66
+ partialFilterExpression: { isOpen: true }
67
+ }
68
+ );
69
+ await c.requests.createIndex(
70
+ { integrationId: 1, unitKey: 1, kind: 1, isOpen: 1 },
71
+ { name: "open_by_unit_kind" }
72
+ );
73
+ await c.runs.createIndex({ unitKey: 1, appliedAt: 1 }, { name: "open_runs" });
74
+ await c.outbox.createIndex({ state: 1, nextAttemptAt: 1 }, { name: "publishable" });
75
+ await c.repairs.createIndex({ integrationId: 1, state: 1 }, { name: "open_repairs" });
76
+ await c.repairs.createIndex(
77
+ { unitKey: 1, cause: 1, "interval.from": 1, "interval.to": 1 },
78
+ { name: "open_repair_dedup", unique: true, partialFilterExpression: { state: "open" } }
79
+ );
80
+ }
81
+
82
+ // src/domain/outcomes.ts
83
+ var ProgrammingError = class extends Error {
84
+ };
85
+ var Abort = class extends Error {
86
+ };
87
+ var TERMINAL_OUTCOME_KINDS = [
88
+ "succeeded",
89
+ "succeeded_empty",
90
+ "partial",
91
+ "failed",
92
+ "terminated_confirmed"
93
+ ];
94
+ var TERMINAL = new Set(TERMINAL_OUTCOME_KINDS);
95
+ function assertTerminal(outcome) {
96
+ if (!TERMINAL.has(outcome.kind)) {
97
+ throw new ProgrammingError(`uncertainty_is_not_terminal:${outcome.kind}`);
98
+ }
99
+ }
100
+ function terminalRunState(kind) {
101
+ switch (kind) {
102
+ case "succeeded":
103
+ return "succeeded";
104
+ case "succeeded_empty":
105
+ return "succeeded_empty";
106
+ case "partial":
107
+ return "partial";
108
+ case "failed":
109
+ return "failed";
110
+ case "terminated_confirmed":
111
+ return "terminated_confirmed";
112
+ }
113
+ }
114
+ function requestDispositionFor(outcome, opts) {
115
+ switch (outcome.kind) {
116
+ case "succeeded":
117
+ case "succeeded_empty":
118
+ return { state: "satisfied", isOpen: false };
119
+ case "partial":
120
+ return opts.safe ? { state: "satisfied_with_repair", isOpen: false } : { state: "blocked", isOpen: true };
121
+ case "failed":
122
+ return opts.attemptsRemaining ? { state: "open", isOpen: true } : { state: "failed", isOpen: false };
123
+ case "terminated_confirmed":
124
+ return opts.safe ? { state: "open", isOpen: true } : { state: "blocked", isOpen: true };
125
+ }
126
+ }
127
+ function isRepetitionSafe(cls) {
128
+ return cls === "idempotent_acquisition" || cls === "idempotent_effect";
129
+ }
130
+ function countsTowardQuarantine(cls) {
131
+ return cls !== "throttled";
132
+ }
133
+
134
+ // src/domain/ids.ts
135
+ import { createHash, randomUUID } from "crypto";
136
+ function compactHash(value) {
137
+ return createHash("sha256").update(JSON.stringify(value)).digest("hex").slice(0, 24);
138
+ }
139
+ function scopeHash(scope) {
140
+ return compactHash({
141
+ resourceTypes: [...scope.resourceTypes].sort(),
142
+ typeFilters: [...scope.typeFilters].sort(),
143
+ patients: scope.patients ? [...scope.patients].sort() : null
144
+ });
145
+ }
146
+ function windowFingerprint(window, scope) {
147
+ return compactHash({
148
+ from: window.from.toISOString(),
149
+ to: window.to ? window.to.toISOString() : null,
150
+ domain: window.domain,
151
+ scope: scopeHash(scope)
152
+ });
153
+ }
154
+ function dedupFingerprint(integrationId, unitKey, kind, window, scope) {
155
+ return compactHash({ integrationId, unitKey, kind, w: windowFingerprint(window, scope) });
156
+ }
157
+ function runId(requestId, attemptNumber) {
158
+ return `${requestId}:${attemptNumber}`;
159
+ }
160
+ function newToken() {
161
+ return randomUUID();
162
+ }
163
+
164
+ // src/domain/time.ts
165
+ var FAR_FUTURE = /* @__PURE__ */ new Date("9999-12-31T00:00:00.000Z");
166
+ function addMs(d, ms) {
167
+ return new Date(d.getTime() + ms);
168
+ }
169
+ function maxDate(...ds) {
170
+ let out = null;
171
+ for (const d of ds) if (d && (!out || d > out)) out = d;
172
+ return out;
173
+ }
174
+ function minDate(...ds) {
175
+ let out = null;
176
+ for (const d of ds) if (d && (!out || d < out)) out = d;
177
+ return out;
178
+ }
179
+ function mergeIntervals(ivs) {
180
+ const sorted = [...ivs].filter((iv) => iv.to > iv.from).sort((a, b) => a.from.getTime() - b.from.getTime());
181
+ const out = [];
182
+ for (const iv of sorted) {
183
+ const last = out[out.length - 1];
184
+ if (last && iv.from <= last.to) {
185
+ if (iv.to > last.to) last.to = iv.to;
186
+ } else {
187
+ out.push({ from: iv.from, to: iv.to });
188
+ }
189
+ }
190
+ return out;
191
+ }
192
+ function intersectionMs(ivs, from, to) {
193
+ let total = 0;
194
+ for (const iv of mergeIntervals(ivs)) {
195
+ const s = Math.max(iv.from.getTime(), from.getTime());
196
+ const e = Math.min(iv.to.getTime(), to.getTime());
197
+ if (e > s) total += e - s;
198
+ }
199
+ return total;
200
+ }
201
+ var FORMATTERS = /* @__PURE__ */ new Map();
202
+ function formatter(tz) {
203
+ let f = FORMATTERS.get(tz);
204
+ if (!f) {
205
+ f = new Intl.DateTimeFormat("en-US", {
206
+ timeZone: tz,
207
+ hourCycle: "h23",
208
+ year: "numeric",
209
+ month: "2-digit",
210
+ day: "2-digit",
211
+ hour: "2-digit",
212
+ minute: "2-digit",
213
+ second: "2-digit"
214
+ });
215
+ FORMATTERS.set(tz, f);
216
+ }
217
+ return f;
218
+ }
219
+ function wallPartsAt(instant, tz) {
220
+ const parts = formatter(tz).formatToParts(instant);
221
+ const get = (type) => {
222
+ const p = parts.find((x) => x.type === type);
223
+ if (!p) throw new Error(`missing ${type} for zone ${tz}`);
224
+ return Number(p.value);
225
+ };
226
+ return {
227
+ year: get("year"),
228
+ month: get("month"),
229
+ day: get("day"),
230
+ hour: get("hour") % 24,
231
+ minute: get("minute"),
232
+ second: get("second")
233
+ };
234
+ }
235
+ function asUtcMs(c) {
236
+ return Date.UTC(c.year, c.month - 1, c.day, c.hour, c.minute, c.second ?? 0);
237
+ }
238
+ function zonedToInstant(c, tz, prefer) {
239
+ const naive = asUtcMs(c);
240
+ const guess1 = naive - offsetMsAt(new Date(naive), tz);
241
+ const guess2 = naive - offsetMsAt(new Date(guess1), tz);
242
+ const candidates = Array.from(/* @__PURE__ */ new Set([guess1, guess2])).sort((a, b) => a - b);
243
+ const valid = candidates.filter((t) => wallEquals(new Date(t), c, tz));
244
+ if (valid.length === 0) {
245
+ return new Date(Math.max(...candidates));
246
+ }
247
+ const chosen = prefer === "earlier" ? valid[0] : valid[valid.length - 1];
248
+ return new Date(chosen);
249
+ }
250
+ function offsetMsAt(instant, tz) {
251
+ return asUtcMs(wallPartsAt(instant, tz)) - instant.getTime();
252
+ }
253
+ function wallEquals(instant, c, tz) {
254
+ const w = wallPartsAt(instant, tz);
255
+ return w.year === c.year && w.month === c.month && w.day === c.day && w.hour === c.hour && w.minute === c.minute;
256
+ }
257
+ function parseHhMm(s) {
258
+ const m = /^(\d{2}):(\d{2})$/.exec(s);
259
+ if (!m) throw new Error(`invalid HH:MM: ${s}`);
260
+ return { hour: Number(m[1]), minute: Number(m[2]) };
261
+ }
262
+ function addCivilDays(d, n) {
263
+ const t = new Date(Date.UTC(d.year, d.month - 1, d.day) + n * 864e5);
264
+ return { year: t.getUTCFullYear(), month: t.getUTCMonth() + 1, day: t.getUTCDate() };
265
+ }
266
+ function dayOfWeek(d) {
267
+ return new Date(Date.UTC(d.year, d.month - 1, d.day)).getUTCDay();
268
+ }
269
+ function localDaysBetween(from, to, tz) {
270
+ const start = wallPartsAt(from, tz);
271
+ const end = wallPartsAt(to, tz);
272
+ const out = [];
273
+ let cur = Date.UTC(start.year, start.month - 1, start.day) - 864e5;
274
+ const last = Date.UTC(end.year, end.month - 1, end.day);
275
+ while (cur <= last) {
276
+ const d = new Date(cur);
277
+ out.push({ year: d.getUTCFullYear(), month: d.getUTCMonth() + 1, day: d.getUTCDate() });
278
+ cur += 864e5;
279
+ }
280
+ return out;
281
+ }
282
+
283
+ // src/engine/traffic.ts
284
+ function materialize(policy, from, to) {
285
+ const out = [];
286
+ const days = localDaysBetween(from, to, policy.timezone);
287
+ for (const day of days) {
288
+ for (const rule of policy.rules) {
289
+ if (!rule.days.includes(dayOfWeek(day))) continue;
290
+ const f = parseHhMm(rule.from);
291
+ const t = parseHhMm(rule.to);
292
+ const spansMidnight = t.hour * 60 + t.minute <= f.hour * 60 + f.minute;
293
+ const endDay = spansMidnight ? addCivilDays(day, 1) : day;
294
+ out.push({
295
+ ruleId: rule.id,
296
+ effect: rule.effect,
297
+ appliesTo: rule.appliesTo,
298
+ // Opposite preferences: a denial must not shrink across a DST transition.
299
+ from: zonedToInstant({ ...day, hour: f.hour, minute: f.minute }, policy.timezone, "earlier"),
300
+ to: zonedToInstant({ ...endDay, hour: t.hour, minute: t.minute }, policy.timezone, "later")
301
+ });
302
+ }
303
+ }
304
+ return out.filter((iv) => iv.to > from && iv.from < to).sort((a, b) => a.from.getTime() - b.from.getTime());
305
+ }
306
+ var HORIZON_MS = 14 * 864e5;
307
+ var LOOKBACK_MS = 2 * 864e5;
308
+ function appliesToOp(iv, op) {
309
+ return iv.appliesTo === "all" || iv.appliesTo === op;
310
+ }
311
+ function trafficStateAt(policy, now, op) {
312
+ const open = { denied: false, opensAt: null, remainingMs: Number.POSITIVE_INFINITY };
313
+ if (policy.enforcement === "throughput_only") return open;
314
+ if (policy.enforcement === "kickoff_only" && op !== "kickoff") return open;
315
+ const ivs = materialize(policy, addMs(now, -LOOKBACK_MS), addMs(now, HORIZON_MS)).filter(
316
+ (iv) => appliesToOp(iv, op)
317
+ );
318
+ const denies = ivs.filter((iv) => iv.effect === "deny");
319
+ const allows = ivs.filter((iv) => iv.effect === "allow");
320
+ const covering = (list, at) => list.find((iv) => iv.from <= at && at < iv.to);
321
+ const activeDeny = covering(denies, now);
322
+ if (activeDeny && !covering(allows, now)) {
323
+ return { denied: true, opensAt: endOfDenial(denies, allows, activeDeny), remainingMs: 0 };
324
+ }
325
+ const nextDeny = denies.find((iv) => iv.from > now && !covering(allows, iv.from));
326
+ return {
327
+ denied: false,
328
+ opensAt: null,
329
+ remainingMs: nextDeny ? nextDeny.from.getTime() - now.getTime() : Number.POSITIVE_INFINITY
330
+ };
331
+ }
332
+ function endOfDenial(denies, allows, start) {
333
+ let end = start.to;
334
+ let moved = true;
335
+ while (moved) {
336
+ moved = false;
337
+ for (const iv of denies) {
338
+ if (iv.from <= end && iv.to > end) {
339
+ end = iv.to;
340
+ moved = true;
341
+ }
342
+ }
343
+ }
344
+ const rescuing = allows.find((a) => a.from <= end && end < a.to);
345
+ return rescuing ? rescuing.from : end;
346
+ }
347
+
348
+ // src/engine/admissibility.ts
349
+ var no = (reason, until = null) => ({
350
+ admissible: false,
351
+ reason,
352
+ until
353
+ });
354
+ function activeSuspension(suspensions, now) {
355
+ for (const s of suspensions) {
356
+ if (s.from <= now && (s.until === null || now < s.until)) return s;
357
+ }
358
+ return null;
359
+ }
360
+ function admissibility(unit, integration, now, kind) {
361
+ if (unit.adminState !== "active") return no("admin_state");
362
+ if (integration.vendorCooldownUntil && now < integration.vendorCooldownUntil) {
363
+ return no("vendor_cooldown", integration.vendorCooldownUntil);
364
+ }
365
+ const a = unit.availability;
366
+ if (kind === "scheduled" && a.dispatchFrom && now < a.dispatchFrom) {
367
+ return no("before_dispatch_from", a.dispatchFrom);
368
+ }
369
+ if (a.dispatchUntil && now >= a.dispatchUntil && kind !== "adhoc") {
370
+ const target = a.dataUntil ?? a.dispatchUntil;
371
+ const marker = unit.watermark.scheduledThrough;
372
+ const incomplete = marker === null || marker < target;
373
+ if (!incomplete) return no("unit_complete");
374
+ const mayCatchUp = kind === "scheduled" && a.allowFinalCatchUp;
375
+ if (!mayCatchUp) return no("after_dispatch_until");
376
+ }
377
+ const sus = activeSuspension(a.suspensions, now);
378
+ if (sus && (sus.appliesTo === "all" || kind === "scheduled")) {
379
+ return no("suspended", sus.until);
380
+ }
381
+ const win = trafficStateAt(integration.trafficPolicy, now, "kickoff");
382
+ if (win.denied) return no("vendor_traffic_window", win.opensAt);
383
+ if (integration.trafficPolicy.enforcement === "all_vendor_traffic" && win.remainingMs < integration.trafficPolicy.minRemainingWindowMs) {
384
+ return no("insufficient_remaining_window", null);
385
+ }
386
+ return { admissible: true };
387
+ }
388
+
389
+ // src/engine/window.ts
390
+ function computeWindow(unit, integration, now) {
391
+ const { policy, profile } = integration;
392
+ const a = unit.availability;
393
+ const floor = maxDate(policy.defaultSince, a.dataFrom) ?? policy.defaultSince;
394
+ const base = unit.watermark.seeded && unit.watermark.scheduledThrough ? addMs(unit.watermark.scheduledThrough, -policy.overlapBufferMs) : floor;
395
+ const from = maxDate(base, floor) ?? floor;
396
+ const hardStop = a.dataUntil;
397
+ let to = profile.until ? minDate(now, hardStop) ?? now : null;
398
+ const span = (to ?? now).getTime() - from.getTime();
399
+ const oversized = span > policy.maxCatchUpWindowMs;
400
+ let capped = false;
401
+ let capUnenforceable = false;
402
+ let deferred = null;
403
+ if (oversized) {
404
+ if (profile.until) {
405
+ const cappedTo = addMs(from, policy.maxCatchUpWindowMs);
406
+ deferred = { from: cappedTo, to: to ?? now };
407
+ to = cappedTo;
408
+ capped = true;
409
+ } else {
410
+ capUnenforceable = true;
411
+ }
412
+ }
413
+ return { window: { from, to, domain: "modification_time" }, capped, capUnenforceable, deferred };
414
+ }
415
+
416
+ // src/engine/marker.ts
417
+ function advanceMarker(set, run, unit, outcome, now) {
418
+ if (run.kind !== "scheduled") return;
419
+ if (outcome.kind !== "succeeded" && outcome.kind !== "succeeded_empty") return;
420
+ const basis = outcome.transactionTime ?? run.window.to ?? run.admittedAt;
421
+ const basisLabel = outcome.transactionTime ? "transaction_time" : "client_start_time";
422
+ let boundary = basis;
423
+ if (run.window.to) boundary = minDate(boundary, run.window.to) ?? boundary;
424
+ const dataUntil = run.admissionSnapshot.dataUntil;
425
+ if (dataUntil) boundary = minDate(boundary, dataUntil) ?? boundary;
426
+ const prior = unit.watermark.scheduledThrough;
427
+ if (prior && boundary <= prior) return;
428
+ set["watermark.scheduledThrough"] = boundary;
429
+ set["watermark.basis"] = basisLabel;
430
+ set["watermark.seeded"] = true;
431
+ set["watermark.updatedAt"] = now;
432
+ }
433
+ function isUnitComplete(unit) {
434
+ const a = unit.availability;
435
+ const target = a.dataUntil ?? a.dispatchUntil;
436
+ if (!target) return false;
437
+ const marker = unit.watermark.scheduledThrough;
438
+ return marker !== null && marker >= target;
439
+ }
440
+
441
+ // src/engine/plan.ts
442
+ function grantIsLive(request, againstRunId = request.lastRunId) {
443
+ const g = request.operatorGrant;
444
+ if (!g || g.consumedAt !== null) return false;
445
+ return g.appliesToRunId === againstRunId;
446
+ }
447
+ function attemptBudget(request, againstRunId = request.lastRunId) {
448
+ const extra = grantIsLive(request, againstRunId) ? request.operatorGrant?.extraAttempts ?? 0 : 0;
449
+ return request.maxAttempts + extra;
450
+ }
451
+ async function plan(c, integrationId, now) {
452
+ const integration = await c.integrations.findOne({ _id: integrationId });
453
+ if (!integration) throw new Error(`unknown integration ${integrationId}`);
454
+ const proposals = [];
455
+ const skipped = [];
456
+ const taken = /* @__PURE__ */ new Set();
457
+ const units = await c.units.find({
458
+ integrationId,
459
+ adminState: "active",
460
+ activeRunId: null,
461
+ executionState: { $in: ["idle", "backoff"] },
462
+ scheduledNextEligibleAt: { $lte: now }
463
+ }).toArray();
464
+ for (const unit of units) {
465
+ const adm = admissibility(unit, integration, now, "scheduled");
466
+ if (!adm.admissible) {
467
+ skipped.push({ unitKey: unit._id, reason: adm.reason });
468
+ continue;
469
+ }
470
+ const p = await scheduledProposal(c, unit, integration, now);
471
+ if ("reason" in p) {
472
+ skipped.push({ unitKey: unit._id, reason: p.reason });
473
+ } else {
474
+ proposals.push(p);
475
+ taken.add(unit._id);
476
+ }
477
+ }
478
+ const openRequests = await c.requests.find({
479
+ integrationId,
480
+ kind: { $in: ["adhoc", "repair"] },
481
+ state: "open",
482
+ isOpen: true,
483
+ activeRunId: null,
484
+ nextAttemptAt: { $lte: now }
485
+ }).toArray();
486
+ for (const request of openRequests) {
487
+ if (request.attemptCounter >= attemptBudget(request)) continue;
488
+ if (taken.has(request.unitKey)) {
489
+ skipped.push({ unitKey: request.unitKey, reason: "unit_already_proposed" });
490
+ continue;
491
+ }
492
+ const unit = await c.units.findOne({ _id: request.unitKey, activeRunId: null });
493
+ if (!unit) continue;
494
+ const adm = admissibility(unit, integration, now, request.kind);
495
+ if (!adm.admissible) {
496
+ skipped.push({ unitKey: unit._id, reason: adm.reason });
497
+ continue;
498
+ }
499
+ proposals.push({
500
+ integrationId,
501
+ unitKey: unit._id,
502
+ kind: request.kind,
503
+ window: request.window,
504
+ scope: request.scope,
505
+ reuseRequestId: request._id,
506
+ consumesGrant: grantIsLive(request),
507
+ effectiveBudget: integration.capacity.budget,
508
+ capUnenforceable: false,
509
+ deferred: null,
510
+ repairId: request.repairId
511
+ });
512
+ taken.add(unit._id);
513
+ }
514
+ const workedRepairs = new Set(
515
+ (await c.requests.find({ integrationId, kind: "repair", isOpen: true, repairId: { $ne: null } }).toArray()).map((r) => r.repairId.toHexString())
516
+ );
517
+ const repairs = await c.repairs.find({ integrationId, state: "open" }).toArray();
518
+ for (const repair of repairs) {
519
+ if (workedRepairs.has(repair._id.toHexString())) continue;
520
+ if (taken.has(repair.unitKey)) {
521
+ skipped.push({ unitKey: repair.unitKey, reason: "unit_already_proposed" });
522
+ continue;
523
+ }
524
+ const unit = await c.units.findOne({
525
+ _id: repair.unitKey,
526
+ adminState: "active",
527
+ activeRunId: null
528
+ });
529
+ if (!unit) continue;
530
+ const adm = admissibility(unit, integration, now, "repair");
531
+ if (!adm.admissible) {
532
+ skipped.push({ unitKey: unit._id, reason: adm.reason });
533
+ continue;
534
+ }
535
+ proposals.push({
536
+ integrationId,
537
+ unitKey: unit._id,
538
+ kind: "repair",
539
+ // A repair carries the interval it owes, not a window recomputed against `now`.
540
+ window: { from: repair.interval.from, to: repair.interval.to, domain: "modification_time" },
541
+ scope: repair.scope,
542
+ reuseRequestId: null,
543
+ consumesGrant: false,
544
+ effectiveBudget: integration.capacity.budget,
545
+ capUnenforceable: false,
546
+ deferred: null,
547
+ repairId: repair._id
548
+ });
549
+ taken.add(unit._id);
550
+ }
551
+ return { proposals, skipped };
552
+ }
553
+ async function scheduledProposal(c, unit, integration, now) {
554
+ const existing = await c.requests.findOne({
555
+ integrationId: integration._id,
556
+ unitKey: unit._id,
557
+ kind: "scheduled",
558
+ isOpen: true
559
+ });
560
+ if (existing) {
561
+ if (existing.activeRunId) return { reason: "request_in_flight" };
562
+ const live = grantIsLive(existing);
563
+ if (existing.attemptCounter >= attemptBudget(existing)) {
564
+ return { reason: "attempts_exhausted" };
565
+ }
566
+ if (existing.nextAttemptAt > now && !(live && existing.operatorGrant?.overridesBackoff)) {
567
+ return { reason: "backoff" };
568
+ }
569
+ return {
570
+ integrationId: integration._id,
571
+ unitKey: unit._id,
572
+ kind: "scheduled",
573
+ window: existing.window,
574
+ scope: existing.scope,
575
+ reuseRequestId: existing._id,
576
+ consumesGrant: live,
577
+ effectiveBudget: integration.capacity.budget,
578
+ capUnenforceable: false,
579
+ deferred: null,
580
+ repairId: null
581
+ };
582
+ }
583
+ const wp = computeWindow(unit, integration, now);
584
+ return {
585
+ integrationId: integration._id,
586
+ unitKey: unit._id,
587
+ kind: "scheduled",
588
+ window: wp.window,
589
+ scope: defaultScope(),
590
+ reuseRequestId: null,
591
+ consumesGrant: false,
592
+ effectiveBudget: integration.capacity.budget,
593
+ capUnenforceable: wp.capUnenforceable,
594
+ deferred: wp.deferred,
595
+ repairId: null
596
+ };
597
+ }
598
+ function defaultScope() {
599
+ return {
600
+ resourceTypes: ["Patient", "Observation"],
601
+ typeFilters: [],
602
+ typeFilterHash: "none",
603
+ patients: null
604
+ };
605
+ }
606
+
607
+ // src/engine/admit.ts
608
+ import { ObjectId } from "mongodb";
609
+ async function admit(c, p, now) {
610
+ const session = c.client.startSession();
611
+ let result = { admitted: false, reason: "not_attempted" };
612
+ try {
613
+ await session.withTransaction(async () => {
614
+ const cap = await c.integrations.updateOne(
615
+ {
616
+ _id: p.integrationId,
617
+ $expr: {
618
+ $lt: [
619
+ { $add: ["$capacity.reserved", "$capacity.active"] },
620
+ { $min: ["$capacity.budget", p.effectiveBudget] }
621
+ ]
622
+ }
623
+ },
624
+ { $inc: { "capacity.reserved": 1 } },
625
+ { session }
626
+ );
627
+ if (cap.matchedCount !== 1) throw new Abort("no_capacity");
628
+ const integration = await c.integrations.findOne({ _id: p.integrationId }, { session });
629
+ if (!integration) throw new Abort("unknown_integration");
630
+ const { request, attemptNumber } = p.reuseRequestId ? await claimRequest(c, p, p.reuseRequestId, now, session) : await createRequest(c, p, now, session);
631
+ const runId2 = runId(request._id.toHexString(), attemptNumber);
632
+ await c.requests.updateOne(
633
+ { _id: request._id, activeRunId: null },
634
+ { $set: { activeRunId: runId2, updatedAt: now } },
635
+ { session }
636
+ );
637
+ const unitFilter = {
638
+ _id: p.unitKey,
639
+ adminState: "active",
640
+ activeRunId: null,
641
+ ...p.kind === "scheduled" ? { scheduledNextEligibleAt: { $lte: now } } : {}
642
+ };
643
+ const unit = await c.units.findOneAndUpdate(
644
+ unitFilter,
645
+ { $set: { activeRunId: runId2, executionState: "dispatching" } },
646
+ { session, returnDocument: "before" }
647
+ );
648
+ if (!unit) throw new Abort("unit_not_claimable");
649
+ const adm = admissibility(unit, integration, now, p.kind);
650
+ if (!adm.admissible) throw new Abort(`inadmissible:${adm.reason}`);
651
+ const run = buildRun(runId2, request, unit, integration, p, attemptNumber, now);
652
+ await c.runs.insertOne(run, { session });
653
+ await c.outbox.insertOne(
654
+ {
655
+ _id: new ObjectId(),
656
+ runId: runId2,
657
+ integrationId: p.integrationId,
658
+ payload: { runId: runId2, unitKey: p.unitKey, kind: p.kind },
659
+ state: "pending",
660
+ nextAttemptAt: now,
661
+ claimToken: null,
662
+ claimedBy: null,
663
+ claimedAt: null,
664
+ claimExpiresAt: null
665
+ },
666
+ { session }
667
+ );
668
+ result = { admitted: true, runId: runId2, requestId: request._id, attemptNumber };
669
+ });
670
+ } catch (e) {
671
+ if (e instanceof Abort) return { admitted: false, reason: e.message };
672
+ if (isDuplicateKey(e)) return { admitted: false, reason: "duplicate_open_request" };
673
+ throw e;
674
+ } finally {
675
+ await session.endSession();
676
+ }
677
+ return result;
678
+ }
679
+ async function claimRequest(c, p, requestId, now, session) {
680
+ const budgetExpr = {
681
+ $lt: [
682
+ "$attemptCounter",
683
+ {
684
+ $add: [
685
+ "$maxAttempts",
686
+ {
687
+ $cond: [
688
+ {
689
+ $and: [
690
+ { $eq: [{ $ifNull: ["$operatorGrant.consumedAt", null] }, null] },
691
+ { $eq: [{ $ifNull: ["$operatorGrant.appliesToRunId", null] }, "$lastRunId"] }
692
+ ]
693
+ },
694
+ { $ifNull: ["$operatorGrant.extraAttempts", 0] },
695
+ 0
696
+ ]
697
+ }
698
+ ]
699
+ }
700
+ ]
701
+ };
702
+ const conditions = [{ $expr: budgetExpr }];
703
+ if (p.consumesGrant) {
704
+ conditions.push({ "operatorGrant.consumedAt": null });
705
+ conditions.push({ $expr: { $eq: ["$operatorGrant.appliesToRunId", "$lastRunId"] } });
706
+ } else {
707
+ conditions.push({ nextAttemptAt: { $lte: now } });
708
+ }
709
+ const set = { updatedAt: now };
710
+ if (p.consumesGrant) set["operatorGrant.consumedAt"] = now;
711
+ const claimed = await c.requests.findOneAndUpdate(
712
+ {
713
+ _id: requestId,
714
+ state: "open",
715
+ isOpen: true,
716
+ activeRunId: null,
717
+ $and: conditions
718
+ },
719
+ { $inc: { attemptCounter: 1 }, $set: set },
720
+ { session, returnDocument: "after" }
721
+ );
722
+ if (!claimed) throw new Abort("request_not_claimable");
723
+ return { request: claimed, attemptNumber: claimed.attemptCounter };
724
+ }
725
+ async function createRequest(c, p, now, session) {
726
+ const integration = await c.integrations.findOne({ _id: p.integrationId }, { session });
727
+ if (!integration) throw new Abort("unknown_integration");
728
+ const request = {
729
+ _id: new ObjectId(),
730
+ dedupFingerprint: dedupFingerprint(p.integrationId, p.unitKey, p.kind, p.window, p.scope),
731
+ integrationId: p.integrationId,
732
+ unitKey: p.unitKey,
733
+ kind: p.kind,
734
+ window: p.window,
735
+ scope: p.scope,
736
+ state: "open",
737
+ isOpen: true,
738
+ attemptCounter: 1,
739
+ maxAttempts: integration.policy.maxAttempts,
740
+ nextAttemptAt: now,
741
+ activeRunId: null,
742
+ lastRunId: null,
743
+ operatorGrant: null,
744
+ repairId: p.repairId,
745
+ createdAt: now,
746
+ updatedAt: now
747
+ };
748
+ await c.requests.insertOne(request, { session });
749
+ return { request, attemptNumber: 1 };
750
+ }
751
+ function buildRun(runId2, request, unit, integration, p, attemptNumber, now) {
752
+ return {
753
+ _id: runId2,
754
+ requestId: request._id,
755
+ integrationId: p.integrationId,
756
+ unitKey: p.unitKey,
757
+ kind: p.kind,
758
+ attemptNumber,
759
+ window: p.window,
760
+ scope: p.scope,
761
+ state: "dispatch_pending",
762
+ publication: "pending",
763
+ execution: "not_started",
764
+ vendorSubmission: "none",
765
+ reservation: "held",
766
+ admittedAt: now,
767
+ publishedAt: null,
768
+ taskId: null,
769
+ appliedAt: null,
770
+ completedAt: null,
771
+ outcome: null,
772
+ blocked: null,
773
+ stall: { count: 0, since: null, blockedOps: [], intervals: [] },
774
+ counterSnapshot: {
775
+ consecutiveFailures: unit.counters[p.kind].consecutiveFailures,
776
+ emptyStreak: unit.counters.scheduled.emptyStreak
777
+ },
778
+ // Historical intent at dispatch. `advanceMarker` reads these and the live unit —
779
+ // never a field that nothing writes.
780
+ admissionSnapshot: {
781
+ dispatchUntil: unit.availability.dispatchUntil,
782
+ dataUntil: unit.availability.dataUntil,
783
+ priorMarker: unit.watermark.scheduledThrough,
784
+ trafficPolicyVersion: integration.trafficPolicy.version,
785
+ sideEffectClass: integration.sideEffectClass,
786
+ sideEffectClassVerifiedBy: integration.sideEffectClassVerifiedBy
787
+ },
788
+ executionOwner: null,
789
+ executionToken: null,
790
+ executionClaimExpiresAt: null,
791
+ submissionAttempts: 0,
792
+ vendorJobRef: null,
793
+ pollingLocation: null,
794
+ pollingExpiresAt: null,
795
+ polledAt: null,
796
+ manifest: null,
797
+ orphanedSubmissions: [],
798
+ lastProgressAt: now,
799
+ lastProgressNote: null
800
+ };
801
+ }
802
+ function isDuplicateKey(e) {
803
+ return typeof e === "object" && e !== null && e.code === 11e3;
804
+ }
805
+ function backoffMs(attempt, integration) {
806
+ const base = integration.policy.backoffBaseMs * 2 ** Math.max(0, attempt - 1);
807
+ return Math.min(base, integration.policy.backoffMaxMs);
808
+ }
809
+
810
+ // src/engine/publish.ts
811
+ var ClaimLost = class extends Error {
812
+ };
813
+ var REJECTED = "cadence/enqueue_rejected";
814
+ var UNCERTAIN = "cadence/enqueue_uncertain";
815
+ var EnqueueUncertain = class extends Error {
816
+ cadenceCode = UNCERTAIN;
817
+ };
818
+ var EnqueueRejected = class extends Error {
819
+ cadenceCode = REJECTED;
820
+ };
821
+ function hasCode(e, code) {
822
+ return typeof e === "object" && e !== null && e.cadenceCode === code;
823
+ }
824
+ var isEnqueueRejected = (e) => hasCode(e, REJECTED);
825
+ var isEnqueueUncertain = (e) => hasCode(e, UNCERTAIN);
826
+ async function activateReservation(c, runId2, integrationId, session) {
827
+ const r = await c.runs.updateOne(
828
+ { _id: runId2, reservation: "held" },
829
+ { $set: { reservation: "active" } },
830
+ { session }
831
+ );
832
+ if (r.matchedCount !== 1) return false;
833
+ await c.integrations.updateOne(
834
+ { _id: integrationId },
835
+ { $inc: { "capacity.reserved": -1, "capacity.active": 1 } },
836
+ { session }
837
+ );
838
+ return true;
839
+ }
840
+ async function publishOutbox(c, queue, now, opts) {
841
+ const claimToken = newToken();
842
+ const claim = await c.outbox.findOneAndUpdate(
843
+ {
844
+ state: "pending",
845
+ nextAttemptAt: { $lte: now },
846
+ $or: [{ claimExpiresAt: null }, { claimExpiresAt: { $lte: now } }]
847
+ },
848
+ {
849
+ $set: {
850
+ state: "publishing",
851
+ claimToken,
852
+ claimedBy: opts.instanceId,
853
+ claimedAt: now,
854
+ claimExpiresAt: addMs(now, opts.claimTtlMs)
855
+ }
856
+ },
857
+ { returnDocument: "after" }
858
+ );
859
+ if (!claim) return { published: false, reason: "nothing_pending" };
860
+ let taskId;
861
+ try {
862
+ const res = await queue.enqueue(claim.payload, { idempotencyKey: claim.runId });
863
+ taskId = res.taskId;
864
+ } catch (e) {
865
+ if (isEnqueueRejected(e)) {
866
+ await recordRejection(c, claim, claimToken, e.message);
867
+ return { published: false, reason: "rejected" };
868
+ }
869
+ await recordUncertainty(c, claim, claimToken);
870
+ return { published: false, reason: "uncertain" };
871
+ }
872
+ try {
873
+ await persistPublication(c, claim, claimToken, taskId, now, opts);
874
+ } catch (e) {
875
+ if (e instanceof ClaimLost) return { published: false, reason: "claim_lost" };
876
+ await recordUncertainty(c, claim, claimToken);
877
+ return { published: false, reason: "uncertain" };
878
+ }
879
+ return { published: true, runId: claim.runId, taskId };
880
+ }
881
+ async function persistPublication(c, claim, claimToken, taskId, now, opts) {
882
+ const session = c.client.startSession();
883
+ try {
884
+ await session.withTransaction(async () => {
885
+ const o = await c.outbox.updateOne(
886
+ { _id: claim._id, state: "publishing", claimToken, claimedBy: opts.instanceId },
887
+ { $set: { state: "published" } },
888
+ { session }
889
+ );
890
+ if (o.matchedCount !== 1) throw new ClaimLost("outbox_claim_lost");
891
+ await c.runs.updateOne(
892
+ { _id: claim.runId, publication: "pending" },
893
+ { $set: { publication: "published", publishedAt: now, taskId } },
894
+ { session }
895
+ );
896
+ await c.runs.updateOne(
897
+ { _id: claim.runId, state: "dispatch_pending" },
898
+ { $set: { state: "submitted" } },
899
+ { session }
900
+ );
901
+ await activateReservation(c, claim.runId, claim.integrationId, session);
902
+ });
903
+ } finally {
904
+ await session.endSession();
905
+ }
906
+ }
907
+ async function recordUncertainty(c, claim, claimToken) {
908
+ const o = await c.outbox.updateOne(
909
+ { _id: claim._id, state: "publishing", claimToken },
910
+ { $set: { state: "awaiting_resolution" } }
911
+ );
912
+ if (o.matchedCount !== 1) return;
913
+ await c.runs.updateOne(
914
+ { _id: claim.runId, publication: "pending", state: "dispatch_pending" },
915
+ { $set: { publication: "awaiting_resolution" }, $inc: { submissionAttempts: 1 } }
916
+ );
917
+ }
918
+ async function recordRejection(c, claim, claimToken, message) {
919
+ const o = await c.outbox.updateOne(
920
+ { _id: claim._id, state: "publishing", claimToken },
921
+ { $set: { state: "failed" } }
922
+ );
923
+ if (o.matchedCount !== 1) return;
924
+ await c.runs.updateOne(
925
+ { _id: claim.runId, publication: "pending" },
926
+ { $set: { publication: "failed", outcome: { kind: "failed", failure: { class: "invalid_config", message } } } }
927
+ );
928
+ }
929
+
930
+ // src/engine/stall.ts
931
+ async function enterStall(c, runId2, stallKey, now) {
932
+ await c.runs.updateOne({ _id: runId2 }, { $addToSet: { "stall.blockedOps": stallKey } });
933
+ const opened = await c.runs.findOneAndUpdate(
934
+ { _id: runId2, "stall.since": null },
935
+ { $set: { "stall.since": now }, $inc: { "stall.count": 1 } },
936
+ { returnDocument: "after" }
937
+ );
938
+ return opened !== null;
939
+ }
940
+ async function exitStall(c, runId2, stallKey, now) {
941
+ await c.runs.updateOne({ _id: runId2 }, { $pull: { "stall.blockedOps": stallKey } });
942
+ const closed = await c.runs.findOneAndUpdate(
943
+ { _id: runId2, "stall.blockedOps": { $size: 0 }, "stall.since": { $ne: null } },
944
+ { $set: { "stall.since": null } },
945
+ { returnDocument: "before" }
946
+ );
947
+ if (!closed?.stall.since) return false;
948
+ await c.runs.updateOne(
949
+ { _id: runId2 },
950
+ { $push: { "stall.intervals": { from: closed.stall.since, to: now } } }
951
+ );
952
+ return true;
953
+ }
954
+ function stallIntervals(run, now) {
955
+ const ivs = [...run.stall.intervals];
956
+ if (run.stall.since) ivs.push({ from: run.stall.since, to: now });
957
+ return ivs;
958
+ }
959
+ function eligibleMs(run, from, to) {
960
+ const wall = to.getTime() - from.getTime();
961
+ return Math.max(0, wall - intersectionMs(stallIntervals(run, to), from, to));
962
+ }
963
+ function externalExpiryElapsedMs(from, to) {
964
+ return to.getTime() - from.getTime();
965
+ }
966
+
967
+ // src/engine/worker.ts
968
+ async function beforeVendorContact(c, run, integration, op, now, stallKey = op) {
969
+ const st = trafficStateAt(integration.trafficPolicy, now, op);
970
+ if (st.denied) {
971
+ await enterStall(c, run._id, stallKey, now);
972
+ return { proceed: false, opensAt: st.opensAt };
973
+ }
974
+ await exitStall(c, run._id, stallKey, now);
975
+ return { proceed: true, opensAt: null };
976
+ }
977
+ async function submitKickoff(c, vendor, integration, runId2, now, opts) {
978
+ const run = await c.runs.findOne({ _id: runId2 });
979
+ if (!run) return { status: "not_owner" };
980
+ if (run.vendorSubmission === "confirmed") return { status: "already_confirmed" };
981
+ if (run.vendorSubmission === "unknown") {
982
+ return { status: "escalated", reason: "already_unknown" };
983
+ }
984
+ if (run.vendorSubmission === "claimed" || run.vendorSubmission === "intent_recorded") {
985
+ return recoverSubmission(c, vendor, run, now, opts);
986
+ }
987
+ const token = newToken();
988
+ const claimed = await c.runs.findOneAndUpdate(
989
+ { _id: runId2, vendorSubmission: "none" },
990
+ {
991
+ $set: {
992
+ vendorSubmission: "claimed",
993
+ execution: "running",
994
+ executionOwner: opts.workerId,
995
+ executionToken: token,
996
+ executionClaimExpiresAt: addMs(now, opts.claimTtlMs)
997
+ }
998
+ },
999
+ { returnDocument: "after" }
1000
+ );
1001
+ if (!claimed) return { status: "not_owner" };
1002
+ await activateReservation(c, runId2, run.integrationId);
1003
+ const gate = await beforeVendorContact(
1004
+ c,
1005
+ claimed,
1006
+ integration,
1007
+ "kickoff",
1008
+ now,
1009
+ `kickoff:${opts.workerId}`
1010
+ );
1011
+ if (!gate.proceed) {
1012
+ await c.runs.updateOne(
1013
+ { _id: runId2, executionToken: token, vendorSubmission: "claimed" },
1014
+ {
1015
+ $set: {
1016
+ vendorSubmission: "none",
1017
+ execution: "not_started",
1018
+ executionOwner: null,
1019
+ executionToken: null,
1020
+ executionClaimExpiresAt: null
1021
+ }
1022
+ }
1023
+ );
1024
+ return { status: "deferred", reason: "vendor_traffic_window", retryAt: gate.opensAt };
1025
+ }
1026
+ return contactVendor(c, vendor, runId2, token, now, opts);
1027
+ }
1028
+ async function contactVendor(c, vendor, runId2, token, now, opts) {
1029
+ const intent = await c.runs.updateOne(
1030
+ {
1031
+ _id: runId2,
1032
+ executionToken: token,
1033
+ executionOwner: opts.workerId,
1034
+ vendorSubmission: "claimed",
1035
+ executionClaimExpiresAt: { $gt: now }
1036
+ },
1037
+ { $set: { vendorSubmission: "intent_recorded" }, $inc: { submissionAttempts: 1 } }
1038
+ );
1039
+ if (intent.matchedCount !== 1) {
1040
+ return { status: "claim_lost", contacted: false };
1041
+ }
1042
+ const run = await c.runs.findOne({ _id: runId2 });
1043
+ if (!run) return { status: "claim_lost", contacted: false };
1044
+ const res = await vendor.kickoff(run);
1045
+ const confirmed = await c.runs.updateOne(
1046
+ { _id: runId2, executionToken: token, vendorSubmission: "intent_recorded" },
1047
+ {
1048
+ $set: {
1049
+ vendorSubmission: "confirmed",
1050
+ state: "in_progress",
1051
+ vendorJobRef: res.jobRef,
1052
+ pollingLocation: res.contentLocation,
1053
+ lastProgressAt: now
1054
+ }
1055
+ }
1056
+ );
1057
+ if (confirmed.matchedCount !== 1) {
1058
+ await c.runs.updateOne(
1059
+ { _id: runId2 },
1060
+ { $push: { orphanedSubmissions: { jobRef: res.jobRef, at: now, by: opts.workerId } } }
1061
+ );
1062
+ return { status: "orphaned", jobRef: res.jobRef };
1063
+ }
1064
+ return { status: "submitted", jobRef: res.jobRef };
1065
+ }
1066
+ async function recoverSubmission(c, vendor, run, now, opts) {
1067
+ const mine = run.executionOwner === opts.workerId && run.executionClaimExpiresAt !== null && run.executionClaimExpiresAt > now;
1068
+ if (!mine) return { status: "not_owner" };
1069
+ const safe = isRepetitionSafe(run.admissionSnapshot.sideEffectClass);
1070
+ if (vendor.findByClientRequestId && safe) {
1071
+ const job = await vendor.findByClientRequestId(run._id);
1072
+ if (job) {
1073
+ await c.runs.updateOne(
1074
+ { _id: run._id, executionToken: run.executionToken },
1075
+ {
1076
+ $set: {
1077
+ vendorSubmission: "confirmed",
1078
+ state: "in_progress",
1079
+ vendorJobRef: job.jobRef,
1080
+ pollingLocation: job.contentLocation,
1081
+ lastProgressAt: now
1082
+ }
1083
+ }
1084
+ );
1085
+ return { status: "submitted", jobRef: job.jobRef };
1086
+ }
1087
+ }
1088
+ await markUnknown(c, run._id);
1089
+ return { status: "escalated", reason: "no_vendor_job_lookup" };
1090
+ }
1091
+ async function markUnknown(c, runId2) {
1092
+ await c.runs.updateOne(
1093
+ { _id: runId2, vendorSubmission: { $in: ["claimed", "intent_recorded"] } },
1094
+ { $set: { vendorSubmission: "unknown", execution: "crashed" } }
1095
+ );
1096
+ }
1097
+ async function expireSubmissionClaim(c, runId2, at) {
1098
+ await c.runs.updateOne({ _id: runId2 }, { $set: { executionClaimExpiresAt: at } });
1099
+ }
1100
+
1101
+ // src/engine/availability.ts
1102
+ function deriveHint(unit, integration, now) {
1103
+ const a = unit.availability;
1104
+ let t = unit.scheduledCadenceAt;
1105
+ if (a.dispatchFrom && t < a.dispatchFrom) t = a.dispatchFrom;
1106
+ const sus = activeSuspension(a.suspensions, now);
1107
+ if (sus) t = sus.until ?? FAR_FUTURE;
1108
+ const upcoming = a.suspensions.filter((s) => s.from > now).sort((x, y) => x.from.getTime() - y.from.getTime())[0];
1109
+ if (upcoming && t >= upcoming.from && t < (upcoming.until ?? FAR_FUTURE)) {
1110
+ t = upcoming.until ?? FAR_FUTURE;
1111
+ }
1112
+ if (integration.vendorCooldownUntil) t = maxDate(t, integration.vendorCooldownUntil) ?? t;
1113
+ const at = maxDate(t, now) ?? t;
1114
+ const win = trafficStateAt(integration.trafficPolicy, at, "kickoff");
1115
+ if (win.denied && win.opensAt) t = maxDate(t, win.opensAt) ?? t;
1116
+ return t;
1117
+ }
1118
+ async function refreshEligibility(c, unitKey, now) {
1119
+ const unit = await c.units.findOne({ _id: unitKey });
1120
+ if (!unit) throw new Error(`unknown unit ${unitKey}`);
1121
+ const integration = await c.integrations.findOne({ _id: unit.integrationId });
1122
+ if (!integration) throw new Error(`unknown integration ${unit.integrationId}`);
1123
+ const hint = deriveHint(unit, integration, now);
1124
+ await c.units.updateOne({ _id: unitKey }, { $set: { scheduledNextEligibleAt: hint } });
1125
+ return hint;
1126
+ }
1127
+ async function refreshIntegrationEligibility(c, integrationId, now) {
1128
+ const units = await c.units.find({ integrationId }).project({ _id: 1 }).toArray();
1129
+ for (const u of units) await refreshEligibility(c, u._id, now);
1130
+ }
1131
+ async function patchAvailability(c, unitKey, patch, now) {
1132
+ const set = {};
1133
+ for (const [k, v] of Object.entries(patch)) set[`availability.${k}`] = v;
1134
+ if (Object.keys(set).length > 0) {
1135
+ await c.units.updateOne({ _id: unitKey }, { $set: set });
1136
+ }
1137
+ await refreshEligibility(c, unitKey, now);
1138
+ }
1139
+ async function addSuspension(c, unitKey, suspension, now) {
1140
+ const unit = await c.units.findOne({ _id: unitKey });
1141
+ if (!unit) throw new Error(`unknown unit ${unitKey}`);
1142
+ const overlaps = unit.availability.suspensions.some(
1143
+ (s) => (s.until === null || s.until > suspension.from) && (suspension.until === null || suspension.until > s.from)
1144
+ );
1145
+ if (overlaps) throw new Error("suspension_overlap");
1146
+ await c.units.updateOne({ _id: unitKey }, { $push: { "availability.suspensions": suspension } });
1147
+ await refreshEligibility(c, unitKey, now);
1148
+ }
1149
+ async function removeSuspension(c, unitKey, suspensionId, now) {
1150
+ await c.units.updateOne(
1151
+ { _id: unitKey },
1152
+ { $pull: { "availability.suspensions": { id: suspensionId } } }
1153
+ );
1154
+ await refreshEligibility(c, unitKey, now);
1155
+ }
1156
+ async function setTrafficPolicy(c, integrationId, policy, now) {
1157
+ await c.integrations.updateOne({ _id: integrationId }, { $set: { trafficPolicy: policy } });
1158
+ await refreshIntegrationEligibility(c, integrationId, now);
1159
+ }
1160
+
1161
+ // src/engine/execute.ts
1162
+ async function pollRun(c, vendor, integration, runId2, now, opts) {
1163
+ const run = await c.runs.findOne({ _id: runId2 });
1164
+ if (!run) return { status: "not_pollable" };
1165
+ if (run.appliedAt !== null) return { status: "not_pollable" };
1166
+ if (run.vendorSubmission !== "confirmed" || run.pollingLocation === null) {
1167
+ return { status: "not_pollable" };
1168
+ }
1169
+ if (run.manifest) return { status: "manifest_ready", entries: run.manifest.entries.length };
1170
+ if (run.pollingExpiresAt && now >= run.pollingExpiresAt) {
1171
+ return {
1172
+ status: "terminal",
1173
+ outcome: {
1174
+ kind: "failed",
1175
+ failure: { class: "vendor_error", message: "polling_location_expired" }
1176
+ }
1177
+ };
1178
+ }
1179
+ const gate = await beforeVendorContact(
1180
+ c,
1181
+ run,
1182
+ integration,
1183
+ "poll",
1184
+ now,
1185
+ `poll:${opts.workerId}`
1186
+ );
1187
+ if (!gate.proceed) {
1188
+ return { status: "deferred", reason: "vendor_traffic_window", retryAt: gate.opensAt };
1189
+ }
1190
+ const result = await vendor.poll(run);
1191
+ switch (result.status) {
1192
+ case "accepted": {
1193
+ const set = { polledAt: now };
1194
+ if (result.expiresAt) set["pollingExpiresAt"] = result.expiresAt;
1195
+ if (result.progress !== void 0 && result.progress !== run.lastProgressNote) {
1196
+ set["lastProgressAt"] = now;
1197
+ set["lastProgressNote"] = result.progress;
1198
+ }
1199
+ await c.runs.updateOne({ _id: runId2 }, { $set: set });
1200
+ return { status: "working" };
1201
+ }
1202
+ case "complete": {
1203
+ const entries = result.entries.map((e, i) => ({
1204
+ id: `${i}`,
1205
+ resourceType: e.resourceType,
1206
+ url: e.url,
1207
+ count: e.count,
1208
+ state: "pending",
1209
+ claimedBy: null,
1210
+ claimExpiresAt: null,
1211
+ attempts: 0,
1212
+ bytes: null
1213
+ }));
1214
+ await c.runs.updateOne(
1215
+ { _id: runId2, manifest: null },
1216
+ {
1217
+ $set: {
1218
+ manifest: {
1219
+ transactionTime: result.transactionTime,
1220
+ entries,
1221
+ partial: result.partial ?? null
1222
+ },
1223
+ lastProgressAt: now,
1224
+ polledAt: now
1225
+ }
1226
+ }
1227
+ );
1228
+ return { status: "manifest_ready", entries: entries.length };
1229
+ }
1230
+ case "throttled": {
1231
+ const until = addMs(now, result.retryAfterMs);
1232
+ await c.integrations.updateOne(
1233
+ { _id: integration._id, $or: [{ vendorCooldownUntil: null }, { vendorCooldownUntil: { $lt: until } }] },
1234
+ { $set: { vendorCooldownUntil: until } }
1235
+ );
1236
+ await refreshIntegrationEligibility(c, integration._id, now);
1237
+ return { status: "deferred", reason: "vendor_cooldown", retryAt: until };
1238
+ }
1239
+ case "expired":
1240
+ return {
1241
+ status: "terminal",
1242
+ outcome: {
1243
+ kind: "failed",
1244
+ failure: { class: "vendor_error", message: "polling_location_expired" }
1245
+ }
1246
+ };
1247
+ case "failed":
1248
+ return { status: "terminal", outcome: { kind: "failed", failure: result.failure } };
1249
+ }
1250
+ }
1251
+ async function downloadNext(c, sink, integration, runId2, now, opts) {
1252
+ const run = await c.runs.findOne({ _id: runId2 });
1253
+ if (!run?.manifest || run.appliedAt !== null) return { status: "nothing_claimable" };
1254
+ const gate = await beforeVendorContact(
1255
+ c,
1256
+ run,
1257
+ integration,
1258
+ "download",
1259
+ now,
1260
+ `download:${opts.workerId}`
1261
+ );
1262
+ if (!gate.proceed) {
1263
+ return { status: "deferred", reason: "vendor_traffic_window", retryAt: gate.opensAt };
1264
+ }
1265
+ const owner = `${opts.workerId}:${newToken()}`;
1266
+ const claim = {
1267
+ $set: {
1268
+ "manifest.entries.$.state": "claimed",
1269
+ "manifest.entries.$.claimedBy": owner,
1270
+ "manifest.entries.$.claimExpiresAt": addMs(now, opts.entryClaimTtlMs)
1271
+ },
1272
+ $inc: { "manifest.entries.$.attempts": 1 }
1273
+ };
1274
+ let claimed = await c.runs.findOneAndUpdate(
1275
+ { _id: runId2, "manifest.entries.state": "pending" },
1276
+ claim,
1277
+ { returnDocument: "after" }
1278
+ );
1279
+ if (!claimed) {
1280
+ claimed = await c.runs.findOneAndUpdate(
1281
+ {
1282
+ _id: runId2,
1283
+ "manifest.entries": { $elemMatch: { state: "claimed", claimExpiresAt: { $lte: now } } }
1284
+ },
1285
+ claim,
1286
+ { returnDocument: "after" }
1287
+ );
1288
+ }
1289
+ const entry = claimed?.manifest?.entries.find((e) => e.claimedBy === owner);
1290
+ if (!entry) return { status: "nothing_claimable" };
1291
+ try {
1292
+ const res = await sink.fetch(entry, claimed);
1293
+ await c.runs.updateOne(
1294
+ { _id: runId2, "manifest.entries.claimedBy": owner },
1295
+ {
1296
+ $set: {
1297
+ "manifest.entries.$.state": "done",
1298
+ "manifest.entries.$.bytes": res.bytes,
1299
+ "manifest.entries.$.count": res.count,
1300
+ lastProgressAt: now
1301
+ }
1302
+ }
1303
+ );
1304
+ return { status: "downloaded", entryId: entry.id };
1305
+ } catch (e) {
1306
+ const message = e instanceof Error ? e.message : String(e);
1307
+ await c.runs.updateOne(
1308
+ { _id: runId2, "manifest.entries.claimedBy": owner },
1309
+ { $set: { "manifest.entries.$.state": "failed", lastProgressAt: now } }
1310
+ );
1311
+ return { status: "entry_failed", entryId: entry.id, message };
1312
+ }
1313
+ }
1314
+ function completionOutcome(run) {
1315
+ const manifest = run.manifest;
1316
+ if (!manifest) return null;
1317
+ if (manifest.entries.some((e) => e.state === "pending" || e.state === "claimed")) return null;
1318
+ if (manifest.entries.some((e) => e.state === "failed")) {
1319
+ const failed = manifest.entries.filter((e) => e.state === "failed").length;
1320
+ return {
1321
+ kind: "failed",
1322
+ failure: {
1323
+ class: "transient_transport",
1324
+ message: `${failed}_of_${manifest.entries.length}_files_unretrieved`
1325
+ }
1326
+ };
1327
+ }
1328
+ if (manifest.partial) {
1329
+ return {
1330
+ kind: "partial",
1331
+ transactionTime: manifest.transactionTime,
1332
+ verifiedPortion: { from: run.window.from, to: manifest.partial.verifiedThrough },
1333
+ failedPortion: { from: manifest.partial.failedFrom, to: manifest.partial.failedTo }
1334
+ };
1335
+ }
1336
+ const total = manifest.entries.reduce((n, e) => n + (e.count ?? 0), 0);
1337
+ return total === 0 ? { kind: "succeeded_empty", transactionTime: manifest.transactionTime } : { kind: "succeeded", transactionTime: manifest.transactionTime };
1338
+ }
1339
+ async function outcomeFor(c, runId2) {
1340
+ const run = await c.runs.findOne({ _id: runId2 });
1341
+ return run ? completionOutcome(run) : null;
1342
+ }
1343
+
1344
+ // src/engine/finalize.ts
1345
+ import { ObjectId as ObjectId2 } from "mongodb";
1346
+ async function finalize(c, runId2, outcome, now) {
1347
+ assertTerminal(outcome);
1348
+ const session = c.client.startSession();
1349
+ let result = { applied: false, reason: "not_attempted" };
1350
+ try {
1351
+ await session.withTransaction(async () => {
1352
+ const claimed = await c.runs.findOneAndUpdate(
1353
+ { _id: runId2, appliedAt: null },
1354
+ { $set: { appliedAt: now, blocked: null } },
1355
+ { session, returnDocument: "before" }
1356
+ );
1357
+ if (!claimed) throw new Abort("already_applied");
1358
+ const integration = await c.integrations.findOne({ _id: claimed.integrationId }, { session });
1359
+ if (!integration) throw new Abort("unknown_integration");
1360
+ const unit = await c.units.findOne(
1361
+ { _id: claimed.unitKey, activeRunId: runId2 },
1362
+ { session }
1363
+ );
1364
+ if (!unit) throw new Abort("unit_occupancy_lost");
1365
+ const request = await c.requests.findOne({ _id: claimed.requestId }, { session });
1366
+ if (!request) throw new Abort("unknown_request");
1367
+ const safe = isRepetitionSafe(claimed.admissionSnapshot.sideEffectClass);
1368
+ const attemptsRemaining = request.attemptCounter < attemptBudget(request, runId2);
1369
+ const disp = requestDispositionFor(outcome, { safe, attemptsRemaining });
1370
+ const retryAt = addMs(now, backoffMs(request.attemptCounter, integration));
1371
+ const repairResolution = request.repairId && !disp.isOpen ? disp.state === "failed" ? "accepted" : "resolved" : null;
1372
+ const counters = unit.counters[claimed.kind];
1373
+ const set = {
1374
+ activeRunId: null,
1375
+ executionState: "idle"
1376
+ };
1377
+ switch (outcome.kind) {
1378
+ case "succeeded": {
1379
+ set[`counters.${claimed.kind}.consecutiveFailures`] = 0;
1380
+ set[`counters.${claimed.kind}.emptyStreak`] = 0;
1381
+ set[`counters.${claimed.kind}.totalRuns`] = counters.totalRuns + 1;
1382
+ advanceMarker(set, claimed, unit, outcome, now);
1383
+ break;
1384
+ }
1385
+ case "succeeded_empty": {
1386
+ const streak = counters.emptyStreak + 1;
1387
+ set[`counters.${claimed.kind}.consecutiveFailures`] = 0;
1388
+ set[`counters.${claimed.kind}.emptyStreak`] = streak;
1389
+ set[`counters.${claimed.kind}.totalRuns`] = counters.totalRuns + 1;
1390
+ advanceMarker(set, claimed, unit, outcome, now);
1391
+ if (streak >= integration.policy.emptyStreakThreshold) {
1392
+ set["attention"] = [
1393
+ ...unit.attention.filter((a) => a.reason !== "empty_streak"),
1394
+ { episodeId: new ObjectId2().toHexString(), reason: "empty_streak", since: now }
1395
+ ];
1396
+ }
1397
+ break;
1398
+ }
1399
+ case "partial": {
1400
+ set[`counters.${claimed.kind}.consecutiveFailures`] = 0;
1401
+ set[`counters.${claimed.kind}.totalRuns`] = counters.totalRuns + 1;
1402
+ if (safe && integration.policy.partialPolicy === "advance_with_repair") {
1403
+ const prior = unit.watermark.scheduledThrough;
1404
+ if (claimed.kind === "scheduled" && (!prior || outcome.verifiedPortion.to > prior)) {
1405
+ set["watermark.scheduledThrough"] = outcome.verifiedPortion.to;
1406
+ set["watermark.basis"] = "verified_portion";
1407
+ set["watermark.seeded"] = true;
1408
+ set["watermark.updatedAt"] = now;
1409
+ }
1410
+ await c.repairs.updateOne(
1411
+ {
1412
+ unitKey: claimed.unitKey,
1413
+ cause: "partial_export",
1414
+ state: "open",
1415
+ "interval.from": outcome.failedPortion.from,
1416
+ "interval.to": outcome.failedPortion.to
1417
+ },
1418
+ {
1419
+ $setOnInsert: {
1420
+ _id: new ObjectId2(),
1421
+ integrationId: claimed.integrationId,
1422
+ scope: claimed.scope,
1423
+ createdAt: now
1424
+ }
1425
+ },
1426
+ { upsert: true, session }
1427
+ );
1428
+ } else {
1429
+ set["executionState"] = "blocked_unconfirmed";
1430
+ }
1431
+ break;
1432
+ }
1433
+ case "failed": {
1434
+ const fails = counters.consecutiveFailures + 1;
1435
+ set[`counters.${claimed.kind}.consecutiveFailures`] = fails;
1436
+ set[`counters.${claimed.kind}.totalRuns`] = counters.totalRuns + 1;
1437
+ if (countsTowardQuarantine(outcome.failure.class) && fails >= integration.policy.failureThreshold) {
1438
+ set["executionState"] = "quarantined";
1439
+ set["adminState"] = "paused";
1440
+ } else {
1441
+ set["executionState"] = "backoff";
1442
+ }
1443
+ break;
1444
+ }
1445
+ case "terminated_confirmed": {
1446
+ set[`counters.${claimed.kind}.totalRuns`] = counters.totalRuns + 1;
1447
+ if (!safe) set["executionState"] = "blocked_unconfirmed";
1448
+ break;
1449
+ }
1450
+ }
1451
+ set[pointerField(claimed.kind)] = runId2;
1452
+ if (repairResolution === "accepted") {
1453
+ set["attention"] = [
1454
+ ...unit.attention.filter((a) => a.reason !== "repair_abandoned"),
1455
+ {
1456
+ episodeId: new ObjectId2().toHexString(),
1457
+ reason: "repair_abandoned",
1458
+ since: now,
1459
+ detail: request.repairId.toHexString()
1460
+ }
1461
+ ];
1462
+ }
1463
+ if (claimed.kind === "scheduled") {
1464
+ set["scheduledCadenceAt"] = disp.isOpen ? retryAt : addMs(now, unit.cadenceMs);
1465
+ }
1466
+ const unitUpdate = await c.units.updateOne(
1467
+ { _id: claimed.unitKey, activeRunId: runId2 },
1468
+ { $set: set },
1469
+ { session }
1470
+ );
1471
+ if (unitUpdate.matchedCount !== 1) throw new Abort("unit_update_lost");
1472
+ if (claimed.reservation === "held") {
1473
+ await c.integrations.updateOne(
1474
+ { _id: claimed.integrationId },
1475
+ { $inc: { "capacity.reserved": -1 } },
1476
+ { session }
1477
+ );
1478
+ } else if (claimed.reservation === "active") {
1479
+ await c.integrations.updateOne(
1480
+ { _id: claimed.integrationId },
1481
+ { $inc: { "capacity.active": -1 } },
1482
+ { session }
1483
+ );
1484
+ }
1485
+ const runState = terminalRunState(outcome.kind);
1486
+ await c.runs.updateOne(
1487
+ { _id: runId2 },
1488
+ {
1489
+ $set: {
1490
+ state: runState,
1491
+ completedAt: now,
1492
+ outcome,
1493
+ reservation: "released",
1494
+ execution: "finished"
1495
+ }
1496
+ },
1497
+ { session }
1498
+ );
1499
+ await c.requests.updateOne(
1500
+ { _id: request._id },
1501
+ {
1502
+ $set: {
1503
+ state: disp.state,
1504
+ isOpen: disp.isOpen,
1505
+ // Nulled on close so the partial unique index stops applying and a future
1506
+ // identical window can be requested again.
1507
+ dedupFingerprint: disp.isOpen ? request.dedupFingerprint : null,
1508
+ activeRunId: null,
1509
+ lastRunId: runId2,
1510
+ nextAttemptAt: disp.isOpen ? retryAt : request.nextAttemptAt,
1511
+ updatedAt: now
1512
+ }
1513
+ },
1514
+ { session }
1515
+ );
1516
+ if (repairResolution) {
1517
+ await c.repairs.updateOne(
1518
+ { _id: request.repairId, state: "open" },
1519
+ { $set: { state: repairResolution } },
1520
+ { session }
1521
+ );
1522
+ }
1523
+ if (outcome.kind === "succeeded" || outcome.kind === "partial") {
1524
+ await c.coverage.insertOne(
1525
+ {
1526
+ _id: new ObjectId2(),
1527
+ unitKey: claimed.unitKey,
1528
+ runId: runId2,
1529
+ kind: claimed.kind,
1530
+ interval: {
1531
+ from: claimed.window.from,
1532
+ to: claimed.window.to ?? outcome.transactionTime
1533
+ },
1534
+ scope: claimed.scope
1535
+ },
1536
+ { session }
1537
+ );
1538
+ }
1539
+ result = { applied: true, runState, requestState: disp.state };
1540
+ });
1541
+ } catch (e) {
1542
+ if (e instanceof Abort) return { applied: false, reason: e.message };
1543
+ throw e;
1544
+ } finally {
1545
+ await session.endSession();
1546
+ }
1547
+ if (result.applied) {
1548
+ const run = await c.runs.findOne({ _id: runId2 });
1549
+ const unit = run ? await c.units.findOne({ _id: run.unitKey }) : null;
1550
+ const integration = run ? await c.integrations.findOne({ _id: run.integrationId }) : null;
1551
+ if (unit && integration) {
1552
+ const hint = maxDate(deriveHint(unit, integration, now), now) ?? now;
1553
+ await c.units.updateOne({ _id: unit._id }, { $set: { scheduledNextEligibleAt: hint } });
1554
+ }
1555
+ }
1556
+ return result;
1557
+ }
1558
+ function pointerField(kind) {
1559
+ switch (kind) {
1560
+ case "scheduled":
1561
+ return "lastScheduledRunId";
1562
+ case "adhoc":
1563
+ return "lastAdhocRunId";
1564
+ case "repair":
1565
+ return "lastRepairRunId";
1566
+ }
1567
+ }
1568
+
1569
+ // src/domain/types.ts
1570
+ var TERMINAL_RUN_STATES = [
1571
+ "succeeded",
1572
+ "succeeded_empty",
1573
+ "partial",
1574
+ "failed",
1575
+ "terminated_confirmed"
1576
+ ];
1577
+
1578
+ // src/engine/block.ts
1579
+ async function blockRun(c, runId2, reason, evidenceNeeded, now) {
1580
+ const r = await c.runs.updateOne(
1581
+ { _id: runId2, appliedAt: null, state: { $nin: [...TERMINAL_RUN_STATES] } },
1582
+ { $set: { state: "blocked", blocked: { reason, since: now, evidenceNeeded } } }
1583
+ );
1584
+ if (r.matchedCount !== 1) return false;
1585
+ const run = await c.runs.findOne({ _id: runId2 });
1586
+ if (run) {
1587
+ await c.units.updateOne(
1588
+ { _id: run.unitKey, activeRunId: runId2 },
1589
+ { $set: { executionState: "blocked_unconfirmed" } }
1590
+ );
1591
+ await c.requests.updateOne(
1592
+ { _id: run.requestId, isOpen: true },
1593
+ { $set: { state: "blocked", updatedAt: now } }
1594
+ );
1595
+ }
1596
+ return true;
1597
+ }
1598
+ async function unblockRun(c, runId2, now) {
1599
+ const r = await c.runs.updateOne(
1600
+ { _id: runId2, state: "blocked", appliedAt: null },
1601
+ { $set: { state: "in_progress", blocked: null, lastProgressAt: now } }
1602
+ );
1603
+ if (r.matchedCount !== 1) return false;
1604
+ const run = await c.runs.findOne({ _id: runId2 });
1605
+ if (run) {
1606
+ await c.units.updateOne(
1607
+ { _id: run.unitKey, activeRunId: runId2 },
1608
+ { $set: { executionState: "in_flight" } }
1609
+ );
1610
+ await c.requests.updateOne(
1611
+ { _id: run.requestId, state: "blocked" },
1612
+ { $set: { state: "open", updatedAt: now } }
1613
+ );
1614
+ }
1615
+ return true;
1616
+ }
1617
+
1618
+ // src/engine/attention.ts
1619
+ import { ObjectId as ObjectId3 } from "mongodb";
1620
+ async function raiseAttention(c, unitKey, reason, now, detail) {
1621
+ const episode = {
1622
+ episodeId: new ObjectId3().toHexString(),
1623
+ reason,
1624
+ since: now,
1625
+ ...detail ? { detail } : {}
1626
+ };
1627
+ const r = await c.units.updateOne(
1628
+ { _id: unitKey, "attention.reason": { $ne: reason } },
1629
+ { $push: { attention: episode } }
1630
+ );
1631
+ return r.modifiedCount === 1;
1632
+ }
1633
+ async function clearAttention(c, unitKey, reason) {
1634
+ await c.units.updateOne({ _id: unitKey }, { $pull: { attention: { reason } } });
1635
+ }
1636
+ async function attentionReasons(c, unitKey) {
1637
+ const u = await c.units.findOne({ _id: unitKey });
1638
+ return (u?.attention ?? []).map((a) => a.reason).sort();
1639
+ }
1640
+
1641
+ // src/engine/reconcile.ts
1642
+ var emptyReport = () => ({
1643
+ submissionClaimsReleased: 0,
1644
+ submissionsAdopted: 0,
1645
+ submissionsEscalated: 0,
1646
+ outboxClaimsReclaimed: 0,
1647
+ publicationsResolved: 0,
1648
+ publicationsEscalated: 0,
1649
+ runsTerminated: 0,
1650
+ runsBlocked: 0
1651
+ });
1652
+ async function reconcile(c, now, opts, deps = {}) {
1653
+ const report = emptyReport();
1654
+ await reclaimSubmissionClaims(c, now, deps, report);
1655
+ await reclaimOutboxClaims(c, now, report);
1656
+ await resolvePublications(c, now, opts, deps, report);
1657
+ await resolveStuckRuns(c, now, opts, deps, report);
1658
+ return report;
1659
+ }
1660
+ async function reclaimSubmissionClaims(c, now, deps, report) {
1661
+ const lapsed = await c.runs.find({
1662
+ appliedAt: null,
1663
+ vendorSubmission: { $in: ["claimed", "intent_recorded"] },
1664
+ executionClaimExpiresAt: { $lte: now }
1665
+ }).toArray();
1666
+ for (const run of lapsed) {
1667
+ if (run.vendorSubmission === "claimed") {
1668
+ const r = await c.runs.updateOne(
1669
+ {
1670
+ _id: run._id,
1671
+ vendorSubmission: "claimed",
1672
+ executionClaimExpiresAt: { $lte: now }
1673
+ },
1674
+ {
1675
+ $set: {
1676
+ vendorSubmission: "none",
1677
+ execution: "not_started",
1678
+ executionOwner: null,
1679
+ executionToken: null,
1680
+ executionClaimExpiresAt: null
1681
+ }
1682
+ }
1683
+ );
1684
+ if (r.modifiedCount === 1) report.submissionClaimsReleased += 1;
1685
+ continue;
1686
+ }
1687
+ const safe = isRepetitionSafe(run.admissionSnapshot.sideEffectClass);
1688
+ const lookup = deps.vendor?.findByClientRequestId;
1689
+ if (safe && lookup) {
1690
+ const job = await lookup.call(deps.vendor, run._id);
1691
+ if (job) {
1692
+ const r2 = await c.runs.updateOne(
1693
+ { _id: run._id, vendorSubmission: "intent_recorded" },
1694
+ {
1695
+ $set: {
1696
+ vendorSubmission: "confirmed",
1697
+ state: "in_progress",
1698
+ execution: "running",
1699
+ vendorJobRef: job.jobRef,
1700
+ pollingLocation: job.contentLocation,
1701
+ executionOwner: null,
1702
+ executionToken: null,
1703
+ executionClaimExpiresAt: null,
1704
+ lastProgressAt: now
1705
+ }
1706
+ }
1707
+ );
1708
+ if (r2.modifiedCount === 1) report.submissionsAdopted += 1;
1709
+ continue;
1710
+ }
1711
+ const r = await c.runs.updateOne(
1712
+ { _id: run._id, vendorSubmission: "intent_recorded" },
1713
+ {
1714
+ $set: {
1715
+ vendorSubmission: "none",
1716
+ execution: "not_started",
1717
+ executionOwner: null,
1718
+ executionToken: null,
1719
+ executionClaimExpiresAt: null
1720
+ }
1721
+ }
1722
+ );
1723
+ if (r.modifiedCount === 1) report.submissionClaimsReleased += 1;
1724
+ continue;
1725
+ }
1726
+ await markUnknown(c, run._id);
1727
+ await blockRun(c, run._id, "unresolved_submission", "vendor job lookup or operator check", now);
1728
+ await raiseAttention(c, run.unitKey, "unresolved_submission", now, run._id);
1729
+ report.submissionsEscalated += 1;
1730
+ }
1731
+ }
1732
+ async function reclaimOutboxClaims(c, now, report) {
1733
+ const stale = await c.outbox.find({ state: "publishing", claimExpiresAt: { $lte: now } }).toArray();
1734
+ for (const entry of stale) {
1735
+ const r = await c.outbox.updateOne(
1736
+ { _id: entry._id, state: "publishing", claimToken: entry.claimToken },
1737
+ {
1738
+ $set: {
1739
+ state: "pending",
1740
+ nextAttemptAt: now,
1741
+ claimToken: null,
1742
+ claimedBy: null,
1743
+ claimedAt: null,
1744
+ claimExpiresAt: null
1745
+ }
1746
+ }
1747
+ );
1748
+ if (r.modifiedCount === 1) report.outboxClaimsReclaimed += 1;
1749
+ }
1750
+ }
1751
+ async function resolvePublications(c, now, opts, deps, report) {
1752
+ const pending = await c.outbox.find({ state: "awaiting_resolution" }).toArray();
1753
+ const lookup = deps.queue?.findByIdempotencyKey;
1754
+ for (const entry of pending) {
1755
+ if (lookup) {
1756
+ const task = await lookup.call(deps.queue, entry.runId);
1757
+ if (task) {
1758
+ await completePublication(c, entry, task.taskId, now);
1759
+ report.publicationsResolved += 1;
1760
+ } else {
1761
+ const r = await c.outbox.updateOne(
1762
+ { _id: entry._id, state: "awaiting_resolution" },
1763
+ { $set: { state: "pending", nextAttemptAt: now } }
1764
+ );
1765
+ if (r.modifiedCount === 1) report.publicationsResolved += 1;
1766
+ }
1767
+ continue;
1768
+ }
1769
+ const run = await c.runs.findOne({ _id: entry.runId });
1770
+ if (!run) continue;
1771
+ if (now.getTime() - run.admittedAt.getTime() < opts.publicationResolutionGraceMs) continue;
1772
+ await blockRun(c, run._id, "needs_investigation", "queue state for this run", now);
1773
+ await raiseAttention(c, run.unitKey, "unresolved_publication", now, run._id);
1774
+ report.publicationsEscalated += 1;
1775
+ }
1776
+ }
1777
+ async function completePublication(c, entry, taskId, now) {
1778
+ const session = c.client.startSession();
1779
+ try {
1780
+ await session.withTransaction(async () => {
1781
+ const o = await c.outbox.updateOne(
1782
+ { _id: entry._id, state: "awaiting_resolution" },
1783
+ { $set: { state: "published" } },
1784
+ { session }
1785
+ );
1786
+ if (o.modifiedCount !== 1) return;
1787
+ await c.runs.updateOne(
1788
+ { _id: entry.runId, publication: { $in: ["pending", "awaiting_resolution"] } },
1789
+ { $set: { publication: "published", publishedAt: now, taskId } },
1790
+ { session }
1791
+ );
1792
+ await c.runs.updateOne(
1793
+ { _id: entry.runId, state: "dispatch_pending" },
1794
+ { $set: { state: "submitted" } },
1795
+ { session }
1796
+ );
1797
+ await activateReservation(c, entry.runId, entry.integrationId, session);
1798
+ });
1799
+ } finally {
1800
+ await session.endSession();
1801
+ }
1802
+ }
1803
+ async function resolveStuckRuns(c, now, opts, deps, report) {
1804
+ const live = await c.runs.find({ appliedAt: null, state: { $in: ["submitted", "in_progress"] } }).toArray();
1805
+ for (const run of live) {
1806
+ const expired = run.pollingExpiresAt !== null && externalExpiryElapsedMs(run.pollingExpiresAt, now) >= 0;
1807
+ const idle = eligibleMs(run, run.lastProgressAt, now) >= opts.noProgressDeadlineMs;
1808
+ if (!expired && !idle) continue;
1809
+ const integration = await c.integrations.findOne({ _id: run.integrationId });
1810
+ if (!integration) continue;
1811
+ const cancelled = await confirmCancellation(run, integration, deps);
1812
+ if (cancelled) {
1813
+ await finalize(c, run._id, { kind: "terminated_confirmed" }, now);
1814
+ report.runsTerminated += 1;
1815
+ } else {
1816
+ await blockRun(c, run._id, "unconfirmed_termination", "vendor job status", now);
1817
+ await raiseAttention(c, run.unitKey, "no_progress", now, run._id);
1818
+ report.runsBlocked += 1;
1819
+ }
1820
+ }
1821
+ }
1822
+ async function confirmCancellation(run, integration, deps) {
1823
+ const cancel = deps.vendor?.cancel;
1824
+ if (!cancel) return false;
1825
+ if (!isRepetitionSafe(integration.sideEffectClass)) return false;
1826
+ return cancel.call(deps.vendor, run);
1827
+ }
1828
+ function nextSweepAt(now, intervalMs) {
1829
+ return addMs(now, intervalMs);
1830
+ }
1831
+ export {
1832
+ DEFAULTS,
1833
+ EnqueueRejected,
1834
+ EnqueueUncertain,
1835
+ ProgrammingError,
1836
+ TERMINAL_OUTCOME_KINDS,
1837
+ activateReservation,
1838
+ addSuspension,
1839
+ admissibility,
1840
+ admit,
1841
+ assertTerminal,
1842
+ attemptBudget,
1843
+ attentionReasons,
1844
+ backoffMs,
1845
+ blockRun,
1846
+ clearAttention,
1847
+ completionOutcome,
1848
+ computeWindow,
1849
+ connect,
1850
+ countsTowardQuarantine,
1851
+ defaultIntegrationPolicy,
1852
+ defaultTrafficPolicy,
1853
+ downloadNext,
1854
+ ensureIndexes,
1855
+ expireSubmissionClaim,
1856
+ finalize,
1857
+ grantIsLive,
1858
+ isEnqueueRejected,
1859
+ isEnqueueUncertain,
1860
+ isRepetitionSafe,
1861
+ isUnitComplete,
1862
+ markUnknown,
1863
+ nextSweepAt,
1864
+ outcomeFor,
1865
+ patchAvailability,
1866
+ plan,
1867
+ pollRun,
1868
+ publishOutbox,
1869
+ raiseAttention,
1870
+ reconcile,
1871
+ refreshEligibility,
1872
+ refreshIntegrationEligibility,
1873
+ removeSuspension,
1874
+ requestDispositionFor,
1875
+ runId,
1876
+ setTrafficPolicy,
1877
+ submitKickoff,
1878
+ terminalRunState,
1879
+ trafficStateAt,
1880
+ unblockRun
1881
+ };
1882
+ //# sourceMappingURL=index.js.map