feat(daemon): resolve ACP approvals
This commit is contained in:
@@ -142,6 +142,10 @@ impl FakeAcp {
|
||||
.next_back()
|
||||
.expect("fake ACP started")
|
||||
}
|
||||
|
||||
fn log(&self) -> String {
|
||||
std::fs::read_to_string(self.dir.path().join("agent.log")).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
async fn structured_session(h: &Harness, fake: &FakeAcp) -> Uuid {
|
||||
@@ -258,6 +262,120 @@ async fn structured_prompt_rejects_a_second_in_flight_turn_and_cancel_clears_it(
|
||||
assert_eq!(next.status(), reqwest::StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn structured_approval_is_secret_single_use_and_race_safe() {
|
||||
let h = spawn("naps-laptop").await;
|
||||
let fake = FakeAcp::new();
|
||||
let mut env = fake.env();
|
||||
env.insert("FAKE_ACP_REQUEST_PERMISSION".into(), "1".into());
|
||||
let id = structured_session_with_env(&h, env).await;
|
||||
let client = reqwest::Client::new();
|
||||
let (mut ws, _) = connect_async(ws_url(h.addr, &format!("/api/structured/{id}")))
|
||||
.await
|
||||
.unwrap();
|
||||
ws.next().await.unwrap().unwrap();
|
||||
ws.next().await.unwrap().unwrap();
|
||||
assert_eq!(
|
||||
client
|
||||
.post(url(
|
||||
h.addr,
|
||||
&format!("/api/sessions/{id}/structured/prompt")
|
||||
))
|
||||
.json(&serde_json::json!({"prompt":"needs permission"}))
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.status(),
|
||||
reqwest::StatusCode::NO_CONTENT
|
||||
);
|
||||
|
||||
let nonce = tokio::time::timeout(Duration::from_secs(2), async {
|
||||
loop {
|
||||
let event: serde_json::Value =
|
||||
serde_json::from_str(&ws.next().await.unwrap().unwrap().into_text().unwrap())
|
||||
.unwrap();
|
||||
if let Some(nonce) = event["state"]["pending_approval"]["nonce"].as_str() {
|
||||
assert_eq!(event["transcript"]["row"]["kind"], "approval");
|
||||
assert!(event["transcript"]["row"]["data"].get("nonce").is_none());
|
||||
break nonce.to_owned();
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
!fake.log().contains(&nonce),
|
||||
"daemon nonce leaked to ACP agent"
|
||||
);
|
||||
|
||||
let path = format!("/api/sessions/{id}/structured/approvals/{nonce}");
|
||||
let (a, b) = tokio::join!(
|
||||
client
|
||||
.post(url(h.addr, &path))
|
||||
.json(&serde_json::json!("allow_once"))
|
||||
.send(),
|
||||
client
|
||||
.post(url(h.addr, &path))
|
||||
.json(&serde_json::json!("deny"))
|
||||
.send(),
|
||||
);
|
||||
assert_eq!(a.unwrap().status(), reqwest::StatusCode::NO_CONTENT);
|
||||
assert_eq!(b.unwrap().status(), reqwest::StatusCode::NO_CONTENT);
|
||||
assert_eq!(
|
||||
client
|
||||
.post(url(h.addr, &path))
|
||||
.json(&serde_json::json!("deny"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.status(),
|
||||
reqwest::StatusCode::NO_CONTENT
|
||||
);
|
||||
tokio::time::timeout(Duration::from_secs(2), async {
|
||||
loop {
|
||||
if fake.log().contains("permission response") {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let log = fake.log();
|
||||
assert!(
|
||||
log.contains("\"optionId\": \"once\"") || log.contains("\"optionId\": \"deny\""),
|
||||
"{log}"
|
||||
);
|
||||
tokio::time::timeout(Duration::from_secs(1), async {
|
||||
loop {
|
||||
let event: serde_json::Value =
|
||||
serde_json::from_str(&ws.next().await.unwrap().unwrap().into_text().unwrap())
|
||||
.unwrap();
|
||||
if event["state"]["pending_approval"].is_null() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("resolved approval clears the reduced state");
|
||||
assert_eq!(
|
||||
client
|
||||
.post(url(
|
||||
h.addr,
|
||||
&format!(
|
||||
"/api/sessions/{}/structured/approvals/{nonce}",
|
||||
Uuid::new_v4()
|
||||
)
|
||||
))
|
||||
.json(&serde_json::json!("deny"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.status(),
|
||||
reqwest::StatusCode::NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn structured_prompt_failure_clears_the_turn() {
|
||||
let h = spawn("naps-laptop").await;
|
||||
|
||||
Reference in New Issue
Block a user