From 81e5227570393abe390c919f92dcab8bec5fb84d Mon Sep 17 00:00:00 2001 From: GenericAgent Date: Sat, 14 Nov 2026 10:46:37 +0800 Subject: [PATCH 1/6] fix(llmcore): reconstruct pseudo-XML tool calls and never execute a truncated one --- ga.py | 45 +++++- llmcore.py | 268 +++++++++++++++++++++++++++++++++++ tests/test_text_tool_call.py | 138 ++++++++++++++++++ 3 files changed, 450 insertions(+), 1 deletion(-) create mode 100644 tests/test_text_tool_call.py diff --git a/ga.py b/ga.py index 82c508994..71aa1fdac 100644 --- a/ga.py +++ b/ga.py @@ -464,8 +464,24 @@ def do_update_working_checkpoint(self, args, response): return StepOutcome({"result": "working key_info updated"}, next_prompt=next_prompt) def _retry_or_exit(self, prompt): + """Give up after 3 consecutive unusable replies — but say so first. + + Returning `StepOutcome({}, should_exit=True)` with nothing printed makes the + front end go quiet mid-task: the user sees the turn stop with no error and no + explanation. That is the same "silent stop" the guards above are meant to + prevent, so the give-up path must be visible too. + """ self._empty_ct = getattr(self, '_empty_ct', 0) + 1 - if self._empty_ct >= 3: return StepOutcome({}, should_exit=True) + if self._empty_ct >= 3: + note = (f"\n[任务中止] 模型连续 {self._empty_ct} 次未产出有效回复" + f"(空响应/仅 /工具格式无效),已停止本轮任务。\n" + f"建议: 重发该任务,或为该渠道降低 reasoning_effort / 提高 max_tokens。\n") + try: + self.history_info.append('[Agent] ' + note.strip().replace('\n', ' ')) + except Exception: + pass + yield note + return StepOutcome({}, should_exit=True) return StepOutcome({}, next_prompt=prompt) def do_no_tool(self, args, response): @@ -484,6 +500,33 @@ def do_no_tool(self, args, response): return self._retry_or_exit("[ERROR] Tool call format invalid; no tool was called. Regenerate using proper tool calls.") if 'max_tokens !!!]' in content[-100:]: return self._retry_or_exit("[ERROR] max_tokens limit reached. Use multi small steps to do it.") + + # 2026-11-11: 伪 XML 工具块冒充最终回答。 + # llmcore 认得出来的(成对闭合 + 参数可重建 + 脚本能通过语法校验)已在 + # `_parse_text_tool_calls` 里还原成真 tool_calls, 走到这里的一定是「想调工具但没调成」: + # unclosed = 流被掐断在半截(max_tokens / 流中断); + # empty = 块闭合但一个参数都没有(用户看到的 ``); + # orphan = 只有 没有工具名(`` 包裹的那种); + # invalid = 块闭合但重建出来的调用不可安全执行(半截脚本 / 语法错)。 + # 任何一条都不能当最终回答交付, 否则本轮任务静默结束且不报错。 + _residue = None + try: + import llmcore as _llmcore + _residue = _llmcore.textual_tool_residue(content) + except Exception: + _residue = None + if _residue: + _why = { + 'unclosed': 'an unterminated textual tool block (the stream was cut mid-call)', + 'empty': 'a textual tool block with no arguments', + 'orphan': 'textual parameters without a tool name', + 'invalid': 'a textual tool block that cannot be executed safely', + }.get(_residue, 'a textual tool block') + yield f"[Info] Detected {_why}. Regenerating.\n" + return self._retry_or_exit( + "[ERROR] Your tool call was emitted as plain XML text (%s) and no tool was executed. " + "Regenerate using the native tool-call interface — not ///DSML text, " + "and make sure the whole call is emitted in one piece." % _why) # 2. 检测"包含较大代码块但未调用工具"的情况 # 关键特征:恰好1个大代码块 + 代码块直接结尾(后面只有空白) diff --git a/llmcore.py b/llmcore.py index 04bdeffd0..b0cf42756 100644 --- a/llmcore.py +++ b/llmcore.py @@ -934,8 +934,276 @@ def _parse_text_tool_calls(content): if name: tcs.append(MockToolCall(name, args)) except: pass if tcs: content = re.sub(_xp, "", content, flags=re.DOTALL).strip() + if not tcs: + tcs, content = _parse_pseudo_xml_tool_calls(content) return tcs, content +# ==== 伪 XML 工具调用兜底 (2026-11-11) ================================== +# 现象: 部分上游不用原生 tool_calls, 而是把工具调用写成**伪 XML 正文**: +# \n\n +# \nprint(1)\n +# \na.py\n (名字后多一个引号) +# print(1) (Qwen 风格) +# <||DSML||tool_calls><||DSML||invoke name="code_run"><||DSML||parameter name="script">… (DeepSeek DSML) +# `_parse_text_tool_calls` 只认 ``/`` + JSON, 这些一个都认不出 +# ⇒ tool_calls 为空 ⇒ `ga.do_no_tool` 又一条判据都不命中 ⇒ 被当成**最终回答** +# ⇒ 本轮任务结束。用户观感 = 「冒出一坨标签然后对话自己中断了, 且不报错」。 +# +# 安全约束(宁可不解析, 不可乱执行): +# * 工具名必须在白名单内(assets/tools_schema.json + 常见别名), 正文里的任意 不猜; +# * 只认**成对闭合**的块 —— 流被掐断在半截时不猜参数; +# * ``` 围栏 / `行内代码` 内的内容不参与匹配 —— 模型讲解/写文档时提到这些标签不应被真执行; +# * 解析结果还要过 `salvaged_call_problem`(python 语法等) —— 压平成一行/半截的脚本 +# 宁可让模型重发, 也不能直接跑起来。 +# +# ★ 2026-11-11 PR #853 review 修复的三处缺口: +# C 参数名被写成 dsml_parameter —— 参数名解析与工具标签的引号风格解耦; +# F `` 还原(此前直接静默终答); +# H 截断的调用被当真正调用执行 —— 安全闸门挪到 `_parse_text_tool_calls` 返回之前, +# 而不是等 do_no_tool; 另外 `textual_tool_residue` 把「想调但调不成」的残渣 +# (unclosed/empty/orphan) 交给 ga 重试, 不再冒充最终回答。 +import ast + +_PSEUDO_XML_ALIASES = frozenset({ + 'code_run', 'file_read', 'file_write', 'file_patch', 'web_scan', 'web_execute_js', + 'web_search', 'web_fetch', 'update_working_checkpoint', 'start_long_term_update', + 'ask_user', 'all_urls', 'bad_json', 'computer', 'bash', 'powershell', +}) +_TOOL_NAMES_CACHE = None +# `||DSML||` 前缀(全角/半角竖线都见过), 可选 +_DSML = r'(?:\s*[\uff5c|]{1,2}\s*DSML\s*[\uff5c|]{1,2}\s*)?' +# 工具名与 '>' 之间的杂音: 多余引号/空白(实测 ``) +_NOISE = r'[ \t"\']{0,4}' +# 行首锚定 —— 正文里顺手提到 `` 不算泄漏, 真的泄漏总是独立成行 +_BOL = r'(?m)^[ \t]*' + + +def _known_tool_names(): + """白名单: 内置别名 ∪ assets/tools_schema.json 里声明的工具名。""" + global _TOOL_NAMES_CACHE + if _TOOL_NAMES_CACHE is not None: return _TOOL_NAMES_CACHE + names = set(_PSEUDO_XML_ALIASES) + try: + with open(os.path.join(_ROOT, 'assets', 'tools_schema.json'), encoding='utf-8') as f: + for t in json.load(f): + fn = t.get('function', t) or {} + if fn.get('name'): names.add(fn['name']) + except Exception: + pass + _TOOL_NAMES_CACHE = frozenset(names) + return _TOOL_NAMES_CACHE + + +def _mask_code(text): + """把 ``` 围栏 / `行内代码` 换成等长空格(保持偏移不变)。 + + ★哨兵不能用 0: 围栏正好从第 0 个字符开始时, `pos == 0` 会被误判成「还没开」, + 于是永远不闭合 —— 负例(整段被 ``` 包住)反而会被当真调用执行。 + """ + out = list(text) + for m in re.finditer(r'```[\s\S]*?(?:```|$)', text): + for i in range(m.start(), m.end()): out[i] = ' ' + for m in re.finditer(r'`[^`\n]*`', text): + for i in range(m.start(), m.end()): out[i] = ' ' + return ''.join(out) + + +class _Scan: + __slots__ = ('blocks', 'unclosed', 'orphans') + + def __init__(self): + self.blocks = [] # dict(start, end, name, body, attrs) + self.unclosed = [] # 有开无闭的块/参数(通常是流被掐断) + self.orphans = [] # 落在任何工具块之外的 (想调工具但没调成) + + +def _scan_pseudo_xml(content): + """扫描正文里的伪工具块。 + + 只看**行首**开标签(正文里 inline 提到标签不算), 围栏/行内代码内全部忽略。 + """ + sc = _Scan() + if not content: return sc + text = _mask_code(content) + names = _known_tool_names() + alt = '|'.join(sorted((re.escape(n) for n in names if n), key=len, reverse=True)) + + # 三种开标签: <工具名 ...> / / + opens = [] + for m in re.finditer(_BOL + r'<' + _DSML + r'(' + alt + r')\b' + _NOISE + r'([^<>]{0,200})>', text, re.I): + opens.append((m.start(), m.end(), m.group(1), m.group(2) or '', 'tag')) + for m in re.finditer(_BOL + r'', text, re.I): + opens.append((m.start(), m.end(), m.group(1), '', 'function')) + for m in re.finditer(_BOL + r'<' + _DSML + r'invoke\s+name\s*=\s*["\']?([A-Za-z_]\w*)["\']?\s*>', text, re.I): + opens.append((m.start(), m.end(), m.group(1), '', 'invoke')) + opens.sort() + + spans = [] + for start, end, name, attrs, form in opens: + if name not in names: continue + if any(a <= start < b for a, b in spans): continue # 嵌套在外层块里 + if form == 'function': + close_re = re.compile(r'', re.I) + elif form == 'invoke': + close_re = re.compile(r'', re.I) + else: + close_re = re.compile(r'', re.I) + c = close_re.search(text, end) + if c is None: + sc.unclosed.append(name); continue + body = content[end:c.start()] + spans.append((start, c.end())) + sc.blocks.append({'start': start, 'end': c.end(), 'name': name, 'body': body, 'attrs': attrs}) + + spans.sort() + # 落在所有工具块之外的 = 形状 D( 包 parameter): 没有任何工具名, + # 重建不了, 只能当残渣重试 —— 绝不能当最终回答交付。 + # 同样行首锚定: 正文里"用法: "这种说明句不该触发重试。 + for m in re.finditer(_BOL + r'<' + _DSML + r'parameter\b[^<>]*>', text, re.I): + if any(a <= m.start() < b for a, b in spans): continue + sc.orphans.append('parameter') + # 块内参数有开无闭 = 截断(例如 到了但 没到) + for b in sc.blocks: + if b['body'].count(' b['body'].count(']*>(.*?)', body, re.DOTALL | re.I) + if m: args.setdefault('script', m.group(1).strip('\n')) + # 参数名解析与工具标签的引号风格解耦: `parameter name="k"` / `parameter name='k'` / + # `parameter=k` / `parameter name=k` 全部认。标签上的 `string="true"` 等属性不参与。 + for pm in re.finditer(r'<' + _DSML + r'parameter\s*(?:name\s*)?=\s*["\']?([\w.-]+)["\']?[^<>]*>' + r'(.*?)', body, re.DOTALL | re.I): + k = pm.group(1) + if k and k.lower() not in ('string', 'true', 'false', 'type'): + args.setdefault(k, _coerce(k, pm.group(2).strip('\n'))) + if args: return args + b = (body or '').strip() + if not b: return {} + if b[0] in '{[': + try: + d = json.loads(b) + if isinstance(d, dict): return d + except Exception: + pass + if name in ('code_run', 'bash', 'powershell') and '<' not in b: + return {'script': b} + return {} + + +_SHELL_HEAD = re.compile( + r'^\s*(ls|cd|pwd|dir|echo|cat|git|pip|pip3|npm|curl|wget|ssh|scp|mkdir|rm|cp|mv|chmod|' + r'python3?|bash|sh|zsh|node|docker|kubectl)\b') + + +def salvaged_call_problem(call): + """抢救出来的调用能不能安全执行。返回问题描述, '' 表示没问题。 + + 宁可让模型重发, 也不能把半截/压平成一段的脚本直接跑起来。 + """ + fn = getattr(call, 'function', None) + name = getattr(fn, 'name', '') or '' + if not name: return 'tool name is empty' + try: + args = json.loads(getattr(fn, 'arguments', '') or '{}') + except Exception as e: + return 'arguments are not valid JSON: %s' % e + if not isinstance(args, dict): return 'arguments are not an object' + if name in ('code_run', 'code_exec', 'bash', 'powershell'): + code = args.get('script') or args.get('code') or '' + if not str(code).strip(): return 'script is empty' + ctype = str(args.get('type') or '').lower() + if not ctype: ctype = 'shell' if _SHELL_HEAD.match(str(code)) else 'python' + if ctype in ('python', 'py'): + try: + ast.parse(str(code)) + except SyntaxError as e: + return 'python syntax error (line %s: %s)' % (e.lineno, e.msg) + elif name in ('file_read', 'file_write', 'file_patch'): + if not args.get('path'): return 'path is empty' + return '' + + +def leaked_markup_incomplete(text): + """伪工具块是否被截断(流中断 / max_tokens)。True = 残块, 绝不能执行。""" + try: + if not text: return False + return bool(_scan_pseudo_xml(text).unclosed) + except Exception: + return False + + +def textual_tool_residue(content): + """正文里是否残留**调不成**的伪工具块。 + + 返回 'unclosed'(有开无闭, 通常是流被掐断) / 'empty'(块闭合但没参数) / + 'orphan'(有 却没有工具名) / 'invalid'(块闭合但重建的调用不可安全执行) / None。 + 一旦命中就绝不能当最终回答交付: + 用户看到的「冒出一坨标签然后对话自己中断了」就是这条路径。 + """ + try: + if not content: return None + sc = _scan_pseudo_xml(content) + if sc.unclosed: return 'unclosed' + if sc.orphans: return 'orphan' + for b in sc.blocks: + args = _pseudo_xml_args(b['body'], b['name'], b['attrs']) + if not args: return 'empty' + # 块是闭合的, 但重建出来的调用不可安全执行(半截脚本/语法错) —— 同样是残渣。 + # 这类不能在 `_parse_pseudo_xml_tool_calls` 里静默丢掉(那样会被当最终回答)。 + if salvaged_call_problem(MockToolCall(b['name'], args)): return 'invalid' + return None + except Exception: + return None + + +def _parse_pseudo_xml_tool_calls(content): + """把伪 XML 工具块还原成 MockToolCall。返回 (tcs, 去掉块后的正文)。 + + ★ 安全闸门放在**这个函数内部**(即 `_parse_text_tool_calls` 返回之前), 而不是 + `ga.do_no_tool`: 半截调用一旦变成真 tool_calls, 就会先走执行路径 —— + PR #853 的 H 正是这样被执行的。`leaked_markup_incomplete` 和 + `salvaged_call_problem` 在这里就把它们拦下, 宁可让模型重发。 + """ + try: + sc = _scan_pseudo_xml(content) + if not sc.blocks: return [], content + if textual_tool_residue(content): return [], content # unclosed / empty / orphan + found = [] + for b in sorted(sc.blocks, key=lambda x: x['start']): + args = _pseudo_xml_args(b['body'], b['name'], b['attrs']) + if not args: return [], content + call = MockToolCall(b['name'], args) + if salvaged_call_problem(call): return [], content # 半截/坏脚本不许执行 + found.append(call) + rest, last = [], 0 + for b in sorted(sc.blocks, key=lambda x: x['start']): + if b['start'] < last: continue + rest.append(content[last:b['start']]); last = b['end'] + rest.append(content[last:]) + tail = ''.join(rest) + tail = re.sub(r'', '', tail, flags=re.I) + return found, tail.strip() + except Exception as e: + print('[WARN] _parse_pseudo_xml_tool_calls failed: %s' % e) + return [], content + def _ensure_text_block(blocks): """If response has thinking but no text block, inject a synthetic summary from thinking's first line.""" if any(b.get("type") == "text" for b in blocks): return None diff --git a/tests/test_text_tool_call.py b/tests/test_text_tool_call.py new file mode 100644 index 000000000..f80cd6bba --- /dev/null +++ b/tests/test_text_tool_call.py @@ -0,0 +1,138 @@ +# -*- coding: utf-8 -*- +"""Pseudo-XML tool calls written into plain content must be reconstructed or retried. + +Run: python tests/test_text_tool_call.py (or: pytest tests/test_text_tool_call.py -q) + +Background (issue #863) +----------------------- +Some upstreams emit the tool call as **plain text** instead of a native `tool_calls` +array. `_parse_text_tool_calls` only understood a trailing JSON `tool_use` array and +``/`` JSON wrappers, so all of the shapes below fell through to +`ga.do_no_tool`, which had no matching guard, and the task ended silently. + + A + B … + C … stray quote after the tool name + D …<||DSML||invoke name="code_run">… + F … + G empty shell + H stream cut mid-call + +A-C, E and F are reconstructable. D, G and H are not: they must become a *retry*, never +a final answer and never an executable call. +""" +from __future__ import annotations + +import json +import os +import sys + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +import llmcore # noqa: E402 + +DSML = "\uff5c\uff5cDSML\uff5c\uff5c" + +SHAPES = { + "A": "\n\n", + "B": '\nprint(1)' + '600\n', + "C": '\n/tmp/a.txt\n', + "D": '\n1', + "E": f'<{DSML}tool_calls>\n<{DSML}invoke name="code_run">\n' + f'<{DSML}parameter name="script" string="true">print(1)\n' + f'\n', + "F": "print(1)", + "G": "\n\n\n\n", + "H": "\n\n" + assert _calls(broken) == [] + assert llmcore.textual_tool_residue(broken) == "invalid" + + +def test_cut_that_still_parses_is_not_executed(): + """The dangerous case: the stream is cut, but the fragment happens to be valid Python.""" + cut = "\n\n\n```", + '示例:{"type":"tool_use","name":"code_run","input":{"script":"print(1)"}}', + "…\n一句话", + ]: + assert _calls(content) == [], content + assert llmcore.textual_tool_residue(content) is None, content + + +def test_fenced_block_is_never_executed_even_at_offset_zero(): + """A fence that starts at index 0 must still be treated as a quotation.""" + fenced = "```\n\n\n\n```" + assert _calls(fenced) == [] + + +def test_inline_mention_of_parameter_syntax_is_not_residue(): + """说明句里提到 `` 不该触发重试(只在行首才算泄漏)。""" + prose = '用法: a.py 这样写。' + assert _calls(prose) == [] + assert llmcore.textual_tool_residue(prose) is None + + +def test_orphan_parameter_alone_still_retries(): + """形状 D: 只有 没有工具名, 必须判残渣。""" + content = '\n1' + assert _calls(content) == [] + assert llmcore.textual_tool_residue(content) == "orphan" + + +if __name__ == "__main__": + fails = [] + for name, fn in sorted(globals().items()): + if name.startswith("test_") and callable(fn): + try: + fn() + except AssertionError as exc: + fails.append(f"{name}: {exc}") + print("FAIL\n" + "\n".join(fails) if fails else "ALL PASS") + sys.exit(1 if fails else 0) From e29901138cfa748a9c381bb6385925c9e99f5dd0 Mon Sep 17 00:00:00 2001 From: GenericAgent Date: Sat, 14 Nov 2026 10:46:38 +0800 Subject: [PATCH 2/6] fix(ga): match truncation markers only at tail, not anywhere in last N chars --- ga.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ga.py b/ga.py index 71aa1fdac..00ff2882d 100644 --- a/ga.py +++ b/ga.py @@ -493,12 +493,22 @@ def do_no_tool(self, args, response): if not response or (not content.strip() and not thinking.strip()): yield "[Warn] LLM returned an empty response. Retrying...\n" return self._retry_or_exit("[ERROR] Blank response, regenerate and tooluse") - if '[!!! 流异常中断' in content[-100:] or '!!!Error:' in content[50:][-100:] or (content.endswith('') and len(content) < 100): + # 2026-11-06: 原判据是「响应末尾窗口内**包含** marker」。 + # 模型在正文中引用这些标记时(排查该 bug / 写文档 / 写测试), 会被误判为真中断 -> + # 整条回复被丢弃并插入 [ERROR] 重试, 用户观感为「回复写到一半停住不动」; + # 连续 3 次还会触发 _retry_or_exit 的任务中止。 + # llmcore 追加 marker 时一定在响应最末尾, 故改为**结尾精确匹配**; + # 同时补齐 llmcore 实际会产出的另外两类截断标记(原代码只认「流异常中断」)。 + _tail = content.rstrip() + _MARK_TAIL = re.compile(r'\[!!!\s*(?:流异常中断|Response truncated|output truncated).*?!!!\]\s*$') + if (_MARK_TAIL.search(_tail) + or _tail.endswith('!!!Error:') or _tail.startswith('!!!Error:') + or (_tail.endswith('') and len(_tail) < 100)): return self._retry_or_exit("[ERROR] Incomplete response. Regenerate and tooluse.") tail = content.rstrip()[-50:] if 'DSML' in tail and '|' in tail and tail.endswith('>'): return self._retry_or_exit("[ERROR] Tool call format invalid; no tool was called. Regenerate using proper tool calls.") - if 'max_tokens !!!]' in content[-100:]: + if _tail.endswith('max_tokens !!!]'): return self._retry_or_exit("[ERROR] max_tokens limit reached. Use multi small steps to do it.") # 2026-11-11: 伪 XML 工具块冒充最终回答。 From 4d98cfb1d39bd1dce274ae2a1ff8c8b98122068a Mon Sep 17 00:00:00 2001 From: GenericAgent Date: Sat, 14 Nov 2026 10:46:38 +0800 Subject: [PATCH 3/6] fix(llmcore): don't replay partial output on retry; treat empty/thinking-only reply as failure --- llmcore.py | 49 ++++++++- tests/test_stream_retry.py | 213 +++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 tests/test_stream_retry.py diff --git a/llmcore.py b/llmcore.py index b0cf42756..892080aa0 100644 --- a/llmcore.py +++ b/llmcore.py @@ -472,6 +472,24 @@ def _stamp_oai_cache_markers(messages, model): c = list(c); c[-1] = dict(c[-1], cache_control={'type': 'ephemeral'}) messages[idx] = {**messages[idx], 'content': c} +def _has_usable_output(blocks): + """True if the parsed response carries something the caller can act on. + + A response made only of ``thinking`` blocks is **not** usable: it has no + answer text and no tool call, so the caller treats it as a successful empty + reply and no retry / failover ever happens (typical causes: the model burned + its whole token budget on reasoning, or the upstream swallowed the answer). + """ + for b in blocks or []: + if not isinstance(b, dict): + continue + if b.get("type") == "tool_use": + return True + if b.get("type") == "text" and (b.get("text") or "").strip(): + return True + return False + + def _stream_with_retry(sess, url, headers, payload, parse_fn): STATS['session'] = sess.name _RETRYABLE = {408, 409, 425, 429, 500, 502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 527, 529} @@ -524,18 +542,35 @@ def _probe(*a, **k): if chunk and STATS.get('t_ttft') is None: STATS['t_ttft'] = time.time() streamed = True; yield chunk except StopIteration as e: - if not e.value and not streamed: raise requests.ConnectionError("empty response") + blocks = e.value or [] + if not _has_usable_output(blocks): + # 2026-11-04: 空正文 / 仅 thinking 不算成功——渠道会静默返回空白, + # 上层既不会重试也不会换渠(用户只看到"什么都没有")。 + if not streamed: + raise requests.ConnectionError("empty response: no text and no tool_use") + trail = "\n\n[!!! 流异常中断: 响应无正文(仅思考/空)!!!]" + print("[WARN] stream produced no usable output (thinking-only or empty)") + blocks = list(blocks) + [{"type": "text", "text": trail}] + STATS['t_end'] = time.time() + yield trail; return blocks STATS['t_end'] = time.time() STATS['tps'] = STATS.get('out', 0) / max(1e-9, STATS['t_end'] - max(STATS['t_ttft'] or 0, STATS['t_start'])) return e.value or [] except (requests.Timeout, requests.ConnectionError, requests.exceptions.ChunkedEncodingError) as e: err = f"!!!Error: {type(e).__name__}: {e}" if str(e) else f"!!!Error: {type(e).__name__}" if getattr(sess, 'should_stop', None) and sess.should_stop(): return [] - if attempt < sess.max_retries: + # 2026-11-04: 已经向下游吐过内容后再重试 = 把前半截正文重放一遍 + # (实证: 用户看到 "AAABBBAAABBBAAABBB")。已交付内容不可撤回, 只能标记中断, + # 并把"下一次调用换渠"交给 MixinSession 处理。 + if not streamed and attempt < sess.max_retries: d = _delay(None, attempt) print(f"[LLM Retry] {type(e).__name__}, retry in {d:.1f}s ({attempt+1}/{sess.max_retries+1})") if _sleep(d): return [] continue + if streamed: + trail = f"\n\n[!!! 流异常中断 {type(e).__name__}: {e} !!!]" + print(f"[WARN] stream broke after partial output, not retrying ({type(e).__name__})") + yield trail; return [{"type": "text", "text": trail}] yield err; return [{"type": "text", "text": err}] except Exception as e: err = f"\n\n[!!! 流异常中断 {type(e).__name__}: {e} !!!]" if streamed else f"!!!Error: {type(e).__name__}: {e}" @@ -1310,9 +1345,17 @@ def raw_ask(self, messages): while True: chunk = next(gen); last_chunk = chunk if not yielded and test_error(chunk): continue - yield chunk; yielded = True + yield chunk + if not test_error(chunk): yielded = True except StopIteration as e: return_val = e.value or [] is_err = test_error(last_chunk) + # 2026-11-04: 正文已经交付给调用方之后再换渠重试, 只会把前半截重放一遍 + # (用户看到 "AAABBB" + "AAABBB")。保持本轮输出, 仅把下一次调用指到别的渠道。 + if is_err and yielded: + if n > 1: + self._cur_idx = (idx + 1) % n; self._switched_at = time.time(); self._lease_rebind() + print(f'[MixinSession] {str(last_chunk)[:80]}, partial output kept, next call → s{self._cur_idx} ({self.current.name})') + return return_val if not is_err: if attempt > 0: self._cur_idx = idx; self._switched_at = time.time() elif isinstance(last_chunk, str) and '[!!! 流异常中断' in last_chunk and n > 1: diff --git a/tests/test_stream_retry.py b/tests/test_stream_retry.py new file mode 100644 index 000000000..3353f29b2 --- /dev/null +++ b/tests/test_stream_retry.py @@ -0,0 +1,213 @@ +# -*- coding: utf-8 -*- +"""Stream retry / empty-response regressions -- 2026-11-04 + +Run: python tests/test_stream_retry.py + +Covers two long-standing llmcore defects, both reproduced with a local fake +SSE server (no network, deterministic): + +1. **Partial output replayed on retry.** + ``_stream_with_retry`` retried a broken stream even when chunks had already + been yielded to the caller, so a mid-stream ``ChunkedEncodingError`` made the + user see the first half twice ("AAABBB" + "AAABBB"). Retrying cannot + un-deliver text, so after any visible output we must stop and mark the + truncation instead. + +2. **Empty / thinking-only response treated as success.** + ``test_error`` only recognises ``!!!Error:`` / ``[Error:`` prefixes, and the + emptiness check used ``not e.value``, which is False as soon as a + ``thinking`` block exists. A response with no text and no tool call was + therefore returned as a successful empty answer: no retry, no channel + failover, and the user just sees nothing. + The fix judges "usable output" (non-blank text or a tool_use block) and + raises ConnectionError so the normal retry / failover path runs. +""" +import json +import os +import socket +import sys +import threading +import time +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +if ROOT not in sys.path: + sys.path.insert(0, ROOT) + +import llmcore # noqa: E402 + +FAIL = [] +HITS = {} +PORT = 0 + + +def check(cond, msg): + print((' PASS ' if cond else ' FAIL ') + msg) + if not cond: + FAIL.append(msg) + + +class Handler(BaseHTTPRequestHandler): + """Fake OpenAI-compatible SSE endpoint; behaviour selected by URL path.""" + protocol_version = "HTTP/1.1" + + def log_message(self, *a): + pass + + def do_POST(self): + mode = self.path.strip("/").split("/")[0] + HITS[mode] = HITS.get(mode, 0) + 1 + self.rfile.read(int(self.headers.get("Content-Length") or 0)) + self.send_response(200) + self.send_header("Content-Type", "text/event-stream") + self.send_header("Transfer-Encoding", "chunked") + self.end_headers() + + def write(b): + self.wfile.write(("%X\r\n" % len(b)).encode() + b + b"\r\n") + self.wfile.flush() + + def event(obj): + write(("data: " + json.dumps(obj) + "\n\n").encode()) + + def delta(d, fr=None): + event({"choices": [{"delta": d, "finish_reason": fr}]}) + + try: + if mode == "partial": + delta({"content": "AAA"}) + time.sleep(0.02) + delta({"content": "BBB"}) + time.sleep(0.02) + # never send the terminating chunk: simulate a dropped stream + self.connection.shutdown(socket.SHUT_RDWR) + self.connection.close() + return + if mode == "empty": + delta({}, "stop") + write(b"data: [DONE]\n\n") + write(b"") + return + if mode == "thinking_only": + delta({"reasoning_content": "let me think ..."}) + delta({}, "stop") + write(b"data: [DONE]\n\n") + write(b"") + return + if mode == "tool_only": + delta({"tool_calls": [{"index": 0, "id": "c1", "type": "function", + "function": {"name": "code_run", + "arguments": "{\"script\": \"1\"}"}}]}) + delta({}, "tool_calls") + write(b"data: [DONE]\n\n") + write(b"") + return + delta({"content": "OK"}) + delta({}, "stop") + write(b"data: [DONE]\n\n") + write(b"") + except OSError: + pass + + +def make_session(mode, retries=1, name=None, hide=False): + cfg = { + "apikey": "k", + "apibase": "http://127.0.0.1:%d/%s/v1" % (PORT, mode), + "model": "m", + "name": name or ("fake_" + mode), + "max_retries": retries, + "stream": True, + "timeout": 3, + "read_timeout": 5, + "no_env_proxy": True, + # 仅部分版本支持(隐藏思维链); 旧版 llmcore 会忽略此键 + "stream_hide_reasoning": hide, + } + return llmcore.NativeOAISession(cfg) + + +def drain(session, messages=None): + """Consume raw_ask, return (yielded text, blocks).""" + out, blocks = [], [] + gen = session.raw_ask(messages or [{"role": "user", "content": "hi"}]) + try: + while True: + out.append(next(gen)) + except StopIteration as e: + blocks = e.value or [] + return "".join(x for x in out if isinstance(x, str)), blocks + + +def main(): + print("1) partial stream must NOT be replayed (no retry after visible output)") + n0 = HITS.get("partial", 0) + text, blocks = drain(make_session("partial", retries=2)) + check(text.count("AAA") == 1 and text.count("BBB") == 1, + "first half appears exactly once, got %r" % text[:60]) + check("流异常中断" in text, "truncation is marked explicitly") + check(HITS.get("partial", 0) - n0 == 1, "no re-request after partial output") + + print("2) empty response retries then surfaces an error (no silent success)") + n0 = HITS.get("empty", 0) + text, blocks = drain(make_session("empty", retries=1)) + check(HITS.get("empty", 0) - n0 == 2, "retried once (2 requests total)") + check(text.lstrip().startswith("!!!Error:"), "final chunk is an error, got %r" % text[:60]) + + print("3) thinking-only response is never a silent empty answer") + n0 = HITS.get("thinking_only", 0) + text, blocks = drain(make_session("thinking_only", retries=1, hide=True)) + # hide=True 时: + # 渠道支持隐藏思维链 -> 什么都产不出来 -> 必须报错/换渠 (!!!Error: / 流异常中断) + # 渠道不支持(旧版) -> 思维链本身作为正文抵达 -> 用户看得见, 不算静默 + # 共同不变式: 调用方**永远**不会拿到一个空串却被告知"成功"。 + surfaced = ("\u6d41\u5f02\u5e38\u4e2d\u65ad" in text) or text.lstrip().startswith("!!!Error:") + check(bool(text.strip()), "caller never gets a blank answer, got %r" % text[:60]) + check(surfaced or "think" in text.lower(), + "visible signal (marker/error or the reasoning text itself), got %r" % text[:60]) + + print("4) tool-only response is still a valid answer (must not be called empty)") + n0 = HITS.get("tool_only", 0) + text, blocks = drain(make_session("tool_only", retries=1)) + check(any(b.get("type") == "tool_use" for b in blocks), "tool_use block preserved") + check("流异常中断" not in text, "no empty-response marker") + check(HITS.get("tool_only", 0) - n0 == 1, "single request") + + print("5) normal response is untouched") + n0 = HITS.get("ok", 0) + text, blocks = drain(make_session("ok", retries=2)) + check(text == "OK", "text == 'OK', got %r" % text) + check("流异常中断" not in text, "no marker on a clean stream") + check(HITS.get("ok", 0) - n0 == 1, "single request") + + print("6) MixinSession does not fail over after delivering partial output") + n0o = HITS.get("ok", 0) + ch1 = llmcore.NativeToolClient(make_session("partial", retries=2, name="ch1")) + ch2 = llmcore.NativeToolClient(make_session("ok", retries=2, name="ch2")) + mixin = llmcore.MixinSession([ch1, ch2], { + "llm_nos": ["ch1", "ch2"], "max_retries": 1, "base_delay": 0.0, + }) + text, blocks = drain(mixin) + check(text.count("AAA") == 1, "channel-1 partial text kept once") + check(HITS.get("ok", 0) - n0o == 0, "channel 2 not replayed in the same call") + check(mixin.current_name == "ch2", "next call is routed to channel 2") + + print() + if FAIL: + print("FAILED (%d):" % len(FAIL)) + for m in FAIL: + print(" - " + m) + return 1 + print("ALL PASS") + return 0 + + +if __name__ == "__main__": + srv = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + PORT = srv.server_address[1] + threading.Thread(target=srv.serve_forever, daemon=True).start() + try: + rc = main() + finally: + srv.shutdown() + sys.exit(rc) From 98407d580c824b12773ab9da69576be774562523 Mon Sep 17 00:00:00 2001 From: GenericAgent Date: Sat, 14 Nov 2026 10:46:38 +0800 Subject: [PATCH 4/6] fix(llmcore): empty tool_use_id results keep their order instead of reversing --- llmcore.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/llmcore.py b/llmcore.py index 892080aa0..b686d673d 100644 --- a/llmcore.py +++ b/llmcore.py @@ -1409,11 +1409,16 @@ def chat(self, messages, tools=None): elif isinstance(c, list): combined_content.extend(c) if msg['role'] == 'user' and msg.get('tool_results'): tool_results.extend(msg['tool_results']) tr_id_set = set(); tool_result_blocks = [] + # 2026-11-13: 空 tool_use_id 的结果按出现顺序收集, 循环结束后统一追加。 + # 原实现是每次往 combined_content 头部 insert, 导致多个空 id 结果顺序反转 + # (实测 R1,R2 -> 发出 R2,R1), 且被插到用户正文之前。 + orphan_results = [] for tr in tool_results: tool_use_id, content = tr.get("tool_use_id", ""), tr.get("content", "") tr_id_set.add(tool_use_id) if tool_use_id: tool_result_blocks.append({"type": "tool_result", "tool_use_id": tool_use_id, "content": tr.get("content", "")}) - else: combined_content = [{"type": "text", "text": f'{content}'}] + combined_content + else: orphan_results.append({"type": "text", "text": f'{content}'}) + combined_content.extend(orphan_results) for tid in self._pending_tool_ids: if tid not in tr_id_set: tool_result_blocks.append({"type": "tool_result", "tool_use_id": tid, "content": ""}) self._pending_tool_ids = [] From a87d251737afc718e542e90b211984048a6c7c1e Mon Sep 17 00:00:00 2001 From: GenericAgent Date: Sat, 14 Nov 2026 10:46:39 +0800 Subject: [PATCH 5/6] fix(code_run): reader join instead of a flat sleep(1) per call --- ga.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ga.py b/ga.py index 00ff2882d..bfdb02478 100644 --- a/ga.py +++ b/ga.py @@ -73,10 +73,13 @@ def stream_reader(proc, logs): if istimeout: full_stdout.append("\n[Timeout Error] 超时强制终止") else: full_stdout.append("\n[Stopped] 用户强制终止") break - time.sleep(1) + t.join(0.05) # 等 reader 退出, 不再固定 sleep(1) t.join(timeout=1) exit_code = process.poll() + if exit_code is None: # reader 可能先于进程回收退出 + try: exit_code = process.wait(timeout=5) + except subprocess.TimeoutExpired: exit_code = process.poll() stdout_str = "".join(full_stdout) status = "success" if exit_code == 0 else "error" From 825d3e5f5c188207ddfde07b24027259009f6893 Mon Sep 17 00:00:00 2001 From: GenericAgent Date: Sat, 14 Nov 2026 10:46:39 +0800 Subject: [PATCH 6/6] fix(agentmain): add --no-reply-wait for unattended one-shot --task runs --- agentmain.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/agentmain.py b/agentmain.py index 78ef4c929..50e12918c 100644 --- a/agentmain.py +++ b/agentmain.py @@ -240,6 +240,14 @@ def run(self): parser.add_argument('--llm_no', type=int, default=0) parser.add_argument('--verbose', action='store_true') parser.add_argument('--nobg', action='store_true') + # ★无人值守一次性任务开关(issue #866)。 + # --task 模式默认在第一轮 `[ROUND END]` 后继续等 reply.txt 最长 600s, + # 供**外部监督者**写 reply 续轮。没有监督者的场景(调度器/批处理/CI)下, + # 这 600s 是纯空转:报告早已写完,进程却要等到外层 runner 的硬超时 + # 把整棵进程树杀掉(rc=-999)。传本开关则第一轮完成即退出。 + # 默认关闭,既有监督者工作流行为不变。 + parser.add_argument('--no-reply-wait', action='store_true', + help='one-shot mode: exit right after round 1, do not wait for reply.txt') parser.add_argument('--nolog', action='store_true') parser.add_argument('--no-user-tools', action='store_true') args, _unknown = parser.parse_known_args() @@ -288,6 +296,7 @@ def run(self): with open(outfile, 'w', encoding='utf-8') as f: f.write(item.get('next', '')) with open(outfile, 'w', encoding='utf-8') as f: f.write(item['done'] + '\n\n[ROUND END]\n') if not args.task: break + if args.no_reply_wait: break # issue #866: unattended one-shot, no supervisor will reply consume_file(d, '_stop') # 已经成功停下来了,避免打断下次reply for _ in range(300): # 等reply.txt,10分钟超时 time.sleep(2)