ai-devkit icon indicating copy to clipboard operation
ai-devkit copied to clipboard

agent send fail "unsupported terminal type unknown" trên macOS — guard pgrep -x iTerm2/Terminal không match (regression của #71)

Open dazzxq opened this issue 5 months ago • 1 comments

Tóm tắt

ai-devkit agent send luôn fail trên macOS với lỗi:

Cannot send input: unsupported terminal type "unknown". Supported: tmux, iTerm2, Terminal.app.

…mặc dù agent đích đang chạy bình thường trong iTerm2 (và lỗi tương tự với Terminal.app). Nguyên nhân là guard pgrep -x <App> trong TerminalFocusManager không match được process terminal GUI trên macOS, nên hàm return null trước khi kịp chạy AppleScript match → rơi xuống unknown.

Đáng chú ý: đây là regression của #71. Bản fix #71 (commit 4cacb56) đã đổi guard của Terminal.app từ pgrep -x Terminal sang ps ... | grep "Terminal.app", nhưng khi refactor sang execFileAsync, cả hai guard bị đưa về lại pgrep -x.

Môi trường

  • macOS 26 (Darwin 25.3.0), Apple Silicon
  • iTerm2 3.6.9
  • ai-devkit 0.40.0 (bản mới nhất trên npm)
  • Node.js 25

Vị trí bug (main HEAD)

packages/agent-manager/src/terminal/TerminalFocusManager.ts

// dòng ~102 — findITerm2Session
await execFileAsync('pgrep', ['-x', 'iTerm2']);   // không match iTerm GUI → catch → return null

// dòng ~140 — findTerminalAppWindow
await execFileAsync('pgrep', ['-x', 'Terminal']); // cùng vấn đề (regression của #71)

Khi guard này throw (vì không tìm thấy process), hàm trả null ngay, không bao giờ chạy tới đoạn osascript match-by-tty (vốn chạy hoàn toàn đúng).

Repro / bằng chứng

Trên máy bị lỗi, pgrep chạy bình thường với các process khác nhưng không match nổi iTerm2, dù ps thấy rõ:

$ pgrep node          # OK
4941 7491 8972 ...
$ pgrep zsh           # OK
2740 4434 7488 ...
$ pgrep -x iTerm2     # (rỗng)  ← guard fail tại đây
$ pgrep iTerm2        # (rỗng)
$ pgrep -f 'iTerm.app/Contents/MacOS/iTerm2'   # (rỗng)
$ ps -Axo comm | grep 'MacOS/iTerm2$'
/Applications/iTerm.app/Contents/MacOS/iTerm2  ← process tồn tại

Kiểm chứng phần còn lại của pipeline vẫn đúng — chỉ mỗi guard hỏng:

# tty của agent đích resolve đúng
getProcessTty(<pid>) = "ttys000"

# osascript match-by-tty trả "found" (cả khi chạy từ node execFile)
$ osascript -e 'tell application "iTerm" ... if tty of s is "/dev/ttys000" then return "found" ...'
found

# nhưng findTerminal() vẫn trả unknown vì guard pgrep bail trước đó
findTerminal(<pid>) = {"type":"unknown","identifier":"","tty":"/dev/ttys000"}

Sau khi thay guard pgrep -x iTerm2 bằng check qua ps, findTerminal() trả {"type":"iterm2",...} và agent send hoạt động end-to-end (đã test gửi giữa 2 session Claude Code, bên nhận trả lời được).

Đề xuất fix

Dùng lại cách của #71 (ps + grep) cho cả hai guard, ví dụ:

// iTerm2
const { stdout: psOut } = await execFileAsync('ps', ['-Axo', 'comm']);
if (!psOut.split('\n').some(l => { const c = l.trim(); return c === 'iTerm2' || c.endsWith('/iTerm2'); })) {
  return null;
}

// Terminal.app
const { stdout: psOut } = await execFileAsync('ps', ['-Axo', 'comm']);
if (!psOut.split('\n').some(l => l.trim().endsWith('/Terminal'))) {
  return null;
}

Hoặc đơn giản là bỏ luôn guard và để đoạn osascript tự xử lý (nó đã có try/catch trả null khi app không chạy) — đánh đổi là osascript có thể tự mở app nếu app chưa chạy.

Nếu anh muốn, em gửi PR luôn ạ. Cảm ơn anh vì tool 🙏

dazzxq avatar Jun 12 '26 19:06 dazzxq

Thanks e, it is also in my list, fixed in https://github.com/codeaholicguy/ai-devkit/commit/b4bdabd74b863b61dd41cd4a96d3cc58a2c8c20a

codeaholicguy avatar Jun 12 '26 21:06 codeaholicguy