Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(access): a database that is down reads as its own error, not requester_scope_unknown
garrytan569594f0b8f7
diff --git a/src/core/access/requester-scope.ts b/src/core/access/requester-scope.tsindex 9f0d00cc1b..a46771e24d 100644--- a/src/core/access/requester-scope.ts+++ b/src/core/access/requester-scope.ts@@ -28,6 +28,7 @@ import { hasScope } from '../scope.ts';import { createAuditWriter } from '../audit/audit-writer.ts';import { credentialScopeFields } from './token-binding.ts';import { stdioBindRoute } from './stdio-bind-route.ts';+import { classifyPgAccessError } from '../pg-access-classify.ts';/*** Read operations that read no page: the caller's own identity, the brain's identity and health, search mode names,@@ -148,13 +149,17 @@ function boundToSomeone(access: Partial<CallerAccess>): boolean {* The access a requester-scope decision reads: unchanged unless whom the brain serves could not be read (servesUnknown)* and the call is from a connection bound to nobody, which gets one targeted re-read of the serves keys. A second* failure leaves it unknown, and requesterScopeGate refuses (requester_scope_unknown) rather than read it as one person.+ * A second failure the database-error envelope already names (the database unreachable, the schema missing) is+ * rethrown to it instead; a dropped connection or a timeout stays requester_scope_unknown, since the next read succeeds.*/export async function withServesReread<T extends Partial<CallerAccess>>(engine: Pick<BrainEngine, 'executeRaw'>, access: T, auth: Pick<ScopeAuth, 'readsUnscoped'> | undefined): Promise<T> {const policy = access.remotePolicy;if (!policy?.servesUnknown || boundToSomeone(access) || auth?.readsUnscoped) return access;try {return { ...access, remotePolicy: { ...policy, serves: await readServesPolicy(engine), servesUnknown: false } };- } catch {+ } catch (e) {+ const reason = classifyPgAccessError(e).reason;+ if (reason !== 'unknown' && reason !== 'conn_dropped') throw e;return access;}}diff --git a/src/mcp/dispatch.ts b/src/mcp/dispatch.tsindex a6e9851f5f..50f73e11b6 100644--- a/src/mcp/dispatch.ts+++ b/src/mcp/dispatch.ts@@ -1037,15 +1037,21 @@ export async function dispatchToolCall(if (opts.enforcedMember) access = { ...access, hidePrivate: true, owner: false, privateReadableSources: [] };// GST-363: on a brain that serves several people, a read from a connection bound to nobody is refused (or, in its grace, noticed) before any work.// An op this connection cannot reach (hidden by its surface, localOnly off stdio, or cliOnly over MCP) keeps its own refusal instead.- const reachable = !!op && (!opts.allowedOps || opts.allowedOps.has(name)) && (!op.localOnly || opts.transport === 'stdio') && !(op.cliOnly && opts.remote !== false);- if (!attested?.actor && reachable && requiresRequesterScope(op, opts.auth)) {- const credential = opts.auth ? await credentialScopeFor(engine, opts.auth) : undefined;- access = await withServesReread(engine, access, credential);- scopeNotice = requesterScopeGate(op, access, credential, opts.transport);- } else if (!attested?.actor && reachable && scoped.metaHook) {- // GST-363: the ops the gate lets through (exempt reads, writes) carry no hot memory to a connection whose reads it refuses.- const credential = opts.auth ? await credentialScopeFor(engine, opts.auth) : undefined;- if (requesterScopeRefuses(await withServesReread(engine, access, credential), credential, opts.transport)) scoped = { ...scoped, metaHook: undefined };+ const reachable = !!op && (!opts.allowedOps || opts.allowedOps.has(name)) && (!op.localOnly || opts.transport === 'stdio') && !op.cliOnly;+ try {+ if (!attested?.actor && reachable && requiresRequesterScope(op, opts.auth)) {+ const credential = opts.auth ? await credentialScopeFor(engine, opts.auth) : undefined;+ access = await withServesReread(engine, access, credential);+ scopeNotice = requesterScopeGate(op, access, credential, opts.transport);+ } else if (!attested?.actor && reachable && scoped.metaHook) {+ // GST-363: the ops the gate lets through (exempt reads, writes) carry no hot memory to a connection whose reads it refuses.+ const credential = opts.auth ? await credentialScopeFor(engine, opts.auth) : undefined;+ if (requesterScopeRefuses(await withServesReread(engine, access, credential), credential, opts.transport)) scoped = { ...scoped, metaHook: undefined };+ }+ } catch (dbFailure) {+ // The serves re-read met a database that is unreachable or missing its schema: its own envelope, nothing read.+ if (dbFailure instanceof OperationError) throw dbFailure;+ return errorResult(dbFailure, opts, { op: name });}} catch (thrown) {const e = identityLookupUnavailable(thrown) ?? thrown;