Files
explicata/server/links/review.ts
T
thalladaandClaude Opus 5.5 46b48d3497 A model reviewer for found links, with every determination kept
server/links/review.ts asks the configured model to judge the scanner's candidates
as a careful Wikipedia editor would: up to 60 a call, grouped by document and
section, each with its sentence, its section's path, the target's title and
description (and first sentence when cached) and, for a library target, how it
stands to the section. No tools; structured verdicts with a one-line reason. The
prompt names the scanner's known blind spots.

The pending store keeps verdicts, a person's decisions and when an approved link
was handed out, keyed by phrase and target, so a candidate found again is never
judged twice and a rejected phrase is not looked for again. Its file keeps the
old shape; old entries read as pending.

The scan route reviews what it found when asked (at most 2 calls) and hands the
approved links back to be made; jobs review as they go within IE_REVIEW_CALLS
(default 50) calls, with the count in their state. No model, a failed call or
the cap leaves the rest pending, with the reason. The mock reviewer links names
and terms, not single lower-case words, and fails on 'mockfail'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
2026-11-16 16:39:15 -05:00

250 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The link reviewer: the configured model judges what the scanner proposed, as a careful
* Wikipedia editor would, and its verdicts (link or not, with a one-line reason) are kept in the
* pending store. It is given no tools: each call is one structured reply of verdicts.
*
* Cost. Proposals are reviewed many to a call (BATCH), grouped by document and section, each with
* just what it takes to judge it: its sentence, the section's title and path, the target's title,
* description and (when the cache has it) first sentence, and, for a library target, how it stands
* to the section. Anything judged before is never sent again. Each caller gives a budget of calls;
* when it is spent, or a call fails, or there is no model, what is left stays pending for a person,
* and the report says why. Only phrases and titles ever go to Wikipedia; the text goes only to the
* model, as it does when the model writes.
*/
import { ownerSection } from "../../src/core/links";
import { ancestors, docOf, documents, label, notesOf, sections, type NodeId, type Snap } from "../../src/core/model";
import type { Ref } from "../../src/core/ref";
import { clip, plain, sentences } from "../../src/core/text";
import type { LinkVerdicts, LLM } from "../../src/llm/types";
import type { WikiClient } from "../wiki";
import type { PendingStore } from "./pending";
import type { Suggestion } from "./scan";
/** Proposals per call. */
export const BATCH = 60;
/** Calls a background job may make, unless IE_REVIEW_CALLS says otherwise. */
export const JOB_CALLS = 50;
/** Calls the scan after one edit may make. */
export const SCAN_CALLS = 2;
/** Calls made and calls allowed, shared by every review in one job. */
export interface Budget {
calls: number;
max: number;
}
export interface ReviewReport {
approved: number;
rejected: number;
calls: number;
/** Proposals left unreviewed. */
left: number;
/** Why some were left: no model, the budget spent, a failed call. */
fallback?: string;
}
export const REVIEW_SYSTEM = `You review proposed links in a personal library of documents. A scanner has proposed linking phrases in the text to Wikipedia articles or to other sections of the library. Decide, for each proposal, whether a reader of this document would be glad of the link, as a careful Wikipedia editor applying the Manual of Style on linking would. A link is worth making only when it helps; when unsure, do not link. Expect to reject most proposals.
Reject a proposal when:
- the phrase is an everyday word, or a thing so famous that every reader knows it (a country, a major city, a common animal or material, an ordinary idea such as energy, water or the brain, a household name mentioned in passing);
- the target is a different sense of the phrase than the text means, or a different person, place or thing with the same name;
- the phrase is a fragment of a longer name or term in the text (a surname out of a full name, one word of a technical term), or the target is about the fragment alone;
- the same thing is already linked in this section, or earlier in the document (listed as "Already linked"), or by another proposal before it in this list: link a thing once, at its first mention that matters;
- a library target is this section's own subsection, parent or sibling (its relation is given): the outline already joins them;
- a library target matches the phrase only by its title, and the text does not mean what that section is about;
- the phrase is inside a verbatim quotation of the request or prompt the document was written from (instructions addressed to an assistant): such text stays as it was written;
- the target is a list, a disambiguation page, a name page, or an article about a film, album, band, company or other namesake.
Approve a proposal when the phrase names a specific concept, result, work, person, organism, place or event that the text means and that a curious reader may well want to look up: a technical term, a named theorem or process, a figure the passage discusses, or a library section elsewhere that treats the phrase's own subject in more depth.
The scanner's tier ("sure" or "unsure") is a weak hint only. Give a verdict for every proposal, by its id, each with a reason of at most twelve words that a person can read in a list.`;
/**
* What a reviewer needs about one suggestion, besides the suggestion: where it is and what is
* around it. `given` is the node's text and title as a page sent them, which may be newer than the
* server's copy (or the node may not have reached the server yet).
*/
export function describe(snap: Snap, s: Suggestion, given?: Given) {
const n = snap.nodes[s.node];
const body = given?.node === s.node && given.text !== undefined ? given.text : (n?.body ?? "");
const d = n ? docOf(snap, s.node) : (given?.doc ?? null);
const sent = sentences(body).find(([a, b]) => s.range[0] >= a && s.range[0] < b) ?? [0, body.length];
const path: string[] = [];
for (let x: NodeId | null = s.node; x && x !== d && snap.nodes[x]; x = snap.nodes[x].parent) path.unshift(label(snap, x));
if (!n) path.push(given?.title || "a new section");
return {
...s,
nodeTitle: n ? label(snap, s.node) : given?.title || "a new section",
document: d ? snap.nodes[d].title : null,
docSlug: d ? snap.nodes[d].slug ?? null : null,
path: path.join(" › "),
sentence: clip(plain(body.slice(sent[0], sent[1])).trim(), 400),
};
}
/** A node as the page that asked for its scan sent it. */
export interface Given {
node: NodeId;
text?: string;
title?: string;
doc?: NodeId | null;
}
/** How a library target stands to the section a phrase is in. */
function relation(snap: Snap, from: NodeId, target: NodeId): string {
if (!snap.nodes[from]) return "elsewhere in the library";
const sec = ownerSection(snap, from);
const t = snap.nodes[target];
if (!t) return "a section that is gone";
if (t.parent === sec) return "a subsection of this section";
if (snap.nodes[sec]?.parent === target) return "this section's parent";
if (ancestors(snap, sec).includes(target)) return "an ancestor of this section";
if (t.parent && t.parent === snap.nodes[sec]?.parent) return "a sibling of this section";
const d = docOf(snap, target);
if (d && d === docOf(snap, sec)) return "elsewhere in this document";
return d ? `in another document, “${snap.nodes[d].title}”` : "elsewhere in the library";
}
const firstSentence = (text: string) => {
const t = plain(text).trim();
const [r] = sentences(t);
return clip(r ? t.slice(r[0], r[1]).trim() : t, 220);
};
/** The titles a node's links go to. */
const linkedTitles = (snap: Snap, id: NodeId): string[] =>
notesOf(snap, id).flatMap((c) => {
const r = snap.nodes[c].ref;
return r ? [targetTitle(snap, r)] : [];
});
const targetTitle = (snap: Snap, r: Ref) => (r.kind === "wiki" ? r.title : r.kind === "url" ? (r.title ?? r.url) : r.id && snap.nodes[r.id] ? label(snap, r.id) : (r.path ?? []).join(" › ") || "a note");
/** Everything already linked in a document, in reading order, each title once. */
function linkedInDoc(snap: Snap, doc: NodeId): string[] {
const out = new Set<string>();
const visit = (id: NodeId) => {
for (const t of linkedTitles(snap, id)) out.add(t);
for (const c of sections(snap, id)) visit(c);
};
visit(doc);
return [...out];
}
export class Reviewer {
constructor(
private o: {
llm: () => LLM | null;
wiki: () => WikiClient | null;
pending: PendingStore;
snap: () => Snap;
},
) {}
/** Whether there is a model to ask. */
get available() {
return !!this.o.llm();
}
/**
* Review these suggestions (already known to be undecided), in batches, within the budget.
* Verdicts go into the pending store as they come. Throws only if the signal aborts.
*/
async review(list: Suggestion[], budget: Budget, signal?: AbortSignal, given?: Given): Promise<ReviewReport> {
const report: ReviewReport = { approved: 0, rejected: 0, calls: 0, left: list.length };
if (!list.length) return report;
const llm = this.o.llm();
if (!llm) return { ...report, fallback: "no model is configured" };
// By document, then in reading order, so a call sees a document's proposals together.
const snap = this.o.snap();
const order = new Map<NodeId, number>();
let i = 0;
for (const d of documents(snap)) for (const id of [d, ...descendantsInOrder(snap, d)]) order.set(id, i++);
const sorted = [...list].sort((a, b) => (order.get(a.node) ?? 1e9) - (order.get(b.node) ?? 1e9) || a.range[0] - b.range[0]);
for (let at = 0; at < sorted.length; at += BATCH) {
if (budget.calls >= budget.max) return { ...report, fallback: `the reviewer's limit of ${budget.max} model call${budget.max === 1 ? "" : "s"} was reached` };
const batch = sorted.slice(at, at + BATCH);
const req = await this.request(batch, given);
budget.calls++;
report.calls++;
let reply: LinkVerdicts;
try {
reply = await llm.json<LinkVerdicts>(req.json, signal);
} catch (e) {
if (signal?.aborted) throw e;
return { ...report, fallback: `the reviewer failed: ${clip((e as Error)?.message ?? String(e), 200)}` };
}
signal?.throwIfAborted();
const now = Date.now();
for (const v of Array.isArray(reply?.verdicts) ? reply.verdicts : []) {
const s = req.ids.get(String(v.id));
if (!s || typeof v.link !== "boolean") continue;
req.ids.delete(String(v.id));
this.o.pending.review(s.id, { link: v.link, reason: clip(String(v.reason ?? "").trim(), 160), at: now });
if (v.link) report.approved++;
else report.rejected++;
report.left--;
}
}
return report;
}
/** The call for one batch: the document and section headings, and a line per proposal. */
private async request(batch: Suggestion[], given?: Given) {
const snap = this.o.snap();
const wiki = this.o.wiki();
const ids = new Map<string, Suggestion>();
const lines: string[] = [];
const proposals: { id: string; tier: string; quote: string; sentence: string }[] = [];
let doc: NodeId | null | undefined;
let node: NodeId | undefined;
for (const [k, s] of batch.entries()) {
const id = `p${k + 1}`;
ids.set(id, s);
const where = describe(snap, s, given);
const d = snap.nodes[s.node] ? docOf(snap, s.node) : (given?.doc ?? null);
if (d !== doc) {
doc = d;
node = undefined;
const already = d ? linkedInDoc(snap, d) : [];
lines.push("", `# Document: ${d ? snap.nodes[d].title : "(none)"}`, `Already linked in this document: ${already.length ? already.slice(0, 60).join("; ") : "nothing"}`);
}
if (s.node !== node) {
node = s.node;
const here = snap.nodes[s.node] ? linkedTitles(snap, s.node) : [];
lines.push("", `## Section: ${where.path || where.nodeTitle}`, `Already linked in this section: ${here.length ? here.join("; ") : "nothing"}`);
}
let target: string;
if (s.target.kind === "node" && s.target.id) {
const t = snap.nodes[s.target.id];
target = `library section “${t ? label(snap, s.target.id) : s.title}”, ${relation(snap, s.node, s.target.id)}${t?.body.trim() ? `. It begins: ${firstSentence(t.body)}` : ""}`;
} else if (s.target.kind === "wiki") {
const sum = wiki ? await wiki.summary(s.target.title, { cacheOnly: true }) : null;
const first = sum?.ok ? firstSentence(sum.value.extract) : "";
target = `Wikipedia article “${s.target.title}”${s.description ? ` (${s.description})` : ""}${first ? `. It begins: ${first}` : ""}`;
} else target = `web page ${s.title}`;
lines.push(`- ${id}: “${s.quote}” → ${target} [scanner: ${s.tier === "auto" ? "sure" : "unsure"}]`, ` in: “${where.sentence}”`);
proposals.push({ id, tier: s.tier, quote: s.quote, sentence: where.sentence });
}
const prompt = `Proposals (${batch.length}):\n${lines.join("\n").trim()}\n\nGive a verdict for each of the ${batch.length} proposals.`;
return {
ids,
json: { task: "review-links", schema: "review" as const, system: REVIEW_SYSTEM, context: "Proposed links, grouped by document and section.", prompt, vars: { proposals: JSON.stringify(proposals) }, effort: "low" as const, maxTokens: 8000 },
};
}
}
/** A node's sections and notes, depth first, in reading order. */
function descendantsInOrder(snap: Snap, id: NodeId): NodeId[] {
const out: NodeId[] = [];
const visit = (x: NodeId) => {
for (const c of snap.nodes[x].children) {
out.push(c);
visit(c);
}
};
visit(id);
return out;
}