Skip to content

Add input detection before message injection#6

Merged
khaliqgant merged 4 commits intomainfrom
claude/detect-input-text-C6nFk
Dec 21, 2025
Merged

Add input detection before message injection#6
khaliqgant merged 4 commits intomainfrom
claude/detect-input-text-C6nFk

Conversation

@khaliqgant
Copy link
Copy Markdown
Member

  • 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

- 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
@khaliqgant khaliqgant requested a review from Copilot December 21, 2025 11:04
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Comment thread src/wrapper/tmux-wrapper.ts Outdated
const isClear = pattern.test(lastLine);

if (this.config.debug) {
this.logStderr(`isInputClear: lastLine="${lastLine.substring(0, 40)}", clear=${isClear}`);
Copy link

Copilot AI Dec 21, 2025

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment thread src/wrapper/tmux-wrapper.ts Outdated
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) {
Copy link

Copilot AI Dec 21, 2025

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
claude and others added 3 commits December 21, 2025 11:12
- 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
@khaliqgant khaliqgant merged commit e2f3fc4 into main Dec 21, 2025
6 checks 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.

3 participants