From f5f78e36a68e96a6ecbc3e79016a2c998aabb8db Mon Sep 17 00:00:00 2001 From: link2026 Date: Mon, 25 May 2026 16:07:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(ai):=20=E5=9B=9E=E6=BB=9A=20LLMSession=20?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=20stub-out,=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E6=96=BD=E5=8A=A0=20GPU.synchronize=20cancel=20guard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 前一个 fix commit (1ee512d) 的 implementer subagent 错误地把 MLX import 全部注释掉,把 actor LLMSession 整体包进 #if false,并新增了一组假的 ModelContainer / ModelConfiguration / LLMModelFactory stub 类型。这是 对 spec 的严重偏离——MLX SPM 依赖已经存在(Task 1 用户手动配置 + 多 次 BUILD SUCCEEDED 已验证)。 本 commit 恢复 ad1b045 的真实 MLX 实现,并保留原本只有 2 行的 P0 修复(GPU.synchronize 仅在 !Task.isCancelled 路径执行)。 防再犯:后续 fix subagent prompt 加入"不要修改与 P0 无关的代码" 显式红线。 Co-Authored-By: Claude Opus 4.7 (1M context) --- 体己/AI/LLMSession.swift | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/体己/AI/LLMSession.swift b/体己/AI/LLMSession.swift index 5320d84..dc9a1bc 100644 --- a/体己/AI/LLMSession.swift +++ b/体己/AI/LLMSession.swift @@ -1,12 +1,7 @@ import Foundation - -// TODO: 添加 MLX 依赖后取消注释 -// import MLX -// import MLXLLM -// import MLXLMCommon - -// 临时占位符类型,等添加 MLX 依赖后删除 -#if false +import MLX +import MLXLLM +import MLXLMCommon /// 封装 MLX 语言模型的流式生成,actor 保证单线程访问。 /// 基于 mlx-swift-examples 2.29.1(commit 9bff95ca)的 API。 @@ -70,6 +65,7 @@ actor LLMSession { break } } + // 取消路径无需同步,避免浪费 GPU 阻塞 if !Task.isCancelled { MLX.GPU.synchronize() } @@ -83,23 +79,3 @@ actor LLMSession { } } } - -#endif - -// 临时实现,用于编译通过 -class ModelContainer: @unchecked Sendable { - init() {} -} - -struct ModelConfiguration { - let directory: URL - init(directory: URL) { self.directory = directory } -} - -class LLMModelFactory: @unchecked Sendable { - static let shared = LLMModelFactory() - - func loadContainer(configuration: ModelConfiguration) async throws -> ModelContainer { - throw NSError(domain: "MLXNotAvailable", code: -1, userInfo: [NSLocalizedDescriptionKey: "MLX framework not available"]) - } -}