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
//! Where a request that reads a session without loading it finds the
//! session: `rho/session/observe`, `rho/session/history` and
//! `rho/session/tool_output`. Each takes what it can from where the
//! session is, as its own module says: a loaded session's feed, the stream
//! of a subagent or workflow agent node run under way that writes it, or
//! its saved file.
use std::sync::Arc;
use rho_sdk::SessionId;
use super::{child_feeds::RunStream, reopen::ReopenGuard, LoadedSession, Sessions};
/// Where a session is, for a request that reads it.
pub(super) enum Located<'a> {
/// The daemon has it loaded.
Loaded(Arc<LoadedSession>),
/// The daemon has not loaded it. `reopening`, its reopen lock, is held,
/// so no request loads it meanwhile, and a read of its file, which
/// takes the lock as proof, does not find the file held by the daemon.
/// `run` is the stream of the subagent or workflow agent node run under
/// way that writes it, once the run opened it. While such a run's job is
/// under way, [`Sessions::read_unloaded`] refuses the session as
/// `SessionBusy`.
Unloaded {
run: Option<RunStream>,
reopening: ReopenGuard<'a>,
},
}
impl Sessions {
/// Finds session `id`: loaded, or not, once no request loading it holds
/// its reopen lock.
pub(super) async fn locate(&self, id: &SessionId) -> Located<'_> {
loop {
let loaded = self.loaded().get(id).cloned();
if let Some(session) = loaded {
return Located::Loaded(session);
}
let reopening = self.reopening.lock(id).await;
// A request that held the lock first may have loaded it.
if self.loaded().contains_key(id) {
continue;
}
return Located::Unloaded {
run: self.subagents.feeds().run(id),
reopening,
};
}
}
}