feat(cli): add remote build support for langgraph deploy (#7234)

**Description:** 

 - extend host_backend client with remote build support
- refactored `cli.py` for better clarity
- update tests
This commit is contained in:
Asamu David
2026-04-02 16:39:30 +01:00
committed by GitHub
parent 57b314c5d7
commit b3eeb4fa1d
17 changed files with 2490 additions and 1268 deletions
+161
View File
@@ -0,0 +1,161 @@
"""Create a tarball of project source for remote builds."""
import os
import pathlib
import tarfile
import tempfile
from contextlib import contextmanager
import click
import pathspec
from langgraph_cli.config import Config, _assemble_local_deps
_WARN_SIZE = 50 * 1024 * 1024 # 50 MB
_MAX_SIZE = 200 * 1024 * 1024 # 200 MB
_ALWAYS_EXCLUDE = [
"__pycache__/",
".git/",
".venv/",
"venv/",
"node_modules/",
".tox/",
".mypy_cache/",
]
def _build_ignore_spec(directory: pathlib.Path) -> pathspec.PathSpec:
"""Build a PathSpec combining built-in exclusions with .dockerignore and .gitignore.
Always excludes common non-source directories (_ALWAYS_EXCLUDE). On top of
that, patterns from .dockerignore and .gitignore (if present) are merged in.
"""
lines: list[str] = list(_ALWAYS_EXCLUDE)
for name in (".dockerignore", ".gitignore"):
ignore_file = directory / name
if ignore_file.is_file():
lines.extend(ignore_file.read_text(encoding="utf-8").splitlines())
return pathspec.PathSpec.from_lines("gitwildmatch", lines)
def _tar_filter(tarinfo: tarfile.TarInfo) -> tarfile.TarInfo | None:
"""Strip symlinks, hardlinks, and traversal paths from archive."""
if tarinfo.issym() or tarinfo.islnk():
return None
if ".." in tarinfo.name.split("/"):
return None
return tarinfo
def _add_directory(
tar: tarfile.TarFile,
source_dir: pathlib.Path,
arcname_prefix: str | None,
ignore_spec: pathspec.PathSpec,
) -> None:
"""Recursively add a directory to the tarball under the given prefix.
If arcname_prefix is None, files are added at the archive root.
Paths matching ignore_spec are excluded.
"""
for root, dirs, files in os.walk(source_dir):
rel_root = os.path.relpath(root, source_dir).replace(os.sep, "/")
dirs[:] = [
d
for d in dirs
if not ignore_spec.match_file(
f"{rel_root}/{d}/" if rel_root != "." else f"{d}/"
)
]
for f in files:
full_path = os.path.join(root, f)
rel = os.path.relpath(full_path, source_dir).replace(os.sep, "/")
if ignore_spec.match_file(rel):
continue
arcname = f"{arcname_prefix}/{rel}" if arcname_prefix else rel
info = tar.gettarinfo(full_path, arcname=arcname)
filtered = _tar_filter(info)
if filtered is None:
continue
with open(full_path, "rb") as fobj:
tar.addfile(filtered, fobj)
@contextmanager
def create_archive(
config_path: pathlib.Path,
config: Config,
):
"""Context manager that creates a .tar.gz archive of the project source.
Uses _assemble_local_deps to discover local dependencies referenced in
langgraph.json, including those outside config.parent (monorepo case).
The archive preserves the real filesystem layout relative to the common
ancestor of config.parent and all external dependency directories, so that
relative references (e.g. `../shared-lib`) resolve correctly after
extraction.
Yields (archive_path, file_size, config_relative_path). The temporary
directory holding the archive is cleaned up automatically on exit.
"""
config_path = config_path.resolve()
context_dir = config_path.parent
local_deps = _assemble_local_deps(config_path, config)
extra_contexts = local_deps.additional_contexts or []
dirs_to_include = [context_dir] + list(extra_contexts)
common = context_dir
for d in extra_contexts:
common = pathlib.Path(os.path.commonpath([common, d]))
tmp_dir = tempfile.mkdtemp(prefix="langgraph-deploy-")
try:
archive_path = os.path.join(tmp_dir, "source.tar.gz")
added_dirs: set[str] = set()
with tarfile.open(archive_path, "w:gz") as tar:
for dir_path in dirs_to_include:
rel = dir_path.relative_to(common)
prefix = str(rel).replace(os.sep, "/") if str(rel) != "." else None
key = prefix or ""
if key in added_dirs:
continue
added_dirs.add(key)
ignore_spec = _build_ignore_spec(dir_path)
_add_directory(
tar, dir_path, arcname_prefix=prefix, ignore_spec=ignore_spec
)
file_size = os.path.getsize(archive_path)
config_rel = str(config_path.relative_to(common)).replace(os.sep, "/")
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
if config_rel not in names:
raise click.ClickException(
f"Archive validation failed: {config_rel} not found in archive"
)
if file_size > _MAX_SIZE:
raise click.ClickException(
f"Source archive is {file_size / 1_048_576:.1f} MB, which exceeds the 200 MB limit. "
"Add large files to .dockerignore or .gitignore (model weights, data sets, etc.)."
)
if file_size > _WARN_SIZE:
click.secho(
f" Warning: source archive is {file_size / 1_048_576:.1f} MB. "
"Consider adding large files to .dockerignore or .gitignore.",
fg="yellow",
)
yield archive_path, file_size, config_rel
finally:
import shutil
shutil.rmtree(tmp_dir, ignore_errors=True)
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+128
View File
@@ -1,10 +1,14 @@
import copy
import json
import pathlib
import platform
import shutil
from collections.abc import Callable, Sequence
from typing import Literal, NamedTuple
import click.exceptions
import langgraph_cli.config
from langgraph_cli.exec import subp_exec
ROOT = pathlib.Path(__file__).parent.resolve()
@@ -45,6 +49,54 @@ def _parse_version(version: str) -> Version:
)
def can_build_locally() -> tuple[bool, str | None]:
"""Return whether local deployment builds can run on this machine.
Checks:
- Docker binary is installed
- Docker daemon is running
- Buildx is available when cross-compilation is required (non-x86_64)
"""
if shutil.which("docker") is None:
return (
False,
"Docker is required but not installed.\n"
"Install Docker Desktop: https://docs.docker.com/get-docker/",
)
try:
import subprocess
docker_info = subprocess.run(
["docker", "info"],
capture_output=True,
timeout=10,
)
if docker_info.returncode != 0:
return (
False,
"Docker is installed but not running.\nStart Docker and try again.",
)
if platform.machine() != "x86_64":
buildx = subprocess.run(
["docker", "buildx", "version"],
capture_output=True,
timeout=10,
)
if buildx.returncode != 0:
return (
False,
"Docker Buildx is required but not installed.\n"
"Your machine architecture ("
+ platform.machine()
+ ") requires Buildx to cross-compile images for linux/amd64.\n"
"Install Buildx: https://docs.docker.com/build/install-buildx/",
)
return True, None
except Exception:
return False, "Unable to verify local Docker build support."
def check_capabilities(runner) -> DockerCapabilities:
# check docker available
if shutil.which("docker") is None:
@@ -276,3 +328,79 @@ def compose(
)
compose_str = dict_to_yaml(compose_content)
return compose_str
def build_docker_image(
runner,
set: Callable[[str], None],
config: pathlib.Path,
config_json: dict,
base_image: str | None,
api_version: str | None,
pull: bool,
tag: str,
passthrough: Sequence[str] = (),
install_command: str | None = None,
build_command: str | None = None,
docker_command: Sequence[str] | None = None,
extra_flags: Sequence[str] = (),
verbose: bool = True,
):
"""Build a Docker image from a LangGraph config."""
# pull latest images
if pull:
runner.run(
subp_exec(
"docker",
"pull",
langgraph_cli.config.docker_tag(config_json, base_image, api_version),
verbose=verbose,
)
)
set("Building...")
# apply options
args = [
"-f",
"-", # stdin
"-t",
tag,
]
# determine build context: use current directory for JS projects, config parent for Python
is_js_project = config_json.get("node_version") and not config_json.get(
"python_version"
)
# build/install commands only apply to JS projects for now
# without install/build command, JS projects will follow the old behavior
if is_js_project and (build_command or install_command):
build_context = str(pathlib.Path.cwd())
else:
build_context = str(config.parent)
# Deep copy to avoid mutating the caller's config (config_to_docker
# rewrites graph paths to container-internal paths in place).
config_json = copy.deepcopy(config_json)
stdin, additional_contexts = langgraph_cli.config.config_to_docker(
config_path=config,
config=config_json,
base_image=base_image,
api_version=api_version,
install_command=install_command,
build_command=build_command,
build_context=build_context,
)
# add additional_contexts
if additional_contexts:
for k, v in additional_contexts.items():
args.extend(["--build-context", f"{k}={v}"])
cmd = tuple(docker_command) if docker_command else ("docker", "build")
runner.run(
subp_exec(
*cmd,
*args,
*extra_flags,
*passthrough,
build_context,
input=stdin,
verbose=verbose,
)
)
-59
View File
@@ -1,59 +0,0 @@
"""Helpers for the ``langgraph logs`` CLI command."""
from __future__ import annotations
from datetime import datetime, timezone
import click
from langgraph_cli.host_backend import HostBackendClient
def resolve_deployment_id(
client: HostBackendClient,
deployment_id: str | None,
name: str | None,
) -> str:
"""Resolve a deployment ID from --deployment-id or --name."""
if deployment_id:
return deployment_id
if not name:
raise click.UsageError("Either --deployment-id or --name is required.")
existing = client.list_deployments(name_contains=name)
if isinstance(existing, dict):
for dep in existing.get("resources", []):
if isinstance(dep, dict) and dep.get("name") == name:
found_id = dep.get("id")
if found_id:
return str(found_id)
raise click.ClickException(f"Deployment '{name}' not found.")
def format_timestamp(ts) -> str:
"""Convert a timestamp (epoch ms or string) to a readable string."""
if isinstance(ts, (int, float)):
dt = datetime.fromtimestamp(ts / 1000, tz=timezone.utc)
return dt.strftime("%Y-%m-%d %H:%M:%S")
return str(ts) if ts else ""
def format_log_entry(entry: dict) -> str:
"""Format a single log entry for display."""
ts = format_timestamp(entry.get("timestamp", ""))
level = entry.get("level", "")
message = entry.get("message", "")
if ts and level:
return f"[{ts}] [{level}] {message}"
elif ts:
return f"[{ts}] {message}"
return message
def level_fg(level: str) -> str | None:
"""Return click color for a log level."""
level_upper = level.upper() if level else ""
if level_upper in {"ERROR", "CRITICAL"}:
return "red"
if level_upper == "WARNING":
return "yellow"
return None
+57 -1
View File
@@ -70,7 +70,25 @@ class HostBackendClient:
f"Failed to decode response from {path}: {err}"
) from None
def create_deployment(self, payload: dict[str, Any]) -> dict[str, Any]:
def create_deployment(
self,
name: str,
deployment_type: str,
source: str,
config_path: str | None = None,
secrets: list[dict[str, str]] | None = None,
) -> dict[str, Any]:
"""Create a deployment."""
payload: dict[str, Any] = {
"name": name,
"source": source,
"source_config": {"deployment_type": deployment_type},
"source_revision_config": {},
}
if source == "internal_source" and config_path:
payload["source_revision_config"]["langgraph_config_path"] = config_path
if secrets is not None:
payload["secrets"] = secrets
return self._request("POST", "/v2/deployments", payload)
def list_deployments(self, name_contains: str = "") -> dict[str, Any]:
@@ -92,6 +110,13 @@ class HostBackendClient:
f"/v2/deployments/{deployment_id}/push-token",
)
def request_upload_url(self, deployment_id: str) -> dict[str, Any]:
"""Get a signed GCS URL for uploading the source tarball."""
return self._request(
"POST",
f"/v2/deployments/{deployment_id}/upload-url",
)
def update_deployment(
self,
deployment_id: str,
@@ -99,6 +124,7 @@ class HostBackendClient:
secrets: list[dict[str, str]] | None = None,
) -> dict[str, Any]:
payload: dict[str, Any] = {
"revision_source": "internal_docker",
"source_revision_config": {"image_uri": image_uri},
}
if secrets is not None:
@@ -109,6 +135,36 @@ class HostBackendClient:
payload,
)
def update_deployment_internal_source(
self,
deployment_id: str,
source_tarball_path: str,
config_path: str,
secrets: list[dict[str, str]] | None = None,
install_command: str | None = None,
build_command: str | None = None,
) -> dict[str, Any]:
"""Trigger a remote build revision with the uploaded tarball."""
payload: dict[str, Any] = {
"revision_source": "internal_source",
"source_revision_config": {
"source_tarball_path": source_tarball_path,
"langgraph_config_path": config_path,
},
}
source_config: dict[str, Any] = {}
if install_command is not None:
source_config["install_command"] = install_command
if build_command is not None:
source_config["build_command"] = build_command
if source_config:
payload["source_config"] = source_config
if secrets is not None:
payload["secrets"] = secrets
return self._request("PATCH", f"/v2/deployments/{deployment_id}", payload)
def list_revisions(self, deployment_id: str, limit: int = 1) -> dict[str, Any]:
return self._request(
"GET",
+9
View File
@@ -18,6 +18,9 @@ class Progress:
self._show_elapsed = elapsed
# use this to make sure we don't kill thread when we set msg to ""
self._stop = threading.Event()
# signalled when the spinner has no text on screen
self._line_clear = threading.Event()
self._line_clear.set()
self.spinner_generator = self.spinning_cursor()
def spinner_iteration(self):
@@ -43,13 +46,16 @@ class Progress:
start = time.monotonic()
while not self._stop.is_set():
if not self.message:
self._line_clear.set()
time.sleep(self.delay)
continue
if self._show_elapsed:
self.message = self._format_elapsed(time.monotonic() - start)
message = self.message
if not message:
self._line_clear.set()
continue
self._line_clear.clear()
sys.stdout.write(next(self.spinner_generator) + " " + message)
sys.stdout.flush()
time.sleep(self.delay)
@@ -60,6 +66,7 @@ class Progress:
+ "\b" * (len(message) + 2)
)
sys.stdout.flush()
self._line_clear.set()
def __enter__(self) -> Callable[[str], None]:
if sys.stdout.isatty():
@@ -69,6 +76,8 @@ class Progress:
def set_message(message):
self.message = message
self._base_message = message or self._base_message
if not message:
self._line_clear.wait(timeout=0.5)
return set_message
else:
+1 -65
View File
@@ -1,4 +1,4 @@
from collections.abc import Sequence
"""General-purpose utilities shared across the LangGraph CLI."""
import click
@@ -25,67 +25,3 @@ def warn_non_wolfi_distro(config_json: dict) -> None:
fg="yellow",
)
click.secho("") # Empty line for better readability
def _extract_deployment_url(deployment: dict[str, object]) -> str:
source_config = deployment.get("source_config")
if isinstance(source_config, dict):
custom_url = source_config.get("custom_url")
if isinstance(custom_url, str) and custom_url:
return custom_url
return "-"
def format_deployments_table(deployments: Sequence[dict[str, object]]) -> str:
headers = ("Deployment ID", "Deployment Name", "Deployment URL")
rows = [
(
str(deployment.get("id", "-") or "-"),
str(deployment.get("name", "-") or "-"),
_extract_deployment_url(deployment),
)
for deployment in deployments
]
widths = [
max(len(headers[index]), *(len(row[index]) for row in rows))
for index in range(len(headers))
]
def format_row(row: Sequence[str]) -> str:
return " ".join(value.ljust(widths[index]) for index, value in enumerate(row))
lines = [format_row(headers), format_row(tuple("-" * width for width in widths))]
lines.extend(format_row(row) for row in rows)
return "\n".join(lines)
def format_revisions_table(revisions: Sequence[dict[str, object]]) -> str:
headers = ("Revision ID", "Status", "Created At")
latest_deployed_seen = False
rows = []
for revision in revisions:
status = str(revision.get("status", "-") or "-")
if status == "DEPLOYED":
if latest_deployed_seen:
status = "REPLACED"
else:
latest_deployed_seen = True
rows.append(
(
str(revision.get("id", "-") or "-"),
status,
str(revision.get("created_at", "-") or "-"),
)
)
widths = [
max(len(headers[index]), *(len(row[index]) for row in rows))
for index in range(len(headers))
]
def format_row(row: Sequence[str]) -> str:
return " ".join(value.ljust(widths[index]) for index, value in enumerate(row))
lines = [format_row(headers), format_row(tuple("-" * width for width in widths))]
lines.extend(format_row(row) for row in rows)
return "\n".join(lines)
+1
View File
@@ -15,6 +15,7 @@ dependencies = [
"click>=8.1.7",
"httpx>=0.24.0",
"langgraph-sdk>=0.1.0 ; python_version >= '3.11'",
"pathspec>=0.11.0",
"python-dotenv>=0.8.0",
]
[tool.hatch.version]
+9 -9
View File
@@ -9,7 +9,7 @@ from pathlib import Path
from click.testing import CliRunner
import langgraph_cli.cli as cli_module
import langgraph_cli.deploy as deploy_module
from langgraph_cli.cli import cli, prepare_args_and_stdin
from langgraph_cli.config import Config, _get_pip_cleanup_lines, validate_config
from langgraph_cli.docker import DEFAULT_POSTGRES_URI, DockerCapabilities, Version
@@ -346,7 +346,7 @@ def test_deploy_list_command(monkeypatch) -> None:
]
}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -386,7 +386,7 @@ def test_deploy_list_command_no_results(monkeypatch) -> None:
def list_deployments(self, name_contains: str = ""):
return {"resources": []}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -432,7 +432,7 @@ def test_deploy_revisions_list_command(monkeypatch) -> None:
]
}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -473,7 +473,7 @@ def test_deploy_revisions_list_command_no_results(monkeypatch) -> None:
def list_revisions(self, deployment_id: str, limit: int = 1):
return {"resources": []}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -506,7 +506,7 @@ def test_deploy_revisions_list_command_with_explicit_limit(monkeypatch) -> None:
captured["limit"] = str(limit)
return {"resources": []}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -552,7 +552,7 @@ def test_deploy_delete_command(monkeypatch) -> None:
captured["deployment_id"] = deployment_id
return None
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -594,7 +594,7 @@ def test_deploy_delete_command_cancelled(monkeypatch) -> None:
deleted = True
return None
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -629,7 +629,7 @@ def test_deploy_delete_command_force(monkeypatch) -> None:
captured["deployment_id"] = deployment_id
return None
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
+299
View File
@@ -0,0 +1,299 @@
import os
import tarfile
from unittest.mock import patch
import click
import pytest
from langgraph_cli.archive import (
_add_directory,
_build_ignore_spec,
_tar_filter,
create_archive,
)
# ---------------------------------------------------------------------------
# _tar_filter
# ---------------------------------------------------------------------------
class TestTarFilter:
def _make_info(self, name: str, *, type_: int = tarfile.REGTYPE) -> tarfile.TarInfo:
info = tarfile.TarInfo(name=name)
info.type = type_
return info
def test_regular_file_passes(self):
info = self._make_info("src/main.py")
assert _tar_filter(info) is info
def test_symlink_rejected(self):
info = self._make_info("link", type_=tarfile.SYMTYPE)
assert _tar_filter(info) is None
def test_hardlink_rejected(self):
info = self._make_info("link", type_=tarfile.LNKTYPE)
assert _tar_filter(info) is None
def test_path_traversal_rejected(self):
info = self._make_info("../../etc/passwd")
assert _tar_filter(info) is None
def test_path_traversal_in_middle_rejected(self):
info = self._make_info("src/../../../etc/passwd")
assert _tar_filter(info) is None
def test_dotdot_as_name_component_rejected(self):
info = self._make_info("foo/../bar")
assert _tar_filter(info) is None
def test_dotdot_in_filename_allowed(self):
"""A file literally named 'foo..bar' is not traversal."""
info = self._make_info("foo..bar")
assert _tar_filter(info) is info
def test_directory_passes(self):
info = self._make_info("src/", type_=tarfile.DIRTYPE)
assert _tar_filter(info) is info
# ---------------------------------------------------------------------------
# _build_ignore_spec
# ---------------------------------------------------------------------------
class TestBuildIgnoreSpec:
def test_always_excludes_builtins(self, tmp_path):
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("__pycache__/")
assert spec.match_file(".git/")
assert spec.match_file(".venv/")
assert spec.match_file("venv/")
assert spec.match_file("node_modules/")
assert spec.match_file(".tox/")
assert spec.match_file(".mypy_cache/")
def test_regular_file_not_excluded(self, tmp_path):
spec = _build_ignore_spec(tmp_path)
assert not spec.match_file("main.py")
assert not spec.match_file("src/app.py")
def test_merges_dockerignore(self, tmp_path):
(tmp_path / ".dockerignore").write_text("*.log\nbuild/\n")
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("server.log")
assert spec.match_file("build/")
# builtins still present
assert spec.match_file("__pycache__/")
def test_merges_gitignore(self, tmp_path):
(tmp_path / ".gitignore").write_text("*.pyc\ndist/\n")
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("module.pyc")
assert spec.match_file("dist/")
def test_merges_both_ignore_files(self, tmp_path):
(tmp_path / ".dockerignore").write_text("*.log\n")
(tmp_path / ".gitignore").write_text("*.pyc\n")
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("app.log")
assert spec.match_file("mod.pyc")
def test_no_ignore_files_only_builtins(self, tmp_path):
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("__pycache__/")
assert not spec.match_file("README.md")
# ---------------------------------------------------------------------------
# _add_directory
# ---------------------------------------------------------------------------
class TestAddDirectory:
def _create_project(self, tmp_path):
"""Create a small project structure for testing."""
(tmp_path / "main.py").write_text("print('hello')")
(tmp_path / "lib").mkdir()
(tmp_path / "lib" / "util.py").write_text("x = 1")
(tmp_path / "__pycache__").mkdir()
(tmp_path / "__pycache__" / "main.cpython-311.pyc").write_bytes(b"\x00")
return tmp_path
def test_adds_files_without_prefix(self, tmp_path):
project = self._create_project(tmp_path)
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix=None, ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert "main.py" in names
assert "lib/util.py" in names
def test_excludes_pycache(self, tmp_path):
project = self._create_project(tmp_path)
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix=None, ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert not any("__pycache__" in n for n in names)
def test_adds_files_with_prefix(self, tmp_path):
project = self._create_project(tmp_path)
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix="myapp", ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert "myapp/main.py" in names
assert "myapp/lib/util.py" in names
def test_respects_custom_ignore_patterns(self, tmp_path):
project = self._create_project(tmp_path)
(project / ".gitignore").write_text("lib/\n")
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix=None, ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert "main.py" in names
assert "lib/util.py" not in names
# ---------------------------------------------------------------------------
# create_archive (integration)
# ---------------------------------------------------------------------------
class TestCreateArchive:
def _make_project(self, tmp_path):
"""Set up a minimal project directory with a config file."""
project = tmp_path / "myproject"
project.mkdir()
config_file = project / "langgraph.json"
config_file.write_text('{"dependencies": ["."]}')
(project / "app.py").write_text("print('hello')")
(project / "__pycache__").mkdir()
(project / "__pycache__" / "app.cpython-311.pyc").write_bytes(b"\x00")
return config_file
@patch("langgraph_cli.archive._assemble_local_deps")
def test_yields_archive_with_config(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with create_archive(config_file, {}) as (archive_path, file_size, config_rel):
assert os.path.isfile(archive_path)
assert archive_path.endswith(".tar.gz")
assert file_size > 0
assert config_rel == "langgraph.json"
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
assert "langgraph.json" in names
assert "app.py" in names
@patch("langgraph_cli.archive._assemble_local_deps")
def test_excludes_pycache(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with create_archive(config_file, {}) as (archive_path, _size, _rel):
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
assert not any("__pycache__" in n for n in names)
@patch("langgraph_cli.archive._assemble_local_deps")
def test_cleans_up_tmp_dir_on_normal_exit(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with create_archive(config_file, {}) as (archive_path, _size, _rel):
tmp_dir = os.path.dirname(archive_path)
assert os.path.isdir(tmp_dir)
assert not os.path.exists(tmp_dir)
@patch("langgraph_cli.archive._assemble_local_deps")
def test_cleans_up_tmp_dir_on_exception(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with pytest.raises(RuntimeError, match="boom"):
with create_archive(config_file, {}) as (archive_path, _size, _rel):
tmp_dir = os.path.dirname(archive_path)
raise RuntimeError("boom")
assert not os.path.exists(tmp_dir)
@patch("langgraph_cli.archive._assemble_local_deps")
@patch("langgraph_cli.archive._MAX_SIZE", 10)
def test_raises_on_oversized_archive(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with pytest.raises(click.ClickException, match="exceeds the 200 MB limit"):
with create_archive(config_file, {}):
pass
@patch("langgraph_cli.archive._assemble_local_deps")
def test_handles_extra_contexts(self, mock_deps, tmp_path):
"""Monorepo case: project + sibling dependency directory."""
from langgraph_cli.config import LocalDeps
project = tmp_path / "myproject"
project.mkdir()
config_file = project / "langgraph.json"
config_file.write_text('{"dependencies": [".", "../shared"]}')
(project / "app.py").write_text("print('hello')")
shared = tmp_path / "shared"
shared.mkdir()
(shared / "lib.py").write_text("y = 2")
mock_deps.return_value = LocalDeps(
pip_reqs=[],
real_pkgs={},
faux_pkgs={},
additional_contexts=[shared],
)
with create_archive(config_file, {}) as (archive_path, _size, config_rel):
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
assert "myproject/app.py" in names
assert "shared/lib.py" in names
assert config_rel == "myproject/langgraph.json"
@@ -6,12 +6,14 @@ import click
import httpx
import pytest
from langgraph_cli.cli import (
from langgraph_cli.deploy import (
_call_host_backend_with_optional_tenant,
_docker_config_for_token,
_normalize_image_name,
_normalize_image_tag,
_env_without_deployment_name,
_parse_env_from_config,
_resolve_env_path,
normalize_image_name,
normalize_image_tag,
)
from langgraph_cli.host_backend import HostBackendClient, HostBackendError
@@ -40,47 +42,47 @@ class TestDockerConfigForToken:
class TestNormalizeImageName:
def test_simple_name(self):
assert _normalize_image_name("myapp") == "myapp"
assert normalize_image_name("myapp") == "myapp"
def test_uppercase_lowered(self):
assert _normalize_image_name("MyApp") == "myapp"
assert normalize_image_name("MyApp") == "myapp"
def test_special_chars_replaced(self):
assert _normalize_image_name("my app!@#v2") == "my-app-v2"
assert normalize_image_name("my app!@#v2") == "my-app-v2"
def test_dots_and_hyphens_kept(self):
assert _normalize_image_name("my-app.v2") == "my-app.v2"
assert normalize_image_name("my-app.v2") == "my-app.v2"
def test_leading_trailing_stripped(self):
assert _normalize_image_name("--my-app..") == "my-app"
assert normalize_image_name("--my-app..") == "my-app"
def test_empty_string_returns_app(self):
assert _normalize_image_name("") == "app"
assert normalize_image_name("") == "app"
def test_none_returns_app(self):
assert _normalize_image_name(None) == "app"
assert normalize_image_name(None) == "app"
def test_all_invalid_chars_returns_app(self):
assert _normalize_image_name("!!!") == "app"
assert normalize_image_name("!!!") == "app"
class TestNormalizeImageTag:
def test_valid_tag(self):
assert _normalize_image_tag("v1.2.3") == "v1.2.3"
assert normalize_image_tag("v1.2.3") == "v1.2.3"
def test_empty_defaults_to_latest(self):
assert _normalize_image_tag("") == "latest"
assert normalize_image_tag("") == "latest"
def test_alphanumeric_and_special(self):
assert _normalize_image_tag("my_tag-1.0") == "my_tag-1.0"
assert normalize_image_tag("my_tag-1.0") == "my_tag-1.0"
def test_invalid_chars_raises(self):
with pytest.raises(click.UsageError, match="Image tag may only contain"):
_normalize_image_tag("v1.0:bad")
normalize_image_tag("v1.0:bad")
def test_spaces_raises(self):
with pytest.raises(click.UsageError, match="Image tag may only contain"):
_normalize_image_tag("has space")
normalize_image_tag("has space")
class TestParseEnvFromConfig:
@@ -137,6 +139,51 @@ class TestParseEnvFromConfig:
assert result["EMPTY"] == ""
class TestResolveEnvPath:
def test_inline_env_dict_returns_none(self, tmp_path):
config_path = tmp_path / "langgraph.json"
config_path.touch()
assert _resolve_env_path({"env": {"FOO": "bar"}}, config_path) is None
def test_relative_env_path_resolves(self, tmp_path):
env_file = tmp_path / "custom.env"
env_file.write_text("FOO=bar\n")
config_path = tmp_path / "langgraph.json"
config_path.touch()
resolved = _resolve_env_path({"env": "custom.env"}, config_path)
assert resolved == env_file.resolve()
def test_missing_env_file_returns_none(self, tmp_path):
config_path = tmp_path / "langgraph.json"
config_path.touch()
assert _resolve_env_path({"env": "missing.env"}, config_path) is None
def test_default_env_is_cwd_dotenv(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
config_path = tmp_path / "langgraph.json"
config_path.touch()
assert _resolve_env_path({}, config_path) == tmp_path / ".env"
class TestEnvWithoutDeploymentName:
def test_removes_deployment_name_only(self):
env = {
"LANGSMITH_DEPLOYMENT_NAME": "my-deploy",
"KEEP_ME": "value",
}
cleaned = _env_without_deployment_name(env)
assert "LANGSMITH_DEPLOYMENT_NAME" not in cleaned
assert cleaned["KEEP_ME"] == "value"
# Original dict should be unchanged.
assert env["LANGSMITH_DEPLOYMENT_NAME"] == "my-deploy"
def test_noop_when_deployment_name_absent(self):
env = {"FOO": "bar"}
assert _env_without_deployment_name(env) == {"FOO": "bar"}
class TestCallHostBackendWithOptionalTenant:
def _make_client(self, handler):
c = HostBackendClient("https://api.example.com", "test-key")
@@ -121,7 +121,9 @@ def test_request_transport_error_raises():
def test_create_deployment(client):
result = client.create_deployment({"name": "my-deploy"})
result = client.create_deployment(
name="my-deploy", deployment_type="dev", source="internal_docker"
)
assert result == {"ok": True}
@@ -1,4 +1,4 @@
from langgraph_cli.helpers import format_log_entry, format_timestamp, level_fg
from langgraph_cli.deploy import format_log_entry, format_timestamp, level_fg
class TestFormatTimestamp:
+2 -3
View File
@@ -1,12 +1,11 @@
from unittest.mock import patch
from langgraph_cli.util import (
from langgraph_cli.deploy import (
_extract_deployment_url,
clean_empty_lines,
format_deployments_table,
format_revisions_table,
warn_non_wolfi_distro,
)
from langgraph_cli.util import clean_empty_lines, warn_non_wolfi_distro
def test_clean_empty_lines():
+3 -1
View File
@@ -1,5 +1,5 @@
version = 1
revision = 3
revision = 4
requires-python = ">=3.10"
resolution-markers = [
"python_full_version >= '3.11'",
@@ -985,6 +985,7 @@ dependencies = [
{ name = "click" },
{ name = "httpx" },
{ name = "langgraph-sdk", marker = "python_full_version >= '3.11'" },
{ name = "pathspec" },
{ name = "python-dotenv" },
]
@@ -1026,6 +1027,7 @@ requires-dist = [
{ name = "langgraph-api", marker = "python_full_version >= '3.11' and extra == 'inmem'", specifier = ">=0.5.35,<0.8.0" },
{ name = "langgraph-runtime-inmem", marker = "python_full_version >= '3.11' and extra == 'inmem'", specifier = ">=0.7" },
{ name = "langgraph-sdk", marker = "python_full_version >= '3.11'", specifier = ">=0.1.0" },
{ name = "pathspec", specifier = ">=0.11.0" },
{ name = "python-dotenv", specifier = ">=0.8.0" },
]
provides-extras = ["inmem"]
+2
View File
@@ -1691,6 +1691,7 @@ dependencies = [
{ name = "click", marker = "python_full_version < '3.14'" },
{ name = "httpx", marker = "python_full_version < '3.14'" },
{ name = "langgraph-sdk", marker = "python_full_version >= '3.11' and python_full_version < '3.14'" },
{ name = "pathspec", marker = "python_full_version < '3.14'" },
{ name = "python-dotenv", marker = "python_full_version < '3.14'" },
]
@@ -1707,6 +1708,7 @@ requires-dist = [
{ name = "langgraph-api", marker = "python_full_version >= '3.11' and extra == 'inmem'", specifier = ">=0.5.35,<0.8.0" },
{ name = "langgraph-runtime-inmem", marker = "python_full_version >= '3.11' and extra == 'inmem'", specifier = ">=0.7" },
{ name = "langgraph-sdk", marker = "python_full_version >= '3.11'", specifier = ">=0.1.0" },
{ name = "pathspec", specifier = ">=0.11.0" },
{ name = "python-dotenv", specifier = ">=0.8.0" },
]
provides-extras = ["inmem"]