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): 修复审查来源切换状态与远程归属判定

DavidShen14c101e54060
diff --git a/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openTurnReview.test.ts b/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openTurnReview.test.ts
index ca004637ae..bc57f5fd33 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openTurnReview.test.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/lib/__tests__/openTurnReview.test.ts
@@ -32,7 +32,11 @@ describe('openTurnReview', () => {
mocks.ensureHydrated.mockResolvedValue(undefined);
mocks.patchTabState.mockImplementation(
async (_sessionId: string, _tabId: string, update: (current: unknown) => unknown) =>
- update({ diffsExpanded: false, turnTarget: { legacy: true } }),
+ update({
+ diffsExpanded: false,
+ branchBaseRef: 'origin/release',
+ turnTarget: { legacy: true },
+ }),
);
});
@@ -49,8 +53,15 @@ describe('openTurnReview', () => {
const update = mocks.patchTabState.mock.calls[0]?.[2] as (
current: unknown,
) => Record<string, unknown>;
- expect(update({ diffsExpanded: false, turnTarget: { legacy: true } })).toEqual({
+ expect(
+ update({
+ diffsExpanded: false,
+ branchBaseRef: 'origin/release',
+ turnTarget: { legacy: true },
+ }),
+ ).toEqual({
diffsExpanded: false,
+ branchBaseRef: 'origin/release',
descriptor: {
kind: 'turn-set',
changeSetIds: ['set-1'],
diff --git a/apps/desktop/src/renderer/features/right-sidebar/lib/openTurnReview.ts b/apps/desktop/src/renderer/features/right-sidebar/lib/openTurnReview.ts
index 6406c1cf43..71cdb33019 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/lib/openTurnReview.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/lib/openTurnReview.ts
@@ -49,7 +49,6 @@ export async function openTurnReview(
targetSessionId: sessionId,
};
delete preserved.turnTarget;
- delete preserved.branchBaseRef;
return {
...preserved,
descriptor: messageSnapshot,
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/ReviewTabBody.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/ReviewTabBody.tsx
index e9a4b8611c..2b30d1f048 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/ReviewTabBody.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/ReviewTabBody.tsx
@@ -424,6 +424,15 @@ export function reviewSourceStatePatch(descriptor: ReviewSourceDescriptor) {
};
}
+export function descriptorForReviewSource(
+ source: ReviewSource,
+ branchBaseRef: string | null,
+): ReviewSourceDescriptor {
+ if (source === 'commit') return { kind: 'commit', commitOid: null };
+ if (source === 'branch') return { kind: 'branch', baseRef: branchBaseRef };
+ return { kind: source };
+}
+
export function ReviewTabBody({ state, ctx }: ReviewTabBodyProps) {
const { t } = useTranslation();
const { confirm } = useConfirmDialog();
@@ -437,7 +446,8 @@ export function ReviewTabBody({ state, ctx }: ReviewTabBodyProps) {
const messageSnapshot =
state.messageSnapshot ?? (descriptor.kind === 'turn-set' ? descriptor : null);
const source: ReviewSource | 'turn' = descriptor.kind === 'turn-set' ? 'turn' : descriptor.kind;
- const branchBaseRef = descriptor.kind === 'branch' ? descriptor.baseRef : null;
+ const branchBaseRef =
+ state.branchBaseRef ?? (descriptor.kind === 'branch' ? descriptor.baseRef : null);
const selectedCommitOid = descriptor.kind === 'commit' ? descriptor.commitOid : null;
const effectiveCommitOid = selectedCommitOid;
const crossSession =
@@ -459,22 +469,16 @@ export function ReviewTabBody({ state, ctx }: ReviewTabBodyProps) {
);
const setSource = useCallback(
(nextSource: ReviewSource) => {
- const nextDescriptor: ReviewSourceDescriptor =
- nextSource === 'commit'
- ? { kind: 'commit', commitOid: null }
- : nextSource === 'branch'
- ? { kind: 'branch', baseRef: null }
- : { kind: nextSource };
- setDescriptor(nextDescriptor);
+ setDescriptor(descriptorForReviewSource(nextSource, branchBaseRef));
},
- [setDescriptor],
+ [branchBaseRef, setDescriptor],
);
const selectMessageSnapshot = useCallback(() => {
if (messageSnapshot) setDescriptor(messageSnapshot);
}, [messageSnapshot, setDescriptor]);
const sourceState = useReviewSource(descriptor, sessionId, {
hideWhitespace,
- deviceLinkDeviceId,
+ deviceLinkDeviceId: ctx.deviceLinkDeviceId,
remoteHostId: ctx.remoteHostId,
});
const data = sourceState.reviewData;
@@ -1107,9 +1111,12 @@ export function ReviewTabBody({ state, ctx }: ReviewTabBodyProps) {
);
const setBranchBaseRef = useCallback(
(nextBaseRef: string) => {
- setDescriptor({ kind: 'branch', baseRef: nextBaseRef });
+ ctx.patchState({
+ ...reviewSourceStatePatch({ kind: 'branch', baseRef: nextBaseRef }),
+ branchBaseRef: nextBaseRef,
+ });
},
- [setDescriptor],
+ [ctx],
);
const selectCommitSource = useCallback(
(oid: string) => {
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/ReviewTabBody.helpers.test.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/ReviewTabBody.helpers.test.ts
index f6249113d3..e691086392 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/ReviewTabBody.helpers.test.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/ReviewTabBody.helpers.test.ts
@@ -21,6 +21,7 @@ import {
buildLastTurnCappedData,
canUsePatchBasedReviewActions,
CappedSourceView,
+ descriptorForReviewSource,
discardForReviewDiff,
filterWhitespaceHiddenDiffs,
getLastTurnCappedSummaryEntries,
@@ -202,6 +203,13 @@ describe('ReviewTabBody source state', () => {
turnTarget: null,
});
});
+
+ it('restores a non-default branch base when returning to the branch source', () => {
+ expect(descriptorForReviewSource('branch', 'origin/release')).toEqual({
+ kind: 'branch',
+ baseRef: 'origin/release',
+ });
+ });
});
describe('ReviewTabBody compact source dropdown', () => {
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/plugin.test.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/plugin.test.ts
index db2b428353..4ecce6dccf 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/plugin.test.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/plugin.test.ts
@@ -61,6 +61,7 @@ describe('review plugin', () => {
wordDiff: boolean;
hideWhitespace: boolean;
richMarkdownPreview: boolean;
+ branchBaseRef: string | null;
};
expect(a.descriptor).toEqual({ kind: 'unstaged' });
expect(a.messageSnapshot).toBeNull();
@@ -72,6 +73,7 @@ describe('review plugin', () => {
expect(a.wordDiff).toBe(false);
expect(a.hideWhitespace).toBe(false);
expect(a.richMarkdownPreview).toBe(true);
+ expect(a.branchBaseRef).toBeNull();
});
it('hydrateState recovers the persisted all-diff expansion preference', () => {
@@ -85,6 +87,7 @@ describe('review plugin', () => {
hideWhitespace: true,
richMarkdownPreview: false,
descriptor: { kind: 'branch', baseRef: 'main' },
+ branchBaseRef: 'origin/release',
jumpTarget: { diffId: 'branch:main:a.ts', path: 'a.ts', nonce: 3 },
}) as {
diffsExpanded: boolean;
@@ -95,6 +98,7 @@ describe('review plugin', () => {
hideWhitespace: boolean;
richMarkdownPreview: boolean;
descriptor: { kind: string; baseRef?: string | null };
+ branchBaseRef: string | null;
jumpTarget: { diffId: string | null; path: string | null; nonce: number } | null;
};
expect(s.diffsExpanded).toBe(false);
@@ -105,6 +109,7 @@ describe('review plugin', () => {
expect(s.hideWhitespace).toBe(true);
expect(s.richMarkdownPreview).toBe(false);
expect(s.descriptor).toEqual({ kind: 'branch', baseRef: 'main' });
+ expect(s.branchBaseRef).toBe('origin/release');
expect(s.jumpTarget).toEqual({ diffId: 'branch:main:a.ts', path: 'a.ts', nonce: 3 });
});
@@ -137,6 +142,25 @@ describe('review plugin', () => {
).toEqual({ kind: 'unstaged' });
});
+ it('derives the remembered branch base from the descriptor and rejects unsafe state', () => {
+ const p = registry.getTabKind('review')!;
+ expect(
+ (
+ p.hydrateState!({ descriptor: { kind: 'branch', baseRef: 'origin/main' } }) as {
+ branchBaseRef: string | null;
+ }
+ ).branchBaseRef,
+ ).toBe('origin/main');
+ expect(
+ (
+ p.hydrateState!({
+ descriptor: { kind: 'unstaged' },
+ branchBaseRef: '-unsafe',
+ }) as { branchBaseRef: string | null }
+ ).branchBaseRef,
+ ).toBeNull();
+ });
+
it('hydrates an exact message snapshot independently from the active git source', () => {
const p = registry.getTabKind('review')!;
const state = p.hydrateState!({
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/useReviewSource.test.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/useReviewSource.test.ts
index bc003652e5..6dca28983f 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/useReviewSource.test.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/__tests__/useReviewSource.test.ts
@@ -119,4 +119,18 @@ describe('useReviewSource', () => {
expect(result.current.error).toBe(REVIEW_TURN_LOCAL_ONLY_ERROR);
expect(mocks.getTurnChangeSets).not.toHaveBeenCalled();
});
+
+ it('fails closed while device-link ownership is unresolved', async () => {
+ const { result } = renderHook(() =>
+ useReviewSource(
+ { kind: 'turn-set', targetSessionId: null, changeSetIds: ['set-1'] },
+ 'host-session',
+ { hideWhitespace: false, deviceLinkDeviceId: undefined, remoteHostId: null },
+ ),
+ );
+
+ await waitFor(() => expect(result.current.loading).toBe(false));
+ expect(result.current.error).toBe(REVIEW_TURN_LOCAL_ONLY_ERROR);
+ expect(mocks.getTurnChangeSets).not.toHaveBeenCalled();
+ });
});
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/index.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/index.tsx
index 02ee971305..9bb3064c57 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/index.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/index.tsx
@@ -23,6 +23,7 @@ import {
type ReviewJumpTarget,
type ReviewSourceDescriptor,
} from '../../../../../shared/reviewSource';
+import { isSafeBranchBaseRef } from '../../../../../shared/reviewBranchRef';
import { registerTabKind } from '../../registry';
import type { TabKindPlugin } from '../../types';
import type { DiffViewMode } from './DiffViewer/PlainUnifiedDiff';
@@ -53,6 +54,8 @@ export interface ReviewState {
hideWhitespace: boolean;
/** Markdown 文件是否默认用富文本预览替换 diff 主体。对齐 Codex,默认开启。 */
richMarkdownPreview: boolean;
+ /** 最近选择的分支比较基线;切到其它来源或消息快照后仍保留。 */
+ branchBaseRef: string | null;
}
const DEFAULT_STATE: ReviewState = {
@@ -66,6 +69,7 @@ const DEFAULT_STATE: ReviewState = {
wordDiff: false,
hideWhitespace: false,
richMarkdownPreview: true,
+ branchBaseRef: null,
};
function ReviewTabPillTitle({ t }: { state: ReviewState; t: TFunction }) {
@@ -113,6 +117,14 @@ const plugin: TabKindPlugin<ReviewState> = {
const legacyTurnTarget = migrateLegacyTurnTarget(obj.turnTarget);
const persistedDescriptor = parseReviewSourceDescriptor(obj.descriptor);
const descriptor = persistedDescriptor ?? legacyTurnTarget?.descriptor ?? { kind: 'unstaged' };
+ const rawBranchBaseRef =
+ typeof obj.branchBaseRef === 'string' ? obj.branchBaseRef.trim() : null;
+ const branchBaseRef =
+ rawBranchBaseRef && isSafeBranchBaseRef(rawBranchBaseRef)
+ ? rawBranchBaseRef
+ : descriptor.kind === 'branch'
+ ? descriptor.baseRef
+ : null;
const persistedMessageSnapshot = parseReviewSourceDescriptor(obj.messageSnapshot);
const messageSnapshot =
persistedMessageSnapshot?.kind === 'turn-set'
@@ -136,6 +148,7 @@ const plugin: TabKindPlugin<ReviewState> = {
wordDiff,
hideWhitespace,
richMarkdownPreview,
+ branchBaseRef,
};
},
onBeforeClose: (state, { sessionId }) => {
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/useReviewSource.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/useReviewSource.ts
index 5bec429019..64b2352dd9 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/review/useReviewSource.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/review/useReviewSource.ts
@@ -97,27 +97,28 @@ export function useReviewSource(
sessionId: string | null,
opts: {
hideWhitespace: boolean;
- deviceLinkDeviceId: string | null;
+ deviceLinkDeviceId: string | null | undefined;
remoteHostId: string | null;
},
): ReviewSourceData {
const { hideWhitespace, deviceLinkDeviceId, remoteHostId } = opts;
+ const gitDeviceLinkDeviceId = deviceLinkDeviceId ?? null;
const gitSessionId = descriptor.kind === 'turn-set' ? null : sessionId;
const branchBaseRef = descriptor.kind === 'branch' ? descriptor.baseRef : null;
const commitOid = descriptor.kind === 'commit' ? descriptor.commitOid : null;
- const gitState = useReviewGitState(gitSessionId, hideWhitespace, deviceLinkDeviceId);
+ const gitState = useReviewGitState(gitSessionId, hideWhitespace, gitDeviceLinkDeviceId);
const commitState = useReviewCommitDiff(
gitSessionId,
commitOid,
hideWhitespace,
- deviceLinkDeviceId,
+ gitDeviceLinkDeviceId,
);
const branchState = useReviewBranchDiff(
gitSessionId,
branchBaseRef,
hideWhitespace,
descriptor.kind === 'branch',
- deviceLinkDeviceId,
+ gitDeviceLinkDeviceId,
);
const currentBranchData = getCurrentBranchDiffData(branchState.data, branchBaseRef);
const lastTurnPaths = useLastTurnFilter(gitSessionId, gitState.data?.scope.repoRoot ?? null);
@@ -184,7 +185,7 @@ export function useReviewSource(
const lastTurnHydratedState = useReviewFileDiffs(
gitSessionId,
lastTurnHydrationRequests,
- deviceLinkDeviceId,
+ gitDeviceLinkDeviceId,
);
const hydratedLastTurnDiffs = useMemo(
() =>