Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: Use exponential backoff for PTY drain (#54)
Maximilian Roos895fd1dbc817
diff --git a/tests/common/mod.rs b/tests/common/mod.rsindex f2ae2d255..a816bf75e 100644--- a/tests/common/mod.rs+++ b/tests/common/mod.rs@@ -977,11 +977,50 @@ pub fn validate_ansi_codes(text: &str) -> Vec<String> {// Timing utilities for background command tests// ============================================================================+/// Configuration for exponential backoff polling.+///+/// Default: 10ms → 20ms → 40ms → ... → 500ms max, 5s timeout.+#[derive(Debug, Clone)]+pub struct ExponentialBackoff {+ /// Initial sleep duration in milliseconds+ pub initial_ms: u64,+ /// Maximum sleep duration in milliseconds+ pub max_ms: u64,+ /// Total timeout+ pub timeout: std::time::Duration,+}++impl Default for ExponentialBackoff {+ fn default() -> Self {+ Self {+ initial_ms: 10,+ max_ms: 500,+ timeout: std::time::Duration::from_secs(5),+ }+ }+}++impl ExponentialBackoff {+ /// Create with fast initial polling (1ms start), good for low-latency scenarios.+ pub fn fast() -> Self {+ Self {+ initial_ms: 1,+ max_ms: 100,+ timeout: std::time::Duration::from_secs(5),+ }+ }++ /// Sleep for the appropriate duration based on attempt number.+ pub fn sleep(&self, attempt: u32) {+ let ms = (self.initial_ms * (1u64 << attempt.min(20))).min(self.max_ms);+ std::thread::sleep(std::time::Duration::from_millis(ms));+ }+}+/// Poll with exponential backoff: 10ms → 20ms → 40ms → ... → 500ms max./// Fast initial checks catch quick completions; backs off to reduce CPU on slow CI.fn exponential_sleep(attempt: u32) {- let ms = (10 * (1 << attempt)).min(500);- std::thread::sleep(std::time::Duration::from_millis(ms));+ ExponentialBackoff::default().sleep(attempt);}/// Wait for a file to exist, polling with exponential backoff.diff --git a/tests/common/progressive_output.rs b/tests/common/progressive_output.rsindex 78128bdd8..c3c9ebabb 100644--- a/tests/common/progressive_output.rs+++ b/tests/common/progressive_output.rs@@ -98,30 +98,21 @@//! - `with_byte_interval(n)` - Capture every n bytes (deterministic)//! - `default()` - Time-based capture every 50ms (less deterministic)-use crate::common::TestRepo;+use crate::common::{ExponentialBackoff, TestRepo};use insta_cmd::get_cargo_bin;use portable_pty::{CommandBuilder, PtySize, native_pty_system};use std::io::Read;use std::time::{Duration, Instant};-/// Maximum time to spend draining remaining PTY data after child exits.-/// Long timeout ensures reliability on slow CI; fast polling ensures quick completion.-const FINAL_DRAIN_TIMEOUT_MS: u64 = 5000;-/// Polling interval when waiting for output or child process exit./// Short interval ensures tests complete quickly when data is available.const OUTPUT_POLL_INTERVAL_MS: u64 = 10;-/// Delay after child exits before starting drain, to allow kernel buffers to flush.-/// On Linux CI, the kernel may not immediately make all buffered PTY data available.-/// 50ms is conservative - typical kernel buffer flush is <10ms, but CI load can cause delays.-const POST_EXIT_SETTLE_MS: u64 = 50;--/// Number of consecutive EOF reads required before considering the stream truly empty.+/// Number of consecutive "no data" reads required before considering the stream truly empty./// On Linux, the PTY may return EOF before all data has arrived (kernel scheduling).-/// With 10ms polling, 3 consecutive EOFs = 30ms of no data, which is sufficient to-/// confirm the stream is truly empty rather than just momentarily drained.-const CONSECUTIVE_EOF_THRESHOLD: u32 = 3;+/// With exponential backoff, this means we wait for increasing intervals between attempts,+/// giving slow systems more time while fast systems complete quickly.+const STABLE_READ_THRESHOLD: u32 = 3;/// Strategy for capturing progressive output snapshots#[derive(Debug, Clone)]@@ -438,40 +429,44 @@ pub fn capture_progressive_output(Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {// Check if child exitedif let Ok(Some(_)) = child.try_wait() {- // Allow kernel buffers to settle before draining.- // On Linux CI, the kernel may not immediately make all PTY data available.- std::thread::sleep(Duration::from_millis(POST_EXIT_SETTLE_MS));-- // Drain remaining PTY data with long timeout but fast polling.- // Long timeout (5s) ensures reliability on slow CI machines.- // Fast polling (10ms) ensures quick completion when data is available.+ // Drain remaining PTY data using exponential backoff.+ // Fast systems: 1ms → 2ms → 4ms → done (~7ms total)+ // Slow systems: can wait 100ms+ per attempt if needed+ let backoff = ExponentialBackoff::fast();+ let mut attempt = 0u32;+ let mut consecutive_no_data = 0u32;let drain_start = Instant::now();- let mut consecutive_eof = 0u32;+loop {match reader.read(&mut temp_buf) {Ok(0) => {- // On Linux, PTY may return EOF before all data is available.- // Require multiple consecutive EOFs to confirm stream is empty.- consecutive_eof += 1;- if consecutive_eof >= CONSECUTIVE_EOF_THRESHOLD {+ // EOF - may be spurious on Linux, require consecutive confirmations+ consecutive_no_data += 1;+ if consecutive_no_data >= STABLE_READ_THRESHOLD {break;}- std::thread::sleep(Duration::from_millis(OUTPUT_POLL_INTERVAL_MS));+ backoff.sleep(attempt);+ attempt += 1;}Ok(n) => {- consecutive_eof = 0; // Reset on successful read+ // Got data - reset stability counter, process immediately+ consecutive_no_data = 0;+ attempt = 0; // Reset backoff when data flowstotal_bytes += n;parser.process(&temp_buf[..n]);}Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {- consecutive_eof = 0; // Reset - WouldBlock means more data may come- // PTY may still have buffered data; retry with timeout- if drain_start.elapsed()- > Duration::from_millis(FINAL_DRAIN_TIMEOUT_MS)- {+ // No data available yet - count toward stability+ consecutive_no_data += 1;+ if consecutive_no_data >= STABLE_READ_THRESHOLD {+ break;+ }+ // Check timeout+ if drain_start.elapsed() > backoff.timeout {break;}- std::thread::sleep(Duration::from_millis(OUTPUT_POLL_INTERVAL_MS));+ backoff.sleep(attempt);+ attempt += 1;}Err(_) => break,}