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
//! What a session another session starts on rho's loop takes from the
//! agent it names, before it opens: whether it may open at all under the
//! runtime its starter chose for the sessions it starts, and whether it
//! opens on its starter's own model (`session_model: starter`).
//!
//! An agent that reports to its starter (`starter_hears: reports`) runs
//! only on rho's loop. A `new` that names such an agent, and no runtime or
//! model of its own, while its starter chose another runtime for the
//! sessions it starts is refused, naming the choice, rather than run on
//! rho's loop on a model nobody chose. `new.rs` checks the choice again
//! once the session opened, so a choice a person changed meanwhile is
//! refused too.
//!
//! The agent is read from the catalog of the workspace the `new` names,
//! under its client's project trust, before any worktree is added for it,
//! so a refusal adds none. The session binds the worktree's catalog when
//! one is added, whose project agents may differ from the workspace's.
use std::path::Path;
use rho_protocol::{self as wire, ErrorCode, RpcError};
use rho_sdk::SessionId;
use super::Sessions;
use crate::{
agent::{AgentCatalog, AgentRuntime, SessionModel, StarterHears, DEFAULT_AGENT_ID},
config::{AuthOrigin, Config},
workspace::ProjectTrusts,
};
/// A `new` of a session another session starts on rho's loop, as
/// [`Sessions::started_agent`] reads it.
pub(super) struct StartRequest<'a> {
pub(super) workspace: &'a Path,
/// The agent it names; the default agent when `None`.
pub(super) agent: Option<&'a str>,
pub(super) trust: ProjectTrusts,
/// The session whose choice applies to it.
pub(super) choosing: &'a SessionId,
/// The model it opens on in place of its starter's, as the `new`
/// decided it.
pub(super) own_model: OwnModel,
}
/// Whether a `new` decided the model its session opens on itself.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum OwnModel {
/// It names a model, which replaces any other.
Named,
/// It names a runtime, so the agent it names runs where it says.
OnNamedRuntime,
/// It names neither: the agent and its starter's choice decide.
Undecided,
}
/// What the agent a started session names takes from its starter.
#[derive(Clone, Debug)]
pub(super) struct StartedAgent {
starter_hears: StarterHears,
own_model: OwnModel,
/// The model it opens on, its starter's, for an agent with
/// `session_model: starter` whose `new` named no model.
pub(super) starters: Option<StartersModel>,
}
impl StartedAgent {
/// The refusal of this agent under `runtime`, its starter's choice for
/// the sessions it starts: one that reports to its starter runs only
/// on rho's loop, so a choice of another runtime, which the `new` did
/// not replace with one of its own, refuses it.
pub(super) fn refused_under(
&self,
agent: Option<&str>,
choosing: &SessionId,
runtime: Option<wire::AgentRuntime>,
) -> Result<(), RpcError> {
let refused = match (self.starter_hears, self.own_model, runtime) {
(StarterHears::Reports, OwnModel::Undecided, Some(runtime)) => runtime,
(StarterHears::TurnEnds, ..)
| (StarterHears::Reports, OwnModel::Named | OwnModel::OnNamedRuntime, _)
| (StarterHears::Reports, OwnModel::Undecided, None) => return Ok(()),
};
let agent = agent.unwrap_or(DEFAULT_AGENT_ID);
let runtime = AgentRuntime::try_from(refused)
.map_or("another runtime", |runtime| runtime.display_name());
Err(RpcError::new(
ErrorCode::SessionOpenFailed,
format!(
"could not open the session as agent '{agent}': it reports to the session that starts it, so it runs only on rho, and a person chose {runtime} for the sessions {choosing} starts; tell them, since only they can choose a model of rho's for those sessions"
),
))
}
}
/// The model, auth and reasoning a session runs on, as its options show
/// them: what a session an agent with `session_model: starter` starts
/// opens on. Taken as that session resolved them, as a saved session
/// restores its own, since it runs them already.
#[derive(Clone, Debug)]
pub(super) struct StartersModel {
model: wire::ModelRef,
auth: String,
reasoning: Option<rho_sdk::ReasoningLevel>,
}
impl StartersModel {
/// Puts this model and reasoning on `config`, as the conversation's.
pub(super) fn apply_to(&self, config: &mut Config) {
config.set_conversation_model(
self.model.provider.clone(),
self.model.model.clone(),
self.auth.clone(),
AuthOrigin::Configured,
);
if let Some(reasoning) = self.reasoning {
config.reasoning = reasoning;
}
}
}
impl Sessions {
/// What the agent `request` names takes from its starter, refused when
/// `chosen`, its starter's choice of runtime, may not run it. `None`
/// when the catalog does not read or has no such agent: the session's
/// own bind then fails, naming why.
pub(super) async fn started_agent(
&self,
request: StartRequest<'_>,
chosen: Option<wire::AgentRuntime>,
) -> Result<Option<StartedAgent>, RpcError> {
let StartRequest {
workspace,
agent,
trust,
choosing,
own_model,
} = request;
let cwd = workspace.to_path_buf();
let id = agent.unwrap_or(DEFAULT_AGENT_ID).to_owned();
let read = tokio::task::spawn_blocking(move || {
let catalog = AgentCatalog::discover(&cwd, trust).ok()?;
let definition = &catalog.find(&id).ok()?.definition;
Some((definition.starter_hears, definition.session_model))
})
.await
.ok()
.flatten();
let Some((starter_hears, session_model)) = read else {
return Ok(None);
};
let starters = match (session_model, own_model) {
(SessionModel::Starter, OwnModel::Undecided | OwnModel::OnNamedRuntime) => {
self.starters_model(choosing)
}
(SessionModel::Starter, OwnModel::Named) | (SessionModel::StartedSessions, _) => None,
};
let started = StartedAgent {
starter_hears,
own_model,
starters,
};
started.refused_under(agent, choosing, chosen)?;
Ok(Some(started))
}
/// The model `choosing` runs on: read from its feed, as it is loaded
/// while its turn starts the session. `None` for one on another
/// runtime, whose models rho's loop does not run, or one not loaded, as
/// a session a subagent of a finished run starts; such a session opens
/// on the chosen model, as any other.
fn starters_model(&self, choosing: &SessionId) -> Option<StartersModel> {
let Some(session) = self.loaded().get(choosing).cloned() else {
tracing::info!(%choosing, "the starter is not loaded; the session opens on the chosen model");
return None;
};
match session.runtime {
AgentRuntime::Rho => {
let options = session.feed.options();
Some(StartersModel {
model: options.model.current,
auth: options.model.auth,
reasoning: options.reasoning.and_then(|reasoning| {
crate::app::session_options::reasoning_from_wire(reasoning.current)
}),
})
}
AgentRuntime::ClaudeCli | AgentRuntime::Antigravity => {
tracing::info!(%choosing, "the starter runs on another runtime; the session opens on the chosen model");
None
}
}
}
}