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
//! Observing sessions (`rho/session/observe`): following a session's events
//! as an attach does, for a client that only reads it, such as the `read`
//! and `wait` tools of `rho mcp serve` (#1672). Observing never loads a
//! session. One the daemon has loaded is followed as an attach follows it,
//! keeping the environment it runs with. One a subagent or workflow agent
//! node run is writing is followed through the run's stream, from when the
//! run opens it until the run ends (#1668; see `child_feeds`). A saved one it has not loaded is
//! opened only to be read, starting none of its MCP servers, OpenAPI
//! sources, or hooks, replayed as its file keeps it with how its last turn
//! ended, and shut down again; the client is then detached as
//! [`DetachReason::NotLoaded`]. A reopen would start the session's servers
//! with the reader's environment, at the reader's nesting depth rather
//! than the session's own. A saved session another engine ran, a subagent
//! run on an external agent runtime, replays what the run showed instead
//! (see `engine_replay`).
use rho_protocol::{RpcError, SessionAttachAnswer, SessionObserveQuery, Subscription};
use super::{
attach_response, engine_replay,
locate::Located,
reopen::{parse_session, ReopenGuard},
unloaded::Unloaded,
ResumeFrom, Sessions,
};
impl Sessions {
/// Subscribes a client to a session's events without loading it: a
/// loaded session's as [`Self::attach`] does, else a replay of what the
/// session saved, after which the client is detached.
pub(in crate::app::daemon_host) async fn observe(
&self,
query: SessionObserveQuery,
) -> Result<(SessionAttachAnswer, Subscription), RpcError> {
self.refuse_when_stopping()?;
let id = parse_session(&query.params.session)?;
let after = query.params.after.as_ref();
let from = ResumeFrom {
after,
entry: query.after_entry.as_ref(),
window: query.window,
deferral: query.defer_tool_output,
};
loop {
match self.locate(&id).await {
Located::Loaded(session) => {
// Under the turn lock and marked in use, as an attach,
// so the idle unload cannot take it meanwhile. Taken
// already: looked up again, which no longer finds it
// loaded.
let Some(_turn) = session.enter() else {
continue;
};
return Ok(attach_response(
&session.workspace,
session.feed.attach(from),
));
}
// A subagent run under way writes it: its stream is the run's.
Located::Unloaded { run: Some(run), .. } => return Ok(run.attach(from)),
Located::Unloaded {
run: None,
reopening,
} => return self.observe_unloaded(from, &reopening).await,
}
}
}
/// The replay of the saved session whose reopen lock `reopening` holds,
/// which the daemon has not loaded, as a client resuming from `from`
/// gets it, then nothing: the session is read and shut down again (see
/// [`Self::read_unloaded`]).
async fn observe_unloaded(
&self,
from: ResumeFrom<'_>,
reopening: &ReopenGuard<'_>,
) -> Result<(SessionAttachAnswer, Subscription), RpcError> {
let id = reopening.id();
match self.read_unloaded(reopening).await? {
Unloaded::Engine {
workspace,
engine,
run,
} => engine_replay::read(id, &workspace, *engine, from.after, run.as_deref()).await,
Unloaded::Saved { workspace, feed } => {
Ok(attach_response(&workspace, feed.attach(from)))
}
}
}
}