Swift Concurrency Pro

twostraws/Swift-Concurrency-Agent-Skill/swift-concurrency-pro/skills/swift-concurrency-pro

作者 twostrawsbee3f69ba17142da148d3c5406f148ed62592b69MIT563 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫4 個月前更新

Reviews Swift code for concurrency correctness, modern API usage, and common async/await pitfalls. Use when reading, writing, or reviewing Swift concurrency code.

AI 產生的概覽

審查 Swift 並行程式碼的正確性、現代 API 用法以及常見的 async/await 陷阱。

功能
依據一組參考文件審查 Swift 並行程式碼,內容涵蓋 actor、結構化與非結構化任務、取消、非同步串流、橋接、互通性、診斷與測試。它會依檔案整理審查結果,指出所違反的規則並提供簡短的修改前後程式碼對照,最後以依優先順序排列的總結收尾。它鎖定 Swift 6.2 或更新版本並啟用嚴格並行檢查,只回報真正存在的問題。
適用情境
適用於閱讀、撰寫或審查 Swift 並行程式碼時,包括針對某個重點領域的部分審查。也適合檢查舊版並行移轉以及嚴格並行編譯器診斷。
執行需求
不需要指令碼;僅包含說明與可供模型讀取的參考檔案。代理需要能存取該技能的 references 目錄。鎖定 Swift 6.2 或更新版本並啟用嚴格並行檢查。

Review Swift concurrency code for correctness, modern API usage, and adherence to project conventions. Report only genuine problems - do not nitpick or invent issues.

Review process:

  1. Scan for known-dangerous patterns using ${CLAUDE_SKILL_DIR}/references/hotspots.md to prioritize what to inspect.
  2. Check for recent Swift 6.2 concurrency behavior using ${CLAUDE_SKILL_DIR}/references/new-features.md.
  3. Validate actor usage for reentrancy and isolation correctness using ${CLAUDE_SKILL_DIR}/references/actors.md.
  4. Ensure structured concurrency is preferred over unstructured where appropriate using ${CLAUDE_SKILL_DIR}/references/structured.md.
  5. Check unstructured task usage for correctness using ${CLAUDE_SKILL_DIR}/references/unstructured.md.
  6. Verify cancellation is handled correctly using ${CLAUDE_SKILL_DIR}/references/cancellation.md.
  7. Validate async stream and continuation usage using ${CLAUDE_SKILL_DIR}/references/async-streams.md.
  8. Check bridging code between sync and async worlds using ${CLAUDE_SKILL_DIR}/references/bridging.md.
  9. Review any legacy concurrency migrations using ${CLAUDE_SKILL_DIR}/references/interop.md.
  10. Cross-check against common failure modes using ${CLAUDE_SKILL_DIR}/references/bug-patterns.md.
  11. If the project has strict-concurrency errors, map diagnostics to fixes using ${CLAUDE_SKILL_DIR}/references/diagnostics.md.
  12. If reviewing tests, check async test patterns using ${CLAUDE_SKILL_DIR}/references/testing.md.

If doing a partial review, load only the relevant reference files.

Core Instructions

  • Target Swift 6.2 or later with strict concurrency checking.
  • If code spans multiple targets or packages, compare their concurrency build settings before assuming behavior should match.
  • Prefer structured concurrency (task groups) over unstructured (Task {}).
  • Prefer Swift concurrency over Grand Central Dispatch for new code. GCD is still acceptable in low-level code, framework interop, or performance-critical synchronous work where queues and locks are the right tool – don't flag these as errors.
  • If an API offers both async/await and closure-based variants, always prefer async/await.
  • Do not introduce third-party concurrency frameworks without asking first.
  • Do not suggest @unchecked Sendable to fix compiler errors. It silences the diagnostic without fixing the underlying race. Prefer actors, value types, or sending parameters instead. The only legitimate use is for types with internal locking that are provably thread-safe.

Output Format

Organize findings by file. For each issue:

  1. State the file and relevant line(s).
  2. Name the rule being violated.
  3. Show a brief before/after code fix.

Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.

Example output:

DataLoader.swift

Line 18: Actor reentrancy – state may have changed across the await.

swift
// Beforeactor Cache {    var items: [String: Data] = [:]
    func fetch(_ key: String) async throws -> Data {        if items[key] == nil {            items[key] = try await download(key)        }        return items[key]!    }}
// Afteractor Cache {    var items: [String: Data] = [:]
    func fetch(_ key: String) async throws -> Data {        if let existing = items[key] { return existing }        let data = try await download(key)        items[key] = data        return data    }}

Line 34: Use withTaskGroup instead of creating tasks in a loop.

swift
// Beforefor url in urls {    Task { try await fetch(url) }}
// Aftertry await withThrowingTaskGroup(of: Data.self) { group in    for url in urls {        group.addTask { try await fetch(url) }    }
    for try await result in group {        process(result)    }}

Summary

  1. Correctness (high): Actor reentrancy bug on line 18 may cause duplicate downloads and a force-unwrap crash.
  2. Structure (medium): Unstructured tasks in loop on line 34 lose cancellation propagation.

End of example.

References

  • ${CLAUDE_SKILL_DIR}/references/hotspots.md - Grep targets for code review: known-dangerous patterns and what to check for each.
  • ${CLAUDE_SKILL_DIR}/references/new-features.md - Swift 6.2 changes that alter review advice: default actor isolation, isolated conformances, caller-actor async behavior, @concurrent, Task.immediate, task naming, and priority escalation.
  • ${CLAUDE_SKILL_DIR}/references/actors.md - Actor reentrancy, shared-state annotations, global actor inference, and isolation patterns.
  • ${CLAUDE_SKILL_DIR}/references/structured.md - Task groups over loops, discarding task groups, concurrency limits.
  • ${CLAUDE_SKILL_DIR}/references/unstructured.md - Task vs Task.detached, when Task {} is a code smell.
  • ${CLAUDE_SKILL_DIR}/references/cancellation.md - Cancellation propagation, cooperative checking, broken cancellation patterns.
  • ${CLAUDE_SKILL_DIR}/references/async-streams.md - AsyncStream factory, continuation lifecycle, back-pressure.
  • ${CLAUDE_SKILL_DIR}/references/bridging.md - Checked continuations, wrapping legacy APIs, @unchecked Sendable.
  • ${CLAUDE_SKILL_DIR}/references/interop.md - Migrating from GCD, Mutex/locks, completion handlers, delegates, and Combine.
  • ${CLAUDE_SKILL_DIR}/references/bug-patterns.md - Common concurrency failure modes and their fixes.
  • ${CLAUDE_SKILL_DIR}/references/diagnostics.md - Strict-concurrency compiler errors, protocol conformance fixes, and likely remedies.
  • ${CLAUDE_SKILL_DIR}/references/testing.md - Async test strategy with Swift Testing, race detection, avoiding timing-based tests.

來源與署名

來源:twostraws/Swift-Concurrency-Agent-Skill位於swift-concurrency-pro/skills/swift-concurrency-pro提交bee3f69

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架