From 4b802eaf2b60fe9998167ce2d96ce3398ca899a8 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 03:41:55 +0000 Subject: [PATCH] fix(scripts): give tree-scaled git listings an explicit maxBuffer check-closed-ceremony-text read `git ls-files -z -- packages content/docs .changeset` into node's 1 MiB default stdout buffer; the listing is 1,048,565 bytes on main, so one added path kills the gate with ENOBUFS. Spawn it with the 64 MiB ceiling dispatch-gates already uses, and pin a fixture index past 1 MiB in --self-test with an ENOBUFS control. affected-docs' sh() wrapper had the same default; its self-test's `git ls-files packages` is 953,424 bytes, and its `git show` readers caught ENOBUFS as "file absent". Same ceiling, same pin shape. Claude-Session: https://claude.ai/code/session_01QSeKYnrQ7UMtHofKQVjcH2 Co-authored-by: Claude --- scripts/check-closed-ceremony-text.mjs | 69 ++++++++++++++++++++++++-- scripts/docs-audit/affected-docs.mjs | 26 +++++++++- 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/scripts/check-closed-ceremony-text.mjs b/scripts/check-closed-ceremony-text.mjs index cc4324bc451..94d167b841f 100644 --- a/scripts/check-closed-ceremony-text.mjs +++ b/scripts/check-closed-ceremony-text.mjs @@ -31,11 +31,13 @@ * every `CHANGELOG.md` (release-owned). */ -import { readFileSync } from 'node:fs'; -import { execFileSync } from 'node:child_process'; +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import process from 'node:process'; +import { gitFreeEnv } from './git-env.mjs'; import { isEntrypoint } from './invoked-as.mjs'; const ROOT = join(fileURLToPath(new URL('.', import.meta.url)), '..'); @@ -78,8 +80,29 @@ export function findingsIn(text) { return out; } -function trackedFiles() { - return execFileSync('git', ['ls-files', '-z', '--', ...ROOT_DIR_WATCH_HINTS.map((h) => h.replace(/\/\*\*$/, ''))], { cwd: ROOT, encoding: 'utf8' }) +/** + * The stdout ceiling the listing below is spawned with — the value and name + * `scripts/pm/dispatch-gates.mjs` gave its own git reads. + * + * `execFileSync`'s default is 1 MiB; past it node kills the child and throws + * `ENOBUFS`, so the gate dies instead of answering. This population's listing + * grows with every path added under `packages/`, `content/docs/` and + * `.changeset/`, and it reached the default: one added changeset put it over + * and red-flagged every PR that carried one. 64 MiB is headroom for a listing + * many times this tree's; the bound stays finite so a runaway child is still + * stopped. Raising it changes no verdict on a tree that already fit — only + * whether a larger tree gets one. + */ +export const GIT_MAX_BUFFER_BYTES = 64 * 1024 * 1024; + +/** The tracked paths under the walk roots, read NUL-separated. `cwd`/`env` exist for the self-test's fixture index. */ +export function trackedFiles({ cwd = ROOT, env = process.env } = {}) { + return execFileSync('git', ['ls-files', '-z', '--', ...ROOT_DIR_WATCH_HINTS.map((h) => h.replace(/\/\*\*$/, ''))], { + cwd, + env, + encoding: 'utf8', + maxBuffer: GIT_MAX_BUFFER_BYTES, + }) .split('\0') .filter(Boolean); } @@ -178,6 +201,44 @@ function selfTest() { // The tree: a non-empty population. t('the population on this tree is not empty', sweep().scanned > 0); + // The listing past spawn's default 1 MiB stdout buffer. The fixture is an + // index alone — `update-index --index-info` stages paths with no files on + // disk — sized past 1 MiB, under a walk root so the pathspec keeps it. The + // CONTROL reads the same listing with the default buffer and must die with + // ENOBUFS, so the pass below cannot come from a fixture that never reached + // the size. + const bufTmp = mkdtempSync(join(tmpdir(), 'closed-ceremony-maxbuffer-')); + try { + const env = gitFreeEnv(); + const init = spawnSync('git', ['init', '-q', '.'], { cwd: bufTmp, env, encoding: 'utf8' }); + t('fixture: git init', init.status === 0); + const EMPTY_BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'; + const paths = Array.from({ length: 16000 }, (_, i) => + `packages/fixture-${String(i).padStart(5, '0')}/src/a-long-enough-module-name-for-a-wide-listing.ts`); + const staged = spawnSync('git', ['update-index', '--index-info'], { + cwd: bufTmp, + env, + encoding: 'utf8', + input: paths.map((p) => `100644 ${EMPTY_BLOB} 0\t${p}`).join('\n') + '\n', + }); + t('fixture: git update-index', staged.status === 0); + const listingBytes = paths.reduce((n, p) => n + Buffer.byteLength(p) + 1, 0); + t('the fixture listing is past the 1 MiB default buffer', listingBytes > 1024 * 1024); + const atDefault = spawnSync('git', ['ls-files', '-z', '--', 'packages'], { cwd: bufTmp, env, encoding: 'utf8' }); + t('CONTROL: the same listing read with the default buffer dies with ENOBUFS', atDefault.error?.code === 'ENOBUFS'); + let listed = null; + try { + listed = trackedFiles({ cwd: bufTmp, env }); + } catch { + listed = null; + } + t('trackedFiles reads the whole listing instead of dying with ENOBUFS', listed?.length === paths.length); + t('…first and last path included', listed?.[0] === paths[0] && listed?.at(-1) === paths.at(-1)); + t('the ceiling stays finite and above this fixture', Number.isFinite(GIT_MAX_BUFFER_BYTES) && GIT_MAX_BUFFER_BYTES > listingBytes); + } finally { + rmSync(bufTmp, { recursive: true, force: true }); + } + if (failures.length) { for (const f of failures) console.error(` ✗ ${f}`); console.error(`check-closed-ceremony-text --self-test: ${failures.length} of ${checked} assertion(s) FAILED.`); diff --git a/scripts/docs-audit/affected-docs.mjs b/scripts/docs-audit/affected-docs.mjs index c8d2ee9a4d5..d4b8850d0e4 100644 --- a/scripts/docs-audit/affected-docs.mjs +++ b/scripts/docs-audit/affected-docs.mjs @@ -288,6 +288,17 @@ let diffBaseRef = null; // down beside their functions makes `--self-test` die in the temporal dead zone. The // functions themselves can stay where they read best — those ARE hoisted. +/** + * The stdout ceiling every `sh()` child is spawned with — the value and name + * `scripts/pm/dispatch-gates.mjs` gave its own git reads. `execSync`'s default + * is 1 MiB, and past it node kills the child and throws `ENOBUFS`: the + * self-test's `git ls-files packages` grows with the tree and sits within a + * tenth of that default, and every `git show` caller below catches the throw + * and reads a file over 1 MiB as absent. Finite, so a runaway child is still + * stopped; raising it changes no answer for output that already fit. + */ +const SH_MAX_BUFFER_BYTES = 64 * 1024 * 1024; + /** * Release-owned pages — AGENTS.md's Documentation Guardrails forbid a code PR from * editing anything under this prefix, and `.claude/workflows/docs-accuracy-audit.js` @@ -1063,7 +1074,7 @@ if (args.includes('--bridge-coverage')) { } function sh(cmd) { - return execSync(cmd, { cwd: repoRoot, stdio: ['ignore', 'pipe', 'ignore'] }).toString(); + return execSync(cmd, { cwd: repoRoot, stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: SH_MAX_BUFFER_BYTES }).toString(); } // --- 1. enumerate hand-written docs ---------------------------------------- @@ -5864,6 +5875,19 @@ function selfTest() { .filter((f) => /(^|\/)\.[^/]+\//.test(f) && f.endsWith('.ts')); check('walkSourceFiles', 'no TRACKED .ts source under packages/** lives in a dot-directory — the premise the prune rests on', 'git ls-files packages, filtered to .ts under dot-directories', 0, trackedDotDirFiles.length); + + // (5) THE `sh()` STDOUT CEILING. The listing above grows with the tree toward + // execSync's 1 MiB default, past which the child dies with ENOBUFS. A synthetic + // child prints 2 MiB; the CONTROL reads it at the default buffer and must die, so + // the pass cannot come from output that never reached the size. + const overDefault = 2 * 1024 * 1024; + const bigOut = `"${process.execPath}" -e "process.stdout.write('x'.repeat(${overDefault}))"`; + let controlCode = null; + try { execSync(bigOut, { cwd: repoRoot, stdio: ['ignore', 'pipe', 'ignore'] }); } catch (e) { controlCode = e.code ?? null; } + check('sh', 'CONTROL: 2 MiB of stdout read at the default buffer dies with ENOBUFS', 'execSync(<2 MiB>)', 'ENOBUFS', controlCode); + let shRead = null; + try { shRead = sh(bigOut).length; } catch (e) { shRead = e.code ?? 'threw'; } + check('sh', 'sh() reads stdout past the 1 MiB default whole instead of dying with ENOBUFS', 'sh(<2 MiB>)', overDefault, shRead); } battery('ROUTE-ANCHOR PRECISION, IN BOTH DIRECTIONS (#16696)');