readest/readest #6907 · decision plan · code pinned at 5273f82 (main, 2026-10-22)
Hardcover duplicates: look up a matching journal entry before every insert, or fix only the path the reporter confirms?
A push inserts whenever this device's record has no row for a note. The source has five ways that happens: four that the questions already asked on the issue point at, and one where an already-pushed quote returns under a new note id. A Hardcover lookup covers three fully and two in part; a per-path fix covers one, and the second-device path has no local fix.
Open question for the maintainer
If Hardcover serves journal entries only to the read:journal scope, add it to sign-in, or keep today's scopes? Keeping them leaves OAuth users on today's insert (the lookup is refused, R4), so their second-device and re-import duplicates remain. Today's sign-in does not ask for it (hardcoverOAuth.ts:14) and a refresh cannot add a scope, so OAuth users sign in again to get the lookup (OAuth.mdx:163-166). Pasted tokens made before August 2026 already carry all (Getting-Started.mdx:37).
Recommendation (conditional)
| Option | ① 2nd device / web app | ② quit mid-push | ③ reopen mid-push | ④ re-import | ⑤ same quote, new id |
|---|---|---|---|---|---|
| A. Look up Hardcover before inserting | covered | covered | covered once the first insert has landed | covered | re-made: covered; Copy to Notebook: no |
| B. Fix only the confirmed path | no local fix | wait on close + save per batch | module-level queue | re-key on merge | Copy to Notebook: skip the excerpt; re-made: delete the removed note's entry |
First, one GraphQL console query (unknown U1): if user_books → reading_journals answers with today's scopes, take A with no sign-in change. If it needs read:journal: A if the answer above is "add it", otherwise B for the path the reporter confirms (A can still ship for tokens that carry all, at one refused request per inserting push for OAuth users).
Where a push loses its record, and what each option covers
Added risk per branch
A: a note whose text matches an existing entry is linked instead of inserted, so two highlights with the same words could share one entry. Mitigation: same event and exact text only, skip entries owned by a live note here, each entry once per push (R2); a refused or failed lookup inserts as today. B: quit waits up to a cap (②); a stuck push holds that book's next pushes (③); deleting on removal (⑤) erases whatever the user added to that entry on Hardcover.
Beyond the issue
- The lookup may not need a new scope. Hardcover gates the root
reading_journalsquery behindread:journal(capability-scopes.json), but its schema also exposes journals onuser_books(schema.graphql:22818), a row every push already reads (hardcover-graphql.ts:87). Whether that path needs the scope is untested (U1). - Quitting mid-push loses the record (②). Closing a book starts the Hardcover push without waiting (ReaderContent.tsx:263), while the Notion push is awaited up to 3 s (:273-276), and a push writes its rows only in its
finally(HardcoverClient.ts:890-901). - The queue that stops overlapping pushes lives and dies with one reader (③). It is a
useRef(useHardcoverSync.ts:88-95); Notion's equivalent is module-level under a cross-window Web Lock (useNotionSync.ts:13-31). Requests wait at a shared gate for up to 60 s (rateLimit.ts:28), which fits "within a minute or so". - Re-import duplicates every note at once, not one (④). Importing a new version merges the old notes, same ids, into a book with a new hash (bookService.ts:390-460), and the record is keyed by book hash (migrations/index.ts:54-61).
- A fifth path none of the four questions reaches (⑤). A removed highlight's entry stays on Hardcover, and a highlight made again on the same passage gets a new id whose payload hash differs (
action_at, HardcoverClient.ts:716-723), so it is inserted beside the old one; with Copy to Notebook on, copying a highlighted passage does the same through an excerpt. Both happen on one device with manual pushes only.
What each answer implies
The four questions are the maintainer's, from the 2026-10-20 comment. Under B, land only the row the reporter confirms, one PR per row (acceptance.md: one PR, one goal).
| Answer | Path | B: per-path fix | Test that fails today (proposed) |
|---|---|---|---|
| Q1 yes: another device, or the web app in a browser, with Hardcover on | ① | None local: each install keeps its own record. A, or the maintainer's choice to tell users to push notes from one device. | A's two-store test (below) |
| Q3 yes: quit within a minute of a push or a highlight, with or without Auto Sync | ② | Write rows after each batch, as Notion does, which covers manual pushes; with Auto Sync on (Q2), also return the push from the flush-hardcover-sync handler and await it on close with a cap, as Notion does (NotionClient.ts:603-631). | Shared-DB harness: 12 notes, the second batch's request never resolves → the DB holds the first 10 rows (0 today). |
| Q3 yes: closed and reopened the book soon after | ③ | Queue notes pushes per book hash at module scope, under the same Web Lock pattern as Notion. | Hook test: readers h1-view1 and h1-view2 push at once → at most 1 active (2 today). |
| Q4 yes: re-imported or updated the file | ④ | Move the book's rows to the surviving hash when books merge, or A. | Merge test: rows follow the surviving hash (none today). |
| Not asked yet: was that highlight removed and made again, or is Copy to Notebook on? | ⑤ | Re-made: a removed note's entry stays on Hardcover (removed notes are dropped before classification, HardcoverClient.ts:798-800) and the new note hashes differently (its action_at, :722), so B deletes the entry of each note that has a row and deletedAt on the next push, as Notion archives a removed note's blocks (NotionClient.ts:805-811); delete_reading_journal needs only write:library, which sign-in already asks for (capability-scopes.json:1198-1203). Whether removing a highlight should remove its Hardcover entry is the maintainer's call. Copy to Notebook (off by default, constants.ts:577) adds an excerpt with a new id (Annotator.tsx:1511-1513), and the filter drops excerpts only beside a note-bearing highlight (HardcoverClient.ts:813-824): drop them beside any highlight with the same location and text. | A plain highlight and an excerpt with the same location and text → 1 insert (2 today). A note with a row and deletedAt → one delete_reading_journal for its journal id (none today). |
| All five no | ? | One more source path: an update answered with an error containing "not found", "does not exist" or "null value" is re-sent as an insert (HardcoverClient.ts:740-747, :770-772); if the entry still exists, that duplicates it. Unpinned against the live API (U4). | Needs U4 first. |
Option A: contributor path
- Close U1 and U2 before code with a test Hardcover account in the API console: does
books(where:{id:{_eq:$id}}){ user_books(where:{user_id:{_eq:$uid}}){ reading_journals(where:{event:{_in:["quote","note"]}}){ id event entry } } }answer with a token holding only Readest's scopes, and does an entry Readest inserted read back byte-for-byte? If the nested read is refused, the query becomes rootreading_journalsfiltered byuser_idandbook_id, plus the scope (only on a "yes" to the question above). src/services/hardcover/hardcover-graphql.ts: one new read query (the shape step 1 proved).src/services/hardcover/HardcoverClient.tssyncBookNotes: after classification (:838-888) and only when an insert is pending, read the book's quote and note entries once; give each pending insert the first entry with the sameeventandentrythat no row of a live note on this device and no earlier insert in this push already uses, save its rows as the existing payload-hash fallback does (:852-861), and count it as skipped. On any lookup error, log it and insert as today.src/services/hardcover/hardcoverOAuth.ts:14: addread:journalonly if step 1 requires it and the maintainer said "add it".
| Requirement | Test (proposed, src/__tests__/services/hardcover/HardcoverClient.test.ts, batching suite) | Today |
|---|---|---|
| R1 A note pushed from one store is not inserted again from another | Shared-DB harness (test.ts:1314-1362) with two separate databases: client 1 pushes 2 notes; client 2 (empty DB), with the lookup mock returning those 2 entries, pushes → 0 inserts, 2 rows saved | fails: 2 inserts |
| R2 Each Hardcover entry is used once | 2 pending inserts with the same text and different pages, 1 matching entry → 1 link, 1 insert; an entry in the row of a live note is not reused | fails: 2 inserts |
| R6 A re-made highlight reuses its old entry | a removed note keeps its row; a new note with the same text and that entry on Hardcover → link, no insert | fails: 1 insert |
| R3 Event must match | entry text equal, event quote vs note → insert | control |
| R4 Refused lookup keeps today's behaviour | lookup answers 403 insufficient_scope → inserts, push succeeds | guard |
| R5 No lookup without pending inserts | all notes have rows → no lookup request sent | guard |
Commands (from apps/readest-app): pnpm test --run src/__tests__/services/hardcover/HardcoverClient.test.ts, then pnpm test --run and pnpm lint. CI runs pnpm test:pr:web:unit in two shards (pull-request.yml:261-263).
Done when (acceptance.md): R1, R2 and R6 fail with the change reverted on an assertion naming the duplicate insert, then pass; R3-R5 pass; the verification.md checks pass on the change merged onto origin/main; real use on macOS against a test Hardcover account: push a highlight from the desktop app, then from the web app (a second record), and see one journal entry; the commit body credits "Reported by aysmack on GitHub".
Unknowns
| Unknown | Blocks | Closes with | |
|---|---|---|---|
| U1 | Whether user_books → reading_journals answers with today's scopes | whether A needs the scope question at all | one console query (step 1) |
| U2 | Whether Hardcover returns entry and event exactly as sent, and how many rows one read returns | A's match rule; paging for books with many entries | read back an inserted entry (step 1) |
| U3 | The reporter's answers | which B row, and whether A is needed for this report | the open questions on the issue |
| U4 | What Hardcover returns for an update to an existing vs a deleted entry | the "all four no" row | one update against a test entry, and one against a deleted entry |
Implementation notes
Why not carry the journal id on the synced note (option C, out)
Notes sync between devices by id (merge.ts:30-42), so a BookNote field would reach the other device, but only for users with Readest sync on, and only after the first device has pushed. In the maintainer's own scenario (Auto Sync on the phone pushes notes that arrive from the Mac) the phone can push before the id arrives. It also puts Hardcover state into the shared note type, which is hard to take back.
Option B details
- ②
useHardcoverSync.ts:273-283: the flush handler callsdebouncedAutoPushNotes.flush(), which drops the push promise (debounce.ts:44-53); Notion's handler cancels the debounce and awaits the push instead (useNotionSync.ts:136-148). The close-time push only runs when Auto Sync is on (useHardcoverSync.ts:239-247), which defaults off (constants.ts:120-125). Added cost: quitting waits up to the cap. Residual: a quit during the one request in flight still loses that entry's row. - Unchanged residual for A and for B without ③: two pushes in flight at once can both insert.
- ③ The queue must key on the book hash (the reader key carries a per-open suffix) and stay in the module, so it survives the reader unmounting. Added cost: a stuck push now also holds the next reader's pushes for that book.
- ④ Re-keying touches the import path (
mergeBooks), outside the Hardcover files; the Notion and BookOrbit stores key by book hash the same way, so the same question applies to them.
Paths checked and ruled out
- Edited highlight: it has a row, so a changed payload is an update (HardcoverClient.ts:877-887).
- Two pushes from one open reader: queued (useHardcoverSync.ts:88-95), and the sync-row tap and the book menu both go through that hook.
- A finished manual push: its rows are written before the toast, because
finallyruns beforesyncBookNotesreturns. - Re-linking to another Hardcover book clears the rows on purpose (HardcoverLinkDialog.tsx:132-136); linking away and back duplicates every note in the original book, which is "all at once", like ④.
Other facts used
- The record is one SQLite file per install,
hardcover-sync.db(HardcoverSyncMapStore.ts:11-12); the web app keeps its own copy in browser storage (#4083). - Settings sync carries only
hardcover.accessToken(settings.ts:75); the OAuth session is device-local (types/settings.ts:173), so a second device needs its own Hardcover connection before ① can happen. - The fix linked from the issue does not apply: #6938 (merged 2026-10-20, after v0.12.16) only changes the legacy localStorage migration, and #4083 says localStorage was the Web store; the desktop app already used SQLite.
- The per-reader queue, shared gate and per-push reload arrived in #6617 (merged 2026-10-04), which is in v0.12.16, the reporter's version.
- Hardcover's Free plan allows 60 requests a minute, burst 10; a missing scope answers 403
insufficient_scopenaming the scope (Getting-Started.mdx:88, :160-167). A's lookup costs one request, only on pushes that insert. - Notion already checks the remote before re-writing a note (NotionClient.ts:694-759): A is the same idea for Hardcover.
- Pasted tokens: Readest still accepts one (types/settings.ts:171). Tokens made before August 2026 carry
alland get the lookup unchanged; a newer scoped token withoutread:journalfalls back to today's insert (R4) until the user makes a new one.

