Repository navigation
feat(api): add a document content hash with ETag, If-Match and no-op writes - #751
Conversation
There was a problem hiding this comment.
ℹ️ No critical issues — one design consequence worth a look.
Reviewed changes
The full PR (4 commits, 23 files) was read end to end and the affected Go packages build and test clean locally.
- Content hash — canonical sha256 of BlockNote content with block-level
ids stripped (recursively throughchildren), sorted keys, exact number literals;props.*/ inline ids still count, and nil/empty/nullall hash as[]. - Storage — migration
000087adds nullablecontent_hashtodocumentsanddoc_snapshotsplus a lookup index; existing rows stay NULL and are hashed in memory until their next write. - Conditional writes —
PATCH /docs/{docId}honoursIf-Match(single hash or*), compared inside the row-lockeddecide; a mismatch returns 412VERSION_CONFLICTcarrying the stored hash,updated_atandupdated_by. - No-op writes — identical content, title, folder and position skip the UPDATE, snapshot,
updated_atbump, activity, realtime event and mention notifications, withDocChangenow driving both the snapshot decision and the activity fields. - Conditional reads — GET returns the hash as
ETagandcontent_hash, answersIf-None-Matchwith a bodyless 304, and setsCache-Control: private, no-cache. - Snapshots — each snapshot records its version's hash and
GET .../snapshots?content_hash=filters on it. - Tests — new unit, handler, PG-gated repository and e2e coverage for the no-op and two-writer cases, plus the
If-Match/If-None-Matchparsing table.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
The ETag was the content hash alone, but the response also carries the title, folder, position and timestamps. A client revalidating with If-None-Match after a rename or a move got 304 and kept a stale title. The ETag is now "<content hash>-<meta tag>", where the meta tag is a fingerprint of every other field of the document response. If-None-Match compares the whole ETag. If-Match still compares content only and accepts either a content hash or a whole ETag, so a rename causes no conflict.
There was a problem hiding this comment.
✅ No new issues found.
The incremental commit 6a952bd4 addresses the prior review's ETag-scope concern: the response ETag is now "<content hash>-<meta tag>", If-None-Match compares the whole tag, and renames/moves now return a 200 with a new ETag. If-Match remains content-only by design and is documented in UpdateDocument; the tests cover both behaviors.
I reviewed the new commit and its six changed files. go build ./... and the affected Go packages (internal/transport/http/..., internal/domain/doc, internal/service/doc, internal/repository/postgres) pass. The prior review thread is resolved.
No further changes requested.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
A write returned the in-memory document, whose updated_at came from time.Now() with nanoseconds, while the row and every later read have microseconds. On Linux the two differed: an identical second push appeared to move updated_at, and the ETag a write returned was not the ETag of the next read, so If-None-Match with it got 200. Document timestamps are now truncated to microseconds when set, and the ETag's meta tag normalizes timestamps to UTC microseconds, so the same instant always gives the same tag.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
The incremental commit since the prior pullfrog review, which aligns document write responses and ETags with the row Postgres stores.
- Write-response timestamps —
CreateDocumentand the lockedUpdateDocumentnow set timestamps throughstoredNow()(time.Now().Truncate(time.Microsecond)). The repository returns the in-memory document on a real write (result = current), so without this the response carried nanoseconds that the row never holds. - ETag stability —
docMetaTagprojectscreated_at/updated_atto UTC microseconds before hashing, so the same instant tags identically whether it came from a freshtime.Now()or a row scanned in another zone or precision. - Coverage —
TestDocumentWrites_TimestampsAtStoredPrecisionandTestDocETag_SameInstantSameTagpin both halves of the round trip.
I verified the load-bearing assumption against pgx v5.9.2 source: TimestamptzCodec encodes microseconds by integer division in binary format and UTC().Truncate(time.Microsecond) in text format, so storedNow() truncation matches what Postgres stores rather than rounding. go test ./internal/service/doc/ ./internal/transport/http/handler/ passes. The prior review's thread remains resolved.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
pikann
left a comment
There was a problem hiding this comment.
LGTM! Thank you for the contribution! 🚀

Summary
First part of #747 (docs pull / push / content check for coding agents). It gives every document a content hash and uses it for conditional requests, so an identical write costs nothing and a stale write is refused instead of silently winning.
content_hashis the sha256 of a document's content with each block's ownidremoved and keys sorted. The same blocks give the same hash whatever ids the writer generated. Only a block's ownidis ignored (the same rule ascleanBlocksin the web app); every other field counts.GET /docs/{docId}returnscontent_hashin the body and anETagof the form"<content hash>-<meta tag>", where the meta tag fingerprints every other field of the response (title, folder, position, timestamps).If-None-Matchwith the current ETag gets304with no body, so a rename or a move is never hidden behind a 304. Responses carryCache-Control: private, no-cache.PATCH /docs/{docId}acceptsIf-Matchwith a content hash or a whole ETag (or*). It is a precondition on content only: a rename or a move since the caller read the document does not fail it. A mismatch gets412(VERSION_CONFLICT) with the stored version'scontent_hash,updated_atandupdated_byin the error details. The check runs under the row lock.UPDATE, no snapshot, noupdated_atchange, no activity, no realtime event, no mention notifications.GET /docs/{docId}/snapshots?content_hash=<hex>returns that version, so a client can fetch the version it last saw.Why
Until now "content changed" was decided by comparing raw JSON strings, which never matched: the MCP Markdown parser gives blocks new ids on every write, the web editor strips ids before saving, and JSONB re-serializes what it stores. Every identical push created a snapshot, bumped
updated_at, recorded adoc.updatedactivity and made the live-editing hub merge every block again. Two agents writing the same doc silently overwrote each other.What does not change
If-Match, so its saves behave as before, except that a save of unchanged content is now skipped.ifMatchVersion). Documents use the content hash instead, for the reason given in migration000083: a row version would bump on every autosave, rename and move.services/realtime.Migration
000087_add_documents_content_hash.sqladds a nullablecontent_hashtodocumentsanddoc_snapshots, plus an index ondoc_snapshots (document_id, content_hash). Existing rows stayNULL: the hash is computed in memory when one is needed and stored on the row's next write. The next PR adds a backfill.Testing
golangci-lint run: 0 issues.If-Matchmatch / mismatch /*, check under the lock, row without a stored hash), handlers (ETag, 304, 400 on a malformedIf-Match, 412 with details, snapshot filter, no activity on a no-op).TestDocs_IdenticalPushIsNoOp,TestDocs_TwoWritersSecondGets412; the fulltest/e2esuite and the real-migrations test pass.apps/e2e, Chromium,tests/docsandtests/realtime, run on macOS against a stack built from this branch): 207 passed, 6 skipped, 1 failed. The failure isdocs.spec.ts"User can edit their own comment": the test pressesControl+a, which does not select all on macOS. With only that key changed toControlOrMeta+athe test passes on the same build. This PR does not touch comment code, and the test file is left unchanged here.Closes two of the acceptance criteria in #747: "pushing identical content creates no snapshot and leaves
updated_atunchanged" and "two agents read the same doc and both write withIf-Match: the second gets412".Remaining PRs for #747
ETag,If-None-Match/If-Match, no-op writesGET /docs/changes?since=<cursor>;POST /docs/batch-get; hash backfillPUTandGET /docs/by-path;sourcemetadata and a lock for managed copies; a change note, client label and task reference on writes; MCP doc tools resolve a path in one requestnpx @paca-ai/paca-mcp pullandpushread_docoutline / section / line range,grep_docs,edit_docPRs 2 and 3 are independent of each other and both build on this one.
Design decisions for the feature
Happy to change any of these; they are listed so they can be challenged early.
If-Matchcompares the content hash, not a row version. It answers "has the body changed since I read it". Renames and moves cause no false conflicts, and a revert to the same text reads as unchanged. TheETagstarts with that hash and adds a tag over the rest of the response, soIf-None-Matchonly gets a 304 when nothing in the response changed.source(the web editor, MCPwrite_doc). A push from the same repository passes.412. There is no live messaging between agent sessions; questions go through document comments.edit_docworks on top-level blocks. A change inside a nested list rewrites that list block; every other block is sent back exactly as stored.Type of Change
Checklist
docs/apihas no list of document endpoints, so there was nothing to update; the handler and migration comments describe the behaviour.)