feat(web): add data-testid attributes to SSH settings UI

Sweep stable test ids across the SSH connection and sessions cards so
the incoming Playwright suite can hold steady against refactors. Splits
the formerly monolithic ssh-settings.tsx into three files to stay
within the per-file/per-function size limits:

- ssh-connection-card.tsx (form + test-then-trust flow)
- ssh-sessions-card.tsx (active-sessions table)
- ssh-settings.tsx (barrel re-export so existing imports keep working)

Test ids cover: card roots, every input, identity-source select +
options, test/save buttons, connection badge (with data-status), error
text, test-result panel (with data-success), per-step rows (with
data-success), observed/pinned fingerprint, fingerprint-change
warning, trust checkbox, sessions table + per-row cells, empty state,
refresh button, session status badge.
This commit is contained in:
José Almeida
2026-05-17 11:00:53 +01:00
parent 21cc4304a6
commit 85bd8a8058
3 changed files with 765 additions and 575 deletions
@@ -0,0 +1,597 @@
"use client";
import { useCallback, useMemo, useState } from "react";
import { Badge } from "@kandev/ui/badge";
import { Button } from "@kandev/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@kandev/ui/card";
import { Input } from "@kandev/ui/input";
import { Label } from "@kandev/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kandev/ui/select";
import {
IconCheck,
IconLoader2,
IconShieldLock,
IconTerminal2,
IconTestPipe,
IconX,
} from "@tabler/icons-react";
import { testSSHConnection } from "@/lib/api/domains/ssh-api";
import type {
SSHIdentitySource,
SSHTestRequest,
SSHTestResult,
SSHTestStep,
} from "@/lib/types/http-ssh";
// SSHExecutorConfig is the shape we persist into executor.Config on save.
// The host_fingerprint is set only after a successful Test Connection has
// completed and the user has ticked "Trust this host".
export interface SSHExecutorConfig {
name: string;
host_alias?: string;
host?: string;
port?: number;
user?: string;
identity_source: SSHIdentitySource;
identity_file?: string;
proxy_jump?: string;
host_fingerprint?: string;
}
export interface SSHConnectionCardProps {
initial?: Partial<SSHExecutorConfig>;
// Called when the user clicks Save after a successful test+trust. The
// returned config carries the freshly pinned fingerprint.
onSave: (config: SSHExecutorConfig) => Promise<void> | void;
// Existing running sessions for this executor. Triggers the
// "this won't affect existing sessions" warning on save.
runningSessionCount?: number;
}
interface SSHConnectionState {
form: SSHExecutorConfig;
testing: boolean;
saving: boolean;
result: SSHTestResult | null;
trust: boolean;
error: string | null;
}
const SSH_FORM_DEFAULTS: SSHExecutorConfig = {
name: "",
host_alias: "",
host: "",
port: 22,
user: "",
identity_source: "agent",
identity_file: "",
proxy_jump: "",
host_fingerprint: undefined,
};
function initialState(initial?: Partial<SSHExecutorConfig>): SSHConnectionState {
return {
form: { ...SSH_FORM_DEFAULTS, ...(initial ?? {}) },
testing: false,
saving: false,
result: null,
trust: false,
error: null,
};
}
function useSSHConnection(props: SSHConnectionCardProps) {
const [state, setState] = useState<SSHConnectionState>(() => initialState(props.initial));
const { form, testing, saving, result, trust, error } = state;
const update = useCallback(
<K extends keyof SSHExecutorConfig>(key: K, value: SSHExecutorConfig[K]) => {
setState((prev) => ({
...prev,
form: { ...prev.form, [key]: value },
result: null,
trust: false,
error: null,
}));
},
[],
);
const setTrust = useCallback((v: boolean) => setState((prev) => ({ ...prev, trust: v })), []);
const canTest = useMemo(() => {
if (testing) return false;
if (form.name.trim() === "") return false;
if ((form.host ?? "").trim() === "" && (form.host_alias ?? "").trim() === "") return false;
return true;
}, [form, testing]);
const handleTest = useCallback(async () => {
setState((prev) => ({ ...prev, testing: true, result: null, error: null }));
try {
const req: SSHTestRequest = {
name: form.name,
host_alias: form.host_alias || undefined,
host: form.host || undefined,
port: form.port || undefined,
user: form.user || undefined,
identity_source: form.identity_source,
identity_file: form.identity_file || undefined,
proxy_jump: form.proxy_jump || undefined,
};
const res = await testSSHConnection(req);
setState((prev) => ({ ...prev, result: res, testing: false }));
} catch (e) {
const msg = e instanceof Error ? e.message : "Failed to reach backend";
setState((prev) => ({ ...prev, error: msg, testing: false }));
}
}, [form]);
const canSave = !!result?.success && !!result.fingerprint && trust && !saving;
const confirmRunningSessions = useCallback(
() =>
props.runningSessionCount
? window.confirm(
`This executor has ${props.runningSessionCount} running session(s). ` +
`They will keep running on the current host. Only new sessions started ` +
`after save will use the updated config. Continue?`,
)
: true,
[props.runningSessionCount],
);
const handleSave = useCallback(async () => {
if (!canSave || !result?.fingerprint) return;
if (!confirmRunningSessions()) return;
setState((prev) => ({ ...prev, saving: true }));
try {
await props.onSave({ ...form, host_fingerprint: result.fingerprint });
} finally {
setState((prev) => ({ ...prev, saving: false }));
}
}, [canSave, confirmRunningSessions, form, props, result]);
return {
form,
testing,
saving,
result,
trust,
error,
canTest,
canSave,
update,
setTrust,
handleTest,
handleSave,
};
}
export function SSHConnectionCard(props: SSHConnectionCardProps) {
const c = useSSHConnection(props);
return (
<Card data-testid="ssh-connection-card">
<CardHeader>
<div className="flex items-center justify-between">
<div>
<CardTitle className="flex items-center gap-2">
<IconTerminal2 className="h-5 w-5" />
Connection
</CardTitle>
<CardDescription>
Run an agent on any Linux box you can reach over SSH. linux/amd64 only in v1.
</CardDescription>
</div>
<ConnectionBadge fingerprint={c.form.host_fingerprint} />
</div>
</CardHeader>
<CardContent className="space-y-4">
<SSHConnectionForm form={c.form} onChange={c.update} />
{c.form.host_fingerprint && <PinnedFingerprintRow fingerprint={c.form.host_fingerprint} />}
<SSHConnectionActions
testing={c.testing}
saving={c.saving}
canTest={c.canTest}
canSave={c.canSave}
onTest={c.handleTest}
onSave={c.handleSave}
/>
{c.error && (
<p data-testid="ssh-error" className="text-sm text-red-600">
{c.error}
</p>
)}
{c.result && (
<TestResultDisplay
result={c.result}
trust={c.trust}
onTrustChange={c.setTrust}
currentlyPinned={c.form.host_fingerprint}
/>
)}
</CardContent>
</Card>
);
}
type FieldOnChange = <K extends keyof SSHExecutorConfig>(
key: K,
value: SSHExecutorConfig[K],
) => void;
function SSHConnectionForm({
form,
onChange,
}: {
form: SSHExecutorConfig;
onChange: FieldOnChange;
}) {
return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<TextField
id="ssh-name"
testId="ssh-input-name"
label="Name"
placeholder="My VPS"
value={form.name}
onChange={(v) => onChange("name", v)}
/>
<TextField
id="ssh-host-alias"
testId="ssh-input-host-alias"
label="Host alias from ~/.ssh/config (optional)"
hint="If set, inherits HostName / Port / User / IdentityFile / ProxyJump from your config."
placeholder="prod"
value={form.host_alias ?? ""}
onChange={(v) => onChange("host_alias", v)}
/>
<TextField
id="ssh-host"
testId="ssh-input-host"
label="Host"
placeholder="dev.example.com"
value={form.host ?? ""}
onChange={(v) => onChange("host", v)}
/>
<TextField
id="ssh-port"
testId="ssh-input-port"
label="Port"
type="number"
placeholder="22"
value={String(form.port ?? 22)}
onChange={(v) => onChange("port", parseInt(v, 10) || 22)}
/>
<TextField
id="ssh-user"
testId="ssh-input-user"
label="User"
placeholder="ubuntu"
value={form.user ?? ""}
onChange={(v) => onChange("user", v)}
/>
<IdentitySourceField
value={form.identity_source}
onChange={(v) => onChange("identity_source", v)}
/>
{form.identity_source === "file" && (
<TextField
id="ssh-identity-file"
testId="ssh-input-identity-file"
label="Identity file path"
hint="Passphrase-protected keys must be loaded into ssh-agent first."
placeholder="~/.ssh/id_ed25519"
value={form.identity_file ?? ""}
onChange={(v) => onChange("identity_file", v)}
/>
)}
<TextField
id="ssh-proxy-jump"
testId="ssh-input-proxy-jump"
label="ProxyJump (optional)"
hint="Single bastion hop. Chained jumps unsupported in v1."
placeholder="bastion.example.com"
value={form.proxy_jump ?? ""}
onChange={(v) => onChange("proxy_jump", v)}
/>
</div>
);
}
function TextField({
id,
testId,
label,
hint,
placeholder,
type,
value,
onChange,
}: {
id: string;
testId: string;
label: string;
hint?: string;
placeholder?: string;
type?: string;
value: string;
onChange: (v: string) => void;
}) {
return (
<FieldShell id={id} label={label} hint={hint}>
<Input
id={id}
data-testid={testId}
type={type}
value={value}
placeholder={placeholder}
onChange={(e) => onChange(e.target.value)}
/>
</FieldShell>
);
}
function IdentitySourceField({
value,
onChange,
}: {
value: SSHIdentitySource;
onChange: (v: SSHIdentitySource) => void;
}) {
return (
<FieldShell id="ssh-identity-source" label="Identity source">
<Select value={value} onValueChange={(v) => onChange(v as SSHIdentitySource)}>
<SelectTrigger id="ssh-identity-source" data-testid="ssh-input-identity-source">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="agent" data-testid="ssh-input-identity-source-agent">
ssh-agent (SSH_AUTH_SOCK)
</SelectItem>
<SelectItem value="file" data-testid="ssh-input-identity-source-file">
Identity file (private key path)
</SelectItem>
</SelectContent>
</Select>
</FieldShell>
);
}
function FieldShell({
id,
label,
hint,
children,
}: {
id: string;
label: string;
hint?: string;
children: React.ReactNode;
}) {
return (
<div className="space-y-1.5">
<Label htmlFor={id}>{label}</Label>
{children}
{hint && <p className="text-xs text-muted-foreground">{hint}</p>}
</div>
);
}
function PinnedFingerprintRow({ fingerprint }: { fingerprint: string }) {
return (
<div
data-testid="ssh-fingerprint-pinned"
className="rounded-md border bg-muted/40 px-3 py-2 text-xs flex items-center gap-2"
>
<IconShieldLock className="h-4 w-4 shrink-0" />
<span className="text-muted-foreground">
Pinned fingerprint:{" "}
<code data-testid="ssh-fingerprint-pinned-value" className="font-mono">
{fingerprint}
</code>
</span>
</div>
);
}
function SSHConnectionActions({
testing,
saving,
canTest,
canSave,
onTest,
onSave,
}: {
testing: boolean;
saving: boolean;
canTest: boolean;
canSave: boolean;
onTest: () => void;
onSave: () => void;
}) {
return (
<div className="flex items-center gap-3">
<Button
variant="outline"
size="sm"
onClick={onTest}
disabled={!canTest}
data-testid="ssh-test-button"
className="cursor-pointer"
>
{testing ? (
<IconLoader2 className="mr-1.5 h-4 w-4 animate-spin" />
) : (
<IconTestPipe className="mr-1.5 h-4 w-4" />
)}
Test connection
</Button>
<Button
size="sm"
onClick={onSave}
disabled={!canSave}
data-testid="ssh-save-button"
className="cursor-pointer"
>
{saving ? <IconLoader2 className="mr-1.5 h-4 w-4 animate-spin" /> : null}
Save
</Button>
</div>
);
}
function ConnectionBadge({ fingerprint }: { fingerprint?: string }) {
if (!fingerprint) {
return (
<Badge data-testid="ssh-connection-badge" data-status="unverified" variant="secondary">
Unverified
</Badge>
);
}
return (
<Badge
data-testid="ssh-connection-badge"
data-status="trusted"
variant="default"
className="bg-green-600"
>
Trusted
</Badge>
);
}
function TestResultDisplay({
result,
trust,
onTrustChange,
currentlyPinned,
}: {
result: SSHTestResult;
trust: boolean;
onTrustChange: (v: boolean) => void;
currentlyPinned?: string;
}) {
return (
<div
data-testid="ssh-test-result"
data-success={result.success ? "true" : "false"}
className="rounded-md border p-3 space-y-2"
>
<TestResultHeader success={result.success} totalMs={result.total_duration_ms} />
{result.steps.map((step: SSHTestStep) => (
<StepRow key={step.name} step={step} />
))}
{result.error && !result.steps.some((s) => s.error) && (
<p data-testid="ssh-test-result-error" className="text-sm text-red-600">
{result.error}
</p>
)}
{result.success && result.fingerprint && (
<FingerprintTrustBlock
fingerprint={result.fingerprint}
currentlyPinned={currentlyPinned}
trust={trust}
onTrustChange={onTrustChange}
/>
)}
</div>
);
}
function TestResultHeader({ success, totalMs }: { success: boolean; totalMs: number }) {
return (
<div
data-testid={success ? "ssh-test-result-success" : "ssh-test-result-failure"}
className="flex items-center gap-2 text-sm font-medium"
>
{success ? (
<IconCheck className="h-4 w-4 text-green-600" />
) : (
<IconX className="h-4 w-4 text-red-600" />
)}
{success ? "Connection test passed" : "Connection test failed"}
<span className="text-muted-foreground font-normal">({totalMs}ms)</span>
</div>
);
}
function FingerprintTrustBlock({
fingerprint,
currentlyPinned,
trust,
onTrustChange,
}: {
fingerprint: string;
currentlyPinned?: string;
trust: boolean;
onTrustChange: (v: boolean) => void;
}) {
const fingerprintChanged = !!currentlyPinned && currentlyPinned !== fingerprint;
return (
<div className="mt-3 space-y-2">
<div className="text-xs">
<span className="text-muted-foreground">Host fingerprint observed: </span>
<code data-testid="ssh-fingerprint-observed" className="font-mono">
{fingerprint}
</code>
</div>
{fingerprintChanged && (
<p data-testid="ssh-fingerprint-change-warning" className="text-xs text-amber-600">
Warning: this fingerprint differs from the one currently pinned (
<code className="font-mono">{currentlyPinned}</code>). Trusting it overwrites the pinned
key. If you didn’t expect a host re-key, stop here.
</p>
)}
<label className="flex items-start gap-2 text-sm cursor-pointer">
<input
type="checkbox"
data-testid="ssh-trust-checkbox"
checked={trust}
onChange={(e) => onTrustChange(e.target.checked)}
className="mt-0.5 cursor-pointer"
/>
<span>
<strong>Trust this host.</strong> I’ve verified the fingerprint above matches my server.
Future connections that report a different fingerprint will be refused.
</span>
</label>
</div>
);
}
function StepRow({ step }: { step: SSHTestStep }) {
const slug = step.name.toLowerCase().replace(/[^a-z0-9]+/g, "-");
return (
<div
data-testid={`ssh-test-step-${slug}`}
data-success={step.success ? "true" : "false"}
className="flex items-start gap-2 text-sm pl-2"
>
{step.success ? (
<IconCheck className="h-3 w-3 text-green-600 shrink-0 mt-1" />
) : (
<IconX className="h-3 w-3 text-red-600 shrink-0 mt-1" />
)}
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<span>{step.name}</span>
<span className="text-muted-foreground text-xs">({step.duration_ms}ms)</span>
</div>
{step.output && (
<p
data-testid={`ssh-test-step-${slug}-output`}
className="text-xs text-muted-foreground truncate font-mono"
>
{step.output}
</p>
)}
{step.error && (
<p data-testid={`ssh-test-step-${slug}-error`} className="text-xs text-red-600 truncate">
{step.error}
</p>
)}
</div>
</div>
);
}
@@ -0,0 +1,161 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { Badge } from "@kandev/ui/badge";
import { Button } from "@kandev/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@kandev/ui/card";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@kandev/ui/table";
import { IconLoader2 } from "@tabler/icons-react";
import { listSSHSessions } from "@/lib/api/domains/ssh-api";
import type { SSHSession } from "@/lib/types/http-ssh";
export interface SSHSessionsCardProps {
executorId: string;
}
const REFRESH_INTERVAL_MS = 90_000;
export function SSHSessionsCard({ executorId }: SSHSessionsCardProps) {
const [sessions, setSessions] = useState<SSHSession[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const refresh = useCallback(async () => {
setLoading(true);
setError(null);
try {
const rows = await listSSHSessions(executorId);
setSessions(rows);
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to load sessions");
} finally {
setLoading(false);
}
}, [executorId]);
useEffect(() => {
refresh();
const id = setInterval(refresh, REFRESH_INTERVAL_MS);
return () => clearInterval(id);
}, [refresh]);
return (
<Card data-testid="ssh-sessions-card">
<CardHeader>
<div className="flex items-center justify-between">
<div>
<CardTitle>Active sessions</CardTitle>
<CardDescription>
Sessions currently running on this SSH host. Refreshes every 90 seconds.
</CardDescription>
</div>
<Button
variant="outline"
size="sm"
onClick={refresh}
disabled={loading}
data-testid="ssh-sessions-refresh"
className="cursor-pointer"
>
{loading ? <IconLoader2 className="mr-1.5 h-4 w-4 animate-spin" /> : null}
Refresh
</Button>
</div>
</CardHeader>
<CardContent>
<SSHSessionsBody loading={loading} error={error} sessions={sessions} />
</CardContent>
</Card>
);
}
function SSHSessionsBody({
loading,
error,
sessions,
}: {
loading: boolean;
error: string | null;
sessions: SSHSession[];
}) {
if (error) {
return (
<p data-testid="ssh-sessions-error" className="text-sm text-red-600">
{error}
</p>
);
}
if (sessions.length === 0 && !loading) {
return (
<p data-testid="ssh-sessions-empty" className="text-sm text-muted-foreground">
No active sessions.
</p>
);
}
if (sessions.length === 0) return null;
return <SSHSessionsTable sessions={sessions} />;
}
function SSHSessionsTable({ sessions }: { sessions: SSHSession[] }) {
return (
<Table data-testid="ssh-sessions-table">
<TableHeader>
<TableRow>
<TableHead>Task</TableHead>
<TableHead>Session</TableHead>
<TableHead>Host</TableHead>
<TableHead>Remote port</TableHead>
<TableHead>Local fwd</TableHead>
<TableHead>Uptime</TableHead>
<TableHead>Status</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{sessions.map((s) => (
<SSHSessionsRow key={s.session_id} session={s} />
))}
</TableBody>
</Table>
);
}
function SSHSessionsRow({ session: s }: { session: SSHSession }) {
return (
<TableRow data-testid={`ssh-session-row-${s.session_id}`}>
<TableCell className="font-mono text-xs" data-testid="ssh-session-task">
{s.task_id.slice(0, 8)}
</TableCell>
<TableCell className="font-mono text-xs" data-testid="ssh-session-id">
{s.session_id.slice(0, 8)}
</TableCell>
<TableCell className="font-mono text-xs" data-testid="ssh-session-host">
{s.user ? `${s.user}@${s.host}` : s.host}
</TableCell>
<TableCell className="font-mono text-xs" data-testid="ssh-session-remote-port">
{s.remote_agentctl_port ?? "—"}
</TableCell>
<TableCell className="font-mono text-xs" data-testid="ssh-session-local-port">
{s.local_forward_port ?? "—"}
</TableCell>
<TableCell className="text-xs" data-testid="ssh-session-uptime">
{formatUptime(s.uptime_seconds)}
</TableCell>
<TableCell>
<Badge
data-testid="ssh-session-status"
data-status={s.status}
variant={s.status === "running" ? "default" : "secondary"}
>
{s.status}
</Badge>
</TableCell>
</TableRow>
);
}
function formatUptime(s: number): string {
if (s < 60) return `${s}s`;
if (s < 3600) return `${Math.floor(s / 60)}m`;
if (s < 86400) return `${Math.floor(s / 3600)}h`;
return `${Math.floor(s / 86400)}d`;
}
+7 -575
View File
@@ -1,577 +1,9 @@
"use client";
import { useCallback, useEffect, useMemo, useState } from "react";
import { Badge } from "@kandev/ui/badge";
import { Button } from "@kandev/ui/button";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@kandev/ui/card";
import { Input } from "@kandev/ui/input";
import { Label } from "@kandev/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kandev/ui/select";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@kandev/ui/table";
import {
IconCheck,
IconLoader2,
IconShieldLock,
IconTerminal2,
IconTestPipe,
IconX,
} from "@tabler/icons-react";
import { testSSHConnection, listSSHSessions } from "@/lib/api/domains/ssh-api";
import type {
SSHIdentitySource,
SSHSession,
SSHTestRequest,
SSHTestResult,
SSHTestStep,
} from "@/lib/types/http-ssh";
// SSHExecutorConfig is the shape we persist into executor.Config on save.
// The host_fingerprint is set only after a successful Test Connection has
// completed and the user has ticked "Trust this host".
export interface SSHExecutorConfig {
name: string;
host_alias?: string;
host?: string;
port?: number;
user?: string;
identity_source: SSHIdentitySource;
identity_file?: string;
proxy_jump?: string;
host_fingerprint?: string;
}
export interface SSHConnectionCardProps {
initial?: Partial<SSHExecutorConfig>;
// Called when the user clicks Save after a successful test+trust. The
// returned config carries the freshly pinned fingerprint.
onSave: (config: SSHExecutorConfig) => Promise<void> | void;
// Existing running sessions for this executor. Triggers the "this won't
// affect existing sessions" warning on save.
runningSessionCount?: number;
}
interface SSHConnectionState {
form: SSHExecutorConfig;
testing: boolean;
saving: boolean;
result: SSHTestResult | null;
trust: boolean;
error: string | null;
}
const SSH_FORM_DEFAULTS: SSHExecutorConfig = {
name: "",
host_alias: "",
host: "",
port: 22,
user: "",
identity_source: "agent",
identity_file: "",
proxy_jump: "",
host_fingerprint: undefined,
};
function initialState(initial?: Partial<SSHExecutorConfig>): SSHConnectionState {
return {
form: { ...SSH_FORM_DEFAULTS, ...(initial ?? {}) },
testing: false,
saving: false,
result: null,
trust: false,
error: null,
};
}
function useSSHConnection(props: SSHConnectionCardProps) {
const [state, setState] = useState<SSHConnectionState>(() => initialState(props.initial));
const { form, testing, saving, result, trust, error } = state;
const update = useCallback(
<K extends keyof SSHExecutorConfig>(key: K, value: SSHExecutorConfig[K]) => {
setState((prev) => ({
...prev,
form: { ...prev.form, [key]: value },
result: null,
trust: false,
error: null,
}));
},
[],
);
const setTrust = useCallback((v: boolean) => setState((prev) => ({ ...prev, trust: v })), []);
const canTest = useMemo(() => {
if (testing) return false;
if (form.name.trim() === "") return false;
if ((form.host ?? "").trim() === "" && (form.host_alias ?? "").trim() === "") return false;
return true;
}, [form, testing]);
const handleTest = useCallback(async () => {
setState((prev) => ({ ...prev, testing: true, result: null, error: null }));
try {
const req: SSHTestRequest = {
name: form.name,
host_alias: form.host_alias || undefined,
host: form.host || undefined,
port: form.port || undefined,
user: form.user || undefined,
identity_source: form.identity_source,
identity_file: form.identity_file || undefined,
proxy_jump: form.proxy_jump || undefined,
};
const res = await testSSHConnection(req);
setState((prev) => ({ ...prev, result: res, testing: false }));
} catch (e) {
const msg = e instanceof Error ? e.message : "Failed to reach backend";
setState((prev) => ({ ...prev, error: msg, testing: false }));
}
}, [form]);
const canSave = !!result?.success && !!result.fingerprint && trust && !saving;
const confirmRunningSessions = useCallback(
() =>
props.runningSessionCount
? window.confirm(
`This executor has ${props.runningSessionCount} running session(s). ` +
`They will keep running on the current host. Only new sessions started ` +
`after save will use the updated config. Continue?`,
)
: true,
[props.runningSessionCount],
);
const handleSave = useCallback(async () => {
if (!canSave || !result?.fingerprint) return;
if (!confirmRunningSessions()) return;
setState((prev) => ({ ...prev, saving: true }));
try {
await props.onSave({ ...form, host_fingerprint: result.fingerprint });
} finally {
setState((prev) => ({ ...prev, saving: false }));
}
}, [canSave, confirmRunningSessions, form, props, result]);
return {
form,
testing,
saving,
result,
trust,
error,
canTest,
canSave,
update,
setTrust,
handleTest,
handleSave,
};
}
export function SSHConnectionCard(props: SSHConnectionCardProps) {
const c = useSSHConnection(props);
return (
<Card>
<CardHeader>
<div className="flex items-center justify-between">
<div>
<CardTitle className="flex items-center gap-2">
<IconTerminal2 className="h-5 w-5" />
Connection
</CardTitle>
<CardDescription>
Run an agent on any Linux box you can reach over SSH. linux/amd64 only in v1.
</CardDescription>
</div>
<ConnectionBadge fingerprint={c.form.host_fingerprint} />
</div>
</CardHeader>
<CardContent className="space-y-4">
<SSHConnectionForm form={c.form} onChange={c.update} />
{c.form.host_fingerprint && <PinnedFingerprintRow fingerprint={c.form.host_fingerprint} />}
<SSHConnectionActions
testing={c.testing}
saving={c.saving}
canTest={c.canTest}
canSave={c.canSave}
onTest={c.handleTest}
onSave={c.handleSave}
/>
{c.error && <p className="text-sm text-red-600">{c.error}</p>}
{c.result && (
<TestResultDisplay
result={c.result}
trust={c.trust}
onTrustChange={c.setTrust}
currentlyPinned={c.form.host_fingerprint}
/>
)}
</CardContent>
</Card>
);
}
function SSHConnectionForm({
form,
onChange,
}: {
form: SSHExecutorConfig;
onChange: <K extends keyof SSHExecutorConfig>(key: K, value: SSHExecutorConfig[K]) => void;
}) {
return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<Field id="ssh-name" label="Name">
<Input
id="ssh-name"
value={form.name}
onChange={(e) => onChange("name", e.target.value)}
placeholder="My VPS"
/>
</Field>
<Field
id="ssh-host-alias"
label="Host alias from ~/.ssh/config (optional)"
hint="If set, inherits HostName / Port / User / IdentityFile / ProxyJump from your config."
>
<Input
id="ssh-host-alias"
value={form.host_alias ?? ""}
onChange={(e) => onChange("host_alias", e.target.value)}
placeholder="prod"
/>
</Field>
<Field id="ssh-host" label="Host">
<Input
id="ssh-host"
value={form.host ?? ""}
onChange={(e) => onChange("host", e.target.value)}
placeholder="dev.example.com"
/>
</Field>
<Field id="ssh-port" label="Port">
<Input
id="ssh-port"
type="number"
value={form.port ?? 22}
onChange={(e) => onChange("port", parseInt(e.target.value, 10) || 22)}
placeholder="22"
/>
</Field>
<Field id="ssh-user" label="User">
<Input
id="ssh-user"
value={form.user ?? ""}
onChange={(e) => onChange("user", e.target.value)}
placeholder="ubuntu"
/>
</Field>
<Field id="ssh-identity-source" label="Identity source">
<Select
value={form.identity_source}
onValueChange={(v) => onChange("identity_source", v as SSHIdentitySource)}
>
<SelectTrigger id="ssh-identity-source">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="agent">ssh-agent (SSH_AUTH_SOCK)</SelectItem>
<SelectItem value="file">Identity file (private key path)</SelectItem>
</SelectContent>
</Select>
</Field>
{form.identity_source === "file" && (
<Field
id="ssh-identity-file"
label="Identity file path"
hint="Passphrase-protected keys must be loaded into ssh-agent first."
>
<Input
id="ssh-identity-file"
value={form.identity_file ?? ""}
onChange={(e) => onChange("identity_file", e.target.value)}
placeholder="~/.ssh/id_ed25519"
/>
</Field>
)}
<Field
id="ssh-proxy-jump"
label="ProxyJump (optional)"
hint="Single bastion hop. Chained jumps unsupported in v1."
>
<Input
id="ssh-proxy-jump"
value={form.proxy_jump ?? ""}
onChange={(e) => onChange("proxy_jump", e.target.value)}
placeholder="bastion.example.com"
/>
</Field>
</div>
);
}
function PinnedFingerprintRow({ fingerprint }: { fingerprint: string }) {
return (
<div className="rounded-md border bg-muted/40 px-3 py-2 text-xs flex items-center gap-2">
<IconShieldLock className="h-4 w-4 shrink-0" />
<span className="text-muted-foreground">
Pinned fingerprint: <code className="font-mono">{fingerprint}</code>
</span>
</div>
);
}
function SSHConnectionActions({
testing,
saving,
canTest,
canSave,
onTest,
onSave,
}: {
testing: boolean;
saving: boolean;
canTest: boolean;
canSave: boolean;
onTest: () => void;
onSave: () => void;
}) {
return (
<div className="flex items-center gap-3">
<Button
variant="outline"
size="sm"
onClick={onTest}
disabled={!canTest}
className="cursor-pointer"
>
{testing ? (
<IconLoader2 className="mr-1.5 h-4 w-4 animate-spin" />
) : (
<IconTestPipe className="mr-1.5 h-4 w-4" />
)}
Test connection
</Button>
<Button size="sm" onClick={onSave} disabled={!canSave} className="cursor-pointer">
{saving ? <IconLoader2 className="mr-1.5 h-4 w-4 animate-spin" /> : null}
Save
</Button>
</div>
);
}
function ConnectionBadge({ fingerprint }: { fingerprint?: string }) {
if (!fingerprint) return <Badge variant="secondary">Unverified</Badge>;
return (
<Badge variant="default" className="bg-green-600">
Trusted
</Badge>
);
}
function Field({
id,
label,
hint,
children,
}: {
id: string;
label: string;
hint?: string;
children: React.ReactNode;
}) {
return (
<div className="space-y-1.5">
<Label htmlFor={id}>{label}</Label>
{children}
{hint && <p className="text-xs text-muted-foreground">{hint}</p>}
</div>
);
}
function TestResultDisplay({
result,
trust,
onTrustChange,
currentlyPinned,
}: {
result: SSHTestResult;
trust: boolean;
onTrustChange: (v: boolean) => void;
currentlyPinned?: string;
}) {
const fingerprintChanged =
!!currentlyPinned && !!result.fingerprint && currentlyPinned !== result.fingerprint;
return (
<div className="rounded-md border p-3 space-y-2">
<div className="flex items-center gap-2 text-sm font-medium">
{result.success ? (
<IconCheck className="h-4 w-4 text-green-600" />
) : (
<IconX className="h-4 w-4 text-red-600" />
)}
{result.success ? "Connection test passed" : "Connection test failed"}
<span className="text-muted-foreground font-normal">({result.total_duration_ms}ms)</span>
</div>
{result.steps.map((step: SSHTestStep) => (
<StepRow key={step.name} step={step} />
))}
{result.error && !result.steps.some((s) => s.error) && (
<p className="text-sm text-red-600">{result.error}</p>
)}
{result.success && result.fingerprint && (
<div className="mt-3 space-y-2">
<div className="text-xs">
<span className="text-muted-foreground">Host fingerprint observed: </span>
<code className="font-mono">{result.fingerprint}</code>
</div>
{fingerprintChanged && (
<p className="text-xs text-amber-600">
Warning: this fingerprint differs from the one currently pinned (
<code className="font-mono">{currentlyPinned}</code>). Trusting it overwrites the
pinned key. If you didn’t expect a host re-key, stop here.
</p>
)}
<label className="flex items-start gap-2 text-sm cursor-pointer">
<input
type="checkbox"
checked={trust}
onChange={(e) => onTrustChange(e.target.checked)}
className="mt-0.5 cursor-pointer"
/>
<span>
<strong>Trust this host.</strong> I’ve verified the fingerprint above matches my
server. Future connections that report a different fingerprint will be refused.
</span>
</label>
</div>
)}
</div>
);
}
function StepRow({ step }: { step: SSHTestStep }) {
return (
<div className="flex items-start gap-2 text-sm pl-2">
{step.success ? (
<IconCheck className="h-3 w-3 text-green-600 shrink-0 mt-1" />
) : (
<IconX className="h-3 w-3 text-red-600 shrink-0 mt-1" />
)}
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<span>{step.name}</span>
<span className="text-muted-foreground text-xs">({step.duration_ms}ms)</span>
</div>
{step.output && (
<p className="text-xs text-muted-foreground truncate font-mono">{step.output}</p>
)}
{step.error && <p className="text-xs text-red-600 truncate">{step.error}</p>}
</div>
</div>
);
}
export interface SSHSessionsCardProps {
executorId: string;
}
export function SSHSessionsCard({ executorId }: SSHSessionsCardProps) {
const [sessions, setSessions] = useState<SSHSession[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const refresh = useCallback(async () => {
setLoading(true);
setError(null);
try {
const rows = await listSSHSessions(executorId);
setSessions(rows);
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to load sessions");
} finally {
setLoading(false);
}
}, [executorId]);
useEffect(() => {
refresh();
const id = setInterval(refresh, 90_000);
return () => clearInterval(id);
}, [refresh]);
return (
<Card>
<CardHeader>
<div className="flex items-center justify-between">
<div>
<CardTitle>Active sessions</CardTitle>
<CardDescription>
Sessions currently running on this SSH host. Refreshes every 90 seconds.
</CardDescription>
</div>
<Button
variant="outline"
size="sm"
onClick={refresh}
disabled={loading}
className="cursor-pointer"
>
{loading ? <IconLoader2 className="mr-1.5 h-4 w-4 animate-spin" /> : null}
Refresh
</Button>
</div>
</CardHeader>
<CardContent>
{error && <p className="text-sm text-red-600">{error}</p>}
{!error && sessions.length === 0 && !loading && (
<p className="text-sm text-muted-foreground">No active sessions.</p>
)}
{sessions.length > 0 && (
<Table>
<TableHeader>
<TableRow>
<TableHead>Task</TableHead>
<TableHead>Session</TableHead>
<TableHead>Host</TableHead>
<TableHead>Remote port</TableHead>
<TableHead>Local fwd</TableHead>
<TableHead>Uptime</TableHead>
<TableHead>Status</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{sessions.map((s) => (
<TableRow key={s.session_id}>
<TableCell className="font-mono text-xs">{s.task_id.slice(0, 8)}</TableCell>
<TableCell className="font-mono text-xs">{s.session_id.slice(0, 8)}</TableCell>
<TableCell className="font-mono text-xs">
{s.user ? `${s.user}@${s.host}` : s.host}
</TableCell>
<TableCell className="font-mono text-xs">
{s.remote_agentctl_port ?? "—"}
</TableCell>
<TableCell className="font-mono text-xs">{s.local_forward_port ?? "—"}</TableCell>
<TableCell className="text-xs">{formatUptime(s.uptime_seconds)}</TableCell>
<TableCell>
<Badge variant={s.status === "running" ? "default" : "secondary"}>
{s.status}
</Badge>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
);
}
function formatUptime(s: number): string {
if (s < 60) return `${s}s`;
if (s < 3600) return `${Math.floor(s / 60)}m`;
if (s < 86400) return `${Math.floor(s / 3600)}h`;
return `${Math.floor(s / 86400)}d`;
}
// SSH executor settings UI. The two cards live in their own files so each
// stays under the component size limits; import from here to keep call sites
// short.
export { SSHConnectionCard } from "./ssh-connection-card";
export type { SSHConnectionCardProps, SSHExecutorConfig } from "./ssh-connection-card";
export { SSHSessionsCard } from "./ssh-sessions-card";
export type { SSHSessionsCardProps } from "./ssh-sessions-card";