Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 目标目录不可用时拒绝在旧 cwd 发消息
Zhang Yunjin903d30c1a632
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.tsindex 806aa85772..0deceed989 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts@@ -1200,9 +1200,10 @@ describe('maker SEND transaction', () => {expect(rebuiltSession.send).toHaveBeenCalled();});- it('keeps the live runtime when the persisted directory is not actually on disk', async () => {+ it('refuses to send in the live cwd when the persisted directory is not actually on disk', async () => {// 只有 DB 目录真实存在才迁移:不存在时不能走 recovery/mkdir 把不存在的项目- // "恢复"成空文件夹,也不能丢掉活 runtime 的上下文。+ // "恢复"成空文件夹。但也不能反过来在旧 cwd 里执行 —— UI 显示的是新项目,消息却会+ // 改旧项目的代码(正是本 PR 要消灭的半移动),所以这里明确失败。const movedSession = createSession({ workDir: '/data/old-project' });const { deps } = createDeps({getSession: () => movedSession,@@ -1212,16 +1213,17 @@ describe('maker SEND transaction', () => {await expect(createMakerSendTransaction(deps).sendToAgentAccepted('session-1', 'hello', {agentKind: 'claude-code', workingDir: '/data/old-project',- })).resolves.toMatchObject({ accepted: true });+ })).resolves.toMatchObject({ accepted: false, reason: 'WORKDIR_MISSING' });expect(deps.closeSession).not.toHaveBeenCalled();expect(deps.bootstrapSession).not.toHaveBeenCalled();- expect(movedSession.send).toHaveBeenCalled();+ expect(movedSession.send).not.toHaveBeenCalled();});- it('keeps the recovery fallback instead of switching a live runtime to the raw persisted path', async () => {+ it('never switches a live runtime to the raw persisted path that recovery took over', async () => {// DB 目录已被 workingDirectoryRecovery 接管(resolve 返回 fallback):文件在- // fallback 里,不能因为原路径"存在"就把 session 拉回去。+ // fallback 里,不能因为原路径"存在"就把 session 拉回去;live 又不在那个 fallback+ // 里,所以这次也不能拿 live 的目录顶替 —— 两边都不是会话现在的目录。const movedSession = createSession({ workDir: '/data/old-project' });const { deps } = createDeps({getSession: () => movedSession,@@ -1233,16 +1235,36 @@ describe('maker SEND transaction', () => {await expect(createMakerSendTransaction(deps).sendToAgentAccepted('session-1', 'hello', {agentKind: 'codex', workingDir: '/data/old-project',- })).resolves.toMatchObject({ accepted: true });+ })).resolves.toMatchObject({ accepted: false, reason: 'WORKDIR_MISSING' });expect(deps.closeSession).not.toHaveBeenCalled();expect(deps.bootstrapSession).not.toHaveBeenCalled();- expect(movedSession.send).toHaveBeenCalled();+ expect(movedSession.send).not.toHaveBeenCalled();});- it('keeps the live runtime while the persisted managed worktree is not ready yet', async () => {+ it('keeps sending when the live runtime already sits in the recovery fallback', async () => {+ // 恢复流程的正常形态:DB 仍写着原路径,但 live 已经重建在 fallback 里 —— 会话就在+ // 这个目录,不能因为 DB 路径不可用就拒绝发消息。+ const recoveredSession = createSession({ workDir: '/userData/fallback' });+ const { deps } = createDeps({+ getSession: () => recoveredSession,+ readSessionWorkingDirFromDb: vi.fn(async () => '/mnt/disk/project'),+ resolveRecoveredWorkingDir: (_id, dir) =>+ (dir === '/mnt/disk/project' ? '/userData/fallback' : dir),+ statDirectory: vi.fn(async () => ({ isDirectory: () => false })),+ });++ await expect(createMakerSendTransaction(deps).sendToAgentAccepted('session-1', 'hello', {+ agentKind: 'codex', workingDir: '/userData/fallback',+ })).resolves.toMatchObject({ accepted: true });++ expect(recoveredSession.send).toHaveBeenCalled();+ });++ it('refuses to send while the persisted managed worktree is not ready yet', async () => {// 托管 worktree 的"存在"不等于 ready(快照 apply 未完成 / 上一轮 apply 冲突会留- // 目录并阻塞):此时不能先关掉旧 runtime 再在重建时报 WORKDIR_MISSING。+ // 目录并阻塞):这时既不能先关掉旧 runtime 再在重建时报 WORKDIR_MISSING,也不能+ // 在旧 cwd(主仓)里执行 —— 会话属于那个 worktree,消息不该落到别处。const liveSession = createSession({ workDir: '/data/old-project' });const worktreeDir = '/repo/.cindy-worktrees/steady-goodall';const { deps } = createDeps({@@ -1255,12 +1277,12 @@ describe('maker SEND transaction', () => {await expect(createMakerSendTransaction(deps).sendToAgentAccepted('session-1', 'hello', {agentKind: 'codex', workingDir: '/data/old-project',- })).resolves.toMatchObject({ accepted: true });+ })).resolves.toMatchObject({ accepted: false, reason: 'WORKDIR_MISSING' });expect(deps.statDirectory).not.toHaveBeenCalledWith(worktreeDir);expect(deps.closeSession).not.toHaveBeenCalled();expect(deps.bootstrapSession).not.toHaveBeenCalled();- expect(liveSession.send).toHaveBeenCalled();+ expect(liveSession.send).not.toHaveBeenCalled();});it('rebuilds into a ready persisted managed worktree after cwd drift', async () => {diff --git a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts b/apps/desktop/src/main/maker-ipc/makerSendTransaction.tsindex 92e6f08da7..3dd018f23d 100644--- a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts+++ b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts@@ -1002,11 +1002,19 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): MakerecoveredDir !== sess.workDir ||((sess.agentKind === 'claude-code' || sess.agentKind === 'pi') &&!!deps.peekWorkingDirectoryRecoveryNote?.(sessionId, sess.workDir));+ // 判漂移比的是「会话现在到底在哪个目录」:DB 值被 workingDirectoryRecovery+ // 接管时文件在 fallback 里,resolve 出来的才是它,而 live 已经就在 fallback+ // 里就不算漂移 —— 否则恢复中的会话会被下面的「目录不可用」判成不能发消息。+ const persistedDir = fallbackDir && !sess.remoteHostId+ ? deps.resolveRecoveredWorkingDir?.(sessionId, fallbackDir) ?? fallbackDir+ : null;+ const persistedDirDrifted =+ !!persistedDir && !workingDirEquals(persistedDir, sess.workDir);const persistedDirReady =- ok && !liveDirNeedsRecovery && fallbackDir && !workingDirEquals(fallbackDir, sess.workDir)+ ok && !liveDirNeedsRecovery && persistedDirDrifted? await isUsablePersistedWorkingDir(sessionId,- fallbackDir,+ fallbackDir!,sess.agentKind,sess.remoteHostId,)@@ -1044,6 +1052,23 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): Make`working directory is missing for session ${sessionId}`,),);+ } else if (persistedDirDrifted) {+ // 会话已经被移到别的目录(DB 是权威),但那个目录当前不可用,而旧 runtime 还活+ // 着:绝不能在旧 cwd 里执行 —— UI 显示新项目、消息却改旧项目,正是本 PR 要消灭的+ // 半移动(2026-09-13 实报的同一类问题)。明确失败,由用户把目录找回来、或再移动+ // 一次会话后重发;这条分支也覆盖「托管 worktree 尚未就绪」与「DB 目录已被+ // recovery 接管但 live 不在 fallback」——两者都不能拿旧目录顶替。+ deps.log.warn('send: persisted working dir unavailable, refusing to send in the live cwd', {+ sessionId,+ liveWorkingDir: sess.workDir,+ persistedWorkingDir: fallbackDir,+ });+ return toCompatibleMakerSendResult(+ createHostSendFailure(+ 'WORKDIR_MISSING',+ `working directory is missing for session ${sessionId}`,+ ),+ );}if (!deps.isOrcaMcpHydrated(sessionId) && createOpts) {const co = deps.buildCreateOptsWithStderr({