Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(maker-core): 处理 codex R14 review 的 2 P1 — 孤儿守卫生命周期
yan381c74feba46
diff --git a/packages/maker-core/src/agents/codex/index.test.ts b/packages/maker-core/src/agents/codex/index.test.tsindex a1fc9deadc..cb7a2fae53 100644--- a/packages/maker-core/src/agents/codex/index.test.ts+++ b/packages/maker-core/src/agents/codex/index.test.ts@@ -7033,6 +7033,150 @@ describe('CodexAgent turn lifecycle', () => {await handle.close();});+ it('quarantines an orphan terminal error that arrives before its turnStarted (codex R14 P1)', async () => {+ // 孤儿 turn 的终态 error 可以比它的 turnStarted 先到: id 尚未入 buffer,+ // 若只按 buffered 集合拦截会穿透 (targetsPendingTurn=true 被按在飞 send+ // 的终态处理, 终结合法的新 send) — 孤儿守卫 + pending + 无活跃 turn 时+ // 未知 id 的 error 视同 started 预缓冲, 等对账。+ const firstStart = deferred<unknown>();+ const secondStart = deferred<unknown>();+ let attempt = 0;+ const agent = new CodexAgent(createDeps());+ const host = installFakeHost(agent, (method) => {+ if (method === Method.TurnStart) {+ attempt += 1;+ if (attempt === 1) return firstStart.promise;+ return secondStart.promise;+ }+ return undefined;+ });+ const handle = await agent.startSession({+ sessionId: 'session-orphan-error-first',+ model: 'gpt-5.4',+ workingDir: '/repo',+ });+ const subscribeCalls = host.subscribeThread.mock.calls as unknown as Array<[string, ThreadEventHandlers]>;+ const handlers = subscribeCalls[0]?.[1];+ const iterator = handle.events()[Symbol.asyncIterator]();++ const send1 = handle.send({ type: 'user', content: 'first' });+ for (let i = 0; i < 5; i += 1) {+ if (host.request.mock.calls.some(([method]) => method === Method.TurnStart)) break;+ await Promise.resolve();+ }+ firstStart.reject(new Error('codex app-server turn/start timed out after 60000ms'));+ await send1;+ expect(await nextEvent(iterator)).toMatchObject({ type: 'status', data: { isRunning: true } });+ expect(await nextEvent(iterator)).toMatchObject({ type: 'error', data: { isTerminal: true } });+ expect(await nextEvent(iterator)).toMatchObject({+ type: 'status',+ data: { status: 'Done', isRunning: false },+ });++ const send2 = handle.send({ type: 'user', content: 'second' });+ for (let i = 0; i < 5; i += 1) {+ if (host.request.mock.calls.filter(([method]) => method === Method.TurnStart).length >= 2) break;+ await Promise.resolve();+ }+ expect(await nextEvent(iterator)).toMatchObject({ type: 'status', data: { isRunning: true } });+ // 孤儿 turn 的 terminal error 先于它的 turnStarted 到达: 预缓冲, 不穿透。+ handlers.error?.({+ threadId: 'start-thread-id',+ turnId: 'orphan-turn',+ scope: 'turn',+ willRetry: false,+ error: { message: 'orphan turn died server-side' },+ });+ // send2 不被终结: 事件流安静, 无 error / Done 出来。+ await expect(nextEvent(iterator)).rejects.toThrow('timed out waiting for event');++ // 响应 turn-2 ≠ orphan-turn → 孤儿坐实: 墓碑 + interrupt; turn-2 激活。+ secondStart.resolve({ turn: { id: 'turn-2' } });+ await send2;+ await vi.waitFor(() => {+ expect(host.request).toHaveBeenCalledWith(+ Method.TurnInterrupt,+ expect.objectContaining({ threadId: 'start-thread-id', turnId: 'orphan-turn' }),+ );+ });+ expect(handle.isTurnRunning?.()).toBe(true);+ expect(handle.getCurrentTurnId?.()).toBe('turn-2');+ await handle.close();+ });++ it('keeps the orphan guard after a successful turn so a late orphan turnStarted is tombstoned (codex R14 P1)', async () => {+ // 失败 RPC 的 turnStarted 可能在新 turn 完成后才到 (currentTurnId 已回+ // null, 无 pending): 守卫若在成功响应时解除, 迟到的孤儿 started 会被+ // 正常激活成假 running (会话永久卡) — 守卫保持, 按孤儿墓碑 + interrupt。+ const firstStart = deferred<unknown>();+ const secondStart = deferred<unknown>();+ let attempt = 0;+ const agent = new CodexAgent(createDeps());+ const host = installFakeHost(agent, (method) => {+ if (method === Method.TurnStart) {+ attempt += 1;+ if (attempt === 1) return firstStart.promise;+ return secondStart.promise;+ }+ return undefined;+ });+ const handle = await agent.startSession({+ sessionId: 'session-late-orphan-started',+ model: 'gpt-5.4',+ workingDir: '/repo',+ });+ const subscribeCalls = host.subscribeThread.mock.calls as unknown as Array<[string, ThreadEventHandlers]>;+ const handlers = subscribeCalls[0]?.[1];+ const iterator = handle.events()[Symbol.asyncIterator]();++ const send1 = handle.send({ type: 'user', content: 'first' });+ for (let i = 0; i < 5; i += 1) {+ if (host.request.mock.calls.some(([method]) => method === Method.TurnStart)) break;+ await Promise.resolve();+ }+ firstStart.reject(new Error('codex app-server turn/start timed out after 60000ms'));+ await send1;+ expect(await nextEvent(iterator)).toMatchObject({ type: 'status', data: { isRunning: true } });+ expect(await nextEvent(iterator)).toMatchObject({ type: 'error', data: { isTerminal: true } });+ expect(await nextEvent(iterator)).toMatchObject({+ type: 'status',+ data: { status: 'Done', isRunning: false },+ });++ // 第二次 send 正常完成: started 缓冲 → 响应激活 → completed 收口。+ const send2 = handle.send({ type: 'user', content: 'second' });+ for (let i = 0; i < 5; i += 1) {+ if (host.request.mock.calls.filter(([method]) => method === Method.TurnStart).length >= 2) break;+ await Promise.resolve();+ }+ expect(await nextEvent(iterator)).toMatchObject({ type: 'status', data: { isRunning: true } });+ handlers.turnStarted?.({ threadId: 'start-thread-id', turn: { id: 'turn-2' } });+ secondStart.resolve({ turn: { id: 'turn-2' } });+ await send2;+ expect(handle.getCurrentTurnId?.()).toBe('turn-2');+ handlers.turnCompleted?.({ threadId: 'start-thread-id', turn: { id: 'turn-2', status: 'completed' } });+ expect(await nextEvent(iterator)).toMatchObject({+ type: 'status',+ data: { status: 'Done', isRunning: false },+ });+ await nextEvent(iterator); // done 事件+ expect(handle.isTurnRunning?.()).toBe(false);+ expect(handle.getCurrentTurnId?.()).toBeNull();++ // 失败 RPC 的孤儿 started 此刻才迟到: 守卫保持 → 墓碑 + interrupt,+ // 不得激活成假 running。+ handlers.turnStarted?.({ threadId: 'start-thread-id', turn: { id: 'orphan-turn' } });+ expect(handle.isTurnRunning?.()).toBe(false);+ expect(handle.getCurrentTurnId?.()).toBeNull();+ await vi.waitFor(() => {+ expect(host.request).toHaveBeenCalledWith(+ Method.TurnInterrupt,+ expect.objectContaining({ threadId: 'start-thread-id', turnId: 'orphan-turn' }),+ );+ });+ await handle.close();+ });+it('does not escalate rate-limit backoff retries to a terminal backend-unreachable error (codex R12 P2)', async () => {// 429 / usage-limit 的 willRetry 是 provider 退避窗口, daemon 会在窗口后// 自己成功 — 不得计入 retry 升级, 否则可恢复的限流被误杀成「后端不可达」。diff --git a/packages/maker-core/src/agents/codex/index.ts b/packages/maker-core/src/agents/codex/index.tsindex 912351a704..6ebb3c8730 100644--- a/packages/maker-core/src/agents/codex/index.ts+++ b/packages/maker-core/src/agents/codex/index.ts@@ -3816,8 +3816,23 @@ export class CodexAgent extends BaseAgent {// 合法则激活后按序重放 (早期输出不丢, 终态自然收口), 孤儿则整队丢弃。// 返回 true = 已入队, 调用方直接 return。重放闭包执行时 buffer 已清空// 且 turn 已激活, 重进 handler 会走正常路径, 不会二次入队。+ //+ // 孤儿证据可以比它的 turnStarted 先到 (codex R14 P1): error/item 先到+ // 时 id 尚未入 buffer, 会穿透 stale guard 被按在飞 send 处理 — 孤儿+ // 守卫生效 + 新 RPC 在飞 + 无活跃 turn 时, 未知 id 视同 started 先进+ // buffer 再入队, 等对账。const enqueueIfBufferedTurn = (turnId: string | null | undefined, replay: () => void): boolean => {- if (!turnId || !bufferedOrphanTurnIds.has(turnId)) return false;+ if (!turnId) return false;+ if (!bufferedOrphanTurnIds.has(turnId)) {+ if (!(turnStartFailedWithoutTurnId && isTurnStartPending && currentTurnId === null)) {+ return false;+ }+ bufferedOrphanTurnIds.add(turnId);+ log.debug('buffering ambiguous turn event (evidence before turnStarted) until turn/start response arrives', {+ turnId,+ threadId,+ });+ }const queue = bufferedTurnEventQueues.get(turnId) ?? [];queue.push(replay);bufferedTurnEventQueues.set(turnId, queue);@@ -4811,9 +4826,14 @@ export class CodexAgent extends BaseAgent {turnRetryTracker.reset();}}- // turn/start 成功 → 孤儿守卫解除 (上次失败的 RPC 若也有迟到 started,- // 那是一次新的语义重叠, 由 turn id 匹配路径处理)。- turnStartFailedWithoutTurnId = false;+ // turn/start 成功后孤儿守卫**保持**, 不解除 (codex R14 P1): 失败+ // RPC 的孤儿证据 (turnStarted/error/completed) 可能任意晚才到 —+ // 解除后迟到的孤儿 started 在 currentTurnId===null 窗口会被正常+ // 激活成假 running (会话永久卡)。守卫保持下: 未知 id 的迟到+ // started 按孤儿墓碑 + interrupt; 合法 turn 的 started 由+ // currentTurnId 匹配 (重复 started 幂等) 或 buffered 对账放行,+ // 不受影响。守卫在 RPC 失败路径 (4413) 已把在飞 buffer 坐实孤儿,+ // 语义依然自洽。// 墓碑: 该 turn 的终态已抢在本响应之前到达 (典型: 收紧补中断后// turnCompleted(interrupted) 先回), 不得重新置活, 否则会话卡 running。const alreadyCompleted = turnsCompletedBeforeStartResp.has(resp.turn.id);