Skip to content

fix(git): 禁用自动全量扫描并约束状态进程 / disable automatic scans and bound status processes - #76

Open
schattgieselman-lgtm wants to merge 2 commits into
sqfcyily:mainfrom
schattgieselman-lgtm:fix/bounded-background-git-status
Open

schattgieselman-lgtm wants to merge 2 commits into
sqfcyily:mainfrom
schattgieselman-lgtm:fix/bounded-background-git-status

Conversation

@schattgieselman-lgtm

Copy link
Copy Markdown

背景 / Context

在 Windows / Deva 0.1.16 / Git 2.49.0.windows.1 上排查到两个长期存活的孤儿进程,命令均为:

git.exe status --porcelain=v2 --branch --untracked-files=all -z

两者合计约 33 GiB 私有内存,系统分页文件曾增长至约 37.5 GiB。终止这两个具体进程后,系统提交量明显下降。

归因边界:历史父进程已退出,缺少启动链日志,因此不能证明这两个进程由 Deva 启动,也没有复现原始 OOM。 但当前 GitWidget 在挂载和每轮 AI 回复结束后执行完全相同的全量扫描;主进程没有跨请求去重,使用 execFile 的直接子进程超时,也没有显式进程树清理。本 PR 对这条风险路径做防御性修复。

Two orphan Git status processes were observed consuming ~33 GiB of private memory in total. Their original parents were gone, so attribution to Deva is not proven, and the original OOM was not reproduced. The matching automatic scan path currently lacks request coalescing and explicit descendant cleanup; this PR hardens that path without claiming to fix every memory-growth cause.

改动 / Changes

  • 后台不运行 Git:默认 git.status(dir) 只读取有界 .git / HEAD / MERGE_HEAD 元数据,支持 linked worktree;不读取 index,不递归枚举文件,也不在挂载时执行 git --version。移除 AI 回复完成后的自动扫描。
    • Background discovery uses bounded metadata I/O only. Remove automatic scans after AI turns.
  • 昂贵扫描必须明确触发:刷新菜单、打开提交框和提交前检查显式传入 { scan: true },保留 -uall / NUL 文件级语义。拒绝磁盘根、用户目录和 Windows 系统目录;不把受信子目录扩展成未授权父目录扫描。先核对 Git 实际 worktree,避免配置重定向。
    • Require strict opt-in for worktree enumeration and preserve per-file staging semantics. Guard overly broad/untrusted/redirected roots.
  • 控制并发与生命周期:同目录请求在元数据 I/O 前去重,同仓库合并在途扫描;应用内只允许一个手动扫描,其他仓库忙时立即返回可重试错误。命令共享 10 秒预算,每条命令 stdout/stderr 合计最多 4 MiB。
    • Coalesce scans and allow only one active repository scan, with bounded command time and output.
  • 只清理本次拥有的进程:Windows 已知 Git cmd/bin 布局优先直启原生 mingw Git;超时/超限/退出应用时,Windows 使用当前 PID 的 taskkill /T,POSIX 使用本次创建的进程组。不按进程名全局终止 Git。
    • Bypass known Windows Git launchers and terminate only owned process trees/groups.
  • 失败不伪装成干净工作区:共享 scanState 区分未扫描、完成和失败;界面显示扫描失败/重试,提交前扫描失败时禁止使用旧快照继续暂存和提交。
    • Represent unknown/failed scans explicitly and fail closed before committing.
  • 按 AGENTS.md 将原有共享类型和状态 hook 的提取单独放在第一个提交;未新增依赖。
    • Keep the preparatory extraction in a separate commit; no new dependencies.

输出大小限制只约束 Deva 捕获的输出,不是 Git RSS 的硬内存限额。主要保护是彻底停止隐式全量扫描,其次才是显式扫描的超时、并发限制和进程清理。
The output cap is not an OS-level Git RSS limit. Avoiding implicit scans is the primary protection.

验证 / Validation

本机 Windows(Node 24.16.0 / pnpm 10.8.0):

  • pnpm --filter @deva/desktop test:git — 19 tests passed
  • pnpm typecheck
  • pnpm build(已有 Vite 分块大小和 npm 配置警告,不影响成功退出)
  • git diff --check

回归覆盖元数据/linked worktree/异常 gitfile、默认不启动 Git、严格 opt-in、信任边界、并发去重与失败释放、真实小仓库 Git 输出、Windows launcher 选择、输出上限、超时终止后代且不影响无关进程、退出应用取消。

Tests include a real disposable Git repository and a real owned descendant process tree; no large-disk or OOM stress scan is run. CI adds the focused suite on Windows and Ubuntu / Node 22. Linux CI and live Electron UI behavior are not claimed as locally verified.

未上传本机安装包、补丁产物、个人路径或诊断日志。
No installed application bundles, local patch artifacts, private paths or personal logs are included.

…d status processes

- 后台仅读取有界元数据,手动扫描才运行 Git / read bounded metadata in the background and run Git only for explicit scans

- 合并重复请求并限制扫描时长与输出,超时清理本次进程树 / coalesce duplicate requests, bound time and output, and clean up owned process trees

- 区分未扫描与失败状态,提交前失败关闭,并添加跨平台回归测试 / distinguish unknown and failed states, fail closed before committing, and add cross-platform regression tests
@sqfcyily

Copy link
Copy Markdown
Owner

感谢这个 PR,不过目前有几处功能回归,需要先修再合。

建议的整体方向

这里其实是两个独立的问题:

  1. 扫描无界:后台自动对整个工作区做 -uall 枚举,不管仓库多大、根在哪里,也不管上一次是否还在跑。
  2. 进程生命周期没管住:超时和退出应用时不清理进程树。

本 PR 对第 2 条的处理(去重、超时、只清理自己创建的进程树)我认为可以保留。对第 1 条,彻底取消自动扫描代价偏大,下面几处回归都由此而来。建议改成更轻的方案:

  • 保留自动刷新,但换成便宜的命令:挂载时和回合结束后,执行不枚举未跟踪文件的状态查询(例如 git status --porcelain=v2 --branch --untracked-files=no,或只用 rev-parse / rev-list 取分支、upstream、ahead/behind)。开销只取决于已跟踪文件数,不会随目录树大小失控。
  • -uall 全量枚举只在显式操作时执行:手动刷新、打开提交框、提交前检查,和本 PR 现在的做法一致。
  • 两类查询都走本 PR 的有界执行器:同样受去重、超时和进程树清理约束。

这样分支名、upstream、ahead/behind 始终是真实值,同时仍然去掉了最危险的那条路径。

需要修改

1. 推送行为改变(GitWidget.tsx,push 菜单项)

推送用 status.upstream ? null : status.branch 决定是否执行 push -u origin <branch>。元数据模式下 upstream 恒为 null,所以几乎每次推送都会变成 git push -u origin <当前分支>。上游在其它 remote 或上游分支名不同的仓库,会推到错误位置并改写 upstream 配置。

2. 工作区为仓库子目录时无法提交(git-status.ts,scan())

扫描前的 deps.assertInside(summary.root) 在仓库根不属于受信根时直接失败。原实现明确支持这个场景(扫描后用 insideRootFlags 过滤根外文件)。现在打开 monorepo 的某个子包,提交框永远是“扫描失败”。

3. ahead/behind 指示失效,分支名会过期

挂载时不扫描,↑↓ 计数默认不显示;每次写操作后的 refresh() 不带扫描,会把手动刷新得到的完整状态覆盖回 not-scanned;回合结束后不再刷新,AI 在回合里切换分支后药丸不会更新。

按上面的轻量方案处理后,第 1 条和本条都会一并解决。

希望讨论

  • 10 秒 / 4 MiB 对大仓偏紧:原先是 30 秒 / 32 MB,我们遇到过上万个未跟踪文件的仓库。超限后提交被完全禁止且无法绕过,建议放宽。
  • 扫描槽可能卡死:超时后若 taskkill 失败且子进程一直不 close,Promise 不会结束,之后所有扫描都会被 “Another Git status scan is already running” 拒绝。建议在清理后加一个兜底的强制 resolve。
  • 家目录仓库:isUnsafeStatusRoot 会让用 home 做 dotfiles 仓库的用户无法提交,是否改成需确认而不是直接拒绝?

其他问题

  • 不再探测 Git 是否可用,未安装 Git 时药丸仍会显示。
  • 主进程的 scanError 是硬编码英文并直接显示在 toast 里,请走 i18n(或返回 reason 码由渲染层翻译)。

想请你补充的信息

为了把归因再坐实一些,方便的话请提供:

  • 出问题时打开的工作区路径,以及在该目录下执行 git rev-parse --show-toplevel 的结果。单个 git status 占用十几 GiB 不太正常,我怀疑仓库根比预期大得多(例如home目录或盘符根下存在 .git)。
  • where git 的输出,用来确认 Deva 解析到的是 cmd\git.exe 启动器还是真实二进制,这关系到“超时只杀掉启动器、真实进程成为孤儿”这个机制是否成立。

另外说明一点:runGitRaw 的其它命令仍用 execFile 超时,同样可能留下孤儿进程,可以后续单独跟进。

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.

2 participants