README.md
modern-swift
modern-swift is a set of three agent skills for writing, reviewing, and porting Swift. The rules describe Swift 6.4 in the Swift 6 language mode. The repository also holds lint settings for the rules a tool can check, and graded tasks that measure whether an agent follows each skill.
Install
| 1 | npx skills add https://git.cafe/dynd/modern-swift.git |
The skills
| Skill | The agent uses it when you ask it to | It produces |
|---|---|---|
swift-best-practices | write, change, refactor, or debug Swift | Swift code that passes swift build and swift format lint |
swift-review | review, audit, or check Swift | a report with ranked findings, and no code changes |
swift-port | port TypeScript, JavaScript, Python, or Kotlin to Swift | a Swift package, ported tests, and PORTING.md |
swift-review and swift-port apply the rules in swift-best-practices, so install all
three together.
How swift-best-practices is organized
The skill tells the agent to start with the simplest form and to move on only for a stated reason.
| Need | Start with | Move on when |
|---|---|---|
| Data | struct or enum | the value needs identity, sharing, or inheritance |
| Polymorphism | some P or a generic | one collection must hold different types |
| Execution | synchronous code on the main actor | a profile shows a hang |
| Safety | the safe API | C interop needs a pointer |
SKILL.md holds the rules that every task needs. A reference file holds the rules for
one kind of task, and the agent reads it only when that kind of task comes up.
| 1 | skills/swift-best-practices/ |
| 2 | ├── SKILL.md # data, errors, concurrency, generics, API design |
| 3 | └── references/ |
| 4 | ├── api-design-guidelines.md # naming or documenting a public API |
| 5 | ├── concurrency.md # isolation errors, callbacks, moving to Swift 6 |
| 6 | ├── swift-testing.md # the user asked for tests |
| 7 | ├── swiftui.md # the target imports SwiftUI |
| 8 | ├── performance-and-arc.md # a profile shows a cost, or a retain cycle |
| 9 | ├── unsafe-and-interop.md # C interop or pointers |
| 10 | ├── macros.md # writing a macro |
| 11 | ├── logging.md # adding logs |
| 12 | └── google-swift-style.md # formatting questions |
Most rules come with a code example, and each swift example in the skills compiles on
Swift 6.4.2 without warnings. For example, the skill shows the rule "use one enum for
states that exclude each other" as a change to a struct.
| 1 | struct Upload { |
| 2 | - var isUploading = false |
| 3 | - var progress: Double? |
| 4 | - var error: (any Error)? |
| 5 | - var resultURL: URL? |
| 6 | + enum State { |
| 7 | + case idle |
| 8 | + case uploading(progress: Double) |
| 9 | + case failed(any Error) |
| 10 | + case finished(URL) |
| 11 | + } |
| 12 | + var state = State.idle |
| 13 | } |
How swift-review works
| 1 | review(target) |
| 2 | 1. decide the scope file, package, or diff; library or app; Swift version |
| 3 | 2. run the tools swift build, swift format lint, swift test |
| 4 | 3. read for problems correctness, errors, API design, idiom, docs |
| 5 | 4. write the report Must fix, Should fix, Consider, What is already good |
| 6 | 5. offer the lint settings |
Each finding gives a file:line location, the rule and its source, and the fix as a
short change.
| 1 | ### 1. The continuation never resumes on failure (`Sources/Feed/FeedView.swift:50`) |
| 2 | |
| 3 | **Rule:** A continuation must resume exactly once on every path (swift-best-practices, section 6) |
| 4 | **Problem:** When the request fails, `data` is `nil` and nothing resumes, so the spinner never stops. |
| 5 | **Fix:** resume with the error in an `else` branch. |
How swift-port works
| 1 | port(source) |
| 2 | 1. list the original's observable behavior |
| 3 | 2. port the tests to Swift Testing |
| 4 | 3. design the Swift API, then implement it |
| 5 | 4. keep the port in proportion to the original |
| 6 | 5. run swift build and swift test |
| 7 | 6. write PORTING.md |
The skill has a mapping table for each source language. Some rows are below.
| In the original | In Swift |
|---|---|
| TypeScript union of string literals | enum with String raw values |
TypeScript Promise.all | async let, or a task group |
| Python exception hierarchy | one error enum with associated values |
Python dict in insertion order | an array of pairs, because Dictionary has no stable order |
Kotlin sealed class | enum with associated values |
Lint settings
lints/ holds the compiler settings and the swift format rules that report the rules
a tool can check. Copy the settings array from lints/Package.swift into your own
Package.swift, and copy lints/.swift-format to the root of your project. The
swift-review skill ships the same file as assets/swift-format.json, so an installed
skill always has it.
| 1 | lints/ |
| 2 | ├── Package.swift # upcoming features, strict memory safety, default isolation |
| 3 | ├── .swift-format # a link to skills/swift-review/assets/swift-format.json |
| 4 | └── check/ # one Swift file for each rule, written to trigger its warning |
To confirm that each setting still reports its rule, run the two commands below. Each
file in check/ names the warning it expects in its first line.
| 1 | cd lints |
| 2 | swift build |
| 3 | swift format lint --recursive check |
Evals
An eval is a task for an agent, with one grader file for each behavior the result should show. The format matches the evals in BeautifulRust.
| 1 | skills/<skill>/evals/<case>/ |
| 2 | ├── case.yaml # the name and the expected outcome |
| 3 | ├── prompt.md # what the user asks |
| 4 | ├── files/ # the starting files, if the task has any |
| 5 | ├── place-files.sh # copies files/ into the working folder |
| 6 | └── graders/ # one file per expected behavior |
| Skill | Eval | The task |
|---|---|---|
swift-best-practices | log-summary-cli | write a command line tool that must not crash on bad input |
swift-best-practices | refactor-inventory-module | remove planted problems and keep the behavior |
swift-best-practices | photo-pipeline-concurrency | move slow work off the main actor without unsafe shortcuts |
swift-review | review-inventory-module | find the planted problems and leave the code unchanged |
swift-review | review-swiftui-feed | find the concurrency faults and accept SwiftUI conventions |
swift-port | port-python-task-graph | port a Python module and its 12 tests |
swift-port | port-typescript-cart-pricing | port a TypeScript module and its 26 tests |
The repository has no script that runs the evals. Use a runner that reads the BeautifulRust format.
Sources
The rules draw on the Swift API Design Guidelines, the Swift 6 migration guide, the Swift Testing documentation, and the Google Swift Style Guide. The layout of the skills and the evals follows BeautifulRust.
License
The repository is under the MIT license, which is in LICENSE.