feat(research): BIT-3602 wire orchestrator for multi-source fact verification
ai-news-writer-ci / test (pull_request) Successful in 38s
ai-news-writer-ci / test (pull_request) Successful in 38s
Add src/research/index.js as the single entry point that composes the
existing source modules already on main (Google Fact Check, ClaimBuster,
cross-reference) plus extractClaims and aggregateVerificationResults
into one verifyArticle(article) call.
Behaviour:
- Honours researchConfig() — short-circuits with status:'disabled'
when RESEARCH_ENABLED is false (opt-in feature flag, no default
network calls).
- Returns status:'skipped' for empty articles.
- Queries the three sources in parallel via Promise.all.
- Wraps each source in safeQuery so a thrown error is contained as
status:'error', logged, and excluded from the aggregate — partial
outage of one source never blocks the publish pipeline.
- Result shape: { status, overall_confidence, sources, claims }
matching the surface aggregateVerificationResults already produces.
Tests: 5 new cases in src/research/index.test.js cover disabled,
empty article, three-source aggregation (matches aggregator weighted
contract: 0.50/0.35/0.15 → 72), partial-outage containment, and
all-skipped → status:'skipped'.
Source modules and their tests (aggregator, claims, config, sources/*)
are not touched: prior PRs already merged refined versions of those
files to main; this change only adds the missing orchestrator that
wires them together.
Paperclip-Issue: BIT-3602
Paperclip-Run: 31991727-135e-4c6b-ba11-982068f121de
This commit is contained in:
1 parent
6b870417fe
commit
7bec3ed03f
2 files changed
+168
No files matched your search
@@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
|
||||
const logger = require('../logger');
|
||||
const { researchConfig } = require('./config');
|
||||
const { extractClaims } = require('./claims');
|
||||
const { queryGoogleFactCheck } = require('./sources/google-factcheck');
|
||||
const { queryClaimBuster } = require('./sources/claimcheck');
|
||||
const { queryCrossReference } = require('./sources/cross-reference');
|
||||
const { aggregateVerificationResults } = require('./aggregator');
|
||||
|
||||
/**
|
||||
* Verify an article against multiple fact-checking sources and return a
|
||||
* single verification report aggregating per-source confidence.
|
||||
*
|
||||
* Sources query in parallel; any source that throws is recorded as
|
||||
* `status:'error'` and excluded from the aggregate, so a partial outage
|
||||
* never blocks the publish pipeline.
|
||||
*
|
||||
* @param {{ title?: string, subtitle?: string, body?: string, sources?: Array }} article
|
||||
* @param {{ enabled?: boolean, timeoutMs?: number, claimsMax?: number,
|
||||
* googleApiKey?: string, claimBusterApiKey?: string }} [overrides]
|
||||
* Optional overrides; `researchConfig()` is used when absent.
|
||||
* @returns {Promise<{
|
||||
* status: 'disabled' | 'verified' | 'unverified' | 'disputed' | 'skipped',
|
||||
* overall_confidence: number | null,
|
||||
* sources: Array<object>,
|
||||
* claims: string[]
|
||||
* }>}
|
||||
*/
|
||||
async function verifyArticle(article, overrides) {
|
||||
const cfg = overrides || researchConfig();
|
||||
|
||||
if (!cfg.enabled) {
|
||||
return { status: 'disabled', overall_confidence: null, sources: [], claims: [] };
|
||||
}
|
||||
if (!article || (!article.title && !article.body)) {
|
||||
return { status: 'skipped', overall_confidence: null, sources: [], claims: [] };
|
||||
}
|
||||
|
||||
const claims = extractClaims(article, cfg.claimsMax);
|
||||
|
||||
const queries = [
|
||||
safeQuery('google_factcheck', () => queryGoogleFactCheck(claims, cfg.googleApiKey, cfg.timeoutMs)),
|
||||
safeQuery('claimcheck', () => queryClaimBuster(claims, cfg.claimBusterApiKey, cfg.timeoutMs)),
|
||||
safeQuery('cross_reference', () => queryCrossReference(article, cfg.timeoutMs))
|
||||
];
|
||||
|
||||
const sources = await Promise.all(queries);
|
||||
const aggregate = aggregateVerificationResults(sources);
|
||||
|
||||
return {
|
||||
status: aggregate.overall_status,
|
||||
overall_confidence: aggregate.overall_confidence,
|
||||
sources,
|
||||
claims
|
||||
};
|
||||
}
|
||||
|
||||
async function safeQuery(name, fn) {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err) {
|
||||
logger.warn(`research source ${name} failed: ${err.message}`);
|
||||
return { source_name: name, status: 'error', confidence: null, error: err.message, results: [] };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { verifyArticle };
|
||||
@@ -0,0 +1,100 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
// Stub source modules before loading the orchestrator so verifyArticle
|
||||
// composes deterministic results without hitting the network.
|
||||
const googleMod = require('./sources/google-factcheck');
|
||||
const claimMod = require('./sources/claimcheck');
|
||||
const xrefMod = require('./sources/cross-reference');
|
||||
|
||||
const _origGoogle = googleMod.queryGoogleFactCheck;
|
||||
const _origClaim = claimMod.queryClaimBuster;
|
||||
const _origXref = xrefMod.queryCrossReference;
|
||||
|
||||
let _googleStub = null;
|
||||
let _claimStub = null;
|
||||
let _xrefStub = null;
|
||||
|
||||
googleMod.queryGoogleFactCheck = async (...a) => (_googleStub ? _googleStub(...a) : _origGoogle(...a));
|
||||
claimMod.queryClaimBuster = async (...a) => (_claimStub ? _claimStub(...a) : _origClaim(...a));
|
||||
xrefMod.queryCrossReference = async (...a) => (_xrefStub ? _xrefStub(...a) : _origXref(...a));
|
||||
|
||||
delete require.cache[require.resolve('./index')];
|
||||
const { verifyArticle } = require('./index');
|
||||
|
||||
function reset() {
|
||||
_googleStub = null;
|
||||
_claimStub = null;
|
||||
_xrefStub = null;
|
||||
}
|
||||
|
||||
test('verifyArticle — disabled config short-circuits without calling sources', async () => {
|
||||
reset();
|
||||
let called = false;
|
||||
_googleStub = async () => { called = true; return { source_name: 'google_factcheck', status: 'found', confidence: 90 }; };
|
||||
const r = await verifyArticle({ title: 'Anything', body: 'x'.repeat(50) },
|
||||
{ enabled: false });
|
||||
assert.equal(r.status, 'disabled');
|
||||
assert.equal(r.overall_confidence, null);
|
||||
assert.deepEqual(r.sources, []);
|
||||
assert.equal(called, false);
|
||||
});
|
||||
|
||||
test('verifyArticle — empty article returns skipped', async () => {
|
||||
reset();
|
||||
const r = await verifyArticle({}, { enabled: true, claimsMax: 3, timeoutMs: 1000 });
|
||||
assert.equal(r.status, 'skipped');
|
||||
});
|
||||
|
||||
test('verifyArticle — aggregates three found sources into a verified report', async () => {
|
||||
reset();
|
||||
_googleStub = async () => ({ source_name: 'google_factcheck', status: 'found', confidence: 80 });
|
||||
_claimStub = async () => ({ source_name: 'claimcheck', status: 'found', confidence: 70 });
|
||||
_xrefStub = async () => ({ source_name: 'cross_reference', status: 'found', confidence: 60 });
|
||||
|
||||
const r = await verifyArticle(
|
||||
{ title: 'Real article with at least twenty chars to count', body: 'Statistics showed 42% growth in Q1.' },
|
||||
{ enabled: true, claimsMax: 3, timeoutMs: 1000, googleApiKey: 'k', claimBusterApiKey: 'k' }
|
||||
);
|
||||
|
||||
assert.equal(r.status, 'verified');
|
||||
assert.equal(r.overall_confidence, 72); // weighted avg matches aggregator contract
|
||||
assert.equal(r.sources.length, 3);
|
||||
assert.ok(Array.isArray(r.claims));
|
||||
});
|
||||
|
||||
test('verifyArticle — source throw is contained as status:error and excluded from aggregate', async () => {
|
||||
reset();
|
||||
_googleStub = async () => { throw new Error('boom'); };
|
||||
_claimStub = async () => ({ source_name: 'claimcheck', status: 'skipped', confidence: null });
|
||||
_xrefStub = async () => ({ source_name: 'cross_reference', status: 'found', confidence: 75 });
|
||||
|
||||
const r = await verifyArticle(
|
||||
{ title: 'Article that survives a partial outage cleanly', body: 'Body text long enough to count.' },
|
||||
{ enabled: true, claimsMax: 3, timeoutMs: 1000, googleApiKey: 'k', claimBusterApiKey: 'k' }
|
||||
);
|
||||
|
||||
// cross_reference at 75 alone — verified
|
||||
assert.equal(r.status, 'verified');
|
||||
assert.equal(r.overall_confidence, 75);
|
||||
const google = r.sources.find((s) => s.source_name === 'google_factcheck');
|
||||
assert.equal(google.status, 'error');
|
||||
assert.equal(google.error, 'boom');
|
||||
});
|
||||
|
||||
test('verifyArticle — disabled when sources skipped (no API keys) and xref returns not_found', async () => {
|
||||
reset();
|
||||
_googleStub = async () => ({ source_name: 'google_factcheck', status: 'skipped', confidence: null });
|
||||
_claimStub = async () => ({ source_name: 'claimcheck', status: 'skipped', confidence: null });
|
||||
_xrefStub = async () => ({ source_name: 'cross_reference', status: 'not_found', confidence: null });
|
||||
|
||||
const r = await verifyArticle(
|
||||
{ title: 'Article without third-party API coverage at all', body: 'Body text.' },
|
||||
{ enabled: true, claimsMax: 3, timeoutMs: 1000 }
|
||||
);
|
||||
|
||||
assert.equal(r.status, 'skipped');
|
||||
assert.equal(r.overall_confidence, null);
|
||||
});
|
||||
Reference in new issue
Block a user