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): resolve physical cwd for worktree detection

guyong19ef1c970430
diff --git a/apps/desktop/src/main/__tests__/worktreeDetectCwd.git-integration.test.ts b/apps/desktop/src/main/__tests__/worktreeDetectCwd.git-integration.test.ts
index 859b870cb3..7e0687b2ec 100644
--- a/apps/desktop/src/main/__tests__/worktreeDetectCwd.git-integration.test.ts
+++ b/apps/desktop/src/main/__tests__/worktreeDetectCwd.git-integration.test.ts
@@ -3,7 +3,7 @@ import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { promisify } from 'node:util';
-import { afterEach, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import { detectCwd } from '../worktree/WorktreeManager';
@@ -15,12 +15,15 @@ afterEach(async () => {
if (fixture) await fs.rm(fixture, { recursive: true, force: true });
});
-it('preserves real Git snapshots for attached, detached, unborn, missing and nested directories', async () => {
+beforeEach(() => {
for (const key of Object.keys(process.env)) {
if (key.startsWith('GIT_')) vi.stubEnv(key, undefined);
}
vi.stubEnv('GIT_CONFIG_GLOBAL', process.platform === 'win32' ? 'NUL' : os.devNull);
vi.stubEnv('GIT_CONFIG_NOSYSTEM', '1');
+});
+
+it('preserves real Git snapshots for attached, detached, unborn, missing and nested directories', async () => {
fixture = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-detect-cwd-'));
const repo = path.join(fixture, 'repo');
await fs.mkdir(repo);
@@ -83,3 +86,41 @@ it('preserves real Git snapshots for attached, detached, unborn, missing and nes
expect(newlineSnapshot.repoRoot).toBe(await fs.realpath(newlinePath));
}
}, 30_000);
+
+// #6409: existing snapshots never crossed a directory symlink. Exercise detectCwd
+// with A/link -> B/workspace/project, where Git can mix absolute git-dir and
+// physical-cwd-relative common-dir. Real Git belongs in the git-integration tier.
+it.for(['unborn', 'committed'] as const)(
+ 'does not label a symlink into a %s ordinary repository as a linked worktree',
+ async (state, context) => {
+ fixture = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-detect-cwd-symlink-'));
+ const repoA = path.join(fixture, 'A');
+ const repoB = path.join(fixture, 'B');
+ const nested = path.join(repoB, 'workspace', 'project');
+ await fs.mkdir(repoA);
+ await fs.mkdir(nested, { recursive: true });
+ const git = (...args: string[]) => exec('git', args, { cwd: repoB, windowsHide: true });
+ await git('-C', repoA, 'init', '--initial-branch=main');
+ await git('init', '--initial-branch=main');
+ if (state === 'committed') {
+ await git('-c', 'user.name=Test', '-c', '[email protected]',
+ 'commit', '--allow-empty', '-m', 'fixture');
+ }
+ const link = path.join(repoA, 'link');
+ try {
+ await fs.symlink(nested, link, 'dir');
+ } catch (error) {
+ if (['EPERM', 'EACCES', 'ENOSYS'].includes((error as NodeJS.ErrnoException).code ?? '')) {
+ context.skip();
+ return;
+ }
+ throw error;
+ }
+ expect(await detectCwd(link)).toMatchObject({
+ isGitRepo: true,
+ isInsideWorktree: false,
+ gitInstalled: true,
+ repoRoot: await fs.realpath(repoB),
+ });
+ },
+);
diff --git a/apps/desktop/src/main/__tests__/worktreeDetectCwd.test.ts b/apps/desktop/src/main/__tests__/worktreeDetectCwd.test.ts
index 76ec9c226b..30825e6214 100644
--- a/apps/desktop/src/main/__tests__/worktreeDetectCwd.test.ts
+++ b/apps/desktop/src/main/__tests__/worktreeDetectCwd.test.ts
@@ -10,6 +10,7 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import path from 'node:path';
+import fs from 'node:fs/promises';
import { detectCwd } from '../worktree/WorktreeManager';
import { GitExecError, type GitExecOpts } from '../worktree/gitExec';
@@ -27,9 +28,14 @@ vi.mock('../worktree/gitExec', async () => {
beforeEach(() => {
gitExecMock.mockReset();
+ // Synthetic paths need no filesystem I/O, including fake-timer timeout tests.
+ vi.spyOn(fs, 'realpath').mockRejectedValue(Object.assign(new Error('missing directory'), { code: 'ENOENT' }));
});
-afterEach(() => vi.useRealTimers());
+afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+});
function timeoutError() {
return new GitExecError({ args: ['rev-parse'], exitCode: null, stderr: 'timed out', stdout: '', timedOut: true });
@@ -62,6 +68,20 @@ function setupGitMock(opts: {
}
describe('detectCwd → isInsideWorktree (I-1 fix)', () => {
+ // #6409: a failed physical-cwd lookup must preserve Git's partial/error result.
+ it.each([true, false])('preserves missing-directory results when gitInstalled=%s', async (gitInstalled) => {
+ gitExecMock.mockImplementation(async (args: readonly string[]) => {
+ if (gitInstalled && args[0] === '--version') return { stdout: 'git version 2.45.0\n', stderr: '' };
+ throw new GitExecError({ args, exitCode: null, stderr: '', stdout: '',
+ cause: Object.assign(new Error('not found'), { code: 'ENOENT' }) });
+ });
+ expect(await detectCwd('/missing')).toMatchObject({
+ isGitRepo: false,
+ isInsideWorktree: false,
+ gitInstalled,
+ });
+ });
+
it('releases all four hung probes after Git timeout and allows queued work and same-path retries', async () => {
vi.useFakeTimers();
gitExecMock.mockImplementation((_args: readonly string[], _cwd: string, opts: GitExecOpts) => (
diff --git a/apps/desktop/src/main/worktree/WorktreeManager.ts b/apps/desktop/src/main/worktree/WorktreeManager.ts
index cd81656761..38ef3b308e 100644
--- a/apps/desktop/src/main/worktree/WorktreeManager.ts
+++ b/apps/desktop/src/main/worktree/WorktreeManager.ts
@@ -411,6 +411,10 @@ export const detectCwd = createCwdProbeScheduler(detectCwdOnce);
const CWD_PROBE_GIT_OPTS = { timeoutMs: 10_000 };
async function detectCwdOnce(cwd: string): Promise<DetectCwdResp> {
+ // Git resolves relative metadata paths from the physical process cwd. A
+ // symlink spelling can otherwise make an ordinary repo look like a linked
+ // worktree. Keep Git's existing error handling if the directory is unavailable.
+ cwd = await fs.realpath(cwd).catch(() => cwd);
// One Git process returns the same snapshot that previously needed five.
// Unborn HEADs, older Git versions and newline-containing paths retain the
// individual-query fallback below rather than changing the IPC contract.