Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(g6): extract_entities on an unmanaged brain refuses a git-canonical source before its direct-write fallback can write the database first (GBRA-60)

garrytan39c9ec178786
diff --git a/src/core/enrichment-service.ts b/src/core/enrichment-service.ts
index c73cad90c8..744b69d19b 100644
--- a/src/core/enrichment-service.ts
+++ b/src/core/enrichment-service.ts
@@ -34,6 +34,7 @@ import { isJunkEntityName } from './entity-name-quality.ts';
import { followRetirement } from './entities/resolve.ts';
import { maintenanceTransaction } from './persistence/attribution.ts';
import { managedPersistenceEnabled } from './persistence/ownership.ts';
+import { assertUnmanagedCanonicalWriter } from './persistence/maintenance.ts';
import { deriveTrust, derivedMaintenanceTransaction, lowerDerivedPage, lowerDerivedPageIn, lowerToDerivedTier } from './trust/taint.ts';
import { derivedGateConfig, derivedGateInput, recordTimelineFlag, timelineRowAllowed } from './trust/derived-gate.ts';
import { assessTimelineForGate } from './write-gate.ts';
@@ -194,7 +195,10 @@ export async function enrichEntity(
const derivation = await deriveTrust(engine, [{ table: 'pages', sourceId, slug: request.sourceSlug }], { channel: 'derive:enrichment' });
const ctx = opts?.ctx;
// #6262: a managed brain refuses direct writes, so each one goes through the coordinator under the caller's authority.
- const managed = ctx !== undefined && await managedPersistenceEnabled(engine);
+ const managedBrain = await managedPersistenceEnabled(engine);
+ const managed = ctx !== undefined && managedBrain;
+ // G6: the direct branch below writes the database first (and falls back to a raw page write), so a git-canonical source refuses here.
+ if (!managedBrain) await assertUnmanagedCanonicalWriter(engine, 'extract_entities', { sourceId });
const coordinated = async <T>(lockSlugs: string[], write: (tx: BrainEngine) => Promise<T>) => {
const { coordinatedDatabaseWrite } = await import('./persistence/database-write.ts');
return coordinatedDatabaseWrite(ctx!, 'extract_entities', slug, lockSlugs, write, derivation.trust);
diff --git a/test/strict-source-unmanaged-import.test.ts b/test/strict-source-unmanaged-import.test.ts
index 994a95976c..c7083a3510 100644
--- a/test/strict-source-unmanaged-import.test.ts
+++ b/test/strict-source-unmanaged-import.test.ts
@@ -16,6 +16,7 @@ import type { BrainEngine } from '../src/core/engine.ts';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { importCodeFile, importFromContent, importImageFile } from '../src/core/import-file.ts';
import { importStreamedMarkdownFile } from '../src/core/page-body/streamed-import.ts';
+import { enrichEntity } from '../src/core/enrichment-service.ts';
import type { PageBodyPolicy } from '../src/core/page-body/limits.ts';
import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
import { testBackends } from './helpers/test-backends.ts';
@@ -51,6 +52,10 @@ for (const backend of backends) test(`[${backend}] an unmanaged brain refuses a
expect(await refusal(importStreamedMarkdownFile(engine, join(import.meta.dir, 'no-such-dir', 'large.md'), 'notes/large.md', { noEmbed: true, sourceId: 'source-a' },
{ size: 1, mtimeMs: 0, birthtime: now, mtime: now }, {} as PageBodyPolicy)))
.toEqual('strict_requires_managed');
+ // extract_entities' unmanaged branch used to fall back to a direct page write when the import refused.
+ expect(await refusal(enrichEntity(engine, { entityName: 'Alice Example', entityType: 'person', context: 'met at the offsite', sourceSlug: 'notes/offsite' }, { sourceId: 'source-a' })))
+ .toEqual('strict_requires_managed');
+ expect(await pageCount(engine, 'source-a', 'people/alice-example')).toBe(0);
expect(await pageCount(engine, 'source-a', 'notes/alice-example')).toBe(0);
expect(await pageCount(engine, 'source-a', 'acme-example-ts')).toBe(0);
expect((await importFromContent(engine, 'notes/alice-example', page('Alice'), { noEmbed: true })).status).toBe('imported');