Skip to content

fix: generalize tmux prompt injection timing for kimi/qwen/opencode (#42)#44

Closed
xzq-xu wants to merge 1 commit intoHKUDS:mainfrom
xzq-xu:fix/spawn-prompt-timing
Closed

fix: generalize tmux prompt injection timing for kimi/qwen/opencode (#42)#44
xzq-xu wants to merge 1 commit intoHKUDS:mainfrom
xzq-xu:fix/spawn-prompt-timing

Conversation

@xzq-xu
Copy link
Copy Markdown
Contributor

@xzq-xu xzq-xu commented Mar 20, 2026

Summary

Fixes #42 — prompt injection in tmux mode fails for non-Claude CLIs (kimi, qwen, opencode, etc.) because the old code only waited 1 second before send-keys, which is insufficient for slower-starting TUIs.

  • Generalized readiness detection: Replaced Claude-specific _wait_for_claude_ready with _wait_for_cli_ready that polls up to 30 seconds using CLI-specific prompt indicators and a generic "content stabilized" fallback (3 consecutive identical pane snapshots). This works for any CLI, not just known ones.
  • Buffer-based prompt injection for all CLIs: Extracted _inject_prompt_via_buffer that uses tmux load-buffer/paste-buffer for ALL interactive CLIs (previously only Claude used this). This prevents multi-line prompt escaping issues and is more reliable than send-keys.
  • First-class support for new CLIs: Added is_kimi_command, is_qwen_command, is_opencode_command detectors and updated is_interactive_cli() to include them.
  • Skip-permissions flags: kimi (--yolo), qwen (--dangerously-skip-permissions), opencode (--yolo) — in both tmux and subprocess backends.

Before vs After

Before After
Claude 30s polling + buffer injection 30s polling + buffer injection (unchanged)
kimi/qwen/opencode sleep(1) + send-keys (broken) 30s polling + buffer injection
Unknown CLI sleep(1) + send-keys 30s polling with stabilization fallback + buffer injection

Test plan

  • All 260 tests pass (241 existing + 19 new)
  • Ruff lint clean
  • New tests cover: CLI detection (5), readiness polling with stabilization (3), skip-permissions for kimi/qwen/opencode (3 subprocess + 1 tmux integration), prompt indicator detection (5), and buffer injection verification (1)
  • Existing Claude/Codex/Gemini/Nanobot tests pass unchanged

Made with Cursor

Fixes HKUDS#42 — prompt injection in tmux mode could fail for non-Claude
CLIs because the old code only waited 1 second before sending keys,
which is insufficient for slower-starting TUIs.

Changes:

1. Add CLI detection for kimi, qwen (qwen-code), and opencode in
   command_validation.py, and include them in is_interactive_cli().

2. Replace the Claude-specific _wait_for_claude_ready with a generic
   _wait_for_cli_ready that:
   - Checks for common TUI prompt indicators (❯, >, ›, $, %)
   - Falls back to a "content stabilized" heuristic (3 consecutive
     identical pane snapshots) for unknown CLIs
   - Polls up to 30 seconds instead of sleeping a fixed 1 second

3. Extract _inject_prompt_via_buffer helper that uses tmux
   load-buffer/paste-buffer for ALL interactive CLIs (not just Claude),
   preventing multi-line prompt escaping issues.

4. Add skip-permissions flags:
   - kimi: --yolo
   - qwen: --dangerously-skip-permissions
   - opencode: --yolo

5. Add 19 new tests covering CLI detection, readiness polling with
   stabilization fallback, and skip-permissions for all new CLIs.

Made-with: Cursor
@xzq-xu
Copy link
Copy Markdown
Contributor Author

xzq-xu commented Mar 20, 2026

Closing in favor of a rebased PR — upstream merged #36 (kimi support + NativeCliAdapter refactor) which conflicts with this branch. Will re-submit the remaining improvements (generic readiness detection, buffer-based injection for all CLIs, qwen/opencode support) on top of current main.

@xzq-xu xzq-xu closed this Mar 20, 2026
@xzq-xu xzq-xu deleted the fix/spawn-prompt-timing branch March 20, 2026 18:45
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.

spawn启动智能体时的提示词有可能不生效

1 participant