Skip to content

优化:将运行时上下文探测从同步改为异步+缓存+预热#118

Closed
xinggitxing wants to merge 1 commit into
lessweb:mainfrom
xinggitxing:perf/async-runtime-context
Closed

优化:将运行时上下文探测从同步改为异步+缓存+预热#118
xinggitxing wants to merge 1 commit into
lessweb:mainfrom
xinggitxing:perf/async-runtime-context

Conversation

@xinggitxing
Copy link
Copy Markdown
Contributor

背景

getRuntimeContext() 原为同步函数,在 Windows 上依次 spawn 5 次 Git Bash
uname -apython3 --versionnode --versioncommand -v rg
command -v jq),累计阻塞事件循环约 12 秒,严重影响启动体验。

改动内容

  1. src/prompt.ts — 核心重构

    • 将所有底层探测函数改为异步:getUnameInfoAsync()getCommandVersionAsync()
      checkToolInstalledAsync()getRuntimeVersionInfoAsync()
    • 通过 Promise.all 并发执行所有探测,消除串行阻塞
    • 引入双层缓存模式:runtimeEnvJsonPromise(防并发重复触发)+ runtimeEnvJsonCached
      (后续调用直接返回缓存的 JSON)
    • 新增 prewarmRuntimeContext():在 SessionManager 构造时后台预计算,用户输入时
      结果可能已就绪
    • 给预计算添加 .catch() 错误日志,避免静默吞异常
  2. src/session.ts — 集成调用

    • 构造时调用 prewarmRuntimeContext(this.projectRoot) 启动后台预热
    • getRuntimeContext() 调用改为 await
  3. src/tests/prompt.test.ts — 测试适配

    • 测试函数改为 asyncawait getRuntimeContext()

效果

  • ✅ 消除 ~12s 的启动阻塞
  • ✅ 并行探测 + 缓存,后续调用毫秒级返回
  • ✅ 后台预热,尽可能在用户按回车前完成探测
  • ✅ 完整的错误降级(失败时回退到 os.type() 等内置 API)
  • ✅ 所有 CI 检查通过(typecheck / lint / format / tests)

…and prewarm

The synchronous getRuntimeContext() spawned 5 Git Bash child_process
calls sequentially on Windows, blocking the event loop ~12s on startup.

Convert all probes (uname, python3 --version, node --version, rg, jq)
to async, running them in parallel via Promise.all.  Cache the result
so subsequent calls are instant.  Add prewarmRuntimeContext() called
from the SessionManager constructor so computation starts while the
user is typing.

Also adds an error .catch() to prewarmRuntimeContext to log background
failures instead of silently swallowing rejected promises.
@qorzj
Copy link
Copy Markdown
Collaborator

qorzj commented May 25, 2026

@xinggitxing
问题1:npm run test未通过

问题2:这个PR的逻辑跟main分支有差异,具体是PR会缓存env: {...}中的字段,这会导致如果用户更新了环境参数(例如:用户安装了ripgrep),也必须重启cli才能生效。

问题3:目前env环境参数虽然是串行获取,也只耗时0.15秒(windows系统实测),并没有耗时12秒这么多。

image

@qorzj qorzj closed this May 25, 2026
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