Skip to content

Commit c748e7d

Browse files
fix(scripts): tree-scaled git listings in two gate scripts read past the 1 MiB default buffer (#39027)
Fixes #39025 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:用户无感;仓库的两个检查脚本读取文件清单时不再受 1 MiB 输出上限限制。 - 为什么做:合并队列里任何新增文件的 PR(几乎都带 changeset)都会让检查崩溃、被踢出队列,这次替全车队解开。 - 出错会怎样:只影响检查脚本自身,不发布、不改任何检查的结论,可直接撤回。 - 要你做什么:无。 ## What changes Two repo scripts read a git listing that grows with the tree into node's 1 MiB default stdout buffer (`maxBuffer`). Past it the child is killed with `ENOBUFS` and the gate dies instead of answering. Both now pass an explicit 64 MiB ceiling, the value and constant name `scripts/pm/dispatch-gates.mjs` already uses (`GIT_MAX_BUFFER_BYTES`). There is no shared git-buffer helper in the repo (`scripts/git-env.mjs` exports none), so each file declares its own constant. Importing the constant from dispatch-gates would make an ordinary gate depend on a governed file. | Site | Listing | Bytes at `444dce61f2` | Fix | |:---|:---|---:|:---| | `scripts/check-closed-ceremony-text.mjs` `trackedFiles()` | `git ls-files -z -- packages content/docs .changeset` | **1,048,565** (11 under 1,048,576) | `maxBuffer: GIT_MAX_BUFFER_BYTES` (64 MiB) | | `scripts/docs-audit/affected-docs.mjs` `sh()` | `git ls-files packages` (in `--self-test`, i.e. `pnpm check:docs-audit-scope`) | **953,424** (91% of the default) | `maxBuffer: SH_MAX_BUFFER_BYTES` (64 MiB) on the wrapper | `sh()` is one wrapper with several callers, so the ceiling goes on the wrapper itself. Its `git show REF:FILE` callers catch the throw, so a file over 1 MiB used to read as absent with no error. The constant sits in the file's hoisted-constants block, because a `const` beside `sh()` is in the temporal dead zone when `--self-test` runs. Behaviour is otherwise unchanged. On this tree both listings already fit, so no verdict moves. ### The sweep: every other git spawn under `scripts/` with no `maxBuffer` I found these with a scanner that reads each `execFileSync` / `spawnSync` / `execSync` / `spawn` of `git` to its closing parenthesis, so multi-line calls count. I then checked every wrapper (`git()`, `run()`, `sh()`, `runGit()`) that is called with `ls-files`, `ls-tree -r`, `diff --name-only` or `grep`. 29 direct `execFileSync('git', ['ls-files'…` sites exist; all the others already pass a ceiling of 16 MiB or more. I left these alone, with the reason: - **Fixture or temp-repo listings (self-test victims):** `git-env.mjs` 322/344/350/369/372, `check-nul-bytes.mjs:1067`, `check-system-context-census.mjs:2463`, `check-bash32-floor.mjs:1414`, `check-gitlink-declared.mjs:424`, `overlap-check.mjs:429`, `tenant-audit-census-replay.mjs:148`. - **Small, fixed-path listings, measured at `444dce61f2`:** - `changeset-changelog.mjs:175` (`*CHANGELOG.md`): 5,078 bytes - `pm/release-rehearsal-clone.mjs:144` (`.changeset/*.md`): 78,763 bytes - `pm/check-prior-rulings.mjs:2179` (`docs/adr/`): 9,243 bytes - `pm/write-pace.mjs:1822` (`scripts/pm`): 1,936 bytes - `check-regen-pending.mjs:1558` (`package.json`): 13 bytes - `git-merge-regen.mjs:1093`: 0 bytes for each of the 6 declared-untracked paths - **Diff-scaled, not tree-scaled:** the output grows with one change set, not with the tree. - `check-test-source-alias.mjs:4103-4104` - `checklist-select.mjs:542` - `pm/check-harness-current.mjs:124` - `pm/git-history.mjs:662` - `pm/overlap-check.mjs:148/160` - `gen-upgrade-harness-fixture.mjs:104` For scale: the whole-tree `git ls-files -z` is 1,902,552 bytes and `ls-files --stage -z` is 3,377,652 bytes. Every gate that reads either one already passes a ceiling of 64 MiB or more. ## Tests - `node scripts/check-closed-ceremony-text.mjs --self-test`: **38 assertion(s) passed**. The new cases stage 16,000 paths under `packages/` into an index-only fixture repo (`update-index --index-info`, about 1.3 MB of listing). The CONTROL reads that listing at the default buffer and must die with `ENOBUFS`. `trackedFiles()` must then read every path, including the first and last, and the ceiling must stay finite and above the fixture. - `node scripts/docs-audit/affected-docs.mjs --self-test`: **607 cases pass**. The new cases use a child that prints 2 MiB. The CONTROL is `execSync` at the default buffer and must answer `ENOBUFS`; `sh()` must then return all 2,097,152 bytes. <details><summary>Ablation (maxBuffer removed on disk, then restored from HEAD)</summary> ```text check-closed-ceremony-text, the trackedFiles maxBuffer line deleted: ✗ trackedFiles reads the whole listing instead of dying with ENOBUFS ✗ …first and last path included check-closed-ceremony-text --self-test: 2 of 38 assertion(s) FAILED. exit 1 affected-docs, the sh() maxBuffer option deleted: ✗ self-test "sh() reads stdout past the 1 MiB default whole instead of dying with ENOBUFS": sh(2 MiB) → expected sh=2097152, got "ENOBUFS" ✗ affected-docs self-test failed (1 case(s)). exit 1 ``` </details> ## Acceptance notes - Changeset: none. Neither file ships in any package's `files[]`, and the root package is `private`. This PR carries `skip-changeset`, which is the convention `check-empty-changeset.mjs` names. - `check-governed-merges --branch` reports 0 of 2 paths governed, 95 changed lines: ordinary queue landing. - Not this card: the `git show REF:FILE` readers in `affected-docs.mjs` turn every failure into "absent", not only `ENOBUFS`. This PR does not change that shape; it only stops a large file from tripping it. ## Verification Local subset at `4b802eaf2b` (`dispatch-gates --commands`, 36 families derived from this diff): **35 exit 0, 1 NOT MEASURED**. `--ran` reconciliation: 36 derived, 35 run, 1 NOT-MEASURED, 0 UNRUN. - `pnpm check:closed-ceremony-text`: exit 0, `6622 shipped file(s) read; none says the ownership ceremony is closed.` - `pnpm check:docs-audit-scope`: exit 0 - `pnpm check:nul-bytes`: exit 0 - `pnpm check:entry-guard`: exit 0 - `pnpm check:scripts-symbol-anchors`: exit 0 - `node scripts/check-self-test-wired.mjs`: exit 0 - NOT MEASURED: `pnpm check:pm-dispatch-gates`, reason: it exited 124 when it hit the local 280 s per-command timeout on the shared container. It does not read either changed file, so CI owns the verdict. Every other family, including the full Lint & Repo Gates farm, is CI's to run on this head. Two-minute check: `node scripts/check-closed-ceremony-text.mjs --self-test` prints `38 assertion(s) passed`. Delete the `maxBuffer: GIT_MAX_BUFFER_BYTES,` line and the same command prints `2 of 38 assertion(s) FAILED`. --- _Generated by [Claude Code](https://claude.ai/code/session_01QSeKYnrQ7UMtHofKQVjcH2)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 444dce6 commit c748e7d

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

‎scripts/check-closed-ceremony-text.mjs‎

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
* every `CHANGELOG.md` (release-owned).
3232
*/
3333

34-
import { readFileSync } from 'node:fs';
35-
import { execFileSync } from 'node:child_process';
34+
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
35+
import { execFileSync, spawnSync } from 'node:child_process';
36+
import { tmpdir } from 'node:os';
3637
import { join } from 'node:path';
3738
import { fileURLToPath } from 'node:url';
3839
import process from 'node:process';
40+
import { gitFreeEnv } from './git-env.mjs';
3941
import { isEntrypoint } from './invoked-as.mjs';
4042

4143
const ROOT = join(fileURLToPath(new URL('.', import.meta.url)), '..');
@@ -78,8 +80,29 @@ export function findingsIn(text) {
7880
return out;
7981
}
8082

81-
function trackedFiles() {
82-
return execFileSync('git', ['ls-files', '-z', '--', ...ROOT_DIR_WATCH_HINTS.map((h) => h.replace(/\/\*\*$/, ''))], { cwd: ROOT, encoding: 'utf8' })
83+
/**
84+
* The stdout ceiling the listing below is spawned with — the value and name
85+
* `scripts/pm/dispatch-gates.mjs` gave its own git reads.
86+
*
87+
* `execFileSync`'s default is 1 MiB; past it node kills the child and throws
88+
* `ENOBUFS`, so the gate dies instead of answering. This population's listing
89+
* grows with every path added under `packages/`, `content/docs/` and
90+
* `.changeset/`, and it reached the default: one added changeset put it over
91+
* and red-flagged every PR that carried one. 64 MiB is headroom for a listing
92+
* many times this tree's; the bound stays finite so a runaway child is still
93+
* stopped. Raising it changes no verdict on a tree that already fit — only
94+
* whether a larger tree gets one.
95+
*/
96+
export const GIT_MAX_BUFFER_BYTES = 64 * 1024 * 1024;
97+
98+
/** The tracked paths under the walk roots, read NUL-separated. `cwd`/`env` exist for the self-test's fixture index. */
99+
export function trackedFiles({ cwd = ROOT, env = process.env } = {}) {
100+
return execFileSync('git', ['ls-files', '-z', '--', ...ROOT_DIR_WATCH_HINTS.map((h) => h.replace(/\/\*\*$/, ''))], {
101+
cwd,
102+
env,
103+
encoding: 'utf8',
104+
maxBuffer: GIT_MAX_BUFFER_BYTES,
105+
})
83106
.split('\0')
84107
.filter(Boolean);
85108
}
@@ -178,6 +201,44 @@ function selfTest() {
178201
// The tree: a non-empty population.
179202
t('the population on this tree is not empty', sweep().scanned > 0);
180203

204+
// The listing past spawn's default 1 MiB stdout buffer. The fixture is an
205+
// index alone — `update-index --index-info` stages paths with no files on
206+
// disk — sized past 1 MiB, under a walk root so the pathspec keeps it. The
207+
// CONTROL reads the same listing with the default buffer and must die with
208+
// ENOBUFS, so the pass below cannot come from a fixture that never reached
209+
// the size.
210+
const bufTmp = mkdtempSync(join(tmpdir(), 'closed-ceremony-maxbuffer-'));
211+
try {
212+
const env = gitFreeEnv();
213+
const init = spawnSync('git', ['init', '-q', '.'], { cwd: bufTmp, env, encoding: 'utf8' });
214+
t('fixture: git init', init.status === 0);
215+
const EMPTY_BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
216+
const paths = Array.from({ length: 16000 }, (_, i) =>
217+
`packages/fixture-${String(i).padStart(5, '0')}/src/a-long-enough-module-name-for-a-wide-listing.ts`);
218+
const staged = spawnSync('git', ['update-index', '--index-info'], {
219+
cwd: bufTmp,
220+
env,
221+
encoding: 'utf8',
222+
input: paths.map((p) => `100644 ${EMPTY_BLOB} 0\t${p}`).join('\n') + '\n',
223+
});
224+
t('fixture: git update-index', staged.status === 0);
225+
const listingBytes = paths.reduce((n, p) => n + Buffer.byteLength(p) + 1, 0);
226+
t('the fixture listing is past the 1 MiB default buffer', listingBytes > 1024 * 1024);
227+
const atDefault = spawnSync('git', ['ls-files', '-z', '--', 'packages'], { cwd: bufTmp, env, encoding: 'utf8' });
228+
t('CONTROL: the same listing read with the default buffer dies with ENOBUFS', atDefault.error?.code === 'ENOBUFS');
229+
let listed = null;
230+
try {
231+
listed = trackedFiles({ cwd: bufTmp, env });
232+
} catch {
233+
listed = null;
234+
}
235+
t('trackedFiles reads the whole listing instead of dying with ENOBUFS', listed?.length === paths.length);
236+
t('…first and last path included', listed?.[0] === paths[0] && listed?.at(-1) === paths.at(-1));
237+
t('the ceiling stays finite and above this fixture', Number.isFinite(GIT_MAX_BUFFER_BYTES) && GIT_MAX_BUFFER_BYTES > listingBytes);
238+
} finally {
239+
rmSync(bufTmp, { recursive: true, force: true });
240+
}
241+
181242
if (failures.length) {
182243
for (const f of failures) console.error(` ✗ ${f}`);
183244
console.error(`check-closed-ceremony-text --self-test: ${failures.length} of ${checked} assertion(s) FAILED.`);

‎scripts/docs-audit/affected-docs.mjs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ let diffBaseRef = null;
288288
// down beside their functions makes `--self-test` die in the temporal dead zone. The
289289
// functions themselves can stay where they read best — those ARE hoisted.
290290

291+
/**
292+
* The stdout ceiling every `sh()` child is spawned with — the value and name
293+
* `scripts/pm/dispatch-gates.mjs` gave its own git reads. `execSync`'s default
294+
* is 1 MiB, and past it node kills the child and throws `ENOBUFS`: the
295+
* self-test's `git ls-files packages` grows with the tree and sits within a
296+
* tenth of that default, and every `git show` caller below catches the throw
297+
* and reads a file over 1 MiB as absent. Finite, so a runaway child is still
298+
* stopped; raising it changes no answer for output that already fit.
299+
*/
300+
const SH_MAX_BUFFER_BYTES = 64 * 1024 * 1024;
301+
291302
/**
292303
* Release-owned pages — AGENTS.md's Documentation Guardrails forbid a code PR from
293304
* editing anything under this prefix, and `.claude/workflows/docs-accuracy-audit.js`
@@ -1063,7 +1074,7 @@ if (args.includes('--bridge-coverage')) {
10631074
}
10641075

10651076
function sh(cmd) {
1066-
return execSync(cmd, { cwd: repoRoot, stdio: ['ignore', 'pipe', 'ignore'] }).toString();
1077+
return execSync(cmd, { cwd: repoRoot, stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: SH_MAX_BUFFER_BYTES }).toString();
10671078
}
10681079

10691080
// --- 1. enumerate hand-written docs ----------------------------------------
@@ -5864,6 +5875,19 @@ function selfTest() {
58645875
.filter((f) => /(^|\/)\.[^/]+\//.test(f) && f.endsWith('.ts'));
58655876
check('walkSourceFiles', 'no TRACKED .ts source under packages/** lives in a dot-directory — the premise the prune rests on',
58665877
'git ls-files packages, filtered to .ts under dot-directories', 0, trackedDotDirFiles.length);
5878+
5879+
// (5) THE `sh()` STDOUT CEILING. The listing above grows with the tree toward
5880+
// execSync's 1 MiB default, past which the child dies with ENOBUFS. A synthetic
5881+
// child prints 2 MiB; the CONTROL reads it at the default buffer and must die, so
5882+
// the pass cannot come from output that never reached the size.
5883+
const overDefault = 2 * 1024 * 1024;
5884+
const bigOut = `"${process.execPath}" -e "process.stdout.write('x'.repeat(${overDefault}))"`;
5885+
let controlCode = null;
5886+
try { execSync(bigOut, { cwd: repoRoot, stdio: ['ignore', 'pipe', 'ignore'] }); } catch (e) { controlCode = e.code ?? null; }
5887+
check('sh', 'CONTROL: 2 MiB of stdout read at the default buffer dies with ENOBUFS', 'execSync(<2 MiB>)', 'ENOBUFS', controlCode);
5888+
let shRead = null;
5889+
try { shRead = sh(bigOut).length; } catch (e) { shRead = e.code ?? 'threw'; }
5890+
check('sh', 'sh() reads stdout past the 1 MiB default whole instead of dying with ENOBUFS', 'sh(<2 MiB>)', overDefault, shRead);
58675891
}
58685892

58695893
battery('ROUTE-ANCHOR PRECISION, IN BOTH DIRECTIONS (#16696)');

0 commit comments

Comments
 (0)