Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): the in-pass stall line counts claimed head reads as progress

garrytand373190590a0
diff --git a/src/core/persistence/sync-drain.ts b/src/core/persistence/sync-drain.ts
index 976f547fe1..76c15de780 100644
--- a/src/core/persistence/sync-drain.ts
+++ b/src/core/persistence/sync-drain.ts
@@ -370,20 +370,21 @@ function stallText(index: number, total: number | null, stalledMs: number, claim
+ `${seen.consumer ? ` · consumer=${seen.consumer}` : ''}${seen.blocked_by ? ` · blocked_by=${headBlockerText(seen.blocked_by)}` : ''}`;
}
-/** Re-enter `pass` until the managed cursor is done, the caller stops it, or it is blocked. */
/**
* The in-pass line's age of an unclaimed head. A head that moved since the last read is the writer making progress (it
* commits requests queued ahead of this pass, a dead owner's in-flight writes among them), so the age counts from that
- * read, never from before `floor` (the drain's own last commit).
+ * read, never from before `floor` (the drain's own last commit). Every head read feeds it, a claimed head's included,
+ * whose line keeps the claim's own step age when it has one.
*/
-function inPassHeadAge(): (requestId: string | null | undefined, at: number, floor: number) => number {
+function inPassHeadAge(): (requestId: string | null | undefined, at: number, floor: number, claimAgeMs?: number | null) => number {
let seen: { id: string | null | undefined; at: number; since: number } | null = null;
- return (id, at, floor) => {
+ return (id, at, floor, claimAgeMs) => {
seen = { id, at, since: seen && seen.id !== id ? seen.at : seen?.since ?? floor };
- return at - Math.max(floor, seen.since);
+ return claimAgeMs ?? at - Math.max(floor, seen.since);
};
}
+/** Re-enter `pass` until the managed cursor is done, the caller stops it, or it is blocked. */
export async function runDrain(input: DrainInput): Promise<SyncResult> {
const clock = input.clock ?? Date.now;
const startedAt = clock(), headAge = inPassHeadAge();
@@ -422,7 +423,7 @@ export async function runDrain(input: DrainInput): Promise<SyncResult> {
Promise.resolve().then(() => probe.head!()).then(head => {
if (!head || !inPass || stall || clock() - lastCommitAt < every || tickAt - lastLine < every) return;
lastLine = tickAt;
- serr(stallText(index, total, head.claim?.step_age_ms ?? headAge(head.request_id, clock(), lastCommitAt), head.claim, head.head_state, head));
+ serr(stallText(index, total, headAge(head.request_id, clock(), lastCommitAt, head.claim?.step_age_ms), head.claim, head.head_state, head));
}).catch(() => undefined).finally(() => { readingHead = false; });
}, every) : null;
stallLine?.unref?.();
diff --git a/test/sync-drain-claims.test.ts b/test/sync-drain-claims.test.ts
index 6e1d2255a5..8ef8941a2c 100644
--- a/test/sync-drain-claims.test.ts
+++ b/test/sync-drain-claims.test.ts
@@ -344,6 +344,31 @@ describe('live stall line during a pass (B6)', () => {
} finally { err.restore(); }
});
+ // The next night's owner-kill stall: between two queued heads the head was claimed (the consumer publishing the dead
+ // serve's writes) for two minutes. Claimed reads print the claim's own age and never moved the queued-head clock, so the
+ // next queued head counted from the last queued read and printed "stalled 150s on queued".
+ test('a claimed head between two queued heads is progress: the next queued head counts from the read before it', async () => {
+ const err = captureStderr();
+ try {
+ let now = 0, reads = 0;
+ const t0 = Date.now();
+ const publishing: DrainClaim = { phase: 'publishing', step: null, waiting_on: null, step_age_ms: 2_000, claim_age_ms: 2_000, lapsed: false,
+ owner_pid: null, owner_kind: null, owner_nonce: null, last_sql: null, allowance_ms: 150_000, ceiling_ms: 600_000 };
+ const head: StallProbe['head'] = async () => {
+ reads++; now += 10_000;
+ if (reads === 1) return { head_state: 'queued', claim: null, request_id: 'r1' };
+ if (reads <= 7) return { head_state: 'claimed', claim: publishing, request_id: `c${reads}` };
+ return { head_state: 'queued', claim: null, request_id: 'r2' };
+ };
+ await runDrain({ announce: true, progressMs: PROGRESS, governMs: 60_000, clock: () => now + (Date.now() - t0) / 10, probe: headProbe(head), pass: async () => {
+ expect(await until(() => err.stalled().length >= 9, 30 * PROGRESS)).toBe(true);
+ return done();
+ } });
+ const ages = err.stalled().slice(0, 9).map(line => Number(/stalled (\d+)s on (?:queued|publishing)/.exec(line)?.[1]));
+ expect(ages).toEqual([10, 2, 2, 2, 2, 2, 2, 10, 20]);
+ } finally { err.restore(); }
+ });
+
test('a head read that rejects or hangs never escapes the ticker or stacks; announce off never reads', async () => {
const err = captureStderr();
try {