Architectural Invariant Design: KMP Compose Main-Actor Thread Affinity & Dispatcher Boundary Enforcement
KMP Compose Main-Actor Thread Affinity & Dispatcher Boundary Enforcement defines an industrial-grade behavioral standard for mobile engineering. Enforces strict compile-time and runtime isolation boundaries between native platform dispatchers and shared Compose Multiplatform UI trees. Prevents cross-thread UI mutations, iOS Darwin main queue deadlocks, and background freeze panics caused by leaking non-Main dispatchers into ViewModel state holders. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
Hazard Analysis: What Breaks Without This Rule
The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:
| Failure Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Dispatching Mutable UI State Updates on Non-Main Dispatchers Across iOS and Desktop Targets | Critical | Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production. Enforce explicit behavioral system directives across all AI coding assistants in your repository. |
| Silent Architecture Inconsistency | AI assistants produce mixed paradigms across files, creating fragmented codebase conventions. | Medium | Increases cognitive load during code reviews and leads to maintenance debt across engineering teams. Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS collect and assign Compose-consumed StateFlow and SharedFlow instances on Dispatchers.Main.immediate.
Engineers MUST ALWAYS collect and assign Compose-consumed StateFlow and SharedFlow instances on Dispatchers.Main.immediate.
Engineers MUST NOT expose MutableStateFlow or MutableState instances directly outside the boundary of the owning ViewModel or StateHolder.
Engineers MUST NOT expose MutableStateFlow or MutableState instances directly outside the boundary of the owning ViewModel or StateHolder.
Engineers MUST ALWAYS isolate platform-native callbacks (e.g., Darwin GCD queues, CoreLocation, Android BroadcastReceivers) into structured coroutine channels or flows routed through Dispatchers.Default or Dispatchers.IO before re-dispatching to Dispatchers.Main.immediate.
Engineers MUST ALWAYS isolate platform-native callbacks (e.g., Darwin GCD queues, CoreLocation, Android BroadcastReceivers) into structured coroutine channels or flows routed through Dispatchers.Default or Dispatchers.IO before re-dispatching to Dispatchers.Main.immediate.
ViewModels MUST NEVER execute state mutations on Dispatchers.IO or Dispatchers.Default directly
ViewModels MUST NEVER execute state mutations on Dispatchers.IO or Dispatchers.Default directly; data loading may occur on background dispatchers, but final reduction into UI state MUST execute under Dispatchers.Main.immediate.
Code SHOULD leverage context-preserving operators like flowOn() and make explicit use of CoroutineScope pinned to Dispatchers.Main.immediate for UI-bound scopes.
Code SHOULD leverage context-preserving operators like flowOn() and make explicit use of CoroutineScope pinned to Dispatchers.Main.immediate for UI-bound scopes.
Components NEVER trigger side-effects from Compose UI compositions without binding to LaunchedEffect or rememberCoroutineScope with explicit Main dispatcher semantics.
Components NEVER trigger side-effects from Compose UI compositions without binding to LaunchedEffect or rememberCoroutineScope with explicit Main dispatcher semantics.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is KMP Compose Main-Actor Thread Affinity & Dispatcher Boundary Enforcement critical for AI coding assistants?
Enforces strict compile-time and runtime isolation boundaries between native platform dispatchers and shared Compose Multiplatform UI trees. Prevents cross-thread UI mutations, iOS Darwin main queue deadlocks, and background freeze panics caused by leaking non-Main dispatchers into ViewModel state holders. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.
Q:How does adopting this rule prevent AI hallucinations?
This rule explicitly declares what the AI must avoid (Dispatching Mutable UI State Updates on Non-Main Dispatchers Across iOS and Desktop Targets) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.
Q:Which AI coding environments are supported?
This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).
Q:Can I combine this rule with other technology stacks?
Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.