name: machine-view description: Explain or review how a UI component, hook system, or stateful feature works as an animated "machine" diagram with a fixed visual language. It has two views, Flow (what happens, in four bands) and Anatomy (how it's built: components, hooks in call order, ref handles, prop drilling), plus two layers: React internals (event/render/commit/layout/paint/passive phases, queued updates, a fiber walk of which components render, bail out, mount or unmount) and accessibility (ARIA state, DOM and virtual focus, approximate screen reader output). Review notes mark smells. Use when the user asks to visualize, map, break down, review, or "show me how it works" for components, state, refs, effects, event handling or accessibility, or wants a mental model of code an AI wrote. Not for static architecture boxes-and-arrows or for data charts.
machine-view 是做什么的?
The user reads code less and less, so they lose the feel of how a system moves and how it's wired. This skill gives that back as a small animated machine. A spec you write is rendered by a fixed renderer. The vocabulary never changes between diagrams, so the user's intuition builds up across every machine they see. Do not draw freehand, invent shapes or colours, or restyle the template.
One spec gives the user three ways to look at the same system:
- Flow: what happens. Four bands. A scenario's tokens move through them.
- Anatomy: how it's built. Each component is a card. Rows are its hooks in call order, then the DOM it renders. Ref handles and prop drilling are drawn explicitly. The same scenarios replay here.
- React internals layer (a toggle in both views): a phase rail, EVENT › RENDER › COMMIT › LAYOUT › PAINT › PASSIVE › LATER, that lights up as each moment happens. State set outside render shows as
old ⇢ new(faded, queued) until the next render applies it. A fiber walk lists, in order, which components render, bail out, mount or unmount, and why; in anatomy view a light travels card to card. - Accessibility layer (a toggle in both views): ARIA attributes on UI parts, DOM focus versus virtual focus rings, and a transcript of approximately what a screen reader says.
Plus review notes: numbered markers on the diagram, listed below it. Some come from you; the build adds others automatically.
The language (memorize; it is the whole point)
Flow view bands, always in this order, top to bottom. "Appearance rises, intent sinks."
| band | holds | kinds |
|---|---|---|
| SURFACE | what the user sees and touches | ui |
| BEHAVIOR | logic that runs during render or on events | hook, handler, derived |
| STATE | what is true; survives re-renders | state, context, ref |
| WORLD | outside React | effect, external |
Parts (node kinds):
ui: a DOM element or rendered child.hook: a custom or library hook.handler: an author-written callback.derived: computed during render (useMemo, selectors, consts in the render body). Dashed.state: a source of truth. List itsfields.context: a provider value.ref: a mutable box that does not re-render. Dotted. Give it acurrentfield when what it points at matters.effect: reaches outside React (useEffect,useLayoutEffect, loaders, subscriptions). Put the timing indetail.external: network, storage, router, timers, browser APIs.
Wires (edge kinds):
read: props, return values, state reads. Continuous drifting dots, because the data is always present.event: an event or callback call. Dashed pink.write: a setter or dispatch. Gold.effect: triggers outside work. Violet.response: an async result. Dotted green.subscribe: a long-lived listener. Grey dots.ref: a ref handle being passed or attached. Drawn ○ → ●: ○ is where the ref was created, ● is where it's attached (to aui) or used (by a hook or effect). Usereadfrom a ref only when code reads.current.
Motion (scenario actions): each has exactly one meaning.
send: one labelled token travels a wire (a single moment).set: a field changes, shown asold → newin gold.render: parts re-render (gold ring).fire: an effect runs (violet ring). Withcleanup: true, its cleanup or abort runs (contracting ring).pulse: draws attention without changing anything.focus: DOM focus moves (solid ring).nullmeans focus fell to<body>or nowhere.vfocus: virtual focus, e.g. aria-activedescendant (dashed cyan ring), with alabel.nullclears it.aria: an ARIA attribute changes (cyanold → new).announce: what a screen reader says, plusvia(focus / live region / activedescendant / why it's silent).wait: pause for N seconds.fiber: the render walk, in order:[{ "c": "<component id>", "op": "render|bailout|mount|unmount", "why": "open prop changed" }].
Phases (React internals): put "phase" on a step (or on a single action to override it). The values are event (handlers; setState only queues), render (components called; pure; may run twice in StrictMode), commit (DOM mutation, refs detached then attached), layout (useLayoutEffect, before paint), paint, passive (useEffect: cleanups, then effects) and async (network or timers that lead to a new event). Model them faithfully. The point is to show when things really happen. Common truths worth showing:
- a
setineventdoesn't change the value untilrender; - a ref is
nullduring the first render and only attached incommit; useEffectruns after paint, so state it sets causes a second render;- a parent's re-render re-renders every non-memoized child, so list those children in the fiber walk with
why: "parent re-rendered"; usebailoutonly when memo or identical elements actually skip them.
Workflow
-
Read the real code first. Trace element → event → handler/hook → setter → state → effects → render. For anatomy, also note which component calls each hook, in what order, every place a ref is created, passed, attached and read, and every prop that is only forwarded.
-
Inventory. Every part gets a
coderef (path/file.tsx:42). If you didn't confirm something in code, set"inferred": true; it renders visibly. -
Pick the zoom. In flow view, keep it to ≤ 10 parts, ≤ 4 per band, ≤ 16 wires. Parts that only matter to the "how" (individual refs, inner library hooks, wrapper elements) get
"views": ["anatomy"]so flow stays readable. Keep anatomy to ≤ 7 components. For bigger systems, make one machine per feature. -
Write 2–4 scenarios, each a real user story:
- the happy path (first);
- the tricky case: a race, cancellation, stale closure, ref timing, error, or optimistic rollback;
- a surprising consequence if one exists. These are where the skill earns its keep.
Each step's
sayis one plain cause→effect sentence (≤ 140 chars) about why. Put the identifier in the tokenlabeland the line incode. -
Accessibility (do this for any interactive UI): give each
uipart theariait actually renders (role, accessible name,aria-expanded,aria-controls,aria-activedescendant,aria-live…). Write"(missing)"or"(none)"when it's absent. That is how gaps become visible. Addfocus/vfocus/ariaactions where they change, and anannouncewherever a screen reader would speak, or would stay silent when it shouldn't ("(silence)", with the reason invia). Announcements are approximate: model them on the ARIA that is present, avoid claiming exact wording for one specific reader, and cite library behaviour only when you've seen it in code. -
Review notes. Add
smellsfor real problems you found, withlevelbug,warnornote, a shorttitle, andtextthat says what goes wrong and what to do instead. Don't repeat what the build detects automatically:- props or refs drilled through ≥ 2 components (wires with
via), grouped per chain; - a ref read during render (
readfrom arefintouiorderived); - one ref attached to several elements;
- a ref used by several other components;
- an effect that writes state it depends on.
With phases and fiber walks present, the build also flags state set during render, state set in layout or passive effects, and pass-through components (which own nothing) that re-render. Model the code faithfully (use
via,owner,views,phase,fiber) so these fire. - props or refs drilled through ≥ 2 components (wires with
-
Build and validate:
python3 <skill-dir>/build.py spec.json out.html. Fix everyerror:, and takewarning:s seriously. Phases: for anything involving refs, effects, focus, measuring or re-render cost, give every step aphaseand add afiberwalk to each render step. Set"internals": trueat the top level to turn the layer on by default. -
Show it. If an inline HTML render tool exists (e.g.
html_render), pass it the built file's full contents. Height is roughly 750 for flow-only and 1,000–1,400 with notes and anatomy; measure if you can. Otherwise give the absolute path to open in a browser. Offer to save the spec next to the code (e.g.docs/machines/<feature>.json) so it can be rebuilt after changes. Set"defaultView": "anatomy"when the question is about structure or code review. -
Keep prose short: at most three sentences on what to watch for. The machine does the explaining.
Spec format
{
"title": "Feature — one-line name",
"subtitle": "What this is and where it lives.",
"defaultView": "flow",
"internals": true,
"components": [
{ "id": "app", "label": "App", "code": "App.tsx" },
{ "id": "pal", "label": "Palette", "parent": "app", "code": "Palette.tsx" }
],
"nodes": [
{ "id": "q", "kind": "state", "owner": "app", "order": 1, "label": "useState(query)", "code": "App.tsx:9", "fields": { "query": "''" } },
{ "id": "inputRef", "kind": "ref", "owner": "app", "order": 2, "label": "useRef(inputRef)", "code": "App.tsx:10", "fields": { "current": "null" }, "views": ["anatomy"] },
{ "id": "input", "kind": "ui", "owner": "pal", "label": "<input>", "code": "Palette.tsx:20",
"aria": { "role": "combobox", "accessible name": "Search", "aria-expanded": "false" } }
],
"edges": [
{ "id": "type", "from": "input", "to": "q", "kind": "write", "label": "onChange → setQuery" },
{ "id": "attach", "from": "inputRef", "to": "input", "kind": "ref", "label": "ref={inputRef}", "views": ["anatomy"] },
{ "id": "drill", "from": "inputRef", "to": "fx", "kind": "ref", "label": "inputRef", "via": ["header", "toolbar"] }
],
"smells": [
{ "at": "input", "level": "bug", "title": "no accessible name", "text": "What goes wrong, and the fix." }
],
"scenarios": [
{ "name": "Type a letter", "summary": "Shown when the scenario ends.", "initialFocus": "input",
"initial": { "q": { "query": "'a'" }, "input": { "aria-expanded": "true" } },
"steps": [
{ "say": "Why this happens, in one sentence.", "code": "Palette.tsx:44", "phase": "event",
"do": [ { "send": "type", "label": "'b'" }, { "set": "q", "field": "query", "value": "'ab'" },
{ "aria": "input", "attr": "aria-expanded", "value": "true" },
{ "announce": "3 results available", "via": "live region" } ] }
] }
]
}
ownerputs a part in a component card.orderis hook call order (UI parts list after the hooks). Parts with no owner go in an "Outside React" card.vialists the components a wire passes through untouched, in travel order. Each one gets a "⇢" row in its card.views(["flow"]or["anatomy"]) hides a part or wire from the other view. Actions on hidden parts are skipped in that view.fieldsandariavalues are display strings. Only declared ones can besetor changed witharia, andinitialmay override either.- Actions in a step play in order, so their order is the causal order.
Mapping beyond React
The bands are generic. A server is SURFACE = endpoints, BEHAVIOR = middleware/handlers, STATE = DB/cache, WORLD = queues/third parties, and components become modules. Keep the kinds; only the labels change.
Examples: examples/rac-combobox.json (React Aria's real ComboBox, read from adobe/react-spectrum source with file:line refs; anatomy, internals, a11y, review notes on an every-render effect and a doubly attached ref) and examples/sloppy-palette.json (an illustrative hand-rolled palette with drilled refs, a ref read during render, cross-subtree focus and a11y gaps).