工具审批三档、智能体护栏取代固定 12 步、检查脚本分道并行

MCP / 自定义工具的审批三档(tools/trust.py)
- 平台看不出这两类工具做什么,以前审批关卡一律当它们安全、照跑不误。现在默认每次调用都等审批;
  工具页可以逐个改成「始终允许 · 门控把关」或「始终允许」,审批卡上的「始终允许」等于前者
- 门控把关:每次调用先由门控模型审一遍工具说明、参数和节点任务;判可疑、出错、超时、答不成格式,
  一律交还给人,不会变成放行。判定包进 checkpoint,审批恢复重放时不再问一遍
- agent 节点、工具节点、协作团队三处关卡都接上;团队成员停不下来等人,门控没放行的调用不执行
- 每次运行在发起时快照档位(runs.tool_trust),恢复和接着跑沿用同一份;正式运行不看档位,一律等审批;
  升级前发起的运行照旧不问——它们可能正停在某个审批上,多问一次答复就会对错号
- 接口:GET /api/tools 带 trust / trust_key,PUT /api/tools/trust;设置新增门控模型;新事件 tool.gated
- 界面:工具页三选一、审批卡「始终允许」、设置页门控模型、运行记录解码 tool.gated;
  顺手修了 MCP 工具详情 404、工具审批被驳回却显示成放行

智能体护栏(engine/guards.py),取代固定的「最多 12 步」
- 步数只留兜底:默认 100,设置里可改;以前画布自动写进节点的 12 按「没填」处理
- 同一个工具、同样的参数第二次不再执行,把上次的结果交还给它;连续 3 步拿不到新信息就收尾
- 每个节点的令牌预算(默认 200 万)和金额预算(默认不限),都可以在设置里改成不限
- 上下文用到窗口的 60% 先压缩早期的工具结果,到 85% 就收尾;临近任何一个上限先提醒模型一次
- 下游结构化校验失败时写明是上游哪个 agent 提前收了尾;复核信号带上收尾原因,
  问数据页只在真是步数用满时才给「放宽步数重跑」
- 上限在发起时快照(runs.agent_limits);升级前发起的运行一字不改走旧逻辑

检查脚本
- check-all 默认 4 道并行:量时间最多的两项互不重叠;并行时没过的最后单独重跑一遍,重跑才过的
  单列成「偶发」。16 项全量从约 21 分钟降到约 7 分钟(加上一次重跑约 10 分钟);--lanes 1 回到串行

顺带修的
- 设置接口里的 limits 改为只读:库里存着旧版设置页写进去的一份(max_agent_steps: 25),从不生效,
  却盖住了真实上限,问数据页照着错的数许诺
- test_kb 里嵌入模型那条测试不再真连 api.openai.com(代理慢时一条要等近 4 分钟)
- check-stream 读滚动容器前先等它挂上,并行跑时不再偶发

验证(沙箱):tsc 干净,生产构建通过,pytest 1655 passed(两遍),check-all 16 项检查、3562 项全过;
新增后端测试 53 条,关键逻辑做了 22 处变异验证、全部抓到。
This commit is contained in:
ThearchyHelios
2026-09-28 11:47:45 +08:00
parent e4f2693918
commit 62aac187d8
40 changed files with 2882 additions and 126 deletions
+52 -3
View File
@@ -149,7 +149,26 @@ const TRASH_MORE = {
trash4: conv('c0trash4', '删掉的对话丁:还在跑', 1, 60 * 24 * 5,
{ archived: true, last_status: 'running', last_run_id: 'run-trash-live' }),
}
/** 只在「护栏」那段出现:放宽步数只在步数用满、而且还有得放宽时给(后端 engine/guards.py) */
const GRAPH8 = { ...GRAPH, nodes: GRAPH.nodes.map((n) => (n.type === 'agent'
? { ...n, data: { ...n.data, config: { max_steps: 8 } } } : n)) }
const guardTurn = (id, graph, reason, detail) => turn(id, '查一下各门店的销量', {
answer: '只查了一部分的结论', graph, run_id: `run-${id}`,
review: {
verdict: 'annotated', note: detail, answer: null, retry: true, severity: 'broken',
signals: [{ kind: 'step_limit', detail, severity: 'broken', ...(reason ? { reason } : {}) }],
},
meta: { v: 1, runId: `run-${id}`, runClass: 'exploratory', runStatus: 'succeeded' },
})
const GUARD_CONVS = {
stall: conv('c0gstall', '护栏:连续几步没进展收的尾', 1, 60 * 24 * 30),
steps: conv('c0gsteps', '护栏:写死 8 步、用满了', 1, 60 * 24 * 31),
dflt: conv('c0gdflt', '护栏:跟随默认步数', 1, 60 * 24 * 32),
}
const DETAIL = {
c0gstall: [guardTurn('tg1', GRAPH8, 'stall', 'agent 连续 3 步没有拿到新信息,收尾轮也没有给出结论。')],
c0gsteps: [guardTurn('tg2', GRAPH8, 'steps', 'agent 用满了 8 步。收尾轮也没有给出结论。')],
c0gdflt: [guardTurn('tg3', GRAPH, null, 'agent 用满了 12 步还没给出结论。')],
c0busya: [],
c0idleb: [turn('tb1', 'B 的问题', { answer: 'B 的答案', graph: GRAPH, run_id: 'run-b1' })],
c0longc: Array.from({ length: 6 }, (_, i) =>
@@ -316,6 +335,8 @@ const ctl = {
few: false, run500: true, reviewDelay: {}, launchFail: true, scope400: false,
/** 回收站里多摆两个(TRASH_MORE) */
trashMore: false,
/** 列表里多摆「护栏」那段的三个会话(GUARD_CONVS) */
guardConvs: false,
/** 数据源列表取不回来 */
sourcesFail: false,
/** 数据源列表里多一个别处刚加的库 */
@@ -328,7 +349,8 @@ const resetDb = () => {
db.purged = new Set()
}
resetDb()
const allConvs = () => [...Object.values(CONVS), ...Object.values(TRASHED), ...(ctl.trashMore ? Object.values(TRASH_MORE) : [])]
const allConvs = () => [...Object.values(CONVS), ...Object.values(TRASHED), ...(ctl.trashMore ? Object.values(TRASH_MORE) : []),
...(ctl.guardConvs ? Object.values(GUARD_CONVS) : [])]
.filter((c) => !db.purged.has(c.id))
.map((c) => ({ ...c, archived: db.archived.has(c.id) }))
let runSeq = 0
@@ -621,7 +643,7 @@ async function fakeStream(ws) {
const browser = await chromium.launch({ executablePath: CHROME })
async function open(theme = 'light', { width = 1440, height = 900, reducedMotion = 'no-preference' } = {}) {
async function open(theme = 'light', { width = 1440, height = 900, reducedMotion = 'no-preference', settings = null } = {}) {
const ctx = await browser.newContext({ viewport: { width, height }, colorScheme: theme, reducedMotion })
opened.add(ctx)
await ctx.addInitScript((t) => localStorage.setItem('agentlab.theme', t), theme)
@@ -632,7 +654,8 @@ async function open(theme = 'light', { width = 1440, height = 900, reducedMotion
await page.route('**/api/settings', async (route) => {
if (route.request().method() !== 'GET') return route.abort()
// 主题以设置为准(沙箱里存的是 light),这里按要看的那套改掉
return route.fulfill({ json: { ui: { theme }, run: {}, limits: { max_agent_steps: 25 }, copilot: {}, embedding: {} } })
return route.fulfill({ json: { ui: { theme }, run: {}, limits: { max_agent_steps: 25 }, copilot: {}, embedding: {},
...(settings ?? {}) } })
})
await page.route(/\/api\/(conversations|copilot\/(generate-stream|review)|runs|approvals|datasources)(\/|\?|$)/, fakeApi)
await page.routeWebSocket(/\/api\/runs\/[^/]+\/stream/, fakeStream)
@@ -1573,6 +1596,32 @@ for (const theme of THEMES) {
await ctx.close()
})
await section('guard', '放宽步数只在对症时给:停滞、预算收的尾不给,跟随默认 100 步的也不给', async () => {
resetDb()
ctl.guardConvs = true
try {
const stepBtn = (page) => page.getByRole('button', { name: /放宽步数重跑/ })
// 新后端:默认 100 步、硬上限 100
const fresh = { run: { agent_max_steps: 100 }, limits: { max_agent_steps: 100 } }
const { page, ctx, errors } = await open(theme, { settings: fresh })
await goto(page, 'c0gstall')
await shows(page, '只查了一部分的结论')
check('连续几步没进展收的尾:不给「放宽步数」(加步数只会原样再撞一次)', await stepBtn(page).count() === 0)
await goto(page, 'c0gsteps')
await shows(page, '只查了一部分的结论')
const eight = stepBtn(page)
check('节点写死 8 步、步数用满:给「放宽步数」,从 8 放到 16', await eight.count() === 1
&& /8 → 16 步/.test(await eight.innerText()), await eight.innerText().catch(() => '(没有按钮)'))
await goto(page, 'c0gdflt')
await shows(page, '只查了一部分的结论')
check('节点上的 12 跟随默认 100 步、已经到硬上限:不许一个放不宽的数', await stepBtn(page).count() === 0)
check('没有运行时报错', errors.length === 0, errors[0] ?? '')
await ctx.close()
} finally {
ctl.guardConvs = false
}
})
await section('rerun', '重跑服务重启挂起的那一轮:旧运行顺手取消', async () => {
const { page, ctx, errors } = await open(theme)
await goto(page, 'c0suspo')