use std::sync::Arc;
use std::time::Duration;
use ag_contracts::{
AgentChannel as _, AgentError, AgentRequestKind, TurnEvent, TurnRequest, TurnResult,
};
use ag_protocol::{
AgentResponse, QuestionItem, TurnPrompt, TurnPromptAttachment, TurnPromptTextSource,
};
use ag_session::AgentKind;
use tempfile::tempdir;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use super::support::make_turn_request;
use crate::agent::MockAgentBackend;
use crate::channel::cli::{CliAgentChannel, append_follow_up_response};
fn steerable_claude_command() -> std::process::Command {
let mut command = std::process::Command::new("sh");
command.arg("-c").arg(
"IFS= read -r initial; IFS= read -r steer; case \"$steer\" in *'focus on the parser'*) \
answer='steered' ;; *) answer='not steered' ;; esac; printf \
'{\"type\":\"result\",\"result\":\"{\\\\\"answer\\\\\":\\\\\"%s\\\\\",\\\\\"questions\\\\\
\":[]}\"}\\n' \"$answer\"; cat >/dev/null",
);
command
}
fn late_steered_claude_command() -> std::process::Command {
let mut command = std::process::Command::new("sh");
command.arg("-c").arg(
"IFS= read -r initial; IFS= read -r steer; printf '%s\\n' \
'{\"type\":\"result\",\"result\":\"{\\\"answer\\\":\\\"first\\\",\\\"questions\\\":[]}\",\
\"usage\":{\"input_tokens\":11,\"output_tokens\":7}}' \
'{\"type\":\"result\",\"result\":\"{\\\"answer\\\":\\\"second\\\",\\\"questions\\\":[]}\"\
,\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}'; cat >/dev/null",
);
command
}
fn malformed_follow_up_claude_command() -> std::process::Command {
let mut command = std::process::Command::new("sh");
command.arg("-c").arg(
"IFS= read -r initial; IFS= read -r steer; printf '%s\\n' \
'{\"type\":\"result\",\"result\":\"{\\\"answer\\\":\\\"first\\\",\\\"questions\\\":[]}\"\
}' '{\"type\":\"result\",\"result\":\"plain follow-up\"}'; cat >/dev/null",
);
command
}
fn failed_repair_claude_command() -> std::process::Command {
let mut command = std::process::Command::new("sh");
command
.arg("-c")
.arg("cat >/dev/null; printf '%s\\n' '{\"type\":\"result\",\"result\":\"still plain\"}'");
command
}
fn utility_claude_command() -> std::process::Command {
let mut command = std::process::Command::new("sh");
command
.arg("-c")
.arg("cat >/dev/null; printf '%s' '{\"answer\":\"done\"}'");
command
}
fn claude_channel(command: fn() -> std::process::Command) -> CliAgentChannel {
let mut backend = MockAgentBackend::new();
backend
.expect_build_command()
.returning(move |_| Ok(command()));
CliAgentChannel::with_backend(Arc::new(backend), AgentKind::Claude)
}
async fn start_turn(
channel: &CliAgentChannel,
request: TurnRequest,
) -> JoinHandle<Result<TurnResult, AgentError>> {
let (events_tx, mut events_rx) = mpsc::unbounded_channel();
let turn = tokio::spawn(channel.run_turn("session".into(), request, events_tx));
tokio::time::timeout(Duration::from_secs(10), async {
while !matches!(
events_rx.recv().await,
Some(TurnEvent::PidUpdate(Some(_))) | None
) {}
})
.await
.expect("turn process starts");
turn
}
async fn finish_turn(turn: JoinHandle<Result<TurnResult, AgentError>>) -> TurnResult {
tokio::time::timeout(Duration::from_secs(10), turn)
.await
.expect("stdin closes once the turn reports its result")
.expect("turn task")
.expect("turn")
}
#[tokio::test]
async fn claude_session_turn_reads_steering_prompt_and_closes_stdin_after_result() {
let directory = tempdir().expect("temporary folder");
let channel = claude_channel(steerable_claude_command);
let turn = start_turn(&channel, make_turn_request(directory.path().to_path_buf())).await;
let steered = channel
.steer_turn("session".into(), "focus on the parser".into())
.await;
let result = finish_turn(turn).await;
let after_turn = channel
.steer_turn("session".into(), "too late".into())
.await;
assert!(steered.is_ok());
assert_eq!(result.assistant_message.to_display_text(), "steered");
assert!(matches!(after_turn, Err(AgentError::Runtime(_))));
}
#[tokio::test]
async fn claude_session_turn_keeps_every_answer_when_a_steer_gets_a_follow_up_turn() {
let directory = tempdir().expect("temporary folder");
let channel = claude_channel(late_steered_claude_command);
let turn = start_turn(&channel, make_turn_request(directory.path().to_path_buf())).await;
let steered = channel
.steer_turn("session".into(), "focus on the parser".into())
.await;
let result = finish_turn(turn).await;
assert!(steered.is_ok());
assert_eq!(
result.assistant_message.to_display_text(),
"first\n\nsecond"
);
assert_eq!(result.usage.input_tokens(), 16);
assert_eq!(result.usage.output_tokens(), 10);
}
#[tokio::test]
async fn claude_session_turn_keeps_primary_answer_when_a_follow_up_reply_is_malformed() {
let directory = tempdir().expect("temporary folder");
let mut backend = MockAgentBackend::new();
let mut sequence = mockall::Sequence::new();
backend
.expect_build_command()
.times(1)
.in_sequence(&mut sequence)
.returning(|_| Ok(malformed_follow_up_claude_command()));
backend
.expect_build_command()
.times(1)
.in_sequence(&mut sequence)
.returning(|_| Ok(failed_repair_claude_command()));
let channel = CliAgentChannel::with_backend(Arc::new(backend), AgentKind::Claude);
let turn = start_turn(&channel, make_turn_request(directory.path().to_path_buf())).await;
let steered = channel
.steer_turn("session".into(), "focus on the parser".into())
.await;
let result = finish_turn(turn).await;
assert!(steered.is_ok());
assert_eq!(
result.assistant_message.to_display_text(),
"first\n\nplain follow-up"
);
let usage = &result.usage.models[0];
assert_eq!(
(usage.requests, usage.unreported_requests),
(2, 2),
"the follow-up repair process is charged to the turn"
);
}
#[test]
fn follow_up_response_joins_only_nonempty_answers_and_keeps_questions() {
let cases = [
("", "second", vec![], "second"),
(
"first",
"",
vec![QuestionItem::new("Which parser?")],
"first",
),
("first", " \n", vec![], "first"),
];
for (primary_answer, follow_up_answer, follow_up_questions, expected_answer) in cases {
let mut response = AgentResponse::plain(primary_answer);
let mut follow_up = AgentResponse::plain(follow_up_answer);
follow_up.questions.clone_from(&follow_up_questions);
append_follow_up_response(&mut response, follow_up);
assert_eq!(response.answer, expected_answer);
assert_eq!(response.questions, follow_up_questions);
}
}
#[tokio::test]
async fn claude_utility_turn_rejects_steering() {
let directory = tempdir().expect("temporary folder");
let channel = claude_channel(utility_claude_command);
let mut request = make_turn_request(directory.path().to_path_buf());
request.request_kind = AgentRequestKind::UtilityPrompt;
let turn = start_turn(&channel, request).await;
let steered = channel
.steer_turn("session".into(), "focus on the parser".into())
.await;
let result = finish_turn(turn).await;
assert!(matches!(steered, Err(AgentError::Runtime(_))));
assert_eq!(result.assistant_message.to_display_text(), "done");
}
#[tokio::test]
async fn claude_session_turn_keeps_image_prompts_for_a_later_turn() {
let directory = tempdir().expect("temporary folder");
let channel = claude_channel(steerable_claude_command);
let turn = start_turn(&channel, make_turn_request(directory.path().to_path_buf())).await;
let image_prompt = TurnPrompt {
attachments: vec![TurnPromptAttachment {
placeholder: "[Image #1]".to_string(),
local_image_path: directory.path().join("image.png"),
}],
text: "Check [Image #1]".to_string(),
text_source: TurnPromptTextSource::UserPrompt,
};
let image_steered = channel.steer_turn("session".into(), image_prompt).await;
let text_steered = channel
.steer_turn("session".into(), "focus on the parser".into())
.await;
let result = finish_turn(turn).await;
assert_eq!(
image_steered
.expect_err("image prompt stays queued")
.to_string(),
"Claude cannot receive images during a turn"
);
assert!(text_steered.is_ok());
assert_eq!(result.assistant_message.to_display_text(), "steered");
}