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): 只在运行时自行关闭时唤醒排队消息

Dash2c5751d6e31e
diff --git a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts
index f556c5229e..ae6add32b5 100644
--- a/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts
+++ b/apps/desktop/src/main/__tests__/makerEventHotPathOrdering.test.ts
@@ -498,6 +498,21 @@ describe('maker:event hot path ordering', () => {
);
});
+ it('wakes queued input only for runtime self-closes, not direct product closes', () => {
+ const closedBlock = extractSessionCloseAdaptersSource();
+
+ // 'unexpected' only means no Maker reason was recorded; IM /new and archive
+ // sync close sessions directly and must not resurrect the old task's queue.
+ expect(closedBlock).toContain(
+ "wakeQueuedInput: context.closeReason === 'unexpected' && session.wasClosedByRuntime(),",
+ );
+ expectOrder(
+ closedBlock,
+ 'wakeQueuedInput:',
+ 'agentInputCoordinatorHolder?.onSessionClosed(session.id, options);',
+ );
+ });
+
it('preserves coordinator input boundary inside the rehydrate suppression window (#1930)', () => {
const closedBlock = extractSessionCloseAdaptersSource();
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index cbf1bec88f..a874124d6d 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -5332,7 +5332,10 @@ const sessionBindings = createSessionBindingLifecycle<WiredSession, WiredSession
...boundaryOptions,
// Requested / rebuild closes own their follow-up; a self-close has no
// done event, so queued user input would otherwise wait forever.
- wakeQueuedInput: context.closeReason === 'unexpected',
+ // 'unexpected' only means no Maker reason was recorded: direct product
+ // closes (IM /new, archive sync) also land there, so require the
+ // runtime's own self-close signal before resurrecting the queue.
+ wakeQueuedInput: context.closeReason === 'unexpected' && session.wasClosedByRuntime(),
};
agentInputCoordinatorHolder?.onSessionClosed(session.id, options);
},
diff --git a/packages/maker-core/src/maker.test.ts b/packages/maker-core/src/maker.test.ts
index fce31a195f..0665188742 100644
--- a/packages/maker-core/src/maker.test.ts
+++ b/packages/maker-core/src/maker.test.ts
@@ -1414,6 +1414,7 @@ describe('Maker session close events', () => {
if (event.type === 'session:closed' && event.sessionId === 'session-crash') {
expect(event.reason).toBe('unexpected');
expect(maker.getSessionCloseReason(event.session)).toBe('unexpected');
+ expect(event.session.wasClosedByRuntime()).toBe(true);
resolveClosed();
}
});
@@ -3521,6 +3522,7 @@ describe('Session turn send guard', () => {
await closed;
expect(vi.getTimerCount()).toBe(0);
expect(terminalErrors).toEqual([]);
+ expect(session.wasClosedByRuntime()).toBe(true);
} finally {
vi.useRealTimers();
}
@@ -3638,6 +3640,32 @@ describe('Session turn send guard', () => {
await closePromise;
expect(session.getStatus()).toBe('closed');
expect(handle.close).toHaveBeenCalledTimes(1);
+ // An external close that began first keeps ownership of the cause.
+ expect(session.wasClosedByRuntime()).toBe(false);
+ });
+
+ it('does not report direct close() or closeIfIdle() as a runtime self-close', async () => {
+ const createIdleSession = (id: string) => {
+ const handle = createHandle({ id: `thread-${id}` });
+ handle.close = vi.fn(async () => undefined);
+ return new Session({
+ id,
+ agentKind: 'codex',
+ workDir: '/repo',
+ handle,
+ capabilities: createAgent(async () => handle).capabilities,
+ logger: createLogger(),
+ });
+ };
+ const direct = createIdleSession('session-direct-close');
+ await direct.close();
+ expect(direct.getStatus()).toBe('closed');
+ expect(direct.wasClosedByRuntime()).toBe(false);
+
+ const idle = createIdleSession('session-close-if-idle');
+ await expect(idle.closeIfIdle()).resolves.toBe(true);
+ expect(idle.getStatus()).toBe('closed');
+ expect(idle.wasClosedByRuntime()).toBe(false);
});
it('does not revive a closed session when abort is called after the event iterator crashes', async () => {
diff --git a/packages/maker-core/src/session.ts b/packages/maker-core/src/session.ts
index 3600a4e9c2..715efae3f7 100644
--- a/packages/maker-core/src/session.ts
+++ b/packages/maker-core/src/session.ts
@@ -488,6 +488,13 @@ export class Session {
private closePromise: Promise<void> | null = null;
/** close/detach 一进入同步入口就置位,早于底层 handle 的异步关闭完成。 */
private terminationStarted = false;
+ /**
+ * The runtime itself began this teardown (dead handle, crashed loop, failed
+ * dispatch, abort / terminal-error recovery). External close()/closeIfIdle()/
+ * detach() callers never set it, so a direct product close is not mistaken
+ * for a self-close even when no Maker close reason was recorded.
+ */
+ private runtimeInitiatedClose = false;
private eventLoopStarted = false;
/**
* 最近一次已观察到的终态事件属于哪个 turn generation;事件迭代器可能在上一轮
@@ -1039,7 +1046,7 @@ export class Session {
this.pendingPriorGeneration = null;
originInstalled = false;
}
- await this.close();
+ await this.closeFromRuntime();
}
throw e;
} finally {
@@ -1421,6 +1428,17 @@ export class Session {
return this.terminationStarted;
}
+ /** True when this runtime, not an external caller, started its own teardown. */
+ wasClosedByRuntime(): boolean {
+ return this.runtimeInitiatedClose;
+ }
+
+ /** Self-close: only claims the cause when no teardown has started yet. */
+ private closeFromRuntime(): Promise<void> {
+ if (!this.terminationStarted && this.status !== 'closed') this.runtimeInitiatedClose = true;
+ return this.close();
+ }
+
/**
* Ordinary close. Without an explicit reason this is navigation: the account
* and its database are unchanged, so an adapter's detached work survives.
@@ -3062,7 +3080,7 @@ export class Session {
generation,
graceMs: TERMINAL_ERROR_DRAIN_GRACE_MS,
});
- void this.close().catch((error) => {
+ void this.closeFromRuntime().catch((error) => {
this.logger.warn('terminal error drain close failed', { error: String(error) });
});
}, TERMINAL_ERROR_DRAIN_GRACE_MS);
@@ -3303,7 +3321,7 @@ export class Session {
{ trigger: ctx.trigger, graceMs: ctx.graceMs },
);
try {
- await this.close();
+ await this.closeFromRuntime();
} catch (e) {
this.logger.warn('abort recovery close failed', { trigger: ctx.trigger, error: String(e) });
}
@@ -3356,7 +3374,7 @@ export class Session {
this.logger.error('event loop crashed', { error: String(e) });
if (this.closePromise || this.status === 'closed') return;
// 先占住 closing gate,避免 terminal error listener 在死掉的 iterator 上重新 send。
- const closing = this.close();
+ const closing = this.closeFromRuntime();
if (this.terminalEventObservedGeneration !== this.turnGeneration) {
this.fanOutEvent({
type: 'error',
@@ -3393,6 +3411,7 @@ export class Session {
const unfinishedTurn =
this.hasUnsettledTurn();
this.logger.debug('event loop ended (handle dead), auto-closing session', { unfinishedTurn });
+ this.runtimeInitiatedClose = true;
this.terminationStarted = true;
this.closePromise = Promise.resolve();
this.settleUnfinishedTurn('Session event loop stopped unexpectedly without a terminal event');