[agent] refactor serializeEvents