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(codex): 修复任务卡片成功结束后仍停在首个步骤 (#326)

JustinBaoeb5dc4ee6e51
diff --git a/apps/desktop/src/renderer/__tests__/planReviewDoneRace.test.ts b/apps/desktop/src/renderer/__tests__/planReviewDoneRace.test.ts
index 0ca428cddf..b0c52818c6 100644
--- a/apps/desktop/src/renderer/__tests__/planReviewDoneRace.test.ts
+++ b/apps/desktop/src/renderer/__tests__/planReviewDoneRace.test.ts
@@ -151,13 +151,18 @@ function emitDone(
source: 'codex' | 'claude-code',
plan?: Array<{ step: string; status: string }>,
turnId = 'turn-1',
+ turnStatus?: string,
): void {
onEvent?.({
sessionId: SESSION_ID,
event: {
type: 'done',
source,
- data: { type: 'task_complete', raw: { id: turnId }, ...(plan ? { plan } : {}) },
+ data: {
+ type: 'task_complete',
+ raw: { id: turnId, ...(turnStatus ? { status: turnStatus } : {}) },
+ ...(plan ? { plan } : {}),
+ },
},
});
}
@@ -263,6 +268,15 @@ describe('plan_review 与 done 的时序', () => {
expect(latestPlanStatuses()).toEqual(['in_progress', 'pending']);
});
+ it('codex:成功完成但缺少最终 plan 快照时收口计划卡片', () => {
+ makerChatStore.setSessionRuntime(SESSION_ID, { agentKind: 'codex' });
+ emitPlanUpdate('codex', ['in_progress', 'pending']);
+
+ emitDone('codex', undefined, 'turn-1', 'completed');
+
+ expect(latestPlanStatuses()).toEqual(['completed', 'completed']);
+ });
+
it('claude:done 不改 Codex update_plan 展示状态', () => {
makerChatStore.setSessionRuntime(SESSION_ID, { agentKind: 'claude-code' });
emitPlanUpdate('claude-code', ['in_progress', 'pending']);
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index c237c0b0fa..6707c0265e 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -2263,10 +2263,18 @@ export function handleStreamEvent(
return next;
});
- const terminalData = event.data as { plan?: unknown; raw?: { id?: unknown } } | null | undefined;
+ const terminalData =
+ event.data as { plan?: unknown; raw?: { id?: unknown; status?: unknown } } | null | undefined;
const terminalTurnId = typeof terminalData?.raw?.id === 'string' ? terminalData.raw.id : null;
+ const terminalTurnStatus =
+ typeof terminalData?.raw?.status === 'string' ? terminalData.raw.status : null;
const doneMessages = event.source === 'codex'
- ? applyCodexPlanSnapshotOnDone(cleanedMessages, terminalData?.plan, terminalTurnId).messages
+ ? applyCodexPlanSnapshotOnDone(
+ cleanedMessages,
+ terminalData?.plan,
+ terminalTurnId,
+ terminalTurnStatus,
+ ).messages
: cleanedMessages;
// F1-a Option C: tool-result-image 孤儿 flush(turn 末残留 pendingFullText)已收口
diff --git a/apps/mobile/src/__tests__/remoteSessionStore.test.ts b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
index 027c00894f..52763431ac 100644
--- a/apps/mobile/src/__tests__/remoteSessionStore.test.ts
+++ b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
@@ -843,6 +843,111 @@ describe('remoteSessionStore', () => {
});
});
+ it('keeps synthetic completion when done precedes the initial plan DB row', () => {
+ remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
+ sessionId: 's1',
+ persistId: 'plan-row-1',
+ event: {
+ type: 'tool_use',
+ data: {
+ toolUseId: 'plan:turn-1',
+ toolName: 'update_plan',
+ input: {
+ plan: [
+ { step: 'Inspect', status: 'in_progress' },
+ { step: 'Patch', status: 'pending' },
+ ],
+ },
+ },
+ },
+ });
+ remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
+ sessionId: 's1',
+ event: {
+ type: 'done',
+ source: 'codex',
+ data: {
+ raw: { id: 'turn-1', status: 'completed' },
+ plan: [
+ { step: 'Inspect', status: 'in_progress' },
+ { step: 'Patch', status: 'pending' },
+ ],
+ },
+ },
+ });
+
+ remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
+ sessionId: 's1',
+ message: {
+ ...message('plan-row-1', 's1'),
+ role: 'tool_use',
+ toolUseId: 'plan:turn-1',
+ content: {
+ toolUseId: 'plan:turn-1',
+ toolName: 'update_plan',
+ input: {
+ plan: [
+ { step: 'Inspect', status: 'in_progress' },
+ { step: 'Patch', status: 'pending' },
+ ],
+ },
+ },
+ },
+ });
+
+ expect(remoteSessionStore.getMessages('s1')[0].content).toMatchObject({
+ input: {
+ plan: [
+ { step: 'Inspect', status: 'completed' },
+ { step: 'Patch', status: 'completed' },
+ ],
+ },
+ });
+ });
+
+ it('does not let a delayed message window revert synthetic completion', () => {
+ const stalePlanRow = {
+ ...message('plan-row-1', 's1'),
+ role: 'tool_use' as const,
+ toolUseId: 'plan:turn-1',
+ content: {
+ toolUseId: 'plan:turn-1',
+ toolName: 'update_plan',
+ input: { plan: [{ step: 'Inspect', status: 'in_progress' }] },
+ },
+ };
+ remoteSessionStore.setMessages('s1', [stalePlanRow]);
+ remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
+ sessionId: 's1',
+ persistId: 'plan-row-1',
+ event: {
+ type: 'tool_use',
+ data: {
+ toolUseId: 'plan:turn-1',
+ toolName: 'update_plan',
+ input: { plan: [{ step: 'Inspect', status: 'in_progress' }] },
+ },
+ },
+ });
+ remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
+ sessionId: 's1',
+ event: {
+ type: 'done',
+ source: 'codex',
+ data: {
+ raw: { id: 'turn-1', status: 'completed' },
+ plan: [{ step: 'Inspect', status: 'in_progress' }],
+ },
+ },
+ });
+
+ remoteSessionStore.setLatestMessageWindow('s1', [stalePlanRow]);
+
+ expect(remoteSessionStore.getMessages('s1')[0].content).toMatchObject({
+ input: { plan: [{ step: 'Inspect', status: 'completed' }] },
+ });
+ });
+
it('finalizes pre-compact streaming rows and de-duplicates the same boundary replay', () => {
vi.useFakeTimers();
try {
diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.ts
index 1a62b25073..91f7a41e53 100644
--- a/apps/mobile/src/session/remoteSessionStore.ts
+++ b/apps/mobile/src/session/remoteSessionStore.ts
@@ -255,6 +255,19 @@ function rememberLivePlanSnapshot(sessionId: string, snapshot: LivePlanSnapshot)
if (snapshot.persistId) sessionSnapshots.set(`persist:${snapshot.persistId}`, snapshot);
}
+function rememberLivePlanContent(
+ sessionId: string,
+ toolUseId: string,
+ content: Record<string, unknown>,
+): void {
+ const previous = livePlanSnapshots.get(sessionId)?.get(`tool:${toolUseId}`);
+ rememberLivePlanSnapshot(sessionId, {
+ content,
+ toolUseId,
+ ...(previous?.persistId ? { persistId: previous.persistId } : {}),
+ });
+}
+
function overlayLivePlanSnapshot(sessionId: string, message: RemoteMessage): RemoteMessage {
if (message.role !== 'tool_use') return message;
const sessionSnapshots = livePlanSnapshots.get(sessionId);
@@ -269,6 +282,29 @@ function overlayLivePlanSnapshot(sessionId: string, message: RemoteMessage): Rem
: message;
}
+function completeLivePlanSnapshotOnDone(
+ sessionId: string,
+ snapshot: unknown,
+ turnId: string | null,
+ terminalStatus: string | null,
+): boolean {
+ if (!turnId) return false;
+ const toolUseId = `plan:${turnId}`;
+ const liveSnapshot = livePlanSnapshots.get(sessionId)?.get(`tool:${toolUseId}`);
+ if (!liveSnapshot) return false;
+
+ const completed = applyCodexPlanSnapshotOnDone(
+ [{ role: 'tool_use', toolUseId, content: liveSnapshot.content }],
+ snapshot,
+ turnId,
+ terminalStatus,
+ );
+ const content = completed.messages[0]?.content;
+ if (!completed.changed || !isRecord(content)) return false;
+ rememberLivePlanContent(sessionId, toolUseId, content);
+ return true;
+}
+
/** End any pre-compaction streaming rows without changing the overall running turn. */
function finishMessageStreamingAtCompactBoundary(message: RemoteMessage): RemoteMessage {
let agentMeta = message.agentMeta;
@@ -1691,20 +1727,33 @@ export const remoteSessionStore = {
const data = isRecord(event.data) ? event.data : null;
const rawTurn = isRecord(data?.raw) ? data.raw : null;
const turnId = readString(rawTurn, 'id');
+ const turnStatus = readString(rawTurn, 'status');
const currentMessages = messages.get(sessionId) ?? [];
- const completed = applyCodexPlanSnapshotOnDone(currentMessages, data?.plan, turnId);
+ const completed = applyCodexPlanSnapshotOnDone(
+ currentMessages,
+ data?.plan,
+ turnId,
+ turnStatus,
+ );
+ completeLivePlanSnapshotOnDone(
+ sessionId,
+ data?.plan,
+ turnId,
+ turnStatus,
+ );
terminalPlanChanged = completed.changed;
if (completed.changed) {
messages.set(sessionId, [...completed.messages]);
- if (Array.isArray(data?.plan) && completed.toolUseId) {
- rememberLivePlanSnapshot(sessionId, {
- toolUseId: completed.toolUseId,
- content: {
- toolUseId: completed.toolUseId,
- toolName: 'update_plan',
- input: { plan: data.plan },
- },
- });
+ const completedMessage = completed.messages.find((message) => {
+ if (message.toolUseId === completed.toolUseId) return true;
+ return readString(message.content, 'toolUseId') === completed.toolUseId;
+ });
+ if (completed.toolUseId && isRecord(completedMessage?.content)) {
+ rememberLivePlanContent(
+ sessionId,
+ completed.toolUseId,
+ completedMessage.content,
+ );
}
}
}
diff --git a/packages/maker-shared/src/__tests__/planCompletion.test.ts b/packages/maker-shared/src/__tests__/planCompletion.test.ts
index 6eeed1c7eb..7b79e294d9 100644
--- a/packages/maker-shared/src/__tests__/planCompletion.test.ts
+++ b/packages/maker-shared/src/__tests__/planCompletion.test.ts
@@ -88,4 +88,89 @@ describe('applyCodexPlanSnapshotOnDone', () => {
toolUseId: null,
});
});
+
+ it('marks the matching plan complete when a successful turn has no final snapshot', () => {
+ const message = planMessage('plan:done', [
+ { step: 'Inspect', status: 'in_progress' },
+ { step: 'Patch', status: 'pending' },
+ ]);
+ const result = applyCodexPlanSnapshotOnDone(
+ [message],
+ null,
+ 'done',
+ 'completed',
+ );
+
+ expect(result.changed).toBe(true);
+ expect(result.toolUseId).toBe('plan:done');
+ expect(result.messages[0]).toMatchObject({
+ toolInput: {
+ plan: [
+ { step: 'Inspect', status: 'completed' },
+ { step: 'Patch', status: 'completed' },
+ ],
+ },
+ content: {
+ input: {
+ plan: [
+ { step: 'Inspect', status: 'completed' },
+ { step: 'Patch', status: 'completed' },
+ ],
+ },
+ },
+ });
+ });
+
+ it('treats an unfinished done snapshot as cached progress for a successful turn', () => {
+ const message = planMessage('plan:done', [
+ { step: 'Inspect', status: 'in_progress' },
+ { step: 'Patch', status: 'pending' },
+ ]);
+ const cachedProgress = [
+ { step: 'Inspect', status: 'completed', description: 'kept from latest update' },
+ { step: 'Patch', status: 'in_progress' },
+ ];
+
+ const result = applyCodexPlanSnapshotOnDone(
+ [message],
+ cachedProgress,
+ 'done',
+ 'completed',
+ );
+
+ expect(result.changed).toBe(true);
+ expect(result.messages[0]).toMatchObject({
+ toolInput: {
+ plan: [
+ { step: 'Inspect', status: 'completed', description: 'kept from latest update' },
+ { step: 'Patch', status: 'completed' },
+ ],
+ },
+ });
+ });
+
+ it('does not infer completion without a matching turn id', () => {
+ const message = planMessage('plan:unrelated', [
+ { step: 'Inspect', status: 'in_progress' },
+ ]);
+ const messages = [message];
+
+ expect(applyCodexPlanSnapshotOnDone(
+ messages,
+ null,
+ null,
+ 'completed',
+ )).toEqual({ messages, changed: false, toolUseId: null });
+ });
+
+ it('does not infer completion for a failed or interrupted turn', () => {
+ const message = planMessage('plan:stopped', [{ step: 'Inspect', status: 'in_progress' }]);
+ const messages = [message];
+
+ expect(applyCodexPlanSnapshotOnDone(messages, null, 'stopped', 'interrupted')).toEqual({
+ messages,
+ changed: false,
+ toolUseId: null,
+ });
+ });
});
diff --git a/packages/maker-shared/src/messageRender.ts b/packages/maker-shared/src/messageRender.ts
index e620999b47..e91b47486f 100644
--- a/packages/maker-shared/src/messageRender.ts
+++ b/packages/maker-shared/src/messageRender.ts
@@ -440,9 +440,19 @@ export function applyCodexPlanSnapshotOnDone<
messages: readonly TMessage[],
snapshot: unknown,
turnId?: string | null,
+ terminalStatus?: unknown,
): CodexPlanSnapshotApplyResult<TMessage> {
- if (!Array.isArray(snapshot)) return { messages, changed: false, toolUseId: null };
+ const authoritativeSnapshot = Array.isArray(snapshot) ? snapshot : null;
+ const hasAuthoritativeSnapshot = authoritativeSnapshot !== null;
+ const canInferCompletion = terminalStatus === 'completed' && Boolean(turnId);
+ if (!hasAuthoritativeSnapshot && !canInferCompletion) {
+ return { messages, changed: false, toolUseId: null };
+ }
const expectedToolUseId = turnId ? `plan:${turnId}` : null;
+ const shouldInferCompletion = canInferCompletion && (
+ !hasAuthoritativeSnapshot
+ || authoritativeSnapshot.some((item) => readRecord(item)?.status !== 'completed')
+ );
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index];
@@ -454,7 +464,24 @@ export function applyCodexPlanSnapshotOnDone<
const input = readRecord(toolInputOf(message));
if (!Array.isArray(input?.plan)) continue;
- if (samePlanSnapshot(input.plan, snapshot)) {
+ // maker-core attaches the latest cached turn/plan/updated snapshot to done.
+ // When Codex omits its final plan update, that array still contains open
+ // items and is in progress rather than a terminal snapshot. Converge that
+ // cached progress (or the persisted row when no snapshot exists) only for a
+ // matching, explicitly successful turn. Without a turn id, an array can
+ // still be applied as supplied but completion must never be inferred for an
+ // unrelated last plan row.
+ const snapshotSource = authoritativeSnapshot ?? input.plan;
+ const nextSnapshot = shouldInferCompletion
+ ? snapshotSource.map((item) => {
+ const record = readRecord(item);
+ return record ? { ...record, status: 'completed' } : item;
+ })
+ : authoritativeSnapshot;
+ if (!nextSnapshot) {
+ return { messages, changed: false, toolUseId: null };
+ }
+ if (samePlanSnapshot(input.plan, nextSnapshot)) {
return { messages, changed: false, toolUseId };
}
@@ -462,10 +489,10 @@ export function applyCodexPlanSnapshotOnDone<
next[index] = {
...message,
...(message.toolInput !== undefined
- ? { toolInput: { ...input, plan: snapshot } }
+ ? { toolInput: { ...input, plan: nextSnapshot } }
: {}),
...(content
- ? { content: { ...content, input: { ...input, plan: snapshot } } }
+ ? { content: { ...content, input: { ...input, plan: nextSnapshot } } }
: {}),
};
return { messages: next, changed: true, toolUseId };