Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(f1c): maintenance attribution never mints a host identity; mocks return row arrays
garrytan23fdce0f4467
diff --git a/docs/architecture/system-of-record.md b/docs/architecture/system-of-record.mdindex d16f1ce018..31cce8b398 100644--- a/docs/architecture/system-of-record.md+++ b/docs/architecture/system-of-record.md@@ -161,8 +161,11 @@ scopes keeps `NULL` attribution, which reads as "unrecorded".principal with `write_request_id` NULL: direct markdown, code and imageimports (every `importFromContent` caller without `prepare`), the`extract_facts` page reconcile, `extract-takes` and the- `gbrain repair stale-atoms --apply` retirement. The direct writers listed- under "unattributed" below still write without an actor.+ `gbrain repair stale-atoms --apply` retirement. The maintenance principal is+ the local CLI registration (`local_cli`), else this host's identity+ (`application`, `host:<id>`), else `host:unregistered`: attribution never+ creates an identity file. The direct writers listed under "unattributed"+ below still write without an actor.Agents: treat `NULL` attribution on an unmanaged brain as "written by a legacymaintenance path", not as evidence of tampering. To get attribution for adiff --git a/src/core/persistence/attribution.ts b/src/core/persistence/attribution.tsindex f61376e7bb..6b3ca90362 100644--- a/src/core/persistence/attribution.ts+++ b/src/core/persistence/attribution.ts@@ -1,4 +1,4 @@-import { currentVerifiedLocalWriter, localHostId, readLocalWriter } from './identity.ts';+import { currentVerifiedLocalWriter, existingLocalHostId, readLocalWriter } from './identity.ts';import type { Principal, SqlEngine, WriteRequest } from './model.ts';import type { BrainEngine } from '../engine.ts';import { withWriteAttribution } from './context.ts';@@ -27,7 +27,9 @@ export function principalAttribution(principal: Principal): WriteAttribution {* (server-side owner work), else this installation's local CLI registration,* else the host itself when the installation has no usable registration.* Attribution records the actor and never authorizes, so an unreadable- * registration falls back to the host instead of failing the write. The+ * registration falls back to the host instead of failing the write, and it+ * never mints an identity file: an installation without one (an ephemeral+ * demo brain, a first direct import) is recorded as `host:unregistered`. The* registration found is memoized per engine (an in-process rotation keeps* naming the registration this process started with): per-page maintenance* loops would otherwise repeat a file read and two queries for every page.@@ -43,7 +45,7 @@ export async function maintenanceAttribution(engine: SqlEngine): Promise<WriteAttry { return principalAttribution({ kind: 'local_cli', id: await registration }); }catch {installationRegistration.delete(engine);- return principalAttribution({ kind: 'application', id: `host:${localHostId()}` });+ return principalAttribution({ kind: 'application', id: `host:${existingLocalHostId() ?? 'unregistered'}` });}}const installationRegistration = new WeakMap<SqlEngine, Promise<string>>();diff --git a/test/embed.serial.test.ts b/test/embed.serial.test.tsindex a620cd9ea4..021c6c9fd8 100644--- a/test/embed.serial.test.ts+++ b/test/embed.serial.test.ts@@ -915,6 +915,8 @@ describe('#3374 — transient network retry branch', () => {if (prop === 'putPage') {return async (slug: string, page: any) => { pageStore.set(slug, page); };}+ // Like a real engine, executeRaw always resolves to a row array.+ if (prop === 'executeRaw') return () => Promise.resolve([]);return () => Promise.resolve(null);},});diff --git a/test/write-attribution.test.ts b/test/write-attribution.test.tsindex ff96a686ce..678c9ebc36 100644--- a/test/write-attribution.test.ts+++ b/test/write-attribution.test.ts@@ -14,7 +14,7 @@import { afterAll, beforeAll, describe, expect, test } from 'bun:test';import { randomUUID } from 'node:crypto';import { execFileSync } from 'node:child_process';-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { PGLiteEngine } from '../src/core/pglite-engine.ts';@@ -22,7 +22,7 @@ import type { BrainEngine } from '../src/core/engine.ts';import type { OperationContext } from '../src/core/ops/contract.ts';import { operationsByName } from '../src/core/operations.ts';import { mintLegacyToken } from '../src/core/token-mint.ts';-import { registerLocalWriter, readLocalWriter } from '../src/core/persistence/identity.ts';+import { localHostId, registerLocalWriter, readLocalWriter } from '../src/core/persistence/identity.ts';import { getWriteRequest } from '../src/core/persistence/journal.ts';import { submitRememberMutation, submitForgetMutation } from '../src/core/persistence/memory-mutations.ts';import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';@@ -415,15 +415,19 @@ describe('write attribution scopes', () => {});describe('maintenance attribution', () => {- test('names the local CLI registration, or the host when this installation has none', async () => {+ test('names the local CLI registration, else the host, and never mints an identity file', async () => {for (const engine of engines) {const brain = await managedBrain(engine);expect(await engine.transaction(tx => maintenanceAttribution(tx))).toEqual({ requestId: null, principal: brain.principals.local });const elsewhere = mkdtempSync(join(home, 'unregistered-'));- const fallback = await withEnv({ GBRAIN_HOME: elsewhere }, () => engine.transaction(tx => maintenanceAttribution(tx)));- expect(fallback.requestId).toBeNull();- expect(fallback.principal.kind).toBe('application');- expect(fallback.principal.id).toMatch(/^host:[0-9a-f-]{36}$/);+ const unregistered = await withEnv({ GBRAIN_HOME: elsewhere }, () => engine.transaction(tx => maintenanceAttribution(tx)));+ expect(unregistered).toEqual({ requestId: null, principal: { kind: 'application', id: 'host:unregistered' } });+ expect(readdirSync(elsewhere)).toEqual([]);+ const fallback = await withEnv({ GBRAIN_HOME: elsewhere }, async () => {+ const hostId = localHostId();+ return { hostId, attribution: await engine.transaction(tx => maintenanceAttribution(tx)) };+ });+ expect(fallback.attribution).toEqual({ requestId: null, principal: { kind: 'application', id: `host:${fallback.hostId}` } });}});});