diff --git a/backend/app/engine/nodes/llm.py b/backend/app/engine/nodes/llm.py index 5d0c51e4..6e2bd28e 100644 --- a/backend/app/engine/nodes/llm.py +++ b/backend/app/engine/nodes/llm.py @@ -541,25 +541,66 @@ async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]: async def extract_step(payload: list[BaseMessage], schema: dict[str, Any]) -> dict[str, Any]: # cite_fields 的结构化抽取:进 checkpoint,节点重放时不再调用、不再计费。失败也在这里收住、 # 作为结果返回——agent 的结论还在,字段记空值、发警告,而不是把整个节点赔进去。 - # thinking 关掉:结构化输出靠强制工具调用,和思考模式不兼容,抽取也用不着思考 - spec = model_spec.model_copy(update={ - "thinking": "off", "max_tokens": max(int(ctx.cfg("max_tokens") or 0), 4096)}) - started = time.perf_counter() - ctx.emit(EventType.LLM_START, model=model_id, structured=True, purpose="cite_fields", - message_count=len(payload)) - try: - async with SessionLocal() as session: - extractor, _ = await get_chat_model(session, spec) - got = await extractor.with_structured_output(schema, include_raw=True).ainvoke(payload) - except Exception as e: # noqa: BLE001 + # thinking 关掉:结构化输出靠强制工具调用,和思考模式不兼容,抽取也用不着思考。但经 OpenAI 兼容 + # 接口接入的推理模型关不掉思考,推理照样算在额度里:额度见 EXTRACT_MAX_TOKENS,被截断时加大重试一次 + budget = max(int(ctx.cfg("max_tokens") or 0), EXTRACT_MAX_TOKENS) + spent = {"input_tokens": 0, "output_tokens": 0, "cost_usd": 0.0, "calls": 0} + rejected: int | None = None # 服务方嫌太大、拒收的额度:之后重试不再超过它 + retried = False + + def count(usage: dict[str, Any]) -> None: + for key in ("input_tokens", "output_tokens", "calls"): + spent[key] += int(usage.get(key) or 0) + spent["cost_usd"] = round(spent["cost_usd"] + float(usage.get("cost_usd") or 0), 6) + + while True: + spec = model_spec.model_copy(update={"thinking": "off", "max_tokens": budget}) + started = time.perf_counter() + ctx.emit(EventType.LLM_START, model=model_id, structured=True, purpose="cite_fields", + message_count=len(payload), max_tokens=budget) + failure: Exception | None = None + raw: BaseMessage | None = None + parsed: Any = None + try: + async with SessionLocal() as session: + extractor, _ = await get_chat_model(session, spec) + got = await extractor.with_structured_output(schema, include_raw=True).ainvoke(payload) + raw, parsed = _split_structured(got) + usage = _usage_of(raw if raw is not None else AIMessage(content=""), model_id) + except Exception as e: # noqa: BLE001 + failure, usage = e, _error_usage(e, model_id) + count(usage) + cut = truncated_error(failure) if failure is not None else (parsed is None and truncated_reply(raw)) + elapsed = int((time.perf_counter() - started) * 1000) + if failure is not None and not cut and _budget_rejected(failure) \ + and (lower := next((b for b in _EXTRACT_FALLBACKS if b < budget), None)) is not None: + # 服务方的输出上限比默认额度小(DeepSeek chat 系只收 8K):退一档再来,别因为额度提高反而抽不成 + ctx.emit(EventType.LLM_END, agent=ctx.node.title, model=model_id, purpose="cite_fields", + duration_ms=elapsed, **usage, + error=f"模型接入不接受 {budget} token 的输出额度,已改用 {lower} token 重试") + rejected, budget = budget, lower + continue + bigger = min(budget * 2, EXTRACT_RETRY_CAP) + if cut and not retried and bigger > budget and (rejected is None or bigger < rejected): + ctx.emit(EventType.LLM_END, agent=ctx.node.title, model=model_id, purpose="cite_fields", + duration_ms=elapsed, **usage, + error=f"输出被截断:{budget} token 的输出额度用完了,结构化结论没有写完," + f"已把额度加大到 {bigger} token 重试一次") + retried, budget = True, bigger + continue + if cut: + ctx.emit(EventType.LLM_END, agent=ctx.node.title, model=model_id, purpose="cite_fields", + duration_ms=elapsed, **usage, + error=f"输出被截断:{budget} token 的输出额度用完了,结构化结论没有写完") + return {"error": TRUNCATED_EXTRACT.format(budget=budget), "truncated": True, + "max_tokens": budget, **spent} + if failure is not None: + ctx.emit(EventType.LLM_END, agent=ctx.node.title, model=model_id, purpose="cite_fields", + duration_ms=elapsed, **usage, error=describe_exception(failure)) + return {"error": f"结构化抽取失败:{explain_model_error(failure, model_id)}", **spent} ctx.emit(EventType.LLM_END, agent=ctx.node.title, model=model_id, purpose="cite_fields", - duration_ms=int((time.perf_counter() - started) * 1000), error=describe_exception(e)) - return {"error": f"结构化抽取失败:{explain_model_error(e, model_id)}"} - raw, parsed = _split_structured(got) - usage = _usage_of(raw if raw is not None else AIMessage(content=""), model_id) - ctx.emit(EventType.LLM_END, agent=ctx.node.title, model=model_id, purpose="cite_fields", - duration_ms=int((time.perf_counter() - started) * 1000), **usage) - return {"parsed": parsed, **usage} + duration_ms=elapsed, **usage) + return {"parsed": parsed, **spent} final_text = "" #: 模型把工具调用写成了文字:纠正过一次了没有 @@ -778,8 +819,10 @@ async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]: total_usage[key] += int(extracted.get(key) or 0) total_usage["cost_usd"] = round(total_usage["cost_usd"] + float(extracted.get("cost_usd") or 0), 6) cited = verify_cited_fields(extracted, fields, queries) - for code, message, names in _field_warnings(*cited, extracted.get("error")): - await once(ctx, EventType.LOG, level="warn", code=code, message=message, fields=names) + truncated = extracted.get("truncated") is True + for code, message, names in _field_warnings(*cited, extracted.get("error"), truncated=truncated): + extra = {"max_tokens": extracted.get("max_tokens")} if code == "agent_field_truncated" else {} + await once(ctx, EventType.LOG, level="warn", code=code, message=message, fields=names, **extra) total_usage["total_tokens"] = total_usage["input_tokens"] + total_usage["output_tokens"] result = { @@ -941,6 +984,60 @@ def cited_schema(schema: dict[str, Any]) -> dict[str, Any]: return {"title": "cited_fields", **rewrite(schema)} +#: cite_fields 结构化抽取的输出额度。thinking: off 只对 Anthropic 有用:经 OpenAI 兼容接口接入的推理模型 +#: (DeepSeek 系等)关不掉思考,推理也算在这个额度里。以前是 4096:2026-10 一次运行 4 个 Agent 的抽取全部在 +#: 推理阶段就用完了额度(completion_tokens=4096),一个字段都没写出来。节点配了更大的「最大输出 token」就用节点的 +EXTRACT_MAX_TOKENS = 16384 +#: 被截断时额度翻倍重试一次,最多到这里 +EXTRACT_RETRY_CAP = 32768 +#: 服务方嫌额度太大、拒收时依次退到这几档(DeepSeek chat 系只收 8K)。拒收发生在请求刚发出时,不产生用量 +_EXTRACT_FALLBACKS = (8192, 4096) +#: 服务方嫌额度太大时报错里会出现的字样(和 api/copilot.py 改图时退回小额度是同一个判断) +_BUDGET_WORDS = ("max_tokens", "max_completion_tokens", "max_output_tokens", "maximum context length") +#: 截断类报错的原话:openai 的 LengthFinishReasonError(结构化输出解析时 finish_reason=length) +_LENGTH_WORDS = ("length limit was reached", "finish_reason=length", "finish_reason='length'") +#: 两次都被截断时的原因(给人看)。重试、去设置页测试模型都不解决额度问题,不能照通用的模型报错那样说 +TRUNCATED_EXTRACT = ("结构化抽取没有完成:模型的输出额度在推理阶段就用完了(最后一次的额度为 {budget} token)," + "结构化结论没能写出来") +#: agent_field_truncated 的下一步(接在「N 个字段都记为空值」后面) +TRUNCATED_NEXT = ("可以为该 Agent 换用非推理模型(抽取沿用 Agent 的模型),或者调大该 Agent 的" + "「最大输出 token」后重新运行") + + +def truncated_error(error: BaseException | None) -> bool: + """这个异常是不是在说「输出被截断」:openai 的 LengthFinishReasonError,或者 langchain、网关转述的同一件事。 + + 服务方嫌额度太大的 400(报错里也有 max_tokens)不算:那是请求被拒,模型一个字都没写。 + """ + if error is None: + return False + if type(error).__name__ == "LengthFinishReasonError": + return True + text = str(error).lower() + return any(word in text for word in _LENGTH_WORDS) + + +def truncated_reply(raw: Any) -> bool: + """这条回复是不是因为额度用完停下的:OpenAI 系是 finish_reason=length,Anthropic 是 stop_reason=max_tokens。""" + meta = getattr(raw, "response_metadata", None) or {} + reason = meta.get("finish_reason") or meta.get("stop_reason") + return reason in ("length", "max_tokens") + + +def _budget_rejected(error: BaseException) -> bool: + status = getattr(error, "status_code", None) or getattr(getattr(error, "response", None), "status_code", None) + return status in (400, 422) and any(word in str(error).lower() for word in _BUDGET_WORDS) + + +def _error_usage(error: BaseException, model_id: str) -> dict[str, Any]: + """失败的那次调用花掉的用量。截断时 openai 的异常带着 completion.usage:额度是真花出去了,要记账。""" + usage = getattr(getattr(error, "completion", None), "usage", None) + inp = int(getattr(usage, "prompt_tokens", 0) or 0) + out = int(getattr(usage, "completion_tokens", 0) or 0) + return {"input_tokens": inp, "output_tokens": out, "total_tokens": inp + out, + "cost_usd": round(catalog.estimate_cost(model_id, inp, out), 6), "calls": int(bool(inp or out))} + + EXTRACT_SYSTEM = ( "你是数据抽取员:把智能体查到的数据按给定结构填好,每个字段都写出处。\n" "- value 是字段的值;from 写它在哪次查询的哪一格:call 是【证据 Qn】里的编号,row 是行号(从 0 数)," @@ -1184,9 +1281,12 @@ def _shown(value: Any) -> str: def _field_warnings(data: dict[str, Any], evidence: dict[str, Any], - failed: str | None) -> list[tuple[str, str, list[str]]]: + failed: str | None, *, truncated: bool = False) -> list[tuple[str, str, list[str]]]: """核对结果里要让人看见的:(code, message, 字段)。mismatch 和 unresolved 各一条;missing 是模型 - 照实说没查到,值已经是空的,不另发警告。""" + 照实说没查到,值已经是空的,不另发警告。 + + 抽取两次都被截断(truncated)时 code 是 agent_field_truncated:原因是输出额度在推理阶段就用完了, + 下一步是换非推理模型或调大最大输出 token,不是「稍后重试」——和别的抽取失败分开说。""" out: list[tuple[str, str, list[str]]] = [] mismatched = [(p, e) for p, e in evidence.items() if e.get("status") == "mismatch"] if mismatched: @@ -1196,6 +1296,11 @@ def _field_warnings(data: dict[str, Any], evidence: dict[str, Any], f"有 {len(mismatched)} 个字段与查询快照不一致,已按快照取值:{detail}", [p for p, _ in mismatched])) unresolved = [(p, e) for p, e in evidence.items() if e.get("status") == "unresolved"] if unresolved: + if failed and truncated: + out.append(("agent_field_truncated", + f"{failed}。{len(unresolved)} 个字段都记为空值(未以 0 代替)。{TRUNCATED_NEXT}", + [p for p, _ in unresolved])) + return out if failed: message = f"{failed}。{len(unresolved)} 个字段都记为空值(未以 0 代替)" else: diff --git a/backend/tests/test_extract_truncation.py b/backend/tests/test_extract_truncation.py new file mode 100644 index 00000000..89ee80ee --- /dev/null +++ b/backend/tests/test_extract_truncation.py @@ -0,0 +1,208 @@ +"""cite_fields 的结构化抽取:输出被截断时加大额度重试,仍截断就照实说是额度在推理阶段用完了。 + +2026-10 一次运行里 4 个开了 cite_fields 的 Agent 抽取全部失败:抽取用 thinking: off、额度 4096,而经 OpenAI 兼容接口 +接入的推理模型关不掉思考,4096 在推理阶段就用完了,openai 抛 LengthFinishReasonError(「Could not parse response +content as the length limit was reached」)。日志却写「请稍后重试;如持续失败,请到设置测试该模型」——重试、测试都没用。 + +- 额度默认至少 16384,节点配了更大的用节点的 +- 认出「输出被截断」:openai 的 length limit 类错误、finish_reason=length、Anthropic 的 stop_reason=max_tokens; + 被截断时额度翻倍(最多 32768)重试一次 +- 仍然截断:日志 code agent_field_truncated,说清是额度在推理阶段用完了,给出能做的下一步;字段记空值,不以 0 代替 +- 服务方嫌额度太大(400,报错里有 max_tokens 一类字样):退回小一档的额度重来,不能因为额度提高反而抽不成 +- 非截断类的失败照旧(agent_field_unverified) + +夹具全部合成:门店、会员这类虚构的表。 +""" +from __future__ import annotations + +import json + +import pytest +from langchain_core.messages import AIMessage +from openai import LengthFinishReasonError +from openai.types import CompletionUsage +from openai.types.chat import ChatCompletion + +from app.engine.nodes import llm as llm_mod +from app.engine.runner import run_manager +from tests.test_agent_cited_fields import ( # noqa: F401 - 夹具按名字引入 + EXTRACTED, + FINAL, + SCHEMA, + SQLS, + TOOL, + Script, + engine_up, + events, + graph, + logs, + output_of, + shop, + wait, +) + + +def length_error(prompt: int = 12000, completion: int = 4096) -> LengthFinishReasonError: + """openai 的结构化输出在 finish_reason=length 时抛的那个错(和真实运行里的原话一样)。""" + usage = CompletionUsage(completion_tokens=completion, prompt_tokens=prompt, total_tokens=prompt + completion) + return LengthFinishReasonError(completion=ChatCompletion.model_construct( + id="x", choices=[], created=0, model="m", object="chat.completion", usage=usage)) + + +class BudgetRejected(Exception): + """服务方嫌输出额度太大时的 400(DeepSeek chat 系只收 8K)。""" + + status_code = 400 + + +class Extraction: + """记下每次抽取调用的额度(thinking 关掉的那次 get_chat_model),并按额度决定这次怎么回。 + + outcome(budget) 返回 None 表示照常回 EXTRACTED,抛异常就是这次调用失败。 + """ + + def __init__(self, monkeypatch, outcome): + from app.providers import mock_model + + self.budgets: list[int | None] = [] + real = llm_mod.get_chat_model + + async def spy(session, spec): + if spec.thinking == "off": + self.budgets.append(spec.max_tokens) + return await real(session, spec) + + monkeypatch.setattr(llm_mod, "get_chat_model", spy) + base = Script(monkeypatch) + loop = mock_model.MockChatModel._decide + + def decide(model, messages): + if model.response_format: + outcome(self.budgets[-1]) + return AIMessage(content=json.dumps(EXTRACTED, ensure_ascii=False)) + return loop(model, messages) + + monkeypatch.setattr(mock_model.MockChatModel, "_decide", decide) + self.script = base + + +# -------------------------------------------------------------------------- +# 认出「输出被截断」 +# -------------------------------------------------------------------------- + + +def test_length_errors_are_recognised(): + assert llm_mod.truncated_error(length_error()) + assert llm_mod.truncated_error(RuntimeError("Could not parse response content as the length limit was reached")) + assert not llm_mod.truncated_error(RuntimeError("网关超时")) + assert not llm_mod.truncated_error(BudgetRejected("max_tokens is too large: 16384")) + + +def test_cut_off_replies_are_recognised_for_both_families(): + assert llm_mod.truncated_reply(AIMessage(content="", response_metadata={"finish_reason": "length"})) + assert llm_mod.truncated_reply(AIMessage(content="", response_metadata={"stop_reason": "max_tokens"})) + assert not llm_mod.truncated_reply(AIMessage(content="", response_metadata={"finish_reason": "stop"})) + assert not llm_mod.truncated_reply(None) + + +# -------------------------------------------------------------------------- +# 额度与重试 +# -------------------------------------------------------------------------- + + +async def test_the_default_budget_leaves_room_for_reasoning(monkeypatch): + spy = Extraction(monkeypatch, lambda budget: None) + row = await wait((await run_manager.start(graph=graph(card=False), input_payload={})).id) + assert row.status == "succeeded", row.error + assert spy.budgets == [16384], "推理模型的思考也算在额度里:默认至少 16384" + + +async def test_a_larger_node_budget_is_used(monkeypatch): + spy = Extraction(monkeypatch, lambda budget: None) + row = await wait((await run_manager.start(graph=graph(card=False, max_tokens=24000), input_payload={})).id) + assert row.status == "succeeded", row.error + assert spy.budgets == [24000] + + +async def test_a_cut_off_extraction_is_retried_with_a_larger_budget(monkeypatch): + def outcome(budget): + if budget < 32768: + raise length_error(completion=budget) + + spy = Extraction(monkeypatch, outcome) + row = await wait((await run_manager.start(graph=graph(card=False), input_payload={})).id) + assert row.status == "succeeded", row.error + assert spy.budgets == [16384, 32768], "截断后翻倍重试一次" + bot = await output_of(row.id, "bot") + assert bot["data"]["gmv"] == 600.5 and bot["data_evidence"]["gmv"]["status"] == "verified" + evts = await events(row.id) + assert not logs(evts, "agent_field_truncated") + # EXTRACTED 里 region 指向本节点没有的 Q9:只有它核对不了,别的字段都照常核对 + assert [w["fields"] for w in logs(evts, "agent_field_unverified")] == [["region"]] + # 两次调用各记一行:第一次写明为什么重来 + ends = [e.data for e in evts if e.type == "llm.end" and e.data.get("purpose") == "cite_fields"] + assert len(ends) == 2 and "32768" in ends[0]["error"] and not ends[1].get("error") + # 被截断那次的用量照样记账(花出去的 token 是真的) + assert ends[0]["output_tokens"] == 16384 + + +async def test_cut_off_twice_is_reported_as_truncation_with_fields_empty(monkeypatch): + def outcome(budget): + raise length_error(completion=budget) + + spy = Extraction(monkeypatch, outcome) + row = await wait((await run_manager.start(graph=graph(card=False), input_payload={})).id) + assert row.status == "succeeded", row.error + assert spy.budgets == [16384, 32768], "只重试一次" + bot = await output_of(row.id, "bot") + assert bot["data"] == {k: None for k in SCHEMA["properties"]}, "记空值,不以 0 代替" + assert all(e["status"] == "unresolved" for e in bot["data_evidence"].values()) + evts = await events(row.id) + assert not logs(evts, "agent_field_unverified") + [warn] = logs(evts, "agent_field_truncated") + assert warn["level"] == "warn" and warn["fields"] == list(SCHEMA["properties"]) + message = warn["message"] + assert "推理阶段就用完了" in message and "结构化结论没能写出来" in message + assert "32768" in message and "非推理模型" in message and "最大输出 token" in message + assert "请稍后重试" not in message and "测试该模型" not in message, "重试、测试模型都不解决额度问题" + assert "未以 0 代替" in message + assert warn.get("max_tokens") == 32768 + + +async def test_a_node_budget_at_the_cap_is_not_retried(monkeypatch): + def outcome(budget): + raise length_error(completion=budget) + + spy = Extraction(monkeypatch, outcome) + row = await wait((await run_manager.start(graph=graph(card=False, max_tokens=40000), input_payload={})).id) + assert row.status == "succeeded", row.error + assert spy.budgets == [40000], "额度已经超过重试上限,再调一次也一样" + [warn] = logs(await events(row.id), "agent_field_truncated") + assert "40000" in warn["message"] + + +async def test_a_budget_the_service_rejects_falls_back(monkeypatch): + def outcome(budget): + if budget > 8192: + raise BudgetRejected(f"Invalid max_tokens value, the valid range of max_tokens is [1, 8192]: {budget}") + + spy = Extraction(monkeypatch, outcome) + row = await wait((await run_manager.start(graph=graph(card=False), input_payload={})).id) + assert row.status == "succeeded", row.error + assert spy.budgets == [16384, 8192] + bot = await output_of(row.id, "bot") + assert bot["data_evidence"]["gmv"]["status"] == "verified" + + +async def test_other_failures_are_reported_as_before(monkeypatch): + def outcome(budget): + raise RuntimeError("网关超时") + + spy = Extraction(monkeypatch, outcome) + row = await wait((await run_manager.start(graph=graph(card=False), input_payload={})).id) + assert row.status == "succeeded", row.error + assert spy.budgets == [16384], "不是截断就不重试" + evts = await events(row.id) + assert not logs(evts, "agent_field_truncated") + [warn] = logs(evts, "agent_field_unverified") + assert "网关超时" in warn["message"] and "未以 0 代替" in warn["message"] diff --git a/frontend/src/run/decode.ts b/frontend/src/run/decode.ts index cd668956..f82b4844 100644 --- a/frontend/src/run/decode.ts +++ b/frontend/src/run/decode.ts @@ -784,6 +784,22 @@ function explainLog(code: string | undefined, message: string, d: Record 0 ? d.max_tokens : undefined + return { + title: `模型的输出额度在推理阶段用完,${n ? `${n} 个` : ''}字段记为空值(未以 0 代替)`, + sub: `按出处抽取字段时输出被截断${budget ? `(已加大额度重试,最后一次为 ${formatNumber(budget)} token)` : ''},` + + '结构化结论没能写出来', + next: '这些字段以空值传给下游,口径卡按缺少输入处理,报告不得据此断言没有数据。请为该 Agent 换用非推理模型' + + '(抽取沿用 Agent 的模型),或调大它的「最大输出 token」后重新运行', + fix: 'canvas', + fixField: 'max_tokens', + } + } case 'metric_incomplete': { // 结构化字段(metrics.py):metric_name、reason(「基于被截断的查询结果计算(只取回了前 1000 行),结果不完整」)。 // 老运行的日志没有这两个字段:只给不点名的标题,原话留在展开区,不从中文里解析 diff --git a/scripts/check-decode.mjs b/scripts/check-decode.mjs index c3ef2f87..5045adb1 100644 --- a/scripts/check-decode.mjs +++ b/scripts/check-decode.mjs @@ -1481,6 +1481,47 @@ await section('agent 字段按出处核对(可点击证据第二期):抽 check('「抽取结果格式有误(不是对象)」:同样说「失败,N 个字段记为空值」,原因放副标题,不说「出处不存在」', notObject?.title === '按出处抽取字段失败,2 个字段记为空值(未以 0 代替)' && notObject?.sub === '抽取结果格式有误(不是对象)' && !notObject?.next?.includes('出处在查询结果中不存在'), `${notObject?.title} / ${notObject?.sub} / ${notObject?.next}`) + // 两次抽取都被截断(2026-10 一次运行:推理模型关不掉思考,4096 的额度在推理阶段就用完了):单独的 code, + // 说清是额度问题、给出能做的下一步,不说「稍后重试」「去设置测试该模型」(llm.py TRUNCATED_EXTRACT、TRUNCATED_NEXT) + const TRUNCATED = '结构化抽取没有完成:模型的输出额度在推理阶段就用完了(最后一次的额度为 32768 token),结构化结论没能写出来。' + + '5 个字段都记为空值(未以 0 代替)。可以为该 Agent 换用非推理模型(抽取沿用 Agent 的模型),或者调大该 Agent 的「最大输出 token」后重新运行' + check('后端发截断警告的说法还是夹具里这一种', llmPy.includes('"agent_field_truncated"') + && llmPy.includes('结构化抽取没有完成:模型的输出额度在推理阶段就用完了(最后一次的额度为 {budget} token),"') + && llmPy.includes('"结构化结论没能写出来"') && llmPy.includes('可以为该 Agent 换用非推理模型(抽取沿用 Agent 的模型),或者调大该 Agent 的"') + && llmPy.includes('已把额度加大到 {bigger} token 重试一次'), '改了 llm.py 的说法就同步改 decode.ts 的 explainLog 和这里的夹具') + const FIELDS5 = ['members', 'stores', 'coupons', 'redeemed', 'rate'] + const cut = flatten(mod.decodeRun([ + E(1, 'node.started', 'fetch', { node_type: 'agent', label: '会员专项' }), + E(2, 'llm.start', 'fetch', { model: 'm1', structured: true, purpose: 'cite_fields', message_count: 5, max_tokens: 16384 }), + E(3, 'llm.end', 'fetch', { agent: '会员专项', model: 'm1', purpose: 'cite_fields', duration_ms: 40000, output_tokens: 16384, + error: '输出被截断:16384 token 的输出额度用完了,结构化结论没有写完,已把额度加大到 32768 token 重试一次' }), + E(4, 'llm.start', 'fetch', { model: 'm1', structured: true, purpose: 'cite_fields', message_count: 5, max_tokens: 32768 }), + E(5, 'llm.end', 'fetch', { agent: '会员专项', model: 'm1', purpose: 'cite_fields', duration_ms: 80000, output_tokens: 32768, + error: '输出被截断:32768 token 的输出额度用完了,结构化结论没有写完' }), + E(6, 'log', 'fetch', { level: 'warn', code: 'agent_field_truncated', message: TRUNCATED, fields: FIELDS5, max_tokens: 32768 }), + E(7, 'node.finished', 'fetch', { duration_ms: 125000 }), + ])) + const tr = cut.find((s) => s.code === 'agent_field_truncated') + check('agent_field_truncated:标题说额度在推理阶段用完、几个字段记为空值', tr?.title === '模型的输出额度在推理阶段用完,5 个字段记为空值(未以 0 代替)', tr?.title) + check('……副标题说抽取时输出被截断、重试到的额度(读 max_tokens 字段)', + tr?.sub === '按出处抽取字段时输出被截断(已加大额度重试,最后一次为 32,768 token),结构化结论没能写出来', tr?.sub) + check('……下一步:换非推理模型或调大最大输出 token;不说稍后重试,不叫人去测试模型', + !!tr?.next?.includes('非推理模型') && !!tr?.next?.includes('「最大输出 token」') && !tr?.next?.includes('稍后重试') + && !tr?.next?.includes('测试') && !!tr?.next?.includes('不得据此断言没有数据'), tr?.next) + check('……入口打开该 Agent 的设置、落到最大输出 token;是提醒(warn),原话留在展开区', + tr?.fix === 'canvas' && tr?.fixField === 'max_tokens' && tr?.level === 'warn' && tr?.detail === TRUNCATED, + `${tr?.fix} ${tr?.fixField} ${tr?.level}`) + const tries = cut.filter((s) => s.kind === 'llm') + check('……两次抽取各成一行,第一次的展开区写明已加大额度重试', tries.length === 2 + && !!tries[0].detail?.includes('已把额度加大到 32768 token 重试一次') && !!tries[1].detail?.includes('结构化结论没有写完'), + tries.map((s) => s.detail).join(' | ')) + check('……节点本身照样成功', cut.find((s) => s.kind === 'node')?.status === 'done') + const bareCut = flatten(mod.decodeRun([E(1, 'node.started', 'x', { node_type: 'agent' }), + E(2, 'log', 'x', { level: 'warn', code: 'agent_field_truncated', message: '结构化抽取没有完成。3 个字段都记为空值(未以 0 代替)' })])) + .find((s) => s.code === 'agent_field_truncated') + check('……缺 fields、max_tokens 字段时字段数从原话里认,不出 undefined', + bareCut?.title === '模型的输出额度在推理阶段用完,3 个字段记为空值(未以 0 代替)' + && bareCut?.sub === '按出处抽取字段时输出被截断,结构化结论没能写出来', `${bareCut?.title} / ${bareCut?.sub}`) const node = mod.decodeRun([E(1, 'node.started', 'fetch', { node_type: 'agent' }), E(2, 'node.finished', 'fetch', { duration_ms: 1 })]) check('对照:普通 agent 节点不多出任何行', flatten(node).filter((s) => s.kind !== 'node').length === 0) const odd = flatten(mod.decodeRun([E(1, 'node.started', 'x', { node_type: 'agent' }),