Ray's Knowledge Base
Unlock

A wait loop on a background task's output ends early if its grep pattern matches the first result

PitfallVerified 27 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

An until grep ...; do sleep 15; done loop on a background task's output file stops too early. The agent then reads partial output while the run is still going. In this case the output was cut off, or the agent judged the run before all results were in.

Cause#

The grep pattern matched the first result line (for example one N fail line or one test result line). A multi-part run, such as cargo test with one test result line per test binary, prints many such lines. The first match is not the end of the run.

Fix#

Wait for the final state, not the first match. Count the expected number of result lines (for example three or more test result lines), and also stop on a failure marker like FAILED. Then sleep a short time and read the whole file. Split a long check run into steps, so that if a session cuts a step off, less work is lost. After a cut-off, check whether the call took effect before you trust or repeat it.

Evidence#

"The wait ended too early (my pattern matched the first result)". The fixed loop counted ^test result lines against a threshold, or stopped on FAILED, then ran sleep 20; cat on the output file and worked. The earlier loop was interrupted when the session ended. "The run was cut off. I start it again, one step at a time so a cut loses less."