@loomwork/worker-runtime 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +75 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/superseded.d.ts +66 -0
- package/dist/superseded.d.ts.map +1 -0
- package/dist/superseded.js +102 -0
- package/dist/superseded.js.map +1 -0
- package/dist/types.d.ts +122 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -3
- package/src/index.ts +9 -0
- package/src/superseded.ts +102 -0
- package/src/types.ts +123 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @loomwork/worker-runtime — the worker SPI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The contract for running one execution on some compute placement: `WorkerRuntime` (`allocate`, `execute`,
|
|
4
|
+
`release`, optional `cancel`) and the JSON `ExecutionPackage` that crosses the boundary. A package carries
|
|
5
|
+
identity and resume coordinates (`executionId`, `workflowId`, `flowId`, `input` or a `resumeEvent`),
|
|
6
|
+
never code: the worker resolves the flow by id from the code it booted with and rebuilds state from the
|
|
7
|
+
shared event and snapshot stores. This is infrastructure. An app does not normally import it; you read it
|
|
8
|
+
when you write a worker backend or a dispatcher. A [`@loomwork/dispatcher`](../dispatcher/README.md) sits in
|
|
9
|
+
front of a worker and owns retries and placement.
|
|
10
|
+
|
|
11
|
+
Implementations: `LocalWorkerRuntime` ([`@loomwork/worker-runtime-local`](../worker-runtime-local/README.md),
|
|
12
|
+
in-process), `RemoteWorkerRuntime` (`@loomwork/plugin-worker-remote`, HTTP to a `@loomwork/plugin-worker-server`
|
|
13
|
+
endpoint) and `CloudRunWorkerRuntime` (`@loomwork/plugin-cloudrun`).
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import {
|
|
17
|
+
isSupersededRefusal,
|
|
18
|
+
type ExecutionPackage,
|
|
19
|
+
type ExecutionResult,
|
|
20
|
+
type WorkerRuntime,
|
|
21
|
+
} from "@loomwork/worker-runtime";
|
|
22
|
+
|
|
23
|
+
// What a dispatcher does around any WorkerRuntime: lease, execute, always release.
|
|
24
|
+
export async function dispatchOnce(
|
|
25
|
+
worker: WorkerRuntime,
|
|
26
|
+
pkg: ExecutionPackage
|
|
27
|
+
): Promise<ExecutionResult | "superseded"> {
|
|
28
|
+
const lease = await worker.allocate({ executionId: pkg.executionId });
|
|
29
|
+
try {
|
|
30
|
+
return await worker.execute(lease, pkg);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
// Another holder owns this execution: settled, not failed. Do not retry it.
|
|
33
|
+
if (isSupersededRefusal(err, pkg.executionId)) return "superseded";
|
|
34
|
+
throw err;
|
|
35
|
+
} finally {
|
|
36
|
+
await worker.release(lease);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Surface
|
|
42
|
+
|
|
43
|
+
- **Interface.** `WorkerRuntime`, `WorkerRequest`, `WorkerLease` (`leaseId`, optional fencing token
|
|
44
|
+
`fence`, `metadata`).
|
|
45
|
+
- **Payloads.** `ExecutionPackage` (a fresh start sets `input`; a resume sets `resumeEvent`; optional
|
|
46
|
+
`snapshotId`, `fromSequence`, `metadata`, `principal`), `ExecutionResult` (`status`, folded `state`,
|
|
47
|
+
`output`, `error`, `suspendedOn`, `lastSequence`) and `ExecutionOutcome`
|
|
48
|
+
(`"completed" | "suspended" | "failed"`).
|
|
49
|
+
- **Superseded refusals.** `isSupersededRefusal(err, executionId)` and `ExecutionSupersededError`, its
|
|
50
|
+
wire form.
|
|
51
|
+
|
|
52
|
+
## Behaviour that surprises people
|
|
53
|
+
|
|
54
|
+
- **A suspend is a normal result.** `execute` runs to suspend-or-complete and returns
|
|
55
|
+
`status: "suspended"`; the caller resumes with a fresh package carrying `resumeEvent`.
|
|
56
|
+
- **Superseded is not failed.** `isSupersededRefusal` is true for a lost start race, a lost or held
|
|
57
|
+
lease, a stale fence or `ExecutionSupersededError`, each only when it names this execution. A
|
|
58
|
+
dispatcher must not retry it: every attempt takes a newer fence, so a retry would fence the winner out
|
|
59
|
+
and the two would ping-pong. It transfers ownership without checking the new holder is alive, so run
|
|
60
|
+
`recoverAbandoned`, and await the outcome by id.
|
|
61
|
+
- **`principal` crosses the wire as plain data.** A receiving worker should honour it only from a caller
|
|
62
|
+
it has authenticated; `runsAsSystem` marks a worker that runs principal-less packages as the kernel's
|
|
63
|
+
system identity, which a network transport refuses to expose without an authentication gate.
|
|
64
|
+
- **`cancel` is best effort.** Durable cancellation settles through the shared store and does not depend
|
|
65
|
+
on this call reaching the worker.
|
|
66
|
+
|
|
67
|
+
## Install
|
|
68
|
+
|
|
69
|
+
Unpublished for now: inside this repo use `workspace:*`; from a sibling checkout use a `link:` override (see [AGENTS.md](../../AGENTS.md)).
|
|
70
|
+
|
|
71
|
+
## See also
|
|
72
|
+
|
|
73
|
+
- API reference: section "`@loomwork/worker-runtime` — the SPI" in [docs/api-reference.org](../../docs/api-reference.org).
|
|
74
|
+
- Guide: [user guide, section 13: where executions run](../../docs/user-guide.md#13-where-executions-run-compute); `runWorkerRuntimeConformance` in `@loomwork/conformance` proves an implementation.
|
|
75
|
+
- ADRs: [0001 dispatcher and worker runtime](../../docs/adr/0001-execution-dispatcher-and-worker-runtime.md), [0002 HTTP worker](../../docs/adr/0002-http-worker-and-remote-worker-runtime.md), [0003 Cloud Run](../../docs/adr/0003-cloud-run-worker-runtime.md), [0004 failure recovery](../../docs/adr/0004-failure-recovery.md).
|
|
76
|
+
- Source: `packages/worker-runtime/src`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,aAAa,EACb,aAAa,GACd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,wBAAwB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isSupersededRefusal = exports.ExecutionSupersededError = void 0;
|
|
4
|
+
var superseded_1 = require("./superseded");
|
|
5
|
+
Object.defineProperty(exports, "ExecutionSupersededError", { enumerable: true, get: function () { return superseded_1.ExecutionSupersededError; } });
|
|
6
|
+
Object.defineProperty(exports, "isSupersededRefusal", { enumerable: true, get: function () { return superseded_1.isSupersededRefusal; } });
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAQA,2CAA6E;AAApE,sHAAA,wBAAwB,OAAA;AAAE,iHAAA,mBAAmB,OAAA"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L20 (ADR 0004) — `execute` was refused because ANOTHER holder owns the execution. This is the
|
|
3
|
+
* portable form of that refusal: the HTTP worker answers it with
|
|
4
|
+
* `409 { code: "superseded", executionId }` and `RemoteWorkerRuntime` rethrows it as this error,
|
|
5
|
+
* so a caller on the far side of a wire can tell it apart from a transport or storage failure
|
|
6
|
+
* exactly as an in-process caller can.
|
|
7
|
+
*
|
|
8
|
+
* It is NOT a failed attempt. Every dispatch acquires a newer fence, so retrying it supersedes
|
|
9
|
+
* the holder that just won, which is then fenced out and retries in turn — the two ping-pong and
|
|
10
|
+
* each bounce re-runs the in-flight step. So a dispatcher settles it instead of retrying.
|
|
11
|
+
*
|
|
12
|
+
* What settling means: the refusal TRANSFERS ownership to the newer fence holder, and nothing
|
|
13
|
+
* here checks that holder is alive. Its liveness is covered only by its own dispatcher's policy
|
|
14
|
+
* (retries, `executeTimeoutMs`) and, past that, by `recoverAbandoned` — which a deployment MUST
|
|
15
|
+
* run to cover holders that die (a duplicate dispatcher that crashed mid-run, or one that timed
|
|
16
|
+
* out while its wedged instance kept the fence). Until then the execution may sit in `running`.
|
|
17
|
+
*
|
|
18
|
+
* The newer holder can even be this caller's own earlier attempt (an HTTP attempt that timed out
|
|
19
|
+
* but kept running): the execution then completes while `dispatch()` rejects with this error.
|
|
20
|
+
* So a caller that needs the OUTCOME should treat this error as "await by id" —
|
|
21
|
+
* `loadState` / `subscribe` on the same executionId — not as a failure of the execution.
|
|
22
|
+
*/
|
|
23
|
+
export declare class ExecutionSupersededError extends Error {
|
|
24
|
+
readonly executionId: string;
|
|
25
|
+
/** What the worker was refused with, when known (in-process). Absent across a wire. */
|
|
26
|
+
readonly cause?: unknown | undefined;
|
|
27
|
+
constructor(executionId: string,
|
|
28
|
+
/** What the worker was refused with, when known (in-process). Absent across a wire. */
|
|
29
|
+
cause?: unknown | undefined);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* L20 (ADR 0004) — does `err` mean "a newer or other holder owns `executionId`", rather than
|
|
33
|
+
* "this attempt failed"? A dispatcher MUST NOT retry or redispatch on `true`.
|
|
34
|
+
*
|
|
35
|
+
* Exactly three shapes, each REQUIRED to name `executionId` so an error about some other
|
|
36
|
+
* execution never qualifies:
|
|
37
|
+
* - `ExecutionAlreadyExistsError` — a start lost the sequence-0 race (ADR 0103);
|
|
38
|
+
* - `LeaseHeldError` / `LeaseLostError` (ADR 0130 D11), bare or as the `cause` of a
|
|
39
|
+
* `StorageWriteError` (a write the lease gate refused) — a live owner holds the lease, or this
|
|
40
|
+
* driver lost its own;
|
|
41
|
+
* - `StaleFenceError`, bare or as the `cause` of a `StorageWriteError` — this attempt's token
|
|
42
|
+
* was superseded by a newer `acquireFence` (a redispatch, a recovery pass, a fenced wake or a
|
|
43
|
+
* cancel). The id check matters: a coalesced batch rejects every caller in it with ONE
|
|
44
|
+
* writer's error (ADR 0074 L1), so a stale fence naming a different execution is that
|
|
45
|
+
* writer's refusal and, for this one, a genuine storage failure — retryable;
|
|
46
|
+
* - `ExecutionSupersededError` — the wire form of either.
|
|
47
|
+
*
|
|
48
|
+
* Not included: `StartLeaseHeldError` (a caller-chosen start that found a lease but NO sequence 0, a
|
|
49
|
+
* possible ghost; same reasoning) and `StartFencedOutError` (`@loomwork/workflow-runtime`) — a start refused on the fence
|
|
50
|
+
* with sequence 0 still absent. Its newer token's holder has written nothing and may be dead, so
|
|
51
|
+
* settling would lose the start outright; it stays retryable.
|
|
52
|
+
*
|
|
53
|
+
* Accepted imprecision: a coalesced batch holding writes for TWO fenced-out executions rejects
|
|
54
|
+
* both with the first one's error, so the second sees a stale fence naming another execution and
|
|
55
|
+
* retries. That retry takes a newer fence, so it can supersede the second execution's live holder
|
|
56
|
+
* and re-run its in-flight step once more; the holder it fences out then gets a refusal naming
|
|
57
|
+
* its own execution and settles. One bounded extra bounce per such batch — not a ping-pong —
|
|
58
|
+
* accepted rather than guessing which execution a batch error "really" meant.
|
|
59
|
+
*
|
|
60
|
+
* Every other error, including any other `StorageWriteError`, is `false`: a genuine failure is
|
|
61
|
+
* never swallowed. Matched by `name`, not class, because `@loomwork/worker-runtime` sits below the
|
|
62
|
+
* packages that define the first two (layering), and so a second bundled copy of any of them
|
|
63
|
+
* cannot hide a refusal — the same rule as `isStaleFence` in `@loomwork/workflow-runtime`.
|
|
64
|
+
*/
|
|
65
|
+
export declare function isSupersededRefusal(err: unknown, executionId: string): boolean;
|
|
66
|
+
//# sourceMappingURL=superseded.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"superseded.d.ts","sourceRoot":"","sources":["../src/superseded.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAE/C,QAAQ,CAAC,WAAW,EAAE,MAAM;IAC5B,uFAAuF;IACvF,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO;gBAFf,WAAW,EAAE,MAAM;IAC5B,uFAAuF;IAC9E,KAAK,CAAC,EAAE,OAAO,YAAA;CAQ3B;AAQD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAyB9E"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExecutionSupersededError = void 0;
|
|
4
|
+
exports.isSupersededRefusal = isSupersededRefusal;
|
|
5
|
+
/**
|
|
6
|
+
* L20 (ADR 0004) — `execute` was refused because ANOTHER holder owns the execution. This is the
|
|
7
|
+
* portable form of that refusal: the HTTP worker answers it with
|
|
8
|
+
* `409 { code: "superseded", executionId }` and `RemoteWorkerRuntime` rethrows it as this error,
|
|
9
|
+
* so a caller on the far side of a wire can tell it apart from a transport or storage failure
|
|
10
|
+
* exactly as an in-process caller can.
|
|
11
|
+
*
|
|
12
|
+
* It is NOT a failed attempt. Every dispatch acquires a newer fence, so retrying it supersedes
|
|
13
|
+
* the holder that just won, which is then fenced out and retries in turn — the two ping-pong and
|
|
14
|
+
* each bounce re-runs the in-flight step. So a dispatcher settles it instead of retrying.
|
|
15
|
+
*
|
|
16
|
+
* What settling means: the refusal TRANSFERS ownership to the newer fence holder, and nothing
|
|
17
|
+
* here checks that holder is alive. Its liveness is covered only by its own dispatcher's policy
|
|
18
|
+
* (retries, `executeTimeoutMs`) and, past that, by `recoverAbandoned` — which a deployment MUST
|
|
19
|
+
* run to cover holders that die (a duplicate dispatcher that crashed mid-run, or one that timed
|
|
20
|
+
* out while its wedged instance kept the fence). Until then the execution may sit in `running`.
|
|
21
|
+
*
|
|
22
|
+
* The newer holder can even be this caller's own earlier attempt (an HTTP attempt that timed out
|
|
23
|
+
* but kept running): the execution then completes while `dispatch()` rejects with this error.
|
|
24
|
+
* So a caller that needs the OUTCOME should treat this error as "await by id" —
|
|
25
|
+
* `loadState` / `subscribe` on the same executionId — not as a failure of the execution.
|
|
26
|
+
*/
|
|
27
|
+
class ExecutionSupersededError extends Error {
|
|
28
|
+
executionId;
|
|
29
|
+
cause;
|
|
30
|
+
constructor(executionId,
|
|
31
|
+
/** What the worker was refused with, when known (in-process). Absent across a wire. */
|
|
32
|
+
cause) {
|
|
33
|
+
super(`execution "${executionId}" is owned by another holder (a lost start race or a newer fence); this dispatch was superseded, not failed — redispatching would fence that holder out. Await the execution by id (loadState/subscribe); if its holder has died, recoverAbandoned re-drives it.`);
|
|
34
|
+
this.executionId = executionId;
|
|
35
|
+
this.cause = cause;
|
|
36
|
+
this.name = "ExecutionSupersededError";
|
|
37
|
+
Object.setPrototypeOf(this, ExecutionSupersededError.prototype);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ExecutionSupersededError = ExecutionSupersededError;
|
|
41
|
+
function field(value, key) {
|
|
42
|
+
return value !== null && typeof value === "object"
|
|
43
|
+
? value[key]
|
|
44
|
+
: undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* L20 (ADR 0004) — does `err` mean "a newer or other holder owns `executionId`", rather than
|
|
48
|
+
* "this attempt failed"? A dispatcher MUST NOT retry or redispatch on `true`.
|
|
49
|
+
*
|
|
50
|
+
* Exactly three shapes, each REQUIRED to name `executionId` so an error about some other
|
|
51
|
+
* execution never qualifies:
|
|
52
|
+
* - `ExecutionAlreadyExistsError` — a start lost the sequence-0 race (ADR 0103);
|
|
53
|
+
* - `LeaseHeldError` / `LeaseLostError` (ADR 0130 D11), bare or as the `cause` of a
|
|
54
|
+
* `StorageWriteError` (a write the lease gate refused) — a live owner holds the lease, or this
|
|
55
|
+
* driver lost its own;
|
|
56
|
+
* - `StaleFenceError`, bare or as the `cause` of a `StorageWriteError` — this attempt's token
|
|
57
|
+
* was superseded by a newer `acquireFence` (a redispatch, a recovery pass, a fenced wake or a
|
|
58
|
+
* cancel). The id check matters: a coalesced batch rejects every caller in it with ONE
|
|
59
|
+
* writer's error (ADR 0074 L1), so a stale fence naming a different execution is that
|
|
60
|
+
* writer's refusal and, for this one, a genuine storage failure — retryable;
|
|
61
|
+
* - `ExecutionSupersededError` — the wire form of either.
|
|
62
|
+
*
|
|
63
|
+
* Not included: `StartLeaseHeldError` (a caller-chosen start that found a lease but NO sequence 0, a
|
|
64
|
+
* possible ghost; same reasoning) and `StartFencedOutError` (`@loomwork/workflow-runtime`) — a start refused on the fence
|
|
65
|
+
* with sequence 0 still absent. Its newer token's holder has written nothing and may be dead, so
|
|
66
|
+
* settling would lose the start outright; it stays retryable.
|
|
67
|
+
*
|
|
68
|
+
* Accepted imprecision: a coalesced batch holding writes for TWO fenced-out executions rejects
|
|
69
|
+
* both with the first one's error, so the second sees a stale fence naming another execution and
|
|
70
|
+
* retries. That retry takes a newer fence, so it can supersede the second execution's live holder
|
|
71
|
+
* and re-run its in-flight step once more; the holder it fences out then gets a refusal naming
|
|
72
|
+
* its own execution and settles. One bounded extra bounce per such batch — not a ping-pong —
|
|
73
|
+
* accepted rather than guessing which execution a batch error "really" meant.
|
|
74
|
+
*
|
|
75
|
+
* Every other error, including any other `StorageWriteError`, is `false`: a genuine failure is
|
|
76
|
+
* never swallowed. Matched by `name`, not class, because `@loomwork/worker-runtime` sits below the
|
|
77
|
+
* packages that define the first two (layering), and so a second bundled copy of any of them
|
|
78
|
+
* cannot hide a refusal — the same rule as `isStaleFence` in `@loomwork/workflow-runtime`.
|
|
79
|
+
*/
|
|
80
|
+
function isSupersededRefusal(err, executionId) {
|
|
81
|
+
const name = field(err, "name");
|
|
82
|
+
if (name === "ExecutionAlreadyExistsError" ||
|
|
83
|
+
name === "ExecutionSupersededError" ||
|
|
84
|
+
name === "StaleFenceError" ||
|
|
85
|
+
// ADR 0130 D11: a driver stood down (another live owner holds the lease) or lost its lease.
|
|
86
|
+
name === "LeaseHeldError" ||
|
|
87
|
+
name === "LeaseLostError") {
|
|
88
|
+
return field(err, "executionId") === executionId;
|
|
89
|
+
}
|
|
90
|
+
if (name === "StorageWriteError") {
|
|
91
|
+
const cause = field(err, "cause");
|
|
92
|
+
// ADR 0130 D11: a write refused because the lease behind it was lost (or held by another) is the
|
|
93
|
+
// same "another holder owns it" as a stale fence, when it names this execution.
|
|
94
|
+
const causeName = field(cause, "name");
|
|
95
|
+
return ((causeName === "StaleFenceError" ||
|
|
96
|
+
causeName === "LeaseLostError" ||
|
|
97
|
+
causeName === "LeaseHeldError") &&
|
|
98
|
+
field(cause, "executionId") === executionId);
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=superseded.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"superseded.js","sourceRoot":"","sources":["../src/superseded.ts"],"names":[],"mappings":";;;AA4EA,kDAyBC;AArGD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,wBAAyB,SAAQ,KAAK;IAEtC;IAEA;IAHX,YACW,WAAmB;IAC5B,uFAAuF;IAC9E,KAAe;QAExB,KAAK,CACH,cAAc,WAAW,kQAAkQ,CAC5R,CAAC;QANO,gBAAW,GAAX,WAAW,CAAQ;QAEnB,UAAK,GAAL,KAAK,CAAU;QAKxB,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;QACvC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,wBAAwB,CAAC,SAAS,CAAC,CAAC;IAClE,CAAC;CACF;AAZD,4DAYC;AAED,SAAS,KAAK,CAAC,KAAc,EAAE,GAAW;IACxC,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAChD,CAAC,CAAE,KAAiC,CAAC,GAAG,CAAC;QACzC,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,SAAgB,mBAAmB,CAAC,GAAY,EAAE,WAAmB;IACnE,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAChC,IACE,IAAI,KAAK,6BAA6B;QACtC,IAAI,KAAK,0BAA0B;QACnC,IAAI,KAAK,iBAAiB;QAC1B,4FAA4F;QAC5F,IAAI,KAAK,gBAAgB;QACzB,IAAI,KAAK,gBAAgB,EACzB,CAAC;QACD,OAAO,KAAK,CAAC,GAAG,EAAE,aAAa,CAAC,KAAK,WAAW,CAAC;IACnD,CAAC;IACD,IAAI,IAAI,KAAK,mBAAmB,EAAE,CAAC;QACjC,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAClC,iGAAiG;QACjG,gFAAgF;QAChF,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACvC,OAAO,CACL,CAAC,SAAS,KAAK,iBAAiB;YAC9B,SAAS,KAAK,gBAAgB;YAC9B,SAAS,KAAK,gBAAgB,CAAC;YACjC,KAAK,CAAC,KAAK,EAAE,aAAa,CAAC,KAAK,WAAW,CAC5C,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { ExecutionState, SuspendCondition } from "@loomwork/core";
|
|
2
|
+
/**
|
|
3
|
+
* The immutable, JSON-serializable description of ONE execution to run on a
|
|
4
|
+
* worker. It is the sole payload that crosses the placement boundary
|
|
5
|
+
* (in-process, HTTP, Cloud Run). It carries **identity + resume coordinates**,
|
|
6
|
+
* never live code: the worker resolves the flow/effects by id from the code it
|
|
7
|
+
* was booted with (`flowId`), and reconstructs state from the shared event +
|
|
8
|
+
* snapshot stores.
|
|
9
|
+
*
|
|
10
|
+
* Two shapes:
|
|
11
|
+
* - fresh start: `input` set, `resumeEvent` absent.
|
|
12
|
+
* - resume: `resumeEvent` set (e.g. an approval decision). The worker
|
|
13
|
+
* rebuilds from the latest snapshot + event tail and continues.
|
|
14
|
+
*/
|
|
15
|
+
export interface ExecutionPackage {
|
|
16
|
+
executionId: string;
|
|
17
|
+
/** Workflow/flow definition id — used to fold identity and resolve code. */
|
|
18
|
+
workflowId: string;
|
|
19
|
+
/** Registry key the worker uses to resolve the flow's runner + effects. */
|
|
20
|
+
flowId: string;
|
|
21
|
+
/** Input for a fresh start. Ignored on resume (it lives in the event log). */
|
|
22
|
+
input?: unknown;
|
|
23
|
+
/** Present ⇒ this is a resume dispatch. Delivered as the resume payload. */
|
|
24
|
+
resumeEvent?: {
|
|
25
|
+
eventType: string;
|
|
26
|
+
payload: unknown;
|
|
27
|
+
waitId?: string;
|
|
28
|
+
};
|
|
29
|
+
/** Optional resume hints (a specific snapshot / event offset to rebuild from). */
|
|
30
|
+
snapshotId?: string;
|
|
31
|
+
fromSequence?: number;
|
|
32
|
+
/** Opaque routing/telemetry metadata; never load-bearing for correctness. */
|
|
33
|
+
metadata?: Record<string, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* R3 (DISPATCHER_SPI 1.2.0) — the AUTHENTICATED principal this dispatch acts for (a
|
|
36
|
+
* `PrincipalChain`: `actor`, `tenant`, …), as JSON. A worker that fails closed on a missing
|
|
37
|
+
* principal (`security.strict`) authorizes the start/resume/query/cancel against it; a package
|
|
38
|
+
* WITHOUT one is anonymous and is refused there unless the worker was explicitly given the
|
|
39
|
+
* kernel's system identity. It crosses the wire as plain data, so a receiving worker must
|
|
40
|
+
* only honour it from a caller it has AUTHENTICATED (the worker-server does so only when its
|
|
41
|
+
* `authorize` gate is configured): whoever can post a package can otherwise name any actor.
|
|
42
|
+
* Never load-bearing for placement.
|
|
43
|
+
*/
|
|
44
|
+
principal?: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
/** Where an execution ended for this dispatch. A suspend is a normal outcome. */
|
|
47
|
+
export type ExecutionOutcome = "completed" | "suspended" | "failed";
|
|
48
|
+
/**
|
|
49
|
+
* What a worker returns after running an ExecutionPackage to a terminal-or-
|
|
50
|
+
* suspended state and persisting its events/snapshot. This is exactly the JSON a
|
|
51
|
+
* remote worker sends back over `POST /execute`, plus the folded terminal state
|
|
52
|
+
* so in-process callers avoid a re-read.
|
|
53
|
+
*/
|
|
54
|
+
export interface ExecutionResult {
|
|
55
|
+
executionId: string;
|
|
56
|
+
status: ExecutionOutcome;
|
|
57
|
+
/** Folded execution state at the stopping point (completed / suspended / failed). */
|
|
58
|
+
state: ExecutionState;
|
|
59
|
+
/** `state.variables._output` on completion; convenience mirror. */
|
|
60
|
+
output?: unknown;
|
|
61
|
+
/** `state.variables._error` on failure; convenience mirror. */
|
|
62
|
+
error?: unknown;
|
|
63
|
+
/** The condition the execution suspended on, when `status === "suspended"`. */
|
|
64
|
+
suspendedOn?: SuspendCondition;
|
|
65
|
+
/** Highest event sequence persisted for this execution. */
|
|
66
|
+
lastSequence: number;
|
|
67
|
+
}
|
|
68
|
+
/** A request to reserve worker capacity for one execution. */
|
|
69
|
+
export interface WorkerRequest {
|
|
70
|
+
executionId: string;
|
|
71
|
+
metadata?: Record<string, unknown>;
|
|
72
|
+
/** R3 — the dispatch's principal (see {@link ExecutionPackage.principal}), so allocation is authorized as the caller. */
|
|
73
|
+
principal?: Record<string, unknown>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A reservation of worker capacity. For LocalWorkerRuntime this is a trivial
|
|
77
|
+
* in-process token; for CloudRunWorkerRuntime it names the allocated instance.
|
|
78
|
+
*/
|
|
79
|
+
export interface WorkerLease {
|
|
80
|
+
leaseId: string;
|
|
81
|
+
/**
|
|
82
|
+
* K9.4 — the monotonic fencing token this dispatch holds (acquired from a
|
|
83
|
+
* fencing event store at allocate time). Every event the run appends carries
|
|
84
|
+
* it, so after a re-dispatch acquires a newer token, this lease's holder can
|
|
85
|
+
* no longer write — a partitioned "zombie" worker is fenced out at the store.
|
|
86
|
+
* Absent on non-fencing stores (e.g. single-node SQLite): correlation only.
|
|
87
|
+
*/
|
|
88
|
+
fence?: number;
|
|
89
|
+
metadata?: Record<string, unknown>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The Platform SPI. A `WorkerRuntime` abstracts *how/where* an ExecutionPackage
|
|
93
|
+
* runs. Implementations: LocalWorkerRuntime (in-process), RemoteWorkerRuntime
|
|
94
|
+
* (HTTP), CloudRunWorkerRuntime. Optional platform capabilities
|
|
95
|
+
* (secrets/identity/logging/metrics) hang off the same adapter so cloud concerns
|
|
96
|
+
* stay isolated behind the interface.
|
|
97
|
+
*/
|
|
98
|
+
export interface WorkerRuntime {
|
|
99
|
+
/**
|
|
100
|
+
* R3 — OPTIONAL: `true` when this worker runs principal-less packages under the kernel's
|
|
101
|
+
* SYSTEM identity (it bypasses the execution authorizer). A transport that exposes the worker
|
|
102
|
+
* to a network (the worker-server) refuses to be built around such a worker without an
|
|
103
|
+
* authentication gate, because every caller it admits would otherwise be the system.
|
|
104
|
+
*/
|
|
105
|
+
readonly runsAsSystem?: boolean;
|
|
106
|
+
allocate(request: WorkerRequest): Promise<WorkerLease>;
|
|
107
|
+
execute(lease: WorkerLease, pkg: ExecutionPackage): Promise<ExecutionResult>;
|
|
108
|
+
release(lease: WorkerLease): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* OPTIONAL (K8.5) — best-effort cancellation of an execution this worker may
|
|
111
|
+
* be driving: aborts its in-flight effect and records the durable
|
|
112
|
+
* `EXECUTION_CANCELLED`. Returns whether a cancellation was performed here
|
|
113
|
+
* (`false` when the execution is unknown to this worker or already terminal).
|
|
114
|
+
* Delivery is best-effort by contract: the caller's durable settlement — the
|
|
115
|
+
* orchestrator's `ExecutionRuntime.cancel()` fence-then-cancel against the
|
|
116
|
+
* shared store — never depends on this call reaching the worker; this exists
|
|
117
|
+
* so a live remote effect is severed promptly instead of running to fenced
|
|
118
|
+
* rejection.
|
|
119
|
+
*/
|
|
120
|
+
cancel?(executionId: string, reason?: string): Promise<boolean>;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvE;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,MAAM,CAAC;IACpB,4EAA4E;IAC5E,UAAU,EAAE,MAAM,CAAC;IACnB,2EAA2E;IAC3E,MAAM,EAAE,MAAM,CAAC;IACf,8EAA8E;IAC9E,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,4EAA4E;IAC5E,WAAW,CAAC,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACvE,kFAAkF;IAClF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,6EAA6E;IAC7E,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC;;;;;;;;;OASG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED,iFAAiF;AACjF,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,WAAW,GAAG,QAAQ,CAAC;AAEpE;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,gBAAgB,CAAC;IACzB,qFAAqF;IACrF,KAAK,EAAE,cAAc,CAAC;IACtB,mEAAmE;IACnE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,+DAA+D;IAC/D,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,+EAA+E;IAC/E,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAC/B,2DAA2D;IAC3D,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,8DAA8D;AAC9D,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,yHAAyH;IACzH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;OAKG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IACvD,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;IAC7E,OAAO,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3C;;;;;;;;;;OAUG;IACH,MAAM,CAAC,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACjE"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loomwork/worker-runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Worker runtime SPI for Loom — the platform-agnostic contract for executing an ExecutionPackage on some compute placement (local, remote HTTP, Cloud Run)",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@loomwork/core": "^0.1.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^22.0.0",
|
|
19
|
+
"typescript": "^5.6.3",
|
|
20
|
+
"vitest": "^2.1.8"
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/chhetrisushil/loom.git",
|
|
26
|
+
"directory": "packages/worker-runtime"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src",
|
|
31
|
+
"!src/**/*.test.ts",
|
|
32
|
+
"!src/**/*.test.tsx",
|
|
33
|
+
"!src/**/__tests__",
|
|
34
|
+
"!src/**/__fixtures__",
|
|
35
|
+
"!src/**/__snapshots__",
|
|
36
|
+
"!src/**/__golden__",
|
|
37
|
+
"LICENSE",
|
|
38
|
+
"NOTICE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"tag": "next"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=20.0.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc --build",
|
|
49
|
+
"dev": "tsc --build --watch",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
53
|
+
"typecheck:tests": "tsc -p tsconfig.test.json"
|
|
54
|
+
}
|
|
6
55
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L20 (ADR 0004) — `execute` was refused because ANOTHER holder owns the execution. This is the
|
|
3
|
+
* portable form of that refusal: the HTTP worker answers it with
|
|
4
|
+
* `409 { code: "superseded", executionId }` and `RemoteWorkerRuntime` rethrows it as this error,
|
|
5
|
+
* so a caller on the far side of a wire can tell it apart from a transport or storage failure
|
|
6
|
+
* exactly as an in-process caller can.
|
|
7
|
+
*
|
|
8
|
+
* It is NOT a failed attempt. Every dispatch acquires a newer fence, so retrying it supersedes
|
|
9
|
+
* the holder that just won, which is then fenced out and retries in turn — the two ping-pong and
|
|
10
|
+
* each bounce re-runs the in-flight step. So a dispatcher settles it instead of retrying.
|
|
11
|
+
*
|
|
12
|
+
* What settling means: the refusal TRANSFERS ownership to the newer fence holder, and nothing
|
|
13
|
+
* here checks that holder is alive. Its liveness is covered only by its own dispatcher's policy
|
|
14
|
+
* (retries, `executeTimeoutMs`) and, past that, by `recoverAbandoned` — which a deployment MUST
|
|
15
|
+
* run to cover holders that die (a duplicate dispatcher that crashed mid-run, or one that timed
|
|
16
|
+
* out while its wedged instance kept the fence). Until then the execution may sit in `running`.
|
|
17
|
+
*
|
|
18
|
+
* The newer holder can even be this caller's own earlier attempt (an HTTP attempt that timed out
|
|
19
|
+
* but kept running): the execution then completes while `dispatch()` rejects with this error.
|
|
20
|
+
* So a caller that needs the OUTCOME should treat this error as "await by id" —
|
|
21
|
+
* `loadState` / `subscribe` on the same executionId — not as a failure of the execution.
|
|
22
|
+
*/
|
|
23
|
+
export class ExecutionSupersededError extends Error {
|
|
24
|
+
constructor(
|
|
25
|
+
readonly executionId: string,
|
|
26
|
+
/** What the worker was refused with, when known (in-process). Absent across a wire. */
|
|
27
|
+
readonly cause?: unknown
|
|
28
|
+
) {
|
|
29
|
+
super(
|
|
30
|
+
`execution "${executionId}" is owned by another holder (a lost start race or a newer fence); this dispatch was superseded, not failed — redispatching would fence that holder out. Await the execution by id (loadState/subscribe); if its holder has died, recoverAbandoned re-drives it.`
|
|
31
|
+
);
|
|
32
|
+
this.name = "ExecutionSupersededError";
|
|
33
|
+
Object.setPrototypeOf(this, ExecutionSupersededError.prototype);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function field(value: unknown, key: string): unknown {
|
|
38
|
+
return value !== null && typeof value === "object"
|
|
39
|
+
? (value as Record<string, unknown>)[key]
|
|
40
|
+
: undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* L20 (ADR 0004) — does `err` mean "a newer or other holder owns `executionId`", rather than
|
|
45
|
+
* "this attempt failed"? A dispatcher MUST NOT retry or redispatch on `true`.
|
|
46
|
+
*
|
|
47
|
+
* Exactly three shapes, each REQUIRED to name `executionId` so an error about some other
|
|
48
|
+
* execution never qualifies:
|
|
49
|
+
* - `ExecutionAlreadyExistsError` — a start lost the sequence-0 race (ADR 0103);
|
|
50
|
+
* - `LeaseHeldError` / `LeaseLostError` (ADR 0130 D11), bare or as the `cause` of a
|
|
51
|
+
* `StorageWriteError` (a write the lease gate refused) — a live owner holds the lease, or this
|
|
52
|
+
* driver lost its own;
|
|
53
|
+
* - `StaleFenceError`, bare or as the `cause` of a `StorageWriteError` — this attempt's token
|
|
54
|
+
* was superseded by a newer `acquireFence` (a redispatch, a recovery pass, a fenced wake or a
|
|
55
|
+
* cancel). The id check matters: a coalesced batch rejects every caller in it with ONE
|
|
56
|
+
* writer's error (ADR 0074 L1), so a stale fence naming a different execution is that
|
|
57
|
+
* writer's refusal and, for this one, a genuine storage failure — retryable;
|
|
58
|
+
* - `ExecutionSupersededError` — the wire form of either.
|
|
59
|
+
*
|
|
60
|
+
* Not included: `StartLeaseHeldError` (a caller-chosen start that found a lease but NO sequence 0, a
|
|
61
|
+
* possible ghost; same reasoning) and `StartFencedOutError` (`@loomwork/workflow-runtime`) — a start refused on the fence
|
|
62
|
+
* with sequence 0 still absent. Its newer token's holder has written nothing and may be dead, so
|
|
63
|
+
* settling would lose the start outright; it stays retryable.
|
|
64
|
+
*
|
|
65
|
+
* Accepted imprecision: a coalesced batch holding writes for TWO fenced-out executions rejects
|
|
66
|
+
* both with the first one's error, so the second sees a stale fence naming another execution and
|
|
67
|
+
* retries. That retry takes a newer fence, so it can supersede the second execution's live holder
|
|
68
|
+
* and re-run its in-flight step once more; the holder it fences out then gets a refusal naming
|
|
69
|
+
* its own execution and settles. One bounded extra bounce per such batch — not a ping-pong —
|
|
70
|
+
* accepted rather than guessing which execution a batch error "really" meant.
|
|
71
|
+
*
|
|
72
|
+
* Every other error, including any other `StorageWriteError`, is `false`: a genuine failure is
|
|
73
|
+
* never swallowed. Matched by `name`, not class, because `@loomwork/worker-runtime` sits below the
|
|
74
|
+
* packages that define the first two (layering), and so a second bundled copy of any of them
|
|
75
|
+
* cannot hide a refusal — the same rule as `isStaleFence` in `@loomwork/workflow-runtime`.
|
|
76
|
+
*/
|
|
77
|
+
export function isSupersededRefusal(err: unknown, executionId: string): boolean {
|
|
78
|
+
const name = field(err, "name");
|
|
79
|
+
if (
|
|
80
|
+
name === "ExecutionAlreadyExistsError" ||
|
|
81
|
+
name === "ExecutionSupersededError" ||
|
|
82
|
+
name === "StaleFenceError" ||
|
|
83
|
+
// ADR 0130 D11: a driver stood down (another live owner holds the lease) or lost its lease.
|
|
84
|
+
name === "LeaseHeldError" ||
|
|
85
|
+
name === "LeaseLostError"
|
|
86
|
+
) {
|
|
87
|
+
return field(err, "executionId") === executionId;
|
|
88
|
+
}
|
|
89
|
+
if (name === "StorageWriteError") {
|
|
90
|
+
const cause = field(err, "cause");
|
|
91
|
+
// ADR 0130 D11: a write refused because the lease behind it was lost (or held by another) is the
|
|
92
|
+
// same "another holder owns it" as a stale fence, when it names this execution.
|
|
93
|
+
const causeName = field(cause, "name");
|
|
94
|
+
return (
|
|
95
|
+
(causeName === "StaleFenceError" ||
|
|
96
|
+
causeName === "LeaseLostError" ||
|
|
97
|
+
causeName === "LeaseHeldError") &&
|
|
98
|
+
field(cause, "executionId") === executionId
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { ExecutionState, SuspendCondition } from "@loomwork/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The immutable, JSON-serializable description of ONE execution to run on a
|
|
5
|
+
* worker. It is the sole payload that crosses the placement boundary
|
|
6
|
+
* (in-process, HTTP, Cloud Run). It carries **identity + resume coordinates**,
|
|
7
|
+
* never live code: the worker resolves the flow/effects by id from the code it
|
|
8
|
+
* was booted with (`flowId`), and reconstructs state from the shared event +
|
|
9
|
+
* snapshot stores.
|
|
10
|
+
*
|
|
11
|
+
* Two shapes:
|
|
12
|
+
* - fresh start: `input` set, `resumeEvent` absent.
|
|
13
|
+
* - resume: `resumeEvent` set (e.g. an approval decision). The worker
|
|
14
|
+
* rebuilds from the latest snapshot + event tail and continues.
|
|
15
|
+
*/
|
|
16
|
+
export interface ExecutionPackage {
|
|
17
|
+
executionId: string;
|
|
18
|
+
/** Workflow/flow definition id — used to fold identity and resolve code. */
|
|
19
|
+
workflowId: string;
|
|
20
|
+
/** Registry key the worker uses to resolve the flow's runner + effects. */
|
|
21
|
+
flowId: string;
|
|
22
|
+
/** Input for a fresh start. Ignored on resume (it lives in the event log). */
|
|
23
|
+
input?: unknown;
|
|
24
|
+
/** Present ⇒ this is a resume dispatch. Delivered as the resume payload. */
|
|
25
|
+
resumeEvent?: { eventType: string; payload: unknown; waitId?: string };
|
|
26
|
+
/** Optional resume hints (a specific snapshot / event offset to rebuild from). */
|
|
27
|
+
snapshotId?: string;
|
|
28
|
+
fromSequence?: number;
|
|
29
|
+
/** Opaque routing/telemetry metadata; never load-bearing for correctness. */
|
|
30
|
+
metadata?: Record<string, unknown>;
|
|
31
|
+
/**
|
|
32
|
+
* R3 (DISPATCHER_SPI 1.2.0) — the AUTHENTICATED principal this dispatch acts for (a
|
|
33
|
+
* `PrincipalChain`: `actor`, `tenant`, …), as JSON. A worker that fails closed on a missing
|
|
34
|
+
* principal (`security.strict`) authorizes the start/resume/query/cancel against it; a package
|
|
35
|
+
* WITHOUT one is anonymous and is refused there unless the worker was explicitly given the
|
|
36
|
+
* kernel's system identity. It crosses the wire as plain data, so a receiving worker must
|
|
37
|
+
* only honour it from a caller it has AUTHENTICATED (the worker-server does so only when its
|
|
38
|
+
* `authorize` gate is configured): whoever can post a package can otherwise name any actor.
|
|
39
|
+
* Never load-bearing for placement.
|
|
40
|
+
*/
|
|
41
|
+
principal?: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Where an execution ended for this dispatch. A suspend is a normal outcome. */
|
|
45
|
+
export type ExecutionOutcome = "completed" | "suspended" | "failed";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* What a worker returns after running an ExecutionPackage to a terminal-or-
|
|
49
|
+
* suspended state and persisting its events/snapshot. This is exactly the JSON a
|
|
50
|
+
* remote worker sends back over `POST /execute`, plus the folded terminal state
|
|
51
|
+
* so in-process callers avoid a re-read.
|
|
52
|
+
*/
|
|
53
|
+
export interface ExecutionResult {
|
|
54
|
+
executionId: string;
|
|
55
|
+
status: ExecutionOutcome;
|
|
56
|
+
/** Folded execution state at the stopping point (completed / suspended / failed). */
|
|
57
|
+
state: ExecutionState;
|
|
58
|
+
/** `state.variables._output` on completion; convenience mirror. */
|
|
59
|
+
output?: unknown;
|
|
60
|
+
/** `state.variables._error` on failure; convenience mirror. */
|
|
61
|
+
error?: unknown;
|
|
62
|
+
/** The condition the execution suspended on, when `status === "suspended"`. */
|
|
63
|
+
suspendedOn?: SuspendCondition;
|
|
64
|
+
/** Highest event sequence persisted for this execution. */
|
|
65
|
+
lastSequence: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A request to reserve worker capacity for one execution. */
|
|
69
|
+
export interface WorkerRequest {
|
|
70
|
+
executionId: string;
|
|
71
|
+
metadata?: Record<string, unknown>;
|
|
72
|
+
/** R3 — the dispatch's principal (see {@link ExecutionPackage.principal}), so allocation is authorized as the caller. */
|
|
73
|
+
principal?: Record<string, unknown>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A reservation of worker capacity. For LocalWorkerRuntime this is a trivial
|
|
78
|
+
* in-process token; for CloudRunWorkerRuntime it names the allocated instance.
|
|
79
|
+
*/
|
|
80
|
+
export interface WorkerLease {
|
|
81
|
+
leaseId: string;
|
|
82
|
+
/**
|
|
83
|
+
* K9.4 — the monotonic fencing token this dispatch holds (acquired from a
|
|
84
|
+
* fencing event store at allocate time). Every event the run appends carries
|
|
85
|
+
* it, so after a re-dispatch acquires a newer token, this lease's holder can
|
|
86
|
+
* no longer write — a partitioned "zombie" worker is fenced out at the store.
|
|
87
|
+
* Absent on non-fencing stores (e.g. single-node SQLite): correlation only.
|
|
88
|
+
*/
|
|
89
|
+
fence?: number;
|
|
90
|
+
metadata?: Record<string, unknown>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The Platform SPI. A `WorkerRuntime` abstracts *how/where* an ExecutionPackage
|
|
95
|
+
* runs. Implementations: LocalWorkerRuntime (in-process), RemoteWorkerRuntime
|
|
96
|
+
* (HTTP), CloudRunWorkerRuntime. Optional platform capabilities
|
|
97
|
+
* (secrets/identity/logging/metrics) hang off the same adapter so cloud concerns
|
|
98
|
+
* stay isolated behind the interface.
|
|
99
|
+
*/
|
|
100
|
+
export interface WorkerRuntime {
|
|
101
|
+
/**
|
|
102
|
+
* R3 — OPTIONAL: `true` when this worker runs principal-less packages under the kernel's
|
|
103
|
+
* SYSTEM identity (it bypasses the execution authorizer). A transport that exposes the worker
|
|
104
|
+
* to a network (the worker-server) refuses to be built around such a worker without an
|
|
105
|
+
* authentication gate, because every caller it admits would otherwise be the system.
|
|
106
|
+
*/
|
|
107
|
+
readonly runsAsSystem?: boolean;
|
|
108
|
+
allocate(request: WorkerRequest): Promise<WorkerLease>;
|
|
109
|
+
execute(lease: WorkerLease, pkg: ExecutionPackage): Promise<ExecutionResult>;
|
|
110
|
+
release(lease: WorkerLease): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* OPTIONAL (K8.5) — best-effort cancellation of an execution this worker may
|
|
113
|
+
* be driving: aborts its in-flight effect and records the durable
|
|
114
|
+
* `EXECUTION_CANCELLED`. Returns whether a cancellation was performed here
|
|
115
|
+
* (`false` when the execution is unknown to this worker or already terminal).
|
|
116
|
+
* Delivery is best-effort by contract: the caller's durable settlement — the
|
|
117
|
+
* orchestrator's `ExecutionRuntime.cancel()` fence-then-cancel against the
|
|
118
|
+
* shared store — never depends on this call reaching the worker; this exists
|
|
119
|
+
* so a live remote effect is severed promptly instead of running to fenced
|
|
120
|
+
* rejection.
|
|
121
|
+
*/
|
|
122
|
+
cancel?(executionId: string, reason?: string): Promise<boolean>;
|
|
123
|
+
}
|