Print the subject hierarchy as a tree on cards, remove the QR code

Each card now shows, under the call number, the Library of Congress
outline levels for its class (from the published outline, down to the
narrowest range) followed by the place or topic from the catalogue
record's subject heading, one level per line. Existing books get the
subject part filled in by a background job at startup that changes
nothing else about them.

The QR code and everything that read it are removed; the subjects line
on the card is replaced by the tree.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 14:06:50 -07:00
1 parent c9b2c2e8c9
commit 7ae1bcef0a
46 files changed
+8591 -545

No files matched your search

+67 -9
View File
@@ -10,6 +10,11 @@ export interface Jobs {
/** `fresh`: the lookup ignores every cached source answer ("Look up again"), through its retries too. */
enqueue(id: number, opts?: { fresh?: boolean }): void;
resume(): void;
/**
* Backfill, for books made before the hierarchy tail existed: look up only the tail (nothing else about the book
* changes). Books that cannot have one get [] at once; a temporary failure leaves NULL for the next start.
*/
resumeTails(): void;
idle(): Promise<void>;
}
@@ -43,6 +48,9 @@ export function createJobs(deps: {
const fresh = new Set<number>();
// Books waiting for a retry: they hold no worker; a timer puts them back in the queue.
const waiting = new Map<number, ReturnType<typeof setTimeout>>();
// The tail backfill queues behind real lookups, and retries on its own timers.
const tailQueue: number[] = [];
const tailAttempts = new Map<number, number>();
let running: Promise<void> | null = null;
let idlers = 0;
let wake: (() => void)[] = [];
@@ -105,18 +113,19 @@ export function createJobs(deps: {
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 } : {};
if (cur.manualOverride) {
emit(id, { dewey });
emit(id, { dewey, ...(cur.hierarchyTail === null ? { hierarchyTail: [] } : {}) });
log(`[lookup] ${label}: kept the hand-entered call number (${took()})`);
} else if (res.callNumber) {
emit(id, {
callNumber: res.callNumber, sortKey: res.sortKey, confidence: res.confidence,
source: res.source, dewey, status: 'resolved', attentionReason: null,
source: res.source, dewey, status: 'resolved', attentionReason: null, ...tail,
});
log(`[lookup] ${label}: ${res.callNumber} (${res.source === 'similar' && similar ? `similar ${similar}` : res.source}) in ${took()}`);
} else {
const reason = res.reason ?? 'No Library of Congress number found';
emit(id, { dewey, status: 'needs_attention', attentionReason: reason });
emit(id, { dewey, status: 'needs_attention', attentionReason: reason, ...tail });
log(`[lookup] ${label}: no call number, ${reason} in ${took()}`);
}
} catch (e) {
@@ -138,17 +147,62 @@ export function createJobs(deps: {
}
}
/** Sources whose number never comes with a tail of its own: borrowed from similar books, or entered by hand. */
const NO_TAIL_SOURCES = new Set(['similar', 'manual']);
async function backfillTail(id: number): Promise<void> {
const book = db.getBook(id);
if (!book || book.hierarchyTail !== null || book.status === 'pending') { tailAttempts.delete(id); return; }
const label = book.isbn13 ?? book.lccn ?? book.title;
// Only the tail column is ever written here: the number, sort key, confidence, source, status, queue flag and
// printed date stay as they are.
if ((!book.isbn13 && !book.lccn) || book.manualOverride || (book.source && NO_TAIL_SOURCES.has(book.source))) {
tailAttempts.delete(id);
emit(id, { hierarchyTail: [] });
return;
}
const n = tailAttempts.get(id) ?? 0;
try {
const tail = await resolver.tail({
...(book.isbn13 ? { isbn13: book.isbn13 } : {}), ...(book.lccn ? { lccn: book.lccn } : {}),
title: book.title, ...(book.authors[0] ? { author: book.authors[0] } : {}),
viaTitle: book.source === 'loc-title' || book.source === 'ol-work',
});
tailAttempts.delete(id);
emit(id, { hierarchyTail: tail });
log(`[tail] ${label}: ${tail.length ? tail.join(' / ') : 'no usable subject heading'}`);
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
if (isTemporary(e) && n < delays.length) {
tailAttempts.set(id, n + 1);
const delay = delays[n]!;
log(`[tail] ${label}: ${message}; trying again in ${Math.round(delay / 1000)} s`);
const t = setTimeout(() => { waiting.delete(-id); tailQueue.push(id); start(); notify(); }, delay);
if (idlers === 0) t.unref();
waiting.set(-id, t); // a negative key keeps tail retries apart from lookup retries
return;
}
tailAttempts.delete(id);
log(`[tail] ${label}: left for the next start, ${message}`); // NULL stays: the next start tries again
}
}
async function drain(): Promise<void> {
while (queue.length) {
const id = queue.shift()!;
queued.delete(id);
try { await attempt(id); } catch { /* a failure on one book must not stop the queue */ }
while (queue.length || tailQueue.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 {
const id = tailQueue.shift()!;
try { await backfillTail(id); } catch { /* ditto */ }
}
}
}
function start(): void {
if (running) return;
running = drain().finally(() => { running = null; if (queue.length) start(); notify(); });
running = drain().finally(() => { running = null; if (queue.length || tailQueue.length) start(); notify(); });
}
return {
@@ -163,11 +217,15 @@ export function createJobs(deps: {
start();
},
resume() { for (const id of db.listPendingIds()) this.enqueue(id); },
resumeTails() {
for (const id of db.listTailMissingIds()) if (!tailQueue.includes(id)) tailQueue.push(id);
start();
},
async idle() {
idlers++;
for (const t of waiting.values()) t.ref();
try {
while (running || queue.length || waiting.size) {
while (running || queue.length || tailQueue.length || waiting.size) {
if (running) await running;
else await new Promise<void>((r) => { wake.push(r); });
}