Architectural Invariant Design: Kotlin Multiplatform 2.1 Atomic State Machine & Darwin/Android Dispatcher Boundary
Kotlin Multiplatform 2.1 Atomic State Machine & Darwin/Android Dispatcher Boundary defines an industrial-grade behavioral standard for mobile engineering. Enforces strict confinement of state machine mutative transitions to dedicated single-threaded dispatcher boundaries while using atomic primitives and immutable snapshots across Android ART and Apple Darwin GCD runtimes. Precludes invalid memory access, cross-thread mutation crashes (SIGSEGV/IllegalState), and coroutine deadlocks when synchronizing offline-first persistent states across platform boundaries in Kotlin Multiplatform 2.1. 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 | Unconfined MutableStateFlow Mutations Across Native GCD Worker Threads and ART Thread Pools | 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 isolate state transitions inside a single-threaded linearized actor or dedicated single-threaded CoroutineDispatcher (`newSingleThreadContext` or serial `CoroutineScope`) to guarantee sequential consistency across platforms.
Engineers MUST ALWAYS isolate state transitions inside a single-threaded linearized actor or dedicated single-threaded CoroutineDispatcher (`newSingleThreadContext` or serial `CoroutineScope`) to guarantee sequential consistency across platforms.
Engineers MUST NOT expose `MutableStateFlow` directly across public architectural boundaries
Engineers MUST NOT expose `MutableStateFlow` directly across public architectural boundaries; exposure MUST be strictly as read-only `StateFlow<T>` yielding deeply immutable data classes.
State data models MUST be pure Kotlin immutable data classes where all nested members are declared with `val` and use persistent collections (e.g., `kotlinx.collections.immutable`) instead of standard mutable collections.
State data models MUST be pure Kotlin immutable data classes where all nested members are declared with `val` and use persistent collections (e.g., `kotlinx.collections.immutable`) instead of standard mutable collections.
Darwin interoperability code MUST ALWAYS observe state streams via a designated `MainCoroutineDispatcher` or platform-safe adapter that bridges Darwin's `dispatch_queue_main_t` without capturing mutable state across GCD concurrent queues.
Darwin interoperability code MUST ALWAYS observe state streams via a designated `MainCoroutineDispatcher` or platform-safe adapter that bridges Darwin's `dispatch_queue_main_t` without capturing mutable state across GCD concurrent queues.
Engineers MUST NEVER execute database I/O, heavy disk persistence, or network synchronization on the state machine's serial transition dispatcher
Engineers MUST NEVER execute database I/O, heavy disk persistence, or network synchronization on the state machine's serial transition dispatcher; offload strictly via `withContext(Dispatchers.IO)`.
Mutations to atomic flags and references MUST use `kotlinx.atomicfu.atomic` or `kotlin.concurrent.AtomicReference` with thread-safe compare-and-swap (CAS) loops when outside coroutine contexts.
Mutations to atomic flags and references MUST use `kotlinx.atomicfu.atomic` or `kotlin.concurrent.AtomicReference` with thread-safe compare-and-swap (CAS) loops when outside coroutine contexts.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Kotlin Multiplatform 2.1 Atomic State Machine & Darwin/Android Dispatcher Boundary critical for AI coding assistants?
Enforces strict confinement of state machine mutative transitions to dedicated single-threaded dispatcher boundaries while using atomic primitives and immutable snapshots across Android ART and Apple Darwin GCD runtimes. Precludes invalid memory access, cross-thread mutation crashes (SIGSEGV/IllegalState), and coroutine deadlocks when synchronizing offline-first persistent states across platform boundaries in Kotlin Multiplatform 2.1. 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 (Unconfined MutableStateFlow Mutations Across Native GCD Worker Threads and ART Thread Pools) 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.