Skip to content

Sync Write Model V1

Define the first implementation write model for sync clients, aligned to:

  • pgxsinkit append-only operation logging
  • deterministic, low-complexity conflict behavior
  • strong mutation traceability
  1. pgxsinkit is external and versioned via npm registry releases.
  2. The team controls pgxsinkit evolution and can ship breaking major versions when required.
  3. pgxsinkit must keep an append-only log of all client-submitted operations.
  4. V1 conflict resolution defaults to simple last-write-wins (LWW) with deterministic server apply ordering.
  5. All syncable domain-table mutations must flow through the same pgxsinkit batch mutation ingress.
  6. The only approved write backend mode for syncable domain tables is bulk-plpgsql-artifact.

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.

Exemption: server-authored derivation and lifecycle writers

Section titled “Exemption: server-authored derivation and lifecycle writers”

One narrow class of writer is exempt: a server-authored process that writes rows no client may author, where routing through the client ingress would be a fiction rather than a control. Recorded 2026-08-02, after an external review observed the practice had outgrown the rule. It is a documented exemption, not a licence: everything reachable from a client request stays on the single ingress, and “a server component finds it convenient” is not a qualification.

A writer qualifies only if all four hold, and the fourth is what makes the exemption safe:

  1. No client can invoke it on its own behalf. It runs as a worker, an operator entrypoint, or a server-side post-commit stage. An API request path that writes a syncable table on a caller’s behalf is not exempt — that is exactly the ingress’s job.
  2. The rows are derived or lifecycle state, not authored content. The client could not have produced them: they are folds of an append-only archive, re-pointings of a merge, anonymizations of an erasure, or pointers to server-generated artifacts.
  3. Ordering is enforced explicitly, since the exempt writer does not inherit the ingress’s single apply-order space. Each names its own mechanism — an advisory lock, a row lock, or an idempotency key — and holds it for the whole write.
  4. Attribution survives the write, because the operation log does not cover it. Each exempt writer leaves a durable, inspectable record of what it changed and from what input.

The exempt writers today, with the mechanism each uses for obligations 3 and 4:

Writer Ordering Attribution
Language projector — fold, replay, sweep, live consume (packages/db/src/lang-projector/) archive-lane quiesce lock (packages/db/src/archive-lane/quiesce-lock.ts), session-scoped for a full rebuild lang.v1: derivation markers plus evidence provenance carrying the consumed archive row range (ADR-0058)
Competency merge (packages/db/src/competency-merge/merge.ts) per-framework advisory lock, taken before the node rows, one lock order everywhere merge-touched stamps as standing re-derivation work; the surviving/losing node ids are recorded
Erasure executor (packages/db/src/erasure/execute.ts) the same archive-lane quiesce lock, taken first in its transaction erasure_receipt (counts and a hashed subject scope — deliberately non-personal, ADR-0014)
Archive forwarder / trim (apps/api/src/features/archive/archive-pass.ts) partial-unique dedupe on the source row id the archive rows themselves are the record
Enrichment artifact publication (apps/api/src/features/lang-enrichment/) FOR UPDATE on the owning row before the pointer swap content hash plus run metrics on the row it publishes

An exempt writer that stops satisfying its row of this table has lost the exemption, not merely drifted from a convention.

  • 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-artifact for syncable domain workloads in every environment.

The append-only operation log must capture at least:

  • operation_id (monotonic/ordered unique ID)
  • client_id
  • client_operation_id (idempotency key from client)
  • person_id (nullable for pre-auth flows)
  • target_table
  • target_record_id (nullable for inserts before server ID assignment)
  • operation_type (insert, update, delete, state-transition)
  • payload_before (nullable)
  • payload_after (nullable)
  • submitted_at
  • applied_at
  • apply_status (applied, rejected, ignored, superseded)
  • rejection_reason (nullable)

For mutable sync tables, concurrent writes resolve by latest authoritative server apply order.

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

Even in v1, avoid destructive LWW for append-oriented records:

  • assessment_attempt
  • feedback_entry
  • person_competency_evidence
  • activity_event
  • change_audit_entry

These should remain append-friendly with idempotency controls.

Each client mutation should include:

  • client_id
  • client_operation_id
  • operation_time_client (informational only)
  • target
  • patch or full record payload
  • expected_record_version (optional in v1, required for diagnostics)
  1. Validate envelope and authorization
  2. Append operation to pgxsinkit operation log
  3. Apply mutation under LWW baseline policy
  4. Update target row updated_at_us, last_operation_id, and last_apply_sequence
  5. Emit audit/event records with source operation link
  6. Acknowledge client with applied/superseded/rejected outcome

Admin/import/batch/system workflows targeting syncable domain tables must use the same ingress envelope and pipeline.

Track at minimum:

  • Operation ingestion rate
  • Rejected/superseded ratio
  • Median and p95 apply latency
  • Replay/retry counts by client
  • Per-table LWW supersession counts

Only replace LWW for a data class when all are true:

  1. Correctness property is explicitly documented.
  2. Proposed merge strategy is deterministic under replay.
  3. Complexity and operational cost are acceptable for early stage.
  4. Conformance tests prove behavior in disconnected and concurrent scenarios.

Until then, keep LWW baseline.

  • 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).

  1. Final operation-log ordering primitive (bigint sequence vs sortable UUID)
  2. Scope of v1 expected_record_version enforcement
  3. Whether rejected operations are replayable or terminal by default
  4. Whether mutable syncable rows should carry enforced FK to operations_log.id or a validated opaque operation identifier