feat: manual, reversible same-book mappings between Open Library works #8

Merged
chris-paganon merged 9 commits from feat/canonical-book-mapping into master 2026-11-14 19:30:59 +00:00

Canonical book mapping

A reader can now say that an Open Library work is the same book as one of their saved books (a duplicate record, or a translation filed as its own work), and undo it later. Mapping is manual only. Nothing is merged automatically.

Changes

  • Schema (migration 0012)
    • New book_work_mapping table: (user_id, work_id) → canonical_work_id, plus display snapshots of both titles.
    • New library_book.merged_into_id column, with a composite self-FK that has ON DELETE CASCADE.
  • Server (server/utils/book-mapping.ts, POST/DELETE /api/library/mappings)
    • Creates and removes mappings and finds a book's single visible entry.
    • Merges entries with the PR #3 rules: custom lists are unioned and the reading status keeps Read > Reading > To read.
    • Unmapping restores entries that were merged.
  • Resolution through mappings
    • Saving a mapped work updates the lists of the saved entry. That entry keeps its own metadata and work ID.
    • Goodreads import merges into the mapped entry. Metadata from the other work is never used to fill the entry.
    • Recommendations exclude every work mapped to a saved book.
    • Search, library, author and series grids and the recommendation resolve dialog show the saved state of the shared entry.
  • UI
    • Cards get a Same book as… action. It opens a dialog where you pick a book from your library (a UiCommand list, searchable by title).
    • Suggestions at the top of the picker are clearly labelled: same author and similar title. They are computed only from your library, so they cost no extra Open Library requests.
    • On confirm, the dialog explains what will happen, including the merge when both books are saved.
    • Mapped cards show Same book as "…" with an Undo button. The card of the canonical book lists the works mapped to it.

Decisions

  • Mappings are per reader, not instance-wide. I did not follow the suggested instance-wide approach, for these reasons:
    • Libraries, merges and manual books are per reader, and manual book IDs are private.
    • With instance-wide mappings, one reader's mapping or undo would silently merge or split another reader's entries.
    • That would also force a permission model that the docs leave undecided.
    • On a small instance, redoing a mapping costs little. Catalog-wide mappings stay in "Catalog-wide cleanup".
  • Original identities are preserved. No stored work_id is ever rewritten. Everything resolves through the mapping table.
  • Mappings are one level deep, so there are no chains or cycles.
    • The canonical work is the target entry's canonical work.
    • A work that is already mapped must be undone before it can be mapped again.
    • A work that has duplicates cannot be mapped onto another book (the error says to map the other book onto it instead).
    • Mapping a book onto its own group returns "already the same book".
    • A per-reader advisory lock serializes mapping, saving and importing.
  • Merge and unmap semantics
    • When both works are saved, confirming hides the reader's entry for the mapped work (merged_into_id). That entry keeps its own lists untouched, and its lists are merged into the chosen entry.
    • Undo shows the hidden entry again with the lists it had before the merge. The entry that stayed visible keeps the combined lists, because later edits can't be separated from the merge.
    • If undo leaves several hidden entries of one book without a visible entry, the earliest becomes visible and the others stay merged into it.
    • Removing an entry also removes the entries merged into it.
  • "Use this edition" is hidden across mapped works. Showing another work's metadata under the original work ID would make the entry wrong after an undo. A mapped duplicate's card says Saved as "…" (or only the mapping line when that already names the entry), and Edit lists edits the shared entry.
  • Manual books can be the canonical target or the mapped work. Mappings that involve a removed manual book are deleted, because that book can never come back.
  • Request counts: no new Open Library requests anywhere.

Verification

  • pnpm lint-full passes.
  • API checks with curl:
    • Merging "El principito" (OL27308483W, Read) into "Le petit prince" (OL10263W, no list) gave the visible entry Read and dropped the library count from 63 to 62.
    • Saving the duplicate updated the shared entry. Editing the lists of the hidden entry returns 404.
    • Undo restored El principito with Read (63 entries).
    • Every guard returned its message: "already the same book", "already mapped", "a work with duplicates can't be mapped", hidden target, unknown manual ID.
    • The chain-repair case worked: an entry was merged into a duplicate's entry, then that duplicate was unmapped, and the merged entry became visible again.
    • A manual book worked as the target, and its mappings were removed when it was removed.
    • The recommendations feed still loads.
  • Browser: the flow in the demo below, on the search page and in the library.
  • Goodreads import was not run end to end. Only the merge step changed, and it uses the same lookup as saving.

Demo

Search "petit prince": map the separate "Petit Prince" work onto the saved "Le petit prince" (picked from the suggestions). The card now shows the saved state with Undo. In the library, merge "El principito" (Read) into "Le petit prince", then Undo, which restores it with its lists.

demo

Demo video

## Canonical book mapping A reader can now say that an Open Library work is the **same book as** one of their saved books (a duplicate record, or a translation filed as its own work), and undo it later. Mapping is manual only. Nothing is merged automatically. ### Changes - **Schema (migration `0012`)** - New `book_work_mapping` table: `(user_id, work_id) → canonical_work_id`, plus display snapshots of both titles. - New `library_book.merged_into_id` column, with a composite self-FK that has `ON DELETE CASCADE`. - **Server** (`server/utils/book-mapping.ts`, `POST`/`DELETE /api/library/mappings`) - Creates and removes mappings and finds a book's single visible entry. - Merges entries with the PR #3 rules: custom lists are unioned and the reading status keeps Read > Reading > To read. - Unmapping restores entries that were merged. - **Resolution through mappings** - **Saving** a mapped work updates the lists of the saved entry. That entry keeps its own metadata and work ID. - **Goodreads import** merges into the mapped entry. Metadata from the other work is never used to fill the entry. - **Recommendations** exclude every work mapped to a saved book. - **Search, library, author and series grids and the recommendation resolve dialog** show the saved state of the shared entry. - **UI** - Cards get a **Same book as…** action. It opens a dialog where you pick a book from your library (a `UiCommand` list, searchable by title). - **Suggestions** at the top of the picker are clearly labelled: same author and similar title. They are computed only from your library, so they cost no extra Open Library requests. - On confirm, the dialog explains what will happen, including the merge when both books are saved. - Mapped cards show *Same book as "…"* with an **Undo** button. The card of the canonical book lists the works mapped to it. ### Decisions - **Mappings are per reader, not instance-wide.** I did not follow the suggested instance-wide approach, for these reasons: - Libraries, merges and manual books are per reader, and manual book IDs are private. - With instance-wide mappings, one reader's mapping or undo would silently merge or split another reader's entries. - That would also force a permission model that the docs leave undecided. - On a small instance, redoing a mapping costs little. Catalog-wide mappings stay in "Catalog-wide cleanup". - **Original identities are preserved.** No stored `work_id` is ever rewritten. Everything resolves through the mapping table. - **Mappings are one level deep, so there are no chains or cycles.** - The canonical work is the target entry's canonical work. - A work that is already mapped must be undone before it can be mapped again. - A work that has duplicates cannot be mapped onto another book (the error says to map the other book onto it instead). - Mapping a book onto its own group returns "already the same book". - A per-reader advisory lock serializes mapping, saving and importing. - **Merge and unmap semantics** - When both works are saved, confirming hides the reader's entry for the mapped work (`merged_into_id`). That entry keeps its own lists untouched, and its lists are merged into the chosen entry. - **Undo** shows the hidden entry again with the lists it had before the merge. The entry that stayed visible keeps the combined lists, because later edits can't be separated from the merge. - If undo leaves several hidden entries of one book without a visible entry, the earliest becomes visible and the others stay merged into it. - Removing an entry also removes the entries merged into it. - **"Use this edition" is hidden across mapped works.** Showing another work's metadata under the original work ID would make the entry wrong after an undo. A mapped duplicate's card says *Saved as "…"* (or only the mapping line when that already names the entry), and **Edit lists** edits the shared entry. - **Manual books** can be the canonical target or the mapped work. Mappings that involve a removed manual book are deleted, because that book can never come back. - **Request counts:** no new Open Library requests anywhere. ### Verification - `pnpm lint-full` passes. - API checks with curl: - Merging "El principito" (OL27308483W, Read) into "Le petit prince" (OL10263W, no list) gave the visible entry **Read** and dropped the library count from 63 to 62. - Saving the duplicate updated the shared entry. Editing the lists of the hidden entry returns 404. - Undo restored El principito with Read (63 entries). - Every guard returned its message: "already the same book", "already mapped", "a work with duplicates can't be mapped", hidden target, unknown manual ID. - The chain-repair case worked: an entry was merged into a duplicate's entry, then that duplicate was unmapped, and the merged entry became visible again. - A manual book worked as the target, and its mappings were removed when it was removed. - The recommendations feed still loads. - Browser: the flow in the demo below, on the search page and in the library. - Goodreads import was **not** run end to end. Only the merge step changed, and it uses the same lookup as saving. ### Demo Search "petit prince": map the separate "Petit Prince" work onto the saved "Le petit prince" (picked from the suggestions). The card now shows the saved state with Undo. In the library, merge "El principito" (Read) into "Le petit prince", then Undo, which restores it with its lists. ![demo](https://forgejo.chrispaganon.com/attachments/a8a6fe55-f512-498f-b45f-faa9a24b530a) [Demo video](https://forgejo.chrispaganon.com/attachments/a8a6fe55-f512-498f-b45f-faa9a24b530a)
Author
Owner

Review of the six commits in master...feat/canonical-book-mapping.

Important — required before merge

  1. [P2] Lock both entries before reading memberships for the merge — server/utils/book-mapping.ts:46–57 (also :65–68).

    The identity advisory lock does not serialize with updateMemberships (server/utils/library.ts:148–154), which only locks the book row. Neither source nor target is row-locked before mergeEntry reads their memberships. For example, an Edit lists request can lock the source and replace its lists while a mapping request reads its old committed memberships. The mapping copies those old memberships, waits at the final source-row update, then hides the source after the edit commits. Both requests succeed, but the source's newly added lists/status are absent from the visible merged entry. Acquire compatible locks on both book rows before taking the membership snapshot, or have membership edits participate in the same identity lock.

  2. [P2] Preserve the imported work identity when reusing Goodreads matches after undo — server/utils/library-import.ts:53–54, :80 (interaction with :120–129).

    Reproduce: save B; map an unsaved catalog work A onto B; import Goodreads row G matched to A; undo A's mapping; upload G again. The first import resolves A to B and stores B's entry ID in library_import_item.bookId. The repeat-import shortcut reconstructs the match from that joined B row, ignoring the original A candidate retained by finishItem. It therefore continues importing G into B even though A and B are now separate. Preserve/reuse the originally confirmed work metadata and resolve it against the current mappings when repeating an import, rather than treating the previous destination entry as the confirmed work identity. Keep the existing reuse behavior for manually created matches.

  3. [P2] Combine memberships when undo reparents multiple hidden entries — server/utils/book-mapping.ts:114–119.

    Reproduce: map unsaved A onto saved B; remove B (the catalog mapping survives); save A; save C and D with different custom lists/statuses and map both onto the saved A entry. The mappings are A→B, C→B, D→B, with C and D hidden under A. Undo A→B. restoreSeparatedEntries restores the earliest of C/D and reparents the other underneath it, but only changes mergedIntoId: it never unions their lists or applies the most-advanced-status rule. C and D still resolve to the same book, yet their visible entry lacks the other hidden entry's lists/status; those lists only remain on the now-separate A entry. When rebuilding a remaining merged group, consolidate its memberships into its new visible entry while preserving the hidden rows' own memberships for later undo.

Categories: important 3; nit picks 0; pre-existing 0; security 0.

Validation: pnpm lint-full passed. No additional upstream Open Library requests found in the PR. Review only; no repository code or tests changed.

Review of the six commits in `master...feat/canonical-book-mapping`. ### Important — required before merge 1. **[P2] Lock both entries before reading memberships for the merge** — `server/utils/book-mapping.ts:46–57` (also `:65–68`). The identity advisory lock does not serialize with `updateMemberships` (`server/utils/library.ts:148–154`), which only locks the book row. Neither source nor target is row-locked before `mergeEntry` reads their memberships. For example, an Edit lists request can lock the source and replace its lists while a mapping request reads its old committed memberships. The mapping copies those old memberships, waits at the final source-row update, then hides the source after the edit commits. Both requests succeed, but the source's newly added lists/status are absent from the visible merged entry. Acquire compatible locks on both book rows before taking the membership snapshot, or have membership edits participate in the same identity lock. 2. **[P2] Preserve the imported work identity when reusing Goodreads matches after undo** — `server/utils/library-import.ts:53–54`, `:80` (interaction with `:120–129`). Reproduce: save B; map an unsaved catalog work A onto B; import Goodreads row G matched to A; undo A's mapping; upload G again. The first import resolves A to B and stores B's entry ID in `library_import_item.bookId`. The repeat-import shortcut reconstructs the match from that joined B row, ignoring the original A candidate retained by `finishItem`. It therefore continues importing G into B even though A and B are now separate. Preserve/reuse the originally confirmed work metadata and resolve it against the current mappings when repeating an import, rather than treating the previous destination entry as the confirmed work identity. Keep the existing reuse behavior for manually created matches. 3. **[P2] Combine memberships when undo reparents multiple hidden entries** — `server/utils/book-mapping.ts:114–119`. Reproduce: map unsaved A onto saved B; remove B (the catalog mapping survives); save A; save C and D with different custom lists/statuses and map both onto the saved A entry. The mappings are A→B, C→B, D→B, with C and D hidden under A. Undo A→B. `restoreSeparatedEntries` restores the earliest of C/D and reparents the other underneath it, but only changes `mergedIntoId`: it never unions their lists or applies the most-advanced-status rule. C and D still resolve to the same book, yet their visible entry lacks the other hidden entry's lists/status; those lists only remain on the now-separate A entry. When rebuilding a remaining merged group, consolidate its memberships into its new visible entry while preserving the hidden rows' own memberships for later undo. Categories: important **3**; nit picks **0**; pre-existing **0**; security **0**. Validation: `pnpm lint-full` passed. No additional upstream Open Library requests found in the PR. Review only; no repository code or tests changed.
Author
Owner

Thanks. I accepted and fixed all three findings, one commit each. pnpm lint-full passes.

  1. Concurrent list edits during a merge (5d6f774): mergeEntry now row-locks both entries, always in id order, before it reads their memberships. A concurrent Edit lists request then either commits before the snapshot is taken or waits until the mapping finishes.
  2. Repeat imports after undo (57b8f28): a repeated import now reuses the catalogue work that was originally confirmed (the import item's stored candidates[0]) and resolves it through the current mappings. It no longer reuses the entry that work was merged into. Manually created books still reuse their own entry. The "only when the saved entry still exists" rule is unchanged.
  3. Reparenting on undo (f70128a): when undo leaves several hidden entries of one book together, the earliest one is restored. The others now merge into it through mergeEntry, so it gains their lists with the usual rules (custom lists combined, most advanced status wins). The hidden entries keep their own lists for a later undo. I checked your scenario against the dev server: C (Reading) came back with D's Read status and D's custom list, and A kept the combined lists.

Master had not moved, so no rebase or migration regeneration was needed.

Thanks. I accepted and fixed all three findings, one commit each. `pnpm lint-full` passes. 1. **Concurrent list edits during a merge** (`5d6f774`): `mergeEntry` now row-locks both entries, always in id order, before it reads their memberships. A concurrent **Edit lists** request then either commits before the snapshot is taken or waits until the mapping finishes. 2. **Repeat imports after undo** (`57b8f28`): a repeated import now reuses the catalogue work that was originally confirmed (the import item's stored `candidates[0]`) and resolves it through the current mappings. It no longer reuses the entry that work was merged into. Manually created books still reuse their own entry. The "only when the saved entry still exists" rule is unchanged. 3. **Reparenting on undo** (`f70128a`): when undo leaves several hidden entries of one book together, the earliest one is restored. The others now merge into it through `mergeEntry`, so it gains their lists with the usual rules (custom lists combined, most advanced status wins). The hidden entries keep their own lists for a later undo. I checked your scenario against the dev server: C (Reading) came back with D's Read status and D's custom list, and A kept the combined lists. Master had not moved, so no rebase or migration regeneration was needed.
chris-paganon deleted branch feat/canonical-book-mapping 2026-11-14 19:31:00 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
chris-paganon/chrisp-books!8
No description provided.