Skip to content

fix: make doctor and start agent-friendly#18

Merged
4ier merged 1 commit intomainfrom
fix/agent-friendly-doctor-start
Mar 8, 2026
Merged

fix: make doctor and start agent-friendly#18
4ier merged 1 commit intomainfrom
fix/agent-friendly-doctor-start

Conversation

@4ier
Copy link
Copy Markdown
Owner

@4ier 4ier commented Mar 8, 2026

问题

AI agent(Neal_5 等)使用 Neo 时反复失败,根因:

  1. neo start 盲目启动新 Chrome 实例,杀掉用户正在用的 Chrome
  2. neo doctor 报错信息对 agent 不可操作——只说 "Not found" 没说该做什么
  3. neo doctor --fix 也会盲目重启 Chrome

Agent 陷入循环:手动启动 headless Chrome → 没扩展 → 复制 profile → 又没登录态 → pkill chrome → 再试...

改动

neo start:检测已有 Chrome

启动前先检查 CDP 端口是否已有 Chrome 在跑:

  • 已有 → 打印信息,直接返回(不启动新实例)
  • --force → 跳过检测,强制启动

neo doctor --fix:不盲目重启

Chrome CDP fix 先检测是否已在运行:

  • 已在运行 → 跳过,报告 Already running
  • 不在运行 → 才调用 neo start

neo doctor:输出结构化状态

末尾始终输出一行 JSON:

NEO_STATUS: {"ready": false, "chrome": true, "extension": false, "captures": 0, "actions": ["Neo extension not found..."]}

actions 数组包含精确的下一步指令,agent 可直接解析和执行。

验证

# Chrome 已在跑时:
neo start          # → "Chrome already running..."
neo doctor         # → NEO_STATUS JSON at end

# Chrome 没在跑时:
neo start          # → 正常启动
neo start --force  # → 强制启动

Copy link
Copy Markdown
Owner Author

@4ier 4ier left a comment

Choose a reason for hiding this comment

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

Code Review

方向正确,几个需要修的问题:

1. status.ready 逻辑有 bug — optional checks 不应影响 ready

status.ready 初始 true,任何 check 失败就 flip 到 false。但 WebSocket bridge 和 IndexedDB captures 都是 optional 的——bridge 不跑是正常的,captures 为 0 也不阻碍 neo read/snapshot/open。

当前逻辑:bridge 没跑 → ready = false → agent 以为环境没准备好。

修复:只有 Chrome CDP 和 extension 这两个 critical checks 失败才 flip ready。或者给 check 加个 optional 标记。

2. doctor --fix 的 CDP 检测是 dead code

fixFn 只在 fn 抛异常时才执行。fn 已经 fetch(CDP_URL/json/version) 失败了才进 fixFn。在 fixFn 里又 fetch 同一个 URL——如果 fn 刚失败,fixFn 里也一定失败(除非在这几毫秒内 Chrome 被外部启动了,极端 race condition)。

实际上这个检测永远走不到 return Already running 分支。这段代码无害但也无用,删掉更干净,或者加个 short sleep 再重试。

3. neo start --force 没有杀旧进程

现在 --force 只跳过检测,直接 spawn 新 Chrome。但如果旧 Chrome 占着 9222 端口,新实例会绑端口失败静默启动到其他端口。--force 要么:

  • 明确告诉用户"端口已被占用,需要先关闭现有 Chrome"
  • 要么真的 kill 旧进程再启动

当前行为是两边都不做,容易让人困惑。

4. actions 消息在 fix 成功路径上漏了

--fix 成功修复了 extension,status.extension = true 了但 status.ready 还是被之前的 catch 设成了 false。fix 成功后应该 re-evaluate ready。

Minor

  • NEO_STATUS: 行的 status.captures 在 extension 不可用时会留默认值 0 而非 -1 或 null,agent 可能误认为"有 extension 但 0 条 capture"。考虑用 null 表示"无法检测"。

@4ier 4ier force-pushed the fix/agent-friendly-doctor-start branch from 011226d to 5b26bec Compare March 8, 2026 02:26
- neo start: detect existing Chrome CDP before launching new instance
- neo doctor --fix: don't blindly restart Chrome if already running
- neo doctor: always output NEO_STATUS JSON line with actionable instructions
- Prevents agents from killing user's Chrome or looping on startup failures
@4ier 4ier force-pushed the fix/agent-friendly-doctor-start branch from 5b26bec to 59f7568 Compare March 8, 2026 03:48
@4ier 4ier merged commit 9ed4f14 into main Mar 8, 2026
1 check passed
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