Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(drain): read the owner pid from the stamp's owner object (preparation_abandoned never fired in production) and the step age from step_since

garrytana99c00565704
diff --git a/src/core/persistence/sync-drain.ts b/src/core/persistence/sync-drain.ts
index 22479c45d8..f8b2fee1df 100644
--- a/src/core/persistence/sync-drain.ts
+++ b/src/core/persistence/sync-drain.ts
@@ -353,11 +353,17 @@ export function drainClaimOf(row: { head_state: string | null; head_claim_phase:
const waiting = own ? stamp!.waiting_on : null;
return { phase, step: own && typeof stamp!.step === 'string' ? stamp!.step : null,
waiting_on: typeof waiting === 'string' && ['git', 'fs', 'db', 'pool', 'unknown'].includes(waiting) ? waiting as DrainClaim['waiting_on'] : own ? 'unknown' : null,
- step_age_ms: own ? age(stamp!.since) : null, claim_age_ms: own ? age(stamp!.claimed_at) : null, lapsed: row.head_lapsed === true,
- owner_pid: own && typeof stamp!.pid === 'number' ? stamp!.pid : null,
+ step_age_ms: own ? age(stamp!.step_since ?? stamp!.since) : null, claim_age_ms: own ? age(stamp!.claimed_at) : null, lapsed: row.head_lapsed === true,
+ owner_pid: own ? ownerPid(stamp!) : null,
allowance_ms: Math.min(budgets.ceilingMs + STALL_GRACE_MS, budget + STALL_GRACE_MS) };
}
function safeJson(text: string): unknown { try { return JSON.parse(text); } catch { return null; } }
+/** The stamp stores the owner as `owner: { kind, pid, version }` (claim-phase.ts); a top-level `pid` is the pre-release shape test fixtures used. */
+function ownerPid(stamp: Record<string, unknown>): number | null {
+ const owner = stamp.owner && typeof stamp.owner === 'object' ? stamp.owner as Record<string, unknown> : null;
+ const pid = owner && typeof owner.pid === 'number' ? owner.pid : stamp.pid;
+ return typeof pid === 'number' ? pid : null;
+}
/** The preparation budgets the drain's allowance reads (Lane A defines and validates the keys; defaults are the documented ones). */
async function readPreparationBudgets(engine: Pick<BrainEngine, 'getConfig'>): Promise<{ syncMs: number; maintenanceMs: number; ceilingMs: number }> {
const read = async (key: string, fallback: number) => { const n = Number((await engine.getConfig(key).catch(() => null))?.trim()); return Number.isInteger(n) && n > 0 ? n : fallback; };
diff --git a/test/sync-drain-claims.test.ts b/test/sync-drain-claims.test.ts
index 15902fb46d..ad7c2051dd 100644
--- a/test/sync-drain-claims.test.ts
+++ b/test/sync-drain-claims.test.ts
@@ -110,8 +110,13 @@ describe('drainClaimOf', () => {
const stamp = (over: Record<string, unknown> = {}) => ({ phase: 'preparing', claimed_at: '2026-10-07T11:58:00Z', since: '2026-10-07T11:59:00Z', token: 't', ...over });
test('reads this claim\'s stamp: phase, step, wait cause, ages, pid and the allowance from the head\'s budget plus grace', () => {
- const c = drainClaimOf({ head_state: 'running', head_claim_phase: stamp({ step: 'origin_check', waiting_on: 'pool', pid: 4242 }), head_token: 't', head_lapsed: false, head_kind: 'managed_sync_import' }, budgets, now);
- expect(c).toEqual({ phase: 'preparing', step: 'origin_check', waiting_on: 'pool', step_age_ms: 60_000, claim_age_ms: 120_000, lapsed: false, owner_pid: 4242, allowance_ms: 120_000 + STALL_GRACE_MS });
+ // The stamp claim-phase.ts writes: the pid lives under `owner`, and the step has its own `step_since`.
+ const c = drainClaimOf({ head_state: 'running', head_claim_phase: stamp({ step: 'origin_check', step_since: '2026-10-07T11:59:30Z', waiting_on: 'pool', owner: { kind: 'sync', pid: 4242, version: '0.60.109.0' } }),
+ head_token: 't', head_lapsed: false, head_kind: 'managed_sync_import' }, budgets, now);
+ expect(c).toEqual({ phase: 'preparing', step: 'origin_check', waiting_on: 'pool', step_age_ms: 30_000, claim_age_ms: 120_000, lapsed: false, owner_pid: 4242, allowance_ms: 120_000 + STALL_GRACE_MS });
+ // A stamp without `step_since` falls back to the phase's `since`; a pre-release top-level `pid` is still read.
+ expect(drainClaimOf({ head_state: 'running', head_claim_phase: stamp({ step: 'origin_check', pid: 4243 }), head_token: 't', head_lapsed: false, head_kind: 'managed_sync_import' }, budgets, now))
+ .toMatchObject({ step_age_ms: 60_000, owner_pid: 4243 });
expect(drainClaimOf({ head_state: 'running', head_claim_phase: JSON.stringify(stamp({ phase: 'publishing' })), head_token: 't', head_lapsed: false, head_kind: 'put_page' }, { ...budgets, maintenanceMs: 300_000 }, now))
.toMatchObject({ phase: 'publishing', step: null, waiting_on: 'unknown', allowance_ms: 300_000 + STALL_GRACE_MS });
// The ceiling caps the allowance.
@@ -138,7 +143,7 @@ describe('engineStallProbe fingerprint', () => {
const [wt] = await engine.executeRaw<{ id: string }>('INSERT INTO persistence_worktrees(owner_host_id) VALUES(gen_random_uuid()) RETURNING id::text AS id');
const [src] = await engine.executeRaw<{ incarnation: string }>('SELECT incarnation::text AS incarnation FROM sources WHERE id=$1', [id]);
const token = randomUUID(), requestId = randomUUID();
- const stampOf = (step: string) => JSON.stringify({ phase: 'preparing', claimed_at: new Date(Date.now() - 90_000).toISOString(), since: new Date(Date.now() - 60_000).toISOString(), token, step, waiting_on: 'db', pid: process.pid });
+ const stampOf = (step: string) => JSON.stringify({ phase: 'preparing', claimed_at: new Date(Date.now() - 90_000).toISOString(), since: new Date(Date.now() - 60_000).toISOString(), token, step, step_since: new Date(Date.now() - 60_000).toISOString(), waiting_on: 'db', owner: { kind: 'sync', pid: process.pid, version: 'test' } });
const [row] = await engine.executeRaw<{ id: string }>(`INSERT INTO persistence_requests(principal_kind,principal_id,request_id,operation,source_id,source_incarnation,slug,worktree_id,digest,authority,intent,intent_bytes,terminal_reservation,
state,execution_token,claim_expires_at,claim_phase)
VALUES('local_cli','cli:example',$1::uuid,'submit_job',$2,$3::uuid,'notes/p',$4::uuid,'d','{}'::jsonb,'{"kind":"managed_sync_import","path":"notes/p.md"}'::jsonb,1,16384,'running',$5::uuid,now()+interval '30 seconds',$6::text::jsonb)