Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(backup): give each source's db_only dump its own export (#5539)

garrytana6aed84917ac
diff --git a/docs/operations/backup-check.md b/docs/operations/backup-check.md
index e548e3523f..b6d7d1f4f9 100644
--- a/docs/operations/backup-check.md
+++ b/docs/operations/backup-check.md
@@ -13,7 +13,7 @@ proof that a whole brain could be restored.
| Bootstrap workspace (skills/, memory/, brain/, identity) | file plane: the install receipt's `repo_url` + the per-root push-status files | receipt without `repo_url` → warn; a failing background push → flagged row only (the push-failure banner owns that alarm) |
| Google / GitHub connector sources | persistence mode and worktree binding | none on its own: a managed unbound source (`connector_database`) and an unmanaged source whose directory is missing or not a Git repo are `connector` info rows. A bound connector, or one whose directory is a Git repo, is checked as a source repo. |
| DB-only brain (pages exist, nothing git-backed) | page count + absence of any git-backed asset | warn on PGLite (local disk loss risks these pages); info on Postgres (database placement and external backup are unverified) |
-| `db_only` storage-tier pages | `gbrain.yml` per source | info row — dump with `gbrain export --dir <backup-dir>` to somewhere OUTSIDE the gitignored dirs (`--restore-only` is the wrong direction for a backup) |
+| `db_only` storage-tier pages | `gbrain.yml` per source | info row: dump with `gbrain export --source <id> --dir BACKUP_DIR/<id>` (replace `BACKUP_DIR` with a durable backup directory) to somewhere OUTSIDE the gitignored dirs (`--restore-only` is the wrong direction for a backup) |
| Harness-native skill dirs (e.g. the agent's installed skills) | skillpack bridge state | info only — these are installed COPIES; the originals live in repos |
`configured_repos` records configuration; the legacy `recoverable_repos` field
@@ -189,8 +189,10 @@ source id. Full per-asset detail (which repo, which fix) is local-only:
`git remote add origin [email protected]:you/your-brain-repo.git && git push -u origin main`,
then `gbrain sources harden <source-id>` for auto-push durability.
- Unpushed workspace work: `gbrain sources push --path <workspace>`.
-- db_only pages: `gbrain export --dir <backup-dir>` (store the dump outside
- the gitignored dirs — another disk, another repo, anywhere durable).
+- db_only pages: `gbrain export --source <id> --dir BACKUP_DIR/<id>` for
+ each source the row names, with `BACKUP_DIR` replaced by a directory
+ outside the gitignored dirs (another disk, another repo, anywhere durable). One directory per source:
+ an unscoped export refuses when two sources share a slug.
- A dirty source repo (uncommitted changes): `gbrain sources push <source-id>`
(secret-scanned commit and push), or discard the changes, then run
`gbrain backup check`. Remote evidence is only read for a clean tree.
diff --git a/src/core/backup/coverage.ts b/src/core/backup/coverage.ts
index 2557591880..e5db14d2c4 100644
--- a/src/core/backup/coverage.ts
+++ b/src/core/backup/coverage.ts
@@ -258,7 +258,11 @@ export async function computeBackupCoverage(
'db_only dirs configured: those pages are not in git and the DB file is deliberately not backed up. ' +
'Dump them somewhere OUTSIDE the gitignored dirs (--restore-only is the wrong direction for a backup); ' +
'run gbrain doctor (undeclared_db_only_pages) for the page-level audit.',
- fix_argv: ['gbrain', 'export', '--dir', '<backup-dir>'],
+ // One source per dump directory: an unscoped export refuses when two
+ // sources share a slug, and a second dump refuses an occupied dir.
+ // BACKUP_DIR is a placeholder with no shell metacharacters, so a
+ // verbatim paste cannot redirect (`<dir>/x` would truncate `/x`).
+ fix_argv: ['gbrain', 'export', '--source', row.id, '--dir', `BACKUP_DIR/${row.id}`],
});
}
}
diff --git a/test/backup-coverage.serial.test.ts b/test/backup-coverage.serial.test.ts
index ed112e409c..e47edc19b0 100644
--- a/test/backup-coverage.serial.test.ts
+++ b/test/backup-coverage.serial.test.ts
@@ -11,11 +11,13 @@
*/
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import { execFileSync } from 'node:child_process';
-import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import type { BrainEngine } from '../src/core/engine.ts';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { runExport } from '../src/commands/export.ts';
import {
computeBackupCoverage,
getBackupStatus,
@@ -327,8 +329,47 @@ describe('computeBackupCoverage — source repos', () => {
expect(asset).toBeDefined();
expect(asset?.id).toBe('tiered-src');
expect(asset?.state).toBe('info');
- expect(asset?.fix_argv).toEqual(['gbrain', 'export', '--dir', '<backup-dir>']);
+ expect(asset?.fix_argv).toEqual(['gbrain', 'export', '--source', 'tiered-src', '--dir', 'BACKUP_DIR/tiered-src']);
+ // Pasted as-is, the command must not redirect or substitute anything:
+ // `<backup-dir>/x` would read stdin from `backup-dir` and truncate `/x`.
+ expect(asset?.fix_argv?.join(' ')).not.toMatch(/[<>|;&$`]/);
});
+
+ test('following each db_only fix dumps every source, even when two share a slug', async () => {
+ const engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ const original = { exit: process.exit, log: console.log, error: console.error };
+ try {
+ for (const id of ['src-a', 'src-b']) {
+ const repo = join(tmp, id);
+ mkdirSync(repo);
+ writeFileSync(join(repo, 'gbrain.yml'), 'storage:\n db_only:\n - private/\n');
+ await engine.executeRaw('INSERT INTO sources (id, name, local_path) VALUES ($1, $1, $2)', [id, repo]);
+ await engine.putPage('private/shared', { type: 'note', title: id, compiled_truth: `body-${id}`, timeline: '' }, { sourceId: id });
+ }
+ const s = await computeBackupCoverage(engine, { localGitProbes: false });
+ const fixes = s.assets.filter((a) => a.kind === 'db_only').map((a) => a.fix_argv ?? []);
+ expect(fixes).toHaveLength(2);
+
+ // Export publication refuses symlinked ancestors (macOS /var).
+ const backupDir = join(realpathSync(tmp), 'dumps');
+ process.exit = ((code: number) => { throw new Error(`EXIT:${code}`); }) as never;
+ console.log = console.error = () => {};
+ for (const argv of fixes) {
+ expect(argv.slice(0, 2)).toEqual(['gbrain', 'export']);
+ await runExport(engine, argv.slice(2).map((arg) => arg.replace(/^BACKUP_DIR\//, backupDir + '/')));
+ }
+ for (const id of ['src-a', 'src-b']) {
+ expect(readFileSync(join(backupDir, id, 'private/shared.md'), 'utf8')).toContain(`body-${id}`);
+ }
+ } finally {
+ process.exit = original.exit;
+ console.log = original.log;
+ console.error = original.error;
+ await engine.disconnect();
+ }
+ }, 60000);
});
// ── DB-only brain + empty brain ──────────────────────────────────────────────