Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(f1a): maintenance attribution never fails a write and is memoized per engine

garrytane31618ac382a
diff --git a/src/core/persistence/attribution.ts b/src/core/persistence/attribution.ts
index 3952508b65..2d8b1f83c0 100644
--- a/src/core/persistence/attribution.ts
+++ b/src/core/persistence/attribution.ts
@@ -1,4 +1,3 @@
-import { OperationError } from '../ops/contract.ts';
import { currentVerifiedLocalWriter, localHostId, readLocalWriter } from './identity.ts';
import type { Principal, SqlEngine, WriteRequest } from './model.ts';
@@ -25,14 +24,24 @@ export function principalAttribution(principal: Principal): WriteAttribution {
* The executing maintenance principal: the verified local writer of this call
* (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 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.
*/
export async function maintenanceAttribution(engine: SqlEngine): Promise<WriteAttribution> {
const verified = currentVerifiedLocalWriter();
if (verified) return principalAttribution(verified.principal);
- try {
- return principalAttribution({ kind: 'local_cli', id: (await readLocalWriter(engine, 'cli')).id });
- } catch (error) {
- if (!(error instanceof OperationError) || !['writer_registration_required', 'permission_denied'].includes(error.code)) throw error;
+ let registration = installationRegistration.get(engine);
+ if (!registration) {
+ registration = readLocalWriter(engine, 'cli').then(local => local.id);
+ installationRegistration.set(engine, registration);
+ }
+ try { return principalAttribution({ kind: 'local_cli', id: await registration }); }
+ catch {
+ installationRegistration.delete(engine);
return principalAttribution({ kind: 'application', id: `host:${localHostId()}` });
}
}
+const installationRegistration = new WeakMap<SqlEngine, Promise<string>>();
diff --git a/test/write-attribution.test.ts b/test/write-attribution.test.ts
index f1e15c3b6e..ff96a686ce 100644
--- a/test/write-attribution.test.ts
+++ b/test/write-attribution.test.ts
@@ -35,7 +35,8 @@ import { LEGACY_EMBEDDING_CONFIG } from './helpers/legacy-embedding-config.ts';
import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
import { TEST_WRITE_ATTRIBUTION } from './helpers/write-attribution.ts';
import { withCoordinatedWrite } from '../src/core/persistence/context.ts';
-import { withWriteAttribution, type WriteAttribution } from '../src/core/persistence/attribution.ts';
+import { maintenanceAttribution, withWriteAttribution, type WriteAttribution } from '../src/core/persistence/attribution.ts';
+import { withEnv } from './helpers/with-env.ts';
import { claimWorktree } from '../src/core/persistence/ownership.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
import { runManagedStaleExtraction } from '../src/core/persistence/links-maintenance.ts';
@@ -413,6 +414,20 @@ describe('write attribution scopes', () => {
}, 120_000);
});
+describe('maintenance attribution', () => {
+ test('names the local CLI registration, or the host when this installation has none', 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}$/);
+ }
+ });
+});
+
describe('write attribution column classification', () => {
test('every column of facts, takes and timeline_entries is content, physical projection or attribution', async () => {
for (const engine of engines) {