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

@@ -0,0 +1,72 @@
"""验证:`ask_user` 弹窗**不限时**(用户要求"AI 询问界面不要限时")。
## 怎么验
以前是 120 秒到点自己消失。所以要证明"改了",必须**跨过 120 秒那条线**:
调一次 ask_user,然后在 5 秒 / 130 秒两个时刻截图:
· 5 秒:弹窗在(说明调通了)
· 130 秒:**弹窗还在**(说明真的不限时了。旧版这时候已经没了)
· 再点一下选项:请求返回,`timeout` 是 false、`choice` 是点的那个
★ 这个脚本会**阻塞到用户点为止**(那正是它要测的东西),所以必须后台跑。
用法:
adb forward tcp:15199 tcp:5199
python scripts/probe-ask-user-forever.py
"""
import json
import sys
import time
import urllib.request
try:
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
URL = "http://127.0.0.1:15199"
def call(path, payload=None, method="POST", timeout=900):
data = json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload is not None else None
req = urllib.request.Request(
URL + path, data=data, method=method,
headers={"Content-Type": "application/json; charset=utf-8"},
)
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read().decode("utf-8"))
def main():
print("调 ask_user —— 这个请求会一直挂着,直到有人在弹窗上点一下", flush=True)
t0 = time.time()
try:
r = call("/api/tools/run", {
"name": "ask_user",
"args": {
"question": "不限时测试:随便点一个(我不催你)",
"options": [
{"label": "选项 A", "value": "A"},
{"label": "选项 B", "value": "B"},
{"label": "选项 C", "value": "C"},
],
},
}, timeout=900)
except Exception as exc: # noqa: BLE001
print(f"!! 请求失败({time.time() - t0:.1f} 秒后):{exc}", flush=True)
return 1
dt = time.time() - t0
print(f"\n等了 {dt:.1f} 秒才返回(旧版 120 秒就会 timeout)", flush=True)
print(json.dumps(r, ensure_ascii=False)[:500], flush=True)
if dt > 125 and not (r.get("result") or {}).get("timeout"):
print("\n✅ 不限时生效:超过 120 秒仍然等到了用户的选择", flush=True)
return 0
print("\n? 结果需要人工看一眼(可能点得太快,或 timeout 语义不对)", flush=True)
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -295,6 +295,76 @@ else:
else:
fail("run_agent 没按开关追加提示词")
# --------------------------------------------------------------------------- #
# ★★ 会等用户的工具必须**豁免工具超时**
# --------------------------------------------------------------------------- #
# 用户要求"AI 询问界面不要限时"。这个需求**要三处一起改才算数**:
# ① 界面侧等多久(AiSurface.ask / showScreen)
# ② 内核侧给工具多少时间(tools_runtime 的 TOOL_TIMEOUT_S)
# ③ 工具自己声明的 timeout
# 只改 ①③ 的后果实测过:弹窗 135 秒还开着,**但工具调用 60 秒就被内核判超时**,
# 模型收到"工具执行超时(60s)",用户五分钟后点的选项根本回不到模型手里 ——
# 表现是"弹窗还在、AI 已经自己往下写了"。这种"看起来改了其实没生效"最坑,
# 所以在这里钉住。
print("\n=== 等用户的工具不被工具超时掐掉 ===")
import inspect # noqa: E402 - 上面那个分支里才 import,这里保证一定有
import tools_runtime as TR
for tool_name in ("ask_user", "show_ui"):
path = os.path.join(TOOLS, f"{tool_name}.py")
src = open(path, encoding="utf-8").read()
if "WAIT_FOR_USER = True" in src:
ok(f"{tool_name}.py 声明了 WAIT_FOR_USER = True")
else:
fail(f"{tool_name}.py 没声明 WAIT_FOR_USER —— 内核会在 60 秒时把这次调用判超时")
if hasattr(TR, "_waits_for_user"):
ok("tools_runtime 里有 _waits_for_user")
else:
fail("tools_runtime 没有 _waits_for_user,豁免逻辑不见了")
_src = inspect.getsource(TR.run_python_tool)
if "_waits_for_user" in _src and "budget" in _src:
ok("run_python_tool 是按它决定超时预算的")
else:
fail("run_python_tool 没用 _waits_for_user 决定预算")
# 引擎侧:0 必须真的等于"不限时"
import builtin_tools as BT
if "thread.join()" in inspect.getsource(BT.run_with_timeout):
ok("run_with_timeout 支持 timeout<=0 = 不限时")
else:
fail("run_with_timeout 不支持不限时(join 那支不见了)")
# 仿真一遍:给一个"永远不返回"的任务配 0 预算,它必须**不返回**
# (真跑起来要等到天荒地老,所以这里用一个 20ms 后自己结束的任务代替,
# 验证的是"0 走的是无限等待那支"而不是"0 立刻超时")
import threading as _th
_done = _th.Event()
def _slow():
_done.wait(0.05)
return "ok"
_r = BT.run_with_timeout(_slow, 0)
if _r == "ok":
ok("预算 0 时会一直等到任务真的跑完(不是立刻超时)")
else:
fail(f"预算 0 的行为不对,拿到 {_r!r}")
# 反面:非豁免工具仍然是 60 秒(安全网还在,别为了这个把超时全关了)
if TR.TOOL_TIMEOUT_S == 60:
ok("普通工具的超时还是 60 秒(死循环的安全网没丢)")
else:
fail(f"TOOL_TIMEOUT_S 被改成 {TR.TOOL_TIMEOUT_S},普通工具的兜底变了")
print()
if fails:
print(f"❌ {len(fails)} 项不合格:")