Skip to main content

agentmux_srv_core/backend/blockcontroller/persistent/
stderr_reader.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The persistent CLI's stderr reader task, started by `spawn_process`
5//! (`spawn.rs`): logs every line and reacts to a stale `--resume`.
6
7use super::*;
8
9/// What the stderr reader task captures from `spawn_process`.
10pub(super) struct StderrReaderCtx {
11    pub(super) stderr_pipe: tokio::process::ChildStderr,
12    pub(super) block_id_stderr: String,
13    pub(super) inner_stderr: Arc<Mutex<PersistentInner>>,
14    pub(super) mstore_stderr: Option<Arc<Store>>,
15    pub(super) event_bus_stderr: Option<Arc<EventBus>>,
16    pub(super) attempted_resume_sid: Option<String>,
17    pub(super) my_generation_stderr: u64,
18}
19
20impl PersistentSubprocessController {
21    /// Body of the stderr reader task; `spawn_process` keeps its JoinHandle
22    /// for the process waiter.
23    pub(super) async fn run_stderr_reader(ctx: StderrReaderCtx) {
24        let StderrReaderCtx {
25            stderr_pipe,
26            block_id_stderr,
27            inner_stderr,
28            mstore_stderr,
29            event_bus_stderr,
30            attempted_resume_sid,
31            my_generation_stderr,
32        } = ctx;
33        let mut reader = BufReader::new(stderr_pipe).lines();
34        while let Ok(Some(line)) = reader.next_line().await {
35            crate::backend::blockcontroller::stderr_log::log_stderr_line(&block_id_stderr, "persistent", &line, true);
36            // Claude Code's own message when `--resume <sid>` targets a
37            // conversation its current CLAUDE_CONFIG_DIR can't see — e.g.
38            // after a relogin/reseed moves the agent onto a different
39            // config dir than the one the session was recorded under. Left
40            // uncleared, EVERY future respawn (one per message, since a
41            // dead persistent process auto-restarts on next send) keeps
42            // retrying the same unreachable --resume and immediately
43            // exits again — a permanent "Agent encountered an error" with
44            // no path to recovery. Clear it so the next respawn starts a
45            // fresh conversation instead.
46            if line.contains("No conversation found with session ID") {
47                if let Some(ref bad_sid) = attempted_resume_sid {
48                    // See PersistentInner::poison_resume — also guards
49                    // against the stdout reader's own capture (`stdout_reader.rs`)
50                    // re-adopting this same dead id if it wins the race.
51                    inner_stderr.lock().unwrap().poison_resume(bad_sid, my_generation_stderr);
52                    tracing::warn!(
53                        block_id = %block_id_stderr,
54                        session_id = %bad_sid,
55                        "stale --resume session id unreachable under the current config dir — \
56                         clearing so the next message starts a fresh conversation"
57                    );
58                    core::persist_session_id(&block_id_stderr, "", &mstore_stderr, &event_bus_stderr);
59                    // Surface this to the user — previously silent
60                    // (only the warn! above). See
61                    // SPEC_PANE_CLOSE_REOPEN_CONTINUITY_GUARANTEE_2026_07_27.md
62                    // §4.2: a resumed conversation silently starting
63                    // fresh, with no indication anything happened, is
64                    // exactly the failure mode this flag exists to close.
65                    if let Some(ref store) = mstore_stderr {
66                        crate::backend::blockcontroller::session_recovery::mark_resume_failed(
67                            store,
68                            &event_bus_stderr,
69                            &block_id_stderr,
70                        );
71                    }
72                }
73            }
74        }
75    }
76}