Architectural Invariant Design: Kotlin 2.1 SharedFlow & Swift 6 AsyncSequence Sendable Isolation Guardrail
Kotlin 2.1 SharedFlow & Swift 6 AsyncSequence Sendable Isolation Guardrail defines an industrial-grade behavioral standard for mobile engineering. Enforces thread-safe, data-race-free interoperability between Kotlin Multiplatform 2.1 SharedFlow/StateFlow emissions and Swift 6 Complete Concurrency AsyncSequences. Eliminates cross-runtime thread pool exhaustion and cooperative thread starvation deadlocks caused by unmanaged coroutine collectors. Mandates strict Sendable isolation and deterministic cancellation bridging across the Darwin runtime boundary. 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 | Unchecked Cross-Boundary Flow Bridging Causing Swift 6 Isolation Escapes and Cooperative Thread Starvation Deadlocks | 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 bridge Kotlin Flow instances to Swift using an AsyncStream or AsyncThrowingStream that deterministically cancels the underlying Kotlin kotlinx.coroutines.Job within `onTermination`.
Engineers MUST ALWAYS bridge Kotlin Flow instances to Swift using an AsyncStream or AsyncThrowingStream that deterministically cancels the underlying Kotlin kotlinx.coroutines.Job within `onTermination`.
Engineers MUST NOT cross Swift 6 actor boundaries with mutable Kotlin reference classes
Engineers MUST NOT cross Swift 6 actor boundaries with mutable Kotlin reference classes; emitted payload models MUST be immutable, marked `@Sendable` on the Swift wrapper side, or strictly confined to the `@MainActor`.
Code MUST NEVER invoke `runBlocking` or block a Swift cooperative thread awaiting a Kotlin suspend function or Flow emission.
Code MUST NEVER invoke `runBlocking` or block a Swift cooperative thread awaiting a Kotlin suspend function or Flow emission.
Services MUST route UI-bound Flow consumption explicitly through `@MainActor` isolated wrappers, preventing cross-thread mutation of Swift state by Kotlin Darwin workers.
Services MUST route UI-bound Flow consumption explicitly through `@MainActor` isolated wrappers, preventing cross-thread mutation of Swift state by Kotlin Darwin workers.
Components NEVER hold strong unmanaged references to Kotlin CoroutineScope instances inside Swift async contexts without structured lifetime binding.
Components NEVER hold strong unmanaged references to Kotlin CoroutineScope instances inside Swift async contexts without structured lifetime binding.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Kotlin 2.1 SharedFlow & Swift 6 AsyncSequence Sendable Isolation Guardrail critical for AI coding assistants?
Enforces thread-safe, data-race-free interoperability between Kotlin Multiplatform 2.1 SharedFlow/StateFlow emissions and Swift 6 Complete Concurrency AsyncSequences. Eliminates cross-runtime thread pool exhaustion and cooperative thread starvation deadlocks caused by unmanaged coroutine collectors. Mandates strict Sendable isolation and deterministic cancellation bridging across the Darwin runtime boundary. 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 (Unchecked Cross-Boundary Flow Bridging Causing Swift 6 Isolation Escapes and Cooperative Thread Starvation Deadlocks) 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.