Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: Improve PTY drain reliability for progressive output tests (#53)

Maximilian Roos901b23f5de9d
diff --git a/README.md b/README.md
index ee754b1b7..ce671dc87 100644
--- a/README.md
+++ b/README.md
@@ -25,8 +25,11 @@ statusline.
I've been using Worktrunk as my daily driver while building it for the past
couple of months, and found the workflows extremely good. It's built with love;
-there's no slop here! If some social proof is helpful: I also created PRQL and
-am a maintainer of Xarray & Insta & Numbagg.
+there's no slop here! If some social proof is helpful: I also created
+[PRQL](https://github.com/PRQL/prql) (10k stars) and am a maintainer of
+[Xarray](https://github.com/pydata/xarray) (4k stars),
+[Insta](https://github.com/mitsuhiko/insta), &
+[Numbagg](https://github.com/numbagg/numbagg).
## Demo
diff --git a/src/commands/statusline.rs b/src/commands/statusline.rs
index 850346d80..7f0d5fa1e 100644
--- a/src/commands/statusline.rs
+++ b/src/commands/statusline.rs
@@ -199,8 +199,12 @@ pub fn run(claude_code: bool) -> Result<()> {
// Output via output system (goes to stderr, like `wt list`)
if !output.is_empty() {
if claude_code {
+ use std::io::Write;
let reset = anstyle::Reset;
- output::data(format!("{reset} {output}"))?;
+ // Bypass anstream - write directly to stdout to preserve ANSI codes
+ // regardless of TTY detection (Claude Code expects raw ANSI)
+ writeln!(std::io::stdout(), "{reset} {output}")?;
+ std::io::stdout().flush()?;
} else {
output::data(output)?;
}
diff --git a/tests/common/progressive_output.rs b/tests/common/progressive_output.rs
index de3535da1..78128bdd8 100644
--- a/tests/common/progressive_output.rs
+++ b/tests/common/progressive_output.rs
@@ -112,6 +112,17 @@ const FINAL_DRAIN_TIMEOUT_MS: u64 = 5000;
/// 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.
+/// 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;
+
/// Strategy for capturing progressive output snapshots
#[derive(Debug, Clone)]
pub enum CaptureStrategy {
@@ -427,18 +438,33 @@ pub fn capture_progressive_output(
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
// Check if child exited
if 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.
let drain_start = Instant::now();
+ let mut consecutive_eof = 0u32;
loop {
match reader.read(&mut temp_buf) {
- Ok(0) => break,
+ 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 {
+ break;
+ }
+ std::thread::sleep(Duration::from_millis(OUTPUT_POLL_INTERVAL_MS));
+ }
Ok(n) => {
+ consecutive_eof = 0; // Reset on successful read
total_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)