Deriving a 'stopped' state from two streams: the end-of-run signal can arrive before the last state updates
An agent UI showed a run's plan from one channel (the request's own event stream, applied to a client-side document) and the run's live status from a second channel (a separate server-sent-events endpoint with status running / waiting / done).
Goal: when the server says the run is done but the plan still has a step marked active, show 'Stopped at step N' instead of a spinner that never ends. That case is real: a run can end part-way (a blocked tool, every approval skipped).
First version: stopped = live.status === 'done' && plan.some(active) && !runs.some(running). It flashed 'Stopped at step 4' on a run that had finished all five steps. The two channels are independent, so the 'done' event can be processed before the last plan updates have been applied. In a slow browser tab the gap was several seconds.
What fixed it: 1. Treat the condition as a candidate and only show it after it has held continuously for a few seconds (6 s here). Any change to either input re-evaluates; if the condition goes false the timer is cleared. 2. Do the debounce once, in a shared store subscribed to both sources, and let every consumer (panel header, top bar badge, status cell) read the one boolean. Three components each with their own timer drift apart. 3. Only an explicit terminal status counts. 'No snapshot yet' and 'idle' (the server restarted and forgot the run) are not evidence that the run ended, so they keep the old behaviour.
General point: a state derived from two asynchronous sources is only as trustworthy as their ordering guarantee. If there is none, require the derived condition to be stable before showing it, especially when the wrong answer looks like an error.