fix(docsync): stop a push from stealing another file's pairing

Papra answers a push of bytes it already holds with the document that has
them. Two trip files with the same content therefore pushed to one document,
and the unique index on (link_id, remote_id) aborted that run and every run
after it with a constraint error. The pairing now stays with the file that
owns it and the second file is recorded as a conflict.

Also in this pass:
- Paperless no longer adopts a same-bytes document that lacks the scope's tag
  when a consume outlives the poll budget; that document belongs to someone
  else's filing and delete-through would have trashed it.
- The pull path writes the file row, the retirement of the copy it replaces
  and the pairing in one transaction, and announces them only once committed.
- PATCH links stops accepting remoteRootId/remoteRootPath, which it read and
  silently dropped.
- The MCP sync tool clears shelved items before running, as the REST route
  does.
This commit is contained in:
Maurice
2026-09-19 00:44:43 +02:00
parent 7237a1881d
commit e6a5ca1521
11 changed files with 267 additions and 39 deletions
+8 -3
View File
@@ -329,9 +329,14 @@ export function planReconcile(input: ReconcileInput): ReconcilePlan {
// `both` as "TREK always wins" silently renamed the provider's copy back
// every time somebody tidied up a folder, which is the opposite of a
// two-way sync.
if (l && r.name !== l.name) {
const providerRenamed = it.remoteName !== null && r.name !== it.remoteName;
const localRenamed = it.remoteName !== null && l.name !== it.remoteName;
if (l && sanitizeIncomingName(r.name) !== l.name) {
// Compared through the same sanitiser the local name went through, or a
// provider name TREK had to clean up (a slash, a control character, a
// leading dot) reads as "TREK renamed this" on the very next run — and
// the provider's copy gets renamed to the cleaned version, unasked.
const agreed = it.remoteName === null ? null : sanitizeIncomingName(it.remoteName);
const providerRenamed = agreed !== null && sanitizeIncomingName(r.name) !== agreed;
const localRenamed = agreed !== null && l.name !== agreed;
if (providerRenamed && !localRenamed && direction !== 'push') {
add({ kind: 'rename_local', itemId: it.id, fileId: l.fileId, name: r.name });