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): 第八轮 review 修复——重水合订阅连接状态;agentId 精确回填;文件补读串行化

Kafeifeid83bbc3f2ce5
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/BackgroundTasksBody.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/BackgroundTasksBody.tsx
index ab8406d0b7..53224964bd 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/BackgroundTasksBody.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/BackgroundTasksBody.tsx
@@ -51,7 +51,7 @@ import { cn } from '@/lib/utils';
import { Spinner } from '@/components/ui/spinner';
import {
getSessionDeviceId,
- remoteProjectsStore,
+ useRemoteDevices,
} from '@/features/device-link/remoteProjectsStore';
import { makerChatStore, EMPTY_TASK_UPDATES } from '@/lib/makerChatStore';
import type { AgentTaskUpdate, ChatMessage } from '@/lib/makerChatStore';
@@ -513,14 +513,18 @@ export function BackgroundTasksBody({
// 面板已挂载时清空 taskUpdates,布尔翻 true 即自动重水合;翻回 false 的那次
// 重跑只是多一次幂等快照(seed 仅补缺),不会循环。
const taskUpdatesEmpty = inputs.taskUpdates.size === 0;
- // liveDeviceId 参与依赖:面板挂载时被控设备恰好瞬断的话,隧道失败被降级成
- // 空表且此后无人重试 —— 订阅注册表,重连(undefined → deviceId)时重水合。
- // 本机会话恒 undefined,零开销;断连翻 undefined 的那次重跑经 sticky 路由
- // 仍走隧道,失败降级空表,无害。
- const liveDeviceId = useSyncExternalStore(
- remoteProjectsStore.subscribe,
- useCallback(() => (sessionId ? getSessionDeviceId(sessionId) : undefined), [sessionId]),
- );
+ // deviceConnectivity 参与依赖:面板挂载时被控设备恰好瞬断的话,隧道失败被
+ // 降级成空表且此后无人重试。注意 markDeviceDisconnected 型瞬断(presence
+ // offline / relay connecting)会**保留** session→device 映射,deviceId 本身
+ // 不变 —— 必须订阅设备 connected 状态,断连→重连的翻转才是重水合的真信号。
+ // 本机会话恒 'local',零开销;断连翻转的那次重跑失败降级空表,无害。
+ const remoteDevices = useRemoteDevices();
+ const sessionDeviceId = sessionId ? getSessionDeviceId(sessionId) : undefined;
+ const deviceConnectivity = sessionDeviceId
+ ? `${sessionDeviceId}:${
+ remoteDevices.find((d) => d.deviceId === sessionDeviceId)?.connected ? '1' : '0'
+ }`
+ : 'local';
useEffect(() => {
if (!sessionId) return;
let disposed = false;
@@ -535,7 +539,7 @@ export function BackgroundTasksBody({
return () => {
disposed = true;
};
- }, [sessionId, taskUpdatesEmpty, liveDeviceId]);
+ }, [sessionId, taskUpdatesEmpty, deviceConnectivity]);
const { running, completed } = useMemo(
() =>
@@ -565,26 +569,39 @@ export function BackgroundTasksBody({
}, [sessionId]);
useEffect(() => {
if (!sessionId) return;
+ const pending: string[] = [];
for (const it of completed) {
if (it.kind !== 'workflow' || it.update || !it.taskId) continue;
- const taskId = it.taskId;
- if (fileStatusRequestedRef.current.has(taskId)) continue;
- fileStatusRequestedRef.current.add(taskId);
- void getWorkflowProgressFor(sessionId, taskId)
- .then((progress) => {
+ if (fileStatusRequestedRef.current.has(it.taskId)) continue;
+ fileStatusRequestedRef.current.add(it.taskId);
+ pending.push(it.taskId);
+ }
+ if (pending.length === 0) return;
+ let disposed = false;
+ // 串行而非并发:每次 miss 都可能触发 main 侧跨 session 目录扫描(远程会话
+ // 则是隧道往返),多行同时 fan-out 会同时打出成倍的重复 IO;串行把它压成
+ // 温和的背景补读,per-task 记忆保证每任务至多一次。
+ void (async () => {
+ for (const taskId of pending) {
+ if (disposed) return;
+ try {
+ const progress = await getWorkflowProgressFor(sessionId, taskId);
const mapped = fileStatusToTaskStatus(progress?.status);
- if (!mapped) return;
+ if (!mapped || disposed) continue;
setFileStatusByTaskId((prev) => {
if (prev.get(taskId) === mapped) return prev;
const next = new Map(prev);
next.set(taskId, mapped);
return next;
});
- })
- .catch(() => {
+ } catch {
// 静默:与 wf 文件辅源的其余读取同口径,保持推导状态。
- });
- }
+ }
+ }
+ })();
+ return () => {
+ disposed = true;
+ };
}, [sessionId, completed]);
const completedResolved = useMemo(() => {
if (fileStatusByTaskId.size === 0) return completed;
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts
index c5128b9cb3..9ae9bfbd80 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts
@@ -390,6 +390,26 @@ describe('buildWorkflowTreeModel', () => {
expect(model.groups[1].agents.map((r) => r.key)).toEqual(['a2']);
});
+ it('同 phase 同 label 的多 agent 按 agentId 精确回填,不再撞名共享首条', () => {
+ // parallel 同 prompt 编队的 label 天然相同 —— agentId 才是行身份。
+ const entries: WorkflowProgressEntry[] = [
+ phaseEntry(0, 'P'),
+ agentEntry(1, { agentId: 'a1', label: 'verify', phaseTitle: 'P', state: 'done' }),
+ agentEntry(2, { agentId: 'a2', label: 'verify', phaseTitle: 'P', state: 'done' }),
+ ];
+ const file = fileProgress({
+ status: 'completed',
+ agents: [
+ fileAgent({ agentId: 'a1', label: 'verify', phaseTitle: 'P', state: 'done', resultPreview: 'first ok', durationMs: 100 }),
+ fileAgent({ agentId: 'a2', label: 'verify', phaseTitle: 'P', state: 'done', resultPreview: 'second ok', durationMs: 200 }),
+ ],
+ });
+ const model = buildWorkflowTreeModel({ entries, fileProgress: file, taskStatus: 'completed' })!;
+ const rows = model.groups[0].agents;
+ expect(rows.find((r) => r.key === 'a1')).toMatchObject({ resultPreview: 'first ok', durationMs: 100 });
+ expect(rows.find((r) => r.key === 'a2')).toMatchObject({ resultPreview: 'second ok', durationMs: 200 });
+ });
+
it('entries 已有终态 state 时文件不覆盖(采纳只发生在事件流停在非终态时)', () => {
const entries: WorkflowProgressEntry[] = [
agentEntry(0, { agentId: 'a1', label: 'alpha', phaseTitle: 'P', state: 'error', error: 'boom' }),
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts
index 41b9e3655f..2fcb43567a 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts
@@ -189,8 +189,8 @@ function groupsFromEntries(
entry.phaseTitle ??
(typeof entry.phaseIndex === 'number' ? titleByPhaseIndex.get(entry.phaseIndex) : undefined);
// 事件流收尾帧可能缺 resultPreview/durationMs/error(节流或提前断流);任务终态时
- // 按 label+phaseTitle 从文件回填,撞名取首个;只补缺,绝不覆盖 entries 已有值。
- if (fileAgentIndex) backfillRowFromFile(row, phaseTitle, fileAgentIndex);
+ // 从文件回填(agentId 精确优先、label+phaseTitle 兜底);只补缺,绝不覆盖 entries 已有值。
+ if (fileAgentIndex) backfillRowFromFile(row, entry.agentId, phaseTitle, fileAgentIndex);
if (phaseTitle && byPhase.has(phaseTitle)) {
byPhase.get(phaseTitle)!.push(row);
} else {
@@ -285,16 +285,22 @@ function fileAgentKey(label: string, phaseTitle: string | undefined): string {
return `${label}\u0000${phaseTitle ?? ''}`;
}
-/** 撞名取首个,与原线性 find 的命中语义一致。 */
-function indexFileAgents(
- fileAgents: readonly WorkflowAgentProgress[],
-): Map<string, WorkflowAgentProgress> {
- const index = new Map<string, WorkflowAgentProgress>();
+/** 回填索引:agentId 精确匹配优先(两源 agentId 同出 CLI runtime,天然同源);
+ * label+phaseTitle 兜底撞名取首个,与原线性 find 的命中语义一致。 */
+interface FileAgentIndex {
+ byAgentId: Map<string, WorkflowAgentProgress>;
+ byLabelPhase: Map<string, WorkflowAgentProgress>;
+}
+
+function indexFileAgents(fileAgents: readonly WorkflowAgentProgress[]): FileAgentIndex {
+ const byAgentId = new Map<string, WorkflowAgentProgress>();
+ const byLabelPhase = new Map<string, WorkflowAgentProgress>();
for (const agent of fileAgents) {
+ if (agent.agentId && !byAgentId.has(agent.agentId)) byAgentId.set(agent.agentId, agent);
const key = fileAgentKey(agent.label, agent.phaseTitle);
- if (!index.has(key)) index.set(key, agent);
+ if (!byLabelPhase.has(key)) byLabelPhase.set(key, agent);
}
- return index;
+ return { byAgentId, byLabelPhase };
}
/**
@@ -304,10 +310,15 @@ function indexFileAgents(
*/
function backfillRowFromFile(
row: WorkflowTreeAgentRow,
+ agentId: string | undefined,
phaseTitle: string | undefined,
- fileAgentIndex: ReadonlyMap<string, WorkflowAgentProgress>,
+ fileAgentIndex: FileAgentIndex,
): void {
- const match = fileAgentIndex.get(fileAgentKey(row.label, phaseTitle));
+ // agentId 命中即精确行;同 phase 撞 label 的多 agent(parallel 同 prompt 编队)
+ // 靠它区分,label 键只服务缺 agentId 的降级形态。
+ const match =
+ (agentId ? fileAgentIndex.byAgentId.get(agentId) : undefined) ??
+ fileAgentIndex.byLabelPhase.get(fileAgentKey(row.label, phaseTitle));
if (!match) return;
if (
CORRECTABLE_AGENT_STATES.has(row.state) &&