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/host.mjs
CHANGED
|
@@ -8,383 +8,485 @@
|
|
|
8
8
|
//
|
|
9
9
|
// Code here has whatever Node.js gives it: capabilities are not enforced.
|
|
10
10
|
// The codemod CLI runs workflows in its own sandboxed runtime instead.
|
|
11
|
-
|
|
11
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
12
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
13
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
14
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return path;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Run a workflow in this Node.js or Bun process.
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { runWorkflow } from "codemod/host";
|
|
24
|
+
*
|
|
25
|
+
* const report = await runWorkflow("./my-codemod", { target: "./repo" });
|
|
26
|
+
* if (report.status !== "succeeded") throw new Error(report.error ?? report.status);
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* The workflow's body, function steps and `forEachFile` callbacks run here,
|
|
30
|
+
* so they can be debugged, covered and mocked. Host tasks (`jssg`, `shell`,
|
|
31
|
+
* `codemod`, ...) and the run store go through the native addon.
|
|
32
|
+
*
|
|
33
|
+
* Code here has whatever Node.js gives it: the capabilities codemod.yaml
|
|
34
|
+
* declares are not enforced. The codemod CLI runs workflows in its own
|
|
35
|
+
* sandboxed runtime instead; run packages from the registry with the CLI.
|
|
36
|
+
*
|
|
37
|
+
* @module
|
|
38
|
+
*/
|
|
12
39
|
import "../register.mjs";
|
|
13
|
-
|
|
14
|
-
import { readFileSync } from "node:fs";
|
|
15
40
|
import { isAbsolute, resolve } from "node:path";
|
|
16
41
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
17
|
-
|
|
18
42
|
import { addon } from "./addon.mjs";
|
|
19
|
-
import { context,
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
CanceledError,
|
|
23
|
-
checkJson,
|
|
24
|
-
createRuntime,
|
|
25
|
-
describeError,
|
|
26
|
-
fileHandle,
|
|
27
|
-
} from "./orchestrator.mjs";
|
|
43
|
+
import { context, stepScope } from "./context.mjs";
|
|
44
|
+
import { ORCHESTRATOR_VERSION } from "./orchestrator-version.mjs";
|
|
45
|
+
import { AwaitingApprovalError, CanceledError, checkJson, createRuntime, describeDuration, describeError, fileHandle, } from "./orchestrator.mjs";
|
|
28
46
|
import { callbackId } from "./workflow.mjs";
|
|
29
|
-
|
|
30
47
|
/**
|
|
31
|
-
* Run a workflow
|
|
48
|
+
* Run a workflow (a workflow file, a package directory, or the workflow
|
|
49
|
+
* module itself). Resolves for every outcome; rejects when the workflow
|
|
50
|
+
* cannot be loaded. YAML workflows run only with the CLI.
|
|
32
51
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* it through their own module system. A module needs `options.path` (or
|
|
52
|
+
* A module (`await import("./workflow.ts")`), for test runners that should
|
|
53
|
+
* load it through their own module system, needs `options.path` (or
|
|
36
54
|
* `options.workflowDir`) so relative task paths resolve.
|
|
37
55
|
*/
|
|
38
56
|
export async function runWorkflow(workflow, options = {}) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
57
|
+
const native = addon();
|
|
58
|
+
checkAddon(native);
|
|
59
|
+
const { path, load } = locate(workflow, options);
|
|
60
|
+
// A workflow that cannot be loaded is not a run: reject.
|
|
61
|
+
const module = await load();
|
|
62
|
+
const main = module?.default;
|
|
63
|
+
if (typeof main !== "function") {
|
|
64
|
+
throw new TypeError(`${path} must export a default function`);
|
|
65
|
+
}
|
|
66
|
+
if (options.captureConsole)
|
|
67
|
+
captureConsole();
|
|
68
|
+
const run = native.Run.open({
|
|
69
|
+
workflow: path,
|
|
70
|
+
target: resolve(options.target ?? "."),
|
|
71
|
+
params: options.params ?? {},
|
|
72
|
+
name: options.name,
|
|
73
|
+
dryRun: options.dryRun ?? false,
|
|
74
|
+
until: options.until,
|
|
75
|
+
given: options.given,
|
|
76
|
+
approve: options.approve ?? true,
|
|
77
|
+
capabilities: options.capabilities ?? [],
|
|
78
|
+
store: options.store,
|
|
79
|
+
resume: options.resume,
|
|
80
|
+
});
|
|
81
|
+
const flight = inFlight();
|
|
82
|
+
const runtime = createRuntime(host(run, flight, () => runtime));
|
|
83
|
+
flight.closed = () => runtime.isClosed();
|
|
84
|
+
const scope = { run, runtime, captureConsole: options.captureConsole ?? false };
|
|
85
|
+
const abort = () => run.cancel();
|
|
86
|
+
if (options.signal?.aborted)
|
|
87
|
+
abort();
|
|
88
|
+
options.signal?.addEventListener?.("abort", abort, { once: true });
|
|
89
|
+
let body;
|
|
90
|
+
try {
|
|
91
|
+
const input = { params: run.params, target: run.target, dryRun: run.dryRun };
|
|
92
|
+
const ended = context
|
|
93
|
+
.run(scope, async () => main(input))
|
|
94
|
+
.then((output) => ({ status: "succeeded", detail: toJson(output) }));
|
|
95
|
+
body = await Promise.race([ended, flight.stalled]);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
body = endingOf(error);
|
|
99
|
+
}
|
|
100
|
+
// Steps still running finish and are recorded before the run reports;
|
|
101
|
+
// none starts from here on.
|
|
102
|
+
runtime.close();
|
|
103
|
+
await flight.idle();
|
|
104
|
+
options.signal?.removeEventListener?.("abort", abort);
|
|
105
|
+
const report = run.finish(JSON.stringify(body));
|
|
106
|
+
return { ...report, events: run.takeEvents() };
|
|
89
107
|
}
|
|
90
|
-
|
|
91
108
|
let addonChecked = false;
|
|
92
|
-
|
|
93
109
|
/**
|
|
94
110
|
* The addon and this package share the orchestrator: one from another
|
|
95
111
|
* version would record and replay steps differently.
|
|
96
112
|
*/
|
|
97
113
|
function checkAddon(native) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
114
|
+
if (addonChecked)
|
|
115
|
+
return;
|
|
116
|
+
// An addon from before the check by version has no `orchestratorVersion`.
|
|
117
|
+
const built = typeof native.orchestratorVersion === "function" ? native.orchestratorVersion() : undefined;
|
|
118
|
+
if (built !== ORCHESTRATOR_VERSION) {
|
|
119
|
+
throw new Error("the codemod native addon is from another version than the codemod package; reinstall codemod so both match (in a checkout, rebuild both: `cargo build -p codemod-node` and `pnpm --filter codemod build`)");
|
|
120
|
+
}
|
|
121
|
+
addonChecked = true;
|
|
106
122
|
}
|
|
107
|
-
|
|
123
|
+
const CONSOLE_METHODS = ["log", "info", "debug", "warn", "error"];
|
|
108
124
|
/**
|
|
109
125
|
* Route `console` output of code that runs in a run started with
|
|
110
126
|
* `captureConsole` to the run's logs, by step. Other code is untouched.
|
|
111
127
|
*/
|
|
112
128
|
function captureConsole() {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
129
|
+
const key = Symbol.for("codemod.host.console");
|
|
130
|
+
const shared = globalThis;
|
|
131
|
+
if (shared[key])
|
|
132
|
+
return;
|
|
133
|
+
shared[key] = true;
|
|
134
|
+
for (const method of CONSOLE_METHODS) {
|
|
135
|
+
const original = console[method].bind(console);
|
|
136
|
+
console[method] = (...items) => {
|
|
137
|
+
const store = context.getStore();
|
|
138
|
+
if (!store?.captureConsole)
|
|
139
|
+
return original(...items);
|
|
140
|
+
store.run.log(store.step?.position ?? null, items.map(formatLogItem).join(" "));
|
|
141
|
+
};
|
|
142
|
+
}
|
|
124
143
|
}
|
|
125
|
-
|
|
126
144
|
function formatLogItem(value) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
145
|
+
if (typeof value === "string")
|
|
146
|
+
return value;
|
|
147
|
+
if (value instanceof Error)
|
|
148
|
+
return value.stack || `${value.name}: ${value.message}`;
|
|
149
|
+
try {
|
|
150
|
+
return JSON.stringify(value) ?? String(value);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return String(value);
|
|
154
|
+
}
|
|
134
155
|
}
|
|
135
|
-
|
|
136
156
|
/** Where the workflow is, and how to load it. */
|
|
137
157
|
function locate(workflow, options) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
158
|
+
if (typeof workflow !== "string" && !isUrl(workflow)) {
|
|
159
|
+
if (!options.path && !options.workflowDir) {
|
|
160
|
+
throw new TypeError("runWorkflow(module, { path }): give the workflow file's path, so relative task paths resolve");
|
|
161
|
+
}
|
|
162
|
+
// The file need not exist: only its directory matters here.
|
|
163
|
+
const file = options.path
|
|
164
|
+
? resolve(String(options.path))
|
|
165
|
+
: resolve(String(options.workflowDir), "workflow.ts");
|
|
166
|
+
return { path: file, load: async () => workflow };
|
|
167
|
+
}
|
|
168
|
+
const source = isUrl(workflow) ? fileURLToPath(workflow.href) : String(workflow);
|
|
169
|
+
const path = addon().resolveWorkflow(isAbsolute(source) ? source : resolve(source), options.workflowName ?? null);
|
|
170
|
+
if (!/\.m?ts$|\.m?js$/.test(path)) {
|
|
171
|
+
throw new Error(`${path} is not a TypeScript workflow; run YAML workflows with the codemod CLI, or upgrade them with \`codemod workflow upgrade\``);
|
|
143
172
|
}
|
|
144
|
-
|
|
145
|
-
const file = options.path
|
|
146
|
-
? resolve(String(options.path))
|
|
147
|
-
: resolve(String(options.workflowDir), "workflow.ts");
|
|
148
|
-
return { path: file, load: async () => workflow };
|
|
149
|
-
}
|
|
150
|
-
const source = isUrl(workflow) ? fileURLToPath(workflow.href) : String(workflow);
|
|
151
|
-
const path = addon().resolveWorkflow(
|
|
152
|
-
isAbsolute(source) ? source : resolve(source),
|
|
153
|
-
options.workflowName ?? null,
|
|
154
|
-
);
|
|
155
|
-
if (!/\.m?ts$|\.m?js$/.test(path)) {
|
|
156
|
-
throw new Error(
|
|
157
|
-
`${path} is not a TypeScript workflow; run YAML workflows with the codemod CLI, or upgrade them with \`codemod workflow upgrade\``,
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
return { path, load: () => import(pathToFileURL(path).href) };
|
|
173
|
+
return { path, load: () => import(__rewriteRelativeImportExtension(pathToFileURL(path).href)) };
|
|
161
174
|
}
|
|
162
|
-
|
|
175
|
+
function isRecord(value) {
|
|
176
|
+
return value !== null && typeof value === "object";
|
|
177
|
+
}
|
|
178
|
+
// Not `instanceof URL`: test environments may have their own URL class.
|
|
163
179
|
function isUrl(value) {
|
|
164
|
-
|
|
180
|
+
return isRecord(value) && typeof value.href === "string";
|
|
165
181
|
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Tracks steps in flight. `stalled` resolves (with `null`) when the run is
|
|
169
|
-
* closed and nothing is running: the body may be waiting on steps that will
|
|
170
|
-
* never start, as after a test's `until` step. `idle()` resolves once nothing
|
|
171
|
-
* is running.
|
|
172
|
-
*/
|
|
173
182
|
function inFlight() {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
183
|
+
let count = 0;
|
|
184
|
+
let stall = () => { };
|
|
185
|
+
const waiters = [];
|
|
186
|
+
const flight = {
|
|
187
|
+
closed: () => false,
|
|
188
|
+
stalled: new Promise((resolvePromise) => (stall = resolvePromise)),
|
|
189
|
+
begin() {
|
|
190
|
+
count++;
|
|
191
|
+
},
|
|
192
|
+
end() {
|
|
193
|
+
count--;
|
|
194
|
+
flight.check();
|
|
195
|
+
},
|
|
196
|
+
check() {
|
|
197
|
+
setImmediate(() => {
|
|
198
|
+
if (count !== 0)
|
|
199
|
+
return;
|
|
200
|
+
if (flight.closed())
|
|
201
|
+
stall(null);
|
|
202
|
+
for (const waiter of waiters.splice(0))
|
|
203
|
+
waiter();
|
|
204
|
+
});
|
|
205
|
+
},
|
|
206
|
+
idle() {
|
|
207
|
+
return new Promise((resolvePromise) => {
|
|
208
|
+
waiters.push(resolvePromise);
|
|
209
|
+
flight.check();
|
|
210
|
+
});
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
return flight;
|
|
203
214
|
}
|
|
204
|
-
|
|
205
215
|
function host(run, flight, runtime) {
|
|
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
|
-
|
|
216
|
+
// Replayed steps settle one per turn of the event loop, so the body reacts
|
|
217
|
+
// to each before the next.
|
|
218
|
+
let replaying = false;
|
|
219
|
+
const replay = () => {
|
|
220
|
+
replaying = runtime().replayNext();
|
|
221
|
+
if (replaying)
|
|
222
|
+
setImmediate(replay);
|
|
223
|
+
};
|
|
224
|
+
return {
|
|
225
|
+
history: () => run.history(),
|
|
226
|
+
record: (entry, ms) => run.record(JSON.stringify(entry), ms),
|
|
227
|
+
event: (event) => run.event(JSON.stringify(event)),
|
|
228
|
+
canceled: () => run.refusesSteps(),
|
|
229
|
+
now: () => Date.now(),
|
|
230
|
+
advanceClock() { },
|
|
231
|
+
testPlan: () => run.testPlan(),
|
|
232
|
+
stop() {
|
|
233
|
+
run.stop();
|
|
234
|
+
flight.check();
|
|
235
|
+
},
|
|
236
|
+
wake() {
|
|
237
|
+
if (replaying)
|
|
238
|
+
return;
|
|
239
|
+
replaying = true;
|
|
240
|
+
setImmediate(replay);
|
|
241
|
+
},
|
|
242
|
+
evaluate: (kind, expression, scope) => run.evaluate(kind, expression, JSON.stringify(scope)),
|
|
243
|
+
callbackId,
|
|
244
|
+
async start(request) {
|
|
245
|
+
const { fn, ...task } = request;
|
|
246
|
+
flight.begin();
|
|
247
|
+
try {
|
|
248
|
+
if (fn && task.task.kind === "jssg") {
|
|
249
|
+
return await reduceInPlace(run, task, fn);
|
|
250
|
+
}
|
|
251
|
+
if (fn)
|
|
252
|
+
return await within(task.timeoutMs, (limit) => runInPlace(run, task, fn, limit));
|
|
253
|
+
return await runNative(run, task);
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
flight.end();
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
async function runNative(run, task) {
|
|
262
|
+
try {
|
|
263
|
+
return await run.runTask(JSON.stringify(task));
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
throw payloadOf(error);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* `jssg(path, { reduce, initial })`: the transform runs natively and returns
|
|
271
|
+
* every file's data, which is then folded here before applying any edits.
|
|
272
|
+
*/
|
|
273
|
+
async function reduceInPlace(run, task, fn) {
|
|
274
|
+
const { reduce, ...content } = task.task;
|
|
275
|
+
const outer = context.getStore();
|
|
276
|
+
if (!outer)
|
|
277
|
+
throw new Error("a step started outside its run");
|
|
278
|
+
const { prepared, folded } = await within(task.timeoutMs, async (limit) => {
|
|
279
|
+
const prepared = await run
|
|
280
|
+
.prepareJssg(JSON.stringify({ ...task, task: content }))
|
|
281
|
+
.catch((error) => {
|
|
282
|
+
throw payloadOf(error);
|
|
283
|
+
});
|
|
284
|
+
const step = stepScope(task.position, task.label);
|
|
285
|
+
// A copy, so a reducer that changes its accumulator can't change a retry's.
|
|
286
|
+
let folded = structuredClone(reduce.initial);
|
|
287
|
+
for (const entry of prepared.data) {
|
|
288
|
+
limit.check();
|
|
289
|
+
if (run.refusesSteps())
|
|
290
|
+
throw new CanceledError();
|
|
291
|
+
folded = (await context.run({ ...outer, step }, () => fn(folded, entry)));
|
|
292
|
+
// A failure reported with failStep() stands even if the reducer caught it.
|
|
293
|
+
if (step.failure !== undefined)
|
|
294
|
+
throw new Error(step.failure);
|
|
241
295
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
296
|
+
if (folded === undefined)
|
|
297
|
+
folded = null;
|
|
298
|
+
checkJson(folded, "the reducer's result");
|
|
299
|
+
const data = JSON.stringify(folded);
|
|
300
|
+
limit.check();
|
|
301
|
+
return { prepared, folded: data };
|
|
302
|
+
});
|
|
303
|
+
// Once the deadline has been met, wait for the commit instead of racing it
|
|
304
|
+
// with the timer: a step must not report a timeout after applying its edits.
|
|
305
|
+
try {
|
|
306
|
+
return await prepared.commit(folded);
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
throw payloadOf(error);
|
|
310
|
+
}
|
|
247
311
|
}
|
|
248
|
-
|
|
249
312
|
/** A host task rejected with `{ name, message }` as JSON in the message. */
|
|
250
313
|
function payloadOf(error) {
|
|
251
|
-
|
|
252
|
-
const payload = JSON.parse(error.message);
|
|
253
|
-
if (payload && typeof payload.message === "string") return payload;
|
|
254
|
-
} catch {
|
|
255
|
-
// a plain message
|
|
256
|
-
}
|
|
257
|
-
return { name: "Error", message: error?.message ?? String(error) };
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// ----------------------------------------------------------- in place ---
|
|
261
|
-
|
|
262
|
-
async function runInPlace(run, task, fn) {
|
|
263
|
-
const outer = context.getStore();
|
|
264
|
-
if (task.task.kind === "function") {
|
|
265
|
-
const step = stepScope(task.position, task.label, task.state);
|
|
266
|
-
let value;
|
|
314
|
+
const message = isRecord(error) && typeof error.message === "string" ? error.message : undefined;
|
|
267
315
|
try {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
316
|
+
const payload = JSON.parse(message ?? "");
|
|
317
|
+
if (isRecord(payload) && typeof payload.message === "string") {
|
|
318
|
+
return { name: String(payload.name ?? "Error"), message: payload.message };
|
|
319
|
+
}
|
|
272
320
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
return outcome(result, step);
|
|
278
|
-
}
|
|
279
|
-
if (task.task.kind === "forEachFile") return forEachFile(run, outer, task, fn);
|
|
280
|
-
throw new Error(`cannot run a ${task.task.kind} task in place`);
|
|
321
|
+
catch {
|
|
322
|
+
// a plain message
|
|
323
|
+
}
|
|
324
|
+
return { name: "Error", message: message ?? String(error) };
|
|
281
325
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
326
|
+
/**
|
|
327
|
+
* Run `work` for at most `ms`. JavaScript that is running cannot be stopped
|
|
328
|
+
* from outside: when the time is up the step fails, `limit.expired` tells
|
|
329
|
+
* the work to stop where it can, and whatever it still does is discarded.
|
|
330
|
+
*/
|
|
331
|
+
async function within(ms, work) {
|
|
332
|
+
const started = performance.now();
|
|
333
|
+
const timeout = (ms) => ({
|
|
334
|
+
name: "Error",
|
|
335
|
+
message: `timed out after ${describeDuration(ms)}`,
|
|
336
|
+
});
|
|
337
|
+
const limit = {
|
|
338
|
+
expired: false,
|
|
339
|
+
check() {
|
|
340
|
+
if (ms !== undefined && (limit.expired || performance.now() - started >= ms))
|
|
341
|
+
throw timeout(ms);
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
if (ms === undefined)
|
|
345
|
+
return work(limit);
|
|
346
|
+
let timer;
|
|
347
|
+
const late = new Promise((_, reject) => {
|
|
348
|
+
timer = setTimeout(() => {
|
|
349
|
+
limit.expired = true;
|
|
350
|
+
reject(timeout(ms));
|
|
351
|
+
}, ms);
|
|
352
|
+
});
|
|
353
|
+
try {
|
|
354
|
+
return await Promise.race([work(limit), late]);
|
|
355
|
+
}
|
|
356
|
+
finally {
|
|
357
|
+
clearTimeout(timer);
|
|
358
|
+
}
|
|
286
359
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
360
|
+
async function runInPlace(run, task, fn, limit) {
|
|
361
|
+
const outer = context.getStore();
|
|
362
|
+
if (!outer)
|
|
363
|
+
throw new Error("a step started outside its run");
|
|
364
|
+
if (task.task.kind === "function") {
|
|
365
|
+
const step = stepScope(task.position, task.label);
|
|
366
|
+
const value = await context.run({ ...outer, step }, () => fn());
|
|
367
|
+
// A failure reported with failStep() stands even if the code caught it.
|
|
368
|
+
if (step.failure !== undefined)
|
|
369
|
+
throw new Error(step.failure);
|
|
370
|
+
const result = value === undefined ? null : value;
|
|
371
|
+
checkJson(result, "the step's result");
|
|
372
|
+
return { result };
|
|
373
|
+
}
|
|
374
|
+
if (task.task.kind === "forEachFile")
|
|
375
|
+
return forEachFile(run, outer, task, fn, limit);
|
|
376
|
+
throw new Error(`cannot run a ${task.task.kind} task in place`);
|
|
292
377
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
378
|
+
async function forEachFile(run, outer, task, fn, limit) {
|
|
379
|
+
const selector = task.task.selector;
|
|
380
|
+
const { language, files } = await run.selectFiles(JSON.stringify(selector));
|
|
381
|
+
const step = stepScope(task.position, task.label, language ?? undefined);
|
|
382
|
+
const total = files.length;
|
|
383
|
+
const every = Math.max(1, Math.floor(total / 100));
|
|
384
|
+
let processed = 0;
|
|
385
|
+
const outputs = Array.from({ length: total });
|
|
386
|
+
const failures = [];
|
|
387
|
+
const effects = [];
|
|
388
|
+
const concurrency = selector.concurrency ?? 1;
|
|
389
|
+
const one = async (index, [path, text]) => {
|
|
390
|
+
const { file, effects: done, close } = fileHandle(path, text, step.language);
|
|
391
|
+
try {
|
|
392
|
+
step.failure = undefined;
|
|
393
|
+
const output = await context.run({ ...outer, step }, () => fn(file));
|
|
394
|
+
// A failure reported with failFile() stands even if the callback
|
|
395
|
+
// caught it. With files in flight together it cannot be told whose it
|
|
396
|
+
// was, as in the CLI.
|
|
397
|
+
if (step.failure !== undefined && concurrency === 1) {
|
|
398
|
+
failures.push({ path, error: step.failure });
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (output !== undefined) {
|
|
402
|
+
checkJson(output, "return value");
|
|
403
|
+
outputs[index] = { output };
|
|
404
|
+
}
|
|
405
|
+
if (done.length)
|
|
406
|
+
effects.push({ path, original: text, effects: done });
|
|
407
|
+
}
|
|
408
|
+
catch (error) {
|
|
409
|
+
failures.push({ path, error: describeError(error) });
|
|
410
|
+
}
|
|
411
|
+
finally {
|
|
412
|
+
close();
|
|
413
|
+
processed++;
|
|
414
|
+
if (processed === total || processed % every === 0) {
|
|
415
|
+
run.progress(task.position, processed, total);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
let next = 0;
|
|
420
|
+
let sinceTurn = 0;
|
|
421
|
+
const worker = async () => {
|
|
422
|
+
for (;;) {
|
|
423
|
+
const index = next++;
|
|
424
|
+
const file = files[index];
|
|
425
|
+
if (!file)
|
|
426
|
+
return;
|
|
427
|
+
// Callbacks that never wait would keep the event loop to themselves:
|
|
428
|
+
// give it a turn now and then, so a cancel can arrive.
|
|
429
|
+
if (++sinceTurn >= 64) {
|
|
430
|
+
sinceTurn = 0;
|
|
431
|
+
await new Promise((turn) => setImmediate(turn));
|
|
432
|
+
}
|
|
433
|
+
if (run.refusesSteps())
|
|
434
|
+
throw new CanceledError();
|
|
435
|
+
if (limit.expired)
|
|
436
|
+
return;
|
|
437
|
+
await one(index, file);
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, total) }, worker));
|
|
441
|
+
// Out of time: the step has already failed, and nothing it did is written.
|
|
442
|
+
if (limit.expired)
|
|
443
|
+
throw new CanceledError("the step ran out of time");
|
|
444
|
+
effects.sort((a, b) => (a.path < b.path ? -1 : 1));
|
|
445
|
+
const committed = await run.commitFiles(task.position, task.label, JSON.stringify(effects));
|
|
446
|
+
const allFailures = [...failures, ...committed.failures].sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
447
|
+
// As the CLI does: each file that failed, then its error.
|
|
448
|
+
for (const { path, error } of allFailures) {
|
|
449
|
+
run.log(task.position, path);
|
|
450
|
+
for (const line of error.split("\n"))
|
|
451
|
+
run.log(task.position, ` ${line}`);
|
|
332
452
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
while (next < total) {
|
|
339
|
-
// Callbacks that never wait would keep the event loop to themselves:
|
|
340
|
-
// give it a turn now and then, so a cancel can arrive.
|
|
341
|
-
if (++sinceTurn >= 64) {
|
|
342
|
-
sinceTurn = 0;
|
|
343
|
-
await new Promise((turn) => setImmediate(turn));
|
|
344
|
-
}
|
|
345
|
-
if (run.refusesSteps()) throw new CanceledError();
|
|
346
|
-
await one(next++);
|
|
453
|
+
// The step fails when the callback failed on every file, and is partial
|
|
454
|
+
// when it failed on some.
|
|
455
|
+
const [first] = allFailures;
|
|
456
|
+
if (first && allFailures.length === total) {
|
|
457
|
+
throw new Error(`the callback failed on every file; first error: ${first.path}: ${first.error}`);
|
|
347
458
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
changes: committed.changes,
|
|
362
|
-
failures: allFailures,
|
|
363
|
-
metrics,
|
|
364
|
-
},
|
|
365
|
-
step,
|
|
366
|
-
);
|
|
459
|
+
const result = {
|
|
460
|
+
result: {
|
|
461
|
+
outputs: outputs.flatMap((entry) => (entry ? [entry.output] : [])),
|
|
462
|
+
changes: committed.changes,
|
|
463
|
+
failures: allFailures,
|
|
464
|
+
},
|
|
465
|
+
};
|
|
466
|
+
if (!allFailures.length)
|
|
467
|
+
return result;
|
|
468
|
+
return {
|
|
469
|
+
...result,
|
|
470
|
+
partial: `${allFailures.length} of ${total} ${total === 1 ? "file" : "files"} failed`,
|
|
471
|
+
};
|
|
367
472
|
}
|
|
368
|
-
|
|
369
473
|
// -------------------------------------------------------------- ending ---
|
|
370
|
-
|
|
371
474
|
/** How a body that threw ended. */
|
|
372
475
|
function endingOf(error) {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
476
|
+
const name = isRecord(error) ? error.name : undefined;
|
|
477
|
+
if (error instanceof CanceledError || name === "CanceledError") {
|
|
478
|
+
return { status: "canceled" };
|
|
479
|
+
}
|
|
480
|
+
if (error instanceof AwaitingApprovalError || name === "AwaitingApprovalError") {
|
|
481
|
+
return { status: "awaiting-approval", detail: String(error.message) };
|
|
482
|
+
}
|
|
483
|
+
const message = isRecord(error) && "message" in error
|
|
484
|
+
? `${String(name ?? "Error")}: ${String(error.message)}`
|
|
485
|
+
: String(error);
|
|
486
|
+
return { status: "failed", detail: message };
|
|
384
487
|
}
|
|
385
|
-
|
|
386
488
|
/** A value as JSON keeps it; what JSON has no form for (a function) is null. */
|
|
387
489
|
function toJson(value) {
|
|
388
|
-
|
|
389
|
-
|
|
490
|
+
const text = JSON.stringify(value);
|
|
491
|
+
return text === undefined ? null : JSON.parse(text);
|
|
390
492
|
}
|