codemod 2.0.0-alpha.3 → 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 CHANGED
@@ -210,7 +210,7 @@ nodes:
210
210
 
211
211
  A codemod package can depend on `codemod` directly: its scripts and `npx codemod` then run that
212
212
  CLI, pinned to your `package.json`, and editors get the matching types for writing codemods and
213
- workflows, the `codemod:*` modules (`codemod/workflow`, `codemod/ast-grep`, `codemod/metrics`, ...)
213
+ workflows, the `codemod:*` modules (`codemod/workflow`, `codemod/ast-grep`, `codemod/runtime`, ...)
214
214
  and the per-language node types:
215
215
 
216
216
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codemod",
3
- "version": "2.0.0-alpha.3",
3
+ "version": "2.0.0-alpha.4",
4
4
  "description": "Codemod platform for semantic code transformations",
5
5
  "keywords": [
6
6
  "ast",
@@ -38,10 +38,6 @@
38
38
  "types": "./types/main.d.ts",
39
39
  "default": "./runtime/ast-grep.mjs"
40
40
  },
41
- "./metrics": {
42
- "types": "./types/metrics.d.ts",
43
- "default": "./runtime/metrics.mjs"
44
- },
45
41
  "./package.json": "./package.json",
46
42
  "./register": {
47
43
  "types": "./register.d.mts",
@@ -96,13 +92,13 @@
96
92
  "typescript": "catalog:"
97
93
  },
98
94
  "optionalDependencies": {
99
- "@codemod.com/cli-darwin-arm64": "2.0.0-alpha.3",
100
- "@codemod.com/cli-darwin-x64": "2.0.0-alpha.3",
101
- "@codemod.com/cli-linux-arm64-gnu": "2.0.0-alpha.3",
102
- "@codemod.com/cli-linux-arm64-musl": "2.0.0-alpha.3",
103
- "@codemod.com/cli-linux-x64-gnu": "2.0.0-alpha.3",
104
- "@codemod.com/cli-linux-x64-musl": "2.0.0-alpha.3",
105
- "@codemod.com/cli-win32-x64-msvc": "2.0.0-alpha.3"
95
+ "@codemod.com/cli-darwin-arm64": "2.0.0-alpha.4",
96
+ "@codemod.com/cli-darwin-x64": "2.0.0-alpha.4",
97
+ "@codemod.com/cli-linux-arm64-gnu": "2.0.0-alpha.4",
98
+ "@codemod.com/cli-linux-arm64-musl": "2.0.0-alpha.4",
99
+ "@codemod.com/cli-linux-x64-gnu": "2.0.0-alpha.4",
100
+ "@codemod.com/cli-linux-x64-musl": "2.0.0-alpha.4",
101
+ "@codemod.com/cli-win32-x64-msvc": "2.0.0-alpha.4"
106
102
  },
107
103
  "engines": {
108
104
  "node": ">= 16.0.0"
package/register.mjs CHANGED
@@ -14,8 +14,8 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
14
14
  };
15
15
  /**
16
16
  * `import "codemod/register"` lets Node.js and Bun import code written with
17
- * the older `codemod:workflow`, `codemod:ast-grep`, `codemod:metrics`,
18
- * `codemod:runtime` and `codemod:llm` spelling: those imports resolve to this
17
+ * the older `codemod:workflow`, `codemod:ast-grep`, `codemod:runtime` and
18
+ * `codemod:llm` spelling: those imports resolve to this
19
19
  * package's `codemod/*` exports. Code that imports `codemod/*` needs none of this.
20
20
  *
21
21
  * @module
@@ -19,7 +19,7 @@ export interface OpenOptions {
19
19
  export type EngineEvent = {
20
20
  readonly event: string;
21
21
  } & Record<string, unknown>;
22
- /** What `Run.finish` returns: `RunReport::to_json`, with metrics and stats. */
22
+ /** What `Run.finish` returns: `RunReport::to_json`, with stats. */
23
23
  export interface NativeReport {
24
24
  runId: string;
25
25
  status: "succeeded" | "failed" | "canceled" | "awaiting-approval" | "stopped";
@@ -35,7 +35,6 @@ export interface NativeReport {
35
35
  error: string | null;
36
36
  given: boolean;
37
37
  }[];
38
- metrics: Record<string, unknown>;
39
38
  stats: {
40
39
  filesModified: number;
41
40
  filesUnmodified: number;
@@ -74,6 +73,14 @@ export interface NativeRun {
74
73
  evaluate(kind: string, expression: string, scope: string): unknown;
75
74
  /** Run a host task (a `StartRequest` as JSON). Rejects with `{ name, message }` as JSON in the message. */
76
75
  runTask(request: string): Promise<Outcome>;
76
+ /** Collect JSSG data without writing edits; only `commit` applies them. */
77
+ prepareJssg(request: string): Promise<{
78
+ readonly data: {
79
+ file: string;
80
+ data: Json;
81
+ }[];
82
+ commit(data: string): Promise<Outcome>;
83
+ }>;
77
84
  /** The files a `forEachFile` selector (as JSON) picks. */
78
85
  selectFiles(selector: string): Promise<{
79
86
  language: string | null;
@@ -87,8 +94,6 @@ export interface NativeRun {
87
94
  error: string;
88
95
  }[];
89
96
  }>;
90
- /** Add metrics a step recorded to the run summary, as JSON. */
91
- addMetrics(metrics: string): void;
92
97
  /** Events since the last call. */
93
98
  takeEvents(): EngineEvent[];
94
99
  /** Record how the run ended, given how its body ended (JSON, or `"null"`). */
@@ -1,27 +1,10 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import type { NativeRun } from "./addon.mts";
3
- import type { Json, Runtime } from "./orchestrator.mts";
4
- /** One count of a metric, for one combination of labels. */
5
- export type MetricEntry = {
6
- cardinality: Record<string, string>;
7
- count: number;
8
- };
3
+ import type { Runtime } from "./orchestrator.mts";
9
4
  /** What a step or `forEachFile` callback sees and leaves behind. */
10
5
  export interface StepScope {
11
6
  position: number;
12
7
  label: string;
13
- /** State as the step sees it. */
14
- state: Map<string, unknown>;
15
- /** The state the step started with, as JSON, by key. */
16
- initial: Map<string, string>;
17
- /** Keys set with `persist: false`: the step's own. */
18
- unpersisted: Set<string>;
19
- /** Keys the step removed. */
20
- removed: Set<string>;
21
- /** Step outputs set here. */
22
- outputs: Map<string, string>;
23
- /** Metrics by name, then by their labels as JSON. */
24
- metrics: Map<string, Map<string, MetricEntry>>;
25
8
  /** A failure reported with `failFile` or `failStep`. */
26
9
  failure: string | undefined;
27
10
  /** `parse()`'s default language. */
@@ -38,12 +21,5 @@ export interface RunScope {
38
21
  export declare const context: AsyncLocalStorage<RunScope>;
39
22
  /** The run the calling code belongs to; throws outside a run. */
40
23
  export declare function current(what: string): RunScope;
41
- /** A fresh step scope, starting from the workflow state the step began with. */
42
- export declare function stepScope(position: number, label: string, state: Record<string, Json> | undefined, language?: string): StepScope;
43
- /**
44
- * The state a step set or removed (`null`), compared with the state it
45
- * started with: what the engine's isolates report for the same code.
46
- */
47
- export declare function stateChanges(scope: StepScope): Record<string, Json>;
48
- /** The metrics a step recorded, as the engine reports them. */
49
- export declare function metricsOf(scope: StepScope): Record<string, MetricEntry[]>;
24
+ /** A fresh step scope. */
25
+ export declare function stepScope(position: number, label: string, language?: string): StepScope;
@@ -17,43 +17,7 @@ export function current(what) {
17
17
  }
18
18
  return store;
19
19
  }
20
- /** A fresh step scope, starting from the workflow state the step began with. */
21
- export function stepScope(position, label, state, language) {
22
- const entries = Object.entries(state ?? {});
23
- return {
24
- position,
25
- label,
26
- state: new Map(entries.map(([key, value]) => [key, structuredClone(value)])),
27
- initial: new Map(entries.map(([key, value]) => [key, JSON.stringify(value)])),
28
- unpersisted: new Set(),
29
- removed: new Set(),
30
- outputs: new Map(),
31
- metrics: new Map(),
32
- failure: undefined,
33
- language,
34
- };
35
- }
36
- /**
37
- * The state a step set or removed (`null`), compared with the state it
38
- * started with: what the engine's isolates report for the same code.
39
- */
40
- export function stateChanges(scope) {
41
- const changes = {};
42
- for (const [key, value] of scope.state) {
43
- if (scope.unpersisted.has(key))
44
- continue;
45
- // What a step sets was made JSON when it was set (`stateValue`).
46
- if (scope.initial.get(key) !== JSON.stringify(value))
47
- changes[key] = value;
48
- }
49
- for (const key of scope.removed)
50
- changes[key] = null;
51
- return changes;
52
- }
53
- /** The metrics a step recorded, as the engine reports them. */
54
- export function metricsOf(scope) {
55
- const data = {};
56
- for (const [name, entries] of scope.metrics)
57
- data[name] = [...entries.values()];
58
- return data;
20
+ /** A fresh step scope. */
21
+ export function stepScope(position, label, language) {
22
+ return { position, label, failure: undefined, language };
59
23
  }
@@ -98,7 +98,6 @@ export interface HostRunReport {
98
98
  /** The `until` step the run stopped after. */
99
99
  readonly stoppedAt: string | null;
100
100
  readonly steps: readonly HostStepReport[];
101
- readonly metrics: Record<string, unknown>;
102
101
  readonly stats: {
103
102
  readonly filesModified: number;
104
103
  readonly filesUnmodified: number;
package/runtime/host.mjs CHANGED
@@ -40,7 +40,7 @@ import "../register.mjs";
40
40
  import { isAbsolute, resolve } from "node:path";
41
41
  import { fileURLToPath, pathToFileURL } from "node:url";
42
42
  import { addon } from "./addon.mjs";
43
- import { context, metricsOf, stateChanges, stepScope } from "./context.mjs";
43
+ import { context, stepScope } from "./context.mjs";
44
44
  import { ORCHESTRATOR_VERSION } from "./orchestrator-version.mjs";
45
45
  import { AwaitingApprovalError, CanceledError, checkJson, createRuntime, describeDuration, describeError, fileHandle, } from "./orchestrator.mjs";
46
46
  import { callbackId } from "./workflow.mjs";
@@ -245,14 +245,12 @@ function host(run, flight, runtime) {
245
245
  const { fn, ...task } = request;
246
246
  flight.begin();
247
247
  try {
248
+ if (fn && task.task.kind === "jssg") {
249
+ return await reduceInPlace(run, task, fn);
250
+ }
248
251
  if (fn)
249
252
  return await within(task.timeoutMs, (limit) => runInPlace(run, task, fn, limit));
250
- try {
251
- return await run.runTask(JSON.stringify(task));
252
- }
253
- catch (error) {
254
- throw payloadOf(error);
255
- }
253
+ return await runNative(run, task);
256
254
  }
257
255
  finally {
258
256
  flight.end();
@@ -260,6 +258,57 @@ function host(run, flight, runtime) {
260
258
  },
261
259
  };
262
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);
295
+ }
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
+ }
311
+ }
263
312
  /** A host task rejected with `{ name, message }` as JSON in the message. */
264
313
  function payloadOf(error) {
265
314
  const message = isRecord(error) && typeof error.message === "string" ? error.message : undefined;
@@ -280,14 +329,25 @@ function payloadOf(error) {
280
329
  * the work to stop where it can, and whatever it still does is discarded.
281
330
  */
282
331
  async function within(ms, work) {
283
- const limit = { expired: false };
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
+ };
284
344
  if (ms === undefined)
285
345
  return work(limit);
286
346
  let timer;
287
347
  const late = new Promise((_, reject) => {
288
348
  timer = setTimeout(() => {
289
349
  limit.expired = true;
290
- reject({ name: "Error", message: `timed out after ${describeDuration(ms)}` });
350
+ reject(timeout(ms));
291
351
  }, ms);
292
352
  });
293
353
  try {
@@ -302,40 +362,23 @@ async function runInPlace(run, task, fn, limit) {
302
362
  if (!outer)
303
363
  throw new Error("a step started outside its run");
304
364
  if (task.task.kind === "function") {
305
- const step = stepScope(task.position, task.label, task.state);
306
- let value;
307
- try {
308
- value = await context.run({ ...outer, step }, () => fn());
309
- }
310
- finally {
311
- // What the step measured counts even when it then fails.
312
- flushMetrics(run, step);
313
- }
365
+ const step = stepScope(task.position, task.label);
366
+ const value = await context.run({ ...outer, step }, () => fn());
314
367
  // A failure reported with failStep() stands even if the code caught it.
315
368
  if (step.failure !== undefined)
316
369
  throw new Error(step.failure);
317
370
  const result = value === undefined ? null : value;
318
371
  checkJson(result, "the step's result");
319
- return outcome(result, step);
372
+ return { result };
320
373
  }
321
374
  if (task.task.kind === "forEachFile")
322
375
  return forEachFile(run, outer, task, fn, limit);
323
376
  throw new Error(`cannot run a ${task.task.kind} task in place`);
324
377
  }
325
- function outcome(result, step) {
326
- const state = stateChanges(step);
327
- return Object.keys(state).length ? { result, state } : { result };
328
- }
329
- function flushMetrics(run, step) {
330
- const metrics = metricsOf(step);
331
- if (Object.keys(metrics).length)
332
- run.addMetrics(JSON.stringify(metrics));
333
- return metrics;
334
- }
335
378
  async function forEachFile(run, outer, task, fn, limit) {
336
379
  const selector = task.task.selector;
337
380
  const { language, files } = await run.selectFiles(JSON.stringify(selector));
338
- const step = stepScope(task.position, task.label, task.state, language ?? undefined);
381
+ const step = stepScope(task.position, task.label, language ?? undefined);
339
382
  const total = files.length;
340
383
  const every = Math.max(1, Math.floor(total / 100));
341
384
  let processed = 0;
@@ -398,8 +441,6 @@ async function forEachFile(run, outer, task, fn, limit) {
398
441
  // Out of time: the step has already failed, and nothing it did is written.
399
442
  if (limit.expired)
400
443
  throw new CanceledError("the step ran out of time");
401
- // Before the commit: a commit that fails must not lose what was measured.
402
- const metrics = flushMetrics(run, step);
403
444
  effects.sort((a, b) => (a.path < b.path ? -1 : 1));
404
445
  const committed = await run.commitFiles(task.position, task.label, JSON.stringify(effects));
405
446
  const allFailures = [...failures, ...committed.failures].sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
@@ -415,12 +456,13 @@ async function forEachFile(run, outer, task, fn, limit) {
415
456
  if (first && allFailures.length === total) {
416
457
  throw new Error(`the callback failed on every file; first error: ${first.path}: ${first.error}`);
417
458
  }
418
- const result = outcome({
419
- outputs: outputs.flatMap((entry) => (entry ? [entry.output] : [])),
420
- changes: committed.changes,
421
- failures: allFailures,
422
- metrics,
423
- }, step);
459
+ const result = {
460
+ result: {
461
+ outputs: outputs.flatMap((entry) => (entry ? [entry.output] : [])),
462
+ changes: committed.changes,
463
+ failures: allFailures,
464
+ },
465
+ };
424
466
  if (!allFailures.length)
425
467
  return result;
426
468
  return {
@@ -5,7 +5,6 @@
5
5
  const MODULES = {
6
6
  "codemod:workflow": "./workflow.mjs",
7
7
  "codemod:ast-grep": "./ast-grep.mjs",
8
- "codemod:metrics": "./metrics.mjs",
9
8
  "codemod:runtime": "./runtime.mjs",
10
9
  "codemod:llm": "./llm.mjs",
11
10
  };
@@ -1,2 +1,2 @@
1
1
  /** Identifies the orchestrator source this package was built from. */
2
- export declare const ORCHESTRATOR_VERSION = "3b77f02d03ab2c51cf3c68c10abfa0efe8889c7299523649b4c2a7209ecca363";
2
+ export declare const ORCHESTRATOR_VERSION = "83617bbc00e2d6a0010c8bae266cd53194abe74202259e77d66edf3936ba1094";
@@ -1,3 +1,3 @@
1
1
  // Generated by scripts/build.mjs from crates/workflow/src/js/orchestrator.ts; do not edit.
2
2
  /** Identifies the orchestrator source this package was built from. */
3
- export const ORCHESTRATOR_VERSION = "3b77f02d03ab2c51cf3c68c10abfa0efe8889c7299523649b4c2a7209ecca363";
3
+ export const ORCHESTRATOR_VERSION = "83617bbc00e2d6a0010c8bae266cd53194abe74202259e77d66edf3936ba1094";
@@ -1,4 +1,4 @@
1
- /** A JSON value: what steps record, tasks are described in, and state holds. */
1
+ /** A JSON value: what steps record and tasks are described in. */
2
2
  export type Json = null | boolean | number | string | Json[] | {
3
3
  [key: string]: Json;
4
4
  };
@@ -16,8 +16,6 @@ export interface StepRecord {
16
16
  key: string;
17
17
  status: StepStatus;
18
18
  result?: Json;
19
- /** State the step set: keys to values, where `null` removes a key. */
20
- state?: Record<string, Json> | null;
21
19
  error?: string | null;
22
20
  /** When a successful step finished, in milliseconds since the epoch. */
23
21
  finishedAt?: number | null;
@@ -41,8 +39,6 @@ export interface StartRequest {
41
39
  position: number;
42
40
  label: string;
43
41
  task: TaskContent;
44
- /** Workflow state when the step was called. */
45
- state: Record<string, Json>;
46
42
  /** The step's function, on hosts that run callbacks in place. */
47
43
  fn?: Callback;
48
44
  /** How long the task may run, from the step's `timeout`. */
@@ -53,8 +49,6 @@ export interface StartRequest {
53
49
  /** What a task resolves to: `TaskOutcome` in `crates/workflow/src/tasks/mod.rs`. */
54
50
  export interface Outcome {
55
51
  result?: Json;
56
- /** State the task set: keys to values, where `null` removes a key. */
57
- state?: Record<string, Json> | null;
58
52
  /** Set when some of the task's files failed: how many, as a message. */
59
53
  partial?: string;
60
54
  }
@@ -147,8 +141,19 @@ export interface ForEachFileTask {
147
141
  export interface HostTask {
148
142
  readonly [TASK]: "host";
149
143
  readonly content: TaskContent;
144
+ /** `jssg(path, { reduce, initial })`: folds each file's data into one result. */
145
+ readonly reducer?: Reducer;
150
146
  }
151
- export type Task = ForEachFileTask | HostTask;
147
+ export interface Reducer {
148
+ callback: Extracted;
149
+ initial: Json;
150
+ }
151
+ export interface DependencyGraphTask {
152
+ readonly [TASK]: "dependencyGraph";
153
+ readonly lockfiles: string[] | undefined;
154
+ readonly callback: Extracted;
155
+ }
156
+ export type Task = ForEachFileTask | DependencyGraphTask | HostTask;
152
157
  /** A length of time: milliseconds, or a string such as "30s" or "10m". */
153
158
  export type Duration = number | string;
154
159
  /** `step(..., options)`: how to run a step. */
@@ -166,6 +171,7 @@ export declare function createTasks(options?: Pick<Host, "callbackId">): {
166
171
  callbackOf: (value: unknown) => Extracted | null;
167
172
  __callback: (id: string, captures: () => Json) => CallbackReference;
168
173
  forEachFile: (selector: unknown, callback?: unknown) => ForEachFileTask | HostTask;
174
+ dependencyGraph: (options: unknown, callback: unknown) => DependencyGraphTask;
169
175
  shell: (command: unknown, ...rest: unknown[]) => HostTask | ShellTag;
170
176
  jssg: (path: string, options?: object) => HostTask;
171
177
  astGrep: (path: string, options?: object) => HostTask;
@@ -191,13 +197,6 @@ export type Runtime = ReturnType<typeof createRuntime>;
191
197
  /** A tag made by `shell(options)`. */
192
198
  type ShellTag = (strings: TemplateStringsArray, ...values: unknown[]) => HostTask;
193
199
  export declare function createRuntime(host: Host): {
194
- getState: (key?: string) => Json | undefined | Readonly<Record<string, Json>>;
195
- setState: (key: string, value: unknown) => void;
196
- unsetState: (key: string) => void;
197
- acquireLock: (_name: string) => () => void;
198
- setStepOutput: (name: string, value: unknown) => void;
199
- getStepOutput: (stepId: string, name: string) => string | null;
200
- getOrSetStepOutput: (stepId: string, name: string, value: unknown) => string;
201
200
  __evaluate: (kind: string, expression: string, scope: unknown) => unknown;
202
201
  step: (...args: unknown[]) => Promise<Json>;
203
202
  sleep: (duration: Duration) => Promise<Json>;
@@ -209,6 +208,7 @@ export declare function createRuntime(host: Host): {
209
208
  isClosed: () => boolean;
210
209
  __callback: (id: string, captures: () => Json) => CallbackReference;
211
210
  forEachFile: (selector: unknown, callback?: unknown) => ForEachFileTask | HostTask;
211
+ dependencyGraph: (options: unknown, callback: unknown) => DependencyGraphTask;
212
212
  shell: (command: unknown, ...rest: unknown[]) => HostTask | ShellTag;
213
213
  jssg: (path: string, options?: object) => HostTask;
214
214
  astGrep: (path: string, options?: object) => HostTask;