fix: "等用户的工具"要豁免 60 秒的工具执行超时(v2.0.44 的"不限时"只做了一半)

用户要求"AI 询问界面不要限时"。v2.0.44 改了界面侧(AiSurface 无限等)和
工具侧(ask_user 的 timeout 默认 0),但**漏了内核的工具执行超时**:

  实测:弹窗 135 秒确实还在,但工具调用在 60.0 秒返回
  {"ok":false,"error":"工具执行超时(60s)"} —— 用户后来点的选项根本
  回不到模型手里,模型已经拿着"超时"自己往下写了。

改动:
- builtin_tools.run_with_timeout:timeout_s <= 0 = 不限时(thread.join() 不带参)
- tools/ask_user.py、tools/show_ui.py:声明 WAIT_FOR_USER = True
- tools_runtime:新增 _waits_for_user(),会等用户的工具把超时预算设成 0;
  普通工具仍是 60 秒(死循环的安全网没丢)

验证(scripts/probe-ask-user-forever.py,真机跑):
  修之前 60.0 秒 → {"ok":false,"error":"工具执行超时(60s)"}
  修之后 66.2 秒 → {"ok":true,"result":{...,"choice":"B","timeout":false}}

守门:scripts/verify-ui-tools.py 新增一节(声明在不在、豁免逻辑在不在、
预算 0 是不是真的无限等、普通工具的 60 秒还在不在)。

文档:docs/AI界面能力.md 补"不限时要三处一起改"和客户端长连接为什么不断
(15 秒心跳 vs 120 秒 readTimeout)。

版本 2.0.45。
This commit is contained in:
l
2026-10-16 09:42:13 +08:00
parent 2c259d4270
commit 4850ea12af
9 changed files with 252 additions and 14 deletions

View File

@@ -248,6 +248,17 @@ def run_with_timeout(fn, timeout_s: float):
而且 CPython 根本杀不掉一个正在跑的线程。超时后我们只能"放弃等待",
被放弃的线程会继续在后台跑完(daemon 线程,不阻塞进程退出)。
这个取舍必须让用户知道,所以超时消息里写清楚。
★★ `timeout_s <= 0` = **不限时**(一直等到它跑完)。
给"停下来等用户"的那类工具用(`ask_user` / `show_ui` 的 wait 模式)——
它们的耗时**完全取决于用户什么时候点**,60 秒的上限只会造成:
弹窗还开着、用户 5 分钟后才选,而模型早就收到"工具执行超时"、
于是按自己猜的答案往下写了。
为什么默认仍然是 60 秒:这是"工具里写了死循环"的安全网
(不让整个内核跟着卡死)。会等用户的工具属于**设计上就该慢**,
由工具自己声明 `WAIT_FOR_USER = True` 来豁免,见 tools_runtime。
"""
box = {}
@@ -259,9 +270,13 @@ def run_with_timeout(fn, timeout_s: float):
thread = threading.Thread(target=worker, name='doubao-python-run', daemon=True)
thread.start()
thread.join(timeout_s)
if thread.is_alive():
return None
if timeout_s and timeout_s > 0:
thread.join(timeout_s)
if thread.is_alive():
return None
else:
# 不限时:join() 不带参数就是一直等
thread.join()
if 'error' in box:
raise box['error']
return box.get('value')