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-core): ignore foreign completed turns and drop cancelled waiters

Dash846c3b0ac57a
diff --git a/docs/dev-rules/maker-core-and-agent-behavior.md b/docs/dev-rules/maker-core-and-agent-behavior.md
index 6281dac8e5..605e9d1d35 100644
--- a/docs/dev-rules/maker-core-and-agent-behavior.md
+++ b/docs/dev-rules/maker-core-and-agent-behavior.md
@@ -84,9 +84,10 @@ timeout 不得触发自动换窗或 replay。Codex 当前没有与 Claude `AutoC
续段 `turn/start` 已被服务端接受后若本地取消,必须先凭响应里的 turn id 落墓碑并
best-effort interrupt,再抛/返回取消;`wait` 仍输出 running marker 视为 cell
存活证据,重试预算内继续等,不得当空续段报 lost-handle。
- claim 只归属于铸造它的 origin turn 及其续段 turn;迟到的外族 `failed`/
- `interrupted` 不得取消当前 claim。同一产品 turn 上的 ask_user/plan 内部续段
- 必须等 yield 空闲后再 `turn/start`,不得并发。
+ claim 只归属于铸造它的 origin turn 及其续段 turn;迟到的外族终态(含成功
+ `completed`)不得结算、取消或 lost-handle 当前 claim。同一产品 turn 上的
+ ask_user/plan 内部续段必须等 yield 空闲后再 `turn/start`,不得并发;Stop/
+ close 取消 yield 时,排队中的内部续段必须退出而不是被当成正常空闲继续发送。
Claude wake continuation 与 Codex yield continuation 先分账,不抽公共模块。
## 3. 守住四项核心数据指标
diff --git a/packages/maker-core/src/agents/codex/index.test.ts b/packages/maker-core/src/agents/codex/index.test.ts
index f91f3d86f6..3a36b8c224 100644
--- a/packages/maker-core/src/agents/codex/index.test.ts
+++ b/packages/maker-core/src/agents/codex/index.test.ts
@@ -18573,6 +18573,134 @@ describe('CodexAgent yield continuation', () => {
expect(handle.isTurnRunning?.()).toBe(true);
await handle.close();
});
+
+ it('does not settle an active yield claim for a late foreign completed turn', async () => {
+ const agent = new CodexAgent(createDeps());
+ let turnSeq = 0;
+ const host = installFakeHost(agent, (method) => {
+ if (method === Method.TurnStart) {
+ turnSeq += 1;
+ return { turn: { id: `turn-${turnSeq}` } };
+ }
+ return undefined;
+ });
+ const handle = await agent.startSession({
+ sessionId: 'session-yield-foreign-completed-turn',
+ model: 'gpt-5.4',
+ workingDir: '/repo',
+ });
+ const handlers = host.getThreadHandlers();
+ if (!handlers?.itemCompleted || !handlers.turnCompleted) {
+ throw new Error('expected item and turn handlers');
+ }
+ const events = await collectYieldEvents(handle);
+ await handle.send({ type: 'user', content: 'run typecheck' });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ item: {
+ id: 'item-exec-foreign-complete',
+ type: 'commandExecution',
+ command: 'pnpm --filter desktop run typecheck',
+ status: 'completed',
+ aggregatedOutput: 'Script running with cell ID 226',
+ },
+ });
+ handlers.turnCompleted({
+ threadId: 'start-thread-id',
+ turn: { id: 'turn-1', status: 'completed' },
+ });
+ await waitForExpectation(() => {
+ expect(events.some((event) => event.type === 'done' && event.turnContinuationId != null)).toBe(true);
+ });
+ await vi.waitFor(() => {
+ expect(yieldTurnStartCalls(host)).toHaveLength(1);
+ });
+ const claimedDone = events.find((event) => event.type === 'done' && event.turnContinuationId != null);
+ expect(handle.beginTurnContinuationWait?.(claimedDone?.turnContinuationId)).toBe('active');
+ handlers.turnCompleted({
+ threadId: 'start-thread-id',
+ turn: { id: 'old-foreign-turn', status: 'completed' },
+ });
+ expect(events.some((event) => (
+ event.type === 'error'
+ && String((event.data as { reason?: string }).reason ?? '') === 'yield-continuation-lost-handle'
+ ))).toBe(false);
+ expect(handle.beginTurnContinuationWait?.(claimedDone?.turnContinuationId)).toBe('active');
+ expect(handle.isTurnRunning?.()).toBe(true);
+ await handle.close();
+ });
+
+ it('does not start a queued ask_user continuation after Stop cancels yield', async () => {
+ const agent = new CodexAgent(createDeps());
+ let turnSeq = 0;
+ const host = installFakeHost(agent, (method) => {
+ if (method === Method.TurnStart) {
+ turnSeq += 1;
+ return { turn: { id: `turn-${turnSeq}` } };
+ }
+ if (method === Method.TurnInterrupt) return {};
+ return undefined;
+ });
+ const handle = await agent.startSession({
+ sessionId: 'session-yield-stop-drops-ask-user',
+ model: 'gpt-5.4',
+ workingDir: '/repo',
+ });
+ const handlers = host.getThreadHandlers();
+ if (!handlers?.itemCompleted || !handlers.turnCompleted || !handlers.requestUserInput) {
+ throw new Error('expected item, turn, and user-input handlers');
+ }
+ const ownerDecision = deferred<InteractionDecision>();
+ handle.setInteractionResolver(async () => ownerDecision.promise);
+ const events = await collectYieldEvents(handle);
+ await handle.send({ type: 'user', content: 'run typecheck' });
+ void handlers.requestUserInput({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ itemId: 'item-ask-stop',
+ questions: [{
+ id: 'native-q1',
+ header: 'Question',
+ question: 'Pick one',
+ isOther: false,
+ isSecret: false,
+ options: [{ label: 'A', description: null }],
+ }],
+ }, { requestId: 'req-ask-stop' });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ item: {
+ id: 'item-exec-ask-stop',
+ type: 'commandExecution',
+ command: 'pnpm --filter desktop run typecheck',
+ status: 'completed',
+ aggregatedOutput: 'Script running with cell ID 226',
+ },
+ });
+ handlers.turnCompleted({
+ threadId: 'start-thread-id',
+ turn: { id: 'turn-1', status: 'completed' },
+ });
+ await vi.waitFor(() => {
+ expect(yieldTurnStartCalls(host)).toHaveLength(1);
+ });
+ ownerDecision.resolve({
+ kind: 'ask_user_question',
+ answers: { 'Pick one': 'A' },
+ });
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ expect(host.request.mock.calls.filter(([method]) => method === Method.TurnStart)).toHaveLength(2);
+ await handle.abort();
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ expect(host.request.mock.calls.filter(([method]) => method === Method.TurnStart)).toHaveLength(2);
+ expect(events.some((event) => (
+ event.type === 'error'
+ && String((event.data as { message?: string }).message ?? '').includes('ask_user continuation turn failed to start')
+ ))).toBe(false);
+ await handle.close();
+ });
});
describe('CodexAgent steer', () => {
diff --git a/packages/maker-core/src/agents/codex/index.ts b/packages/maker-core/src/agents/codex/index.ts
index d9cce27887..6cb65041b9 100644
--- a/packages/maker-core/src/agents/codex/index.ts
+++ b/packages/maker-core/src/agents/codex/index.ts
@@ -3135,7 +3135,7 @@ export class CodexAgent extends BaseAgent {
let activeYieldContinuationId: number | null = null;
let yieldContinuationInFlight = false;
let yieldContinuationAbort: AbortController | null = null;
- let yieldContinuationIdleWaiters: Array<() => void> = [];
+ let yieldContinuationIdleWaiters: Array<(cancelled: boolean) => void> = [];
const emitYieldContinuationState = (
continuationId: number,
state: 'awaiting' | 'active' | 'cancelled',
@@ -3266,15 +3266,15 @@ export class CodexAgent extends BaseAgent {
if (!claim || claim.settled || claim.continuationTurnId) return;
claim.continuationTurnId = turnId;
};
- const flushYieldContinuationIdleWaiters = (): void => {
- if (activeYieldContinuationClaim() != null || yieldContinuationInFlight) return;
+ const flushYieldContinuationIdleWaiters = (cancelled = false): void => {
+ if (!cancelled && (activeYieldContinuationClaim() != null || yieldContinuationInFlight)) return;
const waiters = yieldContinuationIdleWaiters;
yieldContinuationIdleWaiters = [];
- for (const resolve of waiters) resolve();
+ for (const resolve of waiters) resolve(cancelled);
};
- const waitForYieldContinuationIdle = async (): Promise<void> => {
- if (activeYieldContinuationClaim() == null && !yieldContinuationInFlight) return;
- await new Promise<void>((resolve) => {
+ const waitForYieldContinuationIdle = async (): Promise<boolean> => {
+ if (activeYieldContinuationClaim() == null && !yieldContinuationInFlight) return false;
+ return await new Promise<boolean>((resolve) => {
yieldContinuationIdleWaiters.push(resolve);
});
};
@@ -3289,7 +3289,7 @@ export class CodexAgent extends BaseAgent {
log.info('yield continuation cancelled', { reason, continuationId: claim.id });
emitYieldContinuationState(claim.id, 'cancelled');
releaseSettledYieldContinuationClaim(claim);
- flushYieldContinuationIdleWaiters();
+ flushYieldContinuationIdleWaiters(true);
return claim;
};
const discardYieldContinuationClaims = (reason: string): void => {
@@ -3308,7 +3308,7 @@ export class CodexAgent extends BaseAgent {
releaseSettledYieldContinuationClaim(claim);
}
yieldContinuationClaims.clear();
- flushYieldContinuationIdleWaiters();
+ flushYieldContinuationIdleWaiters(true);
};
const releaseYieldContinuationEvent = (event: AgentEvent): void => {
if (event.turnContinuationId === undefined) return;
@@ -5987,7 +5987,7 @@ export class CodexAgent extends BaseAgent {
);
log.debug('plan review ◀ approved — starting implementation turn', { turnId, edited: Boolean(edited && edited !== plan.trim()) });
try {
- await waitForYieldContinuationIdle();
+ if (await waitForYieldContinuationIdle()) return;
if (closed) return;
await handle.send(
{ type: 'user', content: message },
@@ -6017,7 +6017,7 @@ export class CodexAgent extends BaseAgent {
}
log.debug('plan review ◀ revision requested — starting plan revision turn', { turnId });
try {
- await waitForYieldContinuationIdle();
+ if (await waitForYieldContinuationIdle()) return;
if (closed) return;
await handle.send(
{ type: 'user', content: feedback },
@@ -6324,7 +6324,7 @@ export class CodexAgent extends BaseAgent {
autoReviewIntent?: string,
): Promise<void> {
if (closed) return;
- await waitForYieldContinuationIdle();
+ if (await waitForYieldContinuationIdle()) return;
if (closed) return;
const message = formatAskUserContinuationMessage(live.questions, answers);
const sendOptions: CodexInternalSendOptions = {
@@ -8993,7 +8993,9 @@ export class CodexAgent extends BaseAgent {
yieldedExecCellsByTurnId.delete(turn.id);
let yieldClaim: YieldContinuationClaim | null = null;
let suppressSuccessfulYieldBoundary = false;
- if (existingYieldClaim?.state === 'awaiting') {
+ if (existingYieldClaim && !claimOwnsTurn(existingYieldClaim, turn.id)) {
+ // A late foreign completed must not settle or retry the active claim.
+ } else if (existingYieldClaim?.state === 'awaiting') {
yieldClaim = existingYieldClaim;
if (yieldedCells.length > 0) {
yieldClaim.cells = dedupeCells([...yieldClaim.cells, ...yieldedCells]);