withruntime 0.33.0 → 0.33.1

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/COMPATIBILITY.md CHANGED
@@ -329,8 +329,8 @@ share the file path and `compat.provider` resource label.
329
329
  ## Evidence that can be repeated locally
330
330
 
331
331
  Run `bun test tests/compat` from the SDK package and run `bun run check-types`.
332
- The test run compiles unchanged typed consumer bodies from
333
- `tests/compat/consumers/workflows.ts.txt` against all ten TypeScript adapters,
332
+ The type check (`tests/compat/consumers/tsconfig.json`) compiles unchanged typed consumer bodies from
333
+ `tests/compat/consumers/workflows.ts` against all ten TypeScript adapters,
334
334
  including E2B's interpreter. Run
335
335
  `bun tests/compat/check-consumers.ts <verified-cache>` to compile the
336
336
  same bodies against the pinned upstream declarations; the script checks each
@@ -8,3 +8,6 @@ import { Runtime, type RuntimeOptions } from "../client.js";
8
8
  export type * from "../products/ci.js";
9
9
  /** `runtime.ci` of a client made with `options`. */
10
10
  export declare function ci(options?: RuntimeOptions): Runtime["ci"];
11
+ /** GitHub Actions workflows and a CircleCI config as a `runtime.yml`, with
12
+ * notes on what was not carried over (`runtime ci import`). */
13
+ export { importCi, type CiImport, type CiSource } from "../ci-import.js";
package/dist/ci/index.js CHANGED
@@ -9,3 +9,6 @@ import { Runtime } from "../client.js";
9
9
  export function ci(options) {
10
10
  return new Runtime(options).ci;
11
11
  }
12
+ /** GitHub Actions workflows and a CircleCI config as a `runtime.yml`, with
13
+ * notes on what was not carried over (`runtime ci import`). */
14
+ export { importCi } from "../ci-import.js";
package/dist/ci-cli.d.ts CHANGED
@@ -5,6 +5,6 @@ type Out = {
5
5
  write: (text: string) => void;
6
6
  error: (text: string) => void;
7
7
  };
8
- export declare const CI_HELP = "runtime ci <command>\n\n CI/CD checks each push and pull request of a connected GitHub repository in\n sandboxes, as the steps its config file names. A fork's pull request waits\n until a writer re-runs it on GitHub. In a private beta: outside it, each\n command says how to ask to join.\n\n connect <owner/name> [--branch main] [--config runtime.yml] [--installation <id>]\n [--trust <app-slug>[bot]]... [--name <name>]\n Connect a repository; prints its id. Takes a full key.\n --trust runs that App's pushes as its own from the start\n repos [ls] [--all] Connected repositories, with the Apps each trusts;\n --all: disconnected ones too\n repos trust <repoId | owner/name> <app-slug>[bot]...\n Run that App's pushes as the repository's own code,\n keeping the Apps it trusts already\n repos untrust <repoId | owner/name> <app-slug>[bot]...\n Stop trusting those Apps, keeping the rest\n disconnect <repoId | owner/name> Stop checking it; its checks still going are cancelled\n ls [--repository <repoId>] [--branch <name>] [--commit <sha>] [--pr <number>]\n [--state <state>]... [--limit 20] Checks, newest first, with their jobs by state\n get <checkId> One check and its jobs in plan order\n logs <jobId> A job's output, its last 256 KiB, and its sandboxes\n tests <repoId> [--quarantined] Its test files: time, recent outcomes, quarantine\n flakes [--repository <repoId>] [--all] Flaky tests: each quarantined (or still gating), its\n owner and 48-hour deadline; --all: ended ones too\n flake <flakeId> One flaky test: its proof, owner, and recent runs\n flake prove <flakeId> Its 100 proof runs again at main's next commit\n flake hand <flakeId> <login | org/team> Hand it to someone else\n flake delete <flakeId> --reason <why> Say why its test is deleted; it ends once main\n no longer holds the file\n\n A failed job's sandbox may be kept paused: `runtime sandboxes wake <id>`, then\n `runtime sandboxes shell <id>` to look around where it failed.\n\nExamples\n runtime ci connect acme/web --config .runtime/ci.yml\n runtime ci repos trust acme/web acme-bot[bot]\n runtime ci ls --commit $(git rev-parse HEAD)\n runtime ci logs <jobId>\n";
8
+ export declare const CI_HELP = "runtime ci <command>\n\n CI/CD checks each push and pull request of a connected GitHub repository in\n sandboxes, as the steps its config file names. A fork's pull request waits\n until a writer re-runs it on GitHub. In a private beta: outside it, each\n command says how to ask to join.\n\n import [dir] [--write] [--force] This folder's GitHub Actions workflows and CircleCI\n config as a runtime.yml: printed, or written with\n --write; what isn't carried over is noted at its top.\n Needs no key and sends nothing\n connect <owner/name> [--branch main] [--config runtime.yml] [--installation <id>]\n [--trust <app-slug>[bot]]... [--name <name>]\n Connect a repository; prints its id. Takes a full key.\n --trust runs that App's pushes as its own from the start\n repos [ls] [--all] Connected repositories, with the Apps each trusts;\n --all: disconnected ones too\n repos trust <repoId | owner/name> <app-slug>[bot]...\n Run that App's pushes as the repository's own code,\n keeping the Apps it trusts already\n repos untrust <repoId | owner/name> <app-slug>[bot]...\n Stop trusting those Apps, keeping the rest\n disconnect <repoId | owner/name> Stop checking it; its checks still going are cancelled\n ls [--repository <repoId>] [--branch <name>] [--commit <sha>] [--pr <number>]\n [--state <state>]... [--limit 20] Checks, newest first, with their jobs by state\n get <checkId> One check and its jobs in plan order\n cancel <checkId> Cancel a queued or running check: its jobs end,\n its machines are let go, GitHub is told\n logs <jobId> A job's output, its last 256 KiB, and its sandboxes\n tests <repoId> [--quarantined] Its test files: time, recent outcomes, quarantine\n flakes [--repository <repoId>] [--all] Flaky tests: each quarantined (or still gating), its\n owner and 48-hour deadline; --all: ended ones too\n flake <flakeId> One flaky test: its proof, owner, and recent runs\n flake prove <flakeId> Its 100 proof runs again at main's next commit\n flake hand <flakeId> <login | org/team> Hand it to someone else\n flake delete <flakeId> --reason <why> Say why its test is deleted; it ends once main\n no longer holds the file\n\n A failed job's sandbox may be kept paused: `runtime sandboxes wake <id>`, then\n `runtime sandboxes shell <id>` to look around where it failed.\n\nExamples\n runtime ci import --write\n runtime ci connect acme/web --config .runtime/ci.yml\n runtime ci repos trust acme/web acme-bot[bot]\n runtime ci ls --commit $(git rev-parse HEAD)\n runtime ci logs <jobId>\n";
9
9
  export declare function ciCommand(argv: string[], out: Out, client: () => Promise<Runtime>, kit: CliKit): Promise<number>;
10
10
  export {};
package/dist/ci-cli.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { importCi } from "./ci-import.js";
1
4
  import { me, named } from "./cli-name.js";
2
5
  import { ci, } from "./products/ci.js";
3
6
  export const CI_HELP = `runtime ci <command>
@@ -7,6 +10,10 @@ export const CI_HELP = `runtime ci <command>
7
10
  until a writer re-runs it on GitHub. In a private beta: outside it, each
8
11
  command says how to ask to join.
9
12
 
13
+ import [dir] [--write] [--force] This folder's GitHub Actions workflows and CircleCI
14
+ config as a runtime.yml: printed, or written with
15
+ --write; what isn't carried over is noted at its top.
16
+ Needs no key and sends nothing
10
17
  connect <owner/name> [--branch main] [--config runtime.yml] [--installation <id>]
11
18
  [--trust <app-slug>[bot]]... [--name <name>]
12
19
  Connect a repository; prints its id. Takes a full key.
@@ -22,6 +29,8 @@ export const CI_HELP = `runtime ci <command>
22
29
  ls [--repository <repoId>] [--branch <name>] [--commit <sha>] [--pr <number>]
23
30
  [--state <state>]... [--limit 20] Checks, newest first, with their jobs by state
24
31
  get <checkId> One check and its jobs in plan order
32
+ cancel <checkId> Cancel a queued or running check: its jobs end,
33
+ its machines are let go, GitHub is told
25
34
  logs <jobId> A job's output, its last 256 KiB, and its sandboxes
26
35
  tests <repoId> [--quarantined] Its test files: time, recent outcomes, quarantine
27
36
  flakes [--repository <repoId>] [--all] Flaky tests: each quarantined (or still gating), its
@@ -36,6 +45,7 @@ export const CI_HELP = `runtime ci <command>
36
45
  \`runtime sandboxes shell <id>\` to look around where it failed.
37
46
 
38
47
  Examples
48
+ runtime ci import --write
39
49
  runtime ci connect acme/web --config .runtime/ci.yml
40
50
  runtime ci repos trust acme/web acme-bot[bot]
41
51
  runtime ci ls --commit $(git rev-parse HEAD)
@@ -64,6 +74,46 @@ const STATES = ["queued", "running", "passed", "failed", "cancelled"];
64
74
  const ownerOf = (f) => f.owner === null
65
75
  ? "nobody found"
66
76
  : `@${f.owner.login}${f.owner.pullRequest === null ? "" : ` (#${f.owner.pullRequest})`}`;
77
+ /** `runtime ci import [dir] [--write] [--force]`: the folder's GitHub Actions
78
+ * workflows and CircleCI config as a `runtime.yml`, printed or written. */
79
+ async function importCommand(argv, out, kit) {
80
+ const args = kit.parse(argv, ["write", "force"], []);
81
+ const root = args.positional[0] ?? ".";
82
+ const files = [];
83
+ const workflows = join(root, ".github", "workflows");
84
+ if (existsSync(workflows))
85
+ for (const name of readdirSync(workflows).sort())
86
+ if (/\.ya?ml$/.test(name))
87
+ files.push({
88
+ path: `.github/workflows/${name}`,
89
+ text: readFileSync(join(workflows, name), "utf8"),
90
+ });
91
+ const circle = join(root, ".circleci", "config.yml");
92
+ if (existsSync(circle))
93
+ files.push({ path: ".circleci/config.yml", text: readFileSync(circle, "utf8") });
94
+ if (!files.length)
95
+ throw kit.usage(`${root} has neither .github/workflows/*.yml nor .circleci/config.yml to import.`);
96
+ const made = importCi(files);
97
+ if (!made.yaml) {
98
+ out.error(made.notes.join("\n"));
99
+ throw kit.usage("No job of these runs on a push or a pull request: nothing to write.");
100
+ }
101
+ if (!kit.has(args, "write")) {
102
+ out.write(out.json ? JSON.stringify({ ...made, read: files.map((f) => f.path) }) : made.yaml.trimEnd());
103
+ return 0;
104
+ }
105
+ const target = join(root, "runtime.yml");
106
+ if (existsSync(target) && !kit.has(args, "force"))
107
+ throw kit.usage(`${target} is there already: --force writes over it.`);
108
+ writeFileSync(target, made.yaml);
109
+ const notes = made.notes.length
110
+ ? `, ${made.notes.length} notes on what was not carried over at its top`
111
+ : "";
112
+ out.write(out.json
113
+ ? JSON.stringify({ ...made, read: files.map((f) => f.path), written: target })
114
+ : `${target}: ${made.steps.length} steps from ${files.length === 1 ? "1 file" : `${files.length} files`}${notes}. Push it, then \`${me} ci connect <owner/name>\`.`);
115
+ return 0;
116
+ }
67
117
  export async function ciCommand(argv, out, client, kit) {
68
118
  const { parse, flag, has, need, table, usage, unknown } = kit;
69
119
  const [verb, ...rest] = argv;
@@ -72,6 +122,9 @@ export async function ciCommand(argv, out, client, kit) {
72
122
  print(named(CI_HELP), { usage: named(CI_HELP) });
73
123
  return 0;
74
124
  }
125
+ // Reads and writes this folder's files only: no key, nothing sent.
126
+ if (verb === "import")
127
+ return importCommand(rest, out, kit);
75
128
  const api = ci((await client()).transport);
76
129
  const whole = (value, what) => {
77
130
  if (value === undefined)
@@ -220,6 +273,11 @@ export async function ciCommand(argv, out, client, kit) {
220
273
  ].join("\n"), check);
221
274
  return 0;
222
275
  }
276
+ case "cancel": {
277
+ const check = await api.checks.cancel(need(rest[0], "a check's id (runtime ci ls)"));
278
+ print(`${check.repository} ${check.commit.slice(0, 12)} ${where(check)}: ${check.state}${check.reason ? ` (${check.reason})` : ""}`, check);
279
+ return 0;
280
+ }
223
281
  case "logs":
224
282
  case "job": {
225
283
  const job = await api.jobs.get(need(rest[0], "a job's id (runtime ci get <checkId>)"));
@@ -323,6 +381,7 @@ export async function ciCommand(argv, out, client, kit) {
323
381
  "disconnect",
324
382
  "ls",
325
383
  "get",
384
+ "cancel",
326
385
  "logs",
327
386
  "tests",
328
387
  "flakes",
@@ -0,0 +1,23 @@
1
+ export type CiSource = {
2
+ path: string;
3
+ text: string;
4
+ };
5
+ export type CiImport = {
6
+ /** The `runtime.yml`, its notes on top as comments; `null` when no job runs on a push or pull request. */
7
+ yaml: string | null;
8
+ /** What was not carried over, or carried differently, one line each. */
9
+ notes: string[];
10
+ /** The steps written, by name. */
11
+ steps: string[];
12
+ };
13
+ /** Every combination of a GitHub `strategy.matrix`, its `exclude` and `include` applied. */
14
+ export declare function matrixCells(matrix: Record<string, unknown> | undefined): Record<string, string>[];
15
+ /** Files a cache key hashes: GitHub's `hashFiles('a', 'b')` or CircleCI's `{{ checksum "a" }}`. */
16
+ export declare function cacheFiles(key: string): string[];
17
+ /** Whether a line installs the project's own dependencies and nothing else:
18
+ * `npm ci` or `pip install -r requirements.txt`, never `npm install pino@9`,
19
+ * which one matrix cell asks and another doesn't. */
20
+ export declare function installsOnly(line: string): boolean;
21
+ /** `runtime.yml` from GitHub Actions workflows (`.github/workflows/*.yml`)
22
+ * and a CircleCI config (`.circleci/config.yml`), each given with its path. */
23
+ export declare function importCi(files: CiSource[]): CiImport;
@@ -0,0 +1,565 @@
1
+ import { parse, stringify } from "yaml";
2
+ /** The most a step's `timeout:` may say, in seconds: GitHub's own 6 hours. */
3
+ const MOST_TIMEOUT_S = 6 * 3600;
4
+ const MOST_STEPS = 64;
5
+ const MOST_CACHE = 32;
6
+ // A project's own install: run once in setup and kept in the warm machine,
7
+ // so a check runs only its tests.
8
+ const INSTALL = /^(npm (ci|install|i)|yarn( install)?|pnpm (install|i)|bun (install|i)|pip3? install|python3? -m pip install|poetry install|uv sync|pipenv install|go mod download|cargo fetch|bundle install|composer install|mix deps\.get)(?=\s|$)/;
9
+ // Actions whose work a Runtime machine already did: the commit is checked
10
+ // out, and Node, Bun, Python, git and a C compiler are on it.
11
+ const DONE = /^actions\/checkout@/;
12
+ const TOOLS = /^(actions\/setup-(node|python|go|java|dotnet)|oven-sh\/setup-bun|pnpm\/action-setup|dtolnay\/rust-toolchain|actions-rs\/toolchain|ruby\/setup-ruby|astral-sh\/setup-uv|denoland\/setup-deno)@/;
13
+ const CACHE = /^actions\/cache(\/restore|\/save)?@/;
14
+ const HAS = "a machine has Node, Bun, Python, git and a C compiler; install anything else in setup";
15
+ const stepName = (raw) => raw
16
+ .replace(/[^A-Za-z0-9 _.:-]+/g, " ")
17
+ .replace(/\s+/g, " ")
18
+ .trim()
19
+ .replace(/^[^A-Za-z0-9]+/, "")
20
+ .slice(0, 63)
21
+ .trim() || "job";
22
+ const envName = (k) => /^[A-Za-z_][A-Za-z0-9_]{0,127}$/.test(k) && !k.startsWith("RUNTIME_");
23
+ const quoteSh = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
24
+ /** A value as one shell word: its literal text single-quoted, each variable
25
+ * `expand` put in (`${RUNTIME_CI_COMMIT}` for `${{ github.sha }}`) in double
26
+ * quotes, so the shell fills it in and nothing else in the value is read. */
27
+ const wordSh = (parts) => parts.map((p) => (typeof p === "string" ? quoteSh(p) : `"\${${p.v}}"`)).join("") || "''";
28
+ const asText = (v) => v === null || v === undefined
29
+ ? ""
30
+ : typeof v === "string"
31
+ ? v
32
+ : typeof v === "number" || typeof v === "boolean"
33
+ ? `${v}`
34
+ : JSON.stringify(v);
35
+ /** An action's name with its pinned ref left off: `actions/cache@<sha>` is `actions/cache`. */
36
+ const action = (uses) => uses.split("@")[0];
37
+ /** Every combination of a GitHub `strategy.matrix`, its `exclude` and `include` applied. */
38
+ export function matrixCells(matrix) {
39
+ if (!matrix || typeof matrix !== "object")
40
+ return [{}];
41
+ const keys = Object.keys(matrix).filter((k) => k !== "include" && k !== "exclude");
42
+ let cells = keys.length ? [{}] : [];
43
+ for (const key of keys) {
44
+ const values = matrix[key];
45
+ if (!Array.isArray(values))
46
+ continue;
47
+ cells = cells.flatMap((c) => values.map((v) => ({ ...c, [key]: asText(v) })));
48
+ }
49
+ const fits = (cell, rule) => Object.entries(rule).every(([k, v]) => cell[k] === v);
50
+ const text = (rule) => Object.fromEntries(Object.entries(rule && typeof rule === "object" ? rule : {}).map(([k, v]) => [k, asText(v)]));
51
+ for (const rule of matrix.exclude ?? [])
52
+ cells = cells.filter((c) => !fits(c, text(rule)));
53
+ for (const rule of matrix.include ?? []) {
54
+ const add = text(rule);
55
+ const base = Object.fromEntries(Object.entries(add).filter(([k]) => keys.includes(k)));
56
+ const matched = Object.keys(base).length ? cells.filter((c) => fits(c, base)) : [];
57
+ if (matched.length)
58
+ for (const c of matched)
59
+ Object.assign(c, add);
60
+ else
61
+ cells.push(add);
62
+ }
63
+ return cells.length ? cells : [{}];
64
+ }
65
+ /** Files a cache key hashes: GitHub's `hashFiles('a', 'b')` or CircleCI's `{{ checksum "a" }}`. */
66
+ export function cacheFiles(key) {
67
+ const out = [];
68
+ for (const m of key.matchAll(/hashFiles\(([^)]*)\)/g))
69
+ for (const f of m[1].matchAll(/['"]([^'"]+)['"]/g))
70
+ out.push(f[1]);
71
+ for (const m of key.matchAll(/checksum\s+"([^"]+)"/g))
72
+ out.push(m[1]);
73
+ return out.filter((f) => !f.includes("{{") && !f.startsWith("/") && !f.startsWith("~"));
74
+ }
75
+ /** Whether a line installs the project's own dependencies and nothing else:
76
+ * `npm ci` or `pip install -r requirements.txt`, never `npm install pino@9`,
77
+ * which one matrix cell asks and another doesn't. */
78
+ export function installsOnly(line) {
79
+ const t = line.trim();
80
+ const m = INSTALL.exec(t);
81
+ if (!m || /[|;&<>$`]/.test(t))
82
+ return false;
83
+ const words = t.slice(m[0].length).trim().split(/\s+/).filter(Boolean);
84
+ for (let i = 0; i < words.length; i++) {
85
+ const w = words[i];
86
+ if (["-r", "--requirement", "-e", "--editable", "-c"].includes(w))
87
+ i++;
88
+ else if (!w.startsWith("-") && w !== ".")
89
+ return false;
90
+ }
91
+ return true;
92
+ }
93
+ class Writer {
94
+ steps = [];
95
+ setup = [];
96
+ cache = new Set();
97
+ notes = [];
98
+ note(where, what) {
99
+ const line = `${where}: ${what}`.replace(/\s+/g, " ");
100
+ if (!this.notes.includes(line))
101
+ this.notes.push(line);
102
+ }
103
+ /** A step's lines, its installs moved to setup when they run there as
104
+ * they did in the step: `movable` (the step names no folder and no env of
105
+ * its own) and no line before it changes folder. Setup runs at the
106
+ * repository's root with no step's env, so `npm ci` under
107
+ * `working-directory: web`, or after `cd web`, stays in its step. */
108
+ lines(script, movable = true) {
109
+ const kept = [];
110
+ let moved = !movable;
111
+ for (const line of script.split("\n")) {
112
+ if (/(^|[;&|(]\s*)(cd|pushd)\b/.test(line.trim()))
113
+ moved = true;
114
+ if (!moved && installsOnly(line)) {
115
+ if (!this.setup.includes(line.trim()))
116
+ this.setup.push(line.trim());
117
+ }
118
+ else
119
+ kept.push(line.replace(/\s+$/, ""));
120
+ }
121
+ while (kept.length && !kept[kept.length - 1].trim())
122
+ kept.pop();
123
+ while (kept.length && !kept[0].trim())
124
+ kept.shift();
125
+ return kept;
126
+ }
127
+ unique(name) {
128
+ let n = stepName(name);
129
+ for (let i = 2; this.steps.some((s) => s.name === n); i++)
130
+ n = stepName(`${name} ${i}`);
131
+ return n;
132
+ }
133
+ }
134
+ /** A job's steps as one script. Each step of GitHub's or CircleCI's ran in a
135
+ * shell of its own, so a `cd` or an `export` in one never reached the next:
136
+ * with more than one, each is a subshell. Undefined when none runs anything. */
137
+ function script(blocks) {
138
+ const runs = blocks.filter((b) => b.some((l) => l.trim() && !l.trim().startsWith("#")));
139
+ if (!runs.length)
140
+ return undefined;
141
+ const lines = blocks.flatMap((b) => runs.length > 1 && runs.includes(b) ? ["(", ...b.map((l) => ` ${l}`), ")"] : b);
142
+ return ["set -e", ...lines].join("\n");
143
+ }
144
+ /** `${{ ... }}` in a GitHub value: matrix cells, the commit and the branch
145
+ * filled in; a secret, or anything else only GitHub knows, leaves the value
146
+ * out (undefined), and `bad` is told what. */
147
+ function expand(value, cell, bad) {
148
+ const parts = expandParts(value, cell, bad);
149
+ return parts?.map((p) => (typeof p === "string" ? p : `\${${p.v}}`)).join("");
150
+ }
151
+ /** `expand`'s value as its literal parts and the variables put in for
152
+ * GitHub's values, for a value written as one shell word (`wordSh`). */
153
+ function expandParts(value, cell, bad) {
154
+ let ok = true;
155
+ const parts = [];
156
+ const text = (t) => {
157
+ if (!t)
158
+ return;
159
+ const last = parts[parts.length - 1];
160
+ if (typeof last === "string")
161
+ parts[parts.length - 1] = last + t;
162
+ else
163
+ parts.push(t);
164
+ };
165
+ let at = 0;
166
+ for (const m of value.matchAll(/\$\{\{([\s\S]*?)\}\}/g)) {
167
+ text(value.slice(at, m.index));
168
+ at = m.index + m[0].length;
169
+ const expr = m[1].trim();
170
+ const c = /^matrix\.([A-Za-z0-9_-]+)$/.exec(expr);
171
+ const e = /^env\.([A-Za-z_][A-Za-z0-9_]*)$/.exec(expr);
172
+ if (c && cell[c[1]] !== undefined)
173
+ text(cell[c[1]]);
174
+ else if (expr === "github.sha")
175
+ parts.push({ v: "RUNTIME_CI_COMMIT" });
176
+ else if (expr === "github.ref_name" || expr === "github.head_ref")
177
+ parts.push({ v: "RUNTIME_CI_BRANCH" });
178
+ else if (expr === "github.workspace")
179
+ parts.push({ v: "PWD" });
180
+ else if (e)
181
+ parts.push({ v: e[1] });
182
+ else {
183
+ const s = /^secrets\.([A-Za-z_][A-Za-z0-9_]*)$/.exec(expr);
184
+ bad(s ? `secret ${s[1]}` : m[0]);
185
+ ok = false;
186
+ }
187
+ }
188
+ text(value.slice(at));
189
+ return ok ? parts : undefined;
190
+ }
191
+ /** A YAML mapping, or an empty one for anything else. */
192
+ const obj = (v) => (v && typeof v === "object" && !Array.isArray(v) ? v : {});
193
+ const list = (v) => (Array.isArray(v) ? v : []);
194
+ /** A key that is one name or a list of them (`needs: a` or `needs: [a, b]`). */
195
+ const names = (v) => (typeof v === "string" ? [v] : list(v).map(asText));
196
+ const runsOnPush = (on) => {
197
+ const events = typeof on === "string" ? [on] : Array.isArray(on) ? on : Object.keys(obj(on));
198
+ return events.some((e) => e === "push" || e === "pull_request" || e === "pull_request_target");
199
+ };
200
+ function read(w, file) {
201
+ try {
202
+ return obj(parse(file.text));
203
+ }
204
+ catch (error) {
205
+ w.note(file.path, `not read: ${error.message.split("\n")[0]}`);
206
+ return undefined;
207
+ }
208
+ }
209
+ function fromActions(w, file) {
210
+ const doc = read(w, file);
211
+ if (!doc?.jobs)
212
+ return;
213
+ if (!runsOnPush(doc.on)) {
214
+ w.note(file.path, "skipped: it runs on neither push nor pull_request");
215
+ return;
216
+ }
217
+ const top = obj(doc.env);
218
+ const named = new Map();
219
+ const made = [];
220
+ for (const [id, raw] of Object.entries(obj(doc.jobs))) {
221
+ const job = obj(raw);
222
+ const where = `${file.path} ${id}`;
223
+ if (job.uses) {
224
+ w.note(where, `skipped: a reusable workflow (${asText(job.uses)}); import that file instead`);
225
+ continue;
226
+ }
227
+ const runsOn = asText(job["runs-on"]);
228
+ if (job.if)
229
+ w.note(where, `its if (${asText(job.if)}) is not carried: it runs on every check`);
230
+ const timeout = jobTimeout(w, where, job["timeout-minutes"]);
231
+ if (job["continue-on-error"])
232
+ w.note(where, "continue-on-error is not carried: a failure fails the check");
233
+ const dir = asText(obj(obj(job.defaults).run)["working-directory"]);
234
+ const cells = matrixCells(obj(obj(job.strategy).matrix));
235
+ const jobSteps = list(job.steps).map(obj);
236
+ // What the kept commands read: a matrix key only a left-out setup
237
+ // action read (a Node version, say) names no step.
238
+ const reads = JSON.stringify([
239
+ job.env,
240
+ job.services,
241
+ jobSteps.map((s) => [s.run, s.env, s["working-directory"]]),
242
+ ]);
243
+ const steps = [];
244
+ const bodies = new Set();
245
+ for (const cell of cells) {
246
+ const os = expand(runsOn, cell, () => { }) ?? runsOn;
247
+ if (/macos|windows/i.test(os)) {
248
+ w.note(where, `${os} is left out: Runtime machines are Linux`);
249
+ continue;
250
+ }
251
+ const bad = (what) => w.note(where, `${what} is not carried; set it in the step's env or a Runtime secret`);
252
+ const env = {};
253
+ for (const [k, v] of Object.entries({ ...top, ...obj(job.env) })) {
254
+ const value = expand(asText(v), cell, bad);
255
+ if (value !== undefined && envName(k))
256
+ env[k] = value;
257
+ }
258
+ const blocks = [];
259
+ for (const [name, raw] of Object.entries(obj(job.services))) {
260
+ const svc = obj(raw);
261
+ const image = expand(asText(typeof raw === "string" ? raw : svc.image), cell, bad);
262
+ if (!image)
263
+ continue;
264
+ const flags = Object.entries(obj(svc.env)).map(([k, v]) => `-e ${k}=${wordSh(expandParts(asText(v), cell, bad) ?? [])}`);
265
+ const ports = list(svc.ports).map(asText);
266
+ const moved = ports.some((p) => {
267
+ const [a, b] = p.split(":");
268
+ return b !== undefined && a !== b;
269
+ });
270
+ const net = moved ? ports.map((p) => `-p ${p}`) : ["--network host"];
271
+ blocks.push([
272
+ ["docker run -d --name", stepName(name).replace(/ /g, "-"), ...net, ...flags, image].join(" "),
273
+ ]);
274
+ w.note(where, `service ${name} starts with Docker in the step; wait for it as its health check did`);
275
+ }
276
+ for (const s of jobSteps) {
277
+ const uses = asText(s.uses);
278
+ if (uses) {
279
+ if (DONE.test(uses))
280
+ continue;
281
+ if (TOOLS.test(uses))
282
+ w.note(where, `${action(uses)} is not needed: ${HAS}`);
283
+ else if (CACHE.test(uses))
284
+ for (const f of cacheFiles(asText(obj(s.with).key)))
285
+ w.cache.add(f);
286
+ else {
287
+ w.note(where, `action ${action(uses)} is not carried; run its commands in the step`);
288
+ blocks.push([`# was: uses: ${action(uses)}`]);
289
+ }
290
+ continue;
291
+ }
292
+ if (!s.run)
293
+ continue;
294
+ const said = asText(s.name ?? s.run).split("\n")[0];
295
+ if (s.if && /failure\(\)|cancelled\(\)/.test(asText(s.if))) {
296
+ w.note(where, `step "${said}" ran only on failure; left out`);
297
+ continue;
298
+ }
299
+ if (s.if)
300
+ w.note(where, `step "${said}": its if (${asText(s.if)}) is not carried`);
301
+ const run = expand(asText(s.run), cell, bad);
302
+ if (run === undefined) {
303
+ blocks.push([`# left out (it needs a secret or a GitHub value): ${said}`]);
304
+ continue;
305
+ }
306
+ const exports = [];
307
+ for (const [k, v] of Object.entries(obj(s.env))) {
308
+ const value = expandParts(asText(v), cell, bad);
309
+ if (value !== undefined && envName(k))
310
+ exports.push(`export ${k}=${wordSh(value)}`);
311
+ }
312
+ const wd = asText(s["working-directory"]) || dir;
313
+ const lines = w.lines(run, !wd && !exports.length);
314
+ if (!lines.length)
315
+ continue;
316
+ blocks.push([...(wd ? [`cd ${quoteSh(wd)}`] : []), ...exports, ...lines]);
317
+ }
318
+ const run = script(blocks);
319
+ if (!run) {
320
+ w.note(where, "has no command left to run here; not written");
321
+ continue;
322
+ }
323
+ // Cells that differ only in what a left-out action read run the same
324
+ // commands: one step runs them.
325
+ const body = JSON.stringify([run, env]);
326
+ if (bodies.has(body)) {
327
+ w.note(where, "matrix cells that differ only in a tool's version run once, on the machine's own");
328
+ continue;
329
+ }
330
+ bodies.add(body);
331
+ const label = Object.entries(cell)
332
+ .filter(([k]) => reads.includes(`matrix.${k}`))
333
+ .map(([, v]) => v)
334
+ .join(" ");
335
+ const title = asText(job.name || id).includes("${{") ? id : asText(job.name || id);
336
+ steps.push({
337
+ name: w.unique(label ? `${title} ${label}` : title),
338
+ run,
339
+ needs: [],
340
+ env,
341
+ ...(timeout ? { timeout } : {}),
342
+ });
343
+ }
344
+ made.push({ steps, needs: names(job.needs) });
345
+ named.set(id, steps.map((s) => s.name));
346
+ }
347
+ for (const m of made)
348
+ for (const s of m.steps) {
349
+ s.needs = m.needs.flatMap((n) => named.get(n) ?? []);
350
+ w.steps.push(s);
351
+ }
352
+ }
353
+ /** A GitHub job's `timeout-minutes` as a step's `timeout:` in seconds;
354
+ * undefined (the default, 6 hours as GitHub's) when it says none, or a value
355
+ * that is not a number, which is noted. */
356
+ function jobTimeout(w, where, raw) {
357
+ if (raw === undefined || raw === null || raw === "")
358
+ return undefined;
359
+ const minutes = Number(raw);
360
+ if (!Number.isFinite(minutes) || minutes <= 0) {
361
+ w.note(where, `timeout-minutes ${asText(raw)} is not carried; the step has the default 6 hours`);
362
+ return undefined;
363
+ }
364
+ const seconds = Math.ceil(minutes * 60);
365
+ if (seconds > MOST_TIMEOUT_S) {
366
+ w.note(where, `asks ${asText(raw)} minutes; a step here runs at most 360, as on GitHub`);
367
+ return MOST_TIMEOUT_S;
368
+ }
369
+ return seconds;
370
+ }
371
+ // CircleCI's resource classes by vCPUs: medium is 2, large 4, xlarge 8, 2xlarge 16.
372
+ const CIRCLE_MACHINES = {
373
+ small: "2cpu",
374
+ medium: "2cpu",
375
+ large: "4cpu",
376
+ xlarge: "8cpu",
377
+ "2xlarge": "16cpu",
378
+ "2xlarge+": "16cpu",
379
+ };
380
+ const KEPT_BY_CHECK = [
381
+ "store_artifacts",
382
+ "store_test_results",
383
+ "persist_to_workspace",
384
+ "attach_workspace",
385
+ ];
386
+ function fromCircle(w, file) {
387
+ const doc = read(w, file);
388
+ if (!doc)
389
+ return;
390
+ const jobs = obj(doc.jobs);
391
+ const commands = obj(doc.commands);
392
+ if (doc.orbs)
393
+ w.note(file.path, `orbs (${Object.keys(obj(doc.orbs)).join(", ")}) are not carried; run their commands in the step`);
394
+ // The jobs each workflow runs, with what each requires; every job when no workflow says.
395
+ const runs = [];
396
+ const workflows = Object.entries(obj(doc.workflows))
397
+ .filter(([k, v]) => k !== "version" && obj(v).jobs)
398
+ .map(([k, v]) => [k, obj(v)]);
399
+ for (const [wf, flow] of workflows) {
400
+ if (flow.triggers) {
401
+ w.note(`${file.path} ${wf}`, "skipped: a scheduled workflow");
402
+ continue;
403
+ }
404
+ for (const entry of list(flow.jobs)) {
405
+ if (typeof entry === "string")
406
+ runs.push({ job: entry, name: entry, requires: [] });
407
+ else
408
+ for (const [job, raw] of Object.entries(obj(entry))) {
409
+ const opts = obj(raw);
410
+ if (opts.type === "approval") {
411
+ w.note(`${file.path} ${job}`, "an approval hold is not carried");
412
+ continue;
413
+ }
414
+ if (opts.matrix)
415
+ w.note(`${file.path} ${job}`, "its matrix is not carried; one step is written");
416
+ runs.push({ job, name: asText(opts.name ?? job), requires: names(opts.requires) });
417
+ }
418
+ }
419
+ }
420
+ if (!workflows.length)
421
+ for (const job of Object.keys(jobs))
422
+ runs.push({ job, name: job, requires: [] });
423
+ const named = new Map();
424
+ const pending = [];
425
+ for (const r of runs) {
426
+ if (named.has(r.name))
427
+ continue;
428
+ const where = `${file.path} ${r.job}`;
429
+ if (!jobs[r.job]) {
430
+ w.note(where, "is an orb's job; not carried");
431
+ continue;
432
+ }
433
+ const job = obj(jobs[r.job]);
434
+ if (job.macos || job.windows) {
435
+ w.note(where, "skipped: Runtime machines are Linux");
436
+ continue;
437
+ }
438
+ const env = {};
439
+ for (const [k, v] of Object.entries(obj(job.environment)))
440
+ if (envName(k))
441
+ env[k] = asText(v);
442
+ const blocks = [];
443
+ const docker = list(job.docker).map(obj);
444
+ if (docker[0]?.image)
445
+ w.note(where, `ran in ${asText(docker[0].image)}; here ${HAS}`);
446
+ for (const svc of docker.slice(1)) {
447
+ const image = asText(svc.image);
448
+ const flags = Object.entries(obj(svc.environment)).map(([k, v]) => `-e ${k}=${quoteSh(asText(v))}`);
449
+ const name = stepName(asText(svc.name) || (image.split(":")[0].split("/").pop() ?? "")).replace(/ /g, "-");
450
+ blocks.push([["docker run -d --name", name, "--network host", ...flags, image].join(" ")]);
451
+ w.note(where, `service ${image} starts with Docker in the step; wait for it before the tests`);
452
+ }
453
+ const dir = asText(job.working_directory);
454
+ const walk = (steps, depth) => {
455
+ for (const raw of steps) {
456
+ const [kind, opts] = typeof raw === "string"
457
+ ? [raw, undefined]
458
+ : (Object.entries(obj(raw))[0] ?? ["", undefined]);
459
+ // The checkout is made; Docker runs on the machine itself.
460
+ if (kind === "checkout" || kind === "setup_remote_docker")
461
+ continue;
462
+ if (kind === "run") {
463
+ const o = obj(opts);
464
+ const cmd = asText(typeof opts === "string" ? opts : o.command);
465
+ if (o.when === "on_fail") {
466
+ w.note(where, `step "${asText(o.name ?? cmd.split("\n")[0])}" ran only on failure; left out`);
467
+ continue;
468
+ }
469
+ const exports = Object.entries(obj(o.environment))
470
+ .filter(([k]) => envName(k))
471
+ .map(([k, v]) => `export ${k}=${quoteSh(asText(v))}`);
472
+ // A step's working_directory is under the job's, which is where
473
+ // the checkout went: here, the repository's root.
474
+ const wd = asText(o.working_directory);
475
+ const cd = wd && !/^[~/$]/.test(wd) && wd !== "." ? [`cd ${quoteSh(wd)}`] : [];
476
+ const lines = w.lines(cmd, !cd.length && !exports.length);
477
+ if (!lines.length)
478
+ continue;
479
+ if (wd && !cd.length && wd !== "." && wd !== dir)
480
+ w.note(where, `working_directory ${wd} is not carried; the step runs at the repository's root`);
481
+ blocks.push([...cd, ...exports, ...lines]);
482
+ }
483
+ else if (kind === "restore_cache" || kind === "save_cache") {
484
+ const o = obj(opts);
485
+ for (const key of [o.key, ...list(o.keys)])
486
+ for (const f of cacheFiles(asText(key)))
487
+ w.cache.add(f);
488
+ }
489
+ else if (commands[kind] && depth < 4) {
490
+ if (opts)
491
+ w.note(where, `command ${kind}'s parameters are not filled in`);
492
+ walk(list(obj(commands[kind]).steps), depth + 1);
493
+ }
494
+ else if (KEPT_BY_CHECK.includes(kind)) {
495
+ w.note(where, `${kind} is not carried: a check keeps each job's log, and a failed job's sandbox`);
496
+ }
497
+ else if (kind) {
498
+ w.note(where, `${kind} (an orb's or unknown step) is not carried; run its commands in the step`);
499
+ blocks.push([`# was: ${kind}`]);
500
+ }
501
+ }
502
+ };
503
+ walk(list(job.steps), 0);
504
+ const run = script(blocks);
505
+ if (!run) {
506
+ w.note(where, "has no command left to run here; not written");
507
+ continue;
508
+ }
509
+ const machine = CIRCLE_MACHINES[asText(job.resource_class).replace(/^.*\./, "")];
510
+ const step = {
511
+ name: w.unique(r.name),
512
+ run,
513
+ needs: [],
514
+ env,
515
+ ...(machine && machine !== "4cpu" ? { machine } : {}),
516
+ };
517
+ named.set(r.name, step.name);
518
+ pending.push({ step, requires: r.requires });
519
+ }
520
+ for (const p of pending) {
521
+ p.step.needs = p.requires.map((n) => named.get(n)).filter((n) => !!n);
522
+ w.steps.push(p.step);
523
+ }
524
+ }
525
+ /** `runtime.yml` from GitHub Actions workflows (`.github/workflows/*.yml`)
526
+ * and a CircleCI config (`.circleci/config.yml`), each given with its path. */
527
+ export function importCi(files) {
528
+ const w = new Writer();
529
+ for (const f of [...files].sort((a, b) => a.path.localeCompare(b.path)))
530
+ if (f.path.includes(".circleci/"))
531
+ fromCircle(w, f);
532
+ else
533
+ fromActions(w, f);
534
+ if (!w.steps.length)
535
+ return { yaml: null, notes: w.notes, steps: [] };
536
+ if (w.steps.length > MOST_STEPS) {
537
+ w.note("runtime.yml", `${w.steps.length} steps; a file holds ${MOST_STEPS}, the rest are left out`);
538
+ w.steps = w.steps.slice(0, MOST_STEPS);
539
+ }
540
+ const cache = [...w.cache].slice(0, MOST_CACHE);
541
+ const doc = {};
542
+ if (w.setup.length)
543
+ doc.setup = w.setup.join("\n");
544
+ if (cache.length)
545
+ doc.cache = cache;
546
+ doc.steps = w.steps.map((s) => ({
547
+ name: s.name,
548
+ run: s.run,
549
+ ...(s.needs.length ? { needs: s.needs } : {}),
550
+ ...(s.machine ? { machine: s.machine } : {}),
551
+ ...(s.timeout ? { timeout: s.timeout } : {}),
552
+ ...(Object.keys(s.env).length ? { env: s.env } : {}),
553
+ }));
554
+ const head = [
555
+ `# Written by \`runtime ci import\` from ${files.map((f) => f.path).join(", ")}.`,
556
+ ...(w.notes.length
557
+ ? ["# Not carried over, or carried differently:", ...w.notes.map((n) => `# - ${n}`)]
558
+ : []),
559
+ ];
560
+ return {
561
+ yaml: `${head.join("\n")}\n${stringify(doc, { lineWidth: 0 })}`,
562
+ notes: w.notes,
563
+ steps: w.steps.map((s) => s.name),
564
+ };
565
+ }
@@ -310,6 +310,9 @@ export declare function ci(t: Transport): {
310
310
  list: (query?: ListCiChecks, options?: RequestOptions) => Promise<Page<CiCheck>>;
311
311
  /** A check with its jobs in plan order. */
312
312
  get: (id: string, options?: RequestOptions) => Promise<CiCheckDetail>;
313
+ /** Cancel a queued or running check: its jobs end and its machines
314
+ * are let go. A check that has ended is answered as it is. */
315
+ cancel: (id: string, options?: RequestOptions) => Promise<CiCheckDetail>;
313
316
  };
314
317
  jobs: {
315
318
  /** A job with the tail of its log and the sandboxes it ran in. */
@@ -73,6 +73,13 @@ export function ci(t) {
73
73
  list: (query = {}, options) => paged("/v1/ci/checks", query, options),
74
74
  /** A check with its jobs in plan order. */
75
75
  get: (id, options) => t.json({ method: "GET", path: at("/v1/ci/checks", id), ...options }),
76
+ /** Cancel a queued or running check: its jobs end and its machines
77
+ * are let go. A check that has ended is answered as it is. */
78
+ cancel: (id, options) => t.json({
79
+ method: "POST",
80
+ path: `${at("/v1/ci/checks", id)}:cancel`,
81
+ ...options,
82
+ }),
76
83
  },
77
84
  jobs: {
78
85
  /** A job with the tail of its log and the sandboxes it ran in. */
@@ -1,5 +1,5 @@
1
1
  import { RuntimeError } from "./errors.js";
2
- export declare const VERSION = "0.33.0";
2
+ export declare const VERSION = "0.33.1";
3
3
  export declare const DEFAULT_BASE_URL = "https://api.withruntime.com";
4
4
  /** Runtime's API as code inside a Runtime sandbox reaches it: the sandbox's
5
5
  * own host sends each request on to DEFAULT_BASE_URL over HTTPS. The API runs
package/dist/transport.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ConnectionError, DELIBERATE, errorFor, NOTHING_RAN, RuntimeError, WAITS_FOR_ROOM, } from "./errors.js";
2
2
  import { describeRoute, envFetch, openWebSocket, untrustedCertificate } from "./proxy.js";
3
3
  import { warningText } from "./product-renames.js";
4
- export const VERSION = "0.33.0";
4
+ export const VERSION = "0.33.1";
5
5
  export const DEFAULT_BASE_URL = "https://api.withruntime.com";
6
6
  /** Runtime's API as code inside a Runtime sandbox reaches it: the sandbox's
7
7
  * own host sends each request on to DEFAULT_BASE_URL over HTTPS. The API runs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "withruntime",
3
- "version": "0.33.0",
3
+ "version": "0.33.1",
4
4
  "license": "Apache-2.0",
5
5
  "mcpName": "com.withruntime/runtime",
6
6
  "type": "module",
@@ -197,8 +197,8 @@
197
197
  },
198
198
  "scripts": {
199
199
  "build": "node ../typescript-config/tsc5.mjs -p tsconfig.build.json",
200
- "check-types": "tsgo --noEmit",
201
- "lint": "oxlint -c ../eslint-config/oxlint.json --ignore-pattern 'dist/**' --ignore-pattern 'tests/fixtures/modal/**'",
200
+ "check-types": "tsgo --noEmit && tsgo -p tests/compat/consumers",
201
+ "lint": "oxlint -c ../eslint-config/oxlint.json --ignore-pattern 'dist/**' --ignore-pattern 'tests/fixtures/modal/**' --ignore-pattern 'tests/compat/consumers/**'",
202
202
  "test": "bun test"
203
203
  },
204
204
  "devDependencies": {