Skip to content

Refactor SyncResolver to unified state machine with single-pass connection flow #4103

@guiyanakuang

Description

@guiyanakuang

Problem

The current SyncResolver has several design issues that make device connections slower and the code harder to maintain:

  1. Two-phase connection with event round-trip: Connecting from DISCONNECTED requires two separate event cycles (ResolveDisconnected → CONNECTING → ResolveConnecting → CONNECTED), adding unnecessary latency.

  2. Implicit UNVERIFIED state: UNVERIFIED is a side-effect of tokenCache miss during resolveConnecting, not an explicit state transition.

  3. Complex state transition paths: State transitions are scattered across resolveDisconnected, resolveConnecting, resolveConnection, and tryUseTokenCache with implicit dispatch logic.

  4. Redundant event types: Four separate resolve events (ResolveDisconnected, ResolveConnecting, ResolveConnection, ForceResolveConnection) where two suffice.

Solution

Refactor the internal state machine while keeping the external protocol unchanged (VERSION=3, all HTTP endpoints, DB schema — fully compatible with old devices):

  • Unify 4 resolve events into 2: Resolve and ForceResolve
  • Replace scattered resolve methods with a clear phase-based flow: resolve → discoverAndConnect → authenticate → tryTokenCacheOrUnverified / verifyConnection
  • Complete the full discover → authenticate flow in a single resolve pass (no intermediate event round-trip)
  • Avoid unnecessary DB writes when state doesn't change (e.g., UNVERIFIED polling)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions