@lostgradient/weft 0.2.0 → 0.3.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 (207) hide show
  1. package/README.md +47 -22
  2. package/dist/cli/generated/operation-client.generated.d.ts +28 -1
  3. package/dist/cli/generated/operation-client.generated.js +2 -0
  4. package/dist/cli-main.js +79 -79
  5. package/dist/client/handle-delegation.d.ts +4 -0
  6. package/dist/client/handle-delegation.js +6 -0
  7. package/dist/client/http-client-requests.d.ts +2 -0
  8. package/dist/client/http-client-requests.js +3 -0
  9. package/dist/client/http-client.d.ts +4 -1
  10. package/dist/client/http-client.js +9 -1
  11. package/dist/client/interface.d.ts +57 -2
  12. package/dist/client/local.d.ts +4 -1
  13. package/dist/client/local.js +7 -0
  14. package/dist/client/start-body.d.ts +7 -1
  15. package/dist/client/start-body.js +13 -4
  16. package/dist/core/codec/extension-codec.js +4 -2
  17. package/dist/core/codec/index.d.ts +1 -0
  18. package/dist/core/codec/index.js +1 -0
  19. package/dist/core/codec/serializer-registry.d.ts +122 -0
  20. package/dist/core/codec/serializer-registry.js +51 -0
  21. package/dist/core/context/index.d.ts +10 -0
  22. package/dist/core/context/index.js +3 -0
  23. package/dist/core/context/internals.d.ts +10 -0
  24. package/dist/core/context/internals.js +5 -1
  25. package/dist/core/context/run-operation.d.ts +16 -3
  26. package/dist/core/context/run-operation.js +16 -7
  27. package/dist/core/context/speculative-child.js +2 -0
  28. package/dist/core/context/types.d.ts +6 -0
  29. package/dist/core/engine/bulk-operations-purge.js +1 -0
  30. package/dist/core/engine/bulk-operations.js +1 -1
  31. package/dist/core/engine/callback-creators-bundles.js +2 -1
  32. package/dist/core/engine/callback-creators-core.js +2 -1
  33. package/dist/core/engine/construction.d.ts +1 -1
  34. package/dist/core/engine/construction.js +15 -3
  35. package/dist/core/engine/disposal.js +12 -0
  36. package/dist/core/engine/engine-create-types.d.ts +0 -14
  37. package/dist/core/engine/engine-internal-types.d.ts +17 -0
  38. package/dist/core/engine/engine-leak-warnings.d.ts +6 -0
  39. package/dist/core/engine/engine-leak-warnings.js +4 -0
  40. package/dist/core/engine/engine-runtime-helpers.d.ts +17 -0
  41. package/dist/core/engine/engine-runtime-helpers.js +26 -5
  42. package/dist/core/engine/errors.d.ts +74 -0
  43. package/dist/core/engine/errors.js +25 -1
  44. package/dist/core/engine/handle-result.js +1 -1
  45. package/dist/core/engine/handles.d.ts +89 -40
  46. package/dist/core/engine/handles.js +25 -27
  47. package/dist/core/engine/index.d.ts +122 -4
  48. package/dist/core/engine/index.js +82 -5
  49. package/dist/core/engine/inline-launch-queue.d.ts +14 -0
  50. package/dist/core/engine/inline-launch-queue.js +32 -7
  51. package/dist/core/engine/internals.d.ts +26 -10
  52. package/dist/core/engine/lifecycle/fork-helpers.js +1 -7
  53. package/dist/core/engine/lifecycle/persist.js +5 -20
  54. package/dist/core/engine/lifecycle/recovered-services.d.ts +45 -0
  55. package/dist/core/engine/lifecycle/recovered-services.js +34 -0
  56. package/dist/core/engine/lifecycle/resume.js +33 -5
  57. package/dist/core/engine/lifecycle/shared.d.ts +8 -0
  58. package/dist/core/engine/lifecycle/start-batch.js +23 -12
  59. package/dist/core/engine/lifecycle/start-commit.d.ts +47 -0
  60. package/dist/core/engine/lifecycle/start-commit.js +27 -0
  61. package/dist/core/engine/lifecycle/start-exec.d.ts +30 -2
  62. package/dist/core/engine/lifecycle/start-exec.js +38 -0
  63. package/dist/core/engine/lifecycle/start-or-signal-resolution.d.ts +79 -0
  64. package/dist/core/engine/lifecycle/start-or-signal-resolution.js +60 -0
  65. package/dist/core/engine/lifecycle/start-or-signal.d.ts +45 -0
  66. package/dist/core/engine/lifecycle/start-or-signal.js +141 -0
  67. package/dist/core/engine/lifecycle/start.d.ts +3 -3
  68. package/dist/core/engine/lifecycle/start.js +42 -37
  69. package/dist/core/engine/lifecycle.d.ts +3 -2
  70. package/dist/core/engine/lifecycle.js +9 -2
  71. package/dist/core/engine/listing.js +1 -1
  72. package/dist/core/engine/operations-data.d.ts +16 -0
  73. package/dist/core/engine/operations-data.js +6 -0
  74. package/dist/core/engine/operations-time.d.ts +3 -2
  75. package/dist/core/engine/operations-time.js +6 -1
  76. package/dist/core/engine/persisted-data-version.d.ts +5 -9
  77. package/dist/core/engine/persisted-data-version.js +4 -5
  78. package/dist/core/engine/schedule-handle.d.ts +45 -0
  79. package/dist/core/engine/schedule-handle.js +26 -0
  80. package/dist/core/engine/schedules.d.ts +1 -1
  81. package/dist/core/engine/schedules.js +7 -3
  82. package/dist/core/engine/second-instance-detector.d.ts +96 -0
  83. package/dist/core/engine/second-instance-detector.js +108 -0
  84. package/dist/core/engine/signals.d.ts +22 -0
  85. package/dist/core/engine/signals.js +15 -0
  86. package/dist/core/engine/termination/cleanup.d.ts +25 -0
  87. package/dist/core/engine/termination/cleanup.js +21 -1
  88. package/dist/core/engine/termination/complete.js +4 -3
  89. package/dist/core/engine/termination/suspend.d.ts +68 -0
  90. package/dist/core/engine/termination/suspend.js +41 -0
  91. package/dist/core/engine/termination.d.ts +4 -2
  92. package/dist/core/engine/termination.js +2 -0
  93. package/dist/core/engine/validation.js +25 -1
  94. package/dist/core/engine/workflow-feed.d.ts +5 -3
  95. package/dist/core/events/event-map.d.ts +2 -1
  96. package/dist/core/events/workflow-events.d.ts +23 -0
  97. package/dist/core/events/workflow-events.js +9 -0
  98. package/dist/core/inline-execution-strategy.d.ts +5 -0
  99. package/dist/core/inline-execution-strategy.js +2 -1
  100. package/dist/core/list-filter-validation.js +2 -1
  101. package/dist/core/start-workflow-validation.d.ts +22 -0
  102. package/dist/core/start-workflow-validation.js +11 -1
  103. package/dist/core/step-context.d.ts +10 -6
  104. package/dist/core/step-context.js +7 -15
  105. package/dist/core/types/activity.d.ts +6 -3
  106. package/dist/core/types/identity.d.ts +8 -1
  107. package/dist/core/types/launch-metadata.d.ts +33 -0
  108. package/dist/core/types/launch-metadata.js +0 -0
  109. package/dist/core/types/message-handles.d.ts +25 -0
  110. package/dist/core/types/options.d.ts +90 -7
  111. package/dist/core/types/reviews.d.ts +2 -1
  112. package/dist/core/types/services-resolution.d.ts +47 -0
  113. package/dist/core/types/services-resolution.js +0 -0
  114. package/dist/core/types/state.d.ts +11 -11
  115. package/dist/core/types/workflow-builder.d.ts +5 -4
  116. package/dist/core/types/workflow-context.d.ts +25 -0
  117. package/dist/core/types/workflow-function.d.ts +17 -0
  118. package/dist/core/types/workflow-snapshot.d.ts +29 -0
  119. package/dist/core/types/workflow-snapshot.js +0 -0
  120. package/dist/core/types.d.ts +3 -0
  121. package/dist/core/types.js +3 -0
  122. package/dist/core/weft-error.d.ts +46 -14
  123. package/dist/core/weft-error.js +12 -1
  124. package/dist/diagnostics/doctor.js +6 -3
  125. package/dist/diagnostics/format.js +2 -2
  126. package/dist/diagnostics/types.d.ts +1 -0
  127. package/dist/diagnostics/version-check.js +6 -4
  128. package/dist/index.d.ts +10 -5
  129. package/dist/index.js +11 -2
  130. package/dist/json-schema.js +3 -3
  131. package/dist/mcp/cli.js +35 -35
  132. package/dist/mcp/list-filter.js +2 -1
  133. package/dist/mcp/session.js +1 -0
  134. package/dist/observability/index.js +2 -2
  135. package/dist/server/handler.js +30 -30
  136. package/dist/server/index.js +33 -33
  137. package/dist/server/interactive-operations.js +1 -0
  138. package/dist/server/operations/resume-workflow.js +2 -2
  139. package/dist/server/operations/start-or-signal-workflow.d.ts +39 -0
  140. package/dist/server/operations/start-or-signal-workflow.js +140 -0
  141. package/dist/server/operations/start-workflow-options.d.ts +32 -0
  142. package/dist/server/operations/start-workflow-options.js +63 -0
  143. package/dist/server/operations/start-workflow.js +7 -69
  144. package/dist/server/operations/suspend-workflow.d.ts +13 -0
  145. package/dist/server/operations/suspend-workflow.js +36 -0
  146. package/dist/server/rest-binding.d.ts +18 -7
  147. package/dist/server/rest-bindings.js +12 -0
  148. package/dist/server/runtime/task-dispatch.js +5 -3
  149. package/dist/server/runtime/task-polling.d.ts +16 -2
  150. package/dist/server/runtime/task-polling.js +20 -5
  151. package/dist/server/runtime/websocket-worker.js +8 -0
  152. package/dist/server/serve-internals.d.ts +8 -0
  153. package/dist/server/serve-internals.js +4 -2
  154. package/dist/server/task-state.d.ts +8 -0
  155. package/dist/service-worker/index.js +28 -28
  156. package/dist/storage/capabilities.d.ts +10 -2
  157. package/dist/storage/capabilities.js +2 -2
  158. package/dist/storage/http.js +2 -2
  159. package/dist/storage/index.d.ts +7 -1
  160. package/dist/storage/indexeddb.js +1 -1
  161. package/dist/storage/interface.d.ts +40 -0
  162. package/dist/storage/interface.js +1 -1
  163. package/dist/storage/key-prefixes.d.ts +1 -1
  164. package/dist/storage/key-prefixes.js +3 -0
  165. package/dist/storage/lmdb.js +1 -1
  166. package/dist/storage/memory.js +1 -1
  167. package/dist/storage/neon-value-mapping.d.ts +47 -0
  168. package/dist/storage/neon-value-mapping.js +11 -0
  169. package/dist/storage/neon.d.ts +108 -0
  170. package/dist/storage/neon.js +10 -0
  171. package/dist/storage/node-sqlite-loader.d.ts +71 -0
  172. package/dist/storage/node-sqlite-loader.js +41 -0
  173. package/dist/storage/node-sqlite.d.ts +1 -19
  174. package/dist/storage/node-sqlite.js +38 -32
  175. package/dist/storage/postgres-key-value-queries.d.ts +79 -0
  176. package/dist/storage/postgres-key-value-queries.js +63 -0
  177. package/dist/storage/resolve.d.ts +2 -165
  178. package/dist/storage/resolve.js +1 -1
  179. package/dist/storage/scoped-storage.js +1 -1
  180. package/dist/storage/storage-configuration.d.ts +209 -0
  181. package/dist/storage/storage-configuration.js +0 -0
  182. package/dist/storage/text-value-store.d.ts +13 -10
  183. package/dist/storage/turso.js +2 -2
  184. package/dist/storage/typed-storage.js +1 -1
  185. package/dist/storage/web-extension.js +1 -1
  186. package/dist/testing/event-loop.d.ts +36 -2
  187. package/dist/testing/index.d.ts +31 -1
  188. package/dist/testing/index.js +33 -33
  189. package/dist/version.d.ts +1 -1
  190. package/dist/version.js +1 -1
  191. package/dist/worker/index.js +9 -5
  192. package/dist/worker/long-poll.js +4 -0
  193. package/dist/worker/protocol-messages.d.ts +20 -0
  194. package/dist/worker/protocol-schemas.d.ts +32 -0
  195. package/dist/worker/protocol-schemas.js +8 -4
  196. package/dist/worker/protocol-task-result.d.ts +28 -0
  197. package/dist/worker/protocol-task-result.js +76 -0
  198. package/dist/worker/protocol.d.ts +4 -15
  199. package/dist/worker/protocol.js +1 -1
  200. package/dist/worker/registry/fair-share.d.ts +29 -0
  201. package/dist/worker/registry/fair-share.js +30 -0
  202. package/dist/worker/registry/routing.d.ts +18 -0
  203. package/dist/worker/registry/routing.js +14 -0
  204. package/dist/worker/registry/types.d.ts +7 -0
  205. package/dist/worker/registry.d.ts +16 -1
  206. package/dist/worker/registry.js +24 -36
  207. package/package.json +17 -4
@@ -0,0 +1,79 @@
1
+ import type { StartOrSignalSignal } from '../../types.ts';
2
+ import { type WorkflowHandle } from '../handles.ts';
3
+ import type { EngineInternals } from '../internals.ts';
4
+ import { type LifecycleCallbacks } from './shared.ts';
5
+ /**
6
+ * Callbacks `startOrSignal` needs beyond the lifecycle set: a way to deliver a
7
+ * signal to an already-running workflow through the full engine signal path
8
+ * (interceptors, events, parked-run wakeups). Supplied by the engine so the
9
+ * "signal an existing non-terminal run" branch reuses `engine.signal` with the
10
+ * same `signalId` the create batch would have used.
11
+ */
12
+ export type StartOrSignalCallbacks = LifecycleCallbacks & {
13
+ signalExistingWorkflow: (workflowId: string, signalName: string, payload: unknown, signalId: string) => Promise<void>;
14
+ };
15
+ /** The value stored at `KEYS.startIdempotency(key)`: the workflow the key created. */
16
+ export type StartIdempotencyMapping = {
17
+ workflowId: string;
18
+ };
19
+ /** Resolve an existing workflow id for an idempotency key, if one was created. */
20
+ export declare function resolveIdempotencyKeyWorkflowId(internals: EngineInternals, idempotencyKey: string): Promise<string | undefined>;
21
+ /**
22
+ * Resolve a key-mapped workflow id to a handle id, asserting its record still
23
+ * exists. The `start-idem:` mapping is permanent — it survives BOTH terminal
24
+ * cleanup AND purge/retention (those reclaim the workflow record, never the
25
+ * `start-idem:` keyspace) — so a present mapping whose record is gone means the
26
+ * key is spent: a fresh create would fail the still-present mapping CAS and strand
27
+ * the caller. Surface {@link IdempotencyKeyPurgedError} instead of handing back a
28
+ * handle to a vanished run. Shared by the synchronous mapping hit and the
29
+ * post-race winner lookup so both reject a purged key identically.
30
+ */
31
+ export declare function resolveExistingRunOrThrowPurged(internals: EngineInternals, workflowId: string): Promise<string>;
32
+ /**
33
+ * Resolve a caller-`id` create-race loss without conflating an in-memory
34
+ * reservation with a durable record. A loser collides on the winner's
35
+ * `pendingStarts` reservation (start.ts) BEFORE the winner commits, so the bare
36
+ * collision proves nothing about whether a run will exist.
37
+ *
38
+ * Read the winner's record FIRST: this catches a winner that has already committed
39
+ * but is still non-terminal before it can complete (a fast workflow consumes its
40
+ * create-batch signal and finishes the moment it is driven — reading immediately
41
+ * resolves it instead of racing it to a terminal-conflict). Only when the record is
42
+ * absent do we wait for the reservation to clear and read once more to discriminate:
43
+ *
44
+ * - **record present** — the winner committed: signal it (or conflict if terminal)
45
+ * and return the handle.
46
+ * - **record absent after the reservation clears** — the winner aborted before
47
+ * committing (storage failure, oversized payload, throwing start interceptor): no
48
+ * run exists, so return `undefined` and let the caller retry its own create.
49
+ */
50
+ export declare function resolveCallerIdWinnerOrRetry(internals: EngineInternals, winnerId: string, signalSpec: StartOrSignalSignal, signalId: string, callbacks: StartOrSignalCallbacks): Promise<WorkflowHandle | undefined>;
51
+ /**
52
+ * For a workflow that already exists: signal it if non-terminal, conflict if
53
+ * terminal. Returns the handle on a successful signal, or `undefined` when the
54
+ * workflow record is not present (so the caller falls through to create).
55
+ */
56
+ export declare function signalOrConflictExistingWorkflow(internals: EngineInternals, workflowId: string, signalSpec: StartOrSignalSignal, signalId: string, callbacks: StartOrSignalCallbacks): Promise<WorkflowHandle | undefined>;
57
+ /**
58
+ * Signal a KEYED race winner, bounded-retrying when its record is not yet readable.
59
+ * The keyed winner commits its record atomically with the `start-idem:` mapping, so
60
+ * the record is guaranteed to exist — but the loser may read before the commit
61
+ * settles, so a short delay between reads lets it land. (Caller-`id` winners can
62
+ * abort before committing and are handled by `resolveCallerIdWinnerOrRetry`, not
63
+ * here.)
64
+ *
65
+ * After {@link WINNER_RESOLUTION_MAX_ATTEMPTS} reads with no record, the record is
66
+ * absent for a committed-with-mapping winner only because it was purged: re-read
67
+ * the mapping, and if it still resolves to this exact `winnerId` the key is spent —
68
+ * throw {@link IdempotencyKeyPurgedError}. A mapping that now resolves to a
69
+ * DIFFERENT id (or vanished) cannot prove this winner was purged, so it falls
70
+ * through to the invariant throw rather than mislabelling external keyspace
71
+ * mutation as a spent key.
72
+ */
73
+ export declare function resolveWinnerWithSignal(internals: EngineInternals, winnerId: string, signalSpec: StartOrSignalSignal, signalId: string, callbacks: StartOrSignalCallbacks, idempotencyKey: string): Promise<WorkflowHandle>;
74
+ /**
75
+ * Read the winning workflow id from the idempotency mapping after a lost CAS. The
76
+ * mapping must exist once any caller's create commits; its absence means the
77
+ * `start-idem:` keyspace was mutated externally.
78
+ */
79
+ export declare function requireWinnerId(internals: EngineInternals, idempotencyKey: string): Promise<string>;
@@ -0,0 +1,60 @@
1
+ import { sleep } from "../../../runtime/portable.js";
2
+ import { KEYS } from "../../../storage/interface.js";
3
+ import { decode } from "../../codec.js";
4
+ import { IdempotencyKeyPurgedError, StartOrSignalConflictError } from "../errors.js";
5
+ import { loadWorkflowState } from "../storage-io.js";
6
+ import { isTerminalWorkflowStatus } from "../validation.js";
7
+ export async function resolveIdempotencyKeyWorkflowId(internals, idempotencyKey) {
8
+ const bytes = await internals.storage.get(KEYS.startIdempotency(idempotencyKey));
9
+ if (bytes === null)
10
+ return;
11
+ return decode(bytes).workflowId;
12
+ }
13
+ export async function resolveExistingRunOrThrowPurged(internals, workflowId) {
14
+ if (await loadWorkflowState(internals, workflowId) === null)
15
+ throw new IdempotencyKeyPurgedError(workflowId);
16
+ return workflowId;
17
+ }
18
+ const RESERVATION_CLEAR_MAX_ATTEMPTS = 5, RESERVATION_CLEAR_RETRY_DELAY_MS = 5;
19
+ async function awaitReservationCleared(internals, workflowId) {
20
+ for (let attempt = 0;attempt < RESERVATION_CLEAR_MAX_ATTEMPTS; attempt += 1) {
21
+ if (!internals.pendingStarts.has(workflowId))
22
+ return;
23
+ await sleep(RESERVATION_CLEAR_RETRY_DELAY_MS);
24
+ }
25
+ }
26
+ export async function resolveCallerIdWinnerOrRetry(internals, winnerId, signalSpec, signalId, callbacks) {
27
+ const resolved = await signalOrConflictExistingWorkflow(internals, winnerId, signalSpec, signalId, callbacks);
28
+ if (resolved !== void 0)
29
+ return resolved;
30
+ await awaitReservationCleared(internals, winnerId);
31
+ return signalOrConflictExistingWorkflow(internals, winnerId, signalSpec, signalId, callbacks);
32
+ }
33
+ export async function signalOrConflictExistingWorkflow(internals, workflowId, signalSpec, signalId, callbacks) {
34
+ const state = await loadWorkflowState(internals, workflowId);
35
+ if (state === null)
36
+ return;
37
+ if (isTerminalWorkflowStatus(state.status))
38
+ throw new StartOrSignalConflictError(workflowId, state.status);
39
+ await callbacks.signalExistingWorkflow(workflowId, signalSpec.name, signalSpec.payload, signalId);
40
+ return callbacks.getHandle(workflowId);
41
+ }
42
+ const WINNER_RESOLUTION_MAX_ATTEMPTS = 5, WINNER_RESOLUTION_RETRY_DELAY_MS = 5;
43
+ export async function resolveWinnerWithSignal(internals, winnerId, signalSpec, signalId, callbacks, idempotencyKey) {
44
+ for (let attempt = 0;attempt < WINNER_RESOLUTION_MAX_ATTEMPTS; attempt += 1) {
45
+ const resolved = await signalOrConflictExistingWorkflow(internals, winnerId, signalSpec, signalId, callbacks);
46
+ if (resolved !== void 0)
47
+ return resolved;
48
+ if (attempt < WINNER_RESOLUTION_MAX_ATTEMPTS - 1)
49
+ await sleep(WINNER_RESOLUTION_RETRY_DELAY_MS);
50
+ }
51
+ if (await resolveIdempotencyKeyWorkflowId(internals, idempotencyKey) === winnerId)
52
+ throw new IdempotencyKeyPurgedError(winnerId);
53
+ throw Error(`startOrSignal resolved winning workflow "${winnerId}" but its record never became readable after ${WINNER_RESOLUTION_MAX_ATTEMPTS} attempts.`);
54
+ }
55
+ export async function requireWinnerId(internals, idempotencyKey) {
56
+ const winnerId = await resolveIdempotencyKeyWorkflowId(internals, idempotencyKey);
57
+ if (winnerId === void 0)
58
+ throw Error(`start idempotency mapping for key "${idempotencyKey}" vanished after a lost compare-and-swap; the start-idem: keyspace may have been mutated externally.`);
59
+ return winnerId;
60
+ }
@@ -0,0 +1,45 @@
1
+ import type { StartOptions, StartOrSignalSignal } from '../../types.ts';
2
+ import { type WorkflowHandle } from '../handles.ts';
3
+ import type { EngineInternals } from '../internals.ts';
4
+ import { type LifecycleCallbacks } from './shared.ts';
5
+ import { type StartOrSignalCallbacks } from './start-or-signal-resolution.ts';
6
+ export type { StartOrSignalCallbacks };
7
+ /**
8
+ * Enforce at-most-once start for a given `idempotencyKey`. On the first call,
9
+ * the workflow record and a `startIdempotency(key) → { workflowId }` mapping
10
+ * commit in one compare-and-swap gated on the mapping being absent. Every later
11
+ * call with the same key resolves the mapping and returns a handle to that run —
12
+ * even if it has since reached a terminal state (idempotent start is a pure
13
+ * dedup; it never restarts).
14
+ *
15
+ * Concurrent same-key callers race at the lookup→commit gap; the CAS lets exactly
16
+ * one win, and the loser (its create batch rejected) resolves to the winner's
17
+ * run. Requires the `conditionalBatch` capability and throws if it is absent —
18
+ * single-execution semantics cannot be honored without atomic compare-and-swap.
19
+ */
20
+ export declare function startWithIdempotency(internals: EngineInternals, type: string, input: unknown, options: StartOptions, callbacks: LifecycleCallbacks): Promise<WorkflowHandle>;
21
+ /**
22
+ * Atomic start-or-signal (signal-with-start). Resolves the target workflow, then:
23
+ *
24
+ * - **Absent** → create the workflow and deliver the signal in ONE conditional
25
+ * batch (workflow record + `sig:`/`sigres:` pair + optional idempotency
26
+ * mapping). The freshly-launched run consumes the signal on its first drive.
27
+ * - **Non-terminal** (running, pending, suspended) → deliver the signal through
28
+ * the standard engine signal path with the same `signalId`, so it dedups
29
+ * against a create-batch signal a concurrent winner may have written.
30
+ * - **Terminal** → throw {@link StartOrSignalConflictError}: a finished run
31
+ * cannot be signalled and is not silently replaced.
32
+ *
33
+ * Convergence requires a SHARED workflow identity. Concurrent callers converge on
34
+ * one workflow and one signal only when they share an `options.idempotencyKey`
35
+ * (the durable mapping picks one creator and the signal id derives from the key)
36
+ * or an explicit `options.id` (the caller-id reservation picks one creator).
37
+ *
38
+ * A bare `signal.signalId` with NEITHER `options.id` nor `options.idempotencyKey`
39
+ * does NOT converge: each absent-target call generates its own workflow id, so
40
+ * concurrent callers create distinct runs and each delivers its own signal. In
41
+ * that mode `startOrSignal` is an atomic start-with-one-initial-signal, not a
42
+ * convergence primitive. Use `idempotencyKey` (id-free convergence) or
43
+ * `id` + `signalId` when concurrent callers must converge.
44
+ */
45
+ export declare function startOrSignal(internals: EngineInternals, type: string, input: unknown, signalSpec: StartOrSignalSignal, options: StartOptions | undefined, callbacks: StartOrSignalCallbacks): Promise<WorkflowHandle>;
@@ -0,0 +1,141 @@
1
+ import { KEYS, requireStorageCapability } from "../../../storage/interface.js";
2
+ import { encode } from "../../codec.js";
3
+ import {
4
+ assertValidIdempotencyKey,
5
+ StartWorkflowValidationError
6
+ } from "../../start-workflow-validation.js";
7
+ import { IdempotencyKeyPurgedError, WorkflowAlreadyExistsError } from "../errors.js";
8
+ import { buildCreateBatchSignalOperations } from "../signals.js";
9
+ import {
10
+ StartIdempotencyRaceLostError
11
+ } from "./start-commit.js";
12
+ import {
13
+ requireWinnerId,
14
+ resolveCallerIdWinnerOrRetry,
15
+ resolveExistingRunOrThrowPurged,
16
+ resolveIdempotencyKeyWorkflowId,
17
+ resolveWinnerWithSignal,
18
+ signalOrConflictExistingWorkflow
19
+ } from "./start-or-signal-resolution.js";
20
+ import { startWorkflow } from "./start.js";
21
+ function resolveSignalId(signalSpec, idempotencyKey) {
22
+ if (idempotencyKey !== void 0)
23
+ return KEYS.startIdempotencySignalId(idempotencyKey);
24
+ if (signalSpec.signalId !== void 0)
25
+ return signalSpec.signalId;
26
+ throw new StartWorkflowValidationError("startOrSignal requires either signal.signalId or options.idempotencyKey to identify the signal to deliver. (Concurrent callers converge on one workflow and one signal only with a shared idempotencyKey, or a shared id plus signalId; a bare signalId starts a fresh run per caller.)");
27
+ }
28
+ function idempotentStartOperationsFor(internals, idempotencyKey, signal) {
29
+ return (workflowId) => {
30
+ const operations = [], conditions = [];
31
+ if (idempotencyKey !== void 0) {
32
+ const key = KEYS.startIdempotency(idempotencyKey);
33
+ operations.push({
34
+ type: "put",
35
+ key,
36
+ value: encode({ workflowId })
37
+ });
38
+ conditions.push({ key, expectedValue: null });
39
+ }
40
+ if (signal !== void 0) {
41
+ const built = buildCreateBatchSignalOperations(internals, workflowId, signal.name, signal.payload, signal.signalId);
42
+ operations.push(...built.operations);
43
+ conditions.push(built.condition);
44
+ }
45
+ return { operations, conditions };
46
+ };
47
+ }
48
+ export async function startWithIdempotency(internals, type, input, options, callbacks) {
49
+ requireStorageCapability(internals.storage, "conditionalBatch", "start idempotency");
50
+ const { idempotencyKey } = options;
51
+ if (idempotencyKey === void 0)
52
+ throw new StartWorkflowValidationError("startWithIdempotency requires options.idempotencyKey");
53
+ assertValidIdempotencyKey(idempotencyKey, "options.idempotencyKey");
54
+ assertIdAndIdempotencyKeyExclusive(options);
55
+ const existingId = await resolveIdempotencyKeyWorkflowId(internals, idempotencyKey);
56
+ if (existingId !== void 0)
57
+ return callbacks.getHandle(await resolveExistingRunOrThrowPurged(internals, existingId));
58
+ try {
59
+ return await startWorkflow(internals, type, input, options, void 0, callbacks, idempotentStartOperationsFor(internals, idempotencyKey, void 0));
60
+ } catch (error) {
61
+ if (!(error instanceof StartIdempotencyRaceLostError))
62
+ throw error;
63
+ }
64
+ return callbacks.getHandle(await resolveExistingRunOrThrowPurged(internals, await requireWinnerId(internals, idempotencyKey)));
65
+ }
66
+ function assertIdAndIdempotencyKeyExclusive(options) {
67
+ if (options.id !== void 0 && options.idempotencyKey !== void 0)
68
+ throw new StartWorkflowValidationError("options.id and options.idempotencyKey are mutually exclusive: idempotency assigns its own workflow id and dedups through the idempotency key. Provide one or the other.");
69
+ }
70
+ function validateStartOrSignalConvergence(signalSpec, options) {
71
+ const idempotencyKey = options?.idempotencyKey;
72
+ if (idempotencyKey === void 0)
73
+ return;
74
+ assertValidIdempotencyKey(idempotencyKey, "options.idempotencyKey");
75
+ assertIdAndIdempotencyKeyExclusive(options ?? {});
76
+ if (signalSpec.signalId !== void 0)
77
+ throw new StartWorkflowValidationError("startOrSignal does not accept both signal.signalId and options.idempotencyKey: the signal id derives from the idempotency key for convergence. Provide exactly one.");
78
+ }
79
+ export async function startOrSignal(internals, type, input, signalSpec, options, callbacks) {
80
+ requireStorageCapability(internals.storage, "conditionalBatch", "startOrSignal");
81
+ const idempotencyKey = options?.idempotencyKey;
82
+ validateStartOrSignalConvergence(signalSpec, options);
83
+ const signalId = resolveSignalId(signalSpec, idempotencyKey), mappedId = idempotencyKey !== void 0 ? await resolveIdempotencyKeyWorkflowId(internals, idempotencyKey) : void 0, existingId = mappedId ?? options?.id;
84
+ if (existingId !== void 0) {
85
+ const resolved = await signalOrConflictExistingWorkflow(internals, existingId, signalSpec, signalId, callbacks);
86
+ if (resolved !== void 0)
87
+ return resolved;
88
+ if (mappedId !== void 0)
89
+ throw new IdempotencyKeyPurgedError(mappedId);
90
+ }
91
+ return createWithSignalOrFallback(internals, type, input, signalSpec, signalId, options, callbacks);
92
+ }
93
+ const CALLER_ID_CREATE_MAX_ATTEMPTS = 5;
94
+ async function createWithSignalOrFallback(internals, type, input, signalSpec, signalId, options, callbacks) {
95
+ const idempotencyKey = options?.idempotencyKey;
96
+ for (let attempt = 0;attempt < CALLER_ID_CREATE_MAX_ATTEMPTS; attempt += 1) {
97
+ const outcome = await resolveCreateRaceOutcome(internals, options, async () => {
98
+ return startWorkflow(internals, type, input, options, void 0, callbacks, idempotentStartOperationsFor(internals, idempotencyKey, {
99
+ name: signalSpec.name,
100
+ payload: signalSpec.payload,
101
+ signalId
102
+ }));
103
+ });
104
+ if (outcome.kind === "created")
105
+ return outcome.handle;
106
+ if (outcome.kind === "lost-keyed")
107
+ return resolveWinnerWithSignal(internals, outcome.id, signalSpec, signalId, callbacks, outcome.idempotencyKey);
108
+ const resolved = outcome.kind === "lost-caller-id" ? await resolveCallerIdWinnerOrRetry(internals, outcome.id, signalSpec, signalId, callbacks) : await plainCreateBufferedSignalOrResolve(internals, type, input, signalSpec, signalId, options, callbacks);
109
+ if (resolved !== void 0)
110
+ return resolved;
111
+ }
112
+ throw Error(`startOrSignal could not create workflow "${options?.id ?? "<generated>"}" after ${CALLER_ID_CREATE_MAX_ATTEMPTS} attempts: each concurrent same-id winner aborted before its durable commit.`);
113
+ }
114
+ async function plainCreateBufferedSignalOrResolve(internals, type, input, signalSpec, signalId, options, callbacks) {
115
+ try {
116
+ return await startWorkflow(internals, type, input, options, void 0, callbacks);
117
+ } catch (error) {
118
+ if (!(error instanceof WorkflowAlreadyExistsError))
119
+ throw error;
120
+ return resolveCallerIdWinnerOrRetry(internals, error.workflowId, signalSpec, signalId, callbacks);
121
+ }
122
+ }
123
+ async function resolveCreateRaceOutcome(internals, options, runCreate) {
124
+ try {
125
+ return { kind: "created", handle: await runCreate() };
126
+ } catch (error) {
127
+ if (error instanceof WorkflowAlreadyExistsError)
128
+ return { kind: "lost-caller-id", id: error.workflowId };
129
+ if (error instanceof StartIdempotencyRaceLostError) {
130
+ const idempotencyKey = options?.idempotencyKey;
131
+ if (idempotencyKey !== void 0)
132
+ return {
133
+ kind: "lost-keyed",
134
+ id: await requireWinnerId(internals, idempotencyKey),
135
+ idempotencyKey
136
+ };
137
+ return { kind: "signal-already-buffered" };
138
+ }
139
+ throw error;
140
+ }
141
+ }
@@ -3,11 +3,11 @@ import type { Checkpoint, Duration, StartOptions, TimerEntry, WorkflowState } fr
3
3
  import { type WorkflowVersionTuple } from '../../workflow-version-tuple.ts';
4
4
  import { type WorkflowHandle } from '../handles.ts';
5
5
  import type { EngineInternals } from '../internals.ts';
6
- import { type LifecycleCallbacks, type RegistrationEntry } from './shared.ts';
6
+ import { type LifecycleCallbacks } from './shared.ts';
7
+ import { type BuildIdempotentStartOperations } from './start-commit.ts';
7
8
  export declare function start(internals: EngineInternals, type: string, input: unknown, options: StartOptions | undefined, callbacks: LifecycleCallbacks): Promise<WorkflowHandle>;
8
- export declare function startWorkflow(internals: EngineInternals, type: string, input: unknown, options: StartOptions | undefined, additionalStartOperations: BatchOperation[] | undefined, callbacks: LifecycleCallbacks): Promise<WorkflowHandle>;
9
+ export declare function startWorkflow(internals: EngineInternals, type: string, input: unknown, options: StartOptions | undefined, additionalStartOperations: BatchOperation[] | undefined, callbacks: LifecycleCallbacks, buildIdempotentStartOperations?: BuildIdempotentStartOperations): Promise<WorkflowHandle>;
9
10
  export declare function resolveScheduledStartAt(internals: EngineInternals, options: StartOptions | undefined, submissionTime: number, callbacks: LifecycleCallbacks): number | undefined;
10
11
  export declare function parseStartOptionDuration(_internals: EngineInternals, duration: Duration, fieldName: 'options.executionTimeout' | 'options.startAfter', _callbacks: LifecycleCallbacks): number;
11
- export declare function beginWorkflowExecution(internals: EngineInternals, workflowId: string, workflowType: string, input: unknown, checkpoint: Checkpoint, executionDeadline: number | undefined, executionStateOwnerId: string, _registration: RegistrationEntry, callbacks: LifecycleCallbacks): void;
12
12
  export declare function createInitialWorkflowState(internals: EngineInternals, workflowId: string, type: string, input: unknown, versionTuple: WorkflowVersionTuple, options: StartOptions | undefined, tags: string[] | undefined, executionStateOwnerId: string, delayedStartTimer: TimerEntry | undefined, callbacks: LifecycleCallbacks): WorkflowState;
13
13
  export declare function createInitialCheckpoint(internals: EngineInternals, workflowId: string, workflowVersion: string, options: StartOptions | undefined, _callbacks: LifecycleCallbacks): Checkpoint;
@@ -1,6 +1,5 @@
1
1
  import { KEYS } from "../../../storage/interface.js";
2
2
  import { createCheckpoint } from "../../checkpoint.js";
3
- import { WorkflowStartedEvent } from "../../events.js";
4
3
  import { assertPayloadWithinLimit } from "../../payload-size.js";
5
4
  import { normalizeStorageTimestamp } from "../../scheduler.js";
6
5
  import {
@@ -20,8 +19,12 @@ import {
20
19
  normalizeStartWorkflowTags,
21
20
  setWorkflowStartHeaders
22
21
  } from "./shared.js";
23
- import { buildStartBatchOperations } from "./start-batch.js";
24
- import { runWorkflowStartInterceptor, startWorkflowExecution } from "./start-exec.js";
22
+ import { buildAndCommitStartBatch } from "./start-commit.js";
23
+ import {
24
+ assertDeferSupported,
25
+ beginExecutionAwaitingLiveness,
26
+ runWorkflowStartInterceptor
27
+ } from "./start-exec.js";
25
28
  export async function start(internals, type, input, options, callbacks) {
26
29
  return startWorkflow(internals, type, input, options, void 0, callbacks);
27
30
  }
@@ -43,20 +46,25 @@ function prepareStartWorkflow(internals, options, callbacks) {
43
46
  normalizedTags
44
47
  };
45
48
  }
46
- async function persistStartBatch(internals, startOperations) {
47
- await internals.storage.batch(startOperations);
48
- }
49
49
  function rollbackTransientStartState(internals, workflowId) {
50
50
  forgetCommittedCheckpointBytes(internals, workflowId);
51
51
  internals.checkpoints.delete(workflowId);
52
52
  internals.workflowHeaders.delete(workflowId);
53
53
  internals.workflowVersionTuples.delete(workflowId);
54
+ internals.workflowServices.delete(workflowId);
55
+ internals.workflowsNeedingTerminalCleanup.delete(workflowId);
54
56
  }
55
- export async function startWorkflow(internals, type, input, options, additionalStartOperations, callbacks) {
57
+ function assertServicesSupportedForMode(internals, options) {
58
+ if (options?.services !== void 0 && internals.inlineStrategy === null)
59
+ throw Error('options.services is only supported in inline execution mode; it cannot be serialized to a Worker. Remove services or use workflowExecutionMode: "inline".');
60
+ }
61
+ export async function startWorkflow(internals, type, input, options, additionalStartOperations, callbacks, buildIdempotentStartOperations) {
56
62
  const registration = internals.registrations.get(type);
57
63
  if (!registration)
58
64
  throw new WorkflowNotRegisteredError(type);
65
+ assertServicesSupportedForMode(internals, options);
59
66
  const preparation = prepareStartWorkflow(internals, options, callbacks), { workflowId, callerProvidedId, parentHeaders, executionStateOwnerId, delayedStartTimer } = preparation;
67
+ assertDeferSupported(internals, options, Boolean(delayedStartTimer));
60
68
  if (internals.pendingStarts.has(workflowId))
61
69
  throw new WorkflowAlreadyExistsError(workflowId);
62
70
  internals.pendingStarts.add(workflowId);
@@ -71,11 +79,32 @@ export async function startWorkflow(internals, type, input, options, additionalS
71
79
  internals.checkpoints.set(workflowId, checkpoint);
72
80
  setWorkflowStartHeaders(internals, workflowId, workflowStartHeaders, callbacks);
73
81
  internals.workflowVersionTuples.set(workflowId, versionTuple);
74
- const startOperations = buildStartBatchOperations(internals, workflowId, state, checkpoint, registration, options, state.executionDeadline, delayedStartTimer, persistedWorkflowStartHeaders, additionalStartOperations, callbacks);
75
- await persistStartBatch(internals, startOperations);
82
+ await buildAndCommitStartBatch({
83
+ internals,
84
+ workflowId,
85
+ state,
86
+ checkpoint,
87
+ registration,
88
+ options,
89
+ delayedStartTimer,
90
+ persistedWorkflowStartHeaders,
91
+ additionalStartOperations,
92
+ callbacks
93
+ }, buildIdempotentStartOperations);
94
+ if (options?.services !== void 0) {
95
+ internals.workflowServices.set(workflowId, options.services);
96
+ internals.workflowsNeedingTerminalCleanup.add(workflowId);
97
+ }
76
98
  const handle = createWorkflowHandle(internals, workflowId, callbacks);
77
- if (!delayedStartTimer)
78
- beginWorkflowExecution(internals, workflowId, type, input, checkpoint, state.executionDeadline, state.executionStateOwnerId ?? workflowId, registration, callbacks);
99
+ await beginExecutionAwaitingLiveness(internals, {
100
+ type,
101
+ input,
102
+ checkpoint,
103
+ state,
104
+ registration,
105
+ options,
106
+ isDelayed: Boolean(delayedStartTimer)
107
+ }, workflowId, callbacks);
79
108
  startSucceeded = !0;
80
109
  return handle;
81
110
  } finally {
@@ -101,42 +130,18 @@ export function resolveScheduledStartAt(internals, options, submissionTime, call
101
130
  export function parseStartOptionDuration(_internals, duration, fieldName, _callbacks) {
102
131
  return parseStartWorkflowDuration(duration, fieldName);
103
132
  }
104
- export function beginWorkflowExecution(internals, workflowId, workflowType, input, checkpoint, executionDeadline, executionStateOwnerId, _registration, callbacks) {
105
- const nestingDepth = internals.pendingNestingDepth ?? 0;
106
- internals.pendingNestingDepth = void 0;
107
- if (internals.inlineStrategy !== null) {
108
- callbacks.queueInlineWorkflowExecutionStart({
109
- workflowId,
110
- workflowType,
111
- input,
112
- checkpoint,
113
- nestingDepth,
114
- executionDeadline,
115
- executionStateOwnerId
116
- });
117
- return;
118
- }
119
- callbacks.dispatchEvent(new WorkflowStartedEvent(workflowId, workflowType, input));
120
- startWorkflowExecution(internals, workflowId, workflowType, input, checkpoint, nestingDepth, executionDeadline, executionStateOwnerId, callbacks);
121
- }
122
133
  function buildInitialIdentitySlice(workflowId, type, input, versionTuple, executionStateOwnerId, delayedStartTimer, now, tags) {
123
134
  return {
124
135
  id: workflowId,
125
136
  type,
126
137
  status: delayedStartTimer ? "pending" : "running",
127
138
  input,
128
- version: versionTuple.workflowVersion,
139
+ versionTuple,
129
140
  executionStateOwnerId,
130
141
  createdAt: now,
131
142
  ...!delayedStartTimer && { startedAt: now },
132
143
  updatedAt: now,
133
- ...tags !== void 0 && { tags },
134
- ...versionTuple.agentVersion !== void 0 && {
135
- agentVersion: versionTuple.agentVersion
136
- },
137
- ...versionTuple.toolVersions !== void 0 && {
138
- toolVersions: versionTuple.toolVersions
139
- }
144
+ ...tags !== void 0 && { tags }
140
145
  };
141
146
  }
142
147
  function resolveInitialExecutionDeadline(internals, options, delayedStartTimer, now, callbacks) {
@@ -1,8 +1,9 @@
1
1
  export { EMPTY_STORAGE_VALUE, createWorkflowHandle, loadTerminalCleanupTrackedState, loadWorkflowStartHeaders, normalizeStartWorkflowTags, processPendingUpdatesAfterReplay, setWorkflowStartHeaders, type LifecycleCallbacks, type RecoverAllOptions, } from './lifecycle/shared.ts';
2
2
  export { createWorkflowVersionTuple, derivePreparedExecutionState, prepareResumeState, throwVersionMismatch, workflowStateWithVersionTuple, workflowVersionTupleFromState, } from './lifecycle/persist.ts';
3
- export { beginWorkflowExecution, createInitialCheckpoint, createInitialWorkflowState, parseStartOptionDuration, resolveScheduledStartAt, start, startWorkflow, } from './lifecycle/start.ts';
3
+ export { createInitialCheckpoint, createInitialWorkflowState, parseStartOptionDuration, resolveScheduledStartAt, start, startWorkflow, } from './lifecycle/start.ts';
4
+ export { startOrSignal, startWithIdempotency, type StartOrSignalCallbacks, } from './lifecycle/start-or-signal.ts';
4
5
  export { buildInitialSearchAttributeOperations, buildStartBatchOperations, validateSearchAttributes, } from './lifecycle/start-batch.ts';
5
- export { runWorkflowStartInterceptor, startWorkflowExecution } from './lifecycle/start-exec.ts';
6
+ export { beginWorkflowExecution, runWorkflowStartInterceptor, startWorkflowExecution, } from './lifecycle/start-exec.ts';
6
7
  export { buildForkBatchOperations, buildForkSearchAttributes, createForkLineage, createForkedWorkflowState, } from './lifecycle/fork-helpers.ts';
7
8
  export { fork, launchWorkflowFromCheckpoint, recoverAll, resume } from './lifecycle/transition.ts';
8
9
  export { resumeWorkflowFromStorage } from './lifecycle/resume.ts';
@@ -16,7 +16,6 @@ export {
16
16
  workflowVersionTupleFromState
17
17
  } from "./lifecycle/persist.js";
18
18
  export {
19
- beginWorkflowExecution,
20
19
  createInitialCheckpoint,
21
20
  createInitialWorkflowState,
22
21
  parseStartOptionDuration,
@@ -24,12 +23,20 @@ export {
24
23
  start,
25
24
  startWorkflow
26
25
  } from "./lifecycle/start.js";
26
+ export {
27
+ startOrSignal,
28
+ startWithIdempotency
29
+ } from "./lifecycle/start-or-signal.js";
27
30
  export {
28
31
  buildInitialSearchAttributeOperations,
29
32
  buildStartBatchOperations,
30
33
  validateSearchAttributes
31
34
  } from "./lifecycle/start-batch.js";
32
- export { runWorkflowStartInterceptor, startWorkflowExecution } from "./lifecycle/start-exec.js";
35
+ export {
36
+ beginWorkflowExecution,
37
+ runWorkflowStartInterceptor,
38
+ startWorkflowExecution
39
+ } from "./lifecycle/start-exec.js";
33
40
  export {
34
41
  buildForkBatchOperations,
35
42
  buildForkSearchAttributes,
@@ -88,7 +88,7 @@ function summaryFromState(state, attributeFailureCategory) {
88
88
  type: state.type,
89
89
  status: state.status,
90
90
  ...state.tags !== void 0 && { tags: state.tags },
91
- version: state.version,
91
+ version: state.versionTuple.workflowVersion,
92
92
  createdAt: state.createdAt,
93
93
  updatedAt: state.updatedAt,
94
94
  ...state.executionDeadline !== void 0 && { executionDeadline: state.executionDeadline },
@@ -19,5 +19,21 @@ export type DataOperationCallbacks = {
19
19
  export declare function processMemoOperation(_internals: EngineInternals, workflowId: string, operation: MemoOperation, callbacks: DataOperationCallbacks): Promise<void>;
20
20
  export declare function processOffloadOperation(internals: EngineInternals, workflowId: string, operation: OffloadOperation, callbacks: DataOperationCallbacks): Promise<void>;
21
21
  export declare function processLoadOperation(internals: EngineInternals, workflowId: string, operation: LoadOperation, callbacks: DataOperationCallbacks): Promise<void>;
22
+ /**
23
+ * Read an offloaded value back out of storage by `workflowId` + `key`, decoding
24
+ * it with the same codec {@link processOffloadOperation} wrote it with.
25
+ *
26
+ * This is the post-completion sibling of the in-workflow `ctx.load()` read
27
+ * (see {@link processLoadOperation}): `ctx.load()` is restricted to the running
28
+ * workflow's own offloads and throws on a miss, whereas this external reader
29
+ * lets a consumer read a *terminal* workflow's offloaded output after
30
+ * `handle.result()` resolves — the artifact survives normal completion
31
+ * (`completeWorkflow`/`failWorkflow` preserve `offload:` keys) and is swept only
32
+ * when the workflow is terminated/cancelled.
33
+ *
34
+ * @returns The decoded offload value, or `null` when no value is stored under
35
+ * that key (either the key was never written, or the artifact was swept).
36
+ */
37
+ export declare function getOffloadFromInternals(internals: EngineInternals, workflowId: string, key: string): Promise<unknown>;
22
38
  export declare function processArchiveOperation(internals: EngineInternals, workflowId: string, operation: ArchiveOperation, callbacks: DataOperationCallbacks): Promise<void>;
23
39
  export {};
@@ -30,6 +30,12 @@ export async function processLoadOperation(internals, workflowId, operation, cal
30
30
  return decode(raw);
31
31
  });
32
32
  }
33
+ export async function getOffloadFromInternals(internals, workflowId, key) {
34
+ const raw = await internals.storage.get(KEYS.offload(workflowId, key));
35
+ if (raw === null)
36
+ return null;
37
+ return decode(raw);
38
+ }
33
39
  export async function processArchiveOperation(internals, workflowId, operation, callbacks) {
34
40
  return callbacks.runOperationWithResult(workflowId, operation, async () => {
35
41
  await internals.storage.put(KEYS.archive(workflowId, operation.key), encode(operation.data));
@@ -19,10 +19,11 @@ export type TimeOperationCallbacks = {
19
19
  runDeferredTerminalCleanup: (workflowId: string, timerId: string) => Promise<void>;
20
20
  handleScheduleTimer: (entry: TimerEntry) => Promise<void>;
21
21
  timeout: (workflowId: string) => Promise<void>;
22
+ handleCleanupError: (source: string, error: unknown, workflowId: string) => void;
22
23
  };
23
24
  export declare function createDelayedStartTimerEntry(_internals: EngineInternals, workflowId: string, scheduledStartAt: number, options: StartOptions | undefined, callbacks: Pick<TimeOperationCallbacks, 'parseStartOptionDuration'>): TimerEntry;
24
25
  export declare function processSleepOperation(internals: EngineInternals, workflowId: string, operation: SleepOperation, callbacks: Pick<TimeOperationCallbacks, 'completeOperation' | 'loadWorkflowState'>): Promise<void>;
25
26
  export declare function registerSleepResolver(internals: EngineInternals, workflowId: string, operationId: string, resolve: () => void): void;
26
- export declare function startDelayedWorkflow(internals: EngineInternals, entry: TimerEntry, callbacks: Pick<TimeOperationCallbacks, 'beginWorkflowExecution' | 'failWorkflow' | 'loadWorkflowStartHeaders' | 'loadWorkflowState' | 'runSerializedWorkflowStateWrite' | 'setWorkflowStartHeaders' | 'workflowVersionTupleFromState'>): Promise<void>;
27
- export declare function handleTimerFired(internals: EngineInternals, entry: TimerEntry, callbacks: Pick<TimeOperationCallbacks, 'failWorkflow' | 'loadWorkflowStartHeaders' | 'loadWorkflowState' | 'runDeferredTerminalCleanup' | 'runSerializedWorkflowStateWrite' | 'handleScheduleTimer' | 'setWorkflowStartHeaders' | 'timeout' | 'beginWorkflowExecution' | 'workflowVersionTupleFromState'>): Promise<void>;
27
+ export declare function startDelayedWorkflow(internals: EngineInternals, entry: TimerEntry, callbacks: Pick<TimeOperationCallbacks, 'beginWorkflowExecution' | 'failWorkflow' | 'handleCleanupError' | 'loadWorkflowStartHeaders' | 'loadWorkflowState' | 'runSerializedWorkflowStateWrite' | 'setWorkflowStartHeaders' | 'workflowVersionTupleFromState'>): Promise<void>;
28
+ export declare function handleTimerFired(internals: EngineInternals, entry: TimerEntry, callbacks: Pick<TimeOperationCallbacks, 'failWorkflow' | 'handleCleanupError' | 'loadWorkflowStartHeaders' | 'loadWorkflowState' | 'runDeferredTerminalCleanup' | 'runSerializedWorkflowStateWrite' | 'handleScheduleTimer' | 'setWorkflowStartHeaders' | 'timeout' | 'beginWorkflowExecution' | 'workflowVersionTupleFromState'>): Promise<void>;
28
29
  export {};
@@ -1,7 +1,8 @@
1
- import { KEYS } from "../../storage/interface.js";
1
+ import { KEYS, storageHas } from "../../storage/interface.js";
2
2
  import { deserializeCheckpoint } from "../checkpoint.js";
3
3
  import { encode } from "../codec.js";
4
4
  import { buildTimerBatchOperations, normalizeStorageTimestamp } from "../scheduler.js";
5
+ import { reprovideRecoveredServices } from "./lifecycle/recovered-services.js";
5
6
  import { buildWorkflowVisibilityIndexTransition } from "./workflow-indexes.js";
6
7
  export function createDelayedStartTimerEntry(_internals, workflowId, scheduledStartAt, options, callbacks) {
7
8
  return {
@@ -85,6 +86,10 @@ export async function startDelayedWorkflow(internals, entry, callbacks) {
85
86
  });
86
87
  if (!runningState)
87
88
  return;
89
+ if (await reprovideRecoveredServices(internals, runningState, (workflowId, error) => callbacks.failWorkflow(workflowId, error), callbacks.handleCleanupError))
90
+ return;
91
+ if (await storageHas(internals.storage, KEYS.terminalCleanupNeeded(entry.workflowId)))
92
+ internals.workflowsNeedingTerminalCleanup.add(entry.workflowId);
88
93
  internals.checkpoints.set(entry.workflowId, checkpoint);
89
94
  internals.workflowVersionTuples.set(entry.workflowId, callbacks.workflowVersionTupleFromState(runningState));
90
95
  callbacks.setWorkflowStartHeaders(entry.workflowId, await callbacks.loadWorkflowStartHeaders(entry.workflowId));
@@ -12,13 +12,9 @@ import type { Storage as WeftStorage } from '../../storage/interface.ts';
12
12
  * 3. Sentinel is absent. Only stamp the storage when it carries no user
13
13
  * workflow data. Stamping a database that already holds workflow records,
14
14
  * schedules, checkpoints, or any other `wf:` / `op:` / `schedule:` / `ev:`
15
- * prefixed key would silently classify pre-versioned data (written by an
16
- * older Weft binary or by the `new Engine({ storage })` constructor path
17
- * before the sentinel was introduced) as schema-current and risk replaying
18
- * incompatible records. When user data is already present without a
19
- * sentinel, fail with `PersistedDataIncompatibleError(null, …)` so the
20
- * operator can choose explicitly whether to wipe and start fresh.
15
+ * prefixed key would silently classify unversioned data as schema-current
16
+ * and risk replaying incompatible records. When user data is already present
17
+ * without a sentinel, fail with `PersistedDataIncompatibleError(null, …)` so
18
+ * the operator can choose explicitly whether to wipe and start fresh.
21
19
  */
22
- export declare function assertCompatiblePersistedDataVersion(storage: WeftStorage, options?: {
23
- allowLegacyData?: boolean;
24
- }): Promise<void>;
20
+ export declare function assertCompatiblePersistedDataVersion(storage: WeftStorage): Promise<void>;