Agent Loop
Plan, build, QA, review and security as a row of stage cards, with every time the run went back drawn as a counted arc underneath and the artifacts each stage produced below.
"use client"
import * as React from "react"
import { Check } from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
BrandIcon,
BrandStack,
cx,
Edge,
Roll,
toneColor,
type AiStatus,
type AiTone,
} from "@/lib/ai-kit"
export interface LoopStage {
id: string
name: string
tone: AiTone
/** Brand of the model that runs the stage. */
vendor: string
status: AiStatus
doing: string
tokens?: number
tools: readonly string[]
/** 0–1 while the stage runs. */
progress?: number
}
export interface LoopBack {
/** Stage that sends the run back. */
from: string
label: string
count: number
}
export interface LoopArtifact {
id: string
label: string
brand: string
stage: string
kind: "phone" | "desktop" | "form" | "log" | "scan"
/** Leave out while pending. */
result?: "pass" | "fail"
lines?: readonly string[]
}
export interface AgentLoopProps {
repo: string
branch: string
iteration: number
elapsed: string
tokens: number
stages: readonly LoopStage[]
/** The stage every loop-back returns to. */
target: string
backs: readonly LoopBack[]
/** A loop-back travelling right now (its `from`). */
sending?: string | null
artifacts: readonly LoopArtifact[]
className?: string
}
const CSS = `
.aik-lp{--aik-r-shell:26px;--aik-rim:6px;width:min(100%,1060px);container-type:inline-size}
.aik-lp-card{display:grid;grid-template-columns:minmax(0,1fr);padding:18px 0 6px}
.aik-lp-head{display:flex;align-items:center;gap:10px;flex-wrap:wrap;row-gap:8px;padding:0 22px 18px}
.aik-lp-head b{font-size:16px;font-weight:600}
.aik-lp-repo{display:inline-flex;align-items:center;gap:7px;font-size:14px;color:var(--aik-text-3)}
.aik-lp-stats{margin-left:auto;display:flex;align-items:center;gap:16px;font-size:14px;color:var(--aik-text-3);font-variant-numeric:tabular-nums;white-space:nowrap}
.aik-lp-status{display:inline-flex;align-items:center;gap:7px;height:30px;padding:0 11px;border-radius:9px;font-size:14px;font-weight:500;color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 14%,transparent)}
.aik-lp-status::before{content:"";width:7px;height:7px;border-radius:50%;background:currentColor}
.aik-lp-status[data-s="done"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 14%,transparent)}
.aik-lp-status[data-s="fail"]{color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 14%,transparent)}
.aik-lp-scroll{overflow-x:auto;scrollbar-width:thin;padding:10px 22px 0;margin-top:-10px}
.aik-lp-flow{position:relative;min-width:900px}
.aik-lp-row{display:grid;grid-template-columns:repeat(var(--n),minmax(0,1fr));gap:30px}
.aik-lp-stage{position:relative;display:grid;gap:10px;padding:14px 14px 12px;border-radius:16px;background:var(--aik-raised);box-shadow:inset 0 0 0 1px var(--aik-line);transition:background var(--aik-d-panel),box-shadow var(--aik-d-panel),transform var(--aik-d-move) var(--aik-ease-out)}
.aik-lp-stage[data-s="todo"]{background:color-mix(in oklab,var(--aik-raised) 55%,transparent)}
.aik-lp-stage[data-s="run"]{background:var(--aik-card);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 45%,transparent),0 10px 30px -12px color-mix(in oklab,var(--c) 45%,transparent);transform:translateY(-3px)}
.aik-lp-stage + .aik-lp-stage::before{content:"";position:absolute;left:-19px;top:50%;margin-top:-5px;border:5px solid transparent;border-left:6px solid var(--aik-line-3)}
.aik-lp-stage .aik-beam{--aik-beam-c:var(--c);animation:aik-beam 2.4s linear infinite;border-radius:16px}
.aik-lp-top{display:flex;align-items:center;gap:8px;min-width:0}
.aik-lp-top b{font-size:15px;font-weight:600}
.aik-lp-model{display:grid;place-items:center;width:22px;height:22px;border-radius:7px;background:var(--aik-card);box-shadow:inset 0 0 0 1px var(--aik-line-2)}
.aik-lp-st{margin-left:auto;display:grid;place-items:center;width:20px;height:20px;border-radius:50%;color:var(--aik-on-fill)}
.aik-lp-st[data-s="done"]{background:var(--aik-text-3)}
.aik-lp-st[data-s="todo"]{border:1.5px dashed var(--aik-line-3)}
.aik-lp-st[data-s="run"]{border:2px solid color-mix(in oklab,var(--c) 25%,transparent);border-top-color:var(--c);animation:aik-spin .85s linear infinite}
.aik-lp-st[data-s="fail"]{background:var(--aik-red)}
.aik-lp-st[data-s="ask"]{border:1.5px dashed var(--aik-amber)}
.aik-lp-doing{font-size:14px;line-height:20px;color:var(--aik-text-2);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;min-height:20px}
.aik-lp-stage[data-s="todo"] .aik-lp-doing{color:var(--aik-text-3)}
.aik-lp-meta{display:flex;align-items:center;justify-content:space-between;font:13px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-lp-bar{height:3px;border-radius:3px;background:var(--aik-line);overflow:hidden;margin-top:2px}
.aik-lp-bar i{display:block;height:100%;background:linear-gradient(90deg,color-mix(in oklab,var(--c) 40%,transparent),var(--c));transform-origin:left;transition:transform 300ms linear}
.aik-lp-svg{position:absolute;left:0;top:0;overflow:visible;pointer-events:none}
.aik-lp-back{position:absolute;translate:-50% -50%;display:inline-flex;align-items:center;gap:6px;height:28px;padding:0 10px;border-radius:9px;font-size:13.5px;white-space:nowrap;color:var(--aik-text-3);background:var(--aik-card);box-shadow:inset 0 0 0 1px var(--aik-line-2);transition:color var(--aik-d-panel),box-shadow var(--aik-d-panel)}
.aik-lp-back b{font:600 13px var(--aik-mono);color:var(--aik-text-2)}
.aik-lp-back[data-hit]{color:var(--aik-text);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 55%,transparent)}
.aik-lp-back[data-hit] b{color:var(--c)}
.aik-lp-back[data-live]{box-shadow:inset 0 0 0 1.5px var(--c),0 0 18px -4px var(--c)}
.aik-lp-arts{display:grid;gap:14px;margin:6px 22px 0;padding:18px 0 14px;border-top:1px solid var(--aik-line)}
.aik-lp-arts h4{font-size:15px;font-weight:600}
.aik-lp-arts h4 span{margin-left:6px;font-weight:400;color:var(--aik-text-3)}
.aik-lp-grid{display:grid;grid-template-columns:repeat(5,minmax(0,1fr));gap:14px}
.aik-lp-art{display:grid;gap:10px;min-width:0}
.aik-lp-thumb{position:relative;height:122px;border-radius:14px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);overflow:hidden;display:grid;place-items:end center}
.aik-lp-thumb[data-pending]{background:linear-gradient(100deg,var(--aik-sunk) 30%,var(--aik-raised) 50%,var(--aik-sunk) 70%) 0 0/250% 100%;animation:aik-shimmer 2.2s linear infinite}
.aik-lp-mock{width:72%;height:84%;border-radius:10px 10px 0 0;background:var(--aik-card);box-shadow:0 0 0 1px var(--aik-line-2);padding:16px 10px 0;display:grid;align-content:start;gap:6px;animation:aik-lp-rise 520ms var(--aik-ease-soft) both}
.aik-lp-mock[data-k="phone"]{width:38%}
.aik-lp-mock::before{content:"";position:absolute;margin:-10px 0 0 0;width:20px;height:4px;border-radius:3px;background:var(--aik-line-3)}
.aik-lp-mock i{display:block;height:6px;border-radius:3px;background:var(--aik-line-2)}
.aik-lp-mock i[data-w="s"]{width:45%}.aik-lp-mock i[data-w="m"]{width:70%}
.aik-lp-mock i[data-b]{height:22px;border-radius:6px;background:var(--aik-raised)}
.aik-lp-mock i[data-x]{height:18px;border-radius:6px;background:none;box-shadow:inset 0 0 0 1.5px var(--aik-red)}
.aik-lp-log{width:86%;height:84%;border-radius:10px 10px 0 0;background:var(--aik-card);box-shadow:0 0 0 1px var(--aik-line-2);padding:12px;font:11px/17px var(--aik-mono);color:var(--aik-text-2);white-space:nowrap;overflow:hidden;animation:aik-lp-rise 520ms var(--aik-ease-soft) both}
.aik-lp-badge{position:absolute;right:8px;top:8px;height:24px;padding:0 8px;border-radius:7px;display:grid;place-items:center;font-size:12.5px;font-weight:500;animation:aik-lp-pop 360ms var(--aik-ease-out) both}
.aik-lp-badge[data-r="pass"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 16%,var(--aik-card))}
.aik-lp-badge[data-r="fail"]{color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 16%,var(--aik-card))}
.aik-lp-cap{display:grid;gap:2px;min-width:0}
.aik-lp-cap b{display:flex;align-items:center;gap:7px;font-size:14px;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-lp-cap b > span{min-width:0;overflow:hidden;text-overflow:ellipsis}
.aik-lp-cap > span{font-size:13px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
@keyframes aik-lp-rise{from{opacity:0;transform:translateY(16px)}}
@keyframes aik-lp-pop{from{opacity:0;transform:scale(.8)}}
@container (max-width:960px){.aik-lp-scroll{-webkit-mask:linear-gradient(90deg,transparent,#000 14px,#000 calc(100% - 36px),transparent);mask:linear-gradient(90deg,transparent,#000 14px,#000 calc(100% - 36px),transparent);scroll-behavior:smooth}}
@container (max-width:760px){.aik-lp-grid{grid-template-columns:repeat(2,minmax(0,1fr))}.aik-lp-stats{margin-left:0;width:100%;flex-wrap:wrap;gap:10px}.aik-lp-head,.aik-lp-scroll{padding-left:14px;padding-right:14px}.aik-lp-arts{margin:6px 14px 0}}
@container (max-width:600px){
.aik-lp-scroll{overflow:visible;-webkit-mask:none;mask:none;padding:4px 14px 0;margin:0}
.aik-lp-flow{min-width:0;height:auto!important;display:flex;flex-wrap:wrap;gap:8px}
.aik-lp-row{flex-basis:100%;grid-template-columns:minmax(0,1fr);gap:14px}
.aik-lp-stage + .aik-lp-stage::before{left:22px;top:-12px;margin:0;border:5px solid transparent;border-top:6px solid var(--aik-line-3);border-bottom:0}
.aik-lp-stage[data-s="run"]{transform:none}
.aik-lp-svg{display:none}
.aik-lp-back{position:static;translate:none;height:26px;margin-top:6px;font-size:13px}
}
@media (prefers-reduced-motion: reduce){.aik-lp-mock,.aik-lp-log,.aik-lp-badge,.aik-lp-thumb{animation:none}}
`
/**
* A run of agents working in stages (plan, build, QA, review, security) as a
* row of cards. Every time the run goes back, a light travels the arc under
* the row to the stage it returns to, and that arc's count ticks up.
*/
export function AgentLoop({
repo,
branch,
iteration,
elapsed,
tokens,
stages,
target,
backs,
sending,
artifacts,
className,
}: AgentLoopProps) {
const flow = React.useRef<HTMLDivElement>(null)
const scroller = React.useRef<HTMLDivElement>(null)
// On a narrow card the stage row scrolls; keep the running stage in view.
const running = stages.find(
(st) => st.status === "run" || st.status === "ask"
)?.id
React.useEffect(() => {
const box = scroller.current
const el = running ? cards.current.get(running) : undefined
if (!box || !el || box.scrollWidth <= box.clientWidth + 1) return
box.scrollTo({ left: Math.max(0, el.offsetLeft - 28) })
}, [running])
const cards = React.useRef(new Map<string, HTMLElement>())
const [geo, setGeo] = React.useState<{
w: number
bottom: number
mid: Map<string, number>
left: Map<string, number>
width: Map<string, number>
} | null>(null)
React.useLayoutEffect(() => {
const node = flow.current
if (!node) return
const sync = () => {
const mid = new Map<string, number>()
const left = new Map<string, number>()
const width = new Map<string, number>()
let bottom = 0
cards.current.forEach((el, id) => {
mid.set(id, el.offsetLeft + el.offsetWidth / 2)
left.set(id, el.offsetLeft)
width.set(id, el.offsetWidth)
bottom = Math.max(bottom, el.offsetTop + el.offsetHeight)
})
setGeo({ w: node.offsetWidth, bottom, mid, left, width })
}
sync()
const ro = new ResizeObserver(sync)
ro.observe(node)
return () => ro.disconnect()
}, [stages.length])
const active = stages.find((s) => s.status === "run" || s.status === "ask")
const allDone = stages.every((s) => s.status === "done")
const failed = stages.find((s) => s.status === "fail")
const arcs = geo
? backs.map((back, i) => {
const fromX = geo.mid.get(back.from) ?? 0
const tLeft = geo.left.get(target) ?? 0
const tW = geo.width.get(target) ?? 0
const toX = tLeft + tW * 0.5 - (backs.length - 1 - i) * 12
const y0 = geo.bottom + 6
const level = geo.bottom + 30 + i * 30
const d = `M${fromX} ${y0}V${level - 10}Q${fromX} ${level} ${fromX - 10} ${level}H${toX + 10}Q${toX} ${level} ${toX} ${level - 10}V${y0 + 4}`
return { back, d, level, mid: (fromX + toX) / 2, toX, y0 }
})
: []
const flowH = geo ? geo.bottom + 30 + backs.length * 30 + 10 : 260
return (
<div className={cx("aik aik-lp aik-shell", className)}>
<AiKitStyles />
<AiStyle id="agent-loop" css={CSS} />
<div className="aik-surface aik-lp-card">
<div className="aik-lp-head">
<b>Build loop</b>
<span className="aik-lp-repo">
<BrandIcon name="github" size={16} />
{repo} · {branch}
</span>
<span className="aik-lp-stats">
<span>
Iteration <Roll>{String(iteration)}</Roll>
</span>
<span>{elapsed}</span>
<span>
<Roll>{`${Math.round(tokens / 1000)}K tokens`}</Roll>
</span>
<span
className="aik-lp-status"
data-s={allDone ? "done" : failed ? "fail" : "run"}
>
<Roll>
{allDone
? "Shipped"
: failed
? `Failed · ${failed.name}`
: active
? `Running · ${active.name}`
: "Queued"}
</Roll>
</span>
</span>
</div>
<div ref={scroller} className="aik-lp-scroll">
<div ref={flow} className="aik-lp-flow" style={{ height: flowH }}>
<div
className="aik-lp-row"
style={{ "--n": stages.length } as React.CSSProperties}
>
{stages.map((stage) => (
<div
key={stage.id}
ref={(el) => {
if (el) cards.current.set(stage.id, el)
else cards.current.delete(stage.id)
}}
className="aik-lp-stage"
data-s={stage.status}
style={
{ "--c": toneColor(stage.tone) } as React.CSSProperties
}
>
<span
className="aik-beam"
data-on={stage.status === "run" ? "" : undefined}
/>
<div className="aik-lp-top">
<AgentGlyph
seed={stage.id}
tone={stage.tone}
size={17}
live={stage.status === "run"}
/>
<b>{stage.name}</b>
<span className="aik-lp-model">
<BrandIcon name={stage.vendor} tint="brand" size={13} />
</span>
<span
className="aik-lp-st"
data-s={stage.status}
aria-label={stage.status}
>
{stage.status === "done" ? (
<Check size={12} strokeWidth={3} />
) : null}
</span>
</div>
<p className="aik-lp-doing">
<Roll>{stage.doing}</Roll>
</p>
<div className="aik-lp-meta">
<span>
{stage.tokens
? `${Math.round(stage.tokens / 1000)}K tokens`
: "–"}
</span>
<BrandStack names={stage.tools} size={22} />
</div>
{stage.status === "run" ? (
<div className="aik-lp-bar">
<i
style={{
transform: `scaleX(${Math.max(0, Math.min(1, stage.progress ?? 0))})`,
}}
/>
</div>
) : null}
</div>
))}
</div>
{geo ? (
<svg
className="aik-lp-svg"
width={geo.w}
height={flowH}
aria-hidden="true"
>
{arcs.map(({ back, d, toX, y0 }) => {
const from = stages.find((s) => s.id === back.from)
const tone = from?.tone ?? "blue"
return (
<g key={back.from}>
<Edge
d={d}
tone={sending === back.from ? "rose" : tone}
state={
sending === back.from
? "live"
: back.count
? "done"
: "idle"
}
width={1.6}
duration={0.9}
/>
<path
d={`M${toX - 4.5} ${y0 + 9}L${toX} ${y0 + 2}L${toX + 4.5} ${y0 + 9}`}
fill="none"
stroke={
back.count ? toneColor(tone) : "var(--aik-line-3)"
}
strokeWidth={1.6}
strokeLinecap="round"
strokeLinejoin="round"
/>
</g>
)
})}
</svg>
) : null}
{arcs.map(({ back, level, mid }) => {
const from = stages.find((s) => s.id === back.from)
return (
<span
key={back.from}
className="aik-lp-back"
data-hit={back.count ? "" : undefined}
data-live={sending === back.from ? "" : undefined}
style={
{
left: mid,
top: level,
"--c":
sending === back.from
? "var(--aik-f-rose)"
: toneColor(from?.tone ?? "blue"),
} as React.CSSProperties
}
>
{back.label}
<b>
<Roll>{`${back.count}×`}</Roll>
</b>
</span>
)
})}
</div>
</div>
<div className="aik-lp-arts">
<h4>
Artifacts<span>{artifacts.length}</span>
</h4>
<div className="aik-lp-grid">
{artifacts.map((art) => {
const stage = stages.find((s) => s.id === art.stage)
const kind =
art.kind === "log"
? "Log"
: art.kind === "scan"
? "Report"
: "Screenshot"
return (
<div key={art.id} className="aik-lp-art">
<div
className="aik-lp-thumb"
data-pending={art.result ? undefined : ""}
>
{art.result ? (
art.kind === "log" || art.kind === "scan" ? (
<div className="aik-lp-log">
{(art.lines ?? []).map((line) => (
<div key={line}>{line}</div>
))}
</div>
) : (
<div className="aik-lp-mock" data-k={art.kind}>
<i data-w="m" />
<i data-w="s" />
<i data-b="" />
{art.kind === "form" ? (
<i
data-x={art.result === "fail" ? "" : undefined}
data-b={art.result === "fail" ? undefined : ""}
/>
) : (
<i />
)}
<i data-w="m" />
</div>
)
) : null}
{art.result ? (
<span className="aik-lp-badge" data-r={art.result}>
{art.result === "pass" ? "Pass" : "Fail"}
</span>
) : null}
</div>
<div className="aik-lp-cap">
<b>
<BrandIcon name={art.brand} tint="brand" size={15} />
<span>{art.label}</span>
</b>
<span>
{art.result ? kind : "Pending"} · {stage?.name}
</span>
</div>
</div>
)
})}
</div>
</div>
</div>
</div>
)
}
Installation
One command adds agent-loop 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-loop.json