Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): only declared background producers take the pre-receipt owner check; default-origin admissions keep the physical-root fences (#6741)

garrytan49af1ac83371
diff --git a/src/core/persistence/admission-origin.ts b/src/core/persistence/admission-origin.ts
index 9b56399082..0f6af43241 100644
--- a/src/core/persistence/admission-origin.ts
+++ b/src/core/persistence/admission-origin.ts
@@ -50,6 +50,17 @@ export function resolveAdmissionOrigin(explicit?: AdmissionOrigin): AdmissionOri
return explicit ?? surrounding ?? 'background';
}
+/**
+ * #6741 C: whether this admission is declared background work: an explicit background origin, or a surrounding
+ * background run (withAdmissionOrigin). An admission with neither (a direct caller relying on the default) is charged
+ * as background but is not a declared producer run, so it keeps the publication's own fences (a physical-root change
+ * fails its receipt with the specific code, as sync dispatch and put_page always have).
+ */
+export function declaredBackground(explicit?: AdmissionOrigin): boolean {
+ const surrounding = scope.getStore()?.origin;
+ return surrounding === 'background' || (explicit ?? surrounding) === 'background';
+}
+
export function isAdmissionOrigin(value: unknown): value is AdmissionOrigin {
return value === 'interactive' || value === 'background';
}
diff --git a/src/core/persistence/journal.ts b/src/core/persistence/journal.ts
index 56225da639..05779ecf19 100644
--- a/src/core/persistence/journal.ts
+++ b/src/core/persistence/journal.ts
@@ -12,7 +12,7 @@ import { journalLimitKey, oneYearCapacity, readJournalLimits, readReceiptRetenti
import { outstandingCapacityDetail, retryWriteAdmission } from './admission-retry.ts';
import { PreadmitBrainChanged } from './preadmit-cache.ts';
import { pipelined } from '../page-state/transactions.ts';
-import { backgroundPaused, backgroundWorktreeInactive, readBackgroundPauseShare, resolveAdmissionOrigin, type AdmissionOrigin } from './admission-origin.ts';
+import { backgroundPaused, backgroundWorktreeInactive, declaredBackground, readBackgroundPauseShare, resolveAdmissionOrigin, type AdmissionOrigin } from './admission-origin.ts';
import { EFFECT_BYTES_SQL, RECEIPT_RESERVATION_FLOOR, receiptReservation, retainedReceiptBytes, TRUE_UP_CLAMP_SQL } from './receipt-bytes.ts';
import { writeHealth, type WriteHealthFacts } from './health.ts';
import { assertBackgroundOwner } from './owner-verification.ts';
@@ -245,8 +245,8 @@ async function prepareAdmission(engine: BrainEngine, input: WriteAdmission, over
if (!Number.isSafeInteger(terminalBytes) || terminalBytes < 1024) throw new TypeError('Invalid terminal receipt reservation.');
const stamp = writerStamp();
const origin = resolveAdmissionOrigin(input.origin);
- // #6741 C: a background producer is refused before any receipt when the source's owner fails its verification.
- if (origin === 'background' && input.worktreeId) await assertBackgroundOwner(engine, input.sourceId, ownerStatusFix);
+ // #6741 C: a declared background producer is refused before any receipt when the source's owner fails its verification.
+ if (input.worktreeId && declaredBackground(input.origin)) await assertBackgroundOwner(engine, input.sourceId, ownerStatusFix);
return { requestId, apply: async (tx: BrainEngine): Promise<WriteRequest> => {
await declarePersistenceProtocol(tx);
// The guards are sent together in lock order (worktree, source, writer, counters) and judged in
@@ -327,7 +327,7 @@ export async function admitWriteGroupInTransaction(tx: BrainEngine, inputs: Writ
const items = inputs.map(input => ({ input, requestId: requireUuid(input.requestId ?? randomUUID()), fingerprint: intentDigest(input),
bytes: jsonBytes(input.intent) + jsonBytes(input.authority), terminalBytes: Math.max(input.terminalReservation ?? 0, receiptReservation(input)) }));
assertMutationProtocol({ target_kind: 'page', protocol_version: 1 });
- if (first.worktreeId && resolveAdmissionOrigin(first.origin) === 'background') await assertBackgroundOwner(tx, first.sourceId, ownerStatusFix);
+ if (first.worktreeId && declaredBackground(first.origin)) await assertBackgroundOwner(tx, first.sourceId, ownerStatusFix);
const ids = items.map(item => item.requestId);
// #5984: the checks are independent reads and share locks, so they go out back to back in the order they always
// had (protocol, graduation, worktree, source, writer, counters); a failure is raised in that order.
diff --git a/src/core/persistence/owner-verification.ts b/src/core/persistence/owner-verification.ts
index ce3330a1ae..c8958d7b75 100644
--- a/src/core/persistence/owner-verification.ts
+++ b/src/core/persistence/owner-verification.ts
@@ -5,9 +5,11 @@
* still carries its marker and reservation (`assertPhysicalRoot`). A worktree whose state is still `active` can
* fail it, for example after `.gbrain-owner.json` is deleted, so a state-only check misses it.
*
- * A background producer verifies each source once per run (backgroundOwnerCheck in admission-origin.ts caches
- * the outcome for the surrounding background scope; outside a scope it runs per admission) and, when it fails, is
- * refused `owner_unavailable` with the owner-status fix before any receipt, so no request id is spent.
+ * A declared background producer (an explicit background origin or a withAdmissionOrigin('background') run; see
+ * declaredBackground) verifies each source once per run (backgroundOwnerCheck caches the outcome for the surrounding
+ * background scope; an explicit origin outside a scope verifies per admission) and, when it fails, is refused
+ * `owner_unavailable` with the owner-status fix before any receipt, so no request id is spent. An admission on the
+ * implicit default keeps the publication's own physical-root fences and their specific codes.
*/
import type { Action } from '../agent-output.ts';
import { OperationError } from '../ops/contract.ts';