Architectural Invariant Design: KMP 2.1 Coroutine Confinement and Cross-Platform Memory Boundary Enforcement
KMP 2.1 Coroutine Confinement and Cross-Platform Memory Boundary Enforcement defines an industrial-grade behavioral standard for mobile engineering. Enforces strict thread-isolated mutable state boundaries across Darwin Grand Central Dispatch (GCD) queues and Android thread pools in Kotlin Multiplatform 2.1+. Prevents multi-threaded native memory leaks, unconfined Obj-C/Kotlin retain cycles, and cross-thread access race conditions by mandating serial dispatcher confinement and immutable boundary projection. 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 | Mutating Shared State Across Unconfined Dispatchers and Leaking Native Object References Through Coroutine Captures | 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 mutable business logic and repository state to a dedicated serial dispatcher using Dispatchers.Default.limitedParallelism(1) or an explicit Mutex.
Engineers MUST ALWAYS isolate mutable business logic and repository state to a dedicated serial dispatcher using Dispatchers.Default.limitedParallelism(1) or an explicit Mutex.
Engineers MUST NOT expose MutableStateFlow, MutableSharedFlow, or mutable collections across the common-to-native boundary
Engineers MUST NOT expose MutableStateFlow, MutableSharedFlow, or mutable collections across the common-to-native boundary; only StateFlow or immutable snapshots are permitted.
Code SHOULD enforce strict lifecycle teardown by tying cross-platform CoroutineScopes to explicit cancellation triggers (e.g., Darwin dealloc bridges or Android ViewModel.onCleared).
Code SHOULD enforce strict lifecycle teardown by tying cross-platform CoroutineScopes to explicit cancellation triggers (e.g., Darwin dealloc bridges or Android ViewModel.onCleared).
Services MUST NOT capture Darwin platform native handles (such as pointers, platform contexts, or NSObject callbacks) directly inside long-lived Dispatchers.Default coroutines without weak or detached reference wrappers.
Services MUST NOT capture Darwin platform native handles (such as pointers, platform contexts, or NSObject callbacks) directly inside long-lived Dispatchers.Default coroutines without weak or detached reference wrappers.
Components NEVER bridge Darwin completion handlers into GlobalScope or non-confined Dispatchers.Unconfined, which causes ARC object deallocation on arbitrary native worker threads.
Components NEVER bridge Darwin completion handlers into GlobalScope or non-confined Dispatchers.Unconfined, which causes ARC object deallocation on arbitrary native worker threads.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is KMP 2.1 Coroutine Confinement and Cross-Platform Memory Boundary Enforcement critical for AI coding assistants?
Enforces strict thread-isolated mutable state boundaries across Darwin Grand Central Dispatch (GCD) queues and Android thread pools in Kotlin Multiplatform 2.1+. Prevents multi-threaded native memory leaks, unconfined Obj-C/Kotlin retain cycles, and cross-thread access race conditions by mandating serial dispatcher confinement and immutable boundary projection. 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 (Mutating Shared State Across Unconfined Dispatchers and Leaking Native Object References Through Coroutine Captures) 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.