From 4857c578f6707b85fc410dc4aa1c8c6099b75df3 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Tue, 13 Oct 2026 16:46:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=B7=E8=8D=89=E7=9A=84=E6=AD=A2=E6=8D=9F?= =?UTF-8?q?=E5=92=8C=E5=85=AC=E5=B9=B3=EF=BC=9A=E8=BF=9E=E7=BB=AD=202=20?= =?UTF-8?q?=E6=AE=B5=EF=BC=88MODEL=5FGIVE=5FUP=5FAFTER=EF=BC=89=E6=8B=86?= =?UTF-8?q?=E5=88=B0=E5=BA=95=E4=BB=8D=E4=B8=80=E9=A1=B9=E6=B2=A1=E5=86=99?= =?UTF-8?q?=E5=87=BA=E6=9D=A5=EF=BC=8C=E8=BF=99=E6=AC=A1=E8=B5=B7=E8=8D=89?= =?UTF-8?q?=E5=90=8E=E9=9D=A2=E7=9A=84=E6=AE=B5=E4=B8=8D=E5=86=8D=E6=8B=86?= =?UTF-8?q?=E5=B0=8F=E9=87=8D=E8=AF=95=EF=BC=8C=E6=B2=A1=E5=86=99=E5=87=BA?= =?UTF-8?q?=E6=9D=A5=E5=B0=B1=E7=9B=B4=E6=8E=A5=E8=AE=B0=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0=E5=90=8E=E9=9D=A2=E8=A1=A5=E3=80=8C=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E8=BF=9E=E7=BB=AD=202=20=E6=AE=B5=E9=83=BD=E6=B2=A1?= =?UTF-8?q?=E5=86=99=E5=87=BA=E6=9D=A5=EF=BC=8C=E5=B7=B2=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E6=8B=86=E5=B0=8F=E9=87=8D=E8=AF=95=EF=BC=8C=E5=BB=BA=E8=AE=AE?= =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E8=B5=B7=E8=8D=89=E6=A8=A1=E5=9E=8B=E3=80=8D?= =?UTF-8?q?=EF=BC=9B=E6=9C=89=E4=B8=80=E6=AE=B5=E5=86=99=E5=87=BA=E6=9D=A5?= =?UTF-8?q?=E5=B0=B1=E6=81=A2=E5=A4=8D=E6=8B=86=E5=B0=8F=E3=80=82=E6=AF=8F?= =?UTF-8?q?=E6=AC=A1=E8=B5=B7=E8=8D=89=E5=90=8C=E6=97=B6=E5=8F=AA=E8=B5=B7?= =?UTF-8?q?=E8=8D=89=203=20=E6=AE=B5=EF=BC=88MODEL=5FCONCURRENCY=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E4=B8=80=E6=AE=B5=E8=BF=9E=E5=90=8C=E6=8B=86=E5=B0=8F?= =?UTF-8?q?=E3=80=81=E8=A1=A5=E7=BC=BA=E5=81=9A=E5=AE=8C=E5=86=8D=E5=8F=96?= =?UTF-8?q?=E4=B8=8B=E4=B8=80=E6=AE=B5=EF=BC=8C=E5=AE=BD=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E8=A1=A8=E7=BA=A7=E9=82=A3=E6=AE=B5=E5=86=99=E5=AE=8C=EF=BC=8C?= =?UTF-8?q?=E5=AE=83=E7=9A=84=E5=88=97=E5=88=86=E6=AE=B5=E6=8E=92=E5=88=B0?= =?UTF-8?q?=E6=9C=80=E5=89=8D=E9=9D=A2=E4=B8=80=E6=AE=B5=E4=B8=80=E6=AE=B5?= =?UTF-8?q?=E8=B5=B7=E8=8D=89=EF=BC=9B=E5=90=8C=E6=97=B6=E5=9C=A8=E7=AD=89?= =?UTF-8?q?=E5=85=A8=E8=BF=9B=E7=A8=8B=E5=90=8D=E9=A2=9D=E7=9A=84=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=AF=8F=E6=AC=A1=E8=B5=B7=E8=8D=89=E6=9C=80=E5=A4=9A?= =?UTF-8?q?=203=20=E4=B8=AA=EF=BC=8C=E5=87=A0=E6=AC=A1=E8=B5=B7=E8=8D=89?= =?UTF-8?q?=EF=BC=88=E4=B8=8D=E5=90=8C=E6=95=B0=E6=8D=AE=E6=BA=90=E7=9A=84?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E3=80=81=E5=90=8C=E6=AD=A5=E8=B5=B7=E8=8D=89?= =?UTF-8?q?=EF=BC=89=E4=BA=A4=E6=9B=BF=E6=8E=92=E9=98=9F=EF=BC=8C=E5=90=8E?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E7=9A=84=E4=B8=8D=E5=86=8D=E7=AD=89=E5=89=8D?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=8E=92=E7=9D=80=E7=9A=84=E4=B8=8A=E7=99=BE?= =?UTF-8?q?=E6=AE=B5=E5=85=A8=E9=83=A8=E8=B0=83=E5=AE=8C=E3=80=82=E5=AE=9E?= =?UTF-8?q?=E6=B5=8B=E4=B8=80=E7=9B=B4=E5=8F=AA=E7=BB=99=E7=A9=BA=E5=A3=B3?= =?UTF-8?q?=E7=9A=84=20294=20=E5=88=97=E5=AE=BD=E8=A1=A8=E4=BB=8E=20168=20?= =?UTF-8?q?=E6=AC=A1=E8=B0=83=E7=94=A8=E9=99=8D=E5=88=B0=2081=20=E6=AC=A1?= =?UTF-8?q?=EF=BC=8C20=20=E5=BC=A0=20120=20=E5=88=97=E7=9A=84=E5=AE=BD?= =?UTF-8?q?=E8=A1=A8=E4=B8=80=E7=9B=B4=E7=94=A8=E5=AE=8C=E9=A2=9D=E5=BA=A6?= =?UTF-8?q?=E4=BB=8E=E7=BA=A6=20900=20=E6=AC=A1=E9=99=8D=E5=88=B0=20122=20?= =?UTF-8?q?=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/data/catalog.py | 3 + backend/app/data/catalog_draft.py | 138 ++++++++++++++++----- backend/tests/test_catalog_draft_engine.py | 91 +++++++++++++- 3 files changed, 199 insertions(+), 33 deletions(-) diff --git a/backend/app/data/catalog.py b/backend/app/data/catalog.py index 058c8b16..3d0d4275 100644 --- a/backend/app/data/catalog.py +++ b/backend/app/data/catalog.py @@ -1421,6 +1421,9 @@ MODEL_FALLBACK_MAX_TOKENS = 8192 #: 一段没写出来(额度用完、只给空壳)时拆成两半再问:最多拆几层、拆到最少几列为止 MODEL_SPLIT_DEPTH = 3 MODEL_SPLIT_MIN_COLUMNS = 5 +#: 止损:连续这么多段拆到底仍一项没写出来,这次起草后面的段不再拆小重试(一段 40 列拆到底要 15 次调用,模型明显 +#: 用不了时不该照这个倍数烧额度) +MODEL_GIVE_UP_AFTER = 2 #: 网络错误、429、5xx、超时最多重试几次,每次重试前等多久(秒,依次取用) MODEL_RETRIES = 2 MODEL_RETRY_DELAYS_S = (10.0, 30.0) diff --git a/backend/app/data/catalog_draft.py b/backend/app/data/catalog_draft.py index 2c08695c..bbacc754 100644 --- a/backend/app/data/catalog_draft.py +++ b/backend/app/data/catalog_draft.py @@ -30,9 +30,14 @@ - 网络错误、429、5xx、超时:等一会儿重试(最多 MODEL_RETRIES 次,退避)。单次调用的时限是 MODEL_TIMEOUT_S。SDK 自己不再重试(catalog.resolve_draft_model 建模型时 max_retries=0),实际重试次数和报错写的一致。 - 别的错误:这一段没写出来,原因照实写。 +- 止损:连续 MODEL_GIVE_UP_AFTER 段拆到底仍一项没写出来(模型明显用不了,例如思考总是吃满额度),这次起草后面的 + 段不再拆小重试、没写出来就直接记下,报错建议更换起草模型;有一段写出来了就恢复。不然一段拆到底要几十次调用, + 起草几十张宽表会烧掉数百万 token、跑几个小时。 **并发。** 全进程最多 MODEL_CONCURRENCY 个调用同时进行(model_gate:按事件循环各一个信号量,所有起草任务和同步 -起草共用)。 +起草共用,先来先放)。一次起草里同时只起草 MODEL_CONCURRENCY 段(一段拆小、补缺都做完再起草下一段),同时在等 +名额的调用也最多这么多个:不把几百段一下子都排上去,几次起草(不同数据源的任务、同步起草)的调用交替排队,后开始 +的不会等前一个排着的段全部调完。 **进度。** 调用方给 Hooks:开始调用、每次调用的用量、一段结束、一张表的模型部分全部结束。后台起草任务据此落库, 每张表一结束就并入目录(data/catalog_jobs.py)。 @@ -43,6 +48,7 @@ catalog.py 名字带 _SYSTEM / _PROMPT 的常量里;这里的 REASON_* 是给 from __future__ import annotations import asyncio +import collections import copy import json import logging @@ -81,6 +87,8 @@ REASON_TIMEOUT = "模型 {seconds} 秒内没有写完这一段" REASON_CALL = "调用模型出错:{line}" REASON_RETRIED = "(已重试 {n} 次)" REASON_CRASH = "起草这张表时出错:{line}" +#: 止损(连续几段拆到底仍一项没写出来,后面的段不再拆小重试)时补在原因后面 +REASON_HALTED = ";模型连续 {n} 段都没写出来,已停止拆小重试,建议更换起草模型" # ========================================================================== @@ -233,21 +241,25 @@ class _Reply: @dataclass(frozen=True) class _Why: - """一部分没写出来的原因。base 给人看的原因;split 拆小重试过。""" + """一部分没写出来的原因。base 给人看的原因;split 拆小重试过;halted 本来还能拆小,因为前面连续几段都没写出来 + (止损)才没拆。""" base: str split: bool = False + halted: bool = False def _join(whys: Iterable[_Why]) -> str: - """几处没写出来的原因拼成一句:同样的原因只说一次(最多说两种),拆小重试过的在最后补一次(只看说出来的那几种)。 - 不然宽表的表级那段拆不开、列分段拆过时,同一句原因会说两遍。""" + """几处没写出来的原因拼成一句:同样的原因只说一次(最多说两种),拆小重试过、止损的说明各在最后补一次(只看说出来 + 的那几种)。不然宽表的表级那段拆不开、列分段拆过时,同一句原因会说两遍。""" whys = list(whys) shown = list(dict.fromkeys(w.base for w in whys))[:2] told = [w for w in whys if w.base in shown] text = ";".join(shown) if any(w.split for w in told): text += REASON_SPLIT + if any(w.halted for w in told): + text += REASON_HALTED.format(n=max(1, int(C.MODEL_GIVE_UP_AFTER))) return text @@ -586,6 +598,10 @@ class _Table: """这张表已经拿到了模型起草的内容(表级的项或任何一列)。""" return self.got_table or bool(self.got_columns) + def wrote(self, piece: _Piece) -> bool: + """这一段请写的部分拿到了内容。""" + return (piece.table_level and self.got_table) or any(c in self.got_columns for c in piece.columns) + def _restrict(notes: Mapping[str, Any], piece: _Piece) -> dict[str, Any]: """只留这一段请模型写的部分:没请写表级的项就不要表级的项,列只要请写的那几列。""" @@ -599,6 +615,15 @@ def _restrict(notes: Mapping[str, Any], piece: _Piece) -> dict[str, Any]: return out +@dataclass +class _Work: + """排队起草的一段(顶层的段:窄表拼成的整表段、宽表的表级那段或一个列分段)。then:这一段写完才能起草的段(宽表 + 的表级那段写完,才轮到它的列分段)。""" + + pieces: list[_Piece] + then: list[_Work] = field(default_factory=list) + + def _halves(pieces: list[_Piece]) -> tuple[list[_Piece], list[_Piece]] | None: """拆成两半:多表的段按表拆;单表的按列拆,两半都至少 MODEL_SPLIT_MIN_COLUMNS 列(表级的项跟着前一半)。拆不开返回 None。""" if len(pieces) > 1: @@ -630,11 +655,52 @@ class _Run: for name in self.order } self.background = self._background(getattr(source, "description", None), background) + #: 这次起草同时在等名额或正在进行的调用最多几个(全进程的名额另算,见 model_gate) + self.gate = asyncio.Semaphore(max(1, int(C.MODEL_CONCURRENCY))) + #: 连续几段拆到底仍一项没写出来(止损用,有一段写出来就清零) + self.dead_streak = 0 + + @property + def halted(self) -> bool: + """止损:连续 MODEL_GIVE_UP_AFTER 段一项都没写出来,后面的段不再拆小重试。""" + return self.dead_streak >= max(1, int(C.MODEL_GIVE_UP_AFTER)) # ---------------------------------------------------------------- 总流程 async def run(self) -> dict[str, ModelOutcome]: - await asyncio.gather(*(self._unit(unit) for unit in self._plan())) + """按计划的先后起草:同时只起草 MODEL_CONCURRENCY 段,一段(连同拆小、补缺)做完再取下一段;宽表的表级那段 + 写完,它的列分段排到最前面。不一下子把所有段(包括宽表的全部列分段)都排上去:那样后开始的另一次起草要等这次 + 排着的段全部调完;拆小重试也排在所有段的第一次调用后面,一段要等几乎所有调用都做完才结束,止损形同虚设。""" + queue: collections.deque[_Work] = collections.deque() + for unit in self._plan(): + if isinstance(unit, list): + queue.append(_Work([self.tables[n].parts[0] for n in unit])) + else: + head, *rest = self.tables[unit].parts + # 表级那段先写:列分段拿它做背景 + queue.append(_Work([head], [_Work([p]) for p in rest])) + left = {name: len(st.parts) for name, st in self.tables.items()} + busy = 0 + changed = asyncio.Event() + + async def worker() -> None: + nonlocal busy + while True: + if queue: + work = queue.popleft() + busy += 1 + try: + await self._work(work, queue, left) + finally: + busy -= 1 + changed.set() + continue + if not busy: + return + changed.clear() # 队列空着、别的段还在起草(宽表的表级那段写完会排上列分段):等它们 + await changed.wait() + + await asyncio.gather(*(worker() for _ in range(max(1, int(C.MODEL_CONCURRENCY))))) return {name: self._outcome(t) for name, t in self.tables.items()} def _plan(self) -> list[list[str] | str]: @@ -661,24 +727,16 @@ class _Run: used += n return units - async def _unit(self, unit: list[str] | str) -> None: - names = unit if isinstance(unit, list) else [unit] - if isinstance(unit, list): - await self._guarded([self.tables[n].parts[0] for n in unit]) - for name in unit: - await self._part_done(name) - else: - head, *rest = self.tables[unit].parts - await self._guarded([head]) # 表级那段先写:列分段拿它做背景 - await self._part_done(unit) - - async def column_part(piece: _Piece) -> None: - await self._guarded([piece]) - await self._part_done(unit) - - await asyncio.gather(*(column_part(p) for p in rest)) - for name in names: - await self._table_done(name) + async def _work(self, work: _Work, queue: collections.deque[_Work], left: dict[str, int]) -> None: + """起草一段;它之后的段(宽表的列分段)排到最前面;一张表的段都结束了,这张表的模型部分就结束了。""" + await self._guarded(work.pieces) + for piece in work.pieces: + await self._part_done(piece.table) + queue.extendleft(reversed(work.then)) + for piece in work.pieces: + left[piece.table] -= 1 + if not left[piece.table]: + await self._table_done(piece.table) async def _guarded(self, pieces: list[_Piece]) -> None: """起草一段;意料之外的错误只记这一段没写出来,不影响别的段和别的表。""" @@ -691,6 +749,16 @@ class _Run: logger.exception("起草数据目录时出错:%s", "、".join(p.table for p in pieces)) self._fail(pieces, REASON_CRASH.format(line=first_line(e) or describe_exception(e))) + self._tally(pieces) + + def _tally(self, pieces: list[_Piece]) -> None: + """一段(连同拆小、补缺)结束:一项都没写出来的接着数,写出来了清零。""" + if any(self.tables[p.table].wrote(p) for p in pieces): + self.dead_streak = 0 + return + self.dead_streak += 1 + if self.dead_streak == max(1, int(C.MODEL_GIVE_UP_AFTER)): + logger.warning("起草数据目录:模型连续 %d 段都没写出来,这次起草后面的段不再拆小重试", self.dead_streak) # ---------------------------------------------------------------- 一段 @@ -727,7 +795,8 @@ class _Run: return if not anything and not followup: if last: - self._fail(pieces, lambda p: self._reason("hollow", split=depth > 0, piece=p)) + halted = self._stopped(pieces, depth) + self._fail(pieces, lambda p: self._reason("hollow", split=depth > 0, piece=p, halted=halted)) return await self._split(pieces, depth, "hollow", followup) return @@ -753,7 +822,8 @@ class _Run: if not empty: return if last: - self._fail(empty, lambda p: self._reason("hollow", split=depth > 0, piece=p)) + halted = self._stopped(empty, depth) + self._fail(empty, lambda p: self._reason("hollow", split=depth > 0, piece=p, halted=halted)) return await self._split(empty, depth, "hollow", False) @@ -765,9 +835,14 @@ class _Run: logger.info("起草数据目录:结构化输出回了说明文字,后面改走纯文本") await self._segment(pieces, depth, followup, mode="text", last=True) + def _stopped(self, pieces: list[_Piece], depth: int) -> bool: + """这几段本来还能拆小,因为前面连续几段都没写出来(止损)才没拆。""" + return self.halted and depth < int(C.MODEL_SPLIT_DEPTH) and _halves(pieces) is not None + async def _split(self, pieces: list[_Piece], depth: int, kind: str, followup: bool) -> None: halves = _halves(pieces) if depth < int(C.MODEL_SPLIT_DEPTH) else None - if halves is not None: + halted = halves is not None and self.halted + if halves is not None and not halted: first, second = halves if (len(first) == len(second) == 1 and first[0].table == second[0].table and first[0].table_level): # 同一张表按列拆、前一半带着表级的项:先写前一半,后一半拿它做背景 @@ -783,7 +858,7 @@ class _Run: if kind == "hollow" and self.mode == "structured": await self._segment(pieces, depth, followup, mode="text", last=True) return - self._fail(pieces, lambda p: self._reason(kind, split=depth > 0, piece=p)) + self._fail(pieces, lambda p: self._reason(kind, split=depth > 0, piece=p, halted=halted)) def _remainder(self, pieces: list[_Piece], gains: list[_Gain], *, truncated: bool) -> list[_Piece]: """还没写出来的部分:回复里没有这张表、或者这张表一项可用内容都没有的整段重问;有内容的只问没列出来的列 @@ -809,7 +884,8 @@ class _Run: while True: used = getattr(self.model, "budget", None) try: - async with model_gate(): + # 先占这次起草自己的名额,再去排全进程的:一次起草排在全进程名额上的调用最多 MODEL_CONCURRENCY 个 + async with self.gate, model_gate(): await self._hook(self.hooks.call_started, [p.table for p in pieces]) reply, raw = await asyncio.wait_for(self._call(pieces, mode), timeout=C.MODEL_TIMEOUT_S) # 用量记在时限之外:落库慢了不能把已经回来的这次调用算成超时 @@ -950,7 +1026,7 @@ class _Run: if piece.table_level and not st.got_table: st.failed.setdefault(_TABLE, why) - def _reason(self, kind: str, *, split: bool, piece: _Piece | None = None) -> _Why: + def _reason(self, kind: str, *, split: bool, piece: _Piece | None = None, halted: bool = False) -> _Why: if kind == "exhausted": base = REASON_EXHAUSTED.format(budget=self._budget()) elif kind == "truncated": @@ -961,7 +1037,7 @@ class _Run: base = C.MODEL_EMPTY_REASON if st.named else REASON_MISSING else: base = REASON_HOLLOW - return _Why(base, split=split) + return _Why(base, split=split, halted=halted) # ---------------------------------------------------------------- 收尾 @@ -1121,5 +1197,5 @@ class _Run: __all__ = ["BUDGET_WORDS", "Background", "Hooks", "ModelOutcome", "REASON_CALL", "REASON_EXHAUSTED", "REASON_FORMAT", - "REASON_HOLLOW", "REASON_MISSING", "REASON_RETRIED", "REASON_SPLIT", "REASON_TIMEOUT", + "REASON_HALTED", "REASON_HOLLOW", "REASON_MISSING", "REASON_RETRIED", "REASON_SPLIT", "REASON_TIMEOUT", "REASON_TRUNCATED", "REASON_UNANSWERED", "classify_failure", "model_gate", "parts_for", "run_model_draft"] diff --git a/backend/tests/test_catalog_draft_engine.py b/backend/tests/test_catalog_draft_engine.py index b3e36962..4f17ee3f 100644 --- a/backend/tests/test_catalog_draft_engine.py +++ b/backend/tests/test_catalog_draft_engine.py @@ -594,7 +594,7 @@ async def test_tables_are_packed_up_to_40_columns_and_4_tables(): # -------------------------------------------------------------------------- -# 报错去重 +# 报错去重、止损、几次起草轮流用模型 # -------------------------------------------------------------------------- @@ -602,8 +602,9 @@ def _hollow(call: Call) -> Reply: return Reply(obj={"tables": [{"table": t} for t in call.asked]}) -async def test_the_same_reason_is_said_once_even_when_some_parts_were_split(): +async def test_the_same_reason_is_said_once_even_when_some_parts_were_split(monkeypatch): """宽表的表级那段拆不开、列分段拆过:原因只说一次,拆过的在最后补一句。""" + monkeypatch.setattr(catalog, "MODEL_GIVE_UP_AFTER", 99) model = Scripted(_hollow) got = await D.run_model_draft(model, _source({"wide45": _table(45)}), ["wide45"]) o = got["wide45"] @@ -618,6 +619,92 @@ def test_reasons_are_joined_by_their_base(): assert D._join([W(exhausted, split=True), W(D.REASON_HOLLOW)]) == \ f"{exhausted};{D.REASON_HOLLOW}{D.REASON_SPLIT}" assert D._join([W("甲"), W("乙"), W("丙", split=True)]) == "甲;乙", "没说出来的原因不补后缀" + assert D._join([W(D.REASON_HOLLOW, halted=True), W(D.REASON_HOLLOW)]) == \ + D.REASON_HOLLOW + D.REASON_HALTED.format(n=catalog.MODEL_GIVE_UP_AFTER) + + +def _by_table(model: Scripted) -> dict[str, int]: + out: dict[str, int] = {} + for call in model.calls: + for name in call.asked: + out[name] = out.get(name, 0) + 1 + return out + + +async def test_after_two_segments_in_a_row_come_to_nothing_the_rest_are_not_split(monkeypatch): + """模型明显用不了(思考总是吃满额度):连续 2 段拆到底仍一项没写出,后面的段只问一次、不再拆,报错说明已停止拆小重试。 + 有一段写出来了就恢复拆小。一次只起草一段(并发 1),先后是确定的。""" + monkeypatch.setattr(catalog, "MODEL_CONCURRENCY", 1) + monkeypatch.setattr(catalog, "MODEL_BATCH_TABLES", 1) + assert catalog.MODEL_GIVE_UP_AFTER == 2 + tables = {f"t{i}": _table(10) for i in range(1, 5)} + exhausted = Reply(text="", finish="length", thinking="……") + + model = Scripted(lambda call: exhausted) + got = await D.run_model_draft(model, _source(tables), list(tables)) + assert _by_table(model) == {"t1": 3, "t2": 3, "t3": 1, "t4": 1} + budget = D.REASON_EXHAUSTED.format(budget=32768) + assert got["t1"].error == budget + D.REASON_SPLIT + assert got["t3"].error == budget + ";模型连续 2 段都没写出来,已停止拆小重试,建议更换起草模型" + + ok = answering(tables) + model = Scripted(lambda call: ok(call) if "t3" in call.asked else exhausted) + got = await D.run_model_draft(model, _source(tables), list(tables)) + assert _by_table(model) == {"t1": 3, "t2": 3, "t3": 1, "t4": 3}, "t3 写出来了,t4 照常拆小" + assert got["t3"].status == "ok" and got["t4"].error == budget + D.REASON_SPLIT + + +async def test_the_column_parts_of_a_wide_table_stop_splitting_too(monkeypatch): + """宽表的列分段一段一段排队起草(一段拆到底再起草下一段),止损在宽表里同样起作用。 + + 95 列:表级那段拆不开(结构化、纯文本各一次);第 2 段 40 列拆到底(15 次结构化 + 8 次纯文本);这时已经连续 2 段 + 没写出来,第 3、4 段只问一次、换纯文本一次。""" + monkeypatch.setattr(catalog, "MODEL_CONCURRENCY", 1) + model = Scripted(_hollow) + got = await D.run_model_draft(model, _source({"wide95": _table(95)}), ["wide95"]) + assert len(model.calls) == 2 + 23 + 2 + 2 + assert got["wide95"].error == (D.REASON_HOLLOW + D.REASON_SPLIT + + ";模型连续 2 段都没写出来,已停止拆小重试,建议更换起草模型") + + +async def test_a_wide_table_that_never_comes_back_is_bounded_by_the_first_wave(): + """默认并发(3 段同时起草):294 列一直只给空壳,最多是表级那段 2 次、头 3 个列分段拆到底(各 23 次)、其余 5 段 + 各 2 次。以前所有列分段一下子都排上去、一层一层地拆,要 168 次。""" + model = Scripted(_hollow) + got = await D.run_model_draft(model, _source({"wide294": _table(294)}), ["wide294"]) + assert got["wide294"].status == "failed" + assert len(model.calls) <= 2 + 3 * 23 + 5 * 2 + + +async def test_a_big_draft_does_not_hold_the_model_ahead_of_a_later_one(monkeypatch): + """全进程的调用名额先来先放:一次起草不把它的所有段一下子都排上去,后开始的那次起草(另一个数据源)能轮上。""" + monkeypatch.setattr(catalog, "MODEL_CONCURRENCY", 1) + monkeypatch.setattr(catalog, "MODEL_BATCH_TABLES", 1) + big = {f"a{i}": _table(4) for i in range(1, 5)} + small = {"b1": _table(4)} + order: list[str] = [] + release = asyncio.Event() + + def handler(tables: dict) -> Callable[[Call], Any]: + ok = answering(tables) + + async def handle(call: Call) -> Reply: + order.append(next(iter(call.asked))) + if len(order) == 1: + await release.wait() + return ok(call) + return handle + + first = asyncio.create_task(D.run_model_draft(Scripted(handler(big)), _source(big), list(big))) + while not order: + await asyncio.sleep(0) + second = asyncio.create_task(D.run_model_draft(Scripted(handler(small)), _source(small), ["b1"])) + for _ in range(20): + await asyncio.sleep(0) + release.set() + got_big, got_small = await asyncio.gather(first, second) + assert set(_status(got_big).values()) == {"ok"} and _status(got_small) == {"b1": "ok"} + assert order == ["a1", "b1", "a2", "a3", "a4"] # --------------------------------------------------------------------------