Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: Hermes snapshot falls back to a copy only for read-only or unopenable stores

garrytan4b49ec0c9e19
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1442295560..fd2b0796f7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -68,7 +68,7 @@ Hermes transcript snapshots no longer fail while Hermes is writing, and `gbrain
#### Transcripts, import, connectors, loops, facts and doctor (#8319, #8332)
-- `src/core/transcripts/hermes.ts`: when the store has both WAL sidecars, the Hermes adapter opens it read-only and runs `VACUUM INTO` a temporary file, so a checkpoint during the copy no longer tears the snapshot (`SQLITE_IOERR_SHORT_READ`) (#7067). A store at rest, or one in a directory gbrain cannot write, is copied into the temporary directory as before, and nothing is created beside the store. Tests: `test/hermes-snapshot-7067.serial.test.ts`, `test/hermes-snapshot-readonly-dir.test.ts`.
+- `src/core/transcripts/hermes.ts`: when the store has both WAL sidecars, the Hermes adapter opens it read-only and runs `VACUUM INTO` a temporary file, so a checkpoint during the copy no longer tears the snapshot (`SQLITE_IOERR_SHORT_READ`) (#7067). A store at rest, or one in a directory gbrain cannot write, is copied into the temporary directory as before, and nothing is created beside the store. Any other snapshot error, a busy writer included, is reported instead of falling back to a copy that could tear. Tests: `test/hermes-snapshot-7067.serial.test.ts`, `test/hermes-snapshot-readonly-dir.test.ts`, `test/hermes-snapshot-busy-rethrow.test.ts`.
- `src/core/transcripts/ingest.ts`, `src/commands/transcripts.ts`: `transcripts ingest --all` carries each file's discovered format instead of detecting it again, which failed on Claude Code logs whose first record is over 2 MiB; an explicit `--format` still wins (#7700). Test: `test/transcripts-all-discovered-format-7700.test.ts`.
- `src/commands/import.ts`, `src/core/persistence/import-mutations.ts`: a managed import with `sync.write_through` off refuses `owner_unavailable` (`detail: disabled_by_config`) once, before admitting any file, and names the supported path (commit, then `gbrain sync --source <id>`) (#7085). Tests: `test/import-write-through-off-7085.test.ts`, `test/e2e/import-write-through-off-7085-postgres.test.ts`.
- `src/core/connectors/sync.ts`: `connectors sync --dry-run` writes neither `last_sync_at` nor `auth_error_at`, and a preview whose listing stopped early returns `partial` with `listComplete: false` and `wouldFetch` (#7689, part 2). Test: `test/connectors-dry-run-7689.test.ts`.
diff --git a/src/core/transcripts/hermes.ts b/src/core/transcripts/hermes.ts
index cd54ed9df3..b4ec887f43 100644
--- a/src/core/transcripts/hermes.ts
+++ b/src/core/transcripts/hermes.ts
@@ -175,6 +175,8 @@ export const hermesAdapter: TranscriptAdapter = {
// state (WAL frames included) even while it commits and checkpoints. A
// store at rest, or one the read-only open cannot use (an unwritable
// directory), is copied instead, so nothing is ever created beside it.
+ // Any other failure, a busy writer included, surfaces: copying a store
+ // mid-transaction is the torn read the snapshot exists to prevent.
const tmp = mkdtempSync(join(tmpdir(), 'gbrain-hermes-'));
const copyPath = join(tmp, basename(path));
let sessions = 0;
@@ -190,8 +192,10 @@ export const hermesAdapter: TranscriptAdapter = {
} finally {
source.close();
}
- } catch {
+ } catch (err) {
rmSync(copyPath, { force: true });
+ const code = (err as { code?: unknown }).code;
+ if (typeof code !== 'string' || !(code.startsWith('SQLITE_READONLY') || code === 'SQLITE_CANTOPEN')) throw err;
}
}
if (!vacuumed) {
diff --git a/test/hermes-snapshot-busy-rethrow.test.ts b/test/hermes-snapshot-busy-rethrow.test.ts
new file mode 100644
index 0000000000..9961d65865
--- /dev/null
+++ b/test/hermes-snapshot-busy-rethrow.test.ts
@@ -0,0 +1,61 @@
+/**
+ * The Hermes snapshot falls back to a plain file copy only when the read-only
+ * VACUUM INTO cannot run at all (an unwritable directory or an unopenable
+ * store). A busy store has a live writer mid-transaction, and copying it is
+ * the torn read #7067 fixed, so a busy error has to surface instead.
+ *
+ * Protects: SQLITE_BUSY from the snapshot is rethrown, no session is imported
+ * from a copy, and the temp snapshot directory is removed.
+ */
+import { afterAll, describe, expect, test } from 'bun:test';
+import { Database } from 'bun:sqlite';
+import { mkdirSync, mkdtempSync, readdirSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { hermesAdapter } from '../src/core/transcripts/hermes.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const root = mkdtempSync(join(tmpdir(), 'gbrain-hermes-busy-test-'));
+afterAll(() => rmSync(root, { recursive: true, force: true }));
+
+describe('hermes snapshot of a busy store', () => {
+ test('a busy error is rethrown and nothing is copied', async () => {
+ const dir = join(root, 'store');
+ mkdirSync(dir, { recursive: true });
+ const path = join(dir, 'state.db');
+ const writer = new Database(path, { create: true });
+ writer.exec(`
+ PRAGMA journal_mode = WAL;
+ CREATE TABLE sessions (id TEXT PRIMARY KEY, title TEXT, display_name TEXT, started_at REAL, cwd TEXT, model TEXT, source TEXT);
+ CREATE TABLE messages (id INTEGER PRIMARY KEY, session_id TEXT, role TEXT, content TEXT, timestamp REAL);
+ `);
+ writer.prepare('INSERT INTO sessions VALUES (?, ?, ?, ?, ?, ?, ?)').run('session-a', 'synthetic session', null, 1786000000, '/tmp/acme-example', null, 'cli');
+ writer.prepare('INSERT INTO messages (session_id, role, content, timestamp) VALUES (?, ?, ?, ?)').run('session-a', 'user', 'synthetic question', 1786000000);
+ writer.exec('PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE;');
+ writer.prepare('INSERT INTO messages (session_id, role, content, timestamp) VALUES (?, ?, ?, ?)').run('session-a', 'assistant', 'synthetic answer', 1786000001);
+
+ const snapshotTmp = join(root, 'tmp');
+ mkdirSync(snapshotTmp, { recursive: true });
+ const sessions: unknown[] = [];
+ let thrown: unknown;
+ try {
+ await withEnv({ TMPDIR: snapshotTmp }, async () => {
+ const it = hermesAdapter.parse(path, { maxBytes: 64 * 1024 * 1024 });
+ while (true) {
+ const step = await it.next();
+ if (step.done) break;
+ sessions.push(step.value);
+ }
+ });
+ } catch (err) {
+ thrown = err;
+ } finally {
+ writer.exec('COMMIT');
+ writer.close();
+ }
+
+ expect((thrown as { code?: string } | undefined)?.code).toBe('SQLITE_BUSY');
+ expect(sessions).toHaveLength(0);
+ expect(readdirSync(snapshotTmp)).toEqual([]);
+ }, 30_000);
+});