package opencode import ( "context" "os/exec" "time" ) // ShortLivedWaitDelay is the single finite grace for draining inherited output // pipes of short-lived, context-controlled CLI calls (API calls, `--help` // detection and the native export fallback). After the child exits os/exec waits // at most this long for descendants that still hold the inherited stdout/stderr // pipes, then closes the pipes and reports exec.ErrWaitDelay. It bounds pipe // draining only: it does not terminate, signal or confirm cleanup of descendants. const ShortLivedWaitDelay = 250 * time.Millisecond // CommandContext builds a short-lived CLI command with a finite WaitDelay so a // context cancellation or a descendant-held output pipe cannot block the caller // for the child's natural lifetime. Callers still set Dir, Stdout and Stderr. // It has no dependencies and no side effects. func CommandContext(ctx context.Context, executable string, args ...string) *exec.Cmd { cmd := exec.CommandContext(ctx, executable, args...) cmd.WaitDelay = ShortLivedWaitDelay return cmd }