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
//! Questions answered by an agent rather than a person.
//!
//! `rho/session/answer_question`: the session that started the asking one
//! through session control answers (#1714). An agent's connection may call
//! it, so the daemon checks the answering session against its own job
//! records before it answers (`JobRegistry::may_answer`, the one copy of
//! the rule). The answering session is the one the caller declares, as a
//! new session's parent is: the daemon cannot tell who sends a request.
//!
//! `rho/session/answer_question_by_agent`: an agent outside any session
//! answers, as an MCP client of `rho mcp serve` does. Like a prompt or a
//! steer, it is the caller's to hold to who may drive the session: the
//! daemon checks only the request.
//!
//! Either way the request must be a question anyone but the user may
//! answer (see `inputs`): approvals, plan approvals, and MCP servers'
//! elicitations wait for a person whoever asks.
use rho_protocol::{
RpcError, SessionAnswerQuestionByAgentParams, SessionAnswerQuestionByAgentResponse,
SessionAnswerQuestionParams, SessionAnswerQuestionResponse,
};
use super::{
inputs::{self, AgentAnswerer},
reopen, Sessions,
};
use crate::app::daemon_host::jobs::AnswerRefused;
impl Sessions {
/// Hands `by`'s answer to one of a session's open questions, when `by`
/// started the session and may act on sessions. The first answer wins,
/// whoever sends it: a later one finds the request gone.
pub(in crate::app::daemon_host) async fn answer_question(
&self,
params: SessionAnswerQuestionParams,
) -> Result<SessionAnswerQuestionResponse, RpcError> {
let SessionAnswerQuestionParams {
session,
request,
answer,
by,
} = params;
let Some(asking) = self.loaded_session(&session).await? else {
return Err(inputs::not_open(request));
};
let answering = reopen::parse_session(&by)?;
self.jobs
.may_answer(&asking.job, &answering)
.map_err(|refused| {
inputs::invalid(match refused {
AnswerRefused::NotStarter => format!(
"session {by} did not start session {session}; only the session that started it may answer its questions"
),
AnswerRefused::ReadOnly => format!(
"session {by} may not act on sessions; answering a question needs write access"
),
AnswerRefused::NoGrant => format!(
"session {by} runs under no grant the daemon knows, since the session that ran it is gone; answering a question needs write access"
),
})
})?;
asking.change_inputs(|inputs| {
inputs.answer_question(request, answer, AgentAnswerer::StartingSession)
})?;
Ok(SessionAnswerQuestionResponse {})
}
/// Hands an outside agent's answer to one of a session's open
/// questions. The first answer wins, whoever sends it: a later one
/// finds the request gone.
pub(in crate::app::daemon_host) async fn answer_question_by_agent(
&self,
params: SessionAnswerQuestionByAgentParams,
) -> Result<SessionAnswerQuestionByAgentResponse, RpcError> {
let SessionAnswerQuestionByAgentParams {
session,
request,
answer,
..
} = params;
let Some(asking) = self.loaded_session(&session).await? else {
return Err(inputs::not_open(request));
};
asking.change_inputs(|inputs| {
inputs.answer_question(request, answer, AgentAnswerer::Outside)
})?;
Ok(SessionAnswerQuestionByAgentResponse::default())
}
}