diff --git a/apps/web/components/task/executor-settings-button.tsx b/apps/web/components/task/executor-settings-button.tsx new file mode 100644 index 0000000000..67d893dd4b --- /dev/null +++ b/apps/web/components/task/executor-settings-button.tsx @@ -0,0 +1,267 @@ +"use client"; + +import { useCallback, useEffect, useMemo, useState } from "react"; +import { + IconBox, + IconCopy, + IconLoader, + IconRefresh, + IconTrash, +} from "@tabler/icons-react"; +import { Popover, PopoverContent, PopoverTrigger } from "@kandev/ui/popover"; +import { Button } from "@kandev/ui/button"; +import { toast } from "sonner"; + +import { + fetchTaskEnvironment, + resetTaskEnvironment, + type TaskEnvironment, +} from "@/lib/api/domains/task-environment-api"; +import { TaskResetEnvConfirmDialog } from "./task-reset-env-confirm-dialog"; + +type ExecutorSettingsButtonProps = { + taskId?: string | null; + disabled?: boolean; +}; + +export function ExecutorSettingsButton({ taskId, disabled }: ExecutorSettingsButtonProps) { + const [open, setOpen] = useState(false); + const [env, setEnv] = useState(null); + const [loading, setLoading] = useState(false); + const [resetDialogOpen, setResetDialogOpen] = useState(false); + const [isResetting, setIsResetting] = useState(false); + + const loadEnv = useCallback(async () => { + if (!taskId) { + setEnv(null); + return; + } + setLoading(true); + try { + const data = await fetchTaskEnvironment(taskId); + setEnv(data); + } catch { + // 404 when there's no environment yet — normal state, not an error + setEnv(null); + } finally { + setLoading(false); + } + }, [taskId]); + + useEffect(() => { + if (open) { + void loadEnv(); + } + }, [open, loadEnv]); + + const handleReset = useCallback( + async ({ pushBranch }: { pushBranch: boolean }) => { + if (!taskId) return; + setIsResetting(true); + try { + await resetTaskEnvironment(taskId, { push_branch: pushBranch }); + toast.success("Environment reset"); + setEnv(null); + setResetDialogOpen(false); + setOpen(false); + } catch (err) { + const msg = err instanceof Error ? err.message : "Unknown error"; + toast.error(`Reset failed: ${msg}`); + } finally { + setIsResetting(false); + } + }, + [taskId], + ); + + if (!taskId) return null; + + return ( + <> + + + + + + +
+ + +
+
+
+ + + + ); +} + +function EnvironmentInfo({ + env, + loading, +}: { + env: TaskEnvironment | null; + loading: boolean; +}) { + if (loading && !env) { + return ( +
+ +
+ ); + } + + if (!env) { + return ( +
+

No environment yet

+

+ An environment is created when you start a session on this task. +

+
+ ); + } + + return ( +
+
+
+ {formatExecutorType(env.executor_type)} + {env.status} +
+
+ +
+ ); +} + +function EnvironmentFields({ env }: { env: TaskEnvironment }) { + const fields = useMemo(() => buildFields(env), [env]); + if (fields.length === 0) { + return

No resource details available.

; + } + return ( +
+ {fields.map((f) => ( + + ))} +
+ ); +} + +function Field({ + label, + value, + copy, +}: { + label: string; + value: string; + copy?: boolean; +}) { + return ( +
+
{label}
+
+ {value} + {copy && ( + + )} +
+
+ ); +} + +type FieldRow = { label: string; value: string; copy?: boolean }; + +function buildFields(env: TaskEnvironment): FieldRow[] { + const rows: FieldRow[] = []; + + if (env.worktree_path) { + rows.push({ label: "Worktree", value: env.worktree_path, copy: true }); + } + if (env.worktree_branch) { + rows.push({ label: "Branch", value: env.worktree_branch, copy: true }); + } + + if (env.container_id) { + rows.push({ label: "Container", value: env.container_id.slice(0, 12), copy: true }); + rows.push({ + label: "Shell", + value: `docker exec -it ${env.container_id.slice(0, 12)} bash`, + copy: true, + }); + } + + if (env.sandbox_id) { + rows.push({ label: "Sprite", value: env.sandbox_id, copy: true }); + } + + if (env.agent_execution_id) { + rows.push({ label: "Execution", value: env.agent_execution_id, copy: true }); + } + + rows.push({ label: "Environment ID", value: env.id, copy: true }); + + return rows; +} + +function formatExecutorType(type: string): string { + switch (type) { + case "local_pc": + case "worktree": + return "Local (worktree)"; + case "local_docker": + return "Local Docker"; + case "sprites": + return "Sprites sandbox"; + case "remote_docker": + return "Remote Docker"; + default: + return type || "Unknown executor"; + } +} diff --git a/apps/web/components/task/task-reset-env-confirm-dialog.tsx b/apps/web/components/task/task-reset-env-confirm-dialog.tsx new file mode 100644 index 0000000000..e9ebb86fa2 --- /dev/null +++ b/apps/web/components/task/task-reset-env-confirm-dialog.tsx @@ -0,0 +1,107 @@ +"use client"; + +import { useState } from "react"; +import { IconLoader } from "@tabler/icons-react"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from "@kandev/ui/alert-dialog"; +import { Checkbox } from "@kandev/ui/checkbox"; + +export type TaskResetEnvConfirmDialogProps = { + open: boolean; + onOpenChange: (open: boolean) => void; + hasWorktreePath: boolean; + isResetting?: boolean; + onConfirm: (opts: { pushBranch: boolean }) => void; +}; + +export function TaskResetEnvConfirmDialog({ + open, + onOpenChange, + hasWorktreePath, + isResetting, + onConfirm, +}: TaskResetEnvConfirmDialogProps) { + const [acknowledged, setAcknowledged] = useState(false); + const [pushBranch, setPushBranch] = useState(false); + + const handleOpenChange = (next: boolean) => { + if (!next) { + // reset local state when closing + setAcknowledged(false); + setPushBranch(false); + } + onOpenChange(next); + }; + + return ( + + e.stopPropagation()}> + + Reset environment? + +
+

+ This tears down the current container, sandbox, and/or worktree for this task so + the next session starts with a fresh environment. +

+

+ Any uncommitted or unpushed changes in the workspace will be lost. +

+
+
+
+ +
+ {hasWorktreePath && ( + + )} + + +
+ + + Cancel + { + if (isResetting || !acknowledged) return; + onConfirm({ pushBranch }); + }} + > + {isResetting ? : null} + Reset environment + + +
+
+ ); +} diff --git a/apps/web/components/task/task-top-bar.tsx b/apps/web/components/task/task-top-bar.tsx index edb9e1a476..4f79ea3ec5 100644 --- a/apps/web/components/task/task-top-bar.tsx +++ b/apps/web/components/task/task-top-bar.tsx @@ -32,6 +32,7 @@ import { LinearLinkButton } from "@/components/linear/linear-link-button"; import { useJiraAvailable } from "@/hooks/domains/jira/use-jira-availability"; import { useLinearAvailable } from "@/hooks/domains/linear/use-linear-availability"; import { PortForwardButton } from "@/components/task/port-forward-dialog"; +import { ExecutorSettingsButton } from "@/components/task/executor-settings-button"; import { WorkflowStepper, type WorkflowStepperStep } from "@/components/task/workflow-stepper"; import { RemoteCloudTooltip } from "@/components/task/remote-cloud-tooltip"; import { QuickChatButton } from "@/components/task/quick-chat-button"; @@ -344,6 +345,7 @@ function AttentionStatusGroup({ {!isArchived && ( <> + { + test("task environment API response includes executor_type field", async ({ + apiClient, + seedData, + }) => { + // Create task with session, wait for completion + const task = await apiClient.createTaskWithAgent( + seedData.workspaceId, + "Env Fields Task", + seedData.agentProfileId, + { + description: "/e2e:simple-message", + workflow_id: seedData.workflowId, + workflow_step_id: seedData.startStepId, + repository_ids: [seedData.repositoryId], + }, + ); + + await expect + .poll( + async () => { + const { sessions } = await apiClient.listTaskSessions(task.id); + return DONE_STATES.includes(sessions[0]?.state ?? ""); + }, + { timeout: 30_000, message: "Waiting for session to finish" }, + ) + .toBe(true); + + // Get environment and verify response shape + const env = await apiClient.getTaskEnvironment(task.id); + expect(env).not.toBeNull(); + expect(env!.task_id).toBe(task.id); + expect(env!.status).toBe("ready"); + // executor_type should be present (standalone for mock agent) + expect(env!.executor_type).toBeDefined(); + }); + + test("second session reuses same task environment by default", async ({ + testPage, + apiClient, + seedData, + }) => { + test.setTimeout(120_000); + + // 1. Create task with first session + const task = await apiClient.createTaskWithAgent( + seedData.workspaceId, + "Reuse Env Task", + seedData.agentProfileId, + { + description: "/e2e:simple-message", + workflow_id: seedData.workflowId, + workflow_step_id: seedData.startStepId, + repository_ids: [seedData.repositoryId], + }, + ); + + // 2. Wait for first session to complete + await expect + .poll( + async () => { + const { sessions } = await apiClient.listTaskSessions(task.id); + return DONE_STATES.includes(sessions[0]?.state ?? ""); + }, + { timeout: 30_000, message: "Waiting for first session to finish" }, + ) + .toBe(true); + + // 3. Capture environment + const envBefore = await apiClient.getTaskEnvironment(task.id); + expect(envBefore).not.toBeNull(); + + // 4. Navigate to task and create second session via dialog + const kanban = new KanbanPage(testPage); + await kanban.goto(); + + const card = kanban.taskCardByTitle("Reuse Env Task"); + await expect(card).toBeVisible({ timeout: 10_000 }); + await card.click(); + await expect(testPage).toHaveURL(/\/t\//, { timeout: 15_000 }); + + const session = new SessionPage(testPage); + await session.waitForLoad(); + await expect(session.chat.getByText("simple mock response", { exact: false })).toBeVisible({ + timeout: 15_000, + }); + + // 5. Open new session dialog and submit + await session.openNewSessionDialog(); + await expect(session.newSessionDialog()).toBeVisible({ timeout: 5_000 }); + await session.newSessionPromptInput().fill("/e2e:simple-message"); + await session.newSessionStartButton().click(); + await expect(session.newSessionDialog()).not.toBeVisible({ timeout: 10_000 }); + + // 6. Wait for second session + await expect + .poll( + async () => { + const { sessions } = await apiClient.listTaskSessions(task.id); + return sessions.length; + }, + { timeout: 30_000, message: "Waiting for second session to appear" }, + ) + .toBe(2); + + // 7. Verify same environment ID (default reuse behavior) + const envAfter = await apiClient.getTaskEnvironment(task.id); + expect(envAfter).not.toBeNull(); + expect(envAfter!.id).toBe(envBefore!.id); + + // 8. Both sessions reference the same environment + const { sessions: allSessions } = await apiClient.listTaskSessions(task.id); + for (const s of allSessions) { + expect(s.task_environment_id).toBe(envBefore!.id); + } + }); + + test("reset environment from executor settings popover removes the environment", async ({ + testPage, + apiClient, + seedData, + }) => { + test.setTimeout(90_000); + + const task = await apiClient.createTaskWithAgent( + seedData.workspaceId, + "Reset Env Task", + seedData.agentProfileId, + { + description: "/e2e:simple-message", + workflow_id: seedData.workflowId, + workflow_step_id: seedData.startStepId, + repository_ids: [seedData.repositoryId], + }, + ); + + await expect + .poll( + async () => { + const { sessions } = await apiClient.listTaskSessions(task.id); + return DONE_STATES.includes(sessions[0]?.state ?? ""); + }, + { timeout: 30_000, message: "Waiting for session to finish" }, + ) + .toBe(true); + + const envBefore = await apiClient.getTaskEnvironment(task.id); + expect(envBefore).not.toBeNull(); + + // Navigate to task + const kanban = new KanbanPage(testPage); + await kanban.goto(); + + const card = kanban.taskCardByTitle("Reset Env Task"); + await expect(card).toBeVisible({ timeout: 10_000 }); + await card.click(); + await expect(testPage).toHaveURL(/\/t\//, { timeout: 15_000 }); + + const session = new SessionPage(testPage); + await session.waitForLoad(); + + // Open popover + await testPage.getByTestId("executor-settings-button").click(); + const popover = testPage.getByTestId("executor-settings-popover"); + await expect(popover).toBeVisible({ timeout: 5_000 }); + + // Click Reset → confirmation dialog + await testPage.getByTestId("executor-settings-reset").click(); + const ackCheckbox = testPage.getByRole("checkbox").last(); + await ackCheckbox.click(); + await testPage.getByTestId("reset-env-confirm").click(); + + // Poll environment until gone + await expect + .poll( + async () => { + const env = await apiClient.getTaskEnvironment(task.id); + return env; + }, + { timeout: 15_000, message: "Waiting for environment to be reset" }, + ) + .toBeNull(); + }); +}); diff --git a/apps/web/lib/api/domains/task-environment-api.ts b/apps/web/lib/api/domains/task-environment-api.ts new file mode 100644 index 0000000000..2431e31c66 --- /dev/null +++ b/apps/web/lib/api/domains/task-environment-api.ts @@ -0,0 +1,44 @@ +import { fetchJson, type ApiRequestOptions } from "../client"; + +export type TaskEnvironment = { + id: string; + task_id: string; + repository_id: string; + executor_type: string; + executor_id: string; + executor_profile_id: string; + agent_execution_id: string; + control_port: number; + status: string; + worktree_id?: string; + worktree_path?: string; + worktree_branch?: string; + workspace_path?: string; + container_id?: string; + sandbox_id?: string; + created_at: string; + updated_at?: string; +}; + +export type ResetTaskEnvironmentPayload = { + push_branch?: boolean; +}; + +export async function fetchTaskEnvironment(taskId: string, options?: ApiRequestOptions) { + return fetchJson(`/api/v1/tasks/${taskId}/environment`, options); +} + +export async function resetTaskEnvironment( + taskId: string, + payload: ResetTaskEnvironmentPayload, + options?: ApiRequestOptions, +) { + return fetchJson<{ success: boolean }>(`/api/v1/tasks/${taskId}/environment/reset`, { + ...options, + init: { + method: "POST", + body: JSON.stringify(payload), + ...(options?.init ?? {}), + }, + }); +}