Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): finish deleting an agent whose Switch identity is already gone

raullopez-sandboxf5ad01a533f8
diff --git a/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.test.ts b/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.test.ts
index d2f948774..d804da2a9 100644
--- a/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.test.ts
@@ -84,7 +84,8 @@ vi.mock('@main/core/switch-servers/gateway-client', () => ({
GatewayError: class GatewayError extends Error {
constructor(
readonly kind: string,
- message: string
+ message: string,
+ readonly status?: number
) {
super(message);
}
@@ -119,8 +120,9 @@ const { getAgentLocation } = await import('./agent-location');
const { stopRemoteWatcher } = await import('./remote-watcher');
const { autoSessionWatcher } = await import('@main/core/switch-rooms/auto-session-watcher');
const { stopSharedAgentSessions } = await import('./stop-shared-agent-sessions');
-const { deleteAgent: gatewayDeleteAgent } =
+const { deleteAgent: gatewayDeleteAgent, GatewayError } =
await import('@main/core/switch-servers/gateway-client');
+const { forgetManagedAgent } = await import('@main/core/agent-migration/managed-agents-store');
const CREDS = JSON.stringify({
env: { SWITCH_API_ENDPOINT: 'https://s', SWITCH_API_TOKEN: 'tok-123', SWITCH_AGENT_ID: 'sw-1' },
@@ -479,4 +481,59 @@ describe('deleteAgent', () => {
);
});
});
+ describe('deleting in Switch', () => {
+ // An early agent: linked to Switch, its credentials still in the legacy
+ // `.claude/switch-subagents/` file.
+ const legacyAgent = {
+ id: 'agent-1',
+ name: 'cc-hoot',
+ providerId: 'claude',
+ locationId: 'loc',
+ workspaceId: 'ws-1',
+ switchAgentId: 'sw-1',
+ };
+ const full = { deleteInSwitch: true, removeProvisionedFiles: true, trigger: 'user' } as const;
+
+ beforeEach(() => {
+ h.state.agent = legacyAgent;
+ h.state.fs = fakeFs({ '.claude/switch-subagents/cc-hoot.settings.json': CREDS });
+ h.state.repoAgents = {
+ removeLocal: async (fs: PluginFs, name: string) =>
+ fs.delete(`.claude/switch-subagents/${name}.settings.json`),
+ };
+ });
+
+ it('removes a legacy agent for good: its row, managed record and credentials', async () => {
+ vi.mocked(gatewayDeleteAgent).mockResolvedValue(undefined);
+ await deleteAgent('agent-1', full);
+
+ expect(gatewayDeleteAgent).toHaveBeenCalledWith({ id: 'srv-1' }, 'sw-1');
+ expect(h.deleteRow).toHaveBeenCalledTimes(1);
+ expect(forgetManagedAgent).toHaveBeenCalledWith('agent-1');
+ expect(await h.state.fs.exists('.claude/switch-subagents/cc-hoot.settings.json')).toBe(false);
+ });
+
+ it('still removes the agent here when its Switch identity is already gone', async () => {
+ vi.mocked(gatewayDeleteAgent).mockRejectedValue(
+ new GatewayError('http', '404 Not Found', 404)
+ );
+ await deleteAgent('agent-1', full);
+
+ expect(h.deleteRow).toHaveBeenCalledTimes(1);
+ expect(await h.state.fs.exists('.claude/switch-subagents/cc-hoot.settings.json')).toBe(false);
+ });
+
+ it('surfaces any other refusal and leaves the agent, and its controller, untouched', async () => {
+ vi.mocked(gatewayDeleteAgent).mockRejectedValue(
+ new GatewayError('http', '500 Internal Server Error', 500)
+ );
+ await expect(deleteAgent('agent-1', full)).rejects.toThrow('500');
+
+ expect(autoSessionWatcher.stopForAgent).not.toHaveBeenCalled();
+ expect(stopSharedAgentSessions).not.toHaveBeenCalled();
+ expect(h.discardControllerState).not.toHaveBeenCalled();
+ expect(h.deleteRow).not.toHaveBeenCalled();
+ expect(await h.state.fs.exists('.claude/switch-subagents/cc-hoot.settings.json')).toBe(true);
+ });
+ });
});
diff --git a/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.ts b/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.ts
index 32a2b8c89..b11cdf6f8 100644
--- a/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.ts
+++ b/console/apps/switch-console-desktop/src/main/core/agents/deleteAgent.ts
@@ -107,7 +107,19 @@ async function deleteAgentInSwitch(agent: Agent): Promise<void> {
);
}
- await withWorkspaceSession(workspaceId, (server) => gatewayDeleteAgent(server, switchAgentId));
+ try {
+ await withWorkspaceSession(workspaceId, (server) => gatewayDeleteAgent(server, switchAgentId));
+ } catch (error) {
+ // Already gone on the server (an old identity, or a server since reset): the
+ // local delete still has to happen, or the agent can never be removed.
+ if (!(error instanceof GatewayError && error.kind === 'http' && error.status === 404)) {
+ throw error;
+ }
+ log.warn('deleteAgent: the Switch identity was already gone', {
+ agentId: agent.id,
+ switchAgentId,
+ });
+ }
}
/**
@@ -227,16 +239,16 @@ async function removeAgent(
const terminate = removeFiles || options.deleteInSwitch;
const stopController = () =>
location?.sshHost ? stopRemoteWatcher(agentId) : autoSessionWatcher.stopForAgent(agentId);
+ // Gateway cascade first: fail loud before touching local state (the controller
+ // included) so a failure leaves the agent exactly as it was.
+ if (options.deleteInSwitch && agent) {
+ await deleteAgentInSwitch(agent);
+ }
const stoppedUpFront = terminate && agent !== undefined;
if (stoppedUpFront) {
await stopController();
await stopSharedAgentSessions(agent);
}
- // Gateway cascade first: fail loud before touching local state so a failure
- // never leaves the row deleted but the Switch identity orphaned.
- if (options.deleteInSwitch && agent) {
- await deleteAgentInSwitch(agent);
- }
const sessionRows = await db
.select({ id: sessions.id })