Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(embedded): private-visibility reads embedded identity through the leaf scope store

garrytanb41a9cd9d1b6
diff --git a/docs/TESTING.md b/docs/TESTING.md
index 47721250e5..2e90bc58e8 100644
--- a/docs/TESTING.md
+++ b/docs/TESTING.md
@@ -861,8 +861,7 @@ every scanner that names a split file, listed in
`scripts/check-layering.ts` (`bun run check:layering`, in `bun run verify`)
parses every file under `src/core/engine-sql/`, `src/core/schema-migrations/`
and `src/core/persistence/` and fails on any import, type-only included, of an
-engine façade (`pglite-engine.ts`, `postgres-engine.ts`, `engine-factory.ts`,
-`engine-construct.ts`)
+engine façade (`pglite-engine.ts`, `postgres-engine.ts`, `engine-factory.ts`)
from engine-sql, of `src/core/migrate.ts` from schema-migrations, or of
`src/core/ai/gateway.ts` from persistence. The first two directories are loaded
by the engines and by `migrate.ts`, so an import back up is an ESM cycle that
diff --git a/src/core/embedded-engine.ts b/src/core/embedded-engine.ts
index 76f1228248..92f0aa9e5a 100644
--- a/src/core/embedded-engine.ts
+++ b/src/core/embedded-engine.ts
@@ -1,6 +1,7 @@
import { engineKey } from './engine-key.ts';
import { DEFAULT_EMBEDDING_DIMENSIONS, DEFAULT_EMBEDDING_MODEL } from './ai/defaults.ts';
import { opError } from './ops/contract.ts';
+import { setEmbeddedKeyResolver } from './engine-scope-store.ts';
/**
* The value an embedding host passes to `openEngine` (engine-open.ts). Nothing in it is read from a config file, an
@@ -89,6 +90,7 @@ export function settingsConnectionOptions(url: string): { host: string; port: nu
const records = new WeakMap<object, EmbeddedRecord>();
let embeddedProcess = false;
let openEmbeddedEngines = 0;
+setEmbeddedKeyResolver(engine => (records.has(engineKey(engine)) ? engineKey(engine) : undefined));
function recordOf(engine: object): EmbeddedRecord | undefined {
return records.get(engineKey(engine));
diff --git a/src/core/engine-scope-store.ts b/src/core/engine-scope-store.ts
index 2daeceb2b6..a80c830d35 100644
--- a/src/core/engine-scope-store.ts
+++ b/src/core/engine-scope-store.ts
@@ -1,6 +1,7 @@
/**
- * The store engine-scope.ts reads, and the local scope the CLI, gbrain serve and the jobs worker enter at their entry
- * points. A leaf (no imports beyond node), so the CLI entry pays nothing to enter it.
+ * The store engine-scope.ts reads, the local scope the CLI, gbrain serve and the jobs worker enter at their entry
+ * points, and the embedded-engine lookup for leaf readers (search/private-visibility.ts). A leaf (no imports beyond
+ * node), so the CLI entry and the chunker graph pay nothing for it.
*/
import { AsyncLocalStorage } from 'node:async_hooks';
@@ -8,6 +9,24 @@ export const engineScopeStore = new AsyncLocalStorage<object>();
const LOCAL = Object.freeze({});
+/** Set by embedded-engine.ts when it loads (so before any engine is opened from settings): its engineKey, else undefined. */
+let embeddedKeyOf: (engine: object) => object | undefined = () => undefined;
+
+export function setEmbeddedKeyResolver(resolve: (engine: object) => object | undefined): void {
+ embeddedKeyOf = resolve;
+}
+
+/** The engineKey of `engine` when it was opened from settings, else undefined. */
+export function embeddedKey(engine: object): object | undefined {
+ return embeddedKeyOf(engine);
+}
+
+/** The engineKey of the embedded engine the current call runs for, else undefined. */
+export function embeddedKeyInScope(): object | undefined {
+ const engine = engineScopeStore.getStore();
+ return engine ? embeddedKeyOf(engine) : undefined;
+}
+
/** Runs `fn` as a call that belongs to no embedded engine: its AI calls use the process gateway, as before embedded engines existed. */
export function withLocalScope<T>(fn: () => T): T {
return engineScopeStore.run(LOCAL, fn);
diff --git a/src/core/search/private-visibility.ts b/src/core/search/private-visibility.ts
index 69514f80ac..0e8228e6ee 100644
--- a/src/core/search/private-visibility.ts
+++ b/src/core/search/private-visibility.ts
@@ -22,9 +22,7 @@
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 { embeddedKey, embeddedKeyInScope } from '../engine-scope-store.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';
@@ -503,16 +501,15 @@ const aliasOptOutEngines = new Set<BrainEngine>();
* 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);
+const embeddedPolicyOf = (key: object) => {
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;
+ const scoped = embeddedKeyInScope();
+ if (scoped) return embeddedPolicy.get(scoped)?.honorAliases ?? true;
return honorAliasEngines.size > 0 || aliasOptOutEngines.size === 0;
}
@@ -520,15 +517,17 @@ function honorAliases(): boolean {
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;
+ const embedded = embeddedKey(engine);
+ if (embedded) {
+ embeddedPolicyOf(embedded).honorAliases = !optOut;
return !optOut;
}
(optOut ? aliasOptOutEngines : honorAliasEngines).add(engine);
(optOut ? honorAliasEngines : aliasOptOutEngines).delete(engine);
return !optOut;
} catch {
- return isEmbeddedEngine(engine) ? embeddedPolicy.get(engineKey(engine))?.honorAliases ?? true : !aliasOptOutEngines.has(engine);
+ const embedded = embeddedKey(engine);
+ return embedded ? embeddedPolicy.get(embedded)?.honorAliases ?? true : !aliasOptOutEngines.has(engine);
}
}
@@ -757,8 +756,8 @@ export function __resetPrivateVisibilityCacheForTests(): void {
*/
function processPrivacyRelaxed(): boolean {
if (process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1') return true;
- const scoped = embeddedEngineInScope();
- if (scoped) return embeddedPolicy.get(engineKey(scoped))?.expose === true;
+ const scoped = embeddedKeyInScope();
+ if (scoped) return embeddedPolicy.get(scoped)?.expose === true;
return exposeEngines.size > 0 && strictEngines.size === 0;
}
@@ -782,8 +781,9 @@ 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;
+ const embedded = embeddedKey(engine);
+ if (embedded) {
+ embeddedPolicyOf(embedded).expose = expose;
return expose;
}
(expose ? exposeEngines : strictEngines).add(engine);