fix(concurrency): nonisolated(unsafe) static shared + 修同 actor 内冗余 await

项目开启了 -default-isolation=MainActor upcoming feature,导致:

1. static let shared 默认被视为 MainActor 隔离,即使 class 标了
   @unchecked Sendable,从其他 actor(如 AIRuntime)同步访问仍报
   "Expression is 'async' but is not marked with 'await'".

   修法:ModelStore.shared 和 FileVault.shared 都加 nonisolated(unsafe)
   修饰,明确"任何隔离上下文都可同步访问"。

2. AIRuntime.generate() 内的 Task { ... } 继承 AIRuntime actor 隔离,
   self.recordRate 是同 actor 内部调用,不需要 await,否则报
   "No 'async' operations occur within 'await' expression".

   修法:去掉冗余的 await。

** BUILD SUCCEEDED ** 已验证。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
link2026
2026-05-25 17:00:30 +08:00
parent 9fbd31458c
commit acfdaa1f4f
3 changed files with 8 additions and 3 deletions

View File

@@ -80,7 +80,9 @@ actor AIRuntime {
// session.generate actor , await
let stream = await session.generate(prompt: prompt, maxTokens: maxTokens)
for try await chunk in stream {
await self.recordRate(chunk.decodeRate)
// Task generate() , AIRuntime actor ;
// actor recordRate await
self.recordRate(chunk.decodeRate)
continuation.yield(chunk)
}
continuation.finish()

View File

@@ -21,8 +21,10 @@ enum ModelKind: String, CaseIterable {
/// `@unchecked Sendable`:rootURL let, filesystem(线),
/// actor / Task 访
/// `nonisolated(unsafe) shared`: `-default-isolation=MainActor`
/// static MainActor , actor 访 await opt-out
final class ModelStore: @unchecked Sendable {
static let shared: ModelStore = {
nonisolated(unsafe) static let shared: ModelStore = {
do {
let appSupport = try FileManager.default.url(
for: .applicationSupportDirectory,

View File

@@ -10,8 +10,9 @@ enum FileVaultError: Error {
/// `@unchecked Sendable`:rootURL let, I/O (线),
/// actor / Task 访
/// `nonisolated(unsafe) shared`: ModelStore
final class FileVault: @unchecked Sendable {
static let shared: FileVault = {
nonisolated(unsafe) static let shared: FileVault = {
do {
let appSupport = try FileManager.default.url(
for: .applicationSupportDirectory,