Cursor agent mode - when running terminal commands often hangs up the terminal, requiring a click to pop it out in order to continue commands

:counterclockwise_arrows_button: CORRECTION: Updated Solution for Cursor Agent vs IDE Terminal Detection

Update: My original solution using VSCODE_SHELL_INTEGRATION was incorrect. After deeper testing, I discovered the actual environment differences. Here’s the working solution:

:cross_mark: Why My Original Solution Was Wrong

I originally claimed that VSCODE_SHELL_INTEGRATION differentiates between terminals:

  • :cross_mark: Agent Terminal: VSCODE_SHELL_INTEGRATION='1'
  • :cross_mark: IDE Built-in Terminal: VSCODE_SHELL_INTEGRATION=''

This was incorrect. Both terminals actually have VSCODE_SHELL_INTEGRATION='' (empty).

:white_check_mark: The Real Differentiator: npm_config_yes

After extensive environment variable comparison, the actual differentiator is:

Environment npm_config_yes
Agent Terminal ‘true’
IDE Built-in Terminal (absent)

:hammer_and_wrench: Corrected .zshrc Configuration

Replace your detection logic with this tested and working version:

# Agent detection - only activate minimal mode for actual agents  
if [[ -n "$npm_config_yes" ]] || [[ -n "$CI" ]] || [[ "$-" != *i* ]]; then
  export AGENT_MODE=true
else
  export AGENT_MODE=false
fi

if [[ "$AGENT_MODE" == "true" ]]; then
  POWERLEVEL9K_INSTANT_PROMPT=off
  # Disable complex prompt features for AI agents
  POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir vcs)
  POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=()
  # Ensure non-interactive mode
  export DEBIAN_FRONTEND=noninteractive
  export NONINTERACTIVE=1
fi

# Enable Powerlevel10k instant prompt only when not in agent mode
if [[ "$AGENT_MODE" != "true" ]] && [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Set Oh My Zsh theme conditionally - disable for agents only
if [[ "$AGENT_MODE" == "true" ]]; then
  ZSH_THEME=""  # Disable Powerlevel10k for agents
else
  ZSH_THEME="powerlevel10k/powerlevel10k"
fi

# Later in your .zshrc - minimal prompt for agents
if [[ "$AGENT_MODE" == "true" ]]; then
  PROMPT='%n@%m:%~%# '
  RPROMPT=''
  unsetopt CORRECT
  unsetopt CORRECT_ALL
  setopt NO_BEEP
  setopt NO_HIST_BEEP  
  setopt NO_LIST_BEEP
  
  # Agent-friendly aliases to avoid interactive prompts
  alias rm='rm -f'
  alias cp='cp -f' 
  alias mv='mv -f'
  alias npm='npm --no-fund --no-audit'
  alias yarn='yarn --non-interactive'
  alias pip='pip --quiet'
  alias git='git -c advice.detachedHead=false'
else
  [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
fi

:test_tube: How I Discovered This

The agent terminal sets several automation-friendly variables:

npm_config_yes=true                    # Auto-yes for npm prompts
DISABLE_AUTO_UPDATE=true               # Disable package updates  
PIP_NO_INPUT=true                      # No pip interactive prompts
COMPOSER_NO_INTERACTION=1              # No composer prompts
PAGER=head -n 10000 | cat             # Limited output pager

The npm_config_yes=true is the most reliable discriminator as it’s specifically set to enable non-interactive npm operations in agent contexts.

:white_check_mark: Verified Results

  • :white_check_mark: Agent terminals: No hangs, minimal prompt, fast startup
  • :white_check_mark: IDE built-in terminal: Full Powerlevel10k theme and functionality
  • :white_check_mark: Clean separation: Only agents get the minimal config
  • :white_check_mark: Tested extensively: Works reliably across shell restarts

:magnifying_glass_tilted_left: Debugging Your Setup

To verify this works in your environment:

Agent terminal (should show):

❯ echo "AGENT_MODE: $AGENT_MODE, npm_config_yes: $npm_config_yes"
AGENT_MODE: true, npm_config_yes: true

IDE terminal (should show):

❯ echo "AGENT_MODE: $AGENT_MODE, npm_config_yes: $npm_config_yes"  
AGENT_MODE: false, npm_config_yes:

This corrected solution provides the perfect balance - preventing agent hangs while preserving your beautiful terminal experience in the IDE! :rocket:


Sorry for the confusion in my original post - this updated solution is thoroughly tested and working!

11 Likes