Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(maker-shared): 计划归属边界——跨轮不串号、子代理不顶主计划

Kafeifei9088f1d15a50
diff --git a/apps/mobile/src/__tests__/messagePerformance.test.ts b/apps/mobile/src/__tests__/messagePerformance.test.ts
index 45046dfaa5..d1284060ba 100644
--- a/apps/mobile/src/__tests__/messagePerformance.test.ts
+++ b/apps/mobile/src/__tests__/messagePerformance.test.ts
@@ -98,25 +98,28 @@ describe('message render performance', () => {
expect(rawMessages).toHaveLength(1000);
expect(durationMs).toBeLessThan(1500);
// 桌面共享实现把 transcript 中的 plan/todo 卡拆成顶层独立项,较旧的「折叠进 work_group」多出 1 项。
- expect(items).toHaveLength(601);
+ // 计划所有权边界后,20 个隔着 user turn 的未完成 TodoWrite 不再被串成一张卡
+ // (那是历史串号病),而是每个 turn 各自一张:601 + 19 = 620。
+ expect(items).toHaveLength(620);
+ // turn 0 是 todo turn:所有权边界后它的清单卡锚在本 turn(不再被后续 turn
+ // 的更新拖到 transcript 尾部合并),紧跟在 work_group 之后。
expect(items.slice(0, 6).map((item) => item.type)).toEqual([
'message',
'work_group',
+ 'todo',
'message',
'message',
'work_group',
- 'message',
]);
const keys = items.map((item) => item.key);
expect(new Set(keys).size).toBe(keys.length);
- expect(keys.slice(0, 6)).toEqual([
+ expect(keys.slice(0, 5)).toEqual([
'message-user-0',
'work-thinking-0',
+ 'todo-tool-0',
'message-assistant-0',
'message-user-1',
- 'work-thinking-1',
- 'message-assistant-1',
]);
});
diff --git a/packages/maker-shared/src/__tests__/messageRender.test.ts b/packages/maker-shared/src/__tests__/messageRender.test.ts
index 49a51333a4..74263a6796 100644
--- a/packages/maker-shared/src/__tests__/messageRender.test.ts
+++ b/packages/maker-shared/src/__tests__/messageRender.test.ts
@@ -926,6 +926,114 @@ describe('message render todo grouping', () => {
tool('plan2', 'update_plan', { plan: [{ step: 'Ship it', status: 'completed' }] }),
]),
).not.toHaveProperty('turnFailed');
+
+ /**
+ * 计划归属(ownership)。三个历史病根,都源于"同 source 的上一份计划没勾完
+ * 就无条件当续期":
+ * 1. 跨普通 user turn 串号——用户换了话题,新计划仍复用旧 session/key;
+ * 2. 子代理的计划工具调用与主线程平权,子计划能顶掉主计划;
+ * 3. Codex 不同 turn 的 update_plan 被并成同一 session。
+ */
+ describe('plan ownership boundaries', () => {
+ it('starts a new session when an ordinary user turn intervenes', () => {
+ const staleTodo = tool('todo-old', 'TodoWrite', {
+ todos: [
+ { content: 'Old work', status: 'in_progress' },
+ { content: 'Old follow-up', status: 'pending' },
+ ],
+ });
+ const newUserTurn: MessageRenderSourceMessageLike = {
+ role: 'user',
+ clientId: 'user-2',
+ content: '换个话题:帮我做另一件事',
+ createdAt: at(5),
+ };
+ const freshTodo = tool('todo-new', 'TodoWrite', {
+ todos: [{ content: 'New work', status: 'in_progress' }, { content: 'New follow-up', status: 'pending' }],
+ });
+
+ const latest = findLatestMessageTodoInsertion([staleTodo, newUserTurn, freshTodo]);
+ // 新 user turn 之后的计划是新 session:key 必须锚在新调用上,不复用旧 key。
+ expect(latest).toMatchObject({
+ key: 'todo-todo-new',
+ todos: [
+ { content: 'New work', status: 'in_progress' },
+ { content: 'New follow-up', status: 'pending' },
+ ],
+ });
+ });
+
+ it('keeps in-turn progress updates merged into one session (no user turn between)', () => {
+ const first = tool('todo-a', 'TodoWrite', {
+ todos: [{ content: 'Step', status: 'in_progress' }],
+ });
+ const second = tool('todo-b', 'TodoWrite', {
+ todos: [{ content: 'Step', status: 'completed' }],
+ });
+
+ // 同一 turn 内的进度更新仍是续期:key 锚定首次调用(现状契约,不能破坏)。
+ expect(findLatestMessageTodoInsertion([first, second])).toMatchObject({
+ key: 'todo-todo-a',
+ todos: [{ content: 'Step', status: 'completed' }],
+ });
+ });
+
+ it('ignores subagent plan calls for the top-level pinned panel', () => {
+ const mainPlan = tool('plan-main', 'update_plan', {
+ plan: [
+ { step: 'Main step', status: 'in_progress' },
+ { step: 'Main follow-up', status: 'pending' },
+ ],
+ });
+ const subagentTodo: MessageRenderSourceMessageLike = {
+ ...tool('todo-sub', 'TodoWrite', {
+ todos: [
+ { content: 'Subagent internal', status: 'in_progress' },
+ { content: 'Subagent extra', status: 'pending' },
+ ],
+ }),
+ parentToolUseId: 'agent-task-1',
+ };
+
+ // 子代理自己的清单不得顶掉主线程计划。
+ expect(findLatestMessageTodoInsertion([mainPlan, subagentTodo])).toMatchObject({
+ key: 'todo-plan-main',
+ todos: [
+ { content: 'Main step', status: 'in_progress' },
+ { content: 'Main follow-up', status: 'pending' },
+ ],
+ });
+ });
+
+ it('does not merge Codex plans from different turns into one session', () => {
+ const turn1Plan = tool('plan-t1', 'update_plan', {
+ plan: [
+ { step: 'Turn one work', status: 'in_progress' },
+ { step: 'Turn one rest', status: 'pending' },
+ ],
+ }, 'plan:turn-1');
+ const newUserTurn: MessageRenderSourceMessageLike = {
+ role: 'user',
+ clientId: 'user-3',
+ content: '下一个任务',
+ createdAt: at(6),
+ };
+ const turn2Plan = tool('plan-t2', 'update_plan', {
+ plan: [
+ { step: 'Turn two work', status: 'in_progress' },
+ { step: 'Turn two rest', status: 'pending' },
+ ],
+ }, 'plan:turn-2');
+
+ const latest = findLatestMessageTodoInsertion([turn1Plan, newUserTurn, turn2Plan]);
+ expect(latest).toMatchObject({
+ key: 'todo-plan-t2',
+ todos: [
+ { content: 'Turn two work', status: 'in_progress' },
+ { content: 'Turn two rest', status: 'pending' },
+ ],
+ });
+ });
});
it('does not infer completion from an ambiguous legacy Codex turn seal', () => {
diff --git a/packages/maker-shared/src/messageRender.ts b/packages/maker-shared/src/messageRender.ts
index 4f56a76481..8b1917ff61 100644
--- a/packages/maker-shared/src/messageRender.ts
+++ b/packages/maker-shared/src/messageRender.ts
@@ -457,14 +457,24 @@ export function findMessageTodoInsertions<TMessage extends MessageRenderSourceMe
firstIndex: number;
lastIndex: number;
source: MessageRenderTodoSource;
+ /** 该 session 首条计划调用之前最近一条 user 消息的下标(turn 边界锚点)。 */
+ userBoundaryIndex: number;
}> = [];
const lastSessionBySource = new Map<MessageRenderTodoSource, (typeof sessions)[number]>();
const taskState = new Map<string, MessageRenderTodoItem>();
+ let lastUserIndex = -1;
for (let index = 0; index < messages.length; index++) {
const message = messages[index];
+ if (message.role === 'user') {
+ lastUserIndex = index;
+ continue;
+ }
const source = agentPlanSource(toolNameOf(message));
if (!source) continue;
+ // 子代理内部的计划调用不属于顶层面板:它们的 owner 是那个 Agent/Task 工具行,
+ // 混进来会顶掉主线程计划(顶层"最新计划"按位置竞争,历史病 §3.1.5)。
+ if (message.parentToolUseId) continue;
const resultText = resultByToolUseId.get(toolUseIdOf(message) ?? '');
const previous = lastSessionBySource.get(source);
@@ -479,9 +489,18 @@ export function findMessageTodoInsertions<TMessage extends MessageRenderSourceMe
// 置 false)后不算边界,sealed-then-updated 的复亮行为不变。
const previousSealed =
Boolean(previous) && planRowSealOf(messages[previous!.lastIndex]).sealed;
+ // 普通 user turn 也是所有权边界:用户开了新话题,旧的未完成计划不得把新计划
+ // 吞成"续期"(历史病 §3.1.2/3.1.3——串号后新计划复用旧 key、Task 状态跨
+ // turn 拼接)。task source 例外:显式指向已有任务的操作(TaskUpdate/TaskGet
+ // 带已知 id)仍是同一份清单的合法续写。
+ const crossesUserBoundary =
+ Boolean(previous)
+ && lastUserIndex > (previous?.lastIndex ?? -1)
+ && !(source === 'task' && taskToolTargetsExistingTask(message, resultText, taskState));
const startsNewSession =
!previous
|| previousSealed
+ || crossesUserBoundary
|| (Boolean(previousAllDone) && !continuesCompletedTaskSession);
if (source === 'task' && startsNewSession) {
taskState.clear();
@@ -500,7 +519,13 @@ export function findMessageTodoInsertions<TMessage extends MessageRenderSourceMe
previous.todos = parsed;
previous.lastIndex = index;
} else {
- const session = { todos: parsed, firstIndex: index, lastIndex: index, source };
+ const session = {
+ todos: parsed,
+ firstIndex: index,
+ lastIndex: index,
+ source,
+ userBoundaryIndex: lastUserIndex,
+ };
sessions.push(session);
lastSessionBySource.set(source, session);
}
@@ -551,6 +576,9 @@ export function getLatestMessageTodoState<TMessage extends MessageRenderSourceMe
for (let index = 0; index < messages.length; index++) {
const message = messages[index];
if (!isAgentPlanToolName(toolNameOf(message))) continue;
+ // 与 findMessageTodoInsertions 同一条边界:子代理内部的计划调用不参与
+ // 顶层"最新计划事件"的判定,否则子调用会让顶层 insertion 判为过期。
+ if (message.parentToolUseId) continue;
latestPlanIndex = index;
latestPlanMessage = message;
}