mod support;
use std::time::Duration;
use serde_json::{Value, json};
use support::{project_with_agents, spawn_http_daemon, spawn_http_daemon_with_env};
const READINESS_POLL_ROUNDS: usize = 100;
const READINESS_POLL_INTERVAL: Duration = Duration::from_millis(150);
async fn call_rpc(
client: &reqwest::Client,
rpc_url: &str,
id: i64,
method: &str,
params: Value,
) -> Value {
client
.post(rpc_url)
.json(&json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}))
.send()
.await
.unwrap_or_else(|e| panic!("POST {method} failed: {e}"))
.json()
.await
.unwrap_or_else(|e| panic!("parse {method} response failed: {e}"))
}
async fn poll_until_probed(client: &reqwest::Client, rpc_url: &str, session_id: &str) -> Value {
for round in 0..READINESS_POLL_ROUNDS {
let resp = call_rpc(
client,
rpc_url,
1000 + round as i64,
"session.get_readiness",
json!({"session_id": session_id}),
)
.await;
assert!(resp["error"].is_null(), "get_readiness failed: {resp}");
if resp["result"]["status"] == "probed" {
return resp["result"].clone();
}
tokio::time::sleep(READINESS_POLL_INTERVAL).await;
}
panic!("gave up waiting for an index_readiness probe to land for session {session_id}");
}
#[tokio::test]
async fn late_attaching_client_can_query_cached_readiness() {
let project = project_with_agents();
let daemon = spawn_http_daemon_with_env(
project.path(),
&[(
trusty_code::task::mock_llm::MOCK_LLM_ENV,
trusty_code::task::mock_llm::MOCK_LLM_ECHO,
)],
)
.await;
let rpc_url = format!("{}/rpc", daemon.base_url);
let original_client = daemon.client();
let run_resp = call_rpc(
&original_client,
&rpc_url,
1,
"task.run",
json!({"task_description": "say hi"}),
)
.await;
assert!(run_resp["error"].is_null(), "task.run failed: {run_resp}");
let session_id = run_resp["result"]["session_id"]
.as_str()
.expect("task.run must return a session_id")
.to_string();
let probed_by_original = poll_until_probed(&original_client, &rpc_url, &session_id).await;
assert_eq!(probed_by_original["status"], "probed");
assert!(
probed_by_original["state"].is_string(),
"a probed snapshot must carry a state: {probed_by_original}"
);
let late_client = daemon.client();
let late_resp = call_rpc(
&late_client,
&rpc_url,
1,
"session.get_readiness",
json!({"session_id": session_id}),
)
.await;
assert!(
late_resp["error"].is_null(),
"late get_readiness failed: {late_resp}"
);
assert_eq!(
late_resp["result"], probed_by_original,
"a late-attaching client must see the exact same cached readiness snapshot \
the original probe recorded, not a re-probe or a drifted copy"
);
daemon.shutdown_via_sigterm_and_assert_clean_exit().await;
}
#[tokio::test]
async fn never_probed_session_returns_never_probed_over_the_wire() {
let daemon = spawn_http_daemon().await;
let client = daemon.client();
let rpc_url = format!("{}/rpc", daemon.base_url);
let create_resp = call_rpc(
&client,
&rpc_url,
1,
"session.create",
json!({"task": "never runs a task"}),
)
.await;
assert!(
create_resp["error"].is_null(),
"create failed: {create_resp}"
);
let session_id = create_resp["result"]["id"]
.as_str()
.expect("session.create must return an id")
.to_string();
let readiness_resp = call_rpc(
&client,
&rpc_url,
2,
"session.get_readiness",
json!({"session_id": session_id}),
)
.await;
assert!(
readiness_resp["error"].is_null(),
"get_readiness failed: {readiness_resp}"
);
assert_eq!(readiness_resp["result"], json!({"status": "never_probed"}));
daemon.shutdown_via_sigterm_and_assert_clean_exit().await;
}