Memory-mode turn recovery reaps live CLI turns when a detached server shares the runtime home (worker_lost / "Turn event conflict")
Summary
In the default single-process (memory-coordinator) mode, TurnRecoveryService treats every persisted nonterminal turn that has no lease in its own process as an expired worker. When a detached Web app (deeptutor start --detach) and a CLI turn (deeptutor run chat) share the same DEEPTUTOR_HOME, the server cannot see the CLI process's in-memory lease, so it fails the live CLI turn as worker_lost. The CLI's next event-batch flush then collides with the recovery terminal events and the turn dies with Turn event conflict.
Environment
- Branch/commit:
dev@e495fd60 - Source install (
requirements/full.txt+pip install -e . --no-deps), Python 3.11, Linux - Single-user local mode, memory coordinator (no Redis)
- Any configured LLM (issue is provider-independent)
Reproduction (deterministic, 2/2)
deeptutor start --detach --no-browserwith a working model configured- In the same runtime home:
deeptutor run chat "Reply with a short greeting." --format json - The turn streams real events (thinking/content deltas), then fails
With the server stopped first (deeptutor stop), the identical command completes normally (done / status: completed), so the model path and CLI wiring are fine.
Observed
- CLI stderr, from
sqlite_store.append_eventsvialifecycle._flush_buffered_events:ValueError: Turn event conflict: <turn_id> seq=4 - Terminal events surfaced to the CLI:
error: The worker executing this turn was lost; regenerate to retry(error_code: worker_lost) - Stored
turn_eventsfor the failed turn:
| seq | type | source |
|---|---|---|
| 1–3 | session / stage_start / progress | live turn |
| 4 | error (worker_lost) |
turn_recovery |
| 5 | done | turn_recovery |
Root cause
TurnRecoveryService.recover_once (deeptutor/runtime/coordination/recovery.py): in the coordinator.mode == "memory" branch, every row from repository.list_nonterminal_turns() whose lease is absent from this process's coordinator is added as a recovery candidate. The comment describes the intended case — the same process restarting and losing its in-memory lease index — but the same code path also reaps a turn that is live in a different process (the CLI) whose lease is, by design, invisible to the server. _recover_turn then transitions the still-running turn to failed and appends recovery error+done events, consuming sequence numbers that the CLI's flusher is about to write with different content — hence the conflict (_append_turn_events_sync correctly rejects a same-seq payload mismatch).
Workaround
Keep one turn-owning process per runtime home: stop the detached app (deeptutor stop) before running CLI turns, or use the Web app for chats.
Possible directions (maintainer call)
- Record the owning PID (or PID + boot id) on the turn row at admission and check liveness in memory mode before reaping.
- Reap only rows whose
turns.updated_atis older than a grace window (event-batch flushes already bump it); requires bounding how long a quiet-but-alive turn can go without a flush. - Scope the memory-mode persisted scan to rows known to predate this process's startup.
Happy to help test any of these — I have a deterministic reproduction.
Independent reproduction on the stable 1.7.1 PyPI release
Same symptom here, so this is not specific to dev or to a source install.
Environment
deeptutor[full]1.7.1 from PyPI, installed into a venv withuv pip install- macOS 27.0.1 arm64, Python 3.13.13
- Single-user local mode, memory coordinator, no Redis
- LLM
deepseek-chat; embeddings through a local OpenAI-compatible proxy
Setup
deeptutor start --detach --no-browser- In the same runtime home:
deeptutor run chat "..." --kb <LlamaIndex KB>
Observed
File ".../deeptutor/services/session/sqlite_store.py", line 1833, in _append_turn_events_sync
raise ValueError(f"Turn event conflict: {turn_id} seq={seq}")
ValueError: Turn event conflict: turn_1793715780327_91d272c9a9 seq=13
Four failures out of four attempts with the detached server running, always at seq=13. The fastest failure came about 7 s into the turn; the earlier ones ran up to 22 s before dying the same way.
Control
The identical command with the server stopped first (deeptutor stop) completes normally: retrieval sources are cited, session= and turn= are printed, and the turn exits 0. deeptutor kb search is unaffected either way, since it does not create a turn.
Extra observation
A short turn without --kb completed twice while the detached server was running, while every --kb turn failed. That points at "the turn is still live when the background recovery pass runs" as the trigger, with the retrieval/embedding path simply keeping the turn open long enough, rather than at the KB path itself.
Workaround I use until this lands: stop the detached server before running CLI turns, or point the CLI at a separate DEEPTUTOR_HOME.
Fixed on local dev (e885120cf6651952d3e4010ea7b80a53d84888b3). Real child-process recovery: 16 tests passed, including queued, running, waiting_input, process exit and PID reuse. Existing coordination/application tests: 65 passed, 10 optional Redis skips.
will be fixed in the next releases!