From acdc9677a83a6930aa5f0e26f3d6d17493a3589c Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Wed, 14 Oct 2026 20:05:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=B9=B3=E5=8F=B0=20BE-2=20?= =?UTF-8?q?=E6=B5=81=E7=A8=8B=EF=BC=9A=E5=BE=85=E5=8A=9E=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=EF=BC=88=E6=B4=BE=E7=BB=99=E8=B0=81=E3=80=81any/all/quorum=20?= =?UTF-8?q?=E6=B1=87=E6=80=BB=E3=80=81=E6=9D=A1=E4=BB=B6=20UPDATE=20?= =?UTF-8?q?=E6=8A=A2=E6=81=A2=E5=A4=8D=E3=80=81=E6=8F=90=E9=86=92=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E8=B6=85=E6=97=B6=E9=A9=B3=E5=9B=9E=E3=80=81=E5=AE=A1?= =?UTF-8?q?=E6=89=B9=E6=8E=A5=E5=8F=A3=E6=94=B9=E4=B8=BA=E5=9E=AB=E7=89=87?= =?UTF-8?q?=E3=80=81=E5=AD=98=E9=87=8F=E8=A1=A5=E5=BB=BA=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E4=BA=BA=E5=B7=A5=E8=8A=82=E7=82=B9=E6=96=B0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=8E=E6=A0=A1=E9=AA=8C=EF=BC=8C=E5=86=99=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=9A=84=E5=8A=A8=E4=BD=9C=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=EF=BC=88=E7=94=9F=E6=95=88=E8=AE=B0=E5=BD=95=E5=85=88=E5=86=99?= =?UTF-8?q?=E5=90=8E=E5=8F=91=E3=80=81=E7=A1=AE=E8=AE=A4=E5=8D=A1=E5=93=88?= =?UTF-8?q?=E5=B8=8C=E3=80=81=E7=BB=93=E6=9E=9C=E6=9C=AA=E7=9F=A5=E6=8C=82?= =?UTF-8?q?=E8=B5=B7=E4=B8=8E=E6=A0=B8=E5=AE=9E=E3=80=81=E6=B0=B8=E4=B8=8D?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=87=8D=E5=8F=91=EF=BC=89=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E6=94=BE=E8=B1=81=E5=85=8D=E4=B8=8E=E5=8D=87=E7=BA=A7=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E6=B5=8B=E8=AF=95=EF=BC=8C=E6=AD=A3=E5=BC=8F=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E4=B8=8D=E8=AE=B8=E5=B8=A6=E5=9B=BE=E6=8E=A5=E7=9D=80?= =?UTF-8?q?=E8=B7=91=E3=80=81=E6=8E=A5=E7=9D=80=E8=B7=91=E4=B8=8D=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E5=8F=91=E8=B5=B7=E4=BA=BA=EF=BC=88F2=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E8=BF=90=E8=A1=8C=E6=9D=A5=E6=BA=90=EF=BC=8C=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E5=8F=91=E9=80=81=E6=A0=B8=E5=BF=83=E4=B8=8E=E6=B8=A0?= =?UTF-8?q?=E9=81=93=EF=BC=8C=E5=85=A5=E7=AB=99=E7=AD=BE=E5=90=8D=E9=92=A9?= =?UTF-8?q?=E5=AD=90=EF=BC=8Cprocess.*=20=E6=A3=80=E6=9F=A5=EF=BC=8C?= =?UTF-8?q?=E6=8A=A5=E9=94=80=E5=AE=A1=E6=89=B9=E6=A8=A1=E6=9D=BF=EF=BC=8C?= =?UTF-8?q?=E5=81=87=E5=A4=96=E9=83=A8=E7=B3=BB=E7=BB=9F=EF=BC=88=E5=90=AB?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=A4=8D=E6=A0=B8=E5=90=8E=E7=9A=84=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/actions.py | 44 +- backend/app/api/hooks.py | 29 +- backend/app/api/notify.py | 227 +++++- backend/app/api/runs.py | 105 ++- backend/app/api/tasks.py | 123 ++- backend/app/engine/compiler.py | 5 + backend/app/engine/lint_process.py | 255 +++++++ backend/app/engine/nodes/human.py | 83 ++ backend/app/engine/nodes/tools.py | 99 ++- backend/app/engine/replay.py | 9 +- backend/app/engine/runner.py | 108 ++- backend/app/engine/schema.py | 12 + backend/app/notify/__init__.py | 33 +- backend/app/notify/core.py | 381 ++++++++++ backend/app/notify/service.py | 256 +++++++ backend/app/process/actions.py | 538 ++++++++++++- backend/app/process/events.py | 35 + backend/app/process/hooks.py | 277 ++++++- backend/app/process/tasks.py | 877 +++++++++++++++++++++- backend/app/scheduler/loop.py | 16 +- backend/app/scheduler/service.py | 21 +- backend/app/scheduler/webhook.py | 365 ++------- backend/app/seed.py | 23 + backend/tests/test_challenge_node.py | 11 +- backend/tests/test_lint_process.py | 120 +++ backend/tests/test_notify_channels.py | 190 +++++ backend/tests/test_process_actions.py | 317 ++++++++ backend/tests/test_process_hooks.py | 256 +++++++ backend/tests/test_process_tasks.py | 486 ++++++++++++ backend/tests/test_replay_side_effects.py | 184 +++++ backend/tests/test_runs_api.py | 39 + backend/tests/test_runs_origin_f2.py | 118 +++ backend/tests/test_tool_deadline.py | 2 +- scripts/copy-allow.json | 4 +- scripts/fake-erp.py | 44 ++ 35 files changed, 5299 insertions(+), 393 deletions(-) create mode 100644 backend/app/engine/lint_process.py create mode 100644 backend/app/notify/core.py create mode 100644 backend/app/notify/service.py create mode 100644 backend/app/process/events.py create mode 100644 backend/tests/test_lint_process.py create mode 100644 backend/tests/test_notify_channels.py create mode 100644 backend/tests/test_process_actions.py create mode 100644 backend/tests/test_process_hooks.py create mode 100644 backend/tests/test_process_tasks.py create mode 100644 backend/tests/test_runs_origin_f2.py create mode 100644 scripts/fake-erp.py diff --git a/backend/app/api/actions.py b/backend/app/api/actions.py index 56116157..1ece0340 100644 --- a/backend/app/api/actions.py +++ b/backend/app/api/actions.py @@ -1,6 +1,46 @@ -"""生效记录(BE-2,只读;核实走待办)。设计文档 §3.8、§3.10。""" +"""生效记录(BE-2,只读;核实走待办)。设计文档 §3.8、§3.10。预览、回执摘要都是遮罩后的,原文在回执工件里。 + +开登录后 user 角色:列表不给(那是所有应用、所有运行的生效记录,记录页是给搭建者看的),使用者看动作状态经应用页的 +动作状态组件;单条只给它能读的运行(can_read_run)上的,核实待办的卡片要用。""" from __future__ import annotations -from fastapi import APIRouter +from typing import Any + +from fastapi import APIRouter, HTTPException, Query +from sqlalchemy import select + +from app.api.deps import can_read_run, current_principal, ensure_role, restricted +from app.db.base import SessionLocal +from app.db.models import ActionRecord +from app.process.actions import describe, row_out router = APIRouter(prefix="/api/actions", tags=["actions"]) + + +@router.get("") +async def list_actions( + run_id: str | None = None, + app_id: str | None = None, + state: str | None = Query(default=None, description="逗号分隔,如 unknown,failed"), + limit: int = Query(default=100, ge=1, le=500), +) -> dict[str, Any]: + ensure_role("builder") + stmt = select(ActionRecord).order_by(ActionRecord.created_at.desc()).limit(limit) + if run_id: + stmt = stmt.where(ActionRecord.run_id == run_id) + if app_id: + stmt = stmt.where(ActionRecord.app_id == app_id) + if states := [s.strip() for s in (state or "").split(",") if s.strip()]: + stmt = stmt.where(ActionRecord.state.in_(states)) + async with SessionLocal() as session: + return {"items": [row_out(r) for r in (await session.execute(stmt)).scalars()]} + + +@router.get("/{action_id}") +async def get_action(action_id: str) -> dict[str, Any]: + out = await describe(action_id) + if out is None: + raise HTTPException(404, "生效记录不存在,所属的运行可能已被删除") + if restricted() and not await can_read_run(current_principal(), out["run_id"]): + raise HTTPException(403, "没有权限查看这条生效记录") + return out diff --git a/backend/app/api/hooks.py b/backend/app/api/hooks.py index 344b4676..3922be64 100644 --- a/backend/app/api/hooks.py +++ b/backend/app/api/hooks.py @@ -1,6 +1,31 @@ -"""入站钩子(BE-2):POST /api/hooks/{hook_id},只认签名、不走会话。设计文档 §2.4。""" +"""入站钩子(BE-2):POST /api/hooks/{hook_id},只认签名、不走会话。设计文档 §2.4。 + +签名、时间窗、幂等键、start / signal 的规则见 app/process/hooks.py。钩子的增删在 /api/apps/{id}/hooks(应用包)。 +开了登录时,身份中间件要放过这条路径(外部系统没有会话,也不带 X-AgentLab-Request)。 +""" from __future__ import annotations -from fastapi import APIRouter +from typing import Any + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse + +from app.process import hooks +from app.process.tasks import TaskRefused router = APIRouter(prefix="/api/hooks", tags=["hooks"]) + + +@router.post("/{hook_id}", response_model=None) +async def receive(hook_id: str, request: Request) -> Any: + from app.apps.entries import FormRejected + + body = await request.body() + try: + return await hooks.receive(hook_id, request.headers, body) + except hooks.HookRejected as e: + raise HTTPException(e.status_code, str(e)) from e + except FormRejected as e: + return JSONResponse({"detail": str(e), "code": "form_invalid", "errors": e.errors}, status_code=422) + except TaskRefused as e: + raise HTTPException(e.status_code, str(e)) from e diff --git a/backend/app/api/notify.py b/backend/app/api/notify.py index fd3b4780..f5e4ddbb 100644 --- a/backend/app/api/notify.py +++ b/backend/app/api/notify.py @@ -1,6 +1,229 @@ -"""通知渠道与投递记录(BE-2)。设计文档 §2.4、§3.10。""" +"""通知渠道与投递记录(BE-2)。设计文档 §2.4、§3.10。 + +渠道的地址和签名密钥加密落库,接口只回主机名、配没配密钥;改渠道时不传地址(或传空)表示保持原值。格式和地址的 +校验同定时任务的 Webhook(app/notify/core.py)。个人渠道(scope=user)只有开了登录才有:它挂在用户 id 上,只有本人 +能看能改;全局、应用的渠道关登录时谁都能改,开了登录要 builder 以上。定时任务的通知仍在定时任务上设,不迁移(Q7)。 + +个人渠道谁都能建(权限最低的使用者也能),地址另过一遍 SSRF 检查(tools/net.py,不开内网),保存和发送时各一次: +不然服务端替它往内网地址发 POST,测试接口交回的状态码和报错就成了探测内网的信号(A6.2:只有登记的端点能访问内网)。 +全局、应用的渠道只有搭建者以上能建,和定时任务的 Webhook 同一个信任级别,地址照旧只查格式(内网的接收端很常见)。 +""" from __future__ import annotations -from fastapi import APIRouter +from typing import Any, Literal + +from fastapi import APIRouter, HTTPException, Query +from pydantic import BaseModel, Field +from sqlalchemy import or_, select + +from app.api.deps import current_principal +from app.core.crypto import encrypt +from app.db.base import SessionLocal +from app.db.models import NotifyChannel, NotifyDelivery +from app.notify import core +from app.notify.service import EVENTS, EVENT_LABEL, channel_out router = APIRouter(prefix="/api/notify", tags=["notify"]) + + +class ChannelIn(BaseModel): + name: str = Field(min_length=1, max_length=100) + scope: Literal["global", "app", "user"] = "global" + owner_id: str | None = None + format: str = "generic" + #: 新建必填;修改时不传或传空表示保持原值 + url: str | None = None + #: 签名密钥:不传保持原值,传空字符串表示去掉 + secret: str | None = None + events: list[str] = Field(default_factory=list) + enabled: bool = True + + +def _can_edit(row_scope: str, owner_id: str | None) -> None: + principal = current_principal() + if principal.mode == "off": + if row_scope == "user": + raise HTTPException(422, "个人通知渠道需要开启登录后才能使用:关闭登录时没有用户,无法认出是谁的渠道") + return + if row_scope == "user": + if owner_id != principal.user_id: + raise HTTPException(403, "只能修改自己的通知渠道") + return + if not principal.at_least("builder"): + raise HTTPException(403, "只有搭建者和管理员可以修改全局或应用的通知渠道") + + +async def _safe_personal(scope: str, url: str) -> None: + """个人渠道的地址过 SSRF 检查(见模块说明)。""" + from app.notify.service import personal_url_problem + + if scope == "user" and (problem := await personal_url_problem(url)): + raise HTTPException(422, problem) + + +def _validated(payload: ChannelIn, *, url: str | None) -> None: + if payload.format not in core.FORMATS: + raise HTTPException(422, f"消息格式只能是{'、'.join(core.FORMAT_LABEL.values())}之一") + unknown = [e for e in payload.events if e not in EVENTS] + if unknown: + raise HTTPException(422, f"不认识的通知事件:{'、'.join(unknown)}。可选:{'、'.join(EVENT_LABEL.values())}") + if url is not None and (problem := core.format_problem(payload.format, url)): + raise HTTPException(422, problem) + + +@router.get("/channels") +async def list_channels() -> dict[str, Any]: + """{items}:形状同前端的接口约定(frontend/src/api/capabilities.ts)。""" + principal = current_principal() + stmt = select(NotifyChannel).order_by(NotifyChannel.created_at) + if principal.mode != "off": + # 个人渠道只给本人看 + stmt = stmt.where(or_(NotifyChannel.scope != "user", NotifyChannel.owner_id == principal.user_id)) + async with SessionLocal() as session: + return {"items": [channel_out(r) for r in (await session.execute(stmt)).scalars()]} + + +@router.get("/events") +async def list_events() -> list[dict[str, str]]: + return [{"event": e, "label": EVENT_LABEL[e]} for e in EVENTS] + + +@router.post("/channels", status_code=201) +async def create_channel(payload: ChannelIn) -> dict[str, Any]: + owner = current_principal().user_id if payload.scope == "user" else payload.owner_id + _can_edit(payload.scope, owner) + try: + url = core.check_url(payload.url) + except ValueError as e: + raise HTTPException(422, str(e)) from e + _validated(payload, url=url) + await _safe_personal(payload.scope, url) + row = NotifyChannel(name=payload.name.strip(), scope=payload.scope, owner_id=owner, format=payload.format, + url=encrypt(url), secret=encrypt(payload.secret) if payload.secret else None, + events=list(dict.fromkeys(payload.events)), enabled=payload.enabled) + async with SessionLocal() as session: + session.add(row) + await session.commit() + return channel_out(row) + + +class ChannelPatch(BaseModel): + """改一部分:没给的项保持原值(界面上的启用开关只传 enabled)。""" + name: str | None = Field(default=None, min_length=1, max_length=100) + scope: Literal["global", "app", "user"] | None = None + owner_id: str | None = None + format: str | None = None + url: str | None = None + secret: str | None = None + events: list[str] | None = None + enabled: bool | None = None + + +@router.patch("/channels/{channel_id}") +async def patch_channel(channel_id: str, payload: ChannelPatch) -> dict[str, Any]: + async with SessionLocal() as session: + row = await session.get(NotifyChannel, channel_id) + if row is None: + raise HTTPException(404, "通知渠道不存在,可能已被删除") + current = {"name": row.name, "scope": row.scope, "owner_id": row.owner_id, "format": row.format, + "events": list(row.events or []), "enabled": row.enabled} + given = {k: v for k, v in payload.model_dump(exclude_unset=True).items() if v is not None} + return await update_channel(channel_id, ChannelIn(**{**current, **given})) + + +@router.put("/channels/{channel_id}") +async def update_channel(channel_id: str, payload: ChannelIn) -> dict[str, Any]: + async with SessionLocal() as session: + row = await session.get(NotifyChannel, channel_id) + if row is None: + raise HTTPException(404, "通知渠道不存在,可能已被删除") + _can_edit(row.scope, row.owner_id) + if payload.scope != row.scope: + raise HTTPException(422, "通知渠道的范围建好之后不能修改,请新建一个") + url = None + if payload.url: + try: + url = core.check_url(payload.url) + except ValueError as e: + raise HTTPException(422, str(e)) from e + if url is None: + from app.core.crypto import decrypt + + current = decrypt(row.url) or "" + _validated(payload, url=current or None) + else: + _validated(payload, url=url) + await _safe_personal(row.scope, url) + row.url = encrypt(url) + row.name, row.format = payload.name.strip(), payload.format + row.events, row.enabled = list(dict.fromkeys(payload.events)), payload.enabled + if payload.secret is not None: + row.secret = encrypt(payload.secret) if payload.secret else None + if row.scope == "app": + row.owner_id = payload.owner_id + await session.commit() + return channel_out(row) + + +@router.delete("/channels/{channel_id}", status_code=204) +async def delete_channel(channel_id: str) -> None: + async with SessionLocal() as session: + row = await session.get(NotifyChannel, channel_id) + if row is None: + raise HTTPException(404, "通知渠道不存在,可能已被删除") + _can_edit(row.scope, row.owner_id) + await session.delete(row) + await session.commit() + + +@router.post("/channels/{channel_id}/test") +async def test_channel(channel_id: str) -> dict[str, Any]: + """发一条测试消息(不重试),记一条 event=test 的投递记录。返回 {ok, http_status, message},同定时任务的测试。""" + from app.notify import send + + async with SessionLocal() as session: + row = await session.get(NotifyChannel, channel_id) + if row is None: + raise HTTPException(404, "通知渠道不存在,可能已被删除") + _can_edit(row.scope, row.owner_id) + message = {"event": "test", "title": f"通知渠道「{row.name}」测试", "ok": True, "retry": False, + "lines": [("说明", "这是一条测试消息。收到即表示渠道地址配置正确" + + (";请求头带有签名,接收端可据此核对签名密钥" if row.secret and row.format == "generic" + else ""))]} + outcome = await send(row, message) + async with SessionLocal() as session: + session.add(NotifyDelivery(channel_id=row.id, subject_kind="channel", subject_id=row.id, event="test", + status="ok" if outcome.ok else "failed", http_status=outcome.http_status, + attempts=outcome.attempts, error=outcome.error)) + await session.commit() + if outcome.ok: + return {"ok": True, "http_status": outcome.http_status, "message": "请到接收端确认已收到"} + return {"ok": False, "http_status": outcome.http_status, "message": outcome.error or "发送失败"} + + +@router.get("/deliveries") +async def list_deliveries( + channel_id: str | None = None, + run_id: str | None = None, + task_id: str | None = None, + limit: int = Query(default=100, ge=1, le=500), +) -> dict[str, Any]: + """投递记录。开登录后 user 角色只看发往自己的个人渠道的:别的渠道的投递记录里有所有应用、所有运行的待办和动作。""" + stmt = select(NotifyDelivery).order_by(NotifyDelivery.created_at.desc()).limit(limit) + principal = current_principal() + if principal.mode != "off" and not principal.at_least("builder"): + stmt = stmt.join(NotifyChannel, NotifyChannel.id == NotifyDelivery.channel_id).where( + NotifyChannel.scope == "user", NotifyChannel.owner_id == principal.user_id) + if channel_id: + stmt = stmt.where(NotifyDelivery.channel_id == channel_id) + if run_id: + stmt = stmt.where(NotifyDelivery.run_id == run_id) + if task_id: + stmt = stmt.where(NotifyDelivery.task_id == task_id) + async with SessionLocal() as session: + rows = (await session.execute(stmt)).scalars() + return {"items": [{ + "id": r.id, "channel_id": r.channel_id, "subject_kind": r.subject_kind, "subject_id": r.subject_id, + "event": r.event, "run_id": r.run_id, "task_id": r.task_id, "status": r.status, + "http_status": r.http_status, "attempts": r.attempts, "error": r.error, + "created_at": r.created_at.isoformat() if r.created_at else None} for r in rows]} diff --git a/backend/app/api/runs.py b/backend/app/api/runs.py index 153d8b98..5465e59c 100644 --- a/backend/app/api/runs.py +++ b/backend/app/api/runs.py @@ -7,7 +7,7 @@ from typing import Any from fastapi import APIRouter, Depends, Header, HTTPException, Query, WebSocket, WebSocketDisconnect from pydantic import BaseModel, Field -from sqlalchemy import select +from sqlalchemy import or_, select from sqlalchemy.ext.asyncio import AsyncSession from app.api.coded import CodedHTTPException @@ -25,6 +25,11 @@ from app.engine.runner import run_manager router = APIRouter(prefix="/api/runs", tags=["runs"]) +#: POST /api/runs 声明了客户端不能声明的来源(422) +ORIGIN_NOT_ALLOWED = "origin_not_allowed" +#: 删除被拒:这次运行写过外部系统(有演练以外的生效记录,409) +RUN_HAS_EFFECTS = "run_has_effects" + @@ -38,6 +43,9 @@ class StartRunIn(BaseModel): # formal:从已发布的不可变版本发起,可复现可出具;exploratory:随便玩 run_class: str = "exploratory" version: int | None = None # formal 时可指定版本,默认用已发布版本 + #: 运行来源(engine/origin.py):客户端只能声明 chat / canvas / launcher / api,不传按 api 记。 + #: 应用、钩子、定时任务发起的来源只能由服务端写 + origin: str | None = None class RunOut(BaseModel): @@ -63,6 +71,11 @@ class RunOut(BaseModel): collection: str | None = None #: 发起它的定时任务。为空 = 手动发起 schedule_id: str | None = None + #: 运行来源(engine/origin.py 的 ORIGIN_KINDS)。为空是加这一列之前的运行:来源未记录 + origin_kind: str | None = None + #: 应用发起的运行属于哪个应用版本 + app_id: str | None = None + app_version: int | None = None # 时间一律带时区(db.base.UTCDateTime 读出来就是 UTC aware) created_at: Any = None started_at: Any = None @@ -78,6 +91,22 @@ async def start_run( x_actor: str | None = Header(default=None), ) -> Run: """发起规则全在 engine/launch.py(定时任务到点发起也用它),这里只把拒绝换回 HTTP 响应。""" + from app.api.deps import restricted + from app.engine.origin import CLIENT_ORIGINS + from app.tools.datasource import MASK_RESULTS_OPTION + + origin = payload.origin or "api" + if origin not in CLIENT_ORIGINS: + # 应用入口、钩子、定时任务的来源由服务端写:客户端能声明的话,谁都能把一次画布试跑说成应用发起的 + raise CodedHTTPException(422, "运行来源只能是问数据、画布上的试运行、正式运行(手动)或接口,应用、外部系统和定时任务" + "发起的运行由服务端记录来源", ORIGIN_NOT_ALLOWED, field="origin") + options = None + if restricted(): + # user 角色(管理员开了问数据)只能发起问数据当场搭的探索运行:已发布工作流的正式运行、别的来源都是搭建者的事。 + # 查询结果在工具边界就遮,事件、回答里只有遮过的值——它读得到自己这次运行的事件(api/deps.py 的 _run_scope) + if origin != "chat" or payload.run_class != "exploratory" or payload.workflow_id or payload.version is not None: + raise HTTPException(403, "没有权限:使用者只能发起问数据的运行,工作流的运行需要搭建者及以上的角色") + options = {MASK_RESULTS_OPTION: True} try: return await launch_run( session, @@ -89,6 +118,8 @@ async def start_run( run_class=payload.run_class, version=payload.version, actor=actor_of(x_actor), + origin_kind=origin, + options=options, ) except LaunchRefused as e: raise _refused(e) from e @@ -105,6 +136,8 @@ def _refused(e: LaunchRefused) -> HTTPException: #: workflow_id 取这个值时只要没挂在任何工作流上的运行:画布上没保存就跑的临时图, #: 以及工作流被删掉之后留下来的运行(外键是 SET NULL) UNSAVED = "__none__" +#: origin_kind 取这个值时只要来源未记录的运行(加来源这一列之前的老运行) +NO_ORIGIN = "none" @router.get("", response_model=list[RunOut]) @@ -114,6 +147,7 @@ async def list_runs( run_class: str | None = None, q: str | None = Query(default=None, description="按工作流名模糊匹配"), schedule_id: str | None = Query(default=None, description="只要这个定时任务发起的运行"), + origin_kind: str | None = Query(default=None, description=f"逗号分隔的运行来源;{NO_ORIGIN} 表示来源未记录的老运行"), limit: int = Query(default=50, ge=1, le=200), before: datetime | None = Query(default=None, description="翻页游标:只要 created_at 早于它的"), session: AsyncSession = Depends(get_session), @@ -136,6 +170,9 @@ async def list_runs( stmt = stmt.where(Run.workflow_name.ilike(f"%{_like(q.strip())}%", escape="\\")) if schedule_id: stmt = stmt.where(Run.schedule_id == schedule_id) + if origins := _csv(origin_kind): + kinds = [o for o in origins if o != NO_ORIGIN] + stmt = stmt.where(or_(Run.origin_kind.in_(kinds), *([Run.origin_kind.is_(None)] if NO_ORIGIN in origins else []))) if before is not None: stmt = stmt.where(Run.created_at < before) return list((await session.execute(stmt)).scalars()) @@ -264,8 +301,22 @@ def _not_while_inquiring(run_id: str, detail: str) -> None: @router.post("/{run_id}/resume", response_model=RunOut) async def resume_run( run_id: str, payload: ResumeIn, x_actor: str | None = Header(default=None) -) -> Run: +) -> Any: + """兼容垫片:回复审批改走待办服务(app/process/tasks.py 的 respond,按派给谁、会签汇总)。「始终允许」、改过的代码、 + 双盲裁决的选择原样交给节点。没有待审批的(服务重启挂起的运行从断点接着跑)照旧直接恢复。""" _not_while_inquiring(run_id, BUSY_DECIDE) + target = payload.approval_id + if target is None: + async with SessionLocal() as session: + pending = list((await session.execute(select(Approval.id).where( + Approval.run_id == run_id, Approval.status == "pending"))).scalars()) + # 只有一条时才敢认;多条时交给引擎说清楚「无法确定回复的是哪一条」 + target = pending[0] if len(pending) == 1 else None + if target is not None: + async with SessionLocal() as session: + approval = await session.get(Approval, target) + if approval is not None and approval.run_id == run_id and approval.status == "pending": + return await _respond_to(target, payload.response, x_actor) try: # approval_id 以前定义了却从没传下去——一次运行里有多条待审批时, # 引擎无从知道这次回复的是哪一条 @@ -280,6 +331,24 @@ async def resume_run( raise HTTPException(409, str(e)) from e +async def _respond_to(approval_id: str, response: Any, x_actor: str | None) -> Any: + """老接口的回复交给待办服务:只当一票、原样交给节点。待办还没有的(开待办时出过错)现在补一条。""" + from app.api.tasks import principal_of, refused + from app.process.tasks import RawResponse, TaskRefused, respond, task_for_approval + + task_id = await task_for_approval(approval_id) + if task_id is None: + raise HTTPException(404, "审批记录不存在,所属的运行可能已被删除") + try: + out = await respond(task_id, principal_of(x_actor), decision=RawResponse(response)) + except TaskRefused as e: + error = refused(e) + if isinstance(error, HTTPException): + raise error from e + return error + return out["run"] + + class ContinueIn(BaseModel): """接着跑。graph 只能带改过配置的同一张图,结构必须一致。""" @@ -298,12 +367,17 @@ async def continue_run( 重跑一遍,纯属白等。 """ _not_while_inquiring(run_id, BUSY_CONTINUE) + from app.engine.runner import ContinueRefused + try: return await run_manager.continue_failed( run_id, graph=payload.graph, actor=actor_of(x_actor), ) except KeyError as e: raise _missing(e) from e + except ContinueRefused as e: + # 正式运行带图(formal_graph_locked)、写入结果未知还没核实(action_unresolved):带机器码 + raise CodedHTTPException(409, str(e), e.code) from e except ValueError as e: raise HTTPException(409, str(e)) from e @@ -386,16 +460,28 @@ async def get_history(run_id: str, limit: int = 50) -> list[dict[str, Any]]: async def delete_run( run_id: str, force: bool = False, session: AsyncSession = Depends(get_session) ) -> None: - """删除运行记录。运行记录是出具的追溯依据,两种情况要拦: + """删除运行记录。运行记录是出具的追溯依据,三种情况要拦: - 还在跑的:删了会留下没人管的执行任务,它还会往一条不存在的记录里写事件; + - 写过外部系统的(有演练以外的生效记录):生效记录随运行级联删除,删了就对不上外部系统里已经发生的事(P8), + 停在「结果未知、等核实」上的连核实待办一起没了。force 也不放行;工作流的删除检查(F4)也就绕不过去; - 封存过的正式运行:删掉之后清单再也无法核对。确实要删,带 force=true。 """ + from sqlalchemy import func + + from app.db.models import ActionRecord + run = await session.get(Run, run_id) if not run: raise HTTPException(404, "运行记录不存在") if run.status in ("queued", "running") or run_manager.is_active(run_id): raise HTTPException(409, "这次运行仍在进行,现在删除会留下无人管理的后台任务。请先停止运行,再删除") + effects = await session.scalar(select(func.count(ActionRecord.id)).where( + ActionRecord.run_id == run_id, ActionRecord.state != "simulated")) + if effects: + raise CodedHTTPException( + 409, f"这次运行有 {effects} 条写入外部系统的生效记录,是和外部系统对账的依据,不能删除(强制删除也不行)", + RUN_HAS_EFFECTS) if run.run_class == "formal" and run.manifest_hash and not force: raise HTTPException( 409, @@ -589,7 +675,8 @@ async def decide( payload: DecideIn, session: AsyncSession = Depends(get_session), x_actor: str | None = Header(default=None), -) -> Run: +) -> Any: + """兼容垫片:改走待办服务(app/process/tasks.py 的 respond)。请求体照旧,只当一票、原样交给节点。""" approval = await session.get(Approval, approval_id) if not approval: raise HTTPException(404, "审批记录不存在,所属的运行可能已被删除") @@ -607,15 +694,7 @@ async def decide( response["value"] = payload.value if payload.args is not None: response["args"] = payload.args - - try: - return await run_manager.resume( - approval.run_id, response, - approval_id=approval.id, - actor=approval.resolved_by, - ) - except ValueError as e: - raise HTTPException(409, str(e)) from e + return await _respond_to(approval.id, response, x_actor) def _jsonable(value: Any) -> Any: diff --git a/backend/app/api/tasks.py b/backend/app/api/tasks.py index c73a26e6..eec9fc1a 100644 --- a/backend/app/api/tasks.py +++ b/backend/app/api/tasks.py @@ -1,6 +1,125 @@ -"""待办(BE-2):列表、计数、详情、回复、改派、催办。设计文档 §3.7、§3.10。""" +"""待办(BE-2):列表、计数、详情、回复、改派、催办。设计文档 §3.7、§3.10。 + +身份经 api/deps.current_principal 取;关登录时它没有名字,用自报的署名(X-Actor)补上,回复人一栏才写得出人。 +回复被拒回 403 / 404 / 409 / 422,{detail, code};表单不通过时另带 errors(同应用入口的 422)。 +""" from __future__ import annotations -from fastapi import APIRouter +from typing import Any, Literal + +from fastapi import APIRouter, Header, HTTPException, Query +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from app.api.coded import CodedHTTPException +from app.api.deps import current_principal, decode_actor +from app.process import tasks as task_service +from app.process.tasks import FormInvalid, TaskRefused router = APIRouter(prefix="/api/tasks", tags=["tasks"]) + + +def principal_of(x_actor: str | None) -> Any: + return task_service.principal_with_actor(current_principal(), decode_actor(x_actor)) + + +def refused(e: TaskRefused) -> HTTPException | JSONResponse: + if isinstance(e, FormInvalid): + return JSONResponse({"detail": str(e), "code": e.code, "errors": e.errors}, status_code=422) + if e.code: + return CodedHTTPException(e.status_code, str(e), e.code) + return HTTPException(e.status_code, str(e)) + + +def _raise(e: TaskRefused) -> Any: + out = refused(e) + if isinstance(out, JSONResponse): + return out + raise out from e + + +def _csv(value: str | None) -> list[str]: + return [v.strip() for v in (value or "").split(",") if v.strip()] + + +@router.get("") +async def list_tasks( + assigned: Literal["me", "all"] = "me", + status: str = Query(default="pending", description="pending / resolved / all,可逗号分隔"), + app_id: str | None = None, + run_id: str | None = None, + limit: int = Query(default=100, ge=1, le=500), + x_actor: str | None = Header(default=None), +) -> dict[str, Any]: + principal = principal_of(x_actor) + if assigned == "all" and principal.mode != "off" and not principal.at_least("builder"): + raise HTTPException(403, "只能查看派给自己的待办") + statuses = None if status == "all" else _csv(status) or ["pending"] + items = await task_service.list_tasks(principal, assigned=assigned, statuses=statuses, app_id=app_id, + run_id=run_id, limit=limit) + return {"items": items} + + +@router.get("/count") +async def count_tasks(mine: int = 1, x_actor: str | None = Header(default=None)) -> dict[str, int]: + """导航徽标:派给我的、待处理的待办数,按来源分开(apps:应用里的;other:画布、定时任务的审批)。""" + return await task_service.count(principal_of(x_actor)) + + +@router.get("/{task_id}") +async def get_task(task_id: str, x_actor: str | None = Header(default=None)) -> dict[str, Any]: + principal = principal_of(x_actor) + out = await task_service.detail(task_id, principal) + if out is None or not task_service.visible(out, principal): + raise HTTPException(404, "待办不存在,所属的运行可能已被删除") + return out + + +class RespondIn(BaseModel): + decision: str + values: dict[str, Any] | None = None + note: str | None = None + #: 动作确认:确认卡上显示的内容指纹,原样带回 + payload_hash: str | None = None + #: 核实「已生效」时填的外部单号 + external_id: str | None = None + + +@router.post("/{task_id}/respond", response_model=None) +async def respond(task_id: str, payload: RespondIn, x_actor: str | None = Header(default=None)) -> Any: + values = dict(payload.values or {}) + if payload.external_id: + values["external_id"] = payload.external_id + try: + out = await task_service.respond(task_id, principal_of(x_actor), decision=payload.decision, values=values, + note=payload.note, payload_hash=payload.payload_hash) + except TaskRefused as e: + return _raise(e) + return {"task": out["task"], "resolved": out["resolved"]} + + +class Target(BaseModel): + kind: Literal["user", "group"] + name: str + + +class ReassignIn(BaseModel): + to: list[Target] = Field(default_factory=list) + note: str | None = None + + +@router.post("/{task_id}/reassign", response_model=None) +async def reassign(task_id: str, payload: ReassignIn, x_actor: str | None = Header(default=None)) -> Any: + try: + return await task_service.reassign(task_id, principal_of(x_actor), + to=[t.model_dump() for t in payload.to], note=payload.note) + except TaskRefused as e: + return _raise(e) + + +@router.post("/{task_id}/remind", response_model=None) +async def remind(task_id: str, x_actor: str | None = Header(default=None)) -> Any: + try: + return await task_service.remind(task_id, principal_of(x_actor)) + except TaskRefused as e: + return _raise(e) diff --git a/backend/app/engine/compiler.py b/backend/app/engine/compiler.py index 491d59af..ec3e5dfd 100644 --- a/backend/app/engine/compiler.py +++ b/backend/app/engine/compiler.py @@ -16,6 +16,7 @@ from app.engine.nodes import ( ) from app.engine.schema import GraphNode, GraphSpec, NodeType, back_edges, innermost_loops from app.engine.state import GraphState +from app.process.actions import ActionPending NodeRunner = Callable[[GraphState, NodeContext], Awaitable[dict[str, Any]]] @@ -134,6 +135,10 @@ def _wrap( # interrupt() / Command 靠异常向上传递控制流,不是执行失败: # 必须原样放行,否则人工介入会被当成节点报错吞掉。 raise + except ActionPending: + # 写外部系统的结果未知(app/process/actions.py)也不是执行失败:不重试(重试也只会再挂起一次)、 + # 不被 on_error=continue 当成失败往下走,原样交给 runner 收成挂起 + raise except asyncio.CancelledError: raise except Exception as e: # noqa: BLE001 diff --git a/backend/app/engine/lint_process.py b/backend/app/engine/lint_process.py new file mode 100644 index 00000000..45e3b81b --- /dev/null +++ b/backend/app/engine/lint_process.py @@ -0,0 +1,255 @@ +"""流程相关的检查(应用平台 §3.13):人工节点的新配置、按流程使用时会出事的接法。 + +两类问题分两处报,是因为门禁的 flag 只能在受管级别出错误: +- human.*:配置本身不成立(时限写不成时长、会签用在补填模式、升级没写岗位、表单不合目录)。运行时没法照它执行, + 所以由 schema.validate_graph 报错误(human_issues),各级发布、发起运行都挡;publish_issues 本来就把 validate_graph + 并进来,这里不再登记一遍,免得同一处问题说两遍。 +- process.*:图本身能运行,按流程使用(应用钉住它、实例一等几天、中途升级)才出事。登记进 governance.EXTRA_LINTS, + 发布工作流时一律警告;应用把它作为流程钉住时,应用检查(app/apps/lint.py)把它们升成 app.process_unsafe 报错。 + +子工作流里有没有人工节点要读库才知道,门禁本身不查库:调用方先用 load_subgraphs 读好、with_subgraphs 放进来, +和 SQL 检查器、模型接入表同一个做法。没放进来时这一条不查。 +""" +from __future__ import annotations + +import contextlib +import re +from collections.abc import Callable, Iterator, Mapping +from contextvars import ContextVar +from datetime import timedelta +from typing import Any + +from app.engine.schema import GraphNode, GraphSpec, NodeType + +#: ISO 8601 时长里认的部分:周、天、时、分、秒。不认年、月——「一个月」有几天说不准,时限按它算会差出几天 +_DURATION = re.compile( + r"^P(?!$)(?:(?P\d+)W)?(?:(?P\d+)D)?(?:T(?=\d)(?:(?P\d+)H)?(?:(?P\d+)M)?(?:(?P\d+(?:\.\d+)?)S)?)?$") + +#: 人工节点 policy 的写法:any / all / {quorum: m} +POLICIES = ("any", "all") +#: on_timeout 的动作。没有「自动批准」 +TIMEOUT_ACTIONS = ("remind", "escalate", "reject") +#: assignee 的种类 +ASSIGNEE_KINDS = ("slot", "initiator", "expression", "hook") + +_MODE_LABEL = {"approve": "审批", "input": "补充输入", "edit": "编辑稿件"} + + +def parse_duration(text: Any) -> timedelta | None: + """ISO 8601 时长(PT4H、P2D、P1DT30M)→ timedelta;不认得、或者是零的返回 None。""" + if not isinstance(text, str): + return None + m = _DURATION.match(text.strip().upper()) + if not m: + return None + parts = {k: float(v) if v else 0.0 for k, v in m.groupdict().items()} + delta = timedelta(weeks=parts["w"], days=parts["d"], hours=parts["h"], minutes=parts["m"], seconds=parts["s"]) + return delta if delta.total_seconds() > 0 else None + + +def normalize_policy(value: Any) -> dict[str, Any] | None: + """节点配置里的 policy → 待办上存的形状 {"kind": "any" | "all"} / {"kind": "quorum", "m": m};写法不对返回 None。""" + if value in (None, ""): + return {"kind": "any"} + if isinstance(value, str) and value in POLICIES: + return {"kind": value} + if isinstance(value, dict): + if value.get("kind") in POLICIES and set(value) <= {"kind"}: + return {"kind": value["kind"]} + m = value.get("quorum", value.get("m") if value.get("kind") == "quorum" else None) + if isinstance(m, int) and not isinstance(m, bool) and m >= 1: + return {"kind": "quorum", "m": m} + return None + + +def assignee_problem(value: Any) -> str | None: + """assignee 的写法问题,没问题返回 None。人工节点和写入动作的调用工具节点共用。""" + if value in (None, "", {}): + return None + if not isinstance(value, dict) or value.get("kind") not in ASSIGNEE_KINDS: + return "「派给谁」只能是岗位、发起人、表达式或外部系统" + kind = value["kind"] + if kind == "slot" and not str(value.get("slot") or "").strip(): + return "「派给谁」选了岗位,还没有填写岗位名" + if kind == "expression" and not str(value.get("expr") or "").strip(): + return "「派给谁」选了表达式,还没有填写表达式" + if kind == "hook" and not str(value.get("correlation") or "").strip(): + return "「派给谁」选了外部系统,还没有填写关联键模板" + return None + + +def human_issues(node: GraphNode) -> list[tuple[str, str, str]]: + """人工节点新配置的问题:[(编号, 说明, 字段)]。schema.validate_graph 按错误报。 + + form 合不合目录用应用包的 validate_form_schema(表单只有一种说法,P5);只有写了 form 的节点才去问它。""" + cfg = node.config + out: list[tuple[str, str, str]] = [] + mode = cfg.get("mode") or "approve" + + if problem := assignee_problem(cfg.get("assignee")): + out.append(("human.assignee_invalid", problem, "assignee")) + policy = normalize_policy(cfg.get("policy")) + if policy is None: + out.append(("human.policy_invalid", "「会签方式」只能是任一人、全体同意或其中若干人同意", "policy")) + elif policy["kind"] != "any" and mode != "approve": + out.append(("human.policy_mode", + f"「会签方式」的全体同意、若干人同意只用于审批模式;当前是{_MODE_LABEL.get(mode, mode)}模式," + "请改为任一人处理", "policy")) + due = cfg.get("due") + if due not in (None, "") and parse_duration(due) is None: + out.append(("human.due_invalid", f"「时限」需要写成 ISO 8601 时长,例如 PT4H(4 小时)、P2D(2 天),当前为「{due}」", + "due")) + timeout = cfg.get("on_timeout") + if timeout not in (None, "", {}): + action = timeout.get("action") if isinstance(timeout, dict) else None + if action not in TIMEOUT_ACTIONS: + out.append(("human.timeout_invalid", "「超时处理」只能是提醒、升级或驳回", "on_timeout")) + elif action == "escalate" and not str(timeout.get("to_slot") or "").strip(): + out.append(("human.escalate_slot", "「超时处理」选了升级,还没有填写升级给哪个岗位", "on_timeout.to_slot")) + elif action == "remind" and timeout.get("every") not in (None, "") and parse_duration(timeout["every"]) is None: + out.append(("human.due_invalid", f"提醒间隔需要写成 ISO 8601 时长,例如 PT1H,当前为「{timeout['every']}」", + "on_timeout.every")) + if due in (None, ""): + out.append(("human.due_invalid", "设置了「超时处理」,还需要填写「时限」", "due")) + form = cfg.get("form") + if form not in (None, "", {}): + if not isinstance(form, dict): + out.append(("human.form_invalid", "「表单」必须是一个 JSON 对象", "form")) + else: + from app.apps.forms import validate_form_schema + + if problems := validate_form_schema(form): + out.append(("human.form_invalid", f"「表单」不符合组件目录:{problems[0]}", "form")) + source = cfg.get("form_from") + if source not in (None, "") and not (isinstance(source, str) and re.fullmatch(r"(nodes|vars)\.[\w.\-]+", source.strip())): + out.append(("human.form_from_invalid", "「表单来自」要写成 nodes.某节点.字段 或 vars.变量名", "form_from")) + return out + + +# -------------------------------------------------------------------------- +# process.*:登记进发布门禁 +# -------------------------------------------------------------------------- + +#: 子工作流的图:(工作流 id, 固定的版本,没固定为 None) → GraphSpec。见 load_subgraphs +_SUBGRAPHS: ContextVar[Mapping[tuple[str, int | None], GraphSpec] | None] = ContextVar("process_subgraphs", default=None) + + +async def load_subgraphs(session: Any, spec: GraphSpec) -> dict[tuple[str, int | None], GraphSpec]: + """读出图里子工作流节点引用的图(固定了版本的读那一版,没固定的读工作流现在的图)。读不到、不成形的跳过。""" + from sqlalchemy import select + + from app.db.models import Workflow, WorkflowVersion + + out: dict[tuple[str, int | None], GraphSpec] = {} + for node in spec.nodes: + if node.type != NodeType.SUBGRAPH: + continue + wid, pinned = node.config.get("workflow_id"), node.config.get("workflow_version") + version = int(pinned) if str(pinned or "").isdigit() else None + if not wid or (str(wid), version) in out: + continue + query = (select(WorkflowVersion.graph).where(WorkflowVersion.workflow_id == str(wid), + WorkflowVersion.version == version) + if version is not None else select(Workflow.graph).where(Workflow.id == str(wid))) + graph = (await session.execute(query)).scalar_one_or_none() + try: + out[(str(wid), version)] = GraphSpec.model_validate(graph) + except Exception: # noqa: BLE001 - 读不到、不成形:子工作流节点运行时自己会说清楚 + continue + return out + + +@contextlib.contextmanager +def with_subgraphs(graphs: Mapping[tuple[str, int | None], GraphSpec]) -> Iterator[None]: + token = _SUBGRAPHS.set(graphs) + try: + yield + finally: + _SUBGRAPHS.reset(token) + + +def _who(node: GraphNode) -> str: + from app.engine.labels import type_label + + return f"「{node.title}」({type_label(node.type)})" + + +def _approval_of(spec: GraphSpec, cfg: Mapping[str, Any]) -> str | None: + return cfg.get("approval") or spec.defaults.get("approval") + + +def _may_ask(name: str) -> bool: + """这个工具在正式运行的信任快照下可能要人审批:MCP、自定义工具一律要(tools/trust.py 的 FORMAL_MARK), + 内置工具看它是不是危险的。数据源查询看这一次的 SQL,静态说不准,不报。""" + from app.tools.datasource import QUERY_PREFIX, SCHEMA_PREFIX + from app.tools.registry import get_spec + + if name.startswith((QUERY_PREFIX, SCHEMA_PREFIX)) or "{{" in name: + return False + spec = get_spec(name) + return spec is None or bool(spec.dangerous) + + +def _agent_tool_sets(node: GraphNode) -> list[tuple[str, Mapping[str, Any], list[str]]]: + """(给人看的位置, 审批配置所在的 dict, 工具名):Agent 自己,加上协作节点的各个成员。""" + cfg = node.config + out: list[tuple[str, Mapping[str, Any], list[str]]] = [] + if node.type == NodeType.AGENT: + out.append(("", cfg, [t for t in cfg.get("tools") or [] if isinstance(t, str)])) + for member in cfg.get("agents") or []: + if isinstance(member, dict): + label = f"成员「{member.get('name') or '?'}」" + # 成员没写审批策略的跟随节点 + approval_cfg = member if member.get("approval") else cfg + out.append((label, approval_cfg, [t for t in member.get("tools") or [] if isinstance(t, str)])) + return out + + +def _subgraph_humans(spec: GraphSpec, node: GraphNode) -> list[str] | None: + graphs = _SUBGRAPHS.get() + if graphs is None: + return None + wid, pinned = node.config.get("workflow_id"), node.config.get("workflow_version") + version = int(pinned) if str(pinned or "").isdigit() else None + sub = graphs.get((str(wid or ""), version)) + if sub is None: + return None + return [n.title for n in sub.nodes if n.type == NodeType.HUMAN] + + +def lint_process(spec: GraphSpec, flag: Callable[..., None], *, level: str, strict: bool) -> None: + """process.* 一律警告(hard=False):发布工作流时只提醒,应用按流程钉住时由应用检查升成错误。""" + for node in spec.nodes: + cfg = node.config + who = _who(node) + if node.type in (NodeType.AGENT, NodeType.SUPERVISOR): + for label, approval_cfg, tools in _agent_tool_sets(node): + if _approval_of(spec, approval_cfg) == "never": + continue + asking = [t for t in tools if _may_ask(t)] + if asking: + flag(f"{who}{label}的工具「{'、'.join(asking[:3])}」在正式运行中可能需要人工审批,运行会停在 Agent 中途;" + "按流程使用时,系统升级后这种断点无法恢复。请把需要审批的调用移到「调用工具」节点," + "或在能力登记里把只读的工具设为免审批", + code="process.wait_mid_agent", node_id=node.id, field="tools") + break + elif node.type == NodeType.SUBGRAPH: + humans = _subgraph_humans(spec, node) + if humans: + flag(f"{who}引用的工作流里有人工节点「{'、'.join(humans[:3])}」:子工作流不保存断点," + "恢复时整个子工作流从头重新执行。按流程使用时请把人工节点放到外层工作流", + code="process.human_in_subgraph", node_id=node.id, field="workflow_id") + elif node.type == NodeType.CODE and cfg.get("network"): + flag(f"{who}开启了「允许联网」:按流程使用时,对外部系统的写入请放到「调用工具」节点," + "那里先留生效记录再发送,结果未知时可以核实", + code="process.code_network", node_id=node.id, field="network") + + +def _register() -> None: + from app.engine.governance import EXTRA_LINTS + + if lint_process not in EXTRA_LINTS: + EXTRA_LINTS.append(lint_process) + + +_register() diff --git a/backend/app/engine/nodes/human.py b/backend/app/engine/nodes/human.py index a84725d6..0c266bec 100644 --- a/backend/app/engine/nodes/human.py +++ b/backend/app/engine/nodes/human.py @@ -35,6 +35,9 @@ async def run_human(state: GraphState, ctx: NodeContext) -> dict[str, Any]: payload["draft"] = ctx.render(ctx.cfg("draft", ""), state) if reports := _reviewed_reports(state, ctx): payload["reports"] = reports + # 流程配置(应用平台 §3.7):派给谁、会签、时限、超时、表单。待办服务(app/process/tasks.py)按中断内容开待办, + # 不回头读图——表达式、关联键、表单来源都要按这一刻的状态算,只有节点算得出。没写这些配置的老图,中断内容和以前一字不差 + payload.update(await _process(state, ctx)) response = await ask(ctx, payload, **payload) # 谁批的跟着事件走:时间线要写得出"张工驳回了",没署名就是 None,不再默认写"你" @@ -73,6 +76,86 @@ async def run_human(state: GraphState, ctx: NodeContext) -> dict[str, Any]: return updates +async def _process(state: GraphState, ctx: NodeContext) -> dict[str, Any]: + """人工节点的流程配置,按这一刻的状态算好,放进中断内容。 + + form_from 取上游 agent 拟的表单:运行时按目录校验,不合规退回节点上的静态 form,并记一条警告(P3:不挡运行)。 + 警告经 once 发:退不退回只取决于状态,重放时结果一样,task 的位置不会错开。""" + from app.engine.expressions import resolve_path + from app.engine.lint_process import normalize_policy + + out: dict[str, Any] = {} + if assignee := await resolve_assignee(state, ctx): + out["assignee"] = assignee + if assignee["kind"] == "hook": + out["correlation_key"] = assignee["correlation_key"] + policy = normalize_policy(ctx.cfg("policy")) + if policy and policy["kind"] != "any": + out["policy"] = policy + if ctx.cfg("due"): + out["due"] = str(ctx.cfg("due")) + if isinstance(ctx.cfg("on_timeout"), dict) and ctx.cfg("on_timeout"): + out["on_timeout"] = dict(ctx.cfg("on_timeout")) + + form = ctx.cfg("form") if isinstance(ctx.cfg("form"), dict) and ctx.cfg("form") else None + source = str(ctx.cfg("form_from") or "").strip() + if source: + candidate = resolve_path(template_context(state), source) + if isinstance(candidate, str): + try: + candidate = json.loads(candidate) + except json.JSONDecodeError: + pass + from app.apps.forms import validate_form_schema + + problems = validate_form_schema(candidate) if isinstance(candidate, dict) else ["不是一个 JSON 对象"] + if not problems: + form = candidate + else: + fallback = "已改用节点上的静态表单" if form else "节点上没有静态表单,按原来的方式填写" + await once(ctx, EventType.LOG, level="warn", code="form_from_invalid", + message=f"「{ctx.node.title}」从「{source}」取到的表单不符合组件目录({problems[0]}),{fallback}") + if form: + out["form"] = form + return out + + +async def resolve_assignee(state: GraphState, ctx: NodeContext) -> dict[str, Any] | None: + """派给谁:岗位、发起人原样交给待办服务;表达式在这里求值(只能取输入和变量),关联键在这里渲染。 + + 表达式的结果要是用户名或组名(一个字符串,或几个字符串的列表)。算不出来时记一条警告、派给任何人: + 不挡运行,也不猜。""" + value = ctx.cfg("assignee") + if not isinstance(value, dict) or not value.get("kind"): + return None + kind = value["kind"] + if kind == "slot": + return {"kind": "slot", "slot": str(value.get("slot") or "").strip()} + if kind == "initiator": + return {"kind": "initiator"} + if kind == "hook": + return {"kind": "hook", "correlation_key": ctx.render_str(str(value.get("correlation") or ""), state).strip()} + if kind == "expression": + from app.engine.expressions import ExpressionError, eval_expression + + expr = str(value.get("expr") or "") + scope = {"input": state.get("input") or {}, "vars": state.get("vars") or {}} + try: + result = eval_expression(expr, scope) + except ExpressionError as e: + result, reason = None, str(e) + else: + reason = "结果不是用户名或组名" + names = [result] if isinstance(result, str) else result if isinstance(result, list) else [] + names = [n.strip() for n in names if isinstance(n, str) and n.strip()] + if not names: + await once(ctx, EventType.LOG, level="warn", code="assignee_unresolved", + message=f"「{ctx.node.title}」的「派给谁」表达式没有算出用户名或组名({reason}),已派给任何人") + return None + return {"kind": "expression", "expr": expr, "names": names[:20]} + return None + + #: 模板里对某个节点产出的引用:{{ nodes.write.text }}、{{ vars.weekly }} _NODE_REF = re.compile(r"\bnodes\s*\.\s*([\w-]+)") _VAR_REF = re.compile(r"\bvars\s*\.\s*([\w-]+)") diff --git a/backend/app/engine/nodes/tools.py b/backend/app/engine/nodes/tools.py index deb63718..b5b73732 100644 --- a/backend/app/engine/nodes/tools.py +++ b/backend/app/engine/nodes/tools.py @@ -19,7 +19,8 @@ from app.engine.toolcalls import ToolTimeout, limit_fields, limit_of, run_bounde from app.sandbox.base import SandboxLimits from app.sandbox.manager import sandbox_manager from app.engine.sql_problems import event_checks -from app.tools.datasource import DATA_UNAVAILABLE, QUERY_PREFIX, SCHEMA_PREFIX, run_versions +from app.capabilities.registry import is_platform_query +from app.tools.datasource import DATA_UNAVAILABLE, SCHEMA_PREFIX, run_masks_results, run_versions from app.tools.registry import ( ToolArgsError, ToolBuildError, @@ -47,6 +48,7 @@ def _tool_ctx(ctx: NodeContext) -> ToolContext: memory_scope=ctx.cfg("memory_scope") or ctx.run.memory_scope, collection=ctx.cfg("collection") or ctx.run.collection, data_versions=run_versions(ctx.run), + mask_results=run_masks_results(ctx.run), # 数据源工具运行中途补固定版本时经它留一条事件(tools/datasource.py 的 _pin_late) emit=ctx.emit, ) @@ -61,7 +63,8 @@ async def _dynamic_tool(name: str, ctx: NodeContext) -> Any: return None async with SessionLocal() as session: try: - tools = await build_tools([name], _tool_ctx(ctx), session=session) + # 写入能力只能放在调用工具节点里(应用平台 A5):只有这里传 allow_write=True + tools = await build_tools([name], _tool_ctx(ctx), session=session, allow_write=True) except ToolBuildError as e: # 工具自己的配置写坏了:说清是哪个、去哪里改,不再往下走到 call_tool 里炸 raise NodeError(ctx.node.id, str(e)) from e @@ -78,6 +81,11 @@ async def run_tool(state: GraphState, ctx: NodeContext) -> dict[str, Any]: if not isinstance(args, dict): raise NodeError(ctx.node.id, "工具参数必须是 JSON 对象") + # 能力治理后效果为写入的:走动作的路径(先留生效记录再发送,app/process/actions.py)。登记里没有、或不是写入的, + # 照以前的路径执行 + if (capability := await _write_capability(name, ctx)) is not None: + return await _run_write(state, ctx, name, args, capability) + # 审批通过后节点会整个重放、再算一遍要不要问人,所以这里只能取决于 name 和 args。 # 节点没配时取全局设置(「危险工具默认需要人工确认」),见 NodeContext.approval_mode tool = await _dynamic_tool(name, ctx) @@ -163,7 +171,7 @@ async def run_tool(state: GraphState, ctx: NodeContext) -> dict[str, Any]: evidence_on = ledger_enabled(ctx.run) # 查表结构的工具只在数据本身用不了时算失败:它别的回话(「结构信息不可用」之类)是正常的结果 failed = isinstance(result, str) and ( - (name.startswith(QUERY_PREFIX) and result.startswith(QUERY_FAILED)) + (is_platform_query(name) and result.startswith(QUERY_FAILED)) or (name.startswith(SCHEMA_PREFIX) and result.startswith(DATA_UNAVAILABLE))) if evidence_on and failed: # 没查成:原话就是报错(「查询失败:no such table: x」),照写,别再套一层 @@ -182,11 +190,12 @@ async def run_tool(state: GraphState, ctx: NodeContext) -> dict[str, Any]: ) except Exception: # noqa: BLE001 snapshot_id = None - # 只认数据源工具交回的查询:别的工具(MCP、自定义)拼出同样形状的 JSON,里面的工件 id 不是我们落的 - query = query_entry_fields(result) if evidence_on and name.startswith(QUERY_PREFIX) else None + # 只认平台执行的查询(数据源查询、登记里声明了 tabular 的端点读取,capabilities.registry.is_platform_query): + # 别的工具(MCP、自定义)拼出同样形状的 JSON,里面的工件 id 不是我们落的。按身份认,不看结果长得像不像(F11) + query = query_entry_fields(result) if evidence_on and is_platform_query(name) else None schema = query.get("schema_artifact") if query else None # 这次查询的 SQL 检查结果单独带上:preview 只有前 2000 个字符,checks 排在 rows 后面,通常被截掉 - checks = event_checks(result) if name.startswith(QUERY_PREFIX) else [] + checks = event_checks(result) if is_platform_query(name) else [] ctx.emit(EventType.TOOL_END, tool=name, duration_ms=elapsed, preview=json.dumps(result, ensure_ascii=False, default=str)[:2000], artifact=snapshot_id, **({"query_artifact": query["artifact"]} if query else {}), @@ -217,6 +226,84 @@ async def run_tool(state: GraphState, ctx: NodeContext) -> dict[str, Any]: return updates +def _governance(name: str, ctx: NodeContext) -> dict[str, Any]: + """这次运行发起时固定的能力治理快照(Run.options["capabilities"],见 capabilities.registry.capability_snapshot)。 + 恢复、重放用同一份:停在确认上期间有人改了治理,这次运行仍按发起时的。""" + options = ctx.run.extra.get("run_options") or {} + return ((options.get("capabilities") or {}).get(name)) or {} + + +async def _write_capability(name: str, ctx: NodeContext) -> Any | None: + """写外部系统的能力登记(capabilities.registry.writes_externally:治理后效果为 write,或绑定端点、视参数而定); + 不是写入(或登记里没有)返回 None。效果和绑定的端点按发起时的治理快照认。""" + from app.capabilities.registry import effective_effect, get_capability, writes_externally + + async with SessionLocal() as session: + capability = await get_capability(session, name) + if capability is None: + return None + governance = _governance(name, ctx) + effect = governance.get("effect") or effective_effect(capability) + endpoint_id = governance["endpoint_id"] if "endpoint_id" in governance else capability.endpoint_id + return capability if writes_externally(name, effect, endpoint_id) else None + + +async def _run_write(state: GraphState, ctx: NodeContext, name: str, args: dict[str, Any], + capability: Any) -> dict[str, Any]: + """写外部系统(§3.8)。顺序是升级安全的关键:查生效记录 → 需要确认时在 tool.start 之前停下(节点边界上, + 只有 ask 的记录 task)→ 恢复后重算哈希 → 先写记录再发送。发送前一定先查记录,节点重新执行(恢复、接着跑、 + 跨重放协议)时已生效的交回回执、已发出的转核实,不会重发。""" + from app.engine.nodes.human import resolve_assignee + from app.process import actions + + exec_no = next_exec(state, ctx.node.id) + try: + prep = await actions.prepare(run_id=ctx.run.run_id, node_id=ctx.node.id, exec_no=exec_no, + capability=capability, args=args) + except actions.ActionFailed as e: + raise NodeError(ctx.node.id, str(e)) from e + if prep.receipt is not None: + ctx.emit(EventType.LOG, level="info", code="action_already_applied", + message=f"「{name}」这次写入已经生效,交回存下的回执,不再发送") + return _write_updates(ctx, prep.receipt) + + governance = _governance(name, ctx) + unattended = governance.get("unattended_write", getattr(capability, "unattended_write", False)) + confirm = (governance.get("approval") or getattr(capability, "approval", "inherit")) == "confirm" \ + or not (ctx.approval_mode() == "never" and unattended) + if confirm: + title = f"确认发送:{getattr(capability, 'title', '') or name}" + card = {"kind": "action_confirm", "node_id": ctx.node.id, "mode": "approve", "title": title, "tool": name, + "capability": name, "preview": prep.preview, "payload_hash": prep.payload_hash, + "environment": prep.environment, "endpoint": prep.endpoint, "attempt": prep.attempt} + if assignee := await resolve_assignee(state, ctx): + card["assignee"] = assignee + answer = await ask(ctx, card, **card) + decision = read_decision(answer) + confirmed = answer.get("payload_hash") if isinstance(answer, dict) else None + await once(ctx, EventType.HUMAN_RESOLVED, tool=name, approved=decision.approved, note=decision.note, + actor=ctx.actor(), payload_hash=confirmed) + if not decision.approved: + raise NodeError(ctx.node.id, f"「{name}」的发送已被驳回" + (f":{decision.note}" if decision.note else "")) + if confirmed != prep.payload_hash: + # 动作确认不接受改参数:要改在上游的表单里改。确认之后上游的数据变了,看过的就不是要发的,拒发 + raise NodeError(ctx.node.id, f"「{name}」确认时看到的内容和现在要发送的不一致(内容指纹对不上)," + "已拒绝发送。请重新发起运行,确认新的内容") + try: + result = await actions.send(prep, capability, args, run_id=ctx.run.run_id, node_id=ctx.node.id, + exec_no=exec_no, emit=ctx.emit, on_behalf=ctx.actor() or prep.run.get("started_by")) + except actions.ActionFailed as e: + raise NodeError(ctx.node.id, str(e)) from e + return _write_updates(ctx, result) + + +def _write_updates(ctx: NodeContext, result: dict[str, Any]) -> dict[str, Any]: + updates: dict[str, Any] = {"nodes": {ctx.node.id: result}} + if var_name := ctx.cfg("assign_to", ""): + updates["vars"] = {var_name: result} + return updates + + def _query_failure(name: str, result: str) -> str: """调用工具节点上查询没查成时的报错。 diff --git a/backend/app/engine/replay.py b/backend/app/engine/replay.py index 46e0895c..3d4d1078 100644 --- a/backend/app/engine/replay.py +++ b/backend/app/engine/replay.py @@ -28,7 +28,14 @@ from app.engine.context import NodeContext #: 记录 task,agent 的 tool_step 也换了位置和返回值:升级前停下的断点照新代码重放, #: 节点里已经执行过的工具、模型调用对不上旧的位置,会再执行一次(副作用翻倍),缓存 #: 下来的旧返回值还可能直接对不上形状。恢复前先看版本,对不上又有这种风险就拒绝 -#: (见 runner 的 _stale_replay)。以后再改节点里 task 的调用顺序,把它加一 +#: (见 runner 的 _stale_replay)。 +#: +#: 规矩:以后改了任何节点里 task 的调用顺序(多一个、少一个、换位置,含 once / ask 的记录 task),把它加一, +#: 并跑一遍升级安全测试(tests/test_replay_side_effects.py 里「跨重放协议」那几条:monkeypatch 成新版本号, +#: 停在人工节点的能恢复、停在 Agent 半路审批的拒绝、写入节点开头等确认的能恢复且只发一次、结果未知的核实后 +#: 接着跑且不重发)。应用的流程实例一等就是几天,中间升级是常态:不加一,旧断点会按新顺序重放、重复执行; +#: 加了一,_stale_replay 才认得出哪些断点不能续。写入动作(调用工具节点 tool.start 带 action_key)是唯一的豁免, +#: 理由见 _stale_replay PROTOCOL = 2 PROTOCOL_KEY = "agentlab_replay" diff --git a/backend/app/engine/runner.py b/backend/app/engine/runner.py index 8389af5d..fe4b45a4 100644 --- a/backend/app/engine/runner.py +++ b/backend/app/engine/runner.py @@ -27,6 +27,7 @@ from app.engine.context import NodeError, RunContext from app.engine.deadline import segment from app.engine.replay import PROTOCOL, PROTOCOL_KEY, protocol_of from app.engine.schema import GraphSpec, NodeType, loop_steps, topology_of, validate_graph +from app.process.actions import ActionPending from app.tools.datasource import RUN_VERSIONS_KEY logger = logging.getLogger(__name__) @@ -40,6 +41,19 @@ _EPHEMERAL = {EventType.LLM_TOKEN, EventType.LLM_THINKING_DELTA} RUN_OPTIONS_KEY = "run_options" +#: 接着跑被拒的机器码:正式运行带图(F2),有结果未知、还没核实的写入 +FORMAL_GRAPH_LOCKED = "formal_graph_locked" +ACTION_UNRESOLVED = "action_unresolved" + + +class ContinueRefused(ValueError): + """接着跑被拒、带机器码的那几种。是 ValueError:老调用方按 ValueError 接(409)照旧成立。""" + + def __init__(self, message: str, *, code: str) -> None: + super().__init__(message) + self.code = code + + class RunManager: """负责一次运行的完整生命周期:启动、串流、中断、恢复、取消。""" @@ -472,7 +486,13 @@ class RunManager: 服务重启挂起的运行(interrupted 且没有待审批)也走这里:它没有在等谁, 断点完好,从断点驱动和失败后接着跑是同一件事。界面上两者都叫「接着跑」。 + + 正式运行不能带图接着跑(F2):它钉的是发布的不可变版本、按 graph_hash 核对,换一张图接着跑,运行就不再等于它 + 钉住的那一版,而 version_hash 照旧写着那一版。接着跑也不改发起人:接着跑的人记在 run.resumed 里。 + 写外部系统结果未知、还没核实的(app/process/actions.py)也不能接着跑:核实之后系统会自动接着跑。 """ + from app.process.actions import unresolved_action + async with SessionLocal() as session: run = await session.get(Run, run_id) if not run: @@ -484,6 +504,14 @@ class RunManager: )).first() is not None if not (run.status == "failed" or (run.status == "interrupted" and not has_pending)): raise ValueError(_cannot_continue(run.status, has_pending)) + if graph is not None and run.run_class == "formal": + raise ContinueRefused( + "正式运行固定在发布的版本上,不能修改配置后继续运行。如需修改,请在画布上修改并发布新版本后重新发起", + code=FORMAL_GRAPH_LOCKED) + if run.status == "interrupted" and await unresolved_action(session, run_id): + raise ContinueRefused( + "这次运行有一次写入外部系统的结果未知,正在等待核实;核实之后运行会自动继续,无需手动继续运行", + code=ACTION_UNRESOLVED) observed = run.status spec = GraphSpec.model_validate(run.graph) workflow_id = run.workflow_id @@ -509,6 +537,10 @@ class RunManager: config = {"configurable": {"thread_id": run_id}} snapshot = await app.aget_state(config) pending = [str(n) for n in (getattr(snapshot, "next", None) or [])] + if not pending: + # 停在审批上、批了之后才失败的节点(动作确认之后结果未知、工具批准之后执行出错):LangGraph 的 next 里 + # 没有它(答复的写入挂在它身上),可断点完好,从断点驱动会重新执行它、审批的答复取自断点 + pending = [str(t.name) for t in getattr(snapshot, "tasks", None) or () if getattr(t, "error", None)] if not pending: raise ValueError( "这次运行没有可继续的断点,可能在执行第一个节点前就已中断。请重新发起运行。" @@ -525,8 +557,7 @@ class RunManager: # 可能已经先动了它 await _claim(session, run_id, (observed,), lambda status: _cannot_continue(status, False), - **({"graph": spec.model_dump(mode="json")} if graph is not None else {}), - **({"started_by": actor} if actor else {})) + **({"graph": spec.model_dump(mode="json")} if graph is not None else {})) run = await session.get(Run, run_id) bus.set_seq(run_id, await _seq_floor(session, run_id, run.last_seq)) await session.commit() @@ -650,6 +681,8 @@ class RunManager: #: 终态事件(run.failed / run.cancelled)等 _finalize 算完耗时再发,timing 才带得上 terminal: tuple[EventType, dict[str, Any]] | None = None error_node: str | None = None + #: 结果未知、挂起等核实的动作(ActionPending),收尾之后发 action.unknown、安排核实 + pending_action: str | None = None deadline: asyncio.Timeout | None = None # 停在审批上的那一刻:这一段的执行算到这里为止。后面写审批记录、发 run.interrupted 是收尾, @@ -762,6 +795,12 @@ class RunManager: error = "用户取消" terminal = (EventType.RUN_CANCELLED, {"actor": self._stopped_by.pop(run_id, None)}) raise + except ActionPending as e: + # 写外部系统的结果未知(app/process/actions.py):不在节点里等,运行收成挂起、不封存,等核实之后 + # 由系统接着跑。节点重新执行时先查生效记录,已生效的交回回执、未知的照旧挂起,不会重发 + status = "interrupted" + error = str(e) + pending_action = e.action_id except TimeoutError as e: status = "failed" if deadline is not None and deadline.expired(): @@ -806,6 +845,13 @@ class RunManager: self._finalizing.discard(run_id) self._tasks.pop(run_id, None) self._stopped_by.pop(run_id, None) + if pending_action is not None: + from app.process.actions import on_unknown + + try: + await on_unknown(pending_action) + except Exception: # noqa: BLE001 - 运行已经收成挂起;没安排上的核实,下次启动时 actions.startup 补上 + logger.exception("run %s 的动作 %s 结果未知,安排核实时出错", run_id, pending_action) # 运行到终态就把沙箱会话收掉。thread_id 每个 run 都是新的, # 不收的话每跑一次带代码节点的图就多一台常驻 microVM(几百 MB), # 而且没有任何自动回收路径会碰它——闲置回收只在"下次有人执行 @@ -840,6 +886,7 @@ class RunManager: value = getattr(item, "value", item) interrupt_id = getattr(item, "id", None) payload = value if isinstance(value, dict) else {"value": value} + opened: str | None = None async with SessionLocal() as session: existing = ( await session.execute( @@ -851,17 +898,26 @@ class RunManager: ) ).scalar_one_or_none() if existing is None: - session.add( - Approval( - run_id=run_id, - node_id=str(payload.get("node_id") or ""), - interrupt_id=interrupt_id, - mode=str(payload.get("mode") or "approve"), - title=str(payload.get("title") or "待审批"), - payload=payload, - schema_=payload.get("schema") or {}, - ) + approval = Approval( + run_id=run_id, + node_id=str(payload.get("node_id") or ""), + interrupt_id=interrupt_id, + mode=str(payload.get("mode") or "approve"), + title=str(payload.get("title") or "待审批"), + payload=payload, + schema_=payload.get("schema") or {}, ) + session.add(approval) + await session.flush() + # 同一次中断给人的那一面(app/process/tasks.py):和 Approval 同一个事务,有审批就有待办。 + # 开待办出了错不能连审批一起丢——运行确实停在这里等人;下次启动时按「派给任何人」补建 + try: + async with session.begin_nested(): + from app.process.tasks import open_task + + opened = (await open_task(session, approval)).id + except Exception: # noqa: BLE001 + logger.exception("run %s 的审批 %s 没能开出待办,启动时会补建", run_id, approval.id) await session.commit() await self._emit( run_id, @@ -869,6 +925,10 @@ class RunManager: node_id=payload.get("node_id"), data={"interrupt_id": interrupt_id, "payload": _safe(payload)}, ) + if opened: + from app.process.tasks import announce + + await announce(opened) return True async def _finalize( @@ -894,9 +954,11 @@ class RunManager: # 会读到半截——以前先提交状态,前一种偶尔翻不到;反过来先落事件,后一种 # 每次都读到 running 和空的 output event: RunEventModel | None = None + app_id: str | None = None async with SessionLocal() as session: run = await session.get(Run, run_id) if run: + app_id = run.app_id timing = await _timing(session, run, elapsed_ms, now) events_usage = await _usage_of_events(session, run_id) inquiry = events_usage.pop("inquiry", None) @@ -959,6 +1021,11 @@ class RunManager: # 关停时挂起的也要让在线的客户端收尾:进程马上就没了,不说一声, # 界面会一直以为它还在跑 await bus.close(run_id) + if app_id and status in ("succeeded", "failed"): + # 应用实例结束、失败:按应用订阅推送(app/notify/service.py)。放到后台,接收端慢不拖着收尾 + from app.notify.service import notify_run, spawn + + spawn(notify_run(run_id, "run.finished" if status == "succeeded" else "run.failed")) async def _seal(self, run_id: str) -> None: """对到此为止的全部事件算清单哈希,记下封到了哪一条。""" @@ -1243,6 +1310,10 @@ async def _stale_replay(run_id: str, spec: GraphSpec, snapshot: Any) -> str | No 「断点之后」按 checkpoint 的写入时刻算:同一个 superstep 里恢复不会写新的 checkpoint, 所以升级后恢复过一次的运行,断点仍是旧版写的,而那之后的工作是新版做的—— 哪一段是哪一版,看那一段 run.started 里记的 replay_protocol。 + + 只多豁免一种(应用平台 A4 第 9 条):调用工具节点在断点之后的每一条 tool.start 都带 action_key——这是写入动作, + 发送前一定先按 action_key 查生效记录(app/process/actions.py),已生效的交回回执、已发出的转核实,重复执行由数据库 + 挡住,task 的位置错开也不会重发。一条 tool.start 都没有的不算(说不清它是不是写入节点),照旧拒绝。 """ if protocol_of(getattr(snapshot, "metadata", None)) >= PROTOCOL: return None @@ -1263,6 +1334,7 @@ async def _stale_replay(run_id: str, spec: GraphSpec, snapshot: Any) -> str | No .order_by(RunEvent.seq) )).all() protocol = 1 + done: dict[str, list[tuple[str, dict[str, Any]]]] = {} for kind, owner, ts, data in rows: if kind == EventType.RUN_STARTED: protocol = protocol_of({PROTOCOL_KEY: (data or {}).get("replay_protocol")}) @@ -1271,7 +1343,12 @@ async def _stale_replay(run_id: str, spec: GraphSpec, snapshot: Any) -> str | No continue for node_id in suspects: if owner == node_id or (owner or "").startswith(f"{node_id}/"): - return nodes[node_id].title or node_id + done.setdefault(node_id, []).append((kind, data or {})) + for node_id, events in done.items(): + starts = [data for kind, data in events if kind == EventType.TOOL_START] + if nodes[node_id].type == NodeType.TOOL and starts and all(d.get("action_key") for d in starts): + continue + return nodes[node_id].title or node_id return None @@ -1322,7 +1399,9 @@ async def _seq_floor(session: Any, run_id: str, last_seq: int | None) -> int: async def _close_approvals(session: Any, run_id: str, actor: str | None, now: datetime) -> int: - """运行已经结束,它还没处理完的审批一并关掉。返回关掉了几条。""" + """运行已经结束,它还没处理完的审批一并关掉,对应的待办(连同核实待办)跟着关闭。返回关掉了几条审批。""" + from app.process.tasks import close_for_run + rows = list((await session.execute( select(Approval).where(Approval.run_id == run_id, Approval.status.in_(["pending", "answered"])) @@ -1331,6 +1410,7 @@ async def _close_approvals(session: Any, run_id: str, actor: str | None, now: da approval.status = "cancelled" approval.resolved_at = now approval.resolved_by = actor + await close_for_run(session, run_id, now) return len(rows) diff --git a/backend/app/engine/schema.py b/backend/app/engine/schema.py index 7a9434b9..7284f28b 100644 --- a/backend/app/engine/schema.py +++ b/backend/app/engine/schema.py @@ -899,6 +899,18 @@ def validate_graph(spec: GraphSpec) -> ValidationResult: elif node.type == NodeType.TOOL: if not cfg.get("tool"): result.add("「调用工具」节点还没有选择工具", node_id=node.id, field="tool") + # 写入动作的确认派给谁(应用平台 §3.8):写法同人工节点 + from app.engine.lint_process import assignee_problem + + if problem := assignee_problem(cfg.get("assignee")): + result.add(problem, node_id=node.id, field="assignee", code="human.assignee_invalid") + elif node.type == NodeType.HUMAN: + # 人工节点的流程配置(派给谁、会签、时限、超时、表单,§3.7)。运行时没法照着写错的配置执行,按错误报, + # 各级发布和发起运行都挡。实现在 engine/lint_process.py + from app.engine.lint_process import human_issues + + for code, message, field in human_issues(node): + result.add(message, node_id=node.id, field=field, code=code) elif node.type == NodeType.SUBGRAPH: if not cfg.get("workflow_id"): result.add("「子工作流」节点还没有选择要嵌套的工作流", node_id=node.id, diff --git a/backend/app/notify/__init__.py b/backend/app/notify/__init__.py index 64ab1e09..cc70c81e 100644 --- a/backend/app/notify/__init__.py +++ b/backend/app/notify/__init__.py @@ -1,10 +1,35 @@ -"""通知的发送核心(附录 C,BE-2 提供)。从 scheduler/webhook.py 抽出:四种格式、签名、发送、脱敏; -webhook.py 照原名再导出,定时任务的通知存储不迁移(F14)。新对象(待办、实例、动作)写 notify_deliveries。""" +"""通知的发送核心(附录 C,BE-2 提供)。从 scheduler/webhook.py 抽出:四种格式、签名、发送、脱敏(core.py); +webhook.py 照原名再导出,定时任务的通知存储不迁移(F14)。新对象(待办、实例、动作)的订阅和投递记录在 +service.py:渠道(notify_channels)、投递记录(notify_deliveries)。""" from __future__ import annotations from typing import Any +from app.notify.core import Message, Outcome + async def send(channel: Any, message: dict[str, Any]) -> Any: - """按渠道的格式发一条通知。message = {title, status, lines: [...], links: [(文字, 地址)]},不含报告里的具体数字。""" - raise NotImplementedError("BE-2") + """按渠道的格式发一条通知。message = {title, status, lines: [...], links: [(文字, 地址)]},不含报告里的具体数字。 + + channel 是一行 NotifyChannel(地址、密钥是密文)。返回 core.Outcome。""" + from app.core.crypto import decrypt + from app.notify import core + from app.notify.service import generic_body + + url = decrypt(channel.url) or "" + secret = decrypt(channel.secret) if channel.secret else None + msg = Message(title=str(message.get("title") or ""), + lines=[(str(k), str(v)) for k, v in message.get("lines") or []], + links=[(str(k), str(v)) for k, v in message.get("links") or []], + ok=bool(message.get("ok"))) + if not url: + return Outcome(False, None, 0, "渠道的地址无法解密(加密密钥可能已更换),请重新填写地址") + if getattr(channel, "scope", None) == "user": + from app.notify.service import personal_url_problem + + if problem := await personal_url_problem(url): + return Outcome(False, None, 0, problem) + return await core.post(channel.format, url, secret, + lambda: core.build_request(channel.format, url, secret, msg, + generic=generic_body(message, msg)), + label=f"通知渠道「{channel.name}」", retry=bool(message.get("retry", True))) diff --git a/backend/app/notify/core.py b/backend/app/notify/core.py new file mode 100644 index 00000000..d354f5d4 --- /dev/null +++ b/backend/app/notify/core.py @@ -0,0 +1,381 @@ +"""通知的发送核心:四种格式、签名、发送、脱敏(从 scheduler/webhook.py 抽出,定时任务和待办、实例、动作共用一份)。 + +四种格式:通用 JSON、飞书(文本消息)、企业微信(markdown)、钉钉(markdown)。配了签名密钥时:通用格式在请求头 +里带 HMAC-SHA256 签名,飞书、钉钉按各自平台的算法签(飞书放在请求体,钉钉放在地址上);企业微信的 key 本来就在 +地址里,没有另外的签名。入站钩子(app/process/hooks.py)验的也是通用格式这一套签名。 + +Webhook 地址本身就是凭证(企业微信的 key、钉钉的 access_token 都在地址里,飞书的在路径里),和签名密钥一样 +加密落库,日志、推送记录、接口返回里只出现主机名。httpx 每发一个请求都会在 INFO 级别记一行完整地址,这里装了 +一个过滤器把推送期间的那几行换成主机名。 + +三个平台出错时都回 HTTP 200、错误码放在响应体里,所以送没送达要看响应体:按地址的主机认平台,不看选的格式—— +格式选错了(飞书的地址选了通用格式)正是最需要认出来的情形。群机器人的地址只认自己平台的格式,保存时就拦下 +对不上的(见 bot_format)。 + +消息内容(Message)由调用方拼:只放标题、状态、链接这类,不放报告里的具体数字。 +""" +from __future__ import annotations + +import asyncio +import base64 +import contextvars +import hashlib +import hmac +import json +import logging +import unicodedata +from collections.abc import Callable +from dataclasses import dataclass, field +from datetime import datetime +from typing import Any +from urllib.parse import parse_qsl, quote_plus, urlsplit + +import httpx + +from app.db.base import utcnow + +logger = logging.getLogger(__name__) + +FORMATS = ("generic", "feishu", "wecom", "dingtalk") +#: 格式在报错里的叫法,和界面上的选项一致 +FORMAT_LABEL = {"generic": "通用", "feishu": "飞书", "wecom": "企业微信", "dingtalk": "钉钉"} +#: 单次请求的超时(秒) +TIMEOUT_S = 10.0 +#: 失败后再试 3 次,每次之前等这么久(秒)。4xx(429 除外)不重试:地址或参数错了,重试也一样 +RETRY_DELAYS: tuple[float, ...] = (1.0, 2.0, 4.0) +#: 测试用:换成 httpx.MockTransport 接住请求 +_transport: httpx.AsyncBaseTransport | None = None +#: 通用格式签名的请求头。入站钩子验同一套 +TIMESTAMP_HEADER = "X-AgentLab-Timestamp" +SIGNATURE_HEADER = "X-AgentLab-Signature" + + +# -------------------------------------------------------------------------- +# 地址:校验、提示、脱敏 +# -------------------------------------------------------------------------- + + +_URL_FORMAT = ("Webhook 地址需要是 http:// 或 https:// 开头的完整地址,例如飞书机器人的 " + "https://open.feishu.cn/open-apis/bot/v2/hook/…") + + +def check_url(url: Any) -> str: + """校验 Webhook 地址,返回去掉首尾空白的地址。不对抛 ValueError(中文说明,不回显地址)。 + + 除了 http(s)、有主机名,还要 httpx 真发得出去:端口写错、夹着不可见的字符(从聊天软件复制时常带上零宽空格)、 + 主机名不合 IDNA 规则的,保存时就拦下。以前这些照样存进去,到「测试 Webhook」才在 httpx 里抛出 500 或英文报错。 + """ + text = url.strip() if isinstance(url, str) else "" + if not text: + raise ValueError(_URL_FORMAT) + if any(c.isspace() or unicodedata.category(c) in ("Cc", "Cf") for c in text): + raise ValueError("Webhook 地址中有空格或不可见的字符(从聊天软件复制时常会带上),请删除后重新填写") + if len(text) > 2000: + raise ValueError("Webhook 地址过长(超过 2000 个字符),请检查是否粘贴了多余的内容") + try: + parts = urlsplit(text) + except ValueError: + raise ValueError(_URL_FORMAT) from None + if parts.scheme not in ("http", "https") or not parts.hostname: + raise ValueError(_URL_FORMAT) + try: + port = parts.port + except ValueError: # 不是数字,或者超出 65535 + port = -1 + if port is not None and not 1 <= port <= 65535: + raise ValueError("Webhook 地址中的端口需要是 1 到 65535 之间的数字") + try: + # 照发送时的样子建一个请求(不发出去):httpx 到这一步才按 IDNA 解码主机名 + httpx.Request("POST", text) + except (httpx.InvalidURL, ValueError): # IDNA 的错误是 UnicodeError,也是 ValueError + raise ValueError("Webhook 地址的主机名不符合规范,无法发送,请检查后重新填写") from None + return text + + +#: 群机器人的地址(主机,路径前缀)→ 格式。这些地址只认自己平台的消息格式:格式选错了,平台回错误码,群里收不到 +_BOTS = ( + ("open.feishu.cn", "/open-apis/bot/", "feishu"), + ("open.larksuite.com", "/open-apis/bot/", "feishu"), + ("qyapi.weixin.qq.com", "/cgi-bin/webhook/", "wecom"), + ("oapi.dingtalk.com", "/robot/", "dingtalk"), +) +#: 平台的主机 → 按哪个平台读 HTTP 200 里的错误码。同一主机上不是群机器人的接口(飞书集成平台的 Webhook 触发器 +#: 收任意 JSON)也是同一套错误码约定 +_PLATFORM_HOSTS = {host: fmt for host, _, fmt in _BOTS} + + +def bot_format(url: str) -> str | None: + """这条地址是哪个平台的群机器人(feishu / wecom / dingtalk),不是群机器人的返回 None。""" + try: + parts = urlsplit(url) + except ValueError: + return None + host = (parts.hostname or "").lower() + return next((fmt for bot_host, prefix, fmt in _BOTS if host == bot_host and parts.path.startswith(prefix)), None) + + +def format_problem(fmt: str, url: str) -> str | None: + """格式和地址对不上时的说明(群机器人的地址选了别的格式),对得上返回 None。""" + expected = bot_format(url) + if expected is None or expected == fmt: + return None + label = FORMAT_LABEL[expected] + return f"这是{label}群机器人的地址,只接收{label}的消息格式,Webhook 格式请选择「{label}」" + + +def host_of(url: str) -> str: + """主机名(带非默认端口)。日志、报错里只写它。端口写错的地址也照样给出主机名。""" + try: + parts = urlsplit(url) + host = parts.hostname or "?" + except ValueError: + return "?" + try: + port = parts.port + except ValueError: + return host + return f"{host}:{port}" if port else host + + +def url_hint(url: str) -> str: + """给界面看的地址提示:只露主机和末 4 位(open.feishu.cn/…ab12)。""" + return f"{host_of(url)}/…{url[-4:]}" + + +def _sensitive(url: str, secret: str | None) -> list[str]: + """一条地址里不能出现在日志、报错里的片段:整条地址、查询参数的值(key、access_token)、路径里的长片段(飞书 + 的令牌在路径里)、签名密钥。长的在前,免得先换掉短的、长的就认不出来了。""" + out = {url, secret or ""} + try: + parts = urlsplit(url) + out |= {value for _, value in parse_qsl(parts.query)} + out |= {seg for seg in parts.path.split("/") if len(seg) >= 12} + out.add(f"{parts.netloc}{parts.path}") + except ValueError: + pass + return sorted((s for s in out if len(s) >= 4), key=len, reverse=True) + + +def scrub(text: str, url: str, secret: str | None) -> str: + for piece in _sensitive(url, secret): + text = text.replace(piece, "…") + return text + + +#: 正在推送哪条地址、用的哪个密钥。httpx 记请求日志时还在同一个任务里,过滤器据此只改推送的那几行 +_SENDING: contextvars.ContextVar[tuple[str, str | None] | None] = contextvars.ContextVar("webhook_sending", default=None) + + +class _RedactWebhookUrls(logging.Filter): + """把 httpx 记下的推送请求的地址换成主机名。别的请求(模型接入、数据源工具)原样放过。""" + + def filter(self, record: logging.LogRecord) -> bool: + sending = _SENDING.get() + if sending and isinstance(record.args, tuple): + url, secret = sending + record.args = tuple( + url_hint(str(arg)) if isinstance(arg, httpx.URL) + else scrub(arg, url, secret) if isinstance(arg, str) else arg + for arg in record.args) + return True + + +def _install_filter() -> None: + target = logging.getLogger("httpx") + if not any(isinstance(f, _RedactWebhookUrls) for f in target.filters): + target.addFilter(_RedactWebhookUrls()) + + +_install_filter() + + +# -------------------------------------------------------------------------- +# 消息与签名 +# -------------------------------------------------------------------------- + + +@dataclass +class Message: + """一条通知要说的事。lines 是 (项, 内容),links 是 (文字, 地址)。ok:好消息(企业微信里状态用绿色)。""" + + title: str + lines: list[tuple[str, str]] = field(default_factory=list) + links: list[tuple[str, str]] = field(default_factory=list) + ok: bool = False + + +def text_of(msg: Message) -> str: + out = [f"【AgentLab】{msg.title}", *(f"{k}:{v}" for k, v in msg.lines)] + out += [f"{label}:{url}" for label, url in msg.links] + return "\n".join(out) + + +def wecom_markdown(msg: Message) -> str: + color = "info" if msg.ok else "warning" + out = [f"**【AgentLab】{msg.title}**"] + for key, value in msg.lines: + shown = f'{value}' if key == "状态" else value + out.append(f"> {key}:{shown}") + out += [f"[{label}]({url})" for label, url in msg.links] + return "\n".join(out) + + +def dingtalk_markdown(msg: Message) -> tuple[str, str]: + out = [f"#### 【AgentLab】{msg.title}", "", *(f"- {k}:{v}" for k, v in msg.lines)] + for label, url in msg.links: + out += ["", f"[{label}]({url})"] + return msg.title, "\n".join(out) + + +def sign(secret: str, ts: str, body: bytes) -> str: + """通用格式的签名:HMAC-SHA256(密钥, 「时间戳.请求体」),十六进制。请求头里写成 sha256=<签名>。""" + return hmac.new(secret.encode(), f"{ts}.".encode() + body, hashlib.sha256).hexdigest() + + +def build_request(fmt: str, url: str, secret: str | None, msg: Message, *, generic: dict[str, Any], + now: datetime | None = None) -> tuple[str, dict[str, str], bytes]: + """(请求地址, 请求头, 请求体)。generic 是通用格式的请求体(各调用方自己的字段)。签名里的时间戳按 now 取,每次 + 重试重新算:飞书要求时间戳在一小时内。""" + now = now or utcnow() + headers = {"Content-Type": "application/json"} + if fmt == "feishu": + body: dict[str, Any] = {"msg_type": "text", "content": {"text": text_of(msg)}} + if secret: + ts = str(int(now.timestamp())) + # 飞书自定义机器人的签名:以「timestamp + 换行 + 密钥」为键,对空串做 HMAC-SHA256,再 base64 + key = f"{ts}\n{secret}".encode() + body = {"timestamp": ts, "sign": base64.b64encode(hmac.new(key, b"", hashlib.sha256).digest()).decode(), + **body} + return url, headers, _json(body) + if fmt == "wecom": + return url, headers, _json({"msgtype": "markdown", "markdown": {"content": wecom_markdown(msg)}}) + if fmt == "dingtalk": + title, text = dingtalk_markdown(msg) + target = url + if secret: + # 钉钉自定义机器人的签名:毫秒时间戳,以密钥为键对「timestamp + 换行 + 密钥」做 HMAC-SHA256,base64 后 + # 再做 URL 编码,和 timestamp 一起加在地址上 + ts = str(int(now.timestamp() * 1000)) + digest = hmac.new(secret.encode(), f"{ts}\n{secret}".encode(), hashlib.sha256).digest() + sign_text = quote_plus(base64.b64encode(digest).decode()) + target = f"{url}{'&' if '?' in url else '?'}timestamp={ts}&sign={sign_text}" + return target, headers, _json({"msgtype": "markdown", "markdown": {"title": title, "text": text}}) + payload = _json(generic) + if secret: + # 通用格式:接收端按 HMAC-SHA256(密钥, 「时间戳.请求体」) 验签,时间戳防重放 + ts = str(int(now.timestamp())) + headers |= {TIMESTAMP_HEADER: ts, SIGNATURE_HEADER: f"sha256={sign(secret, ts, payload)}"} + return url, headers, payload + + +def _json(value: dict[str, Any]) -> bytes: + return json.dumps(value, ensure_ascii=False, separators=(",", ":")).encode() + + +# -------------------------------------------------------------------------- +# 推送 +# -------------------------------------------------------------------------- + + +@dataclass +class Outcome: + """一次推送(含重试)的结果。error 是中文说明,只有主机名,不含地址和密钥。""" + + ok: bool + http_status: int | None + attempts: int + error: str | None = None + #: 这一次失败值不值得再试(只在单次尝试的结果上有意义) + retryable: bool = False + + +def _client(transport: httpx.AsyncBaseTransport | None = None) -> httpx.AsyncClient: + return httpx.AsyncClient(timeout=TIMEOUT_S, transport=transport or _transport, follow_redirects=False) + + +async def post(fmt: str, url: str, secret: str | None, build: Callable[[], tuple[str, dict[str, str], bytes]], *, + label: str, retry: bool = True, transport: httpx.AsyncBaseTransport | None = None, + delays: tuple[float, ...] | None = None) -> Outcome: + """推送一条通知。build 每次尝试重新拼请求(签名的时间戳要新)。retry 时失败再试 3 次(退避),4xx(429 除外) + 不重试。label 是日志里的「谁的推送」,失败只记主机名。""" + host = host_of(url) + waits = (RETRY_DELAYS if delays is None else delays) if retry else () + last = Outcome(False, None, 0, "没有发出请求") + async with _client(transport) as client: + for attempt in range(len(waits) + 1): + if attempt: + await asyncio.sleep(waits[attempt - 1]) + target, headers, body = build() + last = await _post_once(client, fmt, target, headers, body, url, secret) + last.attempts = attempt + 1 + if last.ok: + return last + logger.warning("%s(%s)第 %d 次推送失败:%s", label, host, attempt + 1, last.error) + if not last.retryable: + break + return last + + +async def _post_once(client: httpx.AsyncClient, fmt: str, target: str, headers: dict[str, str], body: bytes, + url: str, secret: str | None) -> Outcome: + host = host_of(url) + token = _SENDING.set((url, secret)) + try: + response = await client.post(target, content=body, headers=headers) + except httpx.TimeoutException: + return Outcome(False, None, 1, f"请求 {host} 超时({TIMEOUT_S:g} 秒)", retryable=True) + except httpx.ConnectError: + return Outcome(False, None, 1, f"无法连接 {host}", retryable=True) + except httpx.HTTPError as e: + return Outcome(False, None, 1, f"请求 {host} 失败({type(e).__name__})", retryable=True) + except httpx.InvalidURL: + # 保存时的地址校验加严之前存进库的地址:httpx 抛的不是 HTTPError。重试也一样,请人改地址 + return Outcome(False, None, 1, f"发往 {host} 的 Webhook 地址无效,请重新填写地址") + except Exception as e: # noqa: BLE001 - 端口越界之类在传输层才抛(不是 HTTPError),不能让测试接口回 500 + return Outcome(False, None, 1, f"无法向 {host} 发送请求({type(e).__name__}),请检查 Webhook 地址") + finally: + _SENDING.reset(token) + status = response.status_code + reply = _reply_json(response) + if not 200 <= status < 300: + detail = _platform_message(reply) + text = f"{host} 返回 HTTP {status}" + (f":{detail}" if detail else "") + return Outcome(False, status, 1, scrub(text, url, secret)[:300], retryable=status == 429 or status >= 500) + if problem := _platform_error(fmt, url, reply): + return Outcome(False, status, 1, scrub(f"{host} {problem}", url, secret)[:300], retryable=True) + return Outcome(True, status, 1) + + +def _reply_json(response: httpx.Response) -> dict[str, Any] | None: + try: + data = response.json() + except ValueError: + return None + return data if isinstance(data, dict) else None + + +def _platform_message(reply: dict[str, Any] | None) -> str: + if not reply: + return "" + text = reply.get("errmsg") or reply.get("msg") or reply.get("StatusMessage") or "" + return str(text)[:200] + + +def _platform_error(fmt: str, url: str, reply: dict[str, Any] | None) -> str | None: + """HTTP 200 里的平台错误码:飞书 code(老接口 StatusCode),企业微信、钉钉 errcode。0 是成功。 + + 按地址的主机认平台,不看选的格式:以前只看所选格式自己的字段(通用格式完全不看),飞书的地址选了通用格式、 + 平台回 code 19002,照样记「已送达」。主机不是这几个平台的(自建接收端、代理转发)才按所选格式读,通用格式 + 不解读——自建接收端的 code 字段是什么意思由它自己定。说明里写主机名(调用方拼在前面),不写平台名。""" + try: + host = (urlsplit(url).hostname or "").lower() + except ValueError: + host = "" + platform = _PLATFORM_HOSTS.get(host) or (fmt if fmt != "generic" else None) + if not reply or platform is None: + return None + code = reply.get("code", reply.get("StatusCode")) if platform == "feishu" else reply.get("errcode") + if code in (None, 0, "0"): + return None + detail = _platform_message(reply) + return f"返回错误码 {code}" + (f":{detail}" if detail else "") diff --git a/backend/app/notify/service.py b/backend/app/notify/service.py new file mode 100644 index 00000000..61102048 --- /dev/null +++ b/backend/app/notify/service.py @@ -0,0 +1,256 @@ +"""待办、实例、动作的通知:订阅、拼消息、投递记录(notify_channels / notify_deliveries)。 + +谁收得到: +- 应用版本 spec.notify 里订阅了这个事件的渠道([{channel: 渠道 id, events: [...]}]); +- 范围是 global、自己订阅了这个事件的渠道; +- 待办派给的人自己的渠道(范围是 user、owner_id 是他的用户 id;开了登录才有)。 +同一个渠道只推一次。待办改派、升级、催办都是「有一条待办要你处理」,订阅了 task.opened 的渠道一并收到。 + +消息里只放标题、状态、链接(P4,沿用定时任务 Webhook 的规矩):不放报告里的数字,待办的标题用节点在图上的名字 +(搭图时起的),不用按运行数据渲染出来的那一句。链接要设了对外访问地址(设置里的 notify)才有。 + +推送放在后台(spawn):接收端挂住时超时加重试要几十秒,不能拖着开待办、运行收尾。测试用 drain 等它们推完。 +""" +from __future__ import annotations + +import asyncio +import logging +from collections.abc import Coroutine +from typing import Any + +from sqlalchemy import select + +from app.db.base import SessionLocal, utcnow +from app.db.models import App, AppVersion, NotifyChannel, NotifyDelivery, Run, Task, TaskAssignment, User +from app.notify.core import Message, Outcome, host_of + +logger = logging.getLogger(__name__) + +#: 可以订阅的事件 +EVENTS = ("task.opened", "run.finished", "run.failed", "action.unknown") +EVENT_LABEL = {"task.opened": "有待办派给我", "run.finished": "实例完成", "run.failed": "实例失败", + "action.unknown": "写入结果未知"} +#: 这几种也是「有一条待办要你处理」,按 task.opened 的订阅推 +_AS = {"task.reassigned": "task.opened", "task.escalated": "task.opened", "task.reminded": "task.opened"} +_TASK_HEAD = {"task.opened": "新的待办", "task.reassigned": "待办已改派给你", "task.escalated": "待办已超时,升级处理", + "task.reminded": "待办催办"} +_KIND_TEXT = {"approve": "审批", "input": "补充输入", "edit": "编辑稿件", "tool": "工具调用审批", "code": "代码执行审批", + "dual_check": "双盲复算裁决", "action_confirm": "确认写入外部系统", "reconcile": "核实写入结果", + "signal": "等外部系统回调"} +_ENV_TEXT = {"prod": "生产环境", "test": "测试环境", "simulated": "演练"} + +_background: set[asyncio.Task[Any]] = set() + + +def spawn(coro: Coroutine[Any, Any, Any]) -> None: + """在后台推送。留着引用,免得任务还没跑完就被回收。""" + task = asyncio.create_task(coro) + _background.add(task) + task.add_done_callback(_background.discard) + + +async def drain() -> None: + """等后台的推送都推完(测试、关停用)。""" + while _background: + await asyncio.gather(*list(_background), return_exceptions=True) + + +async def personal_url_problem(url: str) -> str | None: + """个人渠道(scope=user,谁都能建)的地址过 SSRF 检查(tools/net.py,不开内网):内网、回环、元数据地址不发。 + api/notify.py 保存时、send 发送前各查一次(域名可能事后改指向内网)。没问题为 None。 + + 说法不带检查的细节(解析到了哪个地址、解析失败还是落在内网):那也是探测内网的信号。""" + from app.tools.net import UnsafeUrlError, assert_safe_url + + try: + await assert_safe_url(url) + except UnsafeUrlError: + return "个人通知渠道只能发往公网地址(不能是内网、回环或云服务器的元数据地址,域名也要能解析),请改用公网的接收地址" + return None + + +def channel_out(row: NotifyChannel) -> dict[str, Any]: + """渠道的接口形状(typesApp.ts 的 NotifyChannelRow):地址只回主机名,密钥只说配没配。""" + from app.core.crypto import decrypt + + url = decrypt(row.url) or "" + return {"id": row.id, "name": row.name, "scope": row.scope, "owner_id": row.owner_id, "format": row.format, + "url_host": host_of(url) if url else "", "signed": bool(row.secret), "events": list(row.events or []), + "enabled": row.enabled} + + +def generic_body(message: dict[str, Any], msg: Message) -> dict[str, Any]: + """通用格式的请求体。subject 只有编号,没有内容。""" + from app.notify.core import text_of + + return {"source": "agentlab", "event": message.get("event"), "title": msg.title, "text": text_of(msg), + "status": message.get("status"), "subject": message.get("subject") or {}, + "links": [{"label": label, "url": url} for label, url in msg.links], "sent_at": utcnow().isoformat()} + + +async def _base_url(session: Any) -> str | None: + from app.api.settings import notify_settings + + return (await notify_settings(session))["public_base_url"] + + +async def _channels(session: Any, event: str, run: Run | None, users: list[str]) -> list[NotifyChannel]: + wanted = _AS.get(event, event) + found: dict[str, NotifyChannel] = {} + if run is not None and run.app_id and run.app_version is not None: + row = (await session.execute(select(AppVersion.spec, AppVersion.bindings).where( + AppVersion.app_id == run.app_id, AppVersion.version == run.app_version))).first() + spec, bindings = (row[0], row[1]) if row is not None else ({}, {}) + # spec 里写的可以是渠道 id,也可以是别名:别名到这个部署的渠道 id 在 bindings.channels 里(spec 可以导出, + # 渠道是这个部署自己的,同岗位绑定) + aliases = (bindings or {}).get("channels") or {} + subscribed: list[str] = [] + for sub in (spec or {}).get("notify") or []: + if isinstance(sub, dict) and wanted in (sub.get("events") or []): + target = aliases.get(str(sub.get("channel")), sub.get("channel")) + subscribed += [str(t) for t in (target if isinstance(target, list) else [target]) if t] + if subscribed: + for row in (await session.execute(select(NotifyChannel).where( + NotifyChannel.id.in_(subscribed), NotifyChannel.enabled.is_(True)))).scalars(): + found[row.id] = row + for row in (await session.execute(select(NotifyChannel).where( + NotifyChannel.scope == "global", NotifyChannel.enabled.is_(True)))).scalars(): + if wanted in (row.events or []): + found.setdefault(row.id, row) + if users: + owners = list((await session.execute(select(User.id).where(User.username.in_(users)))).scalars()) + if owners: + for row in (await session.execute(select(NotifyChannel).where( + NotifyChannel.scope == "user", NotifyChannel.owner_id.in_(owners), + NotifyChannel.enabled.is_(True)))).scalars(): + if wanted in (row.events or []): + found.setdefault(row.id, row) + return list(found.values()) + + +async def _push(channels: list[NotifyChannel], message: dict[str, Any], *, subject_kind: str, subject_id: str, + event: str, run_id: str | None, task_id: str | None) -> int: + from app.notify import send + + for channel in channels: + try: + outcome = await send(channel, message) + except Exception: # noqa: BLE001 - 拼消息、签名出错也不能让别的渠道跟着失败 + logger.exception("通知渠道「%s」推送出错", channel.name) + outcome = Outcome(False, None, 0, "推送时出错,详细信息见服务日志") + async with SessionLocal() as session: + session.add(NotifyDelivery(channel_id=channel.id, subject_kind=subject_kind, subject_id=subject_id, + event=event, run_id=run_id, task_id=task_id, + status="ok" if outcome.ok else "failed", http_status=outcome.http_status, + attempts=outcome.attempts, error=outcome.error)) + try: + await session.commit() + except Exception: # noqa: BLE001 - 渠道刚被删掉(外键):推送记录记不下不影响别的 + logger.warning("通知渠道 %s 的推送记录没能保存", channel.id) + return len(channels) + + +def _label(graph: dict[str, Any] | None, node_id: str | None) -> str: + for n in (graph or {}).get("nodes") or []: + if isinstance(n, dict) and n.get("id") == node_id: + return str(((n.get("data") or {}).get("label")) or node_id)[:80] + return str(node_id or "") + + +async def _where(session: Any, run: Run | None) -> str: + """消息里「在哪」:应用名,没有应用的写工作流名。""" + if run is None: + return "" + if run.app_id: + name = await session.scalar(select(App.name).where(App.id == run.app_id)) + if name: + return f"应用「{name}」" + return f"工作流「{run.workflow_name}」" if run.workflow_name else "" + + +async def notify_task(task_id: str, event: str, *, users: list[str]) -> int: + """待办的通知(task.opened / reassigned / escalated / reminded)。users 是这次要通知到的人(派给的用户名)。返回推了几个渠道。""" + async with SessionLocal() as session: + task = await session.get(Task, task_id) + if task is None: + return 0 + run = await session.get(Run, task.run_id) + channels = await _channels(session, event, run, users) + if not channels: + return 0 + base = await _base_url(session) + where = await _where(session, run) + pending = list((await session.execute(select(TaskAssignment).where( + TaskAssignment.task_id == task_id, TaskAssignment.status == "pending"))).scalars()) + node = _label(run.graph if run else None, task.node_id) + names = "、".join(dict.fromkeys(a.principal or "任何人" for a in pending)) or "任何人" + lines = [("状态", "待处理"), ("种类", _KIND_TEXT.get(task.kind, task.kind)), ("派给", names[:200])] + if where: + lines.insert(0, ("所在", where)) + if task.due_at is not None: + lines.append(("时限", task.due_at.astimezone().strftime("%Y-%m-%d %H:%M"))) + lines.append(("运行", f"#{task.run_id[:6]}")) + message = {"event": event, "status": "pending", "title": f"{_TASK_HEAD.get(event, '待办')}:{node}", + "lines": lines, "links": [("去处理", f"{base}/apps/inbox/{task_id}")] if base else [], + "subject": {"task_id": task_id, "run_id": task.run_id, "app_id": task.app_id}} + return await _push(channels, message, subject_kind="task", subject_id=task_id, event=event, run_id=task.run_id, + task_id=task_id) + + +async def notify_run(run_id: str, event: str) -> int: + """应用实例结束、失败(run.finished / run.failed)。只推应用发起的运行。""" + async with SessionLocal() as session: + run = await session.get(Run, run_id) + if run is None or not run.app_id: + return 0 + channels = await _channels(session, event, run, []) + if not channels: + return 0 + base = await _base_url(session) + where = await _where(session, run) + finished = event == "run.finished" + lines = [("状态", "已完成" if finished else "失败"), ("实例", f"#{run_id[:6]}")] + if not finished and run.error_node_id: + lines.append(("失败节点", f"「{_label(run.graph, run.error_node_id)}」")) + message = {"event": event, "status": run.status, "ok": finished, + "title": f"{where or '应用'}的实例{'已完成' if finished else '失败'}", "lines": lines, + "links": [("查看实例", f"{base}/apps/{run.app_id}/i/{run_id}")] if base else [], + "subject": {"run_id": run_id, "app_id": run.app_id}} + return await _push(channels, message, subject_kind="run", subject_id=run_id, event=event, run_id=run_id, + task_id=None) + + +async def notify_action(action_id: str, event: str) -> int: + """写入外部系统的结果未知(action.unknown)。有核实待办的链接到待办,没有的(系统按幂等键去查)链接到实例。""" + from app.db.models import ActionRecord + + async with SessionLocal() as session: + rec = await session.get(ActionRecord, action_id) + if rec is None: + return 0 + run = await session.get(Run, rec.run_id) + task_id = await session.scalar(select(Task.id).where(Task.correlation_key == f"action:{action_id}", + Task.kind == "reconcile")) + users = list((await session.execute(select(TaskAssignment.principal).where( + TaskAssignment.task_id == task_id, TaskAssignment.principal_kind == "user"))) + .scalars()) if task_id else [] + channels = await _channels(session, event, run, users) + if not channels: + return 0 + base = await _base_url(session) + where = await _where(session, run) + lines = [("状态", "等待核实" if task_id else "系统正在按幂等键查询"), ("环境", _ENV_TEXT.get(rec.environment, rec.environment)), + ("动作", f"#{action_id[:6]}"), ("运行", f"#{rec.run_id[:6]}")] + if where: + lines.insert(0, ("所在", where)) + if task_id and base: + links = [("去核实", f"{base}/apps/inbox/{task_id}")] + elif base and run is not None and run.app_id: + links = [("查看实例", f"{base}/apps/{run.app_id}/i/{rec.run_id}")] + else: + links = [("查看运行", f"{base}/runs/{rec.run_id}")] if base else [] + message = {"event": event, "status": "unknown", "title": f"写入外部系统的结果未知:{rec.capability}", + "lines": lines, "links": links, + "subject": {"action_id": action_id, "run_id": rec.run_id, "task_id": task_id}} + return await _push(channels, message, subject_kind="action", subject_id=action_id, event=event, + run_id=rec.run_id, task_id=task_id) diff --git a/backend/app/process/actions.py b/backend/app/process/actions.py index 0aa29b10..678ff7ee 100644 --- a/backend/app/process/actions.py +++ b/backend/app/process/actions.py @@ -1,8 +1,48 @@ -"""动作服务:写外部系统(附录 C,BE-2 提供)。ActionRecord 是权威记录:先写记录再发送,结果未知永不自动重发。""" +"""动作服务:写外部系统(附录 C,BE-2 提供;设计文档 §2.5 A5、§3.8)。 + +ActionRecord 是权威记录:先写记录再发送,结果未知永不自动重发。调用工具节点(engine/nodes/tools.py)在能力治理后 +效果为 write 时走这里: + + 1. prepare:渲染好的参数 → payload_hash;按 action_key 查记录——已生效的交回存下的回执,已发出 / 结果未知的挂起, + 失败且有人核实为「未生效,重新发送」的 attempt 加一; + 2. (节点)需要确认时在 tool.start 之前中断(kind=action_confirm),恢复后重算哈希,对不上拒绝发送; + 3. send:先写 pending 并提交 → 改成 sent 并提交 → 发请求(幂等键 {action_key}:{attempt})→ 2xx confirmed(回执落工件)、 + 4xx(429 除外)failed、超时 / 中断 / 5xx unknown(抛 ActionPending,运行挂起、不封存)。 + +pending 和 sent 分开记,是为了重启之后说得清:pending 一定还没发出去,可以照原样再发;sent 可能已经发出去了,只能核实。 +环境:正式运行走 prod;探索运行走 test,端点没有 test 环境时演练(不发请求,记 simulated,回执标 simulated: true)。 + +结果未知之后(on_unknown):登记了查询能力(lookup)的,调度循环每拍跑 tick 带退避去查,查到就核实为已生效、系统接着跑; +查满 12 次还没结果、或者没登记查询能力的,开一条核实待办派给应用的 reconciler 岗位,选项是已生效 / 未生效重新发送 / +未生效放弃,核实之后系统接着跑。查询能力按幂等键查:调用参数是 {"idempotency_key": 幂等键},2xx 算查到(回执取响应体), +别的(404 没查到、出错)都算还没结果。 +""" from __future__ import annotations -from datetime import datetime -from typing import Any +import copy +import hashlib +import logging +from dataclasses import dataclass, field +from datetime import datetime, timedelta +from typing import Any, Callable + +from sqlalchemy import exists, select, update +from sqlalchemy.ext.asyncio import AsyncSession + +from app.core.events import EventType +from app.db.base import SessionLocal, utcnow +from app.db.models import ActionRecord, Endpoint, Run + +logger = logging.getLogger(__name__) + +#: 查询能力最多查几次;第 n 次之后等 2^(n-1) 分钟再查,单次间隔最长 LOOKUP_CAP_MIN 分钟 +LOOKUP_MAX = 12 +LOOKUP_CAP_MIN = 60 +#: 预览、回执摘要里遮罩值的写法 +MASK = "••••" +#: 回执摘要最多存多少字(完整响应在回执工件里) +SUMMARY_CHARS = 2000 +_ENV_LABEL = {"prod": "生产环境", "test": "测试环境", "simulated": "演练"} class ActionPending(Exception): @@ -13,19 +53,499 @@ class ActionPending(Exception): self.action_id = action_id +class ActionFailed(Exception): + """这一次写入确定没有生效(外部系统拒绝了、核实为未生效并放弃了)。节点据此失败,说明写在 message 里。""" + + def action_key(run_id: str, node_id: str, exec_no: int) -> str: """sha256(run_id, node_id, exec)。同一次执行第 n 次发送的幂等键是 f"{action_key}:{attempt}"。""" - import hashlib - return hashlib.sha256(f"{run_id}\x1f{node_id}\x1f{exec_no}".encode()).hexdigest() +def payload_hash(capability: str, args: dict[str, Any]) -> str: + """确认卡上的内容指纹:能力名 + 渲染后的参数(真值,不是遮罩后的预览)。确认时看过的就是发出去的,靠它核对。""" + from app.core.artifact_store import canonical_json + + return hashlib.sha256(canonical_json({"capability": capability, "args": args}).encode()).hexdigest() + + +def masked(doc: Any, pointers: list[str]) -> Any: + """按 JSON 指针(RFC 6901)把值换成遮罩。指到不存在的路径就跳过。不改原对象。""" + out = copy.deepcopy(doc) + for pointer in pointers or []: + if not isinstance(pointer, str) or not pointer.startswith("/"): + continue + parts = [p.replace("~1", "/").replace("~0", "~") for p in pointer[1:].split("/")] + node = out + for part in parts[:-1]: + node = node.get(part) if isinstance(node, dict) else ( + node[int(part)] if isinstance(node, list) and part.isdigit() and int(part) < len(node) else None) + if node is None: + break + last = parts[-1] + if isinstance(node, dict) and last in node: + node[last] = MASK + elif isinstance(node, list) and last.isdigit() and int(last) < len(node): + node[int(last)] = MASK + return out + + +# -------------------------------------------------------------------------- +# 准备与发送(调用工具节点用) +# -------------------------------------------------------------------------- + + +@dataclass +class Prepared: + key: str + attempt: int + payload_hash: str + #: 遮罩后的参数:确认卡、事件、记录上给人看的都是这一份 + preview: Any + environment: str + endpoint: str | None + #: 已经生效(或演练过)的:存下的回执,节点直接交回,不再发 + receipt: dict[str, Any] | None = None + run: dict[str, Any] = field(default_factory=dict) + + @property + def idem_key(self) -> str: + return f"{self.key}:{self.attempt}" + + +async def latest(session: AsyncSession, key: str) -> ActionRecord | None: + return await session.scalar(select(ActionRecord).where(ActionRecord.action_key == key) + .order_by(ActionRecord.attempt.desc()).limit(1)) + + +async def prepare(*, run_id: str, node_id: str, exec_no: int, capability: Any, args: dict[str, Any]) -> Prepared: + """节点每次执行都先走这里:按 action_key 查记录,决定发不发、第几次发、走哪个环境。 + + sent / unknown 抛 ActionPending(不发);failed 且没有人核实为「重新发送」的抛 ActionFailed。""" + from app.capabilities.registry import masked_paths + + name = capability.name + key = action_key(run_id, node_id, exec_no) + digest = payload_hash(name, args) + async with SessionLocal() as session: + run = await session.get(Run, run_id) + endpoint = await _endpoint(session, capability) + prev = await latest(session, key) + formal = run is not None and run.run_class == "formal" + environment = "prod" if formal else "test" if endpoint is not None and (endpoint.environments or {}).get("test") \ + else "simulated" + prep = Prepared(key=key, attempt=1, payload_hash=digest, preview=masked(args, masked_paths(capability)), + environment=environment, endpoint=endpoint.name if endpoint is not None else None, + run={"app_id": run.app_id, "workflow_id": run.workflow_id, "workflow_version": run.version, + "graph_hash": run.version_hash, "started_by": run.started_by} if run is not None else {}) + if prev is None: + return prep + if prev.state in ("confirmed", "simulated"): + prep.receipt = _receipt_of(prev) + return prep + if prev.state in ("sent", "unknown"): + raise ActionPending(prev.id) + if prev.state == "pending": + # 记下了、还没发出去(发之前进程没了):照原样再发,同一个幂等键 + prep.attempt, prep.environment = prev.attempt, prev.environment + return prep + outcome = (prev.resolution or {}).get("outcome") + if outcome == "resend": + prep.attempt = prev.attempt + 1 + return prep + if outcome == "abandon": + raise ActionFailed(f"「{name}」这次写入核实为未生效,已选择放弃发送") + raise ActionFailed(f"「{name}」上一次发送被外部系统拒绝({prev.error or '未生效'}),不会自动重发。" + "请检查上游的数据后重新发起运行") + + +async def send(prep: Prepared, capability: Any, args: dict[str, Any], *, run_id: str, node_id: str, exec_no: int, + emit: Callable[..., None], on_behalf: str | None = None) -> dict[str, Any]: + """先写记录再发送。返回节点的产出;结果未知抛 ActionPending,确定失败抛 ActionFailed。""" + from app.capabilities.registry import endpoint_request, masked_paths + + name = capability.name + async with SessionLocal() as session: + rec = await session.scalar(select(ActionRecord).where(ActionRecord.idem_key == prep.idem_key)) + if rec is None: + rec = ActionRecord( + action_key=prep.key, attempt=prep.attempt, idem_key=prep.idem_key, run_id=run_id, node_id=node_id, + exec_no=exec_no, capability=name, capability_version=getattr(capability, "version", None), + endpoint_id=getattr(capability, "endpoint_id", None), environment=prep.environment, + payload_hash=prep.payload_hash, preview=prep.preview, state="pending", + confirm_task_id=await _confirm_task(session, run_id, node_id), + app_id=prep.run.get("app_id"), workflow_id=prep.run.get("workflow_id"), + workflow_version=prep.run.get("workflow_version"), graph_hash=prep.run.get("graph_hash")) + session.add(rec) + await session.commit() + action_id = rec.id + emit(EventType.ACTION_PREPARED, action_id=action_id, capability=name, payload_hash=prep.payload_hash, + environment=prep.environment, preview=prep.preview, attempt=prep.attempt) + + if prep.environment == "simulated": + receipt = {"simulated": True, "environment": "simulated", "endpoint": prep.endpoint} + await _set(action_id, ("pending",), state="simulated", receipt=receipt) + emit(EventType.ACTION_SIMULATED, action_id=action_id, capability=name, + message="演练:探索运行里端点没有测试环境,没有发送请求") + return _output(action_id, prep, "simulated", None, receipt) + + # 先记 sent 再发:发到一半进程没了,重启后看到的是 sent,只能核实,不会当成没发过再发一次 + if not await _set(action_id, ("pending",), state="sent", request_at=utcnow()): + raise ActionPending(action_id) + emit(EventType.ACTION_SENT, action_id=action_id, capability=name, idem_key=prep.idem_key, + environment=prep.environment, attempt=prep.attempt) + emit(EventType.TOOL_START, tool=name, args=prep.preview, action_key=prep.key, action_id=action_id, + environment=prep.environment) + try: + async with SessionLocal() as session: + result = await endpoint_request(session, capability, args, environment=prep.environment, + idem_key=prep.idem_key, on_behalf=on_behalf, run_id=run_id, + node_id=node_id) + except Exception as e: # noqa: BLE001 - 请求可能已经发出去了:一律按结果未知处理,不能当成失败再发 + logger.exception("动作 %s 发送时出错,按结果未知处理", action_id) + await _set(action_id, ("sent",), state="unknown", error=f"发送时出错:{type(e).__name__}") + raise ActionPending(action_id) from e + + status = result.status + shown = masked(result.body, masked_paths(capability)) + if result.environment == "simulated": + receipt = {"simulated": True, "environment": "simulated", "endpoint": prep.endpoint} + await _set(action_id, ("sent",), state="simulated", receipt=receipt, environment="simulated") + emit(EventType.ACTION_SIMULATED, action_id=action_id, capability=name, + message="演练:端点没有测试环境,没有发送请求") + return _output(action_id, prep, "simulated", None, receipt) + if status is not None and 200 <= status < 300: + artifact = await _store_receipt(run_id, node_id, status, result.body, prep) + receipt = {"artifact": artifact, "status": status, "summary": _summary(shown)} + await _set(action_id, ("sent",), state="confirmed", response_status=status, receipt=receipt) + emit(EventType.ACTION_CONFIRMED, action_id=action_id, capability=name, status=status, + receipt=receipt["summary"], artifact=artifact) + emit(EventType.TOOL_END, tool=name, preview=receipt["summary"], artifact=artifact, action_id=action_id) + return _output(action_id, prep, "confirmed", status, result.body) + if status is None and not result.unknown: + # 连接都没建立、端点配置不对:请求确定没有发出去 + await _set(action_id, ("sent",), state="failed", error=result.error or "请求没有发出") + emit(EventType.ACTION_FAILED, action_id=action_id, capability=name, error=result.error) + emit(EventType.TOOL_ERROR, tool=name, error=result.error or "请求没有发出", action_id=action_id) + raise ActionFailed(f"「{name}」没有发送成功:{result.error or '请求没有发出'}") + if status is not None and 400 <= status < 500 and status != 429: + await _set(action_id, ("sent",), state="failed", response_status=status, + error=f"HTTP {status}:{_summary(shown)[:300]}") + emit(EventType.ACTION_FAILED, action_id=action_id, capability=name, status=status) + emit(EventType.TOOL_ERROR, tool=name, error=f"外部系统拒绝了这次写入(HTTP {status})", action_id=action_id) + raise ActionFailed(f"「{name}」被外部系统拒绝(HTTP {status}),没有生效。请检查上游的数据后重新发起运行") + # 超时、连接中断、5xx、429:对方可能已经处理了,只能核实 + await _set(action_id, ("sent",), state="unknown", response_status=status, + error=result.error or (f"HTTP {status}" if status else "没有拿到确定的结果")) + emit(EventType.TOOL_ERROR, tool=name, error="写入的结果未知,等待核实", action_id=action_id) + raise ActionPending(action_id) + + +def _output(action_id: str, prep: Prepared, state: str, status: int | None, body: Any) -> dict[str, Any]: + """节点的产出:receipt 是外部系统的回执(响应体),action 是这次动作的记录摘要。""" + return {"status": status, "receipt": body, "simulated": state == "simulated", + "action": {"id": action_id, "state": state, "environment": prep.environment, + "payload_hash": prep.payload_hash, "attempt": prep.attempt, "idem_key": prep.idem_key}} + + +def _receipt_of(rec: ActionRecord) -> dict[str, Any]: + """已生效 / 演练过的动作交回存下的回执:落了工件的取工件里的响应体,人工核实的是填的外部单号。""" + from app.core.artifact_store import load + + receipt = rec.receipt or {} + body: Any = None + if receipt.get("artifact"): + body = (load(receipt["artifact"]) or {}).get("body") + elif receipt.get("external_id"): + body = {"external_id": receipt["external_id"]} + prep = Prepared(key=rec.action_key, attempt=rec.attempt, payload_hash=rec.payload_hash, preview=rec.preview, + environment=rec.environment, endpoint=None) + return _output(rec.id, prep, rec.state, rec.response_status, body) + + +async def _store_receipt(run_id: str, node_id: str, status: int | None, body: Any, prep: Prepared) -> str | None: + from app.core.artifact_store import put_json + + try: + return await put_json({"status": status, "body": body, "environment": prep.environment, + "idem_key": prep.idem_key, "payload_hash": prep.payload_hash}, + kind="action_receipt", run_id=run_id, node_id=node_id) + except Exception: # noqa: BLE001 - 回执存不下不改变已生效这个事实,记录上留摘要 + logger.exception("动作的回执没能落成工件") + return None + + +def _summary(body: Any) -> str: + import json + + text = body if isinstance(body, str) else json.dumps(body, ensure_ascii=False, default=str) + return text[:SUMMARY_CHARS] + + +async def _set(action_id: str, expected: tuple[str, ...], **values: Any) -> bool: + """条件 UPDATE:状态还是 expected 才改。返回改没改到。""" + async with SessionLocal() as session: + done = await session.execute(update(ActionRecord).where(ActionRecord.id == action_id, + ActionRecord.state.in_(expected)).values(**values)) + await session.commit() + return done.rowcount == 1 + + +async def _endpoint(session: AsyncSession, capability: Any) -> Endpoint | None: + ref = getattr(capability, "endpoint_id", None) + if not ref: + return None + return await session.get(Endpoint, ref) or await session.scalar(select(Endpoint).where(Endpoint.name == ref)) + + +async def _confirm_task(session: AsyncSession, run_id: str, node_id: str) -> str | None: + """这个节点最近一次动作确认的待办(记在生效记录上,查得到是谁确认的)。""" + from app.db.models import Task + + return await session.scalar(select(Task.id).where(Task.run_id == run_id, Task.node_id == node_id, + Task.kind == "action_confirm") + .order_by(Task.created_at.desc()).limit(1)) + + +# -------------------------------------------------------------------------- +# 结果未知之后:查询、核实 +# -------------------------------------------------------------------------- + + +async def unresolved_action(session: AsyncSession, run_id: str) -> bool: + """这次运行有没有已发出、还没核实的动作。有的话不能手动接着跑(核实之后系统自己接着跑)。""" + return bool(await session.scalar(select(exists().where(ActionRecord.run_id == run_id, + ActionRecord.state.in_(("sent", "unknown")))))) + + +async def on_unknown(action_id: str) -> None: + """运行因为结果未知挂起之后(runner 调)、重启时发现 sent 的记录(startup 调):记 action.unknown,安排核实。 + + 幂等:已经排上查询、或已经有核实待办的不再安排,也不再记事件——节点重新执行时撞上同一条未知记录会再抛一次 + ActionPending,那不是一次新的「结果未知」。""" + from app.capabilities.registry import get_capability + from app.db.models import Task + + now = utcnow() + async with SessionLocal() as session: + rec = await session.get(ActionRecord, action_id) + if rec is None or rec.state not in ("sent", "unknown"): + return + if rec.state == "sent": + rec.state = "unknown" + rec.error = rec.error or "服务重启时请求已发出、还没有拿到结果" + cap = await get_capability(session, rec.capability) + has_lookup = bool(getattr(cap, "lookup", None)) + reconciling = await session.scalar(select(Task.id).where( + Task.correlation_key == f"action:{rec.id}", Task.kind == "reconcile", Task.status == "pending")) + arranged = reconciling is not None or (has_lookup and (rec.next_lookup_at is not None + or rec.lookup_attempts > 0)) + task_id: str | None = None + if not arranged: + if has_lookup and rec.lookup_attempts < LOOKUP_MAX: + rec.next_lookup_at = now + timedelta(minutes=1) + else: + task_id = await _open_reconcile(session, rec) + await session.commit() + run_id, node_id, capability, environment = rec.run_id, rec.node_id, rec.capability, rec.environment + if arranged: + return + from app.process.events import record + + await record(run_id, EventType.ACTION_UNKNOWN, node_id=node_id, action_id=action_id, capability=capability, + environment=environment, lookup=has_lookup, task_id=task_id, + message="写入外部系统的结果未知," + ("系统会按幂等键去查" if has_lookup else "等待人工核实")) + await _after_reconcile_opened(task_id, run_id, action_id) + + +async def _open_reconcile(session: AsyncSession, rec: ActionRecord) -> str: + from app.process.tasks import open_reconcile + + task = await open_reconcile(session, rec, title=f"核实写入结果:{rec.capability}") + return task.id + + +async def _after_reconcile_opened(task_id: str | None, run_id: str, action_id: str) -> None: + from app.notify.service import notify_action, spawn + + if task_id: + from app.process.tasks import announce + + await announce(task_id) + spawn(notify_action(action_id, "action.unknown")) + + async def reconcile(action_id: str, principal: Any, *, outcome: str, external_id: str | None = None, note: str | None = None) -> dict[str, Any]: - """核实:outcome ∈ confirmed | not_applied_resend | not_applied_abandon。核实后系统自动接着跑。""" - raise NotImplementedError("BE-2") + """核实:outcome ∈ confirmed | not_applied_resend | not_applied_abandon。核实后系统自动接着跑。 + + 重新发送不在这里发:记下「核实为未生效、要重发」,接着跑时节点按 attempt+1 的新幂等键({action_key}:2)发。""" + from app.process.tasks import TaskRefused, close_reconcile + + if outcome not in ("confirmed", "not_applied_resend", "not_applied_abandon"): + raise TaskRefused("核实只能选已生效、未生效重新发送、未生效放弃之一", status_code=422) + now = utcnow() + username = getattr(principal, "username", None) + resolution = {"by": username, "verified": bool(getattr(principal, "verified", False)), "how": "manual", + "outcome": {"confirmed": "confirmed", "not_applied_resend": "resend", + "not_applied_abandon": "abandon"}[outcome], + "external_id": external_id, "note": note or "", "at": now.isoformat()} + values: dict[str, Any] = {"resolution": resolution} + if outcome == "confirmed": + values.update(state="confirmed", receipt={"external_id": external_id, "manual": True}) + else: + values.update(state="failed", error="核实为未生效") + async with SessionLocal() as session: + done = await session.execute(update(ActionRecord).where( + ActionRecord.id == action_id, ActionRecord.state.in_(("sent", "unknown"))).values(**values)) + if done.rowcount != 1: + await session.rollback() + raise TaskRefused("这个动作已经核实过,或者不在等待核实,请刷新页面查看最新状态") + await close_reconcile(session, action_id, principal, outcome, note) + rec = await session.get(ActionRecord, action_id) + await session.commit() + run_id, node_id = rec.run_id, rec.node_id + from app.process.events import record + + await record(run_id, EventType.ACTION_RECONCILED, node_id=node_id, action_id=action_id, how="manual", + outcome=resolution["outcome"], actor=username, verified=resolution["verified"], + external_id=external_id) + return {"action": await describe(action_id), "run": await _continue(run_id, username)} + + +async def _continue(run_id: str, actor: str | None) -> Any: + """核实之后接着跑。运行已经结束(被放弃了)、或者还在等别的审批时不动它,记日志。""" + from app.engine.runner import run_manager + + try: + return await run_manager.continue_failed(run_id, actor=actor) + except (ValueError, KeyError) as e: + logger.warning("动作核实之后没能接着运行 %s:%s", run_id, e) + return None async def tick(now: datetime | None = None) -> int: - """调度循环每一拍:对登记了 lookup 的结果未知动作带退避地查询。""" - raise NotImplementedError("BE-2") + """调度循环每一拍:对登记了 lookup 的结果未知动作带退避地查询。返回查了几条。""" + now = now or utcnow() + async with SessionLocal() as session: + due = list((await session.execute(select(ActionRecord.id, ActionRecord.next_lookup_at).where( + ActionRecord.state == "unknown", ActionRecord.next_lookup_at.is_not(None), + ActionRecord.next_lookup_at <= now))).all()) + done = 0 + for action_id, claimed_at in due: + try: + done += await _lookup(action_id, claimed_at, now) + except Exception: # noqa: BLE001 - 一条出错不拖住这一拍的其他动作 + logger.exception("动作 %s 的结果查询失败,下一拍再试", action_id) + return done + + +async def _lookup(action_id: str, claimed_at: datetime, now: datetime) -> int: + from app.capabilities.registry import endpoint_request, get_capability, masked_paths + + async with SessionLocal() as session: + # 认领:两拍撞在一起只有一拍查 + claimed = await session.execute(update(ActionRecord).where( + ActionRecord.id == action_id, ActionRecord.state == "unknown", ActionRecord.next_lookup_at == claimed_at) + .values(next_lookup_at=None, lookup_attempts=ActionRecord.lookup_attempts + 1)) + await session.commit() + if claimed.rowcount != 1: + return 0 + rec = await session.get(ActionRecord, action_id) + cap = await get_capability(session, rec.capability) + finder = await get_capability(session, cap.lookup) if cap is not None and cap.lookup else None + if finder is None: + task_id = await _open_reconcile(session, rec) + await session.commit() + await _after_reconcile_opened(task_id, rec.run_id, action_id) + return 1 + try: + result = await endpoint_request(session, finder, {"idempotency_key": rec.idem_key}, + environment=rec.environment, run_id=rec.run_id, node_id=rec.node_id) + except Exception: # noqa: BLE001 - 查询本身出错:按没查到,下次再查 + logger.exception("动作 %s 的结果查询出错", action_id) + result = None + attempts, run_id, node_id = rec.lookup_attempts, rec.run_id, rec.node_id + + if result is not None and result.status is not None and 200 <= result.status < 300: + prep = Prepared(key=rec.action_key, attempt=rec.attempt, payload_hash=rec.payload_hash, + preview=rec.preview, environment=rec.environment, endpoint=None) + artifact = await _store_receipt(run_id, node_id, result.status, result.body, prep) + resolution = {"by": None, "how": "lookup", "outcome": "confirmed", "at": now.isoformat(), + "attempts": attempts} + receipt = {"artifact": artifact, "status": result.status, + "summary": _summary(masked(result.body, masked_paths(cap)))} + if not await _set(action_id, ("unknown",), state="confirmed", receipt=receipt, resolution=resolution, + response_status=result.status): + return 1 + async with SessionLocal() as session: + from app.process.tasks import close_reconcile + + await close_reconcile(session, action_id, None, "confirmed", "按幂等键查到外部系统已生效") + await session.commit() + from app.process.events import record + + await record(run_id, EventType.ACTION_RECONCILED, node_id=node_id, action_id=action_id, how="lookup", + outcome="confirmed", attempts=attempts, artifact=artifact) + await _continue(run_id, None) + return 1 + + if attempts >= LOOKUP_MAX: + async with SessionLocal() as session: + rec = await session.get(ActionRecord, action_id) + task_id = await _open_reconcile(session, rec) + await session.commit() + await _after_reconcile_opened(task_id, run_id, action_id) + return 1 + wait = min(2 ** attempts, LOOKUP_CAP_MIN) + await _set(action_id, ("unknown",), next_lookup_at=now + timedelta(minutes=wait)) + return 1 + + +async def startup() -> int: + """启动时:上次进程没了的时候已经发出(sent)、还没拿到结果的动作收成结果未知,安排核实;结果未知却还没安排核实的 + (挂起之后、安排之前进程没了)补上。返回处理的条数。""" + async with SessionLocal() as session: + ids = list((await session.execute(select(ActionRecord.id).where( + ActionRecord.state.in_(("sent", "unknown")), ActionRecord.resolution.is_(None)))).scalars()) + for action_id in ids: + try: + await on_unknown(action_id) + except Exception: # noqa: BLE001 + logger.exception("动作 %s 的启动整理失败", action_id) + if ids: + logger.info("启动时整理了 %d 条结果未知的动作", len(ids)) + return len(ids) + + +# -------------------------------------------------------------------------- +# 读 +# -------------------------------------------------------------------------- + + +def row_out(rec: ActionRecord) -> dict[str, Any]: + """生效记录的接口形状(frontend/src/typesApp.ts 的 ActionRecordRow)。预览、回执摘要都是遮罩后的。""" + def iso(moment: datetime | None) -> str | None: + from app.process.tasks import _iso + + return _iso(moment) + + return {"id": rec.id, "run_id": rec.run_id, "node_id": rec.node_id, "app_id": rec.app_id, + "capability": rec.capability, "environment": rec.environment, "state": rec.state, "attempt": rec.attempt, + "payload_hash": rec.payload_hash, "preview": rec.preview, "receipt": rec.receipt, "error": rec.error, + "resolution": rec.resolution, "idem_key": rec.idem_key, "endpoint_id": rec.endpoint_id, + "workflow_id": rec.workflow_id, "workflow_version": rec.workflow_version, + "capability_version": rec.capability_version, "request_at": iso(rec.request_at), + "response_status": rec.response_status, "lookup_attempts": rec.lookup_attempts, + "confirm_task_id": rec.confirm_task_id, + "created_at": iso(rec.created_at), "updated_at": iso(rec.updated_at)} + + +async def describe(action_id: str) -> dict[str, Any] | None: + async with SessionLocal() as session: + rec = await session.get(ActionRecord, action_id) + return row_out(rec) if rec is not None else None + + +def environment_label(env: str) -> str: + return _ENV_LABEL.get(env, env) diff --git a/backend/app/process/events.py b/backend/app/process/events.py new file mode 100644 index 00000000..b3e5a9d0 --- /dev/null +++ b/backend/app/process/events.py @@ -0,0 +1,35 @@ +"""待办、动作在运行之外记的事件(task.* / action.*)。 + +这些事件大多发生在运行停着的时候(有人回复、到期升级、核实),也有发生在执行当中的(开待办、动作挂起)。 +停着的运行要接在库里最后一条后面写(engine/run_append.py:进程重启过内存里的序号从 0 起,直接取号会撞号); +执行当中的交给 run_manager.note,和执行任务共用一个序号计数。已经结束、封存的运行不再写:事件会落在封存范围外, +又不是批注。 +""" +from __future__ import annotations + +from typing import Any + +from sqlalchemy import select + +from app.core.events import EventType +from app.db.base import SessionLocal +from app.db.models import Run + + +async def record(run_id: str, event_type: EventType, *, node_id: str | None = None, **data: Any) -> bool: + """往运行里记一条事件。写进去了返回 True;运行已经结束(或不存在)返回 False。""" + from app.engine.run_append import append_events + from app.engine.runner import run_manager + + async with SessionLocal() as session: + status = await session.scalar(select(Run.status).where(Run.id == run_id)) + if status == "interrupted": + if await append_events(run_id, [(event_type, node_id, data)], require="interrupted") is not None: + return True + # 读完状态之后运行被恢复了:落到下面按执行当中的记 + async with SessionLocal() as session: + status = await session.scalar(select(Run.status).where(Run.id == run_id)) + if status in ("running", "queued") or run_manager.is_active(run_id): + await run_manager.note(run_id, event_type, node_id=node_id, **data) + return True + return False diff --git a/backend/app/process/hooks.py b/backend/app/process/hooks.py index 86aa8677..bb1479bc 100644 --- a/backend/app/process/hooks.py +++ b/backend/app/process/hooks.py @@ -1,12 +1,52 @@ """入站签名钩子(附录 C,BE-2 提供)。签名同出站通用格式:HMAC-SHA256(f"{ts}." + body),时间窗 5 分钟, -Idempotency-Key 去重。start 按入口发起实例;signal 按 correlation 解决一条「等外部系统」的待办。""" +Idempotency-Key 去重。start 按入口发起实例;signal 按 correlation 解决一条「等外部系统」的待办。 + +请求头: +- X-AgentLab-Timestamp:秒级 Unix 时间戳; +- X-AgentLab-Signature:sha256=<十六进制签名>,签的是「时间戳.请求体」的原始字节; +- Idempotency-Key:同一个键只处理一次。处理成功的,再来同一个键直接交回上次的结果;被拒的(表单不对、没有在等的待办) + 可以用同一个键重试——外部系统多半会原样重发,拒过一次就永远拒,它只能换键,去重也就落空了。处理中途服务停了的 + (回执停在 processing 超过 STALE_S),同一个键可以重新认领:start 先找这个键已经发起的实例,signal 先找这个关联键 + 已经解决的待办,找到就交回它,不重复发起、不重复回调。 + +幂等键不在签名串里(签名和出站通用格式是同一套),所以同一个签名只认一个幂等键(_bind_signature):截获一个合法请求、 +在时间窗里换个幂等键重放,签名对得上也拒。原样重发(同一个签名、同一个键)照常按幂等键交回上次的结果。 + +signal 的请求体:{"correlation": 关联键, "decision"?: "approve" | "reject", "note"?: …, 其余字段原样交给节点}。关联键 +也可以放在 X-AgentLab-Correlation 头里。 + +钩子的增删放在 /api/apps/{id}/hooks(应用包的接口),这里给 create_hook / list_hooks / delete_hook 三个函数; +密钥只在新建时交回一次,库里存密文。 +""" from __future__ import annotations +import hmac +import json +import logging +import secrets +import time from dataclasses import dataclass from typing import Any +from sqlalchemy import select, update +from sqlalchemy.exc import IntegrityError +from sqlalchemy.ext.asyncio import AsyncSession + +from app.db.base import SessionLocal, utcnow +from app.db.models import AppHook, HookReceipt, Task + +logger = logging.getLogger(__name__) + #: 签名时间窗(秒) WINDOW_S = 300 +#: 回执停在 processing 超过这么久(秒),同一个幂等键可以重新认领:处理一次请求用不了这么久,多半是中途服务停了 +STALE_S = 600 +#: 「这个签名认了哪个幂等键」的记录也放在回执表里(idem_key 是这个前缀加时间戳和签名,detail 是认的幂等键):同一个 +#: 唯一约束管两件事,不另起表;过了两个时间窗就删 +SIGNATURE_PREFIX = "sig:" +IDEMPOTENCY_HEADER = "Idempotency-Key" +CORRELATION_HEADER = "X-AgentLab-Correlation" +KINDS = ("start", "signal") @dataclass(frozen=True) @@ -23,6 +63,239 @@ class HookRejected(Exception): self.status_code = status_code +def _header(headers: Any, name: str) -> str: + """请求头不分大小写(Starlette 的 Headers 本来就不分;普通 dict 按小写再找一遍)。""" + value = headers.get(name) + if value is None and isinstance(headers, dict): + lowered = {str(k).lower(): v for k, v in headers.items()} + value = lowered.get(name.lower()) + return str(value or "").strip() + + def verify(secret: str, headers: dict[str, str], body: bytes) -> None: """签名不对、时间窗外抛 HookRejected。""" - raise NotImplementedError("BE-2") + from app.notify.core import SIGNATURE_HEADER, TIMESTAMP_HEADER, sign + + ts = _header(headers, TIMESTAMP_HEADER) + given = _header(headers, SIGNATURE_HEADER).removeprefix("sha256=") + if not ts or not given: + raise HookRejected("请求缺少签名,请按通用格式在请求头里带上时间戳和签名") + try: + moment = int(ts) + except ValueError: + raise HookRejected("签名的时间戳不是秒级的 Unix 时间戳") from None + if abs(time.time() - moment) > WINDOW_S: + raise HookRejected("签名的时间戳超出 5 分钟的时间窗,请用当前时间重新签名") + if not hmac.compare_digest(sign(secret, ts, body), given): + raise HookRejected("签名不正确") + + +async def receive(hook_id: str, headers: Any, body: bytes) -> dict[str, Any]: + """处理一次入站请求:认钩子 → 验签 → 按幂等键认领 → start 发起实例 / signal 解决待办。 + + 返回 {status: accepted, kind, run_id?, task_id?, duplicate?}。被拒抛 HookRejected(带 HTTP 状态码);入口表单校验 + 不通过抛 apps.entries.FormRejected。""" + from app.core.crypto import decrypt + + async with SessionLocal() as session: + hook = await session.get(AppHook, hook_id) + if hook is None or not hook.enabled: + raise HookRejected("钩子不存在或已停用", status_code=404) + secret = decrypt(hook.secret) or "" + if not secret: + raise HookRejected("钩子的密钥无法解密(加密密钥可能已更换),请在应用里重新生成钩子", status_code=409) + verify(secret, headers, body) + idem_key = _header(headers, IDEMPOTENCY_HEADER)[:200] + if not idem_key: + raise HookRejected(f"请求缺少 {IDEMPOTENCY_HEADER} 请求头:同一件事重发时要带同一个键", status_code=400) + try: + payload = json.loads(body or b"{}") + except ValueError: + raise HookRejected("请求体不是 JSON", status_code=400) from None + if not isinstance(payload, dict): + raise HookRejected("请求体需要是一个 JSON 对象", status_code=400) + + from app.notify.core import SIGNATURE_HEADER, TIMESTAMP_HEADER + + await _bind_signature(hook.id, _header(headers, TIMESTAMP_HEADER), + _header(headers, SIGNATURE_HEADER).removeprefix("sha256="), idem_key) + previous, reclaimed = await _claim(hook.id, idem_key) + if previous is not None: + return previous + request = HookRequest(hook_id=hook.id, kind=hook.kind, idem_key=idem_key, payload=payload) + try: + if hook.kind == "start": + out = await _start(hook, request, reclaimed=reclaimed) + else: + out = await _signal(hook, request, _header(headers, CORRELATION_HEADER), reclaimed=reclaimed) + except Exception as e: + await _finish(hook.id, idem_key, status="rejected", detail=str(e)[:500]) + raise + await _finish(hook.id, idem_key, status="accepted", run_id=out.get("run_id"), task_id=out.get("task_id")) + return {"status": "accepted", "kind": hook.kind, **out} + + +async def _bind_signature(hook_id: str, ts: str, signature: str, idem_key: str) -> None: + """同一个签名只认一个幂等键(见模块说明)。头一回见到这个签名就记下它认的键;再见到时键对不上就拒。顺带删掉过了 + 两个时间窗的记录:时间窗外的签名验签那一步就拒了,记着没用。""" + from datetime import timedelta + + from sqlalchemy import delete + + key = f"{SIGNATURE_PREFIX}{ts}.{signature}"[:200] + async with SessionLocal() as session: + await session.execute(delete(HookReceipt).where( + HookReceipt.hook_id == hook_id, HookReceipt.status == "signature", + HookReceipt.received_at < utcnow() - timedelta(seconds=2 * WINDOW_S))) + session.add(HookReceipt(hook_id=hook_id, idem_key=key, status="signature", detail=idem_key)) + try: + await session.commit() + return + except IntegrityError: + await session.rollback() + bound = await session.scalar(select(HookReceipt.detail).where(HookReceipt.hook_id == hook_id, + HookReceipt.idem_key == key)) + if bound != idem_key: + raise HookRejected("这个签名已经用过:同一个请求重发时要带同一个幂等键,新的请求请重新签名") + + +async def _claim(hook_id: str, idem_key: str) -> tuple[dict[str, Any] | None, bool]: + """按 (钩子, 幂等键) 认领这一次,交回 (上次的结果, 是不是重新认领了一条停在处理中的)。处理成功过的交回上次的结果 + (duplicate: true);被拒过的重新认领;正在处理的 409,停在处理中超过 STALE_S 的重新认领(条件 UPDATE,并发的两次 + 只有一次认领得到)。""" + from datetime import timedelta + + async with SessionLocal() as session: + session.add(HookReceipt(hook_id=hook_id, idem_key=idem_key, status="processing")) + try: + await session.commit() + return None, False + except IntegrityError: + await session.rollback() + row = await session.scalar(select(HookReceipt).where(HookReceipt.hook_id == hook_id, + HookReceipt.idem_key == idem_key)) + if row.status == "accepted": + return {"status": "accepted", "duplicate": True, "run_id": row.run_id, "task_id": row.task_id}, False + stale = row.status == "processing" and row.received_at < utcnow() - timedelta(seconds=STALE_S) + if row.status == "rejected" or stale: + again = await session.execute(update(HookReceipt).where( + HookReceipt.id == row.id, HookReceipt.status == row.status, + HookReceipt.received_at == row.received_at) + .values(status="processing", detail=None, received_at=utcnow())) + await session.commit() + if again.rowcount == 1: + return None, stale + raise HookRejected("同一个幂等键的请求正在处理,请稍后再查", status_code=409) + + +async def _finish(hook_id: str, idem_key: str, *, status: str, detail: str | None = None, + run_id: str | None = None, task_id: str | None = None) -> None: + async with SessionLocal() as session: + await session.execute(update(HookReceipt).where(HookReceipt.hook_id == hook_id, + HookReceipt.idem_key == idem_key) + .values(status=status, detail=detail, run_id=run_id, task_id=task_id)) + await session.commit() + + +def _caller(hook: AppHook) -> Any: + """钩子发起、回复时的身份:签名认过的外部系统。没有用户名(发起人一栏留空),名字写钩子名,审计里记 verified。""" + from app.api.deps import Principal, auth_mode + + return Principal(username=None, role="admin", display_name=f"外部系统({hook.name})", verified=True, + mode=auth_mode()) + + +async def _start(hook: AppHook, request: HookRequest, *, reclaimed: bool = False) -> dict[str, Any]: + from app.apps.entries import start_entry + + if not hook.entry_id: + raise HookRejected("这个钩子没有指定入口,无法发起", status_code=409) + if reclaimed and (run_id := await _started_before(hook, request.idem_key)): + # 上一次处理到一半服务停了:实例其实已经发起,交回它,不再发起一个 + return {"run_id": run_id} + run = await start_entry(hook.app_id, hook.entry_id, request.payload, _caller(hook), origin_kind="hook", + hook=hook.name, hook_key=request.idem_key) + return {"run_id": run.id} + + +async def _started_before(hook: AppHook, idem_key: str) -> str | None: + """这个钩子按这个幂等键发起过的实例(来源里记着 hook 和 hook_key)。""" + from app.db.models import Run + + async with SessionLocal() as session: + rows = (await session.execute(select(Run.id, Run.origin).where( + Run.app_id == hook.app_id, Run.origin_kind == "hook").order_by(Run.created_at.desc()).limit(500))).all() + for run_id, origin in rows: + if isinstance(origin, dict) and origin.get("hook") == hook.name and origin.get("hook_key") == idem_key: + return run_id + return None + + +async def _signal(hook: AppHook, request: HookRequest, header_key: str, *, reclaimed: bool = False) -> dict[str, Any]: + from app.process.tasks import RawResponse, respond + + key = str(request.payload.get("correlation") or header_key or "").strip() + if not key: + raise HookRejected("请求里没有关联键(correlation)", status_code=400) + async with SessionLocal() as session: + task_id = await session.scalar(select(Task.id).where( + Task.app_id == hook.app_id, Task.kind == "signal", Task.status == "pending", + Task.correlation_key == key).order_by(Task.created_at).limit(1)) + if task_id is None and reclaimed: + # 上一次处理到一半服务停了:在等的待办其实已经解决,交回它,不当成没人在等 + done = (await session.execute(select(Task.id, Task.run_id).where( + Task.app_id == hook.app_id, Task.kind == "signal", Task.status != "pending", + Task.correlation_key == key).order_by(Task.created_at.desc()).limit(1))).first() + if done is not None: + return {"task_id": done[0], "run_id": done[1]} + if task_id is None: + raise HookRejected("没有在等这个关联键的待办(可能已处理过,或还没走到这一步)", status_code=404) + decision = str(request.payload.get("decision") or "approve").lower() + value = {k: v for k, v in request.payload.items() if k not in ("correlation", "decision", "note")} + response = {"approved": decision not in ("reject", "rejected"), "note": str(request.payload.get("note") or ""), + "value": value, "via": "hook", "hook": hook.name} + out = await respond(task_id, _caller(hook), decision=RawResponse(response)) + return {"task_id": task_id, "run_id": getattr(out.get("run"), "id", None)} + + +# -------------------------------------------------------------------------- +# 钩子的增删(给 /api/apps/{id}/hooks 用) +# -------------------------------------------------------------------------- + + +def hook_out(row: AppHook) -> dict[str, Any]: + return {"id": row.id, "app_id": row.app_id, "name": row.name, "kind": row.kind, "entry_id": row.entry_id, + "enabled": row.enabled, "url_path": f"/api/hooks/{row.id}", + "created_at": row.created_at.isoformat() if row.created_at else None} + + +async def create_hook(session: AsyncSession, app_id: str, *, name: str, kind: str, + entry_id: str | None = None) -> tuple[AppHook, str]: + """新建一个钩子,返回 (钩子, 密钥明文)。密钥只在这一次交回,库里存密文。调用方提交。""" + from app.core.crypto import encrypt + + if kind not in KINDS: + raise ValueError("钩子只能是发起实例或回调待办两种") + if kind == "start" and not entry_id: + raise ValueError("发起实例的钩子需要指定入口") + secret = secrets.token_urlsafe(32) + row = AppHook(app_id=app_id, name=name.strip()[:100] or kind, kind=kind, + entry_id=entry_id if kind == "start" else None, secret=encrypt(secret), enabled=True) + session.add(row) + await session.flush() + return row, secret + + +async def list_hooks(session: AsyncSession, app_id: str) -> list[dict[str, Any]]: + rows = (await session.execute(select(AppHook).where(AppHook.app_id == app_id) + .order_by(AppHook.created_at))).scalars() + return [hook_out(r) for r in rows] + + +async def delete_hook(session: AsyncSession, app_id: str, hook_id: str) -> bool: + """删掉一个钩子(去重记录随外键一起删)。不是这个应用的、不存在的返回 False。调用方提交。""" + row = await session.get(AppHook, hook_id) + if row is None or row.app_id != app_id: + return False + await session.delete(row) + return True diff --git a/backend/app/process/tasks.py b/backend/app/process/tasks.py index 54a0a61c..8cf3a2e0 100644 --- a/backend/app/process/tasks.py +++ b/backend/app/process/tasks.py @@ -2,14 +2,49 @@ 一次中断对应一条 Approval(给引擎)和一条 Task(给人)。所有人的决定都经 respond:按 policy 汇总,凑齐后在同一个 事务里用条件 UPDATE 把 Task 从 pending 改成 resolving,抢到的请求负责调 run_manager.resume。引擎和重放都不改。 + +- 种类按中断内容的 kind 认(人工节点按它的 mode),不看 Approval.mode:工具审批的中断里没有 mode,Approval.mode 一律是 approve。 +- 派给谁:人工节点(和写入动作的调用工具节点)的 assignee。岗位按应用版本的 bindings 展开到人或组;派给组时在打开这一刻 + 展开成当时的成员(快照,写审计);没有应用绑定的运行(画布、定时任务)派给「任何人」,行为和以前一样。 +- 会签只用于批准 / 驳回这一类:all 一票否决,quorum(m) 凑够 m 票通过、驳回超过 n − m 票否决。升级加派的那一份 + (source=escalation)一人说了算:升级就是让上级拍板,不是再多一个要等的人。 +- 关登录时谁都能回复(「都算」),回复人记自报的署名。开了登录:派给人的认用户名,派给组的认组,派给「任何人」的要 + builder 以上(这类待办来自画布、定时任务,是给搭建者看的),等外部系统的只有管理员能代为处理。 +- tick:提醒、升级(加派一份,escalation_level+1,只升一次)、超时驳回。永不自动批准。 """ from __future__ import annotations +import logging +from dataclasses import dataclass, replace from datetime import datetime from typing import Any +from sqlalchemy import and_, exists, func, or_, select, update from sqlalchemy.ext.asyncio import AsyncSession +from app.core.events import EventType +from app.db.base import SessionLocal, utcnow +from app.db.models import ( + App, Approval, AppVersion, AuditEvent, Group, GroupMember, Run, Task, TaskAssignment, User, +) +from app.engine.lint_process import normalize_policy, parse_duration +from app.process.events import record + +logger = logging.getLogger(__name__) + +#: 中断内容的 kind → 待办种类。人工节点(human_node)按 mode,派给外部系统的是 signal +_KIND_OF = {"tool_approval": "tool", "code_approval": "code", "dual_check": "dual_check", + "action_confirm": "action_confirm"} +#: 结论只有批准 / 驳回两种、按会签策略汇总的种类。补填、改稿、双盲裁决、等外部系统只认第一份回复 +VOTING = ("approve", "tool", "code", "action_confirm") +#: 核实待办的三种结论(同 actions.reconcile 的 outcome) +RECONCILE_OUTCOMES = ("confirmed", "not_applied_resend", "not_applied_abandon") +#: 超时驳回交给节点的那句备注 +EXPIRED_NOTE = "超过时限无人处理,系统已自动驳回" +_TERMINAL_RUN = ("succeeded", "failed", "cancelled") +_STATUS_TEXT = {"resolving": "正在处理", "resolved": "已处理", "cancelled": "已关闭(所属的运行已结束)", + "expired": "已过期"} + class TaskRefused(Exception): """回复不被接受:不是派给你的、已经处理过了、哈希对不上……status_code 照 HTTP 语义(403 / 409 / 422)。""" @@ -20,21 +55,851 @@ class TaskRefused(Exception): self.code = code +class FormInvalid(TaskRefused): + """填写的值没有通过表单校验(422)。errors 的形状同 agentlab.form/1:{path, code, message}。""" + + def __init__(self, errors: list[dict[str, Any]]) -> None: + first = errors[0].get("message") if errors else "" + super().__init__(f"表单没有通过校验:{first}" if first else "表单没有通过校验", status_code=422, + code="form_invalid") + self.errors = errors + + +@dataclass(frozen=True) +class RawResponse: + """老接口(/api/approvals/{id}/decide、/api/runs/{id}/resume)带来的原样回复:「始终允许」、改过的代码、双盲裁决的 + 选择都在里面,照原样交给节点。只当一票,结论按 read_decision 认。""" + + response: Any + + +def kind_of(payload: dict[str, Any]) -> str: + kind = payload.get("kind") + if kind == "human_node": + if (payload.get("assignee") or {}).get("kind") == "hook": + return "signal" + mode = payload.get("mode") or "approve" + return mode if mode in ("approve", "input", "edit") else "approve" + return _KIND_OF.get(str(kind), "approve") + + +# -------------------------------------------------------------------------- +# 打开 +# -------------------------------------------------------------------------- + + async def open_task(session: AsyncSession, approval: Any) -> Any: - """为一条新的 Approval 建 Task 和派给谁(按人工节点配置 + 应用版本的岗位绑定)。runner._handle_updates 调。""" - raise NotImplementedError("BE-2") + """为一条新的 Approval 建 Task 和派给谁(按人工节点配置 + 应用版本的岗位绑定)。runner._handle_updates 调。 + + 和 Approval 同一个事务,调用方提交;task.opened 和通知在提交之后由 announce 发。同一条 Approval 已经有待办的直接返回。""" + existing = await session.scalar(select(Task).where(Task.approval_id == approval.id)) + if existing is not None: + return existing + payload = approval.payload or {} + run = await session.get(Run, approval.run_id) + kind = kind_of(payload) + policy = (normalize_policy(payload.get("policy")) or {"kind": "any"}) if kind in VOTING else {"kind": "any"} + now = utcnow() + due = parse_duration(payload.get("due")) + timeout = payload.get("on_timeout") if isinstance(payload.get("on_timeout"), dict) else None + due_at = now + due if due else None + task = Task( + approval_id=approval.id, run_id=approval.run_id, node_id=approval.node_id or None, + app_id=run.app_id if run else None, app_version=run.app_version if run else None, + kind=kind, title=str(payload.get("title") or approval.title or "待处理")[:300], + form=payload.get("form") if isinstance(payload.get("form"), dict) else None, + policy=policy, due_at=due_at, + remind_at=due_at if timeout and timeout.get("action") == "remind" else None, + on_timeout=timeout if due_at else None, + correlation_key=(str(payload.get("correlation_key") or "")[:200] or None) if kind == "signal" else None, + ) + session.add(task) + await session.flush() + await _assign(session, task, run, payload.get("assignee")) + return task + + +async def open_reconcile(session: AsyncSession, action: Any, *, title: str) -> Task: + """结果未知、没有登记查询能力的动作:开一条核实待办,派给应用的 reconciler 岗位(没有就派给任何人)。 + 核实待办没有 Approval,按 correlation_key = action: 找回动作。""" + run = await session.get(Run, action.run_id) + task = Task(run_id=action.run_id, node_id=action.node_id, app_id=run.app_id if run else None, + app_version=run.app_version if run else None, kind="reconcile", title=title[:300], + policy={"kind": "any"}, correlation_key=f"action:{action.id}") + session.add(task) + await session.flush() + await _assign(session, task, run, {"kind": "slot", "slot": "reconciler"}) + return task + + +async def _assign(session: AsyncSession, task: Task, run: Run | None, assignee: Any, *, + source: str | None = None) -> list[TaskAssignment]: + rows = [] + for kind, name, origin in await _targets(session, task, run, assignee): + row = TaskAssignment(task_id=task.id, principal_kind=kind, principal=name, source=source or origin) + session.add(row) + rows.append(row) + await session.flush() + return rows + + +async def _targets(session: AsyncSession, task: Task, run: Run | None, assignee: Any) -> list[tuple[str, str, str]]: + """(principal_kind, principal, source) 列表,去重。""" + spec = assignee if isinstance(assignee, dict) else {} + kind = spec.get("kind") + out: list[tuple[str, str, str]] = [] + if kind == "slot": + slot = str(spec.get("slot") or "") + bound = await _slot_binding(session, run, slot) + if bound: + for item in bound: + out += await _expand(session, task, str(item.get("kind") or "user"), str(item.get("name") or ""), + f"slot:{slot}") + else: + out.append(("anyone", "", f"slot:{slot}")) + elif kind == "initiator": + out.append(("user", run.started_by, "initiator") if run is not None and run.started_by + else ("anyone", "", "initiator")) + elif kind == "expression": + for name in spec.get("names") or []: + is_group = await session.scalar(select(Group.id).where(Group.name == str(name))) + out += await _expand(session, task, "group" if is_group else "user", str(name), "expression") + elif kind == "hook": + out.append(("hook", "", "hook")) + out = [t for t in out if t[1] or t[0] in ("anyone", "hook")] + return list(dict.fromkeys(out)) or [("anyone", "", "")] + + +async def _slot_binding(session: AsyncSession, run: Run | None, slot: str) -> list[dict[str, Any]] | None: + """应用版本里这个岗位绑定的人或组。没有应用(画布、定时任务)或岗位没绑定:None(派给任何人)。""" + if run is None or not run.app_id or run.app_version is None or not slot: + return None + bindings = await session.scalar(select(AppVersion.bindings).where( + AppVersion.app_id == run.app_id, AppVersion.version == run.app_version)) + bound = ((bindings or {}).get("slots") or {}).get(slot) + return [b for b in bound if isinstance(b, dict)] if isinstance(bound, list) else None + + +async def _expand(session: AsyncSession, task: Task, kind: str, name: str, source: str) -> list[tuple[str, str, str]]: + """组在这一刻展开成成员(快照):之后加入组的人不会补派。组里一个人都没有时仍派给组,开了登录后组里的人能处理。""" + if kind != "group": + return [("user", name, source)] + members = list((await session.execute( + select(User.username).join(GroupMember, GroupMember.user_id == User.id) + .join(Group, Group.id == GroupMember.group_id) + .where(Group.name == name, User.disabled.is_(False)).order_by(User.username))).scalars()) + if not members: + return [("group", name, source)] + session.add(AuditEvent(actor=None, verified=False, action="task.group_expanded", subject_kind="task", + subject_id=task.id, data={"group": name, "members": members, "source": source})) + return [("user", m, source) for m in members] + + +async def announce(task_id: str, event: EventType = EventType.TASK_OPENED) -> None: + """待办提交之后:记 task.opened、按订阅推送通知。出错只记日志:开待办这件事已经成了。""" + try: + async with SessionLocal() as session: + task = await session.get(Task, task_id) + if task is None: + return + rows = await _assignments(session, [task.id]) + await record(task.run_id, event, node_id=task.node_id, task_id=task.id, kind=task.kind, title=task.title, + assignees=[_who(a) for a in rows.get(task.id, [])], policy=task.policy, + due_at=_iso(task.due_at)) + except Exception: # noqa: BLE001 + logger.exception("待办 %s 打开后的事件没能记下", task_id) + return + from app.notify.service import spawn + + # 推送放到后台:接收端挂住时超时加重试要几十秒,不能让运行停在「执行中」等它 + spawn(_notify(task_id, "task.opened", [a.principal for a in rows.get(task_id, []) if a.principal_kind == "user"])) + + +# -------------------------------------------------------------------------- +# 回复 +# -------------------------------------------------------------------------- + + +def _matches(a: TaskAssignment, principal: Any) -> bool: + if a.status != "pending": + return False + if getattr(principal, "mode", "off") == "off": + return True + if a.principal_kind == "user": + return a.principal == principal.username + if a.principal_kind == "group": + return a.principal in (principal.groups or ()) + if a.principal_kind == "anyone": + return principal.at_least("builder") + return a.principal_kind == "hook" and principal.at_least("admin") + + +def _pick(task: Task, rows: list[TaskAssignment], principal: Any) -> TaskAssignment: + """这个人回复的是哪一份。同一个人在一条会签待办上只能投一票(关登录时按自报的署名认,没署名的认不出)。""" + name = principal.username + if name and task.kind in VOTING and any(a.status == "responded" and a.responded_by == name for a in rows): + raise TaskRefused("你已经回复过这条待办,正在等其他人处理", status_code=409, code="task_already_responded") + candidates = [a for a in rows if _matches(a, principal)] + if not candidates: + if not any(a.status == "pending" for a in rows): + raise TaskRefused("这条待办已经没有待回复的人,请刷新页面查看最新状态", status_code=409) + raise TaskRefused("这条待办没有派给你", status_code=403, code="task_not_assigned") + # 派给自己的那一份优先,其次是组,最后是「任何人」 + order = {"user": 0, "group": 1, "hook": 2, "anyone": 3} + candidates.sort(key=lambda a: (0 if a.principal == name and a.principal_kind == "user" else 1, + order.get(a.principal_kind, 9))) + return candidates[0] + + +def _closed(status: str) -> str: + return f"这条待办{_STATUS_TEXT.get(status, '已处理')},请刷新页面查看最新状态" + + +def _verdict_of_raw(task: Task, response: Any) -> str: + from app.engine.approval import read_decision + + decision = read_decision(response) + if task.kind in VOTING: + return "approve" if decision.approved else "reject" + # 补填、改稿只有明说了「不」才算驳回:那两种模式下裸字符串就是内容本身(同 human 节点) + return "reject" if isinstance(response, dict) and decision.rejected else "submit" + + +def _engine_response(task: Task, approval: Approval | None, decision: Any, values: dict[str, Any] | None, + note: str | None, payload_hash: str | None) -> tuple[Any, str]: + """(交给节点的回复, 这一票的结论 approve / reject / submit)。""" + card = (approval.payload if approval is not None else None) or {} + if isinstance(decision, RawResponse): + response = decision.response + if task.kind == "action_confirm": + given = response.get("payload_hash") if isinstance(response, dict) else None + _check_hash(card, given, approving=_verdict_of_raw(task, response) == "approve") + return response, _verdict_of_raw(task, response) + + values = dict(values or {}) + text = note or "" + if task.kind in VOTING: + if decision not in ("approve", "reject"): + raise TaskRefused("这条待办只能批准或驳回", status_code=422, code="task_decision_invalid") + out: dict[str, Any] = {"approved": decision == "approve", "note": text} + if task.kind == "tool": + if isinstance(values.get("args"), dict): + out["args"] = values["args"] + if values.get("always") is True: + out["always"] = True + elif task.kind == "code" and isinstance(values.get("code"), str) and values["code"]: + out["code"] = values["code"] + elif task.kind == "action_confirm": + _check_hash(card, payload_hash, approving=decision == "approve") + out["payload_hash"] = payload_hash + elif task.form and decision == "approve" and values: + out["value"] = _validated(task.form, values) + return out, decision + + if decision not in ("submit", "approve", "reject"): + raise TaskRefused("这条待办只能提交或驳回", status_code=422, code="task_decision_invalid") + if decision == "reject": + return {"approved": False, "note": text}, "reject" + if task.form: + value: Any = _validated(task.form, values) + elif set(values) == {"value"}: + value = values["value"] # 老写法:一个输入框的内容 + else: + value = values or None + return {"approved": True, "note": text, "value": value}, "submit" + + +def _check_hash(card: dict[str, Any], given: Any, *, approving: bool) -> None: + """动作确认按引用确认:确认卡上的哈希原样带回来,和这张卡对得上才算数(驳回不用带)。""" + if not approving: + return + if not given: + raise TaskRefused("确认发送需要带上确认卡上的内容指纹,请刷新页面后重新确认", status_code=422, + code="payload_hash_missing") + if given != card.get("payload_hash"): + raise TaskRefused("你确认的内容和现在待发送的不一致(内容指纹对不上),请刷新页面查看最新的确认卡", + status_code=422, code="payload_hash_mismatch") + + +def _validated(form: dict[str, Any], values: dict[str, Any]) -> dict[str, Any]: + from app.apps.forms import validate_form + + if errors := validate_form(form, values): + raise FormInvalid([dict(e) for e in errors]) + return values + + +def _tally(task: Task, rows: list[TaskAssignment], mine: TaskAssignment, verdict: str) -> str | None: + """凑齐了没有:返回结论(approve / reject / submit),还要等别人返回 None。""" + policy = task.policy or {"kind": "any"} + if task.kind not in VOTING or policy.get("kind", "any") == "any" or mine.source == "escalation": + return verdict + voters = [a for a in rows if a.status != "withdrawn" and a.source != "escalation"] + n = len(voters) + said = [((a.response or {}).get("decision")) for a in voters if a.status == "responded"] + yes, no = said.count("approve"), said.count("reject") + if policy["kind"] == "all": + return "reject" if no else "approve" if yes >= n else None + m = min(int(policy.get("m") or 1), n) + return "approve" if yes >= m else "reject" if no > n - m else None + + +def _signoffs(rows: list[TaskAssignment]) -> list[dict[str, Any]]: + return [{"by": a.responded_by, "decision": (a.response or {}).get("decision"), + "note": (a.response or {}).get("note") or "", "at": _iso(a.responded_at), "verified": a.verified, + "source": a.source} + for a in sorted((a for a in rows if a.status == "responded"), key=lambda a: a.responded_at or utcnow())] async def respond(task_id: str, principal: Any, *, decision: Any, values: dict[str, Any] | None = None, note: str | None = None, payload_hash: str | None = None) -> dict[str, Any]: - """一个人的回复。返回 {task, resolved: bool, run?}。""" - raise NotImplementedError("BE-2") + """一个人的回复。返回 {task, resolved: bool, run?}。 + + decision:approve / reject(批准类)、submit / reject(补填、改稿、双盲裁决)、核实待办的三种结论;老接口传 + RawResponse。values:表单的值,工具审批改过的参数(args)、「始终允许」(always),代码审批改过的代码(code), + 核实「已生效」时的外部单号(external_id)。""" + from app.engine.inquiry import BUSY_DECIDE, INQUIRY_BUSY, inquiry_busy + + async with SessionLocal() as session: + task = await session.get(Task, task_id) + if task is None: + raise TaskRefused("待办不存在,所属的运行可能已被删除", status_code=404) + if task.kind == "reconcile": + return await _respond_reconcile(session, task, principal, decision, values, note) + if task.status != "pending": + raise TaskRefused(_closed(task.status)) + if inquiry_busy(task.run_id): + raise TaskRefused(BUSY_DECIDE, code=INQUIRY_BUSY) + approval = await session.get(Approval, task.approval_id) if task.approval_id else None + rows = (await _assignments(session, [task.id])).get(task.id, []) + mine = _pick(task, rows, principal) + response, verdict = _engine_response(task, approval, decision, values, note, payload_hash) + now = utcnow() + marked = await session.execute( + update(TaskAssignment).where(TaskAssignment.id == mine.id, TaskAssignment.status == "pending") + .values(status="responded", responded_by=principal.username, responded_at=now, + verified=bool(principal.verified), response={"decision": verdict, "note": note or ""})) + if marked.rowcount != 1: + await session.rollback() + raise TaskRefused("这一份已经有人回复过,请刷新页面查看最新状态") + rows = (await _assignments(session, [task.id], fresh=True)).get(task.id, []) + mine = next(a for a in rows if a.id == mine.id) + decided = _tally(task, rows, mine, verdict) + voters = [a for a in rows if a.status != "withdrawn"] + responded = sum(1 for a in voters if a.status == "responded") + final: Any = None + if decided is not None: + final = response + if task.kind in VOTING and (task.policy or {}).get("kind", "any") != "any": + # 会签:交给节点的是汇总的结论和明细,human.resolved 里写得出谁、怎么决定的 + signoffs = _signoffs(rows) + final = {"approved": decided == "approve", + "note": ";".join(s["note"] for s in signoffs if s["note"]), + "signoffs": signoffs, "policy": task.policy} + claimed = await session.execute( + update(Task).where(Task.id == task.id, Task.status == "pending") + .values(status="resolving", resolved_by=principal.username, + outcome={"decision": decided, "by_assignment": mine.id, "signoffs": _signoffs(rows)})) + if claimed.rowcount != 1: + await session.rollback() + raise TaskRefused(_closed("resolving")) + await session.commit() + run_id, approval_id, node_id = task.run_id, task.approval_id, task.node_id + + await record(run_id, EventType.TASK_RESPONDED, node_id=node_id, task_id=task_id, actor=principal.username, + verified=bool(principal.verified), decision=verdict, index=responded, of=len(voters)) + if decided is None: + return {"task": await detail(task_id, principal), "resolved": False, "run": await _run(run_id)} + + from app.engine.runner import run_manager + + try: + run = await run_manager.resume(run_id, final, approval_id=approval_id, actor=principal.username) + except (ValueError, KeyError) as e: + await _unclaim(task_id, mine.id) + raise TaskRefused(str(e.args[0]) if e.args else str(e)) from e + await _settle(task_id) + await record(run_id, EventType.TASK_RESOLVED, node_id=node_id, task_id=task_id, decision=decided, + actor=principal.username, signoffs=_signoffs(rows)) + return {"task": await detail(task_id, principal), "resolved": True, "run": run} + + +async def _respond_reconcile(session: AsyncSession, task: Task, principal: Any, decision: Any, + values: dict[str, Any] | None, note: str | None) -> dict[str, Any]: + if task.status != "pending": + raise TaskRefused(_closed(task.status)) + if decision not in RECONCILE_OUTCOMES: + raise TaskRefused("核实只能选已生效、未生效重新发送、未生效放弃之一", status_code=422, + code="task_decision_invalid") + rows = (await _assignments(session, [task.id])).get(task.id, []) + _pick(task, rows, principal) + action_id = (task.correlation_key or "").removeprefix("action:") + task_id = task.id + await session.rollback() + from app.process.actions import reconcile + + out = await reconcile(action_id, principal, outcome=decision, + external_id=str((values or {}).get("external_id") or "") or None, note=note) + return {"task": await detail(task_id, principal), "resolved": True, "run": out.get("run")} + + +async def close_reconcile(session: AsyncSession, action_id: str, principal: Any, outcome: str, + note: str | None) -> str | None: + """核实之后把这个动作的核实待办收掉(条件 UPDATE,和 tick、别人的回复抢同一行)。返回待办 id,没有返回 None。""" + task = await session.scalar(select(Task).where(Task.correlation_key == f"action:{action_id}", + Task.kind == "reconcile", Task.status == "pending")) + if task is None: + return None + now = utcnow() + username = getattr(principal, "username", None) + claimed = await session.execute( + update(Task).where(Task.id == task.id, Task.status == "pending") + .values(status="resolved", resolved_by=username, resolved_at=now, outcome={"decision": outcome})) + if claimed.rowcount != 1: + return None + rows = (await _assignments(session, [task.id])).get(task.id, []) + mine = next((a for a in rows if principal is not None and _matches(a, principal)), None) + if mine is not None: + await session.execute(update(TaskAssignment).where(TaskAssignment.id == mine.id).values( + status="responded", responded_by=username, responded_at=now, + verified=bool(getattr(principal, "verified", False)), response={"decision": outcome, "note": note or ""})) + await session.execute(update(TaskAssignment).where(TaskAssignment.task_id == task.id, + TaskAssignment.status == "pending") + .values(status="withdrawn")) + return task.id + + +async def _unclaim(task_id: str, assignment_id: str) -> None: + """恢复没成:运行已经结束的,待办随之关闭;还停着的,退回 pending,这一票撤回、可以再回复。""" + async with SessionLocal() as session: + task = await session.get(Task, task_id) + status = await session.scalar(select(Run.status).where(Run.id == task.run_id)) if task else None + if task is None: + return + if status in _TERMINAL_RUN or status is None: + task.status = "cancelled" + task.resolved_at = utcnow() + await session.execute(update(TaskAssignment).where(TaskAssignment.task_id == task_id, + TaskAssignment.status == "pending") + .values(status="withdrawn")) + else: + task.status, task.outcome, task.resolved_by = "pending", None, None + await session.execute(update(TaskAssignment).where(TaskAssignment.id == assignment_id) + .values(status="pending", response=None, responded_by=None, responded_at=None, + verified=False)) + await session.commit() + logger.warning("待办 %s 判定后恢复运行没有成功,已%s", task_id, + "随运行关闭" if status in _TERMINAL_RUN else "退回待处理") + + +async def _settle(task_id: str) -> None: + async with SessionLocal() as session: + await session.execute(update(Task).where(Task.id == task_id, Task.status == "resolving") + .values(status="resolved", resolved_at=utcnow())) + await session.execute(update(TaskAssignment).where(TaskAssignment.task_id == task_id, + TaskAssignment.status == "pending") + .values(status="withdrawn")) + await session.commit() + + +async def _run(run_id: str) -> Run | None: + async with SessionLocal() as session: + return await session.get(Run, run_id) + + +# -------------------------------------------------------------------------- +# 改派、催办 +# -------------------------------------------------------------------------- + + +async def _can_manage(session: AsyncSession, task: Task, principal: Any) -> bool: + """改派、催办:关登录或管理员都行;builder 要有这个应用的 manage 授权(或者是应用的创建者);没有应用的待办 builder 就行。""" + if getattr(principal, "mode", "off") == "off" or principal.at_least("admin"): + return True + if not principal.at_least("builder"): + return False + if not task.app_id: + return True + from app.db.models import AppGrant + + app = await session.get(App, task.app_id) + if app is not None and app.created_by and app.created_by == principal.username: + return True + grant = await session.scalar(select(AppGrant.id).where( + AppGrant.app_id == task.app_id, AppGrant.role == "manage", + or_(and_(AppGrant.principal_kind == "user", AppGrant.principal == principal.username), + and_(AppGrant.principal_kind == "group", AppGrant.principal.in_(list(principal.groups or ())))))) + return grant is not None async def reassign(task_id: str, principal: Any, *, to: list[dict[str, str]], note: str | None = None) -> Any: - raise NotImplementedError("BE-2") + """改派:撤回还没回复的那几份,派给新的人或组(组照样按当时的成员展开)。已经回复的票留着。返回待办详情。""" + targets = [(str(t.get("kind") or ""), str(t.get("name") or "").strip()) for t in to or []] + if not targets or any(k not in ("user", "group") or not n for k, n in targets): + raise TaskRefused("改派需要选择至少一位用户或一个组", status_code=422, code="reassign_empty") + async with SessionLocal() as session: + task = await session.get(Task, task_id) + if task is None: + raise TaskRefused("待办不存在,所属的运行可能已被删除", status_code=404) + if task.status != "pending": + raise TaskRefused(_closed(task.status)) + if not await _can_manage(session, task, principal): + raise TaskRefused("只有管理员或这个应用的管理者可以改派待办", status_code=403) + await session.execute(update(TaskAssignment).where(TaskAssignment.task_id == task_id, + TaskAssignment.status == "pending") + .values(status="withdrawn")) + added: list[TaskAssignment] = [] + for kind, name in dict.fromkeys(targets): + for k, n, _ in await _expand(session, task, kind, name, "reassign"): + row = TaskAssignment(task_id=task_id, principal_kind=k, principal=n, source="reassign") + session.add(row) + added.append(row) + await session.commit() + run_id, node_id = task.run_id, task.node_id + await record(run_id, EventType.TASK_REASSIGNED, node_id=node_id, task_id=task_id, actor=principal.username, + to=[_who(a) for a in added], note=note or "") + from app.notify.service import spawn + + spawn(_notify(task_id, "task.reassigned", [a.principal for a in added if a.principal_kind == "user"])) + return await detail(task_id, principal) + + +async def remind(task_id: str, principal: Any) -> dict[str, Any]: + """催办:给还没回复的人再推一次通知。返回 {sent: 推送了几条}。""" + async with SessionLocal() as session: + task = await session.get(Task, task_id) + if task is None: + raise TaskRefused("待办不存在,所属的运行可能已被删除", status_code=404) + if task.status != "pending": + raise TaskRefused(_closed(task.status)) + if not await _can_manage(session, task, principal): + raise TaskRefused("只有管理员或这个应用的管理者可以催办", status_code=403) + rows = (await _assignments(session, [task_id])).get(task_id, []) + users = [a.principal for a in rows if a.status == "pending" and a.principal_kind == "user"] + await record(task.run_id, EventType.TASK_REMINDED, node_id=task.node_id, task_id=task_id, + actor=principal.username, to=[_who(a) for a in rows if a.status == "pending"]) + return {"sent": await _notify(task_id, "task.reminded", users)} + + +async def _notify(task_id: str, event: str, users: list[str]) -> int: + try: + from app.notify.service import notify_task + + return await notify_task(task_id, event, users=users) + except Exception: # noqa: BLE001 - 通知推不出去不影响待办本身 + logger.exception("待办 %s 的通知(%s)没能推送", task_id, event) + return 0 + + +# -------------------------------------------------------------------------- +# 时限:提醒、升级、超时驳回 +# -------------------------------------------------------------------------- async def tick(now: datetime | None = None) -> int: """调度循环每一拍:提醒、升级、超时驳回(永不自动批准)。返回处理的条数。""" - raise NotImplementedError("BE-2") + now = now or utcnow() + action = func.json_extract(Task.on_timeout, "$.action") + async with SessionLocal() as session: + due = list((await session.execute(select(Task.id).where(Task.status == "pending", or_( + and_(Task.remind_at.is_not(None), Task.remind_at <= now), + and_(Task.due_at.is_not(None), Task.due_at <= now, action == "reject"), + and_(Task.due_at.is_not(None), Task.due_at <= now, action == "escalate", Task.escalation_level == 0), + )).order_by(Task.due_at))).scalars()) + done = 0 + for task_id in due: + try: + done += await _on_timeout(task_id, now) + except Exception: # noqa: BLE001 - 一条出错不拖住这一拍的其他待办 + logger.exception("待办 %s 的超时处理失败,下一拍再试", task_id) + return done + + +async def _on_timeout(task_id: str, now: datetime) -> int: + async with SessionLocal() as session: + task = await session.get(Task, task_id) + if task is None or task.status != "pending": + return 0 + timeout = task.on_timeout or {} + action = timeout.get("action") + if action == "remind" and task.remind_at is not None and _aware(task.remind_at) <= now: + every = parse_duration(timeout.get("every")) + claimed = await session.execute(update(Task).where( + Task.id == task_id, Task.status == "pending", Task.remind_at == task.remind_at) + .values(remind_at=now + every if every else None)) + await session.commit() + if claimed.rowcount != 1: + return 0 + rows = (await _assignments(session, [task_id])).get(task_id, []) + pending = [a for a in rows if a.status == "pending"] + await record(task.run_id, EventType.TASK_REMINDED, node_id=task.node_id, task_id=task_id, actor=None, + overdue=True, to=[_who(a) for a in pending]) + from app.notify.service import spawn + + spawn(_notify(task_id, "task.reminded", [a.principal for a in pending if a.principal_kind == "user"])) + return 1 + if action == "escalate" and task.escalation_level == 0: + slot = str(timeout.get("to_slot") or "") + claimed = await session.execute(update(Task).where( + Task.id == task_id, Task.status == "pending", Task.escalation_level == 0) + .values(escalation_level=Task.escalation_level + 1)) + if claimed.rowcount != 1: + await session.rollback() + return 0 + run = await session.get(Run, task.run_id) + added = await _assign(session, task, run, {"kind": "slot", "slot": slot}, source="escalation") + await session.commit() + await record(task.run_id, EventType.TASK_ESCALATED, node_id=task.node_id, task_id=task_id, slot=slot, + level=1, to=[_who(a) for a in added]) + from app.notify.service import spawn + + spawn(_notify(task_id, "task.escalated", [a.principal for a in added if a.principal_kind == "user"])) + return 1 + if action != "reject" or task.due_at is None or _aware(task.due_at) > now: + return 0 + claimed = await session.execute(update(Task).where(Task.id == task_id, Task.status == "pending") + .values(status="resolving", outcome={"decision": "expired_reject"})) + await session.commit() + if claimed.rowcount != 1: + return 0 + run_id, approval_id, node_id = task.run_id, task.approval_id, task.node_id + await record(run_id, EventType.TASK_EXPIRED, node_id=node_id, task_id=task_id, action="reject") + from app.engine.runner import run_manager + + try: + await run_manager.resume(run_id, {"approved": False, "note": EXPIRED_NOTE}, approval_id=approval_id) + except (ValueError, KeyError): + logger.warning("待办 %s 超时驳回时运行已无法恢复", task_id) + async with SessionLocal() as session: + await session.execute(update(Task).where(Task.id == task_id).values(status="cancelled", + resolved_at=utcnow())) + await session.commit() + return 1 + await _settle(task_id) + await record(run_id, EventType.TASK_RESOLVED, node_id=node_id, task_id=task_id, decision="expired_reject", + actor=None) + return 1 + + +def _aware(moment: datetime) -> datetime: + from datetime import timezone + + return moment if moment.tzinfo else moment.replace(tzinfo=timezone.utc) + + +# -------------------------------------------------------------------------- +# 启动时整理、运行结束时收尾 +# -------------------------------------------------------------------------- + + +async def startup() -> int: + """启动时:存量的待审批补建 Task(派给任何人,幂等);上次停在 resolving 的(判定之后、恢复之前进程没了)按 Approval + 的状态收拾——还在等的退回 pending、撤回那一票,已经交给引擎的记成已处理。返回补建的条数。""" + async with SessionLocal() as session: + orphans = list((await session.execute( + select(Approval).outerjoin(Task, Task.approval_id == Approval.id) + .where(Approval.status == "pending", Task.id.is_(None)))).scalars()) + for approval in orphans: + await open_task(session, approval) + stuck = list((await session.execute(select(Task).where(Task.status == "resolving"))).scalars()) + for task in stuck: + waiting = task.approval_id and await session.scalar( + select(Approval.status).where(Approval.id == task.approval_id)) == "pending" + if waiting: + assignment = (task.outcome or {}).get("by_assignment") + task.status, task.outcome, task.resolved_by = "pending", None, None + if assignment: + await session.execute(update(TaskAssignment).where(TaskAssignment.id == assignment).values( + status="pending", response=None, responded_by=None, responded_at=None, verified=False)) + else: + task.status, task.resolved_at = "resolved", task.resolved_at or utcnow() + await session.commit() + if orphans: + logger.info("为 %d 条存量的待审批补建了待办", len(orphans)) + return len(orphans) + + +async def close_for_run(session: AsyncSession, run_id: str, now: datetime) -> int: + """运行结束(失败、取消)时,它还没处理的待办一并关闭。和关审批同一个事务(runner._close_approvals 调)。""" + ids = list((await session.execute(select(Task.id).where(Task.run_id == run_id, Task.status == "pending"))) + .scalars()) + if not ids: + return 0 + await session.execute(update(Task).where(Task.id.in_(ids)).values(status="cancelled", resolved_at=now)) + await session.execute(update(TaskAssignment).where(TaskAssignment.task_id.in_(ids), + TaskAssignment.status == "pending") + .values(status="withdrawn")) + return len(ids) + + +async def task_for_approval(approval_id: str) -> str | None: + """老接口用:这条审批对应的待办 id。还没有(开待办时出过错、老数据没补建)就现在补一条。""" + async with SessionLocal() as session: + task_id = await session.scalar(select(Task.id).where(Task.approval_id == approval_id)) + if task_id: + return task_id + approval = await session.get(Approval, approval_id) + if approval is None: + return None + task = await open_task(session, approval) + await session.commit() + return task.id + + +# -------------------------------------------------------------------------- +# 读 +# -------------------------------------------------------------------------- + + +def _who(a: TaskAssignment) -> dict[str, str]: + return {"kind": a.principal_kind, "name": a.principal} + + +def _iso(moment: datetime | None) -> str | None: + return _aware(moment).isoformat() if moment is not None else None + + +async def _assignments(session: AsyncSession, task_ids: list[str], *, fresh: bool = False + ) -> dict[str, list[TaskAssignment]]: + if not task_ids: + return {} + stmt = select(TaskAssignment).where(TaskAssignment.task_id.in_(task_ids)).order_by(TaskAssignment.created_at) + if fresh: + stmt = stmt.execution_options(populate_existing=True) + out: dict[str, list[TaskAssignment]] = {} + for row in (await session.execute(stmt)).scalars(): + out.setdefault(row.task_id, []).append(row) + return out + + +def _mine(principal: Any) -> Any: + """「派给我的」:有一份没撤回的派给我(off 模式下全部都算)。""" + if getattr(principal, "mode", "off") == "off": + return None + conds = [] + if principal.username: + conds.append(and_(TaskAssignment.principal_kind == "user", TaskAssignment.principal == principal.username)) + if principal.groups: + conds.append(and_(TaskAssignment.principal_kind == "group", + TaskAssignment.principal.in_(list(principal.groups)))) + if principal.at_least("builder"): + conds.append(TaskAssignment.principal_kind == "anyone") + if principal.at_least("admin"): + conds.append(TaskAssignment.principal_kind == "hook") + if not conds: + return Task.id.is_(None) + return exists().where(TaskAssignment.task_id == Task.id, TaskAssignment.status != "withdrawn", or_(*conds)) + + +async def list_tasks(principal: Any, *, assigned: str = "me", statuses: list[str] | None = None, + app_id: str | None = None, run_id: str | None = None, limit: int = 100) -> list[dict[str, Any]]: + async with SessionLocal() as session: + stmt = select(Task).order_by(Task.created_at.desc()).limit(limit) + if statuses: + stmt = stmt.where(Task.status.in_(statuses)) + if app_id: + stmt = stmt.where(Task.app_id == app_id) + if run_id: + stmt = stmt.where(Task.run_id == run_id) + if assigned == "me" and (clause := _mine(principal)) is not None: + stmt = stmt.where(clause) + tasks = list((await session.execute(stmt)).scalars()) + return await _describe(session, tasks, principal) + + +async def count(principal: Any) -> dict[str, int]: + """导航徽标:派给我的、待处理的待办数。apps 是应用里的,other 是不属于应用的(画布、定时任务的审批)。""" + async with SessionLocal() as session: + stmt = select(Task.app_id.is_not(None), func.count()).where(Task.status == "pending") + if (clause := _mine(principal)) is not None: + stmt = stmt.where(clause) + rows = dict((await session.execute(stmt.group_by(Task.app_id.is_not(None)))).all()) + apps, other = int(rows.get(True, 0)), int(rows.get(False, 0)) + return {"mine": apps + other, "apps": apps, "other": other} + + +async def detail(task_id: str, principal: Any) -> dict[str, Any] | None: + async with SessionLocal() as session: + task = await session.get(Task, task_id) + if task is None: + return None + return (await _describe(session, [task], principal))[0] + + +async def _describe(session: AsyncSession, tasks: list[Task], principal: Any) -> list[dict[str, Any]]: + """Task 的接口形状(frontend/src/typesApp.ts 的 Task)。payload 原样是中断内容,核实待办换成那个动作的预览。""" + ids = [t.id for t in tasks] + assignments = await _assignments(session, ids) + approvals = {a.id: a for a in (await session.execute( + select(Approval).where(Approval.id.in_([t.approval_id for t in tasks if t.approval_id])))).scalars()} + runs = {r.id: r for r in (await session.execute( + select(Run.id, Run.workflow_name, Run.graph).where(Run.id.in_({t.run_id for t in tasks})))).all()} + apps = dict((await session.execute(select(App.id, App.name).where( + App.id.in_({t.app_id for t in tasks if t.app_id})))).all()) + out = [] + for task in tasks: + rows = assignments.get(task.id, []) + payload = dict(approvals[task.approval_id].payload or {}) if task.approval_id in approvals else {} + if task.kind == "reconcile": + payload = await _reconcile_payload(session, task) + run = runs.get(task.run_id) + out.append({ + "id": task.id, "approval_id": task.approval_id, "run_id": task.run_id, "node_id": task.node_id, + "node_label": _node_label(run.graph if run else None, task.node_id), + "app_id": task.app_id, "app_name": apps.get(task.app_id), "app_version": task.app_version, + "workflow_name": run.workflow_name if run else None, "kind": task.kind, "title": task.title, + "form": task.form, "policy": task.policy or {"kind": "any"}, "status": task.status, + "due_at": _iso(task.due_at), "escalation_level": task.escalation_level, "payload": payload, + "assignments": [{ + "id": a.id, "principal_kind": a.principal_kind, "principal": a.principal, "source": a.source, + "status": a.status, "responded_by": a.responded_by, "responded_at": _iso(a.responded_at), + "decision": (a.response or {}).get("decision"), "verified": a.verified} for a in rows], + "can_respond": task.status == "pending" and any(_matches(a, principal) for a in rows) + and not _voted(task, rows, principal), + "outcome": task.outcome, "resolved_by": task.resolved_by, "resolved_at": _iso(task.resolved_at), + "created_at": _iso(task.created_at), + }) + return out + + +def visible(item: dict[str, Any], principal: Any) -> bool: + """这条待办(_describe 的形状)这个人看不看得到:关登录、builder 以上都看得到;user 角色只看派给自己(或自己的组)的, + 撤回了的不算。待办里带着中断内容(报告概况、动作预览),不能谁都能按 id 读。""" + if getattr(principal, "mode", "off") == "off" or principal.at_least("builder"): + return True + return any(a["status"] != "withdrawn" and ( + (a["principal_kind"] == "user" and a["principal"] == principal.username) + or (a["principal_kind"] == "group" and a["principal"] in (principal.groups or ()))) + for a in item["assignments"]) + + +def _voted(task: Task, rows: list[TaskAssignment], principal: Any) -> bool: + name = getattr(principal, "username", None) + return bool(name) and task.kind in VOTING and any(a.status == "responded" and a.responded_by == name + for a in rows) + + +def _node_label(graph: dict[str, Any] | None, node_id: str | None) -> str | None: + for n in (graph or {}).get("nodes") or []: + if isinstance(n, dict) and n.get("id") == node_id: + return ((n.get("data") or {}).get("label")) or node_id + return None + + +async def _reconcile_payload(session: AsyncSession, task: Task) -> dict[str, Any]: + from app.db.models import ActionRecord + + action = await session.get(ActionRecord, (task.correlation_key or "").removeprefix("action:")) + if action is None: + return {"kind": "reconcile"} + return {"kind": "reconcile", "action_id": action.id, "capability": action.capability, + "environment": action.environment, "payload_hash": action.payload_hash, "preview": action.preview, + "attempt": action.attempt, "idem_key": action.idem_key, "state": action.state, "error": action.error, + "request_at": _iso(action.request_at), "options": list(RECONCILE_OUTCOMES)} + + +def principal_with_actor(principal: Any, actor: str | None) -> Any: + """关登录时请求的身份没有名字:用自报的署名(X-Actor)补上,回复人一栏才写得出人。""" + if getattr(principal, "mode", "off") == "off" and not principal.username and actor: + return replace(principal, username=actor) + return principal diff --git a/backend/app/scheduler/loop.py b/backend/app/scheduler/loop.py index 7a6ed3ee..4c850231 100644 --- a/backend/app/scheduler/loop.py +++ b/backend/app/scheduler/loop.py @@ -112,13 +112,26 @@ def _span(first: datetime, last: datetime, tz: str) -> str: async def tick(now: datetime | None = None) -> None: - """完整的一拍:发起到点的,再推送该推送的通知。测试和手动调用用;循环里两件事分开跑(见 _Runner._loop)。""" + """完整的一拍:发起到点的,处理流程的时限和核实,再推送该推送的通知。测试和手动调用用;循环里分开跑(见 _Runner._loop)。""" from app.scheduler import webhook await fire_due(now) + await process_due(now) await webhook.notify_due(now) +async def process_due(now: datetime | None = None) -> None: + """流程的一拍(app/process):待办的提醒、升级、超时驳回(永不自动批准);结果未知的写入按退避去查。 + 各自出错只记日志:这一拍出错不该让调度循环停下,下一拍照常再查。""" + from app.process import actions, tasks + + for name, step in (("待办的时限处理", tasks.tick), ("写入结果的查询", actions.tick)): + try: + await step(now) + except Exception: # noqa: BLE001 + logger.exception("%s这一拍出错,下一拍照常进行", name) + + async def _notify(webhook) -> None: # noqa: ANN001 - 传进来的是 app.scheduler.webhook 模块 """后台跑一遍通知扫描。出错只记日志:这个任务没人 await,异常留在里面只会变成一句「从未取回的异常」。""" try: @@ -152,6 +165,7 @@ class _Runner: while not stopping.is_set(): try: await fire_due() + await process_due() # stop 撞在这一拍的发起当中时,收尾不再新起通知扫描:stop 只等它动手时手上的那一份 if not stopping.is_set() and (self.notify is None or self.notify.done()): self.notify = asyncio.create_task(_notify(webhook), name="agentlab-scheduler-notify") diff --git a/backend/app/scheduler/service.py b/backend/app/scheduler/service.py index 8eab64e3..022d5ba5 100644 --- a/backend/app/scheduler/service.py +++ b/backend/app/scheduler/service.py @@ -281,7 +281,9 @@ async def fire_once(schedule_id: str, fire_at: datetime, kind: str, *, actor: st async with SessionLocal() as session: run = await launch_run(session, workflow_id=workflow_id, run_class=run_class, input_payload=inputs, actor=actor or actor_label(name), - schedule_id=schedule_id) + schedule_id=schedule_id, origin_kind="schedule", + origin={"schedule_id": schedule_id, "schedule": name, + "fire_id": fire.id, "fire_kind": kind}) except LaunchRefused as e: return await _finish(fire.id, status="failed_to_start", reason=e.message) except Exception: # noqa: BLE001 - 发起失败不能让调度循环停下;详细信息进日志,记录上写通用说明 @@ -297,10 +299,11 @@ async def record_skip(schedule_id: str, fire_at: datetime, reason: str) -> Sched return await _record(schedule_id, fire_at, "misfire", "skipped_misfire", reason, {}) -async def _active_run(schedule_id: str) -> tuple[str, str, bool] | None: - """这个任务发起的、还没结束的运行(最近的那一个):(运行 id, 状态, 是否停在审批),没有返回 None。 +async def _active_run(schedule_id: str) -> tuple[str, str, bool, bool] | None: + """这个任务发起的、还没结束的运行(最近的那一个):(运行 id, 状态, 是否停在审批, 是否等写入核实),没有返回 None。 - 停在审批:有 pending 的审批,或者刚批完(answered)、还没接着执行。interrupted 而两样都没有的是挂起的。""" + 停在审批:有 pending 的审批,或者刚批完(answered)、还没接着执行。interrupted 而两样都没有的是挂起的;挂起的里面 + 有写外部系统结果未知、等核实的(app/process/actions.py),处理办法不一样,单独说。""" async with SessionLocal() as session: row = (await session.execute( select(Run.id, Run.status).where(Run.schedule_id == schedule_id, Run.status.in_(ACTIVE_RUN)) @@ -311,16 +314,22 @@ async def _active_run(schedule_id: str) -> tuple[str, str, bool] | None: waiting = status == "interrupted" and bool(await session.scalar( select(func.count()).select_from(Approval).where( Approval.run_id == run_id, Approval.status.in_(("pending", "answered"))))) - return run_id, status, waiting + from app.process.actions import unresolved_action + + held = status == "interrupted" and not waiting and await unresolved_action(session, run_id) + return run_id, status, waiting, held -def overlap_reason(run_id: str, status: str, waiting: bool) -> str: +def overlap_reason(run_id: str, status: str, waiting: bool, action_pending: bool = False) -> str: """跳过的原因按挡路的那次运行说清楚该等还是该处理。运行编号和界面上的短编号一样写(#前 6 位)。""" tag = f"#{run_id[:6]}" if status != "interrupted": return f"上一次运行 {tag} 仍在进行中,本次触发已跳过" if waiting: return f"上一次运行 {tag} 正在等待审批,处理审批、运行结束后才会按时发起新的运行;本次触发已跳过" + if action_pending: + return (f"上一次运行 {tag} 有一次写入外部系统的结果未知,正在等待核实,核实后会自动继续;它结束后才会按时发起" + "新的运行,本次触发已跳过") return (f"上一次运行 {tag} 已挂起(通常是服务重启所致),请在「记录」中继续运行或放弃这次运行," "它结束后才会按时发起新的运行;本次触发已跳过") diff --git a/backend/app/scheduler/webhook.py b/backend/app/scheduler/webhook.py index 8d8927d4..4a19e1c6 100644 --- a/backend/app/scheduler/webhook.py +++ b/backend/app/scheduler/webhook.py @@ -1,36 +1,21 @@ -"""定时任务的通知:运行结束、失败、停在审批(或因服务重启挂起)时按任务的设置推送 Webhook。 +"""定时任务的通知:运行结束、失败、停在审批(或因服务重启挂起、写入外部系统的结果未知)时按任务的设置推送 Webhook。 -四种格式:通用 JSON、飞书(文本消息)、企业微信(markdown)、钉钉(markdown)。配了签名密钥时:通用格式在请求头 -里带 HMAC-SHA256 签名,飞书、钉钉按各自平台的算法签(飞书放在请求体,钉钉放在地址上);企业微信的 key 本来就在 -地址里,没有另外的签名。 - -Webhook 地址本身就是凭证(企业微信的 key、钉钉的 access_token 都在地址里,飞书的在路径里),和签名密钥一样 -加密落库,日志、推送记录、接口返回里只出现主机名。httpx 每发一个请求都会在 INFO 级别记一行完整地址,这里装了 -一个过滤器把推送期间的那几行换成主机名。 +格式、签名、发送、脱敏抽到了 app/notify/core.py,待办、实例、动作的通知(app/notify/service.py)共用同一份。这里留 +定时任务自己的部分:消息内容、每一拍的通知扫描、推送记录(webhook_deliveries,不迁移,F14)。原来在这里的名字照原名 +再导出,调用方和测试不用改;_transport、RETRY_DELAYS 仍是这个模块自己的(测试照旧替换它们),send 每次调用时读。 通知里只有任务名、状态、出具档位、缺口条数、运行链接、封存哈希(界面上叫清单哈希)前 12 位和起止时间,失败时 另写失败节点的名字和下一步(不许诺能继续运行);运行成功结束后和同一任务上一次成功的运行的对比(auto_compare)只写 各类指标的个数和对比页链接。不放报告里的具体数字,也不放失败时的原始报错——报错里可能带着查询到的数据,Webhook 推去的群不一定有权看。 - -三个平台出错时都回 HTTP 200、错误码放在响应体里,所以送没送达要看响应体:按地址的主机认平台,不看选的格式—— -格式选错了(飞书的地址选了通用格式)正是最需要认出来的情形。群机器人的地址只认自己平台的格式,保存时就拦下 -对不上的(见 bot_format)。 """ from __future__ import annotations import asyncio -import base64 -import contextvars -import hashlib -import hmac -import json import logging -import unicodedata from dataclasses import dataclass from datetime import datetime, timedelta, timezone from typing import Any -from urllib.parse import parse_qsl, quote_plus, urlsplit import httpx from sqlalchemy import and_, func, or_, select, update @@ -38,21 +23,25 @@ from sqlalchemy import and_, func, or_, select, update from app.core.crypto import decrypt from app.core.events import EventType from app.db.base import SessionLocal, utcnow -from app.db.models import Approval, Run, RunEvent, Schedule, ScheduleFire, WebhookDelivery +from app.db.models import ActionRecord, Approval, Run, RunEvent, Schedule, ScheduleFire, WebhookDelivery from app.engine.labels import ISSUANCE_LABEL +from app.notify import core +# 抽到 app/notify/core.py 的名字照原名再导出 +from app.notify.core import ( # noqa: F401 + _BOTS, _PLATFORM_HOSTS, _SENDING, FORMAT_LABEL, FORMATS, TIMEOUT_S, Outcome, _platform_error, + _platform_message, _RedactWebhookUrls, _reply_json, _sensitive, bot_format, check_url, format_problem, host_of, + url_hint, +) +from app.notify.core import scrub as _scrub # noqa: F401 from app.scheduler import auto_compare, service logger = logging.getLogger(__name__) -FORMATS = ("generic", "feishu", "wecom", "dingtalk") EVENTS = ("finished", "failed", "awaiting_approval") -#: 格式、事件在报错里的叫法,和界面上的选项一致 -FORMAT_LABEL = {"generic": "通用", "feishu": "飞书", "wecom": "企业微信", "dingtalk": "钉钉"} +#: 事件在报错里的叫法,和界面上的选项一致 EVENT_LABEL = {"finished": "运行结束", "failed": "运行失败", "awaiting_approval": "等待审批"} -#: 单次请求的超时(秒) -TIMEOUT_S = 10.0 -#: 失败后再试 3 次,每次之前等这么久(秒)。4xx(429 除外)不重试:地址或参数错了,重试也一样 -RETRY_DELAYS: tuple[float, ...] = (1.0, 2.0, 4.0) +#: 失败后再试 3 次,每次之前等这么久(秒)。测试里换成 (0, 0, 0) +RETRY_DELAYS: tuple[float, ...] = core.RETRY_DELAYS #: 只看这么多天内的触发:更早结束的运行状态不会再变,不用每一拍都扫。例外见 notify_due:停在审批上、有待审批的不受 #: 这个限制,运行记录这些天内有过变化的也照扫——审批可以等上一个多月,批完接着执行、停到下一个审批点或跑完都要推 SCAN_DAYS = 30 @@ -67,6 +56,10 @@ _STATUS_LABEL = {"succeeded": "已完成", "failed": "失败", "cancelled": "已 #: 挂起的运行要人去处理:不处理,这个任务之后的触发都按「上一次还没结束」跳过 SUSPENDED_REASON = "运行停在断点上、没有待审批,通常是服务重启所致" SUSPENDED_ACTION = "请在 AgentLab 的「记录」中继续运行或放弃这次运行;它结束之前,这个定时任务之后的触发都会跳过" +#: 写外部系统的结果未知、挂起等核实的运行(app/process/actions.py):核实之后系统自动接着跑,不用人去点继续运行 +ACTION_PENDING_REASON = "运行中有一次写入外部系统的结果未知,已挂起等待核实" +ACTION_PENDING_ACTION = ("请在 AgentLab 的「应用」中处理核实待办(登记了查询能力的由系统自动核实);核实之后运行会自动继续," + "它结束之前,这个定时任务之后的触发都会跳过") #: 失败的运行怎么处理:不许诺「修正后可继续运行」。开始执行过的也可能没有断点(执行第一个节点前就失败)或已不兼容 #: (系统升级),能不能继续以记录页的提示为准。「记录」是导航里那一页的名字,和挂起那句一致 FAILED_ACTION = "请在 AgentLab 的「记录」中查看失败原因" @@ -80,145 +73,6 @@ NEXT_AS_USUAL = "下一次触发会照常发起" RERUN_ACTION = "如需重新发起,请在 AgentLab 的「定时任务」中点击「立即运行」" -# -------------------------------------------------------------------------- -# 地址:校验、提示、脱敏 -# -------------------------------------------------------------------------- - - -_URL_FORMAT = ("Webhook 地址需要是 http:// 或 https:// 开头的完整地址,例如飞书机器人的 " - "https://open.feishu.cn/open-apis/bot/v2/hook/…") - - -def check_url(url: Any) -> str: - """校验 Webhook 地址,返回去掉首尾空白的地址。不对抛 ValueError(中文说明,不回显地址)。 - - 除了 http(s)、有主机名,还要 httpx 真发得出去:端口写错、夹着不可见的字符(从聊天软件复制时常带上零宽空格)、 - 主机名不合 IDNA 规则的,保存时就拦下。以前这些照样存进去,到「测试 Webhook」才在 httpx 里抛出 500 或英文报错。 - """ - text = url.strip() if isinstance(url, str) else "" - if not text: - raise ValueError(_URL_FORMAT) - if any(c.isspace() or unicodedata.category(c) in ("Cc", "Cf") for c in text): - raise ValueError("Webhook 地址中有空格或不可见的字符(从聊天软件复制时常会带上),请删除后重新填写") - if len(text) > 2000: - raise ValueError("Webhook 地址过长(超过 2000 个字符),请检查是否粘贴了多余的内容") - try: - parts = urlsplit(text) - except ValueError: - raise ValueError(_URL_FORMAT) from None - if parts.scheme not in ("http", "https") or not parts.hostname: - raise ValueError(_URL_FORMAT) - try: - port = parts.port - except ValueError: # 不是数字,或者超出 65535 - port = -1 - if port is not None and not 1 <= port <= 65535: - raise ValueError("Webhook 地址中的端口需要是 1 到 65535 之间的数字") - try: - # 照发送时的样子建一个请求(不发出去):httpx 到这一步才按 IDNA 解码主机名 - httpx.Request("POST", text) - except (httpx.InvalidURL, ValueError): # IDNA 的错误是 UnicodeError,也是 ValueError - raise ValueError("Webhook 地址的主机名不符合规范,无法发送,请检查后重新填写") from None - return text - - -#: 群机器人的地址(主机,路径前缀)→ 格式。这些地址只认自己平台的消息格式:格式选错了,平台回错误码,群里收不到 -_BOTS = ( - ("open.feishu.cn", "/open-apis/bot/", "feishu"), - ("open.larksuite.com", "/open-apis/bot/", "feishu"), - ("qyapi.weixin.qq.com", "/cgi-bin/webhook/", "wecom"), - ("oapi.dingtalk.com", "/robot/", "dingtalk"), -) -#: 平台的主机 → 按哪个平台读 HTTP 200 里的错误码。同一主机上不是群机器人的接口(飞书集成平台的 Webhook 触发器 -#: 收任意 JSON)也是同一套错误码约定 -_PLATFORM_HOSTS = {host: fmt for host, _, fmt in _BOTS} - - -def bot_format(url: str) -> str | None: - """这条地址是哪个平台的群机器人(feishu / wecom / dingtalk),不是群机器人的返回 None。""" - try: - parts = urlsplit(url) - except ValueError: - return None - host = (parts.hostname or "").lower() - return next((fmt for bot_host, prefix, fmt in _BOTS if host == bot_host and parts.path.startswith(prefix)), None) - - -def format_problem(fmt: str, url: str) -> str | None: - """格式和地址对不上时的说明(群机器人的地址选了别的格式),对得上返回 None。""" - expected = bot_format(url) - if expected is None or expected == fmt: - return None - label = FORMAT_LABEL[expected] - return f"这是{label}群机器人的地址,只接收{label}的消息格式,Webhook 格式请选择「{label}」" - - -def host_of(url: str) -> str: - """主机名(带非默认端口)。日志、报错里只写它。端口写错的地址也照样给出主机名。""" - try: - parts = urlsplit(url) - host = parts.hostname or "?" - except ValueError: - return "?" - try: - port = parts.port - except ValueError: - return host - return f"{host}:{port}" if port else host - - -def url_hint(url: str) -> str: - """给界面看的地址提示:只露主机和末 4 位(open.feishu.cn/…ab12)。""" - return f"{host_of(url)}/…{url[-4:]}" - - -def _sensitive(url: str, secret: str | None) -> list[str]: - """一条地址里不能出现在日志、报错里的片段:整条地址、查询参数的值(key、access_token)、路径里的长片段(飞书 - 的令牌在路径里)、签名密钥。长的在前,免得先换掉短的、长的就认不出来了。""" - out = {url, secret or ""} - try: - parts = urlsplit(url) - out |= {value for _, value in parse_qsl(parts.query)} - out |= {seg for seg in parts.path.split("/") if len(seg) >= 12} - out.add(f"{parts.netloc}{parts.path}") - except ValueError: - pass - return sorted((s for s in out if len(s) >= 4), key=len, reverse=True) - - -def _scrub(text: str, url: str, secret: str | None) -> str: - for piece in _sensitive(url, secret): - text = text.replace(piece, "…") - return text - - -#: 正在推送哪条地址、用的哪个密钥。httpx 记请求日志时还在同一个任务里,过滤器据此只改推送的那几行 -_SENDING: contextvars.ContextVar[tuple[str, str | None] | None] = contextvars.ContextVar("webhook_sending", default=None) - - -class _RedactWebhookUrls(logging.Filter): - """把 httpx 记下的推送请求的地址换成主机名。别的请求(模型接入、数据源工具)原样放过。""" - - def filter(self, record: logging.LogRecord) -> bool: - sending = _SENDING.get() - if sending and isinstance(record.args, tuple): - url, secret = sending - record.args = tuple( - url_hint(str(arg)) if isinstance(arg, httpx.URL) - else _scrub(arg, url, secret) if isinstance(arg, str) else arg - for arg in record.args) - return True - - -def _install_filter() -> None: - target = logging.getLogger("httpx") - if not any(isinstance(f, _RedactWebhookUrls) for f in target.filters): - target.addFilter(_RedactWebhookUrls()) - - -_install_filter() - - # -------------------------------------------------------------------------- # 消息内容 # -------------------------------------------------------------------------- @@ -253,6 +107,8 @@ class Notice: reason: str | None = None pending_approvals: int | None = None suspended: bool = False + #: 挂起的原因是写外部系统的结果未知(等核实,核实后自动接着跑),不是服务重启。只在 suspended 时有意义 + action_pending: bool = False error_node: str | None = None next_fire: bool = False compare: dict[str, Any] | None = None @@ -303,6 +159,8 @@ def message(notice: Notice) -> tuple[str, list[tuple[str, str]], str | None]: "这个定时任务之后的触发都会跳过")) if notice.status == "failed_to_start" and notice.reason: lines.append(("原因", notice.reason)) + elif notice.suspended and notice.action_pending: + lines += [("原因", ACTION_PENDING_REASON), ("处理", ACTION_PENDING_ACTION)] elif notice.suspended: lines += [("原因", SUSPENDED_REASON), ("处理", SUSPENDED_ACTION)] elif notice.status == "failed": @@ -358,7 +216,9 @@ def _date_of(text: Any, tz: str) -> str | None: def _status_label(notice: Notice) -> str: if notice.status == "interrupted": - return "等待审批" if notice.pending_approvals else "已挂起,可继续运行" + if notice.pending_approvals: + return "等待审批" + return "已挂起,等待核实" if notice.action_pending else "已挂起,可继续运行" return _STATUS_LABEL.get(notice.status or "", notice.status or "") @@ -377,30 +237,21 @@ def _links(notice: Notice) -> list[tuple[str, str]]: return [(label, url) for label, url in (("查看运行", notice.run_url), ("查看对比", notice.compare_url)) if url] -def _text(notice: Notice) -> str: +def _message(notice: Notice) -> core.Message: title, lines, _ = message(notice) - out = [f"【AgentLab】{title}", *(f"{k}:{v}" for k, v in lines)] - out += [f"{label}:{url}" for label, url in _links(notice)] - return "\n".join(out) + return core.Message(title=title, lines=lines, links=_links(notice), ok=notice.event in ("finished", "test")) + + +def _text(notice: Notice) -> str: + return core.text_of(_message(notice)) def _wecom_markdown(notice: Notice) -> str: - title, lines, _ = message(notice) - color = "info" if notice.event in ("finished", "test") else "warning" - out = [f"**【AgentLab】{title}**"] - for key, value in lines: - shown = f'{value}' if key == "状态" else value - out.append(f"> {key}:{shown}") - out += [f"[{label}]({url})" for label, url in _links(notice)] - return "\n".join(out) + return core.wecom_markdown(_message(notice)) def _dingtalk_markdown(notice: Notice) -> tuple[str, str]: - title, lines, _ = message(notice) - out = [f"#### 【AgentLab】{title}", "", *(f"- {k}:{v}" for k, v in lines)] - for label, url in _links(notice): - out += ["", f"[{label}]({url})"] - return title, "\n".join(out) + return core.dingtalk_markdown(_message(notice)) def _iso(moment: datetime | None) -> str | None: @@ -430,6 +281,7 @@ def _generic(notice: Notice, now: datetime) -> dict[str, Any]: "started_at": _iso(notice.started_at), "finished_at": _iso(notice.finished_at), "reason": (notice.reason if notice.status == "failed_to_start" + else f"{ACTION_PENDING_REASON}。{ACTION_PENDING_ACTION}" if notice.suspended and notice.action_pending else f"{SUSPENDED_REASON}。{SUSPENDED_ACTION}" if notice.suspended else NOT_STARTED_REASON if notice.never_started else None), "error_node": notice.error_node, @@ -446,40 +298,7 @@ def build_request(fmt: str, url: str, secret: str | None, notice: Notice, *, now: datetime | None = None) -> tuple[str, dict[str, str], bytes]: """(请求地址, 请求头, 请求体)。签名里的时间戳按 now 取,每次重试重新算:飞书要求时间戳在一小时内。""" now = now or utcnow() - headers = {"Content-Type": "application/json"} - if fmt == "feishu": - body: dict[str, Any] = {"msg_type": "text", "content": {"text": _text(notice)}} - if secret: - ts = str(int(now.timestamp())) - # 飞书自定义机器人的签名:以「timestamp + 换行 + 密钥」为键,对空串做 HMAC-SHA256,再 base64 - key = f"{ts}\n{secret}".encode() - body = {"timestamp": ts, "sign": base64.b64encode(hmac.new(key, b"", hashlib.sha256).digest()).decode(), - **body} - return url, headers, _json(body) - if fmt == "wecom": - return url, headers, _json({"msgtype": "markdown", "markdown": {"content": _wecom_markdown(notice)}}) - if fmt == "dingtalk": - title, text = _dingtalk_markdown(notice) - target = url - if secret: - # 钉钉自定义机器人的签名:毫秒时间戳,以密钥为键对「timestamp + 换行 + 密钥」做 HMAC-SHA256,base64 后 - # 再做 URL 编码,和 timestamp 一起加在地址上 - ts = str(int(now.timestamp() * 1000)) - digest = hmac.new(secret.encode(), f"{ts}\n{secret}".encode(), hashlib.sha256).digest() - sign = quote_plus(base64.b64encode(digest).decode()) - target = f"{url}{'&' if '?' in url else '?'}timestamp={ts}&sign={sign}" - return target, headers, _json({"msgtype": "markdown", "markdown": {"title": title, "text": text}}) - payload = _json(_generic(notice, now)) - if secret: - # 通用格式:接收端按 HMAC-SHA256(密钥, 「时间戳.请求体」) 验签,时间戳防重放 - ts = str(int(now.timestamp())) - mac = hmac.new(secret.encode(), f"{ts}.".encode() + payload, hashlib.sha256).hexdigest() - headers |= {"X-AgentLab-Timestamp": ts, "X-AgentLab-Signature": f"sha256={mac}"} - return url, headers, payload - - -def _json(value: dict[str, Any]) -> bytes: - return json.dumps(value, ensure_ascii=False, separators=(",", ":")).encode() + return core.build_request(fmt, url, secret, _message(notice), generic=_generic(notice, now), now=now) # -------------------------------------------------------------------------- @@ -487,106 +306,13 @@ def _json(value: dict[str, Any]) -> bytes: # -------------------------------------------------------------------------- -@dataclass -class Outcome: - """一次推送(含重试)的结果。error 是中文说明,只有主机名,不含地址和密钥。""" - - ok: bool - http_status: int | None - attempts: int - error: str | None = None - #: 这一次失败值不值得再试(只在单次尝试的结果上有意义) - retryable: bool = False - - -def _client() -> httpx.AsyncClient: - return httpx.AsyncClient(timeout=TIMEOUT_S, transport=_transport, follow_redirects=False) - - async def send(fmt: str, url: str, secret: str | None, notice: Notice, *, retry: bool = True) -> Outcome: - """推送一条通知。retry 时失败再试 3 次(退避),4xx(429 除外)不重试。失败只记主机名。""" - host = host_of(url) - delays = RETRY_DELAYS if retry else () - last = Outcome(False, None, 0, "没有发出请求") - async with _client() as client: - for attempt in range(len(delays) + 1): - if attempt: - await asyncio.sleep(delays[attempt - 1]) - target, headers, body = build_request(fmt, url, secret, notice) - last = await _post_once(client, fmt, target, headers, body, url, secret) - last.attempts = attempt + 1 - if last.ok: - return last - logger.warning("定时任务「%s」的 Webhook(%s)第 %d 次推送失败:%s", - notice.schedule_name, host, attempt + 1, last.error) - if not last.retryable: - break - return last + """推送一条通知。retry 时失败再试 3 次(退避),4xx(429 除外)不重试。失败只记主机名。 - -async def _post_once(client: httpx.AsyncClient, fmt: str, target: str, headers: dict[str, str], body: bytes, - url: str, secret: str | None) -> Outcome: - host = host_of(url) - token = _SENDING.set((url, secret)) - try: - response = await client.post(target, content=body, headers=headers) - except httpx.TimeoutException: - return Outcome(False, None, 1, f"请求 {host} 超时({TIMEOUT_S:g} 秒)", retryable=True) - except httpx.ConnectError: - return Outcome(False, None, 1, f"无法连接 {host}", retryable=True) - except httpx.HTTPError as e: - return Outcome(False, None, 1, f"请求 {host} 失败({type(e).__name__})", retryable=True) - except httpx.InvalidURL: - # 保存时的地址校验加严之前存进库的地址:httpx 抛的不是 HTTPError。重试也一样,请人改地址 - return Outcome(False, None, 1, f"发往 {host} 的 Webhook 地址无效,请重新填写地址") - except Exception as e: # noqa: BLE001 - 端口越界之类在传输层才抛(不是 HTTPError),不能让测试接口回 500 - return Outcome(False, None, 1, f"无法向 {host} 发送请求({type(e).__name__}),请检查 Webhook 地址") - finally: - _SENDING.reset(token) - status = response.status_code - reply = _reply_json(response) - if not 200 <= status < 300: - detail = _platform_message(reply) - text = f"{host} 返回 HTTP {status}" + (f":{detail}" if detail else "") - return Outcome(False, status, 1, _scrub(text, url, secret)[:300], retryable=status == 429 or status >= 500) - if problem := _platform_error(fmt, url, reply): - return Outcome(False, status, 1, _scrub(f"{host} {problem}", url, secret)[:300], retryable=True) - return Outcome(True, status, 1) - - -def _reply_json(response: httpx.Response) -> dict[str, Any] | None: - try: - data = response.json() - except ValueError: - return None - return data if isinstance(data, dict) else None - - -def _platform_message(reply: dict[str, Any] | None) -> str: - if not reply: - return "" - text = reply.get("errmsg") or reply.get("msg") or reply.get("StatusMessage") or "" - return str(text)[:200] - - -def _platform_error(fmt: str, url: str, reply: dict[str, Any] | None) -> str | None: - """HTTP 200 里的平台错误码:飞书 code(老接口 StatusCode),企业微信、钉钉 errcode。0 是成功。 - - 按地址的主机认平台,不看选的格式:以前只看所选格式自己的字段(通用格式完全不看),飞书的地址选了通用格式、 - 平台回 code 19002,照样记「已送达」。主机不是这几个平台的(自建接收端、代理转发)才按所选格式读,通用格式 - 不解读——自建接收端的 code 字段是什么意思由它自己定。说明里写主机名(调用方拼在前面),不写平台名。""" - try: - host = (urlsplit(url).hostname or "").lower() - except ValueError: - host = "" - platform = _PLATFORM_HOSTS.get(host) or (fmt if fmt != "generic" else None) - if not reply or platform is None: - return None - code = reply.get("code", reply.get("StatusCode")) if platform == "feishu" else reply.get("errcode") - if code in (None, 0, "0"): - return None - detail = _platform_message(reply) - return f"返回错误码 {code}" + (f":{detail}" if detail else "") + _transport、RETRY_DELAYS 在调用时从这个模块读:测试替换的是它们。""" + return await core.post(fmt, url, secret, lambda: build_request(fmt, url, secret, notice), + label=f"定时任务「{notice.schedule_name}」的 Webhook", retry=retry, transport=_transport, + delays=RETRY_DELAYS) async def _record(schedule_id: str, fire_id: str | None, run_id: str | None, event: str, outcome: Outcome) -> None: @@ -715,6 +441,7 @@ class _Seen: event: str | None = None pending: int = 0 sealed: bool = False + action_pending: bool = False async def _state_of(session: Any, fire: ScheduleFire, run: Run | None, now: datetime) -> _Seen: @@ -725,6 +452,8 @@ async def _state_of(session: Any, fire: ScheduleFire, run: Run | None, now: date 第几段执行(每段执行从一条 run.started 开始),同一个审批点不重复推。 - 没有待审批:通常是服务重启挂起的。它不会自己接着执行,这个任务之后的触发都会按「上一次还没结束」跳过, 不说一声任务就此静默停摆——按 failed 推一次,写明要去继续运行或放弃。 + 其中写外部系统的结果未知、挂起等核实的(动作挂起,app/process/actions.py)另记一种状态:处理办法不一样(去核实, + 核实后自动接着跑,点继续运行会被拒),说明也不一样。 审批批完到运行改回 running 之间,审批是 answered、还没有 resolved:那不是挂起,等下一拍。 """ if fire.status == "failed_to_start": @@ -753,6 +482,9 @@ async def _state_of(session: Any, fire: ScheduleFire, run: Run | None, now: date return _Seen(f"interrupted:approval:{segment}", "awaiting_approval", counts["pending"]) if counts.get("answered"): return _Seen(None) + if await session.scalar(select(func.count()).select_from(ActionRecord).where( + ActionRecord.run_id == run.id, ActionRecord.state.in_(("sent", "unknown")))): + return _Seen(f"interrupted:action:{segment}", "failed", action_pending=True) return _Seen(f"interrupted:held:{segment}", "failed") @@ -783,6 +515,7 @@ async def _notice(session: Any, schedule: Schedule, fire: ScheduleFire, run: Run reason=fire.reason if fire.status == "failed_to_start" else None, pending_approvals=pending or None, suspended=run is not None and run.status == "interrupted" and not pending, + action_pending=seen.action_pending, error_node=_failed_node(run) if run is not None and run.status == "failed" else None, next_fire=bool(schedule.enabled and schedule.next_run_at is not None), ) diff --git a/backend/app/seed.py b/backend/app/seed.py index 6328a67d..cb56b4b8 100644 --- a/backend/app/seed.py +++ b/backend/app/seed.py @@ -312,6 +312,29 @@ TEMPLATES: list[dict[str, Any]] = [ _e("each", "done", "done"), ], }, + { + # 流程模板(应用平台 §3.7):图里只写岗位名,不写具体的人——做成应用时在应用里把「财务」「总监」绑到人或组。 + # 不依赖任何外部端点:发布、做成应用、会签、超时升级都能直接演示 + "name": "⑩ 报销审批(会签)", + "description": "员工发起报销,财务岗位全体同意才通过,1 小时内没处理完升级给总监;任何一人驳回即退回。", + "tags": ["流程", "人工审批", "会签"], + "nodes": [ + _n("start", "input", "发起报销", fields=[ + {"name": "applicant", "required": True, "description": "报销人"}, + {"name": "amount", "required": True, "description": "金额(元)"}, + {"name": "reason", "required": True, "description": "事由"}]), + _n("review", "human", "财务会签", mode="approve", + title="报销审批:{{ input.applicant }}", + message="报销人:{{ input.applicant }}\n金额(元):{{ input.amount }}\n事由:{{ input.reason }}", + assignee={"kind": "slot", "slot": "finance"}, policy="all", due="PT1H", + on_timeout={"action": "escalate", "to_slot": "director"}), + _n("approved", "output", "已通过", fields=[ + {"name": "结论", "value": "已通过"}, {"name": "会签备注", "value": "{{ nodes.review.note }}"}]), + _n("rejected", "output", "已退回", fields=[ + {"name": "结论", "value": "已退回"}, {"name": "退回原因", "value": "{{ nodes.review.note }}"}]), + ], + "edges": [_e("start", "review"), _e("review", "approved", "approved"), _e("review", "rejected", "rejected")], + }, ] diff --git a/backend/tests/test_challenge_node.py b/backend/tests/test_challenge_node.py index 22ded299..896b1dac 100644 --- a/backend/tests/test_challenge_node.py +++ b/backend/tests/test_challenge_node.py @@ -908,9 +908,11 @@ async def test_a_fail_that_would_be_swallowed_degrades_instead(monkeypatch, shop async def test_continuing_with_only_the_disposition_changed_keeps_the_review(monkeypatch, shop): - """判失败后只把「结论被推翻时」改成「只标注」再接着跑(报错里就是这么建议的):审稿沿用 checkpoint 里的,不重审。""" + """判失败后只把「结论被推翻时」改成「只标注」再接着跑(报错里就是这么建议的):审稿沿用 checkpoint 里的,不重审。 + + 探索运行:正式运行固定在发布的版本上,不能改了配置再接着跑(F2,runner.continue_failed)。""" models = Models(monkeypatch, shop) - run = await run_manager.start(graph=graph(shop, on_refuted="fail"), input_payload={}, run_class="formal") + run = await run_manager.start(graph=graph(shop, on_refuted="fail"), input_payload={}, run_class="exploratory") row = await wait(run.id) assert row.status == "failed", row.error calls = len(models.calls) @@ -924,9 +926,10 @@ async def test_continuing_with_only_the_disposition_changed_keeps_the_review(mon async def test_continuing_with_a_revised_review_reviews_again(monkeypatch, shop): - """接着跑时改了审稿本身(句数、反方模型):checkpoint 里那份是按旧配置审的,对不上就重审,不拿旧结果冒充。""" + """接着跑时改了审稿本身(句数、反方模型):checkpoint 里那份是按旧配置审的,对不上就重审,不拿旧结果冒充。 + 探索运行,理由同上。""" models = Models(monkeypatch, shop) - run = await run_manager.start(graph=graph(shop, on_refuted="fail"), input_payload={}, run_class="formal") + run = await run_manager.start(graph=graph(shop, on_refuted="fail"), input_payload={}, run_class="exploratory") row = await wait(run.id) assert row.status == "failed", row.error assert models.reviewed() == ["居各区域之首", "华南销售额超过华东"] diff --git a/backend/tests/test_lint_process.py b/backend/tests/test_lint_process.py new file mode 100644 index 00000000..0f47c0d0 --- /dev/null +++ b/backend/tests/test_lint_process.py @@ -0,0 +1,120 @@ +"""流程相关的检查(engine/lint_process.py,设计文档 §3.13)。 + +human.*:人工节点的流程配置写得不成立,运行时没法照着执行——validate_graph 报错误,各级发布、发起运行都挡。 +process.*:图能运行,按流程使用才出事——发布门禁里一律警告,应用把它作为流程钉住时由应用检查升成错误。 +""" +from __future__ import annotations + +from datetime import timedelta + +import pytest + +from app.db.base import SessionLocal +from app.db.models import Workflow, WorkflowVersion +from app.engine.governance import lint_for_publish, publish_issues +from app.engine.lint_process import load_subgraphs, parse_duration, with_subgraphs +from app.engine.schema import GraphSpec, validate_graph + + +def node(nid, ntype, **config): + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +def spec(*nodes) -> GraphSpec: + nodes = [node("start", "input", fields=[{"name": "q"}]), *nodes, node("out", "output", fields=[])] + return GraphSpec.model_validate({"nodes": nodes, "edges": [ + {"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]}) + + +def codes(result) -> dict[str, str]: + return {i.code: i.level for i in result.issues if i.code and i.code.startswith(("human.", "process."))} + + +def test_parse_duration(): + assert parse_duration("PT4H") == timedelta(hours=4) + assert parse_duration("P2D") == timedelta(days=2) + assert parse_duration("P1DT30M") == timedelta(days=1, minutes=30) + assert parse_duration("pt90s") == timedelta(seconds=90) + for bad in ("4h", "P", "PT", "P1M", "PT0H", "", None, 3): + assert parse_duration(bad) is None, bad + + +@pytest.mark.parametrize("config, code", [ + ({"due": "4 小时"}, "human.due_invalid"), + ({"on_timeout": {"action": "remind"}}, "human.due_invalid"), # 有超时处理却没写时限 + ({"mode": "input", "policy": "all"}, "human.policy_mode"), + ({"mode": "edit", "policy": {"quorum": 2}}, "human.policy_mode"), + ({"due": "PT1H", "on_timeout": {"action": "escalate"}}, "human.escalate_slot"), + ({"due": "PT1H", "on_timeout": {"action": "approve"}}, "human.timeout_invalid"), # 没有「自动批准」 + ({"policy": "most"}, "human.policy_invalid"), + ({"assignee": {"kind": "slot"}}, "human.assignee_invalid"), + ({"form": "不是对象"}, "human.form_invalid"), + ({"form_from": "{{ nodes.a.form }}"}, "human.form_from_invalid"), +]) +def test_bad_human_config_is_an_error(config, code): + result = validate_graph(spec(node("h", "human", **{"mode": "approve", **config}))) + assert codes(result).get(code) == "error", [i.model_dump() for i in result.issues] + assert not result.ok + gate = publish_issues(spec(node("h", "human", **{"mode": "approve", **config})), level="published") + assert any(i.code == code and i.level == "error" for i in gate) + + +def test_form_is_checked_against_the_catalog(monkeypatch): + from app.apps import forms + + monkeypatch.setattr(forms, "validate_form_schema", lambda form: ["不支持 pattern"]) + result = validate_graph(spec(node("h", "human", mode="input", form={"type": "object", "properties": {}}))) + assert codes(result) == {"human.form_invalid": "error"} + monkeypatch.setattr(forms, "validate_form_schema", lambda form: []) + assert validate_graph(spec(node("h", "human", mode="input", form={"type": "object", "properties": {}}))).ok + + +def test_good_human_config_passes(): + result = validate_graph(spec(node("h", "human", mode="approve", assignee={"kind": "slot", "slot": "finance"}, + policy={"quorum": 2}, due="PT1H", + on_timeout={"action": "escalate", "to_slot": "director"}))) + assert result.ok and codes(result) == {} + + +def test_agent_that_may_ask_mid_way_is_warned(): + agent = node("bot", "agent", prompt="查", tools=["mcp:erp/get_order", "current_time"]) + assert codes(lint_for_publish(spec(agent), level="governed")) == {"process.wait_mid_agent": "warning"} + quiet = node("bot", "agent", prompt="查", tools=["mcp:erp/get_order"], approval="never") + assert "process.wait_mid_agent" not in codes(lint_for_publish(spec(quiet), level="published")) + reads = node("bot", "agent", prompt="查", tools=["current_time", "db_query__shop"]) + assert "process.wait_mid_agent" not in codes(lint_for_publish(spec(reads), level="published")) + team = node("team", "supervisor", goal="x", agents=[{"name": "r", "tools": ["mcp:erp/pay"]}]) + assert codes(lint_for_publish(spec(team), level="published")) == {"process.wait_mid_agent": "warning"} + + +def test_code_with_network_is_warned(): + code = node("c", "code", code="print(1)", network=True) + assert codes(lint_for_publish(spec(code), level="governed")).get("process.code_network") == "warning" + + +async def test_human_inside_a_subgraph_is_warned_when_subgraphs_are_loaded(): + inner = {"nodes": [node("start", "input"), node("h", "human", mode="approve"), node("out", "output")], + "edges": [{"source": "start", "target": "h"}, {"source": "h", "target": "out"}]} + async with SessionLocal() as session: + wf = Workflow(name="带审批的子流程", graph=inner) + session.add(wf) + await session.flush() + session.add(WorkflowVersion(workflow_id=wf.id, version=1, graph=inner)) + await session.commit() + outer = spec(node("sub", "subgraph", workflow_id=wf.id, workflow_version=1)) + graphs = await load_subgraphs(session, outer) + assert "process.human_in_subgraph" not in codes(lint_for_publish(outer, level="published")) # 没读就不查 + with with_subgraphs(graphs): + assert codes(lint_for_publish(outer, level="published")) == {"process.human_in_subgraph": "warning"} + + +def test_seeded_expense_template_passes_validation_and_the_gate(): + from app.seed import TEMPLATES + + template = next(t for t in TEMPLATES if t["name"] == "⑩ 报销审批(会签)") + graph = GraphSpec.model_validate({"nodes": template["nodes"], "edges": template["edges"]}) + assert validate_graph(graph).ok + assert not [i for i in publish_issues(graph, level="published") if i.level == "error"] + review = next(n for n in template["nodes"] if n["type"] == "human")["data"]["config"] + assert review["assignee"] == {"kind": "slot", "slot": "finance"} and review["policy"] == "all" + assert review["due"] == "PT1H" and review["on_timeout"] == {"action": "escalate", "to_slot": "director"} diff --git a/backend/tests/test_notify_channels.py b/backend/tests/test_notify_channels.py new file mode 100644 index 00000000..b5ffd120 --- /dev/null +++ b/backend/tests/test_notify_channels.py @@ -0,0 +1,190 @@ +"""通知渠道与投递(app/notify/,设计文档 §2.4 第 7 条)。 + +发送核心从定时任务的 Webhook 抽出来共用(格式、签名、发送、脱敏),定时任务的老测试一个字不改照样通过; +渠道的地址、密钥加密落库,接口只回主机名;待办派出、应用实例结束、动作结果未知时按应用的订阅推送,带直达链接, +不放报告里的数字。 +""" +from __future__ import annotations + +import asyncio +import json +import uuid + +import httpx +import pytest +from httpx import ASGITransport, AsyncClient +from sqlalchemy import select + +from app.db.base import SessionLocal +from app.db.models import App, AppVersion, NotifyChannel, NotifyDelivery, Run, Setting +from app.engine.runner import run_manager +from app.notify import core, service + +URL = "https://hooks.example.com/agentlab/inbox-7f3a9c2e1b" + + +@pytest.fixture(autouse=True) +async def engine_up(): + await run_manager.setup() + yield + await service.drain() + await run_manager.shutdown() + + +@pytest.fixture +async def client(): + from app.main import app + + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: + yield c + + +@pytest.fixture +def received(monkeypatch): + got: list[httpx.Request] = [] + + def handler(request: httpx.Request) -> httpx.Response: + got.append(request) + return httpx.Response(200, json={"ok": True}) + + monkeypatch.setattr(core, "_transport", httpx.MockTransport(handler)) + monkeypatch.setattr(core, "RETRY_DELAYS", (0, 0, 0)) + return got + + +async def test_channel_crud_hides_the_address_and_secret(client): + resp = await client.post("/api/notify/channels", json={ + "name": "审批群", "format": "generic", "url": URL, "secret": "s3cret", "events": ["task.opened"]}) + assert resp.status_code == 201, resp.text + row = resp.json() + assert row["url_host"] == "hooks.example.com" and row["signed"] is True + assert URL not in json.dumps(row) and "s3cret" not in json.dumps(row) + async with SessionLocal() as session: + stored = await session.get(NotifyChannel, row["id"]) + assert stored.url != URL and stored.secret != "s3cret" # 密文 + + updated = await client.put(f"/api/notify/channels/{row['id']}", json={ + "name": "审批群", "format": "generic", "events": ["task.opened", "action.unknown"]}) + assert updated.status_code == 200 and updated.json()["events"] == ["task.opened", "action.unknown"] + assert updated.json()["url_host"] == "hooks.example.com" and updated.json()["signed"] is True # 不传就保持原值 + assert row["id"] in [c["id"] for c in (await client.get("/api/notify/channels")).json()["items"]] + toggled = await client.patch(f"/api/notify/channels/{row['id']}", json={"enabled": False}) + assert toggled.status_code == 200 and toggled.json()["enabled"] is False + assert toggled.json()["events"] == ["task.opened", "action.unknown"] and toggled.json()["signed"] is True + assert (await client.delete(f"/api/notify/channels/{row['id']}")).status_code == 204 + + +async def test_channel_validation(client): + bad_event = await client.post("/api/notify/channels", json={"name": "x", "url": URL, "events": ["nope"]}) + assert bad_event.status_code == 422 + wrong_format = await client.post("/api/notify/channels", json={ + "name": "x", "format": "generic", "url": "https://open.feishu.cn/open-apis/bot/v2/hook/abcdef123456"}) + assert wrong_format.status_code == 422 and "飞书" in wrong_format.json()["detail"] + personal = await client.post("/api/notify/channels", json={"name": "x", "scope": "user", "url": URL}) + assert personal.status_code == 422 # 关登录时没有用户,个人渠道无从认起 + + +async def test_personal_channels_cannot_point_at_the_intranet(received): + """个人渠道权限最低的使用者也能建:地址过 SSRF 检查,保存、发送时各一次;投递记录只看得到自己渠道的。""" + from tests.fakes import auth_env + + await auth_env.reset() + try: + await auth_env.turn_on_local() + uma_row = await auth_env.make_user("uma", "user") + uma = await auth_env.login("uma") + for url in ("http://127.0.0.1:8080/hook", "http://169.254.169.254/latest/meta-data", "http://10.0.0.8/x"): + r = await uma.post("/api/notify/channels", json={"name": "我的", "scope": "user", "url": url}) + assert r.status_code == 422 and "公网" in r.json()["detail"], (url, r.text) + assert "10.0.0.8" not in r.json()["detail"], "说法不带检查的细节" + async with SessionLocal() as session: + # 存进库之后域名才改指向内网的:发送前再查一次,不发 + from app.core.crypto import encrypt + + row = NotifyChannel(name="我的", scope="user", owner_id=uma_row.id, url=encrypt("http://127.0.0.1:9/x"), + events=[]) + other = NotifyChannel(name="全局", scope="global", url=encrypt(URL), events=[]) + session.add_all([row, other]) + await session.flush() + session.add(NotifyDelivery(channel_id=other.id, subject_kind="run", subject_id="r", event="run.failed", + status="ok")) + await session.commit() + tested = (await uma.post(f"/api/notify/channels/{row.id}/test")).json() + assert tested["ok"] is False and "公网" in tested["message"] and received == [] + assert [d["channel_id"] for d in (await uma.get("/api/notify/deliveries")).json()["items"]] == [row.id] + finally: + async with SessionLocal() as session: + for r in (await session.execute(select(NotifyChannel))).scalars(): + await session.delete(r) + await session.commit() + await auth_env.reset() + + +async def test_test_message_is_signed_and_recorded(client, received): + row = (await client.post("/api/notify/channels", json={ + "name": "审批群", "format": "generic", "url": URL, "secret": "s3cret"})).json() + resp = await client.post(f"/api/notify/channels/{row['id']}/test") + assert resp.json()["ok"] is True + [request] = received + from app.process.hooks import verify + + verify("s3cret", dict(request.headers), request.content) # 同一套签名 + deliveries = (await client.get(f"/api/notify/deliveries?channel_id={row['id']}")).json()["items"] + assert [(d["event"], d["status"]) for d in deliveries] == [("test", "ok")] + + +def node(nid, ntype, **config): + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +async def test_task_opened_is_pushed_to_the_app_subscription_without_numbers(received): + async with SessionLocal() as session: + channel = NotifyChannel(name="财务群", format="generic", url=core_encrypt(URL), events=[]) + session.add(channel) + app = App(name=f"报销-{uuid.uuid4().hex[:6]}", published_version=1) + session.add(app) + await session.flush() + session.add(AppVersion(app_id=app.id, version=1, spec_hash="h" * 64, bindings={}, + spec={"notify": [{"channel": channel.id, "events": ["task.opened"]}]})) + setting = await session.get(Setting, "notify") + if setting is None: + session.add(Setting(key="notify", value={"public_base_url": "https://agentlab.example"})) + await session.commit() + app_id, channel_id = app.id, channel.id + graph = {"nodes": [node("start", "input", fields=[{"name": "amount"}]), + node("review", "human", mode="approve", title="审批 {{ input.amount }} 元的报销"), + node("out", "output", fields=[])], + "edges": [{"source": "start", "target": "review"}, {"source": "review", "target": "out"}]} + run = await run_manager.start(graph=graph, input_payload={"amount": 45678}, app_id=app_id, app_version=1) + for _ in range(200): + async with SessionLocal() as session: + status = await session.scalar(select(Run.status).where(Run.id == run.id)) + if status == "interrupted": + break + await asyncio.sleep(0.05) + await service.drain() + assert len(received) == 1 + body = json.loads(received[0].content) + assert body["event"] == "task.opened" and "45678" not in received[0].content.decode() + assert body["title"] == "新的待办:review" + assert body["links"][0]["url"].startswith("https://agentlab.example/apps/inbox/") + async with SessionLocal() as session: + rows = (await session.execute(select(NotifyDelivery).where(NotifyDelivery.channel_id == channel_id)))\ + .scalars().all() + assert [(r.event, r.status, r.subject_kind) for r in rows] == [("task.opened", "ok", "task")] + + +def core_encrypt(value: str) -> str: + from app.core.crypto import encrypt + + return encrypt(value) + + +def test_webhook_module_still_exports_the_moved_names(): + """抽出去的名字照原名再导出:调用方(api/schedules.py 等)和定时任务的测试不用改。""" + from app.scheduler import webhook + + for name in ("FORMATS", "FORMAT_LABEL", "check_url", "bot_format", "format_problem", "host_of", "url_hint", + "Outcome", "send", "build_request", "send_test", "notify_due"): + assert hasattr(webhook, name), name + assert webhook.check_url is core.check_url diff --git a/backend/tests/test_process_actions.py b/backend/tests/test_process_actions.py new file mode 100644 index 00000000..105cd80a --- /dev/null +++ b/backend/tests/test_process_actions.py @@ -0,0 +1,317 @@ +"""动作:调用工具节点写外部系统(app/process/actions.py,设计文档 §3.8、§7.2)。 + +用假外部系统(tests/fakes/fake_erp.py)数「它收到了几次写请求」: +- 确认卡是遮罩后的预览 + 内容指纹;确认之后上游变了,指纹对不上,拒发; +- 写成功之后超时 → 结果未知,运行挂起(不封存);登记了查询能力的自动核实为已生效、接着跑,外部系统只收到 1 次; +- 没有查询能力的开核实待办,选「未生效,重新发送」,第 2 次的幂等键带 :2; +- 探索运行、端点没有测试环境 → 演练,外部系统收到 0 次; +- 发出之后进程没了 → 重启时收成结果未知; +- 4xx 确定失败,接着跑也不重发。 +能力登记和平台执行器是治理底座包的,这里用替身(get_capability / endpoint_request)。 +""" +from __future__ import annotations + +import asyncio +from datetime import timedelta +import pytest +from httpx import ASGITransport, AsyncClient +from sqlalchemy import select + +from app.api.deps import Principal +from app.db.base import SessionLocal, utcnow +from app.db.models import ActionRecord, Run, RunEvent, Task +from app.engine.runner import ContinueRefused, run_manager +from app.process import actions, tasks +from tests.fakes.fake_erp import PAY, FakeErp, endpoint_row, install + + +@pytest.fixture(autouse=True) +async def engine_up(): + await run_manager.setup() + yield + await run_manager.shutdown() + + +@pytest.fixture +def erp() -> FakeErp: + return FakeErp() + + +@pytest.fixture +def capabilities(monkeypatch, erp): + """登记付款(写入,遮罩卡号)和按幂等键查付款两个能力;参数是端点、要不要查询能力。""" + return lambda endpoint_id, *, lookup: install(monkeypatch, erp, endpoint_id, lookup=lookup) + + +async def _endpoint(*, test: bool) -> str: + return await endpoint_row(test=test) + + +def node(nid, ntype, **config): + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +def pay_graph(amount: str = "{{ input.amount }}") -> dict: + nodes = [node("start", "input", fields=[{"name": "amount"}]), + node("pay", "tool", tool=PAY, args={"amount": amount, "card": "6222020200112233"}), + node("out", "output", fields=[{"name": "单号", "value": "{{ nodes.pay.receipt.id }}"}])] + return {"nodes": nodes, "edges": [{"source": "start", "target": "pay"}, {"source": "pay", "target": "out"}]} + + +ANN = Principal(username="ann", role="admin", mode="off") + + +async def _wait(run_id: str, statuses: tuple[str, ...], timeout: float = 20.0) -> Run: + deadline = asyncio.get_event_loop().time() + timeout + while asyncio.get_event_loop().time() < deadline: + async with SessionLocal() as session: + row = await session.get(Run, run_id) + if row and row.status in statuses and not run_manager.is_active(run_id): + return row + await asyncio.sleep(0.05) + raise AssertionError(f"运行 {run_id} 没有进入 {statuses}") + + +async def _task(run_id: str, kind: str) -> Task: + for _ in range(100): + async with SessionLocal() as session: + row = (await session.execute(select(Task).where(Task.run_id == run_id, Task.kind == kind, + Task.status == "pending"))).scalars().first() + if row is not None: + return row + await asyncio.sleep(0.05) + raise AssertionError(f"没有开出 {kind} 待办") + + +async def _records(run_id: str) -> list[ActionRecord]: + async with SessionLocal() as session: + return list((await session.execute(select(ActionRecord).where(ActionRecord.run_id == run_id) + .order_by(ActionRecord.attempt))).scalars()) + + +async def _events(run_id: str, kind: str) -> list[RunEvent]: + async with SessionLocal() as session: + return list((await session.execute(select(RunEvent).where(RunEvent.run_id == run_id, RunEvent.type == kind) + .order_by(RunEvent.seq))).scalars()) + + +async def _confirm(run_id: str) -> Task: + """等确认卡出来,按卡上的内容指纹确认。""" + task = await _task(run_id, "action_confirm") + card = (await tasks.detail(task.id, ANN))["payload"] + out = await tasks.respond(task.id, ANN, decision="approve", payload_hash=card["payload_hash"]) + assert out["resolved"] is True + return task + + +async def _formal(graph: dict, **inputs) -> str: + run = await run_manager.start(graph=graph, input_payload=inputs or {"amount": 120}, run_class="formal") + await _wait(run.id, ("interrupted", "failed", "succeeded")) + return run.id + + +async def test_confirm_card_is_masked_and_sends_exactly_once(erp, capabilities): + capabilities(await _endpoint(test=True), lookup=False) + run_id = await _formal(pay_graph()) + task = await _task(run_id, "action_confirm") + card = (await tasks.detail(task.id, ANN))["payload"] + assert card["kind"] == "action_confirm" and card["environment"] == "prod" + assert card["preview"] == {"amount": "120", "card": "••••"} + assert card["payload_hash"] == actions.payload_hash(PAY, {"amount": "120", "card": "6222020200112233"}) + assert erp.requests == [] # 确认之前一次都不发 + + with pytest.raises(tasks.TaskRefused) as wrong: + await tasks.respond(task.id, ANN, decision="approve", payload_hash="0" * 64) + assert wrong.value.status_code == 422 and wrong.value.code == "payload_hash_mismatch" + + await _confirm(run_id) + final = await _wait(run_id, ("succeeded", "failed")) + assert final.status == "succeeded", final.error + assert final.output["单号"].startswith("PAY-") + assert len(erp.writes("prod")) == 1 + [rec] = await _records(run_id) + assert rec.state == "confirmed" and rec.attempt == 1 and rec.idem_key == f"{rec.action_key}:1" + assert rec.preview["card"] == "••••" and rec.confirm_task_id == task.id and rec.receipt["artifact"] + assert erp.requests[0]["idem_key"] == rec.idem_key + kinds = [e.type for e in await _events(run_id, "tool.start")] + assert len(kinds) == 1 + started = (await _events(run_id, "tool.start"))[0].data + assert started["action_key"] == rec.action_key and started["args"]["card"] == "••••" + for kind in ("action.prepared", "action.sent", "action.confirmed"): + assert len(await _events(run_id, kind)) == 1, kind + + +async def test_changed_upstream_after_confirm_refuses_to_send(erp, capabilities): + capabilities(await _endpoint(test=True), lookup=False) + run_id = await _formal(pay_graph()) + await _task(run_id, "action_confirm") + async with SessionLocal() as session: # 确认之后上游变了:节点恢复时重新渲染出来的参数不一样 + run = await session.get(Run, run_id) + graph = dict(run.graph) + graph["nodes"] = [dict(n) for n in graph["nodes"]] + pay = next(n for n in graph["nodes"] if n["id"] == "pay") + pay["data"] = {**pay["data"], "config": {**pay["data"]["config"], "args": {"amount": "999", "card": "x"}}} + run.graph = graph + await session.commit() + await _confirm(run_id) + final = await _wait(run_id, ("succeeded", "failed")) + assert final.status == "failed" and "内容指纹对不上" in final.error + assert erp.requests == [] and await _records(run_id) == [] + + +async def test_unknown_result_with_lookup_is_reconciled_automatically_and_sent_once(erp, capabilities): + capabilities(await _endpoint(test=True), lookup=True) + erp.mode = "timeout_after_write" + run_id = await _formal(pay_graph()) + await _confirm(run_id) + held = await _wait(run_id, ("interrupted", "failed", "succeeded")) + assert held.status == "interrupted" and held.manifest_hash is None + [rec] = await _records(run_id) + assert rec.state == "unknown" and rec.next_lookup_at is not None + assert len(await _events(run_id, "action.unknown")) == 1 + with pytest.raises(ContinueRefused) as refused: + await run_manager.continue_failed(run_id) + assert refused.value.code == "action_unresolved" + + assert await actions.tick(utcnow()) == 0 # 还没到第一次查询的时候 + assert await actions.tick(utcnow() + timedelta(minutes=2)) == 1 + final = await _wait(run_id, ("succeeded", "failed")) + assert final.status == "succeeded", final.error + assert len(erp.writes()) == 1 and len(erp.lookups) == 1 + [rec] = await _records(run_id) + assert rec.state == "confirmed" and rec.resolution["how"] == "lookup" + assert final.output["单号"] == erp.payments[("prod", rec.idem_key)]["id"] + assert len(await _events(run_id, "action.reconciled")) == 1 + + +async def test_without_lookup_manual_resend_uses_attempt_two(erp, capabilities): + capabilities(await _endpoint(test=True), lookup=False) + erp.mode = "timeout_after_write" + run_id = await _formal(pay_graph()) + await _confirm(run_id) + assert (await _wait(run_id, ("interrupted", "failed", "succeeded"))).status == "interrupted" + task = await _task(run_id, "reconcile") + detail = await tasks.detail(task.id, ANN) + assert detail["payload"]["options"] == ["confirmed", "not_applied_resend", "not_applied_abandon"] + assert detail["payload"]["preview"]["card"] == "••••" + + erp.mode = "ok" + out = await tasks.respond(task.id, ANN, decision="not_applied_resend", note="对方说没收到") + assert out["resolved"] is True + final = await _wait(run_id, ("succeeded", "failed")) + assert final.status == "succeeded", final.error + first, second = await _records(run_id) + assert (first.state, first.resolution["outcome"]) == ("failed", "resend") + assert second.state == "confirmed" and second.idem_key.endswith(":2") + assert [r["idem_key"].rsplit(":", 1)[1] for r in erp.writes()] == ["1", "2"] + assert (await tasks.detail(task.id, ANN))["status"] == "resolved" + async with SessionLocal() as session: # 接着跑时确认的答复取自断点:内容指纹没变,不再要一次确认 + confirms = (await session.execute(select(Task).where(Task.run_id == run_id, + Task.kind == "action_confirm"))).scalars().all() + assert len(confirms) == 1 + + +async def test_manual_confirm_hands_back_the_external_id_without_sending(erp, capabilities): + capabilities(await _endpoint(test=True), lookup=False) + erp.mode = "timeout_after_write" + run_id = await _formal(pay_graph()) + await _confirm(run_id) + await _wait(run_id, ("interrupted",)) + task = await _task(run_id, "reconcile") + await tasks.respond(task.id, ANN, decision="confirmed", values={"external_id": "PAY-777"}) + final = await _wait(run_id, ("succeeded", "failed")) + assert final.status == "succeeded", final.error + assert len(erp.writes()) == 1 + + +async def test_exploratory_run_without_test_environment_is_simulated(erp, capabilities): + capabilities(await _endpoint(test=False), lookup=False) + run = await run_manager.start(graph=pay_graph(), input_payload={"amount": 5}) + task = await _task(run.id, "action_confirm") + assert (await tasks.detail(task.id, ANN))["payload"]["environment"] == "simulated" + await _confirm(run.id) + final = await _wait(run.id, ("succeeded", "failed")) + assert final.status == "succeeded", final.error + assert erp.requests == [] + [rec] = await _records(run.id) + assert rec.state == "simulated" and rec.receipt["simulated"] is True + assert len(await _events(run.id, "action.simulated")) == 1 + + +async def test_exploratory_run_with_test_environment_writes_to_test(erp, capabilities): + capabilities(await _endpoint(test=True), lookup=False) + run = await run_manager.start(graph=pay_graph(), input_payload={"amount": 5}) + await _confirm(run.id) + assert (await _wait(run.id, ("succeeded", "failed"))).status == "succeeded" + assert len(erp.writes("test")) == 1 and erp.writes("prod") == [] + + +async def test_a_4xx_fails_and_continuing_does_not_resend(erp, capabilities): + capabilities(await _endpoint(test=True), lookup=False) + erp.mode = "error_4xx" + run_id = await _formal(pay_graph()) + await _confirm(run_id) + final = await _wait(run_id, ("succeeded", "failed", "interrupted")) + assert final.status == "failed" and "HTTP 422" in final.error + erp.mode = "ok" + await run_manager.continue_failed(run_id) + again = await _wait(run_id, ("succeeded", "failed")) + assert again.status == "failed" and "不会自动重发" in again.error + assert len(erp.writes()) == 1 + + +async def test_sent_then_restart_becomes_unknown(capabilities, monkeypatch): + """请求发出去、还没回话时进程没了:重启后记录是 sent,收成结果未知、开核实待办,绝不当成没发过。""" + from app.capabilities import registry + + capabilities(await _endpoint(test=True), lookup=False) + gate = asyncio.Event() + sent = asyncio.Event() + + async def hang(*args, **kwargs): + sent.set() + await gate.wait() + + monkeypatch.setattr(registry, "endpoint_request", hang) + run_id = await _formal(pay_graph()) + await _confirm(run_id) + await asyncio.wait_for(sent.wait(), 10) + await run_manager.shutdown() # 关停:执行任务被取消,运行挂起 + [rec] = await _records(run_id) + assert rec.state == "sent" + await run_manager.setup() + assert await actions.startup() == 1 + [rec] = await _records(run_id) + assert rec.state == "unknown" + await _task(run_id, "reconcile") + assert len(await _events(run_id, "action.unknown")) == 1 + assert await actions.startup() == 1 and len(await _events(run_id, "action.unknown")) == 1 # 不重复安排 + + +async def test_continue_api_refuses_while_unresolved(erp, capabilities): + from app.main import app + + capabilities(await _endpoint(test=True), lookup=False) + erp.mode = "timeout_after_write" + run_id = await _formal(pay_graph()) + await _confirm(run_id) + await _wait(run_id, ("interrupted",)) + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: + resp = await client.post(f"/api/runs/{run_id}/continue", json={}) + assert resp.status_code == 409 and resp.json()["code"] == "action_unresolved" + listed = (await client.get(f"/api/actions?run_id={run_id}")).json()["items"] + assert [r["state"] for r in listed] == ["unknown"] and listed[0]["preview"]["card"] == "••••" + + +def test_schedule_overlap_says_the_run_is_waiting_for_reconciliation(): + from app.scheduler.service import overlap_reason + + assert "核实" in overlap_reason("abcdef123", "interrupted", False, True) + assert "服务重启" in overlap_reason("abcdef123", "interrupted", False) + + +def test_masking_by_json_pointer(): + doc = {"a": {"b": "secret", "c": [1, "x"]}, "d~e": 1, "f/g": 2} + out = actions.masked(doc, ["/a/b", "/a/c/1", "/d~0e", "/f~1g", "/missing/x"]) + assert out == {"a": {"b": "••••", "c": [1, "••••"]}, "d~e": "••••", "f/g": "••••"} + assert doc["a"]["b"] == "secret" diff --git a/backend/tests/test_process_hooks.py b/backend/tests/test_process_hooks.py new file mode 100644 index 00000000..90fb6922 --- /dev/null +++ b/backend/tests/test_process_hooks.py @@ -0,0 +1,256 @@ +"""入站签名钩子(app/process/hooks.py,设计文档 §2.4 第 8 条、§7.2)。 + +签名同出站通用格式:HMAC-SHA256(f"{ts}." + body),时间窗 5 分钟;Idempotency-Key 去重——处理成功过的同一个键交回上次的 +结果、不再处理,被拒的可以用同一个键重试。signal 按关联键解决「等外部系统」的待办;start 按入口发起实例(来源记 hook)。 +""" +from __future__ import annotations + +import asyncio +import json +import time +import uuid +from types import SimpleNamespace + +import pytest +from httpx import ASGITransport, AsyncClient +from sqlalchemy import select + +from app.db.base import SessionLocal +from app.db.models import App, AppVersion, HookReceipt, Run, RunEvent, Task +from app.engine.runner import run_manager +from app.notify.core import sign +from app.process import hooks + + +@pytest.fixture(autouse=True) +async def engine_up(): + await run_manager.setup() + yield + await run_manager.shutdown() + + +@pytest.fixture +async def client(): + from app.main import app + + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: + yield c + + +def node(nid, ntype, **config): + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +async def _app() -> str: + async with SessionLocal() as session: + app = App(name=f"付款-{uuid.uuid4().hex[:6]}", published_version=1) + session.add(app) + await session.flush() + session.add(AppVersion(app_id=app.id, version=1, spec={}, spec_hash="h" * 64, bindings={})) + await session.commit() + return app.id + + +async def _hook(app_id: str, kind: str, entry_id: str | None = None) -> tuple[str, str]: + async with SessionLocal() as session: + row, secret = await hooks.create_hook(session, app_id, name="paid" if kind == "signal" else "new", kind=kind, + entry_id=entry_id) + await session.commit() + return row.id, secret + + +def signed(secret: str, payload: dict, *, key: str | None, ts: int | None = None) -> tuple[dict, bytes]: + body = json.dumps(payload).encode() + stamp = str(ts if ts is not None else int(time.time())) + headers = {"X-AgentLab-Timestamp": stamp, "X-AgentLab-Signature": f"sha256={sign(secret, stamp, body)}", + "Content-Type": "application/json"} + if key: + headers["Idempotency-Key"] = key + return headers, body + + +async def _wait(run_id: str, statuses: tuple[str, ...], timeout: float = 20.0) -> Run: + deadline = asyncio.get_event_loop().time() + timeout + while asyncio.get_event_loop().time() < deadline: + async with SessionLocal() as session: + row = await session.get(Run, run_id) + if row and row.status in statuses and not run_manager.is_active(row.id): + return row + await asyncio.sleep(0.05) + raise AssertionError(f"运行 {run_id} 没有进入 {statuses}") + + +async def _waiting_run(app_id: str, order: str) -> str: + graph = {"nodes": [node("start", "input", fields=[{"name": "order_id"}]), + node("wait", "human", mode="input", title="等付款回调", + assignee={"kind": "hook", "correlation": "{{ input.order_id }}"}), + node("out", "output", fields=[{"name": "付款单", "value": "{{ nodes.wait.value.payment_id }}"}])], + "edges": [{"source": "start", "target": "wait"}, {"source": "wait", "target": "out"}]} + run = await run_manager.start(graph=graph, input_payload={"order_id": order}, app_id=app_id, app_version=1) + await _wait(run.id, ("interrupted",)) + return run.id + + +async def test_signal_resolves_the_waiting_task_once(client): + app_id = await _app() + hook_id, secret = await _hook(app_id, "signal") + order = f"ORD-{uuid.uuid4().hex[:6]}" + run_id = await _waiting_run(app_id, order) + async with SessionLocal() as session: + task = (await session.execute(select(Task).where(Task.run_id == run_id))).scalar_one() + assert task.kind == "signal" and task.correlation_key == order + + payload = {"correlation": order, "payment_id": "PAY-9"} + headers, body = signed(secret, payload, key="cb-1") + first = await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers) + assert first.status_code == 200, first.text + assert first.json()["task_id"] == task.id and not first.json().get("duplicate") + assert (await _wait(run_id, ("succeeded", "failed"))).output.get("付款单") == "PAY-9" + + headers, body = signed(secret, payload, key="cb-1") # 重复投递:同一个幂等键 + again = await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers) + assert again.status_code == 200 and again.json()["duplicate"] is True and again.json()["task_id"] == task.id + async with SessionLocal() as session: + resumed = (await session.execute(select(RunEvent).where(RunEvent.run_id == run_id, + RunEvent.type == "run.resumed"))).scalars().all() + receipts = (await session.execute(select(HookReceipt).where( + HookReceipt.hook_id == hook_id, HookReceipt.status != "signature"))).scalars().all() + assert len(resumed) == 1 + assert [(r.idem_key, r.status) for r in receipts] == [("cb-1", "accepted")] + + +async def test_signature_window_and_idempotency_key_are_required(client): + app_id = await _app() + hook_id, secret = await _hook(app_id, "signal") + payload = {"correlation": "nope"} + + headers, body = signed("wrong-secret", payload, key="k") + assert (await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).status_code == 401 + headers, body = signed(secret, payload, key="k", ts=int(time.time()) - 301) + stale = await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers) + assert stale.status_code == 401 and "时间窗" in stale.json()["detail"] + headers, body = signed(secret, payload, key=None) + assert (await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).status_code == 400 + tampered_headers, body = signed(secret, payload, key="k") + tampered = await client.post(f"/api/hooks/{hook_id}", content=body.replace(b"nope", b"nopf"), + headers=tampered_headers) + assert tampered.status_code == 401 + assert (await client.post("/api/hooks/missing", content=body, headers=tampered_headers)).status_code == 404 + + +async def test_a_rejected_signal_can_be_retried_with_the_same_key(client): + app_id = await _app() + hook_id, secret = await _hook(app_id, "signal") + order = f"ORD-{uuid.uuid4().hex[:6]}" + headers, body = signed(secret, {"correlation": order, "payment_id": "PAY-1"}, key="early") + early = await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers) + assert early.status_code == 404 # 回调比流程走到这一步还早 + run_id = await _waiting_run(app_id, order) + headers, body = signed(secret, {"correlation": order, "payment_id": "PAY-1"}, key="early") + retried = await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers) + assert retried.status_code == 200, retried.text + assert (await _wait(run_id, ("succeeded", "failed"))).status == "succeeded" + + +async def test_start_hook_launches_through_the_entry_with_origin_hook(client, monkeypatch): + from app.apps import entries + + calls: list[dict] = [] + + async def start_entry(app_id, entry_id, values, principal, *, origin_kind="app_entry", hook=None, hook_key=None): + calls.append({"app_id": app_id, "entry": entry_id, "values": values, "origin_kind": origin_kind, + "hook": hook, "hook_key": hook_key, "verified": principal.verified}) + return SimpleNamespace(id="run-from-hook") + + monkeypatch.setattr(entries, "start_entry", start_entry) + app_id = await _app() + hook_id, secret = await _hook(app_id, "start", entry_id="apply") + headers, body = signed(secret, {"amount": 12}, key="new-1") + resp = await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers) + assert resp.status_code == 200 and resp.json()["run_id"] == "run-from-hook" + assert calls == [{"app_id": app_id, "entry": "apply", "values": {"amount": 12}, "origin_kind": "hook", + "hook": "new", "hook_key": "new-1", "verified": True}] + headers, body = signed(secret, {"amount": 12}, key="new-1") + assert (await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).json()["duplicate"] is True + assert len(calls) == 1 + + +async def test_a_captured_request_cannot_be_replayed_under_another_key(client, monkeypatch): + """签名里没有幂等键:同一个签名只认头一回带的那个键。原样重发照常去重,换个键重放拒绝。""" + from app.apps import entries + + calls: list[str] = [] + + async def start_entry(app_id, entry_id, values, principal, **kw): + calls.append(kw["hook_key"]) + return SimpleNamespace(id=f"run-{len(calls)}") + + monkeypatch.setattr(entries, "start_entry", start_entry) + app_id = await _app() + hook_id, secret = await _hook(app_id, "start", entry_id="apply") + headers, body = signed(secret, {"amount": 5}, key="pay-1") + assert (await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).status_code == 200 + assert (await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).json()["duplicate"] is True + replay = await client.post(f"/api/hooks/{hook_id}", content=body, headers={**headers, "Idempotency-Key": "pay-2"}) + assert replay.status_code == 401 and "签名已经用过" in replay.json()["detail"] + assert calls == ["pay-1"] + + +async def test_a_receipt_stuck_in_processing_can_be_reclaimed_without_starting_twice(client, monkeypatch): + """处理中途服务停了:回执停在 processing。超过 STALE_S 之后同一个键可以重新认领;实例其实已经发起的,交回它。""" + from datetime import timedelta + + from app.apps import entries + from app.db.base import utcnow + + calls: list[str] = [] + + async def start_entry(app_id, entry_id, values, principal, **kw): + calls.append(kw["hook_key"]) + return SimpleNamespace(id="run-new") + + monkeypatch.setattr(entries, "start_entry", start_entry) + app_id = await _app() + hook_id, secret = await _hook(app_id, "start", entry_id="apply") + async with SessionLocal() as session: + session.add(HookReceipt(hook_id=hook_id, idem_key="fresh", status="processing")) + session.add(HookReceipt(hook_id=hook_id, idem_key="stuck", status="processing", + received_at=utcnow() - timedelta(seconds=hooks.STALE_S + 5))) + session.add(Run(workflow_name="实例", run_class="formal", app_id=app_id, origin_kind="hook", + origin={"hook": "new", "hook_key": "stuck"}, id="run-before-crash")) + await session.commit() + headers, body = signed(secret, {"amount": 1}, key="fresh") + assert (await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers)).status_code == 409 + headers, body = signed(secret, {"amount": 2}, key="stuck") + resp = await client.post(f"/api/hooks/{hook_id}", content=body, headers=headers) + assert resp.status_code == 200 and resp.json()["run_id"] == "run-before-crash" + assert calls == [] + + +async def test_hook_management_functions(): + app_id = await _app() + async with SessionLocal() as session: + with pytest.raises(ValueError): + await hooks.create_hook(session, app_id, name="x", kind="start") # 发起实例要指定入口 + row, secret = await hooks.create_hook(session, app_id, name="paid", kind="signal") + await session.commit() + listed = await hooks.list_hooks(session, app_id) + assert [h["id"] for h in listed] == [row.id] and "secret" not in listed[0] + assert listed[0]["url_path"] == f"/api/hooks/{row.id}" + assert row.secret != secret and len(secret) >= 32 # 库里是密文 + assert await hooks.delete_hook(session, "other-app", row.id) is False + assert await hooks.delete_hook(session, app_id, row.id) is True + await session.commit() + assert await hooks.list_hooks(session, app_id) == [] + + +def test_outbound_generic_signature_verifies_inbound(): + """出站通用格式的签名和入站验的是同一套:接收端拿同一个密钥能验过。""" + from app.notify import core + + msg = core.Message(title="测试") + _, headers, body = core.build_request("generic", "https://hooks.example.com/in", "s3cret", msg, + generic={"event": "test"}) + hooks.verify("s3cret", headers, body) + with pytest.raises(hooks.HookRejected): + hooks.verify("other", headers, body) diff --git a/backend/tests/test_process_tasks.py b/backend/tests/test_process_tasks.py new file mode 100644 index 00000000..10213380 --- /dev/null +++ b/backend/tests/test_process_tasks.py @@ -0,0 +1,486 @@ +"""待办服务(app/process/tasks.py):一次中断 = 一条 Approval(给引擎)+ 一条 Task(给人)。 + +会签按策略在待办服务这一层汇总,凑齐了才恢复一次运行(引擎、重放都不改);时限到了提醒、升级、驳回,永不自动批准; +老接口(/api/approvals/{id}/decide、/api/runs/{id}/resume)改成垫片,请求体照旧;存量的待审批启动时补建待办。 +""" +from __future__ import annotations + +import asyncio +import uuid +from datetime import timedelta + +import pytest +from httpx import ASGITransport, AsyncClient +from sqlalchemy import delete, select + +from app.api.deps import Principal +from app.db.base import SessionLocal, utcnow +from app.db.models import ( + App, Approval, AppVersion, Group, GroupMember, Run, RunEvent, Task, TaskAssignment, User, +) +from app.engine.runner import run_manager +from app.process import tasks +from app.process.tasks import TaskRefused + + +@pytest.fixture(autouse=True) +async def engine_up(): + await run_manager.setup() + yield + await run_manager.shutdown() + + +@pytest.fixture +async def client(): + from app.main import app + + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: + yield c + + +def node(nid, ntype, **config): + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +def approval_flow(**human): + nodes = [node("start", "input", fields=[{"name": "q"}, {"name": "manager"}]), + node("review", "human", mode=human.pop("mode", "approve"), title="审一下", **human), + node("ok", "output", fields=[{"name": "结论", "value": "通过"}]), + node("no", "output", fields=[{"name": "结论", "value": "退回"}])] + edges = [{"source": "start", "target": "review"}, + {"source": "review", "target": "ok", "sourceHandle": "approved"}, + {"source": "review", "target": "no", "sourceHandle": "rejected"}] + return {"nodes": nodes, "edges": edges} + + +def off(name: str | None) -> Principal: + return Principal(username=name, role="admin", mode="off") + + +async def _wait(run_id: str, statuses: tuple[str, ...], timeout: float = 20.0) -> Run: + deadline = asyncio.get_event_loop().time() + timeout + while asyncio.get_event_loop().time() < 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.05) + raise AssertionError(f"运行 {run_id} 没有进入 {statuses}") + + +async def _task(run_id: str) -> Task: + for _ in range(100): + async with SessionLocal() as session: + row = (await session.execute(select(Task).where(Task.run_id == run_id, Task.status == "pending") + .order_by(Task.created_at.desc()))).scalars().first() + if row is not None: + return row + await asyncio.sleep(0.05) + raise AssertionError("没有开出待办") + + +async def _events(run_id: str, kind: str) -> list[RunEvent]: + async with SessionLocal() as session: + return list((await session.execute(select(RunEvent).where(RunEvent.run_id == run_id, RunEvent.type == kind) + .order_by(RunEvent.seq))).scalars()) + + +async def _people(*names: str, group: str | None = None) -> None: + async with SessionLocal() as session: + users = [] + for name in names: + user = User(username=name, role="user") + session.add(user) + users.append(user) + await session.flush() + if group: + g = Group(name=group) + session.add(g) + await session.flush() + for user in users: + session.add(GroupMember(group_id=g.id, user_id=user.id)) + await session.commit() + + +async def _app(slots: dict) -> tuple[str, int]: + async with SessionLocal() as session: + app = App(name=f"报销-{uuid.uuid4().hex[:6]}", published_version=1) + session.add(app) + await session.flush() + session.add(AppVersion(app_id=app.id, version=1, spec={"format": "agentlab.app/1"}, spec_hash="x" * 64, + bindings={"slots": slots})) + await session.commit() + return app.id, 1 + + +async def _app_run(graph: dict, slots: dict, **inputs) -> str: + app_id, version = await _app(slots) + run = await run_manager.start(graph=graph, input_payload={"q": "x", **inputs}, app_id=app_id, + app_version=version, started_by="dave") + await _wait(run.id, ("interrupted",)) + await run_manager.wait_idle(run.id) + return run.id + + +def _u() -> str: + return uuid.uuid4().hex[:6] + + +# -------------------------------------------------------------------------- +# 打开 +# -------------------------------------------------------------------------- + + +async def test_an_interrupt_opens_one_task_assigned_to_anyone_without_an_app(): + run = await run_manager.start(graph=approval_flow(), input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + assert task.kind == "approve" and task.policy == {"kind": "any"} and task.app_id is None + async with SessionLocal() as session: + approval = await session.get(Approval, task.approval_id) + rows = list((await session.execute(select(TaskAssignment).where(TaskAssignment.task_id == task.id))) + .scalars()) + assert approval.run_id == run.id + assert [(a.principal_kind, a.principal) for a in rows] == [("anyone", "")] + opened = await _events(run.id, "task.opened") + assert len(opened) == 1 and opened[0].data["task_id"] == task.id + + +async def test_task_kind_follows_the_interrupt_kind_not_approval_mode(): + graph = {"nodes": [node("start", "input", fields=[{"name": "q"}]), + node("t", "tool", tool="current_time", args={}, approval="always"), + node("out", "output", fields=[])], + "edges": [{"source": "start", "target": "t"}, {"source": "t", "target": "out"}]} + run = await run_manager.start(graph=graph, input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + assert task.kind == "tool" + out = await tasks.respond(task.id, off("ann"), decision="approve") + assert out["resolved"] is True + assert (await _wait(run.id, ("succeeded", "failed"))).status == "succeeded" + + +async def test_slot_expands_group_members_at_open_time(): + a, b, g = f"a{_u()}", f"b{_u()}", f"finance{_u()}" + await _people(a, b, group=g) + run_id = await _app_run(approval_flow(assignee={"kind": "slot", "slot": "finance"}, policy="all"), + {"finance": [{"kind": "group", "name": g}]}) + task = await _task(run_id) + async with SessionLocal() as session: + rows = list((await session.execute(select(TaskAssignment).where(TaskAssignment.task_id == task.id))) + .scalars()) + assert sorted((r.principal_kind, r.principal, r.source) for r in rows) == [ + ("user", a, "slot:finance"), ("user", b, "slot:finance")] + assert task.policy == {"kind": "all"} and task.app_id is not None + + +async def test_initiator_and_expression_assignees(): + run_id = await _app_run(approval_flow(assignee={"kind": "initiator"}), {}) + task = await _task(run_id) + detail = await tasks.detail(task.id, off(None)) + assert [(x["principal_kind"], x["principal"]) for x in detail["assignments"]] == [("user", "dave")] + + who = f"m{_u()}" + run_id = await _app_run(approval_flow(assignee={"kind": "expression", "expr": "input.manager"}), {}, manager=who) + detail = await tasks.detail((await _task(run_id)).id, off(None)) + assert [(x["principal_kind"], x["principal"], x["source"]) for x in detail["assignments"]] == [ + ("user", who, "expression")] + + +# -------------------------------------------------------------------------- +# 会签 +# -------------------------------------------------------------------------- + + +async def test_all_policy_waits_for_everyone_then_resumes_once_with_signoffs(): + a, b, g = f"a{_u()}", f"b{_u()}", f"finance{_u()}" + await _people(a, b, group=g) + run_id = await _app_run(approval_flow(assignee={"kind": "slot", "slot": "finance"}, policy="all"), + {"finance": [{"kind": "group", "name": g}]}) + task = await _task(run_id) + + first = await tasks.respond(task.id, off(a), decision="approve", note="票据齐全") + assert first["resolved"] is False + assert (await _wait(run_id, ("interrupted",))).status == "interrupted" + with pytest.raises(TaskRefused) as again: + await tasks.respond(task.id, off(a), decision="approve") + assert again.value.status_code == 409 + + second = await tasks.respond(task.id, off(b), decision="approve") + assert second["resolved"] is True + final = await _wait(run_id, ("succeeded", "failed")) + assert final.status == "succeeded" and final.output.get("结论") == "通过" + resolved = [e for e in await _events(run_id, "human.resolved") if e.node_id == "review"] + assert len(resolved) == 1 + signoffs = resolved[0].data["response"]["signoffs"] + assert [(s["by"], s["decision"]) for s in signoffs] == [(a, "approve"), (b, "approve")] + responded = await _events(run_id, "task.responded") + assert [(e.data["index"], e.data["of"]) for e in responded] == [(1, 2), (2, 2)] + assert len(await _events(run_id, "run.resumed")) == 1 + + +async def test_all_policy_one_veto_rejects_immediately(): + a, b, g = f"a{_u()}", f"b{_u()}", f"finance{_u()}" + await _people(a, b, group=g) + run_id = await _app_run(approval_flow(assignee={"kind": "slot", "slot": "finance"}, policy="all"), + {"finance": [{"kind": "group", "name": g}]}) + task = await _task(run_id) + out = await tasks.respond(task.id, off(b), decision="reject", note="金额超标") + assert out["resolved"] is True + final = await _wait(run_id, ("succeeded", "failed")) + assert final.output.get("结论") == "退回" + detail = await tasks.detail(task.id, off(None)) + assert detail["status"] == "resolved" + assert sorted(x["status"] for x in detail["assignments"]) == ["responded", "withdrawn"] + + +async def test_quorum_two_of_three(): + a, b, c, g = f"a{_u()}", f"b{_u()}", f"c{_u()}", f"finance{_u()}" + await _people(a, b, c, group=g) + run_id = await _app_run(approval_flow(assignee={"kind": "slot", "slot": "finance"}, policy={"quorum": 2}), + {"finance": [{"kind": "group", "name": g}]}) + task = await _task(run_id) + assert (await tasks.respond(task.id, off(a), decision="reject"))["resolved"] is False + assert (await tasks.respond(task.id, off(b), decision="approve"))["resolved"] is False + assert (await tasks.respond(task.id, off(c), decision="approve"))["resolved"] is True + assert (await _wait(run_id, ("succeeded", "failed"))).output.get("结论") == "通过" + + +async def test_login_on_only_assignees_can_respond(): + a, g = f"a{_u()}", f"finance{_u()}" + await _people(a, group=g) + run_id = await _app_run(approval_flow(assignee={"kind": "slot", "slot": "finance"}), + {"finance": [{"kind": "group", "name": g}]}) + task = await _task(run_id) + stranger = Principal(username="eve", role="user", mode="local", verified=True) + me = Principal(username=a, role="user", mode="local", verified=True) + assert (await tasks.detail(task.id, stranger))["can_respond"] is False + assert task.id not in [t["id"] for t in await tasks.list_tasks(stranger)] + assert task.id in [t["id"] for t in await tasks.list_tasks(me)] + with pytest.raises(TaskRefused) as refused: + await tasks.respond(task.id, stranger, decision="approve") + assert refused.value.status_code == 403 + out = await tasks.respond(task.id, me, decision="approve") + assert out["resolved"] is True + rows = out["task"]["assignments"] + assert rows[0]["responded_by"] == a and rows[0]["verified"] is True + + +# -------------------------------------------------------------------------- +# 时限:提醒、升级、超时驳回 +# -------------------------------------------------------------------------- + + +async def test_escalation_adds_the_director_whose_answer_decides(): + a, b, g, boss = f"a{_u()}", f"b{_u()}", f"finance{_u()}", f"boss{_u()}" + await _people(a, b, group=g) + run_id = await _app_run( + approval_flow(assignee={"kind": "slot", "slot": "finance"}, policy="all", due="PT1H", + on_timeout={"action": "escalate", "to_slot": "director"}), + {"finance": [{"kind": "group", "name": g}], "director": [{"kind": "user", "name": boss}]}) + task = await _task(run_id) + assert task.due_at is not None + assert await tasks.tick(utcnow()) == 0 # 还没到时限 + assert await tasks.tick(utcnow() + timedelta(hours=2)) == 1 + assert await tasks.tick(utcnow() + timedelta(hours=3)) == 0 # 只升一次 + detail = await tasks.detail(task.id, off(None)) + assert detail["escalation_level"] == 1 and detail["status"] == "pending" + assert ("user", boss, "escalation") in [(x["principal_kind"], x["principal"], x["source"]) + for x in detail["assignments"]] + assert len(await _events(run_id, "task.escalated")) == 1 + out = await tasks.respond(task.id, off(boss), decision="approve") + assert out["resolved"] is True + assert (await _wait(run_id, ("succeeded", "failed"))).output.get("结论") == "通过" + + +async def test_timeout_reject_never_approves(): + run = await run_manager.start( + graph=approval_flow(due="PT30M", on_timeout={"action": "reject"}), input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + await run_manager.wait_idle(run.id) + task = await _task(run.id) + assert await tasks.tick(utcnow() + timedelta(hours=1)) == 1 + final = await _wait(run.id, ("succeeded", "failed")) + assert final.output.get("结论") == "退回" + assert len(await _events(run.id, "task.expired")) == 1 + async with SessionLocal() as session: + row = await session.get(Task, task.id) + assert row.status == "resolved" and row.outcome["decision"] == "expired_reject" + + +async def test_remind_repeats_every_interval(): + run = await run_manager.start( + graph=approval_flow(due="PT1H", on_timeout={"action": "remind", "every": "PT1H"}), input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + await run_manager.wait_idle(run.id) + task = await _task(run.id) + later = utcnow() + timedelta(hours=1, minutes=1) + assert await tasks.tick(later) == 1 + assert await tasks.tick(later) == 0 + assert await tasks.tick(later + timedelta(hours=1)) == 1 + assert len(await _events(run.id, "task.reminded")) == 2 + async with SessionLocal() as session: + assert (await session.get(Task, task.id)).status == "pending" + + +# -------------------------------------------------------------------------- +# 表单 +# -------------------------------------------------------------------------- + + +async def test_form_values_are_validated_and_reach_the_node(monkeypatch, client): + from app.apps import forms + + monkeypatch.setattr(forms, "validate_form_schema", lambda form: []) + monkeypatch.setattr(forms, "validate_form", lambda form, values: ( + [] if isinstance(values.get("amount"), (int, float)) else + [{"path": "amount", "code": "type", "message": "金额需要是数字"}])) + form = {"format": "agentlab.form/1", "type": "object", "required": ["amount"], + "properties": {"amount": {"type": "number", "title": "金额"}}} + graph = {"nodes": [node("start", "input", fields=[{"name": "q"}]), + node("fill", "human", mode="input", title="补金额", form=form), + node("out", "output", fields=[{"name": "金额", "value": "{{ nodes.fill.value.amount }}"}])], + "edges": [{"source": "start", "target": "fill"}, {"source": "fill", "target": "out"}]} + run = await run_manager.start(graph=graph, input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + assert task.kind == "input" and task.form == form + + bad = await client.post(f"/api/tasks/{task.id}/respond", json={"decision": "submit", "values": {"amount": "多"}}) + assert bad.status_code == 422 and bad.json()["code"] == "form_invalid" + assert bad.json()["errors"] == [{"path": "amount", "code": "type", "message": "金额需要是数字"}] + good = await client.post(f"/api/tasks/{task.id}/respond", json={"decision": "submit", "values": {"amount": 12}}) + assert good.status_code == 200 and good.json()["resolved"] is True + assert (await _wait(run.id, ("succeeded", "failed"))).output.get("金额") == "12" + + +async def test_form_from_falls_back_to_the_static_form_with_a_warning(monkeypatch): + from app.apps import forms + + static = {"format": "agentlab.form/1", "type": "object", "properties": {"note": {"type": "string"}}} + monkeypatch.setattr(forms, "validate_form_schema", + lambda form: [] if form is static or form == static else ["用了不支持的 pattern"]) + graph = {"nodes": [node("start", "input", fields=[{"name": "q"}]), + node("ask", "transform", mode="expression", + expression="{'type': 'object', 'properties': {'x': {'type': 'string', 'pattern': 'a+'}}}", + assign_to="asked"), + node("fill", "human", mode="input", title="补充", form=static, form_from="vars.asked"), + node("out", "output", fields=[])], + "edges": [{"source": "start", "target": "ask"}, {"source": "ask", "target": "fill"}, + {"source": "fill", "target": "out"}]} + run = await run_manager.start(graph=graph, input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + assert task.form == static + warned = [e for e in await _events(run.id, "log") if e.data.get("code") == "form_from_invalid"] + assert len(warned) == 1 and "pattern" in warned[0].data["message"] + + +# -------------------------------------------------------------------------- +# 老接口:垫片 +# -------------------------------------------------------------------------- + + +async def test_decide_shim_still_works_and_goes_through_the_task(client): + run = await run_manager.start(graph=approval_flow(), input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + resp = await client.post(f"/api/approvals/{task.approval_id}/decide", json={"approved": False, "note": "不行"}, + headers={"X-Actor": "zhang"}) + assert resp.status_code == 200, resp.text + assert (await _wait(run.id, ("succeeded", "failed"))).output.get("结论") == "退回" + detail = await tasks.detail(task.id, off(None)) + assert detail["status"] == "resolved" and detail["assignments"][0]["responded_by"] == "zhang" + async with SessionLocal() as session: + assert (await session.get(Approval, task.approval_id)).resolved_by == "zhang" + + +async def test_resume_shim_passes_the_raw_response_through(client): + graph = {"nodes": [node("start", "input", fields=[{"name": "q"}]), + node("fill", "human", mode="input", title="写一句"), + node("out", "output", fields=[{"name": "回复", "value": "{{ nodes.fill.value.choices.m1 }}"}])], + "edges": [{"source": "start", "target": "fill"}, {"source": "fill", "target": "out"}]} + run = await run_manager.start(graph=graph, input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + # 双盲裁决那种回复:value 里是 {choices, note},原样交给节点 + resp = await client.post(f"/api/runs/{run.id}/resume", json={ + "response": {"approved": True, "note": "用 B", "value": {"choices": {"m1": "b"}, "note": "用 B"}}, + "approval_id": task.approval_id}) + assert resp.status_code == 200, resp.text + assert (await _wait(run.id, ("succeeded", "failed"))).output.get("回复") == "b" + assert (await tasks.detail(task.id, off(None)))["status"] == "resolved" + + +async def test_startup_backfills_tasks_for_old_pending_approvals(): + run = await run_manager.start(graph=approval_flow(assignee={"kind": "initiator"}), input_payload={"q": "x"}, + started_by="dave") + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + async with SessionLocal() as session: # 模拟升级前的存量:只有审批,没有待办 + await session.execute(delete(Task).where(Task.id == task.id)) + await session.commit() + assert await tasks.startup() >= 1 + again = await _task(run.id) + assert again.approval_id == task.approval_id + assert await tasks.startup() == 0 # 幂等 + out = await tasks.respond(again.id, off("z"), decision="approve") + assert out["resolved"] is True + + +async def test_cancelling_the_run_closes_its_tasks(): + run = await run_manager.start(graph=approval_flow(), input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + await run_manager.wait_idle(run.id) + task = await _task(run.id) + assert await run_manager.cancel(run.id) == "cancelled" + detail = await tasks.detail(task.id, off(None)) + assert detail["status"] == "cancelled" and detail["can_respond"] is False + + +async def test_count_and_list_api(client): + run = await run_manager.start(graph=approval_flow(), input_payload={"q": "x"}) + await _wait(run.id, ("interrupted",)) + task = await _task(run.id) + count = (await client.get("/api/tasks/count?mine=1")).json() + assert set(count) == {"mine", "apps", "other"} and count["other"] >= 1 + listed = (await client.get(f"/api/tasks?assigned=me&run_id={run.id}")).json()["items"] + assert [t["id"] for t in listed] == [task.id] + item = listed[0] + assert item["payload"]["kind"] == "human_node" and item["can_respond"] is True + assert item["node_label"] == "review" + missing = await client.post(f"/api/tasks/{task.id}/respond", json={"decision": "maybe"}) + assert missing.status_code == 422 + + +async def test_reassign_and_remind(client): + run_id = await _app_run(approval_flow(assignee={"kind": "initiator"}), {}) + task = await _task(run_id) + resp = await client.post(f"/api/tasks/{task.id}/reassign", json={"to": [{"kind": "user", "name": "frank"}], + "note": "dave 休假"}) + assert resp.status_code == 200, resp.text + rows = {(a["principal"], a["status"], a["source"]) for a in resp.json()["assignments"]} + assert rows == {("dave", "withdrawn", "initiator"), ("frank", "pending", "reassign")} + assert len(await _events(run_id, "task.reassigned")) == 1 + frank = Principal(username="frank", role="user", mode="local", verified=True) + dave = Principal(username="dave", role="user", mode="local", verified=True) + assert (await tasks.detail(task.id, frank))["can_respond"] is True + assert (await tasks.detail(task.id, dave))["can_respond"] is False + with pytest.raises(TaskRefused) as refused: + await tasks.reassign(task.id, dave, to=[{"kind": "user", "name": "dave"}]) + assert refused.value.status_code == 403 # 开了登录,user 角色不能改派 + resp = await client.post(f"/api/tasks/{task.id}/remind") + assert resp.status_code == 200 and resp.json() == {"sent": 0} # 没有订阅的渠道 + assert len(await _events(run_id, "task.reminded")) == 1 + empty = await client.post(f"/api/tasks/{task.id}/reassign", json={"to": []}) + assert empty.status_code == 422 + + +async def test_user_role_only_sees_tasks_assigned_to_them(): + run_id = await _app_run(approval_flow(assignee={"kind": "initiator"}), {}) + task = await _task(run_id) + item = await tasks.detail(task.id, off(None)) + assert tasks.visible(item, Principal(username="dave", role="user", mode="local")) + assert not tasks.visible(item, Principal(username="eve", role="user", mode="local")) + assert tasks.visible(item, Principal(username="eve", role="builder", mode="local")) + assert tasks.visible(item, off(None)) diff --git a/backend/tests/test_replay_side_effects.py b/backend/tests/test_replay_side_effects.py index 4c3f9fc4..8035004a 100644 --- a/backend/tests/test_replay_side_effects.py +++ b/backend/tests/test_replay_side_effects.py @@ -315,3 +315,187 @@ async def test_continuing_an_old_failure_after_tools_ran_is_refused(monkeypatch) with pytest.raises(ValueError, match="重复执行"): await run_manager.continue_failed(run.id) assert len(CALLS) == 1 + + +# -------------------------------------------------------------------------- +# 跨重放协议的升级安全(应用平台 §7.5):流程实例一等几天,中间系统升级是常态。 +# +# 照「以后改了节点里 task 的调用顺序,PROTOCOL 加一」的规矩,把升级模拟成 PROTOCOL 从现在的 2 变成 3: +# 断点是按 2 写的,恢复时代码按 3 认。 +# -------------------------------------------------------------------------- + + +def _to_protocol_3(monkeypatch) -> None: + from app.engine import replay, runner + + assert replay.PROTOCOL == 2, "PROTOCOL 已经升过了:把这几条的「旧版」「新版」跟着改" + monkeypatch.setattr(runner, "PROTOCOL", 3) + + +async def _paused(run_id: str) -> None: + await _wait(run_id, ("interrupted",)) + await run_manager.wait_idle(run_id) + + +async def test_upgrade_pause_at_human_node_resumes(monkeypatch): + graph = chain( + node("start", "input", fields=[{"name": "question"}]), + node("h", "human", mode="approve", title="批一下", assignee={"kind": "initiator"}, due="PT4H"), + node("out", "output", fields=[]), + ) + run = await run_manager.start(graph=graph, input_payload={"question": "x"}, started_by="ann") + await _paused(run.id) + _to_protocol_3(monkeypatch) + from app.api.deps import Principal + from app.process import tasks + + task = (await tasks.list_tasks(Principal(username="ann", mode="off"), run_id=run.id))[0] + out = await tasks.respond(task["id"], Principal(username="ann", mode="off"), decision="approve") + assert out["resolved"] is True + assert (await _wait(run.id, ("failed", "succeeded"))).status == "succeeded" + + +async def test_upgrade_pause_mid_agent_is_refused_and_flagged_at_publish(monkeypatch): + from app.engine.governance import lint_for_publish + from app.engine.schema import GraphSpec + + _script_two_calls(monkeypatch) + graph = chain( + node("start", "input", fields=[{"name": "question"}]), + node("bot", "agent", prompt="记两笔", tools=["tally_side_effect"], approval="always", max_steps=5), + node("out", "output", fields=[]), + ) + flagged = {i.code for i in lint_for_publish(GraphSpec.model_validate(graph), level="published").issues} + assert "process.wait_mid_agent" in flagged # 应用按流程钉住它时,这一条会升成错误 + run = await run_manager.start(graph=graph, input_payload={"question": "x"}) + await _paused(run.id) + await run_manager.resume(run.id, {"approved": True}) + await asyncio.sleep(0.1) + await _paused(run.id) + assert len(CALLS) == 1 + _to_protocol_3(monkeypatch) + with pytest.raises(ValueError, match="重复执行"): + await run_manager.resume(run.id, {"approved": True}) + assert len(CALLS) == 1 + + +def _pay_graph(): + from tests.fakes.fake_erp import PAY + + return chain( + node("start", "input", fields=[{"name": "amount"}]), + node("pay", "tool", tool=PAY, args={"amount": "{{ input.amount }}", "card": "6222020200112233"}), + node("out", "output", fields=[{"name": "单号", "value": "{{ nodes.pay.receipt.id }}"}]), + ) + + +async def _confirm_card(run_id: str): + from app.api.deps import Principal + from app.process import tasks + + who = Principal(username="ann", mode="off") + [task] = [t for t in await tasks.list_tasks(who, run_id=run_id) if t["kind"] == "action_confirm"] + return await tasks.respond(task["id"], who, decision="approve", payload_hash=task["payload"]["payload_hash"]) + + +async def test_upgrade_pause_at_write_confirmation_resumes_and_sends_once(monkeypatch): + from tests.fakes.fake_erp import FakeErp, endpoint_row, install + + erp = FakeErp() + install(monkeypatch, erp, await endpoint_row(test=True), lookup=False) + run = await run_manager.start(graph=_pay_graph(), input_payload={"amount": 30}, run_class="formal") + await _paused(run.id) + _to_protocol_3(monkeypatch) + await _confirm_card(run.id) + final = await _wait(run.id, ("failed", "succeeded")) + assert final.status == "succeeded", final.error + assert len(erp.writes()) == 1 + + +async def test_upgrade_with_unknown_result_reconciles_and_continues_without_resending(monkeypatch): + """结果未知时停下的断点是按旧协议写的,节点里已经批过一次、发过一次(tool.start 带 action_key)。 + 升级后核实为已生效:_stale_replay 只对这种写入节点放行,节点重新执行时查到已生效,交回回执、不再发。""" + from app.api.deps import Principal + from app.db.models import ActionRecord + from app.process import tasks + from tests.fakes.fake_erp import FakeErp, endpoint_row, install + + erp = FakeErp(mode="timeout_after_write") + install(monkeypatch, erp, await endpoint_row(test=True), lookup=False) + run = await run_manager.start(graph=_pay_graph(), input_payload={"amount": 30}, run_class="formal") + await _paused(run.id) + await _confirm_card(run.id) + await _paused(run.id) + assert len(erp.writes()) == 1 + + _to_protocol_3(monkeypatch) + who = Principal(username="ann", mode="off") + [task] = [t for t in await tasks.list_tasks(who, run_id=run.id) if t["kind"] == "reconcile"] + out = await tasks.respond(task["id"], who, decision="confirmed", values={"external_id": "PAY-1001"}) + assert out["resolved"] is True + final = await _wait(run.id, ("failed", "succeeded")) + assert final.status == "succeeded", final.error + assert len(erp.writes()) == 1 # 不重发 + async with SessionLocal() as session: + [rec] = (await session.execute(select(ActionRecord).where(ActionRecord.run_id == run.id))).scalars().all() + assert rec.state == "confirmed" and rec.resolution["how"] == "manual" + resumed = [e for e in await _events(run.id) if e.type == "run.resumed"] + assert resumed[-1].data["from"] == ["pay"] # 写入节点从头重新执行:查到已生效,交回回执 + + +async def test_upgrade_a_plain_tool_node_after_approval_is_still_refused(monkeypatch): + """豁免只给写入节点:普通的调用工具节点批过、执行过(tool.start 不带 action_key)之后失败,升级后照旧拒绝接着跑。""" + state = {"fail": True} + + class _Args(BaseModel): + label: str + + from app.tools.registry import register + + name = "tally_then_maybe_fail" + + @register(name, "测试用:记一笔,第一次之后失败", "test", _Args, dangerous=True, needs_context=False) + async def _tally_fail(label: str) -> str: + CALLS.append({"label": label}) + if state["fail"]: + raise RuntimeError("下游断了") + return "ok" + + graph = chain( + node("start", "input", fields=[{"name": "question"}]), + node("t", "tool", tool=name, args={"label": "一笔"}, approval="always"), + node("out", "output", fields=[]), + ) + run = await run_manager.start(graph=graph, input_payload={"question": "x"}) + await _paused(run.id) + await run_manager.resume(run.id, {"approved": True}) + assert (await _wait(run.id, ("failed", "succeeded"))).status == "failed" + await run_manager.wait_idle(run.id) + assert len(CALLS) == 1 + _to_protocol_3(monkeypatch) + state["fail"] = False + with pytest.raises(ValueError, match="重复执行"): + await run_manager.continue_failed(run.id) + assert len(CALLS) == 1 + + +async def test_upgrade_human_inside_subgraph_is_flagged_at_publish(): + from app.db.models import Workflow, WorkflowVersion + from app.engine.governance import lint_for_publish + from app.engine.lint_process import load_subgraphs, with_subgraphs + from app.engine.schema import GraphSpec + + inner = chain(node("start", "input"), node("h", "human", mode="approve"), node("out", "output")) + async with SessionLocal() as session: + wf = Workflow(name="子流程", graph=inner) + session.add(wf) + await session.flush() + session.add(WorkflowVersion(workflow_id=wf.id, version=1, graph=inner)) + await session.commit() + outer = GraphSpec.model_validate(chain( + node("start", "input"), node("sub", "subgraph", workflow_id=wf.id, workflow_version=1), + node("out", "output"))) + graphs = await load_subgraphs(session, outer) + with with_subgraphs(graphs): + codes = {i.code for i in lint_for_publish(outer, level="published").issues} + assert "process.human_in_subgraph" in codes diff --git a/backend/tests/test_runs_api.py b/backend/tests/test_runs_api.py index 52dbd22f..6dbce582 100644 --- a/backend/tests/test_runs_api.py +++ b/backend/tests/test_runs_api.py @@ -152,6 +152,21 @@ async def test_run_class_and_name_filters(client, seeded_runs): assert (await client.get("/api/runs", params={"q": "列表%周报"})).json() == [] +async def test_origin_filter_takes_a_comma_list_and_none_for_old_runs(client, seeded_runs): + """记录页的来源筛选(X3):以前服务端不认这个参数,筛了也返回全部。none 是来源未记录的老运行。""" + wf_id, ids, _ = seeded_runs + async with SessionLocal() as session: + for run_id, kind in zip(ids, ["chat", "canvas", "schedule"]): + (await session.get(Run, run_id)).origin_kind = kind + await session.commit() + got = (await client.get("/api/runs", params={"workflow_id": wf_id, "origin_kind": "chat,schedule"})).json() + assert {x["id"] for x in got} == {ids[0], ids[2]} + old = (await client.get("/api/runs", params={"workflow_id": wf_id, "origin_kind": "none"})).json() + assert {x["id"] for x in old} == set(ids[3:]) + both = (await client.get("/api/runs", params={"workflow_id": wf_id, "origin_kind": "canvas,none"})).json() + assert {x["id"] for x in both} == {ids[1], *ids[3:]} + + 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() @@ -300,6 +315,30 @@ async def test_a_sealed_formal_run_needs_force(client): assert (await client.get(f"/api/runs/{run_id}")).status_code == 404 +@pytest.mark.parametrize("state", ["sent", "unknown", "confirmed", "failed"]) +async def test_a_run_that_wrote_to_an_external_system_cannot_be_deleted(client, state): + """生效记录随运行级联删除(P8):写过外部系统的运行不能删,强制删除也不行。演练不算生效,照常能删。""" + from app.db.models import ActionRecord + + run_id = await _plain_run(status="interrupted" if state in ("sent", "unknown") else "succeeded", + run_class="formal", manifest_hash="ab" * 32, manifest_seq=3) + async with SessionLocal() as session: + session.add(ActionRecord(action_key=uuid.uuid4().hex, idem_key=uuid.uuid4().hex, run_id=run_id, + node_id="pay", capability="erp_pay", payload_hash="h", state=state)) + await session.commit() + for params in ({}, {"force": "true"}): + r = await client.delete(f"/api/runs/{run_id}", params=params) + assert r.status_code == 409 and r.json()["code"] == "run_has_effects", r.text + assert (await client.get(f"/api/runs/{run_id}")).status_code == 200 + + rehearsal = await _plain_run(status="succeeded", run_class="exploratory") + async with SessionLocal() as session: + session.add(ActionRecord(action_key=uuid.uuid4().hex, idem_key=uuid.uuid4().hex, run_id=rehearsal, + node_id="pay", capability="erp_pay", payload_hash="h", state="simulated")) + await session.commit() + assert (await client.delete(f"/api/runs/{rehearsal}")).status_code == 204 + + 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 diff --git a/backend/tests/test_runs_origin_f2.py b/backend/tests/test_runs_origin_f2.py new file mode 100644 index 00000000..fa6dc52c --- /dev/null +++ b/backend/tests/test_runs_origin_f2.py @@ -0,0 +1,118 @@ +"""运行来源(X3)和 F2 回归。 + +- POST /api/runs 只接受客户端能声明的来源(chat / canvas / launcher / api),不传按 api 记;app_*、hook、schedule + 一律拒绝——否则谁都能把一次画布试跑说成应用发起的。来源记在运行上,run.origin 事件在 run.started 之前、封存范围内。 +- F2:正式运行带图接着跑一律 409(formal_graph_locked):它钉的是发布的不可变版本,换一张图运行就不再等于那一版。 + 接着跑也不再改发起人,接着跑的人记在 run.resumed 里。 +""" +from __future__ import annotations + +import asyncio + +import pytest +from httpx import ASGITransport, AsyncClient +from sqlalchemy import select + +from app.db.base import SessionLocal +from app.db.models import Run, RunEvent +from app.engine.runner import run_manager + + +@pytest.fixture(autouse=True) +async def engine_up(): + await run_manager.setup() + yield + await run_manager.shutdown() + + +@pytest.fixture +async def client(): + from app.main import app + + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: + yield c + + +def node(nid, ntype, **config): + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +def graph(schema: dict | None = None) -> dict: + nodes = [node("start", "input", fields=[{"name": "q"}]), + node("check", "validate", source="{{ input.q }}", schema=schema or {"type": "string"}, + repair_with_llm=False, max_retries=0), + node("out", "output", fields=[])] + return {"nodes": nodes, "edges": [{"source": "start", "target": "check"}, {"source": "check", "target": "out"}]} + + +async def _wait(run_id: str, statuses: tuple[str, ...], timeout: float = 20.0) -> Run: + deadline = asyncio.get_event_loop().time() + timeout + while asyncio.get_event_loop().time() < deadline: + async with SessionLocal() as session: + row = await session.get(Run, run_id) + if row and row.status in statuses and not run_manager.is_active(run_id): + return row + await asyncio.sleep(0.05) + raise AssertionError(f"运行 {run_id} 没有进入 {statuses}") + + +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()) + + +@pytest.mark.parametrize("origin", ["app_entry", "app_view", "app_backfill", "hook", "schedule", "nonsense"]) +async def test_client_cannot_claim_server_only_origins(client, origin): + resp = await client.post("/api/runs", json={"graph": graph(), "input": {"q": "x"}, "origin": origin}) + assert resp.status_code == 422 and resp.json()["code"] == "origin_not_allowed" + + +async def test_declared_origin_is_recorded_before_run_started(client): + resp = await client.post("/api/runs", json={"graph": graph(), "input": {"q": "x"}, "origin": "canvas"}, + headers={"X-Actor": "ann"}) + assert resp.status_code == 201, resp.text + run_id = resp.json()["id"] + assert resp.json()["origin_kind"] == "canvas" + await _wait(run_id, ("succeeded", "failed")) + events = await _events(run_id) + kinds = [e.type for e in events] + assert kinds.index("run.origin") < kinds.index("run.started") + origin = next(e for e in events if e.type == "run.origin").data + assert origin["kind"] == "canvas" and origin["actor"] == "ann" + + +async def test_no_origin_is_recorded_as_api(client): + resp = await client.post("/api/runs", json={"graph": graph(), "input": {"q": "x"}}) + assert resp.status_code == 201 and resp.json()["origin_kind"] == "api" + + +async def test_formal_run_cannot_continue_with_a_graph_and_keeps_its_initiator(client): + run = await run_manager.start(graph=graph({"type": "object", "required": ["a"]}), input_payload={"q": '"x"'}, + run_class="formal", started_by="alice") + assert (await _wait(run.id, ("failed", "succeeded"))).status == "failed" + + resp = await client.post(f"/api/runs/{run.id}/continue", json={"graph": graph()}, headers={"X-Actor": "bob"}) + assert resp.status_code == 409 and resp.json()["code"] == "formal_graph_locked" + async with SessionLocal() as session: + row = await session.get(Run, run.id) + assert row.graph == graph({"type": "object", "required": ["a"]}) + + resp = await client.post(f"/api/runs/{run.id}/continue", json={}, headers={"X-Actor": "bob"}) + assert resp.status_code == 200, resp.text + await _wait(run.id, ("failed", "succeeded")) + async with SessionLocal() as session: + row = await session.get(Run, run.id) + assert row.started_by == "alice" + resumed = [e for e in await _events(run.id) if e.type == "run.resumed"] + assert resumed[-1].data["actor"] == "bob" + + +async def test_exploratory_run_can_still_continue_with_a_revised_graph(client): + run = await run_manager.start(graph=graph({"type": "object", "required": ["a"]}), input_payload={"q": '"x"'}, + started_by="alice") + assert (await _wait(run.id, ("failed", "succeeded"))).status == "failed" + resp = await client.post(f"/api/runs/{run.id}/continue", json={"graph": graph()}, headers={"X-Actor": "bob"}) + assert resp.status_code == 200, resp.text + final = await _wait(run.id, ("failed", "succeeded")) + assert final.status == "succeeded" and final.started_by == "alice" diff --git a/backend/tests/test_tool_deadline.py b/backend/tests/test_tool_deadline.py index c1713067..78774107 100644 --- a/backend/tests/test_tool_deadline.py +++ b/backend/tests/test_tool_deadline.py @@ -66,7 +66,7 @@ def _slow_tool(name: str = "slow_lookup") -> StructuredTool: def _serve(monkeypatch, tool: StructuredTool) -> None: """让各节点按名字拿到这个工具。""" - async def _build(names, ctx, *, session=None): + async def _build(names, ctx, *, session=None, allow_write=False): return [tool] if tool.name in names else [] import app.engine.nodes.llm as llm_nodes diff --git a/scripts/copy-allow.json b/scripts/copy-allow.json index 964b664f..34a09347 100644 --- a/scripts/copy-allow.json +++ b/scripts/copy-allow.json @@ -44,10 +44,10 @@ "reason": "按用户数据源描述里的业务词(采购、供应商)造的示例问法,这里的「供应商」是采购供应商,不是模型接入" }, { - "file": "backend/app/scheduler/webhook.py", + "file": "backend/app/notify/core.py", "text": "钉钉", "rule": "钉", - "reason": "「钉钉」是 Webhook 通知的平台名(定时任务的消息格式:通用 / 飞书 / 企业微信 / 钉钉),用在格式不可选、格式和群机器人地址对不上的报错里,不是「钉住」的意思" + "reason": "「钉钉」是 Webhook 通知的平台名(定时任务和通知渠道的消息格式:通用 / 飞书 / 企业微信 / 钉钉),用在格式不可选、格式和群机器人地址对不上的报错里,不是「钉住」的意思。格式、签名、发送从 scheduler/webhook.py 抽到了 notify/core.py" }, { "file": "backend/app/seed.py", diff --git a/scripts/fake-erp.py b/scripts/fake-erp.py new file mode 100644 index 00000000..d4391652 --- /dev/null +++ b/scripts/fake-erp.py @@ -0,0 +1,44 @@ +"""起一个假的外部系统(付款),供浏览器验收「审批流程应用 + 写入外部系统」(设计文档 §7.2)。 + + ~/miniforge3/envs/agentlab/bin/python scripts/fake-erp.py [--port 8765] [--mode ok] + +接口(路径以环境开头,test 和 prod 各记各的账): + POST /{test|prod}/payments 认 Idempotency-Key:同一个键只记一笔 + GET /{test|prod}/payments/lookup?idempotency_key=… 查到 200,查不到 404 + POST /_fake/mode {"mode": "ok" | "timeout_after_write" | "error_500" | "error_4xx"} 切换注入的情况 + GET /_fake/state 收到的写请求、查询、记下的付款 + POST /_fake/reset 清空 + +在 AgentLab 里登记成一个端点:test 环境 base_url = http://127.0.0.1:端口/test,prod 环境 = …/prod,开内网访问 +(只有管理员能开)。付款能力绑定这个端点、POST /payments,治理成写入、查询能力填下面这个;查询能力 GET /payments/lookup, +参数 idempotency_key。实现在 backend/tests/fakes/fake_erp.py(测试里不起服务,直接用它的替身)。 +""" +from __future__ import annotations + +import argparse +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent / "backend" + + +def main() -> None: + parser = argparse.ArgumentParser(description="假的外部系统(付款),只供验收") + parser.add_argument("--host", default="127.0.0.1") + parser.add_argument("--port", type=int, default=8765) + parser.add_argument("--mode", default="ok", choices=("ok", "timeout_after_write", "error_500", "error_4xx")) + parser.add_argument("--hang", type=float, default=30.0, help="timeout_after_write 时挂多久(秒)") + args = parser.parse_args() + + sys.path.insert(0, str(ROOT)) + import uvicorn + + from tests.fakes.fake_erp import FakeErp, app + + erp = FakeErp(mode=args.mode, hang_s=args.hang) + print(f"假外部系统:http://{args.host}:{args.port}/{{test|prod}}/payments,注入:{args.mode}", flush=True) + uvicorn.run(app(erp), host=args.host, port=args.port, log_level="warning") + + +if __name__ == "__main__": + main()