Fix cpx_agent_event recording for agent_state

* Fixed states, termination of old recs
* Added dumping of agent_state upon log out

Warning:
* Old data produced by the buggy cpx_agent_event may be dumped
  upon the agent's login. The first login of said agent might be
  wrongly recorded due to wrong data.