agent: refactor task monitoring