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): 对话任务顶栏也显示分支与 PR 徽标 (#5105)

Dashcdbfc4a68e2d
diff --git a/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx b/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
index debfe697c9..9f981af12e 100644
--- a/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/SessionContentHeader.tsx
@@ -674,7 +674,7 @@ export function SessionContentHeader({
</DropdownMenu>
)}
- {/* session-git-pr-context:当前分支 + 关联 PR 徽标(非 git 目录 / dialogue 会话自动隐藏) */}
+ {/* session-git-pr-context:当前分支 + 关联 PR 徽标(项目任务与对话一致;非 git 目录且无 PR 时自动隐藏) */}
<GitContextBadge session={session} />
{tagEditorOpen && <TaskTagEditor session={session} onClose={() => setTagEditorOpen(false)} />}
diff --git a/apps/desktop/src/renderer/features/cc-agent/__tests__/GitContextBadge.test.tsx b/apps/desktop/src/renderer/features/cc-agent/__tests__/GitContextBadge.test.tsx
new file mode 100644
index 0000000000..3bf91c81de
--- /dev/null
+++ b/apps/desktop/src/renderer/features/cc-agent/__tests__/GitContextBadge.test.tsx
@@ -0,0 +1,84 @@
+// @vitest-environment jsdom
+
+import { cleanup, render, screen } from '@testing-library/react';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import { Tooltip } from '@/components/ui/tooltip';
+import type { Session } from '@/lib/ccAgent.types';
+import type { SessionGitContext } from '@/hooks/useSessionGitContext';
+
+const { gitContextMock } = vi.hoisted(() => ({
+ gitContextMock: vi.fn<() => SessionGitContext>(),
+}));
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({
+ t: (key: string, params?: Record<string, unknown>) =>
+ params ? `${key} ${JSON.stringify(params)}` : key,
+ }),
+}));
+vi.mock('@/hooks/useSessionGitContext', () => ({
+ useSessionGitContext: gitContextMock,
+}));
+
+import { GitContextBadge } from '../GitContextBadge';
+
+const dialogueSession = {
+ id: 'session-1',
+ workspaceKind: 'dialogue',
+ workingDir: '/Users/me/Cindy/dialogues/abc',
+ remoteHostId: null,
+} as Session;
+
+afterEach(() => {
+ cleanup();
+ gitContextMock.mockReset();
+});
+
+describe('GitContextBadge', () => {
+ it('对话目录不是 git 仓库时,仍显示消息里的 PR 徽标', () => {
+ gitContextMock.mockReturnValue({
+ head: null,
+ branchSource: null,
+ prRefs: [
+ {
+ id: 'ref-1',
+ sessionId: 'session-1',
+ owner: 'octo',
+ repo: 'repo',
+ prNumber: 7,
+ url: 'https://github.com/octo/repo/pull/7',
+ firstSeenAt: 1,
+ lastSeenAt: 2,
+ },
+ ],
+ prStatuses: new Map(),
+ });
+
+ render(
+ <Tooltip.Provider>
+ <GitContextBadge session={dialogueSession} />
+ </Tooltip.Provider>,
+ );
+
+ expect(screen.getByRole('button').textContent).toContain('#7');
+ expect(screen.queryByLabelText(/ccAgent\.gitContext\.branchAria/)).toBeNull();
+ });
+
+ it('既无分支也无 PR 时不渲染', () => {
+ gitContextMock.mockReturnValue({
+ head: null,
+ branchSource: null,
+ prRefs: [],
+ prStatuses: new Map(),
+ });
+
+ const { container } = render(
+ <Tooltip.Provider>
+ <GitContextBadge session={dialogueSession} />
+ </Tooltip.Provider>,
+ );
+
+ expect(container.innerHTML).toBe('');
+ });
+});
diff --git a/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.ts b/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.ts
index 2e0217e99c..8fc2754f42 100644
--- a/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.ts
+++ b/apps/desktop/src/renderer/hooks/__tests__/useSessionGitContext.remote.test.ts
@@ -238,6 +238,69 @@ describe('useSessionGitContext remote routing', () => {
unmount();
});
+ it.each([
+ {
+ label: 'Agent 进入仓库时显示分支',
+ dir: {
+ workdir: '/Users/me/repo',
+ head: { kind: 'branch', branch: 'feature/dialogue', shortSha: null },
+ source: 'telemetry',
+ },
+ },
+ {
+ label: '对话目录不是 git 仓库时分支为空',
+ dir: { workdir: null, head: null, source: null },
+ },
+ ])('对话任务照常返回消息里的 PR:$label', async ({ dir }) => {
+ const gitContext = makeGitContext();
+ gitContext.getForSession.mockResolvedValue(dir);
+ const prRef = {
+ id: 'ref-1',
+ sessionId: 'session-1',
+ owner: 'octo',
+ repo: 'repo',
+ prNumber: 7,
+ url: 'https://github.com/octo/repo/pull/7',
+ firstSeenAt: 1,
+ lastSeenAt: 2,
+ };
+ gitContext.listAllPrRefs.mockResolvedValue([prRef]);
+ gitContext.listPrRefs.mockResolvedValue([prRef]);
+ gitContext.getPrStatuses.mockResolvedValue([
+ {
+ ok: true,
+ owner: 'octo',
+ repo: 'repo',
+ prNumber: 7,
+ status: 'merged',
+ title: 'Dialogue PR',
+ htmlUrl: 'https://github.com/octo/repo/pull/7',
+ branch: 'feature/x',
+ unresolvedCount: 0,
+ },
+ ]);
+ window.electronAPI = {
+ gitContext,
+ deviceLink: { invoke: vi.fn() },
+ } as never;
+
+ const session = { ...sessionBase, workspaceKind: 'dialogue' as const };
+ const { result, unmount } = renderHook(() => useSessionGitContext(session), { wrapper });
+
+ await waitFor(() => {
+ expect(gitContext.getForSession).toHaveBeenCalled();
+ expect(result.current.prRefs).toHaveLength(1);
+ expect(result.current.prStatuses.size).toBe(1);
+ });
+ await waitFor(() => expect(result.current.head).toEqual(dir.head));
+ if (dir.workdir) {
+ expect(gitContext.watch).toHaveBeenCalledWith(dir.workdir);
+ } else {
+ expect(gitContext.watch).not.toHaveBeenCalled();
+ }
+ unmount();
+ });
+
// 2026-08-13 用户裁决:设备明确断线时不发注定失败的 PR 隧道查询(fail-open:
// shard 缺失照常尝试,见 prRefsRefreshGating.test.ts 的判定语义)。
it('被控端标记断线时跳过 PR 引用的隧道查询,重连后恢复', async () => {
diff --git a/apps/desktop/src/renderer/hooks/useSessionGitContext.ts b/apps/desktop/src/renderer/hooks/useSessionGitContext.ts
index d1d12030c0..c5dd2398fc 100644
--- a/apps/desktop/src/renderer/hooks/useSessionGitContext.ts
+++ b/apps/desktop/src/renderer/hooks/useSessionGitContext.ts
@@ -14,8 +14,9 @@
* 远程路由、失败自愈全部由 PrRefsContext 单点负责;返回值按当前会话过滤,
* 语义与旧实现一致(切会话/断链即空)。
*
- * 约束:dialogue 会话(workspaceKind !== 'project')不启用——workingDir 是对话自有目录,
- * 分支语义无意义。SSH 与 device-link 远程会话则把查询发往真实执行端。
+ * 项目任务与 dialogue 会话同等对待:对话目录不是 git 仓库时 head 自然为空,Agent 进入
+ * 仓库工作时由遥测解析出真实分支;PR 引用来自消息里的 GitHub 链接,与本地仓库无关。
+ * SSH 与 device-link 远程会话则把查询发往真实执行端。
*/
import { useEffect, useMemo, useState } from 'react';
@@ -58,7 +59,7 @@ async function invokeRemoteGitContext<T>(
}
export interface SessionGitContext {
- /** 当前分支信息;null = 非 git 目录 / dialogue 会话 / 尚未加载。 */
+ /** 当前分支信息;null = 非 git 目录 / 尚未加载。 */
head: GitHeadInfo | null;
/** head 的来源,决定徽标对分支的信任度(telemetry/worktree/remote 可信,workingDir 让位 PR)。 */
branchSource: GitContextDirSource;
@@ -68,13 +69,6 @@ export interface SessionGitContext {
prStatuses: Map<string, PrStatusResult>;
}
-const EMPTY: SessionGitContext = {
- head: null,
- branchSource: null,
- prRefs: [],
- prStatuses: new Map(),
-};
-
export function useSessionGitContext(session: Session): SessionGitContext {
const sessionId = session.id;
// 本地 worktree 路径用 WorktreeContext 的 live 元数据,**不读 session.worktreePath**:
@@ -86,7 +80,6 @@ export function useSessionGitContext(session: Session): SessionGitContext {
const deviceLinkDeviceId =
session.deviceLinkDeviceId ?? getStickySessionDeviceId(sessionId) ?? null;
const remoteHostId = session.remoteHostId ?? null;
- const isProjectSession = session.workspaceKind === 'project';
const isDeviceLinkSession = Boolean(deviceLinkDeviceId);
const isSshSession = Boolean(remoteHostId) && !isDeviceLinkSession;
const isLocalSession = !isDeviceLinkSession && !isSshSession;
@@ -102,11 +95,6 @@ export function useSessionGitContext(session: Session): SessionGitContext {
// ── 分支:getForSession 解析真实工作目录 + 可换目录的 HEAD watch ──
useEffect(() => {
- if (!isProjectSession) {
- setHead(null);
- setBranchSource(null);
- return;
- }
// A single header instance can survive session switches. Clear the old
// task's branch immediately so a failed remote invoke cannot leave stale
// Git context beside the newly selected title.
@@ -197,7 +185,6 @@ export function useSessionGitContext(session: Session): SessionGitContext {
};
}, [
sessionId,
- isProjectSession,
isLocalSession,
isSshSession,
isDeviceLinkSession,
@@ -214,12 +201,12 @@ export function useSessionGitContext(session: Session): SessionGitContext {
const { registerPrConsumer } = usePrActions();
const sharedPrRefs = usePrRefsForSession(sessionId);
const { statuses: allStatuses } = usePrStatuses(sessionId);
- useEffect(() => {
- if (!isProjectSession) return undefined;
- return registerPrConsumer(sessionId, deviceLinkDeviceId ?? undefined);
- }, [isProjectSession, sessionId, deviceLinkDeviceId, registerPrConsumer]);
+ useEffect(
+ () => registerPrConsumer(sessionId, deviceLinkDeviceId ?? undefined),
+ [sessionId, deviceLinkDeviceId, registerPrConsumer],
+ );
- const prRefs = isProjectSession ? sharedPrRefs : EMPTY.prRefs;
+ const prRefs = sharedPrRefs;
// 状态已按会话隔离;再按本会话前 MAX_STATUS_QUERIES 条引用过滤,
// 保住旧契约「prStatuses 只含本会话条目」(消费方有 size 判断)。
const prStatuses = useMemo(() => {
@@ -232,6 +219,5 @@ export function useSessionGitContext(session: Session): SessionGitContext {
return map;
}, [prRefs, allStatuses]);
- if (!isProjectSession) return EMPTY;
return { head, branchSource, prRefs, prStatuses };
}