Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Merge origin/capy/gbra218-member-reads (714ea87e) into capy/gbra218-folder-rules

garrytan0bd82f8d216f
diff --cc scripts/bench-member-access.ts
index 88cd1d4cd0,098f55aae6..7c9831a2ae
--- a/scripts/bench-member-access.ts
+++ b/scripts/bench-member-access.ts
@@@ -72,18 -69,16 +72,21 @@@ async function seed(engine: PGLiteEngin
}
}
-async function runCell(share: number) {
+async function runCell(share: number, rules: number) {
const engine = await newEngine();
const hasMembers = (await engine.executeRaw<{ t: string | null }>("SELECT to_regclass('access_members')::text AS t"))[0]?.t != null;
- if (share > 0 && !hasMembers) { await engine.disconnect(); return null; }
+ const hasRules = (await engine.executeRaw<{ t: string | null }>("SELECT to_regclass('access_rules')::text AS t"))[0]?.t != null;
+ if ((share > 0 && !hasMembers) || (rules > 0 && !hasRules)) { await engine.disconnect(); return null; }
await seed(engine, share);
+ if (rules > 0) {
+ const { proposeRevision } = await import('../src/core/access/policy.ts');
+ await proposeRevision(engine, SRC, Array.from({ length: rules }, (_, k) => ({ id: `r-${k}`, prefix: `restricted/r-${k}/`, readable_by: ['member:alice-example'] })), { origin: 'cli', store: 'database' });
+ }
- if (hasMembers) await engine.executeRaw("INSERT INTO access_members (id) VALUES ('alice-example') ON CONFLICT DO NOTHING");
+ // Share 0 is a brain with no restriction at all (no member, no readers), the case nearly every brain is in; its
+ // "member" token is a second unbound token. Restricted shares create the member and bind it.
+ const bindMember = hasMembers && share > 0;
+ if (bindMember) await engine.executeRaw("INSERT INTO access_members (id) VALUES ('alice-example') ON CONFLICT DO NOTHING");
const mint = async (name: string) => mintLegacyToken(engine, { name, scopes: ['read'], takesHolders: ['world'], sourceGrant: [SRC] });
const unbound = await mint('bench-unbound');
const alice = await mint('bench-alice');
diff --cc src/core/search/private-visibility.ts
index 4bef690279,fd5083ae84..a9a7c36258
--- a/src/core/search/private-visibility.ts
+++ b/src/core/search/private-visibility.ts
@@@ -69,40 -68,9 +69,41 @@@ function privacyPartSql(pageAlias: stri
* `identity` must pass validIdentity (its alphabet has no quote), so it is
* embedded as literals; anything else empties it (fail closed).
*/
-export function accessAllowedSql(pageAlias: string, identity: readonly string[]): string {
+export function accessAllowedSql(pageAlias: string, identity: readonly string[], satisfied: readonly string[] = satisfiedFor(identity)): string {
+ if (!currentCallerAccess().restricted) return `(${pageAlias}.page_readers IS NULL)`;
- return `(${readersAllowedSql(pageAlias, identity)} AND ${originAllowedSql(pageAlias)})`;
+ return `(${readersAllowedSql(pageAlias, identity)} AND ${folderRulesAllowedSql(pageAlias, satisfied)} AND ${originAllowedSql(pageAlias)})`;
+}
+
+/**
+ * GBRA-218 (PR A2): the folder layer. `folder_rule_ids` holds the source-qualified ids
+ * of every applied folder rule whose prefix the slug starts with (a trigger stamps it from the applied rulebook,
+ * migration v275); a page passes only when the caller satisfies all of them (`<@`, set semantics), so nested member
+ * and group rules both have to pass. `satisfied` is the caller's satisfied set (CallerAccess.satisfiedRuleIds); an
+ * entry that is not a valid qualified id empties it (fail closed). Prefixes under a narrowing still being stamped,
+ * or touched by a policy file that does not validate, read as nobody (CallerAccess.folderCoverage). Column tests
+ * lead, so a page under no rule costs one NULL test.
+ */
+export function folderRulesAllowedSql(alias: string, satisfied: readonly string[]): string {
+ const ids = satisfied.length && satisfied.every(validQualifiedRuleId) ? [...new Set(satisfied)].sort() : [];
+ const listed = ids.length ? ` OR ${alias}.folder_rule_ids <@ ARRAY[${ids.map(id => `'${id}'`).join(', ')}]::text[]` : '';
+ return `(${alias}.folder_rule_ids IS NULL${listed})${coverageSql(alias)}`;
+}
+
+/** The pending or invalid prefixes of the current record as a conjunct (empty when none); an ill-formed entry hides everything. */
+function coverageSql(alias: string): string {
+ const coverage = currentCallerAccess().folderCoverage;
+ if (!coverage.length) return '';
+ return coverage.map(c => !isValidSourceId(c.source_id) || (c.prefix !== '' && !validPrefix(c.prefix)) ? ' AND FALSE'
+ : c.prefix === '' ? ` AND ${alias}.source_id <> '${c.source_id}'`
+ : ` AND NOT (${alias}.source_id = '${c.source_id}' AND starts_with(${alias}.slug, '${c.prefix}'))`).join('');
+}
+
+/** The satisfied rule ids for `identity`: the current record's set when it is the record's identity, else none. */
+function satisfiedFor(identity: readonly string[]): readonly string[] {
+ const access = currentCallerAccess();
+ if (!identity.length || identity.length !== access.identity.length) return [];
+ const sorted = [...identity].sort();
+ return sorted.every((p, i) => p === access.identity[i]) ? access.satisfiedRuleIds : [];
}
/**
@@@ -184,13 -158,13 +195,14 @@@ export interface CallerAccess
privateReadableSources: readonly string[];
hidePrivate: boolean;
applyRemoteReadPolicy: boolean;
+ restricted: boolean;
}
+export interface FolderCoverage { source_id: string; prefix: string }
const callerAccessStore = new AsyncLocalStorage<CallerAccess>();
/** The current read's caller-access record (see CallerAccess). */
export function currentCallerAccess(): CallerAccess {
- return callerAccessStore.getStore() ?? { identity: [], satisfiedRuleIds: [], folderCoverage: [], privateReadableSources: [], hidePrivate: !processPrivacyRelaxed(), applyRemoteReadPolicy: false };
- return callerAccessStore.getStore() ?? { identity: [], privateReadableSources: [], hidePrivate: !processPrivacyRelaxed(), applyRemoteReadPolicy: false, restricted: true };
++ return callerAccessStore.getStore() ?? { identity: [], satisfiedRuleIds: [], folderCoverage: [], privateReadableSources: [], hidePrivate: !processPrivacyRelaxed(), applyRemoteReadPolicy: false, restricted: true };
}
/** Runs `fn` under `access` merged over the current record; an invalid identity or source id empties that set (fail closed). */