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): broadcast session status changes to all windows (#3206)

Tsai Yuan3ce3d4161dd0
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
index 69abcd89b7..16b3024215 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
@@ -8,6 +8,10 @@
*
* 通过 mock electron ipcMain 捕获真实 handler + 内存 sqlite 全列 sessions 表做集成断言。
*/
+import { mkdtempSync, rmSync } from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+
import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest';
@@ -31,6 +35,7 @@ const h = vi.hoisted(() => ({
routeLock: vi.fn(async <T>(_sessionId: string, task: () => Promise<T>): Promise<T> =>
task(),
) as SessionRouteLockMock,
+ userDataDir: null as string | null,
}));
vi.mock('electron', () => ({
@@ -40,6 +45,11 @@ vi.mock('electron', () => ({
}),
},
BrowserWindow: { getAllWindows: () => [] },
+ // status 写路径(removeHookAttachmentDir / removeTurnChangeSetsForSession)会调
+ // app.getPath('userData') 并对真实文件系统做 fire-and-forget fs.rm。这里返回每次
+ // 测试用 mkdtemp 生成的独立目录,避免并发 worktree 共享同一字面量路径互相删 fixture,
+ // 也避免 Windows 把 POSIX 字面量解析成盘符根相对路径。
+ app: { getPath: () => h.userDataDir },
}));
vi.mock('../../../logger', () => ({
createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
@@ -168,6 +178,7 @@ beforeEach(() => {
h.relocate.mockImplementation(async () => ({ persistedSdkSessionId: null }));
h.routeLock.mockImplementation(async (_sessionId, task) => task());
h.handlers.clear();
+ h.userDataDir = mkdtempSync(path.join(os.tmpdir(), 'cindy-sessions-update-'));
createDb();
setSessionRouteLockImplementation(h.routeLock);
registerSessionIpc();
@@ -175,6 +186,11 @@ beforeEach(() => {
afterEach(() => {
setSessionRouteLockImplementation(null);
+ const dir = h.userDataDir;
+ if (dir) {
+ rmSync(dir, { recursive: true, force: true });
+ h.userDataDir = null;
+ }
});
describe('local-db:sessions:update handler wiring', () => {
@@ -233,6 +249,24 @@ describe('local-db:sessions:update handler wiring', () => {
);
});
+ it('broadcasts status-only patches so secondary windows converge on delete', async () => {
+ await invokeUpdate('cc-local', { status: 'deleted' });
+
+ const persisted = h
+ .sqlite!.prepare('SELECT status FROM sessions WHERE id = ?')
+ .get('cc-local') as { status: string };
+ expect(persisted.status).toBe('deleted');
+ // #3175 回归:纯 status 变化(无 title / settings / project / pinnedAt)也必须广播
+ // sessions:patched,否则「在新窗口打开」的副窗口收不到删除,仍停留在旧视图。
+ expect(h.tapWindowBroadcast).toHaveBeenCalledWith(
+ 'local-db:sessions:patched',
+ expect.objectContaining({
+ sessionId: 'cc-local',
+ patch: { status: 'deleted' },
+ }),
+ );
+ });
+
it('broadcasts pin and unpin patches to device-link subscribers', async () => {
const pinnedAt = '2026-08-03T04:08:26.000Z';
await invokeUpdate('codex-local', { pinnedAt });
diff --git a/apps/desktop/src/main/localDb/ipc/sessions.ts b/apps/desktop/src/main/localDb/ipc/sessions.ts
index b3e44b385e..2e55ec60e9 100644
--- a/apps/desktop/src/main/localDb/ipc/sessions.ts
+++ b/apps/desktop/src/main/localDb/ipc/sessions.ts
@@ -1498,6 +1498,9 @@ export function registerSessionIpc(
const projectTargetChanged = p.workspaceKind !== undefined || p.workingDir !== undefined;
const settingsChanged = Object.keys(p).some((key) => REMOTE_PERSIST_FIELDS.has(key));
const titleChanged = p.title !== undefined;
+ // 归档/删除这类纯 status 变化也要广播:本机多窗口收敛靠 sessions:patched,
+ // 否则「在新窗口打开」的副窗口无从得知会话已被移除,仍停留在旧视图(#3175)。
+ const statusChanged = p.status !== undefined;
if (
projectTargetChanged &&
row.workspaceKind === 'project' &&
@@ -1506,7 +1509,13 @@ export function registerSessionIpc(
) {
await upsertRecentWorkdir(row.workingDir, Date.now());
}
- if (projectTargetChanged || settingsChanged || titleChanged || p.pinnedAt !== undefined) {
+ if (
+ projectTargetChanged ||
+ settingsChanged ||
+ titleChanged ||
+ statusChanged ||
+ p.pinnedAt !== undefined
+ ) {
if (isOwnerScopeCurrent(ownerScope)) {
broadcastSessionPatched(sid, broadcastPatch, ownerScope);
}