Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: hold a later write beside a held import write; lock the source before expiry
garrytan03b8912102b7
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.tsindex 64122a7171..97bc4246fe 100644--- a/src/core/persistence/page-prepare.ts+++ b/src/core/persistence/page-prepare.ts@@ -407,6 +407,12 @@ async function compareFileTarget(engine: BrainEngine, row: Pick<WriteRequest, 'scontent: string | Uint8Array | StreamedFileContent | null, options: Parameters<typeof prepareFileTarget>[5] & object,target: { root: string; path: string; mode: Awaited<ReturnType<typeof scannerSlugRootMode>> | undefined; capturedPath: string | null | undefined; before: Buffer | FileBytes | null }): Promise<PreparedMutation['file']> {const { root, path, mode, capturedPath, before } = target;+ // GST-1582: an earlier write to this page held for an import that has not landed goes first, so this one is held behind the+ // same import (holdForImport) and both are released, in admission order, at its checkpoint; it never commits before the held one.+ const [heldFirst] = await engine.executeRaw<{ deferred_import_from: string }>(`SELECT r.deferred_import_from FROM persistence_requests r+ JOIN sources s ON s.id=r.source_id WHERE r.source_id=$1 AND r.slug=$2 AND r.state='deferred' AND r.deferred_import_from IS NOT NULL+ AND COALESCE(s.last_commit,'')=r.deferred_import_from ORDER BY r.sequence LIMIT 1`, [row.source_id, row.slug]);+ if (heldFirst) throw pendingImportRefusal(row, options.remote === true, heldFirst.deferred_import_from);const beforeHash = () => before instanceof FileBytes ? before.hash() : sha256(before!);if (options.deleting === true && options.sharedOrigin && snapshot && before) {const target: FileClaimTarget = { sourceId: row.source_id, slug: row.slug, pageId: snapshot.page.id, root, path, canonicalPath: realpathSync(path),diff --git a/src/core/persistence/refresh-barrier.ts b/src/core/persistence/refresh-barrier.tsindex bae287eee2..78006eab87 100644--- a/src/core/persistence/refresh-barrier.ts+++ b/src/core/persistence/refresh-barrier.ts@@ -82,7 +82,8 @@ export const REFRESH_BARRIER_MAX_WAIT_MS = 15 * 60_000;* the release that would make the row claimable never both win.* GST-1582: one held for an import that has not landed past the limit ends `pending_import_expired` the same way; its* claim and its expiry both move the row, so the expiry checks the row is still unclaimed and its import still pending- * under the request lock completeWrite takes.+ * with the sources row locked FOR SHARE (after the protocol's brain lock, the order a fence takes them in), so an import+ * that is landing makes the check wait and then see it landed.*/export async function expireDeferredWrites(engine: BrainEngine, hostId: string, signal?: AbortSignal): Promise<WriteRequest[]> {const [config] = await engine.executeRaw<{ value: string }>('SELECT value FROM config WHERE key=$1', [REFRESH_BARRIER_MAX_WAIT_KEY], { signal });@@ -110,8 +111,9 @@ export async function expireDeferredWrites(engine: BrainEngine, hostId: string,AND r.created_at<now()-($2::double precision*interval '1 millisecond') ORDER BY r.sequence LIMIT 100`, [hostId, maxWaitMs], { signal });for (const row of imports) {const done = await engine.transaction(async tx => {+ await declarePersistenceProtocol(tx);const [held] = await tx.executeRaw(`SELECT 1 FROM persistence_requests r JOIN sources s ON s.id=r.source_id- WHERE r.id=$1::uuid AND r.state='deferred' AND COALESCE(s.last_commit,'')=r.deferred_import_from`, [row.id]);+ WHERE r.id=$1::uuid AND r.state='deferred' AND COALESCE(s.last_commit,'')=r.deferred_import_from FOR SHARE OF s`, [row.id]);if (!held) return null;return completeWrite(tx, row, 'failed', {}, { code: 'pending_import_expired',message: `Request ${row.request_id} waited more than ${Math.round(maxWaitMs / 1000)} s for managed sync to import a committed change to the file of ${row.slug}, and the import has not landed, so nothing was applied. Check the source with gbrain sync status --source ${row.source_id} --json, then submit the write again with a new request_id.` });