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(review): recover ambiguous and non-Git workspaces

Chris2abcfbd81009
diff --git a/apps/desktop/src/main/__tests__/gitContextSessionDirResolver.test.ts b/apps/desktop/src/main/__tests__/gitContextSessionDirResolver.test.ts
index e342049c39..0858219756 100644
--- a/apps/desktop/src/main/__tests__/gitContextSessionDirResolver.test.ts
+++ b/apps/desktop/src/main/__tests__/gitContextSessionDirResolver.test.ts
@@ -77,6 +77,17 @@ describe('extractDirCandidate', () => {
]) expect(extractDirCandidate(toolUse('Bash', { command }))).toBeNull();
});
+ it.each([
+ 'cd /repo/main && cd ../feature && git diff',
+ 'cd /repo/main && git status; cd /repo/feature && git diff',
+ 'cd /repo/main &&\ncd -- /repo/feature && git diff',
+ 'cd /repo/main && command cd /repo/feature && git diff',
+ 'cd /repo/main && pushd /repo/feature && git diff',
+ 'cd /repo/main && popd && git diff',
+ ])('does not mistake the first directory for the final one: %s', (command) => {
+ expect(extractDirCandidate(toolUse('Bash', { command }))).toBeNull();
+ });
+
it('相对路径不采纳(cwd / file_path 都要求绝对)→ null', () => {
expect(extractDirCandidate(toolUse('exec', { command: 'ls', cwd: '../wt-a' }))).toBeNull();
expect(extractDirCandidate(toolUse('Edit', { file_path: 'src/y.ts' }))).toBeNull();
diff --git a/apps/desktop/src/main/git-context/sessionDirResolver.ts b/apps/desktop/src/main/git-context/sessionDirResolver.ts
index 9f82f25580..855b0d1d92 100644
--- a/apps/desktop/src/main/git-context/sessionDirResolver.ts
+++ b/apps/desktop/src/main/git-context/sessionDirResolver.ts
@@ -105,12 +105,16 @@ export function extractDirCandidate(
// Claude's Bash tool does not report cwd. A literal leading `cd ... &&`
// identifies where this command ran; ordinary Bash/Read remain non-signals.
- // Do not evaluate variables, substitutions, relative paths or later cd's.
+ // Do not evaluate variables, substitutions or relative paths. If another
+ // directory switch appears later, the leading directory is not a reliable
+ // signal; leave the actual directory to other telemetry or explicit selection.
if (toolName === 'Bash' && typeof (input as { command?: unknown }).command === 'string') {
const command = (input as { command: string }).command;
const match = /^\s*cd\s+(?:--\s+)?(?:'([^'\n]+)'|"([^"\n]+)"|([^\s;&|]+))\s*&&/.exec(command);
const dir = match?.[1] ?? match?.[2] ?? match?.[3];
- if (dir && !/[\x00-\x1f$`\\*?{}]/.test(dir) && path.isAbsolute(dir)) {
+ const remaining = match ? command.slice(match[0].length) : '';
+ const hasLaterDirectorySwitch = /(?:^|[\s;&|()'"])(?:cd|pushd|popd)(?=[\s;&|()'"]|$)/.test(remaining);
+ if (dir && !hasLaterDirectorySwitch && !/[\x00-\x1f$`\\*?{}]/.test(dir) && path.isAbsolute(dir)) {
return posixDriveToWin32(dir, pathPlatform);
}
}
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 7b94521e72..1b80e903ba 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -329,7 +329,7 @@ import { messageToCamel, setSessionRuntimeProjector } from '../localDb/mapper.js
import { visibleMessageTextForConversationSearch } from '../localDb/conversationSearch.pure.js';
import { buildReviewPrompt } from '../reviewer/reviewPrompt.js';
import { resolveReviewScope, withSessionReviewWorkspace } from '../git-review/scopeResolver.js';
-import { selectReviewWorkspace } from '../reviewer/reviewWorkspaceSelection.js';
+import { selectReviewWorkspace, shouldSelectReviewWorkspace } from '../reviewer/reviewWorkspaceSelection.js';
import {
listReviewHistoricalAttachments,
loadReviewEvidence,
@@ -8368,14 +8368,10 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
sourceArtifactFingerprint = prepared.fingerprint;
};
await prepareEvidence();
- // An empty main checkout commonly means the work happened in a linked
- // worktree. Historical turn metadata cannot tell us which current code
+ // An empty checkout or non-Git source commonly means the work happened
+ // elsewhere. Historical turn metadata cannot tell us which current code
// the user wants; ask about the directory instead of refusing the run.
- if (
- evidence.workspace && !evidence.workspace.disabledReason &&
- !evidence.workspace.dirty && !evidence.branch &&
- evidence.artifacts.length === 0 && !evidence.focusPath && !isDeviceLinkInvoke()
- ) {
+ if (shouldSelectReviewWorkspace(evidence, isDeviceLinkInvoke())) {
const selectedDir = await selectReviewWorkspace(event as IpcMainInvokeEvent, sourceWorkingDir);
if (!selectedDir) throwIpcError('MUTATION_CANCELLED', 'Review workspace selection cancelled');
workspaceSelected = true;
diff --git a/apps/desktop/src/main/reviewer/__tests__/reviewWorkspaceSelection.test.ts b/apps/desktop/src/main/reviewer/__tests__/reviewWorkspaceSelection.test.ts
index e3a0c454d6..28b73ea157 100644
--- a/apps/desktop/src/main/reviewer/__tests__/reviewWorkspaceSelection.test.ts
+++ b/apps/desktop/src/main/reviewer/__tests__/reviewWorkspaceSelection.test.ts
@@ -14,7 +14,18 @@ vi.mock('electron', () => ({
vi.mock('../../i18n.js', () => ({ t: (key: string) => key }));
vi.mock('../../security/trustedAppRenderer.js', () => ({ assertTrustedAppRendererEvent: assertTrusted }));
-import { selectReviewWorkspace } from '../reviewWorkspaceSelection.js';
+import { selectReviewWorkspace, shouldSelectReviewWorkspace } from '../reviewWorkspaceSelection.js';
+import type { LoadedReviewEvidence } from '../reviewEvidence.js';
+
+function emptyEvidence(): Pick<LoadedReviewEvidence, 'workspace' | 'branch' | 'artifacts' | 'focusPath'> {
+ return {
+ workspace: {
+ dirty: false, totalFiles: 0, stagedFiles: 0, unstagedFiles: 0, untrackedFiles: 0,
+ disabledReason: null, diffs: { staged: [], unstaged: [] },
+ },
+ branch: null, artifacts: [], focusPath: null,
+ };
+}
beforeEach(() => {
vi.clearAllMocks();
@@ -27,6 +38,33 @@ beforeEach(() => {
describe('Review directory selection', () => {
const event = { sender: {} } as IpcMainInvokeEvent;
+ it.each([null, 'non-git'] as const)('recovers the actual checkout from an empty %s source', async (reason) => {
+ const evidence = emptyEvidence();
+ evidence.workspace!.disabledReason = reason;
+ expect(shouldSelectReviewWorkspace(evidence, false)).toBe(true);
+ expect(await selectReviewWorkspace(event, '/projects')).toBe('/repo/actual');
+ });
+
+ it.each(['git-unavailable', 'no-workdir', 'invalid-worktree', 'remote-session', 'no-session', 'unknown'] as const)(
+ 'does not hide a %s failure behind a directory picker', (reason) => {
+ const evidence = emptyEvidence();
+ evidence.workspace!.disabledReason = reason;
+ expect(shouldSelectReviewWorkspace(evidence, false)).toBe(false);
+ },
+ );
+
+ it('preserves explicit targets, existing code changes, and remote caller behavior', () => {
+ const evidence = emptyEvidence();
+ expect(shouldSelectReviewWorkspace(evidence, true)).toBe(false);
+ expect(shouldSelectReviewWorkspace({ ...evidence, workspace: null }, false)).toBe(false);
+ expect(shouldSelectReviewWorkspace({ ...evidence, workspace: { ...evidence.workspace!, dirty: true } }, false)).toBe(false);
+ expect(shouldSelectReviewWorkspace({ ...evidence, artifacts: [{ kind: 'file', label: 'result.ts' }] }, false)).toBe(false);
+ expect(shouldSelectReviewWorkspace({ ...evidence, focusPath: '/repo/file.ts' }, false)).toBe(false);
+ expect(shouldSelectReviewWorkspace({ ...evidence, branch: {
+ baseRef: 'main', baseOid: 'base', mergeBaseOid: 'base', fileCount: 1, diffs: [], capped: null,
+ } }, false)).toBe(false);
+ });
+
it('uses the directory picked in a native dialog without changing the source task', async () => {
expect(await selectReviewWorkspace(event, '/repo/main')).toBe('/repo/actual');
expect(assertTrusted).toHaveBeenCalledWith(event);
diff --git a/apps/desktop/src/main/reviewer/reviewWorkspaceSelection.ts b/apps/desktop/src/main/reviewer/reviewWorkspaceSelection.ts
index 47d3c8625f..b586ff1a5f 100644
--- a/apps/desktop/src/main/reviewer/reviewWorkspaceSelection.ts
+++ b/apps/desktop/src/main/reviewer/reviewWorkspaceSelection.ts
@@ -4,6 +4,21 @@ import { BrowserWindow, dialog, type IpcMainInvokeEvent } from 'electron';
import { t } from '../i18n.js';
import { assertTrustedAppRendererEvent } from '../security/trustedAppRenderer.js';
+import type { LoadedReviewEvidence } from './reviewEvidence.js';
+
+/** A non-Git source directory also needs recovery, but a failed Git read or a
+ * remote task must not be turned into a local directory-selection fallback.
+ */
+export function shouldSelectReviewWorkspace(
+ evidence: Pick<LoadedReviewEvidence, 'workspace' | 'branch' | 'artifacts' | 'focusPath'>,
+ deviceLinkInvoke: boolean,
+): boolean {
+ const workspace = evidence.workspace;
+ return !deviceLinkInvoke && !!workspace &&
+ (!workspace.disabledReason || workspace.disabledReason === 'non-git') &&
+ !workspace.dirty && !evidence.branch &&
+ evidence.artifacts.length === 0 && !evidence.focusPath;
+}
/** No changes were found in the task's directory. Let the user choose the
* actual checkout, or explicitly review the current code without a diff.