Add input detection before message injection#6
Conversation
- Add isInputClear() to detect empty input buffer via prompt pattern matching - Add getCursorX() to get cursor position for stability detection - Add waitForClearInput() loop-wait mechanism that polls until input is clear - Integrate input detection into injectNextMessage() - waits for clear input before injecting, falls back to forceful clear only on timeout - Add configurable inputWaitTimeoutMs and inputWaitPollMs options - Add dev:local, dev:unlink, dev:rebuild npm scripts for local development
There was a problem hiding this comment.
Pull request overview
This PR introduces intelligent input detection before message injection to prevent interference with user-typed text in tmux sessions. The implementation adds a polling mechanism that waits for input to clear before injecting relay messages, with a fallback to forceful clearing on timeout.
Key Changes:
- Added input detection methods (
isInputClear(),getCursorX()) that use prompt pattern matching and cursor position tracking - Implemented
waitForClearInput()with configurable timeout and polling intervals - Refactored message injection to wait for clear input instead of immediately clearing the buffer
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/wrapper/tmux-wrapper.ts | Added input detection logic, cursor tracking, and wait mechanism; moved buffer clearing to timeout fallback |
| package.json | Added npm scripts for local development workflow |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const isClear = pattern.test(lastLine); | ||
|
|
||
| if (this.config.debug) { | ||
| this.logStderr(`isInputClear: lastLine="${lastLine.substring(0, 40)}", clear=${isClear}`); |
There was a problem hiding this comment.
The substring operation could fail if lastLine is shorter than 40 characters. Use lastLine.substring(0, Math.min(40, lastLine.length)) or simply rely on substring's built-in handling by removing the length check.
| stableCursorCount++; | ||
| // If cursor has been stable for 3 polls and at typical prompt position (1-4), | ||
| // the agent might be done but we just can't match the prompt pattern | ||
| if (stableCursorCount >= 3 && cursorX <= 4) { |
There was a problem hiding this comment.
The magic numbers 3 and 4 should be defined as named constants (e.g., STABLE_CURSOR_THRESHOLD and MAX_PROMPT_CURSOR_POSITION) to improve code readability and maintainability.
…into claude/fix-pr-comments-ZLyFU
- Add named constants for cursor stability detection: - STABLE_CURSOR_THRESHOLD (3) for poll count before assuming clear - MAX_PROMPT_CURSOR_POSITION (4) for typical prompt cursor position - Add constants for log truncation lengths: - DEBUG_LOG_TRUNCATE_LENGTH (40) for debug log messages - RELAY_LOG_TRUNCATE_LENGTH (50) for relay command logs - Use Math.min for safe substring operations in all log truncations - Add comprehensive unit tests for constants and truncation logic
before injecting, falls back to forceful clear only on timeout