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.
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:
- 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.
- 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.
- 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.
- 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.
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