mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-18 17:21:58 +00:00
全栈安全加固、性能提速与架构演进:连接池与沙箱止血、双路混合检索、多版本归档及前端分包治理
- 第一批(安全加固与生命周期止血):隔离测试宿主机密钥与 .env 泄漏;修复 SQLite 取消查询挂死、:memory: 连接池报错与 EngineCache 世代竞态;补齐运行与评测删除时的 Checkpointer 线程、Artifact 磁盘文件与无主附件回收;加固三套沙箱路径越界、进程组清理与 Seatbelt 数据目录隔离,封堵 SSRF IPv4-mapped IPv6 元数据绕过与响应体 OOM;修复推理模型多轮 reasoning_content、结构化输出 Token 漏计与缓存折扣计费 - 第二批(数据库/集群/问数提速与首屏分包):补齐核心表索引与 SQLite WAL 调优,列表接口按需 defer 大列,收敛启动扫描并打通跨进程 slot 槽位唤醒;优化跨源全局选表、哈希桶数字绑定、流式 CSV 导入及多方言分析算子;剥离首屏静态引入的 @xyflow/react 与全局样式(首屏主包降至 461.59 kB),补充流式 rAF 攒批、Store 精细订阅与表单无障碍绑定 - 第三批(架构演进与工程自动化闭环):记录库超挂接阈值历史版本自动增量归档至单库多表(解除 SQLite 10 库 ATTACH 硬上限);重构知识库为词面 BM25 与全集合归一化向量矩阵真·双路独立召回并支持游标分批重建;升级多方言感知 SQL 模板词法分析(E'...' / $$...$$ / q'[...]' / # / 嵌套注释)与 Decimal / bool / NaN 参数校验,消除 ledger 层对 api 路由层的反向依赖;拆分 3101 行 CaliberDrafts 页面为 7 个领域子模块,消除检查脚本本机路径硬编码并新增 GitHub Actions CI 门禁与三批回归测试套件
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
from collections.abc import AsyncIterator
|
||||
from collections.abc import AsyncIterator, Iterable
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -64,6 +64,12 @@ async def open_sqlite_saver(path: Path | str) -> AsyncIterator[BaseCheckpointSav
|
||||
from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
|
||||
|
||||
async with AsyncSqliteSaver.from_conn_string(str(path)) as saver:
|
||||
conn = getattr(saver, "conn", None)
|
||||
if conn is not None:
|
||||
await conn.execute("PRAGMA journal_mode=WAL")
|
||||
await conn.execute("PRAGMA synchronous=NORMAL")
|
||||
await conn.execute("PRAGMA journal_size_limit=67108864")
|
||||
await conn.execute("PRAGMA busy_timeout=5000")
|
||||
yield saver
|
||||
|
||||
|
||||
@@ -75,6 +81,39 @@ def open_saver() -> contextlib.AbstractAsyncContextManager[BaseCheckpointSaver]:
|
||||
return open_sqlite_saver(settings.checkpoint_path)
|
||||
|
||||
|
||||
async def _delete_on(saver: Any, ids: list[str]) -> None:
|
||||
adelete = getattr(saver, "adelete_thread", None)
|
||||
if not callable(adelete):
|
||||
return
|
||||
setup = getattr(saver, "setup", None)
|
||||
if callable(setup):
|
||||
with contextlib.suppress(Exception):
|
||||
await setup()
|
||||
for tid in ids:
|
||||
with contextlib.suppress(Exception):
|
||||
await adelete(tid)
|
||||
|
||||
|
||||
async def delete_threads(thread_ids: Iterable[str]) -> None:
|
||||
"""运行被彻底删除或评测明细被回收时,一并清理断点库里对应的 thread。
|
||||
|
||||
注意:正常运行结束(succeeded / cancelled)时不能删,因为 GET /api/runs/{id}/state 和 /checkpoints
|
||||
还要回看历史断点;只有在运行记录本身从数据库删除之后才调这里。
|
||||
"""
|
||||
ids = sorted({str(t) for t in thread_ids if t})
|
||||
if not ids:
|
||||
return
|
||||
from app.engine.runner import run_manager
|
||||
|
||||
saver = run_manager._checkpointer
|
||||
if saver is not None:
|
||||
await _delete_on(saver, ids)
|
||||
return
|
||||
if settings.checkpoint_conninfo or settings.checkpoint_path.is_file():
|
||||
async with open_saver() as opened:
|
||||
await _delete_on(opened, ids)
|
||||
|
||||
|
||||
async def count(saver: Any, thread_id: str | None = None) -> tuple[int, int]:
|
||||
"""(断点条数, pending writes 条数)。thread_id 为空数全部。copy-to-postgres 核对用,走公开接口。"""
|
||||
config = {"configurable": {"thread_id": thread_id}} if thread_id else None
|
||||
|
||||
@@ -11,6 +11,10 @@ from app.engine.evidence import ledger_enabled
|
||||
from app.engine.lineage import scoped_id as _scoped
|
||||
from app.analysis.contract import LEDGER_KIND
|
||||
|
||||
MASKED = "已遮罩"
|
||||
RUN_NOT_FOUND = "run_not_found"
|
||||
REPORT_NOT_FOUND = "evidence_report_not_found"
|
||||
|
||||
|
||||
def _fetch(artifact: str) -> tuple[Any, bool]:
|
||||
try:
|
||||
@@ -236,3 +240,157 @@ def metric_cards(sealed: Sealed) -> list[tuple[str, str, dict[str, Any]]]:
|
||||
if ok is True and isinstance(content, dict) and isinstance(content.get("metrics"), list):
|
||||
out.append((node_id, art, content))
|
||||
return out
|
||||
|
||||
|
||||
async def load_sealed(run_id: str) -> Sealed:
|
||||
"""读取封存范围;运行记录不存在时抛 404 CodedHTTPException(供引擎与领域层共用,无需引入 api/evidence)。"""
|
||||
from app.api.coded import CodedHTTPException
|
||||
|
||||
try:
|
||||
return await read_sealed(run_id)
|
||||
except LookupError:
|
||||
raise CodedHTTPException(404, "运行记录不存在", RUN_NOT_FOUND) from None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Report:
|
||||
node_id: str
|
||||
doc_artifact: str
|
||||
ok: bool | None
|
||||
repairs: int | None
|
||||
stats: dict[str, Any] | None
|
||||
doc: dict[str, Any] | None
|
||||
hash_ok: bool | None
|
||||
fields: list[str]
|
||||
claims: str | None = None
|
||||
|
||||
|
||||
def output_evidence(sealed: Sealed) -> list[dict[str, Any]]:
|
||||
output = sealed.payload_of(sealed.nodes_of("output")) or {}
|
||||
primary = output.get("_evidence")
|
||||
if not isinstance(primary, dict):
|
||||
return []
|
||||
return [primary, *[r for r in primary.get("others") or [] if isinstance(r, dict)]]
|
||||
|
||||
|
||||
def reports_of(sealed: Sealed, *, nested: bool = False) -> list[Report]:
|
||||
"""封存范围内每个报告节点最后一次的自查结果,按第一次出现的顺序。"""
|
||||
latest: dict[str, dict[str, Any]] = {}
|
||||
for _, etype, node_id, data in sealed.events:
|
||||
if (
|
||||
etype == "report.checked"
|
||||
and node_id
|
||||
and isinstance(data, dict)
|
||||
and data.get("doc_artifact")
|
||||
and (nested or "/" not in node_id)
|
||||
):
|
||||
latest[node_id] = data
|
||||
evidence = output_evidence(sealed)
|
||||
out: list[Report] = []
|
||||
for node_id, data in latest.items():
|
||||
artifact = str(data["doc_artifact"])
|
||||
try:
|
||||
doc = load(artifact)
|
||||
hash_ok: bool | None = True if doc is not None else None
|
||||
except ValueError:
|
||||
doc, hash_ok = None, False
|
||||
if isinstance(doc, dict) and (doc.get("node_id") != node_id or doc.get("run_id") != sealed.run_id):
|
||||
doc, hash_ok = None, False
|
||||
fields = [
|
||||
f
|
||||
for r in evidence
|
||||
if r.get("report_node") == node_id and r.get("doc_artifact") == artifact
|
||||
for f in r.get("fields") or []
|
||||
]
|
||||
out.append(
|
||||
Report(
|
||||
node_id=node_id,
|
||||
doc_artifact=artifact,
|
||||
ok=data.get("ok"),
|
||||
repairs=data.get("repairs"),
|
||||
stats=(doc or {}).get("stats") or data.get("stats"),
|
||||
doc=doc if isinstance(doc, dict) else None,
|
||||
hash_ok=hash_ok,
|
||||
fields=fields,
|
||||
claims=data.get("claims") if isinstance(data.get("claims"), str) else None,
|
||||
)
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
def choose_report(reports: list[Report], wanted: str | None, sealed: Sealed) -> Report:
|
||||
"""从封存报告列表里选出目标报告(指定 wanted 优先,否则取出口 _evidence 主报告,再退回第一份)。"""
|
||||
from app.api.coded import CodedHTTPException
|
||||
|
||||
if wanted:
|
||||
chosen = next((r for r in reports if r.node_id == wanted), None)
|
||||
if chosen is None:
|
||||
raise CodedHTTPException(404, "本次运行中没有所选报告撰写节点的文档", REPORT_NOT_FOUND)
|
||||
return chosen
|
||||
primary = next(iter(output_evidence(sealed)), {})
|
||||
return next((r for r in reports if r.node_id == primary.get("report_node")), reports[0])
|
||||
|
||||
|
||||
class ShownText:
|
||||
"""把展示用 SQL、检查片段、合并警告里命中的运行遮罩值替换为「已遮罩」。"""
|
||||
|
||||
def __init__(self, secrets: Any) -> None:
|
||||
self._secrets = secrets
|
||||
self.changed = False
|
||||
|
||||
def text(self, value: Any) -> Any:
|
||||
if self._secrets is None or not isinstance(value, str) or not value:
|
||||
return value
|
||||
out = self._secrets.scrub(value, loose=True)
|
||||
self.changed = self.changed or out != value
|
||||
return out
|
||||
|
||||
def checks(self, checks: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
return [
|
||||
{**c, **{k: self.text(c[k]) for k in ("message", "sql_excerpt") if isinstance(c.get(k), str)}}
|
||||
for c in checks
|
||||
]
|
||||
|
||||
def merge(self, view: dict[str, Any]) -> dict[str, Any]:
|
||||
view["sql"] = self.text(view.get("sql"))
|
||||
view["warnings"] = [{**w, "message": self.text(w.get("message"))} for w in view.get("warnings") or []]
|
||||
view["inputs"] = [
|
||||
{**i, **({"checks": self.checks(i["checks"])} if i.get("checks") else {})}
|
||||
for i in view.get("inputs") or []
|
||||
]
|
||||
return view
|
||||
|
||||
|
||||
def open_masks(sealed: Sealed | None = None) -> Any:
|
||||
"""获取请求级数据源遮罩上下文(通过统一边界暴露给领域层,兼容测试对证据遮罩构造器的桩替换)。"""
|
||||
from app.api import evidence as _evidence_mod
|
||||
|
||||
return _evidence_mod._Masks(sealed)
|
||||
|
||||
|
||||
async def resolve_metric_masks(
|
||||
metric: dict[str, Any],
|
||||
card_node: str,
|
||||
artifact: str,
|
||||
sealed: Sealed,
|
||||
masks: Any,
|
||||
*,
|
||||
card: dict[str, Any] | None = None,
|
||||
output: Any = None,
|
||||
by_value: bool = True,
|
||||
sure: bool = False,
|
||||
) -> tuple[dict[str, Any], str | None]:
|
||||
"""判断单个指标及其输入是否命中的数据源遮罩。"""
|
||||
from app.api import evidence as _evidence_mod
|
||||
|
||||
return await _evidence_mod._metric_masks(
|
||||
metric,
|
||||
card_node,
|
||||
artifact,
|
||||
sealed,
|
||||
masks,
|
||||
card=card,
|
||||
output=output,
|
||||
by_value=by_value,
|
||||
sure=sure,
|
||||
)
|
||||
|
||||
@@ -10,6 +10,7 @@ from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from contextvars import ContextVar
|
||||
from dataclasses import dataclass, field
|
||||
from decimal import Decimal
|
||||
from typing import Any, Callable
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
@@ -36,6 +37,8 @@ def resolve_path(ctx: dict[str, Any], path: str) -> Any:
|
||||
if isinstance(cur, dict):
|
||||
cur = cur.get(name)
|
||||
else:
|
||||
if name.startswith("_"):
|
||||
return None
|
||||
cur = getattr(cur, name, None)
|
||||
if cur is None:
|
||||
return None
|
||||
@@ -65,11 +68,24 @@ _FILTERS = {
|
||||
}
|
||||
|
||||
|
||||
def _format_decimal(value: Decimal) -> str:
|
||||
if not value.is_finite():
|
||||
return str(value)
|
||||
text = str(value)
|
||||
if "e" in text or "E" in text:
|
||||
exp = value.as_tuple().exponent
|
||||
if isinstance(exp, int) and abs(exp) <= 100:
|
||||
return format(value, "f")
|
||||
return text
|
||||
|
||||
|
||||
def _stringify(value: Any) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
if isinstance(value, Decimal):
|
||||
return _format_decimal(value)
|
||||
if isinstance(value, (dict, list, tuple)):
|
||||
return json.dumps(value, ensure_ascii=False, default=str)
|
||||
return str(value)
|
||||
@@ -223,6 +239,7 @@ class ExpressionError(ValueError):
|
||||
# 一条画布上的分支条件就能把整个后端冻死。
|
||||
_MAX_POW_EXPONENT = 1024
|
||||
_MAX_POW_BASE = 1e15
|
||||
_MAX_SEQ_REPEAT = 1_000_000
|
||||
|
||||
|
||||
def _safe_pow(base: Any, exponent: Any) -> Any:
|
||||
@@ -237,10 +254,22 @@ def _safe_pow(base: Any, exponent: Any) -> Any:
|
||||
return operator.pow(base, exponent)
|
||||
|
||||
|
||||
def _safe_mul(a: Any, b: Any) -> Any:
|
||||
"""带上界的乘法:序列重复('x' * 10**9、[0] * 10**8)超界直接报错,防止在事件循环里把内存撑爆。"""
|
||||
seq_types = (str, bytes, bytearray, list, tuple, TruncatedRows)
|
||||
if isinstance(a, seq_types) and isinstance(b, int):
|
||||
if len(a) * max(0, b) > _MAX_SEQ_REPEAT:
|
||||
raise ExpressionError(f"序列重复长度超过上限 {_MAX_SEQ_REPEAT}")
|
||||
elif isinstance(b, seq_types) and isinstance(a, int):
|
||||
if len(b) * max(0, a) > _MAX_SEQ_REPEAT:
|
||||
raise ExpressionError(f"序列重复长度超过上限 {_MAX_SEQ_REPEAT}")
|
||||
return operator.mul(a, b)
|
||||
|
||||
|
||||
_BIN_OPS = {
|
||||
ast.Add: operator.add,
|
||||
ast.Sub: operator.sub,
|
||||
ast.Mult: operator.mul,
|
||||
ast.Mult: _safe_mul,
|
||||
ast.Div: operator.truediv,
|
||||
ast.FloorDiv: operator.floordiv,
|
||||
ast.Mod: operator.mod,
|
||||
|
||||
@@ -175,11 +175,23 @@ def _usage_of(message: BaseMessage, model_id: str) -> dict[str, Any]:
|
||||
meta = getattr(message, "usage_metadata", None) or {}
|
||||
inp = int(meta.get("input_tokens") or 0)
|
||||
out = int(meta.get("output_tokens") or 0)
|
||||
details = meta.get("input_token_details") or {}
|
||||
cache_read = int(details.get("cache_read") or 0) if isinstance(details, dict) else 0
|
||||
cache_create = int(details.get("cache_creation") or 0) if isinstance(details, dict) else 0
|
||||
if cache_read > 0 or cache_create > 0:
|
||||
try:
|
||||
cost = catalog.estimate_cost(
|
||||
model_id, inp, out, cache_read_tokens=cache_read, cache_creation_tokens=cache_create
|
||||
)
|
||||
except TypeError:
|
||||
cost = catalog.estimate_cost(model_id, inp, out)
|
||||
else:
|
||||
cost = catalog.estimate_cost(model_id, inp, out)
|
||||
return {
|
||||
"input_tokens": inp,
|
||||
"output_tokens": out,
|
||||
"total_tokens": inp + out,
|
||||
"cost_usd": round(catalog.estimate_cost(model_id, inp, out), 6),
|
||||
"cost_usd": round(cost, 6),
|
||||
"calls": 1,
|
||||
}
|
||||
|
||||
@@ -213,14 +225,31 @@ def explain_model_error(exc: Exception, model_id: str) -> str:
|
||||
"请稍后重试;如持续失败,请到「设置 → 模型接入」测试该模型")
|
||||
|
||||
|
||||
def _non_retriable_stream_error(exc: Exception) -> bool:
|
||||
"""鉴权失败、模型不存在、额度用尽、频率限制、上下文超长、输出额度被拒:非流式重发也必然因为同一原因失败,直接上报。"""
|
||||
from langchain_core.exceptions import ContextOverflowError
|
||||
|
||||
from app.providers.budget import budget_refused
|
||||
|
||||
if isinstance(exc, ContextOverflowError) or budget_refused(exc):
|
||||
return True
|
||||
status = getattr(exc, "status_code", None)
|
||||
if status in (401, 403, 413, 429):
|
||||
return True
|
||||
low = str(exc).lower()
|
||||
return any(k in low for k in (
|
||||
"invalid_model", "model does not exist", "insufficient", "quota",
|
||||
"rate limit", "unauthorized", "invalid api key",
|
||||
))
|
||||
|
||||
|
||||
async def _invoke_streaming(
|
||||
model: Any, messages: list[BaseMessage], ctx: NodeContext, model_id: str = ""
|
||||
) -> AIMessage:
|
||||
"""流式调用并把 token 实时推给前端。
|
||||
|
||||
失败时回退到非流式 —— 有些兼容网关不支持 SSE,不该因此让整个节点挂掉。
|
||||
但回退也失败的话,两次都是同一个原因,不能只把原始异常往上抛:用户看到的
|
||||
会是两段一模一样的供应商报错,而里面既没有模型名也没有下一步该干什么。
|
||||
但鉴权、模型不存在、限流、上下文超长这类请求本身就不成立的报错直接抛出,不再白发第二次请求。
|
||||
"""
|
||||
# 模型 id 要跟着 llm.start 走。以前这条事件只有 message_count,于是调用失败时
|
||||
# 整条轨迹里**没有任何地方**记着试的是哪个模型——排查只能靠翻配置猜。
|
||||
@@ -240,6 +269,8 @@ async def _invoke_streaming(
|
||||
except NotImplementedError:
|
||||
response = await model.ainvoke(messages)
|
||||
except Exception as e: # noqa: BLE001
|
||||
if _non_retriable_stream_error(e):
|
||||
raise NodeError(ctx.node.id, explain_model_error(e, model_id)) from e
|
||||
ctx.emit(EventType.LOG, level="warn",
|
||||
message=f"流式输出失败,已改用非流式调用:{explain_model_error(e, model_id)}",
|
||||
code="stream_fallback")
|
||||
@@ -290,15 +321,24 @@ async def run_llm(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
spent: list[dict[str, Any]] = []
|
||||
|
||||
if schema:
|
||||
# 要结构化结果就走原生 structured output,这条路径拿不到 token 流
|
||||
# 要结构化结果就走原生 structured output,带上 include_raw=True 把真实 token 用量一起拿回来
|
||||
ctx.emit(EventType.LLM_START, model=model_id, structured=True,
|
||||
message_count=len(messages))
|
||||
try:
|
||||
# langchain 靠 "title" 识别裸 JSON Schema dict,缺了会抛 Unsupported function
|
||||
if isinstance(schema, dict) and "title" not in schema:
|
||||
schema = {"title": "output", **schema}
|
||||
structured = model.with_structured_output(schema)
|
||||
value = await structured.ainvoke(messages)
|
||||
try:
|
||||
structured = model.with_structured_output(schema, include_raw=True)
|
||||
except TypeError:
|
||||
structured = model.with_structured_output(schema)
|
||||
got = await structured.ainvoke(messages)
|
||||
if isinstance(got, dict) and got.get("parsing_error"):
|
||||
err = got["parsing_error"]
|
||||
raise err if isinstance(err, BaseException) else ValueError(str(err))
|
||||
raw_msg, value = _split_structured(got)
|
||||
if value is None:
|
||||
raise ValueError("模型返回的结构化结果为空")
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 模型压根不存在也会走到这里,那不是"结构化输出失败"
|
||||
if "invalid_model" in str(e) or "model does not exist" in str(e).lower():
|
||||
@@ -310,8 +350,12 @@ async def run_llm(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
) from e
|
||||
payload = value if isinstance(value, (dict, list)) else getattr(value, "model_dump", lambda: value)()
|
||||
output = {"data": payload, "text": json.dumps(payload, ensure_ascii=False, indent=2)}
|
||||
response: BaseMessage = AIMessage(content=output["text"])
|
||||
spent.append(_report_call(ctx, response, model_id, started))
|
||||
response = AIMessage(
|
||||
content=output["text"],
|
||||
usage_metadata=getattr(raw_msg, "usage_metadata", None),
|
||||
response_metadata=dict(getattr(raw_msg, "response_metadata", None) or {}),
|
||||
)
|
||||
spent.append(_report_call(ctx, raw_msg if raw_msg is not None else response, model_id, started))
|
||||
else:
|
||||
response = await _invoke_streaming(model, messages, ctx, model_id)
|
||||
spent.append(_report_call(ctx, response, model_id, started))
|
||||
|
||||
@@ -107,9 +107,14 @@ class RunManager:
|
||||
# 接下来各处都按 last_seq 接号:本模块的几处另外兜了底,按需裁判记判定(api/evidence.py 的
|
||||
# _append_judged)没有,不改正的话它重启后第一次追加就撞上已有的序号。有 (run_id, seq) 索引,
|
||||
# 每次运行一次索引查找。updated_at 不动:这是补记录,不是运行有了变化
|
||||
from sqlalchemy import exists
|
||||
|
||||
latest = select(func.max(RunEvent.seq)).where(RunEvent.run_id == Run.id).scalar_subquery()
|
||||
lagging = exists(
|
||||
select(1).where(RunEvent.run_id == Run.id, RunEvent.seq > func.coalesce(Run.last_seq, 0))
|
||||
)
|
||||
await session.execute(
|
||||
update(Run).where(func.coalesce(Run.last_seq, 0) < latest)
|
||||
update(Run).where(lagging)
|
||||
.values(last_seq=latest, updated_at=Run.updated_at)
|
||||
.execution_options(synchronize_session=False))
|
||||
await session.commit()
|
||||
@@ -195,6 +200,8 @@ class RunManager:
|
||||
if self._cm is not None:
|
||||
with contextlib.suppress(Exception):
|
||||
await self._cm.__aexit__(None, None, None)
|
||||
self._cm = None
|
||||
self._checkpointer = None
|
||||
|
||||
@property
|
||||
def checkpointer(self) -> BaseCheckpointSaver:
|
||||
|
||||
Reference in New Issue
Block a user