diff --git a/lib/ship-release.ts b/lib/ship-release.ts index 2b287a2fa..57dbdf6ea 100644 --- a/lib/ship-release.ts +++ b/lib/ship-release.ts @@ -485,15 +485,17 @@ function prepushGuards(cwd: string, base: string | undefined): { line: string; s if (tree && rec.tree === tree && rows.every(row => row?.status === 'pass')) return { line: `GUARDS: pass (reused from ${path.basename(file)} at tree ${tree.slice(0, 12)}: ${rows.map(row => `${row!.id} ${row!.detail ?? 'pass'}`).join('; ')})` }; } fs.rmSync(out, { force: true }); - const pg = bin(cwd, 'gstack-pregate', ['--stage', 'guards', '--allow-repo-commands', '--out', out, ...(base ? ['--base', base] : [])], 3_600_000); - const text = pg.stdout + pg.stderr; - const verdict = /^PREGATE: (.+?) tree=/m.exec(text)?.[1] ?? `unreadable (exit ${pg.status})`; - let detail: string | undefined; - try { detail = (JSON.parse(fs.readFileSync(out, 'utf8')).checks as { id: string; detail?: string }[]).map(c => `${c.id} ${c.detail ?? ''}`.trim()).join('; '); } catch { /* no record: the verdict line says why */ } - const line = `GUARDS: ${verdict}${detail ? ` (${detail})` : ''}`; + const pg = bin(cwd, 'gstack-pregate', ['--stage', 'guards', '--allow-repo-commands', '--json', '--out', out, ...(base ? ['--base', base] : [])], 3_600_000); + let res: { status?: string; verdict?: string; tree?: string; code?: string; detail?: string; fix?: string; checks?: { id: string; status: string; detail?: string; fix?: string }[] } = {}; + try { res = JSON.parse(pg.stdout.trim().split('\n').pop() ?? ''); } catch { /* unreadable: stops below */ } + if (res.status === 'refused' && res.code === 'POLICY_SOURCE_UNAVAILABLE' && /is not a resolvable commit/.test(res.detail ?? '')) return { line: `GUARDS: not run (no base branch to read guards from: ${res.detail})` }; + if (res.status === 'refused') return { line: `GUARDS: refused ${res.code}`, stop: `the pre-push guards could not run (${res.detail}; fix: ${res.fix}): repair it, then rerun push` }; + if (res.tree && res.tree !== tree) return { line: `GUARDS: not certified (they checked the working tree ${res.tree.slice(0, 12)}, not HEAD's tree ${tree.slice(0, 12)})`, stop: 'the working tree differs from HEAD (uncommitted or untracked files): commit or stash them, then rerun push' }; + const verdict = res.verdict ?? `unreadable (exit ${pg.status}: ${firstLine(pg.stderr)})`; + const line = `GUARDS: ${verdict}${res.checks ? ` (${res.checks.map(c => `${c.id} ${c.detail ?? c.status}`).join('; ')})` : ''}`; if (pg.status === 0 && /^(pass|warn)/.test(verdict)) return { line }; - const fix = /^gstack-pregate: (?:regen|guards): .*$/m.exec(pg.stderr)?.[0] ?? firstLine(pg.stderr || pg.stdout); - return { line, stop: `the pre-push guards did not pass (${fix}): fix it, commit, then rerun push` }; + const bad = (res.checks ?? []).filter(c => c.status !== 'pass' && c.status !== 'warn').map(c => `${c.id}: ${c.fix ?? c.detail ?? c.status}`).join('; '); + return { line, stop: `the pre-push guards did not pass (${bad || firstLine(pg.stderr || pg.stdout)}): fix it, commit, then rerun push` }; } export function push(argv: readonly string[], cwd: string): Report { diff --git a/test/gstack-clean-producers.test.ts b/test/gstack-clean-producers.test.ts index 937eb19cb..4d857c1f3 100644 --- a/test/gstack-clean-producers.test.ts +++ b/test/gstack-clean-producers.test.ts @@ -31,6 +31,7 @@ const KEPT: Record = { 'restamp-staging.json': 'gstack-restamp files still to stage, read by its noop rerun (bin/gstack-restamp)', 'gate-ahead*': 'gate-ahead record and worktree (lib/restamp.ts)', 'pregate.json': 'pregate result (lib/pregate/index.ts)', + 'pregate-guards.json': 'gstack-pregate --stage guards result, the pre-push housekeeping run (lib/pregate/index.ts PREGATE_GUARDS_OUT_REL)', 'ship-queue': 'ship-queue state (lib/ship-queue-gate.ts)', 'ship-receipt.md': '/ship handoff receipt', 'ship-gate.json': '/ship handoff gate record', diff --git a/test/ship-release.test.ts b/test/ship-release.test.ts index dcf370b84..a9c1f05f3 100644 --- a/test/ship-release.test.ts +++ b/test/ship-release.test.ts @@ -378,4 +378,40 @@ describe('push: the pre-push housekeeping guards (GST-1138)', () => { expect(lineOf(f.run(RELEASE, ['push', '--branch', 'feat']).stdout, 'GUARDS')).not.toContain('reused'); expect(fs.readFileSync(path.join(f.root, 'guard-runs'), 'utf8')).toBe('ran\nran\n'); }); + + test('an uncommitted or untracked file the guards saw stops the push: the verdict must describe the pushed tree', () => { + const f = fx({ '.gstack/pregate.json': policy('test -f src/needed.ts') }); + ready(f); + f.write({ 'src/needed.ts': 'export const n = 1;\n' }); + const r = f.run(RELEASE, ['push', '--branch', 'feat']); + expect(r.status).toBe(3); + expect(lineOf(r.stdout, 'GUARDS')).toContain('not certified'); + expect(r.stdout).toContain('STOP: the working tree differs from HEAD'); + expect(pushedHead(f)).toBe(''); + }); + + test("finish's preflight record is reused only at HEAD's tree with passing regen and guards rows", () => { + const f = fx({ '.gstack/pregate.json': policy('echo ran >> ../guard-runs') }); + ready(f); + const tree = f.git('rev-parse', 'HEAD^{tree}').stdout.trim(); + const record = (at: string, guards: string) => JSON.stringify({ tree: at, checks: [{ id: 'regen', status: 'pass' }, { id: 'guards', status: guards }] }); + const finishRecord = path.join(f.work, '.git', 'gstack', 'pregate-finish-preflight.json'); + fs.mkdirSync(path.dirname(finishRecord), { recursive: true }); + const runs = () => (fs.existsSync(path.join(f.root, 'guard-runs')) ? fs.readFileSync(path.join(f.root, 'guard-runs'), 'utf8') : ''); + for (const [at, guards] of [[tree, 'fail'], ['0'.repeat(40), 'pass']]) { + fs.rmSync(path.join(f.work, '.git', 'gstack', 'pregate-guards.json'), { force: true }); + fs.writeFileSync(finishRecord, record(at, guards)); + fs.rmSync(path.join(f.root, 'guard-runs'), { force: true }); + expect(lineOf(f.run(RELEASE, ['push', '--branch', 'feat']).stdout, 'GUARDS')).not.toContain('reused'); + expect(runs()).toBe('ran\n'); + f.git('push', '-q', 'origin', '--delete', 'feat'); + } + fs.rmSync(path.join(f.work, '.git', 'gstack', 'pregate-guards.json'), { force: true }); + fs.writeFileSync(finishRecord, record(tree, 'pass')); + fs.rmSync(path.join(f.root, 'guard-runs'), { force: true }); + const reused = f.run(RELEASE, ['push', '--branch', 'feat']); + expect(lineOf(reused.stdout, 'GUARDS')).toMatch(/^GUARDS: pass \(reused from pregate-finish-preflight\.json at tree /); + expect(runs()).toBe(''); + expect(lineOf(reused.stdout, 'PUSH')).toMatch(/^PUSH: pushed /); + }); });