Skip to content

An open-source, faster Devin alternative

TypeScript

fix(deploy): use async exec for builder probe

Igor Zalutski87f6410ae4d0
diff --git a/ci/flue-builder-substrate-probe.py b/ci/flue-builder-substrate-probe.py
index 86cbe5cb..4f6376b9 100755
--- a/ci/flue-builder-substrate-probe.py
+++ b/ci/flue-builder-substrate-probe.py
@@ -11,6 +11,7 @@ import os
from pathlib import Path
import secrets
import sys
+import time
from typing import Any
from urllib import error, parse, request
import uuid
@@ -184,16 +185,37 @@ class LiveProbe:
if envs is not None:
body["envs"] = envs
quoted = parse.quote(sandbox_id, safe="")
- _, response = self.call(
+ _, handle = self.call(
"POST",
- f"/sandboxes/{quoted}/exec/run",
+ f"/sandboxes/{quoted}/exec/run-async",
body,
- allowed_statuses={200},
- timeout=timeout + 20,
+ allowed_statuses={202},
+ timeout=60,
)
- if not isinstance(response, dict) or not isinstance(response.get("exitCode"), int):
- raise ProbeError("exec response is missing exitCode")
- return response
+ if not isinstance(handle, dict) or not isinstance(handle.get("execId"), str):
+ raise ProbeError("async exec response is missing execId")
+
+ exec_id = parse.quote(handle["execId"], safe="")
+ deadline = time.monotonic() + timeout + 60
+ delay = 0.2
+ while time.monotonic() < deadline:
+ _, response = self.call(
+ "GET",
+ f"/sandboxes/{quoted}/exec/{exec_id}/result",
+ allowed_statuses={200},
+ timeout=30,
+ )
+ if not isinstance(response, dict) or not isinstance(response.get("running"), bool):
+ raise ProbeError("async exec result is invalid")
+ if not response["running"]:
+ if response.get("error"):
+ raise ProbeError("async exec failed before returning a process result")
+ if not isinstance(response.get("exitCode"), int):
+ raise ProbeError("async exec result is missing exitCode")
+ return response
+ time.sleep(delay)
+ delay = min(delay * 2, 2.0)
+ raise ProbeError("async exec result exceeded the bounded client wait")
def read_guest_file(self, sandbox_id: str, path: str, max_bytes: int = 512 * 1024) -> bytes:
quoted_id = parse.quote(sandbox_id, safe="")
diff --git a/ci/test_flue_builder_substrate_probe.py b/ci/test_flue_builder_substrate_probe.py
index 62dfc434..36ec8ac2 100644
--- a/ci/test_flue_builder_substrate_probe.py
+++ b/ci/test_flue_builder_substrate_probe.py
@@ -117,6 +117,45 @@ class SandboxProbeTest(unittest.TestCase):
live.create_sandbox()
self.assertEqual(live.sandboxes, ["sb-test"])
+ def test_exec_uses_the_public_async_result_contract(self) -> None:
+ live = PROBE.LiveProbe.__new__(PROBE.LiveProbe)
+ calls: list[tuple[str, str, object]] = []
+ responses = iter(
+ [
+ (202, {"execId": "exec-test", "running": True}),
+ (200, {"running": True}),
+ (200, {"running": False, "exitCode": 0, "stdout": "ok", "stderr": ""}),
+ ]
+ )
+
+ def fake_call(
+ method: str,
+ path: str,
+ body: object = None,
+ allowed_statuses: set[int] | None = None,
+ timeout: float = 60,
+ ) -> tuple[int, object]:
+ calls.append((method, path, body))
+ return next(responses)
+
+ live.call = fake_call
+ with mock.patch.object(PROBE.time, "sleep"):
+ result = live.exec("sb-test", "/bin/echo", ["ok"], {"CI": "1"}, timeout=5)
+
+ self.assertEqual(result["stdout"], "ok")
+ self.assertEqual(calls[0], (
+ "POST",
+ "/sandboxes/sb-test/exec/run-async",
+ {"cmd": "/bin/echo", "args": ["ok"], "timeout": 5, "envs": {"CI": "1"}},
+ ))
+ self.assertEqual(
+ [call[:2] for call in calls[1:]],
+ [
+ ("GET", "/sandboxes/sb-test/exec/exec-test/result"),
+ ("GET", "/sandboxes/sb-test/exec/exec-test/result"),
+ ],
+ )
+
def test_destroy_attempts_every_created_sandbox(self) -> None:
live = PROBE.LiveProbe.__new__(PROBE.LiveProbe)
live.sandboxes = ["sb-one", "sb-two"]