Infer
How kern review reverse-compiles TypeScript to KERN IR for analysis and LLM review.
What it does
The inferrer is the first phase of kern review. It scans a TypeScript AST (via ts-morph) and converts every declaration into a KERN IR node with a stable ID, source span, confidence score, and token count. The resulting IR is 3–5x smaller than the original TypeScript — small enough to send to an LLM in a single prompt.
Inference runs automatically when you use kern review. You can see the raw output with the --export-kern flag:
kern review src/auth.ts --export-kernTwo phases
Phase 1 (deterministic) matches individual declarations. Phase 2 (composite) detects higher-level patterns across multiple declarations.
Phase 1 — Deterministic matchers
Each matcher scans the TypeScript AST for a specific declaration kind and emits a KERN IR node:
| Matcher | Detects | Confidence |
|---|---|---|
| inferTypes | Type aliases, string literal unions | 95–98% |
| inferInterfaces | Interfaces with fields, extends clauses | 97% |
| inferFunctions | Functions, arrow functions, class methods | 95% |
| inferErrors | Classes extending Error | 96% |
| inferImports | Import declarations (named, default, type-only) | 99% |
| inferConsts | Const declarations (non-function) | 90% |
| inferExports | Re-exports, export default | 95% |
Phase 2 — Composite patterns
Phase 2 looks across Phase 1 results to detect higher-level structures:
- inferMachines — Detects state machines from XState-style patterns: a *State type alias (string literal union) + *StateError class + transition functions
- inferConfigs — Detects configuration patterns: a *Config interface paired with a DEFAULT_*_CONFIG const
- inferEvents — Detects event systems from matching type + handler patterns
Output format
Each inferred node gets a stable nodeId, a sequential prompt alias (N1, N2, ...), source spans pointing back to the original TypeScript, and a confidence percentage. Here is an example of the KERN IR output:
// KERN IR — inferred from TypeScript source
// Send this to AI for structural review (5x smaller than original TS)
// [N1] L1-3 (98% confidence)
type UserRole values=admin|editor|viewer
// [N2] L5-10 (97% confidence)
interface UserConfig
field name=role type=UserRole
field name=timeout type=number optional=true
// [N3] L12-20 (95% confidence)
fn validateUser params=config:UserConfig returns=booleanToken reduction
The inferrer tracks token counts for both the original TypeScript and the KERN IR representation. Typical reduction is 60–80%, meaning a 1000-token TypeScript file becomes 200–400 tokens in KERN IR. This is what makes LLM review practical — you can fit an entire codebase into a single context window.
Language support
The inferrer currently supports TypeScript (.ts, .tsx) files. It uses ts-morph for AST parsing, which handles all TypeScript syntax including generics, decorators, and JSX. Python file review uses a separate analysis pipeline.
Related
- kern review --export-kern — Output raw KERN IR
- kern review --llm — LLM-assisted review using KERN IR
- kern review --fix — Auto-migrate TypeScript to .kern files