Agent Branches
A run that forks into sub-agents and merges back, drawn as a git graph: each lane curves out in its own colour, works in parallel and waits at the join while a context bar per agent fills.
"use client"
import * as React from "react"
import { GitBranch, Pause, Play } from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
cx,
Roll,
toneColor,
type AiTone,
} from "@/lib/ai-kit"
export interface BranchStep {
label: string
/** Seconds from the start of the run. */
start: number
/** Seconds; leave out while the step is still running. */
end?: number
failed?: boolean
}
export interface BranchLane {
id: string
name: string
tone: AiTone
steps: readonly BranchStep[]
/** Context used and context window, in tokens. */
used: number
limit: number
}
export interface AgentBranchesProps {
title: string
/** The lead agent; it forks at `fork` and waits at `join`. */
lead: BranchLane
/** The sub-agents it forks into. */
lanes: readonly BranchLane[]
fork: number
join: number
/** Length of the run in seconds, and how far it has got. */
total: number
now: number
playing?: boolean
onToggle?: () => void
/** Drag across the timeline to scrub. */
onScrub?: (seconds: number) => void
className?: string
}
const AXIS = 34
const LEAD_H = 84
const LANE_H = 92
const BLOCK_H = 34
const CSS = `
.aik-br{--aik-r-shell:26px;--aik-rim:6px;width:min(100%,1060px);container-type:inline-size}
.aik-br-card{display:grid;grid-template-columns:minmax(0,1fr)}
.aik-br-head{display:flex;align-items:center;gap:12px;padding:16px 20px;border-bottom:1px solid var(--aik-line);min-width:0}
.aik-br-head > svg{color:var(--aik-text-3);flex:none}
.aik-br-head b{font-size:16px;font-weight:600;white-space:nowrap}
.aik-br-work{display:inline-flex;align-items:center;gap:8px;color:var(--aik-text-3);font-size:14px;white-space:nowrap;min-width:0}
.aik-br-work .aik-mark{transform:scale(.8)}
.aik-br-clock{margin-left:auto;height:30px;display:inline-flex;align-items:center;padding:0 10px;border-radius:9px;background:var(--aik-raised);font:500 13.5px var(--aik-mono);font-variant-numeric:tabular-nums;color:var(--aik-text-2);white-space:nowrap}
.aik-br-scroll{overflow-x:auto;scrollbar-width:thin}
.aik-br-time{position:relative;min-width:680px;margin:0 20px;touch-action:pan-y;cursor:ew-resize;user-select:none}
.aik-br-svg{position:absolute;inset:0;overflow:visible;pointer-events:none}
.aik-br-tick{position:absolute;top:10px;translate:-50% 0;transition:opacity var(--aik-d-state);font:12.5px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-br-now{position:absolute;top:7px;translate:-50% 0;height:22px;padding:0 7px;border-radius:6px;display:grid;place-items:center;font:500 12px var(--aik-mono);color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 16%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-blue) 35%,transparent);z-index:2}
.aik-br-lane{position:absolute;left:0;right:0;border-top:1px solid var(--aik-line)}
.aik-br-name{position:absolute;top:12px;display:flex;align-items:center;gap:8px;font-size:15px;font-weight:500;white-space:nowrap;transition:opacity var(--aik-d-panel)}
.aik-br-name small{font-size:12.5px;font-weight:500;color:var(--aik-text-3)}
.aik-br-block{position:absolute;height:${BLOCK_H}px;display:flex;align-items:center;padding:0 10px;border-radius:9px;font-size:14px;font-weight:500;white-space:nowrap;overflow:hidden;color:color-mix(in oklab,var(--c) 78%,var(--aik-text));background:color-mix(in oklab,var(--c) 16%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 38%,transparent);animation:aik-br-in 380ms var(--aik-ease-out) both}
.aik-br-block span{overflow:hidden;text-overflow:ellipsis;min-width:0}
.aik-br-block[data-live]::after{content:"";position:absolute;right:0;top:0;bottom:0;width:36px;background:linear-gradient(90deg,transparent,color-mix(in oklab,var(--c) 45%,transparent));animation:aik-br-glow 1.1s ease-in-out infinite alternate}
.aik-br-join{position:absolute;translate:-50% -50%;height:24px;min-width:38px;padding:0 7px;border-radius:7px;display:grid;place-items:center;font:600 12.5px var(--aik-mono);color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 14%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-blue) 40%,transparent);z-index:2;transition:color var(--aik-d-panel),background var(--aik-d-panel),scale var(--aik-d-panel) var(--aik-ease-out);animation:aik-br-in 420ms var(--aik-ease-out) both}
.aik-br-join[data-full]{color:var(--aik-on-fill);background:var(--aik-blue);scale:1.08}
.aik-br-ctx{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px 36px;padding:16px 20px 18px;border-top:1px solid var(--aik-line);background:var(--aik-sunk);border-radius:0 0 calc(var(--aik-r-shell) - var(--aik-rim)) calc(var(--aik-r-shell) - var(--aik-rim))}
.aik-br-ctx h4{grid-column:1/-1;display:flex;justify-content:space-between;font-size:14px;font-weight:500;color:var(--aik-text-2)}
.aik-br-ctx h4 span{font-weight:400;color:var(--aik-text-3)}
.aik-br-c{display:grid;grid-template-columns:18px minmax(90px,140px) minmax(0,1fr) 44px;align-items:center;gap:10px;font-size:14px;color:var(--aik-text-2)}
.aik-br-c > i{height:5px;border-radius:5px;background:var(--aik-line);overflow:hidden}
.aik-br-c > i b{display:block;height:100%;border-radius:inherit;background:var(--c);transform-origin:left;transition:transform 120ms linear}
.aik-br-c em{font-style:normal;text-align:right;font:13px var(--aik-mono);color:var(--aik-text);font-variant-numeric:tabular-nums}
@keyframes aik-br-in{from{opacity:0;transform:scaleX(.6);transform-origin:left}}
@keyframes aik-br-glow{from{opacity:.35}to{opacity:1}}
@container (max-width:640px){.aik-br-ctx{grid-template-columns:minmax(0,1fr)}.aik-br-head{flex-wrap:wrap;padding:14px}.aik-br-time{margin:0 14px}}
@container (max-width:760px){.aik-br-scroll{-webkit-mask:linear-gradient(90deg,#000 calc(100% - 40px),transparent);mask:linear-gradient(90deg,#000 calc(100% - 40px),transparent)}}
@media (prefers-reduced-motion: reduce){.aik-br-block,.aik-br-join{animation:none}.aik-br-block[data-live]::after{animation:none}}
`
function fmt(s: number) {
const t = Math.max(0, Math.round(s))
return `${Math.floor(t / 60)}:${String(t % 60).padStart(2, "0")}`
}
function tokens(n: number) {
return n >= 1000 ? `${Math.round(n / 1000)}k` : String(Math.round(n))
}
/**
* One agent run that forks into sub-agents and joins back, drawn like a git
* graph. Each lane curves out in its own colour, its steps grow towards the
* playhead, and the lead waits at the join until every branch is back.
*/
export function AgentBranches({
title,
lead,
lanes,
fork,
join,
total,
now,
playing = true,
onToggle,
onScrub,
className,
}: AgentBranchesProps) {
const area = React.useRef<HTMLDivElement>(null)
const [w, setW] = React.useState(900)
React.useLayoutEffect(() => {
const node = area.current
if (!node) return
const sync = () => setW(node.clientWidth)
sync()
const ro = new ResizeObserver(sync)
ro.observe(node)
return () => ro.disconnect()
}, [])
const pad = 6
const x = (t: number) =>
pad + (Math.max(0, Math.min(total, t)) / total) * (w - pad * 2)
const height = AXIS + LEAD_H + lanes.length * LANE_H
const leadY = AXIS + 30 + BLOCK_H / 2 + 8
const laneTop = (i: number) => AXIS + LEAD_H + i * LANE_H
const laneY = (i: number) => laneTop(i) + 42 + BLOCK_H / 2
const lastEnd = (lane: BranchLane) => {
const tail = lane.steps[lane.steps.length - 1]
return tail?.end
}
const joined = lanes.filter((l) => {
const e = lastEnd(l)
return e !== undefined && e <= now
}).length
const working = lanes.filter((l) => {
const e = lastEnd(l)
return now >= fork && (e === undefined || e > now)
}).length
const scrub = (clientX: number) => {
const box = area.current?.getBoundingClientRect()
if (!box || !onScrub) return
onScrub(
Math.max(
0,
Math.min(
total,
((clientX - box.left - pad) / (box.width - pad * 2)) * total
)
)
)
}
const block = (step: BranchStep, i: number, top: number, tone: AiTone) => {
if (step.start > now) return null
const end = step.end === undefined ? now : Math.min(step.end, now)
const live = step.end === undefined || step.end > now
const left = x(step.start)
const width = Math.max(22, x(end) - left - 3)
return (
<div
key={`${step.label}-${i}`}
className="aik-br-block"
data-live={live ? "" : undefined}
data-fail={step.failed ? "" : undefined}
style={
{
left,
top,
width,
"--c": step.failed ? "var(--aik-red)" : toneColor(tone),
} as React.CSSProperties
}
title={step.label}
>
<span>{step.label}</span>
</div>
)
}
const ticks = [0, 0.25, 0.5, 0.75, 1].map((f) => f * total)
const clip = React.useId().replace(/:/g, "")
return (
<div className={cx("aik aik-br aik-shell", className)}>
<AiKitStyles />
<AiStyle id="agent-branches" css={CSS} />
<div className="aik-surface aik-br-card">
<div className="aik-br-head">
<GitBranch size={18} aria-hidden="true" />
<b>{title}</b>
<span className="aik-br-work">
<i
className="aik-mark"
data-s={
working ? "run" : joined === lanes.length ? "done" : "todo"
}
/>
<Roll>
{working
? `${working} sub-agent${working > 1 ? "s" : ""} at work`
: joined === lanes.length
? "All branches joined"
: now < fork
? "Planning"
: "Joining"}
</Roll>
</span>
<span className="aik-br-clock">
{fmt(now)} / {fmt(total)}
</span>
{onToggle ? (
<button
type="button"
className="aik-btn ghost icon"
onClick={onToggle}
aria-label={playing ? "Pause" : "Play"}
>
{playing ? <Pause size={15} /> : <Play size={15} />}
</button>
) : null}
</div>
<div className="aik-br-scroll">
<div
ref={area}
className="aik-br-time"
style={{ height: height + 8 }}
onPointerDown={(e) => {
if (!onScrub) return
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
scrub(e.clientX)
}}
onPointerMove={(e) => {
if (e.buttons === 1) scrub(e.clientX)
}}
>
<svg
className="aik-br-svg"
width={w}
height={height + 8}
aria-hidden="true"
>
<defs>
<clipPath id={clip}>
<rect x={0} y={0} width={x(now)} height={height + 8} />
</clipPath>
</defs>
{ticks.map((t) => (
<line
key={t}
x1={x(t)}
x2={x(t)}
y1={AXIS}
y2={height}
stroke="var(--aik-line)"
strokeDasharray="2 4"
/>
))}
<g clipPath={`url(#${clip})`}>
{/* The lead waits on a dotted line while its branches work. */}
<line
x1={x(fork)}
x2={x(join)}
y1={leadY}
y2={leadY}
stroke="var(--aik-f-blue)"
strokeOpacity={0.7}
strokeWidth={1.6}
strokeDasharray="3 4"
/>
{lanes.map((lane, i) => {
const xf = x(fork) + 8 + i * 7
const y = laneY(i)
const e = lastEnd(lane)
const xe = x(e ?? now)
const xj = x(join) - 8 - (lanes.length - 1 - i) * 7
const c = toneColor(lane.tone)
const out = `M${x(fork)} ${leadY}H${xf - 10}Q${xf} ${leadY} ${xf} ${leadY + 10}V${y - 12}Q${xf} ${y} ${xf + 12} ${y}H${xe}`
const back = `M${xe} ${y}H${xj - 12}Q${xj} ${y} ${xj} ${y - 12}V${leadY + 10}Q${xj} ${leadY} ${xj + 10} ${leadY}H${x(join)}`
return (
<g key={lane.id}>
<path
d={out}
fill="none"
stroke={c}
strokeWidth={1.75}
strokeLinejoin="round"
/>
{e !== undefined ? (
<path
d={back}
fill="none"
stroke={c}
strokeWidth={1.75}
strokeDasharray="3 4"
strokeOpacity={0.85}
/>
) : null}
</g>
)
})}
</g>
{now >= fork ? (
<circle
cx={x(fork)}
cy={leadY}
r={5}
fill="var(--aik-card)"
stroke="var(--aik-f-blue)"
strokeWidth={2}
/>
) : null}
<line
x1={x(now)}
x2={x(now)}
y1={AXIS - 4}
y2={height}
stroke="var(--aik-blue)"
strokeWidth={1.25}
/>
</svg>
{ticks.map((t, i) =>
i === ticks.length - 1 ? null : (
<span
key={t}
className="aik-br-tick"
style={{
left: x(t),
// Step aside while the "now" chip passes over the label.
opacity: Math.abs(x(t) - x(now)) < 40 ? 0 : 1,
}}
>
{fmt(t)}
</span>
)
)}
<span className="aik-br-now" style={{ left: x(now) }}>
now
</span>
<div
className="aik-br-lane"
style={{ top: AXIS, height: LEAD_H, borderTop: 0 }}
>
<span className="aik-br-name" style={{ left: 8 }}>
<AgentGlyph
seed={lead.id}
tone={lead.tone}
size={16}
live={now < fork || now > join}
/>
{lead.name}
</span>
{lead.steps.map((s, i) => block(s, i, 38, lead.tone))}
</div>
{now >= Math.min(...lanes.map((l) => lastEnd(l) ?? Infinity)) ? (
<span
className="aik-br-join"
data-full={joined === lanes.length ? "" : undefined}
style={{ left: x(join), top: leadY }}
>
{joined}/{lanes.length}
</span>
) : null}
{lanes.map((lane, i) => (
<div
key={lane.id}
className="aik-br-lane"
style={{
top: laneTop(i),
height: LANE_H,
opacity: now >= fork ? 1 : 0.35,
}}
>
<span
className="aik-br-name"
style={{ left: x(fork) + 30 + i * 7 }}
>
<AgentGlyph
seed={lane.id}
tone={lane.tone}
size={16}
live={now >= fork && (lastEnd(lane) ?? Infinity) > now}
/>
{lane.name}
{lane.steps.some(
(s) => s.failed && (s.end ?? 0) <= now && s.start <= now
) ? (
<small>1 retry</small>
) : null}
</span>
{lane.steps.map((s, j) => block(s, j, 42, lane.tone))}
</div>
))}
</div>
</div>
<div className="aik-br-ctx">
<h4>
Context <span>200k window each</span>
</h4>
{[lead, ...lanes].map((lane) => (
<div
key={lane.id}
className="aik-br-c"
style={{ "--c": toneColor(lane.tone) } as React.CSSProperties}
>
<AgentGlyph seed={lane.id} tone={lane.tone} size={16} />
<span>{lane.name}</span>
<i>
<b
style={{
transform: `scaleX(${Math.min(1, lane.used / lane.limit)})`,
}}
/>
</i>
<em>{tokens(lane.used)}</em>
</div>
))}
</div>
</div>
</div>
)
}
Installation
One command adds agent-branches and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/agent-branches.json