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): 失败印记只落在当前 turn 段内的计划行

Kafeifei28cad0484b1c
diff --git a/packages/maker-shared/src/__tests__/planCompletion.test.ts b/packages/maker-shared/src/__tests__/planCompletion.test.ts
index 8bb0d9ef99..0fabb0c9a5 100644
--- a/packages/maker-shared/src/__tests__/planCompletion.test.ts
+++ b/packages/maker-shared/src/__tests__/planCompletion.test.ts
@@ -203,4 +203,24 @@ describe('markCodexPlanTurnFailed', () => {
const noPlan = { role: 'tool_use' as const, clientId: 'b1', toolName: 'Bash', content: '' };
expect(markCodexPlanTurnFailed([noPlan])).toEqual({ messages: [noPlan], changed: false });
});
+
+ it('never reaches past the latest user message into an older turn plan', () => {
+ // 所有权边界:本次失败的 turn 没发过 update_plan 时,不得把上一段历史里
+ // 未盖章的旧计划(如升级前已全勾完退场的行)标成失败复活——main 侧对应
+ // 落库也不会发生,内存里这枚错印记将没有任何广播能纠正。
+ const historicPlan = planMessage('plan:old', [{ step: 'Ship', status: 'completed' }]);
+ const newUserTurn = { role: 'user' as const, clientId: 'u2', content: 'next task' };
+ const failingTool = { role: 'tool_use' as const, clientId: 'b2', toolName: 'Bash', content: '' };
+
+ expect(markCodexPlanTurnFailed([historicPlan, newUserTurn, failingTool])).toEqual({
+ messages: [historicPlan, newUserTurn, failingTool],
+ changed: false,
+ });
+
+ // 计划在当前 user 段内(属于本次失败 turn)时照常落印。
+ const currentPlan = planMessage('plan:cur', [{ step: 'Ship', status: 'completed' }]);
+ const result = markCodexPlanTurnFailed([newUserTurn, currentPlan]);
+ expect(result.changed).toBe(true);
+ expect(result.messages[1]).toMatchObject({ turnCompleted: false });
+ });
});
diff --git a/packages/maker-shared/src/messageRender.ts b/packages/maker-shared/src/messageRender.ts
index 109fcf3782..f966167691 100644
--- a/packages/maker-shared/src/messageRender.ts
+++ b/packages/maker-shared/src/messageRender.ts
@@ -665,16 +665,26 @@ export function applyCodexPlanSnapshotOnDone<
/**
* Live-side twin of main's `persistCodexPlanOnTerminalError`: a Codex turn that
* dies on a terminal `error` never gets a `done`, so nothing seals its plan row
- * and nothing stamps `turnCompleted:false` in memory. Stamp the latest unsealed
+ * and nothing stamps `turnCompleted:false` in memory. Stamp the current turn's
* plan row here so the pinned capsule sees the task as alive immediately, in
* the window before the durable stamp's row broadcast arrives. Step statuses
* are untouched; already-sealed or already-stamped rows are left alone.
+ *
+ * Ownership boundary: only rows inside the failing turn's segment — after the
+ * latest user message — may be stamped. Codex plan rows are per-turn
+ * (`plan:<turnId>` is created within its own turn), so the scan stops cold at
+ * the first user row. A failed turn that never emitted `update_plan` stamps
+ * nothing; reaching past the boundary would resurrect an unrelated historical
+ * plan (pre-seal-era all-done rows retire via the legacy fallback, and a stray
+ * failure stamp would flip them back to "alive" with no durable write to
+ * correct it — main's stamper correctly no-ops in that case).
*/
export function markCodexPlanTurnFailed<TMessage extends MessageRenderSourceMessageLike>(
messages: readonly TMessage[],
): { messages: readonly TMessage[]; changed: boolean } {
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index];
+ if (message.role === 'user') break;
if (message.role !== 'tool_use' || toolNameOf(message) !== 'update_plan') continue;
if (message.terminalPlanSnapshot === true || message.turnCompleted === false) {
return { messages, changed: false };