feat(tv): drive connection lifecycle through the FSM (spec 001, Phase 2) - #155
Open
zehnm wants to merge 1 commit into
Open
feat(tv): drive connection lifecycle through the FSM (spec 001, Phase 2)#155zehnm wants to merge 1 commit into
zehnm wants to merge 1 commit into
Conversation
Integrate the pure ConnectionFsm from Phase 1 into AndroidTv: the runtime connection state is now committed exclusively by ConnectionFsm.apply(), driven through a single _dispatch(trigger) entry point, with all side effects (tasks, library calls, event emission) performed by a thin, defensive intent executor (_execute). - connect()/disconnect()/_is_available_updated()/invalid-auth callback dispatch triggers instead of writing DeviceState (INV-1, INV-3) - command decorator gates on ConnectionState instead of DeviceState, preserving the returned StatusCodes - Stage-1 IP-rediscovery watcher and the new reconnect grace timer are wired through START/CANCEL intents; grace duration comes from connection_fsm.RECONNECT_GRACE (INV-5, no DISCONNECTED flicker) - AUTH_ERROR/UNREACHABLE are terminal until an explicit user action (INV-6); disconnect() cancels every owned task via CANCEL_TASKS (INV-7) - new Events.RECONNECTING emitted on CONNECTED -> RECONNECTING (driver-side wiring follows in Phase 3) - reconnect-owner supervision (spec F11): a done-callback on the library _reconnect_task re-establishes ownership via DISCONNECT_REQUESTED + CONNECT_REQUESTED if the task dies without cancellation or auth error - integration test suite tests/test_tv_connection.py with a fake AndroidTVRemote covering all nine Test-Plan items incl. the single-writer AST audit (AC-9); no network, no real sleeps Spec: clarify the already-connected re-emit path and the setup-flow DeviceState read after connect() (masked on main by disconnect(); unchanged or improved with Phase 2). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
zehnm
force-pushed
the
feat/connection-fsm-phase2
branch
from
July 16, 2026 11:18
02cdf2c to
0190126
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.
Summary
Implements Phase 2 of spec 001 — Connection lifecycle state machine: the runtime connection lifecycle of
AndroidTvis now driven by the pureConnectionFsmmerged in Phase 1 (#154), on top of the Stage-1 reconnection fixes (#153).What changed, for a reviewer who knows the old
tv.pyself._state = DeviceState.*runtime writes inconnect(),disconnect(),_is_available_updated()and the invalid-auth callback are gone. They are replaced by_dispatch(Trigger.*)— the single placeConnectionFsm.apply()is called — and a thin, defensive intent executor_execute()that performs all side effects (task starts,keep_reconnecting, Cast reconnect, event emission). Pairing/init keep usingDeviceState; the setup flow is untouched._initial_connect(), spawned by theSTART_INITIAL_CONNECTintent and still guarded by_connect_lock;connect()keeps its awaitableboolcontract._is_available_updatedcallback is reduced to a single trigger dispatch; transport loss arms a 1.5 s grace timer (provisional,connection_fsm.RECONNECT_GRACE) and the Stage-1 IP-rediscovery watcher via intents.Events.RECONNECTING(emitted on transport loss; driver wiring is Phase 3, harmless no-op until then).androidtvremote2reconnect-owner task is supervised via a done-callback; if it dies silently (unexpected exception, no cancellation, no auth callback), an error is logged and ownership is re-established through the existing transition table (DISCONNECT_REQUESTED→CONNECT_REQUESTED).Invariants / acceptance criteria implemented and tested
INV-1 single writer (AST audit test), INV-2 single reconnect owner (P0-2 regression test), INV-3 state/event coherence (single emit call site), INV-4 liveness-not-
is_on(P0-3 regression test), INV-5 no flicker (grace-window tests), INV-6 terminalAUTH_ERROR/UNREACHABLE, INV-7 deterministic teardown; AC-1…AC-7, AC-9 (newtests/test_tv_connection.py: 18 tests with a fakeAndroidTVRemote, no network, no real sleeps — grace tested by dispatchingGRACE_ELAPSEDdirectly), AC-11.Deviations from the spec (fed back as spec edits in this PR, per the specs README)
CONNECTED, so the single-emit-call-site and state/event coherence hold.DeviceStateread afterconnect():setup_flow.py:552also reads.stateafterconnect(timeout), which the spec's analysis missed. Not a regression — onmainthat read was always masked toDISCONNECTEDby the precedingdisconnect()write; with Phase 2, init/pairing auth/timeout classification now actually survives to the read (equal or better).Minor intended behaviour notes for the reviewer:
CONNECT_ABORTEDnow emitsDISCONNECTED(old code emitted nothing on abort); an idempotentdisconnect()fromDISCONNECTEDno longer emits; a command during a failed pairing now returnsSERVICE_UNAVAILABLEinstead ofCONFLICT(PAIRING_ERRORis a pairing-lifecycle state, out of FSM scope).Verification
python -m unittest discover tests— 92/92 pass (74 pre-existing unchanged + 18 new); run 5× consecutively, stableblack/isort/flake8clean,pylint10.00/10Phase 3 (driver event-contract cleanup, wiring
RECONNECTINGto the UI) follows as a small standalone PR.🤖 Generated with Claude Code