Agent Ledger
Everything an agent changed in your systems, each with an undo. Pointing at Undo shows what builds on that change and what can't be taken back.
"use client"
import * as React from "react"
import { ArrowRight, Lock, NotebookTabs, Redo2, Undo2 } from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
BrandTile,
cx,
Roll,
} from "@/lib/ai-kit"
export interface LedgerChange {
id: string
/** What happened, e.g. "Changed price". */
verb: string
/** What it happened to, e.g. "Pro plan". */
object: string
before: string
after: string
/** Brand mark of the system it changed. */
brand: string
service: string
ago: string
/** Changes this one builds on: undoing those also undoes this. */
dependsOn?: readonly string[]
/** Effects that already left your systems and can't be undone. */
lasting?: readonly string[]
undone?: boolean
}
export interface AgentLedgerProps {
agent: string
changes: readonly LedgerChange[]
onUndo?: (id: string) => void
onRedo?: (id: string) => void
/** Show the undo preview for one change, as if its Undo were pointed at. */
preview?: string | null
className?: string
}
const CSS = `
.aik-lg{--aik-r-shell:26px;--aik-rim:6px;width:min(100%,820px);container-type:inline-size}
.aik-lg-head{display:flex;align-items:center;gap:10px;padding:6px 8px 14px}
.aik-lg-head svg{color:var(--aik-blue)}
.aik-lg-head b{font-size:16px;font-weight:600}
.aik-lg-head span{display:inline-flex;align-items:center;gap:7px;color:var(--aik-text-3);font-size:15px;min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-lg-head b{white-space:nowrap}
.aik-lg-head em{margin-left:auto;white-space:nowrap;flex:none;font-style:normal;color:var(--aik-text-3);font-size:14px;font-variant-numeric:tabular-nums}
.aik-lg-head em strong{color:var(--aik-text);font-weight:600}
.aik-lg-list{position:relative;display:grid;padding:6px 8px}
.aik-lg-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
.aik-lg-row{position:relative;display:grid;grid-template-columns:auto minmax(0,1fr) auto;align-items:center;gap:16px;padding:14px 14px 14px 12px;border-radius:15px;transition:background var(--aik-d-state),box-shadow var(--aik-d-panel),opacity var(--aik-d-panel);animation:aik-lg-in 520ms var(--aik-ease-soft) both}
.aik-lg-row + .aik-lg-row::before{content:"";position:absolute;top:0;left:62px;right:14px;height:1px;background:var(--aik-line)}
.aik-lg-row:hover,.aik-lg-row:focus-within{background:var(--aik-hover)}
.aik-lg-row[data-dep]{background:color-mix(in oklab,var(--aik-amber) 9%,transparent);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-amber) 40%,transparent)}
.aik-lg-row[data-src]{z-index:6;background:color-mix(in oklab,var(--aik-red) 8%,transparent);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-red) 38%,transparent)}
.aik-lg-row[data-dep]::before,.aik-lg-row[data-src]::before,.aik-lg-row[data-dep] + .aik-lg-row::before,.aik-lg-row[data-src] + .aik-lg-row::before{opacity:0}
.aik-lg-row[data-undone] .aik-lg-main{opacity:.55}
.aik-lg-main{display:grid;gap:4px;min-width:0;transition:opacity var(--aik-d-panel)}
.aik-lg-main b{font-size:15.5px;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-lg-main b span{margin-left:8px;font-weight:400;color:var(--aik-text-2)}
.aik-lg-diff{display:flex;align-items:center;gap:8px;min-width:0;font-size:14.5px;color:var(--aik-text-2);white-space:nowrap}
.aik-lg-diff s{color:var(--aik-text-3);text-decoration-color:var(--aik-text-3)}
.aik-lg-diff svg{color:var(--aik-text-3);flex:none}
.aik-lg-diff > span{overflow:hidden;text-overflow:ellipsis}
.aik-lg-side{position:relative;display:grid;justify-items:end;min-width:150px;min-height:32px;align-items:center}
.aik-lg-meta{grid-area:1/1;font-size:13.5px;color:var(--aik-text-3);white-space:nowrap;transition:opacity var(--aik-d-state),transform var(--aik-d-state) var(--aik-ease-out)}
.aik-lg-side .aik-btn{grid-area:1/1;opacity:0;transform:translateX(6px);transition:opacity var(--aik-d-state),transform var(--aik-d-state) var(--aik-ease-out),background var(--aik-d-state)}
.aik-lg-row:hover .aik-lg-side .aik-btn,.aik-lg-row:focus-within .aik-lg-side .aik-btn,.aik-lg-row[data-src] .aik-lg-side .aik-btn,.aik-lg-row[data-undone] .aik-lg-side .aik-btn{opacity:1;transform:none}
.aik-lg-row:hover .aik-lg-meta,.aik-lg-row:focus-within .aik-lg-meta,.aik-lg-row[data-src] .aik-lg-meta,.aik-lg-row[data-undone] .aik-lg-meta{opacity:0;transform:translateX(-6px)}
.aik-lg-row[data-src] .aik-lg-side .aik-btn{background:var(--aik-red);color:var(--aik-on-fill)}
.aik-lg-tag{display:inline-flex;align-items:center;height:22px;padding:0 8px;border-radius:7px;font-size:12px;font-weight:500;color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 15%,transparent)}
.aik-lg-pop{grid-column:2/-1;display:grid;grid-template-columns:repeat(auto-fit,minmax(min(200px,100%),1fr));gap:14px;padding:12px 14px;min-width:0;border-radius:12px;background:var(--aik-card);box-shadow:var(--aik-sh-card);animation:aik-lg-pop 320ms var(--aik-ease-out)}
.aik-lg-pop h4{margin-bottom:8px;display:flex;align-items:center;gap:7px;font-size:12px;font-weight:500;letter-spacing:.04em;text-transform:uppercase;color:var(--aik-text-3)}
.aik-lg-pop ul{display:grid;gap:6px}
.aik-lg-pop li{display:flex;align-items:center;gap:9px;font-size:14px;line-height:19px}
.aik-lg-pop li i{width:7px;height:7px;flex:none;border-radius:50%;background:var(--aik-amber)}
.aik-lg-pop [data-lock] li{color:var(--aik-text-2)}
.aik-lg-pop [data-lock] li svg{color:var(--aik-red);flex:none}
.aik-lg-pop p{font-size:13.5px;color:var(--aik-text-3)}
.aik-lg-rail{fill:none;stroke:var(--aik-amber);stroke-width:1.75;stroke-dasharray:4 4;animation:aik-lg-flow 800ms linear infinite}
@keyframes aik-lg-in{from{opacity:0;transform:translateY(-10px)}}
@keyframes aik-lg-pop{from{opacity:0;transform:translateY(-6px)}}
@keyframes aik-lg-flow{to{stroke-dashoffset:-16}}
@container (max-width:560px){
.aik-lg-row{grid-template-columns:auto minmax(0,1fr);row-gap:8px}
.aik-lg-pop{grid-column:1/-1}
.aik-lg-side{grid-column:2;display:flex;align-items:center;gap:10px;min-width:0}
.aik-lg-side .aik-lg-meta,.aik-lg-row .aik-lg-side .aik-lg-meta{opacity:1;transform:none}
.aik-lg-side .aik-btn{opacity:1;transform:none}
.aik-lg-diff{flex-wrap:wrap}
}
@media (prefers-reduced-motion: reduce){.aik-lg-row,.aik-lg-pop{animation:none}.aik-lg-rail{animation:none}}
`
/**
* Everything an agent changed in your systems, newest first, each with an
* undo. Pointing at Undo lights the changes that build on it (they would be
* undone too), draws the chain between them, and lists what already left your
* systems and can't be taken back.
*/
export function AgentLedger({
agent,
changes,
onUndo,
onRedo,
preview,
className,
}: AgentLedgerProps) {
const list = React.useRef<HTMLDivElement>(null)
const rows = React.useRef(new Map<string, HTMLElement>())
const [hover, setHover] = React.useState<string | null>(null)
const [rail, setRail] = React.useState<string | null>(null)
const source = hover ?? preview ?? null
// Everything that builds on the source, directly or through another change.
const deps = React.useMemo(() => {
const out = new Set<string>()
if (!source) return out
let grew = true
while (grew) {
grew = false
for (const c of changes) {
if (out.has(c.id) || c.undone) continue
if (c.dependsOn?.some((d) => d === source || out.has(d))) {
out.add(c.id)
grew = true
}
}
}
return out
}, [source, changes])
React.useLayoutEffect(() => {
if (!source || !deps.size || !list.current) {
setRail(null)
return
}
const ys = [source, ...deps]
.map((id) => rows.current.get(id))
.filter((n): n is HTMLElement => Boolean(n))
.map((n) => n.offsetTop + n.offsetHeight / 2)
.sort((a, b) => a - b)
const x = 4
let d = `M${x + 10} ${ys[0]}H${x}`
d += `V${ys[ys.length - 1]}H${x + 10}`
for (const y of ys.slice(1, -1)) d += `M${x} ${y}H${x + 10}`
setRail(d)
}, [source, deps])
const active = changes.filter((c) => !c.undone).length
const src = changes.find((c) => c.id === source)
return (
<div className={cx("aik aik-lg aik-shell", className)}>
<AiKitStyles />
<AiStyle id="agent-ledger" css={CSS} />
<div className="aik-surface">
<div className="aik-lg-head" style={{ padding: "16px 20px 10px" }}>
<NotebookTabs size={18} aria-hidden="true" />
<b>Ledger</b>
<span>
<AgentGlyph seed="launch" tone="blue" size={14} live />
{agent}
</span>
<em>
<strong>
<Roll>{String(active)}</Roll>
</strong>{" "}
changes
</em>
</div>
<div
ref={list}
className="aik-lg-list"
style={{ padding: "4px 12px 12px" }}
>
<svg className="aik-lg-svg" aria-hidden="true">
{rail ? <path className="aik-lg-rail" d={rail} /> : null}
</svg>
{changes.map((change) => {
const isSrc = change.id === source
const isDep = deps.has(change.id)
return (
<div
key={change.id}
ref={(node) => {
if (node) rows.current.set(change.id, node)
else rows.current.delete(change.id)
}}
className="aik-lg-row"
data-src={isSrc && !change.undone ? "" : undefined}
data-dep={isDep ? "" : undefined}
data-undone={change.undone ? "" : undefined}
>
<BrandTile name={change.brand} size={38} />
<div className="aik-lg-main">
<b>
{change.verb}
<span>{change.object}</span>
</b>
<span className="aik-lg-diff">
{change.undone ? (
<>
<s>{change.after}</s>
<ArrowRight size={14} aria-hidden="true" />
<span>{change.before}</span>
</>
) : (
<>
<s>{change.before}</s>
<ArrowRight size={14} aria-hidden="true" />
<span>{change.after}</span>
</>
)}
{isDep ? (
<span className="aik-lg-tag">Undone too</span>
) : null}
</span>
</div>
<div className="aik-lg-side">
<span className="aik-lg-meta">
{change.ago} · {change.service}
</span>
{change.undone ? (
<button
type="button"
className="aik-btn line sm"
onClick={() => onRedo?.(change.id)}
>
<Redo2 size={13} aria-hidden="true" /> Redo
</button>
) : (
<button
type="button"
className="aik-btn line sm"
onPointerEnter={() => setHover(change.id)}
onPointerLeave={() => setHover(null)}
onFocus={() => setHover(change.id)}
onBlur={() => setHover(null)}
onClick={() => {
setHover(null)
onUndo?.(change.id)
}}
>
<Undo2 size={13} aria-hidden="true" /> Undo
</button>
)}
</div>
{isSrc && src && !src.undone ? (
<div className="aik-lg-pop" role="status">
<div>
<h4>Also undoes</h4>
{deps.size ? (
<ul>
{changes
.filter((c) => deps.has(c.id))
.map((c) => (
<li key={c.id}>
<i aria-hidden="true" />
{c.verb} · {c.object}
</li>
))}
</ul>
) : (
<p>Nothing else builds on this change.</p>
)}
</div>
{src.lasting?.length ? (
<div data-lock="">
<h4>Can't be taken back</h4>
<ul>
{src.lasting.map((line) => (
<li key={line}>
<Lock size={13} aria-hidden="true" />
{line}
</li>
))}
</ul>
</div>
) : null}
</div>
) : null}
</div>
)
})}
</div>
</div>
</div>
)
}
Installation
One command adds agent-ledger 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-ledger.json