Skip to content

fix(mcp): stop caching a failed pnpm probe - #1507

Open
yoruuuchan wants to merge 1 commit into
AAswordman:devfrom
yoruuuchan:fix/mcp-pnpm-probe-cache
Open

yoruuuchan wants to merge 1 commit into
AAswordman:devfrom
yoruuuchan:fix/mcp-pnpm-probe-cache

Conversation

@yoruuuchan

Copy link
Copy Markdown
Contributor

变更说明 / Description

MCPStarter.isPnpmInstalled() 把探测失败也当成确定结果缓存:无论是拿不到共享终端会话,还是 terminal.executeCommand 抛异常,都会写入 pnpmInstalled = false。后续调用看到缓存非空就直接返回,不再重新探测。

背景与动机 / Context and motivation

Issue #1433:冷启动时共享终端会话可能还没就绪,pnpm 探测失败;失败结果被缓存后,同一个 MCPStarter 实例内的后续 initBridge() 都在 pnpm 检查处直接返回,走不到 deployBridge / startBridge。缓存把「探测没做成」和「确认没装 pnpm」混成了同一种状态。

改动范围 / Changes

兼容性与风险 / Compatibility and risks

  • 已安装 pnpm 的环境行为不变:第一次探测成功后照常缓存,后续不再执行命令。
  • 未安装 pnpm 时,每次进入启动流程会重新执行一次 command -v pnpm;开销可忽略,换来的是终端就绪后下一次调用会重新探测,而不是一直沿用第一次的失败结果。
  • 数据格式、API、配置均无变化。

关联 Issue / Related issue

Fixes #1433

验证方式 / Verification

检查或命令:
  git diff --check
  python -B ci/script/check_repo_hygiene.py --base upstream/dev --candidate HEAD
环境与变体:
  Windows 11,Python 3.12.10;未连接 Android 设备
结果:
  git diff --check 通过;
  Repository hygiene: errors=0 warnings=0。
  未运行 Gradle 构建与单元测试:项目 AGENTS.md 默认不执行构建/测试。
  未在真机上复现冷启动时序:需要等待终端会话未就绪的窗口,本次环境不具备。
  本次改动是私有函数内部的缓存语义修正,没有可自动化的回归入口;未新增 UI、字符串或文档。

证据 / Evidence

无(未在设备上采集日志)。

检查清单 / Checklist

  • 我已记录可复现验证和未运行项原因 / Reproducible verification and reasons for unrun checks are recorded
  • 我已确认 Candidate checks 覆盖改动范围,并会处理技术失败项 / Candidate checks covers the change scope and technical failures will be addressed
  • 最终 diff 无无关、临时、生成、二进制或敏感内容 / Final diff has no unrelated, temporary, generated, binary, or secret content
  • 已提供对应的回归、UI、文档/字符串或兼容性证据 / Relevant regression, UI, docs/strings, or compatibility evidence is provided

isPnpmInstalled() stored a negative result for the lifetime of the
MCPStarter instance, including when the probe could not run because the
shared terminal session was not ready yet. A cold start that probed too
early then made every later initBridge() with npx plugins return at the
pnpm check, before deployBridge/startBridge.

Cache only a successful probe. A missing session or a failed command now
returns false without writing the cache, so the next call probes again
once the terminal is ready.

Fixes AAswordman#1433

@CATMIAOZHI CATMIAOZHI left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

审计结论:✅ 批准合并

改动(1 个文件,+10/−7):MCPStarter.isPnpmInstalled() 只缓存探测成功的结果,失败(共享终端会话未就绪 / executeCommand 抛异常 / 未装 pnpm)不再写入缓存,修复冷启动时失败结果被缓存、导致同一 MCPStarter 实例内后续 initBridge() 一直在 pnpm 检查处返回、走不到 deployBridge / startBridge 的问题(Fixes #1433)。

通过的检查:

  • 目标分支为 dev,符合要求。
  • 缓存语义修正正确:入口改为 if (pnpmInstalled == true) return true,只有 command -v pnpm 命中才写缓存;会话为 null 时记一条 warn 返回 false、命令抛异常时记 error 返回 false,两路都不写缓存,与 PR 描述逐项一致。
  • 改动范围与描述一致:只动私有函数内部,未碰 START_COMMAND_THROTTLE_MS、bridge 生命周期、UI、MCPBridge;无新增依赖、无字符串改动。
  • 失败重试的代价可控:pnpm 确实未安装时每次启动流程会重新执行一次 command -v pnpm,开销可忽略(PR 描述中已说明此取舍)。
  • 无密钥 / 权限 / 外部输入相关改动;无新增用户可见字符串;result.contains("pnpm") 的判定逻辑沿用旧代码,未改动。

问题:

  1. [P2] catch (e: Exception) 会吞掉 CancellationException:isPnpmInstalled() 是 suspend 函数,terminal.executeCommand() 内部在 deferred.await() 上挂起,探测中的协程被取消时会以 CancellationException 抛出;当前 catch 会把它当作普通探测失败记一条 error 日志并返回 false,调用方随后按"pnpm 未安装"上报 NODEJS_MISSING,而不是按取消处理。建议在 catch 之前加 catch (e: CancellationException) { throw e }。这是该函数原有写法、本 PR 改写 try/catch 时未顺带处理,不阻塞本次合并,可后续跟进。
  2. [P3] 单次失败启动会探测两次:initBridge() 内 isPnpmInstalled() 返回 false 后,startPluginInternal() 的状态映射处又调一次 isPnpmInstalled(),失败结果未缓存时等于一次启动跑两遍 command -v pnpm。开销可忽略,可接受;如想省一次,可把 initBridge 的探测结果往外带。

后续可跟进(不阻塞):作者已说明未在真机上复现冷启动时序、未跑 Gradle 构建与单测;改动仅限私有函数缓存语义,风险低,建议合并后在真机冷启动场景验证一次 pnpm 探测重试是否生效。

由 水晴喵的muse 审计

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