Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

33.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { SegmentedPill } from "@/components/segmented-control";
import { Button } from "@/components/ui/button";
import {
  InputGroup,
  InputGroupAddon,
  InputGroupInput,
} from "@/components/ui/input-group";
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { useCopyFeedback } from "@/features/hub/hooks/use-copy-feedback";
import { formatBytes } from "@/features/hub";
import { useT } from "@/i18n";
import { isTauri } from "@/lib/api-base";
import { stripAnsi } from "@/lib/strip-ansi";
import { toast } from "@/lib/toast";
import { cn } from "@/lib/utils";
import {
  Alert02Icon,
  Download01Icon,
  InformationCircleIcon,
  Refresh01Icon,
  Search01Icon,
  Shield01Icon,
} from "@hugeicons/core-free-icons";
import { LineWrapDisableIcon, LineWrapIcon } from "@/lib/hugeicons-derived";
import { FolderOpenIcon } from "@/lib/folder-icons";
import { Copy01Icon } from "@/lib/copy-icon";
import { HugeiconsIcon } from "@hugeicons/react";
import { ArrowDownIcon } from "lucide-react";
import {
  type KeyboardEvent as ReactKeyboardEvent,
  type PointerEvent as ReactPointerEvent,
  useCallback,
  useEffect,
  useId,
  useMemo,
  useRef,
  useState,
} from "react";
import {
  type DebugLogSource,
  LogExportError,
  exportAllLogs,
  loadDebugLog,
  loadDebugLogSources,
  openLogsFolder,
  revealSavedArchive,
} from "../api/debug-logs";
import { SettingsRow } from "../components/settings-row";
import { SettingsSection } from "../components/settings-section";
import {
  DEFAULT_REFRESH_MODE,
  EMPTY_BUFFER,
  type LogBufferState,
  REFRESH_MODE_STORAGE_KEY,
  REQUEST_TIMEOUT_MS,
  type RefreshMode,
  applyLogChunk,
  clearSelectionInside,
  isPageStale,
  nextDroppedState,
  parseRefreshMode,
  isRequestTimeout,
  pollDelayMs,
  selectionInside,
  withRequestTimeout,
} from "../lib/debug-log-buffer";
import { isAbort, isLogSourceGone } from "../lib/debug-log-error";
import {
  NO_PENDING_LOG_REQUEST,
  pendingLogRequestKey,
  useSettingsDialogStore,
} from "../stores/settings-dialog-store";
import { Tick02Icon } from "@/lib/tick-icon";

const MODES: RefreshMode[] = ["live", "3s", "manual"];

const SOURCE_RESCAN_MS = 10_000;

const FOLLOW_THRESHOLD_PX = 40;

const PANE_HEIGHT_STORAGE_KEY = "unsloth_debug_log_pane_height";
const PANE_MIN_HEIGHT_PX = 160;
const PANE_KEY_STEP_PX = 24;

function maxPaneHeight(): number {
  return Math.round(window.innerHeight * 0.85);
}

function readStoredPaneHeight(): number | null {
  if (typeof window === "undefined") {
    return null;
  }
  try {
    const stored = Number(window.localStorage.getItem(PANE_HEIGHT_STORAGE_KEY));
    return Number.isFinite(stored) && stored >= PANE_MIN_HEIGHT_PX
      ? stored
      : null;
  } catch {
    return null;
  }
}

function readStoredMode(): RefreshMode {
  if (typeof window === "undefined") return DEFAULT_REFRESH_MODE;
  try {
    return parseRefreshMode(
      window.localStorage.getItem(REFRESH_MODE_STORAGE_KEY),
    );
  } catch {
    return DEFAULT_REFRESH_MODE;
  }
}

function groupByFamily(
  sources: DebugLogSource[],
): { family: string; sources: DebugLogSource[] }[] {
  const groups = new Map<string, DebugLogSource[]>();
  for (const source of sources) {
    const group = groups.get(source.family);
    if (group) group.push(source);
    else groups.set(source.family, [source]);
  }
  return [...groups].map(([family, members]) => ({ family, sources: members }));
}

function NoticeStrip({
  tone,
  children,
  testId,
}: {
  tone: "warning" | "info";
  children: string;
  testId?: string;
}) {
  return (
    <p
      data-testid={testId}
      className={cn(
        "flex items-start gap-2 text-xs leading-snug",
        tone === "warning"
          ? "text-amber-600 dark:text-amber-400"
          : "text-muted-foreground",
      )}
    >
      {tone === "warning" ? (
        <HugeiconsIcon
          strokeWidth={1.75}
          icon={Alert02Icon}
          className="mt-px size-3.5 shrink-0"
        />
      ) : null}
      {children}
    </p>
  );
}

export function DebuggingTab() {
  const t = useT();
  const pillId = useId();
  const [sources, setSources] = useState<DebugLogSource[]>([]);
  const [sourceId, setSourceId] = useState<string | null>(null);
  const [mode, setMode] = useState<RefreshMode>(readStoredMode);
  const [buffer, setBuffer] = useState<LogBufferState>(EMPTY_BUFFER);
  const [realpath, setRealpath] = useState<string | null>(null);
  const [logRoot, setLogRoot] = useState<string | null>(null);
  const [notice, setNotice] = useState<string | null>(null);
  const [dropped, setDropped] = useState(false);
  const [morePending, setMorePending] = useState(false);
  const [staleSession, setStaleSession] = useState(false);
  const [exporting, setExporting] = useState(false);
  const [revealing, setRevealing] = useState(false);
  const [filter, setFilter] = useState("");
  const [wrap, setWrap] = useState(true);
  const [following, setFollowing] = useState(true);
  const { copied, copy } = useCopyFeedback();
  const { copied: pathCopied, copy: copyPath } = useCopyFeedback();

  const cursorRef = useRef<string | null>(null);
  const selectionRef = useRef(0);
  // The selection in flight, not a flag, so a slow read of the old source cannot swallow the new poll.
  const inFlightRef = useRef<number | null>(null);
  const paneRef = useRef<HTMLPreElement | null>(null);
  const frameRef = useRef<HTMLDivElement | null>(null);
  const [paneHeight, setPaneHeight] = useState<number | null>(
    readStoredPaneHeight,
  );
  const pinnedRef = useRef(true);
  const lastSourceScanRef = useRef(Date.now());

  useEffect(() => {
    try {
      window.localStorage.setItem(REFRESH_MODE_STORAGE_KEY, mode);
    } catch {
    }
  }, [mode]);

  const sourceFetchSeqRef = useRef(0);
  const appliedSourceFetchRef = useRef(0);

  const refreshSources = useCallback(
    async (options: { signal?: AbortSignal; reselect?: boolean } = {}) => {
      const seq = ++sourceFetchSeqRef.current;
      try {
        const requestedFor = pendingLogRequestKey(
          useSettingsDialogStore.getState(),
        );
        const pendingPath =
          useSettingsDialogStore.getState().logSourcePathRequested;
        const result = await withRequestTimeout(
          (signal) => loadDebugLogSources(signal, pendingPath),
          REQUEST_TIMEOUT_MS,
          options.signal,
        );
        if (seq < appliedSourceFetchRef.current) return;
        setSources(result.sources);
        setLogRoot(result.logRoot);
        const dialog = useSettingsDialogStore.getState();
        const requested = dialog.logFamilyRequested;
        const byPath = result.matchedSourceId
          ? result.sources.find(
              (source) => source.id === result.matchedSourceId,
            )
          : undefined;
        const fromFailure =
          byPath ??
          (requested
            ? result.sources.find((source) => source.family === requested)
            : undefined);
        const stillTheSameRequest =
          pendingLogRequestKey(dialog) === requestedFor;
        if (fromFailure && stillTheSameRequest)
          useSettingsDialogStore.getState().consumeLogFamilyRequest();
        if (fromFailure && !stillTheSameRequest) return;
        appliedSourceFetchRef.current = seq;
        setSourceId((current) =>
          fromFailure
            ? fromFailure.id
            : options.reselect
              ? result.defaultSourceId
              : (current ?? result.defaultSourceId),
        );
      } catch {
      }
    },
    [],
  );

  useEffect(() => {
    const controller = new AbortController();
    void refreshSources({ signal: controller.signal });
    return () => controller.abort();
  }, [refreshSources]);

  const pendingLogRequest = useSettingsDialogStore(pendingLogRequestKey);
  useEffect(() => {
    if (pendingLogRequest === NO_PENDING_LOG_REQUEST) return;
    const controller = new AbortController();
    void refreshSources({ signal: controller.signal });
    return () => controller.abort();
  }, [pendingLogRequest, refreshSources]);

  const onPollFailed = useCallback(
    async (error: unknown, signal?: AbortSignal) => {
      if (isAbort(error)) return;
      if (isRequestTimeout(error)) {
        setNotice(t("settings.debugging.timeout"));
        return;
      }
      if (isLogSourceGone(error)) {
        // Default reselection terminates: an empty source list returns 200, not another 404.
        cursorRef.current = null;
        await refreshSources({ signal, reselect: true });
        return;
      }
      setNotice((error as Error).message);
    },
    [refreshSources, t],
  );

  const rescanSourcesIfStale = useCallback(
    async (signal?: AbortSignal) => {
      if (Date.now() - lastSourceScanRef.current < SOURCE_RESCAN_MS) return;
      lastSourceScanRef.current = Date.now();
      await refreshSources({ signal });
    },
    [refreshSources],
  );

  const poll = useCallback(
    async (signal?: AbortSignal) => {
      const selection = selectionRef.current;
      if (inFlightRef.current === selection) return;
      inFlightRef.current = selection;
      try {
        const page = await withRequestTimeout(
          (requestSignal) =>
            loadDebugLog({
              sourceId,
              cursor: cursorRef.current,
              signal: requestSignal,
            }),
          REQUEST_TIMEOUT_MS,
          signal,
        );
        if (
          isPageStale({
            requestSelection: selection,
            currentSelection: selectionRef.current,
            requestSourceId: sourceId,
            pageSourceId: page.sourceId,
          })
        )
          return;
        cursorRef.current = page.cursor;
        if (page.realpath) setRealpath(page.realpath);
        setDropped((previous) => nextDroppedState(previous, page));
        setMorePending(page.morePending);
        setStaleSession(page.fileLoggingDisabled);
        setNotice(
          page.status === "ok" || page.status === "empty"
            ? null
            : (page.reason ?? t(`settings.debugging.${page.status}` as never)),
        );
        setBuffer((previous) =>
          applyLogChunk(previous, {
            lines: page.lines,
            cursor: page.cursor,
            reset: page.reset,
          }),
        );
      } catch (error) {
        if (selection === selectionRef.current)
          await onPollFailed(error, signal);
      } finally {
        if (inFlightRef.current === selection) inFlightRef.current = null;
      }
    },
    [onPollFailed, sourceId, t],
  );

  useEffect(() => {
    clearSelectionInside(document.getSelection(), paneRef.current);
    selectionRef.current += 1;
    cursorRef.current = null;
    setBuffer(EMPTY_BUFFER);
    setRealpath(null);
    setDropped(false);
    setMorePending(false);
    setStaleSession(false);
    setNotice(null);
  }, [sourceId]);

  useEffect(() => {
    const controller = new AbortController();
    let timer: number | undefined;
    let stopped = false;

    const tick = async () => {
      if (stopped) return;
      if (
        typeof document === "undefined" ||
        document.visibilityState !== "hidden"
      ) {
        await rescanSourcesIfStale(controller.signal);
        await poll(controller.signal);
      }
      if (stopped) return;
      const delay = pollDelayMs(mode);
      if (delay !== null) timer = window.setTimeout(tick, delay);
    };

    void tick();
    return () => {
      stopped = true;
      controller.abort();
      if (timer !== undefined) window.clearTimeout(timer);
    };
  }, [mode, poll, rescanSourcesIfStale]);

  const plainLines = useMemo(() => buffer.lines.map(stripAnsi), [buffer.lines]);
  const trimmedFilter = filter.trim().toLowerCase();
  const visibleLines = useMemo(
    () =>
      trimmedFilter
        ? plainLines.filter((line) =>
            line.toLowerCase().includes(trimmedFilter),
          )
        : plainLines,
    [plainLines, trimmedFilter],
  );

  const text = useMemo(() => visibleLines.join("\n"), [visibleLines]);

  // Keep presses in a ref: rendering before the drag's first selection collapses it in Chromium.
  const [holding, setHolding] = useState(false);
  const pointerDownRef = useRef(false);
  const textRef = useRef(text);
  textRef.current = text;
  const [shownText, setShownText] = useState(text);
  if (!holding && !pointerDownRef.current && shownText !== text)
    setShownText(text);

  useEffect(() => {
    const inside = () =>
      selectionInside(document.getSelection(), paneRef.current);
    const update = () => {
      const held = inside();
      if (pointerDownRef.current && !held) return;
      setHolding(held);
    };
    const release = () => {
      if (!pointerDownRef.current) return;
      pointerDownRef.current = false;
      const held = inside();
      setHolding(held);
      if (!held) setShownText(textRef.current);
    };
    const onPointerMove = (event: PointerEvent) => {
      if (event.buttons === 0) release();
    };
    document.addEventListener("selectionchange", update);
    window.addEventListener("pointermove", onPointerMove);
    window.addEventListener("pointerup", release);
    window.addEventListener("pointercancel", release);
    // A native context menu or text drag can swallow the pointerup.
    window.addEventListener("contextmenu", release);
    window.addEventListener("dragend", release);
    window.addEventListener("drop", release);
    return () => {
      document.removeEventListener("selectionchange", update);
      window.removeEventListener("pointermove", onPointerMove);
      window.removeEventListener("pointerup", release);
      window.removeEventListener("pointercancel", release);
      window.removeEventListener("contextmenu", release);
      window.removeEventListener("dragend", release);
      window.removeEventListener("drop", release);
    };
  }, []);

  const scrollToBottom = useCallback(() => {
    const pane = paneRef.current;
    if (!pane) return;
    clearSelectionInside(document.getSelection(), pane);
    pane.scrollTop = pane.scrollHeight;
    pinnedRef.current = true;
    setFollowing(true);
  }, []);

  useEffect(() => {
    const pane = paneRef.current;
    if (pane && pinnedRef.current) pane.scrollTop = pane.scrollHeight;
  }, [shownText, wrap, paneHeight]);

  const resizePane = useCallback((next: number | null) => {
    const height =
      next === null
        ? null
        : Math.round(
            Math.min(Math.max(next, PANE_MIN_HEIGHT_PX), maxPaneHeight()),
          );
    setPaneHeight(height);
    try {
      if (height === null) {
        window.localStorage.removeItem(PANE_HEIGHT_STORAGE_KEY);
      } else {
        window.localStorage.setItem(PANE_HEIGHT_STORAGE_KEY, String(height));
      }
    } catch {
    }
  }, []);

  const onResizeStart = useCallback(
    (event: ReactPointerEvent<HTMLButtonElement>) => {
      const frame = frameRef.current;
      if (!frame || event.button !== 0) {
        return;
      }
      event.preventDefault();
      const handle = event.currentTarget;
      const startY = event.clientY;
      const startHeight = frame.getBoundingClientRect().height;
      handle.setPointerCapture(event.pointerId);
      const move = (moveEvent: PointerEvent) =>
        resizePane(startHeight + moveEvent.clientY - startY);
      const end = () => {
        handle.removeEventListener("pointermove", move);
        handle.removeEventListener("pointerup", end);
        handle.removeEventListener("pointercancel", end);
      };
      handle.addEventListener("pointermove", move);
      handle.addEventListener("pointerup", end);
      handle.addEventListener("pointercancel", end);
    },
    [resizePane],
  );

  const onResizeKey = useCallback(
    (event: ReactKeyboardEvent<HTMLButtonElement>) => {
      const frame = frameRef.current;
      if (!frame) {
        return;
      }
      const step =
        event.key === "ArrowDown"
          ? PANE_KEY_STEP_PX
          : event.key === "ArrowUp"
            ? -PANE_KEY_STEP_PX
            : 0;
      if (!step) {
        return;
      }
      event.preventDefault();
      resizePane(frame.getBoundingClientRect().height + step);
    },
    [resizePane],
  );

  const onScroll = useCallback(() => {
    const pane = paneRef.current;
    if (!pane) return;
    const pinned =
      pane.scrollHeight - pane.scrollTop - pane.clientHeight <
      FOLLOW_THRESHOLD_PX;
    if (pinned !== pinnedRef.current) {
      pinnedRef.current = pinned;
      setFollowing(pinned);
    }
  }, []);

  const revealLogsFolder = useCallback(async () => {
    setRevealing(true);
    try {
      await openLogsFolder(logRoot, realpath);
    } catch (error) {
      toast.error(t("settings.debugging.openLogsFolderFailed"), {
        description: (error as Error).message,
      });
    } finally {
      setRevealing(false);
    }
  }, [t, logRoot, realpath]);

  const downloadAllLogs = useCallback(async () => {
    setExporting(true);
    try {
      const savedPath = await exportAllLogs();
      if (savedPath) {
        toast.success(
          t("settings.debugging.downloadedTo", { path: savedPath }),
          {
            action: {
              label: t("settings.debugging.showInFolder"),
              onClick: () => {
                void revealSavedArchive(savedPath).catch((error: unknown) => {
                  toast.error(t("settings.debugging.openLogsFolderFailed"), {
                    description: (error as Error).message,
                  });
                });
              },
            },
          },
        );
      } else {
        toast.success(t("settings.debugging.downloadedToBrowser"));
      }
    } catch (error) {
      const failure =
        error instanceof LogExportError ? error.failure : "failed";
      if (failure === "outdated") {
        toast.error(t("settings.debugging.exportTooOld"));
      } else if (failure === "forbidden") {
        toast.error(t("settings.debugging.exportForbidden"));
      } else {
        toast.error(t("settings.debugging.exportFailed"), {
          description: (error as Error).message,
        });
      }
    } finally {
      setExporting(false);
    }
  }, [t]);

  const groupedSources = useMemo(() => groupByFamily(sources), [sources]);
  const shownPath = realpath ?? logRoot;
  const modeLabel = (candidate: RefreshMode) =>
    t(
      candidate === "live"
        ? "settings.debugging.modeLive"
        : candidate === "3s"
          ? "settings.debugging.modeInterval"
          : "settings.debugging.modeManual",
    );

  return (
    <div className="settings-page">
      <header className="flex min-w-0 flex-col gap-1 pr-6">
        <h1 className="text-xl font-semibold font-heading">
          {t("settings.tabs.debugging")}
        </h1>
        <p className="text-xs text-muted-foreground">
          {t("settings.debugging.description")}
        </p>
      </header>

      <SettingsSection
        title={t("settings.debugging.logSection")}
        description={t("settings.debugging.sourceHint")}
      >
        <SettingsRow label={t("settings.debugging.source")} wideControl={true}>
          <Select
            value={sourceId ?? ""}
            onValueChange={(value) => setSourceId(value || null)}
            disabled={sources.length === 0}
          >
            <SelectTrigger
              size="sm"
              data-testid="debug-log-source"
              aria-label={t("settings.debugging.source")}
              className="max-w-[calc(22rem*var(--ui-space-scale,1))] font-mono text-ui-12"
            >
              <SelectValue placeholder={t("settings.debugging.missing")} />
            </SelectTrigger>
            <SelectContent align="end">
              {groupedSources.map((group) => (
                <SelectGroup key={group.family}>
                  <SelectLabel className="font-mono uppercase tracking-wide text-ui-10">
                    {group.family}
                  </SelectLabel>
                  {group.sources.map((source) => (
                    <SelectItem
                      key={source.id}
                      value={source.id}
                      className="font-mono text-ui-12"
                    >
                      <span className="flex min-w-0 items-center gap-2">
                        <span className="min-w-0 truncate">{source.label}</span>
                        {source.isCurrent ? (
                          <span className="shrink-0 rounded-full bg-primary/10 px-1.5 py-px font-sans text-ui-10 font-medium text-primary">
                            {t("settings.debugging.currentSession")}
                          </span>
                        ) : null}
                        <span className="shrink-0 font-sans text-ui-10 text-muted-foreground tabular-nums">
                          {formatBytes(source.sizeBytes)}
                        </span>
                      </span>
                    </SelectItem>
                  ))}
                </SelectGroup>
              ))}
            </SelectContent>
          </Select>
        </SettingsRow>
        <SettingsRow label={t("settings.debugging.mode")} wideControl={true}>
          <div className="flex items-center gap-2">
            <div
              role="radiogroup"
              aria-label={t("settings.debugging.mode")}
              className="hub-tab-toggle inline-flex h-8 items-center rounded-full"
            >
              {MODES.map((candidate) => {
                const active = candidate === mode;
                return (
                  <button
                    key={candidate}
                    type="button"
                    role="radio"
                    data-testid={`debug-log-mode-${candidate}`}
                    aria-checked={active}
                    onClick={() => setMode(candidate)}
                    className={cn(
                      "relative flex h-8 items-center rounded-full px-3 text-xs font-medium transition-colors",
                      active
                        ? "text-foreground"
                        : "text-muted-foreground hover:text-foreground",
                    )}
                  >
                    {active && <SegmentedPill group={`${pillId}-mode`} />}
                    <span className="relative z-10">
                      {modeLabel(candidate)}
                    </span>
                  </button>
                );
              })}
            </div>
            <Button
              size="sm"
              variant="outline"
              disabled={mode !== "manual"}
              onClick={() => {
                clearSelectionInside(document.getSelection(), paneRef.current);
                void refreshSources();
                void poll();
              }}
            >
              <HugeiconsIcon strokeWidth={1.75} icon={Refresh01Icon} />
              {t("settings.debugging.refreshNow")}
            </Button>
          </div>
        </SettingsRow>
      </SettingsSection>

      <div
        data-settings-label={t("settings.debugging.path")}
        className="flex flex-col gap-3"
      >
        <div className="flex flex-wrap items-center gap-2">
          <InputGroup className="h-8 min-w-48 flex-1">
            <InputGroupAddon align="inline-start">
              <HugeiconsIcon
                icon={Search01Icon}
                strokeWidth={2}
                className="size-3.5 text-muted-foreground"
              />
            </InputGroupAddon>
            <InputGroupInput
              type="search"
              value={filter}
              onChange={(event) => setFilter(event.target.value)}
              placeholder={t("settings.debugging.filterPlaceholder")}
              aria-label={t("settings.debugging.filterPlaceholder")}
              data-testid="debug-log-filter"
              className="text-sm"
            />
          </InputGroup>
          <div className="ml-auto flex items-center gap-2">
            <span
              className="text-xs text-muted-foreground tabular-nums"
              data-testid="debug-log-line-count"
            >
              {trimmedFilter
                ? t("settings.debugging.filteredLineCount", {
                    shown: visibleLines.length,
                    total: buffer.lines.length,
                  })
                : t("settings.debugging.lineCount", {
                    count: buffer.lines.length,
                  })}
            </span>
            {notice ? null : (
              <span
                className={cn(
                  "inline-flex h-5 items-center gap-1.5 rounded-full px-2 text-xs font-medium",
                  staleSession
                    ? "bg-amber-500/10 text-amber-600 dark:text-amber-400"
                    : mode === "manual" || holding
                      ? "bg-muted text-muted-foreground"
                      : "bg-primary/10 text-primary",
                )}
              >
                <span
                  className={cn(
                    "size-1.5 rounded-full bg-current",
                    !staleSession &&
                      mode !== "manual" &&
                      !holding &&
                      "motion-safe:animate-pulse",
                  )}
                />
                {staleSession
                  ? t("settings.debugging.statusStale")
                  : mode === "manual" || holding
                    ? t("settings.debugging.statusPaused")
                    : t("settings.debugging.statusLive")}
              </span>
            )}
          </div>
        </div>

        {notice || dropped || staleSession || morePending ? (
          <div className="flex flex-col gap-1.5">
            {notice ? (
              <NoticeStrip tone="warning" testId="debug-log-notice">
                {notice}
              </NoticeStrip>
            ) : null}
            {dropped ? (
              <NoticeStrip tone="warning">
                {t("settings.debugging.droppedNotice")}
              </NoticeStrip>
            ) : null}
            {staleSession ? (
              <NoticeStrip tone="warning">
                {t("settings.debugging.staleSession")}
              </NoticeStrip>
            ) : null}
            {morePending ? (
              <NoticeStrip tone="info">
                {t("settings.debugging.morePending")}
              </NoticeStrip>
            ) : null}
          </div>
        ) : null}

        <div className="relative">
          <div
            ref={frameRef}
            style={paneHeight === null ? undefined : { height: paneHeight }}
            className={cn(
              "flex rounded-lg border border-border/60 bg-muted/20 py-1 pr-0 pl-1 dark:border-transparent dark:bg-[rgb(255_255_255_/_calc(0.04*var(--contrast-wash-gain,1)))]",
              paneHeight === null && "h-[min(26rem,45vh)]",
            )}
          >
              {/* Keep one text node; per-line nodes make frequent polls expensive. */}
            <pre
              ref={paneRef}
              onScroll={onScroll}
              onPointerDown={() => {
                pointerDownRef.current = true;
              }}
              data-testid="debug-log-pane"
              className={cn(
                "h-full w-full overflow-auto [overflow-anchor:none] scroll-rounded rounded-md py-2 pr-11 pl-2.5 font-mono text-ui-11 leading-[1.55] text-foreground/90",
                wrap ? "whitespace-pre-wrap break-words" : "whitespace-pre",
                !shownText && "text-muted-foreground",
              )}
            >
              {shownText ||
                (trimmedFilter && buffer.lines.length > 0
                  ? t("settings.debugging.noMatches")
                  : t("settings.debugging.empty"))}
            </pre>
          </div>
          <Tooltip>
            <TooltipTrigger asChild={true}>
              <Button
                size="icon-sm"
                variant="ghost"
                aria-pressed={wrap}
                aria-label={t("settings.debugging.wrapLines")}
                onClick={() => setWrap((previous) => !previous)}
                data-testid="debug-log-wrap"
                className={cn(
                  "absolute top-1.5 right-3.5 size-7 bg-background/80 shadow-sm backdrop-blur-sm hover:bg-background [&_svg]:size-3.5",
                  wrap ? "text-foreground" : "text-muted-foreground",
                )}
              >
                <HugeiconsIcon strokeWidth={1.75} icon={wrap ? LineWrapDisableIcon : LineWrapIcon} />
              </Button>
            </TooltipTrigger>
            <TooltipContent>{t("settings.debugging.wrapLines")}</TooltipContent>
          </Tooltip>
          {!following && shownText ? (
            <Button
              size="xs"
              variant="dark"
              onClick={scrollToBottom}
              data-testid="debug-log-jump-to-latest"
              className="absolute right-4 bottom-3 rounded-full pr-2.5 pl-2 shadow-md"
            >
              <ArrowDownIcon strokeWidth={2} />
              {t("settings.debugging.jumpToLatest")}
            </Button>
          ) : null}
        </div>
        <button
          // biome-ignore lint/a11y/useSemanticElements: a focusable drag splitter, which an <hr> cannot be
          type="button"
          role="separator"
          aria-orientation="horizontal"
          aria-label={t("settings.debugging.resizeLog")}
          aria-valuemin={PANE_MIN_HEIGHT_PX}
          aria-valuenow={paneHeight ?? undefined}
          title={t("settings.debugging.resizeLog")}
          onPointerDown={onResizeStart}
          onKeyDown={onResizeKey}
          onDoubleClick={() => resizePane(null)}
          data-testid="debug-log-resize"
          className="group/resize -mt-2.5 flex h-3 w-full cursor-row-resize touch-none items-center justify-center rounded-full focus-visible:outline-none"
        >
          <span className="h-1 w-10 rounded-full bg-border transition-colors group-hover/resize:bg-muted-foreground/50 group-focus-visible/resize:bg-ring" />
        </button>

        <div className="flex flex-wrap items-center justify-between gap-x-4 gap-y-2">
          <div className="flex min-w-0 flex-1 basis-48 items-center gap-1">
            <code
              className="min-w-0 truncate font-mono text-ui-11 text-muted-foreground"
              title={shownPath ?? undefined}
            >
              {shownPath ?? "-"}
            </code>
            <Tooltip>
              <TooltipTrigger asChild={true}>
                <Button
                  size="icon-xs"
                  variant="ghost"
                  aria-label={t("settings.debugging.pathCopy")}
                  disabled={!shownPath}
                  onClick={() => shownPath && copyPath(shownPath)}
                  className="text-muted-foreground"
                >
                  <HugeiconsIcon
                    strokeWidth={1.75}
                    icon={pathCopied ? Tick02Icon : Copy01Icon}
                  />
                </Button>
              </TooltipTrigger>
              <TooltipContent>
                {t("settings.debugging.pathCopy")}
              </TooltipContent>
            </Tooltip>
          </div>
          <div className="flex max-w-full shrink-0 flex-wrap items-center justify-end gap-1">
            <Button
              size="sm"
              variant="ghost"
              onClick={() => copy(shownText)}
              disabled={!shownText}
            >
              <HugeiconsIcon
                strokeWidth={1.75}
                icon={copied ? Tick02Icon : Copy01Icon}
              />
              {t("settings.debugging.copyVisible")}
            </Button>
            <Button
              size="sm"
              variant="muted"
              data-testid="debug-log-download-all"
              aria-busy={exporting}
              disabled={exporting}
              onClick={() => void downloadAllLogs()}
            >
              <HugeiconsIcon strokeWidth={1.75} icon={Download01Icon} />
              {exporting
                ? t("settings.debugging.downloadingAllLogs")
                : t("settings.debugging.downloadAllLogs")}
            </Button>
            <Tooltip>
              <TooltipTrigger asChild={true}>
                <button
                  type="button"
                  data-testid="debug-log-export-note"
                  aria-label={t("settings.debugging.exportMaskedNote")}
                  className="flex size-8 shrink-0 cursor-help items-center justify-center rounded-full text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
                >
                  <HugeiconsIcon
                    strokeWidth={1.75}
                    icon={InformationCircleIcon}
                    className="size-[var(--ui-icon-size-hint)]"
                  />
                </button>
              </TooltipTrigger>
              <TooltipContent className="max-w-[calc(300px*var(--ui-space-scale,1))] text-ui-11 leading-snug">
                {t("settings.debugging.exportMaskedNote")}
              </TooltipContent>
            </Tooltip>
            {isTauri ? (
              <Button
                size="sm"
                variant="ghost"
                data-testid="debug-log-open-folder"
                aria-busy={revealing}
                disabled={revealing}
                onClick={() => void revealLogsFolder()}
              >
                <HugeiconsIcon strokeWidth={1.75} icon={FolderOpenIcon} />
                {t("settings.debugging.openLogsFolder")}
              </Button>
            ) : null}
          </div>
        </div>

        <p className="flex items-center gap-2 text-xs text-muted-foreground">
          <HugeiconsIcon
            strokeWidth={1.75}
            icon={Shield01Icon}
            className="size-3.5 shrink-0"
          />
          {t("settings.debugging.privacyNote")}
        </p>
      </div>
    </div>
  );
}