use std::sync::Arc;
use pretty_assertions::assert_eq;
use rho_sdk::{
hooks::{
testing::{recording_dispatcher, RecordingObserver},
HookInputKind, HookPayload,
},
ApprovalDecision, ApprovalRequest, CancellationToken, CapabilityRequest, CapabilitySource,
HostChoice, HostInputId, HostInputRequest, HostQuestion, PathScope, PendingApproval, RunEvent,
SelectionMode, SessionId, ToolCallId,
};
use tokio::{
sync::{
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
oneshot,
},
task::JoinHandle,
};
use super::{pump, ApprovalSource, EventSource, PumpSources};
use crate::{
app::session_runtime::{
events::{
cancelled_approval, ApprovalId, PlanApprovalId, SecretAskId, SessionEvent, TurnControl,
},
turn::TurnIo,
QuestionSupport,
},
permission::PermissionMode,
tools::{
mcp::secret_tool::{PendingSecret, SecretOutcome, SecretRequest},
plan_exit::{PlanExitDecision, PlanPermissionRequest},
},
};
#[derive(Debug, PartialEq)]
enum Seen {
RunStarted,
Run,
BoundaryApplied,
ApprovalRequested(ApprovalId),
ApprovalWithdrawn(ApprovalId),
PlanRequested(PlanApprovalId),
PlanWithdrawn(PlanApprovalId),
SecretRequested(SecretAskId),
SecretWithdrawn(SecretAskId),
QuestionAsked(HostInputId),
QuestionWithdrawn(HostInputId),
Notice,
RunClosed,
RunFinished,
}
fn seen(event: SessionEvent) -> Seen {
match event {
SessionEvent::MessageInserted(_) | SessionEvent::TitleRequested(_) => {
unreachable!("the turn inserts the message and asks for a title, not the pump")
}
SessionEvent::RunStarted { .. } => Seen::RunStarted,
SessionEvent::Run(_) => Seen::Run,
SessionEvent::BoundaryApplied { .. } => Seen::BoundaryApplied,
SessionEvent::ApprovalRequested { id, .. } => Seen::ApprovalRequested(id),
SessionEvent::ApprovalWithdrawn { id } => Seen::ApprovalWithdrawn(id),
SessionEvent::PlanApprovalRequested { id, .. } => Seen::PlanRequested(id),
SessionEvent::PlanApprovalWithdrawn { id } => Seen::PlanWithdrawn(id),
SessionEvent::SecretRequested { id, .. } => Seen::SecretRequested(id),
SessionEvent::SecretWithdrawn { id } => Seen::SecretWithdrawn(id),
SessionEvent::QuestionAsked { id, .. } => Seen::QuestionAsked(id),
SessionEvent::QuestionWithdrawn { id } => Seen::QuestionWithdrawn(id),
SessionEvent::Notice { .. } => Seen::Notice,
SessionEvent::RunClosed => Seen::RunClosed,
SessionEvent::RunFinished { .. } => Seen::RunFinished,
}
}
struct Harness {
events: UnboundedReceiver<SessionEvent>,
control: UnboundedSender<TurnControl>,
run_events: Option<UnboundedSender<RunEvent>>,
approvals: UnboundedSender<PendingApproval>,
plans: UnboundedSender<PlanPermissionRequest>,
secrets: UnboundedSender<PendingSecret>,
cancel: CancellationToken,
hooks: Arc<RecordingObserver>,
pump: JoinHandle<anyhow::Result<()>>,
}
impl Harness {
fn start(questions: QuestionSupport) -> Self {
let (run_events, mut run_rx) = unbounded_channel();
let (approvals, mut approval_rx) = unbounded_channel();
let (plans, mut plan_rx) = unbounded_channel();
let (secrets, mut secret_rx) = unbounded_channel();
let (events_tx, events) = unbounded_channel();
let (control, control_rx) = unbounded_channel();
let cancel = CancellationToken::new();
let pump_cancel = cancel.clone();
let (dispatcher, hooks) = recording_dispatcher();
let pump = tokio::spawn(async move {
let session = SessionId::new();
let diagnostics = crate::diagnostics::test_diagnostics("test", "test");
let mut io = TurnIo {
events: events_tx,
control: control_rx,
};
pump(
PumpSources {
cancel: pump_cancel,
questions,
events: EventSource::Channel(&mut run_rx),
approvals: ApprovalSource::Channel(&mut approval_rx),
plan_approvals: Some(&mut plan_rx),
secret_asks: Some(&mut secret_rx),
displays: None,
hooks: dispatcher,
session: &session,
diagnostics: &diagnostics,
},
&mut io,
)
.await
});
Self {
events,
control,
run_events: Some(run_events),
approvals,
plans,
secrets,
cancel,
hooks,
pump,
}
}
fn asked(&self) -> Vec<(HookInputKind, String)> {
assert!(
self.pump.is_finished(),
"read the hooks after the pump ends"
);
self.hooks
.envelopes()
.iter()
.map(|envelope| match envelope.payload() {
HookPayload::InputRequested(payload) => (payload.kind, payload.summary.clone()),
other => panic!("unexpected hook payload: {other:?}"),
})
.collect()
}
async fn next(&mut self) -> Seen {
seen(self.events.recv().await.expect("the pump reports an event"))
}
fn emit(&self, event: RunEvent) {
let events = self.run_events.as_ref().expect("the run is still going");
events.send(event).expect("the pump reads run events");
}
fn end_run(&mut self) {
self.run_events = None;
}
fn answer(&self, control: TurnControl) {
self.control.send(control).expect("the pump reads answers");
}
}
fn approval() -> (PendingApproval, oneshot::Receiver<ApprovalDecision>) {
PendingApproval::new(ApprovalRequest::new(
CapabilityRequest::write_path(
"/workspace/file.rs",
PathScope::PrimaryWorkspace,
CapabilitySource::built_in_tool("write"),
),
"edit file",
))
}
fn plan() -> (PlanPermissionRequest, oneshot::Receiver<PlanExitDecision>) {
let (reply, answer) = oneshot::channel();
(
PlanPermissionRequest {
call_id: ToolCallId::new(),
plan: "1. edit".into(),
targets: vec![PermissionMode::AllowEdits],
reply,
},
answer,
)
}
fn secret() -> (PendingSecret, oneshot::Receiver<SecretOutcome>) {
let (reply, outcome) = oneshot::channel();
(
PendingSecret {
request: SecretRequest {
call_id: ToolCallId::new(),
server: "probe".into(),
name: "TOKEN".into(),
reason: None,
destination: rho_protocol::SecretDestination::Process {
command: "python3".into(),
args: vec!["probe.py".into()],
},
},
reply,
},
outcome,
)
}
fn question() -> HostInputRequest {
let question = HostQuestion::new(
"go",
"Continue?",
vec![HostChoice::new("yes", "Yes")],
SelectionMode::One,
)
.unwrap();
HostInputRequest::questionnaire("Confirm", vec![question]).unwrap()
}
#[tokio::test]
async fn cancel_settles_every_request_waiting_on_the_host() {
let mut harness = Harness::start(QuestionSupport::Available);
let (waiting, waiting_decision) = approval();
harness.approvals.send(waiting).unwrap();
assert_eq!(harness.next().await, Seen::ApprovalRequested(ApprovalId(0)));
let (plan, plan_answer) = plan();
harness.plans.send(plan).unwrap();
assert_eq!(harness.next().await, Seen::PlanRequested(PlanApprovalId(0)));
let (asked, secret_outcome) = secret();
harness.secrets.send(asked).unwrap();
assert_eq!(harness.next().await, Seen::SecretRequested(SecretAskId(0)));
let request = question();
let question_id = request.id().clone();
harness.emit(RunEvent::HostInputRequested { request });
assert_eq!(
[harness.next().await, harness.next().await],
[Seen::Run, Seen::QuestionAsked(question_id.clone())]
);
harness.cancel.cancel();
assert_eq!(
[
harness.next().await,
harness.next().await,
harness.next().await,
harness.next().await
],
[
Seen::ApprovalWithdrawn(ApprovalId(0)),
Seen::PlanWithdrawn(PlanApprovalId(0)),
Seen::SecretWithdrawn(SecretAskId(0)),
Seen::QuestionWithdrawn(question_id),
]
);
assert_eq!(waiting_decision.await.unwrap(), cancelled_approval());
assert!(
plan_answer.await.is_err(),
"a cancelled plan approval must drop its reply"
);
assert!(
secret_outcome.await.is_err(),
"a cancelled secret ask must drop its reply"
);
let (late, late_decision) = approval();
harness.approvals.send(late).unwrap();
assert_eq!(late_decision.await.unwrap(), cancelled_approval());
harness.end_run();
assert_eq!(harness.next().await, Seen::RunClosed);
harness.pump.await.unwrap().unwrap();
assert!(
harness.events.recv().await.is_none(),
"the late approval must not reach the host"
);
}
#[tokio::test]
async fn answers_reach_the_request_they_name() {
let mut harness = Harness::start(QuestionSupport::Available);
let (first, first_decision) = approval();
let (second, second_decision) = approval();
harness.approvals.send(first).unwrap();
harness.approvals.send(second).unwrap();
let (kept, kept_answer) = plan();
let (abandoned, abandoned_answer) = plan();
harness.plans.send(kept).unwrap();
harness.plans.send(abandoned).unwrap();
let (stored, stored_outcome) = secret();
let (left, left_outcome) = secret();
harness.secrets.send(stored).unwrap();
harness.secrets.send(left).unwrap();
let mut requested = Vec::new();
for _ in 0..6 {
requested.push(harness.next().await);
}
assert_eq!(
requested,
[
Seen::ApprovalRequested(ApprovalId(0)),
Seen::ApprovalRequested(ApprovalId(1)),
Seen::PlanRequested(PlanApprovalId(0)),
Seen::PlanRequested(PlanApprovalId(1)),
Seen::SecretRequested(SecretAskId(0)),
Seen::SecretRequested(SecretAskId(1)),
]
);
harness.answer(TurnControl::RespondApproval {
id: ApprovalId(7),
decision: ApprovalDecision::AllowOnce,
});
harness.answer(TurnControl::RespondApproval {
id: ApprovalId(1),
decision: ApprovalDecision::AllowForSession,
});
harness.answer(TurnControl::RespondApproval {
id: ApprovalId(0),
decision: ApprovalDecision::Deny {
reason: "no".into(),
},
});
harness.answer(TurnControl::AnswerPlan {
id: PlanApprovalId(0),
answer: PlanExitDecision::approve(PermissionMode::AllowEdits),
});
harness.answer(TurnControl::AnswerSecret {
id: SecretAskId(0),
outcome: SecretOutcome::Stored,
});
drop(abandoned_answer);
assert_eq!(harness.next().await, Seen::PlanWithdrawn(PlanApprovalId(1)));
drop(left_outcome);
assert_eq!(harness.next().await, Seen::SecretWithdrawn(SecretAskId(1)));
assert_eq!(
(
first_decision.await.unwrap(),
second_decision.await.unwrap(),
kept_answer.await.unwrap(),
stored_outcome.await.unwrap(),
),
(
ApprovalDecision::Deny {
reason: "no".into()
},
ApprovalDecision::AllowForSession,
PlanExitDecision::approve(PermissionMode::AllowEdits),
SecretOutcome::Stored,
)
);
harness.answer(TurnControl::HostFailed {
message: "connection closed".into(),
});
let error = harness.pump.await.unwrap().unwrap_err();
assert_eq!(error.to_string(), "connection closed");
}
#[tokio::test]
async fn hooks_hear_of_each_request_put_to_the_host() {
let mut harness = Harness::start(QuestionSupport::Available);
let (waiting, _decision) = approval();
harness.approvals.send(waiting).unwrap();
assert_eq!(harness.next().await, Seen::ApprovalRequested(ApprovalId(0)));
let (plan, _answer) = plan();
harness.plans.send(plan).unwrap();
assert_eq!(harness.next().await, Seen::PlanRequested(PlanApprovalId(0)));
let request = question();
let question_id = request.id().clone();
harness.emit(RunEvent::HostInputRequested { request });
assert_eq!(
[harness.next().await, harness.next().await],
[Seen::Run, Seen::QuestionAsked(question_id)]
);
harness.cancel.cancel();
let (late, late_decision) = approval();
harness.approvals.send(late).unwrap();
assert_eq!(late_decision.await.unwrap(), cancelled_approval());
harness.emit(RunEvent::HostInputRequested {
request: question(),
});
harness.end_run();
(&mut harness.pump).await.unwrap().unwrap();
let mut unasked = Harness::start(QuestionSupport::Unavailable);
unasked.emit(RunEvent::HostInputRequested {
request: question(),
});
unasked.end_run();
(&mut unasked.pump).await.unwrap().unwrap();
assert_eq!(
(harness.asked(), unasked.asked()),
(
vec![
(
HookInputKind::Approval,
"write /workspace/file.rs (edit file)".into()
),
(HookInputKind::PlanApproval, "1. edit".into()),
(HookInputKind::Question, "Confirm".into()),
],
Vec::new(),
)
);
}