Skip to content

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

Go
75.0 KB
Raw Blame
"use client";

import { Fragment, memo, useCallback, useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react";
import { UI_MOTION_DURATION } from "@multica/ui/lib/motion";
import { revealElement } from "./use-run-comment-motion";
import { CheckCircle2, ChevronRight, CornerUpLeft, ListChevronsDownUp, Copy, Link2, Loader2, MessageSquare, MessageSquarePlus, MoreHorizontal, Pencil, Plus, RotateCcw, SmilePlus, Trash2 } from "@multica/ui/icons";
import { toast } from "sonner";
import { Card } from "@multica/ui/components/ui/card";
import { Button, buttonVariants } from "@multica/ui/components/ui/button";
import {
  DropdownMenu,
  DropdownMenuTrigger,
  DropdownMenuContent,
} from "@multica/ui/components/ui/dropdown-menu";
import { Tooltip, TooltipTrigger, TooltipContent } from "@multica/ui/components/ui/tooltip";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@multica/ui/components/ui/alert-dialog";
import { ActorAvatar } from "../../common/actor-avatar";
import { shouldOpenRowMenu, useRowMenu } from "../../common/point-menu";
import { MenuActionGroups, useContextMenuScope } from "../../common/context-menu-scope";
import { dropdownPrimitives } from "../../common/menu-primitives";
import type { RowActionItem } from "../../common/row-actions-menu";
import { DeferredTooltip } from "../../common/deferred-tooltip";
import { ReactionBar } from "@multica/ui/components/common/reaction-bar";
import { QuickEmojiPicker } from "@multica/ui/components/common/quick-emoji-picker";
import { cn } from "@multica/ui/lib/utils";
import { copyText } from "@multica/ui/lib/clipboard";
import { useActorName } from "@multica/core/workspace/hooks";
import { useLocale, useTimeAgo } from "../../i18n";
import { StorageLimitNotice } from "../../billing/storage-limit-notice";
import { ContentEditor, type ContentEditorRef, ReadonlyContent, useFileDropZone, FileDropOverlay, Attachment as AttachmentRenderer, AttachmentDownloadProvider, useUploadGate, useComposerSubmit } from "../../editor";
import { useCommentUploads } from "./use-comment-uploads";
import { FileUploadButton } from "@multica/ui/components/common/file-upload-button";
import { api, dispatchReasonCode, errorCode } from "@multica/core/api";
import { ReplyInput, type ReplyInputHandle } from "./reply-input";
import { VirtualThreadRow, shouldVirtualizeThreadRows } from "./virtual-thread-row";
import { CommentTriggerChips } from "./comment-trigger-chips";
import { AppCallChips } from "./app-call-chips";
import { useCommentTriggerPreview } from "../hooks/use-comment-trigger-preview";
import { useRecipientActions } from "../hooks/use-recipient-actions";
import { SteerBadge, SteerReceipts } from "./steer-receipts";
import type { AgentTask, TimelineEntry, Attachment } from "@multica/core/types";
import { contentReferencesAttachment } from "@multica/core/types";
import { isDeletedComment } from "@multica/core/issues/comment-deletion";
import { commentSupplementReceipts, isSupplementInFlight } from "@multica/core/issues/run-steering";
import { useConfigStore } from "@multica/core/config";
import {
  orderStandaloneAttachments,
  selectStandaloneAttachments,
  standaloneAttachmentGroup,
} from "@multica/core/attachments/image-sequence";
import { useCommentCollapseStore, useCommentDraftStore } from "@multica/core/issues/stores";
import { useT } from "../../i18n";
import { CommentsFoldBar } from "./resolved-thread-bar";
import { deriveThreadResolution } from "./thread-utils";
import { RevisionConflictCompare } from "./revision-conflict-compare";
import { InlineCommentRun, PlacedInlineCommentRun, useInlineCommentRunState, type InlineCommentRunState } from "./inline-comment-run";
import { EMPTY_COMMENT_RUNS, isRunFailureNotice, orderVisibleThreadRows, type CommentRun, type ThreadRunSlot } from "./comment-runs";
import { descriptionPreview } from "./description-preview";
import { useCommentAnnotations } from "./use-comment-annotations";
import { useAttachmentVersions } from "./deliverables/attachment-versions";
import { VersionBadge } from "./deliverables/version-badge";
import { useRunCommentMotion } from "./use-run-comment-motion";
import { WakeupSourceChip } from "./wakeup-source-chip";

const commentActionClassName =
  "text-muted-foreground aria-expanded:bg-transparent aria-expanded:hover:bg-muted dark:aria-expanded:hover:bg-muted/50";

const highlightedCommentBackgroundClass =
  "bg-[color-mix(in_srgb,var(--card)_95%,var(--brand)_5%)]";
const stickyHeaderFadeClass =
  "after:pointer-events-none after:absolute after:inset-x-0 after:top-full after:h-1 after:bg-[inherit] after:[mask-image:linear-gradient(to_bottom,#000,transparent)] after:[-webkit-mask-image:linear-gradient(to_bottom,#000,transparent)]";

function StickyHeaderShell({
  className,
  sticky = true,
  highlighted,
  children,
}: {
  className?: string;
  sticky?: boolean;
  highlighted?: boolean;
  children: ReactNode;
}) {
  if (!sticky) {
    return (
      <div className={cn(highlighted && highlightedCommentBackgroundClass, className)}>
        {children}
      </div>
    );
  }

  // `mb-1` reserves room for the fade's `h-1` below the header, so at rest the
  // fade covers empty space instead of the first line of the body. Callers
  // take the same 4px out of their bottom padding.
  return (
    <div
      className={cn(
        "sticky top-0 z-10 mb-1 transition-colors duration-700",
        !highlighted && stickyHeaderFadeClass,
        highlighted ? highlightedCommentBackgroundClass : "bg-card",
        className,
      )}
    >
      {children}
    </div>
  );
}

// Types

interface CommentCardProps {
  issueId: string;
  replyScrollParent?: HTMLElement | null;
  runs?: CommentRun[];
  runViewState?: InlineCommentRunState;
  enteringRunIds?: ReadonlySet<string>;
  entry: TimelineEntry;
  /**
   * Flat list of every nested reply under this thread root, in render order.
   * Computed once in `issue-detail.tsx`'s `timelineView` and stabilized so
   * the array reference only changes when *this* thread's replies change —
   * an unrelated thread receiving a new reply must NOT bust this card's
   * memo. Passing the full Map here used to do exactly that.
   */
  replies: TimelineEntry[];
  currentUserId?: string;
  /**
   * True when the current user is a workspace owner/admin and can therefore
   * moderate comments authored by anyone — restoring the admin override that
   * the backend already grants at `comment.go:507-512`. Computed once in
   * `issue-detail.tsx` and threaded down so neither this component nor
   * `CommentRow` has to rerun the rule per row.
   */
  canModerate?: boolean;
  onReply: (parentId: string, content: string, attachmentIds?: string[], suppressAgentIds?: string[], steerTaskIds?: string[]) => Promise<string | boolean>;
  onReplyAccepted?: (commentId: string) => void;
  onEdit: (commentId: string, content: string, attachmentIds: string[], suppressAgentIds?: string[], contentBase?: string) => Promise<void>;
  onDelete: (commentId: string) => void;
  onToggleReaction: (commentId: string, emoji: string) => void;
  onCreateSubIssue?: (commentId: string, prompt?: string) => void;
  /** Resolve/unresolve any comment in this thread (commentId = the target row). */
  onResolveToggle?: (commentId: string, resolved: boolean) => void;
  /** Copy a deep link (`#comment-…`) to a single comment in this thread. */
  onCopyLink?: (commentId: string) => void;
  /** Scroll to and flash a comment in this thread. */
  onJumpToComment?: (commentId: string) => void;
  /**
   * When non-null, the thread root is currently rendered as a resolved-but-
   * expanded card. Pass a "Collapse" affordance into the header so the user
   * can fold the thread back to the bar; the parent owns the session state.
   */
  onCollapseResolved?: () => void;
  /**
   * Per-session set of thread ROOT ids whose reply-resolution fold is expanded.
   * Used only when a REPLY is the resolution (root-resolution folding is handled
   * one level up in issue-detail's resolved-bar). Keyed on root id.
   */
  expandedResolvedIds?: ReadonlySet<string>;
  onResolvedExpandChange?: (rootId: string, expand: boolean) => void;
  /** ID of the comment to highlight (flash animation). */
  highlightedCommentId?: string | null;
}

// Shared delete confirmation dialog

function DeleteCommentDialog({
  open,
  onOpenChange,
  onConfirm,
  hasReplies,
}: {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onConfirm: () => void;
  hasReplies?: boolean;
}) {
  const { t } = useT("issues");
  // Only a server that declares it keeps the replies (#8296); an older one
  // deletes them with the comment, and the copy must say so.
  const keepsReplies = useConfigStore((s) => s.commentDeleteKeepRepliesSupported);
  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{t(($) => $.comment.delete_title)}</AlertDialogTitle>
          <AlertDialogDescription>
            {!hasReplies
              ? t(($) => $.comment.delete_desc)
              : keepsReplies
                ? t(($) => $.comment.delete_desc_replies_kept)
                : t(($) => $.comment.delete_desc_with_replies)}
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>{t(($) => $.comment.cancel_action)}</AlertDialogCancel>
          <AlertDialogAction variant="destructive" onClick={onConfirm}>
            {t(($) => $.comment.delete_action)}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

// Standalone attachment list — renders attachments not already in the markdown

export function AttachmentList({
  attachments,
  content,
  className,
  onRemove,
}: {
  attachments?: Attachment[];
  content?: string;
  className?: string;
  onRemove?: (attachmentId: string) => void;
}) {
  const versions = useAttachmentVersions();
  if (!attachments?.length) return null;
  // Skip attachments whose URL (stable or legacy) is already referenced in the
  // markdown content, and duplicates of the same file that are referenced.
  // Shared with the image-sequence builder (MUL-5752) so the cards rendered
  // here and the images the viewer pages through can't disagree — as is the
  // grouping below, so paging follows the screen (MUL-7649).
  const standalone = orderStandaloneAttachments(
    selectStandaloneAttachments(content, attachments),
  );
  if (!standalone.length) return null;

  const images = standalone.filter((a) => standaloneAttachmentGroup(a) === "image");
  const files = standalone.filter((a) => standaloneAttachmentGroup(a) === "file");
  const render = (a: Attachment) => {
    const version = versions?.get(a.id);
    return (
      <AttachmentRenderer
        key={a.id}
        attachment={{ kind: "record", attachment: a }}
        layout="card"
        badge={version ? <VersionBadge version={version} /> : undefined}
        editable={!!onRemove}
        onDelete={onRemove ? () => onRemove(a.id) : undefined}
      />
    );
  };

  // Images stack at full size so they read without opening (MUL-7736), or —
  // under the timeline's "thumbnails" image preference — sit in a row of
  // thumbnails (editor/styles/attachment.css, MUL-9037). Every other file —
  // HTML too: an uploaded file is a deliverable to open, not part of the
  // text — is a card in a grid, not a full-width row each.
  return (
    <AttachmentDownloadProvider attachments={attachments}>
      <div className={cn("flex flex-col gap-2", className)}>
        {images.length > 0 && (
          <div data-comment-image-list className="flex flex-col items-start gap-2">{images.map(render)}</div>
        )}
        {files.length > 0 && (
          <div className="grid grid-cols-[repeat(auto-fill,minmax(min(15rem,100%),1fr))] gap-2">
            {files.map(render)}
          </div>
        )}
      </div>
    </AttachmentDownloadProvider>
  );
}

function collectActiveAttachmentIds(
  content: string,
  attachments: Attachment[],
  retainedStandaloneIds?: Set<string> | null,
): string[] {
  const ids = new Set<string>();
  for (const attachment of attachments) {
    if (contentReferencesAttachment(content, attachment)) ids.add(attachment.id);
  }
  for (const id of retainedStandaloneIds ?? []) ids.add(id);
  return [...ids];
}

function sameIdSet(a: string[], b: string[]): boolean {
  if (a.length !== b.length) return false;
  const set = new Set(a);
  return b.every((id) => set.has(id));
}

function initialStandaloneAttachmentIds(entry: TimelineEntry): Set<string> {
  const content = entry.content ?? "";
  return new Set(
    (entry.attachments ?? [])
      .filter((attachment) => !contentReferencesAttachment(content, attachment))
      .map((attachment) => attachment.id),
  );
}

function retryableAgentFailureComment(entry: TimelineEntry): entry is TimelineEntry & { source_task_id: string } {
  return (
    entry.actor_type === "agent" &&
    entry.comment_type === "system" &&
    typeof entry.source_task_id === "string" &&
    entry.source_task_id.length > 0
  );
}

function TaskCommentRetryButton({
  issueId,
  taskId,
  className,
}: {
  issueId: string;
  taskId: string;
  className?: string;
}) {
  const { t } = useT("issues");
  const [retrying, setRetrying] = useState(false);

  const handleRetry = async () => {
    if (retrying) return;
    setRetrying(true);
    try {
      await api.rerunIssue(issueId, taskId);
    } catch (e) {
      // Rerun re-checks the operator's invoke permission (MUL-4525); a
      // structured 403 is a permission block, not a transient failure.
      toast.error(
        dispatchReasonCode(e) === "invocation_not_allowed"
          ? t(($) => $.execution_log.retry_blocked)
          : e instanceof Error
            ? e.message
            : t(($) => $.execution_log.retry_failed),
      );
    } finally {
      setRetrying(false);
    }
  };

  return (
    <div className={cn("flex", className)}>
      <Button
        type="button"
        size="sm"
        variant="outline"
        onClick={handleRetry}
        disabled={retrying}
        aria-label={t(($) => $.execution_log.retry_task_aria)}
      >
        {retrying ? (
          <Loader2 className="h-3.5 w-3.5 animate-spin" />
        ) : (
          <RotateCcw className="h-3.5 w-3.5" />
        )}
        {t(($) => $.execution_log.retry_task_tooltip)}
      </Button>
    </div>
  );
}

// Shared edit-attachment state hook

const NEVER_STEER = () => false;

function useEditAttachmentState(
  issueId: string,
  entry: TimelineEntry,
  onEdit: (commentId: string, content: string, attachmentIds: string[], suppressAgentIds?: string[], contentBase?: string) => Promise<void>,
) {
  const { t } = useT("issues");
  const { t: tEditor } = useT("editor");
  const [editing, setEditing] = useState(false);
  const [initialValue, setInitialValue] = useState(entry.content ?? "");
  const [initialContentBase, setInitialContentBase] = useState(entry.content ?? "");
  const [revisionConflict, setRevisionConflict] = useState(false);
  const editorRef = useRef<ContentEditorRef>(null);
  // Saving mid-upload would persist the edit without the file the user just
  // pasted in — same failure as posting a new comment.
  const uploadGate = useUploadGate(editorRef);
  const cancelledRef = useRef(false);
  const [content, setContent] = useState(entry.content ?? "");
  // Uploads for this edit session (MUL-5181) — coordinator-owned, persisted in
  // the draft store keyed by the edit draft so scroll-out/close no longer drops
  // an in-flight upload.
  const draftKey = `edit:${issueId}:${entry.id}` as const;
  // `gate` widens the editor gate with coordinator-owned placeholders — see
  // CommentInput.
  const { uploads, attachments: pendingAttachments, handleUpload, removeUpload, gate, storageRefusal, dismissStorageRefusal } =
    useCommentUploads(draftKey, { issueId }, uploadGate, editorRef);
  const [retainedStandaloneIds, setRetainedStandaloneIds] = useState<Set<string> | null>(null);
  const triggerPreview = useCommentTriggerPreview({
    issueId,
    parentId: entry.parent_id ?? undefined,
    editingCommentId: entry.id,
    content: editing ? content : "",
  });
  // An edit neither steers nor stops a run: it can only skip a recipient.
  const { recipients, routing, setAction: setRecipientAction, reset: resetRecipients } = useRecipientActions({
    issueId,
    agents: triggerPreview.agents,
    allowSteer: false,
    steerByDefault: NEVER_STEER,
    resetKey: `${issueId}:${entry.id}:${entry.parent_id ?? ""}`,
  });

  const editorAttachments = pendingAttachments.length > 0
    ? [...(entry.attachments ?? []), ...pendingAttachments]
    : entry.attachments;

  useEffect(() => {
    if (revisionConflict) setInitialContentBase(entry.content ?? "");
  }, [entry.content, revisionConflict]);

  const { isDragOver, dropZoneProps } = useFileDropZone({
    onDrop: (files) => files.forEach((f) => editorRef.current?.uploadFile(f)),
    enabled: editing,
  });

  const getDraft = useCommentDraftStore.getState().getDraft;
  const setDraft = useCommentDraftStore((s) => s.setDraft);
  const clearDraft = useCommentDraftStore((s) => s.clearDraft);

  const standaloneEditAttachments = (entry.attachments ?? []).filter((a) =>
    retainedStandaloneIds?.has(a.id),
  );

  const resetState = () => {
    setEditing(false);
    setRevisionConflict(false);
    setContent(entry.content ?? "");
    resetRecipients();
    setRetainedStandaloneIds(null);
    // clearDraft drops both the edit text and its pending attachments.
    clearDraft(draftKey);
  };

  const startEdit = () => {
    cancelledRef.current = false;
    const draft = getDraft(draftKey) ?? entry.content ?? "";
    setInitialValue(draft);
    setInitialContentBase(entry.content ?? "");
    setContent(draft);
    setRetainedStandaloneIds(initialStandaloneAttachmentIds(entry));
    setEditing(true);
  };

  const cancelEdit = () => {
    cancelledRef.current = true;
    resetState();
  };

  /**
   * Discard the local draft and continue from the version the server holds.
   * Local-only — the server already stores this content, so nothing is written.
   * The editor is dirty (that is why the conflict exists), so `adoptContent` is
   * the only channel that lands: a plain `defaultValue` change is mount-only.
   */
  const adoptServerVersion = () => {
    const serverContent = entry.content ?? "";
    editorRef.current?.adoptContent(serverContent);
    setContent(serverContent);
    setDraft(draftKey, serverContent);
    setInitialContentBase(serverContent);
    setRevisionConflict(false);
  };

  // Await-then-render save (MUL-5181): shared submit contract, with the edit-
  // only concerns folded into onSubmit — the cancel-race guard, the no-op
  // short-circuit, and the failure toast. The hook owns the empty guard,
  // upload re-check, single-flight, and lock/spin via `submitting`.
  // Stale-submit guard (MUL-5181 P0) — see CommentInput. The edit hook lives
  // in CommentRow, so "unmounted" here means the issue detail closed.
  const editMountedRef = useRef(true);
  useEffect(() => {
    editMountedRef.current = true;
    return () => {
      editMountedRef.current = false;
    };
  }, []);
  const submittedEntryRef = useRef<unknown>(null);

  const { submitting: saving, submit: saveEdit } = useComposerSubmit({
    editorRef,
    uploadGate: gate,
    onSubmit: async (trimmed) => {
      // A save racing a just-pressed Cancel must never reach the server.
      if (cancelledRef.current) return false;
      // Flush pending debounce before snapshotting — see CommentInput.
      const pendingMd = editorRef.current?.flushPendingUpdate?.();
      if (pendingMd != null) setDraft(draftKey, pendingMd);
      submittedEntryRef.current = useCommentDraftStore.getState().drafts[draftKey];
      const activeIds = collectActiveAttachmentIds(
        trimmed,
        [...(entry.attachments ?? []), ...pendingAttachments],
        // Body-referenced + retained-standalone only (MUL-5181): an upload the
        // user removed from the body is really unbound. Close-surviving
        // uploads are written back into the body by the settle handler.
        retainedStandaloneIds,
      );
      const attachmentsChanged = !sameIdSet(activeIds, (entry.attachments ?? []).map((a) => a.id));
      // Nothing changed — close the editor without a write. Accepted, so
      // onAccepted resets the edit state.
      if (trimmed === (entry.content ?? "").trim() && !attachmentsChanged) {
        return true;
      }
      const { suppressAgentIds } = routing;
      try {
        await onEdit(
          entry.id,
          trimmed,
          activeIds,
          suppressAgentIds.length > 0 ? suppressAgentIds : undefined,
          initialContentBase,
        );
        setRevisionConflict(false);
        return true;
      } catch (err) {
        if (errorCode(err) === "revision_conflict") {
          setRevisionConflict(true);
        }
        toast.error(
          errorCode(err) === "revision_conflict"
            ? t(($) => $.revision.conflict)
            : err instanceof Error && err.message
            ? err.message
            : t(($) => $.comment.update_failed),
        );
        return false;
      }
    },
    onAccepted: () => {
      // Success may only consume the entry it submitted: edits made during the
      // save survive, and the editor then STAYS in edit mode on them.
      const lateMd = editorRef.current?.flushPendingUpdate?.();
      if (lateMd != null) setDraft(draftKey, lateMd);
      const store = useCommentDraftStore.getState();
      const live = store.drafts[draftKey];
      const untouched = live === undefined || live === submittedEntryRef.current;
      if (!editMountedRef.current) {
        if (untouched) store.clearDraft(draftKey);
        return;
      }
      if (!untouched) return;
      resetState();
    },
  });

  return {
    editing,
    saving,
    uploading: gate.uploading,
    onUploadingChange: gate.onUploadingChange,
    uploadingLabel: tEditor(($) => $.upload.in_progress),
    editorRef,
    editorAttachments,
    handleUpload,
    uploads,
    removeUpload,
    storageRefusal,
    dismissStorageRefusal,
    isDragOver,
    dropZoneProps,
    triggerPreview,
    content,
    recipients,
    setRecipientAction,
    draftKey,
    setDraft,
    setContent,
    clearDraft,
    initialValue,
    standaloneEditAttachments,
    retainedStandaloneIds,
    setRetainedStandaloneIds,
    startEdit,
    cancelEdit,
    saveEdit,
    revisionConflict,
    adoptServerVersion,
  };
}

function CommentRevisionConflict({
  serverContent,
  localContent,
  onKeepLocal,
  onUseServer,
  saving,
}: {
  serverContent: string;
  localContent: string;
  onKeepLocal: () => void;
  onUseServer: () => void;
  saving?: boolean;
}) {
  const { t } = useT("issues");
  return (
    <RevisionConflictCompare
      className="mt-2"
      title={t(($) => $.revision.compare_comment)}
      serverLabel={t(($) => $.revision.server_version)}
      localLabel={t(($) => $.revision.local_version)}
      serverValue={serverContent}
      localValue={localContent}
      serverAction={(
        <Button
          type="button"
          size="sm"
          variant="outline"
          onClick={onUseServer}
        >
          {t(($) => $.revision.use_server)}
        </Button>
      )}
      localAction={(
        <Button
          type="button"
          size="sm"
          variant="outline"
          disabled={saving}
          onClick={onKeepLocal}
        >
          {t(($) => $.revision.keep_local)}
        </Button>
      )}
    />
  );
}

// Single comment row (used for both parent and replies within the same Card)

/**
 * A comment's menu, the same groups on its ⋯ button, on right-click, and as
 * the "Comment actions" submenu of content inside it: answer it, change it,
 * take it elsewhere, then delete alone.
 */
function useCommentMenuGroups({
  entry,
  editing,
  canEdit,
  canDelete,
  resolve,
  onStartEdit,
  onReply,
  onAddReaction,
  onCopyLink,
  onCreateSubIssue,
  onDelete,
}: {
  entry: TimelineEntry;
  editing: boolean;
  canEdit: boolean;
  canDelete: boolean;
  resolve?: Pick<RowActionItem, "icon" | "label" | "onSelect">;
  onStartEdit: () => void;
  onReply?: () => void;
  onAddReaction: () => void;
  onCopyLink?: (commentId: string) => void;
  onCreateSubIssue?: (commentId: string, prompt?: string) => void;
  onDelete: () => void;
}): RowActionItem[][] {
  const { t } = useT("issues");
  const respond: RowActionItem[] = [];
  if (onReply) {
    respond.push({ key: "reply", icon: <CornerUpLeft className="h-3.5 w-3.5" />, label: t(($) => $.comment.reply_action), onSelect: onReply });
  }
  if (!editing) {
    respond.push({ key: "react", icon: <SmilePlus className="h-3.5 w-3.5" />, label: t(($) => $.comment.add_reaction), onSelect: onAddReaction });
  }
  if (canEdit) {
    respond.push({ key: "edit", icon: <Pencil className="h-3.5 w-3.5" />, label: t(($) => $.comment.edit_action), onSelect: onStartEdit });
  }
  const act: RowActionItem[] = [];
  if (resolve) act.push({ key: "resolve", ...resolve });
  if (onCreateSubIssue && entry.comment_type === "comment") {
    act.push({
      key: "sub-issue",
      icon: <MessageSquarePlus className="h-3.5 w-3.5" aria-hidden />,
      label: t(($) => $.source_context.create_action),
      onSelect: () => onCreateSubIssue(entry.id),
    });
  }
  const share: RowActionItem[] = [{
    key: "copy",
    icon: <Copy className="h-3.5 w-3.5" />,
    label: t(($) => $.comment.copy_action),
    onSelect: () => {
      void copyText(entry.content ?? "").then((ok) => {
        if (ok) toast.success(t(($) => $.comment.copied_toast));
      });
    },
  }];
  if (onCopyLink) {
    share.push({ key: "copy-link", icon: <Link2 className="h-3.5 w-3.5" />, label: t(($) => $.comment.copy_link_action), onSelect: () => onCopyLink(entry.id) });
  }
  const remove: RowActionItem[] = canDelete
    ? [{ key: "delete", icon: <Trash2 className="h-3.5 w-3.5" />, label: t(($) => $.comment.delete_action), danger: true, onSelect: onDelete }]
    : [];
  return [respond, act, share, remove];
}

/**
 * Registers a comment as the scope of content menus inside it: a link, a
 * mention or selected text there carries the comment's actions in a submenu,
 * and selected text can become a sub-issue.
 */
function useCommentScope(
  entry: TimelineEntry,
  groups: RowActionItem[][] | null,
  onCreateSubIssue?: (commentId: string, prompt?: string) => void,
) {
  const { t } = useT("issues");
  return useContextMenuScope(() => groups && {
    label: t(($) => $.comment.actions_label),
    icon: <MessageSquare className="h-3.5 w-3.5" />,
    groups,
    selectionActions: onCreateSubIssue && entry.comment_type === "comment"
      ? (text) => [{
          key: "create-issue",
          icon: <Plus className="h-3.5 w-3.5" />,
          label: t(($) => $.comment.create_issue_from_selection),
          onSelect: () => onCreateSubIssue(entry.id, quoteMarkdown(text)),
        }]
      : undefined,
  });
}

function quoteMarkdown(text: string) {
  return text.trim().split("\n").map((line) => `> ${line}`).join("\n");
}

/** Runs an action a menu item picked once the menu has closed, so the menu
 *  does not hand focus back to its trigger over whatever the action opened. */
function useAfterMenuClose() {
  const pendingRef = useRef(false);
  const finalFocus = useCallback(() => !pendingRef.current, []);
  const schedule = useCallback((action: () => void) => () => {
    pendingRef.current = true;
    setTimeout(() => {
      action();
      setTimeout(() => { pendingRef.current = false; }, 300);
    }, 0);
  }, []);
  return { finalFocus, schedule };
}

function CommentRow({
  runHeader,
  runMetadata,
  replyTo,
  issueId,
  entry,
  currentUserId,
  canModerate = false,
  isResolution = false,
  isHighlighted = false,
  hasReplies = false,
  onEdit,
  onDelete,
  onToggleReaction,
  onCreateSubIssue,
  onResolveToggle,
  onCopyLink,
  onFocusReply,
}: {
  runHeader?: ReactNode;
  runMetadata?: ReactNode;
  replyTo?: ReactNode;
  issueId: string;
  entry: TimelineEntry;
  currentUserId?: string;
  canModerate?: boolean;
  /** True when this reply is the thread's resolution (shows the green badge). */
  isResolution?: boolean;
  /** True when other replies answer this one; they are kept when it is deleted. */
  hasReplies?: boolean;
  /** True when this row is the deep-link target currently being highlighted. */
  isHighlighted?: boolean;
  onEdit: (commentId: string, content: string, attachmentIds: string[], suppressAgentIds?: string[], contentBase?: string) => Promise<void>;
  onDelete: (commentId: string) => void;
  onToggleReaction: (commentId: string, emoji: string) => void;
  onCreateSubIssue?: (commentId: string, prompt?: string) => void;
  onResolveToggle?: (commentId: string, resolved: boolean) => void;
  onCopyLink?: (commentId: string) => void;
  /** Moves the caret into the thread's reply box. */
  onFocusReply?: () => void;
}) {
  const { t } = useT("issues");
  const locale = useLocale();
  const timeAgo = useTimeAgo();
  const { getActorName } = useActorName();

  const edit = useEditAttachmentState(issueId, entry, onEdit);

  const isOwn = entry.actor_type === "member" && entry.actor_id === currentUserId;
  // A message that steered a run is part of that run's input: it cannot be
  // rewritten, and it cannot be removed while a turn is still receiving it.
  const receipts = commentSupplementReceipts(entry);
  const canEditEntry = receipts.length === 0 && (isOwn || (canModerate && entry.actor_type === "member"));
  const canDeleteEntry = !receipts.some(isSupplementInFlight) && (isOwn || canModerate);
  const [confirmDelete, setConfirmDelete] = useState(false);
  // Right-click on the comment opens its ⋯ menu at the cursor; links, images
  // and selected text inside it keep their own menu.
  const rowMenu = useRowMenu();
  const [reactionOpen, setReactionOpen] = useState(false);
  const afterClose = useAfterMenuClose();
  const menuGroups = useCommentMenuGroups({
    entry,
    editing: edit.editing,
    canEdit: canEditEntry,
    canDelete: canDeleteEntry,
    resolve: onResolveToggle && (isResolution
      ? { icon: <RotateCcw className="h-3.5 w-3.5" />, label: t(($) => $.comment.resolve.unresolve_action), onSelect: () => onResolveToggle(entry.id, false) }
      : { icon: <CheckCircle2 className="h-3.5 w-3.5" />, label: t(($) => $.comment.resolve.resolve_with_comment_action), onSelect: () => onResolveToggle(entry.id, true) }),
    onStartEdit: edit.startEdit,
    onReply: onFocusReply && afterClose.schedule(onFocusReply),
    onAddReaction: afterClose.schedule(() => setReactionOpen(true)),
    onCopyLink,
    onCreateSubIssue,
    onDelete: () => setConfirmDelete(true),
  });
  const scopeRef = useCommentScope(entry, edit.editing ? null : menuGroups, onCreateSubIssue);

  const reactions = entry.reactions ?? [];

  // A deleted reply renders nothing at all. Its row is kept only so the
  // replies to it keep a direct parent (#8296), and the thread renders those
  // replies flat, in its place — a placeholder row would say nothing they do
  // not already say. Callers drop this row's chrome too, so the thread shows
  // no empty divider where it was.
  if (isDeletedComment(entry)) return null;

  return (
    <div
      ref={scopeRef}
      data-comment-block
      className="pb-3"
      onContextMenu={(e) => {
        if (edit.editing || !shouldOpenRowMenu(e, { deferToContent: true })) return;
        rowMenu.openAtPointer(e);
      }}
    >
      {/* Header pins to the timeline's scroll parent within this reply's own
          row box, so a LONG reply keeps its
          author + actions visible while you scroll its body, then releases once
          this reply ends. The header stays opaque and matches this comment's
          highlight state while it occludes the body scrolling underneath. */}
      <StickyHeaderShell
        highlighted={isHighlighted}
        className="flex items-center gap-2.5 px-4 max-md:px-3 pt-3 pb-1"
      >
        <ActorAvatar
          actorType={entry.actor_type}
          actorId={entry.actor_id}
          name={entry.actor_name}
          avatarUrl={entry.actor_avatar_url}
          profileRequiresDirectoryEntry
          size="md"
          enableHoverCard
          showStatusDot
        />
        <span className="cursor-pointer text-body font-medium">
          {entry.actor_name || getActorName(entry.actor_type, entry.actor_id)}
        </span>
        <Tooltip>
          <TooltipTrigger
            render={
              <span className="text-label text-muted-foreground cursor-default">
                {timeAgo(entry.created_at)}
              </span>
            }
          />
          <TooltipContent side="top">
            {new Date(entry.created_at).toLocaleString(locale)}
          </TooltipContent>
        </Tooltip>

        <SteerBadge issueId={issueId} entry={entry} />
        {runHeader}

        {entry.actor_type === "agent" && entry.source_task_id && (
          <WakeupSourceChip issueId={issueId} taskId={entry.source_task_id} />
        )}

        {isResolution && (
          <span className="text-caption font-medium text-success">
            {t(($) => $.comment.resolve.resolution_badge)}
          </span>
        )}

        <div data-comment-actions className="ml-auto flex shrink-0 items-center gap-0.5">
          {!edit.editing && <QuickEmojiPicker
            onSelect={(emoji) => onToggleReaction(entry.id, emoji)}
            ariaLabel={t(($) => $.comment.add_reaction)}
            align="end"
            open={reactionOpen}
            onOpenChange={setReactionOpen}
            className={buttonVariants({ variant: "ghost", size: "icon-sm", className: commentActionClassName })}
          />}
          <DropdownMenu open={rowMenu.menu.open} onOpenChange={rowMenu.onOpenChange}>
            <DeferredTooltip
              content={t(($) => $.comment.more_actions)}
              trigger={
                <DropdownMenuTrigger
                  render={
                    <Button
                      variant="ghost"
                      size="icon-sm"
                      className={commentActionClassName}
                      aria-label={t(($) => $.comment.more_actions)}
                    >
                      <MoreHorizontal className="h-4 w-4" aria-hidden />
                    </Button>
                  }
                />
              }
            />
            <DropdownMenuContent
              align={rowMenu.menu.point ? "start" : "end"}
              anchor={rowMenu.anchor}
              finalFocus={afterClose.finalFocus}
              className="w-auto min-w-44"
            >
              <MenuActionGroups groups={menuGroups} primitives={dropdownPrimitives} />
            </DropdownMenuContent>
          </DropdownMenu>
          <DeleteCommentDialog
            open={confirmDelete}
            onOpenChange={setConfirmDelete}
            onConfirm={() => onDelete(entry.id)}
            hasReplies={hasReplies}
          />
        </div>
      </StickyHeaderShell>
      {replyTo && <div className="pl-12 pr-4 max-md:pl-3 max-md:pr-3 pb-1.5">{replyTo}</div>}

      {edit.editing ? (
        <div
          {...edit.dropZoneProps}
          className="relative pl-12 pr-4 max-md:pl-3 max-md:pr-3 pt-1"
          onKeyDown={(e) => { if (e.key === "Escape") edit.cancelEdit(); }}
        >
          <div className="text-body leading-relaxed">
            <ContentEditor
              ref={edit.editorRef}
              defaultValue={edit.initialValue}
              placeholder={t(($) => $.comment.edit_placeholder)}
              onUpdate={(md) => {
                edit.setContent(md);
                if (md.trim().length > 0) edit.setDraft(edit.draftKey, md);
                else edit.clearDraft(edit.draftKey);
              }}
              onSubmit={edit.saveEdit}
              onUploadFile={edit.handleUpload}
              onUploadingChange={edit.onUploadingChange}
              debounceMs={100}
              currentIssueId={issueId}
              attachments={edit.editorAttachments}
            />
          </div>
          {edit.storageRefusal && (
            <StorageLimitNotice refusal={edit.storageRefusal} onDismiss={edit.dismissStorageRefusal} className="mt-2" />
          )}
          {edit.revisionConflict ? (
            <CommentRevisionConflict
              serverContent={entry.content ?? ""}
              localContent={edit.content}
              onKeepLocal={() => void edit.saveEdit()}
              onUseServer={edit.adoptServerVersion}
              saving={edit.saving}
            />
          ) : null}
          {edit.standaloneEditAttachments.length > 0 && (
            <AttachmentList
              attachments={edit.standaloneEditAttachments}
              className="mt-2 max-w-full"
              onRemove={(attachmentId) =>
                edit.setRetainedStandaloneIds((ids) => {
                  const next = new Set(ids ?? []);
                  next.delete(attachmentId);
                  return next;
                })
              }
            />
          )}
          <div className="flex items-center justify-between gap-2 mt-2">
            <div className="min-w-0 flex-1">
              <CommentTriggerChips
                recipients={edit.recipients}
                blocked={edit.triggerPreview.blocked}
                hasAllMembersMention={edit.triggerPreview.hasAllMembersMention}
                draftContent={edit.content}
                onActionChange={edit.setRecipientAction}
              />
            </div>
            <div className="flex shrink-0 items-center gap-2">
              <FileUploadButton
                size="sm"
                multiple
                onSelect={(file) => edit.editorRef.current?.uploadFile(file)}
              />
              <Button size="sm" variant="ghost" onClick={edit.cancelEdit} disabled={edit.saving}>{t(($) => $.comment.cancel_edit)}</Button>
              <Button
                size="sm"
                variant="outline"
                onClick={edit.saveEdit}
                disabled={edit.saving || edit.uploading}
                aria-disabled={edit.uploading || undefined}
                aria-busy={edit.uploading || undefined}
              >
                {edit.saving && <Loader2 className="h-3.5 w-3.5 animate-spin" />}
                {edit.uploading ? edit.uploadingLabel : t(($) => $.comment.save_action)}
              </Button>
            </div>
          </div>
          {edit.isDragOver && <FileDropOverlay />}
        </div>
      ) : (
        <>
          <div tabIndex={currentUserId ? 0 : undefined} role="group"
            aria-label={t(($) => $.reply.annotations.source_label, { name: entry.actor_name || getActorName(entry.actor_type, entry.actor_id) })}
            data-comment-content={entry.id} data-find-unit={entry.id} className="pl-12 pr-4 max-md:pl-3 max-md:pr-3 text-body leading-relaxed text-foreground">
            <ReadonlyContent content={entry.content ?? ""} attachments={entry.attachments} />
          </div>
          <AttachmentList attachments={entry.attachments} content={entry.content} className="mt-1.5 pl-12 pr-4 max-md:pl-3 max-md:pr-3" />
          <div className="pl-12 pr-4 max-md:pl-3 max-md:pr-3">
            <SteerReceipts issueId={issueId} entry={entry} />
            {entry.actor_type === "agent" && entry.source_task_id && (
              <AppCallChips issueId={issueId} taskId={entry.source_task_id} />
            )}
          </div>
          {retryableAgentFailureComment(entry) && (
            <TaskCommentRetryButton
              issueId={issueId}
              taskId={entry.source_task_id}
              className="mt-2 pl-12 pr-4 max-md:pl-3 max-md:pr-3"
            />
          )}
        </>
      )}
      <div className="pl-12 pr-4 max-md:pl-3 max-md:pr-3">{runMetadata}</div>
      {!edit.editing && <ReactionBar
        reactions={reactions}
        showPicker={false}
        currentUserId={currentUserId}
        onToggle={(emoji) => onToggleReaction(entry.id, emoji)}
        getActorName={getActorName}
        className="mt-1.5 pl-12 pr-4 max-md:pl-3 max-md:pr-3"
      />}
    </div>
  );
}

/** Names the input a run answers once other rows separate the two (MUL-7628). */
function ReplyToQuote({ entry, onJump }: { entry: TimelineEntry; onJump?: (commentId: string) => void }) {
  const { t } = useT("issues");
  const { getActorName } = useActorName();
  const label = t(($) => $.comment.replying_to, {
    name: entry.actor_name || getActorName(entry.actor_type, entry.actor_id),
    preview: descriptionPreview(entry.content ?? ""),
  });
  return (
    <button type="button" disabled={!onJump} onClick={() => onJump?.(entry.id)} title={label}
      className="flex max-w-full min-w-0 items-center gap-1.5 rounded-xs text-left text-caption text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:hover:text-muted-foreground">
      <CornerUpLeft aria-hidden className="size-3 shrink-0" />
      <span className="min-w-0 truncate">{label}</span>
    </button>
  );
}

type AgentRunCommentProps = {
  run: CommentRun;
  managedAnchor?: boolean;
  standalone?: boolean;
  entering?: boolean;
  commentProps?: CommentCardProps;
  /** The input this run answers, shown when the thread no longer places the two together. */
  replyTo?: ReactNode;
  /** The flashed jump target: the slot's comment id, or the run's task id until it has one. */
  highlightedId?: string | null;
};

function shallowEqual<T extends object>(a: T | undefined, b: T | undefined): boolean {
  if (a === b) return true;
  if (!a || !b) return false;
  const keys = Object.keys(a) as (keyof T)[];
  if (keys.length !== Object.keys(b).length) return false;
  return keys.every((key) => Object.is(a[key], b[key]));
}

/**
 * The timeline rebuilds `commentProps` on every render, so a plain shallow
 * compare would never match. Its fields are the same stable values
 * `CommentCard` already memoizes on; compare them one level down. Without
 * this every agent reply on screen re-rendered on each Virtuoso range or
 * height update — every frame of a sidebar toggle (MUL-9426).
 */
function agentRunCommentPropsEqual(prev: AgentRunCommentProps, next: AgentRunCommentProps): boolean {
  const { commentProps: prevComment, ...prevRest } = prev;
  const { commentProps: nextComment, ...nextRest } = next;
  return shallowEqual(prevRest, nextRest) && shallowEqual(prevComment, nextComment);
}

/** A run without a persisted reply still belongs to the agent, never its trigger author. */
export const AgentRunComment = memo(AgentRunCommentImpl, agentRunCommentPropsEqual);

function AgentRunCommentImpl({ run, standalone = false, commentProps, entering = false, replyTo, highlightedId, managedAnchor = false }: AgentRunCommentProps) {
  const viewState = useInlineCommentRunState();
  const replyEntry = commentProps?.entry;
  // A deleted reply renders nothing in a run slot either (see CommentRow), so
  // the slot falls back to the run's own activity block: the run happened, and
  // it still carries `hasReply`, so it never prints the deleted body as its
  // output. A deleted thread ROOT keeps rendering — it heads its own thread.
  const replyHidden = !standalone && !!replyEntry && isDeletedComment(replyEntry);
  // The platform's failure notice only restates how the run ended, so the
  // run's own block takes its slot and says it once (MUL-7692). A standalone
  // notice that people replied to still heads its thread as a card.
  const notice = replyEntry && !replyHidden && isRunFailureNotice(replyEntry, run.task)
    && !(standalone && commentProps?.replies.some((entry) => !isDeletedComment(entry)))
    ? replyEntry : undefined;
  const reply = replyHidden || notice ? undefined : replyEntry;
  // The notice keeps its deep-link target and highlight in the run's slot.
  const slotEntry = reply ?? notice;
  const motionRef = useRunCommentMotion(entering, reply?.id, run.task.status);
  const highlighted = (highlightedId ?? commentProps?.highlightedCommentId) === (slotEntry?.id ?? run.task.id);
  return (
    <div ref={motionRef} data-run-slot-id={run.task.id}
      data-run-comment-id={!reply ? run.task.id : undefined}
      id={!standalone && !managedAnchor ? `comment-${slotEntry?.id ?? run.task.id}` : undefined}
      className={cn("transition-colors duration-700", standalone ? !reply && "rounded-xl border" : "border-t border-border/50", !reply && "py-1.5",
        highlighted ? highlightedCommentBackgroundClass : standalone && !reply && "bg-card")}>
      {commentProps && reply ? standalone ? (
        <CommentCard {...commentProps} runs={commentProps.runs ?? [run]} runViewState={viewState} />
      ) : (
        <CommentRow {...commentProps}
          isHighlighted={commentProps.highlightedCommentId === reply?.id}
          isResolution={!!reply?.resolved_at}
          replyTo={replyTo}
          runHeader={<PlacedInlineCommentRun run={run} viewState={viewState} presentation="header" />}
          runMetadata={<PlacedInlineCommentRun run={run} viewState={viewState} />} />
      ) : <div className="px-4 max-md:px-3">
        <InlineCommentRun run={run} viewState={viewState} showIdentity replyTo={replyTo} replacesFailureNotice={!!notice} />
      </div>}
    </div>
  );
}

// CommentCard — One Card per thread (parent + all replies flat inside)

// A quick action posts an ordinary comment and is rendered as one (MUL-5465).
// It briefly had a collapsed one-line header that expanded to reveal the
// prompt, on the theory that repeated runs would bury the discussion. In
// practice the prompts are short, the header restated what the body already
// said, and the disclosure only added a click between the reader and the text.
// Provenance still lives on `quick_action_id`; it is data, not decoration.

function CommentCardImpl({
  issueId,
  replyScrollParent,
  runs = EMPTY_COMMENT_RUNS,
  runViewState,
  enteringRunIds,
  entry,
  replies,
  currentUserId,
  canModerate = false,
  onReply,
  onReplyAccepted,
  onEdit,
  onDelete,
  onToggleReaction,
  onCreateSubIssue,
  onResolveToggle,
  onCopyLink,
  onJumpToComment,
  onCollapseResolved,
  expandedResolvedIds,
  onResolvedExpandChange,
  highlightedCommentId,
}: CommentCardProps) {
  const { t } = useT("issues");
  const locale = useLocale();
  const timeAgo = useTimeAgo();
  const { getActorName } = useActorName();
  const replyTarget = useCommentDraftStore((s) => s.drafts[`reply:${issueId}:${entry.id}`]?.replyTarget);
  const replyTargetId = replyTarget?.commentId ?? entry.id;
  // A deleted target's tombstone is as gone as a removed row.
  const isReplyTargetMissing = (id: string) => id !== entry.id
    && !replies.some((r) => r.id === id && !isDeletedComment(r));
  const replyTargetMissing = !!replyTarget && isReplyTargetMissing(replyTargetId);
  // A reply here goes into the running turn of a run that belongs to this
  // thread by default; another thread's turn is only reached by choice.
  const steerThreadRunByDefault = useCallback(
    (task: AgentTask) => runs.some((run) => run.task.id === task.id),
    [runs],
  );
  const annotation = useCommentAnnotations({
    issueId,
    draftKey: `reply:${issueId}:${entry.id}`,
    sources: [entry, ...replies].filter((e) => e.type === "comment" && !isDeletedComment(e))
      .map((e) => ({ id: e.id, name: e.actor_name || getActorName(e.actor_type, e.actor_id), revision: e.revision })),
    enabled: !!currentUserId,
    onOpen: () => { if (replyFolded) onResolvedExpandChange?.(entry.id, true); },
  });
  const isCollapsed = useCommentCollapseStore((s) => s.isCollapsed(issueId, entry.id));
  const toggleCollapse = useCommentCollapseStore((s) => s.toggle);
  const open = !isCollapsed;
  // One-shot WAAPI reveal on explicit user toggles only — Virtuoso scroll
  // remounts stay completely still.
  const bodyRevealRef = useRef<HTMLDivElement>(null);
  const cardElRef = useRef<HTMLDivElement | null>(null);
  const userToggledOpenRef = useRef(false);
  const userExpandedRepliesRef = useRef(false);
  const handleToggle = useCallback(() => {
    userToggledOpenRef.current = true;
    toggleCollapse(issueId, entry.id);
  }, [toggleCollapse, issueId, entry.id]);
  useLayoutEffect(() => {
    if (!userToggledOpenRef.current) return;
    userToggledOpenRef.current = false;
    if (!open) return;
    const anim = revealElement(bodyRevealRef.current, UI_MOTION_DURATION.fast * 1000, -3);
    return () => { anim?.cancel(); };
  }, [open]);

  const edit = useEditAttachmentState(issueId, entry, onEdit);

  const isOwn = entry.actor_type === "member" && entry.actor_id === currentUserId;
  const receipts = commentSupplementReceipts(entry);
  const canEditEntry = receipts.length === 0 && (isOwn || (canModerate && entry.actor_type === "member"));
  const canDeleteEntry = !receipts.some(isSupplementInFlight) && (isOwn || canModerate);
  const [confirmDelete, setConfirmDelete] = useState(false);
  const rowMenu = useRowMenu();
  const [reactionOpen, setReactionOpen] = useState(false);
  const afterClose = useAfterMenuClose();
  const replyBoxRef = useRef<ReplyInputHandle>(null);
  const revealReplyBox = () => {
    if (replyFolded) onResolvedExpandChange?.(entry.id, true);
    if (!open) toggleCollapse(issueId, entry.id);
  };
  const focusReply = () => {
    revealReplyBox();
    requestAnimationFrame(() => replyBoxRef.current?.focus());
  };
  const menuGroups = useCommentMenuGroups({
    entry,
    editing: edit.editing,
    canEdit: canEditEntry,
    canDelete: canDeleteEntry,
    resolve: onResolveToggle && (entry.resolved_at
      ? { icon: <RotateCcw className="h-3.5 w-3.5" />, label: t(($) => $.comment.resolve.unresolve_thread_action), onSelect: () => onResolveToggle(entry.id, false) }
      : { icon: <CheckCircle2 className="h-3.5 w-3.5" />, label: t(($) => $.comment.resolve.resolve_thread_action), onSelect: () => onResolveToggle(entry.id, true) }),
    onStartEdit: edit.startEdit,
    onReply: currentUserId ? afterClose.schedule(focusReply) : undefined,
    onAddReaction: afterClose.schedule(() => setReactionOpen(true)),
    onCopyLink,
    onCreateSubIssue,
    onDelete: () => setConfirmDelete(true),
  });
  const rootScopeRef = useCommentScope(entry, !open || isDeletedComment(entry) || edit.editing ? null : menuGroups, onCreateSubIssue);
  // Selected text gets the same "Add to reply" as the selection bubble.
  const threadScopeRef = useContextMenuScope(() => currentUserId ? {
    selectionActions: () => [
      {
        key: "annotate",
        icon: <MessageSquarePlus className="h-3.5 w-3.5" />,
        label: t(($) => $.reply.annotations.add),
        onSelect: () => { annotation.addSelection(); },
      },
    ],
  } : null);
  const annotationCardRef = annotation.cardRef;
  const cardRef = useCallback((node: HTMLDivElement | null) => {
    cardElRef.current = node;
    annotationCardRef(node);
    threadScopeRef(node);
  }, [annotationCardRef, threadScopeRef]);

  const allNestedReplies = replies;
  // What the thread shows. Tombstones are excluded from display and counts but
  // stay in `allNestedReplies`, which run anchoring and "has replies" reason
  // over. Every tombstone has at least one live descendant (the server prunes
  // one that loses its last reply), so no content hides behind this.
  const visibleReplies = allNestedReplies.filter((reply) => !isDeletedComment(reply));
  // A failure notice heading a thread keeps the thread's header; its run line
  // below says how the run ended, so the raw body and its button are dropped.
  const noticeRun = runs.find((run) => run.hasReply && run.commentId === entry.id && isRunFailureNotice(entry, run.task));
  const renderRuns = (commentId: string, presentation: "inline" | "header" = "inline") => runs.filter((run) => run.commentId === commentId && run.hasReply
    && (!run.anchorCommentId || run.anchorCommentId === commentId || replyFolded))
    .map((run) => <PlacedInlineCommentRun key={run.task.id} run={run} presentation={presentation}
      viewState={run.commentId === entry.id ? runViewState : undefined} replacesFailureNotice={run === noticeRun} />);

  // A run slot renders the run's latest comment, or its activity until it
  // posts one.
  const renderThreadRow = (row: TimelineEntry | ThreadRunSlot, managedAnchor = false) => "run" in row ? (
    <AgentRunComment key={row.run.task.id} run={row.run} managedAnchor={managedAnchor} entering={enteringRunIds?.has(row.run.task.id)}
      highlightedId={highlightedCommentId}
      replyTo={row.replyTo && <ReplyToQuote entry={row.replyTo} onJump={onJumpToComment} />}
      commentProps={row.reply ? {
        issueId, entry: row.reply, replies: [], currentUserId, canModerate, onReply, onEdit, onDelete,
        onToggleReaction, onCreateSubIssue, onResolveToggle, onCopyLink, highlightedCommentId, enteringRunIds,
      } : undefined} />
  ) : renderReply(row, managedAnchor);

  const renderReply = (reply: TimelineEntry, managedAnchor: boolean) => (
    <Fragment key={reply.id}>
      {/* A tombstone keeps its place in the thread but contributes no row
          of its own. */}
      {!isDeletedComment(reply) && (
        <div
          data-mc="comment-reply"
          data-author-type={reply.actor_type}
          id={managedAnchor ? undefined : `comment-${reply.id}`}
          className={cn(
            "border-t border-border/50 transition-colors duration-700",
            highlightedCommentId === reply.id && highlightedCommentBackgroundClass,
          )}
        >
          <CommentRow
            issueId={issueId}
            entry={reply}
            runHeader={renderRuns(reply.id, "header")}
            runMetadata={renderRuns(reply.id)}
            currentUserId={currentUserId}
            canModerate={canModerate}
            isResolution={reply.id === replyResolutionId}
            isHighlighted={highlightedCommentId === reply.id}
            hasReplies={repliedToIds.has(reply.id)}
            onEdit={onEdit}
            onDelete={onDelete}
            onToggleReaction={onToggleReaction}
            onCreateSubIssue={onCreateSubIssue}
            onResolveToggle={onResolveToggle}
            onCopyLink={onCopyLink}
            onFocusReply={currentUserId ? focusReply : undefined}
          />
        </div>
      )}
    </Fragment>
  );

  const replyCount = visibleReplies.length;
  const repliedToIds = new Set(allNestedReplies.map((reply) => reply.parent_id));
  const deleted = isDeletedComment(entry);
  const contentPreview = (entry.content ?? "").replace(/\n/g, " ").slice(0, 80);
  const reactions = entry.reactions ?? [];

  const isHighlighted = highlightedCommentId === entry.id;
  const toggleLabel = open ? t(($) => $.comment.collapse_thread) : t(($) => $.comment.expand_thread);

  // Reply-resolution display. When a REPLY is the thread's resolution, the other
  // replies fold behind a bar and the resolution stays visible (root-resolution
  // is handled one level up in issue-detail's resolved-bar, so kind "root" here
  // renders the normal full thread under the Collapse header).
  const resolution = deriveThreadResolution(entry, allNestedReplies);
  const replyResolutionId = resolution.kind === "reply" ? resolution.resolutionId : null;
  const threadExpanded = !!expandedResolvedIds?.has(entry.id);
  const replyFolded = replyResolutionId != null && !threadExpanded;
  useLayoutEffect(() => {
    if (!userExpandedRepliesRef.current) return;
    userExpandedRepliesRef.current = false;
    if (replyFolded) return;
    const replies = cardElRef.current?.querySelectorAll<HTMLElement>('[data-mc="comment-reply"]');
    if (!replies || replies.length === 0) return;
    const anims: Animation[] = [];
    for (const el of replies) {
      const anim = revealElement(el, UI_MOTION_DURATION.fast * 1000, -3);
      if (anim) anims.push(anim);
    }
    return () => { for (const anim of anims) anim.cancel(); };
  }, [replyFolded]);
  const foldedReplies = replyResolutionId
    ? visibleReplies.filter((r) => r.id !== replyResolutionId)
    : visibleReplies;
  // Replies and run slots in the order they happened (MUL-7628). Folded, the
  // resolution stays visible along with any run that has not replied yet.
  const threadRows = orderVisibleThreadRows(entry, allNestedReplies, runs, replyFolded ? replyResolutionId : null);

  // Long threads mount replies on demand: many rows, or few rows carrying a
  // lot of Markdown (MUL-9409). Short threads render inline, but through the
  // same wrapper, so crossing the threshold never remounts a row (an open
  // reply editor survives a new long report arriving).
  const virtualizeRows = !!replyScrollParent && shouldVirtualizeThreadRows(
    threadRows.map((row) => ("run" in row ? row.reply?.content : row.content)?.length ?? 0),
  );
  const renderRows = () => threadRows.map((row) => {
    if (!replyScrollParent) return renderThreadRow(row);
    if (!("run" in row) && isDeletedComment(row)) return null;
    const anchorId = "run" in row
      ? (row.reply && !isDeletedComment(row.reply) ? row.reply.id : row.run.task.id)
      : row.id;
    const rowCommentId = "run" in row
      ? (row.reply && !isDeletedComment(row.reply) ? row.reply.id : undefined)
      : row.id;
    const rowEngaged = !!rowCommentId && annotation.activeSourceCommentId === rowCommentId;
    // Keyed like the inline rows: a run slot keeps its task key when its reply
    // arrives, so the slot (and its open activity panel) stays mounted.
    return <VirtualThreadRow key={"run" in row ? row.run.task.id : row.id} id={`comment-${anchorId}`} virtualize={virtualizeRows}
      engaged={rowEngaged} taskId={"run" in row ? row.run.task.id : undefined} scrollParent={replyScrollParent}>
      {() => renderThreadRow(row, virtualizeRows)}
    </VirtualThreadRow>;
  });

  // Pin the root comment's header to the timeline's scroll parent while the
  // thread is open, so a LONG root comment keeps its author + actions visible
  // as you scroll its body (overflow-clip on the Card anchors this to the
  // timeline, not the card — see below). The root-section wrapper below scopes
  // its containing block to the header + body, so it releases the moment the
  // replies begin — exactly one header is pinned at a time. Each reply pins its
  // header the same way, scoped to its own row (see CommentRow). Skip the root
  // header whenever a resolution collapse bar already owns the top-0 sticky slot
  // (root resolved + expanded, or reply-resolution expanded): two sticky bars at
  // the same offset would stack and hide one.
  const stickyHeader =
    open && !onCollapseResolved && !(replyResolutionId != null && threadExpanded);

  return (
    // overflow-clip (not -hidden) clips the rounded corners WITHOUT creating a
    // scroll container, so the sticky collapse affordances below resolve to the
    // timeline's scroll parent instead of this card. See PR #3623.
    <Card data-mc="comment" data-author-type={entry.actor_type} ref={cardRef} {...annotation.captureProps} className="!py-0 !gap-0 overflow-clip transition-colors duration-700">
      {annotation.popup}
      {onCollapseResolved && (
        <button
          type="button"
          onClick={onCollapseResolved}
          data-thread-sticky-bar
          className="sticky top-0 z-20 flex w-full items-center gap-2.5 border-b border-border/50 bg-muted px-4 max-md:px-3 py-2.5 text-left text-body text-muted-foreground transition-colors cursor-pointer hover:bg-accent hover:text-accent-foreground"
          aria-label={t(($) => $.comment.resolve.collapse)}
        >
          <ListChevronsDownUp className="h-3.5 w-3.5" />
          {t(($) => $.comment.resolve.collapse)}
        </button>
      )}
      {/* root-section — the sticky header's containing block. It wraps ONLY
            the header + root body, so the header releases the moment you scroll
            past the body into the replies (which render OUTSIDE this wrapper).
            That is what keeps exactly one header pinned at a time: without this
            wrapper the header's containing block is the whole thread and it
            stays stuck behind every reply. */}
        <div
          ref={rootScopeRef}
          data-comment-block
          className={cn("transition-colors duration-700", isHighlighted && highlightedCommentBackgroundClass)}
          // Same right-click as a reply's; only while the thread is open, since
          // the menu lives in the open header.
          onContextMenu={(e) => {
            if (!open || deleted || edit.editing || !shouldOpenRowMenu(e, { deferToContent: true })) return;
            rowMenu.openAtPointer(e);
          }}
        >
          {/* Keep the author aligned with replies; thread controls sit on the right. */}
          <StickyHeaderShell
            sticky={stickyHeader}
            highlighted={isHighlighted}
            className={cn("px-4 max-md:px-3", !open ? "py-3" : stickyHeader ? "pt-3 pb-1" : "pt-3 pb-2")}
          >
            <div className="flex items-center gap-2.5">
              {deleted ? (
                // The thread's root was deleted; its replies still hang off it.
                <span className="min-w-0 truncate text-body italic text-muted-foreground">
                  {t(($) => $.comment.deleted_placeholder)}
                </span>
              ) : (
                <>
                  <ActorAvatar
                    actorType={entry.actor_type}
                    actorId={entry.actor_id}
                    name={entry.actor_name}
                    avatarUrl={entry.actor_avatar_url}
                    profileRequiresDirectoryEntry
                    size="md"
                    enableHoverCard
                    showStatusDot
                  />
                  <span className="shrink-0 cursor-pointer text-body font-medium">
                    {entry.actor_name || getActorName(entry.actor_type, entry.actor_id)}
                  </span>
                  <Tooltip>
                    <TooltipTrigger
                      render={
                        <span className="shrink-0 text-label text-muted-foreground cursor-default">
                          {timeAgo(entry.created_at)}
                        </span>
                      }
                    />
                    <TooltipContent side="top">
                      {new Date(entry.created_at).toLocaleString(locale)}
                    </TooltipContent>
                  </Tooltip>

                  <SteerBadge issueId={issueId} entry={entry} />
                  {renderRuns(entry.id, "header")}
                  {entry.actor_type === "agent" && entry.source_task_id && (
                    <WakeupSourceChip issueId={issueId} taskId={entry.source_task_id} />
                  )}
                </>
              )}

              {!open && contentPreview && (
                <span className="min-w-0 flex-1 truncate text-label text-muted-foreground">
                  {contentPreview}
                </span>
              )}
              {!open && replyCount > 0 && (
                <span className="shrink-0 text-label text-muted-foreground">
                  {t(($) => $.comment.reply_count, { count: replyCount })}
                </span>
              )}

              <div data-comment-actions className="ml-auto flex shrink-0 items-center gap-0.5">
                {open && !deleted && <>
                  {!edit.editing && <QuickEmojiPicker
                    onSelect={(emoji) => onToggleReaction(entry.id, emoji)}
                    ariaLabel={t(($) => $.comment.add_reaction)}
                    align="end"
                    open={reactionOpen}
                    onOpenChange={setReactionOpen}
                    className={buttonVariants({ variant: "ghost", size: "icon-sm", className: commentActionClassName })}
                  />}
                  <DropdownMenu open={rowMenu.menu.open} onOpenChange={rowMenu.onOpenChange}>
                    <DeferredTooltip
                      content={t(($) => $.comment.more_actions)}
                      trigger={
                        <DropdownMenuTrigger
                          render={
                            <Button
                              variant="ghost"
                              size="icon-sm"
                              className={commentActionClassName}
                              aria-label={t(($) => $.comment.more_actions)}
                            >
                              <MoreHorizontal className="h-4 w-4" aria-hidden />
                            </Button>
                          }
                        />
                      }
                    />
                    <DropdownMenuContent
                      align={rowMenu.menu.point ? "start" : "end"}
                      anchor={rowMenu.anchor}
                      finalFocus={afterClose.finalFocus}
                      className="w-auto min-w-44"
                    >
                      <MenuActionGroups groups={menuGroups} primitives={dropdownPrimitives} />
                    </DropdownMenuContent>
                  </DropdownMenu>
                  <DeleteCommentDialog
                    open={confirmDelete}
                    onOpenChange={setConfirmDelete}
                    onConfirm={() => onDelete(entry.id)}
                    hasReplies={replyCount > 0}
                  />
                </>}
                <DeferredTooltip
                  content={toggleLabel}
                  trigger={
                    <Button
                      type="button"
                      variant="ghost"
                      size="icon-sm"
                      aria-label={toggleLabel}
                      aria-expanded={open}
                      aria-controls={open ? `root-comment-body-${entry.id}` : undefined}
                      onClick={handleToggle}
                      className={commentActionClassName}
                    >
                      <ChevronRight aria-hidden className={cn("h-3.5 w-3.5 transition-transform motion-reduce:transition-none", open && "rotate-90")} />
                    </Button>
                  }
                />
              </div>
            </div>
          </StickyHeaderShell>

        {/* Root comment body. Avoid Base UI's Panel here: every mounted panel
            probes computed styles to detect animations, forcing a style
            recalculation across long issue-detail documents. Its id must not
            start with "comment-": timeline lookups resolve the nearest
            `[id^="comment-"]` ancestor to a comment id, so a reading anchor
            taken inside the body would name a comment that does not exist. */}
        {open && !deleted && (
          <div ref={bodyRevealRef} id={`root-comment-body-${entry.id}`} className="px-4 max-md:px-3 pb-3">
            {edit.editing ? (
              <div
                {...edit.dropZoneProps}
                className="relative pl-8 max-md:pl-0"
                onKeyDown={(e) => { if (e.key === "Escape") edit.cancelEdit(); }}
              >
                <div className="text-body leading-relaxed">
                  <ContentEditor
                    ref={edit.editorRef}
                    defaultValue={edit.initialValue}
                    placeholder={t(($) => $.comment.edit_placeholder)}
                    onUpdate={(md) => {
                      edit.setContent(md);
                      if (md.trim().length > 0) edit.setDraft(edit.draftKey, md);
                      else edit.clearDraft(edit.draftKey);
                    }}
                    onSubmit={edit.saveEdit}
                    onUploadFile={edit.handleUpload}
                    onUploadingChange={edit.onUploadingChange}
                    debounceMs={100}
                    currentIssueId={issueId}
                    attachments={edit.editorAttachments}
                  />
                </div>
                {edit.storageRefusal && (
                  <StorageLimitNotice refusal={edit.storageRefusal} onDismiss={edit.dismissStorageRefusal} className="mt-2" />
                )}
                {edit.revisionConflict ? (
                  <CommentRevisionConflict
                    serverContent={entry.content ?? ""}
                    localContent={edit.content}
                    onKeepLocal={() => void edit.saveEdit()}
                    onUseServer={edit.adoptServerVersion}
                    saving={edit.saving}
                  />
                ) : null}
                <div className="flex items-center justify-between mt-2">
                  <div className="flex min-w-0 flex-1 flex-col gap-1">
                    {edit.standaloneEditAttachments.length > 0 && (
                      <AttachmentList
                        attachments={edit.standaloneEditAttachments}
                        className="max-w-full"
                        onRemove={(attachmentId) =>
                          edit.setRetainedStandaloneIds((ids) => {
                            const next = new Set(ids ?? []);
                            next.delete(attachmentId);
                            return next;
                          })
                        }
                        />
                      )}
                    <CommentTriggerChips
                      recipients={edit.recipients}
                      blocked={edit.triggerPreview.blocked}
                      hasAllMembersMention={edit.triggerPreview.hasAllMembersMention}
                      draftContent={edit.content}
                      onActionChange={edit.setRecipientAction}
                    />
                    <FileUploadButton
                      size="sm"
                      multiple
                      onSelect={(file) => edit.editorRef.current?.uploadFile(file)}
                    />
                  </div>
                  <div className="flex items-center gap-2">
                    <Button size="sm" variant="ghost" onClick={edit.cancelEdit} disabled={edit.saving}>{t(($) => $.comment.cancel_edit)}</Button>
                    <Button
                      size="sm"
                      variant="outline"
                      onClick={edit.saveEdit}
                      disabled={edit.saving || edit.uploading}
                      aria-disabled={edit.uploading || undefined}
                      aria-busy={edit.uploading || undefined}
                    >
                      {edit.saving && <Loader2 className="h-3.5 w-3.5 animate-spin" />}
                      {edit.uploading ? edit.uploadingLabel : t(($) => $.comment.save_action)}
                    </Button>
                  </div>
                </div>
                {edit.isDragOver && <FileDropOverlay />}
              </div>
            ) : !noticeRun && (
              <>
                <div tabIndex={currentUserId ? 0 : undefined} role="group"
            aria-label={t(($) => $.reply.annotations.source_label, { name: entry.actor_name || getActorName(entry.actor_type, entry.actor_id) })}
            data-comment-content={entry.id} data-find-unit={entry.id} className="pl-8 max-md:pl-0 text-body leading-relaxed text-foreground">
                  <ReadonlyContent content={entry.content ?? ""} attachments={entry.attachments} />
                </div>
                <AttachmentList attachments={entry.attachments} content={entry.content} className="mt-1.5 pl-8 max-md:pl-0" />
                <div className="pl-8 max-md:pl-0">
                  <SteerReceipts issueId={issueId} entry={entry} />
                  {entry.actor_type === "agent" && entry.source_task_id && (
                    <AppCallChips issueId={issueId} taskId={entry.source_task_id} />
                  )}
                </div>
                {retryableAgentFailureComment(entry) && (
                  <TaskCommentRetryButton
                    issueId={issueId}
                    taskId={entry.source_task_id}
                    className="mt-2 pl-8 max-md:pl-0"
                  />
                )}
              </>
            )}
            <div className="pl-8 max-md:pl-0">{renderRuns(entry.id)}</div>
            {!edit.editing && <ReactionBar
              reactions={reactions}
              showPicker={false}
              currentUserId={currentUserId}
              onToggle={(emoji) => onToggleReaction(entry.id, emoji)}
              getActorName={getActorName}
              className="mt-1.5 pl-8 max-md:pl-0"
            />}
          </div>
        )}
        </div>

        {/* Replies + reply input — rendered OUTSIDE root-section so the root
            header's sticky containing block ends with the body. Gated on `open`
            to mirror the body Panel's collapse visibility. */}
        {open && (
          <>
          {replyFolded ? (
            <>
              {/* reply-mode folded: other replies behind a bar, resolution pinned below */}
              {foldedReplies.length > 0 && (
                <div className="border-t border-border/50 px-4 max-md:px-3 py-2.5">
                  <CommentsFoldBar
                    replies={foldedReplies}
                    onExpand={() => {
                      userExpandedRepliesRef.current = true;
                      onResolvedExpandChange?.(entry.id, true);
                    }}
                  />
                </div>
              )}
              {renderRows()}
            </>
          ) : (
            <>
              {/* reply-mode expanded: a Collapse affordance to fold back */}
              {replyResolutionId != null && onResolvedExpandChange && (
                <button
                  type="button"
                  onClick={() => onResolvedExpandChange(entry.id, false)}
                  data-thread-sticky-bar
                  className="sticky top-0 z-20 flex w-full items-center gap-2.5 border-t border-border/50 bg-muted px-4 max-md:px-3 py-2.5 text-left text-body text-muted-foreground transition-colors cursor-pointer hover:bg-accent hover:text-accent-foreground"
                  aria-label={t(($) => $.comment.resolve.collapse)}
                >
                  <ListChevronsDownUp className="h-3.5 w-3.5" />
                  {t(($) => $.comment.resolve.collapse)}
                </button>
              )}
              {/* Replies — chronological; the resolution keeps its place with a badge */}
              {renderRows()}

              {/* Reply input */}
              <div className="border-t border-border/50 px-4 max-md:px-3 py-2.5">
                <ReplyInput
                  handleRef={replyBoxRef}
                  issueId={issueId}
                  parentId={replyTargetId}
                  targetMissing={replyTargetMissing}
                  annotationComposer={annotation.composer}
                  placeholder={t(($) => $.reply.placeholder)}
                  size="sm"
                  avatarType="member"
                  avatarId={currentUserId ?? ""}
                  draftKey={`reply:${issueId}:${entry.id}`}
                  onEditAnnotation={(id) => annotation.editAnnotation(id, true)}
                  steerByDefault={steerThreadRunByDefault}
                  onSubmit={(content, attachmentIds, suppressAgentIds, steerTaskIds) => {
                    // The popup can save the first annotation and submit in the same event.
                    const targetId = useCommentDraftStore.getState().drafts[`reply:${issueId}:${entry.id}`]?.replyTarget?.commentId ?? entry.id;
                    return isReplyTargetMissing(targetId) ? Promise.resolve(false)
                      : onReply(targetId, content, attachmentIds, suppressAgentIds, steerTaskIds);
                  }}
                  onAccepted={onReplyAccepted}
                />
              </div>
            </>
          )}
          </>
        )}
    </Card>
  );
}

// Memoized so a long timeline (e.g. Inbox-embedded IssueDetail with thousands
// of comments) does not re-render every card on each parent state update or
// WS-driven cache refresh. Default shallow comparison is sufficient: the
// timeline grouping is useMemo'd in issue-detail.tsx (stable Map ref), and
// every callback is stabilized via useCallback in use-issue-timeline.ts.
const CommentCard = memo(CommentCardImpl);

export { CommentCard, type CommentCardProps };