Introducing a new way to review your PRs.

Read the announcement

crmne / zapfast

ZapFast: a fast, native WhatsApp client in Rust and egui

View on GitHub

19 documents · Updated Nov 18, 2026

docs / event-sync-and-recovery

Event synchronization and recovery

Event synchronization keeps the local archive aligned with WhatsApp when related events arrive out of order, when a message disappears, or when an incremental sync cannot recover old state. The background worker uses durable archive marks, short-lived waiting state, ordered writes, and reconnect work to make the message view converge without restoring data that the user removed.

Event ordering

The worker treats a message as the anchor for related state. A read, played mark, reaction, receipt, or timer acknowledgement can arrive before that message is filed, so the event waits until the message is available and then updates the archive and UI together. This keeps event order differences from creating false unread counts, missing reactions, or incomplete delivery state.

References: src/…/worker.rs• L5626-5645 src/…/worker.rs• L5942-6013 src/…/receipts.rs• L52-84

Early events

Early events are phone-originated reads and reactions that name a message not yet in the archive. The worker keeps them in bounded memory, merges repeated reads, keeps only the newest reaction from each sender, and drops entries that wait too long. When an id changes from a privacy id to a phone-number id, the waiting events move with it.

References: src/…/early_events.rs• L1-16 src/…/early_events.rs• L59-182 src/…/worker.rs• L3459-3512

When the message arrives, the worker applies the saved read or played state and then applies the saved reactions. A missing message does not leave unbounded state behind: early events expire by age or count.

References: src/…/worker.rs• L5626-5645 src/…/early_events.rs• L177-187

Receipt synchronization

Receipts use the archive as a holding area when they outrun an outgoing message. Direct-chat receipts later update the message itself. Group receipts wait for the saved audience so the least advanced expected recipient determines the message state. A periodic cleanup removes receipts whose message never arrives.

References: src/…/receipts.rs• L36-104 src/…/receipts.rs• L116-188 src/…/receipts.rs• L222-237

Read state

Read state has two directions: local reads and local unread marks must reach the phone, while phone updates must change the local chat state. The archive records the local position and queues the write. The worker sends one write at a time because both actions use the same phone state collection.

References: src/archive.rs• L858-910 src/archive.rs• L926-994 src/…/read_sync.rs• L1-18

Read write flow

The worker sends queued reads before queued unread marks. Each completion names the chat and position that it belongs to, so a late response cannot complete a newer request. A failed write holds all later writes behind capped backoff; success clears the backoff and advances the durable queue.

References: src/…/worker.rs• L9959-10044 src/…/worker.rs• L7339-7370 src/…/read_sync.rs• L20-75

This diagram shows how a local chat action becomes a durable archive state and then a phone update.

flowchart LR
    A[Local read or unread mark] --> B[Archive pending state]
    B --> C[Worker sends one phone action]
    C -->|success| D[Archive clears matching pending state]
    C -->|failure| E[Backoff]
    E --> C
    F[Phone mark update] --> G[Archive chat state]

Read update handling

A phone read update advances the local read position and clears the local unread mark. A phone unread update preserves the empty unread marker while leaving any real unread count intact. History snapshots also respect a newer local read or unread action instead of replacing it with stale phone data.

References: src/…/worker.rs• L4480-4502 src/archive.rs• L912-970

Disappearing-message state

Disappearing-message settings are synchronized as accepted state, not as an optimistic local change. Direct-chat changes become visible after the server acknowledges the setting. Group changes become visible from the server's group notice, which supplies the ordering time and actor.

References: src/…/disappearing.rs• L1-12 src/…/disappearing.rs• L49-123 src/…/disappearing.rs• L184-235

Timer acknowledgements

Timer acknowledgements can arrive before the send task reports the message id. The worker holds such acknowledgements briefly and matches them by exact id. If an acknowledgement never arrives, the UI is released after a timeout, while a late accepted acknowledgement can still update the timer without a second failure report.

References: src/…/disappearing.rs• L16-47 src/…/disappearing.rs• L238-311

Timer notices use the setting change's stable id. A redelivery therefore replaces the same system row instead of adding a duplicate. A notice covered by a prior chat clear or deletion is rejected so recovery cannot restore removed history.

References: src/…/disappearing.rs• L313-365 src/…/worker.rs• L2212-2229

Disappearing-message handling

Chat and message removal create durable barriers in the archive. The worker checks those barriers before importing live messages or history, so a replay cannot bring back content that the phone or the user cleared. A chat clear keeps the chat entry but removes its covered content. A chat delete also removes the chat entry and stops work that could recreate it.

References: src/…/worker.rs• L2139-2219 src/archive.rs• L2140-2245 src/…/worker.rs• L5945-5955

Message deletion recovery

An accepted “delete for me” action is saved before the local message copy is removed. This durable record lets reconnect work finish local cleanup even when the original event is not delivered again. Pending requests retry after reconnect, while confirmed requests replay local cleanup without sending a second request.

References: src/…/worker.rs• L11190-11248 src/…/worker.rs• L11260-11329 src/archive.rs• L2090-2129

Identity reconciliation

A privacy id and a phone-number id can describe the same chat before their mapping is known. When the mapping arrives, the archive moves messages and related state into the canonical chat in one transaction. The worker then replays deletion barriers and waiting early events under the canonical id. Stars, pins, motion clips, and receipts follow the message only when the target is still valid.

References: src/…/worker.rs• L3459-3516 src/archive.rs• L1167-1249 src/…/reconcile.rs• L7-77

Star recovery

Stars need a one-time full replay because incremental state sync may already have consumed stars made before this client began following them. On a connected worker, a snapshot of the phone's star state replays every star as a normal update. The archive's event ordering prevents that replay from undoing a newer unstar.

References: src/…/star_recovery.rs• L1-10 src/…/star_recovery.rs• L17-49 src/…/stars.rs• L44-70

Recovery marker

The worker records a durable recovery marker only after the snapshot completes. A failed snapshot remains eligible for retry on the next connection. A new link skips this extra recovery because its first sync already includes the phone's existing stars.

References: src/…/star_recovery.rs• L52-67 src/…/worker.rs• L915-968 src/…/worker.rs• L4099-4121

Worker reconciliation

The worker is the single coordinator for connection events, archive writes, retry queues, and UI notifications. Its connection handler starts synchronization and recovery work. Its periodic worker tick retries queued state and prunes bounded waiting state. This design lets transient network failures recover without making each feature invent its own lifecycle.

References: src/…/worker.rs• L1099-1231 src/…/worker.rs• L4099-4121 src/…/worker.rs• L1465-1484

Reconciliation lifecycle

When the worker processes a connection or event, it follows a structured sequence:

  1. Resolve the event's chat and message ids to the current canonical identity.
  2. Check durable removal barriers before importing or restoring state.
  3. Apply the event to the archive, or queue it until its message or acknowledgement exists.
  4. Retry failed durable work after the required backoff or on the next connection.
  5. Emit the resulting message or chat state after the archive accepts the change.

This diagram shows the shared path used by normal events and recovery work.

sequenceDiagram
    participant P as Phone
    participant W as Worker
    participant A as Archive
    participant U as UI
    P->>W: event or sync result
    W->>W: resolve identity and check barriers
    alt related data is not ready
        W->>W: hold bounded waiting state
    else data is ready
        W->>A: apply or reconcile state
        A-->>W: accepted durable state
        W->>U: emit updated chat or message
    end
    W->>W: retry queued work on tick or reconnect

The worker emits UI state only after the archive write or durable queue update succeeds. This ordering keeps a displayed recovery result from getting ahead of the local source of truth.

References: src/…/worker.rs• L5945-6065 src/…/worker.rs• L1213-1231 src/…/worker.rs• L7326-7369

This page fits within the broader background data services flow. For the device state that surrounds these sync tasks, see downloads, previews, and device state.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep event identity checks aligned between ReadSync and the archive's pending read and unread markers. A completion must clear only the state it started.
  • Keep privacy-id mapping updates aligned across early events, receipts, deletion barriers, and message metadata. A new alias must not create a second logical message state.
  • Keep full-sync star ordering compatible with live star ordering. A replay must never override a newer live unstar at the same timestamp.
  • Keep removal barriers in front of both live message filing and history import. Otherwise a reconnect can restore deleted or cleared content.
  • Keep recovery markers durable and completion-based. A failed snapshot must retry, while a completed snapshot must not run on every connection.

Important failure modes

TriggerConsequenceGuard
A read completion names an older chat positionA newer pending read can be cleared by the wrong responseMatch chat, position, and operation kind before finishing ReadSync
A phone receipt arrives before its outgoing messageMessage ticks stay behind the phone stateHold the receipt and settle it when the message is filed
A history replay includes a message removed locallyDeleted content returns after reconnectCheck message and chat removal barriers before archive insertion
A star snapshot fails during recoveryOlder stars remain absent from the Starred viewLeave the recovery marker unset and retry on the next connection
A privacy-id mapping arrives after related eventsReads, reactions, receipts, or stars split across two chat idsRekey waiting state and move archive metadata during canonicalization
A timer acknowledgement arrives after its UI timeoutThe timer could remain stale or show a second errorRetain the timed-out acknowledgement briefly and apply one late success

Key files

FileWhy to read it
src/…/worker.rsMain coordinator for event filing, history import, deletion barriers, read sync, reconnect recovery, and UI emission
src/…/early_events.rsBounded waiting model for reads and reactions that arrive before their messages
src/…/read_sync.rsOrdering and backoff rules for read and unread writes
src/…/disappearing.rsAccepted-state flow for disappearing-message timers and early acknowledgements
src/…/star_recovery.rsOne-time full snapshot recovery for older stars
src/…/receipts.rsDurable receipt holding and settlement for direct and group messages
src/…/stars.rsTimestamp rules that protect live star state from replayed updates
src/…/reconcile.rsMetadata movement when privacy ids become canonical phone-number ids
src/archive.rsDurable pending state and removal barriers used by worker recovery