Repository navigation
Fix(remote): zn never writes over a save another device made meanwhile - #60
Merged
Merged
Conversation
…ce made meanwhile Every zn command and MCP tool that changes part of a note on a server reads the note, changes the text and writes the whole note back: append, prepend, insert, replace, edit a section, toggle or update a task, and the record page a database row mirrors into. The write was last writer wins, so a save another device made during that round trip was written over and lost (ZenNotes/zennotes#1108). A ZenNotes server with conditional note writes (znserver#85, `supportsConditionalNoteWrites`) returns the sha256 of a note's bytes with each read and write, and refuses with 409, writing nothing, a write that sends it back as `baseSha256` when the note changed or is gone. These edits now go through one routine, backend.RewriteNote, that writes on the hash of the read it just made. A refusal means someone saved in between, so it reads again and makes the same change on the newer text, since appending a line or ticking a box is a function of the body: nobody's edit is lost and none is written over. After three refusals in a row it stops with the desktop's sentence, "<path> changed on the server since it was read, so it was not saved over", and how many times it tried. It is the same routine and count as the desktop's rewriteNoteOnServer. zn never hashes a note itself. It echoes the server's value, which it keeps on NoteMeta.Sha256, a field that is never printed, so `zn read --json`, read_note and every other output are as they were. An older server returns no hash, so nothing is sent: one read, one write, exactly as before. A local vault is not touched. `zn write` and write_note replace the whole note and stay unconditional, and a base that is not a hex SHA-256 is never sent, so a caller cannot turn it into a 400. The scoped MCP backend passes the base through to the server, so a path or read-only scope gets the same protection. Its tests failed until it did. remotetest is an in-memory server with and without the capability whose hook lands a save between a read and a write. The tests run each edit through it, through the MCP tools under full access and a path scope, a note that keeps changing, a note deleted in between, a replace with no match, and an older server. Writing with an empty base makes them fail.
…conflict can keep both Before an autosave or :w the TUI reads the note and stops with a conflict when it no longer holds the text the buffer was opened with or last saved. That check and the write were two requests, so a save another device made between them was written over without a word (ZenNotes/zennotes#1108). Against a server with conditional note writes the save now names the version that check read. When the server refuses, the save ends exactly as one the check caught: the buffer keeps every keystroke, the status line shows the conflict, autosave holds off, and :conflict offers the way out. The note on the server is never written over. :conflict gains Keep both (b), the outcome the desktop gives a save that found the note changed: the version on the server is kept beside the note as "Title (server copy)" ("(external copy)" in a local vault), then the buffer's edits are saved over the note on the version just read. The copy comes first, and if it cannot be made nothing is overwritten. A note no tab holds, which the TUI changes for a task toggle, goes through the same read, change and write routine as the CLI and retries the same way. A local vault or an older server hands out no hash, so there a save behaves exactly as before. The README says what the check now covers and where the old window remains.
The fake server proves zn sends the base and handles the refusal; only the real one proves the refusal happens. These tests start a ZenNotes server binary behind a proxy that lands another client's save just before zn's write is forwarded, then read the file on disk. Against a server with conditional note writes: an append keeps their line with zn's under it (409 then 200), a task toggle on the changed note keeps both, a note that changes before every write ends in the give-up error with their last text on disk, and a TUI save is refused and leaves their text. Against an older server the same steps write once and save zn's text, as they always did there, so the tests never skip once a binary is given. The server-contract job now runs ./internal/backend and the RealServer tests of ./internal/tui against znserver main. Checked here with a server built from znserver 5ea16d5 (main) and from 00c5603 (2.84.1).
adibhanna
added a commit
to ZenNotes/zennotes
that referenced
this pull request
Nov 23, 2026
zn 0.18.2 (ZenNotes/tui v0.18.2, ZenNotes/tui#60) names the version it read on every edit it makes through a self-hosted server, so its CLI and MCP edits and its terminal app's saves never write over a save another device just made, the same protection this release gives the app's own retry (f7f585f). The pin is that release's signed terminal-release.json, verified against zn-release-1 and copied byte for byte; the staged binary reports zn v0.18.2. The in-app help's CLI card names 0.18.2 and desktop 2.91.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zn's CLI, TUI and MCP server now use the conditional note writes from ZenNotes/znserver#85 (
supportsConditionalNoteWrites) when they talk to a self-hosted server. A save another device makes in the middle of a read, change and write is no longer written over (ZenNotes/zennotes#1108).What changes
backend.RewriteNote. It writes on thesha256of the read it just made. When the server answers 409, it reads the note again and makes the same change on the newer text. After 3 refusals it stops with the desktop's sentence ("… changed on the server since it was read, so it was not saved over") and says how many times it tried. The count and the message match the desktop'srewriteNoteOnServer.:w) already checked that the note still held the opened or last-saved text. The write now names the version that check read, so a save landing between the check and the write is refused and shows as the usual conflict: the buffer is kept, the status line shows the conflict, autosave holds off.:conflictgains Keep both (b), the desktop's keep-both outcome: the server's version is kept as "Title (server copy)" (or "(external copy)" in a local vault), then the local edits are saved on the version just read. The copy is made first; if that fails, nothing is overwritten.zn writeandwrite_notestay last writer wins. A local vault behaves as before. An older server returns no hash, so nothing is sent and every edit is one read and one write, exactly as before. zn never hashes anything itself: it echoes the server's value, which it keeps onNoteMeta.Sha256(json:"-"), so nothing zn prints changes.How it was verified
go vet ./...andgo test ./...pass.internal/remote/remotetestis an in-memory server, with and without the capability, that can land a save between a read and a write. Unit tests cover every edit, the MCP tools under full access and under a path scope, a note that keeps changing, a note deleted in between, a replace with no match, an older server, and the TUI save, autosave and keep-both. With the base removed (in RewriteNote, the scoped backend or writeSnapshot) these tests fail.5ea16d5(main) and00c5603(2.84.1). A proxy lands another client's save just before zn's write reaches the server. On main the write gets 409 then 200, the file keeps both edits, the give-up path leaves the other client's text, and the TUI save is refused. On 2.84.1 the same steps write once, as before. The existing HTTP contract passes against both./mcproute (ZENNOTES_SERVER_MCP_CONTRACT_BINARY) passes with a server built against this branch through areplace../internal/backendandgo test -run RealServer ./internal/tui.For the release
/mcproute uses zn's MCP package in-process and pinsgithub.com/ZenNotes/tui v0.17.1. Its MCP edits are conditional only once itsgo.modmoves to the zn release that ships this.zn base) still write unconditionally, and so do comment sidecars.