From 75d9644d44ba998a32ed14c072e883a75145ab72 Mon Sep 17 00:00:00 2001 From: soryu Date: Fri, 16 Jan 2026 19:50:27 +0000 Subject: Add autopilot panel and retry system --- .../src/components/contracts/AutopilotPanel.tsx | 208 +++++++++++++++++++++ .../src/components/contracts/ContractDetail.tsx | 7 + makima/frontend/src/lib/api.ts | 114 +++++++++++ 3 files changed, 329 insertions(+) create mode 100644 makima/frontend/src/components/contracts/AutopilotPanel.tsx (limited to 'makima/frontend/src') diff --git a/makima/frontend/src/components/contracts/AutopilotPanel.tsx b/makima/frontend/src/components/contracts/AutopilotPanel.tsx new file mode 100644 index 0000000..a8a8e2e --- /dev/null +++ b/makima/frontend/src/components/contracts/AutopilotPanel.tsx @@ -0,0 +1,208 @@ +import { useState, useCallback } from "react"; +import type { ContractWithRelations } from "../../lib/api"; +import { + getSupervisorStatus, + startSupervisor, + stopSupervisor, + resumeSupervisor, + type SupervisorStatus, +} from "../../lib/api"; + +interface AutopilotPanelProps { + contract: ContractWithRelations; + onUpdate: () => void; +} + +const statusConfig: Record< + SupervisorStatus["status"], + { label: string; color: string; bgColor: string } +> = { + not_configured: { + label: "Not Configured", + color: "text-[#555]", + bgColor: "bg-[#555]/10", + }, + pending: { + label: "Ready", + color: "text-yellow-400", + bgColor: "bg-yellow-400/10", + }, + starting: { + label: "Starting...", + color: "text-blue-400", + bgColor: "bg-blue-400/10", + }, + running: { + label: "Running", + color: "text-green-400", + bgColor: "bg-green-400/10", + }, + paused: { + label: "Paused", + color: "text-orange-400", + bgColor: "bg-orange-400/10", + }, + done: { + label: "Completed", + color: "text-blue-400", + bgColor: "bg-blue-400/10", + }, + failed: { + label: "Failed", + color: "text-red-400", + bgColor: "bg-red-400/10", + }, +}; + +export function AutopilotPanel({ contract, onUpdate }: AutopilotPanelProps) { + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const supervisorStatus = getSupervisorStatus(contract); + const config = statusConfig[supervisorStatus.status]; + + const handleStart = useCallback(async () => { + if (!supervisorStatus.supervisorTaskId) return; + + setLoading(true); + setError(null); + + try { + await startSupervisor(supervisorStatus.supervisorTaskId); + onUpdate(); + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to start autopilot"); + } finally { + setLoading(false); + } + }, [supervisorStatus.supervisorTaskId, onUpdate]); + + const handleStop = useCallback(async () => { + if (!supervisorStatus.supervisorTaskId) return; + + setLoading(true); + setError(null); + + try { + await stopSupervisor(supervisorStatus.supervisorTaskId); + onUpdate(); + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to stop autopilot"); + } finally { + setLoading(false); + } + }, [supervisorStatus.supervisorTaskId, onUpdate]); + + const handleResume = useCallback(async () => { + setLoading(true); + setError(null); + + try { + await resumeSupervisor(contract.id, { resumeMode: "continue" }); + // After resuming, we need to start the task + if (supervisorStatus.supervisorTaskId) { + await startSupervisor(supervisorStatus.supervisorTaskId); + } + onUpdate(); + } catch (e) { + setError(e instanceof Error ? e.message : "Failed to resume autopilot"); + } finally { + setLoading(false); + } + }, [contract.id, supervisorStatus.supervisorTaskId, onUpdate]); + + // Don't show panel for task-type contracts (they don't have supervisors) + if (contract.contractType === "task") { + return null; + } + + return ( +
+
+

+ Autopilot Mode +

+
+ {config.label} +
+
+ +

+ {supervisorStatus.status === "not_configured" ? ( + "This contract does not have an autopilot supervisor configured." + ) : supervisorStatus.status === "running" ? ( + "Autopilot is actively working on this contract, spawning tasks and managing progress." + ) : supervisorStatus.status === "pending" ? ( + "Autopilot is ready to start. Click 'Enable' to begin autonomous work." + ) : supervisorStatus.status === "paused" ? ( + "Autopilot is paused. Click 'Resume' to continue work." + ) : supervisorStatus.status === "failed" ? ( + "Autopilot encountered an error. You can resume to retry." + ) : supervisorStatus.status === "done" ? ( + "Autopilot has completed its work on this contract." + ) : ( + "Autopilot is initializing..." + )} +

+ + {error && ( +
+ {error} +
+ )} + +
+ {supervisorStatus.canStart && ( + + )} + + {supervisorStatus.canResume && ( + + )} + + {supervisorStatus.canStop && ( + + )} +
+ + {/* Show running indicator when active */} + {supervisorStatus.status === "running" && ( +
+
+ + Autopilot is actively working + +
+ )} + + {supervisorStatus.status === "starting" && ( +
+
+ + Initializing autopilot... + +
+ )} +
+ ); +} diff --git a/makima/frontend/src/components/contracts/ContractDetail.tsx b/makima/frontend/src/components/contracts/ContractDetail.tsx index cf5f8f2..f93097a 100644 --- a/makima/frontend/src/components/contracts/ContractDetail.tsx +++ b/makima/frontend/src/components/contracts/ContractDetail.tsx @@ -18,6 +18,7 @@ import { PhaseHint } from "./PhaseHint"; import { RepositoryPanel } from "./RepositoryPanel"; import { ContractCliInput } from "./ContractCliInput"; import { PhaseDeliverablesPanel } from "./PhaseDeliverablesPanel"; +import { AutopilotPanel } from "./AutopilotPanel"; import { TaskTree } from "../mesh/TaskTree"; type Tab = "overview" | "repos" | "files" | "tasks"; @@ -225,6 +226,7 @@ export function ContractDetail({ onStatusChange={onStatusChange} onPhaseChange={onPhaseChange} onCreateFile={onCreateFileFromTemplate} + onRefresh={onRefresh} /> )} @@ -276,14 +278,19 @@ function OverviewTab({ onStatusChange, onPhaseChange, onCreateFile, + onRefresh, }: { contract: ContractWithRelations; onStatusChange: (status: ContractStatus) => void; onPhaseChange: (phase: ContractPhase) => void; onCreateFile?: (templateId: string, suggestedName: string) => void; + onRefresh: () => void; }) { return (
+ {/* Autopilot controls */} + + {/* Phase deliverables checklist */} { + return startTask(supervisorTaskId); +} + +/** + * Stop a contract's supervisor task (pause autopilot mode). + * This is a convenience wrapper around stopTask. + */ +export async function stopSupervisor(supervisorTaskId: string): Promise { + return stopTask(supervisorTaskId); +} + +/** Status of the supervisor/autopilot for a contract */ +export interface SupervisorStatus { + supervisorTaskId: string | null; + status: "not_configured" | "pending" | "starting" | "running" | "paused" | "done" | "failed"; + daemonId: string | null; + canStart: boolean; + canStop: boolean; + canResume: boolean; +} + +/** + * Get the supervisor status for a contract. + */ +export function getSupervisorStatus( + contract: ContractWithRelations +): SupervisorStatus { + const supervisorTaskId = contract.supervisorTaskId; + + if (!supervisorTaskId) { + return { + supervisorTaskId: null, + status: "not_configured", + daemonId: null, + canStart: false, + canStop: false, + canResume: false, + }; + } + + // Find the supervisor task in the contract's tasks + const supervisorTask = contract.tasks.find( + (t) => t.id === supervisorTaskId && t.isSupervisor + ); + + if (!supervisorTask) { + return { + supervisorTaskId, + status: "pending", + daemonId: null, + canStart: true, + canStop: false, + canResume: false, + }; + } + + // Map task status to supervisor status + let status: SupervisorStatus["status"]; + let canStart = false; + let canStop = false; + let canResume = false; + + switch (supervisorTask.status) { + case "pending": + status = "pending"; + canStart = true; + break; + case "initializing": + case "starting": + status = "starting"; + canStop = true; + break; + case "running": + status = "running"; + canStop = true; + break; + case "paused": + case "blocked": + status = "paused"; + canResume = true; + canStop = true; + break; + case "done": + case "merged": + status = "done"; + break; + case "failed": + status = "failed"; + canResume = true; + break; + default: + status = "pending"; + canStart = true; + } + + return { + supervisorTaskId, + status, + daemonId: null, // Task summary doesn't have daemon_id, would need full task + canStart, + canStop, + canResume, + }; +} -- cgit v1.2.3