@@ -1458,45 +1458,83 @@ mod tests {
14581458 if message. contains( "printed nothing for" ) )
14591459 }
14601460
1461+ /// The pid a stand-in CLI wrote to `dir`, if it got that far. The
1462+ /// provider kills the CLI at its 1 s quiet limit, and a stand-in that a
1463+ /// busy host keeps off the CPU until then is killed before it writes:
1464+ /// the file then never appears, so waiting for it cannot help.
1465+ #[ cfg( unix) ]
1466+ fn recorded_pid ( dir : & std:: path:: Path ) -> Option < String > {
1467+ let pid = std:: fs:: read_to_string ( dir. join ( "pid" ) ) . ok ( ) ?;
1468+ let pid = pid. trim ( ) ;
1469+ pid. parse :: < u32 > ( ) . is_ok ( ) . then ( || pid. to_string ( ) )
1470+ }
1471+
1472+ /// Runs of a pid-recording scenario before giving up. A run whose
1473+ /// stand-in was killed before it wrote its pid shows nothing about
1474+ /// whether a killed CLI is left running, so the scenario runs again.
1475+ #[ cfg( unix) ]
1476+ const PID_ATTEMPTS : usize = 3 ;
1477+
1478+ #[ cfg( unix) ]
1479+ fn no_pid_recorded ( ) -> ! {
1480+ panic ! (
1481+ "in {PID_ATTEMPTS} runs the stand-in CLI had not written its pid when the turn \
1482+ ended at the 1 s quiet limit; the host is likely too busy to run this test"
1483+ )
1484+ }
1485+
1486+ /// Checks that the stand-in CLI in `dir` is no longer running, and
1487+ /// removes `dir`. Returns false, having checked nothing, when the
1488+ /// stand-in wrote no pid.
1489+ #[ cfg( unix) ]
1490+ async fn assert_recorded_cli_stopped ( dir : std:: path:: PathBuf ) -> bool {
1491+ let Some ( pid) = recorded_pid ( & dir) else {
1492+ let _ = std:: fs:: remove_dir_all ( dir) ;
1493+ return false ;
1494+ } ;
1495+ let running = still_running_after_a_wait ( & pid) . await ;
1496+ let _ = std:: fs:: remove_dir_all ( dir) ;
1497+ assert ! ( !running, "the hung CLI (pid {pid}) is still running" ) ;
1498+ true
1499+ }
1500+
14611501 /// The keepalive tells the query loop a silent CLI is busy for as long as
14621502 /// its process lives. A CLI that has hung is alive too, so without a
14631503 /// limit of the provider's own the turn would wait on it for ever.
14641504 #[ cfg( unix) ]
14651505 #[ tokio:: test]
14661506 async fn a_running_cli_silent_past_the_limit_ends_the_turn_with_an_error ( ) {
1467- // The CLI records its pid, then hangs: one process, alive and silent.
1468- let ( dir , items , took ) = run_with_quiet_limit (
1469- "echo $$ > \" $(dirname \" $0 \" )/pid \" \n exec sleep 60 \n " ,
1470- Some ( std :: time :: Duration :: from_secs ( 1 ) ) ,
1471- )
1472- . await ;
1473- let pid = std :: fs :: read_to_string ( dir . join ( "pid" ) ) . unwrap ( ) ;
1507+ for _ in 0 .. PID_ATTEMPTS {
1508+ // The CLI records its pid, then hangs: one process, alive and silent.
1509+ let ( dir , items , took ) = run_with_quiet_limit (
1510+ "echo $$ > \" $(dirname \" $0 \" )/pid \" \n exec sleep 60 \n " ,
1511+ Some ( std :: time :: Duration :: from_secs ( 1 ) ) ,
1512+ )
1513+ . await ;
14741514
1475- // It was kept alive up to the limit, then failed once, and the stream
1476- // ended there instead of waiting out the minute.
1477- assert ! (
1478- took >= std:: time:: Duration :: from_secs( 1 ) ,
1479- "ended after {took:?}"
1480- ) ;
1481- assert ! (
1482- took < std:: time:: Duration :: from_secs( 30 ) ,
1483- "ended after {took:?}"
1484- ) ;
1485- let ( last, before) = items. split_last ( ) . unwrap ( ) ;
1486- assert ! ( is_quiet_limit_error( last) , "last item was {last:?}" ) ;
1487- assert ! ( before. iter( ) . all( Result :: is_ok) ) ;
1488- assert ! ( before
1489- . iter( )
1490- . any( |item| matches!( item, Ok ( StreamEvent :: Ping ) ) ) ) ;
1515+ // It was kept alive up to the limit, then failed once, and the
1516+ // stream ended there instead of waiting out the minute.
1517+ assert ! (
1518+ took >= std:: time:: Duration :: from_secs( 1 ) ,
1519+ "ended after {took:?}"
1520+ ) ;
1521+ assert ! (
1522+ took < std:: time:: Duration :: from_secs( 30 ) ,
1523+ "ended after {took:?}"
1524+ ) ;
1525+ let ( last, before) = items. split_last ( ) . unwrap ( ) ;
1526+ assert ! ( is_quiet_limit_error( last) , "last item was {last:?}" ) ;
1527+ assert ! ( before. iter( ) . all( Result :: is_ok) ) ;
1528+ assert ! ( before
1529+ . iter( )
1530+ . any( |item| matches!( item, Ok ( StreamEvent :: Ping ) ) ) ) ;
14911531
1492- // Ending the stream stopped the CLI: nothing is left running.
1493- let running = still_running_after_a_wait ( pid. trim ( ) ) . await ;
1494- let _ = std:: fs:: remove_dir_all ( dir) ;
1495- assert ! (
1496- !running,
1497- "the hung CLI (pid {}) is still running" ,
1498- pid. trim( )
1499- ) ;
1532+ // Ending the stream stopped the CLI: nothing is left running.
1533+ if assert_recorded_cli_stopped ( dir) . await {
1534+ return ;
1535+ }
1536+ }
1537+ no_pid_recorded ( ) ;
15001538 }
15011539
15021540 /// A CLI can hang before it prints anything. No stream exists yet at
@@ -1505,44 +1543,42 @@ mod tests {
15051543 #[ cfg( unix) ]
15061544 #[ tokio:: test]
15071545 async fn a_cli_silent_from_the_start_fails_the_turn_at_the_limit ( ) {
1508- let dir = std:: env:: temp_dir ( ) . join ( format ! ( "mute-claude-{}" , uuid:: Uuid :: new_v4( ) ) ) ;
1509- std:: fs:: create_dir ( & dir) . unwrap ( ) ;
1510- let script =
1511- "#!/bin/sh\n echo $$ > \" $(dirname \" $0\" )/pid\" \n cat >/dev/null\n exec sleep 60\n " ;
1512- let binary = fixture_binary ( & dir, script) ;
1513- let provider = ClaudeCliProvider {
1514- id : ProviderId :: new ( ProviderId :: ANTHROPIC ) ,
1515- binary : Some ( binary) ,
1516- heartbeat : std:: time:: Duration :: from_millis ( 50 ) ,
1517- quiet_limit : Some ( std:: time:: Duration :: from_secs ( 1 ) ) ,
1518- goal_authority : None ,
1519- } ;
1520- let request = request_with ( vec ! [ Message :: user( "fixture prompt" ) ] ) ;
1521- let started = std:: time:: Instant :: now ( ) ;
1522- let error = match provider. create_message_stream ( request) . await {
1523- Ok ( _) => panic ! ( "a CLI that printed nothing produced a stream" ) ,
1524- Err ( error) => error,
1525- } ;
1526- let took = started. elapsed ( ) ;
1527- let error_result = Err ( error) ;
1528- assert ! ( is_quiet_limit_error( & error_result) , "{error_result:?}" ) ;
1529- let pid = std:: fs:: read_to_string ( dir. join ( "pid" ) ) . unwrap ( ) ;
1546+ for _ in 0 ..PID_ATTEMPTS {
1547+ let dir = std:: env:: temp_dir ( ) . join ( format ! ( "mute-claude-{}" , uuid:: Uuid :: new_v4( ) ) ) ;
1548+ std:: fs:: create_dir ( & dir) . unwrap ( ) ;
1549+ let script =
1550+ "#!/bin/sh\n echo $$ > \" $(dirname \" $0\" )/pid\" \n cat >/dev/null\n exec sleep 60\n " ;
1551+ let binary = fixture_binary ( & dir, script) ;
1552+ let provider = ClaudeCliProvider {
1553+ id : ProviderId :: new ( ProviderId :: ANTHROPIC ) ,
1554+ binary : Some ( binary) ,
1555+ heartbeat : std:: time:: Duration :: from_millis ( 50 ) ,
1556+ quiet_limit : Some ( std:: time:: Duration :: from_secs ( 1 ) ) ,
1557+ goal_authority : None ,
1558+ } ;
1559+ let request = request_with ( vec ! [ Message :: user( "fixture prompt" ) ] ) ;
1560+ let started = std:: time:: Instant :: now ( ) ;
1561+ let error = match provider. create_message_stream ( request) . await {
1562+ Ok ( _) => panic ! ( "a CLI that printed nothing produced a stream" ) ,
1563+ Err ( error) => error,
1564+ } ;
1565+ let took = started. elapsed ( ) ;
1566+ let error_result = Err ( error) ;
1567+ assert ! ( is_quiet_limit_error( & error_result) , "{error_result:?}" ) ;
1568+ assert ! (
1569+ took >= std:: time:: Duration :: from_secs( 1 ) ,
1570+ "failed after {took:?}"
1571+ ) ;
1572+ assert ! (
1573+ took < std:: time:: Duration :: from_secs( 30 ) ,
1574+ "failed after {took:?}"
1575+ ) ;
15301576
1531- assert ! (
1532- took >= std:: time:: Duration :: from_secs( 1 ) ,
1533- "failed after {took:?}"
1534- ) ;
1535- assert ! (
1536- took < std:: time:: Duration :: from_secs( 30 ) ,
1537- "failed after {took:?}"
1538- ) ;
1539- let running = still_running_after_a_wait ( pid. trim ( ) ) . await ;
1540- let _ = std:: fs:: remove_dir_all ( dir) ;
1541- assert ! (
1542- !running,
1543- "the hung CLI (pid {}) is still running" ,
1544- pid. trim( )
1545- ) ;
1577+ if assert_recorded_cli_stopped ( dir) . await {
1578+ return ;
1579+ }
1580+ }
1581+ no_pid_recorded ( ) ;
15461582 }
15471583
15481584 /// The limit is on unbroken silence, not on the length of the turn: each
0 commit comments