mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-15 08:11:59 +00:00
7739340607
- 问数据和个人应用的图放行绑定只读查询等合规工具的调用工具节点,只读和遮罩在这条路径上生效;被治理成写入的工具在只读运行里直接拒;知识库范围在工具层固定(图级默认值、参数名纠错、Agent 都改不了) - 个人应用的刷新和个人蓝图的试运行只有拥有者能读:运行、事件、证据、工件、文件、运行与审批列表、对比、可验证报告、质询、假设分析、提取工作流一并收紧;个人蓝图的试运行不论谁发起都按个人应用约束 - 共享蓝图只给拥有者、对应应用的管理者、管理员,看不到的一律 404;审核副本归审核人;管理员看别人的个人应用只有元数据、可以归档 - 记录表跨版本汇总(只读挂载旧版本、按当前列 UNION ALL、各版本各自固定高水位,缺的版本照实写进印章);停用前发起的运行可以继续写旧版本;更正沿用原提交人、更正人记审计 - 约定收口:待办种类 review、事件 file.extracted、表单错误 file、模板定义带示例与预置;前端类型一次补齐、去掉临时的局部类型;审核待办卡不再给打不开的链接;读附件事件有中文说法;检查脚本补断言
157 lines
5.7 KiB
Python
157 lines
5.7 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from collections import defaultdict
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.db.base import get_session
|
|
from app.db.models import Run, RunEvent, Workflow
|
|
from app.engine.schema import GraphSpec, NodeType
|
|
|
|
router = APIRouter(prefix="/api/governance", tags=["governance"])
|
|
|
|
|
|
@router.get("/tool-usage")
|
|
async def tool_usage(
|
|
workflow_id: str,
|
|
limit_runs: int = Query(default=100, le=500),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> dict[str, Any]:
|
|
"""工具白名单衰减报表。
|
|
|
|
关节层最可能的退化方式不是模板太死,而是白名单为了长尾一个个放宽,
|
|
半年后等于没有白名单。这份报表把"授了权但从来没用过"的工具摆到台面上,
|
|
放宽才能被定期回收。
|
|
"""
|
|
workflow = await session.get(Workflow, workflow_id)
|
|
if not workflow:
|
|
raise HTTPException(404, "工作流不存在,可能已被删除")
|
|
spec = GraphSpec.model_validate(workflow.graph)
|
|
|
|
# 各节点声明的白名单
|
|
granted: dict[str, list[str]] = {}
|
|
for node in spec.nodes:
|
|
if node.type == NodeType.AGENT:
|
|
granted[node.id] = list(node.config.get("tools") or [])
|
|
elif node.type == NodeType.TOOL and node.config.get("tool"):
|
|
granted[node.id] = [node.config["tool"]]
|
|
elif node.type == NodeType.SUPERVISOR:
|
|
merged: list[str] = []
|
|
for agent in node.config.get("agents") or []:
|
|
merged.extend(agent.get("tools") or [])
|
|
granted[node.id] = sorted(set(merged))
|
|
|
|
run_ids = [
|
|
rid
|
|
for (rid,) in await session.execute(
|
|
select(Run.id)
|
|
.where(Run.workflow_id == workflow_id)
|
|
.order_by(Run.created_at.desc())
|
|
.limit(limit_runs)
|
|
)
|
|
]
|
|
usage: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int))
|
|
if run_ids:
|
|
rows = await session.execute(
|
|
select(RunEvent.node_id, RunEvent.data).where(
|
|
RunEvent.run_id.in_(run_ids), RunEvent.type == "tool.start"
|
|
)
|
|
)
|
|
for node_id, data in rows:
|
|
tool = (data or {}).get("tool")
|
|
if node_id and tool:
|
|
# supervisor 内部子 agent 的 node_id 形如 "team:researcher"
|
|
usage[node_id.split(":")[0]][tool] += 1
|
|
|
|
report = []
|
|
for node_id, tools in granted.items():
|
|
used = usage.get(node_id, {})
|
|
report.append(
|
|
{
|
|
"node_id": node_id,
|
|
"granted": tools,
|
|
"used": dict(used),
|
|
"unused": [t for t in tools if not used.get(t)],
|
|
"ungrated_but_used": [t for t in used if t not in tools],
|
|
}
|
|
)
|
|
return {
|
|
"workflow_id": workflow_id,
|
|
"runs_considered": len(run_ids),
|
|
"nodes": report,
|
|
"hint": "授权后多次运行都未使用的工具说明授权范围偏宽,建议从节点中移除",
|
|
}
|
|
|
|
|
|
@router.get("/exploratory-clusters")
|
|
async def exploratory_clusters(
|
|
limit: int = Query(default=200, le=500),
|
|
threshold: float = Query(default=0.62, ge=0.3, le=0.95),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> dict[str, Any]:
|
|
"""探索性问题聚类:同一个问题被反复问,就该长成模板里的固定一节。
|
|
|
|
这就是模板生长的触发器。用本地 embedding 做贪心聚类,
|
|
出现 ≥3 次的簇标记为晋升候选。
|
|
"""
|
|
from sqlalchemy import not_
|
|
|
|
from app.api.deps import current_principal
|
|
from app.auth.access import hidden_runs
|
|
from app.memory.embeddings import cosine, embed_texts
|
|
|
|
rows = list(
|
|
await session.execute(
|
|
select(Run.id, Run.input, Run.workflow_name, Run.created_at)
|
|
# 别人的个人应用刷新、个人蓝图试运行只给拥有者(同运行列表),不进聚类
|
|
.where(Run.run_class != "formal", not_(hidden_runs(current_principal())))
|
|
.order_by(Run.created_at.desc())
|
|
.limit(limit)
|
|
)
|
|
)
|
|
items = []
|
|
for rid, input_payload, wf_name, created in rows:
|
|
text = json.dumps(input_payload or {}, ensure_ascii=False, sort_keys=True)
|
|
if len(text) <= 4: # 空输入没有聚类意义
|
|
continue
|
|
items.append({"run_id": rid, "text": text, "workflow": wf_name,
|
|
"at": created.isoformat() if created else None})
|
|
if not items:
|
|
return {"clusters": [], "considered": 0}
|
|
|
|
vectors = await embed_texts([i["text"] for i in items])
|
|
clusters: list[dict[str, Any]] = []
|
|
for i, item in enumerate(items):
|
|
placed = False
|
|
for cluster in clusters:
|
|
if cosine(vectors[i], vectors[cluster["seed"]]) >= threshold:
|
|
cluster["members"].append(item)
|
|
placed = True
|
|
break
|
|
if not placed:
|
|
clusters.append({"seed": i, "members": [item]})
|
|
|
|
out = [
|
|
{
|
|
"count": len(c["members"]),
|
|
"representative": c["members"][0]["text"][:200],
|
|
"workflow": c["members"][0]["workflow"],
|
|
"run_ids": [m["run_id"] for m in c["members"]][:20],
|
|
"first_at": c["members"][-1]["at"],
|
|
"last_at": c["members"][0]["at"],
|
|
"promote_candidate": len(c["members"]) >= 3,
|
|
}
|
|
for c in clusters
|
|
if len(c["members"]) >= 2
|
|
]
|
|
out.sort(key=lambda c: c["count"], reverse=True)
|
|
return {
|
|
"clusters": out,
|
|
"considered": len(items),
|
|
"hint": "被标记为可固化的问题反复出现,建议用「提取模板」把它固定为工作流中的一步",
|
|
}
|