Skip to content

Commit cf250f0

Browse files
committed
test(api): launch CLI scenarios through a stable fixture
fixes #509 fixes #510 Linux CI failed before scenario execution with ETXTBSY on a newly written script. Read scenarios through a committed launcher while preserving PID ownership, argv and default test concurrency. Measure the unchanged grace ceiling after startup, and record the mute fixture PID before its stdin wait. All eight local Rust gates and 52 provider tests passed. A grace-reset mutation still fails the one-second reporting-phase ceiling; the source was restored byte-for-byte.
1 parent 0620dc2 commit cf250f0

4 files changed

Lines changed: 45 additions & 26 deletions

File tree

‎src-rust/crates/api/src/providers/claude_cli.rs‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,22 @@ impl LlmProvider for ClaudeCliProvider {
12401240
mod tests {
12411241
use super::*;
12421242

1243+
/// Keep newly written scenario files out of execve's ETXTBSY checks.
1244+
#[cfg(unix)]
1245+
pub(super) fn fixture_binary(dir: &std::path::Path, script: &str) -> PathBuf {
1246+
std::fs::write(dir.join("claude"), script).unwrap();
1247+
let binary = dir.join("launcher");
1248+
std::os::unix::fs::symlink(
1249+
concat!(
1250+
env!("CARGO_MANIFEST_DIR"),
1251+
"/tests/fixtures/claude_cli_fixture.sh"
1252+
),
1253+
&binary,
1254+
)
1255+
.unwrap();
1256+
binary
1257+
}
1258+
12431259
pub(super) fn request_with(messages: Vec<Message>) -> ProviderRequest {
12441260
ProviderRequest {
12451261
model: "claude-opus-4-8".to_string(),
@@ -1280,19 +1296,16 @@ mod tests {
12801296
/// seconds the way a long tool call does, then finishes the turn.
12811297
#[cfg(unix)]
12821298
fn quiet_cli(quiet: &str) -> (std::path::PathBuf, std::path::PathBuf) {
1283-
use std::os::unix::fs::PermissionsExt;
12841299
let dir = std::env::temp_dir().join(format!("quiet-claude-{}", uuid::Uuid::new_v4()));
12851300
std::fs::create_dir(&dir).unwrap();
1286-
let binary = dir.join("claude");
12871301
let script = format!(
12881302
"#!/bin/sh\ncat >/dev/null\n\
12891303
echo '{{\"type\":\"system\",\"subtype\":\"init\",\"model\":\"fixture\"}}'\n\
12901304
sleep {quiet}\n\
12911305
echo '{{\"type\":\"assistant\",\"message\":{{\"content\":[{{\"type\":\"text\",\"text\":\"after the wait\"}}]}}}}'\n\
12921306
echo '{{\"type\":\"result\",\"is_error\":false,\"usage\":{{}}}}'\n"
12931307
);
1294-
std::fs::write(&binary, script).unwrap();
1295-
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o700)).unwrap();
1308+
let binary = fixture_binary(&dir, &script);
12961309
(dir, binary)
12971310
}
12981311

@@ -1390,16 +1403,13 @@ mod tests {
13901403
std::time::Duration,
13911404
) {
13921405
use futures::StreamExt;
1393-
use std::os::unix::fs::PermissionsExt;
13941406
let dir = std::env::temp_dir().join(format!("silent-claude-{}", uuid::Uuid::new_v4()));
13951407
std::fs::create_dir(&dir).unwrap();
1396-
let binary = dir.join("claude");
13971408
let script = format!(
13981409
"#!/bin/sh\ncat >/dev/null\n\
13991410
echo '{{\"type\":\"system\",\"subtype\":\"init\",\"model\":\"fixture\"}}'\n{body}"
14001411
);
1401-
std::fs::write(&binary, script).unwrap();
1402-
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o700)).unwrap();
1412+
let binary = fixture_binary(&dir, &script);
14031413
let provider = ClaudeCliProvider {
14041414
id: ProviderId::new(ProviderId::ANTHROPIC),
14051415
binary: Some(binary),
@@ -1489,14 +1499,11 @@ mod tests {
14891499
#[cfg(unix)]
14901500
#[tokio::test]
14911501
async fn a_cli_silent_from_the_start_fails_the_turn_at_the_limit() {
1492-
use std::os::unix::fs::PermissionsExt;
14931502
let dir = std::env::temp_dir().join(format!("mute-claude-{}", uuid::Uuid::new_v4()));
14941503
std::fs::create_dir(&dir).unwrap();
1495-
let binary = dir.join("claude");
14961504
let script =
1497-
"#!/bin/sh\ncat >/dev/null\necho $$ > \"$(dirname \"$0\")/pid\"\nexec sleep 60\n";
1498-
std::fs::write(&binary, script).unwrap();
1499-
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o700)).unwrap();
1505+
"#!/bin/sh\necho $$ > \"$(dirname \"$0\")/pid\"\ncat >/dev/null\nexec sleep 60\n";
1506+
let binary = fixture_binary(&dir, script);
15001507
let provider = ClaudeCliProvider {
15011508
id: ProviderId::new(ProviderId::ANTHROPIC),
15021509
binary: Some(binary),
@@ -1511,6 +1518,8 @@ mod tests {
15111518
Err(error) => error,
15121519
};
15131520
let took = started.elapsed();
1521+
let error_result = Err(error);
1522+
assert!(is_quiet_limit_error(&error_result), "{error_result:?}");
15141523
let pid = std::fs::read_to_string(dir.join("pid")).unwrap();
15151524

15161525
assert!(
@@ -1521,7 +1530,6 @@ mod tests {
15211530
took < std::time::Duration::from_secs(30),
15221531
"failed after {took:?}"
15231532
);
1524-
assert!(is_quiet_limit_error(&Err(error)));
15251533
let running = still_running_after_a_wait(pid.trim()).await;
15261534
let _ = std::fs::remove_dir_all(dir);
15271535
assert!(

‎src-rust/crates/api/src/providers/claude_cli/background_work_tests.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// script that prints the lines the real CLI prints, in the order it prints
44
// them (recorded from Claude Code 2.1.326).
55

6-
use super::tests::request_with;
6+
use super::tests::{fixture_binary, request_with};
77
use super::*;
88
use futures::StreamExt;
99
use std::time::{Duration, Instant};
@@ -25,13 +25,10 @@ const LAST_RESULT: &str =
2525
/// the stream the way the query loop does, up to its `MessageStop`, and
2626
/// returns the events with how long they took.
2727
async fn run(lines: &[&str], heartbeat: Duration) -> (Vec<StreamEvent>, Duration) {
28-
use std::os::unix::fs::PermissionsExt;
2928
let dir = std::env::temp_dir().join(format!("background-claude-{}", uuid::Uuid::new_v4()));
3029
std::fs::create_dir(&dir).unwrap();
31-
let binary = dir.join("claude");
3230
let script = format!("#!/bin/sh\ncat >/dev/null\n{}\n", lines.join("\n"));
33-
std::fs::write(&binary, script).unwrap();
34-
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o700)).unwrap();
31+
let binary = fixture_binary(&dir, &script);
3532
let provider = ClaudeCliProvider {
3633
binary: Some(binary),
3734
heartbeat,

‎src-rust/crates/api/src/providers/claude_cli/integration_tests.rs‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Adversarial combined-background/silence lifecycle regressions.
2-
use super::tests::request_with;
2+
use super::tests::{fixture_binary, request_with};
33
use super::*;
44
use futures::StreamExt;
55
use std::time::{Duration, Instant};
@@ -10,12 +10,9 @@ struct Fixture {
1010
}
1111
impl Fixture {
1212
fn new(body: &str, heartbeat: Duration, limit: Option<Duration>) -> Self {
13-
use std::os::unix::fs::PermissionsExt;
1413
let dir = std::env::temp_dir().join(format!("claude-lifecycle-{}", uuid::Uuid::new_v4()));
1514
std::fs::create_dir(&dir).unwrap();
16-
let binary = dir.join("claude");
17-
std::fs::write(&binary, format!("#!/bin/sh\ncat >/dev/null\n{body}\n")).unwrap();
18-
std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(0o700)).unwrap();
15+
let binary = fixture_binary(&dir, &format!("#!/bin/sh\ncat >/dev/null\n{body}\n"));
1916
let provider = ClaudeCliProvider {
2017
binary: Some(binary),
2118
heartbeat,
@@ -171,10 +168,24 @@ async fn an_error_result_publishes_its_usage_before_the_error() {
171168

172169
#[tokio::test]
173170
async fn unrelated_metadata_does_not_extend_the_exit_grace() {
174-
let body = format!("{INIT}\n{TASK}\n{RESULT}\n{NO_TASK}\nfor _ in $(seq 40); do sleep 0.05; {NO_TASK}; done\nexec sleep 2");
171+
let body = format!("sleep 1.2\n{INIT}\n{TASK}\n{RESULT}\n{NO_TASK}\nfor _ in $(seq 40); do sleep 0.05; {NO_TASK}; done\nexec sleep 2");
175172
let f = Fixture::new(&body, Duration::from_millis(100), None);
173+
let request = request_with(vec![Message::user("fixture prompt")]);
174+
let mut stream = f.provider.create_message_stream(request).await.unwrap();
175+
// The grace belongs to the reporting phase, after the startup handshake.
176176
let start = Instant::now();
177-
let events = f.collect().await;
177+
let mut events = Vec::new();
178+
while let Some(event) = stream.next().await {
179+
let terminal = matches!(
180+
event,
181+
Ok(StreamEvent::MessageStop | StreamEvent::Error { .. }) | Err(_)
182+
);
183+
events.push(event);
184+
if terminal {
185+
break;
186+
}
187+
}
188+
drop(stream);
178189
assert!(
179190
matches!(events.last(), Some(Ok(StreamEvent::MessageStop))),
180191
"{events:?}"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
# Execute a stable launcher while reading each newly written scenario as data.
3+
exec /bin/sh "${0%/*}/claude" "$@"

0 commit comments

Comments
 (0)