[bug] Forced sleep (force_rem / user_sleep_request) skips the drowsy-edge deferred-capture drain — turns strand in the spool while doctor exits 0 (3.0.2)
Summary
drain_deferred_captures is reachable from three places, and the only recurring one is an edge
check on the lifecycle FSM:
# daemon/__init__.py:165
def _should_drain_on_drowsy_edge(prev, current) -> bool:
from iai_mcp.lifecycle_state import LifecycleState as _L
return prev is _L.WAKE and current is _L.DROWSY
A forced sleep (force_rem_request or user_sleep_request) advances the machine
WAKE -> DROWSY -> SLEEP synchronously inside a single tick, before that same tick reaches the
edge check. current is already SLEEP when the check runs, so the condition is never true and
the drain is skipped for that transition.
Rotated spool files are then stranded until something else happens to trigger a drain. Throughout,
iai-mcp doctor exits 0 and every capture hook logs rc=0, so nothing surfaces it.
Version 3.0.2, Linux (systemd user unit), Python 3.12, uv tool install.
Why it is silent
doctorhas no check for "spool has rotated files that are aging".(w) no permanent-failed capturespasses, because these files never reached a failure state — nothing ever claimed them.- The capture hooks all return
rc=0: they did their job (write + rotate). The gap is downstream. iai statuskeeps reporting a healthy record count, because background consolidation continues producing records while zero conversation turns arrive.
The only way I noticed was iai last returning nothing newer than the previous day while sessions
had clearly been active.
Observed
04:13:48.571 HIBERNATION -> WAKE daemon boot; startup drain runs here (spool still empty)
04:22:49 Stop hook rotates spool file #1
04:53:09 Stop hook rotates spool file #2
05:15:36 Stop hook rotates spool file #3
05:15:46 force_rem requested
05:15:52.737 WAKE -> DROWSY <-- edge exists for 7 ms
05:15:52.744 DROWSY -> SLEEP <-- same tick
(no further transition; still SLEEP 2 h later, through an active session)
lifecycle-events-<date>.jsonl contained exactly three state_transition rows for the day — the
two above plus boot. Meanwhile the backlog kept growing (3 files -> 5 files, 26 -> 43 events) because
each Stop-hook rotation adds a file and none were being claimed.
Natural idle sleep is unaffected: it dwells in DROWSY for ~25 min
(IDLE_5MIN -> DROWSY, then IDLE_30MIN -> SLEEP), which the 30 s tick observes easily. The
previous day, with only idle-driven transitions, drained normally. So this reproduces specifically
on the forced path.
Root cause
daemon/__init__.py:2461-2479 dispatches FORCE_SLEEP twice back to back:
_force_rem = bool((_ds.get("force_rem_request") or {}).get("pending"))
_user_sleep = bool((_ds.get("user_sleep_request") or {}).get("pending"))
if _force_rem or _user_sleep:
await _state_machine.dispatch(_LifecycleEvent.FORCE_SLEEP, reason="force_sleep_request")
if _state_machine.current_state is _LifecycleState.DROWSY:
await _state_machine.dispatch(
_LifecycleEvent.FORCE_SLEEP, reason="force_sleep_drowsy_to_sleep",
)
Both hops complete here. Further down the same tick, daemon/__init__.py:2557:
current = _state_machine.current_state # already SLEEP
if _should_drain_on_drowsy_edge(_prev_lifecycle_state[0], current): # False
... drain_capture_backlog ...
Note this covers user_sleep_request too, so the user-facing "sleep now" path has the same effect,
not just force_rem.
Why the other two triggers do not cover it
- Startup drain (
daemon/__init__.py:2096) — only at boot. Anything rotated afterwards waits. session_refresh_if_staleRPC (core/__init__.py:1811-1819) — the handler does calldrain_deferred_captures(store), but the client-side gate that decides whether to open the socket measures other sessions' growth withentry.name.endswith('.live.jsonl')(hooks/iai-mcp-turn-capture.sh,_gate_get_other_live_size). The moment the Stop hook renames a file to<session>.live-<epoch>-<pid>.jsonlthat suffix stops matching, so a rotated-but-undrained file can never itself be the reason the gate fires.
So after a forced sleep there is no scheduled path back to the backlog until the daemon happens to
pass through an observably-DROWSY tick, or is restarted.
What is not the cause
Ruling this out to save time, since the filename shape invites it: the rotated
<session>.live-<epoch>-<pid>.jsonl names do pass the candidate filter.
_LIVE_ACTIVE_RE = re.compile(r"\.live\.jsonl$") (capture.py:93) is anchored and does not match
them, so they are correctly treated as drainable in both _drain_deferred_captures_locked and
deferred_drain.enumerate_backlog. Eligibility was never the problem — nothing invoked the drain.
IAI_MCP_DISABLE_INDAEMON_DRAIN was confirmed unset on the live process.
Recovery works
systemctl --user restart iai-mcp-daemon — the startup drain claimed the whole backlog:
5 rotated files -> 0, records 128 -> 202, spool directory left completely empty (no
.processing-*, .partial, or .failed-* residue), and the previously-missing turns became
recallable via iai last. So this is a scheduling gap, not corruption, and no data is lost.
Suggested fix
Any one of these would close it:
- Make the drain a level check rather than an edge one — e.g. drain when the backlog is
non-empty and the state is not
WAKE, instead of keying on a transition that a same-tick double dispatch erases. - Drain explicitly in the force-sleep branch, before the second
FORCE_SLEEPdispatch. - Capture
prev/currentaround each dispatch so aWAKE -> DROWSYhop is observed even when a later hop lands in the same tick.
Separately, a doctor check for "deferred-capture backlog older than N minutes" would have turned
a two-hour silent gap into an immediate red row — the failure mode here is precisely that every
existing signal stayed green.
Happy to test a patch.
Fixed in v3.0.5: deferred captures now drain on every entry into sleep (including forced sleep). Thanks for the precise repro.