Keep retrying lookups until the sources answer

A network error, timeout, 5xx or 429 no longer ends a lookup or sends a
book to Needs attention. Lookups back off exponentially with jitter (2 s
doubling to 5 min, honouring Retry-After) for as long as the book is
pending, keep each definitive answer so a retry only asks for what is
missing, and limit requests to two at a time per host. Once a call number
is in hand, a step that still fails after six tries is left for the
backfills. Books an earlier version failed for a temporary reason are
queued again at startup.

The cards show which source is not answering and when the next try is,
title search waits for Open Library with a Cancel button, and Look up
again sits next to Fix.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-22 11:35:37 -07:00
1 parent ee10e00ccd
commit e65b10c0fb
24 files changed
+700 -104

No files matched your search

+20 -7
View File
@@ -19,7 +19,11 @@ export type { SourceCache };
export interface ResolveOpts {
onMeta?: (meta: BookMeta) => void;
/** Last-attempt mode: a Library of Congress failure is not fatal; use Open Library's numbers if it has any. */
/**
* Settling mode, for a lookup that has failed for a while: a source that cannot be asked no longer blocks a number
* already in hand (the other source's check, the edition, the tail stay unknown, to be filled in later). With no
* number at all it still throws, so the lookup goes on.
*/
tolerateLocFailure?: boolean;
/** Called when the similar-books tier chose the number, for example "GV199.42.W8 (4 of 9 candidates)". */
onSimilar?: (detail: string) => void;
@@ -132,11 +136,15 @@ export function createResolver(deps: {
}
return r.v ? { ...r.v, meta: withoutLccn(r.v.meta) } : null;
};
let olError: unknown = null;
if (isbn) {
workP = ol.work(isbn).then((v) => ({ v }), (e: unknown) => ({ e }));
edition = await ol.edition(isbn);
try { edition = await ol.edition(isbn); } catch (e) {
if (!(opts?.tolerateLocFailure && e instanceof TemporaryError)) throw e;
olError = e; // settling: the Library of Congress may still have the number
}
if (edition) notify(mergeMeta([edition.meta]));
else { work = await getWork(true); notify(mergeMeta([work?.meta])); }
else { work = await getWork(!olError); notify(mergeMeta([work?.meta])); }
}
// Library of Congress: by LCCN when known, else by ISBN.
@@ -171,8 +179,12 @@ export function createResolver(deps: {
// a work-level record, another edition or the similar-books search. The Library of Congress first, then Open
// Library's edition record. null (unknown, asked again later) when the Library of Congress failed and had none.
let edit: string | null = locRecs.find((r) => r.edition)?.edition ?? null;
if (edit === null && isbn && !locError) edit = (await ol.editionName(isbn)) ?? '';
else if (edit === null && !isbn && !locError) edit = '';
if (edit === null && isbn && !locError) {
try { edit = (await ol.editionName(isbn)) ?? ''; } catch (e) {
// Settling with a number in hand: the edition stays unknown (NULL) and is looked for later.
if (!(opts?.tolerateLocFailure && e instanceof TemporaryError)) throw e;
}
} else if (edit === null && !isbn && !locError) edit = '';
const olEditionCands: Candidate[] = edition?.lcc ?? [];
const metaOf = (extra: Partials = []): BookMeta | null =>
@@ -221,7 +233,6 @@ export function createResolver(deps: {
);
// Tier 5: borrow the class from similar books. Last resort, and only for a book with a title.
let olError: unknown = null;
const similarRecs: MarcFields[] = [];
let similarSearched = false;
if (!chosen && meta?.title) {
@@ -306,9 +317,11 @@ export function createResolver(deps: {
if (ownRec) opts?.onTailSource?.({ step: 'own', records: [ownRec] });
if (chosen) {
// Settled with a source unasked: the number was not checked against it, so it is marked as approximate.
const settled = !!opts?.tolerateLocFailure && !!(locError || olError);
return {
meta, callNumber: chosen.callNumber, sortKey: chosen.sortKey,
confidence: chosen.confidence, source: chosen.source, dewey, tail, edition: edit, reason: null,
confidence: settled && chosen.confidence === 'exact' ? 'approximate' : chosen.confidence, source: chosen.source, dewey, tail, edition: edit, reason: null,
};
}
// Open Library had nothing and the LoC could not be asked: report that, not "no number found".