Architectural Invariant Design: Compose Multiplatform Retained State & Cross-Process Lifecycle Eviction Contract
Compose Multiplatform Retained State & Cross-Process Lifecycle Eviction Contract defines an industrial-grade behavioral standard for mobile engineering. Enforces deterministic state restoration and native bridge lifecycle synchronization between Android ViewModel/SavedState semantics and iOS UIViewController/Jetsam lifecycles. Prevents silent state loss on Android OS process recreation and eliminates retain cycles between Kotlin/Native GC and Swift ARC under extreme memory pressure. 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 | Retaining In-Memory Compose State Without SavedState Serialization and Leaking Kotlin-Darwin Native Bridges Across View Controller Eviction | 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 serialize critical UI state using `rememberSaveable` with a strict `Saver` or `SavedStateHandle` with `kotlinx.serialization` to survive Android process kills and iOS background eviction.
Engineers MUST ALWAYS serialize critical UI state using `rememberSaveable` with a strict `Saver` or `SavedStateHandle` with `kotlinx.serialization` to survive Android process kills and iOS background eviction.
Engineers MUST NOT capture strong references to native iOS platform objects (such as `UIViewController`, `UIView`, or Darwin delegates) inside Kotlin retained `ViewModel` instances or long-lived coroutine scopes.
Engineers MUST NOT capture strong references to native iOS platform objects (such as `UIViewController`, `UIView`, or Darwin delegates) inside Kotlin retained `ViewModel` instances or long-lived coroutine scopes.
Native iOS bridge adapters MUST implement explicit teardown contracts triggered by `UIViewController.viewDidDisappear` or `deinit` to cancel active coroutine scopes and detach Kotlin/Native root references.
Native iOS bridge adapters MUST implement explicit teardown contracts triggered by `UIViewController.viewDidDisappear` or `deinit` to cancel active coroutine scopes and detach Kotlin/Native root references.
Shared ViewModels bridging to iOS MUST NOT rely exclusively on platform garbage collection
Shared ViewModels bridging to iOS MUST NOT rely exclusively on platform garbage collection; they MUST expose an explicit `clear()` or `dispose()` contract bound to the iOS navigation coordinator.
Components NEVER store unsaved business-critical transactional state purely in Compose `mutableStateOf` without a persistent fallback or saved state restorer.
Components NEVER store unsaved business-critical transactional state purely in Compose `mutableStateOf` without a persistent fallback or saved state restorer.
State restoration logic MUST be verified against both Android Activity recreation (`Don't keep activities` / `am kill`) and iOS memory pressure simulation (`didReceiveMemoryWarning` / task suspension).
State restoration logic MUST be verified against both Android Activity recreation (`Don't keep activities` / `am kill`) and iOS memory pressure simulation (`didReceiveMemoryWarning` / task suspension).
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Compose Multiplatform Retained State & Cross-Process Lifecycle Eviction Contract critical for AI coding assistants?
Enforces deterministic state restoration and native bridge lifecycle synchronization between Android ViewModel/SavedState semantics and iOS UIViewController/Jetsam lifecycles. Prevents silent state loss on Android OS process recreation and eliminates retain cycles between Kotlin/Native GC and Swift ARC under extreme memory pressure. 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 (Retaining In-Memory Compose State Without SavedState Serialization and Leaking Kotlin-Darwin Native Bridges Across View Controller Eviction) 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.