"""Private, fail-closed transport build for real Antigravity adapter scenarios. The runner still owns startup and teardown. Only its copied native transport is redirected; production source, profiles and endpoint policy are unchanged. """ import json from pathlib import Path import re import shlex import shutil import subprocess from harness import Albedo, Provider, Reply, ROOT ENDPOINT = "https://daily-cloudcode-pa.googleapis.com" INFERENCE = ENDPOINT + "/v1internal:streamGenerateContent?alt=sse" TOKEN = "fixture-antigravity" SIGNATURE = "Zml4dHVyZS1zaWduYXR1cmU=" GEMINI = "gemini-3-flash" CLAUDE = "claude-opus-5-5" GUARD = Path(__file__).with_name("albedo_antigravity_offline.erl") # No inets entry point in the private build can contact a provider. Retain # the production exports so an added front door fails fixture setup. HTTP_STUB = """-module(albedo_http). -export([get_response/4, get_body/4, post_body/6, get/4, post/6, request/6, fetch/4, failure/3, tls_options/1, locations/2]). get_response(_, _, _, _) -> {error, <<"offline fixture">>}. get_body(_, _, _, _) -> {error, <<"offline fixture">>}. post_body(_, _, _, _, _, _) -> {error, <<"offline fixture">>}. get(U, H, T, C) -> request(get, U, H, none, T, C). post(U, H, K, B, T, C) -> request(post, U, H, {K, B}, T, C). request(_, _, _, _, _, _) -> {error, offline_fixture}. fetch(_, _, _, _) -> {error, <<"offline fixture">>}. failure(_, _, _) -> <<"offline fixture">>. tls_options(_) -> error(offline_fixture). locations(Urls, _) -> Urls. """ def compile_offline(ebin: Path, source: Path, home: Path, target: str) -> None: """Compile replacements without touching the checkout or a running VM.""" route = re.fullmatch(r"http://127\.0\.0\.1:([0-9]+)/t/[0-9]+", target) if not home.is_absolute() or route is None or not 0 < int(route[1]) < 65536: raise ValueError("offline fixture requires an absolute home and loopback route") transport = (ROOT / "src/albedo/openai_api/albedo_openai_transport.erl").read_text() marker = "open_valid_headers(Method, URL, Headers, Body, Timeout) ->\n case parse_url(URL) of" if transport.count(marker) != 1: raise AssertionError("stream transport interception point changed") def literal(value: str) -> str: return "<<" + ",".join(str(byte) for byte in value.encode()) + ">>" routed = f"albedo_antigravity_offline:route(URL, Headers, {literal(target)}, {literal(str(home))})" source.mkdir() (source / "albedo_openai_transport.erl").write_text( transport.replace( marker, marker.replace("parse_url(URL)", f"parse_url({routed})") ) ) original = (ROOT / "src/albedo/harness/albedo_http.erl").read_text() def exports(text: str) -> set[str]: return set( re.findall( r"\b[a-z_]+/[0-9]+", text.split("-export([", 1)[1].split("]).", 1)[0] ) ) if exports(original) != exports(HTTP_STUB): raise AssertionError("non-streaming HTTP exports changed") (source / "albedo_http.erl").write_text(HTTP_STUB) subprocess.run( [ "erlc", "-Wall", "-Werror", "-o", str(ebin), str(GUARD), *map(str, source.glob("*.erl")), ], check=True, text=True, ) def prepare(app: Albedo, provider: Provider) -> None: """Install a sealed private build and dummy accounts before exclusive boot.""" if app.daemon.concurrent or app.daemon.booted: raise AssertionError("Antigravity fixture requires a fresh exclusive daemon") original = Path(app.env["ALBEDO_DAEMON"]).parent for module in ["albedo_openai_transport", "albedo_http"]: built = ROOT / f"build/dev/erlang/albedo/ebin/{module}.beam" if (original / f"albedo/ebin/{module}.beam").read_bytes() != built.read_bytes(): raise AssertionError("snapshot differs from the current daemon build") snapshot = app.root / "antigravity-daemon" shutil.copytree(original, snapshot) ebin = snapshot / "albedo/ebin" ebin.chmod(ebin.stat().st_mode | 0o200) for module in ["albedo_openai_transport", "albedo_http"]: (ebin / f"{module}.beam").unlink() (app.home / ".antigravity-offline").write_text("offline fixture\n") compile_offline(ebin, app.root / "offline-source", app.home, provider.url) subprocess.run( [ str(ROOT / "priv/bin/albedo-daemon"), "erl", "-pa", *map(str, snapshot.glob("*/ebin")), "-noshell", "-eval", 'application:ensure_all_started(crypto), ok = albedo_daemon:seal_build(unicode:characters_to_binary(os:getenv("ALBEDO_BUILD_APP"))), halt().', ], check=True, capture_output=True, text=True, env={**app.env, "ALBEDO_BUILD_APP": str(snapshot / "albedo")}, ) launcher = snapshot / "albedo-daemon" launcher.write_text( "#!/bin/sh\nset -eu\n" f'test "${{ALBEDO_HOME-}}" = {shlex.quote(str(app.home))}\n' f"exec {shlex.quote(str(ROOT / 'priv/bin/albedo-daemon'))} erl -pa {shlex.quote(str(snapshot))}/*/ebin -eval 'albedo@@main:run(albedo)' -noshell -extra \"$@\"\n" ) launcher.chmod(0o700) app.env.update(ALBEDO_DAEMON=str(launcher), ALBEDO_STANDBY="0") app.write_extensions({"quota": {"enabled": False}}) app.store_secrets( "accounts", { "google-antigravity": [ { "type": "oauth", "access": TOKEN, "refresh": "fixture-refresh", "expires": 9999999999999, "projectId": "fixture-project", "email": "fixture@example.invalid", "extension_metadata": {"keep": True}, } ] }, ) (app.home / "antigravity.json").write_text( json.dumps( { "version": "2.16.0", "models": [ { "id": name, "name": name, "context": 200000, "output": 8192, "images": True, } for name in [GEMINI, CLAUDE + "-low", CLAUDE + "-high"] ], "renamed": {}, } ) ) def limit_response(details: object) -> Reply: """A Google HTTP failure with structured details, rather than SSE events.""" return Reply( "error", status=429, error_body={ "error": {"code": 429, "status": "RESOURCE_EXHAUSTED", "details": details} }, )