Created
January 16, 2026 19:49
-
-
Save barretts/4e09dfa5f676be827206a10c46a365ba to your computer and use it in GitHub Desktop.
monitor_claude.exp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/expect -f | |
| # debugging thing | |
| # exp_internal 1 | |
| # --- Configuration --- | |
| set command "/Users/bsonntag/.local/bin/claude" | |
| # CHANGE THIS LINE: Add a wildcard * to jump over the formatting codes | |
| set watch_phrase "esc*to interrupt" | |
| set proceed_prompt "1*Yes" | |
| set initial_timeout 30 | |
| set monitor_timeout 1 | |
| # --- End Configuration --- | |
| spawn $command --dangerously-skip-permissions --permission-mode bypassPermissions /init | |
| send_user "🚀 Starting '$command --dangerously-skip-permissions --permission-mode bypassPermissions /init'.\n" | |
| # --- STAGE 1: Wait for the phrase to APPEAR --- | |
| send_user "Stage 1: Waiting for '$watch_phrase' to appear (max $initial_timeout seconds)...\n" | |
| expect { | |
| timeout { | |
| send_user "\n❌ ERROR: Timed out. The phrase '$watch_phrase' never appeared.\n" | |
| exit 1 | |
| } | |
| # CHANGE: Using -gl (glob match) with wildcards (*) to ignore formatting. | |
| -gl "*$watch_phrase*" { | |
| send_user "✅ Phrase found. Proceeding to Stage 2.\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Claude process exited before monitoring could start.\n" | |
| # Don't exit here - continue to second command | |
| } | |
| } | |
| # --- STAGE 2: Wait for proceed prompt and loop --- | |
| send_user "Stage 2: Waiting for '$proceed_prompt' prompt...\n" | |
| set timeout 30 | |
| expect { | |
| -gl "*$proceed_prompt*" { | |
| send_user "🔵 Found '$proceed_prompt'.\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Claude process finished.\n" | |
| # Don't exit here - continue to second command | |
| } | |
| timeout { | |
| send_user "\n⚠️ WARNING: '$proceed_prompt' never appeared.\n" | |
| # Don't exit here - continue to second command | |
| } | |
| } | |
| # Now loop: send enter, wait for esc, wait for esc to disappear | |
| send_user "Stage 2: Starting monitoring loop...\n" | |
| set timeout $monitor_timeout | |
| while {1} { | |
| # Wait a moment and send enter | |
| send_user "🔵 Sending enter...\n" | |
| send "\r" | |
| # Wait for esc to appear | |
| set timeout 30 | |
| send_user " ⏳ Waiting for '$watch_phrase' to appear...\n" | |
| expect { | |
| -gl "*$watch_phrase*" { | |
| send_user " ✅ '$watch_phrase' appeared.\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Claude process finished.\n" | |
| break | |
| } | |
| timeout { | |
| send_user "\n⚠️ WARNING: '$watch_phrase' never reappeared. Breaking loop.\n" | |
| break | |
| } | |
| } | |
| # Wait for esc to disappear | |
| set timeout $monitor_timeout | |
| send_user " ⏳ Waiting for '$watch_phrase' to disappear...\n" | |
| expect { | |
| -gl "*$watch_phrase*" { | |
| exp_continue | |
| } | |
| timeout { | |
| send_user " ✅ '$watch_phrase' disappeared. Looping...\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Claude process finished.\n" | |
| break | |
| } | |
| } | |
| # Check if we should break (if esc never reappeared, meaning task is done) | |
| set timeout 5 | |
| expect { | |
| -gl "*$proceed_prompt*" { | |
| # Good, prompt is still there, continue loop | |
| } | |
| timeout { | |
| # No prompt visible, task might be done | |
| send_user "\n✅ Task appears complete. Sending Ctrl+C twice.\n" | |
| send "\x03" | |
| send "\x03" | |
| break | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Claude process finished.\n" | |
| break | |
| } | |
| } | |
| } | |
| # --- STAGE 3: Run second command --- | |
| send_user "\n🚀 Starting second command...\n" | |
| # Second command: add commit hash to CLAUDE.md | |
| set second_command "/Users/bsonntag/.local/bin/claude" | |
| spawn $second_command --permission-mode bypassPermissions "add the commit hash to the CLAUDE.md file" | |
| send_user "🚀 Starting '$second_command --permission-mode bypassPermissions \"add the commit hash to the CLAUDE.md file\"'.\n" | |
| # Wait for the same phrase in the second command | |
| send_user "Stage 3: Waiting for '$watch_phrase' to appear in second command (max $initial_timeout seconds)...\n" | |
| set timeout $initial_timeout | |
| expect { | |
| timeout { | |
| send_user "\n❌ ERROR: Timed out on second command. The phrase '$watch_phrase' never appeared.\n" | |
| exit 1 | |
| } | |
| -gl "*$watch_phrase*" { | |
| send_user "✅ Phrase found in second command. Proceeding to Stage 4.\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Second Claude process exited before monitoring could start.\n" | |
| exit 0 | |
| } | |
| } | |
| # --- STAGE 4: Wait for proceed prompt and loop --- | |
| send_user "Stage 4: Waiting for '$proceed_prompt' prompt in second command...\n" | |
| set timeout 30 | |
| expect { | |
| -gl "*$proceed_prompt*" { | |
| send_user "🔵 Found '$proceed_prompt'.\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Second Claude process finished.\n" | |
| exit 0 | |
| } | |
| timeout { | |
| send_user "\n⚠️ WARNING: '$proceed_prompt' never appeared in second command.\n" | |
| exit 0 | |
| } | |
| } | |
| # Now loop: send enter, wait for esc, wait for esc to disappear | |
| send_user "Stage 4: Starting monitoring loop for second command...\n" | |
| set timeout $monitor_timeout | |
| while {1} { | |
| # Wait a moment and send enter | |
| send_user "🔵 Sending enter...\n" | |
| send "\r" | |
| # Wait for esc to appear | |
| set timeout 30 | |
| send_user " ⏳ Waiting for '$watch_phrase' to appear...\n" | |
| expect { | |
| -gl "*$watch_phrase*" { | |
| send_user " ✅ '$watch_phrase' appeared.\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Second Claude process finished.\n" | |
| exit 0 | |
| } | |
| timeout { | |
| send_user "\n⚠️ WARNING: '$watch_phrase' never reappeared. Breaking loop.\n" | |
| break | |
| } | |
| } | |
| # Wait for esc to disappear | |
| set timeout $monitor_timeout | |
| send_user " ⏳ Waiting for '$watch_phrase' to disappear...\n" | |
| expect { | |
| -gl "*$watch_phrase*" { | |
| exp_continue | |
| } | |
| timeout { | |
| send_user " ✅ '$watch_phrase' disappeared. Looping...\n" | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Second Claude process finished.\n" | |
| exit 0 | |
| } | |
| } | |
| # Check if we should break (if esc never reappeared, meaning task is done) | |
| set timeout 5 | |
| expect { | |
| -gl "*$proceed_prompt*" { | |
| # Good, prompt is still there, continue loop | |
| } | |
| timeout { | |
| # No prompt visible, task might be done | |
| send_user "\n✅ Task appears complete. Sending Ctrl+C twice.\n" | |
| send "\x03" | |
| send "\x03" | |
| exit 0 | |
| } | |
| eof { | |
| send_user "\nℹ️ INFO: Second Claude process finished.\n" | |
| exit 0 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment