Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

perf(access): a remote write's admission reuses the request's fresh policy read (GBRA-218)

garrytan97cbebf3963e
diff --git a/src/core/access/caller.ts b/src/core/access/caller.ts
index 1a62a9e8a1..703eea252b 100644
--- a/src/core/access/caller.ts
+++ b/src/core/access/caller.ts
@@ -13,7 +13,7 @@
*/
import type { BrainEngine } from '../engine.ts';
import type { AuthInfo } from '../ops/contract.ts';
-import { accessRestrictionsPresent, remotePrivacyRelaxed, type CallerAccess } from '../search/private-visibility.ts';
+import { readRemotePolicyNow, remotePrivacyRelaxed, type CallerAccess } from '../search/private-visibility.ts';
import { ACCESS_ID_RE, identityFor, normalizePrincipal } from './principals.ts';
/** Config key binding a local stdio MCP server to one member. */
@@ -67,7 +67,8 @@ export async function stdioMemberIdentity(engine: BrainEngine): Promise<string[]
/** The CallerAccess fields for a remote operation (identity, privacy relaxation and the GBRA-156 private-readable set). */
export async function remoteCallerAccess(engine: BrainEngine, auth: AuthInfo | undefined, privateSources: readonly string[] | undefined, transport?: string): Promise<Partial<CallerAccess>> {
- const [relaxed, restricted] = await Promise.all([remotePrivacyRelaxed(engine), accessRestrictionsPresent(engine)]);
+ const [relaxed, remotePolicy] = await Promise.all([remotePrivacyRelaxed(engine), readRemotePolicyNow(engine)]);
+ const restricted = remotePolicy.restricted;
const identity = !restricted ? []
: !auth && transport === 'stdio' ? await stdioMemberIdentity(engine)
: await memberIdentityFor(engine, auth);
@@ -77,6 +78,7 @@ export async function remoteCallerAccess(engine: BrainEngine, auth: AuthInfo | u
hidePrivate: !relaxed,
applyRemoteReadPolicy: false,
restricted,
+ remotePolicy,
};
}
diff --git a/src/core/persistence/authority.ts b/src/core/persistence/authority.ts
index fb76323cb1..82cbad740c 100644
--- a/src/core/persistence/authority.ts
+++ b/src/core/persistence/authority.ts
@@ -9,7 +9,7 @@ import { normalizeTokenScopes } from '../legacy-token-scope.ts';
import { authSourcesFromGrant, grantFromTokenRow } from '../grants/model.ts';
import { readLocalWriter, currentVerifiedLocalWriter, takeRegisteredWriterRow, verifyLocalWriter, type LocalGrant } from './identity.ts';
import type { Principal, SqlEngine, WriteAuthority, WriteRequest } from './model.ts';
-import { authorizePageVisibility, excludesPrivateWrites } from './page-visibility.ts';
+import { authorizePageVisibility, excludesPrivateWritesAtAdmission } from './page-visibility.ts';
import { pipelined, transactionMemo } from '../page-state/transactions.ts';
function deny(message: string): never { throw new OperationError('permission_denied', message, 'Inspect the current writer registration and source/operation grants.'); }
@@ -59,7 +59,7 @@ export async function submissionAuthority(ctx: OperationContext, operation: stri
const delegatedJobId = ctx.auth && ctx.viaSubagent ? jobNamespaceKey(ctx.subagentId) : undefined;
const a: WriteAuthority = {
version: 1, principal, remote: ctx.remote !== false, sourceId, sourceIncarnation,
- excludePrivate: await excludesPrivateWrites(ctx.engine, ctx.remote !== false),
+ excludePrivate: await excludesPrivateWritesAtAdmission(ctx.engine, ctx.remote !== false),
autoLinkTrusted: ctx.remote === false || ctx.viaSubagent === true && !ctx.auth && !!ctx.allowedSlugPrefixes?.length,
takesHolders: ctx.remote === false ? null : [...(ctx.takesHoldersAllowList ?? ['world'])],
scopes: [...(ctx.auth?.scopes ?? localGrant?.scopes ?? [])],
diff --git a/src/core/persistence/page-visibility.ts b/src/core/persistence/page-visibility.ts
index 3f128e51ab..cddad8b844 100644
--- a/src/core/persistence/page-visibility.ts
+++ b/src/core/persistence/page-visibility.ts
@@ -1,5 +1,5 @@
import { opError } from '../ops/contract.ts';
-import { callerAccessAllowedSql, callerIdentity, readPolicyFragment, readRemotePolicyNow } from '../search/private-visibility.ts';
+import { callerAccessAllowedSql, callerIdentity, currentCallerAccess, readPolicyFragment, readRemotePolicyNow } from '../search/private-visibility.ts';
import { parseMarkdown } from '../markdown.ts';
import { memberIdentityFor } from '../access/caller.ts';
import { hasReaders, normalizePrincipal, normalizeReaders, readersAllow, NOBODY } from '../access/principals.ts';
@@ -12,6 +12,15 @@ export async function excludesPrivateWrites(engine: SqlEngine, remote: boolean):
return !(await readRemotePolicyNow(engine)).privacyRelaxed;
}
+/**
+ * excludesPrivateWrites for a write's admission: reuses the fresh policy this remote request read when it started
+ * (remoteCallerAccess) instead of reading it again. A false result is rechecked fresh at publication.
+ */
+export async function excludesPrivateWritesAtAdmission(engine: SqlEngine, remote: boolean): Promise<boolean> {
+ if (!remote) return false;
+ return !(currentCallerAccess().remotePolicy ?? await readRemotePolicyNow(engine)).privacyRelaxed;
+}
+
/**
* Recheck after page guards at publication; also hide inaccessible receipt targets. GBRA-218: on a brain with
* member-access restrictions the writer's identity is re-resolved from the stored principal on every check, so a
diff --git a/src/core/search/private-visibility.ts b/src/core/search/private-visibility.ts
index bb397779cc..b7c2c72a00 100644
--- a/src/core/search/private-visibility.ts
+++ b/src/core/search/private-visibility.ts
@@ -159,12 +159,14 @@ export interface CallerAccess {
hidePrivate: boolean;
applyRemoteReadPolicy: boolean;
restricted: boolean;
+ /** The fresh remote policy (readRemotePolicyNow) a remote request read when it started; null outside one. */
+ remotePolicy: RemotePolicy | null;
}
const callerAccessStore = new AsyncLocalStorage<CallerAccess>();
/** The current read's caller-access record (see CallerAccess). */
export function currentCallerAccess(): CallerAccess {
- return callerAccessStore.getStore() ?? { identity: [], privateReadableSources: [], hidePrivate: !processPrivacyRelaxed(), applyRemoteReadPolicy: false, restricted: true };
+ return callerAccessStore.getStore() ?? { identity: [], privateReadableSources: [], hidePrivate: !processPrivacyRelaxed(), applyRemoteReadPolicy: false, restricted: true, remotePolicy: null };
}
/** Runs `fn` under `access` merged over the current record; an invalid identity or source id empties that set (fail closed). */
@@ -178,6 +180,7 @@ export function withCallerAccess<T>(access: Partial<CallerAccess>, fn: () => T):
hidePrivate: access.hidePrivate ?? base.hidePrivate,
applyRemoteReadPolicy: access.applyRemoteReadPolicy ?? base.applyRemoteReadPolicy,
restricted: access.restricted ?? base.restricted,
+ remotePolicy: access.remotePolicy ?? base.remotePolicy,
}), fn);
}
@@ -604,6 +607,8 @@ export async function remotePrivacyRelaxed(engine: BrainEngine): Promise<boolean
return expose;
}
+export interface RemotePolicy { privacyRelaxed: boolean; restricted: boolean }
+
/**
* The remote read policy read fresh, for publication: whether an operator
* relaxed privacy (the env hatch or the config opt-out, uncached) and whether
@@ -611,7 +616,7 @@ export async function remotePrivacyRelaxed(engine: BrainEngine): Promise<boolean
* transaction included). An unreadable config counts as not opted out and
* restricted (fail closed); the env hatch needs no read.
*/
-export async function readRemotePolicyNow(engine: Pick<BrainEngine, 'executeRaw'>): Promise<{ privacyRelaxed: boolean; restricted: boolean }> {
+export async function readRemotePolicyNow(engine: Pick<BrainEngine, 'executeRaw'>): Promise<RemotePolicy> {
const hatch = process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1';
try {
const rows = await engine.executeRaw<{ key: string; value: string }>('SELECT key, value FROM config WHERE key = ANY($1::text[])', [[REMOTE_PRIVATE_PAGES_KEY, ACCESS_RESTRICTIONS_KEY]]);