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 Yunjinc85f016c00fc
diff --git a/apps/desktop/src/main/maker-host/session-storage.ts b/apps/desktop/src/main/maker-host/session-storage.ts
index db3614eb3d..d1169ff001 100644
--- a/apps/desktop/src/main/maker-host/session-storage.ts
+++ b/apps/desktop/src/main/maker-host/session-storage.ts
@@ -212,6 +212,29 @@ export async function readSessionWorkingDirFromDb(id: string): Promise<string |
* SEND lazy-create handler 用它兜底 (renderer 不走 createOpts 透传 extraDirs)。
* 失败 / 空 / 不是数组 → 返回 [],不抛错。
*/
+/**
+ * 读 working_dir 时同时报告「行是否存在」。lazy-create 必须区分两种 null:
+ * 「没有这一行」(首次建 runtime,沿用 caller 快照)与「行在、但 working_dir 已被
+ * 显式清空」(DB 明确说这个会话没有目录,不能让排队/重试快照里的旧目录把它复活)。
+ */
+export async function readSessionWorkingDirRow(
+ id: string,
+): Promise<{ exists: boolean; workingDir: string | null }> {
+ const db = getDbClient().drizzle;
+ const rows = await db
+ .select({ workingDir: sessions.workingDir })
+ .from(sessions)
+ .where(eq(sessions.id, id))
+ .limit(1);
+ const row = rows[0];
+ if (!row) return { exists: false, workingDir: null };
+ const raw = row.workingDir;
+ return {
+ exists: true,
+ workingDir: typeof raw === 'string' && raw.trim() !== '' ? raw : null,
+ };
+}
+
export async function readSessionExtraDirsFromDb(id: string): Promise<string[]> {
const db = getDbClient().drizzle;
const rows = await db
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 0deceed989..e871444f51 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
@@ -63,6 +63,8 @@ function createDeps(overrides: Partial<MakerSendTransactionDeps> = {}) {
synthesizeOrcaVendorOptionsFromDb: vi.fn(async () => false),
readSessionExtraDirsFromDb: vi.fn(async () => []),
readSessionWorkingDirFromDb: vi.fn(async () => null),
+ // 默认「DB 没有这一行」:lazy-create 沿用 caller 快照的既有语义不变。
+ readSessionWorkingDirState: vi.fn(async () => ({ exists: false, workingDir: null })),
readWorkingDirectoryRecoveryCreateOpts: vi.fn(async (): Promise<MakerSessionCreateOpts> => ({
agentKind: 'codex', workingDir: 'C:\\repo', model: 'gpt-5.4',
})),
@@ -1613,6 +1615,57 @@ describe('maker SEND transaction', () => {
);
});
+ it('lazy-create refuses the stale snapshot when the DB row exists with no working_dir', async () => {
+ // working_dir 被显式清空(null)后 DB 明确说这个会话没有目录;排队/重试快照里
+ // 内嵌的旧目录不能把它复活,否则 runtime 又会在库里已经不认的项目里跑。
+ const staleDir = '/data/old-project';
+ const checkWorkDirExists = vi.fn(async () => true);
+ const { deps } = createDeps({
+ getSession: vi.fn(() => undefined),
+ checkWorkDirExists,
+ readSessionWorkingDirFromDb: vi.fn(async () => null),
+ readSessionWorkingDirState: vi.fn(async () => ({ exists: true, workingDir: null })),
+ });
+
+ await expect(
+ createMakerSendTransaction(deps).sendToAgentAccepted('cleared-session', 'hello', {
+ agentKind: 'claude-code',
+ model: 'claude-opus-4-7',
+ workingDir: staleDir,
+ }),
+ ).resolves.toMatchObject({ accepted: false, reason: 'WORKDIR_MISSING' });
+
+ expect(checkWorkDirExists).not.toHaveBeenCalled();
+ expect(deps.bootstrapSession).not.toHaveBeenCalled();
+ });
+
+ it('lazy-create keeps the caller snapshot when the DB has no row yet', async () => {
+ // 行不存在(首次 lazy-create)不是「被清空」:caller 快照仍是唯一可用的目录。
+ const callerDir = '/data/fresh-project';
+ const checkWorkDirExists = vi.fn(async () => true);
+ const { deps } = createDeps({
+ getSession: vi.fn(() => undefined),
+ checkWorkDirExists,
+ readSessionWorkingDirFromDb: vi.fn(async () => null),
+ readSessionWorkingDirState: vi.fn(async () => ({ exists: false, workingDir: null })),
+ });
+
+ await expect(
+ createMakerSendTransaction(deps).sendToAgentAccepted('fresh-session', 'hello', {
+ agentKind: 'claude-code',
+ model: 'claude-opus-4-7',
+ workingDir: callerDir,
+ }),
+ ).resolves.toMatchObject({ accepted: true });
+
+ expect(checkWorkDirExists).toHaveBeenCalledWith(
+ 'fresh-session', callerDir, 'claude-code', undefined,
+ );
+ expect(deps.bootstrapSession).toHaveBeenCalledWith(
+ expect.objectContaining({ workingDir: callerDir }),
+ );
+ });
+
it('lazy-create resolves the DB working_dir through the recovery fallback', async () => {
// 数据库里还是原始挂载路径,workingDirectoryRecovery 已为它登记临时目录:
// 采用 DB 值时必须过 resolve(),不能把 runtime 拉回不可用的原路径。
diff --git a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
index 3dd018f23d..518c8eddc7 100644
--- a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
+++ b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
@@ -351,6 +351,13 @@ export interface MakerSendTransactionDeps {
* 输入队列崩溃快照等缓存的 createOpts 可能内嵌已被启动 sweep 改写掉的老路径。
*/
readSessionWorkingDirFromDb(sessionId: string): Promise<string | null>;
+ /**
+ * 区分「没有这一行」与「行在但 working_dir 被显式清空」:DB 返回空目录时,
+ * lazy-create 用它判断能不能沿用 caller 快照(行在=DB 说这个会话没有目录)。
+ */
+ readSessionWorkingDirState?(
+ sessionId: string,
+ ): Promise<{ exists: boolean; workingDir: string | null }>;
isOrcaMcpHydrated(sessionId: string): boolean;
buildCreateOptsWithStderr(opts: CreateOpts): CreateOpts;
synthesizeOrcaVendorOptionsFromDb(sessionId: string, opts: CreateOpts): Promise<boolean>;
@@ -665,6 +672,25 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): Make
opts?: { preferDbWorkingDir?: boolean },
): Promise<boolean> {
const dbDir = await deps.readSessionWorkingDirFromDb(sessionId).catch(() => null);
+ if (
+ opts?.preferDbWorkingDir &&
+ !dbDir &&
+ !createOpts.remoteHostId &&
+ createOpts.workingDir &&
+ deps.readSessionWorkingDirState
+ ) {
+ // DB 行存在但 working_dir 已被清空:DB 明确说这个会话没有目录,排队/重试快照
+ // 里的旧目录不能把它复活 —— 否则又是「库里没有目录、runtime 却在旧项目里跑」。
+ // 行不存在(首次 lazy-create)时才沿用 caller 快照。
+ const state = await deps.readSessionWorkingDirState(sessionId).catch(() => null);
+ if (state?.exists) {
+ deps.log.warn('send: lazy-create dropped stale caller workingDir because DB row has none', {
+ sessionId,
+ staleWorkingDir: createOpts.workingDir,
+ });
+ return false;
+ }
+ }
if (opts?.preferDbWorkingDir && dbDir && dbDir !== createOpts.workingDir) {
const adopted = createOpts.remoteHostId
? dbDir
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 3fe711fcba..ee5788a292 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -498,6 +498,7 @@ import {
readSessionExtraDirsFromDb,
readSessionWritableDirsFromDb,
readSessionWorkingDirFromDb,
+ readSessionWorkingDirRow,
listVisibleActiveSessionDirectoryGrants,
} from '../maker-host/session-storage.js';
import { libraryExtraDirSyncTargets } from './libraryExtraDirSyncTargets.js';
@@ -12139,6 +12140,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
readSessionExtraDirsFromDb,
readSessionWritableDirsFromDb,
readSessionWorkingDirFromDb,
+ readSessionWorkingDirState: readSessionWorkingDirRow,
withRehydrateCloseSuppressed,
bootstrapSession,
markOrcaRoleIfNeeded,