swift-type-system-architecture
Skill8 starsChanged 6 days agoApache-2.0View on GitHub
Explore a Swift codebase for high-leverage opportunities to use generics, protocols, associated types, some/any, enums, value types, and related type-system features to improve architecture. Surface only cases where stronger typing removes runtime ambiguity, invalid states, casts, duplication, or caller knowledge. Write the HTML report, then auto-execute Strong candidates via create-specification and implement-spec (ticket PRs, size-routed review, worktree cleanup) unless --explore-only. Do not wait for the user to pick a candidate.
In this repository18 other files it gives its agents.
- codebase-designSkill
- principle-type-system-disciplineSkill
- swift-64-concurrencySkill
- swift-adversarial-pr-reviewSkill
- swift-api-design-guidelinesSkill
- swift-concurrencySkill
- swift-evaluate-paritySkill
- swift-functional-architectureSkill
- swift-functional-coreSkill
- swift-hexagonal-spmSkill
- swift-hook-xpcSkill
- swift-pr-orchestrationSkill
- swift-protocol-boundariesSkill
- swift-pr-reviewSkill
- swift-systems-safetySkill
- swift-testing-proSkill
- swift-thermo-nuclear-reviewSkill
- swift-value-semanticsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
88 rules. About 0 are common, 84 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈84
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 44
Do not recommend a type-system refactor merely because it is more "Swifty."
- Distinctive: line 46
Every recommendation must eliminate or materially reduce at least one of:
- Distinctive: line 61
test seams that exist only because type relationships are weak
- Distinctive: line 63
If you cannot name the concrete problem the compiler would help prevent, reject the candidate.
- Distinctive: line 69
Every candidate must account for the genericity budget it consumes:
- Distinctive: line 83
Prefer designs where generic complexity is concentrated inside a deep module and callers see a small, stable interface.
- Distinctive: line 110
Follow these phases in order:
- Distinctive: line 114
Search for type-system pressure points.
- Distinctive: line 122
Do not implement a candidate during exploration (steps 1–7).
- Distinctive: line 155
Do not propose a feature unsupported by the repository's compiler/language mode.
- Distinctive: line 159
Do not silently require a tools-version or deployment-target migration.
- Distinctive: line 163
Before recommending protocol/generic changes in public libraries or binary frameworks, inspect:
- Distinctive: line 175
Do not recommend a type-system improvement as a "refactor" if it is actually an API migration.
- Distinctive: line 177
Call out migration semantics explicitly.
- Distinctive: line 185
Do not grep mechanically and immediately propose refactors.
- Distinctive: line 236
Look for APIs where two or more types are related semantically but the compiler does not know it.
- Distinctive: line 272
If yes, generics or associated types may be justified.
- Distinctive: line 278
Use an associated type when the type relationship belongs to the conforming type's identity/capability.
- Distinctive: line 280
Use a generic parameter when the relationship belongs to one operation.
- Distinctive: line 301
Do not force associated types into protocols when the operation-level generic is simpler.
- Distinctive: line 309
Use an associated type when the relationship belongs to the conforming type's identity or capability.
- Distinctive: line 324
Prefer an operation-level generic when that relationship varies per call.
- Distinctive: line 326
Do not force associated types into protocols when a generic function is simpler.
- Distinctive: line 329
Look for generic parameters that are unconstrained or loosely constrained even though the domain requires relationships between them.
- Distinctive: line 380
Do not recommend a design that materially harms compile-time or diagnostic quality unless the correctness gain is substantial.
- Distinctive: line 382
Prefer moving complex generic machinery behind a deep module so most of the codebase does not see it.
- Distinctive: line 389
Look for warning signs:
- Distinctive: line 399
Do not recommend a design that materially harms compile-time or diagnostics unless the correctness gain is substantial.
- Distinctive: line 407
Treat this as a first-class architectural question.
- Distinctive: line 468
Do not automatically move upward.
- Very specific: line 479
Never recommend some/any changes from syntax alone.
- Distinctive: line 534
Do not remove useful erasure just to make types more generic.
- Distinctive: line 578
add associated types when conformers have meaningful dependent types
- Distinctive: line 579
remove protocols where a concrete deep module is clearer
- Distinctive: line 581
Do not equate "protocol-oriented" with "more protocols."
- Distinctive: line 585
Before recommending a protocol or preserving one, look for evidence of an open world:
- Distinctive: line 593
If the family is closed and controlled by one module, an enum or concrete deep module may be clearer.
- Distinctive: line 595
When adding associated types to an existing protocol, inspect whether current callers rely on existential use.
- Distinctive: line 602
test or platform substitution at a genuine boundary
- Distinctive: line 612
Ask whether a family of behaviors/types is genuinely extensible.
- Distinctive: line 614
If the set is closed and known:
- Distinctive: line 633
Prefer enums for closed state spaces.
- Distinctive: line 635
Prefer protocols for open capability sets.
- Distinctive: line 643
Verify that "closed" is actually stable.
- Very specific: line 675
Avoid default in closed enum switches when explicit exhaustiveness provides future refactor safety.
- Distinctive: line 681
Look for classes that:
- Distinctive: line 695
Do not mechanically convert classes to structs.
- Distinctive: line 716
Do not equate value semantics with "struct everywhere."
- Distinctive: line 732
Look for raw primitive values that are easy to mix accidentally.
- Distinctive: line 750
Use wrappers when they:
- Distinctive: line 757
Do not create wrappers around primitives with no semantic gain.
- Distinctive: line 765
Look for lifecycle/state transitions where using the wrong phase is dangerous and frequently checked at runtime.
- Distinctive: line 800
Before recommending one, explicitly compare it to:
- Distinctive: line 806
If callers do not need to observe the state parameter, hiding the transition behind a module is often superior.
- Distinctive: line 821
Look for generic wrappers/containers that manually reimplement behavior based on their element type.
- Distinctive: line 835
Use conditional conformance to preserve composition.
- Distinctive: line 846
Prefer conformances where at least one side of the relationship is architecturally owned by the module, or document the compatibility risk.
- Distinctive: line 852
Look for domains where callers need to reason about a closed failure set.
- Distinctive: line 862
Use typed throws when:
- Distinctive: line 868
Do not force typed throws onto infrastructure APIs wrapping open-ended external errors.
- Distinctive: line 872
Prefer typed throws where exhaustive failure knowledge is part of the contract.
- Distinctive: line 885
Look for generic values crossing tasks/actors where sendability is implicit.
- Very specific: line 902
Do not add Sendable mechanically.
- Distinctive: line 904
Add it where the architecture actually transfers values across concurrency domains.
- Distinctive: line 929
Use constrained existentials when they simplify callers and preserve the relationship that matters.
- Distinctive: line 931
Do not force primary-associated-type syntax into protocols that do not benefit from existential use.
- Distinctive: line 939
Look for genuinely unique resources:
- Distinctive: line 965
Consider only when the codebase has repeated generic overload families or tuple-like APIs where parameter packs materially reduce …
- Distinctive: line 967
Do not introduce parameter packs because they are advanced Swift.
- Distinctive: line 986
If callers know too much because coordination leaks outward, deepen the module first.
- Distinctive: line 988
Do not solve module-depth problems with:
- Distinctive: line 1098
If the candidate cannot answer these clearly, discard it.
- Distinctive: line 1118
Do not mechanically add the scores.
- Distinctive: line 1126
Before surfacing a candidate, attack it.
- Distinctive: line 1173
Write it outside the repository, preferably:
- Very specific: line 1179
Use the visual guidance in HTML-REPORT.md.
- Distinctive: line 1201
If browser opening is unavailable, print the path.
- Distinctive: line 1242
Use actual repository names.
- Distinctive: line 1266
Use it only as explanatory context, not as a rule that "higher is better."
- Distinctive: line 1328
Every candidate must show what gets more complicated.
- Distinctive: line 1337
Do not hide costs.
- Distinctive: line 1351
Before each ticket's implementation, re-explore that candidate deeply:
- Distinctive: line 1364
Do not defend the original recommendation if deeper evidence disproves it.
- Distinctive: line 1390
Do not measure success by generic line count.
- Distinctive: line 1392
Do not measure success by protocol count.
- Distinctive: line 1394
Do not measure success by sophistication.
- Distinctive: line 1406
Prefer the simplest type-system design that makes an important invariant impossible to violate.
- Distinctive: line 1408
When two designs provide similar safety, prefer the one with:
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Swift Type-System Architecture
Explore a Swift codebase and identify places where Swift's type system can carry more of the architecture.
This skill is intentionally biased toward expert-level use of:
- generics
- generic constraints
- protocols
- associated types
- primary associated types
- opaque types (
some) - existentials (
any) - constrained existentials
- enums
- associated values
- value semantics
- conditional conformances
- semantic/newtype wrappers
- typed errors
Sendable- ownership features when justified
The goal is not to maximize generic code.
The goal is to replace runtime ambiguity, invalid states, casts, duplicated coordination, and weakly expressed relationships with compiler-checked design.
Core principle:
Use generics to preserve relationships, protocols to define capabilities, enums to close state spaces, value types to make data flow explicit,
someto hide concrete types while preserving identity, andanyonly where runtime heterogeneity is intentional.
A sophisticated generic design is only good when it makes the system easier to reason about.
Non-Negotiable Rule
Do not recommend a type-system refactor merely because it is more "Swifty."
Every recommendation must eliminate or materially reduce at least one of:
- runtime casts
Any/AnyObject- stringly typed state
- invalid state combinations
- duplicated type checks
- duplicated protocol plumbing
- repeated coordination logic caused by weak type relationships
- public API ambiguity
- unnecessary caller knowledge
- unsafe concurrency transfer
- incorrect combinations of related types
- type erasure at the wrong layer
- abstraction noise
- test seams that exist only because type relationships are weak
If you cannot name the concrete problem the compiler would help prevent, reject the candidate.
Genericity Has a Budget
Static information is valuable only when the cost of carrying it remains local.
Every candidate must account for the genericity budget it consumes:
- generic parameter propagation across layers
- longer signatures and
whereclauses - harder diagnostics
- code specialization/build-time cost
- source/ABI constraints
- reduced ability to store heterogeneous values
- extra type-erasure boundaries
- harder test fixtures
- increased cognitive load for humans and coding agents
A design that improves one local invariant but forces generic parameters through unrelated modules is usually a net loss.
Prefer designs where generic complexity is concentrated inside a deep module and callers see a small, stable interface.
Genericity Has a Budget
Static information is valuable only when the cost of carrying it remains local.
Every candidate must account for the genericity budget it consumes:
- generic parameter propagation across layers
- longer signatures and
whereclauses - harder diagnostics
- code specialization/build-time cost
- source/ABI constraints
- reduced ability to store heterogeneous values
- extra type-erasure boundaries
- harder test fixtures
- increased cognitive load for humans and coding agents
A design that improves one local invariant but forces generic parameters through unrelated modules is usually a net loss.
Prefer designs where generic complexity is concentrated inside a deep module and callers see a small, stable interface.
Workflow
Follow these phases in order:
- Establish repository and toolchain constraints.
- Map modules, major types, and important call paths.
- Search for type-system pressure points.
- Generate 3–5 strong candidates.
- Adversarially attack each candidate.
- Present the candidates in a visual HTML decision report.
- Open the report in the user's browser.
- Auto-select Strong candidates + the top recommendation.
- Load
swift-architecture-pipelineand execute (spec → implement-spec → review → PRs → cleanup).
Do not implement a candidate during exploration (steps 1–7).
--explore-only restores ask-and-stop after step 7.
Phase 1: Establish Constraints
Inspect:
Package.swift// swift-tools-versionswiftLanguageModes.swift-version- Xcode project settings
- deployment targets
- CI
- repository docs
- architecture docs
AGENTS.md- project-local skills
Record:
- Swift version
- language mode
- supported platforms
- module/target graph
- public library vs application boundaries
- ABI/source compatibility constraints
- testing framework
- concurrency model where documented
Do not propose a feature unsupported by the repository's compiler/language mode.
On Swift 6.4, ~Sendable, some P?/any P?, Iterable, Ref/MutableRef, and associated-type ~Copyable suppression are in play. Route implementation through swift-64-whats-new. Do not recommend FilePath in the Swift module; SE-0529 is Accepted, not Implemented.
Do not silently require a tools-version or deployment-target migration.
Public API and ABI Gate
Before recommending protocol/generic changes in public libraries or binary frameworks, inspect:
- whether the protocol/type is
publicoropen @frozen@inlinable@usableFromInline- library evolution settings
- whether external conformances exist
- whether adding associated types or protocol requirements is source-breaking
- whether changing opaque/existential return types changes source compatibility
- whether generic specialization leaks into public ABI
Do not recommend a type-system improvement as a "refactor" if it is actually an API migration.
Call out migration semantics explicitly.
For app-internal/package-internal code, do not manufacture ABI concerns.
Phase 2: Build a Type-System Map
Do not grep mechanically and immediately propose refactors.
Build a mental map of where types matter.
Identify:
- central protocols
- protocol hierarchies
- type erasers
- generic containers
- registries
- factories
- state machines
- domain enums
- classes carrying mostly immutable data
- classes with identity/lifecycle
- generic functions
- concrete functions with repeated overload families
- APIs using
Any - APIs using
any Protocol - APIs returning
some Protocol - cross-module interfaces
- concurrency boundaries
- public/package APIs
- frequently changed types
For important types, understand:
- callers
- conformers
- associated inputs/outputs
- ownership
- mutation
- isolation
- storage requirements
- whether heterogeneity is real
- whether the type is open or closed
- whether the relationship is static or runtime-selected
Phase 3: Search for Type-System Pressure
Use these lenses.
These are signals, not automatic findings.
Lens A: Lost Type Relationships
Look for APIs where two or more types are related semantically but the compiler does not know it.
Examples:
protocol Tool {
func execute(_ input: Any) async throws -> Any
}Potential direction:
protocol Tool {
associatedtype Input: Sendable
associatedtype Output: Sendable
func execute(_ input: Input) async throws -> Output
}Look for:
- input/output pairs related by convention
- source/destination types that must share a value type
- request/response pairs
- parser/input/result relationships
- command/handler/result relationships
- repository/entity relationships
- decoder/model relationships
- event/payload relationships
Strong candidate test:
Can a wrong combination compile today that should be rejected by the compiler?
If yes, generics or associated types may be justified.
Associated Type vs Generic Parameter
Use an associated type when the type relationship belongs to the conforming type's identity/capability.
Use a generic parameter when the relationship belongs to one operation.
Example intuition:
protocol Parser {
associatedtype Input
associatedtype Output
}when each parser has a stable input/output pair.
Prefer:
func transform<Input, Output>(...)when the relationship varies per call rather than per conformer.
Do not force associated types into protocols when the operation-level generic is simpler.
Lens B: Generic Constraints That Encode Domain Law
Associated Type vs Generic Parameter
Use an associated type when the relationship belongs to the conforming type's identity or capability.
Use a generic parameter when the relationship belongs to one operation.
Example:
protocol Parser {
associatedtype Input
associatedtype Output
}when each parser has a stable input/output pair.
Prefer an operation-level generic when that relationship varies per call.
Do not force associated types into protocols when a generic function is simpler.
Look for generic parameters that are unconstrained or loosely constrained even though the domain requires relationships between them.
Example:
protocol Store {
associatedtype Value
}Potential operation:
func transfer<S: Store, D: Store>(
from source: S,
to destination: D
) where S.Value == D.ValueSearch for:
- runtime equality checks on type metadata
- repeated casts after generic calls
- documentation saying "these types must match"
- functions that accept independent type parameters that are actually linked
- protocol combinations repeatedly checked at runtime
Use:
where- same-type constraints
- associated-type constraints
- generic parameter constraints
- conditional conformances
only when they encode a real semantic relationship.
Compile-Time Ergonomics
Advanced generic design can impose real compiler and developer costs.
Look for:
- deeply nested generic wrappers
- long same-type constraint chains
- recursive generic protocols
- overload sets that become ambiguous
- diagnostics that no longer point clearly to the offending call
- generic signatures leaking through many layers
- large generic bodies specialized repeatedly
Do not recommend a design that materially harms compile-time or diagnostic quality unless the correctness gain is substantial.
Prefer moving complex generic machinery behind a deep module so most of the codebase does not see it.
Compile-Time Ergonomics
Advanced generic design can impose real compiler and developer costs.
Look for warning signs:
- deeply nested generic wrappers
- long same-type constraint chains
- recursive generic protocols
- overload sets that become ambiguous
- diagnostics that no longer point clearly to the offending call
- large generic bodies duplicated across specializations
- generic signatures leaking through many public/internal layers
Do not recommend a design that materially harms compile-time or diagnostics unless the correctness gain is substantial.
Prefer moving complex generic machinery behind a deep module so most of the codebase does not see it.
Lens C: some vs any
Treat this as a first-class architectural question.
Prefer some when:
- the implementation chooses one concrete conforming type
- callers do not need to switch concrete implementations at runtime
- the concrete type should remain hidden
- preserving static identity helps composition
- existential storage is unnecessary
Example:
func makeParser() -> some ParserPrefer any when:
- runtime heterogeneity is real
- values with different concrete types must share storage
- plugins/adapters are selected dynamically
- module boundaries intentionally erase concrete implementation
- existential semantics simplify the architecture more than generics would
Example:
let tools: [any Tool]Prefer constrained existentials when:
- heterogeneity is required
- but an associated type relationship still matters
Conceptually:
any Collection<Element>when the supported Swift version allows the required primary-associated-type expression.
Review the type-information continuum:
Concrete Type
↓
T: Protocol
↓
some Protocol
↓
constrained any Protocol<...>
↓
any Protocol
↓
AnyMoving downward generally loses compiler-visible information.
Do not automatically move upward.
The right point depends on runtime requirements.
Also verify semantics:
somepreserves one hidden concrete type per declaration; it is not arbitrary runtime variationanyintroduces existential semantics and can affectSelf/associated-type usability- constrained existentials are useful only when that constrained associated type matters to callers
- changing between
someandanycan alter storage, overload resolution, composition, and source compatibility
Never recommend some/any changes from syntax alone. Inspect call sites and storage needs.
Also verify semantics:
- opaque result types (
some) preserve one hidden concrete type per declaration, not arbitrary runtime variation - existentials (
any) introduce existential semantics and may affect protocol requirements involvingSelf/associated types - constrained existentials are only useful when the constrained associated type is actually part of the caller's contract
- switching between
someandanycan change overload resolution, storage, composition, and source compatibility
Never recommend some/any changes from syntax alone. Inspect call sites and storage needs.
Lens D: Misplaced Type Erasure
Search for:
AnyFooAnyRepositoryAnyStoreAnyToolAnyHandler- closures used as ad-hoc erased protocol implementations
AnyAnyObject
Ask:
- Is erasure required for heterogeneous storage?
- Is it required at a module boundary?
- Does it hide a relationship callers still need?
- Is it being introduced too early?
- Could the erased boundary move outward?
A strong refactor often preserves concrete/generic types internally and erases only at the true dynamic boundary.
The skill should identify the erasure boundary, not merely whether erasure exists.
Good:
generic domain core
→ package/internal adapter
→ erased plugin registrySuspicious:
public API
→ immediate erasure
→ repeated casts internallyDo not remove useful erasure just to make types more generic.
Identify the erasure boundary, not merely whether erasure exists.
Good:
generic domain core
→ package/internal adapter
→ erased plugin registrySuspicious:
public API
→ immediate erasure
→ repeated casts internallyStrong designs usually preserve type information internally and erase only at the true dynamic boundary.
Lens E: Protocol Quality
Protocols should define meaningful capabilities or substitution boundaries.
Look for:
- giant protocols
- protocols with unrelated requirements
- protocols that simply mirror one concrete type
- protocols created only for tests
- protocol inheritance pyramids
- optional/default requirements hiding weak cohesion
- protocols with associated relationships flattened into
Any
Potential directions:
- split by capability
- merge shallow protocol layers
- replace open protocol families with enums when the set is truly closed
- add associated types when conformers have meaningful dependent types
- remove protocols where a concrete deep module is clearer
Do not equate "protocol-oriented" with "more protocols."
Elite protocol design means few strong protocols.
Before recommending a protocol or preserving one, look for evidence of an open world:
- multiple real conformers
- plugin/extensibility requirements
- platform substitution at a genuine seam
- package/client extension points
- heterogeneous runtime storage
If the family is closed and controlled by one module, an enum or concrete deep module may be clearer.
When adding associated types to an existing protocol, inspect whether current callers rely on existential use. Do not make common storage/use sites dramatically harder without a compensating architectural gain.
Before recommending a protocol or preserving one, look for evidence of an open world:
- multiple real conformers
- plugin/extensibility requirements
- test or platform substitution at a genuine boundary
- package/client extension points
- heterogeneous storage/dynamic dispatch needs
If the family is closed and controlled by one module, an enum or concrete deep module may be clearer.
Lens F: Open vs Closed Worlds
Ask whether a family of behaviors/types is genuinely extensible.
If the set is closed and known:
protocol State {}
struct Idle: State {}
struct Running: State {}
struct Failed: State {}may be worse than:
enum State {
case idle
case running
case failed(Error)
}Prefer enums for closed state spaces.
Prefer protocols for open capability sets.
But verify that "closed" is actually stable.
A public enum used as an external extension point may be the wrong closure mechanism, while an internal protocol with controlled conformers may still be appropriate.
This is one of the highest-value architecture distinctions in Swift.
Verify that "closed" is actually stable.
A public enum used as an external extension point may be the wrong closure mechanism, while an internal protocol with controlled conformers can still be appropriate.
Lens G: Enums and Associated Values
Look for:
- multiple booleans encoding modes
- optional soup
- parallel optional payloads
- error + result stored simultaneously
- string states
- dictionaries keyed by magic values
- switch logic spread across unrelated fields
Potential direction:
enum ExecutionState {
case idle
case running(Request)
case completed(Output)
case failed(ExecutionError)
}The compiler should force handling of meaningful state transitions.
Avoid default in closed enum switches when explicit exhaustiveness provides future refactor safety.
Lens H: Value Semantics
Look for classes that:
- have no meaningful identity
- mostly store immutable data
- are passed as snapshots
- exist only as DTOs/configuration/results
- require synchronization only because they are shared references
Potential directions:
struct- immutable properties
- copy-on-write where justified
Do not mechanically convert classes to structs.
Reference semantics may be correct for:
- identity
- graph nodes
- lifecycle
- observation
- Objective-C/XPC interop
- shared caches
- framework requirements
A value-type recommendation must explain what correctness or locality improves.
Also inspect semantic and performance cost:
- large values may need copy-on-write or reference-backed storage
- graph nodes and identity-sensitive objects may not map cleanly to structs
- observation frameworks may require reference identity
- copying a lock/resource wrapper may be semantically wrong
Do not equate value semantics with "struct everywhere."
Also inspect cost/semantics:
- large values may need copy-on-write or reference-backed storage
- identity-sensitive collections/graphs may not map cleanly to structs
- observation frameworks may require reference identity
- copying a lock/resource wrapper may be semantically wrong
Do not equate value semantics with "struct everywhere."
Lens I: Semantic/Newtype Wrappers
Look for raw primitive values that are easy to mix accidentally.
Examples:
String // Rule ID
String // Pack ID
String // User IDPotential direction:
struct RuleID: Hashable, Sendable {
let rawValue: String
}Use wrappers when they:
- prevent accidental mixing
- enforce validation
- carry domain meaning
- improve APIs
Do not create wrappers around primitives with no semantic gain.
Lens J: Phantom-State Generics
Advanced tool. Use sparingly.
Look for lifecycle/state transitions where using the wrong phase is dangerous and frequently checked at runtime.
Example:
enum Unvalidated {}
enum Validated {}
struct Request<State> {
...
}
func validate(
_ request: Request<Unvalidated>
) throws -> Request<Validated>Good candidates:
- authentication state
- validation state
- transaction lifecycle
- resource initialization
- permission/authorization state
- compile/build pipeline stages
Reject if:
- there are only one or two call sites
- the state machine is simple enough for an enum
- generic state leaks everywhere
- the type burden exceeds the safety gain
Phantom-state designs are Tier 3 tools.
Before recommending one, explicitly compare it to:
- a plain enum state machine
- a validating initializer or factory
- a deeper module that hides the transition entirely
If callers do not need to observe the state parameter, hiding the transition behind a module is often superior.
Before recommending one, explicitly compare it to:
- a plain enum state machine
- a validating initializer/factory
- a deeper module that hides the transition entirely
If callers do not need to observe the state parameter, hiding the transition behind a module is often superior.
Lens K: Conditional Conformance
Look for generic wrappers/containers that manually reimplement behavior based on their element type.
Potentially useful:
extension Box: Equatable where Value: Equatable {}
extension Box: Sendable where Value: Sendable {}Look for:
- repeated forwarding equality/hash/codable/sendable implementations
- wrapper types whose capabilities should derive from contained type capabilities
Use conditional conformance to preserve composition.
Be cautious with retroactive conformances across module boundaries.
A convenient conformance can create future conflicts if neither the type nor protocol is owned by the module.
Be cautious with retroactive conformances across module boundaries.
A convenient conformance can create future conflicts if neither the type nor protocol is owned by the current module.
Prefer conformances where at least one side of the relationship is architecturally owned by the module, or document the compatibility risk.
Lens L: Typed Errors
Look for domains where callers need to reason about a closed failure set.
Potential direction:
func compile(
_ graph: Graph
) throws(CompilationError) -> ProgramUse typed throws when:
- failure domain is intentionally closed
- exhaustive caller handling is valuable
- the type is stable enough
Do not force typed throws onto infrastructure APIs wrapping open-ended external errors.
Typed throws can also become noisy if exact error types propagate through generic layers that callers do not care about.
Prefer typed throws where exhaustive failure knowledge is part of the contract.
Also inspect protocol/generic propagation.
Typed throws can improve a closed domain but become noisy if every generic layer must carry an exact error type that callers do not care about.
Prefer typed throws where exhaustive failure knowledge is part of the contract.
Lens M: Generics Across Concurrency Boundaries
Look for generic values crossing tasks/actors where sendability is implicit.
Potential directions:
protocol Tool {
associatedtype Input: Sendable
associatedtype Output: Sendable
}or generic constraints such as:
func execute<T: Sendable>(_ value: T) async -> TDo not add Sendable mechanically.
Add it where the architecture actually transfers values across concurrency domains.
Inspect closure captures and associated types, not only the top-level generic parameter.
A generic API can constrain T: Sendable while still capturing non-Sendable state unsafely.
Inspect closure captures and associated types, not just the top-level generic parameter.
A generic API can be T: Sendable while still capturing non-Sendable state unsafely.
Lens N: Primary Associated Types and Constrained Existentials
Where supported by the toolchain, inspect protocols whose key associated type is part of the protocol's semantic identity.
These may support APIs that are more expressive than unconstrained existentials while avoiding full generic propagation.
Look for:
- collections of protocol values where one associated type must be fixed
- APIs accepting heterogenous conformers that share one associated type
- type erasers whose only purpose is to pin one associated type
Use constrained existentials when they simplify callers and preserve the relationship that matters.
Do not force primary-associated-type syntax into protocols that do not benefit from existential use.
Lens O: Ownership Features
Advanced and narrow.
Look for genuinely unique resources:
- file descriptors
- native handles
- locks
- transaction handles
- sockets
- buffers with exclusive ownership
- resources that must be consumed exactly once
Potential tools:
borrowingconsuming~Copyable
Reject ordinary model/value candidates.
Ownership syntax should encode a real lifecycle invariant.
Lens P: Variadic Generics / Parameter Packs
Tier 3 only.
Consider only when the codebase has repeated generic overload families or tuple-like APIs where parameter packs materially reduce duplication while preserving type information.
Do not introduce parameter packs because they are advanced Swift.
The benefit must be obvious and recurring.
Also verify:
- supported Swift version
- compiler maturity for the specific pattern
- whether diagnostics remain understandable
- whether a simpler overload family is actually easier to maintain
Architecture Before Generics
For every candidate, ask:
Is this fundamentally a type-relationship problem, or a module-boundary problem?
If callers know too much because coordination leaks outward, deepen the module first.
Do not solve module-depth problems with:
GenericManager<A, B, C, D>when the real fix is a smaller interface.
Generics should encode relationships inside or across good boundaries, not compensate for bad boundaries.
Anti-Patterns: Reject These Recommendations
Do not recommend:
- generics used once with no meaningful constraint
- protocol + associated type for a closed three-case state machine
- generic wrappers that merely rename a concrete implementation
somereplacinganywhen runtime heterogeneity is requiredanyreplacing generics merely to simplify compiler errors- type erasure solely to make a generic API storable
- phantom types for trivial state
- complex recursive generic structures with no clear invariant gain
- new protocols solely for mocking
- generic performance specialization without evidence
- class→struct churn with no semantic benefit
Sendableannotations added only to silence diagnostics- ownership features in ordinary app models
- parameter packs for code that is already clear
"More advanced Swift" is not a success criterion.
Candidate Tiers
Classify opportunities.
Tier 1 — Clear Compiler Wins
Examples:
- replace
Any - replace force casts
- enum state modeling
- associated values
- meaningful newtypes
someinstead of unnecessary existential returns- generic same-type constraints
- natural
Sendableconstraints - narrow capability protocols
These should usually have obvious local correctness benefits.
Tier 2 — Architectural Type-System Wins
Examples:
- associated-type protocol redesign
- primary associated types
- constrained existentials
- moving type erasure outward
- type-safe registries
- conditional conformances
- generic adapters
- value-semantic subsystem boundaries
- typed error architecture
These may affect a subsystem or module.
Tier 3 — Advanced / High Scrutiny
Examples:
- phantom-state generics
~Copyableborrowing/consuming- parameter packs
- sophisticated type erasure
- recursive generic designs
Include a Tier 3 candidate only with strong repository evidence.
Candidate Requirements
Generate at most 5 candidates.
Prefer 3 excellent candidates.
Each candidate must answer:
- What weak type relationship exists today?
- What runtime behavior/cast/state check exists because of it?
- Which Swift feature would encode the relationship?
- What becomes impossible to express incorrectly?
- What becomes easier for callers?
- What becomes harder for callers?
- Does generic complexity leak across module boundaries?
- Could an enum or deeper module solve this more simply?
- Is runtime heterogeneity real?
- What migration risk exists?
- Where does generic complexity stop?
- What is the intended erasure boundary?
- Does the proposal worsen compiler diagnostics or build-time complexity?
- Is any public/source/ABI migration required?
- What simpler design was considered and rejected?
If the candidate cannot answer these clearly, discard it.
Candidate Scoring
Score 1–5:
- Compiler gain — how much correctness moves to compile time
- Type information — how much useful type identity/relationship is preserved
- Architecture leverage — how many callers/changes benefit
- API clarity — whether call sites become easier to understand
- Locality — whether less code/context must be understood
- Testability — whether fewer runtime permutations/mocks are needed
- Complexity cost — how much generic/protocol complexity is introduced
- Migration risk — regression/churn risk
- Genericity spread — how far type parameters/constraints propagate
- Diagnostic quality — whether compiler errors remain understandable
- Boundary quality — whether erasure/dynamic dispatch sits at the right layer
Do not mechanically add the scores.
A candidate with 5/5 compiler gain but 5/5 complexity cost may be a bad design.
Adversarial Review
Before surfacing a candidate, attack it.
Ask:
- Is this real type safety or generic cleverness?
- Does the compiler gain justify the syntax burden?
- Could a plain enum solve this?
- Could a deeper module solve this?
- Does the design force generic parameters through unrelated layers?
- Is
anyactually correct here? - Does
somepreserve enough flexibility? - Would type erasure at one outer boundary be simpler?
- Is the protocol truly open for extension?
- Does value semantics break identity/lifecycle assumptions?
- Does this create source/ABI compatibility issues?
- Does the supported Swift version allow the design?
- Is this likely to make coding agents struggle with compiler diagnostics?
- Would a senior Swift engineer understand the API without decoding type gymnastics?
- Does the proposal force generic parameters through modules that do not care about them?
- Is the new protocol still usable at the actual call sites that matter?
- Am I creating an ABI/source migration and calling it a refactor?
- Does the compiler produce better errors after this change, or worse ones?
- Can I move the generic complexity behind a deeper module?
- Is the type-erasure boundary intentionally chosen?
- Is the recommendation robust if another conformer or case is added later?
- Does the proposal force generic parameters through modules that do not care about them?
- Is the new protocol still usable at the actual call sites that matter?
- Am I creating an ABI/source migration and calling it a refactor?
- Does the compiler produce better errors after this change, or worse ones?
- Can I move the generic complexity behind a deeper module?
- Is the type erasure boundary intentionally chosen?
- Is the recommendation robust if another conformer/case is added later?
Reject candidates that fail this review.
HTML Decision Report
Render the result as a local HTML report.
Name it:
swift-type-system-review-<repo>-<timestamp>.htmlWrite it outside the repository, preferably:
${TMPDIR:-/tmp}Use the visual guidance in HTML-REPORT.md.
Open it in the user's default browser.
macOS:
open "<absolute-path>"Linux:
xdg-open "<absolute-path>"Windows:
start "" "<absolute-path>"If browser opening is unavailable, print the path.
Required Report Structure
Header
Show:
- repository
- branch
- Swift version/language mode
- modules explored
- candidate count
Type-System Map
Show a compact diagram of the important relationships discovered.
Examples:
ToolRegistry
│
├── [any Tool]
│ ├── ShellTool
│ ├── FileTool
│ └── SearchTool
│
└── execute(input: Any) -> Anyor:
Parser<Input, Output>
Command<Payload>
Store<Value>Use actual repository names.
Type-Information Gradient
Include a small visual:
MORE STATIC INFORMATION
Concrete
↓
T: P
↓
some P
↓
any P<Constraint>
↓
any P
↓
Any
MORE RUNTIME FLEXIBILITYUse it only as explanatory context, not as a rule that "higher is better."
Candidate Cards
Each card must include:
- title
- tier
- recommendation strength
- repository evidence
- current type relationship
- proposed type relationship
- relevant Swift mechanisms
- compiler gain
- API/caller gain
- complexity introduced
- migration risk
- likely scope
- "why not a simpler design?" section
Before / After Type Diagram
Example:
BEFORE
Tool
├── execute(Any) → Any
└── callers cast result
AFTER
Tool
├── associatedtype Input
├── associatedtype Output
└── execute(Input) → Output
Compiler proves:
Input ↔ Tool ↔ OutputCompiler Gain
Make this prominent.
Examples:
Wrong parser/result pair
Before: runtime cast failure
After: does not compileUnhandled state
Before: optional/boolean combination
After: exhaustive switch diagnosticComplexity Cost
Every candidate must show what gets more complicated.
Examples:
- generic parameters propagate to two callers
- existential storage now requires an outer erasure boundary
- public protocol changes are source-breaking
- additional where-clauses increase signature complexity
Do not hide costs.
Top Recommendation
Recommend one candidate. Do not implement it during exploration.
Explain why its compiler/architecture gain justifies its complexity.
After Candidates Are Ready
Unless --explore-only, load the swift-architecture-pipeline skill (if installed; otherwise stop after reporting candidates) and continue from auto-select through spec, implement-spec, review, PRs, and cleanup.
Before each ticket's implementation, re-explore that candidate deeply:
- inspect all callers
- inspect conformers
- inspect public/package API
- inspect tests
- inspect serialization/persistence
- inspect concurrency/sendability
- inspect compatibility requirements
- challenge the proposed generic design again
Then produce a concrete refactor design (in the spec/ticket, implemented by implement-spec subagents).
Do not defend the original recommendation if deeper evidence disproves it.
Success Criteria
A successful type-system refactor should produce one or more of these outcomes:
- invalid combinations stop compiling
- casts disappear
Anydisappears from domain code- caller-visible state space shrinks
- protocols become smaller and more meaningful
- associated input/output relationships become explicit
- existential erasure moves to a deliberate dynamic boundary
somehides implementation without losing type identityanyis used only where runtime heterogeneity is intentional- closed state becomes exhaustive
- value semantics make ownership clearer
- concurrency boundaries become
Sendable-checked - tests need fewer invalid runtime combinations
- a deep module can expose a smaller typed interface
- future coding agents get stronger compiler diagnostics during refactors
- generic complexity remains local rather than infecting unrelated layers
- dynamic erasure is concentrated at intentional boundaries
Do not measure success by generic line count.
Do not measure success by protocol count.
Do not measure success by sophistication.
Measure success by how much architecture the compiler can now prove.
Final Principle
Elite Swift generics are not about writing the most generic code.
They are about expressing relationships precisely enough that the compiler can reject entire categories of incorrect programs while keeping the API understandable.
Prefer the simplest type-system design that makes an important invariant impossible to violate.
When two designs provide similar safety, prefer the one with:
- smaller caller-facing types
- less generic propagation
- clearer diagnostics
- fewer compatibility constraints
- a deeper module boundary
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

