Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

GBRA-218 (A2): tightenToStoredPage keeps extract-conversation-facts within its size ceiling; lane the lock-order Postgres arm

garrytan342d486f8f1b
diff --git a/src/commands/extract-conversation-facts.ts b/src/commands/extract-conversation-facts.ts
index 4618042e2c..46e11a8040 100644
--- a/src/commands/extract-conversation-facts.ts
+++ b/src/commands/extract-conversation-facts.ts
@@ -104,8 +104,7 @@ import { writeReceipt, shortRunId } from '../core/extract/receipt-writer.ts';
import { upsertExtractRollup, classifyRunStop } from '../core/extract/rollup-writer.ts';
import { ALLOWED_TYPES, ALLOWED_TYPE_ALIASES, isConversationFactsEligiblePage, pageTypesForAllowed, requireParseableConversationFlag, type AllowedType } from '../core/facts/conversation-types.ts';
import { TERMINAL_AUDIT_SOURCE, NON_EXTRACTABLE_AUDIT_SOURCE, stampExtractorVersion } from '../core/facts/audit-sources.ts';
-import { resolveDefaultVisibility, tightenToPage, type FactVisibility } from '../core/facts/visibility.ts';
-import { folderRestricted } from '../core/search/private-visibility.ts';
+import { resolveDefaultVisibility, tightenToStoredPage, type FactVisibility } from '../core/facts/visibility.ts';
import {
emptySaveTimeResolutionCounts,
formatSaveTimeResolutionCounts,
@@ -1017,7 +1016,7 @@ async function processPage(
let segmentsThisPage = 0;
const pageRows: Array<NewFact & { row_num: number; source_markdown_slug: string }> = [];
const pageResolution = emptySaveTimeResolutionCounts();
- const factVisibility = tightenToPage(state.factVisibility, { ...page, folder_restricted: await folderRestricted(state.engine, state.sourceId, page.slug) });
+ const factVisibility = await tightenToStoredPage(state.engine, state.sourceId, state.factVisibility, page);
const entityHints = await conversationEntityHints(state.engine, state.sourceId, page.slug, factVisibility); // #6731
for (const seg of segments) {
diff --git a/src/core/facts/backstop.ts b/src/core/facts/backstop.ts
index d048ae24d1..6fad1245a9 100644
--- a/src/core/facts/backstop.ts
+++ b/src/core/facts/backstop.ts
@@ -42,7 +42,6 @@ import { factEventTime } from './event-time.ts';
import type { BrainEngine, FactInsertStatus, NewFact } from '../engine.ts';
import type { ResolutionSource } from '../entities/resolve.ts';
import { isFactsBackstopEligible } from './eligibility.ts';
-import { folderRestricted } from '../search/private-visibility.ts';
import type { PageType } from '../types.ts';
import type { OperationContext } from '../ops/contract.ts';
import type { WriteReceipt } from '../persistence/types.ts';
@@ -369,9 +368,8 @@ export async function runFactsBackstop(
: { mode: 'inline', inserted: 0, duplicate: 0, superseded: 0, fact_ids: [], skipped };
}
// Facts extracted from a private page stay private, whatever the caller or the brain default says.
- const { tightenToPage } = await import('./visibility.ts');
- const folder_restricted = await folderRestricted(ctx.engine, ctx.sourceId, parsedPage.slug);
- if (tightenToPage('world', { ...parsedPage, folder_restricted }) === 'private') ctx = { ...ctx, visibility: 'private' };
+ const { tightenToStoredPage } = await import('./visibility.ts');
+ if (await tightenToStoredPage(ctx.engine, ctx.sourceId, 'world', parsedPage) === 'private') ctx = { ...ctx, visibility: 'private' };
const { managedPersistenceEnabled } = await import('../persistence/ownership.ts');
if (await managedPersistenceEnabled(ctx.engine)) {
if (mode !== 'inline') {
diff --git a/src/core/facts/visibility.ts b/src/core/facts/visibility.ts
index c0c511ac0c..203a5e7216 100644
--- a/src/core/facts/visibility.ts
+++ b/src/core/facts/visibility.ts
@@ -20,7 +20,7 @@
*/
import type { BrainEngine } from '../engine.ts';
-import { isDerivationPrivate } from '../search/private-visibility.ts';
+import { folderRestricted, isDerivationPrivate } from '../search/private-visibility.ts';
export type FactVisibility = 'private' | 'world';
@@ -66,13 +66,19 @@ export async function resolveVisibilityParam(
* Facts derived from a page never take a broader visibility than that page: a
* `visibility: private` page, a derived page that reads as private, a page
* carrying a privacy marker (`access: private`, a private `confidentiality`) or
- * a page under a folder rule (`folder_restricted`, from a snapshot or
- * folderRestricted) yields private facts whatever the caller or the brain default asked for.
+ * a page under a folder rule (`folder_restricted`, see tightenToStoredPage) yields private facts whatever the caller
+ * or the brain default asked for.
*/
export function tightenToPage(visibility: FactVisibility, page: { type?: string | null; frontmatter?: unknown; folder_restricted?: boolean | null } | null | undefined): FactVisibility {
return page && isDerivationPrivate(page) ? 'private' : visibility;
}
+/** tightenToPage for a page of `sourceId` known by its slug, its folder restriction looked up (folderRestricted). */
+export async function tightenToStoredPage(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string, visibility: FactVisibility,
+ page: { slug: string; type?: string | null; frontmatter?: unknown }): Promise<FactVisibility> {
+ return tightenToPage(visibility, { ...page, folder_restricted: await folderRestricted(engine, sourceId, page.slug) });
+}
+
/**
* `remember` without a `visibility`: the v1 default is `world`, except where
* that would widen what the caller is saving into. A private target page, an
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index 0bcf504bf2..3c90092544 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -13,6 +13,7 @@
# themselves from scripts/postgres-arm-weights.json.
test/access-as.test.ts
test/access-binding.test.ts
+test/access-folder-lock-order.test.ts
test/access-folder-rules.test.ts
test/access-members.test.ts
test/access-writes.test.ts