Skip to content

fix(desktop): 修复伙伴与群聊前进后退历史并优化首次进入加载速度 - #15927

Open
dashhuang wants to merge 2 commits into
mainfrom
cindy/keen-mendel
Open

dashhuang wants to merge 2 commits into
mainfrom
cindy/keen-mendel

Conversation

@dashhuang

@dashhuang dashhuang commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

这次改了什么

摘要

本次在同一个 PR 中修复桌面端「伙伴(Companion)与群聊」的两个体验问题:

  1. 修复从伙伴/群聊后退到普通任务后无法再前进回来的问题

    • 根因:useSessionBrowseHistory 只通过 sessionIdFromPathname 将 /cc-agent/:sessionId 纳入浏览栈,所有 /bots/*(含本地伙伴、远程伙伴与 /bots/groups/:groupId 群聊/直聊)均被当作栈外页面处理。从伙伴或群聊点后退可回到栈顶普通任务,但伙伴/群聊从未入栈,导致前进按钮置灰(canForward = false)。
    • 修复:将本地伙伴(/bots/:id 与 /bots/:id/session/:sid 统一归一为 /bots/:id)、远程伙伴(/bots/remote/:deviceId/:botId)和群聊/直聊(/bots/groups/:groupId,保留 ?conversationKind=direct,剥离 ?messageId=... / ?groupSettings=1 避免重复占格或重播跳转)纳入浏览栈;后退/前进命中本地伙伴时直接解析其健康 canonical 会话并携带 BOT_DIRECT_ENTRY_STATE 直达 /bots/:id/session/:sid,避免绕行 BotsHomeView;同时订阅伙伴名册与群聊列表并在删除、deleting 或切换账号(ownerKey)时清理失效历史项与跟踪状态。
  2. 彻底优化首次点击进入伙伴与群聊的加载速度

    • 根因定位与优化项:
      1. 移除首跳携带的重型设置抽屉:BotsFeatureLayout 此前静态导入并无条件挂载 BotSettingsDrawer 与 BotGroupSettingsDrawer,导致首次进入任意伙伴或群聊对话都要先同步加载并解析 13 个分包(498.9 KB)的设置/渠道/语音/壁纸代码。改为 React.lazy 且仅在 URL 带 ?settings=1 / ?groupSettings=1 时挂载,并给设置按钮加悬停/聚焦预取。
      2. 拆分设置重型子页:BotsHomeView 与 RemoteTeammateSettings 将渠道(BotChannelsPage)、实时语音(CompanionVoiceSettings)、壁纸(BotWallpaperSettings)、项目(BotProjectsSettings)、自动化(BotAutomationsSettings)改为共享缓存按需加载,并在设置行悬停/聚焦时预取,消除混用静态与动态导入。
      3. 补齐伙伴与群聊路由分包空闲预热和侧栏交互预取:新增 botRoutePreload.ts 共享缓存加载器——主窗口首帧稳定后空闲预热全部聊天视图分包(BotsFeatureLayout + BotSessionView + RemoteBotSessionView + BotGroupChatView,不含重型设置抽屉与子页);router.tsx、CompanionSplitPane、侧栏伙伴行/群聊行/头像条/合体 Cindy 行/隐藏行/收起态图标栏共用同一缓存加载器并在悬停与聚焦时预取对应分包。
      4. 补齐伙伴与群聊主按键按下(onPointerDown)实时数据预取与去重:
        • 本地/远程伙伴在非激活项主按键按下时预取 makerChatStore.ensureInitialMessages;
        • 群聊在 botGroupDetailCache.ts 新增 prefetchBotGroupLiveDetail / consumeBotGroupLiveDetail:本地群聊与服务端群聊(含首次进入尚无磁盘缓存的房间)在主按键按下时即刻发起实时 getBotGroup 读取,BotGroupChatView 首轮挂载直接复用该 Promise(0 次重复 IPC),遇到推送(含 attachments)、账号代次切换或 NOT_FOUND/PERMISSION_DENIED 立即作废并清理缓存;同时为已知本地群聊跳过无意义的 cacheOnly 重复 SQLite 读取,并为服务端群聊短暂去重磁盘缓存 miss。
      5. 消除直达缺口、双重卸载与首帧校验无效重渲染:侧栏头像条、合体 Cindy 行、隐藏本地伙伴行、/bots 入口以及群聊内成员私聊入口(openMemberDirectChat)命中健康 canonical 会话时改为直达 botOpenRoute(bot)(带 BOT_DIRECT_ENTRY_STATE);MainLayout 将裸 /bots 归入 'cc-agent' contentFeatureKey,避免 /bots → /bots/:id 触发 <FadeSwitcher> 双重卸载;稳定 BotSessionView 与 CCAgentSessionView 的 identity / mentions / botAssistantAvatar 引用。

生产包首跳分包实测对比(vite build --config vite.renderer.config.ts):

路由 / 场景 优化前(新增分包 / 体积) 优化后(新增分包 / 体积) 降幅
主入口 App 静态闭包 59 chunks / 12,921.8 KB 71 chunks / 12,930.4 KB +0.07%(无膨胀)
BotsFeatureLayout 壳层 13 chunks / 498.9 KB 1 chunk / 2.4 KB -99.5%
本地伙伴首进(Layout + BotSessionView) 17 chunks / 515.0 KB 5 chunks / 18.2 KB -96.5%(主窗口空闲预热)
远程伙伴首进(Layout + RemoteBotSessionView) 15 chunks / 508.9 KB 3 chunks / 12.5 KB -97.5%(主窗口空闲预热)
群聊首进(Layout + BotGroupChatView) 15 chunks / 571.9 KB 4 chunks / 85.0 KB -85.1%(主窗口空闲预热 + 按下实时数据预取)
/bots 入口页(Layout + BotsHomeView) 13 chunks / 498.9 KB 5 chunks / 106.2 KB -78.7%
BotSettingsDrawer(仅点设置时加载) 静态合入 Layout 6 chunks / 135.6 KB(壁纸子页再拆 30.9 KB) 按需加载

变更类型

  • feat 新功能
  • fix 缺陷修复
  • refactor / perf 重构或性能优化
  • docs / test / chore 文档、测试或工程维护
  • 其他:

范围

  • 关联 Issue / 需求:无(来自内部使用反馈)
  • 本 PR 包含:
    • 任务/伙伴/群聊统一浏览历史栈(sessionBrowseHistory.ts、useSessionBrowseHistory.ts)与回归单测
    • 伙伴与群聊路由分包拆分、按需挂载设置抽屉、主窗口空闲预热、侧栏悬停/按下实时预取复用、直达路由与渲染引用稳定化
  • 明确不包含:
    • 不修改主进程 SQLite / localDb 读写并发模型(保持 botCanonicalSession.test.ts 串行约束)
    • 不修改视觉布局、文案、颜色或控件样式
  • 用户可见变化:
    • 从本地伙伴、远程伙伴或群聊/直聊后退到普通任务后,可正常点击前进按钮或使用 ⌘] / 触控板手势前进回原对话
    • 首次点击进入伙伴或群聊更迅速:分包已完成空闲预热、设置抽屉按需挂载、按下即刻发起可复用的实时数据读取
  • 是否存在 breaking change:无

UI 变化

不涉及视觉、排版、色彩或控件结构变更,仅修复导航历史行为并优化路由与数据加载性能。

  • 引用的设计规范:docs/design-rules/task-sidebar.md(侧栏伙伴行/群聊行/头像条直达聊天入口,不改变行高与交互态);docs/design-rules/DESIGN.md(导航前进/后退历史栈与路由过渡容器一致性,加载占位 Spinner 保持 role="status" 与既有 token)。

怎么验证的

自动验证

# 1. 桌面端全分片类型检查(main + main-tests + renderer,覆盖 12,310 个文件)
pnpm --filter desktop typecheck
结果:PASS(0 errors)

# 2. 相关渲染进程回归测试文件 + 隔离单测 + 主进程 botCanonicalSession
cd apps/desktop && pnpm exec vitest run \
  src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx \
  src/renderer/features/bots/__tests__/botGroupSidebar.test.tsx \
  src/renderer/features/bots/__tests__/CompanionSplitPane.test.tsx \
  src/renderer/features/bots/__tests__/botGroupStore.test.ts \
  src/renderer/features/bots/__tests__/teammatesSection.test.tsx \
  src/renderer/components/layout/__tests__/ChromeActions.fullscreen.test.tsx
结果:6 passed / 577 passed(叠加首轮 22 个渲染单测文件与主进程 botCanonicalSession.test.ts 369/369 全部 PASS)

# 3. 工程门禁与设计清单校验
pnpm check:test-hygiene && pnpm check:engineering-guards && node scripts/design-inventory.mjs --check && pnpm check:dev-docs
结果:全部 PASS(test-hygiene 472 files within baseline;engineering-guards 12 strict rules 0 violations;design-inventory 66 surfaces 最新;dev-docs 15/15)

# 4. 生产渲染包构建与分包体积对比
cd apps/desktop && pnpm exec vite build --config vite.renderer.config.ts --outDir /tmp/cindy-bots-group-dist
结果:PASS,拆分目标无 mixed static+dynamic warning,实测体积降幅见摘要表

测试价值说明(engineering-conventions.md §3.2)

  • sessionBrowseHistory.test.ts(单文件实测 <0.1s):覆盖 browseTargetFromLocation / localBotIdFromBrowseTarget / botGroupIdFromBrowseTarget 对本地伙伴、远程伙伴、群聊/直聊及非聊天静态段(/bots/list、/bots/roster 等)的归一与过滤。
  • ChromeActions.fullscreen.test.tsx(单文件实测 ~1.8s):端到端覆盖伙伴与群聊(含通知 ?messageId=...、多群切换、直聊)后退到普通任务再前进回来、基地路由重定向去重、栈外后退、删除/deleting 伙伴与删除群聊自动清理、以及 ownerKey 切换重置已见跟踪集合。
  • BotGroupChatView.test.tsx(本文件为存量 385 用例完整群聊视图套件,本次新增 5 用例):覆盖 pointerDown 实时预取在挂载时复用(0 重复 IPC)、推送(含已缓存时的 attachments 原地分支)与账号切换作废、已知本地群聊(省略 serverBacked)跳过冗余 cacheOnly 读取,以及预取 NOT_FOUND 立即清理旧内存缓存。
  • botGroupSidebar.test.tsx(单文件实测 ~5.4s,本次新增 1 用例):覆盖列表态与头像条态下本地群聊与服务端群聊主按键 pointerDown 预取行为及激活项/右键/修饰键防护。
  • botsFeatureLayout.test.tsx(单文件实测 ~0.2s):覆盖进入伙伴/群聊对话时不挂载 BotSettingsDrawer / BotGroupSettingsDrawer,仅在 URL 打开 ?settings=1 / ?groupSettings=1 时按需挂载。
  • teammatesSection.test.tsx(单文件实测 ~2.0s):覆盖头像条与合体 Cindy 行点击直达 /bots/:id/session/:sid(带 botDirectEntry: true),及仅在非激活项主按键 pointerDown 时触发 ensureInitialMessages 预取。
  • botNavigation.test.ts / botSessionRead.test.tsx / botSettings.test.tsx / remoteTeammateSettings.test.tsx(单文件实测均 <3.5s):覆盖缓存加载器失败清缓存重试、mentions 引用稳定、/bots 入口直达 canonical 会话与懒加载设置子页。

读写与通知开销说明(engineering-conventions.md §8)

  • 主窗口空闲预热:主窗口可见后延迟 2.5s 并经 requestIdleCallback 执行一次纯本地前端分包动态加载(BotsFeatureLayout、BotSessionView、RemoteBotSessionView、BotGroupChatView),0 次 IPC、0 次数据库读写、0 次远端请求;卸载时清理监听、定时器与 idle 回调,次级窗口与独立侧栏窗口不触发。
  • 侧栏悬停/聚焦:仅预取本地静态分包(带 Promise 去重),0 次 IPC/数据库/远端请求。
  • 侧栏主按键 onPointerDown:
    • 伙伴:对非激活项最多触发 1 次 makerChatStore.ensureInitialMessages(sessionId)(与现有 SessionCard / SessionItem 一致);
    • 群聊:对非激活项发起 1 次实时 getBotGroup 并在 BotGroupChatView 首轮挂载时消费复用(0 重复 IPC;已知本地群聊跳过 cacheOnly;服务端群聊的磁盘 cacheOnly miss 在 5s 内去重,且仅对未在途或新按键生效)。不增加额外 bots.list / listBotGroups 或轮询。
  • useSessionBrowseHistory 订阅:复用现有内存态 subscribeBotProfiles / subscribeBotGroups(0 次 IPC 读),仅在内存中对当前最多 50 格的浏览栈做 O(n) 过滤,卸载或 ownerKey 切换时自动解绑。

手工验证

不涉及(本环境以自动化回归测试 + 生产包分包闭包实测为主)。

未执行的验证

  • 未启动真机 Electron GUI 做交互点击手工录屏(已通过 jsdom 交互单测覆盖按钮/快捷键后退前进、抽屉开关、侧栏点击预取与群聊实时预取复用,并通过真实 vite build 验证生产分包闭包)。

风险

风险分类

  • 无已知风险
  • SQLite / migration
  • system prompt
  • 协议兼容
  • 权限 / 安全 / 用户数据
  • 存量插件兼容(批准状态 / 指纹 / manifest 校验 / 安装布局 / 包格式)
  • 插件引擎(ACP)与插件模型影响面(新任务 / 模型选择器 / 权限 / 多端 / 引擎枚举分支)
  • 原生层 / fingerprint / OTA
  • 跨平台差异
  • 其他:

影响与回滚

  • 影响范围:仅限 Desktop 渲染进程导航浏览历史与伙伴/群聊相关路由、设置子页及前端预取缓存,不涉及数据存储、主进程协议或插件系统。
  • 回滚 / 降级方式:直接 revert 本 PR 提交即可完整恢复原有静态导入、群聊挂载读取与浏览栈行为。

提交前检查

  • 已 review 完整 diff
  • 每个 commit 都带 DCO 签名(git commit -s,见 DCO)
  • UI 改动已在「UI 变化」注明引用的设计规范章节(不涉及 UI 则跳过)
  • 未提交凭证、令牌或授权文件
  • 已核对受影响的文档,行为变化涉及的旧结论已同步修订(不涉及则无需修改文档)
  • 已确认测试结果或说明未执行原因
  • 涉及数据读取、通知后刷新、界面频繁写入或常驻组件时,已在「怎么验证的」写明:每个用户动作和每次通知触发几次 list/扫描/IPC/写事务(读远程数据再写远程调用次数和回包字节),以及隐藏或卸载后是否停止(见 docs/dev-rules/engineering-conventions.md §8;远程读取同时见 docs/dev-rules/server-channel-review.md)
  • 如用户操作、界面入口或报错发生变化,已同步 apps/docs 或说明自动同步会从哪些来源更新(不涉及新增入口或文案变化)。

Note

Medium Risk
Touches navigation history, routing, and lazy-load boundaries across companion UI; regressions could affect back/forward, account switches, or first-open settings, but scope is renderer-only with broad test coverage.

Overview
Fixes desktop back/forward so local companions, remote companions, and group chats join the same browse stack as ordinary tasks (browseTargetFromLocation + navigation that resolves local bots to canonical session routes). The stack dedupes /bots/:id vs session URLs, drops deleted/deleting bots and removed groups, and resets on account ownerKey change. Bare /bots stays on the same layout feature key to avoid an extra remount before redirect.

First-open performance is addressed with shared botRoutePreload loaders: router and split pane use them, the main window idle-warms chat chunks after first paint, and the sidebar preloads on hover/focus/pointerDown (including ensureInitialMessages and live group getBotGroup prefetch reused on mount). Settings drawers and heavy settings subpages (channels, voice, wallpaper, projects, automations) load only when the URL opens them, with hover preloads. Sidebar and /bots entry use botOpenRoute to skip the home hop; BotSessionView / avatar memo tweaks reduce pointless re-renders after validation.

Reviewed by Cursor Bugbot for commit c6299fc. Bugbot is set up for automated code reviews on this repo. Configure here.

1. 浏览历史栈(sessionBrowseHistory / useSessionBrowseHistory):
   - 将本地伙伴(/bots/:id 与 /bots/:id/session/:sid 归一为 /bots/:id)、远程伙伴与群聊/直聊纳入任务浏览栈,保留非聊天子页在栈外;
   - 后退/前进回到本地伙伴时直接解析 canonical 会话并带 BOT_DIRECT_ENTRY_STATE 跳转,避免绕行 BotsHomeView;
   - 订阅伙伴名册与群聊列表变更,删除或切换账号时及时清理失效条目与状态。

2. 伙伴首次进入加载速度优化:
   - BotsFeatureLayout 改为按需懒加载并仅在 URL 打开设置参数时挂载 BotSettingsDrawer / BotGroupSettingsDrawer,将 BotsFeatureLayout 首跳分包从 498.9 KB(13 chunks)降至 2.4 KB(1 chunk),本地伙伴首进从 515.0 KB(17 chunks)降至 18.2 KB(5 chunks);
   - BotsHomeView 与 RemoteTeammateSettings 将重型设置子页(渠道、语音、壁纸、项目、自动化)改为按需懒加载并在行悬停/聚焦时预取;
   - 新增 botRoutePreload 共享缓存加载器:主窗口首帧稳定后空闲预热轻量聊天路由分包,侧栏悬停/聚焦/主按键按下预取对应路由分包与会话消息(ensureInitialMessages / preloadBotGroupDetail);
   - 侧栏头像条、合体 Cindy 行、隐藏伙伴行及 /bots 入口命中健康 canonical 会话时直达 /bots/:id/session/:sid,且 MainLayout 将 /bots 归入同一 contentFeatureKey 避免 FadeSwitcher 双重卸载;
   - 稳定 BotSessionView 与 CCAgentSessionView 的 mentions / botAssistantAvatar 引用,减少首次校验回包触发的无效重渲染。

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
@dashhuang
dashhuang requested a review from a team as a code owner December 6, 2026 00:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T00:37:36.159923Z c6299fc New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] 本次修改看起来可以合并,未发现需要阻止合并的问题。

Summary

本次修改将伙伴聊天纳入任务浏览历史,并减少首次进入伙伴时需要加载的代码。

  • Back and forward now return to local, remote, and group chats.
  • Companion chats open with less code on the first load.

Diagram

%%{init: {'theme': 'neutral', 'htmlLabels': false}}%%
flowchart TD
  A[打开任务或伙伴] --> B[归一浏览目标]
  B --> C[记录浏览历史]
  C --> D[点击后退或前进]
  D --> E{本地伙伴}
  E -->|是| F[解析当前主会话]
  E -->|否| G[打开历史入口]
  F --> H[重新检查伙伴绑定]
  H --> I[显示聊天或返回修复入口]
Loading

Reviews (1) · Last reviewed commit: "fix(desktop): 修复伙伴前进后退历史并优化首次进入加载速度" · Reviewed by Greptile

@zqchris zqchris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto-approved by Chris’s trusted-author rule for makecindy/cindy. This is an automated approval.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4076254. Configure here.

<Suspense fallback={null}>
<LazyBotGroupSettingsDrawer />
</Suspense>
) : null}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Settings close skips dialog focus

Medium Severity

BotSettingsDrawer and BotGroupSettingsDrawer now unmount as soon as settings or groupSettings leaves the URL. They no longer stay mounted while open flips to false, so Radix never runs dialog close. Mentioning a member from group settings therefore never reaches onCloseAutoFocus, and focusBotGroupMemberComposer does not run.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 4076254. Configure here.

}
}
seenGroupIds = currentIds;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hidden groups dropped from history

Medium Severity

Group browse entries are removed whenever their id leaves getBotGroupListSnapshot(), not only when the group is deleted. That snapshot is already filtered by visibleGroups, so hiding a group drops it from back/forward. forget can also move the cursor while that chat is still open.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 4076254. Configure here.

- botRoutePreload: scheduleBotRoutePreload 纳入 BotGroupChatView 空闲预热,避免首次点击群聊等待 85 KB 分包冷加载;收起态侧栏伙伴入口与 CompanionSplitPane 共用同一缓存加载器;
- botGroupDetailCache / BotGroupSidebarRow / TeammatesSection / BotGroupChatView: 新增 prefetchBotGroupLiveDetail / consumeBotGroupLiveDetail,本地群聊与服务端群聊在主按键按下时即刻发起可复用的实时 getBotGroup 读取,BotGroupChatView 首轮挂载直接消费该 Promise(0 重复 IPC),遇到推送(含 attachments)、账号代次切换或 NOT_FOUND/PERMISSION_DENIED 立即作废并清理缓存;
- BotGroupChatView: 已知本地群聊跳过冗余 cacheOnly 预读;群成员发起私聊走 botOpenRoute 直达 canonical 会话;
- 补齐群聊实时预取复用与失效隔离、本地群聊去重预读、群聊通知/多群间前进后退回归测试。

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
@dashhuang dashhuang changed the title fix(desktop): 修复伙伴前进后退历史并优化首次进入加载速度 fix(desktop): 修复伙伴与群聊前进后退历史并优化首次进入加载速度 Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants