Logo vun Understanding Stability Inference

Understanding Stability Inference

Communautéit
skydoves
understanding-stability-inference

Use this skill to explain why the Compose compiler classified a class or composable parameter as stable, runtime, unknown, or unstable. Covers the 12-phase inference algorithm, the five compiler-level stability types (Certain / Runtime / Unknown / Parameter / Combined), the generic bitmask encoding (Pair=0b11, ImmutableList=0b1), the Known Stable Constructs registry, and the runtime `$stable: Int` field generated by `@StabilityInferred`. Use when the developer asks "why is X classified as Y?", when a stability report shows a surprising `runtime stable`, `unknown`, or `unstable` verdict, when generics, inheritance, cycles, interfaces, or cross-module classes are involved, or when the user mentions `$stable`, `@StabilityInferred`, separate compilation, or "the compiler thinks my class is unstable but it looks fine".

Iwwersiicht

Editeurskydoves
Repositorycompose-performance-skills
Skill-Nummunderstanding-stability-inference
Stären
515
Forks
19
Matgeliwwert Fichieren
2
LizenzApache-2.0
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 2 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    Vum skydoves op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Understanding Stability Inference an TypingMind, fir e mat all LLM ze benotzen, oder füügt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vun GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier hei drënner an a bestätegt.
    https://github.com/skydoves/compose-performance-skills/tree/main/stability/understanding-stability-inference
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill afügen → Vu GitHub-URL, pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an de Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/skydoves/compose-performance-skills.git /tmp/compose-performance-skills
mkdir -p .claude/skills
cp -r /tmp/compose-performance-skills/stability/understanding-stability-inference .claude/skills/understanding-stability-inference
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Understanding Stability Inference an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Understanding Stability Inference automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Understanding Stability Inference net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Keys.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

Understanding Stability Inference — read the compiler's mind

Stability is decided by a 12-phase algorithm baked into the Compose compiler. This skill teaches Claude how the algorithm walks a type so it can explain why a report says what it says, and predict classifications before the report is even generated. Pair this with ../diagnosing-compose-stability/SKILL.md (which generates the report) and ../stabilizing-compose-types/SKILL.md (which fixes obvious unstable types). Reach for this skill when the simpler skills produce a verdict that surprises the developer.

When to use this skill

  • The developer asks "why is Foo classified as runtime stable and not stable?"
  • A report shows runtime or unknown for a class that "looks fine".
  • Generics involved: Box<T>, Wrapper<A, B>, Pair<String, Int>, ImmutableList<User>.
  • The class lives in another module and ships as a .class/.kotlin_metadata artifact.
  • The developer asks about the $stable: Int field, @StabilityInferred, or cross-module classification.
  • A self-referential type (class Node(val children: List<Node>)) is unstable for non-obvious reasons.
  • A Java type, an interface, or an abstract base appears in a parameter list and surprises the developer.

When NOT to use this skill

  • The fix is mechanical (var → val, List → ImmutableList, Flow parameter removal). Use ../stabilizing-compose-types/SKILL.md.
  • No report exists yet. Run ../diagnosing-compose-stability/SKILL.md first.
  • The developer wants CI enforcement of stability. Use ../enforcing-stability-in-ci/SKILL.md.

Prerequisites

  • Compose Compiler reports already generated, or at least one <module>-classes.txt and <module>-composables.txt available in build/compose_compiler/.
  • The developer understands the basic stability vocabulary: stable, unstable, skippable, restartable, @Stable, @Immutable.
  • Kotlin 2.0+ with the Compose compiler plugin (Strong Skipping default ON).

Workflow — diagnostic question and answer tree

Walk the type through the same 12 phases the compiler does. For each call site, ask the questions in order; the first matching phase wins.

The canonical phase order (used everywhere in this skill and matching references/twelve-phase-algorithm.md):

  1. Phase 1 — primitive / String / function / Unit fast path → Stable. The compiler returns immediately. No field analysis runs. Mention the fast path so the developer knows nothing else was inspected.
  2. Phase 2 — type parameter substitution. A bare type variable T becomes Stability.Parameter(T); resolution is deferred to the call site that substitutes it.
  3. Phase 3 — nullable unwrap (Int? → analyze Int). Nullability does not change stability; the algorithm strips the ? and recurses.
  4. Phase 4 — inline class — check underlying type. value class Wrapper(val raw: T) is exactly as stable as T.
  5. Phase 5 — cycle detection (recursive trees → conservative UNSTABLE). The algorithm bails on cycles to guarantee termination. The escape hatch is @Stable/@Immutable on the recursive class, which fires in phase 6 before phase 5 is reached.
  6. Phase 6 — annotations check (@Stable, @Immutable, @StableMarker). Yes → Stability.Certain (stable). @Immutable enables additional optimizations beyond @Stable because the compiler can promote reads of properties to static expressions and elide equality checks; @Stable only promises change notification.
  7. Phase 7 — Known Stable Constructs registry hit (Pair / Triple / Result / ImmutableList / dagger.Lazy / ClosedRange / etc.). Returns Stability.Parameter with the registry's bitmask. See references/bitmask-encoding.md for the full registry.
  8. Phase 8 — external configuration match (stability_config.conf). Returns Stability.Parameter with the bitmask declared in the config file.
  9. Phase 9 — external module (@StabilityInferred annotation generated by separate compilation). Returns Stability.Runtime. The compiler emits a $stable: Int field on the JVM (a mangled top-level property on Native/JS) that the runtime queries via Composer.changed. Tell the developer this is not a bug — runtime is the compiler saying "I cannot prove this at compile time, so I will check at runtime".
  10. Phase 10 — Java type (default UNSTABLE — fix via config file). Java final fields look like var to the inference because the algorithm has no Kotlin metadata to read. Fix via stabilityConfigurationFiles, not by editing the Java source.
  11. Phase 11 — interface (UNKNOWN; runtime ===). The compiler cannot enumerate implementations from a single call site; the runtime falls back to identity (===) for the equality probe.
  12. Phase 12 — field-by-field analysis (the slow path):
    • Walks the linearized class hierarchy so inherited fields participate. There is no separate "inheritance" phase — inheritance lives here.
    • Any var property → Unstable (mutation observed without Snapshot integration).
    • Any property whose type is Unstable → Unstable (Combined dominates).
    • Otherwise the class is Stable (Combined of all-stable fields collapses to Stable).

For full pseudocode of all 12 phases plus the field-by-field loop, see references/twelve-phase-algorithm.md.

Patterns

Pattern: "Why does Box<String> show as runtime stable?"

kotlin
// Source — the developer's class, in a library module
class Box<T>(val value: T)

// Call site, in app module
@Composable fun BoxRow(box: Box<String>) { Text(box.value) }

The compiler walks Box<T>:

  1. Phase 12 (field-by-field) inside the defining module finds one field value: T; recursion on T hits phase 2 (type parameter) → Stability.Parameter. Combined collapses to Stability.Parameter with bitmask 0b1 (the single type parameter affects stability).
  2. Because Box is consumed from a different module, the compiler emits @StabilityInferred(parameters = 0b1) on Box and a $stable: Int field initialized from T's stability at runtime. Downstream call sites pick this up via phase 9.
  3. Call-site substitution → T = String → String is Certain Stable → bit 0 satisfied.
  4. Final report line: runtime stable class Box<T> and at the call site BoxRow is skippable.
kotlin
// WRONG mental model
// "runtime stable means there is a runtime cost on every recomposition" — partly true but misleading
// WRONG because: the cost is one Int field load and a bitwise AND, performed once when the runtime
// computes the call-site stability. It is far cheaper than the unskipped recomposition it prevents.
kotlin
// RIGHT mental model
// runtime stable = "the compiler proved stability conditional on the type arguments, and emitted
// a $stable: Int field whose bits the runtime ANDs against the substituted argument stabilities".
// The skip decision is still made; it is just made at runtime instead of compile time.

Pattern: "Why does @Immutable data class Person(val name: String) enable more optimizations than @Stable?"

@Stable is a contract: "I will notify Compose of changes". @Immutable is a stronger contract: "I will never change". With @Immutable the compiler may promote reads of Person.name to static expressions and elide equality probes for nested usages; with @Stable it must still emit equality checks. Both classify as Stability.Certain, but the downstream optimizer treats @Immutable more aggressively.

kotlin
// WRONG
@Stable data class Coordinates(val lat: Double, val lng: Double)
// WRONG because: Coordinates never mutates after construction. @Stable understates the contract
// and forfeits static-expression promotion at every read site.
kotlin
// RIGHT
@Immutable data class Coordinates(val lat: Double, val lng: Double)

Pattern: "Why is my recursive tree unstable even though it looks fine?"

kotlin
data class Node(val id: String, val children: List<Node>)

Phase 5 (cycle detection) bails. The compiler does not attempt fixed-point analysis because it would have to assume the answer to prove the answer. The conservative verdict is Unstable. Even if every field is otherwise stable, the recursion through children returns Unstable to the parent call.

kotlin
// WRONG — adds @Stable to "force" stability
@Stable data class Node(val id: String, val children: List<Node>)
// WRONG because: List<Node> is a mutable interface backed by ArrayList in practice. The @Stable
// annotation tells the compiler to trust the contract, but the actual List instance can mutate
// between recompositions without notifying Compose, producing silent missed recompositions.
kotlin
// RIGHT
import kotlinx.collections.immutable.ImmutableList
@Immutable data class Node(val id: String, val children: ImmutableList<Node>)

ImmutableList is in the Known Stable Constructs registry (phase 7) with bitmask 0b1, so the recursion through children is permitted: cycle detection still triggers in phase 5, but the registry hit short-circuits the conservative verdict.

Pattern: "Why does Set<String> block skipping but ImmutableSet<String> doesn't?"

kotlin.collections.Set is an interface (phase 11 → Unknown) backed in practice by LinkedHashSet, which mutates. kotlinx.collections.immutable.ImmutableSet is in the Known Stable Constructs registry with bitmask 0b1, so it is Stability.Parameter and resolves to stable when the element type is stable.

kotlin
// WRONG
@Composable fun TagRow(tags: Set<String>) { /* ... */ }
// WRONG because: Set is an interface — phase 11 returns Unknown, the call site is non-skippable.
kotlin
// RIGHT
@Composable fun TagRow(tags: ImmutableSet<String>) { /* ... */ }

Pattern: "Why is a class from another module runtime stable even when it has only vals?"

Separate compilation. At the time the call site compiles, the compiler does not have the full source AST of the dependency, only its .class files plus the metadata in @StabilityInferred(parameters = ...). Phase 9 reads that annotation; the runtime resolves the bitmask against actual type arguments via the generated $stable: Int field. The classification is correct — there is no extra work to do — but it must be deferred to runtime because cross-module compile-time analysis is impossible without the source.

Five compiler-level stability types

Cite these by name when answering "why" questions. The compiler stores stability as one of:

  • Stability.Certain — primitives, String, Unit, function types, enums, @Stable/@Immutable-annotated classes. Decision is final and compile-time.
  • Stability.Runtime — separately compiled class. Compile-time emits a $stable: Int field and @StabilityInferred; runtime ANDs the bits against actual type arguments.
  • Stability.Unknown — interface, abstract class without concrete analysis, or anything the compiler refuses to commit on. Runtime falls back to === identity for the equality probe.
  • Stability.Parameter — generic. Stability is a function of the type arguments via a bitmask.
  • Stability.Combined — aggregate of multiple components (fields of a class, or multiple type arguments). Unstable dominates — any single Unstable component poisons the whole.

Bitmask encoding (preview)

Container<T1, T2, T3> uses an Int bitmask where bit i set means Ti participates in stability:

TypeBitmaskReading
kotlin.Pair<A, B>0b11both A and B affect stability
kotlin.Triple<A, B, C>0b111all three affect stability
kotlinx.collections.immutable.ImmutableList<E>0b1only E affects stability
java.math.BigInteger0b0no parameters; classified as stable regardless of erased type arguments

The full rules — including how @StabilityInferred(parameters = ...) is generated for separately-compiled types and how the $stable: Int field is laid out on the JVM versus the mangled top-level property used on Kotlin/Native and Kotlin/JS — are in references/bitmask-encoding.md.

Mandatory rules

  • MUST teach the developer that runtime stable is not a bug or an unstable verdict — it is the compiler's way of saying "stability is conditional on type arguments and will be checked once at runtime via the $stable: Int field".
  • MUST NOT suggest structural changes (changing var to val, swapping collection types) before explaining why the current structure is unstable. Diagnosis before treatment.
  • MUST distinguish Stability.Unknown from Stability.Unstable when answering — Unknown means "cannot tell" and falls back to identity equality, Unstable means "proven unstable" and disables skipping outright.
  • MUST NOT tell the developer to add @Stable to a type whose contract they cannot guarantee. A stability annotation is a contract; breaking it produces silent missed recompositions, which is worse than a non-skippable composable.
  • PREFERRED: cite the Kotlin compiler source when depth helps. Concrete files: Stability.kt (the algebraic data type), KnownStableConstructs.kt (the registry), ClassStabilityTransformer.kt (the $stable field emission), and StabilityConfigParser.kt (the config-file reader).
  • PREFERRED: when explaining a generic, walk the bitmask explicitly: "bit 0 of Pair's bitmask is set, A=String is Certain Stable, satisfied; bit 1 is set, B=List is Unstable, fails — Combined collapses to Unstable".

Verification

  • Claude can predict, before running the report, whether a candidate type will be classified Certain, Runtime, Unknown, Parameter, or Combined.
  • Claude can name which of the 12 phases produced the verdict.
  • For a runtime stable class, Claude can explain that the compiler emitted @StabilityInferred(parameters = ...) on the class declaration and a $stable: Int field that the runtime ANDs against substituted type-argument stabilities.
  • Claude refuses to recommend @Stable or @Immutable on a type whose mutation contract is not guaranteed.
  • Claude correctly identifies Set<T>, List<T>, Map<K, V> as Unknown interfaces (not Unstable) when explaining why they block skipping.

References

Reference files

  • references/twelve-phase-algorithm.md — pseudocode walkthrough of all 12 phases plus the field-by-field analysis pseudocode.
  • references/bitmask-encoding.md — generic stability bitmask rules, the Known Stable Constructs registry, the @StabilityInferred(parameters = 0b1) annotation generated by the compiler, the runtime $stable: Int field on JVM, and the mangled top-level property approach on Native and JS.

Matgeliwwert Fichieren

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Understanding Stability Inference?

Use this skill to explain why the Compose compiler classified a class or composable parameter as stable, runtime, unknown, or unstable. Covers the 12-phase inference algorithm, the five compiler-level stability types (Certain / Runtime / Unknown / Parameter / Combined), the generic bitmask encoding (Pair=0b11, ImmutableList=0b1), the Known Stable Constructs registry, and the runtime `$stable: Int` field generated by `@StabilityInferred`. Use when the developer asks "why is X classified as Y?", when a stability report shows a surprising `runtime stable`, `unknown`, or `unstable` verdict, whe...

Firwat Understanding Stability Inference op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Understanding Stability Inference besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt amplaz vun engem Abonnement pro Benotzerplaz. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Understanding Stability Inference an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/skydoves/compose-performance-skills/tree/main/stability/understanding-stability-inference an. TypingMind liest d'Datei SKILL.md, bündelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Understanding Stability Inference benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Understanding Stability Inference benotzen?

Esou vill Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Understanding Stability Inference domat benotzen – GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama a méi – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Understanding Stability Inference gratis?

Jo. De Skill gëtt vum skydoves ënner der Lizenz Apache-2.0 op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbaart Pak vun Instruktiounen, dat engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt en nokënnt a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a keng Lafzäitëmfeld a funktionéiere mat all Modell.

All weisen

Elo Ären eegene KI-Workspace ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇