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

@@ -30,8 +30,8 @@ android {
* versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装",
* 改小了会装不上)。
*/
versionCode = 244
versionName = "2.0.44"
versionCode = 245
versionName = "2.0.45"
ndk {
// Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份

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')

View File

@@ -35,6 +35,33 @@ _loaded_modules: dict = {}
TOOL_TIMEOUT_S = 60
BUILTIN_FILE_LABEL = '<内置>'
# --------------------------------------------------------------------------- #
# 会"停下来等用户"的工具:执行超时对它们不适用
# --------------------------------------------------------------------------- #
#
# ★★ 这个坑很隐蔽,记下来:
#
# 用户要求"AI 询问界面不要限时",我把弹窗那侧改成了无限等(`AiSurface.ask`),
# 实测弹窗确实 135 秒还在 —— **但工具调用在 60 秒时就被内核判超时了**,
# 模型收到的是"工具执行超时(60s)",用户五分钟后点的那个选项**根本回不到
# 模型手里**。表现就是"弹窗还在、但 AI 已经自己往下写了"。
#
# 所以"不限时"必须**三处一起改**才算数:
# ① 界面侧等多久(AiSurface)
# ② 内核侧给工具多少时间(这里)
# ③ 工具自己声明的 timeout(tools/ask_user.py、tools/show_ui.py)
#
# 判定方式:工具模块里声明 `WAIT_FOR_USER = True`。
# 放在工具文件里而不是在这个列表里硬编码 —— 加新工具时不会漏(写工具的人
# 自己知道它会不会等用户)。
def _waits_for_user(name: str, spec: dict, module) -> bool:
"""这个工具会不会停下来等用户交互"""
if spec.get('_builtin'):
return bool((BUILTIN_TOOLS.get(name) or {}).get('WAIT_FOR_USER'))
return bool(getattr(module, 'WAIT_FOR_USER', False))
# --------------------------------------------------------------------------- #
# 界面工具的开关
# --------------------------------------------------------------------------- #
@@ -320,8 +347,12 @@ def run_python_tool(name: str, args: dict) -> dict:
_ctx.value = None
try:
# 进程内执行同样加超时:工具里死循环时至少让对话能返回错误,而不是整个内核卡死
outcome = run_with_timeout(_invoke, TOOL_TIMEOUT_S)
# 进程内执行同样加超时:工具里死循环时至少让对话能返回错误,而不是整个内核卡死。
# ★ 会等用户的工具(ask_user / show_ui 的 wait 模式)**不限时** ——
# 它们的耗时取决于用户什么时候点,给上限等于"用户想久了 AI 就自己编"。
# 见 _waits_for_user 的注释。
budget = 0 if _waits_for_user(name, spec, module) else TOOL_TIMEOUT_S
outcome = run_with_timeout(_invoke, budget)
except BaseException as exc: # noqa: BLE001 - 工具抛错要当成"工具失败",不是接口 500
# 与 Node 版一致:_protocol 把异常转成 {"ok":false,"error":"ValueError: xxx"},
# 这里在进程内直接捕获,格式保持一致(前端/模型看到的东西不变)。