Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a connector import still counts as a later write after its receipt is compacted

garrytan9a580f9b4d06
diff --git a/src/core/persistence/refresh-barrier.ts b/src/core/persistence/refresh-barrier.ts
index 382112bcde..234cea9355 100644
--- a/src/core/persistence/refresh-barrier.ts
+++ b/src/core/persistence/refresh-barrier.ts
@@ -60,13 +60,14 @@ const APPENDING_OPERATIONS = ['remember', 'add_timeline_entry'];
* consumer, which has no EARLIER_DEFERRED in its claim, claimed the later write first), so the deferred one, replayed now,
* never lands on top of it. Managed sync's own rows are the import the write waited for and do not count; neither does a
* write that only appends. A sync row is a `submit_job` with a managed_sync_ kind, or with an imported file once compaction
- * dropped its intent (#8765 review): a caller cannot make its own write look like one.
+ * dropped its intent (#8765 review): a caller cannot make its own write look like one. A connector import names
+ * `connector_database` in its outcome and stays a later write (overwritten-edits.ts MANAGED_IMPORT).
*/
async function laterWriteRefusal(engine: SqlEngine, row: WriteRequest): Promise<DeferredRefusal | null> {
if (APPENDING_OPERATIONS.includes(row.operation)) return null;
const [later] = await engine.executeRaw<{ request_id: string }>(`SELECT r.request_id::text AS request_id FROM persistence_requests r
WHERE r.source_id=$1 AND r.slug=$2 AND r.sequence>$3 AND r.state='committed' AND NOT (r.operation='submit_job'
- AND (${SYNC_KIND('r')} OR (r.intent IS NULL AND COALESCE(r.outcome->>'imported_file','')='true'))) ORDER BY r.sequence LIMIT 1`,
+ AND (${SYNC_KIND('r')} OR (r.intent IS NULL AND COALESCE(r.outcome->>'imported_file','')='true' AND NOT (r.outcome ? 'connector_database')))) ORDER BY r.sequence LIMIT 1`,
[row.source_id, row.slug, String(row.sequence)]);
if (!later) return null;
return { state: 'conflict', failure: { code: 'revision_conflict', message: `Request ${row.request_id} waited ${row.deferred_refresh_id ? `behind refresh ${row.deferred_refresh_id}` : 'for an import'}, and request ${later.request_id}, accepted after it, has already changed ${row.slug}, so nothing was applied and the later write was not overwritten. Read the page again and submit the change with a new request_id.` } };
diff --git a/test/refresh-barrier-gst1623.test.ts b/test/refresh-barrier-gst1623.test.ts
index 39191a5476..e0cdbbd341 100644
--- a/test/refresh-barrier-gst1623.test.ts
+++ b/test/refresh-barrier-gst1623.test.ts
@@ -256,6 +256,26 @@ test('#8765 outside voice: a later write counts unless managed sync wrote it (su
expect((await f.engine.readPageSnapshot('two', { sourceId: f.alpha }))?.revision).toBe(before);
}), 180_000);
+test('#8765 review: a connector import stays a later write after compaction drops its intent, as overwritten-edits counts it', () => each(async f => {
+ let deferred!: WriteRequest;
+ const refresh = await duringRefresh(f, async () => {
+ const out = await local(f, 'put_page', { slug: 'one', content: page('Alpha one', 'Deferred, accepted first.') });
+ deferred = await requestRow(f, out.receipt.request_id);
+ expect(deferred.state).toBe('deferred');
+ await disposePersistenceConsumer(f.engine);
+ });
+ expect((await refresh).status).toBe('completed');
+ const later = await requestRow(f, (await local(f, 'put_page', { slug: 'one', content: page('Alpha one', 'Accepted second.') })).receipt.request_id);
+ expect(later.state).toBe('committed');
+ const held = { ...deferred, deferred_refresh_id: null, deferred_import_from: '' } as WriteRequest;
+ // connector-sync.ts records every connector import with imported_file and connector_database (false on a bound source).
+ for (const connectorDatabase of [true, false]) {
+ await f.engine.executeRaw(`UPDATE persistence_requests SET operation='submit_job', intent=NULL, compacted=true,
+ outcome=COALESCE(outcome,'{}'::jsonb)||jsonb_build_object('imported_file',true,'connector_database',$2::boolean) WHERE id=$1::uuid`, [later.id, connectorDatabase]);
+ expect(await deferredReplayRefusal(f.engine, held, [])).toMatchObject({ state: 'conflict', failure: { code: 'revision_conflict' } });
+ }
+}), 180_000);
+
test('item 16: a write deferred behind a refresh is held for an import without its released refresh, unless the refresh changed its file', () => each(async f => {
f.push('alpha/three.md', page('Alpha three', 'Before the refresh.'));
expect((await refreshWorktree(f.engine, f.alpha)).status).toBe('completed');