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
//! What runs a loaded session's turns: Rho's own loop, or an agent on an
//! external runtime over ACP (see [`super::acp_turns`]).
//!
//! The engine sits in the session's turn slot, which turns, settings
//! changes and the daemon's stop take one at a time either way. A client's
//! request that only Rho's loop can serve, such as a model change, the
//! tree, or an MCP reload, reaches it through [`Engine::rho_for`], which
//! names the capability the request needs and refuses it, typed, for a
//! session an agent runs (see `capabilities.rs`).
use std::path::Path;
use rho_protocol::{RpcError, SessionCapability};
use rho_sdk::SessionId;
use super::{acp_turns::AcpTurns, capabilities};
use crate::app::daemon_host::jobs::JobAgent;
use crate::{
agent::AgentRuntime,
app::{background_delivery::CountedWork, session_runtime::SessionRuntime},
session::SessionPersistence,
};
pub(super) enum Engine {
Rho(Box<SessionRuntime>),
Acp(Box<AcpTurns>),
}
impl Engine {
pub(super) fn session_id(&self) -> &SessionId {
match self {
Self::Rho(runtime) => runtime.session_id(),
Self::Acp(turns) => turns.session_id(),
}
}
pub(super) fn workspace_root(&self) -> &Path {
match self {
Self::Rho(runtime) => runtime.workspace_root(),
Self::Acp(turns) => turns.workspace(),
}
}
pub(super) fn persistence(&self) -> SessionPersistence {
match self {
Self::Rho(runtime) => runtime.persistence(),
// An agent's own session outlives each turn's process, so its
// daemon session is always saved.
Self::Acp(_) => SessionPersistence::Saved,
}
}
/// Whether the session has a file: always, for one an agent runs.
pub(super) fn is_saved(&self) -> bool {
match self {
Self::Rho(runtime) => runtime.is_saved(),
Self::Acp(_) => true,
}
}
/// Whether work the session's turns started runs on, or holds a result
/// or a child's notice no turn took, as `counted` says, which a teardown
/// would stop or drop. An agent's own process ends with its turn, but
/// what the tools rho serves it started runs on; a result counts only
/// where the daemon wakes the session to deliver it (see
/// `acp_background::has_work`).
pub(super) fn has_background_work(&self, counted: CountedWork) -> bool {
match self {
Self::Rho(runtime) => runtime.has_background_work(counted),
Self::Acp(turns) => turns.served().is_some_and(|served| {
super::acp_background::has_work(turns.session_id().as_str(), served, counted)
}),
}
}
/// The agent the session runs as now, and what runs it, as its job
/// records them.
pub(super) fn job_agent(&self) -> JobAgent {
let id = match self {
Self::Rho(runtime) => runtime.agent_id().as_str().to_owned(),
Self::Acp(turns) => turns.agent_id().to_owned(),
};
JobAgent {
id,
engine: self.runtime().into(),
}
}
/// What runs the session: Rho's loop, or the agent's runtime.
pub(super) fn runtime(&self) -> AgentRuntime {
match self {
Self::Rho(_) => AgentRuntime::Rho,
Self::Acp(turns) => turns.runtime(),
}
}
/// Rho's loop, to serve a client's request for `capability`, which only
/// it does; for a session an agent runs, the typed refusal.
pub(super) fn rho_for(
&self,
capability: SessionCapability,
) -> Result<&SessionRuntime, RpcError> {
match self {
Self::Rho(runtime) => Ok(runtime),
Self::Acp(turns) => Err(capabilities::rho_only(turns.runtime(), capability)),
}
}
/// [`Self::rho_for`], to change it.
pub(super) fn rho_for_mut(
&mut self,
capability: SessionCapability,
) -> Result<&mut SessionRuntime, RpcError> {
match self {
Self::Rho(runtime) => Ok(runtime),
Self::Acp(turns) => Err(capabilities::rho_only(turns.runtime(), capability)),
}
}
/// Rho's loop, when it runs the session, for what the daemon does with
/// it on its own, such as warming its cache. A client's request goes
/// through [`Self::rho_for`] instead, which refuses it typed.
pub(super) fn as_rho(&self) -> Option<&SessionRuntime> {
match self {
Self::Rho(runtime) => Some(runtime),
Self::Acp(_) => None,
}
}
/// [`Self::as_rho`], to change it.
pub(super) fn as_rho_mut(&mut self) -> Option<&mut SessionRuntime> {
match self {
Self::Rho(runtime) => Some(runtime),
Self::Acp(_) => None,
}
}
/// Tears the engine down: Rho's loop stops its tools; an agent has no
/// process between turns, but the tools rho served it may have started
/// work, which stops.
pub(super) async fn shutdown(self) {
match self {
Self::Rho(runtime) => runtime.shutdown().await,
Self::Acp(turns) => turns.shutdown().await,
}
}
/// Tears down a new session whose open was refused, leaving nothing
/// saved: Rho's loop saves nothing before its first prompt, but an
/// agent's session wrote its file as it opened.
pub(super) async fn discard_new(self) {
match self {
Self::Rho(runtime) => runtime.shutdown().await,
Self::Acp(turns) => turns.discard(),
}
}
}