codemod 2.0.0-alpha.2 → 2.0.0-alpha.4
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/README.md +1 -2
- package/package.json +14 -16
- package/register.d.mts +2 -11
- package/register.mjs +53 -46
- package/runtime/addon.d.mts +119 -0
- package/runtime/addon.mjs +36 -40
- package/runtime/ast-grep.d.mts +20 -0
- package/runtime/ast-grep.mjs +50 -61
- package/runtime/context.d.mts +25 -0
- package/runtime/context.mjs +10 -64
- package/runtime/hooks.d.mts +2 -0
- package/runtime/hooks.mjs +4 -6
- package/runtime/host.d.mts +81 -81
- package/runtime/host.mjs +428 -326
- package/runtime/llm.d.mts +2 -0
- package/runtime/llm.mjs +1 -7
- package/runtime/modules.d.mts +3 -0
- package/runtime/modules.mjs +7 -10
- package/runtime/orchestrator-version.d.mts +2 -0
- package/runtime/orchestrator-version.mjs +3 -0
- package/runtime/orchestrator.d.mts +261 -0
- package/runtime/orchestrator.mjs +823 -608
- package/runtime/runtime.d.mts +15 -0
- package/runtime/runtime.mjs +18 -26
- package/runtime/workflow.d.mts +24 -0
- package/runtime/workflow.mjs +42 -121
- package/testing/index.d.mts +107 -155
- package/testing/index.mjs +552 -545
- package/types/index.d.ts +0 -1
- package/types/langs/package.json +3 -0
- package/types/modules/ast-grep.d.ts +22 -1
- package/types/modules/workflow.d.ts +386 -231
- package/vite.d.mts +4 -21
- package/vite.mjs +23 -17
- package/runtime/metrics.mjs +0 -45
- package/types/metrics.d.ts +0 -5
- package/types/modules/metrics.d.ts +0 -107
package/runtime/orchestrator.mjs
CHANGED
|
@@ -1,657 +1,872 @@
|
|
|
1
|
-
// Generated from crates/workflow/src/js/orchestrator.
|
|
2
|
-
|
|
3
|
-
// The workflow orchestrator: step(), replay, test plans and the task
|
|
4
|
-
// builders that make up `codemod/workflow`. It holds no host globals, so the
|
|
5
|
-
// same code runs in the engine's QuickJS context and in Node.js or Bun.
|
|
6
|
-
//
|
|
7
|
-
// `createRuntime(host)` returns the module's exports for one run. The host:
|
|
8
|
-
// history(): StepRecord[] steps recorded by earlier runs
|
|
9
|
-
// record(entry, ms) persist one step's outcome
|
|
10
|
-
// event(event) report step started / replayed
|
|
11
|
-
// canceled(): boolean whether a step about to start must not,
|
|
12
|
-
// because the run was asked to stop
|
|
13
|
-
// start(request): Promise<outcome> run a task; rejects with { name, message }
|
|
14
|
-
// evaluate(kind, expression, scope) evaluate a workflow.yaml `${{ }}` expression
|
|
15
|
-
// now(): number the real time, for step records
|
|
16
|
-
// advanceClock(ms) move the body's clock forward
|
|
17
|
-
// testPlan(): { until?, given? } | null when the run is a test
|
|
18
|
-
// stop() the test's `until` step has started
|
|
19
|
-
// wake?() a replayed step waits for `replayNext()`
|
|
20
|
-
// callbackId?(fn): string identifies a callback given as a function;
|
|
21
|
-
// hosts without it need the build to extract callbacks
|
|
22
|
-
//
|
|
23
|
-
// The host drives two more things, on the object `createRuntime` returns:
|
|
24
|
-
// replayNext(): boolean settle the next replayed step; call it whenever the
|
|
25
|
-
// workflow has nothing left to run, until it is false
|
|
26
|
-
// close() the run is ending: no step starts from here on
|
|
27
|
-
// isClosed(): boolean whether steps issued now would never start
|
|
28
|
-
|
|
29
|
-
export class NondeterminismError extends Error {
|
|
30
|
-
constructor(message) {
|
|
31
|
-
super(message);
|
|
32
|
-
this.name = "NondeterminismError";
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
1
|
+
// Generated by scripts/build.mjs from crates/workflow/src/js/orchestrator.ts; do not edit.
|
|
2
|
+
// --------------------------------------------------------------- errors ---
|
|
36
3
|
export class StepError extends Error {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
4
|
+
constructor(label, cause) {
|
|
5
|
+
super(`step '${label}' failed: ${messageOf(cause)}`);
|
|
6
|
+
this.name = "StepError";
|
|
7
|
+
this.label = label;
|
|
8
|
+
}
|
|
42
9
|
}
|
|
43
|
-
|
|
44
10
|
export class CanceledError extends Error {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
11
|
+
constructor(message = "the run was canceled") {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "CanceledError";
|
|
14
|
+
}
|
|
49
15
|
}
|
|
50
|
-
|
|
51
16
|
export class AwaitingApprovalError extends Error {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "AwaitingApprovalError";
|
|
20
|
+
}
|
|
56
21
|
}
|
|
57
|
-
|
|
22
|
+
// ---------------------------------------------------------------- tasks ---
|
|
58
23
|
const TASK = Symbol.for("codemod.workflow.task");
|
|
59
|
-
|
|
60
24
|
/**
|
|
61
25
|
* The task builders (`forEachFile`, `shell`, `jssg`, ...). They only describe
|
|
62
26
|
* work, so they need no run. `options.callbackId(fn)` lets callbacks be given
|
|
63
27
|
* as functions, for hosts that run them in place.
|
|
64
28
|
*/
|
|
65
29
|
export function createTasks(options = {}) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
30
|
+
/** Inserted by the build in place of an extracted callback. */
|
|
31
|
+
function __callback(id, captures) {
|
|
32
|
+
return { __flowCallback: id, captures };
|
|
33
|
+
}
|
|
34
|
+
/** A callback the build extracted, or one given as a function on hosts that run them in place. */
|
|
35
|
+
function callbackOf(value) {
|
|
36
|
+
if (isRecord(value) && typeof value.__flowCallback === "string") {
|
|
37
|
+
return { id: value.__flowCallback, captures: value.captures };
|
|
38
|
+
}
|
|
39
|
+
if (typeof value === "function" && typeof options.callbackId === "function") {
|
|
40
|
+
const fn = value;
|
|
41
|
+
return { id: options.callbackId(fn), fn };
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Run a callback, or a `jssg(...)` or `astGrep(...)` transform, on every
|
|
47
|
+
* selected file.
|
|
48
|
+
*/
|
|
49
|
+
function forEachFile(selector, callback) {
|
|
50
|
+
if (callback === undefined && (callbackOf(selector) || isTask(selector))) {
|
|
51
|
+
callback = selector;
|
|
52
|
+
selector = undefined;
|
|
53
|
+
}
|
|
54
|
+
if (isTask(callback))
|
|
55
|
+
return transformFiles(selector, callback);
|
|
56
|
+
const extracted = callbackOf(callback);
|
|
57
|
+
if (!extracted) {
|
|
58
|
+
throw new TypeError(typeof callback === "function"
|
|
59
|
+
? "forEachFile: the callback was not extracted by the build; run the workflow through the codemod-workflow build"
|
|
60
|
+
: "forEachFile takes a callback written inline as a function, jssg(...) or astGrep(...)");
|
|
61
|
+
}
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
[TASK]: "forEachFile",
|
|
64
|
+
selector: normalizeSelector(selector),
|
|
65
|
+
callback: extracted,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* `forEachFile(selector, jssg(...))`: the transform's task, on the files
|
|
70
|
+
* the selector picks. The selector's fields become the transform's.
|
|
71
|
+
*/
|
|
72
|
+
function transformFiles(selector, transform) {
|
|
73
|
+
const kind = transform[TASK] === "host" ? transform.content.kind : "";
|
|
74
|
+
if (transform[TASK] !== "host" || (kind !== "jssg" && kind !== "astGrep")) {
|
|
75
|
+
throw new TypeError("forEachFile takes a callback written inline as a function, jssg(...) or astGrep(...)");
|
|
76
|
+
}
|
|
77
|
+
const { include, exclude, language, containing, concurrency } = normalizeSelector(selector);
|
|
78
|
+
if (containing !== undefined || concurrency !== undefined) {
|
|
79
|
+
const field = containing !== undefined ? "containing" : "concurrency";
|
|
80
|
+
throw new TypeError(`forEachFile: selector.${field} is for callbacks, not for ${kind}(...)`);
|
|
81
|
+
}
|
|
82
|
+
if (language !== undefined && kind === "astGrep") {
|
|
83
|
+
throw new TypeError("forEachFile: the rules of astGrep(...) name their languages; select files with globs");
|
|
84
|
+
}
|
|
85
|
+
const selected = {};
|
|
86
|
+
if (include.length)
|
|
87
|
+
selected.include = include;
|
|
88
|
+
if (exclude.length)
|
|
89
|
+
selected.exclude = exclude;
|
|
90
|
+
if (language !== undefined)
|
|
91
|
+
selected.language = language;
|
|
92
|
+
for (const field of Object.keys(selected)) {
|
|
93
|
+
if (field in transform.content) {
|
|
94
|
+
throw new TypeError(`forEachFile: '${field}' is given twice, in the selector and in ${kind}(...)`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const content = { ...transform.content, ...selected };
|
|
98
|
+
// Without a language, the package's (codemod.yaml), as for callbacks.
|
|
99
|
+
if (kind === "jssg" && content.language === undefined)
|
|
100
|
+
content.packageLanguage = true;
|
|
101
|
+
return Object.freeze({ [TASK]: "host", content, reducer: transform.reducer });
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Read the target's lockfiles into one dependency graph and run `callback`
|
|
105
|
+
* with a live handle on it, in its own isolate. Only what the callback
|
|
106
|
+
* returns is recorded.
|
|
107
|
+
*/
|
|
108
|
+
function dependencyGraph(options, callback) {
|
|
109
|
+
const extracted = callbackOf(callback);
|
|
110
|
+
if (!extracted || extracted.fn) {
|
|
111
|
+
throw new TypeError("dependencyGraph: the callback was not extracted by the build; run the workflow through the codemod-workflow build");
|
|
112
|
+
}
|
|
113
|
+
const { lockfiles, ...rest } = isRecord(options) ? options : {};
|
|
114
|
+
const unknown = Object.keys(rest);
|
|
115
|
+
if (unknown.length)
|
|
116
|
+
throw new TypeError(`dependencyGraph: unknown option '${unknown[0]}'`);
|
|
117
|
+
return Object.freeze({
|
|
118
|
+
[TASK]: "dependencyGraph",
|
|
119
|
+
lockfiles: lockfiles === undefined ? undefined : globs(lockfiles, "lockfiles", "dependencyGraph: "),
|
|
120
|
+
callback: extracted,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function hostTask(kind, options) {
|
|
124
|
+
const content = withoutUndefined(options);
|
|
125
|
+
checkJson(content, kind);
|
|
126
|
+
return Object.freeze({
|
|
127
|
+
[TASK]: "host",
|
|
128
|
+
content: { ...content, kind },
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Run a shell command: `shell(command, options?)`, or as a tagged
|
|
133
|
+
* template, ``shell`grep -r ${pattern} src` ``, where each value is quoted
|
|
134
|
+
* for the shell. ``shell(options)`...` `` gives the template options.
|
|
135
|
+
*/
|
|
136
|
+
function shell(command, ...rest) {
|
|
137
|
+
if (isTemplateStrings(command))
|
|
138
|
+
return shellTemplate({}, command, rest);
|
|
139
|
+
if (typeof command === "string") {
|
|
140
|
+
return hostTask("shell", { ...rest[0], command });
|
|
141
|
+
}
|
|
142
|
+
if (isRecord(command) && rest.length === 0) {
|
|
143
|
+
const options = command;
|
|
144
|
+
return (strings, ...values) => {
|
|
145
|
+
if (!isTemplateStrings(strings)) {
|
|
146
|
+
throw new TypeError("shell(options) is used as a tag: shell(options)`command`");
|
|
147
|
+
}
|
|
148
|
+
return shellTemplate(options, strings, values);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
throw new TypeError("shell: expected a command, shell`command`, or shell(options)`command`");
|
|
152
|
+
}
|
|
153
|
+
function shellTemplate(options, strings, values) {
|
|
154
|
+
// The text as written, backslashes included: it is shell, not JavaScript.
|
|
155
|
+
let command = strings.raw[0] ?? "";
|
|
156
|
+
values.forEach((value, index) => {
|
|
157
|
+
command += shellQuote(value, index) + (strings.raw[index + 1] ?? "");
|
|
158
|
+
});
|
|
159
|
+
return hostTask("shell", { ...options, command });
|
|
160
|
+
}
|
|
161
|
+
/** Run a JSSG transform file over the target. */
|
|
162
|
+
function jssg(path, options = {}) {
|
|
163
|
+
if (!isRecord(options) || !("reduce" in options)) {
|
|
164
|
+
if (isRecord(options) && "initial" in options) {
|
|
165
|
+
throw new TypeError("jssg: initial is the starting value of reduce; give reduce too");
|
|
166
|
+
}
|
|
167
|
+
return hostTask("jssg", { ...options, path });
|
|
168
|
+
}
|
|
169
|
+
const { reduce, initial, ...rest } = options;
|
|
170
|
+
const callback = callbackOf(reduce);
|
|
171
|
+
if (!callback) {
|
|
172
|
+
throw new TypeError(typeof reduce === "function"
|
|
173
|
+
? "jssg: reduce was not extracted by the build; run the workflow through the codemod-workflow build"
|
|
174
|
+
: "jssg: reduce must be a function, written inline as reduce: (result, file) => ...");
|
|
175
|
+
}
|
|
176
|
+
if (initial === undefined) {
|
|
177
|
+
throw new TypeError("jssg: reduce needs an initial value, as { reduce, initial }");
|
|
178
|
+
}
|
|
179
|
+
checkJson(initial, "jssg: initial");
|
|
180
|
+
const task = hostTask("jssg", { ...rest, path });
|
|
181
|
+
return Object.freeze({ ...task, reducer: { callback, initial: initial } });
|
|
182
|
+
}
|
|
183
|
+
/** Apply an ast-grep YAML rule file. */
|
|
184
|
+
function astGrep(path, options = {}) {
|
|
185
|
+
return hostTask("astGrep", { ...options, path });
|
|
186
|
+
}
|
|
187
|
+
/** Split files into shards. */
|
|
188
|
+
function shard(options) {
|
|
189
|
+
return hostTask("shard", options);
|
|
190
|
+
}
|
|
191
|
+
/** Run another codemod package (registry name or local path). */
|
|
192
|
+
function codemod(source, options = {}) {
|
|
193
|
+
return hostTask("codemod", { ...options, source });
|
|
194
|
+
}
|
|
195
|
+
/** Hand a prompt to a coding agent. */
|
|
196
|
+
function agent(prompt, options = {}) {
|
|
197
|
+
return hostTask("agent", { ...options, prompt });
|
|
198
|
+
}
|
|
199
|
+
/** Install an agent skill package. */
|
|
200
|
+
function installSkill(options) {
|
|
201
|
+
return hostTask("installSkill", options);
|
|
202
|
+
}
|
|
203
|
+
/** Wait for a person to approve before continuing. */
|
|
204
|
+
function approval(message) {
|
|
205
|
+
return hostTask("approval", { message });
|
|
206
|
+
}
|
|
207
|
+
/** Wait for a time: milliseconds, or a string such as "30s" or "10m". */
|
|
208
|
+
function sleep(duration) {
|
|
209
|
+
return hostTask("sleep", { duration: milliseconds(duration, "sleep") });
|
|
210
|
+
}
|
|
211
|
+
/** Wait for an event sent to the run (`codemod workflow send`). */
|
|
212
|
+
function waitForEvent(name, options = {}) {
|
|
213
|
+
if (typeof name !== "string" || !/^[A-Za-z0-9._-]{1,100}$/.test(name)) {
|
|
214
|
+
throw new TypeError("waitForEvent: the event name must be 1 to 100 letters, digits, '.', '_' or '-'");
|
|
215
|
+
}
|
|
216
|
+
onlyKeys(options, ["timeout"], "waitForEvent");
|
|
217
|
+
return hostTask("waitForEvent", {
|
|
218
|
+
name,
|
|
219
|
+
timeout: options.timeout === undefined
|
|
220
|
+
? undefined
|
|
221
|
+
: milliseconds(options.timeout, "waitForEvent: timeout"),
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/** Check out a branch for the following changes (managed git only). */
|
|
225
|
+
function branch(name) {
|
|
226
|
+
return hostTask("branch", { name });
|
|
227
|
+
}
|
|
228
|
+
/** Commit changes (managed git only). */
|
|
229
|
+
function commit(message, options = {}) {
|
|
230
|
+
return hostTask("commit", { ...options, message });
|
|
231
|
+
}
|
|
232
|
+
/** Push the branch and open a pull request (managed git only). */
|
|
233
|
+
function pullRequest(options) {
|
|
234
|
+
return hostTask("pullRequest", options);
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
callbackOf,
|
|
238
|
+
__callback,
|
|
239
|
+
forEachFile,
|
|
240
|
+
dependencyGraph,
|
|
241
|
+
shell,
|
|
242
|
+
jssg,
|
|
243
|
+
astGrep,
|
|
244
|
+
shard,
|
|
245
|
+
codemod,
|
|
246
|
+
agent,
|
|
247
|
+
installSkill,
|
|
248
|
+
approval,
|
|
249
|
+
sleep,
|
|
250
|
+
waitForEvent,
|
|
251
|
+
branch,
|
|
252
|
+
commit,
|
|
253
|
+
pullRequest,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** Tasks that wait: their step records when the wait began. */
|
|
257
|
+
const WAITS = new Set(["sleep", "waitForEvent"]);
|
|
258
|
+
function isRecord(value) {
|
|
259
|
+
return value !== null && typeof value === "object";
|
|
260
|
+
}
|
|
261
|
+
function onlyKeys(options, known, what) {
|
|
262
|
+
if (!isRecord(options) || Array.isArray(options)) {
|
|
263
|
+
throw new TypeError(`${what}: options must be an object`);
|
|
264
|
+
}
|
|
265
|
+
for (const key of Object.keys(options)) {
|
|
266
|
+
if (!known.includes(key)) {
|
|
267
|
+
throw new TypeError(`${what}: unknown option '${key}'; expected ${known.join(", ")}`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const UNITS = { ms: 1, s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 };
|
|
272
|
+
/** A duration in milliseconds, from a number of them or a string such as "30s". */
|
|
273
|
+
export function milliseconds(value, what) {
|
|
274
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0)
|
|
275
|
+
return Math.round(value);
|
|
276
|
+
const match = typeof value === "string" ? /^(\d+(?:\.\d+)?)\s*(ms|s|m|h|d)$/.exec(value.trim()) : null;
|
|
277
|
+
if (!match) {
|
|
278
|
+
throw new TypeError(`${what}: expected milliseconds or a duration such as "500ms", "30s", "10m", "1h" or "2d"`);
|
|
279
|
+
}
|
|
280
|
+
return Math.round(Number(match[1]) * UNITS[match[2]]);
|
|
281
|
+
}
|
|
282
|
+
/** A duration in its largest whole unit, as the engine words it. */
|
|
283
|
+
export function describeDuration(ms) {
|
|
284
|
+
for (const unit of ["h", "m", "s"]) {
|
|
285
|
+
if (ms >= UNITS[unit] && ms % UNITS[unit] === 0)
|
|
286
|
+
return `${ms / UNITS[unit]}${unit}`;
|
|
287
|
+
}
|
|
288
|
+
return `${ms}ms`;
|
|
289
|
+
}
|
|
290
|
+
/** What `step(..., options)` asks for. */
|
|
291
|
+
function policyOf(options, what) {
|
|
292
|
+
if (options === undefined)
|
|
293
|
+
return { attempts: 1, retryDelay: 0, timeout: undefined };
|
|
294
|
+
if (isRecord(options) && "state" in options) {
|
|
295
|
+
throw new TypeError(`${what}: steps take no \`state\`: give values to the task itself (a function step's variables, jssg \`params\`, shell \`env\`) and read what it found from its result`);
|
|
296
|
+
}
|
|
297
|
+
onlyKeys(options, ["retries", "retryDelay", "timeout"], what);
|
|
298
|
+
const { retries = 0, retryDelay = "1s", timeout } = options;
|
|
299
|
+
if (!Number.isInteger(retries) || retries < 0) {
|
|
300
|
+
throw new TypeError(`${what}: retries must be a whole number, 0 or more`);
|
|
301
|
+
}
|
|
302
|
+
const limit = timeout === undefined ? undefined : milliseconds(timeout, `${what}: timeout`);
|
|
303
|
+
if (limit === 0)
|
|
304
|
+
throw new TypeError(`${what}: timeout must be more than 0`);
|
|
305
|
+
return {
|
|
306
|
+
attempts: retries + 1,
|
|
307
|
+
retryDelay: milliseconds(retryDelay, `${what}: retryDelay`),
|
|
308
|
+
timeout: limit,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function isTemplateStrings(value) {
|
|
312
|
+
return Array.isArray(value) && Array.isArray(value.raw);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* A value in a `shell` template, quoted for the shell: one word whatever it
|
|
316
|
+
* holds. An array is one word per item.
|
|
317
|
+
*/
|
|
318
|
+
function shellQuote(value, index) {
|
|
319
|
+
if (Array.isArray(value))
|
|
320
|
+
return value.map((item) => shellQuote(item, index)).join(" ");
|
|
321
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") {
|
|
322
|
+
throw new TypeError(`shell: value ${index + 1} in the command is ${value === null ? "null" : typeof value}; give a string, a number, a boolean, or an array of them`);
|
|
323
|
+
}
|
|
324
|
+
const text = String(value);
|
|
325
|
+
if (/^[\w@%+=:,./-]+$/.test(text))
|
|
326
|
+
return text;
|
|
327
|
+
return `'${text.replaceAll("'", `'\\''`)}'`;
|
|
177
328
|
}
|
|
178
|
-
|
|
179
329
|
export function createRuntime(host) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
recorded
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (value === null) state.delete(key);
|
|
259
|
-
else state.set(key, freeze(value));
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function stateSnapshot() {
|
|
264
|
-
return Object.fromEntries(state);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// The body runs alone, so a lock there has nothing to wait for; steps and
|
|
268
|
-
// callbacks get the real one from their own runtime.
|
|
269
|
-
function acquireLock(_name) {
|
|
270
|
-
return () => {};
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Step outputs are kept for the transforms that still set them; nothing in
|
|
274
|
-
// a workflow reads them back but these functions.
|
|
275
|
-
const stepOutputs = new Map();
|
|
276
|
-
const outputKey = (stepId, name) => JSON.stringify([stepId, name]);
|
|
277
|
-
|
|
278
|
-
function setStepOutput(name, value) {
|
|
279
|
-
stepOutputs.set(outputKey("", name), String(value));
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function getStepOutput(stepId, name) {
|
|
283
|
-
return stepOutputs.get(outputKey(stepId, name)) ?? null;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
function getOrSetStepOutput(stepId, name, value) {
|
|
287
|
-
const key = outputKey(stepId, name);
|
|
288
|
-
if (!stepOutputs.has(key)) stepOutputs.set(key, String(value));
|
|
289
|
-
return stepOutputs.get(key);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/** Evaluate a workflow.yaml `${{ }}` expression; used by translated workflows. */
|
|
293
|
-
function __evaluate(kind, expression, scope) {
|
|
294
|
-
return host.evaluate(kind, expression, scope);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// ----------------------------------------------------------------- step ---
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Run a task as a durable step. Starts immediately; the returned promise
|
|
301
|
-
* resolves to the step's frozen result.
|
|
302
|
-
*/
|
|
303
|
-
function step(labelOrTask, maybeTask) {
|
|
304
|
-
const position = nextPosition++;
|
|
305
|
-
const [label, task] =
|
|
306
|
-
typeof labelOrTask === "string" ? [labelOrTask, maybeTask] : [undefined, labelOrTask];
|
|
307
|
-
return runStep(position, label, task).then(freeze);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
async function runStep(position, label, task) {
|
|
311
|
-
// Everything up to the first await runs synchronously inside step(), so
|
|
312
|
-
// captures and state are a snapshot taken when the step is called.
|
|
313
|
-
let content;
|
|
314
|
-
let fn;
|
|
315
|
-
const callback = callbackOf(task);
|
|
316
|
-
if (callback) {
|
|
317
|
-
content = { kind: "function", callback: callback.id, captures: capturesOf(callback) };
|
|
318
|
-
fn = callback.fn;
|
|
319
|
-
} else if (typeof task === "function") {
|
|
320
|
-
throw new TypeError(
|
|
321
|
-
`step #${position}: write the function inline, as step(() => ...), so the build can run it in its own isolate`,
|
|
322
|
-
);
|
|
323
|
-
} else if (task && task[TASK] === "forEachFile") {
|
|
324
|
-
content = {
|
|
325
|
-
kind: "forEachFile",
|
|
326
|
-
selector: task.selector,
|
|
327
|
-
callback: task.callback.id,
|
|
328
|
-
captures: capturesOf(task.callback),
|
|
329
|
-
};
|
|
330
|
-
fn = task.callback.fn;
|
|
331
|
-
} else if (task && task[TASK] === "host") {
|
|
332
|
-
content = task.content;
|
|
333
|
-
} else {
|
|
334
|
-
throw new TypeError(
|
|
335
|
-
`step #${position}: expected a task such as forEachFile(...) or a function`,
|
|
336
|
-
);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
const name = label ?? defaultLabel(position, content);
|
|
340
|
-
const key = canonical(content);
|
|
341
|
-
if (closed) return new Promise(() => {});
|
|
342
|
-
if (plan.until !== undefined && name === plan.until) {
|
|
343
|
-
closed = true;
|
|
344
|
-
host.stop();
|
|
345
|
-
}
|
|
346
|
-
if (given.has(name)) {
|
|
347
|
-
const result = given.get(name);
|
|
348
|
-
host.event({ type: "stepReplayed", position, label: name });
|
|
349
|
-
host.record(
|
|
350
|
-
{ position, label: name, key, status: "succeeded", result, given: true, seq: nextSeq++ },
|
|
351
|
-
0,
|
|
352
|
-
);
|
|
353
|
-
return JSON.parse(JSON.stringify(result ?? null));
|
|
354
|
-
}
|
|
355
|
-
const prior = recorded.get(position);
|
|
356
|
-
if (prior && prior.status === "succeeded") {
|
|
357
|
-
if (prior.key !== key) {
|
|
358
|
-
throw new NondeterminismError(
|
|
359
|
-
`step #${position} ('${name}') differs from the recorded run; the workflow changed before this step, so its recorded result cannot be reused`,
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
return new Promise((resolve) => {
|
|
363
|
-
replaying.push({
|
|
364
|
-
// Steps recorded before finishing order was kept replay in the
|
|
365
|
-
// order they were issued.
|
|
366
|
-
order: prior.seq ?? prior.position,
|
|
367
|
-
settle() {
|
|
368
|
-
applyState(prior.state);
|
|
369
|
-
settleClock(prior);
|
|
370
|
-
host.event({ type: "stepReplayed", position, label: name });
|
|
371
|
-
resolve(prior.result);
|
|
372
|
-
},
|
|
373
|
-
});
|
|
374
|
-
host.wake?.();
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
if (host.canceled()) {
|
|
379
|
-
closed = true;
|
|
380
|
-
throw new CanceledError();
|
|
381
|
-
}
|
|
382
|
-
host.event({ type: "stepStarted", position, label: name });
|
|
383
|
-
const started = host.now();
|
|
384
|
-
const record = (entry) =>
|
|
385
|
-
host.record({ position, label: name, key, seq: nextSeq++, ...entry }, host.now() - started);
|
|
386
|
-
let result;
|
|
387
|
-
let changes = null;
|
|
388
|
-
try {
|
|
389
|
-
const outcome = await host.start({
|
|
390
|
-
position,
|
|
391
|
-
label: name,
|
|
392
|
-
task: content,
|
|
393
|
-
state: stateSnapshot(),
|
|
394
|
-
fn,
|
|
395
|
-
});
|
|
396
|
-
result = outcome.result ?? null;
|
|
397
|
-
changes = outcome.state ?? null;
|
|
398
|
-
} catch (cause) {
|
|
399
|
-
const error = asError(cause);
|
|
400
|
-
// A step that was canceled or waits for an approval runs again when
|
|
401
|
-
// the run is resumed; nothing else starts in this execution.
|
|
402
|
-
const interrupted =
|
|
403
|
-
error instanceof CanceledError
|
|
404
|
-
? "canceled"
|
|
405
|
-
: error instanceof AwaitingApprovalError
|
|
406
|
-
? "awaiting-approval"
|
|
407
|
-
: null;
|
|
408
|
-
if (interrupted) {
|
|
330
|
+
const tasks = createTasks(host);
|
|
331
|
+
const { callbackOf } = tasks;
|
|
332
|
+
// What earlier executions of the run recorded. A step is the one recorded
|
|
333
|
+
// when it has the same label and content (its key), wherever and whenever
|
|
334
|
+
// the body issues it: the order steps are issued in depends on how fast
|
|
335
|
+
// other steps finish, so it cannot say which step is which. Steps that are
|
|
336
|
+
// the same in both are interchangeable and take the records in turn.
|
|
337
|
+
const recorded = new Map();
|
|
338
|
+
// Recorded steps that did not finish, for a changed step to take over.
|
|
339
|
+
const unfinished = new Set();
|
|
340
|
+
// A step keeps its position for the whole run; new steps go after.
|
|
341
|
+
let nextPosition = 0;
|
|
342
|
+
// Steps are numbered in the order they finish, across executions of the run.
|
|
343
|
+
let nextSeq = 0;
|
|
344
|
+
for (const entry of [...host.history()].sort((a, b) => a.position - b.position)) {
|
|
345
|
+
const same = recorded.get(entry.key) ?? [];
|
|
346
|
+
same.push(entry);
|
|
347
|
+
recorded.set(entry.key, same);
|
|
348
|
+
if (!finished(entry))
|
|
349
|
+
unfinished.add(entry);
|
|
350
|
+
nextPosition = Math.max(nextPosition, entry.position + 1);
|
|
351
|
+
if (typeof entry.seq === "number")
|
|
352
|
+
nextSeq = Math.max(nextSeq, entry.seq + 1);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* The recorded step this one is, or the position of an unfinished step it
|
|
356
|
+
* replaces: a step with a recorded name but new content is the failed step,
|
|
357
|
+
* fixed. A finished step is never replaced; its record stays for a step
|
|
358
|
+
* with its content.
|
|
359
|
+
*/
|
|
360
|
+
function claim(label, key, content) {
|
|
361
|
+
const entry = recorded.get(key)?.shift();
|
|
362
|
+
if (entry) {
|
|
363
|
+
unfinished.delete(entry);
|
|
364
|
+
return { position: entry.position, entry };
|
|
365
|
+
}
|
|
366
|
+
for (const entry of unfinished) {
|
|
367
|
+
if (entry.label !== (label ?? defaultLabel(entry.position, content)))
|
|
368
|
+
continue;
|
|
369
|
+
unfinished.delete(entry);
|
|
370
|
+
const others = recorded.get(entry.key);
|
|
371
|
+
others?.splice(others.indexOf(entry), 1);
|
|
372
|
+
return { position: entry.position };
|
|
373
|
+
}
|
|
374
|
+
return { position: nextPosition++ };
|
|
375
|
+
}
|
|
376
|
+
// A test run can stop after one step (`until`) and skip steps with a given
|
|
377
|
+
// result (`given`), both matched by label.
|
|
378
|
+
const plan = host.testPlan() ?? {};
|
|
379
|
+
const given = new Map(Object.entries(plan.given ?? {}));
|
|
380
|
+
// Once closed, no step starts: steps issued from then on never settle. A
|
|
381
|
+
// run closes when a test's `until` step starts, when a step is canceled or
|
|
382
|
+
// waits for an approval, and when the host ends the run.
|
|
383
|
+
let closed = false;
|
|
384
|
+
// Recorded steps the body has issued again, waiting to settle. They settle
|
|
385
|
+
// one at a time in the order they finished when they ran, so the body sees
|
|
386
|
+
// results, the clock and promise races as it did then.
|
|
387
|
+
const replaying = [];
|
|
388
|
+
/**
|
|
389
|
+
* Settle the next replayed step. The host calls it whenever the workflow
|
|
390
|
+
* has nothing left to run, until it returns false.
|
|
391
|
+
*/
|
|
392
|
+
function replayNext() {
|
|
393
|
+
if (closed)
|
|
394
|
+
return false;
|
|
395
|
+
let next;
|
|
396
|
+
for (const replay of replaying) {
|
|
397
|
+
if (!next || replay.order < next.order)
|
|
398
|
+
next = replay;
|
|
399
|
+
}
|
|
400
|
+
if (!next)
|
|
401
|
+
return false;
|
|
402
|
+
replaying.splice(replaying.indexOf(next), 1);
|
|
403
|
+
next.settle();
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
/** The run is ending: no step starts from here on. */
|
|
407
|
+
function close() {
|
|
409
408
|
closed = true;
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
return
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
409
|
+
}
|
|
410
|
+
/** Whether steps issued now would never start. */
|
|
411
|
+
function isClosed() {
|
|
412
|
+
return closed;
|
|
413
|
+
}
|
|
414
|
+
// The body's clock only moves when a step finishes, to the time recorded
|
|
415
|
+
// for it, so Date.now() in the body gives the same answer on every replay.
|
|
416
|
+
function settleClock(entry) {
|
|
417
|
+
if (typeof entry.finishedAt === "number")
|
|
418
|
+
host.advanceClock(entry.finishedAt);
|
|
419
|
+
}
|
|
420
|
+
/** Evaluate a workflow.yaml `${{ }}` expression; used by translated workflows. */
|
|
421
|
+
function __evaluate(kind, expression, scope) {
|
|
422
|
+
return host.evaluate(kind, expression, scope);
|
|
423
|
+
}
|
|
424
|
+
// ----------------------------------------------------------------- step ---
|
|
425
|
+
/**
|
|
426
|
+
* Run a task as a durable step: `step(label?, task, options?)`. Starts
|
|
427
|
+
* immediately; the returned promise resolves to the step's frozen result.
|
|
428
|
+
* `options` may give `retries`, `retryDelay` and `timeout`; they say how to
|
|
429
|
+
* run the step, not what it is, so changing them does not make it another
|
|
430
|
+
* step.
|
|
431
|
+
*/
|
|
432
|
+
function step(...args) {
|
|
433
|
+
const label = typeof args[0] === "string" ? args.shift() : undefined;
|
|
434
|
+
return runStep(label, args[0], args[1]).then(freeze);
|
|
435
|
+
}
|
|
436
|
+
/** Wait for a time, as a step: a resumed run waits for what is left. */
|
|
437
|
+
function sleep(duration) {
|
|
438
|
+
return step(tasks.sleep(duration));
|
|
439
|
+
}
|
|
440
|
+
/** Wait for an event sent to the run, as a step; resolves to the event's data. */
|
|
441
|
+
function waitForEvent(name, options) {
|
|
442
|
+
return step(tasks.waitForEvent(name, options));
|
|
443
|
+
}
|
|
444
|
+
async function runStep(label, task, options) {
|
|
445
|
+
const described = label === undefined ? "step" : `step '${label}'`;
|
|
446
|
+
const policy = policyOf(options, described);
|
|
447
|
+
// Everything up to the first await runs synchronously inside step(), so
|
|
448
|
+
// captures are a snapshot taken when the step is called.
|
|
449
|
+
let content;
|
|
450
|
+
let fn;
|
|
451
|
+
const callback = callbackOf(task);
|
|
452
|
+
if (callback) {
|
|
453
|
+
content = { kind: "function", callback: callback.id, captures: capturesOf(callback) };
|
|
454
|
+
fn = callback.fn;
|
|
455
|
+
}
|
|
456
|
+
else if (typeof task === "function") {
|
|
457
|
+
throw new TypeError(`${described}: write the function inline, as step(() => ...), so the build can run it in its own isolate`);
|
|
458
|
+
}
|
|
459
|
+
else if (isTask(task) && task[TASK] === "forEachFile") {
|
|
460
|
+
content = {
|
|
461
|
+
kind: "forEachFile",
|
|
462
|
+
selector: task.selector,
|
|
463
|
+
callback: task.callback.id,
|
|
464
|
+
captures: capturesOf(task.callback),
|
|
465
|
+
};
|
|
466
|
+
fn = task.callback.fn;
|
|
467
|
+
}
|
|
468
|
+
else if (isTask(task) && task[TASK] === "dependencyGraph") {
|
|
469
|
+
content = withoutUndefined({
|
|
470
|
+
kind: "dependencyGraph",
|
|
471
|
+
lockfiles: task.lockfiles,
|
|
472
|
+
callback: task.callback.id,
|
|
473
|
+
captures: capturesOf(task.callback),
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
else if (isTask(task) && task[TASK] === "host") {
|
|
477
|
+
content = task.content;
|
|
478
|
+
if (task.reducer) {
|
|
479
|
+
const { callback, initial } = task.reducer;
|
|
480
|
+
content = {
|
|
481
|
+
...content,
|
|
482
|
+
reduce: { callback: callback.id, captures: capturesOf(callback), initial },
|
|
483
|
+
};
|
|
484
|
+
fn = callback.fn;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
throw new TypeError(`${described}: expected a task such as forEachFile(...) or a function`);
|
|
489
|
+
}
|
|
490
|
+
const key = canonical([label ?? null, content]);
|
|
491
|
+
if (closed)
|
|
492
|
+
return new Promise(() => { });
|
|
493
|
+
const { position, entry: prior } = claim(label, key, content);
|
|
494
|
+
const name = label ?? defaultLabel(position, content);
|
|
495
|
+
if (plan.until !== undefined && name === plan.until) {
|
|
496
|
+
closed = true;
|
|
497
|
+
host.stop();
|
|
498
|
+
}
|
|
499
|
+
if (given.has(name)) {
|
|
500
|
+
const result = given.get(name) ?? null;
|
|
501
|
+
host.event({ type: "stepReplayed", position, label: name });
|
|
502
|
+
host.record({ position, label: name, key, status: "succeeded", result, given: true, seq: nextSeq++ }, 0);
|
|
503
|
+
return JSON.parse(JSON.stringify(result));
|
|
504
|
+
}
|
|
505
|
+
if (prior && finished(prior)) {
|
|
506
|
+
return new Promise((resolve) => {
|
|
507
|
+
replaying.push({
|
|
508
|
+
// Steps recorded before finishing order was kept replay in the
|
|
509
|
+
// order they were issued.
|
|
510
|
+
order: prior.seq ?? prior.position,
|
|
511
|
+
settle() {
|
|
512
|
+
settleClock(prior);
|
|
513
|
+
host.event({ type: "stepReplayed", position, label: name });
|
|
514
|
+
resolve(prior.result ?? null);
|
|
515
|
+
},
|
|
516
|
+
});
|
|
517
|
+
host.wake?.();
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
if (host.canceled()) {
|
|
521
|
+
closed = true;
|
|
522
|
+
throw new CanceledError();
|
|
523
|
+
}
|
|
524
|
+
host.event({ type: "stepStarted", position, label: name });
|
|
525
|
+
const started = host.now();
|
|
526
|
+
// A wait counts from when it first began, also when it was interrupted
|
|
527
|
+
// and goes on now, so that is recorded before it starts. One that failed
|
|
528
|
+
// ran out of time, and starts over.
|
|
529
|
+
const since = !WAITS.has(content.kind)
|
|
530
|
+
? undefined
|
|
531
|
+
: prior && prior.status !== "failed" && typeof prior.since === "number"
|
|
532
|
+
? prior.since
|
|
533
|
+
: started;
|
|
534
|
+
if (since !== undefined) {
|
|
535
|
+
host.record({ position, label: name, key, status: "waiting", since }, 0);
|
|
536
|
+
}
|
|
537
|
+
let attempt = 1;
|
|
538
|
+
const record = (entry) => host.record({
|
|
539
|
+
position,
|
|
540
|
+
label: name,
|
|
541
|
+
key,
|
|
542
|
+
seq: nextSeq++,
|
|
543
|
+
...(since !== undefined && { since }),
|
|
544
|
+
...(attempt > 1 && { attempts: attempt }),
|
|
545
|
+
...entry,
|
|
546
|
+
}, host.now() - started);
|
|
547
|
+
const request = {
|
|
548
|
+
position,
|
|
549
|
+
label: name,
|
|
550
|
+
task: content,
|
|
551
|
+
fn,
|
|
552
|
+
...(policy.timeout !== undefined && { timeoutMs: policy.timeout }),
|
|
553
|
+
...(since !== undefined && { since }),
|
|
554
|
+
};
|
|
555
|
+
let outcome;
|
|
556
|
+
for (;;) {
|
|
557
|
+
let error;
|
|
558
|
+
try {
|
|
559
|
+
outcome = await host.start(request);
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
catch (cause) {
|
|
563
|
+
error = asError(cause);
|
|
564
|
+
}
|
|
565
|
+
// A failed step runs again while it has attempts left and the run is
|
|
566
|
+
// still going.
|
|
567
|
+
if (!interruptionOf(error) && attempt < policy.attempts && !closed) {
|
|
568
|
+
host.event({
|
|
569
|
+
type: "stepRetrying",
|
|
570
|
+
position,
|
|
571
|
+
label: name,
|
|
572
|
+
attempt,
|
|
573
|
+
attempts: policy.attempts,
|
|
574
|
+
error: messageOf(error),
|
|
575
|
+
});
|
|
576
|
+
try {
|
|
577
|
+
if (policy.retryDelay > 0) {
|
|
578
|
+
await host.start({
|
|
579
|
+
position,
|
|
580
|
+
label: name,
|
|
581
|
+
task: { kind: "sleep", duration: policy.retryDelay },
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
attempt++;
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
catch (cause) {
|
|
588
|
+
// Canceled between attempts.
|
|
589
|
+
error = asError(cause);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
// A step that was canceled or waits for an approval runs again when
|
|
593
|
+
// the run is resumed; nothing else starts in this execution.
|
|
594
|
+
const interrupted = interruptionOf(error);
|
|
595
|
+
if (interrupted) {
|
|
596
|
+
closed = true;
|
|
597
|
+
record({ status: interrupted, error: messageOf(error) });
|
|
598
|
+
throw error;
|
|
599
|
+
}
|
|
600
|
+
record({ status: "failed", error: messageOf(error) });
|
|
601
|
+
throw new StepError(name, error);
|
|
602
|
+
}
|
|
603
|
+
const result = outcome.result ?? null;
|
|
604
|
+
const finishedAt = host.now();
|
|
605
|
+
// Some files failed: the step finished, and the rest of the run goes on.
|
|
606
|
+
if (outcome.partial !== undefined) {
|
|
607
|
+
record({ status: "partial", result, finishedAt, error: outcome.partial });
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
record({ status: "succeeded", result, finishedAt });
|
|
611
|
+
}
|
|
612
|
+
settleClock({ finishedAt });
|
|
613
|
+
return result;
|
|
614
|
+
}
|
|
615
|
+
/** Whether a recorded step finished, so a resume reuses its result. */
|
|
616
|
+
function finished(entry) {
|
|
617
|
+
return entry.status === "succeeded" || entry.status === "partial";
|
|
618
|
+
}
|
|
619
|
+
function interruptionOf(error) {
|
|
620
|
+
if (error instanceof CanceledError)
|
|
621
|
+
return "canceled";
|
|
622
|
+
if (error instanceof AwaitingApprovalError)
|
|
623
|
+
return "awaiting-approval";
|
|
624
|
+
return null;
|
|
625
|
+
}
|
|
626
|
+
/** Captured values of an extracted callback; none for callbacks run in place. */
|
|
627
|
+
function capturesOf(callback) {
|
|
628
|
+
if (typeof callback.captures !== "function")
|
|
629
|
+
return undefined;
|
|
630
|
+
const captures = callback.captures();
|
|
631
|
+
checkJson(captures, "captures");
|
|
632
|
+
return captures;
|
|
633
|
+
}
|
|
634
|
+
/** Turn a host rejection (`{ name, message }`) back into the runtime's errors. */
|
|
635
|
+
function asError(cause) {
|
|
636
|
+
if (cause instanceof Error) {
|
|
637
|
+
if (cause.name === "CanceledError" && !(cause instanceof CanceledError)) {
|
|
638
|
+
return new CanceledError(cause.message);
|
|
639
|
+
}
|
|
640
|
+
if (cause.name === "AwaitingApprovalError" && !(cause instanceof AwaitingApprovalError)) {
|
|
641
|
+
return new AwaitingApprovalError(cause.message);
|
|
642
|
+
}
|
|
643
|
+
return cause;
|
|
644
|
+
}
|
|
645
|
+
if (isRecord(cause) && typeof cause.message === "string") {
|
|
646
|
+
if (cause.name === "CanceledError")
|
|
647
|
+
return new CanceledError(cause.message);
|
|
648
|
+
if (cause.name === "AwaitingApprovalError")
|
|
649
|
+
return new AwaitingApprovalError(cause.message);
|
|
650
|
+
return new Error(cause.message);
|
|
651
|
+
}
|
|
652
|
+
return new Error(String(cause));
|
|
653
|
+
}
|
|
654
|
+
const { callbackOf: _, ...builders } = tasks;
|
|
655
|
+
return {
|
|
656
|
+
...builders,
|
|
657
|
+
__evaluate,
|
|
658
|
+
step,
|
|
659
|
+
sleep,
|
|
660
|
+
waitForEvent,
|
|
661
|
+
replayNext,
|
|
662
|
+
close,
|
|
663
|
+
isClosed,
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
function isTask(value) {
|
|
667
|
+
return isRecord(value) && TASK in value;
|
|
466
668
|
}
|
|
467
|
-
|
|
468
669
|
function withoutUndefined(value) {
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
670
|
+
if (Array.isArray(value))
|
|
671
|
+
return value.map(withoutUndefined);
|
|
672
|
+
if (isRecord(value) && Object.getPrototypeOf(value) === Object.prototype) {
|
|
673
|
+
const result = {};
|
|
674
|
+
for (const [key, item] of Object.entries(value)) {
|
|
675
|
+
if (item !== undefined)
|
|
676
|
+
result[key] = withoutUndefined(item);
|
|
677
|
+
}
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
return value;
|
|
478
681
|
}
|
|
479
|
-
|
|
480
682
|
function normalizeSelector(selector) {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
683
|
+
if (selector === undefined)
|
|
684
|
+
selector = {};
|
|
685
|
+
if (typeof selector === "string" || Array.isArray(selector))
|
|
686
|
+
selector = { include: selector };
|
|
687
|
+
if (!isRecord(selector)) {
|
|
688
|
+
throw new TypeError("forEachFile: the selector must be a glob, an array of globs, or an object");
|
|
689
|
+
}
|
|
690
|
+
const known = new Set(["include", "exclude", "language", "containing", "concurrency"]);
|
|
691
|
+
for (const key of Object.keys(selector)) {
|
|
692
|
+
if (!known.has(key))
|
|
693
|
+
throw new TypeError(`forEachFile: unknown selector field '${key}'`);
|
|
694
|
+
}
|
|
695
|
+
const normalized = {
|
|
696
|
+
include: globs(selector.include, "include"),
|
|
697
|
+
exclude: globs(selector.exclude, "exclude"),
|
|
698
|
+
};
|
|
699
|
+
for (const field of ["language", "containing"]) {
|
|
700
|
+
const value = selector[field];
|
|
701
|
+
if (value === undefined)
|
|
702
|
+
continue;
|
|
703
|
+
if (typeof value !== "string") {
|
|
704
|
+
throw new TypeError(`forEachFile: selector.${field} must be a string`);
|
|
705
|
+
}
|
|
706
|
+
normalized[field] = value;
|
|
707
|
+
}
|
|
708
|
+
const { concurrency } = selector;
|
|
709
|
+
if (concurrency !== undefined) {
|
|
710
|
+
if (typeof concurrency !== "number" || !Number.isInteger(concurrency) || concurrency < 1) {
|
|
711
|
+
throw new TypeError("forEachFile: concurrency must be a whole number of at least 1");
|
|
712
|
+
}
|
|
713
|
+
normalized.concurrency = concurrency;
|
|
714
|
+
}
|
|
715
|
+
// Neither include nor language: the package's languages from codemod.yaml.
|
|
716
|
+
return normalized;
|
|
511
717
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
718
|
+
function globs(value, field, owner = "forEachFile: selector.") {
|
|
719
|
+
if (value === undefined)
|
|
720
|
+
return [];
|
|
721
|
+
if (typeof value === "string")
|
|
722
|
+
return [value];
|
|
723
|
+
if (Array.isArray(value) && value.every((item) => typeof item === "string"))
|
|
724
|
+
return [...value];
|
|
725
|
+
throw new TypeError(`${owner}${field} must be a glob or an array of globs`);
|
|
518
726
|
}
|
|
519
|
-
|
|
520
727
|
function defaultLabel(position, content) {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
728
|
+
switch (content.kind) {
|
|
729
|
+
case "forEachFile": {
|
|
730
|
+
const { include, language } = content.selector;
|
|
731
|
+
return `forEachFile ${include.length ? include.join(", ") : (language ?? "")}`.trimEnd();
|
|
732
|
+
}
|
|
733
|
+
case "function":
|
|
734
|
+
return `step #${position}`;
|
|
735
|
+
case "shell":
|
|
736
|
+
return `shell ${truncate(content.command)}`;
|
|
737
|
+
case "jssg":
|
|
738
|
+
case "astGrep":
|
|
739
|
+
return `${content.kind} ${String(content.path)}`;
|
|
740
|
+
case "approval":
|
|
741
|
+
return `approval: ${truncate(content.message)}`;
|
|
742
|
+
case "sleep":
|
|
743
|
+
return `sleep ${describeDuration(Number(content.duration))}`;
|
|
744
|
+
case "waitForEvent":
|
|
745
|
+
return `wait for ${String(content.name)}`;
|
|
746
|
+
case "codemod":
|
|
747
|
+
return `codemod ${String(content.source)}`;
|
|
748
|
+
case "dependencyGraph": {
|
|
749
|
+
const lockfiles = content.lockfiles;
|
|
750
|
+
return lockfiles ? `dependencyGraph ${lockfiles.join(", ")}` : "dependencyGraph";
|
|
751
|
+
}
|
|
752
|
+
case "agent":
|
|
753
|
+
return `agent: ${truncate(content.prompt)}`;
|
|
754
|
+
default:
|
|
755
|
+
return content.kind;
|
|
756
|
+
}
|
|
542
757
|
}
|
|
543
|
-
|
|
544
758
|
function truncate(text, length = 60) {
|
|
545
|
-
|
|
546
|
-
|
|
759
|
+
const line = String(text ?? "").split("\n")[0] ?? "";
|
|
760
|
+
return line.length > length ? `${line.slice(0, length - 1)}…` : line;
|
|
547
761
|
}
|
|
548
|
-
|
|
549
762
|
function canonical(value) {
|
|
550
|
-
|
|
763
|
+
return JSON.stringify(sortKeys(value));
|
|
551
764
|
}
|
|
552
|
-
|
|
553
765
|
function sortKeys(value) {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
766
|
+
if (Array.isArray(value))
|
|
767
|
+
return value.map(sortKeys);
|
|
768
|
+
if (isRecord(value)) {
|
|
769
|
+
const sorted = {};
|
|
770
|
+
for (const key of Object.keys(value).sort())
|
|
771
|
+
sorted[key] = sortKeys(value[key]);
|
|
772
|
+
return sorted;
|
|
773
|
+
}
|
|
774
|
+
return value;
|
|
561
775
|
}
|
|
562
|
-
|
|
563
776
|
/** Freeze a JSON value, all the way down. */
|
|
564
777
|
export function freeze(value) {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
778
|
+
if (isRecord(value) && !Object.isFrozen(value)) {
|
|
779
|
+
Object.freeze(value);
|
|
780
|
+
for (const key of Object.keys(value))
|
|
781
|
+
freeze(value[key]);
|
|
782
|
+
}
|
|
783
|
+
return value;
|
|
570
784
|
}
|
|
571
|
-
|
|
572
785
|
/**
|
|
573
786
|
* An error as a step's failure says it. Changing a captured value or a step
|
|
574
787
|
* result fails as a TypeError that does not say why; the hint does.
|
|
575
788
|
*/
|
|
576
789
|
export function describeError(error) {
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
error instanceof TypeError &&
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
return message;
|
|
790
|
+
if (!isRecord(error))
|
|
791
|
+
return String(error);
|
|
792
|
+
let message = `${String(error.name || "Error")}: ${String(error.message)}`;
|
|
793
|
+
if (error instanceof TypeError &&
|
|
794
|
+
/extensible|read-only|read only|frozen|non-writable/i.test(error.message)) {
|
|
795
|
+
message += " (captured values and step results are frozen; copy one before changing it)";
|
|
796
|
+
}
|
|
797
|
+
return message;
|
|
586
798
|
}
|
|
587
|
-
|
|
588
799
|
/**
|
|
589
|
-
* The `file` a `forEachFile` callback gets, and the effects it records on it
|
|
590
|
-
* `{ op: "write", text }`, `{ op: "rename", to }` and `{ op: "delete" }`.
|
|
800
|
+
* The `file` a `forEachFile` callback gets, and the effects it records on it.
|
|
591
801
|
* After `close()`, when the callback has finished, the file takes no more.
|
|
592
802
|
*/
|
|
593
803
|
export function fileHandle(path, text, language) {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
804
|
+
const effects = [];
|
|
805
|
+
let open = true;
|
|
806
|
+
const guard = (name) => {
|
|
807
|
+
if (!open)
|
|
808
|
+
throw new Error(`file.${name}() was called after the callback finished`);
|
|
809
|
+
};
|
|
810
|
+
const file = Object.freeze({
|
|
811
|
+
path,
|
|
812
|
+
text,
|
|
813
|
+
language,
|
|
814
|
+
write(next) {
|
|
815
|
+
guard("write");
|
|
816
|
+
if (typeof next !== "string")
|
|
817
|
+
throw new TypeError("file.write(text) expects a string");
|
|
818
|
+
effects.push({ op: "write", text: next });
|
|
819
|
+
},
|
|
820
|
+
rename(to) {
|
|
821
|
+
guard("rename");
|
|
822
|
+
if (typeof to !== "string" || to === "") {
|
|
823
|
+
throw new TypeError("file.rename(path) expects a non-empty path");
|
|
824
|
+
}
|
|
825
|
+
effects.push({ op: "rename", to });
|
|
826
|
+
},
|
|
827
|
+
delete() {
|
|
828
|
+
guard("delete");
|
|
829
|
+
effects.push({ op: "delete" });
|
|
830
|
+
},
|
|
831
|
+
});
|
|
832
|
+
return {
|
|
833
|
+
file,
|
|
834
|
+
effects,
|
|
835
|
+
close() {
|
|
836
|
+
open = false;
|
|
837
|
+
},
|
|
838
|
+
};
|
|
627
839
|
}
|
|
628
|
-
|
|
629
840
|
function messageOf(error) {
|
|
630
|
-
|
|
841
|
+
return isRecord(error) && "message" in error ? String(error.message) : String(error);
|
|
631
842
|
}
|
|
632
|
-
|
|
633
843
|
/** Throw a TypeError naming the first part of `value` that is not JSON. */
|
|
634
844
|
export function checkJson(value, path, seen = new Set()) {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
if (
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
845
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
846
|
+
return;
|
|
847
|
+
if (typeof value === "number") {
|
|
848
|
+
if (!Number.isFinite(value))
|
|
849
|
+
throw new TypeError(`${path} is ${value}, which is not JSON`);
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
if (typeof value !== "object") {
|
|
853
|
+
const what = value === undefined ? "undefined" : `a ${typeof value}`;
|
|
854
|
+
throw new TypeError(`${path} is ${what}, which is not JSON`);
|
|
855
|
+
}
|
|
856
|
+
if (seen.has(value))
|
|
857
|
+
throw new TypeError(`${path} is a circular reference`);
|
|
858
|
+
seen.add(value);
|
|
859
|
+
if (Array.isArray(value)) {
|
|
860
|
+
value.forEach((item, index) => checkJson(item, `${path}[${index}]`, seen));
|
|
861
|
+
}
|
|
862
|
+
else {
|
|
863
|
+
const proto = Object.getPrototypeOf(value);
|
|
864
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
865
|
+
const name = value.constructor?.name || "non-plain object";
|
|
866
|
+
throw new TypeError(`${path} is a ${name}, which is not JSON`);
|
|
867
|
+
}
|
|
868
|
+
for (const [key, item] of Object.entries(value))
|
|
869
|
+
checkJson(item, `${path}.${key}`, seen);
|
|
870
|
+
}
|
|
871
|
+
seen.delete(value);
|
|
657
872
|
}
|