use pretty_assertions::assert_eq;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use super::{generate_title, TitleRequest};
use crate::{config::Config, session_environment::SessionEnvironment};
enum Reply {
ReadFile(&'static str),
Text(&'static str),
}
async fn serve(listener: tokio::net::TcpListener, replies: Vec<Reply>) -> Vec<String> {
let mut requests = Vec::new();
for reply in replies {
let (mut stream, _) = listener.accept().await.unwrap();
let mut request = Vec::new();
let mut buffer = [0; 16_384];
loop {
let read = stream.read(&mut buffer).await.unwrap();
if read == 0 {
break;
}
request.extend_from_slice(&buffer[..read]);
let Some(header_end) = request.windows(4).position(|window| window == b"\r\n\r\n")
else {
continue;
};
let headers = String::from_utf8_lossy(&request[..header_end]).to_ascii_lowercase();
let length = headers
.lines()
.find_map(|line| line.strip_prefix("content-length:"))
.map_or(0, |value| value.trim().parse::<usize>().unwrap());
if request.len() >= header_end + 4 + length {
break;
}
}
let chunk = |delta: serde_json::Value, finish: Option<&str>| {
let chunk = serde_json::json!({
"id": "title",
"object": "chat.completion.chunk",
"created": 0,
"model": "local-model",
"choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
});
format!("data: {chunk}\n\n")
};
let (delta, finish) = match reply {
Reply::ReadFile(path) => (
serde_json::json!({"role": "assistant", "tool_calls": [{
"index": 0,
"id": "call_1",
"type": "function",
"function": {
"name": "read_file",
"arguments": serde_json::json!({"path": path}).to_string(),
},
}]}),
"tool_calls",
),
Reply::Text(text) => (
serde_json::json!({"role": "assistant", "content": text}),
"stop",
),
};
let body = [
chunk(delta, None),
chunk(serde_json::json!({}), Some(finish)),
"data: [DONE]\n\n".to_owned(),
]
.concat();
let reply = format!(
"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\nconnection: close\r\n\r\n{body}"
);
stream.write_all(reply.as_bytes()).await.unwrap();
requests.push(String::from_utf8_lossy(&request).into_owned());
}
requests
}
struct Fixture {
workspace: tempfile::TempDir,
config: Config,
server: tokio::task::JoinHandle<Vec<String>>,
}
const PLAN: &str = "Migrate the billing tables to the ledger schema";
const TITLE: &str = "Migrate billing tables to ledger";
impl Fixture {
async fn new() -> Self {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let mut config = Config {
provider: "titlehost".into(),
model: "local-model".into(),
auth: rho_providers::provider::KEYLESS_AUTH.into(),
..Config::default()
};
config
.providers
.set_endpoint(
"titlehost",
&format!("http://{}/v1", listener.local_addr().unwrap()),
)
.unwrap();
let workspace = tempfile::tempdir().unwrap();
std::fs::write(workspace.path().join("plan.md"), PLAN).unwrap();
let server = tokio::spawn(serve(
listener,
vec![Reply::ReadFile("plan.md"), Reply::Text(TITLE)],
));
Self {
workspace,
config,
server,
}
}
async fn title(self, environment: SessionEnvironment) -> (String, Vec<String>) {
let title = generate_title(TitleRequest {
config: self.config,
config_path: self.workspace.path().join("config.toml"),
cwd: self.workspace.path().to_path_buf(),
added_dirs: Default::default(),
environment,
session: rho_sdk::SessionId::new(),
input: crate::title::session_input(&[rho_sdk::model::ContentBlock::Text(
"implement plan.md".into(),
)]),
})
.await
.unwrap();
(title, self.server.await.unwrap())
}
}
#[tokio::test]
async fn the_title_agent_reads_the_workspace_on_the_configured_model() {
let (title, requests) = Fixture::new().await.title(Default::default()).await;
let (head, body) = requests[0].split_once("\r\n\r\n").unwrap();
let body: serde_json::Value = serde_json::from_str(body).unwrap();
let mut tools: Vec<_> = body["tools"]
.as_array()
.unwrap()
.iter()
.map(|tool| tool["function"]["name"].as_str().unwrap())
.collect();
tools.sort_unstable();
assert_eq!(
(
title.as_str(),
requests.len(),
head.lines().next().unwrap(),
tools,
requests[1].contains(PLAN),
),
(
TITLE,
2,
"POST /v1/chat/completions HTTP/1.1",
vec![
"codemode",
"glob",
"grep",
"list_dir",
"read_file",
"tool_search"
],
true,
)
);
}
#[cfg(unix)]
#[tokio::test]
async fn a_hook_that_denies_a_read_stops_the_title_agents_read() {
use crate::workspace::{ProjectTrust, ProjectTrusts};
let fixture = Fixture::new().await;
let hooks = fixture.workspace.path().join(".rho");
std::fs::create_dir(&hooks).unwrap();
std::fs::write(
hooks.join("hooks.toml"),
r#"
version = 1
[[hook]]
id = "deny-reads"
on = "before_tool_use"
tools = ["read_file"]
command = ["/bin/sh", "-c", "echo '{\"version\":1,\"decision\":\"deny\",\"reason\":\"guarded\"}'"]
timeout = "10s"
"#,
)
.unwrap();
let (title, requests) = fixture
.title(SessionEnvironment::for_reading(ProjectTrusts {
hooks: ProjectTrust::Trusted,
..ProjectTrusts::UNTRUSTED
}))
.await;
assert_eq!(
(title.as_str(), requests.len(), requests[1].contains(PLAN)),
(TITLE, 2, false)
);
}