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 徽标支持 device-link 远程会话

Dasha58eeb99e700
diff --git a/apps/desktop/src/renderer/contexts/PrRefsContext.tsx b/apps/desktop/src/renderer/contexts/PrRefsContext.tsx
index f525b55210..08adccf3f3 100644
--- a/apps/desktop/src/renderer/contexts/PrRefsContext.tsx
+++ b/apps/desktop/src/renderer/contexts/PrRefsContext.tsx
@@ -47,6 +47,27 @@ const PrStatusesContext = createContext<PrStatusesContextValue>({
fetchStatusesForSession: () => undefined,
});
+/**
+ * 行级动作 context——value 恒定(回调身份稳定,无状态字段),SessionItem /
+ * SessionCard 可以安全订阅而不被 statuses 更新连带重渲染(文件头性能边界)。
+ * device-link 远程会话的 PR 引用不在本地 db(listAllPrRefs 看不见它们),
+ * 由渲染中的行按需经远程通道补拉,落进同一张 refsBySession 映射。
+ */
+interface PrActionsContextValue {
+ /**
+ * 远程(device-link)会话按需拉取 PR 引用:复用白名单通道
+ * git-context:pr-refs:list(与聊天顶栏 useSessionGitContext 同一条),结果
+ * 合入共享 refs 映射并登记 sessionId→deviceId,后续状态查询自动走远程路由。
+ * 每会话只发一次(失败允许下次重试);仅当「任务信息」勾选 pr 且行实际
+ * 渲染时才调用——数量被列表 collapse 上限约束。
+ */
+ fetchRefsForRemoteSession: (sessionId: string, deviceId: string) => void;
+}
+
+const PrActionsContext = createContext<PrActionsContextValue>({
+ fetchRefsForRemoteSession: () => undefined,
+});
+
function groupBySession(rows: SessionPrRef[]): Map<string, SessionPrRef[]> {
const map = new Map<string, SessionPrRef[]>();
for (const row of rows) {
@@ -69,11 +90,17 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {
refsRef.current = refsBySession;
// tip 快速划过多行时防止同一会话重复在飞;main 侧有 60s 缓存,这里不做 TTL。
const inFlightSessions = useRef(new Set<string>());
+ // 远程会话:已发起过引用拉取的 sessionId(成功后不再重复;失败移除允许重试),
+ // 以及 sessionId → deviceId 路由表(状态查询据此改走 device-link 通道)。
+ const remoteFetchedSessions = useRef(new Set<string>());
+ const remoteDeviceBySession = useRef(new Map<string, string>());
useEffect(() => {
// owner 切换边界:先清掉上一 owner 的缓存,无 owner 时保持空、不发 IPC。
setRefsBySession(new Map());
setStatuses(new Map());
+ remoteFetchedSessions.current.clear();
+ remoteDeviceBySession.current.clear();
if (dataOwnerId === null) return;
let cancelled = false;
@@ -126,6 +153,9 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {
}, [dataOwnerId]);
// 回调身份稳定(空闭包依赖):refs 经 refsRef 读,statuses 经函数式 setState 写。
+ // 远程会话(remoteDeviceBySession 命中)状态改走 device-link 通道——被控端持有
+ // gh token,且 main 侧按该会话已提取的引用过滤查询(git-context/ipc.ts 的
+ // fail-closed 逻辑),与聊天顶栏同一条安全路径。
const fetchStatusesForSession = useRef((sessionId: string) => {
const refs = refsRef.current.get(sessionId)?.slice(0, MAX_STATUS_QUERIES);
if (!refs || refs.length === 0) return;
@@ -133,9 +163,13 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {
inFlightSessions.current.add(sessionId);
void (async () => {
try {
- const results = await window.electronAPI.gitContext.getPrStatuses(
- refs.map((r) => ({ owner: r.owner, repo: r.repo, prNumber: r.prNumber })),
- );
+ const queries = refs.map((r) => ({ owner: r.owner, repo: r.repo, prNumber: r.prNumber }));
+ const deviceId = remoteDeviceBySession.current.get(sessionId);
+ const results = deviceId
+ ? ((await window.electronAPI.deviceLink.invoke(deviceId, 'git-context:pr-status', [
+ { sessionId, queries },
+ ])) as PrStatusResult[])
+ : await window.electronAPI.gitContext.getPrStatuses(queries);
setStatuses((prev) => {
const next = new Map(prev);
for (const r of results) next.set(prStatusKey(r), r);
@@ -149,6 +183,32 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {
})();
}).current;
+ // 远程会话 PR 引用按需拉取(回调身份稳定)。结果进共享 refs 映射,渲染路径
+ // (usePrRefsForSession)对本地/远程一视同仁;空结果也算成功(不再重发)。
+ const fetchRefsForRemoteSession = useRef((sessionId: string, deviceId: string) => {
+ if (remoteFetchedSessions.current.has(sessionId)) return;
+ remoteFetchedSessions.current.add(sessionId);
+ remoteDeviceBySession.current.set(sessionId, deviceId);
+ void (async () => {
+ try {
+ const refs = (await window.electronAPI.deviceLink.invoke(
+ deviceId,
+ 'git-context:pr-refs:list',
+ [sessionId],
+ )) as SessionPrRef[];
+ if (refs.length > 0) {
+ setRefsBySession((prev) => new Map(prev).set(sessionId, refs));
+ }
+ } catch (err) {
+ // 断链/超时:移出已发集,该行下次挂载(或勾选变化)时重试。
+ log.warn('remote pr refs fetch failed', String(err));
+ remoteFetchedSessions.current.delete(sessionId);
+ }
+ })();
+ }).current;
+
+ const actionsValue = useMemo(() => ({ fetchRefsForRemoteSession }), [fetchRefsForRemoteSession]);
+
const statusesValue = useMemo(
() => ({ statuses, fetchStatusesForSession }),
[statuses, fetchStatusesForSession],
@@ -156,7 +216,9 @@ export function PrRefsProvider({ children }: { children: ReactNode }) {
return (
<PrRefsMapContext.Provider value={refsBySession}>
- <PrStatusesContext.Provider value={statusesValue}>{children}</PrStatusesContext.Provider>
+ <PrActionsContext.Provider value={actionsValue}>
+ <PrStatusesContext.Provider value={statusesValue}>{children}</PrStatusesContext.Provider>
+ </PrActionsContext.Provider>
</PrRefsMapContext.Provider>
);
}
@@ -172,3 +234,8 @@ export function usePrRefsForSession(sessionId: string): SessionPrRef[] {
export function usePrStatuses(): PrStatusesContextValue {
return useContext(PrStatusesContext);
}
+
+/** 行级动作(value 恒定,订阅不会因 statuses 更新而重渲染)。 */
+export function usePrActions(): PrActionsContextValue {
+ return useContext(PrActionsContext);
+}
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx
index 2d36b28deb..d2e20d8271 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx
@@ -61,7 +61,7 @@ import { toast } from '@/lib/toast';
import { buildSessionDeepLink } from '@/lib/deepLink';
import { createLogger } from '@/lib/logger';
import type { Session } from '@/lib/ccAgent.types';
-import { usePrRefsForSession } from '@/contexts/PrRefsContext';
+import { usePrActions, usePrRefsForSession } from '@/contexts/PrRefsContext';
import { buildSessionInfoPieces, SessionInfoMeta } from './SessionInfoMeta';
import { useTaskInfoFields } from '../hooks/useTaskInfoFields';
import { highlightSegments } from '../lib/highlightSegments';
@@ -238,6 +238,14 @@ export function SessionCard({
const cardInfoPieces = buildSessionInfoPieces(session, taskInfoFields, activityIso, t);
const cardPrRefs = usePrRefsForSession(session.id);
const cardInfoPrRef = taskInfoFields.includes('pr') ? cardPrRefs[0] : undefined;
+ // 远程(device-link)会话的 PR 引用不在本地 db,勾选 pr 且行渲染时按需经
+ // 远程通道补拉(与 SessionItem 同一条路径;usePrActions 的 value 恒定)。
+ const { fetchRefsForRemoteSession } = usePrActions();
+ const wantsPrInfo = taskInfoFields.includes('pr');
+ const remoteDeviceId = session.deviceLinkDeviceId;
+ useEffect(() => {
+ if (wantsPrInfo && remoteDeviceId) fetchRefsForRemoteSession(session.id, remoteDeviceId);
+ }, [wantsPrInfo, remoteDeviceId, session.id, fetchRefsForRemoteSession]);
const [menuPos, setMenuPos] = useState<{ x: number; y: number } | null>(null);
const [archivePending, setArchivePending] = useState(false);
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionItem.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionItem.tsx
index 905b49a2b9..62686923cf 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionItem.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionItem.tsx
@@ -41,7 +41,7 @@ import { makerChatStore } from '@/lib/makerChatStore';
import { WorktreeBadge } from '@/components/sidebar/WorktreeBadge';
import { SessionStatusIcon } from './SessionStatusIcon';
import { SessionRenameInput } from '../SessionRenameInput';
-import { usePrRefsForSession } from '@/contexts/PrRefsContext';
+import { usePrActions, usePrRefsForSession } from '@/contexts/PrRefsContext';
import { SessionTooltip } from './SessionTooltip';
import {
DropdownMenu,
@@ -312,6 +312,14 @@ export const SessionItem = memo(function SessionItem({
const prRefs = usePrRefsForSession(session.id);
// 任务信息复选(C 期):行右侧信息槽内容,与整理菜单同源共享状态。
const { fields: taskInfoFields } = useTaskInfoFields();
+ // 远程(device-link)会话的 PR 引用不在本地 db,勾选 pr 且行渲染时按需经
+ // 远程通道补拉(每会话一次,usePrActions 的 value 恒定不引入重渲染)。
+ const { fetchRefsForRemoteSession } = usePrActions();
+ const wantsPrInfo = taskInfoFields.includes('pr');
+ const remoteDeviceId = session.deviceLinkDeviceId;
+ useEffect(() => {
+ if (wantsPrInfo && remoteDeviceId) fetchRefsForRemoteSession(session.id, remoteDeviceId);
+ }, [wantsPrInfo, remoteDeviceId, session.id, fetchRefsForRemoteSession]);
// mod+1..9 序号徽标:模块 store 按 sessionId 精准订阅(性能不变量第 2 条),
// 非按住态恒为 null,不惊动 memo。
const ordinalBadgeLabel = useSessionOrdinalBadge(session.id);
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionRowRenderIsolation.test.ts b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionRowRenderIsolation.test.ts
index e1e892455d..f2d037ee42 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionRowRenderIsolation.test.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/sessionRowRenderIsolation.test.ts
@@ -62,9 +62,13 @@ vi.mock('react-router-dom', () => ({
vi.mock('@/contexts/PrRefsContext', () => {
const EMPTY: unknown[] = [];
+ // usePrActions 的真实实现保证 value 恒定;mock 同样给稳定引用,
+ // 避免 effect deps 每渲染变化干扰本文件的重渲染计数断言。
+ const ACTIONS = { fetchRefsForRemoteSession: vi.fn() };
return {
usePrRefsForSession: () => EMPTY,
usePrStatuses: () => ({ statuses: new Map(), fetchStatusesForSession: vi.fn() }),
+ usePrActions: () => ACTIONS,
};
});
diff --git a/docs/product-rules/sidebar-redesign-plan.md b/docs/product-rules/sidebar-redesign-plan.md
index ea6fe8eb40..b935c17e79 100644
--- a/docs/product-rules/sidebar-redesign-plan.md
+++ b/docs/product-rules/sidebar-redesign-plan.md
@@ -143,9 +143,13 @@ More/Archive,复用 `TimeActionsSlot`),行 2 = 固定 1 行预览;整行恒两
`git-context:pr-status` IPC 实时查。侧栏常驻显示必须:仅查视口可见行、拉长
TTL、批量合并;一会话多 PR 时取 `lastSeenAt` 最新;无 token/查询失败降级为
灰色 `#号`。
-6. **远程会话的 token/费用/PR**:字段在远端 DB,device-link 投影扩字段触碰
- `protocol-compatibility` 规则。第一期仅本机会话生效,远程留空,协议扩展
- 单独评审。
+6. **远程会话的 token/费用/PR**:token/费用字段在远端 DB,device-link 投影
+ 扩字段触碰 `protocol-compatibility` 规则——第一期仅本机会话生效,远程留空,
+ 协议扩展单独评审。**PR 不受此限**(2026-08-12 实机反馈修正):device-link
+ 白名单已有 `git-context:pr-refs:list` / `git-context:pr-status` 逐会话通道
+ (聊天顶栏在用),侧栏对远程会话按需补拉引用、状态经远程路由查询(被控端持
+ gh token + main 侧按已提取引用 fail-closed 过滤),不改协议。触发条件:勾选
+ pr 且行实际渲染,每会话一次,数量被 collapse 上限约束。
7. **筛选与置顶**:定稿为不作用于置顶。置顶条目不会因筛选消失。
8. **PR 状态色登记**:绿/红对齐 `--diff-add-fg` / `--diff-del-fg`,「侧边栏 PR
状态指示」作为新消费场景需在 DESIGN.md §2 登记后方可实现。