Skip to content

Fix serverOnlyFields handling for new documents in replication#387

Open
pubkey wants to merge 1 commit intomasterfrom
claude/fix-conflict-handling-bug-39BPu
Open

Fix serverOnlyFields handling for new documents in replication#387
pubkey wants to merge 1 commit intomasterfrom
claude/fix-conflict-handling-bug-39BPu

Conversation

@pubkey
Copy link
Copy Markdown
Owner

@pubkey pubkey commented Apr 4, 2026

This PR contains:

  • A BUGFIX
  • IMPROVED TESTS

Describe the problem you have without this PR

When replicating new documents via the server endpoint with serverOnlyFields configured, the conflict resolution logic incorrectly transformed falsy assumedMasterState (used for new document inserts) into an object and initialized server-only fields to null. This caused:

  1. Schema validation failures when the schema doesn't allow null for those fields
  2. False conflicts during replication

Changes

Source Code Fix (src/plugins/server/helper.ts)

Modified mergeServerDocumentFieldsMonad to:

  • Return falsy clientDoc as-is without transformation
  • Skip initializing server-only fields to null when serverDoc is undefined (new documents)

This ensures new documents pushed via replication are not modified with server-only fields that don't exist on the client side.

Tests

  • Added integration test in endpoint-replication.test.ts verifying new documents replicate correctly from client to server with serverOnlyFields configured
  • Updated unit tests in server.test.ts:
    • Changed expectation: server-only fields should not be added to new documents (not set to null)
    • Added test for falsy clientDoc handling

Test Plan

Added comprehensive test coverage:

  • Integration test: should replicate a new document from client to server when serverOnlyFields are set - verifies end-to-end replication of new documents with server-only fields
  • Unit tests: Verify mergeServerDocumentFieldsMonad correctly handles new documents and falsy inputs

All existing tests pass with the corrected behavior.

https://claude.ai/code/session_014Dbfva2dD4vTxEaYZfjpgb

mergeServerDocumentFieldsMonad had two bugs when handling new document
inserts via replication push with serverOnlyFields configured:

1. When assumedMasterState was undefined (new insert), it was transformed
   into an object (e.g. {lastName: null}), causing masterWrite to treat
   inserts as updates and produce false conflicts.

2. Server-only fields were set to null on newDocumentState for new
   documents, which failed schema validation for non-nullable types
   like string.

Fix: return falsy clientDoc as-is, and don't modify server-only fields
when serverDoc is undefined (new document).

https://claude.ai/code/session_014Dbfva2dD4vTxEaYZfjpgb
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.

2 participants