Architectural Invariant Design: Kotlin Native & Swift ARC Interop Boundary Memory Isolation
Kotlin Native & Swift ARC Interop Boundary Memory Isolation defines an industrial-grade behavioral standard for mobile engineering. Enforces strict isolation between Swift Automatic Reference Counting (ARC) and Kotlin/Native tracing Garbage Collection (GC) across the Objective-C/Swift-Export ABI. Prohibits cross-runtime cyclic retain paths where Kotlin objects hold strong references to Swift instances implementing exported interfaces without deterministic lifecycle detachment. 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 | Cross-Runtime Cyclic Retain Graph Causing Undetectable Native Memory Leaks | 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 invalidate listeners, observers, and coroutine scopes crossing the Swift/Kotlin boundary during Swift lifecycle deinitialization (e.g., UIViewController.viewDidDisappear, SwiftUI .onDisappear, or deinit).
Engineers MUST ALWAYS invalidate listeners, observers, and coroutine scopes crossing the Swift/Kotlin boundary during Swift lifecycle deinitialization (e.g., UIViewController.viewDidDisappear, SwiftUI .onDisappear, or deinit).
Engineers MUST NOT pass Swift closures or delegates capturing strong references to Swift actors/classes into long-lived Kotlin singletons or retained state holders.
Engineers MUST NOT pass Swift closures or delegates capturing strong references to Swift actors/classes into long-lived Kotlin singletons or retained state holders.
Code SHOULD utilize unidirectional Kotlin StateFlow/SharedFlow wrapped in SKIE or Swift-Export AsyncSequences rather than bidirectional observer interfaces.
Code SHOULD utilize unidirectional Kotlin StateFlow/SharedFlow wrapped in SKIE or Swift-Export AsyncSequences rather than bidirectional observer interfaces.
Services MUST provide an explicit `detach()`, `dispose()`, or `close()` method in Kotlin interfaces exported to Swift when bridging non-ephemeral instances.
Services MUST provide an explicit `detach()`, `dispose()`, or `close()` method in Kotlin interfaces exported to Swift when bridging non-ephemeral instances.
Components NEVER store raw Swift protocol implementations in long-lived Kotlin coroutine context elements or collection fields without a WeakReference wrapper.
Components NEVER store raw Swift protocol implementations in long-lived Kotlin coroutine context elements or collection fields without a WeakReference wrapper.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Kotlin Native & Swift ARC Interop Boundary Memory Isolation critical for AI coding assistants?
Enforces strict isolation between Swift Automatic Reference Counting (ARC) and Kotlin/Native tracing Garbage Collection (GC) across the Objective-C/Swift-Export ABI. Prohibits cross-runtime cyclic retain paths where Kotlin objects hold strong references to Swift instances implementing exported interfaces without deterministic lifecycle detachment. 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 (Cross-Runtime Cyclic Retain Graph Causing Undetectable Native Memory Leaks) 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.