{
  "schemaVersion": 1,
  "target": "react",
  "name": "graph-canvas",
  "version": "0.3.0",
  "category": "agent",
  "title": "Graph Canvas",
  "description": "The shared graph engine for the graph components: deterministic dagre layout keyed on structure, and a React Flow canvas with one visual language. Cards with a kind icon tile, name, meta line, and status glyph; explicit port rings; smooth edges into those ports with small swept arrowheads; a light pulse into running work; zoom, pan, fit-to-view, and a keyboard listbox.",
  "tags": [
    "agent",
    "graph",
    "react-flow",
    "dagre",
    "layout",
    "canvas"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@xyflow/react": "^12.12.1",
    "@dagrejs/dagre": "^3.1.3"
  },
  "registryDependencies": [
    {
      "name": "agent-run",
      "version": "0.1.2"
    },
    {
      "name": "step-list",
      "version": "0.3.0"
    }
  ],
  "docsPath": "/components/graph-canvas",
  "props": [
    {
      "name": "layoutGraph(nodes, edges, options?)",
      "type": "(GraphNodeRef[], GraphEdgeRef[], GraphLayoutOptions) => GraphLayout",
      "description": "Ranked dagre layout. Deterministic: the same structure always gives the same positions and edge routes."
    },
    {
      "name": "useGraphLayout(nodes, edges, options?)",
      "type": "hook => GraphLayout",
      "description": "Memoizes layoutGraph on structureKey (node ids, edge ends, labels present), so status updates never move nodes."
    },
    {
      "name": "GraphCanvas",
      "type": "{ nodes, edges, layout, selectedNodeId?, selectedEdgeId?, onNodeClick?, onEdgeClick?, onPaneClick?, height?, label, minimap?, animateEdges?, maxAnimatedEdges?, grid?, toolbar? }",
      "description": "React Flow canvas on a black (or white) stage with a faint dot grid. Nodes are cards: a kind icon tile (inverted when selected), the name, a meta line with the status in text, and the shared status glyph. Each node has port rings where edges leave and enter, shown on hover, on the selected node, along the selected path, and in the running color where work flows in. Edges are smooth curves that leave and enter square to the node and end in 7px swept arrowheads (SVG markers); solid or dashed by type. Edges marked animated carry a light pulse into the running node (transform and opacity only, at most maxAnimatedEdges at once, off with reduced motion). Cards fade and scale in by rank on first render. Selection is instant; only the viewport eases. Includes zoom out, zoom level (click to reset), zoom in, fit-to-view, and a mini map for graphs over 24 nodes. Imports @xyflow/react/dist/style.css."
    },
    {
      "name": "ancestorsOf / descendantsOf / branchOf / isAcyclic",
      "type": "(id, edges) => Set<string>",
      "description": "Graph helpers used for highlighting and branch focus. Safe on cycles."
    },
    {
      "name": "GraphListbox",
      "type": "{ items, getId, selectedId, onSelect, renderItem, label }",
      "description": "Single-select listbox with one tab stop; arrow keys, Home, and End move the selection."
    },
    {
      "name": "GraphStatusIcon",
      "type": "({ status: GraphStatus }) => JSX",
      "description": "StepStatusIcon plus icons for waiting-for-approval and cancelled."
    },
    {
      "name": "GraphCanvasNode",
      "type": "{ id, title, subtitle?, mono?, status?, statusLabel?, icon?, avatar?, avatarShape?, tag?, tone?, hidden? }",
      "description": "icon picks the kind icon in the node's tile (plan, step, tool, model, agent, approval, result, handoff, person, system, search, design, code, test, docs, check, release); avatar shows initials instead. tone and tag mark how a node relates to the selection."
    },
    {
      "name": "GraphCanvasEdge",
      "type": "{ id, source, target, type, label?, labelStatus?, dashed?, tone?, animated?, hidden?, selectable? }",
      "description": "animated marks an edge that feeds running work: drawn in the running color, with the pulse while animateEdges is on (the default). Edges that point back up the graph use their own ports, beside the forward ones."
    },
    {
      "name": "GraphNodeIcon",
      "type": "({ name: GraphIconName }) => JSX",
      "description": "The 16px kind icons, drawn on the same grid and stroke as the status glyphs but never as a full circle, so a kind is not mistaken for a status."
    }
  ],
  "accessibility": [
    "Nodes are not draggable and not individually focusable; the list view each graph component provides is the keyboard path, with selection shared with the canvas.",
    "Zoom in, zoom out, and fit-to-view are labelled buttons with a visible focus outline.",
    "Statuses use the status glyph and text on every node; kind icons are decorative, and the kind is also in text. Edge types use solid or dashed lines plus a text legend; edges into running work also use the running color, so the pulse is never the only signal.",
    "With prefers-reduced-motion: reduce, the entrance, the edge pulse, and viewport easing are off: fit, zoom, and pan-to-selection jump, and running edges stay still in the running color.",
    "Selecting from the list pans the canvas to the selected node when it is off screen. Selection itself never animates, and status-only updates never move nodes."
  ],
  "states": [
    "queued",
    "running",
    "waiting-for-approval",
    "succeeded",
    "failed",
    "skipped",
    "cancelled"
  ],
  "files": [
    {
      "path": "graph/graph-layout.ts",
      "content": "// Layout and graph helpers shared by the graph components. No React, no DOM:\n// safe to call on the server, in tests, or in a worker.\nimport { graphlib, layout as dagreLayout } from \"@dagrejs/dagre\";\nimport type { RunStatus, StepStatus } from \"../agent/agent-run\";\n\n/** Every status a graph node can show. Step statuses plus the run-level approval and cancel states. */\nexport type GraphStatus = StepStatus | Extract<RunStatus, \"waiting-for-approval\" | \"cancelled\">;\n\nexport const GRAPH_STATUSES: GraphStatus[] = [\"queued\", \"running\", \"waiting-for-approval\", \"succeeded\", \"failed\", \"skipped\", \"cancelled\"];\n\nexport const GRAPH_STATUS_LABELS: Record<GraphStatus, string> = {\n  queued: \"Queued\",\n  running: \"Running\",\n  \"waiting-for-approval\": \"Needs approval\",\n  succeeded: \"Succeeded\",\n  failed: \"Failed\",\n  skipped: \"Skipped\",\n  cancelled: \"Cancelled\",\n};\n\n/** The minimum a node needs for layout: a stable id. */\nexport interface GraphNodeRef {\n  id: string;\n}\n\n/** The minimum an edge needs: a stable id, both ends, and an explicit type. */\nexport interface GraphEdgeRef {\n  id: string;\n  source: string;\n  target: string;\n  type: string;\n  /** Edges with a label get space reserved for it in the layout. */\n  label?: string;\n}\n\nexport type GraphDirection = \"LR\" | \"TB\";\n\nexport interface GraphLayoutOptions {\n  /** Top to bottom (default), which fits narrow panels, or left to right. */\n  direction?: GraphDirection;\n  nodeWidth?: number;\n  nodeHeight?: number;\n  /** Space between nodes in the same rank. */\n  nodeSep?: number;\n  /** Space between ranks. */\n  rankSep?: number;\n  /** Space reserved for labelled edges. */\n  labelWidth?: number;\n  labelHeight?: number;\n}\n\nexport interface Point {\n  x: number;\n  y: number;\n}\n\nexport interface GraphLayout {\n  /** The structure this layout was computed for. Equal keys mean equal layouts. */\n  key: string;\n  direction: GraphDirection;\n  nodeWidth: number;\n  nodeHeight: number;\n  /** Top-left corner of each node. */\n  positions: Record<string, Point>;\n  /** Route of each edge from its source boundary to its target boundary, and the label center. */\n  edges: Record<string, { points: Point[]; label: Point }>;\n  width: number;\n  height: number;\n}\n\nconst DEFAULTS: Required<GraphLayoutOptions> = {\n  direction: \"TB\",\n  // Room for the kind icon tile, a two-line label, and the status glyph.\n  nodeWidth: 232,\n  nodeHeight: 56,\n  nodeSep: 28,\n  // Room for an edge to curve between ranks and still end in a straight arrowhead.\n  rankSep: 56,\n  labelWidth: 132,\n  labelHeight: 26,\n};\n\n/** Drops edges whose ends are missing and self-loops, so the layout never invents nodes. */\nexport function layoutEdges<E extends GraphEdgeRef>(nodes: readonly GraphNodeRef[], edges: readonly E[]): E[] {\n  const ids = new Set(nodes.map((node) => node.id));\n  return edges.filter((edge) => edge.source !== edge.target && ids.has(edge.source) && ids.has(edge.target));\n}\n\n/**\n * Identifies the graph's structure: node ids, edge ends, and whether an edge has a label,\n * in input order. Statuses, text, and edge types are not part of it, so live status\n * updates keep the same key and therefore the same positions.\n */\nexport function structureKey(nodes: readonly GraphNodeRef[], edges: readonly GraphEdgeRef[], options: GraphLayoutOptions = {}): string {\n  const o = { ...DEFAULTS, ...options };\n  const edgePart = layoutEdges(nodes, edges).map((edge) => [edge.id, edge.source, edge.target, edge.label ? 1 : 0]);\n  return JSON.stringify([o.direction, o.nodeWidth, o.nodeHeight, o.nodeSep, o.rankSep, o.labelWidth, o.labelHeight, nodes.map((node) => node.id), edgePart]);\n}\n\n/**\n * Lays the graph out in ranks with dagre. Deterministic: the same structure always\n * produces the same positions. Cycles (for example, a hand-off and its return) are allowed.\n */\nexport function layoutGraph(nodes: readonly GraphNodeRef[], edges: readonly GraphEdgeRef[], options: GraphLayoutOptions = {}): GraphLayout {\n  const o = { ...DEFAULTS, ...options };\n  const kept = layoutEdges(nodes, edges);\n  const g = new graphlib.Graph({ multigraph: true });\n  g.setGraph({ rankdir: o.direction, nodesep: o.nodeSep, ranksep: o.rankSep, edgesep: 16, marginx: 0, marginy: 0 });\n  g.setDefaultEdgeLabel(() => ({}));\n  for (const node of nodes) g.setNode(node.id, { width: o.nodeWidth, height: o.nodeHeight });\n  for (const edge of kept) {\n    g.setEdge(edge.source, edge.target, edge.label ? { width: o.labelWidth, height: o.labelHeight, labelpos: \"c\" } : {}, edge.id);\n  }\n  dagreLayout(g);\n\n  const positions: Record<string, Point> = {};\n  for (const node of nodes) {\n    const placed = g.node(node.id) as { x: number; y: number } | undefined;\n    positions[node.id] = placed ? { x: placed.x - o.nodeWidth / 2, y: placed.y - o.nodeHeight / 2 } : { x: 0, y: 0 };\n  }\n  const routes: GraphLayout[\"edges\"] = {};\n  for (const edge of kept) {\n    const placed = g.edge({ v: edge.source, w: edge.target, name: edge.id }) as { points?: Point[]; x?: number; y?: number } | undefined;\n    const points = (placed?.points ?? []).map((point) => ({ x: point.x, y: point.y }));\n    const middle = points[Math.floor(points.length / 2)] ?? { x: 0, y: 0 };\n    routes[edge.id] = { points, label: placed?.x != null && placed.y != null ? { x: placed.x, y: placed.y } : middle };\n  }\n  const size = g.graph() as { width?: number; height?: number };\n  return {\n    key: structureKey(nodes, edges, options),\n    direction: o.direction,\n    nodeWidth: o.nodeWidth,\n    nodeHeight: o.nodeHeight,\n    positions,\n    edges: routes,\n    width: size.width ?? 0,\n    height: size.height ?? 0,\n  };\n}\n\nexport interface Adjacency<E extends GraphEdgeRef> {\n  incoming: Map<string, E[]>;\n  outgoing: Map<string, E[]>;\n}\n\n/** Incoming and outgoing edges per node id. */\nexport function adjacency<E extends GraphEdgeRef>(edges: readonly E[]): Adjacency<E> {\n  const incoming = new Map<string, E[]>();\n  const outgoing = new Map<string, E[]>();\n  for (const edge of edges) {\n    (incoming.get(edge.target) ?? incoming.set(edge.target, []).get(edge.target)!).push(edge);\n    (outgoing.get(edge.source) ?? outgoing.set(edge.source, []).get(edge.source)!).push(edge);\n  }\n  return { incoming, outgoing };\n}\n\nfunction walk(start: string, next: (id: string) => readonly GraphEdgeRef[] | undefined, end: \"source\" | \"target\"): Set<string> {\n  const seen = new Set<string>();\n  const stack = [start];\n  while (stack.length) {\n    const id = stack.pop()!;\n    for (const edge of next(id) ?? []) {\n      const other = edge[end];\n      if (other !== start && !seen.has(other)) {\n        seen.add(other);\n        stack.push(other);\n      }\n    }\n  }\n  return seen;\n}\n\n/** Every node the given node depends on, directly or transitively (not including itself). */\nexport function ancestorsOf(id: string, edges: readonly GraphEdgeRef[]): Set<string> {\n  const { incoming } = adjacency(edges);\n  return walk(id, (node) => incoming.get(node), \"source\");\n}\n\n/** Every node that depends on the given node, directly or transitively (not including itself). */\nexport function descendantsOf(id: string, edges: readonly GraphEdgeRef[]): Set<string> {\n  const { outgoing } = adjacency(edges);\n  return walk(id, (node) => outgoing.get(node), \"target\");\n}\n\n/** The node, its ancestors, and its descendants: what \"focus on branch\" shows. */\nexport function branchOf(id: string, edges: readonly GraphEdgeRef[]): Set<string> {\n  return new Set([id, ...ancestorsOf(id, edges), ...descendantsOf(id, edges)]);\n}\n\n/** True when the edges form no cycle (Kahn's algorithm). */\nexport function isAcyclic(nodes: readonly GraphNodeRef[], edges: readonly GraphEdgeRef[]): boolean {\n  const indegree = new Map(nodes.map((node) => [node.id, 0]));\n  for (const edge of edges) indegree.set(edge.target, (indegree.get(edge.target) ?? 0) + 1);\n  const { outgoing } = adjacency(edges);\n  const ready = [...indegree].filter(([, count]) => count === 0).map(([id]) => id);\n  let visited = 0;\n  while (ready.length) {\n    const id = ready.pop()!;\n    visited += 1;\n    for (const edge of outgoing.get(id) ?? []) {\n      const count = (indegree.get(edge.target) ?? 0) - 1;\n      indegree.set(edge.target, count);\n      if (count === 0) ready.push(edge.target);\n    }\n  }\n  return visited === indegree.size;\n}\n"
    },
    {
      "path": "graph/graph-ui.tsx",
      "content": "\"use client\";\n\nimport { type ComponentProps, type KeyboardEvent, type ReactNode, useCallback, useEffect, useId, useRef, useState } from \"react\";\nimport { StatusGlyph, StepStatusIcon } from \"../agent/step-list\";\nimport { GRAPH_STATUS_LABELS, type GraphStatus } from \"./graph-layout\";\n\nexport function cx(...classes: Array<string | false | null | undefined>) {\n  return classes.filter(Boolean).join(\" \");\n}\n\n/** Status text colors from the design system. Neutral states stay gray; color is reserved for meaning. */\nexport const GRAPH_STATUS_TEXT: Record<GraphStatus, string> = {\n  queued: \"text-zinc-500\",\n  running: \"text-sky-600 dark:text-sky-400\",\n  \"waiting-for-approval\": \"text-amber-600 dark:text-amber-400\",\n  succeeded: \"text-emerald-600 dark:text-emerald-400\",\n  failed: \"text-red-600 dark:text-red-400\",\n  skipped: \"text-zinc-500\",\n  cancelled: \"text-zinc-500\",\n};\n\n/** Hairline and divider recipes shared by every graph surface. */\nexport const hairline = \"border-zinc-950/10 dark:border-white/10\";\nexport const divider = \"border-zinc-950/[0.07] dark:border-white/[0.08]\";\n\n/** The small secondary button used in graph headers and the canvas toolbar. */\nexport const graphButtonClass = cx(\n  \"inline-flex h-7 items-center justify-center gap-1.5 rounded-lg border px-2.5 text-xs font-medium whitespace-nowrap select-none\",\n  \"border-zinc-950/12 bg-white text-zinc-700 hover:border-zinc-950/20 hover:bg-zinc-950/[0.03] hover:text-zinc-950\",\n  \"dark:border-white/15 dark:bg-black dark:text-zinc-300 dark:hover:border-white/25 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n  \"transition-colors duration-150 ease-out motion-reduce:transition-none\",\n  \"disabled:pointer-events-none disabled:opacity-45\",\n  \"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n);\n\n/**\n * Status icon for graph nodes, from the shared stroke glyph set. Step statuses reuse\n * `StepStatusIcon`; waiting for approval shows the pause glyph and cancelled the slash, so no\n * state relies on color alone. Remount with `key={status}` to play the status-change entry.\n */\nexport function GraphStatusIcon({ status, className = \"\" }: { status: GraphStatus; className?: string }) {\n  if (status === \"waiting-for-approval\") return <StatusGlyph kind=\"waiting\" className={className} />;\n  if (status === \"cancelled\") return <StatusGlyph kind=\"skipped\" className={className} />;\n  return <StepStatusIcon status={status} className={className} />;\n}\n\n/** Every kind of node the graphs draw an icon for. */\nexport type GraphIconName =\n  | \"plan\"\n  | \"step\"\n  | \"tool\"\n  | \"model\"\n  | \"agent\"\n  | \"approval\"\n  | \"result\"\n  | \"handoff\"\n  | \"person\"\n  | \"system\"\n  | \"search\"\n  | \"design\"\n  | \"code\"\n  | \"test\"\n  | \"docs\"\n  | \"check\"\n  | \"release\";\n\n// 16px grid, 1.5px strokes, round caps and joins: the same drawing rules as the status glyphs,\n// but never a full circle, so a kind icon is not mistaken for a status.\nconst graphIcons: Record<GraphIconName, ReactNode> = {\n  plan: <path d=\"M6.25 4h7M6.25 8h7M6.25 12h4.5M3 4h.01M3 8h.01M3 12h.01\" />,\n  step: (\n    <>\n      <rect x=\"2.75\" y=\"2.75\" width=\"10.5\" height=\"10.5\" rx=\"2.75\" />\n      <path d=\"m7 5.75 2.25 2.25L7 10.25\" />\n    </>\n  ),\n  tool: <path d=\"M5.75 3C4.6 3 4.25 3.6 4.25 4.6v1.6c0 .85-.45 1.45-1.25 1.8.8.35 1.25.95 1.25 1.8v1.6c0 1 .35 1.6 1.5 1.6M10.25 3c1.15 0 1.5.6 1.5 1.6v1.6c0 .85.45 1.45 1.25 1.8-.8.35-1.25.95-1.25 1.8v1.6c0 1-.35 1.6-1.5 1.6\" />,\n  model: <path d=\"M8 2.25Q8.6 7.4 13.75 8 8.6 8.6 8 13.75 7.4 8.6 2.25 8 7.4 7.4 8 2.25Z\" />,\n  agent: (\n    <>\n      <rect x=\"2.75\" y=\"5\" width=\"10.5\" height=\"8.25\" rx=\"2.5\" />\n      <path d=\"M8 2.5V5M6 8.75v.75M10 8.75v.75\" />\n    </>\n  ),\n  approval: <path d=\"M6.25 2.75h3.5v3.25l3 2.5v1.75h-9.5V8.5l3-2.5ZM3.25 13.25h9.5\" />,\n  result: <path d=\"M4 13.5V2.75M4 3.25h7.75L10 6l1.75 2.75H4\" />,\n  handoff: <path d=\"M3 5.25h9.5M10.25 3l2.25 2.25-2.25 2.25M13 10.75H3.5M5.75 8.5 3.5 10.75 5.75 13\" />,\n  person: <path d=\"M8 7.75a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5ZM3.25 13.25c.65-2.2 2.5-3.5 4.75-3.5s4.1 1.3 4.75 3.5\" />,\n  system: (\n    <>\n      <rect x=\"2.75\" y=\"2.75\" width=\"10.5\" height=\"4.25\" rx=\"1.25\" />\n      <rect x=\"2.75\" y=\"9\" width=\"10.5\" height=\"4.25\" rx=\"1.25\" />\n      <path d=\"M5.25 4.9h.01M5.25 11.1h.01\" />\n    </>\n  ),\n  search: <path d=\"M7 11.25a4.25 4.25 0 1 0 0-8.5 4.25 4.25 0 0 0 0 8.5ZM10.1 10.1l3.15 3.15\" />,\n  design: <path d=\"M10.5 2.75 13.25 5.5 5.75 13H3v-2.75ZM9 4.25 11.75 7\" />,\n  code: <path d=\"M5.5 4.5 2 8l3.5 3.5M10.5 4.5 14 8l-3.5 3.5\" />,\n  test: <path d=\"M6.25 2.75h3.5M6.75 2.75v3.5L3.4 12a1 1 0 0 0 .87 1.5h7.46a1 1 0 0 0 .87-1.5L9.25 6.25v-3.5M4.6 9.75h6.8\" />,\n  docs: <path d=\"M9.25 2.75H5A1.25 1.25 0 0 0 3.75 4v8A1.25 1.25 0 0 0 5 13.25h6A1.25 1.25 0 0 0 12.25 12V5.75ZM9.25 2.75v3h3M6 8.75h4M6 11h2.5\" />,\n  check: <path d=\"m2.75 4.25 1.25 1.25L6.5 3M2.75 10.25 4 11.5l2.5-2.5M8.75 4.5h4.5M8.75 10.5h4.5\" />,\n  release: <path d=\"M8 10.25V2.75M5 5.75l3-3 3 3M2.75 10v2A1.25 1.25 0 0 0 4 13.25h8A1.25 1.25 0 0 0 13.25 12v-2\" />,\n};\n\n/** The icon for a kind of node: plan, step, tool call, model call, agent, approval, result, and more. */\nexport function GraphNodeIcon({ name, className }: { name: GraphIconName; className?: string }) {\n  return (\n    <svg\n      aria-hidden=\"true\"\n      viewBox=\"0 0 16 16\"\n      fill=\"none\"\n      stroke=\"currentColor\"\n      strokeWidth=\"1.5\"\n      strokeLinecap=\"round\"\n      strokeLinejoin=\"round\"\n      className={cx(\"size-4 shrink-0\", className)}\n    >\n      {graphIcons[name]}\n    </svg>\n  );\n}\n\n/** Icon plus the status as text. */\nexport function GraphStatusLabel({ status, label, className }: { status: GraphStatus; label?: string; className?: string }) {\n  return (\n    <span className={cx(\"inline-flex items-center gap-1.5 text-xs font-medium text-zinc-950 dark:text-white\", className)}>\n      <GraphStatusIcon key={status} status={status} />\n      {label ?? GRAPH_STATUS_LABELS[status]}\n    </span>\n  );\n}\n\n/** Controlled when `value` is defined (including `null`), otherwise keeps its own state. */\nexport function useControllableState<T>(value: T | undefined, defaultValue: T, onChange?: (next: T) => void): [T, (next: T) => void] {\n  const [own, setOwn] = useState(defaultValue);\n  const controlled = value !== undefined;\n  const current = controlled ? value : own;\n  const set = useCallback(\n    (next: T) => {\n      if (!controlled) setOwn(next);\n      onChange?.(next);\n    },\n    [controlled, onChange],\n  );\n  return [current, set];\n}\n\nexport type GraphView = \"graph\" | \"list\";\n\nconst viewIcons: Record<GraphView, ReactNode> = {\n  graph: (\n    <>\n      <rect x=\"2\" y=\"2.5\" width=\"5\" height=\"3.5\" rx=\"1\" />\n      <rect x=\"9\" y=\"10\" width=\"5\" height=\"3.5\" rx=\"1\" />\n      <path d=\"M4.5 6v2.5a1.5 1.5 0 0 0 1.5 1.5h3\" />\n    </>\n  ),\n  list: <path d=\"M2.5 4h11M2.5 8h11M2.5 12h7\" />,\n};\n\n/** Switches between the graph and its list. Both show the same data and share selection. */\nexport function GraphViewSwitch({ view, onChange, className }: { view: GraphView; onChange: (view: GraphView) => void; className?: string }) {\n  return (\n    <div role=\"group\" aria-label=\"View\" className={cx(\"inline-flex rounded-lg border p-0.5\", hairline, className)}>\n      {([\"graph\", \"list\"] as const).map((value) => (\n        <button\n          key={value}\n          type=\"button\"\n          aria-pressed={view === value}\n          onClick={() => onChange(value)}\n          className={cx(\n            \"inline-flex h-6 items-center gap-1.5 rounded-md px-2 text-xs font-medium text-zinc-500 select-none\",\n            \"transition-colors duration-150 ease-out motion-reduce:transition-none\",\n            \"hover:text-zinc-950 aria-pressed:bg-zinc-950/[0.06] aria-pressed:text-zinc-950\",\n            \"dark:hover:text-white dark:aria-pressed:bg-white/[0.09] dark:aria-pressed:text-white\",\n            \"focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-sky-500\",\n          )}\n        >\n          <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-3.5\">\n            {viewIcons[value]}\n          </svg>\n          {value === \"graph\" ? \"Graph\" : \"List\"}\n        </button>\n      ))}\n    </div>\n  );\n}\n\nexport interface GraphListboxProps<T> {\n  items: readonly T[];\n  getId: (item: T) => string;\n  selectedId: string | null;\n  onSelect: (id: string) => void;\n  renderItem: (item: T, selected: boolean) => ReactNode;\n  /** Accessible name of the list. */\n  label: string;\n  className?: string;\n  /** Shown when there are no items. */\n  empty?: ReactNode;\n}\n\n/**\n * A single-select listbox with one tab stop. Arrow keys, Home, and End move the selection\n * (selection follows focus); Enter and Space select the focused option. Scrolls the\n * selected option into view when the selection changes elsewhere, such as in the graph.\n * Selection changes are instant: this is high-frequency inspection UI.\n */\nexport function GraphListbox<T>({ items, getId, selectedId, onSelect, renderItem, label, className, empty }: GraphListboxProps<T>) {\n  const prefix = useId();\n  const refs = useRef(new Map<string, HTMLLIElement>());\n  const index = selectedId == null ? -1 : items.findIndex((item) => getId(item) === selectedId);\n  const tabStop = index >= 0 ? index : 0;\n\n  useEffect(() => {\n    if (selectedId == null) return;\n    const element = refs.current.get(selectedId);\n    if (element && typeof element.scrollIntoView === \"function\") element.scrollIntoView({ block: \"nearest\" });\n  }, [selectedId]);\n\n  const move = (to: number) => {\n    const item = items[Math.max(0, Math.min(items.length - 1, to))];\n    if (!item) return;\n    const id = getId(item);\n    onSelect(id);\n    refs.current.get(id)?.focus();\n  };\n\n  const onKeyDown = (event: KeyboardEvent<HTMLUListElement>) => {\n    const keys: Record<string, () => void> = {\n      ArrowDown: () => move(index < 0 ? 0 : index + 1),\n      ArrowUp: () => move(index < 0 ? 0 : index - 1),\n      Home: () => move(0),\n      End: () => move(items.length - 1),\n    };\n    const action = keys[event.key];\n    if (action) {\n      event.preventDefault();\n      action();\n    }\n  };\n\n  if (!items.length) return <p className=\"px-4 py-6 text-sm text-zinc-500\">{empty ?? \"Nothing to show yet.\"}</p>;\n\n  return (\n    <ul role=\"listbox\" aria-label={label} onKeyDown={onKeyDown} className={cx(\"grid grid-cols-[minmax(0,1fr)] content-start gap-px p-1.5\", className)}>\n      {items.map((item, i) => {\n        const id = getId(item);\n        const selected = id === selectedId;\n        return (\n          <li\n            key={id}\n            id={`${prefix}-${i}`}\n            data-id={id}\n            ref={(element) => {\n              if (element) refs.current.set(id, element);\n              else refs.current.delete(id);\n            }}\n            role=\"option\"\n            aria-selected={selected}\n            tabIndex={i === tabStop ? 0 : -1}\n            onClick={() => onSelect(id)}\n            onKeyDown={(event) => {\n              if (event.key === \"Enter\" || event.key === \" \") {\n                event.preventDefault();\n                onSelect(id);\n              }\n            }}\n            className={cx(\n              \"min-w-0 cursor-pointer rounded-lg px-2.5 py-2 text-left\",\n              \"focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-sky-500\",\n              selected ? \"bg-zinc-950/[0.06] dark:bg-white/[0.09]\" : \"hover:bg-zinc-950/[0.04] dark:hover:bg-white/[0.06]\",\n            )}\n          >\n            {renderItem(item, selected)}\n          </li>\n        );\n      })}\n    </ul>\n  );\n}\n\n/** One list row: status glyph, a name, an optional trailing value, and a meta line. Shared by every list view. */\nexport function GraphListRow({\n  status,\n  icon,\n  title,\n  mono,\n  muted,\n  struck,\n  trailing,\n  meta,\n  children,\n}: {\n  status?: GraphStatus;\n  /** Replaces the status glyph, for example an avatar. */\n  icon?: ReactNode;\n  title: ReactNode;\n  mono?: boolean;\n  /** Secondary text color for steps that have not started. */\n  muted?: boolean;\n  /** Strike the title through, for skipped steps. */\n  struck?: boolean;\n  /** Right-aligned value such as a duration. */\n  trailing?: ReactNode;\n  meta?: ReactNode;\n  children?: ReactNode;\n}) {\n  return (\n    <div className=\"flex items-start gap-2.5\">\n      <span className=\"flex h-5 shrink-0 items-center\">{icon ?? (status ? <GraphStatusIcon key={status} status={status} /> : null)}</span>\n      <div className=\"min-w-0 flex-1\">\n        <div className=\"flex items-baseline gap-2\">\n          <p\n            className={cx(\n              \"min-w-0 flex-1 truncate text-sm leading-5 font-medium\",\n              muted ? \"text-zinc-600 dark:text-zinc-400\" : \"text-zinc-950 dark:text-white\",\n              mono && \"font-mono text-[13px]\",\n              struck && \"line-through decoration-zinc-950/30 dark:decoration-white/30\",\n            )}\n          >\n            {title}\n          </p>\n          {trailing != null ? <span className=\"shrink-0 font-mono text-[11px] text-zinc-500 tabular-nums\">{trailing}</span> : null}\n        </div>\n        {meta ? <p className=\"mt-0.5 truncate text-xs text-zinc-500\">{meta}</p> : null}\n        {children}\n      </div>\n    </div>\n  );\n}\n\n/** Code, JSON, and quoted values inside a panel. */\nexport function Well({ children, className }: { children: ReactNode; className?: string }) {\n  return (\n    <pre\n      className={cx(\n        \"max-h-44 overflow-auto rounded-lg border px-3 py-2.5 font-mono text-xs leading-relaxed whitespace-pre-wrap text-zinc-800\",\n        \"border-zinc-950/[0.07] bg-zinc-950/[0.02] dark:border-white/[0.08] dark:bg-transparent dark:text-zinc-200\",\n        className,\n      )}\n    >\n      {children}\n    </pre>\n  );\n}\n\n/** A labelled block in a details panel. Strings render in a well. */\nexport function DetailSection({ label, children, aside }: { label: string; children: ReactNode; aside?: ReactNode }) {\n  return (\n    <div className=\"min-w-0\">\n      <div className=\"mb-1.5 flex items-baseline justify-between gap-2\">\n        <p className=\"text-xs font-medium text-zinc-500\">{label}</p>\n        {aside ? <span className=\"font-mono text-[11px] text-zinc-500 tabular-nums\">{aside}</span> : null}\n      </div>\n      {typeof children === \"string\" ? <Well>{children}</Well> : <div className=\"text-sm text-zinc-700 dark:text-zinc-300\">{children}</div>}\n    </div>\n  );\n}\n\n/** Title and status line at the top of a details panel. */\nexport function DetailHeader({ title, mono, status, statusLabel, meta }: { title: ReactNode; mono?: boolean; status?: GraphStatus; statusLabel?: string; meta?: ReactNode[] }) {\n  const parts = (meta ?? []).filter((part) => part != null && part !== false && part !== \"\");\n  return (\n    <div className=\"min-w-0\">\n      <h4 className={cx(\"text-sm font-semibold break-words text-zinc-950 dark:text-white\", mono && \"font-mono text-[13px]\")}>{title}</h4>\n      <div className=\"mt-1.5 flex flex-wrap items-center gap-x-2 gap-y-1 text-xs text-zinc-500\">\n        {status ? <GraphStatusLabel status={status} label={statusLabel} /> : null}\n        {parts.map((part, i) => (\n          <span key={i} className=\"inline-flex items-center gap-2\">\n            {status || i > 0 ? <span aria-hidden=\"true\" className=\"text-zinc-950/25 dark:text-white/25\">·</span> : null}\n            {part}\n          </span>\n        ))}\n      </div>\n    </div>\n  );\n}\n\n/** Key-value facts such as kind, duration, and start time. */\nexport function DetailProperties({ items }: { items: { label: string; value: ReactNode; mono?: boolean }[] }) {\n  return (\n    <dl className=\"grid grid-cols-[minmax(5.5rem,auto)_minmax(0,1fr)] gap-x-4 gap-y-1.5 text-xs\">\n      {items.map((item) => (\n        <div key={item.label} className=\"contents\">\n          <dt className=\"text-zinc-500\">{item.label}</dt>\n          <dd className={cx(\"min-w-0 break-words text-zinc-950 dark:text-white\", item.mono && \"font-mono tabular-nums\")}>{item.value}</dd>\n        </div>\n      ))}\n    </dl>\n  );\n}\n\n/** A failure message with the failure tint. */\nexport function ErrorNote({ children }: { children: ReactNode }) {\n  return (\n    <div className=\"flex items-start gap-2 rounded-lg border border-red-500/35 bg-red-500/[0.08] px-3 py-2 text-sm text-red-700 dark:text-red-300\">\n      <GraphStatusIcon status=\"failed\" className=\"mt-0.5\" />\n      <p className=\"min-w-0 break-words\">{children}</p>\n    </div>\n  );\n}\n\n/** The scrolling stack of sections in a details panel, separated by dividers. */\nexport function DetailStack({ children, className }: { children: ReactNode; className?: string }) {\n  return <div className={cx(\"divide-y divide-zinc-950/[0.07] dark:divide-white/[0.08] [&>*]:px-4 [&>*]:py-3.5\", className)}>{children}</div>;\n}\n\n/** A related node in a details panel. Selecting it moves the selection there. */\nexport function RelatedButton({ label, status, note, onClick, mono }: { label: string; status?: GraphStatus; note?: string; onClick: () => void; mono?: boolean }) {\n  return (\n    <button\n      type=\"button\"\n      onClick={onClick}\n      className={cx(\n        \"group -mx-2 flex min-h-8 w-[calc(100%+1rem)] items-center gap-2 rounded-md px-2 py-1 text-left text-sm text-zinc-700 dark:text-zinc-300\",\n        \"hover:bg-zinc-950/[0.04] hover:text-zinc-950 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n        \"focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-sky-500\",\n      )}\n    >\n      {status ? <GraphStatusIcon status={status} /> : null}\n      <span className={cx(\"min-w-0 flex-1 truncate\", mono && \"font-mono text-[13px]\")}>{label}</span>\n      {status ? <span className=\"sr-only\">, {GRAPH_STATUS_LABELS[status]}</span> : null}\n      {note ? <span className=\"shrink-0 text-xs text-zinc-500\">{note}</span> : null}\n      <svg\n        aria-hidden=\"true\"\n        viewBox=\"0 0 16 16\"\n        fill=\"none\"\n        stroke=\"currentColor\"\n        strokeWidth=\"1.5\"\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n        className=\"size-3.5 shrink-0 text-zinc-950/25 group-hover:text-zinc-950/60 dark:text-white/25 dark:group-hover:text-white/60\"\n      >\n        <path d=\"m6 4 4 4-4 4\" />\n      </svg>\n    </button>\n  );\n}\n\n/** Quiet text for an empty related list. */\nexport function DetailNote({ children }: { children: ReactNode }) {\n  return <p className=\"text-xs text-zinc-500\">{children}</p>;\n}\n\nexport function formatDuration(ms: number): string {\n  return ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(1)} s`;\n}\n\nconst SUMMARY_ORDER: GraphStatus[] = [\"running\", \"waiting-for-approval\", \"failed\", \"succeeded\", \"queued\", \"skipped\", \"cancelled\"];\n\nfunction countStatuses(statuses: readonly GraphStatus[]) {\n  const counts = new Map<GraphStatus, number>();\n  for (const status of statuses) counts.set(status, (counts.get(status) ?? 0) + 1);\n  return SUMMARY_ORDER.filter((status) => counts.get(status)).map((status) => ({ status, count: counts.get(status)! }));\n}\n\n/** Counts per status, in a fixed order, for summaries like \"3 succeeded · 1 failed\". */\nexport function statusSummary(statuses: readonly GraphStatus[]): string {\n  return countStatuses(statuses)\n    .map(({ status, count }) => `${count} ${GRAPH_STATUS_LABELS[status].toLowerCase()}`)\n    .join(\" · \");\n}\n\n/** The status summary drawn as glyph, count, and word, for a header meta line. Reads as plain text. */\nexport function StatusCounts({ statuses, lead }: { statuses: readonly GraphStatus[]; lead?: ReactNode }) {\n  const counts = countStatuses(statuses);\n  return (\n    <span className=\"inline-flex flex-wrap items-center gap-x-3 gap-y-1\">\n      {lead != null ? <span className=\"text-zinc-600 dark:text-zinc-400\">{lead}</span> : null}\n      {counts.map(({ status, count }) => (\n        <span key={status} className=\"inline-flex items-center gap-1 [&>svg]:size-3.5\">\n          <GraphStatusIcon status={status} />\n          <span className=\"font-mono text-zinc-700 tabular-nums dark:text-zinc-300\">{count}</span>\n          {GRAPH_STATUS_LABELS[status].toLowerCase()}\n        </span>\n      ))}\n    </span>\n  );\n}\n\nexport interface GraphShellProps extends Omit<ComponentProps<\"section\">, \"title\"> {\n  title: ReactNode;\n  /** One line under the title, for example a status summary. */\n  meta?: ReactNode;\n  /** Buttons on the right of the header, such as the view switch. */\n  controls?: ReactNode;\n  /** Accessible name of the details panel. */\n  detailsLabel: string;\n  details: ReactNode;\n  /** Height of the main area; the details panel scrolls within it on wide layouts. */\n  height: number;\n  footer?: ReactNode;\n}\n\n/** The container recipe: white in light mode, pure black with a hairline in dark mode. */\nexport const graphSurfaceClass = cx(\n  \"@container w-full overflow-hidden rounded-xl border bg-white text-left shadow-[0_1px_2px_rgb(0_0_0/0.04)]\",\n  \"dark:bg-black dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]\",\n  hairline,\n);\n\n/** Header with title and meta. */\nexport function GraphHeader({ title, meta, controls }: { title: ReactNode; meta?: ReactNode; controls?: ReactNode }) {\n  return (\n    <header className={cx(\"flex flex-wrap items-center justify-between gap-x-4 gap-y-2.5 border-b px-4 py-3\", divider)}>\n      <div className=\"min-w-0 flex-[1_1_16rem]\">\n        <h3 className=\"text-sm font-semibold text-zinc-950 dark:text-white\">{title}</h3>\n        {meta ? <div className=\"mt-1 text-xs text-zinc-500\">{meta}</div> : null}\n      </div>\n      {controls ? <div className=\"flex shrink-0 flex-wrap items-center gap-2\">{controls}</div> : null}\n    </header>\n  );\n}\n\n/** Header, main area, and a details panel beside it (below it when the component is narrow). */\nexport function GraphShell({ title, meta, controls, detailsLabel, details, height, footer, className, children, ...props }: GraphShellProps) {\n  return (\n    <section className={cx(graphSurfaceClass, className)} {...props}>\n      <GraphHeader title={title} meta={meta} controls={controls} />\n      <div className=\"grid @2xl:grid-cols-[minmax(0,1fr)_18rem]\">\n        <div className=\"min-w-0\">{children}</div>\n        <aside\n          aria-label={detailsLabel}\n          style={{ [\"--graph-height\" as string]: `${height}px` }}\n          className={cx(\"min-w-0 border-t @2xl:max-h-(--graph-height) @2xl:overflow-y-auto @2xl:border-t-0 @2xl:border-l\", divider)}\n        >\n          {details}\n        </aside>\n      </div>\n      {footer ? <footer className={cx(\"border-t px-4 py-2.5\", divider)}>{footer}</footer> : null}\n    </section>\n  );\n}\n\n/**\n * Explains edge styles in text, so edge types never rely on color or dash alone. Each sample\n * is drawn like the canvas edges: a port ring, the line, and the swept arrowhead. `active`\n * shows the running color used for edges that feed running work.\n */\nexport function EdgeLegend({ items }: { items: { label: string; dashed?: boolean; active?: boolean }[] }) {\n  return (\n    <ul aria-label=\"Edge types\" className=\"flex flex-wrap gap-x-5 gap-y-1 text-xs text-zinc-500\">\n      {items.map((item) => (\n        <li key={item.label} className=\"flex items-center gap-2\">\n          <svg\n            aria-hidden=\"true\"\n            viewBox=\"0 0 32 8\"\n            className={cx(\"h-2 w-8\", item.active ? \"text-sky-600 dark:text-sky-400\" : \"text-zinc-950/40 dark:text-white/45\")}\n          >\n            <circle cx=\"3.5\" cy=\"4\" r=\"2.5\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.25\" />\n            <path d=\"M6 4h18.5\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeDasharray={item.dashed ? \"3.5 2.5\" : undefined} />\n            <path d=\"M31 4 24 0.75 25.75 4 24 7.25Z\" fill=\"currentColor\" />\n          </svg>\n          {item.label}\n        </li>\n      ))}\n    </ul>\n  );\n}\n\n/** Placeholder for the details panel before anything is selected. */\nexport function EmptyDetails({ children, hint }: { children: ReactNode; hint?: ReactNode }) {\n  return (\n    <div className=\"flex flex-col gap-3 p-4\">\n      <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-5 text-zinc-950/30 dark:text-white/30\">\n        <rect x=\"2.5\" y=\"3\" width=\"6\" height=\"4.5\" rx=\"1.25\" />\n        <rect x=\"11.5\" y=\"12.5\" width=\"6\" height=\"4.5\" rx=\"1.25\" />\n        <path d=\"M5.5 7.5v3a2 2 0 0 0 2 2h4\" />\n      </svg>\n      <p className=\"text-sm text-pretty text-zinc-600 dark:text-zinc-400\">{children}</p>\n      {hint ? <p className=\"text-xs text-pretty text-zinc-500\">{hint}</p> : null}\n    </div>\n  );\n}\n\n/** Up to three names, then \"+N more\". */\nexport function nameList(names: readonly string[]): string {\n  return names.length <= 3 ? names.join(\", \") : `${names.slice(0, 3).join(\", \")} +${names.length - 3} more`;\n}\n"
    },
    {
      "path": "graph/graph-canvas.tsx",
      "content": "\"use client\";\n\nimport \"@xyflow/react/dist/style.css\";\nimport {\n  Background,\n  BackgroundVariant,\n  BaseEdge,\n  type Edge,\n  EdgeLabelRenderer,\n  type EdgeProps,\n  type FitViewOptions,\n  Handle,\n  MiniMap,\n  type Node,\n  type NodeProps,\n  Panel,\n  type ReactFlowState,\n  Position,\n  ReactFlow,\n  ReactFlowProvider,\n  useReactFlow,\n  useStore,\n  useStoreApi,\n  ViewportPortal,\n} from \"@xyflow/react\";\nimport { type CSSProperties, createContext, memo, type ReactNode, useContext, useEffect, useId, useMemo, useRef, useState, useSyncExternalStore } from \"react\";\nimport {\n  GRAPH_STATUS_LABELS,\n  type GraphDirection,\n  type GraphEdgeRef,\n  type GraphLayout,\n  type GraphLayoutOptions,\n  type GraphNodeRef,\n  type GraphStatus,\n  layoutGraph,\n  type Point,\n  structureKey,\n} from \"./graph-layout\";\nimport { cx, type GraphIconName, GraphNodeIcon, GraphStatusIcon } from \"./graph-ui\";\n\n/** How a node relates to the current selection. Shown with a text tag as well as color. */\nexport type GraphNodeTone = \"default\" | \"upstream\" | \"downstream\" | \"muted\";\nexport type GraphEdgeTone = \"default\" | \"highlight\" | \"selected\" | \"muted\";\n\n/** A node as the canvas draws it. Plain data, so unchanged nodes are not re-rendered. */\nexport interface GraphCanvasNode {\n  id: string;\n  title: string;\n  /** Secondary text after the status, for example the node kind. */\n  subtitle?: string;\n  /** Render the title in a monospace font, for tool names. */\n  mono?: boolean;\n  status?: GraphStatus;\n  /** Replaces the default status text, for example \"Pending\" for a hand-off. */\n  statusLabel?: string;\n  /** The kind icon drawn in the node's tile, for example \"tool\" or \"model\". Defaults to \"step\". */\n  icon?: GraphIconName;\n  /** Initials shown in the tile instead of the icon, for example for a person. */\n  avatar?: string;\n  /** Shape of the avatar tile: a circle (default) for people, a square for agents and systems. */\n  avatarShape?: \"circle\" | \"square\";\n  /** A short text tag at the start of the node's meta line, for example \"Upstream\". */\n  tag?: string;\n  tone?: GraphNodeTone;\n  hidden?: boolean;\n}\n\nexport interface GraphCanvasEdge {\n  id: string;\n  source: string;\n  target: string;\n  type: string;\n  label?: string;\n  /** Status icon shown in the label. */\n  labelStatus?: GraphStatus;\n  dashed?: boolean;\n  tone?: GraphEdgeTone;\n  /**\n   * The edge feeds work that is running now. Drawn in the running color, with a light pulse\n   * traveling along it into the running node while `animateEdges` is on and the user has not\n   * asked for reduced motion.\n   */\n  animated?: boolean;\n  hidden?: boolean;\n  /** Clicking the edge or its label calls `onEdgeClick`. */\n  selectable?: boolean;\n}\n\nexport interface GraphCanvasProps {\n  nodes: readonly GraphCanvasNode[];\n  edges: readonly GraphCanvasEdge[];\n  /** From `useGraphLayout`. Positions come from here, never from statuses. */\n  layout: GraphLayout;\n  selectedNodeId?: string | null;\n  selectedEdgeId?: string | null;\n  onNodeClick?: (id: string) => void;\n  onEdgeClick?: (id: string) => void;\n  onPaneClick?: () => void;\n  /** React Flow needs a sized container. Defaults to 420px. */\n  height?: number | string;\n  /** Accessible name of the canvas region. */\n  label: string;\n  /** Show the mini map. Defaults to on for graphs with more than 24 nodes. */\n  minimap?: boolean;\n  /**\n   * Send a light pulse along edges marked `animated`, into the running work they feed.\n   * On by default; always off with reduced motion, where those edges keep the running color.\n   */\n  animateEdges?: boolean;\n  /** At most this many edges pulse at once, so large graphs stay fast. Defaults to 12. */\n  maxAnimatedEdges?: number;\n  /** Show the dot grid behind the graph. Defaults to on. */\n  grid?: boolean;\n  className?: string;\n  /** Extra controls rendered in the canvas, top left. */\n  toolbar?: ReactNode;\n}\n\nconst REDUCED_MOTION = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribeReducedMotion(callback: () => void) {\n  const query = window.matchMedia(REDUCED_MOTION);\n  query.addEventListener(\"change\", callback);\n  return () => query.removeEventListener(\"change\", callback);\n}\n\n/** True when the user asked for reduced motion. False during server rendering. */\nexport function usePrefersReducedMotion(): boolean {\n  return useSyncExternalStore(\n    subscribeReducedMotion,\n    () => window.matchMedia(REDUCED_MOTION).matches,\n    () => false,\n  );\n}\n\n/**\n * Lays the graph out once per structure. Status, label, and type changes keep the same\n * layout object, so live updates never move nodes; adding or removing nodes or edges re-runs it.\n */\nexport function useGraphLayout(nodes: readonly GraphNodeRef[], edges: readonly GraphEdgeRef[], options: GraphLayoutOptions = {}): GraphLayout {\n  const key = structureKey(nodes, edges, options);\n  const latest = useRef({ nodes, edges, options });\n  latest.current = { nodes, edges, options };\n  // Keyed on the structure only: dagre is deterministic, so equal keys give equal layouts.\n  return useMemo(() => layoutGraph(latest.current.nodes, latest.current.edges, latest.current.options), [key]);\n}\n\n// ---------------------------------------------------------------------------------------\n// Ports and edge geometry\n\n/**\n * Port state, in order of emphasis: none (no edge uses it), hover (shown while the node is\n * hovered), path (on the selected path), active (feeds running work), selected.\n */\ntype PortState = 0 | 1 | 2 | 3 | 4;\n/** Entry, exit, and the two ports used by edges that point back up the graph. */\ntype Ports = [entry: PortState, exit: PortState, backEntry: PortState, backExit: PortState];\n\nconst PORT_SIZE = 8;\n/** Space between an arrow's tip and the node edge: the arrow tip just meets the port ring. */\nconst ARROW_GAP = 3.5;\n/** From the arrowhead's notch, where the line ends, to its tip. Matches the marker path. */\nconst ARROW_REACH = 5.25;\n/** Back edges use ports a quarter of the side away from the center, clear of the forward ports. */\nconst BACK_SHIFT = 0.25;\n\nconst round = (value: number) => Math.round(value * 100) / 100;\n\nfunction cubicLength(a: Point, b: Point, c: Point, d: Point): number {\n  let length = 0;\n  let previous = a;\n  for (let step = 1; step <= 8; step += 1) {\n    const t = step / 8;\n    const u = 1 - t;\n    const point = {\n      x: u * u * u * a.x + 3 * u * u * t * b.x + 3 * u * t * t * c.x + t * t * t * d.x,\n      y: u * u * u * a.y + 3 * u * u * t * b.y + 3 * u * t * t * c.y + t * t * t * d.y,\n    };\n    length += Math.hypot(point.x - previous.x, point.y - previous.y);\n    previous = point;\n  }\n  return length;\n}\n\ntype Segment = [Point, Point, Point, Point];\ninterface EdgeGeometry {\n  path: string;\n  length: number;\n  back: boolean;\n  /** The exit port on the source and the entry port on the target, on their outer edges. */\n  from: Point;\n  to: Point;\n  /** The cubic spans of the path, for sampling the pulse's motion. */\n  segments: Segment[];\n}\n\nfunction cubicAt([a, b, c, d]: Segment, t: number): Point {\n  const u = 1 - t;\n  return {\n    x: u * u * u * a.x + 3 * u * u * t * b.x + 3 * u * t * t * c.x + t * t * t * d.x,\n    y: u * u * u * a.y + 3 * u * u * t * b.y + 3 * u * t * t * c.y + t * t * t * d.y,\n  };\n}\n\n/** Points evenly spaced along the path, with the travel direction at each, for the pulse keyframes. */\nfunction samplePath(segments: Segment[], count: number): { x: number; y: number; angle: number }[] {\n  const dense: Point[] = [];\n  for (const segment of segments) for (let step = dense.length ? 1 : 0; step <= 24; step += 1) dense.push(cubicAt(segment, step / 24));\n  const lengths = [0];\n  for (let i = 1; i < dense.length; i += 1) lengths.push(lengths[i - 1]! + Math.hypot(dense[i]!.x - dense[i - 1]!.x, dense[i]!.y - dense[i - 1]!.y));\n  const total = lengths[lengths.length - 1] || 1;\n  const samples: { x: number; y: number; angle: number }[] = [];\n  let j = 1;\n  for (let i = 0; i <= count; i += 1) {\n    const at = (total * i) / count;\n    while (j < dense.length - 1 && lengths[j]! < at) j += 1;\n    const a = dense[j - 1]!;\n    const b = dense[j]!;\n    const span = lengths[j]! - lengths[j - 1]! || 1;\n    const t = Math.min(1, Math.max(0, (at - lengths[j - 1]!) / span));\n    samples.push({ x: round(a.x + (b.x - a.x) * t), y: round(a.y + (b.y - a.y) * t), angle: round(Math.atan2(b.y - a.y, b.x - a.x)) });\n  }\n  return samples;\n}\n\n/**\n * A smooth edge from the source's exit port to the target's entry port. It leaves and enters\n * straight along the layout direction, so arrowheads always sit square to the node, and passes\n * through dagre's waypoints only at node ranks (where they keep the line clear of other nodes)\n * and at the label. Each span between waypoints is one S-shaped cubic curve.\n */\nfunction edgeRoute(\n  route: { points: Point[]; label: Point },\n  source: Point,\n  target: Point,\n  size: { width: number; height: number },\n  direction: GraphDirection,\n  ranks: ReadonlySet<number>,\n  hasLabel: boolean,\n): EdgeGeometry {\n  const { width, height } = size;\n  const vertical = direction === \"TB\";\n  const axis = (point: Point) => (vertical ? point.y : point.x);\n  const back = vertical ? target.y < source.y : target.x < source.x;\n  const sign = back ? -1 : 1;\n  let start: Point;\n  let end: Point;\n  if (vertical) {\n    const shift = back ? width * BACK_SHIFT : 0;\n    const tip = back ? target.y + height + ARROW_GAP : target.y - ARROW_GAP;\n    start = { x: source.x + width / 2 + shift, y: back ? source.y : source.y + height };\n    end = { x: target.x + width / 2 + shift, y: tip - sign * ARROW_REACH };\n  } else {\n    const shift = back ? height * BACK_SHIFT : 0;\n    const tip = back ? target.x + width + ARROW_GAP : target.x - ARROW_GAP;\n    start = { x: back ? source.x : source.x + width, y: source.y + height / 2 + shift };\n    end = { x: tip - sign * ARROW_REACH, y: target.y + height / 2 + shift };\n  }\n  const isLabel = (point: Point) => hasLabel && Math.abs(point.x - route.label.x) < 0.5 && Math.abs(point.y - route.label.y) < 0.5;\n  const inner = route.points.slice(1, -1).filter((point) => {\n    const between = (axis(point) - axis(start)) * sign > 2 && (axis(end) - axis(point)) * sign > 2;\n    return (ranks.has(Math.round(axis(point) * 10)) || isLabel(point)) && between;\n  });\n  const points = [start, ...inner, end];\n  // Ends and rank waypoints run straight along the layout direction; a label waypoint takes the\n  // direction of its neighbors, so the edge bows through its label instead of kinking.\n  const tangents = points.map((point, i) => {\n    if (i === 0 || i === points.length - 1 || !isLabel(point)) return null;\n    const before = points[i - 1]!;\n    const after = points[i + 1]!;\n    const length = Math.hypot(after.x - before.x, after.y - before.y) || 1;\n    return { x: (after.x - before.x) / length, y: (after.y - before.y) / length };\n  });\n  let path = `M ${round(start.x)} ${round(start.y)}`;\n  let length = 0;\n  const segments: Segment[] = [];\n  for (let i = 1; i < points.length; i += 1) {\n    const a = points[i - 1]!;\n    const b = points[i]!;\n    // A little over half the gap, so each span leaves and enters square before it crosses over.\n    const half = (axis(b) - axis(a)) * 0.6;\n    const reach = Math.hypot(b.x - a.x, b.y - a.y) * 0.4;\n    const ta = tangents[i - 1];\n    const tb = tangents[i];\n    const c1 = ta ? { x: a.x + ta.x * reach, y: a.y + ta.y * reach } : vertical ? { x: a.x, y: a.y + half } : { x: a.x + half, y: a.y };\n    const c2 = tb ? { x: b.x - tb.x * reach, y: b.y - tb.y * reach } : vertical ? { x: b.x, y: b.y - half } : { x: b.x - half, y: b.y };\n    path += ` C ${round(c1.x)} ${round(c1.y)} ${round(c2.x)} ${round(c2.y)} ${round(b.x)} ${round(b.y)}`;\n    length += cubicLength(a, c1, c2, b);\n    segments.push([a, c1, c2, b]);\n  }\n  const to = vertical ? { x: end.x, y: back ? target.y + height : target.y } : { x: back ? target.x + width : target.x, y: end.y };\n  return { path, length, back, from: start, to, segments };\n}\n\n// ---------------------------------------------------------------------------------------\n// Nodes\n\ntype CardData = {\n  node: GraphCanvasNode;\n  selected: boolean;\n  clickable: boolean;\n  direction: GraphDirection;\n  width: number;\n  height: number;\n  ports: Ports;\n  /** Entrance delay in milliseconds, by rank, so the graph builds in its reading order. */\n  delay: number;\n};\ntype FlowNode = Node<CardData, \"card\">;\ntype EdgeLook = GraphEdgeTone | \"active\";\ntype RouteData = {\n  edge: GraphCanvasEdge;\n  geometry: EdgeGeometry;\n  label: Point;\n  selected: boolean;\n  look: EdgeLook;\n  pulse: boolean;\n  delay: number;\n};\ntype FlowEdge = Edge<RouteData, \"route\">;\n\nconst CanvasContext = createContext<{ onEdgeClick?: (id: string) => void; markerId: string }>({ markerId: \"graph-arrow\" });\n\nconst portColor = [\"var(--graph-port)\", \"var(--graph-port)\", \"var(--graph-port-on)\", \"var(--graph-port-active)\", \"var(--graph-port-selected)\"] as const;\n\nfunction portStyle(state: PortState, side: Position, extra?: CSSProperties): CSSProperties {\n  // React Flow's handle styles are not in a cascade layer, so the ring is styled inline.\n  return {\n    width: PORT_SIZE,\n    height: PORT_SIZE,\n    minWidth: 0,\n    minHeight: 0,\n    boxSizing: \"border-box\",\n    border: `1.5px solid ${portColor[state]}`,\n    borderRadius: 999,\n    background: \"var(--graph-port-fill)\",\n    pointerEvents: \"none\",\n    // Centered on the outer edge of the card's 1px border.\n    [side]: -1,\n    ...extra,\n  };\n}\n\nfunction portClass(state: PortState) {\n  return cx(\n    state === 0 ? \"opacity-0\" : state === 1 ? \"opacity-0 group-hover/node:opacity-100\" : \"opacity-100\",\n  );\n}\n\n// Status frames: a border tint plus a soft fill of the status hue, painted as a flat gradient\n// layer over the opaque card color so the dot grid never shows through.\nconst statusFrame: Partial<Record<GraphStatus, string>> = {\n  running: \"border-sky-500/45 bg-linear-to-b from-sky-500/[0.07] to-sky-500/[0.07]\",\n  \"waiting-for-approval\": \"border-amber-500/50 bg-linear-to-b from-amber-500/[0.09] to-amber-500/[0.09]\",\n  failed: \"border-red-500/50 bg-linear-to-b from-red-500/[0.09] to-red-500/[0.09]\",\n};\n\nconst statusWord: Partial<Record<GraphStatus, string>> = {\n  running: \"text-sky-600 dark:text-sky-400\",\n  \"waiting-for-approval\": \"text-amber-600 dark:text-amber-400\",\n  failed: \"text-red-600 dark:text-red-400\",\n};\n\nconst idleStatuses = new Set<GraphStatus>([\"queued\", \"skipped\", \"cancelled\"]);\n\nconst NodeCard = memo(function NodeCard({ data }: NodeProps<FlowNode>) {\n  const { node, selected, clickable, direction, width, height, ports, delay } = data;\n  const muted = node.tone === \"muted\" && !selected;\n  const idle = node.status ? idleStatuses.has(node.status) : false;\n  const vertical = direction !== \"LR\";\n  const entry = vertical ? Position.Top : Position.Left;\n  const exit = vertical ? Position.Bottom : Position.Right;\n  const backShift: CSSProperties = vertical ? { left: `calc(50% + ${width * BACK_SHIFT}px)` } : { top: `calc(50% + ${height * BACK_SHIFT}px)` };\n  const frame = muted ? \"border-zinc-950/[0.07] dark:border-white/[0.08]\" : (node.status && statusFrame[node.status]) || \"border-zinc-950/12 dark:border-white/15\";\n  return (\n    // The outer box plays the entrance while the canvas first appears; the card inside handles hover and selection.\n    <div\n      style={{ width, height, transitionDelay: `${delay}ms` }}\n      className={cx(\n        \"motion-safe:transition-[opacity,scale] motion-safe:duration-300 motion-safe:ease-[cubic-bezier(0.23,1,0.32,1)]\",\n        \"group-data-entering/canvas:motion-safe:starting:scale-[0.97] group-data-entering/canvas:motion-safe:starting:opacity-0\",\n      )}\n    >\n      <div\n        className={cx(\n          \"group/node relative flex size-full items-center gap-2.5 rounded-xl border bg-white px-3 text-left dark:bg-black\",\n          \"shadow-[0_1px_2px_rgb(0_0_0/0.05)] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]\",\n          \"transition-[border-color] duration-150 ease-out motion-reduce:transition-none\",\n          frame,\n          selected\n            ? \"border-zinc-950! shadow-[0_0_0_3px_rgb(9_9_11/0.08),0_8px_20px_-10px_rgb(0_0_0/0.3)] dark:border-white! dark:shadow-[0_0_0_3px_rgb(255_255_255/0.16),inset_0_1px_0_rgb(255_255_255/0.08)]\"\n            : clickable && !muted && \"hover:border-zinc-950/30 dark:hover:border-white/35\",\n        )}\n      >\n        <Handle type=\"target\" position={entry} isConnectable={false} className={portClass(ports[0])} style={portStyle(ports[0], entry)} />\n        {ports[2] ? <Handle id=\"back-in\" type=\"target\" position={exit} isConnectable={false} className={portClass(ports[2])} style={portStyle(ports[2], exit, backShift)} /> : null}\n        <span\n          aria-hidden=\"true\"\n          className={cx(\n            \"grid size-8 shrink-0 place-items-center border\",\n            node.avatar && node.avatarShape !== \"square\" ? \"rounded-full\" : \"rounded-lg\",\n            selected\n              ? \"border-transparent bg-zinc-950 text-white dark:bg-white dark:text-black\"\n              : \"border-zinc-950/[0.08] bg-zinc-950/[0.03] text-zinc-600 dark:border-white/10 dark:bg-white/[0.06] dark:text-zinc-300\",\n            muted && \"opacity-35\",\n          )}\n        >\n          {node.avatar ? <span className=\"text-[10px] leading-none font-semibold\">{node.avatar}</span> : <GraphNodeIcon name={node.icon ?? \"step\"} />}\n        </span>\n        <div className={cx(\"min-w-0 flex-1\", muted && \"opacity-35\")}>\n          <p\n            className={cx(\n              \"truncate text-[13px] leading-[18px] font-medium\",\n              idle && !selected ? \"text-zinc-600 dark:text-zinc-400\" : \"text-zinc-950 dark:text-white\",\n              node.mono && \"font-mono text-[12.5px] tracking-tight\",\n              node.status === \"skipped\" && \"line-through decoration-zinc-950/30 dark:decoration-white/30\",\n            )}\n          >\n            {node.title}\n          </p>\n          <p className=\"truncate text-[11px] leading-4 text-zinc-500\">\n            {node.tag ? (\n              <span\n                className={cx(\n                  \"mr-1.5 inline-block rounded-[4px] border px-1 align-[0.5px] text-[10px] leading-[13px] font-medium\",\n                  \"border-zinc-950/20 text-zinc-800 dark:border-white/25 dark:text-zinc-100\",\n                )}\n              >\n                {node.tag}\n              </span>\n            ) : null}\n            {node.status ? <span className={statusWord[node.status]}>{node.statusLabel ?? GRAPH_STATUS_LABELS[node.status]}</span> : null}\n            {node.status && node.subtitle ? <span aria-hidden=\"true\"> · </span> : null}\n            {node.subtitle}\n          </p>\n        </div>\n        {node.status ? (\n          <span className={cx(\"flex shrink-0\", muted && \"opacity-35\")}>\n            <GraphStatusIcon key={node.status} status={node.status} />\n          </span>\n        ) : null}\n        <Handle type=\"source\" position={exit} isConnectable={false} className={portClass(ports[1])} style={portStyle(ports[1], exit)} />\n        {ports[3] ? <Handle id=\"back-out\" type=\"source\" position={entry} isConnectable={false} className={portClass(ports[3])} style={portStyle(ports[3], entry, backShift)} /> : null}\n      </div>\n    </div>\n  );\n});\n\n// ---------------------------------------------------------------------------------------\n// Edges\n\n// React Flow's stylesheet is not in a cascade layer, so edge colors are set through its CSS\n// variables rather than stroke utilities, which it would override. Colors are opaque mixes\n// for the card background, so crossings and arrowheads never double up.\nconst edgeTone: Record<EdgeLook, string> = {\n  default: \"[--xy-edge-stroke:#b4b4bb] dark:[--xy-edge-stroke:#4b4b52] [--xy-edge-stroke-width:1.5]\",\n  highlight: \"[--xy-edge-stroke:#52525b] dark:[--xy-edge-stroke:#c8c8cc] [--xy-edge-stroke-width:1.5]\",\n  selected: \"[--xy-edge-stroke:#09090b] dark:[--xy-edge-stroke:#ffffff] [--xy-edge-stroke-width:2]\",\n  muted: \"[--xy-edge-stroke:#ececee] dark:[--xy-edge-stroke:#1f1f22] [--xy-edge-stroke-width:1.5]\",\n  active: \"[--xy-edge-stroke:#0284c7] dark:[--xy-edge-stroke:#38bdf8] [--xy-edge-stroke-width:1.5]\",\n};\n\n// Arrowhead fills, one marker per look. Markers do not inherit the edge's variables.\nconst markerFill: Record<EdgeLook, string> = {\n  default: \"fill-[#b4b4bb] dark:fill-[#4b4b52]\",\n  highlight: \"fill-[#52525b] dark:fill-[#c8c8cc]\",\n  selected: \"fill-[#09090b] dark:fill-white\",\n  muted: \"fill-[#ececee] dark:fill-[#1f1f22]\",\n  active: \"fill-[#0284c7] dark:fill-[#38bdf8]\",\n};\nconst LOOKS = Object.keys(markerFill) as EdgeLook[];\n\n/** Small, swept, filled arrowheads: 7px long, the line ending in the notch so it never shows past the tip. */\nfunction ArrowMarkers({ id }: { id: string }) {\n  return (\n    <svg aria-hidden=\"true\" width=\"0\" height=\"0\" className=\"absolute overflow-hidden\">\n      <defs>\n        {LOOKS.map((look) => (\n          <marker\n            key={look}\n            id={`${id}-${look}`}\n            viewBox=\"0 0 7 6.5\"\n            refX={7 - ARROW_REACH}\n            refY=\"3.25\"\n            markerWidth=\"7\"\n            markerHeight=\"6.5\"\n            markerUnits=\"userSpaceOnUse\"\n            orient=\"auto\"\n          >\n            <path d=\"M0 0 7 3.25 0 6.5 1.75 3.25Z\" className={markerFill[look]} />\n          </marker>\n        ))}\n      </defs>\n    </svg>\n  );\n}\n\n/** Share of each cycle the pulse spends traveling; it rests for the rest. */\nconst PULSE_TRAVEL = 0.62;\nconst PULSE_CYCLE = 2600;\n\n/**\n * A short comet of light that travels along the edge into the running node, then rests. It moves\n * with transform and opacity keyframes only, so the compositor runs it without main-thread work,\n * and every pulse shares one clock, so pulses converging on a node arrive together.\n */\nfunction Pulse({ segments }: { segments: Segment[] }) {\n  const ref = useRef<HTMLDivElement>(null);\n  useEffect(() => {\n    const element = ref.current;\n    if (!element || typeof element.animate !== \"function\") return;\n    const samples = samplePath(segments, 28);\n    const last = samples.length - 1;\n    const frames: Keyframe[] = samples.map((point, i) => {\n      const progress = i / last;\n      return {\n        // Ease in and out along the path: the inverse of a sine ease places each sample in time.\n        offset: PULSE_TRAVEL * (Math.acos(1 - 2 * progress) / Math.PI),\n        transform: `translate(${point.x}px, ${point.y}px) rotate(${point.angle}rad)`,\n        opacity: progress < 0.12 ? progress / 0.12 : progress > 0.82 ? (1 - progress) / 0.18 : 1,\n      };\n    });\n    frames.push({ offset: 1, transform: frames[last]!.transform, opacity: 0 });\n    const animation = element.animate(frames, { duration: PULSE_CYCLE, iterations: Infinity });\n    animation.startTime = 0;\n    return () => animation.cancel();\n  }, [segments]);\n  return (\n    <div\n      ref={ref}\n      aria-hidden=\"true\"\n      className={cx(\n        \"pointer-events-none absolute top-0 left-0 -mt-[1.5px] -ml-7 h-[3px] w-7 origin-right rounded-full opacity-0 will-change-transform motion-reduce:hidden\",\n        \"bg-linear-to-r from-transparent via-sky-500/35 to-sky-500 dark:via-sky-400/35 dark:to-sky-300\",\n      )}\n    >\n      <span className=\"absolute top-1/2 right-0 size-[5px] -translate-y-1/2 translate-x-1/2 rounded-full bg-sky-500 shadow-[0_0_6px_1px_rgb(14_165_233/0.55)] dark:bg-sky-200 dark:shadow-[0_0_8px_2px_rgb(56_189_248/0.6)]\" />\n    </div>\n  );\n}\n\n/** A port ring drawn by an edge on the selected path, above the nodes, so its nodes need no update. */\nfunction PathRing({ at, color }: { at: Point; color: string }) {\n  return (\n    <div\n      aria-hidden=\"true\"\n      style={{ width: PORT_SIZE, height: PORT_SIZE, transform: `translate(${at.x - PORT_SIZE / 2}px, ${at.y - PORT_SIZE / 2}px)`, borderColor: color }}\n      className=\"pointer-events-none absolute top-0 left-0 box-border rounded-full border-[1.5px] bg-(--graph-port-fill)\"\n    />\n  );\n}\n\nfunction RouteEdge({ id, data }: EdgeProps<FlowEdge>) {\n  const { onEdgeClick, markerId } = useContext(CanvasContext);\n  if (!data) return null;\n  const { edge, selected, look, geometry } = data;\n  const { path } = geometry;\n  const muted = look === \"muted\";\n  // Rings mark both ends of the selected path; an edge into running work marks its entry port.\n  const onPath = look === \"selected\" || look === \"highlight\" || (look === \"active\" && edge.tone === \"highlight\");\n  const pathColor = look === \"selected\" ? \"var(--graph-port-selected)\" : \"var(--graph-port-on)\";\n  const startRing = onPath ? pathColor : null;\n  const endRing = look === \"active\" ? \"var(--graph-port-active)\" : onPath ? pathColor : null;\n  return (\n    <>\n      <g\n        style={{ transitionDelay: `${data.delay}ms` }}\n        className={cx(edgeTone[look], \"motion-safe:transition-opacity motion-safe:duration-300 motion-safe:ease-out group-data-entering/canvas:motion-safe:starting:opacity-0\")}\n      >\n        <BaseEdge\n          id={id}\n          path={path}\n          markerEnd={`url(#${markerId}-${look})`}\n          interactionWidth={edge.selectable ? 18 : 0}\n          style={edge.dashed ? { strokeDasharray: \"5 4\" } : undefined}\n        />\n      </g>\n      {data.pulse ? (\n        <EdgeLabelRenderer>\n          <Pulse segments={geometry.segments} />\n        </EdgeLabelRenderer>\n      ) : null}\n      {startRing || endRing ? (\n        <ViewportPortal>\n          {startRing ? <PathRing at={geometry.from} color={startRing} /> : null}\n          {endRing ? <PathRing at={geometry.to} color={endRing} /> : null}\n        </ViewportPortal>\n      ) : null}\n      {edge.label ? (\n        <EdgeLabelRenderer>\n          <div\n            style={{ transform: `translate(-50%, -50%) translate(${data.label.x}px, ${data.label.y}px)` }}\n            title={edge.label}\n            onClick={edge.selectable ? () => onEdgeClick?.(id) : undefined}\n            className={cx(\n              \"nodrag nopan pointer-events-auto absolute flex h-5 max-w-[9rem] items-center gap-1 rounded-md border bg-white px-1.5 text-[11px] leading-none whitespace-nowrap dark:bg-black\",\n              \"transition-[border-color,color] duration-150 ease-out motion-reduce:transition-none [&>svg]:size-3.5\",\n              selected\n                ? \"z-10 border-zinc-950 text-zinc-950 shadow-[0_0_0_3px_rgb(9_9_11/0.08)] dark:border-white dark:text-white dark:shadow-[0_0_0_3px_rgb(255_255_255/0.16)]\"\n                : muted\n                  ? \"border-zinc-950/[0.07] text-zinc-400 dark:border-white/[0.08] dark:text-zinc-600 [&>svg]:opacity-40\"\n                  : look === \"active\"\n                    ? \"border-sky-500/40 text-zinc-700 dark:text-zinc-300\"\n                    : \"border-zinc-950/12 text-zinc-600 dark:border-white/15 dark:text-zinc-400\",\n              edge.selectable && !selected && \"cursor-pointer hover:border-zinc-950/35 hover:text-zinc-950 dark:hover:border-white/40 dark:hover:text-white\",\n              edge.hidden && \"hidden\",\n            )}\n          >\n            {edge.labelStatus ? <GraphStatusIcon key={edge.labelStatus} status={edge.labelStatus} /> : null}\n            <span className=\"truncate\">{edge.label}</span>\n          </div>\n        </EdgeLabelRenderer>\n      ) : null}\n    </>\n  );\n}\n\nconst nodeTypes = { card: NodeCard };\nconst edgeTypes = { route: RouteEdge };\n\n// Mini map fills read CSS variables set on the canvas, so they follow the theme.\nconst minimapColor: Partial<Record<GraphStatus, string>> = {\n  running: \"#0ea5e9\",\n  \"waiting-for-approval\": \"#f59e0b\",\n  failed: \"#ef4444\",\n};\n\nconst NO_PORTS: Ports = [0, 0, 0, 0];\n/** Stagger between ranks for the entrance, capped so a deep graph still settles quickly. */\nconst RANK_DELAY = 28;\nconst MAX_DELAY = 280;\n\n/**\n * The shared React Flow canvas: laid-out cards with kind icons and explicit ports, smooth\n * edges with small arrowheads, a pulse into running work, zoom, pan, fit-to-view, and an\n * optional mini map. Nodes are not draggable; positions come from the layout. Keyboard users\n * select through the list each graph component renders alongside it.\n */\nexport function GraphCanvas(props: GraphCanvasProps) {\n  return (\n    <ReactFlowProvider>\n      <Canvas {...props} />\n    </ReactFlowProvider>\n  );\n}\n\nfunction Canvas({\n  nodes,\n  edges,\n  layout,\n  selectedNodeId = null,\n  selectedEdgeId = null,\n  onNodeClick,\n  onEdgeClick,\n  onPaneClick,\n  height = 420,\n  label,\n  minimap,\n  animateEdges = true,\n  maxAnimatedEdges = 12,\n  grid = true,\n  className,\n  toolbar,\n}: GraphCanvasProps) {\n  const reduced = usePrefersReducedMotion();\n  // Only the viewport eases (fit, zoom, bringing a selection into view); selection itself is instant.\n  const duration = reduced ? 0 : 240;\n  const showMinimap = minimap ?? nodes.length > 24;\n  const animate = !reduced && animateEdges;\n  // Keep fitted graphs clear of the zoom controls (bottom left) and the mini map (top right).\n  const padding = useMemo(() => ({ top: showMinimap ? \"116px\" : \"28px\", right: \"28px\", bottom: \"64px\", left: \"28px\" }) as const, [showMinimap]);\n  const flow = useReactFlow<FlowNode, FlowEdge>();\n  const store = useStoreApi<FlowNode, FlowEdge>();\n  const { nodeWidth: width, nodeHeight: nodeHeight, direction } = layout;\n  const clickable = Boolean(onNodeClick);\n  const markerId = `graph-arrow-${useId().replace(/[^\\w-]/g, \"\")}`;\n  // The entrance plays only while the canvas first appears. React Flow remounts an edge when its\n  // stacking changes (a highlighted edge moves above the rest), and that must not replay it.\n  const [entering, setEntering] = useState(true);\n  useEffect(() => {\n    const timer = setTimeout(() => setEntering(false), MAX_DELAY + 400);\n    return () => clearTimeout(timer);\n  }, []);\n\n  // Rank of each node (for the entrance stagger) and the rank lines, where edge waypoints are kept.\n  const ranking = useMemo(() => {\n    const vertical = layout.direction === \"TB\";\n    const center = (point: Point) => (vertical ? point.y + layout.nodeHeight / 2 : point.x + layout.nodeWidth / 2);\n    const lines = [...new Set(Object.values(layout.positions).map((point) => Math.round(center(point) * 10)))].sort((a, b) => a - b);\n    const index = new Map(lines.map((line, i) => [line, i]));\n    const rankOf = new Map(Object.entries(layout.positions).map(([id, point]) => [id, index.get(Math.round(center(point) * 10)) ?? 0]));\n    return { lines: new Set(lines), rankOf };\n  }, [layout]);\n  const delayOf = (id: string) => Math.min(MAX_DELAY, (ranking.rankOf.get(id) ?? 0) * RANK_DELAY);\n\n  // Edge paths depend only on the layout, so they are computed once per layout and reused.\n  const geometryCache = useMemo(() => new Map<string, EdgeGeometry>(), [layout, ranking]);\n\n  // Each edge's route and look, then which ports it lights up.\n  const routed = useMemo(() => {\n    const result: { edge: GraphCanvasEdge; route: GraphLayout[\"edges\"][string]; look: EdgeLook; selected: boolean; pulse: boolean; geometry: EdgeGeometry }[] = [];\n    const size = { width: layout.nodeWidth, height: layout.nodeHeight };\n    let pulses = 0;\n    for (const edge of edges) {\n      const route = layout.edges[edge.id];\n      const from = layout.positions[edge.source];\n      const to = layout.positions[edge.target];\n      if (!route || !from || !to) continue;\n      const selected = edge.id === selectedEdgeId;\n      const look: EdgeLook = selected ? \"selected\" : edge.tone === \"muted\" ? \"muted\" : edge.animated ? \"active\" : (edge.tone ?? \"default\");\n      const pulse = animate && Boolean(edge.animated) && !edge.hidden && look !== \"muted\" && pulses < maxAnimatedEdges;\n      if (pulse) pulses += 1;\n      const key = `${edge.id}|${edge.label ? 1 : 0}`;\n      let geometry = geometryCache.get(key);\n      if (!geometry) {\n        geometry = edgeRoute(route, from, to, size, layout.direction, ranking.lines, Boolean(edge.label));\n        geometryCache.set(key, geometry);\n      }\n      result.push({ edge, route, look, selected, pulse, geometry });\n    }\n    return result;\n  }, [edges, layout, ranking, geometryCache, selectedEdgeId, animate, maxAnimatedEdges]);\n\n  // Which ports each node has. Structure only: selection never changes it, so selecting a node\n  // re-renders just that node and the edges whose look changes, never its neighbors.\n  const ports = useMemo(() => {\n    const map = new Map<string, Ports>();\n    const mark = (id: string, slot: number) => {\n      const current = map.get(id) ?? ([0, 0, 0, 0] as Ports);\n      current[slot] = 1;\n      map.set(id, current);\n    };\n    for (const { edge, geometry } of routed) {\n      if (edge.hidden) continue;\n      mark(edge.source, geometry.back ? 3 : 1);\n      mark(edge.target, geometry.back ? 2 : 0);\n    }\n    return map;\n  }, [routed]);\n\n  // Reuse node and edge objects whose content did not change, so React Flow re-renders only what changed.\n  const nodeCache = useRef(new Map<string, { signature: string; value: FlowNode }>());\n  const flowNodes = useMemo(() => {\n    const next = new Map<string, { signature: string; value: FlowNode }>();\n    const result = nodes.map((node) => {\n      const position = layout.positions[node.id] ?? { x: 0, y: 0 };\n      const selected = node.id === selectedNodeId;\n      // Ports show on hover; a selected node shows all of them.\n      let nodePorts = ports.get(node.id) ?? NO_PORTS;\n      if (selected) nodePorts = nodePorts.map((state) => (state ? 4 : 0)) as Ports;\n      const delay = delayOf(node.id);\n      const signature = JSON.stringify([node, position, selected, clickable, direction, width, nodeHeight, nodePorts, delay]);\n      const cached = nodeCache.current.get(node.id);\n      const value: FlowNode =\n        cached && cached.signature === signature\n          ? cached.value\n          : {\n              id: node.id,\n              type: \"card\",\n              position,\n              width,\n              height: nodeHeight,\n              hidden: node.hidden,\n              zIndex: selected ? 1 : 0,\n              className: clickable ? \"cursor-pointer\" : undefined,\n              data: { node, selected, clickable, direction, width, height: nodeHeight, ports: nodePorts, delay },\n            };\n      next.set(node.id, { signature, value });\n      return value;\n    });\n    nodeCache.current = next;\n    return result;\n  }, [nodes, layout, selectedNodeId, direction, width, nodeHeight, clickable, ports]); // eslint-disable-line react-hooks/exhaustive-deps\n\n  const edgeCache = useRef(new Map<string, { signature: string; route: unknown; value: FlowEdge }>());\n  const flowEdges = useMemo(() => {\n    const next = new Map<string, { signature: string; route: unknown; value: FlowEdge }>();\n    const result: FlowEdge[] = [];\n    for (const { edge, route, look, selected, pulse, geometry } of routed) {\n      const delay = Math.min(MAX_DELAY, delayOf(edge.target)) + 60;\n      const signature = JSON.stringify([edge, look, selected, pulse, delay]);\n      const cached = edgeCache.current.get(edge.id);\n      const value: FlowEdge =\n        cached && cached.signature === signature && cached.route === route && cached.value.data?.geometry === geometry\n          ? cached.value\n          : {\n              id: edge.id,\n              source: edge.source,\n              target: edge.target,\n              sourceHandle: geometry.back ? \"back-out\" : undefined,\n              targetHandle: geometry.back ? \"back-in\" : undefined,\n              type: \"route\",\n              hidden: edge.hidden,\n              zIndex: selected ? 3 : look === \"highlight\" ? 2 : look === \"active\" ? 1 : 0,\n              data: { edge, geometry, label: route.label, selected, look, pulse, delay },\n            };\n      next.set(edge.id, { signature, route, value });\n      result.push(value);\n    }\n    edgeCache.current = next;\n    return result;\n  }, [routed]); // eslint-disable-line react-hooks/exhaustive-deps\n\n  // Refit when the visible structure changes (nodes added or removed, or a branch focused), not on status changes.\n  const hiddenKey = nodes.filter((node) => node.hidden).map((node) => node.id).join(\",\");\n  const fitKey = `${layout.key}|${hiddenKey}`;\n  const lastFit = useRef(fitKey);\n  useEffect(() => {\n    if (lastFit.current === fitKey) return;\n    lastFit.current = fitKey;\n    const frame = requestAnimationFrame(() => void flow.fitView({ padding, maxZoom: 1, duration }));\n    return () => cancelAnimationFrame(frame);\n  }, [fitKey, flow, duration, padding]);\n\n  // Bring a selection made elsewhere (the list, a details link) into view, keeping the zoom.\n  const target = useMemo(() => {\n    if (selectedNodeId && layout.positions[selectedNodeId]) {\n      const position = layout.positions[selectedNodeId]!;\n      return { x: position.x + width / 2, y: position.y + nodeHeight / 2, halfWidth: width / 2, halfHeight: nodeHeight / 2 };\n    }\n    if (selectedEdgeId && layout.edges[selectedEdgeId]) {\n      const point = layout.edges[selectedEdgeId]!.label;\n      return { x: point.x, y: point.y, halfWidth: 60, halfHeight: 14 };\n    }\n    return null;\n  }, [selectedNodeId, selectedEdgeId, layout, width, nodeHeight]);\n  useEffect(() => {\n    if (!target) return;\n    const { width: viewWidth, height: viewHeight, transform } = store.getState();\n    if (!viewWidth || !viewHeight) return;\n    const [tx, ty, zoom] = transform;\n    const left = (target.x - target.halfWidth) * zoom + tx;\n    const right = (target.x + target.halfWidth) * zoom + tx;\n    const top = (target.y - target.halfHeight) * zoom + ty;\n    const bottom = (target.y + target.halfHeight) * zoom + ty;\n    const margin = 16;\n    if (left < margin || top < margin || right > viewWidth - margin || bottom > viewHeight - margin) {\n      void flow.setCenter(target.x, target.y, { zoom: Math.max(zoom, 0.6), duration });\n    }\n    // Only when the selection target changes; not on every viewport change.\n  }, [target]); // eslint-disable-line react-hooks/exhaustive-deps\n\n  const context = useMemo(() => ({ onEdgeClick, markerId }), [onEdgeClick, markerId]);\n\n  return (\n    <CanvasContext.Provider value={context}>\n      <div\n        role=\"group\"\n        aria-label={label}\n        data-entering={entering ? \"\" : undefined}\n        style={{ height }}\n        className={cx(\n          \"group/canvas relative w-full bg-zinc-950/[0.015] text-zinc-950 dark:bg-black dark:text-white\",\n          \"[--xy-background-pattern-color:rgb(9_9_11/0.12)] dark:[--xy-background-pattern-color:rgb(255_255_255/0.1)]\",\n          \"[--xy-minimap-background-color:#fff] dark:[--xy-minimap-background-color:#000]\",\n          \"[--xy-minimap-mask-background-color:rgb(9_9_11/0.035)] dark:[--xy-minimap-mask-background-color:rgb(255_255_255/0.045)]\",\n          \"[--xy-minimap-mask-stroke-color:rgb(9_9_11/0.35)] dark:[--xy-minimap-mask-stroke-color:rgb(255_255_255/0.45)]\",\n          \"[--graph-minimap-node:#d4d4d8] dark:[--graph-minimap-node:#3f3f46] [--graph-minimap-selected:#09090b] dark:[--graph-minimap-selected:#ffffff]\",\n          // Port rings: idle, on the selected path, feeding running work, selected; filled with the card color.\n          \"[--graph-port:#a1a1aa] dark:[--graph-port:#71717a] [--graph-port-on:#52525b] dark:[--graph-port-on:#c8c8cc]\",\n          \"[--graph-port-active:#0284c7] dark:[--graph-port-active:#38bdf8] [--graph-port-selected:#09090b] dark:[--graph-port-selected:#ffffff]\",\n          \"[--graph-port-fill:#ffffff] dark:[--graph-port-fill:#000000]\",\n          className,\n        )}\n      >\n        <ArrowMarkers id={markerId} />\n        <ReactFlow<FlowNode, FlowEdge>\n          nodes={flowNodes}\n          edges={flowEdges}\n          nodeTypes={nodeTypes}\n          edgeTypes={edgeTypes}\n          onNodeClick={onNodeClick ? (_, node) => onNodeClick(node.id) : undefined}\n          onEdgeClick={onEdgeClick ? (_, edge) => (edge.data?.edge.selectable ? onEdgeClick(edge.id) : undefined) : undefined}\n          onPaneClick={onPaneClick}\n          nodesDraggable={false}\n          nodesConnectable={false}\n          nodesFocusable={false}\n          edgesFocusable={false}\n          elementsSelectable={false}\n          disableKeyboardA11y\n          fitView\n          fitViewOptions={{ padding, maxZoom: 1 }}\n          minZoom={0.05}\n          maxZoom={2}\n          // The attribution link is drawn below in the canvas style instead of React Flow's default badge.\n          proOptions={{ hideAttribution: true }}\n        >\n          {grid ? <Background variant={BackgroundVariant.Dots} gap={16} size={1} /> : null}\n          {toolbar ? (\n            <Panel position=\"top-left\" className=\"flex flex-wrap items-center gap-1.5\">\n              {toolbar}\n            </Panel>\n          ) : null}\n          <Panel position=\"bottom-left\">\n            <CanvasControls duration={duration} padding={padding} />\n          </Panel>\n          <Panel position=\"bottom-right\" className=\"m-2!\">\n            <a\n              href=\"https://reactflow.dev\"\n              target=\"_blank\"\n              rel=\"noreferrer\"\n              className=\"rounded px-1 text-[10px] text-zinc-950/35 hover:text-zinc-950/60 focus-visible:outline-2 focus-visible:outline-sky-500 dark:text-white/30 dark:hover:text-white/60\"\n            >\n              React Flow\n            </a>\n          </Panel>\n          {showMinimap ? (\n            <MiniMap<FlowNode>\n              position=\"top-right\"\n              pannable\n              zoomable\n              ariaLabel=\"Mini map\"\n              nodeColor={(node) => (node.data.selected ? \"var(--graph-minimap-selected)\" : (node.data.node.status && minimapColor[node.data.node.status]) || \"var(--graph-minimap-node)\")}\n              nodeStrokeWidth={0}\n              nodeBorderRadius={4}\n              style={{ width: 140, height: 92 }}\n              className=\"overflow-hidden rounded-lg border border-zinc-950/10 dark:border-white/15\"\n            />\n          ) : null}\n        </ReactFlow>\n      </div>\n    </CanvasContext.Provider>\n  );\n}\n\nconst zoomSelector = (state: ReactFlowState) => Math.round(state.transform[2] * 100);\n\n/** Zoom out, the zoom level (click to reset to 100%), zoom in, and fit to view, in one hairline group. */\nfunction CanvasControls({ duration, padding }: { duration: number; padding: FitViewOptions[\"padding\"] }) {\n  const flow = useReactFlow();\n  const zoom = useStore(zoomSelector);\n  return (\n    <div\n      role=\"group\"\n      aria-label=\"Canvas controls\"\n      className=\"flex items-center gap-0.5 rounded-lg border border-zinc-950/10 bg-white p-0.5 shadow-[0_1px_2px_rgb(0_0_0/0.05)] dark:border-white/15 dark:bg-black dark:shadow-none\"\n    >\n      <CanvasButton label=\"Zoom out\" onClick={() => void flow.zoomOut({ duration })}>\n        <path d=\"M3.5 8h9\" />\n      </CanvasButton>\n      <button\n        type=\"button\"\n        aria-label={`Zoom ${zoom}%. Reset to 100%`}\n        title=\"Reset to 100%\"\n        onClick={() => void flow.zoomTo(1, { duration })}\n        className={cx(\n          \"h-7 min-w-11 rounded-md px-1 font-mono text-[11px] text-zinc-600 tabular-nums dark:text-zinc-400\",\n          \"transition-colors duration-150 ease-out hover:bg-zinc-950/[0.05] hover:text-zinc-950 motion-reduce:transition-none dark:hover:bg-white/[0.08] dark:hover:text-white\",\n          \"focus-visible:outline-2 focus-visible:outline-sky-500\",\n        )}\n      >\n        {zoom}%\n      </button>\n      <CanvasButton label=\"Zoom in\" onClick={() => void flow.zoomIn({ duration })}>\n        <path d=\"M8 3.5v9M3.5 8h9\" />\n      </CanvasButton>\n      <span aria-hidden=\"true\" className=\"mx-0.5 h-4 w-px bg-zinc-950/10 dark:bg-white/15\" />\n      <CanvasButton label=\"Fit to view\" onClick={() => void flow.fitView({ padding, maxZoom: 1, duration })}>\n        <path d=\"M2.75 6V3.75a1 1 0 0 1 1-1H6M10 2.75h2.25a1 1 0 0 1 1 1V6M13.25 10v2.25a1 1 0 0 1-1 1H10M6 13.25H3.75a1 1 0 0 1-1-1V10\" />\n      </CanvasButton>\n    </div>\n  );\n}\n\nfunction CanvasButton({ label, onClick, children }: { label: string; onClick: () => void; children: ReactNode }) {\n  return (\n    <button\n      type=\"button\"\n      aria-label={label}\n      title={label}\n      onClick={onClick}\n      className={cx(\n        \"grid size-7 place-items-center rounded-md text-zinc-600 dark:text-zinc-400\",\n        \"transition-colors duration-150 ease-out hover:bg-zinc-950/[0.05] hover:text-zinc-950 motion-reduce:transition-none dark:hover:bg-white/[0.08] dark:hover:text-white\",\n        \"focus-visible:outline-2 focus-visible:outline-sky-500\",\n      )}\n    >\n      <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-4\">\n        {children}\n      </svg>\n    </button>\n  );\n}\n"
    }
  ],
  "example": {
    "content": "\"use client\";\n\nimport { useMemo, useState } from \"react\";\nimport { GraphCanvas, type GraphCanvasEdge, type GraphCanvasNode, useGraphLayout } from \"@/components/shaken/graph/graph-canvas\";\n\n// A report pipeline: a plan fans out to four parallel tool calls, results fan back in,\n// and an approval gates the report. `icon` picks the kind icon in each node's tile.\nconst nodes: GraphCanvasNode[] = [\n  { id: \"plan\", title: \"Plan the report\", subtitle: \"Plan\", icon: \"plan\", status: \"succeeded\" },\n  { id: \"web\", title: \"search_web\", subtitle: \"Tool call\", icon: \"tool\", mono: true, status: \"succeeded\" },\n  { id: \"papers\", title: \"search_papers\", subtitle: \"Tool call\", icon: \"tool\", mono: true, status: \"succeeded\" },\n  { id: \"warehouse\", title: \"query_warehouse\", subtitle: \"Tool call\", icon: \"tool\", mono: true, status: \"failed\" },\n  { id: \"cache\", title: \"read_metrics_cache\", subtitle: \"Tool call\", icon: \"tool\", mono: true, status: \"succeeded\" },\n  { id: \"summarize\", title: \"Summarize sources\", subtitle: \"Model call\", icon: \"model\", status: \"succeeded\" },\n  { id: \"analyst\", title: \"Analyst\", subtitle: \"Agent\", icon: \"agent\", status: \"succeeded\" },\n  { id: \"writer\", title: \"Writer\", subtitle: \"Agent\", icon: \"agent\", status: \"running\" },\n  { id: \"charts\", title: \"render_charts\", subtitle: \"Tool call\", icon: \"tool\", mono: true, status: \"running\" },\n  { id: \"factcheck\", title: \"check_citations\", subtitle: \"Tool call\", icon: \"tool\", mono: true, status: \"queued\" },\n  { id: \"approve\", title: \"Publish the report?\", subtitle: \"Approval\", icon: \"approval\", status: \"queued\" },\n  { id: \"publish\", title: \"Quarterly report\", subtitle: \"Result\", icon: \"result\", status: \"queued\" },\n  { id: \"notify\", title: \"notify_channel\", subtitle: \"Tool call\", icon: \"tool\", mono: true, status: \"queued\" },\n];\n\n// Solid: runs after. Dashed: a result or an approval gate.\nconst links: [source: string, target: string, dashed?: boolean][] = [\n  [\"plan\", \"web\"],\n  [\"plan\", \"papers\"],\n  [\"plan\", \"warehouse\"],\n  [\"plan\", \"cache\"],\n  [\"web\", \"summarize\", true],\n  [\"papers\", \"summarize\", true],\n  [\"cache\", \"analyst\", true],\n  [\"summarize\", \"writer\", true],\n  [\"analyst\", \"writer\", true],\n  [\"analyst\", \"charts\"],\n  [\"writer\", \"factcheck\"],\n  [\"writer\", \"approve\", true],\n  [\"charts\", \"approve\", true],\n  [\"factcheck\", \"publish\", true],\n  [\"approve\", \"publish\"],\n  [\"approve\", \"notify\"],\n];\n\nexport default function GraphCanvasExample() {\n  const [selected, setSelected] = useState<string | null>(\"writer\");\n  const status = useMemo(() => new Map(nodes.map((node) => [node.id, node.status])), []);\n  const edges = useMemo<GraphCanvasEdge[]>(\n    () =>\n      links.map(([source, target, dashed]) => ({\n        id: `${source}-${target}`,\n        source,\n        target,\n        type: dashed ? \"result\" : \"sequence\",\n        dashed,\n        // Edges into running work carry a light pulse (static under reduced motion).\n        animated: status.get(target) === \"running\",\n        tone: selected && (source === selected || target === selected) ? \"highlight\" : \"default\",\n      })),\n    [status, selected],\n  );\n  // The layout is computed once per structure; status and selection changes reuse it.\n  const layout = useGraphLayout(nodes, edges);\n  return (\n    <GraphCanvas\n      label=\"Report pipeline\"\n      nodes={nodes}\n      edges={edges}\n      layout={layout}\n      selectedNodeId={selected}\n      onNodeClick={setSelected}\n      onPaneClick={() => setSelected(null)}\n      height={480}\n      className=\"overflow-hidden rounded-xl border border-zinc-950/10 dark:border-white/10\"\n    />\n  );\n}\n"
  }
}
