Since the environmentalgorithm takes an optional argument as part of its definition, it "checks ahead" to see if you follow \algorithm with [ or not. You've placed the older definition \oldalgorithm at the start of the newly redefined \algorithm, meaning it is literally followed by \global, which is not [. Placing \oldalgorithm at the end of \algorithm allows scanning to pick up [ in the input stream correctly.