Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
30.6 KB
Raw Blame
"use client";

import { useEffect, useId, useMemo, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { ChevronRight, Loader2, Maximize2, Square } from "@multica/ui/icons";
import { toast } from "sonner";
import { api } from "@multica/core/api";
import { issueTasksOptions } from "@multica/core/issues/queries";
import { useActorName } from "@multica/core/workspace/hooks";
import { useCustomPricingStore } from "@multica/core/runtimes/custom-pricing-store";
import type { AgentTask } from "@multica/core/types";
import { IconButton } from "@multica/ui/components/common/icon-button";
import { cn } from "@multica/ui/lib/utils";
import { Checkbox } from "@multica/ui/components/ui/checkbox";
import { Skeleton } from "@multica/ui/components/ui/skeleton";
import { useTimeAgo } from "../../i18n";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@multica/ui/components/ui/tooltip";
import { ActorAvatar } from "../../common/actor-avatar";
import { AgentAvatarStack } from "../../agents/components/agent-avatar-stack";
import { formatDuration } from "../../agents/components/agent-activity-hover-content";
import { TranscriptButton } from "../../common/task-transcript";
import { cancellationActorLabel, cancelReasonLabel, failureReasonLabel } from "../../agents/components/tabs/task-failure";
import { IssueSidebarCollapsible, IssueSidebarSectionHeader } from "./issue-sidebar-section";
import { useT } from "../../i18n";
import { compareActiveIssueTasks } from "./active-task-order";
import { formatDuration as formatAgentTime } from "../../dashboard/utils";
import {
  formatTokens,
  hasTokenFigure,
  summarizeTaskUsage,
  summarizeTaskUsageAcross,
} from "../../runtimes/utils";
import { TerminateTaskConfirmDialog } from "./terminate-task-confirm-dialog";
import { IssueRunsDialog } from "./issue-runs-dialog";
import { buildRunTimeline } from "./issue-run-timeline";
import { canRetryRun, RetryRunButton } from "./retry-run-button";
import { TaskStatusIcon } from "./task-status-icon";
import { useFormatSpend, useStatusLabel, useTriggerText } from "./task-run-labels";
import { WakeupRunLabel } from "./wakeup-source-chip";

// Sidebar summary: total spend in the header, active runs and recent history.
// Totals, charts and full history live in the Runs dialog.
//
// Row layout — simple left/right flex:
//   1. Agent avatar (no status dot — agent availability is not the
//      story here; the row's right column carries the task status)
//   2. Trigger description flexes and truncates
//   3. Status is a normal shrink-0 right column; on hover it is replaced
//      in place by the action buttons (status is removed, not covered).
//      Left text keeps flex-1 so the row never shows a mid-row gap. Do
//      not use masks/padding gymnastics here.
//
// The timeline index supplies every status and usage without provider output.
// Full run history and charts load only while the Runs dialog is open.

interface ExecutionLogSectionProps {
  issueId: string;
  /** Shown in the Runs dialog's subtitle so it names the issue it lays out. */
  identifier?: string;
  issueTitle?: string;
  hasChildren?: boolean;
}

// The sidebar lists this many rows; everything else is in the Runs dialog.
// Active runs come first and are never cut (they carry the stop action);
// past runs fill whatever is left.
const LIST_ROWS = 3;
// Queued runs at or past this count fold into one expandable row: they have
// nothing to show yet but their trigger, so a row each mostly costs height.
const MIN_QUEUED_GROUP = 2;

// Past-runs sort priority: newest first by timestamp. When two runs
// share the same timestamp, failed ranks above cancelled, which ranks
// above completed.
const PAST_STATUS_RANK: Record<string, number> = {
  failed: 0,
  cancelled: 1,
  completed: 2,
};

export function ExecutionLogSection({
  issueId,
  identifier,
  issueTitle,
  hasChildren = false,
}: ExecutionLogSectionProps) {
  const { t } = useT("issues");
  const [open, setOpen] = useState(true);
  const [runsOpen, setRunsOpen] = useState(false);
  const [includeChildren, setIncludeChildren] = useState(false);
  const scopeLabelId = useId();
  const includeDescendants = hasChildren && includeChildren;
  const pricings = useCustomPricingStore((s) => s.pricings);

  // Cache key registered in `issueKeys.tasks` (packages/core/issues/queries.ts)
  // so the global useRealtimeSync `task:` prefix path invalidates it via
  // a `["issues", "tasks"]` prefix-match — no local WS subscriptions
  // needed, and the cache stays fresh even when this component isn't
  // mounted (e.g. user cancels from agent-side, then navigates here).
  const { data: tasks = [], isPending, isError, refetch } = useQuery(
    issueTasksOptions(issueId, includeDescendants),
  );

  const activeTasks = useMemo(
    () =>
      tasks.filter(
        (t) =>
          t.status === "queued" ||
          t.status === "dispatched" ||
          // Daemon-parked task on a busy local_directory — still active
          // (waiting on a path lock), not terminal. Surfacing it here is
          // what tells the user the agent is alive and will resume.
          t.status === "waiting_local_directory" ||
          t.status === "running",
      ).toSorted(compareActiveIssueTasks),
    [tasks],
  );

  const pastTasks = useMemo(() => {
    const past = tasks.filter(
      (t) =>
        t.status === "completed" ||
        t.status === "failed" ||
        t.status === "cancelled",
    );
    return past.toSorted((a, b) => {
      const at = a.completed_at ?? a.created_at;
      const bt = b.completed_at ?? b.created_at;
      const timeDiff = new Date(bt).getTime() - new Date(at).getTime();
      if (timeDiff !== 0) return timeDiff;
      return (
        (PAST_STATUS_RANK[a.status] ?? 99) -
        (PAST_STATUS_RANK[b.status] ?? 99)
      );
    });
  }, [tasks]);

  const formatSpend = useFormatSpend();
  // Keep the header total priced with the same helpers and custom rates as the dialog.
  const timeline = useMemo(
    () => buildRunTimeline(tasks, Date.now()),
    // eslint-disable-next-line react-hooks/exhaustive-deps -- `pricings` re-prices on a saved custom rate
    [tasks, pricings],
  );

  if (!hasChildren && activeTasks.length === 0 && pastTasks.length === 0) return null;

  const queuedTasks = activeTasks.filter((task) => task.status === "queued");
  const startedTasks = activeTasks.filter((task) => task.status !== "queued");
  const groupQueued = queuedTasks.length >= MIN_QUEUED_GROUP;
  const activeRows = startedTasks.length + (groupQueued ? 1 : queuedTasks.length);
  const latest = pastTasks.slice(0, Math.max(0, LIST_ROWS - activeRows));
  const hiddenCount = pastTasks.length - latest.length;
  const openRuns = () => setRunsOpen(true);
  // Open, the active rows speak for themselves; collapsed, the header says it.
  const status = !open && activeTasks.length > 0 ? (
    <span className="inline-flex shrink-0 items-center gap-1 text-caption text-info tabular-nums">
      <span aria-hidden="true" className="size-1.5 rounded-full bg-info motion-safe:animate-pulse" />
      {t(($) => $.runs_timeline.count_active, { count: activeTasks.length })}
    </span>
  ) : timeline.pricedCount > 0 ? (
    <span data-testid="execution-log-spend" title={t(($) => $.runs_timeline.stat_spent)} className="shrink-0 text-caption text-muted-foreground tabular-nums">
      {formatSpend({ cost: timeline.totalCost, credits: timeline.totalCredits })}
    </span>
  ) : undefined;

  return (
    <div className="@container/execution-log">
      <IssueSidebarSectionHeader
        title={t(($) => $.execution_log.section)} open={open} onOpenChange={setOpen}
        count={activeTasks.length + pastTasks.length}
        countWhenOpen={hiddenCount > 0}
        actions={<IconButton size="icon-xs" onClick={openRuns}
          label={[t(($) => $.execution_log.open_timeline), hiddenCount > 0 ? t(($) => $.execution_log.more_runs, { count: hiddenCount }) : null].filter(Boolean).join(" · ")}>
          <Maximize2 />
        </IconButton>}
        status={status}
      />
      <IssueSidebarCollapsible open={open}>
        <div className="space-y-2 px-2">
          {activeTasks.length > 0 && (
            <div className="max-h-48 space-y-0.5 overflow-y-auto overscroll-contain rounded-md focus-visible:outline-2 focus-visible:outline-ring" role="region" aria-label={t(($) => $.runs_timeline.count_active, { count: activeTasks.length })} tabIndex={0}>
              {startedTasks.map((task) => <ActiveTaskRow key={task.id} task={task} issueId={task.issue_id || issueId} showIssue={includeDescendants} />)}
              {groupQueued ? (
                <QueuedTasksGroup tasks={queuedTasks} issueId={issueId} showIssue={includeDescendants} />
              ) : (
                queuedTasks.map((task) => <ActiveTaskRow key={task.id} task={task} issueId={task.issue_id || issueId} showIssue={includeDescendants} />)
              )}
            </div>
          )}
          {hasChildren && (
            <div className="flex min-h-7 items-center justify-between gap-2 px-2 text-micro text-muted-foreground">
              <span>{t(($) => $.execution_log.latest)}</span>
              <label className="flex shrink-0 cursor-pointer items-center gap-1.5 whitespace-nowrap">
                <Checkbox
                  aria-labelledby={scopeLabelId}
                  checked={includeDescendants}
                  onCheckedChange={setIncludeChildren}
                  className="size-3 [&_svg]:size-2.5"
                />
                <span id={scopeLabelId}>{t(($) => $.execution_log.include_children)}</span>
              </label>
            </div>
          )}
          {isPending ? (
            <div role="status" aria-label={t(($) => $.execution_log.loading)} className="space-y-2 px-2 py-1">
              <Skeleton className="h-6 w-full" />
              <Skeleton className="h-6 w-3/4" />
            </div>
          ) : isError ? (
            <button type="button" onClick={() => void refetch()} className="px-2 py-1 text-caption text-destructive underline">
              {t(($) => $.execution_log.load_failed_retry)}
            </button>
          ) : tasks.every((task) => task.status === "deferred") ? (
            <p className="px-2 py-1 text-caption text-muted-foreground">{t(($) => $.execution_log.empty)}</p>
          ) : latest.length > 0 && (
            <div className="space-y-0.5">
              {latest.map((task) => <PastRow key={task.id} task={task} issueId={task.issue_id || issueId} showIssue={includeDescendants} />)}
            </div>
          )}
        </div>
      </IssueSidebarCollapsible>
      <IssueRunsDialog
        open={runsOpen}
        onOpenChange={setRunsOpen}
        issueId={issueId}
        identifier={identifier ?? ""}
        issueTitle={issueTitle}
        hasChildren={hasChildren}
        includeDescendants={includeDescendants}
      />
    </div>
  );
}

// ─── Issue total ───────────────────────────────────────────────────────────

// The issue's run count and whole spend, as a header affordance: "21 runs ·
// $166". Answers "what has this issue cost" without expanding anything, and is
// the entry point to the Runs dialog.
//
// The cost is left out when no run on the issue has recorded usage — an issue
// whose runs all predate usage reporting shows its run count rather than a
// "$0.00" that would read as "this was free".
//
// Narrow sections drop the run count and keep the cost. Something has to give
// at the narrow end — the header's full form needs ~246px next to the
// active-run chip and the sidebar's 260px minimum leaves 228px — and the count
// is the piece whose absence costs least: the cost answers "what has this
// issue spent", and the runs are listed right below. It is a figure that
// yields, never a figure's digits: a clipped "$31.1…" would read as a
// different number than the issue actually spent.
export function IssueRunsTotal({
  tasks,
  alone,
  onOpen,
}: {
  tasks: AgentTask[];
  alone: boolean;
  onOpen: () => void;
}) {
  const { t } = useT("issues");
  const formatSpend = useFormatSpend();
  // Custom rates are read imperatively inside `estimateCost`, so a saved rate
  // change does not re-render this on its own — subscribe and make the memo
  // depend on the snapshot, or the header total keeps quoting the old price
  // until the task list refetches.
  const pricings = useCustomPricingStore((s) => s.pricings);
  const total = useMemo(
    () => summarizeTaskUsageAcross(tasks.map((task) => task.usage)),
    // eslint-disable-next-line react-hooks/exhaustive-deps -- see above
    [tasks, pricings],
  );
  const runCount = tasks.filter((task) => task.status !== "deferred").length;
  if (runCount === 0) return null;

  // Two thresholds because the header has two shapes, and the tier should cost
  // the reader a figure only where the row genuinely runs out: beside the
  // active-run chip the full form needs ~246px, alone ~218px. Written as whole
  // literal classes — Tailwind scans source text, so a composed string would
  // generate neither. `@max-…` (rather than showing at `@min-…`) is what makes
  // a host that renders this outside the section's `@container` degrade to the
  // full form instead of silently losing the count forever. With no cost to
  // keep, the count is the whole affordance and never tiers away.
  const narrowTier = !total
    ? ""
    : alone
      ? "@max-[14rem]/execution-log:hidden"
      : "@max-[16rem]/execution-log:hidden";

  return (
    <Tooltip>
      <TooltipTrigger
        render={<button type="button" onClick={onOpen} />}
        className={`flex min-w-0 max-w-full items-center gap-1 rounded-md px-1.5 py-0.5 text-caption tabular-nums transition-colors hover:bg-accent/70 ${
          alone ? "ml-auto" : ""
        }`}
      >
        <span className={`text-muted-foreground ${narrowTier}`}>
          {t(($) => $.execution_log.summary_runs, { count: runCount })}
        </span>
        {total && (
          <>
            <span className={`text-faint-foreground ${narrowTier}`}>·</span>
            <span className="min-w-0 break-words text-right font-medium">{formatSpend(total)}</span>
          </>
        )}
      </TooltipTrigger>
      <TooltipContent>{t(($) => $.execution_log.usage_total_tooltip)}</TooltipContent>
    </Tooltip>
  );
}

// Trigger description and status labels live in ./task-run-labels so the
// usage dialog lists a run exactly the way this section does.

// ─── Row visual config ─────────────────────────────────────────────────────

const STATUS_TONE: Record<AgentTask["status"], string> = {
  queued: "text-warning",
  deferred: "text-warning",
  dispatched: "text-warning",
  // Same tone as queued/dispatched — visually "stopped" so users see the
  // task is parked, but distinguished by the status label.
  waiting_local_directory: "text-warning",
  running: "text-info",
  completed: "text-success",
  failed: "text-destructive",
  cancelled: "text-muted-foreground",
};

// ─── Active row ────────────────────────────────────────────────────────────

// One active (running / queued / dispatched / parked) task row. Running rows
// keep status to a single live elapsed timer; transcript and stop stay available
// as hover actions. Transcript content lazy-loads on click via TranscriptButton,
// so the row no longer fetches task messages just to render a count.
export function ActiveTaskRow({
  task,
  issueId,
  onTranscriptOpenChange,
  showIssue = false,
  onJump,
}: {
  task: AgentTask;
  issueId: string;
  onTranscriptOpenChange?: (open: boolean, fromKeyboard?: boolean) => void;
  showIssue?: boolean;
  /** Turns the trigger into a button that takes the reader to this run's block. */
  onJump?: () => void;
}) {
  const { t } = useT("issues");
  const [cancelling, setCancelling] = useState(false);
  const [confirmOpen, setConfirmOpen] = useState(false);
  const tone = STATUS_TONE[task.status];
  const label = useStatusLabel(task.status);
  const trigger = useTriggerText(task);

  // Running rows show a live-ticking elapsed timer (the ticking digits carry
  // "alive", the duration carries "how long"). Only running rows tick.
  const [now, setNow] = useState(() => Date.now());
  useEffect(() => {
    if (task.status !== "running") return;
    const id = setInterval(() => setNow(Date.now()), 1000);
    return () => clearInterval(id);
  }, [task.status]);
  const elapsed =
    task.status === "running"
      ? formatDuration(
          task.started_at ?? task.dispatched_at ?? task.created_at,
          now,
        )
      : "";

  // Transcript only meaningful once messages exist — pure-queued and
  // waiting_local_directory tasks haven't streamed any agent output yet.
  const showTranscript =
    task.status !== "queued" && task.status !== "waiting_local_directory";

  const handleCancel = async () => {
    if (cancelling) return;
    setCancelling(true);
    try {
      await api.cancelTask(issueId, task.id);
    } catch (e) {
      toast.error(e instanceof Error ? e.message : t(($) => $.execution_log.cancel_failed));
      setCancelling(false);
    }
  };

  const requestCancel = () => {
    if (cancelling) return;
    setConfirmOpen(true);
  };

  // Deliberately no token figure on an active row: the daemon reports usage
  // once, after `runner.run` returns (server/internal/daemon/daemon.go), and
  // the write publishes no realtime event — so a running task has no usage to
  // show, and would not learn of it mid-run if it did. Rendering the branch
  // anyway would only ever be exercised by hand-written fixtures, which is a
  // test that asserts a scenario production cannot produce. Restore it in the
  // same change that adds incremental reporting + cache invalidation.
  const triggerText = task.wakeup_id ? (
    <WakeupRunLabel task={task} fallback={trigger} render={(label) => <TriggerText text={label} />} />
  ) : (
    <TriggerText text={trigger} />
  );
  return (
    <RowShell task={task} showIssue={showIssue}>
      {onJump ? (
        <button
          type="button"
          onClick={onJump}
          className="flex min-w-0 flex-1 cursor-pointer items-center gap-2 self-stretch rounded-xs text-left outline-none focus-visible:ring-2 focus-visible:ring-ring"
        >
          {triggerText}
          <TaskCommentCoverage task={task} />
          <span className="sr-only">{t(($) => $.execution_log.jump_to_run)}</span>
        </button>
      ) : (
        <>
          {triggerText}
          <TaskCommentCoverage task={task} />
        </>
      )}
      <RowStatus title={label}>
        {task.status === "running" ? (
          <>
            <span className="text-info tabular-nums">{elapsed}</span>
            <span className="sr-only">{label}</span>
          </>
        ) : (
          <span className={`${tone} min-w-0 truncate`}>{label}</span>
        )}
      </RowStatus>
      <RowActions>
        {showTranscript && (
          <TranscriptButton
            task={task}
            agentName=""
            isLive={task.status === "running"}
            title={t(($) => $.execution_log.transcript_tooltip)}
            onOpenChange={onTranscriptOpenChange}
          />
        )}
        <Tooltip>
          <TooltipTrigger
            render={
              <button
                type="button"
                onClick={requestCancel}
                disabled={cancelling}
                aria-label={t(($) => $.execution_log.cancel_task_aria)}
              />
            }
            className="flex items-center justify-center rounded-xs p-1 text-destructive transition-colors hover:bg-destructive/10 disabled:cursor-not-allowed disabled:opacity-50"
          >
            {cancelling ? (
              <Loader2 className="h-3.5 w-3.5 animate-spin" />
            ) : (
              <Square className="h-3.5 w-3.5" />
            )}
          </TooltipTrigger>
          <TooltipContent>{t(($) => $.execution_log.cancel_task_tooltip)}</TooltipContent>
        </Tooltip>
      </RowActions>
      <TerminateTaskConfirmDialog
        open={confirmOpen}
        onOpenChange={setConfirmOpen}
        onConfirm={() => void handleCancel()}
        showRunningNote={
          task.status === "running" ||
          task.status === "dispatched" ||
          task.status === "waiting_local_directory"
        }
      />
    </RowShell>
  );
}

// ─── Queued group ──────────────────────────────────────────────────────────

// Several queued runs as one row: who is waiting and how many. Expanding it
// lists each run in place, with its cancel action.
function QueuedTasksGroup({ tasks, issueId, showIssue }: { tasks: AgentTask[]; issueId: string; showIssue: boolean }) {
  const { t } = useT("issues");
  const { getActorName } = useActorName();
  const [expanded, setExpanded] = useState(false);
  const agentIds = [...new Set(tasks.map((task) => task.agent_id).filter((id): id is string => Boolean(id)))];

  return (
    <>
      <button
        type="button"
        aria-expanded={expanded}
        onClick={() => setExpanded((v) => !v)}
        className="flex w-full items-center gap-2 rounded-md px-2 py-1 text-left transition-colors hover:bg-accent/50 focus-visible:outline-2 focus-visible:outline-ring"
      >
        {agentIds.length > 0 ? (
          <AgentAvatarStack agentIds={agentIds} size="sm" max={3} className="shrink-0" />
        ) : (
          <span className="inline-block h-5 w-5 shrink-0 rounded-full bg-muted" />
        )}
        <span className="min-w-0 flex-1 truncate text-caption text-muted-foreground">
          {agentIds.map((id) => getActorName("agent", id)).join(", ")}
        </span>
        <span className="flex h-6 shrink-0 items-center gap-1 whitespace-nowrap text-caption text-warning">
          {t(($) => $.execution_log.queued_count, { count: tasks.length })}
          <ChevronRight aria-hidden="true" className={cn("size-3 text-faint-foreground transition-transform motion-reduce:transition-none", expanded && "rotate-90")} />
        </span>
      </button>
      {expanded && tasks.map((task) => <ActiveTaskRow key={task.id} task={task} issueId={task.issue_id || issueId} showIssue={showIssue} />)}
    </>
  );
}

// ─── Past row ──────────────────────────────────────────────────────────────

function PastRow({ task, issueId, showIssue = false }: { task: AgentTask; issueId: string; showIssue?: boolean }) {
  const { t } = useT("issues");
  const formatSpend = useFormatSpend();
  const { t: tAgents } = useT("agents");
  const { getActorName } = useActorName();
  const timeAgo = useTimeAgo();
  const label = useStatusLabel(task.status);
  const trigger = useTriggerText(task);
  const time = task.completed_at ? timeAgo(task.completed_at) : "—";
  // A failed run always explains itself. A cancelled one only when the SERVER
  // cancelled it for a persisted reason (worktree claim gate, preserved-work
  // delivery). Actor provenance is rendered independently below.
  const failureLabel =
    task.status === "failed"
      ? failureReasonLabel(task.failure_reason, tAgents)
      : cancelReasonLabel(task, tAgents);
  const cancellationLabel = cancellationActorLabel(task, tAgents);
  // Hovering the status mark reveals the localized reason, never the raw
  // `task.error`. That field is operator-facing English prose the daemon and
  // server write for classification and logs (#7411) — pasting it into a
  // tooltip made every non-English workspace read English at the exact moment
  // something broke, and dragged absolute worktree paths and machine names
  // into hover text and screenshots. The full diagnostic stays one click away
  // in the transcript's Run details.
  const statusTitle = cancellationLabel
    ? [cancellationLabel, failureLabel].filter(Boolean).join(" · ")
    : failureLabel ?? label;

  // A run that ended normally is one line: trigger, status, cost. Agent,
  // duration, usage and model live in the tooltip and the Runs dialog. Only a
  // failure or cancellation reason earns a second line, since it is the one
  // detail that changes what the reader does next.
  const usage = summarizeTaskUsage(task.usage);
  const agentName = task.agent_id ? getActorName("agent", task.agent_id) : null;
  const reason = cancellationLabel
    ? [cancellationLabel, failureLabel].filter(Boolean).join(" · ")
    : failureLabel;
  const duration = task.started_at && task.completed_at
    ? formatAgentTime((new Date(task.completed_at).getTime() - new Date(task.started_at).getTime()) / 1000, "0s")
    : null;
  const rowTitle = [
    agentName,
    reason ? null : duration,
    time,
    usage && hasTokenFigure(usage)
      ? t(($) => $.runs_timeline.tokens_count, { tokens: formatTokens(usage.tokens) })
      : null,
    usage?.models.join(", "),
  ].filter(Boolean).join(" · ");

  return (
    <RowShell task={task} title={rowTitle} multiline={Boolean(reason)} showIssue={showIssue}>
      <div className="min-w-0 flex-1">
        <div className="flex min-w-0 items-center gap-2">
          {task.wakeup_id ? (
            <WakeupRunLabel task={task} fallback={trigger} render={(label) => <TriggerText text={label} emphasis />} />
          ) : (
            <TriggerText text={trigger} emphasis />
          )}
          <TaskCommentCoverage task={task} />
          <RowStatus title={statusTitle} wrap={!!usage?.credits.size}>
            <TaskStatusIcon status={task.status} />
            <span className="sr-only">{[label, time].filter(Boolean).join(" · ")}</span>
            {usage ? (
              <span className="tabular-nums">{formatSpend(usage)}</span>
            ) : (
              <span className="text-faint-foreground">—</span>
            )}
          </RowStatus>
          <RowActions>
            <TranscriptButton task={task} agentName="" title={t(($) => $.execution_log.transcript_tooltip)} />
            {canRetryRun(task) && <RetryRunButton task={task} issueId={issueId} />}
          </RowActions>
        </div>
        {reason && (
          <p className="break-words text-micro text-muted-foreground">
            {agentName && (
              <>
                {agentName}
                <span className="text-faint-foreground"> · </span>
              </>
            )}
            <span className={task.status === "failed" ? "text-destructive" : undefined}>{reason}</span>
          </p>
        )}
      </div>
    </RowShell>
  );
}

// ─── Shared row chrome ─────────────────────────────────────────────────────

function RowShell({
  task,
  title,
  multiline = false,
  children,
  showIssue = false,
}: {
  task: AgentTask;
  /** Carries the details the right column no longer has room for (time,
   *  duration, model). Lives on the row, not on RowStatus, because RowStatus
   *  is swapped out for the action buttons on hover — a title there would
   *  disappear at exactly the moment the pointer arrives. */
  title?: string;
  /** Top-aligns the avatar with the first of several lines. */
  multiline?: boolean;
  children: React.ReactNode;
  showIssue?: boolean;
}) {
  return (
    <div
      title={title || undefined}
      className={cn(
        "group/execution-log-row flex gap-2 overflow-hidden rounded-md px-2 py-1 transition-colors hover:bg-accent/50",
        multiline ? "items-start" : "items-center",
      )}
    >
      {/* 20px avatar + 4px puts the row text on the sidebar's shared text column. */}
      <span className={cn("-mr-1 flex shrink-0", multiline && "mt-0.5")}>
        {task.agent_id ? (
          <ActorAvatar
            actorType="agent"
            actorId={task.agent_id}
            size="sm"
            enableHoverCard
          />
        ) : (
          <span className="inline-block h-5 w-5 rounded-full bg-muted" />
        )}
      </span>
      {showIssue && (
        <span className="max-w-20 shrink-0 truncate font-mono text-micro text-muted-foreground" title={task.issue_title}>
          {task.issue_identifier || task.issue_id}
        </span>
      )}
      {children}
    </div>
  );
}

function TriggerText({ text, emphasis = false }: { text: string; emphasis?: boolean }) {
  return (
    <span className={cn("min-w-0 flex-1 truncate text-caption", emphasis ? "text-foreground" : "text-muted-foreground")}>
      {text}
    </span>
  );
}

function supportsCommentCoverage(status: AgentTask["status"]): boolean {
  switch (status) {
    case "queued":
    case "dispatched":
    case "waiting_local_directory":
    case "running":
    case "completed":
    case "failed":
    case "cancelled":
      return true;
    default:
      return false;
  }
}

export function TaskCommentCoverage({ task }: { task: AgentTask }) {
  const { t } = useT("issues");
  if (!supportsCommentCoverage(task.status)) return null;

  // Queued rows show the planned coverage: coalesced_comment_ids deliberately
  // excludes the newest trigger. Once claimed, prefer the server's actual
  // delivery receipt. Only legacy rows where that field is absent fall back to
  // the plan; an explicit [] means the claim delivered no comments.
  const plannedCommentIds = [
    task.trigger_comment_id,
    ...(task.coalesced_comment_ids ?? []),
  ];
  const coverageIds =
    task.status !== "queued" && task.delivered_comment_ids !== undefined
      ? task.delivered_comment_ids
      : plannedCommentIds;
  const commentIds = new Set(
    coverageIds.filter((id): id is string => Boolean(id)),
  );
  if (commentIds.size <= 1) return null;

  return (
    <span className="shrink-0 whitespace-nowrap text-micro text-muted-foreground">
      {t(($) => $.execution_log.included_comments, { count: commentIds.size })}
    </span>
  );
}

function RowStatus({
  children,
  title,
  wrap = false,
}: {
  children: React.ReactNode;
  title?: string;
  wrap?: boolean;
}) {
  return (
    <div
      title={title}
      className={cn(
        "flex shrink-0 items-center justify-end gap-1 text-caption [@media(hover:hover)]:group-hover/execution-log-row:hidden",
        wrap ? "min-h-6 max-w-[60%] break-words text-right" : "h-6 overflow-hidden whitespace-nowrap",
      )}
    >
      {children}
    </div>
  );
}

// Action slot — visible by default for touch devices. On hover-capable
// surfaces, it replaces the status column in place on row hover.
function RowActions({ children }: { children: React.ReactNode }) {
  return (
    <div className="flex h-6 items-center gap-0.5 [@media(hover:hover)]:hidden [@media(hover:hover)]:group-hover/execution-log-row:flex">
      {children}
    </div>
  );
}