Swift Concurrency Refactoring and Actor Isolation Specification
Generate an actionable architectural specification to refactor legacy iOS asynchronous code into strict Swift Concurrency and Actors.
Use when modernizing iOS asynchronous codebases to Swift 6 strict concurrency without runtime regressions. Ideal for debugging data races and thread-safety issues during architectural overhauls.
Role: Staff iOS Systems Architect specializing in Swift Concurrency and memory safety.
Context
- Target Module: {{target_module}}
- Current Asynchronous Pattern: {{legacy_concurrency_pattern}}
- Actor Isolation Boundaries: {{actor_isolation_rules}}
- Memory Allocation Ceiling: {{memory_threshold_mb}}
- Minimum iOS Deployment Target: {{ios_deployment_target}}
Task
Draft a comprehensive Swift Concurrency refactoring specification for {{target_module}} that replaces {{legacy_concurrency_pattern}} with structured concurrency, eliminates data races under Swift 6 language mode, and enforces strict actor isolation while maintaining performance within {{memory_threshold_mb}}.
Method
- Analyze {{target_module}} entry points currently bound to {{legacy_concurrency_pattern}} and classify thread hops.
- Map existing state variables to appropriate
@MainActor, global actor, or customactorboundaries following {{actor_isolation_rules}}. - Identify non-Sendable type leaks across concurrency boundaries and specify
@Sendableconversions or immutable value type replacements. - Design structured task hierarchies using
withTaskGrouporasync letto replace unstructured callbacks and dispatch queues. - Audit cancellation propagation paths to ensure immediate resource teardown and avoid detached task leaks.
- Specify backpressure and rate-limiting patterns for streaming updates using
AsyncStream. - Formulate a memory safety verification protocol to ensure heap usage does not exceed {{memory_threshold_mb}} during burst loads on {{ios_deployment_target}}.
- Define compiler diagnostic flag configurations and runtime Instruments verification strategies.
Constraints
- MUST guarantee zero compiler warnings under Swift 6 strict concurrency checks.
- MUST NOT introduce non-cooperative background thread blocking or
Task.sleepworkarounds. - All async API contracts MUST target {{ios_deployment_target}} without deprecated fallback shims.
- Architectural diagrams and code examples must use idiomatic, production-ready Swift.
Output format
- Section 1: Architectural Overview (Scope, Isolation Topology)
- Section 2: Concurrency Model Transformations (Legacy vs Swift 6 Mappings)
- Section 3: Sendable & Actor Boundary Specification (Type contracts)
- Section 4: Memory & Performance Budget (Telemetry, Allocation Profile)
- Section 5: Verification & Debugging Plan (Instruments recipes, unit test harnesses)
Self-review
- Verify all legacy patterns from {{legacy_concurrency_pattern}} are fully addressed without dangling closures.
- Confirm actor isolation logic matches {{actor_isolation_rules}} without creating deadlock hazards.
- Ensure memory allocation limits adhere strictly to {{memory_threshold_mb}}.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.