Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Merge origin/master (#6054 global brain-first lock order); core writes rely on the protocol declaration's brain lock (lockCoreBrain removed), lockCoreSources stays

garrytanbff4df96670c
diff --git a/docs/architecture/key-files/core-memory.md b/docs/architecture/key-files/core-memory.md
index 52ade3c446..267b03e67c 100644
--- a/docs/architecture/key-files/core-memory.md
+++ b/docs/architecture/key-files/core-memory.md
@@ -19,16 +19,14 @@ notice). User guide: [core memory](../../guides/core-memory.md).
the publish transaction), the remote-edit policy and `core_edit_notices`
rows, and `lockCoreSources`.
- Lock order for a core-touching publish (`coordinator.ts`): worktree native
- lock, then `lockCoreBrain` (`persistence_brain` FOR SHARE, the first lock
- effects take in `guardEffectSource`), then `guardOwnership`
- (`persistence_worktrees` FOR SHARE), then `lockCoreSources` (`sources` rows
- FOR UPDATE, `ORDER BY id`, own source plus `default`) in the
- source-exclusive slot, then `authorizeStoredRequest` (`sources` FOR SHARE),
- then `lockCounters`. Worktree claims and topology changes hold the brain row
- FOR UPDATE before worktrees and sources, and every publication share-locks
- it later through the request and effect triggers, so taking it first keeps
- the orders agreeing. `put_pages` batch groups skip the per-request source
- lock, so `groupable()` excludes core-locked writes.
+ lock, then the protocol declaration takes the brain row first
+ (`declareDurablePersistence` in `protocol.ts`, `persistence_brain` FOR
+ SHARE), then `guardOwnership` (`persistence_worktrees` FOR SHARE), then
+ `lockCoreSources` (`sources` rows FOR UPDATE, `ORDER BY id`, own source plus
+ `default`) in the source-exclusive slot, then `authorizeStoredRequest`
+ (`sources` FOR SHARE), then `lockCounters`: the global order `protocol.ts`
+ documents. `put_pages` batch groups skip the per-request source lock, so
+ `groupable()` excludes core-locked writes.
`test/e2e/core-memory-locks-postgres.test.ts` races core writes against
topology changes and a claim-shaped transaction on Postgres.
- `scripts/check-core-guard-coverage.mjs` (`bun run check:core-guard-coverage`,
diff --git a/src/core/persistence/coordinator.ts b/src/core/persistence/coordinator.ts
index 0584dd6bdf..9c8890c8aa 100644
--- a/src/core/persistence/coordinator.ts
+++ b/src/core/persistence/coordinator.ts
@@ -17,7 +17,7 @@ import { clearResolvedRecovery, completeWrite, getWriteRequestById, lockCounters
import { isTerminal, principalKey, requestPrincipal, recoveryFiles, type FileRecoveryRecord, type RecoveryRecord, type WriteRequest } from './model.ts';
import type { NativeLockHandle } from './native-lock.ts';
import { withCoordinatedWrite } from './context.ts';
-import { lockCoreBrain, lockCoreSources } from './core-guard.ts';
+import { lockCoreSources } from './core-guard.ts';
import { requestAttribution } from './attribution.ts';
import { withFilesystemPublication } from './filesystem-guard.ts';
import { mayReprepare } from './semantic.ts';
@@ -274,7 +274,6 @@ export async function publishMutation(engine: BrainEngine, row: WriteRequest, pr
}
const done = await engine.transaction(async tx => {
await declareDurablePersistence(tx);
- if (prepared.exclusiveSources?.length) await lockCoreBrain(tx);
const liveBinding = await guardOwnership(tx, row, hostId);
if (prepared.sourceExclusive && !prepared.exclusiveSources?.length) await tx.executeRaw('SELECT id FROM sources WHERE id=$1 FOR UPDATE', [row.source_id]);
if (prepared.exclusiveSources?.length) await lockCoreSources(tx, prepared.sourceExclusive ? [row.source_id, ...prepared.exclusiveSources] : prepared.exclusiveSources);
diff --git a/src/core/persistence/core-guard.ts b/src/core/persistence/core-guard.ts
index 9d383cd10d..3a445caf72 100644
--- a/src/core/persistence/core-guard.ts
+++ b/src/core/persistence/core-guard.ts
@@ -13,15 +13,12 @@
* after `lockCoreSources` serialized every core writer, so concurrent core
* writes can never pass the limit together.
*
- * Lock order: worktree native lock, then `lockCoreBrain` (the
- * `persistence_brain` row FOR SHARE, the same first lock effects take in
- * `guardEffectSource`), then the `persistence_worktrees` FOR SHARE ownership
- * check, then `lockCoreSources` (source rows FOR UPDATE in id order) before
- * `authorizeStoredRequest` (source FOR SHARE), then `lockCounters`. The brain
- * row comes first because every publication share-locks it later through the
- * request and effect protocol triggers, while worktree claims and topology
- * changes hold it FOR UPDATE before they lock sources or worktrees: taking it
- * after a source row would invert that order. So every path takes brain row →
+ * Lock order: worktree native lock, then the protocol declaration takes the
+ * brain row first (protocol.ts: `declareDurablePersistence`, the first
+ * statement of the publish transaction), then the `persistence_worktrees`
+ * FOR SHARE ownership check, then `lockCoreSources` (source rows FOR UPDATE
+ * in id order) before `authorizeStoredRequest` (source FOR SHARE), then
+ * `lockCounters`: the global order protocol.ts documents, brain row →
* worktree row → source rows (id order) → counters.
*/
import type { BrainEngine } from '../engine.ts';
@@ -45,11 +42,6 @@ export function coreLockSources(sourceId: string): string[] {
* FOR UPDATE in id order inside the publish transaction (transaction-scoped,
* PgBouncer-safe). See the module comment for the global order.
*/
-/** First lock of a core write's publish transaction; see the module comment for the global order. */
-export async function lockCoreBrain(tx: Pick<BrainEngine, 'executeRaw'>): Promise<void> {
- await tx.executeRaw('SELECT singleton FROM persistence_brain WHERE singleton=1 FOR SHARE');
-}
-
export async function lockCoreSources(tx: Pick<BrainEngine, 'executeRaw'>, sourceIds: readonly string[]): Promise<void> {
const ids = [...new Set(sourceIds)].sort();
if (!ids.length) return;
diff --git a/test/e2e/core-memory-locks-postgres.test.ts b/test/e2e/core-memory-locks-postgres.test.ts
index f697ce8cb7..5c78638a65 100644
--- a/test/e2e/core-memory-locks-postgres.test.ts
+++ b/test/e2e/core-memory-locks-postgres.test.ts
@@ -1,6 +1,6 @@
/**
* Always-loaded core writes on Postgres: the core lock order (core-guard.ts:
- * `persistence_brain` FOR SHARE first, then the worktree ownership check,
+ * the protocol declaration's `persistence_brain` FOR SHARE first, then the worktree ownership check,
* then `sources` rows FOR UPDATE in id order, own source plus `default`,
* then the authority FOR SHARE and the counter locks) never deadlocks with
* core writes in another source, non-core writes in `default`, a concurrent