Fix Auto-GPT looping forever (#87)

This commit is contained in:
merwanehamadi
2023-07-11 17:02:29 -07:00
committed by GitHub
parent 4ecb70c5e3
commit b3c506cd94
2 changed files with 31 additions and 21 deletions

View File

@@ -42,35 +42,45 @@ def run_agent(
)
start_time = time.time()
timeout = config["cutoff"]
print(
f"Running Python function '{config['entry_path']}' with timeout {config['cutoff']}"
)
command = [sys.executable, "-m", config["entry_path"], str(task)]
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
start_time = time.time()
while True:
if process.stdout is None:
continue
while output := process.stdout.readline():
output = ""
if process.stdout is not None:
output = process.stdout.readline()
print(output.strip())
# Check if process has ended
if process.poll() is not None:
print("The Python function has finished running.")
# Check if process has ended, has no more output, or exceeded timeout
if (
process.poll() is not None
or output == ""
or (time.time() - start_time > config["cutoff"])
):
break
# Check if process has exceeded timeout
if time.time() - start_time > timeout:
print(
"The Python function has exceeded the time limit and was terminated."
)
# Terminate the process group
process.terminate()
break
if time.time() - start_time > config["cutoff"]:
print("The Python function has exceeded the time limit and was terminated.")
process.kill()
else:
print("The Python function has finished running.")
# Optional: sleep for a while
time.sleep(0.1)
# Wait for process to terminate, then get return code
process.wait()
if process.returncode != 0:
print(f"The agent timed out")
def copy_artifacts_into_workspace(
workspace: str, artifact_folder_name: str, challenge_dir_path: str