Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
perf(#6803): the drain walks the queue once per run, not once per batch
garrytan6314b5e1a624
diff --git a/src/core/page-state/projections.ts b/src/core/page-state/projections.tsindex 672ca2e40b..fa816c35a6 100644--- a/src/core/page-state/projections.ts+++ b/src/core/page-state/projections.ts@@ -497,9 +497,18 @@ export interface ProjectionRebuildOptions {window?: ProjectionScanCursor;/** #6803: a media drain seals images next, so the statistics refresh also waits for its `sealable` rows. */media?: boolean;+ /**+ * #6803, the drain's walk position: only rows after `walk.after`, which+ * each call advances to the last row it attempted. Every row before it was+ * attempted or is not an inline row, so 93K queued images are walked once+ * per run instead of once per batch. Ignored with `window`.+ */+ walk?: { after?: QueueKey };}-type QueuedJob = { source_id: string; source_incarnation: string; slug: string; revision: string; page_kind: string };+/** A queue row's position in the walk order (updated_at as text, so microseconds survive). */+export type QueueKey = { updated_at: string; source_incarnation: string; slug: string };+type QueuedJob = { source_id: string; source_incarnation: string; slug: string; revision: string; page_kind: string; updated_at?: string };type WindowKey = { source_incarnation: string; slug: string };/**@@ -631,9 +640,14 @@ export async function rebuildPendingPageProjections(engine: BrainEngine, limit =if (opts.notAfter !== undefined) { params.push(opts.notAfter); filters.push(`j.updated_at<=$${params.length}::text::timestamptz`); }if (opts.reasons) { params.push(opts.reasons); filters.push(`j.reason=ANY($${params.length}::text[])`); }if (opts.pages) { params.push(opts.pages.slugs); filters.push(`j.slug=ANY($${params.length}::text[])`); }+ const after = opts.window ? undefined : opts.walk?.after;+ if (after) {+ params.push(after.updated_at, after.source_incarnation, after.slug);+ filters.push(`(j.updated_at,j.source_incarnation,j.slug)>($${params.length - 2}::text::timestamptz,$${params.length - 1}::uuid,$${params.length})`);+ }const sourceFilter = opts.pages ? `WHERE s.id=$${params.push(opts.pages.sourceId)}` : '';const selected = opts.window ? await selectWindowJobs(engine, opts.window, opts.retryCooldown === true) : null;- const jobs = selected ? selected.jobs.slice(0, Number(params[0])) : await engine.executeRaw<QueuedJob>(`SELECT s.id AS source_id,j.source_incarnation,j.slug,j.revision,p.page_kind+ const jobs = selected ? selected.jobs.slice(0, Number(params[0])) : await engine.executeRaw<QueuedJob>(`SELECT s.id AS source_id,j.source_incarnation,j.slug,j.revision,p.page_kind,j.updated_at::text AS updated_atFROM (SELECT source_incarnation,slug,revision,updated_at FROM page_projection_jobs j${filters.length ? `WHERE ${filters.join(' AND ')}` : ''}ORDER BY updated_at,source_incarnation,slug OFFSET 0) j@@ -710,6 +724,10 @@ export async function rebuildPendingPageProjections(engine: BrainEngine, limit =: !(await engine.executeRaw(`SELECT 1 FROM page_projection_jobs j${projectionJobProbes({ media: opts.media })}WHERE j.reason<>'rebuild_failed' LIMIT 1`)).length);+ const last = jobs[attempted - 1];+ if (opts.walk && !selected && last?.updated_at !== undefined) {+ opts.walk.after = { updated_at: last.updated_at, source_incarnation: last.source_incarnation, slug: last.slug };+ }return { rebuilt, superseded };}@@ -760,6 +778,7 @@ export async function drainProjections(engine: BrainEngine, opts: { limit?: numblet superseded = 0;let tried = 0;let seal = { sealed: 0, sealed_with_visual: 0, sealed_text_only: 0 };+ const walk: { after?: QueueKey } = {};try {// Continue while a batch rebuilt, removed, failed or durably deferred a// row; stop on a no-op batch. A bare `superseded` row (another worker@@ -768,7 +787,7 @@ export async function drainProjections(engine: BrainEngine, opts: { limit?: numblet healed = 0;let failedNow = 0;const batch = await rebuildPendingPageProjections(engine, Math.min(DRAIN_BATCH, (opts.limit ?? Infinity) - tried),- { notAfter: runStart, reasons: opts.reasons, media: opts.media, onHeal: () => { healed++; },+ { notAfter: runStart, reasons: opts.reasons, media: opts.media, walk, onHeal: () => { healed++; },onFailure: failure => { failed.push(failure); if (!failure.reason.startsWith('revision_conflict:')) failedNow++; } });rebuilt += batch.rebuilt;superseded += batch.superseded;