mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-18 13:32:02 +00:00
界面与交互全面翻修(前两波):运行态有了 HUD 和时间轴,终态会收尾,全站令牌、组件、文案统一
来自 2026-09 的 UI/UX 审查(142 条)。本提交是前两波,第三波(记录页接入时间轴、跨页互链、check-ui 更新、全站扫尾)另行提交。 地基 - 设计令牌:全局规则和 .btn/.field/.chip 放进 CSS 层,Tailwind 工具类重新生效,选中态、开启态不再被吞掉;字号阶梯; 辅助文字在所有底色上 >= 4.5:1;状态色专用,节点类型色与之拉开色相;「跟随系统」真的跟随,首帧不闪深色;新字标与 favicon - 基础组件:confirmDialog / promptDialog 取代原生对话框;toast 出错不自动消失、可复制、读屏可播报;离线横幅与断线恢复; 九种剪影的状态徽标;唯一一份状态文案(lib/status)、格式化(lib/format)、术语(lib/terms)、报错人话(lib/errors) - 运行态内核:事件折成航迹(run/trace.ts),可投影到任意时刻;取消 / 失败 / 服务重启后统一收尾,不再永远转圈; run.status 随事件回写;token 事件不再堆进 events - 后端:协作成员逐个完成即发事件,调度者决策有事件;时长分执行 / 墙钟 / 等人;API 时间带时区; 中断的运行也发 stream.end;运行列表可筛选翻页;删除运行 / 工作流有保护;运行默认值接通;可测未保存的配置; 报错说清发生了什么、为什么、怎么办 功能 - 画布:状态四重编码、固定高度遥测槽、按缩放三档、运行胶囊与 HUD、航迹时间轴与回放、执行轨迹边、 等待闸门、跟随执行、正式运行只读、一次性时刻(开场点亮、回扫、阻断级联、出具印章) - 编辑:撤销重做、版本历史、问题面板可定位、三栏可收起、模板与表达式字段区分、变量血缘、快捷键、带搜索的选择器 - 回答流 / 问数据:贴底才跟随滚动、工件下钻、出具横幅列出缺口、答案可复制导出、按会话的忙闲、 答案不再在 2000 字处截断、重启后这一轮会收尾 - 记录页:待审批 / 运行中 / 失败页签、筛选翻页、实时接流、排错路径指到节点、封存凭证常驻 - 管理页:新的「数据」页、写操作失败都有反馈、弹窗里先测连接、统一持久的连接状态 - 外壳:导航改版、⌘K 命令面板、? 快捷键帮助、404、页面标题与 favicon 状态点、后台提醒 验证(沙箱):tsc 干净,pytest 579 passed,14 个检查脚本中 13 个全绿;check-ui 剩 1 项旧断言(按旧日期格式找运行行),第三波更新。
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
"""面向用户的报错不露 Python 异常类名和内部符号,并且说得出下一步。
|
||||
|
||||
以前界面首行红字是「_make_query_tool.<locals>._run() got an unexpected keyword
|
||||
argument 'query'」,助手输入坞里是「RuntimeError: …」,校验失败是一大段 pydantic
|
||||
英文。对问数据的业务用户这些都是天书,而且没有下一步。原始异常不丢:放进 detail,
|
||||
排查的人照样拿得到。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
_INTERNAL = ("Error:", "Error)", "Exception", "Traceback", "<locals>", "Input should be",
|
||||
"validation error", "errors.pydantic.dev")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
def _clean(text: str) -> None:
|
||||
for token in _INTERNAL:
|
||||
assert token not in text, f"面向用户的报错里露出了 {token!r}:{text}"
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 翻译规则本身
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_common_failures_are_put_in_plain_words():
|
||||
import httpx
|
||||
|
||||
from app.api.errors import explain, graph_error, raw
|
||||
|
||||
reason, hint = explain(httpx.ConnectError("[Errno 61] Connection refused"))
|
||||
assert "连不上" in reason and hint
|
||||
|
||||
req = httpx.Request("GET", "https://x/v1/models")
|
||||
denied = httpx.HTTPStatusError("401", request=req, response=httpx.Response(401, request=req))
|
||||
assert "密钥" in explain(denied)[0]
|
||||
|
||||
reason, hint = explain(TypeError("_run() got an unexpected keyword argument 'query'"))
|
||||
_clean(reason)
|
||||
assert "内部" in reason and hint
|
||||
assert raw(TypeError("x")) == "TypeError: x", "原始异常要原样留给 detail"
|
||||
|
||||
from app.engine.schema import GraphSpec
|
||||
|
||||
with pytest.raises(Exception) as e:
|
||||
GraphSpec.model_validate({"nodes": [{"id": "a", "type": "nope"}]})
|
||||
text = graph_error(e.value)
|
||||
_clean(text)
|
||||
assert "第 1 个节点" in text and "nope" in text
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 各接口
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_a_failing_provider_test_explains_itself(client, monkeypatch):
|
||||
import httpx
|
||||
|
||||
import app.api.settings as settings_api
|
||||
|
||||
class Boom:
|
||||
async def ainvoke(self, _prompt):
|
||||
raise httpx.ConnectError("[Errno 61] Connection refused")
|
||||
|
||||
monkeypatch.setattr(settings_api, "build_chat_model", lambda *_a, **_k: Boom())
|
||||
body = (await client.post("/api/providers/test", json={"kind": "mock", "default_model": "m"})).json()
|
||||
assert body["ok"] is False
|
||||
_clean(body["error"])
|
||||
assert "连不上" in body["error"] and body["hint"]
|
||||
assert "ConnectError" in body["detail"], "技术细节里要留着原始异常"
|
||||
|
||||
|
||||
async def test_copilot_stream_errors_are_readable(client, monkeypatch):
|
||||
import app.api.copilot as copilot
|
||||
|
||||
class Broken:
|
||||
async def astream(self, _messages):
|
||||
raise RuntimeError("upstream closed")
|
||||
yield # pragma: no cover
|
||||
|
||||
async def _model(*_a, **_k):
|
||||
return Broken(), "mock-fast"
|
||||
|
||||
monkeypatch.setattr(copilot, "get_chat_model", _model)
|
||||
events = []
|
||||
async with client.stream("POST", "/api/copilot/generate-stream", json={"instruction": "x"}) as r:
|
||||
async for line in r.aiter_lines():
|
||||
if line.startswith("data: "):
|
||||
events.append(json.loads(line[6:]))
|
||||
err = next(e for e in events if e["op"] == "error")
|
||||
_clean(err["message"])
|
||||
assert "upstream closed" in err["message"] and "RuntimeError" in err["detail"]
|
||||
|
||||
|
||||
async def test_copilot_generate_errors_are_readable(client, monkeypatch):
|
||||
import app.api.copilot as copilot
|
||||
|
||||
class Garbage:
|
||||
def with_structured_output(self, _schema):
|
||||
return self
|
||||
|
||||
async def ainvoke(self, _messages):
|
||||
raise ValueError("not json")
|
||||
|
||||
async def _model(*_a, **_k):
|
||||
return Garbage(), "mock-fast"
|
||||
|
||||
monkeypatch.setattr(copilot, "get_chat_model", _model)
|
||||
r = await client.post("/api/copilot/generate", json={"instruction": "x"})
|
||||
assert r.status_code == 502
|
||||
_clean(r.json()["detail"])
|
||||
|
||||
|
||||
async def test_graph_validation_points_at_the_node(client):
|
||||
body = (await client.post("/api/workflows/validate",
|
||||
json={"graph": {"nodes": [{"id": "a", "type": "nope"}]}})).json()
|
||||
message = body["issues"][0]["message"]
|
||||
_clean(message)
|
||||
assert "第 1 个节点的类型「nope」不认识" in message
|
||||
|
||||
|
||||
async def test_publish_level_uses_ui_words(client):
|
||||
wf = (await client.post("/api/workflows", json={"name": "档位", "graph": {}})).json()
|
||||
r = await client.post(f"/api/workflows/{wf['id']}/publish", json={"level": "gold"})
|
||||
assert r.status_code == 400
|
||||
assert "「已发布」" in r.json()["detail"] and "governed" not in r.json()["detail"]
|
||||
await client.delete(f"/api/workflows/{wf['id']}")
|
||||
|
||||
|
||||
async def test_a_failing_tool_run_explains_itself(client, monkeypatch):
|
||||
import app.api.tools as tools_api
|
||||
|
||||
async def boom(*_a, **_k):
|
||||
raise TypeError("_run() got an unexpected keyword argument 'query'")
|
||||
|
||||
monkeypatch.setattr(tools_api, "call_tool", boom)
|
||||
body = (await client.post("/api/tools/file_list/run", json={"args": {}})).json()
|
||||
assert body["ok"] is False
|
||||
_clean(body["error"])
|
||||
assert "TypeError" in body["detail"]
|
||||
|
||||
|
||||
async def test_datasource_introspect_failure_is_readable(client):
|
||||
r = await client.post("/api/datasources", json={"name": "ghostfile", "kind": "sqlite",
|
||||
"database": "/no/such/dir/ghost.db"})
|
||||
assert r.status_code == 201
|
||||
sid = r.json()["id"]
|
||||
try:
|
||||
r = await client.post(f"/api/datasources/{sid}/introspect")
|
||||
assert r.status_code == 400
|
||||
_clean(r.json()["detail"])
|
||||
body = (await client.post(f"/api/datasources/{sid}/test")).json()
|
||||
assert body["ok"] is False
|
||||
_clean(body["error"])
|
||||
assert body["detail"], "原始报错要留在 detail"
|
||||
finally:
|
||||
await client.delete(f"/api/datasources/{sid}")
|
||||
|
||||
|
||||
async def test_embedding_probe_failure_is_readable(client):
|
||||
r = await client.get("/api/kb/embedding/probe", params={"base_url": "http://127.0.0.1:9/v1"})
|
||||
assert r.status_code == 400
|
||||
_clean(r.json()["detail"])
|
||||
|
||||
|
||||
def test_a_broken_document_is_explained_without_class_names():
|
||||
pytest.importorskip("pypdf")
|
||||
from app.memory.parsing import UnsupportedDocument, extract
|
||||
|
||||
with pytest.raises(UnsupportedDocument) as e:
|
||||
extract(b"%PDF-1.4 definitely not a pdf", "broken.pdf")
|
||||
_clean(str(e.value))
|
||||
assert "broken" not in str(e.value) or "PDF" in str(e.value)
|
||||
|
||||
|
||||
async def test_background_processing_failure_is_readable(monkeypatch):
|
||||
from app.api import knowledge as kb_api
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Document
|
||||
from app.memory import kb
|
||||
|
||||
async with SessionLocal() as session:
|
||||
doc = await kb.create_document(session, collection="errs", title="t", content="正文",
|
||||
source="t.txt", mime="text/plain", status="processing")
|
||||
doc_id = doc.id
|
||||
|
||||
async def boom(*_a, **_k):
|
||||
raise AttributeError("'NoneType' object has no attribute 'encode'")
|
||||
|
||||
monkeypatch.setattr(kb, "process_document", boom)
|
||||
await kb_api._process_in_background(doc_id)
|
||||
async with SessionLocal() as session:
|
||||
doc = await session.get(Document, doc_id)
|
||||
assert doc.status == "failed"
|
||||
_clean(doc.error)
|
||||
|
||||
|
||||
async def test_governance_hints_do_not_quote_field_names(client):
|
||||
wf = (await client.post("/api/workflows", json={"name": "治理", "graph": {}})).json()
|
||||
try:
|
||||
body = (await client.get("/api/governance/tool-usage", params={"workflow_id": wf["id"]})).json()
|
||||
assert "unused" not in body["hint"]
|
||||
finally:
|
||||
await client.delete(f"/api/workflows/{wf['id']}")
|
||||
body = (await client.get("/api/governance/exploratory-clusters")).json()
|
||||
assert "promote_candidate" not in body.get("hint", "")
|
||||
|
||||
|
||||
async def test_an_unconfigured_copilot_model_uses_settings_words(client, monkeypatch):
|
||||
import app.api.copilot as copilot
|
||||
from app.providers.factory import ProviderNotConfigured
|
||||
|
||||
async def _model(*_a, **_k):
|
||||
raise ProviderNotConfigured("provider 'gw' 需要填 base_url")
|
||||
|
||||
monkeypatch.setattr(copilot, "get_chat_model", _model)
|
||||
r = await client.post("/api/copilot/generate-stream", json={"instruction": "x"})
|
||||
assert r.status_code == 400
|
||||
detail = r.json()["detail"]
|
||||
assert "模型接入「gw」" in detail and "Base URL" in detail and "provider" not in detail, detail
|
||||
|
||||
|
||||
async def test_a_missing_item_says_it_may_have_been_deleted(client):
|
||||
"""404 多半是别的标签页里删掉了,或者链接过期了。只说「不存在」,人会以为
|
||||
是自己输错了什么;统一说一句「可能已经被删了」,下一步就清楚了。"""
|
||||
gone = "no-such-id"
|
||||
calls = [
|
||||
("get", f"/api/workflows/{gone}", None),
|
||||
("patch", f"/api/workflows/{gone}", {"name": "x"}),
|
||||
("delete", f"/api/workflows/{gone}", None),
|
||||
("post", f"/api/workflows/{gone}/duplicate", None),
|
||||
("get", f"/api/workflows/{gone}/versions/1", None),
|
||||
("post", f"/api/workflows/{gone}/versions/1/restore", None),
|
||||
("post", f"/api/workflows/{gone}/publish", {}),
|
||||
("get", "/api/governance/tool-usage", None),
|
||||
("post", "/api/copilot/from-run", {"run_id": gone}),
|
||||
("post", "/api/copilot/review", {"run_id": gone}),
|
||||
("get", f"/api/conversations/{gone}", None),
|
||||
("get", f"/api/kb/documents/{gone}", None),
|
||||
("delete", f"/api/kb/documents/{gone}", None),
|
||||
("delete", f"/api/skills/{gone}", None),
|
||||
("get", f"/api/artifacts/{'0' * 64}", None),
|
||||
]
|
||||
for method, url, body in calls:
|
||||
kwargs = {"json": body} if body is not None else {}
|
||||
if url.endswith("tool-usage"):
|
||||
kwargs = {"params": {"workflow_id": gone}}
|
||||
r = await client.request(method.upper(), url, **kwargs)
|
||||
assert r.status_code == 404, (method, url, r.status_code, r.text)
|
||||
detail = r.json()["detail"]
|
||||
assert "可能已经被删了" in detail, (method, url, detail)
|
||||
assert gone not in detail, f"把 id 原样念给用户听没用:{detail}"
|
||||
@@ -0,0 +1,260 @@
|
||||
"""「危险工具默认需要人工确认」是工具审批策略的全局默认,而不是一个摆设。
|
||||
|
||||
以前审批只看节点上的 approval(缺省 dangerous),设置页的开关勾不勾都一样——
|
||||
取消勾选以为关掉了审批,勾上以为全局开了,两种理解都不对。
|
||||
|
||||
规则:节点自己配了就用节点的;没配时取全局设置(勾上 = dangerous,取消 = never)。
|
||||
正式运行不吃"取消":它跑的是封存的发布版,受管门禁要求危险工具至少人工确认,
|
||||
一个全局开关不能把它悄悄降掉。
|
||||
|
||||
另外守一件连带的事:记忆域和知识库以前只在第一次 _drive 时传进去,审批恢复、
|
||||
接着跑时回落成 "default"——同一次运行前后两段查的不是同一个库。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sqlite3
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import DataSource, Run, RunEvent, Setting, Workflow
|
||||
from app.engine import compiler
|
||||
from app.engine.runner import run_manager
|
||||
from app.engine.schema import NodeType
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def engine_up():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def confirm_setting():
|
||||
"""把设置里的 run 组改成给定值;测试结束删掉,别影响别的测试。"""
|
||||
async def put(value: bool) -> None:
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Setting, "run")
|
||||
if row:
|
||||
row.value = {**row.value, "confirm_dangerous_tools": value}
|
||||
else:
|
||||
session.add(Setting(key="run", value={"confirm_dangerous_tools": value}))
|
||||
await session.commit()
|
||||
|
||||
yield put
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Setting, "run")
|
||||
if row:
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def writable(tmp_path):
|
||||
path = tmp_path / "w.db"
|
||||
c = sqlite3.connect(path)
|
||||
c.execute("CREATE TABLE t (x INTEGER)")
|
||||
c.executemany("INSERT INTO t VALUES (?)", [(i,) for i in range(5)])
|
||||
c.commit()
|
||||
c.close()
|
||||
name = f"w{uuid.uuid4().hex[:6]}"
|
||||
async with SessionLocal() as session:
|
||||
session.add(DataSource(name=name, kind="sqlite", database=str(path), readonly=False,
|
||||
description="可写测试库", options={}, schema_cache={}, enabled=True))
|
||||
await session.commit()
|
||||
|
||||
def count() -> int:
|
||||
conn = sqlite3.connect(path)
|
||||
try:
|
||||
return conn.execute("SELECT COUNT(*) FROM t").fetchone()[0]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return f"db_query__{name}", count
|
||||
|
||||
|
||||
def node(nid, ntype, **config):
|
||||
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0},
|
||||
"data": {"label": nid, "config": config}}
|
||||
|
||||
|
||||
def chain(*nodes):
|
||||
return {"nodes": list(nodes),
|
||||
"edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
|
||||
|
||||
|
||||
def _delete_graph(tool: str, **approval):
|
||||
return chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("q", "tool", tool=tool, args={"sql": "DELETE FROM t WHERE x = 0"}, **approval),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.q }}"}]),
|
||||
)
|
||||
|
||||
|
||||
async def _settle(run_id: str) -> Run:
|
||||
for _ in range(400):
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run_id)
|
||||
if row.status in ("interrupted", "succeeded", "failed", "cancelled"):
|
||||
return row
|
||||
await asyncio.sleep(0.03)
|
||||
raise AssertionError("等超时了")
|
||||
|
||||
|
||||
async def test_unchecked_setting_lets_unconfigured_nodes_run(writable, confirm_setting):
|
||||
tool, count = writable
|
||||
await confirm_setting(False)
|
||||
run = await run_manager.start(graph=_delete_graph(tool), input_payload={"question": "q"})
|
||||
row = await _settle(run.id)
|
||||
assert row.status == "succeeded", f"全局关了审批,节点没配,却还是停下来了:{row.status}"
|
||||
assert count() == 4
|
||||
|
||||
|
||||
async def test_checked_setting_keeps_asking(writable, confirm_setting):
|
||||
tool, count = writable
|
||||
await confirm_setting(True)
|
||||
run = await run_manager.start(graph=_delete_graph(tool), input_payload={"question": "q"})
|
||||
row = await _settle(run.id)
|
||||
assert row.status == "interrupted"
|
||||
assert count() == 5
|
||||
|
||||
|
||||
async def test_what_the_node_says_wins_over_the_setting(writable, confirm_setting):
|
||||
tool, count = writable
|
||||
await confirm_setting(False)
|
||||
run = await run_manager.start(
|
||||
graph=_delete_graph(tool, approval="dangerous"), input_payload={"question": "q"})
|
||||
row = await _settle(run.id)
|
||||
assert row.status == "interrupted", "节点明说了要审批,全局开关不该盖过它"
|
||||
assert count() == 5
|
||||
|
||||
|
||||
async def test_governed_formal_runs_do_not_take_the_unchecked_setting(writable, confirm_setting):
|
||||
tool, count = writable
|
||||
await confirm_setting(False)
|
||||
graph = _delete_graph(tool)
|
||||
async with SessionLocal() as session:
|
||||
wf = Workflow(name=f"受管-{uuid.uuid4().hex[:4]}", graph=graph, status="governed",
|
||||
published_version=1)
|
||||
session.add(wf)
|
||||
await session.commit()
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": "q"},
|
||||
workflow_id=wf.id, run_class="formal", version=1)
|
||||
row = await _settle(run.id)
|
||||
assert row.status == "interrupted", "受管门禁要独立生效"
|
||||
assert count() == 5
|
||||
|
||||
|
||||
def _governable(tool: str):
|
||||
"""过得了受管门禁的最小图:危险工具节点不配 approval,后面接口径卡和出具契约。"""
|
||||
return chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("q", "tool", tool=tool, args={"sql": "DELETE FROM t WHERE x = 0"}),
|
||||
node("caliber", "metrics", caliber="删除口径", caliber_version="v1", assign_to="m",
|
||||
metrics=[{"id": "done", "name": "已执行", "unit": "次", "expression": "1"}]),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.q }}"}],
|
||||
contract={"metrics_from": ["caliber"], "narrative": "执行了 {{ vars.m.done }} 次",
|
||||
"required": ["done"], "strict": True}),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("level", ["governed", "published"])
|
||||
async def test_formal_runs_keep_asking_after_the_canvas_is_edited(
|
||||
writable, confirm_setting, level
|
||||
):
|
||||
"""发布之后在画布上再存一次,status 就退回 draft,published_version 仍指着发布的那一版。
|
||||
|
||||
以前下限看的是 status == "governed":受管模板只要被编辑过,它的正式运行就跟着
|
||||
全局开关降到「全部自动放行」,DELETE 不经人手直接执行。现在正式运行一律不吃
|
||||
"取消"——跑的是封存的发布版,它的审批行为不该取决于一个随时能改的偏好。
|
||||
"""
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
tool, count = writable
|
||||
await confirm_setting(False)
|
||||
graph = _governable(tool)
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
||||
wf = (await c.post("/api/workflows", json={"name": f"受管-{uuid.uuid4().hex[:4]}",
|
||||
"graph": graph})).json()
|
||||
published = (await c.post(f"/api/workflows/{wf['id']}/publish",
|
||||
json={"level": level})).json()
|
||||
assert published["ok"], published
|
||||
edited = {**graph, "nodes": [*graph["nodes"][:-1],
|
||||
{**graph["nodes"][-1], "data": {
|
||||
**graph["nodes"][-1]["data"], "label": "改过的出口"}}]}
|
||||
after = (await c.patch(f"/api/workflows/{wf['id']}", json={"graph": edited})).json()
|
||||
assert after["status"] == "draft" and after["published_version"] == 1
|
||||
r = await c.post("/api/runs", json={"workflow_id": wf["id"], "run_class": "formal",
|
||||
"input": {"question": "q"}})
|
||||
assert r.status_code == 201, r.text
|
||||
row = await _settle(r.json()["id"])
|
||||
assert row.status == "interrupted", f"编辑过画布后,正式运行跳过了审批:{row.status}"
|
||||
assert row.approval_default == "dangerous"
|
||||
assert count() == 5
|
||||
|
||||
|
||||
async def test_exploratory_runs_of_a_governed_workflow_follow_the_setting(
|
||||
writable, confirm_setting
|
||||
):
|
||||
"""下限只管正式运行。探索运行照旧听全局开关,不然这个开关又成了摆设。"""
|
||||
tool, count = writable
|
||||
await confirm_setting(False)
|
||||
graph = _delete_graph(tool)
|
||||
async with SessionLocal() as session:
|
||||
wf = Workflow(name=f"受管-{uuid.uuid4().hex[:4]}", graph=graph, status="governed",
|
||||
published_version=1)
|
||||
session.add(wf)
|
||||
await session.commit()
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": "q"}, workflow_id=wf.id)
|
||||
row = await _settle(run.id)
|
||||
assert row.status == "succeeded", row.status
|
||||
assert count() == 4
|
||||
|
||||
|
||||
async def test_effective_values_are_sealed_into_the_event_stream(confirm_setting):
|
||||
await confirm_setting(False)
|
||||
graph = chain(node("start", "input"), node("out", "output"))
|
||||
run = await run_manager.start(graph=graph, input_payload={}, memory_scope="ops",
|
||||
collection="manuals")
|
||||
await _settle(run.id)
|
||||
async with SessionLocal() as session:
|
||||
started = (await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run.id, RunEvent.type == "run.started")
|
||||
)).scalars().first()
|
||||
assert started.data["memory_scope"] == "ops"
|
||||
assert started.data["collection"] == "manuals"
|
||||
assert started.data["approval_default"] == "never"
|
||||
|
||||
|
||||
async def test_resume_keeps_the_scope_the_run_started_with(monkeypatch):
|
||||
seen: list[tuple[str, str]] = []
|
||||
original = compiler.RUNNERS[NodeType.TRANSFORM]
|
||||
|
||||
async def spy(state, ctx):
|
||||
seen.append((ctx.run.memory_scope, ctx.run.collection))
|
||||
return await original(state, ctx)
|
||||
|
||||
monkeypatch.setitem(compiler.RUNNERS, NodeType.TRANSFORM, spy)
|
||||
graph = chain(
|
||||
node("start", "input"),
|
||||
node("before", "transform", mode="template", template="前"),
|
||||
node("gate", "human", mode="approve", title="看一眼"),
|
||||
node("after", "transform", mode="template", template="后"),
|
||||
node("out", "output"),
|
||||
)
|
||||
run = await run_manager.start(graph=graph, input_payload={}, memory_scope="ops",
|
||||
collection="manuals")
|
||||
assert (await _settle(run.id)).status == "interrupted"
|
||||
await run_manager.resume(run.id, {"approved": True})
|
||||
for _ in range(200):
|
||||
if len(seen) == 2:
|
||||
break
|
||||
await asyncio.sleep(0.03)
|
||||
assert seen == [("ops", "manuals"), ("ops", "manuals")], "恢复之后换了个库在查"
|
||||
@@ -0,0 +1,223 @@
|
||||
"""让助手改图时,用户摆好的布局不能被整张重排。
|
||||
|
||||
以前 generate-stream 收尾时无条件 auto_layout 整张图:只改了一句提示词,
|
||||
所有节点也被挪到排版位置。工业场景的图常被人工整理成有业务含义的分区
|
||||
(上游数据、口径计算、出具),再加上没有撤销,手工布局就回不来了。
|
||||
|
||||
这里守三件事:已有节点坐标一个不动;新节点落在空处、不压住旧节点;
|
||||
新建(没有 base_graph)照旧整体排版。顺带确认 final.issues 和 reply 照常下发。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.engine.layout import NODE_W, _height
|
||||
from app.engine.schema import GraphSpec
|
||||
from app.main import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
def _node(nid: str, ntype: str, x: float, y: float, config: dict) -> dict:
|
||||
return {"id": nid, "type": ntype, "position": {"x": x, "y": y},
|
||||
"data": {"label": nid, "config": config}}
|
||||
|
||||
|
||||
#: 用户手工摆过的布局:故意不是排版算法会给出的样子
|
||||
BASE = {
|
||||
"nodes": [
|
||||
_node("start", "input", 500, 900, {"fields": [{"name": "question"}]}),
|
||||
_node("answer", "llm", -300, 40, {"prompt": "{{ input.question }}", "assign_to": "answer"}),
|
||||
_node("out", "output", 1200, -600, {"fields": [{"name": "答案", "value": "{{ vars.answer }}"}]}),
|
||||
],
|
||||
"edges": [{"source": "start", "target": "answer"}, {"source": "answer", "target": "out"}],
|
||||
}
|
||||
|
||||
|
||||
class _Scripted:
|
||||
def __init__(self, lines: list[dict]) -> None:
|
||||
self.lines = [json.dumps(o, ensure_ascii=False) for o in lines]
|
||||
|
||||
async def astream(self, messages):
|
||||
from langchain_core.messages import AIMessageChunk
|
||||
|
||||
for line in self.lines:
|
||||
yield AIMessageChunk(content=line + "\n")
|
||||
|
||||
|
||||
async def _events(client, monkeypatch, ops: list[dict], base: dict | None) -> list[dict]:
|
||||
import app.api.copilot as copilot
|
||||
|
||||
async def _model(*_a, **_k):
|
||||
return _Scripted(ops), "mock-fast"
|
||||
|
||||
monkeypatch.setattr(copilot, "get_chat_model", _model)
|
||||
body = {"instruction": "改一下", "base_graph": base}
|
||||
out: list[dict] = []
|
||||
async with client.stream("POST", "/api/copilot/generate-stream", json=body) as r:
|
||||
async for line in r.aiter_lines():
|
||||
if line.startswith("data: "):
|
||||
out.append(json.loads(line[6:]))
|
||||
return out
|
||||
|
||||
|
||||
def _positions(graph: dict) -> dict[str, tuple[float, float]]:
|
||||
return {n["id"]: (n["position"]["x"], n["position"]["y"]) for n in graph["nodes"]}
|
||||
|
||||
|
||||
def _box(graph: dict, nid: str) -> tuple[float, float, float, float]:
|
||||
spec = GraphSpec.model_validate(graph)
|
||||
node = spec.node_map()[nid]
|
||||
return node.position.x, node.position.y, NODE_W, _height(node)
|
||||
|
||||
|
||||
def _overlaps(a, b) -> bool:
|
||||
ax, ay, aw, ah = a
|
||||
bx, by, bw, bh = b
|
||||
return ax < bx + bw and bx < ax + aw and ay < by + bh and by < ay + ah
|
||||
|
||||
|
||||
async def test_editing_a_prompt_moves_nothing(client, monkeypatch):
|
||||
ops = [
|
||||
{"op": "plan", "summary": "改短提示词"},
|
||||
{"op": "update_node", "id": "answer",
|
||||
"config": {"prompt": "简短回答:{{ input.question }}", "assign_to": "answer"}},
|
||||
{"op": "done", "explanation": "改好了", "run": False},
|
||||
]
|
||||
final = (await _events(client, monkeypatch, ops, BASE))[-1]
|
||||
assert final["op"] == "final"
|
||||
assert _positions(final["graph"]) == _positions(BASE), "只改了一句提示词,节点却被挪了"
|
||||
assert final["layout"] == {"mode": "keep", "placed": []}
|
||||
|
||||
|
||||
async def test_a_new_node_lands_in_free_space_and_old_ones_stay(client, monkeypatch):
|
||||
ops = [
|
||||
{"op": "plan", "summary": "加一步复核"},
|
||||
{"op": "add_node", "node": {"id": "review", "type": "llm", "label": "复核",
|
||||
"config": {"prompt": "复核:{{ vars.answer }}", "assign_to": "checked"}}},
|
||||
{"op": "remove_edge", "source": "answer", "target": "out"},
|
||||
{"op": "add_edge", "edge": {"source": "answer", "target": "review"}},
|
||||
{"op": "add_edge", "edge": {"source": "review", "target": "out"}},
|
||||
{"op": "done", "explanation": "加了复核", "run": False},
|
||||
]
|
||||
final = (await _events(client, monkeypatch, ops, BASE))[-1]
|
||||
graph = final["graph"]
|
||||
before = _positions(BASE)
|
||||
after = _positions(graph)
|
||||
assert {k: after[k] for k in before} == before, "新增一个节点,旧节点跟着被挪了"
|
||||
|
||||
new_box = _box(graph, "review")
|
||||
for nid in before:
|
||||
assert not _overlaps(new_box, _box(graph, nid)), f"新节点压住了「{nid}」"
|
||||
assert after["review"] != (0, 0), "新节点没排版,堆在原点"
|
||||
# 落在上游右边:从 answer 连过来,不该跑到它左边去
|
||||
assert after["review"][0] > before["answer"][0]
|
||||
assert final["layout"] == {"mode": "keep", "placed": ["review"]}
|
||||
|
||||
|
||||
async def test_a_node_inserted_into_a_tidy_chain_does_not_cover_its_neighbour(client, monkeypatch):
|
||||
"""排得整整齐齐的链上插一步:正对着上游的那一格已经被下游占了,得另找空处。"""
|
||||
tidy = {
|
||||
"nodes": [
|
||||
_node("start", "input", 80, 0, {"fields": [{"name": "question"}]}),
|
||||
_node("answer", "llm", 414, 0, {"prompt": "{{ input.question }}", "assign_to": "answer"}),
|
||||
_node("out", "output", 748, 0, {"fields": [{"name": "答案", "value": "{{ vars.answer }}"}]}),
|
||||
],
|
||||
"edges": BASE["edges"],
|
||||
}
|
||||
ops = [
|
||||
{"op": "add_node", "node": {"id": "review", "type": "llm", "label": "复核",
|
||||
"config": {"prompt": "{{ vars.answer }}", "assign_to": "checked"}}},
|
||||
{"op": "remove_edge", "source": "answer", "target": "out"},
|
||||
{"op": "add_edge", "edge": {"source": "answer", "target": "review"}},
|
||||
{"op": "add_edge", "edge": {"source": "review", "target": "out"}},
|
||||
{"op": "done", "explanation": "加了复核", "run": False},
|
||||
]
|
||||
graph = (await _events(client, monkeypatch, ops, tidy))[-1]["graph"]
|
||||
after = _positions(graph)
|
||||
assert {k: after[k] for k in _positions(tidy)} == _positions(tidy)
|
||||
for nid in ("start", "answer", "out"):
|
||||
assert not _overlaps(_box(graph, "review"), _box(graph, nid)), f"新节点压住了「{nid}」"
|
||||
|
||||
|
||||
async def test_several_new_nodes_do_not_pile_on_each_other(client, monkeypatch):
|
||||
ops = [{"op": "plan", "summary": "并行三路"}]
|
||||
for i in range(3):
|
||||
ops.append({"op": "add_node", "node": {"id": f"side{i}", "type": "llm", "label": f"旁路{i}",
|
||||
"config": {"prompt": "{{ input.question }}"}}})
|
||||
ops.append({"op": "add_edge", "edge": {"source": "start", "target": f"side{i}"}})
|
||||
ops.append({"op": "done", "explanation": "并行", "run": False})
|
||||
graph = (await _events(client, monkeypatch, ops, BASE))[-1]["graph"]
|
||||
|
||||
ids = [n["id"] for n in graph["nodes"]]
|
||||
for i, a in enumerate(ids):
|
||||
for b in ids[i + 1:]:
|
||||
if a in _positions(BASE) and b in _positions(BASE):
|
||||
continue
|
||||
assert not _overlaps(_box(graph, a), _box(graph, b)), f"「{a}」和「{b}」叠在一起"
|
||||
|
||||
|
||||
async def test_building_from_scratch_still_lays_out_everything(client, monkeypatch):
|
||||
ops = [
|
||||
{"op": "plan", "summary": "两步"},
|
||||
{"op": "add_node", "node": {"id": "start", "type": "input", "label": "入口",
|
||||
"config": {"fields": [{"name": "question"}]}}},
|
||||
{"op": "add_node", "node": {"id": "out", "type": "output", "label": "成果",
|
||||
"config": {"fields": [{"name": "答案", "value": "{{ input.question }}"}]}}},
|
||||
{"op": "add_edge", "edge": {"source": "start", "target": "out"}},
|
||||
{"op": "done", "explanation": "d", "run": False},
|
||||
]
|
||||
final = (await _events(client, monkeypatch, ops, None))[-1]
|
||||
pos = _positions(final["graph"])
|
||||
assert pos["out"][0] > pos["start"][0], "新建的图没有排版"
|
||||
assert final["layout"]["mode"] == "full"
|
||||
|
||||
|
||||
async def test_skipped_node_types_are_reported_in_final_issues(client, monkeypatch):
|
||||
ops = [
|
||||
{"op": "plan", "summary": "x"},
|
||||
{"op": "add_node", "node": {"id": "ghost", "type": "no_such_type", "config": {}}},
|
||||
{"op": "done", "explanation": "d", "run": False},
|
||||
]
|
||||
final = (await _events(client, monkeypatch, ops, BASE))[-1]
|
||||
skipped = [i for i in final["issues"] if i.get("code") == "unknown_node_type"]
|
||||
assert len(skipped) == 1 and skipped[0]["node_id"] is None
|
||||
assert skipped[0]["type"] == "no_such_type" and "no_such_type" in skipped[0]["message"]
|
||||
|
||||
|
||||
async def test_a_text_reply_is_passed_through_on_the_canvas_too(client, monkeypatch):
|
||||
ops = [{"op": "reply", "text": "按 mode 字段分成两路"}]
|
||||
events = await _events(client, monkeypatch, ops, BASE)
|
||||
kinds = [e["op"] for e in events]
|
||||
assert {"op": "reply", "text": "按 mode 字段分成两路"} in events
|
||||
assert "final" not in kinds, "纯文字回复不该再附一张图"
|
||||
|
||||
|
||||
async def test_the_non_streaming_generate_keeps_positions_too(client, monkeypatch):
|
||||
import app.api.copilot as copilot
|
||||
|
||||
class _Structured:
|
||||
def with_structured_output(self, _schema):
|
||||
return self
|
||||
|
||||
async def ainvoke(self, _messages):
|
||||
return {"explanation": "改了提示词", "nodes": [
|
||||
{"id": n["id"], "type": n["type"], "label": n["id"], "config": n["data"]["config"]}
|
||||
for n in BASE["nodes"]
|
||||
], "edges": BASE["edges"]}
|
||||
|
||||
async def _model(*_a, **_k):
|
||||
return _Structured(), "mock-fast"
|
||||
|
||||
monkeypatch.setattr(copilot, "get_chat_model", _model)
|
||||
r = await client.post("/api/copilot/generate", json={"instruction": "改", "base_graph": BASE})
|
||||
assert r.status_code == 200, r.text
|
||||
assert _positions(r.json()["graph"]) == _positions(BASE)
|
||||
@@ -0,0 +1,554 @@
|
||||
"""管理页背后的接口:调试台只看不改、危险操作先说清后果、没保存的配置也能先测。
|
||||
|
||||
每一条都对应界面上一个「看起来生效、其实没生效」或者「说的和做的不一样」的地方:
|
||||
回忆测试会抬高召回计数;标着「需确认」的工具点一下就真跑;数据源的提示指向
|
||||
表单上不存在的入口;填错一项要保存→关弹窗→点测试→再打开改。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy import func, select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import DataSource, Provider, Run, RunEvent, Workflow
|
||||
from app.main import app
|
||||
from app.memory import embeddings as emb
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _local_embedder():
|
||||
emb.configure("local")
|
||||
yield
|
||||
emb.configure("local")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def shop_db(tmp_path):
|
||||
path = tmp_path / "shop.db"
|
||||
db = sqlite3.connect(path)
|
||||
db.execute("CREATE TABLE orders (id INTEGER PRIMARY KEY, amount REAL)")
|
||||
db.execute("INSERT INTO orders VALUES (1, 9.5)")
|
||||
db.commit()
|
||||
db.close()
|
||||
return str(path)
|
||||
|
||||
|
||||
async def _count(model) -> int:
|
||||
async with SessionLocal() as session:
|
||||
return int((await session.execute(select(func.count()).select_from(model))).scalar_one())
|
||||
|
||||
|
||||
def _no_class_names(text: str) -> None:
|
||||
for name in ("Error:", "Exception", "Traceback", "<locals>"):
|
||||
assert name not in text, f"面向用户的报错里露出了内部符号 {name!r}:{text}"
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# manage-4:调试台和运行时对得上,回忆测试只看不改
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def _use_count(client, scope: str) -> int:
|
||||
items = (await client.get("/api/memory", params={"scope": scope})).json()
|
||||
return items[0]["use_count"]
|
||||
|
||||
|
||||
async def test_peek_recall_leaves_the_use_count_alone(client):
|
||||
r = await client.post("/api/memory", json={"content": "活跃用户指当月有订单的用户", "scope": "peek"})
|
||||
assert r.status_code == 201
|
||||
|
||||
r = await client.get("/api/memory/search", params={"q": "活跃用户", "scope": "peek", "peek": "true"})
|
||||
assert r.json()["results"], "peek 也得照常召回,否则调试台就没用了"
|
||||
assert await _use_count(client, "peek") == 0
|
||||
|
||||
# 不带 peek 的是运行时的语义:计一次,下次排序时用得上
|
||||
await client.get("/api/memory/search", params={"q": "活跃用户", "scope": "peek"})
|
||||
assert await _use_count(client, "peek") == 1
|
||||
|
||||
|
||||
async def test_kb_status_and_search_agree_on_the_default_alpha(client):
|
||||
status = (await client.get("/api/kb/embedding")).json()
|
||||
assert status["default_alpha"] == emb.default_alpha()
|
||||
|
||||
body = (await client.get("/api/kb/search", params={"q": "口径"})).json()
|
||||
assert body["alpha"] == status["default_alpha"], "不传 alpha 时实际用了多少,要跟结果一起交出去"
|
||||
body = (await client.get("/api/kb/search", params={"q": "口径", "alpha": 0.3})).json()
|
||||
assert body["alpha"] == 0.3
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# manage-5:危险工具在工具库里直接执行,先说清会做什么
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_a_dangerous_tool_is_not_run_without_confirm(client):
|
||||
from app.core.config import settings
|
||||
|
||||
target = settings.session_dir("playground") / "confirm-probe.txt"
|
||||
target.unlink(missing_ok=True)
|
||||
args = {"path": "confirm-probe.txt", "content": "hi"}
|
||||
|
||||
r = await client.post("/api/tools/file_write/run", json={"args": args})
|
||||
assert r.status_code == 409
|
||||
detail = r.json()["detail"]
|
||||
assert "confirm-probe.txt" in detail and "审批" in detail, detail
|
||||
# 怎么确认(弹窗、再点一次)是界面的事,后端的话不能替它许诺
|
||||
assert "再点" not in detail and "点击" not in detail, detail
|
||||
assert not target.exists(), "409 之前就已经写下去了"
|
||||
|
||||
r = await client.post("/api/tools/file_write/run", params={"confirm": "true"}, json={"args": args})
|
||||
assert r.status_code == 200 and r.json()["ok"] is True
|
||||
assert target.exists()
|
||||
|
||||
target.unlink()
|
||||
r = await client.post("/api/tools/file_write/run", json={"args": args, "confirm": True})
|
||||
assert r.status_code == 200 and target.exists()
|
||||
target.unlink()
|
||||
|
||||
|
||||
async def test_a_safe_tool_runs_straight_away(client):
|
||||
r = await client.post("/api/tools/file_list/run", json={"args": {}})
|
||||
assert r.status_code == 200 and r.json()["ok"] is True
|
||||
|
||||
|
||||
async def test_the_tool_list_says_whether_runtime_approval_really_applies(client):
|
||||
r = await client.post("/api/custom-tools", json={"name": "probe_hook", "kind": "http",
|
||||
"config": {"url": "https://example.com"}})
|
||||
assert r.status_code == 201
|
||||
try:
|
||||
tools = {t["id"]: t for t in (await client.get("/api/tools")).json()}
|
||||
assert tools["file_write"]["runtime_approval"] is True
|
||||
assert tools["file_read"]["runtime_approval"] is False
|
||||
# 有副作用,但运行时的审批关卡认不出它——标签不能说它会被审批
|
||||
assert tools["probe_hook"]["dangerous"] is True
|
||||
assert tools["probe_hook"]["runtime_approval"] is False
|
||||
finally:
|
||||
await client.delete(f"/api/custom-tools/{r.json()['id']}")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# manage-7:提示只提表单上有的东西
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_datasource_hints_only_point_at_fields_the_form_has(client):
|
||||
kinds = {k["value"]: k for k in (await client.get("/api/datasources/kinds")).json()["kinds"]}
|
||||
for kind in kinds.values():
|
||||
assert "options" not in kind["hint"], f"{kind['value']} 的提示指向了表单上没有的 options:{kind['hint']}"
|
||||
assert "schema" in kinds["oracle"]["hint"] and "SID" in kinds["oracle"]["hint"]
|
||||
# 高级连接参数里有什么,由后端给出:提示里提到的键在这里都找得到
|
||||
assert "charset" in [a["key"] for a in kinds["mysql"]["advanced"]]
|
||||
assert {"service_name", "sid"} <= {a["key"] for a in kinds["oracle"]["advanced"]}
|
||||
|
||||
|
||||
async def _oracle(name: str, **fields) -> dict:
|
||||
"""直接写库,好造出新表单之前的存法:service_name 在「数据库」一栏里。"""
|
||||
async with SessionLocal() as session:
|
||||
row = DataSource(name=name, kind="oracle", host="db", username="u", **fields)
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
return {"id": row.id}
|
||||
|
||||
|
||||
async def _oracle_url(source_id: str) -> str:
|
||||
from app.data.engine import build_url
|
||||
|
||||
async with SessionLocal() as session:
|
||||
return build_url(await session.get(DataSource, source_id))
|
||||
|
||||
|
||||
async def test_switching_an_old_oracle_source_to_sid_is_not_ignored(client):
|
||||
"""引擎是 `service_name or database`:老数据的「数据库」一栏有值时,新表单切到
|
||||
SID 存下去会被悄悄无视,连的还是原来那个 service_name。"""
|
||||
src = await _oracle("ora_sid", database="ORCLPDB1", options={})
|
||||
try:
|
||||
r = await client.patch(f"/api/datasources/{src['id']}", json={"options": {"sid": "XE"}})
|
||||
assert r.status_code == 200, r.text
|
||||
assert r.json()["database"] is None and r.json()["options"] == {"sid": "XE"}
|
||||
assert (await _oracle_url(src["id"])).endswith("/?sid=XE")
|
||||
|
||||
r = await client.patch(f"/api/datasources/{src['id']}", json={"options": {"service_name": "BI"}})
|
||||
assert (await _oracle_url(src["id"])).endswith("/?service_name=BI")
|
||||
finally:
|
||||
await client.delete(f"/api/datasources/{src['id']}")
|
||||
|
||||
|
||||
async def test_oracle_service_name_is_kept_in_one_place(client):
|
||||
# 改无关的字段:老数据挪进 options.service_name,连的还是同一个库
|
||||
src = await _oracle("ora_legacy", database="ORCLPDB1", options={"schema": "ODS"})
|
||||
try:
|
||||
r = await client.patch(f"/api/datasources/{src['id']}", json={"enabled": False})
|
||||
body = r.json()
|
||||
assert body["database"] is None
|
||||
assert body["options"] == {"schema": "ODS", "service_name": "ORCLPDB1"}
|
||||
assert (await _oracle_url(src["id"])).endswith("/?service_name=ORCLPDB1")
|
||||
finally:
|
||||
await client.delete(f"/api/datasources/{src['id']}")
|
||||
|
||||
# 旧表单把 service_name 框绑在「数据库」上,保存时整张表单连 options 一起发:
|
||||
# 新填的值得生效,不能被 options 里原样带回来的旧值压住
|
||||
src = await _oracle("ora_oldform", database=None, options={"service_name": "BI", "schema": "ODS"})
|
||||
try:
|
||||
r = await client.patch(f"/api/datasources/{src['id']}", json={
|
||||
"database": "BI2", "options": {"service_name": "BI", "schema": "ODS"}})
|
||||
assert r.json()["options"] == {"service_name": "BI2", "schema": "ODS"}
|
||||
assert (await _oracle_url(src["id"])).endswith("/?service_name=BI2")
|
||||
finally:
|
||||
await client.delete(f"/api/datasources/{src['id']}")
|
||||
|
||||
# 两处都有值的老数据原样再存一次:引擎原来连哪个,存完还连哪个
|
||||
src = await _oracle("ora_both", database="STALE", options={"service_name": "BI"})
|
||||
try:
|
||||
r = await client.patch(f"/api/datasources/{src['id']}", json={
|
||||
"database": "STALE", "options": {"service_name": "BI"}})
|
||||
assert r.json()["database"] is None
|
||||
assert (await _oracle_url(src["id"])).endswith("/?service_name=BI")
|
||||
finally:
|
||||
await client.delete(f"/api/datasources/{src['id']}")
|
||||
|
||||
r = await client.post("/api/datasources", json={"name": "ora_new", "kind": "oracle", "host": "db",
|
||||
"username": "u", "database": "LEFTOVER",
|
||||
"options": {"sid": "XE"}})
|
||||
assert r.status_code == 201, r.text
|
||||
created = r.json()
|
||||
try:
|
||||
assert created["database"] is None and created["options"] == {"sid": "XE"}
|
||||
finally:
|
||||
await client.delete(f"/api/datasources/{created['id']}")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# manage-8:没保存的配置先测,不落库
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_testing_an_unsaved_datasource_does_not_save_it(client, shop_db):
|
||||
before = await _count(DataSource)
|
||||
|
||||
r = await client.post("/api/datasources/test", json={"name": "draft", "kind": "sqlite",
|
||||
"database": shop_db})
|
||||
assert r.status_code == 200 and r.json()["ok"] is True, r.text
|
||||
|
||||
r = await client.post("/api/datasources/test", json={"kind": "sqlite",
|
||||
"database": "/no/such/dir/x.db"})
|
||||
body = r.json()
|
||||
assert body["ok"] is False
|
||||
_no_class_names(body["error"])
|
||||
assert body["hint"] and body["detail"], "人话之外,原始报错也得留着给排查用"
|
||||
|
||||
assert await _count(DataSource) == before, "测试接口往库里写了东西"
|
||||
|
||||
|
||||
async def test_testing_a_mistyped_sqlite_path_does_not_create_the_file(client, tmp_path):
|
||||
"""可写模式下 SQLite 连一个不存在的路径会当场建一个空库:测试说「连得上」,
|
||||
指的却是一个空文件,而且「只测不存」的接口往磁盘上写了东西。"""
|
||||
missing = tmp_path / "typo.db"
|
||||
r = await client.post("/api/datasources/test", json={"kind": "sqlite", "database": str(missing),
|
||||
"readonly": False})
|
||||
body = r.json()
|
||||
assert body["ok"] is False, body
|
||||
assert "打不开这个数据库文件" in body["error"] and body["hint"] and body["detail"]
|
||||
assert not missing.exists(), "测连接把一个空库建出来了"
|
||||
|
||||
r = await client.post("/api/datasources/test", json={"kind": "sqlite", "database": "", "readonly": False})
|
||||
assert r.json()["ok"] is False, "没填路径时连的是内存库,说「连得上」是假的"
|
||||
|
||||
# 已保存的数据源走的是缓存连接,同样不能顺手建文件
|
||||
r = await client.post("/api/datasources", json={"name": "typo_src", "kind": "sqlite",
|
||||
"database": str(missing), "readonly": False})
|
||||
saved = r.json()
|
||||
try:
|
||||
body = (await client.post(f"/api/datasources/{saved['id']}/test")).json()
|
||||
assert body["ok"] is False and not missing.exists()
|
||||
finally:
|
||||
await client.delete(f"/api/datasources/{saved['id']}")
|
||||
|
||||
|
||||
async def test_draft_test_of_an_existing_source_keeps_the_saved_password(client, shop_db):
|
||||
from app.api.datasources import DataSourceTestIn, _draft_source
|
||||
from app.core.crypto import decrypt, encrypt
|
||||
|
||||
row = DataSource(name="saved", kind="postgres", host="db", password=encrypt("s3cret"))
|
||||
draft = _draft_source(DataSourceTestIn(kind="postgres", host="db2"), row)
|
||||
assert decrypt(draft.password) == "s3cret", "编辑时密码框留空表示不改,测连接也得用存着的那个"
|
||||
assert draft.host == "db2"
|
||||
draft = _draft_source(DataSourceTestIn(kind="postgres", password="new"), row)
|
||||
assert decrypt(draft.password) == "new"
|
||||
|
||||
|
||||
async def test_testing_an_unsaved_provider_does_not_save_it(client):
|
||||
before = await _count(Provider)
|
||||
|
||||
r = await client.post("/api/providers/test", json={"name": "草稿", "kind": "mock",
|
||||
"default_model": "mock-fast"})
|
||||
body = r.json()
|
||||
assert body["ok"] is True, body
|
||||
assert body["reply"]
|
||||
|
||||
# 设置页照着契约里的路径调也得通
|
||||
r = await client.post("/api/settings/providers/test", json={"kind": "mock", "default_model": "mock-fast"})
|
||||
assert r.json()["ok"] is True
|
||||
|
||||
r = await client.post("/api/providers/test", json={"kind": "openai_compatible", "api_key": "k",
|
||||
"default_model": "m"})
|
||||
body = r.json()
|
||||
assert body["ok"] is False and "Base URL" in body["error"]
|
||||
|
||||
assert await _count(Provider) == before
|
||||
|
||||
|
||||
async def test_an_openai_compatible_provider_needs_a_base_url(client):
|
||||
r = await client.post("/api/providers", json={"name": "no-url", "kind": "openai_compatible",
|
||||
"api_key": "k"})
|
||||
assert r.status_code == 400 and "Base URL" in r.json()["detail"]
|
||||
|
||||
|
||||
async def test_listing_models_of_an_unsaved_provider(client):
|
||||
r = await client.post("/api/providers/models", json={"kind": "mock"})
|
||||
body = r.json()
|
||||
assert body["ok"] is True and "mock-fast" in body["models"]
|
||||
|
||||
# 连不上时照实说,不抛 500
|
||||
r = await client.post("/api/providers/models", json={"kind": "openai_compatible",
|
||||
"base_url": "http://127.0.0.1:9/v1"})
|
||||
body = r.json()
|
||||
assert body["ok"] is False
|
||||
_no_class_names(body["error"])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def models_endpoint():
|
||||
"""一个本地的 /models:记下每次请求带了哪些头,拿来看钥匙发给了谁。"""
|
||||
import json
|
||||
import threading
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
seen: list[dict[str, str]] = []
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def do_GET(self): # noqa: N802 - http.server 的约定
|
||||
seen.append({k.lower(): v for k, v in self.headers.items()})
|
||||
body = json.dumps({"data": [{"id": "m1"}]}).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("content-type", "application/json")
|
||||
self.send_header("content-length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def log_message(self, *args):
|
||||
pass
|
||||
|
||||
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
try:
|
||||
yield f"http://127.0.0.1:{server.server_address[1]}", seen
|
||||
finally:
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
|
||||
|
||||
async def test_listing_models_never_sends_the_env_openai_key_to_another_host(
|
||||
client, models_endpoint, monkeypatch
|
||||
):
|
||||
base, seen = models_endpoint
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "sk-env-secret")
|
||||
for name in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL"):
|
||||
monkeypatch.delenv(name, raising=False)
|
||||
|
||||
# 表单上还没填 Key 就点「拉取模型」:兼容网关是别人家的服务,环境里那把
|
||||
# OpenAI 的钥匙不能顺手发过去——真正调用时(factory)也从不这么做
|
||||
r = await client.post("/api/providers/models", json={"kind": "openai_compatible",
|
||||
"base_url": f"{base}/v1"})
|
||||
assert r.json()["models"] == ["m1"]
|
||||
assert "authorization" not in seen[-1], seen[-1]
|
||||
|
||||
r = await client.post("/api/providers/models", json={"kind": "openai_compatible", "api_key": "sk-typed",
|
||||
"base_url": f"{base}/v1"})
|
||||
assert seen[-1]["authorization"] == "Bearer sk-typed"
|
||||
|
||||
# 官方 OpenAI 本来就会退回环境变量,和真正调用时一致
|
||||
r = await client.post("/api/providers/models", json={"kind": "openai", "base_url": f"{base}/v1"})
|
||||
assert seen[-1]["authorization"] == "Bearer sk-env-secret"
|
||||
|
||||
|
||||
async def test_listing_models_authenticates_like_the_real_call(client, models_endpoint):
|
||||
base, seen = models_endpoint
|
||||
|
||||
await client.post("/api/providers/models", json={"kind": "anthropic", "api_key": "k1", "base_url": base})
|
||||
assert seen[-1]["x-api-key"] == "k1" and "authorization" not in seen[-1]
|
||||
|
||||
await client.post("/api/providers/models", json={"kind": "anthropic", "api_key": "k2", "base_url": base,
|
||||
"extra": {"auth_style": "bearer"}})
|
||||
assert seen[-1]["authorization"] == "Bearer k2" and "x-api-key" not in seen[-1]
|
||||
|
||||
# 编辑已有接入时,请求头回显的是 ***:拉模型得换回存着的真值
|
||||
r = await client.post("/api/providers", json={
|
||||
"name": "gw-headers", "kind": "openai_compatible", "base_url": f"{base}/v1", "api_key": "k3",
|
||||
"extra": {"headers": {"X-Tenant": "acme-real"}},
|
||||
})
|
||||
assert r.status_code in (200, 201), r.text
|
||||
saved = r.json()
|
||||
try:
|
||||
await client.post("/api/providers/models", json={
|
||||
"id": saved["id"], "kind": "openai_compatible", "base_url": f"{base}/v1",
|
||||
"extra": {"headers": {"X-Tenant": "***"}},
|
||||
})
|
||||
assert seen[-1]["x-tenant"] == "acme-real"
|
||||
assert seen[-1]["authorization"] == "Bearer k3", "Key 框留空表示沿用已保存的"
|
||||
finally:
|
||||
await client.delete(f"/api/providers/{saved['id']}")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# manage-17:记忆看得出是哪来的,能原地改
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_memory_items_say_where_they_came_from_and_can_be_edited(client):
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name="周报", status="succeeded", graph={"nodes": [
|
||||
{"id": "remember_table", "type": "memory", "data": {"label": "记下口径", "config": {}}},
|
||||
], "edges": []})
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
run_id = run.id
|
||||
|
||||
r = await client.post("/api/memory", json={"content": "毛利率按含税收入算", "scope": "src",
|
||||
"meta": {"run_id": run_id, "node_id": "remember_table"}})
|
||||
from_run = r.json()
|
||||
assert from_run["source"] == {"kind": "run", "run_id": run_id, "node_id": "remember_table",
|
||||
"node_label": "记下口径", "workflow_name": "周报", "run_exists": True}
|
||||
assert from_run["created_at"]
|
||||
|
||||
r = await client.post("/api/memory", json={"content": "我叫张三", "scope": "src"})
|
||||
assert r.json()["source"]["kind"] == "manual"
|
||||
|
||||
listed = {m["id"]: m for m in (await client.get("/api/memory", params={"scope": "src"})).json()}
|
||||
assert listed[from_run["id"]]["source"]["node_label"] == "记下口径"
|
||||
|
||||
r = await client.patch(f"/api/memory/{from_run['id']}",
|
||||
json={"content": "毛利率按不含税收入算", "importance": 0.8})
|
||||
assert r.status_code == 200, r.text
|
||||
edited = r.json()
|
||||
assert edited["content"] == "毛利率按不含税收入算" and edited["importance"] == 0.8
|
||||
assert edited["source"]["run_id"] == run_id, "改内容不该把来源抹掉"
|
||||
from app.db.models import MemoryItem
|
||||
from app.memory.embeddings import embed_text, from_blob
|
||||
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(MemoryItem, from_run["id"])
|
||||
stored = from_blob(row.embedding, row.embed_dim)
|
||||
fresh = await embed_text("毛利率按不含税收入算")
|
||||
assert stored is not None and list(stored) == list(fresh), "改完内容,向量得跟着重算"
|
||||
|
||||
assert (await client.patch("/api/memory/nope", json={"content": "x"})).status_code == 404
|
||||
# 只有空白的内容和空串是一回事:存下去就是一条空记忆
|
||||
for blank in ("", " ", "\n\t"):
|
||||
r = await client.patch(f"/api/memory/{from_run['id']}", json={"content": blank})
|
||||
assert r.status_code == 422, (blank, r.text)
|
||||
assert (await client.post("/api/memory", json={"content": " ", "scope": "src"})).status_code == 422
|
||||
|
||||
|
||||
async def test_memory_items_do_not_offer_a_time_that_moves_on_recall(client):
|
||||
"""updated_at 每次召回都会跟着动(记计数也是一次写),当「修改于」显示就是错的。
|
||||
|
||||
界面要的两个时间是「记下于」和「上次召回」,各自有字段。
|
||||
"""
|
||||
r = await client.post("/api/memory", json={"content": "库存周转按月末库存算", "scope": "when"})
|
||||
item = r.json()
|
||||
assert item["created_at"] and "last_used_at" in item
|
||||
assert "updated_at" not in item
|
||||
await client.get("/api/memory/search", params={"q": "库存周转", "scope": "when"})
|
||||
listed = (await client.get("/api/memory", params={"scope": "when"})).json()[0]
|
||||
assert listed["last_used_at"] and "updated_at" not in listed
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# manage-6:知识库收什么格式,一处定义
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_kb_formats_come_from_the_parser(client):
|
||||
formats = (await client.get("/api/kb/formats")).json()
|
||||
assert ".pptx" in formats["extensions"]
|
||||
assert ".csv" not in formats["extensions"] and ".csv" in formats["tabular"]
|
||||
assert formats["accept"] == ",".join(formats["extensions"])
|
||||
|
||||
from app.memory.parsing import extract
|
||||
|
||||
for ext in formats["text"]:
|
||||
assert extract("你好".encode(), f"a{ext}") == "你好"
|
||||
|
||||
|
||||
async def test_a_csv_upload_is_pointed_at_datasources(client):
|
||||
r = await client.post("/api/kb/upload", files={"file": ("sales.csv", b"a,b\n1,2", "text/csv")})
|
||||
assert r.status_code == 415
|
||||
detail = r.json()["detail"]
|
||||
assert "数据源" in detail and "传表格" in detail, detail
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# studio-05:取一个历史版本的图
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _graph(field: str) -> dict:
|
||||
return {"nodes": [{"id": "a", "type": "input", "position": {"x": 0, "y": 0},
|
||||
"data": {"label": "入口", "config": {"fields": [{"name": field}]}}}],
|
||||
"edges": []}
|
||||
|
||||
|
||||
async def test_a_published_version_can_be_fetched_with_its_input_fields(client):
|
||||
wf = (await client.post("/api/workflows", json={"name": "版本", "graph": _graph("q")})).json()
|
||||
await client.patch(f"/api/workflows/{wf['id']}", json={"graph": _graph("q2")})
|
||||
|
||||
r = await client.get(f"/api/workflows/{wf['id']}/versions/1")
|
||||
assert r.status_code == 200, r.text
|
||||
body = r.json()
|
||||
assert body["version"] == 1 and body["graph"] == _graph("q")
|
||||
assert [f["name"] for f in body["input_fields"]] == ["q"]
|
||||
assert body["graph_hash"]
|
||||
|
||||
assert (await client.get(f"/api/workflows/{wf['id']}/versions/9")).status_code == 404
|
||||
assert (await client.get("/api/workflows/nope/versions/1")).status_code == 404
|
||||
await client.delete(f"/api/workflows/{wf['id']}")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# runs-15:只从跑通的运行提取模板
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def _run_with(status: str) -> str:
|
||||
graph = {"nodes": [{"id": "a", "type": "input", "data": {"label": "入口", "config": {}}}],
|
||||
"edges": []}
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name="提取", status=status, graph=graph)
|
||||
session.add(run)
|
||||
await session.flush()
|
||||
session.add(RunEvent(run_id=run.id, seq=1, type="node.started", node_id="a", ts=0.0, data={}))
|
||||
await session.commit()
|
||||
return run.id
|
||||
|
||||
|
||||
async def test_only_a_succeeded_run_can_become_a_template(client):
|
||||
for status in ("failed", "cancelled", "interrupted", "running"):
|
||||
r = await client.post("/api/copilot/from-run", json={"run_id": await _run_with(status)})
|
||||
assert r.status_code == 409, (status, r.text)
|
||||
assert "没有跑完" in r.json()["detail"]
|
||||
|
||||
before = await _count(Workflow)
|
||||
r = await client.post("/api/copilot/from-run", json={"run_id": await _run_with("succeeded")})
|
||||
assert r.status_code == 200, r.text
|
||||
assert await _count(Workflow) == before + 1
|
||||
await client.delete(f"/api/workflows/{r.json()['workflow_id']}")
|
||||
@@ -0,0 +1,127 @@
|
||||
"""设置页的「运行默认值」必须真的被读到。
|
||||
|
||||
以前这三项只存在于 DEFAULT_SETTINGS 里:发起运行永远落在 "default",
|
||||
「危险工具默认需要人工确认」勾不勾都一样。用户改了默认知识库,以为以后的
|
||||
运行都会用它——这比没有这个开关更糟,它给的是虚假的确定感。
|
||||
|
||||
这里守发起运行这一侧:请求没带的 memory_scope / collection 取设置里的值;
|
||||
审批默认值的换算给引擎一个唯一的出处。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Run, Setting
|
||||
from app.main import app
|
||||
|
||||
GRAPH = {
|
||||
"nodes": [
|
||||
{"id": "a", "type": "input", "data": {"label": "入口", "config": {"fields": [{"name": "q"}]}}},
|
||||
{"id": "b", "type": "output", "data": {"label": "成果",
|
||||
"config": {"fields": [{"name": "x", "value": "{{ input.q }}"}]}}},
|
||||
],
|
||||
"edges": [{"source": "a", "target": "b"}],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def _clean_run_settings():
|
||||
async def drop() -> None:
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Setting, "run")
|
||||
if row:
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
|
||||
await drop()
|
||||
yield
|
||||
await drop()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def started(monkeypatch):
|
||||
"""截下交给 run_manager.start 的参数,不真的起执行任务(测试里没有 checkpointer)。"""
|
||||
from app.api import runs as runs_api
|
||||
|
||||
captured: dict = {}
|
||||
|
||||
async def fake_start(**kw):
|
||||
captured.update(kw)
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name=kw.get("workflow_name", ""), status="queued",
|
||||
graph=kw["graph"], input=kw["input_payload"])
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
await session.refresh(run)
|
||||
return run
|
||||
|
||||
monkeypatch.setattr(runs_api.run_manager, "start", fake_start)
|
||||
return captured
|
||||
|
||||
|
||||
async def _put_run_settings(client, **values) -> None:
|
||||
base = {"default_memory_scope": "default", "default_collection": "default",
|
||||
"stream_tokens": True, "confirm_dangerous_tools": True}
|
||||
r = await client.put("/api/settings", json={"values": {"run": {**base, **values}}})
|
||||
assert r.status_code == 200
|
||||
|
||||
|
||||
async def test_a_run_without_scope_uses_the_settings_defaults(client, started):
|
||||
await _put_run_settings(client, default_memory_scope="ops", default_collection="manuals")
|
||||
|
||||
r = await client.post("/api/runs", json={"graph": GRAPH, "input": {"q": "x"}})
|
||||
assert r.status_code == 201, r.text
|
||||
assert started["memory_scope"] == "ops"
|
||||
assert started["collection"] == "manuals"
|
||||
|
||||
|
||||
async def test_what_the_request_says_wins_over_the_defaults(client, started):
|
||||
await _put_run_settings(client, default_memory_scope="ops", default_collection="manuals")
|
||||
|
||||
r = await client.post("/api/runs", json={"graph": GRAPH, "memory_scope": "team",
|
||||
"collection": "specs"})
|
||||
assert r.status_code == 201, r.text
|
||||
assert (started["memory_scope"], started["collection"]) == ("team", "specs")
|
||||
|
||||
|
||||
async def test_blank_or_missing_settings_fall_back_to_default(client, started):
|
||||
r = await client.post("/api/runs", json={"graph": GRAPH})
|
||||
assert r.status_code == 201, r.text
|
||||
assert (started["memory_scope"], started["collection"]) == ("default", "default")
|
||||
|
||||
# 设置页把输入框清空再保存,存下来的是空串——不能拿空串当记忆域
|
||||
await _put_run_settings(client, default_memory_scope="", default_collection=" ")
|
||||
r = await client.post("/api/runs", json={"graph": GRAPH})
|
||||
assert (started["memory_scope"], started["collection"]) == ("default", "default")
|
||||
|
||||
|
||||
async def test_run_defaults_reads_through_to_the_stored_group(client):
|
||||
from app.api.settings import run_defaults
|
||||
|
||||
await _put_run_settings(client, default_collection="manuals", confirm_dangerous_tools=False)
|
||||
async with SessionLocal() as session:
|
||||
values = await run_defaults(session)
|
||||
assert values["default_collection"] == "manuals"
|
||||
assert values["confirm_dangerous_tools"] is False
|
||||
# 没存过的键回落到内置默认,而不是 KeyError
|
||||
assert values["stream_tokens"] is True
|
||||
|
||||
|
||||
def test_confirm_dangerous_tools_maps_onto_an_approval_policy():
|
||||
"""引擎那边只认 approval 的取值,这里是唯一的换算出处。"""
|
||||
from app.api.settings import tool_approval_default
|
||||
|
||||
assert tool_approval_default({"confirm_dangerous_tools": True}) == "dangerous"
|
||||
assert tool_approval_default({"confirm_dangerous_tools": False}) == "never"
|
||||
# 缺省、存坏了都按开着算:关审批必须是一次明确的选择
|
||||
assert tool_approval_default({}) == "dangerous"
|
||||
assert tool_approval_default({"confirm_dangerous_tools": None}) == "dangerous"
|
||||
@@ -0,0 +1,253 @@
|
||||
"""耗时口径:经过审批恢复或接着跑的运行,耗时不能只算最后一段。
|
||||
|
||||
真实运行 2609b1c7:事件从头到尾约 87 秒,列表和页脚却写 22ms——每次 _drive
|
||||
都把 started_at 改成"这一段的开始",_finalize 又拿这一段的耗时覆盖 usage.duration_ms。
|
||||
审批恢复和接着跑正是治理的主路径,所以凡是走过这条路的运行,耗时全是错的。
|
||||
|
||||
现在的口径:
|
||||
- started_at 只在第一次开始时写;
|
||||
- duration_ms(= active_ms)是各段执行时长之和;
|
||||
- wall_ms 是从第一次开始到结束的墙钟;wait_ms 是等人审批的总时长;
|
||||
- 三者随 run.finished / run.failed / run.cancelled 的 timing 一起发出。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Run, RunEvent
|
||||
from app.engine import compiler
|
||||
from app.engine.context import NodeError
|
||||
from app.engine.runner import run_manager
|
||||
from app.engine.schema import NodeType
|
||||
|
||||
SEGMENT = 0.3
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def engine_up():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
def node(nid, ntype, **config):
|
||||
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0},
|
||||
"data": {"label": nid, "config": config}}
|
||||
|
||||
|
||||
def chain(*nodes):
|
||||
return {"nodes": list(nodes),
|
||||
"edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def slow_transforms(monkeypatch):
|
||||
"""id 以 slow 开头的整形节点各卡 SEGMENT 秒:让每一段执行都有可量的长度。"""
|
||||
original = compiler.RUNNERS[NodeType.TRANSFORM]
|
||||
|
||||
async def runner(state, ctx):
|
||||
if ctx.node.id.startswith("slow"):
|
||||
await asyncio.sleep(SEGMENT)
|
||||
return await original(state, ctx)
|
||||
|
||||
monkeypatch.setitem(compiler.RUNNERS, NodeType.TRANSFORM, runner)
|
||||
|
||||
|
||||
async def _wait(run_id: str, statuses: tuple[str, ...], timeout: float = 20.0) -> Run:
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run_id)
|
||||
if row and row.status in statuses:
|
||||
return row
|
||||
await asyncio.sleep(0.03)
|
||||
raise AssertionError(f"等超时了:{run_id}")
|
||||
|
||||
|
||||
async def _events(run_id: str) -> list[RunEvent]:
|
||||
async with SessionLocal() as session:
|
||||
return list((await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run_id).order_by(RunEvent.seq)
|
||||
)).scalars())
|
||||
|
||||
|
||||
def _same_instant(a: datetime, b: datetime) -> bool:
|
||||
return abs((a - b).total_seconds()) < 1e-3
|
||||
|
||||
|
||||
APPROVAL_GRAPH = chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("slow1", "transform", mode="template", template="第一段"),
|
||||
node("gate", "human", mode="approve", title="看一眼"),
|
||||
node("slow2", "transform", mode="template", template="第二段"),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.slow2 }}"}]),
|
||||
)
|
||||
|
||||
WAIT = 0.6
|
||||
|
||||
|
||||
async def test_resumed_run_keeps_its_start_and_sums_every_segment(slow_transforms):
|
||||
run = await run_manager.start(graph=APPROVAL_GRAPH, input_payload={"question": "q"})
|
||||
paused = await _wait(run.id, ("interrupted", "failed"))
|
||||
assert paused.status == "interrupted", paused.error
|
||||
first_start = paused.started_at
|
||||
assert first_start is not None
|
||||
|
||||
await asyncio.sleep(WAIT) # 人去倒了杯水
|
||||
await run_manager.resume(run.id, {"approved": True})
|
||||
done = await _wait(run.id, ("succeeded", "failed"))
|
||||
assert done.status == "succeeded", done.error
|
||||
|
||||
assert _same_instant(done.started_at, first_start), \
|
||||
f"恢复把 started_at 从 {first_start} 改成了 {done.started_at}"
|
||||
|
||||
usage = done.usage
|
||||
# 两段各至少 SEGMENT 秒。只算最后一段的话这里只有一个 SEGMENT
|
||||
assert usage["duration_ms"] >= 2 * SEGMENT * 1000 * 0.9, usage
|
||||
assert usage["active_ms"] == usage["duration_ms"]
|
||||
assert WAIT * 1000 * 0.8 <= usage["wait_ms"] < usage["wall_ms"], usage
|
||||
assert usage["wall_ms"] >= usage["active_ms"] + usage["wait_ms"] - 50, usage
|
||||
wall = (done.finished_at - done.started_at).total_seconds() * 1000
|
||||
assert abs(usage["wall_ms"] - wall) < 100, (usage, wall)
|
||||
|
||||
finished = next(e for e in await _events(run.id) if e.type == "run.finished")
|
||||
assert finished.data["timing"] == {
|
||||
"wall_ms": usage["wall_ms"], "active_ms": usage["active_ms"], "wait_ms": usage["wait_ms"],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fails_once(monkeypatch):
|
||||
original = compiler.RUNNERS[NodeType.TRANSFORM]
|
||||
calls = {"n": 0}
|
||||
|
||||
async def runner(state, ctx):
|
||||
if ctx.node.id.startswith("slow"):
|
||||
await asyncio.sleep(SEGMENT)
|
||||
if ctx.node.id == "flaky":
|
||||
calls["n"] += 1
|
||||
if calls["n"] == 1:
|
||||
raise NodeError(ctx.node.id, "第一次故意失败")
|
||||
return await original(state, ctx)
|
||||
|
||||
monkeypatch.setitem(compiler.RUNNERS, NodeType.TRANSFORM, runner)
|
||||
return calls
|
||||
|
||||
|
||||
async def test_continued_run_keeps_its_start_and_sums_every_segment(fails_once):
|
||||
graph = chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("slow1", "transform", mode="template", template="前面这步"),
|
||||
node("flaky", "transform", mode="template", template="会挂一次"),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.flaky }}"}]),
|
||||
)
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": "q"})
|
||||
failed = await _wait(run.id, ("failed", "succeeded"))
|
||||
assert failed.status == "failed"
|
||||
first_start = failed.started_at
|
||||
assert failed.usage["duration_ms"] >= SEGMENT * 1000 * 0.9
|
||||
|
||||
failed_ev = next(e for e in await _events(run.id) if e.type == "run.failed")
|
||||
assert set(failed_ev.data["timing"]) == {"wall_ms", "active_ms", "wait_ms"}
|
||||
assert failed_ev.data["node_id"] == "flaky"
|
||||
|
||||
await run_manager.continue_failed(run.id)
|
||||
done = await _wait(run.id, ("succeeded", "failed"))
|
||||
assert done.status == "succeeded", done.error
|
||||
assert _same_instant(done.started_at, first_start)
|
||||
# 第二段只跑了 flaky 和 out,很快。只算最后一段的话会远小于 SEGMENT
|
||||
assert done.usage["duration_ms"] >= SEGMENT * 1000 * 0.9, done.usage
|
||||
# 失败到接着跑之间不是在等审批
|
||||
assert done.usage["wait_ms"] == 0, done.usage
|
||||
|
||||
|
||||
async def test_cancel_reports_timing_too(slow_transforms):
|
||||
graph = chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("slow1", "transform", mode="template", template="x"),
|
||||
node("slow2", "transform", mode="template", template="y"),
|
||||
)
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": "q"})
|
||||
await asyncio.sleep(SEGMENT / 2)
|
||||
assert await run_manager.cancel(run.id)
|
||||
await _wait(run.id, ("cancelled",))
|
||||
cancelled = next(e for e in await _events(run.id) if e.type == "run.cancelled")
|
||||
assert set(cancelled.data["timing"]) == {"wall_ms", "active_ms", "wait_ms"}
|
||||
assert cancelled.data["timing"]["active_ms"] > 0
|
||||
|
||||
|
||||
async def test_long_output_says_it_was_clipped():
|
||||
"""事件里的 output 为了控制体积会截断——截了就得说,消费方才知道要回源取全文。"""
|
||||
long_text = "很长的结论。" * 1500
|
||||
graph = chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("out", "output", fields=[{"name": "answer", "value": "{{ input.question }}"}]),
|
||||
)
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": long_text})
|
||||
done = await _wait(run.id, ("succeeded", "failed"))
|
||||
assert done.status == "succeeded", done.error
|
||||
finished = next(e for e in await _events(run.id) if e.type == "run.finished")
|
||||
assert finished.data.get("output_truncated") is True
|
||||
assert len(finished.data["output"]["answer"]) < len(long_text)
|
||||
assert done.output["answer"] == long_text, "库里的是完整版本"
|
||||
|
||||
short = await run_manager.start(graph=graph, input_payload={"question": "短的"})
|
||||
await _wait(short.id, ("succeeded",))
|
||||
finished = next(e for e in await _events(short.id) if e.type == "run.finished")
|
||||
assert not finished.data.get("output_truncated")
|
||||
|
||||
|
||||
async def test_event_ts_is_when_it_happened_not_when_it_was_forwarded():
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name="ts", status="running", graph={}, input={})
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
run_id = run.id
|
||||
produced_at = time.time() - 5
|
||||
await run_manager._handle_custom(run_id, {
|
||||
"__agentlab__": True, "type": "log", "node_id": "x", "ts": produced_at,
|
||||
"data": {"message": "早先发生的事"},
|
||||
})
|
||||
[event] = await _events(run_id)
|
||||
assert abs(event.ts - produced_at) < 1e-6
|
||||
|
||||
|
||||
async def test_node_started_says_which_iteration_and_whether_it_was_a_replay(slow_transforms):
|
||||
graph = {
|
||||
"nodes": [
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("lp", "loop", mode="foreach", items='["a", "b", "c"]', max_iterations=5),
|
||||
node("body", "transform", mode="template", template="{{ vars.item }}"),
|
||||
node("gate", "human", mode="approve", title="收尾前看一眼"),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "ok"}]),
|
||||
],
|
||||
"edges": [
|
||||
{"source": "start", "target": "lp"},
|
||||
{"source": "lp", "target": "body", "sourceHandle": "body"},
|
||||
{"source": "body", "target": "lp"},
|
||||
{"source": "lp", "target": "gate", "sourceHandle": "done"},
|
||||
{"source": "gate", "target": "out"},
|
||||
],
|
||||
}
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": "q"})
|
||||
await _wait(run.id, ("interrupted",))
|
||||
await run_manager.resume(run.id, {"approved": True})
|
||||
await _wait(run.id, ("succeeded",))
|
||||
|
||||
events = await _events(run.id)
|
||||
body = [e.data.get("iteration") for e in events
|
||||
if e.type == "node.started" and e.node_id == "body"]
|
||||
assert body == [1, 2, 3]
|
||||
gate = [e.data for e in events if e.type == "node.started" and e.node_id == "gate"]
|
||||
assert len(gate) == 2
|
||||
assert not gate[0].get("resumed")
|
||||
assert gate[1].get("resumed") is True, "恢复后重放的那一次要标出来"
|
||||
out = [e.data for e in events if e.type == "node.started" and e.node_id == "out"]
|
||||
assert not out[0].get("resumed"), "恢复之后新走到的节点不是重放"
|
||||
@@ -0,0 +1,469 @@
|
||||
"""运行与审批接口的形状:时区、列表筛选与翻页、审批上下文、删除保护、事件流收尾。
|
||||
|
||||
- 时区:库是 SQLite,DateTime 读回来是 naive 的,API 吐出 '2026-09-26T01:11:19'。
|
||||
JS 把不带偏移的 ISO 串当本地时间解析,列表整体慢 8 小时。
|
||||
- 列表:只有一个 limit,待审批的运行排在第 145 位就永远翻不到。
|
||||
- 审批:返回项里没有工作流名、节点名,全局待办列表拼不出一行像样的字。
|
||||
- 删除:运行中的能删、封存过的正式运行一键就删——出具的追溯依据说没就没。
|
||||
- 事件流:连到一条停在 interrupted 的运行,回放完就不再有下文,客户端以为它还在跑。
|
||||
- 接着跑:服务重启挂起的运行被说成「在等待审批」,还让人去一张不存在的审批卡上处理;
|
||||
已取消的只说不行,不说该怎么办。不存在的运行,detail 带着 Python repr 的引号。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from urllib.parse import quote
|
||||
|
||||
import pytest
|
||||
from fastapi import WebSocketDisconnect
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Run, RunEvent, Workflow
|
||||
from app.engine import compiler
|
||||
from app.engine.runner import run_manager
|
||||
from app.engine.schema import NodeType
|
||||
from app.main import app
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def engine_up():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
def node(nid, ntype, label=None, **config):
|
||||
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0},
|
||||
"data": {"label": label or nid, "config": config}}
|
||||
|
||||
|
||||
def chain(*nodes):
|
||||
return {"nodes": list(nodes),
|
||||
"edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}
|
||||
|
||||
|
||||
GATED = chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("gate", "human", label="发布前把关", mode="approve", title="这条公告可以发吗?"),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.gate.approved }}"}]),
|
||||
)
|
||||
|
||||
|
||||
async def _wait(run_id: str, statuses: tuple[str, ...], timeout: float = 15.0) -> Run:
|
||||
for _ in range(int(timeout / 0.03)):
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run_id)
|
||||
if row and row.status in statuses:
|
||||
return row
|
||||
await asyncio.sleep(0.03)
|
||||
raise AssertionError(f"等超时了:{run_id}")
|
||||
|
||||
|
||||
def _aware(value: str) -> datetime:
|
||||
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
||||
assert parsed.tzinfo is not None, f"时间不带时区:{value}"
|
||||
return parsed
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 时区
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_every_time_the_api_returns_carries_an_offset(client):
|
||||
r = await client.post("/api/runs", json={"graph": GATED, "input": {"question": "q"}})
|
||||
assert r.status_code == 201, r.text
|
||||
run_id = r.json()["id"]
|
||||
await _wait(run_id, ("interrupted",))
|
||||
|
||||
body = (await client.get(f"/api/runs/{run_id}")).json()
|
||||
created = _aware(body["created_at"])
|
||||
_aware(body["started_at"])
|
||||
# 写进去的是 UTC 的"现在",读回来也得是同一个时刻
|
||||
assert abs((datetime.now(timezone.utc) - created).total_seconds()) < 60
|
||||
|
||||
listed = (await client.get("/api/runs", params={"limit": 5})).json()
|
||||
assert all(_aware(r["created_at"]) for r in listed)
|
||||
|
||||
[approval] = (await client.get("/api/approvals", params={"run_id": run_id})).json()
|
||||
_aware(approval["created_at"])
|
||||
|
||||
for artifact in (await client.get(f"/api/runs/{run_id}/artifacts")).json():
|
||||
_aware(artifact["created_at"])
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 列表
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def seeded_runs():
|
||||
"""一个专属工作流下的一组运行,时间逐条错开。只查它们,不受别的测试干扰。"""
|
||||
base = datetime(2026, 9, 1, 8, 0, tzinfo=timezone.utc)
|
||||
async with SessionLocal() as session:
|
||||
wf = Workflow(name=f"列表测试·周报-{uuid.uuid4().hex[:6]}", graph={})
|
||||
session.add(wf)
|
||||
await session.flush()
|
||||
spec = [
|
||||
("succeeded", "formal"), ("failed", "exploratory"), ("cancelled", "exploratory"),
|
||||
("interrupted", "formal"), ("succeeded", "exploratory"), ("failed", "formal"),
|
||||
]
|
||||
runs = []
|
||||
for i, (status, run_class) in enumerate(spec):
|
||||
run = Run(workflow_id=wf.id, workflow_name=wf.name, status=status,
|
||||
run_class=run_class, graph={}, input={},
|
||||
created_at=base + timedelta(minutes=i))
|
||||
session.add(run)
|
||||
runs.append(run)
|
||||
await session.commit()
|
||||
ids = [r.id for r in runs]
|
||||
return wf.id, ids, wf.name # 按创建时间从旧到新
|
||||
|
||||
|
||||
async def test_status_filter_takes_a_comma_list(client, seeded_runs):
|
||||
wf_id, ids, _ = seeded_runs
|
||||
r = await client.get("/api/runs", params={"workflow_id": wf_id, "status": "failed,cancelled"})
|
||||
assert r.status_code == 200, r.text
|
||||
assert {x["status"] for x in r.json()} == {"failed", "cancelled"}
|
||||
assert len(r.json()) == 3
|
||||
|
||||
|
||||
async def test_run_class_and_name_filters(client, seeded_runs):
|
||||
wf_id, ids, name = seeded_runs
|
||||
formal = (await client.get("/api/runs", params={"workflow_id": wf_id, "run_class": "formal"})).json()
|
||||
assert len(formal) == 3 and all(x["run_class"] == "formal" for x in formal)
|
||||
|
||||
by_name = (await client.get("/api/runs", params={"q": name[3:]})).json()
|
||||
assert {x["id"] for x in by_name} == set(ids), "按工作流名模糊匹配"
|
||||
# 用户输入的 % 和 _ 按字面算,不当通配符
|
||||
assert (await client.get("/api/runs", params={"q": "列表%周报"})).json() == []
|
||||
|
||||
|
||||
async def test_newest_first_and_before_is_a_cursor(client, seeded_runs):
|
||||
wf_id, ids, _ = seeded_runs
|
||||
page1 = (await client.get("/api/runs", params={"workflow_id": wf_id, "limit": 2})).json()
|
||||
assert [x["id"] for x in page1] == [ids[5], ids[4]]
|
||||
cursor = page1[-1]["created_at"]
|
||||
page2 = (await client.get("/api/runs", params={
|
||||
"workflow_id": wf_id, "limit": 2, "before": cursor})).json()
|
||||
assert [x["id"] for x in page2] == [ids[3], ids[2]]
|
||||
|
||||
|
||||
async def test_limit_is_capped(client):
|
||||
assert (await client.get("/api/runs", params={"limit": 201})).status_code == 422
|
||||
assert (await client.get("/api/runs", params={"limit": 200})).status_code == 200
|
||||
|
||||
|
||||
async def test_run_out_has_the_new_fields(client, seeded_runs):
|
||||
_, ids, _ = seeded_runs
|
||||
body = (await client.get(f"/api/runs/{ids[0]}")).json()
|
||||
for key in ("manifest_seq", "finished_at", "error_node_id"):
|
||||
assert key in body, key
|
||||
|
||||
|
||||
async def test_graph_snapshot_endpoint(client):
|
||||
r = await client.post("/api/runs", json={"graph": GATED, "input": {"question": "q"}})
|
||||
run_id = r.json()["id"]
|
||||
got = await client.get(f"/api/runs/{run_id}/graph")
|
||||
assert got.status_code == 200, got.text
|
||||
body = got.json()
|
||||
assert set(body) == {"graph", "workflow_id", "version"}
|
||||
assert [n["id"] for n in body["graph"]["nodes"]] == ["start", "gate", "out"]
|
||||
assert (await client.get("/api/runs/nope/graph")).status_code == 404
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 审批
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_pending_approvals_carry_their_context_newest_first(client):
|
||||
wf = None
|
||||
async with SessionLocal() as session:
|
||||
wf = Workflow(name="审批测试·公告", graph=GATED)
|
||||
session.add(wf)
|
||||
await session.commit()
|
||||
first = (await client.post("/api/runs", json={
|
||||
"workflow_id": wf.id, "input": {"question": "一"}})).json()["id"]
|
||||
await _wait(first, ("interrupted",))
|
||||
second = (await client.post("/api/runs", json={
|
||||
"workflow_id": wf.id, "input": {"question": "二"}})).json()["id"]
|
||||
await _wait(second, ("interrupted",))
|
||||
|
||||
items = (await client.get("/api/approvals", params={"status": "pending"})).json()
|
||||
mine = [a for a in items if a["run_id"] in (first, second)]
|
||||
assert [a["run_id"] for a in mine] == [second, first], "按发起时间倒序"
|
||||
a = mine[0]
|
||||
assert a["workflow_name"] == "审批测试·公告"
|
||||
assert a["node_label"] == "发布前把关"
|
||||
assert a["run_status"] == "interrupted"
|
||||
assert a["run_class"] == "exploratory"
|
||||
assert a["resolved_by"] is None and a["resolved_at"] is None
|
||||
|
||||
|
||||
async def test_resolution_records_who_signed_and_when(client):
|
||||
run_id = (await client.post("/api/runs", json={
|
||||
"graph": GATED, "input": {"question": "q"}})).json()["id"]
|
||||
await _wait(run_id, ("interrupted",))
|
||||
[pending] = (await client.get("/api/approvals", params={"run_id": run_id})).json()
|
||||
|
||||
# 浏览器的请求头只能是 Latin-1,中文署名按 encodeURIComponent 编码后发
|
||||
r = await client.post(f"/api/approvals/{pending['id']}/decide",
|
||||
json={"approved": True}, headers={"X-Actor": quote("张工")})
|
||||
assert r.status_code == 200, r.text
|
||||
await _wait(run_id, ("succeeded",))
|
||||
|
||||
[done] = (await client.get("/api/approvals",
|
||||
params={"run_id": run_id, "status": "all"})).json()
|
||||
assert done["resolved_by"] == "张工"
|
||||
_aware(done["resolved_at"])
|
||||
|
||||
events = (await client.get(f"/api/runs/{run_id}/events")).json()
|
||||
resolved = next(e for e in events if e["type"] == "human.resolved")
|
||||
assert resolved["data"].get("actor") == "张工"
|
||||
resumed = next(e for e in events if e["type"] == "run.resumed")
|
||||
assert resumed["data"].get("actor") == "张工"
|
||||
|
||||
|
||||
async def test_unsigned_resolution_stays_null(client):
|
||||
run_id = (await client.post("/api/runs", json={
|
||||
"graph": GATED, "input": {"question": "q"}})).json()["id"]
|
||||
await _wait(run_id, ("interrupted",))
|
||||
[pending] = (await client.get("/api/approvals", params={"run_id": run_id})).json()
|
||||
await client.post(f"/api/approvals/{pending['id']}/decide", json={"approved": False})
|
||||
await _wait(run_id, ("succeeded", "failed"))
|
||||
[done] = (await client.get("/api/approvals",
|
||||
params={"run_id": run_id, "status": "all"})).json()
|
||||
assert done["resolved_by"] is None
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 删除保护
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def _plain_run(**fields) -> str:
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name="删除测试", graph={}, input={}, **fields)
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
return run.id
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status", ["running", "queued"])
|
||||
async def test_a_live_run_cannot_be_deleted(client, status):
|
||||
run_id = await _plain_run(status=status)
|
||||
r = await client.delete(f"/api/runs/{run_id}")
|
||||
assert r.status_code == 409
|
||||
assert "停止" in r.json()["detail"]
|
||||
assert (await client.get(f"/api/runs/{run_id}")).status_code == 200
|
||||
|
||||
|
||||
async def test_a_sealed_formal_run_needs_force(client):
|
||||
run_id = await _plain_run(status="succeeded", run_class="formal",
|
||||
manifest_hash="ab" * 32, manifest_seq=12)
|
||||
r = await client.delete(f"/api/runs/{run_id}")
|
||||
assert r.status_code == 409
|
||||
detail = r.json()["detail"]
|
||||
assert "force" not in detail.lower() or "清单" in detail
|
||||
assert "清单" in detail and "无法" in detail, detail
|
||||
assert (await client.get(f"/api/runs/{run_id}")).status_code == 200
|
||||
|
||||
r = await client.delete(f"/api/runs/{run_id}", params={"force": "true"})
|
||||
assert r.status_code == 204
|
||||
assert (await client.get(f"/api/runs/{run_id}")).status_code == 404
|
||||
|
||||
|
||||
async def test_an_ordinary_finished_run_deletes_straight_away(client):
|
||||
run_id = await _plain_run(status="succeeded", run_class="exploratory", manifest_hash="cd" * 32)
|
||||
assert (await client.delete(f"/api/runs/{run_id}")).status_code == 204
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 事件流收尾
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
class _FakeSocket:
|
||||
"""够 stream_run 用的最小 WebSocket:记下发出去的,客户端不主动说话。"""
|
||||
|
||||
def __init__(self, after: int = 0) -> None:
|
||||
self.query_params = {"after": str(after)}
|
||||
self.sent: list[dict] = []
|
||||
self.closed = asyncio.Event()
|
||||
|
||||
async def accept(self) -> None:
|
||||
return None
|
||||
|
||||
async def send_json(self, data) -> None:
|
||||
if self.closed.is_set():
|
||||
raise RuntimeError("closed")
|
||||
self.sent.append(data)
|
||||
|
||||
async def receive_text(self) -> str:
|
||||
await self.closed.wait()
|
||||
raise WebSocketDisconnect()
|
||||
|
||||
async def close(self, code: int = 1000) -> None:
|
||||
self.closed.set()
|
||||
|
||||
|
||||
async def _stream(run_id: str, timeout: float = 5.0) -> list[dict]:
|
||||
from app.api.runs import stream_run
|
||||
|
||||
ws = _FakeSocket()
|
||||
await asyncio.wait_for(stream_run(ws, run_id), timeout)
|
||||
return ws.sent
|
||||
|
||||
|
||||
async def test_connecting_to_an_interrupted_run_ends_with_a_marker(client):
|
||||
run_id = (await client.post("/api/runs", json={
|
||||
"graph": GATED, "input": {"question": "q"}})).json()["id"]
|
||||
await _wait(run_id, ("interrupted",))
|
||||
sent = await _stream(run_id)
|
||||
assert sent[-1]["type"] == "stream.end"
|
||||
assert sent[-1]["data"] == {"status": "interrupted"}
|
||||
assert any(m.get("type") == "run.interrupted" for m in sent[:-1])
|
||||
|
||||
|
||||
async def test_connecting_to_a_finished_run_ends_with_a_marker(client):
|
||||
graph = chain(node("start", "input"), node("out", "output"))
|
||||
run_id = (await client.post("/api/runs", json={"graph": graph, "input": {}})).json()["id"]
|
||||
await _wait(run_id, ("succeeded",))
|
||||
sent = await _stream(run_id)
|
||||
assert sent[-1]["type"] == "stream.end"
|
||||
assert sent[-1]["data"] == {"status": "succeeded"}
|
||||
assert sent[-1]["status"] == "succeeded", "老客户端读的是顶层 status"
|
||||
|
||||
|
||||
async def test_a_live_stream_ends_with_a_marker_when_the_run_finishes(client):
|
||||
run_id = (await client.post("/api/runs", json={
|
||||
"graph": GATED, "input": {"question": "q"}})).json()["id"]
|
||||
await _wait(run_id, ("interrupted",))
|
||||
[pending] = (await client.get("/api/approvals", params={"run_id": run_id})).json()
|
||||
await client.post(f"/api/approvals/{pending['id']}/decide", json={"approved": True})
|
||||
|
||||
# 恢复之后连上去跟着它跑完:终态之后也要有结束标记,而不是连接悬着
|
||||
sent = await _stream(run_id)
|
||||
assert sent[-1]["type"] == "stream.end", [m.get("type") for m in sent[-3:]]
|
||||
assert sent[-1]["data"]["status"] == "succeeded"
|
||||
assert any(m.get("type") == "run.finished" for m in sent)
|
||||
|
||||
|
||||
async def test_a_missing_run_ends_the_stream_instead_of_hanging():
|
||||
sent = await _stream("no-such-run")
|
||||
assert sent[-1]["type"] == "stream.end"
|
||||
|
||||
|
||||
async def test_a_run_stranded_by_a_hard_kill_says_so_after_restart():
|
||||
"""进程被强杀时连 server_shutdown 日志都来不及发。重启扫描把它标成 interrupted 的
|
||||
同时补一条,回放这条运行的客户端才知道它是停在了半路,而不是还在跑。"""
|
||||
from app.core.bus import bus
|
||||
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name="强杀", status="running", graph={}, input={}, last_seq=3)
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
run_id = run.id
|
||||
await run_manager.shutdown()
|
||||
bus._seq.pop(run_id, None) # 新进程:内存里的序号是空的
|
||||
await run_manager.setup()
|
||||
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run_id)
|
||||
assert row.status == "interrupted"
|
||||
sent = await _stream(run_id)
|
||||
log = [m for m in sent if m.get("type") == "log"]
|
||||
assert [m["data"].get("code") for m in log] == ["server_shutdown"]
|
||||
assert log[0]["seq"] == 4, "序号要接着库里的 last_seq 往下排"
|
||||
assert sent[-1]["data"] == {"status": "interrupted"}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 接着跑:拒绝时说清楚为什么、下一步做什么
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_continuing_a_cancelled_run_says_start_again(client):
|
||||
run_id = await _plain_run(status="cancelled")
|
||||
r = await client.post(f"/api/runs/{run_id}/continue", json={})
|
||||
assert r.status_code == 409
|
||||
detail = r.json()["detail"]
|
||||
assert "已取消" in detail and "重新发起" in detail, detail
|
||||
assert "审批" not in detail, detail
|
||||
|
||||
|
||||
async def test_continuing_a_run_that_waits_for_approval_points_to_the_card(client):
|
||||
run_id = (await client.post("/api/runs", json={
|
||||
"graph": GATED, "input": {"question": "q"}})).json()["id"]
|
||||
await _wait(run_id, ("interrupted",))
|
||||
r = await client.post(f"/api/runs/{run_id}/continue", json={})
|
||||
assert r.status_code == 409
|
||||
assert "审批卡" in r.json()["detail"]
|
||||
assert (await client.get(f"/api/runs/{run_id}")).json()["status"] == "interrupted"
|
||||
|
||||
|
||||
async def test_a_run_suspended_by_a_restart_continues_from_its_checkpoint(client, monkeypatch):
|
||||
"""服务重启挂起的运行没有在等谁,断点完好。「接着跑」就是从断点驱动,
|
||||
和失败后接着跑是同一件事——以前这里回 409,说它「在等待审批」。"""
|
||||
original = compiler.RUNNERS[NodeType.TRANSFORM]
|
||||
calls = {"slow": 0}
|
||||
|
||||
async def slow_once(state, ctx):
|
||||
if ctx.node.id == "slow":
|
||||
calls["slow"] += 1
|
||||
if calls["slow"] == 1:
|
||||
await asyncio.sleep(30)
|
||||
return await original(state, ctx)
|
||||
|
||||
monkeypatch.setitem(compiler.RUNNERS, NodeType.TRANSFORM, slow_once)
|
||||
graph = chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("a", "transform", mode="template", template="前面这步"),
|
||||
node("slow", "transform", mode="template", template="慢的这步"),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.slow }}"}]),
|
||||
)
|
||||
run_id = (await client.post("/api/runs", json={
|
||||
"graph": graph, "input": {"question": "q"}})).json()["id"]
|
||||
for _ in range(300):
|
||||
if calls["slow"]:
|
||||
break
|
||||
await asyncio.sleep(0.03)
|
||||
await run_manager.shutdown()
|
||||
await run_manager.setup() # 重启
|
||||
assert (await client.get(f"/api/runs/{run_id}")).json()["status"] == "interrupted"
|
||||
|
||||
r = await client.post(f"/api/runs/{run_id}/continue", json={})
|
||||
assert r.status_code == 200, r.text
|
||||
row = await _wait(run_id, ("succeeded", "failed"))
|
||||
assert row.status == "succeeded", row.error
|
||||
assert row.output.get("结果") == "慢的这步"
|
||||
async with SessionLocal() as session:
|
||||
starts = [e.node_id for e in (await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run_id, RunEvent.type == "node.started")
|
||||
)).scalars()]
|
||||
assert starts.count("a") == 1, f"接着跑把前面跑完的节点又跑了一遍:{starts}"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("method,action", [("post", "resume"), ("post", "continue"),
|
||||
("get", "verify")])
|
||||
async def test_a_missing_run_is_reported_without_python_quotes(client, method, action):
|
||||
kwargs = {"json": {}} if method == "post" else {}
|
||||
r = await getattr(client, method)(f"/api/runs/nope/{action}", **kwargs)
|
||||
assert r.status_code == 404
|
||||
detail = r.json()["detail"]
|
||||
assert detail and not detail.startswith("'"), detail
|
||||
assert "nope" in detail or "不存在" in detail, detail
|
||||
@@ -0,0 +1,229 @@
|
||||
"""协作团队的实时性:谁先做完谁先报到,调度者在想什么也要有事件。
|
||||
|
||||
以前成员的 agent.step.end 要等整轮 gather 结束才按派活顺序统一发出——并行时先做完
|
||||
的成员一直显示"进行中",直到最慢的那个做完;矩阵声称能回答的"谁还在跑"其实答
|
||||
不了。调度者那次结构化模型调用前后没有任何事件,一个 74 秒的团队节点里约 66 秒
|
||||
是调度者在决策,界面上是个黑盒。
|
||||
|
||||
用量也一样:agent 节点、成员、调度者的模型调用都不发 llm.end,界面只能等终态
|
||||
才知道花了多少 token。补上之后必须守住另一头:run.finished 的 usage 是后端累计
|
||||
的权威值,事件只是把同一笔账逐次摊开,加起来要对得上,不能记两遍。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk
|
||||
from langchain_core.outputs import ChatGenerationChunk
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Run, RunEvent
|
||||
from app.engine.runner import run_manager
|
||||
|
||||
SLOW, FAST = 0.6, 0.05
|
||||
ROUTE_SECONDS = 0.15
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def engine_up():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
def _team_graph() -> dict:
|
||||
agents = [
|
||||
{"name": n, "description": f"{n} 的职责", "system": f"你是 {n}", "max_steps": 1}
|
||||
for n in ("researcher", "analyst")
|
||||
]
|
||||
return {
|
||||
"nodes": [
|
||||
{"id": "in", "type": "input", "data": {"config": {}}},
|
||||
{"id": "team", "type": "supervisor", "data": {"label": "协作团队", "config": {
|
||||
"goal": "比较两种向量库", "agents": agents, "max_rounds": 3, "max_parallel": 2,
|
||||
}}},
|
||||
{"id": "out", "type": "output", "data": {"config": {}}},
|
||||
],
|
||||
"edges": [{"source": "in", "target": "team"}, {"source": "team", "target": "out"}],
|
||||
}
|
||||
|
||||
|
||||
def _usage(messages, reply: str) -> dict:
|
||||
text = " ".join(str(getattr(m, "content", m)) for m in messages)
|
||||
return {"input_tokens": 10 + len(text) // 50, "output_tokens": 3 + len(reply) // 20,
|
||||
"total_tokens": 0}
|
||||
|
||||
|
||||
def _script(monkeypatch, decisions: list[dict]) -> None:
|
||||
"""调度者按剧本派活;成员 researcher 慢、analyst 快;每次调用都报 token。
|
||||
|
||||
调度者走 with_structured_output(include_raw=True):真实供应商会连原始消息
|
||||
一起给回来,用量就在那条消息上。mock 默认只给解析结果,这里补齐这层行为。
|
||||
"""
|
||||
from app.providers import mock_model
|
||||
|
||||
turns = {"n": 0}
|
||||
|
||||
def _decide(self, messages):
|
||||
if self.response_format:
|
||||
i = turns["n"]
|
||||
turns["n"] += 1
|
||||
payload = decisions[i] if i < len(decisions) else {"assignments": [], "done": True}
|
||||
return AIMessage(content=json.dumps(payload, ensure_ascii=False))
|
||||
return AIMessage(content="(干完了)")
|
||||
|
||||
async def _agenerate(self, messages, stop=None, run_manager=None, **kw):
|
||||
text = " ".join(str(getattr(m, "content", "")) for m in messages)
|
||||
if self.response_format:
|
||||
await asyncio.sleep(ROUTE_SECONDS)
|
||||
else:
|
||||
await asyncio.sleep(FAST if "快活" in text else SLOW)
|
||||
result = self._generate(messages, stop, **kw)
|
||||
msg = result.generations[0].message
|
||||
msg.usage_metadata = _usage(messages, str(msg.content))
|
||||
return result
|
||||
|
||||
def _structured(self, schema, *, include_raw=False, **kw):
|
||||
bound = self.model_copy(update={"response_format": schema})
|
||||
|
||||
async def parse(msg):
|
||||
parsed = json.loads(str(msg.content))
|
||||
return {"raw": msg, "parsed": parsed, "parsing_error": None} if include_raw else parsed
|
||||
|
||||
return bound | parse
|
||||
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "_decide", _decide)
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "_agenerate", _agenerate)
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "with_structured_output", _structured)
|
||||
|
||||
|
||||
async def _run(graph: dict) -> tuple[list[RunEvent], Run]:
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": "比较"})
|
||||
for _ in range(400):
|
||||
await asyncio.sleep(0.05)
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run.id)
|
||||
if row.status in ("succeeded", "failed", "cancelled"):
|
||||
break
|
||||
else:
|
||||
pytest.fail("运行没有在 20 秒内结束")
|
||||
async with SessionLocal() as session:
|
||||
events = list((await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run.id).order_by(RunEvent.seq)
|
||||
)).scalars())
|
||||
return events, row
|
||||
|
||||
|
||||
PARALLEL_ROUND = [{
|
||||
"assignments": [
|
||||
{"agent": "researcher", "instruction": "慢活"},
|
||||
{"agent": "analyst", "instruction": "快活"},
|
||||
],
|
||||
"reason": "两边互不相干",
|
||||
}, {"assignments": [], "done": True, "reason": "够了"}]
|
||||
|
||||
|
||||
async def test_members_report_done_in_the_order_they_actually_finish(monkeypatch):
|
||||
_script(monkeypatch, PARALLEL_ROUND)
|
||||
events, run = await _run(_team_graph())
|
||||
assert run.status == "succeeded", run.error
|
||||
|
||||
ends = [e for e in events if e.type == "agent.step.end"]
|
||||
assert [e.data["agent"] for e in ends] == ["analyst", "researcher"], \
|
||||
"快的那个先做完,就该先报到——而不是按派活顺序等整轮结束再一起报"
|
||||
|
||||
start = next(e for e in events if e.type == "agent.step.start")
|
||||
fast_end = ends[0]
|
||||
# 快的那个结束事件的时刻要贴着它自己做完的时刻,而不是被慢的那个拖到整轮结束
|
||||
assert fast_end.ts - start.ts < (SLOW + FAST) / 2, \
|
||||
f"analyst {FAST}s 就做完了,结束事件却在 {fast_end.ts - start.ts:.2f}s 才出现"
|
||||
# 字段不变:界面照旧按这些字段画矩阵
|
||||
for e in ends:
|
||||
assert {"agent", "duration_ms", "round", "parallel", "preview"} <= set(e.data)
|
||||
|
||||
|
||||
async def test_the_coordinator_is_not_a_black_box(monkeypatch):
|
||||
_script(monkeypatch, PARALLEL_ROUND)
|
||||
events, run = await _run(_team_graph())
|
||||
assert run.status == "succeeded", run.error
|
||||
|
||||
starts = [e for e in events if e.type == "agent.route.start"]
|
||||
ends = [e for e in events if e.type == "agent.route.end"]
|
||||
assert [e.data["round"] for e in starts] == [0, 1]
|
||||
assert [e.data["round"] for e in ends] == [0, 1]
|
||||
assert all(e.node_id == "team" for e in starts + ends)
|
||||
|
||||
first, last = ends
|
||||
assert first.data["agents"] == ["researcher", "analyst"]
|
||||
assert first.data["parallel"] == 2
|
||||
assert first.data["done"] is False
|
||||
assert first.data["reason"] == "两边互不相干"
|
||||
assert last.data["done"] is True and last.data["agents"] == []
|
||||
assert last.data["reason"] == "够了"
|
||||
for e in ends:
|
||||
assert e.data["duration_ms"] >= ROUTE_SECONDS * 1000 * 0.8, e.data
|
||||
|
||||
# 先"开始想",再"想好了",然后才派人出去
|
||||
seq = {e.type + str(e.data.get("round", "")): e.seq for e in events}
|
||||
first_dispatch = next(e.seq for e in events if e.type == "agent.step.start")
|
||||
assert seq["agent.route.start0"] < seq["agent.route.end0"] < first_dispatch
|
||||
|
||||
|
||||
async def test_every_model_call_reports_usage_and_the_sum_matches_the_run(monkeypatch):
|
||||
_script(monkeypatch, PARALLEL_ROUND)
|
||||
events, run = await _run(_team_graph())
|
||||
assert run.status == "succeeded", run.error
|
||||
|
||||
calls = [e for e in events if e.type == "llm.end" and e.node_id == "team"]
|
||||
by_agent: dict[str, list[dict]] = {}
|
||||
for e in calls:
|
||||
by_agent.setdefault(e.data["agent"], []).append(e.data)
|
||||
assert set(by_agent) == {"调度者", "researcher", "analyst"}, by_agent.keys()
|
||||
assert len(by_agent["调度者"]) == 2, "调度了两次,就是两次模型调用"
|
||||
for d in (d for items in by_agent.values() for d in items):
|
||||
assert {"model", "input_tokens", "output_tokens", "cost_usd"} <= set(d), d
|
||||
assert d["input_tokens"] > 0 and d["output_tokens"] > 0, d
|
||||
|
||||
finished = next(e for e in events if e.type == "run.finished")
|
||||
usage = finished.data["usage"]
|
||||
# 权威值是后端累计;逐次事件加起来必须等于它——多一笔少一笔都说明记重或漏记了
|
||||
assert usage["input_tokens"] == sum(e.data["input_tokens"] for e in calls)
|
||||
assert usage["output_tokens"] == sum(e.data["output_tokens"] for e in calls)
|
||||
assert usage["calls"] == len(calls)
|
||||
assert run.usage["input_tokens"] == usage["input_tokens"]
|
||||
|
||||
|
||||
async def test_agent_node_reports_each_model_call(monkeypatch):
|
||||
"""agent 节点以前只有 llm.start 没有 llm.end:用量要等整个节点结束才知道。"""
|
||||
from app.providers import mock_model
|
||||
|
||||
async def _astream(self, messages, stop=None, run_manager=None, **kw):
|
||||
reply = "查完了,结论如下。"
|
||||
await asyncio.sleep(0.01)
|
||||
yield ChatGenerationChunk(message=AIMessageChunk(
|
||||
content=reply, usage_metadata=_usage(messages, reply)))
|
||||
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "_astream", _astream)
|
||||
graph = {
|
||||
"nodes": [
|
||||
{"id": "in", "type": "input", "data": {"config": {}}},
|
||||
{"id": "ag", "type": "agent", "data": {"label": "分析员", "config": {
|
||||
"prompt": "{{ input.question }}", "tools": [], "max_steps": 3}}},
|
||||
{"id": "out", "type": "output", "data": {"config": {}}},
|
||||
],
|
||||
"edges": [{"source": "in", "target": "ag"}, {"source": "ag", "target": "out"}],
|
||||
}
|
||||
events, run = await _run(graph)
|
||||
assert run.status == "succeeded", run.error
|
||||
|
||||
starts = [e for e in events if e.type == "llm.start" and e.node_id == "ag"]
|
||||
ends = [e for e in events if e.type == "llm.end" and e.node_id == "ag"]
|
||||
assert len(ends) == len(starts) >= 1
|
||||
assert all(e.data["agent"] == "分析员" for e in ends)
|
||||
usage = next(e for e in events if e.type == "run.finished").data["usage"]
|
||||
assert usage["input_tokens"] == sum(e.data["input_tokens"] for e in ends) > 0
|
||||
assert usage["output_tokens"] == sum(e.data["output_tokens"] for e in ends) > 0
|
||||
@@ -0,0 +1,208 @@
|
||||
"""校验和报错写给人看:用界面上的叫法,说清原因和下一步,不露 Python 内部名字。
|
||||
|
||||
- 分支 case 的 key 用了保留名 default:和兜底出口撞成同一个 id,跑完两条一起点亮,
|
||||
看不出实际走了哪条。重复 key、空 key 更糟——那个 case 根本路由不到。
|
||||
- 门禁文案写「受管模板不允许 supervisor 节点」「approval=never」「该是个 llm 节点」,
|
||||
界面上它们叫「多 Agent 协作」「全部自动放行」「模型调用」,报错引用了用户从没见过的词。
|
||||
- 运行失败的首行写着「工具 db_query__shop 执行失败:_make_query_tool.<locals>._run()
|
||||
got an unexpected keyword argument 'query'」,恢复失败写「当前状态是 succeeded」。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Run, RunEvent
|
||||
from app.engine import compiler
|
||||
from app.engine.governance import lint_for_publish
|
||||
from app.engine.runner import run_manager
|
||||
from app.engine.schema import GraphSpec, NodeType, validate_graph
|
||||
|
||||
|
||||
def node(nid, ntype, label=None, **config):
|
||||
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0},
|
||||
"data": {"label": label or nid, "config": config}}
|
||||
|
||||
|
||||
def _branch(cases: list[dict]) -> GraphSpec:
|
||||
return GraphSpec.model_validate({
|
||||
"nodes": [
|
||||
node("start", "input"),
|
||||
node("br", "branch", label="走哪条", mode="expression", cases=cases),
|
||||
node("a", "output"), node("b", "output"),
|
||||
],
|
||||
"edges": [
|
||||
{"source": "start", "target": "br"},
|
||||
{"source": "br", "target": "a", "sourceHandle": "fast"},
|
||||
{"source": "br", "target": "b", "sourceHandle": "default"},
|
||||
],
|
||||
})
|
||||
|
||||
|
||||
def _issues(spec: GraphSpec, level: str) -> list[str]:
|
||||
return [i.message for i in validate_graph(spec).issues if i.level == level and i.node_id == "br"]
|
||||
|
||||
|
||||
def test_case_key_default_is_flagged_as_the_reserved_fallback():
|
||||
spec = _branch([{"key": "fast", "condition": "true"},
|
||||
{"key": "default", "condition": "true", "label": "协作模式"}])
|
||||
warnings = _issues(spec, "warning")
|
||||
assert any("default" in m and "保留" in m for m in warnings), warnings
|
||||
# 现存的 ⑥ 模板就是这么写的:条件为 true 的 default 等价于兜底,运行语义自洽。
|
||||
# 升成 error 会让它和所有从它复制出来的图直接不能跑
|
||||
assert validate_graph(spec).ok
|
||||
|
||||
|
||||
def test_duplicate_case_keys_are_an_error():
|
||||
spec = _branch([{"key": "fast", "condition": "vars.a"}, {"key": "fast", "condition": "vars.b"}])
|
||||
errors = _issues(spec, "error")
|
||||
assert any("fast" in m and "重复" in m for m in errors), errors
|
||||
assert not validate_graph(spec).ok
|
||||
|
||||
|
||||
def test_blank_case_key_is_an_error():
|
||||
spec = _branch([{"key": "fast", "condition": "vars.a"}, {"key": " ", "condition": "vars.b"}])
|
||||
assert any("标识" in m for m in _issues(spec, "error")), _issues(spec, "error")
|
||||
|
||||
|
||||
def test_validation_uses_the_names_on_the_canvas():
|
||||
spec = GraphSpec.model_validate({
|
||||
"nodes": [node("start", "input"), node("sub", "subgraph"), node("t", "tool"),
|
||||
node("v", "validate"), node("out", "output")],
|
||||
"edges": [{"source": "start", "target": "sub"}, {"source": "sub", "target": "t"},
|
||||
{"source": "t", "target": "v"}, {"source": "v", "target": "out"}],
|
||||
})
|
||||
messages = " ".join(i.message for i in validate_graph(spec).issues)
|
||||
assert "子工作流" in messages and "子图节点" not in messages
|
||||
assert "「调用工具」" in messages
|
||||
assert "结构校验" in messages
|
||||
|
||||
|
||||
def _governed(*nodes) -> str:
|
||||
spec = GraphSpec.model_validate({
|
||||
"nodes": [node("start", "input"), *nodes],
|
||||
"edges": [{"source": "start", "target": n["id"]} for n in nodes],
|
||||
})
|
||||
return " ".join(i.message for i in lint_for_publish(spec, level="governed").issues)
|
||||
|
||||
|
||||
def test_governance_speaks_the_ui_vocabulary():
|
||||
text = _governed(
|
||||
node("team", "supervisor", label="协作团队"),
|
||||
node("ag", "agent", label="取数员", tools=["web_search"], approval="never"),
|
||||
node("bare", "agent", label="空手的"),
|
||||
node("c", "code", label="算一下", network=True),
|
||||
)
|
||||
for internal in ("supervisor", "approval=never", "llm 节点", "contract"):
|
||||
assert internal not in text, f"门禁文案里还有内部词 {internal!r}:{text}"
|
||||
assert "「协作团队」" in text and "多 Agent 协作" in text
|
||||
assert "审批策略" in text and "全部自动放行" in text
|
||||
assert "模型调用" in text
|
||||
assert "出具契约" in text
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 运行期报错
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def engine_up():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
async def _finish(run_id: str) -> Run:
|
||||
for _ in range(300):
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run_id)
|
||||
if row.status in ("succeeded", "failed", "cancelled", "interrupted"):
|
||||
return row
|
||||
await asyncio.sleep(0.03)
|
||||
raise AssertionError("等超时了")
|
||||
|
||||
|
||||
RAW = "_make_query_tool.<locals>._run() got an unexpected keyword argument 'query'"
|
||||
|
||||
|
||||
async def test_a_python_error_reads_like_a_sentence(engine_up, monkeypatch):
|
||||
async def broken(state, ctx):
|
||||
raise TypeError(RAW)
|
||||
|
||||
monkeypatch.setitem(compiler.RUNNERS, NodeType.TRANSFORM, broken)
|
||||
graph = {
|
||||
"nodes": [node("start", "input"), node("shape", "transform", label="整理数据",
|
||||
mode="template", template="x")],
|
||||
"edges": [{"source": "start", "target": "shape"}],
|
||||
}
|
||||
run = await run_manager.start(graph=graph, input_payload={})
|
||||
row = await _finish(run.id)
|
||||
assert row.status == "failed"
|
||||
for text in (row.error or "",):
|
||||
assert "TypeError" not in text and "<locals>" not in text, text
|
||||
assert "query" in text, "原因要说到点子上:哪个参数对不上"
|
||||
assert row.error_node_id == "shape"
|
||||
|
||||
async with SessionLocal() as session:
|
||||
events = list((await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run.id).order_by(RunEvent.seq)
|
||||
)).scalars())
|
||||
failed_node = next(e for e in events if e.type == "node.failed")
|
||||
assert "TypeError" not in failed_node.data["error"]
|
||||
assert "TypeError" in failed_node.data["detail"], "原始异常要留着,给排查的人看"
|
||||
failed_run = next(e for e in events if e.type == "run.failed")
|
||||
assert failed_run.data["node_id"] == "shape"
|
||||
assert failed_run.data["label"] == "整理数据"
|
||||
assert "TypeError" not in failed_run.data["error"]
|
||||
|
||||
|
||||
async def test_resume_refusal_does_not_leak_the_status_enum(engine_up):
|
||||
async with SessionLocal() as session:
|
||||
run = Run(workflow_name="x", status="succeeded", graph={"nodes": [], "edges": []}, input={})
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
with pytest.raises(ValueError) as caught:
|
||||
await run_manager.resume(run.id, {"approved": True})
|
||||
assert "succeeded" not in str(caught.value)
|
||||
assert "已完成" in str(caught.value)
|
||||
|
||||
|
||||
def test_exception_wording_helper():
|
||||
from app.engine.errors import describe_exception
|
||||
|
||||
assert "query" in describe_exception(TypeError(RAW))
|
||||
assert "TypeError" not in describe_exception(TypeError(RAW))
|
||||
timeout = describe_exception(TimeoutError())
|
||||
assert timeout and "Timeout" not in timeout
|
||||
generic = describe_exception(RuntimeError("Integer exceeds 64-bit range"))
|
||||
assert "RuntimeError" not in generic and "64-bit" in generic
|
||||
nested = describe_exception(RuntimeError("连接失败:ConnectError: All connection attempts failed"))
|
||||
assert "Error" not in nested and "All connection attempts failed" in nested
|
||||
|
||||
|
||||
async def test_issuance_event_says_why_it_was_downgraded(engine_up):
|
||||
"""只有 gaps 时判成降档,事件里却不带 gaps:时间线上只剩一个光秃秃的「降档出具」,
|
||||
横幅还说「请对照下方声明」,下方什么都没有。"""
|
||||
graph = {
|
||||
"nodes": [
|
||||
node("start", "input"),
|
||||
node("out", "output", fields=[{"name": "结论", "value": "本周 12 单"}],
|
||||
contract={"metrics_from": "没有这个口径卡", "narrative": "{{ output.结论 }}"}),
|
||||
],
|
||||
"edges": [{"source": "start", "target": "out"}],
|
||||
}
|
||||
run = await run_manager.start(graph=graph, input_payload={})
|
||||
row = await _finish(run.id)
|
||||
assert row.status == "succeeded", row.error
|
||||
async with SessionLocal() as session:
|
||||
issuance = (await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run.id, RunEvent.type == "issuance")
|
||||
)).scalars().one()
|
||||
assert issuance.data["tier"] == "degraded"
|
||||
assert any("没有这个口径卡" in g for g in issuance.data["gaps"]), issuance.data
|
||||
assert issuance.data["metrics_checked"] == 0
|
||||
@@ -0,0 +1,75 @@
|
||||
"""删除工作流时的两道口子:还在跑的运行、以及中文署名。
|
||||
|
||||
- 运行记录的 workflow_id 是 ON DELETE SET NULL:删掉工作流,运行记录本身留着,
|
||||
封存核对用的是运行自带的图快照,不受影响。但还在跑的那次运行会失去归属,
|
||||
界面上再也回不到它的画布——和删除运行记录一样,先停再删。
|
||||
- 发布署名从 X-Actor 请求头来。浏览器请求头只能是 Latin-1,前端按
|
||||
encodeURIComponent 编码后发,发布时要解回来,否则中文署名存成 %E5...。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from urllib.parse import quote
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Run, Workflow
|
||||
from app.main import app
|
||||
|
||||
GRAPH = {
|
||||
"nodes": [
|
||||
{"id": "start", "type": "input", "position": {"x": 0, "y": 0},
|
||||
"data": {"label": "输入", "config": {"fields": [{"name": "q"}]}}},
|
||||
{"id": "out", "type": "output", "position": {"x": 300, "y": 0},
|
||||
"data": {"label": "成果", "config": {"fields": [{"name": "r", "value": "{{ input.q }}"}]}}},
|
||||
],
|
||||
"edges": [{"source": "start", "target": "out"}],
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
async def _workflow_with_run(status: str) -> tuple[str, str]:
|
||||
async with SessionLocal() as session:
|
||||
workflow = Workflow(name="删除保护", graph=GRAPH)
|
||||
session.add(workflow)
|
||||
await session.flush()
|
||||
run = Run(workflow_id=workflow.id, workflow_name=workflow.name, graph=GRAPH,
|
||||
input={}, status=status)
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
return workflow.id, run.id
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status", ["running", "queued"])
|
||||
async def test_a_workflow_with_a_live_run_cannot_be_deleted(client, status):
|
||||
workflow_id, _ = await _workflow_with_run(status)
|
||||
r = await client.delete(f"/api/workflows/{workflow_id}")
|
||||
assert r.status_code == 409
|
||||
assert "停止" in r.json()["detail"]
|
||||
assert (await client.get(f"/api/workflows/{workflow_id}")).status_code == 200
|
||||
|
||||
|
||||
async def test_finished_runs_survive_their_workflow(client):
|
||||
workflow_id, run_id = await _workflow_with_run("succeeded")
|
||||
assert (await client.delete(f"/api/workflows/{workflow_id}")).status_code == 204
|
||||
r = await client.get(f"/api/runs/{run_id}")
|
||||
assert r.status_code == 200
|
||||
assert r.json()["workflow_id"] is None
|
||||
|
||||
|
||||
async def test_a_chinese_publisher_name_is_stored_decoded(client):
|
||||
r = await client.post("/api/workflows", json={"name": "署名", "graph": GRAPH})
|
||||
workflow_id = r.json()["id"]
|
||||
r = await client.post(f"/api/workflows/{workflow_id}/publish",
|
||||
json={"level": "published"}, headers={"X-Actor": quote("张工")})
|
||||
assert r.status_code == 200, r.text
|
||||
assert r.json()["published_by"] == "张工"
|
||||
assert (await client.get(f"/api/workflows/{workflow_id}")).json()["published_by"] == "张工"
|
||||
Reference in New Issue
Block a user