协作团队真的能并行了,界面上也看得出来

先说一个和名字不符的事实:这个节点叫「多 Agent 协作」,但在此之前它是一条
严格的串行流水线。调度协议写死成 `next: string`——"一次只能派一个"被编进了
协议本身,模型再想并行也表达不出来。三个专家各花 10 秒,总是 30 秒。

(真并行一直在图级 fan-out:实测 3 个各 sleep 2 秒的节点连在同一个上游,
全部 +0.00s 开始、+2.37s 结束,墙钟 2.6 秒。只是协作团队走不到那条路。)

**协议改成 assignments 数组**,一轮可以派多个互不依赖的专家,asyncio.gather
并发执行,总耗时按最慢的那个算。max_parallel 可配,设成 1 即退回严格串行。

**并发只在任务真的互不依赖时才成立**,这件事的成本必须说清楚:同一轮发出去的
专家拿到的是**同一份进展快照**,谁也看不见谁这一轮干了什么。派错了不报错,
只会让两个人基于一样的旧信息重复劳动,而这在结果里很难看出来。所以判断责任
压在调度者身上,提示词里把反例写出来:

    可以同时派:「查 A 的资料」和「查 B 的资料」
    不能同时派:「查资料」和「根据资料写报告」
    不能同时派:「写初稿」和「校对初稿」

并明确告诉它拿不准就分两轮——代价只是慢一点。

**每个人单独计时**,而不是事后拿整轮墙钟去摊。界面要显示"并行省下多少",
那个数必须是量出来的:按 N×墙钟 推算等于假设每个人都跑满了最慢那条,
而实际上快的那个可能只花了三分之一。测试里专门有个快慢不对称的用例守这条。

**调度日志改成结构化字段**(agents / done / reason / parallel)。以前只有一句
中文消息串,解码层得拿正则去拆「调度 → X(理由)」——后端文案一改就散架,
而这次正是改了文案。老运行的事件没有这些字段,正则那条路留着兜底。

**界面加泳道。** 每个成员一行、每轮一格,条形宽度按耗时归一化,所以一眼看得出
谁和谁是同时干的、谁是这一轮的瓶颈。顶上一行汇总"3 名成员 · 3 轮 · 2 轮并行,
省下 11.8s";点任意一格展开那个人这一轮的任务和回复。全程串行时不装作有并行,
而是说"本次任务互相依赖,逐轮进行"。

泳道排在子步骤**前面**:先给一眼看清的形状,再往下是每一步的内容。反过来的话,
得把十几行步骤读完才知道这个团队是怎么分工的。窄栏(画布右栏 360px)下隐去
"N 人同时"的文字——并行与否看条形本来就一目了然,而缩写成"并2"没人看得懂。

验证:后端 276 passed(新增 test_supervisor.py 7 条,核心那条用**墙钟**判定
并发而不是看事件顺序——顺序只能说明"发出去了",说明不了"同时跑");
check-stream 新增 10 条泳道断言,含"同一轮里慢的那个条更长"和两种栏宽不溢出;
tsc 干净,三个 check 不回归。
This commit is contained in:
ThearchyHelios
2026-09-20 20:57:03 +08:00
parent 4d237c3b08
commit c3f7d6ffcd
7 changed files with 773 additions and 37 deletions
+119 -6
View File
@@ -53,6 +53,44 @@ export interface Step {
* 两者要同时展示(问什么 + 查到什么),合成一个字段就只能二选一 */
result?: string
children?: Step[]
/** 协作团队的泳道数据。只有 supervisor 节点的顶层 Step 会有 */
team?: TeamRun
}
/**
* 协作团队一轮里的一个人。
*
* ms 是**各自量出来**的耗时,不是拿整轮墙钟摊的——界面要显示"并行省了多少",
* 那个数按 N×墙钟 推算会把快的那个也记成最慢那条,省下的时间就被夸大了。
*/
export interface TeamMember {
agent: string
instruction: string
ms: number
status: StepStatus
result?: string
}
export interface TeamRound {
round: number
/** 这一轮同时派了几个人。1 就是串行的一步 */
parallel: number
/** 这一轮实际花的时间:并发时是最慢那个 */
wallMs: number
/** 各人耗时之和:并发时它大于 wallMs,差值就是省下的 */
sumMs: number
reason?: string
members: TeamMember[]
}
export interface TeamRun {
/** 花名册,按第一次出现的顺序——泳道的行顺序 */
members: string[]
rounds: TeamRound[]
/** 并行一共省下多少毫秒。全程串行则为 0 */
savedMs: number
/** 协作是不是已经收尾了 */
finished: boolean
}
// 这两个是流式增量,后端根本不落库(_EPHEMERAL)。单次运行的 delta 量级会
@@ -297,6 +335,28 @@ export function decodeRun(events: RunEvent[]): Step[] {
const pendingLlm = new Map<string, Step>()
/** supervisor 里待配对的单个 agent 步骤 */
const pendingAgents = new Map<string, Step>()
/** node_id → 协作团队的泳道数据。边解码边攒,最后挂到该节点的顶层 Step 上 */
const teams = new Map<string, TeamRun>()
const teamOf = (nodeId: string | undefined): TeamRun | null => {
if (!nodeId) return null
let t = teams.get(nodeId)
if (!t) {
t = { members: [], rounds: [], savedMs: 0, finished: false }
teams.set(nodeId, t)
}
return t
}
const roundOf = (team: TeamRun, round: number): TeamRound => {
let r = team.rounds.find((x) => x.round === round)
if (!r) {
r = { round, parallel: 1, wallMs: 0, sumMs: 0, members: [] }
team.rounds.push(r)
team.rounds.sort((a, b) => a.round - b.round)
}
return r
}
/**
* node_id → 这个节点当前那条尚未闭合的审批步骤。
*
@@ -626,6 +686,18 @@ export function decodeRun(events: RunEvent[]): Step[] {
}
pendingAgents.set(agentKey(nodeId, d), step)
push(step, nodeId)
const team = teamOf(nodeId)
if (team) {
const name = String(d.agent ?? '')
if (name && !team.members.includes(name)) team.members.push(name)
const r = roundOf(team, num(d.round) ?? 0)
r.parallel = Math.max(r.parallel, num(d.parallel) ?? 1)
r.members.push({
agent: name, instruction: String(d.instruction ?? ''),
ms: 0, status: 'running',
})
}
break
}
@@ -645,6 +717,23 @@ export function decodeRun(events: RunEvent[]): Step[] {
detail: preview,
}, nodeId)
}
const team = teamOf(nodeId)
if (team) {
const r = roundOf(team, num(d.round) ?? 0)
const name = String(d.agent ?? '')
const ms = num(d.duration_ms) ?? 0
const m = r.members.find((x) => x.agent === name && x.status === 'running')
?? (r.members.push({ agent: name, instruction: '', ms: 0, status: 'running' }),
r.members[r.members.length - 1])
m.ms = ms
m.status = 'done'
m.result = preview || undefined
// 这一轮实际花的是最慢那个,各人之和减去它就是省下的
r.wallMs = Math.max(...r.members.map((x) => x.ms))
r.sumMs = r.members.reduce((acc, x) => acc + x.ms, 0)
team.savedMs = team.rounds.reduce((acc, x) => acc + Math.max(0, x.sumMs - x.wallMs), 0)
}
break
}
@@ -656,17 +745,33 @@ export function decodeRun(events: RunEvent[]): Step[] {
// 就是它,和普通 info 日志区分得开
if (level === 'info' && d.round != null) {
const text = String(d.message ?? '')
const round = (num(d.round) ?? 0) + 1
// 优先读结构化字段。以前只有一句中文消息串,这里得拿正则去拆
// 「调度 → X(理由)」——后端文案一改就散架,而文案是会改的。
// 老运行的事件没有这些字段,正则那条路留着兜底
const structured = Array.isArray(d.agents)
const m = text.match(/^调度\s*→\s*([^((]+)[((](.*)[))]\s*$/)
const legacyTarget = m?.[1].trim() ?? ''
const agents: string[] = structured
? (d.agents as unknown[]).map(String)
: (legacyTarget && legacyTarget !== 'FINISH' ? [legacyTarget] : [])
const done = structured ? !!d.done : legacyTarget === 'FINISH'
const reason = structured ? String(d.reason ?? '') : (m?.[2].trim() ?? '')
const team = teamOf(nodeId)
if (team) {
if (done) team.finished = true
else if (reason) roundOf(team, num(d.round) ?? 0).reason = reason
}
// FINISH 是协议里的收尾标记,不是某个 agent。"交给 FINISH"
// 会让人以为还有个叫 FINISH 的成员
const target = m?.[1].trim() ?? ''
const round = Number(d.round) + 1
const title = done ? `第 ${round} 轮:结束协作`
: agents.length > 1 ? `第 ${round} 轮:${agents.join('、')} 同时进行`
: agents.length === 1 ? `第 ${round} 轮:交给 ${agents[0]}`
: text
push({
id: `sv-${seq}`, seq, kind: 'branch', nodeId, status: 'done',
title: !m ? text
: target === 'FINISH' ? `第 ${round} 轮:结束协作`
: `第 ${round} 轮:交给 ${target}`,
detail: m ? m[2].trim() : undefined,
title, detail: reason || undefined,
}, nodeId)
break
}
@@ -778,6 +883,14 @@ export function decodeRun(events: RunEvent[]): Step[] {
}
// 还没收到 end 的工具/模型调用保持 running——它们正在进行,不是丢了
// 泳道挂到 supervisor 节点的顶层 Step 上。节点内部那些"第N轮交给谁""X:指令"
// 仍然留着——泳道给的是一眼看清的形状,那些步骤给的是内容,两者不重复
for (const [nodeId, team] of teams) {
if (!team.rounds.length) continue
const step = nodeSteps.get(nodeId)
if (step) step.team = team
}
return out
}