regenerate.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #!/bin/bash
  2. set -eo pipefail
  3. ##############################################################
  4. ## CONSTANTS AND ENVIRONMENTAL VARIABLES ##
  5. ##############################################################
  6. # unset environmental variables that might disturb testing
  7. unset OPENAI_API_KEY
  8. unset SANDBOX_ENV_OPENAI_API_KEY
  9. unset OPENAI_BASE_URL
  10. unset OPENAI_MODEL
  11. # Get the absolute path of the script directory
  12. get_script_dir() {
  13. local source="${BASH_SOURCE[0]}"
  14. while [ -h "$source" ]; do
  15. local dir="$( cd -P "$( dirname "$source" )" && pwd )"
  16. source="$(readlink "$source")"
  17. [[ $source != /* ]] && source="$dir/$source"
  18. done
  19. echo "$( cd -P "$( dirname "$source" )" && pwd )"
  20. }
  21. TMP_FILE="${TMP_FILE:-tmp.log}"
  22. if [ -z $WORKSPACE_MOUNT_PATH ]; then
  23. WORKSPACE_MOUNT_PATH=$(pwd)
  24. fi
  25. if [ -z $WORKSPACE_BASE ]; then
  26. WORKSPACE_BASE=$(pwd)
  27. fi
  28. export SCRIPT_DIR=$(get_script_dir)
  29. export PROJECT_ROOT=$(realpath "$SCRIPT_DIR/../..")
  30. WORKSPACE_MOUNT_PATH=$(realpath "${WORKSPACE_MOUNT_PATH}")/_test_workspace
  31. WORKSPACE_BASE=$(realpath "${WORKSPACE_BASE}")/_test_workspace
  32. WORKSPACE_MOUNT_PATH_IN_SANDBOX="/workspace"
  33. echo "Current working directory: $(pwd)"
  34. echo "SCRIPT_DIR: $SCRIPT_DIR"
  35. echo "PROJECT_ROOT: $PROJECT_ROOT"
  36. echo "WORKSPACE_BASE: $WORKSPACE_BASE"
  37. echo "WORKSPACE_MOUNT_PATH: $WORKSPACE_MOUNT_PATH"
  38. echo "WORKSPACE_MOUNT_PATH_IN_SANDBOX: $WORKSPACE_MOUNT_PATH_IN_SANDBOX"
  39. # Ensure we're in the correct directory
  40. cd "$PROJECT_ROOT" || exit 1
  41. mkdir -p $WORKSPACE_BASE
  42. # use environmental variable if exists, otherwise use "ssh"
  43. SANDBOX_BOX_TYPE="${SANDBOX_TYPE:-ssh}"
  44. # TODO: we should also test PERSIST_SANDBOX = true, once it's fixed
  45. PERSIST_SANDBOX=false
  46. MAX_ITERATIONS=15
  47. agents=(
  48. "DelegatorAgent"
  49. "ManagerAgent"
  50. "BrowsingAgent"
  51. "CodeActAgent"
  52. "PlannerAgent"
  53. "CodeActSWEAgent"
  54. )
  55. tasks=(
  56. "Fix typos in bad.txt."
  57. "Write a shell script 'hello.sh' that prints 'hello'."
  58. "Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'."
  59. "Write a git commit message for the current staging area."
  60. "Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt."
  61. "Browse localhost:8000, and tell me the ultimate answer to life."
  62. )
  63. test_names=(
  64. "test_edits"
  65. "test_write_simple_script"
  66. "test_ipython"
  67. "test_simple_task_rejection"
  68. "test_ipython_module"
  69. "test_browse_internet"
  70. )
  71. num_of_tests=${#test_names[@]}
  72. num_of_agents=${#agents[@]}
  73. ##############################################################
  74. ## FUNCTIONS ##
  75. ##############################################################
  76. # run integration test against a specific agent & test
  77. run_test() {
  78. # Ensure we're in the correct directory
  79. cd "$PROJECT_ROOT" || exit 1
  80. local pytest_cmd="poetry run pytest --cache-clear -s $SCRIPT_DIR/test_agent.py::$test_name"
  81. # Check if TEST_IN_CI is defined
  82. if [ -n "$TEST_IN_CI" ]; then
  83. pytest_cmd+=" --cov=agenthub --cov=opendevin --cov-report=xml --cov-append"
  84. fi
  85. env SCRIPT_DIR="$SCRIPT_DIR" \
  86. PROJECT_ROOT="$PROJECT_ROOT" \
  87. SANDBOX_BOX_TYPE="$SANDBOX_BOX_TYPE" \
  88. PERSIST_SANDBOX=$PERSIST_SANDBOX \
  89. WORKSPACE_BASE=$WORKSPACE_BASE \
  90. WORKSPACE_MOUNT_PATH=$WORKSPACE_MOUNT_PATH \
  91. WORKSPACE_MOUNT_PATH_IN_SANDBOX=$WORKSPACE_MOUNT_PATH_IN_SANDBOX \
  92. MAX_ITERATIONS=$MAX_ITERATIONS \
  93. DEFAULT_AGENT=$agent \
  94. $pytest_cmd 2>&1 | tee $TMP_FILE
  95. # Capture the exit code of pytest
  96. pytest_exit_code=${PIPESTATUS[0]}
  97. if grep -q "docker.errors.DockerException" $TMP_FILE; then
  98. echo "Error: docker.errors.DockerException found in the output. Exiting."
  99. echo "Please check if your Docker daemon is running!"
  100. exit 1
  101. fi
  102. if grep -q "tenacity.RetryError" $TMP_FILE; then
  103. echo "Error: tenacity.RetryError found in the output. Exiting."
  104. echo "This is mostly a transient error. Please retry."
  105. exit 1
  106. fi
  107. if grep -q "ExceptionPxssh" $TMP_FILE; then
  108. echo "Error: ExceptionPxssh found in the output. Exiting."
  109. echo "Could not connect to sandbox via ssh. Please stop any stale docker container and retry."
  110. exit 1
  111. fi
  112. if grep -q "Address already in use" $TMP_FILE; then
  113. echo "Error: Address already in use found in the output. Exiting."
  114. echo "Browsing tests need a local http server. Please check if there's any zombie process running start_http_server.py."
  115. exit 1
  116. fi
  117. # Return the exit code of pytest
  118. return $pytest_exit_code
  119. }
  120. # browsing capability needs a local http server
  121. launch_http_server() {
  122. poetry run python $SCRIPT_DIR/start_http_server.py &
  123. HTTP_SERVER_PID=$!
  124. echo "Test http server launched, PID = $HTTP_SERVER_PID"
  125. sleep 10
  126. }
  127. cleanup() {
  128. echo "Cleaning up before exit..."
  129. if [ -n "$HTTP_SERVER_PID" ]; then
  130. echo "Killing HTTP server..."
  131. kill $HTTP_SERVER_PID
  132. unset HTTP_SERVER_PID
  133. fi
  134. [ -f $TMP_FILE ] && rm $TMP_FILE
  135. echo "Cleanup done!"
  136. }
  137. # Trap the EXIT signal to run the cleanup function
  138. trap cleanup EXIT
  139. # generate prompts again, using existing LLM responses under tests/integration/mock/[agent]/[test_name]/response_*.log
  140. # this is a compromise; the prompts might be non-sense yet still pass the test, because we don't use a real LLM to
  141. # respond to the prompts. The benefit is developers don't have to regenerate real responses from LLM, if they only
  142. # apply a small change to prompts.
  143. regenerate_without_llm() {
  144. # set -x to print the command being executed
  145. set -x
  146. env SCRIPT_DIR="$SCRIPT_DIR" \
  147. PROJECT_ROOT="$PROJECT_ROOT" \
  148. SANDBOX_BOX_TYPE="$SANDBOX_BOX_TYPE" \
  149. PERSIST_SANDBOX=$PERSIST_SANDBOX \
  150. WORKSPACE_BASE=$WORKSPACE_BASE \
  151. WORKSPACE_MOUNT_PATH=$WORKSPACE_MOUNT_PATH \
  152. WORKSPACE_MOUNT_PATH_IN_SANDBOX=$WORKSPACE_MOUNT_PATH_IN_SANDBOX \
  153. MAX_ITERATIONS=$MAX_ITERATIONS \
  154. FORCE_APPLY_PROMPTS=true \
  155. DEFAULT_AGENT=$agent \
  156. poetry run pytest -s $SCRIPT_DIR/test_agent.py::$test_name
  157. set +x
  158. }
  159. regenerate_with_llm() {
  160. if [[ "$test_name" = "test_browse_internet" ]]; then
  161. launch_http_server
  162. fi
  163. rm -rf $WORKSPACE_BASE/*
  164. if [ -d "$SCRIPT_DIR/workspace/$test_name" ]; then
  165. cp -r "$SCRIPT_DIR/workspace/$test_name"/* $WORKSPACE_BASE
  166. fi
  167. rm -rf logs
  168. rm -rf "$SCRIPT_DIR/mock/$agent/$test_name/*"
  169. # set -x to print the command being executed
  170. set -x
  171. echo -e "/exit\n" | \
  172. env SCRIPT_DIR="$SCRIPT_DIR" \
  173. PROJECT_ROOT="$PROJECT_ROOT" \
  174. DEBUG=true \
  175. SANDBOX_BOX_TYPE="$SANDBOX_BOX_TYPE" \
  176. PERSIST_SANDBOX=$PERSIST_SANDBOX \
  177. WORKSPACE_BASE=$WORKSPACE_BASE \
  178. WORKSPACE_MOUNT_PATH=$WORKSPACE_MOUNT_PATH \
  179. AGENT=$agent \
  180. WORKSPACE_MOUNT_PATH_IN_SANDBOX=$WORKSPACE_MOUNT_PATH_IN_SANDBOX \
  181. poetry run python "$PROJECT_ROOT/opendevin/core/main.py" \
  182. -i $MAX_ITERATIONS \
  183. -t "$task Do not ask me for confirmation at any point." \
  184. -c $agent
  185. set +x
  186. mkdir -p "$SCRIPT_DIR/mock/$agent/$test_name/"
  187. mv logs/llm/**/* "$SCRIPT_DIR/mock/$agent/$test_name/"
  188. }
  189. ##############################################################
  190. ## MAIN PROGRAM ##
  191. ##############################################################
  192. if [ "$num_of_tests" -ne "${#test_names[@]}" ]; then
  193. echo "Every task must correspond to one test case"
  194. exit 1
  195. fi
  196. rm -rf logs
  197. rm -rf $WORKSPACE_BASE/*
  198. for ((i = 0; i < num_of_tests; i++)); do
  199. task=${tasks[i]}
  200. test_name=${test_names[i]}
  201. # skip other tests if only one test is specified
  202. if [[ -n "$ONLY_TEST_NAME" && "$ONLY_TEST_NAME" != "$test_name" ]]; then
  203. continue
  204. fi
  205. for ((j = 0; j < num_of_agents; j++)); do
  206. agent=${agents[j]}
  207. # skip other agents if only one agent is specified
  208. if [[ -n "$ONLY_TEST_AGENT" && "$ONLY_TEST_AGENT" != "$agent" ]]; then
  209. continue
  210. fi
  211. echo -e "\n\n\n\n========STEP 1: Running $test_name for $agent========\n\n\n\n"
  212. rm -rf $WORKSPACE_BASE/*
  213. if [ -d "$SCRIPT_DIR/workspace/$test_name" ]; then
  214. cp -r "$SCRIPT_DIR/workspace/$test_name"/* $WORKSPACE_BASE
  215. fi
  216. if [ "$TEST_ONLY" = true ]; then
  217. set -e
  218. else
  219. # Temporarily disable 'exit on error'
  220. set +e
  221. fi
  222. TEST_STATUS=1
  223. if [ -z $FORCE_REGENERATE ]; then
  224. run_test
  225. TEST_STATUS=$?
  226. fi
  227. # Re-enable 'exit on error'
  228. set -e
  229. if [[ $TEST_STATUS -ne 0 ]]; then
  230. if [ "$FORCE_USE_LLM" = true ]; then
  231. echo -e "\n\n\n\n========FORCE_USE_LLM, skipping step 2 & 3========\n\n\n\n"
  232. elif [ ! -d "$SCRIPT_DIR/mock/$agent/$test_name" ]; then
  233. echo -e "\n\n\n\n========No existing mock responses for $agent/$test_name, skipping step 2 & 3========\n\n\n\n"
  234. else
  235. echo -e "\n\n\n\n========STEP 2: $test_name failed, regenerating prompts for $agent WITHOUT money cost========\n\n\n\n"
  236. # Temporarily disable 'exit on error'
  237. set +e
  238. regenerate_without_llm
  239. echo -e "\n\n\n\n========STEP 3: $test_name prompts regenerated for $agent, rerun test again to verify========\n\n\n\n"
  240. run_test
  241. TEST_STATUS=$?
  242. # Re-enable 'exit on error'
  243. set -e
  244. fi
  245. if [[ $TEST_STATUS -ne 0 ]]; then
  246. echo -e "\n\n\n\n========STEP 4: $test_name failed, regenerating prompts and responses for $agent WITH money cost========\n\n\n\n"
  247. regenerate_with_llm
  248. echo -e "\n\n\n\n========STEP 5: $test_name prompts and responses regenerated for $agent, rerun test again to verify========\n\n\n\n"
  249. # Temporarily disable 'exit on error'
  250. set +e
  251. run_test
  252. TEST_STATUS=$?
  253. # Re-enable 'exit on error'
  254. set -e
  255. if [[ $TEST_STATUS -ne 0 ]]; then
  256. echo -e "\n\n\n\n========$test_name for $agent RERUN FAILED========\n\n\n\n"
  257. echo -e "There are multiple possibilities:"
  258. echo -e " 1. The agent is unable to finish the task within $MAX_ITERATIONS steps."
  259. echo -e " 2. The agent thinks itself has finished the task, but fails the validation in the test code."
  260. echo -e " 3. There is something non-deterministic in the prompt."
  261. echo -e " 4. There is a bug in this script, or in OpenDevin code."
  262. echo -e "NOTE: Some of the above problems could sometimes be fixed by a retry (with a more powerful LLM)."
  263. echo -e " You could also consider improving the agent, increasing MAX_ITERATIONS, or skipping this test for this agent."
  264. exit 1
  265. else
  266. echo -e "\n\n\n\n========$test_name for $agent RERUN PASSED========\n\n\n\n"
  267. sleep 1
  268. fi
  269. else
  270. echo -e "\n\n\n\n========$test_name for $agent RERUN PASSED========\n\n\n\n"
  271. sleep 1
  272. fi
  273. else
  274. echo -e "\n\n\n\n========$test_name for $agent PASSED========\n\n\n\n"
  275. sleep 1
  276. fi
  277. done
  278. done
  279. rm -rf logs
  280. rm -rf $WORKSPACE_BASE
  281. echo "Done!"