An open-source, faster Devin alternative
TypeScript
python SDK sends the same x-oc-sdk-version header and ships as 1.0.0, so Python callers migrate to MicroVM by upgrading exactly like TypeScript ones
Brian Reardon5b444395584d
diff --git a/.github/workflows/publish-python-sdk.yml b/.github/workflows/publish-python-sdk.ymlindex dab5ed0c..c7c91b76 100644--- a/.github/workflows/publish-python-sdk.yml+++ b/.github/workflows/publish-python-sdk.yml@@ -28,7 +28,12 @@ jobs:- name: Check if version already publishedid: checkrun: |- LOCAL_VERSION=$(grep '^version' pyproject.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')+ # Read from opencomputer/version.py, not pyproject.toml: the version is+ # dynamic there (hatch reads this same file) so that the published+ # version and the version the SDK announces in x-oc-sdk-version cannot+ # drift apart. Grepping pyproject would now match nothing, leaving+ # LOCAL_VERSION empty and defeating the already-published guard.+ LOCAL_VERSION=$(grep '^__version__ = ' opencomputer/version.py | head -1 | sed 's/.*"\(.*\)".*/\1/')PUBLISHED=$(pip index versions opencomputer-sdk 2>/dev/null | head -1 | sed 's/.*(\(.*\))/\1/' || echo "")if [ "$LOCAL_VERSION" = "$PUBLISHED" ]; thenecho "skip=true" >> "$GITHUB_OUTPUT"diff --git a/sdks/python/opencomputer/__init__.py b/sdks/python/opencomputer/__init__.pyindex 65c02174..3b96e9f5 100644--- a/sdks/python/opencomputer/__init__.py+++ b/sdks/python/opencomputer/__init__.py@@ -68,4 +68,6 @@ __all__ = ["TagKeyInfo",]-__version__ = "0.6.9"+# Single source of truth — pyproject.toml reads the same file, so the published+# version and the version the SDK announces to the server cannot disagree.+from opencomputer.version import __version__ # noqa: E402,F401diff --git a/sdks/python/opencomputer/project.py b/sdks/python/opencomputer/project.pyindex 29703682..1bf9ae88 100644--- a/sdks/python/opencomputer/project.py+++ b/sdks/python/opencomputer/project.py@@ -7,6 +7,8 @@ from typing import Anyimport httpx+from opencomputer.version import sdk_version_headers+def _get_client(api_key: str | None = None,@@ -18,7 +20,7 @@ def _get_client(api_base = url if url.endswith("/api") else f"{url}/api"- headers: dict[str, str] = {}+ headers: dict[str, str] = dict(sdk_version_headers())if key:headers["X-API-Key"] = keydiff --git a/sdks/python/opencomputer/sandbox.py b/sdks/python/opencomputer/sandbox.pyindex 628f9904..d5c61cb5 100644--- a/sdks/python/opencomputer/sandbox.py+++ b/sdks/python/opencomputer/sandbox.py@@ -15,6 +15,7 @@ from opencomputer.image import Imagefrom opencomputer.mounts import Mountsfrom opencomputer.pty import Ptyfrom opencomputer.sse import parse_sse_stream+from opencomputer.version import sdk_version_headersclass ScalingLockedError(Exception):@@ -149,7 +150,10 @@ class Sandbox:# Control plane client always uses /api prefixapi_base = url if url.endswith("/api") else f"{url}/api"- headers: dict[str, str] = {}+ # Which runtime serves this create. See version.py: an org that has not+ # been pinned is routed by the calling SDK's major version, so upgrading+ # this package is how a customer migrates.+ headers: dict[str, str] = dict(sdk_version_headers())if key:headers["X-API-Key"] = keydiff --git a/sdks/python/opencomputer/snapshot.py b/sdks/python/opencomputer/snapshot.pyindex c3da8db2..64dd1f6a 100644--- a/sdks/python/opencomputer/snapshot.py+++ b/sdks/python/opencomputer/snapshot.py@@ -9,6 +9,7 @@ import httpxfrom opencomputer.image import Imagefrom opencomputer.sse import parse_sse_stream+from opencomputer.version import sdk_version_headersclass Snapshots:@@ -41,7 +42,11 @@ class Snapshots:api_base = url if url.endswith("/api") else f"{url}/api"- self._headers: dict[str, str] = {}+ # A template is built differently per runtime — a whole-disk checkpoint on+ # one, a machine image on the other — so the same version header the+ # create path sends has to ride along here, or a caller would build a+ # template their sandboxes cannot use. See version.py.+ self._headers: dict[str, str] = dict(sdk_version_headers())if key:self._headers["X-API-Key"] = keydiff --git a/sdks/python/opencomputer/version.py b/sdks/python/opencomputer/version.pynew file mode 100644index 00000000..7c9443f7--- /dev/null+++ b/sdks/python/opencomputer/version.py@@ -0,0 +1,38 @@+"""The SDK's own identity, sent on the requests that decide which runtime serves+a customer: creating a sandbox and creating a template or snapshot.++This is what makes self-service migration possible. An org that has not been+pinned to a runtime lands on the MicroVM backend when it calls with a 1.x SDK+and on the QEMU fleet when it calls with an older one — so upgrading the+dependency IS the migration, and pinning the old version is the rollback.+Nothing has to change in our database for either direction.++Only the MAJOR is load-bearing: the server compares it against a minimum, so+patch and minor releases never move anyone.++1.0.0, not 2.0.0, even though the docs call this platform generation "v2": every+version this package has ever published is 0.x, so 1.0.0 is both the first+stable release and a clean major break from all of them. The product generation+and the package major are not the same number.++This file is the single source of truth for the version: pyproject.toml declares+it dynamic and hatch's version hook reads the assignment below, and CI reads the+same line to decide whether to publish. That is deliberate — a constant that+drifted from the published version would ship a release still announcing the old+one, leaving every customer who upgraded on the old runtime with no error+anywhere. Keep the assignment on one line, at column 0, and do not let prose in+this docstring start with that name: two readers of this file are greps.+"""++from __future__ import annotations++__version__ = "1.0.0"++#: Header carrying the version above. Lower-case to match the TypeScript SDK;+#: HTTP/2 requires it there and consistency costs nothing here.+SDK_VERSION_HEADER = "x-oc-sdk-version"+++def sdk_version_headers() -> dict[str, str]:+ """The identifying header, ready to merge into a request's headers."""+ return {SDK_VERSION_HEADER: __version__}diff --git a/sdks/python/pyproject.toml b/sdks/python/pyproject.tomlindex c85ea8da..3ae2acab 100644--- a/sdks/python/pyproject.toml+++ b/sdks/python/pyproject.toml@@ -4,7 +4,10 @@ build-backend = "hatchling.build"[project]name = "opencomputer-sdk"-version = "0.6.9"+# Read from opencomputer/version.py, which is also what the SDK announces to the+# server in x-oc-sdk-version. One source, so the published version and the+# routed-on version cannot disagree.+dynamic = ["version"]description = "Python SDK for OpenComputer - cloud sandbox platform"readme = "README.md"requires-python = ">=3.10"@@ -41,6 +44,9 @@ Repository = "https://github.com/diggerhq/opensandbox"Documentation = "https://github.com/diggerhq/opensandbox/tree/main/sdks/python"Issues = "https://github.com/diggerhq/opensandbox/issues"+[tool.hatch.version]+path = "opencomputer/version.py"+[tool.hatch.build.targets.wheel]packages = ["opencomputer"]exclude = ["__pycache__"]