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.
109 lines
4.1 KiB
TypeScript
109 lines
4.1 KiB
TypeScript
// PaperTrail MoA — the optional Claude PLANNER. Reads the claim and the roster of
|
|
// experts and proposes a small BOOST per expert (which the router applies but which can
|
|
// never revive a gate of 0). This is the only place Claude influences routing, and it is
|
|
// strictly advisory: it re-weights, it never decides the verdict and never fabricates
|
|
// relevance. If the planner is disabled or fails, routing falls back to pure deterministic
|
|
// gating — the system is fully functional without it.
|
|
|
|
import { z } from "zod";
|
|
import type { MoaAgent, OrchestrationContext } from "./types";
|
|
import type { PlannerBoosts } from "./router";
|
|
|
|
// The planner may nudge an expert up by at most this much; kept small so the deterministic
|
|
// gate remains the dominant signal.
|
|
const PLANNER_MAX_BOOST = 0.3;
|
|
|
|
const PlannerOutputSchema = z.object({
|
|
boosts: z
|
|
.array(
|
|
z.object({
|
|
expertId: z.string(),
|
|
// 0..1 from the model; we rescale into the allowed additive band below.
|
|
emphasis: z.number().min(0).max(1),
|
|
reason: z.string().max(240).default(""),
|
|
})
|
|
)
|
|
.default([]),
|
|
});
|
|
|
|
export interface PlannerResult {
|
|
boosts: PlannerBoosts;
|
|
rationale: Array<{ expertId: string; emphasis: number; reason: string }>;
|
|
usedClaude: boolean;
|
|
}
|
|
|
|
// Injectable Claude caller so this module is testable offline and imports the SDK lazily.
|
|
export type PlannerClaudeCaller = <T>(args: {
|
|
system: string;
|
|
user: string;
|
|
schema: { parse: (v: unknown) => T };
|
|
maxTokens?: number;
|
|
}) => Promise<T>;
|
|
|
|
const PLANNER_SYSTEM =
|
|
"You are the routing planner for PaperTrail's Mixture-of-Agents evidence verifier. You " +
|
|
"are given a CLAIM and a roster of expert engines (id, name, what each contributes). Decide " +
|
|
"which experts are MOST worth emphasizing for THIS claim and return an emphasis in [0,1] per " +
|
|
"expert you want to nudge up (omit experts you have no opinion on). You do NOT decide the " +
|
|
"verdict, you do NOT rank sources, and your emphasis can only RAISE an expert the system has " +
|
|
"already judged relevant — it can never force an irrelevant expert to run. Prefer emphasizing " +
|
|
"quantitative verification (effect-size meta-analysis, entailment, cross-source aggregation) " +
|
|
"for efficacy/safety magnitude claims. Return ONLY JSON: " +
|
|
'{"boosts":[{"expertId":string,"emphasis":number,"reason":string}]}.';
|
|
|
|
function buildPlannerUser(ctx: OrchestrationContext, agents: readonly MoaAgent[]): string {
|
|
return JSON.stringify({
|
|
claim: ctx.claim,
|
|
sourceCount: ctx.sources.length,
|
|
experts: agents.map((e) => ({
|
|
id: e.id,
|
|
name: e.name,
|
|
category: e.category,
|
|
contributes: e.description,
|
|
})),
|
|
});
|
|
}
|
|
|
|
const lazyClaude: PlannerClaudeCaller = async (args) => {
|
|
const { callClaudeForJson } = await import("../claude");
|
|
return callClaudeForJson(args);
|
|
};
|
|
|
|
/**
|
|
* Ask the Claude planner for advisory routing boosts. Never throws: on any failure it
|
|
* returns empty boosts (usedClaude:false) so routing degrades to pure deterministic gating.
|
|
* The returned boosts are keyed by expertId and already rescaled into the router's band.
|
|
*/
|
|
export async function planBoosts(
|
|
ctx: OrchestrationContext,
|
|
agents: readonly MoaAgent[],
|
|
caller: PlannerClaudeCaller = lazyClaude
|
|
): Promise<PlannerResult> {
|
|
if (!ctx.options.llm || agents.length === 0) {
|
|
return { boosts: {}, rationale: [], usedClaude: false };
|
|
}
|
|
|
|
const validIds = new Set(agents.map((e) => e.id));
|
|
try {
|
|
const raw = await caller({
|
|
system: PLANNER_SYSTEM,
|
|
user: buildPlannerUser(ctx, agents),
|
|
schema: PlannerOutputSchema,
|
|
maxTokens: 700,
|
|
});
|
|
|
|
const boosts: Record<string, number> = {};
|
|
const rationale: PlannerResult["rationale"] = [];
|
|
for (const b of raw.boosts) {
|
|
// Ignore ids the model invented; only emphasize real experts.
|
|
if (!validIds.has(b.expertId)) continue;
|
|
const boost = b.emphasis * PLANNER_MAX_BOOST;
|
|
boosts[b.expertId] = boost;
|
|
rationale.push({ expertId: b.expertId, emphasis: b.emphasis, reason: b.reason });
|
|
}
|
|
return { boosts, rationale, usedClaude: true };
|
|
} catch {
|
|
return { boosts: {}, rationale: [], usedClaude: false };
|
|
}
|
|
}
|