@lostgradient/weft 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/README.md +2 -2
  2. package/dist/cli/generated/operation-catalog.snapshot.json +207 -80
  3. package/dist/cli/generated/operation-client.generated.d.ts +120 -43
  4. package/dist/client/http-client-storage.js +1 -1
  5. package/dist/core/engine/activity-reconciliation.d.ts +2 -2
  6. package/dist/core/engine/activity-reconciliation.js +12 -8
  7. package/dist/core/engine/async-activity-completion.js +12 -4
  8. package/dist/core/engine/async-activity-records.d.ts +11 -1
  9. package/dist/core/engine/async-activity-records.js +4 -3
  10. package/dist/core/engine/attributes-tags.d.ts +11 -1
  11. package/dist/core/engine/attributes-tags.js +4 -3
  12. package/dist/core/engine/bulk-operations-purge.js +5 -2
  13. package/dist/core/engine/bulk-operations.js +17 -5
  14. package/dist/core/engine/callback-creators-bundles.js +1 -1
  15. package/dist/core/engine/callback-creators-core.js +4 -2
  16. package/dist/core/engine/checkpoint-io.js +1 -1
  17. package/dist/core/engine/child-workflow.js +2 -1
  18. package/dist/core/engine/completed-review-storage.js +1 -1
  19. package/dist/core/engine/condition-waiters.d.ts +45 -3
  20. package/dist/core/engine/condition-waiters.js +12 -3
  21. package/dist/core/engine/disposal.js +6 -0
  22. package/dist/core/engine/engine-internal-types.d.ts +12 -4
  23. package/dist/core/engine/fenced-write.d.ts +46 -15
  24. package/dist/core/engine/fenced-write.js +39 -6
  25. package/dist/core/engine/finalizer-state.d.ts +3 -1
  26. package/dist/core/engine/handle-result.d.ts +49 -1
  27. package/dist/core/engine/handle-result.js +95 -20
  28. package/dist/core/engine/index.d.ts +13 -5
  29. package/dist/core/engine/index.js +155 -5
  30. package/dist/core/engine/inline-launch-queue.js +9 -0
  31. package/dist/core/engine/internals.d.ts +73 -0
  32. package/dist/core/engine/invoke-update-handler.d.ts +19 -0
  33. package/dist/core/engine/invoke-update-handler.js +7 -0
  34. package/dist/core/engine/lease-deposition.d.ts +132 -0
  35. package/dist/core/engine/lease-deposition.js +31 -0
  36. package/dist/core/engine/lease-errors.d.ts +80 -9
  37. package/dist/core/engine/lease-errors.js +28 -2
  38. package/dist/core/engine/lifecycle/resume.js +3 -1
  39. package/dist/core/engine/lifecycle/standalone-claim-acquire.d.ts +33 -0
  40. package/dist/core/engine/lifecycle/standalone-claim-acquire.js +25 -0
  41. package/dist/core/engine/lifecycle/start-commit.js +20 -6
  42. package/dist/core/engine/lifecycle/transition.d.ts +15 -1
  43. package/dist/core/engine/lifecycle/transition.js +6 -3
  44. package/dist/core/engine/operations-activity.js +1 -1
  45. package/dist/core/engine/operations-time.js +21 -7
  46. package/dist/core/engine/owner-side-signal-poll.d.ts +132 -0
  47. package/dist/core/engine/owner-side-signal-poll.js +22 -0
  48. package/dist/core/engine/owner-side-update-poll.d.ts +116 -0
  49. package/dist/core/engine/owner-side-update-poll.js +22 -0
  50. package/dist/core/engine/ownership-bootstrap.d.ts +280 -0
  51. package/dist/core/engine/ownership-bootstrap.js +89 -0
  52. package/dist/core/engine/ownership-mode-marker.d.ts +82 -0
  53. package/dist/core/engine/ownership-mode-marker.js +52 -0
  54. package/dist/core/engine/ownership-options.d.ts +22 -6
  55. package/dist/core/engine/ownership-options.js +42 -12
  56. package/dist/core/engine/pending-updates.js +35 -24
  57. package/dist/core/engine/queries.d.ts +70 -0
  58. package/dist/core/engine/queries.js +58 -4
  59. package/dist/core/engine/schedules.js +1 -1
  60. package/dist/core/engine/signals.d.ts +11 -1
  61. package/dist/core/engine/signals.js +11 -7
  62. package/dist/core/engine/sleep-timer-acknowledgements.d.ts +68 -0
  63. package/dist/core/engine/sleep-timer-acknowledgements.js +19 -0
  64. package/dist/core/engine/storage-io.d.ts +58 -10
  65. package/dist/core/engine/storage-io.js +27 -9
  66. package/dist/core/engine/termination/cleanup.d.ts +5 -4
  67. package/dist/core/engine/termination/complete.js +23 -4
  68. package/dist/core/engine/termination/finalizer-claim.js +4 -4
  69. package/dist/core/engine/termination/state-commit-callbacks.d.ts +15 -0
  70. package/dist/core/engine/termination/state-commit-callbacks.js +0 -0
  71. package/dist/core/engine/termination/suspend.js +1 -1
  72. package/dist/core/engine/update-validation.d.ts +28 -0
  73. package/dist/core/engine/update-validation.js +37 -0
  74. package/dist/core/engine/updates.d.ts +17 -16
  75. package/dist/core/engine/updates.js +24 -47
  76. package/dist/core/engine/wake-ownership-check.d.ts +86 -0
  77. package/dist/core/engine/wake-ownership-check.js +19 -0
  78. package/dist/core/engine/wake-ownership-guard.d.ts +46 -0
  79. package/dist/core/engine/wake-ownership-guard.js +23 -0
  80. package/dist/core/engine/workflow-claim-codec.d.ts +64 -0
  81. package/dist/core/engine/workflow-claim-codec.js +44 -0
  82. package/dist/core/engine/workflow-claim-cooldown.d.ts +60 -0
  83. package/dist/core/engine/workflow-claim-cooldown.js +17 -0
  84. package/dist/core/engine/workflow-claim-fold.d.ts +82 -0
  85. package/dist/core/engine/workflow-claim-fold.js +53 -0
  86. package/dist/core/engine/workflow-claim-metrics.d.ts +131 -0
  87. package/dist/core/engine/workflow-claim-metrics.js +62 -0
  88. package/dist/core/engine/workflow-claim-reclaim-scan.d.ts +96 -0
  89. package/dist/core/engine/workflow-claim-reclaim-scan.js +66 -0
  90. package/dist/core/engine/workflow-claim-reclaim-target.d.ts +107 -0
  91. package/dist/core/engine/workflow-claim-reclaim-target.js +155 -0
  92. package/dist/core/engine/workflow-claim-registry.d.ts +227 -0
  93. package/dist/core/engine/workflow-claim-registry.js +198 -0
  94. package/dist/core/engine/workflow-claim-renewal-interval.d.ts +50 -0
  95. package/dist/core/engine/workflow-claim-renewal-interval.js +132 -0
  96. package/dist/core/engine/workflow-claim-renewal-subpasses.d.ts +265 -0
  97. package/dist/core/engine/workflow-claim-renewal-subpasses.js +104 -0
  98. package/dist/core/engine/workflow-claim-renewal-task.d.ts +159 -0
  99. package/dist/core/engine/workflow-claim-renewal-task.js +65 -0
  100. package/dist/core/engine/workflow-claim-transitions.d.ts +186 -0
  101. package/dist/core/engine/workflow-claim-transitions.js +120 -0
  102. package/dist/core/inline-execution-strategy.d.ts +20 -0
  103. package/dist/core/inline-execution-strategy.js +15 -4
  104. package/dist/core/scheduler/scheduler-class.js +3 -3
  105. package/dist/core/scheduler/timer-sources.d.ts +13 -0
  106. package/dist/core/scheduler/timer-sources.js +1 -1
  107. package/dist/core/types/options.d.ts +17 -1
  108. package/dist/core/weft-error.d.ts +1 -1
  109. package/dist/core/weft-error.js +2 -1
  110. package/dist/http.js +2 -2
  111. package/dist/index.d.ts +2 -1
  112. package/dist/index.js +8 -0
  113. package/dist/indexeddb.js +1 -1
  114. package/dist/json-schema.js +3 -3
  115. package/dist/observability/index.js +2 -2
  116. package/dist/server/operations/get-task-diagnostics.d.ts +115 -25
  117. package/dist/server/operations/get-task-diagnostics.js +93 -7
  118. package/dist/storage/auto.js +1 -1
  119. package/dist/storage/bun-sql.js +115 -2
  120. package/dist/storage/compressed-storage.js +1 -1
  121. package/dist/storage/http.js +1 -1
  122. package/dist/storage/index.d.ts +38 -35
  123. package/dist/storage/interface.d.ts +87 -125
  124. package/dist/storage/interface.js +1 -1
  125. package/dist/storage/key-encoding.d.ts +51 -0
  126. package/dist/storage/key-encoding.js +13 -0
  127. package/dist/storage/key-prefixes.d.ts +1 -1
  128. package/dist/storage/key-prefixes.js +3 -0
  129. package/dist/storage/lmdb.js +1 -1
  130. package/dist/storage/memory.js +1 -1
  131. package/dist/storage/neon.js +4 -4
  132. package/dist/storage/node-sqlite.js +115 -2
  133. package/dist/storage/ownership-keys.d.ts +55 -0
  134. package/dist/storage/ownership-keys.js +6 -0
  135. package/dist/storage/postgres.js +4 -4
  136. package/dist/storage/resolve.js +1 -1
  137. package/dist/storage/scoped-storage.js +1 -1
  138. package/dist/storage/testing.js +1 -1
  139. package/dist/storage/turso.js +2 -2
  140. package/dist/version.d.ts +1 -1
  141. package/dist/version.js +1 -1
  142. package/dist/web-extension.js +1 -1
  143. package/dist/worker/protocol.js +1 -1
  144. package/package.json +3 -2
@@ -10,7 +10,8 @@ import { assertWorkflowTagCount, coerceStartWorkflowTags } from "../start-workfl
10
10
  import { buildWorkflowTagIndexOperations, normalizeWorkflowTags } from "../workflow-tags.js";
11
11
  import { WorkflowNotFoundError } from "./errors.js";
12
12
  import {
13
- commitFencedWorkflowStateOperations,
13
+ commitExternalTerminalWorkflowStateOperations,
14
+ commitSelfWorkflowStateOperations,
14
15
  runSerializedWorkflowStateWrite
15
16
  } from "./storage-io.js";
16
17
  import {
@@ -63,7 +64,7 @@ export function buildTerminalWorkflowIndexOperations(previousState, nextState) {
63
64
  });
64
65
  return operations;
65
66
  }
66
- export async function updateWorkflowState(internals, workflowId, updates, options = {}) {
67
+ export async function updateWorkflowState(internals, workflowId, updates, category, options = {}) {
67
68
  return await runSerializedWorkflowStateWrite(internals, workflowId, async () => {
68
69
  const bytes = await internals.storage.get(KEYS.workflow(workflowId));
69
70
  if (!bytes)
@@ -76,7 +77,7 @@ export async function updateWorkflowState(internals, workflowId, updates, option
76
77
  ...updates,
77
78
  updatedAt
78
79
  }, additionalOperations = options.buildAdditionalOperations?.(state, updatedAt) ?? [];
79
- await commitFencedWorkflowStateOperations(internals, state, [
80
+ await (category === "self" ? commitSelfWorkflowStateOperations : commitExternalTerminalWorkflowStateOperations)(internals, state, [
80
81
  ...buildTerminalWorkflowIndexOperations(state, updated),
81
82
  { type: "put", key: KEYS.workflow(workflowId), value: encode(updated) },
82
83
  ...buildWorkflowVisibilityIndexTransition(workflowId, state, updated).batchOps,
@@ -14,6 +14,7 @@ import { commitFencedEngineWrite } from "./fenced-write.js";
14
14
  import { streamWorkflowStates } from "./listing.js";
15
15
  import { decodeScheduleRunMetadata } from "./schedule-run-metadata.js";
16
16
  import { createTerminalCleanupTimerId } from "./state-utilities.js";
17
+ import { buildExternalTerminalRotationFragment } from "./storage-io.js";
17
18
  import {
18
19
  decodeWorkflowState,
19
20
  isTerminalWorkflowStatus,
@@ -132,8 +133,10 @@ function getWorkflowRetentionDeadline(internals, state) {
132
133
  return state.updatedAt + retentionMs;
133
134
  }
134
135
  export async function purgeWorkflow(internals, state, cleanupWaiters) {
135
- const deleteOperations = await collectWorkflowPurgeDeleteOperations(internals, state);
136
- await commitFencedEngineWrite(internals, deleteOperations, [], () => Error(`Purge commit for workflow "${state.id}" lost its precondition.`));
136
+ const deleteOperations = await collectWorkflowPurgeDeleteOperations(internals, state), rotation = await buildExternalTerminalRotationFragment(internals, state.id), operations = [...deleteOperations, ...rotation.operations];
137
+ await commitFencedEngineWrite(internals, null, operations, rotation.conditions, () => {
138
+ return Error(`Purge commit for workflow "${state.id}" lost its precondition.`);
139
+ });
137
140
  clearPurgedWorkflowInMemoryState(internals, state.id, cleanupWaiters);
138
141
  }
139
142
  export async function collectWorkflowPurgeDeleteOperations(internals, state) {
@@ -31,6 +31,11 @@ import { BULK_OPERATION_BATCH_SIZE } from "./listing.js";
31
31
  import { createTerminalCleanupTimerId } from "./state-utilities.js";
32
32
  import { loadWorkflowState, runSerializedWorkflowStateWrite } from "./storage-io.js";
33
33
  import { decodeWorkflowState, isTerminalWorkflowStatus } from "./validation.js";
34
+ import {
35
+ commitWithWorkflowClaimFold,
36
+ prepareWorkflowClaimFold,
37
+ throwWorkflowClaimUnavailable
38
+ } from "./workflow-claim-fold.js";
34
39
  import { buildWorkflowConcurrencyStartOperations } from "./workflow-concurrency.js";
35
40
  import { buildWorkflowVisibilityIndexTransition } from "./workflow-indexes.js";
36
41
  export { purgeInternal, TERMINAL_CLEANUP_DELAY_MS } from "./bulk-operations-purge.js";
@@ -171,17 +176,24 @@ async function reactivateFailedWorkflowFromCheckpointSerialized(internals, workf
171
176
  }),
172
177
  ...concurrencyStartOperations?.operations ?? []
173
178
  ], conditions = concurrencyStartOperations?.conditions ?? [];
174
- if (await commitFailedWorkflowReactivation(internals, operations, conditions))
179
+ if (await commitFailedWorkflowReactivation(internals, workflowId, operations, conditions))
175
180
  return {
176
181
  terminalCleanupTimerId: currentState.terminalCleanupToken === void 0 ? void 0 : createTerminalCleanupTimerId(!1, currentState.terminalCleanupToken)
177
182
  };
178
183
  }
179
184
  throw Error(`Workflow concurrency admission for "${lastConcurrencyStateKey ?? workflowId}" changed too many times while retrying failed workflow "${workflowId}"`);
180
185
  }
181
- async function commitFailedWorkflowReactivation(internals, operations, conditions) {
182
- if (conditions.length > 0)
183
- requireStorageCapability(internals.storage, "conditionalBatch", "retry failed workflow");
184
- return commitFencedEngineWriteAllowingPreconditionFailure(internals, operations, conditions);
186
+ async function commitFailedWorkflowReactivation(internals, workflowId, operations, conditions) {
187
+ const claimFold = await prepareWorkflowClaimFold(internals, workflowId);
188
+ if (claimFold === void 0) {
189
+ if (conditions.length > 0)
190
+ requireStorageCapability(internals.storage, "conditionalBatch", "retry failed workflow");
191
+ return commitFencedEngineWriteAllowingPreconditionFailure(internals, workflowId, operations, conditions);
192
+ }
193
+ const result = await commitWithWorkflowClaimFold(internals, claimFold, operations, conditions, "retry failed workflow claim acquisition");
194
+ if (result.status === "committed")
195
+ return !0;
196
+ return result.claimConflict ? throwWorkflowClaimUnavailable(internals, workflowId) : !1;
185
197
  }
186
198
  function buildReactivatedWorkflowState(internals, state) {
187
199
  const reactivatedState = {
@@ -154,7 +154,7 @@ export function createUpdateCallbacks(engine) {
154
154
  async function persistCoordinatedUpdateResponse(engine, workflowId, updateName, updateId, idempotencyKey, value) {
155
155
  const internals = getInternals(engine), responseOperations = internals.updateCoordinator.buildResponseOperations(updateId, workflowId, value, void 0, idempotencyKey);
156
156
  try {
157
- await commitFencedEngineWrite(internals, responseOperations, [], () => Error(`Coordinated update response for workflow "${workflowId}" lost its CAS race.`));
157
+ await commitFencedEngineWrite(internals, workflowId, responseOperations, [], () => Error(`Coordinated update response for workflow "${workflowId}" lost its CAS race.`));
158
158
  engine.dispatchEvent(new UpdateCompletedEvent(updateId, workflowId, updateName, value));
159
159
  broadcastFromInternals(internals, { type: "update:completed", workflowId, updateId }, createBroadcastCallbacks(engine));
160
160
  } catch (error) {
@@ -18,7 +18,8 @@ import {
18
18
  import { resolveWorkflowTypeTarget } from "./registration.js";
19
19
  import { cleanupReviews } from "./reviews.js";
20
20
  import {
21
- commitFencedWorkflowStateOperations,
21
+ commitExternalTerminalWorkflowStateOperations,
22
+ commitSelfWorkflowStateOperations,
22
23
  loadWorkflowState,
23
24
  runSerializedWorkflowStateWrite
24
25
  } from "./storage-io.js";
@@ -94,7 +95,8 @@ export function createTerminationCallbacksWith(engine, handleScheduledWorkflowTe
94
95
  handleScheduledWorkflowTerminal,
95
96
  loadWorkflowState: (workflowId) => loadWorkflowState(getInternals(engine), workflowId),
96
97
  runSerializedWorkflowStateWrite: (workflowId, writeOperation) => runSerializedWorkflowStateWrite(getInternals(engine), workflowId, writeOperation),
97
- commitWorkflowStateOperations: (state, operations, options) => commitFencedWorkflowStateOperations(getInternals(engine), state, operations, options),
98
+ commitSelfWorkflowStateOperations: (state, operations, options) => commitSelfWorkflowStateOperations(getInternals(engine), state, operations, options),
99
+ commitExternalTerminalWorkflowStateOperations: (state, operations, options) => commitExternalTerminalWorkflowStateOperations(getInternals(engine), state, operations, options),
98
100
  cleanupReviews: (workflowId) => cleanupReviews(getInternals(engine), workflowId)
99
101
  };
100
102
  }
@@ -122,7 +122,7 @@ async function commitCheckpoint(internals, workflowId, operation, commit, callba
122
122
  if (pendingSideEffects !== void 0)
123
123
  commit.operations.push(...pendingSideEffects.operations);
124
124
  const storageSupportsConditionalBatch = internals.storage.capabilities().conditionalBatch, sideEffectConditions = checkpointSideEffectConditions(pendingSideEffects, storageSupportsConditionalBatch), conditions = buildCheckpointCommitConditions(workflowId, commit, storageSupportsConditionalBatch, sideEffectConditions);
125
- await commitFencedEngineWrite(internals, commit.operations, conditions, () => {
125
+ await commitFencedEngineWrite(internals, workflowId, commit.operations, conditions, () => {
126
126
  return Error(`Checkpoint commit for workflow "${workflowId}" lost its CAS race against a newer checkpoint.`);
127
127
  });
128
128
  if (pendingSideEffects !== void 0)
@@ -5,6 +5,7 @@ import {
5
5
  registerChildCancellationHandler
6
6
  } from "./child-workflow-cancellation.js";
7
7
  import { WorkflowAlreadyExistsError } from "./errors.js";
8
+ import { getGeneratorOwnedWorkflowResultPromise } from "./handle-result.js";
8
9
  import { encodedValuesEqual } from "./state-utilities.js";
9
10
  export async function processChildWorkflowOperation(internals, workflowId, operation, callbacks) {
10
11
  return callbacks.runOperationWithResult(workflowId, operation, () => executeChildWorkflow(internals, workflowId, operation, assertChildWorkflowNestingDepth(internals, workflowId), callbacks));
@@ -83,7 +84,7 @@ export async function executeChildWorkflow(internals, workflowId, operation, cur
83
84
  registerChildCancellationHandler(internals, workflowId, childHandle.id, callbacks);
84
85
  return createChildWorkflowHandleReference(childHandle.id);
85
86
  }
86
- return childHandle.result();
87
+ return getGeneratorOwnedWorkflowResultPromise(internals, childHandle.id, workflowId);
87
88
  }, composedInterceptor = callbacks.getComposedWorkflowInterceptor();
88
89
  if (!composedInterceptor)
89
90
  return executeChild();
@@ -35,7 +35,7 @@ export async function listCompletedReviewsFromStorage(storage, filter) {
35
35
  }
36
36
  export async function persistCompletedReviewRecord(internals, reviewKey, reviewData, decisionResult) {
37
37
  const completedReview = toCompletedReviewEntry(reviewData, decisionResult);
38
- await commitFencedEngineWrite(internals, [
38
+ await commitFencedEngineWrite(internals, reviewData.workflowId, [
39
39
  {
40
40
  type: "put",
41
41
  key: completedReviewStorageKey(reviewData.workflowId, reviewData.reviewId),
@@ -1,14 +1,56 @@
1
1
  import type { EngineInternals } from './internals.ts';
2
+ import { type WakeOwnershipDecision } from './wake-ownership-guard.ts';
2
3
  /**
3
4
  * Re-drive every in-process `ctx.waitUntil` waiter registered for a workflow.
4
- * Called after an inline `onUpdate` handler runs (`tryInlineUpdateHandler`),
5
- * since the handler may have mutated the workflow-local state a predicate reads.
6
- * Each call wakes the parked `processWaitConditionOperation`, which re-evaluates
5
+ * Called after an inline `onUpdate` handler runs (`tryInlineUpdateHandler`)
6
+ * since the handler may have mutated the workflow-local state a predicate
7
+ * reads. Wakes the parked `processWaitConditionOperation`, which re-evaluates
7
8
  * its predicate against the (possibly mutated) state.
8
9
  *
9
10
  * Weft signals are pull-only (`ctx.waitForSignal`) and run no state-mutating
10
11
  * handler, so signal delivery is intentionally NOT a re-drive trigger — `onUpdate`
11
12
  * is the push path. Lives in its own module so `updates.ts` can call it without
12
13
  * an import cycle through `operations-coordination.ts`.
14
+ *
15
+ * **`wakeOwnershipCheck` under `ownership: 'workflow-lease'`.** This is a
16
+ * claim-requiring wake site (ADR 0002's `wait-condition` kind): resolving
17
+ * `conditionWaiters` drives a parked generator turn. This function's only
18
+ * remaining callers — `updates.ts` and `pending-updates.ts` — invoke it from
19
+ * an `onUpdate` handler with no associated durable timer to protect, so they
20
+ * call it synchronously with no `await` and must stay that way: this
21
+ * function's signature stays `void`, never `Promise<void>`, so neither
22
+ * becomes a floating-promise lint violation. Under `'none'`/`'lease'`
23
+ * (`workflowClaimRegistry === null`) the resolver is looked up and called
24
+ * synchronously — byte-identical to before this check existed. Under
25
+ * `'workflow-lease'` the ownership re-read runs as a fire-and-forget async
26
+ * check, and the resolver is looked up fresh (never captured before the
27
+ * await) once that check settles, so a newer waiter registered in the
28
+ * meantime is never resolved by a stale reference.
29
+ *
30
+ * The durable-timer-driven wake (a `wait-condition` deadline timer firing)
31
+ * does NOT use this function — see {@link notifyConditionWaitersForTimerFire}
32
+ * below. That caller feeds directly into the `Scheduler`'s "was this fire
33
+ * processed" decision, so it cannot be fire-and-forget the way an
34
+ * `onUpdate`-driven poke safely can.
13
35
  */
14
36
  export declare function notifyConditionWaiters(internals: EngineInternals, workflowId: string): void;
37
+ /**
38
+ * Timer-driven counterpart to {@link notifyConditionWaiters}, used only by
39
+ * `operations-time.ts`'s `resolveConditionTimer` when a durable
40
+ * `ctx.waitUntil()` deadline timer fires under `ownership: 'workflow-lease'`.
41
+ *
42
+ * Unlike the `onUpdate`-driven path above, this caller feeds directly into
43
+ * the `Scheduler`'s `#processSelectedTimer`: once the returned promise
44
+ * settles, the caller decides whether the Scheduler may treat this fire as
45
+ * "processed" and durably delete the timer key. A fire-and-forget check here
46
+ * (mirroring `notifyConditionWaiters`'s `'workflow-lease'` branch) would let
47
+ * the Scheduler delete the timer before the ownership decision — and, on a
48
+ * `'proceed'`, the wake dispatch itself — has actually happened, stranding a
49
+ * parked workflow with no deterministic timeout record if the process exits
50
+ * or disposes in that window. So this returns the raw {@link
51
+ * WakeOwnershipDecision} instead of swallowing it: the caller uses a
52
+ * `'discard'` to decide retain-vs-collect via
53
+ * `sleep-timer-acknowledgements.ts`'s `resolveDiscardedTimerDisposition`,
54
+ * the same disposal policy the `sleep` wake kind shares this hazard with.
55
+ */
56
+ export declare function notifyConditionWaitersForTimerFire(internals: EngineInternals, workflowId: string): Promise<WakeOwnershipDecision>;
@@ -1,5 +1,14 @@
1
+ import { confirmWakeOwnership } from "./wake-ownership-guard.js";
1
2
  export function notifyConditionWaiters(internals, workflowId) {
2
- const resolve = internals.conditionWaiters.get(workflowId);
3
- if (resolve)
4
- resolve();
3
+ if (internals.workflowClaimRegistry === null) {
4
+ internals.conditionWaiters.get(workflowId)?.();
5
+ return;
6
+ }
7
+ confirmWakeOwnership(internals, workflowId, "wait-condition").then((decision) => decision === "proceed" ? internals.conditionWaiters.get(workflowId)?.() : void 0);
8
+ }
9
+ export async function notifyConditionWaitersForTimerFire(internals, workflowId) {
10
+ const decision = await confirmWakeOwnership(internals, workflowId, "wait-condition");
11
+ if (decision === "proceed")
12
+ internals.conditionWaiters.get(workflowId)?.();
13
+ return decision;
5
14
  }
@@ -8,6 +8,11 @@ function settleSleepResolverReadyWaitersForTesting(internals) {
8
8
  notifyReady();
9
9
  internals.sleepResolverReadyWaitersForTesting?.clear();
10
10
  }
11
+ function clearPendingResultPollTimers(internals) {
12
+ for (const timer of internals.pendingResultPollTimers)
13
+ clearTimeout(timer);
14
+ internals.pendingResultPollTimers.clear();
15
+ }
11
16
  export function disposeEngine(internals) {
12
17
  internals.disposed = !0;
13
18
  internals.alertManager?.[Symbol.dispose]();
@@ -52,6 +57,7 @@ export function disposeEngine(internals) {
52
57
  for (const controller of internals.pendingWebhooks)
53
58
  controller.abort();
54
59
  internals.pendingWebhooks.clear();
60
+ clearPendingResultPollTimers(internals);
55
61
  internals.sleepResolvers.clear();
56
62
  internals.sleepResolversByWorkflow.clear();
57
63
  settleSleepResolverReadyWaitersForTesting(internals);
@@ -46,17 +46,25 @@ export interface ResolvedOptions {
46
46
  /** Heartbeat interval (ms) for the second-instance detector when enabled. */
47
47
  secondInstanceHeartbeatIntervalMs: number;
48
48
  /**
49
- * Single-writer ownership posture. `'none'` recovers immediately (infra owns
50
- * mutual exclusion); `'lease'` acquires a storage lease before recovery. The
51
- * lease tuning fields below are only meaningful when this is `'lease'`.
49
+ * Ownership posture. `'none'` recovers immediately (infra owns mutual
50
+ * exclusion); `'lease'` acquires a single store-wide storage lease before
51
+ * recovery; `'workflow-lease'` fences execution per workflow instead
52
+ * (ADR 0002), letting multiple engines share one durable store. The lease
53
+ * tuning fields below are only meaningful when this is `'lease'`; the
54
+ * workflow-claim tuning fields are only meaningful when this is
55
+ * `'workflow-lease'`.
52
56
  */
53
- ownershipMode: 'none' | 'lease';
57
+ ownershipMode: 'none' | 'lease' | 'workflow-lease';
54
58
  /** Lease time-to-live (ms) when `ownershipMode` is `'lease'`. */
55
59
  leaseTtlMs: number;
56
60
  /** Lease renewal interval (ms) when `ownershipMode` is `'lease'`. */
57
61
  leaseRenewIntervalMs: number;
58
62
  /** Boot-time lease acquisition wait window (ms) when `ownershipMode` is `'lease'`. */
59
63
  leaseWaitTimeoutMs: number;
64
+ /** Per-workflow claim time-to-live (ms) when `ownershipMode` is `'workflow-lease'`. */
65
+ workflowClaimTtlMs: number;
66
+ /** Per-workflow claim renewal interval (ms) when `ownershipMode` is `'workflow-lease'`. */
67
+ workflowClaimRenewIntervalMs: number;
60
68
  getNow: () => number;
61
69
  /**
62
70
  * Re-provides the non-serialized per-run `services` value on recovery; `null`
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Step-2 epoch fencing for `ownership: 'lease'` (issue #470).
2
+ * Step-2 epoch fencing for `ownership: 'lease'` (issue #470), extended by ADR
3
+ * 0002 to per-workflow fencing under `ownership: 'workflow-lease'`.
3
4
  *
4
5
  * Step 1 made a rolling deploy a clean lease handoff but added no correctness
5
6
  * backstop: a deposed zombie instance (GC pause, partition) that emerged after
@@ -15,6 +16,20 @@
15
16
  * from silently spinning on swallowed write losses, surface the deposition to the
16
17
  * operator, and release resources. They are not what makes the system safe.
17
18
  *
19
+ * ADR 0002 adds a SECOND, independent fencing token: `wf-owner-epoch:<workflowId>`.
20
+ * Every caller of {@link commitFencedEngineWrite} / {@link
21
+ * commitFencedEngineWriteAllowingPreconditionFailure} now states, via the
22
+ * required `workflowId` parameter, whether the write is scoped to one
23
+ * workflow's execution (pass its id) or is engine-scoped/cross-workflow (pass
24
+ * `null`) — an optional parameter would let a call site silently forget the
25
+ * fence, which is exactly the correctness hole this mechanism exists to close.
26
+ * Under `ownership: 'workflow-lease'` with a non-null `workflowId`, the write
27
+ * is fenced on THAT workflow's claim epoch instead of the global lease epoch;
28
+ * losing that fence deposes only that one workflow (warn + throw), never the
29
+ * whole engine. Under `'lease'` and `'none'`, and under `'workflow-lease'`
30
+ * with `workflowId: null`, behavior is byte-for-byte unchanged from Step 2 —
31
+ * `workflowId` is only consulted in the one new branch.
32
+ *
18
33
  * This module is allow-listed for import only from `src/core/engine/**`.
19
34
  */
20
35
  import type { BatchOperation, ConditionalBatchCondition } from '../../storage/interface.ts';
@@ -31,37 +46,53 @@ import type { EngineInternals } from './internals.ts';
31
46
  * `pauseSchedule`, `resumeSchedule`, `cancelSchedule`, and `updateSchedule`) would
32
47
  * reach `resolveFenceEpochOrHalt` with no held epoch and be misreported as a
33
48
  * deposition (warn + teardown) rather than the true "lease not held yet"
34
- * condition. A no-op under `ownership: 'none'`.
49
+ * condition. A no-op under `ownership: 'none'` and under `ownership:
50
+ * 'workflow-lease'` — the analogous "claim not held yet" guard for a
51
+ * workflow-scoped write is a later stage's concern (folding `acquire()` into
52
+ * the enabling write), not this global-lease-only assertion.
35
53
  */
36
54
  export declare function assertLeaseHeldForEngineWork(internals: EngineInternals): void;
37
55
  /**
38
- * Commit an engine-generator-owned durable write, fenced on the held lease epoch
39
- * under `ownership: 'lease'` (a byte-for-byte no-op under `ownership: 'none'`). A
40
- * lost CAS race throws the caller-supplied `onLostRace` error so existing retry
41
- * semantics apply; a deposition halts the engine and throws
42
- * {@link EngineDeposedError}. The helper owns the batch-vs-conditionalBatch
43
- * decision pass plain operations plus whatever base conditions you already need.
56
+ * Commit an engine-generator-owned durable write, fenced on the applicable
57
+ * epoch: the global lease epoch under `ownership: 'lease'`, this workflow's
58
+ * claim epoch under `ownership: 'workflow-lease'` when `workflowId` is
59
+ * non-null, or under `'none'` and every other `workflowId`/mode
60
+ * combination — byte-for-byte unfenced. A lost CAS race throws the
61
+ * caller-supplied `onLostRace` error so existing retry semantics apply; a
62
+ * deposition halts (the engine under global `'lease'`, or just this one
63
+ * workflow under `'workflow-lease'`) and throws {@link EngineDeposedError}.
64
+ * The helper owns the batch-vs-conditionalBatch decision — pass plain
65
+ * operations plus whatever base conditions you already need.
44
66
  *
45
- * @param internals - the engine internals (ownership mode, lease manager, deposed flag)
67
+ * @param internals - the engine internals (ownership mode, lease manager,
68
+ * workflow claim registry, deposed flag)
69
+ * @param workflowId - the workflow this write is scoped to, fenced on that
70
+ * workflow's claim epoch under `ownership: 'workflow-lease'`; or `null` for
71
+ * an engine-scoped/cross-workflow write, which is never fenced on a
72
+ * per-workflow claim regardless of ownership mode. Required — there is no
73
+ * default — so every call site states its scope explicitly.
46
74
  * @param operations - the durable operations to commit atomically
47
75
  * @param baseConditions - CAS conditions the caller already requires (may be empty)
48
76
  * @param onLostRace - builds the error thrown on a same-epoch lost CAS race
49
77
  */
50
- export declare function commitFencedEngineWrite(internals: EngineInternals, operations: BatchOperation[], baseConditions: ConditionalBatchCondition[], onLostRace: () => Error): Promise<void>;
78
+ export declare function commitFencedEngineWrite(internals: EngineInternals, workflowId: string | null, operations: BatchOperation[], baseConditions: ConditionalBatchCondition[], onLostRace: () => Error): Promise<void>;
51
79
  /**
52
80
  * Like {@link commitFencedEngineWrite}, but the caller treats a base-precondition
53
81
  * failure as a legitimate outcome rather than an error — used by the idempotent
54
82
  * start path, where a `false` means a concurrent same-key caller already wrote the
55
83
  * record (resolve to the existing run) rather than "retry". Returns `true` when the
56
84
  * batch committed and `false` when a base condition failed. Deposition is still a
57
- * hard halt: if the epoch condition is the one that failed, this drives
58
- * {@link handleDeposition} and throws {@link EngineDeposedError} — a deposed engine
59
- * must never report a precondition-failure the caller would read as "already exists"
60
- * and silently move on.
85
+ * hard halt: if the epoch condition is the one that failed, this drives the
86
+ * applicable deposition path and throws {@link EngineDeposedError} — a deposed
87
+ * engine (or a deposed single workflow, under `workflow-lease`) must never report a
88
+ * precondition-failure the caller would read as "already exists" and silently
89
+ * move on.
61
90
  *
62
91
  * @param internals - the engine internals
92
+ * @param workflowId - see {@link commitFencedEngineWrite} — the workflow this
93
+ * write is scoped to, or `null` for an engine-scoped write
63
94
  * @param operations - the durable operations to commit atomically
64
95
  * @param baseConditions - the caller's required CAS conditions (non-empty in
65
96
  * practice — this path is for conditional starts, which always carry one)
66
97
  */
67
- export declare function commitFencedEngineWriteAllowingPreconditionFailure(internals: EngineInternals, operations: BatchOperation[], baseConditions: ConditionalBatchCondition[]): Promise<boolean>;
98
+ export declare function commitFencedEngineWriteAllowingPreconditionFailure(internals: EngineInternals, workflowId: string | null, operations: BatchOperation[], baseConditions: ConditionalBatchCondition[]): Promise<boolean>;
@@ -1,6 +1,6 @@
1
1
  import { KEYS, storageConditionalBatch } from "../../storage/interface.js";
2
2
  import { decodeEpoch } from "./lease-codec.js";
3
- import { handleDeposition } from "./lease-deposition.js";
3
+ import { emitWorkflowClaimLostWarning, handleDeposition } from "./lease-deposition.js";
4
4
  import { EngineDeposedError, EngineLeaseNotHeldError } from "./lease-errors.js";
5
5
  export function assertLeaseHeldForEngineWork(internals) {
6
6
  if (internals.options.ownershipMode !== "lease")
@@ -8,7 +8,9 @@ export function assertLeaseHeldForEngineWork(internals) {
8
8
  if (!(!internals.deposed && internals.leaseManager !== null && internals.leaseManager.currentEpochBytes() !== null))
9
9
  throw new EngineLeaseNotHeldError;
10
10
  }
11
- async function fencedCommit(internals, operations, baseConditions) {
11
+ async function fencedCommit(internals, workflowId, operations, baseConditions) {
12
+ if (internals.options.ownershipMode === "workflow-lease" && workflowId !== null)
13
+ return fencedCommitForWorkflow(internals, workflowId, operations, baseConditions);
12
14
  const epochBytes = resolveFenceEpochOrHalt(internals);
13
15
  if (epochBytes === null) {
14
16
  if (baseConditions.length === 0) {
@@ -41,12 +43,30 @@ function resolveFenceEpochOrHalt(internals) {
41
43
  }
42
44
  return epochBytes;
43
45
  }
44
- export async function commitFencedEngineWrite(internals, operations, baseConditions, onLostRace) {
45
- if (await fencedCommit(internals, operations, baseConditions) === "lost-race")
46
+ async function fencedCommitForWorkflow(internals, workflowId, operations, baseConditions) {
47
+ const epochBytes = internals.workflowClaimRegistry?.currentEpochBytes(workflowId) ?? null;
48
+ if (epochBytes === null)
49
+ haltWorkflowClaim(workflowId);
50
+ const conditions = [
51
+ ...baseConditions,
52
+ { key: KEYS.workflowOwnerEpoch(workflowId), expectedValue: epochBytes }
53
+ ];
54
+ if (await storageConditionalBatch(internals.storage, conditions, operations))
55
+ return "committed";
56
+ if (await isWorkflowDeposed(internals, workflowId, epochBytes))
57
+ haltWorkflowClaim(workflowId);
58
+ return "lost-race";
59
+ }
60
+ function haltWorkflowClaim(workflowId) {
61
+ emitWorkflowClaimLostWarning(workflowId);
62
+ throw new EngineDeposedError(workflowId);
63
+ }
64
+ export async function commitFencedEngineWrite(internals, workflowId, operations, baseConditions, onLostRace) {
65
+ if (await fencedCommit(internals, workflowId, operations, baseConditions) === "lost-race")
46
66
  throw onLostRace();
47
67
  }
48
- export async function commitFencedEngineWriteAllowingPreconditionFailure(internals, operations, baseConditions) {
49
- return await fencedCommit(internals, operations, baseConditions) === "committed";
68
+ export async function commitFencedEngineWriteAllowingPreconditionFailure(internals, workflowId, operations, baseConditions) {
69
+ return await fencedCommit(internals, workflowId, operations, baseConditions) === "committed";
50
70
  }
51
71
  function epochBytesEqual(a, b) {
52
72
  if (b === null || a.byteLength !== b.byteLength)
@@ -69,3 +89,16 @@ async function isDeposed(internals, heldEpochBytes) {
69
89
  return !0;
70
90
  return !epochBytesEqual(heldEpochBytes, currentEpochRaw);
71
91
  }
92
+ async function isWorkflowDeposed(internals, workflowId, heldEpochBytes) {
93
+ let currentEpochRaw;
94
+ try {
95
+ currentEpochRaw = await internals.storage.get(KEYS.workflowOwnerEpoch(workflowId));
96
+ } catch {
97
+ return !0;
98
+ }
99
+ if (currentEpochRaw === null)
100
+ return !0;
101
+ if (decodeEpoch(currentEpochRaw) === null)
102
+ return !0;
103
+ return !epochBytesEqual(heldEpochBytes, currentEpochRaw);
104
+ }
@@ -16,7 +16,9 @@ import type { EngineInternals } from './internals.ts';
16
16
  * the next checkpoint still flushes the staged op in the terminal batch, so the
17
17
  * finalizer always sees the resource id. The staged op inherits the lease-epoch
18
18
  * fence (#470) for free, since checkpoint and terminal commits route through
19
- * `commitFencedWorkflowStateOperations`.
19
+ * `commitSelfWorkflowStateOperations` (completion) or
20
+ * `commitExternalTerminalWorkflowStateOperations` (cancel/timeout/suspend —
21
+ * ADR 0002), both in `storage-io.ts`.
20
22
  *
21
23
  * Oversized payloads are rejected before staging (the same hostile-input guard
22
24
  * activity results use). A call made once the workflow is already terminalizing
@@ -4,5 +4,53 @@ import type { EngineInternals } from './internals.ts';
4
4
  export declare function createWorkflowHandleWithResultPromise(internals: EngineInternals, workflowId: string): WorkflowHandle;
5
5
  export declare function createWorkflowResultWaiter(internals: EngineInternals, workflowId: string): WorkflowResultWaiter;
6
6
  export declare function getWorkflowResultPromise(internals: EngineInternals, workflowId: string): Promise<unknown>;
7
- export declare function bootstrapWorkflowResultResolver(internals: EngineInternals, workflowId: string, waiter: WorkflowResultWaiter): Promise<void>;
7
+ /**
8
+ * Result promise for a parent generator parked on `ctx.startChild()`'s
9
+ * default `parentClosePolicy: 'await'` — the ONLY intended caller (see
10
+ * `child-workflow.ts`'s `executeChildWorkflow`, which must call this instead
11
+ * of the plain `childHandle.result()` / `handle.result()` path). Identical to
12
+ * {@link getWorkflowResultPromise} except the waiter this settles is marked
13
+ * generator-owned for `parentWorkflowId`: under `ownership: 'workflow-lease'`
14
+ * a settle attempt first confirms `parentWorkflowId` still holds the claim
15
+ * generation it parked under (`confirmWakeOwnership`, `'child-completion'`),
16
+ * fencing duplicate generator advancement (WFT-79 F1) — see
17
+ * {@link bootstrapWorkflowResultResolver}. Top-level, non-generator callers
18
+ * must keep using {@link getWorkflowResultPromise}, which stays unfenced by
19
+ * design: cross-engine `handle.result()` polling has no generator to
20
+ * duplicate and must remain settleable from durable state alone.
21
+ *
22
+ * If `internals.resultResolvers` already holds a waiter for `workflowId` —
23
+ * e.g. an observational `handle.result()` caller got there first — that
24
+ * SHARED waiter is untouched: {@link fenceResultOnParentGeneration} wraps
25
+ * only THIS caller's derived promise, so the observational caller's own
26
+ * promise settles normally from the shared waiter regardless of the parent's
27
+ * claim generation. See that function's doc for why fencing lives on the
28
+ * parent's view instead of the shared waiter itself.
29
+ */
30
+ export declare function getGeneratorOwnedWorkflowResultPromise(internals: EngineInternals, workflowId: string, parentWorkflowId: string): Promise<unknown>;
31
+ /**
32
+ * Settle result waiters for workflows this engine does not own, whose terminal
33
+ * transition lands on another engine and so never touches this engine's
34
+ * in-memory resolver map.
35
+ *
36
+ * Exported for `backgroundTasks: 'manual'`, where no timer runs and the host
37
+ * drives every background step through an awaited `runMaintenance()`.
38
+ */
39
+ export declare function pollPendingCrossEngineResultWaiters(internals: EngineInternals): Promise<void>;
40
+ /**
41
+ * Outcome of one {@link bootstrapWorkflowResultResolver} attempt, used by
42
+ * this file's callers to decide whether to keep polling:
43
+ *
44
+ * - `'settled'`: the waiter was resolved or rejected.
45
+ * - `'pending'`: no terminal result yet (still running, or a transient read
46
+ * failure under a guaranteed-retry ownership mode). Keep polling.
47
+ *
48
+ * A generator-owned caller's discarded parent generation needs no outcome
49
+ * here at all: `fenceResultOnParentGeneration` withholds settlement on that
50
+ * caller's own derived promise, independent of whether the SHARED waiter
51
+ * this function settles ever resolves.
52
+ */
53
+ type ResultResolutionOutcome = 'settled' | 'pending';
54
+ export declare function bootstrapWorkflowResultResolver(internals: EngineInternals, workflowId: string, waiter: WorkflowResultWaiter): Promise<ResultResolutionOutcome>;
8
55
  export declare function cacheHandle(internals: EngineInternals, workflowId: string, handle: WorkflowHandle): void;
56
+ export {};