Skip to content

feat(library): merge a same-kind upload into the item already at its key - #67

Open
GianniCarlo wants to merge 1 commit into
mainfrom
feat/same-name-merge-uploads
Open

GianniCarlo wants to merge 1 commit into
mainfrom
feat/same-name-merge-uploads

Conversation

@GianniCarlo

Copy link
Copy Markdown
Contributor

Step 2 of docs/same-name-merges.md: PUT / records merges. Builds on #66, which made every lookup by uuid follow a merge pointer.

Why

PUT / onto a key another item already holds answers 200 with that item and drops the app's uuid. Neither app reads the uuid in that reply, so its next request by its own uuid (a /move, most often) gets item_not_found and holds its sync queue for good. It happens when the same book, or a same-named folder, is added on two devices before either has synced.

What changes

LibraryService.mergeUploadIntoOccupant, called when PUT / finds its key taken:

The upload Result
A book onto a folder or bound book (or the reverse) Today's behaviour. They are never one item; that clash goes away with uuid-based placement and is fixed by hand until then (see the doc)
A uuid that has an active row of its own elsewhere Today's behaviour. It is a move onto a taken key, which the merge rule for key rewrites (step 3) will cover
Same kind, onto a row from before uuids The row takes the uploaded uuid, as POST /uuids already does
Same kind, any other uuid Merge. The item already there keeps its uuid; the uploaded uuid is recorded in library_item_merges (a retry re-records it); the upload's listening state replaces the survivor's only when it was played later; the reply lists the merge

The reply is now { content, conflicts }. conflicts uses the POST /uuids shape, [{ key: <uuid as sent>, uuid: <survivor's uuid> }], which both apps already decode and apply to their library and queued tasks. It is empty unless the upload merged, and today's apps ignore it. Until the apps read it, the pointer keeps them working, and their weekly missing-items pass moves them to the survivor's uuid.

POST /uuids records no pointer (decided with the maintainer): both apps already switch to the uuid its conflicts name, so nothing would use one, and it is sent paths without item kinds, so it could record a book-vs-folder pointer.

Supporting changes: LibraryDB.adoptUuidIfMissing, recordMerge (upsert) and setItemProgress; putObject returns PutObjectResult; docs/same-name-merges.md and CLAUDE.md describe the reply.

Testing

  • src/__tests__/services/LibraryServiceUploadMerge.test.ts, 11 cases:
    • book onto book;
    • folder onto bound book;
    • book vs folder left alone, including on a row from before uuids;
    • a uuid with its own row left alone;
    • a legacy row taking the uuid;
    • the same uuid changing nothing;
    • a retry keeping one pointer;
    • newer and older listening state;
    • the stuck case end to end: the folder merges on upload, then the app's move into it by its own uuid works.
  • Four existing tests updated to read content.
  • Full suite locally on a throwaway Postgres 17: 45 suites, 520 tests passing.

🤖 Generated with Claude Code

Step 2 of docs/same-name-merges.md. PUT / onto a key another item holds used
to answer 200 with that item and drop the app's uuid, so the app's next
request by its uuid (a move, most often) got item_not_found for good.

Now, when the upload is the same kind of item (two books, or two containers)
under a uuid with no active row of its own, it merges into the item already
there: that item keeps its uuid, the uploaded uuid is recorded as a pointer to
it (which PR 1's lookups follow), the upload's listening state replaces the
survivor's only when it was played later, and the reply lists the merge as
conflicts: [{ key, uuid }], the shape POST /uuids already answers with and both
apps already apply. A row from before uuids takes the uploaded uuid of the same
kind, as POST /uuids does. A book against a container, or a uuid that has an
active row elsewhere, keeps today's behaviour. POST /uuids records no pointer:
the apps already adopt its conflicts, and it carries no item kinds.

putObject now returns { content, conflicts }; every PUT / answers conflicts,
empty unless it merged.
Comment thread src/services/LibraryService.ts Resolved
@github-actions

Copy link
Copy Markdown

✅ Claude PR Review — PASS

This PR adds LibraryService.mergeUploadIntoOccupant, called when PUT / lands on a key another item already holds. Uploads of the same kind are merged: a pointer goes into library_item_merges (an upsert, so a retry is safe), and the upload's listening state replaces the survivor's only when it was played later. A row from before uuids takes the uploaded uuid instead. The reply changes to { content, conflicts }.

Authorization: every new query is scoped to user.id_user, and that includes adoptUuidIfMissing, recordMerge and setItemProgress. into_item_id always comes from the occupant row, which getLibrary(user.id_user, …) returned, so one user can't point a uuid at another user's row. The route and its middleware are unchanged.

Correctness: the pointer write and the progress update share one transaction that rolls back on error. The 'newer play wins' check is the same as withoutStaleProgress. The 'no active row of its own' check uses getItemsByUuids, which does not follow pointers, so a retried upload merges again instead of being taken for a move. The only other caller of putObject is the controller, and it is updated. There are no migrations, secrets or IAP/passkey changes. One minor point is below.

Findings: 1 info

Model claude-opus-5-5 · run log · 1 new · 0 carried over · 0 resolved · advisory (a human should still review). Findings are de-duplicated across pushes; an earlier finding closes only when the verification pass judges it against the current code — fixed, no longer applicable, accepted by a maintainer, or a duplicate of a finding reported on this push.

This branch was successfully deployed

1 active deployment
reviewer — 9614cce9 Deployed Nov 27, 2026 by GianniCarlo via review #141
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant