Skip to content

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

Devices: Remove waits for the agent list; restart a controller Console runs

Petr Bauch244f44706b73
diff --git a/console/apps/switch-console-desktop/src/renderer/features/machines/devices-settings-page.tsx b/console/apps/switch-console-desktop/src/renderer/features/machines/devices-settings-page.tsx
index faf8d2d0b..f078412e1 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/machines/devices-settings-page.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/machines/devices-settings-page.tsx
@@ -30,6 +30,7 @@ import {
machineCategory,
machineConnection,
} from './machine-state';
+import { type PlacedAgents, placedAgents } from './machine-state';
import { useDeviceActions } from './use-device-actions';
import { useMachineDetails } from './use-machines';
@@ -122,8 +123,7 @@ function DevicesBody({
filter === 'all' ? list : list.filter((machine) => machineCategory(machine) === filter);
const live = shown(arranged.live);
const removed = shown(arranged.removed);
- const agentsOn = (machine: MachineDetail) =>
- (managed.data ?? []).filter((agent) => agent.machine?.id === machine.id).length;
+ const agentsOn = (machine: MachineDetail) => placedAgents(managed, machine.id);
return (
<div className="flex flex-col gap-6 pt-4 pb-10">
@@ -180,7 +180,7 @@ function DeviceCard({
}: {
serverId: string;
machine: MachineDetail;
- agentCount: number;
+ agentCount: PlacedAgents;
}) {
const { navigate } = useNavigate();
const connection = machineConnection(machine);
@@ -258,9 +258,13 @@ function DeviceCard({
: machine.name}
</div>
<div className="truncate text-sm text-foreground-muted">
- {agentCount === 0
- ? 'No agents'
- : `${agentCount} ${agentCount === 1 ? 'agent' : 'agents'}`}
+ {agentCount.kind === 'loading'
+ ? 'Agents…'
+ : agentCount.kind === 'unreadable'
+ ? 'Agents could not be listed'
+ : agentCount.count === 0
+ ? 'No agents'
+ : `${agentCount.count} ${agentCount.count === 1 ? 'agent' : 'agents'}`}
</div>
</div>
{actions.error && (
diff --git a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-details.tsx b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-details.tsx
index f94d2920b..b296fdb22 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-details.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-details.tsx
@@ -1,5 +1,5 @@
import { format } from 'date-fns';
-import { ExternalLink, MoreHorizontal, Pencil, Plus, Trash2 } from 'lucide-react';
+import { ExternalLink, MoreHorizontal, Pencil, Plus, RotateCw, Trash2 } from 'lucide-react';
import { CopyButton } from '@renderer/features/admin/copy-button';
import { CloudMachineCard } from '@renderer/features/cloud-agents/cloud-machine-card';
import { useCloudMachines } from '@renderer/features/cloud-agents/use-cloud-agents';
@@ -39,6 +39,7 @@ import {
type MachineTone,
platformLabel,
} from './machine-state';
+import type { PlacedAgents } from './machine-state';
import { useDeviceActions } from './use-device-actions';
/** A notice's box, in the server page's hues. */
@@ -81,17 +82,20 @@ export function MachineDetails({
serverId,
machine,
placed,
+ placedState,
}: {
serverId: string;
machine: MachineDetail;
/** The managed agents placed on it. */
placed: ManagedAgentView[];
+ /** Whether `placed` is read yet, for Remove's warning about the agents it strands. */
+ placedState: PlacedAgents;
}) {
const connection = machineConnection(machine);
const notices = machineNotices(machine);
const removed = machine.state === 'revoked';
const showAddAgentModal = useShowModal('addAgentModal');
- const actions = useDeviceActions(serverId, machine, placed.length);
+ const actions = useDeviceActions(serverId, machine, placedState);
const owner = switchServersStore.statusFor(serverId)?.user?.name ?? null;
return (
@@ -109,6 +113,17 @@ export function MachineDetails({
</p>
</div>
<div className="flex shrink-0 items-center gap-2">
+ {actions.restartable && (
+ <Button
+ size="sm"
+ variant="outline"
+ disabled={actions.restarting}
+ onClick={() => void actions.restart()}
+ >
+ <RotateCw className="size-3.5" />
+ Restart controller
+ </Button>
+ )}
{actions.renamable && (
<Button size="sm" variant="outline" onClick={actions.rename}>
<Pencil className="size-3.5" />
diff --git a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.test.ts b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.test.ts
index 80ec8d15a..0cd9bf08a 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';
import type {
MachineDetail,
MachineProviderDetail,
+ ManagedAgentView,
} from '@shared/core/managed-agents/managed-agents';
import {
arrangeMachines,
@@ -10,6 +11,7 @@ import {
machineCategory,
machineConnection,
machineNotices,
+ placedAgents,
moveTarget,
platformLabel,
sessionsLabel,
@@ -286,3 +288,22 @@ describe('machineCategory', () => {
expect(machineCategory(machine({ id: 'd' }))).toBe('computer');
});
});
+
+describe('placedAgents', () => {
+ const on = (id: string | null) =>
+ ({
+ machine: id ? { id, name: id, kind: 'daemon', state: 'online' } : null,
+ }) as ManagedAgentView;
+
+ it('counts the agents placed on the device once the list is read', () => {
+ expect(
+ placedAgents({ data: [on('m1'), on('m1'), on('m2'), on(null)], isError: false }, 'm1')
+ ).toEqual({ kind: 'known', count: 2 });
+ expect(placedAgents({ data: null, isError: false }, 'm1')).toEqual({ kind: 'known', count: 0 });
+ });
+
+ it('says when the list is not read yet, or could not be', () => {
+ expect(placedAgents({ data: undefined, isError: false }, 'm1')).toEqual({ kind: 'loading' });
+ expect(placedAgents({ data: undefined, isError: true }, 'm1')).toEqual({ kind: 'unreadable' });
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.ts b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.ts
index ff0d18928..749afc731 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-state.ts
@@ -1,4 +1,8 @@
-import type { MachineDetail, OwnedMachine } from '@shared/core/managed-agents/managed-agents';
+import type {
+ MachineDetail,
+ ManagedAgentView,
+ OwnedMachine,
+} from '@shared/core/managed-agents/managed-agents';
import { providerDisplayName } from '@shared/core/providers/agent-provider-registry';
export type MachineTone = 'ok' | 'warning' | 'problem' | 'idle';
@@ -311,3 +315,25 @@ export function machineCategory(machine: Pick<MachineDetail, 'local' | 'cloud'>)
if (machine.local?.kind === 'ssh-host') return 'ssh-host';
return 'computer';
}
+
+/**
+ * How many of the person's agents are placed on a device, as far as their
+ * agent list says: not yet read, could not be read, or a count. Removing a
+ * device warns about the agents it strands, so it waits for the list.
+ */
+export type PlacedAgents =
+ | { kind: 'known'; count: number }
+ | { kind: 'loading' }
+ | { kind: 'unreadable' };
+
+export function placedAgents(
+ managed: { data: ManagedAgentView[] | null | undefined; isError: boolean },
+ machineId: string
+): PlacedAgents {
+ if (managed.isError) return { kind: 'unreadable' };
+ if (managed.data === undefined) return { kind: 'loading' };
+ return {
+ kind: 'known',
+ count: (managed.data ?? []).filter((agent) => agent.machine?.id === machineId).length,
+ };
+}
diff --git a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-view.tsx b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-view.tsx
index d79f4b763..fe77d5be0 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/machines/machine-view.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/machines/machine-view.tsx
@@ -10,6 +10,7 @@ import { failureText } from '@renderer/lib/errors/describe-failure';
import { useNavigate, useParams } from '@renderer/lib/layout/navigation-provider';
import { Spinner } from '@renderer/lib/ui/spinner';
import { MachineDetails } from './machine-details';
+import { placedAgents } from './machine-state';
import { useMachineDetails } from './use-machines';
type MachineParams = { serverId: string; machineId: string; name: string };
@@ -62,6 +63,7 @@ const MachinePanel = observer(function MachinePanel() {
serverId={params.serverId}
machine={machine}
placed={(managed.data ?? []).filter((agent) => agent.machine?.id === machine.id)}
+ placedState={placedAgents(managed, machine.id)}
/>
)}
</div>
diff --git a/console/apps/switch-console-desktop/src/renderer/features/machines/use-device-actions.ts b/console/apps/switch-console-desktop/src/renderer/features/machines/use-device-actions.ts
index 68dc30935..a44dc2bec 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/machines/use-device-actions.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/machines/use-device-actions.ts
@@ -5,6 +5,7 @@ import { failureText } from '@renderer/lib/errors/describe-failure';
import { rpc } from '@renderer/lib/ipc';
import { useShowModal } from '@renderer/lib/modal/modal-provider';
import type { MachineDetail } from '@shared/core/managed-agents/managed-agents';
+import type { PlacedAgents } from './machine-state';
/**
* Renaming and removing one of the person's devices, as its card and its page
@@ -12,12 +13,31 @@ import type { MachineDetail } from '@shared/core/managed-agents/managed-agents';
* computer and SSH hosts belong to the Console that set them up, which takes
* them away itself, so only a standalone machine is removed from here.
*/
-export function useDeviceActions(serverId: string, machine: MachineDetail, agentCount: number) {
+export function useDeviceActions(serverId: string, machine: MachineDetail, placed: PlacedAgents) {
const showRenameModal = useShowModal('renameMachineModal');
const showConfirm = useShowModal('confirmActionModal');
const queryClient = useQueryClient();
const [error, setError] = useState<string | null>(null);
+ const [restarting, setRestarting] = useState(false);
const live = machine.state !== 'revoked';
+ const local = machine.local;
+
+ // This computer's and this Console's SSH hosts' controllers are Console's to
+ // run, so it can start one that stopped (an error, or another copy took over).
+ const restart = async () => {
+ if (!local) return;
+ setRestarting(true);
+ setError(null);
+ try {
+ if (local.kind === 'this-computer') await rpc.embeddedController.restart(serverId);
+ else await rpc.hostControllers.restart({ sshHost: local.sshHost, serverId });
+ await queryClient.invalidateQueries({ queryKey: [MANAGED_AGENTS_KEY, serverId] });
+ } catch (err) {
+ setError(failureText(err, `Could not restart the agent controller on ${machine.name}.`));
+ } finally {
+ setRestarting(false);
+ }
+ };
const rename = () =>
showRenameModal({
@@ -31,9 +51,11 @@ export function useDeviceActions(serverId: string, machine: MachineDetail, agent
showConfirm({
title: `Remove ${machine.name}?`,
description:
- agentCount === 0
- ? `${machine.name} stops running agents for you and cannot reconnect. To use it again, enroll it again.`
- : `${machine.name} stops running agents for you and cannot reconnect. The ${agentCount === 1 ? 'agent' : `${agentCount} agents`} on it stay placed on it and do not run until you move them.`,
+ placed.kind === 'unreadable'
+ ? `${machine.name} stops running agents for you and cannot reconnect. Your agents could not be listed, so this cannot say which are on it: any placed on it stay there and do not run until you move them.`
+ : placed.kind === 'known' && placed.count > 0
+ ? `${machine.name} stops running agents for you and cannot reconnect. The ${placed.count === 1 ? 'agent' : `${placed.count} agents`} on it stay placed on it and do not run until you move them.`
+ : `${machine.name} stops running agents for you and cannot reconnect. To use it again, enroll it again.`,
confirmLabel: 'Remove',
onSuccess: () => {
setError(null);
@@ -46,9 +68,12 @@ export function useDeviceActions(serverId: string, machine: MachineDetail, agent
return {
renamable: live && !machine.cloud,
- removable: live && !machine.cloud && machine.local === null,
+ removable: live && !machine.cloud && machine.local === null && placed.kind !== 'loading',
+ restartable: live && local !== null,
+ restarting,
rename,
remove,
+ restart,
error,
};
}