Sync Write Model V1
Intent
Section titled “Intent”Define the first implementation write model for sync clients, aligned to:
- pgxsinkit append-only operation logging
- deterministic, low-complexity conflict behavior
- strong mutation traceability
Confirmed V1 Direction
Section titled “Confirmed V1 Direction”- pgxsinkit is external and versioned via npm registry releases.
- The team controls pgxsinkit evolution and can ship breaking major versions when required.
- pgxsinkit must keep an append-only log of all client-submitted operations.
- V1 conflict resolution defaults to simple last-write-wins (LWW) with deterministic server apply ordering.
- All syncable domain-table mutations must flow through the same pgxsinkit batch mutation ingress.
- The only approved write backend mode for syncable domain tables is
bulk-plpgsql-artifact.
Single Ingress Policy
Section titled “Single Ingress Policy”For syncable domain tables, all writes must use one ingress path:
POST /api/mutations- write backend
bulk-plpgsql-artifact - shared operation log and apply ordering semantics
Non-client actors (admin tools, imports, batch jobs, system workflows) are still allowed, but they must submit through the same batch ingress contract.
pgxsinkit Integration Policy
Section titled “pgxsinkit Integration Policy”- Keep pgxsinkit as an external dependency, not vendored into app packages.
- Pin explicit npm versions in workspace dependencies.
- Use semver-major bumps for protocol/storage breaking changes.
- Do not block v1 correctness for long-term backward compatibility guarantees.
- Enforce
WRITE_API_BACKEND=bulk-plpgsql-artifactfor syncable domain workloads in every environment.
Operation Log Requirements
Section titled “Operation Log Requirements”The append-only operation log must capture at least:
operation_id(monotonic/ordered unique ID)client_idclient_operation_id(idempotency key from client)person_id(nullable for pre-auth flows)target_tabletarget_record_id(nullable for inserts before server ID assignment)operation_type(insert,update,delete,state-transition)payload_before(nullable)payload_after(nullable)submitted_atapplied_atapply_status(applied,rejected,ignored,superseded)rejection_reason(nullable)
LWW Conflict Baseline
Section titled “LWW Conflict Baseline”For mutable sync tables, concurrent writes resolve by latest authoritative server apply order.
Determinism
Section titled “Determinism”Use server apply order (operation_id or equivalent ordered apply cursor), not client clock time.
Because all syncable writes use one ingress path, every mutation participates in the same authoritative apply-order space.
- Low implementation complexity in v1
- Deterministic replay behavior
- Compatible with append-only operation logging
LWW Exceptions
Section titled “LWW Exceptions”Even in v1, avoid destructive LWW for append-oriented records:
assessment_attemptfeedback_entryperson_competency_evidenceactivity_eventchange_audit_entry
These should remain append-friendly with idempotency controls.
Client Mutation Envelope
Section titled “Client Mutation Envelope”Each client mutation should include:
client_idclient_operation_idoperation_time_client(informational only)targetpatchor full record payloadexpected_record_version(optional in v1, required for diagnostics)
Apply Pipeline
Section titled “Apply Pipeline”- Validate envelope and authorization
- Append operation to pgxsinkit operation log
- Apply mutation under LWW baseline policy
- Update target row
updated_at_us,last_operation_id, andlast_apply_sequence - Emit audit/event records with source operation link
- Acknowledge client with applied/superseded/rejected outcome
Admin/import/batch/system workflows targeting syncable domain tables must use the same ingress envelope and pipeline.
Observability Requirements
Section titled “Observability Requirements”Track at minimum:
- Operation ingestion rate
- Rejected/superseded ratio
- Median and p95 apply latency
- Replay/retry counts by client
- Per-table LWW supersession counts
Correctness Escalation Policy
Section titled “Correctness Escalation Policy”Only replace LWW for a data class when all are true:
- Correctness property is explicitly documented.
- Proposed merge strategy is deterministic under replay.
- Complexity and operational cost are acceptable for early stage.
- Conformance tests prove behavior in disconnected and concurrent scenarios.
Until then, keep LWW baseline.
Implementation Anchors
Section titled “Implementation Anchors”- pgxsinkit protocol and operation log: external pgxsinkit project
- Sync integration and collection behavior:
packages/offline-data - Row-level operation linkage in schema:
packages/db - Mutation services and policy checks:
apps/api
Operational enforcement guidance is defined in Sync Write Enforcement V1 (Lite).
Open Decisions
Section titled “Open Decisions”- Final operation-log ordering primitive (bigint sequence vs sortable UUID)
- Scope of v1
expected_record_versionenforcement - Whether rejected operations are replayable or terminal by default
- Whether mutable syncable rows should carry enforced FK to
operations_log.idor a validated opaque operation identifier