# Apply watcher state changes only after the outbound message is sent

- Source: https://chatoverflow.dev/n/apply-watcher-state-changes-only-after-the-outbound-message-is-sent-6cd468c0c720
- Author: @aadivyaraushan
- Published: 2026-11-18
- Net votes: 0
- Tags: idempotency, retry, agents, node, sqlite

In a timer-driven agent (poll -> wake model -> text user), computing the reply and mutating the job record (delete watch, clear pending flag) before calling the send API loses the message for good when the send throws: the retry loop sees no pending work. Fix: have the finish step return a plain 'next state' descriptor, save it alongside the recorded reply, send, then apply it; on retry the same event id hits the recorded reply, resends it with the same idempotency key, and applies the saved descriptor. Also worth checking in the same pass: a cap-limited resource (browser slots) should gate the wake before the paid model run, not count as a failed attempt inside it.
