1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Attaching a session again on a new connection once the daemon
//! restarted: what the reattach asks for and what it comes back with.
use std::{future::Future, pin::Pin, sync::Arc};
use rho_protocol::{
methods::{SessionAttach, SessionNew},
ClientEnvironment, ErrorCode, SessionAttachParams, SessionAttachResponse, SessionId,
SessionNewParams, SessionOptions,
};
use tokio::sync::mpsc;
use super::{
initial_settings::{carry, changes_between, ModelChange},
route,
tool_sources::ToolSources,
DaemonSettings,
};
use crate::{
app::daemon_client::{Daemon, Link, Routed, SettingsChange},
tui::daemon_settings_read::{load_settings, route_settings},
};
pub(super) type Reattach = Pin<Box<dyn Future<Output = anyhow::Result<Reattached>> + Send>>;
/// What a reattach came back with.
pub(super) struct Reattached {
pub(super) response: SessionAttachResponse,
/// After the daemon restarted: where the session attached again.
pub(super) relinked: Option<Relinked>,
}
/// The session's attachment after the daemon restarted.
pub(super) struct Relinked {
/// The connection it attached on.
pub(super) link: Arc<Link>,
/// The route its events now arrive on.
pub(super) routed: mpsc::UnboundedReceiver<Routed>,
/// The session's id: a new one when the old one had saved nothing.
pub(super) session: SessionId,
/// The settings, read again: the restarted daemon knows neither
/// this connection's subscription nor the TUI's registration.
pub(super) settings_changes: mpsc::UnboundedReceiver<SettingsChange>,
pub(super) settings: anyhow::Result<DaemonSettings>,
/// Whether the session's MCP servers connect, read again: the
/// restarted daemon starts them again.
pub(super) tool_sources: ToolSources,
}
/// Attaching a session again once the daemon restarted.
pub(super) struct Relink {
pub(super) daemon: Arc<Daemon>,
/// The attach, after the session's cursor.
pub(super) params: SessionAttachParams,
/// What opened the session, when it had saved nothing yet, and
/// the options it ran with, which a new session in its place takes.
pub(super) opened: Option<(SessionNewParams, Option<SessionOptions>)>,
}
impl Relink {
/// Waits for `old`, the connection the daemon detached the session
/// on, to close, then attaches on the next one, which waits for the
/// restarted daemon. Once the router handed the session's old route
/// `Lost`, a new route belongs to the next connection.
pub(super) async fn run(self, old: Option<Arc<Link>>) -> anyhow::Result<Reattached> {
let Self {
daemon,
params,
opened,
} = self;
if let Some(old) = old {
old.closed().await;
}
let routed = route(&daemon, ¶ms.session);
let settings_changes = route_settings(&daemon);
// A daemon that failed to come back resets the connections that
// waited for it; the second try starts a new daemon.
let link = match daemon.link().await {
Ok(link) => link,
Err(_) => daemon.link().await?,
};
let settings = load_settings(&link).await;
match link.request::<SessionAttach>(¶ms).await {
Ok(response) => Ok(Reattached {
response,
relinked: Some(Relinked {
tool_sources: ToolSources::read(&link, ¶ms.session).await,
link,
routed,
session: params.session,
settings_changes,
settings,
}),
}),
Err(error) => match opened {
Some((mut opened, carried))
if error.error_code() == Some(ErrorCode::SessionNotFound) =>
{
daemon.unroute(¶ms.session);
opened.environment = ClientEnvironment::of_this_process();
let created = link.request::<SessionNew>(&opened).await?;
let session = created.session;
// The settings the person changed carry over; one the
// restarted daemon refuses keeps its default.
let changes = carried
.map(|carried| {
changes_between(&created.options, &carried, ModelChange::Setup)
})
.unwrap_or_default();
carry(&link, &session, changes).await;
let routed = route(&daemon, &session);
let attach = SessionAttachParams {
session: session.clone(),
after: None,
..params
};
let response = link.request::<SessionAttach>(&attach).await?;
Ok(Reattached {
response,
relinked: Some(Relinked {
tool_sources: ToolSources::read(&link, &session).await,
link,
routed,
session,
settings_changes,
settings,
}),
})
}
_ => Err(error.into()),
},
}
}
}