Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

scale: find_orphans known answer reads totals and one maximal page

garrytan3565852b7bb4
diff --git a/scripts/scale/gates.ts b/scripts/scale/gates.ts
index 1334d71762..6fa1cabeec 100644
--- a/scripts/scale/gates.ts
+++ b/scripts/scale/gates.ts
@@ -15,6 +15,7 @@
* variable GBRAIN_SCALE_ENFORCE_CEILINGS=1.
*/
+import { ORPHANS_MAX_LIMIT } from '../../src/core/ops/orphans.ts';
import { PLANNER_STATS_MIN_PENDING } from '../../src/core/planner-stats.ts';
/**
@@ -212,3 +213,27 @@ export function resultHits(value: unknown): Array<{ slug: string; source_id?: st
walk(value);
return hits;
}
+
+/**
+ * The find_orphans known-answer call: one page at the op's maximum size. The
+ * op returns a page of rows (default 100) plus `total_orphans`, and recomputes
+ * the whole orphan set per call, so the check reads totals and one maximal page
+ * instead of paging (a 20k brain holds more orphans than one default page).
+ */
+export const FIND_ORPHANS_PARAMS = { limit: ORPHANS_MAX_LIMIT } as const;
+
+/** Null when every fixture island is among the orphans, else what is wrong. */
+export function orphansProblem(result: unknown, islands: readonly string[]): string | null {
+ const r = (result ?? {}) as { orphans?: Array<{ slug?: string }>; total_orphans?: unknown };
+ const rows = Array.isArray(r.orphans) ? r.orphans : [];
+ const total = Number(r.total_orphans);
+ if (!Number.isInteger(total)) return 'find_orphans returned no total_orphans';
+ if (total < islands.length) return `find_orphans counts ${total} orphans, fewer than the fixture's ${islands.length} island pages`;
+ if (rows.length < total) {
+ return `find_orphans returned ${rows.length} of ${total} orphans in one call; the known-answer check reads every orphan in one page `
+ + `of at most ${ORPHANS_MAX_LIMIT}, so this fixture size needs a paged check`;
+ }
+ const found = new Set(rows.map(h => h.slug));
+ const missing = islands.filter(slug => !found.has(slug));
+ return missing.length ? `${missing.length} of ${islands.length} island pages missing from find_orphans (first: ${missing[0]})` : null;
+}
diff --git a/scripts/scale/run.ts b/scripts/scale/run.ts
index f6884df568..82b79e67dc 100644
--- a/scripts/scale/run.ts
+++ b/scripts/scale/run.ts
@@ -31,7 +31,7 @@ import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { corpusMarkdownFiles, generateScaleFixture, SCALE_SOURCES, scaleVector, writeScaleCorpus, type ScaleFixture } from './fixture.ts';
import {
- BUDGET_MULTIPLIER, evaluateScaleGates, HEADLINE_OP, HOT_TABLES, PLANNER_HEALTH_ENFORCED, PLANNER_STATS_MIN_ROWS, reproduceCommand, resultHits, verdictLines,
+ BUDGET_MULTIPLIER, evaluateScaleGates, FIND_ORPHANS_PARAMS, HEADLINE_OP, HOT_TABLES, PLANNER_HEALTH_ENFORCED, PLANNER_STATS_MIN_ROWS, orphansProblem, reproduceCommand, resultHits, verdictLines,
type DataCheck, type GatePolicy, type OpPlan, type OpResult, type PlanStatement, type ScaleReport,
} from './gates.ts';
@@ -236,7 +236,8 @@ async function main(): Promise<FullReport> {
const phases: Record<string, number> = {};
const timed = async <T>(name: string, fn: () => Promise<T>): Promise<T> => {
const t = performance.now();
- try { return await fn(); } finally { phases[name] = Math.round(performance.now() - t); }
+ console.log(`[scale] phase ${name} start`);
+ try { return await fn(); } finally { phases[name] = Math.round(performance.now() - t); console.log(`[scale] phase ${name} done in ${phases[name]} ms`); }
};
console.log(`[scale] engine=${engineKind} pages=${pagesArg} seed=${seed} import-mode=${importMode} ${enforce ? 'enforce' : 'report-only'}`);
const fixture: ScaleFixture = generateScaleFixture({ pages: pagesArg, seed });
@@ -363,8 +364,8 @@ async function main(): Promise<FullReport> {
verify: r => hub.links.every(target => resultHits(r).some(h => h.slug === target)) ? null : 'a direct link target of the hub is missing' },
{ op: 'get_backlinks', run: () => op('get_backlinks').handler(local, { slug: hub.links[0] }),
verify: r => JSON.stringify(r).includes(`"${fixture.hub}"`) ? null : `${fixture.hub} missing from backlinks of ${hub.links[0]}` },
- { op: 'find_orphans', run: () => op('find_orphans').handler(local, {}),
- verify: r => fixture.islands.every(slug => resultHits(r).some(h => h.slug === slug)) ? null : 'an island page is missing from find_orphans' },
+ { op: 'find_orphans', run: () => op('find_orphans').handler(local, { ...FIND_ORPHANS_PARAMS }),
+ verify: r => orphansProblem(r, fixture.islands) },
];
const ops: OpResult[] = [];
const statRows: Record<string, number> = {};
diff --git a/src/core/ops/orphans.ts b/src/core/ops/orphans.ts
index 1d2c8becc2..b19638ba16 100644
--- a/src/core/ops/orphans.ts
+++ b/src/core/ops/orphans.ts
@@ -11,8 +11,8 @@ import { invalidParam } from './op-fix.ts';
// --- Orphans ---
-const ORPHANS_DEFAULT_LIMIT = 100;
-const ORPHANS_MAX_LIMIT = 1000;
+export const ORPHANS_DEFAULT_LIMIT = 100;
+export const ORPHANS_MAX_LIMIT = 1000;
const find_orphans: Operation = {
name: 'find_orphans',
diff --git a/test/scripts/scale-orphans-verifier.test.ts b/test/scripts/scale-orphans-verifier.test.ts
new file mode 100644
index 0000000000..31b8a42ff0
--- /dev/null
+++ b/test/scripts/scale-orphans-verifier.test.ts
@@ -0,0 +1,59 @@
+/**
+ * The scale tier's find_orphans known answer (scripts/scale/gates.ts
+ * FIND_ORPHANS_PARAMS + orphansProblem) on a brain holding more orphans than
+ * one default find_orphans page.
+ *
+ * Protects: the 20k scale gate's verdict on find_orphans. Since #5891 the op
+ * returns one page of rows (default 100) plus `total_orphans`; a 20k fixture
+ * holds 104 orphans in its default source, so a verifier that reads one
+ * default page reports "an island page is missing" against a correct engine.
+ * Fails when: the known-answer call drops back to the default page size, or
+ * the check stops comparing the returned rows with `total_orphans`.
+ * Why new: test/scripts/scale-gates.test.ts plants verdicts into finished
+ * reports and never runs the op; the 40-page harness run holds one island.
+ * Seam: none (the real op handler on an in-memory PGLite brain).
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';
+import { operations, type OperationContext } from '../../src/core/operations.ts';
+import { FIND_ORPHANS_PARAMS, orphansProblem } from '../../scripts/scale/gates.ts';
+
+const ISLANDS = 150;
+let engine: PGLiteEngine;
+const islands = Array.from({ length: ISLANDS }, (_, i) => `notes/island-${String(i).padStart(3, '0')}`);
+const ctx = () => ({
+ engine, config: {}, logger: { info() {}, warn() {}, error() {}, debug() {} }, dryRun: false, remote: false, sourceId: 'default',
+}) as unknown as OperationContext;
+const findOrphans = (params: Record<string, unknown>) => operations.find(o => o.name === 'find_orphans')!.handler(ctx(), params);
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ for (const slug of islands) {
+ await engine.putPage(slug, { type: 'note', title: slug, compiled_truth: `Island ${slug}.`, frontmatter: {} });
+ }
+}, 120_000);
+afterAll(async () => { await engine.disconnect(); });
+
+describe('scale find_orphans known answer with more orphans than one default page', () => {
+ test('the verifier call returns every island and passes', async () => {
+ const result = await findOrphans({ ...FIND_ORPHANS_PARAMS });
+ expect((result as { total_orphans: number }).total_orphans).toBe(ISLANDS);
+ expect(orphansProblem(result, islands)).toBeNull();
+ });
+
+ test('one default page is reported as truncated, never as a missing island', async () => {
+ const page = await findOrphans({});
+ expect((page as { orphans: unknown[] }).orphans).toHaveLength(100);
+ expect(orphansProblem(page, islands)).toContain(`returned 100 of ${ISLANDS} orphans in one call`);
+ });
+
+ test('a real omission is still named', async () => {
+ const result = await findOrphans({ ...FIND_ORPHANS_PARAMS }) as { orphans: Array<{ slug: string }>; total_orphans: number };
+ const missingOne = { ...result, orphans: result.orphans.slice(1), total_orphans: result.total_orphans - 1 };
+ expect(orphansProblem(missingOne, islands)).toContain(`fewer than the fixture's ${ISLANDS} island pages`);
+ const wrongRow = { ...result, orphans: [{ slug: 'notes/not-an-island' }, ...result.orphans.slice(1)] };
+ expect(orphansProblem(wrongRow, islands)).toBe(`1 of ${ISLANDS} island pages missing from find_orphans (first: ${islands[0]})`);
+ });
+});