mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-18 15:21:58 +00:00
审批只认明确的"同意",而且每个会执行危险操作的地方都得过这道门
三个口子,都实测复现过:
1. 回复"拒绝",工具照样执行。四个节点四套审批解析:工具节点和代码节点对
回复一律 bool(),"拒绝"、"no" 都是真;{"decision": "approve"} 反被当成
拒绝;{"approved": "false"} 也是批准。现在收成一份 engine/approval.py:
只认明确的批准,看不懂的一律按拒绝。填内容的 human input/edit 模式另算——
裸字符串是交稿,只有结构体里明说"不"才驳回。
2. 可写数据源上的 DELETE 从来不问人。数据源工具是动态生成的,查不到
ToolSpec,任何审批模式下都被当成安全;is_dangerous_call 写好了但全仓库
没人调用。现在危险与否逐次判定:registry.call_is_dangerous 统一问,数据源
工具把判定挂在 metadata["dangerous_if"] 上——同一个 db_query,查询直接
跑,可写源上的写要先确认。工具节点、agent 节点、协作成员都走它。
这一步以前没出事,只因为写入从不提交:run_query 走 stream,拿不到结果集就
抛 ResourceClosedError,连接关闭时再回滚——审批通过的写也没落过库。审批接
好之后才让写真正提交(先后不能反,反过来就是一个无审批写库的窗口);
RETURNING 这类带结果集的写照样返回行。工具页的"试一下"是人自己在执行,
不设审批。
3. 协作团队的成员调工具完全不经审批。成员们并行跑在一个节点里、停不下来等
人(恢复时节点整个重放,几个人的 interrupt 对不上号),所以需要确认的调用
不执行,把原因交还给成员让它换做法;节点上显式设成"全部自动放行"才跑。
前端补了这个选项。顺带修了同一处的两个老问题:成员步数不受 max_agent_steps
约束;步数用完时把工具的原始返回当结论交上去(agent 节点在 4057cdc 修过
同一个问题,这里没跟上)——现在补一轮不带工具的收尾。
连带修的:多条审批并存时,交给引擎的是库里存的那份回复,而它存之前被 _safe
截到了 2000 字,解包时又只留 value——人工改过的长稿被砍一截,带了内容的驳回
被当成交稿。现在存全文、只拆自己包的那层 {"value": x}。
test_approval.py 36 例;去掉修复重跑,引擎层 10 例全部失败,各对应一个真问题。
并行驳回那例翻出了一个运行卡死的老 bug,已在上一个提交单独修掉。
338 → 374 passed,连跑三次全绿。
This commit is contained in:
@@ -345,6 +345,9 @@ Excel / CSV 若传入知识库,只能被切块检索,数字即成为模型
|
||||
`EXPLAIN ANALYZE`、`PRAGMA … =`、`set_config()` 等)按写操作处理
|
||||
- 只读数据源在**连接层**也只读,不只靠守卫猜:SQLite 以 `mode=ro` 打开,PostgreSQL / MySQL
|
||||
会话事务只读。**Oracle 没有会话级只读开关**,只剩守卫一道——只读数据源请配只读账号
|
||||
- 人工审批只认明确的批准(`true` / 同意 / approve),看不懂的回复一律按拒绝处理。
|
||||
危不危险逐次判定:同一个 `db_query__<源>`,查询直接执行,可写源上的写操作要先确认,
|
||||
确认后才提交。协作团队的成员停不下来等人,需要确认的调用不执行,会告诉成员换一种做法
|
||||
- API Key 经 Fernet 加密落库,仅向前端返回掩码
|
||||
|
||||
---
|
||||
|
||||
@@ -14,7 +14,7 @@ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
|
||||
from sqlalchemy import text
|
||||
|
||||
from app.core.crypto import decrypt
|
||||
from app.data.guard import QueryLimits, SqlRejected, check
|
||||
from app.data.guard import QueryLimits, SqlRejected, check, is_write
|
||||
|
||||
# 各方言的 SQLAlchemy 驱动。都选 asyncio 原生驱动,没有一个需要装数据库客户端:
|
||||
# oracledb 走 thin 模式(纯 Python 协议实现),省掉了 Oracle Instant Client 这个
|
||||
@@ -222,6 +222,9 @@ async def run_query(
|
||||
engine = await engines.get(source)
|
||||
started = time.perf_counter()
|
||||
|
||||
if not source.readonly and is_write(statement):
|
||||
return await _run_write(engine, statement, limits, started)
|
||||
|
||||
async def _exec() -> tuple[list[str], list[list[Any]], bool]:
|
||||
async with engine.connect() as conn:
|
||||
cursor = await conn.stream(text(statement))
|
||||
@@ -258,6 +261,40 @@ async def run_query(
|
||||
)
|
||||
|
||||
|
||||
async def _run_write(
|
||||
engine: AsyncEngine, statement: str, limits: QueryLimits, started: float
|
||||
) -> QueryResult:
|
||||
"""可写源上的写操作:真的提交。
|
||||
|
||||
以前写和查询走同一条 stream 路径:拿不到结果集就抛 ResourceClosedError,
|
||||
连接关闭时再回滚——可写源上的写从来没落过库,调用方只看到一个报错。
|
||||
能走到这里的写都已经过了审批(registry.call_is_dangerous → 各节点的审批关卡),
|
||||
只读源上的写在 check 里就被拒了。
|
||||
"""
|
||||
import time
|
||||
|
||||
async def _exec() -> tuple[list[str], list[list[Any]]]:
|
||||
async with engine.begin() as conn: # 正常退出提交,出错回滚
|
||||
result = await conn.execute(text(statement))
|
||||
if result.returns_rows: # RETURNING、PG 的数据修改 CTE
|
||||
return (list(result.keys()),
|
||||
[[_jsonable(v) for v in row] for row in result.fetchmany(limits.max_rows)])
|
||||
return ["affected_rows"], [[result.rowcount]]
|
||||
|
||||
try:
|
||||
columns, rows = await asyncio.wait_for(_exec(), timeout=limits.timeout_seconds)
|
||||
except asyncio.TimeoutError as e:
|
||||
raise SqlRejected(f"写操作超过 {limits.timeout_seconds}s 被中断,已回滚。") from e
|
||||
return QueryResult(
|
||||
columns=columns,
|
||||
rows=rows,
|
||||
row_count=len(rows),
|
||||
truncated=len(rows) >= limits.max_rows,
|
||||
elapsed_ms=int((time.perf_counter() - started) * 1000),
|
||||
sql=statement,
|
||||
)
|
||||
|
||||
|
||||
def _jsonable(value: Any) -> Any:
|
||||
"""把驱动返回的类型转成能进 JSON 的形态。
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
"""人工审批的回复怎么读。全引擎只此一份。
|
||||
|
||||
以前有四份:human 节点、agent 节点、工具节点、代码节点各读各的,口径不一。
|
||||
后果不是风格问题:工具节点和代码节点对字符串一律 bool(),回复"拒绝"、"no"
|
||||
都被当成批准,工具真的执行了;而 {"decision": "approve"} 反被判成拒绝。
|
||||
|
||||
规则只有一条:**只认明确的批准,看不懂的一律按拒绝处理。** 审批这道门放错了
|
||||
的代价(危险工具被执行)远大于拦错了的代价(再批一次)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
_YES = frozenset({
|
||||
"approve", "approved", "yes", "y", "true", "ok", "allow",
|
||||
"同意", "通过", "批准", "允许", "可以",
|
||||
})
|
||||
_NO = frozenset({
|
||||
"reject", "rejected", "deny", "denied", "no", "n", "false",
|
||||
"拒绝", "驳回", "不同意", "不通过", "不允许", "不可以",
|
||||
})
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Decision:
|
||||
#: 明确批准了。审批类的关卡(工具、代码、human 的 approve 模式)只认这个。
|
||||
approved: bool
|
||||
#: 明确拒绝了。填内容类的关卡(human 的 input / edit 模式)只在这时才停:
|
||||
#: 那两种模式的回复本身就是内容,没说"不"就是交了稿。
|
||||
rejected: bool
|
||||
note: str = ""
|
||||
#: 人工改过的工具参数
|
||||
args: dict[str, Any] | None = None
|
||||
#: 人工改过的代码
|
||||
code: str | None = None
|
||||
#: human 节点收到的内容(input / edit 模式)
|
||||
value: Any = None
|
||||
|
||||
|
||||
def _word(value: Any) -> str:
|
||||
return value.strip().lower() if isinstance(value, str) else ""
|
||||
|
||||
|
||||
def _verdict(value: Any) -> bool | None:
|
||||
"""True / False / None(没表态)。"""
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
word = _word(value)
|
||||
if word in _YES:
|
||||
return True
|
||||
if word in _NO:
|
||||
return False
|
||||
return None
|
||||
|
||||
|
||||
def read_decision(response: Any) -> Decision:
|
||||
if isinstance(response, dict):
|
||||
verdict = _verdict(response["approved"]) if "approved" in response \
|
||||
else _verdict(response.get("decision"))
|
||||
args = response.get("args")
|
||||
code = response.get("code")
|
||||
return Decision(
|
||||
approved=verdict is True,
|
||||
rejected=verdict is False,
|
||||
note=str(response.get("note") or response.get("comment") or ""),
|
||||
args=args if isinstance(args, dict) else None,
|
||||
code=code if isinstance(code, str) and code else None,
|
||||
value=response.get("value"),
|
||||
)
|
||||
verdict = _verdict(response)
|
||||
# 不是一个是/否词的字符串("拒绝,这个表是生产库")既算不上批准,本身又
|
||||
# 是理由——留作备注,agent 转告模型时才不会只剩一句"未说明"
|
||||
note = response.strip() if isinstance(response, str) and verdict is None else ""
|
||||
return Decision(
|
||||
approved=verdict is True,
|
||||
rejected=verdict is False,
|
||||
note=note,
|
||||
# 字符串回复本身就是内容:input 模式下用户可能直接回一句话
|
||||
value=response,
|
||||
)
|
||||
@@ -7,6 +7,7 @@ from langgraph.types import interrupt
|
||||
|
||||
from app.core.events import EventType
|
||||
from app.db.base import SessionLocal
|
||||
from app.engine.approval import read_decision
|
||||
from app.engine.context import NodeContext, NodeError
|
||||
from app.engine.state import GraphState, message_text, template_context
|
||||
from app.providers.factory import ModelSpec, get_chat_model
|
||||
@@ -34,12 +35,13 @@ async def run_human(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
ctx.emit(EventType.HUMAN_REQUESTED, **payload)
|
||||
response = interrupt(payload)
|
||||
ctx.emit(EventType.HUMAN_RESOLVED, response=response)
|
||||
decision = read_decision(response)
|
||||
|
||||
if mode == "approve":
|
||||
approved = _truthy(response)
|
||||
approved = decision.approved
|
||||
result: dict[str, Any] = {
|
||||
"approved": approved,
|
||||
"note": _note_of(response),
|
||||
"note": decision.note,
|
||||
"__decision__": "approved" if approved else "rejected",
|
||||
}
|
||||
if not approved and ctx.cfg("stop_on_reject", False):
|
||||
@@ -52,12 +54,13 @@ async def run_human(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
# 这两种模式在图上只有一个 out 出口(没有 rejected 那条边),所以驳回
|
||||
# 的正确语义是终止运行,而不是当作通过继续。缺省仍是通过:只传 value
|
||||
# 不带 approved 的老调用方行为不变。
|
||||
rejected = isinstance(response, dict) and response.get("approved") is False
|
||||
if rejected:
|
||||
note = _note_of(response)
|
||||
raise NodeError(ctx.node.id, f"人工驳回:{note or '未说明原因'}")
|
||||
value = response.get("value") if isinstance(response, dict) else response
|
||||
result = {"value": value, "note": _note_of(response), "__decision__": "approved"}
|
||||
#
|
||||
# 只有结构体里明说了"不"才算驳回:这两种模式下裸字符串就是内容本身,
|
||||
# 用户在输入框里回一句"no"是在交稿,不是在驳回。
|
||||
if isinstance(response, dict) and decision.rejected:
|
||||
raise NodeError(ctx.node.id, f"人工驳回:{decision.note or '未说明原因'}")
|
||||
note = decision.note if isinstance(response, dict) else "" # 裸字符串是内容,不是备注
|
||||
result = {"value": decision.value, "note": note, "__decision__": "approved"}
|
||||
|
||||
updates: dict[str, Any] = {"nodes": {ctx.node.id: result}}
|
||||
var_name = ctx.cfg("assign_to", "")
|
||||
@@ -66,24 +69,6 @@ async def run_human(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
return updates
|
||||
|
||||
|
||||
def _truthy(value: Any) -> bool:
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
if isinstance(value, dict):
|
||||
if "approved" in value:
|
||||
return bool(value["approved"])
|
||||
return str(value.get("decision", "")).lower() in ("approve", "yes", "true", "同意")
|
||||
if isinstance(value, str):
|
||||
return value.strip().lower() in ("yes", "y", "true", "approve", "ok", "同意", "通过")
|
||||
return bool(value)
|
||||
|
||||
|
||||
def _note_of(value: Any) -> str:
|
||||
if isinstance(value, dict):
|
||||
return str(value.get("note", "") or value.get("comment", ""))
|
||||
return ""
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 结构化校验
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
@@ -14,6 +14,7 @@ from app.core.config import settings
|
||||
from app.core.events import EventType
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Skill
|
||||
from app.engine.approval import read_decision
|
||||
from app.engine.context import NodeContext, NodeError
|
||||
from app.engine.state import GraphState, message_text, template_context, thinking_text
|
||||
from app.providers import catalog
|
||||
@@ -28,8 +29,8 @@ from app.tools.registry import (
|
||||
ToolContext,
|
||||
args_model_of,
|
||||
build_tools,
|
||||
call_is_dangerous,
|
||||
describe_args,
|
||||
get_spec,
|
||||
prepare_args,
|
||||
)
|
||||
|
||||
@@ -312,14 +313,13 @@ def split_tool_calls(
|
||||
return tool_calls[:1], deferred
|
||||
|
||||
|
||||
def _needs_approval(ctx: NodeContext, tool_name: str) -> bool:
|
||||
def _needs_approval(ctx: NodeContext, tool: BaseTool, args: dict[str, Any]) -> bool:
|
||||
mode = ctx.cfg("approval", "dangerous") # never | dangerous | always
|
||||
if mode == "always":
|
||||
return True
|
||||
if mode == "never":
|
||||
return False
|
||||
spec = get_spec(tool_name)
|
||||
return bool(spec and spec.dangerous)
|
||||
return call_is_dangerous(tool, tool.name, args)
|
||||
|
||||
|
||||
async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
@@ -410,7 +410,7 @@ async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
ctx.emit(EventType.LOG, level="warn",
|
||||
message=f"工具 {name}:{fix_note}", code="tool_args_fixed")
|
||||
|
||||
if _needs_approval(ctx, name):
|
||||
if _needs_approval(ctx, tool_map[name], args):
|
||||
ctx.emit(
|
||||
EventType.HUMAN_REQUESTED,
|
||||
mode="approve",
|
||||
@@ -427,9 +427,10 @@ async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
"title": f"是否允许调用 {name}?",
|
||||
}
|
||||
)
|
||||
approved, note, override = _parse_decision(decision)
|
||||
ctx.emit(EventType.HUMAN_RESOLVED, tool=name, approved=approved, note=note)
|
||||
if not approved:
|
||||
verdict = read_decision(decision)
|
||||
note = verdict.note
|
||||
ctx.emit(EventType.HUMAN_RESOLVED, tool=name, approved=verdict.approved, note=note)
|
||||
if not verdict.approved:
|
||||
messages.append(
|
||||
ToolMessage(
|
||||
content=f"用户拒绝了这次调用。原因:{note or '未说明'}。请换一种方式。",
|
||||
@@ -438,8 +439,8 @@ async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
)
|
||||
transcript.append({"tool": name, "args": args, "denied": True, "note": note})
|
||||
continue
|
||||
if isinstance(override, dict) and override:
|
||||
args = override
|
||||
if verdict.args:
|
||||
args = verdict.args
|
||||
|
||||
ctx.emit(EventType.TOOL_START, tool=name, args=args, call_id=call_id)
|
||||
started = time.perf_counter()
|
||||
@@ -555,17 +556,3 @@ async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
if var_name:
|
||||
updates["vars"] = {var_name: final_text}
|
||||
return updates
|
||||
|
||||
|
||||
def _parse_decision(decision: Any) -> tuple[bool, str, dict[str, Any] | None]:
|
||||
"""人工审批的返回值兼容几种写法:布尔、字符串、或带备注的结构体。"""
|
||||
if isinstance(decision, bool):
|
||||
return decision, "", None
|
||||
if isinstance(decision, str):
|
||||
return decision.lower() in ("yes", "y", "true", "approve", "ok", "同意"), decision, None
|
||||
if isinstance(decision, dict):
|
||||
approved = decision.get("approved")
|
||||
if approved is None:
|
||||
approved = decision.get("decision") in ("approve", "yes", True)
|
||||
return bool(approved), str(decision.get("note", "")), decision.get("args")
|
||||
return bool(decision), "", None
|
||||
|
||||
@@ -21,6 +21,7 @@ from app.tools.registry import (
|
||||
ToolContext,
|
||||
args_model_of,
|
||||
build_tools,
|
||||
call_is_dangerous,
|
||||
prepare_args,
|
||||
)
|
||||
|
||||
@@ -97,6 +98,8 @@ async def run_supervisor(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
#: 一轮最多同时派几个。再多的话调度者多半只是在把任务拆碎,
|
||||
#: 而每多一路就多一份"基于陈旧进展"的风险
|
||||
max_parallel = max(1, min(int(ctx.cfg("max_parallel", 3) or 3), len(names)))
|
||||
#: dangerous:需要人工确认的调用不执行(成员停不下来等人);never:全部放行
|
||||
approval = ctx.cfg("approval", "dangerous")
|
||||
|
||||
transcript: list[dict[str, Any]] = []
|
||||
usage_total = {"input_tokens": 0, "output_tokens": 0, "cost_usd": 0.0, "calls": 0}
|
||||
@@ -168,7 +171,9 @@ async def run_supervisor(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
HumanMessage(content=f"团队目标:{goal}\n\n已有进展:\n{progress or '(无)'}\n\n你的任务:{instruction}"),
|
||||
]
|
||||
calls: list[dict[str, Any]] = []
|
||||
for _ in range(int(cfg.get("max_steps") or 4)):
|
||||
# 和 agent 节点同一个硬顶:成员上配多少步都过不去 max_agent_steps
|
||||
max_steps = min(int(cfg.get("max_steps") or 4), settings.max_agent_steps)
|
||||
for _ in range(max_steps):
|
||||
reply = await bound.ainvoke(messages)
|
||||
_accumulate(reply, model_id)
|
||||
messages.append(reply)
|
||||
@@ -199,6 +204,18 @@ async def run_supervisor(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
if args_error:
|
||||
# 参数就不对,没必要真调一次。把"它接受什么"喂回去让它改
|
||||
content = args_error
|
||||
elif approval != "never" and call_is_dangerous(tool_map[tname], tname, targs):
|
||||
# 专家们并行跑在同一个节点里,停不下来等人:审批恢复时节点整个
|
||||
# 重放,几个人的 interrupt 谁先谁后对不上号。以前的做法是不审,
|
||||
# shell_exec / file_write / 可写库上的 DELETE 照跑不误——
|
||||
# agent 节点守着的那道门,在这里是敞开的。现在是不跑,并说清原因
|
||||
content = (
|
||||
f"没有执行:{tname} 这次调用需要人工确认,而协作团队里的成员"
|
||||
"不能停下来等人。换一种不需要它的做法;实在需要,交给团队外"
|
||||
"的 agent 节点去做(那里可以逐次审批)。"
|
||||
)
|
||||
ctx.emit(EventType.LOG, level="warn", code="tool_needs_approval",
|
||||
message=f"{name} 想调用 {tname},需要人工确认,协作节点里不执行")
|
||||
else:
|
||||
try:
|
||||
raw = await tool_map[tname].ainvoke(targs)
|
||||
@@ -208,7 +225,27 @@ async def run_supervisor(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
ctx.emit(EventType.TOOL_END, tool=tname, agent=name, call_id=cid, preview=content[:1500])
|
||||
calls.append({"tool": tname, "args": targs, "result": content[:2000]})
|
||||
messages.append(ToolMessage(content=content, tool_call_id=cid))
|
||||
return {"text": message_text(messages[-1]), "tool_calls": calls}
|
||||
|
||||
# 步数用完了:最后一步一定是要了工具的,模型从没拿到过"说结论"的那一轮。
|
||||
# messages[-1] 是一条 ToolMessage——以前它就被当成这个成员的产出交给调度者,
|
||||
# 工具的原始返回冒充了结论。agent 节点修过同一个问题,这里照做:不给工具
|
||||
# (用没绑工具的 model,结构上就发不出调用)补一轮收尾。
|
||||
messages.append(HumanMessage(content=(
|
||||
f"步数预算用完了({max_steps} 步),现在起不能再调用任何工具。基于已经查到的"
|
||||
"信息给出你这部分的结论,说清哪些没查到、结论因此有什么局限。不要编造数据。"
|
||||
)))
|
||||
try:
|
||||
settled = await model.ainvoke(messages)
|
||||
_accumulate(settled, model_id)
|
||||
text = message_text(settled).strip()
|
||||
except Exception as e: # noqa: BLE001 - 收尾失败不能把已有的过程一起赔进去
|
||||
ctx.emit(EventType.LOG, level="warn", code="settle_failed",
|
||||
message=f"{name} 的收尾轮没跑成:{type(e).__name__}: {e}")
|
||||
text = next((t for m in reversed(messages)
|
||||
if isinstance(m, AIMessage) and (t := message_text(m).strip())), "")
|
||||
ctx.emit(EventType.LOG, level="warn", code="step_limit_settled",
|
||||
message=f"{name} 用满了 {max_steps} 步,结论基于已经查到的部分")
|
||||
return {"text": text or f"({name} 用满了 {max_steps} 步,没有给出结论)", "tool_calls": calls}
|
||||
|
||||
progress_lines: list[str] = []
|
||||
final_text = ""
|
||||
|
||||
@@ -8,11 +8,19 @@ from langgraph.types import interrupt
|
||||
|
||||
from app.core.events import EventType
|
||||
from app.db.base import SessionLocal
|
||||
from app.engine.approval import read_decision
|
||||
from app.engine.context import NodeContext, NodeError
|
||||
from app.engine.state import GraphState
|
||||
from app.sandbox.base import SandboxLimits
|
||||
from app.sandbox.manager import sandbox_manager
|
||||
from app.tools.registry import ToolArgsError, ToolContext, call_tool, get_spec
|
||||
from app.tools.registry import (
|
||||
ToolArgsError,
|
||||
ToolContext,
|
||||
build_tools,
|
||||
call_is_dangerous,
|
||||
call_tool,
|
||||
get_spec,
|
||||
)
|
||||
|
||||
|
||||
def _tool_ctx(ctx: NodeContext) -> ToolContext:
|
||||
@@ -25,6 +33,18 @@ def _tool_ctx(ctx: NodeContext) -> ToolContext:
|
||||
)
|
||||
|
||||
|
||||
async def _is_dangerous(name: str, args: dict[str, Any], ctx: NodeContext) -> bool:
|
||||
"""内置工具看 ToolSpec;数据源这类动态工具得先建出来,才问得到这一次危不危险。
|
||||
|
||||
审批通过后节点会整个重放,这里会再算一遍——所以它必须只取决于 name 和 args。
|
||||
"""
|
||||
if get_spec(name) is not None:
|
||||
return call_is_dangerous(None, name, args)
|
||||
async with SessionLocal() as session:
|
||||
tools = await build_tools([name], _tool_ctx(ctx), session=session)
|
||||
return call_is_dangerous(tools[0] if tools else None, name, args)
|
||||
|
||||
|
||||
async def run_tool(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
"""直接调用一个工具。参数里的 {{ }} 会先用当前状态渲染。"""
|
||||
name = ctx.cfg("tool", "")
|
||||
@@ -35,23 +55,21 @@ async def run_tool(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
if not isinstance(args, dict):
|
||||
raise NodeError(ctx.node.id, "工具参数必须是对象")
|
||||
|
||||
spec = get_spec(name)
|
||||
approval = ctx.cfg("approval", "dangerous")
|
||||
if approval == "always" or (approval == "dangerous" and spec and spec.dangerous):
|
||||
if approval == "always" or (approval == "dangerous" and await _is_dangerous(name, args, ctx)):
|
||||
ctx.emit(EventType.HUMAN_REQUESTED, mode="approve", tool=name, args=args,
|
||||
title=f"是否允许调用 {name}?")
|
||||
decision = interrupt(
|
||||
decision = read_decision(interrupt(
|
||||
{"kind": "tool_approval", "node_id": ctx.node.id, "tool": name, "args": args,
|
||||
"title": f"是否允许调用 {name}?"}
|
||||
)
|
||||
approved = decision if isinstance(decision, bool) else bool(
|
||||
(decision or {}).get("approved") if isinstance(decision, dict) else decision
|
||||
)
|
||||
if isinstance(decision, dict) and isinstance(decision.get("args"), dict):
|
||||
args = decision["args"]
|
||||
ctx.emit(EventType.HUMAN_RESOLVED, tool=name, approved=approved)
|
||||
if not approved:
|
||||
raise NodeError(ctx.node.id, f"用户拒绝执行工具 {name}")
|
||||
))
|
||||
if decision.args is not None:
|
||||
args = decision.args
|
||||
ctx.emit(EventType.HUMAN_RESOLVED, tool=name, approved=decision.approved,
|
||||
note=decision.note)
|
||||
if not decision.approved:
|
||||
raise NodeError(ctx.node.id, f"用户拒绝执行工具 {name}"
|
||||
+ (f":{decision.note}" if decision.note else ""))
|
||||
|
||||
ctx.emit(EventType.TOOL_START, tool=name, args=args)
|
||||
started = time.perf_counter()
|
||||
@@ -118,18 +136,16 @@ async def run_code(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
if ctx.cfg("approval", "never") == "always":
|
||||
ctx.emit(EventType.HUMAN_REQUESTED, mode="approve", title="是否执行这段代码?",
|
||||
code=code[:4000], language=language)
|
||||
decision = interrupt(
|
||||
decision = read_decision(interrupt(
|
||||
{"kind": "code_approval", "node_id": ctx.node.id, "code": code,
|
||||
"language": language, "title": "是否执行这段代码?"}
|
||||
)
|
||||
approved = decision if isinstance(decision, bool) else bool(
|
||||
(decision or {}).get("approved") if isinstance(decision, dict) else decision
|
||||
)
|
||||
if isinstance(decision, dict) and decision.get("code"):
|
||||
code = decision["code"] # 允许人工改完再跑
|
||||
ctx.emit(EventType.HUMAN_RESOLVED, approved=approved)
|
||||
if not approved:
|
||||
raise NodeError(ctx.node.id, "用户拒绝执行代码")
|
||||
))
|
||||
if decision.code:
|
||||
code = decision.code # 允许人工改完再跑
|
||||
ctx.emit(EventType.HUMAN_RESOLVED, approved=decision.approved, note=decision.note)
|
||||
if not decision.approved:
|
||||
raise NodeError(ctx.node.id, "用户拒绝执行代码"
|
||||
+ (f":{decision.note}" if decision.note else ""))
|
||||
|
||||
# 隔离档位:strict 要硬件级(microVM),fast 要低延迟(Seatbelt/bwrap),
|
||||
# 留空跟随整机默认。要的那档不可用时会安静退回默认,不阻断运行。
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
@@ -245,9 +246,10 @@ class RunManager:
|
||||
now = datetime.now(timezone.utc)
|
||||
if target is not None:
|
||||
target.status = "answered" # 已回复,但还没提交给引擎
|
||||
target.response = (
|
||||
_safe(response) if isinstance(response, dict) else {"value": _safe(response)}
|
||||
)
|
||||
# 存全文,不能用 _safe 截:多条审批并存时,交给引擎的就是这一份
|
||||
# (见下面 answers)。截过的版本会把人工改过的长稿砍掉一截再送进节点
|
||||
full = json.loads(json.dumps(response, ensure_ascii=False, default=str))
|
||||
target.response = full if isinstance(full, dict) else {"value": full}
|
||||
target.resolved_at = now
|
||||
if actor:
|
||||
target.resolved_by = actor
|
||||
@@ -271,7 +273,11 @@ class RunManager:
|
||||
missing.append(iid)
|
||||
continue
|
||||
payload = rec.response or {}
|
||||
answers[iid] = payload.get("value", payload) if isinstance(payload, dict) else payload
|
||||
# 只拆我们自己包的那层 {"value": x}(裸值回复)。带 approved / note / args
|
||||
# 的结构体要原样交给节点——以前一律取 value,只要回复里带了内容,
|
||||
# 驳回、备注、改过的参数就全在这里丢了
|
||||
wrapped = isinstance(payload, dict) and set(payload) == {"value"}
|
||||
answers[iid] = payload["value"] if wrapped else payload
|
||||
|
||||
if missing:
|
||||
# 还差人没回。保持 interrupted,把已回复的那条留在 answered 上等齐
|
||||
|
||||
@@ -154,6 +154,8 @@ def _make_query_tool(source: DataSource, ctx: ToolContext) -> StructuredTool:
|
||||
args_schema=_QueryArgs,
|
||||
coroutine=_run,
|
||||
func=None,
|
||||
# 同一个工具,危不危险看这一次的 SQL。审批关卡经 registry.call_is_dangerous 问它
|
||||
metadata={"dangerous_if": lambda args: is_dangerous_call(source, str(args.get("sql") or ""))},
|
||||
)
|
||||
|
||||
|
||||
@@ -187,5 +189,8 @@ def _make_schema_tool(source: DataSource) -> StructuredTool:
|
||||
|
||||
|
||||
def is_dangerous_call(source: DataSource, sql: str) -> bool:
|
||||
"""这次调用要不要走人工审批:非只读源上的写操作要。"""
|
||||
"""这次调用要不要走人工审批:非只读源上的写操作要。
|
||||
|
||||
只读源上的写不需要审批——守卫和连接层都会拒,批了也写不进去。
|
||||
"""
|
||||
return not source.readonly and is_write(sql)
|
||||
|
||||
@@ -77,6 +77,21 @@ def get_spec(name: str) -> ToolSpec | None:
|
||||
return all_specs().get(name)
|
||||
|
||||
|
||||
def call_is_dangerous(tool: BaseTool | None, name: str, args: dict[str, Any]) -> bool:
|
||||
"""这一次调用要不要人工确认。所有审批关卡都问这一个函数。
|
||||
|
||||
内置工具危不危险是固定的(ToolSpec.dangerous)。动态工具要看这一次传了
|
||||
什么:同一个 db_query__<源>,SELECT 不用确认,可写源上的 DELETE 必须确认。
|
||||
它们把判定挂在工具的 metadata["dangerous_if"] 上。以前没有这一层,数据源
|
||||
工具查不到 ToolSpec,于是在任何审批模式下都被当成安全的。
|
||||
"""
|
||||
spec = get_spec(name)
|
||||
if spec is not None:
|
||||
return spec.dangerous
|
||||
judge = ((tool.metadata or {}) if tool is not None else {}).get("dangerous_if")
|
||||
return bool(judge and judge(args))
|
||||
|
||||
|
||||
def _stringify(value: Any) -> str:
|
||||
"""工具结果最终是喂给模型的,所以统一成字符串;结构化数据转紧凑 JSON。"""
|
||||
if isinstance(value, str):
|
||||
|
||||
@@ -0,0 +1,396 @@
|
||||
"""审批这道门:只认明确的批准,而且每一个会执行危险操作的地方都得过它。
|
||||
|
||||
以前的三个口子(都实测过):
|
||||
1. 工具节点和代码节点对回复一律 bool()——回"拒绝"、"no",工具照样执行;
|
||||
{"decision": "approve"} 反被当成拒绝。四个节点四套解析,口径各不相同。
|
||||
2. 数据源工具查不到 ToolSpec,于是可写库上的 DELETE 在任何审批模式下都不问人
|
||||
(is_dangerous_call 写好了,全仓库没人调用)。只是因为写入从不提交,才没出事。
|
||||
3. 协作团队的成员调工具完全不经审批。
|
||||
|
||||
另外两处连带的:多条审批并存时,存进库的回复被截断、解包时只留 value——
|
||||
改过的长稿被砍掉一截,驳回被当成交稿。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import sqlite3
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from langchain_core.messages import AIMessage, ToolMessage
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Approval, DataSource, Run, RunEvent
|
||||
from app.engine.approval import read_decision
|
||||
from app.engine.runner import run_manager
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def engine_up():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
|
||||
|
||||
def node(nid, ntype, **config):
|
||||
return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0},
|
||||
"data": {"label": nid, "config": config}}
|
||||
|
||||
|
||||
def chain(*nodes):
|
||||
return {
|
||||
"nodes": list(nodes),
|
||||
"edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])],
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run_id)
|
||||
raise AssertionError(f"等超时了,run {run_id} 还是 {row.status if row else '不存在'}")
|
||||
|
||||
|
||||
async def _start(graph) -> str:
|
||||
run = await run_manager.start(graph=graph, input_payload={"question": "随便"})
|
||||
return run.id
|
||||
|
||||
|
||||
async def _events(run_id: str) -> list[RunEvent]:
|
||||
async with SessionLocal() as session:
|
||||
return list((await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run_id).order_by(RunEvent.seq)
|
||||
)).scalars())
|
||||
|
||||
|
||||
async def _approvals(run_id: str) -> dict[str, Approval]:
|
||||
async with SessionLocal() as session:
|
||||
rows = (await session.execute(
|
||||
select(Approval).where(Approval.run_id == run_id, Approval.status == "pending")
|
||||
)).scalars()
|
||||
return {a.node_id: a for a in rows}
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 解析口径
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.parametrize("reply", [
|
||||
"拒绝", "no", "deny", "不同意", "拒绝,这是生产库", "", None, 1, [],
|
||||
{"note": "看不懂这个回复"},
|
||||
{"approved": "false"}, # bool("false") 是 True——以前就这么被批了
|
||||
{"approved": "no"},
|
||||
{"decision": "reject"},
|
||||
])
|
||||
def test_anything_short_of_a_clear_yes_is_not_approval(reply):
|
||||
assert read_decision(reply).approved is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("reply", [
|
||||
True, "yes", "同意", "approve", " OK ",
|
||||
{"approved": True}, {"approved": "true"},
|
||||
{"decision": "approve"}, # 以前在工具节点里被当成拒绝
|
||||
{"decision": "批准"},
|
||||
])
|
||||
def test_clear_approvals(reply):
|
||||
assert read_decision(reply).approved is True
|
||||
|
||||
|
||||
def test_rejection_is_only_what_was_said_out_loud():
|
||||
"""填内容的关卡只在明说"不"时才停:没表态的回复是交稿。"""
|
||||
assert read_decision({"approved": False}).rejected
|
||||
assert read_decision({"decision": "驳回"}).rejected
|
||||
assert not read_decision({"value": "改好的稿子"}).rejected
|
||||
assert not read_decision("随便写点什么").rejected
|
||||
|
||||
|
||||
def test_note_and_overrides_come_through():
|
||||
d = read_decision({"approved": True, "note": "只这一次", "args": {"sql": "SELECT 1"},
|
||||
"code": "print(1)"})
|
||||
assert (d.note, d.args, d.code) == ("只这一次", {"sql": "SELECT 1"}, "print(1)")
|
||||
# 一句话的拒绝本身就是理由,agent 转告模型时不能只剩"未说明"
|
||||
assert read_decision("拒绝,这是生产库").note == "拒绝,这是生产库"
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 工具节点
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _calc_graph():
|
||||
return chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("t", "tool", tool="calculator", args={"expression": "1+1"}, approval="always"),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.t.result }}"}]),
|
||||
)
|
||||
|
||||
|
||||
async def test_tool_node_takes_a_refusal_as_a_refusal():
|
||||
run_id = await _start(_calc_graph())
|
||||
await _wait(run_id, ("interrupted",))
|
||||
await run_manager.resume(run_id, "拒绝")
|
||||
run = await _wait(run_id, ("failed", "succeeded"))
|
||||
|
||||
assert run.status == "failed" and "拒绝" in (run.error or "")
|
||||
assert not [e for e in await _events(run_id) if e.type == "tool.start"], "被拒绝的工具执行了"
|
||||
|
||||
|
||||
async def test_tool_node_takes_decision_approve_as_approval():
|
||||
run_id = await _start(_calc_graph())
|
||||
await _wait(run_id, ("interrupted",))
|
||||
await run_manager.resume(run_id, {"decision": "approve"})
|
||||
run = await _wait(run_id, ("failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error
|
||||
assert run.output["结果"] == "2"
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 可写数据源:写要先审批,批了就真的写
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def writable(tmp_path):
|
||||
path = tmp_path / "rw.db"
|
||||
conn = sqlite3.connect(path)
|
||||
conn.execute("CREATE TABLE t (x INTEGER)")
|
||||
conn.executemany("INSERT INTO t VALUES (?)", [(i,) for i in range(5)])
|
||||
conn.commit()
|
||||
conn.close()
|
||||
name = f"rw_{uuid.uuid4().hex[:8]}"
|
||||
async with SessionLocal() as session:
|
||||
session.add(DataSource(name=name, kind="sqlite", database=str(path), readonly=False,
|
||||
description="可写测试库", options={}, schema_cache={}, enabled=True))
|
||||
await session.commit()
|
||||
|
||||
def count() -> int:
|
||||
c = sqlite3.connect(path)
|
||||
try:
|
||||
return c.execute("SELECT COUNT(*) FROM t").fetchone()[0]
|
||||
finally:
|
||||
c.close()
|
||||
|
||||
return f"db_query__{name}", count
|
||||
|
||||
|
||||
def _query_graph(tool: str, sql: str):
|
||||
return chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("q", "tool", tool=tool, args={"sql": sql}), # approval 缺省 = dangerous
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.q }}"}]),
|
||||
)
|
||||
|
||||
|
||||
async def test_write_on_a_writable_source_waits_for_approval_then_really_writes(writable):
|
||||
tool, count = writable
|
||||
run_id = await _start(_query_graph(tool, "DELETE FROM t WHERE x = 0"))
|
||||
run = await _wait(run_id, ("interrupted", "failed", "succeeded"))
|
||||
assert run.status == "interrupted", f"可写库上的 DELETE 没有等审批:{run.status} {run.error}"
|
||||
assert count() == 5
|
||||
|
||||
await run_manager.resume(run_id, {"approved": True})
|
||||
run = await _wait(run_id, ("failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error
|
||||
assert count() == 4, "批准了,但写没有落库"
|
||||
|
||||
|
||||
async def test_a_refused_write_leaves_the_data_alone(writable):
|
||||
tool, count = writable
|
||||
run_id = await _start(_query_graph(tool, "WITH k AS (SELECT 1) DELETE FROM t"))
|
||||
await _wait(run_id, ("interrupted",))
|
||||
await run_manager.resume(run_id, "拒绝")
|
||||
run = await _wait(run_id, ("failed", "succeeded"))
|
||||
assert run.status == "failed"
|
||||
assert count() == 5
|
||||
|
||||
|
||||
async def test_reads_on_a_writable_source_do_not_ask(writable):
|
||||
tool, count = writable
|
||||
run_id = await _start(_query_graph(tool, "SELECT COUNT(*) AS n FROM t"))
|
||||
run = await _wait(run_id, ("interrupted", "failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# agent 节点:同一个工具,危不危险看这一次的 SQL
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _script_agent(monkeypatch, tool: str, sql: str) -> None:
|
||||
from app.providers import mock_model
|
||||
|
||||
def _decide(self, messages):
|
||||
if self.response_format:
|
||||
return AIMessage(content="{}")
|
||||
if self.tools and not any(isinstance(m, ToolMessage) for m in messages):
|
||||
return AIMessage(content="动手。", tool_calls=[
|
||||
{"name": tool, "args": {"sql": sql}, "id": "call_w"}])
|
||||
return AIMessage(content="好了。")
|
||||
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "_decide", _decide)
|
||||
|
||||
|
||||
def _agent_graph(tool: str):
|
||||
return chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("bot", "agent", prompt="处理一下", tools=[tool], max_steps=3),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.bot.text }}"}]),
|
||||
)
|
||||
|
||||
|
||||
async def test_agent_asks_before_writing_to_a_writable_source(writable, monkeypatch):
|
||||
tool, count = writable
|
||||
_script_agent(monkeypatch, tool, "DELETE FROM t WHERE x = 0")
|
||||
run_id = await _start(_agent_graph(tool))
|
||||
run = await _wait(run_id, ("interrupted", "failed", "succeeded"))
|
||||
assert run.status == "interrupted", f"agent 没等审批就写了:{run.status}"
|
||||
|
||||
await run_manager.resume(run_id, "拒绝")
|
||||
run = await _wait(run_id, ("failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error # 拒绝喂回给模型,它换个说法收口
|
||||
assert count() == 5
|
||||
|
||||
|
||||
async def test_agent_reads_without_asking(writable, monkeypatch):
|
||||
tool, count = writable
|
||||
_script_agent(monkeypatch, tool, "SELECT COUNT(*) FROM t")
|
||||
run_id = await _start(_agent_graph(tool))
|
||||
run = await _wait(run_id, ("interrupted", "failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 协作团队:成员停不下来等人,所以要确认的调用不执行
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _script_team(monkeypatch, *, member_call: dict | None, settle_text: str = "收尾结论") -> None:
|
||||
from app.providers import mock_model
|
||||
|
||||
turns = {"route": 0}
|
||||
|
||||
def _decide(self, messages):
|
||||
if self.response_format: # 调度者:第一轮派 dba,第二轮收工
|
||||
turns["route"] += 1
|
||||
plan = ({"assignments": [{"agent": "dba", "instruction": "清理测试数据"}]}
|
||||
if turns["route"] == 1 else {"assignments": [], "done": True})
|
||||
return AIMessage(content=json.dumps(plan, ensure_ascii=False))
|
||||
if not self.tools: # 没绑工具 = 步数用完后的收尾轮
|
||||
return AIMessage(content=settle_text)
|
||||
if member_call and not any(isinstance(m, ToolMessage) for m in messages):
|
||||
return AIMessage(content="动手。", tool_calls=[{**member_call, "id": "call_m"}])
|
||||
return AIMessage(content="清理完了。")
|
||||
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "_decide", _decide)
|
||||
|
||||
|
||||
def _team_graph(tool: str, *, approval: str | None = None, max_steps: int = 3):
|
||||
cfg = {"goal": "清理", "max_rounds": 3,
|
||||
"agents": [{"name": "dba", "system": "你是 DBA", "tools": [tool], "max_steps": max_steps}]}
|
||||
if approval:
|
||||
cfg["approval"] = approval
|
||||
return chain(
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("team", "supervisor", **cfg),
|
||||
node("out", "output", fields=[]),
|
||||
)
|
||||
|
||||
|
||||
async def test_team_member_does_not_run_a_call_that_needs_approval(writable, monkeypatch):
|
||||
tool, count = writable
|
||||
_script_team(monkeypatch, member_call={"name": tool, "args": {"sql": "DELETE FROM t"}})
|
||||
run_id = await _start(_team_graph(tool))
|
||||
run = await _wait(run_id, ("interrupted", "failed", "succeeded"))
|
||||
|
||||
assert run.status == "succeeded", run.error
|
||||
assert count() == 5, "协作成员绕过审批写了库"
|
||||
codes = [e.data.get("code") for e in await _events(run_id) if e.type == "log"]
|
||||
assert "tool_needs_approval" in codes
|
||||
|
||||
|
||||
async def test_team_with_approval_never_runs_it(writable, monkeypatch):
|
||||
"""放行是显式的选择:节点上设成 never 才执行。"""
|
||||
tool, count = writable
|
||||
_script_team(monkeypatch, member_call={"name": tool, "args": {"sql": "DELETE FROM t"}})
|
||||
run_id = await _start(_team_graph(tool, approval="never"))
|
||||
run = await _wait(run_id, ("interrupted", "failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error
|
||||
assert count() == 0
|
||||
|
||||
|
||||
async def test_team_member_out_of_steps_settles_instead_of_handing_in_a_tool_result(
|
||||
writable, monkeypatch,
|
||||
):
|
||||
"""步数用完时交上去的得是结论,不能是工具的原始返回。"""
|
||||
tool, _ = writable
|
||||
_script_team(monkeypatch, member_call={"name": tool, "args": {"sql": "SELECT 1 AS one"}})
|
||||
run_id = await _start(_team_graph(tool, max_steps=1))
|
||||
run = await _wait(run_id, ("interrupted", "failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error
|
||||
|
||||
events = await _events(run_id)
|
||||
ends = [e.data for e in events if e.type == "agent.step.end"]
|
||||
assert ends and ends[0]["preview"] == "收尾结论", ends
|
||||
assert "step_limit_settled" in [e.data.get("code") for e in events if e.type == "log"]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 多条审批并存
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _two_editors():
|
||||
return {
|
||||
"nodes": [
|
||||
node("start", "input", fields=[{"name": "question"}]),
|
||||
node("h1", "human", mode="edit", title="改稿一", draft="草稿一"),
|
||||
node("h2", "human", mode="edit", title="改稿二", draft="草稿二"),
|
||||
node("out", "output", fields=[
|
||||
{"name": "一", "value": "{{ nodes.h1.value }}"},
|
||||
{"name": "二", "value": "{{ nodes.h2.value }}"},
|
||||
]),
|
||||
],
|
||||
"edges": [
|
||||
{"source": "start", "target": "h1"}, {"source": "start", "target": "h2"},
|
||||
{"source": "h1", "target": "out"}, {"source": "h2", "target": "out"},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
async def test_parallel_edits_arrive_in_full():
|
||||
"""两条审批并存时,交给引擎的是库里存的那份——以前存之前先截到 2000 字。"""
|
||||
run_id = await _start(_two_editors())
|
||||
await _wait(run_id, ("interrupted",))
|
||||
pending = await _approvals(run_id)
|
||||
assert set(pending) == {"h1", "h2"}
|
||||
|
||||
await run_manager.resume(run_id, {"approved": True, "value": "甲" * 5000},
|
||||
approval_id=pending["h1"].id)
|
||||
await run_manager.resume(run_id, {"approved": True, "value": "乙" * 5000},
|
||||
approval_id=pending["h2"].id)
|
||||
run = await _wait(run_id, ("failed", "succeeded"))
|
||||
assert run.status == "succeeded", run.error
|
||||
assert len(run.output["一"]) == 5000 and len(run.output["二"]) == 5000
|
||||
|
||||
|
||||
async def test_parallel_rejection_with_content_is_still_a_rejection():
|
||||
"""回复里带了 value,以前解包时只留 value——驳回就这么被当成了交稿。"""
|
||||
run_id = await _start(_two_editors())
|
||||
await _wait(run_id, ("interrupted",))
|
||||
pending = await _approvals(run_id)
|
||||
|
||||
await run_manager.resume(run_id, {"approved": True, "value": "甲"},
|
||||
approval_id=pending["h1"].id)
|
||||
await run_manager.resume(run_id, {"approved": False, "note": "不行", "value": "乙"},
|
||||
approval_id=pending["h2"].id)
|
||||
run = await _wait(run_id, ("failed", "succeeded"))
|
||||
assert run.status == "failed" and "驳回" in (run.error or ""), run.error
|
||||
@@ -167,10 +167,19 @@ export const NODE_DEFS: Record<NodeType, NodeDef> = {
|
||||
+ '设为 1 即退回严格串行——并发的成员看到的是同一份进展快照,'
|
||||
+ '任务其实有依赖却被同时派出去,两个人会基于一样的旧信息重复劳动',
|
||||
},
|
||||
{
|
||||
key: 'approval', label: '工具审批', type: 'select', advanced: true,
|
||||
options: [
|
||||
{ value: 'dangerous', label: '需要确认的调用不执行' },
|
||||
{ value: 'never', label: '全部自动放行' },
|
||||
],
|
||||
help: '成员们并行跑在一个节点里,停不下来等人审批。默认把危险工具和可写库上的写操作'
|
||||
+ '挡下来,并告诉成员换一种做法;需要逐次审批的事交给团队外的 agent 节点',
|
||||
},
|
||||
...MODEL_FIELDS,
|
||||
...COMMON_TAIL,
|
||||
],
|
||||
defaults: { max_rounds: 6, max_parallel: 3, agents: [] },
|
||||
defaults: { max_rounds: 6, max_parallel: 3, agents: [], approval: 'dangerous' },
|
||||
},
|
||||
tool: {
|
||||
type: 'tool', label: '调用工具', category: '执行', icon: Wrench,
|
||||
|
||||
Reference in New Issue
Block a user