/** * @file ErrorPropagationMap.tsx * @description A React component that visualizes error propagation across agent hierarchies in a workflow system. It displays the distribution of errors by hierarchy depth, identifies error-prone agent types, and highlights API and session errors. The component uses horizontal bars to represent error counts at different depths and types, providing an intuitive overview of where errors are occurring within the agent structure. * @author Nguyễn Ngọc Trí Vĩ */ /* ============================================================================= * MODULE_GUIDE — extended in-file reference (comments only; safe to read, never executed) * ============================================================================= * **Purpose:** Workflow analytics visualization built on D3; consumes aggregated session/run metrics from the workflows API. * * ## Design constraints * - Local-first: no telemetry leaves the machine unless the user configures webhooks. * - Fail-safe hooks path on the server must never block Claude Code; UI mirrors that * philosophy by degrading gracefully (empty states, stale badges, reconnect loops). * - Destructive flows stay behind explicit confirmation modals and server-side gates. * - Internationalization: user-visible strings belong in i18n JSON, not literals here. * * ## Remote data & SSH * Remote Data Sources let operators aggregate multiple machines. SSH entries describe * how to reach a peer dashboard; the global data scope (`dataScope.ts`) narrows every * scoped GET via `?sources=`. Health checks and import history surface in Settings. * * ## Observability * Prometheus scrapes `GET /api/metrics` (see `monitoring/`). Grafana ships four * provisioned boards (overview, sessions, tools, alerts). Native npm scripts and * Docker Compose profiles are documented in `monitoring/README.md`. * * ## Internal dependencies * - `../../lib/types` * * ## Public surface * - `ErrorPropagationMapProps` — exported API; see TSDoc on the symbol for behavior. * - `ErrorPropagationMap` — exported API; see TSDoc on the symbol for behavior. * * ## Testing pointers * - Prefer colocated `__tests__` with Vitest + Testing Library for UI. * - Server contract changes require `npm run test:server` and OpenAPI sync. * - MCP edits: `npm run mcp:typecheck` and `npm run mcp:build`. * * ## Related docs * - `ARCHITECTURE.md` — hooks → API → SQLite → WebSocket → UI pipeline. * - `docs/API.md` — REST reference. * - `.claude/skills/file-headers/` — mandatory `@author` header policy. * ============================================================================= */ /* ----------------------------------------------------------------------------- * EXPORT CATALOG — quick index of symbols defined below (documentation only). * ----------------------------------------------------------------------------- * **ErrorPropagationMapProps** * Part of this module's public contract. Downstream imports should treat * the signature and return type as stable unless release notes say otherwise. * When behavior changes, update the `@file` overview and relevant tests. * * **ErrorPropagationMap** * Part of this module's public contract. Downstream imports should treat * the signature and return type as stable unless release notes say otherwise. * When behavior changes, update the `@file` overview and relevant tests. * * ----------------------------------------------------------------------------- */ import { useState } from "react"; import { useTranslation } from "react-i18next"; import type { ErrorPropagationData } from "../../lib/types"; const DEPTH_COLORS = ["#ef4444", "#f97316", "#eab308", "#a855f7"]; // ── Component ───────────────────────────────────────────────────────────────── export interface ErrorPropagationMapProps { data: ErrorPropagationData; } export function ErrorPropagationMap({ data }: ErrorPropagationMapProps) { const { t } = useTranslation("workflows"); const [hoveredDepth, setHoveredDepth] = useState(null); function depthLabel(depth: number): string { const keys = [ t("errorPropagation.depthLabels.sessionMain"), t("errorPropagation.depthLabels.directSubagent"), t("errorPropagation.depthLabels.nested"), t("errorPropagation.depthLabels.deep"), ]; return keys[depth] ?? `${t("common:depth")} ${depth}`; } const hasErrors = data.byDepth.some((d) => d.count > 0) || data.byType.some((t) => t.count > 0) || (data.eventErrors && data.eventErrors.length > 0) || data.sessionsWithErrors > 0; if (!hasErrors) { return (
{t("errorPropagation.noErrors")} {t("errorPropagation.allSuccess")}
); } const errorRatePct = data.errorRate; const totalErrors = data.byDepth.reduce((s, d) => s + d.count, 0); const maxDepthCount = Math.max(...data.byDepth.map((d) => d.count), 1); const topTypes = [...data.byType].sort((a, b) => b.count - a.count).slice(0, 6); const hasDepthData = data.byDepth.some((d) => d.count > 0); return (
{/* Error rate summary bar */}
{errorRatePct}%

{t("errorPropagation.sessionsErrorSummary", { errorSessions: data.sessionsWithErrors, totalSessions: data.totalSessions, })}

{totalErrors > 0 ? `${totalErrors}${t("errorPropagation.agentErrors")}` : t("errorPropagation.sessionErrorsOnly")}

{/* Errors by depth - horizontal bars */} {hasDepthData && (

{t("errorPropagation.errorsByDepth")}

{data.byDepth .filter((d) => d.count > 0) .map((d) => { const pct = (d.count / maxDepthCount) * 100; const color = DEPTH_COLORS[d.depth] ?? DEPTH_COLORS[DEPTH_COLORS.length - 1]; const isHovered = hoveredDepth === d.depth; return (
setHoveredDepth(d.depth)} onMouseLeave={() => setHoveredDepth(null)} > {depthLabel(d.depth)}
{d.count}
); })}
)} {/* Error-prone agent types */} {topTypes.length > 0 && (

{t("errorPropagation.errorProneTypes")}

{topTypes.map((t, i) => { const maxCount = topTypes[0]?.count ?? 1; const pct = (t.count / maxCount) * 100; return (
{t.subagent_type}
{t.count}
); })}
)} {/* API & session errors */} {data.eventErrors && data.eventErrors.length > 0 && (

{t("errorPropagation.apiSessionErrors")}

{data.eventErrors.map((e) => (
{e.summary} {e.count}x
))}
)}
); }