/** * #4704: gstack-relink renders the tracked root SKILL.md in place when * disabled_skills is set (C8, #3018), so the router's generated bytes are * gstack's own install state, not a local edit. gstack-upgrade must not refuse * over them, must still refuse over any real edit to SKILL.md or another * tracked file (nothing changed on refusal), and must leave the router * rendered for the same disabled list after it moves the checkout. * Fixture: a bare remote with A (1.0.0) and B (1.0.1, which also changes the * committed router), a depth-1 clone at A under the simulated Capy host's temp * root, the real gstack-config and gstack-relink, and an untracked `scripts` * link to this checkout's generator, which reads templates there and writes * only to its own temp out-dir. */ import { afterEach, describe, expect, test } from 'bun:test'; import { spawnSync } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; import { capyHost, type CapyHost } from './helpers/capy-host'; const REPO = path.resolve(import.meta.dir, '..'); const DEPS = [ 'bin/gstack-upgrade', 'bin/gstack-bun-version.sh', 'bin/gstack-paths', 'bin/gstack-state-root.sh', 'bin/gstack-lock.sh', 'bin/gstack-egress-lib.sh', 'bin/gstack-egress-receipt', 'lib/egress-receipt.ts', 'lib/state-root.ts', 'bin/gstack-lease', 'bin/gstack-harness-pid.sh', 'bin/gstack-install-verdict.sh', 'bin/gstack-ff-update', 'bin/gstack-config', 'bin/gstack-relink', 'bin/gstack-patch-names', 'bin/gstack-skill-name.sh', 'bin/gstack-install-registry.sh', ]; const DISABLED_LINE = 'Disabled on this install (never invoke or suggest them): `/qa-only`, `/careful`.'; const hosts: CapyHost[] = []; afterEach(() => { for (const h of hosts.splice(0)) h.cleanup(); }); function fixture() { const host = capyHost({ systemBun: '1.4.2' }); hosts.push(host); const env = { ...host.env, UPGRADE_LOG: path.join(host.base, 'calls.log'), GSTACK_SETUP_RUNNING: '' }; const git = (args: string[], cwd: string) => { const r = spawnSync('git', args, { cwd, encoding: 'utf8', env, timeout: 30_000 }); if (r.status !== 0) throw new Error(`git ${args.join(' ')}: ${r.stderr}`); return r.stdout.trim(); }; const write = (file: string, text: string, mode = 0o644) => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, text, { mode }); }; const seed = path.join(host.base, 'seed'); const bare = path.join(host.base, 'remote.git'); fs.mkdirSync(seed, { recursive: true }); git(['init', '-q', '-b', 'main'], seed); for (const rel of DEPS) { fs.mkdirSync(path.dirname(path.join(seed, rel)), { recursive: true }); fs.copyFileSync(path.join(REPO, rel), path.join(seed, rel)); fs.chmodSync(path.join(seed, rel), fs.statSync(path.join(REPO, rel)).mode); } const committed = fs.readFileSync(path.join(REPO, 'SKILL.md'), 'utf8'); write(path.join(seed, 'SKILL.md'), committed); write(path.join(seed, 'VERSION'), '1.0.0\n'); write(path.join(seed, 'README.md'), 'gstack fixture\n'); write(path.join(seed, 'setup'), '#!/usr/bin/env bash\necho "setup $*" >> "$UPGRADE_LOG"\n', 0o755); for (const skill of ['qa', 'qa-only', 'careful', 'guard']) write(path.join(seed, skill, 'SKILL.md'), `---\nname: ${skill}\ndescription: test\n---\n`); git(['add', '-A'], seed); git(['commit', '-qm', 'A'], seed); git(['init', '-q', '--bare', '-b', 'main', bare], host.base); git(['push', '-q', bare, 'main'], seed); const skills = path.join(host.base, 'skills'); const root = path.join(skills, 'gstack'); fs.mkdirSync(skills, { recursive: true }); git(['clone', '-q', '--single-branch', '--depth', '1', `file://${bare}`, root], host.base); fs.symlinkSync(path.join(REPO, 'scripts'), path.join(root, 'scripts')); write(path.join(seed, 'VERSION'), '1.0.1\n'); write(path.join(seed, 'NEW-IN-B.md'), 'added in B\n'); write(path.join(seed, 'SKILL.md'), committed.replace('\n## Route first\n', '\n## Route first\n\n\n')); git(['add', '-A'], seed); git(['commit', '-qm', 'B'], seed); const b = git(['rev-parse', 'HEAD'], seed); git(['push', '-q', bare, 'main'], seed); const routerEnv = { ...env, GSTACK_INSTALL_DIR: root, GSTACK_SKILLS_DIR: skills }; return { root, b, committed, git: (args: string[]) => git(args, root), router: () => fs.readFileSync(path.join(root, 'SKILL.md'), 'utf8'), config: (key: string, op: string, value: string, extra: Record = {}) => spawnSync('bash', [path.join(root, 'bin/gstack-config'), key, op, value], { cwd: host.home, encoding: 'utf8', env: { ...routerEnv, ...extra }, timeout: 60_000 }), relink: () => spawnSync('bash', [path.join(root, 'bin/gstack-relink')], { cwd: host.home, encoding: 'utf8', env: routerEnv, timeout: 60_000 }), upgrade: (args: string[] = []) => spawnSync('bash', [path.join(root, 'bin/gstack-upgrade'), ...args], { cwd: host.home, encoding: 'utf8', env, timeout: 120_000 }), }; } /** disabled_skills set through gstack-config, which runs gstack-relink: the router is rendered in place and shows as modified. */ function disabledFixture() { const f = fixture(); const set = f.config('set', 'disabled_skills', 'qa-only,careful'); expect(set.status, set.stdout + set.stderr).toBe(0); expect(f.router()).toContain(DISABLED_LINE); expect(f.git(['status', '--porcelain', '--untracked-files=no'])).toBe('M SKILL.md'); return f; } describe.skipIf(process.platform === 'win32')('#4704: gstack-upgrade and the router gstack-relink renders for disabled_skills', () => { test('the rendered router does not block the upgrade, and the upgraded router still leaves the disabled skills out', () => { const f = disabledFixture(); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(0); expect(r.stdout).toContain('gstack-upgrade: upgraded 1.0.0 -> 1.0.1'); expect(f.git(['rev-parse', 'HEAD'])).toBe(f.b); const router = f.router(); expect(router).toContain(DISABLED_LINE); expect(router).not.toMatch(/→ invoke `\/qa-only`/); expect(router).toContain('→ invoke `/guard`'); expect(f.git(['status', '--porcelain', '--untracked-files=no'])).toBe('M SKILL.md'); }); test('--ff-only-no-reinstall also re-renders the router, since no setup runs after it', () => { const f = disabledFixture(); const r = f.upgrade(['--ff-only-no-reinstall']); expect(r.status, r.stdout + r.stderr).toBe(0); expect(f.git(['rev-parse', 'HEAD'])).toBe(f.b); expect(f.router()).toContain(DISABLED_LINE); }); test('a router rendered for plan_mode_preamble=off does not block the upgrade and stays rendered', () => { const f = fixture(); const set = f.config('set', 'plan_mode_preamble', 'off'); expect(set.status, set.stdout + set.stderr).toBe(0); const relink = f.relink(); expect(relink.status, relink.stdout + relink.stderr).toBe(0); expect(f.git(['status', '--porcelain', '--untracked-files=no'])).toBe('M SKILL.md'); const rendered = f.router(); expect(rendered).not.toContain('## Plan Mode Safe Operations'); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(0); expect(f.git(['rev-parse', 'HEAD'])).toBe(f.b); expect(f.router()).toBe(rendered); }); test('a fast-forward that fails after the router was restored renders it again before refusing', () => { const f = disabledFixture(); const rendered = f.router(); fs.writeFileSync(path.join(f.root, 'NEW-IN-B.md'), 'untracked local file\n'); const head = f.git(['rev-parse', 'HEAD']); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(3); expect(r.stderr).toContain('git merge --ff-only FETCH_HEAD failed'); expect(f.git(['rev-parse', 'HEAD'])).toBe(head); expect(f.router()).toBe(rendered); }); test('a user edit on top of the rendered router still refuses, and nothing changes', () => { const f = disabledFixture(); const edited = f.router() + '\nmy own routing note\n'; fs.writeFileSync(path.join(f.root, 'SKILL.md'), edited); const head = f.git(['rev-parse', 'HEAD']); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(3); expect(r.stderr).toContain('gstack-upgrade: tracked files changed: SKILL.md; nothing was changed'); expect(f.git(['rev-parse', 'HEAD'])).toBe(head); expect(f.router()).toBe(edited); }); test('a mode change on the rendered router still refuses', () => { const f = disabledFixture(); fs.chmodSync(path.join(f.root, 'SKILL.md'), 0o755); const head = f.git(['rev-parse', 'HEAD']); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(3); expect(r.stderr).toContain('tracked files changed: SKILL.md'); expect(f.git(['rev-parse', 'HEAD'])).toBe(head); expect(fs.statSync(path.join(f.root, 'SKILL.md')).mode & 0o111).not.toBe(0); }); test('an edit to SKILL.md with no disabled_skills set still refuses', () => { const f = fixture(); fs.writeFileSync(path.join(f.root, 'SKILL.md'), f.committed + '\nmy own routing note\n'); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(3); expect(r.stderr).toContain('tracked files changed: SKILL.md'); }); test('another tracked edit beside the rendered router still refuses and leaves the router rendered', () => { const f = disabledFixture(); const rendered = f.router(); fs.writeFileSync(path.join(f.root, 'README.md'), 'local edit\n'); const head = f.git(['rev-parse', 'HEAD']); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(3); expect(r.stderr).toContain('tracked files changed: README.md'); expect(f.git(['rev-parse', 'HEAD'])).toBe(head); expect(f.router()).toBe(rendered); }); test('a router rendered for an older disabled list (config changed with no relink) still refuses', () => { const f = disabledFixture(); const rendered = f.router(); const set = f.config('set', 'disabled_skills', 'qa-only', { GSTACK_SETUP_RUNNING: '1' }); expect(set.status, set.stdout + set.stderr).toBe(0); expect(f.router()).toBe(rendered); const r = f.upgrade(); expect(r.status, r.stdout + r.stderr).toBe(3); expect(r.stderr).toContain('tracked files changed: SKILL.md'); expect(f.router()).toBe(rendered); }); });