Web pages: notes on a page's words, as on Wikipedia's
A web page's text now takes the same selection bar, menu and keys as an article's: the lenses write a note anchored to the words, shown in the text, reached by Tab, opened, edited and deleted like any other. The notes are filed under the page's node (keyed by normUrl, a url target) in the same hidden container as articles' notes; an anchor's rev is the page's content hash as a number. Words are found again in whichever section holds them after a reload or an arrangement; gone, the note is listed as detached, and a page that cannot be shown lists every note kept on it. Page text goes to the model fenced as untrusted. Existing Wikipedia notes read as before. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
f8c6576ea1
commit
58bc8b5a2f
20 files changed
+972
-288
No files matched your search
+177
-3
@@ -1,10 +1,21 @@
|
||||
/** Annotations on Wikipedia articles: finding an anchor again in a later revision, and the article nodes in the library. */
|
||||
/**
|
||||
* Annotations on Wikipedia articles and web pages: finding an anchor again in a later revision (or
|
||||
* a page read again, or arranged by a model and so cut into other sections), and the article and
|
||||
* page nodes in the library, with the data kept before pages could be annotated still read as it was.
|
||||
*/
|
||||
import fc from "fast-check";
|
||||
import { parseFragment, type DefaultTreeAdapterTypes as T } from "parse5";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { annotationsOf, articleNodes, articleParent, containers, fold, inArticles, makeAnchor, reanchor, type ArticleKey, type SectionText } from "../src/core/article";
|
||||
import { CONTAINER_TITLE, annotationsOf, articleNodes, articleParent, containers, fold, inArticles, makeAnchor, pageRev, reanchor, type ArticleKey, type SectionText } from "../src/core/article";
|
||||
import { Doc, isStale, staleness, tops, type NodeId, type Op } from "../src/core/model";
|
||||
import { mergeUpdate, verifyHistory } from "../src/core/repo";
|
||||
import type { Range } from "../src/core/text";
|
||||
import { sentences, type Range } from "../src/core/text";
|
||||
import { mockLLM } from "../src/llm/mock";
|
||||
import { buildText, type DomLike } from "../src/ui/articleText";
|
||||
import { pageSectionTexts } from "../src/ui/web";
|
||||
import { PageClient, type WebArticle } from "../server/page";
|
||||
import { Arranger, DailyBudget } from "../server/pageArrange";
|
||||
import { MOCK_CHANGING_LINE, MOCK_PAGES, makeMockPageFetch } from "../server/pageMock";
|
||||
|
||||
// ---- re-anchoring ----
|
||||
|
||||
@@ -188,3 +199,166 @@ describe("article nodes", () => {
|
||||
expect(() => doc.apply([{ op: "create", parent: tops(doc.snap())[0], title: "Box", articles: true }], "Bad")).toThrow(/container/);
|
||||
});
|
||||
});
|
||||
|
||||
// ---- web pages ----
|
||||
|
||||
const PAGE: ArticleKey = { url: "https://tracing.example.org/posts/following-a-request", title: "Following one request" };
|
||||
|
||||
describe("page nodes", () => {
|
||||
it("are filed by address as normUrl compares them, beside the articles in the one container, their target the page", () => {
|
||||
const doc = library();
|
||||
const art = annotate(doc, OCTOPUS, "arms");
|
||||
const first = annotate(doc, PAGE, "the request");
|
||||
const second = annotate(doc, { url: `${PAGE.url}/#the-queue` }, "a queue");
|
||||
const snap = doc.snap();
|
||||
expect(containers(snap)).toHaveLength(1);
|
||||
expect(snap.nodes[containers(snap)[0]].title).toBe(CONTAINER_TITLE);
|
||||
const nodes = articleNodes(snap, PAGE);
|
||||
expect(nodes).toHaveLength(1);
|
||||
expect(articleNodes(snap, { url: `${PAGE.url}#x` })).toEqual(nodes);
|
||||
expect(snap.nodes[nodes[0]].ref).toEqual({ kind: "url", url: PAGE.url, title: "Following one request" });
|
||||
expect(snap.nodes[nodes[0]].title).toBe("Following one request");
|
||||
expect(annotationsOf(snap, PAGE)).toEqual([first, second]);
|
||||
expect(annotationsOf(snap, OCTOPUS)).toEqual([art]);
|
||||
// Another page, or an article titled as the page's address, is not this page.
|
||||
expect(articleNodes(snap, { url: "https://tracing.example.org/posts/another" })).toEqual([]);
|
||||
expect(articleNodes(snap, { lang: "en", title: PAGE.url })).toEqual([]);
|
||||
// A page with no title known is named by its address.
|
||||
annotate(doc, { url: "https://notes.example.org/a" }, "words");
|
||||
expect(doc.snap().nodes[articleNodes(doc.snap(), { url: "https://notes.example.org/a" })[0]].title).toBe("https://notes.example.org/a");
|
||||
// Like an article's: not one of the root's sections, nothing in it goes stale.
|
||||
expect(tops(doc.snap())).toHaveLength(1);
|
||||
expect(inArticles(snap, first) && inArticles(snap, nodes[0])).toBe(true);
|
||||
for (const id of [nodes[0], first, second]) expect(isStale(snap, id)).toBe(false);
|
||||
expect(verifyHistory(doc).mismatches).toEqual([]);
|
||||
});
|
||||
|
||||
it("the data kept before pages could be annotated reads as it did: the container titled Wikipedia keeps its articles' notes, and takes pages' too", () => {
|
||||
const doc = library();
|
||||
const root = doc.snap().root;
|
||||
// As the app wrote it then: the container, an article's node, an annotation.
|
||||
const anchor = { sec: ["Description"], q: "eight arms", pre: "has ", post: ".", rev: 1180000000 };
|
||||
const { created } = doc.apply(
|
||||
[
|
||||
{ op: "create", parent: root, kind: "section", title: "Wikipedia", body: "", articles: true },
|
||||
{ op: "create", parent: "$0" as NodeId, kind: "section", title: "Octopus", body: "", ref: { kind: "wiki", lang: "en", title: "Octopus", pageId: 1200106 } },
|
||||
{ op: "create", parent: "$1" as NodeId, kind: "note", form: "note", body: "Old.", wanchor: anchor },
|
||||
],
|
||||
"Annotate before pages",
|
||||
{ actor: "llm" },
|
||||
);
|
||||
expect(annotationsOf(doc.snap(), OCTOPUS)).toEqual([created[2]]);
|
||||
expect(articleParent(doc.snap(), OCTOPUS)).toEqual({ ops: [], parent: created[1] });
|
||||
const page = annotate(doc, PAGE, "the request");
|
||||
const snap = doc.snap();
|
||||
expect(containers(snap)).toEqual([created[0]]);
|
||||
expect(snap.nodes[created[0]].title).toBe("Wikipedia");
|
||||
expect(snap.nodes[articleNodes(snap, PAGE)[0]].parent).toBe(created[0]);
|
||||
expect(annotationsOf(snap, OCTOPUS)).toEqual([created[2]]);
|
||||
expect(annotationsOf(snap, PAGE)).toEqual([page]);
|
||||
expect(snap.nodes[created[2]].wanchor).toEqual(anchor);
|
||||
});
|
||||
|
||||
it("a page's copy is an anchor's revision as a whole number taken from its content's digest", () => {
|
||||
expect(pageRev("00ff00ff00ff1234")).toBe(0x00ff00ff00ff);
|
||||
expect(pageRev("ffffffffffffffff")).toBe(0xffffffffffff);
|
||||
expect(pageRev("")).toBe(0);
|
||||
expect(pageRev("not hex")).toBe(0);
|
||||
const doc = library();
|
||||
const { ops, parent } = articleParent(doc.snap(), PAGE);
|
||||
const wanchor = { sec: [], q: "x", pre: "", post: "", rev: pageRev("ffffffffffffffff") };
|
||||
const { created } = doc.apply([...ops, { op: "create", parent, kind: "note", body: "y", wanchor }], "Page note");
|
||||
expect(doc.snap().nodes[created[created.length - 1]].wanchor).toEqual(wanchor);
|
||||
});
|
||||
});
|
||||
|
||||
/** A parse5 node as the little of the DOM the text walk uses (as in articleText.test.ts). */
|
||||
function adapt(n: T.Node): DomLike {
|
||||
if (n.nodeName === "#text") return { nodeType: 3, data: (n as T.TextNode).value, textContent: (n as T.TextNode).value, childNodes: [] };
|
||||
if (!("tagName" in n)) return { nodeType: 8, textContent: "", childNodes: [] };
|
||||
const el = n as T.Element;
|
||||
const text = (x: T.Node): string => (x.nodeName === "#text" ? (x as T.TextNode).value : "childNodes" in x ? (x as T.Element).childNodes.map(text).join("") : "");
|
||||
return { nodeType: 1, localName: el.tagName, textContent: text(el), childNodes: el.childNodes.map(adapt), attributes: el.attrs, outerHTML: "" };
|
||||
}
|
||||
const textOfHtml = (html: string) => buildText({ nodeType: 11, textContent: "", childNodes: parseFragment(html).childNodes.map(adapt) }).text;
|
||||
|
||||
async function read(pages: PageClient, url: string): Promise<WebArticle> {
|
||||
const r = await pages.article(url);
|
||||
if (!r.ok) throw new Error(`could not read ${url}`);
|
||||
return r.value;
|
||||
}
|
||||
|
||||
/** The page's text as a whole, its sections joined as one, and where each section starts in it. */
|
||||
function whole(secs: SectionText[]) {
|
||||
let at = 0;
|
||||
const off = secs.map((s) => {
|
||||
const o = at;
|
||||
if (s.text) at += s.text.length + 2;
|
||||
return o;
|
||||
});
|
||||
return { text: secs.map((s) => s.text).filter(Boolean).join("\n\n"), off };
|
||||
}
|
||||
|
||||
describe("notes on a web page's words", () => {
|
||||
it("with their section gone (a page cut anew), are taken where their surroundings agree best, in whichever section", () => {
|
||||
const secs: SectionText[] = [
|
||||
{ path: [], text: "A lead." },
|
||||
{ path: ["Before it the tide…"], text: "Before it the tide turned. After that, nothing." },
|
||||
{ path: ["The keeper saw the…"], text: "The keeper saw the tide turned at dawn." },
|
||||
];
|
||||
const a = makeAnchor(secs[2].text, [15, 30], ["Mock part two"], 1);
|
||||
expect(reanchor(a, secs)).toEqual({ sec: 2, range: [15, 30] });
|
||||
});
|
||||
|
||||
it("are found again when a model arranges the page, which cuts it into other sections: on the same words, wherever they now are", async () => {
|
||||
const pages = new PageClient({ fetch: makeMockPageFetch(), spacingMs: 0, persist: false });
|
||||
const before = await read(pages, MOCK_PAGES.essay);
|
||||
expect(before.outline).toBe("chunks");
|
||||
const arranged = await new Arranger({ pages: () => pages, llm: () => mockLLM(), budget: new DailyBudget(null, 20) }).arrange(MOCK_PAGES.essay);
|
||||
if (!arranged.ok) throw new Error(arranged.message);
|
||||
const after = arranged.article;
|
||||
expect(after.outline).toBe("model");
|
||||
const a = pageSectionTexts(before, textOfHtml);
|
||||
const b = pageSectionTexts(after, textOfHtml);
|
||||
expect(b.ids).toEqual([null, "m1", "m2", "m3"]);
|
||||
expect(b.secs.map((s) => s.path)).not.toEqual(a.secs.map((s) => s.path));
|
||||
const wa = whole(a.secs);
|
||||
const wb = whole(b.secs);
|
||||
// The same words, cut elsewhere.
|
||||
expect(wb.text).toBe(wa.text);
|
||||
let checked = 0;
|
||||
a.secs.forEach((s, i) => {
|
||||
for (const r of sentences(s.text)) {
|
||||
const anchor = makeAnchor(s.text, r, s.path, pageRev(before.hash));
|
||||
const got = reanchor(anchor, b.secs);
|
||||
if ("detached" in got) throw new Error(`lost “${anchor.q}”`);
|
||||
expect(b.secs[got.sec].text.slice(got.range[0], got.range[1])).toBe(anchor.q);
|
||||
// Words found once in the page are found exactly where they are.
|
||||
if (wa.text.indexOf(anchor.q) === wa.text.lastIndexOf(anchor.q)) expect(wb.off[got.sec] + got.range[0]).toBe(wa.off[i] + r[0]);
|
||||
checked++;
|
||||
}
|
||||
});
|
||||
expect(checked).toBeGreaterThan(50);
|
||||
});
|
||||
|
||||
it("read again with other text: a note whose words remain is found in the parts as they are now cut; one whose words are gone is detached", async () => {
|
||||
const pages = new PageClient({ fetch: makeMockPageFetch(), spacingMs: 0, persist: false });
|
||||
const first = await read(pages, MOCK_PAGES.changing);
|
||||
expect(first.outline).toBe("chunks");
|
||||
const a = pageSectionTexts(first, textOfHtml);
|
||||
const i = a.secs.findIndex((s) => s.text.includes(MOCK_CHANGING_LINE));
|
||||
const at = a.secs[i].text.indexOf(MOCK_CHANGING_LINE);
|
||||
const gone = makeAnchor(a.secs[i].text, [at, at + MOCK_CHANGING_LINE.length], a.secs[i].path, pageRev(first.hash));
|
||||
const last = a.secs[a.secs.length - 1];
|
||||
const kept = makeAnchor(last.text, sentences(last.text).at(-1)!, last.path, pageRev(first.hash));
|
||||
const again = await pages.reload(MOCK_PAGES.changing);
|
||||
if (!again.ok) throw new Error("the page was not read again");
|
||||
const second = again.value;
|
||||
expect(pageRev(second.hash)).not.toBe(pageRev(first.hash));
|
||||
const b = pageSectionTexts(second, textOfHtml);
|
||||
expect(b.secs.map((s) => s.path)).not.toEqual(a.secs.map((s) => s.path));
|
||||
expect(reanchor(gone, b.secs)).toMatchObject({ detached: true });
|
||||
const got = reanchor(kept, b.secs);
|
||||
expect("range" in got && b.secs[got.sec].text.slice(got.range[0], got.range[1])).toBe(kept.q);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user