fix(ai-outfit): 停止生成打断当前目录行 + 「停止中…」反馈 (§19.24)

修「开始生成→几秒后停止→两个按钮都灰几十秒像卡死」:非死锁,是温和停止
在等当前目录行收尾,而 _generate_directory_outfit 一次性 submit 全部图片、
shutdown(wait=True) 等全跑完,停止信号进不到该循环;且 _stop 后无反馈。

- outfit_batch:run() 经 _accepts_should_stop 给 generate_func 传 should_stop
  (= _stop_event.is_set),2-arg 才传、1-arg 仍兼容(含既有 mock)
- ai_outfit:generate_outfit_image/_generate_directory_outfit 增 should_stop;
  目录循环改有界提交——始终最多 image_concurrency 张在飞,每张完成后、提交
  下一张前查 should_stop,已停止则停止提交、在飞收尾即返回(结果注明
  「已停止,N 张未生成」,已生成的带 output_paths)
- 面板:_OutfitWorker.gen(task, should_stop) 透传;_stop() 把停止按钮改
  「停止中…」;_set_running(False) 复位「停止生成」

测试:目录行 should_stop 触发后只生成到停止点、返回「已停止」结果;2-arg
generate_func 收到 should_stop;既有用例(1-arg mock、目录并发/部分失败)保持绿。
全套 py37 通过(test_config_service 的 packaging 模板失败属并行 §19.13,无关)。
离屏冒烟:stop 截断 run 且 runner 及时返回;面板停止按钮「停止中…」→结束复位。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-23 16:46:49 +08:00
co-authored by Claude Opus 4.8
parent 61d8e20d86
commit 7313c8a443
7 changed files with 136 additions and 18 deletions
+22 -1
View File
@@ -1417,4 +1417,25 @@
- [x] `ai_text_service.py`:`_clean_titles(text)` 改为**按逗号拆分**——先把原始文本按 `,`/`,`(并把换行也当分隔,兜底)切成段,每段走 `_clean_title_line`(去首尾空白/序号/符号/引号),丢空,返回列表。`extract_titles_from_response`/`extract_text_from_response`(取首条)签名不变
- [x] `config_service.py`:`DEFAULT_TITLE_PROMPT` 改为「逗号分隔」风格——明确「标题之间用逗号分隔、不要 Markdown 表格/换行/序号/引号/表情」,数量由用户写
- [x] 测试:`test_ai_text_service.py` 更新/新增——逗号分隔(半角/全角)→ 多条;混换行+逗号→拆开;段内去序号/引号;Markdown 表格输入**不再**当多条(要么单条要么靠提示词避免,至少不产出表头那种垃圾的多条断言);`extract_text_from_response` 取首条
- [x] 验证:相关单测 + 全套 py37 通过;离屏冒烟:逗号分隔字符串 → 按序回填各行 A
- [x] 验证:相关单测 + 全套 py37 通过;离屏冒烟:逗号分隔字符串 → 按序回填各行 A
### 19.24 停止生成:打断当前目录行 + 「停止中…」反馈 — docs/11 §8
前置阅读:
- `docs/11-ai-outfit.md`(§8 并发/限速/重试/停止)
- `src/core/outfit_batch.py`(`OutfitBatchRunner.run`/`stop`/`_run_one_with_retry`/`_stop_event`)
- `src/core/ai_outfit.py`(`generate_outfit_image`/`_generate_directory_outfit` 的 `ThreadPoolExecutor` 一次性提交)
- `src/app/widgets/ai_outfit_panel.py`(`_OutfitWorker.run` 的 `gen` 闭包、`_stop`、`_set_running`)
背景:
点「开始生成」→ 几秒后点「停止生成」→ 之后几十秒两个按钮都灰、像卡死。根因(非死锁,会自行恢复):温和停止只 `set` `_stop_event`、不提交新行,但**当前目录行**的 `_generate_directory_outfit` 一次性把所有图片 submit 进线程池并 `shutdown(wait=True)` 等全部跑完(每张一次 AI 请求、读超时最高 600s),停止信号进不到这个循环;且 `_stop()` 后无 UI 反馈。已确认修复方向:快速停止 + 反馈。
任务:
- [x] `core/outfit_batch.py`:`run()` 调 `generate_func` 时传 `should_stop=self._stop_event.is_set`;`_run_one_with_retry(task)` → `_run_one_with_retry(task)` 内 `self.generate_func(task, should_stop)`(或闭包捕获),向下传递
- [x] `core/ai_outfit.py`:`generate_outfit_image(..., should_stop=None)` 透传;`_generate_directory_outfit` 改**有界提交**——始终最多 `image_concurrency` 张在飞,每张完成后、提交下一张前检查 `should_stop()`,已停止则停止提交剩余、让在飞收尾返回;结果 error 注明「已停止,N 张未生成」,`output_paths` 带已生成的;单文件行进入前检查一次 `should_stop()`(已停止→跳过结果)
- [x] `ai_outfit_panel.py`:`_OutfitWorker.gen(task, should_stop)` 闭包接新参并透传 `generate_outfit_image`;`_stop()` 把 `停止生成` 文案改「停止中…」(保持禁用);`_set_running(False)` 复位文案为「停止生成」
- [x] 测试:`test_ai_outfit.py` —— 目录行设 `should_stop` 在第 k 张后置真 → 之后不再新增生成、返回「已停止」结果且已生成的在 `output_paths`;`test_outfit_batch.py` —— 停止后不起新行、当前行提前返回、`finished` 及时;既有用例保持绿(注意 `generate_func` 新签名向后兼容/同步改 mock)
- [x] 验证:相关单测 + 全套 py37 通过;离屏冒烟:mock 计数/慢 `generate`,开跑后置 stop → 总调用被 `should_stop` 截断、`finished` 及时、`_set_running(False)` 恢复「开始生成」、停止按钮文案回「停止生成」