mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-12-02 10:33:07 +01:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8bad677b0a | ||
|
|
83888ec170 | ||
|
|
6a3005b545 | ||
|
|
1ddf3cc19c | ||
|
|
c8a9bdda08 |
No files matched your search
@@ -1 +1 @@
|
||||
__version__ = "0.4.44"
|
||||
__version__ = "0.4.45"
|
||||
@@ -290,6 +290,17 @@ class RequestedPlacement:
|
||||
return self.k8s_namespace
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class StoredPlacement:
|
||||
listener_id: str | None
|
||||
k8s_namespace: str | None
|
||||
|
||||
def allows(self, requested: RequestedPlacement) -> bool:
|
||||
return requested.listener_id in (None, self.listener_id) and (
|
||||
requested.k8s_namespace in (None, self.k8s_namespace)
|
||||
)
|
||||
|
||||
|
||||
def _describe_listeners(listeners: Sequence[Listener]) -> str:
|
||||
shown = listeners[:_LISTENERS_SHOWN]
|
||||
lines = [
|
||||
@@ -306,6 +317,21 @@ def _describe_listeners(listeners: Sequence[Listener]) -> str:
|
||||
class ExistingDeployment:
|
||||
id: str
|
||||
source: str | None
|
||||
placement: StoredPlacement | None = None
|
||||
image_pull_secrets: frozenset[str] | None = None
|
||||
|
||||
@classmethod
|
||||
def from_resource(
|
||||
cls, deployment_id: str, resource: Mapping[str, object]
|
||||
) -> "ExistingDeployment":
|
||||
source_config = resource.get("source_config")
|
||||
stored = source_config if isinstance(source_config, Mapping) else {}
|
||||
return cls(
|
||||
deployment_id,
|
||||
_source_of(resource),
|
||||
_stored_placement(stored),
|
||||
_stored_image_pull_secrets(stored),
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -517,12 +543,47 @@ def _source_of(resource: object) -> str | None:
|
||||
return source if isinstance(source, str) else None
|
||||
|
||||
|
||||
def _stored_placement(source_config: Mapping[str, object]) -> StoredPlacement | None:
|
||||
if "listener_id" not in source_config:
|
||||
return None
|
||||
listener_id = source_config["listener_id"]
|
||||
if listener_id is not None and not isinstance(listener_id, str):
|
||||
return None
|
||||
listener_config = source_config.get("listener_config")
|
||||
namespace = (
|
||||
listener_config.get("k8s_namespace")
|
||||
if isinstance(listener_config, Mapping)
|
||||
else None
|
||||
)
|
||||
return StoredPlacement(
|
||||
listener_id, namespace if isinstance(namespace, str) else None
|
||||
)
|
||||
|
||||
|
||||
def _stored_image_pull_secrets(
|
||||
source_config: Mapping[str, object],
|
||||
) -> frozenset[str] | None:
|
||||
if "resource_spec" not in source_config:
|
||||
return None
|
||||
resource_spec = source_config["resource_spec"] or {}
|
||||
if not isinstance(resource_spec, Mapping):
|
||||
return None
|
||||
entries = resource_spec.get("image_pull_secrets") or []
|
||||
if not isinstance(entries, list):
|
||||
return None
|
||||
return frozenset(
|
||||
entry["name"]
|
||||
for entry in entries
|
||||
if isinstance(entry, Mapping) and isinstance(entry.get("name"), str)
|
||||
)
|
||||
|
||||
|
||||
def find_deployment_by_name(
|
||||
client: HostBackendClient, name: str
|
||||
) -> ExistingDeployment | None:
|
||||
for resource in client.list_deployments(name=name, name_contains=name):
|
||||
if resource.get("name") == name and resource.get("id"):
|
||||
return ExistingDeployment(str(resource["id"]), _source_of(resource))
|
||||
return ExistingDeployment.from_resource(str(resource["id"]), resource)
|
||||
return None
|
||||
|
||||
|
||||
@@ -829,7 +890,7 @@ def _fetch_deployment(
|
||||
resource = _call_host_backend_with_optional_tenant(
|
||||
client, lambda c: c.get_deployment(selector.deployment_id)
|
||||
)
|
||||
return ExistingDeployment(selector.deployment_id, _source_of(resource)), step + 1
|
||||
return ExistingDeployment.from_resource(selector.deployment_id, resource), step + 1
|
||||
|
||||
|
||||
def _find_deployment(
|
||||
@@ -858,7 +919,7 @@ def _find_deployment(
|
||||
)
|
||||
found = next(
|
||||
(
|
||||
ExistingDeployment(str(dep["id"]), _source_of(dep))
|
||||
ExistingDeployment.from_resource(str(dep["id"]), dep)
|
||||
for dep in existing
|
||||
if dep.get("id") and not dep.get("is_preview")
|
||||
),
|
||||
@@ -1773,11 +1834,39 @@ _K8S_SECRET_NAME_RE = re.compile(
|
||||
)
|
||||
|
||||
|
||||
def _placement_refusal(stored: StoredPlacement | None) -> str:
|
||||
if stored is None:
|
||||
return "already exists, so drop --listener-id and --k8s-namespace"
|
||||
if stored.listener_id is None:
|
||||
return (
|
||||
"was created without a listener, so drop --listener-id and --k8s-namespace"
|
||||
)
|
||||
if stored.k8s_namespace is None:
|
||||
return (
|
||||
f"runs on listener {stored.listener_id}, but its namespace was not "
|
||||
f"reported, so pass --listener-id {stored.listener_id} without "
|
||||
"--k8s-namespace, or drop both flags"
|
||||
)
|
||||
return (
|
||||
f"runs on listener {stored.listener_id} in namespace "
|
||||
f"{stored.k8s_namespace}, so pass those values or drop --listener-id "
|
||||
"and --k8s-namespace"
|
||||
)
|
||||
|
||||
|
||||
def _pull_secrets_refusal(stored: frozenset[str] | None) -> str:
|
||||
remedy = (
|
||||
"drop --image-pull-secret and update resource_spec through the API or "
|
||||
"the LangSmith UI instead"
|
||||
)
|
||||
if stored is None:
|
||||
return f"already exists, so {remedy}"
|
||||
held = ", ".join(sorted(stored)) or "no image pull secrets"
|
||||
return f"uses {held}, so pass the same secrets or {remedy}"
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CreateOnlySettings:
|
||||
"""Options for an external-image deployment that only apply when
|
||||
creating one, never when updating an existing deployment."""
|
||||
|
||||
placement: RequestedPlacement
|
||||
image_pull_secrets: tuple[str, ...] = ()
|
||||
|
||||
@@ -1801,21 +1890,26 @@ class CreateOnlySettings:
|
||||
"apply when creating an Agent Server with --push-to or --image-uri."
|
||||
)
|
||||
|
||||
def ensure_not_existing(self, deployment_id: str) -> None:
|
||||
if self.placement.requested:
|
||||
def ensure_unchanged(self, existing: ExistingDeployment) -> None:
|
||||
stored_placement = existing.placement
|
||||
if self.placement.requested and not (
|
||||
stored_placement is not None and stored_placement.allows(self.placement)
|
||||
):
|
||||
raise click.UsageError(
|
||||
"Listener and namespace are fixed when an Agent Server is "
|
||||
f"created. Agent Server {deployment_id} already exists, so "
|
||||
"drop --listener-id and --k8s-namespace, or create a new "
|
||||
"Agent Server with a different --name."
|
||||
f"created. Agent Server {existing.id} "
|
||||
f"{_placement_refusal(stored_placement)}, or create a new Agent "
|
||||
"Server with a different --name."
|
||||
)
|
||||
if self.image_pull_secrets:
|
||||
stored_secrets = existing.image_pull_secrets
|
||||
if self.image_pull_secrets and stored_secrets != frozenset(
|
||||
self.image_pull_secrets
|
||||
):
|
||||
raise click.UsageError(
|
||||
"langgraph deploy does not update image pull secrets on an "
|
||||
f"existing Agent Server. Agent Server {deployment_id} already "
|
||||
"exists, so drop --image-pull-secret and update resource_spec "
|
||||
"through the API or the LangSmith UI instead, or create a "
|
||||
"new Agent Server with a different --name."
|
||||
f"existing Agent Server. Agent Server {existing.id} "
|
||||
f"{_pull_secrets_refusal(stored_secrets)}, or create a new "
|
||||
"Agent Server with a different --name."
|
||||
)
|
||||
|
||||
|
||||
@@ -1844,7 +1938,7 @@ class CustomerRegistrySource:
|
||||
self, ctx: DeployContext, existing: ExistingDeployment, step: int
|
||||
) -> DeployOutcome:
|
||||
_ensure_customer_registry_source(existing)
|
||||
self.create_only.ensure_not_existing(existing.id)
|
||||
self.create_only.ensure_unchanged(existing)
|
||||
image_uri, step = self._publish(ctx, step)
|
||||
_log_deploy_step(step, f"Updating Agent Server {existing.id}")
|
||||
updated = ctx.client.update_deployment(
|
||||
@@ -1985,12 +2079,9 @@ def _select_source(
|
||||
remote_build_flag: bool | None,
|
||||
placement: RequestedPlacement,
|
||||
image_pull_secrets: tuple[str, ...],
|
||||
selector: DeploymentSelector,
|
||||
) -> DeploymentSource:
|
||||
create_only = CreateOnlySettings(placement, image_pull_secrets)
|
||||
create_only.ensure_creatable(push_to, image_uri)
|
||||
if create_only.requested and isinstance(selector, ById):
|
||||
create_only.ensure_not_existing(selector.deployment_id)
|
||||
if image_uri is not None:
|
||||
if push_to is not None:
|
||||
raise click.UsageError("--image-uri cannot be combined with --push-to.")
|
||||
@@ -2624,7 +2715,6 @@ def _deploy_cmd(
|
||||
remote_build_flag=remote_build_flag,
|
||||
placement=RequestedPlacement(listener_id, k8s_namespace),
|
||||
image_pull_secrets=image_pull_secrets,
|
||||
selector=selector,
|
||||
)
|
||||
|
||||
client = _create_host_backend_client(host_url, api_key, env_vars=env_vars)
|
||||
|
||||
@@ -50,6 +50,11 @@ TWO_NAMESPACE_LISTENER = {
|
||||
"compute_id": "prod-cluster",
|
||||
"compute_config": {"k8s_namespaces": ["agents", "agents-staging"]},
|
||||
}
|
||||
STORED_SOURCE_CONFIG = {
|
||||
"listener_id": LISTENER_ID,
|
||||
"listener_config": {"k8s_namespace": "agents"},
|
||||
"resource_spec": {"image_pull_secrets": [{"name": "regcred"}]},
|
||||
}
|
||||
CREATED_ID = "dep-created"
|
||||
TRACKED_PACKAGES = ["langgraph:1.0.0"]
|
||||
SIGNED_UPLOAD_URL = "https://storage.example.com/signed"
|
||||
@@ -98,6 +103,15 @@ def _get(deployment_id: str) -> str:
|
||||
return f"GET /v2/deployments/{deployment_id}"
|
||||
|
||||
|
||||
def _external_deployment(source_config: dict) -> dict:
|
||||
return {
|
||||
"id": "dep-ext",
|
||||
"name": "my-app",
|
||||
"source": "external_docker",
|
||||
"source_config": source_config,
|
||||
}
|
||||
|
||||
|
||||
def _looks_like_a_uuid(value: str) -> bool:
|
||||
try:
|
||||
uuid.UUID(value)
|
||||
@@ -331,8 +345,14 @@ class DeployProject:
|
||||
timeline: list[str]
|
||||
uploads: list[tuple[str, str, int, dict[str, str] | None]]
|
||||
|
||||
def run(self, *args: str, host_url: str | None = CONTROL_PLANE_URL) -> Result:
|
||||
def run(
|
||||
self,
|
||||
*args: str,
|
||||
host_url: str | None = CONTROL_PLANE_URL,
|
||||
name: str | None = "my-app",
|
||||
) -> Result:
|
||||
host_args = ("--host-url", host_url) if host_url else ()
|
||||
name_args = ("--name", name) if name else ()
|
||||
return CliRunner().invoke(
|
||||
cli,
|
||||
[
|
||||
@@ -340,8 +360,7 @@ class DeployProject:
|
||||
"--api-key",
|
||||
"test-key",
|
||||
*host_args,
|
||||
"--name",
|
||||
"my-app",
|
||||
*name_args,
|
||||
"--no-input",
|
||||
"--no-wait",
|
||||
*args,
|
||||
@@ -1281,9 +1300,13 @@ def test_an_invalid_image_pull_secret_name_is_rejected_before_any_docker_work(
|
||||
assert deploy_project.timeline == []
|
||||
|
||||
|
||||
def test_image_pull_secret_cannot_update_an_existing_deployment(
|
||||
def test_image_pull_secret_is_refused_when_the_stored_secrets_are_unknown(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
{"id": "dep-1", "source": "external_docker"}
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--deployment-id",
|
||||
"dep-1",
|
||||
@@ -1296,7 +1319,8 @@ def test_image_pull_secret_cannot_update_an_existing_deployment(
|
||||
assert result.exit_code != 0
|
||||
assert "--image-pull-secret" in result.output
|
||||
assert "dep-1" in result.output
|
||||
assert deploy_project.timeline == []
|
||||
assert deploy_project.docker.verbs() == []
|
||||
assert _patch("dep-1") not in deploy_project.timeline
|
||||
|
||||
|
||||
def test_image_pull_secret_cannot_update_a_deployment_found_by_name(
|
||||
@@ -1332,9 +1356,13 @@ def test_updating_a_deployment_never_looks_up_listeners(
|
||||
assert LIST_LISTENERS not in deploy_project.timeline
|
||||
|
||||
|
||||
def test_listener_flags_are_refused_for_a_deployment_id_without_any_call(
|
||||
def test_listener_flags_are_refused_for_a_deployment_id_with_an_unknown_placement(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
{"id": "dep-ext", "source": "external_docker"}
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--push-to",
|
||||
PUSH_REPOSITORY,
|
||||
@@ -1347,7 +1375,8 @@ def test_listener_flags_are_refused_for_a_deployment_id_without_any_call(
|
||||
|
||||
assert result.exit_code != 0
|
||||
assert "fixed when an Agent Server is created" in result.output
|
||||
assert deploy_project.timeline == []
|
||||
assert deploy_project.docker.verbs() == []
|
||||
assert _patch("dep-ext") not in deploy_project.timeline
|
||||
|
||||
|
||||
def test_listener_flags_are_refused_on_an_existing_deployment(
|
||||
@@ -1371,6 +1400,198 @@ def test_listener_flags_are_refused_on_an_existing_deployment(
|
||||
assert deploy_project.docker.verbs() == []
|
||||
|
||||
|
||||
def test_create_only_flags_equal_to_the_stored_ones_update_the_agent_server(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.listeners = [LISTENER]
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment(STORED_SOURCE_CONFIG)
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--push-to",
|
||||
PUSH_REPOSITORY,
|
||||
"--listener-id",
|
||||
LISTENER_ID,
|
||||
"--k8s-namespace",
|
||||
"agents",
|
||||
"--image-pull-secret",
|
||||
"regcred",
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert _patch("dep-ext") in deploy_project.timeline
|
||||
assert LIST_LISTENERS not in deploy_project.timeline
|
||||
assert f"GET /v2/listeners/{LISTENER_ID}" not in deploy_project.timeline
|
||||
|
||||
|
||||
def test_an_agent_bound_upgrade_accepts_the_stored_listener(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment(STORED_SOURCE_CONFIG)
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--agent-id",
|
||||
"support-agent",
|
||||
"--agent-environment",
|
||||
"staging",
|
||||
"--image-uri",
|
||||
EXTERNAL_DIGEST,
|
||||
"--listener-id",
|
||||
LISTENER_ID,
|
||||
name=None,
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert _patch("dep-ext") in deploy_project.timeline
|
||||
|
||||
|
||||
def test_a_deployment_id_with_the_stored_flags_updates_the_agent_server(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment(STORED_SOURCE_CONFIG)
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--deployment-id",
|
||||
"dep-ext",
|
||||
"--push-to",
|
||||
PUSH_REPOSITORY,
|
||||
"--listener-id",
|
||||
LISTENER_ID,
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert _patch("dep-ext") in deploy_project.timeline
|
||||
|
||||
|
||||
def test_a_namespace_alone_is_compared_only_with_the_stored_namespace(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment(STORED_SOURCE_CONFIG)
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--push-to", PUSH_REPOSITORY, "--k8s-namespace", "agents"
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert _patch("dep-ext") in deploy_project.timeline
|
||||
|
||||
|
||||
def test_image_pull_secrets_match_the_stored_ones_in_any_order(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment(
|
||||
{
|
||||
"resource_spec": {
|
||||
"image_pull_secrets": [{"name": "b-cred"}, {"name": "a-cred"}]
|
||||
}
|
||||
}
|
||||
)
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--push-to",
|
||||
PUSH_REPOSITORY,
|
||||
"--image-pull-secret",
|
||||
"a-cred",
|
||||
"--image-pull-secret",
|
||||
"b-cred",
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert _patch("dep-ext") in deploy_project.timeline
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("flag", "value", "stored"),
|
||||
[
|
||||
pytest.param(
|
||||
"--listener-id", OTHER_LISTENER_ID, LISTENER_ID, id="other_listener"
|
||||
),
|
||||
pytest.param(
|
||||
"--k8s-namespace", "agents-staging", "agents", id="other_namespace"
|
||||
),
|
||||
pytest.param(
|
||||
"--image-pull-secret", "other-cred", "regcred", id="other_pull_secret"
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_a_create_only_flag_that_differs_from_the_stored_value_is_refused(
|
||||
deploy_project: DeployProject, flag: str, value: str, stored: str
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment(STORED_SOURCE_CONFIG)
|
||||
]
|
||||
|
||||
result = deploy_project.run("--push-to", PUSH_REPOSITORY, flag, value)
|
||||
|
||||
assert result.exit_code != 0
|
||||
assert stored in result.output
|
||||
assert deploy_project.docker.verbs() == []
|
||||
assert _patch("dep-ext") not in deploy_project.timeline
|
||||
|
||||
|
||||
def test_a_listener_for_an_agent_server_created_without_one_is_refused(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment({"listener_id": None, "listener_config": None})
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--push-to", PUSH_REPOSITORY, "--listener-id", LISTENER_ID
|
||||
)
|
||||
|
||||
assert result.exit_code != 0
|
||||
assert "created without a listener" in result.output
|
||||
assert _patch("dep-ext") not in deploy_project.timeline
|
||||
|
||||
|
||||
def test_a_namespace_for_an_agent_server_without_a_reported_one_is_refused(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment({"listener_id": LISTENER_ID, "listener_config": {}})
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--push-to",
|
||||
PUSH_REPOSITORY,
|
||||
"--listener-id",
|
||||
LISTENER_ID,
|
||||
"--k8s-namespace",
|
||||
"agents",
|
||||
)
|
||||
|
||||
assert result.exit_code != 0
|
||||
assert "namespace was not reported" in result.output
|
||||
assert "namespace None" not in result.output
|
||||
assert _patch("dep-ext") not in deploy_project.timeline
|
||||
|
||||
|
||||
def test_a_known_listener_alone_updates_an_agent_server_without_a_reported_namespace(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
deploy_project.control_plane.listeners = [LISTENER]
|
||||
deploy_project.control_plane.existing_deployments = [
|
||||
_external_deployment({"listener_id": LISTENER_ID, "listener_config": {}})
|
||||
]
|
||||
|
||||
result = deploy_project.run(
|
||||
"--push-to", PUSH_REPOSITORY, "--listener-id", LISTENER_ID
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert _patch("dep-ext") in deploy_project.timeline
|
||||
|
||||
|
||||
def test_a_deployment_without_a_listener_announces_nothing(
|
||||
deploy_project: DeployProject,
|
||||
) -> None:
|
||||
|
||||
@@ -14,8 +14,6 @@ import pytest
|
||||
import langgraph_cli.deploy as deploy_mod
|
||||
from langgraph_cli.deploy import (
|
||||
BuildAndPush,
|
||||
ById,
|
||||
ByName,
|
||||
CreateOnlySettings,
|
||||
CustomerRegistrySource,
|
||||
DockerBuildCommand,
|
||||
@@ -27,6 +25,7 @@ from langgraph_cli.deploy import (
|
||||
PublishedImage,
|
||||
RemoteBuildSource,
|
||||
RequestedPlacement,
|
||||
StoredPlacement,
|
||||
Unplaced,
|
||||
_call_host_backend_with_optional_tenant,
|
||||
_create_host_backend_client,
|
||||
@@ -816,7 +815,6 @@ class TestSelectSource:
|
||||
"remote_build_flag": None,
|
||||
"placement": RequestedPlacement(),
|
||||
"image_pull_secrets": (),
|
||||
"selector": ByName("my-app"),
|
||||
}
|
||||
REPOSITORY = "registry.example.com/app"
|
||||
|
||||
@@ -1466,48 +1464,125 @@ def test_a_listener_without_an_id_is_refused(resource):
|
||||
Listener.from_resource(resource)
|
||||
|
||||
|
||||
def test_a_deployment_id_with_listener_flags_is_refused_without_probing_docker(
|
||||
monkeypatch,
|
||||
):
|
||||
def explode() -> tuple[bool, str | None]:
|
||||
raise AssertionError("docker must not be probed for an argv-only conflict")
|
||||
class TestExistingDeploymentFromResource:
|
||||
@pytest.mark.parametrize(
|
||||
("source_config", "placement", "image_pull_secrets"),
|
||||
[
|
||||
pytest.param(None, None, None, id="not_reported"),
|
||||
pytest.param({}, None, None, id="empty_source_config"),
|
||||
pytest.param(
|
||||
{
|
||||
"listener_id": "listener-1",
|
||||
"listener_config": {"k8s_namespace": "a"},
|
||||
},
|
||||
StoredPlacement("listener-1", "a"),
|
||||
None,
|
||||
id="listener_and_namespace",
|
||||
),
|
||||
pytest.param(
|
||||
{"listener_id": "listener-1", "listener_config": {}},
|
||||
StoredPlacement("listener-1", None),
|
||||
None,
|
||||
id="listener_without_a_reported_namespace",
|
||||
),
|
||||
pytest.param(
|
||||
{"listener_id": None, "listener_config": None},
|
||||
StoredPlacement(None, None),
|
||||
None,
|
||||
id="created_without_a_listener",
|
||||
),
|
||||
pytest.param({"listener_id": 1}, None, None, id="malformed_listener_id"),
|
||||
pytest.param(
|
||||
{"resource_spec": None}, None, frozenset(), id="no_resource_spec"
|
||||
),
|
||||
pytest.param(
|
||||
{"resource_spec": {"image_pull_secrets": None}},
|
||||
None,
|
||||
frozenset(),
|
||||
id="no_image_pull_secrets",
|
||||
),
|
||||
pytest.param(
|
||||
{
|
||||
"resource_spec": {
|
||||
"image_pull_secrets": [{"name": "b"}, {"name": "a"}]
|
||||
}
|
||||
},
|
||||
None,
|
||||
frozenset({"a", "b"}),
|
||||
id="image_pull_secrets",
|
||||
),
|
||||
pytest.param(
|
||||
{"resource_spec": "unexpected"},
|
||||
None,
|
||||
None,
|
||||
id="malformed_resource_spec",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_the_stored_create_only_values_are_read_or_left_unknown(
|
||||
self, source_config, placement, image_pull_secrets
|
||||
):
|
||||
resource = {"id": "dep-1", "source": "external_docker"}
|
||||
if source_config is not None:
|
||||
resource["source_config"] = source_config
|
||||
|
||||
monkeypatch.setattr(deploy_mod, "can_build_locally", explode)
|
||||
|
||||
with pytest.raises(click.UsageError, match="dep-1"):
|
||||
_select_source(
|
||||
push_to="registry.example.com/app",
|
||||
image=None,
|
||||
image_uri=None,
|
||||
image_name=None,
|
||||
tag=None,
|
||||
remote_build_flag=None,
|
||||
placement=RequestedPlacement(listener_id="listener-1"),
|
||||
image_pull_secrets=(),
|
||||
selector=ById("dep-1"),
|
||||
assert ExistingDeployment.from_resource(
|
||||
"dep-1", resource
|
||||
) == ExistingDeployment(
|
||||
"dep-1", "external_docker", placement, image_pull_secrets
|
||||
)
|
||||
|
||||
|
||||
def test_a_deployment_id_with_image_pull_secrets_is_refused_without_probing_docker(
|
||||
monkeypatch,
|
||||
):
|
||||
def explode() -> tuple[bool, str | None]:
|
||||
raise AssertionError("docker must not be probed for an argv-only conflict")
|
||||
|
||||
monkeypatch.setattr(deploy_mod, "can_build_locally", explode)
|
||||
|
||||
with pytest.raises(click.UsageError, match="dep-1"):
|
||||
_select_source(
|
||||
push_to="registry.example.com/app",
|
||||
image=None,
|
||||
image_uri=None,
|
||||
image_name=None,
|
||||
tag=None,
|
||||
remote_build_flag=None,
|
||||
placement=RequestedPlacement(),
|
||||
image_pull_secrets=("regcred",),
|
||||
selector=ById("dep-1"),
|
||||
)
|
||||
class TestStoredPlacement:
|
||||
@pytest.mark.parametrize(
|
||||
("stored", "requested", "allowed"),
|
||||
[
|
||||
pytest.param(
|
||||
StoredPlacement("listener-1", "a"),
|
||||
RequestedPlacement("listener-1", "a"),
|
||||
True,
|
||||
id="same_listener_and_namespace",
|
||||
),
|
||||
pytest.param(
|
||||
StoredPlacement("listener-1", "a"),
|
||||
RequestedPlacement(k8s_namespace="a"),
|
||||
True,
|
||||
id="same_namespace_alone",
|
||||
),
|
||||
pytest.param(
|
||||
StoredPlacement("listener-1", "a"),
|
||||
RequestedPlacement("listener-2"),
|
||||
False,
|
||||
id="other_listener",
|
||||
),
|
||||
pytest.param(
|
||||
StoredPlacement("listener-1", "a"),
|
||||
RequestedPlacement("listener-1", "b"),
|
||||
False,
|
||||
id="other_namespace",
|
||||
),
|
||||
pytest.param(
|
||||
StoredPlacement(None, None),
|
||||
RequestedPlacement("listener-1"),
|
||||
False,
|
||||
id="created_without_a_listener",
|
||||
),
|
||||
pytest.param(
|
||||
StoredPlacement("listener-1", None),
|
||||
RequestedPlacement("listener-1"),
|
||||
True,
|
||||
id="known_listener_with_an_unreported_namespace",
|
||||
),
|
||||
pytest.param(
|
||||
StoredPlacement("listener-1", None),
|
||||
RequestedPlacement("listener-1", "a"),
|
||||
False,
|
||||
id="namespace_that_was_not_reported",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_only_the_passed_values_must_match(self, stored, requested, allowed):
|
||||
assert stored.allows(requested) is allowed
|
||||
|
||||
|
||||
class TestResourceSpec:
|
||||
@@ -1540,18 +1615,55 @@ class TestCreateOnlySettings:
|
||||
def test_a_valid_secret_name_is_accepted(self):
|
||||
CreateOnlySettings(RequestedPlacement(), ("regcred-1.example",))
|
||||
|
||||
def test_ensure_not_existing_mentions_the_api_and_ui_for_pull_secrets(self):
|
||||
def test_unknown_pull_secrets_point_to_the_api_and_ui(self):
|
||||
settings = CreateOnlySettings(RequestedPlacement(), ("regcred",))
|
||||
with pytest.raises(click.UsageError, match="API or the LangSmith UI"):
|
||||
settings.ensure_not_existing("dep-1")
|
||||
settings.ensure_unchanged(ExistingDeployment("dep-1", "external_docker"))
|
||||
|
||||
def test_ensure_not_existing_keeps_the_placement_message_unchanged(self):
|
||||
def test_an_unknown_placement_keeps_the_original_message(self):
|
||||
settings = CreateOnlySettings(RequestedPlacement(listener_id="listener-1"))
|
||||
with pytest.raises(
|
||||
click.UsageError,
|
||||
match="Listener and namespace are fixed when an Agent Server is created",
|
||||
match=(
|
||||
"Listener and namespace are fixed when an Agent Server is created. "
|
||||
"Agent Server dep-1 already exists, so drop --listener-id and "
|
||||
"--k8s-namespace, or create a new Agent Server with a different --name."
|
||||
),
|
||||
):
|
||||
settings.ensure_not_existing("dep-1")
|
||||
settings.ensure_unchanged(ExistingDeployment("dep-1", "external_docker"))
|
||||
|
||||
def test_an_unreported_namespace_names_the_listener_and_asks_to_drop_the_namespace(
|
||||
self,
|
||||
):
|
||||
settings = CreateOnlySettings(RequestedPlacement("listener-1", "a"))
|
||||
with pytest.raises(
|
||||
click.UsageError,
|
||||
match=(
|
||||
"Agent Server dep-1 runs on listener listener-1, but its namespace "
|
||||
"was not reported, so pass --listener-id listener-1 without "
|
||||
"--k8s-namespace, or drop both flags, or create a new Agent Server "
|
||||
"with a different --name."
|
||||
),
|
||||
):
|
||||
settings.ensure_unchanged(
|
||||
ExistingDeployment(
|
||||
"dep-1", "external_docker", StoredPlacement("listener-1", None)
|
||||
)
|
||||
)
|
||||
|
||||
def test_equal_values_pass_without_a_refusal(self):
|
||||
settings = CreateOnlySettings(
|
||||
RequestedPlacement("listener-1", "a"), ("regcred",)
|
||||
)
|
||||
|
||||
settings.ensure_unchanged(
|
||||
ExistingDeployment(
|
||||
"dep-1",
|
||||
"external_docker",
|
||||
StoredPlacement("listener-1", "a"),
|
||||
frozenset({"regcred"}),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class TestPlacementOnAKnownListener:
|
||||
|
||||
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph"
|
||||
version = "1.2.24"
|
||||
version = "1.2.25"
|
||||
description = "Building stateful, multi-actor applications with LLMs"
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
|
||||
@@ -2,34 +2,12 @@ import re
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
|
||||
from typing_extensions import Self
|
||||
|
||||
|
||||
class AnyObject:
|
||||
def __eq__(self, value):
|
||||
return True
|
||||
|
||||
|
||||
class FloatBetween(float):
|
||||
def __new__(cls, min_value: float, max_value: float) -> Self:
|
||||
return super().__new__(cls, min_value)
|
||||
|
||||
def __init__(self, min_value: float, max_value: float) -> None:
|
||||
super().__init__()
|
||||
self.min_value = min_value
|
||||
self.max_value = max_value
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
return (
|
||||
isinstance(other, float)
|
||||
and other >= self.min_value
|
||||
and other <= self.max_value
|
||||
)
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash((float(self), self.min_value, self.max_value))
|
||||
|
||||
|
||||
class AnyStr(str):
|
||||
def __init__(self, prefix: str | re.Pattern = "") -> None:
|
||||
super().__init__()
|
||||
|
||||
@@ -88,7 +88,7 @@ from langgraph.types import (
|
||||
interrupt,
|
||||
)
|
||||
from tests.agents import AgentAction, AgentFinish
|
||||
from tests.any_str import AnyStr, AnyVersion, FloatBetween, UnsortedSequence
|
||||
from tests.any_str import AnyStr, AnyVersion, UnsortedSequence
|
||||
from tests.messages import (
|
||||
_AnyIdAIMessage,
|
||||
_AnyIdAIMessageChunk,
|
||||
@@ -3385,6 +3385,12 @@ def test_subgraph_checkpoint_true_interrupt(
|
||||
def test_stream_subgraphs_during_execution(
|
||||
sync_checkpointer: BaseCheckpointSaver,
|
||||
) -> None:
|
||||
streamed = {"inner_1": threading.Event(), "outer_1": threading.Event()}
|
||||
|
||||
def wait_until_streamed(node: str) -> None:
|
||||
if not streamed[node].wait(timeout=10):
|
||||
raise TimeoutError(f"{node} was not streamed while the graph was running")
|
||||
|
||||
class InnerState(TypedDict):
|
||||
my_key: Annotated[str, operator.add]
|
||||
my_other_key: str
|
||||
@@ -3393,7 +3399,7 @@ def test_stream_subgraphs_during_execution(
|
||||
return {"my_key": "got here", "my_other_key": state["my_key"]}
|
||||
|
||||
def inner_2(state: InnerState):
|
||||
time.sleep(0.5)
|
||||
wait_until_streamed("outer_1")
|
||||
return {
|
||||
"my_key": " and there",
|
||||
"my_other_key": state["my_key"],
|
||||
@@ -3410,7 +3416,7 @@ def test_stream_subgraphs_during_execution(
|
||||
my_key: Annotated[str, operator.add]
|
||||
|
||||
def outer_1(state: State):
|
||||
time.sleep(0.2)
|
||||
wait_until_streamed("inner_1")
|
||||
return {"my_key": " and parallel"}
|
||||
|
||||
def outer_2(state: State):
|
||||
@@ -3428,34 +3434,22 @@ def test_stream_subgraphs_during_execution(
|
||||
|
||||
app = graph.compile(checkpointer=sync_checkpointer)
|
||||
|
||||
start = time.perf_counter()
|
||||
chunks: list[tuple[float, Any]] = []
|
||||
chunks: list[tuple[tuple[str, ...], dict[str, dict[str, str]]]] = []
|
||||
config = {"configurable": {"thread_id": "2"}}
|
||||
for c in app.stream({"my_key": ""}, config, subgraphs=True):
|
||||
chunks.append((round(time.perf_counter() - start, 1), c))
|
||||
for idx in range(len(chunks)):
|
||||
elapsed, c = chunks[idx]
|
||||
chunks[idx] = (round(elapsed - chunks[0][0], 1), c)
|
||||
for namespace, update in app.stream({"my_key": ""}, config, subgraphs=True):
|
||||
chunks.append((namespace, update))
|
||||
for node in update.keys() & streamed.keys():
|
||||
streamed[node].set()
|
||||
|
||||
assert chunks == [
|
||||
# arrives before "inner" finishes
|
||||
((AnyStr("inner:"),), {"inner_1": {"my_key": "got here", "my_other_key": ""}}),
|
||||
((), {"outer_1": {"my_key": " and parallel"}}),
|
||||
(
|
||||
FloatBetween(0.0, 0.1),
|
||||
(
|
||||
(AnyStr("inner:"),),
|
||||
{"inner_1": {"my_key": "got here", "my_other_key": ""}},
|
||||
),
|
||||
(AnyStr("inner:"),),
|
||||
{"inner_2": {"my_key": " and there", "my_other_key": "got here"}},
|
||||
),
|
||||
(FloatBetween(0.2, 0.3), ((), {"outer_1": {"my_key": " and parallel"}})),
|
||||
(
|
||||
FloatBetween(0.5, 0.8),
|
||||
(
|
||||
(AnyStr("inner:"),),
|
||||
{"inner_2": {"my_key": " and there", "my_other_key": "got here"}},
|
||||
),
|
||||
),
|
||||
(FloatBetween(0.5, 0.8), ((), {"inner": {"my_key": "got here and there"}})),
|
||||
(FloatBetween(0.5, 0.8), ((), {"outer_2": {"my_key": " and back again"}})),
|
||||
((), {"inner": {"my_key": "got here and there"}}),
|
||||
((), {"outer_2": {"my_key": " and back again"}}),
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ from langgraph.types import (
|
||||
StreamWriter,
|
||||
interrupt,
|
||||
)
|
||||
from tests.any_str import AnyStr, AnyVersion, FloatBetween, UnsortedSequence
|
||||
from tests.any_str import AnyStr, AnyVersion, UnsortedSequence
|
||||
from tests.fake_tracer import FakeTracer
|
||||
from tests.memory_assert import MemorySaverNoPending
|
||||
from tests.messages import (
|
||||
@@ -5141,6 +5141,11 @@ async def test_subgraph_checkpoint_true_interrupt(
|
||||
async def test_stream_subgraphs_during_execution(
|
||||
async_checkpointer: BaseCheckpointSaver,
|
||||
) -> None:
|
||||
streamed = {"inner_1": asyncio.Event(), "outer_1": asyncio.Event()}
|
||||
|
||||
async def wait_until_streamed(node: str) -> None:
|
||||
await asyncio.wait_for(streamed[node].wait(), timeout=10)
|
||||
|
||||
class InnerState(TypedDict):
|
||||
my_key: Annotated[str, operator.add]
|
||||
my_other_key: str
|
||||
@@ -5149,7 +5154,7 @@ async def test_stream_subgraphs_during_execution(
|
||||
return {"my_key": "got here", "my_other_key": state["my_key"]}
|
||||
|
||||
async def inner_2(state: InnerState):
|
||||
await asyncio.sleep(0.5)
|
||||
await wait_until_streamed("outer_1")
|
||||
return {
|
||||
"my_key": " and there",
|
||||
"my_other_key": state["my_key"],
|
||||
@@ -5166,7 +5171,7 @@ async def test_stream_subgraphs_during_execution(
|
||||
my_key: Annotated[str, operator.add]
|
||||
|
||||
async def outer_1(state: State):
|
||||
await asyncio.sleep(0.2)
|
||||
await wait_until_streamed("inner_1")
|
||||
return {"my_key": " and parallel"}
|
||||
|
||||
async def outer_2(state: State):
|
||||
@@ -5184,34 +5189,22 @@ async def test_stream_subgraphs_during_execution(
|
||||
|
||||
app = graph.compile(checkpointer=async_checkpointer)
|
||||
|
||||
start = perf_counter()
|
||||
chunks: list[tuple[float, Any]] = []
|
||||
chunks: list[tuple[tuple[str, ...], dict[str, dict[str, str]]]] = []
|
||||
config = {"configurable": {"thread_id": "2"}}
|
||||
async for c in app.astream({"my_key": ""}, config, subgraphs=True):
|
||||
chunks.append((round(perf_counter() - start, 1), c))
|
||||
for idx in range(len(chunks)):
|
||||
elapsed, c = chunks[idx]
|
||||
chunks[idx] = (round(elapsed - chunks[0][0], 1), c)
|
||||
async for namespace, update in app.astream({"my_key": ""}, config, subgraphs=True):
|
||||
chunks.append((namespace, update))
|
||||
for node in update.keys() & streamed.keys():
|
||||
streamed[node].set()
|
||||
|
||||
assert chunks == [
|
||||
# arrives before "inner" finishes
|
||||
((AnyStr("inner:"),), {"inner_1": {"my_key": "got here", "my_other_key": ""}}),
|
||||
((), {"outer_1": {"my_key": " and parallel"}}),
|
||||
(
|
||||
FloatBetween(0.0, 0.1),
|
||||
(
|
||||
(AnyStr("inner:"),),
|
||||
{"inner_1": {"my_key": "got here", "my_other_key": ""}},
|
||||
),
|
||||
(AnyStr("inner:"),),
|
||||
{"inner_2": {"my_key": " and there", "my_other_key": "got here"}},
|
||||
),
|
||||
(FloatBetween(0.2, 0.4), ((), {"outer_1": {"my_key": " and parallel"}})),
|
||||
(
|
||||
FloatBetween(0.5, 0.8),
|
||||
(
|
||||
(AnyStr("inner:"),),
|
||||
{"inner_2": {"my_key": " and there", "my_other_key": "got here"}},
|
||||
),
|
||||
),
|
||||
(FloatBetween(0.5, 0.8), ((), {"inner": {"my_key": "got here and there"}})),
|
||||
(FloatBetween(0.5, 0.8), ((), {"outer_2": {"my_key": " and back again"}})),
|
||||
((), {"inner": {"my_key": "got here and there"}}),
|
||||
((), {"outer_2": {"my_key": " and back again"}}),
|
||||
]
|
||||
|
||||
|
||||
@@ -5222,10 +5215,12 @@ async def test_stream_buffering_single_node(
|
||||
class State(TypedDict):
|
||||
my_key: Annotated[str, operator.add]
|
||||
|
||||
received = asyncio.Event()
|
||||
|
||||
async def node(state: State, writer: StreamWriter):
|
||||
writer("Before sleep")
|
||||
await asyncio.sleep(0.2)
|
||||
writer("After sleep")
|
||||
writer("Before wait")
|
||||
await asyncio.wait_for(received.wait(), timeout=10)
|
||||
writer("After wait")
|
||||
return {"my_key": "got here"}
|
||||
|
||||
builder = StateGraph(State)
|
||||
@@ -5235,16 +5230,13 @@ async def test_stream_buffering_single_node(
|
||||
|
||||
graph = builder.compile(checkpointer=async_checkpointer)
|
||||
|
||||
start = perf_counter()
|
||||
chunks: list[tuple[float, Any]] = []
|
||||
config = {"configurable": {"thread_id": "2"}}
|
||||
async for c in graph.astream({"my_key": ""}, config, stream_mode="custom"):
|
||||
chunks.append((round(perf_counter() - start, 1), c))
|
||||
chunks: list[str] = []
|
||||
async for chunk in graph.astream({"my_key": ""}, config, stream_mode="custom"):
|
||||
chunks.append(chunk)
|
||||
received.set()
|
||||
|
||||
assert chunks == [
|
||||
(FloatBetween(0.0, 0.1), "Before sleep"),
|
||||
(FloatBetween(0.2, 0.3), "After sleep"),
|
||||
]
|
||||
assert chunks == ["Before wait", "After wait"]
|
||||
|
||||
|
||||
async def test_nested_graph_interrupts_parallel(
|
||||
|
||||
Generated
+1
-1
@@ -1537,7 +1537,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.24"
|
||||
version = "1.2.25"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
Generated
+1
-1
@@ -381,7 +381,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.24"
|
||||
version = "1.2.25"
|
||||
source = { editable = "../langgraph" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
Generated
+1
-1
@@ -394,7 +394,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "1.2.24"
|
||||
version = "1.2.25"
|
||||
source = { editable = "../langgraph" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
|
||||
Reference in new issue
Block a user