Architectural Invariant Design: KMP Bi-Directional Swift Task to Coroutine Cancellation Bridge
KMP Bi-Directional Swift Task to Coroutine Cancellation Bridge defines an industrial-grade behavioral standard for mobile engineering. Enforces deterministic, bi-directional cooperative cancellation bridging between Swift async/await Tasks and Kotlin Coroutine Jobs across the Kotlin/Native boundary. Eliminates orphaned background execution threads, UI memory retention cycles, and phantom network operations when Swift views discard active tasks. 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 | Orphaned Kotlin Coroutine Execution on Swift Task Cancellation | 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 wrap raw Kotlin/Native suspend exports with `withTaskCancellationHandler` in Swift or rely on verified structured concurrency adapters (e.g., SKIE or KMP-NativeCoroutines) that bind Swift cancellation directly to `Job.cancel()`.
Engineers MUST ALWAYS wrap raw Kotlin/Native suspend exports with `withTaskCancellationHandler` in Swift or rely on verified structured concurrency adapters (e.g., SKIE or KMP-NativeCoroutines) that bind Swift cancellation directly to `Job.cancel()`.
Engineers MUST NOT invoke Kotlin suspend functions directly via raw Swift `async` wrappers without binding the Swift cancellation handler to the returned Kotlin `Cancellable` or `Job` handle.
Engineers MUST NOT invoke Kotlin suspend functions directly via raw Swift `async` wrappers without binding the Swift cancellation handler to the returned Kotlin `Cancellable` or `Job` handle.
Kotlin iOS bridge interfaces MUST return a handle exposing `cancel()` or accept a cooperative cancellation token across the Objective-C bridging barrier.
Kotlin iOS bridge interfaces MUST return a handle exposing `cancel()` or accept a cooperative cancellation token across the Objective-C bridging barrier.
Shared Flows exposed to iOS MUST be collected using a lifecycle-aware wrapper that cancels the underlying Kotlin `Job` immediately when the Swift `AsyncSequence` terminates or throws `CancellationError`.
Shared Flows exposed to iOS MUST be collected using a lifecycle-aware wrapper that cancels the underlying Kotlin `Job` immediately when the Swift `AsyncSequence` terminates or throws `CancellationError`.
SwiftUI views MUST NEVER fire-and-forget Kotlin coroutines in `.task` or `.onAppear` without structured cancellation hooks
SwiftUI views MUST NEVER fire-and-forget Kotlin coroutines in `.task` or `.onAppear` without structured cancellation hooks; abandoning a view MUST immediately propagate cancellation down to active CoroutineContext dispatchers.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is KMP Bi-Directional Swift Task to Coroutine Cancellation Bridge critical for AI coding assistants?
Enforces deterministic, bi-directional cooperative cancellation bridging between Swift async/await Tasks and Kotlin Coroutine Jobs across the Kotlin/Native boundary. Eliminates orphaned background execution threads, UI memory retention cycles, and phantom network operations when Swift views discard active tasks. 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 (Orphaned Kotlin Coroutine Execution on Swift Task Cancellation) 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.