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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! The session catalog over the socket (#1670, #1667): `rho/session/list`,
//! `rho/session/rename`, and `rho/session/delete`, over the session store's
//! catalog, titles, and delete, which ACP's `session/list` and
//! `session/delete` and `rho sessions` use too.
//!
//! A rename tells a loaded session's attached clients the new title. A
//! delete must unload a loaded session first, since its runtime holds the
//! file's lease: it refuses one with a turn or background work under way,
//! or a settings change queued or applying, or whose delete the store would
//! refuse, holds it from prompts and changes while it tears it down, and
//! then detaches its clients as `deleted`.
use std::{num::NonZeroU32, num::NonZeroUsize, sync::Arc};
use rho_protocol::{
self as wire, DetachReason, ErrorCode, RpcError, SessionDeleteParams, SessionDeleteResponse,
SessionListParams, SessionListResponse, SessionRenameParams, SessionRenameResponse,
};
use rho_sdk::SessionId;
use super::{
being_deleted,
reopen::{not_found, parse_session},
stopping,
store::{store_blocking, store_failed},
t3_wake::WakeSignal,
writing, Held, Hold, LoadedSession, Sessions,
};
use crate::{
app::daemon_host::jobs::SessionJob,
session::{
catalog::{self, CatalogError},
ExactDelete, NoSessionFound, Session as StoredSession, SessionSummary,
},
};
/// The most sessions one `rho/session/list` page holds.
//
// Measured on a dev machine's store: its 249 listable sessions encode to
// 86 KB of summaries, 393 B at most each, so a full page is about 400 KB,
// far under the 16 MiB line limit (`rho_protocol::MAX_LINE_BYTES`), and four
// times that whole store still lists in one page. A client that wants more
// follows `next_cursor`.
const MAX_PAGE_SIZE: usize = 1000;
impl Sessions {
/// One page of the saved sessions a person can resume, newest first.
pub(in crate::app::daemon_host) async fn list(
&self,
params: SessionListParams,
) -> Result<SessionListResponse, RpcError> {
let SessionListParams { cwd, cursor, limit } = params;
let limit = page_size(limit)?;
let page = store_blocking("list the sessions", move || {
Ok(catalog::list(cwd.as_deref(), cursor.as_deref(), limit))
})
.await?
.map_err(|error| match error {
CatalogError::RelativeCwd | CatalogError::UnknownCursor(_) => {
invalid(error.to_string())
}
CatalogError::Store(error) => store_failed("list the sessions", &error),
})?;
Ok(SessionListResponse {
sessions: page.sessions.into_iter().map(summary_to_wire).collect(),
next_cursor: page.next_cursor,
})
}
/// Saves a session's title, then tells the clients attached to it, if
/// the daemon has it loaded.
pub(in crate::app::daemon_host) async fn rename(
&self,
params: SessionRenameParams,
) -> Result<SessionRenameResponse, RpcError> {
let id = parse_session(¶ms.session)?;
let renamed = {
let id = id.clone();
store_blocking("rename the session", move || {
let Some(cwd) = StoredSession::workspace_of(id.as_str())? else {
return Ok(None);
};
match StoredSession::set_title(&cwd, id.as_str(), ¶ms.title) {
Ok(update) => Ok(Some(update.title)),
// Deleted since the lookup.
Err(error) if error.is::<NoSessionFound>() => Ok(None),
Err(error) => Err(error),
}
})
.await?
};
let title = renamed.ok_or_else(|| not_found(&id))?;
let loaded = self.loaded().get(&id).cloned();
if let Some(session) = loaded {
session
.feed
.publish([wire::SessionEvent::TitleChanged { title }]);
}
Ok(SessionRenameResponse {})
}
/// Deletes a saved session and the sessions it started, unloading it
/// first when the daemon has it loaded.
pub(in crate::app::daemon_host) async fn delete(
&self,
params: SessionDeleteParams,
) -> Result<SessionDeleteResponse, RpcError> {
self.refuse_when_stopping()?;
let id = parse_session(¶ms.session)?;
// Both held until the files are gone: the stop waits for the first,
// and a request that would reopen the session, or link or unlink a
// pull request for it, waits for the second, then finds nothing
// saved.
let _unloading = self.unloading.read().await;
let _reopening = self.reopening.lock(&id).await;
let loaded = self.loaded().get(&id).cloned();
match &loaded {
Some(session) => {
self.unload_to_delete(&id, session, params.unless_attached)
.await?;
}
None => {
if let SessionJob::Busy(job) = self.jobs.session_job(&id) {
return Err(writing(&job, &id));
}
}
}
let deleted = {
let id = id.clone();
store_blocking("delete the session", move || {
StoredSession::delete_exact(id.as_str())
})
.await
};
if deleted.is_ok() {
// A T3 wake watcher's recorded wait goes with its session.
WakeSignal::forget(&self.wakes, id.as_str());
self.pull_requests.forget(&id);
}
let child_sessions = match (loaded, deleted) {
(Some(session), Ok(ExactDelete::Deleted(outcome))) => {
session.feed.close(DetachReason::Deleted);
outcome.deleted_child_session_count
}
// A session loaded before it saved anything has no files: it is
// gone once unloaded.
(Some(session), Ok(ExactDelete::NothingSaved)) => {
session.feed.close(DetachReason::Deleted);
0
}
// The store refused after its check passed, as on an I/O error,
// with the session unloaded: its clients attach again to reopen
// it.
(Some(session), Err(error)) => {
session.feed.close(DetachReason::Lagged);
return Err(error);
}
(None, Ok(ExactDelete::Deleted(outcome))) => outcome.deleted_child_session_count,
(None, Ok(ExactDelete::NothingSaved)) => return Err(not_found(&id)),
(None, Err(error)) => return Err(error),
};
tracing::info!(session = %id, child_sessions, "deleted session");
Ok(SessionDeleteResponse {})
}
/// Unloads `session`, loaded as `id`, for its delete, unless a settings
/// change is queued or applying, its turn or background work is under
/// way, or the store would refuse the delete. Held from prompts and
/// settings changes meanwhile; once this returns, the session is out of
/// `loaded` and its runtime, which held its file, torn down.
async fn unload_to_delete(
&self,
id: &SessionId,
session: &Arc<LoadedSession>,
unless_attached: bool,
) -> Result<(), RpcError> {
let held = {
// Under the turn lock a prompt starts its turn under, so this
// sees every turn started before it, and a prompt after it sees
// the delete.
let mut turn = session.turn();
match turn.hold {
Some(Hold::Configuring(_)) => return Err(busy(CHANGE_QUEUED)),
// Deletes of one session wait on its reopen lock, so none
// finds another under way.
Some(Hold::Deleting) => return Err(being_deleted()),
None => {}
}
if unless_attached && session.feed.followed() {
return Err(open_in_a_client(id));
}
match self.jobs.session_job(id) {
SessionJob::Busy(job) if job == session.job => return Err(busy(TURN_UNDER_WAY)),
SessionJob::Busy(job) => return Err(writing(&job, id)),
SessionJob::New | SessionJob::Last(_) => {}
}
Held::delete(session, &mut turn)
};
// Free once a turn that just recorded its end released it.
let mut slot = session.runtime.lock().await;
// Only the daemon's stop takes the runtime of a session in use.
let Some(runtime) = slot.take() else {
return Err(stopping());
};
if runtime.has_background_work() {
*slot = Some(runtime);
return Err(busy(BACKGROUND_WORK));
}
// The store's refusals, checked while the session is still loaded:
// a delete it refuses leaves the session as it was.
let checked = {
let id = id.clone();
store_blocking("check the session's delete", move || {
StoredSession::check_delete_exact(id.as_str())
})
.await
};
if let Err(error) = checked {
*slot = Some(runtime);
return Err(error);
}
{
let mut turn = session.turn();
// Again, under the lock that evicts: a client that attached
// while the checks above waited is not detached by the delete,
// and one that attaches from here finds the session gone.
if unless_attached && session.feed.followed() {
*slot = Some(runtime);
return Err(open_in_a_client(id));
}
// Only the stop takes a session out of `loaded` while its
// reopen lock is held, as it is here.
if !self.evict(id, session, &mut turn) {
*slot = Some(runtime);
return Err(stopping());
}
}
drop(slot);
runtime.shutdown().await;
drop(held);
Ok(())
}
}
const TURN_UNDER_WAY: &str = "the session has a turn in progress; delete it once the turn ends";
const CHANGE_QUEUED: &str =
"a change to the session's settings is queued or applying; delete it once the change has applied";
const BACKGROUND_WORK: &str =
"background work the session started, such as a subagent, is still running; delete it once that ends";
/// `limit` as a page size: [`catalog::DEFAULT_PAGE_SIZE`] when absent, and
/// refused over [`MAX_PAGE_SIZE`].
fn page_size(limit: Option<NonZeroU32>) -> Result<NonZeroUsize, RpcError> {
let Some(limit) = limit else {
return Ok(catalog::DEFAULT_PAGE_SIZE);
};
NonZeroUsize::try_from(limit)
.ok()
.filter(|size| size.get() <= MAX_PAGE_SIZE)
.ok_or_else(|| {
invalid(format!(
"limit {limit} is over the session list's page limit (MAX_PAGE_SIZE = {MAX_PAGE_SIZE})"
))
})
}
/// `summary` as `rho/session/list` sends it.
fn summary_to_wire(summary: SessionSummary) -> wire::SessionSummary {
let SessionSummary {
id,
path: _,
cwd,
created_at: _,
updated_at,
message_count: _,
title,
first_user_message,
last_user_message,
added_dirs,
origin: _,
} = summary;
wire::SessionSummary {
session: wire::SessionId::new(id),
cwd,
added_dirs,
title,
first_user_message: first_user_message.as_deref().and_then(catalog::preview),
last_user_message: last_user_message.as_deref().and_then(catalog::preview),
updated_at,
}
}
fn invalid(message: impl Into<String>) -> RpcError {
RpcError::new(ErrorCode::InvalidParams, message)
}
/// The refusal of a delete that spares a session a client follows.
fn open_in_a_client(id: &SessionId) -> RpcError {
RpcError::new(
ErrorCode::SessionInUse,
format!("session {id} is open in a client; close it there first"),
)
}
fn busy(message: &str) -> RpcError {
RpcError::new(ErrorCode::SessionBusy, message)
}
#[cfg(test)]
#[path = "catalog_tests.rs"]
mod tests;