writing-kea-logics — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited writing-kea-logics (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
PostHog uses kea as the state container for the frontend. Almost all non-trivial business logic lives in a *Logic.ts / *Logic.tsx file, not in React. We may be on a kea pre-release ahead of the version the keajs.org docs cover — when in doubt, check pnpm-workspace.yaml for the pinned version.
This skill captures the PostHog-specific conventions on top of the upstream kea docs. When in doubt about a builder's signature, go upstream. When in doubt about whether to use it, read here.
*Logic.ts / *Logic.tsx filereducer vs selector vs cache vs loader for a piece of statesetInterval, addEventListener,and any other resource that needs cleanup.
tabId, tabAwareUrlToAction/tabAwareActionToUrl, useAttachedLogic.
If your work overlaps either of those, read the companion skill first.
CLAUDE.md is explicit: "If there is a kea logic file, write all business logic there, avoid React hooks at all costs." Hooks are for view concerns.
derived selector, async loader. Don't mirror the same value into multiple places.
subscription that re-runs on every dispatch and is measurably slower. Listen to the action that changed the value instead. See references/reacting-to-changes.md.
*LogicType.ts next to it. Import with import type and pass it as the kea type parameter. Never edit the generated file.
using-kea-disposables skill.
import { actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import type { fooLogicType } from './fooLogicType'
export interface FooLogicProps {
fooId: string
}
export const fooLogic = kea<fooLogicType>([
props({} as FooLogicProps),
key((props) => props.fooId),
path((key) => ['scenes', 'foo', 'fooLogic', key]),
connect(() => ({ values: [teamLogic, ['currentTeamId']] })),
actions({ setName: (name: string) => ({ name }) }),
loaders(({ props }) => ({
foo: [null as Foo | null, { loadFoo: async () => api.foos.get(props.fooId) }],
})),
reducers({ name: ['', { setName: (_, { name }) => name }] }),
selectors({ nameUpper: [(s) => [s.name], (name): string => name.toUpperCase()] }),
listeners(({ actions }) => ({
loadFooSuccess: () => {
/* ... */
},
})),
afterMount(({ actions }) => {
actions.loadFoo()
}),
])Conventional block order: props → key → path → connect → actions → forms → loaders → reducers → selectors → sharedListeners → listeners → subscriptions (rare) → windowValues → urlToAction / actionToUrl → afterMount / propsChanged / beforeUnmount.
You almost never need all of those — half a dozen blocks is typical. Pick the ones the logic actually uses and leave the rest out.
Most kea bugs come from picking the wrong container for a piece of state. Work through this before reaching for any builder:
selector.Use a reducer.
cache.disposables— see using-kea-disposables.
cache.foo is an escape hatch for transient flags the UI never reads — reach for it last, not first. See references/state-decision.md for the full breakdown.
Each reference covers one job-to-be-done with the pattern shape, why it's the right shape, and the trade-offs. File citations inside references are "examples in the wild today" — they age, so the pattern itself is the source of truth.
| You want to... | Read |
|---|---|
| Decide between reducer / selector / cache / loader | references/state-decision.md |
| Load data from the API | references/loading-data.md |
| Poll an endpoint or refresh on an interval | references/polling.md |
| Build a form | references/forms.md |
| Sync state with the URL | references/routing.md |
| Persist state across reloads | references/persisting-state.md |
| React to a value change | references/reacting-to-changes.md |
| Have multiple instances of one logic | references/keyed-logics.md |
| Share state across logics or a component subtree | references/connecting-logics.md |
| Test the logic | references/testing.md |
| Recognise a pattern you should convert on sight | references/anti-patterns.md |
import type { fooLogicType } from './fooLogicType'
export const fooLogic = kea<fooLogicType>([...])Generated *LogicType.ts files are produced by kea-typegen (we use the 3.6.2-leakfix.x private fork). Commands:
pnpm --filter=@posthog/frontend typegen:watch — watch mode while writing logicspnpm --filter=@posthog/frontend typegen:write — one-shot writepnpm --filter=@posthog/frontend typegen:check — CI parity checkFull typegen + tsc --noEmit over the whole codebase is slow. When you're iterating on a single logic, scope both to that file:
# Regenerate the type for one logic
pnpm --filter=@posthog/frontend exec kea-typegen write \
-f frontend/src/scenes/foo/fooLogic.ts -r ./frontend/src
# Type-check just that file (and its imports — fast, but won't catch downstream
# breakage in other files that consume the new types)
pnpm --filter=@posthog/frontend exec tsc --noEmit \
frontend/src/scenes/foo/fooLogic.tsUse this loop while writing the logic; run the full typegen:check / typescript:check once at the end to confirm nothing else broke.
Never edit a *LogicType.ts file by hand — change the logic and re-run typegen.
For keyed logics, annotate the export explicitly: export const fooLogic: LogicWrapper<fooLogicType> = kea<fooLogicType>([...]).
*Logic.ts — there are hundreds of workingexamples and the conventions are stable.
~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.