cleanup and only write output from agent if debug is on