codemod 2.0.0-alpha.1 → 2.0.0-alpha.2
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 +57 -2
- package/codemod +17 -30
- package/package.json +78 -10
- package/register.d.mts +11 -0
- package/register.mjs +60 -0
- package/runtime/addon.mjs +59 -0
- package/runtime/ast-grep.mjs +93 -0
- package/runtime/context.mjs +77 -0
- package/runtime/hooks.mjs +9 -0
- package/runtime/host.d.mts +120 -0
- package/runtime/host.mjs +390 -0
- package/runtime/llm.mjs +11 -0
- package/runtime/metrics.mjs +45 -0
- package/runtime/modules.mjs +19 -0
- package/runtime/orchestrator.mjs +657 -0
- package/runtime/runtime.mjs +48 -0
- package/runtime/workflow.mjs +135 -0
- package/testing/index.d.mts +203 -0
- package/testing/index.mjs +638 -0
- package/types/index.d.ts +8 -0
- package/types/langs/angular.d.ts +5 -0
- package/types/langs/c.d.ts +5 -0
- package/types/langs/c_sharp.d.ts +5 -0
- package/types/langs/cpp.d.ts +5 -0
- package/types/langs/css.d.ts +5 -0
- package/types/langs/elixir.d.ts +5 -0
- package/types/langs/go.d.ts +5 -0
- package/types/langs/html.d.ts +5 -0
- package/types/langs/java.d.ts +5 -0
- package/types/langs/javascript.d.ts +5 -0
- package/types/langs/json.d.ts +5 -0
- package/types/langs/kotlin.d.ts +5 -0
- package/types/langs/php.d.ts +5 -0
- package/types/langs/python.d.ts +5 -0
- package/types/langs/ruby.d.ts +5 -0
- package/types/langs/rust.d.ts +5 -0
- package/types/langs/scala.d.ts +5 -0
- package/types/langs/toml.d.ts +5 -0
- package/types/langs/tsx.d.ts +5 -0
- package/types/langs/typescript.d.ts +5 -0
- package/types/langs/xml.d.ts +5 -0
- package/types/langs/yaml.d.ts +5 -0
- package/types/llm.d.ts +6 -0
- package/types/llrt/abort.d.ts +69 -0
- package/types/llrt/assert.d.ts +39 -0
- package/types/llrt/async_hooks.d.ts +210 -0
- package/types/llrt/buffer.d.ts +1186 -0
- package/types/llrt/child_process.d.ts +447 -0
- package/types/llrt/console.d.ts +210 -0
- package/types/llrt/crypto.d.ts +854 -0
- package/types/llrt/dgram.d.ts +381 -0
- package/types/llrt/dns.d.ts +124 -0
- package/types/llrt/dom-events.d.ts +51 -0
- package/types/llrt/events.d.ts +245 -0
- package/types/llrt/exceptions.d.ts +26 -0
- package/types/llrt/fetch.d.ts +332 -0
- package/types/llrt/fs/promises.d.ts +554 -0
- package/types/llrt/fs.d.ts +360 -0
- package/types/llrt/globals.d.ts +23 -0
- package/types/llrt/https.d.ts +27 -0
- package/types/llrt/index.d.ts +33 -0
- package/types/llrt/module.d.ts +285 -0
- package/types/llrt/navigator.d.ts +9 -0
- package/types/llrt/net.d.ts +488 -0
- package/types/llrt/os.d.ts +293 -0
- package/types/llrt/path.d.ts +150 -0
- package/types/llrt/perf_hooks.d.ts +23 -0
- package/types/llrt/process.d.ts +502 -0
- package/types/llrt/stream/web.d.ts +526 -0
- package/types/llrt/stream.d.ts +366 -0
- package/types/llrt/string_decoder.d.ts +63 -0
- package/types/llrt/timers.d.ts +72 -0
- package/types/llrt/timezone.d.ts +101 -0
- package/types/llrt/tty.d.ts +11 -0
- package/types/llrt/url.d.ts +774 -0
- package/types/llrt/util.d.ts +240 -0
- package/types/llrt/zlib.d.ts +177 -0
- package/types/main.d.ts +5 -0
- package/types/metrics.d.ts +5 -0
- package/types/modules/ast-grep.d.ts +634 -0
- package/types/modules/langs.d.ts +87 -0
- package/types/modules/llm.d.ts +23 -0
- package/types/modules/metrics.d.ts +107 -0
- package/types/modules/runtime.d.ts +66 -0
- package/types/modules/workflow.d.ts +750 -0
- package/types/runtime.d.ts +6 -0
- package/types/workflow.d.ts +5 -0
- package/vite.d.mts +24 -0
- package/vite.mjs +26 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run a workflow in this Node.js or Bun process.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { runWorkflow } from "codemod/host";
|
|
6
|
+
*
|
|
7
|
+
* const report = await runWorkflow("./my-codemod", { target: "./repo" });
|
|
8
|
+
* if (report.status !== "succeeded") throw new Error(report.error ?? report.status);
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* The workflow's body, function steps and `forEachFile` callbacks run here,
|
|
12
|
+
* so they can be debugged, covered and mocked. Host tasks (`jssg`, `shell`,
|
|
13
|
+
* `codemod`, ...) and the run store go through the native addon.
|
|
14
|
+
*
|
|
15
|
+
* Code here has whatever Node.js gives it: the capabilities codemod.yaml
|
|
16
|
+
* declares are not enforced. The codemod CLI runs workflows in its own
|
|
17
|
+
* sandboxed runtime instead; run packages from the registry with the CLI.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { JsonValue } from "../testing/index.d.mts";
|
|
23
|
+
|
|
24
|
+
/** A `URL` (`new URL("./workflow.ts", import.meta.url)`); only `file:` URLs are paths. */
|
|
25
|
+
export interface UrlLike {
|
|
26
|
+
readonly href: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** An `AbortSignal`. */
|
|
30
|
+
export interface AbortSignalLike {
|
|
31
|
+
readonly aborted: boolean;
|
|
32
|
+
addEventListener(type: "abort", listener: () => void, options?: { once?: boolean }): void;
|
|
33
|
+
removeEventListener(type: "abort", listener: () => void): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A workflow module: what `import("./workflow.ts")` gives. */
|
|
37
|
+
export interface WorkflowModule {
|
|
38
|
+
default: (context: {
|
|
39
|
+
params: Record<string, JsonValue>;
|
|
40
|
+
target: string;
|
|
41
|
+
dryRun: boolean;
|
|
42
|
+
}) => unknown;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface HostRunOptions {
|
|
46
|
+
/** The directory the workflow changes. Default: the current directory. */
|
|
47
|
+
target?: string;
|
|
48
|
+
/** The workflow's parameters. */
|
|
49
|
+
params?: Record<string, JsonValue>;
|
|
50
|
+
/** Run every step but change no files. */
|
|
51
|
+
dryRun?: boolean;
|
|
52
|
+
/** Stop after the step with this label (a test run). */
|
|
53
|
+
until?: string;
|
|
54
|
+
/** Results for steps that should not run, by label (a test run). */
|
|
55
|
+
given?: Record<string, JsonValue>;
|
|
56
|
+
/** Give every approval the workflow asks for. Default `true`. */
|
|
57
|
+
approve?: boolean;
|
|
58
|
+
/** Capabilities for host tasks' transforms, on top of those codemod.yaml declares. */
|
|
59
|
+
capabilities?: ("fs" | "fetch" | "child_process")[];
|
|
60
|
+
/** Which workflow to run when codemod.yaml defines several. */
|
|
61
|
+
workflowName?: string;
|
|
62
|
+
/**
|
|
63
|
+
* `"memory"` (the default) keeps nothing; `"local"` records the run where
|
|
64
|
+
* `codemod workflow list` and `status` show it.
|
|
65
|
+
*/
|
|
66
|
+
store?: "memory" | "local";
|
|
67
|
+
/**
|
|
68
|
+
* Continue this run (with `store: "local"`). A run is resumed by the host
|
|
69
|
+
* that started it: one started here cannot be resumed with
|
|
70
|
+
* `codemod workflow resume`, nor one the CLI started here.
|
|
71
|
+
*/
|
|
72
|
+
resume?: string;
|
|
73
|
+
/** The workflow file, when `workflow` is the module itself. */
|
|
74
|
+
path?: string;
|
|
75
|
+
/** Instead of `path`: the directory relative task paths resolve against. */
|
|
76
|
+
workflowDir?: string;
|
|
77
|
+
/** Send `console` output of the workflow's code to the report's events. */
|
|
78
|
+
captureConsole?: boolean;
|
|
79
|
+
/** Stops the run. */
|
|
80
|
+
signal?: AbortSignalLike;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface HostStepReport {
|
|
84
|
+
readonly position: number;
|
|
85
|
+
readonly label: string;
|
|
86
|
+
readonly status: "succeeded" | "failed";
|
|
87
|
+
readonly result: JsonValue | null;
|
|
88
|
+
readonly error: string | null;
|
|
89
|
+
readonly given: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface HostRunReport {
|
|
93
|
+
readonly runId: string;
|
|
94
|
+
readonly status: "succeeded" | "failed" | "canceled" | "awaiting-approval" | "stopped";
|
|
95
|
+
readonly output: JsonValue | null;
|
|
96
|
+
readonly error: string | null;
|
|
97
|
+
/** The approval the run waits for. */
|
|
98
|
+
readonly awaiting: string | null;
|
|
99
|
+
/** The `until` step the run stopped after. */
|
|
100
|
+
readonly stoppedAt: string | null;
|
|
101
|
+
readonly steps: readonly HostStepReport[];
|
|
102
|
+
readonly metrics: Record<string, unknown>;
|
|
103
|
+
readonly stats: {
|
|
104
|
+
readonly filesModified: number;
|
|
105
|
+
readonly filesUnmodified: number;
|
|
106
|
+
readonly filesWithErrors: number;
|
|
107
|
+
};
|
|
108
|
+
/** What the engine reported while the run progressed. */
|
|
109
|
+
readonly events: readonly ({ readonly event: string } & Record<string, unknown>)[];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Run a workflow (a workflow file, a package directory, or the workflow
|
|
114
|
+
* module itself). Resolves for every outcome; rejects when the workflow
|
|
115
|
+
* cannot be loaded. YAML workflows run only with the CLI.
|
|
116
|
+
*/
|
|
117
|
+
export function runWorkflow(
|
|
118
|
+
workflow: string | UrlLike | WorkflowModule,
|
|
119
|
+
options?: HostRunOptions,
|
|
120
|
+
): Promise<HostRunReport>;
|
package/runtime/host.mjs
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// `codemod/host`: run a workflow in this Node.js or Bun process.
|
|
2
|
+
//
|
|
3
|
+
// The workflow module is imported here (or given already imported), so its
|
|
4
|
+
// body, function steps and `forEachFile` callbacks run in this process: they
|
|
5
|
+
// can be debugged, covered and mocked like any other code. What the engine
|
|
6
|
+
// owns goes through the native addon: the run store, host tasks (`jssg`,
|
|
7
|
+
// `shell`, `codemod`, ...), and selecting and committing files.
|
|
8
|
+
//
|
|
9
|
+
// Code here has whatever Node.js gives it: capabilities are not enforced.
|
|
10
|
+
// The codemod CLI runs workflows in its own sandboxed runtime instead.
|
|
11
|
+
|
|
12
|
+
import "../register.mjs";
|
|
13
|
+
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
import { isAbsolute, resolve } from "node:path";
|
|
16
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
17
|
+
|
|
18
|
+
import { addon } from "./addon.mjs";
|
|
19
|
+
import { context, metricsOf, stateChanges, stepScope } from "./context.mjs";
|
|
20
|
+
import {
|
|
21
|
+
AwaitingApprovalError,
|
|
22
|
+
CanceledError,
|
|
23
|
+
checkJson,
|
|
24
|
+
createRuntime,
|
|
25
|
+
describeError,
|
|
26
|
+
fileHandle,
|
|
27
|
+
} from "./orchestrator.mjs";
|
|
28
|
+
import { callbackId } from "./workflow.mjs";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Run a workflow and resolve with its report, for every outcome.
|
|
32
|
+
*
|
|
33
|
+
* `workflow` is a workflow file, a package directory, or the workflow module
|
|
34
|
+
* itself (`await import("./workflow.ts")`), for test runners that should load
|
|
35
|
+
* it through their own module system. A module needs `options.path` (or
|
|
36
|
+
* `options.workflowDir`) so relative task paths resolve.
|
|
37
|
+
*/
|
|
38
|
+
export async function runWorkflow(workflow, options = {}) {
|
|
39
|
+
const native = addon();
|
|
40
|
+
checkAddon(native);
|
|
41
|
+
const { path, load } = locate(workflow, options);
|
|
42
|
+
// A workflow that cannot be loaded is not a run: reject.
|
|
43
|
+
const module = await load();
|
|
44
|
+
const main = module?.default;
|
|
45
|
+
if (typeof main !== "function") {
|
|
46
|
+
throw new TypeError(`${path} must export a default function`);
|
|
47
|
+
}
|
|
48
|
+
if (options.captureConsole) captureConsole();
|
|
49
|
+
const run = native.Run.open({
|
|
50
|
+
workflow: path,
|
|
51
|
+
target: resolve(options.target ?? "."),
|
|
52
|
+
params: options.params ?? {},
|
|
53
|
+
name: options.name,
|
|
54
|
+
dryRun: options.dryRun ?? false,
|
|
55
|
+
until: options.until,
|
|
56
|
+
given: options.given,
|
|
57
|
+
approve: options.approve ?? true,
|
|
58
|
+
capabilities: options.capabilities ?? [],
|
|
59
|
+
store: options.store,
|
|
60
|
+
resume: options.resume,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const flight = inFlight();
|
|
64
|
+
const runtime = createRuntime(host(run, flight, () => runtime));
|
|
65
|
+
flight.closed = () => runtime.isClosed();
|
|
66
|
+
const scope = { run, runtime, captureConsole: options.captureConsole ?? false };
|
|
67
|
+
const abort = () => run.cancel();
|
|
68
|
+
if (options.signal?.aborted) abort();
|
|
69
|
+
options.signal?.addEventListener?.("abort", abort, { once: true });
|
|
70
|
+
|
|
71
|
+
// How the body ended; `null` when it cannot end any more.
|
|
72
|
+
let body;
|
|
73
|
+
try {
|
|
74
|
+
const input = { params: run.params, target: run.target, dryRun: run.dryRun };
|
|
75
|
+
const ended = context
|
|
76
|
+
.run(scope, () => main(input))
|
|
77
|
+
.then((output) => ({ status: "succeeded", detail: toJson(output) }));
|
|
78
|
+
body = await Promise.race([ended, flight.stalled]);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
body = endingOf(error);
|
|
81
|
+
}
|
|
82
|
+
// Steps still running finish and are recorded before the run reports;
|
|
83
|
+
// none starts from here on.
|
|
84
|
+
runtime.close();
|
|
85
|
+
await flight.idle();
|
|
86
|
+
options.signal?.removeEventListener?.("abort", abort);
|
|
87
|
+
const report = run.finish(JSON.stringify(body));
|
|
88
|
+
return { ...report, events: run.takeEvents() };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let addonChecked = false;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The addon and this package share the orchestrator: one from another
|
|
95
|
+
* version would record and replay steps differently.
|
|
96
|
+
*/
|
|
97
|
+
function checkAddon(native) {
|
|
98
|
+
if (addonChecked) return;
|
|
99
|
+
const shipped = readFileSync(new URL("./orchestrator.mjs", import.meta.url), "utf8");
|
|
100
|
+
if (!shipped.endsWith(native.orchestratorSource())) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
"the codemod native addon is from another version than the codemod package; reinstall codemod so both match (in a checkout, rebuild with `cargo build -p codemod-node`)",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
addonChecked = true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Route `console` output of code that runs in a run started with
|
|
110
|
+
* `captureConsole` to the run's logs, by step. Other code is untouched.
|
|
111
|
+
*/
|
|
112
|
+
function captureConsole() {
|
|
113
|
+
const key = Symbol.for("codemod.host.console");
|
|
114
|
+
if (globalThis[key]) return;
|
|
115
|
+
globalThis[key] = true;
|
|
116
|
+
for (const method of ["log", "info", "debug", "warn", "error"]) {
|
|
117
|
+
const original = console[method].bind(console);
|
|
118
|
+
console[method] = (...items) => {
|
|
119
|
+
const store = context.getStore();
|
|
120
|
+
if (!store?.captureConsole) return original(...items);
|
|
121
|
+
store.run.log(store.step?.position ?? null, items.map(formatLogItem).join(" "));
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function formatLogItem(value) {
|
|
127
|
+
if (typeof value === "string") return value;
|
|
128
|
+
if (value instanceof Error) return value.stack || `${value.name}: ${value.message}`;
|
|
129
|
+
try {
|
|
130
|
+
return JSON.stringify(value) ?? String(value);
|
|
131
|
+
} catch {
|
|
132
|
+
return String(value);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Where the workflow is, and how to load it. */
|
|
137
|
+
function locate(workflow, options) {
|
|
138
|
+
if (workflow && typeof workflow === "object" && !isUrl(workflow)) {
|
|
139
|
+
if (!options.path && !options.workflowDir) {
|
|
140
|
+
throw new TypeError(
|
|
141
|
+
"runWorkflow(module, { path }): give the workflow file's path, so relative task paths resolve",
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
// The file need not exist: only its directory matters here.
|
|
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) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function isUrl(value) {
|
|
164
|
+
return value !== null && typeof value === "object" && typeof value.href === "string";
|
|
165
|
+
}
|
|
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
|
+
function inFlight() {
|
|
174
|
+
let count = 0;
|
|
175
|
+
let stall;
|
|
176
|
+
let waiters = [];
|
|
177
|
+
const flight = {
|
|
178
|
+
closed: () => false,
|
|
179
|
+
stalled: new Promise((resolvePromise) => (stall = resolvePromise)),
|
|
180
|
+
begin() {
|
|
181
|
+
count++;
|
|
182
|
+
},
|
|
183
|
+
end() {
|
|
184
|
+
count--;
|
|
185
|
+
flight.check();
|
|
186
|
+
},
|
|
187
|
+
/** Look again once the body has reacted to whatever just happened. */
|
|
188
|
+
check() {
|
|
189
|
+
setImmediate(() => {
|
|
190
|
+
if (count !== 0) return;
|
|
191
|
+
if (flight.closed()) stall(null);
|
|
192
|
+
for (const waiter of waiters.splice(0)) waiter();
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
idle() {
|
|
196
|
+
return new Promise((resolvePromise) => {
|
|
197
|
+
waiters.push(resolvePromise);
|
|
198
|
+
flight.check();
|
|
199
|
+
});
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
return flight;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function host(run, flight, runtime) {
|
|
206
|
+
// Replayed steps settle one per turn of the event loop, so the body reacts
|
|
207
|
+
// to each before the next.
|
|
208
|
+
let replaying = false;
|
|
209
|
+
const replay = () => {
|
|
210
|
+
replaying = runtime().replayNext();
|
|
211
|
+
if (replaying) setImmediate(replay);
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
history: () => run.history(),
|
|
215
|
+
record: (entry, ms) => run.record(JSON.stringify(entry), ms),
|
|
216
|
+
event: (event) => run.event(JSON.stringify(event)),
|
|
217
|
+
canceled: () => run.refusesSteps(),
|
|
218
|
+
now: () => Date.now(),
|
|
219
|
+
advanceClock() {},
|
|
220
|
+
testPlan: () => run.testPlan(),
|
|
221
|
+
stop() {
|
|
222
|
+
run.stop();
|
|
223
|
+
flight.check();
|
|
224
|
+
},
|
|
225
|
+
wake() {
|
|
226
|
+
if (replaying) return;
|
|
227
|
+
replaying = true;
|
|
228
|
+
setImmediate(replay);
|
|
229
|
+
},
|
|
230
|
+
evaluate: (kind, expression, scope) => run.evaluate(kind, expression, JSON.stringify(scope)),
|
|
231
|
+
callbackId,
|
|
232
|
+
async start(request) {
|
|
233
|
+
const { fn, ...task } = request;
|
|
234
|
+
flight.begin();
|
|
235
|
+
try {
|
|
236
|
+
if (fn) return await runInPlace(run, task, fn);
|
|
237
|
+
try {
|
|
238
|
+
return await run.runTask(JSON.stringify(task));
|
|
239
|
+
} catch (error) {
|
|
240
|
+
throw payloadOf(error);
|
|
241
|
+
}
|
|
242
|
+
} finally {
|
|
243
|
+
flight.end();
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** A host task rejected with `{ name, message }` as JSON in the message. */
|
|
250
|
+
function payloadOf(error) {
|
|
251
|
+
try {
|
|
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;
|
|
267
|
+
try {
|
|
268
|
+
value = await context.run({ ...outer, step }, () => fn());
|
|
269
|
+
} finally {
|
|
270
|
+
// What the step measured counts even when it then fails.
|
|
271
|
+
flushMetrics(run, step);
|
|
272
|
+
}
|
|
273
|
+
// A failure reported with failStep() stands even if the code caught it.
|
|
274
|
+
if (step.failure !== undefined) throw new Error(step.failure);
|
|
275
|
+
const result = value === undefined ? null : value;
|
|
276
|
+
checkJson(result, "the step's result");
|
|
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`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function outcome(result, step) {
|
|
284
|
+
const state = stateChanges(step);
|
|
285
|
+
return Object.keys(state).length ? { result, state } : { result };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function flushMetrics(run, step) {
|
|
289
|
+
const metrics = metricsOf(step);
|
|
290
|
+
if (Object.keys(metrics).length) run.addMetrics(JSON.stringify(metrics));
|
|
291
|
+
return metrics;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async function forEachFile(run, outer, task, fn) {
|
|
295
|
+
const { selector } = task.task;
|
|
296
|
+
const { language, files } = await run.selectFiles(JSON.stringify(selector));
|
|
297
|
+
const step = stepScope(task.position, task.label, task.state, language ?? undefined);
|
|
298
|
+
const total = files.length;
|
|
299
|
+
const every = Math.max(1, Math.floor(total / 100));
|
|
300
|
+
let processed = 0;
|
|
301
|
+
const outputs = Array.from({ length: total });
|
|
302
|
+
const failures = [];
|
|
303
|
+
const effects = [];
|
|
304
|
+
const concurrency = selector.concurrency ?? 1;
|
|
305
|
+
|
|
306
|
+
const one = async (index) => {
|
|
307
|
+
const [path, text] = files[index];
|
|
308
|
+
const { file, effects: done, close } = fileHandle(path, text, step.language);
|
|
309
|
+
try {
|
|
310
|
+
step.failure = undefined;
|
|
311
|
+
const output = await context.run({ ...outer, step }, () => fn(file));
|
|
312
|
+
// A failure reported with failFile() stands even if the callback
|
|
313
|
+
// caught it. With files in flight together it cannot be told whose it
|
|
314
|
+
// was, as in the CLI.
|
|
315
|
+
if (step.failure !== undefined && concurrency === 1) {
|
|
316
|
+
failures.push({ path, error: step.failure });
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (output !== undefined) {
|
|
320
|
+
checkJson(output, "return value");
|
|
321
|
+
outputs[index] = { output };
|
|
322
|
+
}
|
|
323
|
+
if (done.length) effects.push({ path, original: text, effects: done });
|
|
324
|
+
} catch (error) {
|
|
325
|
+
failures.push({ path, error: describeError(error) });
|
|
326
|
+
} finally {
|
|
327
|
+
close();
|
|
328
|
+
processed++;
|
|
329
|
+
if (processed === total || processed % every === 0) {
|
|
330
|
+
run.progress(task.position, processed, total);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
let next = 0;
|
|
336
|
+
let sinceTurn = 0;
|
|
337
|
+
const worker = async () => {
|
|
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++);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, total) }, worker));
|
|
350
|
+
|
|
351
|
+
// Before the commit: a commit that fails must not lose what was measured.
|
|
352
|
+
const metrics = flushMetrics(run, step);
|
|
353
|
+
effects.sort((a, b) => (a.path < b.path ? -1 : 1));
|
|
354
|
+
const committed = await run.commitFiles(task.position, task.label, JSON.stringify(effects));
|
|
355
|
+
const allFailures = [...failures, ...committed.failures].sort((a, b) =>
|
|
356
|
+
a.path < b.path ? -1 : a.path > b.path ? 1 : 0,
|
|
357
|
+
);
|
|
358
|
+
return outcome(
|
|
359
|
+
{
|
|
360
|
+
outputs: outputs.filter(Boolean).map((entry) => entry.output),
|
|
361
|
+
changes: committed.changes,
|
|
362
|
+
failures: allFailures,
|
|
363
|
+
metrics,
|
|
364
|
+
},
|
|
365
|
+
step,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// -------------------------------------------------------------- ending ---
|
|
370
|
+
|
|
371
|
+
/** How a body that threw ended. */
|
|
372
|
+
function endingOf(error) {
|
|
373
|
+
if (error instanceof CanceledError || error?.name === "CanceledError") {
|
|
374
|
+
return { status: "canceled" };
|
|
375
|
+
}
|
|
376
|
+
if (error instanceof AwaitingApprovalError || error?.name === "AwaitingApprovalError") {
|
|
377
|
+
return { status: "awaiting-approval", detail: error.message };
|
|
378
|
+
}
|
|
379
|
+
const message =
|
|
380
|
+
error && typeof error === "object" && "message" in error
|
|
381
|
+
? `${error.name ?? "Error"}: ${error.message}`
|
|
382
|
+
: String(error);
|
|
383
|
+
return { status: "failed", detail: message };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** A value as JSON keeps it; what JSON has no form for (a function) is null. */
|
|
387
|
+
function toJson(value) {
|
|
388
|
+
const text = JSON.stringify(value);
|
|
389
|
+
return text === undefined ? null : JSON.parse(text);
|
|
390
|
+
}
|
package/runtime/llm.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// `codemod/llm` is not available when Node.js runs the workflow yet.
|
|
2
|
+
|
|
3
|
+
export function generate() {
|
|
4
|
+
return Promise.reject(
|
|
5
|
+
new Error(
|
|
6
|
+
"codemod/llm is not available when Node.js runs the workflow; run it with the codemod CLI",
|
|
7
|
+
),
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default generate;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// `codemod/metrics` in Node.js and Bun. Metrics belong to the step that
|
|
2
|
+
// records them: they go into a `forEachFile` step's result and the run's
|
|
3
|
+
// report. The workflow body cannot record them, as in the engine.
|
|
4
|
+
|
|
5
|
+
import { context } from "./context.mjs";
|
|
6
|
+
|
|
7
|
+
class MetricAtom {
|
|
8
|
+
constructor(name) {
|
|
9
|
+
this.name = name;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
increment(cardinality = {}, amount = 1) {
|
|
13
|
+
const scope = context.getStore()?.step;
|
|
14
|
+
if (!scope) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`metric '${this.name}': metrics can only be recorded inside a step or forEachFile callback`,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
const clean = {};
|
|
20
|
+
for (const key of Object.keys(cardinality ?? {}).sort()) {
|
|
21
|
+
const value = cardinality[key];
|
|
22
|
+
if (value !== undefined && value !== null) clean[key] = String(value);
|
|
23
|
+
}
|
|
24
|
+
// A whole amount of at least 0; anything that is not a number counts 1.
|
|
25
|
+
amount =
|
|
26
|
+
typeof amount === "number" && Number.isFinite(amount) ? Math.max(0, Math.trunc(amount)) : 1;
|
|
27
|
+
const entryKey = JSON.stringify(clean);
|
|
28
|
+
let entries = scope.metrics.get(this.name);
|
|
29
|
+
if (!entries) scope.metrics.set(this.name, (entries = new Map()));
|
|
30
|
+
const entry = entries.get(entryKey);
|
|
31
|
+
if (entry) entry.count += amount;
|
|
32
|
+
else entries.set(entryKey, { cardinality: clean, count: amount });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getEntries() {
|
|
36
|
+
const entries = context.getStore()?.step?.metrics.get(this.name);
|
|
37
|
+
return entries ? [...entries.values()].map((entry) => ({ ...entry })) : [];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function useMetricAtom(name) {
|
|
42
|
+
return new MetricAtom(name);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { MetricAtom };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The older `codemod:*` spelling of the codemod modules, and this package's
|
|
2
|
+
// Node.js implementations they resolve to. `codemod/workflow` and the rest
|
|
3
|
+
// need no mapping: they are this package's own exports.
|
|
4
|
+
|
|
5
|
+
const MODULES = {
|
|
6
|
+
"codemod:workflow": "./workflow.mjs",
|
|
7
|
+
"codemod:ast-grep": "./ast-grep.mjs",
|
|
8
|
+
"codemod:metrics": "./metrics.mjs",
|
|
9
|
+
"codemod:runtime": "./runtime.mjs",
|
|
10
|
+
"codemod:llm": "./llm.mjs",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const CODEMOD_MODULES = Object.keys(MODULES);
|
|
14
|
+
|
|
15
|
+
/** The URL an older `codemod:*` specifier resolves to, or undefined. */
|
|
16
|
+
export function resolveCodemodModule(specifier) {
|
|
17
|
+
const path = MODULES[specifier];
|
|
18
|
+
return path ? new URL(path, import.meta.url).href : undefined;
|
|
19
|
+
}
|