agent: pass error through when writing state files