The best way to control Claude Code, Codex and more from any computer or phone

main
ts 658 lines 27.7 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off - hashes inputs with node:crypto and sizes the budget with node:os.
2 // Runs CI's fast checks for this branch's changes: format and lint of the
3 // changed files, knip, generated GraphQL operations, typecheck of the affected
4 // packages and their dependents, the test files beside the changed files, and
5 // the cross-file checks those miss (agent context budget, React Compiler
6 // coverage of the changed client files).
7 // A dependency change also gets the unused-patch install check below. CI still
8 // runs the full test suites and the jobs listed in the summary.
9 //
10 // vp run check:changed compare against the closest merge base with any remote main
11 // vp run check:changed <ref> compare against the merge base with <ref>
12 // vp run check:changed --concurrency 1 run the steps one at a time
13 //
14 // Uncommitted and untracked files count. Affected packages come from
15 // .github/scripts/ci-changes.cjs, the same mapping CI uses to gate its jobs.
16 // The steps run side by side within a budget derived from the machine's cores
17 // and available memory, and each prints its output when it finishes. A step
18 // running past the timeout is killed with its process group and reported as
19 // TIMEOUT. A rerun on exactly the files of the last passing run prints that
20 // run's summary.
21 import * as NodeCrypto from "node:crypto";
22 import * as NodeOS from "node:os";
23
24 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
25 import * as NodeServices from "@effect/platform-node/NodeServices";
26 import * as HostProcess from "@t3tools/shared/HostProcess";
27 import * as Clock from "effect/Clock";
28 import * as Config from "effect/Config";
29 import * as Console from "effect/Console";
30 import * as Effect from "effect/Effect";
31 import * as FileSystem from "effect/FileSystem";
32 import * as Option from "effect/Option";
33 import * as Path from "effect/Path";
34 import type * as PlatformError from "effect/PlatformError";
35 import * as Schema from "effect/Schema";
36 import * as Semaphore from "effect/Semaphore";
37 import * as Stream from "effect/Stream";
38 import { Argument, Command, Flag } from "effect/cli";
39 import { ChildProcess, ChildProcessSpawner } from "effect/process";
40
41 import { decide, readWorkspace } from "../.github/scripts/ci-changes.cjs";
42 import { vitePlusCommand } from "./check-changed-command.ts";
43
44 const GiB = 1024 ** 3;
45
46 // A typecheck or test step can take several cores and gigabytes of memory, so
47 // allow one step per two cores and per 6 GiB of available memory.
48 export const defaultConcurrency = (cores: number, availableBytes: number) =>
49 Math.max(1, Math.min(Math.floor(cores / 2), Math.floor(availableBytes / (6 * GiB))));
50
51 export type Outcome = "pass" | "FAIL" | "TIMEOUT";
52
53 export interface Step<R> {
54 readonly name: string;
55 readonly run: Effect.Effect<{ readonly ok: boolean; readonly output: string }, never, R>;
56 // Budget slots the step holds: one per worker it runs in parallel.
57 readonly weight?: number;
58 // Vitest runs started together race to rebuild node_modules/.vitest-cache (ENOTEMPTY).
59 readonly serial?: boolean;
60 }
61
62 // Interrupting a step at its timeout closes the scopes of the processes it
63 // spawned, which kills each one's process group.
64 export const runSteps = Effect.fn("checkChanged.runSteps")(function* <R>(
65 steps: ReadonlyArray<Step<R>>,
66 options: { readonly concurrency: number; readonly timeoutSeconds: number },
67 ) {
68 const budget = yield* Semaphore.make(options.concurrency);
69 // Steps claim budget one at a time, in order: Semaphore lets a light request
70 // take free slots ahead of a waiting heavier one, which would delay typecheck.
71 const admission = yield* Semaphore.make(1);
72 const serial = yield* Semaphore.make(1);
73 return yield* Effect.forEach(
74 steps,
75 (step) => {
76 const timed = Effect.gen(function* () {
77 const started = yield* Clock.currentTimeMillis;
78 const result = yield* Effect.timeoutOption(step.run, `${options.timeoutSeconds} seconds`);
79 const seconds = ((yield* Clock.currentTimeMillis) - started) / 1000;
80 const outcome: Outcome = Option.match(result, {
81 onNone: () => "TIMEOUT",
82 onSome: ({ ok }) => (ok ? "pass" : "FAIL"),
83 });
84 const time =
85 outcome === "TIMEOUT"
86 ? `killed after ${options.timeoutSeconds}s`
87 : `${seconds.toFixed(1)}s`;
88 yield* Console.log(`\n> vp ${step.name} ${outcome} ${time}`);
89 const output = Option.getOrUndefined(result)?.output.trimEnd();
90 if (output) yield* Console.log(output);
91 return { name: step.name, outcome, time };
92 });
93 const slots = Math.min(step.weight ?? 1, options.concurrency);
94 const limited = Effect.acquireUseRelease(
95 admission.withPermit(budget.take(slots)),
96 () => timed,
97 () => budget.release(slots),
98 );
99 return step.serial ? serial.withPermit(limited) : limited;
100 },
101 { concurrency: "unbounded" },
102 );
103 });
104
105 const CacheEntry = Schema.fromJsonString(
106 Schema.Struct({ key: Schema.String, summary: Schema.Array(Schema.String) }),
107 );
108 const decodeCacheEntry = Schema.decodeEffect(CacheEntry);
109 const encodeCacheEntry = Schema.encodeEffect(CacheEntry);
110 const decodeRootPackageJson = Schema.decodeEffect(
111 Schema.fromJsonString(
112 Schema.Struct({
113 scripts: Schema.Struct({
114 "knip:check:files": Schema.String,
115 "knip:check:exports": Schema.String,
116 }),
117 }),
118 ),
119 );
120
121 const collectBytes = (stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>) =>
122 Stream.runCollect(stream).pipe(Effect.map((chunks) => Buffer.concat(chunks)));
123
124 // Fails on a nonzero exit: an empty file list from a broken index would
125 // otherwise pass as "no changes". quotePath=false keeps non-ASCII paths as real
126 // paths instead of quoted escapes.
127 export const git = Effect.fn("checkChanged.git")(function* (
128 cwd: string,
129 args: ReadonlyArray<string>,
130 ) {
131 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
132 return yield* Effect.scoped(
133 Effect.gen(function* () {
134 const handle = yield* spawner.spawn(
135 ChildProcess.make("git", ["-c", "core.quotePath=false", ...args], { cwd }),
136 );
137 const [stdout, stderr, exitCode] = yield* Effect.all(
138 [collectBytes(handle.stdout), collectBytes(handle.stderr), handle.exitCode],
139 { concurrency: "unbounded" },
140 );
141 if (exitCode !== 0) {
142 return yield* Effect.fail(
143 `check:changed: git ${args.join(" ")} exited with ${exitCode}: ${stderr.toString().trim()}`,
144 );
145 }
146 return stdout;
147 }),
148 );
149 });
150 export const gitLines = (cwd: string, args: ReadonlyArray<string>) =>
151 git(cwd, args).pipe(Effect.map((stdout) => stdout.toString().split("\n").filter(Boolean)));
152
153 // Ignored files that checks read: generated sources, declarations, and env files.
154 const checkInput = /(^|\/)\.(generated|astro)\/|\.d\.ts$|(^|\/)\.env[^/]*$/;
155 const inNodeModules = /(^|\/)node_modules\//;
156
157 // Pathspecs don't stop git from walking an ignored directory, and on Windows it follows pnpm's
158 // junctions in node_modules until MAX_PATH. `--directory` lists one without entering it.
159 export const ignoredCheckInputs = Effect.fn("checkChanged.ignoredCheckInputs")(function* (
160 root: string,
161 ) {
162 const list = (args: ReadonlyArray<string>) =>
163 git(root, ["ls-files", "-z", ...args]).pipe(
164 Effect.map((stdout) => stdout.toString().split("\0").filter(Boolean)),
165 );
166 const ignored = ["--others", "--ignored", "--exclude-standard"];
167 // `--directory` hides a directory where the index has a file; `-k` lists those.
168 const [listed, replaced] = yield* Effect.all(
169 [list([...ignored, "--directory"]), list(["-k", "--directory"])],
170 { concurrency: 2 },
171 );
172 const entries = [...new Set([...listed, ...replaced])].sort();
173 // git lists an untracked directory whose contents are all ignored together with them;
174 // listing it again would walk its node_modules.
175 const directories = entries.filter(
176 (entry, index) =>
177 entry.endsWith("/") && !inNodeModules.test(entry) && !entries[index + 1]?.startsWith(entry),
178 );
179 const files = entries.filter((entry) => !entry.endsWith("/"));
180 // Calls fit Windows' 32,767-character command line: 16 covers `:(literal)`, quotes, a space.
181 // `.git/` matches nothing; it stops git starting its walk inside a nested repository.
182 const longest = directories.reduce((max, dir) => Math.max(max, dir.length), 0);
183 const size = Math.max(1, Math.floor(30_000 / (longest + 16)));
184 for (let start = 0; start < directories.length; start += size) {
185 const batch = directories.slice(start, start + size).map((dir) => `:(literal)${dir}`);
186 files.push(...(yield* list([...ignored, "--", ":(literal).git/", ...batch])));
187 }
188 // A nested repository is listed by its directory, as the old pathspecs listed it.
189 return files
190 .filter((file) => !inNodeModules.test(file) && (file.endsWith("/") || checkInput.test(file)))
191 .sort();
192 });
193
194 export const comparisonBase = Effect.fn("checkChanged.comparisonBase")(function* (
195 root: string,
196 explicitRef: Option.Option<string>,
197 ) {
198 const candidates = Option.isSome(explicitRef)
199 ? [{ ref: explicitRef.value, rev: explicitRef.value }]
200 : (yield* gitLines(root, ["remote"])).map((name) => ({
201 ref: name + "/main",
202 rev: "refs/remotes/" + name + "/main",
203 }));
204 let closest: { ref: string; base: string; ahead: number } | undefined;
205 for (const { ref, rev } of candidates) {
206 const base = yield* git(root, ["merge-base", "HEAD", rev]).pipe(
207 Effect.map((stdout) => stdout.toString().trim()),
208 Effect.orElseSucceed(() => undefined),
209 );
210 if (!base) continue;
211 const ahead = Number((yield* git(root, ["rev-list", "--count", base + "..HEAD"])).toString());
212 if (!closest || ahead < closest.ahead) closest = { ref, base, ahead };
213 }
214 if (!closest) {
215 const tried = candidates.map(({ ref }) => ref).join(", ") || "a remote main";
216 return yield* Effect.fail(
217 "check:changed: no merge base with " + tried + ". Run git fetch or pass a ref.",
218 );
219 }
220 return closest;
221 });
222
223 // CI's lint and knip also read Astro components' frontmatter.
224 const CODE = /\.(?:[cm]?[jt]sx?|astro)$/;
225 const TEST = /\.test\.[cm]?[jt]sx?$/;
226 // CI runs the .github/scripts tests with `node --test`; vitest passes a failing node:test file.
227 const NODE_TEST = /^\.github[/\\]scripts[/\\]/;
228 export const splitTests = (tests: Iterable<string>) => {
229 const all = [...tests];
230 return {
231 vitestTests: all.filter((file) => !NODE_TEST.test(file)),
232 nodeTests: all.filter((file) => NODE_TEST.test(file)),
233 };
234 };
235 const escapeRegExp = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
236
237 // A flag wins over its environment variable; either must be a positive integer.
238 const override = Effect.fn("checkChanged.override")(function* (
239 flagName: string,
240 flag: Option.Option<number>,
241 envName: string,
242 ) {
243 const [name, value] = Option.isSome(flag)
244 ? [`--${flagName}`, flag]
245 : [envName, yield* Config.option(Config.Int(envName))];
246 if (Option.isSome(value) && value.value < 1) {
247 return yield* Effect.fail(`check:changed: ${name} must be a positive integer.`);
248 }
249 return value;
250 });
251
252 // Tests that read a doc; a changed doc has no test beside it to select them.
253 export const docReaderTests = (files: ReadonlyArray<string>) =>
254 files.includes("docs/user/t3-connect-troubleshooting.md")
255 ? ["apps/server/src/cloud/ConnectReachability.test.ts"]
256 : [];
257
258 const checkChanged = Effect.fn("checkChanged")(function* (input: {
259 readonly ref: Option.Option<string>;
260 readonly concurrency: Option.Option<number>;
261 readonly timeout: Option.Option<number>;
262 }) {
263 const fs = yield* FileSystem.FileSystem;
264 const path = yield* Path.Path;
265 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
266 const root = yield* path.fromFileUrl(new URL("..", import.meta.url));
267 const inRoot = (file: string) => path.join(root, file);
268
269 // macOS counts its file cache as used, so available memory there would
270 // starve a large Mac. Elsewhere availableMemory() honors a cgroup limit.
271 const platform = yield* HostProcess.Platform;
272 const concurrency = Option.getOrElse(
273 yield* override("concurrency", input.concurrency, "CHECK_CHANGED_CONCURRENCY"),
274 () =>
275 defaultConcurrency(
276 NodeOS.availableParallelism(),
277 platform === "darwin" ? NodeOS.totalmem() : process.availableMemory(),
278 ),
279 );
280 const timeoutSeconds = Option.getOrElse(
281 yield* override("timeout", input.timeout, "CHECK_CHANGED_TIMEOUT"),
282 () => 900,
283 );
284
285 const lines = (...args: Array<string>) => gitLines(root, args);
286
287 const { ref, base } = yield* comparisonBase(root, input.ref);
288 // --no-renames lists a rename as its old and new path, as CI does.
289 const changedFiles = lines("diff", "--name-only", "--no-renames", base, "--");
290 const untrackedFiles = lines("ls-files", "--others", "--exclude-standard");
291 const files = [...new Set([...(yield* changedFiles), ...(yield* untrackedFiles)])].sort();
292
293 if (files.length === 0) {
294 yield* Console.log(`check:changed: no changes since ${base.slice(0, 12)} (${ref}).`);
295 return true;
296 }
297 yield* Console.log(`check:changed: ${files.length} files since ${base.slice(0, 12)} (${ref})`);
298
299 // Ignored generated sources, declarations, env files, and install state affect check results.
300 // Include them in the key, but skip large ignored folders that no check reads.
301 // Recompute after a passing run so edits made during it are never cached as passing.
302 const ci = Option.getOrElse(yield* Config.option(Config.String("CI")), () => "");
303 const inputKey = Effect.gen(function* () {
304 const hash = NodeCrypto.createHash("sha256");
305 // CI changes test behavior (vite.config.ts rejects .only under CI).
306 hash.update(`${process.version}\0${base}\0${ci}\0`);
307 hash.update(
308 yield* git(root, [
309 "diff",
310 "--binary",
311 "--no-renames",
312 "--no-ext-diff",
313 "--no-textconv",
314 base,
315 "--",
316 ]),
317 );
318 const ignored = yield* ignoredCheckInputs(root);
319 for (const file of [
320 // Raw bytes of changed files too: .gitattributes normalizes line endings in
321 // the diff, but the formatter reads the file as it is on disk.
322 ...(yield* changedFiles),
323 ...(yield* untrackedFiles),
324 ...ignored,
325 "node_modules/.modules.yaml",
326 "node_modules/.pnpm/lock.yaml",
327 ]) {
328 hash.update(`\0${file}\0`);
329 // Untracked nested repositories are listed as directories.
330 const info = yield* Effect.option(fs.stat(inRoot(file)));
331 if (Option.isSome(info) && info.value.type === "File") {
332 hash.update(yield* fs.readFile(inRoot(file)));
333 }
334 }
335 return hash.digest("hex");
336 });
337 const key = yield* inputKey;
338 const cachePath = inRoot("node_modules/.cache/check-changed.json");
339 // A missing or unreadable cache is a miss, never a failure.
340 const cached = yield* fs
341 .readFileString(cachePath)
342 .pipe(Effect.flatMap(decodeCacheEntry), Effect.option);
343 if (Option.isSome(cached) && cached.value.key === key) {
344 yield* Console.log("Nothing changed since the last passing run.\n");
345 for (const line of cached.value.summary) yield* Console.log(line);
346 return true;
347 }
348
349 const present = yield* Effect.filter(files, (file) => fs.exists(inRoot(file)));
350 const code = present.filter((file) => CODE.test(file));
351 const packages = readWorkspace();
352 const { affected, outputs } = decide(files, packages, false);
353
354 // Changed test files, and the test files beside a changed source file
355 // (foo.ts -> foo.test.ts, foo.browser.test.tsx). Vitest's import-graph
356 // `related` reaches most of a package: one leaf server file pulled in 252 of
357 // 406 server test files and took 7 minutes. CI runs the rest.
358 const tests = new Set(code.filter((file) => TEST.test(file)));
359 for (const file of code) {
360 const dir = path.dirname(file);
361 const prefix = `${path.basename(file).replace(CODE, "")}.`;
362 for (const entry of yield* fs.readDirectory(inRoot(dir))) {
363 if (entry.startsWith(prefix) && TEST.test(entry)) tests.add(path.join(dir, entry));
364 }
365 }
366 for (const test of docReaderTests(files)) tests.add(test);
367 // It reads files other than the ones beside it, so no changed file selects it.
368 if (affected.includes("t3")) tests.add("apps/server/src/provider/AgentContextBudget.test.ts");
369 const { vitestTests, nodeTests } = splitTests(tests);
370
371 // React Compiler coverage compiles every client source file, so run only the
372 // changed files' cases, in one extra step: a -t filter in the main test step
373 // would drop every other test. An app is skipped when the main step already
374 // runs its coverage file unfiltered. File filters mirror each coverage test.
375 const compilerApps = [
376 {
377 test: "apps/web/src/reactCompilerCoverage.test.ts",
378 root: "apps/web/src/",
379 // Test names keep the repo-relative path.
380 name: (file: string) => file,
381 compiled: (file: string) =>
382 /\.tsx?$/.test(file) && !/\.(test|browser)\.tsx?$/.test(file) && !file.endsWith(".gen.ts"),
383 // Web has only per-file cases.
384 always: [],
385 },
386 {
387 test: "apps/mobile/src/reactCompilerCoverage.test.ts",
388 root: "apps/mobile/src/",
389 name: (file: string) => file.slice("apps/mobile/".length),
390 compiled: (file: string) => /\.tsx?$/.test(file) && !/\.(test\.tsx?|d\.ts)$/.test(file),
391 // The Metro, cache, and stale-allowlist cases check more than one file, deletions included.
392 always: ["^(?!.*compiles )"],
393 },
394 ].flatMap(({ test, root: appRoot, name, compiled, always }) => {
395 const changed = files.filter((file) => file.startsWith(appRoot) && compiled(file));
396 if (changed.length === 0 || tests.has(test)) return [];
397 const names = changed
398 .filter((file) => present.includes(file))
399 .map((file) => `compiles ${escapeRegExp(name(file))}$`);
400 const pattern = [...names, ...always];
401 return pattern.length > 0 ? [{ test, names, pattern }] : [];
402 });
403
404 // knip:check cannot be scoped to a package. Any code change can leave a file,
405 // export, or dependency unused (removing the last import of a helper does), so
406 // it runs whenever code or a manifest changes.
407 const runKnip = files.some(
408 (file) => CODE.test(file) || path.basename(file) === "package.json" || file === "knip.jsonc",
409 );
410 // CI runs knip:check as a task that depends on one script per pass. The passes
411 // do not depend on each other, so they run side by side here.
412 const rootPackage = yield* fs
413 .readFileString(inRoot("package.json"))
414 .pipe(Effect.flatMap(decodeRootPackageJson), Effect.orDie);
415 const knipPasses: Array<Array<string>> = [];
416 for (const command of [
417 rootPackage.scripts["knip:check:files"],
418 rootPackage.scripts["knip:check:exports"],
419 ]) {
420 const [bin, ...args] = command.trim().split(/\s+/);
421 if (bin !== "knip" || /["'$\\|;&<>]/.test(command)) {
422 return yield* Effect.fail(`check:changed cannot split knip:check: ${command}`);
423 }
424 knipPasses.push(["exec", "knip", ...args]);
425 }
426
427 // A version bump can leave a patch unused, causing pnpm to reject the install
428 // with ERR_PNPM_UNUSED_PATCH. Check it here as Release Smoke does in CI.
429 const runPatchCheck = files.some(
430 (file) =>
431 file === "pnpm-workspace.yaml" ||
432 file === "pnpm-lock.yaml" ||
433 file === ".pnpmfile.cjs" ||
434 file.startsWith("patches/") ||
435 file.startsWith("apps/mobile/deps/") ||
436 path.basename(file) === "package.json",
437 );
438
439 // Output is piped, so keep the colors a terminal would get.
440 const env = process.stdout.isTTY ? { FORCE_COLOR: "1" } : {};
441 const vp = (args: ReadonlyArray<string>, cwd = root) =>
442 Effect.scoped(
443 Effect.gen(function* () {
444 const [command, commandArgs] = vitePlusCommand(args, platform);
445 const handle = yield* spawner.spawn(
446 ChildProcess.make(command, commandArgs, {
447 cwd,
448 env,
449 extendEnv: true,
450 forceKillAfter: "5 seconds",
451 }),
452 );
453 const [output, exitCode] = yield* Effect.all(
454 [Stream.mkString(Stream.decodeText(handle.all)), handle.exitCode],
455 { concurrency: 2 },
456 );
457 return { ok: exitCode === 0, output };
458 }),
459 ).pipe(Effect.catch((error) => Effect.succeed({ ok: false, output: error.message })));
460
461 // Resolve without the lockfile in a temp copy to preserve the working tree and lockfile.
462 // Copy mobile modules too: they are file: dependencies, not workspace packages.
463 // Cached registry metadata limits this to patches stranded by local changes, not registry drift.
464 const checkPatches = Effect.scoped(
465 Effect.gen(function* () {
466 const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-patch-check-" });
467 const copy = Effect.fnUntraced(function* (relative: string) {
468 const dest = path.join(temp, relative);
469 yield* fs.makeDirectory(path.dirname(dest), { recursive: true });
470 yield* fs.copy(inRoot(relative), dest);
471 });
472 yield* copy("package.json");
473 yield* copy("pnpm-workspace.yaml");
474 for (const pkg of packages) yield* copy(`${pkg.dir}/package.json`);
475 const modulesDir = "apps/mobile/modules";
476 if (yield* fs.exists(inRoot(modulesDir))) {
477 for (const entry of yield* fs.readDirectory(inRoot(modulesDir))) {
478 const manifest = `${modulesDir}/${entry}/package.json`;
479 if (yield* fs.exists(inRoot(manifest))) yield* copy(manifest);
480 }
481 }
482 for (const extra of ["apps/mobile/deps", "patches", ".pnpmfile.cjs"]) {
483 if (yield* fs.exists(inRoot(extra))) yield* copy(extra);
484 }
485 const result = yield* vp(["install", "--lockfile-only", "--ignore-scripts"], temp);
486 if (result.ok || !result.output.includes("ERR_PNPM_UNUSED_PATCH")) return result;
487 return {
488 ok: false,
489 output:
490 `${result.output}\nUnused dependency patches: remove each patch named above from ` +
491 "patchedDependencies in pnpm-workspace.yaml and from patches/, " +
492 "update it to the version the install resolves, " +
493 "or pin the dependency that pulls it in to the patched version.",
494 };
495 }),
496 ).pipe(Effect.catch((error) => Effect.succeed({ ok: false, output: error.message })));
497
498 // CI's Lint job checks that generated GraphQL operations match their sources.
499 const graphql = [
500 "apps/server",
501 "packages/issue-tracker-linear",
502 "packages/source-control-github",
503 ].filter((dir) => files.some((file) => file.startsWith(`${dir}/`)));
504 const filters = (names: ReadonlyArray<string>) => names.flatMap((name) => ["--filter", name]);
505 const typecheckWorkers = Math.min(concurrency, affected.length);
506 const command = (name: string, args: ReadonlyArray<string>) => ({ name, run: vp(args) });
507 const compilerFiles = compilerApps.reduce((count, app) => count + app.names.length, 0);
508
509 const steps: Array<Step<never>> = [
510 ...(runPatchCheck ? [{ name: "unused patches", run: checkPatches }] : []),
511 ...(present.length > 0
512 ? [command("format", ["fmt", "--check", "--no-error-on-unmatched-pattern", ...present])]
513 : []),
514 ...(code.length > 0
515 ? [command("lint", ["lint", "--no-error-on-unmatched-pattern", ...code])]
516 : []),
517 ...(runKnip
518 ? knipPasses.map((args, index) => command(`knip ${index + 1}/${knipPasses.length}`, args))
519 : []),
520 ...graphql.map((dir) =>
521 command(`graphql (${dir})`, ["run", "--filter", `./${dir}`, "generate", "--check"]),
522 ),
523 ...(affected.length > 0
524 ? [
525 {
526 // `effect-tsgo tsrs --noEmit` writes nothing another package reads, so dependency order
527 // only made dependents wait: --parallel cut a contracts change from 13 s to 9 s.
528 ...command(`typecheck (${affected.length} packages)`, [
529 "run",
530 "--cache",
531 "--parallel",
532 "--concurrency-limit",
533 String(typecheckWorkers),
534 ...filters(affected),
535 "typecheck",
536 ]),
537 weight: typecheckWorkers,
538 },
539 ]
540 : []),
541 // From the repo root, the root vite.config.ts hands each file to its package's config.
542 ...(vitestTests.length > 0
543 ? [
544 {
545 ...command(`test (${vitestTests.length} files)`, [
546 "test",
547 "run",
548 "--passWithNoTests",
549 ...vitestTests,
550 ]),
551 serial: true,
552 },
553 ]
554 : []),
555 ...(nodeTests.length > 0
556 ? [
557 command(`node --test (${nodeTests.length} files)`, [
558 "exec",
559 "node",
560 "--test",
561 ...nodeTests,
562 ]),
563 ]
564 : []),
565 ...(compilerApps.length > 0
566 ? [
567 {
568 ...command(`react compiler (${compilerFiles} files)`, [
569 "test",
570 "run",
571 ...compilerApps.map((app) => app.test),
572 "-t",
573 compilerApps.flatMap((app) => app.pattern).join("|"),
574 ]),
575 serial: true,
576 },
577 ]
578 : []),
579 ];
580
581 const results = yield* runSteps(steps, { concurrency, timeoutSeconds });
582
583 const summary = ["check:changed summary"];
584 for (const result of results) {
585 summary.push(` ${result.outcome} ${result.name} ${result.time}`);
586 }
587 if (results.some((result) => result.outcome === "TIMEOUT")) {
588 summary.push(
589 ` TIMEOUT: ran past ${timeoutSeconds}s and was killed; this is not a check failure. ` +
590 "Raise --timeout or lower --concurrency.",
591 );
592 }
593 const ciOnly = Object.entries({
594 "full test suites": outputs.test || outputs.test_web || outputs.test_server,
595 build: outputs.build,
596 rust: outputs.rust,
597 "mobile native lint": outputs.mobile_native,
598 "release smoke": outputs.release_smoke,
599 }).flatMap(([job, runs]) => (runs ? [job] : []));
600 if (ciOnly.length > 0) summary.push(` CI also runs: ${ciOnly.join(", ")}`);
601 if (!runPatchCheck) summary.push(" unused patch check skipped: no dependency files changed");
602 if (!runKnip) summary.push(" knip skipped: no code or manifest changed");
603 yield* Console.log(`\n${summary.join("\n")}`);
604
605 if (results.some((result) => result.outcome !== "pass")) return false;
606 if ((yield* inputKey) !== key) {
607 yield* Console.log(" pass not cached: files changed during the run");
608 return true;
609 }
610 // Write then rename, so a concurrent run never reads half a file.
611 const temp = `${cachePath}.${process.pid}`;
612 yield* Effect.gen(function* () {
613 yield* fs.makeDirectory(path.dirname(cachePath), { recursive: true });
614 yield* fs.writeFileString(temp, yield* encodeCacheEntry({ key, summary }));
615 yield* fs.rename(temp, cachePath);
616 }).pipe(Effect.catch((error) => Console.warn(` pass not cached: ${error.message}`)));
617 return true;
618 });
619
620 export const checkChangedCommand = Command.make(
621 "check-changed",
622 {
623 ref: Argument.String("ref").pipe(
624 Argument.withDescription("Compare against the merge base with this ref."),
625 Argument.optional,
626 ),
627 concurrency: Flag.Int("concurrency").pipe(
628 Flag.withDescription(
629 "Budget of parallel steps and typecheck workers; also CHECK_CHANGED_CONCURRENCY. Defaults to one per two cores and per 6 GiB of available memory.",
630 ),
631 Flag.optional,
632 ),
633 timeout: Flag.Int("timeout").pipe(
634 Flag.withDescription(
635 "Seconds before a step is killed; also CHECK_CHANGED_TIMEOUT. Defaults to 900.",
636 ),
637 Flag.optional,
638 ),
639 },
640 (input) =>
641 checkChanged(input).pipe(
642 Effect.catch((message) => Console.error(message).pipe(Effect.as(false))),
643 // exitCode, not a failed exit: runMain calls process.exit() on failure, and
644 // piped stdout is asynchronous on macOS, so it would cut off step output.
645 Effect.flatMap((ok) =>
646 Effect.sync(() => {
647 if (!ok) process.exitCode = 1;
648 }),
649 ),
650 ),
651 ).pipe(Command.withDescription("Run CI's fast checks for this branch's changes."));
652
653 if (import.meta.main) {
654 Command.run(checkChangedCommand, { version: "0.0.0" }).pipe(
655 Effect.provide(NodeServices.layer),
656 NodeRuntime.runMain,
657 );
658 }