refactor(agent): enhance agent deletion logic and add cleanup for main agents

This commit is contained in:
Abhimanyu Saharan
2026-02-10 20:10:36 +05:30
parent 9bd34503d6
commit d9199f8d8d
3 changed files with 189 additions and 19 deletions

View File

@@ -941,6 +941,29 @@ async def cleanup_agent(
return None
async def cleanup_main_agent(
agent: Agent,
gateway: Gateway,
) -> str | None:
"""Remove the gateway-main agent from gateway config and delete its session."""
if not gateway.url:
return None
if not gateway.workspace_root:
msg = "gateway_workspace_root is required"
raise ValueError(msg)
workspace_path = _workspace_path(agent, gateway.workspace_root)
control_plane = _control_plane_for_gateway(gateway)
agent_id = GatewayAgentIdentity.openclaw_agent_id(gateway)
await control_plane.delete_agent(agent_id, delete_files=True)
session_key = (agent.openclaw_session_id or GatewayAgentIdentity.session_key(gateway) or "").strip()
if session_key:
with suppress(OpenClawGatewayError):
await control_plane.delete_agent_session(session_key)
return workspace_path
_T = TypeVar("_T")