Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(serve-sync): publish done only after the run settles markers and schedules deferred embeds (#8354)

garrytan89a75252ae66
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2602c11358..87ba820571 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,15 @@ credits are retained; no result has been reassigned to another provider. Origina
identifiers and attribution are available in the pre-removal Git revision
`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).
+## [Unreleased]
+
+**A delegated sync no longer reports `done` while it is still finishing.** The serve's sync runner published `done` before it re-checked the wanted markers and scheduled the deferred embeds. A client polling in that window saw a finished job with no embed backlog queued yet, and a second `sync_start` could begin while the first run was still settling its markers. Since #7941 the marker re-check also reads the checkout HEAD through git, which widened the window enough to fail `test/serve-sync-runner.serial.test.ts` intermittently on master (#8354). Both lanes (the shared-secret lane and the verified lane) now set `done` only after those steps.
+
+### Itemized changes
+
+- `src/core/serve-sync-runner.ts`: `job.state = 'done'` moves after `settleRunMarkers` and `scheduleDeferredSyncEmbeds` in both lanes (#8354).
+- `test/serve-sync-runner.serial.test.ts`: a probe slows the post-run marker read and checks that `done` is only visible once the markers have settled and the deferred embeds are pending. It fails on master and passes with the fix.
+
## [0.60.531.0] - 2026-10-31
**Five severe fixes: the secret scan now redacts keys that follow a JSON or percent escape, a connector page that turns private leaves Git HEAD, deleted pages no longer stay searchable under a scoped writer role, a stale freshness marker can no longer starve the job queue, and a slow embedder no longer loses `remember` writes (#7978, #7971, #7637, #7941, #8135).**
diff --git a/src/core/serve-sync-runner.ts b/src/core/serve-sync-runner.ts
index ac8cda1fc2..81da6e0fee 100644
--- a/src/core/serve-sync-runner.ts
+++ b/src/core/serve-sync-runner.ts
@@ -319,7 +319,6 @@ function startDelegatedSyncScoped(
},
});
job.result = toWireSyncResult(r);
- job.state = 'done';
log(
`done job=${job.id} status=${r.status} added=${r.added} modified=${r.modified} ` +
`deleted=${r.deleted} in=${Math.round((Date.now() - job.startedAt) / 1000)}s`,
@@ -330,6 +329,7 @@ function startDelegatedSyncScoped(
if (!options.dryRun && !options.noEmbed && r.added + r.modified > 0) {
scheduleDeferredSyncEmbeds(engine, sourceId);
}
+ job.state = 'done';
} catch (e) {
fail(e);
} finally {
@@ -431,7 +431,6 @@ async function startVerifiedDelegatedSync(
const id = sourceId ?? 'default';
job.next = drainNext(r, syncResumeCommand(managedSyncResumeArgs({ sourceId: id, processingOptions: options })), id);
}
- job.state = 'done';
log(
`done job=${job.id} status=${r.status} added=${r.added} modified=${r.modified} ` +
`deleted=${r.deleted}${r.drain ? ` drain=${r.drain.outcome}` : ''}${stopFields} in=${Math.round((Date.now() - job.startedAt) / 1000)}s`,
@@ -442,6 +441,7 @@ async function startVerifiedDelegatedSync(
if (!managed && !options.dryRun && !options.noEmbed && r.added + r.modified > 0) {
scheduleDeferredSyncEmbeds(engine, sourceId);
}
+ job.state = 'done';
}));
} catch (e) {
hooks.fail(e);
diff --git a/test/serve-sync-runner.serial.test.ts b/test/serve-sync-runner.serial.test.ts
index 97306d263f..bc9ab57693 100644
--- a/test/serve-sync-runner.serial.test.ts
+++ b/test/serve-sync-runner.serial.test.ts
@@ -159,6 +159,34 @@ describe('serve-sync-runner delegated jobs', () => {
expect(await engine.getPage('topics/alpha')).not.toBeNull();
}, 60_000);
+ test('#8354: done is published only after the run settles its markers and schedules the deferred embeds', async () => {
+ // The post-run marker re-check is slowed so a status poll lands inside it: done must not be visible yet.
+ let markerReads = 0;
+ const slowSettle = new Proxy(engine, { get(target, key) {
+ const value = Reflect.get(target, key, target);
+ if (key === 'executeRaw') return async (sql: string, params?: unknown[]) => {
+ if (sql.includes('FROM sync_wanted_markers m JOIN sources') && ++markerReads > 1) await new Promise((r) => setTimeout(r, 400));
+ return target.executeRaw(sql, params);
+ };
+ return typeof value === 'function' ? value.bind(target) : value;
+ } });
+ const start = startDelegatedSync(slowSettle, { sourceId: 'default', noPull: true, timeoutSeconds: 120 }, 'token-8354');
+ expect(start.ok).toBe(true);
+ const deadline = Date.now() + 30_000;
+ for (;;) {
+ const s = getDelegatedSyncStatus(start.jobId!);
+ if (s.ok && (s.state === 'done' || s.state === 'error')) {
+ expect(s.state).toBe('done');
+ expect(markerReads).toBeGreaterThan(1);
+ expect(__deferredEmbedsPendingForTests()).toBe(true);
+ expect(isDelegatedSyncRunning()).toBe(false);
+ break;
+ }
+ if (Date.now() > deadline) throw new Error('job did not settle');
+ await new Promise((r) => setTimeout(r, 10));
+ }
+ }, 60_000);
+
test('single-flight: a different token while running answers busy with the jobId', async () => {
const first = startDelegatedSync(engine, { noPull: true, timeoutSeconds: 60, sourceId: 'default' }, 'token-a');
expect(first.ok).toBe(true);