{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "run-snapshot-timeline",
  "title": "Run Snapshot Timeline",
  "description": "Step back through an AI agent's run: drag along the line to see each saved state, with its files, tests and cost, then restore to any point.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/run-snapshot-timeline/run-snapshot-timeline.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type SnapshotKind = \"step\" | \"error\" | \"done\";\n\nexport interface SnapshotFile {\n  path: string;\n  added: number;\n  removed: number;\n}\n\nexport interface RunSnapshot {\n  id: string;\n  label: string;\n  /** Time into the run, e.g. \"05:40\". */\n  at: string;\n  kind?: SnapshotKind;\n  note: string;\n  /** Share of tests passing, from 0 to 100. */\n  testsPassed: number;\n  /** Share of the run's budget spent, from 0 to 100. */\n  budgetUsed: number;\n  files: SnapshotFile[];\n  stats: { label: string; value: string }[];\n}\n\nexport interface RunSnapshotTimelineProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  /** Oldest first. */\n  snapshots?: RunSnapshot[];\n  /** Fires with the snapshot the run goes back to. */\n  onRestore?: (snapshot: RunSnapshot) => void;\n  onUndoRestore?: () => void;\n}\n\nconst FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst ACCENT = \"#5E6AD2\";\nconst BLUE = \"#4EA7FC\";\nconst GREEN = \"#27A644\";\nconst RED = \"#EB5757\";\nconst RING = \"outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2]\";\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    border: \"rgba(255,255,255,0.08)\",\n    grid: \"rgba(255,255,255,0.05)\",\n    fill: 0.2,\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n    btn: \"#E5E5E6\",\n    btnText: \"#08090A\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(8,9,10,0.08)\",\n    grid: \"rgba(8,9,10,0.06)\",\n    fill: 0.14,\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.05)\",\n    btn: \"#08090A\",\n    btnText: \"#F7F8F8\",\n  },\n};\n\nconst DEFAULT_SNAPSHOTS: RunSnapshot[] = [\n  {\n    id: \"plan\",\n    label: \"Plan approved\",\n    at: \"00:00\",\n    note: \"Read the old billing code and agreed four steps with you.\",\n    testsPassed: 100,\n    budgetUsed: 4,\n    files: [{ path: \"docs/billing-plan.md\", added: 42, removed: 0 }],\n    stats: [\n      { label: \"Tests passing\", value: \"152 of 152\" },\n      { label: \"Tokens used\", value: \"6k\" },\n      { label: \"Cost so far\", value: \"$0.04\" },\n    ],\n  },\n  {\n    id: \"schema\",\n    label: \"Schema updated\",\n    at: \"02:14\",\n    note: \"Added the new invoice tables, and kept the old ones for now.\",\n    testsPassed: 100,\n    budgetUsed: 13,\n    files: [\n      { path: \"db/schema.sql\", added: 86, removed: 4 },\n      { path: \"db/migrate-012.sql\", added: 31, removed: 0 },\n    ],\n    stats: [\n      { label: \"Tests passing\", value: \"152 of 152\" },\n      { label: \"Tokens used\", value: \"19k\" },\n      { label: \"Cost so far\", value: \"$0.13\" },\n    ],\n  },\n  {\n    id: \"invoices\",\n    label: \"Invoices moved\",\n    at: \"05:40\",\n    note: \"Pointed invoice creation at the new API and copied the old records.\",\n    testsPassed: 98,\n    budgetUsed: 29,\n    files: [\n      { path: \"src/billing/invoices.ts\", added: 118, removed: 74 },\n      { path: \"src/billing/client.ts\", added: 52, removed: 9 },\n      { path: \"src/billing/types.ts\", added: 24, removed: 12 },\n    ],\n    stats: [\n      { label: \"Tests passing\", value: \"149 of 152\" },\n      { label: \"Tokens used\", value: \"41k\" },\n      { label: \"Cost so far\", value: \"$0.29\" },\n    ],\n  },\n  {\n    id: \"failing\",\n    label: \"Tests failing\",\n    at: \"08:02\",\n    kind: \"error\",\n    note: \"Refund totals came out wrong after the tax change.\",\n    testsPassed: 86,\n    budgetUsed: 42,\n    files: [\n      { path: \"src/billing/refunds.ts\", added: 37, removed: 21 },\n      { path: \"src/billing/tax.ts\", added: 16, removed: 30 },\n    ],\n    stats: [\n      { label: \"Tests passing\", value: \"131 of 152\" },\n      { label: \"Tokens used\", value: \"58k\" },\n      { label: \"Cost so far\", value: \"$0.42\" },\n    ],\n  },\n  {\n    id: \"fix\",\n    label: \"Fix applied\",\n    at: \"10:31\",\n    note: \"Rounded tax per line instead of per invoice, as the old code did.\",\n    testsPassed: 99,\n    budgetUsed: 53,\n    files: [\n      { path: \"src/billing/tax.ts\", added: 22, removed: 8 },\n      { path: \"src/billing/tax.test.ts\", added: 40, removed: 0 },\n    ],\n    stats: [\n      { label: \"Tests passing\", value: \"151 of 152\" },\n      { label: \"Tokens used\", value: \"72k\" },\n      { label: \"Cost so far\", value: \"$0.53\" },\n    ],\n  },\n  {\n    id: \"ready\",\n    label: \"Ready for review\",\n    at: \"12:05\",\n    kind: \"done\",\n    note: \"All tests pass and the pull request is open.\",\n    testsPassed: 100,\n    budgetUsed: 60,\n    files: [\n      { path: \"src/billing/webhooks.ts\", added: 19, removed: 6 },\n      { path: \"CHANGELOG.md\", added: 8, removed: 0 },\n    ],\n    stats: [\n      { label: \"Tests passing\", value: \"152 of 152\" },\n      { label: \"Tokens used\", value: \"81k\" },\n      { label: \"Cost so far\", value: \"$0.60\" },\n    ],\n  },\n];\n\nconst SERIES = [\n  { key: \"testsPassed\", label: \"Tests passing\", color: ACCENT },\n  { key: \"budgetUsed\", label: \"Budget used\", color: BLUE },\n] as const;\n\ntype SeriesKey = (typeof SERIES)[number][\"key\"];\n\nconst H = 212;\nconst TOP = 10;\nconst BASE = H - 26;\nconst LEFT = 40;\nconst RIGHT = 10;\nconst TICKS = [0, 25, 50, 75, 100];\n\nconst round = (n: number) => Math.round(n * 100) / 100;\n\nfunction curve(points: [number, number][]) {\n  return points\n    .map(([x, y], i) => {\n      if (!i) return `M${x} ${y}`;\n      const [px, py] = points[i - 1];\n      const mid = round((px + x) / 2);\n      return `C${mid} ${py} ${mid} ${y} ${x} ${y}`;\n    })\n    .join(\" \");\n}\n\n/** Draws at the real pixel width, so nothing stretches on a phone. */\nfunction useWidth<T extends HTMLElement>(fallback: number) {\n  const ref = React.useRef<T>(null);\n  const [width, setWidth] = React.useState(fallback);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) => {\n      setWidth(Math.round(entry.contentRect.width));\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n  return [ref, width] as const;\n}\n\n/** Drawn as SVG, because a tiny border-radius circle turns lumpy on 1x screens. */\nfunction Dot({ size, color }: { size: number; color: string }) {\n  const r = size / 2;\n  return (\n    <svg\n      aria-hidden=\"true\"\n      width={size}\n      height={size}\n      viewBox={`0 0 ${size} ${size}`}\n      className=\"block shrink-0\"\n    >\n      <circle cx={r} cy={r} r={r} style={{ fill: color }} />\n    </svg>\n  );\n}\n\nexport function RunSnapshotTimeline({\n  theme = \"dark\",\n  title = \"Move invoices to the new billing API\",\n  subtitle = \"Run 482, finished in 12 minutes\",\n  snapshots = DEFAULT_SNAPSHOTS,\n  onRestore,\n  onUndoRestore,\n  className,\n  style,\n  ...rest\n}: RunSnapshotTimelineProps) {\n  const c = PALETTES[theme];\n  const last = snapshots.length - 1;\n  const [selected, setSelected] = React.useState(last);\n  const [restored, setRestored] = React.useState<number | null>(null);\n  const [hidden, setHidden] = React.useState<SeriesKey[]>([]);\n  const [chartRef, width] = useWidth<HTMLDivElement>(780);\n  const uid = React.useId();\n  const head = restored ?? last;\n  const current = snapshots[selected];\n  const figure = current?.stats[current.stats.length - 1];\n\n  const x = (i: number) =>\n    round(LEFT + (last > 0 ? i / last : 0) * (width - LEFT - RIGHT));\n  const y = (v: number) => round(BASE - (v / 100) * (BASE - TOP));\n  const headX = x(head);\n  const guide = x(selected);\n\n  const colorOf = (s: RunSnapshot) =>\n    s.kind === \"error\" ? RED : s.kind === \"done\" ? GREEN : ACCENT;\n\n  const nearest = (clientX: number) => {\n    const box = chartRef.current?.getBoundingClientRect();\n    if (!box?.width) return selected;\n    const ratio = (clientX - box.left - LEFT) / (box.width - LEFT - RIGHT);\n    return Math.max(0, Math.min(last, Math.round(ratio * last)));\n  };\n\n  const undo = () => {\n    setRestored(null);\n    setSelected(last);\n    onUndoRestore?.();\n  };\n\n  const restore = (i: number) => {\n    if (i === last) {\n      undo();\n      return;\n    }\n    setRestored(i);\n    onRestore?.(snapshots[i]);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    const next =\n      e.key === \"ArrowRight\" || e.key === \"ArrowUp\"\n        ? selected + 1\n        : e.key === \"ArrowLeft\" || e.key === \"ArrowDown\"\n          ? selected - 1\n          : e.key === \"Home\"\n            ? 0\n            : e.key === \"End\"\n              ? last\n              : null;\n    if (next === null) return;\n    e.preventDefault();\n    setSelected(Math.max(0, Math.min(last, next)));\n  };\n\n  return (\n    <section\n      aria-label={title}\n      className={`@container w-full rounded-2xl border ${className ?? \"\"}`}\n      style={{\n        background: c.card,\n        borderColor: c.border,\n        color: c.text,\n        fontFamily: FONT,\n        ...style,\n      }}\n      {...rest}\n    >\n      <div className=\"p-5 pb-4 @2xl:p-6 @2xl:pb-4\">\n        <div className=\"flex items-start justify-between gap-4\">\n          <header className=\"min-w-0\">\n            <h3 className=\"m-0 text-[20px] leading-[26px] font-[590] tracking-[-0.012em]\">\n              {title}\n            </h3>\n            <p\n              className=\"m-0 mt-1 text-[13px] leading-[20px] tracking-[-0.011em]\"\n              style={{ color: c.sub }}\n            >\n              {subtitle}\n            </p>\n          </header>\n          {figure && (\n            <div className=\"shrink-0 text-right\">\n              <p className=\"m-0 text-[32px] leading-[32px] font-[510] tracking-[-0.022em] tabular-nums\">\n                {figure.value}\n              </p>\n              <p\n                className=\"m-0 mt-1.5 text-[13px] leading-[20px]\"\n                style={{ color: c.sub }}\n              >\n                {figure.label}\n              </p>\n            </div>\n          )}\n        </div>\n\n        <div className=\"mt-4 flex flex-wrap gap-1.5\">\n          {SERIES.map((series) => {\n            const off = hidden.includes(series.key);\n            return (\n              <button\n                key={series.key}\n                type=\"button\"\n                aria-pressed={!off}\n                onClick={() =>\n                  setHidden((h) =>\n                    off\n                      ? h.filter((k) => k !== series.key)\n                      : [...h, series.key],\n                  )\n                }\n                className={`flex h-7 cursor-pointer items-center gap-2 rounded-full border-0 px-3 text-[13px] leading-[20px] font-[510] tracking-[-0.011em] motion-safe:transition-opacity motion-safe:duration-150 ${RING}`}\n                style={{\n                  background: c.chip,\n                  color: c.text,\n                  opacity: off ? 0.45 : 1,\n                }}\n              >\n                <Dot size={8} color={series.color} />\n                {series.label}\n                <span className=\"tabular-nums\" style={{ color: c.sub }}>\n                  {current?.[series.key]}%\n                </span>\n              </button>\n            );\n          })}\n        </div>\n\n        <div\n          ref={chartRef}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label=\"Snapshot\"\n          aria-valuemin={1}\n          aria-valuemax={snapshots.length}\n          aria-valuenow={selected + 1}\n          aria-valuetext={`${current?.label}, ${current?.at}`}\n          onKeyDown={onKeyDown}\n          onPointerDown={(e) => {\n            e.currentTarget.setPointerCapture(e.pointerId);\n            setSelected(nearest(e.clientX));\n          }}\n          onPointerMove={(e) => {\n            if (e.currentTarget.hasPointerCapture(e.pointerId)) {\n              setSelected(nearest(e.clientX));\n            }\n          }}\n          className={`mt-3 w-full cursor-pointer touch-pan-y rounded-lg select-none ${RING}`}\n          style={{ height: H }}\n        >\n          {/* Everything here is SVG with explicit coordinates, so it can't fall out of line. */}\n          <svg\n            aria-hidden=\"true\"\n            width={width}\n            height={H}\n            className=\"block\"\n            fontFamily=\"inherit\"\n          >\n            <defs>\n              <clipPath id={`${uid}-live`}>\n                <rect x=\"0\" y=\"0\" width={headX} height={H} />\n              </clipPath>\n              <clipPath id={`${uid}-rest`}>\n                <rect\n                  x={headX}\n                  y=\"0\"\n                  width={Math.max(0, width - headX)}\n                  height={H}\n                />\n              </clipPath>\n              {SERIES.map((series) => (\n                <linearGradient\n                  key={series.key}\n                  id={`${uid}-${series.key}`}\n                  x1=\"0\"\n                  x2=\"0\"\n                  y1=\"0\"\n                  y2=\"1\"\n                >\n                  <stop stopColor={series.color} stopOpacity={c.fill} />\n                  <stop offset=\"1\" stopColor={series.color} stopOpacity=\"0\" />\n                </linearGradient>\n              ))}\n            </defs>\n\n            {TICKS.map((t) => (\n              <g key={t}>\n                <line\n                  x1={LEFT}\n                  x2={width - RIGHT}\n                  y1={y(t)}\n                  y2={y(t)}\n                  stroke={c.grid}\n                />\n                <text\n                  x={LEFT - 10}\n                  y={y(t) + 4}\n                  textAnchor=\"end\"\n                  fontSize=\"11\"\n                  fill={c.faint}\n                >\n                  {t}%\n                </text>\n              </g>\n            ))}\n\n            {SERIES.map((series) => {\n              if (hidden.includes(series.key)) return null;\n              const points = snapshots.map(\n                (s, i) => [x(i), y(s[series.key])] as [number, number],\n              );\n              const line = curve(points);\n              const area = `${line} L${x(last)} ${BASE} L${x(0)} ${BASE} Z`;\n              return (\n                <g key={series.key}>\n                  <g clipPath={`url(#${uid}-live)`}>\n                    <path d={area} fill={`url(#${uid}-${series.key})`} />\n                    <path\n                      d={line}\n                      fill=\"none\"\n                      stroke={series.color}\n                      strokeWidth=\"1.75\"\n                    />\n                  </g>\n                  <path\n                    d={line}\n                    fill=\"none\"\n                    stroke={c.faint}\n                    strokeWidth=\"1.5\"\n                    strokeDasharray=\"3 4\"\n                    clipPath={`url(#${uid}-rest)`}\n                  />\n                </g>\n              );\n            })}\n\n            <line\n              x1={guide}\n              x2={guide}\n              y1={TOP}\n              y2={BASE}\n              stroke={c.faint}\n              className=\"motion-safe:transition-[x1,x2] motion-safe:duration-150\"\n            />\n\n            {snapshots.map((s, i) => {\n              const on = i === selected;\n              const dropped = i > head;\n              return (\n                <g key={s.id}>\n                  {!hidden.includes(\"testsPassed\") && (\n                    <circle\n                      data-mark={i}\n                      cx={x(i)}\n                      cy={y(s.testsPassed)}\n                      r={on ? 5 : 3.5}\n                      fill={dropped ? c.card : colorOf(s)}\n                      stroke={dropped ? c.faint : c.card}\n                      strokeWidth={dropped ? 1.5 : 2}\n                    />\n                  )}\n                  {on && !hidden.includes(\"budgetUsed\") && (\n                    <circle\n                      cx={x(i)}\n                      cy={y(s.budgetUsed)}\n                      r=\"4.5\"\n                      fill={dropped ? c.card : BLUE}\n                      stroke={dropped ? c.faint : c.card}\n                      strokeWidth={dropped ? 1.5 : 2}\n                    />\n                  )}\n                  <text\n                    x={x(i)}\n                    y={H - 6}\n                    textAnchor={\n                      i === 0 ? \"start\" : i === last ? \"end\" : \"middle\"\n                    }\n                    fontSize=\"11\"\n                    fill={on ? c.text : c.faint}\n                    style={{ fontVariantNumeric: \"tabular-nums\" }}\n                  >\n                    {s.at}\n                  </text>\n                </g>\n              );\n            })}\n          </svg>\n        </div>\n      </div>\n\n      {/* Every snapshot shares one cell, so scrubbing never changes the card's height. */}\n      <div\n        className=\"grid border-t p-5 @2xl:px-6\"\n        style={{ borderColor: c.border }}\n      >\n        {snapshots.map((s, i) => {\n          const on = i === selected;\n          const isHead = i === head;\n          const dropped = i > head;\n          return (\n            <article\n              key={s.id}\n              aria-hidden={!on}\n              inert={!on}\n              className={`grid min-w-0 gap-x-8 gap-y-4 [grid-area:1/1] motion-safe:transition-opacity motion-safe:duration-150 @2xl:grid-cols-[minmax(0,1.25fr)_minmax(0,1fr)] ${on ? \"visible opacity-100\" : \"invisible opacity-0\"}`}\n            >\n              <div className=\"min-w-0\">\n                <p\n                  className=\"m-0 text-[12px] leading-[16px] tracking-[-0.005em] tabular-nums\"\n                  style={{ color: s.kind === \"error\" ? RED : c.faint }}\n                >\n                  Snapshot {i + 1} of {snapshots.length}\n                  {dropped ? \", set aside\" : \"\"}\n                </p>\n                <h4 className=\"m-0 mt-1 text-[16px] leading-[22px] font-[590] tracking-[-0.014em]\">\n                  {s.label}\n                </h4>\n                <p\n                  className=\"m-0 mt-1 text-[13.5px] leading-[20px] tracking-[-0.011em]\"\n                  style={{ color: c.soft }}\n                >\n                  {s.note}\n                </p>\n                <ul\n                  className=\"m-0 mt-2 list-none p-0\"\n                  style={{ minHeight: 84 }}\n                >\n                  {s.files.map((f) => (\n                    <li\n                      key={f.path}\n                      className=\"flex items-center justify-between gap-3 py-[3px] text-[12.5px] leading-[22px] tracking-[-0.008em] tabular-nums\"\n                    >\n                      <span className=\"truncate\" style={{ color: c.sub }}>\n                        {f.path}\n                      </span>\n                      <span className=\"flex shrink-0 gap-2\">\n                        <span style={{ color: GREEN }}>+{f.added}</span>\n                        <span style={{ color: RED }}>−{f.removed}</span>\n                      </span>\n                    </li>\n                  ))}\n                </ul>\n              </div>\n              <div className=\"flex min-w-0 flex-col\">\n                <dl className=\"m-0\">\n                  {s.stats.map((st) => (\n                    <div\n                      key={st.label}\n                      className=\"flex items-center justify-between gap-4 border-b py-2 text-[13px] leading-[20px] tracking-[-0.011em]\"\n                      style={{ borderColor: c.border }}\n                    >\n                      <dt style={{ color: c.sub }}>{st.label}</dt>\n                      <dd className=\"m-0 font-[510] tabular-nums\">\n                        {st.value}\n                      </dd>\n                    </div>\n                  ))}\n                </dl>\n                <button\n                  type=\"button\"\n                  data-restore=\"\"\n                  disabled={isHead}\n                  onClick={() => restore(i)}\n                  className={`mt-4 h-9 w-full cursor-pointer rounded-full border-0 text-[13px] leading-[20px] font-[510] tracking-[-0.011em] disabled:cursor-default @2xl:mt-auto ${RING}`}\n                  style={{\n                    background: isHead ? c.chip : c.btn,\n                    color: isHead ? c.faint : c.btnText,\n                  }}\n                >\n                  {isHead\n                    ? \"Run is here\"\n                    : dropped\n                      ? \"Go forward to here\"\n                      : \"Restore to here\"}\n                </button>\n              </div>\n            </article>\n          );\n        })}\n      </div>\n\n      <footer\n        className=\"flex h-[52px] items-center justify-between gap-4 border-t px-5 @2xl:px-6\"\n        style={{ borderColor: c.border }}\n      >\n        <p\n          aria-live=\"polite\"\n          className=\"m-0 truncate text-[13px] leading-[20px] tracking-[-0.011em]\"\n          style={{ color: c.sub }}\n        >\n          {restored === null\n            ? \"Drag along the line to rewind.\"\n            : `Back at ${snapshots[restored].label}. Later work is set aside.`}\n        </p>\n        <button\n          type=\"button\"\n          data-undo=\"\"\n          tabIndex={restored === null ? -1 : 0}\n          onClick={undo}\n          className={`h-8 shrink-0 cursor-pointer rounded-full border-0 px-3.5 text-[13px] leading-[20px] font-[510] tracking-[-0.011em] ${RING} ${restored === null ? \"invisible\" : \"\"}`}\n          style={{ background: c.chip, color: c.soft }}\n        >\n          Undo\n        </button>\n      </footer>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}