Repository navigation
Expand file tree
/
Copy pathrun_task.py
More file actions
63 lines (58 loc) · 2.31 KB
/
Copy pathrun_task.py
File metadata and controls
63 lines (58 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import os
import time
from pathlib import Path
from lib import CURSOR_DIR, PROJECT_DIR, CursorApiError, cursor_api
repo_url = os.environ.get("TASK_REPO_URL")
if not repo_url:
raise SystemExit("Set TASK_REPO_URL first.")
pool = os.environ.get("CURSOR_POOL", "render-sandboxes")
terminal = {"FINISHED", "ERROR", "CANCELLED", "EXPIRED"}
# Map the shared task paths to the repository Cursor will clone.
prompt = (CURSOR_DIR / "task-note.txt").read_text() + (PROJECT_DIR / "prompt.txt").read_text()
created = cursor_api("POST", "/v1/agents", {
"prompt": {"text": prompt},
"env": {"type": "pool", "name": pool},
"repos": [{"url": repo_url, "startingRef": os.environ.get("TASK_REPO_REF", "main")}],
})
agent_id = created["agent"]["id"]
run_id = (created.get("run") or {}).get("id") or created["agent"]["latestRunId"]
# Save IDs before polling so cleanup can still run after an error or timeout.
Path("agent.json").write_text(json.dumps({"agentId": agent_id, "runId": run_id}, indent=2) + "\n")
print(f"Agent {agent_id} created. Follow it at {created['agent'].get('url')}")
# Polling observes the remote run; reaching this deadline does not cancel it.
deadline = time.monotonic() + 30 * 60
last_status = ""
failures = 0
while True:
try:
run = cursor_api("GET", f"/v1/agents/{agent_id}/runs/{run_id}")
failures = 0
except (CursorApiError, OSError) as error:
if isinstance(error, CursorApiError) and error.status < 500:
raise
failures += 1
if failures >= 5:
raise
time.sleep(10)
continue
if run["status"] != last_status:
print(f"Run status: {run['status']}")
last_status = run["status"]
if run["status"] in terminal:
break
if time.monotonic() > deadline:
raise SystemExit("The run is still active after 30 minutes.")
time.sleep(10)
branch = next(
(entry["branch"] for entry in (run.get("git") or {}).get("branches", []) if entry.get("branch")),
None,
)
Path("run.json").write_text(json.dumps({**run, "branch": branch}, indent=2) + "\n")
if run.get("result"):
print(f"\n{run['result']}\n")
if run["status"] != "FINISHED":
raise SystemExit(f"The run ended with {run['status']}.")
if not branch:
raise SystemExit("The run finished without pushing a branch.")
print(f"Pushed branch: {branch}")