Skip to content

后台任务输出日志在打开的标签页内自动跟随磁盘变化刷新 - #775

Open
leehom0123 wants to merge 2 commits into
spacering-net:mainfrom
leehom0123:feat/live-output-auto-reload
Open

leehom0123 wants to merge 2 commits into
spacering-net:mainfrom
leehom0123:feat/live-output-auto-reload

Conversation

@leehom0123

@leehom0123 leehom0123 commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

背景

后台任务执行时,界面上会有一个提醒条,条上的"输出"按钮通过 openFilePreview 打开任务日志,而日志写在系统临时目录,这个目录不属于任何已注册的工作区根目录,后端文件监听听不到它。所以,标签页一旦打开,内容不会随着文件变动而更新。已有的文件检查只在切换标签页那一刻运行,用户如果保留日志页在界面上,永远就看不到新的内容。

改动

新增组件 LiveOutputFileWatcher,挂在 conversation-shell.tsx 的 AsyncTaskStrip 旁边,它自身不渲染任何界面。任务还在执行时,如果界面上打开的正是这个任务的日志标签页,而且标签页没有未保存的修改、不在保存中、也不是图片或 Office 预览,组件就每 2 秒拿 etag 比对一次标签页与磁盘上的文件。

比对发现文件变了,走 applyExternalReload 把新内容写进标签页。这条路径自带代数检查,不会覆盖用户没保存的内容,也会刷新 git 基线,正合适。读不到文件(最常见是日志被清理掉了)就走 rejectFileTab,把错误显示在标签页上。任务结束、用户关闭标签页或者开始编辑,定时器随即拆掉,不再跟随。

组件单独挂载:fileTabs 这份高频变动的状态只触发本组件空渲染,不会牵动会话外壳。

验证

  • 新增 live-output-file-watcher.test.tsx,7 个测试:任务存活时磁盘变化写入、etag 相同不写入、有未保存修改的标签页不轮询、任务结束后停止轮询、忽略非日志标签页、日志消失显示错误、读取期间切换标签页丢弃结果。7 个全过。
  • pnpm test 全量:764 个测试文件、11363 个测试通过(合入最新 main 后复跑)。
  • eslint、prettier --check、tsc --noEmit 全部干净。

取舍

为什么用轮询而不是文件系统监听:后端现有的 notify 监听器有已知的事件丢失问题,src-tauri/src/workspace_state/mod.rs:1590 的注释记载 macOS FSEvents 会丢事件,加了监听也要靠轮询兜底。现在的轮询只在任务还在执行、日志页显示在最前、没有未保存修改时才跑,空闲时几乎没有开销。

The task strip's "Output" button opens the task log through
openFilePreview, but the log lives under the OS temp root, outside every
notify-watched workspace root, so the open-tab watcher never sees it
change. A null-rendering leaf component now polls the active, clean
output tab every 2 s while its task is live and routes changes through
applyExternalReload; the interval stops as soon as the task settles,
the tab closes, or the buffer turns dirty.
…-reload

Keep the strip's new agentName prop alongside the null-rendering
LiveOutputFileWatcher in one fragment, and type the test helper's
overrides as Partial<FileLikeWorkspaceTab> now that FileWorkspaceTab
is a discriminated union with a browser variant.
@leehom0123
leehom0123 force-pushed the feat/live-output-auto-reload branch from f3cc04e to b867f97 Compare November 10, 2026 08:23
@leehom0123

Copy link
Copy Markdown
Contributor Author

本分支已合并最新 main(5c03d7af),冲突已解决(b867f979):conversation-shell.tsx 中 main 新增的 AsyncTaskStrip agentName prop 与本 PR 的 <LiveOutputFileWatcher> 一并保留;测试 helper 类型改为 Partial<FileLikeWorkspaceTab>,适配 main 对 FileWorkspaceTab 的联合类型重构。

合并后本地验证:tsc、eslint、prettier 干净,pnpm test 全量 764 个测试文件、11363 个用例全部通过。

@xintaofei 已处于可合并状态,请审查。

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.

1 participant