Make agent error handling consistent with remote sessions, and create more agent test cases.