Skip to content

Commit d2b3d52

Browse files
fix(scripts): count typecheck workers against the check:changed budget
The typecheck step held one limiter slot but ran up to the full budget of package typechecks itself, so the real total could exceed the limit. Tasks now take weighted slots, typecheck takes one per worker, and queued tasks start in order so a heavy one is never starved. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 09bcea8 commit d2b3d52

3 files changed

Lines changed: 51 additions & 9 deletions

File tree

‎scripts/check-changed.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ const graphql = ["apps/server", "packages/source-control-github"].filter((dir) =
274274

275275
// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone script has no Effect runtime.
276276
const platform = process.platform;
277-
// The typecheck step gets the same budget for its own package runs. macOS
277+
// macOS
278278
// counts its file cache as used, so available memory there would starve a large
279279
// Mac. Elsewhere availableMemory() also honors a container's cgroup limit.
280280
const concurrency =
@@ -285,6 +285,7 @@ const concurrency =
285285
);
286286
const timeoutSeconds = positiveInteger(process.env.CHECK_CHANGED_TIMEOUT) ?? 900;
287287

288+
const typecheckWorkers = Math.min(concurrency, affected.length);
288289
const filters = (names: ReadonlyArray<string>) => names.flatMap((name) => ["--filter", name]);
289290
const steps = [
290291
runPatchCheck && { name: "unused patches", run: checkPatches },
@@ -300,14 +301,16 @@ const steps = [
300301
})),
301302
affected.length > 0 && {
302303
name: `typecheck (${affected.length} packages)`,
304+
// Each parallel package typecheck takes a slot of the step budget.
305+
weight: typecheckWorkers,
303306
// `tsc --noEmit` writes nothing another package reads, so dependency order
304307
// only made dependents wait: --parallel cut a contracts change from 13 s to 9 s.
305308
args: [
306309
"run",
307310
"--cache",
308311
"--parallel",
309312
"--concurrency-limit",
310-
String(concurrency),
313+
String(typecheckWorkers),
311314
...filters(affected),
312315
"typecheck",
313316
],
@@ -376,7 +379,8 @@ const limit = createLimiter(concurrency);
376379
let previousVitest: Promise<unknown> = Promise.resolve();
377380
const results = await Promise.all(
378381
steps.map((step) => {
379-
if (!("args" in step) || step.args[0] !== "test") return limit(() => runStep(step));
382+
const weight = "weight" in step ? step.weight : 1;
383+
if (!("args" in step) || step.args[0] !== "test") return limit(() => runStep(step), weight);
380384
const result = previousVitest.then(() => limit(() => runStep(step)));
381385
previousVitest = result;
382386
return result;

‎scripts/lib/check-changed-steps.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ describe("check:changed concurrency", () => {
5555
gates[3]!.resolve();
5656
await expect(Promise.all(results.slice(1))).resolves.toEqual([1, 2, 3]);
5757
});
58+
59+
it("counts a weighted task's slots and starts nothing ahead of a waiting heavy task", async () => {
60+
const limit = createLimiter(3);
61+
const gates = Array.from({ length: 3 }, () => Promise.withResolvers<void>());
62+
const started: Array<string> = [];
63+
const run = (name: string, gate: number, weight?: number) =>
64+
limit(async () => {
65+
started.push(name);
66+
await gates[gate]!.promise;
67+
}, weight);
68+
const light = run("light", 0);
69+
// Above the limit, so it holds every slot rather than never starting.
70+
const heavy = run("heavy", 1, 5);
71+
const after = run("after", 2);
72+
await Promise.resolve();
73+
expect(started).toEqual(["light"]);
74+
75+
gates[0]!.resolve();
76+
await light;
77+
expect(started).toEqual(["light", "heavy"]);
78+
79+
gates[1]!.resolve();
80+
await heavy;
81+
expect(started).toEqual(["light", "heavy", "after"]);
82+
gates[2]!.resolve();
83+
await after;
84+
});
5885
});
5986

6087
const fakeChild = () =>

‎scripts/lib/check-changed-steps.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,28 @@ export const positiveInteger = (value: string | undefined) => {
1111
export const defaultConcurrency = (cores: number, availableBytes: number) =>
1212
Math.max(1, Math.min(Math.floor(cores / 2), Math.floor(availableBytes / (6 * GiB))));
1313

14+
// A task holds `weight` slots, capped at the limit, so a step that runs its own
15+
// workers counts each one. Tasks start in order: a heavy task waiting for room
16+
// is never overtaken by lighter ones behind it.
1417
export const createLimiter = (concurrency: number) => {
1518
let active = 0;
16-
const queue: Array<() => void> = [];
17-
return async <A>(task: () => Promise<A>): Promise<A> => {
18-
if (active >= concurrency) await new Promise<void>((resolve) => queue.push(resolve));
19-
active++;
19+
const queue: Array<{ slots: number; start: () => void }> = [];
20+
const startQueued = () => {
21+
while (queue.length > 0 && active + queue[0]!.slots <= concurrency) {
22+
const next = queue.shift()!;
23+
active += next.slots;
24+
next.start();
25+
}
26+
};
27+
return async <A>(task: () => Promise<A>, weight = 1): Promise<A> => {
28+
const slots = Math.min(Math.max(weight, 1), concurrency);
29+
if (queue.length === 0 && active + slots <= concurrency) active += slots;
30+
else await new Promise<void>((start) => queue.push({ slots, start }));
2031
try {
2132
return await task();
2233
} finally {
23-
active--;
24-
queue.shift()?.();
34+
active -= slots;
35+
startQueued();
2536
}
2637
};
2738
};

0 commit comments

Comments
 (0)