Agent rules
NewA rule builder for AI agents: when an agent wants to act and these conditions hold, then ask, notify or block. Rows drag into and out of groups.
agent-rules/agent-rules.tsxGuard risky commands
When
- isgit pushnpm publishrm -rf
- Group2 conditions
- is
- includemigrations/*
Then
When it matches
- from
Otherwise
The live component, numbered part by part. Every measurement is read from it as it is drawn. Point at a row to outline its part, or at a part to find its row.
When to use
- Setting what an agent may do on its own and what needs a person: approve a
git pushtomain, block an edit tomigrations/*. - A policy that reads as a sentence, WHEN the agent wants to do something and these conditions hold, THEN these actions, OTHERWISE those.
- Conditions that mix And and Or a level deep: a group of rows joined among themselves, then with the rest.
- An admin surface where rules are written now and then and read often, so each one should be legible at a glance.
When not to use
- Filtering a table or a search. Use a filter bar: the result is a list of rows, not an action.
- Logic deeper than one group, or with arithmetic and scripts. Offer a code editor with a schema, and show the rule it compiles to.
- A single yes or no setting (“Agents may push to main”). A switch in a settings list says it in one line.
- Rules a person will never read back. A short form with presets is faster to fill.
Moving rows
A row moves by its handle. Dragged, it stays under the pointer and takes a place as soon as it passes a neighbour’s middle, so the others slide out of its way.
- Conditions move anywhere in WHEN: past a group’s header into it, past its last row out of it.
- Actions move within their own list, When it matches or Otherwise.
- The keyboard makes the same moves one place at a time: Alt with ↑ or ↓, or the handle’s menu (Move up, Move down, Move into group, Move out of group, Delete).
And and Or
Each list has one join for all its items, set on its bracket. Mixed logic goes in a group: Command is any of git push And (Branch is main Or Files changed include migrations/*).
Each one drawn with the component itself, used well or badly.
Fields and actions are nouns and verbs a reviewer knows from their tools; values are what they type.
- Field names are short nouns:
Command,Branch,Files changed. - Commands and paths are set in code type, exactly as typed:
rm -rf,migrations/*. - Actions start with a verb:
Ask for approval,Notify,Expire after,Log to. - The meta line says scope and recency:
Every agent in acme · Last matched 12 min ago.
You can add this component with the shadcn CLI or by hand:
Into components/agent-rules of any React project, with no setup:
npx shadcn@latest add https://brunodelorence.com/r/agent-rules.json- Copy the component files
Copy these files into one folder of your project, keeping their relative imports together, or download them as a ZIP:
- Import it
As shown under Usage, below.
Either way it needs React 19 and a bundler that supports CSS Modules, such as Next.js or Vite. It has a dark colour scheme: it follows your page’s color-scheme, light unless the page sets one, or its colorScheme prop, with nothing to configure. Browsers without light-dark() show it light.
Usage
'use client';import { AgentRules, defaultRule, type Rule } from './agent-rules';export function AgentPolicy({ save }: { save: (rule: Rule) => void }) { return ( <AgentRules title="Guard risky commands" meta={['Every agent in acme', 'Last matched 12 min ago']} defaultRule={defaultRule()} onChange={save} autoplay={false} /> );}Each state below is a live instance, locked so you can study it.
The pieces it is built from, each with its measurements read live.
A policy editor for what agents may do on their own. WHEN the agent wants to run a command and the command is any of git push, npm publish or rm -rf, and a group says the branch is main or the files changed include migrations/*, THEN ask the code owners for approval, notify #deploys and expire after 30 minutes; OTHERWISE allow it and log it. Every chip is a listbox, values are tokens you add and remove, and each list is joined by a bracket whose And or Or flips with a click. Drag a row by its handle and it takes its place as it passes the others, which slide out of its way, into a group or out of one; the keyboard makes the same moves.
- The rule is plain data, a trigger, a list of conditions and groups with its join, and two lists of actions. Every edit returns a new rule, and a drag and the keyboard make their moves through the same function, which takes the row out and puts it back at a slot.
- Every slide is one FLIP pass: before a change, the editor notes where each box marked data-flip is on screen; after the new layout it puts each one back there and plays it home on the Web Animations API, 280 ms on cubic-bezier(0.2, 0.8, 0.2, 1). A row inside a moving group or section plays only its own share of the move, so the two add up.
- A dragged row keeps its place under the pointer with the translate property, apart from the FLIP transforms. It takes a new slot as soon as the pointer passes the middle of a neighbour, measured on the new layout, not on boxes still sliding, so it never flickers between two places. It lifts with a 28 px shadow and a 1.2% scale, and settles in 280 ms when dropped.
- Conditions walk every slot in reading order: past a group’s header into it, past its Add condition out of it. Actions stay within their own list. A press on the handle that moves less than 4 px is a click, and opens a menu of the same moves.
- The brackets are a bordered box from the middle of a list’s first row to its last (22 px down each, the middle of a 44 px row), measured from offsetTop after every change and on resize; their ends and the word on them move on transitions with the same timing as the rows.
- Rows are 44 px on one line and wrap their chips on a narrow editor; under 520 px (a container query) the gutter shrinks from 26 to 22 px and the margins tighten, so a 390 px phone never scrolls sideways. Popups shift left inside the editor and open upward when there is no room below.
- While on screen and untouched, a demo pointer picks what the agent wants, flips the top join to Or, drags Files changed out of the group, sets it to All of, carries Command to the bottom, then lets the rule slide back as it was. Any input stops it.
- Every colour is a token on the root with light-dark(): at night the editor turns near-black and the tints deepen, the joins keeping blue for And and violet for Or.
| Prop | Type | Default | Description |
|---|---|---|---|
defaultRule | Rule | — | The rule it opens on: trigger, join, items (conditions and groups), match and otherwise actions. Without it, the risky-commands rule. |
onChange | (rule: Rule) => void | — | Called with the whole rule after every change, a move or an edit. |
title | string | 'Guard risky commands' | The rule’s name, in the header. |
meta | string[] | ['Every agent in acme', 'Last matched 12 min ago'] | The quiet line under the title, its parts set apart by a dot. |
defaultEnabled | boolean | true | Whether the rule starts switched on. |
onEnabledChange | (enabled: boolean) => void | — | Called when the reader flips the switch. |
autoplay | boolean | true | While on screen and untouched, a demo pointer edits the rule and drags rows; any input from the reader stops it. |
colorScheme | 'light' | 'dark' | 'system' | — | Light or dark colours, or the reader’s; without it, the page’s color-scheme. |
className | string | — | A class for the stage. |
Keyboard
- Every chip is a button that opens a listbox (a menu for Add action and the handles): ↓ or ↑ opens it, arrows, Home, End and the first letter move, Enter or Space picks, Escape closes and hands the focus back.
- Alt with ↑ or ↓ moves the row the focus is in, into and out of groups; the focus stays on the same control.
- Tokens: Enter adds and keeps the field open, Backspace in an empty field removes the last one, Escape closes it.
Screen readers
- Each handle is named for its row (“Move Command condition”) and says Alt and an arrow moves it too.
- The join is a button named with its state and what a press does (“Conditions joined by And; switch to Or”).
- A polite live region says each move, drop, add, delete and join change: “Files changed moved to the rule, 3 of 3”.
In this build
- Every chip is a real button with aria-haspopup that opens a listbox (a menu for Add action and the row handles) with aria-selected on the chosen value: ↓ or ↑ opens it, arrows, Home, End and type-ahead move, Enter or Space picks, Escape closes and gives the focus back.
- Dragging has a keyboard way: Alt with ↑ or ↓ moves the row the focus is in, one slot at a time, into and out of groups, and the focus stays on the same control. The handle’s menu offers Move up, Move down, Move into group, Move out of group and Delete.
- The join on each bracket is a button named with its state and what a press does (“Conditions joined by And; switch to Or”); the lists are labelled with their join.
- A polite live region says each move, drop, addition, deletion and join change, with where the row is now (“Files changed moved to the rule, 3 of 3”).
- The switch is role="switch" with aria-checked. Delete buttons show on hover and focus, always on touch screens. Every control shows a focus ring on keyboard focus; icons and the demo pointer are hidden from screen readers.
- With reduced motion, rows, brackets and popups change at once, without slides, and the demo does not play.
The complete source behind this demo. Open source under the MIT license, © 2026 Bruno Delorence: copy it, change it, ship it.
// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent, type PointerEvent as ReactPointerEvent,} from 'react';import { DemoPointer, runDemo, type Point } from './demo-pointer';import { Glyph } from './glyphs';import { ACTION_CHOICES, ActionRow, ConditionRow, type Move } from './rows';import { FIELD_ORDER, TRIGGERS, action, condition, defaultRule as startingRule, editAction, editCondition,/* Agent rules (agent-rules.tsx): a rule editor for what agents may do. A header with the rule's name and its switch, then WHEN (what the agent wants to do, and the conditions, in rows and groups) and THEN (the actions when it matches, and otherwise). Sizes. Rows are 44 px tall on one line (7 px around a 30 px chip) on a 12 px radius; they wrap their chips on a narrow editor and grow. Each list of two or more hangs a bracket in the gutter at its left (26 px, 22 px on a phone) from the middle of the first row's first line to the last's, with its And or Or on it. Motion. Rows and groups slide to their new places in 280 ms on cubic-bezier(0.2, 0.8, 0.2, 1), played from the script (FLIP); the brackets' ends and words follow on transitions with the same timing. A dragged row lifts with a shadow. With reduced motion, everything moves at once. Colours are the tokens below, light, then light-dark(light, dark) where @supports allows: the component takes the color-scheme it inherits from the page, or the one its colorScheme prop sets. */.root { --surface: #fff; --ink: #1d1d1f; --chip-ink: #3a3a3d; --soft: #76767c; --muted: #9b9ba1; --row: #f6f6f7; --row-active: #efeff1;// Copyright (c) 2026 Bruno Delorence. MIT License./* * The rule an agent policy is made of, as plain data, and the pure edits on it: what the agent * wants to do (the trigger), the conditions that must hold (rows, and groups of rows, each list * joined by And or Or), and the actions taken when it matches and when it doesn't. * * Nothing here knows about the DOM. The editor (agent-rules.tsx) draws a Rule and hands every * change back as a new Rule, so the moves a drag makes and the moves the keyboard makes are the * same function: `place`. */export type Join = 'and' | 'or';export type FieldId = 'command' | 'branch' | 'files' | 'repository' | 'agent';/** One test on what the agent is about to do: `Command is Any of git push, rm -rf`. */export type Condition = { kind: 'condition'; id: string; field: FieldId; /** The operator: `Any of`, `Equal to`… one of its field's operators. */ op: string; /** Tokens for a list field, one value for the others. */// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import { useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type PointerEvent as ReactPointerEvent, type ReactNode,} from 'react';import { Glyph, type GlyphName } from './glyphs';import { ACTION_ORDER, ACTIONS, FIELD_ORDER, FIELDS, type Action, type ActionId, type Condition, type FieldId,} from './rule';import { Popup, type Choice } from './select';import styles from './agent-rules.module.css';// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import { createContext, useContext, useEffect, useId, useLayoutEffect, useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent, type ReactNode, type RefObject,} from 'react';import { Glyph } from './glyphs';import styles from './agent-rules.module.css';/* * Every chip that opens a list: a button and its popup, a listbox when it picks a value (Command, * Any of, Code owners), a menu when it runs a command (Add action, a row's handle). The popup hangs * under its button inside the editor, shifted left when it would pass the editor's right edge and// Copyright (c) 2026 Bruno Delorence. MIT License./* * The editor's icons, drawn here on a 16-unit grid with a 1.4 stroke in currentColor, so each * takes the colour of its chip or button. All are decorative: the text next to them names the * control. */const PATHS = { chevron: <path d="M5 6.5l3 3 3-3" />, lock: ( <> <rect x="3.5" y="7" width="9" height="6.5" rx="1.8" /> <path d="M5.5 7V5.2a2.5 2.5 0 015 0V7M8 9.6v1.4" /> </> ), grip: ( <g fill="currentColor" stroke="none"> <circle cx="6" cy="4" r="1.1" /> <circle cx="10" cy="4" r="1.1" /> <circle cx="6" cy="8" r="1.1" /> <circle cx="10" cy="8" r="1.1" /> <circle cx="6" cy="12" r="1.1" /> <circle cx="10" cy="12" r="1.1" />// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import type { CSSProperties, Ref } from 'react';/* * The demo's pointer, and the steps that move it. A demo is a script of awaited steps: wait, or * glide the pointer somewhere. Stopping the demo (the reader takes over, or it scrolls away) ends * the step in progress at once: its promise rejects, which ends the script right there, and the * pointer fades out. * * Each glide is a curved flight played by the Web Animations API, so nothing runs frame by frame. * The points of the flight are handed to whoever wants to move along with it (nothing here uses * them yet). The arrow and the hand are HTML boxes * that swap by their visibility: a change of style inside an SVG would lay the SVG out again. */export type Point = { x: number; y: number };/** A glide's path and timing, for an animation that plays along with it. */export type Flight = { points: Point[]; timing: KeyframeAnimationOptions };export type Demo = {// Copyright (c) 2026 Bruno Delorence. MIT License.'use client';import { useCallback, useEffect, useRef, useState } from 'react';/* * When a demo may play on its own: while part of it is on screen and the tab is visible, never * under reduced motion, and never again once the reader has used it: `take()` hands it to the * reader for good (until the component mounts again), so it can't come back on its own. */export function useAutoplay<T extends HTMLElement>() { const ref = useRef<T>(null); const [visible, setVisible] = useState(false); const [reduced, setReduced] = useState(true); const [taken, setTaken] = useState(false); useEffect(() => { const element = ref.current; if (!element) return; let onScreen = false; const update = () => setVisible(onScreen && !document.hidden); const observer = new IntersectionObserver( ([entry]) => {