āœ… test(kavach): run-child's pipe test waits for the descendant, not 0.5 s

Merged
AT
15d ago
+43-12

packages/kavach/corpus/run-child.test.ts, "cuts the pipes a descendant outside the group holds open", now waits for the descendant on an observed event instead of a fixed 0.5 s. A local root pnpm check passes all the way on this Mac. The failure was reported in #147.

Cause

The child ran python3 … & sleep 0.5; echo started. Python had to call setsid() and print "left" within that 0.5 s. On this Mac, python3 is the pyenv shim, and it takes 460 to 1100 ms to start. So the shell printed "started" and exited first, and runChild's group kill took python while it was still in the group. stdout was 'started\n' on every run.

This is not about macOS process-group or pipe semantics:

  • With /usr/bin first on PATH, the same command gives 'left\nstarted\n'.
  • CI's ubuntu runner starts python quickly, and the test passes there (1.5 s on main).

Change

The test file is the only change; run-child.ts is untouched.

  • Once python has left the group, it writes its pid to a file: it writes a temp file, then renames it. The shell polls for that file before echo started. A 20 s timeoutMs is a hang guard only.
  • The old took < 2900 bound counted python's start-up, so a 1.5 s start would still have broken it. It is replaced by the property itself: the holder is still alive when the outcome returns, so the pipes were cut. The holder is SIGKILLed in t.after.
  • The drain's lower bound is now timed from the record's mtime, not from the spawn, so start-up time cannot stand in for the drain.

Checked

  • The test file passes alone, with the pyenv shim first on PATH.
  • It passes with a PATH shim that sleeps 1.5 s before exec-ing the real python3, as docs/lessons/testing.md prescribes. The old test fails with plain pyenv.
  • With the drain's destroy() calls in run-child.ts disabled, the new test fails: the holder had exited, so kill returned ESRCH.
  • The root pnpm check exits 0 on this Mac. kavach passed 759 tests, sarathi 305, pariksha 3527, dvara 328, shuddhi 2804 and agnipariksha 91. That run used the first commit. The second commit changes only assertion order and the drain's anchor, and that file was re-run alone and under the shim.

Slice: #149

šŸ¤– Generated with Claude Code


Summary by cubic

Fixes the flaky run-child pipe test so it passes on Macs where python3 is a pyenv shim that takes up to 1.1 s to start.

The test previously waited a fixed 0.5 s for the descendant to leave the process group; slow python startup meant the shell exited first and the group kill hit python while still in the group. Now the descendant writes its pid to a temp file (renamed once it has left the group), and the shell polls for that file before exiting. A 20 s timeoutMs is only a hang guard.

  • Replaces the old took < 2900 bound with the property itself: the holder is still alive when the outcome returns, so the pipes were cut; the holder is SIGKILLed in t.after.
  • Times the drain's lower bound from the record's mtime, so python's start-up can't count as the drain.
  • Only packages/kavach/corpus/run-child.test.ts changes; run-child.ts is untouched.