regenerate.sh 11 KB

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