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(desktop): 允许 recovery 状态手动压缩 (#940)

DavidShenXDa3d128ed5e6a
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
index 1a8059567f..865bf7c9bc 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/agent-input-coordinator.test.ts
@@ -2079,7 +2079,7 @@ describe('AgentInputCoordinator send transaction', () => {
expect(projection.pendingQueue.map((q) => q.clientId)).toEqual(['q-head', 'q-second']);
});
- it('does not clear an existing recovery when compact is requested', async () => {
+ it('does not clear a queue-head recovery when compact is requested', async () => {
const h = createHarness();
const sid = 'compact-preserves-recovery';
const failed = makeItem('q-failed', 'failed');
@@ -2101,6 +2101,63 @@ describe('AgentInputCoordinator send transaction', () => {
expect(after.errorRetryText).toBe('failed');
});
+ it('abandons an idle active-turn recovery and dispatches compact immediately', async () => {
+ const h = createHarness();
+ const sid = 'compact-abandons-idle-active-turn-recovery';
+
+ h.coordinator.enqueue(sid, makeItem('q-failed', 'failed'));
+ await flush();
+
+ h.setRunning(false);
+ h.coordinator.onTurnEvent(sid, 'error', 'context window exhausted');
+ await flush();
+ expect(latestProjection(h.projections).recovery?.kind).toBe('active-turn');
+
+ await h.coordinator.compact(sid, makeItem('q-compact', 'ignored').createOpts);
+ await flush();
+
+ const projection = latestProjection(h.projections);
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).toEqual({ type: 'user', content: '/compact' });
+ expect(projection.error).toBeNull();
+ expect(projection.recovery).toBeNull();
+
+ h.setRunning(false);
+ h.coordinator.onTurnEvent(sid, 'done');
+ await h.coordinator.retryLastError(sid);
+ await flush();
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ });
+
+ it('queues compact after abandoning active-turn recovery while the dispatch boundary is still busy', async () => {
+ const h = createHarness();
+ const sid = 'compact-queues-after-active-turn-recovery';
+
+ h.coordinator.enqueue(sid, makeItem('q-failed', 'failed'));
+ await flush();
+
+ h.coordinator.onTurnEvent(sid, 'error', 'context window exhausted');
+ await flush();
+ expect(latestProjection(h.projections).recovery?.kind).toBe('active-turn');
+
+ await h.coordinator.compact(sid, makeItem('q-compact', 'ignored').createOpts);
+ await flush();
+
+ let projection = latestProjection(h.projections);
+ expect(h.sendToAgent).toHaveBeenCalledTimes(1);
+ expect(projection.error).toBeNull();
+ expect(projection.recovery).toBeNull();
+
+ h.setRunning(false);
+ h.coordinator.onTurnEvent(sid, 'done');
+ await flush();
+
+ projection = latestProjection(h.projections);
+ expect(h.sendToAgent).toHaveBeenCalledTimes(2);
+ expect(h.sendToAgent.mock.calls[1]?.[1]).toEqual({ type: 'user', content: '/compact' });
+ expect(projection.recovery).toBeNull();
+ });
+
it('wakes queued turns after compact dispatch failure releases the active turn', async () => {
const h = createHarness();
const sid = 'compact-failure-wakes-queue';
diff --git a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
index 3c8a3099b4..840e77dbb9 100644
--- a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
+++ b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@@ -772,7 +772,7 @@ export class AgentInputCoordinator {
clientId: item.clientId,
});
}
- this.abandonActiveTurnRecoveryForNewInput(state);
+ this.abandonActiveTurnRecoveryForUserAction(state);
this.clearErrorUnlessQueueHeadBlocked(state);
// 用户点「继续任务」表达的是恢复刚才中断/失败的 turn,必须先于此前
// 已排队的新任务执行;普通 composer / Orca / scheduler 输入仍保持 FIFO。
@@ -817,6 +817,11 @@ export class AgentInputCoordinator {
async compact(sessionId: string, createOpts: AgentInputCreateOpts, opts?: { userName?: string }): Promise<AgentInputProjection> {
const state = this.getState(sessionId);
+ // 手动压缩与发送新消息一样,都是用户对失败 turn 的明确后续选择:
+ // 放弃 active-turn retry,让 /compact 在真实 dispatch boundary 空闲时立即执行,
+ // 仍忙时则进入 pendingCompacts。queue-head recovery 表示消息从未受理且仍在
+ // 队首,不能越过它静默改变顺序,继续保留原阻塞语义。
+ this.abandonActiveTurnRecoveryForUserAction(state);
if (state.recovery) {
log.info('compact ignored while dispatch boundary is busy', { sessionId });
state.error = 'Cannot compact while the session is busy';
@@ -2470,7 +2475,7 @@ export class AgentInputCoordinator {
private fallbackPreparedAsTurn(sessionId: string, item: AgentInputQueuedMessage, removeFromQueue: boolean): void {
const state = this.getState(sessionId);
// 插话回落成普通派发 = 也是一条新用户输入,同 enqueue 放弃 active-turn 重试。
- this.abandonActiveTurnRecoveryForNewInput(state);
+ this.abandonActiveTurnRecoveryForUserAction(state);
this.clearErrorUnlessQueueHeadBlocked(state, item.clientId);
state.queuePaused = false;
state.steeringQueueClientIds = state.steeringQueueClientIds.filter((id) => id !== item.clientId);
@@ -2563,12 +2568,14 @@ export class AgentInputCoordinator {
}
/**
- * 新用户输入放弃 active-turn 重试入口(2026-07-13 Lizi 拍板)。错误的本质是
+ * 用户后续动作放弃 active-turn 重试入口。错误的本质是
* "上一条消息执行失败了",用户此后**主动发出的新消息**(composer 发送 / 插话
* 回落派发)就是对"要不要重试"的表态 —— 清掉错误横幅与重试入口,让新消息正常
* 派发,不再默默排队等一个可能没被注意到的重试按钮。失败消息已落库、仍在会话
* 里可手动重发;「重试」按钮与新输入赛跑时后到的 retryLastError 读到
* recovery=null 即 no-op,无双发风险。
+ * 手动 compact 同样表达"先压缩而非重试上一轮":必须先清 recovery,再按真实
+ * dispatch boundary 决定立即派发或排队,否则上下文耗尽后的空闲会话会被误报 busy。
*
* 边界(刻意不收进来的入口):
* - resume(继续队列)**不**放弃:Stop 中断留下的 recovery 可能是"已落库但从未
@@ -2582,7 +2589,7 @@ export class AgentInputCoordinator {
* 新消息是对会话现状的明确表态,三类来源一视同仁(2026-07-13 拍板口径
* "上一条消息失败了 → 新消息 = 不重试",Stop/派发失败同属"没执行成功")。
*/
- private abandonActiveTurnRecoveryForNewInput(state: SessionInputState): void {
+ private abandonActiveTurnRecoveryForUserAction(state: SessionInputState): void {
if (state.recovery?.kind !== 'active-turn') return;
state.error = null;
state.stickyError = null;