Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(grade_takes): oldest-first window that skips too-recent takes in SQL and puts ungraded takes first

garrytan2f502588960d
diff --git a/src/core/cycle/grade-takes.ts b/src/core/cycle/grade-takes.ts
index 9b00183476..d98f7f6947 100644
--- a/src/core/cycle/grade-takes.ts
+++ b/src/core/cycle/grade-takes.ts
@@ -618,11 +618,19 @@ class GradeTakesPhase extends BaseCyclePhase {
// streak; auth/billing halt on the first hit.
const llmHalt = createGlobalLlmHaltTracker();
- // Load unresolved active takes, oldest-first.
+ // Load unresolved active takes, oldest-first. Takes too recent to grade
+ // are left out in SQL so they cannot fill the window, and takes with no
+ // verdict for this prompt and judge come first, so graded takes (which stay
+ // unresolved while auto-resolve is off) leave room for the ones behind them.
+ const cutoff = new Date();
+ cutoff.setMonth(cutoff.getMonth() - minAgeMonths);
const takes = await engine.listTakes({
resolved: false,
active: true,
sortBy: 'since_date',
+ oldestFirst: true,
+ sinceOnOrBefore: cutoff.toISOString().slice(0, 10),
+ ungradedFirst: { promptVersion, judgeModelId },
limit: takeLimit,
});
diff --git a/src/core/engine-sql/takes.ts b/src/core/engine-sql/takes.ts
index ea7bd0df9f..3ebcd4c5de 100644
--- a/src/core/engine-sql/takes.ts
+++ b/src/core/engine-sql/takes.ts
@@ -323,10 +323,16 @@ export async function listTakes(exec: LegacyUnscopedRead, opts: TakesListOpts =
${opts.takesHoldersAllowList ?? null}::text[] IS NULL
OR t.holder = ANY(${opts.takesHoldersAllowList ?? null}::text[])
)
+ AND (${opts.sinceOnOrBefore ?? null}::text IS NULL OR (t.since_date IS NOT NULL AND NOT (
+ t.since_date ~ '^[0-9]{4}-[0-9]{2}(-[0-9]{2})?$'
+ AND (CASE WHEN length(t.since_date) = 7 THEN t.since_date || '-15' ELSE t.since_date END) > ${opts.sinceOnOrBefore ?? null}::text)))
${sourceFilter}${takeEligibility(opts.eligibility)}
ORDER BY
+ ${opts.ungradedFirst ? sqlFragment`EXISTS (SELECT 1 FROM take_grade_cache g WHERE g.take_id = t.id
+ AND g.prompt_version = ${opts.ungradedFirst.promptVersion} AND g.judge_model_id = ${opts.ungradedFirst.judgeModelId}),` : sqlFragment``}
+ CASE WHEN ${opts.sortBy ?? 'created_at'} = 'since_date' AND ${opts.oldestFirst ?? false}::boolean THEN t.since_date END ASC NULLS LAST,
CASE WHEN ${opts.sortBy ?? 'created_at'} = 'weight' THEN t.weight END DESC NULLS LAST,
- CASE WHEN ${opts.sortBy ?? 'created_at'} = 'since_date' THEN t.since_date END DESC NULLS LAST,
+ CASE WHEN ${opts.sortBy ?? 'created_at'} = 'since_date' AND NOT ${opts.oldestFirst ?? false}::boolean THEN t.since_date END DESC NULLS LAST,
CASE WHEN ${opts.sortBy ?? 'created_at'} = 'created_at' THEN t.created_at END DESC NULLS LAST
LIMIT ${limit} OFFSET ${offset}
`)).rows;
diff --git a/src/core/engine.ts b/src/core/engine.ts
index 885dc7cd20..feef306fc3 100644
--- a/src/core/engine.ts
+++ b/src/core/engine.ts
@@ -325,6 +325,16 @@ export interface TakesListOpts extends PageReadPolicy {
/** Per-token MCP allow-list. Server applies AND holder = ANY($takesHoldersAllowList) when set. */
takesHoldersAllowList?: string[];
sortBy?: 'weight' | 'since_date' | 'created_at';
+ /** With `sortBy: 'since_date'`, oldest first (default newest first). */
+ oldestFirst?: boolean;
+ /**
+ * Only takes made on or before this ISO date (YYYY-MM-DD): a take with no
+ * since_date is left out, and one in ISO form (YYYY-MM-DD, or YYYY-MM read as
+ * the 15th) after the date is left out; any other form is kept for the caller.
+ */
+ sinceOnOrBefore?: string;
+ /** Takes with no take_grade_cache row for this prompt version and judge come first. */
+ ungradedFirst?: { promptVersion: string; judgeModelId: string };
limit?: number;
offset?: number;
/** Federated/source scope via the take's page.source_id. Array wins over
diff --git a/test/grade-takes-window.test.ts b/test/grade-takes-window.test.ts
new file mode 100644
index 0000000000..dc4774ca9c
--- /dev/null
+++ b/test/grade-takes-window.test.ts
@@ -0,0 +1,89 @@
+/**
+ * grade_takes work window (src/core/cycle/grade-takes.ts, listTakes in src/core/engine-sql/takes.ts).
+ *
+ * Protects: the phase reads unresolved takes oldest-first, as documented; takes
+ * too recent to grade (an ISO since_date after the min-age cutoff, or none) are
+ * left out in SQL so they cannot fill the window; takes with no verdict for the
+ * current prompt and judge come before graded ones, so with auto-resolve off a
+ * graded take leaves the window and the next pass grades the ones behind it;
+ * a brain smaller than the window grades exactly the same takes as before.
+ * Fails when: the window sorts newest-first, 50 too-recent takes block all
+ * grading, or the same graded takes hold the window every pass while older
+ * ungraded takes are never judged.
+ * Why new: grade-takes.test.ts drives a mocked listTakes, so no test ran the
+ * window query on a real engine.
+ * Runs on PGLite, and on Postgres with a safe DATABASE_URL (test/postgres-unit-arms.txt).
+ */
+import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import type { BrainEngine, Take } from '../src/core/engine.ts';
+import type { OperationContext } from '../src/core/operations.ts';
+import { runPhaseGradeTakes, type EvidenceRetrieverFn, type JudgeFn } from '../src/core/cycle/grade-takes.ts';
+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
+import { testBackends } from './helpers/test-backends.ts';
+
+const monthsAgo = (n: number) => { const d = new Date(); d.setMonth(d.getMonth() - n); return d.toISOString().slice(0, 10); };
+
+for (const kind of testBackends()) {
+ describe(`grade_takes window (${kind})`, () => {
+ let engine: BrainEngine;
+ let close: () => Promise<void>;
+ let n = 0;
+
+ beforeAll(async () => {
+ if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));
+ else {
+ engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema();
+ close = () => engine.disconnect();
+ }
+ }, 120_000);
+ afterAll(async () => { await close?.(); });
+ beforeEach(async () => {
+ await engine.executeRaw('DELETE FROM take_grade_cache');
+ await engine.executeRaw('DELETE FROM takes');
+ });
+
+ const seed = async (rows: Array<{ claim: string; since: string | undefined }>) => {
+ const page = await engine.putPage(`notes/grade-window-${++n}`, { type: 'note', title: 'Grade window', compiled_truth: 'Seed page.', timeline: '' });
+ await engine.addTakesBatch(rows.map((r, i) => ({ page_id: page.id, row_num: i + 1, claim: r.claim, kind: 'bet' as const, holder: 'alice-example', weight: 0.6,
+ ...(r.since ? { since_date: r.since } : {}) })));
+ };
+ const ctx = (): OperationContext => ({ engine, config: {} as never, logger: { info() {}, warn() {}, error() {} } as never, dryRun: false, remote: false, sourceId: 'default' });
+ const pass = async (takeLimit: number) => {
+ const judged: string[] = [];
+ const evidence: string[] = [];
+ const judge: JudgeFn = async ({ take }: { take: Take }) => { judged.push(take.claim); return { verdict: 'unresolvable', confidence: 0.5, reasoning: 'stub' }; };
+ const evidenceRetriever: EvidenceRetrieverFn = async (take: Take) => { evidence.push(take.claim); return `evidence for ${take.claim}`; };
+ const result = await runPhaseGradeTakes(ctx(), { judge, evidenceRetriever, takeLimit, minAgeMonths: 6, model: 'anthropic:claude-sonnet-4-6' });
+ return { judged, evidence, details: result.details as Record<string, unknown> };
+ };
+
+ test('too-recent takes do not fill the window, and it reads oldest-first', async () => {
+ await seed([
+ { claim: 'recent-1', since: monthsAgo(1) }, { claim: 'recent-2', since: monthsAgo(1) }, { claim: 'recent-3', since: monthsAgo(2) },
+ { claim: 'undated', since: undefined }, { claim: 'old-2021', since: '2021-03' }, { claim: 'old-2020', since: '2020-01-01' },
+ ]);
+ const first = await pass(3);
+ expect(first.judged).toEqual(['old-2020', 'old-2021']);
+ expect(first.evidence).toEqual(['old-2020', 'old-2021']);
+ });
+
+ test('with auto-resolve off, graded takes leave the window and the next pass grades the takes behind them', async () => {
+ await seed(['2016', '2017', '2018', '2019', '2020'].map(y => ({ claim: `take-${y}`, since: `${y}-06-01` })));
+ expect((await pass(3)).judged).toEqual(['take-2016', 'take-2017', 'take-2018']);
+ const second = await pass(3);
+ expect(second.judged).toEqual(['take-2019', 'take-2020']);
+ const third = await pass(3);
+ expect(third.judged).toEqual([]);
+ expect(third.details.cache_hits).toBe(3);
+ });
+
+ test('a brain smaller than the window grades every eligible take once', async () => {
+ await seed([{ claim: 'a', since: '2019-01-01' }, { claim: 'b', since: '2018-01-01' }, { claim: 'c', since: '2020-01' }, { claim: 'young', since: monthsAgo(1) }]);
+ expect((await pass(50)).judged.sort()).toEqual(['a', 'b', 'c']);
+ const again = await pass(50);
+ expect(again.judged).toEqual([]);
+ expect(again.details.cache_hits).toBe(3);
+ });
+ });
+}
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index f74601016c..566a246ae6 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -72,6 +72,7 @@ test/fence-repair-receipts.test.ts
test/forecast-journal.test.ts
test/frontmatter-reserved-keys-6454.test.ts
test/get-chunks-paging.test.ts
+test/grade-takes-window.test.ts
test/graduation-copy.test.ts
test/graduation-digest.test.ts
test/graduation-drain.test.ts