regenerate.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/bin/bash
  2. set -eo pipefail
  3. ##############################################################
  4. ## CONSTANTS AND ENVIRONMENTAL VARIABLES ##
  5. ##############################################################
  6. if [ -z $WORKSPACE_MOUNT_PATH ]; then
  7. WORKSPACE_MOUNT_PATH=$(pwd)
  8. fi
  9. if [ -z $WORKSPACE_BASE ]; then
  10. WORKSPACE_BASE=$(pwd)
  11. fi
  12. WORKSPACE_MOUNT_PATH+="/_test_workspace"
  13. WORKSPACE_BASE+="/_test_workspace"
  14. WORKSPACE_MOUNT_PATH_IN_SANDBOX="/workspace"
  15. SANDBOX_TYPE="ssh"
  16. MAX_ITERATIONS=10
  17. agents=("MonologueAgent" "CodeActAgent" "PlannerAgent" "SWEAgent")
  18. tasks=(
  19. "Fix typos in bad.txt."
  20. "Write a shell script 'hello.sh' that prints 'hello'."
  21. "Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'."
  22. )
  23. test_names=(
  24. "test_edits"
  25. "test_write_simple_script"
  26. "test_ipython"
  27. )
  28. num_of_tests=${#test_names[@]}
  29. num_of_agents=${#agents[@]}
  30. ##############################################################
  31. ## FUNCTIONS ##
  32. ##############################################################
  33. # run integration test against a specific agent & test
  34. run_test() {
  35. SANDBOX_TYPE=$SANDBOX_TYPE \
  36. WORKSPACE_BASE=$WORKSPACE_BASE \
  37. WORKSPACE_MOUNT_PATH=$WORKSPACE_MOUNT_PATH \
  38. WORKSPACE_MOUNT_PATH_IN_SANDBOX=$WORKSPACE_MOUNT_PATH_IN_SANDBOX \
  39. MAX_ITERATIONS=$MAX_ITERATIONS \
  40. AGENT=$agent \
  41. poetry run pytest -s ./tests/integration/test_agent.py::$test_name
  42. # return exit code of pytest
  43. return $?
  44. }
  45. # generate prompts again, using existing LLM responses under tests/integration/mock/[agent]/[test_name]/response_*.log
  46. # this is a compromise; the prompts might be non-sense yet still pass the test, because we don't use a real LLM to
  47. # respond to the prompts. The benefit is developers don't have to regenerate real responses from LLM, if they only
  48. # apply a small change to prompts.
  49. regenerate_without_llm() {
  50. # set -x to print the command being executed
  51. set -x
  52. SANDBOX_TYPE=$SANDBOX_TYPE \
  53. WORKSPACE_BASE=$WORKSPACE_BASE \
  54. WORKSPACE_MOUNT_PATH=$WORKSPACE_MOUNT_PATH \
  55. WORKSPACE_MOUNT_PATH_IN_SANDBOX=$WORKSPACE_MOUNT_PATH_IN_SANDBOX \
  56. MAX_ITERATIONS=$MAX_ITERATIONS \
  57. FORCE_APPLY_PROMPTS=true \
  58. AGENT=$agent \
  59. poetry run pytest -s ./tests/integration/test_agent.py::$test_name
  60. set +x
  61. }
  62. regenerate_with_llm() {
  63. rm -rf $WORKSPACE_BASE
  64. mkdir -p $WORKSPACE_BASE
  65. if [ -d "tests/integration/workspace/$test_name" ]; then
  66. cp -r tests/integration/workspace/$test_name/* $WORKSPACE_BASE
  67. fi
  68. rm -rf logs
  69. rm -rf tests/integration/mock/$agent/$test_name/*
  70. # set -x to print the command being executed
  71. set -x
  72. echo -e "/exit\n" | \
  73. DEBUG=true \
  74. SANDBOX_TYPE=$SANDBOX_TYPE \
  75. WORKSPACE_BASE=$WORKSPACE_BASE \
  76. WORKSPACE_MOUNT_PATH=$WORKSPACE_MOUNT_PATH AGENT=$agent \
  77. WORKSPACE_MOUNT_PATH_IN_SANDBOX=$WORKSPACE_MOUNT_PATH_IN_SANDBOX \
  78. poetry run python ./opendevin/core/main.py \
  79. -i $MAX_ITERATIONS \
  80. -t "$task Do not ask me for confirmation at any point." \
  81. -c $agent
  82. set +x
  83. mkdir -p tests/integration/mock/$agent/$test_name/
  84. mv logs/llm/**/* tests/integration/mock/$agent/$test_name/
  85. }
  86. ##############################################################
  87. ## MAIN PROGRAM ##
  88. ##############################################################
  89. if [ "$num_of_tests" -ne "${#test_names[@]}" ]; then
  90. echo "Every task must correspond to one test case"
  91. exit 1
  92. fi
  93. rm -rf logs
  94. rm -rf $WORKSPACE_BASE
  95. for ((i = 0; i < num_of_tests; i++)); do
  96. task=${tasks[i]}
  97. test_name=${test_names[i]}
  98. # skip other tests if only one test is specified
  99. if [[ -n "$ONLY_TEST_NAME" && "$ONLY_TEST_NAME" != "$test_name" ]]; then
  100. continue
  101. fi
  102. for ((j = 0; j < num_of_agents; j++)); do
  103. agent=${agents[j]}
  104. # skip other agents if only one agent is specified
  105. if [[ -n "$ONLY_TEST_AGENT" && "$ONLY_TEST_AGENT" != "$agent" ]]; then
  106. continue
  107. fi
  108. echo -e "\n\n\n\n========STEP 1: Running $test_name for $agent========\n\n\n\n"
  109. rm -rf $WORKSPACE_BASE
  110. mkdir $WORKSPACE_BASE
  111. if [ -d "tests/integration/workspace/$test_name" ]; then
  112. cp -r tests/integration/workspace/$test_name/* $WORKSPACE_BASE
  113. fi
  114. if [ "$TEST_ONLY" = true ]; then
  115. set -e
  116. else
  117. # Temporarily disable 'exit on error'
  118. set +e
  119. fi
  120. run_test
  121. TEST_STATUS=$?
  122. # Re-enable 'exit on error'
  123. set -e
  124. if [[ $TEST_STATUS -ne 0 ]]; then
  125. if [ "$FORCE_USE_LLM" = true ]; then
  126. echo -e "\n\n\n\n========FORCE_USE_LLM, skipping step 2 & 3========\n\n\n\n"
  127. elif [ ! -d "tests/integration/mock/$agent/$test_name" ]; then
  128. echo -e "\n\n\n\n========No existing mock responses for $agent/$test_name, skipping step 2 & 3========\n\n\n\n"
  129. else
  130. echo -e "\n\n\n\n========STEP 2: $test_name failed, regenerating prompts for $agent WITHOUT money cost========\n\n\n\n"
  131. regenerate_without_llm
  132. echo -e "\n\n\n\n========STEP 3: $test_name prompts regenerated for $agent, rerun test again to verify========\n\n\n\n"
  133. # Temporarily disable 'exit on error'
  134. set +e
  135. run_test
  136. TEST_STATUS=$?
  137. # Re-enable 'exit on error'
  138. set -e
  139. fi
  140. if [[ $TEST_STATUS -ne 0 ]]; then
  141. echo -e "\n\n\n\n========STEP 4: $test_name failed, regenerating prompts and responses for $agent WITH money cost========\n\n\n\n"
  142. regenerate_with_llm
  143. 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"
  144. # Temporarily disable 'exit on error'
  145. set +e
  146. run_test
  147. TEST_STATUS=$?
  148. # Re-enable 'exit on error'
  149. set -e
  150. if [[ $TEST_STATUS -ne 0 ]]; then
  151. echo -e "\n\n\n\n========$test_name for $agent RERUN FAILED========\n\n\n\n"
  152. echo -e "There are multiple possibilities:"
  153. echo -e " 1. The agent is unable to finish the task within $MAX_ITERATIONS steps."
  154. echo -e " 2. The agent thinks itself has finished the task, but fails the validation in the test code."
  155. echo -e " 3. There is something non-deterministic in the prompt."
  156. echo -e " 4. There is a bug in this script, or in OpenDevin code."
  157. echo -e "NOTE: Some of the above problems could sometimes be fixed by a retry (with a more powerful LLM)."
  158. echo -e " You could also consider improving the agent, increasing MAX_ITERATIONS, or skipping this test for this agent."
  159. exit 1
  160. else
  161. echo -e "\n\n\n\n========$test_name for $agent RERUN PASSED========\n\n\n\n"
  162. sleep 1
  163. fi
  164. else
  165. echo -e "\n\n\n\n========$test_name for $agent RERUN PASSED========\n\n\n\n"
  166. sleep 1
  167. fi
  168. else
  169. echo -e "\n\n\n\n========$test_name for $agent PASSED========\n\n\n\n"
  170. sleep 1
  171. fi
  172. done
  173. done
  174. rm -rf logs
  175. rm -rf $WORKSPACE_BASE
  176. echo "Done!"