mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 07:31:57 +00:00
点名的模型接入:发布前检查给每一处配修复,提示指到界面上真能改的地方,复核发现的几处漏查、误查补齐
多数节点(模型调用、Agent、报告撰写、多 Agent 协作、条件分支、结构校验、知识检索)、协作成员和全图默认的接入在检查器里没有下拉框,以前点名的接入坏了只能去设置里建一个同名接入。现在门禁接受接入表(governance.lint_for_publish 的 providers,接口层读好传进来,同数据目录 SQL 检查器),点名接入的问题成了门禁的一部分,自动修复给每一处一条要人选的修复:改用一个启用的接入,或不指定接入;选的接入不提供这一处写着的模型时模型一并去掉。一处一条(修复 id 带字段,复核只认那个字段),协作节点自己的接入和成员的接入各修各的。校验和发布检查的提示按位置写:有下拉框的说「在节点中换一个接入」,别处说「在发布前检查中改用其他接入」;剩下的只有接入问题时交给助手的说明也改成指向这些修复。 复核发现一并修:核查节点自己的开关关着、结构校验不许重试时不再查(执行时根本不取模型,以前会挡住发布);报告没写裁判配置时查全图默认里的那一份(以前放行,正式运行却全部未裁判);接入名不是字符串时查助手模型的接口不再 500,设置接口对门控、助手两组的这类值回 422 带字段;助手、表格导入、数据目录起草用的模型点名的接入不存在或已停用时照实说是哪个接入、去哪里改(以前说「尚未配置完成」,停用接入的下一步丢了),删掉两处已走不到的分支。 测试补齐:结构校验让模型修复、知识检索让模型重排、按需裁判各路径的不存在与已停用,助手、定时正式运行、裁判的两种情况,反方审稿节点上点名的接入;修复的候选、应用、拒收和图级默认。
This commit is contained in:
@@ -20,7 +20,7 @@ from app.api.coded import DATASOURCE_SCOPE_EMPTY, CodedHTTPException
|
||||
from app.api.copilot_context import DATASOURCE_BUDGET_CHARS as _DATASOURCE_BUDGET_CHARS # noqa: F401
|
||||
from app.api.copilot_context import DETAIL_TABLE_LIMIT as _DETAIL_TABLE_LIMIT # noqa: F401
|
||||
# 起别名:本文件底下有个叫 explain 的接口,generate 里又有个局部变量叫 raw,同名会互相盖掉
|
||||
from app.core.errors import explain as explain_error, graph_error, not_configured, raw as raw_error
|
||||
from app.core.errors import explain as explain_error, graph_error, raw as raw_error
|
||||
from app.data.sqlcheck import CHECK_CODES
|
||||
from app.db.base import get_session
|
||||
from app.db.models import Workflow
|
||||
@@ -567,7 +567,10 @@ async def set_copilot_model(
|
||||
|
||||
|
||||
def _unconfigured(e: ProviderNotConfigured) -> str:
|
||||
return f"助手使用的模型尚未配置完成:{not_configured(e, with_hint=False)}。请到「设置 → 模型接入」检查"
|
||||
# 点名的接入不存在、已停用照实说是哪个、去哪里改,其余说「尚未配置完成」(表格导入、数据目录的 AI 起草同一句)
|
||||
from app.providers.factory import assistant_unavailable
|
||||
|
||||
return assistant_unavailable(e)
|
||||
|
||||
|
||||
class GenerateOut(BaseModel):
|
||||
|
||||
@@ -745,12 +745,18 @@ async def _provider_problem(session: AsyncSession, values: dict[str, Any]) -> tu
|
||||
|
||||
以前点名的接入不存在时运行悄悄换成演示模型,名字写错一个字就由演示模型写报告、做核查,现在运行时照实报错;保存时
|
||||
先拦一道。留空照旧表示沿用上一级。只查这次改了的:存着的值没改(写好之后接入才被删、停用)不挡这次保存别的项——
|
||||
设置页标着「不存在」,运行时照实报错,同设置页「只校验改过的」的做法。名字不是字符串这类格式问题由各组自己的
|
||||
检查先报。"""
|
||||
设置页在那一项下标着「不存在」「已停用」,运行时照实报错,同设置页「只校验改过的」的做法。
|
||||
|
||||
名字不是字符串(接口写进来的数字一类):核查、裁判两组自己的格式检查在前面先报;门控、助手两组没有自己的检查,
|
||||
在这里报,免得存下去之后读它的地方(查助手当前用哪个模型)出错。"""
|
||||
providers = None
|
||||
for (group, key), (label, blank) in _provider_fields().items():
|
||||
sent = values.get(group)
|
||||
if not isinstance(sent, dict) or (name := _model_text(sent.get(key))) is None:
|
||||
if not isinstance(sent, dict):
|
||||
continue
|
||||
if (raw := sent.get(key)) is not None and not isinstance(raw, str):
|
||||
return f"{group}.{key}", f"「{label}」需要填写模型接入的名称,{blank},当前为「{raw}」"
|
||||
if (name := _model_text(raw)) is None:
|
||||
continue
|
||||
row = await session.get(Setting, group)
|
||||
saved = row.value if row is not None and isinstance(row.value, dict) else {}
|
||||
|
||||
@@ -14,6 +14,7 @@ from app.data.catalog_impact import catalog_drift, catalog_versions_for, recorde
|
||||
from app.db.base import get_session
|
||||
from app.db.models import Run, Workflow, WorkflowVersion
|
||||
from app.engine.governance import publish_issues
|
||||
from app.engine.provider_refs import CODES as PROVIDER_CODES, load_issues, load_providers
|
||||
from app.engine.schema import GraphSpec, NodeType, validate_graph
|
||||
|
||||
router = APIRouter(prefix="/api/workflows", tags=["workflows"])
|
||||
@@ -21,8 +22,9 @@ router = APIRouter(prefix="/api/workflows", tags=["workflows"])
|
||||
_GONE = "工作流不存在,可能已被删除"
|
||||
_LEVELS = ("published", "governed")
|
||||
_BAD_LEVEL = "发布级别只能是「已发布」或「受管」"
|
||||
#: 自动修复之后剩下的只有点名接入的问题、人又要交给助手时的说明
|
||||
PROVIDER_LEFT = "剩下的问题是节点指定的模型接入不存在或已停用,助手无法代为处理:请在「设置 → 模型接入」中核对,或在节点中换一个接入"
|
||||
#: 剩下的挡住发布的只有点名接入的问题、人又要交给助手时的说明:换成哪个接入要人拿主意,每一处都有自己的修复
|
||||
PROVIDER_LEFT = ("剩下的问题是指定的模型接入不存在或已停用,需要你决定改用哪个接入:请在每一处的修复中选择,"
|
||||
"或在「设置 → 模型接入」中核对")
|
||||
|
||||
|
||||
class WorkflowIn(BaseModel):
|
||||
@@ -336,8 +338,8 @@ async def publish_workflow(
|
||||
return {"ok": False, "issues": [{"level": "error", "message": f"无法解析工作流结构:{graph_error(e)}"}]}
|
||||
|
||||
checkers = await _sql_checkers(session, spec)
|
||||
issues = [i.model_dump() for i in publish_issues(spec, level=payload.level, checkers=checkers)]
|
||||
issues += await _provider_issues(session, spec, level="error")
|
||||
issues = [i.model_dump() for i in publish_issues(spec, level=payload.level, checkers=checkers,
|
||||
providers=await _providers(session, spec))]
|
||||
if any(i["level"] == "error" for i in issues):
|
||||
return {"ok": False, "level": payload.level, "version": version, "issues": issues}
|
||||
|
||||
@@ -424,20 +426,11 @@ async def _sql_checkers(session: AsyncSession, spec: GraphSpec) -> dict[str, Any
|
||||
return await load_checkers_by_name(session, graph_sources(spec.nodes))
|
||||
|
||||
|
||||
async def _provider_issues(session: AsyncSession, spec: GraphSpec, *, level: str) -> list[dict[str, Any]]:
|
||||
"""节点点名的模型接入不存在或已停用(engine/provider_refs.py):画布校验给 warning,发布、发布前检查、自动修复的
|
||||
复核给 error、挡住发布。要读接入表,和 _sql_checkers 一样由接口这一层查,门禁本身不查库。没有对应的修复。"""
|
||||
from app.engine.provider_refs import load_issues
|
||||
|
||||
return [i.model_dump() for i in await load_issues(session, spec, level=level)]
|
||||
|
||||
|
||||
def _with_provider_issues(out: dict[str, Any], issues: list[dict[str, Any]], key: str) -> dict[str, Any]:
|
||||
"""发布前检查(issues)、自动修复(remaining)的结果补上点名接入的问题:都是 error,有就不能发布。"""
|
||||
if issues:
|
||||
out[key] = [*out[key], *issues]
|
||||
out["ok"] = False
|
||||
return out
|
||||
async def _providers(session: AsyncSession, spec: GraphSpec) -> list[Any]:
|
||||
"""模型接入表:节点点名的接入不存在或已停用时,发布、发布前检查、自动修复的复核给 error、挡住发布,修复从启用的
|
||||
接入里列候选(engine/provider_refs.py、autofix 的 provider.*)。和 _sql_checkers 一样由接口这一层读好交给门禁,
|
||||
门禁本身不查库;三处传同一份。图里没点名任何接入时不读库。"""
|
||||
return await load_providers(session, spec)
|
||||
|
||||
|
||||
def _unreadable(level: str, e: Exception) -> dict[str, Any]:
|
||||
@@ -462,8 +455,8 @@ async def publish_check(
|
||||
except Exception as e: # noqa: BLE001
|
||||
return _unreadable(payload.level, e)
|
||||
versions, cards = await _gate_context(session, spec)
|
||||
out = check(spec, level=payload.level, versions=versions, cards=cards, checkers=await _sql_checkers(session, spec))
|
||||
return _with_provider_issues(out, await _provider_issues(session, spec, level="error"), "issues")
|
||||
return check(spec, level=payload.level, versions=versions, cards=cards, checkers=await _sql_checkers(session, spec),
|
||||
providers=await _providers(session, spec))
|
||||
|
||||
|
||||
@router.post("/{workflow_id}/autofix")
|
||||
@@ -488,17 +481,19 @@ async def autofix(
|
||||
"remaining": bad["issues"], "fixes": [], "handoff": [], "assist": None, "ok": False}
|
||||
versions, cards = await _gate_context(session, spec)
|
||||
checkers = await _sql_checkers(session, spec)
|
||||
providers = await _providers(session, spec)
|
||||
out = apply_fixes(graph, payload.apply, payload.choices, level=payload.level, versions=versions, cards=cards,
|
||||
checkers=checkers)
|
||||
checkers=checkers, providers=providers)
|
||||
out["assist"] = None
|
||||
# 人在选项里选了「交给 Copilot」(handoff),和点「交给 Copilot」是一回事
|
||||
if payload.assist or out["handoff"]:
|
||||
if out["ok"]:
|
||||
# 剩下的只有点名接入的问题时不这么说:助手改不了接入表,也不知道该换成哪个接入
|
||||
lost = await _provider_issues(session, GraphSpec.model_validate(out["graph"]), level="error")
|
||||
out["assist"] = {"ok": not lost, "summary": PROVIDER_LEFT if lost else "修复后已没有阻止发布的错误,未再交给助手",
|
||||
"questions": []}
|
||||
return _with_provider_issues(out, lost, "remaining")
|
||||
out["assist"] = {"ok": True, "summary": "修复后已没有阻止发布的错误,未再交给助手", "questions": []}
|
||||
return out
|
||||
if all(i.get("code") in PROVIDER_CODES for i in out["remaining"] if i["level"] == "error"):
|
||||
# 剩下的只有点名接入的问题:换成哪个接入要人拿主意(助手的自查也不看接入表),不交给助手
|
||||
out["assist"] = {"ok": False, "summary": PROVIDER_LEFT, "questions": []}
|
||||
return out
|
||||
from app.api.copilot import assist_publish_fix
|
||||
|
||||
helped = await assist_publish_fix(session, out["graph"], level=payload.level,
|
||||
@@ -509,13 +504,14 @@ async def autofix(
|
||||
out["changes"] += diff_changes(out["graph"], fixed, fix_id="assist", label="助手的修改")
|
||||
out["ops"] += helped["ops"]
|
||||
out["applied"].append("assist")
|
||||
fixed_spec = GraphSpec.model_validate(fixed)
|
||||
after = apply_fixes(fixed, [], level=payload.level, versions=versions, cards=cards,
|
||||
checkers=await _sql_checkers(session, GraphSpec.model_validate(fixed)))
|
||||
checkers=await _sql_checkers(session, fixed_spec),
|
||||
providers=await _providers(session, fixed_spec))
|
||||
out.update(graph=fixed, remaining=after["remaining"], fixes=after["fixes"], ok=after["ok"])
|
||||
elif helped["reason"]:
|
||||
out["rejected"].append({"fix_id": "assist", "reason": helped["reason"]})
|
||||
return _with_provider_issues(out, await _provider_issues(session, GraphSpec.model_validate(out["graph"]),
|
||||
level="error"), "remaining")
|
||||
return out
|
||||
|
||||
|
||||
class ValidateIn(BaseModel):
|
||||
@@ -532,7 +528,7 @@ async def validate(payload: ValidateIn, session: AsyncSession = Depends(get_sess
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"ok": False, "issues": [{"level": "error", "message": f"无法解析工作流结构:{graph_error(e)}"}]}
|
||||
result = validate_graph(spec).model_dump()
|
||||
result["issues"] += await _provider_issues(session, spec, level="warning")
|
||||
result["issues"] += [i.model_dump() for i in await load_issues(session, spec, level="warning")]
|
||||
return result
|
||||
|
||||
|
||||
|
||||
@@ -1509,34 +1509,30 @@ async def resolve_draft_model(session: AsyncSession) -> tuple[Any, str]:
|
||||
起草出来的目录会被当成真的写进库里。数据层不导入接口层,设置键取 recipe_ai 里那份(与助手的同值,
|
||||
那边有测试核对)。
|
||||
"""
|
||||
from app.core.errors import not_configured
|
||||
from app.data.recipe_ai import COPILOT_SETTING_KEY
|
||||
from app.db.models import Setting
|
||||
from app.providers.factory import ModelSpec, ProviderNotConfigured, build_chat_model, resolve_provider
|
||||
from app.providers.factory import (
|
||||
ModelSpec, ProviderNotConfigured, assistant_unavailable, build_chat_model, resolve_provider,
|
||||
)
|
||||
|
||||
row = await session.get(Setting, COPILOT_SETTING_KEY)
|
||||
saved = (row.value if row else None) or {}
|
||||
spec = ModelSpec(provider=saved.get("provider") or None, model=saved.get("model") or None,
|
||||
max_tokens=MODEL_MAX_TOKENS, temperature=0, thinking="off")
|
||||
unconfigured = "助手使用的模型尚未配置完成:{reason}。请到「设置 → 模型接入」检查"
|
||||
try:
|
||||
# 存的接入名不存在、已停用,只存了模型名、提供它的接入已停用:resolve_provider 都会报出来
|
||||
provider = await resolve_provider(session, spec.provider, spec.model)
|
||||
except ProviderNotConfigured as e:
|
||||
raise CatalogModelUnavailable(unconfigured.format(reason=not_configured(e, with_hint=False))) from e
|
||||
raise CatalogModelUnavailable(assistant_unavailable(e)) from e
|
||||
if provider is None:
|
||||
if spec.provider:
|
||||
raise CatalogModelUnavailable(f"助手设置里的模型接入「{spec.provider}」不存在。请到「设置 → 模型接入」检查")
|
||||
raise CatalogModelUnavailable("未配置模型接入,无法由助手起草数据目录。请到「设置 → 模型接入」添加")
|
||||
if provider.kind == "mock":
|
||||
raise CatalogModelUnavailable("当前只有演示用的模型接入,无法用于起草数据目录。"
|
||||
"请到「设置 → 模型接入」添加真实的模型接入")
|
||||
if not provider.enabled:
|
||||
raise CatalogModelUnavailable(f"助手使用的模型接入「{provider.name}」已停用。"
|
||||
"请到「设置 → 模型接入」启用它,或为助手换一个模型")
|
||||
try:
|
||||
model = build_chat_model(provider, spec)
|
||||
except ProviderNotConfigured as e:
|
||||
raise CatalogModelUnavailable(unconfigured.format(reason=not_configured(e, with_hint=False))) from e
|
||||
raise CatalogModelUnavailable(assistant_unavailable(e)) from e
|
||||
return model, spec.model or provider.default_model or ""
|
||||
|
||||
|
||||
|
||||
@@ -253,10 +253,10 @@ class _BudgetFallback:
|
||||
|
||||
|
||||
def _unconfigured(e: BaseException) -> str:
|
||||
"""写法照 copilot._unconfigured。"""
|
||||
from app.core.errors import not_configured
|
||||
"""和 copilot._unconfigured 同一句(providers/factory.assistant_unavailable)。"""
|
||||
from app.providers.factory import assistant_unavailable
|
||||
|
||||
return f"助手使用的模型尚未配置完成:{not_configured(e, with_hint=False)}。请到「设置 → 模型接入」检查"
|
||||
return assistant_unavailable(e)
|
||||
|
||||
|
||||
async def resolve_draft_model(session: AsyncSession) -> tuple[Any | None, str, AiAvailability]:
|
||||
@@ -271,21 +271,13 @@ async def resolve_draft_model(session: AsyncSession) -> tuple[Any | None, str, A
|
||||
try:
|
||||
provider = await resolve_provider(session, spec.provider, spec.model)
|
||||
except ProviderNotConfigured as e:
|
||||
# 只存了模型名、提供它的接入已停用:resolve_provider 自己会报出来
|
||||
# 存的接入名不存在、已停用,只存了模型名、提供它的接入已停用:resolve_provider 都会报出来
|
||||
return None, "", AiAvailability(False, reason=_unconfigured(e))
|
||||
if provider is None:
|
||||
if spec.provider:
|
||||
reason = f"助手设置里的模型接入「{spec.provider}」不存在。请到「设置 → 模型接入」检查"
|
||||
else:
|
||||
reason = "未配置模型接入,无法使用 AI 起草。请到「设置 → 模型接入」添加"
|
||||
return None, "", AiAvailability(False, reason=reason)
|
||||
return None, "", AiAvailability(False, reason="未配置模型接入,无法使用 AI 起草。请到「设置 → 模型接入」添加")
|
||||
if provider.kind == "mock":
|
||||
return None, "", AiAvailability(False, reason="当前只有演示用的模型接入,无法用于 AI 起草。"
|
||||
"请到「设置 → 模型接入」添加真实的模型接入")
|
||||
if not provider.enabled:
|
||||
# 存的是接入名时 resolve_provider 会把停用的接入原样返回,build_chat_model 也不查 enabled
|
||||
return None, "", AiAvailability(False, reason=f"助手使用的模型接入「{provider.name}」已停用。"
|
||||
"请到「设置 → 模型接入」启用它,或为助手换一个模型")
|
||||
try:
|
||||
big = build_chat_model(provider, spec)
|
||||
small = build_chat_model(provider, spec.model_copy(update={"max_tokens": AI_FALLBACK_MAX_TOKENS}))
|
||||
|
||||
+150
-22
@@ -54,6 +54,7 @@ _ORDER = (
|
||||
"governed.caliber_agent_cite_fields", "governed.caliber_compute_input", "governed.agent_approval_never",
|
||||
"governed.default_approval_never", "governed.subgraph_unpinned", "governed.supervisor", "contract.not_object",
|
||||
"governed.text_bypass", "governed.caliber_agent_schema", "governed.caliber_model_input",
|
||||
"provider.missing", "provider.disabled",
|
||||
)
|
||||
|
||||
#: 修复落在全图上、不落在任何节点上的问题:问题各挂在跟随全图默认的那几个节点上(点一条能定位),
|
||||
@@ -85,12 +86,14 @@ _GRAPH_OPS = frozenset({"add_node", "update_node", "add_edge"})
|
||||
|
||||
class _Ctx:
|
||||
def __init__(self, spec: GraphSpec, versions: dict[str, Any] | None, cards: dict[str, Any] | None,
|
||||
level: str = "governed") -> None:
|
||||
level: str = "governed", providers: list[Any] | None = None) -> None:
|
||||
self.spec = spec
|
||||
self.nodes = spec.node_map()
|
||||
self.versions = versions or {}
|
||||
self.cards = cards or {}
|
||||
self.level = level
|
||||
#: 模型接入表(provider_refs.load_providers):点名的接入出了问题时,候选从里面启用的那些里挑
|
||||
self.providers = providers or []
|
||||
|
||||
def upstream(self, node_id: str, *types: NodeType) -> list[GraphNode]:
|
||||
"""这个节点上游的某几类节点,按画布上的节点顺序。"""
|
||||
@@ -459,6 +462,90 @@ def _plan_not_object(ctx: _Ctx, issue: dict[str, Any], node: GraphNode | None) -
|
||||
return _assist(f"「{_title(node)}」的出具契约不是一个 JSON 对象", "contract")
|
||||
|
||||
|
||||
#: 「不指定接入」那个候选的值:去掉这一处点名的接入
|
||||
PROVIDER_CLEAR = ""
|
||||
|
||||
|
||||
def _path_steps(path: str) -> list[str | int]:
|
||||
"""agents[0].provider → ['agents', 0, 'provider']。"""
|
||||
return [int(i) if i else key for i, key in re.findall(r"\[(\d+)\]|([^.\[\]]+)", path)]
|
||||
|
||||
|
||||
def _step(root: Any, step: str | int) -> Any:
|
||||
if isinstance(step, int):
|
||||
return root[step] if isinstance(root, list) and step < len(root) else None
|
||||
return root.get(step) if isinstance(root, dict) else None
|
||||
|
||||
|
||||
def _read_path(root: Any, path: str) -> Any:
|
||||
for step in _path_steps(path):
|
||||
if (root := _step(root, step)) is None:
|
||||
return None
|
||||
return root
|
||||
|
||||
|
||||
def _put_path(root: dict[str, Any], path: str, value: Any) -> Any:
|
||||
"""按路径写(provider、judge.provider、agents[0].provider),value 为 None 时去掉这一项。返回原值;
|
||||
路径上缺了哪一层就什么都不写,返回 None。"""
|
||||
*heads, last = _path_steps(path)
|
||||
cur: Any = root
|
||||
for step in heads:
|
||||
cur = _step(cur, step)
|
||||
if not isinstance(cur, dict) or not isinstance(last, str):
|
||||
return None
|
||||
before = copy.deepcopy(cur.get(last))
|
||||
if value is None:
|
||||
cur.pop(last, None)
|
||||
else:
|
||||
cur[last] = copy.deepcopy(value)
|
||||
return before
|
||||
|
||||
|
||||
def _model_path(path: str) -> str:
|
||||
"""接入旁边的模型:provider → model,judge.provider → judge.model,agents[0].provider → agents[0].model。"""
|
||||
return path[: -len("provider")] + "model"
|
||||
|
||||
|
||||
def _serves(provider: Any, model: str) -> bool:
|
||||
models = [m.get("id") for m in getattr(provider, "models", None) or [] if isinstance(m, dict)]
|
||||
return model in models or model == getattr(provider, "default_model", None)
|
||||
|
||||
|
||||
def _plan_provider(ctx: _Ctx, issue: dict[str, Any], node: GraphNode | None) -> dict[str, Any] | None:
|
||||
"""点名的模型接入不存在或已停用(engine/provider_refs.py):改用一个启用的接入,或者不再指定。
|
||||
|
||||
要人选、不给默认:换成哪个接入只有人知道(同一个模型可能有两家提供,价格、配额都不同)。多数节点的检查器里没有
|
||||
接入下拉框(只能选模型),全图默认、协作成员也没有,这里是界面上唯一能改它的地方。同一处写着模型、选的接入
|
||||
又不提供它时,模型一并去掉、改用那个接入的默认模型——拿 A 家的模型名去调 B 家必然失败(设置页换接入时同一个
|
||||
做法),候选的说明里写清。一处一条(plan_fixes 的 id 带字段):协作节点自己的接入和成员的接入各修各的。
|
||||
"""
|
||||
from app.engine.provider_refs import provider_refs
|
||||
|
||||
field = str(issue.get("field") or "")
|
||||
ref = next((r for r in provider_refs(ctx.spec) if r.node_id == issue.get("node_id") and r.field == field), None)
|
||||
if ref is None:
|
||||
return None
|
||||
path = field.removeprefix("defaults.") if ref.node_id is None else field
|
||||
root = ctx.spec.defaults if ref.node_id is None else (node.config if node is not None else {})
|
||||
model = _read_path(root, _model_path(path))
|
||||
model = model.strip() if isinstance(model, str) and model.strip() else None
|
||||
options: list[dict[str, Any]] = [{"value": PROVIDER_CLEAR, "label": "不指定接入",
|
||||
"hint": "去掉这一处指定的接入,按上一级的设置或模型名找接入"}]
|
||||
drop: list[str] = []
|
||||
for p in ctx.providers:
|
||||
if not getattr(p, "enabled", False) or not getattr(p, "name", None):
|
||||
continue
|
||||
notes = []
|
||||
if model and not _serves(p, model):
|
||||
drop.append(p.name)
|
||||
default = getattr(p, "default_model", None)
|
||||
notes.append(f"不提供模型「{model}」,将改用它的默认模型" + (f"「{default}」" if default else ""))
|
||||
if getattr(p, "kind", None) == "mock":
|
||||
notes.append("演示用的模型接入,不调用真实模型")
|
||||
options.append({"value": p.name, "label": p.name, "hint": ";".join(notes) or None})
|
||||
return _choice(f"选择{ref.who}改用的模型接入", field, options, _path=path, _drop_model=drop)
|
||||
|
||||
|
||||
_PLANNERS: dict[str, Callable[[_Ctx, dict[str, Any], GraphNode | None], dict[str, Any] | None]] = {
|
||||
"governed.agent_approval_never": _plan_approval,
|
||||
"governed.default_approval_never": _plan_default_approval,
|
||||
@@ -486,8 +573,14 @@ _PLANNERS: dict[str, Callable[[_Ctx, dict[str, Any], GraphNode | None], dict[str
|
||||
"governed.caliber_agent_cite_fields": _plan_cite_fields,
|
||||
"governed.caliber_agent_schema": _plan_agent_schema,
|
||||
"governed.caliber_model_input": _plan_model_input,
|
||||
# 点名的模型接入不存在或已停用(只在调用方交了接入表时才有这类问题)
|
||||
"provider.missing": _plan_provider,
|
||||
"provider.disabled": _plan_provider,
|
||||
}
|
||||
|
||||
#: 点名的模型接入这一类:一处一条修复,复核时只认那个字段上的那一条(同一节点上别的字段可能还有同类问题)
|
||||
_PER_FIELD = frozenset({"provider.missing", "provider.disabled"})
|
||||
|
||||
|
||||
def _as_dict(issue: Any) -> dict[str, Any]:
|
||||
return issue.model_dump() if isinstance(issue, BaseModel) else dict(issue)
|
||||
@@ -506,14 +599,16 @@ def _target(ctx: _Ctx, issue: dict[str, Any]) -> str | None:
|
||||
|
||||
|
||||
def plan_fixes(spec: GraphSpec, issues: list[Any], *, level: str, versions: dict[str, Any] | None = None,
|
||||
cards: dict[str, list[dict[str, Any]]] | None = None) -> list[dict[str, Any]]:
|
||||
cards: dict[str, list[dict[str, Any]]] | None = None,
|
||||
providers: list[Any] | None = None) -> list[dict[str, Any]]:
|
||||
"""这批问题能怎么修:Fix 列表,按问题出现的顺序,同一处只出一条。
|
||||
|
||||
versions:子工作流 id → 它当前的发布版本号(没发布过为 None)。
|
||||
cards:钉在别处(caliber_from)的口径卡节点 id → 那一版里的指标定义,用来列 required 的候选。
|
||||
providers:模型接入表,点名的接入出了问题时列候选(governance.lint_for_publish 的同一份)。
|
||||
返回的 Fix 里以 _ 开头的键是应用时要用的内部信息,交给前端前用 public() 去掉。
|
||||
"""
|
||||
ctx = _Ctx(spec, versions, cards, level)
|
||||
ctx = _Ctx(spec, versions, cards, level, providers)
|
||||
warned = _WARNING_FIXES.get(level, frozenset())
|
||||
out: dict[str, dict[str, Any]] = {}
|
||||
for issue in map(_as_dict, issues):
|
||||
@@ -522,7 +617,7 @@ def plan_fixes(spec: GraphSpec, issues: list[Any], *, level: str, versions: dict
|
||||
if planner is None or (issue.get("level") != "error" and code not in warned):
|
||||
continue
|
||||
target = _target(ctx, issue)
|
||||
fid = f"{code}:{target or 'graph'}"
|
||||
fid = f"{code}:{target or 'graph'}" + (f":{issue.get('field') or ''}" if code in _PER_FIELD else "")
|
||||
if fid in out:
|
||||
continue
|
||||
fix = planner(ctx, issue, ctx.nodes.get(target) if target else None)
|
||||
@@ -538,9 +633,11 @@ def public(fix: dict[str, Any]) -> dict[str, Any]:
|
||||
def annotate(issues: list[Any], fixes: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""问题的字典形状,带上对应的修复 id(fix)。validate 和门禁报的同一处问题指向同一个修复。
|
||||
|
||||
先按(编号、节点)认;认不出时,图级问题(没有节点)认同一编号的第一条修复,图级修复(不落在
|
||||
节点上,比如改全图默认)认同一编号的每一条问题。前端 canvas/issues.fixFor 是同一个退路。
|
||||
先按(编号、节点)认(点名的模型接入一处一条,同一节点上可能有几条,再带上字段);认不出时,图级问题(没有节点)
|
||||
认同一编号的第一条修复,图级修复(不落在节点上,比如改全图默认)认同一编号的每一条问题。前端 canvas/issues.fixFor
|
||||
是同一个退路。
|
||||
"""
|
||||
exact = {(f["code"], f["node_id"], f.get("field")): f["id"] for f in fixes if f["code"] in _PER_FIELD}
|
||||
by_node = {(f["code"], f["node_id"]): f["id"] for f in fixes}
|
||||
by_code: dict[str, str] = {}
|
||||
by_graph: dict[str, str] = {}
|
||||
@@ -551,7 +648,8 @@ def annotate(issues: list[Any], fixes: list[dict[str, Any]]) -> list[dict[str, A
|
||||
out = []
|
||||
for issue in map(_as_dict, issues):
|
||||
code = issue.get("code")
|
||||
fid = by_node.get((code, issue.get("node_id"))) if code else None
|
||||
fid = exact.get((code, issue.get("node_id"), issue.get("field"))) if code else None
|
||||
fid = fid or (by_node.get((code, issue.get("node_id"))) if code else None)
|
||||
if fid is None and code:
|
||||
fid = by_code.get(code) if issue.get("node_id") is None else by_graph.get(code)
|
||||
out.append({**issue, "fix": fid})
|
||||
@@ -560,13 +658,14 @@ def annotate(issues: list[Any], fixes: list[dict[str, Any]]) -> list[dict[str, A
|
||||
|
||||
def check(spec: GraphSpec, *, level: str, versions: dict[str, Any] | None = None,
|
||||
cards: dict[str, list[dict[str, Any]]] | None = None,
|
||||
checkers: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
checkers: dict[str, Any] | None = None, providers: list[Any] | None = None) -> dict[str, Any]:
|
||||
"""发布前检查:和真正发布同一套口径的问题,以及能怎么修。只读。
|
||||
|
||||
checkers:基于数据目录的 SQL 检查器(data/sqlcheck),和发布接口传的是同一份(governance.lint_for_publish)。
|
||||
checkers:基于数据目录的 SQL 检查器(data/sqlcheck),providers:模型接入表,都和发布接口传的是同一份
|
||||
(governance.lint_for_publish)。
|
||||
"""
|
||||
issues = publish_issues(spec, level=level, checkers=checkers)
|
||||
fixes = plan_fixes(spec, issues, level=level, versions=versions, cards=cards)
|
||||
issues = publish_issues(spec, level=level, checkers=checkers, providers=providers)
|
||||
fixes = plan_fixes(spec, issues, level=level, versions=versions, cards=cards, providers=providers)
|
||||
return {"level": level, "ok": not any(i.level == "error" for i in issues),
|
||||
"issues": annotate(issues, fixes), "fixes": [public(f) for f in fixes]}
|
||||
|
||||
@@ -659,6 +758,27 @@ def _edit_judge_budget(graph: dict[str, Any], fix: dict[str, Any], value: Any) -
|
||||
return [(str(node["id"]), "judge", before, copy.deepcopy(after))]
|
||||
|
||||
|
||||
def _edit_provider(graph: dict[str, Any], fix: dict[str, Any], value: Any) -> list[tuple[str | None, str, Any, Any]]:
|
||||
"""点名的模型接入(provider.*):落在节点上改节点配置,落在全图默认上改 defaults(节点 id 记 None,字段写全路径
|
||||
defaults.x)。选「不指定接入」去掉这一项;选的接入不提供同一处写着的模型时,模型一并去掉。"""
|
||||
if fix["node_id"] is None:
|
||||
if not isinstance(graph.get("defaults"), dict):
|
||||
graph["defaults"] = {}
|
||||
root, prefix = graph["defaults"], "defaults."
|
||||
else:
|
||||
node = _node(graph, fix["node_id"])
|
||||
if node is None:
|
||||
return []
|
||||
root, prefix = _config(node), ""
|
||||
path = str(fix.get("_path") or "")
|
||||
after = value if isinstance(value, str) and value != PROVIDER_CLEAR else None
|
||||
edits: list[tuple[str | None, str, Any, Any]] = [(fix["node_id"], prefix + path, _put_path(root, path, after), after)]
|
||||
if after is not None and after in (fix.get("_drop_model") or ()):
|
||||
if (model := _put_path(root, _model_path(path), None)) is not None:
|
||||
edits.append((fix["node_id"], prefix + _model_path(path), model, None))
|
||||
return edits
|
||||
|
||||
|
||||
#: 每类问题怎么改图。大多是同一种「按节点字段写值」,改全图默认、改成果字段的各走一格;留着这张表是为了
|
||||
#: 某类修复要特殊处理时只换它自己的那一格(测试里也借它模拟一条出错的修复规则)
|
||||
_EDITORS: dict[str, Callable[[dict[str, Any], dict[str, Any], Any], list[tuple[str | None, str, Any, Any]]]] = {
|
||||
@@ -666,6 +786,7 @@ _EDITORS: dict[str, Callable[[dict[str, Any], dict[str, Any], Any], list[tuple[s
|
||||
}
|
||||
_EDITORS["governed.exit_text_source"] = _edit_exit_fields
|
||||
_EDITORS["governed.judge_budget"] = _edit_judge_budget
|
||||
_EDITORS["provider.missing"] = _EDITORS["provider.disabled"] = _edit_provider
|
||||
|
||||
|
||||
def _handoff(fix: dict[str, Any], value: Any) -> bool:
|
||||
@@ -697,7 +818,9 @@ def _op(graph: dict[str, Any], node_id: str | None, edits: list[tuple[str | None
|
||||
顶层字段(整份新值);改全图默认的是 update_defaults,defaults 写改过的那几项。"""
|
||||
if node_id is None:
|
||||
defaults = graph.get("defaults") if isinstance(graph.get("defaults"), dict) else {}
|
||||
keys = dict.fromkeys(path.split(".", 1)[1] for _, path, _, _ in edits if path.startswith("defaults."))
|
||||
# 顶层那一项整份写:defaults.judge.provider 记 judge
|
||||
keys = dict.fromkeys(re.split(r"[.\[]", path.split(".", 1)[1], maxsplit=1)[0]
|
||||
for _, path, _, _ in edits if path.startswith("defaults."))
|
||||
return {"op": "update_defaults", "defaults": {k: copy.deepcopy(defaults.get(k)) for k in keys}}
|
||||
config = _config(_node(graph, node_id) or {})
|
||||
tops = dict.fromkeys(re.split(r"[.\[]", path, maxsplit=1)[0] for _, path, _, _ in edits)
|
||||
@@ -707,17 +830,18 @@ def _op(graph: dict[str, Any], node_id: str | None, edits: list[tuple[str | None
|
||||
def apply_fixes(graph: dict[str, Any] | GraphSpec, fix_ids: list[str], choices: dict[str, Any] | None = None, *,
|
||||
level: str, versions: dict[str, Any] | None = None,
|
||||
cards: dict[str, list[dict[str, Any]]] | None = None,
|
||||
checkers: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
checkers: dict[str, Any] | None = None, providers: list[Any] | None = None) -> dict[str, Any]:
|
||||
"""在副本上逐个应用修复,每一个都复核:降低要求、错误没变少或者冒出新错误的,丢弃并写明原因。
|
||||
|
||||
返回 {graph, changes, ops, applied, rejected, handoff, remaining, fixes, ok}。graph 只是预览,不落库。
|
||||
handoff 是人在 choice 里选了「交给 Copilot」的修复 id:图不动,调用方据此把剩下的 error 交给 Copilot。
|
||||
checkers、providers 同 check()。
|
||||
"""
|
||||
raw = graph.model_dump(mode="json") if isinstance(graph, GraphSpec) else graph
|
||||
current = copy.deepcopy(raw)
|
||||
spec = GraphSpec.model_validate(current)
|
||||
issues = publish_issues(spec, level=level, checkers=checkers)
|
||||
plan = plan_fixes(spec, issues, level=level, versions=versions, cards=cards)
|
||||
issues = publish_issues(spec, level=level, checkers=checkers, providers=providers)
|
||||
plan = plan_fixes(spec, issues, level=level, versions=versions, cards=cards, providers=providers)
|
||||
known = {f["id"] for f in plan}
|
||||
rank = {code: i for i, code in enumerate(_ORDER)}
|
||||
wanted = sorted(dict.fromkeys(fix_ids), key=lambda fid: rank.get(fid.split(":")[0], len(rank)))
|
||||
@@ -748,11 +872,12 @@ def apply_fixes(graph: dict[str, Any] | GraphSpec, fix_ids: list[str], choices:
|
||||
except Exception: # noqa: BLE001 - 修复规则把图改坏了:丢弃,不交给人一张存不回去的图
|
||||
rejected.append({"fix_id": fid, "reason": "修复后的工作流结构无法解析,已放弃这项修复"})
|
||||
continue
|
||||
trial_issues = publish_issues(trial_spec, level=level, checkers=checkers)
|
||||
trial_issues = publish_issues(trial_spec, level=level, checkers=checkers, providers=providers)
|
||||
# choice 里人亲手选的值不算「替人决定」(比如把沙箱代码标成取数);别的降低要求照样拦
|
||||
chosen = {(str(nid), path) for nid, path, _, _ in edits} if fix["kind"] == "choice" else set()
|
||||
why = ";".join(forbidden_changes(current, trial, chosen=chosen)) or judge(
|
||||
issues, trial_issues, target=(fix["code"], fix["node_id"]))
|
||||
target = (fix["code"], fix["node_id"], fix.get("field")) if fix["code"] in _PER_FIELD \
|
||||
else (fix["code"], fix["node_id"])
|
||||
why = ";".join(forbidden_changes(current, trial, chosen=chosen)) or judge(issues, trial_issues, target=target)
|
||||
if why:
|
||||
rejected.append({"fix_id": fid, "reason": why})
|
||||
continue
|
||||
@@ -763,7 +888,7 @@ def apply_fixes(graph: dict[str, Any] | GraphSpec, fix_ids: list[str], choices:
|
||||
"before": before, "after": after, "label": fix["label"]} for nid, path, before, after in edits]
|
||||
if edits:
|
||||
ops.append(_op(current, fix["node_id"], edits))
|
||||
plan = plan_fixes(spec, issues, level=level, versions=versions, cards=cards)
|
||||
plan = plan_fixes(spec, issues, level=level, versions=versions, cards=cards, providers=providers)
|
||||
return {"graph": current, "changes": changes, "ops": ops, "applied": applied, "rejected": rejected,
|
||||
"handoff": handoff, "remaining": annotate(issues, plan), "fixes": [public(f) for f in plan],
|
||||
"ok": not any(i.level == "error" for i in issues)}
|
||||
@@ -812,11 +937,12 @@ def worse(before: list[Any], after: list[Any]) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def judge(before: list[Any], after: list[Any], *, target: tuple[str, str | None] | None = None) -> str | None:
|
||||
def judge(before: list[Any], after: list[Any], *, target: tuple[Any, ...] | None = None) -> str | None:
|
||||
"""改完是不是更好了:不能冒出新的 error,error 不能变多;要修的那一处必须没了。
|
||||
|
||||
target 是 (问题编号, 节点 id):修的是一条警告(strict)时,error 不增加、那条警告消失就算更好;
|
||||
修的是 error,或者不指定 target(Copilot 兜底),error 必须变少。返回 None 表示更好,否则是原因。
|
||||
一处一条的修复(点名的模型接入)再带上字段 (问题编号, 节点 id, 字段):同一节点上别的字段还有同类问题不算没修好。
|
||||
"""
|
||||
if why := worse(before, after):
|
||||
return why
|
||||
@@ -824,11 +950,13 @@ def judge(before: list[Any], after: list[Any], *, target: tuple[str, str | None]
|
||||
a = [_as_dict(i) for i in after]
|
||||
b_err, a_err = _errors(before, after)
|
||||
if target is not None:
|
||||
code, node_id = target
|
||||
code, node_id, *at = target
|
||||
field = at[0] if at else None
|
||||
|
||||
def hit(i: dict[str, Any]) -> bool:
|
||||
# 图级修复(node_id 为 None,改全图默认)管的是这个编号下挂在各个节点上的每一条
|
||||
return i.get("code") == code and (node_id is None or i.get("node_id") in (node_id, None))
|
||||
return i.get("code") == code and (node_id is None or i.get("node_id") in (node_id, None)) \
|
||||
and (field is None or i.get("field") == field)
|
||||
|
||||
still = [i for i in a if hit(i)]
|
||||
if still:
|
||||
|
||||
@@ -30,7 +30,8 @@ UPGRADE_POLICIES = {"recompute", "dual", "incomparable"}
|
||||
UPGRADE_POLICY_LABELS = {k: option_label("upgrade_policy", k) for k in ("recompute", "dual", "incomparable")}
|
||||
|
||||
|
||||
def lint_for_publish(spec: GraphSpec, *, level: str, checkers: Mapping[str, Any] | None = None) -> ValidationResult:
|
||||
def lint_for_publish(spec: GraphSpec, *, level: str, checkers: Mapping[str, Any] | None = None,
|
||||
providers: list[Any] | None = None) -> ValidationResult:
|
||||
"""发布门禁。
|
||||
|
||||
published:基础可运行性之外只给警告。
|
||||
@@ -42,6 +43,10 @@ def lint_for_publish(spec: GraphSpec, *, level: str, checkers: Mapping[str, Any]
|
||||
|
||||
checkers 是各数据源基于数据目录的 SQL 检查器(data/sqlcheck.load_checkers_by_name),由调用方读好传进来:
|
||||
门禁本身不查库。给了的话,调用工具节点里写死的 SQL 对照目录查一遍(_lint_sql)。
|
||||
|
||||
providers 是模型接入表(provider_refs.load_providers),同样由调用方读好传进来。给了的话,节点点名的接入
|
||||
不存在或已停用的每一处一条 error(provider.missing / provider.disabled),两档都挡:正式运行到那里一定失败,
|
||||
或者少一块核查。不给(一键升级、助手自查这类只看图本身的地方)就不查。
|
||||
"""
|
||||
result = ValidationResult()
|
||||
strict = level == "governed"
|
||||
@@ -105,6 +110,11 @@ def lint_for_publish(spec: GraphSpec, *, level: str, checkers: Mapping[str, Any]
|
||||
|
||||
_lint_evidence(spec, flag, strict=strict)
|
||||
_lint_sql(spec, flag, checkers)
|
||||
if providers is not None:
|
||||
from app.engine.provider_refs import issues as provider_issues, provider_refs
|
||||
|
||||
for issue in provider_issues(provider_refs(spec), providers, level="error"):
|
||||
result.add(issue.message, level="error", node_id=issue.node_id, field=issue.field, code=issue.code)
|
||||
|
||||
if strict and not has_contract_output:
|
||||
result.add(
|
||||
@@ -114,13 +124,14 @@ def lint_for_publish(spec: GraphSpec, *, level: str, checkers: Mapping[str, Any]
|
||||
return result
|
||||
|
||||
|
||||
def publish_issues(spec: GraphSpec, *, level: str, checkers: Mapping[str, Any] | None = None) -> list[Any]:
|
||||
def publish_issues(spec: GraphSpec, *, level: str, checkers: Mapping[str, Any] | None = None,
|
||||
providers: list[Any] | None = None) -> list[Any]:
|
||||
"""发布时拦的全部问题:validate_graph 和 lint_for_publish 的并集,顺序也照发布接口。
|
||||
|
||||
发布、发布前检查、自动修复复核都走这一个口径——检查说能发的,真发布时不会被拦。checkers 见 lint_for_publish:
|
||||
这几处都要传同一份,否则检查和发布对 SQL 的判断不一致。
|
||||
发布、发布前检查、自动修复复核都走这一个口径——检查说能发的,真发布时不会被拦。checkers、providers 见
|
||||
lint_for_publish:这几处都要传同一份,否则检查和发布对 SQL、对模型接入的判断不一致。
|
||||
"""
|
||||
gate = lint_for_publish(spec, level=level, checkers=checkers).issues
|
||||
gate = lint_for_publish(spec, level=level, checkers=checkers, providers=providers).issues
|
||||
return [*_not_repeated(validate_graph(spec).issues, gate, spec), *gate]
|
||||
|
||||
|
||||
|
||||
@@ -5,13 +5,20 @@
|
||||
发布——正式运行到那里一定失败,或者少一块核查。
|
||||
|
||||
查的是执行时真会去取模型的那几处,取法和执行器一致:
|
||||
- 模型调用、Agent、报告撰写、多 Agent 协作的接入;条件分支只在「让模型分类」时、结构校验只在「让模型修复」时、
|
||||
知识检索只在「让模型重排」时才取模型。这几类节点没写自己的接入时取全图默认(NodeContext.cfg);
|
||||
- 多 Agent 协作各成员的接入;报告撰写交给模型裁判结论句时,「结论句裁判」里的接入;
|
||||
- 双盲复算、反方审稿的接入(核查模型):只读节点自己的配置,不取全图默认;
|
||||
- 模型调用、Agent、报告撰写、多 Agent 协作的接入;条件分支只在「让模型分类」时、结构校验只在「让模型修复」且允许
|
||||
重试时(max_retries 为 0 一次都不修)、知识检索只在「让模型重排」时才取模型。这几类节点没写自己的接入时取全图
|
||||
默认(NodeContext.cfg);
|
||||
- 多 Agent 协作各成员的接入;报告撰写交给模型裁判结论句时,「结论句裁判」里的接入(节点没写裁判配置时取全图默认
|
||||
里的那一份,同 nodes/report.report_judge);
|
||||
- 双盲复算、反方审稿的接入(核查模型):只读节点自己的配置,不取全图默认。节点自己的开关关着时执行器直接跳过、
|
||||
不取模型,不查;设置里的总开关不看——它随时会被打开,打开时这一处就会失败;
|
||||
- 全图默认的接入:有节点会取到它时才查。
|
||||
|
||||
接入表要读库,门禁本身不查库(governance.lint_for_publish),所以由接口这一层读出接入表交给 issues(),同数据目录
|
||||
说法分两种:检查器里有接入下拉框的几处(双盲复算、反方审稿、节点自己的结论句裁判)说「在节点中换一个接入」;
|
||||
别处(节点自己的接入、协作成员、全图默认)界面上没有下拉框,说「在发布前检查中改用其他接入」——发布前检查给这类
|
||||
问题配了修复(engine/autofix.py 的 provider.*:改用一个启用的接入,或不再指定)。
|
||||
|
||||
接入表要读库,门禁本身不查库(governance.lint_for_publish),所以由接口这一层读出接入表交进来,同数据目录
|
||||
SQL 检查器的做法。设置里点名接入的几项由 api/settings.py 在保存时查。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
@@ -23,7 +30,12 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.engine.labels import field_label, type_label
|
||||
from app.engine.schema import CLAIMS_JUDGE, GraphNode, GraphSpec, NodeType, ValidationIssue
|
||||
from app.providers.factory import ProviderNotConfigured, list_providers, named_provider
|
||||
from app.providers.factory import NAMED_FIX, ProviderNotConfigured, list_providers, named_provider
|
||||
|
||||
#: 这类问题的编号:provider.missing(点名的接入不存在)、provider.disabled(已停用)
|
||||
CODES = frozenset({"provider.missing", "provider.disabled"})
|
||||
#: 提示的后半句:这一处在检查器里有没有接入下拉框
|
||||
_WHERE = {True: "或在节点中换一个接入", False: "或在「发布前检查」中改用其他接入"}
|
||||
|
||||
|
||||
def _cfg(spec: GraphSpec, node: GraphNode, key: str, default: Any = None) -> Any:
|
||||
@@ -32,6 +44,17 @@ def _cfg(spec: GraphSpec, node: GraphNode, key: str, default: Any = None) -> Any
|
||||
return spec.defaults.get(key, default) if value in (None, "") else value
|
||||
|
||||
|
||||
def _repairs(spec: GraphSpec, node: GraphNode) -> bool:
|
||||
"""结构校验会不会让模型修复:允许修复,而且至少重试一次(nodes/human.run_validate 同一个取值)。认不出的重试次数
|
||||
按会修复算:执行时它照样报错,这里宁可多提一句。"""
|
||||
if not _cfg(spec, node, "repair_with_llm", True):
|
||||
return False
|
||||
try:
|
||||
return int(_cfg(spec, node, "max_retries", 2) or 0) > 0
|
||||
except (TypeError, ValueError):
|
||||
return True
|
||||
|
||||
|
||||
#: 按 NodeContext.cfg 取接入的节点 → 执行时真会去取模型的条件(None 是总会取)
|
||||
_TAKES_MODEL: dict[NodeType, Callable[[GraphSpec, GraphNode], bool] | None] = {
|
||||
NodeType.LLM: None,
|
||||
@@ -39,7 +62,7 @@ _TAKES_MODEL: dict[NodeType, Callable[[GraphSpec, GraphNode], bool] | None] = {
|
||||
NodeType.REPORT: None,
|
||||
NodeType.SUPERVISOR: None,
|
||||
NodeType.BRANCH: lambda spec, node: _cfg(spec, node, "mode", "expression") == "llm",
|
||||
NodeType.VALIDATE: lambda spec, node: bool(_cfg(spec, node, "repair_with_llm", True)),
|
||||
NodeType.VALIDATE: _repairs,
|
||||
NodeType.RETRIEVE: lambda spec, node: _cfg(spec, node, "rerank", "off") == "model",
|
||||
}
|
||||
#: 核查节点:接入只读节点自己的配置(verify_model_spec),图级默认的接入是写作模型的
|
||||
@@ -52,24 +75,27 @@ def _name(value: Any) -> str | None:
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProviderRef:
|
||||
"""图里点名接入的一处:落在哪个节点(全图默认为 None)、哪个字段,点的名字,句子里怎么称呼这一处。"""
|
||||
"""图里点名接入的一处:落在哪个节点(全图默认为 None)、哪个字段,点的名字,句子里怎么称呼这一处,
|
||||
检查器里有没有这一处的接入下拉框。"""
|
||||
|
||||
node_id: str | None
|
||||
field: str
|
||||
name: str
|
||||
who: str
|
||||
editable: bool = False
|
||||
|
||||
|
||||
def provider_refs(spec: GraphSpec) -> list[ProviderRef]:
|
||||
"""图里执行时会用到的接入点名,按画布上节点的顺序,全图默认排最后。"""
|
||||
out: list[ProviderRef] = []
|
||||
falls_back = False
|
||||
default_judge = False
|
||||
for node in spec.nodes:
|
||||
cfg = node.config
|
||||
who = f"「{node.title}」({type_label(node.type)})"
|
||||
if node.type in _VERIFY:
|
||||
if name := _name(cfg.get("provider")):
|
||||
out.append(ProviderRef(node.id, "provider", name, who + _VERIFY[node.type]))
|
||||
if cfg.get("enabled") is not False and (name := _name(cfg.get("provider"))):
|
||||
out.append(ProviderRef(node.id, "provider", name, who + _VERIFY[node.type], editable=True))
|
||||
continue
|
||||
if node.type not in _TAKES_MODEL:
|
||||
continue
|
||||
@@ -84,25 +110,41 @@ def provider_refs(spec: GraphSpec) -> list[ProviderRef]:
|
||||
if isinstance(member, dict) and (name := _name(member.get("provider"))):
|
||||
label = _name(member.get("name")) or f"第 {i + 1} 位"
|
||||
out.append(ProviderRef(node.id, f"agents[{i}].provider", name, f"{who}的成员「{label}」"))
|
||||
if node.type == NodeType.REPORT and _cfg(spec, node, "claims", "off") == CLAIMS_JUDGE \
|
||||
and isinstance(cfg.get("judge"), dict) and (name := _name(cfg["judge"].get("provider"))):
|
||||
out.append(ProviderRef(node.id, "judge.provider", name, f"{who}的{field_label('judge')}"))
|
||||
if node.type == NodeType.REPORT and _cfg(spec, node, "claims", "off") == CLAIMS_JUDGE:
|
||||
judge = _cfg(spec, node, "judge")
|
||||
if judge is not cfg.get("judge"):
|
||||
# 节点没写裁判配置,执行时用全图默认里的那一份:问题记在全图默认上,几个报告共用时只记一条
|
||||
default_judge = True
|
||||
elif isinstance(judge, dict) and (name := _name(judge.get("provider"))):
|
||||
out.append(ProviderRef(node.id, "judge.provider", name, f"{who}的{field_label('judge')}",
|
||||
editable=True))
|
||||
if falls_back and (name := _name(spec.defaults.get("provider"))):
|
||||
out.append(ProviderRef(None, "defaults.provider", name, field_label("defaults")))
|
||||
judge = spec.defaults.get("judge")
|
||||
if default_judge and isinstance(judge, dict) and (name := _name(judge.get("provider"))):
|
||||
out.append(ProviderRef(None, "defaults.judge.provider", name,
|
||||
f"{field_label('defaults')}中的{field_label('judge')}"))
|
||||
return out
|
||||
|
||||
|
||||
def broken(refs: list[ProviderRef], providers: list[Any]) -> list[tuple[ProviderRef, ProviderNotConfigured]]:
|
||||
"""点名的接入不存在或已停用的那几处,带着 named_provider 报的错(code 是 missing / disabled)。"""
|
||||
out: list[tuple[ProviderRef, ProviderNotConfigured]] = []
|
||||
for ref in refs:
|
||||
try:
|
||||
named_provider(providers, ref.name)
|
||||
except ProviderNotConfigured as e:
|
||||
out.append((ref, e))
|
||||
return out
|
||||
|
||||
|
||||
def issues(refs: list[ProviderRef], providers: list[Any], *, level: str) -> list[ValidationIssue]:
|
||||
"""点名的接入不存在或已停用的每一处一条问题。providers 是接入表(factory.list_providers),level 由调用方定:
|
||||
画布校验 warning,发布一类 error。说法取自 named_provider,和运行时报的原因、提示同一句。"""
|
||||
out: list[ValidationIssue] = []
|
||||
for ref in refs:
|
||||
try:
|
||||
named_provider(providers, ref.name)
|
||||
except ProviderNotConfigured as e:
|
||||
out.append(ValidationIssue(level=level, node_id=ref.node_id, field=ref.field, # type: ignore[arg-type]
|
||||
code=f"provider.{e.code}", message=f"{ref.who}指定的{e.reason}。{e.hint}"))
|
||||
return out
|
||||
画布校验 warning,发布一类 error。原因和运行时报的同一句(named_provider),提示按这一处在界面上能不能改。"""
|
||||
return [ValidationIssue(level=level, node_id=ref.node_id, field=ref.field, # type: ignore[arg-type]
|
||||
code=f"provider.{e.code}",
|
||||
message=f"{ref.who}指定的{e.reason}。{NAMED_FIX[e.code]},{_WHERE[ref.editable]}")
|
||||
for ref, e in broken(refs, providers)]
|
||||
|
||||
|
||||
async def load_issues(session: AsyncSession, spec: GraphSpec, *, level: str) -> list[ValidationIssue]:
|
||||
@@ -111,3 +153,9 @@ async def load_issues(session: AsyncSession, spec: GraphSpec, *, level: str) ->
|
||||
if not refs:
|
||||
return []
|
||||
return issues(refs, await list_providers(session), level=level)
|
||||
|
||||
|
||||
async def load_providers(session: AsyncSession, spec: GraphSpec) -> list[Any] | None:
|
||||
"""发布、发布前检查、自动修复交给门禁的接入表(governance.lint_for_publish 的 providers)。图里没点名任何接入时
|
||||
不读库,给空表:没有要查的,修复也用不上候选。"""
|
||||
return await list_providers(session) if provider_refs(spec) else []
|
||||
|
||||
@@ -48,22 +48,39 @@ async def list_providers(session: AsyncSession) -> list[Provider]:
|
||||
return list(rows.scalars())
|
||||
|
||||
|
||||
def named_provider(providers: list[Provider], ref: str) -> Provider:
|
||||
#: 点名的接入不存在、已停用时,提示的前半句:在设置里能做什么。后半句(去哪里换一个接入)看场合:运行时报错、
|
||||
#: 设置页写「或换一个接入」;画布校验、发布前检查按这一处在界面上能不能改另写(engine/provider_refs.py)
|
||||
NAMED_FIX = {"missing": "请在「设置 → 模型接入」中核对名称", "disabled": "请在「设置 → 模型接入」中启用它"}
|
||||
|
||||
|
||||
def named_provider(providers: list[Provider], ref: Any) -> Provider:
|
||||
"""按名称或 id 找显式点名的接入(providers 是 list_providers 的结果)。找不到、找到的已停用都抛
|
||||
ProviderNotConfigured:点名了就只用它,不换别家。运行时取模型、设置和画布校验都按这一处认,说法一致。
|
||||
名称两边的空白不算。"""
|
||||
key = ref.strip()
|
||||
名称两边的空白不算;不是字符串的(接口写进来的数字一类)当作点了一个不存在的名字。"""
|
||||
key = ref.strip() if isinstance(ref, str) else str(ref)
|
||||
found = next((p for p in providers if key in (p.id, p.name)), None) \
|
||||
or next((p for p in providers if (p.name or "").strip() == key), None)
|
||||
if found is None:
|
||||
raise ProviderNotConfigured(f"模型接入「{key}」不存在",
|
||||
"请在「设置 → 模型接入」中核对名称,或换一个接入", code="missing")
|
||||
raise ProviderNotConfigured(f"模型接入「{key}」不存在", f"{NAMED_FIX['missing']},或换一个接入", code="missing")
|
||||
if not found.enabled:
|
||||
raise ProviderNotConfigured(f"模型接入「{found.name}」已停用",
|
||||
"请在「设置 → 模型接入」中启用它,或换一个接入", code="disabled")
|
||||
raise ProviderNotConfigured(f"模型接入「{found.name}」已停用", f"{NAMED_FIX['disabled']},或换一个接入",
|
||||
code="disabled")
|
||||
return found
|
||||
|
||||
|
||||
def assistant_unavailable(e: BaseException) -> str:
|
||||
"""助手(画布上改图、表格导入与数据目录的 AI 起草)用的模型取不到时给人看的那句话。
|
||||
|
||||
点名的接入不存在、已停用(按模型名找到的接入已停用也算)照实说是哪个接入、在设置里能做什么,或者给助手换一个
|
||||
模型;以前这几种要么悄悄换成演示模型,要么各处自己判停用。其余(缺 key、没有模型……)是接入本身没配完。"""
|
||||
from app.core.errors import not_configured
|
||||
|
||||
code = getattr(e, "code", "")
|
||||
if code in NAMED_FIX:
|
||||
return f"助手使用的{getattr(e, 'reason', '') or e}。{NAMED_FIX[code]},或为助手换一个模型"
|
||||
return f"助手使用的模型尚未配置完成:{not_configured(e, with_hint=False)}。请到「设置 → 模型接入」检查"
|
||||
|
||||
|
||||
async def resolve_provider(
|
||||
session: AsyncSession, ref: str | None, model: str | None = None
|
||||
) -> Provider | None:
|
||||
@@ -77,12 +94,13 @@ async def resolve_provider(
|
||||
get_chat_model 把它当成「一个接入都没配」,悄悄换成演示模型:名字写错一个字、接入改了名或删了,
|
||||
报告就由演示模型写成假文本,核查也由它来「核」,运行照样成功。停用的照常返回、照常调用,
|
||||
又和下面「按模型名找到的接入已停用就报错」对不上。
|
||||
返回 None 只剩一种情况:没给 ref(只有空白的也算没给),而一个启用的接入都没有。
|
||||
返回 None 只剩一种情况:没给 ref(只有空白的也算没给),而一个启用的接入都没有。不是字符串的 ref(接口写进
|
||||
设置里的数字一类)按点名了一个不存在的接入报,不当成没给。
|
||||
"""
|
||||
providers = await list_providers(session)
|
||||
enabled = [p for p in providers if p.enabled]
|
||||
|
||||
if ref and ref.strip():
|
||||
if ref is not None and (not isinstance(ref, str) or ref.strip()):
|
||||
return named_provider(providers, ref)
|
||||
|
||||
if model:
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
code 和设置页上的叫法;一组出错整次不落库。留空照旧表示沿用上一级;存着的值没改(写好之后接入才被删、停用)不挡
|
||||
这次保存别的项——设置页标着「不存在」,运行时照实报错;
|
||||
- 画布校验:节点点名的接入不存在或已停用给警告,说清哪个节点、哪个接入;发布前检查、发布、自动修复的复核给错误,
|
||||
挡住发布。只查执行时真会去取模型的那几处(分支「让模型分类」、校验「让模型修复」、检索「让模型重排」时才算)。
|
||||
挡住发布。只查执行时真会去取模型的那几处(分支「让模型分类」、校验「让模型修复」且允许重试、检索「让模型重排」
|
||||
时才算;核查节点自己的开关关着不算);报告没写裁判配置时查全图默认里的那一份;
|
||||
- 提示按这一处在界面上能不能改:检查器里有接入下拉框的说「在节点中换一个接入」,别处说「在发布前检查中改用」;
|
||||
- 发布前检查给每一处配一条要人选的修复:改用一个启用的接入,或不指定接入。
|
||||
|
||||
接入名、节点名都是虚构的。
|
||||
"""
|
||||
@@ -250,7 +253,8 @@ async def test_publish_check_and_publish_stop_at_a_broken_provider(client, level
|
||||
assert r.status_code == 200, r.text
|
||||
check = r.json()
|
||||
[issue] = provider_issues(check)
|
||||
assert issue["level"] == "error" and issue["node_id"] == "write" and issue["fix"] is None
|
||||
assert issue["level"] == "error" and issue["node_id"] == "write"
|
||||
assert issue["fix"] == "provider.missing:write:provider"
|
||||
assert check["ok"] is False
|
||||
r = await client.post(f"/api/workflows/{wf}/publish", json={"level": level})
|
||||
body = r.json()
|
||||
@@ -295,3 +299,189 @@ async def test_publishing_a_clean_graph_is_unaffected(client):
|
||||
wf = await _workflow(writer(LIVE))
|
||||
r = await client.post(f"/api/workflows/{wf}/publish", json={"level": "published"})
|
||||
assert r.json()["ok"] is True, r.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize("group,key,label", [("run", "tool_gate_provider", "门控模型 · 接入"),
|
||||
("copilot", "provider", "助手模型 · 接入")])
|
||||
async def test_a_provider_name_that_is_not_text_is_refused_with_the_field(client, group, key, label):
|
||||
"""门控、助手两组没有自己的格式检查:名字写成数字一类以前照样存下,查助手当前用哪个模型时就出错。"""
|
||||
r = await client.put("/api/settings", json={"values": {group: {key: 123}}})
|
||||
assert r.status_code == 422, r.text
|
||||
body = r.json()
|
||||
assert body["code"] == "settings_provider_invalid" and body["field"] == f"{group}.{key}"
|
||||
assert body["detail"].startswith(f"「{label}」需要填写模型接入的名称,留空表示"), body["detail"]
|
||||
assert body["detail"].endswith("当前为「123」")
|
||||
assert await stored(group) is None
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 只查真会取模型的地方(续)、报告的裁判取自全图默认、提示指到能改的地方
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_a_switched_off_verify_node_or_a_validate_without_retries_is_not_checked(client):
|
||||
"""核查节点自己的开关关着时执行器直接跳过、不取模型;结构校验不许重试(max_retries 为 0)时一次都不修。它们点名的
|
||||
接入写错了不提、不挡发布。开关打开、允许重试就照查。"""
|
||||
graph = chain(
|
||||
node("start", "input"),
|
||||
node("write", "report", "写周报", instructions="写周报", provider=LIVE),
|
||||
node("dual", "dual_check", "复核销售额", enabled=False, provider=GONE),
|
||||
node("chal", "challenge", "审稿", report="write", enabled=False, provider=OFF),
|
||||
node("check", "validate", "校验", max_retries=0, provider=GONE, schema={"type": "object"}),
|
||||
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}]),
|
||||
)
|
||||
wf = await _workflow(graph)
|
||||
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": graph})
|
||||
assert provider_issues(r.json()) == []
|
||||
r = await client.post("/api/workflows/validate", json={"graph": graph})
|
||||
assert provider_issues(r.json()) == []
|
||||
graph["nodes"][2]["data"]["config"]["enabled"] = True
|
||||
del graph["nodes"][3]["data"]["config"]["enabled"] # 没写开关:跟随设置,照查
|
||||
graph["nodes"][4]["data"]["config"]["max_retries"] = 1
|
||||
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": graph})
|
||||
assert {i["node_id"] for i in provider_issues(r.json())} == {"dual", "chal", "check"}
|
||||
|
||||
|
||||
async def test_a_report_judged_with_the_graph_default_judge_checks_that_provider_once(client):
|
||||
"""报告没写自己的裁判配置时执行用全图默认里的那一份(report_judge):它点名的接入记在全图默认上,几个报告共用
|
||||
时只记一条;写了自己的裁判配置的报告不受它影响;不交给模型裁判的报告用不上它。"""
|
||||
graph = chain(
|
||||
node("start", "input"),
|
||||
node("a", "report", "写周报", instructions="写周报", provider=LIVE), # claims 取全图默认的 judge
|
||||
node("b", "report", "写月报", instructions="写月报", provider=LIVE),
|
||||
node("c", "report", "写年报", instructions="写年报", provider=LIVE, judge={"provider": LIVE}),
|
||||
node("d", "report", "写日报", instructions="写日报", provider=LIVE, claims="off"),
|
||||
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.a.text }}"}]),
|
||||
defaults={"claims": "judge", "judge": {"provider": GONE}},
|
||||
)
|
||||
r = await client.post("/api/workflows/validate", json={"graph": graph})
|
||||
[issue] = provider_issues(r.json())
|
||||
assert (issue["node_id"], issue["field"], issue["code"]) == (None, "defaults.judge.provider", "provider.missing")
|
||||
assert issue["message"].startswith(f"工作流默认设置中的结论句裁判指定的模型接入「{GONE}」不存在。"), issue["message"]
|
||||
wf = await _workflow(graph)
|
||||
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": graph})
|
||||
check = r.json()
|
||||
assert check["ok"] is False
|
||||
assert [i["fix"] for i in provider_issues(check)] == ["provider.missing:graph:defaults.judge.provider"]
|
||||
|
||||
|
||||
async def test_the_hint_points_to_where_the_provider_can_actually_be_changed(client):
|
||||
"""检查器里有接入下拉框的(双盲复算、反方审稿、报告自己的结论句裁判)说「在节点中换一个接入」;节点自己的接入、
|
||||
协作成员、全图默认在界面上没有下拉框,说「在发布前检查中改用」——那里有这一处的修复。"""
|
||||
graph = chain(
|
||||
node("start", "input"),
|
||||
node("ask", "llm", "提问", provider=GONE, prompt="总结"),
|
||||
node("write", "report", "写周报", instructions="写周报", provider=LIVE, claims="judge", judge={"provider": OFF}),
|
||||
node("dual", "dual_check", "复核销售额", provider=GONE),
|
||||
node("team", "supervisor", "协作", provider=LIVE, agents=[{"name": "分析师", "provider": OFF, "system": "分析"}]),
|
||||
node("out", "output", fields=[{"name": "周报", "value": "{{ nodes.write.text }}"}]),
|
||||
)
|
||||
r = await client.post("/api/workflows/validate", json={"graph": graph})
|
||||
got = {(i["node_id"], i["field"]): i["message"] for i in provider_issues(r.json())}
|
||||
in_node, in_check = "或在节点中换一个接入", "或在「发布前检查」中改用其他接入"
|
||||
assert got[("ask", "provider")].endswith(f"。请在「设置 → 模型接入」中核对名称,{in_check}"), got
|
||||
assert got[("dual", "provider")].endswith(f"。请在「设置 → 模型接入」中核对名称,{in_node}"), got
|
||||
assert got[("write", "judge.provider")].endswith(f"。请在「设置 → 模型接入」中启用它,{in_node}"), got
|
||||
assert got[("team", "agents[0].provider")].endswith(f"。请在「设置 → 模型接入」中启用它,{in_check}"), got
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 发布前检查的修复:改用一个启用的接入,或不指定接入
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def _fix(client, wf: str, graph: dict, fix_id: str, value) -> dict:
|
||||
r = await client.post(f"/api/workflows/{wf}/autofix", json={"level": "published", "graph": graph,
|
||||
"apply": [fix_id], "choices": {fix_id: value}})
|
||||
assert r.status_code == 200, r.text
|
||||
return r.json()
|
||||
|
||||
|
||||
def config_of(graph: dict, node_id: str) -> dict:
|
||||
return next(n for n in graph["nodes"] if n["id"] == node_id)["data"]["config"]
|
||||
|
||||
|
||||
async def test_each_broken_provider_gets_a_choice_to_switch_or_stop_naming_one(client):
|
||||
"""多数节点的检查器里没有接入下拉框:发布前检查里的这条修复是界面上唯一能改它的地方。候选只列启用的接入,不替人选;
|
||||
选的接入不提供这一处写着的模型时,模型一并去掉(拿 A 家的模型名去调 B 家必然失败),候选的说明里写清。"""
|
||||
graph = writer(GONE, model="核对旧模型")
|
||||
wf = await _workflow(graph)
|
||||
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": graph})
|
||||
check = r.json()
|
||||
[issue] = provider_issues(check)
|
||||
[fix] = [f for f in check["fixes"] if f["id"] == issue["fix"]]
|
||||
assert (fix["kind"], fix["node_id"], fix["field"]) == ("choice", "write", "provider")
|
||||
assert fix["label"] == "选择「写周报」(模型调用)改用的模型接入"
|
||||
assert "default" not in fix and not any(k.startswith("_") for k in fix)
|
||||
values = [o["value"] for o in fix["options"]]
|
||||
assert values[0] == "" and fix["options"][0]["label"] == "不指定接入"
|
||||
assert LIVE in values and OFF not in values and GONE not in values
|
||||
live = next(o for o in fix["options"] if o["value"] == LIVE)
|
||||
assert "不提供模型「核对旧模型」,将改用它的默认模型「核对模型」" in live["hint"] and "演示用" in live["hint"]
|
||||
|
||||
out = await _fix(client, wf, graph, fix["id"], LIVE)
|
||||
assert out["applied"] == [fix["id"]] and out["rejected"] == [] and out["ok"] is True, out["rejected"]
|
||||
assert config_of(out["graph"], "write")["provider"] == LIVE and "model" not in config_of(out["graph"], "write")
|
||||
assert [(c["node_id"], c["field"], c["before"], c["after"]) for c in out["changes"]] == [
|
||||
("write", "provider", GONE, LIVE), ("write", "model", "核对旧模型", None)]
|
||||
assert provider_issues({"issues": out["remaining"]}) == []
|
||||
|
||||
out = await _fix(client, wf, graph, fix["id"], "")
|
||||
assert out["applied"] == [fix["id"]] and out["ok"] is True, out["rejected"]
|
||||
config = config_of(out["graph"], "write")
|
||||
assert "provider" not in config and config["model"] == "核对旧模型"
|
||||
assert [(c["field"], c["before"], c["after"]) for c in out["changes"]] == [("provider", GONE, None)]
|
||||
|
||||
|
||||
async def test_a_team_node_and_its_member_are_fixed_one_place_at_a_time(client):
|
||||
"""同一个协作节点上自己的接入和成员的接入各有一条修复:只修成员那一处时,节点自己那一处还在不算没修好。"""
|
||||
graph = chain(
|
||||
node("start", "input"),
|
||||
node("team", "supervisor", "协作", provider=GONE, goal="分析本周销售",
|
||||
agents=[{"name": "分析师", "provider": GONE, "system": "分析"}]),
|
||||
node("out", "output", fields=[{"name": "结论", "value": "{{ last_message }}"}]),
|
||||
)
|
||||
wf = await _workflow(graph)
|
||||
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": graph})
|
||||
issues = provider_issues(r.json())
|
||||
member = "provider.missing:team:agents[0].provider"
|
||||
assert [(i["field"], i["fix"]) for i in issues] == [("provider", "provider.missing:team:provider"),
|
||||
("agents[0].provider", member)]
|
||||
assert {f["id"] for f in r.json()["fixes"]} >= {"provider.missing:team:provider", member}
|
||||
out = await _fix(client, wf, graph, member, LIVE)
|
||||
assert out["applied"] == [member], out["rejected"]
|
||||
config = config_of(out["graph"], "team")
|
||||
assert config["agents"][0]["provider"] == LIVE and config["provider"] == GONE
|
||||
assert [i["field"] for i in provider_issues({"issues": out["remaining"]})] == ["provider"]
|
||||
assert out["ok"] is False
|
||||
|
||||
|
||||
async def test_the_graph_default_provider_has_its_own_fix(client):
|
||||
graph = chain(node("start", "input"), node("ask", "llm", "提问", prompt="总结"),
|
||||
node("out", "output", fields=[{"name": "结论", "value": "{{ last_message }}"}]),
|
||||
defaults={"provider": OFF, "model": "核对停用模型"})
|
||||
wf = await _workflow(graph)
|
||||
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": graph})
|
||||
[issue] = provider_issues(r.json())
|
||||
assert (issue["node_id"], issue["code"], issue["fix"]) == (
|
||||
None, "provider.disabled", "provider.disabled:graph:defaults.provider")
|
||||
out = await _fix(client, wf, graph, issue["fix"], LIVE)
|
||||
assert out["applied"] == [issue["fix"]] and out["ok"] is True, out["rejected"]
|
||||
assert out["graph"]["defaults"] == {"provider": LIVE}
|
||||
assert [(c["node_id"], c["field"], c["before"], c["after"]) for c in out["changes"]] == [
|
||||
(None, "defaults.provider", OFF, LIVE), (None, "defaults.model", "核对停用模型", None)]
|
||||
|
||||
|
||||
async def test_stop_naming_a_provider_is_refused_when_the_graph_default_is_broken_too(client):
|
||||
"""节点不再指定接入就改取全图默认:全图默认的接入也坏了,修完冒出新问题,这一项不采用并说清原因。"""
|
||||
graph = chain(node("start", "input"), node("ask", "llm", "提问", provider=GONE, prompt="总结"),
|
||||
node("out", "output", fields=[{"name": "结论", "value": "{{ last_message }}"}]),
|
||||
defaults={"provider": OFF})
|
||||
wf = await _workflow(graph)
|
||||
r = await client.post(f"/api/workflows/{wf}/publish-check", json={"level": "published", "graph": graph})
|
||||
[issue] = provider_issues(r.json()) # 唯一的节点写了自己的接入,全图默认的没人用,不提
|
||||
out = await _fix(client, wf, graph, issue["fix"], "")
|
||||
assert out["applied"] == []
|
||||
[(fid, reason)] = [(x["fix_id"], x["reason"]) for x in out["rejected"]]
|
||||
assert fid == issue["fix"] and reason.startswith("修改后出现了新的问题:工作流默认设置指定的模型接入"), reason
|
||||
assert config_of(out["graph"], "ask")["provider"] == GONE and out["changes"] == []
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
以前点名的接入不存在时 get_chat_model 悄悄换成演示模型(mock-fast):报告由它写成假文本,双盲复算、反方审稿、审批
|
||||
质询由它来「核查」,正式运行照样出具。现在(providers/factory.named_provider):
|
||||
|
||||
- 模型调用、Agent、报告撰写、多 Agent 协作、让模型分类的条件分支:节点失败,报错带原因和去哪里改,一次模型调用都不发;
|
||||
- 模型调用、Agent、报告撰写、多 Agent 协作、让模型分类的条件分支、让模型修复的结构校验:节点失败,报错带原因和去哪里改,
|
||||
一次模型调用都不发;让模型重排的知识检索:不重排,降级日志写明原因;
|
||||
- 双盲复算:该指标无法复算,原因「核查模型不可用(…)」,记缺口;反方审稿:这几句未能反查,原因同上;
|
||||
- 审批质询:回答 ok 为假,error 说明原因;结论句裁判:按「裁判模型不可用」照实记未裁判和缺口;
|
||||
- 审批质询:回答 ok 为假,error 说明原因;结论句裁判(运行中、按需):按「裁判模型不可用」照实记未裁判和缺口;
|
||||
- 助手:报错给用户,查当前用哪个模型的接口照常返回、写明原因;门控模型:转人工审批、写明原因;
|
||||
- 定时任务发起的正式运行:运行失败,通知照常推送。
|
||||
|
||||
@@ -171,6 +172,9 @@ NODES = {
|
||||
agents=[{"name": "分析师", "system": "你是销售分析师"}]),
|
||||
"branch": lambda ref: node("ask", "branch", "分流", mode="llm", provider=ref,
|
||||
cases=[{"key": "follow", "label": "需要跟进"}]),
|
||||
# 输入不是 JSON:第一次校验不通过,要让模型修复时才取模型
|
||||
"validate": lambda ref: node("ask", "validate", "校验", provider=ref, source="门店甲本周销售平稳",
|
||||
schema={"type": "object", "required": ["区域"]}),
|
||||
}
|
||||
|
||||
|
||||
@@ -193,6 +197,36 @@ async def test_a_node_naming_a_broken_provider_fails_and_says_why(monkeypatch, k
|
||||
no_mock(evs, run.output, run.error)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ref", [MISSING, DISABLED])
|
||||
async def test_a_retrieve_reranked_by_a_broken_provider_keeps_its_hits_and_says_why(monkeypatch, ref):
|
||||
"""让模型重排的知识检索:接入取不到就不重排、照初筛的顺序给命中,降级日志写明原因(不悄悄让演示模型去排)。"""
|
||||
from app.db.models import Document
|
||||
from app.memory import kb
|
||||
|
||||
called = script(monkeypatch)
|
||||
collection = f"k1_{uuid.uuid4().hex[:8]}"
|
||||
async with SessionLocal() as session:
|
||||
await kb.ingest_document(session, collection=collection, title="门店巡检规范",
|
||||
content="门店巡检每周一次,巡检结果当天上报区域经理。")
|
||||
try:
|
||||
spec = chain(node("start", "input"),
|
||||
node("look", "retrieve", "检索", query="门店巡检多久一次", collection=collection, rerank="model",
|
||||
provider=ref),
|
||||
node("out", "output", fields=[{"name": "结论", "value": "见检索结果"}]))
|
||||
run = await start(spec)
|
||||
assert run.status == "succeeded", run.error
|
||||
evs = await events(run.id)
|
||||
assert output_of(evs, "look")["count"] >= 1
|
||||
said = [d["message"] for d in logs(evs, "retrieve_degraded")]
|
||||
assert any(m.startswith("重排不可用:") and REASONS[ref] in m and HINTS[ref] in m for m in said), said
|
||||
assert called == [] and not [e for e in evs if e.type in ("llm.start", "llm.end")]
|
||||
no_mock(evs, run.output)
|
||||
finally:
|
||||
async with SessionLocal() as session:
|
||||
for doc in list((await session.execute(select(Document).where(Document.collection == collection))).scalars()):
|
||||
await kb.delete_document(session, doc.id)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 双盲复算:无法复算,记缺口
|
||||
# --------------------------------------------------------------------------
|
||||
@@ -281,11 +315,14 @@ def report_graph(source: str, *between) -> dict:
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("where", ["settings", "node"])
|
||||
@pytest.mark.parametrize("ref", [MISSING, DISABLED])
|
||||
async def test_challenge_with_a_broken_verify_provider_reviews_no_claim_and_says_why(monkeypatch, sales, ref):
|
||||
async def test_challenge_with_a_broken_verify_provider_reviews_no_claim_and_says_why(monkeypatch, sales, ref, where):
|
||||
called = script(monkeypatch, writer=TEXT)
|
||||
await put_setting("verify", {"provider": ref})
|
||||
run = await start(report_graph(sales, node("chal", "challenge", "审稿", report="write")), run_class="formal")
|
||||
if where == "settings":
|
||||
await put_setting("verify", {"provider": ref})
|
||||
chal = node("chal", "challenge", "审稿", report="write", **({"provider": ref} if where == "node" else {}))
|
||||
run = await start(report_graph(sales, chal), run_class="formal")
|
||||
assert run.status == "succeeded", run.error
|
||||
evs = await events(run.id)
|
||||
out = output_of(evs, "chal")
|
||||
@@ -343,7 +380,8 @@ def weekly(**report) -> dict:
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("where,ref", [("node", MISSING), ("settings", DISABLED)])
|
||||
@pytest.mark.parametrize("where", ["node", "settings"])
|
||||
@pytest.mark.parametrize("ref", [MISSING, DISABLED])
|
||||
async def test_a_broken_judge_provider_leaves_the_claims_unjudged_and_says_why(monkeypatch, where, ref):
|
||||
called = script(monkeypatch, writer=JUDGED)
|
||||
if where == "settings":
|
||||
@@ -364,25 +402,78 @@ async def test_a_broken_judge_provider_leaves_the_claims_unjudged_and_says_why(m
|
||||
no_mock(evs, run.output, doc)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ref", [MISSING, DISABLED])
|
||||
async def test_judging_on_demand_with_a_broken_judge_provider_says_why(monkeypatch, client, ref):
|
||||
"""探索运行里按需裁判几句(POST /evidence/judge):设置里的裁判接入取不到,照「裁判模型不可用」说清,句子仍是未裁判,
|
||||
不调用模型、不记判定。"""
|
||||
called = script(monkeypatch, writer=JUDGED)
|
||||
run = await start(weekly(claims="require_citation"))
|
||||
assert run.status == "succeeded", run.error
|
||||
evs = await events(run.id)
|
||||
[checked] = [e for e in evs if e.type == "report.checked"]
|
||||
doc = artifact_store.load(checked.data["doc_artifact"])
|
||||
units = [u["id"] for _, u in iter_units(doc)]
|
||||
await put_setting("judge", {"provider": ref})
|
||||
r = await client.post(f"/api/runs/{run.id}/evidence/judge", json={"units": units})
|
||||
assert r.status_code == 200, r.text
|
||||
body = r.json()
|
||||
assert body["judged"] == [] and body["calls"] == 0 and body["event"] is None
|
||||
assert body["gaps"] and all("裁判模型不可用" in g and REASONS[ref] in g for g in body["gaps"]), body["gaps"]
|
||||
assert body["message"].startswith("结论句裁判未完成:") and REASONS[ref] in body["message"]
|
||||
assert all(v["status"] == "unjudged" for v in body["verdicts"].values()) and body["verdicts"]
|
||||
assert called == [WRITER_MODEL]
|
||||
no_mock(await events(run.id), body)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 助手、门控模型
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_the_assistant_with_a_broken_provider_tells_the_user(monkeypatch, client):
|
||||
#: 助手报给人的那一句:照实说是哪个接入、在设置里能做什么,或者给助手换一个模型(不再说「尚未配置完成」)
|
||||
SAID = {MISSING: f"助手使用的{REASONS[MISSING]}。请在「设置 → 模型接入」中核对名称,或为助手换一个模型",
|
||||
DISABLED: f"助手使用的{REASONS[DISABLED]}。请在「设置 → 模型接入」中启用它,或为助手换一个模型"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ref", [MISSING, DISABLED])
|
||||
async def test_the_assistant_with_a_broken_provider_tells_the_user(monkeypatch, client, ref):
|
||||
called = script(monkeypatch)
|
||||
await put_setting("copilot", {"provider": MISSING, "model": ""})
|
||||
await put_setting("copilot", {"provider": ref, "model": ""})
|
||||
r = await client.post("/api/copilot/generate", json={"instruction": "做一张门店周报"})
|
||||
assert r.status_code == 400
|
||||
assert r.json()["detail"] == f"助手使用的模型尚未配置完成:{REASONS[MISSING]}。请到「设置 → 模型接入」检查"
|
||||
assert r.json()["detail"] == SAID[ref]
|
||||
assert called == []
|
||||
# 查当前用哪个模型:照常返回,不 500,写明为什么用不了
|
||||
got = await client.get("/api/copilot/model")
|
||||
assert got.status_code == 200, got.text
|
||||
body = got.json()
|
||||
assert (body["configured"], body["provider"]) == (True, MISSING)
|
||||
assert (body["configured"], body["provider"]) == (True, ref)
|
||||
assert body["effective_provider"] is None and body["effective_model"] is None
|
||||
assert REASONS[MISSING] in body["error"]
|
||||
assert body["error"] == SAID[ref]
|
||||
no_mock([], r.json(), body)
|
||||
|
||||
|
||||
async def test_the_assistant_model_lookup_survives_a_name_that_is_not_text(client):
|
||||
"""设置里存进了不是字符串的接入名(以前经设置接口就能写进来):照常返回、说清用不了,不 500。"""
|
||||
await put_setting("copilot", {"provider": 123, "model": ""})
|
||||
got = await client.get("/api/copilot/model")
|
||||
assert got.status_code == 200, got.text
|
||||
assert got.json()["effective_provider"] is None and "模型接入「123」不存在" in got.json()["error"]
|
||||
|
||||
|
||||
async def test_drafting_with_the_assistant_model_says_which_provider_is_broken(monkeypatch):
|
||||
"""表格导入、数据目录的 AI 起草也用助手的模型:说法和助手同一句(以前停用的接入说「尚未配置完成」)。"""
|
||||
from app.data import catalog, recipe_ai
|
||||
|
||||
called = script(monkeypatch)
|
||||
for ref in (MISSING, DISABLED):
|
||||
await put_setting("copilot", {"provider": ref, "model": ""})
|
||||
async with SessionLocal() as session:
|
||||
assert (await recipe_ai.ai_availability(session)).reason == SAID[ref]
|
||||
with pytest.raises(catalog.CatalogModelUnavailable) as e:
|
||||
await catalog.resolve_draft_model(session)
|
||||
assert str(e.value) == SAID[ref]
|
||||
assert called == []
|
||||
|
||||
|
||||
async def test_the_assistant_model_lookup_survives_a_model_whose_provider_is_disabled(client):
|
||||
@@ -430,7 +521,8 @@ NINE = datetime(2026, 10, 5, 1, 0, tzinfo=UTC)
|
||||
HOOK = "https://hooks.example.com/agentlab/inbox-k1"
|
||||
|
||||
|
||||
async def test_a_scheduled_formal_run_naming_a_broken_provider_fails_and_is_still_notified(monkeypatch):
|
||||
@pytest.mark.parametrize("ref", [MISSING, DISABLED])
|
||||
async def test_a_scheduled_formal_run_naming_a_broken_provider_fails_and_is_still_notified(monkeypatch, ref):
|
||||
from app.scheduler import loop, webhook
|
||||
|
||||
called = script(monkeypatch)
|
||||
@@ -442,7 +534,7 @@ async def test_a_scheduled_formal_run_naming_a_broken_provider_fails_and_is_stil
|
||||
|
||||
monkeypatch.setattr(webhook, "_transport", httpx.MockTransport(receive))
|
||||
monkeypatch.setattr(webhook, "RETRY_DELAYS", (0, 0, 0))
|
||||
spec = chain(node("start", "input"), node("ask", "llm", "写日报", provider=MISSING, prompt="写今天的门店日报"),
|
||||
spec = chain(node("start", "input"), node("ask", "llm", "写日报", provider=ref, prompt="写今天的门店日报"),
|
||||
node("out", "output", fields=[{"name": "日报", "value": "{{ last_message }}"}]))
|
||||
async with SessionLocal() as session:
|
||||
wf = Workflow(name=f"门店日报-{uuid.uuid4().hex[:6]}", graph=spec, status="published", published_version=1)
|
||||
@@ -463,7 +555,7 @@ async def test_a_scheduled_formal_run_naming_a_broken_provider_fails_and_is_stil
|
||||
assert fire.status == "started" and fire.run_id
|
||||
run = await wait(fire.run_id, ("succeeded", "failed", "cancelled"))
|
||||
assert run.status == "failed" and run.run_class == "formal"
|
||||
assert REASONS[MISSING] in run.error and HINTS[MISSING] in run.error, run.error
|
||||
assert REASONS[ref] in run.error and HINTS[ref] in run.error, run.error
|
||||
assert await webhook.notify_due(datetime.now(UTC) + timedelta(seconds=webhook.SEAL_WAIT_S + 5)) == 1
|
||||
[body] = sent
|
||||
assert (body["event"], body["status"], body["run_id"]) == ("failed", "failed", run.id)
|
||||
|
||||
@@ -178,3 +178,33 @@ async def test_get_chat_model_builds_the_named_provider(session, monkeypatch):
|
||||
monkeypatch.setattr(factory, "build_chat_model", spy)
|
||||
_, model_id = await get_chat_model(session, ModelSpec(provider="乙接入", model="m-b"))
|
||||
assert built == [(target.id, "m-b")] and model_id == "m-b"
|
||||
|
||||
|
||||
async def test_a_name_that_is_not_text_counts_as_named_and_missing(session):
|
||||
"""接口写进设置里的数字一类:以前 resolve_provider 在这里抛 AttributeError(查助手模型的接口跟着 500),
|
||||
再往前是当成没给、悄悄回退。现在按点名了一个不存在的接入报。"""
|
||||
await add(session, "云端接入")
|
||||
for ref in (123, 0):
|
||||
with pytest.raises(ProviderNotConfigured) as e:
|
||||
await resolve_provider(session, ref) # type: ignore[arg-type]
|
||||
assert e.value.code == "missing" and e.value.reason == f"模型接入「{ref}」不存在"
|
||||
|
||||
|
||||
async def test_the_assistant_is_told_which_provider_is_broken(session):
|
||||
"""助手(改图、表格导入、数据目录起草)用的模型取不到时的那句话:点名的接入不存在、已停用,按模型名找到的接入
|
||||
已停用,照实说是哪个、在设置里能做什么;接入本身没配完(缺 key)仍说「尚未配置完成」。"""
|
||||
from app.providers.factory import assistant_unavailable
|
||||
|
||||
await add(session, "停用接入", models=("m-off",), enabled=False)
|
||||
said = {}
|
||||
for case, ref, model in (("missing", "甲接人", None), ("disabled", "停用接入", None), ("by_model", None, "m-off")):
|
||||
with pytest.raises(ProviderNotConfigured) as e:
|
||||
await resolve_provider(session, ref, model)
|
||||
said[case] = assistant_unavailable(e.value)
|
||||
assert said["missing"] == "助手使用的模型接入「甲接人」不存在。请在「设置 → 模型接入」中核对名称,或为助手换一个模型"
|
||||
assert said["disabled"] == "助手使用的模型接入「停用接入」已停用。请在「设置 → 模型接入」中启用它,或为助手换一个模型"
|
||||
assert said["by_model"] == ("助手使用的模型「m-off」所属的模型接入「停用接入」已停用。"
|
||||
"请在「设置 → 模型接入」中启用它,或为助手换一个模型")
|
||||
keyless = ProviderNotConfigured("模型接入「云端接入」没有配置 API Key", "请在接入中填写", code="missing_key")
|
||||
assert assistant_unavailable(keyless) == (
|
||||
"助手使用的模型尚未配置完成:模型接入「云端接入」没有配置 API Key。请到「设置 → 模型接入」检查")
|
||||
|
||||
Reference in New Issue
Block a user