Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): atom session waits out the previous batch's git effects (#7461)
garrytan5334b58c8252
diff --git a/src/core/cycle/extract-atoms.ts b/src/core/cycle/extract-atoms.tsindex 23da89ad5c..67a922751b 100644--- a/src/core/cycle/extract-atoms.ts+++ b/src/core/cycle/extract-atoms.ts@@ -705,7 +705,7 @@ export async function runPhaseExtractAtoms(): Promise<PhaseResult> {const sourceId = opts.sourceId ?? 'default';const chat = opts._chat ?? gatewayChat;- const managed = await managedAtomSession(engine, sourceId, opts._managedRetry, opts.attempt?.writeWait);+ const managed = await managedAtomSession(engine, sourceId, opts._managedRetry, opts.attempt?.writeWait, opts.signal);// v0.41.2.1: loadConfigWithEngine() so the dream.* DB-plane merge from Phase 1 reaches this phase.let transcriptDirs: { session_corpus_dir?: string; meeting_transcripts_dir?: string } | undefined;try {diff --git a/src/core/persistence/atom-maintenance.ts b/src/core/persistence/atom-maintenance.tsindex 4e356b69df..cf3d33e725 100644--- a/src/core/persistence/atom-maintenance.ts+++ b/src/core/persistence/atom-maintenance.ts@@ -11,7 +11,7 @@ import { authorizeStoredRequest, authorizeWrite, submissionAuthority } from './aimport { currentVerifiedLocalWriter, localHostId, registerLocalWriter } from './identity.ts';import { admitWriteInTransaction, getWriteRequest, receiptFor } from './journal.ts';import { digest, requireUuid, sha256 } from './digest.ts';-import { getWorktreeBinding, managedPersistenceEnabled, probeWorktreeWriter, type WorktreeBinding } from './ownership.ts';+import { getWorktreeBinding, MANAGED_WRITER_PROBE_WAIT_MS, managedPersistenceEnabled, probeWorktreeWriter, type WorktreeBinding } from './ownership.ts';import { isConnectorSourceKind } from './connector-identity.ts';import { preparePageMutation } from './page-prepare.ts';import { assertPersistenceAccepting, waitForWrite, writeResponse } from './service.ts';@@ -120,7 +120,7 @@ export async function atomDrainOwnerRefusal(engine: BrainEngine, sourceId: strin}export async function managedAtomSession(engine: BrainEngine, sourceId: string, retry?: { requestId: string; retryId: string },- wait: MaintenanceWriteWait = new MaintenanceWriteWait()): Promise<ManagedAtomSession | null> {+ wait: MaintenanceWriteWait = new MaintenanceWriteWait(), signal?: AbortSignal): Promise<ManagedAtomSession | null> {if (!(await managedPersistenceEnabled(engine))) return null;assertPersistenceAccepting(engine);const caller = currentSubmissionAuthority();@@ -150,7 +150,7 @@ export async function managedAtomSession(engine: BrainEngine, sourceId: string,`No extraction was started for source ${sourceId}. Check its canonical owner: run atom extraction on the host that owns the checkout, or have the user claim the checkout on this host with gbrain sources writer claim.`,{ fix: ownerStatusFix(sourceId) });if (writeThrough && binding) {- if (!await probeWorktreeWriter(binding, engine)) throw opError('writer_lock_unavailable', 'The canonical atom writer is busy; no extraction was started.',+ if (!await probeWorktreeWriter(binding, engine, wait.probeMs(MANAGED_WRITER_PROBE_WAIT_MS), signal)) throw opError('writer_lock_unavailable', 'The canonical atom writer is busy; no extraction was started.',`Another write holds source ${sourceId}'s canonical writer. Nothing was started, so run the atom drain again once the owner status shows it idle.`,{ fix: ownerStatusFix(sourceId) });}diff --git a/src/core/persistence/atom-retry.ts b/src/core/persistence/atom-retry.tsindex f1fb9b3018..9dcc23d7a1 100644--- a/src/core/persistence/atom-retry.ts+++ b/src/core/persistence/atom-retry.ts@@ -55,7 +55,7 @@ export async function retryManagedAtomBatch(engine: BrainEngine, sourceId: strinstop = anyAbortSignal(opts.signal ? [opts.signal, lockSignal] : [lockSignal]);check();const wait = new MaintenanceWriteWait(opts.deadlineAtMs ?? null);- const session = await managedAtomSession(engine, sourceId, { requestId, retryId }, wait);+ const session = await managedAtomSession(engine, sourceId, { requestId, retryId }, wait, stop.signal);check();if (!session?.retry) throw opError('invalid_params', 'Explicit atom retry requires a managed source.',`Source ${sourceId} is not under managed persistence, so retained atom batch ${requestId} cannot be retried there; nothing was started. Run the ordinary atom drain for the source instead (it calls the configured LLM, so confirm the spend).`,diff --git a/src/core/persistence/maintenance-wait.ts b/src/core/persistence/maintenance-wait.tsindex 4997572084..9d7a76ce07 100644--- a/src/core/persistence/maintenance-wait.ts+++ b/src/core/persistence/maintenance-wait.ts@@ -37,7 +37,18 @@ export class MaintenanceWriteWait {/** The wait for the job's next publish. */ms(): number {- if (this.pending) return 0;+ return this.pending ? 0 : this.remaining();+ }++ /**+ * #7461: how long a session's writer-lock probe waits before the job's next publish: the publish wait, but never+ * less than `floor`, so a pending publish (wait 0) still gets the probe. Not a publish wait itself.+ */+ probeMs(floor: number): number {+ return Math.max(floor, this.pending ? 0 : this.remaining());+ }++ private remaining(): number {const base = testWaitMs ?? MAINTENANCE_WRITE_WAIT_MS;return this.deadlineAtMs == null ? base : Math.max(0, Math.min(base, this.deadlineAtMs - this.now()));}diff --git a/src/core/persistence/ownership.ts b/src/core/persistence/ownership.tsindex bf2461f832..cd4807f67a 100644--- a/src/core/persistence/ownership.ts+++ b/src/core/persistence/ownership.ts@@ -256,9 +256,14 @@ async function lockWorktree(binding: WorktreeBinding, waitMs: number, signal: Ab*/export const MANAGED_WRITER_PROBE_WAIT_MS = 1000;-/** Acquire-and-release probe of the worktree lock within MANAGED_WRITER_PROBE_WAIT_MS; false when it stayed busy. */-export async function probeWorktreeWriter(binding: WorktreeBinding, engine?: BrainEngine): Promise<boolean> {- const lock = await acquireWorktree(binding, MANAGED_WRITER_PROBE_WAIT_MS, undefined, engine);+/**+ * Acquire-and-release probe of the worktree lock within `waitMs` (MANAGED_WRITER_PROBE_WAIT_MS by default);+ * false when it stayed busy. #7461: atom maintenance passes its longer publish wait, since the previous+ * batch's Git effects can hold the lock past the default.+ */+export async function probeWorktreeWriter(binding: WorktreeBinding, engine?: BrainEngine,+ waitMs = MANAGED_WRITER_PROBE_WAIT_MS, signal?: AbortSignal): Promise<boolean> {+ const lock = await acquireWorktree(binding, waitMs, signal, engine);if (!lock) return false;await lock.release();return true;diff --git a/test/managed-writer-probe.test.ts b/test/managed-writer-probe.test.tsindex dce45de7f1..911039dde4 100644--- a/test/managed-writer-probe.test.ts+++ b/test/managed-writer-probe.test.ts@@ -5,7 +5,11 @@* write, after that receipt already reads committed. The facts preflight, the* atom-maintenance session and the connector-sync preflight all use it. A* holder that releases within the bound is waited out; one held past it is- * still busy (the atom session refuses with writer_lock_unavailable).+ * still busy. #7461: the atom session waits the longer of that bound and its+ * maintenance publish wait, because the previous batch's Git effects can hold+ * the lock past one second; past its deadline it refuses with+ * writer_lock_unavailable, and a job whose publish is already pending (wait+ * 0) still gets the one-second probe.** Synthetic data only. PGLite.*/@@ -16,6 +20,8 @@ import { tmpdir } from 'node:os';import { join } from 'node:path';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { managedAtomSession } from '../src/core/persistence/atom-maintenance.ts';+import { MaintenanceWriteWait } from '../src/core/persistence/maintenance-wait.ts';+import type { WriteRequest } from '../src/core/persistence/model.ts';import { acquireWorktree, claimWorktree, getWorktreeBinding, MANAGED_WRITER_PROBE_WAIT_MS, probeWorktreeWriter, type WorktreeBinding } from '../src/core/persistence/ownership.ts';import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';import { withEnv } from './helpers/with-env.ts';@@ -82,10 +88,51 @@ describe('managedAtomSession preflight', () => {await released;}), 30_000);- test('a holder kept past the bound still refuses with writer_lock_unavailable', () => run(async () => {+ test('a holder that outlasts a job near its deadline refuses with writer_lock_unavailable after the probe', () => run(async () => {const binding = await managedSource();- const { released } = await hold(binding, MANAGED_WRITER_PROBE_WAIT_MS + 1500);- await expect(managedAtomSession(engine, sourceId)).rejects.toMatchObject({ code: 'writer_lock_unavailable' });+ const nearDeadline = new MaintenanceWriteWait(Date.now() + 200);+ const busy = await hold(binding, MANAGED_WRITER_PROBE_WAIT_MS + 2000);+ const started = performance.now();+ await expect(managedAtomSession(engine, sourceId, undefined, nearDeadline)).rejects.toMatchObject({ code: 'writer_lock_unavailable' });+ const waited = performance.now() - started;+ expect(waited).toBeGreaterThanOrEqual(MANAGED_WRITER_PROBE_WAIT_MS - 50);+ expect(waited).toBeLessThan(MANAGED_WRITER_PROBE_WAIT_MS + 1500);+ await busy.released;+ }), 30_000);++ test('a holder that releases after the one-second probe but within the publish wait is waited out', () => run(async () => {+ const binding = await managedSource();+ const busy = await hold(binding, MANAGED_WRITER_PROBE_WAIT_MS + 800);+ const started = performance.now();+ const session = await managedAtomSession(engine, sourceId);+ expect(session).toMatchObject({ sourceId });+ expect(performance.now() - started).toBeGreaterThanOrEqual(MANAGED_WRITER_PROBE_WAIT_MS + 700);+ await busy.released;+ }), 30_000);++ test('a job whose publish is pending (wait 0) still waits the one-second probe', () => run(async () => {+ const binding = await managedSource();+ const wait = new MaintenanceWriteWait();+ wait.observe({ state: 'queued' } as WriteRequest);+ expect(wait.ms()).toBe(0);+ const { released } = await hold(binding, 400);+ expect(await managedAtomSession(engine, sourceId, undefined, wait)).not.toBeNull();+ await released;+ const busy = await hold(binding, MANAGED_WRITER_PROBE_WAIT_MS + 1500);+ const started = performance.now();+ await expect(managedAtomSession(engine, sourceId, undefined, wait)).rejects.toMatchObject({ code: 'writer_lock_unavailable' });+ expect(performance.now() - started).toBeGreaterThanOrEqual(MANAGED_WRITER_PROBE_WAIT_MS - 50);+ await busy.released;+ }), 30_000);++ test('an abort ends the wait before the publish budget', () => run(async () => {+ const binding = await managedSource();+ const { released } = await hold(binding, 4000);+ const controller = new AbortController();+ setTimeout(() => controller.abort(new Error('stopped')), 300);+ const started = performance.now();+ await expect(managedAtomSession(engine, sourceId, undefined, new MaintenanceWriteWait(), controller.signal)).rejects.toBeDefined();+ expect(performance.now() - started).toBeLessThan(MANAGED_WRITER_PROBE_WAIT_MS);await released;}), 30_000);});