diff --git a/apps/desktop/src/state/loops.test.ts b/apps/desktop/src/state/loops.test.ts index e8bcb610d..75196882e 100644 --- a/apps/desktop/src/state/loops.test.ts +++ b/apps/desktop/src/state/loops.test.ts @@ -574,4 +574,22 @@ describe("paired machine loops", () => { expect(loopThreadNames(snapshot.loops)).toEqual({ "gw-vps/t-9": "Cleanup VPS" }); expect(loopIdsByThread(snapshot.loops)).toEqual({ "gw-vps/t-9": "gw-vps/loop-vps" }); }); + + test("resolves hub-dispatched run threads when a resolver is provided", () => { + const dispatched = [ + loop({ + id: "loop-hub", + host: "gw-hub", + name: "Nightly macOS", + machine: "Main Mac", + runs: [run("r1", "succeeded", 1, { machine: "Main Mac", threadId: "t-mac" })], + }), + ]; + const resolve = (_loop: { host: string }, r: { machine?: string; threadId?: string }) => + r.machine === "Main Mac" && r.threadId ? `mac-1/${r.threadId}` : undefined; + expect(loopRunThreads(dispatched, resolve)).toEqual({ "mac-1/t-mac": true }); + expect(loopThreadNames(dispatched, resolve)).toEqual({ "mac-1/t-mac": "Nightly macOS" }); + expect(loopIdsByThread(dispatched, resolve)).toEqual({ "mac-1/t-mac": "gw-hub/loop-hub" }); + expect(isJobRunThread({ id: "mac-1/t-mac" }, loopRunThreads(dispatched, resolve))).toBe(true); + }); }); diff --git a/apps/desktop/src/state/loops.ts b/apps/desktop/src/state/loops.ts index 0b21be5de..adba93221 100644 --- a/apps/desktop/src/state/loops.ts +++ b/apps/desktop/src/state/loops.ts @@ -888,16 +888,30 @@ export const useLoopThreads = create<{ const loopThreadKey = (loop: Pick, id: string) => threadKey(loop.host ?? LOCAL_HOST, id); +/** Resolves a run's thread key across local, paired, or hub-dispatched targets. */ +export type RunThreadResolver = ( + loop: Pick, + run: Pick, +) => string | undefined; + +const defaultRunThread: RunThreadResolver = (loop, run) => + run.threadId && !run.machine ? loopThreadKey(loop, run.threadId) : undefined; + /** * The threads fresh runs started, by id. A job that sends every run to one thread leaves that * thread out: the user picked it, and it stays in the list. */ -export function loopRunThreads(loops: readonly Loop[]): Record { +export function loopRunThreads( + loops: readonly Loop[], + resolveRunThread: RunThreadResolver = defaultRunThread, +): Record { const runs: Record = {}; for (const loop of loops) { - for (const run of loop.runs) - if (run.threadId && !run.machine && run.threadId !== loop.threadId) - runs[loopThreadKey(loop, run.threadId)] = true; + for (const run of loop.runs) { + if (!run.threadId || run.threadId === loop.threadId) continue; + const key = resolveRunThread(loop, run); + if (key) runs[key] = true; + } } return runs; } @@ -914,41 +928,57 @@ export function isJobRunThread( } /** Every thread the loops' recorded runs went to, and the loop's name. */ -export function loopThreadNames(loops: readonly Loop[]): Record { +export function loopThreadNames( + loops: readonly Loop[], + resolveRunThread: RunThreadResolver = defaultRunThread, +): Record { const byThread: Record = {}; for (const loop of loops) { if (loop.threadId && !loop.machine) byThread[loopThreadKey(loop, loop.threadId)] = loop.name; - for (const run of loop.runs) - if (run.threadId && !run.machine) byThread[loopThreadKey(loop, run.threadId)] = loop.name; + for (const run of loop.runs) { + const key = resolveRunThread(loop, run); + if (key) byThread[key] = loop.name; + } } return byThread; } /** Every thread the loops' recorded runs went to, and the loop's id (prefixed with host when remote). */ -export function loopIdsByThread(loops: readonly Loop[]): Record { +export function loopIdsByThread( + loops: readonly Loop[], + resolveRunThread: RunThreadResolver = defaultRunThread, +): Record { const byThread: Record = {}; for (const loop of loops) { const id = loop.host ? `${loop.host}/${loop.id}` : loop.id; if (loop.threadId && !loop.machine) byThread[loopThreadKey(loop, loop.threadId)] = id; - for (const run of loop.runs) - if (run.threadId && !run.machine) byThread[loopThreadKey(loop, run.threadId)] = id; + for (const run of loop.runs) { + const key = resolveRunThread(loop, run); + if (key) byThread[key] = id; + } } return byThread; } /** Keep the sidebar's Loop badges in step with a list the page or a refresh read. */ -export function rememberLoops(snapshot: LoopsSnapshot | readonly Loop[]): void { +export function rememberLoops( + snapshot: LoopsSnapshot | readonly Loop[], + resolveRunThread: RunThreadResolver = defaultRunThread, +): void { const loops = "loops" in snapshot ? snapshot.loops : snapshot; useLoopThreads.setState({ - byThread: loopThreadNames(loops), - runs: loopRunThreads(loops), - loopOf: loopIdsByThread(loops), + byThread: loopThreadNames(loops, resolveRunThread), + runs: loopRunThreads(loops, resolveRunThread), + loopOf: loopIdsByThread(loops, resolveRunThread), }); } /** Read the loops on this computer and every connected paired machine for the sidebar badges. */ -export async function refreshLoopThreads(): Promise { - const hosts = [LOCAL_HOST, ...useApp.getState().connectedHosts.filter(Boolean)]; +export async function refreshLoopThreads( + resolveRunThread?: RunThreadResolver, + remoteHosts?: readonly string[], +): Promise { + const hosts = [LOCAL_HOST, ...(remoteHosts ?? useApp.getState().connectedHosts).filter(Boolean)]; const settled = await Promise.allSettled(hosts.map((host) => fetchLoopsOn(host))); const loops: Loop[] = []; let anySucceeded = false; @@ -958,5 +988,5 @@ export async function refreshLoopThreads(): Promise { loops.push(...result.value.loops); } } - if (anySucceeded) rememberLoops(loops); + if (anySucceeded) rememberLoops(loops, resolveRunThread); } diff --git a/apps/desktop/src/ui/JobCard.tsx b/apps/desktop/src/ui/JobCard.tsx index ef0fb0614..6979710d1 100644 --- a/apps/desktop/src/ui/JobCard.tsx +++ b/apps/desktop/src/ui/JobCard.tsx @@ -1,7 +1,7 @@ /** - * One job on the Jobs page: everything worth a glance without opening it (its agent, what the last - * run said, the schedule, recent runs and the next one). The card opens the job's dialog, where it - * is edited and its runs are listed; Run now and the menu stay on the card. + * One job on the Jobs page: everything worth a glance without opening it (its agent, last run or + * prompt preview, project, machine, schedule, next run, and recent runs). The card opens the job + * overview dialog; Run now and the menu stay on the card. */ import { DropdownMenu } from "radix-ui"; @@ -23,11 +23,14 @@ import { openSettings } from "../state/store"; import { selectThread } from "../state/threadOpen"; import { ArrowUpRight, + Clock, Copy, Ellipsis, + Folder, Pause, Pencil, Play, + ShieldAlert, SquareStop, Terminal, Trash2, @@ -90,7 +93,7 @@ function StatusChip({ loop, waiting }: { loop: Loop; waiting: boolean }) { ); if (state === "failing") - return Paused; + return Needs attention; if (state === "paused") return Paused; if (waiting) return ( @@ -164,6 +167,8 @@ export function JobCard({ const next = !loop.paused && loop.nextDueAt !== undefined ? loop.nextDueAt : undefined; const waiting = state === "active" && next === undefined && listening(loop); const quiet = latest?.state === "succeeded" && (latest.quiet || latest.silent); + const hasRuns = loop.runs.length > 0; + const promptPreview = loop.prompt.trim(); const togglePause = () => act(loop.paused ? "Resumed" : "Paused", () => setLoopPaused(loop.id, !loop.paused, loop.host)); @@ -171,16 +176,16 @@ export function JobCard({
  • @@ -245,14 +250,26 @@ export function JobCard({
    - {agent && ( - - {agent} - + {(agent || !loop.readOnly) && ( +
    + {agent && ( + + {agent} + + )} + {!loop.readOnly && ( + + + + Full access + + + )} +
    )} {reason ? ( -
    +

    {reason}

    @@ -266,11 +283,11 @@ export function JobCard({ )}
    - ) : ( + ) : hasRuns ? (

    {result.text}

    + ) : ( +

    + {promptPreview || "No prompt set"} +

    )} -
    - - {projectName(loop.workspace)} - {(machineLabel || loop.machine) && ` on ${machineLabel || loop.machine}`} ·{" "} - {scheduleInWords(loop.schedule)} - - {loop.script && ( - - - - - - )} - {loop.webhook && loop.schedule.kind !== "manual" && ( - - - - - - )} - {next !== undefined ? ( - - ) : ( - {nextRunText(loop, now)} - )} -
    -
    - - {loop.runs.length > 0 && ( - - {loop.runs.length} {loop.runs.length === 1 ? "run" : "runs"} +
    +
    + + + + {projectName(loop.workspace)} + {(machineLabel || loop.machine) && ` · ${machineLabel || loop.machine}`} + + {next !== undefined ? ( + + ) : ( + {nextRunText(loop, now)} )} - - - - Run history - +
    +
    + + + {scheduleInWords(loop.schedule)} + {loop.script && ( + + + + + + )} + {loop.webhook && loop.schedule.kind !== "manual" && ( + + + + + + )} + + + + {hasRuns && ( + + {loop.runs.length} {loop.runs.length === 1 ? "run" : "runs"} + + )} + +
  • ); diff --git a/apps/desktop/src/ui/JobDetails.tsx b/apps/desktop/src/ui/JobDetails.tsx index 136197ba0..25546ecd2 100644 --- a/apps/desktop/src/ui/JobDetails.tsx +++ b/apps/desktop/src/ui/JobDetails.tsx @@ -1,35 +1,146 @@ +/** + * Job overview dialog: a roomy two-column summary of one scheduled job — its live status, quick + * actions (run, stop, pause/resume, edit, duplicate, delete), schedule, configuration, prompt, + * pre-check script, health metrics, and full clickable run history. + */ + import { Dialog } from "radix-ui"; -import { useRef } from "react"; +import { useRef, useState } from "react"; import { projectName } from "../acp/types"; import { runThreadKey } from "../state/fleetLoops"; -import { type Loop, nextRunText, scheduleInWords } from "../state/loops"; +import { + cancelLoopRun, + type Loop, + loopState, + nextRunText, + pauseReason, + runLoopNow, + scheduleInWords, + setLoopPaused, +} from "../state/loops"; import { selfName, useLocalIdentity, useMachines } from "../state/machines"; -import { FULL_RUN_FORMAT, isFailedRun, RunList } from "./JobRuns"; -import { Pill } from "./primitives"; +import { openSettings } from "../state/store"; +import { selectThread } from "../state/threadOpen"; +import { + ArrowUpRight, + Clock, + Copy, + Folder, + History, + Pause, + Pencil, + Play, + Server, + Shield, + ShieldAlert, + SquareStop, + Terminal, + Trash2, + Webhook, + X, +} from "./icons"; +import { averageRunDuration, FULL_RUN_FORMAT, isFailedRun, RunList, RunTimelineStrip } from "./JobRuns"; +import { CopyButton } from "./Markdown"; +import { Callout, cx, Pill, Segments, Spinner, Tooltip } from "./primitives"; + +type RunFilter = "all" | "failed"; + +function OverviewStatusChip({ loop }: { loop: Loop }) { + const state = loopState(loop); + const base = "inline-flex shrink-0 items-center gap-1 rounded-full px-2 py-0.5 font-medium text-caption"; + if (state === "running") { + return ( + + + Running + + ); + } + if (state === "failing") { + return Needs attention; + } + if (state === "paused") { + return Paused; + } + if (loop.schedule.kind === "manual" && loop.webhook) { + return ( + + + Webhook + + ); + } + return ( + + + Active + + ); +} + +function ConfigRow({ label, children }: { label: string; children: React.ReactNode }) { + return ( +
    +
    {label}
    +
    {children}
    +
    + ); +} export function JobDetails({ loop, now, + agent, machine, onClose, onEdit, + onDuplicate, + onDelete, + act, }: { loop: Loop; now: number; + agent: string; machine: string; onClose: () => void; onEdit: () => void; + onDuplicate: () => void; + onDelete: () => void; + act: (what: string, run: () => Promise) => void; }) { const returnFocus = useRef(null); + const [runFilter, setRunFilter] = useState("all"); const machines = useMachines((s) => s.machines); const local = useLocalIdentity(); const localName = local.displayName ?? selfName(); + + const state = loopState(loop); + const running = state === "running"; + const reason = pauseReason(loop); + const latest = loop.runs[0]; + const latestThread = running ? undefined : runThreadKey(loop, latest, machines, localName); const next = !loop.paused && loop.schedule.kind !== "manual" ? loop.nextDueAt : undefined; - const stats = [ - { label: "Recorded runs", value: loop.runs.length }, - { label: "Succeeded", value: loop.runs.filter((run) => run.state === "succeeded").length }, - { label: "Failed", value: loop.runs.filter(isFailedRun).length }, - ]; + const timezone = "timezone" in loop.schedule ? loop.schedule.timezone : undefined; + + const succeededCount = loop.runs.filter((run) => run.state === "succeeded").length; + const failedCount = loop.runs.filter(isFailedRun).length; + const avgDuration = averageRunDuration(loop.runs); + + const showFilter = failedCount > 0 && loop.runs.length > failedCount; + const effectiveFilter: RunFilter = showFilter ? runFilter : "all"; + const visibleRuns = effectiveFilter === "failed" ? loop.runs.filter(isFailedRun) : loop.runs; + + const togglePause = () => + act(loop.paused ? "Resumed" : "Paused", () => setLoopPaused(loop.id, !loop.paused, loop.host)); + + const triggerLabel = + loop.schedule.kind === "manual" + ? loop.webhook + ? "Webhook or manual" + : "Manual only" + : loop.webhook + ? "Schedule + webhook" + : "Schedule"; return ( !open && onClose()}> @@ -45,36 +156,320 @@ export function JobDetails({ event.preventDefault(); if (document.activeElement === document.body) returnFocus.current?.focus(); }} - className="elevated pointer-events-auto flex max-h-full w-full max-w-xl flex-col overflow-hidden" + className="elevated pointer-events-auto flex max-h-[min(780px,calc(100vh-48px))] w-[min(900px,calc(100vw-48px))] flex-col overflow-hidden" > -
    - {loop.name} - - {projectName(loop.workspace)} · {machine} · {scheduleInWords(loop.schedule)} - -

    - {next === undefined ? nextRunText(loop, now) : `Next run ${FULL_RUN_FORMAT.format(next)}`} -

    +
    +
    +
    +
    + + {loop.name} + + +
    + + + + + {projectName(loop.workspace) || "No project"} + + + + + {machine} + + + + + {scheduleInWords(loop.schedule)} + {timezone ? ` (${timezone})` : ""} + + + +
    + + + +
    + +
    + {running ? ( + act("Stopped run", () => cancelLoopRun(loop.id, loop.host))} + > + + Stop run + + ) : ( + act("Started a run", () => runLoopNow(loop.id, loop.host))} + > + + Run now + + )} + {(!reason || latest?.signedOut) && ( + + {loop.paused ? : } + {loop.paused ? "Resume" : "Pause"} + + )} + + + Edit + + + + Duplicate + + {latestThread && ( + { + onClose(); + selectThread(latestThread); + }} + > + + Open latest run + + )} +
    + + {reason && ( +
    + { + onClose(); + openSettings("agents"); + }} + > + Sign in + + ) : ( + + + Resume + + ) + } + > + {reason} + +
    + )}
    -
    -
    - {stats.map(({ label, value }) => ( -
    -
    {label}
    -
    {value}
    + +
    +
    +
    +
    + Next run + {loop.webhook && ( + + + Webhook + + )} +
    +

    + {running + ? "Running now" + : next !== undefined + ? FULL_RUN_FORMAT.format(next) + : nextRunText(loop, now)} +

    +

    + {running + ? "A run is in progress." + : next !== undefined + ? `Starts ${nextRunText(loop, now)} · ${scheduleInWords(loop.schedule)}` + : scheduleInWords(loop.schedule)} +

    +
    + +
    +
    +

    Prompt

    + {loop.prompt && }
    - ))} -
    -

    Run history

    - runThreadKey(loop, run, machines, localName)} - onOpenThread={onClose} - /> +
    + {loop.prompt || No prompt configured.} +
    + + + {loop.script && ( +
    +
    + + + Pre-check script + + +
    +
    +                      {loop.script}
    +                    
    +
    + )} + +
    +
    + + {agent || "Default harness"} + + + + + {loop.workspace || "—"} + + + + {machine} + + {loop.readOnly ? ( + + + Read-only + + ) : ( + + + Full access + + )} + + + {loop.threadId ? "Existing thread" : "New thread each run"} + + {triggerLabel} +
    +
    +
    + +
    +
    +
    +
    +
    Runs
    +
    {loop.runs.length}
    +
    +
    +
    Succeeded
    +
    0 ? "text-success" : "text-fg", + )} + > + {succeededCount} +
    +
    +
    +
    Failed
    +
    0 ? "text-danger" : "text-fg", + )} + > + {failedCount} +
    +
    +
    +
    Avg time
    +
    {avgDuration ?? "—"}
    +
    +
    + {loop.runs.length > 0 && ( +
    + +
    + )} +
    + +
    +
    +

    + Run history + {loop.runs.length > 0 && ( + + {visibleRuns.length} + + )} +

    + {showFilter && ( + + )} +
    + {visibleRuns.length > 0 ? ( + runThreadKey(loop, run, machines, localName)} + onOpenThread={onClose} + /> + ) : ( +
    + + + +

    No runs recorded yet

    +

    + {next !== undefined + ? `Scheduled for ${FULL_RUN_FORMAT.format(next)}. Use Run now to try it right away.` + : "Use Run now above whenever you want to run this job."} +

    + {!running && ( +
    + act("Started a run", () => runLoopNow(loop.id, loop.host))} + > + + Run now + +
    + )} +
    + )} +
    +
    -