Stream the report to the terminal as the writer generates it #5

Merged
schaefera merged 5 commits from worktree-deep-research-streaming into master 2026-08-17 10:48:53 +00:00
Showing only changes of commit 4f32dd83d6 - Show all commits

View file

@ -178,6 +178,11 @@ async fn write_report(
drop(spinner); drop(spinner);
let mut report = String::new(); let mut report = String::new();
// Locked once for the whole stream rather than per chunk (as print!
// would do internally) — chunks arrive in a tight loop, so re-acquiring
// the lock on every one adds up.
let stdout = std::io::stdout();
let mut handle = stdout.lock();
while let Some(chunk) = response_stream.next().await { while let Some(chunk) = response_stream.next().await {
match chunk? { match chunk? {
@ -189,13 +194,16 @@ async fn write_report(
// Terminal stdout is line-buffered, so a flush is needed here — // Terminal stdout is line-buffered, so a flush is needed here —
// otherwise a chunk without a trailing newline sits in the // otherwise a chunk without a trailing newline sits in the
// buffer instead of appearing as it streams in. // buffer instead of appearing as it streams in.
print!("{text}"); write!(handle, "{text}")?;
std::io::stdout().flush()?; handle.flush()?;
} }
_ => continue, _ => continue,
} }
} }
println!(); // `handle` still holds stdout's lock here, so this must go through it
// rather than `println!` — reacquiring the same lock from this thread
// would deadlock.
writeln!(handle)?;
Ok(report) Ok(report)
} }