Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/desktop/src/state/loops.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
64 changes: 47 additions & 17 deletions apps/desktop/src/state/loops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,16 +888,30 @@ export const useLoopThreads = create<{

const loopThreadKey = (loop: Pick<Loop, "host">, 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<Loop, "host">,
run: Pick<LoopRun, "machine" | "threadId">,
) => 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<string, true> {
export function loopRunThreads(
loops: readonly Loop[],
resolveRunThread: RunThreadResolver = defaultRunThread,
): Record<string, true> {
const runs: Record<string, true> = {};
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;
}
Expand All @@ -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<string, string> {
export function loopThreadNames(
loops: readonly Loop[],
resolveRunThread: RunThreadResolver = defaultRunThread,
): Record<string, string> {
const byThread: Record<string, string> = {};
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<string, string> {
export function loopIdsByThread(
loops: readonly Loop[],
resolveRunThread: RunThreadResolver = defaultRunThread,
): Record<string, string> {
const byThread: Record<string, string> = {};
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<void> {
const hosts = [LOCAL_HOST, ...useApp.getState().connectedHosts.filter(Boolean)];
export async function refreshLoopThreads(
resolveRunThread?: RunThreadResolver,
remoteHosts?: readonly string[],
): Promise<void> {
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;
Expand All @@ -958,5 +988,5 @@ export async function refreshLoopThreads(): Promise<void> {
loops.push(...result.value.loops);
}
}
if (anySucceeded) rememberLoops(loops);
if (anySucceeded) rememberLoops(loops, resolveRunThread);
}
138 changes: 82 additions & 56 deletions apps/desktop/src/ui/JobCard.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -90,7 +93,7 @@ function StatusChip({ loop, waiting }: { loop: Loop; waiting: boolean }) {
</span>
);
if (state === "failing")
return <span className={cx(chip, "bg-danger/12 font-medium text-danger")}>Paused</span>;
return <span className={cx(chip, "bg-danger/12 font-medium text-danger")}>Needs attention</span>;
if (state === "paused") return <span className={cx(chip, "bg-fill text-fg-muted")}>Paused</span>;
if (waiting)
return (
Expand Down Expand Up @@ -164,23 +167,25 @@ 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));

return (
<li
data-loop={loopKey(loop)}
className={cx(
"group/job relative flex min-w-0 flex-col gap-2 rounded-lg bg-surface p-4 hover:bg-fill-subtle",
"group/job relative flex min-w-0 flex-col gap-2 rounded-xl bg-surface p-4 hover:bg-surface-2",
reason && "ring-1 ring-danger/35",
loop.paused && !reason && "opacity-70",
loop.paused && !reason && "opacity-75",
)}
>
<div className="flex items-start gap-2">
<button
type="button"
onClick={onOpen}
className="min-w-0 flex-1 text-left font-semibold text-title3 text-fg after:absolute after:inset-0 after:rounded-lg focus-visible:outline-none focus-visible:after:ring-1 focus-visible:after:ring-accent"
className="min-w-0 flex-1 text-left font-semibold text-title3 text-fg after:absolute after:inset-0 after:rounded-xl focus-visible:outline-none focus-visible:after:ring-1 focus-visible:after:ring-accent"
>
<span className="line-clamp-2">{loop.name}</span>
</button>
Expand Down Expand Up @@ -245,14 +250,26 @@ export function JobCard({
</div>
</div>

{agent && (
<span title={agent} className="-mt-1 truncate text-caption text-fg-muted">
{agent}
</span>
{(agent || !loop.readOnly) && (
<div className="-mt-1 flex min-w-0 items-center gap-2 text-caption text-fg-muted">
{agent && (
<span title={agent} className="min-w-0 truncate">
{agent}
</span>
)}
{!loop.readOnly && (
<Tooltip label="Runs with full access (can edit files, commit, and push)">
<span className="relative inline-flex shrink-0 items-center gap-1 text-warning">
<ShieldAlert size={12} />
Full access
</span>
</Tooltip>
)}
</div>
)}

{reason ? (
<div className="relative flex items-center gap-2">
<div className="relative flex items-center gap-2 pt-0.5">
<p title={reason} className="line-clamp-2 min-w-0 flex-1 text-callout text-danger">
{reason}
</p>
Expand All @@ -266,11 +283,11 @@ export function JobCard({
</Pill>
)}
</div>
) : (
) : hasRuns ? (
<p
title={result.text}
className={cx(
"line-clamp-2 text-callout",
"line-clamp-2 pt-0.5 text-callout",
result.tone === "bad"
? "text-danger"
: quiet || result.tone === "muted"
Expand All @@ -280,52 +297,61 @@ export function JobCard({
>
{result.text}
</p>
) : (
<p title={promptPreview || undefined} className="line-clamp-2 pt-0.5 text-callout text-fg-muted">
{promptPreview || "No prompt set"}
</p>
)}

<div className="mt-auto flex min-w-0 items-center gap-2 border-t border-border pt-2 text-caption text-fg-muted">
<span className="min-w-0 flex-1 truncate">
{projectName(loop.workspace)}
{(machineLabel || loop.machine) && ` on ${machineLabel || loop.machine}`} ·{" "}
{scheduleInWords(loop.schedule)}
</span>
{loop.script && (
<Tooltip label="Runs a pre-check script first">
<span className="relative inline-flex shrink-0 text-fg-faint">
<Terminal size={12} />
</span>
</Tooltip>
)}
{loop.webhook && loop.schedule.kind !== "manual" && (
<Tooltip label="Webhook trigger enabled">
<span className="relative inline-flex shrink-0 text-fg-faint">
<Webhook size={12} />
</span>
</Tooltip>
)}
{next !== undefined ? (
<time
dateTime={new Date(next).toISOString()}
title={`${nextRunText(loop, now)} · ${FULL_RUN_FORMAT.format(next)}`}
className="tabular shrink-0 text-fg-faint"
>
{nextRunLine(next, now)}
</time>
) : (
<span className="shrink-0 text-fg-faint">{nextRunText(loop, now)}</span>
)}
</div>
<div className="flex items-center justify-between gap-2 text-caption text-fg-muted">
<span className="flex items-center gap-2">
{loop.runs.length > 0 && (
<span>
{loop.runs.length} {loop.runs.length === 1 ? "run" : "runs"}
<div className="mt-auto flex flex-col gap-1.5 border-t border-border pt-3 text-caption text-fg-muted">
<div className="flex min-w-0 items-center justify-between gap-2">
<span className="flex min-w-0 items-center gap-1.5 truncate">
<Folder size={12} className="shrink-0 text-fg-faint" />
<span className="truncate">
{projectName(loop.workspace)}
{(machineLabel || loop.machine) && ` · ${machineLabel || loop.machine}`}
</span>
</span>
{next !== undefined ? (
<time
dateTime={new Date(next).toISOString()}
title={`${nextRunText(loop, now)} · ${FULL_RUN_FORMAT.format(next)}`}
className="tabular shrink-0 text-fg-faint"
>
{nextRunLine(next, now)}
</time>
) : (
<span className="shrink-0 text-fg-faint">{nextRunText(loop, now)}</span>
)}
<RunHistory runs={loop.runs} onOpen={onOpen} />
</span>
<span className="shrink-0">
Run history <ArrowUpRight size={12} className="inline" />
</span>
</div>
<div className="flex min-w-0 items-center justify-between gap-2">
<span className="flex min-w-0 items-center gap-1.5">
<Clock size={12} className="shrink-0 text-fg-faint" />
<span className="truncate">{scheduleInWords(loop.schedule)}</span>
{loop.script && (
<Tooltip label="Runs a pre-check script first">
<span className="relative inline-flex shrink-0 text-fg-faint">
<Terminal size={12} />
</span>
</Tooltip>
)}
{loop.webhook && loop.schedule.kind !== "manual" && (
<Tooltip label="Webhook trigger enabled">
<span className="relative inline-flex shrink-0 text-fg-faint">
<Webhook size={12} />
</span>
</Tooltip>
)}
</span>
<span className="flex shrink-0 items-center gap-1.5 text-fg-faint">
<RunHistory runs={loop.runs} onOpen={onOpen} />
{hasRuns && (
<span className="tabular">
{loop.runs.length} {loop.runs.length === 1 ? "run" : "runs"}
</span>
)}
</span>
</div>
</div>
</li>
);
Expand Down
Loading
Loading