Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: Step 11 voice fixes (credential redaction order, home directory in stacks, access errors keep their repair fix, unreadable refresh row said as such, refresh table in the holder lookup)

garrytan9644786ac346
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e511348bb1..517d04fe02 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,10 +18,10 @@ Before, a stuck transaction holding a source, worktree or brain row (a long-runn
### Itemized changes
-- `src/core/persistence/worktree-refresh.ts`: Postgres `55P03` in either fence transaction (`fenceDraining`, `fenceAndMerge`) is the new `refresh_lock_timeout` refusal (registry, catalogue, `docs/guides/write-refusals.md`, `docs/guides/error-codes.md`). Its `why` lists up to five open transactions in the current database holding row-level locks on `persistence_brain`, `persistence_worktrees` or `sources` (pid, application, state, seconds open). Plain readers, which cannot block the fence, are left out, and the holders' SQL text is never copied into `why`. A refusal after the drain aborts the draining row as other fence refusals do.
-- `src/core/persistence/worktree-refresh.ts`: `refreshWorktree` turns any other non-`OperationError` throw into `internal_error` with the error name, SQLSTATE and message (connection details and URLs redacted), the stack in `why`, and a fix naming the active refresh row and its state. The row is not changed, and a host identity file that cannot be read still yields that refusal. The already-current path no longer throws a `TypeError` of its own when a member sync rejects with a non-object.
+- `src/core/persistence/worktree-refresh.ts`: Postgres `55P03` in either fence transaction (`fenceDraining`, `fenceAndMerge`) is the new `refresh_lock_timeout` refusal (registry, catalogue, `docs/guides/write-refusals.md`, `docs/guides/error-codes.md`). Its `why` lists up to five open transactions in the current database holding row-level locks on `persistence_brain`, `persistence_worktrees`, `sources` or `persistence_worktree_refreshes` (pid, application, state, seconds open); the blocking transaction is among them. Plain readers, which cannot block the fence, are left out, the holders' SQL text is never copied into `why`, and a failed lookup says so. A refusal after the drain aborts the draining row as other fence refusals do.
+- `src/core/persistence/worktree-refresh.ts`: `refreshWorktree` turns any other non-`OperationError` throw into `internal_error` with the error name, SQLSTATE and message (credentials, connection details and the home directory redacted), the stack in `why`, and a fix naming the active refresh row and its state, or saying the row could not be read. The row is not changed, and a host identity file that cannot be read still yields that refusal. A database access error the classifier recognizes (connection refused, a missing table) is rethrown unchanged, so it keeps its `gbrain db-repair` or `gbrain apply-migrations` fix. The already-current path no longer throws a `TypeError` of its own when a member sync rejects with a non-object.
- `src/commands/sources-refresh.ts`: refusals print `why` (a `why` key under `--json`, a `Why:` line otherwise).
-- Test: `test/worktree-refresh-unexpected-throw.test.ts` (PGLite, and Postgres under `DATABASE_URL`; the lock-timeout cases hold a source row from a second connection, before the fence and after the drain, and check that a plain reader is not named and no SQL text is echoed; an unreadable host identity file still gives a typed refusal).
+- Test: `test/worktree-refresh-unexpected-throw.test.ts` (PGLite, and Postgres under `DATABASE_URL`; the lock-timeout cases hold a source row from a second connection, before the fence and after the drain, and check that a plain reader is not named and no SQL text is echoed; an unreadable host identity file still gives a typed refusal, an access error is not wrapped, and credentials and the home directory are redacted).
## [0.60.529.0] - 2026-10-31
diff --git a/docs/guides/write-refusals.md b/docs/guides/write-refusals.md
index 9c71fd71cd..169ede5405 100644
--- a/docs/guides/write-refusals.md
+++ b/docs/guides/write-refusals.md
@@ -553,7 +553,7 @@ this anchor (`--json` puts them in one object on stdout); the command exits 1.
| <a id="sync_in_progress"></a>`sync_in_progress` | `sync_in_progress` | A managed sync of one of the checkout's sources has an unfinished cursor. Moving the checkout now would strand it, and the refresh never waits on a sync. `gbrain repair frontmatter --apply` refuses the same way while an unfinished cursor still names a selected file. | Run the printed `gbrain sync --source <source> --no-pull ...` resume command (it keeps the cursor's options), then retry (for a repair, preview again and approve the new hash). |
| <a id="refresh_in_progress"></a>`refresh_in_progress` | `refresh_in_progress` | Another refresh of the same checkout is active and its process is still running (the message names its holder's pid and host when the active refresh row is already readable), or a command that changes which sources share the checkout (`sources add`, `remove`, `archive`, `restore`, `reclone`) ran while one was active. A refresh whose process died is not a reason to refuse: the next refresh on the owner host finishes it first (`--dry-run` does not, so it still refuses while the orphan stands, and neither does a refresh that cannot read the checkout's HEAD: it leaves the orphan as it is and refuses until git can read HEAD again). A refresh whose holder is recorded on another host is never taken over either: writer status shows its `liveness` as `unknown`, and `gbrain sources refresh <source> --resume` on the owner host finishes it once that host's refresh is known to have stopped. Pending publication or topology recovery on the checkout refuses `refresh_recovery_required` first. An interrupted refresh that needs a reset decision refuses `refresh_recovery_required` instead. | Wait for the running refresh, or check it with `gbrain sources writer status <source>` (`liveness`, `holder`). `gbrain sources refresh <source> --resume` finishes an orphaned one now; then retry the source command with the same request ID. |
| <a id="refresh_drain_timeout"></a>`refresh_drain_timeout` | `refresh_drain_timeout` | Queued writes or Git effects on the checkout did not finish within `--wait-drain` (default 60 s; `sources.refresh_drain_wait_ms`; `GBRAIN_REFRESH_DRAIN_WAIT_MS`). The counts are in the message. The fence was lifted and HEAD did not move. | Retry, or wait longer with `--wait-drain <seconds>`. Inspect what is queued with `gbrain sources writer status <source> --json`. |
-| <a id="refresh_lock_timeout"></a>`refresh_lock_timeout` | `refresh_lock_timeout` | The refresh fence locks the brain, worktree and source rows with a 5 s lock timeout, and another database transaction held one of them longer. `why` lists the open transactions in this database holding row locks on those tables (pid, application, state, how long the transaction has been open); it never repeats their SQL. Nothing moved: no fence was set, or the draining refresh was aborted, and HEAD did not move. | Retry `gbrain sources refresh <source>`. If it repeats, the transaction in `why` is stuck: find out what it is on the brain host before ending it. |
+| <a id="refresh_lock_timeout"></a>`refresh_lock_timeout` | `refresh_lock_timeout` | The refresh fence locks the brain, worktree, source and refresh rows with a 5 s lock timeout, and another database transaction held one of them longer. `why` lists the open transactions in this database holding row locks on those tables (pid, application, state, how long the transaction has been open), the blocking one among them; it never repeats their SQL, and says so when that lookup failed. Nothing moved: no fence was set, or the draining refresh was aborted, and HEAD did not move. | Retry `gbrain sources refresh <source>`. If it repeats, one of the transactions in `why` is stuck: find out which on the brain host before ending it. |
| <a id="refresh_sync_timeout"></a>`refresh_sync_timeout` | `refresh_sync_timeout` | Reported per source in a refresh's `sync_blocked`, not as a refusal: the source's managed sync committed nothing within its share of the refresh sync budget (each member gets an equal share of what is left; `sources.refresh_sync_budget_ms`, `GBRAIN_REFRESH_SYNC_BUDGET_MS`, default 5 min), for example because no consumer claimed its write. The message names that write and its state. The checkout is at the upstream commit, the refresh completed and released its lock, and the source is behind until its next sync. A sync stopped by the drain's own rules is reported with that stop's code instead (`drain_stalled` and the others). | Run the `resume` command (`gbrain sync --source <source> --no-pull`). If it stalls the same way, check the write it names with `gbrain sources writer status <source> --json`. |
| <a id="refresh_source_changed"></a>`refresh_source_changed` | `refresh_source_changed` | HEAD moved (an outside commit or checkout), or the checkout's ownership or set of sources changed, between the precheck and the merge. Nothing was merged and the fence was lifted. | Retry `gbrain sources refresh <source>`. |
| <a id="refresh_recovery_required"></a>`refresh_recovery_required` | `refresh_recovery_required` | Publication or source recovery is pending on the checkout, or a refresh was interrupted and HEAD is neither the commit it started from nor the verified upstream commit. Writes to the checkout stay refused until it is resolved; gbrain never adopts a HEAD it cannot verify. | `gbrain sources writer status <source>`, then `gbrain sources refresh <source> --resume`. When HEAD is unverified the fix names both ways out: reset to the start commit and run `--abandon`, or reset to the upstream commit and run `--resume`. Resetting is destructive, so ask the user first. |
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 15e1b4c41b..d68a566d09 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -21,7 +21,7 @@
* next plain refresh finishes it through the `--resume` path before refreshing.
*/
import { mkdirSync, mkdtempSync, rmSync } from 'node:fs';
-import { hostname } from 'node:os';
+import { homedir, hostname } from 'node:os';
import { join } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import type { SyncResult } from '../../commands/sync.ts';
@@ -44,6 +44,7 @@ import { ACTIVE_REFRESH_STATES_SQL, CHECKOUT_EFFECT_KINDS_SQL, type WorktreeRefr
import { gitChildEnv } from '../git-env.ts';
import { redactConnectionInfo } from '../audit/redact-connection-info.ts';
import { redactUrlsInText } from '../url-redact.ts';
+import { classifyPgAccessError } from '../pg-access-classify.ts';
import { classifyOriginUniqueViolation } from './origin-guard.ts';
export interface WorktreeRefreshRow {
@@ -485,21 +486,32 @@ function finished(row: WorktreeRefreshRow, sync: { synced: RefreshMemberSync[];
*/
export async function refreshWorktree(engine: BrainEngine, sourceId: string, opts: RefreshOptions = {}): Promise<WorktreeRefreshResult> {
try { return await runRefresh(engine, sourceId, opts); }
- catch (error) { throw error instanceof OperationError ? error : await unexpectedRefreshFailure(engine, sourceId, error); }
+ catch (error) {
+ if (error instanceof OperationError || classifyPgAccessError(error).reason !== 'unknown') throw error;
+ throw await unexpectedRefreshFailure(engine, sourceId, error);
+ }
}
-const redact = (text: string) => redactUrlsInText(redactConnectionInfo(text));
+const redact = (text: string) => {
+ const home = homedir();
+ return redactConnectionInfo(redactUrlsInText(home.length > 1 ? text.split(home).join('~') : text));
+};
/**
* GBRA-250: an untyped throw (a statement error, a TypeError) leaves the refresh where it stood, so a fenced or syncing row
* resumes on the next run. The refusal names that row and carries the stack in `why`, so a failure seen once leaves a trace.
+ * A database access error the classifier recognizes (connection refused, a missing table) is rethrown unchanged, so the CLI
+ * keeps its own repair fix for it.
*/
async function unexpectedRefreshFailure(engine: BrainEngine, sourceId: string, error: unknown): Promise<OperationError> {
const thrown = error instanceof Error ? error : new Error(String(error));
const sqlstate = typeof (error as { code?: unknown } | null)?.code === 'string' ? ` (${(error as { code: string }).code})` : '';
- const binding = await Promise.resolve().then(() => getWorktreeBinding(engine, sourceId, existingLocalHostId())).catch(() => null);
- const active = binding ? await readActiveRefresh(engine, binding.worktree_id).catch(() => null) : null;
- const left = active ? `Refresh ${active.id} stays ${active.state}, and the next gbrain sources refresh ${sourceId} resumes it.` : 'No refresh row was left active.';
+ const lookup = await Promise.resolve().then(async () => {
+ const binding = await getWorktreeBinding(engine, sourceId, existingLocalHostId());
+ return { active: binding ? await readActiveRefresh(engine, binding.worktree_id) : null };
+ }).catch(() => null);
+ const left = !lookup ? 'The refresh row could not be read, so a fenced or syncing refresh may still be active.'
+ : lookup.active ? `Refresh ${lookup.active.id} stays ${lookup.active.state}, and the next gbrain sources refresh ${sourceId} resumes it.` : 'No refresh row was left active.';
return opError('internal_error', redact(`The refresh of source ${sourceId} failed unexpectedly: ${thrown.name}${sqlstate}: ${thrown.message}`),
`${left} Run gbrain sources refresh ${sourceId} again; if it fails the same way, report this output (why holds the stack) with gbrain doctor --json.`,
{ why: redact(thrown.stack ?? `${thrown.name}: ${thrown.message}`) });
@@ -608,13 +620,13 @@ async function lockTimeoutRefusal(engine: BrainEngine, sourceId: string, refresh
WHERE a.pid <> pg_backend_pid() AND a.xact_start IS NOT NULL AND a.datname = current_database()
AND EXISTS (SELECT 1 FROM pg_locks l WHERE l.pid = a.pid AND l.granted AND l.mode <> 'AccessShareLock'
AND l.database = (SELECT oid FROM pg_database WHERE datname = current_database())
- AND l.relation IN ('persistence_brain'::regclass, 'persistence_worktrees'::regclass, 'sources'::regclass))
- ORDER BY a.xact_start LIMIT 5`).catch(() => []);
- const error = refusal('refresh_lock_timeout', `The refresh fence of source ${sourceId} waited past its 5 s lock timeout for a brain, worktree or source row another database transaction holds; nothing moved.`,
- `Retry gbrain sources refresh ${sourceId}. If it repeats, the transaction named in why is stuck: find out what it is on the brain host before ending it.`, refreshId);
- error.why = holders.length
- ? redact(`Open transactions holding row locks on those tables: ${holders.map(h => `pid ${h.pid} (${h.application_name || 'no application name'}, ${h.state ?? 'unknown state'}, open ${h.open_seconds} s)`).join('; ')}`)
- : 'No open transaction holding a lock on those tables was visible to this database role.';
+ AND l.relation IN ('persistence_brain'::regclass, 'persistence_worktrees'::regclass, 'sources'::regclass, 'persistence_worktree_refreshes'::regclass))
+ ORDER BY a.xact_start LIMIT 5`).catch(() => null);
+ const error = refusal('refresh_lock_timeout', `The refresh fence of source ${sourceId} waited past its 5 s lock timeout for a brain, worktree, source or refresh row another database transaction holds; nothing moved.`,
+ `Retry gbrain sources refresh ${sourceId}. If it repeats, one of the transactions named in why is stuck: find out which on the brain host before ending it.`, refreshId);
+ error.why = holders === null ? 'The lookup of open transactions holding those rows failed.'
+ : holders.length ? redact(`Open transactions holding row locks on those tables (the transaction blocking the fence is among them): ${holders.map(h => `pid ${h.pid} (${h.application_name || 'no application name'}, ${h.state ?? 'unknown state'}, open ${h.open_seconds} s)`).join('; ')}`)
+ : 'No open transaction holding a row lock on those tables was visible to this database role.';
return error;
}
diff --git a/test/worktree-refresh-unexpected-throw.test.ts b/test/worktree-refresh-unexpected-throw.test.ts
index 4e1724d4ea..d7b6b37ff0 100644
--- a/test/worktree-refresh-unexpected-throw.test.ts
+++ b/test/worktree-refresh-unexpected-throw.test.ts
@@ -10,6 +10,7 @@
*/
import { afterAll, beforeAll, expect, test } from 'bun:test';
import { readFileSync, writeFileSync } from 'node:fs';
+import { homedir } from 'node:os';
import { join } from 'node:path';
import postgres from '#postgres';
import type { BrainEngine } from '../src/core/engine.ts';
@@ -75,6 +76,28 @@ test('sources refresh prints an untyped throw as a refused document carrying the
expect(doc.fix).toContain('gbrain sources refresh source-a');
});
+test('a database access error the classifier recognizes is rethrown unchanged, keeping its own repair fix', async () => {
+ const refused = Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:5432'), { code: 'ECONNREFUSED' });
+ const fake = { kind: 'postgres', getConfig: async () => { throw refused; }, executeRaw: async () => { throw refused; } } as unknown as BrainEngine;
+ const failure = await withEnv({ GBRAIN_REFRESH_SYNC_BUDGET_MS: undefined }, () => refreshWorktree(fake, 'source-a').then(() => null, error => error));
+ expect(failure).toBe(refused);
+});
+
+test('an unexpected throw whose refresh row cannot be read says so, with credentials and the home directory redacted', async () => {
+ const dsn = ['postgres', '://example:prefix)', 'SYNTHETIC', '[email protected]/brain'].join('');
+ const statement = Object.assign(new Error(`canceling statement due to user request near ${dsn}`), { code: '57014' });
+ statement.stack = `Error: canceling statement\n at fence (${homedir()}/brain-checkout/src/x.ts:1:1)\n at ${dsn}`;
+ const fake = { kind: 'postgres', getConfig: async () => { throw statement; }, executeRaw: async () => { throw statement; } } as unknown as BrainEngine;
+ const failure = await withEnv({ GBRAIN_REFRESH_SYNC_BUDGET_MS: undefined }, () => refreshWorktree(fake, 'source-a').then(() => null, error => error));
+ expect(failure).toBeInstanceOf(OperationError);
+ expect(failure.code).toBe('internal_error');
+ expect(failure.suggestion).toContain('The refresh row could not be read');
+ expect(failure.message).not.toContain('SYNTHETIC_SECRET_SUFFIX');
+ expect(failure.why).not.toContain('SYNTHETIC_SECRET_SUFFIX');
+ expect(failure.why).not.toContain(homedir());
+ expect(failure.why).toContain('~/brain-checkout/src/x.ts');
+});
+
test('a host identity file that cannot be read still yields a typed internal_error refusal', () => withRefreshFixture(engine, async f => {
const hostFile = join(persistenceHome(), 'host.json');
const original = readFileSync(hostFile, 'utf8');
@@ -85,7 +108,7 @@ test('a host identity file that cannot be read still yields a typed internal_err
expect(failure).toBeInstanceOf(OperationError);
expect((failure as OperationError).code).toBe('internal_error');
expect((failure as OperationError).message).toContain('SyntaxError');
- expect((failure as OperationError).suggestion).toContain('No refresh row was left active.');
+ expect((failure as OperationError).suggestion).toContain('The refresh row could not be read');
}), 120_000);
/**