fix(roundtable): 业务链条「本阶段目标」串行链可保存/回显 + 新增「总控提示词」
本阶段目标 bug:每步只有一个智能体(无并行)时,stageGroupsToStagePayload/ stageGroupsToStageGoals 一律折叠为 null(旧设计=线性链),导致目标保存即丢、 无从回显。改为「有并行 或 任一 stage 填了目标」就落显式分层,串行带目标链按 分层 DAG 跑(总控逐阶段按目标派活);BusinessChainPicker 同步:有并行/有目标/ 有总控提示都走 dag。 新增「总控提示词」(per-chain,仅 dag 模式生效):写给总控的整体派活指引。 后端 roundtable_chains 加 coordinator_prompt LONGTEXT 列 + migration 20260608_06; 前端编辑器加输入框,注入 buildDagStageLeaderTask 每阶段派活提示。填了总控提示 也强制显式分层(forceExplicit),否则纯线性链里被静默忽略。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
1938c80061
commit
3405795371
13 files changed
+275
-24
No files matched your search
@@ -39,6 +39,12 @@ export interface BusinessChain {
|
||||
* 为 null(线性)时本字段也为 null。某位为 null / 空 = 该阶段不带目标。
|
||||
*/
|
||||
stageGoals?: (string | null)[] | null;
|
||||
/**
|
||||
* 可选「总控编排提示」:用户写给**总控(协调)智能体**的一段指引,告诉总控该怎么派活、
|
||||
* 做什么(整体编排策略 / 风格 / 约束)。**仅 dag 模式生效**,注入总控每阶段派活提示。
|
||||
* 空 / null = 不注入。
|
||||
*/
|
||||
coordinatorPrompt?: string | null;
|
||||
/** 是否已发布为公共(所有账号可在「公共」筛选查看 / 选入研讨)。 */
|
||||
isPublic?: boolean;
|
||||
/** 是否当前用户所有(仅本人可编辑 / 删除 / 发布;公共筛选里别人的链条为 false)。 */
|
||||
|
||||
@@ -64,11 +64,25 @@ test("stageGroupsToSeats flattens in order", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("stageGroupsToStagePayload returns null when all singletons (linear)", () => {
|
||||
test("stageGroupsToStagePayload returns null when all singletons AND no goals (linear)", () => {
|
||||
const groups = grp([seat("a")], [seat("b")]);
|
||||
assert.equal(stageGroupsToStagePayload(groups), null);
|
||||
});
|
||||
|
||||
test("stageGroupsToStagePayload returns explicit stages when serial but a goal is set", () => {
|
||||
// 串行单席位 + 任一「本阶段目标」→ 落显式分层(目标必须挂在显式 stages 上,否则丢失)。
|
||||
const groups = setStageGoal(grp([seat("a")], [seat("b")]), 0, "分析目的");
|
||||
assert.deepEqual(stageGroupsToStagePayload(groups), [["a"], ["b"]]);
|
||||
});
|
||||
|
||||
test("stageGroupsToStagePayload forceExplicit lifts a pure-linear chain to explicit stages", () => {
|
||||
// 无并行无目标,但调用方因「总控编排提示」要求强制显式分层(否则提示在线性链里被忽略)。
|
||||
const groups = grp([seat("a")], [seat("b")]);
|
||||
assert.equal(stageGroupsToStagePayload(groups), null);
|
||||
assert.deepEqual(stageGroupsToStagePayload(groups, true), [["a"], ["b"]]);
|
||||
assert.deepEqual(stageGroupsToStageGoals(groups, true), [null, null]);
|
||||
});
|
||||
|
||||
test("stageGroupsToStagePayload returns groups when any stage is parallel", () => {
|
||||
const groups = grp([seat("a"), seat("b")], [seat("c")]);
|
||||
assert.deepEqual(stageGroupsToStagePayload(groups), [["a", "b"], ["c"]]);
|
||||
@@ -81,12 +95,18 @@ test("stageGroupsToStagePayload ignores empty stages", () => {
|
||||
|
||||
// ── stage goals ─────────────────────────────────────────────────────────────
|
||||
|
||||
test("stageGroupsToStageGoals returns null when linear (no parallel)", () => {
|
||||
const groups = setStageGoal(grp([seat("a")], [seat("b")]), 0, "目标");
|
||||
// 全单席位 = 线性 → 目标也折叠为 null(仅 dag 模式落库)。
|
||||
test("stageGroupsToStageGoals returns null when linear AND no goals", () => {
|
||||
const groups = grp([seat("a")], [seat("b")]);
|
||||
assert.equal(stageGroupsToStageGoals(groups), null);
|
||||
});
|
||||
|
||||
test("stageGroupsToStageGoals persists goals on a serial chain when any goal is set", () => {
|
||||
// 旧行为:串行 → 目标折叠为 null(被丢)。现改为:填了目标的串行链也落库,与显式分层对齐,
|
||||
// 否则保存 / 回显皆失(用户反馈的 bug)。stage 1 无目标 → null。
|
||||
const groups = setStageGoal(grp([seat("a")], [seat("b")]), 0, " 分析目的 ");
|
||||
assert.deepEqual(stageGroupsToStageGoals(groups), ["分析目的", null]);
|
||||
});
|
||||
|
||||
test("stageGroupsToStageGoals aligns to surviving stages, trims, nulls empties", () => {
|
||||
let groups = grp([seat("a"), seat("b")], [seat("c")]);
|
||||
groups = setStageGoal(groups, 0, " 分析目的 ");
|
||||
@@ -182,6 +202,22 @@ test("round-trip: groups → {seats, stages, stageGoals} → chainToStageGroups"
|
||||
assert.equal(restored[1].goal, "依赖上一阶段");
|
||||
});
|
||||
|
||||
// round-trip on a **serial single-seat chain with a goal** (the reported bug):
|
||||
// save must persist stages+goals, and reload must show the goal back on its stage.
|
||||
test("round-trip: serial single-seat chain + goal preserves goal across save/reload", () => {
|
||||
const groups = setStageGoal(grp([seat("a")], [seat("b")]), 0, "作战目的分析");
|
||||
const seats = stageGroupsToSeats(groups);
|
||||
const stages = stageGroupsToStagePayload(groups);
|
||||
const stageGoals = stageGroupsToStageGoals(groups);
|
||||
// 不再折叠为 null —— 串行+目标落显式分层。
|
||||
assert.deepEqual(stages, [["a"], ["b"]]);
|
||||
assert.deepEqual(stageGoals, ["作战目的分析", null]);
|
||||
const restored = chainToStageGroups({ seats, stages, stageGoals });
|
||||
assert.deepEqual(ids(restored), [["a"], ["b"]]);
|
||||
assert.equal(restored[0].goal, "作战目的分析");
|
||||
assert.equal(restored[1].goal, undefined);
|
||||
});
|
||||
|
||||
test("chainToStageGroups: goal drops with a stage whose agents are all gone", () => {
|
||||
// stages[0] 全是 ghost → 整组(含 g0)丢弃;存活的 ["c"] 应拿到 g1,不是 g0。
|
||||
const restored = chainToStageGroups({
|
||||
|
||||
@@ -54,27 +54,40 @@ export function stageGroupsToSeats(groups: StageGroups): ChainSeat[] {
|
||||
return groups.flatMap((g) => g.seats);
|
||||
}
|
||||
|
||||
/** 一组里是否有人填了「本阶段目标」(trim 后非空)。 */
|
||||
function hasAnyGoal(groups: StageGroups): boolean {
|
||||
return groups.some((g) => (g.goal?.trim() ?? "") !== "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 折叠成落库的 `stages`。**全是单元素 stage(无并行)时返回 null** —— 语义上就是线性
|
||||
* 链,落库为 null 以便老行为不变(Step 2 仍走 chain 模式);只要有一个 stage ≥2 席位
|
||||
* 就返回显式分层(Step 2 走 dag 模式)。空 stage 已被忽略。
|
||||
* 折叠成落库的 `stages`。返回 null = 线性链(Step 2 走 chain 模式,老行为不变);返回显式
|
||||
* 分层 = Step 2 走 dag 模式。判定:**只要有一个 stage ≥2 席位(并行),或用户给任一 stage
|
||||
* 填了「本阶段目标」,就落显式分层**;否则(纯单席位且无目标)落 null。空 stage 已被忽略。
|
||||
*
|
||||
* 为什么「填了目标也要落显式分层」:每阶段目标按 stage 下标对齐落库(见
|
||||
* {@link stageGroupsToStageGoals}),只能挂在显式 `stages` 上。若串行单席位链照旧折叠为
|
||||
* null,目标就会被一并丢掉——保存丢、回显也丢(正是用户反馈的「本阶段目标没保存上 / 没回显」)。
|
||||
* 填了目标的串行链因此落成「每席位一层」的显式分层,由总控**逐阶段按目标派活**(dag 模式)。
|
||||
*
|
||||
* `forceExplicit`:调用方还有别的「必须走 dag」的理由时(如填了链条级「总控编排提示」,
|
||||
* 仅 dag 模式注入)传 true,强制落显式分层,避免该提示在纯线性链里被静默忽略。
|
||||
*/
|
||||
export function stageGroupsToStagePayload(groups: StageGroups): string[][] | null {
|
||||
export function stageGroupsToStagePayload(groups: StageGroups, forceExplicit = false): string[][] | null {
|
||||
const nonEmpty = groups.filter((g) => g.seats.length > 0);
|
||||
const hasParallel = nonEmpty.some((g) => g.seats.length > 1);
|
||||
if (!hasParallel) return null;
|
||||
if (!hasParallel && !hasAnyGoal(nonEmpty) && !forceExplicit) return null;
|
||||
return nonEmpty.map((g) => g.seats.map((s) => s.agent_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 折叠成落库的 `stageGoals`,与 {@link stageGroupsToStagePayload} 做**完全相同的折叠**
|
||||
* (过滤空组、无并行返回 null),保证两个数组下标一一对应。每个存活组取其 `goal`
|
||||
* (trim 后为空则该位为 null)。仅 dag 模式(有并行)才落库目标——与「目标仅分层 DAG 生效」一致。
|
||||
* 折叠成落库的 `stageGoals`,与 {@link stageGroupsToStagePayload} 做**完全相同的折叠判定**
|
||||
* (过滤空组;无并行且无目标、且未 `forceExplicit` 时才返回 null),保证两个数组下标一一对应。
|
||||
* 每个存活组取其 `goal`(trim 后为空则该位为 null)。只要落了 `stages`,就同步落目标。
|
||||
*/
|
||||
export function stageGroupsToStageGoals(groups: StageGroups): (string | null)[] | null {
|
||||
export function stageGroupsToStageGoals(groups: StageGroups, forceExplicit = false): (string | null)[] | null {
|
||||
const nonEmpty = groups.filter((g) => g.seats.length > 0);
|
||||
const hasParallel = nonEmpty.some((g) => g.seats.length > 1);
|
||||
if (!hasParallel) return null;
|
||||
if (!hasParallel && !hasAnyGoal(nonEmpty) && !forceExplicit) return null;
|
||||
return nonEmpty.map((g) => g.goal?.trim() || null);
|
||||
}
|
||||
|
||||
|
||||
@@ -207,6 +207,16 @@ test("buildPlanFromStages without goals leaves stage.goal undefined (old plans)"
|
||||
assert.equal(plan.stages[1].goal, undefined);
|
||||
});
|
||||
|
||||
test("buildPlanFromStages stamps trimmed coordinatorPrompt onto the plan", () => {
|
||||
const plan = buildPlanFromStages([["a"], ["b"]], undefined, true, null, " 按阶段派活 ");
|
||||
assert.equal(plan.coordinatorPrompt, "按阶段派活");
|
||||
});
|
||||
|
||||
test("buildPlanFromStages leaves coordinatorPrompt undefined when blank/absent", () => {
|
||||
assert.equal(buildPlanFromStages([["a"], ["b"]]).coordinatorPrompt, undefined);
|
||||
assert.equal(buildPlanFromStages([["a"]], undefined, true, null, " ").coordinatorPrompt, undefined);
|
||||
});
|
||||
|
||||
// ── buildDependencySummary (§3.1 依赖摘要注入) ─────────────────────────────────
|
||||
|
||||
test("buildDependencySummary is empty for no deliveries (first stage)", () => {
|
||||
@@ -472,6 +482,46 @@ test("buildDagStageLeaderTask omits the goal line when goal is empty/blank", ()
|
||||
assert.doesNotMatch(task, /本阶段目标/);
|
||||
});
|
||||
|
||||
// ── coordinator prompt injection (写给总控的编排提示) ─────────────────────────
|
||||
|
||||
test("buildDagStageLeaderTask injects the coordinator prompt as a top-priority guideline", () => {
|
||||
const task = buildDagStageLeaderTask({
|
||||
seedMessage: "x",
|
||||
stageIndex: 0,
|
||||
totalStages: 2,
|
||||
seats: [{ agentId: "a", name: "甲" }],
|
||||
isFirst: true,
|
||||
coordinatorPrompt: "严格按阶段顺序派活,最终做综合收口",
|
||||
});
|
||||
assert.match(task, /总控编排指引/);
|
||||
assert.match(task, /严格按阶段顺序派活,最终做综合收口/);
|
||||
});
|
||||
|
||||
test("buildDagStageLeaderTask injects the coordinator prompt on later stages too", () => {
|
||||
const task = buildDagStageLeaderTask({
|
||||
seedMessage: "x",
|
||||
stageIndex: 1,
|
||||
totalStages: 2,
|
||||
seats: [{ agentId: "b", name: "乙" }],
|
||||
isFirst: false,
|
||||
coordinatorPrompt: "聚焦本职、显式承接",
|
||||
});
|
||||
assert.match(task, /总控编排指引/);
|
||||
assert.match(task, /聚焦本职、显式承接/);
|
||||
});
|
||||
|
||||
test("buildDagStageLeaderTask omits the coordinator block when prompt is empty/blank", () => {
|
||||
const task = buildDagStageLeaderTask({
|
||||
seedMessage: "x",
|
||||
stageIndex: 0,
|
||||
totalStages: 1,
|
||||
seats: [{ agentId: "a", name: "甲" }],
|
||||
isFirst: true,
|
||||
coordinatorPrompt: " ",
|
||||
});
|
||||
assert.doesNotMatch(task, /总控编排指引/);
|
||||
});
|
||||
|
||||
test("pickDagSeatTask injects the stage goal in BOTH the exact-match and the miss branches", () => {
|
||||
const goal = "根据上一阶段的各个目的,分析出行为体";
|
||||
const hit = pickDagSeatTask([["actor", "干这个"]], "actor", "行为体", "种子", true, false, goal);
|
||||
|
||||
@@ -34,6 +34,12 @@ export interface OrchestrationPlan {
|
||||
stages: OrchestrationStage[];
|
||||
/** 收口:是否在所有 stage 完成后由总控综合(默认 true)。 */
|
||||
finalSynthesis: boolean;
|
||||
/**
|
||||
* 可选「总控编排提示」:来自业务链条 `coordinatorPrompt`,写给总控的整体派活指引
|
||||
* (怎么派活 / 做什么)。注入总控**每阶段**派活提示({@link buildDagStageLeaderTask})。
|
||||
* 空 = 不注入。
|
||||
*/
|
||||
coordinatorPrompt?: string;
|
||||
}
|
||||
|
||||
// ── 通用 DAG 表示:自由连线时才需要(§1.2,本期仅预留)───────────────────────
|
||||
@@ -187,12 +193,15 @@ export function emptyPlan(finalSynthesis = true): OrchestrationPlan {
|
||||
* @param finalSynthesis 是否收口综合,默认 true。
|
||||
* @param goals 可选「每阶段目标」,与 `stages` **同源下标**对齐;存活 stage 取对应 goal
|
||||
* (被丢弃的 stage 其 goal 一并丢弃,因为按源下标取)。
|
||||
* @param coordinatorPrompt 可选「总控编排提示」(写给总控的整体派活指引),挂在计划上,
|
||||
* 注入总控每阶段派活提示。空 = 不带。
|
||||
*/
|
||||
export function buildPlanFromStages(
|
||||
stages: string[][],
|
||||
allowedIds?: Iterable<string>,
|
||||
finalSynthesis = true,
|
||||
goals?: ReadonlyArray<string | null> | null,
|
||||
coordinatorPrompt?: string | null,
|
||||
): OrchestrationPlan {
|
||||
const allow = allowedIds ? new Set(allowedIds) : null;
|
||||
const cleaned: OrchestrationStage[] = [];
|
||||
@@ -202,7 +211,10 @@ export function buildPlanFromStages(
|
||||
const goal = goals?.[idx]?.trim() || undefined;
|
||||
cleaned.push(goal ? { id: stageId(idx), agentIds: ids, goal } : { id: stageId(idx), agentIds: ids });
|
||||
});
|
||||
return { mode: "dag", stages: cleaned, finalSynthesis };
|
||||
const cp = coordinatorPrompt?.trim() || undefined;
|
||||
return cp
|
||||
? { mode: "dag", stages: cleaned, finalSynthesis, coordinatorPrompt: cp }
|
||||
: { mode: "dag", stages: cleaned, finalSynthesis };
|
||||
}
|
||||
|
||||
/** 该 stage 是否真并行(≥2 席位)—— Phase 3 决定是否禁写文件 / Phase 5 决定显示。 */
|
||||
@@ -342,14 +354,20 @@ export function buildDagStageLeaderTask(args: {
|
||||
isFirst: boolean;
|
||||
/** 可选「本阶段目标」:用户在业务链条里为本阶段写的产出 / 依赖说明。空 = 不注入。 */
|
||||
goal?: string;
|
||||
/**
|
||||
* 可选「总控编排提示」:用户在业务链条里写给总控的整体派活指引(怎么派活 / 做什么 /
|
||||
* 风格 / 约束)。每个 stage 的派活轮都注入,作为总控的高优先编排原则。空 = 不注入。
|
||||
*/
|
||||
coordinatorPrompt?: string;
|
||||
}): string {
|
||||
const { seedMessage, stageIndex, totalStages, seats, isFirst, goal } = args;
|
||||
const { seedMessage, stageIndex, totalStages, seats, isFirst, goal, coordinatorPrompt } = args;
|
||||
const ordinal = `${stageIndex + 1}/${totalStages}`;
|
||||
const seatList = seats
|
||||
.map((s) => `「${s.name}」(agent_name: ${s.agentId})`)
|
||||
.join("、");
|
||||
const isParallel = seats.length > 1;
|
||||
const goalText = goal?.trim();
|
||||
const coordText = coordinatorPrompt?.trim();
|
||||
const lines: string[] = [];
|
||||
if (isFirst) {
|
||||
lines.push("这是一次【分层 DAG 编排】的多智能体圆桌研讨。任务背景如下:", seedMessage, "");
|
||||
@@ -359,6 +377,13 @@ export function buildDagStageLeaderTask(args: {
|
||||
"",
|
||||
);
|
||||
}
|
||||
if (coordText) {
|
||||
lines.push(
|
||||
"【总控编排指引(用户预设,最高优先)】以下是本次业务链条为你(总控)预设的整体派活原则,请在每一阶段派活时都严格遵循:",
|
||||
coordText,
|
||||
"",
|
||||
);
|
||||
}
|
||||
lines.push(
|
||||
`本研讨严格按预设分层顺序进行,共 ${totalStages} 个阶段,现在是第 ${ordinal} 个阶段。`,
|
||||
`本阶段需要执行的席位(已由编排计划钉死)是:${seatList}。`,
|
||||
|
||||
Reference in new issue
Block a user