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): keep anonymous yield cells and defer plan review

Dash148e21721fae
diff --git a/docs/dev-rules/maker-core-and-agent-behavior.md b/docs/dev-rules/maker-core-and-agent-behavior.md
index 7bc33c3e88..a963d0f6a4 100644
--- a/docs/dev-rules/maker-core-and-agent-behavior.md
+++ b/docs/dev-rules/maker-core-and-agent-behavior.md
@@ -77,9 +77,12 @@ timeout 不得触发自动换窗或 replay。Codex 当前没有与 Claude `AutoC
`codex-rs` daemon),近期检测只能是 adapter 内、用真实 rollout fixture 锁死的启发式,
用来铸造有界 claim,再由宿主确定性开续段让模型 wait 同一 cell。无 `id`/`call_id`
的 item 只认 `itemCompleted` 快照:`itemUpdated` 不得入账,无 yield marker 的完成
- 必须清掉匿名桶,不得给匿名条目发明身份。同 turn 或续段里
+ 必须清掉匿名桶,不得给匿名条目发明身份。匿名 `wait` 若按 `cell_id` 结算了其中一个
+ cell,只从匿名桶拿掉该 cell,不得清空仍在跑的其它匿名 cell。同 turn 或续段里
后续 `wait` 输出 `Script completed` / `Script terminated` 后视为该 cell 已结算,不得
- 再铸 claim,也不得报 lost-handle。禁止把 `last_agent_message == null` 或开场白当结算
+ 再铸 claim,也不得报 lost-handle。Plan Mode 审批只在产品终态跑:存在 awaiting
+ yield claim 时不得把空计划当循环结束,也不得在 SDK `turn/completed` 上提前挂审批;
+ origin 已产出的计划挂在 claim 上,续段结算后再审。禁止把 `last_agent_message == null` 或开场白当结算
判据;cell 跨 turn 存活性未证实前,续段失败必须诚实报 lost-handle,不得 replay 原请求
或重跑已执行命令。续段 claim 一旦挡住产品结束,所有非重试终态错误路径(不限
transport)都必须同步结算它,不能只推 Done 而让 `isTurnRunning()` 仍为 true。
diff --git a/packages/maker-core/src/agents/codex/index.test.ts b/packages/maker-core/src/agents/codex/index.test.ts
index fa17890f6f..b647a3a1c7 100644
--- a/packages/maker-core/src/agents/codex/index.test.ts
+++ b/packages/maker-core/src/agents/codex/index.test.ts
@@ -17589,6 +17589,76 @@ describe('CodexAgent yield continuation', () => {
await handle.close();
});
+ it('keeps other nameless yielded cells when a nameless wait settles one cell', 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-nameless-two-cells-one-settled',
+ 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: {
+ type: 'function_call',
+ name: 'exec_command',
+ arguments: JSON.stringify({ cmd: 'pnpm typecheck', yield_time_ms: 10_000 }),
+ content: [{ type: 'output_text', text: 'Script running with cell ID 226' }],
+ },
+ });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ item: {
+ type: 'function_call',
+ name: 'exec_command',
+ arguments: JSON.stringify({ cmd: 'pnpm lint', yield_time_ms: 10_000 }),
+ content: [{ type: 'output_text', text: 'Script running with cell ID 229' }],
+ },
+ });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ item: {
+ type: 'function_call',
+ name: 'wait',
+ arguments: JSON.stringify({ cell_id: '226', yield_time_ms: 1000 }),
+ content: [{ type: 'output_text', text: 'Script completed\nWall time 1.0 seconds\nOutput:\n' }],
+ },
+ });
+ 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 [, params] = yieldTurnStartCalls(host)[0] as [string, {
+ input: Array<{ text?: string }>;
+ }];
+ const prompt = params.input[0]?.text ?? '';
+ expect(prompt).toContain('cell ID 229');
+ expect(prompt).not.toContain('cell ID 226');
+ await handle.close();
+ });
+
it('does not mint a claim after wait settles the yielded cell in the same turn', async () => {
const agent = new CodexAgent(createDeps());
const host = installFakeHost(agent, (method) => {
@@ -19152,6 +19222,166 @@ describe('CodexAgent yield continuation', () => {
expect(handle.isTurnRunning?.()).toBe(false);
await handle.close();
});
+
+ it('defers plan review until the yield product terminal after an empty plan 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-plan-review-after-product-terminal',
+ model: 'gpt-5.4',
+ workingDir: '/repo',
+ planMode: true,
+ });
+ const handlers = host.getThreadHandlers();
+ if (!handlers?.itemCompleted || !handlers.turnCompleted || !handlers.turnStarted) {
+ throw new Error('expected item and turn handlers');
+ }
+ const seen: unknown[] = [];
+ handle.setInteractionResolver(async (req) => {
+ seen.push(req);
+ return { kind: 'plan_review', behavior: 'deny' };
+ });
+ const events = await collectYieldEvents(handle);
+ await handle.send({ type: 'user', content: 'make a plan' }, { planMode: true });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ item: {
+ id: 'item-exec-plan-yield',
+ 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);
+ });
+ expect(seen).toHaveLength(0);
+ const [, continuationParams] = yieldTurnStartCalls(host)[0] as [string, {
+ collaborationMode?: { mode: string };
+ }];
+ expect(continuationParams.collaborationMode?.mode).toBe('plan');
+ handlers.turnStarted({
+ threadId: 'start-thread-id',
+ turn: { id: 'turn-2', status: 'inProgress' },
+ });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-2',
+ item: { type: 'plan', id: 'turn-2-plan', text: '1. inspect\n2. edit' },
+ } as never);
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-2',
+ item: {
+ id: 'item-wait-plan-yield',
+ 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 vi.waitFor(() => {
+ expect(seen.length).toBeGreaterThan(0);
+ });
+ expect(seen[0]).toMatchObject({ kind: 'plan_review', plan: '1. inspect\n2. edit' });
+ await waitForExpectation(() => {
+ expect(events.filter((event) => event.type === 'done' && event.turnContinuationId == null)).toHaveLength(1);
+ });
+ await handle.close();
+ });
+
+ it('reviews a plan produced before yield on the product terminal, not the SDK turn boundary', 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-plan-before-yield-reviews-at-product-terminal',
+ model: 'gpt-5.4',
+ workingDir: '/repo',
+ planMode: true,
+ });
+ const handlers = host.getThreadHandlers();
+ if (!handlers?.itemCompleted || !handlers.turnCompleted || !handlers.turnStarted) {
+ throw new Error('expected item and turn handlers');
+ }
+ const seen: unknown[] = [];
+ handle.setInteractionResolver(async (req) => {
+ seen.push(req);
+ return { kind: 'plan_review', behavior: 'deny' };
+ });
+ await handle.send({ type: 'user', content: 'make a plan' }, { planMode: true });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ item: { type: 'plan', id: 'turn-1-plan', text: '1. do X\n2. do Y' },
+ } as never);
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-1',
+ item: {
+ id: 'item-exec-plan-before-yield',
+ 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);
+ });
+ expect(seen).toHaveLength(0);
+ handlers.turnStarted({
+ threadId: 'start-thread-id',
+ turn: { id: 'turn-2', status: 'inProgress' },
+ });
+ handlers.itemCompleted({
+ threadId: 'start-thread-id',
+ turnId: 'turn-2',
+ item: {
+ id: 'item-wait-plan-before-yield',
+ 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 vi.waitFor(() => {
+ expect(seen.length).toBeGreaterThan(0);
+ });
+ expect(seen[0]).toMatchObject({ kind: 'plan_review', plan: '1. do X\n2. do Y' });
+ 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 dcb2c645bb..b070e38320 100644
--- a/packages/maker-core/src/agents/codex/index.ts
+++ b/packages/maker-core/src/agents/codex/index.ts
@@ -1184,6 +1184,9 @@ type YieldContinuationClaim = {
originTurnId: string;
continuationTurnId: string | null;
permissionPolicy: TurnPermissionPolicy | null;
+ deferredPlanText: string | null;
+ deferredPlanTurnId: string | null;
+ deferredPlanCapabilitySelectionText: string;
};
const SYSTEM_PLAN_REVIEW_DISMISSAL_REASONS = new Set([
'no_listener_attached',
@@ -3211,9 +3214,13 @@ export class CodexAgent extends BaseAgent {
if (phase === 'completed') {
// Named completions drop that item. Nameless completions cannot match
// a prior updated snapshot, so they clear the anonymous bucket rather
- // than invent a synthetic identity.
+ // than invent a synthetic identity — unless this is a wait that settled
+ // one cell_id. That wait shares the anonymous bucket with other yielded
+ // execs; wiping '' would drop still-running cells and skip their claim.
if (itemId) existing.delete(itemId);
- else existing.delete('');
+ else if (extractSettledYieldCellIdsFromCodexItem(item).length === 0) {
+ existing.delete('');
+ }
}
} else if (itemId) {
existing.set(itemId, cells);
@@ -3257,6 +3264,9 @@ export class CodexAgent extends BaseAgent {
originTurnId,
continuationTurnId: null,
permissionPolicy: activeTurnPermissionPolicy,
+ deferredPlanText: null,
+ deferredPlanTurnId: null,
+ deferredPlanCapabilitySelectionText: '',
};
yieldContinuationClaims.set(claim.id, claim);
activeYieldContinuationId = claim.id;
@@ -3318,6 +3328,10 @@ export class CodexAgent extends BaseAgent {
emitYieldContinuationState(claim.id, 'cancelled');
releaseSettledYieldContinuationClaim(claim);
flushYieldContinuationIdleWaiters(true);
+ // Cancel is a product terminal. Do not leak a deferred plan cycle into the
+ // next user send after Stop / owned-turn failure / replacement send.
+ planCycleActive = false;
+ proposedPlanText = null;
return claim;
};
const discardYieldContinuationClaims = (reason: string): void => {
@@ -9069,6 +9083,10 @@ export class CodexAgent extends BaseAgent {
suppressSuccessfulYieldBoundary = true;
} else {
yieldClaim = mintYieldContinuationClaim(outstandingCells, turn.id, retryCount);
+ yieldClaim.deferredPlanText = existingYieldClaim.deferredPlanText;
+ yieldClaim.deferredPlanTurnId = existingYieldClaim.deferredPlanTurnId;
+ yieldClaim.deferredPlanCapabilitySelectionText =
+ existingYieldClaim.deferredPlanCapabilitySelectionText;
}
} else if (yieldedCells.length > 0) {
yieldClaim = mintYieldContinuationClaim(yieldedCells, turn.id, 0);
@@ -9118,24 +9136,47 @@ export class CodexAgent extends BaseAgent {
}
latestPlanByTurn.delete(turn.id);
if (yieldClaim?.state === 'awaiting') {
+ if (completedTurnWasPlanMode && planCycleActive) {
+ const livePlan = proposedPlanText?.trim() || null;
+ if (livePlan) {
+ yieldClaim.deferredPlanText = livePlan;
+ yieldClaim.deferredPlanTurnId = turn.id;
+ yieldClaim.deferredPlanCapabilitySelectionText = completedCapabilitySelectionText;
+ } else if (!yieldClaim.deferredPlanText) {
+ yieldClaim.deferredPlanTurnId = turn.id;
+ yieldClaim.deferredPlanCapabilitySelectionText = completedCapabilitySelectionText;
+ }
+ }
+ proposedPlanText = null;
void startYieldContinuation(yieldClaim);
- }
- // 计划模式: plan turn 正常收尾且产出了 proposed plan → 发起审批闭环。
- // 放在 done 之后 (AsyncQueue FIFO), renderer 先做完 turn 收尾再挂 plan 卡片。
- // 空跑(模型没产出 <proposed_plan>, 例如直接回答了问题) → 循环结束,
- // 下一 turn 经 threadTouchedPlanMode 复位 default。
- const planForReview = proposedPlanText?.trim();
- proposedPlanText = null;
- if (completedTurnWasPlanMode && planCycleActive) {
- if (planForReview) {
- void runPlanReviewFlow(
- planForReview,
- turn.id,
- completedCapabilitySelectionText,
- );
- } else {
- log.debug('plan turn produced no proposed plan — plan cycle ends', { turnId: turn.id });
- planCycleActive = false;
+ } else {
+ // 计划模式: 只在产品终态审批。awaiting yield claim 时 SDK turn 边界不是
+ // 产品结束 —— 空跑不得把 planCycleActive 关掉,已有计划也等续段结算后再挂卡。
+ // 放在 done 之后 (AsyncQueue FIFO), renderer 先做完 turn 收尾再挂 plan 卡片。
+ // 空跑(模型没产出 <proposed_plan>, 例如直接回答了问题) → 循环结束,
+ // 下一 turn 经 threadTouchedPlanMode 复位 default。
+ const livePlan = proposedPlanText?.trim() || null;
+ proposedPlanText = null;
+ const deferredPlan = existingYieldClaim?.deferredPlanText?.trim() || null;
+ const planForReview = livePlan ?? deferredPlan;
+ const planReviewTurnId = livePlan
+ ? turn.id
+ : (existingYieldClaim?.deferredPlanTurnId ?? turn.id);
+ const planCapabilitySelection = livePlan
+ ? completedCapabilitySelectionText
+ : (existingYieldClaim?.deferredPlanCapabilitySelectionText
+ || completedCapabilitySelectionText);
+ if (planCycleActive && (completedTurnWasPlanMode || deferredPlan)) {
+ if (planForReview) {
+ void runPlanReviewFlow(
+ planForReview,
+ planReviewTurnId,
+ planCapabilitySelection,
+ );
+ } else {
+ log.debug('plan turn produced no proposed plan — plan cycle ends', { turnId: turn.id });
+ planCycleActive = false;
+ }
}
}
flushDeferredTerminalTurnCompletionsIfIdle();