Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(embedded): alias and remote-privacy policy are per engine; closed engines are not retained (B3a F)

garrytan8fcdaac7ddc1
diff --git a/src/core/search/private-visibility.ts b/src/core/search/private-visibility.ts
index ae578d9442..69514f80ac 100644
--- a/src/core/search/private-visibility.ts
+++ b/src/core/search/private-visibility.ts
@@ -22,6 +22,9 @@
import { AsyncLocalStorage } from 'node:async_hooks';
import type { BrainEngine } from '../engine.ts';
+import { embeddedEngineInScope } from '../engine-scope.ts';
+import { isEmbeddedEngine } from '../embedded-engine.ts';
+import { engineKey } from '../engine-key.ts';
import { requestConfigRows } from '../config-snapshot.ts';
import { isValidSourceId } from '../source-id.ts';
import { ACCESS_ID_RE, hasReaders, MEMBER_PAGE_PREFIX, normalizePrincipal, readersAllow, validIdentity } from '../access/principals.ts';
@@ -495,20 +498,37 @@ export function privacyAliasSql(alias: string): string {
*/
const honorAliasEngines = new Set<BrainEngine>();
const aliasOptOutEngines = new Set<BrainEngine>();
+/**
+ * An engine opened from settings keeps its own policy here instead (keyed by engineKey, so it goes with the engine and a
+ * per-call engine is never retained): inside its scope its own setting decides, and it never sways the process sets.
+ */
+let embeddedPolicy = new WeakMap<object, { honorAliases?: boolean; expose?: boolean }>();
+const embeddedPolicyOf = (engine: object) => {
+ const key = engineKey(engine);
+ let policy = embeddedPolicy.get(key);
+ if (!policy) embeddedPolicy.set(key, policy = {});
+ return policy;
+};
const ALIAS_OPT_OUT_VALUES = ['false', '0', 'off', 'no'];
function honorAliases(): boolean {
+ const scoped = embeddedEngineInScope();
+ if (scoped) return embeddedPolicy.get(engineKey(scoped))?.honorAliases ?? true;
return honorAliasEngines.size > 0 || aliasOptOutEngines.size === 0;
}
-/** Reads this brain's `visibility.honor_aliases` into the process setting; true unless it is false/0/off/no. Unreadable keeps the last value. */
+/** Reads this brain's `visibility.honor_aliases` into the process setting (an embedded engine's: its own); true unless it is false/0/off/no. Unreadable keeps the last value. */
export async function readHonorAliases(engine: BrainEngine): Promise<boolean> {
try {
const optOut = ALIAS_OPT_OUT_VALUES.includes((await engine.getConfig(HONOR_ALIASES_KEY))?.trim().toLowerCase() ?? '');
+ if (isEmbeddedEngine(engine)) {
+ embeddedPolicyOf(engine).honorAliases = !optOut;
+ return !optOut;
+ }
(optOut ? aliasOptOutEngines : honorAliasEngines).add(engine);
(optOut ? honorAliasEngines : aliasOptOutEngines).delete(engine);
return !optOut;
} catch {
- return !aliasOptOutEngines.has(engine);
+ return isEmbeddedEngine(engine) ? embeddedPolicy.get(engineKey(engine))?.honorAliases ?? true : !aliasOptOutEngines.has(engine);
}
}
@@ -715,9 +735,15 @@ const strictEngines = new Set<BrainEngine>();
/** GBRA-218: set once a page names readers or a member exists (sticky; a trigger and `gbrain access` write it). */
export const ACCESS_RESTRICTIONS_KEY = 'access.restrictions_present';
+/** @internal exported for tests: engines the process-wide policy sets hold. */
+export function __aliasPolicyEngineCountForTests(): number {
+ return honorAliasEngines.size + aliasOptOutEngines.size + exposeEngines.size + strictEngines.size;
+}
+
/** Test helper: drop the per-engine config cache. */
export function __resetPrivateVisibilityCacheForTests(): void {
cache = new WeakMap();
+ embeddedPolicy = new WeakMap();
honorAliasEngines.clear();
aliasOptOutEngines.clear();
exposeEngines.clear();
@@ -730,7 +756,10 @@ export function __resetPrivateVisibilityCacheForTests(): void {
* strict (the same lean-private rule as visibility.honor_aliases).
*/
function processPrivacyRelaxed(): boolean {
- return process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1' || (exposeEngines.size > 0 && strictEngines.size === 0);
+ if (process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1') return true;
+ const scoped = embeddedEngineInScope();
+ if (scoped) return embeddedPolicy.get(engineKey(scoped))?.expose === true;
+ return exposeEngines.size > 0 && strictEngines.size === 0;
}
/**
@@ -753,6 +782,10 @@ export async function remotePrivacyRelaxed(engine: BrainEngine): Promise<boolean
}
await readHonorAliases(engine);
cache.set(engine, { at: Date.now(), expose });
+ if (isEmbeddedEngine(engine)) {
+ embeddedPolicyOf(engine).expose = expose;
+ return expose;
+ }
(expose ? exposeEngines : strictEngines).add(engine);
(expose ? strictEngines : exposeEngines).delete(engine);
return expose;
diff --git a/test/engine-alias-policy.test.ts b/test/engine-alias-policy.test.ts
new file mode 100644
index 0000000000..d0c89bedd1
--- /dev/null
+++ b/test/engine-alias-policy.test.ts
@@ -0,0 +1,75 @@
+/**
+ * GBRA-283 B3a (N2): visibility.honor_aliases and search.remote_private_pages are per engine for engines opened from
+ * settings. Inside such an engine's scope its own setting decides (one brain's setting never decides another's), and
+ * the process-wide sets that the CLI's lean-private rule reads never hold an embedded engine, so a per-call engine is
+ * not retained after it closes. The CLI keeps its process-wide behaviour.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import { randomUUID } from 'node:crypto';
+import { openEngine, withEngineScope, withLocalScope, type EngineSettings } from '../src/core/engine-open.ts';
+import { createEngine } from '../src/core/engine-factory.ts';
+import {
+ HONOR_ALIASES_KEY, REMOTE_PRIVATE_PAGES_KEY, __aliasPolicyEngineCountForTests, __resetPrivateVisibilityCacheForTests,
+ isPrivacyPrivatePage, readHonorAliases, remotePrivacyRelaxed,
+} from '../src/core/search/private-visibility.ts';
+
+const settings = (): EngineSettings => ({ embedded_core: true, engine: 'pglite', database: {}, host: { id: randomUUID() } });
+const aliased = { type: 'note', frontmatter: { access: 'private' } };
+
+afterEach(() => __resetPrivateVisibilityCacheForTests());
+
+describe('per-engine alias and privacy policy', () => {
+ test('two embedded engines with opposite visibility.honor_aliases each follow their own', async () => {
+ const off = await openEngine(settings());
+ const on = await openEngine(settings());
+ try {
+ await off.initSchema();
+ await on.initSchema();
+ await off.setConfig(HONOR_ALIASES_KEY, 'false');
+ for (const order of [[off, on], [on, off]]) {
+ __resetPrivateVisibilityCacheForTests();
+ for (const engine of order) await readHonorAliases(engine);
+ expect(withEngineScope(off, () => isPrivacyPrivatePage(aliased))).toBe(false);
+ expect(withEngineScope(on, () => isPrivacyPrivatePage(aliased))).toBe(true);
+ }
+ } finally {
+ await Promise.all([off.disconnect(), on.disconnect()]);
+ }
+ }, 120_000);
+
+ test('the CLI keeps the process-wide lean-private rule', async () => {
+ const off = await createEngine({ engine: 'pglite' });
+ const on = await createEngine({ engine: 'pglite' });
+ try {
+ await off.connect({ engine: 'pglite' } as never);
+ await on.connect({ engine: 'pglite' } as never);
+ await off.initSchema();
+ await on.initSchema();
+ await off.setConfig(HONOR_ALIASES_KEY, 'false');
+ await readHonorAliases(off);
+ expect(withLocalScope(() => isPrivacyPrivatePage(aliased))).toBe(false);
+ await readHonorAliases(on);
+ expect(withLocalScope(() => isPrivacyPrivatePage(aliased))).toBe(true);
+ } finally {
+ await Promise.all([off.disconnect(), on.disconnect()]);
+ }
+ }, 120_000);
+
+ test('open, read and close cycles retain no embedded engine in the policy sets', async () => {
+ const refs: WeakRef<object>[] = [];
+ for (let i = 0; i < 5; i++) {
+ const engine = await openEngine(settings());
+ await engine.initSchema();
+ await engine.setConfig(REMOTE_PRIVATE_PAGES_KEY, i % 2 ? 'visible' : 'hidden');
+ await withEngineScope(engine, () => remotePrivacyRelaxed(engine));
+ await readHonorAliases(engine);
+ refs.push(new WeakRef(engine));
+ await engine.disconnect();
+ }
+ expect(__aliasPolicyEngineCountForTests()).toBe(0);
+ Bun.gc(true);
+ await Bun.sleep(50);
+ Bun.gc(true);
+ expect(refs.filter(ref => ref.deref() !== undefined).length).toBeLessThan(refs.length);
+ }, 120_000);
+});