diff --git a/.env.example b/.env.example index 452b0d52..cede4581 100644 --- a/.env.example +++ b/.env.example @@ -36,6 +36,13 @@ # ---- HTTP 工具 ---- # 逗号分隔的域名白名单;留空表示只拦内网地址,不限制公网域名 # AGENTLAB_HTTP_TOOL_ALLOWLIST=api.github.com,news.ycombinator.com +# 本机代理(Clash / mihomo 等)开了 TUN + fake-ip 时,查任何域名都先拿到这个段的假地址, +# 网页工具会把它当内网拦掉。填上之后,落在段里的解析结果改向公共 DNS 核实真实地址: +# 公网放行,内网照拦,核实不了也拦。没开 fake-ip 就留空 +# AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES=198.18.0.0/15 +# 核实用的 DNS-over-HTTPS(JSON 接口),逗号分隔,按顺序试;写 IP 形式免得解析它自己的域名。 +# 境外的放前面:境内公共 DNS 对被封锁的域名返回污染结果,会被当成内网误拦 +# AGENTLAB_HTTP_TOOL_DOH_URLS=https://1.1.1.1/dns-query,https://223.5.5.5/resolve # ---- 落库加密密钥 ---- # 留空会自动生成并存到 data/.secret_key。换了这个值,之前存的 API Key 会解不开。 diff --git a/README.md b/README.md index bda256ad..64872dc1 100644 --- a/README.md +++ b/README.md @@ -358,6 +358,8 @@ Excel / CSV 若传入知识库,只能被切块检索,数字即成为模型 - 分支条件使用 AST 白名单求值器而非 `eval` - HTTP 工具解析域名后逐 IP 检查,拦截内网、回环与云元数据地址,且不跟随重定向(逐跳重新校验) + 本机代理开 fake-ip 时,解析结果是 198.18 段的假地址:配置 fake-ip 网段后,这类结果改向公共 DNS(DoH) + 核实真实地址再判断,核实不了按拦截处理 - 文件工具路径 `resolve()` 后必须仍位于工作区内 - SQL 守卫采用白名单动词判定 + 正文扫描 + 多语句拦截 + 结果集上限;`DROP` / `TRUNCATE` / `GRANT` 等 在可写数据源上亦一律拒绝。以查询开头却会写的语句(`WITH … DELETE`、`SELECT … INTO`、 @@ -452,6 +454,12 @@ pip install -e 'backend[microvm,dev,db,docs]' -c backend/requirements.lock # 需先处理超时或更换 schema。数据源卡片会显示失败原因与该服务器上的可选库列表。 agent 侧同样会收到实情,并被告知可直接查询 `information_schema` 自行确认表结构。 +**网页工具报「解析到内网地址 198.18.x.x,已拦截」。** 本机代理(Clash / mihomo 等)开了 TUN + fake-ip, +查任何域名都先拿到 198.18 段的假地址,被当成内网拦掉了。在 `.env` 里加一行 +`AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES=198.18.0.0/15`,重启后端即可:落在这个段里的解析结果会改向公共 DNS +核实真实地址,公网放行、内网照拦。核实用的 DoH 地址由 `AGENTLAB_HTTP_TOOL_DOH_URLS` 指定(逗号分隔)。 +不想改配置的话,把代理 DNS 改成 redir-host,或把要访问的域名加进 `fake-ip-filter`。 + **端口冲突。** 前端默认 5273、后端 8000,通过 `AGENTLAB_WEB_PORT` / `AGENTLAB_PORT` 修改。 **改了后端代码,正在跑的运行显示中断。** `dev.sh` 开着热重载,改一个后端文件就是一次 diff --git a/backend/app/api/coded.py b/backend/app/api/coded.py new file mode 100644 index 00000000..bf9a53aa --- /dev/null +++ b/backend/app/api/coded.py @@ -0,0 +1,27 @@ +"""带机器码的 HTTP 错误。 + +detail 仍是给人看的那一句,老前端照旧直接显示;code 是给程序认的稳定标识。 +以前前端只能按 detail 的开头几个字认出某一类错误,文案一改就悄悄失灵。 +""" +from __future__ import annotations + +from fastapi import HTTPException, Request +from fastapi.responses import JSONResponse + +#: 限定的数据源一个都不在了(删了或停用了),见 copilot._sources +DATASOURCE_SCOPE_EMPTY = "datasource_scope_empty" + + +class CodedHTTPException(HTTPException): + def __init__(self, status_code: int, detail: str, code: str) -> None: + super().__init__(status_code, detail) + self.code = code + + +async def coded_handler(_: Request, exc: Exception) -> JSONResponse: + assert isinstance(exc, CodedHTTPException) + return JSONResponse( + status_code=exc.status_code, + content={"detail": exc.detail, "code": exc.code}, + headers=exc.headers, + ) diff --git a/backend/app/api/conversations.py b/backend/app/api/conversations.py index 4111e813..a92381b5 100644 --- a/backend/app/api/conversations.py +++ b/backend/app/api/conversations.py @@ -91,6 +91,8 @@ class TurnOut(BaseModel): #: 可信度元数据。老轮次挂在 review.meta 下,读出来时统一放到这里 meta: dict[str, Any] | None = None created_at: Any = None + #: 最后一次落库。列表判「建图断了」按它算(BUILD_STALE),前端用同一只钟 + updated_at: Any = None model_config = {"from_attributes": True} @@ -132,6 +134,15 @@ def turn_meta(turn: Any) -> dict[str, Any] | None: return bridged if isinstance(bridged, dict) else None +def turn_run_id(turn: Any) -> str | None: + """这一轮现在的运行。meta 里写了 runId 就以它为准,哪怕是 null:重试会换一次运行, + run_id 列却清不掉(回填里 None 表示「不改」),还指着上一次的。老轮次没有这个键,看列。""" + meta = turn_meta(turn) or {} + if "runId" in meta: + return meta["runId"] or None + return turn.run_id or None + + def _turn_out(turn: ConversationTurn) -> TurnOut: out = TurnOut.model_validate(turn) out.meta = turn_meta(turn) @@ -212,9 +223,7 @@ async def _statuses( session: AsyncSession, turns: dict[str, ConversationTurn] ) -> dict[str, tuple[str, str | None]]: """会话 id → (last_status, last_run_id)。运行状态和待审批各一次查询。""" - run_of = { - cid: t.run_id or (turn_meta(t) or {}).get("runId") or None for cid, t in turns.items() - } + run_of = {cid: turn_run_id(t) for cid, t in turns.items()} run_ids = {r for r in run_of.values() if r} runs: dict[str, str] = {} waiting: set[str] = set() diff --git a/backend/app/api/copilot.py b/backend/app/api/copilot.py index 52489510..f7e97c0b 100644 --- a/backend/app/api/copilot.py +++ b/backend/app/api/copilot.py @@ -12,6 +12,7 @@ from pydantic import BaseModel, Field from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession +from app.api.coded import DATASOURCE_SCOPE_EMPTY, CodedHTTPException # 起别名:本文件底下有个叫 explain 的接口,generate 里又有个局部变量叫 raw,同名会互相盖掉 from app.core.errors import explain as explain_error, graph_error, not_configured, raw as raw_error from app.db.base import get_session @@ -43,9 +44,10 @@ async def _sources(session: AsyncSession, scope: list[str] | None) -> list[Any]: return rows picked = [r for r in rows if r.id in wanted or r.name in wanted] if not picked: - raise HTTPException( + raise CodedHTTPException( 400, "限定的数据源都不在了:可能已经被删掉或停用。去掉限定再问," "或者到「数据」页确认它还在、而且是启用的", + DATASOURCE_SCOPE_EMPTY, ) return picked @@ -157,7 +159,7 @@ NODE_REFERENCE = """\ **不要写 scope**。记忆域由平台给(当前统一是 default)。自己起一个域的后果是 写进去再也读不回来——真发生过:一轮写进 scope"user",下一轮没写 scope 落回 default,于是「我叫张三」存下来了,问「我是谁」却答不上来 -- human:人工介入。config: {mode: approve|input|edit, title, message} +- human:人工审批。config: {mode: approve|input|edit, title, message} - validate:JSON Schema 校验,可自动让模型修复。config: {schema, source, max_retries} - transform:数据整形。config: {mode: expression|template|json, expression/template, assign_to} - subgraph:嵌套另一个工作流。config: {workflow_id, input} @@ -1390,7 +1392,7 @@ async def extract_template( "先让它跑成功一次,再从那次运行提取", ) if not run.graph.get("nodes"): - raise HTTPException(400, "这次运行没有保存图快照,无法提取") + raise HTTPException(400, "这次运行没有保存工作流快照,无法提取") events = list( ( diff --git a/backend/app/api/runs.py b/backend/app/api/runs.py index a24c31c0..e2212e11 100644 --- a/backend/app/api/runs.py +++ b/backend/app/api/runs.py @@ -83,7 +83,8 @@ async def start_run( x_actor: str | None = Header(default=None), ) -> Run: actor = actor_of(x_actor) - name = "临时图" + # 没挂在工作流上(画布没保存就跑、问数据当场搭的)。老数据里是「临时图」,前端两种都认 + name = "未保存的工作流" version: int | None = None version_hash: str | None = None @@ -93,7 +94,7 @@ async def start_run( if not payload.workflow_id: raise HTTPException(400, "正式运行要从一个已保存的工作流发起:请求里没有指定工作流") if payload.graph is not None: - raise HTTPException(400, "正式运行只跑已发布的版本,不接受画布上的临时图——先保存并发布") + raise HTTPException(400, "正式运行只跑已发布的版本,不接受画布上没发布的改动——先保存并发布") workflow = await session.get(Workflow, payload.workflow_id) if not workflow: raise HTTPException(404, "找不到这个工作流,可能已经被删除了") @@ -141,7 +142,7 @@ async def start_run( graph = graph or workflow.graph name = workflow.name if not graph: - raise HTTPException(400, "没有要运行的内容:请求里既没有工作流,也没有图") + raise HTTPException(400, "没有要运行的内容:请求里既没有工作流 id,也没有工作流内容") from app.api.settings import resolve_run_scope @@ -453,7 +454,7 @@ async def delete_run( if not run: raise HTTPException(404, "运行记录不存在") if run.status in ("queued", "running") or run_manager.is_active(run_id): - raise HTTPException(409, "这次运行还在执行,现在删除会留下没人管的任务。先停止它,再删除") + raise HTTPException(409, "这次运行还在进行,现在删除会留下没人管的任务。先停止它,再删除") if run.run_class == "formal" and run.manifest_hash and not force: raise HTTPException( 409, diff --git a/backend/app/api/workflows.py b/backend/app/api/workflows.py index 7d95bd36..ef8f6720 100644 --- a/backend/app/api/workflows.py +++ b/backend/app/api/workflows.py @@ -152,7 +152,7 @@ async def delete_workflow( ) ) if live: - raise HTTPException(409, f"这个工作流还有 {live} 次运行在执行,现在删除它们会失去归属。先停止运行,再删除") + raise HTTPException(409, f"这个工作流还有 {live} 次运行在进行,现在删除它们会失去归属。先停止运行,再删除") await session.delete(workflow) await session.commit() diff --git a/backend/app/core/config.py b/backend/app/core/config.py index f0af072f..49c9e953 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -1,9 +1,12 @@ from __future__ import annotations +import ipaddress from functools import lru_cache from pathlib import Path +from typing import Annotated -from pydantic_settings import BaseSettings, SettingsConfigDict +from pydantic import field_validator +from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict BACKEND_DIR = Path(__file__).resolve().parents[2] PROJECT_DIR = BACKEND_DIR.parent @@ -83,7 +86,38 @@ class Settings(BaseSettings): http_tool_timeout: int = 20 http_tool_max_bytes: int = 2_000_000 # 逗号分隔的域名白名单;为空表示不限制(仅拦截内网地址) - http_tool_allowlist: list[str] = [] + http_tool_allowlist: Annotated[list[str], NoDecode] = [] + # 本机代理 fake-ip 模式给的假地址段,逗号分隔(Clash / mihomo 默认 198.18.0.0/15)。 + # 为空表示不认:解析到这些地址一律按内网拦截。配了之后,落在段里的解析结果改向 + # 下面的公共 DNS 核实真实地址——不能直接放行,内网域名在 fake-ip 下也是这个段 + http_tool_fake_ip_ranges: Annotated[list[str], NoDecode] = [] + # 核实用的 DNS-over-HTTPS(JSON 接口),按顺序试。写 IP 形式免得解析 DoH 自己的域名。 + # 境外的排前面:境内公共 DNS 对被封锁的域名返回污染结果(维基百科的 AAAA 给 2001::1), + # 会被当成内网误拦;开 fake-ip 的机器本来就有代理,连得上境外的 DoH + http_tool_doh_urls: Annotated[list[str], NoDecode] = [ + "https://1.1.1.1/dns-query", + "https://223.5.5.5/resolve", + ] + + @field_validator("http_tool_allowlist", "http_tool_fake_ip_ranges", "http_tool_doh_urls", mode="before") + @classmethod + def _comma_list(cls, value: object) -> object: + # 环境变量里写逗号分隔;也兼容 JSON 数组的写法 + if isinstance(value, str): + text = value.strip() + if text.startswith("["): + import json + + return json.loads(text) + return [part.strip() for part in text.split(",") if part.strip()] + return value + + @field_validator("http_tool_fake_ip_ranges") + @classmethod + def _valid_ranges(cls, value: list[str]) -> list[str]: + for cidr in value: + ipaddress.ip_network(cidr, strict=False) # 写错就在启动时报出来,别到运行时才静默失效 + return value @property def db_path(self) -> Path: diff --git a/backend/app/data/engine.py b/backend/app/data/engine.py index cb986582..d668ead0 100644 --- a/backend/app/data/engine.py +++ b/backend/app/data/engine.py @@ -292,7 +292,11 @@ def _causes(e: BaseException) -> list[BaseException]: class _Deadline: - """一条语句的时限。数据库那边按它停下;停下时报的错据此认成超时。""" + """一条语句的时限。数据库那边按它停下;停下时报的错据此认成超时。 + + 从取连接之前起算,和后端兜底同一个起点:取连接花掉的也算在时限里,数据库只拿到 + 剩下的那段(remaining_ms)。否则取连接一慢,兜底就抢在数据库前面取消语句。 + """ def __init__(self, seconds: float) -> None: self.seconds = seconds @@ -302,9 +306,11 @@ class _Deadline: #: 语句已经结束。回调这时要是还挂在连接上(摘除没成功),也不能再掐别人的语句 self.over = False - @property - def ms(self) -> int: - return max(1, int(self.seconds * 1000)) + def remaining_ms(self) -> int: + return max(1, int((self.ends - time.monotonic()) * 1000)) + + def remaining_seconds(self) -> float: + return max(0.001, round(self.ends - time.monotonic(), 3)) def expired(self) -> bool: return time.monotonic() >= self.ends @@ -354,7 +360,7 @@ async def _server_deadline(conn: AsyncConnection, kind: str, deadline: _Deadline """ if kind in ("postgres", "postgresql"): # SET LOCAL 只管这一个事务:连接还回池子时回滚,设置随之作废 - await conn.execute(text(f"SET LOCAL statement_timeout = {deadline.ms}")) + await conn.execute(text(f"SET LOCAL statement_timeout = {deadline.remaining_ms()}")) yield return @@ -362,9 +368,9 @@ async def _server_deadline(conn: AsyncConnection, kind: str, deadline: _Deadline # MySQL 的 max_execution_time 以毫秒计、只管 SELECT;MariaDB 没有它, # 用 max_statement_time(秒)。按连上的服务器认,不按表单里选的类型 if getattr(conn.dialect, "is_mariadb", False): - var, value = "max_statement_time", _seconds(deadline.seconds) + var, value = "max_statement_time", _seconds(deadline.remaining_seconds()) else: - var, value = "max_execution_time", str(deadline.ms) + var, value = "max_execution_time", str(deadline.remaining_ms()) try: await conn.execute(text(f"SET SESSION {var} = {value}")) except Exception as e: # noqa: BLE001 @@ -390,7 +396,7 @@ async def _server_deadline(conn: AsyncConnection, kind: str, deadline: _Deadline driver = await _driver(conn) before = getattr(driver, "call_timeout", 0) try: - driver.call_timeout = deadline.ms + driver.call_timeout = deadline.remaining_ms() except Exception as e: # noqa: BLE001 logger.warning("Oracle call_timeout 没设上(%s),只剩后端兜底", e) yield @@ -427,13 +433,17 @@ async def _bounded( """在 opener() 开的连接上跑 work,时限交给数据库执行,后端这边再留一道兜底。 数据库按时限停下语句时报的错,翻成 over 这句话:「超过 Ns 被中断」。 + 两道时限从同一刻(取连接之前)算起,数据库那道先到,兜底晚 BACKSTOP_S。 """ deadline: _Deadline | None = None async def _exec() -> Any: nonlocal deadline + deadline = _Deadline(limits.timeout_seconds) async with opener() as conn: - deadline = _Deadline(limits.timeout_seconds) + # 光等连接就把时限用完了:语句不必再发,发出去也只剩 1ms + if deadline.expired(): + raise SqlRejected(over) async with _server_deadline(conn, kind, deadline): return await work(conn) diff --git a/backend/app/data/introspect.py b/backend/app/data/introspect.py index 8d1a9fbd..65be8813 100644 --- a/backend/app/data/introspect.py +++ b/backend/app/data/introspect.py @@ -20,6 +20,7 @@ from typing import Any from sqlalchemy import inspect as sa_inspect from sqlalchemy.engine.reflection import ObjectKind +from app.core.errors import describe_exception, first_line from app.data.engine import engines # 一次探查最多带回多少张表。生产库动辄上千张,全拉回来既慢又没人看得完。 @@ -198,8 +199,9 @@ async def introspect(source: Any, *, schema: str | None = None) -> dict[str, Any candidates = await list_schemas(source) if not candidates: raise + # error 原样画在数据源卡片上、也交给 agent 看:驱动的异常类名和文档链接去掉,只留它自己的说明 payload = {"tables": {}, "truncated": False, "total": 0, - "failed": True, "error": f"{type(exc).__name__}: {exc}"} + "failed": True, "error": first_line(exc) or describe_exception(exc)} payload["synced_at"] = datetime.now(timezone.utc).isoformat() payload["schema"] = target diff --git a/backend/app/data/tabular.py b/backend/app/data/tabular.py index ae68262f..1c539f39 100644 --- a/backend/app/data/tabular.py +++ b/backend/app/data/tabular.py @@ -22,9 +22,12 @@ import csv import io import re import sqlite3 +import zipfile from dataclasses import dataclass, field from typing import Any +from app.core.errors import describe_exception + #: 扫多少行来推断列类型。全表扫一遍对几十万行的文件太慢,而类型这种东西 #: 前几千行看不出来的,后面多半也看不出来——真看错了,SQLite 的动态类型 #: 也还能把值原样存下去,不会丢数据 @@ -246,7 +249,10 @@ def _read_excel(raw: bytes, *, header_row: int) -> list[tuple[str, list[list[Any # data_only 取公式算出来的值而不是公式本身——用户要的是数 book = load_workbook(io.BytesIO(raw), read_only=True, data_only=True) except Exception as e: # noqa: BLE001 - raise UnsupportedTable(f"这个 Excel 读不开:{type(e).__name__}: {e}") from e + # 这句话原样进上传的 400:异常类名留在日志(from e),界面上只说能照着做的 + reason = "文件不是有效的 .xlsx(可能损坏,或者只是改了扩展名)" if isinstance(e, zipfile.BadZipFile) \ + else describe_exception(e) + raise UnsupportedTable(f"这个 Excel 读不开:{reason}。在 Excel 里另存为 .xlsx 或 .csv 再传") from e out: list[tuple[str, list[list[Any]]]] = [] for sheet in book.worksheets: diff --git a/backend/app/engine/compiler.py b/backend/app/engine/compiler.py index e7de975f..46ae78e4 100644 --- a/backend/app/engine/compiler.py +++ b/backend/app/engine/compiler.py @@ -319,7 +319,7 @@ def compile_graph(spec: GraphSpec, run_ctx: RunContext) -> StateGraph: # 入口 entries = spec.entry_nodes() if not entries: - raise ValueError("这张图没有入口节点") + raise ValueError("这个工作流没有入口节点") for node in entries: builder.add_edge(START, node.id) diff --git a/backend/app/engine/schema.py b/backend/app/engine/schema.py index 70693d51..2a03cae4 100644 --- a/backend/app/engine/schema.py +++ b/backend/app/engine/schema.py @@ -384,7 +384,7 @@ def validate_graph(spec: GraphSpec) -> ValidationResult: result.add(f"边指向了不存在的目标节点 {edge.target!r}", edge_id=edge.id) if not spec.entry_nodes(): - result.add("找不到入口:每个节点都有入边,图里存在环且没有起点") + result.add("找不到入口:每个节点都有入边,工作流里有环且没有起点") # 节点级必填项 for node in spec.nodes: diff --git a/backend/app/main.py b/backend/app/main.py index 5fe5b524..16a5f458 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -12,6 +12,7 @@ from app.api import ( artifacts, conversations, copilot, datasources, governance, knowledge, runs, sandbox, settings as settings_api, tools, workflows, ) +from app.api.coded import CodedHTTPException, coded_handler from app.core.config import settings from app.db.base import init_db from app.engine.runner import run_manager @@ -107,6 +108,9 @@ for router in ( app.include_router(router) +app.add_exception_handler(CodedHTTPException, coded_handler) + + @app.exception_handler(ValueError) async def value_error_handler(_: Request, exc: ValueError) -> JSONResponse: return JSONResponse(status_code=400, content={"detail": str(exc)}) diff --git a/backend/app/sandbox/bubblewrap_sandbox.py b/backend/app/sandbox/bubblewrap_sandbox.py index 81bd2573..c2341443 100644 --- a/backend/app/sandbox/bubblewrap_sandbox.py +++ b/backend/app/sandbox/bubblewrap_sandbox.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import logging import os import resource import shutil @@ -9,9 +10,12 @@ import time from pathlib import Path from app.core.config import settings, sanitize_session +from app.core.errors import describe_exception, raw from app.sandbox import interpreter from app.sandbox.base import ENTRY_PREFIX, ExecResult, Sandbox, SandboxLimits, entry_name, truncate +logger = logging.getLogger(__name__) + # 入口脚本后缀 + 执行命令前缀(沙箱内路径在运行时拼)。文件名不能写死: # 同会话并发执行都写 main.py 会互相覆盖。 _LANG = { @@ -156,7 +160,9 @@ class BubblewrapSandbox(Sandbox): error=f"执行超过 {limits.timeout}s 被终止", ) except Exception as e: # noqa: BLE001 - return ExecResult(ok=False, backend=self.name, error=f"{type(e).__name__}: {e}") + # 起进程和收输出在同一个 try 里。首行只说原因,类名和 errno 这些留给日志 + logger.warning("%s 沙箱进程出错:%s", self.name, raw(e)) + return ExecResult(ok=False, backend=self.name, error=f"沙箱进程出错:{describe_exception(e)}") stdout, t1 = truncate(out.decode(errors="replace"), limits.max_output) stderr, t2 = truncate(err.decode(errors="replace"), limits.max_output) diff --git a/backend/app/sandbox/local_sandbox.py b/backend/app/sandbox/local_sandbox.py index d5e4e95a..ea98f911 100644 --- a/backend/app/sandbox/local_sandbox.py +++ b/backend/app/sandbox/local_sandbox.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import logging import os import resource import shutil @@ -9,9 +10,12 @@ import time from pathlib import Path from app.core.config import settings, sanitize_session +from app.core.errors import describe_exception, raw from app.sandbox import interpreter from app.sandbox.base import ENTRY_PREFIX, ExecResult, Sandbox, SandboxLimits, entry_name, truncate +logger = logging.getLogger(__name__) + # 入口脚本后缀 + 执行命令。文件名运行时生成(entry_name),不能写死: # 同会话并发执行都写 main.py 会互相覆盖。 _LANG = { @@ -131,7 +135,9 @@ class LocalSandbox(Sandbox): preexec_fn=self._preexec(limits), ) except Exception as e: # noqa: BLE001 - return ExecResult(ok=False, backend=self.name, error=f"{type(e).__name__}: {e}") + # 首行只说原因,类名和 errno 这些留给日志 + logger.warning("%s 沙箱起进程失败:%s", self.name, raw(e)) + return ExecResult(ok=False, backend=self.name, error=f"没能启动进程:{describe_exception(e)}") try: out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout) diff --git a/backend/app/sandbox/microvm_sandbox.py b/backend/app/sandbox/microvm_sandbox.py index b271c7a0..f66d71ef 100644 --- a/backend/app/sandbox/microvm_sandbox.py +++ b/backend/app/sandbox/microvm_sandbox.py @@ -1,12 +1,14 @@ from __future__ import annotations import asyncio +import logging import time from dataclasses import dataclass, field from pathlib import Path from typing import Any from app.core.config import sanitize_session, settings +from app.core.errors import describe_exception, raw from app.sandbox.base import ( ENTRY_PREFIX, ExecResult, @@ -16,6 +18,8 @@ from app.sandbox.base import ( truncate, ) +logger = logging.getLogger(__name__) + _WORKDIR = "/workspace" # 各语言的入口脚本后缀与执行命令。文件名在运行时生成(见 entry_name), @@ -279,10 +283,11 @@ class MicroVMSandbox(Sandbox): + ("" if self.image_ready() else "(镜像未缓存,首次需拉取约 55s)"), ) except Exception as e: # noqa: BLE001 + logger.warning("microVM 启动失败:%s", raw(e)) return ExecResult( ok=False, backend=self.name, duration_ms=int((time.perf_counter() - started) * 1000), - error=f"microVM 启动失败:{type(e).__name__}: {e}", + error=f"microVM 启动失败:{describe_exception(e)}", ) try: @@ -327,10 +332,11 @@ class MicroVMSandbox(Sandbox): + ("" if killed else ";VM 已重置,该会话之前写的文件已丢失"), ) except Exception as e: # noqa: BLE001 + logger.warning("microVM 执行中断:%s", raw(e)) return ExecResult( ok=False, backend=self.name, duration_ms=int((time.perf_counter() - started) * 1000), - error=f"{type(e).__name__}: {e}", + error=f"microVM 里的执行中断了:{describe_exception(e)}", ) finally: if ephemeral: diff --git a/backend/app/sandbox/seatbelt_sandbox.py b/backend/app/sandbox/seatbelt_sandbox.py index 459fe522..1c1de97b 100644 --- a/backend/app/sandbox/seatbelt_sandbox.py +++ b/backend/app/sandbox/seatbelt_sandbox.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import logging import os import resource import shutil @@ -9,9 +10,12 @@ import time from pathlib import Path from app.core.config import sanitize_session, settings +from app.core.errors import describe_exception, raw from app.sandbox import interpreter from app.sandbox.base import ENTRY_PREFIX, ExecResult, Sandbox, SandboxLimits, entry_name, truncate +logger = logging.getLogger(__name__) + SANDBOX_EXEC = "/usr/bin/sandbox-exec" def _real(path: str | Path) -> str: @@ -227,7 +231,9 @@ class SeatbeltSandbox(Sandbox): preexec_fn=self._preexec(limits), ) except Exception as e: # noqa: BLE001 - return ExecResult(ok=False, backend=self.name, error=f"{type(e).__name__}: {e}") + # 首行只说原因,类名和 errno 这些留给日志 + logger.warning("%s 沙箱起进程失败:%s", self.name, raw(e)) + return ExecResult(ok=False, backend=self.name, error=f"没能启动进程:{describe_exception(e)}") try: out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout) diff --git a/backend/app/seed.py b/backend/app/seed.py index d86a7bee..d4ff543a 100644 --- a/backend/app/seed.py +++ b/backend/app/seed.py @@ -74,7 +74,7 @@ TEMPLATES: list[dict[str, Any]] = [ { "name": "③ 带人工审批的发布流", "description": "起草 → 人工把关 → 通过就发布、驳回就改写。演示中断与断点恢复。", - "tags": ["人工介入", "分支"], + "tags": ["人工审批", "分支"], "nodes": [ _n("start", "input", "主题", fields=[{"name": "topic", "required": True}]), _n("draft", "llm", "起草", system="你是品牌文案。", diff --git a/backend/app/tools/net.py b/backend/app/tools/net.py index c04b26c4..f4bb729e 100644 --- a/backend/app/tools/net.py +++ b/backend/app/tools/net.py @@ -1,7 +1,8 @@ from __future__ import annotations +import asyncio import ipaddress -import socket +import time from urllib.parse import urlparse import httpx @@ -31,12 +32,79 @@ def _is_private(ip: str) -> bool: ) -def assert_safe_url(url: str) -> str: +# 本机代理 fake-ip 模式最常见的假地址段。没配 fake-ip 网段时,拦截报错里据此提示一句 +_COMMON_FAKE_IP = ipaddress.ip_network("198.18.0.0/15") +# 核实过的真实地址:host -> (过期时刻, 地址)。TTL 取 DNS 给的,夹在 30s–300s 之间 +_real_ip_cache: dict[str, tuple[float, tuple[str, ...]]] = {} +_DOH_TYPES = {"A": 1, "AAAA": 28} + + +def _fake_networks() -> list[ipaddress.IPv4Network | ipaddress.IPv6Network]: + return [ipaddress.ip_network(c, strict=False) for c in settings.http_tool_fake_ip_ranges] + + +def _in(ip: str, networks) -> bool: + try: + addr = ipaddress.ip_address(ip) + except ValueError: + return False + return any(addr.version == n.version and addr in n for n in networks) + + +def _doh_client() -> httpx.AsyncClient: + # 不走环境变量里的代理:核实要的就是公共 DNS 的原话 + return httpx.AsyncClient(timeout=5, trust_env=False) + + +async def _real_ips(host: str) -> tuple[str, ...]: + """向公共 DNS(DoH)问 host 的真实地址。问不到就拦——核实不了不能当作安全。""" + cached = _real_ip_cache.get(host) + if cached and cached[0] > time.monotonic(): + return cached[1] + + nxdomain = False + async with _doh_client() as client: + for base in settings.http_tool_doh_urls: + try: + replies = await asyncio.gather(*( + client.get(base, params={"name": host, "type": t}, + headers={"accept": "application/dns-json"}) + for t in _DOH_TYPES + )) + bodies = [r.json() for r in replies if r.status_code == 200] + except (httpx.HTTPError, ValueError): + continue + if len(bodies) != len(_DOH_TYPES): + continue + ips: list[str] = [] + ttls: list[int] = [] + for body in bodies: + for answer in body.get("Answer") or []: + if answer.get("type") in _DOH_TYPES.values() and answer.get("data"): + ips.append(str(answer["data"])) + ttls.append(int(answer.get("TTL") or 0)) + if ips: + ttl = min(max(min(ttls), 30), 300) + _real_ip_cache[host] = (time.monotonic() + ttl, tuple(ips)) + return tuple(ips) + nxdomain = nxdomain or all(b.get("Status") == 3 for b in bodies) + if nxdomain: + break + if nxdomain: + raise UnsafeUrlError(f"{host} 在公共 DNS 里查不到(本机只解析到代理的 fake-ip 假地址),已拦截") + raise UnsafeUrlError(f"{host} 解析到代理的 fake-ip 假地址,而且没能向公共 DNS 核实真实地址,已拦截") + + +async def assert_safe_url(url: str) -> str: """挡住 SSRF。 agent 手里的 HTTP 工具就是一把面向内网的扫描枪,所以这里做三件事: 只放行 http/https、解析域名后逐个 IP 检查是不是内网/回环/元数据地址、 再按配置里的域名白名单过一遍。 + + 本机代理开了 fake-ip 时,查什么都先拿到 198.18 段的假地址。配了 + http_tool_fake_ip_ranges 的话,落在段里的解析结果改向公共 DNS 核实真实地址 + 再判断;直接放行不行——内网域名在 fake-ip 下也是这个段。 """ parsed = urlparse(url) if parsed.scheme not in ("http", "https"): @@ -54,13 +122,35 @@ def assert_safe_url(url: str) -> str: # 逐个解析结果都要查:一个域名可能同时解析到公网和内网地址 try: - infos = socket.getaddrinfo(host, parsed.port or (443 if parsed.scheme == "https" else 80)) - except socket.gaierror as e: + infos = await asyncio.get_running_loop().getaddrinfo( + host, parsed.port or (443 if parsed.scheme == "https" else 80)) + except OSError as e: raise UnsafeUrlError(f"域名解析失败:{host}") from e + + fake = _fake_networks() + try: + ipaddress.ip_address(host) + is_literal = True # 直接写的 IP 没有域名可以去核实 + except ValueError: + is_literal = False + needs_check = False for info in infos: ip = info[4][0] + if fake and _in(ip, fake) and not is_literal: + needs_check = True # 假地址本身说明不了什么,下面问公共 DNS + continue if _is_private(ip): - raise UnsafeUrlError(f"{host} 解析到内网地址 {ip},已拦截") + hint = "" + if not fake and _in(ip, [_COMMON_FAKE_IP]): + hint = ("。198.18.x.x 通常是本机代理 fake-ip 模式给的假地址:可以设置 " + "AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES=198.18.0.0/15 让它向公共 DNS 核实真实地址," + "或者把代理的 DNS 改成 redir-host、把这个域名加进 fake-ip-filter") + raise UnsafeUrlError(f"{host} 解析到内网地址 {ip},已拦截{hint}") + + if needs_check: + for ip in await _real_ips(host): + if _is_private(ip): + raise UnsafeUrlError(f"{host} 的真实地址 {ip} 是内网地址,已拦截") return url @@ -79,7 +169,7 @@ async def safe_request( 逐跳重新校验,并对响应体截断。""" timeout = timeout or settings.http_tool_timeout max_bytes = max_bytes or settings.http_tool_max_bytes - current = assert_safe_url(url) + current = await assert_safe_url(url) async with httpx.AsyncClient( timeout=timeout, follow_redirects=False, trust_env=False @@ -94,7 +184,7 @@ async def safe_request( content=data, ) if resp.status_code in (301, 302, 303, 307, 308) and "location" in resp.headers: - current = assert_safe_url( + current = await assert_safe_url( str(httpx.URL(current).join(resp.headers["location"])) ) continue diff --git a/backend/tests/test_conversation_status.py b/backend/tests/test_conversation_status.py index 3b37ca5a..dc673b30 100644 --- a/backend/tests/test_conversation_status.py +++ b/backend/tests/test_conversation_status.py @@ -193,3 +193,50 @@ async def test_the_detail_carries_the_same_status(client): await _turn(client, conv, run_id=run) detail = (await client.get(f"/api/conversations/{conv}")).json() assert (detail["last_status"], detail["last_run_id"]) == ("waiting", run) + + +# -------------------------------------------------------------------------- +# 重试之后:meta.runId 才是这一轮现在的运行 +# -------------------------------------------------------------------------- + + +async def test_a_retried_turn_reports_the_new_run_not_the_old_one(client): + """重试会换一次运行,可 run_id 列清不掉(TurnPatch 的 None 表示「不改」), + 还指着上一次失败的运行;前端写的 meta.runId 才是权威。列表得按它报, + 不然重试正在建图时,左栏还挂着上一次的「失败」。""" + conv = await _conversation(client, "重试了") + old = await _run("failed") + turn = await _turn(client, conv, run_id=old, status="error", error="查询超时", + meta={"v": 1, "runId": old}) + # 重试:重新建图,还没有运行 + r = await client.patch(f"/api/conversations/{conv}/turns/{turn['id']}", + json={"status": "running", "meta": {"v": 1, "runId": None}}) + assert r.status_code == 200 + item = (await _listed(client))[conv] + assert (item["last_status"], item["last_run_id"]) == ("running", None) + + # 重试出来的运行起来了,还在跑 + new = await _run("running") + await client.patch(f"/api/conversations/{conv}/turns/{turn['id']}", + json={"meta": {"v": 1, "runId": new}}) + item = (await _listed(client))[conv] + assert (item["last_status"], item["last_run_id"]) == ("running", new) + detail = (await client.get(f"/api/conversations/{conv}")).json() + assert (detail["last_status"], detail["last_run_id"]) == ("running", new) + + +async def test_old_turns_without_run_id_in_meta_still_use_the_column(client): + conv = await _conversation(client, "老轮次") + run = await _run("interrupted", pending=True) + await _turn(client, conv, run_id=run, meta={"v": 1, "ms": 1200}) + item = (await _listed(client))[conv] + assert (item["last_status"], item["last_run_id"]) == ("waiting", run) + + +async def test_turns_carry_updated_at_for_the_same_staleness_clock(client): + """前端判断「建图断了」要和后端用同一只钟:后端按 updated_at 算。""" + conv = await _conversation(client, "带时间") + turn = await _turn(client, conv) + assert turn.get("updated_at"), turn + detail = (await client.get(f"/api/conversations/{conv}")).json() + assert detail["turns"][0]["updated_at"] diff --git a/backend/tests/test_copilot_scope.py b/backend/tests/test_copilot_scope.py index d0ed604e..5eb66696 100644 --- a/backend/tests/test_copilot_scope.py +++ b/backend/tests/test_copilot_scope.py @@ -170,6 +170,19 @@ async def test_a_scope_of_unknown_sources_is_refused_up_front(client, monkeypatc assert status == 400 detail = body[0]["detail"] assert "数据源" in detail and "no_such_source" not in detail.split(":")[0] + # 前端按机器码认,不按「限定的数据源」这几个字的前缀认:文案改了也不失灵。 + # detail 仍是一句话,老前端照旧能显示 + assert isinstance(detail, str) + assert body[0]["code"] == "datasource_scope_empty" + + +async def test_the_one_shot_generate_refuses_an_empty_scope_with_the_same_code(client, sources): + r = await client.post("/api/copilot/generate", json={ + "instruction": "订单多少", "datasource_ids": ["no_such_source"], + }) + assert r.status_code == 400, r.text + assert r.json()["code"] == "datasource_scope_empty" + assert isinstance(r.json()["detail"], str) async def test_the_one_shot_generate_honours_the_scope(client, monkeypatch, sources): diff --git a/backend/tests/test_datasource_preview.py b/backend/tests/test_datasource_preview.py index 7dcae041..9f0f592b 100644 --- a/backend/tests/test_datasource_preview.py +++ b/backend/tests/test_datasource_preview.py @@ -8,6 +8,8 @@ schema 把缓存换回来,而那一次要是失败,缓存和配置就对不 """ from __future__ import annotations +import re + import sqlite3 import pytest @@ -95,6 +97,8 @@ async def test_a_dry_run_that_finds_nothing_says_why_and_still_leaves_the_cache( preview = r.json() assert preview["dry_run"] is True and preview["table_count"] == 0 assert preview["schema_error"], "探失败了要说出来,不能看起来像「这个 schema 是空的」" + # 这句话原样画在数据源卡片上:不带驱动的异常类名(OperationalError: …) + assert not re.search(r"\b[A-Z]\w*(Error|Exception)\b", preview["schema_error"]), preview["schema_error"] assert "main" in preview["available_schemas"] assert await _cache(source["id"]) == before diff --git a/backend/tests/test_net_fake_ip.py b/backend/tests/test_net_fake_ip.py new file mode 100644 index 00000000..5d68eafd --- /dev/null +++ b/backend/tests/test_net_fake_ip.py @@ -0,0 +1,198 @@ +"""本机代理 fake-ip 模式下的 SSRF 检查。 + +Clash / mihomo 一类代理开 TUN + fake-ip 时,本机查任何域名都先拿到 198.18.0.0/15 +里的一个假地址,真正的连接由代理接管。Python 把这个网段算作私有地址,于是网页工具 +对所有外网地址都报「解析到内网地址,已拦截」。 + +不能简单放行这个网段:fake-ip 模式下内网域名同样被映射成 198.18 的假地址,放行 +等于关掉防护。做法是配置了 fake-ip 网段之后,落在网段里的解析结果改向公共 DNS +(DNS-over-HTTPS,不会被 TUN 劫持)核实真实地址:公网放行,内网照拦,核实不了按拦截算。 +""" + +from __future__ import annotations + +import asyncio +import json +import socket + +import httpx +import pytest + +from app.core.config import Settings, settings +from app.tools import net +from app.tools.net import UnsafeUrlError, assert_safe_url + +FAKE = "198.18.1.183" + + +@pytest.fixture(autouse=True) +def clean(monkeypatch): + net._real_ip_cache.clear() + monkeypatch.setattr(settings, "http_tool_allowlist", []) + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", []) + monkeypatch.setattr(settings, "http_tool_doh_urls", ["https://doh-a.test/resolve", "https://doh-b.test/dns-query"]) + yield + net._real_ip_cache.clear() + + +def resolves_to(monkeypatch, *ips): + async def fake_getaddrinfo(host, port, *args, **kwargs): + return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (ip, port)) for ip in ips] + loop = asyncio.get_running_loop() + monkeypatch.setattr(loop, "getaddrinfo", fake_getaddrinfo) + + +class DoH: + """假的公共 DNS:按 (resolver 主机, 记录类型) 返回;记下查了几次。""" + + def __init__(self, answers: dict[str, dict] | None = None, fail: set[str] | None = None): + self.answers = answers or {} + self.fail = fail or set() + self.calls: list[tuple[str, str, str]] = [] + + def handler(self, request: httpx.Request) -> httpx.Response: + host = request.url.host + name = request.url.params.get("name") + rtype = request.url.params.get("type") + self.calls.append((host, name, rtype)) + assert request.headers.get("accept") == "application/dns-json" + if host in self.fail: + raise httpx.ConnectError("resolver unreachable") + body = self.answers.get(f"{host}:{rtype}", {"Status": 0, "Answer": []}) + return httpx.Response(200, content=json.dumps(body)) + + def install(self, monkeypatch): + monkeypatch.setattr( + net, "_doh_client", + lambda: httpx.AsyncClient(transport=httpx.MockTransport(self.handler), timeout=5), + ) + + +def a_record(*ips, ttl=120, cname=None): + answers = [{"name": "x", "type": 5, "TTL": ttl, "data": cname}] if cname else [] + answers += [{"name": "x", "type": 1, "TTL": ttl, "data": ip} for ip in ips] + return {"Status": 0, "Answer": answers} + + +async def test_without_config_a_fake_ip_is_blocked_with_a_hint(monkeypatch): + resolves_to(monkeypatch, FAKE) + with pytest.raises(UnsafeUrlError) as e: + await assert_safe_url("https://ir.example.com/page") + message = str(e.value) + assert "已拦截" in message + assert "fake-ip" in message and "AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES" in message + + +async def test_a_fake_ip_is_allowed_when_public_dns_says_the_host_is_public(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE) + doh = DoH({"doh-a.test:A": a_record("93.184.216.34", cname="edge.example.net")}) + doh.install(monkeypatch) + assert await assert_safe_url("https://ir.example.com/page") == "https://ir.example.com/page" + assert ("doh-a.test", "ir.example.com", "A") in doh.calls + + +async def test_a_fake_ip_hiding_an_intranet_address_is_still_blocked(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE) + DoH({"doh-a.test:A": a_record("10.0.0.5")}).install(monkeypatch) + with pytest.raises(UnsafeUrlError) as e: + await assert_safe_url("http://wiki.corp.example/") + assert "10.0.0.5" in str(e.value) and "已拦截" in str(e.value) + + +async def test_an_ipv6_intranet_answer_is_blocked_too(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE) + DoH({ + "doh-a.test:A": a_record("93.184.216.34"), + "doh-a.test:AAAA": {"Status": 0, "Answer": [{"name": "x", "type": 28, "TTL": 60, "data": "fd00::1"}]}, + }).install(monkeypatch) + with pytest.raises(UnsafeUrlError): + await assert_safe_url("https://mixed.example.com/") + + +async def test_a_name_public_dns_does_not_know_is_blocked(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE) + DoH({"doh-a.test:A": {"Status": 3}, "doh-a.test:AAAA": {"Status": 3}}).install(monkeypatch) + with pytest.raises(UnsafeUrlError) as e: + await assert_safe_url("http://build-server.internal/") + assert "查不到" in str(e.value) + + +async def test_when_no_resolver_answers_it_fails_closed(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE) + DoH(fail={"doh-a.test", "doh-b.test"}).install(monkeypatch) + with pytest.raises(UnsafeUrlError) as e: + await assert_safe_url("https://ir.example.com/") + assert "核实" in str(e.value) + + +async def test_the_second_resolver_is_tried_when_the_first_is_down(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE) + DoH({"doh-b.test:A": a_record("93.184.216.34")}, fail={"doh-a.test"}).install(monkeypatch) + assert await assert_safe_url("https://ir.example.com/") + + +async def test_an_ip_literal_inside_the_fake_range_cannot_be_verified_and_is_blocked(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + doh = DoH() + doh.install(monkeypatch) + with pytest.raises(UnsafeUrlError): + await assert_safe_url(f"http://{FAKE}/") + assert doh.calls == [] + + +async def test_a_real_private_answer_is_blocked_without_asking_public_dns(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE, "192.168.1.10") + doh = DoH({"doh-a.test:A": a_record("93.184.216.34")}) + doh.install(monkeypatch) + with pytest.raises(UnsafeUrlError) as e: + await assert_safe_url("https://split.example.com/") + assert "192.168.1.10" in str(e.value) + assert doh.calls == [] + + +async def test_verified_answers_are_cached_for_a_while(monkeypatch): + monkeypatch.setattr(settings, "http_tool_fake_ip_ranges", ["198.18.0.0/15"]) + resolves_to(monkeypatch, FAKE) + doh = DoH({"doh-a.test:A": a_record("93.184.216.34")}) + doh.install(monkeypatch) + await assert_safe_url("https://ir.example.com/a") + first = len(doh.calls) + await assert_safe_url("https://ir.example.com/b") + assert len(doh.calls) == first + + +async def test_public_and_private_answers_behave_as_before(monkeypatch): + resolves_to(monkeypatch, "93.184.216.34") + assert await assert_safe_url("https://www.example.com/") + resolves_to(monkeypatch, "10.1.2.3") + with pytest.raises(UnsafeUrlError) as e: + await assert_safe_url("https://intra.example.com/") + assert "fake-ip" not in str(e.value) + + +def test_list_settings_accept_comma_separated_env(monkeypatch): + monkeypatch.setenv("AGENTLAB_HTTP_TOOL_ALLOWLIST", "example.com, example.org") + monkeypatch.setenv("AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES", "198.18.0.0/15,fdfe:dcba:9876::/48") + s = Settings() + assert s.http_tool_allowlist == ["example.com", "example.org"] + assert s.http_tool_fake_ip_ranges == ["198.18.0.0/15", "fdfe:dcba:9876::/48"] + + +def test_a_malformed_fake_ip_range_is_rejected_at_startup(monkeypatch): + monkeypatch.setenv("AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES", "198.18.0.0/99") + with pytest.raises(Exception): + Settings() + + +def test_the_default_resolver_order_puts_an_unpolluted_one_first(monkeypatch): + """境内公共 DNS 对被封锁的域名返回污染结果:维基百科的 AAAA 给 2001::1,被当成 + 内网拦掉。默认先问境外的,境内的只作兜底""" + monkeypatch.delenv("AGENTLAB_HTTP_TOOL_DOH_URLS", raising=False) + assert Settings().http_tool_doh_urls[0] == "https://1.1.1.1/dns-query" diff --git a/backend/tests/test_query_deadline.py b/backend/tests/test_query_deadline.py index 5a7030b2..92df4e5a 100644 --- a/backend/tests/test_query_deadline.py +++ b/backend/tests/test_query_deadline.py @@ -258,6 +258,12 @@ def _fake(monkeypatch, **kw: Any) -> _FakeEngine: return fake +def _budget(sql: str, prefix: str, scale: float = 1) -> float: + """下发的时限值。数据库拿到的是剩下的预算,取连接也算在里面,所以略小于整段时限。""" + assert sql.startswith(prefix), sql + return float(sql[len(prefix):]) * scale + + def _remote(kind: str, *, readonly: bool = True) -> Any: return types.SimpleNamespace(id=f"r_{kind}", name="shop", kind=kind, readonly=readonly, options={}, database="shop") @@ -266,7 +272,8 @@ def _remote(kind: str, *, readonly: bool = True) -> Any: async def test_postgres_gets_a_statement_timeout_for_this_transaction_only(monkeypatch): fake = _fake(monkeypatch) await run_query(_remote("postgres"), "SELECT 1", limits=QueryLimits(timeout_seconds=5)) - assert fake.log[:2] == ["SET LOCAL statement_timeout = 5000", "SELECT 1"], fake.log + assert 4900 <= _budget(fake.log[0], "SET LOCAL statement_timeout = ") <= 5000, fake.log + assert fake.log[1] == "SELECT 1", fake.log # SET LOCAL 随事务回滚作废,不用也不该再发一条恢复 assert not [s for s in fake.log[2:] if s.startswith("SET")] @@ -275,13 +282,14 @@ async def test_postgres_writes_are_bounded_too(monkeypatch): fake = _fake(monkeypatch) await run_query(_remote("postgresql", readonly=False), "UPDATE orders SET id = 1", limits=QueryLimits(timeout_seconds=5)) - assert fake.log[:2] == ["SET LOCAL statement_timeout = 5000", "UPDATE orders SET id = 1"] + assert 4900 <= _budget(fake.log[0], "SET LOCAL statement_timeout = ") <= 5000, fake.log + assert fake.log[1] == "UPDATE orders SET id = 1" async def test_mysql_gets_max_execution_time_and_it_is_put_back(monkeypatch): fake = _fake(monkeypatch) await run_query(_remote("mysql"), "SELECT 1", limits=QueryLimits(timeout_seconds=5)) - assert fake.log[0] == "SET SESSION max_execution_time = 5000" + assert 4900 <= _budget(fake.log[0], "SET SESSION max_execution_time = ") <= 5000, fake.log assert "SELECT 1" in fake.log # 会话级设置跟着连接回池子:用完恢复成服务器的缺省,探查结构这类长操作不受它牵连 assert fake.log[-1] == "SET SESSION max_execution_time = DEFAULT" @@ -290,14 +298,15 @@ async def test_mysql_gets_max_execution_time_and_it_is_put_back(monkeypatch): async def test_mariadb_uses_its_own_variable_in_seconds(monkeypatch): fake = _fake(monkeypatch, mariadb=True) await run_query(_remote("mysql"), "SELECT 1", limits=QueryLimits(timeout_seconds=5)) - assert fake.log[0] == "SET SESSION max_statement_time = 5" + assert 4900 <= _budget(fake.log[0], "SET SESSION max_statement_time = ", 1000) <= 5000, fake.log assert fake.log[-1] == "SET SESSION max_statement_time = DEFAULT" async def test_oracle_gets_a_call_timeout_and_it_is_put_back(monkeypatch): fake = _fake(monkeypatch) await run_query(_remote("oracle"), "SELECT 1 FROM DUAL", limits=QueryLimits(timeout_seconds=5)) - assert "call_timeout=5000" in fake.log, fake.log + sent = next(_budget(s, "call_timeout=") for s in fake.log if s.startswith("call_timeout=")) + assert 4900 <= sent <= 5000, fake.log assert fake.driver.call_timeout == 0, "call_timeout 跟着连接回池子,探查结构时也会被它掐" @@ -348,6 +357,81 @@ async def test_other_errors_are_not_mistaken_for_a_timeout(monkeypatch): await run_query(_remote("postgres"), "SELECT 1", limits=QueryLimits(timeout_seconds=5)) +# -------------------------------------------------------------------------- +# 取连接慢:数据库的时限和后端兜底从同一刻算起 +# -------------------------------------------------------------------------- + + +class _HonouringConn: + """一台守 statement_timeout 的「服务器」:从收到 SET LOCAL 那一刻起算,到点报超时。""" + + def __init__(self, log: list[tuple[float, str]], t0: float) -> None: + self.log, self.t0 = log, t0 + self.dialect = types.SimpleNamespace(is_mariadb=False) + + async def execute(self, clause): + self.log.append((time.monotonic() - self.t0, str(clause))) + return _Result([]) + + async def stream(self, clause): + self.log.append((time.monotonic() - self.t0, str(clause))) + set_at, sql = next((t, s) for t, s in self.log if s.startswith("SET LOCAL")) + ms = int(sql.rsplit("=", 1)[1]) + await asyncio.sleep(max(0.0, set_at + ms / 1000 - (time.monotonic() - self.t0))) + raise RuntimeError("canceling statement due to statement timeout") + + +class _SlowPool: + """取一个连接要等 wait 秒:冷启动的远程库、pool_recycle 之后重连、团队并行把池子占满。""" + + def __init__(self, wait: float) -> None: + self.wait = wait + self.log: list[tuple[float, str]] = [] + self.t0 = time.monotonic() + + @asynccontextmanager + async def connect(self): + await asyncio.sleep(self.wait) + yield _HonouringConn(self.log, self.t0) + + begin = connect + + +def _slow(monkeypatch, wait: float) -> _SlowPool: + pool = _SlowPool(wait) + + async def _get(_source): + pool.t0 = time.monotonic() + return pool + + monkeypatch.setattr(engines, "get", _get) + return pool + + +async def test_a_slow_connection_does_not_let_the_backstop_beat_the_database(monkeypatch): + """取连接花掉的时间也算在时限里:数据库拿到的是剩下的预算,于是它先停下语句、 + 报出「超过 Ns 被中断」;后端兜底只在数据层也卡住时才出面。以前兜底从取连接之前 + 算起、数据库却拿到整段时限,取连接一慢,兜底就抢在数据库前面把语句取消了—— + PG 的连接照样被占到「连上的时刻 + 时限」。""" + pool = _slow(monkeypatch, 0.7) + with pytest.raises(SqlRejected) as caught: + await run_query(_remote("postgres"), "SELECT 1", limits=QueryLimits(timeout_seconds=1)) + sent = next(int(s.rsplit("=", 1)[1]) for _t, s in pool.log if s.startswith("SET LOCAL")) + assert 0 < sent < 1000, f"数据库拿到的该是剩下的预算,实际是 {sent}ms" + assert not isinstance(caught.value.__cause__, TimeoutError), \ + "该是数据库按时限停下的那条路(deadline.stopped),不是后端兜底的 TimeoutError" + assert "查询超过 1s 被中断" in str(caught.value) + + +async def test_a_budget_spent_waiting_for_a_connection_sends_nothing(monkeypatch): + """取到连接时时限已经用完:直接按超时报,一条语句也不发给数据库。""" + pool = _slow(monkeypatch, 0.4) + with pytest.raises(SqlRejected) as caught: + await run_query(_remote("postgres"), "SELECT 1", limits=QueryLimits(timeout_seconds=0.3)) + assert pool.log == [], pool.log + assert "查询超过 0.3s 被中断" in str(caught.value) + + # -------------------------------------------------------------------------- # 接口:按源配置的时限 # -------------------------------------------------------------------------- diff --git a/backend/tests/test_runs_api.py b/backend/tests/test_runs_api.py index 96bf1750..52dbd22f 100644 --- a/backend/tests/test_runs_api.py +++ b/backend/tests/test_runs_api.py @@ -214,6 +214,19 @@ async def test_pending_approvals_carry_their_context_newest_first(client): assert a["resolved_by"] is None and a["resolved_at"] is None +async def test_a_run_without_a_workflow_is_named_by_the_glossary(client): + # 画布没保存就跑、问数据当场搭的:以前记成「临时图」,运行和审批上原样露给人看。 + # 术语表里「图」只在技术细节里出现,这里叫「未保存的工作流」 + run_id = (await client.post("/api/runs", json={ + "graph": GATED, "input": {"question": "q"}})).json()["id"] + await _wait(run_id, ("interrupted",)) + + assert (await client.get(f"/api/runs/{run_id}")).json()["workflow_name"] == "未保存的工作流" + [pending] = (await client.get("/api/approvals", params={"run_id": run_id})).json() + assert pending["workflow_id"] is None + assert pending["workflow_name"] == "未保存的工作流" + + async def test_resolution_records_who_signed_and_when(client): run_id = (await client.post("/api/runs", json={ "graph": GATED, "input": {"question": "q"}})).json()["id"] diff --git a/backend/tests/test_sandbox_errors.py b/backend/tests/test_sandbox_errors.py new file mode 100644 index 00000000..52240209 --- /dev/null +++ b/backend/tests/test_sandbox_errors.py @@ -0,0 +1,111 @@ +"""沙箱起不来时的报错:说原因,不带 Python 异常类名(SPEC §3.1)。 + +四个后端起进程、连 VM 失败时以前都写 f"{type(e).__name__}: {e}",代码节点再把它 +拼进首行,界面上的红字就成了「代码执行失败(exit=0):FileNotFoundError: [Errno 2] +No such file or directory: …」。原始异常留在日志里,排查时照样找得到。 +""" +from __future__ import annotations + +import asyncio +import re +import types + +import pytest + +from app.engine.context import NodeContext, NodeError, RunContext +from app.engine.nodes.tools import run_code +from app.engine.schema import GraphNode, GraphSpec +from app.sandbox import interpreter +from app.sandbox.bubblewrap_sandbox import BubblewrapSandbox +from app.sandbox.local_sandbox import LocalSandbox +from app.sandbox.microvm_sandbox import MicroVMSandbox +from app.sandbox.seatbelt_sandbox import SeatbeltSandbox + +_CLASS_NAME = re.compile(r"\b[A-Z]\w*(?:Error|Exception)\b") + + +def _broken_interpreter(tmp_path) -> str: + """一个存在、可执行,但 shebang 指向不存在的解释器的文件:which 找得到,exec 起不来。""" + fake = tmp_path / "python-broken" + fake.write_text("#!/nonexistent/interpreter\n") + fake.chmod(0o755) + return str(fake) + + +def test_a_code_node_whose_interpreter_cannot_start_says_why(tmp_path, monkeypatch) -> None: + import app.engine.nodes.tools as tools + + monkeypatch.setattr(interpreter, "python_argv", lambda: [_broken_interpreter(tmp_path)]) + local = LocalSandbox() + + class _Manager: + async def run(self, code, *, backend=None, **kwargs): # noqa: ANN001, ANN003, ANN201 + return await local.run(code, **kwargs) + + monkeypatch.setattr(tools, "sandbox_manager", _Manager()) + node = GraphNode(id="calc", type="code", + data={"label": "计算", "config": {"language": "python", "code": "print(1)", + "isolation": "fast"}}) + ctx = NodeContext(node=node, run=RunContext(run_id="r1", thread_id="t1", + spec=GraphSpec(nodes=[node], edges=[]))) + + with pytest.raises(NodeError) as caught: + asyncio.run(run_code({"vars": {}}, ctx)) + text = str(caught.value) + assert text.startswith("代码执行失败"), text + assert not _CLASS_NAME.search(text), text + + +async def _boom(*args, **kwargs): # noqa: ANN002, ANN003, ANN202 + raise PermissionError(13, "Permission denied", "/usr/bin/sandbox-exec") + + +@pytest.mark.parametrize("backend", [LocalSandbox, SeatbeltSandbox, BubblewrapSandbox]) +def test_a_process_that_cannot_spawn_has_no_class_name(backend, monkeypatch) -> None: + if backend is not LocalSandbox: + monkeypatch.setattr(backend, "available", staticmethod(lambda: True)) + monkeypatch.setattr(asyncio, "create_subprocess_exec", _boom) + + result = asyncio.run(backend().run("print(1)")) + assert not result.ok + assert result.error and not _CLASS_NAME.search(result.error), result.error + + +def _microvm(monkeypatch) -> MicroVMSandbox: + async def nothing(*args, **kwargs): # noqa: ANN002, ANN003, ANN202 + return None + + monkeypatch.setattr(MicroVMSandbox, "available", staticmethod(lambda: True)) + monkeypatch.setattr(MicroVMSandbox, "_reap_idle", nothing) + monkeypatch.setattr(MicroVMSandbox, "_destroy", nothing) + return MicroVMSandbox() + + +def test_a_microvm_that_cannot_boot_has_no_class_name(monkeypatch) -> None: + async def no_vm(*args, **kwargs): # noqa: ANN002, ANN003, ANN202 + raise RuntimeError("虚拟化框架拒绝了这次启动") + + vm = _microvm(monkeypatch) + monkeypatch.setattr(MicroVMSandbox, "_lease", no_vm) + result = asyncio.run(vm.run("print(1)")) + assert result.error and result.error.startswith("microVM 启动失败"), result.error + assert not _CLASS_NAME.search(result.error), result.error + + +def test_a_microvm_that_drops_mid_exec_has_no_class_name(monkeypatch) -> None: + async def write(*args, **kwargs): # noqa: ANN002, ANN003, ANN202 + return None + + async def gone(*args, **kwargs): # noqa: ANN002, ANN003, ANN202 + raise ConnectionResetError(54, "Connection reset by peer") + + lease = types.SimpleNamespace(vm=types.SimpleNamespace(fs=types.SimpleNamespace(write=write, mkdir=write), + exec=gone)) + + async def leased(*args, **kwargs): # noqa: ANN002, ANN003, ANN202 + return lease + + vm = _microvm(monkeypatch) + monkeypatch.setattr(MicroVMSandbox, "_lease", leased) + result = asyncio.run(vm.run("print(1)")) + assert result.error and not _CLASS_NAME.search(result.error), result.error diff --git a/backend/tests/test_tabular.py b/backend/tests/test_tabular.py index 29a034d5..dd381d18 100644 --- a/backend/tests/test_tabular.py +++ b/backend/tests/test_tabular.py @@ -282,3 +282,17 @@ async def test_upload_will_not_hijack_a_real_database(client): resp = await _upload(client, "tab_real", xlsx({"s": [["a"], [1]]})) assert resp.status_code == 409 assert "换个名字" in resp.json()["detail"] + + +def test_broken_excel_message_has_no_exception_class_name(): + """坏掉的 .xlsx:报错说人话,不带 Python 异常类名(BadZipFile、InvalidFileException…)。 + + 这句话原样进上传的 400 detail,界面上是红字首行。 + """ + from app.data.tabular import read_tables + + with pytest.raises(UnsupportedTable) as info: + read_tables(b"not really an xlsx", "orders.xlsx") + text = str(info.value) + assert text.startswith("这个 Excel 读不开") + assert "BadZipFile" not in text and "Error" not in text and "Exception" not in text diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 91be0f5e..0c617c95 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState, useSyncExternalStore } from 'react' +import { useEffect, useRef, useState } from 'react' import type { ReactNode, RefObject } from 'react' import { Link, NavLink, Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom' import { Bell, BellOff, Moon, RotateCw, Sun, UserRound } from 'lucide-react' @@ -33,6 +33,8 @@ import { } from './lib/notify' import type { Attention, FaviconDot } from './lib/notify' import { useLeaveBlocker } from './lib/leave' +import { useLocalActor } from './lib/actor' +import { runName } from './lib/terms' export default function App() { const loaded = useCatalog((s) => s.loaded) @@ -383,27 +385,10 @@ function ThemeToggle() { ) } -const ACTOR_KEY = 'agentlab_actor' - -function readActor(): string { - try { - return localStorage.getItem(ACTOR_KEY)?.trim() ?? '' - } catch { - return '' - } -} - -function subscribeActor(cb: () => void): () => void { - // 设置页在同一个标签页里改署名不会触发 storage 事件;它若派发 agentlab:actor - // 就立刻跟上,否则下一次导航重绘时也会重新读到 - const events = ['storage', 'agentlab:actor', 'focus'] - events.forEach((e) => window.addEventListener(e, cb)) - return () => events.forEach((e) => window.removeEventListener(e, cb)) -} - /** 操作者署名的首字:发布、审批、正式运行都记在这个名下,得一直看得见是谁 */ function ActorButton() { - const actor = useSyncExternalStore(subscribeActor, readActor, () => '') + const actor = useLocalActor() + // 兜底:绕开 setLocalActor 直接写 localStorage 的地方不会通知,导航重绘时也重读一次 useLocation() const initial = actor ? Array.from(actor)[0].toUpperCase() : '' return ( @@ -605,6 +590,7 @@ function TelemetryPanel({ onClose, anchor, stalled }: { const secs = down && retryAt ? Math.max(0, Math.ceil((retryAt - now) / 1000)) : null const retry = async () => { + if (busy) return setBusy(true) try { const ok = await useCatalog.getState().checkBackend() @@ -676,7 +662,10 @@ function TelemetryPanel({ onClose, anchor, stalled }: { )}