From c8641cc486567a88f37b38d19f776365a7ee3364 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Sat, 21 Nov 2026 14:00:53 -0800 Subject: [PATCH] Large repos can raise the review-plan time limit for every /ship step (GST-3398) (#4632) `GSTACK_REVIEW_PLAN_TIMEOUT_MS` now raises the review-plan time limit in every place /ship runs the plan, not just two of them. Why: on a 301,669-file repository the plan takes 255 to 316 s on a 2-core machine; the plan and the review pass read the variable, but render and preflight still cut it off at 180 s. Nothing to do unless your repository is large; then set the variable (docs/troubleshooting.md#review-plan-stopped). Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- bin/gstack-review-dispatch | 32 ++++++++------ changelog.d/review-plan-timeout-everywhere.md | 1 + docs/review-policy.md | 2 +- docs/troubleshooting.md | 39 +++++++++++++---- lib/review-collect.ts | 4 +- lib/ship-preflight.ts | 3 +- test/review-collect.test.ts | 40 ++++++++++++----- test/review-dispatch-plan-timeout.test.ts | 43 ++++++++++++++++++- 8 files changed, 126 insertions(+), 38 deletions(-) create mode 100644 changelog.d/review-plan-timeout-everywhere.md diff --git a/bin/gstack-review-dispatch b/bin/gstack-review-dispatch index 1b84f9174..c30341d01 100755 --- a/bin/gstack-review-dispatch +++ b/bin/gstack-review-dispatch @@ -29,7 +29,6 @@ import { classifyOutsideReview } from '../lib/outside-review-result'; import { gateOutcomeLine } from '../lib/gate-outcomes'; import { diffCommand, type Dispatch } from '../lib/review-depth'; import { mayExclude, policySelection, selectFor } from '../lib/review-selection'; -import { REVIEW_PLAN_TIMEOUT_MS } from '../lib/review-collect'; import { reviewPlanTimeoutMs } from '../lib/review-collect'; import { resultLine } from '../lib/result-codes'; import { runScriptSync } from '../lib/script-invocation'; @@ -351,6 +350,18 @@ function settleExcerpt(r: Pick & { state: string }) | null = null; if (mayExclude(process.cwd(), base)) { const tail = args.includes('--') ? args.slice(args.indexOf('--') + 1) : []; - const p = runScriptSync(path.join(ROOT, 'bin', 'gstack-review-plan'), ['--base', base, ...tail, '--json'], { encoding: 'utf8', timeout: REVIEW_PLAN_TIMEOUT_MS, maxBuffer: 64 * 1024 * 1024, env: { ...process.env, GSTACK_REVIEW_PLAN_BOUNDED: '1' } }); + const p = runPlan('gstack-review-dispatch render', base, tail, true); + if (p.stopLine) { process.stderr.write(p.stopLine + '\n'); throw new BriefError(`gstack-review-plan --base ${base} --json did not finish, so the base policy's review selection is unknown and no brief is rendered without it`); } if (p.status !== 0) throw new BriefError(`gstack-review-plan --base ${base} --json exited ${p.status ?? p.signal}, so the base policy's review selection is unknown and no brief is rendered without it: ${(p.stderr ?? '').trim().slice(-400)}`); x = (JSON.parse(p.stdout) as { review_exclude?: Record & { state: string } }).review_exclude ?? null; } @@ -398,19 +410,11 @@ function pass(): number { process.stdout.write([...m.plan_lines, `MANIFEST: file=${file} pass=${id} readers=${m.readers.length} audit-only`, ...m.readers.map(r => readerLine(m, r))].join('\n') + '\n'); return EXIT.ok; } - const timeoutMs = reviewPlanTimeoutMs(); - const plan = (json: boolean) => runScriptSync(path.join(ROOT, 'bin', 'gstack-review-plan'), ['--base', base, ...planArgs, ...(json ? ['--json'] : [])], { encoding: 'utf8', timeout: timeoutMs, maxBuffer: 64 * 1024 * 1024, env: { ...process.env, GSTACK_REVIEW_PLAN_BOUNDED: '1' } }); - const stopped = (r: ReturnType, json: boolean) => { - if (r.status !== null || (!r.signal && !r.error)) return false; - const reason = (r.error as NodeJS.ErrnoException | undefined)?.code === 'ETIMEDOUT' ? 'timeout' : r.signal ? 'signal' : 'spawn'; - process.stderr.write(`${r.stderr ?? ''}${resultLine('gstack-review-dispatch pass', 'REVIEW_PLAN_STOPPED', `step="gstack-review-plan --base ${base}${json ? ' --json' : ''}" reason=${reason} timeout_ms=${timeoutMs} signal=${r.signal ?? 'none'}`)}\n`); - return true; - }; - const text = plan(false); - if (stopped(text, false)) return EXIT.fail; + const text = runPlan('gstack-review-dispatch pass', base, planArgs, false); + if (text.stopLine) { process.stderr.write(text.stopLine + '\n'); return EXIT.fail; } if (text.status !== 0) { process.stdout.write(text.stdout ?? ''); process.stderr.write(text.stderr ?? ''); return EXIT.fail; } - const j = plan(true); - if (stopped(j, true)) return EXIT.fail; + const j = runPlan('gstack-review-dispatch pass', base, planArgs, true); + if (j.stopLine) { process.stderr.write(j.stopLine + '\n'); return EXIT.fail; } if (j.status !== 0) { process.stderr.write(j.stderr ?? ''); return EXIT.fail; } const p = JSON.parse(j.stdout) as { next: { step: string }; dispatches: Dispatch[]; targeted?: { classes: string[] }; fix_range?: { base: string } | null; review_exclude?: Record & { state: string; excluded?: number; kept_files?: unknown; references?: unknown }; readers?: EffectiveReaders }; const step = p.next.step; diff --git a/changelog.d/review-plan-timeout-everywhere.md b/changelog.d/review-plan-timeout-everywhere.md new file mode 100644 index 000000000..3d183011d --- /dev/null +++ b/changelog.d/review-plan-timeout-everywhere.md @@ -0,0 +1 @@ +**`GSTACK_REVIEW_PLAN_TIMEOUT_MS` now raises the review-plan limit everywhere /ship runs the plan, so large repositories can finish /ship.** `gstack-review-dispatch render` and `gstack-ship-preflight` ran `gstack-review-plan` under a fixed 180-second limit while the plan itself and `gstack-review-dispatch pass` read the variable; all four now read it through one parser (a whole number of milliseconds; an empty, zero, negative or non-numeric value keeps 180 s), and render prints the same `REVIEW_PLAN_STOPPED` line as the pass when the plan runs out of time. On a 301,669-file repository the plan takes 255 to 316 seconds on a 2-core machine, so set the variable there: docs/troubleshooting.md#review-plan-stopped. (GST-3398) diff --git a/docs/review-policy.md b/docs/review-policy.md index 834a2a76a..3069fd363 100644 --- a/docs/review-policy.md +++ b/docs/review-policy.md @@ -423,7 +423,7 @@ costs one cheap pass. Each scan gets one 120-second deadline, set in millisecond 256 MB, is also `consumer_scan_failed`, and its detail says what stopped it and names the deadline. One scan fits a minute inside the 180 seconds /ship gives `gstack-review-plan`; a plan that also scans a fix range runs two scans, and if they -pass that limit the plan, run by itself or under `gstack-review-dispatch pass`, stops with +pass that limit the plan, run by itself or under `gstack-review-dispatch pass` or `render`, stops with [`REVIEW_PLAN_STOPPED`](troubleshooting.md#review-plan-stopped) before any reader runs (`GSTACK_REVIEW_PLAN_TIMEOUT_MS` sets the limit in milliseconds). Only `classifyDiff` applies the rule (`lib/review-collect.ts` finds the consumers, `gstack-ship-preflight`'s `RUNTIME_LINES` reads the same diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index bf6b908b9..a924cb088 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -4220,16 +4220,37 @@ and ask a person with the log file. ### `REVIEW_PLAN_STOPPED` **Meaning.** `gstack-review-plan` did not finish inside its limit, so it printed -no plan, or `gstack-review-dispatch pass`, which runs it before it writes a -manifest, stopped without dispatching a reader (exit 1 either way). The line -names the step (the plan command), `reason=timeout` with `timeout_ms` (180,000 -ms; `GSTACK_REVIEW_PLAN_TIMEOUT_MS` sets it in milliseconds), `reason=signal` with the signal when something else -killed it, or `reason=spawn` when it could not start. Before this line existed, a -plan that ran past the limit was killed with no output (GST-1442). +no plan, or `gstack-review-dispatch pass` (which runs it before it writes a +manifest) or `gstack-review-dispatch render` (which runs it for the base +policy's `review_exclude_paths` before it writes a brief) stopped without +dispatching a reader (exit 1 either way). The line names the step (the plan +command), `reason=timeout` with `timeout_ms`, `reason=signal` with the signal +when something else killed it, or `reason=spawn` when it could not start. Before +this line existed, a plan that ran past the limit was killed with no output +(GST-1442). -**Fix.** Rerun once unchanged. If it stops again, report the line and the -repository's file count (`git ls-files | wc -l`) to your Captain. For -`reason=signal`, check the machine's free memory first. +**The limit.** 180,000 ms (3 minutes) by default. Set +`GSTACK_REVIEW_PLAN_TIMEOUT_MS` to a whole number of milliseconds to change it, +for example `GSTACK_REVIEW_PLAN_TIMEOUT_MS=600000` for 10 minutes. The plan run +by itself, `gstack-review-dispatch pass` and `render`, and +`gstack-ship-preflight` all read the same variable, so set it once in the +environment /ship runs in. An empty value, `0`, a negative number or anything +that is not a number is ignored and the 180,000 ms default applies; a fraction +is rounded down. `timeout_ms` in the line shows the limit that was actually used, +so `timeout_ms=180000` after you set the variable means it did not reach the +process. + +**Large repositories should set it.** The plan's time grows with the number of +tracked files: on a 301,669-file repository it took 255 to 316 seconds on a +2-core machine, so every /ship there stopped at the default before any reviewer +ran (GST-3398). If `git ls-files | wc -l` is in the hundreds of thousands, set +the limit to about twice the plan's measured time (`time gstack-review-plan +--base ` with the variable set high) before running /ship. + +**Fix.** Rerun once unchanged. If it stops again with `reason=timeout` on a large +repository, set `GSTACK_REVIEW_PLAN_TIMEOUT_MS` as above and rerun; otherwise +report the line and the repository's file count (`git ls-files | wc -l`) to your +Captain. For `reason=signal`, check the machine's free memory first. ### `gstack-review-plan: not run (commits after the reviewed head would ship with no reviewer ...) (review_required_no_readers)` diff --git a/lib/review-collect.ts b/lib/review-collect.ts index a34c7b231..c75fc75d1 100644 --- a/lib/review-collect.ts +++ b/lib/review-collect.ts @@ -138,11 +138,11 @@ export const CONSUMER_SCAN_CAP = 2000; export const CONSUMER_SCAN_DEPTH = 4; /** * One consumer scan's whole budget, every git grep in it (GSTACK_CONSUMER_SCAN_TIMEOUT_MS overrides it); past it the scan fails - * closed. One scan stays a minute under the 180 s gstack-review-dispatch and gstack-ship-preflight give all of gstack-review-plan; + * closed. One scan stays a minute under the default 180 s (REVIEW_PLAN_TIMEOUT_MS) every caller gives all of gstack-review-plan; * a plan that also scans a fix range (--range-base) runs a second scan and can still reach that limit, which fails the plan. */ export const CONSUMER_SCAN_TIMEOUT_MS = 120_000; -/** How long one gstack-review-plan run may take, by itself or under gstack-review-dispatch pass (GSTACK_REVIEW_PLAN_TIMEOUT_MS overrides it); past it the run stops with REVIEW_PLAN_STOPPED. */ +/** How long one gstack-review-plan run may take, by itself or under gstack-review-dispatch pass and render or gstack-ship-preflight (GSTACK_REVIEW_PLAN_TIMEOUT_MS overrides it through reviewPlanTimeoutMs, which every caller reads); past it the run stops with REVIEW_PLAN_STOPPED. */ export const REVIEW_PLAN_TIMEOUT_MS = 180_000; /** Pathspec bytes per git grep call: a long list of files to search splits across calls instead of growing one command line. */ export const CONSUMER_SCAN_PATHSPEC_BYTES = 16_000; diff --git a/lib/ship-preflight.ts b/lib/ship-preflight.ts index 4d66cf861..d3ec7256d 100644 --- a/lib/ship-preflight.ts +++ b/lib/ship-preflight.ts @@ -13,6 +13,7 @@ import { defaultBaseBranch } from './default-base'; import { isConfigUnavailable, loadConfigFromBase, loadConfigFromHead } from './pregate/config'; import { canDispatch } from './pregate/workflows'; import { buildRunCheck } from './pregate/run-check'; +import { reviewPlanTimeoutMs } from './review-collect'; import { classifyDiff } from './review-depth'; import { wtreeFingerprint } from './script-invocation'; import { audit } from './ship-audit'; @@ -191,7 +192,7 @@ function merge(cwd: string, base: string, r: Report): boolean { .next('Step 2: read each candidate (and manifests for new binaries or exports); a new distributable without a pipeline needs Step 2\'s question'); } r.add(`POLICY: ${['bump', 'queue_mode', 'review_scope', 'history'].map(k => `${k}=${policy?.[k] ?? '?'}`).join(' ')} (binds Steps 12-16 and history)`); - const plan = bin(cwd, 'gstack-review-plan', ['--base', base, '--json'], 180_000); + const plan = bin(cwd, 'gstack-review-plan', ['--base', base, '--json'], reviewPlanTimeoutMs()); let runtime = 'unknown'; try { const j = JSON.parse(plan.stdout); diff --git a/test/review-collect.test.ts b/test/review-collect.test.ts index 56f4d6e24..5e8903265 100644 --- a/test/review-collect.test.ts +++ b/test/review-collect.test.ts @@ -3,7 +3,7 @@ import { spawnSync } from 'node:child_process'; import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; -import { collectCommit, collectLive, CONSUMER_SCAN_CAP, CONSUMER_SCAN_TIMEOUT_MS, consumerScanTimeoutMs, grepConsumers, loadRegistryAt, REVIEW_PLAN_TIMEOUT_MS, scanConsumers } from '../lib/review-collect'; +import { collectCommit, collectLive, CONSUMER_SCAN_CAP, CONSUMER_SCAN_TIMEOUT_MS, consumerScanTimeoutMs, grepConsumers, loadRegistryAt, REVIEW_PLAN_TIMEOUT_MS, reviewPlanTimeoutMs, scanConsumers } from '../lib/review-collect'; import { depthOfCommitInput } from '../lib/review-depth'; const dirs: string[] = []; @@ -349,15 +349,35 @@ describe('GST-544: the consumer scan reads only files that can match, under one const got = grepConsumers(r.dir, ['rows.json'], '', () => true, Date.now() + 30_000); expect(Array.isArray(got) && got.filter(Boolean).length).toBe(1_100_000); }, 30_000); - // Value: protects=a slow scan ends in its own fail-closed consumer_scan_failed, not in the parent killing gstack-review-plan; - // fails_when=the scan deadline reaches the timeout the dispatch and preflight callers give the whole plan; seam=caller source - test('the default scan deadline leaves a minute of the plan timeout its callers set', () => { - const callers = ['lib/ship-preflight.ts'].map(f => fs.readFileSync(path.join(import.meta.dir, '..', f), 'utf8')); - const dispatch = fs.readFileSync(path.join(import.meta.dir, '..', 'bin/gstack-review-dispatch'), 'utf8'); - expect(dispatch).toMatch(/import \{ REVIEW_PLAN_TIMEOUT_MS \} from '\.\.\/lib\/review-collect'/); - const limits = [REVIEW_PLAN_TIMEOUT_MS, ...callers.flatMap(src => [...src.matchAll(/gstack-review-plan'[^\n]*?(?:timeout: |\], )(\d[\d_]+)/g)].map(m => Number(m[1]!.replace(/_/g, ''))))]; - expect(limits.length).toBeGreaterThanOrEqual(2); - expect(CONSUMER_SCAN_TIMEOUT_MS).toBeLessThanOrEqual(Math.min(...limits) - 60_000); + // Value: protects=a slow scan ends in its own fail-closed consumer_scan_failed, not in the parent killing gstack-review-plan, + // and every caller that bounds the plan reads the one limit GSTACK_REVIEW_PLAN_TIMEOUT_MS raises; fails_when=the scan deadline + // reaches the plan's default limit, or a caller bounds the plan with its own literal again (GST-3398: render and preflight + // held 180 s while the pass and the plan read the variable); seam=caller source + test('the default scan deadline leaves a minute of the plan timeout, and every plan caller reads the shared limit', () => { + const read = (f: string) => fs.readFileSync(path.join(import.meta.dir, '..', f), 'utf8'); + const callers = { 'bin/gstack-review-dispatch': read('bin/gstack-review-dispatch'), 'lib/ship-preflight.ts': read('lib/ship-preflight.ts'), 'bin/gstack-review-plan': read('bin/gstack-review-plan') }; + for (const [f, src] of Object.entries(callers)) { + expect(src, f).toMatch(/import \{[^}]*\breviewPlanTimeoutMs\b[^}]*\} from '\.\.?\/(?:lib\/)?review-collect'/); + expect(src, f).not.toMatch(/\bREVIEW_PLAN_TIMEOUT_MS\b/); + expect([...src.matchAll(/gstack-review-plan'[^\n]*?(?:timeout: |\], )\d/g)].map(m => m[0]), f).toEqual([]); + } + expect(CONSUMER_SCAN_TIMEOUT_MS).toBeLessThanOrEqual(REVIEW_PLAN_TIMEOUT_MS - 60_000); + }); + // Value: protects=the one parser every plan caller shares keeps its bounds: a whole number of at least 1 ms sets the limit, + // and anything else keeps 180 s; fails_when=an empty, zero, negative or non-numeric value sets a limit, or the default moves; + // why_new=GST-3398 (large repositories raise it); seam=reviewPlanTimeoutMs with the variable set per case; no_claim=caller wiring + test('reviewPlanTimeoutMs reads GSTACK_REVIEW_PLAN_TIMEOUT_MS and keeps 180 s for unset or invalid values', () => { + const saved = process.env.GSTACK_REVIEW_PLAN_TIMEOUT_MS; + try { + const at = (v: string | undefined) => { if (v === undefined) delete process.env.GSTACK_REVIEW_PLAN_TIMEOUT_MS; else process.env.GSTACK_REVIEW_PLAN_TIMEOUT_MS = v; return reviewPlanTimeoutMs(); }; + expect(REVIEW_PLAN_TIMEOUT_MS).toBe(180_000); + for (const v of [undefined, '', 'soon', '0', '-5', '0.5', 'Infinity', 'NaN']) expect(at(v), String(v)).toBe(180_000); + expect(at('600000')).toBe(600_000); + expect(at('1')).toBe(1); + expect(at('2500.9')).toBe(2500); + } finally { + if (saved === undefined) delete process.env.GSTACK_REVIEW_PLAN_TIMEOUT_MS; else process.env.GSTACK_REVIEW_PLAN_TIMEOUT_MS = saved; + } }); }); diff --git a/test/review-dispatch-plan-timeout.test.ts b/test/review-dispatch-plan-timeout.test.ts index 3af1a69fd..c5bb51301 100644 --- a/test/review-dispatch-plan-timeout.test.ts +++ b/test/review-dispatch-plan-timeout.test.ts @@ -7,10 +7,18 @@ import { spawnSync } from 'node:child_process'; import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; +import { EXIT } from '../lib/headless-artifacts'; +import { runScriptSync } from '../lib/script-invocation'; +import { REVIEW_EXCLUDE_KEY_VERSION } from '../lib/ship-policy'; +import { cleanup, commit, makeFixtureRepo, type FixtureRepo } from './helpers/pregate-fixture'; const DISPATCH = path.join(import.meta.dir, '..', 'bin', 'gstack-review-dispatch'); const dirs: string[] = []; -afterEach(() => { while (dirs.length) fs.rmSync(dirs.pop()!, { recursive: true, force: true }); }); +const repos: FixtureRepo[] = []; +afterEach(() => { + while (dirs.length) fs.rmSync(dirs.pop()!, { recursive: true, force: true }); + while (repos.length) cleanup(repos.pop()!); +}); describe('pass names the plan step and its limit when the plan does not finish (GST-1442)', () => { // Value: protects=a plan past the pass's limit ends in a line naming the step, the reason and the limit, with no manifest; @@ -60,3 +68,36 @@ describe('gstack-review-plan run by itself stops at its own limit (GST-1855)', ( expect(ok.stdout).toContain('REVIEW_PLAN:'); }, 180_000); }); + +describe('render reads the plan limit from GSTACK_REVIEW_PLAN_TIMEOUT_MS too (GST-3398)', () => { + // Value: protects=a standalone render on a base whose policy excludes paths runs gstack-review-plan under the limit the pass + // and the plan read, so a large repository that raises GSTACK_REVIEW_PLAN_TIMEOUT_MS is not cut off at 180 s by render; + // fails_when=render runs the plan under a hard-coded 180 s again (harness-evals #548 stopped twice at timeout_ms=180000); + // why_new=GST-3398; seam=gstack-review-dispatch render with the variable set, unset, empty and invalid; no_claim=the plan's speed + test('a plan past the variable\'s limit stops render with REVIEW_PLAN_STOPPED; unset or invalid values keep the default', () => { + const policy = JSON.stringify({ review_exclude_paths: ['docs/results/**'], min_gstack_version: REVIEW_EXCLUDE_KEY_VERSION }) + '\n'; + const repo = makeFixtureRepo({ prefix: 'rd-render-timeout-', baseFiles: { '.gstack/ship-policy.json': policy } }); + repos.push(repo); + commit(repo, { 'lib/a.ts': 'export const a = 1;\n', 'docs/results/r.json': '{}\n' }); + const mb = repo.git('merge-base', 'origin/main', 'HEAD'); + const render = (out: string, value?: string) => { + const env: Record = { ...process.env, ...repo.env }; + if (value === undefined) delete env.GSTACK_REVIEW_PLAN_TIMEOUT_MS; else env.GSTACK_REVIEW_PLAN_TIMEOUT_MS = value; + const dir = path.join(repo.root, out); + const r = runScriptSync(DISPATCH, ['render', '--role', 'outside-voice', '--out', dir, '--base', 'main', '--diff-base', mb], { cwd: repo.work, env, encoding: 'utf8', timeout: 120_000, maxBuffer: 64 * 1024 * 1024 }); + return { status: r.status, err: r.stderr ?? '', diff: path.join(dir, 'outside-voice.diff') }; + }; + const stopped = render('o-stopped', '1'); + expect(stopped.status, stopped.err).toBe(EXIT.fail); + expect(stopped.err).toContain('gstack-review-dispatch render: gstack-review-plan did not finish inside the pass'); + expect(stopped.err).toContain('step="gstack-review-plan --base main --json" reason=timeout timeout_ms=1 signal=SIGTERM'); + expect(stopped.err).toContain('(REVIEW_PLAN_STOPPED)'); + expect(fs.existsSync(stopped.diff)).toBe(false); + for (const [out, value] of [['o-unset', undefined], ['o-empty', ''], ['o-word', 'soon'], ['o-zero', '0'], ['o-negative', '-5']] as const) { + const r = render(out, value); + expect(r.status, `${value}: ${r.err}`).toBe(EXIT.ok); + expect(r.err).not.toContain('REVIEW_PLAN_STOPPED'); + expect(fs.readFileSync(r.diff, 'utf8')).toContain('diff --git a/lib/a.ts'); + } + }, 180_000); +});