From 82adf2c881e557bd6e045ddbafa9d2f5c879f141 Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 30 Nov 2026 11:21:46 +0100 Subject: [PATCH 1/6] Paste world takes the number off each note it replaces with other bytes (a Save is refused as stale) A pasted note's text is the clipboard's, possibly older than the world's, and its recorded change number was the pull's: a Save sent the older text under a number that still matched. The number goes to 0 for a replaced note; bytes left as they were keep it. --- Commands/PasteWorldCommand.ts | 9 +++++++++ test/paste-copies.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Commands/PasteWorldCommand.ts b/Commands/PasteWorldCommand.ts index ba22c1c..5215cff 100644 --- a/Commands/PasteWorldCommand.ts +++ b/Commands/PasteWorldCommand.ts @@ -10,6 +10,7 @@ import { pickNoteForId, worldNoteIds, writeElementNote, type WrittenNote } from import { KeptCopies, stoppedMessage } from '../vault/kept-copy'; import { copiedMessage, leftAsTypedMessage } from '../vault/note-mark-core'; import { isHiddenId, isStubNote, type HiddenListHost } from '../sync/hidden-on-server'; +import { readChangeSeqIndex, recordChangeSeqs } from '../vault/change-seq-index'; import { knownOfKeptText, knownOfWrite, noteStateOf } from '../sync/known-of'; export class PasteWorldCommand { @@ -135,6 +136,9 @@ export class PasteWorldCommand { const copies = await KeptCopies.open(this.app, worldName, (p) => this.markSelfWrite?.(p)); // Each element's note wherever it sits in the world, found as Download finds it. const worldIds = await worldNoteIds(this.app, worldName); + // The numbers a Save sends as If-Match, read once: a note this replaces with other bytes loses its number (#2454). + const numbers = await readChangeSeqIndex(this.app, worldName); + const unnumbered: Record = {}; const left: string[] = []; let stopped: { label: string; error: unknown } | null = null; try { @@ -211,9 +215,14 @@ export class PasteWorldCommand { } if (wrote.text === null) { left.push(label); continue; } copy.landed(); + // The clipboard's text may be older than the world's, and the number was the pull's, not the clipboard's: a + // Save of these bytes under it would match and write over newer text. 0 is behind every element's change, so the + // Save is refused as stale (keel's 412) until a Download brings the world's copy. Bytes left as they were keep it. + if (current !== null && wrote.text !== current && typeof numbers[element.id] === 'number') unnumbered[element.id] = 0; } } } finally { + await recordChangeSeqs(this.app, worldName, unnumbered); if (copies.copied.length > 0) new Notice(copiedMessage(copies.copied, 'Paste world'), 20000); if (left.length > 0) new Notice(leftAsTypedMessage(left, 'Paste world'), 20000); } diff --git a/test/paste-copies.test.ts b/test/paste-copies.test.ts index 69b7484..258125c 100644 --- a/test/paste-copies.test.ts +++ b/test/paste-copies.test.ts @@ -165,3 +165,27 @@ test("★ a hidden stub outside the type folders is found and never written over assert.equal(vault.contentAt(CAST), stub, "a pasted old copy was written over a hidden stub"); assert.equal(vault.contentAt(NOTE), undefined); }); + +// #2454: a Save sends the index's number as If-Match. A pasted note's text is the clipboard's, possibly older than the world's, so +// the number the note was pulled at no longer describes its bytes: a Save would send the older text under a number that still matches. +test("★ #2454 Paste world takes the number off each note it replaces, so a Save of older text is refused, not matched", async () => { + const { vault, dl, paste } = await world(); + await dl.generateElementNotes(ELEMENTS, { Character: [{ id: ID, name: "Ireena", description: "A noble." }, { id: OTHER, name: "Kolyan", description: "A priest." }] }, false); + vault.dotfiles.set(SEQ, JSON.stringify({ ...index(vault), [ID]: 7, [OTHER]: 9 })); + // Only Ireena is in the clipboard; Kolyan's note is not replaced. + await paste.generateElementNotes(ELEMENTS, el("Older text."), false); + assert.match(vault.contentAt(NOTE) ?? "", /Older text\./); + assert.equal(index(vault)[ID], 0, "the replaced note still carries the number its older bytes were not pulled at"); + assert.equal(index(vault)[OTHER], 9, "a note Paste world did not replace lost its number"); +}); + +test("#2454 a paste that leaves a note's bytes as they were keeps its number", async () => { + const { vault, dl, paste } = await world(); + await dl.generateElementNotes(ELEMENTS, el("A noble."), false); + await paste.generateElementNotes(ELEMENTS, el("A noble."), false); + vault.dotfiles.set(SEQ, JSON.stringify({ ...index(vault), [ID]: 7 })); + const note = vault.contentAt(NOTE) ?? ""; + await paste.generateElementNotes(ELEMENTS, el("A noble."), false); + assert.equal(vault.contentAt(NOTE), note, "the same paste changed the note"); + assert.equal(index(vault)[ID], 7, "a paste that left the note's bytes as they were took its number"); +}); From 2a12c192bea07fa6eefac81c3ecfa59034b43bbc Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 30 Nov 2026 14:37:47 +0100 Subject: [PATCH 2/6] Paste world numbers each note it writes 0 at once; Download pulls from the start while one holds it A cold read found the first version left a pasted note stuck at 0 for good when the world had not changed since the last pull, and left a note made for an id with a number or none untouched. The number is now recorded per note, for every note written with other bytes, and a Download pulls from the start while an id holds it. --- Commands/DownloadWorldCommand.ts | 7 ++++++- Commands/PasteWorldCommand.ts | 15 ++++++--------- test/copy-edges.test.ts | 22 ++++++++++++++++++++++ test/paste-copies.test.ts | 25 +++++++++++++++++++++++++ vault/change-seq-index.ts | 13 +++++++++++++ 5 files changed, 72 insertions(+), 10 deletions(-) diff --git a/Commands/DownloadWorldCommand.ts b/Commands/DownloadWorldCommand.ts index 01ddc78..3bd9e8b 100644 --- a/Commands/DownloadWorldCommand.ts +++ b/Commands/DownloadWorldCommand.ts @@ -21,7 +21,7 @@ import { Category } from '../enums'; import { V2ApiError, V2Change, V2Client, V2_ORIGIN } from '../client-v2'; import { recordServerWorldId } from '../vault/world-id-marker'; import { readFolderSidecar, writeFolderSidecar } from '../vault/folder-sidecar'; -import { asChangeSeq, changeSeqOf, UNREADABLE_FLAG, heldKey, readChangeSeqIndex, recordChangeSeqs } from '../vault/change-seq-index'; +import { asChangeSeq, changeSeqOf, holdsPastedSeq, UNREADABLE_FLAG, heldKey, readChangeSeqIndex, recordChangeSeqs } from '../vault/change-seq-index'; import { groupKeysById, groupMarkClears, groupMarkEntries, copiedMessage, leftAsTypedMessage, notWrittenMessage, markKey, noteMark, removedGroupKeys } from '../vault/note-mark-core'; import { inKeptFolder, KeptCopies, KeptCopyFailed, stoppedMessage } from '../vault/kept-copy'; import type OnlyWorldsPlugin from '../main'; @@ -124,6 +124,11 @@ export class DownloadWorldCommand { console.log('OnlyWorlds: vault has no notes for this world — ignoring sync cursor, full download.'); stored = undefined; } + // A note Paste world wrote holds the number 0 until a pull brings the world's own: the feed from the cursor + // holds only what changed since, so an element that did not would keep its 0 and its Saves would be refused for good. + if (stored && holdsPastedSeq(await readChangeSeqIndex(this.app, await this.worldService.generateUniqueWorldName(worldName, apiKeyInput.trim())))) { + stored = undefined; + } // An install from before the list (a stored cursor, no // knownOnServer entry for this world) walks the feed from // the start once, for the list alone: nothing else of that diff --git a/Commands/PasteWorldCommand.ts b/Commands/PasteWorldCommand.ts index 5215cff..af764d5 100644 --- a/Commands/PasteWorldCommand.ts +++ b/Commands/PasteWorldCommand.ts @@ -10,7 +10,7 @@ import { pickNoteForId, worldNoteIds, writeElementNote, type WrittenNote } from import { KeptCopies, stoppedMessage } from '../vault/kept-copy'; import { copiedMessage, leftAsTypedMessage } from '../vault/note-mark-core'; import { isHiddenId, isStubNote, type HiddenListHost } from '../sync/hidden-on-server'; -import { readChangeSeqIndex, recordChangeSeqs } from '../vault/change-seq-index'; +import { PASTED_SEQ, recordChangeSeqs } from '../vault/change-seq-index'; import { knownOfKeptText, knownOfWrite, noteStateOf } from '../sync/known-of'; export class PasteWorldCommand { @@ -136,9 +136,6 @@ export class PasteWorldCommand { const copies = await KeptCopies.open(this.app, worldName, (p) => this.markSelfWrite?.(p)); // Each element's note wherever it sits in the world, found as Download finds it. const worldIds = await worldNoteIds(this.app, worldName); - // The numbers a Save sends as If-Match, read once: a note this replaces with other bytes loses its number (#2454). - const numbers = await readChangeSeqIndex(this.app, worldName); - const unnumbered: Record = {}; const left: string[] = []; let stopped: { label: string; error: unknown } | null = null; try { @@ -215,14 +212,14 @@ export class PasteWorldCommand { } if (wrote.text === null) { left.push(label); continue; } copy.landed(); - // The clipboard's text may be older than the world's, and the number was the pull's, not the clipboard's: a - // Save of these bytes under it would match and write over newer text. 0 is behind every element's change, so the - // Save is refused as stale (keel's 412) until a Download brings the world's copy. Bytes left as they were keep it. - if (current !== null && wrote.text !== current && typeof numbers[element.id] === 'number') unnumbered[element.id] = 0; + // The clipboard's text may be older than the world's, and any number the index holds for this id was the pull's, + // not the clipboard's: a Save of these bytes under it would match and write over newer text. PASTED_SEQ is behind + // every element's change, so Save is refused as stale until a Download (which pulls from the start while any id + // holds it) brings the world's copy. Recorded now, before the next note, not at the end. Bytes left as they were keep their number. + if (wrote.text !== current) await recordChangeSeqs(this.app, worldName, { [element.id]: PASTED_SEQ }); } } } finally { - await recordChangeSeqs(this.app, worldName, unnumbered); if (copies.copied.length > 0) new Notice(copiedMessage(copies.copied, 'Paste world'), 20000); if (left.length > 0) new Notice(leftAsTypedMessage(left, 'Paste world'), 20000); } diff --git a/test/copy-edges.test.ts b/test/copy-edges.test.ts index d6d44fc..d4c9650 100644 --- a/test/copy-edges.test.ts +++ b/test/copy-edges.test.ts @@ -11,6 +11,7 @@ import type { App } from "obsidian"; import { makeApp, setRequestUrl, __notices } from "./support/obsidian-mock"; import type { MockVault } from "./support/obsidian-mock"; import { DownloadWorldCommand } from "../Commands/DownloadWorldCommand"; +import { PasteWorldCommand } from "../Commands/PasteWorldCommand"; import { markKey, noteMark } from "../vault/note-mark-core"; const WID = "019e0000-0000-7000-8000-0000000005ee"; @@ -222,3 +223,24 @@ test("★ D3: a note copied then left as the person has it (it changed under eac assert.ok(!r.notices.some((x) => x.includes("could not tell were unedited")), r.notices.join(" | ")); assert.ok(r.notices.some((x) => x.includes("did not write")), r.notices.join(" | ")); }); + +test("★ #2454 a Download after a Paste world pulls from the start, so a note the world has not changed gets its number back", async () => { + const s = fresh(); + await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3); + assert.equal(index(s.vault)[IRE], 3); + // Paste an OLDER clipboard over the note: its number is 0, and the world has not changed since the pull. + const paste = new PasteWorldCommand(s.app, {} as never); + await paste.generateElementNotes(`${W}/Elements`, { Character: [{ id: IRE, name: "Ireena", description: "Older text." }] }, false); + assert.equal(index(s.vault)[IRE], 0); + // The feed from the cursor is empty: only a walk from the start brings the element again. + const r = await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3, []); + assert.equal(index(s.vault)[IRE], 3, "the note kept the pasted number: every Save of it would be refused as stale, for good. " + r.notices.join(" | ")); + assert.match(s.vault.contentAt(IRE_NOTE) ?? "", /Current text\./); +}); + +test("#2454 a Download with no pasted number still walks from its cursor", async () => { + const s = fresh(); + await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3); + await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3, [whole(IRE, "Ireena", 4, "Changed.")]); + assert.equal(index(s.vault)[IRE], 4, "the pull from the cursor brought the one change"); +}); diff --git a/test/paste-copies.test.ts b/test/paste-copies.test.ts index 258125c..63c9e79 100644 --- a/test/paste-copies.test.ts +++ b/test/paste-copies.test.ts @@ -189,3 +189,28 @@ test("#2454 a paste that leaves a note's bytes as they were keeps its number", a assert.equal(vault.contentAt(NOTE), note, "the same paste changed the note"); assert.equal(index(vault)[ID], 7, "a paste that left the note's bytes as they were took its number"); }); + +test("★ #2454 a note Paste world makes for an id that holds a number, or none, takes the number 0 too", async () => { + const { vault, paste } = await world(); + vault.dotfiles.set(SEQ, JSON.stringify({ [ID]: 7 })); + await paste.generateElementNotes(ELEMENTS, { Character: [{ id: ID, name: "Ireena", description: "Older." }, { id: OTHER, name: "Kolyan", description: "New here." }] }, false); + assert.equal(index(vault)[ID], 0, "a note made for an id that held a number kept it: the note could be saved under it"); + assert.equal(index(vault)[OTHER], 0, "a note made for an id with no number can be saved with no check at all"); +}); + +test("★ #2454 a paste stopped at a later note leaves the notes it wrote before at 0", async () => { + const { app, vault, dl, paste } = await world(); + await dl.generateElementNotes(ELEMENTS, { Character: [{ id: ID, name: "Ireena", description: "A noble." }, { id: OTHER, name: "Kolyan", description: "A priest." }] }, false); + vault.dotfiles.set(SEQ, JSON.stringify({ ...index(vault), [ID]: 7, [OTHER]: 9 })); + typeInto(vault, OTHER_NOTE, "Typed by a person."); + const v = app.vault as unknown as Vaultish; + const create = v.create.bind(v); + v.create = async (p, c) => { + if (p.startsWith(KEPT)) throw new Error("the disk is full"); + return create(p, c); + }; + const r = await paste.generateElementNotes(ELEMENTS, { Character: [{ id: ID, name: "Ireena", description: "Pasted." }, { id: OTHER, name: "Kolyan", description: "Pasted too." }] }, false); + assert.match(r.stopped ?? "", /^Paste world stopped at Character "Kolyan"/); + assert.equal(index(vault)[ID], 0, "the note written before the stop kept its number"); + assert.equal(index(vault)[OTHER], 9, "the note the stop left unchanged lost its number"); +}); diff --git a/vault/change-seq-index.ts b/vault/change-seq-index.ts index 919187e..e4ca789 100644 --- a/vault/change-seq-index.ts +++ b/vault/change-seq-index.ts @@ -48,6 +48,19 @@ export function heldKey(id: string): string { return `${id}^`; } +/** + * The number Paste world gives each note it writes with other bytes than it held: 0, behind every element's change_seq (keel's + * start at 1), so a Save is refused as stale and the person is sent to Download. A pasted text may be older than the world's, and + * the number the note had was the pull's, not the clipboard's. Download pulls from the start while any id holds it, so the + * number is replaced by the world's own even for an element that has not changed on the server since. + */ +export const PASTED_SEQ = 0; + +/** True when an id of the index holds PASTED_SEQ: a note Paste world wrote that no Download has since brought the world's number to. */ +export function holdsPastedSeq(index: ChangeSeqIndex): boolean { + return Object.entries(index).some(([key, seq]) => seq === PASTED_SEQ && !/[#^]$/.test(key) && key !== UNREADABLE_FLAG); +} + /** The world's index, or an empty one when absent or unreadable. */ export async function readChangeSeqIndex(app: App, world: string): Promise { const path = indexPath(world); From e37156792666cb41d43848bc09b14af33297ea64 Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 30 Nov 2026 14:42:57 +0100 Subject: [PATCH 3/6] Download after Paste takes only the pasted ids' rows from a full walk; Paste batches its writes The second cold read found the cold walk lost a pending view resync, replaced every typed-into note of the world, and left ids the feed never names at 0 for good. The incremental walk stays; rows of the ids at 0 are added from a walk from the start; an id still at 0 after a complete run is cleared. Paste writes its zeros 25 notes at a time. --- Commands/DownloadWorldCommand.ts | 29 +++++++++++++++++++++++------ Commands/PasteWorldCommand.ts | 20 ++++++++++++++++++-- test/copy-edges.test.ts | 16 ++++++++++++++++ vault/change-seq-index.ts | 6 +++--- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/Commands/DownloadWorldCommand.ts b/Commands/DownloadWorldCommand.ts index 3bd9e8b..c85a746 100644 --- a/Commands/DownloadWorldCommand.ts +++ b/Commands/DownloadWorldCommand.ts @@ -21,7 +21,7 @@ import { Category } from '../enums'; import { V2ApiError, V2Change, V2Client, V2_ORIGIN } from '../client-v2'; import { recordServerWorldId } from '../vault/world-id-marker'; import { readFolderSidecar, writeFolderSidecar } from '../vault/folder-sidecar'; -import { asChangeSeq, changeSeqOf, holdsPastedSeq, UNREADABLE_FLAG, heldKey, readChangeSeqIndex, recordChangeSeqs } from '../vault/change-seq-index'; +import { asChangeSeq, changeSeqOf, pastedSeqIds, UNREADABLE_FLAG, heldKey, readChangeSeqIndex, recordChangeSeqs } from '../vault/change-seq-index'; import { groupKeysById, groupMarkClears, groupMarkEntries, copiedMessage, leftAsTypedMessage, notWrittenMessage, markKey, noteMark, removedGroupKeys } from '../vault/note-mark-core'; import { inKeptFolder, KeptCopies, KeptCopyFailed, stoppedMessage } from '../vault/kept-copy'; import type OnlyWorldsPlugin from '../main'; @@ -124,11 +124,10 @@ export class DownloadWorldCommand { console.log('OnlyWorlds: vault has no notes for this world — ignoring sync cursor, full download.'); stored = undefined; } - // A note Paste world wrote holds the number 0 until a pull brings the world's own: the feed from the cursor - // holds only what changed since, so an element that did not would keep its 0 and its Saves would be refused for good. - if (stored && holdsPastedSeq(await readChangeSeqIndex(this.app, await this.worldService.generateUniqueWorldName(worldName, apiKeyInput.trim())))) { - stored = undefined; - } + // A note Paste world wrote holds the number 0 until a pull brings the world's own. The feed from the cursor + // holds only what changed since, so the rows of those ids are taken from a walk from the start and added to it + // (below): nothing else of that walk is applied, so a typed-into note of another element is not touched. + const pastedIds = pastedSeqIds(await readChangeSeqIndex(this.app, await this.worldService.generateUniqueWorldName(worldName, apiKeyInput.trim()))); // An install from before the list (a stored cursor, no // knownOnServer entry for this world) walks the feed from // the start once, for the list alone: nothing else of that @@ -165,6 +164,16 @@ export class DownloadWorldCommand { } } + if (!fromStart && pastedIds.size > 0) { + try { + const rows = (await client.changesWalk()).changes.filter(c => pastedIds.has(c.id)); + const have = new Set(walk.changes.map(c => c.id)); + walk = { ...walk, changes: [...rows.filter(c => !have.has(c.id)), ...walk.changes] }; + } catch (e) { + console.warn('OnlyWorlds: could not read the feed for the pasted notes; the next Download tries again.', e); + } + } + // Fold the change stream into per-category buckets. Changes // arrive in sequence order, so later ops win per element id. const latest = new Map(); @@ -340,6 +349,14 @@ export class DownloadWorldCommand { // a write that threw). The lists are held with the cursor: the // next walk of the same changes derives the same stubs from them. const holdCursor = knownOf.left.length + knownOf.failed.length + stubbed.left.length + stubbed.failed.length > 0; + // A pasted id the walk brought no number for (no row: a note the feed does not name, an element never sent) loses + // its 0 once every note landed: Save then creates it or finds it gone, and sends no If-Match. + if (pastedIds.size > 0 && !holdCursor) { + const now = await readChangeSeqIndex(this.app, uniqueWorldName); + const unfinished: Record = {}; + for (const id of pastedIds) if (now[id] === 0) unfinished[id] = null; + await recordChangeSeqs(this.app, uniqueWorldName, unfinished); + } if (this.plugin && walk.cursor && !holdCursor) { this.plugin.settings.syncCursors[worldId] = { cursor: walk.cursor, head: walk.head }; this.plugin.settings.deletedOnServer = { diff --git a/Commands/PasteWorldCommand.ts b/Commands/PasteWorldCommand.ts index af764d5..31fe567 100644 --- a/Commands/PasteWorldCommand.ts +++ b/Commands/PasteWorldCommand.ts @@ -13,6 +13,9 @@ import { isHiddenId, isStubNote, type HiddenListHost } from '../sync/hidden-on-s import { PASTED_SEQ, recordChangeSeqs } from '../vault/change-seq-index'; import { knownOfKeptText, knownOfWrite, noteStateOf } from '../sync/known-of'; +/** How many notes' numbers Paste world records in one write of the index. */ +const PASTE_BATCH = 25; + export class PasteWorldCommand { app: App; manifest: any; @@ -136,6 +139,15 @@ export class PasteWorldCommand { const copies = await KeptCopies.open(this.app, worldName, (p) => this.markSelfWrite?.(p)); // Each element's note wherever it sits in the world, found as Download finds it. const worldIds = await worldNoteIds(this.app, worldName); + // The 0 of each note written, recorded every PASTE_BATCH notes and at the end: one write of the index per note would read and + // rewrite the whole index n times. + const pasted: Record = {}; + const flushPasted = async () => { + if (Object.keys(pasted).length === 0) return; + const entries = { ...pasted }; + for (const id of Object.keys(pasted)) delete pasted[id]; + await recordChangeSeqs(this.app, worldName, entries); + }; const left: string[] = []; let stopped: { label: string; error: unknown } | null = null; try { @@ -215,11 +227,15 @@ export class PasteWorldCommand { // The clipboard's text may be older than the world's, and any number the index holds for this id was the pull's, // not the clipboard's: a Save of these bytes under it would match and write over newer text. PASTED_SEQ is behind // every element's change, so Save is refused as stale until a Download (which pulls from the start while any id - // holds it) brings the world's copy. Recorded now, before the next note, not at the end. Bytes left as they were keep their number. - if (wrote.text !== current) await recordChangeSeqs(this.app, worldName, { [element.id]: PASTED_SEQ }); + // holds it) brings the world's copy. Bytes left as they were keep their number. + if (wrote.text !== current) { + pasted[element.id] = PASTED_SEQ; + if (Object.keys(pasted).length >= PASTE_BATCH) await flushPasted(); + } } } } finally { + await flushPasted(); if (copies.copied.length > 0) new Notice(copiedMessage(copies.copied, 'Paste world'), 20000); if (left.length > 0) new Notice(leftAsTypedMessage(left, 'Paste world'), 20000); } diff --git a/test/copy-edges.test.ts b/test/copy-edges.test.ts index d4c9650..330ad8e 100644 --- a/test/copy-edges.test.ts +++ b/test/copy-edges.test.ts @@ -244,3 +244,19 @@ test("#2454 a Download with no pasted number still walks from its cursor", async await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3, [whole(IRE, "Ireena", 4, "Changed.")]); assert.equal(index(s.vault)[IRE], 4, "the pull from the cursor brought the one change"); }); + +test("★ #2454 that Download touches only the pasted notes: a typed-into note of another element stays, and a pasted id the feed lacks loses its 0", async () => { + const s = fresh(); + const both = [whole(IRE, "Ireena", 3, "Current text."), whole(KOL, "Kolyan", 4, "A priest.")]; + await download(s.app, s.plugin, both, 4); + const paste = new PasteWorldCommand(s.app, {} as never); + const GONE = "019e0000-0000-7000-8000-0000000005c9"; + await paste.generateElementNotes(`${W}/Elements`, { Character: [{ id: IRE, name: "Ireena", description: "Older text." }, { id: GONE, name: "Ghost", description: "Never sent." }] }, false); + typeInto(s.vault, KOL_NOTE, "Typed by a person."); + const typed = s.vault.contentAt(KOL_NOTE); + await download(s.app, s.plugin, both, 4, []); + assert.equal(index(s.vault)[IRE], 3, "the pasted note did not get the world's number"); + assert.equal(s.vault.contentAt(KOL_NOTE), typed, "a note of another element, typed into, was replaced by a pull that was only for the pasted ones"); + assert.equal(index(s.vault)[KOL], 4); + assert.ok(!(GONE in index(s.vault)), "an id the feed has no row for kept its 0: every Download would look for it again"); +}); diff --git a/vault/change-seq-index.ts b/vault/change-seq-index.ts index e4ca789..09603d1 100644 --- a/vault/change-seq-index.ts +++ b/vault/change-seq-index.ts @@ -56,9 +56,9 @@ export function heldKey(id: string): string { */ export const PASTED_SEQ = 0; -/** True when an id of the index holds PASTED_SEQ: a note Paste world wrote that no Download has since brought the world's number to. */ -export function holdsPastedSeq(index: ChangeSeqIndex): boolean { - return Object.entries(index).some(([key, seq]) => seq === PASTED_SEQ && !/[#^]$/.test(key) && key !== UNREADABLE_FLAG); +/** The ids of the index that hold PASTED_SEQ: notes Paste world wrote that no Download has since brought the world's number to. */ +export function pastedSeqIds(index: ChangeSeqIndex): Set { + return new Set(Object.entries(index).filter(([key, seq]) => seq === PASTED_SEQ && !/[#^]$/.test(key) && key !== UNREADABLE_FLAG).map(([key]) => key)); } /** The world's index, or an empty one when absent or unreadable. */ From 590afcba182005ed971d1363d08e237379a563f3 Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 30 Nov 2026 14:45:26 +0100 Subject: [PATCH 4/6] A pasted 0 is cleared only once its rows were read; the extra walk failing leaves it Third cold read: a failed extra walk still cleared the pasted ids, so the next Save went out with no check. Also a test for a paste of more notes than one batch. --- Commands/DownloadWorldCommand.ts | 5 ++++- test/copy-edges.test.ts | 15 +++++++++++++++ test/paste-copies.test.ts | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Commands/DownloadWorldCommand.ts b/Commands/DownloadWorldCommand.ts index c85a746..f3d7715 100644 --- a/Commands/DownloadWorldCommand.ts +++ b/Commands/DownloadWorldCommand.ts @@ -164,11 +164,14 @@ export class DownloadWorldCommand { } } + // The pasted ids' rows were read (a walk from the start, or the extra one below): only then may a 0 that no row replaced be cleared. + let pastedRowsRead = fromStart; if (!fromStart && pastedIds.size > 0) { try { const rows = (await client.changesWalk()).changes.filter(c => pastedIds.has(c.id)); const have = new Set(walk.changes.map(c => c.id)); walk = { ...walk, changes: [...rows.filter(c => !have.has(c.id)), ...walk.changes] }; + pastedRowsRead = true; } catch (e) { console.warn('OnlyWorlds: could not read the feed for the pasted notes; the next Download tries again.', e); } @@ -351,7 +354,7 @@ export class DownloadWorldCommand { const holdCursor = knownOf.left.length + knownOf.failed.length + stubbed.left.length + stubbed.failed.length > 0; // A pasted id the walk brought no number for (no row: a note the feed does not name, an element never sent) loses // its 0 once every note landed: Save then creates it or finds it gone, and sends no If-Match. - if (pastedIds.size > 0 && !holdCursor) { + if (pastedIds.size > 0 && pastedRowsRead && !holdCursor) { const now = await readChangeSeqIndex(this.app, uniqueWorldName); const unfinished: Record = {}; for (const id of pastedIds) if (now[id] === 0) unfinished[id] = null; diff --git a/test/copy-edges.test.ts b/test/copy-edges.test.ts index 330ad8e..7897ba0 100644 --- a/test/copy-edges.test.ts +++ b/test/copy-edges.test.ts @@ -260,3 +260,18 @@ test("★ #2454 that Download touches only the pasted notes: a typed-into note o assert.equal(index(s.vault)[KOL], 4); assert.ok(!(GONE in index(s.vault)), "an id the feed has no row for kept its 0: every Download would look for it again"); }); + +test("★ #2454 an extra walk that fails leaves the pasted 0s: no Save may go out unchecked", async () => { + const s = fresh(); + await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3); + await new PasteWorldCommand(s.app, {} as never).generateElementNotes(`${W}/Elements`, { Character: [{ id: IRE, name: "Ireena", description: "Older text." }] }, false); + setRequestUrl((r) => { + const url = new URL(r.url); + const path = url.pathname.replace(/^\/api\/v2/, "").replace(/\/$/, ""); + if (path === "/world") return { status: 200, json: { id: WID, name: "Barovia", description: "" } }; + if (path === "/changes") return url.searchParams.get("since") ? { status: 200, json: { cursor: "c3", has_more: false, head: 3, changes: [] } } : { status: 500, json: {} }; + return { status: 404, json: {} }; + }); + await new DownloadWorldCommand(s.app, { version: "t" }, s.plugin as never).downloadWithKey("ow_k_test", "1234"); + assert.equal(index(s.vault)[IRE], 0, "the walk for the pasted ids failed and the 0 was cleared: the next Save sends no check"); +}); diff --git a/test/paste-copies.test.ts b/test/paste-copies.test.ts index 63c9e79..40165ae 100644 --- a/test/paste-copies.test.ts +++ b/test/paste-copies.test.ts @@ -214,3 +214,11 @@ test("★ #2454 a paste stopped at a later note leaves the notes it wrote before assert.equal(index(vault)[ID], 0, "the note written before the stop kept its number"); assert.equal(index(vault)[OTHER], 9, "the note the stop left unchanged lost its number"); }); + +test("#2454 a paste of more notes than one batch records every 0", async () => { + const { vault, paste } = await world(); + const ids = Array.from({ length: 60 }, (_, i) => `019a0000-0000-7000-8000-${String(i + 1).padStart(12, "0")}`); + await paste.generateElementNotes(ELEMENTS, { Character: ids.map((id, i) => ({ id, name: `Guard ${i}`, description: "x" })) }, false); + const zeros = ids.filter((id) => index(vault)[id] === 0).length; + assert.equal(zeros, 60, `${zeros} of 60 notes took the 0`); +}); From 04020dab5798e496b72b3a41c967ba867f2398f9 Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 30 Nov 2026 14:47:55 +0100 Subject: [PATCH 5/6] 3.24.2 --- manifest.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- versions.json | 3 ++- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/manifest.json b/manifest.json index 82adf20..7dbc2b5 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "onlyworlds-builder", "name": "OnlyWorlds Builder", - "version": "3.24.1", + "version": "3.24.2", "minAppVersion": "1.4.0", "description": "Build and sync OnlyWorlds worlds from Obsidian.", "author": "Titus", diff --git a/package-lock.json b/package-lock.json index 3a55117..21b3d0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "onlyworlds-obsidian-plugin", - "version": "3.24.1", + "version": "3.24.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "onlyworlds-obsidian-plugin", - "version": "3.24.1", + "version": "3.24.2", "license": "MIT", "dependencies": { "@onlyworlds/sdk": "^4.176.0", diff --git a/package.json b/package.json index 1540bf9..8764697 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "onlyworlds-obsidian-plugin", - "version": "3.24.1", + "version": "3.24.2", "description": "Obsidian plugin for the OnlyWorlds worldbuilding framework", "main": "main.js", "type": "module", diff --git a/versions.json b/versions.json index 439fbcf..7a6aa5f 100644 --- a/versions.json +++ b/versions.json @@ -58,5 +58,6 @@ "3.23.1": "1.4.0", "3.23.2": "1.4.0", "3.24.0": "1.4.0", - "3.24.1": "1.4.0" + "3.24.1": "1.4.0", + "3.24.2": "1.4.0" } \ No newline at end of file From 54a68bf2c9726b818f37275f84dc7194f0f41ec1 Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 30 Nov 2026 15:01:42 +0100 Subject: [PATCH 6/6] Tests: no tracker numbers in test names --- test/copy-edges.test.ts | 8 ++++---- test/paste-copies.test.ts | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/copy-edges.test.ts b/test/copy-edges.test.ts index 7897ba0..ab7f74a 100644 --- a/test/copy-edges.test.ts +++ b/test/copy-edges.test.ts @@ -224,7 +224,7 @@ test("★ D3: a note copied then left as the person has it (it changed under eac assert.ok(r.notices.some((x) => x.includes("did not write")), r.notices.join(" | ")); }); -test("★ #2454 a Download after a Paste world pulls from the start, so a note the world has not changed gets its number back", async () => { +test("★ a Download after a Paste world pulls from the start, so a note the world has not changed gets its number back", async () => { const s = fresh(); await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3); assert.equal(index(s.vault)[IRE], 3); @@ -238,14 +238,14 @@ test("★ #2454 a Download after a Paste world pulls from the start, so a note t assert.match(s.vault.contentAt(IRE_NOTE) ?? "", /Current text\./); }); -test("#2454 a Download with no pasted number still walks from its cursor", async () => { +test("a Download with no pasted number still walks from its cursor", async () => { const s = fresh(); await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3); await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3, [whole(IRE, "Ireena", 4, "Changed.")]); assert.equal(index(s.vault)[IRE], 4, "the pull from the cursor brought the one change"); }); -test("★ #2454 that Download touches only the pasted notes: a typed-into note of another element stays, and a pasted id the feed lacks loses its 0", async () => { +test("★ that Download touches only the pasted notes: a typed-into note of another element stays, and a pasted id the feed lacks loses its 0", async () => { const s = fresh(); const both = [whole(IRE, "Ireena", 3, "Current text."), whole(KOL, "Kolyan", 4, "A priest.")]; await download(s.app, s.plugin, both, 4); @@ -261,7 +261,7 @@ test("★ #2454 that Download touches only the pasted notes: a typed-into note o assert.ok(!(GONE in index(s.vault)), "an id the feed has no row for kept its 0: every Download would look for it again"); }); -test("★ #2454 an extra walk that fails leaves the pasted 0s: no Save may go out unchecked", async () => { +test("★ an extra walk that fails leaves the pasted 0s: no Save may go out unchecked", async () => { const s = fresh(); await download(s.app, s.plugin, [whole(IRE, "Ireena", 3, "Current text.")], 3); await new PasteWorldCommand(s.app, {} as never).generateElementNotes(`${W}/Elements`, { Character: [{ id: IRE, name: "Ireena", description: "Older text." }] }, false); diff --git a/test/paste-copies.test.ts b/test/paste-copies.test.ts index 40165ae..8f7841d 100644 --- a/test/paste-copies.test.ts +++ b/test/paste-copies.test.ts @@ -166,9 +166,9 @@ test("★ a hidden stub outside the type folders is found and never written over assert.equal(vault.contentAt(NOTE), undefined); }); -// #2454: a Save sends the index's number as If-Match. A pasted note's text is the clipboard's, possibly older than the world's, so +// a Save sends the index's number as If-Match. A pasted note's text is the clipboard's, possibly older than the world's, so // the number the note was pulled at no longer describes its bytes: a Save would send the older text under a number that still matches. -test("★ #2454 Paste world takes the number off each note it replaces, so a Save of older text is refused, not matched", async () => { +test("★ Paste world takes the number off each note it replaces, so a Save of older text is refused, not matched", async () => { const { vault, dl, paste } = await world(); await dl.generateElementNotes(ELEMENTS, { Character: [{ id: ID, name: "Ireena", description: "A noble." }, { id: OTHER, name: "Kolyan", description: "A priest." }] }, false); vault.dotfiles.set(SEQ, JSON.stringify({ ...index(vault), [ID]: 7, [OTHER]: 9 })); @@ -179,7 +179,7 @@ test("★ #2454 Paste world takes the number off each note it replaces, so a Sav assert.equal(index(vault)[OTHER], 9, "a note Paste world did not replace lost its number"); }); -test("#2454 a paste that leaves a note's bytes as they were keeps its number", async () => { +test("a paste that leaves a note's bytes as they were keeps its number", async () => { const { vault, dl, paste } = await world(); await dl.generateElementNotes(ELEMENTS, el("A noble."), false); await paste.generateElementNotes(ELEMENTS, el("A noble."), false); @@ -190,7 +190,7 @@ test("#2454 a paste that leaves a note's bytes as they were keeps its number", a assert.equal(index(vault)[ID], 7, "a paste that left the note's bytes as they were took its number"); }); -test("★ #2454 a note Paste world makes for an id that holds a number, or none, takes the number 0 too", async () => { +test("★ a note Paste world makes for an id that holds a number, or none, takes the number 0 too", async () => { const { vault, paste } = await world(); vault.dotfiles.set(SEQ, JSON.stringify({ [ID]: 7 })); await paste.generateElementNotes(ELEMENTS, { Character: [{ id: ID, name: "Ireena", description: "Older." }, { id: OTHER, name: "Kolyan", description: "New here." }] }, false); @@ -198,7 +198,7 @@ test("★ #2454 a note Paste world makes for an id that holds a number, or none, assert.equal(index(vault)[OTHER], 0, "a note made for an id with no number can be saved with no check at all"); }); -test("★ #2454 a paste stopped at a later note leaves the notes it wrote before at 0", async () => { +test("★ a paste stopped at a later note leaves the notes it wrote before at 0", async () => { const { app, vault, dl, paste } = await world(); await dl.generateElementNotes(ELEMENTS, { Character: [{ id: ID, name: "Ireena", description: "A noble." }, { id: OTHER, name: "Kolyan", description: "A priest." }] }, false); vault.dotfiles.set(SEQ, JSON.stringify({ ...index(vault), [ID]: 7, [OTHER]: 9 })); @@ -215,7 +215,7 @@ test("★ #2454 a paste stopped at a later note leaves the notes it wrote before assert.equal(index(vault)[OTHER], 9, "the note the stop left unchanged lost its number"); }); -test("#2454 a paste of more notes than one batch records every 0", async () => { +test("a paste of more notes than one batch records every 0", async () => { const { vault, paste } = await world(); const ids = Array.from({ length: 60 }, (_, i) => `019a0000-0000-7000-8000-${String(i + 1).padStart(12, "0")}`); await paste.generateElementNotes(ELEMENTS, { Character: ids.map((id, i) => ({ id, name: `Guard ${i}`, description: "x" })) }, false);