Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(migrations): resolve v0.11.0 and v0.22.4 state through gbrainPath (#5549)
Andrei Neacsu749414d4d51b
diff --git a/src/commands/migrations/v0_11_0.ts b/src/commands/migrations/v0_11_0.tsindex cc9be74ab0..fd7980104b 100644--- a/src/commands/migrations/v0_11_0.ts+++ b/src/commands/migrations/v0_11_0.ts@@ -25,7 +25,7 @@ import { join, resolve, dirname } from 'path';import { execSync } from 'child_process';import type { Migration, OrchestratorOpts, OrchestratorResult, OrchestratorPhaseResult } from './types.ts';import { savePreferences, loadPreferences } from '../../core/preferences.ts';-import { loadConfig } from '../../core/config.ts';+import { loadConfig, configPath, gbrainPath } from '../../core/config.ts';// Bug 3 — appendCompletedMigration moved to the runner (apply-migrations.ts).import { promptLine } from '../../core/cli-util.ts';import { VERSION } from '../../version.ts';@@ -35,9 +35,10 @@ const AGENTS_MD_MARKER = '<!-- gbrain:subagent-routing v0.11.0 -->';const CRON_MIGRATED_PROPERTY = '_gbrain_migrated_by';const MAX_HOST_FILE_BYTES = 1_000_000;+// home() locates host-agent config ($HOME/.claude, $HOME/.openclaw); gbrain's+// own state follows GBRAIN_HOME via gbrainPath() (#5549).function home(): string { return process.env.HOME || ''; }-function gbrainDir(): string { return join(home(), '.gbrain'); }-function pendingHostWorkPath(): string { return join(gbrainDir(), 'migrations', 'pending-host-work.jsonl'); }+function pendingHostWorkPath(): string { return gbrainPath('migrations', 'pending-host-work.jsonl'); }export interface PendingHostWorkEntry {type: 'cron-handler-needs-host-registration' | 'agents-md-dispatcher-needs-host-review';@@ -259,7 +260,7 @@ function rewriteCronManifest(// We load config lazily to avoid a hard dep.let enginePglite = false;try {- const cfg = JSON.parse(readFileSync(join(gbrainDir(), 'config.json'), 'utf-8'));+ const cfg = JSON.parse(readFileSync(configPath(), 'utf-8'));enginePglite = cfg?.engine === 'pglite';} catch { /* best-effort */ }diff --git a/src/commands/migrations/v0_22_4.ts b/src/commands/migrations/v0_22_4.tsindex e05b29f19e..8f33d9fc8f 100644--- a/src/commands/migrations/v0_22_4.ts+++ b/src/commands/migrations/v0_22_4.ts@@ -25,7 +25,7 @@ import { existsSync, mkdirSync, writeFileSync, readFileSync, appendFileSync } frimport { join } from 'path';import type { Migration, OrchestratorOpts, OrchestratorResult, OrchestratorPhaseResult } from './types.ts';import type { BrainEngine } from '../../core/engine.ts';-import { loadConfig, toEngineConfig } from '../../core/config.ts';+import { loadConfig, toEngineConfig, gbrainPath } from '../../core/config.ts';import { createEngine } from '../../core/engine-factory.ts';import { scanBrainSources, type AuditReport } from '../../core/brain-writer.ts';@@ -37,10 +37,7 @@ export function __setTestEngineOverride(engine: BrainEngine | null): void {testEngineOverride = engine;}-function gbrainDir(): string {- return join(process.env.HOME || '', '.gbrain');-}-function migrationsDir(): string { return join(gbrainDir(), 'migrations'); }+function migrationsDir(): string { return gbrainPath('migrations'); }function auditReportPath(): string { return join(migrationsDir(), 'v0.22.4-audit.json'); }function pendingHostWorkPath(): string { return join(migrationsDir(), 'pending-host-work.jsonl'); }diff --git a/test/migrations-v0_11_0.test.ts b/test/migrations-v0_11_0.test.tsindex 0e1a6d176a..f0870b086d 100644--- a/test/migrations-v0_11_0.test.ts+++ b/test/migrations-v0_11_0.test.ts@@ -28,18 +28,29 @@ const {} = __testing;let tmp: string;+let gbrainHome: string;let origHome: string | undefined;+let origGbrainHome: string | undefined;+// HOME holds the host-agent scopes ($HOME/.claude, $HOME/.openclaw); the+// gbrain home is a SEPARATE dir so every test also proves that migration+// state follows GBRAIN_HOME, never $HOME/.gbrain (#5549).beforeEach(() => {origHome = process.env.HOME;+ origGbrainHome = process.env.GBRAIN_HOME;tmp = mkdtempSync(join(tmpdir(), 'gbrain-v0_11_0-test-'));+ gbrainHome = mkdtempSync(join(tmpdir(), 'gbrain-v0_11_0-gbrain-home-'));process.env.HOME = tmp;+ process.env.GBRAIN_HOME = gbrainHome;});afterEach(() => {if (origHome === undefined) delete process.env.HOME;else process.env.HOME = origHome;+ if (origGbrainHome === undefined) delete process.env.GBRAIN_HOME;+ else process.env.GBRAIN_HOME = origGbrainHome;try { rmSync(tmp, { recursive: true, force: true }); } catch { /* best-effort */ }+ try { rmSync(gbrainHome, { recursive: true, force: true }); } catch { /* best-effort */ }});function writeAgentsMd(dir: string, body: string) {@@ -259,6 +270,37 @@ describe('cron manifest rewrite — gbrain builtins only', () => {});});+describe('GBRAIN_HOME resolution (#5549)', () => {+ test('pending-host-work.jsonl lands under GBRAIN_HOME, not $HOME/.gbrain', () => {+ const path = writeCronJson(join(tmp, '.claude'), [+ { schedule: '0 */30 * * *', kind: 'agentTurn', skill: 'ea-inbox-sweep' },+ ]);+ rewriteCronManifest(path, DEFAULT_OPTS);++ const expected = join(gbrainHome, '.gbrain', 'migrations', 'pending-host-work.jsonl');+ expect(existsSync(expected)).toBe(true);+ expect(readFileSync(expected, 'utf-8')).toContain('ea-inbox-sweep');+ expect(existsSync(join(tmp, '.gbrain'))).toBe(false);+ });++ test('engine detection reads config.json from GBRAIN_HOME', () => {+ // Only the GBRAIN_HOME brain is PGLite; $HOME/.gbrain says postgres.+ // A PGLite brain needs --follow on rewritten builtin cron entries.+ mkdirSync(join(gbrainHome, '.gbrain'), { recursive: true });+ writeFileSync(join(gbrainHome, '.gbrain', 'config.json'), JSON.stringify({ engine: 'pglite' }));+ mkdirSync(join(tmp, '.gbrain'), { recursive: true });+ writeFileSync(join(tmp, '.gbrain', 'config.json'), JSON.stringify({ engine: 'postgres' }));++ const path = writeCronJson(join(tmp, '.claude'), [+ { schedule: '*/5 * * * *', kind: 'agentTurn', skill: 'sync' },+ ]);+ rewriteCronManifest(path, DEFAULT_OPTS);++ const after = JSON.parse(readFileSync(path, 'utf-8'));+ expect(after.jobs[0].cmd).toContain('--follow');+ });+});+describe('findAgentsMdFiles + findCronManifests scoping', () => {test('finds AGENTS.md in $HOME/.claude and $HOME/.openclaw scopes', () => {mkdirSync(join(tmp, '.claude'), { recursive: true });diff --git a/test/migrations-v0_22_4.test.ts b/test/migrations-v0_22_4.test.tsindex ccdf048097..a62076b704 100644--- a/test/migrations-v0_22_4.test.ts+++ b/test/migrations-v0_22_4.test.ts@@ -1,6 +1,8 @@import { describe, expect, test } from 'bun:test';import { v0_22_4 } from '../src/commands/migrations/v0_22_4.ts';import { migrations, getMigration } from '../src/commands/migrations/index.ts';+import { join } from 'path';+import { withEnv } from './helpers/with-env.ts';describe('v0.22.4 migration (B11)', () => {test('exports a Migration with the right version', () => {@@ -57,6 +59,15 @@ describe('v0.22.4 migration (B11)', () => {expect(__testing.pendingHostWorkPath()).toMatch(/pending-host-work\.jsonl$/);});+ test('output paths resolve under GBRAIN_HOME, not $HOME/.gbrain (#5549)', async () => {+ const { __testing } = await import('../src/commands/migrations/v0_22_4.ts');+ await withEnv({ HOME: '/tmp/gbrain-5549-home', GBRAIN_HOME: '/tmp/gbrain-5549-gbrain-home' }, () => {+ const migrationsDir = join('/tmp/gbrain-5549-gbrain-home', '.gbrain', 'migrations');+ expect(__testing.auditReportPath()).toBe(join(migrationsDir, 'v0.22.4-audit.json'));+ expect(__testing.pendingHostWorkPath()).toBe(join(migrationsDir, 'pending-host-work.jsonl'));+ });+ });+test('dotted migration filename references — emit-todo entries point at v0.22.4.md', async () => {// The runtime convention is dotted (v0.22.4.md), not underscored.// Source-grep guards the contract without spinning up a real audit.@@ -71,9 +82,15 @@ describe('v0.22.4 migration (B11)', () => {const fs = await import('fs');const path = await import('path');const os = await import('os');+ // HOME and GBRAIN_HOME point at different dirs so the write must follow+ // GBRAIN_HOME, never $HOME/.gbrain (#5549). Bun's os.homedir() ignores+ // runtime HOME changes, so HOME alone does not isolate the test.const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'gbrain-migration-test-'));+ const gbrainHome = fs.mkdtempSync(path.join(os.tmpdir(), 'gbrain-migration-gbrain-home-'));const origHome = process.env.HOME;+ const origGbrainHome = process.env.GBRAIN_HOME;process.env.HOME = tmpHome;+ process.env.GBRAIN_HOME = gbrainHome;try {const fakeReport = {ok: false,@@ -120,7 +137,10 @@ describe('v0.22.4 migration (B11)', () => {fakeReport,);expect(r.status).toBe('complete');- const jsonl = fs.readFileSync(__testing.pendingHostWorkPath(), 'utf8');+ const expectedPath = path.join(gbrainHome, '.gbrain', 'migrations', 'pending-host-work.jsonl');+ expect(fs.existsSync(expectedPath)).toBe(true);+ expect(fs.existsSync(path.join(tmpHome, '.gbrain'))).toBe(false);+ const jsonl = fs.readFileSync(expectedPath, 'utf8');const lines = jsonl.split('\n').filter(Boolean);// Two sources had issues; clean-source should NOT produce an entry.expect(lines.length).toBe(2);@@ -142,8 +162,12 @@ describe('v0.22.4 migration (B11)', () => {expect(e.command).toContain('--fix');}} finally {- process.env.HOME = origHome;+ if (origHome === undefined) delete process.env.HOME;+ else process.env.HOME = origHome;+ if (origGbrainHome === undefined) delete process.env.GBRAIN_HOME;+ else process.env.GBRAIN_HOME = origGbrainHome;fs.rmSync(tmpHome, { recursive: true, force: true });+ fs.rmSync(gbrainHome, { recursive: true, force: true });}});});