Show the edition statement on cards and in the library

The edition comes only from a record for this exact edition: the Library
of Congress record's edition statement first, then Open Library's edition
name when it is a well-formed statement. Existing books are filled in at
startup without changing anything else about them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 17:49:59 -07:00
1 parent e83774adbd
commit bb46d343a8
26 files changed
+455 -34

No files matched your search

+58 -7
View File
@@ -21,6 +21,12 @@ export interface Jobs {
* changes). Books that cannot have one get [] at once; a temporary failure leaves NULL for the next start.
*/
resumeTails(): void;
/**
* Backfill, for books made before the edition statement was collected: look up only the edition (nothing else about
* the book changes, and a hand-typed edition is never touched: only NULL rows are filled). A book with neither ISBN
* nor LCCN gets '' at once; a temporary failure leaves NULL for the next start.
*/
resumeEditions(): void;
idle(): Promise<void>;
}
@@ -57,6 +63,8 @@ export function createJobs(deps: {
// The tail backfill queues behind real lookups, and retries on its own timers.
const tailQueue: number[] = [];
const tailAttempts = new Map<number, number>();
const editionQueue: number[] = [];
const editionAttempts = new Map<number, number>();
let running: Promise<void> | null = null;
let idlers = 0;
let wake: (() => void)[] = [];
@@ -113,15 +121,22 @@ export function createJobs(deps: {
let similar = '';
const took = () => `${Date.now() - started} ms`;
try {
const res = await resolver.resolve(input, { onMeta: (m) => applyMeta(id, m), tolerateLocFailure: final, fresh: fresh.has(id), onSimilar: (d) => { similar = d; } });
const wasFresh = fresh.has(id);
const res = await resolver.resolve(input, { onMeta: (m) => applyMeta(id, m), tolerateLocFailure: final, fresh: wasFresh, onSimilar: (d) => { similar = d; } });
attempts.delete(id); fresh.delete(id);
const cur = db.getBook(id);
if (!cur) return;
if (res.meta) applyMeta(id, res.meta);
const dewey = res.dewey ?? cur.dewey ?? undefined;
const tail = res.tail !== undefined && res.tail !== null ? { hierarchyTail: res.tail } : {};
const tail: Parameters<Db['updateBook']>[1] = res.tail !== undefined && res.tail !== null ? { hierarchyTail: res.tail } : {};
// The edition: a normal lookup fills only a book not looked up yet (NULL); "Look up again" replaces it with
// what the catalogue says now, but never erases a value with "none".
if (res.edition !== undefined && res.edition !== null) {
if (cur.edition === null) tail.edition = res.edition;
else if (wasFresh && res.edition !== '') tail.edition = res.edition;
}
if (cur.manualOverride) {
emit(id, { dewey, ...(cur.hierarchyTail === null ? { hierarchyTail: [] } : {}) });
emit(id, { dewey, ...(cur.hierarchyTail === null ? { hierarchyTail: [] } : {}), ...(tail.edition !== undefined ? { edition: tail.edition } : {}) });
log(`[lookup] ${label}: kept the hand-entered call number (${took()})`);
} else if (res.callNumber) {
emit(id, {
@@ -191,22 +206,54 @@ export function createJobs(deps: {
}
}
async function backfillEdition(id: number): Promise<void> {
const book = db.getBook(id);
if (!book || book.edition !== null || book.status === 'pending') { editionAttempts.delete(id); return; }
const label = book.isbn13 ?? book.lccn ?? book.title;
// Only the edition column is ever written here.
if (!book.isbn13 && !book.lccn) { editionAttempts.delete(id); emit(id, { edition: '' }); return; }
const n = editionAttempts.get(id) ?? 0;
try {
const edition = await resolver.edition({ isbn13: book.isbn13, lccn: book.lccn });
editionAttempts.delete(id);
// The row may have been edited by hand while the catalogue was asked: only fill it if it is still NULL.
if (db.getBook(id)?.edition === null) emit(id, { edition });
log(`[edition] ${label}: ${edition || 'none'}`);
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
if (isTemporary(e) && n < delays.length) {
editionAttempts.set(id, n + 1);
const delay = delays[n]!;
log(`[edition] ${label}: ${message}; trying again in ${Math.round(delay / 1000)} s`);
const t = setTimeout(() => { waiting.delete(-1_000_000_000 - id); editionQueue.push(id); start(); notify(); }, delay);
if (idlers === 0) t.unref();
waiting.set(-1_000_000_000 - id, t);
return;
}
editionAttempts.delete(id);
log(`[edition] ${label}: left for the next start, ${message}`); // NULL stays: the next start tries again
}
}
async function drain(): Promise<void> {
while (queue.length || tailQueue.length) {
while (queue.length || tailQueue.length || editionQueue.length) {
if (queue.length) {
const id = queue.shift()!;
queued.delete(id);
try { await attempt(id); } catch { /* a failure on one book must not stop the queue */ }
} else {
} else if (tailQueue.length) {
const id = tailQueue.shift()!;
try { await backfillTail(id); } catch { /* ditto */ }
} else {
const id = editionQueue.shift()!;
try { await backfillEdition(id); } catch { /* ditto */ }
}
}
}
function start(): void {
if (running) return;
running = drain().finally(() => { running = null; if (queue.length || tailQueue.length) start(); notify(); });
running = drain().finally(() => { running = null; if (queue.length || tailQueue.length || editionQueue.length) start(); notify(); });
}
return {
@@ -227,11 +274,15 @@ export function createJobs(deps: {
for (const id of db.listTailMissingIds()) if (!tailQueue.includes(id)) tailQueue.push(id);
start();
},
resumeEditions() {
for (const id of db.listEditionMissingIds()) if (!editionQueue.includes(id)) editionQueue.push(id);
start();
},
async idle() {
idlers++;
for (const t of waiting.values()) t.ref();
try {
while (running || queue.length || tailQueue.length || waiting.size) {
while (running || queue.length || tailQueue.length || editionQueue.length || waiting.size) {
if (running) await running;
else await new Promise<void>((r) => { wake.push(r); });
}