Article nodes and annotation anchors in the library

A node per annotated Wikipedia article, created with its first annotation
under one container: a section of the library root flagged articles. It is
not a document (sections() and documents() skip it, it has no slug), its
nodes never go stale, the link scanner, the vector index, the title index
and the backlinks index leave it out, and a prompt about an annotation
carries its article's node rather than the whole container.

An annotation is an ordinary note under the article's node whose anchor is
a WikiAnchor (headings, exact words, context either side, revision) instead
of CRDT cursors. reanchor() finds it again in a later revision: its own
section, then anywhere, exactly, then tolerantly, never inside other words;
otherwise it is detached. Two replicas annotating a new article at once keep
both nodes; readers take annotations from all of them and write under the
first, so they read as one. The new fields hash only when set, so old
commits still verify. Property tests for re-anchoring, a convergence test,
and the tasks that write a note from an article's lead and section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-11-15 21:53:00 -05:00
1 parent 02e044af35
commit f63fc10dc0
8 files changed
+502 -18

No files matched your search

+31 -1
View File
@@ -50,7 +50,9 @@ Web research (only in a run where you have web search and web fetch tools):
*/
export function renderContext(snap: Snap, focus: NodeId, opts: { ids?: boolean; budget?: number } = {}): string {
const budget = opts.budget ?? 160_000;
const top = docOf(snap, focus);
let top = docOf(snap, focus);
// An annotation on an article is read in its article's node, not the whole Wikipedia container.
if (top && snap.nodes[top].articles) top = [focus, ...ancestors(snap, focus)].find((x) => snap.nodes[x].parent === top) ?? top;
if (!top) {
const lines = documents(snap).map((d) => `- ${opts.ids ? `[${d}] ` : ""}${snap.nodes[d].title || "(untitled)"}`);
return `<library>\n${lines.join("\n")}\n</library>`;
@@ -135,6 +137,34 @@ export function expandPrompt(snap: Snap, id: NodeId, range: [number, number], fo
].filter(Boolean).join("\n\n");
}
/** What a note on words of a Wikipedia article is written from: the article's lead, the section the words are in (trimmed), the words, and the reader's own sentence when they came from one. */
export interface ArticleContext {
title: string;
lead: string;
/** The headings down to the section, or null when the words are in the lead. */
heading: string | null;
section: string;
selection: string;
/** The sentence of the reader's own text the article was opened from, if any. */
from?: string;
}
/** The article as a prompt's cached context: its title and lead. */
export const articleContext = (c: ArticleContext) => `<wikipedia_article title="${c.title}">\n${c.lead}\n</wikipedia_article>`;
/** Ask for a note on a selection inside a Wikipedia article. */
export function articlePrompt(c: ArticleContext, form: Form, lens: string, question?: string): string {
return [
`The reader is reading the Wikipedia article “${c.title}”, whose lead is given above${c.heading ? `, in its section “${c.heading}”` : ""}.`,
c.heading ? `The passage around the selection:\n<section>\n${c.section}\n</section>` : "",
`They selected:\n<selection>\n${c.selection}\n</selection>`,
c.from ? `They came to this article from their own notes, from this sentence:\n<their_text>\n${plain(c.from)}\n</their_text>` : "",
question ? `They asked about the selection: ${question}` : (LENS[lens] ?? LENS.elaborate),
SIZE[form],
"The note sits beside Wikipedia's text as the reader's own: do not restate the article, and say so plainly where the article and what you know disagree or where you are unsure.",
].filter(Boolean).join("\n\n");
}
/** Ask for subsections of a node that has none. */
export function deepenPrompt(snap: Snap, id: NodeId, nearby?: Nearby[]): string {
const n = snap.nodes[id];
+42 -1
View File
@@ -8,8 +8,9 @@ import { nodeRef, resolveNode as resolveTargetPath } from "../core/links";
import { mergeBranch, resolveConflict, type MergeReport } from "../core/repo";
import { clip, digest, type Form } from "../core/text";
import { stripMarks, type Mark } from "../core/marks";
import { articleParent, type ArticleKey, type WikiAnchor } from "../core/article";
import {
AGENT_SYSTEM, SYSTEM, type Grounding, type Nearby, cascadePrompt, contradictionPrompt, deepenPrompt, expandPrompt, pathOf, regenDownPrompt,
AGENT_SYSTEM, SYSTEM, type ArticleContext, type Grounding, type Nearby, articleContext, articlePrompt, cascadePrompt, contradictionPrompt, deepenPrompt, expandPrompt, pathOf, regenDownPrompt,
regenUpPrompt, renderContext, resolvePrompt, writePrompt,
} from "./prompts";
import type { Block, CascadeDecision, Contradictions, LLM, Resolutions, ToolUseBlock, TurnReq } from "./types";
@@ -98,6 +99,46 @@ export function commitExpansion(doc: Doc, r: ExpansionRequest, text: string, onM
return report(created[0]);
}
/** A note on words of a Wikipedia article: where it attaches, and what it is written from. */
export interface ArticleNoteRequest {
article: ArticleKey;
anchor: WikiAnchor;
form: Form;
lens: string;
question?: string;
context: ArticleContext;
}
/** Stream a note on words of an article. Does not touch the library. */
export function streamArticleNote(llm: LLM, r: ArticleNoteRequest, onText: OnText = noop, signal?: AbortSignal): Promise<string> {
return llm.stream(
{
task: "expand",
system: SYSTEM,
context: articleContext(r.context),
prompt: articlePrompt(r.context, r.form, r.lens, r.question),
vars: { quote: r.anchor.q, form: r.form, lens: r.question ?? r.lens },
effort: "low",
maxTokens: r.form === "passage" ? 2000 : 800,
},
onText,
signal,
);
}
/** Keep a note on an article: under the article's node, made (with its container) on the first one. Link marks are dropped: nothing in an article's notes is linked. */
export function commitArticleNote(doc: Doc, r: ArticleNoteRequest, text: string): NodeId {
text = stripMarks(text.trim()).text;
if (r.form === "gloss") text = text.replace(/[.\s]+$/, "");
const { ops, parent } = articleParent(doc.snap(), r.article);
const { created } = doc.apply(
[...ops, { op: "create", parent, kind: "note", wanchor: r.anchor, form: r.form, lens: r.question ?? r.lens, body: text }],
`Expand “${clip(r.anchor.q, 40)}” in “${clip(r.context.title, 40)}”`,
{ actor: "llm", tag: "expand" },
);
return created[created.length - 1];
}
export function parseSections(text: string): { title: string; body: string }[] {
const out: { title: string; body: string }[] = [];
for (const chunk of text.split(/^##\s+/m).slice(1)) {