Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
41.2 KB
Raw Blame
import { afterEach, describe, expect, test } from 'bun:test';
import { spawnSync } from 'child_process';
import { builtinModules } from 'module';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { runBashScript } from './helpers/bash-script';

// ./setup installs production dependencies only (GSTA-97). devDependencies are
// the test and eval toolchain, chiefly the Claude Agent SDK's per-platform
// native binaries (~440 MB on Linux). This file pins the two halves of that:
// a user install (no dev toolchain in node_modules) is production-only, while a
// checkout that already has the dev toolchain gets the full frozen install; and
// nothing setup, the build or an installed skill loads is a devDependency.

const ROOT = path.resolve(import.meta.dir, '..');
const read = (rel: string) => fs.readFileSync(path.join(ROOT, rel), 'utf-8');
const PKG = JSON.parse(read('package.json'));
const bases: string[] = [];
afterEach(() => { for (const b of bases.splice(0)) fs.rmSync(b, { recursive: true, force: true }); });

/** process.env without the developer's global Bun config: HOME and XDG_CONFIG_HOME move into the temp base, BUN_CONFIG_* is dropped. */
function isolatedBunEnv(base: string): NodeJS.ProcessEnv {
  const env = Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith('BUN_CONFIG_')));
  for (const dir of ['home', 'xdg']) fs.mkdirSync(path.join(base, dir), { recursive: true });
  return { ...env, HOME: path.join(base, 'home'), XDG_CONFIG_HOME: path.join(base, 'xdg') };
}

function tempBase(): string {
  const base = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-prod-deps-'));
  bases.push(base);
  return base;
}

/** A bash function sliced out of a script, by its `name() {` line through the closing `}`. */
function sliceFunction(source: string, name: string): string {
  const start = source.indexOf(`\n${name}() {\n`);
  if (start === -1) throw new Error(`${name}() not found`);
  const end = source.indexOf('\n}\n', start);
  return source.slice(start + 1, end + 3);
}

/** install_runtime_deps and the helpers it calls, sliced out of setup in definition order, after the lock helper setup sources. */
function depsFunctions(setup: string): string {
  const names = ['bun_cmd', '_deps_role', '_deps_has_dev', '_deps_verified', '_deps_lock', '_deps_unlock', '_deps_recover',
    '_deps_migration_start', '_deps_migration_finish', 'install_runtime_deps'];
  return `. ${JSON.stringify(path.join(ROOT, 'bin', 'gstack-lock.sh'))}\n` + names.map((n) => (n === '_deps_unlock' ? setup.split('\n').find((l) => l.startsWith('_deps_unlock() {'))! + '\n' : sliceFunction(setup, n))).join('\n');
}

/** A git repository marked as a contributor's checkout (what bin/dev-setup writes). */
function markContributor(dir: string): void {
  gitInit(dir);
  spawnSync('git', ['-C', dir, 'config', '--local', 'gstack.contributor', 'true'], { timeout: 10_000 });
}

function gitInit(dir: string): void {
  spawnSync('git', ['init', '-q', dir], { timeout: 10_000 });
}

/**
 * A bun that records each argv line. Like Bun, a --frozen-lockfile install fails
 * ($FROZEN_RC) until `install --lockfile-only` has refreshed the lockfile; any other
 * call exits $FALLBACK_RC.
 */
function stubBun(base: string): { bun: string; calls: () => string[] } {
  const log = path.join(base, 'calls');
  const bun = path.join(base, 'bun');
  const fresh = path.join(base, 'lockfile-refreshed');
  fs.writeFileSync(bun, `#!/usr/bin/env bash\necho "$*" >> ${JSON.stringify(log)}\ncase "$*" in\n  *--frozen-lockfile*) [ -f ${JSON.stringify(fresh)} ] && exit 0; exit "\${FROZEN_RC:-0}" ;;\n  *--lockfile-only*) rc="\${FALLBACK_RC:-0}"; [ "$rc" -eq 0 ] && : > ${JSON.stringify(fresh)}; exit "$rc" ;;\nesac\nexit "\${FALLBACK_RC:-0}"\n`, { mode: 0o755 });
  return { bun, calls: () => (fs.existsSync(log) ? fs.readFileSync(log, 'utf-8').trim().split('\n') : []) };
}

describe.skipIf(process.platform === 'win32')('setup installs production dependencies only', () => {
  const setup = read('setup');
  const script = (bun: string, call: string) =>
    `set -e\nBUN_CMD=${JSON.stringify(bun)}\n${depsFunctions(setup)}\n${call}\n`;

  test('install_runtime_deps passes --production with the frozen lockfile, and refreshes a stale lockfile before installing production-only again', () => {
    const base = tempBase();
    const { bun, calls } = stubBun(base);
    expect(runBashScript(script(bun, 'install_runtime_deps'), { cwd: base, env: { ...process.env, FROZEN_RC: '0' } }).status).toBe(0);
    expect(calls()).toEqual(['install --production --frozen-lockfile']);

    const base2 = tempBase();
    const stub2 = stubBun(base2);
    expect(runBashScript(script(stub2.bun, 'install_runtime_deps'), { cwd: base2, env: { ...process.env, FROZEN_RC: '1' } }).status).toBe(0);
    expect(stub2.calls()).toEqual(['install --production --frozen-lockfile', 'install --lockfile-only', 'install --production --frozen-lockfile']);

    // Generated by /ship Step 7 coverage audit (GSTA-97).
    // Value: protects=a failed fallback install stops setup's install step instead of building on a missing node_modules;
    //   fails_when=install_runtime_deps swallows the fallback's failure (an added `|| true` or a trailing command);
    //   why_new=both rows above end in a successful install; seam=none; no_claim=setup's own subshell wiring around the call
    const base3 = tempBase();
    const stub3 = stubBun(base3);
    const failed = runBashScript(script(stub3.bun, 'install_runtime_deps\necho AFTER'), { cwd: base3, env: { ...process.env, FROZEN_RC: '1', FALLBACK_RC: '3' } });
    expect(failed.status).toBe(3);
    expect(failed.stdout).not.toContain('AFTER');
    expect(stub3.calls()).toEqual(['install --production --frozen-lockfile', 'install --lockfile-only']);
  });

  // Value: protects=a contributor checkout (marked, dev toolchain present) keeps its dev packages at bun.lock's versions after ./setup;
  //   fails_when=setup runs the production install there, which Bun reports as "no changes" and leaves old dev versions;
  //   why_new=every other row starts without node_modules; seam=none; no_claim=Bun's own update behavior (stub bun)
  // #3447: the contributor is the checkout marked with git config gstack.contributor, not any checkout that has typescript.
  test('a marked contributor checkout gets the full frozen install, falling back to a plain install', () => {
    const base = tempBase();
    markContributor(base);
    fs.mkdirSync(path.join(base, 'node_modules', 'typescript'), { recursive: true });
    const { bun, calls } = stubBun(base);
    expect(runBashScript(script(bun, 'install_runtime_deps'), { cwd: base, env: { ...process.env, FROZEN_RC: '0' } }).status).toBe(0);
    expect(calls()).toEqual(['install --frozen-lockfile']);

    const base2 = tempBase();
    markContributor(base2);
    fs.mkdirSync(path.join(base2, 'node_modules', 'typescript'), { recursive: true });
    const stub2 = stubBun(base2);
    expect(runBashScript(script(stub2.bun, 'install_runtime_deps'), { cwd: base2, env: { ...process.env, FROZEN_RC: '1' } }).status).toBe(0);
    expect(stub2.calls()).toEqual(['install --frozen-lockfile', 'install']);

    // Generated by /ship Step 7 coverage audit (GSTA-97).
    // Value: protects=a contributor checkout whose full install fails stops setup's install step instead of building on stale packages;
    //   fails_when=the dev branch swallows the plain install's failure (an added `|| true`, or a warn-and-continue fallback);
    //   why_new=both rows above succeed, and the failure row in the test above runs the production branch; seam=none;
    //   no_claim=setup's own subshell wiring around the call (stub bun)
    const base3 = tempBase();
    markContributor(base3);
    fs.mkdirSync(path.join(base3, 'node_modules', 'typescript'), { recursive: true });
    const stub3 = stubBun(base3);
    const failed = runBashScript(script(stub3.bun, 'install_runtime_deps\necho AFTER'), { cwd: base3, env: { ...process.env, FROZEN_RC: '1', FALLBACK_RC: '4' } });
    expect(failed.status).toBe(4);
    expect(failed.stdout).not.toContain('AFTER');
    expect(stub3.calls()).toEqual(['install --frozen-lockfile', 'install']);
  });

  // #4288: setup runs under umask 077 for its own state and logs. Bun extracts
  // packages into its shared cache with the caller's umask, and later installs of
  // unrelated projects hardlink those files, so the installs run under 022.
  test('install_runtime_deps installs under umask 022 on every branch while setup keeps 077 (#4288)', () => {
    const umaskBun = (base: string) => {
      const log = path.join(base, 'umasks');
      const bun = path.join(base, 'umask-bun');
      fs.writeFileSync(bun, `#!/usr/bin/env bash\numask >> ${JSON.stringify(log)}\nexec ${JSON.stringify(stubBun(base).bun)} "$@"\n`, { mode: 0o755 });
      return { bun, umasks: () => fs.readFileSync(log, 'utf-8').trim().split('\n') };
    };
    const run = (base: string, env: Record<string, string>) => {
      const stub = umaskBun(base);
      const r = runBashScript(`umask 077\n${script(stub.bun, 'install_runtime_deps')}\necho "after=$(umask)"\n`, { cwd: base, env: { ...process.env, ...env } });
      expect(r.status, r.stderr).toBe(0);
      expect(r.stdout).toContain('after=0077');
      return stub.umasks();
    };
    expect(run(tempBase(), { FROZEN_RC: '0' })).toEqual(['0022']);
    expect(run(tempBase(), { FROZEN_RC: '1' })).toEqual(['0022', '0022', '0022']);
    const contributor = tempBase();
    markContributor(contributor);
    fs.mkdirSync(path.join(contributor, 'node_modules', 'typescript'), { recursive: true });
    expect(run(contributor, { FROZEN_RC: '1' })).toEqual(['0022', '0022']);
    expect(setup).toMatch(/\(\n\s+umask 022\n\s+cd "\$SOURCE_GSTACK_DIR"\n\s+if \[ -n "\$_PLAYWRIGHT_PLATFORM_OVERRIDE" \]/);
  });

  test('every bun install in setup goes through install_runtime_deps', () => {
    const body = setup.replace(sliceFunction(setup, 'install_runtime_deps'), '');
    const directInstall = /(?:^|[\s;&|(])(?:bun_cmd|"?\$\{?BUN_CMD\}?"?|bun)\s+install\b/;
    const direct = body.split('\n').filter((l) => directInstall.test(l) && !l.trim().startsWith('#'));
    expect(direct).toEqual([]);
    // The build, the Codex render and each host render install first.
    expect(setup.match(/^\s+install_runtime_deps$/gm)?.length).toBeGreaterThanOrEqual(7);
    // Negative controls: the pre-GSTA-97 line and every other spelling of a direct install are caught.
    for (const line of [
      '    bun_cmd install --frozen-lockfile 2>/dev/null || bun_cmd install',
      '    "$BUN_CMD" install --frozen-lockfile',
      '    $BUN_CMD install',
      '    "${BUN_CMD}" install',
      '  (cd x && bun install)',
    ]) expect(directInstall.test(line), line).toBe(true);
    expect(directInstall.test('    bun_cmd x playwright install chromium')).toBe(false);
  });

  test('gstack-browser-ensure never runs a full bun install', () => {
    const fullInstalls = (text: string) => text.split('\n')
      .filter((l) => !l.trim().startsWith('#') && !/^\s*log /.test(l))
      .flatMap((l) => [...l.matchAll(/\bbun install\b(?! --production| --lockfile-only| failed)/g)].map(() => l.trim()));
    const ensure = read('bin/gstack-browser-ensure');
    expect(ensure).toContain('bun install --production --frozen-lockfile');
    expect(fullInstalls(ensure)).toEqual([]);
    // Negative control: the pre-GSTA-97 install line is caught.
    expect(fullInstalls('  (cd "$ROOT" && { bun install --frozen-lockfile >/dev/null 2>&1 || bun install >/dev/null; })')).toHaveLength(2);
  });

  test('bin/dev-setup installs the full set even over a production-only node_modules', () => {
    const devSetup = read('bin/dev-setup');
    const start = devSetup.indexOf('# 2. Install dependencies');
    const block = devSetup.slice(start, devSetup.indexOf('# 3.', start));
    const base = tempBase();
    const { bun, calls } = stubBun(base);
    fs.mkdirSync(path.join(base, 'node_modules'));
    const r = runBashScript(`set -e\nREPO_ROOT=${JSON.stringify(base)}\nbun() { ${JSON.stringify(bun)} "$@"; }\n${block}`);
    expect(r.status).toBe(0);
    expect(calls()).toEqual(['install']);
    // #3447: outside git the marker cannot be written; dev-setup says how to mark the run instead and still installs.
    expect(r.stderr).toContain('GSTACK_CONTRIBUTOR=1');
  });

  // Value: protects=a contributor's clone keeps its dev toolchain on every later ./setup;
  //   fails_when=bin/dev-setup stops writing the marker install_runtime_deps reads; why_new=#3447 adds the marker; seam=none; no_claim=none
  test('bin/dev-setup marks the clone as a contributor checkout', () => {
    const devSetup = read('bin/dev-setup');
    const start = devSetup.indexOf('# 2. Install dependencies');
    const block = devSetup.slice(start, devSetup.indexOf('# 3.', start));
    const base = tempBase();
    gitInit(base);
    const { bun } = stubBun(base);
    const r = runBashScript(`set -e\nREPO_ROOT=${JSON.stringify(base)}\nbun() { ${JSON.stringify(bun)} "$@"; }\n${block}`, { env: { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' } });
    expect(r.status, r.stderr).toBe(0);
    const marker = spawnSync('git', ['-C', base, 'config', '--bool', '--get', 'gstack.contributor'], { encoding: 'utf-8', timeout: 10_000 });
    expect(marker.stdout.trim()).toBe('true');
    const role = runBashScript(`${sliceFunction(read('setup'), '_deps_role')}\n_deps_role\n`, { cwd: base, env: { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' } });
    expect(role.stdout.trim()).toBe('contributor');
  });
});

// Generated by /ship Step 7 coverage audit (GSTA-97).
// Value: protects=with the real Bun, install_runtime_deps omits devDependencies on a fresh install and keeps the ones a full install already put there;
//   fails_when=Bun starts pruning devDependencies on --production (a later ./setup strips a contributor's toolchain) or --production stops excluding them;
//   why_new=every other test here stubs bun, so the CHANGELOG and CONTRIBUTING claim that ./setup never removes packages was untested;
//   seam=none; no_claim=registry packages or the repo's own bun.lock (two local file: packages stand in; offline)
describe.skipIf(process.platform === 'win32')('install_runtime_deps with the real Bun', () => {
  test('a fresh install leaves out devDependencies; a later production install keeps the ones a full install added', () => {
    const base = tempBase();
    const project = path.join(base, 'project');
    for (const name of ['runtime-pkg', 'dev-pkg']) {
      fs.mkdirSync(path.join(project, 'pkgs', name), { recursive: true });
      fs.writeFileSync(path.join(project, 'pkgs', name, 'package.json'), JSON.stringify({ name, version: '1.0.0' }));
    }
    fs.writeFileSync(path.join(project, 'package.json'), JSON.stringify({
      name: 'prod-deps-fixture', version: '0.0.0',
      dependencies: { 'runtime-pkg': 'file:./pkgs/runtime-pkg' },
      devDependencies: { 'dev-pkg': 'file:./pkgs/dev-pkg' },
    }));
    const env = { ...isolatedBunEnv(base), BUN_INSTALL_CACHE_DIR: path.join(base, 'cache') };
    const setup = read('setup');
    const run = (call: string) => runBashScript(
      `set -e\nBUN_CMD=${JSON.stringify(process.execPath)}\n${depsFunctions(setup)}\n${call}\n`,
      { cwd: project, env, timeout: 90_000 });
    const has = (name: string) => fs.existsSync(path.join(project, 'node_modules', name, 'package.json'));

    const lockfile = run('bun_cmd install');
    expect(lockfile.status, lockfile.stderr).toBe(0);
    const lock = fs.readFileSync(path.join(project, 'bun.lock'), 'utf-8');
    fs.rmSync(path.join(project, 'node_modules'), { recursive: true, force: true });

    const fresh = run('install_runtime_deps');
    expect(fresh.status, fresh.stderr).toBe(0);
    expect([has('runtime-pkg'), has('dev-pkg')]).toEqual([true, false]);

    const full = run('bun_cmd install');
    expect(full.status, full.stderr).toBe(0);
    expect(has('dev-pkg')).toBe(true);

    const later = run('install_runtime_deps');
    expect(later.status, later.stderr).toBe(0);
    expect([has('runtime-pkg'), has('dev-pkg')]).toEqual([true, true]);
    expect(fs.readFileSync(path.join(project, 'bun.lock'), 'utf-8')).toBe(lock);
  });

  // Value: protects=a checkout whose bun.lock is behind package.json (a fork, a local edit) still installs, production-only;
  //   fails_when=the fallback reruns a --production install, which Bun treats as frozen, so setup aborts on the stale lockfile;
  //   why_new=the stub-bun rows cannot model Bun's rule that --production implies a frozen lockfile;
  //   seam=none; no_claim=registry resolution (local file: packages; offline)
  test('a stale lockfile is refreshed, then the install stays production-only', () => {
    const base = tempBase();
    const project = path.join(base, 'project');
    for (const name of ['runtime-pkg', 'late-pkg', 'dev-pkg']) {
      fs.mkdirSync(path.join(project, 'pkgs', name), { recursive: true });
      fs.writeFileSync(path.join(project, 'pkgs', name, 'package.json'), JSON.stringify({ name, version: '1.0.0' }));
    }
    const manifest = (deps: Record<string, string>) => fs.writeFileSync(path.join(project, 'package.json'), JSON.stringify({
      name: 'prod-deps-fixture', version: '0.0.0', dependencies: deps, devDependencies: { 'dev-pkg': 'file:./pkgs/dev-pkg' },
    }));
    const env = { ...isolatedBunEnv(base), BUN_INSTALL_CACHE_DIR: path.join(base, 'cache') };
    const setup = read('setup');
    const run = (call: string) => runBashScript(
      `set -e\nBUN_CMD=${JSON.stringify(process.execPath)}\n${depsFunctions(setup)}\n${call}\n`,
      { cwd: project, env, timeout: 90_000 });
    const has = (name: string) => fs.existsSync(path.join(project, 'node_modules', name, 'package.json'));

    manifest({ 'runtime-pkg': 'file:./pkgs/runtime-pkg' });
    expect(run('bun_cmd install --lockfile-only').status).toBe(0);
    manifest({ 'runtime-pkg': 'file:./pkgs/runtime-pkg', 'late-pkg': 'file:./pkgs/late-pkg' });

    const stale = run('install_runtime_deps');
    expect(stale.status, stale.stderr).toBe(0);
    expect([has('runtime-pkg'), has('late-pkg'), has('dev-pkg')]).toEqual([true, true, false]);
    expect(fs.readFileSync(path.join(project, 'bun.lock'), 'utf-8')).toContain('late-pkg');
  });
});

// #3447: installs made before v1.91.231.0 ran a full bun install, so they carry the dev
// toolchain, and the old rule ("node_modules/typescript means contributor") kept them on it
// forever. The contributor is now the checkout marked with git config gstack.contributor
// (bin/dev-setup) or GSTACK_CONTRIBUTOR=1; every other checkout with dev packages migrates once.
describe.skipIf(process.platform === 'win32')('install_runtime_deps moves older installs to production dependencies', () => {
  const setup = read('setup');
  const SDK = '@anthropic-ai/claude-agent-sdk';

  /** A user checkout as an install made before v1.91.231.0 leaves it: a git clone with the full dev toolchain. */
  function oldInstall(opts: { git?: boolean } = {}): string {
    const base = tempBase();
    if (opts.git !== false) gitInit(base);
    fs.writeFileSync(path.join(base, 'package.json'), JSON.stringify({ name: 'fixture', dependencies: { 'runtime-pkg': '1.0.0' } }, null, 2) + '\n');
    fs.writeFileSync(path.join(base, 'bun.lock'), `{\n  "packages": {\n    "${SDK}": ["${SDK}@0.2.141", "", {}],\n  }\n}\n`);
    for (const dir of ['typescript', SDK, 'runtime-pkg']) {
      fs.mkdirSync(path.join(base, 'node_modules', dir), { recursive: true });
      fs.writeFileSync(path.join(base, 'node_modules', dir, 'package.json'), '{}');
    }
    return base;
  }

  /**
   * A bun for the migration rows: records argv; a --production install recreates node_modules with
   * runtime-pkg unless PROD_RC is nonzero (or PROD_SKIP_DEPS=1 leaves it out); `pm cache` prints
   * $CACHE_DIR; any other install exits 0.
   */
  function migrateBun(base: string): { bun: string; calls: () => string[] } {
    const log = path.join(base, '.calls');
    const bun = path.join(base, '.bun-stub');
    fs.writeFileSync(bun, `#!/usr/bin/env bash
echo "$*" >> ${JSON.stringify(log)}
case "$*" in
  "pm cache") echo "\${CACHE_DIR:-}"; exit 0 ;;
  *--production*)
    [ "\${PROD_RC:-0}" -ne 0 ] && { echo "error: network down" >&2; exit "\${PROD_RC}"; }
    mkdir -p node_modules
    [ "\${PROD_SKIP_DEPS:-0}" = 1 ] || { mkdir -p node_modules/runtime-pkg && echo '{}' > node_modules/runtime-pkg/package.json; }
    exit 0 ;;
esac
exit 0
`, { mode: 0o755 });
    return { bun, calls: () => (fs.existsSync(log) ? fs.readFileSync(log, 'utf-8').trim().split('\n').filter((l) => l !== 'pm cache') : []) };
  }

  const run = (base: string, bun: string, env: Record<string, string> = {}) => {
    const clean = Object.fromEntries(Object.entries(process.env).filter(([k]) => k !== 'CI' && k !== 'GSTACK_CONTRIBUTOR'));
    return runBashScript(`set -e\nBUN_CMD=${JSON.stringify(bun)}\n${depsFunctions(setup)}\ninstall_runtime_deps\n`,
      { cwd: base, env: { ...clean, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1', ...env } });
  };
  const has = (base: string, dir: string) => fs.existsSync(path.join(base, 'node_modules', dir));
  const leftovers = (base: string) => ['node_modules.gstack-migrate', 'node_modules.gstack-migrate.lock'].filter((p) => fs.existsSync(path.join(base, p)));

  // Value: protects=an install made before v1.91.231.0 drops its ~520 MB of dev packages on the next ./setup;
  //   fails_when=setup keeps treating node_modules/typescript as a contributor marker (the pre-#3447 rule);
  //   why_new=no row ran an unmarked checkout that already has the dev toolchain; seam=none; no_claim=real Bun (stub bun)
  test('an unmarked checkout with the dev toolchain migrates to the production install once', () => {
    const base = oldInstall();
    const { bun, calls } = migrateBun(base);
    const r = run(base, bun);
    expect(r.status, r.stderr).toBe(0);
    expect(calls()).toEqual(['install --production --frozen-lockfile']);
    expect([has(base, 'typescript'), has(base, SDK), has(base, 'runtime-pkg')]).toEqual([false, false, true]);
    expect(leftovers(base)).toEqual([]);
    expect(r.stdout).toContain('gstack: moved this install to production dependencies');
    expect(r.stdout).toContain('git config gstack.contributor true, then ./setup again');

    const again = run(base, bun);
    expect(again.status, again.stderr).toBe(0);
    expect(again.stdout).not.toContain('moved this install');
    expect(calls()).toEqual(['install --production --frozen-lockfile', 'install --production --frozen-lockfile']);
  });

  test('a contributor, a CI run and a checkout whose role git cannot read keep the dev toolchain', () => {
    const marked = oldInstall();
    spawnSync('git', ['-C', marked, 'config', '--local', 'gstack.contributor', 'true'], { timeout: 10_000 });
    const envOnly = oldInstall();
    const ci = oldInstall();
    const unreadable = oldInstall();
    const fakeGit = path.join(unreadable, '.fake-bin');
    fs.mkdirSync(fakeGit);
    fs.writeFileSync(path.join(fakeGit, 'git'), '#!/usr/bin/env bash\necho "fatal: detected dubious ownership" >&2\nexit 128\n', { mode: 0o755 });
    for (const [base, env] of [[marked, {}], [envOnly, { GSTACK_CONTRIBUTOR: '1' }], [ci, { CI: 'true' }], [unreadable, { PATH: `${fakeGit}:${process.env.PATH}` }]] as const) {
      const { bun, calls } = migrateBun(base);
      const r = run(base, bun, env);
      expect(r.status, r.stderr).toBe(0);
      expect(calls()).toEqual(['install --frozen-lockfile']);
      expect([has(base, 'typescript'), has(base, SDK)]).toEqual([true, true]);
      expect(leftovers(base)).toEqual([]);
    }
    const marker = spawnSync('git', ['-C', marked, 'config', '--bool', 'gstack.contributor', 'false'], { timeout: 10_000 });
    expect(marker.status).toBe(0);
    const { bun } = migrateBun(marked);
    expect(run(marked, bun).status).toBe(0);
    expect(has(marked, 'typescript')).toBe(false);
  });

  test('a fresh CI checkout still gets the production install', () => {
    const base = tempBase();
    gitInit(base);
    fs.writeFileSync(path.join(base, 'package.json'), JSON.stringify({ name: 'fixture', dependencies: { 'runtime-pkg': '1.0.0' } }, null, 2) + '\n');
    const { bun, calls } = migrateBun(base);
    expect(run(base, bun, { CI: 'true' }).status).toBe(0);
    expect(calls()).toEqual(['install --production --frozen-lockfile']);
  });

  // Value: protects=an offline upgrade never leaves a user without node_modules;
  //   fails_when=the migration deletes the old node_modules before the production install is proven, or swallows the failure;
  //   why_new=the success row cannot fail the install; seam=none; no_claim=real network failures (stub bun)
  test('a failed or unverified production install puts the full install back and says why', () => {
    for (const env of [{ PROD_RC: '7' }, { PROD_SKIP_DEPS: '1' }] as Record<string, string>[]) {
      const base = oldInstall();
      const { bun, calls } = migrateBun(base);
      const r = run(base, bun, env);
      expect(r.status, r.stderr).toBe(0);
      expect([has(base, 'typescript'), has(base, SDK), has(base, 'runtime-pkg')]).toEqual([true, true, true]);
      expect(leftovers(base)).toEqual([]);
      expect(r.stderr).toContain('SETUP_DEPS_MIGRATE_FAILED');
      expect(r.stderr).toContain('Nothing was removed');
      expect(calls().at(-1)).toBe('install --frozen-lockfile');
    }
    const offline = oldInstall();
    expect(run(offline, migrateBun(offline).bun, { PROD_RC: '7' }).stderr).toContain('error: network down');
  });

  test('a backup left by an interrupted migration is recovered on the next run', () => {
    const moved = oldInstall();
    fs.renameSync(path.join(moved, 'node_modules'), path.join(moved, 'node_modules.gstack-migrate'));
    const stub = migrateBun(moved);
    expect(run(moved, stub.bun).status).toBe(0);
    expect([has(moved, 'typescript'), has(moved, 'runtime-pkg')]).toEqual([false, true]);
    expect(leftovers(moved)).toEqual([]);

    const finished = oldInstall();
    fs.renameSync(path.join(finished, 'node_modules'), path.join(finished, 'node_modules.gstack-migrate'));
    fs.mkdirSync(path.join(finished, 'node_modules', 'runtime-pkg'), { recursive: true });
    fs.writeFileSync(path.join(finished, 'node_modules', 'runtime-pkg', 'package.json'), '{}');
    expect(run(finished, migrateBun(finished).bun).status).toBe(0);
    expect(has(finished, 'typescript')).toBe(false);
    expect(leftovers(finished)).toEqual([]);
  });

  test('a live migration lock skips the migration; a dead holder is reclaimed', () => {
    const busy = oldInstall();
    fs.mkdirSync(path.join(busy, 'node_modules.gstack-migrate.lock'));
    fs.writeFileSync(path.join(busy, 'node_modules.gstack-migrate.lock', 'pid'), String(process.pid));
    const b = migrateBun(busy);
    expect(run(busy, b.bun).status).toBe(0);
    expect(b.calls()).toEqual(['install --frozen-lockfile']);
    expect(has(busy, 'typescript')).toBe(true);

    const stale = oldInstall();
    fs.mkdirSync(path.join(stale, 'node_modules.gstack-migrate.lock'));
    fs.writeFileSync(path.join(stale, 'node_modules.gstack-migrate.lock', 'pid'), '2147483646');
    expect(run(stale, migrateBun(stale).bun).status).toBe(0);
    expect(has(stale, 'typescript')).toBe(false);
    expect(leftovers(stale)).toEqual([]);
  });

  // Value: protects=another project's install that reads the Agent SDK out of Bun's shared cache (Bun's symlink
  //   backend links each file there with no extra hardlink) keeps working after a gstack migration;
  //   fails_when=the migration deletes Bun cache entries; why_new=a review reproduced a symlink-backend consumer
  //   losing its package.json when cache entries with no extra hardlinks were deleted; seam=none; no_claim=Bun's cache layout
  test('the migration never touches Bun\'s shared cache', () => {
    const base = oldInstall();
    const cache = path.join(base, '.cache');
    const entry = path.join(cache, '@anthropic-ai', '[email protected]@@@1');
    fs.mkdirSync(entry, { recursive: true });
    fs.writeFileSync(path.join(entry, 'cli.js'), 'x');
    const consumer = path.join(base, '.symlink-project', 'node_modules', 'sdk');
    fs.mkdirSync(consumer, { recursive: true });
    fs.symlinkSync(path.join(entry, 'cli.js'), path.join(consumer, 'cli.js'));
    const { bun, calls } = migrateBun(base);
    const r = run(base, bun, { CACHE_DIR: cache });
    expect(r.status, r.stderr).toBe(0);
    expect(has(base, 'typescript')).toBe(false);
    expect(calls()).toEqual(['install --production --frozen-lockfile']);
    expect(fs.readFileSync(path.join(consumer, 'cli.js'), 'utf-8')).toBe('x');
  });

  test('a checkout outside git with the dev toolchain is a user install', () => {
    const base = oldInstall({ git: false });
    expect(run(base, migrateBun(base).bun).status).toBe(0);
    expect(has(base, 'typescript')).toBe(false);
  });
});

// Value: protects=with the real Bun, an older full install loses its dev packages and a marked contributor keeps them;
//   fails_when=the production install after the move resolves differently from a fresh one, or the marker read breaks;
//   why_new=the migration rows stub bun; seam=none; no_claim=registry packages (local file: packages; offline)
describe.skipIf(process.platform === 'win32')('the migration with the real Bun', () => {
  test('an unmarked full install loses its dev packages; a marked one keeps them across two runs', () => {
    for (const marked of [false, true]) {
      const base = tempBase();
      const project = path.join(base, 'project');
      for (const name of ['runtime-pkg', 'typescript']) {
        fs.mkdirSync(path.join(project, 'pkgs', name), { recursive: true });
        fs.writeFileSync(path.join(project, 'pkgs', name, 'package.json'), JSON.stringify({ name, version: '1.0.0' }));
      }
      fs.writeFileSync(path.join(project, 'package.json'), JSON.stringify({
        name: 'migrate-fixture', version: '0.0.0',
        dependencies: { 'runtime-pkg': 'file:./pkgs/runtime-pkg' },
        devDependencies: { typescript: 'file:./pkgs/typescript' },
      }, null, 2) + '\n');
      gitInit(project);
      if (marked) spawnSync('git', ['-C', project, 'config', '--local', 'gstack.contributor', 'true'], { timeout: 10_000 });
      const env = { ...isolatedBunEnv(base), BUN_INSTALL_CACHE_DIR: path.join(base, 'cache'), GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' };
      delete (env as Record<string, string | undefined>).CI;
      delete (env as Record<string, string | undefined>).GSTACK_CONTRIBUTOR;
      const setup = read('setup');
      const runIt = (call: string) => runBashScript(`set -e\nBUN_CMD=${JSON.stringify(process.execPath)}\n${depsFunctions(setup)}\n${call}\n`, { cwd: project, env, timeout: 90_000 });
      const has = (name: string) => fs.existsSync(path.join(project, 'node_modules', name, 'package.json'));
      const full = runIt('bun_cmd install');
      expect(full.status, full.stderr).toBe(0);
      expect(has('typescript')).toBe(true);
      for (let i = 0; i < 2; i++) {
        const r = runIt('install_runtime_deps');
        expect(r.status, r.stderr).toBe(0);
        expect([has('runtime-pkg'), has('typescript')]).toEqual([true, marked]);
      }
      expect(fs.existsSync(path.join(project, 'node_modules.gstack-migrate'))).toBe(false);
    }
  });
});

/** Bare package names a source file loads (import/require/dynamic import), type-only imports excluded. */
function importedPackages(text: string): string[] {
  const out = new Set<string>();
  const patterns = [
    /^\s*(?:import|export)\s+(?!type\b)[^'";]*?from\s+['"]([^'"]+)['"]/gm,
    /^\s*import\s+['"]([^'"]+)['"]/gm,
    /\brequire\(\s*['"]([^'"]+)['"]\s*\)/g,
    /\bimport\(\s*['"]([^'"]+)['"]\s*\)/g,
  ];
  for (const re of patterns) {
    for (const m of text.matchAll(re)) {
      const spec = m[1];
      if (spec.startsWith('.') || spec.startsWith('/') || spec.startsWith('node:') || spec === 'bun' || spec.startsWith('bun:')) continue;
      const name = spec.startsWith('@') ? spec.split('/').slice(0, 2).join('/') : spec.split('/')[0];
      if (builtinModules.includes(name)) continue;
      out.add(name);
    }
  }
  return [...out];
}

/** Every shipped-runtime file that loads a package which is not a production dependency. */
function nonProductionImports(pkg: { dependencies?: Record<string, string> }, files: Record<string, string>): string[] {
  const deps = pkg.dependencies ?? {};
  const bad: string[] = [];
  for (const [file, text] of Object.entries(files)) {
    for (const name of importedPackages(text)) if (!(name in deps)) bad.push(`${file}: ${name}`);
  }
  return bad.sort();
}

/** The code setup, the build and installed skills run: shipped dirs plus the scripts setup and the build call by path. */
function runtimeFiles(): Record<string, string> {
  const roots = ['bin/', 'lib/', 'browse/src/', 'design/src/', 'make-pdf/src/', 'hosts/', 'extension/', 'scripts/resolvers/'];
  const tracked = spawnSync('git', ['ls-files'], { cwd: ROOT, encoding: 'utf-8', timeout: 30_000 }).stdout
    .split('\n').filter((rel) => roots.some((r) => rel.startsWith(r)));
  const ownPackage = (rel: string) => rel.startsWith('lib/diagram-render/');
  const invoked = new Set(['scripts/gen-skill-docs.ts']);
  for (const src of ['setup', 'scripts/build.sh']) for (const m of read(src).matchAll(/scripts\/[A-Za-z0-9_./-]+\.ts/g)) invoked.add(m[0]);
  const files: Record<string, string> = {};
  for (const rel of [...tracked, ...invoked]) {
    if (ownPackage(rel) || /(^|\/)(test|node_modules|dist)\//.test(rel) || /\.(test\.ts|md|json|png|svg|css|html|c|h)$/.test(rel)) continue;
    const abs = path.join(ROOT, rel);
    if (!fs.existsSync(abs) || !fs.statSync(abs).isFile() || fs.statSync(abs).size > 2_000_000) continue;
    const text = fs.readFileSync(abs, 'utf-8');
    if (text.includes('\u0000')) continue;
    files[rel] = text;
  }
  return files;
}

describe('setup and runtime need only production dependencies', () => {
  test('no shipped code loads a devDependency or an undeclared package', () => {
    const files = runtimeFiles();
    expect(Object.keys(files).length).toBeGreaterThan(300);
    expect(nonProductionImports(PKG, files)).toEqual([]);
  });

  // Generated by /ship Step 7 coverage audit (GSTA-97).
  // Value: protects=modules the shipped entry points load by relative import (gen-skill-docs' scripts/ helpers) use production dependencies only;
  //   fails_when=a scripts/ module gen-skill-docs imports adds a devDependency import, breaking every user render;
  //   why_new=runtimeFiles() scans fixed roots, and scripts/discover-skills.ts, gen-agents-digest.ts and the rest are outside them;
  //   seam=none; no_claim=dynamic import paths built at runtime, or packages loaded by a spawned subprocess
  test('every module the shipped code reaches by relative import loads only production dependencies', () => {
    const files = runtimeFiles();
    const reached: Record<string, string> = {};
    const queue = Object.keys(files);
    const relSpec = /^\s*(?:import|export)\s+(?!type\b)[^'";]*?from\s+['"](\.\.?\/[^'"]+)['"]|\b(?:import|require)\(\s*['"](\.\.?\/[^'"]+)['"]\s*\)|^\s*import\s+['"](\.\.?\/[^'"]+)['"]/gm;
    while (queue.length) {
      const rel = queue.pop()!;
      const text = files[rel] ?? reached[rel];
      for (const m of text.matchAll(relSpec)) {
        const spec = path.posix.normalize(path.posix.join(path.posix.dirname(rel), m[1] ?? m[2] ?? m[3]));
        const target = [spec, `${spec}.ts`, `${spec}.js`, `${spec}/index.ts`].find((c) => fs.existsSync(path.join(ROOT, c)) && fs.statSync(path.join(ROOT, c)).isFile());
        if (!target || target.startsWith('..') || target in files || target in reached || /(^|\/)node_modules\//.test(target) || (/(^|\/)test\//.test(target) && !target.startsWith('test/helpers/')) || !/\.(ts|js|mjs)$/.test(target)) continue;
        reached[target] = fs.readFileSync(path.join(ROOT, target), 'utf-8');
        queue.push(target);
      }
    }
    expect(Object.keys(reached)).toContain('scripts/gen-agents-digest.ts');
    expect(Object.keys({ ...files, ...reached })).toContain('scripts/discover-skills.ts');
    expect(Object.keys(reached)).toContain('test/helpers/providers/claude.ts');
    expect(nonProductionImports(PKG, reached)).toEqual([]);
  });

  test('negative controls: a devDependency import and a moved-back runtime package are caught', () => {
    expect(nonProductionImports(PKG, { 'bin/x': "import { query } from '@anthropic-ai/claude-agent-sdk';" })).toEqual(['bin/x: @anthropic-ai/claude-agent-sdk']);
    expect(nonProductionImports(PKG, { 'bin/x': "import type { Q } from '@anthropic-ai/claude-agent-sdk';\nimport fs from 'node:fs';" })).toEqual([]);
    expect(nonProductionImports(PKG, { 'lib/x.ts': "export { query } from '@anthropic-ai/claude-agent-sdk';" })).toEqual(['lib/x.ts: @anthropic-ai/claude-agent-sdk']);
    expect(nonProductionImports(PKG, { 'lib/x.ts': "export * from 'typescript';" })).toEqual(['lib/x.ts: typescript']);
    expect(nonProductionImports(PKG, { 'lib/x.ts': "export type { Q } from '@anthropic-ai/claude-agent-sdk';" })).toEqual([]);
    // Generated by /ship Step 7 coverage audit (GSTA-97).
    // Value: protects=the scan sees an import statement split over several lines;
    //   fails_when=importedPackages only matches one-line imports, so a multi-line devDependency import ships unflagged;
    //   why_new=both existing controls are one-line imports; seam=none; no_claim=none beyond protects
    expect(nonProductionImports(PKG, { 'bin/x': "import {\n  query,\n  type SDKMessage,\n} from '@anthropic-ai/claude-agent-sdk';" })).toEqual(['bin/x: @anthropic-ai/claude-agent-sdk']);
    const { ['@anthropic-ai/sdk']: _sdk, ...rest } = PKG.dependencies;
    const files = runtimeFiles();
    expect(nonProductionImports({ dependencies: rest }, files)).toContain('bin/gstack-distill-free-text: @anthropic-ai/sdk');
  });

  test('packages the build copies out of node_modules are production dependencies', () => {
    const scripts: Record<string, string> = PKG.scripts;
    const run = [...read('scripts/build.sh').matchAll(/"\$BUN_CMD" run ([A-Za-z0-9:_-]+)/g)].map((m) => m[1]);
    expect(run).toContain('vendor:xterm');
    const copied = run.flatMap((name) => [...(scripts[name] ?? '').matchAll(/node_modules\/((?:@[^/\s]+\/)?[^/\s]+)\//g)].map((m) => m[1]));
    expect(copied.sort()).toEqual(['xterm', 'xterm', 'xterm-addon-fit']);
    for (const name of copied) expect(PKG.dependencies).toHaveProperty([name]);
  });

  // Generated by /ship Step 7 coverage audit (GSTA-97).
  // Value: protects=a production-only install never creates a dev-toolchain package _deps_has_dev reads, so a user's next ./setup stays production-only;
  //   fails_when=a production dependency, or a package it pulls in (required peers included), starts depending on the marker package, so every repeat ./setup goes full;
  //   why_new=the real-Bun rows use local fixture packages, and nothing walks the repo's own bun.lock production closure;
  //   seam=none; no_claim=Bun's actual hoisting or platform filtering (a static, name-based walk of bun.lock that counts every candidate entry)
  test('the production closure in bun.lock never installs the dev-toolchain packages setup checks', () => {
    const markers = [...sliceFunction(read('setup'), '_deps_has_dev').matchAll(/\[ -d node_modules\/([^\s\]]+) \]/g)].map((m) => m[1]);
    expect(markers).toEqual(['typescript', '@anthropic-ai/claude-agent-sdk']);
    for (const marker of markers) expect(PKG.devDependencies).toHaveProperty([marker]);
    type Lock = { workspaces: Record<string, { dependencies?: Record<string, string> }>; packages: Record<string, unknown[]> };
    const closure = (lock: Lock): Set<string> => {
      const seen = new Set<string>();
      const queue = Object.keys(lock.workspaces[''].dependencies ?? {});
      while (queue.length) {
        const name = queue.pop()!;
        if (seen.has(name)) continue;
        seen.add(name);
        for (const [key, entry] of Object.entries(lock.packages)) {
          if (key !== name && !(key.endsWith(`/${name}`) && key.slice(0, -name.length - 1) in lock.packages)) continue;
          const meta = (entry.find((e) => e && typeof e === 'object' && !Array.isArray(e)) ?? {}) as Record<string, any>;
          const optionalPeers = new Set<string>(meta.optionalPeers ?? []);
          for (const field of ['dependencies', 'optionalDependencies', 'peerDependencies']) {
            for (const dep of Object.keys(meta[field] ?? {})) if (field !== 'peerDependencies' || !optionalPeers.has(dep)) queue.push(dep);
          }
        }
      }
      return seen;
    };
    const lock: Lock = JSON.parse(read('bun.lock').replace(/,(\s*[}\]])/g, '$1'));
    const production = closure(lock);
    expect(production.has('playwright-core')).toBe(true);
    for (const marker of markers) expect(production.has(marker), marker).toBe(false);
    // Negative controls: a required peer on a marker is caught, through a nested entry too; an optional peer is not.
    const synthetic = (meta: Record<string, unknown>): Lock => ({
      workspaces: { '': { dependencies: { a: '1' } } },
      packages: { a: ['a@1', '', { dependencies: { b: '1' } }, 'sha'], b: ['b@1', '', {}, 'sha'], 'a/b': ['b@2', '', meta, 'sha'] },
    });
    for (const marker of markers) {
      expect(closure(synthetic({ peerDependencies: { [marker]: '*' } })).has(marker)).toBe(true);
      expect(closure(synthetic({ peerDependencies: { [marker]: '*' }, optionalPeers: [marker] })).has(marker)).toBe(false);
    }
  });

  test('the dev toolchain stays out of the user install', () => {
    for (const name of ['@anthropic-ai/claude-agent-sdk', 'typescript', '@types/bun']) {
      expect(PKG.devDependencies).toHaveProperty([name]);
      expect(PKG.dependencies).not.toHaveProperty([name]);
    }
  });
});