Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(upgrade): stop reporting success when a pinned bun install did not move (#5311)
garrytanbd63e6acb1e0
diff --git a/src/commands/upgrade.ts b/src/commands/upgrade.tsindex 2abf6d77d8..0f50adba00 100644--- a/src/commands/upgrade.ts+++ b/src/commands/upgrade.ts@@ -181,6 +181,30 @@ export async function runUpgrade(args: string[], opts: { targetVersion?: stringsetCliExitVerdict(1);return;}+ // #5311: a bare `gbrain upgrade` has no target, so the check above cannot+ // fire, and on an exact-tag Git pin `bun update` is a successful no-op.+ // When the version did not change and the global install pins a tag, say+ // so and exit non-zero instead of reporting an upgrade.+ if (!target && method === 'bun' && newVersion && newVersion === oldVersion) {+ const pin = bunGlobalExactTagPin(resolveBunGlobalRoot());+ if (pin) {+ const { pendingUpgradeVersion } = await import('../core/self-upgrade.ts');+ const latest = pendingUpgradeVersion(oldVersion);+ const reinstall = latest ? `bun add -g github:garrytan/gbrain#v${latest}` : 'bun add -g github:garrytan/gbrain';+ console.error(`Upgrade did not take effect: still running ${newVersion}, because the global install is pinned to ${pin} and \`bun update\` keeps a pinned tag.`);+ console.error('Reinstall to move off the pin:');+ console.error(` ${reinstall}`);+ recordUpgradeError({+ phase: 'verify-pin',+ fromVersion: oldVersion,+ toVersion: latest ?? 'latest',+ error: `pinned to ${pin}; still running ${newVersion} after upgrade`,+ hint: reinstall,+ });+ setCliExitVerdict(1);+ return;+ }+ }// Save old version for post-upgrade migration detectionsaveUpgradeState(oldVersion, newVersion);@@ -290,6 +314,21 @@ export function resolveBunGlobalRoot(): string {return installRoot ?? defaultRoot;}+/**+ * #5311: the exact Git tag the bun global install pins gbrain to+ * (`github:garrytan/gbrain#v0.51.0`), or null for an unpinned or branch spec.+ */+export function bunGlobalExactTagPin(globalRoot: string): string | null {+ try {+ const pkg = JSON.parse(readFileSync(join(globalRoot, 'package.json'), 'utf-8')) as { dependencies?: Record<string, string> };+ const spec = pkg.dependencies?.gbrain;+ if (typeof spec !== 'string') return null;+ return /#v?\d+\.\d+(\.\d+)*$/.test(spec.trim()) ? spec.trim() : null;+ } catch {+ return null;+ }+}+function isBunGlobalRoot(dir: string): boolean {return existsSync(join(dir, 'package.json')) && existsSync(join(dir, 'node_modules'));}diff --git a/test/upgrade-exact-tag-pin.serial.test.ts b/test/upgrade-exact-tag-pin.serial.test.tsnew file mode 100644index 0000000000..a7fd3b4265--- /dev/null+++ b/test/upgrade-exact-tag-pin.serial.test.ts@@ -0,0 +1,90 @@+/**+ * #5311: bare `gbrain upgrade` reported success on an exact-tag Bun Git+ * install. `bun update` keeps a pinned tag (exit 0, nothing changes) and the+ * #4366 mismatch check only runs when a caller passes a target version, so the+ * user saw "Upgrade complete" while still on the old version.+ *+ * SERIAL + subprocess: detectInstallMethod keys on argv[1] living under+ * node_modules and the `bun update` / `gbrain --version` calls must hit PATH+ * shims, which Bun snapshots at process start.+ */++import { describe, test, expect } from 'bun:test';+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';+import { join, resolve } from 'path';+import { tmpdir } from 'os';++import { bunGlobalExactTagPin } from '../src/commands/upgrade.ts';+import { VERSION } from '../src/version.ts';++const REPO = resolve(import.meta.dir, '..');++async function runBareUpgrade(spec: string, observedVersion: string): Promise<{ home: string; exitCode: number; stdout: string; stderr: string }> {+ const home = mkdtempSync(join(tmpdir(), 'gbrain-5311-'));+ const bin = join(home, 'bin');+ mkdirSync(bin);+ // `bun update` succeeds without changing anything (the pinned-tag no-op);+ // `gbrain --version` keeps reporting the observed version.+ writeFileSync(join(bin, 'bun'), `#!/bin/sh\necho "$*" >> '${join(home, 'bun-calls.log')}'\nexit 0\n`);+ writeFileSync(join(bin, 'gbrain'), `#!/bin/sh\necho "gbrain ${observedVersion}"\n`);+ chmodSync(join(bin, 'bun'), 0o755);+ chmodSync(join(bin, 'gbrain'), 0o755);+ const globalRoot = join(home, '.bun', 'install', 'global');+ const pkgDir = join(globalRoot, 'node_modules', 'gbrain');+ mkdirSync(pkgDir, { recursive: true });+ writeFileSync(join(globalRoot, 'package.json'), JSON.stringify({ dependencies: { gbrain: spec } }));+ writeFileSync(join(pkgDir, 'package.json'), JSON.stringify({ name: 'gbrain', repository: 'github:garrytan/gbrain' }));+ const driver = join(pkgDir, 'driver.ts');+ writeFileSync(driver, `import { runUpgrade } from '${REPO}/src/commands/upgrade.ts';\nawait runUpgrade(['--swap-only']);\n`);+ const env: Record<string, string | undefined> = { ...process.env, HOME: home, GBRAIN_HOME: home, PATH: `${bin}:${process.env.PATH}` };+ delete env.BUN_INSTALL;+ const proc = Bun.spawn([process.execPath, 'run', driver], { cwd: REPO, env, stdout: 'pipe', stderr: 'pipe' });+ const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]);+ return { home, exitCode: await proc.exited, stdout, stderr };+}++describe('#5311 bare upgrade on an exact-tag pin', () => {+ test('bunGlobalExactTagPin recognizes tag pins only', () => {+ const root = mkdtempSync(join(tmpdir(), 'gbrain-5311-pin-'));+ try {+ const pin = (spec: string | null) => {+ writeFileSync(join(root, 'package.json'), JSON.stringify({ dependencies: spec === null ? {} : { gbrain: spec } }));+ return bunGlobalExactTagPin(root);+ };+ expect(pin('github:garrytan/gbrain#v0.51.6.0')).toBe('github:garrytan/gbrain#v0.51.6.0');+ expect(pin('github:garrytan/gbrain#0.51.6')).toBe('github:garrytan/gbrain#0.51.6');+ expect(pin('github:garrytan/gbrain')).toBeNull();+ expect(pin('github:garrytan/gbrain#master')).toBeNull();+ expect(pin(null)).toBeNull();+ } finally {+ rmSync(root, { recursive: true, force: true });+ }+ });++ test('an unchanged version on a pinned tag exits non-zero with the reinstall command', async () => {+ const r = await runBareUpgrade(`github:garrytan/gbrain#v${VERSION}`, VERSION);+ try {+ expect(r.exitCode).toBe(1);+ expect(r.stderr).toContain('Upgrade did not take effect');+ expect(r.stderr).toContain(`pinned to github:garrytan/gbrain#v${VERSION}`);+ expect(r.stderr).toContain('bun add -g github:garrytan/gbrain');+ expect(readFileSync(join(r.home, 'bun-calls.log'), 'utf-8')).toContain('update gbrain');+ // No false "just upgraded" breadcrumb for an upgrade that never happened.+ expect(existsSync(join(r.home, '.gbrain', 'just-upgraded-from'))).toBe(false);+ const errors = readFileSync(join(r.home, '.gbrain', 'upgrade-errors.jsonl'), 'utf-8').trim().split('\n').map(l => JSON.parse(l));+ expect(errors.some(e => e.phase === 'verify-pin' && e.hint.startsWith('bun add -g github:garrytan/gbrain'))).toBe(true);+ } finally {+ rmSync(r.home, { recursive: true, force: true });+ }+ }, 60_000);++ test('an unpinned install that is already current still succeeds', async () => {+ const r = await runBareUpgrade('github:garrytan/gbrain', VERSION);+ try {+ expect(r.exitCode).toBe(0);+ expect(r.stderr).not.toContain('Upgrade did not take effect');+ } finally {+ rmSync(r.home, { recursive: true, force: true });+ }+ }, 60_000);+});