Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: pre-landing review fixes (facts block cap is the caller limit under alias fan-out; CLI fact-row fallbacks tested)

garrytanefd23bb2755e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 62295ee3f3..dafc92f4d6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,7 @@ identifiers and attribution are available in the pre-removal Git revision
## [0.60.555.0] - 2026-11-02
-**`gbrain query --limit N` returns N rows again, the facts block never outscores the pages, and the CLI names fact rows instead of printing `undefined` (#8411).** Since the facts block went on by default (v0.60.491.0), it also ran on queries that passed no budget, because the shipped no-budget evidence plan carries an implicit 24,000-token budget. Its 22% share let up to 40 fact rows in ahead of the pages, so `--limit 3` could print more than a dozen fact rows before the 3 pages. Each block row was scored a hair above the top page, so they all showed the same score and outranked every hit, and the human CLI printed them as `[0.84xx] undefined -- …` because fact rows have no page slug.
+**`gbrain query --limit N` without a token budget returns N rows again, a budgeted facts block takes at most N rows, the facts block never outscores the pages, and the CLI names fact rows instead of printing `undefined` (#8411).** Since the facts block went on by default (v0.60.491.0), it also ran on queries that passed no budget, because the shipped no-budget evidence plan carries an implicit 24,000-token budget. Its 22% share let up to 40 fact rows in ahead of the pages, so `--limit 3` could print more than a dozen fact rows before the 3 pages. Each block row was scored a hair above the top page, so they all showed the same score and outranked every hit, and the human CLI printed them as `[0.84xx] undefined -- …` because fact rows have no page slug.
### What changed
diff --git a/src/core/search/facts-arm.ts b/src/core/search/facts-arm.ts
index b29f242686..2c839a5555 100644
--- a/src/core/search/facts-arm.ts
+++ b/src/core/search/facts-arm.ts
@@ -237,8 +237,9 @@ async function applyFactReserve(engine: BrainEngine, query: string, results: Sea
{ depth: { keyword: 50, cosine: 50, entity: policy.maxRows }, cosineForAll: true });
const scored = scoreFactCandidates(query, candidates).filter(s => admitted(s, 'reserve')).map(s => ({ c: s.candidate }));
if (!scored.length) return results;
- const rowCap = Math.max(results.length, (await opts.rowCap?.().catch(() => 0)) ?? 0);
- const maxRows = policy.rowShare === null ? Math.min(policy.maxRows, rowCap) : Math.min(policy.maxRows, Math.max(1, Math.floor(rowCap * policy.rowShare)));
+ const callerRows = (await opts.rowCap?.().catch(() => 0)) ?? 0;
+ const rowCap = Math.max(results.length, callerRows);
+ const maxRows = policy.rowShare === null ? Math.min(policy.maxRows, callerRows || policy.maxRows) : Math.min(policy.maxRows, Math.max(1, Math.floor(rowCap * policy.rowShare)));
const pageSlugs = await readableEntityPages(engine, scored.map(x => x.c), opts.readScope).catch(() => new Set<string>());
const fromQuoted = await factsFromQuotedPages(engine, scored.map(x => Number(x.c.id)));
let left = Math.floor(opts.budget * policy.share);
diff --git a/test/query-facts-block-8411.test.ts b/test/query-facts-block-8411.test.ts
index e42e320acd..d532edac7b 100644
--- a/test/query-facts-block-8411.test.ts
+++ b/test/query-facts-block-8411.test.ts
@@ -10,6 +10,8 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { handleToolCall } from '../src/mcp/server.ts';
import { dispatchToolCall } from '../src/mcp/dispatch.ts';
import { formatResult } from '../src/cli/main.ts';
+import { applyFactsBlock } from '../src/core/search/facts-arm.ts';
+import { formatSearchRow } from '../src/cli/search-row-format.ts';
import type { SearchResult } from '../src/core/types.ts';
import { newSource, page, putPage } from './helpers/pinned-questions-fixture.ts';
@@ -77,6 +79,14 @@ describe('#8411 budgeted query: the facts block is capped by limit and never out
});
}
+ test('the block cap is the caller limit even when page rows already run past it (alias fan-out)', async () => {
+ const pages = Array.from({ length: 5 }, (_, i) => ({ page_id: i + 1, slug: `conversations/launch-${i}`, title: 't', type: 'conversation', chunk_text: 'x',
+ chunk_id: i + 1, chunk_index: 0, chunk_source: 'compiled_truth', score: 1 - i / 10, stale: false, source_id: sourceId })) as unknown as SearchResult[];
+ const rows = await applyFactsBlock(engine, Q, pages, { sourceId, remote: false, budget: 4000, tokenBudget: 4000, rowCap: async () => 1 });
+ expect(rows.filter(r => r.result_type === 'fact').length).toBe(1);
+ expect(rows.filter(r => r.result_type !== 'fact').length).toBe(5);
+ });
+
test('a budget with a large limit still delivers the whole block (the measured arm is unchanged)', async () => {
const rows = await local({ limit: 25, token_budget: 4000 });
expect(rows.filter(isFact).length).toBe(FACTS);
@@ -96,6 +106,13 @@ describe('#8411 CLI rendering', () => {
expect(out.split('\n').filter(l => /^\[\d/.test(l)).length).toBe(rows.length);
});
+ test('a fact row without a readable entity page falls back to follow_up, and to the bare id', () => {
+ const fact = { result_type: 'fact', fact_id: '7', score: 0.5, chunk_text: '[observed unknown]\nThe acme-example venue moved.' } as unknown as SearchResult;
+ expect(formatSearchRow({ ...fact, follow_up: { op: 'recall', args: { entity: 'companies/acme-example' } } } as never))
+ .toBe('[0.5000] fact:7 companies/acme-example -- [observed unknown] The acme-example venue moved.');
+ expect(formatSearchRow(fact as never)).toBe('[0.5000] fact:7 -- [observed unknown] The acme-example venue moved.');
+ });
+
test('--json: every fact row carries fact_id', async () => {
const rows = await local({ limit: 3, token_budget: 4000 });
const parsed = JSON.parse(formatResult('query', rows, { json: true }));