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 修复——重连重水合;降级场景详情兜底文本

Kafeifeia515e36f0ff5
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 0a19ceeee1..ab8406d0b7 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
@@ -49,6 +49,10 @@ import type { LucideIcon } from 'lucide-react';
import { cn } from '@/lib/utils';
import { Spinner } from '@/components/ui/spinner';
+import {
+ getSessionDeviceId,
+ remoteProjectsStore,
+} from '@/features/device-link/remoteProjectsStore';
import { makerChatStore, EMPTY_TASK_UPDATES } from '@/lib/makerChatStore';
import type { AgentTaskUpdate, ChatMessage } from '@/lib/makerChatStore';
import {
@@ -454,6 +458,12 @@ function WorkflowDetail({
<div className="min-h-0 flex-1 overflow-y-auto px-2 py-2">
{model ? (
<WorkflowProgressTree model={model} />
+ ) : item.update?.summary || item.resultPreview ? (
+ // 降级兜底:事件树与 wf 文件都拿不到(老被控端 / SSH / 未落盘即退出)时,
+ // 展示 workflow 的结果文本 —— 面板是主视图,点开不能只剩一句「暂无进度」。
+ <div className="whitespace-pre-wrap break-words px-2 py-1 text-12 leading-5 text-[var(--text-secondary)]">
+ {item.update?.summary ?? item.resultPreview}
+ </div>
) : (
<div className="px-2 py-6 text-center text-12 text-[var(--text-tertiary)]">
{t('rightSidebar.backgroundTasks.noProgress')}
@@ -503,6 +513,14 @@ 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]),
+ );
useEffect(() => {
if (!sessionId) return;
let disposed = false;
@@ -517,7 +535,7 @@ export function BackgroundTasksBody({
return () => {
disposed = true;
};
- }, [sessionId, taskUpdatesEmpty]);
+ }, [sessionId, taskUpdatesEmpty, liveDeviceId]);
const { running, completed } = useMemo(
() =>
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/listSessionTasks.test.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/listSessionTasks.test.ts
index b0d7801bd7..1d8d58fe2c 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/listSessionTasks.test.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/listSessionTasks.test.ts
@@ -172,6 +172,26 @@ describe('listSessionTasks 配对', () => {
});
});
+ it('workflow 条目携带 resultPreview(保留换行、截断);非 workflow 不带', () => {
+ // 详情视图降级兜底(事件树与 wf 文件都拿不到时)的数据来源。
+ const longTail = 'x'.repeat(3000);
+ const { completed } = listSessionTasks({
+ messages: [
+ toolUse('c1', 'tu-wf', 'Workflow'),
+ toolResult('r1', 'tu-wf', `第一行结论\n第二行明细\n${longTail}`),
+ toolUse('c2', 'tu-task', 'Task', { description: 'agent task' }),
+ toolResult('r2', 'tu-task', 'agent result'),
+ ],
+ taskUpdates: undefined,
+ isSessionStreaming: false,
+ });
+ const wf = completed.find((it) => it.toolUseId === 'tu-wf')!;
+ expect(wf.resultPreview).toContain('第一行结论\n第二行明细');
+ expect(wf.resultPreview!.length).toBeLessThanOrEqual(2000);
+ expect(wf.resultPreview!.endsWith('…')).toBe(true);
+ expect(completed.find((it) => it.toolUseId === 'tu-task')!.resultPreview).toBeUndefined();
+ });
+
it('提取出的 taskId 进去重别名:同 taskId 的孤儿 update 不再重复出行', () => {
const orphan = makeUpdate({ taskId: 'w9gvjxzk1', taskType: 'local_workflow' });
const { running, completed } = listSessionTasks({
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/listSessionTasks.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/listSessionTasks.ts
index 930325c83e..8321ce658b 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/listSessionTasks.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/listSessionTasks.ts
@@ -32,6 +32,12 @@ export interface SessionTaskItem {
update?: AgentTaskUpdate;
toolCallClientId?: string;
toolUseId?: string;
+ /**
+ * workflow 的最终输出文本(配对 tool_result,保留换行、截断到 2000):
+ * 详情视图在事件树与 wf 文件都拿不到时(老被控端 / SSH / 未落盘即退出)
+ * 的兜底展示,让「点开面板」在降级场景也不落空。仅 workflow 条目携带。
+ */
+ resultPreview?: string;
/** 消息窗口内出现顺序;孤儿 update 排最后。 */
orderIndex: number;
}
@@ -186,6 +192,32 @@ export function extractWorkflowTaskId(content: unknown): string | undefined {
return match?.[1];
}
+/** workflow 最终输出的兜底展示上限(详情视图无进度树时用)。 */
+const RESULT_PREVIEW_MAX = 2000;
+
+/**
+ * tool_result 内容 → 可展示文本:字符串原样(保留换行,区别于单行化的
+ * compactText),对象/数组 JSON 化;超限截断加省略号;空白纯空返回 undefined。
+ */
+function clipResultText(content: unknown): string | undefined {
+ let text: string;
+ if (typeof content === 'string') {
+ text = content;
+ } else if (isRecord(content) || Array.isArray(content)) {
+ try {
+ text = JSON.stringify(content, null, 1);
+ } catch {
+ return undefined;
+ }
+ } else {
+ return undefined;
+ }
+ const trimmed = text.trim();
+ if (!trimmed) return undefined;
+ if (trimmed.length <= RESULT_PREVIEW_MAX) return trimmed;
+ return `${trimmed.slice(0, RESULT_PREVIEW_MAX - 1)}…`;
+}
+
/** 与 findTaskUpdate 同口径:先 toolUseId、后 clientId 查 map。 */
function findUpdate(
taskUpdates: ReadonlyMap<string, AgentTaskUpdate> | undefined,
@@ -296,6 +328,7 @@ export function listSessionTasks(input: {
update?.provider ?? (toolName.startsWith('collab:') ? 'codex' : 'claude-code');
const taskId = update?.taskId ?? derivedTaskId;
+ const resultPreview = kind === 'workflow' ? clipResultText(resultContent) : undefined;
items.push({
// key 用统一 taskId 链(含 derivedTaskId):恢复行先出现、快照水合后 update
// 才到时 key 不得从 toolUseId 翻成 taskId —— 详情视图按 key 选中,翻 key 会
@@ -309,6 +342,7 @@ export function listSessionTasks(input: {
...(update ? { update } : {}),
toolCallClientId: msg.clientId,
...(toolUseId ? { toolUseId } : {}),
+ ...(resultPreview !== undefined ? { resultPreview } : {}),
orderIndex: i,
});
}