Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(post-upgrade): an up-to-date apply-migrations no longer ends the run

garrytana05d07830740
diff --git a/src/commands/upgrade.ts b/src/commands/upgrade.ts
index 210d62ffba..30f623e438 100644
--- a/src/commands/upgrade.ts
+++ b/src/commands/upgrade.ts
@@ -638,9 +638,11 @@ export async function runPostUpgrade(args: string[] = []): Promise<void> {
const { applyMigrations } = await import('./apply-migrations.ts');
const { exitCode, failure } = await applyMigrations(['--yes', '--non-interactive', ...(noAutopilotInstall ? ['--no-autopilot-install'] : [])]);
report.apply_migrations = { exit_code: exitCode ?? 0 };
- // Any status apply-migrations returns ends post-upgrade here, as its
- // in-process process.exit always did.
- if (exitCode !== undefined) process.exit(finishPostUpgrade(json, report, exitCode, failure));
+ // A failure, a consent refusal (3) or a held migration lock (75) ends
+ // post-upgrade here. 0 ("all migrations up to date", the common case on
+ // an upgrade with no new orchestrator migration) continues: the schema
+ // pass, banners, prompts and recovery checks below still run.
+ if (exitCode !== undefined && exitCode !== 0) process.exit(finishPostUpgrade(json, report, exitCode, failure));
} catch (e) {
// Surface the error but don't throw — post-upgrade is best-effort.
// Users can re-run `gbrain apply-migrations` manually if they want
diff --git a/test/upgrade-no-autopilot.serial.test.ts b/test/upgrade-no-autopilot.serial.test.ts
index eeed22df79..bef79e1d36 100644
--- a/test/upgrade-no-autopilot.serial.test.ts
+++ b/test/upgrade-no-autopilot.serial.test.ts
@@ -68,4 +68,38 @@ await runPostUpgrade(['--no-autopilot-install']);
rmSync(home, { recursive: true, force: true });
}
});
+ test('post-upgrade continues past an up-to-date apply-migrations (exit 0): the schema pass still runs; a failure still ends it', () => {
+ const run = (exitCode: number) => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-post-upgrade-uptodate-'));
+ try {
+ mkdirSync(join(home, '.gbrain'), { recursive: true });
+ writeFileSync(join(home, '.gbrain', 'config.json'), JSON.stringify({ engine: 'pglite', database_path: join(home, '.gbrain', 'brain.pglite') }));
+ const driver = join(home, 'driver.ts');
+ writeFileSync(driver, `import { mock } from 'bun:test';
+mock.module(${JSON.stringify(join(REPO, 'src/commands/apply-migrations.ts'))}, () => ({
+ applyMigrations: async () => ({ exitCode: ${exitCode} }),
+}));
+const { runPostUpgrade } = await import(${JSON.stringify(join(REPO, 'src/commands/upgrade.ts'))});
+await runPostUpgrade(['--no-autopilot-install', '--json']);
+`);
+ const result = spawnSync(process.execPath, ['--no-env-file', driver], {
+ cwd: home,
+ env: { HOME: home, GBRAIN_HOME: home, PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, GBRAIN_SKIP_REFERENCE_SWEEP: '1', GBRAIN_NO_ONBOARD_NUDGE: '1' },
+ encoding: 'utf8', timeout: 120_000,
+ });
+ const docs = result.stdout.split('\n').filter(l => l.trim().startsWith('{')).map(l => JSON.parse(l));
+ return { status: result.status, docs, out: result.stderr + result.stdout };
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ };
+ const upToDate = run(0);
+ expect(upToDate.status, upToDate.out).toBe(0);
+ expect(upToDate.docs).toHaveLength(1);
+ expect(upToDate.docs[0]).toMatchObject({ status: 'ok', apply_migrations: { exit_code: 0 }, schema: 'up_to_date' });
+ const failed = run(1);
+ expect(failed.status, failed.out).toBe(1);
+ expect(failed.docs.at(-1)).toMatchObject({ status: 'failed', apply_migrations: { exit_code: 1 } });
+ expect(failed.docs.at(-1).schema).toBeUndefined();
+ }, 240_000);
});