Skip to content

perf: 降低右侧工作区的文件轮询流量与首屏加载 - #1788

Merged
qqqqqf-q merged 6 commits into
felinics:mainfrom
qqqqqf-q:refactor/memoh-300-workspace-panel-lifecycle
Nov 18, 2026
Merged

qqqqqf-q merged 6 commits into
felinics:mainfrom
qqqqqf-q:refactor/memoh-300-workspace-panel-lifecycle

Conversation

@qqqqqf-q

@qqqqqf-q qqqqqf-q commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Author

  • Human
  • Agent

Type

  • bug
  • feat
  • test

Summary

右侧工作区(文件编辑器、预览、终端)原先有三处固定开销:打开的文本文件每 2 秒完整读取一次正文,编辑器与预览并排时各读一次;进入聊天页即加载 Monaco 与 xterm,即使没有打开任何文件;编辑器每次同步主题都重跑一遍自写的 Shiki → Monaco 主题转换。本 PR 只改前端,不改后端接口、OpenAPI 规范和 @memohai/sdk。

改动后的行为:

  1. 文本文件变更检测改为先查元数据。 新增 useContainerTextFileQuery:按 bot + 路径共享一份查询和一个轮询,每 2 秒只调用轻量的 GET /bots/{bot_id}/container/fs,文件大小或修改时间变化后才读取正文。编辑器(FileViewer)与预览(PanelPreview)订阅同一份数据,编辑器加载或保存后的内容直接同步给预览。modTime 只有秒级精度,因此指纹需保持 1.5 秒不变才视为稳定:一次外部改动会先读一次正文让变化尽快显示,稳定后再读一次确认。该查询键已加入持久化排除名单,不写入 localStorage。
  2. 重型面板按需加载。 文件、终端、资产、日程面板的实现放到 defineAsyncComponent 之后,面板首次可见时才加载;未打开文件时不再下载 Monaco 与 xterm。
  3. 主题同步改为只补 Minimap 底色。 删除自写的主题转换与对 shiki/themes 的引用,新增 minimap-theme.ts,在主题注册时并入 --surface-editor,语言增量加载重新注册主题后底色不再丢失。文件树节点与面包屑从设置 store 读取明暗状态,不再各自监听 DOM。
  4. 后台面板降耗。 终端切走标签时不再序列化整个缓冲区,隐藏时停止光标闪烁;标签的尺寸监听只在可见时挂载;文件树目录内容未变时不替换列表。
  5. 修复本分支引入的一个问题。 文本文件首次打开时编辑器可能以空内容创建且不再更新。原因是读取完成与内容写入之间隔了一次渲染,而 Monaco 异步创建期间的内容更新会被跳过。现在两步在同一同步步骤内完成,编辑器创建结束后再核对一次内容,并补了回归用例。
  6. 按代码审查修正四处状态处理。 后台元数据检查失败一次后点「重试」现在立即生效,有草稿时「重新载入」的意图保留到真正读到内容;刚打开或刚保存后文件被外部修改,首次检查发现大小不同即读取,不再晚一个轮询周期;有草稿时外部改动又被还原到编辑器基线,冲突提示自动收起并放行保存;Bot 设置页 MCP 导入与网络配置弹窗里按需加载的编辑器补上加载与失败占位。

未改变的行为:未保存修改、冲突提示、对比、重新加载、仍要保存的语义与入口不变;后台标签里的编辑器仍全部保持挂载(曾尝试的后台编辑器回收已在本 PR 内撤掉,收益无法测量,且会清空干净文件的撤销历史)。

已知限制:变更检测依赖 modTime,同一秒内且大小不变的外部写入会晚一个稳定期(约 1.5 秒)才被发现。保存冲突保护使用内容 revision,不受影响。文件树的目录轮询次数没有减少,仍为每分钟约 60 次。

实测对比

基线为 main 的 ff8e7bca4,两边均为 vite build + vite preview,同一个 bot、同一批文件。内存与脚本体积在撤掉后台编辑器回收之前测得,之后未重测。

指标 main 本 PR
进聊天页、不开文件时下载的脚本与样式 2543 KB 1741 KB
聊天页 JS 堆 23 MB 15 MB
打开一个文本文件后的 JS 堆 67 MB 25 MB
3 KB 文件静置 1 分钟的读取流量 101 KB 13 KB
220 KB 文件静置 1 分钟的读取流量 6.7 MB 13 KB
文件树目录请求(每分钟) 60 次 60 次

代码量

相对 main 共 35 个文件,+1656 / −731。

类别 文件数 新增 删除
生产代码 28 1134 617
其中剔除注释与空行 — 890 521
测试 7 522 114

无生成物与机械性变更。净增主要来自三个新文件:共享文本文件查询(319 行,其中约三分之一是说明并发与时钟前提的注释)、Minimap 底色同步(98 行)、面板加载占位(73 行,由原 panel-loading.vue 拆出)。

需要重点 review 的行为:

  • use-container-text-file.ts 中的稳定期判定与 syncEpoch:决定何时读正文,以及在途读取不得覆盖刚保存的内容。
  • file-viewer.vue 的加载与轮询流程改为订阅共享查询,冲突状态机的入口随之调整;其中 reconcileText / applyDiskSnapshot 决定共享状态对编辑器意味着什么,两轮修复都落在这里。
  • monaco-editor/index.vue 创建完成后的内容核对。

Related Issues

Validation

以下均为 Agent 执行的自动检查与浏览器自测,未经过人工 QA。

自动检查(head 88b6aec14):

  • pnpm typecheck(apps/web)通过。
  • eslint 对改动文件 0 error;2 个 warning 位于 panel-preview.test.ts,属既有的单文件多组件规则。
  • node scripts/check-ui-contract.mjs 通过。
  • vitest 相关 39 个测试文件、343 个用例中 341 个通过;失败的 2 个在未改动的 use-session-follow-up-queue.test.ts,属本机环境问题,以 CI 结果为准。首次打开的回归用例,以及审查修正对应的 4 个用例(3 个新增、1 个既有断言随行为更新),均已确认在修复前的代码上失败。
  • 未跑全量 vitest:在本机更大范围运行时有 6 个文件 14 个用例失败(缺少 document / sessionStorage、i18n 未加载),均不在改动范围内,未在基线上逐一对照,以 CI 结果为准。

浏览器自测(宿主机 Vite http://127.0.0.1:8083,指向本地 OSS 后端,用测试 bot 的工作区文件):

  1. 打开文本文件,内容正确显示。
  2. 外部修改未编辑的文件,编辑器自动刷新。
  3. 编辑后保存,草稿标记消失。
  4. 有未保存修改时外部修改,出现提示条。
  5. 点「对比」显示磁盘内容与本地编辑的差异;对比期间磁盘再次变化,提示已过期,「刷新差异」后更新。
  6. 「重新加载」丢弃草稿并载入磁盘内容。
  7. 外部删除文件出现删除提示;以相同内容重建后提示消失。
  8. 侧边预览随外部修改更新。
  9. 刷新页面后标签与内容恢复。
  10. 冲突时普通保存被拦截,「仍要保存」写入成功。
  11. 终端标签可用,能输入并得到输出。
  12. 标签隐藏期间该文件请求数为 0,切回后内容更新。
  13. 图片、PDF 可显示,切走再切回正常。
  14. Bot 设置页 MCP 导入对话框中的编辑器正常显示(head 88b6aec14 上重测)。

第 1–13 项在 73331e216 上执行;88b6aec14 的四处修正由单元测试覆盖,未在浏览器中逐一重放。

未覆盖:弹窗内编辑器的加载与失败占位(本机加载过快未出现)、Bot 设置页「网络」里的编辑器对话框、多个文件标签并存、DOCX 预览、Agent 在对话中写文件触发的刷新、Electron 桌面端、浏览器控制台报错检查。

Screenshots / Recordings

打开文本文件:编辑器显示文件内容

有未保存修改时文件被外部修改:出现提示条与对比、重新加载、仍要保存入口

对比期间磁盘再次变化:标出已过期并提供刷新差异

文件被外部删除:提示已删除并可保存以重建

编辑器与预览并排:外部修改后两侧同步更新

冲突时选择仍要保存:写入成功

终端面板按需加载后可用

Bot 设置页 MCP 导入对话框中的编辑器

PDF 预览

Human QA

  • Human QA passed

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 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-11-18T07:33:31.675513Z 88b6aec Draft marked ready
ℹ️ 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.

@github-actions github-actions Bot added change:server Changes backend code, configuration, or API contracts change:web Changes web frontend or shared frontend packages feat Adds or improves functionality size:L PR size uses the larger of added or deleted lines, excluding generated files labels Nov 13, 2026

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 492daff040

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +93 to +97
const editorRetained = useSurfaceRetention(
'editor',
() => `${props.params.api.id}:${renderUrl.value}`,
visible,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Initialize renderUrl before registering surface retention

Opening an attachment tab now calls useSurfaceRetention() before renderUrl is initialized. That composable immediately registers a key watcher, which evaluates this getter even without immediate, so setup throws ReferenceError: Cannot access 'renderUrl' before initialization. This prevents both text and image attachment previews, including their Download UI, from mounting. Move the renderUrl computed declaration above this registration.

Useful? React with 👍 / 👎.

const latestKnownRevision = inCompare
? ((compareStale.value ? observedDiskRevision : compareDiskRevision.value) || baseRevision.value)
: (observedDiskRevision || baseRevision.value)
const omitContent = isDirty.value || (inCompare && Boolean(compareDiskRevision.value))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep metadata-only poll results out of the content-apply path

If a dirty editor starts a poll and the user undoes their edits before a changed-disk response arrives, omitContent remains true but resolvePolledTextChange() reevaluates isDirty as false and returns apply. The response intentionally contains empty content, so applyPolledTextContent() replaces both buffers with an empty string and advances baseRevision; subsequent polls report unchanged and leave the editor falsely clean and blank. Preserve the omission information through completion and fetch the actual content before taking the apply branch.

Useful? React with 👍 / 👎.

promise: Promise<{ data: HandlersFsReadResponse }>
}

const inFlightReads = new Map<string, InFlightReadEntry>()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use Pinia Colada for the new filesystem request coordinator

The new module-global map and subscriber/abort bookkeeping introduce an independent request lifecycle for a normal REST read. This violates root AGENTS.md's “state uses Pinia + Pinia Colada” requirement and frontend M3, required by apps/web/AGENTS.md, which assigns server-data caching, loading, retries and invalidation to Colada. The generated getBotsByBotIdContainerFsReadQuery and key factory already exist in packages/sdk/src/@pinia/colada.gen.ts; implement conditional-read coordination through a shared Colada query/composable instead of adding another coordinator outside the existing query reset and invalidation mechanisms.

AGENTS.md reference: AGENTS.md:L192-L193

Useful? React with 👍 / 👎.

Comment on lines +134 to +136
const sig = `${name}|${surfaceHex}`
if (definedMonacoThemeSignatures.has(sig)) return false
definedMonacoThemeSignatures.add(sig)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Track the currently registered surface per Monaco theme name

When appearance changes produce surfaces A → B → A while retaining the same Shiki theme name, defineTheme(name, ...) overwrites the same Monaco registration first with A and then B. This set remembers both historical signatures, so returning to A makes this function return false and syncEditorTheme() skips restoring A's definition. The minimap therefore retains B's background rather than matching the current editor surface. Track the last registered surface per theme name, or register each signature under a distinct Monaco theme name.

Useful? React with 👍 / 👎.

@qqqqqf-q
qqqqqf-q marked this pull request as draft November 13, 2026 12:34
@qqqqqf-q
qqqqqf-q force-pushed the refactor/memoh-300-workspace-panel-lifecycle branch from 1ba7550 to 9e9b2d7 Compare November 13, 2026 13:31
@github-actions github-actions Bot removed the change:server Changes backend code, configuration, or API contracts label Nov 13, 2026
@qqqqqf-q
qqqqqf-q force-pushed the refactor/memoh-300-workspace-panel-lifecycle branch from 9e9b2d7 to 9a6e492 Compare November 13, 2026 13:32
- 文件查看器不再自行读取正文,加载、轮询、保存后同步与 Compare 都走共享查询,
  磁盘状态由一处对账函数决定
- 文档预览的下载与渲染改由面板可见性驱动,不再依赖 KeepAlive 钩子
- Bot 详情页的两个编辑器对话框与附件面板按需加载 Monaco
- 懒加载占位不再嵌套面板框;只有未保存修改会让隐藏的编辑器常驻
- 合并重复的文件指纹与 404 判断,修正过期注释
读取完成后先清 loading、隔一个微任务才写入内容,中间的一次渲染会用空缓冲
创建编辑器;编辑器异步创建期间到达的内容又被 model-value 监听跳过。
现在读取结果在同一同步步骤内应用,编辑器创建完成后也会补上期间变化的内容。
没有量出收益,而它在文件查看器里多出一组挂载条件与视图状态的保存恢复。
已打开过的文件编辑器在面板隐藏时保持挂载,与 main 上 KeepAlive 的行为一致。
@qqqqqf-q
qqqqqf-q force-pushed the refactor/memoh-300-workspace-panel-lifecycle branch from 9a6e492 to 73331e2 Compare November 18, 2026 06:03
@qqqqqf-q qqqqqf-q changed the title refactor: 重构右侧工作区面板变更检测与多面板后台生命周期 perf: 降低右侧工作区的文件轮询流量与首屏加载 Nov 18, 2026
- 读取完成后清除此前失败的元数据检查状态,「重试」可立即恢复;
  重新载入的意图保留到读取被应用或读取本身失败
- 同步后的首次元数据检查发现文件大小不同即读取,不再等下一轮
- 外部改动被还原到编辑器基线后收起冲突提示并放行保存
- Bot 设置页弹窗内的编辑器补上加载与失败占位
@qqqqqf-q
qqqqqf-q marked this pull request as ready for review November 18, 2026 07:20
@qqqqqf-q
qqqqqf-q merged commit 909d96a into felinics:main Nov 18, 2026
15 checks passed

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 88b6aec14e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (recovered || revertedToBase) {
diskState.value = 'available'
observedExternalRevision.value = ''
if (revertedToBase) lastLoadedAt.value = Date.now()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Advance the save watermark after successful unchanged reads

When a same-bot exec emits a wildcard filesystem beacon while this file is visible and clean, but its contents are unchanged, loadTextContent() successfully refetches the same revision and reaches this branch without advancing lastLoadedAt. After the user edits the file, handleSave() still sees fsChangedAt > lastLoadedAt and refuses to POST, reporting a conflict that the successful read already ruled out. The previous explicit-load path updated this timestamp even for unchanged content. Refresh the verification watermark after a successful same-revision read without replacing the user's buffer.

Useful? React with 👍 / 👎.

Comment on lines +95 to +98
if (modTimeMs < lastSync.wallAt - FINGERPRINT_SETTLE_MS - CLOCK_SKEW_TOLERANCE_MS) {
lockedFingerprint = fingerprint
contentFresh = false
return 'skip'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Read changed fingerprints before applying the clock shortcut

If the workspace clock is two minutes behind the browser, a same-size external write can produce a different fingerprint whose modTime still satisfies this condition. The tracker then locks that new fingerprint without reading its content; subsequent observations return through the earlier locked-fingerprint check, so neither the editor nor preview receives the change, even after the settling period. Replacing a file with a preserved older modification time has the same problem. A changed fingerprint must not be considered synchronized solely because its size matches and its timestamp looks old relative to the browser clock.

Useful? React with 👍 / 👎.

}

watch(
[() => textQuery.data.value, () => textQuery.error.value, textQuery.pollError, () => props.botId, filePath] as const,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reconcile the disk snapshot when the draft becomes clean

If an external revision arrives while the editor is dirty, let its fingerprint settle and then undo the local edits back to originalContent. The editor becomes clean, but this watcher does not observe that transition and the separate isDirty watcher only emits the dirty flag. Subsequent polls skip the unchanged fingerprint, so nothing reapplies the already-fetched newer disk snapshot: the tab remains marked clean while displaying old content and a conflict chip until another disk change or manual reload. Trigger reconciliation on the dirty-to-clean transition, retaining the existing load/save guards.

Useful? React with 👍 / 👎.

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

Labels

change:web Changes web frontend or shared frontend packages feat Adds or improves functionality size:L PR size uses the larger of added or deleted lines, excluding generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant