/factory-kit:factory-state-modelingfrom factory-kit
Teaches a disciplined approach to modeling UI state in types before writing components. Covers boolean-to-union collapse, exhaustive switches, tracing control flow, deriving values, and feature-flag lifecycle. Read before scaffolding or reviewing any frontend surface.
Ships in factory-kit. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Each section leads with Principle (one sentence, stack-agnostic), then Why (constraint, option, tradeoff), then Recipe (the React + TypeScript shape we use), and Failure mode where one has been observed. factory-frontend owns CRUD recipes; this skill owns the reasoning layer those recipes depend on.
Principle. An interface program never exits; its whole job is choosing what to show for the current state, so branching over state is the product, not overhead.
Why. A pipeline runs once from input to output. Every branch there is a path the data might take and costs twice: once at runtime across every record, once in your head when tracing what happened. Less code is better because the program has one job. An interface is a different kind of program. It is re-run on every state change, and "given this user, role, device, selection and pending request, what is visible" is the entire specification. Engineers arriving from scripts carry the minimize-code instinct into UI and express state as a handful of ad hoc booleans "to keep it simple." The result is fewer lines and more states than anyone can enumerate. The correct instinct in an interface is to enumerate state explicitly, in one place per concern, and let the compiler hold the list.
Recipe. Before touching markup, write the states as a type. If you cannot write the type, you do not yet know what you are building.
Failure mode. Pipeline instincts applied to a shell component: a boolean per concern added one at a time, each one locally reasonable, until nobody can say which combinations are legal.
Principle. More than three boolean props on one component is a signal to collapse them into a discriminated union with an exhaustive switch.
Why. n independent booleans are 2^n states. Five is thirty-two, and most of them are nonsense: "drawer closed but editing row 7," "confirming with nothing selected." Nonsense states are still reachable, so they still need code paths, and that code is written by whoever hits the bug first. A discriminated union names only the legal states, each carrying only the fields it needs, so the illegal ones cannot be constructed. The exhaustive never check turns "I forgot a case" from a runtime surprise into a typecheck failure at the line that forgot it. The union is slightly more code than the booleans. That is the trade: a few more lines in exchange for a state space you can read.
Recipe.
type PanelMode =
| { kind: 'closed' }
| { kind: 'command'; query: string }
| { kind: 'peek'; id: string }
| { kind: 'create'; draft: Draft }
| { kind: 'confirm'; action: PendingAction };
function assertNever(x: never): never {
throw new Error(`Unhandled case: ${JSON.stringify(x)}`);
}
switch (mode.kind) {
case 'closed': return null;
case 'command': return <CommandBody query={mode.query} />;
case 'peek': return <PeekBody id={mode.id} />;
case 'create': return <CreateBody draft={mode.draft} />;
case 'confirm': return <ConfirmBody action={mode.action} />;
default: return assertNever(mode);
}
When proposing the union for an existing component, list the boolean combinations it removes. That list is the review artifact.
Failure mode. Kairos FloatingPanelDialog on KAI-309 reached ten boolean props, each added for one host (command, peek, create, confirm), before anyone wrote the union. Nobody could say which of the thousand combinations were intended.
Principle. The compiler sees nothing past an RPC response, an environment variable or a cast, so those three places get runtime validation, and every UI control must trace to the server predicate it changes.
Why. Look at where the costly incidents in a typed codebase actually come from. They are almost never logic the compiler could have checked. They are a row-level security change that silently nulled an embedded relation, a role guard with a NULL-permissive NOT IN, a backfill keyed on the wrong timestamp, a date picker wired to a query that ignores dates. In every case the TypeScript layer trusted a shape or an effect that the other side never guaranteed. Casts are the explicit version of the same mistake: as any is the developer telling the compiler to stop looking, and it does. The rule is not "never cast." It is "cast only where a runtime check stands beside it, so the trust is earned."
Recipe.
as any, as unknown as T) is allowed only at an import boundary with a Zod parse next to it, or with a comment naming the ticket that removes it. Inside component logic it is a defect.process.env.X is a string or undefined; compare to 'true', never rely on truthiness.Failure mode. Kairos: an RLS lockdown nulled a distributors embed and the rep's name was stamped as the distributor on new orders. A NOT IN guard over a nullable role let a profile-less auth user mark invoices paid. A commissions timeframe control was provably inert because the RPC behind it was date-blind by construction.
Principle. A value computable from props or other state is computed on render, never kept in a second piece of state that must be synced.
Why. Two copies of one fact will disagree. The second useState holding a derived total is correct on the render that set it and stale on the next one that changed an input, and the bug surfaces as "sometimes the total is wrong," which nobody can reproduce. useMemo recomputes from inputs; there is nothing to sync. This is the UI-local case of a broader rule: factory-frontend §One direction of truth covers server state versus client state. This section covers state versus state within one component tree.
Recipe. If the value can be written as f(props, state), write it as useMemo(() => f(...), [deps]) or as a plain expression. Reserve useState for facts the user created that cannot be recomputed: text they typed, a row they selected, a drawer they opened. A codebase with twice as many useState calls as useMemo calls usually has mirrored state hiding in the difference.
Failure mode. Kairos KAI-148: an uncontrolled form held line totals in state and recomputed them only on some edits, so the submitted total did not match the lines.
Principle. A feature flag is read once at the server boundary, decides which component tree to mount, and carries a ticket that deletes it.
Why. A flag separates deploying code from releasing a feature. Code merges to main small and early, ships to production dark, goes live when you choose and retreats in seconds without a revert. That is worth one if. The danger is that a flag is a boolean whose value comes from outside the type system, exactly the kind of switch the previous sections warn about. Threaded through twenty components, it multiplies against every state those components already have. Read once at the top, it produces two trees that are each internally unaware of the flag, and the state space below the boundary does not grow. Flags are also temporary by definition. Every other state in the app is permanent product state. A flag nobody owns stays forever, and a year later there are two UIs and nobody remembers which one is real.
Recipe.
// app/admin/layout.tsx (server component)
const panelOn = process.env.KAIROS_MOBILE_COMMAND_PANEL === 'true';
return <AdminShell mobileCommandPanel={panelOn} ... />;
// Below the shell, nothing reads the env var.
Failure mode. Flag threaded below the shell: each component gains one more bit of state it did not need. Flag with no removal owner: permanent dual maintenance. The env-var flag is global; when someone needs "on for one user," move it to a small admin-writable settings table read at the same boundary rather than adding a second mechanism alongside.
Principle. For any new or substantially changed component, the props type and the state union are approved first; the implementation follows, and the review leads with which states were added or removed.
Why. Agentic development means the operator reviews outcomes, and outcomes hide the state model. A screenshot that looks right says nothing about how many states the component can be in. Approving the type first moves the review from "does it look right" to "which states exist," which is the question the incidents above all failed. It also costs almost nothing: a type and one sentence per state on when it occurs. For a fixed defect, the first failing test is the operator's to write; the agent offers the shape and the assertion. A test you wrote and watched fail for the right reason is a test you understand. See factory-mentor.md for the pedagogy behind this.
Recipe. The agent's review message opens with:
## State model
- Added: { kind: 'confirm'; action } (nested confirmation suspends the panel)
- Removed: isConfirming, pendingAction (collapsed into the union)
- Unchanged: 'closed' | 'command' | 'peek' | 'create'
## Diff
...
Where it lives. When the work has a Linear issue, put the state model in the ticket's working doc with factory-log state before writing the component: ts code blocks plus a state → screen table. The operator pushes back in a comment; each agreed change replaces the section. The review message links to the doc instead of repeating it.
Failure mode. Review by screenshot. The surface looks right at 390px and desktop; the component has four booleans more than it did last week.
factory-frontend.md owns the CRUD recipes (DataTable + drawer mode union) this reasoning producesfactory-frontend-engineer.md runs "What are the states?" as a workflow step before scaffoldingfactory-design.md owns visual coherence; this skill does not touch tokens or layoutfactory-mentor.md owns the operator-learning contract that "types before code" servesfactory-pitfalls.md indexes the failure modes above under Frontendfactory-log.md writes the state model into the Linear issueWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
47 skills in this plugin
/factory-animationMotion discipline distilled across builds. Attention as a fixed per-viewport budget, figure/ground separation (calm the ground, load motion into the figure), one focal point of motion per screen, diegetic-over-decorative test, trigger-on-intent-then-settle over perpetual loops, and the prefers-reduced-motion fallback. Recipe pairs Astro with a Remotion video kit (rendered assets in public/). Read whenever you add motion to a marketing surface or any page where movement competes with reading. Paired with factory-design, which owns the static visual vocabulary motion sits on top of./factory-apiAPI conventions for both server actions and tRPC builds. Covers the decision between them, per-mutation Zod input schemas, central router composition, pagination shape, multi-field search via Drizzle `ilike` + `or()`, mutation lifecycle hooks, conditional query enabling, stale-time defaults, error response shape and custom error class taxonomy, mutation-boundary audit logging, fetch adapter for tRPC routes. Read when designing or implementing any data-mutation endpoint./factory-api-route-engineerUse when designing or implementing API endpoints — server actions, tRPC procedures, REST routes for external consumers. Carries the factory's API conventions — the server actions vs tRPC decision, procedure tier stacking, per-mutation Zod schemas, central router composition with manual registration, pagination (limit/offset/orderBy default; cursor only when needed), multi-field search via `ilike` + `or()`, aggregated stats in list queries, mutation lifecycle hooks, stale-time defaults, custom error class taxonomy, fetch adapter for tRPC in App Router. Outputs endpoints that fit the house style — not bespoke per-route handlers./factory-authAuth and authorization conventions distilled across builds with three different auth stacks (Better Auth + orgs, Supabase Auth + RLS, Clerk). Covers the provider decision matrix, the unified wrapper interface (`requireAuth` / `requireRole` / `withOrgContext`), procedure tier stacking, session handling, OAuth callback safety, plugin composition with Better Auth, and the anti-patterns (hardcoded allowlists, admin client at module scope, triple-fallback auth surfaces)./factory-auth-wiring-specialistUse when wiring auth into a new project, switching auth providers, or adding role/org features. Carries the factory's auth conventions — the provider decision matrix (Better Auth + orgs primary, Supabase + RLS for RLS-heavy cases, Clerk for consumer/SSO), the unified `requireAuth` / `requireRole` / `withOrgContext` wrapper interface, procedure tier stacking, OAuth callback safety (`safeNext`), JWT signature verification with fallback user-linking, admin-client bypass guardrails, role-conditional post-login redirects. Produces auth code that fits the house seam — provider is a swap point, not a leak./factory-autopilotThe contract for an unattended build run started by `factory-kit autopilot run`. The agent edits and verifies in its assigned worktree; the runner owns the gate, commits, push, draft PR and Linear handoff. Read when a prompt says you are running under autopilot.npx claudepluginhub nonlinear-xyz/factory-kit --plugin factory-kitOther plugins' skills for the same job