Architectural Invariant Design: Kotlin Multiplatform Compose Surface Lifecycle and Skiko Hardware Context Sync
Kotlin Multiplatform Compose Surface Lifecycle and Skiko Hardware Context Sync defines an industrial-grade behavioral standard for mobile engineering. Enforces strict ordering and lifecycle synchronization between UI surface transitions and underlying Skiko GPU contexts (Vulkan on Android, Metal on iOS). Prevents deadlocks, GPU context abandonment, and SIGSEGV memory faults when an application transitions between foreground, background, and configuration changes during Compose multiplatform rendering. 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 | Accessing or reallocating Skiko render targets asynchronously during platform surface invalidation or backgrounding | 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 suspend render dispatches and synchronously flush Skiko command buffers before yielding to native platform surface destruction callbacks (e.g., surfaceDestroyed on Android, viewWillDisappear/applicationDidEnterBackground on iOS).
Engineers MUST ALWAYS suspend render dispatches and synchronously flush Skiko command buffers before yielding to native platform surface destruction callbacks (e.g., surfaceDestroyed on Android, viewWillDisappear/applicationDidEnterBackground on iOS).
Engineers MUST NOT acquire or reuse Direct3D, Metal, or Vulkan Skia DirectContexts across different surface lifecycle generations.
Engineers MUST NOT acquire or reuse Direct3D, Metal, or Vulkan Skia DirectContexts across different surface lifecycle generations.
Code SHOULD decouple high-frequency Compose state mutations from immediate native canvas redraws by utilizing disciplined frame synchronization signals (Choreographer / CADisplayLink).
Code SHOULD decouple high-frequency Compose state mutations from immediate native canvas redraws by utilizing disciplined frame synchronization signals (Choreographer / CADisplayLink).
Native interop delegates MUST wrap Skia/Skiko canvas draw passes with thread-safe guards verifying the native surface handle's validity (e.g., ANativeWindow, CAMetalLayer) before issuing drawing commands.
Native interop delegates MUST wrap Skia/Skiko canvas draw passes with thread-safe guards verifying the native surface handle's validity (e.g., ANativeWindow, CAMetalLayer) before issuing drawing commands.
Components NEVER dispatch asynchronous background work to a Skiko Skia context after the backing surface has triggered its detachment hook.
Components NEVER dispatch asynchronous background work to a Skiko Skia context after the backing surface has triggered its detachment hook.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Kotlin Multiplatform Compose Surface Lifecycle and Skiko Hardware Context Sync critical for AI coding assistants?
Enforces strict ordering and lifecycle synchronization between UI surface transitions and underlying Skiko GPU contexts (Vulkan on Android, Metal on iOS). Prevents deadlocks, GPU context abandonment, and SIGSEGV memory faults when an application transitions between foreground, background, and configuration changes during Compose multiplatform rendering. 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 (Accessing or reallocating Skiko render targets asynchronously during platform surface invalidation or backgrounding) 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.