Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(orphans): read the orphan policy before starting the progress heartbeat

garrytan5c8d770ae7c1
diff --git a/src/commands/orphans.ts b/src/commands/orphans.ts
index 7224aff413..f3120a8d5b 100644
--- a/src/commands/orphans.ts
+++ b/src/commands/orphans.ts
@@ -139,6 +139,10 @@ export async function findOrphans(
const sourceId = opts.sourceId;
const sourceIds =
opts.sourceIds && opts.sourceIds.length > 0 ? opts.sourceIds : undefined;
+ const overrides = includePseudo ? undefined : await loadOrphanPolicyOverrides(engine);
+ const connectorSources = includePseudo ? new Set<string>() : await loadConnectorSourceIds(engine);
+ const isConnectorRender = (row: { type?: string | null; source_id?: string }) =>
+ !!row.source_id && connectorSources.has(row.source_id) && !!row.type && CONNECTOR_RENDER_TYPES.has(row.type);
// The NOT EXISTS anti-join over pages × links can take seconds on 50K-page
// brains. Heartbeat every second so agents see the scan is alive. Keyset
// pagination was considered and rejected: without an index on
@@ -150,10 +154,6 @@ export async function findOrphans(
let allOrphans: { slug: string; title: string; domain: string | null; type?: string | null; quarantined?: boolean; source_id?: string }[];
let total: number;
let excludedAll: number;
- const overrides = includePseudo ? undefined : await loadOrphanPolicyOverrides(engine);
- const connectorSources = includePseudo ? new Set<string>() : await loadConnectorSourceIds(engine);
- const isConnectorRender = (row: { type?: string | null; source_id?: string }) =>
- !!row.source_id && connectorSources.has(row.source_id) && !!row.type && CONNECTOR_RENDER_TYPES.has(row.type);
try {
allOrphans = await engine.findOrphanPages({
excludePrivate: opts.excludePrivate,
diff --git a/test/orphans.test.ts b/test/orphans.test.ts
index db9c6a46bc..d117479854 100644
--- a/test/orphans.test.ts
+++ b/test/orphans.test.ts
@@ -1,4 +1,4 @@
-import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
+import { describe, test, expect, beforeEach, afterEach, spyOn } from 'bun:test';
import {
shouldExclude,
deriveDomain,
@@ -10,6 +10,21 @@ import {
} from '../src/commands/orphans.ts';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+// --- findOrphans setup failure ---
+
+describe('findOrphans setup failure', () => {
+ test('a policy read that fails starts no heartbeat (nothing keeps writing to stderr after the throw)', async () => {
+ const intervals = spyOn(globalThis, 'setInterval');
+ try {
+ const engine = { getConfig: async () => { throw new Error('config read failed'); }, executeRaw: async () => [] };
+ await expect(findOrphans(engine as never)).rejects.toThrow('config read failed');
+ expect(intervals).not.toHaveBeenCalled();
+ } finally {
+ intervals.mockRestore();
+ }
+ });
+});
+
// --- shouldExclude ---
describe('shouldExclude', () => {