feat: record explicit relationships between books #9

Merged
chris-paganon merged 11 commits from feat/explicit-relationships into master 2026-11-14 19:40:28 +00:00

Explicit relationships

Records relationships between different books: split volumes, collections/box sets and companions (study guides, summaries, adaptations). Same-book equivalence stays out of scope (canonical book mapping).

Changes

  • Model (book_relationship, migration 0012): source/target work ID (Open Library or the reader's own manual: ID), kind (part_of, contains, adaptation_of, study_guide_for, plus not_related for rejected suggestions), provenance, optional position, title snapshots, created_by/created_at. A unique index on the unordered pair keeps one record per pair, so a correction replaces it whichever direction it had.
  • Heuristics (server/utils/book-relationships.ts). They only use the books already in the response:
    • part labels (extractPart) plus the same base title and a shared author → part_of, with the part number as position;
    • collection title patterns reused from the author bibliography (box set, "Books 1-3", "A / B", "6 Books", trilogy) or the subject "book set" → a collection hint with an estimated size, plus contains for books of the same author named in the title/subtitle;
    • companion title patterns (study guide, summary, workbook, reader's guide, graphic novel, …) or subjects → study_guide_for/adaptation_of for the book named in the title.
  • API: GET /api/books/search and GET /api/library return relationships (stored and suggested, with provenance and basis) and collectionHints, ready for the later "Cleaner search results" grouping (no grouping here). POST /api/relationships, DELETE /api/relationships/:id.
  • Cards (search, library, author pages via BookLibraryGrid):
    • compact lines: "Part 2 of Words of Radiance", "Split into parts: Part 1 · Part 2 · 1 of 2 known parts read", "Contains N books ▾", "In N collections", "Study guide for X", "2 companion books ▾";
    • related books in the same results link to their card anchor; others link to a title search;
    • a "Related" menu in the existing controls row: Mark as part of… / collection containing… / companion or study guide of…, with a searchable picker over the current results and the library, and Remove for recorded relationships.

Decisions

  • Instance-wide, like shared catalogue metadata: a correction helps every reader, and any signed-in reader may add or remove one. Linking manual: books requires owning them.
  • contains is not the inverse of part_of: a box set bundles complete books, while a split volume is a fragment of one. The two are displayed and counted differently.
  • Suggestions are never stored. They are recomputed per response and shown dashed with "Possibly …?", Confirm and Not related. Confirm records a manual row. Not related records not_related, so the suggestion stays hidden. Storing heuristic rows would persist guesses from one search page and mix them with reader decisions. The heuristic provenance value is reserved for future persisted classifications (e.g. catalogue import), which manual rows override. Any stored record wins for its pair, and a book manually placed as a part or companion gets no competing suggestion.
  • Reading statuses never propagate between parts and the whole, or between collections and their books. Part progress is informational and says "known parts".
  • Requests: no extra upstream request. Search asks for subject in the same single query, which changes the search cache key once. Relationship reads are one DB query per search/library response.

Investigation cases

  • Words of Radiance: Part 1/2, "part two", "(1 of 5)" and the leather "Volume 1" are suggested as parts of the work by Sanderson. Four box sets are suggested to contain it, each with a size hint. The same-title BookBuddy record and the author-less records get no suggestion: unknown stays unknown.
  • House in the Cerulean Sea: the PowerNotes workbook and the SuperSummary study guide are suggested as study guides. Two collections are suggested to contain it.
  • Mistborn trilogy boxed set: the only result shows "Possibly a collection of 3 books". The reader can link the contained books from their library with "Mark as collection containing…".

Verification

  • pnpm lint-full passes.
  • API checks: same-ID, invalid-kind, non-owned manual: ID (404), unknown delete (404), and a reverse-direction correction replacing the pair.
  • Browser: confirming, manual part-of, anchor link, part progress with one part in Read while the whole book stays unsaved, study-guide confirm, Not related and removal.

Demo

demo

Demo video

## Explicit relationships Records relationships between *different* books: split volumes, collections/box sets and companions (study guides, summaries, adaptations). Same-book equivalence stays out of scope (canonical book mapping). ### Changes - **Model** (`book_relationship`, migration `0012`): source/target work ID (Open Library or the reader's own `manual:` ID), `kind` (`part_of`, `contains`, `adaptation_of`, `study_guide_for`, plus `not_related` for rejected suggestions), `provenance`, optional `position`, title snapshots, `created_by`/`created_at`. A unique index on the unordered pair keeps one record per pair, so a correction replaces it whichever direction it had. - **Heuristics** (`server/utils/book-relationships.ts`). They only use the books already in the response: - part labels (`extractPart`) plus the same base title and a shared author → `part_of`, with the part number as position; - collection title patterns reused from the author bibliography (box set, "Books 1-3", "A / B", "6 Books", trilogy) or the subject "book set" → a collection hint with an estimated size, plus `contains` for books of the same author named in the title/subtitle; - companion title patterns (study guide, summary, workbook, reader's guide, graphic novel, …) or subjects → `study_guide_for`/`adaptation_of` for the book named in the title. - **API**: `GET /api/books/search` and `GET /api/library` return `relationships` (stored and suggested, with `provenance` and `basis`) and `collectionHints`, ready for the later "Cleaner search results" grouping (no grouping here). `POST /api/relationships`, `DELETE /api/relationships/:id`. - **Cards** (search, library, author pages via `BookLibraryGrid`): - compact lines: "Part 2 of Words of Radiance", "Split into parts: Part 1 · Part 2 · 1 of 2 known parts read", "Contains N books ▾", "In N collections", "Study guide for X", "2 companion books ▾"; - related books in the same results link to their card anchor; others link to a title search; - a "Related" menu in the existing controls row: Mark as part of… / collection containing… / companion or study guide of…, with a searchable picker over the current results and the library, and Remove for recorded relationships. ### Decisions - **Instance-wide**, like shared catalogue metadata: a correction helps every reader, and any signed-in reader may add or remove one. Linking `manual:` books requires owning them. - **`contains` is not the inverse of `part_of`**: a box set bundles complete books, while a split volume is a fragment of one. The two are displayed and counted differently. - **Suggestions are never stored.** They are recomputed per response and shown dashed with "Possibly …?", Confirm and Not related. Confirm records a `manual` row. Not related records `not_related`, so the suggestion stays hidden. Storing heuristic rows would persist guesses from one search page and mix them with reader decisions. The `heuristic` provenance value is reserved for future persisted classifications (e.g. catalogue import), which manual rows override. Any stored record wins for its pair, and a book manually placed as a part or companion gets no competing suggestion. - **Reading statuses never propagate** between parts and the whole, or between collections and their books. Part progress is informational and says "known parts". - **Requests**: no extra upstream request. Search asks for `subject` in the same single query, which changes the search cache key once. Relationship reads are one DB query per search/library response. ### Investigation cases - *Words of Radiance*: Part 1/2, "part two", "(1 of 5)" and the leather "Volume 1" are suggested as parts of the work by Sanderson. Four box sets are suggested to contain it, each with a size hint. The same-title BookBuddy record and the author-less records get no suggestion: unknown stays unknown. - *House in the Cerulean Sea*: the PowerNotes workbook and the SuperSummary study guide are suggested as study guides. Two collections are suggested to contain it. - *Mistborn trilogy boxed set*: the only result shows "Possibly a collection of 3 books". The reader can link the contained books from their library with "Mark as collection containing…". ### Verification - `pnpm lint-full` passes. - API checks: same-ID, invalid-kind, non-owned `manual:` ID (404), unknown delete (404), and a reverse-direction correction replacing the pair. - Browser: confirming, manual part-of, anchor link, part progress with one part in Read while the whole book stays unsaved, study-guide confirm, Not related and removal. ### Demo ![demo](https://forgejo.chrispaganon.com/attachments/e7d522d9-d407-4b3f-ad0e-e4ba64ed13e8) [Demo video](https://forgejo.chrispaganon.com/attachments/e7d522d9-d407-4b3f-ad0e-e4ba64ed13e8)
Author
Owner

Review of the six commits on feat/explicit-relationships against git merge-base origin/master HEAD; excludes the unrelated same-book mapping PR. Reviewed endpoint validation/authorization, persistence, suggestion generation and client integration. pnpm lint-full passes. No repository files changed.

Security — 1 finding

[P1] Filter relationships involving private manual books by ownership — must fix before merge.

Location: server/utils/book-relationships.ts:149–159 (also called by server/api/books/search.get.ts:30 and server/utils/library.ts:74).

saveBookRelationship checks ownership of manual: books, but bookRelationshipsFor has no reader identity and returns every stored relationship touching any requested work. If Alice links her private manually added manuscript to an Open Library work, Bob can search for that public work (or save it in his library) and receive Alice's manual-book ID and title snapshot, plus the relationship ID. This violates the stated rule that manual books are visible only to their owner. Read-only execution of the actual helper with a representative stored row confirms that the private title is returned for a response containing only the public work. Pass the authenticated reader through the read path and exclude links unless they own every manual endpoint; keep this filtering on the server so it also protects API/SSR payloads.

Important — 2 findings

[P2] Load stored relationships for author/series results — must fix before merge.

Location: app/components/book/BookLibraryGrid.vue:20; server/utils/author-bibliography.ts:138–149; app/components/author/AuthorBibliographyGroup.vue:11–14; app/components/book/BookBibliography.vue:81–85.

The new Related menu is rendered on author and series cards, but those callers never provide relationships, and their APIs never call bookRelationshipsFor. The grid can therefore retrieve existing records only through the reader's saved library. Reproduction: with neither book saved, open an author page and mark one displayed book as a part/companion of another. The link appears through the local override; reload the page and it disappears, including its Remove action, despite still existing in the database. Other readers browsing that author never see the correction either. Return and pass relationship data for these result sets (one batched lookup, without new upstream requests).

[P2] Preserve rejection records when merging independently cached responses — must fix before merge.

Location: server/utils/book-relationships.ts:154–161; app/composables/useBookRelationships.ts:9–22; app/components/book/BookLibraryGrid.vue:20.

The server strips not_related records entirely, so a fresh response cannot override a rejected suggestion retained in the other response cache. For example, a reader has search results and library data containing the same suggested pair; another reader rejects it. Refreshing library data now correctly omits the pair, but the grid merges the old search suggestion back in. The reverse happens when refreshing search while the library cache still contains the suggestion. The first reader can then Confirm this obsolete suggestion and overwrite the rejection. Executing the actual client merge with a cached suggestion plus the fresh empty relationship list reproduces the resurrection. Return rejected-pair tombstones (or equivalent suppression metadata) and apply them across both sources before displaying suggestions, rather than treating omission as authoritative rejection.

Nit picks — 0

Pre-existing — 0

Additional checks: relationship writes validate kind, distinct work IDs and bounded positions; unordered uniqueness is enforced in PostgreSQL, and saving a correct manual link replaces an earlier not_related row. Deletion by any signed-in reader follows the stated shared-catalog policy. Suggestion computation makes no upstream calls and relationship reads use one batch query, without an N+1 lookup. Adding subject changes search cache keys, so existing cached searches incur a new upstream fetch on first use; it does not add a second request per search. Author responses currently perform no relationship suggestion computation at all, as described above.

Review of the six commits on `feat/explicit-relationships` against `git merge-base origin/master HEAD`; excludes the unrelated same-book mapping PR. Reviewed endpoint validation/authorization, persistence, suggestion generation and client integration. `pnpm lint-full` passes. No repository files changed. ## Security — 1 finding **[P1] Filter relationships involving private manual books by ownership — must fix before merge.** Location: `server/utils/book-relationships.ts:149–159` (also called by `server/api/books/search.get.ts:30` and `server/utils/library.ts:74`). `saveBookRelationship` checks ownership of `manual:` books, but `bookRelationshipsFor` has no reader identity and returns every stored relationship touching any requested work. If Alice links her private manually added manuscript to an Open Library work, Bob can search for that public work (or save it in his library) and receive Alice's manual-book ID and title snapshot, plus the relationship ID. This violates the stated rule that manual books are visible only to their owner. Read-only execution of the actual helper with a representative stored row confirms that the private title is returned for a response containing only the public work. Pass the authenticated reader through the read path and exclude links unless they own every manual endpoint; keep this filtering on the server so it also protects API/SSR payloads. ## Important — 2 findings **[P2] Load stored relationships for author/series results — must fix before merge.** Location: `app/components/book/BookLibraryGrid.vue:20`; `server/utils/author-bibliography.ts:138–149`; `app/components/author/AuthorBibliographyGroup.vue:11–14`; `app/components/book/BookBibliography.vue:81–85`. The new Related menu is rendered on author and series cards, but those callers never provide relationships, and their APIs never call `bookRelationshipsFor`. The grid can therefore retrieve existing records only through the reader's saved library. Reproduction: with neither book saved, open an author page and mark one displayed book as a part/companion of another. The link appears through the local override; reload the page and it disappears, including its Remove action, despite still existing in the database. Other readers browsing that author never see the correction either. Return and pass relationship data for these result sets (one batched lookup, without new upstream requests). **[P2] Preserve rejection records when merging independently cached responses — must fix before merge.** Location: `server/utils/book-relationships.ts:154–161`; `app/composables/useBookRelationships.ts:9–22`; `app/components/book/BookLibraryGrid.vue:20`. The server strips `not_related` records entirely, so a fresh response cannot override a rejected suggestion retained in the other response cache. For example, a reader has search results and library data containing the same suggested pair; another reader rejects it. Refreshing library data now correctly omits the pair, but the grid merges the old search suggestion back in. The reverse happens when refreshing search while the library cache still contains the suggestion. The first reader can then Confirm this obsolete suggestion and overwrite the rejection. Executing the actual client merge with a cached suggestion plus the fresh empty relationship list reproduces the resurrection. Return rejected-pair tombstones (or equivalent suppression metadata) and apply them across both sources before displaying suggestions, rather than treating omission as authoritative rejection. ## Nit picks — 0 ## Pre-existing — 0 Additional checks: relationship writes validate kind, distinct work IDs and bounded positions; unordered uniqueness is enforced in PostgreSQL, and saving a correct manual link replaces an earlier `not_related` row. Deletion by any signed-in reader follows the stated shared-catalog policy. Suggestion computation makes no upstream calls and relationship reads use one batch query, without an N+1 lookup. Adding `subject` changes search cache keys, so existing cached searches incur a new upstream fetch on first use; it does not add a second request per search. Author responses currently perform no relationship suggestion computation at all, as described above.
chris-paganon force-pushed feat/explicit-relationships from 96f7634432 to f4aa297e6e 2026-11-14 19:40:18 +00:00 Compare
Author
Owner

Thanks for the review. All three findings are accepted and fixed. The branch is rebased on master (#8 same-book mappings), and the relationship migration is regenerated as 0013_cold_gravity.sql, chained to master's 0012.

[P1] Manual-book privacy (10bb78d)

  • bookRelationshipsFor and removeBookRelationship now take the reader's ID. Stored rows are filtered server-side so a reader only sees rows where they own every manual: endpoint. This covers search, library, author and series payloads, including SSR.
  • Hidden rows also don't count towards suppressing suggestions, so their existence doesn't leak indirectly.
  • Deleting a hidden record returns 404.
  • Verified: a row linking another reader's manual book to Words of Radiance is absent from the search response, and deleting it by ID returns 404.

[P2] Author/series pages (25cfe97, f4aa297)

  • The author and series endpoints return relationship data from one batched lookup, with no upstream request. BookBibliography and AuthorBibliographyGroup pass it to the grid, so links made there persist after reload and are visible to other readers.
  • Series pages also get suggestions, like a search page.
  • Author pages return recorded relationships only. On Sanderson's 500-record bibliography, suggestions produced 90 uncertain rows (78 box-set "contains"), which buried the page, and that page already groups collections.
  • Verified: a part link recorded on search shows up in /api/authors/OL1394865A.

[P2] Rejected pairs across cached responses (9bfbbcd)

  • Responses now include rejectedPairs (visible not_related pair keys).
  • The client merge collects them from every source and drops heuristic links for those pairs before display, so an older cached response can no longer resurrect a rejected suggestion or let it be confirmed over the rejection.

Docs are updated (d1b4c5f). Also adapted during the rebase:

  • the grid keeps master's same-book resolver;
  • part read-progress counts parts mapped as the same book as a read entry;
  • the Related menu uses a different icon from "Same book as…".

pnpm lint-full passes. All 14 migrations apply to a fresh clone. In the browser, Words of Radiance suggestions and a manual "part of" link still work next to the new same-book control, and the link persists after reload.

Thanks for the review. All three findings are accepted and fixed. The branch is rebased on master (#8 same-book mappings), and the relationship migration is regenerated as `0013_cold_gravity.sql`, chained to master's `0012`. **[P1] Manual-book privacy** (`10bb78d`) - `bookRelationshipsFor` and `removeBookRelationship` now take the reader's ID. Stored rows are filtered server-side so a reader only sees rows where they own every `manual:` endpoint. This covers search, library, author and series payloads, including SSR. - Hidden rows also don't count towards suppressing suggestions, so their existence doesn't leak indirectly. - Deleting a hidden record returns 404. - Verified: a row linking another reader's manual book to Words of Radiance is absent from the search response, and deleting it by ID returns 404. **[P2] Author/series pages** (`25cfe97`, `f4aa297`) - The author and series endpoints return relationship data from one batched lookup, with no upstream request. `BookBibliography` and `AuthorBibliographyGroup` pass it to the grid, so links made there persist after reload and are visible to other readers. - Series pages also get suggestions, like a search page. - Author pages return recorded relationships only. On Sanderson's 500-record bibliography, suggestions produced 90 uncertain rows (78 box-set "contains"), which buried the page, and that page already groups collections. - Verified: a part link recorded on search shows up in `/api/authors/OL1394865A`. **[P2] Rejected pairs across cached responses** (`9bfbbcd`) - Responses now include `rejectedPairs` (visible `not_related` pair keys). - The client merge collects them from every source and drops heuristic links for those pairs before display, so an older cached response can no longer resurrect a rejected suggestion or let it be confirmed over the rejection. Docs are updated (`d1b4c5f`). Also adapted during the rebase: - the grid keeps master's same-book resolver; - part read-progress counts parts mapped as the same book as a read entry; - the Related menu uses a different icon from "Same book as…". `pnpm lint-full` passes. All 14 migrations apply to a fresh clone. In the browser, Words of Radiance suggestions and a manual "part of" link still work next to the new same-book control, and the link persists after reload.
chris-paganon deleted branch feat/explicit-relationships 2026-11-14 19:40:28 +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!9
No description provided.