```\nfunction upgradeAgent(\n address agent\n) external returns (address newAgent) {\n IAgent oldAgent = IAgent(agent);\n address owner = IAuth(address(oldAgent)).owner();\n uint256 agentId = agents[agent];\n // only the Agent's owner can upgrade, and only a registered agent can be upgraded\n if (owner != msg.sender || agentId == 0) revert Unauthorized();\n // deploy a new instance of Agent with the same ID and auth\n newAgent = GetRoute.agentDeployer(router).deploy(\n router,\n agentId,\n owner,\n IAuth(address(oldAgent)).operator()\n );\n // Register the new agent and unregister the old agent\n agents[newAgent] = agentId;\n // transfer funds from old agent to new agent and mark old agent as decommissioning\n oldAgent.decommissionAgent(newAgent);\n // delete the old agent from the registry\n agents[agent] = 0;\n}\n```\n