Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): doctor owner_host_stale reads the database clock

garrytan0fbddc1e2a0f
diff --git a/src/commands/doctor/checks/owner-host-stale.ts b/src/commands/doctor/checks/owner-host-stale.ts
index 5e04ac1d0f..e7544be8fa 100644
--- a/src/commands/doctor/checks/owner-host-stale.ts
+++ b/src/commands/doctor/checks/owner-host-stale.ts
@@ -16,7 +16,7 @@ import { connectedEngine, type DoctorContext, type DoctorEntry } from '../contex
const DOCS = 'docs/guides/troubleshooting.md#owner-host-stale';
-export async function ownerHostStaleCheck(engine: BrainEngine, now = Date.now()): Promise<Check> {
+export async function ownerHostStaleCheck(engine: BrainEngine): Promise<Check> {
try {
const [present] = await engine.executeRaw<{ managed: boolean }>(`SELECT to_regclass('public.persistence_requests') IS NOT NULL AND to_regclass('public.persistence_worktrees') IS NOT NULL AS managed`);
if (!present?.managed) return { name: 'owner_host_stale', status: 'ok', message: 'This brain keeps no managed write journal.', details: { count: 0, stale_after_ms: OWNER_STALE_MS, stale: [], docs: DOCS } };
@@ -25,7 +25,7 @@ export async function ownerHostStaleCheck(engine: BrainEngine, now = Date.now())
(SELECT array_agg(b.source_id ORDER BY b.source_id) FROM persistence_source_bindings b WHERE b.worktree_id=r.worktree_id) AS source_ids
FROM persistence_requests r WHERE r.worktree_id IS NOT NULL AND r.state IN ('queued','running','recovering')
GROUP BY r.worktree_id`);
- const owners = await ownerActivity(engine, open.map(row => row.worktree_id), { now });
+ const owners = await ownerActivity(engine, open.map(row => row.worktree_id));
const stale = open.flatMap(row => {
const owner = owners.get(row.worktree_id);
return owner?.stale ? [{ worktree_id: row.worktree_id, source_ids: row.source_ids ?? [], owner_host_id: owner.owner_host_id, last_active: owner.last_active,
diff --git a/src/core/persistence/owner-activity.ts b/src/core/persistence/owner-activity.ts
index 8c7cfc8a0a..7a96da597c 100644
--- a/src/core/persistence/owner-activity.ts
+++ b/src/core/persistence/owner-activity.ts
@@ -38,7 +38,7 @@ export interface OwnerActivity {
stale: boolean;
}
-export async function ownerActivity(engine: BrainEngine, worktreeIds: readonly string[], opts: { now?: number; signal?: AbortSignal } = {}): Promise<Map<string, OwnerActivity>> {
+export async function ownerActivity(engine: BrainEngine, worktreeIds: readonly string[], opts: { signal?: AbortSignal } = {}): Promise<Map<string, OwnerActivity>> {
const result = new Map<string, OwnerActivity>();
const ids = [...new Set(worktreeIds)].filter(Boolean);
if (!ids.length) return result;
@@ -63,7 +63,7 @@ export async function ownerActivity(engine: BrainEngine, worktreeIds: readonly s
const self = local === row.owner_host_id;
const foreign = engine.kind === 'pglite' && local !== null && !self;
const since = lastActive ?? (row.oldest_open === null ? null : new Date(row.oldest_open).toISOString());
- const quiet = since !== null && (opts.now ?? new Date(row.db_now).getTime()) - Date.parse(since) > OWNER_STALE_MS;
+ const quiet = since !== null && new Date(row.db_now).getTime() - Date.parse(since) > OWNER_STALE_MS;
result.set(row.worktree_id, { worktree_id: row.worktree_id, owner_host_id: row.owner_host_id, live: !!row.live,
last_active: lastActive, foreign_file_holder: foreign, stale: !self && !row.live && (foreign || quiet) });
}
diff --git a/test/persistence-owner-activity.test.ts b/test/persistence-owner-activity.test.ts
index f60cf44073..502d5ebdd3 100644
--- a/test/persistence-owner-activity.test.ts
+++ b/test/persistence-owner-activity.test.ts
@@ -6,7 +6,7 @@
* a live consumer keeps its owner fresh and its clean stop leaves a durable
* timestamp; a repo-less (database-only) write never names an owner.
*/
-import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { afterAll, beforeAll, describe, expect, spyOn, test } from 'bun:test';
import { randomUUID } from 'node:crypto';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
@@ -20,6 +20,7 @@ import { getWriteRequestById, receiptFor, writeHealthFacts } from '../src/core/p
import { isWriteReceipt, publicWriteReceipt } from '../src/core/persistence/types.ts';
import { acceptPendingCovers, writeErrorExitCode } from '../src/core/persistence/write-wait.ts';
import { PENDING_WRITE_EXIT_CODE } from '../src/core/exit-codes.ts';
+import { ownerHostStaleCheck } from '../src/commands/doctor/checks/owner-host-stale.ts';
import { withEnv } from './helpers/with-env.ts';
import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
@@ -119,6 +120,19 @@ describe('owner activity', () => {
}
}));
+ test('Postgres: staleness reads the database clock, so a caller clock days ahead neither stales a recent owner in the receipt nor in doctor', async () => inHome(async () => {
+ for (const engine of engines.filter(e => e.kind === 'postgres')) {
+ const recent = await worktree(engine, randomUUID(), 1);
+ await request(engine, recent, 'queued', 1);
+ const clock = spyOn(Date, 'now').mockReturnValue(Date.now() + 3 * OWNER_STALE_MS);
+ try {
+ expect((await ownerActivity(engine, [recent])).get(recent)).toMatchObject({ live: false, stale: false });
+ const check = await ownerHostStaleCheck(engine);
+ expect((check.details as { stale: { worktree_id: string }[] }).stale.map(row => row.worktree_id)).not.toContain(recent);
+ } finally { clock.mockRestore(); }
+ }
+ }));
+
test('Postgres: a live consumer keeps its owner fresh and a clean stop leaves the time it stopped', async () => inHome(async () => {
for (const engine of engines.filter(e => e.kind === 'postgres')) {
const owner = randomUUID();