[agent] make toJSON resialiant to circular refs