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
This commit is contained in:
1 parent
428f7290ee
commit
46b48d3497
10 files changed
+889
-93
No files matched your search
+49
-7
@@ -2,16 +2,19 @@
|
||||
* Scanning a document or the whole library in the background: one job at a time, a node at a
|
||||
* time, at bulk priority (a person's own requests to Wikipedia go first), with progress announced
|
||||
* and a cancel. Lookups are gathered across the next few nodes and asked together, 50 phrases a
|
||||
* request, which is most of what keeps a bulk pass affordable. A job only fills the pending list;
|
||||
* applying is up to whoever reviews it (the browser applies the auto tier itself, so `u` can take
|
||||
* it back; a reviewing agent uses the accept route).
|
||||
* request, which is most of what keeps a bulk pass affordable. A job started with `review` also has
|
||||
* the model reviewer judge what it finds as it goes (review.ts: many proposals a call, within a
|
||||
* budget of calls for the job). A job only fills the pending list and records verdicts; making the
|
||||
* links is up to a page (the browser makes the approved ones itself, as one commit by "reviewer",
|
||||
* so `u` can take them back; a reviewing agent uses the accept route).
|
||||
*/
|
||||
import type { NodeId, Snap } from "../../src/core/model";
|
||||
import type { WikiClient } from "../wiki";
|
||||
import { extract } from "./extract";
|
||||
import type { NodeIndex } from "./nodeIndex";
|
||||
import type { PendingStore } from "./pending";
|
||||
import type { Linker } from "./scan";
|
||||
import { BATCH, JOB_CALLS, type Reviewer } from "./review";
|
||||
import type { Linker, Suggestion } from "./scan";
|
||||
|
||||
export interface JobState {
|
||||
id: string;
|
||||
@@ -32,6 +35,8 @@ export interface JobState {
|
||||
/** The client is spacing requests for want of a contact (IE_WIKI_CONTACT). */
|
||||
slow: boolean;
|
||||
message?: string;
|
||||
/** The reviewer's part, for a job started with review: verdicts so far, calls made of those allowed, and why it stopped early if it did. */
|
||||
review?: { approved: number; rejected: number; left: number; calls: number; max: number; fallback?: string };
|
||||
}
|
||||
|
||||
/** Nodes whose lookups are asked together. */
|
||||
@@ -49,11 +54,14 @@ export class Jobs {
|
||||
index: NodeIndex;
|
||||
pending: PendingStore;
|
||||
announce: (state: JobState) => void;
|
||||
reviewer?: Reviewer;
|
||||
/** Calls a job's review may make (IE_REVIEW_CALLS). */
|
||||
reviewCalls?: number;
|
||||
},
|
||||
) {}
|
||||
|
||||
/** Start a job over these nodes (in reading order). A running job is cancelled first. */
|
||||
start(scope: "doc" | "library", nodes: NodeId[], title: string, doc?: NodeId): JobState {
|
||||
start(scope: "doc" | "library", nodes: NodeId[], title: string, doc?: NodeId, review = false): JobState {
|
||||
this.cancel();
|
||||
const wiki = this.o.wiki();
|
||||
const spacing = wiki?.spacingMs ?? 0;
|
||||
@@ -71,6 +79,7 @@ export class Jobs {
|
||||
startedAt: Date.now(),
|
||||
etaSec: null,
|
||||
slow: spacing > 2000,
|
||||
...(review && this.o.reviewer ? { review: { approved: 0, rejected: 0, left: 0, calls: 0, max: this.o.reviewCalls ?? JOB_CALLS } } : {}),
|
||||
};
|
||||
// First estimate: the uncached phrases in batches of 50, plus about one sense or search for every other node.
|
||||
const snap = this.o.snap();
|
||||
@@ -98,6 +107,32 @@ export class Jobs {
|
||||
const spacing = wiki?.spacingMs ?? 0;
|
||||
const r0 = wiki?.requests ?? 0;
|
||||
let lastAnnounce = 0;
|
||||
// What waits for the reviewer: gathered across chunks, sent when a call's worth is ready.
|
||||
let queue: Suggestion[] = [];
|
||||
const review = async (all: boolean) => {
|
||||
const r = state.review;
|
||||
if (!r || !this.o.reviewer) return;
|
||||
if (r.fallback) {
|
||||
r.left += queue.length;
|
||||
queue = [];
|
||||
return;
|
||||
}
|
||||
while (queue.length >= BATCH || (all && queue.length)) {
|
||||
const batch = queue.splice(0, BATCH);
|
||||
const budget = { calls: r.calls, max: r.max };
|
||||
const rep = await this.o.reviewer.review(batch, budget, signal);
|
||||
r.calls = budget.calls;
|
||||
r.approved += rep.approved;
|
||||
r.rejected += rep.rejected;
|
||||
r.left += rep.left;
|
||||
if (rep.fallback) {
|
||||
r.fallback = rep.fallback;
|
||||
r.left += queue.length;
|
||||
queue = [];
|
||||
}
|
||||
this.o.announce(state);
|
||||
}
|
||||
};
|
||||
try {
|
||||
await this.o.index.refresh();
|
||||
for (let i = 0; i < nodes.length; i += CHUNK) {
|
||||
@@ -110,11 +145,15 @@ export class Jobs {
|
||||
}
|
||||
for (const id of chunk) {
|
||||
if (signal.aborted) return;
|
||||
if (this.o.snap().nodes[id]) {
|
||||
const r = await this.o.linker().scan({ node: id, priority: "bulk" }, signal);
|
||||
const n = this.o.snap().nodes[id];
|
||||
if (n) {
|
||||
// Declined phrases and those the reviewer rejected are not looked for again.
|
||||
const rejected = [...(n.nolink ?? []), ...this.o.pending.rejectedKeys(id)];
|
||||
const r = await this.o.linker().scan({ node: id, priority: "bulk", rejected }, signal);
|
||||
if (signal.aborted) return;
|
||||
this.o.pending.put(id, r.suggestions);
|
||||
for (const s of r.suggestions) state[s.tier]++;
|
||||
if (state.review) queue.push(...this.o.pending.undecided(this.o.snap(), [id]));
|
||||
}
|
||||
state.done++;
|
||||
state.requests = (wiki?.requests ?? 0) - r0;
|
||||
@@ -129,7 +168,10 @@ export class Jobs {
|
||||
this.o.announce(state);
|
||||
}
|
||||
}
|
||||
await review(false);
|
||||
}
|
||||
await review(true);
|
||||
if (signal.aborted) return;
|
||||
state.state = "done";
|
||||
state.etaSec = 0;
|
||||
} catch (e) {
|
||||
|
||||
Reference in new issue
Block a user