Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Keep doctor's dream write deferrals inside a source-bound caller's sources

garrytanb5c3e2094c21
diff --git a/src/commands/doctor/checks/dream-write-deferred.ts b/src/commands/doctor/checks/dream-write-deferred.ts
index 38d66a71b5..ad58431ec8 100644
--- a/src/commands/doctor/checks/dream-write-deferred.ts
+++ b/src/commands/doctor/checks/dream-write-deferred.ts
@@ -11,8 +11,8 @@ import { DREAM_WRITE_DEFERRAL_ENABLED_KEY, DREAM_WRITE_DEFERRAL_MAX_REPLAYS, dre
import type { Check } from '../../doctor.ts';
import { agentFix } from '../check-fix.ts';
-export async function dreamWriteDeferredCheck(engine: BrainEngine): Promise<Check> {
- const deferrals = await readDreamWriteDeferrals(engine);
+export async function dreamWriteDeferredCheck(engine: BrainEngine, sourceIds?: string[]): Promise<Check> {
+ const deferrals = (await readDreamWriteDeferrals(engine)).filter(entry => !sourceIds || sourceIds.includes(entry.source_id));
const items = deferrals.map(entry => ({ phase: entry.phase, source_id: entry.source_id, request_id: entry.request_id,
replays: entry.replays, bound: DREAM_WRITE_DEFERRAL_MAX_REPLAYS, deferred_at: entry.deferred_at, status_command: writeStatusCommand(entry.request_id) }));
const escalated = items.filter(item => item.replays >= DREAM_WRITE_DEFERRAL_MAX_REPLAYS);
diff --git a/src/commands/doctor/checks/graph-health.ts b/src/commands/doctor/checks/graph-health.ts
index 592f6ae067..828f4d27aa 100644
--- a/src/commands/doctor/checks/graph-health.ts
+++ b/src/commands/doctor/checks/graph-health.ts
@@ -13,7 +13,7 @@ import { quarantineFilterFragment } from '../../../core/quarantine.ts';
import { entityTypePredicateSql, resolveEntityTypes } from '../../../core/schema-pack/entity-types.ts';
import { MIN_ENTITY_PAGES_FOR_COVERAGE } from '../../../core/types.ts';
import type { Check } from '../../doctor.ts';
-import { connectedEngine, type DoctorContext, type DoctorEntry, doctorHealth } from '../context.ts';
+import { connectedEngine, type DoctorContext, type DoctorEntry, doctorHealth, doctorSourceIds } from '../context.ts';
import { agentFix, brainScorePlanFix, checkError } from '../check-fix.ts';
import { ORPHAN_FULL_SCAN_KEY, countLivePages, estimateOrphanRatio, orphanFullScanMaxPages, type OrphanEstimate } from './orphan-estimate.ts';
@@ -320,7 +320,7 @@ async function runTimelineHistory(ctx: DoctorContext): Promise<Check[]> {
}
// Wave checks registered in doctor/wave-checks.ts rather than inline here.
const { runWaveChecks } = await import('../wave-checks.ts');
- for (const finding of await runWaveChecks(engine, { only: 'wave', ids: only ?? undefined, sourceIds: orphanRatioSourceId ? [orphanRatioSourceId] : undefined })) checks.push(finding.check);
+ for (const finding of await runWaveChecks(engine, { only: 'wave', ids: only ?? undefined, sourceIds: doctorSourceIds(ctx) })) checks.push(finding.check);
}
return checks;
}
diff --git a/src/commands/doctor/wave-checks.ts b/src/commands/doctor/wave-checks.ts
index 7fc3250953..e98a4183a8 100644
--- a/src/commands/doctor/wave-checks.ts
+++ b/src/commands/doctor/wave-checks.ts
@@ -264,7 +264,7 @@ export const WAVE_CHECKS: readonly WaveCheckSpec[] = [
count: d => (d.items ?? []).filter((item: { replays: number; bound: number }) => item.replays >= item.bound).length,
impact: 'A dream phase write is still pending after its replay bound, so the phase submits nothing more for that source',
instruction: 'Read the receipt with the `gbrain write-request` command doctor prints; if the writer is stuck, `gbrain sources writer status --source <id> --json` names why.',
- run: async engine => (await import('./checks/dream-write-deferred.ts')).dreamWriteDeferredCheck(engine),
+ run: async (engine, scope) => (await import('./checks/dream-write-deferred.ts')).dreamWriteDeferredCheck(engine, scope.sourceIds),
},
{
id: 'embedding_restamp_stalled', resolution: 'operator', registration: 'wave',
diff --git a/test/doctor-dream-write-deferred-source-scope.test.ts b/test/doctor-dream-write-deferred-source-scope.test.ts
new file mode 100644
index 0000000000..1e2455f138
--- /dev/null
+++ b/test/doctor-dream-write-deferred-source-scope.test.ts
@@ -0,0 +1,41 @@
+/**
+ * W3 x W4 (Train B review): dream_write_deferred lists source ids, request ids and status commands, so a
+ * source-bound run_doctor caller (the ops tier and the alert tier's page-class `only`) must see only its own
+ * sources' deferrals, as parked_effects does.
+ */
+import { afterAll, beforeAll, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import type { OperationContext } from '../src/core/ops/contract.ts';
+import { runRemoteDoctorTier } from '../src/commands/doctor/remote-tier.ts';
+import { DREAM_WRITE_DEFERRALS_KEY } from '../src/core/cycle/dream-write-deferral.ts';
+
+let engine: PGLiteEngine;
+const ACME_REQ = '11111111-1111-4111-8111-111111111111';
+const FUND_REQ = '22222222-2222-4222-8222-222222222222';
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ await engine.executeRaw("INSERT INTO sources(id,name) VALUES('acme-a','acme-a'),('fund-b','fund-b') ON CONFLICT DO NOTHING");
+ const entry = (source: string, request: string) => ({ phase: 'purge', source_id: source, subject: source + '/notes/x', request_id: request,
+ receipt_id: null, replays: 3, deferred_at: '2026-11-13T00:00:00.000Z' });
+ await engine.setConfig(DREAM_WRITE_DEFERRALS_KEY, JSON.stringify({ 'purge:acme-a': entry('acme-a', ACME_REQ), 'purge:fund-b': entry('fund-b', FUND_REQ) }));
+}, 120_000);
+
+afterAll(async () => {
+ await engine.disconnect();
+});
+
+const ctx = (sourceId: string): OperationContext => ({ engine, remote: true, transport: 'http', sourceId, config: {} as never,
+ logger: { info() {}, warn() {}, error() {} } } as unknown as OperationContext);
+
+for (const only of [['dream_write_deferred'], ['dream_write_deferred', 'jsonb_integrity']]) {
+ test(`a source-bound run_doctor only=${only.join(',')} sees its own deferrals and none of another source's`, async () => {
+ const report = await runRemoteDoctorTier(ctx('acme-a'), { only });
+ const text = JSON.stringify(report);
+ expect(text).toContain(ACME_REQ);
+ expect(text).not.toContain(FUND_REQ);
+ expect(text).not.toContain('fund-b');
+ }, 60_000);
+}