caelum0x ab05bb4dc6 feat: Mixture-of-Agents verifier with real composition across all engines
Replace the flat "run 17 adapters in parallel and sum" design with a
collaborative, layered blackboard MoA where the backend engines compose:
agents declare produces/consumes typed artifacts, a scheduler topologically
orders them into layers, and each layer commits its artifacts to a shared
blackboard before the next runs — so downstream agents genuinely build on
upstream findings.

Composition core (lib/moa):
- types.ts: MoaAgent contract (produces/consumes/gate/run(ctx,bb)) + typed
  artifact taxonomy (source_labels, effect_sizes, quality, relevance,
  contested, entities, mechanisms, design_priors, sufficiency, debate,
  research_brief)
- blackboard.ts: typed single-producer-per-kind shared memory
- scheduler.ts: dependency-DAG layering; parallel within a layer, artifacts
  committed between layers
- router.ts + planner.ts: deterministic gate + advisory Claude planner
  (boosts never revive a 0 gate)
- aggregate.ts: deterministic vote mix -> verdict + trust (no LLM; the moat)
- synthesize.ts: grounded Claude narrative (explains, never decides)
- 21 composing agents under lib/moa/agents/*

Real composition edges proven live: MiniCheck produces source_labels ->
MultiVerS aggregates them + Valsci produces contested -> STORM debates;
quant-extractor produces effect_sizes -> PyMARE pools them; paper-qa quality
+ Loki relevance feed MiniCheck. Sample (EMPA-REG vs DECLARE) yields a
calibrated "supported, trust 73/100" with an honest heterogeneity caveat.

New production engines from vendored research repos (MVP -> production),
wired as Layer-3 deliberation agents with papertrail_*.py + PAPERTRAIL.md:
- autoreview (eimenhmdt/autoresearcher): citation-grounded literature review
- autogather (lucereal/AutoResearcher): query-gen + coverage-gap analysis
- autoloop (karpathy/autoresearch pattern): bounded evidence-refinement loop

Platform robustness: callClaudeForJson now tolerates prose-wrapped JSON
(balanced-value extraction) with one stricter retry — fixes model
editorializing that previously broke MiniCheck and any strict-JSON engine.

API /api/moa/orchestrate + console page /console/orchestrator visualizing the
layered DAG, artifact provenance, and per-agent produce/consume.
2026-07-11 05:07:08 +03:00

110 lines
5.3 KiB
TypeScript

// PaperTrail MoA expert adapter — pyalex CITATION VELOCITY (category: sources).
//
// WHAT THIS ENGINE CONTRIBUTES: a living-evidence WEIGHTING signal — is a primary
// source's evidence base still MOVING? The pyalex specialization
// (backend/engines/pyalex/pyalex/papertrail_citation_velocity.py::citation_velocity)
// reads a work's per-year `counts_by_year` from OpenAlex and labels the trend
// (accelerating / decelerating / steady). A rising velocity means the question is
// still actively contested and a pooled verdict may still flip; a flat/falling one
// means the field has settled. That is CONTEXT/weighting, never a support/refute
// vote — so the signal, on the rare occasion this runs, is always `neutral`.
//
// WHY IT GATES ~0 IN THE ORCHESTRATE PATH (honest Mixture-of-Experts skip):
// citation_velocity is a LIVE OpenAlex HTTP GET keyed by an OpenAlex work id or DOI
// (it fetches `counts_by_year`). The stateless orchestrator provides NO network fetch
// and NO per-year velocity data on a MoaSource — `MoaSource` carries `doi`/`citations`
// /`year`, but a single lifetime `citations` count is NOT a velocity (it has no
// per-year shape and no trend). There is no way to compute velocity PURELY from the
// fields already present, so this adapter does not invent a fetch or a pool. It stays
// REGISTERED (so it fires the moment velocity is provided via the Living Evidence
// ingestion path) but returns an honest skip in the plain-claim orchestrate case.
//
// STATELESS / DETERMINISTIC / NO LLM: gate is a pure DOI-presence check; run never
// performs I/O and never calls Claude (usedClaude is always false).
import type {
Expert,
OrchestrationContext,
ExpertContribution,
MoaSource,
} from "../types";
import { skippedContribution, erroredContribution, clamp01 } from "../types";
const EXPERT_ID = "pyalex";
// A source is "velocity-addressable" only if it carries a stable identifier pyalex could
// resolve against OpenAlex (a DOI, or an OpenAlex work id "W…"). Lifetime `citations`
// alone is NOT enough — velocity needs the per-year `counts_by_year` shape a live fetch
// returns, which the stateless context does not provide.
function velocityIdentifier(source: MoaSource): string | null {
const doi = source.doi?.trim();
if (doi !== undefined && doi.length > 0) return doi;
// OpenAlex work ids sometimes arrive on the url (e.g. https://openalex.org/W123…).
const url = source.url?.trim();
if (url !== undefined && /(?:^|\/)W\d+$/i.test(url)) return url;
return null;
}
// Count of sources that at least COULD be velocity-queried (have a resolvable id).
function addressableCount(sources: readonly MoaSource[]): number {
return sources.reduce(
(n, s) => (velocityIdentifier(s) !== null ? n + 1 : n),
0
);
}
const expert: Expert = {
id: EXPERT_ID,
name: "pyalex citation-velocity monitor",
category: "sources",
description:
"Living-evidence weighting: labels whether a primary source's citation velocity is " +
"accelerating / decelerating / steady (is the question still contested?). Needs a live " +
"OpenAlex fetch keyed by DOI, so it only fires via the Living Evidence ingestion path; " +
"in the stateless orchestrate path it skips. Weights context; casts no support/refute vote.",
// Pure + deterministic, no I/O, never throws. Velocity fundamentally needs a live
// OpenAlex fetch the stateless orchestrator cannot perform, so:
// - 0 when NO source carries a resolvable DOI / OpenAlex id (never applicable), and
// - a LOW, non-zero gate when at least one source IS velocity-addressable — this is
// the honest "could run if the Living Evidence ingestion context were provided
// later" signal, kept low because run() will still skip until that data exists.
// A low gate (not 0) keeps the engine REGISTERED and lets a planner boost it once the
// ingestion path actually attaches per-year velocity data.
gate(ctx: OrchestrationContext): number {
const addressable = addressableCount(ctx.sources);
if (addressable === 0) return 0;
// Small, bounded relevance: enough to register + be boostable, low enough that a
// deterministic aggregator never leans on an expert that cannot vote statelessly.
return clamp01(0.1);
},
async run(ctx: OrchestrationContext): Promise<ExpertContribution> {
try {
const addressable = addressableCount(ctx.sources);
// Honest MoE skip. Citation velocity requires a live OpenAlex GET (per-year
// `counts_by_year`) keyed by DOI/work id — the stateless orchestrate path provides
// neither the fetch nor the per-year data on a MoaSource. We refuse to invent a
// pool or a network call, so we do not vote. Not an error.
if (addressable === 0) {
return skippedContribution(
EXPERT_ID,
"No source carries a DOI/OpenAlex id, and no per-year velocity data is present — " +
"needs OpenAlex ingestion context (run via Living Evidence)."
);
}
return skippedContribution(
EXPERT_ID,
`Found ${addressable} source(s) with a resolvable DOI/OpenAlex id, but citation ` +
"velocity needs a live OpenAlex fetch — needs OpenAlex ingestion context (run via Living Evidence)."
);
} catch (err: unknown) {
return erroredContribution(EXPERT_ID, err);
}
},
};
export default expert;