| name | state-machine |
| description | Expert in RemoteCamSession's hierarchical state machine. Use when working with state transitions, `become`/`unbecome`/`popToState`, adding new states, or debugging state flow issues. |
| allowed-tools | Read, Grep, Glob, Edit, Write |
RemoteCamSession State Machine Expert
Context
RemoteCamSession implements a hierarchical state machine using Theater's become/unbecome/popToState pattern.
State Hierarchy
States are defined in RemoteCamStates (States.swift):
scanning
โโโ connected (role selection)
โโโ camera
โ โโโ cameraTakingPic
โ โโโ cameraRecordingVideo
โโโ monitorPhotoMode
โโโ monitorTakingPicture
โโโ monitorTogglingFlash
โโโ monitorTogglingCamera
โโโ monitorSwitchingLens
โโโ monitorVideoMode
โโโ monitorRecordingVideo
State Files
- RemoteCamScanning.swift โ
scanning: peer discovery via MultipeerConnectivity
- RemoteCamConnected.swift โ
connected: role selection (camera vs monitor)
- CamStates.swift โ
camera, cameraTakingPic, cameraRecordingVideo
- MonitorStates.swift โ
monitorTogglingFlash, monitorTogglingCamera, monitorSwitchingLens
- MonitorPhotoStates.swift โ
monitorPhotoMode, monitorTakingPicture
- MonitorVideoStates.swift โ
monitorVideoMode, monitorRecordingVideo
- CameraVideoStates.swift โ Camera-side video recording states
Key Patterns
State Transitions
become(stateName, state) โ Push a new state onto the stack
unbecome() โ Pop current state, return to previous
popToState(stateName) โ Pop states until reaching the named state
OnEnter Handlers
- Each state can define an
OnEnter block that runs when the state is entered
- Common pattern:
OnEnter dispatches UI setup to main thread via ^{ }
Error Recovery
- Most error conditions trigger
popToState("scanning") to reset to discovery
sendCommandOrGoToScanning helper sends a remote command and falls back to scanning on failure
Rules
- Always define new states in
RemoteCamStates enum (States.swift)
- Implement state behavior in the appropriate state file
- Handle the error/disconnect case โ usually pop back to scanning
- Use
OnEnter for state initialization, not the message handler
- Test state transitions using
statesStack assertions