fabro/apps/fabro-web/app/routes/run-logs.tsx
Bryan Helmkamp 62f0b3e7d1
refactor(web): improve React Doctor score (#405)
## Summary

Improves the web UI's React Doctor audit score by separating reusable
helpers from React component modules, tightening effect/state ownership,
and extracting real component boundaries in the install wizard, stage
activity view, run-files diff browser, RunDetail route, and Runs
workspace. The branch removes the previously deferred RunDetail and Runs
giant-component diagnostics without changing RunDetail UX, route
contracts, action ordering, or Runs workspace behavior.

| Metric | Main baseline | Initial PR | Current PR |
|--------|---------------|------------|------------|
| React Doctor score | 63 | 71 | 99 |
| React Doctor errors | 123 | 0 | 0 |
| React Doctor warnings | 241 | 163 | 3 |
| React Doctor diagnostics | 364 | 163 | 3 |

## Changes

- Moves exported helper logic out of component files so Fast
Refresh/component-export rules no longer dominate the audit.
- Adds a targeted React Doctor config exception for React Router route
modules, where non-component exports like route metadata are
intentional.
- Refactors low-risk state/effect patterns: keyed interview question
state, reducer-backed editable run title state, event-owned preview
opening, route-keyed insights editor initialization, refresh timer
ownership, and selection/derived list cleanup.
- Reworks `InstallApp` around an install reducer, a controller hook for
install lifecycle state, and focused wizard step components for LLM,
server, object-store, sandbox, and GitHub setup.
- Moves `RunStages` selected-stage activity into a keyed boundary for
panel/debug detail state while preserving stage activity filters across
navigation.
- Extracts the `RunFiles` loaded diff-browser view from route/query
coordination so the route owns data/URL state and the loaded view owns
rendering.
- Splits `RunDetail` into route-local header, actions, tab shell, docked
controls, model, and lifecycle-toast modules; the actions menu now uses
grouped descriptors instead of a large boolean/callback prop matrix.
- Extracts Runs workspace preference ownership into
`useRunsWorkspacePreferences` and moves toolbar rendering into
`RunsToolbar`, leaving the route focused on data, DnD state, filtering,
and view selection.
- Guards `InsightsEditor` query execution with a latest-run id and
timeout cleanup so stale or unmounted mock query runs cannot overwrite
newer results.
- Adds regression coverage for archived-run deletion from RunDetail and
stale-result handling in InsightsEditor.
- Improves semantic/accessibility coverage with labeled controls, native
meter/section semantics, decorative status dots, and clearer unavailable
copy.
- Removes dead UI code and applies local suppressions only where the
rule is a documented false positive or an intentional imperative
integration boundary.

## Remaining React Doctor warnings

Current score is 99 with 0 errors and 3 warnings. The remaining warnings
are intentionally left for separate judgment rather than mechanical
churn:

- `prefer-useReducer` (3): `AutomationsNew`, `InsightsEditor`, and
`CreateSecretForm` need reducers only if they encode real coupled
transitions, not simple field setters.

## Verification

- `cd apps/fabro-web && bun test app/routes/run-detail.test.ts` -> `22
pass`, `0 fail`
- `cd apps/fabro-web && bun test app/routes/insights-editor.test.tsx
app/routes/runs.preferences.test.tsx` -> `7 pass`, `0 fail`
- `cd apps/fabro-web && bun run typecheck`
- `cd apps/fabro-web && bun test --isolate` -> `490 pass`, `0 fail`
- `cd apps/fabro-web && bunx react-doctor@latest --full --json >
/tmp/fabro-react-doctor-runs-insights.json` -> score `99`, `0` errors,
`3` warnings
- Earlier branch verification also included `cd apps/fabro-web && bun
run build`
- `git diff --check`

---

[![Compound
Engineering](https://img.shields.io/badge/Compound_Engineering-6366f1)](https://github.com/EveryInc/compound-engineering-plugin)
🤖 Generated with GPT-5 (context not reported, default reasoning) via
[Codex](https://openai.com/codex)
2026-05-25 22:41:37 -04:00

322 lines
10 KiB
TypeScript

import { useMemo, useState } from "react";
import { useParams } from "react-router";
import {
Listbox,
ListboxButton,
ListboxOption,
ListboxOptions,
} from "@headlessui/react";
import {
CheckIcon,
ChevronUpDownIcon,
FunnelIcon,
MagnifyingGlassIcon,
} from "@heroicons/react/16/solid";
import { EmptyState, ErrorState, LoadingState } from "../components/state";
import { StageSidebar } from "../components/stage-sidebar";
import { CopyButton } from "../components/ui";
import { useRun, useRunLogs, useRunStages } from "../lib/queries";
import { mapRunStagesToSidebarStages } from "../lib/stage-sidebar";
export const handle = { wide: true };
const LIVE_REFRESH_MS = 5000;
export default function RunLogs() {
const { id } = useParams();
const runQuery = useRun(id);
const stagesQuery = useRunStages(id);
const isLive = runQuery.data?.lifecycle.status.kind === "running";
const logsQuery = useRunLogs(id, isLive ? LIVE_REFRESH_MS : undefined);
const stages = useMemo(
() => mapRunStagesToSidebarStages(stagesQuery.data),
[stagesQuery.data],
);
return (
<div className="flex gap-6">
<StageSidebar stages={stages} runId={id!} activeLink="logs" />
<div className="min-w-0 flex-1">
<RunLogsBody logsQuery={logsQuery} />
</div>
</div>
);
}
function RunLogsBody({
logsQuery,
}: {
logsQuery: ReturnType<typeof useRunLogs>;
}) {
if (logsQuery.error) {
return (
<ErrorState
title="Couldn't load run log"
description={errorMessage(logsQuery.error)}
onRetry={() => void logsQuery.mutate()}
/>
);
}
if (logsQuery.data === undefined) {
return <LoadingState label="Loading log…" />;
}
if (logsQuery.data === null) {
return (
<EmptyState
title="No run log yet"
description="The worker hasn't written any tracing output for this run."
/>
);
}
return <LogPanel text={logsQuery.data} />;
}
const LOG_LEVELS = ["TRACE", "DEBUG", "INFO", "WARN", "ERROR"] as const;
type LogLevel = (typeof LOG_LEVELS)[number];
const LEVEL_COLOR: Record<LogLevel, string> = {
ERROR: "text-coral",
WARN: "text-amber",
INFO: "text-teal-500",
DEBUG: "text-fg-3",
TRACE: "text-fg-muted",
};
const LOG_LINE_RE =
/^(\S+)(\s+)(TRACE|DEBUG|INFO|WARN|ERROR)(\s+)(.*)$/;
interface LogRecord {
level: LogLevel | null;
lines: LogLineEntry[];
}
interface LogLineEntry {
key: string;
text: string;
}
function splitLogLines(text: string): LogLineEntry[] {
let offset = 0;
return text.split("\n").map((line) => {
const key = `${offset}:${line.length}`;
offset += line.length + 1;
return { key, text: line };
});
}
function parseRecords(lines: LogLineEntry[]): LogRecord[] {
const records: LogRecord[] = [];
let current: LogRecord | null = null;
for (const line of lines) {
const match = LOG_LINE_RE.exec(line.text);
if (match) {
if (current) records.push(current);
current = { level: match[3] as LogLevel, lines: [line] };
} else if (current) {
current.lines.push(line);
} else {
current = { level: null, lines: [line] };
}
}
if (current) records.push(current);
return records;
}
function LogPanel({ text }: { text: string }) {
const byteCount = new Blob([text]).size;
const lines = useMemo(() => splitLogLines(text), [text]);
const records = useMemo(() => parseRecords(lines), [lines]);
const [selectedLevels, setSelectedLevels] = useState<LogLevel[]>([
...LOG_LEVELS,
]);
const [search, setSearch] = useState("");
const allLevelsSelected = selectedLevels.length === LOG_LEVELS.length;
const isFiltering = !allLevelsSelected || search.length > 0;
const filteredLines = useMemo(() => {
if (!isFiltering) return lines;
const levelSet = new Set(selectedLevels);
const needle = search.toLowerCase();
const kept = records.filter((record) => {
if (record.level !== null && !levelSet.has(record.level)) return false;
if (needle && !record.lines.some((l) => l.text.toLowerCase().includes(needle))) {
return false;
}
return true;
});
return kept.flatMap((r) => r.lines);
}, [lines, records, selectedLevels, search, isFiltering]);
function clearFilters() {
setSelectedLevels([...LOG_LEVELS]);
setSearch("");
}
return (
<div className="rounded-md border border-line bg-panel-alt">
<div className="flex flex-wrap items-center gap-x-3 gap-y-2 border-b border-line px-3 py-2">
<div className="flex flex-1 flex-wrap items-center gap-2">
<LevelFilter selected={selectedLevels} onChange={setSelectedLevels} />
<SearchInput value={search} onChange={setSearch} />
{isFiltering && (
<button
type="button"
onClick={clearFilters}
className="rounded px-2 py-1 text-xs text-fg-muted transition-colors hover:bg-overlay hover:text-fg-2 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-teal-500"
>
Clear
</button>
)}
</div>
<div className="flex items-center gap-3">
<span className="text-xs tabular-nums text-fg-muted">
{isFiltering
? `${filteredLines.length.toLocaleString()} of ${lines.length.toLocaleString()} lines`
: formatWholeBytes(byteCount)}
</span>
<CopyButton value={text} label="Copy run log" />
</div>
</div>
<pre className="max-h-[70vh] overflow-auto whitespace-pre p-4 font-mono text-xs leading-5 text-fg-2">
{filteredLines.length === 0 ? (
<span className="text-fg-muted">No lines match these filters.</span>
) : (
filteredLines.map((line, i) => (
<LogLine
key={line.key}
line={line.text}
trailingNewline={i < filteredLines.length - 1}
/>
))
)}
</pre>
</div>
);
}
function LevelFilter({
selected,
onChange,
}: {
selected: LogLevel[];
onChange: (levels: LogLevel[]) => void;
}) {
const summary = useMemo(() => {
if (selected.length === LOG_LEVELS.length) return "All levels";
if (selected.length === 0) return "No levels";
if (selected.length <= 2) {
return LOG_LEVELS.filter((l) => selected.includes(l)).join(", ");
}
return `${selected.length} levels`;
}, [selected]);
return (
<Listbox value={selected} onChange={onChange} multiple>
<ListboxButton className="inline-flex items-center gap-2 rounded-md bg-panel px-2.5 py-1.5 text-xs text-fg-2 outline-1 -outline-offset-1 outline-line-strong transition-colors hover:bg-overlay-strong focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-teal-500">
<FunnelIcon className="size-3.5 text-fg-muted" aria-hidden="true" />
<span className="tabular-nums">{summary}</span>
<ChevronUpDownIcon className="size-3.5 text-fg-muted" aria-hidden="true" />
</ListboxButton>
<ListboxOptions
transition
anchor={{ to: "bottom start", gap: 4 }}
className="z-20 w-44 rounded-md bg-panel py-1 outline-1 -outline-offset-1 outline-line-strong transition data-closed:scale-95 data-closed:opacity-0 data-enter:duration-100 data-enter:ease-out data-leave:duration-75 data-leave:ease-in"
>
{LOG_LEVELS.map((level) => (
<ListboxOption
key={level}
value={level}
className="group flex cursor-pointer items-center gap-2.5 px-3 py-1.5 text-xs text-fg-3 data-focus:bg-overlay data-focus:text-fg data-focus:outline-hidden"
>
<span className="flex size-3.5 items-center justify-center rounded-sm border border-line-strong bg-panel-alt group-data-selected:border-teal-500 group-data-selected:bg-teal-500">
<CheckIcon
className="size-2.5 text-on-primary opacity-0 group-data-selected:opacity-100"
aria-hidden="true"
/>
</span>
<span className={`font-mono font-semibold ${LEVEL_COLOR[level]}`}>
{level}
</span>
</ListboxOption>
))}
</ListboxOptions>
</Listbox>
);
}
function SearchInput({
value,
onChange,
}: {
value: string;
onChange: (value: string) => void;
}) {
return (
<div className="relative w-full max-w-sm min-w-48 flex-1">
<MagnifyingGlassIcon
className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-fg-muted"
aria-hidden="true"
/>
<input
type="search"
name="log-search"
aria-label="Search logs"
placeholder="Search logs"
autoComplete="off"
spellCheck={false}
value={value}
onChange={(e) => onChange(e.target.value)}
className="block w-full rounded-md bg-panel py-1.5 pl-8 pr-2.5 text-xs text-fg outline-1 -outline-offset-1 outline-line-strong placeholder:text-fg-muted focus:outline-2 focus:-outline-offset-1 focus:outline-teal-500 max-sm:text-base/5"
/>
</div>
);
}
function LogLine({ line, trailingNewline }: { line: string; trailingNewline: boolean }) {
const newline = trailingNewline ? "\n" : "";
const match = LOG_LINE_RE.exec(line);
if (!match) {
return <span>{line}{newline}</span>;
}
const [, timestamp, gap1, level, gap2, rest] = match;
return (
<span>
<span className="text-fg-muted">{timestamp}</span>
{gap1}
<span className={`font-semibold ${LEVEL_COLOR[level as LogLevel]}`}>{level}</span>
{gap2}
<LogRest text={rest} />
{newline}
</span>
);
}
const LOG_REST_RE = /^([a-zA-Z_][\w:]*):(\s+)(.*)$/;
function LogRest({ text }: { text: string }) {
const match = LOG_REST_RE.exec(text);
if (!match) return <>{text}</>;
const [, target, gap, message] = match;
return (
<>
<span className="text-fg-3">{target}</span>
<span className="text-fg-muted">:</span>
{gap}
<span>{message}</span>
</>
);
}
function errorMessage(error: unknown): string | undefined {
return error instanceof Error ? error.message : undefined;
}
function formatWholeBytes(bytes: number): string {
if (bytes >= 1e9) return `${Math.round(bytes / 1e9)} GB`;
if (bytes >= 1e6) return `${Math.round(bytes / 1e6)} MB`;
if (bytes >= 1e3) return `${Math.round(bytes / 1e3)} KB`;
return `${bytes} B`;
}