Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(fact-check): read the passage that discusses the claim, not the article lead

nirholas36740bf1df7f
diff --git a/agents/fact-checker/src/search-sources.js b/agents/fact-checker/src/search-sources.js
index ec570e7bd..b3ceebb60 100644
--- a/agents/fact-checker/src/search-sources.js
+++ b/agents/fact-checker/src/search-sources.js
@@ -16,6 +16,30 @@ function withTimeout(promise, ms) {
]);
}
+// Resolve to `fallback` if `promise` has not settled within `ms`. Unlike
+// withTimeout this never rejects: it is for work whose result is an improvement
+// on an answer the caller already holds, so running out of time is not an error.
+function withDeadline(promise, ms, fallback) {
+ let timer;
+ return Promise.race([
+ promise.finally(() => clearTimeout(timer)),
+ new Promise((resolve) => {
+ timer = setTimeout(() => resolve(fallback), ms);
+ timer.unref?.();
+ }),
+ ]);
+}
+
+// How long the Wikipedia rung may spend widening its intros into article bodies
+// when the caller set no deadline of its own.
+const BODY_WIDEN_MS = 6_000;
+
+// Headroom left between the end of widening and the caller's own cutoff. Without
+// it the rung finishes at the exact moment searchAll stops waiting, so the race
+// is a coin flip and the intros it already had are thrown away. With under this
+// much time left there is no room to widen at all: return what is in hand.
+const WIDEN_RESERVE_MS = 500;
+
// ── Vertex-grounded Google Search (keyless, GCP service-account auth) ────────
//
// The one rung that works in production today: no Brave/Tavily/Exa/Serper key
@@ -186,15 +210,166 @@ async function searchSerper(query) {
// coverage). Wikipedia's own search API (`list=search`) does real full-text
// ranking over sentence-shaped queries and needs no API key or account, so it
// sits ahead of the DDG fallback: strictly more capable, same zero-config cost.
-async function searchWikipedia(query) {
- // generator=search + prop=extracts fetches the ranked pages AND their intro
- // text in ONE request. The old list=search call only returned the ~150-char
+// Terms carried by nearly every sentence, so their presence says nothing about
+// whether a paragraph is about the claim. Kept deliberately small: an aggressive
+// stopword list starts deleting the words that make a claim specific ("not",
+// "most", "all" are load-bearing in "You lose MOST of your body heat...").
+const PASSAGE_STOPWORDS = new Set([
+ 'and', 'are', 'but', 'for', 'from', 'had', 'has', 'have', 'its', 'that',
+ 'the', 'their', 'them', 'they', 'this', 'was', 'were', 'what', 'when', 'which',
+ 'who', 'will', 'with', 'you', 'your',
+]);
+
+// A plaintext extract is bounded before scoring so one outlier article (the
+// English "Napoleon" page is ~150 KB of plaintext) cannot dominate the CPU of a
+// budgeted fact-check request. Truncation only ever costs late-article prose,
+// which the search ranking already considers least relevant.
+const MAX_EXTRACT_CHARS = 200_000;
+
+// Sections that hold apparatus rather than assertions. Their entries match query
+// terms readily (titles repeat the subject) and support no stance at all.
+const NON_PROSE_SECTIONS = new Set([
+ 'bibliography', 'citations', 'external links', 'footnotes', 'further reading',
+ 'notes', 'references', 'see also', 'sources', 'works cited',
+]);
+
+function passageTerms(query) {
+ return new Set(
+ String(query)
+ .toLowerCase()
+ .split(/[^a-z0-9]+/)
+ .filter((t) => t.length >= 3 && !PASSAGE_STOPWORDS.has(t)),
+ );
+}
+
+/**
+ * Pick the paragraphs of a plaintext Wikipedia extract that actually discuss
+ * `query`, most relevant first, capped at `maxChars`.
+ *
+ * Why this exists: `exintro=1` returned only an article's lead section, which
+ * states what the page is ABOUT rather than the attribute a claim asserts. On
+ * 2026-09-09 the live chain answered "Napoleon Bonaparte was unusually short"
+ * with the lead paragraphs of "Napoleon", "Napoleon III" and "Napoleonic Wars",
+ * none of which mention height at all: every stance came back "neutral" and the
+ * verdict was "insufficient", while the height discussion sat in the article
+ * BODY the whole time. Ranking body paragraphs by how much of the claim's own
+ * vocabulary they carry recovers that prose.
+ *
+ * A paragraph that scores nothing is not evidence, so when no paragraph matches
+ * this falls back to the leading prose, which is exactly the old behaviour.
+ *
+ * @param {string} extract Plaintext extract (MediaWiki `explaintext` output).
+ * @param {string} query The search query the passage should be relevant to.
+ * @param {{ maxChars?: number }} [opts]
+ * @returns {string} Selected passages joined by blank lines.
+ */
+export function selectRelevantPassages(extract, query, { maxChars = 1200 } = {}) {
+ const text = String(extract || '').slice(0, MAX_EXTRACT_CHARS).trim();
+ if (!text) return '';
+
+ const terms = passageTerms(query);
+
+ // Plaintext extracts separate paragraphs with blank lines and render section
+ // titles as "== Heading ==". Headings are not prose, but they name the
+ // context a body paragraph sits in, so they are carried as a prefix instead
+ // of being emitted as passages of their own. A heading is not reliably
+ // surrounded by blank lines, so it is forced onto its own block first;
+ // without that, "== Gustatory system ==" rides inside the paragraph above it
+ // and the section prefix names the wrong part of the article.
+ const blocks = [];
+ let section = '';
+ const paragraphs = text.replace(/\n(=+[^\n=][^\n]*?=+)\n/g, '\n\n$1\n\n').split(/\n\s*\n/);
+ for (const raw of paragraphs) {
+ const block = raw.trim();
+ if (!block) continue;
+ const heading = block.match(/^=+\s*(.+?)\s*=+$/);
+ if (heading) {
+ section = heading[1];
+ continue;
+ }
+ // An apparatus section is citations, not claims: a Bibliography entry can
+ // carry every term in the query (a book titled "The Great Wall of China")
+ // while asserting nothing a stance judgment can read.
+ if (NON_PROSE_SECTIONS.has(section.trim().toLowerCase())) continue;
+ blocks.push({ text: block, section, index: blocks.length });
+ }
+ if (!blocks.length) return '';
+
+ // Term counts per paragraph, and the number of paragraphs each term appears
+ // in, so scoring can weight by how discriminative a term is WITHIN this
+ // article. Plain distinct-term coverage ties constantly (in the "Tongue"
+ // article almost every paragraph says "tongue" and "taste") and the tie then
+ // resolves to document order, which hands the win to the lead every time.
+ const docFreq = new Map();
+ for (const block of blocks) {
+ block.counts = new Map();
+ for (const word of block.text.toLowerCase().split(/[^a-z0-9]+/)) {
+ if (!terms.has(word)) continue;
+ block.counts.set(word, (block.counts.get(word) || 0) + 1);
+ }
+ for (const term of block.counts.keys()) docFreq.set(term, (docFreq.get(term) || 0) + 1);
+ }
+
+ // Textbook TF-IDF over the article's own paragraphs. A term carried by every
+ // paragraph earns almost nothing; the rare term that makes the claim specific
+ // ("map" in an article about the tongue) is what pulls the right passage up.
+ const total = blocks.length;
+ for (const block of blocks) {
+ let score = 0;
+ for (const [term, count] of block.counts) {
+ const idf = Math.log(1 + total / (docFreq.get(term) || 1));
+ score += idf * (1 + Math.log(count));
+ }
+ block.score = score;
+ block.matched = block.counts.size;
+ }
+
+ const scored = blocks.filter((b) => b.matched > 0);
+ // A single incidental term is a mention, not a discussion of the claim, so a
+ // multi-term paragraph outranks a one-term paragraph whatever its TF-IDF
+ // weight. Below that, best weight first, and the article's own order breaks
+ // exact ties so the lead stays ahead of a late aside that matches equally.
+ scored.sort(
+ (a, b) =>
+ Number(b.matched > 1) - Number(a.matched > 1) ||
+ b.score - a.score ||
+ a.index - b.index,
+ );
+ const chosen = scored.length ? scored : blocks.slice(0, 2);
+
+ const out = [];
+ let used = 0;
+ for (const block of chosen) {
+ const prefix = block.section ? `${block.section}: ` : '';
+ const piece = prefix + block.text;
+ if (used && used + piece.length > maxChars) break;
+ out.push(piece);
+ used += piece.length + 2;
+ if (used >= maxChars) break;
+ }
+ return out.join('\n\n').slice(0, maxChars).trim();
+}
+
+async function searchWikipedia(query, { deadline = 0 } = {}) {
+ // generator=search + prop=extracts fetches the ranked pages AND their text in
+ // ONE request. The old list=search call only returned the ~150-char
// search-match snippet (the bolded fragment around the keyword hit), which
// rarely contains the fact being checked — downstream stance extraction
// marked nearly every such source "neutral" and the whole chain collapsed to
- // "mixed" (the 20% benchmark run of 2026-07-08). A plain-text intro extract
- // actually states the page's core facts, which is what a stance judgment
- // needs. Same endpoint, same zero-config cost.
+ // "mixed" (the 20% benchmark run of 2026-07-08).
+ //
+ // `exintro=1` was the 2026-07-08 answer to that and overcorrected: an intro
+ // states what a page is about, so it carries the fact only when the claim is
+ // about the page's subject. Claims about an ATTRIBUTE ("Napoleon was
+ // unusually short", "the tongue has taste zones") are covered in the body,
+ // and the intro-only extract dropped exactly the prose the verdict needed.
+ //
+ // The ranked intros are still fetched in this one request, because MediaWiki
+ // refuses whole-article extracts in bulk ("exlimit was too large for a whole
+ // article extracts request, lowered to 1"), so a single no-exintro call would
+ // return text for ONE page and an empty extract for the other four. The
+ // intros are the guaranteed floor; fetchFullExtract then widens each page to
+ // its body below, and anything that fails there degrades to the intro.
const url =
'https://en.wikipedia.org/w/api.php?action=query&generator=search' +
`&gsrsearch=${encodeURIComponent(query)}&gsrlimit=5` +
@@ -210,11 +385,58 @@ async function searchWikipedia(query) {
const pages = Object.values(data?.query?.pages || {});
// generator results are unordered; `index` carries the search ranking.
pages.sort((a, b) => (a.index ?? 99) - (b.index ?? 99));
- return pages.map((p) => ({
- url: `https://en.wikipedia.org/wiki/${encodeURIComponent(String(p.title || '').replace(/ /g, '_'))}`,
- title: p.title || '',
- snippet: String(p.extract || '').trim().slice(0, 1200),
- })).filter((r) => r.snippet);
+
+ // One whole-article request per ranked page, in parallel. These are cheap
+ // CDN reads against a keyless API, and each one is independent: a rejected
+ // page simply keeps the intro it already has.
+ //
+ // Widening is strictly best-effort. The intros above are already a complete,
+ // returnable answer, so this step must never make the rung slower than the
+ // answer it already holds: it gets whatever is left of the caller's deadline
+ // and, when that runs out, every page falls back to its intro. Without that
+ // bound a single hung body fetch spent the caller's whole search budget and
+ // returned nothing, discarding intros that had arrived in milliseconds.
+ const widenMs = deadline
+ ? Math.min(BODY_WIDEN_MS, deadline - Date.now() - WIDEN_RESERVE_MS)
+ : BODY_WIDEN_MS;
+ const bodies =
+ widenMs > 0
+ ? await withDeadline(
+ Promise.allSettled(pages.map((p) => fetchFullExtract(p.title))),
+ widenMs,
+ [],
+ )
+ : [];
+
+ return pages.map((p, i) => {
+ const intro = String(p.extract || '').trim().slice(0, 1200);
+ // `bodies` is empty when widening was skipped or timed out, so every index
+ // must tolerate being absent: an intro is always a valid answer here.
+ const body = bodies[i]?.status === 'fulfilled' ? bodies[i].value : '';
+ return {
+ url: `https://en.wikipedia.org/wiki/${encodeURIComponent(String(p.title || '').replace(/ /g, '_'))}`,
+ title: p.title || '',
+ snippet: selectRelevantPassages(body, query) || intro,
+ };
+ }).filter((r) => r.snippet);
+}
+
+// Whole-article plaintext for one page. Separate from the search request because
+// MediaWiki only serves a full extract one page at a time (see searchWikipedia).
+async function fetchFullExtract(title) {
+ const url =
+ 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&explaintext=1' +
+ `&titles=${encodeURIComponent(String(title))}&format=json&origin=*`;
+ const res = await withTimeout(
+ fetch(url, { headers: { accept: 'application/json', 'user-agent': 'three.ws-fact-checker/1.0 (+https://three.ws)' } }),
+ TIMEOUT_MS,
+ );
+ if (!res.ok) {
+ throw new Error(`Wikipedia extract HTTP ${res.status}`);
+ }
+ const data = await res.json();
+ const page = Object.values(data?.query?.pages || {})[0];
+ return String(page?.extract || '');
}
// ── DuckDuckGo instant answer (fallback) ──────────────────────────────────────
@@ -281,7 +503,7 @@ function deduplicate(results) {
* @param {string} query
* @returns {Promise<Array<{url: string, title: string, snippet: string}>>}
*/
-export async function searchWeb(query) {
+export async function searchWeb(query, { deadline = 0 } = {}) {
const hasVertex = webSearchAvailable();
const hasBrave = Boolean(process.env.BRAVE_API_KEY);
const hasTavily = Boolean(process.env.TAVILY_API_KEY);
@@ -326,7 +548,7 @@ export async function searchWeb(query) {
// configured (or absent) paid providers haven't reached 3 results yet.
if (deduplicate(combined).length < 3) {
try {
- const wiki = await searchWikipedia(query);
+ const wiki = await searchWikipedia(query, { deadline });
combined.push(...wiki);
} catch (err) {
errors.push(err.message);
@@ -394,7 +616,11 @@ export async function searchAll(queries, opts = {}) {
}),
]);
};
- const settled = await Promise.allSettled(queries.map((q) => bounded(searchWeb(q))));
+ // The same budget that bounds a query is handed to the query itself, so a
+ // rung can degrade gracefully inside the deadline instead of being cut off at
+ // it and losing evidence it had already retrieved.
+ const deadline = budgetMs > 0 ? Date.now() + budgetMs : 0;
+ const settled = await Promise.allSettled(queries.map((q) => bounded(searchWeb(q, { deadline }))));
const lists = settled
.filter((o) => o.status === 'fulfilled' && Array.isArray(o.value))
.map((o) => o.value);
diff --git a/data/changelog.json b/data/changelog.json
index 944039deb..fa34f9f9d 100644
--- a/data/changelog.json
+++ b/data/changelog.json
@@ -27848,6 +27848,15 @@
],
"link": "/docs/smart-home"
},
+ {
+ "date": "2026-09-09",
+ "title": "Fact-check now reads the part of a source that is actually about your claim",
+ "summary": "When the fact-checker fell back to Wikipedia, it only ever read each article's opening summary. An opening summary says what a page is about, so it carries the answer only when your claim is about the page's subject. Ask whether Napoleon was unusually short and it read the first lines of \"Napoleon\", \"Napoleon III\" and \"Napoleonic Wars\", none of which mention height at all, so every source was scored as saying nothing and the verdict came back \"not enough evidence\" while the passage about his height sat further down the same article. The checker now reads each article in full and picks the paragraphs that actually discuss your claim, so that question returns the \"Appearance and image\" and \"Napoleon's height\" sections instead. Reference lists are skipped, since they repeat your words without asserting anything, and the deeper read is bounded by the time the check already had, so a slow source can never cost you the evidence that already arrived.",
+ "tags": [
+ "improvement"
+ ],
+ "link": "/docs/fact-check"
+ },
{
"date": "2026-09-09",
"title": "Fact-check now reads the part of a source that is actually about your claim",
diff --git a/docs/fact-check.md b/docs/fact-check.md
index b87439cfe..49868a80c 100644
--- a/docs/fact-check.md
+++ b/docs/fact-check.md
@@ -24,6 +24,8 @@ The fact-check pipeline (`runFactCheck`):
1. Generate exactly 3 search queries with an LLM.
2. Search across a source chain (Vertex-grounded Google Search, then Brave, Tavily, Exa, Serper, then keyless Wikipedia full-text and DuckDuckGo Instant Answer as always-available fallbacks) and take the top 5 unique results. Each keyed rung is skipped when its key is absent, so the chain runs whatever is configured and always reaches the keyless rungs.
+
+ The Wikipedia rung returns the paragraphs of each ranked article that discuss the claim, not the article's opening summary. A lead section says what a page is about, so it carries the fact only when the claim is about the page's subject; a claim about an attribute is covered further down. Asked whether Napoleon was unusually short, the intro-only rung returned the opening lines of "Napoleon", "Napoleon III" and "Napoleonic Wars", none of which mention height, and every stance came back `neutral`. The rung now reads each article in full and ranks its paragraphs by TF-IDF over the claim's own vocabulary, so the same query returns the "Appearance and image" and "Napoleon's height" sections instead. Citation and See-also sections are skipped, since their entries match query terms while asserting nothing. Widening the intros into full articles is best-effort and bounded by the caller's own search deadline: when there is no time for it, each source keeps the intro the ranked search already returned.
3. Run LLM stance extraction on each of the 5: an excerpt plus a stance of `supports`, `contradicts`, `partial`, or `neutral`. `partial` is a source that engages the claim and finds it true in one respect while wrong, overstated, or only conditionally true in another (right about the fact, wrong about its scale, cause, exclusivity or universality). It is deliberately narrow: hedged prose, thin coverage, or an extractor that is merely unsure is `neutral`, not `partial`.
4. Weight each source by an authority score, adjusted by strictness (`high` halves low-authority weights, `low` floors them, `medium` is the default).
5. Compute the verdict over the stance-bearing evidence only, so tangential results cannot drown a clear answer. Fewer than 2 sources, zero weight, or no source that took a stance at all is `insufficient`, and so is a lone stance-bearing source lost in otherwise-silent evidence (under 30 percent coverage): evidence that never engages the claim is absence of evidence, not disagreement. Otherwise direction is judged over stance-bearing weight alone, 70 percent or more one way is `supported` or `contradicted`, and anything else is `mixed`. `partial` weight is stance-bearing but takes neither side, so it dilutes dominance: a claim every source calls half-true reaches `mixed` on its own, without needing the sources to disagree with each other. Confidence blends dominance with coverage, so a unanimous verdict read off thin engagement scores lower than one read off broad engagement; for `mixed` the dominance term is how strongly the evidence establishes mixedness (partial weight, plus opposed stances counted as the joint evidence of a split that they are), not how lopsided the split was.
diff --git a/prompts/finish/_context/production-100-PROGRESS.md b/prompts/finish/_context/production-100-PROGRESS.md
index 036febebf..e38e7ab11 100644
--- a/prompts/finish/_context/production-100-PROGRESS.md
+++ b/prompts/finish/_context/production-100-PROGRESS.md
@@ -658,6 +658,72 @@ Left:
and `openai` still answers `429 billing_not_active`, so two of thirteen rungs are dead
weight on every single call.
+## 2026-09-09 (later pass): 04b, the starved evidence layer got the one fix that did not need the owner
+
+The session above proved the `mixed` fix works and named the blocker: with the Vertex
+grounded rung denied, 100% of sources are Wikipedia and **15 of 38 claims saw ZERO
+stance-bearing sources**. This pass attacked that starvation, which is the half of the
+problem that is fixable from here.
+
+**Re-derived the blocker independently, and it is unchanged.** `/api/web-search` answers
+`502 upstream_error`. That is diagnostic on its own: the handler returns `200 {enabled:false}`
+when `GOOGLE_CLOUD_PROJECT` is unset, so a 502 means the var IS set and the Vertex call
+itself is failing, which matches the dunning denial recorded above. Two free-lane probes of
+the live endpoint came back with Wikipedia-only sources, confirming production is still on
+the keyless tier.
+
+**Root cause of the starvation, one level down: the Wikipedia rung only ever read each
+article's LEAD section** (`exintro=1`, chosen 2026-07-08 to replace an even worse ~150-char
+search snippet). A lead says what a page is *about*, so it carries the fact only when the
+claim is about the page's subject. For a claim about an ATTRIBUTE it carries nothing. The
+live probe of "Napoleon Bonaparte was unusually short" is the clean example: the rung
+returned the opening lines of `Napoleon`, `Napoleon III`, `Napoleonic Wars` and
+`Cultural depictions of Napoleon`, **not one of which mentions height**, every stance came
+back `neutral`, and the verdict was `insufficient` at 0.3 confidence. The height prose was
+in those same articles the whole time, further down. The verdict layer was behaving
+correctly on evidence that never contained the answer.
+
+**Fixed:** `selectRelevantPassages` in `agents/fact-checker/src/search-sources.js` now reads
+each ranked article in full and returns the paragraphs carrying the claim's own vocabulary,
+scored by TF-IDF over that article's paragraphs (plain term-coverage ties constantly, and
+the tie then resolves to document order, which hands the win to the lead every time).
+Citation and See-also sections are skipped: their entries match query terms while asserting
+nothing. Measured live, keyless, on the same claim: `Napoleon` now yields
+`Appearance and image: In his youth, Bonaparte was consistently described as small and
+thin...` and `Cultural depictions of Napoleon` yields its `Napoleon's height` section, in
+1.06s for the whole `searchAll` sweep. Claim-relevant sources on that query went 2/5 to 4/5.
+
+Two things this pass did NOT do, deliberately:
+- **It did not re-measure accuracy.** No LLM lane exists on this machine now (the keys the
+ session above read off the service are unreachable: `gcloud` is back to
+ `Reauthentication failed. cannot prompt during non-interactive execution`, and the keyless
+ floor is gone, OVH 429s every call and Pollinations now answers ~6 requests then 402s
+ `KEY_BUDGET_EXHAUSTED`). So this is a measured **evidence-relevance** improvement, not a
+ measured accuracy improvement. Whether it moves the per-class table is unknown until a run
+ is possible.
+- **It did not touch search RANKING**, which is the other half and is upstream of this
+ function. "Great Wall of China single continuous wall" still ranks
+ `Great Green Wall (China)` and `Mexico-United States border wall` while never returning
+ the `Great Wall of China` article at all. No passage selector can recover from that.
+
+**One real bug caught by an existing test while doing it.** The first cut had the rung wait
+on the body fetches unconditionally, so a single hung fetch spent the caller's entire search
+budget and returned nothing, discarding intros that had arrived in milliseconds
+(`tests/fact-check-degradation.test.js` "keeps evidence from the queries that did answer in
+time" went red, correctly). Widening is now best-effort: `searchAll` threads its deadline
+down through `searchWeb` into the rung, widening gets what is left minus a 500ms reserve,
+and on expiry every source keeps the intro the ranked search already returned. That reserve
+is what stops the rung from finishing at the exact moment the caller stops waiting.
+
+Coverage: `tests/fact-check-wikipedia-passages.test.js` (8 cases, pure, no network) pins the
+selection, the apparatus-section skip, the heading split, and the fallback floor. All five
+fact-check suites pass (80 tests).
+
+Left: unchanged. The published run is still the 2026-08-10 one, and publishing is still
+gated on the GCP billing hold (OWNER-ACTIONS row 20). `04b` stays on disk. When the hold
+lifts, the finishing command is the same one recorded above, and this change should be
+re-measured in that run rather than assumed.
+
## Retire this file when the campaign is done (required)
This file is shared context rather than a single order, so it outlives the
diff --git a/tests/fact-check-wikipedia-passages.test.js b/tests/fact-check-wikipedia-passages.test.js
new file mode 100644
index 000000000..f1bdb2abb
--- /dev/null
+++ b/tests/fact-check-wikipedia-passages.test.js
@@ -0,0 +1,88 @@
+// Passage selection for the keyless Wikipedia search rung.
+//
+// The rung used to fetch `exintro=1`, so a source's text was always the article's
+// LEAD section. That carries the fact only when the claim is about the page's
+// subject. For a claim about an attribute it carries nothing: on 2026-09-09 the
+// live chain answered "Napoleon Bonaparte was unusually short" with the leads of
+// "Napoleon", "Napoleon III" and "Napoleonic Wars", none of which mention height,
+// so every stance came back "neutral" and the verdict was "insufficient" while
+// the height discussion sat in the article body untouched.
+//
+// selectRelevantPassages reads the whole article and returns the paragraphs that
+// actually carry the claim's vocabulary. These tests pin that behaviour and, just
+// as importantly, pin the floor: when nothing matches it must still return the
+// leading prose rather than nothing at all.
+import { describe, expect, it } from 'vitest';
+
+import { selectRelevantPassages } from '../agents/fact-checker/src/search-sources.js';
+
+// Shaped like MediaWiki `explaintext` output: blank-line-separated paragraphs
+// with "== Heading ==" section titles.
+const ARTICLE = [
+ 'Napoleon Bonaparte was Emperor of the French from 1804 until 1814.',
+ '== Military career ==',
+ 'He commanded the Grande Armee across a long series of European campaigns.',
+ '== Cultural depictions ==',
+ 'British cartoons depicted Napoleon as a short man, and the image of his height persists today.',
+ '== References ==',
+ 'Roberts, Andrew. Napoleon: A Life. A biography covering his height and legacy.',
+].join('\n\n');
+
+describe('selectRelevantPassages', () => {
+ it('returns the body paragraph that carries the claim, not the lead', () => {
+ const out = selectRelevantPassages(ARTICLE, 'Napoleon height short');
+ expect(out).toContain('depicted Napoleon as a short man');
+ // The lead is about him being Emperor and says nothing about height.
+ expect(out.startsWith('Napoleon Bonaparte was Emperor')).toBe(false);
+ });
+
+ it('prefixes a passage with the section that contains it', () => {
+ const out = selectRelevantPassages(ARTICLE, 'Napoleon height short');
+ expect(out).toContain('Cultural depictions: British cartoons');
+ });
+
+ it('ignores apparatus sections, which match terms but assert nothing', () => {
+ const out = selectRelevantPassages(ARTICLE, 'Napoleon height short');
+ expect(out).not.toContain('Roberts, Andrew');
+ });
+
+ it('falls back to the leading prose when no paragraph matches', () => {
+ // The old intro-only behaviour is the floor: an unmatched query must never
+ // turn a real source into an empty snippet.
+ const out = selectRelevantPassages(ARTICLE, 'photosynthesis chlorophyll');
+ expect(out).toContain('Emperor of the French');
+ });
+
+ it('returns nothing for an empty extract', () => {
+ expect(selectRelevantPassages('', 'anything')).toBe('');
+ expect(selectRelevantPassages(' \n ', 'anything')).toBe('');
+ expect(selectRelevantPassages(null, 'anything')).toBe('');
+ });
+
+ it('splits a heading onto its own block even without a blank line around it', () => {
+ const tight = 'Lead prose about the organ.\n== Taste ==\nThe tongue map claim about taste zones is a misconception.';
+ const out = selectRelevantPassages(tight, 'tongue map taste zones');
+ expect(out).toContain('Taste: The tongue map claim');
+ // The heading must not be glued into the paragraph text itself.
+ expect(out).not.toContain('== Taste ==');
+ });
+
+ it('ranks a multi-term paragraph above one that merely repeats a single term', () => {
+ const doc = [
+ 'Coffee coffee coffee coffee coffee is widely consumed.',
+ 'Moderate coffee intake shows both health benefits and health risks.',
+ ].join('\n\n');
+ const out = selectRelevantPassages(doc, 'coffee health risks benefits');
+ expect(out.startsWith('Moderate coffee intake')).toBe(true);
+ });
+
+ it('respects the character cap', () => {
+ const long = Array.from(
+ { length: 40 },
+ (_, i) => `Paragraph ${i} discusses coffee and health at some length to consume budget.`,
+ ).join('\n\n');
+ const out = selectRelevantPassages(long, 'coffee health', { maxChars: 200 });
+ expect(out.length).toBeLessThanOrEqual(200);
+ expect(out.length).toBeGreaterThan(0);
+ });
+});