|
@@ -30,8 +30,10 @@ _DEFAULT_ALLOWED_TOOLS = [
|
|
|
"Read", "Write", "Edit", "Bash", "Glob", "Grep", "NotebookEdit", "TodoWrite",
|
|
"Read", "Write", "Edit", "Bash", "Glob", "Grep", "NotebookEdit", "TodoWrite",
|
|
|
]
|
|
]
|
|
|
_DEFAULT_FIRST_BYTE_TIMEOUT_S = 90.0 # 原生长跑首个事件可能稍慢(要先想)
|
|
_DEFAULT_FIRST_BYTE_TIMEOUT_S = 90.0 # 原生长跑首个事件可能稍慢(要先想)
|
|
|
-_DEFAULT_IDLE_TIMEOUT_S = 180.0 # 工具间隔(跑实验/编译)可能较长
|
|
|
|
|
-_DEFAULT_HARD_TIMEOUT_S = 1800.0 # 30 分钟硬顶
|
|
|
|
|
|
|
+# idle 只在**没有工具在飞**时生效(模型 turn 间静默)。工具在飞时(Bash 跑实验/
|
|
|
|
|
+# pdflatex 编译可达数分钟,期间 stream-json 完全静默)不按 idle 杀——靠硬超时兜底。
|
|
|
|
|
+_DEFAULT_IDLE_TIMEOUT_S = 300.0 # 模型 turn 间最长静默(无工具在飞)
|
|
|
|
|
+_DEFAULT_HARD_TIMEOUT_S = 3600.0 # 60 分钟总硬顶(重任务:重做实验+出 PDF)
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
@dataclass
|
|
@@ -46,6 +48,7 @@ class NativeResult:
|
|
|
num_turns: int = 0
|
|
num_turns: int = 0
|
|
|
is_error: bool = False
|
|
is_error: bool = False
|
|
|
trace: List[dict] = field(default_factory=list)
|
|
trace: List[dict] = field(default_factory=list)
|
|
|
|
|
+ pending_tools: int = 0 # 在飞工具数(tool_use 未等到 tool_result);>0 时禁 idle 超时
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_cmd(claude_bin: str, prompt: str, *, cwd: str, model: str,
|
|
def build_cmd(claude_bin: str, prompt: str, *, cwd: str, model: str,
|
|
@@ -93,6 +96,7 @@ def _handle_event(obj: dict, on_event, result: NativeResult):
|
|
|
if c.get("type") == "text" and c.get("text"):
|
|
if c.get("type") == "text" and c.get("text"):
|
|
|
_emit(on_event, "think_chunk", {"text": c["text"]})
|
|
_emit(on_event, "think_chunk", {"text": c["text"]})
|
|
|
elif c.get("type") == "tool_use":
|
|
elif c.get("type") == "tool_use":
|
|
|
|
|
+ result.pending_tools += 1 # 工具开飞 → 禁 idle 超时直到结果回来
|
|
|
_emit(on_event, "tool_call",
|
|
_emit(on_event, "tool_call",
|
|
|
{"tool": c.get("name", ""), "input": c.get("input", {})})
|
|
{"tool": c.get("name", ""), "input": c.get("input", {})})
|
|
|
result.trace.append({"tool": c.get("name", ""),
|
|
result.trace.append({"tool": c.get("name", ""),
|
|
@@ -101,6 +105,7 @@ def _handle_event(obj: dict, on_event, result: NativeResult):
|
|
|
if t == "user":
|
|
if t == "user":
|
|
|
for c in (obj.get("message", {}) or {}).get("content", []) or []:
|
|
for c in (obj.get("message", {}) or {}).get("content", []) or []:
|
|
|
if isinstance(c, dict) and c.get("type") == "tool_result":
|
|
if isinstance(c, dict) and c.get("type") == "tool_result":
|
|
|
|
|
+ result.pending_tools = max(0, result.pending_tools - 1)
|
|
|
out = c.get("content", "")
|
|
out = c.get("content", "")
|
|
|
if isinstance(out, list):
|
|
if isinstance(out, list):
|
|
|
out = " ".join(x.get("text", "") for x in out if isinstance(x, dict))
|
|
out = " ".join(x.get("text", "") for x in out if isinstance(x, dict))
|
|
@@ -188,11 +193,15 @@ def run_native(prompt: str, *, cwd: str, model: str = "sonnet",
|
|
|
raise ProviderError(
|
|
raise ProviderError(
|
|
|
f"claude-code native first-byte-timeout after {now-t0:.0f}s", "claude-code",
|
|
f"claude-code native first-byte-timeout after {now-t0:.0f}s", "claude-code",
|
|
|
retryable=True)
|
|
retryable=True)
|
|
|
- if last_byte_at is not None and now - last_byte_at > idle_timeout:
|
|
|
|
|
|
|
+ # idle 只在没有工具在飞时判定。工具在飞(Bash 跑实验/编译可达数分钟,
|
|
|
|
|
+ # 期间 stream-json 静默)时不杀,靠上面的 hard_timeout 兜底——否则会把
|
|
|
|
|
+ # 正常长跑命令误判成卡死(run_84f6fdb6f11b: idle-timeout after 181s)。
|
|
|
|
|
+ if (last_byte_at is not None and result.pending_tools == 0
|
|
|
|
|
+ and now - last_byte_at > idle_timeout):
|
|
|
proc.kill()
|
|
proc.kill()
|
|
|
raise ProviderError(
|
|
raise ProviderError(
|
|
|
- f"claude-code native idle-timeout after {now-last_byte_at:.0f}s", "claude-code",
|
|
|
|
|
- retryable=True)
|
|
|
|
|
|
|
+ f"claude-code native idle-timeout after {now-last_byte_at:.0f}s "
|
|
|
|
|
+ f"(no tool in flight)", "claude-code", retryable=True)
|
|
|
|
|
|
|
|
ready, _, _ = select.select([proc.stdout, proc.stderr], [], [], 0.5)
|
|
ready, _, _ = select.select([proc.stdout, proc.stderr], [], [], 0.5)
|
|
|
for fobj in ready:
|
|
for fobj in ready:
|