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。

修复移动会话后继续使用旧工作目录

nothingyuancando613644e40e26
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..95e0adef4c 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
@@ -25,6 +25,7 @@ const h = vi.hoisted(() => ({
relocate: vi.fn(async (): Promise<{ persistedSdkSessionId: string | null }> => ({
persistedSdkSessionId: null,
})),
+ closeSession: vi.fn(async () => undefined),
tapWindowBroadcast: vi.fn(),
summarizeSession: vi.fn(async () => undefined),
setPinnedSectionCardMode: vi.fn(),
@@ -70,6 +71,9 @@ vi.mock('../../../sessionIds', () => ({ resolveBusinessSessionId: (id: string) =
vi.mock('../../../maker-host/claude-transcript-relocation.js', () => ({
relocateClaudeTranscriptsForSessionMove: h.relocate,
}));
+vi.mock('../../../maker-host/index.js', () => ({
+ getMakerIfReady: () => ({ closeSession: h.closeSession }),
+}));
import { registerSessionIpc } from '../sessions';
import { setSessionRouteLockImplementation } from '../../sessionRouteLock';
@@ -166,6 +170,7 @@ async function invokeUpdate(id: string, patch: Record<string, unknown>): Promise
beforeEach(() => {
vi.clearAllMocks();
h.relocate.mockImplementation(async () => ({ persistedSdkSessionId: null }));
+ h.closeSession.mockClear();
h.routeLock.mockImplementation(async (_sessionId, task) => task());
h.handlers.clear();
createDb();
@@ -341,11 +346,21 @@ describe('local-db:sessions:update handler wiring', () => {
it('does nothing for codex sessions', async () => {
await invokeUpdate('codex-local', { workingDir: '/new/dir' });
expect(h.relocate).not.toHaveBeenCalled();
+ expect(h.closeSession).toHaveBeenCalledWith('codex-local');
+ });
+
+ it('closes a local Pi runtime before moving its working directory', async () => {
+ h.sqlite!.prepare('UPDATE sessions SET agent_kind = ? WHERE id = ?').run('pi', 'codex-local');
+
+ await invokeUpdate('codex-local', { workingDir: '/new/dir' });
+
+ expect(h.closeSession).toHaveBeenCalledWith('codex-local');
});
it('does nothing for remote sessions', async () => {
await invokeUpdate('cc-remote', { workingDir: '/new/dir' });
expect(h.relocate).not.toHaveBeenCalled();
+ expect(h.closeSession).not.toHaveBeenCalled();
});
});
diff --git a/apps/desktop/src/main/localDb/ipc/sessions.ts b/apps/desktop/src/main/localDb/ipc/sessions.ts
index 9c6c6eed73..1bf0a940c3 100644
--- a/apps/desktop/src/main/localDb/ipc/sessions.ts
+++ b/apps/desktop/src/main/localDb/ipc/sessions.ts
@@ -1410,6 +1410,21 @@ export function registerSessionIpc(
.where(eq(sessions.id, sid))
)[0]
: undefined;
+ const movingLocalNonClaudeSession =
+ beforeMove &&
+ beforeMove.agentKind !== 'cc' &&
+ !beforeMove.remoteHostId &&
+ beforeMove.workingDir &&
+ typeof p.workingDir === 'string' &&
+ p.workingDir &&
+ normalizeWorkingDirForStorage(beforeMove.workingDir) !== p.workingDir;
+ // Pi/Codex keep a live Maker handle whose cwd is fixed at bootstrap. Close it
+ // before persisting the new directory so the next send lazily recreates the
+ // runtime with the moved session's cwd instead of continuing in the old one.
+ if (movingLocalNonClaudeSession) {
+ const makerHost = await import('../../maker-host/index.js');
+ await makerHost.getMakerIfReady()?.closeSession(sid);
+ }
// 只有纯设置字段(model/effort 等)才跳过 bump;凡带 activity 字段
// (clearedAt / sdkSessionId / status / token 用量等)仍需更新 updatedAt,
// 否则本地 /clear 后重启侧栏时间回退旧值。