@proofoftech/flowsafe 0.22.0 → 0.24.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/CHANGELOG.md +160 -0
- package/README.md +77 -12
- package/dist/agent-host/catalog.d.ts.map +1 -1
- package/dist/agent-host/catalog.js +5 -2
- package/dist/agent-host/catalog.js.map +1 -1
- package/dist/agent-host/thread-host.d.ts +16 -0
- package/dist/agent-host/thread-host.d.ts.map +1 -1
- package/dist/agent-host/thread-host.js +81 -27
- package/dist/agent-host/thread-host.js.map +1 -1
- package/dist/agent-runner/durable-agent-runner.d.ts +121 -80
- package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
- package/dist/agent-runner/durable-agent-runner.js +747 -468
- package/dist/agent-runner/durable-agent-runner.js.map +1 -1
- package/dist/approval-ui/use-approval-dashboard.js +6 -6
- package/dist/approval-ui/use-approval-dashboard.js.map +1 -1
- package/dist/background-tasks/d1-storage.d.ts.map +1 -1
- package/dist/background-tasks/d1-storage.js +2 -3
- package/dist/background-tasks/d1-storage.js.map +1 -1
- package/dist/background-tasks/host.d.ts.map +1 -1
- package/dist/background-tasks/host.js +27 -38
- package/dist/background-tasks/host.js.map +1 -1
- package/dist/do-runner/cause-chain.d.ts +5 -0
- package/dist/do-runner/cause-chain.d.ts.map +1 -1
- package/dist/do-runner/cause-chain.js +12 -0
- package/dist/do-runner/cause-chain.js.map +1 -1
- package/dist/do-runner/cf-types.d.ts +3 -0
- package/dist/do-runner/cf-types.d.ts.map +1 -1
- package/dist/do-runner/cf-types.js.map +1 -1
- package/dist/do-runner/constants.d.ts +1 -1
- package/dist/do-runner/constants.d.ts.map +1 -1
- package/dist/do-runner/constants.js +1 -1
- package/dist/do-runner/constants.js.map +1 -1
- package/dist/do-runner/core-events-nonce.d.ts +2 -0
- package/dist/do-runner/core-events-nonce.d.ts.map +1 -0
- package/dist/do-runner/core-events-nonce.js +16 -0
- package/dist/do-runner/core-events-nonce.js.map +1 -0
- package/dist/do-runner/d1-storage.js +1 -9
- package/dist/do-runner/d1-storage.js.map +1 -1
- package/dist/do-runner/do-error-response.d.ts.map +1 -1
- package/dist/do-runner/do-error-response.js +2 -1
- package/dist/do-runner/do-error-response.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts +17 -2
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +442 -55
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/fenced-workflow-capability.d.ts +24 -0
- package/dist/do-runner/fenced-workflow-capability.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.d.ts +5 -1
- package/dist/do-runner/fenced-workflows-d1.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.js +149 -48
- package/dist/do-runner/fenced-workflows-d1.js.map +1 -1
- package/dist/do-runner/index.d.ts +2 -2
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +2 -2
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/init.d.ts +2 -3
- package/dist/do-runner/init.d.ts.map +1 -1
- package/dist/do-runner/init.js.map +1 -1
- package/dist/do-runner/memory-id.js +1 -1
- package/dist/do-runner/memory-id.js.map +1 -1
- package/dist/do-runner/pubsub.d.ts +6 -2
- package/dist/do-runner/pubsub.d.ts.map +1 -1
- package/dist/do-runner/pubsub.js +12 -22
- package/dist/do-runner/pubsub.js.map +1 -1
- package/dist/do-runner/run-lifecycle.d.ts +42 -2
- package/dist/do-runner/run-lifecycle.d.ts.map +1 -1
- package/dist/do-runner/run-lifecycle.js +57 -1
- package/dist/do-runner/run-lifecycle.js.map +1 -1
- package/dist/do-runner/run-terminal-state.d.ts +2 -0
- package/dist/do-runner/run-terminal-state.d.ts.map +1 -1
- package/dist/do-runner/run-terminal-state.js +4 -0
- package/dist/do-runner/run-terminal-state.js.map +1 -1
- package/dist/do-runner/runtime.d.ts +91 -58
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +351 -191
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/suspension-deadline.d.ts +25 -17
- package/dist/do-runner/suspension-deadline.d.ts.map +1 -1
- package/dist/do-runner/suspension-deadline.js +44 -21
- package/dist/do-runner/suspension-deadline.js.map +1 -1
- package/dist/do-runner/workflow-snapshot-row.d.ts +7 -0
- package/dist/do-runner/workflow-snapshot-row.d.ts.map +1 -1
- package/dist/do-runner/workflow-snapshot-row.js +21 -0
- package/dist/do-runner/workflow-snapshot-row.js.map +1 -1
- package/dist/host-kit/approval-bridge.d.ts +11 -8
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +15 -9
- package/dist/host-kit/approval-bridge.js.map +1 -1
- package/dist/host-kit/do-response.d.ts.map +1 -1
- package/dist/host-kit/do-response.js +7 -1
- package/dist/host-kit/do-response.js.map +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +2 -1
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +4 -2
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +3 -2
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/schedules/schedules-d1.d.ts +1 -0
- package/dist/schedules/schedules-d1.d.ts.map +1 -1
- package/dist/schedules/schedules-d1.js +35 -22
- package/dist/schedules/schedules-d1.js.map +1 -1
- package/dist/schedules/tick.d.ts +17 -2
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +63 -44
- package/dist/schedules/tick.js.map +1 -1
- package/dist/signals/client.d.ts +7 -4
- package/dist/signals/client.d.ts.map +1 -1
- package/dist/signals/client.js +9 -7
- package/dist/signals/client.js.map +1 -1
- package/dist/signals/d1-shared.js +1 -1
- package/dist/signals/d1-shared.js.map +1 -1
- package/dist/signals/index.d.ts +1 -1
- package/dist/signals/index.d.ts.map +1 -1
- package/dist/signals/index.js.map +1 -1
- package/dist/signals/thread-do-routes.d.ts +56 -10
- package/dist/signals/thread-do-routes.d.ts.map +1 -1
- package/dist/signals/thread-do-routes.js +434 -247
- package/dist/signals/thread-do-routes.js.map +1 -1
- package/package.json +18 -9
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
// enforced by RunnerRuntime's per-run FIFO locks; routing one DO instance per run
|
|
9
9
|
// (idFromName(`${workflowId}:${runId}`)) makes those locks authoritative,
|
|
10
10
|
// since all traffic for a run lands on one instance. The object's alarm is
|
|
11
|
-
// reserved for
|
|
12
|
-
//
|
|
13
|
-
//
|
|
11
|
+
// reserved for settling a run whose execution leg stopped mid-step, for
|
|
12
|
+
// recovering run-owner claims that outlive an interrupted start, and for the
|
|
13
|
+
// per-suspension deadlines of this run (suspension-deadline.ts); the duties
|
|
14
|
+
// share one wake, armed at the earliest due time.
|
|
14
15
|
import { decodeExecutionPrincipal, isExecutionPrincipalId, isExecutionPrincipalKind, } from '../approval-api/principal.js';
|
|
16
|
+
import { errorMessageOf } from './cause-chain.js';
|
|
15
17
|
import { newWebSocketPair, safeSend } from './cf-types.js';
|
|
16
18
|
import { isDatabaseBinding, verifyDurableObjectDeploymentIdentity, verifyDurableObjectDeploymentRequest, } from './deployment-identity.js';
|
|
17
19
|
import { DoStatusError, doErrorResponse } from './do-error-response.js';
|
|
@@ -21,11 +23,11 @@ import { EXECUTION_PRINCIPAL_HEADER } from './execution-principal-header.js';
|
|
|
21
23
|
import { isDefinitiveInitialAdmissionRefusal } from './initial-admission-refusal.js';
|
|
22
24
|
import { isPathSafeId } from './path-safe-id.js';
|
|
23
25
|
import { lifecycleFromRequestContext, terminalCleanupFor, } from './run-lifecycle.js';
|
|
24
|
-
import { isTerminalRunStatus } from './run-terminal-state.js';
|
|
25
|
-
import { InvalidRunRequestError, RunAlreadyExistsError, RunnerRuntime, RunStateUnreadableError, UnknownRunError, } from './runtime.js';
|
|
26
|
+
import { isDurableRunStatus, isTerminalRunStatus, } from './run-terminal-state.js';
|
|
27
|
+
import { InvalidRunRequestError, RUN_LEG_TOUCH_MS, RunAlreadyExistsError, RunnerRuntime, RunStateUnreadableError, UnknownRunError, } from './runtime.js';
|
|
26
28
|
import { resolveScheduleStartOwner, } from './schedule-source.js';
|
|
27
29
|
import { captureReservation, sameReservationIdentity, } from './start-reservation-contract.js';
|
|
28
|
-
import { dueSuspensionDeadline, isReadableRunSummary, isSuspensionDeadlineDue, MAX_SUSPENSION_DEADLINE_ATTEMPTS, MAX_SUSPENSION_DEADLINES_PER_RUN, mergeSuspensionDeadlines, nextSuspensionDeadlineAt, parseSuspensionDeadlineRecord, SUSPENSION_DEADLINE_PRINCIPAL_ID, SUSPENSION_DEADLINE_STORAGE_KEY, SUSPENSION_TIMEOUT_RESUME_KEY, suspensionDeadlinesOf, suspensionTimeoutResumeData, tombstoned, } from './suspension-deadline.js';
|
|
30
|
+
import { dueSuspensionDeadline, isReadableRunSummary, isSuspensionDeadlineDue, MAX_SUSPENSION_DEADLINE_ATTEMPTS, MAX_SUSPENSION_DEADLINES_PER_RUN, mergeSuspensionDeadlines, nextSuspensionDeadlineAt, ownSuspensionDeadlines, parseSuspensionDeadlineRecord, SUSPENSION_DEADLINE_PRINCIPAL_ID, SUSPENSION_DEADLINE_STORAGE_KEY, SUSPENSION_TIMEOUT_RESUME_KEY, suspensionDeadlinesOf, suspensionTimeoutResumeData, suspensionTimerSteps, tombstoned, } from './suspension-deadline.js';
|
|
29
31
|
const RUN_OWNER_RECOVERY_KEY = 'flowsafe:run-owner-recovery:v1';
|
|
30
32
|
export const RUN_OWNER_RECOVERY_DELAY_MS = 60_000;
|
|
31
33
|
const SUSPENSION_DEADLINE_RETRY_MS = 60_000;
|
|
@@ -59,6 +61,20 @@ const SUSPENSION_DEADLINE_UNREADABLE_LIMIT_MS = 86_400_000;
|
|
|
59
61
|
// deferred to the watchdog — up to a minute late, and only while the fault
|
|
60
62
|
// lasts.
|
|
61
63
|
const SUSPENSION_DEADLINE_ARM_FLOOR_MS = 1_000;
|
|
64
|
+
// The marker of the execution leg this object is driving. It outlives an
|
|
65
|
+
// isolate that dies mid-step, which is how the next wake learns that a run
|
|
66
|
+
// left `running` has nothing driving it.
|
|
67
|
+
const RUN_LEG_KEY = 'flowsafe:run-leg:v1';
|
|
68
|
+
// Far past the 15 minutes a pending operation keeps a Durable Object running
|
|
69
|
+
// without a connected client. A leg frame this old belongs to an invocation
|
|
70
|
+
// that ended without settling the leg's promise — which still holds this
|
|
71
|
+
// object's locks — or to a leg whose client has stayed connected all along.
|
|
72
|
+
// Resetting the object bounds the first and ends the second.
|
|
73
|
+
const RUN_LEG_MAX_AGE_MS = 7_200_000;
|
|
74
|
+
// The bound on a marker whose run cannot be settled, for instance because a
|
|
75
|
+
// deploy unregistered its workflow: the bound the suspension-deadline duty
|
|
76
|
+
// allows an unreadable run.
|
|
77
|
+
const RUN_LEG_UNSETTLED_LIMIT_MS = SUSPENSION_DEADLINE_UNREADABLE_LIMIT_MS;
|
|
62
78
|
function recoveryObject(value) {
|
|
63
79
|
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
64
80
|
throw new Error('stored run owner recovery is malformed');
|
|
@@ -70,6 +86,32 @@ function recoveryObject(value) {
|
|
|
70
86
|
}
|
|
71
87
|
return copy;
|
|
72
88
|
}
|
|
89
|
+
function runLegMarker(value) {
|
|
90
|
+
let stored;
|
|
91
|
+
try {
|
|
92
|
+
stored = recoveryObject(value);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
throw new Error('stored run leg marker is malformed');
|
|
96
|
+
}
|
|
97
|
+
if (stored.version !== 1 ||
|
|
98
|
+
!isPathSafeId(stored.workflowId) ||
|
|
99
|
+
!isPathSafeId(stored.runId) ||
|
|
100
|
+
!isPathSafeId(stored.token) ||
|
|
101
|
+
(stored.trigger !== 'start' &&
|
|
102
|
+
stored.trigger !== 'resume' &&
|
|
103
|
+
stored.trigger !== 'deadline') ||
|
|
104
|
+
!Number.isSafeInteger(stored.startedAt))
|
|
105
|
+
throw new Error('stored run leg marker is malformed');
|
|
106
|
+
return {
|
|
107
|
+
version: 1,
|
|
108
|
+
workflowId: stored.workflowId,
|
|
109
|
+
runId: stored.runId,
|
|
110
|
+
token: stored.token,
|
|
111
|
+
trigger: stored.trigger,
|
|
112
|
+
startedAt: stored.startedAt,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
73
115
|
function sameRunRecovery(actual, expected) {
|
|
74
116
|
const claim = actual.startReservation;
|
|
75
117
|
const wantedClaim = expected.startReservation;
|
|
@@ -150,6 +192,8 @@ export class DurableObjectRunner {
|
|
|
150
192
|
#reportedSuspensionRejections = new Set();
|
|
151
193
|
/** Persisted claims outlive an isolate and cannot establish run liveness. */
|
|
152
194
|
#startsInFlight = new Map();
|
|
195
|
+
/** Execution legs running in this isolate. */
|
|
196
|
+
#legs = new Set();
|
|
153
197
|
constructor(state, env) {
|
|
154
198
|
this.state = state;
|
|
155
199
|
this.env = env;
|
|
@@ -158,7 +202,11 @@ export class DurableObjectRunner {
|
|
|
158
202
|
scheduleSource(_env) {
|
|
159
203
|
return undefined;
|
|
160
204
|
}
|
|
161
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* Required when the host exposes terminate or deadline lifecycle routes.
|
|
207
|
+
* Its optional `reconcileApprovals` files the approvals the object reaches
|
|
208
|
+
* from its alarm.
|
|
209
|
+
*/
|
|
162
210
|
runLifecycle(_env) {
|
|
163
211
|
return undefined;
|
|
164
212
|
}
|
|
@@ -205,11 +253,14 @@ export class DurableObjectRunner {
|
|
|
205
253
|
// populated for idFromName-created ids; it is absent under node tests
|
|
206
254
|
// (state undefined / minimal stubs), where the runtime's own validation
|
|
207
255
|
// still applies.
|
|
256
|
+
/** Does `workflowId:runId` name this object's run (or does it carry no name)? */
|
|
257
|
+
#isOwnRun(workflowId, runId) {
|
|
258
|
+
const name = this.state?.id?.name;
|
|
259
|
+
return name === undefined || name === `${workflowId}:${runId}`;
|
|
260
|
+
}
|
|
208
261
|
#assertRunIdentity(workflowId, runId) {
|
|
209
262
|
const name = this.state?.id?.name;
|
|
210
|
-
if (
|
|
211
|
-
return;
|
|
212
|
-
if (name !== `${workflowId}:${runId}`) {
|
|
263
|
+
if (!this.#isOwnRun(workflowId, runId)) {
|
|
213
264
|
throw new Error(`DO identity mismatch: instance is '${name}' but the request names '${workflowId}:${runId}' — refusing`);
|
|
214
265
|
}
|
|
215
266
|
}
|
|
@@ -416,8 +467,9 @@ export class DurableObjectRunner {
|
|
|
416
467
|
}
|
|
417
468
|
}
|
|
418
469
|
/**
|
|
419
|
-
* The arming path for this object's one alarm, which
|
|
420
|
-
* share: run-owner recovery and
|
|
470
|
+
* The arming path for this object's one alarm, which independent duties
|
|
471
|
+
* share: run-owner recovery and leg settlement (one cadence, kept while
|
|
472
|
+
* either record exists) and the run's suspension deadlines. Every arm
|
|
421
473
|
* that reflects CONVERGED state routes through here, or through the
|
|
422
474
|
* #armAlarmFor it delegates to, because an alarm set for one duty from a
|
|
423
475
|
* site that cannot see the other would silently drop the other's wake — a
|
|
@@ -448,8 +500,11 @@ export class DurableObjectRunner {
|
|
|
448
500
|
throw new Error('run owner recovery and suspension deadlines require Durable Object alarms');
|
|
449
501
|
}
|
|
450
502
|
const suspensionDueAt = nextSuspensionDeadlineAt(stored);
|
|
503
|
+
// A leg marker keeps the recovery cadence: it is the wake that finds a
|
|
504
|
+
// leg which stopped without reaching a durable state.
|
|
451
505
|
const recoveryDue = recoveryDueAt ??
|
|
452
|
-
((await storage.get(RUN_OWNER_RECOVERY_KEY)) !== undefined
|
|
506
|
+
((await storage.get(RUN_OWNER_RECOVERY_KEY)) !== undefined ||
|
|
507
|
+
(await storage.get(RUN_LEG_KEY)) !== undefined
|
|
453
508
|
? Date.now() + RUN_OWNER_RECOVERY_DELAY_MS
|
|
454
509
|
: undefined);
|
|
455
510
|
// `now` is sampled AFTER the awaited recovery-key read above. Hoisting it
|
|
@@ -685,10 +740,7 @@ export class DurableObjectRunner {
|
|
|
685
740
|
// born abandoned. `previous` still reaches the write below, whose
|
|
686
741
|
// write-or-skip decision is about the key that is stored, not about whose
|
|
687
742
|
// run it names.
|
|
688
|
-
|
|
689
|
-
? previous
|
|
690
|
-
: undefined;
|
|
691
|
-
await this.#writeSuspensionDeadlines(workflowId, runId, previous, mergeSuspensionDeadlines(inherited, derived));
|
|
743
|
+
await this.#writeSuspensionDeadlines(workflowId, runId, previous, mergeSuspensionDeadlines(ownSuspensionDeadlines(previous, workflowId, runId), derived));
|
|
692
744
|
}
|
|
693
745
|
/**
|
|
694
746
|
* Report a deadline the run asked for and did not get, once. Derivation runs
|
|
@@ -708,6 +760,32 @@ export class DurableObjectRunner {
|
|
|
708
760
|
this.#reportedSuspensionRejections.add(seen);
|
|
709
761
|
console.error(`suspension deadline not armed for step '${rejection.step}' of run '${runId}': ${rejection.reason}`);
|
|
710
762
|
}
|
|
763
|
+
/**
|
|
764
|
+
* The summary with its timer steps listed, so approval bridges file nothing
|
|
765
|
+
* for a step this object will resume itself. It never throws and never
|
|
766
|
+
* deletes: the lifecycle change behind the response has already persisted,
|
|
767
|
+
* and the status and replay routes run outside the operation lock. Without a
|
|
768
|
+
* scheduled wake nothing would resume a timer, so a step this read cannot
|
|
769
|
+
* vouch for is left out and gets an approval a person can resume.
|
|
770
|
+
*/
|
|
771
|
+
async #withSuspensionTimers(workflowId, summary) {
|
|
772
|
+
try {
|
|
773
|
+
// Most suspensions ask for no timer; they pay no storage read.
|
|
774
|
+
if (suspensionTimerSteps(summary, undefined).length === 0)
|
|
775
|
+
return summary;
|
|
776
|
+
const storage = this.state?.storage;
|
|
777
|
+
if (!storage?.getAlarm || (await storage.getAlarm()) === null)
|
|
778
|
+
return summary;
|
|
779
|
+
const steps = suspensionTimerSteps(summary, ownSuspensionDeadlines(parseSuspensionDeadlineRecord(await storage.get(SUSPENSION_DEADLINE_STORAGE_KEY)), workflowId, summary.runId));
|
|
780
|
+
return steps.length === 0
|
|
781
|
+
? summary
|
|
782
|
+
: { ...summary, suspensionTimers: steps };
|
|
783
|
+
}
|
|
784
|
+
catch (error) {
|
|
785
|
+
console.error('suspension timer read failed', error);
|
|
786
|
+
return summary;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
711
789
|
/**
|
|
712
790
|
* Reconciliation is bookkeeping ABOUT a transition that has already been
|
|
713
791
|
* persisted by the runtime. Failing the caller's start, resume, or
|
|
@@ -745,6 +823,36 @@ export class DurableObjectRunner {
|
|
|
745
823
|
return false;
|
|
746
824
|
}
|
|
747
825
|
}
|
|
826
|
+
/**
|
|
827
|
+
* File the approvals of this object's run, suspended at a boundary the
|
|
828
|
+
* object reached on its own, through the lifecycle's `reconcileApprovals`.
|
|
829
|
+
* `known` is an authoritative summary already in hand; without one the run
|
|
830
|
+
* is read. Never throws: the boundary has already happened, and a host read
|
|
831
|
+
* with reconciliation files what a failure here misses.
|
|
832
|
+
*/
|
|
833
|
+
async #reconcileApprovalsBestEffort(workflowId, runId, known) {
|
|
834
|
+
// A foreign record names another object's run: nothing here to file.
|
|
835
|
+
if (!this.#isOwnRun(workflowId, runId))
|
|
836
|
+
return;
|
|
837
|
+
try {
|
|
838
|
+
const hooks = this.runLifecycle(this.env);
|
|
839
|
+
if (!hooks?.reconcileApprovals)
|
|
840
|
+
return;
|
|
841
|
+
const summary = known ??
|
|
842
|
+
(await this.#authoritativeStatusOrUnreadable(this.#ensureRuntime(), workflowId, runId));
|
|
843
|
+
if (summary?.status !== 'suspended' || !isReadableRunSummary(summary))
|
|
844
|
+
return;
|
|
845
|
+
await hooks.reconcileApprovals(workflowId, await this.#withSuspensionTimers(workflowId, summary));
|
|
846
|
+
}
|
|
847
|
+
catch (error) {
|
|
848
|
+
console.error(JSON.stringify({
|
|
849
|
+
type: 'reconcile-error',
|
|
850
|
+
workflowId,
|
|
851
|
+
runId,
|
|
852
|
+
error: errorMessageOf(error),
|
|
853
|
+
}));
|
|
854
|
+
}
|
|
855
|
+
}
|
|
748
856
|
// An entry is keyed by the DOT-JOINED suspended path, exactly as the summary
|
|
749
857
|
// keys the two fence fields read below, so the path is joined here too. The
|
|
750
858
|
// wake reads the rehydrated projection, which splits a stored key on every
|
|
@@ -776,6 +884,7 @@ export class DurableObjectRunner {
|
|
|
776
884
|
* and the budget would bound one burst instead of the suspension. The merge
|
|
777
885
|
* carries the tombstone while the fence is unchanged and drops it when the
|
|
778
886
|
* suspension moves on, so a later suspension of the same step starts fresh.
|
|
887
|
+
* Answers whether this charge abandoned the entry.
|
|
779
888
|
*/
|
|
780
889
|
async #chargeSuspensionDeadlineAttempt(stored, entry, now, error) {
|
|
781
890
|
const { workflowId, runId } = stored;
|
|
@@ -787,7 +896,7 @@ export class DurableObjectRunner {
|
|
|
787
896
|
...remaining,
|
|
788
897
|
tombstoned(entry),
|
|
789
898
|
]);
|
|
790
|
-
return;
|
|
899
|
+
return true;
|
|
791
900
|
}
|
|
792
901
|
console.error(`suspension deadline wake for step '${entry.step}' of run '${runId}' failed (attempt ${attempts})`, error);
|
|
793
902
|
// Rebuilt field by field rather than spread from `entry`: the REBUILD is
|
|
@@ -811,6 +920,7 @@ export class DurableObjectRunner {
|
|
|
811
920
|
nextAttemptAt: now + SUSPENSION_DEADLINE_RETRY_MS * 2 ** (attempts - 1),
|
|
812
921
|
},
|
|
813
922
|
]);
|
|
923
|
+
return false;
|
|
814
924
|
}
|
|
815
925
|
/**
|
|
816
926
|
* Record that this wake could not read the run's authoritative state, and
|
|
@@ -972,14 +1082,31 @@ export class DurableObjectRunner {
|
|
|
972
1082
|
// record this wake read goes with it: re-reading it would be the second
|
|
973
1083
|
// storage read #armAlarmFor's JSDoc refuses to pay.
|
|
974
1084
|
await this.#reconcileSuspensionDeadlines(workflowId, runId, summary, stored);
|
|
1085
|
+
// A timeout resume that stopped after persisting the next suspension
|
|
1086
|
+
// reaches here with no host request to file its approval.
|
|
1087
|
+
await this.#reconcileApprovalsBestEffort(workflowId, runId, summary);
|
|
975
1088
|
return;
|
|
976
1089
|
}
|
|
977
|
-
const next = await runtime.resume(workflowId, runId, {
|
|
1090
|
+
const next = await this.#withLeg(workflowId, runId, 'deadline', () => runtime.resume(workflowId, runId, {
|
|
978
1091
|
step: [entry.step],
|
|
979
1092
|
resumeData: suspensionTimeoutResumeData(entry, now),
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1093
|
+
// A timeout is no principal's action, so the run keeps its recorded
|
|
1094
|
+
// requester and the next gate is filed against them: the runtime
|
|
1095
|
+
// inherits a complete pair. Legacy provenance without a kind takes
|
|
1096
|
+
// the approval bridge's legacy default, which the runtime will not
|
|
1097
|
+
// infer, and a run without a requester resumes under the reserved id.
|
|
1098
|
+
...(summary.requestedBy === undefined
|
|
1099
|
+
? {
|
|
1100
|
+
requestedBy: SUSPENSION_DEADLINE_PRINCIPAL_ID,
|
|
1101
|
+
requestedByKind: 'system',
|
|
1102
|
+
}
|
|
1103
|
+
: summary.requestedByKind === undefined
|
|
1104
|
+
? {
|
|
1105
|
+
requestedBy: summary.requestedBy,
|
|
1106
|
+
requestedByKind: 'human',
|
|
1107
|
+
}
|
|
1108
|
+
: {}),
|
|
1109
|
+
}), true);
|
|
983
1110
|
// EVERYTHING after this point is best effort, because the resume has
|
|
984
1111
|
// already run the step: charging a bookkeeping failure to the retry ledger
|
|
985
1112
|
// would record a failed resume that in fact succeeded, and could resume the
|
|
@@ -994,6 +1121,10 @@ export class DurableObjectRunner {
|
|
|
994
1121
|
console.error('suspension deadline broadcast failed', error);
|
|
995
1122
|
}
|
|
996
1123
|
await this.#reconcileSuspensionDeadlinesBestEffort(workflowId, runId, next);
|
|
1124
|
+
// From a fresh read, not `next`: the live projection keys a nested gate
|
|
1125
|
+
// differently from the stored one a host read files against.
|
|
1126
|
+
if (next.status === 'suspended')
|
|
1127
|
+
await this.#reconcileApprovalsBestEffort(workflowId, runId);
|
|
997
1128
|
}
|
|
998
1129
|
/**
|
|
999
1130
|
* The shared end of every wake that will not resume AND holds no
|
|
@@ -1001,9 +1132,8 @@ export class DurableObjectRunner {
|
|
|
1001
1132
|
* directly from the one it read): re-derive this run's deadlines from
|
|
1002
1133
|
* authoritative state. The set of wakes that land here with nothing due is
|
|
1003
1134
|
* ordinary, not anomalous — the watchdog survivor whose duty already ran,
|
|
1004
|
-
* H1's retry wake after a failed reconcile, a recovery-armed wake
|
|
1005
|
-
* start
|
|
1006
|
-
* workerd alarm redelivery or at-least-once retry after a rethrown recovery
|
|
1135
|
+
* H1's retry wake after a failed reconcile, a recovery-armed wake that fell
|
|
1136
|
+
* due after its start leg returned, a workerd alarm redelivery or at-least-once retry after a rethrown recovery
|
|
1007
1137
|
* error (up to 6 retries), and an entry still inside the 1 s arm floor. The
|
|
1008
1138
|
* `authoritativeStatus()` read lands off the lifecycle hot path on exactly
|
|
1009
1139
|
* those wakes — and it is the authoritative read, not `status()`, because
|
|
@@ -1187,14 +1317,19 @@ export class DurableObjectRunner {
|
|
|
1187
1317
|
// above — has nothing to charge, so it keeps the watchdog cadence.
|
|
1188
1318
|
if (!stored || !entry)
|
|
1189
1319
|
return false;
|
|
1320
|
+
let abandoned;
|
|
1190
1321
|
try {
|
|
1191
|
-
await this.#chargeSuspensionDeadlineAttempt(stored, entry, now, error);
|
|
1192
|
-
return true;
|
|
1322
|
+
abandoned = await this.#chargeSuspensionDeadlineAttempt(stored, entry, now, error);
|
|
1193
1323
|
}
|
|
1194
1324
|
catch (ledgerError) {
|
|
1195
1325
|
console.error('suspension deadline ledger update failed', ledgerError);
|
|
1196
1326
|
return false;
|
|
1197
1327
|
}
|
|
1328
|
+
// A step whose timer this object gave up on gets the approval a person
|
|
1329
|
+
// can resume it with.
|
|
1330
|
+
if (abandoned)
|
|
1331
|
+
await this.#reconcileApprovalsBestEffort(stored.workflowId, stored.runId);
|
|
1332
|
+
return true;
|
|
1198
1333
|
}
|
|
1199
1334
|
}
|
|
1200
1335
|
async #recoverRunOwner(recovery, ownFrame, ownFailure) {
|
|
@@ -1221,6 +1356,7 @@ export class DurableObjectRunner {
|
|
|
1221
1356
|
isOwnerQuiescent: quiescent,
|
|
1222
1357
|
startReservation: recovery.startReservation,
|
|
1223
1358
|
expectedTarget: { kind: 'workflow' },
|
|
1359
|
+
startLeg: ownFrame?.unwound === true ? 'unwound' : 'touched',
|
|
1224
1360
|
});
|
|
1225
1361
|
}
|
|
1226
1362
|
else if (recovery.phase === 'prepared-unfenced') {
|
|
@@ -1320,13 +1456,235 @@ export class DurableObjectRunner {
|
|
|
1320
1456
|
}
|
|
1321
1457
|
: { ...base, phase: 'prepared-unfenced', execution };
|
|
1322
1458
|
}
|
|
1459
|
+
/** The recovered run's summary, or undefined when none was recovered. */
|
|
1323
1460
|
async #recoverPendingRunOwner() {
|
|
1324
1461
|
const stored = await this.state?.storage?.get(RUN_OWNER_RECOVERY_KEY);
|
|
1325
|
-
if (stored
|
|
1326
|
-
|
|
1462
|
+
if (stored === undefined)
|
|
1463
|
+
return undefined;
|
|
1464
|
+
const recovery = this.#runOwnerRecovery(stored);
|
|
1465
|
+
const summary = await this.#recoverRunOwner(recovery);
|
|
1466
|
+
return summary ? { workflowId: recovery.workflowId, summary } : undefined;
|
|
1467
|
+
}
|
|
1468
|
+
/**
|
|
1469
|
+
* Drive one execution leg under a durable marker and an in-memory frame.
|
|
1470
|
+
*
|
|
1471
|
+
* The marker is written before the engine runs and removed once the leg is
|
|
1472
|
+
* known to have left the run in a durable state (suspended or terminal). A
|
|
1473
|
+
* leg that throws is not such evidence: when a platform limit cancels the
|
|
1474
|
+
* invocation, the engine's promise rejects mid-step and this frame still
|
|
1475
|
+
* unwinds, so a throw clears the marker only when an authoritative read then
|
|
1476
|
+
* shows a durable run — the read a cancelled invocation cannot make. A kept
|
|
1477
|
+
* marker is the next wake's to settle.
|
|
1478
|
+
*
|
|
1479
|
+
* While the leg runs it also marks its run row live in D1 every
|
|
1480
|
+
* RUN_LEG_TOUCH_MS. The marker says a leg began; the touch says one is still
|
|
1481
|
+
* running, from whichever instance runs it — during a deploy an outgoing
|
|
1482
|
+
* instance keeps executing a leg that never touches this object's storage,
|
|
1483
|
+
* while the next wake already runs on the new instance.
|
|
1484
|
+
*
|
|
1485
|
+
* `armed` is true where the caller already holds a wake within the recovery
|
|
1486
|
+
* delay: the start journal's, or the watchdog an alarm body opens with.
|
|
1487
|
+
* Clearing never arms: each caller's own boundary re-arms from what is left.
|
|
1488
|
+
* Everything after `body` is best effort, because the leg has already run.
|
|
1489
|
+
*/
|
|
1490
|
+
async #withLeg(workflowId, runId, trigger, body, armed) {
|
|
1491
|
+
const runtime = this.#ensureRuntime();
|
|
1492
|
+
const frame = {
|
|
1493
|
+
token: crypto.randomUUID(),
|
|
1494
|
+
startedAt: Date.now(),
|
|
1495
|
+
};
|
|
1496
|
+
this.#legs.add(frame);
|
|
1497
|
+
const touch = setInterval(() => {
|
|
1498
|
+
runtime
|
|
1499
|
+
.touchRun(workflowId, runId)
|
|
1500
|
+
.catch((error) => console.error('run leg liveness touch failed', error));
|
|
1501
|
+
}, RUN_LEG_TOUCH_MS);
|
|
1502
|
+
const clearIfDurable = async (summary) => {
|
|
1503
|
+
try {
|
|
1504
|
+
if (summary && isDurableRunStatus(summary.status))
|
|
1505
|
+
await this.#clearLegMarker(frame.token);
|
|
1506
|
+
}
|
|
1507
|
+
catch (error) {
|
|
1508
|
+
console.error('run leg marker clear failed', error);
|
|
1509
|
+
}
|
|
1510
|
+
};
|
|
1511
|
+
try {
|
|
1512
|
+
const storage = this.state?.storage;
|
|
1513
|
+
if (storage) {
|
|
1514
|
+
if (!armed)
|
|
1515
|
+
await this.#armNextAlarm(Date.now() + RUN_OWNER_RECOVERY_DELAY_MS);
|
|
1516
|
+
await storage.put(RUN_LEG_KEY, {
|
|
1517
|
+
version: 1,
|
|
1518
|
+
workflowId,
|
|
1519
|
+
runId,
|
|
1520
|
+
token: frame.token,
|
|
1521
|
+
trigger,
|
|
1522
|
+
startedAt: frame.startedAt,
|
|
1523
|
+
});
|
|
1524
|
+
}
|
|
1525
|
+
let summary;
|
|
1526
|
+
try {
|
|
1527
|
+
summary = await body();
|
|
1528
|
+
}
|
|
1529
|
+
catch (error) {
|
|
1530
|
+
let current = null;
|
|
1531
|
+
try {
|
|
1532
|
+
current = await runtime.authoritativeStatus(workflowId, runId);
|
|
1533
|
+
}
|
|
1534
|
+
catch {
|
|
1535
|
+
// Unreadable: the marker stays for the next wake.
|
|
1536
|
+
}
|
|
1537
|
+
await clearIfDurable(current);
|
|
1538
|
+
throw error;
|
|
1539
|
+
}
|
|
1540
|
+
await clearIfDurable(summary);
|
|
1541
|
+
return summary;
|
|
1542
|
+
}
|
|
1543
|
+
finally {
|
|
1544
|
+
clearInterval(touch);
|
|
1545
|
+
this.#legs.delete(frame);
|
|
1327
1546
|
}
|
|
1328
1547
|
}
|
|
1548
|
+
/** Remove the leg marker unless a later leg has replaced it. */
|
|
1549
|
+
async #clearLegMarker(token) {
|
|
1550
|
+
const storage = this.state?.storage;
|
|
1551
|
+
if (!storage)
|
|
1552
|
+
return;
|
|
1553
|
+
const stored = await storage.get(RUN_LEG_KEY);
|
|
1554
|
+
if (stored === undefined)
|
|
1555
|
+
return;
|
|
1556
|
+
let current;
|
|
1557
|
+
try {
|
|
1558
|
+
current = runLegMarker(stored);
|
|
1559
|
+
}
|
|
1560
|
+
catch {
|
|
1561
|
+
current = undefined;
|
|
1562
|
+
}
|
|
1563
|
+
if (current !== undefined && current.token !== token)
|
|
1564
|
+
return;
|
|
1565
|
+
await storage.delete(RUN_LEG_KEY);
|
|
1566
|
+
}
|
|
1567
|
+
/**
|
|
1568
|
+
* The alarm's check before it queues behind the operation lock, which a leg
|
|
1569
|
+
* holds for its whole run. It reads only this isolate's frames, so the
|
|
1570
|
+
* watchdog it may arm is still the wake's first storage operation.
|
|
1571
|
+
*
|
|
1572
|
+
* A leg in this isolate is executing as far as the isolate can tell: the
|
|
1573
|
+
* wake has nothing to do while it runs (the run is not suspended, and its
|
|
1574
|
+
* start journal cannot be recovered while it is live), so it keeps the
|
|
1575
|
+
* recovery cadence instead of waiting out the leg — which can outlast the
|
|
1576
|
+
* alarm's own wall-time limit.
|
|
1577
|
+
*
|
|
1578
|
+
* A frame older than RUN_LEG_MAX_AGE_MS is reset instead. Its promise still
|
|
1579
|
+
* holds the operation and run locks, so nothing in this isolate can settle
|
|
1580
|
+
* the run; the interrupted alarm retries on a fresh instance, which settles
|
|
1581
|
+
* through #settleInterruptedLeg. Writing the snapshot from here would race a
|
|
1582
|
+
* promise that could still write it. Returns whether this wake stops here.
|
|
1583
|
+
*/
|
|
1584
|
+
async #deferToLiveLeg() {
|
|
1585
|
+
const frames = [...this.#legs];
|
|
1586
|
+
if (frames.length === 0)
|
|
1587
|
+
return false;
|
|
1588
|
+
const now = Date.now();
|
|
1589
|
+
if (frames.some((frame) => now - frame.startedAt >= RUN_LEG_MAX_AGE_MS)) {
|
|
1590
|
+
console.error(JSON.stringify({ type: 'run-leg-reset', legs: frames.length }));
|
|
1591
|
+
this.state?.abort?.('flowsafe: run leg outlived its invocation');
|
|
1592
|
+
}
|
|
1593
|
+
await this.#armAlarmWatchdog();
|
|
1594
|
+
return true;
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Settle the run a leg marker names once no leg of this object drives it.
|
|
1598
|
+
*
|
|
1599
|
+
* The first duty of every alarm body, ahead of start recovery: a stranded
|
|
1600
|
+
* start settled here reaches the journal duty as a terminal run, whose
|
|
1601
|
+
* terminal branch settles the start reservation and clears the journal.
|
|
1602
|
+
*
|
|
1603
|
+
* The marker is this object's own record, written only after its run's
|
|
1604
|
+
* identity was asserted, so its ids are asserted against `id.name` like the
|
|
1605
|
+
* start journal's. No leg of this object can be executing here: each one
|
|
1606
|
+
* drops its frame before it releases the operation lock this duty runs
|
|
1607
|
+
* under. A leg on an instance being replaced still touches the run row, and
|
|
1608
|
+
* the runtime settles nothing while it does (`live`).
|
|
1609
|
+
*
|
|
1610
|
+
* A refused or `live` settlement keeps the marker, so the recovery cadence
|
|
1611
|
+
* retries it, for at most RUN_LEG_UNSETTLED_LIMIT_MS after the leg began;
|
|
1612
|
+
* past that the marker is dropped with a log line rather than waking the
|
|
1613
|
+
* object forever.
|
|
1614
|
+
*/
|
|
1615
|
+
async #settleInterruptedLeg() {
|
|
1616
|
+
const storage = this.state?.storage;
|
|
1617
|
+
if (!storage)
|
|
1618
|
+
return;
|
|
1619
|
+
const stored = await storage.get(RUN_LEG_KEY);
|
|
1620
|
+
if (stored === undefined)
|
|
1621
|
+
return;
|
|
1622
|
+
let marker;
|
|
1623
|
+
try {
|
|
1624
|
+
marker = runLegMarker(stored);
|
|
1625
|
+
}
|
|
1626
|
+
catch (error) {
|
|
1627
|
+
console.error('stored run leg marker discarded', error);
|
|
1628
|
+
await storage.delete(RUN_LEG_KEY);
|
|
1629
|
+
return;
|
|
1630
|
+
}
|
|
1631
|
+
const { workflowId, runId } = marker;
|
|
1632
|
+
const dropIfStale = async (cause) => {
|
|
1633
|
+
if (Date.now() - marker.startedAt < RUN_LEG_UNSETTLED_LIMIT_MS)
|
|
1634
|
+
return false;
|
|
1635
|
+
console.error(`run leg marker for run '${runId}' of workflow '${workflowId}' dropped without settlement`, cause);
|
|
1636
|
+
await this.#clearLegMarker(marker.token);
|
|
1637
|
+
return true;
|
|
1638
|
+
};
|
|
1639
|
+
let settled;
|
|
1640
|
+
let runtime;
|
|
1641
|
+
try {
|
|
1642
|
+
this.#assertRunIdentity(workflowId, runId);
|
|
1643
|
+
runtime = this.#ensureRuntime();
|
|
1644
|
+
settled = await runtime.settleInterruptedRun(workflowId, runId);
|
|
1645
|
+
}
|
|
1646
|
+
catch (error) {
|
|
1647
|
+
if (await dropIfStale(error))
|
|
1648
|
+
return;
|
|
1649
|
+
throw error;
|
|
1650
|
+
}
|
|
1651
|
+
if (settled.kind === 'live') {
|
|
1652
|
+
await dropIfStale();
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1655
|
+
let summary;
|
|
1656
|
+
if (settled.kind === 'interrupted') {
|
|
1657
|
+
summary = settled.summary;
|
|
1658
|
+
console.error(JSON.stringify({
|
|
1659
|
+
type: 'run-leg-interrupted',
|
|
1660
|
+
workflowId,
|
|
1661
|
+
runId,
|
|
1662
|
+
trigger: marker.trigger,
|
|
1663
|
+
}));
|
|
1664
|
+
}
|
|
1665
|
+
else if (settled.kind === 'transition') {
|
|
1666
|
+
const owner = await this.runOwnership(this.env).owner('run', runId);
|
|
1667
|
+
summary = await this.#finalizeTerminal(runtime, workflowId, runId, owner ?? settled.replayPrincipal, settled.transition);
|
|
1668
|
+
}
|
|
1669
|
+
if (summary) {
|
|
1670
|
+
try {
|
|
1671
|
+
this.#broadcastRunSummary(summary);
|
|
1672
|
+
}
|
|
1673
|
+
catch (error) {
|
|
1674
|
+
console.error('interrupted run broadcast failed', error);
|
|
1675
|
+
}
|
|
1676
|
+
await this.#reconcileSuspensionDeadlinesBestEffort(workflowId, runId, summary);
|
|
1677
|
+
}
|
|
1678
|
+
else if (settled.kind === 'durable') {
|
|
1679
|
+
// A leg that stopped after persisting a suspension reached it with no
|
|
1680
|
+
// host request to file its approval.
|
|
1681
|
+
await this.#reconcileApprovalsBestEffort(workflowId, runId);
|
|
1682
|
+
}
|
|
1683
|
+
await this.#clearLegMarker(marker.token);
|
|
1684
|
+
}
|
|
1329
1685
|
async alarm() {
|
|
1686
|
+
if (await this.#deferToLiveLeg())
|
|
1687
|
+
return;
|
|
1330
1688
|
await this.#withOperationLock(async () => {
|
|
1331
1689
|
await this.#armAlarmWatchdog();
|
|
1332
1690
|
// The watchdog arm above is deliberately the ONLY arm on the path where
|
|
@@ -1337,12 +1695,23 @@ export class DurableObjectRunner {
|
|
|
1337
1695
|
// long as the misbinding lasts. Failing with the watchdog in place keeps
|
|
1338
1696
|
// the recovery cadence this object woke at before it served deadlines.
|
|
1339
1697
|
await verifyDurableObjectDeploymentIdentity(this.state, this.env);
|
|
1340
|
-
// The
|
|
1341
|
-
//
|
|
1342
|
-
//
|
|
1698
|
+
// The duties are failure-isolated: none that throws may skip the
|
|
1699
|
+
// others (or their re-arm), and none may leave the object with no wake.
|
|
1700
|
+
// A leg settlement that fails keeps its marker, which the final arm
|
|
1701
|
+
// reads back into the recovery cadence; it is never rethrown, because a
|
|
1702
|
+
// retried alarm would repeat the other duties' work.
|
|
1703
|
+
try {
|
|
1704
|
+
await this.#settleInterruptedLeg();
|
|
1705
|
+
}
|
|
1706
|
+
catch (error) {
|
|
1707
|
+
console.error('interrupted run leg settlement failed', error);
|
|
1708
|
+
}
|
|
1343
1709
|
let recoveryError;
|
|
1344
1710
|
try {
|
|
1345
|
-
await this.#recoverPendingRunOwner();
|
|
1711
|
+
const recovered = await this.#recoverPendingRunOwner();
|
|
1712
|
+
// Only the alarm files: its recovery has no host request behind it.
|
|
1713
|
+
if (recovered)
|
|
1714
|
+
await this.#reconcileApprovalsBestEffort(recovered.workflowId, recovered.summary.runId, recovered.summary);
|
|
1346
1715
|
}
|
|
1347
1716
|
catch (error) {
|
|
1348
1717
|
recoveryError = error;
|
|
@@ -1364,6 +1733,12 @@ export class DurableObjectRunner {
|
|
|
1364
1733
|
// comes from.
|
|
1365
1734
|
console.error('run owner recovery could not read authoritative state', recoveryError);
|
|
1366
1735
|
}
|
|
1736
|
+
else if (isRunStartPendingError(recoveryError)) {
|
|
1737
|
+
// Not rethrown either: a start whose leg may still run, or whose
|
|
1738
|
+
// row the touch rule does not yet read as silent, settled nothing.
|
|
1739
|
+
// The journal survives, and the arm below keeps the 60 s cadence
|
|
1740
|
+
// without workerd's immediate retries.
|
|
1741
|
+
}
|
|
1367
1742
|
else {
|
|
1368
1743
|
if (converged)
|
|
1369
1744
|
await this.#rearmRunOwnerRecovery();
|
|
@@ -1381,8 +1756,8 @@ export class DurableObjectRunner {
|
|
|
1381
1756
|
const [, workflowId, runId, action] = segments;
|
|
1382
1757
|
if (request.method === 'POST' && segments.length === 1) {
|
|
1383
1758
|
const principal = this.#trustedExecutionPrincipal(encodedPrincipal);
|
|
1759
|
+
const body = await readJson(request);
|
|
1384
1760
|
return this.#withOperationLock(async () => {
|
|
1385
|
-
const body = await readJson(request);
|
|
1386
1761
|
if (body &&
|
|
1387
1762
|
[
|
|
1388
1763
|
'mutationEpoch',
|
|
@@ -1489,7 +1864,7 @@ export class DurableObjectRunner {
|
|
|
1489
1864
|
await this.#reserveRunOwner(startRunId, owner, recovery.token);
|
|
1490
1865
|
let summary;
|
|
1491
1866
|
try {
|
|
1492
|
-
summary = await runtime.start(startWorkflowId, {
|
|
1867
|
+
summary = await this.#withLeg(startWorkflowId, startRunId, 'start', () => runtime.start(startWorkflowId, {
|
|
1493
1868
|
runId: startRunId,
|
|
1494
1869
|
inputData: resolvedInput,
|
|
1495
1870
|
initialState: resolvedState,
|
|
@@ -1510,14 +1885,14 @@ export class DurableObjectRunner {
|
|
|
1510
1885
|
...(deadlineMs === undefined
|
|
1511
1886
|
? {}
|
|
1512
1887
|
: { deadlineMs: deadlineMs }),
|
|
1513
|
-
});
|
|
1888
|
+
}), true);
|
|
1514
1889
|
}
|
|
1515
1890
|
finally {
|
|
1516
1891
|
frame.unwound = true;
|
|
1517
1892
|
}
|
|
1518
1893
|
summary = await this.#finishRunOwner(recovery, summary);
|
|
1519
1894
|
this.#broadcastRunSummary(summary);
|
|
1520
|
-
return json(summary);
|
|
1895
|
+
return json(await this.#withSuspensionTimers(startWorkflowId, summary));
|
|
1521
1896
|
}
|
|
1522
1897
|
catch (error) {
|
|
1523
1898
|
frame.unwound = true;
|
|
@@ -1529,7 +1904,7 @@ export class DurableObjectRunner {
|
|
|
1529
1904
|
throw new Error('run owner recovery changed');
|
|
1530
1905
|
const recovered = await this.#recoverRunOwner(current, frame, error);
|
|
1531
1906
|
if (recovered)
|
|
1532
|
-
return json(recovered);
|
|
1907
|
+
return json(await this.#withSuspensionTimers(startWorkflowId, recovered));
|
|
1533
1908
|
}
|
|
1534
1909
|
}
|
|
1535
1910
|
catch (recoveryError) {
|
|
@@ -1576,12 +1951,16 @@ export class DurableObjectRunner {
|
|
|
1576
1951
|
});
|
|
1577
1952
|
return json(state.kind === 'initial'
|
|
1578
1953
|
? { kind: 'initial', execution }
|
|
1579
|
-
: {
|
|
1954
|
+
: {
|
|
1955
|
+
kind: 'result',
|
|
1956
|
+
execution,
|
|
1957
|
+
value: await this.#withSuspensionTimers(workflowId, state.summary),
|
|
1958
|
+
});
|
|
1580
1959
|
}
|
|
1581
1960
|
const summary = await runtime.status(workflowId, runId);
|
|
1582
1961
|
if (!summary)
|
|
1583
1962
|
return json({ error: 'run not found' }, 404);
|
|
1584
|
-
return json(summary);
|
|
1963
|
+
return json(await this.#withSuspensionTimers(workflowId, summary));
|
|
1585
1964
|
}
|
|
1586
1965
|
if (request.method === 'GET' &&
|
|
1587
1966
|
segments.length === 4 &&
|
|
@@ -1595,7 +1974,7 @@ export class DurableObjectRunner {
|
|
|
1595
1974
|
const summary = await runtime.status(workflowId, runId);
|
|
1596
1975
|
if (!summary)
|
|
1597
1976
|
return json({ error: 'run not found' }, 404);
|
|
1598
|
-
return json(summary);
|
|
1977
|
+
return json(await this.#withSuspensionTimers(workflowId, summary));
|
|
1599
1978
|
});
|
|
1600
1979
|
}
|
|
1601
1980
|
if (request.method === 'GET' &&
|
|
@@ -1650,7 +2029,7 @@ export class DurableObjectRunner {
|
|
|
1650
2029
|
// resumes — the suspended runs it is draining are waiting for exactly
|
|
1651
2030
|
// these — and proof-only admits its one nominated run.
|
|
1652
2031
|
await runtime.assertExistingRunAllowed(workflowId, runId);
|
|
1653
|
-
const summary = await runtime.resume(workflowId, runId, {
|
|
2032
|
+
const summary = await this.#withLeg(workflowId, runId, 'resume', () => runtime.resume(workflowId, runId, {
|
|
1654
2033
|
step: body.step,
|
|
1655
2034
|
resumeData,
|
|
1656
2035
|
requestedBy,
|
|
@@ -1658,11 +2037,21 @@ export class DurableObjectRunner {
|
|
|
1658
2037
|
...(body.deadlineMs === undefined
|
|
1659
2038
|
? {}
|
|
1660
2039
|
: { deadlineMs: body.deadlineMs }),
|
|
1661
|
-
});
|
|
1662
|
-
|
|
2040
|
+
}), false);
|
|
2041
|
+
// The leg armed a recovery-cadence wake for its marker. A boundary
|
|
2042
|
+
// that converged re-arms from what is left, which drops that wake
|
|
2043
|
+
// when nothing is pending; one that did not keeps its retry wake.
|
|
2044
|
+
if (await this.#reconcileSuspensionDeadlinesBestEffort(workflowId, runId, summary)) {
|
|
2045
|
+
try {
|
|
2046
|
+
await this.#armNextAlarm();
|
|
2047
|
+
}
|
|
2048
|
+
catch (error) {
|
|
2049
|
+
console.error('resume boundary re-arm failed', error);
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
1663
2052
|
// Broadcast the post-resume authoritative summary.
|
|
1664
2053
|
this.#broadcastRunSummary(summary);
|
|
1665
|
-
return json(summary);
|
|
2054
|
+
return json(await this.#withSuspensionTimers(workflowId, summary));
|
|
1666
2055
|
});
|
|
1667
2056
|
}
|
|
1668
2057
|
if (request.method === 'POST' &&
|
|
@@ -1683,10 +2072,10 @@ export class DurableObjectRunner {
|
|
|
1683
2072
|
return this.#withOperationLock(async () => {
|
|
1684
2073
|
const owner = await this.runOwnership(this.env).owner('run', runId);
|
|
1685
2074
|
if (action === 'terminate-replay') {
|
|
1686
|
-
//
|
|
1687
|
-
//
|
|
1688
|
-
//
|
|
1689
|
-
//
|
|
2075
|
+
// The in-memory fallback has no requestContext for lifecycle
|
|
2076
|
+
// projection, and core's statuses include neither cancelled nor
|
|
2077
|
+
// timed_out. It cannot authorize a terminal replay during a degraded
|
|
2078
|
+
// read.
|
|
1690
2079
|
const summary = await runtime.status(workflowId, runId);
|
|
1691
2080
|
if (summary?.status !== 'cancelled' &&
|
|
1692
2081
|
summary?.status !== 'timed_out') {
|
|
@@ -1720,12 +2109,10 @@ export class DurableObjectRunner {
|
|
|
1720
2109
|
return this.#withOperationLock(async () => {
|
|
1721
2110
|
const owner = await this.runOwnership(this.env).owner('run', runId);
|
|
1722
2111
|
if (!owner) {
|
|
1723
|
-
//
|
|
1724
|
-
//
|
|
1725
|
-
//
|
|
1726
|
-
//
|
|
1727
|
-
// degraded read delays the sweep rather than timing out a run whose
|
|
1728
|
-
// state nothing could read.
|
|
2112
|
+
// The in-memory fallback has no lifecycle requestContext and core
|
|
2113
|
+
// has no timed_out status, so it cannot authorize this ownerless
|
|
2114
|
+
// deadline route. A degraded read fails the maintenance sweep pass,
|
|
2115
|
+
// which retries on the next interval.
|
|
1729
2116
|
const summary = await runtime.status(workflowId, runId);
|
|
1730
2117
|
if (summary?.status !== 'timed_out') {
|
|
1731
2118
|
throw new UnknownRunError(workflowId, runId);
|