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): lazy-create 以 DB working_dir 为准,避免沿用旧目录快照

Zhang Yunjindbe98f7b37fa
diff --git a/apps/desktop/src/main/maker-host/session-storage.ts b/apps/desktop/src/main/maker-host/session-storage.ts
index e83a4abbe8..db3614eb3d 100644
--- a/apps/desktop/src/main/maker-host/session-storage.ts
+++ b/apps/desktop/src/main/maker-host/session-storage.ts
@@ -191,10 +191,10 @@ export async function writeCodexHistoryHasProductPrompt(
}
/**
- * 读 sessions.working_dir(既有会话的权威值)。SEND lazy-create / rehydrate 用它
- * 兜底 caller 传入的陈旧 createOpts.workingDir(典型:输入队列崩溃快照里内嵌的
- * 老路径,启动 sweep 改写 DB 后快照回放仍带旧值,2026-07-20 实报)。
- * 行不存在 / 空值 → null,不抛错。
+ * 读 sessions.working_dir(既有会话的权威值)。SEND lazy-create 把它当唯一真源直接
+ * 采纳(caller 传入的 createOpts.workingDir 可能是陈旧快照 —— 输入队列崩溃回放里
+ * 内嵌的老路径,或用户把任务移走后排队/重试项里内嵌的旧目录);rehydrate 在 caller
+ * 目录校验失败时用它兜底。行不存在 / 空值 → null,不抛错。
*/
export async function readSessionWorkingDirFromDb(id: string): Promise<string | null> {
const db = getDbClient().drizzle;
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
index 34c250debc..afb34f525d 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
@@ -1344,15 +1344,83 @@ describe('maker SEND transaction', () => {
}),
).resolves.toMatchObject({ accepted: true });
- // 首检拿 caller 值且静默(有 DB 兜底候选);兜底检拿 DB 值正常广播语义。
- expect(checkWorkDirExists).toHaveBeenNthCalledWith(1, 'lazy-1', staleDir, 'codex', undefined, {
- suppressMissingBroadcast: true,
- });
- expect(checkWorkDirExists).toHaveBeenNthCalledWith(2, 'lazy-1', dbDir, 'codex', undefined);
+ // lazy-create 直接采纳 DB 值,不再先校验 caller 快照(旧目录也是 caller 值时
+ // 会误判为可用)。
+ expect(checkWorkDirExists).toHaveBeenCalledTimes(1);
+ expect(checkWorkDirExists).toHaveBeenNthCalledWith(1, 'lazy-1', dbDir, 'codex', undefined);
// bootstrap 用采纳后的 DB 路径 spawn。
expect(deps.bootstrapSession).toHaveBeenCalledWith(expect.objectContaining({ workingDir: dbDir }));
});
+ it('lazy-create prefers the DB working_dir even when the stale caller directory still exists', async () => {
+ // 用户把任务移动到别的项目后,排队/重试项里内嵌的还是旧目录;旧目录通常还在,
+ // 必须仍然以 DB 为准,否则 runtime 会在旧 cwd 启动(与移动语义矛盾)。
+ const staleDir = '/data/old-project';
+ const dbDir = '/data/new-project';
+ const checkWorkDirExists = vi.fn(async () => true);
+ const { deps } = createDeps({
+ getSession: vi.fn(() => undefined),
+ checkWorkDirExists,
+ readSessionWorkingDirFromDb: vi.fn(async () => dbDir),
+ });
+
+ await expect(
+ createMakerSendTransaction(deps).sendToAgentAccepted('lazy-moved', 'hello', {
+ agentKind: 'claude-code',
+ model: 'claude-opus-4-7',
+ workingDir: staleDir,
+ }),
+ ).resolves.toMatchObject({ accepted: true });
+
+ expect(checkWorkDirExists).toHaveBeenCalledTimes(1);
+ expect(checkWorkDirExists).toHaveBeenNthCalledWith(
+ 1,
+ 'lazy-moved',
+ dbDir,
+ 'claude-code',
+ undefined,
+ );
+ expect(deps.bootstrapSession).toHaveBeenCalledWith(
+ expect.objectContaining({ workingDir: dbDir }),
+ );
+ });
+
+ it('lazy-create resolves the DB working_dir through the recovery fallback', async () => {
+ // 数据库里还是原始挂载路径,workingDirectoryRecovery 已为它登记临时目录:
+ // 采用 DB 值时必须过 resolve(),不能把 runtime 拉回不可用的原路径。
+ const staleDir = '/data/stale-project';
+ const dbDir = '/mnt/disk/project';
+ const fallbackDir = '/userData/dialogues/fallback';
+ const checkWorkDirExists = vi.fn(async (_sid: string, dir: string | undefined | null) => dir === fallbackDir);
+ const { deps } = createDeps({
+ getSession: vi.fn(() => undefined),
+ checkWorkDirExists,
+ readSessionWorkingDirFromDb: vi.fn(async () => dbDir),
+ resolveRecoveredWorkingDir: (_sid, dir) => (dir === dbDir ? fallbackDir : dir),
+ });
+
+ await expect(
+ createMakerSendTransaction(deps).sendToAgentAccepted('lazy-fallback', 'hello', {
+ agentKind: 'codex',
+ model: 'gpt-5.5',
+ workingDir: staleDir,
+ }),
+ ).resolves.toMatchObject({ accepted: true });
+
+ expect(checkWorkDirExists).toHaveBeenCalledTimes(1);
+ expect(checkWorkDirExists).toHaveBeenNthCalledWith(
+ 1,
+ 'lazy-fallback',
+ fallbackDir,
+ 'codex',
+ undefined,
+ { suppressMissingBroadcast: true },
+ );
+ expect(deps.bootstrapSession).toHaveBeenCalledWith(
+ expect.objectContaining({ workingDir: fallbackDir }),
+ );
+ });
+
it('lazy-create still fails with WORKDIR_MISSING when caller and DB workdirs are both gone', async () => {
const { deps } = createDeps({
getSession: vi.fn(() => undefined),
diff --git a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
index 1765cac037..605750b39d 100644
--- a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
+++ b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
@@ -338,9 +338,9 @@ export interface MakerSendTransactionDeps {
): Promise<boolean>;
resolveRecoveredWorkingDir?(sessionId: string, workingDir: string): string;
/**
- * 读 DB 里既有会话的权威 working_dir(行不存在 → null)。lazy-create /
- * rehydrate 在 caller 传入的 workingDir 校验失败时用它兜底——输入队列崩溃
- * 快照等缓存的 createOpts 可能内嵌已被启动 sweep 改写掉的老路径。
+ * 读 DB 里既有会话的权威 working_dir(行不存在 → null)。lazy-create 把它当唯一
+ * 真源直接采纳;rehydrate 只在 caller 传入的 workingDir 校验失败时用它兜底——
+ * 输入队列崩溃快照等缓存的 createOpts 可能内嵌已被启动 sweep 改写掉的老路径。
*/
readSessionWorkingDirFromDb(sessionId: string): Promise<string | null>;
isOrcaMcpHydrated(sessionId: string): boolean;
@@ -634,17 +634,38 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): Make
}
/**
- * workDir 校验 + DB 权威值兜底。caller 传入的 createOpts.workingDir 可能是
- * 缓存的陈旧值(典型:输入队列崩溃快照在启动 sweep 改写 DB 之前入的库,
- * 回放时仍内嵌老路径,2026-07-20 实报)——校验失败时读 DB 行的 working_dir
- * 重试,通过则就地采纳进 createOpts(后续 bootstrap 用新 cwd spawn)。
+ * workDir 校验 + DB 权威值兜底。caller 传入的 createOpts.workingDir 可能是缓存的
+ * 陈旧值(典型:输入队列崩溃快照在启动 sweep 改写 DB 之前入的库,回放时仍内嵌老
+ * 路径,2026-07-20 实报;用户把任务移动到别的项目后,排队/重试项里内嵌的还是旧
+ * 目录,2026-09-13 实报)。两条调用路径的权威不同:
+ * - lazy-create(preferDbWorkingDir=true):没有活 runtime,DB 行是唯一真源 ——
+ * 快照与 DB 不一致时**无条件**采纳 DB 值再校验。只在旧目录缺失时才回退不够:
+ * 用户移动到别的项目后旧目录通常还在,校验会通过,新 runtime 就在旧 cwd 启动,
+ * 与「移动时关闭 runtime、下一次 send 以新目录 lazy resume」的契约矛盾。
+ * - rehydrate(默认):调用方(recovery / orca)已显式决定目标目录(可能是
+ * workingDirectoryRecovery 选定的 fallback),DB 只在 caller 目录不可用时兜底,
+ * 不能无条件改回 DB。
* 存在兜底候选时首检静默,避免"先弹错误横幅再静默成功"的假错误。
*/
async function ensureWorkDirWithDbFallback(
sessionId: string,
createOpts: CreateOpts,
+ opts?: { preferDbWorkingDir?: boolean },
): Promise<boolean> {
const dbDir = await deps.readSessionWorkingDirFromDb(sessionId).catch(() => null);
+ if (opts?.preferDbWorkingDir && dbDir && dbDir !== createOpts.workingDir) {
+ const adopted = createOpts.remoteHostId
+ ? dbDir
+ : deps.resolveRecoveredWorkingDir?.(sessionId, dbDir) ?? dbDir;
+ if (adopted !== createOpts.workingDir) {
+ deps.log.info('send: lazy-create adopted DB working_dir over stale caller snapshot', {
+ sessionId,
+ staleWorkingDir: createOpts.workingDir,
+ workingDir: adopted,
+ });
+ createOpts.workingDir = adopted;
+ }
+ }
const fallbackDir = dbDir && dbDir !== createOpts.workingDir ? dbDir : null;
const ok = fallbackDir
? await deps.checkWorkDirExists(
@@ -801,7 +822,9 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): Make
sessionId: string,
createOpts: CreateOpts,
): Promise<ResolveSessionResult> {
- const okLazy = await ensureWorkDirWithDbFallback(sessionId, createOpts);
+ const okLazy = await ensureWorkDirWithDbFallback(sessionId, createOpts, {
+ preferDbWorkingDir: true,
+ });
if (!okLazy) {
return {
kind: 'failure',