Delegation chain
The chain of actors behind a token: the person, the agent that acts for the person, and each sub-agent after it.
A small diagram for audit views and for "How this works" panels. It shows the person on whose behalf the work is done, then each agent in the order in which it received the work.
Installation
- CLI
- Manual
The command adds the file to your project, with the shadcn/ui components that it uses. Your project needs shadcn/ui.
- pnpm
- npm
- yarn
- bun
pnpm dlx shadcn@latest add https://examples.lumoauth.dev/r/delegation-chain.json
npx shadcn@latest add https://examples.lumoauth.dev/r/delegation-chain.json
yarn dlx shadcn@latest add https://examples.lumoauth.dev/r/delegation-chain.json
bunx --bun shadcn@latest add https://examples.lumoauth.dev/r/delegation-chain.json
Add the shadcn/ui components that the pattern uses.
npx shadcn@latest add badgeInstall the packages.
npm install lucide-reactCopy the code into your project.
components/delegation-chain.tsximport * as React from 'react';import { ArrowDown, Bot, UserRound } from 'lucide-react';import { Badge } from '@/components/ui/badge';import { cn } from '@/lib/utils';export interface ChainActor {/** The subject of the actor: an agent ID or a client ID. */id: string;/** A name to show in place of the ID. */label?: string;/** The scopes that the token held at this step. */scopes?: string[];}export interface DelegationChainData {/** The person or the service on whose behalf the work is done: the `sub` of the token. */subject: { id: string; label?: string };/** The actors in the order in which they received the work. The last one made the call. */actors: ChainActor[];}/*** Reads the chain from an access token (RFC 8693). The `act` claim of the* token names the caller, and each nested `act` names the actor before it.* The signature is NOT checked here: verify the token first.*/export function chainFromToken(token: string): DelegationChainData | null {try {const part = token.split('.')[1];const json = atob(part.replace(/-/g, '+').replace(/_/g, '/'));const payload = JSON.parse(decodeURIComponent(escape(json))) as Record<string, any>;const actors: ChainActor[] = [];for (let act = payload.act; act && typeof act === 'object'; act = act.act) {if (typeof act.sub === 'string') actors.unshift({ id: act.sub });}const scopes = typeof payload.scope === 'string' ? payload.scope.split(' ').filter(Boolean) : undefined;if (actors.length > 0) actors[actors.length - 1].scopes = scopes;return { subject: { id: String(payload.sub ?? '') }, actors };} catch {return null;}}export interface DelegationChainProps {chain: DelegationChainData;/** The largest number of actors that a chain may have. At the limit the last step is marked. */maxDepth?: number;className?: string;}function Step({ icon: Icon, title, id, note, children }: { icon: React.ElementType; title: string; id?: string; note?: string; children?: React.ReactNode }) {return (<li className="flex gap-3"><span className="flex size-8 shrink-0 items-center justify-center rounded-full border bg-card"><Icon className="size-4" aria-hidden="true" /></span><div className="grid min-w-0 flex-1 gap-1 pb-1"><p className="truncate font-medium">{title}</p>{id && <p className="truncate font-mono text-xs text-muted-foreground">{id}</p>}{note && <p className="text-xs text-muted-foreground">{note}</p>}{children}</div></li>);}export function DelegationChain({ chain, maxDepth, className }: DelegationChainProps) {const { subject, actors } = chain;return (<ol className={cn('grid w-full max-w-sm gap-1 text-sm', className)} aria-label="Delegation chain"><Stepicon={UserRound}title={subject.label ?? subject.id}id={subject.label ? subject.id : undefined}note={actors.length === 0 ? 'Made the call directly. No agent is involved.' : 'The work is done on behalf of this subject.'}/>{actors.map((actor, index) => {const previous = index > 0 ? actors[index - 1].scopes : undefined;const dropped = previous && actor.scopes ? previous.filter((s) => !actor.scopes!.includes(s)) : [];const last = index === actors.length - 1;return (<React.Fragment key={`${actor.id}-${index}`}><li aria-hidden="true" className="flex h-5 w-8 items-center justify-center text-muted-foreground"><ArrowDown className="size-4" /></li><Stepicon={Bot}title={actor.label ?? actor.id}id={actor.label ? actor.id : undefined}note={last ? 'Made the call.' : 'Handed the work on.'}>{actor.scopes && actor.scopes.length > 0 && (<div className="flex flex-wrap gap-1">{actor.scopes.map((scope) => (<Badge key={scope} variant="secondary" className="font-mono font-normal">{scope}</Badge>))}</div>)}{dropped.length > 0 && (<p className="text-xs text-muted-foreground">Narrower than the step before: without <span className="font-mono">{dropped.join(', ')}</span>.</p>)}{last && maxDepth !== undefined && actors.length >= maxDepth && (<p className="text-xs text-amber-700 dark:text-amber-400">The chain is at its largest length. This agent cannot hand the work on.</p>)}</Step></React.Fragment>);})}</ol>);}Change the import paths if your project keeps its components in another place.
Usage
Build the chain yourself, or read it from a token with chainFromToken.
import { DelegationChain, chainFromToken } from '@/components/delegation-chain';
export function Chain({ token }: { token: string }) {
const chain = chainFromToken(token);
if (!chain) return null;
return <DelegationChain chain={chain} maxDepth={5} />;
}
chainFromToken reads the token and does not check its signature. Verify the token on your server first, with verifyAccessToken() of @lumoauth/agent.
The act claim of a token names the caller, and each nested act names the actor before it. The pattern turns that order around, so the chain reads from the person to the caller. To show names in place of IDs, set label on the subject and on each actor.
Where the data comes from
| What the pattern shows | Call |
|---|---|
| The chain of a token | The sub and the nested act claim of the access token |
| The same, with the SDK | DelegationModule.parseActorChain(token), DelegationModule.getSubject(token) |
| Create a link of the chain | delegation.exchange(), delegation.delegateToSubAgent() |
States
- No delegation. The subject made the call directly.
- From a token. The middle chain is read from a token with a nested
actclaim. - At the largest length. With
maxDepth, the last step says that it cannot hand the work on.
Props
| Prop | Type | Description |
|---|---|---|
chainrequired | DelegationChainData | The subject and the actors. |
maxDepth | number | The largest number of actors that a chain may have. |
className | string | Class name for the list. |
DelegationChainData
| Option | Type | Description |
|---|---|---|
subjectrequired | { id: string; label?: string } | The person or the service on whose behalf the work is done. |
actorsrequired | ChainActor[] | The actors in the order in which they received the work. Each has id, label and scopes. |