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): ignore foreign yield terminals before usage settle
Dash55129681a8f8
diff --git a/docs/dev-rules/maker-core-and-agent-behavior.md b/docs/dev-rules/maker-core-and-agent-behavior.mdindex 5c03365da0..d9282d0689 100644--- a/docs/dev-rules/maker-core-and-agent-behavior.md+++ b/docs/dev-rules/maker-core-and-agent-behavior.md@@ -85,8 +85,8 @@ timeout 不得触发自动换窗或 replay。Codex 当前没有与 Claude `AutoCbest-effort interrupt,再抛/返回取消;`wait` 仍输出 running marker 视为 cell存活证据,重试预算内继续等,不得当空续段报 lost-handle。claim 只归属于铸造它的 origin turn 及其续段 turn;迟到的外族终态(含成功- `completed`)不得结算、取消、lost-handle 当前 claim,也不得发出未认领的- 产品 `done`。同一产品 turn 上的+ `completed`)不得结算、取消、lost-handle 当前 claim,不得发出未认领的+ 产品 `done`,也不得结算当前续段的 generation/usage。同一产品 turn 上的ask_user/plan 内部续段必须等 yield 空闲后再 `turn/start`,不得并发;Stop/close 取消 yield 时,排队中的内部续段必须退出而不是被当成正常空闲继续发送。只有 cell 真正结算的成功空闲才能唤醒排队续段;lost-handle、重试耗尽、diff --git a/packages/maker-core/src/agents/codex/index.test.ts b/packages/maker-core/src/agents/codex/index.test.tsindex b2c7e051eb..6f8503c059 100644--- a/packages/maker-core/src/agents/codex/index.test.ts+++ b/packages/maker-core/src/agents/codex/index.test.ts@@ -18608,8 +18608,8 @@ describe('CodexAgent yield continuation', () => {workingDir: '/repo',});const handlers = host.getThreadHandlers();- if (!handlers?.itemCompleted || !handlers.turnCompleted) {- throw new Error('expected item and turn handlers');+ if (!handlers?.itemCompleted || !handlers.turnCompleted || !handlers.turnStarted || !handlers.tokenUsageUpdated) {+ throw new Error('expected item, turn, started, and usage handlers');}const events = await collectYieldEvents(handle);await handle.send({ type: 'user', content: 'run typecheck' });@@ -18636,6 +18636,18 @@ describe('CodexAgent yield continuation', () => {});const claimedDone = events.find((event) => event.type === 'done' && event.turnContinuationId != null);expect(handle.beginTurnContinuationWait?.(claimedDone?.turnContinuationId)).toBe('active');+ handlers.turnStarted({+ threadId: 'start-thread-id',+ turn: { id: 'turn-2', status: 'inProgress' },+ });+ handlers.tokenUsageUpdated({+ threadId: 'start-thread-id',+ turnId: 'turn-2',+ tokenUsage: {+ total: { totalTokens: 42, inputTokens: 30, outputTokens: 12, cachedInputTokens: 0 },+ last: { totalTokens: 42, inputTokens: 30, outputTokens: 12, cachedInputTokens: 0 },+ },+ });handlers.turnCompleted({threadId: 'start-thread-id',turn: { id: 'old-foreign-turn', status: 'failed', error: { message: 'stale turn failed' } },@@ -18644,6 +18656,30 @@ describe('CodexAgent yield continuation', () => {expect(events.find((event) => event.type === 'done' && event.turnContinuationId == null)).toBeUndefined();expect(handle.beginTurnContinuationWait?.(claimedDone?.turnContinuationId)).toBe('active');expect(handle.isTurnRunning?.()).toBe(true);+ handlers.itemCompleted({+ threadId: 'start-thread-id',+ turnId: 'turn-2',+ item: {+ id: 'item-wait-after-foreign-fail',+ type: 'function_call',+ name: 'wait',+ arguments: JSON.stringify({ cell_id: '226', max_tokens: 1000 }),+ content: [{ type: 'output_text', text: 'Script completed\nWall time 0.1 seconds\nOutput:\n' }],+ },+ });+ handlers.turnCompleted({+ threadId: 'start-thread-id',+ turn: { id: 'turn-2', status: 'completed' },+ });+ await waitForExpectation(() => {+ expect(events.filter((event) => event.type === 'done')).toHaveLength(2);+ });+ const productDone = events.find((event) => event.type === 'done' && event.turnContinuationId == null);+ expect((productDone?.data as { usage?: { promptTokens?: number; completionTokens?: number } }).usage).toMatchObject({+ promptTokens: 30,+ completionTokens: 12,+ });+ expect(handle.isTurnRunning?.()).toBe(false);await handle.close();});@@ -18663,8 +18699,8 @@ describe('CodexAgent yield continuation', () => {workingDir: '/repo',});const handlers = host.getThreadHandlers();- if (!handlers?.itemCompleted || !handlers.turnCompleted) {- throw new Error('expected item and turn handlers');+ if (!handlers?.itemCompleted || !handlers.turnCompleted || !handlers.turnStarted || !handlers.tokenUsageUpdated) {+ throw new Error('expected item, turn, started, and usage handlers');}const events = await collectYieldEvents(handle);await handle.send({ type: 'user', content: 'run typecheck' });@@ -18691,6 +18727,18 @@ describe('CodexAgent yield continuation', () => {});const claimedDone = events.find((event) => event.type === 'done' && event.turnContinuationId != null);expect(handle.beginTurnContinuationWait?.(claimedDone?.turnContinuationId)).toBe('active');+ handlers.turnStarted({+ threadId: 'start-thread-id',+ turn: { id: 'turn-2', status: 'inProgress' },+ });+ handlers.tokenUsageUpdated({+ threadId: 'start-thread-id',+ turnId: 'turn-2',+ tokenUsage: {+ total: { totalTokens: 42, inputTokens: 30, outputTokens: 12, cachedInputTokens: 0 },+ last: { totalTokens: 42, inputTokens: 30, outputTokens: 12, cachedInputTokens: 0 },+ },+ });handlers.turnCompleted({threadId: 'start-thread-id',turn: { id: 'old-foreign-turn', status: 'completed' },@@ -18703,6 +18751,30 @@ describe('CodexAgent yield continuation', () => {expect(events.find((event) => event.type === 'done' && event.turnContinuationId == null)).toBeUndefined();expect(handle.beginTurnContinuationWait?.(claimedDone?.turnContinuationId)).toBe('active');expect(handle.isTurnRunning?.()).toBe(true);+ handlers.itemCompleted({+ threadId: 'start-thread-id',+ turnId: 'turn-2',+ item: {+ id: 'item-wait-after-foreign',+ type: 'function_call',+ name: 'wait',+ arguments: JSON.stringify({ cell_id: '226', max_tokens: 1000 }),+ content: [{ type: 'output_text', text: 'Script completed\nWall time 0.1 seconds\nOutput:\n' }],+ },+ });+ handlers.turnCompleted({+ threadId: 'start-thread-id',+ turn: { id: 'turn-2', status: 'completed' },+ });+ await waitForExpectation(() => {+ expect(events.filter((event) => event.type === 'done')).toHaveLength(2);+ });+ const productDone = events.find((event) => event.type === 'done' && event.turnContinuationId == null);+ expect((productDone?.data as { usage?: { promptTokens?: number; completionTokens?: number } }).usage).toMatchObject({+ promptTokens: 30,+ completionTokens: 12,+ });+ expect(handle.isTurnRunning?.()).toBe(false);await handle.close();});diff --git a/packages/maker-core/src/agents/codex/index.ts b/packages/maker-core/src/agents/codex/index.tsindex d1e767b427..3489cf0ca1 100644--- a/packages/maker-core/src/agents/codex/index.ts+++ b/packages/maker-core/src/agents/codex/index.ts@@ -8831,6 +8831,15 @@ export class CodexAgent extends BaseAgent {clearActiveToolContextsForTurn(turn.id);const overlapsActiveTurn = suppressTerminalUi && currentTurnId !== null && currentTurnId !== turn.id;if (overlapsActiveTurn) return;+ const activeYieldClaim = activeYieldContinuationClaim();+ if (activeYieldClaim && !claimOwnsTurn(activeYieldClaim, turn.id)) {+ // Foreign terminals must not settle the live continuation's usage,+ // generation timing, claim, or product Done. Tombstone the foreign+ // turn above, then leave the owned continuation untouched.+ latestPlanByTurn.delete(turn.id);+ flushDeferredTerminalTurnCompletionsIfIdle();+ return;+ }const completedTurnWasPlanMode = currentTurnPlanModeActive;if (currentTurnId === turn.id || currentTurnId === null) {isTurnInFlight = false;@@ -8952,12 +8961,6 @@ export class CodexAgent extends BaseAgent {}if (turn.status === 'failed' || turn.status === 'interrupted') {- const claim = activeYieldContinuationClaim();- if (claim && !claimOwnsTurn(claim, turn.id)) {- latestPlanByTurn.delete(turn.id);- flushDeferredTerminalTurnCompletionsIfIdle();- return;- }// 失败 / 中断的 plan turn 不发审批 — 半截计划没有审批意义, 循环就此结束。proposedPlanText = null;planCycleActive = false;@@ -9007,13 +9010,7 @@ export class CodexAgent extends BaseAgent {yieldedExecCellsByTurnId.delete(turn.id);let yieldClaim: YieldContinuationClaim | null = null;let suppressSuccessfulYieldBoundary = false;- if (existingYieldClaim && !claimOwnsTurn(existingYieldClaim, turn.id)) {- // A late foreign completed must not settle the claim or emit an- // unclaimed product Done while the continuation is still running.- latestPlanByTurn.delete(turn.id);- flushDeferredTerminalTurnCompletionsIfIdle();- return;- } else if (existingYieldClaim?.state === 'awaiting') {+ if (existingYieldClaim?.state === 'awaiting') {yieldClaim = existingYieldClaim;if (yieldedCells.length > 0) {yieldClaim.cells = dedupeCells([...yieldClaim.cells, ...yieldedCells]);