engsys 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +202 -0
  3. package/core/agents/aaron.md +152 -0
  4. package/core/agents/bert.md +115 -0
  5. package/core/agents/isabelle.md +136 -0
  6. package/core/agents/jody.md +150 -0
  7. package/core/agents/leith.md +111 -0
  8. package/core/agents/marcelo.md +282 -0
  9. package/core/agents/melvin.md +101 -0
  10. package/core/agents/nyx.md +152 -0
  11. package/core/agents/otto.md +168 -0
  12. package/core/agents/patricia.md +283 -0
  13. package/core/commands/design-audit-local.md +155 -0
  14. package/core/commands/design-audit.md +235 -0
  15. package/core/commands/design-critique.md +96 -0
  16. package/core/commands/file-issue.md +22 -0
  17. package/core/commands/generate-project.md +45 -0
  18. package/core/commands/implement-issue.md +37 -0
  19. package/core/commands/implement-project.md +40 -0
  20. package/core/commands/naturalize.md +61 -0
  21. package/core/commands/pre-push.md +29 -0
  22. package/core/commands/prep-review-collect.md +130 -0
  23. package/core/commands/prep-review-finalize.md +121 -0
  24. package/core/commands/prep-review-publish.md +113 -0
  25. package/core/commands/prep-review.md +65 -0
  26. package/core/commands/project-closeout.md +25 -0
  27. package/core/skills/agentic-eval/SKILL.md +195 -0
  28. package/core/skills/chrome-devtools/SKILL.md +97 -0
  29. package/core/skills/code-review/SKILL.md +26 -0
  30. package/core/skills/gh-cli/SKILL.md +2202 -0
  31. package/core/skills/git-commit/SKILL.md +124 -0
  32. package/core/skills/git-workflow-agents/SKILL.md +462 -0
  33. package/core/skills/git-workflow-agents/reference.md +220 -0
  34. package/core/skills/github-actions/SKILL.md +190 -0
  35. package/core/skills/github-issues/SKILL.md +154 -0
  36. package/core/skills/llm-structured-outputs/SKILL.md +323 -0
  37. package/core/skills/llm-structured-outputs/references/provider-details.md +392 -0
  38. package/core/skills/pre-push/SKILL.md +115 -0
  39. package/core/skills/refactor/SKILL.md +645 -0
  40. package/core/skills/web-design-reviewer/SKILL.md +371 -0
  41. package/core/skills/webapp-testing/SKILL.md +127 -0
  42. package/core/skills/webapp-testing/test-helper.js +56 -0
  43. package/core/templates/CLAUDE.md.tmpl +98 -0
  44. package/core/templates/adr-template.md +67 -0
  45. package/core/templates/gh-issue-templates/bug.md +39 -0
  46. package/core/templates/gh-issue-templates/content.md +42 -0
  47. package/core/templates/gh-issue-templates/enhancement.md +36 -0
  48. package/core/templates/gh-issue-templates/feature.md +39 -0
  49. package/core/templates/gh-issue-templates/infrastructure.md +41 -0
  50. package/core/templates/post-edit-reminders.sh.tmpl +19 -0
  51. package/core/templates/settings.json.tmpl +90 -0
  52. package/core/templates/settings.local.json.tmpl +3 -0
  53. package/core/workflows/agent-implementation-workflow.md +346 -0
  54. package/core/workflows/generate-project.md +258 -0
  55. package/core/workflows/implement-project-workflow.md +190 -0
  56. package/core/workflows/issue-tracking.md +89 -0
  57. package/core/workflows/project-closeout-ceremony.md +77 -0
  58. package/core/workflows/review-workflow.md +266 -0
  59. package/engsys.config.example.yaml +46 -0
  60. package/install +202 -0
  61. package/lessons-library/README.md +80 -0
  62. package/lessons-library/async-callbacks-verify-liveness.md +15 -0
  63. package/lessons-library/change-isnt-done-until-every-surface-updated.md +15 -0
  64. package/lessons-library/claim-then-act-for-irreversible-ops.md +16 -0
  65. package/lessons-library/co-commit-entangled-work.md +15 -0
  66. package/lessons-library/dependabot-triage-playbook.md +17 -0
  67. package/lessons-library/deploy-by-digest-and-verify-the-running-revision.md +15 -0
  68. package/lessons-library/enforce-your-guarantee-at-your-boundary.md +16 -0
  69. package/lessons-library/gate-changes-on-measurement-not-vibes.md +15 -0
  70. package/lessons-library/iac-first-no-console-changes.md +15 -0
  71. package/lessons-library/independent-objective-review-gate.md +15 -0
  72. package/lessons-library/keep-an-immutable-source-of-truth.md +15 -0
  73. package/lessons-library/long-agent-runs-checkpoint-not-poll.md +15 -0
  74. package/lessons-library/model-identity-with-stable-ids-and-provenance.md +15 -0
  75. package/lessons-library/operator-choices-are-first-class.md +15 -0
  76. package/lessons-library/prefer-tool-enforced-structured-output.md +15 -0
  77. package/lessons-library/prove-causation-before-acting.md +15 -0
  78. package/lessons-library/re-read-state-before-acting.md +14 -0
  79. package/lessons-library/read-layer-tolerates-unbackfilled-rows.md +15 -0
  80. package/lessons-library/shell-safety-pipefail-and-validate-before-teardown.md +14 -0
  81. package/lessons-library/shift-correctness-left-and-distrust-false-greens.md +15 -0
  82. package/lessons-library/stray-control-bytes-hide-changes.md +14 -0
  83. package/lessons-library/tests-can-assert-the-bug.md +15 -0
  84. package/lessons-library/verify-ground-truth-not-reports.md +15 -0
  85. package/lessons-library/worktrees-need-bootstrap-from-origin-main.md +15 -0
  86. package/lib/commands.js +356 -0
  87. package/lib/generate-team-avatars.mjs +251 -0
  88. package/lib/manifest.js +155 -0
  89. package/lib/render.js +135 -0
  90. package/lib/selftest.js +90 -0
  91. package/lib/util.js +89 -0
  92. package/lib/yaml.js +156 -0
  93. package/optional-agents/gary.md +86 -0
  94. package/optional-agents/jos.md +136 -0
  95. package/optional-agents/sandy.md +101 -0
  96. package/optional-agents/steve.md +161 -0
  97. package/package.json +43 -0
  98. package/stacks/cloud/aws/claude.fragment.md +17 -0
  99. package/stacks/cloud/aws/settings.fragment.json +39 -0
  100. package/stacks/cloud/aws/skills/aws-deployment-preflight/SKILL.md +165 -0
  101. package/stacks/cloud/aws/skills/cloud-architecture-aws/SKILL.md +265 -0
  102. package/stacks/cloud/azure/claude.fragment.md +17 -0
  103. package/stacks/cloud/azure/settings.fragment.json +45 -0
  104. package/stacks/cloud/azure/skills/azure-deployment-preflight/SKILL.md +175 -0
  105. package/stacks/cloud/azure/skills/cloud-architecture-azure/SKILL.md +211 -0
  106. package/stacks/cloud/cloudflare/claude.fragment.md +21 -0
  107. package/stacks/cloud/cloudflare/settings.fragment.json +31 -0
  108. package/stacks/cloud/cloudflare/skills/cloud-architecture-cloudflare/SKILL.md +294 -0
  109. package/stacks/cloud/cloudflare/skills/cloudflare-deployment-preflight/SKILL.md +175 -0
  110. package/stacks/cloud/gcp/claude.fragment.md +17 -0
  111. package/stacks/cloud/gcp/settings.fragment.json +40 -0
  112. package/stacks/cloud/gcp/skills/cloud-architecture-gcp/SKILL.md +208 -0
  113. package/stacks/cloud/gcp/skills/gcp-deployment-preflight/SKILL.md +137 -0
  114. package/stacks/db/mongo/skills/mongo-conventions/SKILL.md +96 -0
  115. package/stacks/db/prisma/claude.fragment.md +49 -0
  116. package/stacks/db/prisma/skills/docker-database-package-copy/SKILL.md +44 -0
  117. package/stacks/db/prisma/skills/prisma-conventions/SKILL.md +37 -0
  118. package/stacks/domain/mobile-growth/skills/apple-ads/SKILL.md +184 -0
  119. package/stacks/domain/mobile-growth/skills/apple-ads/references/benchmark-notes.md +47 -0
  120. package/stacks/domain/mobile-growth/skills/apple-ads/references/official-links.md +53 -0
  121. package/stacks/domain/mobile-growth/skills/google-play-growth/SKILL.md +197 -0
  122. package/stacks/domain/mobile-growth/skills/google-play-growth/references/benchmark-notes.md +47 -0
  123. package/stacks/domain/mobile-growth/skills/google-play-growth/references/official-links.md +45 -0
  124. package/stacks/iac/bicep/claude.fragment.md +14 -0
  125. package/stacks/iac/bicep/settings.fragment.json +20 -0
  126. package/stacks/iac/bicep/skills/iac-bicep/SKILL.md +113 -0
  127. package/stacks/iac/cdk/claude.fragment.md +14 -0
  128. package/stacks/iac/cdk/settings.fragment.json +23 -0
  129. package/stacks/iac/cdk/skills/iac-cdk/SKILL.md +104 -0
  130. package/stacks/iac/terraform/claude.fragment.md +13 -0
  131. package/stacks/iac/terraform/settings.fragment.json +25 -0
  132. package/stacks/iac/terraform/skills/iac-terraform/SKILL.md +93 -0
  133. package/stacks/iac/terraform/skills/terraform-conventions/SKILL.md +87 -0
  134. package/stacks/lang/kotlin/skills/android-testing/SKILL.md +263 -0
  135. package/stacks/lang/kotlin/skills/jetpack-compose/SKILL.md +264 -0
  136. package/stacks/lang/kotlin/skills/kotlin-coroutines/SKILL.md +329 -0
  137. package/stacks/lang/python/skills/python-conventions/SKILL.md +61 -0
  138. package/stacks/lang/shell/skills/shell-scripting/SKILL.md +110 -0
  139. package/stacks/lang/swift/skills/swift-concurrency/SKILL.md +423 -0
  140. package/stacks/lang/swift/skills/swift-concurrency/references/approachable-concurrency.md +80 -0
  141. package/stacks/lang/swift/skills/swift-concurrency/references/concurrency-patterns.md +233 -0
  142. package/stacks/lang/swift/skills/swift-concurrency/references/swiftui-concurrency.md +187 -0
  143. package/stacks/lang/swift/skills/swift-concurrency/references/synchronization-primitives.md +341 -0
  144. package/stacks/lang/swift/skills/swift-testing/SKILL.md +497 -0
  145. package/stacks/lang/swift/skills/swift-testing/references/testing-advanced.md +106 -0
  146. package/stacks/lang/swift/skills/swift-testing/references/testing-patterns.md +504 -0
  147. package/stacks/lang/swift/skills/swiftdata/SKILL.md +334 -0
  148. package/stacks/lang/swift/skills/swiftdata/references/core-data-coexistence.md +504 -0
  149. package/stacks/lang/swift/skills/swiftdata/references/swiftdata-advanced.md +975 -0
  150. package/stacks/lang/swift/skills/swiftdata/references/swiftdata-queries.md +675 -0
  151. package/stacks/lang/swift/skills/swiftui-patterns/SKILL.md +371 -0
  152. package/stacks/lang/swift/skills/swiftui-patterns/references/architecture-patterns.md +486 -0
  153. package/stacks/lang/swift/skills/swiftui-patterns/references/deprecated-migration.md +1097 -0
  154. package/stacks/lang/swift/skills/swiftui-patterns/references/design-polish.md +780 -0
  155. package/stacks/lang/swift/skills/swiftui-patterns/references/platform-and-sharing.md +696 -0
  156. package/stacks/lang/typescript/skills/typescript-conventions/SKILL.md +91 -0
  157. package/stacks/platform/android/claude.fragment.md +40 -0
  158. package/stacks/platform/android/hooks/pre-push-gradle.sh +70 -0
  159. package/stacks/platform/android/settings.fragment.json +13 -0
  160. package/stacks/platform/android/skills/android-build-conventions/SKILL.md +247 -0
  161. package/stacks/platform/ios/claude.fragment.md +24 -0
  162. package/stacks/platform/ios/hooks/pre-push-xcodebuild.sh +82 -0
  163. package/stacks/platform/ios/settings.fragment.json +21 -0
  164. package/stacks/platform/ios/skills/xcodebuildmcp-simulator-logs/SKILL.md +76 -0
  165. package/stacks/platform/web/skills/frontend-testing/SKILL.md +246 -0
  166. package/stacks/platform/web/skills/react-conventions/SKILL.md +261 -0
  167. package/stacks/platform/web/skills/web-platform-conventions/SKILL.md +55 -0
  168. package/stacks/tooling/issue-tracker-github/claude.fragment.md +10 -0
  169. package/stacks/tooling/issue-tracker-github/settings.fragment.json +24 -0
  170. package/stacks/tooling/issue-tracker-github/skills/issue-tracker-github/SKILL.md +278 -0
  171. package/stacks/tooling/issue-tracker-linear/claude.fragment.md +17 -0
  172. package/stacks/tooling/issue-tracker-linear/settings.fragment.json +9 -0
  173. package/stacks/tooling/issue-tracker-linear/skills/issue-tracker-linear/SKILL.md +183 -0
@@ -0,0 +1,233 @@
1
+ # Concurrency Patterns
2
+
3
+ Approachable concurrency patterns introduced in Swift 6.2+ — a philosophy shift where
4
+ code stays single-threaded by default until you choose to introduce concurrency.
5
+
6
+ ## Contents
7
+
8
+ - [Core Problem Solved](#core-problem-solved)
9
+ - [SE-0466: Default MainActor Isolation](#se-0466-default-mainactor-isolation)
10
+ - [SE-0461: nonisolated(nonsending)](#se-0461-nonisolatednonsending)
11
+ - [@concurrent Attribute](#concurrent-attribute)
12
+ - [SE-0472: Task.immediate](#se-0472-taskimmediate)
13
+ - [Isolated Conformances](#isolated-conformances)
14
+ - [SE-0481: weak let (Proposed)](#se-0481-weak-let-proposed--swift-62)
15
+ - [SE-0475: Transactional Observation (Observations)](#se-0475-transactional-observation-observations)
16
+ - [Global and Static State](#global-and-static-state)
17
+ - [Migration and Build Settings](#migration-and-build-settings)
18
+ - [Summary](#summary)
19
+
20
+ ## Core Problem Solved
21
+
22
+ In Swift 6.0/6.1, data-race safety was enforced at compile time, but the most
23
+ natural code to write often produced data-race errors. Async functions on types
24
+ with mutable state would implicitly hop to the global concurrent executor,
25
+ causing send-safety violations even when no actual parallelism was intended.
26
+
27
+ ```swift
28
+ // Swift 6.0/6.1: This produces a data-race error
29
+ class PhotoProcessor {
30
+ func extractSticker(data: Data, with id: String?) async -> Sticker? { /* ... */ }
31
+ }
32
+
33
+ @MainActor
34
+ final class StickerModel {
35
+ let photoProcessor = PhotoProcessor()
36
+
37
+ func extractSticker(_ item: PhotosPickerItem) async throws -> Sticker? {
38
+ guard let data = try await item.loadTransferable(type: Data.self) else { return nil }
39
+ // Error: Sending 'self.photoProcessor' risks causing data races
40
+ return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier)
41
+ }
42
+ }
43
+ ```
44
+
45
+ ```swift
46
+ // Swift 6.2: The same code compiles without error
47
+ // because extractSticker stays on the caller's actor
48
+ class PhotoProcessor {
49
+ func extractSticker(data: Data, with id: String?) async -> Sticker? { /* ... */ }
50
+ }
51
+
52
+ @MainActor
53
+ final class StickerModel {
54
+ let photoProcessor = PhotoProcessor()
55
+
56
+ func extractSticker(_ item: PhotosPickerItem) async throws -> Sticker? {
57
+ guard let data = try await item.loadTransferable(type: Data.self) else { return nil }
58
+ return await photoProcessor.extractSticker(data: data, with: item.itemIdentifier)
59
+ }
60
+ }
61
+ ```
62
+
63
+ ## SE-0466: Default MainActor Isolation
64
+
65
+ Enable with the `-default-isolation MainActor` compiler flag or the Xcode 26
66
+ "Approachable Concurrency" build setting.
67
+
68
+ **What it does:**
69
+ - All declarations in the module are implicitly `@MainActor` unless opted out.
70
+ - Global and static variables are protected by the main actor by default.
71
+ - Protocol conformances are implicitly isolated to `@MainActor`.
72
+ - Eliminates most annotation burden for single-threaded UI code.
73
+
74
+ **Recommended for:** Apps, scripts, and executable targets. Not recommended for
75
+ library targets that should remain actor-agnostic.
76
+
77
+ ```swift
78
+ // With default MainActor isolation -- no @MainActor annotations needed:
79
+ final class StickerLibrary {
80
+ static let shared = StickerLibrary()
81
+ }
82
+
83
+ final class StickerModel {
84
+ let photoProcessor = PhotoProcessor()
85
+ var selection: [PhotosPickerItem] = []
86
+ }
87
+
88
+ extension StickerModel: Exportable {
89
+ func export() { photoProcessor.exportAsPNG() }
90
+ }
91
+ ```
92
+
93
+ ## SE-0461: nonisolated(nonsending)
94
+
95
+ Nonisolated async functions stay on the caller's actor by default instead of
96
+ hopping to the global concurrent executor. This is the `nonisolated(nonsending)`
97
+ default behavior.
98
+
99
+ **Key implication:** Values passed into an async function are never sent outside
100
+ the actor, eliminating data races without annotation.
101
+
102
+ To explicitly opt into background execution, use `@concurrent`.
103
+
104
+ ## @concurrent Attribute
105
+
106
+ Ensures a function always runs on the concurrent thread pool, freeing the
107
+ calling actor for other work.
108
+
109
+ ```swift
110
+ class PhotoProcessor {
111
+ var cachedStickers: [String: Sticker] = [:]
112
+
113
+ func extractSticker(data: Data, with id: String) async -> Sticker {
114
+ if let sticker = cachedStickers[id] { return sticker }
115
+ let sticker = await Self.extractSubject(from: data)
116
+ cachedStickers[id] = sticker
117
+ return sticker
118
+ }
119
+
120
+ @concurrent
121
+ static func extractSubject(from data: Data) async -> Sticker { /* ... */ }
122
+ }
123
+ ```
124
+
125
+ **Steps to offload a function to background:**
126
+ 1. Ensure the containing type is `nonisolated` (or the function itself).
127
+ 2. Add `@concurrent` to the function.
128
+ 3. Add `async` if not already asynchronous.
129
+ 4. Add `await` at call sites.
130
+
131
+ ```swift
132
+ nonisolated struct PhotoProcessor {
133
+ @concurrent
134
+ func process(data: Data) async -> ProcessedPhoto? { /* ... */ }
135
+ }
136
+
137
+ processedPhotos[item.id] = await PhotoProcessor().process(data: data)
138
+ ```
139
+
140
+ ## SE-0472: Task.immediate
141
+
142
+ `Task.immediate` starts executing synchronously on the current actor before any
143
+ suspension point, rather than being enqueued. There is also
144
+ `Task.immediateDetached` which combines immediate start with detached semantics.
145
+
146
+ ```swift
147
+ Task.immediate { await handleUserInput() }
148
+ ```
149
+
150
+ Use for latency-sensitive work where enqueue delay is unacceptable.
151
+
152
+ ## Isolated Conformances
153
+
154
+ A conformance that needs MainActor state is called an *isolated conformance*.
155
+ The compiler ensures the conformance is only used in a matching isolation
156
+ context.
157
+
158
+ ```swift
159
+ protocol Exportable {
160
+ func export()
161
+ }
162
+
163
+ extension StickerModel: @MainActor Exportable {
164
+ func export() { photoProcessor.exportAsPNG() }
165
+ }
166
+
167
+ @MainActor
168
+ struct ImageExporter {
169
+ var items: [any Exportable]
170
+
171
+ mutating func add(_ item: StickerModel) {
172
+ items.append(item) // OK -- on MainActor
173
+ }
174
+ }
175
+
176
+ // But in a nonisolated context:
177
+ nonisolated struct GenericExporter {
178
+ var items: [any Exportable]
179
+
180
+ mutating func add(_ item: StickerModel) {
181
+ // Error: Main actor-isolated conformance of 'StickerModel' to
182
+ // 'Exportable' cannot be used in nonisolated context
183
+ items.append(item)
184
+ }
185
+ }
186
+ ```
187
+
188
+ ## SE-0481: weak let (Proposed — Swift 6.2+)
189
+
190
+ Immutable weak references (`weak let`) enable `Sendable` conformance for types
191
+ that hold weak references, since immutability guarantees thread safety. Proposed
192
+ in SE-0481; may not yet be available in shipping toolchains.
193
+
194
+ ## SE-0475: Transactional Observation (Observations)
195
+
196
+ `Observations { }` provides transactional observation of `@Observable` types
197
+ via `AsyncSequence`.
198
+
199
+ ```swift
200
+ for await _ in Observations { model.count } {
201
+ print("Count changed to \(model.count)")
202
+ }
203
+ ```
204
+
205
+ ## Global and Static State
206
+
207
+ Global and static variables are prone to data races. The most common protection
208
+ is `@MainActor`:
209
+
210
+ ```swift
211
+ @MainActor
212
+ final class StickerLibrary {
213
+ static let shared = StickerLibrary() // protected by MainActor
214
+ }
215
+ ```
216
+
217
+ With default MainActor isolation (SE-0466), this annotation is implicit.
218
+
219
+ ## Migration and Build Settings
220
+
221
+ All approachable concurrency features are opt-in via:
222
+ - **Xcode 26:** Swift Compiler > Concurrency section in build settings.
223
+ - **SwiftPM:** `swiftSettings` in Package.swift using the `SwiftSetting` API.
224
+
225
+ Swift 6.2 includes migration tooling to help make necessary code changes
226
+ automatically. See swift.org/migration for details.
227
+
228
+ ## Summary
229
+
230
+ The Swift 6.2 concurrency progression:
231
+ 1. Start with code that runs on the main actor by default (no data race risk).
232
+ 2. Async functions run wherever they are called from (still no data race risk).
233
+ 3. When you need performance, offload specific code with `@concurrent`.
@@ -0,0 +1,187 @@
1
+ # SwiftUI Concurrency Guide
2
+
3
+ Concurrency patterns and best practices specific to SwiftUI applications.
4
+
5
+ ## Contents
6
+
7
+ - [MainActor Default in SwiftUI](#mainactor-default-in-swiftui)
8
+ - [Where SwiftUI Runs Code Off the Main Thread](#where-swiftui-runs-code-off-the-main-thread)
9
+ - [Sendable Closures and Data-Race Safety](#sendable-closures-and-data-race-safety)
10
+ - [Structuring Async Work](#structuring-async-work)
11
+ - [The .task Modifier](#the-task-modifier)
12
+ - [@Observable View Models](#observable-view-models)
13
+ - [Async Observation with Observations (SE-0475)](#async-observation-with-observations-se-0475)
14
+ - [Performance-Driven Concurrency](#performance-driven-concurrency)
15
+ - [Common SwiftUI Concurrency Mistakes](#common-swiftui-concurrency-mistakes)
16
+
17
+ ## MainActor Default in SwiftUI
18
+
19
+ - `View` is `@MainActor` isolated by default; `body` and all members inherit
20
+ this isolation.
21
+ - Swift 6.2 can infer `@MainActor` for all types in a module via default actor
22
+ isolation (SE-0466).
23
+ - This default aligns with UIKit/AppKit `@MainActor` APIs and simplifies UI
24
+ code.
25
+
26
+ ## Where SwiftUI Runs Code Off the Main Thread
27
+
28
+ SwiftUI may evaluate some view logic on background threads for performance:
29
+
30
+ - `Shape` path generation
31
+ - `Layout` methods (`sizeThatFits`, `placeSubviews`)
32
+ - `visualEffect` closures
33
+ - `onGeometryChange` closures
34
+
35
+ These APIs often require `Sendable` closures to reflect their off-main-thread
36
+ runtime semantics.
37
+
38
+ ## Sendable Closures and Data-Race Safety
39
+
40
+ Accessing `@MainActor` state from a `Sendable` closure is unsafe and flagged by
41
+ the compiler.
42
+
43
+ **Fix:** Capture value copies in the closure capture list.
44
+
45
+ ```swift
46
+ // WRONG: Captures @MainActor state directly
47
+ .visualEffect { content, proxy in
48
+ content.offset(y: self.offset) // Error: @MainActor state in Sendable closure
49
+ }
50
+
51
+ // CORRECT: Capture a copy
52
+ let currentOffset = offset
53
+ // ... use in closure:
54
+ .visualEffect { [currentOffset] content, proxy in
55
+ content.offset(y: currentOffset)
56
+ }
57
+ ```
58
+
59
+ Avoid sending `self` into a `Sendable` closure just to read a single property.
60
+
61
+ ## Structuring Async Work
62
+
63
+ SwiftUI action callbacks are synchronous so UI updates (like loading states) can
64
+ be immediate.
65
+
66
+ ```swift
67
+ struct ContentView: View {
68
+ @State private var isLoading = false
69
+ @State private var result: String?
70
+
71
+ var body: some View {
72
+ Button("Load") {
73
+ isLoading = true // Immediate UI update
74
+ Task {
75
+ result = await fetchData()
76
+ isLoading = false
77
+ }
78
+ }
79
+ }
80
+ }
81
+ ```
82
+
83
+ **Pattern:** Use state as the boundary. Async work updates model/state; UI
84
+ reacts synchronously.
85
+
86
+ ## The .task Modifier
87
+
88
+ Prefer `.task` over manual `Task` creation in views:
89
+
90
+ ```swift
91
+ .task {
92
+ await loadInitialData()
93
+ }
94
+ ```
95
+
96
+ **Advantages:**
97
+ - Automatically cancels on view disappear.
98
+ - Inherits the view's actor isolation (`@MainActor`).
99
+ - No need to store `Task` references for cancellation.
100
+
101
+ Use `.task(id:)` to restart work when a value changes:
102
+
103
+ ```swift
104
+ .task(id: selectedItem) {
105
+ details = await fetchDetails(for: selectedItem)
106
+ }
107
+ ```
108
+
109
+ ## @Observable View Models
110
+
111
+ - Annotate view models with both `@Observable` and `@MainActor`.
112
+ - Use `@State` to own an `@Observable` instance (replaces `@StateObject`).
113
+ - Avoid `@ObservedObject` / `@StateObject` / `ObservableObject` in new code.
114
+
115
+ ```swift
116
+ @Observable @MainActor
117
+ final class ViewModel {
118
+ var items: [Item] = []
119
+ var isLoading = false
120
+
121
+ func load() async {
122
+ isLoading = true
123
+ items = await fetchItems()
124
+ isLoading = false
125
+ }
126
+ }
127
+
128
+ struct ItemListView: View {
129
+ @State private var viewModel = ViewModel()
130
+
131
+ var body: some View {
132
+ List(viewModel.items) { item in
133
+ Text(item.name)
134
+ }
135
+ .task { await viewModel.load() }
136
+ }
137
+ }
138
+ ```
139
+
140
+ ## Async Observation with Observations (SE-0475)
141
+
142
+ Use `Observations { }` for transactional async observation:
143
+
144
+ ```swift
145
+ .task {
146
+ for await _ in Observations { viewModel.searchText } {
147
+ await viewModel.performSearch()
148
+ }
149
+ }
150
+ ```
151
+
152
+ ## Performance-Driven Concurrency
153
+
154
+ - Offload expensive work from the main actor to avoid hitches.
155
+ - Keep time-sensitive UI logic (animations, gesture responses) synchronous.
156
+ - Separate UI code from long-running async work.
157
+
158
+ ```swift
159
+ @Observable @MainActor
160
+ final class ImageProcessor {
161
+ var processedImage: UIImage?
162
+
163
+ func process(data: Data) async {
164
+ // Offload heavy work
165
+ let result = await Self.runProcessing(data: data)
166
+ processedImage = result
167
+ }
168
+
169
+ @concurrent
170
+ nonisolated static func runProcessing(data: Data) async -> UIImage {
171
+ // Runs on background thread pool
172
+ // ...
173
+ }
174
+ }
175
+ ```
176
+
177
+ ## Common SwiftUI Concurrency Mistakes
178
+
179
+ 1. **Creating `Task` in `body`.** Use `.task` modifier instead.
180
+ 2. **Not cancelling tasks.** `.task` does this automatically; manual `Task`
181
+ references must be cancelled in `onDisappear`.
182
+ 3. **Blocking MainActor in view updates.** Move heavy computation to
183
+ `@concurrent` functions.
184
+ 4. **Using `Task.detached` in views.** Loses actor context. Use `Task { }` or
185
+ `.task` modifier.
186
+ 5. **Updating state from background.** Always update `@State` / `@Observable`
187
+ properties on `@MainActor`.