@proofoftech/flowsafe 0.34.0 → 0.36.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 +84 -0
- package/README.md +15 -3
- package/dist/answer-schema.d.ts +30 -0
- package/dist/answer-schema.d.ts.map +1 -0
- package/dist/answer-schema.js +311 -0
- package/dist/answer-schema.js.map +1 -0
- package/dist/approval-api/answer-schema.d.ts +2 -0
- package/dist/approval-api/answer-schema.d.ts.map +1 -0
- package/dist/approval-api/answer-schema.js +3 -0
- package/dist/approval-api/answer-schema.js.map +1 -0
- package/dist/approval-api/d1-store.d.ts +3 -16
- package/dist/approval-api/d1-store.d.ts.map +1 -1
- package/dist/approval-api/d1-store.js +117 -33
- package/dist/approval-api/d1-store.js.map +1 -1
- package/dist/approval-api/grants.d.ts +2 -3
- package/dist/approval-api/grants.d.ts.map +1 -1
- package/dist/approval-api/grants.js +5 -3
- package/dist/approval-api/grants.js.map +1 -1
- package/dist/approval-api/human-notifications.d.ts +34 -0
- package/dist/approval-api/human-notifications.d.ts.map +1 -0
- package/dist/approval-api/human-notifications.js +160 -0
- package/dist/approval-api/human-notifications.js.map +1 -0
- package/dist/approval-api/index.d.ts +3 -1
- package/dist/approval-api/index.d.ts.map +1 -1
- package/dist/approval-api/index.js +1 -0
- package/dist/approval-api/index.js.map +1 -1
- package/dist/approval-api/retention.d.ts +3 -1
- package/dist/approval-api/retention.d.ts.map +1 -1
- package/dist/approval-api/retention.js +21 -1
- package/dist/approval-api/retention.js.map +1 -1
- package/dist/approval-api/router.d.ts +1 -1
- package/dist/approval-api/router.d.ts.map +1 -1
- package/dist/approval-api/router.js +7 -0
- package/dist/approval-api/router.js.map +1 -1
- package/dist/approval-api/service.d.ts +6 -1
- package/dist/approval-api/service.d.ts.map +1 -1
- package/dist/approval-api/service.js +55 -10
- package/dist/approval-api/service.js.map +1 -1
- package/dist/approval-api/store-factory.d.ts +2 -0
- package/dist/approval-api/store-factory.d.ts.map +1 -1
- package/dist/approval-api/store-factory.js.map +1 -1
- package/dist/approval-api/store.d.ts +3 -0
- package/dist/approval-api/store.d.ts.map +1 -1
- package/dist/approval-api/store.js +2 -1
- package/dist/approval-api/store.js.map +1 -1
- package/dist/approval-api/types.d.ts +78 -4
- package/dist/approval-api/types.d.ts.map +1 -1
- package/dist/approval-api/types.js +1 -0
- package/dist/approval-api/types.js.map +1 -1
- package/dist/do-runner/constants.d.ts +3 -0
- package/dist/do-runner/constants.d.ts.map +1 -1
- package/dist/do-runner/constants.js +3 -0
- package/dist/do-runner/constants.js.map +1 -1
- package/dist/do-runner/d1-storage.d.ts.map +1 -1
- package/dist/do-runner/d1-storage.js +34 -1
- package/dist/do-runner/d1-storage.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts +10 -3
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +590 -65
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/execution-context.d.ts +6 -0
- package/dist/do-runner/execution-context.d.ts.map +1 -1
- package/dist/do-runner/execution-context.js +8 -0
- package/dist/do-runner/execution-context.js.map +1 -1
- package/dist/do-runner/fenced-workflow-capability.d.ts +12 -1
- package/dist/do-runner/fenced-workflow-capability.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflow-capability.js +17 -0
- package/dist/do-runner/fenced-workflow-capability.js.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.js +165 -21
- package/dist/do-runner/fenced-workflows-d1.js.map +1 -1
- package/dist/do-runner/index.d.ts +6 -4
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +4 -2
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/init.d.ts +2 -2
- package/dist/do-runner/inventory.d.ts +1 -1
- package/dist/do-runner/inventory.d.ts.map +1 -1
- package/dist/do-runner/inventory.js +57 -1
- package/dist/do-runner/inventory.js.map +1 -1
- package/dist/do-runner/questions.d.ts +70 -0
- package/dist/do-runner/questions.d.ts.map +1 -0
- package/dist/do-runner/questions.js +440 -0
- package/dist/do-runner/questions.js.map +1 -0
- package/dist/do-runner/run-lifecycle.d.ts +25 -7
- package/dist/do-runner/run-lifecycle.d.ts.map +1 -1
- package/dist/do-runner/run-lifecycle.js +54 -1
- package/dist/do-runner/run-lifecycle.js.map +1 -1
- package/dist/do-runner/run-storage-tables.d.ts +2 -0
- package/dist/do-runner/run-storage-tables.d.ts.map +1 -1
- package/dist/do-runner/run-storage-tables.js +2 -0
- package/dist/do-runner/run-storage-tables.js.map +1 -1
- package/dist/do-runner/run-terminal-state.d.ts +1 -1
- package/dist/do-runner/run-terminal-state.d.ts.map +1 -1
- package/dist/do-runner/run-terminal-state.js +2 -2
- package/dist/do-runner/run-terminal-state.js.map +1 -1
- package/dist/do-runner/runtime.d.ts +92 -17
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +468 -42
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/schedule-source.d.ts +4 -3
- package/dist/do-runner/schedule-source.d.ts.map +1 -1
- package/dist/do-runner/schedule-source.js.map +1 -1
- package/dist/do-runner/suspension-deadline.d.ts.map +1 -1
- package/dist/do-runner/suspension-deadline.js +9 -4
- package/dist/do-runner/suspension-deadline.js.map +1 -1
- package/dist/do-runner/suspension-kinds.d.ts +6 -0
- package/dist/do-runner/suspension-kinds.d.ts.map +1 -0
- package/dist/do-runner/suspension-kinds.js +33 -0
- package/dist/do-runner/suspension-kinds.js.map +1 -0
- package/dist/host-kit/approval-bridge.d.ts +12 -14
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +40 -18
- 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 +30 -1
- package/dist/host-kit/do-response.js.map +1 -1
- package/dist/host-kit/do-run-topology.d.ts +8 -3
- package/dist/host-kit/do-run-topology.d.ts.map +1 -1
- package/dist/host-kit/do-run-topology.js +64 -1
- package/dist/host-kit/do-run-topology.js.map +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +23 -6
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +121 -16
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/host-approval-service.d.ts +1 -1
- package/dist/host-kit/host-approval-service.d.ts.map +1 -1
- package/dist/host-kit/host-approval-service.js +10 -4
- package/dist/host-kit/host-approval-service.js.map +1 -1
- package/dist/host-kit/index.d.ts +3 -1
- package/dist/host-kit/index.d.ts.map +1 -1
- package/dist/host-kit/index.js +1 -0
- package/dist/host-kit/index.js.map +1 -1
- package/dist/host-kit/run-router.d.ts +35 -3
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +119 -6
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/human-notification-storage.d.ts +5 -0
- package/dist/human-notification-storage.d.ts.map +1 -0
- package/dist/human-notification-storage.js +57 -0
- package/dist/human-notification-storage.js.map +1 -0
- package/dist/notification-retry.d.ts +3 -0
- package/dist/notification-retry.d.ts.map +1 -0
- package/dist/notification-retry.js +6 -0
- package/dist/notification-retry.js.map +1 -0
- package/dist/schedules/fire-plan.d.ts +30 -0
- package/dist/schedules/fire-plan.d.ts.map +1 -0
- package/dist/schedules/fire-plan.js +111 -0
- package/dist/schedules/fire-plan.js.map +1 -0
- package/dist/schedules/index.d.ts +4 -2
- package/dist/schedules/index.d.ts.map +1 -1
- package/dist/schedules/index.js +2 -0
- package/dist/schedules/index.js.map +1 -1
- package/dist/schedules/mutation-contract.d.ts +8 -6
- package/dist/schedules/mutation-contract.d.ts.map +1 -1
- package/dist/schedules/mutation-contract.js.map +1 -1
- package/dist/schedules/router.d.ts +9 -6
- package/dist/schedules/router.d.ts.map +1 -1
- package/dist/schedules/router.js +362 -94
- package/dist/schedules/router.js.map +1 -1
- package/dist/schedules/schedule-extensions.d.ts +46 -0
- package/dist/schedules/schedule-extensions.d.ts.map +1 -0
- package/dist/schedules/schedule-extensions.js +7 -0
- package/dist/schedules/schedule-extensions.js.map +1 -0
- package/dist/schedules/schedules-d1.d.ts +44 -10
- package/dist/schedules/schedules-d1.d.ts.map +1 -1
- package/dist/schedules/schedules-d1.js +437 -68
- package/dist/schedules/schedules-d1.js.map +1 -1
- package/dist/schedules/tick.d.ts +81 -18
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +701 -168
- package/dist/schedules/tick.js.map +1 -1
- package/dist/signals/notification-dispatch.d.ts +1 -1
- package/dist/signals/notification-dispatch.d.ts.map +1 -1
- package/dist/signals/notification-dispatch.js +3 -4
- package/dist/signals/notification-dispatch.js.map +1 -1
- package/package.json +1 -1
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
// recovering run-owner claims that outlive an interrupted start, and for the
|
|
13
13
|
// per-suspension deadlines of this run (suspension-deadline.ts); the duties
|
|
14
14
|
// share one wake, armed at the earliest due time.
|
|
15
|
+
import { AnswersInvalidError } from '../answer-schema.js';
|
|
15
16
|
import { decodeExecutionPrincipal, isExecutionPrincipalId, isExecutionPrincipalKind, isOwnedBy, samePrincipal, } from '../approval-api/principal.js';
|
|
16
17
|
import { readBoundedBody } from '../http-body.js';
|
|
17
18
|
import { errorMessageOf } from './cause-chain.js';
|
|
@@ -24,14 +25,16 @@ import { EXECUTION_PRINCIPAL_HEADER } from './execution-principal-header.js';
|
|
|
24
25
|
import { isDefinitiveInitialAdmissionRefusal } from './initial-admission-refusal.js';
|
|
25
26
|
import { ownDataCopy } from './own-data-copy.js';
|
|
26
27
|
import { isPathSafeId } from './path-safe-id.js';
|
|
28
|
+
import { answerQuestion, currentQuestions, deleteEarlyAnswer, expireQuestion, getQuestion, isQuestionFingerprint, isQuestionId, listQuestions, openQuestion, QUESTION_ANSWER_RESUME_KEY, QuestionNotOpenError, readEarlyAnswer, retainEarlyAnswer, sameQuestionSuspension, supersedeOpenQuestions, supersedeQuestion, } from './questions.js';
|
|
27
29
|
import { RemoteStepSettlementFencedError, WORKFLOW_REMOTE_STEP_RESULT_KEY, } from './remote-step-journal.js';
|
|
28
30
|
import { lifecycleFromRequestContext, terminalCleanupFor, } from './run-lifecycle.js';
|
|
29
31
|
import { decodeRunRequester, RUN_REQUESTER_HEADER } from './run-requester.js';
|
|
30
32
|
import { isDurableRunStatus, isTerminalRunStatus, } from './run-terminal-state.js';
|
|
31
|
-
import { InvalidRunRequestError, RunAlreadyExistsError, RunnerRuntime, RunStateUnreadableError, SuspensionChangedError, UnknownRunError, } from './runtime.js';
|
|
33
|
+
import { InvalidRunRequestError, parsePauseGeneration, parsePauseRunOptions, RunAlreadyExistsError, RunNotSuspendedError, RunnerRuntime, RunStateUnreadableError, RunTerminalConflictError, SuspensionChangedError, UnknownRunError, } from './runtime.js';
|
|
32
34
|
import { resolveScheduleStartOwner, } from './schedule-source.js';
|
|
33
35
|
import { captureReservation, sameReservationIdentity, } from './start-reservation-contract.js';
|
|
34
36
|
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';
|
|
37
|
+
import { suspensionKindsOf } from './suspension-kinds.js';
|
|
35
38
|
const RUN_OWNER_RECOVERY_KEY = 'flowsafe:run-owner-recovery:v1';
|
|
36
39
|
const START_PRINCIPAL_RECEIPT_KEY = 'flowsafe:start-principal:v1';
|
|
37
40
|
export const RUN_OWNER_RECOVERY_DELAY_MS = 60_000;
|
|
@@ -70,6 +73,7 @@ const SUSPENSION_DEADLINE_ARM_FLOOR_MS = 1_000;
|
|
|
70
73
|
// isolate that dies mid-step, which is how the next wake learns that a run
|
|
71
74
|
// left `running` has nothing driving it.
|
|
72
75
|
const RUN_LEG_KEY = 'flowsafe:run-leg:v1';
|
|
76
|
+
const QUESTION_RECOVERY_KEY = 'flowsafe:question-recovery:v1';
|
|
73
77
|
// Far past the 15 minutes a pending operation keeps a Durable Object running
|
|
74
78
|
// without a connected client. A leg frame this old belongs to an invocation
|
|
75
79
|
// that ended without settling the leg's promise — which still holds this
|
|
@@ -80,6 +84,9 @@ const RUN_LEG_MAX_AGE_MS = 7_200_000;
|
|
|
80
84
|
// deploy unregistered its workflow: the bound the suspension-deadline duty
|
|
81
85
|
// allows an unreadable run.
|
|
82
86
|
const RUN_LEG_UNSETTLED_LIMIT_MS = SUSPENSION_DEADLINE_UNREADABLE_LIMIT_MS;
|
|
87
|
+
class QuestionResumeUnresolvedError extends DoStatusError {
|
|
88
|
+
status = 503;
|
|
89
|
+
}
|
|
83
90
|
function recoveryObject(value) {
|
|
84
91
|
return ownDataCopy(value, () => {
|
|
85
92
|
throw new Error('stored run owner recovery is malformed');
|
|
@@ -322,17 +329,380 @@ export class DurableObjectRunner {
|
|
|
322
329
|
}
|
|
323
330
|
/** Caller-supplied timeout data would select the timeout branch with caller provenance. */
|
|
324
331
|
#resumeData(value) {
|
|
325
|
-
if (value !== null &&
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
WORKFLOW_REMOTE_STEP_RESULT_KEY
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
332
|
+
if (value !== null && typeof value === 'object') {
|
|
333
|
+
for (const key of [
|
|
334
|
+
SUSPENSION_TIMEOUT_RESUME_KEY,
|
|
335
|
+
WORKFLOW_REMOTE_STEP_RESULT_KEY,
|
|
336
|
+
QUESTION_ANSWER_RESUME_KEY,
|
|
337
|
+
]) {
|
|
338
|
+
if (key in value) {
|
|
339
|
+
throw new InvalidRunRequestError(`resume data must not carry the reserved '${key}' key`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
333
342
|
}
|
|
334
343
|
return value;
|
|
335
344
|
}
|
|
345
|
+
#questionDatabase() {
|
|
346
|
+
const db = this.env?.DB;
|
|
347
|
+
return isDatabaseBinding(db) ? db : undefined;
|
|
348
|
+
}
|
|
349
|
+
async #reconcileQuestions(workflowId, summary) {
|
|
350
|
+
const questions = currentQuestions(workflowId, summary);
|
|
351
|
+
if (questions.length === 0 && !isTerminalRunStatus(summary.status))
|
|
352
|
+
return;
|
|
353
|
+
const db = this.#questionDatabase();
|
|
354
|
+
if (!db) {
|
|
355
|
+
if (questions.length > 0)
|
|
356
|
+
throw new Error('question suspensions require a D1 database');
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (isTerminalRunStatus(summary.status)) {
|
|
360
|
+
await supersedeOpenQuestions(db, workflowId, summary.runId);
|
|
361
|
+
}
|
|
362
|
+
for (const question of questions) {
|
|
363
|
+
let existing = await getQuestion(db, workflowId, summary.runId, question.questionId, question);
|
|
364
|
+
if (!existing) {
|
|
365
|
+
await this.#ensureRuntime().assertExistingRunAllowed(workflowId, summary.runId);
|
|
366
|
+
await openQuestion(db, question, this.runLifecycle(this.env)?.humanNotifications);
|
|
367
|
+
existing = await getQuestion(db, workflowId, summary.runId, question.questionId, question);
|
|
368
|
+
}
|
|
369
|
+
if (existing)
|
|
370
|
+
await this.#applyEarlyAnswer(db, existing);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
async #armQuestionRecovery(workflowId, runId) {
|
|
374
|
+
const storage = this.state?.storage;
|
|
375
|
+
if (!storage)
|
|
376
|
+
return;
|
|
377
|
+
await this.#armNextAlarm(Date.now() + RUN_OWNER_RECOVERY_DELAY_MS);
|
|
378
|
+
const previous = await storage.get(QUESTION_RECOVERY_KEY);
|
|
379
|
+
if (previous?.workflowId !== workflowId || previous.runId !== runId)
|
|
380
|
+
await storage.put(QUESTION_RECOVERY_KEY, {
|
|
381
|
+
workflowId,
|
|
382
|
+
runId,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
async #applyEarlyAnswer(db, question) {
|
|
386
|
+
const early = await readEarlyAnswer(db, question);
|
|
387
|
+
if (!early)
|
|
388
|
+
return;
|
|
389
|
+
if (question.state !== 'open') {
|
|
390
|
+
await deleteEarlyAnswer(db, question);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const { workflowId, runId, questionId } = question;
|
|
394
|
+
await this.#ensureRuntime().assertExistingRunAllowed(workflowId, runId);
|
|
395
|
+
const hooks = this.runLifecycle(this.env);
|
|
396
|
+
let decision = 'denied';
|
|
397
|
+
let reason = 'forbidden';
|
|
398
|
+
let fieldIds = [];
|
|
399
|
+
let allowed = false;
|
|
400
|
+
if (early.state === 'invalid') {
|
|
401
|
+
decision = 'error';
|
|
402
|
+
reason = 'invalid stored submission';
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
try {
|
|
406
|
+
allowed =
|
|
407
|
+
(await hooks?.authorizeAnswer?.(question, early.principal, {
|
|
408
|
+
storedSubmission: true,
|
|
409
|
+
})) === true;
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
decision = 'error';
|
|
413
|
+
reason = 'authorization failed';
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (early.state === 'ready' && allowed) {
|
|
417
|
+
await this.#armQuestionRecovery(workflowId, runId);
|
|
418
|
+
try {
|
|
419
|
+
await answerQuestion(db, question, early.principal, early.answers, early.idempotencyKey);
|
|
420
|
+
await deleteEarlyAnswer(db, question);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
catch (error) {
|
|
424
|
+
if (!(error instanceof AnswersInvalidError))
|
|
425
|
+
throw error;
|
|
426
|
+
fieldIds = error.fieldIds;
|
|
427
|
+
reason = 'invalid answers';
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
await deleteEarlyAnswer(db, question);
|
|
431
|
+
const event = {
|
|
432
|
+
action: 'question.early-answer',
|
|
433
|
+
actor: early.principal?.kind === 'human'
|
|
434
|
+
? { id: early.principal.id, role: early.principal.role }
|
|
435
|
+
: null,
|
|
436
|
+
resource: `question:${workflowId}:${runId}:${questionId}`,
|
|
437
|
+
decision,
|
|
438
|
+
reason,
|
|
439
|
+
detail: { fieldIds },
|
|
440
|
+
};
|
|
441
|
+
try {
|
|
442
|
+
if (hooks?.audit) {
|
|
443
|
+
Promise.resolve(hooks.audit(event)).catch((error) => {
|
|
444
|
+
console.error('early question answer audit failed', error);
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
console.log(JSON.stringify({ type: 'audit', ...event }));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
catch (error) {
|
|
452
|
+
console.error('early question answer audit failed', error);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
async #reconcileQuestionsOnRead(workflowId, summary) {
|
|
456
|
+
const questions = currentQuestions(workflowId, summary);
|
|
457
|
+
if (questions.length === 0)
|
|
458
|
+
return summary;
|
|
459
|
+
const db = this.#questionDatabase();
|
|
460
|
+
if (db) {
|
|
461
|
+
const settled = await Promise.all(questions.map(async (question) => {
|
|
462
|
+
const existing = await getQuestion(db, workflowId, summary.runId, question.questionId, question);
|
|
463
|
+
return (existing !== null &&
|
|
464
|
+
existing.state !== 'answered' &&
|
|
465
|
+
existing.state !== 'expired' &&
|
|
466
|
+
(existing.state !== 'open' ||
|
|
467
|
+
!(await readEarlyAnswer(db, question))));
|
|
468
|
+
}));
|
|
469
|
+
if (settled.every(Boolean))
|
|
470
|
+
return summary;
|
|
471
|
+
}
|
|
472
|
+
return this.#withOperationLock(async () => {
|
|
473
|
+
const runtime = this.#ensureRuntime();
|
|
474
|
+
const current = await runtime.authoritativeStatus(workflowId, summary.runId);
|
|
475
|
+
if (!current)
|
|
476
|
+
return summary;
|
|
477
|
+
try {
|
|
478
|
+
return await this.#recoverQuestionResumes(runtime, workflowId, current, false);
|
|
479
|
+
}
|
|
480
|
+
catch (error) {
|
|
481
|
+
console.error('question status reconciliation failed', error);
|
|
482
|
+
return current;
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
async #recoverQuestionResumes(runtime, workflowId, summary, armed) {
|
|
487
|
+
await this.#reconcileQuestions(workflowId, summary);
|
|
488
|
+
const db = this.#questionDatabase();
|
|
489
|
+
let pending = false;
|
|
490
|
+
if (db) {
|
|
491
|
+
for (const question of currentQuestions(workflowId, summary)) {
|
|
492
|
+
const stored = await getQuestion(db, workflowId, summary.runId, question.questionId, question);
|
|
493
|
+
if (stored?.state !== 'answered' && stored?.state !== 'expired')
|
|
494
|
+
continue;
|
|
495
|
+
pending = true;
|
|
496
|
+
const resumed = await this.#resumeSettledQuestion(runtime, db, stored, summary, armed);
|
|
497
|
+
if (resumed === null)
|
|
498
|
+
continue;
|
|
499
|
+
return (resumed ??
|
|
500
|
+
(await runtime.authoritativeStatus(workflowId, summary.runId)) ??
|
|
501
|
+
summary);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
if (!pending)
|
|
505
|
+
await this.#clearQuestionRecovery();
|
|
506
|
+
return summary;
|
|
507
|
+
}
|
|
508
|
+
async #clearQuestionRecovery() {
|
|
509
|
+
const storage = this.state?.storage;
|
|
510
|
+
if (storage && (await storage.get(QUESTION_RECOVERY_KEY)) !== undefined)
|
|
511
|
+
await storage.delete(QUESTION_RECOVERY_KEY);
|
|
512
|
+
}
|
|
513
|
+
async #resumeSettledQuestion(runtime, db, question, summary, armed) {
|
|
514
|
+
const { workflowId, runId, questionId, stepKey, suspendedAt, resumeCount } = question;
|
|
515
|
+
const storage = this.state?.storage;
|
|
516
|
+
const recovery = await storage?.get(QUESTION_RECOVERY_KEY);
|
|
517
|
+
const retries = recovery?.workflowId === workflowId && recovery.runId === runId
|
|
518
|
+
? (recovery.retries ?? [])
|
|
519
|
+
: [];
|
|
520
|
+
const retry = retries.find((entry) => entry.questionId === questionId &&
|
|
521
|
+
sameQuestionSuspension(entry, question));
|
|
522
|
+
if (retry && retry.nextAttemptAt > Date.now())
|
|
523
|
+
return null;
|
|
524
|
+
if (recovery?.workflowId !== workflowId || recovery.runId !== runId)
|
|
525
|
+
await this.#armQuestionRecovery(workflowId, runId);
|
|
526
|
+
const settle = async () => {
|
|
527
|
+
const failed = await runtime.failQuestionResume(workflowId, runId, question);
|
|
528
|
+
if (failed) {
|
|
529
|
+
await this.#reconcileQuestions(workflowId, failed);
|
|
530
|
+
await this.#finishQuestionResume(workflowId, failed);
|
|
531
|
+
}
|
|
532
|
+
return failed;
|
|
533
|
+
};
|
|
534
|
+
if (retry?.attempts === MAX_SUSPENSION_DEADLINE_ATTEMPTS)
|
|
535
|
+
return settle();
|
|
536
|
+
try {
|
|
537
|
+
return await this.#resumeQuestion(runtime, db, question, summary, armed);
|
|
538
|
+
}
|
|
539
|
+
catch (error) {
|
|
540
|
+
if (isExecutionFenceRefusal(error) ||
|
|
541
|
+
isRunStartPendingError(error) ||
|
|
542
|
+
error instanceof RunStateUnreadableError ||
|
|
543
|
+
error instanceof RunTerminalConflictError)
|
|
544
|
+
throw error;
|
|
545
|
+
const attempts = (retry?.attempts ?? 0) + 1;
|
|
546
|
+
const exhausted = attempts >= MAX_SUSPENSION_DEADLINE_ATTEMPTS;
|
|
547
|
+
const current = currentQuestions(workflowId, summary);
|
|
548
|
+
const now = Date.now();
|
|
549
|
+
await storage?.put(QUESTION_RECOVERY_KEY, {
|
|
550
|
+
workflowId,
|
|
551
|
+
runId,
|
|
552
|
+
retries: [
|
|
553
|
+
...retries.filter((entry) => entry !== retry &&
|
|
554
|
+
current.some((candidate) => candidate.questionId === entry.questionId &&
|
|
555
|
+
sameQuestionSuspension(candidate, entry))),
|
|
556
|
+
{
|
|
557
|
+
questionId,
|
|
558
|
+
stepKey,
|
|
559
|
+
suspendedAt,
|
|
560
|
+
resumeCount,
|
|
561
|
+
attempts,
|
|
562
|
+
nextAttemptAt: exhausted
|
|
563
|
+
? now
|
|
564
|
+
: now + SUSPENSION_DEADLINE_RETRY_MS * 2 ** (attempts - 1),
|
|
565
|
+
},
|
|
566
|
+
],
|
|
567
|
+
});
|
|
568
|
+
if (exhausted)
|
|
569
|
+
return settle();
|
|
570
|
+
throw error;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
async #resumeQuestion(runtime, db, question, summary, armed) {
|
|
574
|
+
const { workflowId, runId, questionId, stepKey, suspendedAt, resumeCount } = question;
|
|
575
|
+
const expired = question.state === 'expired';
|
|
576
|
+
let answer;
|
|
577
|
+
let requestedBy;
|
|
578
|
+
let requestedByKind;
|
|
579
|
+
if (expired) {
|
|
580
|
+
answer = { questionId, answered: false, reason: 'expired' };
|
|
581
|
+
requestedBy = summary.requestedBy ?? SUSPENSION_DEADLINE_PRINCIPAL_ID;
|
|
582
|
+
requestedByKind =
|
|
583
|
+
summary.requestedByKind ??
|
|
584
|
+
(summary.requestedBy === undefined ? 'system' : 'human');
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
const { answers, answeredBy } = question;
|
|
588
|
+
if (!answers || !answeredBy)
|
|
589
|
+
throw new Error('stored question answer has no answers');
|
|
590
|
+
answer = { questionId, answered: true, answers, answeredBy };
|
|
591
|
+
requestedBy = answeredBy.id;
|
|
592
|
+
requestedByKind = answeredBy.kind;
|
|
593
|
+
}
|
|
594
|
+
let resumed;
|
|
595
|
+
try {
|
|
596
|
+
resumed = await this.#withLeg(workflowId, runId, expired ? 'deadline' : 'resume', () => runtime.resume(workflowId, runId, {
|
|
597
|
+
// The storage key is the complete step id, which can contain dots.
|
|
598
|
+
step: [stepKey],
|
|
599
|
+
expectedSuspension: {
|
|
600
|
+
suspendedAt,
|
|
601
|
+
...(resumeCount === 0 ? {} : { resumeCount }),
|
|
602
|
+
},
|
|
603
|
+
requestedBy,
|
|
604
|
+
requestedByKind,
|
|
605
|
+
resumeData: {
|
|
606
|
+
[QUESTION_ANSWER_RESUME_KEY]: answer,
|
|
607
|
+
},
|
|
608
|
+
}), armed);
|
|
609
|
+
}
|
|
610
|
+
catch (error) {
|
|
611
|
+
if (error instanceof SuspensionChangedError) {
|
|
612
|
+
await supersedeQuestion(db, question);
|
|
613
|
+
return undefined;
|
|
614
|
+
}
|
|
615
|
+
if (error instanceof RunNotSuspendedError)
|
|
616
|
+
return undefined;
|
|
617
|
+
throw error;
|
|
618
|
+
}
|
|
619
|
+
const remaining = currentQuestions(workflowId, resumed);
|
|
620
|
+
if (remaining.some((entry) => entry.questionId === questionId &&
|
|
621
|
+
sameQuestionSuspension(entry, question))) {
|
|
622
|
+
throw new QuestionResumeUnresolvedError('question resume outcome is unresolved');
|
|
623
|
+
}
|
|
624
|
+
await this.#finishQuestionResume(workflowId, resumed);
|
|
625
|
+
return resumed;
|
|
626
|
+
}
|
|
627
|
+
async #finishQuestionResume(workflowId, summary) {
|
|
628
|
+
const { runId } = summary;
|
|
629
|
+
try {
|
|
630
|
+
if (currentQuestions(workflowId, summary).length === 0)
|
|
631
|
+
await this.#clearQuestionRecovery();
|
|
632
|
+
if (await this.#reconcileSuspensionDeadlinesBestEffort(workflowId, runId, summary))
|
|
633
|
+
await this.#armNextAlarm();
|
|
634
|
+
this.#broadcastRunSummary(summary);
|
|
635
|
+
}
|
|
636
|
+
catch (error) {
|
|
637
|
+
console.error('question resume finalization failed', error);
|
|
638
|
+
}
|
|
639
|
+
await this.#reconcileApprovalsBestEffort(workflowId, runId, summary);
|
|
640
|
+
}
|
|
641
|
+
async #answerQuestion(request, db, workflowId, runId, questionId, encodedPrincipal) {
|
|
642
|
+
const principal = this.#trustedExecutionPrincipal(encodedPrincipal);
|
|
643
|
+
const body = await readJson(request);
|
|
644
|
+
const idempotencyKey = body?.idempotencyKey;
|
|
645
|
+
if (!isPathSafeId(idempotencyKey))
|
|
646
|
+
throw new InvalidRunRequestError('question answer requires an idempotency key');
|
|
647
|
+
let expected;
|
|
648
|
+
const runtime = this.#ensureRuntime();
|
|
649
|
+
const early = body?.early === true;
|
|
650
|
+
if (early) {
|
|
651
|
+
if (body?.expected !== undefined)
|
|
652
|
+
throw new InvalidRunRequestError('early answers cannot name a suspension');
|
|
653
|
+
if (!this.runLifecycle(this.env)?.authorizeAnswer)
|
|
654
|
+
return json({
|
|
655
|
+
error: 'question answer authorization is not configured',
|
|
656
|
+
reason: { code: 'ANSWER_AUTHORIZATION_UNCONFIGURED' },
|
|
657
|
+
}, 403);
|
|
658
|
+
// A running leg holds the operation lock until it suspends.
|
|
659
|
+
const summary = await runtime.authoritativeStatus(workflowId, runId);
|
|
660
|
+
if (!summary)
|
|
661
|
+
throw new UnknownRunError(workflowId, runId);
|
|
662
|
+
if (isTerminalRunStatus(summary.status))
|
|
663
|
+
throw new QuestionNotOpenError('question is not open');
|
|
664
|
+
await runtime.assertExistingRunAllowed(workflowId, runId);
|
|
665
|
+
const current = currentQuestions(workflowId, summary).find((entry) => entry.questionId === questionId);
|
|
666
|
+
if (!current) {
|
|
667
|
+
const retained = await retainEarlyAnswer(db, { workflowId, runId, questionId }, principal, body?.answers, idempotencyKey);
|
|
668
|
+
return json(retained, 202);
|
|
669
|
+
}
|
|
670
|
+
if (await getQuestion(db, workflowId, runId, questionId))
|
|
671
|
+
throw new QuestionNotOpenError('question requires a suspension fingerprint');
|
|
672
|
+
expected = current;
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
if (!isQuestionFingerprint(body?.expected))
|
|
676
|
+
throw new InvalidRunRequestError('question answer requires a suspension fingerprint');
|
|
677
|
+
expected = body.expected;
|
|
678
|
+
}
|
|
679
|
+
return this.#withOperationLock(async () => {
|
|
680
|
+
const summary = await runtime.authoritativeStatus(workflowId, runId);
|
|
681
|
+
if (!summary)
|
|
682
|
+
throw new UnknownRunError(workflowId, runId);
|
|
683
|
+
const isCurrent = currentQuestions(workflowId, summary).some((entry) => entry.questionId === questionId &&
|
|
684
|
+
sameQuestionSuspension(entry, expected));
|
|
685
|
+
if (isCurrent) {
|
|
686
|
+
await runtime.assertExistingRunAllowed(workflowId, runId);
|
|
687
|
+
await this.#reconcileQuestions(workflowId, summary);
|
|
688
|
+
}
|
|
689
|
+
const question = await getQuestion(db, workflowId, runId, questionId, expected);
|
|
690
|
+
if (!question ||
|
|
691
|
+
(question.state !== 'answered' &&
|
|
692
|
+
(question.state !== 'open' || !isCurrent)))
|
|
693
|
+
throw new QuestionNotOpenError('question is not open');
|
|
694
|
+
if (early &&
|
|
695
|
+
(await this.runLifecycle(this.env)?.authorizeAnswer?.(question, principal)) !== true)
|
|
696
|
+
return json({ error: 'forbidden' }, 403);
|
|
697
|
+
if (isCurrent)
|
|
698
|
+
await this.#armQuestionRecovery(workflowId, runId);
|
|
699
|
+
const stored = await answerQuestion(db, question, principal, body?.answers, idempotencyKey);
|
|
700
|
+
if (isCurrent &&
|
|
701
|
+
(await this.#resumeSettledQuestion(runtime, db, stored, summary, false)) === undefined)
|
|
702
|
+
throw new SuspensionChangedError(workflowId, runId, question.stepKey);
|
|
703
|
+
return json(stored);
|
|
704
|
+
});
|
|
705
|
+
}
|
|
336
706
|
#trustedExecutionPrincipal(encoded) {
|
|
337
707
|
const principal = encoded ? decodeExecutionPrincipal(encoded) : undefined;
|
|
338
708
|
if (!principal) {
|
|
@@ -423,6 +793,7 @@ export class DurableObjectRunner {
|
|
|
423
793
|
async #completeTerminalEffects(runtime, workflowId, runId, owner, result, validate) {
|
|
424
794
|
if (validate)
|
|
425
795
|
await validate();
|
|
796
|
+
await this.#reconcileQuestions(workflowId, result.summary);
|
|
426
797
|
if (result.cleanup.cleanupCompleted)
|
|
427
798
|
return result.summary;
|
|
428
799
|
const hooks = this.runLifecycle(this.env);
|
|
@@ -538,7 +909,8 @@ export class DurableObjectRunner {
|
|
|
538
909
|
// leg which stopped without reaching a durable state.
|
|
539
910
|
const recoveryDue = recoveryDueAt ??
|
|
540
911
|
((await storage.get(RUN_OWNER_RECOVERY_KEY)) !== undefined ||
|
|
541
|
-
(await storage.get(RUN_LEG_KEY)) !== undefined
|
|
912
|
+
(await storage.get(RUN_LEG_KEY)) !== undefined ||
|
|
913
|
+
(await storage.get(QUESTION_RECOVERY_KEY)) !== undefined
|
|
542
914
|
? Date.now() + RUN_OWNER_RECOVERY_DELAY_MS
|
|
543
915
|
: undefined);
|
|
544
916
|
// `now` is sampled AFTER the awaited recovery-key read above. Hoisting it
|
|
@@ -907,30 +1279,31 @@ export class DurableObjectRunner {
|
|
|
907
1279
|
console.error(`suspension deadline not armed for step '${rejection.step}' of run '${runId}': ${rejection.reason}`);
|
|
908
1280
|
}
|
|
909
1281
|
/**
|
|
910
|
-
*
|
|
911
|
-
* for
|
|
912
|
-
* deletes: the lifecycle change behind the response has already persisted,
|
|
913
|
-
* and the status and replay routes run outside the operation lock. Without a
|
|
914
|
-
* scheduled wake nothing would resume a timer, so a step this read cannot
|
|
915
|
-
* vouch for is left out and gets an approval a person can resume.
|
|
1282
|
+
* Without a scheduled wake nothing resumes a timer, so a timer this read
|
|
1283
|
+
* cannot vouch for gets an approval a person can resume.
|
|
916
1284
|
*/
|
|
917
1285
|
async #withSuspensionTimers(workflowId, summary) {
|
|
1286
|
+
let timers = [];
|
|
918
1287
|
try {
|
|
919
1288
|
// Most suspensions ask for no timer; they pay no storage read.
|
|
920
|
-
if (suspensionTimerSteps(summary, undefined).length
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
return steps.length === 0
|
|
927
|
-
? summary
|
|
928
|
-
: { ...summary, suspensionTimers: steps };
|
|
1289
|
+
if (suspensionTimerSteps(summary, undefined).length > 0) {
|
|
1290
|
+
const storage = this.state?.storage;
|
|
1291
|
+
if (storage?.getAlarm && (await storage.getAlarm()) !== null) {
|
|
1292
|
+
timers = suspensionTimerSteps(summary, ownSuspensionDeadlines(parseSuspensionDeadlineRecord(await storage.get(SUSPENSION_DEADLINE_STORAGE_KEY)), workflowId, summary.runId));
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
929
1295
|
}
|
|
930
1296
|
catch (error) {
|
|
931
1297
|
console.error('suspension timer read failed', error);
|
|
932
|
-
return summary;
|
|
933
1298
|
}
|
|
1299
|
+
const suspensionKinds = suspensionKindsOf(summary, timers);
|
|
1300
|
+
if (suspensionKinds === undefined)
|
|
1301
|
+
return summary;
|
|
1302
|
+
return {
|
|
1303
|
+
...summary,
|
|
1304
|
+
...(timers.length > 0 ? { suspensionTimers: timers } : {}),
|
|
1305
|
+
suspensionKinds,
|
|
1306
|
+
};
|
|
934
1307
|
}
|
|
935
1308
|
/**
|
|
936
1309
|
* Reconciliation is bookkeeping ABOUT a transition that has already been
|
|
@@ -1210,7 +1583,7 @@ export class DurableObjectRunner {
|
|
|
1210
1583
|
// the caller can then keep the watchdog instead of spending an abandonment
|
|
1211
1584
|
// budget on an incident, and nothing AFTER this fence can masquerade as
|
|
1212
1585
|
// one.
|
|
1213
|
-
|
|
1586
|
+
let summary = await this.#authoritativeStatusOrUnreadable(runtime, workflowId, runId);
|
|
1214
1587
|
// A read that succeeded and found NOTHING is not evidence that a signal got
|
|
1215
1588
|
// there first either — a read replica may not yet show a snapshot this
|
|
1216
1589
|
// object itself wrote — but it is evidence the store answered, and D1
|
|
@@ -1224,7 +1597,49 @@ export class DurableObjectRunner {
|
|
|
1224
1597
|
if (!summary || !isReadableRunSummary(summary)) {
|
|
1225
1598
|
throw new Error(`run '${runId}' of workflow '${workflowId}' read back absent or self-inconsistent`);
|
|
1226
1599
|
}
|
|
1227
|
-
|
|
1600
|
+
let matches = this.#suspensionFenceMatches(summary, entry);
|
|
1601
|
+
if (matches &&
|
|
1602
|
+
suspensionKindsOf(summary, [])?.[entry.step] === 'question') {
|
|
1603
|
+
await this.#reconcileQuestions(workflowId, summary);
|
|
1604
|
+
const question = currentQuestions(workflowId, summary).find((candidate) => candidate.stepKey === entry.step);
|
|
1605
|
+
const db = this.#questionDatabase();
|
|
1606
|
+
if (!question || !db)
|
|
1607
|
+
throw new Error('deadline has no valid question record');
|
|
1608
|
+
await runtime.assertExistingRunAllowed(workflowId, runId);
|
|
1609
|
+
await this.#armQuestionRecovery(workflowId, runId);
|
|
1610
|
+
const settled = await expireQuestion(db, question);
|
|
1611
|
+
if (settled?.state !== 'expired' && settled?.state !== 'answered')
|
|
1612
|
+
throw new QuestionNotOpenError('question is not open');
|
|
1613
|
+
// Once the question's outcome is durable, recovery owns its delivery.
|
|
1614
|
+
await this.#writeSuspensionDeadlines(workflowId, runId, stored, [
|
|
1615
|
+
...entriesWithoutStep(stored.entries, entry.step),
|
|
1616
|
+
tombstoned(entry),
|
|
1617
|
+
]);
|
|
1618
|
+
try {
|
|
1619
|
+
const resumed = await this.#resumeSettledQuestion(runtime, db, settled, summary, true);
|
|
1620
|
+
if (resumed === undefined)
|
|
1621
|
+
await this.#reconcileFromAuthoritativeStatus();
|
|
1622
|
+
}
|
|
1623
|
+
catch (error) {
|
|
1624
|
+
console.error('question deadline resume failed', error);
|
|
1625
|
+
}
|
|
1626
|
+
return;
|
|
1627
|
+
}
|
|
1628
|
+
try {
|
|
1629
|
+
const recovered = await this.#recoverQuestionResumes(runtime, workflowId, summary, true);
|
|
1630
|
+
if (recovered !== summary)
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
catch (error) {
|
|
1634
|
+
// A question recovery failure does not spend another step's deadline budget.
|
|
1635
|
+
console.error('question recovery before suspension deadline failed', error);
|
|
1636
|
+
const current = await this.#authoritativeStatusOrUnreadable(runtime, workflowId, runId);
|
|
1637
|
+
if (!current || !isReadableRunSummary(current))
|
|
1638
|
+
throw new RunStateUnreadableError(workflowId, runId);
|
|
1639
|
+
summary = current;
|
|
1640
|
+
matches = this.#suspensionFenceMatches(summary, entry);
|
|
1641
|
+
}
|
|
1642
|
+
if (!matches) {
|
|
1228
1643
|
// A discarded deadline must be visible: this is the branch a real signal
|
|
1229
1644
|
// reaches, and also the one a projection disagreement would reach.
|
|
1230
1645
|
console.error(`suspension deadline for step '${entry.step}' of run '${runId}' dropped: the suspension it was armed against has moved on`);
|
|
@@ -1311,13 +1726,9 @@ export class DurableObjectRunner {
|
|
|
1311
1726
|
* idFromName(`${workflowId}:${runId}`), PATH_SAFE_ID_PATTERN excludes ':'
|
|
1312
1727
|
* from both ids, and stored ids are never asserted against it (a foreign
|
|
1313
1728
|
* record's entries are simply dropped by the merge). A name that is present
|
|
1314
|
-
* but does not split into two path-safe ids skips the step entirely
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1317
|
-
* (Node stubs) the stored record supplies the ids; without either, the wake
|
|
1318
|
-
* converges to no alarm and a deadline whose record was never written is
|
|
1319
|
-
* lost. Production is safe from that last case because the exported topology
|
|
1320
|
-
* (host-kit/do-run-topology.ts) always addresses this object by idFromName.
|
|
1729
|
+
* but does not split into two path-safe ids skips the step entirely. In
|
|
1730
|
+
* Node stubs without a name, the deadline record or question recovery
|
|
1731
|
+
* marker supplies the IDs.
|
|
1321
1732
|
*/
|
|
1322
1733
|
async #reconcileFromAuthoritativeStatus(stored) {
|
|
1323
1734
|
const name = this.state?.id?.name;
|
|
@@ -1340,7 +1751,10 @@ export class DurableObjectRunner {
|
|
|
1340
1751
|
runId = stored.runId;
|
|
1341
1752
|
}
|
|
1342
1753
|
else {
|
|
1343
|
-
|
|
1754
|
+
const pending = await this.state?.storage?.get(QUESTION_RECOVERY_KEY);
|
|
1755
|
+
if (!isPathSafeId(pending?.workflowId) || !isPathSafeId(pending?.runId))
|
|
1756
|
+
return;
|
|
1757
|
+
({ workflowId, runId } = pending);
|
|
1344
1758
|
}
|
|
1345
1759
|
// Built outside the wrap below on purpose: a runtime that cannot be BUILT
|
|
1346
1760
|
// is a binding fault, not an unreadable run, and its throw belongs to the
|
|
@@ -1366,7 +1780,8 @@ export class DurableObjectRunner {
|
|
|
1366
1780
|
console.error(`run '${runId}' of workflow '${workflowId}' state read back self-inconsistent; keeping the record`);
|
|
1367
1781
|
return;
|
|
1368
1782
|
}
|
|
1369
|
-
await this.#
|
|
1783
|
+
const recovered = await this.#recoverQuestionResumes(runtime, workflowId, summary, true);
|
|
1784
|
+
await this.#reconcileSuspensionDeadlines(workflowId, runId, recovered, stored);
|
|
1370
1785
|
}
|
|
1371
1786
|
/**
|
|
1372
1787
|
* Never rethrows. workerd retries a thrown alarm(), which would re-run this
|
|
@@ -1661,11 +2076,18 @@ export class DurableObjectRunner {
|
|
|
1661
2076
|
this.#legs.add(frame);
|
|
1662
2077
|
const clearIfDurable = async (summary) => {
|
|
1663
2078
|
try {
|
|
1664
|
-
if (summary && isDurableRunStatus(summary.status))
|
|
2079
|
+
if (summary && isDurableRunStatus(summary.status)) {
|
|
2080
|
+
const current = currentQuestions(workflowId, summary).length > 0
|
|
2081
|
+
? await runtime.authoritativeStatus(workflowId, runId)
|
|
2082
|
+
: summary;
|
|
2083
|
+
if (!current)
|
|
2084
|
+
return;
|
|
2085
|
+
await this.#reconcileQuestions(workflowId, current);
|
|
1665
2086
|
await this.#clearLegMarker(frame.token);
|
|
2087
|
+
}
|
|
1666
2088
|
}
|
|
1667
2089
|
catch (error) {
|
|
1668
|
-
console.error('run leg
|
|
2090
|
+
console.error('run leg finalization failed', error);
|
|
1669
2091
|
}
|
|
1670
2092
|
};
|
|
1671
2093
|
try {
|
|
@@ -1812,7 +2234,10 @@ export class DurableObjectRunner {
|
|
|
1812
2234
|
return;
|
|
1813
2235
|
}
|
|
1814
2236
|
let summary;
|
|
1815
|
-
if (settled.kind === '
|
|
2237
|
+
if (settled.kind === 'paused') {
|
|
2238
|
+
summary = settled.summary;
|
|
2239
|
+
}
|
|
2240
|
+
else if (settled.kind === 'interrupted') {
|
|
1816
2241
|
summary = settled.summary;
|
|
1817
2242
|
console.error(JSON.stringify({
|
|
1818
2243
|
type: 'run-leg-interrupted',
|
|
@@ -1827,6 +2252,7 @@ export class DurableObjectRunner {
|
|
|
1827
2252
|
await this.#settleClosedRunInvocationsBestEffort(workflowId, runId);
|
|
1828
2253
|
}
|
|
1829
2254
|
if (summary) {
|
|
2255
|
+
await this.#reconcileQuestions(workflowId, summary);
|
|
1830
2256
|
try {
|
|
1831
2257
|
this.#broadcastRunSummary(summary);
|
|
1832
2258
|
}
|
|
@@ -1836,9 +2262,12 @@ export class DurableObjectRunner {
|
|
|
1836
2262
|
await this.#reconcileSuspensionDeadlinesBestEffort(workflowId, runId, summary);
|
|
1837
2263
|
}
|
|
1838
2264
|
else if (settled.kind === 'durable') {
|
|
2265
|
+
const current = await this.#authoritativeStatusOrUnreadable(runtime, workflowId, runId);
|
|
2266
|
+
if (current)
|
|
2267
|
+
await this.#reconcileQuestions(workflowId, current);
|
|
1839
2268
|
// A leg that stopped after persisting a suspension reached it with no
|
|
1840
2269
|
// host request to file its approval.
|
|
1841
|
-
await this.#reconcileApprovalsBestEffort(workflowId, runId);
|
|
2270
|
+
await this.#reconcileApprovalsBestEffort(workflowId, runId, current ?? undefined);
|
|
1842
2271
|
}
|
|
1843
2272
|
await this.#clearLegMarker(marker.token);
|
|
1844
2273
|
}
|
|
@@ -2140,13 +2569,14 @@ export class DurableObjectRunner {
|
|
|
2140
2569
|
: {
|
|
2141
2570
|
kind: 'result',
|
|
2142
2571
|
execution,
|
|
2143
|
-
value: await this.#withSuspensionTimers(workflowId, state.summary),
|
|
2572
|
+
value: await this.#withSuspensionTimers(workflowId, await this.#reconcileQuestionsOnRead(workflowId, state.summary)),
|
|
2144
2573
|
});
|
|
2145
2574
|
}
|
|
2146
2575
|
const summary = await runtime.status(workflowId, runId);
|
|
2147
2576
|
if (!summary)
|
|
2148
2577
|
return json({ error: 'run not found' }, 404);
|
|
2149
|
-
|
|
2578
|
+
const current = await this.#reconcileQuestionsOnRead(workflowId, summary);
|
|
2579
|
+
return json(await this.#withSuspensionTimers(workflowId, current));
|
|
2150
2580
|
}
|
|
2151
2581
|
if (request.method === 'GET' &&
|
|
2152
2582
|
segments.length === 4 &&
|
|
@@ -2157,7 +2587,7 @@ export class DurableObjectRunner {
|
|
|
2157
2587
|
const summary = await this.#authoritativeStatusOrUnreadable(this.#ensureRuntime(), workflowId, runId);
|
|
2158
2588
|
return summary === null
|
|
2159
2589
|
? new Response(null, { status: 204 })
|
|
2160
|
-
: json(summary);
|
|
2590
|
+
: json(await this.#reconcileQuestionsOnRead(workflowId, summary));
|
|
2161
2591
|
}
|
|
2162
2592
|
if (request.method === 'GET' &&
|
|
2163
2593
|
segments.length === 4 &&
|
|
@@ -2168,9 +2598,14 @@ export class DurableObjectRunner {
|
|
|
2168
2598
|
return this.#withOperationLock(async () => {
|
|
2169
2599
|
const runtime = this.#ensureRuntime();
|
|
2170
2600
|
await this.#recoverPendingRunOwner();
|
|
2171
|
-
|
|
2601
|
+
let summary = await runtime.status(workflowId, runId);
|
|
2172
2602
|
if (!summary)
|
|
2173
2603
|
return json({ error: 'run not found' }, 404);
|
|
2604
|
+
if (currentQuestions(workflowId, summary).length > 0) {
|
|
2605
|
+
const current = await runtime.authoritativeStatus(workflowId, runId);
|
|
2606
|
+
if (current)
|
|
2607
|
+
summary = await this.#recoverQuestionResumes(runtime, workflowId, current, false);
|
|
2608
|
+
}
|
|
2174
2609
|
return json(await this.#withSuspensionTimers(workflowId, summary));
|
|
2175
2610
|
});
|
|
2176
2611
|
}
|
|
@@ -2337,16 +2772,105 @@ export class DurableObjectRunner {
|
|
|
2337
2772
|
});
|
|
2338
2773
|
});
|
|
2339
2774
|
}
|
|
2775
|
+
if (action === 'questions' &&
|
|
2776
|
+
workflowId &&
|
|
2777
|
+
runId &&
|
|
2778
|
+
(request.method === 'GET' || request.method === 'POST')) {
|
|
2779
|
+
this.#assertRunIdentity(workflowId, runId);
|
|
2780
|
+
const questionId = segments[4];
|
|
2781
|
+
const db = this.#questionDatabase();
|
|
2782
|
+
if (segments.length === 4 && request.method === 'GET') {
|
|
2783
|
+
return json(db ? await listQuestions(db, workflowId, runId) : []);
|
|
2784
|
+
}
|
|
2785
|
+
if (!isQuestionId(questionId))
|
|
2786
|
+
return json({ error: 'question not found' }, 404);
|
|
2787
|
+
if (!db)
|
|
2788
|
+
return json({ error: 'question not found' }, 404);
|
|
2789
|
+
if (segments.length === 5 && request.method === 'GET') {
|
|
2790
|
+
const params = new URL(request.url).searchParams;
|
|
2791
|
+
const stepKey = params.get('stepKey');
|
|
2792
|
+
const expected = stepKey === null
|
|
2793
|
+
? undefined
|
|
2794
|
+
: {
|
|
2795
|
+
stepKey,
|
|
2796
|
+
suspendedAt: Number(params.get('suspendedAt') || Number.NaN),
|
|
2797
|
+
resumeCount: Number(params.get('resumeCount') || Number.NaN),
|
|
2798
|
+
};
|
|
2799
|
+
if (expected && !isQuestionFingerprint(expected)) {
|
|
2800
|
+
throw new InvalidRunRequestError('invalid question suspension fingerprint');
|
|
2801
|
+
}
|
|
2802
|
+
const readQuestion = () => getQuestion(db, workflowId, runId, questionId, expected);
|
|
2803
|
+
const question = expected
|
|
2804
|
+
? await this.#withOperationLock(async () => {
|
|
2805
|
+
const found = await readQuestion();
|
|
2806
|
+
if (found)
|
|
2807
|
+
return found;
|
|
2808
|
+
const current = await this.#ensureRuntime().authoritativeStatus(workflowId, runId);
|
|
2809
|
+
if (!current ||
|
|
2810
|
+
!currentQuestions(workflowId, current).some((entry) => entry.questionId === questionId &&
|
|
2811
|
+
sameQuestionSuspension(entry, expected))) {
|
|
2812
|
+
return null;
|
|
2813
|
+
}
|
|
2814
|
+
await this.#reconcileQuestions(workflowId, current);
|
|
2815
|
+
return readQuestion();
|
|
2816
|
+
})
|
|
2817
|
+
: await readQuestion();
|
|
2818
|
+
return question
|
|
2819
|
+
? json(question)
|
|
2820
|
+
: json({ error: 'question not found' }, 404);
|
|
2821
|
+
}
|
|
2822
|
+
if (segments.length === 6 &&
|
|
2823
|
+
segments[5] === 'answer' &&
|
|
2824
|
+
request.method === 'POST') {
|
|
2825
|
+
return this.#answerQuestion(request, db, workflowId, runId, questionId, encodedPrincipal);
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2340
2828
|
if (request.method === 'POST' &&
|
|
2341
2829
|
segments.length === 4 &&
|
|
2342
|
-
action === 'resume' &&
|
|
2830
|
+
(action === 'resume' || action === 'resume-paused') &&
|
|
2343
2831
|
workflowId &&
|
|
2344
2832
|
runId) {
|
|
2345
2833
|
this.#assertRunIdentity(workflowId, runId);
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2834
|
+
let resume;
|
|
2835
|
+
if (action === 'resume-paused') {
|
|
2836
|
+
const principal = this.#trustedExecutionPrincipal(encodedPrincipal);
|
|
2837
|
+
const body = await readJson(request);
|
|
2838
|
+
const expectedGeneration = parsePauseGeneration(body?.expectedGeneration);
|
|
2839
|
+
const cleared = await this.#ensureRuntime().clearPendingPause(workflowId, runId, expectedGeneration);
|
|
2840
|
+
if (cleared) {
|
|
2841
|
+
this.#broadcastRunSummary(cleared);
|
|
2842
|
+
return json(await this.#withSuspensionTimers(workflowId, cleared));
|
|
2843
|
+
}
|
|
2844
|
+
resume = (runtime) => runtime.restartPaused(workflowId, runId, principal, { expectedGeneration }, (summary) => {
|
|
2845
|
+
try {
|
|
2846
|
+
this.#broadcastRunSummary(summary);
|
|
2847
|
+
}
|
|
2848
|
+
catch (error) {
|
|
2849
|
+
console.error('restart state broadcast failed', error);
|
|
2850
|
+
}
|
|
2851
|
+
});
|
|
2852
|
+
}
|
|
2853
|
+
else {
|
|
2854
|
+
const body = (await readJson(request)) ?? {};
|
|
2855
|
+
const requestedBy = this.#requestedBy(body.requestedBy);
|
|
2856
|
+
const requestedByKind = this.#requestedByKind(body.requestedByKind);
|
|
2857
|
+
const resumeData = this.#resumeData(body.resumeData);
|
|
2858
|
+
resume = (runtime) => runtime.resume(workflowId, runId, {
|
|
2859
|
+
step: body.step,
|
|
2860
|
+
resumeData,
|
|
2861
|
+
requestedBy,
|
|
2862
|
+
requestedByKind,
|
|
2863
|
+
...(body.deadlineMs === undefined
|
|
2864
|
+
? {}
|
|
2865
|
+
: { deadlineMs: body.deadlineMs }),
|
|
2866
|
+
// The runtime validates it.
|
|
2867
|
+
...(body.expectedSuspension === undefined
|
|
2868
|
+
? {}
|
|
2869
|
+
: {
|
|
2870
|
+
expectedSuspension: body.expectedSuspension,
|
|
2871
|
+
}),
|
|
2872
|
+
});
|
|
2873
|
+
}
|
|
2350
2874
|
return this.#withOperationLock(async () => {
|
|
2351
2875
|
const runtime = this.#ensureRuntime();
|
|
2352
2876
|
// Refused here as well as inside resume(), so a fenced deployment
|
|
@@ -2356,21 +2880,7 @@ export class DurableObjectRunner {
|
|
|
2356
2880
|
await runtime.assertExistingRunAllowed(workflowId, runId);
|
|
2357
2881
|
let summary;
|
|
2358
2882
|
try {
|
|
2359
|
-
summary = await this.#withLeg(workflowId, runId, 'resume', () =>
|
|
2360
|
-
step: body.step,
|
|
2361
|
-
resumeData,
|
|
2362
|
-
requestedBy,
|
|
2363
|
-
requestedByKind,
|
|
2364
|
-
...(body.deadlineMs === undefined
|
|
2365
|
-
? {}
|
|
2366
|
-
: { deadlineMs: body.deadlineMs }),
|
|
2367
|
-
// The runtime validates it.
|
|
2368
|
-
...(body.expectedSuspension === undefined
|
|
2369
|
-
? {}
|
|
2370
|
-
: {
|
|
2371
|
-
expectedSuspension: body.expectedSuspension,
|
|
2372
|
-
}),
|
|
2373
|
-
}), false);
|
|
2883
|
+
summary = await this.#withLeg(workflowId, runId, 'resume', () => resume(runtime), false);
|
|
2374
2884
|
}
|
|
2375
2885
|
catch (error) {
|
|
2376
2886
|
// The step moved on to another suspension: file that suspension's
|
|
@@ -2390,11 +2900,26 @@ export class DurableObjectRunner {
|
|
|
2390
2900
|
console.error('resume boundary re-arm failed', error);
|
|
2391
2901
|
}
|
|
2392
2902
|
}
|
|
2903
|
+
if (action === 'resume-paused')
|
|
2904
|
+
await this.#reconcileApprovalsBestEffort(workflowId, runId, summary);
|
|
2393
2905
|
// Broadcast the post-resume authoritative summary.
|
|
2394
2906
|
this.#broadcastRunSummary(summary);
|
|
2395
2907
|
return json(await this.#withSuspensionTimers(workflowId, summary));
|
|
2396
2908
|
});
|
|
2397
2909
|
}
|
|
2910
|
+
if (request.method === 'POST' &&
|
|
2911
|
+
segments.length === 4 &&
|
|
2912
|
+
action === 'pause' &&
|
|
2913
|
+
workflowId &&
|
|
2914
|
+
runId) {
|
|
2915
|
+
this.#assertRunIdentity(workflowId, runId);
|
|
2916
|
+
const principal = this.#trustedExecutionPrincipal(encodedPrincipal);
|
|
2917
|
+
const options = parsePauseRunOptions(await readJson(request));
|
|
2918
|
+
// The active leg holds the operation lock until it sees the pause.
|
|
2919
|
+
const summary = await this.#ensureRuntime().requestPause(workflowId, runId, principal, options);
|
|
2920
|
+
this.#broadcastRunSummary(summary);
|
|
2921
|
+
return json(await this.#withSuspensionTimers(workflowId, summary));
|
|
2922
|
+
}
|
|
2398
2923
|
if (request.method === 'POST' &&
|
|
2399
2924
|
segments.length === 4 &&
|
|
2400
2925
|
(action === 'terminate' || action === 'terminate-replay') &&
|