Buffer state_changed events across the sync window and preserve delivery order - #21
Merged
Conversation
…ery order Subscribe to state_changed before fetching the initial snapshot so events firing during the network-bound fetch window are buffered and flushed in arrival order once the snapshot lands, instead of being dropped and leaving entities stale until the next resync. resync() re-arms buffering; the flush runs on both the success and failure paths so the buffer can't grow unbounded. Remove the per-event unstructured Task in both the state_changed subscription and the connection-transition delegate. HAKit delivers both callbacks synchronously on .main (callbackQueue = .main), so handle them via MainActor.assumeIsolated to keep HAKit's serial delivery order — a Task per callback dropped that guarantee, risking stale-overwrite of newer state and missed/spurious onReconnect under connection flapping. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Follow-up to the review of the buffer-then-flush change: - Bound bufferedEvents with a drop-oldest cap (maxBufferedEvents) so a stalled sync — a live socket whose getStates never returns — can't grow the buffer without bound. Past the cap the oldest events are dropped and a one-shot warning is logged; the snapshot re-baselines every entity on flush, so only intermediate states of a fast-changing entity during the stall are lost. - Document the resync buffering tradeoff: live updates are held (not lost) until the snapshot lands, so a foreground/pull-to-refresh resync briefly pauses live UI updates. This is the intended cost of closing the reconnect gap; it is bounded, self-healing, and order-preserving. Adds a test asserting the oldest event is evicted once the cap is exceeded. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
adborbas
force-pushed
the
fix/sync-realtime-ordering
branch
from
July 16, 2026 20:37
afa9289 to
da28eb0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
state_changedbefore fetching the initial snapshot in bothstart()andresync(), buffering events that arrive during the fetch window and flushing them in arrival order once the snapshot lands. The flush runs on both the success and failure paths so no data is wiped and the buffer can't persist across a failed sync.Taskin both thestate_changedsubscription and the connection-transition delegate, handling each callback synchronously viaMainActor.assumeIsolated. HAKit dispatches both callbacks oncallbackQueue = .main, so this preserves HAKit's serial delivery order.bufferedEventswith a drop-oldest cap (maxBufferedEvents = 2000) plus a one-shot warning, so a stalled sync (live socket, unansweredgetStates) can't grow the buffer without bound.HAConnectionManager.handleTransition(_:)from the delegate body.Why
state_changedfiring in that gap was never delivered and never reconciled — an entity could show a stale value (e.g. a light shown off after being turned on) until the next full resync. Subscribe-first buffering closes the gap.Taskdiscards HAKit's ordering guarantee. Back-to-back changes for one entity could apply out of order (persisting the older state), and connection flapping could compute the reconnect check against stale state and miss or spuriously fireonReconnect.Notes
resync().fetchSyncPayloadfor a hung sync — that changes sync-failure semantics broadly and is better scoped as its own task.HADataSyncBufferingTests(buffer/flush ordering, last-writer-wins, drop-oldest cap) andHAConnectionManagerTransitionTests(reconnect fires once, never spuriously). FullHemeraTestssuite green (374 tests).