From 9df387ec6e22ec0edf120635a0c0d4021e8bdc4a Mon Sep 17 00:00:00 2001 From: kfiramar <83420275+kfiramar@users.noreply.github.com> Date: Sat, 11 Jul 2026 00:40:43 +0300 Subject: [PATCH 1/6] Add Docker-backed application runtime --- contracts/python_runtime_gap_report.json | 6 +- contracts/runtime_feature_matrix.json | 3 +- docs/reference/configuration.md | 59 ++ docs/reference/important-flags.md | 10 + docs/user/common-workflows.md | 27 + .../planning/worktree_setup_coordinator.py | 3 - python/envctl_engine/runtime/cli.py | 2 + .../runtime/docker_service_runtime.py | 666 ++++++++++++++++++ .../runtime/engine_runtime_artifacts.py | 22 +- .../runtime/engine_runtime_commands.py | 7 +- .../runtime/engine_runtime_hooks.py | 4 + python/envctl_engine/runtime/help_general.py | 1 + .../runtime/help_topic_lifecycle.py | 2 + .../runtime/lifecycle_blast_docker.py | 5 +- .../runtime/lifecycle_cleanup_orchestrator.py | 16 + .../runtime/lifecycle_dependency_stop.py | 50 ++ .../runtime/lifecycle_service_termination.py | 24 + .../runtime/lifecycle_worktree_containers.py | 15 + .../runtime/service_status_truth.py | 27 + .../runtime/startup_inspection_support.py | 3 + .../startup/finalization_run_state.py | 6 + .../startup/restart_prestop_support.py | 4 + .../startup/run_reuse_decision.py | 29 +- .../startup/run_reuse_fresh_start.py | 64 +- .../startup/run_reuse_identity.py | 10 +- .../envctl_engine/startup/run_reuse_resume.py | 4 + .../startup/service_attach_execution.py | 177 ++++- .../startup/service_project_execution.py | 46 +- python/envctl_engine/state/__init__.py | 8 + .../state/action_command_support.py | 57 +- .../state/action_health_support.py | 4 + .../envctl_engine/state/action_log_support.py | 2 + .../state/action_orchestrator.py | 19 +- python/envctl_engine/state/models.py | 4 + python/envctl_engine/state/runtime_map.py | 4 + .../runtime/test_docker_service_runtime.py | 307 ++++++++ .../runtime/test_engine_runtime_artifacts.py | 35 +- .../runtime/test_engine_runtime_commands.py | 52 ++ .../test_engine_runtime_startup_support.py | 35 + .../runtime/test_lifecycle_dependency_stop.py | 42 ++ .../test_lifecycle_worktree_cleanup.py | 37 + tests/python/runtime/test_prereq_policy.py | 31 + .../startup/test_restart_prestop_support.py | 21 + .../python/startup/test_run_reuse_identity.py | 16 + .../python/startup/test_run_reuse_support.py | 92 +++ .../startup/test_service_attach_execution.py | 73 ++ .../test_state_action_orchestrator_logs.py | 46 ++ tests/python/state/test_state_roundtrip.py | 14 + 48 files changed, 2099 insertions(+), 92 deletions(-) create mode 100644 python/envctl_engine/runtime/docker_service_runtime.py create mode 100644 tests/python/runtime/test_docker_service_runtime.py diff --git a/contracts/python_runtime_gap_report.json b/contracts/python_runtime_gap_report.json index b135b3f8e..8c7270b80 100644 --- a/contracts/python_runtime_gap_report.json +++ b/contracts/python_runtime_gap_report.json @@ -1,8 +1,8 @@ { "version": 1, - "generated_at": "2026-07-06T17:49:55+00:00", - "matrix_generated_at": "2026-07-06T17:49:55+00:00", - "matrix_sha256": "19b3bb085a8392aa9e6f99856983555c06da3606d5e81a6d1f2be7d20022899d", + "generated_at": "2026-07-10T21:33:31+00:00", + "matrix_generated_at": "2026-07-10T21:30:49+00:00", + "matrix_sha256": "9968ef658488512f15c0ebda73d08c82da359ab389b1ff880b585dd47d5bb935", "summary": { "feature_count": 59, "gap_count": 0, diff --git a/contracts/runtime_feature_matrix.json b/contracts/runtime_feature_matrix.json index a29441403..44926b96c 100644 --- a/contracts/runtime_feature_matrix.json +++ b/contracts/runtime_feature_matrix.json @@ -1,6 +1,6 @@ { "version": 1, - "generated_at": "2026-07-06T17:49:55+00:00", + "generated_at": "2026-07-10T21:30:49+00:00", "inventory_sources": { "python_supported_commands": [ "blast-all", @@ -327,6 +327,7 @@ "--deps", "--deps-parallel", "--deps-sequential", + "--docker", "--doctor", "--entire-system", "--failed", diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index 2e6bb6613..aff9e7939 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md @@ -365,6 +365,65 @@ The wizard saves accepted backend/frontend test suggestions to `ENVCTL_BACKEND_T | `ENVCTL_WORKTREE_CGC_DATABASE` | `kuzudb` | Backend passed as `--database ` to `cgc context create` for generated worktree contexts. | ## Execution Policy + +### Docker application runtime + +`envctl ... --docker` preserves normal project selection, port allocation, dependency startup, +service ordering, state, dashboard, logs, health, restart, and stop behavior, but launches every +selected app service in a managed container instead of as a host process. Managed dependencies such +as Postgres, Redis, Supabase, and n8n continue to use their existing Docker adapters. + +Settings resolve service-specific keys before global fallbacks. Additional service names are +uppercased and non-alphanumeric characters become underscores, so `voice-runtime` accepts the +standard `ENVCTL_SERVICE_VOICE_RUNTIME_*` prefix (and the shorter `ENVCTL_VOICE_RUNTIME_*` alias). + +| Variable | Default | Purpose | +| --- | --- | --- | +| `ENVCTL__DOCKER_IMAGE` / `ENVCTL_DOCKER_IMAGE` | unset | Existing image to run. Without a Dockerfile setting, no build runs. | +| `ENVCTL__DOCKERFILE` / `ENVCTL_DOCKERFILE` | discovered | Dockerfile to build; discovery checks the service directory and then project root. | +| `ENVCTL__DOCKER_CONTEXT` / `ENVCTL_DOCKER_CONTEXT` | Dockerfile directory | Build context relative to the project root. | +| `ENVCTL__DOCKER_TARGET` / `ENVCTL_DOCKER_TARGET` | unset | Optional multi-stage build target. | +| `ENVCTL__DOCKER_BUILD_ARGS` / `ENVCTL_DOCKER_BUILD_ARGS` | public frontend env keys | Comma-separated build-arg names or `NAME=value` entries. Named values come from the service env and are not placed on the Docker command line. | +| `ENVCTL__DOCKER_COMMAND` / `ENVCTL_DOCKER_COMMAND` | image command | Explicit command executed inside the container. | +| `ENVCTL__DOCKER_COMMAND_MODE` / `ENVCTL_DOCKER_COMMAND_MODE` | `image` | `image` keeps the image CMD/ENTRYPOINT; `service` translates and runs envctl's normal service command. | +| `ENVCTL__DOCKER_WORKDIR` / `ENVCTL_DOCKER_WORKDIR` | image default | Container working directory. | +| `ENVCTL__DOCKER_PORT` / `ENVCTL_DOCKER_PORT` | single exposed image port or allocated host port | Container-side listener port. | +| `ENVCTL__DOCKER_BUILD_POLICY` / `ENVCTL_DOCKER_BUILD_POLICY` | `cached` | `cached`, `missing`, or `never`. | +| `ENVCTL_DOCKER_BUILD_TIMEOUT_SECONDS` | `900` | Image build timeout. | +| `ENVCTL_DOCKER_RUN_TIMEOUT_SECONDS` | `120` | Container creation timeout. | + +The default `cached` policy invokes `docker build` with BuildKit enabled on every start. Docker +evaluates the context and reuses unchanged layers, while changed source cannot accidentally reuse a +stale whole image. Generated tags are stable for one checkout/worktree, preventing parallel trees +from racing on one tag. Use `missing` for immutable/prebuilt local tags and `never` when image +lifecycle is owned outside envctl. + +Containers publish only to `127.0.0.1`, carry `io.envctl.*` ownership labels, and persist their ID, +name, and image in runtime state. Internal URLs and host settings pointing at `localhost` map to +`host.docker.internal`; browser-facing `VITE_*`, `NEXT_PUBLIC_*`, `PUBLIC_*`, CORS, and frontend URL +values remain host-facing. Service env passes through a short-lived mode-`0600` file and is not +rendered into Docker command diagnostics. Normal logs are snapshotted from Docker on demand; +`logs --follow` creates followers only for that command and tears them down on exit, so application +runtime state does not depend on long-lived host helper processes. + +```dotenv +ENVCTL_BACKEND_DOCKER_IMAGE=acme/api-dev:latest +ENVCTL_BACKEND_DOCKER_COMMAND=uvicorn app.main:app --host 0.0.0.0 --port 8000 +ENVCTL_BACKEND_DOCKER_PORT=8000 + +ENVCTL_FRONTEND_DOCKERFILE=frontend/Dockerfile +ENVCTL_FRONTEND_DOCKER_CONTEXT=frontend +ENVCTL_FRONTEND_DOCKER_PORT=5173 +``` + +```bash +envctl start --trees --entire-system --docker --headless +envctl health --json +envctl logs --project feature-a-1 --service backend +envctl restart --project feature-a-1 --service backend --docker --headless +envctl stop --project feature-a-1 --entire-system +``` + | Variable | Default | Purpose | | --- | --- | --- | | `ENVCTL_SERVICE_ATTACH_PARALLEL` | `true` | Run backend+frontend service attach in parallel when both are selected. | diff --git a/docs/reference/important-flags.md b/docs/reference/important-flags.md index 4441f34f9..0a5f853ed 100644 --- a/docs/reference/important-flags.md +++ b/docs/reference/important-flags.md @@ -89,12 +89,22 @@ Plan-agent handoff note: if `--plan --tmux/--omx --headless` starts the implemen | `--parallel-trees` | Enable parallel tree startup workers. | | `--parallel-trees-max ` | Max parallel tree startup workers. | | `--service-parallel` / `--service-sequential` | Run backend+frontend startup attach in parallel or sequential mode (default: parallel). | +| `--docker` | Build or reuse images and run backend, frontend, and additional app services as managed Docker containers. | | `--parallel` / `--sequential` | For `envctl test`, run backend/frontend test suites in parallel or sequential mode (default: parallel for multiple suites, with sequential fallback). Legacy aliases: `--test-parallel` / `--test-sequential`. | | `--test-parallel-max ` | Cap concurrent test suites in parallel mode and pytest-xdist workers when pytest-xdist is explicitly enabled (default suite cap: CPU-aware, max `4`; default pytest workers: current free CPU cores). | | `--clear-port-state` | Clear saved port reservations/state. | | `--force` | Free configured ports if needed. | | `ENVCTL_DOCKER_PREWARM=0\|1` | Enable/disable one-shot Docker daemon prewarm before requirements startup (default: `1`). | | `ENVCTL_DOCKER_PREWARM_TIMEOUT_SECONDS=` | Timeout for Docker prewarm command (default: `10`). | +| `ENVCTL__DOCKER_IMAGE=` | Use an existing image for one app service. `ENVCTL_DOCKER_IMAGE` is the global fallback. | +| `ENVCTL__DOCKERFILE=` | Build an app service image from a Dockerfile. `ENVCTL_DOCKERFILE` is the global fallback. | +| `ENVCTL__DOCKER_CONTEXT=` | Override the Docker build context; defaults to the Dockerfile directory. | +| `ENVCTL__DOCKER_BUILD_ARGS=` | Add comma-separated Docker build-arg names (or `NAME=value` entries); public frontend env keys are passed automatically. | +| `ENVCTL__DOCKER_COMMAND=` | Override the image command inside the container. | +| `ENVCTL__DOCKER_COMMAND_MODE=image\|service` | Keep image CMD/ENTRYPOINT (default) or run envctl's normal service command. | +| `ENVCTL__DOCKER_PORT=` | Override the container-side port while preserving envctl's allocated host port. | +| `ENVCTL__DOCKER_WORKDIR=` | Override the image working directory for the service command. | +| `ENVCTL__DOCKER_BUILD_POLICY=cached\|missing\|never` | `cached` (default) reuses Docker layers; `missing` builds only when absent; `never` requires an existing image. | | `ENVCTL_REQUIREMENTS_PORT_MISMATCH_POLICY=adopt_existing\|recreate` | On existing container port mismatch, adopt existing mapping (default) or force recreate. | ## Logs and Debugging diff --git a/docs/user/common-workflows.md b/docs/user/common-workflows.md index f9de37747..0b478e40e 100644 --- a/docs/user/common-workflows.md +++ b/docs/user/common-workflows.md @@ -212,6 +212,33 @@ This is especially useful: - before CI or agent automation - when debugging resume surprises +## Run the Same System in Docker + +Use `--docker` to preserve envctl's main/trees, selective-service, dependency, dashboard, health, +logs, restart, and stop workflows while replacing app host processes with managed containers: + +```bash +envctl start --main --entire-system --docker --headless +envctl start --trees --entire-system --isolated-deps --docker --headless +``` + +If a service directory contains a `Dockerfile`, envctl builds it with Docker's layer cache. Otherwise, +configure an existing image with `ENVCTL_BACKEND_DOCKER_IMAGE`, `ENVCTL_FRONTEND_DOCKER_IMAGE`, or +the corresponding prefix for an additional app service. Production-oriented images commonly need +`ENVCTL__DOCKER_COMMAND` and `ENVCTL__DOCKER_PORT` so the service listens on +`0.0.0.0` at the expected container port. + +```bash +envctl endpoints --json +envctl health --json +envctl logs --project Main --service backend +envctl restart --project Main --service backend --docker --headless +envctl stop --project Main --entire-system +``` + +See [Configuration: Docker application runtime](../reference/configuration.md#docker-application-runtime) +for image, Dockerfile, command, port, workdir, target, and cache-policy settings. + ## Debug a Bad Interactive Session Use this when the issue is in the dashboard, selector, spinner, or input flow. diff --git a/python/envctl_engine/planning/worktree_setup_coordinator.py b/python/envctl_engine/planning/worktree_setup_coordinator.py index 21bc724fb..92d2292f2 100644 --- a/python/envctl_engine/planning/worktree_setup_coordinator.py +++ b/python/envctl_engine/planning/worktree_setup_coordinator.py @@ -28,9 +28,6 @@ def apply_setup_worktree_selection( ) -> list[ProjectContextLike]: if not setup_worktree_requested(route): return project_contexts - if bool(route.flags.get("docker")): - raise RuntimeError("setup-worktrees is not supported in Docker mode.") - multi_entries = coerce_setup_entries( route=route, flag_name="setup_worktrees", diff --git a/python/envctl_engine/runtime/cli.py b/python/envctl_engine/runtime/cli.py index 9ee0d43af..92831ea00 100644 --- a/python/envctl_engine/runtime/cli.py +++ b/python/envctl_engine/runtime/cli.py @@ -80,6 +80,8 @@ def _effective_prereq_mode(route: Route) -> str: def _requires_docker(mode: str, config: EngineConfig, *, route: Route | None = None) -> bool: + if route is not None and bool(route.flags.get("docker")): + return True if route is not None and route.flags.get("launch_dependencies") is False: return False if effective_dependency_scope(route, mode) == "shared" and mode == "trees": diff --git a/python/envctl_engine/runtime/docker_service_runtime.py b/python/envctl_engine/runtime/docker_service_runtime.py new file mode 100644 index 000000000..bc14c3542 --- /dev/null +++ b/python/envctl_engine/runtime/docker_service_runtime.py @@ -0,0 +1,666 @@ +from __future__ import annotations + +import hashlib +import json +import os +import re +import shlex +import threading +from dataclasses import dataclass +from pathlib import Path +from typing import Any, Mapping, Sequence + +from envctl_engine.shared.parsing import parse_bool + + +_HOST_ENV_KEYS = { + "HOME", + "OLDPWD", + "PATH", + "PWD", + "PYTHONHOME", + "PYTHONPATH", + "SHELL", + "SHLVL", + "TMPDIR", + "VIRTUAL_ENV", + "VIRTUAL_ENV_PROMPT", + "ZDOTDIR", + "_", +} +_PUBLIC_ENV_PREFIXES = ("VITE_", "NEXT_PUBLIC_", "PUBLIC_") +_PUBLIC_ENV_KEYS = { + "BACKEND_CORS_ORIGINS", + "CORS_ORIGINS", + "FRONTEND_URL", + "PUBLIC_URL", +} +_DOCKER_INFO_LOCK = threading.Lock() +_RUNTIME_SCOPE_LABEL = "io.envctl.runtime-scope" + + +def _docker_name(value: str, *, limit: int) -> str: + normalized = re.sub(r"[^a-z0-9_.-]+", "-", value.strip().lower()).strip("-._") + return (normalized or "service")[:limit] + + +def docker_service_container_name(*, project_name: str, project_root: Path, service_name: str) -> str: + digest = hashlib.sha256(str(project_root.resolve()).encode()).hexdigest()[:10] + prefix = _docker_name(f"envctl-app-{project_name}-{service_name}", limit=52) + return f"{prefix}-{digest}" + + +@dataclass(frozen=True, slots=True) +class DockerServiceLaunch: + container_id: str + container_name: str + image: str + log_path: str + + +def docker_service_mode_enabled(runtime: Any) -> bool: + env = getattr(runtime, "env", {}) + return parse_bool(str(env.get("DOCKER_MODE", "")), False) + + +def docker_service_uses_image_command( + runtime: Any, + service_name: str, + *, + docker_mode: bool | None = None, +) -> bool: + if not (docker_service_mode_enabled(runtime) if docker_mode is None else docker_mode): + return False + env = getattr(runtime, "env", {}) + raw = getattr(getattr(runtime, "config", None), "raw", {}) + suffix = re.sub(r"[^A-Z0-9]+", "_", service_name.upper()).strip("_") or "SERVICE" + + def setting(name: str) -> str: + for key in (f"ENVCTL_SERVICE_{suffix}_{name}", f"ENVCTL_{suffix}_{name}", f"ENVCTL_{name}"): + value = str(env.get(key) or raw.get(key) or "").strip() + if value: + return value + return "" + + if setting("DOCKER_COMMAND"): + return False + return (setting("DOCKER_COMMAND_MODE") or "image").lower() in {"image", "default"} + + +def state_uses_docker_services(state: object) -> bool: + metadata = getattr(state, "metadata", {}) + if isinstance(metadata, dict) and str(metadata.get("application_runtime", "")).lower() == "docker": + return True + services = getattr(state, "services", {}) + return isinstance(services, dict) and any( + str(getattr(service, "runtime_kind", "process") or "process").lower() == "docker" + for service in services.values() + ) + + +class DockerServiceRuntime: + """Build and run application services as managed Docker containers.""" + + def __init__(self, runtime: Any, process_runtime: Any, *, refresh_cache: bool = False) -> None: + self.runtime = runtime + self.process_runtime = process_runtime + self.refresh_cache = refresh_cache + + def launch( + self, + *, + project_name: str, + project_root: Path, + service_name: str, + cwd: Path, + command: Sequence[str], + env: Mapping[str, str], + host_port: int, + container_port: int, + listener_expected: bool, + log_path: str, + ) -> DockerServiceLaunch: + self._require_docker() + image = self._resolve_image( + project_root=project_root, + service_name=service_name, + cwd=cwd, + build_env=env, + ) + container_name = self._container_name( + project_name=project_name, + project_root=project_root, + service_name=service_name, + ) + self._prepare_container_name(container_name) + effective_container_port = self._container_port(service_name, container_port, image=image) + container_env = self._container_env(env) + if listener_expected and effective_container_port > 0: + container_env["PORT"] = str(effective_container_port) + env_file = self._write_env_file( + project_name=project_name, + service_name=service_name, + values=container_env, + ) + try: + docker_command = [ + "docker", + "run", + "--detach", + "--name", + container_name, + "--label", + "io.envctl.managed=true", + "--label", + f"io.envctl.project={project_name}", + "--label", + f"io.envctl.service={service_name}", + "--label", + f"{_RUNTIME_SCOPE_LABEL}={self._runtime_scope_identity()}", + "--add-host", + "host.docker.internal:host-gateway", + "--env-file", + str(env_file), + ] + if listener_expected and host_port > 0: + docker_command.extend(["--publish", f"127.0.0.1:{host_port}:{effective_container_port}"]) + workdir = self._setting(service_name, "DOCKER_WORKDIR") + if workdir: + docker_command.extend(["--workdir", workdir]) + docker_command.append(image) + docker_command.extend( + self._container_command( + service_name, + command, + project_root=project_root, + cwd=cwd, + host_port=host_port, + container_port=effective_container_port, + ) + ) + result = self.process_runtime.run( + docker_command, + cwd=project_root, + env={**os.environ, "DOCKER_BUILDKIT": "1"}, + timeout=self._timeout("ENVCTL_DOCKER_RUN_TIMEOUT_SECONDS", 120.0), + ) + finally: + env_file.unlink(missing_ok=True) + if result.returncode != 0: + detail = str(result.stderr or result.stdout or "docker run failed").strip() + raise RuntimeError(detail) + container_id = str(result.stdout or "").strip().splitlines()[-1] + if not container_id: + raise RuntimeError(f"docker run returned no container id for {project_name} {service_name}") + launch = DockerServiceLaunch( + container_id=container_id, + container_name=container_name, + image=image, + log_path=log_path, + ) + self.runtime._emit( + "service.container.start", + project=project_name, + service=service_name, + container_name=container_name, + image=image, + port=host_port if listener_expected else None, + ) + return launch + + def wait_until_ready(self, launch: DockerServiceLaunch, *, port: int, listener_expected: bool) -> bool: + if not self.container_running(launch.container_id): + self.snapshot_logs(launch.container_id, launch.log_path, tail=200) + return False + if not listener_expected: + return True + ready = bool( + self.process_runtime.wait_for_port( + port, + timeout=self._timeout("ENVCTL_SERVICE_LISTENER_TIMEOUT_SECONDS", 30.0), + ) + ) + if not ready: + self.snapshot_logs(launch.container_id, launch.log_path, tail=200) + return ready + + def snapshot_logs(self, container: str, log_path: str, *, tail: int) -> bool: + command = ["docker", "logs"] + since_marker = Path(f"{log_path}.docker-since") + if since_marker.is_file(): + try: + command.extend(["--since", str(since_marker.stat().st_mtime)]) + except OSError: + pass + command.extend(["--tail", str(max(tail, 0)), container]) + result = self.process_runtime.run( + command, + timeout=30.0, + ) + if result.returncode != 0: + return False + stdout = str(result.stdout or "") + stderr = str(result.stderr or "") + content = stdout + (("\n" if stdout and stderr and not stdout.endswith("\n") else "") + stderr) + path = Path(log_path) + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content, encoding="utf-8") + return True + + def follow_logs(self, container: str, log_path: str) -> int | None: + process = self.process_runtime.start_background( + ["docker", "logs", "--follow", "--since", "0s", container], + stdout_path=log_path, + stderr_path=log_path, + ) + pid = getattr(process, "pid", None) + return pid if isinstance(pid, int) and pid > 0 else None + + def stop(self, container: str, *, remove: bool = True) -> bool: + if not container: + return True + command = ["docker", "rm", "--force", container] if remove else ["docker", "stop", container] + result = self.process_runtime.run(command, timeout=30.0) + return result.returncode == 0 or "No such container" in str(result.stderr or "") + + def container_running(self, container: str) -> bool: + if not container: + return False + result = self.process_runtime.run( + ["docker", "inspect", "--format", "{{.State.Running}}", container], + timeout=10.0, + ) + return result.returncode == 0 and str(result.stdout or "").strip().lower() == "true" + + def _resolve_image( + self, + *, + project_root: Path, + service_name: str, + cwd: Path, + build_env: Mapping[str, str], + ) -> str: + configured_image = self._setting(service_name, "DOCKER_IMAGE") + dockerfile_setting = self._setting(service_name, "DOCKERFILE") + dockerfile = self._resolve_path(dockerfile_setting, project_root) if dockerfile_setting else None + if dockerfile is None and not configured_image: + dockerfile = next( + (path for path in (cwd / "Dockerfile", project_root / "Dockerfile") if path.is_file()), + None, + ) + if dockerfile is None: + if configured_image: + return configured_image + raise RuntimeError( + f"Docker mode needs ENVCTL_{self._service_key(service_name)}_DOCKER_IMAGE, " + "ENVCTL_DOCKER_IMAGE, or a Dockerfile" + ) + if not dockerfile.is_file(): + raise RuntimeError(f"Configured Dockerfile does not exist: {dockerfile}") + context_setting = self._setting(service_name, "DOCKER_CONTEXT") + context = self._resolve_path(context_setting, project_root) if context_setting else dockerfile.parent + if context is None or not context.is_dir(): + raise RuntimeError(f"Configured Docker build context does not exist: {context}") + image = configured_image or self._generated_image_name(project_root, service_name) + self._build_image( + image=image, + dockerfile=dockerfile, + context=context, + service_name=service_name, + build_env=build_env, + ) + return image + + def _build_image( + self, + *, + image: str, + dockerfile: Path, + context: Path, + service_name: str, + build_env: Mapping[str, str], + ) -> None: + policy = (self._setting(service_name, "DOCKER_BUILD_POLICY") or "cached").strip().lower() + if policy in {"never", "image", "existing"}: + if not self._image_exists(image): + raise RuntimeError(f"Docker image {image!r} does not exist and build policy is {policy!r}") + return + if policy == "missing" and self._image_exists(image): + self.runtime._emit("service.container.build", service=service_name, image=image, cache="image") + return + command = ["docker", "build", "--file", str(dockerfile), "--tag", image] + if self.refresh_cache or parse_bool( + str(getattr(self.runtime, "env", {}).get("RUN_SH_REFRESH_CACHE", "")), + False, + ): + command.append("--pull") + target = self._setting(service_name, "DOCKER_TARGET") + if target: + command.extend(["--target", target]) + build_arguments = self._docker_build_arguments(service_name, build_env) + for key in sorted(build_arguments): + command.extend(["--build-arg", key]) + command.append(str(context)) + self.runtime._emit( + "service.container.build", + service=service_name, + image=image, + dockerfile=str(dockerfile), + context=str(context), + cache="docker_layer_cache", + ) + result = self.process_runtime.run( + command, + cwd=context, + env={**os.environ, **build_arguments, "DOCKER_BUILDKIT": "1"}, + timeout=self._timeout("ENVCTL_DOCKER_BUILD_TIMEOUT_SECONDS", 900.0), + ) + if result.returncode != 0: + detail = str(result.stderr or result.stdout or "docker build failed").strip() + raise RuntimeError(detail) + + def _image_exists(self, image: str) -> bool: + return self.process_runtime.run(["docker", "image", "inspect", image], timeout=10.0).returncode == 0 + + def _prepare_container_name(self, name: str) -> None: + inspect = self.process_runtime.run( + [ + "docker", + "container", + "inspect", + "--format", + f"{{{{.State.Running}}}}|{{{{index .Config.Labels {json.dumps(_RUNTIME_SCOPE_LABEL)}}}}}", + name, + ], + timeout=10.0, + ) + if inspect.returncode != 0: + if "No such" in str(inspect.stderr or "") or "not found" in str(inspect.stderr or "").lower(): + return + raise RuntimeError(str(inspect.stderr or inspect.stdout or f"failed to inspect {name}").strip()) + running_text, _separator, owner = str(inspect.stdout or "").strip().partition("|") + expected_owner = self._runtime_scope_identity() + if owner and owner != expected_owner: + raise RuntimeError( + f"Docker container {name!r} is managed by another envctl runtime scope; " + "stop it from that runtime before starting this one" + ) + if running_text.lower() == "true": + raise RuntimeError( + f"Docker container {name!r} is already running but was not resumed from current state; " + "run envctl stop for this project before starting it again" + ) + self._remove_container(name) + + def _remove_container(self, name: str) -> None: + result = self.process_runtime.run(["docker", "rm", "--force", name], timeout=30.0) + if result.returncode != 0 and "No such container" not in str(result.stderr or ""): + raise RuntimeError(str(result.stderr or result.stdout or f"failed to remove {name}").strip()) + + def _runtime_scope_identity(self) -> str: + config = getattr(self.runtime, "config", None) + scope = getattr(config, "runtime_scope_dir", None) + if scope is None: + scope = Path(self.runtime._run_dir_path("docker-scope")).parent + return hashlib.sha256(str(Path(scope).resolve()).encode()).hexdigest()[:16] + + def _require_docker(self) -> None: + command_exists = getattr(self.runtime, "_command_exists", None) + if callable(command_exists) and not command_exists("docker"): + raise RuntimeError("Docker mode requires the docker CLI") + with _DOCKER_INFO_LOCK: + if bool(getattr(self.runtime, "_docker_application_daemon_checked", False)): + return + result = self.process_runtime.run( + ["docker", "info", "--format", "{{.ServerVersion}}"], + timeout=15.0, + ) + if result.returncode != 0: + raise RuntimeError(str(result.stderr or "Docker daemon is unavailable").strip()) + setattr(self.runtime, "_docker_application_daemon_checked", True) + + def _container_command( + self, + service_name: str, + command: Sequence[str], + *, + project_root: Path, + cwd: Path, + host_port: int, + container_port: int, + ) -> list[str]: + override = self._setting(service_name, "DOCKER_COMMAND") + command_mode = (self._setting(service_name, "DOCKER_COMMAND_MODE") or "image").lower() + if override: + resolved = shlex.split(override) + elif command_mode in {"service", "envctl", "host"}: + resolved = [str(part) for part in command] + elif command_mode in {"image", "default"}: + return [] + else: + raise RuntimeError( + f"Invalid Docker command mode for {service_name}: {command_mode!r}; expected image or service" + ) + if not resolved: + return [] + executable = Path(resolved[0]) + if executable.is_absolute(): + try: + resolved[0] = str(executable.relative_to(cwd)) + except ValueError: + try: + resolved[0] = str(executable.relative_to(project_root)) + except ValueError: + pass + normalized: list[str] = [] + for part in resolved: + if part in {"127.0.0.1", "localhost"}: + normalized.append("0.0.0.0") + continue + if host_port > 0 and container_port > 0: + if part == str(host_port): + normalized.append(str(container_port)) + continue + part = re.sub( + rf"^(--port=){re.escape(str(host_port))}$", + rf"\g<1>{container_port}", + part, + ) + part = re.sub(r"^(--host=)(localhost|127\.0\.0\.1)$", r"\g<1>0.0.0.0", part) + normalized.append(part) + return normalized + + def _docker_build_arguments( + self, + service_name: str, + build_env: Mapping[str, str], + ) -> dict[str, str]: + arguments = { + str(key): str(value) + for key, value in build_env.items() + if str(key).startswith(_PUBLIC_ENV_PREFIXES) + } + configured = self._setting(service_name, "DOCKER_BUILD_ARGS") + for token in configured.split(","): + item = token.strip() + if not item: + continue + key, separator, configured_value = item.partition("=") + key = key.strip() + if not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", key): + raise RuntimeError(f"Invalid Docker build argument name: {key!r}") + if separator: + arguments[key] = configured_value + elif key in build_env: + arguments[key] = str(build_env[key]) + else: + raise RuntimeError(f"Docker build argument {key!r} is not present in the service environment") + return arguments + + def _container_env(self, values: Mapping[str, str]) -> dict[str, str]: + result: dict[str, str] = {} + for raw_key, raw_value in values.items(): + key = str(raw_key) + if key in _HOST_ENV_KEYS or key.startswith("TERM_") or key.startswith("LC_"): + continue + value = str(raw_value) + if key in {"HOST", "BIND_HOST", "LISTEN_HOST", "SERVER_HOST"} and value in { + "localhost", + "127.0.0.1", + }: + result[key] = "0.0.0.0" + continue + if key not in _PUBLIC_ENV_KEYS and not key.startswith(_PUBLIC_ENV_PREFIXES): + value = re.sub( + r"(://(?:[^/@\s]+@)?)(localhost|127\.0\.0\.1)(?=[:/]|$)", + r"\1host.docker.internal", + value, + ) + if key.endswith("_HOST") and value in {"localhost", "127.0.0.1"}: + value = "host.docker.internal" + result[key] = value + result["ENVCTL_CONTAINER_RUNTIME"] = "docker" + result["ENVCTL_HOST_GATEWAY"] = "host.docker.internal" + return result + + def _write_env_file(self, *, project_name: str, service_name: str, values: Mapping[str, str]) -> Path: + run_dir = Path(self.runtime._run_dir_path(getattr(self.runtime, "_active_run_id", None) or "docker")) + run_dir.mkdir(parents=True, exist_ok=True) + digest = hashlib.sha256(f"{project_name}\0{service_name}".encode()).hexdigest()[:12] + path = run_dir / f".docker-env-{digest}" + lines = [f"{key}={str(value).replace(chr(10), '')}" for key, value in sorted(values.items())] + path.write_text("\n".join(lines) + "\n", encoding="utf-8") + path.chmod(0o600) + return path + + def _setting(self, service_name: str, suffix: str) -> str: + env = getattr(self.runtime, "env", {}) + raw = getattr(getattr(self.runtime, "config", None), "raw", {}) + service_key = f"ENVCTL_{self._service_key(service_name)}_{suffix}" + additional_service_key = f"ENVCTL_SERVICE_{self._service_key(service_name)}_{suffix}" + global_key = f"ENVCTL_{suffix}" + for key in (additional_service_key, service_key, global_key): + value = str(env.get(key) or raw.get(key) or "").strip() + if value: + return value + return "" + + def _container_port(self, service_name: str, host_port: int, *, image: str) -> int: + value = self._setting(service_name, "DOCKER_PORT") + if not value: + exposed = self._single_exposed_port(image) + return exposed if exposed is not None else host_port + try: + port = int(value) + except ValueError as exc: + raise RuntimeError(f"Invalid Docker container port for {service_name}: {value!r}") from exc + if port <= 0 or port > 65535: + raise RuntimeError(f"Invalid Docker container port for {service_name}: {value!r}") + return port + + def _single_exposed_port(self, image: str) -> int | None: + result = self.process_runtime.run( + ["docker", "image", "inspect", "--format", "{{json .Config.ExposedPorts}}", image], + timeout=10.0, + ) + if result.returncode != 0: + return None + try: + payload = json.loads(str(result.stdout or "null")) + except json.JSONDecodeError: + return None + if not isinstance(payload, dict) or len(payload) != 1: + return None + raw_port = str(next(iter(payload))).partition("/")[0] + try: + port = int(raw_port) + except ValueError: + return None + return port if 0 < port <= 65535 else None + + def _container_name(self, *, project_name: str, project_root: Path, service_name: str) -> str: + return docker_service_container_name( + project_name=project_name, + project_root=project_root, + service_name=service_name, + ) + + def _generated_image_name(self, project_root: Path, service_name: str) -> str: + identity = self._repository_identity(project_root) + digest = hashlib.sha256(str(project_root.resolve()).encode()).hexdigest()[:10] + repo = _docker_name(identity.name, limit=32) + service = _docker_name(service_name, limit=24) + return f"envctl/{repo}-{service}:{digest}" + + @staticmethod + def _repository_identity(project_root: Path) -> Path: + dot_git = project_root / ".git" + if dot_git.is_file(): + try: + gitdir = dot_git.read_text(encoding="utf-8").strip().removeprefix("gitdir:").strip() + resolved = (project_root / gitdir).resolve() + if resolved.parent.name == "worktrees": + return resolved.parent.parent.parent + except OSError: + pass + return project_root.resolve() + + @staticmethod + def _service_key(service_name: str) -> str: + return re.sub(r"[^A-Z0-9]+", "_", service_name.upper()).strip("_") or "SERVICE" + + @staticmethod + def _resolve_path(value: str, project_root: Path) -> Path | None: + if not value: + return None + path = Path(value).expanduser() + return path.resolve() if path.is_absolute() else (project_root / path).resolve() + + def _timeout(self, key: str, default: float) -> float: + value = str(getattr(self.runtime, "env", {}).get(key, "") or "").strip() + try: + return max(float(value), 1.0) if value else default + except ValueError: + return default + + +def refresh_container_log_snapshots( + runtime: Any, + state: object, + *, + tail: int, + follow: bool, +) -> list[int]: + docker_services = [ + service + for service in getattr(state, "services", {}).values() + if str(getattr(service, "runtime_kind", "process") or "process").lower() == "docker" + ] + if not docker_services: + return [] + process_runner = getattr(runtime, "process_runner", None) + if process_runner is None: + return [] + docker_runtime = DockerServiceRuntime(runtime, process_runner) + follower_pids: list[int] = [] + for service in docker_services: + container = str( + getattr(service, "container_id", "") or getattr(service, "container_name", "") or "" + ).strip() + log_path = str(getattr(service, "log_path", "") or "").strip() + if not container or not log_path: + continue + docker_runtime.snapshot_logs(container, log_path, tail=tail) + if follow: + pid = docker_runtime.follow_logs(container, log_path) + if pid is not None: + follower_pids.append(pid) + return follower_pids + + +def stop_container_log_followers(runtime: Any, pids: Sequence[int]) -> None: + for pid in pids: + try: + runtime.process_runner.terminate_process_group(pid, term_timeout=1.0, kill_timeout=1.0) + except Exception: # noqa: BLE001 + continue diff --git a/python/envctl_engine/runtime/engine_runtime_artifacts.py b/python/envctl_engine/runtime/engine_runtime_artifacts.py index aaf7d9310..2f50a73f5 100644 --- a/python/envctl_engine/runtime/engine_runtime_artifacts.py +++ b/python/envctl_engine/runtime/engine_runtime_artifacts.py @@ -221,12 +221,28 @@ def _runtime_readiness_async_enabled(runtime: Any) -> bool: def print_summary(_runtime: Any, state: object, contexts: Sequence[_SummaryContext]) -> None: - _ = state print("envctl Python engine run summary") for context in contexts: - backend = context.ports["backend"].final - frontend = context.ports["frontend"].final + backend = _summary_service_port(state, context.name, "backend") or context.ports["backend"].final + frontend = _summary_service_port(state, context.name, "frontend") or context.ports["frontend"].final db = context.ports["db"].final redis = context.ports["redis"].final n8n = context.ports["n8n"].final print(f"- {context.name}: backend={backend} frontend={frontend} db={db} redis={redis} n8n={n8n}") + + +def _summary_service_port(state: object, project_name: str, service_type: str) -> int | None: + for service_name, service in getattr(state, "services", {}).items(): + project = str(getattr(service, "project", "") or "").strip() + if not project: + suffix = f" {service_type}".lower() + rendered_name = str(service_name) + project = rendered_name[: -len(suffix)] if rendered_name.lower().endswith(suffix) else "" + if project.casefold() != project_name.casefold(): + continue + if str(getattr(service, "type", "") or "").casefold() != service_type.casefold(): + continue + port = getattr(service, "actual_port", None) or getattr(service, "requested_port", None) + if isinstance(port, int) and port > 0: + return port + return None diff --git a/python/envctl_engine/runtime/engine_runtime_commands.py b/python/envctl_engine/runtime/engine_runtime_commands.py index 49fbddf58..39c5a5c95 100644 --- a/python/envctl_engine/runtime/engine_runtime_commands.py +++ b/python/envctl_engine/runtime/engine_runtime_commands.py @@ -7,6 +7,7 @@ from typing import Any, Mapping from envctl_engine.runtime.command_resolution import resolve_requirement_start_command, resolve_service_start_command +from envctl_engine.runtime.docker_service_runtime import docker_service_uses_image_command SERVICE_LAUNCH_ENV_REMOVALS = { "ACTIONS_CACHE_URL", @@ -105,13 +106,17 @@ def service_start_command_resolved( project_root: Path | None = None, port: int = 0, ) -> tuple[list[str], str]: + command_exists = runtime._command_exists + if docker_service_uses_image_command(runtime, service_name): + def command_exists(_executable: str) -> bool: + return True result = resolve_service_start_command( service_name=service_name, project_root=(project_root or runtime.config.base_dir), port=port, env=runtime.env, config_raw=runtime.config.raw, - command_exists=runtime._command_exists, + command_exists=command_exists, ) return result.command, result.source diff --git a/python/envctl_engine/runtime/engine_runtime_hooks.py b/python/envctl_engine/runtime/engine_runtime_hooks.py index 2085ec347..4ba8394ad 100644 --- a/python/envctl_engine/runtime/engine_runtime_hooks.py +++ b/python/envctl_engine/runtime/engine_runtime_hooks.py @@ -148,6 +148,10 @@ def services_from_hook_payload( actual_port=(actual_port if actual_port > 0 else None), log_path=(str(log_path) if log_path else None), status=status, + runtime_kind=str(raw.get("runtime_kind") or "process"), + container_id=str(raw.get("container_id") or "") or None, + container_name=str(raw.get("container_name") or "") or None, + container_image=str(raw.get("container_image") or "") or None, ) records[name] = record if service_type == "backend" and actual_port > 0: diff --git a/python/envctl_engine/runtime/help_general.py b/python/envctl_engine/runtime/help_general.py index 0e7b00251..7451029a7 100644 --- a/python/envctl_engine/runtime/help_general.py +++ b/python/envctl_engine/runtime/help_general.py @@ -87,6 +87,7 @@ def render_general_help() -> str: " --only-backend launch only backend; skip frontend and dependencies/prep", " --no-deps skip managed dependencies and plan-agent dependency prep", " --no-infra skip backend, frontend, managed dependencies, and plan-agent prep", + " --docker run configured app services as managed Docker containers", "", "Important global flags:", " --headless / --batch do not prompt; use deterministic automation-friendly output", diff --git a/python/envctl_engine/runtime/help_topic_lifecycle.py b/python/envctl_engine/runtime/help_topic_lifecycle.py index 4de800366..c80ba9975 100644 --- a/python/envctl_engine/runtime/help_topic_lifecycle.py +++ b/python/envctl_engine/runtime/help_topic_lifecycle.py @@ -26,6 +26,7 @@ "--deps-sequential|--sequential-deps force managed dependencies to start one at a time", "--project limit startup to selected project/worktree when supported", "--no-resume skip automatic resume of a compatible saved run", + "--docker build/use images and run app services in managed containers", ), examples=( "envctl", @@ -33,6 +34,7 @@ "envctl start --trees --entire-system --headless", "envctl start --project feature-a-1 --backend --headless", "envctl --trees --only-backend --headless", + "envctl start --trees --entire-system --docker --headless", ), related=("restart", "resume", "dashboard", "stop", "health"), ), diff --git a/python/envctl_engine/runtime/lifecycle_blast_docker.py b/python/envctl_engine/runtime/lifecycle_blast_docker.py index 73997ac0a..ab62412c1 100644 --- a/python/envctl_engine/runtime/lifecycle_blast_docker.py +++ b/python/envctl_engine/runtime/lifecycle_blast_docker.py @@ -10,7 +10,10 @@ def matches_blast_container(*, image: str, name: str) -> bool: image_l = image.lower() name_l = name.lower() - return any(token in name_l or token in image_l for token in ("supabase", "n8n", "redis", "postgres")) + return any( + token in name_l or token in image_l + for token in ("envctl-app-", "supabase", "n8n", "redis", "postgres") + ) class BlastDockerCleanupSupport: diff --git a/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py b/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py index b2680e985..f145dc768 100644 --- a/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py +++ b/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py @@ -3,6 +3,7 @@ from collections.abc import Iterable from typing import Any, Callable, Mapping, cast +from envctl_engine.requirements.core import dependency_definitions from envctl_engine.runtime.lifecycle_blast_support import LifecycleBlastCleanupSupport from envctl_engine.runtime.engine_runtime_lifecycle_support import release_requirement_ports from envctl_engine.runtime.lifecycle_dependency_stop import ( @@ -10,6 +11,7 @@ release_selected_dependency_components, requirements_have_enabled_components, select_dependency_components_for_stop, + stop_requirement_component_containers, ) from envctl_engine.runtime.lifecycle_service_stop_selection import ( select_services_for_stop, @@ -223,6 +225,11 @@ def operation() -> int: return 0 for project in list(state.requirements): + requirements = state.requirements[project] + for definition in dependency_definitions(): + component = requirements.component(definition.id) + if bool(component.get("enabled", False)): + self._stop_requirement_component_containers(component) self._release_requirement_ports(state.requirements[project]) state.requirements.pop(project, None) @@ -253,8 +260,12 @@ def _release_selected_dependency_components( state, selected_dependencies, release_component_ports_fn=self._release_requirement_component_ports, + stop_component_fn=self._stop_requirement_component_containers, ) + def _stop_requirement_component_containers(self, component: Mapping[str, object]) -> None: + stop_requirement_component_containers(self.runtime, component) + def _release_requirement_component_ports(self, component: Mapping[str, object]) -> None: port_allocator = self._port_allocator(self.runtime) release_requirement_component_ports(component, release_port_fn=port_allocator.release) @@ -312,6 +323,11 @@ def clear_runtime_state(self, *, command: str, aggressive: bool = False, route: aggressive=aggressive, verify_ownership=not aggressive, ) + for requirements in state.requirements.values(): + for definition in dependency_definitions(): + component = requirements.component(definition.id) + if bool(component.get("enabled", False)): + self._stop_requirement_component_containers(component) if aggressive and self.blast_all_ecosystem_enabled(): self.blast_all_ecosystem_cleanup(route=route) diff --git a/python/envctl_engine/runtime/lifecycle_dependency_stop.py b/python/envctl_engine/runtime/lifecycle_dependency_stop.py index aedc97432..c18c7ba02 100644 --- a/python/envctl_engine/runtime/lifecycle_dependency_stop.py +++ b/python/envctl_engine/runtime/lifecycle_dependency_stop.py @@ -1,6 +1,7 @@ from __future__ import annotations from collections.abc import Callable, Mapping +from typing import Any from envctl_engine.requirements.core import dependency_definitions from envctl_engine.runtime.command_router import Route @@ -9,6 +10,17 @@ def select_dependency_components_for_stop(state: RunState, route: Route) -> dict[str, set[str]]: + if route.flags.get("runtime_scope") == "entire-system": + requested_projects = {str(project).strip().casefold() for project in route.projects if str(project).strip()} + return { + project_name: { + definition.id + for definition in dependency_definitions() + if bool(requirements.component(definition.id).get("enabled", False)) + } + for project_name, requirements in state.requirements.items() + if not requested_projects or str(project_name).strip().casefold() in requested_projects + } raw_components = route.flags.get("stop_dependency_components") if not isinstance(raw_components, list): return {} @@ -38,6 +50,7 @@ def release_selected_dependency_components( selected_dependencies: dict[str, set[str]], *, release_component_ports_fn: Callable[[Mapping[str, object]], None], + stop_component_fn: Callable[[Mapping[str, object]], None] | None = None, ) -> None: for project_name, dependency_ids in selected_dependencies.items(): requirements = state.requirements.get(project_name) @@ -48,6 +61,8 @@ def release_selected_dependency_components( if not bool(component.get("enabled", False)): continue if not bool(component.get("external")): + if stop_component_fn is not None: + stop_component_fn(component) release_component_ports_fn(component) requirements.components[dependency_id] = {} if not requirements_have_enabled_components(requirements): @@ -72,3 +87,38 @@ def requirements_have_enabled_components(requirements: object) -> bool: for component in components.values() if isinstance(component, Mapping) ) + + +def stop_requirement_component_containers(runtime: Any, component: Mapping[str, object]) -> None: + if bool(component.get("external")) or bool(component.get("simulated")): + return + container_name = str(component.get("container_name") or "").strip() + if not container_name: + return + process_runner = getattr(runtime, "process_runner", None) + if process_runner is None: + return + if str(component.get("id") or "").strip().lower() == "supabase": + listed = process_runner.run( + [ + "docker", + "ps", + "--all", + "--quiet", + "--filter", + f"label=com.docker.compose.project={container_name}", + ], + timeout=30.0, + ) + if listed.returncode != 0: + raise RuntimeError(str(listed.stderr or listed.stdout or "failed listing Supabase containers").strip()) + container_ids = [line.strip() for line in str(listed.stdout or "").splitlines() if line.strip()] + if container_ids: + removed = process_runner.run(["docker", "rm", "--force", *container_ids], timeout=60.0) + if removed.returncode != 0: + detail = removed.stderr or removed.stdout or "failed stopping Supabase containers" + raise RuntimeError(str(detail).strip()) + return + removed = process_runner.run(["docker", "rm", "--force", container_name], timeout=30.0) + if removed.returncode != 0 and "No such container" not in str(removed.stderr or ""): + raise RuntimeError(str(removed.stderr or removed.stdout or f"failed stopping {container_name}").strip()) diff --git a/python/envctl_engine/runtime/lifecycle_service_termination.py b/python/envctl_engine/runtime/lifecycle_service_termination.py index 0c8ff9841..88ca18af7 100644 --- a/python/envctl_engine/runtime/lifecycle_service_termination.py +++ b/python/envctl_engine/runtime/lifecycle_service_termination.py @@ -45,6 +45,30 @@ def terminate_one(item: tuple[str, object]) -> tuple[str, bool, int | None]: def terminate_service_record(runtime: Any, service: object, *, aggressive: bool, verify_ownership: bool) -> bool: + if str(getattr(service, "runtime_kind", "process") or "process").lower() == "docker": + from envctl_engine.runtime.docker_service_runtime import DockerServiceRuntime + + container = str( + getattr(service, "container_id", "") or getattr(service, "container_name", "") or "" + ).strip() + stopped = DockerServiceRuntime(runtime, runtime.process_runner).stop(container) + pid = getattr(service, "pid", None) + if isinstance(pid, int) and pid > 0: + try: + runtime.process_runner.terminate_process_group( + pid, + term_timeout=0.5 if aggressive else 2.0, + kill_timeout=1.0, + ) + except Exception: # noqa: BLE001 + pass + runtime._emit( + "service.container.stop", + service=getattr(service, "name", "unknown"), + container_name=getattr(service, "container_name", None), + stopped=stopped, + ) + return stopped pid = getattr(service, "pid", None) if not isinstance(pid, int) or pid <= 0: return True diff --git a/python/envctl_engine/runtime/lifecycle_worktree_containers.py b/python/envctl_engine/runtime/lifecycle_worktree_containers.py index 55717aab7..6f796743b 100644 --- a/python/envctl_engine/runtime/lifecycle_worktree_containers.py +++ b/python/envctl_engine/runtime/lifecycle_worktree_containers.py @@ -7,6 +7,7 @@ from envctl_engine.requirements.common_contracts import build_container_name from envctl_engine.requirements.docker_runtime import run_docker, run_result_error from envctl_engine.requirements.supabase import build_supabase_project_name +from envctl_engine.runtime.docker_service_runtime import docker_service_container_name def legacy_container_name(*, prefix: str, project_name: str) -> str: @@ -36,6 +37,20 @@ def remove_tree_containers( legacy_container_name(prefix="envctl-redis", project_name=project_name): "redis", legacy_container_name(prefix="envctl-n8n", project_name=project_name): "n8n", } + app_service_names = ["backend", "frontend"] + app_service_names.extend( + str(getattr(service, "name", "") or "").strip() + for service in getattr(getattr(runtime, "config", None), "additional_services", ()) + if str(getattr(service, "name", "") or "").strip() + ) + for service_name in app_service_names: + exact_names[ + docker_service_container_name( + project_name=project_name, + project_root=resolved_root, + service_name=service_name, + ) + ] = service_name supabase_prefixes: set[str] = set() if include_supabase: supabase_prefixes = { diff --git a/python/envctl_engine/runtime/service_status_truth.py b/python/envctl_engine/runtime/service_status_truth.py index c5001a5a3..e2335c245 100644 --- a/python/envctl_engine/runtime/service_status_truth.py +++ b/python/envctl_engine/runtime/service_status_truth.py @@ -6,6 +6,33 @@ def service_truth_status(runtime: Any, service: object) -> str: + if str(getattr(service, "runtime_kind", "process") or "process").lower() == "docker": + from envctl_engine.runtime.docker_service_runtime import DockerServiceRuntime + + container = str( + getattr(service, "container_id", "") or getattr(service, "container_name", "") or "" + ).strip() + docker_runtime = DockerServiceRuntime(runtime, runtime.process_runner) + if not docker_runtime.container_running(container): + status = "stale" + elif not bool(getattr(service, "listener_expected", True)): + status = "running" + else: + port = getattr(service, "actual_port", None) or getattr(service, "requested_port", None) + try: + reachable = isinstance(port, int) and port > 0 and bool( + runtime.process_runner.wait_for_port(port, timeout=runtime._service_truth_timeout()) + ) + except Exception: # noqa: BLE001 + reachable = False + status = "running" if reachable else "unreachable" + runtime._emit( + "service.truth.check", + service=service_display_name(service), + status=status, + reason_code=f"container_truth_status_{status}", + ) + return status backend = getattr(runtime.process_probe, "backend", None) if isinstance(backend, ShellProbeBackend): backend.process_runner = runtime.process_runner diff --git a/python/envctl_engine/runtime/startup_inspection_support.py b/python/envctl_engine/runtime/startup_inspection_support.py index 58ec1b20e..1b2cb1f22 100644 --- a/python/envctl_engine/runtime/startup_inspection_support.py +++ b/python/envctl_engine/runtime/startup_inspection_support.py @@ -84,6 +84,9 @@ def build(self) -> dict[str, Any]: payload = { "command": getattr(self.startup_route, "command", "start"), "mode": self.runtime_mode, + "application_runtime": ( + "docker" if bool(getattr(self.startup_route, "flags", {}).get("docker")) else "process" + ), "headless": self.batch or not self.can_tty, "interactive_tty": self.can_tty, "selection": selection_info, diff --git a/python/envctl_engine/startup/finalization_run_state.py b/python/envctl_engine/startup/finalization_run_state.py index 37a34cc90..423e4377a 100644 --- a/python/envctl_engine/startup/finalization_run_state.py +++ b/python/envctl_engine/startup/finalization_run_state.py @@ -54,6 +54,9 @@ def build_planning_dashboard_state( ), } ) + metadata["application_runtime"] = ( + "docker" if bool(route.flags.get("docker")) else "process" + ) run_state = RunState( run_id=run_id, mode=runtime_mode, @@ -80,6 +83,9 @@ def _build_run_state(runtime: StartupRuntime, session: StartupSession, *, failed { "command": session.effective_route.command, "repo_scope_id": runtime.config.runtime_scope_id, + "application_runtime": ( + "docker" if bool(session.effective_route.flags.get("docker")) else "process" + ), } ) dependency_mode = effective_dependency_scope(session.effective_route, session.runtime_mode) diff --git a/python/envctl_engine/startup/restart_prestop_support.py b/python/envctl_engine/startup/restart_prestop_support.py index aa0d1e981..72c2ea6d8 100644 --- a/python/envctl_engine/startup/restart_prestop_support.py +++ b/python/envctl_engine/startup/restart_prestop_support.py @@ -5,6 +5,7 @@ from typing import Any, Callable, Iterable from envctl_engine.runtime.command_router import Route +from envctl_engine.runtime.docker_service_runtime import state_uses_docker_services from envctl_engine.runtime.runtime_context import resolve_port_allocator, resolve_process_runtime from envctl_engine.shared.services import service_project_name, service_slug_from_record from envctl_engine.startup.startup_selection_support import ( @@ -77,6 +78,9 @@ def restart_prestop_state(*, route: Route, runtime: Any) -> RestartPrestopState: run_id=resumed.run_id, ) resumed = None + if resumed is not None and state_uses_docker_services(resumed): + route.flags["docker"] = True + runtime.env["DOCKER_MODE"] = "true" fallback_route = None if resumed is None: fallback_route = restart_fallback_start_route(route, restart_lookup_mode=restart_lookup_mode) diff --git a/python/envctl_engine/startup/run_reuse_decision.py b/python/envctl_engine/startup/run_reuse_decision.py index c97bcfc65..4c0a9228e 100644 --- a/python/envctl_engine/startup/run_reuse_decision.py +++ b/python/envctl_engine/startup/run_reuse_decision.py @@ -7,6 +7,7 @@ from envctl_engine.runtime.command_router import Route from envctl_engine.state.models import RunState from envctl_engine.startup.run_reuse_identity import ( + ProjectIdentity, _auto_resume_start_enabled, _identity_keys, _root_mismatches, @@ -57,6 +58,21 @@ def evaluate(self) -> RunReuseDecision: startup_enabled=startup_enabled, ) if not _auto_resume_start_enabled(self.route): + if bool(self.route.flags.get("no_resume")): + candidate = self.runtime._try_load_existing_state( + mode=self.runtime_mode, + strict_mode_match=True, + ) + if candidate is not None: + state_identities = project_identities_from_state(self.runtime, candidate) + return RunReuseDecision( + candidate_state=candidate, + decision_kind="fresh_run", + reason="explicit_no_resume", + selected_projects=selected_payload, + state_projects=identities_to_payload(state_identities), + startup_enabled=startup_enabled, + ) return self._fresh_without_state( reason="auto_resume_disabled", selected_payload=selected_payload, @@ -97,7 +113,7 @@ def _evaluate_candidate( self, *, candidate: RunState, - selected_identities: list[object], + selected_identities: list[ProjectIdentity], selected_payload: list[dict[str, str | None]], startup_enabled: bool, ) -> RunReuseDecision: @@ -200,7 +216,11 @@ def _initial_candidate_rejection( ) return None - def _startup_identity_mismatch(self, candidate: RunState, state_identities: list[object]) -> dict[str, object]: + def _startup_identity_mismatch( + self, + candidate: RunState, + state_identities: list[ProjectIdentity], + ) -> dict[str, object]: previous_identity = candidate.metadata.get("startup_identity") if not isinstance(previous_identity, dict): return {} @@ -316,10 +336,7 @@ def _candidate_fresh( def mark_run_reused(metadata: Mapping[str, object] | None, *, reason: str) -> dict[str, object]: updated = dict(metadata or {}) raw_count = updated.get("run_reuse_count", 0) - try: - count = int(raw_count) - except (TypeError, ValueError): - count = 0 + count = int(raw_count) if isinstance(raw_count, (bool, float, int, str)) else 0 updated["run_reuse_count"] = max(0, count) + 1 updated["last_reopened_at"] = datetime.now(tz=UTC).isoformat() updated["last_reuse_reason"] = reason diff --git a/python/envctl_engine/startup/run_reuse_fresh_start.py b/python/envctl_engine/startup/run_reuse_fresh_start.py index 2cb75fc60..e1b350e21 100644 --- a/python/envctl_engine/startup/run_reuse_fresh_start.py +++ b/python/envctl_engine/startup/run_reuse_fresh_start.py @@ -18,26 +18,35 @@ def fresh_start_replacement_services( additional_services: tuple[object, ...], project_name_from_service: Callable[[str], str], ) -> set[str]: - target_projects = {str(context.name).strip().lower() for context in selected_contexts} + target_projects = {str(getattr(context, "name", "")).strip().lower() for context in selected_contexts} target_projects.discard("") if not target_projects: return set() selected_by_project = { - str(context.name).strip().lower(): _restart_service_types_for_project( + str(getattr(context, "name", "")).strip().lower(): _restart_service_types_for_project( route=route, - project_name=str(context.name), + project_name=str(getattr(context, "name", "")), default_service_types=configured_service_types, additional_services=additional_services, ) for context in selected_contexts - if str(context.name).strip() + if str(getattr(context, "name", "")).strip() } + runtime_scope = str(route.flags.get("runtime_scope") or "").strip().lower() + replace_all_for_explicit_fresh_start = ( + bool(route.flags.get("no_resume")) + and not bool(route.flags.get("services")) + and runtime_scope not in {"backend", "frontend", "dependencies", "fullstack"} + ) selected: set[str] = set() for service_name, service in getattr(candidate_state, "services", {}).items(): project = service_project_name(service) or project_name_from_service(service_name) project_key = str(project).strip().lower() if project_key not in target_projects: continue + if replace_all_for_explicit_fresh_start: + selected.add(service_name) + continue service_type = service_slug_from_record(service) if service_type and service_type in selected_by_project.get(project_key, set()): selected.add(service_name) @@ -56,7 +65,7 @@ class FreshStartServiceReplacer: terminate_restart_orphan_listeners: Callable[..., None] def replace(self) -> None: - if self.reason != "startup_fingerprint_mismatch": + if self.reason not in {"explicit_no_resume", "startup_fingerprint_mismatch"}: return route = self.session.effective_route if route.flags.get("runtime_scope") == "dependencies": @@ -65,10 +74,12 @@ def replace(self) -> None: if not selected_services: return self.announce_session_identifiers(self.session) - self.report_progress( - route, - f"Startup selection changed; replacing {len(selected_services)} existing service(s)...", + message = ( + f"Fresh start requested; replacing {len(selected_services)} existing service(s)..." + if self.reason == "explicit_no_resume" + else f"Startup selection changed; replacing {len(selected_services)} existing service(s)..." ) + self.report_progress(route, message) self.runtime._emit( "state.run_reuse.replace_existing_services", run_id=self.candidate_state.run_id, @@ -87,6 +98,43 @@ def replace(self) -> None: selected_services=selected_services, aggressive=True, ) + self._rebind_replacement_ports(selected_services) + + def _rebind_replacement_ports(self, selected_services: set[str]) -> None: + port_planner = getattr(self.runtime, "port_planner", None) + release = getattr(port_planner, "release", None) + if not callable(release): + return + contexts = { + str(getattr(context, "name", "")).casefold(): context + for context in getattr(self.session, "selected_contexts", []) + } + for name in sorted(selected_services): + service = getattr(self.candidate_state, "services", {}).get(name) + if service is None or not bool(getattr(service, "listener_expected", True)): + continue + project = service_project_name(service) or self.runtime._project_name_from_service(name) + service_type = service_slug_from_record(service) + context = contexts.get(str(project).casefold()) + plans = getattr(context, "ports", {}) if context is not None else {} + plan = plans.get(service_type) if service_type else None + old_port = getattr(service, "actual_port", None) or getattr(service, "requested_port", None) + current_port = getattr(plan, "final", None) + if plan is None or not isinstance(old_port, int) or old_port <= 0: + continue + owner = f"{getattr(context, 'name', project)}:{service_type}" + if isinstance(current_port, int) and current_port > 0: + release(current_port, owner=owner) + plan.assigned = old_port + plan.final = old_port + plan.source = "fresh_start_replacement" + self.runtime._emit( + "port.rebound.after_fresh_start_cleanup", + project=project, + service=service_type, + previous_port=old_port, + port=old_port, + ) def replace_existing_project_services_for_fresh_start( diff --git a/python/envctl_engine/startup/run_reuse_identity.py b/python/envctl_engine/startup/run_reuse_identity.py index bd8e66f43..e0245561a 100644 --- a/python/envctl_engine/startup/run_reuse_identity.py +++ b/python/envctl_engine/startup/run_reuse_identity.py @@ -65,7 +65,9 @@ def _startup_identity_mismatch(previous: Mapping[str, object], current: Mapping[ def _startup_identity_comparison_payload(identity: Mapping[str, object]) -> dict[str, object]: - return {str(key): value for key, value in identity.items() if str(key) != "fingerprint"} + payload = {str(key): value for key, value in identity.items() if str(key) != "fingerprint"} + payload.setdefault("application_runtime", "process") + return payload def project_identities_from_contexts(contexts: list[object]) -> list[ProjectIdentity]: @@ -166,6 +168,12 @@ def _startup_identity_payload( } payload = { "mode": runtime_mode, + "application_runtime": ( + "docker" + if bool(route is not None and route.flags.get("docker")) + or str(getattr(runtime, "env", {}).get("DOCKER_MODE", "")).strip().lower() == "true" + else "process" + ), "projects": identities_to_payload(project_identities_from_contexts(project_contexts)), "startup_enabled": startup_enabled, "services": services, diff --git a/python/envctl_engine/startup/run_reuse_resume.py b/python/envctl_engine/startup/run_reuse_resume.py index bed5d1840..331da5562 100644 --- a/python/envctl_engine/startup/run_reuse_resume.py +++ b/python/envctl_engine/startup/run_reuse_resume.py @@ -5,6 +5,7 @@ from typing import Any, cast from envctl_engine.runtime.command_router import Route +from envctl_engine.runtime.docker_service_runtime import state_uses_docker_services from envctl_engine.runtime.engine_runtime_startup_support import mark_run_reused from envctl_engine.runtime.runtime_context import resolve_state_repository from envctl_engine.state.runtime_map import build_runtime_map @@ -122,6 +123,9 @@ def _resume_route(self) -> Route: } if self._attach_plan_agent_after_resume(): resume_flags["batch"] = True + if state_uses_docker_services(self.candidate_state): + resume_flags["docker"] = True + self.runtime.env["DOCKER_MODE"] = "true" return Route( command="resume", mode=self.runtime_mode, diff --git a/python/envctl_engine/startup/service_attach_execution.py b/python/envctl_engine/startup/service_attach_execution.py index 1421c745b..686e44664 100644 --- a/python/envctl_engine/startup/service_attach_execution.py +++ b/python/envctl_engine/startup/service_attach_execution.py @@ -1,13 +1,19 @@ from __future__ import annotations from collections.abc import Callable, Mapping -from dataclasses import dataclass +from dataclasses import dataclass, field import os from pathlib import Path import time from typing import Any, cast from envctl_engine.runtime.service_manager import ServiceStartDescriptor +from envctl_engine.runtime.docker_service_runtime import ( + DockerServiceLaunch, + DockerServiceRuntime, + docker_service_mode_enabled, + docker_service_uses_image_command, +) from envctl_engine.startup.service_execution_policy import ordered_service_layers from envctl_engine.startup.service_execution_records import PreparedServiceLaunch from envctl_engine.state.models import PortPlan, ServiceRecord @@ -34,6 +40,9 @@ class ServiceAttachRunner: additional_services: tuple[Any, ...] backend_listener_expected: bool rebound_delta: int + docker_mode: bool = False + refresh_cache: bool = False + _container_launches: dict[str, DockerServiceLaunch] = field(default_factory=dict, init=False) def start( self, @@ -42,20 +51,27 @@ def start( on_service_retry: Callable[[str, int, int, int, str | None], None], ) -> dict[str, ServiceRecord]: descriptors = self._service_descriptors() - generic_attach = getattr(self.runtime.services, "start_services_with_attach", None) - if callable(generic_attach): - return self._start_with_generic_attach( - descriptors=descriptors, - attach_parallel=attach_parallel, - on_service_retry=on_service_retry, - generic_attach=cast(Callable[..., dict[str, ServiceRecord]], generic_attach), - ) - if self.selected_service_types <= {"backend", "frontend"}: - return self._start_core_services_with_legacy_attach( - attach_parallel=attach_parallel, - on_service_retry=on_service_retry, - ) - raise RuntimeError("Service manager does not support additional app services") + try: + generic_attach = getattr(self.runtime.services, "start_services_with_attach", None) + if callable(generic_attach): + records = self._start_with_generic_attach( + descriptors=descriptors, + attach_parallel=attach_parallel, + on_service_retry=on_service_retry, + generic_attach=cast(Callable[..., dict[str, ServiceRecord]], generic_attach), + ) + elif self.selected_service_types <= {"backend", "frontend"}: + records = self._start_core_services_with_legacy_attach( + attach_parallel=attach_parallel, + on_service_retry=on_service_retry, + ) + else: + raise RuntimeError("Service manager does not support additional app services") + except Exception: + self._cleanup_container_launches() + raise + self._annotate_container_records(records) + return records def reserve_next(self, port: int) -> int: return self.port_allocator.reserve_next(port, owner=f"{self.project_name}:services") @@ -200,13 +216,41 @@ def _launch_prepared_process( pid_fallback: int, ) -> tuple[bool, str | None, int | None]: launch_started = time.monotonic() - process = self._start_process( - command, - cwd=str(launch.cwd), - env=self.command_env_builder(port=launch_port, extra=env_extra), - log_path=launch.log_path, - ) - self._emit_attach_phase(service_name, "process_launch", launch_started) + service_env = self.command_env_builder(port=launch_port, extra=env_extra) + if self.docker_mode or docker_service_mode_enabled(self.runtime): + try: + container_launch = DockerServiceRuntime( + self.runtime, + self.process_runtime, + refresh_cache=self.refresh_cache, + ).launch( + project_name=self.project_name, + project_root=self.project_root, + service_name=service_name, + cwd=launch.cwd, + command=command, + env=service_env, + host_port=event_port, + container_port=launch_port, + listener_expected=launch.listener_expected, + log_path=launch.log_path, + ) + except RuntimeError as exc: + self._emit_attach_phase(service_name, "container_launch", launch_started) + return False, str(exc), None + self._container_launches[service_name] = container_launch + process_pid = None + phase = "container_launch" + else: + process = self._start_process( + command, + cwd=str(launch.cwd), + env=service_env, + log_path=launch.log_path, + ) + process_pid = getattr(process, "pid", pid_fallback) + phase = "process_launch" + self._emit_attach_phase(service_name, phase, launch_started) self.runtime._emit( "service.start", project=self.project_name, @@ -214,7 +258,52 @@ def _launch_prepared_process( port=event_port, retry=False, ) - return True, None, getattr(process, "pid", pid_fallback) + return True, None, process_pid + + def _container_actual_port( + self, + service_name: str, + requested: int, + *, + listener_expected: bool = True, + ) -> int | None: + launch = self._container_launches.get(service_name) + if launch is None: + return None + ready = DockerServiceRuntime(self.runtime, self.process_runtime).wait_until_ready( + launch, + port=requested, + listener_expected=listener_expected, + ) + if ready: + return requested if listener_expected else None + detail = f"container {launch.container_name} exited or did not become ready" + self.runtime._emit( + "service.failure", + project=self.project_name, + service=service_name, + failure_class="container_not_ready", + requested_port=requested if listener_expected else None, + detail=detail, + ) + raise RuntimeError(detail) + + def _annotate_container_records(self, records: dict[str, ServiceRecord]) -> None: + for record in records.values(): + service_name = str(record.service_slug or record.type) + launch = self._container_launches.get(service_name) + if launch is None: + continue + record.runtime_kind = "docker" + record.container_id = launch.container_id + record.container_name = launch.container_name + record.container_image = launch.image + + def _cleanup_container_launches(self) -> None: + docker_runtime = DockerServiceRuntime(self.runtime, self.process_runtime) + for launch in tuple(self._container_launches.values()): + docker_runtime.stop(launch.container_id) + self._container_launches.clear() def start_backend(self, port: int) -> tuple[bool, str | None, int | None]: conflict_result = self._conflicting_start_result("backend", port) @@ -273,16 +362,23 @@ def start_additional_service(self, service_name: str, port: int) -> tuple[bool, if service is None: return False, f"unknown additional service {service_name}", None command_resolve_started = time.monotonic() - command = self.runtime._split_command( - service.start_cmd, - port=port, - replacements={ - "project_root": str(self.project_root), - "service_dir": str(launch.cwd), - "service_name": service_name, - }, - cwd=launch.cwd, - ) + if docker_service_uses_image_command( + self.runtime, + service_name, + docker_mode=self.docker_mode or docker_service_mode_enabled(self.runtime), + ): + command = [] + else: + command = self.runtime._split_command( + service.start_cmd, + port=port, + replacements={ + "project_root": str(self.project_root), + "service_dir": str(launch.cwd), + "service_name": service_name, + }, + cwd=launch.cwd, + ) self._emit_attach_phase(service_name, "command_resolution", command_resolve_started) return self._launch_prepared_process( service_name=service_name, @@ -295,6 +391,12 @@ def start_additional_service(self, service_name: str, port: int) -> tuple[bool, ) def detect_backend_actual(self, pid: int | None, requested: int) -> int | None: + if "backend" in self._container_launches: + return self._container_actual_port( + "backend", + requested, + listener_expected=self.backend_listener_expected, + ) if not self.backend_listener_expected: self.runtime._emit( "service.bind.skipped", @@ -334,6 +436,9 @@ def detect_backend_actual(self, pid: int | None, requested: int) -> int | None: return actual def detect_frontend_actual(self, pid: int | None, requested: int) -> int | None: + container_actual = self._container_actual_port("frontend", requested) + if "frontend" in self._container_launches: + return container_actual self.runtime._emit( "service.bind.requested", project=self.project_name, @@ -374,6 +479,12 @@ def detect_frontend_actual(self, pid: int | None, requested: int) -> int | None: def detect_additional_actual(self, service_name: str, pid: int | None, requested: int) -> int | None: launch = self.prepared_launches[service_name] + if service_name in self._container_launches: + return self._container_actual_port( + service_name, + requested, + listener_expected=launch.listener_expected, + ) if not launch.listener_expected: self.runtime._emit( "service.bind.skipped", diff --git a/python/envctl_engine/startup/service_project_execution.py b/python/envctl_engine/startup/service_project_execution.py index 5602ed65c..3a2c04d30 100644 --- a/python/envctl_engine/startup/service_project_execution.py +++ b/python/envctl_engine/startup/service_project_execution.py @@ -6,6 +6,7 @@ from typing import cast from envctl_engine.runtime.command_resolution import CommandExists, suggest_service_start_command +from envctl_engine.runtime.docker_service_runtime import docker_service_mode_enabled from envctl_engine.runtime.command_router import Route from envctl_engine.runtime.runtime_context import resolve_process_runtime from envctl_engine.startup.startup_selection_support import _restart_service_types_for_project @@ -302,6 +303,7 @@ def project_env_for_service(service_name: str) -> dict[str, str]: return {} service_started = time.monotonic() + use_docker_services = bool(route is not None and route.flags.get("docker")) or docker_service_mode_enabled(rt) prepare_backend_duration_ms = 0.0 prepare_frontend_duration_ms = 0.0 prepare_backend = "backend" in selected_service_types @@ -320,28 +322,34 @@ def project_env_for_service(service_name: str) -> dict[str, str]: def prepare_backend_runtime() -> float: started = time.monotonic() - rt._prepare_backend_runtime( - context=context, - backend_cwd=backend_cwd, - backend_log_path=backend_log_path, - project_env_base=project_env_internal, - route=route, - backend_env_file=backend_env_file, - backend_env_is_default=backend_env_is_default, - ) + if use_docker_services: + rt._emit("service.prepare.skipped", project=context.name, service="backend", reason="docker_image") + else: + rt._prepare_backend_runtime( + context=context, + backend_cwd=backend_cwd, + backend_log_path=backend_log_path, + project_env_base=project_env_internal, + route=route, + backend_env_file=backend_env_file, + backend_env_is_default=backend_env_is_default, + ) return round((time.monotonic() - started) * 1000.0, 2) def prepare_frontend_runtime() -> float: started = time.monotonic() - rt._prepare_frontend_runtime( - context=context, - frontend_cwd=frontend_cwd, - frontend_log_path=frontend_log_path, - project_env_base=frontend_project_env_base, - frontend_env_file=frontend_env_file, - backend_port=backend_plan.final, - route=route, - ) + if use_docker_services: + rt._emit("service.prepare.skipped", project=context.name, service="frontend", reason="docker_image") + else: + rt._prepare_frontend_runtime( + context=context, + frontend_cwd=frontend_cwd, + frontend_log_path=frontend_log_path, + project_env_base=frontend_project_env_base, + frontend_env_file=frontend_env_file, + backend_port=backend_plan.final, + route=route, + ) return round((time.monotonic() - started) * 1000.0, 2) if prepare_backend and prepare_frontend and prep_parallel: @@ -501,6 +509,8 @@ def prepare_frontend_runtime() -> float: additional_services=tuple(additional_services), backend_listener_expected=backend_listener_expected, rebound_delta=rebound_delta, + docker_mode=use_docker_services, + refresh_cache=bool(route is not None and route.flags.get("refresh_cache")), ).start(attach_parallel=attach_parallel, on_service_retry=on_service_retry) attach_duration_ms = round((time.monotonic() - attach_started) * 1000.0, 2) diff --git a/python/envctl_engine/state/__init__.py b/python/envctl_engine/state/__init__.py index 37b34ba27..33cc97981 100644 --- a/python/envctl_engine/state/__init__.py +++ b/python/envctl_engine/state/__init__.py @@ -43,6 +43,10 @@ def load_state(path: str, *, allowed_root: str | None = None) -> RunState: failure_detail=str(svc.get("failure_detail") or "") or None, critical=parse_bool(svc.get("critical"), True), degraded=parse_bool(svc.get("degraded"), False), + runtime_kind=str(svc.get("runtime_kind") or "process"), + container_id=str(svc.get("container_id") or "") or None, + container_name=str(svc.get("container_name") or "") or None, + container_image=str(svc.get("container_image") or "") or None, ) for name, svc in data.get("services", {}).items() } @@ -182,6 +186,10 @@ def state_to_dict(state: RunState) -> dict[str, object]: "failure_detail": svc.failure_detail, "critical": svc.critical, "degraded": svc.degraded, + "runtime_kind": svc.runtime_kind, + "container_id": svc.container_id, + "container_name": svc.container_name, + "container_image": svc.container_image, } for name, svc in state.services.items() }, diff --git a/python/envctl_engine/state/action_command_support.py b/python/envctl_engine/state/action_command_support.py index c5e0708d4..690f28570 100644 --- a/python/envctl_engine/state/action_command_support.py +++ b/python/envctl_engine/state/action_command_support.py @@ -2,10 +2,17 @@ from dataclasses import dataclass, field from typing import Any, cast +from contextlib import contextmanager import json +import signal +import threading from envctl_engine.runtime.browser_diagnostics import build_runtime_diagnostics from envctl_engine.runtime.command_router import Route +from envctl_engine.runtime.docker_service_runtime import ( + refresh_container_log_snapshots, + stop_container_log_followers, +) from envctl_engine.shared.parsing import parse_float_or_none, parse_int from envctl_engine.state.action_log_support import DEFAULT_LOG_ISSUE_LIMIT from envctl_engine.state.models import RunState @@ -25,6 +32,26 @@ class StateActionReconciliation: reconciled: bool = False +@contextmanager +def _interrupt_log_follow_on_termination(enabled: bool): + if not enabled or threading.current_thread() is not threading.main_thread(): + yield + return + previous: dict[signal.Signals, Any] = {} + + def interrupt(_signum: int, _frame: object) -> None: + raise KeyboardInterrupt + + for signal_name in (signal.SIGHUP, signal.SIGTERM): + previous[signal_name] = signal.getsignal(signal_name) + signal.signal(signal_name, interrupt) + try: + yield + finally: + for signal_name, handler in previous.items(): + signal.signal(signal_name, handler) + + class StateActionCommandRunner: def __init__(self, orchestrator: Any) -> None: self.orchestrator = orchestrator @@ -200,6 +227,12 @@ def _run_logs(self, route: Route) -> int: if duration_seconds is not None and duration_seconds < 0: duration_seconds = 0.0 no_color = bool(route.flags.get("logs_no_color")) or self.orchestrator._human_output_no_color(route) + follower_pids = refresh_container_log_snapshots( + self.runtime.raw_runtime, + self.state, + tail=max(tail, 0), + follow=follow and not bool(route.flags.get("json")), + ) if bool(route.flags.get("json")): print( json.dumps( @@ -215,13 +248,17 @@ def _run_logs(self, route: Route) -> int: ) ) return self._finish_action(0) - self.runtime.print_logs( - self.state, - tail=max(tail, 0), - follow=follow, - duration_seconds=duration_seconds, - no_color=no_color, - ) + try: + with _interrupt_log_follow_on_termination(bool(follower_pids)): + self.runtime.print_logs( + self.state, + tail=max(tail, 0), + follow=follow, + duration_seconds=duration_seconds, + no_color=no_color, + ) + finally: + stop_container_log_followers(self.runtime.raw_runtime, follower_pids) return self._finish_action(0) def _run_clear_logs(self, route: Route) -> int: @@ -450,6 +487,12 @@ def _run_errors(self, route: Route) -> int: parse_int(str(route.flags.get("logs_tail", DEFAULT_LOG_ISSUE_LIMIT)), DEFAULT_LOG_ISSUE_LIMIT), 0, ) + refresh_container_log_snapshots( + self.runtime.raw_runtime, + self.state, + tail=log_issue_limit, + follow=False, + ) log_issues = self.orchestrator._service_log_issues(self.state, max_matches=log_issue_limit) has_errors = bool(failed or reconciliation.requirement_issues or reconciliation.recent_failures or log_issues) if bool(route.flags.get("json")): diff --git a/python/envctl_engine/state/action_health_support.py b/python/envctl_engine/state/action_health_support.py index d61667e3f..fae8e9f05 100644 --- a/python/envctl_engine/state/action_health_support.py +++ b/python/envctl_engine/state/action_health_support.py @@ -205,6 +205,10 @@ def _service_row(service: ServiceRecord) -> dict[str, object]: "failure_detail": getattr(service, "failure_detail", None), "critical": getattr(service, "critical", True), "degraded": getattr(service, "degraded", False), + "runtime_kind": getattr(service, "runtime_kind", "process"), + "container_id": getattr(service, "container_id", None), + "container_name": getattr(service, "container_name", None), + "container_image": getattr(service, "container_image", None), } rows = self._parallel_service_map( diff --git a/python/envctl_engine/state/action_log_support.py b/python/envctl_engine/state/action_log_support.py index 851e6646e..b73a67e63 100644 --- a/python/envctl_engine/state/action_log_support.py +++ b/python/envctl_engine/state/action_log_support.py @@ -219,6 +219,8 @@ def clear_one(service: ServiceRecord) -> tuple[int, int, int, int, str | None]: try: with log_path.open("w", encoding="utf-8"): pass + if str(getattr(service, "runtime_kind", "process") or "process").lower() == "docker": + Path(f"{log_path}.docker-since").touch() line = f"{service.name}: log cleared at {log_path}" return 1, 0, 0, 0, ( None diff --git a/python/envctl_engine/state/action_orchestrator.py b/python/envctl_engine/state/action_orchestrator.py index a0989e80c..1c65e2753 100644 --- a/python/envctl_engine/state/action_orchestrator.py +++ b/python/envctl_engine/state/action_orchestrator.py @@ -216,24 +216,31 @@ def _resolve_selected_services(self, route: Route, state: RunState) -> set[str] services_flag = route.flags.get("services") project_filters = {name.lower() for name in route.projects} project_filters.update(self.runtime.selectors_from_passthrough(route.passthrough_args)) - selected: set[str] = set() + service_selected: set[str] | None = None + project_selected: set[str] | None = None if isinstance(services_flag, list): + service_selected = set() for raw in services_flag: target = str(raw).strip() if not target: continue for name, service in state.services.items(): if service_matches_selector(service, target): - selected.add(name) + service_selected.add(name) if project_filters: + project_selected = set() for name in state.services: project = self.runtime.project_name_from_service(name).lower() if project and project in project_filters: - selected.add(name) - - if selected: - return selected + project_selected.add(name) + + if service_selected is not None and project_selected is not None: + return service_selected.intersection(project_selected) + if service_selected is not None: + return service_selected + if project_selected is not None: + return project_selected if project_filters or (isinstance(services_flag, list) and services_flag): return set() return None diff --git a/python/envctl_engine/state/models.py b/python/envctl_engine/state/models.py index cfcab4405..7bd715f94 100644 --- a/python/envctl_engine/state/models.py +++ b/python/envctl_engine/state/models.py @@ -37,6 +37,10 @@ class ServiceRecord: failure_detail: str | None = None critical: bool = True degraded: bool = False + runtime_kind: str = "process" + container_id: str | None = None + container_name: str | None = None + container_image: str | None = None @dataclass(slots=True) diff --git a/python/envctl_engine/state/runtime_map.py b/python/envctl_engine/state/runtime_map.py index a3eac6dad..218974297 100644 --- a/python/envctl_engine/state/runtime_map.py +++ b/python/envctl_engine/state/runtime_map.py @@ -86,6 +86,10 @@ def build_runtime_map_without_projection(state: RunState, *, host: str = "localh "failure_detail": getattr(service, "failure_detail", None), "critical": getattr(service, "critical", True), "degraded": getattr(service, "degraded", False), + "runtime_kind": getattr(service, "runtime_kind", "process"), + "container_id": getattr(service, "container_id", None), + "container_name": getattr(service, "container_name", None), + "container_image": getattr(service, "container_image", None), } if service.type == "backend": project_entry["backend_port"] = port diff --git a/tests/python/runtime/test_docker_service_runtime.py b/tests/python/runtime/test_docker_service_runtime.py new file mode 100644 index 000000000..0e01dea3d --- /dev/null +++ b/tests/python/runtime/test_docker_service_runtime.py @@ -0,0 +1,307 @@ +from __future__ import annotations + +from pathlib import Path +from types import SimpleNamespace + +from envctl_engine.runtime.docker_service_runtime import ( + DockerServiceRuntime, + docker_service_container_name, + refresh_container_log_snapshots, + stop_container_log_followers, +) +from envctl_engine.runtime.lifecycle_service_termination import terminate_service_record +from envctl_engine.runtime.service_status_truth import service_truth_status +from envctl_engine.state.models import ServiceRecord + + +class _Runner: + def __init__(self) -> None: + self.calls: list[list[str]] = [] + self.env_file_payload = "" + self.running = True + self.port_ready = True + self.terminated: list[int] = [] + self.build_env: dict[str, str] = {} + self.existing_container: tuple[bool, str] | None = None + + def run(self, command, **_kwargs): + argv = [str(part) for part in command] + self.calls.append(argv) + if argv[:2] == ["docker", "build"]: + self.build_env = dict(_kwargs.get("env", {})) + if argv[:2] == ["docker", "run"]: + env_file = Path(argv[argv.index("--env-file") + 1]) + self.env_file_payload = env_file.read_text(encoding="utf-8") + return SimpleNamespace(returncode=0, stdout="container-id\n", stderr="") + if argv[:3] == ["docker", "container", "inspect"]: + if self.existing_container is None: + return SimpleNamespace(returncode=1, stdout="", stderr="No such container") + running, owner = self.existing_container + return SimpleNamespace(returncode=0, stdout=f"{str(running).lower()}|{owner}\n", stderr="") + if argv[:3] == ["docker", "inspect", "--format"]: + return SimpleNamespace(returncode=0 if self.running else 1, stdout="true\n" if self.running else "", stderr="") + if argv[:3] == ["docker", "image", "inspect"]: + return SimpleNamespace(returncode=1, stdout="", stderr="missing") + if argv[:3] == ["docker", "rm", "--force"]: + return SimpleNamespace(returncode=0, stdout="", stderr="") + if argv[:2] == ["docker", "logs"]: + return SimpleNamespace(returncode=0, stdout="container log\n", stderr="") + return SimpleNamespace(returncode=0, stdout="ok\n", stderr="") + + def start_background(self, command, **_kwargs): + self.calls.append([str(part) for part in command]) + return SimpleNamespace(pid=4321) + + def wait_for_port(self, _port, **_kwargs): + return self.port_ready + + def terminate_process_group(self, pid, **_kwargs): + self.terminated.append(pid) + return True + + +def _runtime(tmp_path: Path, runner: _Runner, *, env: dict[str, str] | None = None): + events: list[tuple[str, dict[str, object]]] = [] + runtime = SimpleNamespace( + env={"DOCKER_MODE": "true", **(env or {})}, + config=SimpleNamespace(raw={}, runtime_scope_dir=tmp_path / "runtime-scope"), + process_runner=runner, + _command_exists=lambda command: command == "docker", + _run_dir_path=lambda run_id: tmp_path / "runs" / str(run_id), + _emit=lambda event, **payload: events.append((event, payload)), + _service_truth_timeout=lambda: 0.01, + ) + return runtime, events + + +def test_container_names_preserve_root_identity_for_long_project_names(tmp_path: Path) -> None: + first = docker_service_container_name( + project_name="feature-" + ("very-long-name-" * 8), + project_root=tmp_path / "one", + service_name="backend", + ) + second = docker_service_container_name( + project_name="feature-" + ("very-long-name-" * 8), + project_root=tmp_path / "two", + service_name="backend", + ) + + assert len(first) <= 63 + assert len(second) <= 63 + assert first != second + + +def test_builds_with_docker_layer_cache_and_runs_managed_container(tmp_path: Path) -> None: + runner = _Runner() + runtime, events = _runtime(tmp_path, runner, env={"ENVCTL_BACKEND_DOCKER_COMMAND_MODE": "service"}) + service_root = tmp_path / "backend" + service_root.mkdir() + (service_root / "Dockerfile").write_text("FROM python:3.12-slim\nWORKDIR /app\n", encoding="utf-8") + + launch = DockerServiceRuntime(runtime, runner).launch( + project_name="Feature One", + project_root=tmp_path, + service_name="backend", + cwd=service_root, + command=["python", "-m", "uvicorn", "app:api", "--host", "127.0.0.1", "--port", "8123"], + env={ + "PATH": "/host/bin", + "DATABASE_URL": "postgres://u:p@127.0.0.1:5432/db", + "PORT": "8123", + "VITE_API_URL": "http://localhost:8123/api/v1", + }, + host_port=8123, + container_port=8123, + listener_expected=True, + log_path=str(tmp_path / "backend.log"), + ) + + build_index = next(index for index, call in enumerate(runner.calls) if call[:2] == ["docker", "build"]) + run_index = next(index for index, call in enumerate(runner.calls) if call[:2] == ["docker", "run"]) + assert build_index < run_index + docker_build = runner.calls[build_index] + assert docker_build[docker_build.index("--build-arg") + 1] == "VITE_API_URL" + assert "http://localhost:8123/api/v1" not in docker_build + assert runner.build_env["VITE_API_URL"] == "http://localhost:8123/api/v1" + docker_run = runner.calls[run_index] + publish_index = docker_run.index("--publish") + assert docker_run[publish_index : publish_index + 2] == ["--publish", "127.0.0.1:8123:8123"] + assert "0.0.0.0" in docker_run + runtime_label_index = docker_run.index("io.envctl.service=backend") + 2 + assert docker_run[runtime_label_index].startswith("io.envctl.runtime-scope=") + assert "PATH=" not in runner.env_file_payload + assert "DATABASE_URL=postgres://u:p@host.docker.internal:5432/db" in runner.env_file_payload + assert launch.container_id == "container-id" + assert any(event == "service.container.build" and payload["cache"] == "docker_layer_cache" for event, payload in events) + + +def test_running_untracked_container_is_not_replaced_or_removed(tmp_path: Path) -> None: + runner = _Runner() + runtime, _events = _runtime(tmp_path, runner, env={"ENVCTL_BACKEND_DOCKER_IMAGE": "example/backend:dev"}) + docker_runtime = DockerServiceRuntime(runtime, runner) + runner.existing_container = (True, docker_runtime._runtime_scope_identity()) + + try: + docker_runtime.launch( + project_name="Main", + project_root=tmp_path, + service_name="backend", + cwd=tmp_path, + command=[], + env={}, + host_port=8123, + container_port=8123, + listener_expected=True, + log_path=str(tmp_path / "backend.log"), + ) + except RuntimeError as exc: + assert "already running" in str(exc) + else: + raise AssertionError("expected the untracked running container to block startup") + + assert not any(call[:3] == ["docker", "rm", "--force"] for call in runner.calls) + assert not any(call[:2] == ["docker", "run"] for call in runner.calls) + + +def test_container_owned_by_another_runtime_scope_is_not_replaced(tmp_path: Path) -> None: + runner = _Runner() + runtime, _events = _runtime(tmp_path, runner, env={"ENVCTL_BACKEND_DOCKER_IMAGE": "example/backend:dev"}) + runner.existing_container = (False, "another-runtime") + + try: + DockerServiceRuntime(runtime, runner).launch( + project_name="Main", + project_root=tmp_path, + service_name="backend", + cwd=tmp_path, + command=[], + env={}, + host_port=8123, + container_port=8123, + listener_expected=True, + log_path=str(tmp_path / "backend.log"), + ) + except RuntimeError as exc: + assert "another envctl runtime scope" in str(exc) + else: + raise AssertionError("expected the foreign runtime scope to block startup") + + assert not any(call[:3] == ["docker", "rm", "--force"] for call in runner.calls) + + +def test_stopped_container_from_same_runtime_scope_is_removed(tmp_path: Path) -> None: + runner = _Runner() + runtime, _events = _runtime(tmp_path, runner, env={"ENVCTL_BACKEND_DOCKER_IMAGE": "example/backend:dev"}) + docker_runtime = DockerServiceRuntime(runtime, runner) + runner.existing_container = (False, docker_runtime._runtime_scope_identity()) + + docker_runtime.launch( + project_name="Main", + project_root=tmp_path, + service_name="backend", + cwd=tmp_path, + command=[], + env={}, + host_port=8123, + container_port=8123, + listener_expected=True, + log_path=str(tmp_path / "backend.log"), + ) + + inspect_index = next(index for index, call in enumerate(runner.calls) if call[:3] == ["docker", "container", "inspect"]) + remove_index = next(index for index, call in enumerate(runner.calls) if call[:3] == ["docker", "rm", "--force"]) + run_index = next(index for index, call in enumerate(runner.calls) if call[:2] == ["docker", "run"]) + assert inspect_index < remove_index < run_index + + +def test_configured_image_runs_without_build_and_preserves_browser_public_url(tmp_path: Path) -> None: + runner = _Runner() + runtime, _events = _runtime( + tmp_path, + runner, + env={ + "ENVCTL_FRONTEND_DOCKER_IMAGE": "example/frontend:dev", + "ENVCTL_FRONTEND_DOCKER_COMMAND": "npm run dev -- --host 0.0.0.0 --port 9000", + "ENVCTL_FRONTEND_DOCKER_PORT": "9000", + }, + ) + + launch = DockerServiceRuntime(runtime, runner).launch( + project_name="Main", + project_root=tmp_path, + service_name="frontend", + cwd=tmp_path, + command=["host-only-command"], + env={ + "VITE_API_URL": "http://localhost:8000/api/v1", + "REDIS_URL": "redis://localhost:6379", + "PORT": "9100", + "HOST": "127.0.0.1", + }, + host_port=9100, + container_port=9100, + listener_expected=True, + log_path=str(tmp_path / "frontend.log"), + ) + + assert not any(call[:2] == ["docker", "build"] for call in runner.calls) + docker_run = next(call for call in runner.calls if call[:2] == ["docker", "run"]) + assert "example/frontend:dev" in docker_run + assert "127.0.0.1:9100:9000" in docker_run + assert docker_run[-8:] == ["npm", "run", "dev", "--", "--host", "0.0.0.0", "--port", "9000"] + assert "VITE_API_URL=http://localhost:8000/api/v1" in runner.env_file_payload + assert "REDIS_URL=redis://host.docker.internal:6379" in runner.env_file_payload + assert "PORT=9000" in runner.env_file_payload + assert "HOST=0.0.0.0" in runner.env_file_payload + assert launch.image == "example/frontend:dev" + + +def test_container_state_drives_health_and_stop(tmp_path: Path) -> None: + runner = _Runner() + runtime, events = _runtime(tmp_path, runner) + service = ServiceRecord( + name="Main Backend", + type="backend", + cwd=str(tmp_path), + pid=4321, + requested_port=8123, + actual_port=8123, + status="running", + runtime_kind="docker", + container_id="container-id", + container_name="envctl-app-main-backend", + container_image="example/backend:dev", + ) + + assert service_truth_status(runtime, service) == "running" + runner.port_ready = False + assert service_truth_status(runtime, service) == "unreachable" + assert terminate_service_record(runtime, service, aggressive=False, verify_ownership=True) + assert ["docker", "rm", "--force", "container-id"] in runner.calls + assert runner.terminated == [4321] + assert any(event == "service.container.stop" for event, _payload in events) + + +def test_container_logs_are_snapshotted_and_followed_only_on_demand(tmp_path: Path) -> None: + runner = _Runner() + runtime, _events = _runtime(tmp_path, runner) + log_path = tmp_path / "backend.log" + service = ServiceRecord( + name="Main Backend", + type="backend", + cwd=str(tmp_path), + log_path=str(log_path), + runtime_kind="docker", + container_id="container-id", + ) + state = SimpleNamespace(services={service.name: service}) + + assert refresh_container_log_snapshots(runtime, state, tail=20, follow=False) == [] + assert log_path.read_text(encoding="utf-8") == "container log\n" + assert not any(call[:3] == ["docker", "logs", "--follow"] for call in runner.calls) + + followers = refresh_container_log_snapshots(runtime, state, tail=20, follow=True) + assert followers == [4321] + assert any(call[:3] == ["docker", "logs", "--follow"] for call in runner.calls) + stop_container_log_followers(runtime, followers) + assert runner.terminated == [4321] diff --git a/tests/python/runtime/test_engine_runtime_artifacts.py b/tests/python/runtime/test_engine_runtime_artifacts.py index cf3973fee..879bf3771 100644 --- a/tests/python/runtime/test_engine_runtime_artifacts.py +++ b/tests/python/runtime/test_engine_runtime_artifacts.py @@ -21,7 +21,7 @@ ) from envctl_engine.runtime.engine_runtime_event_support import persist_events_snapshot # noqa: E402 from envctl_engine.runtime.runtime_readiness import RuntimeReadinessResult # noqa: E402 -from envctl_engine.state.models import PortPlan, RunState # noqa: E402 +from envctl_engine.state.models import PortPlan, RunState, ServiceRecord # noqa: E402 class EngineRuntimeArtifactsTests(unittest.TestCase): @@ -59,6 +59,39 @@ def test_print_summary_includes_project_ports(self) -> None: self.assertIn("envctl Python engine run summary", rendered) self.assertIn("- Main: backend=8000 frontend=9000 db=5432 redis=6379 n8n=5678", rendered) + def test_print_summary_prefers_preserved_service_truth_over_unused_planned_port(self) -> None: + context = SimpleNamespace( + name="Main", + ports={ + name: PortPlan(project="Main", requested=port, assigned=port, final=port, source="fixed") + for name, port in { + "backend": 8001, + "frontend": 9001, + "db": 5432, + "redis": 6379, + "n8n": 5678, + }.items() + }, + ) + state = RunState( + run_id="run-restart", + mode="main", + services={ + "Main Backend": ServiceRecord( + name="Main Backend", type="backend", cwd="/repo/backend", actual_port=8001 + ), + "Main Frontend": ServiceRecord( + name="Main Frontend", type="frontend", cwd="/repo/frontend", actual_port=9000 + ), + }, + ) + + buffer = io.StringIO() + with redirect_stdout(buffer): + print_summary(SimpleNamespace(), state, [context]) + + self.assertIn("- Main: backend=8001 frontend=9000", buffer.getvalue()) + def test_write_artifacts_forwards_expected_state_repository_payload(self) -> None: captured: dict[str, object] = {} diff --git a/tests/python/runtime/test_engine_runtime_commands.py b/tests/python/runtime/test_engine_runtime_commands.py index 9b1af759b..6730825d6 100644 --- a/tests/python/runtime/test_engine_runtime_commands.py +++ b/tests/python/runtime/test_engine_runtime_commands.py @@ -14,11 +14,63 @@ command_env, command_override_value, default_python_executable, + service_start_command_resolved, split_command, ) class EngineRuntimeCommandsTests(unittest.TestCase): + def test_docker_image_command_does_not_require_container_executable_on_host(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + (root / "frontend").mkdir() + runtime = SimpleNamespace( + env={ + "DOCKER_MODE": "true", + "ENVCTL_FRONTEND_START_CMD": "container-only-server --port {port}", + }, + config=SimpleNamespace( + base_dir=root, + raw={"FRONTEND_DIR": "frontend"}, + ), + _command_exists=lambda _executable: False, + ) + + command, source = service_start_command_resolved( + runtime, + service_name="frontend", + project_root=root, + port=8010, + ) + + self.assertEqual(command, ["container-only-server", "--port", "8010"]) + self.assertEqual(source, "configured") + + def test_docker_service_command_still_requires_executable_on_host(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + (root / "frontend").mkdir() + runtime = SimpleNamespace( + env={ + "DOCKER_MODE": "true", + "ENVCTL_FRONTEND_DOCKER_COMMAND_MODE": "service", + "ENVCTL_FRONTEND_START_CMD": "missing-command --port {port}", + }, + config=SimpleNamespace( + base_dir=root, + raw={"FRONTEND_DIR": "frontend"}, + ), + _command_exists=lambda _executable: False, + ) + + with self.assertRaises(RuntimeError): + service_start_command_resolved( + runtime, + service_name="frontend", + project_root=root, + port=8010, + ) + def test_command_override_value_prefers_runtime_env(self) -> None: runtime = SimpleNamespace( env={"DB_HOST": "env-db"}, diff --git a/tests/python/runtime/test_engine_runtime_startup_support.py b/tests/python/runtime/test_engine_runtime_startup_support.py index ca8a73908..b86206be0 100644 --- a/tests/python/runtime/test_engine_runtime_startup_support.py +++ b/tests/python/runtime/test_engine_runtime_startup_support.py @@ -319,6 +319,41 @@ def test_run_reuse_rejects_startup_fingerprint_mismatch(self) -> None: self.assertEqual(decision.decision_kind, "fresh_run") self.assertEqual(decision.reason, "startup_fingerprint_mismatch") + def test_explicit_no_resume_retains_candidate_for_safe_replacement(self) -> None: + context = ProjectContext(name="Main", root=Path("/repo"), ports={}) + route = Route( + command="start", + mode="main", + raw_args=["start", "--no-resume"], + passthrough_args=[], + projects=[], + flags={"no_resume": True}, + ) + state = RunState( + run_id="run-existing", + mode="main", + services={ + "Main Backend": ServiceRecord( + name="Main Backend", + type="backend", + cwd="/repo/backend", + ) + }, + metadata={"project_roots": {"Main": "/repo"}}, + ) + runtime = self._reuse_runtime(state=state) + + decision = startup_support.evaluate_run_reuse( + runtime, + runtime_mode="main", + route=route, + contexts=[context], + ) + + self.assertEqual(decision.decision_kind, "fresh_run") + self.assertEqual(decision.reason, "explicit_no_resume") + self.assertIs(decision.candidate_state, state) + def test_run_reuse_rejects_dependency_external_mode_mismatch(self) -> None: context = ProjectContext(name="Main", root=Path("/repo"), ports={}) route = Route(command="start", mode="main", raw_args=[], passthrough_args=[], projects=[], flags={}) diff --git a/tests/python/runtime/test_lifecycle_dependency_stop.py b/tests/python/runtime/test_lifecycle_dependency_stop.py index 59d176d1e..9f0ab968d 100644 --- a/tests/python/runtime/test_lifecycle_dependency_stop.py +++ b/tests/python/runtime/test_lifecycle_dependency_stop.py @@ -1,6 +1,7 @@ from __future__ import annotations import unittest +from types import SimpleNamespace from envctl_engine.runtime.command_router import Route from envctl_engine.runtime.lifecycle_dependency_stop import ( @@ -8,11 +9,52 @@ release_selected_dependency_components, requirements_have_enabled_components, select_dependency_components_for_stop, + stop_requirement_component_containers, ) from envctl_engine.state.models import RequirementsResult, RunState class LifecycleDependencyStopTests(unittest.TestCase): + def test_entire_system_selects_all_enabled_dependencies_for_requested_project(self) -> None: + state = RunState( + run_id="run-entire", + mode="trees", + requirements={ + "Alpha": RequirementsResult(project="Alpha", redis={"enabled": True}, n8n={"enabled": True}), + "Beta": RequirementsResult(project="Beta", redis={"enabled": True}), + }, + ) + route = Route(command="stop", mode="trees", projects=["Alpha"], flags={"runtime_scope": "entire-system"}) + + self.assertEqual(select_dependency_components_for_stop(state, route), {"Alpha": {"redis", "n8n"}}) + + def test_stop_requirement_component_removes_direct_and_compose_containers(self) -> None: + calls: list[list[str]] = [] + + def run(command, **_kwargs): # noqa: ANN001 + argv = [str(part) for part in command] + calls.append(argv) + if argv[:3] == ["docker", "ps", "--all"]: + return SimpleNamespace(returncode=0, stdout="one\ntwo\n", stderr="") + return SimpleNamespace(returncode=0, stdout="", stderr="") + + runtime = SimpleNamespace(process_runner=SimpleNamespace(run=run)) + stop_requirement_component_containers( + runtime, + {"id": "redis", "enabled": True, "container_name": "envctl-redis-alpha"}, + ) + stop_requirement_component_containers( + runtime, + {"id": "supabase", "enabled": True, "container_name": "envctl-supabase-alpha"}, + ) + + self.assertIn(["docker", "rm", "--force", "envctl-redis-alpha"], calls) + self.assertIn( + ["docker", "ps", "--all", "--quiet", "--filter", "label=com.docker.compose.project=envctl-supabase-alpha"], + calls, + ) + self.assertIn(["docker", "rm", "--force", "one", "two"], calls) + def test_select_dependency_components_filters_unknown_projects_dependencies_and_disabled_components(self) -> None: state = RunState( run_id="run-1", diff --git a/tests/python/runtime/test_lifecycle_worktree_cleanup.py b/tests/python/runtime/test_lifecycle_worktree_cleanup.py index 79773b828..450579fe3 100644 --- a/tests/python/runtime/test_lifecycle_worktree_cleanup.py +++ b/tests/python/runtime/test_lifecycle_worktree_cleanup.py @@ -4,6 +4,7 @@ from pathlib import Path from types import SimpleNamespace +from envctl_engine.runtime.docker_service_runtime import docker_service_container_name from envctl_engine.runtime.lifecycle_worktree_containers import legacy_container_name, remove_tree_containers from envctl_engine.runtime.lifecycle_worktree_cleanup import WorktreeCleanupDependencies, blast_worktree_before_delete from envctl_engine.runtime.lifecycle_worktree_metadata import cleanup_artifact_paths, prune_project_metadata @@ -12,6 +13,42 @@ class LifecycleWorktreeCleanupTests(unittest.TestCase): + def test_remove_tree_containers_removes_managed_application_container(self) -> None: + calls: list[list[str]] = [] + project_root = Path(".").resolve() + container_name = docker_service_container_name( + project_name="Feature", + project_root=project_root, + service_name="backend", + ) + + class Runner: + def run(self, command: list[str], **kwargs: object) -> SimpleNamespace: + calls.append(command) + if command[1:4] == ["ps", "-a", "--format"]: + return SimpleNamespace(returncode=0, stdout=f"app-id|{container_name}\n", stderr="") + return SimpleNamespace(returncode=0, stdout="", stderr="") + + runtime = SimpleNamespace( + process_runner=Runner(), + env={}, + config=SimpleNamespace(additional_services=()), + _emit=lambda event, **payload: None, + ) + warnings: list[str] = [] + + remove_tree_containers( + runtime, + project_name="Feature", + project_root=project_root, + include_supabase=False, + remove_named_volumes=False, + warnings=warnings, + ) + + self.assertEqual(warnings, []) + self.assertIn(["docker", "rm", "-f", "-v", "app-id"], calls) + def test_blast_worktree_before_delete_updates_matching_project_state(self) -> None: events: list[tuple[str, dict[str, object]]] = [] saved_states: list[RunState] = [] diff --git a/tests/python/runtime/test_prereq_policy.py b/tests/python/runtime/test_prereq_policy.py index eeab8e36a..e9bbab344 100644 --- a/tests/python/runtime/test_prereq_policy.py +++ b/tests/python/runtime/test_prereq_policy.py @@ -13,6 +13,37 @@ class PrereqPolicyTests(unittest.TestCase): + def test_docker_application_mode_requires_docker_without_dependencies(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + repo = Path(tmpdir) / "repo" + runtime = Path(tmpdir) / "runtime" + (repo / ".git").mkdir(parents=True, exist_ok=True) + config = load_config( + { + "RUN_REPO_ROOT": str(repo), + "RUN_SH_RUNTIME_DIR": str(runtime), + "POSTGRES_MAIN_ENABLE": "false", + "REDIS_ENABLE": "false", + "SUPABASE_MAIN_ENABLE": "false", + "N8N_ENABLE": "false", + } + ) + route = parse_route(["start", "--main", "--docker"], env={}) + + def fake_which(binary: str) -> str | None: + if binary == "docker": + return None + return f"/usr/bin/{binary}" + + with ( + patch("envctl_engine.runtime.cli.shutil.which", side_effect=fake_which), + patch("envctl_engine.runtime.cli._python_dependency_available", return_value=True), + ): + ok, reason = cli.check_prereqs(route, config) + + self.assertFalse(ok) + self.assertIn("docker", str(reason).lower()) + def test_main_start_without_requirements_does_not_require_docker(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: repo = Path(tmpdir) / "repo" diff --git a/tests/python/startup/test_restart_prestop_support.py b/tests/python/startup/test_restart_prestop_support.py index 49bc46997..aca2119bb 100644 --- a/tests/python/startup/test_restart_prestop_support.py +++ b/tests/python/startup/test_restart_prestop_support.py @@ -218,6 +218,27 @@ def test_restart_prestop_state_rejects_mode_mismatch_and_builds_fallback_route(s ], ) + def test_restart_prestop_state_inherits_docker_runtime_from_saved_state(self) -> None: + route = Route(command="restart", mode="main", raw_args=["restart"], flags={}) + state = SimpleNamespace( + mode="main", + run_id="run-docker", + metadata={"application_runtime": "docker"}, + services={}, + ) + runtime = SimpleNamespace( + env={}, + _effective_start_mode=lambda route: "main", + _try_load_existing_state=lambda *, mode: state, + _emit=lambda event, **payload: None, + ) + + result = restart_prestop_state(route=route, runtime=runtime) + + self.assertIs(result.state, state) + self.assertTrue(route.flags["docker"]) + self.assertEqual(runtime.env["DOCKER_MODE"], "true") + def test_restart_prestop_preservation_splits_services_and_requirements(self) -> None: state = SimpleNamespace( services={ diff --git a/tests/python/startup/test_run_reuse_identity.py b/tests/python/startup/test_run_reuse_identity.py index 15cd8531d..56e9998d1 100644 --- a/tests/python/startup/test_run_reuse_identity.py +++ b/tests/python/startup/test_run_reuse_identity.py @@ -45,6 +45,7 @@ def test_build_startup_identity_metadata_records_roots_and_fingerprint_payload(s self.assertEqual(metadata["project_roots"], {"Main": normalize_project_root("/tmp/repo")}) identity = metadata["startup_identity"] self.assertEqual(identity["mode"], "main") + self.assertEqual(identity["application_runtime"], "process") self.assertEqual(identity["projects"], [{"name": "Main", "root": normalize_project_root("/tmp/repo")}]) self.assertEqual(identity["services"], {"backend": True, "frontend": False}) self.assertIn("fingerprint", identity) @@ -94,6 +95,21 @@ def test_startup_identity_mismatch_ignores_matching_fingerprints_and_reports_cha self.assertEqual(mismatch, {"fields": ["mode"]}) + self.assertEqual( + _startup_identity_mismatch( + {"mode": "main", "services": {"backend": True}}, + {"mode": "main", "services": {"backend": True}, "application_runtime": "process"}, + ), + {}, + ) + self.assertEqual( + _startup_identity_mismatch( + {"mode": "main", "application_runtime": "process"}, + {"mode": "main", "application_runtime": "docker"}, + ), + {"fields": ["application_runtime"]}, + ) + def test_root_mismatches_compare_only_strong_roots(self) -> None: selected = [ProjectIdentity("Main", "/repo"), ProjectIdentity("Weak", None)] state = [ProjectIdentity("Main", "/other"), ProjectIdentity("Weak", "/weak")] diff --git a/tests/python/startup/test_run_reuse_support.py b/tests/python/startup/test_run_reuse_support.py index 566c0b45e..4f80f1735 100644 --- a/tests/python/startup/test_run_reuse_support.py +++ b/tests/python/startup/test_run_reuse_support.py @@ -194,6 +194,31 @@ def test_fresh_start_replacement_services_selects_configured_service_types_for_t {"Main Backend"}, ) + def test_unfiltered_explicit_no_resume_replaces_tracked_removed_additional_services(self) -> None: + route = Route(command="start", mode="main", raw_args=[], flags={"no_resume": True}) + state = SimpleNamespace( + services={ + "Main Backend": SimpleNamespace(name="Main Backend", project="Main", type="backend"), + "Main Old Worker": SimpleNamespace( + name="Main Old Worker", + project="Main", + type="old-worker", + service_slug="old-worker", + ), + } + ) + + selected = fresh_start_replacement_services( + route=route, + selected_contexts=[SimpleNamespace(name="Main")], + candidate_state=state, + configured_service_types={"backend"}, + additional_services=(), + project_name_from_service=lambda _name: "Main", + ) + + self.assertEqual(selected, {"Main Backend", "Main Old Worker"}) + def test_replace_existing_project_services_for_fresh_start_terminates_selected_services_and_orphans(self) -> None: route = Route(command="start", mode="trees", raw_args=["start"], flags={}) session = SimpleNamespace(effective_route=route, runtime_mode="trees") @@ -242,6 +267,73 @@ def test_replace_existing_project_services_for_fresh_start_terminates_selected_s self.assertEqual(terminated, [(candidate_state, {"Main Backend", "Main Frontend"}, False, True)]) self.assertEqual(orphaned, [(candidate_state, {"Main Backend", "Main Frontend"}, True)]) + def test_explicit_no_resume_terminates_tracked_services_before_fresh_start(self) -> None: + route = Route(command="start", mode="main", raw_args=["start", "--no-resume"], flags={"no_resume": True}) + session = SimpleNamespace(effective_route=route, runtime_mode="main") + candidate_state = SimpleNamespace(run_id="run-existing") + progress: list[str] = [] + terminated: list[set[str]] = [] + + replace_existing_project_services_for_fresh_start( + runtime=SimpleNamespace( + _emit=lambda *args, **kwargs: None, + _terminate_services_from_state=lambda _state, *, selected_services, **_kwargs: terminated.append( + set(selected_services) + ), + ), + session=session, + candidate_state=candidate_state, + reason="explicit_no_resume", + fresh_start_replacement_services=lambda _session, *, candidate_state: {"Main Backend"}, + announce_session_identifiers=lambda _session: None, + report_progress=lambda _route, message: progress.append(message), + terminate_restart_orphan_listeners=lambda **_kwargs: None, + ) + + self.assertEqual(terminated, [{"Main Backend"}]) + self.assertEqual(progress, ["Fresh start requested; replacing 1 existing service(s)..."]) + + def test_explicit_no_resume_rebinds_reserved_plan_to_released_service_port(self) -> None: + route = Route(command="start", mode="main", raw_args=["start", "--no-resume"], flags={"no_resume": True}) + backend_plan = SimpleNamespace(assigned=8001, final=8001, source="retry") + context = SimpleNamespace(name="Main", ports={"backend": backend_plan}) + session = SimpleNamespace(effective_route=route, runtime_mode="main", selected_contexts=[context]) + service = SimpleNamespace( + name="Main Backend", + project="Main", + type="backend", + service_slug="backend", + listener_expected=True, + actual_port=8000, + requested_port=8000, + ) + candidate_state = SimpleNamespace(run_id="run-existing", services={"Main Backend": service}) + calls: list[tuple[str, int, str]] = [] + planner = SimpleNamespace( + release=lambda port, *, owner: calls.append(("release", port, owner)), + ) + runtime = SimpleNamespace( + port_planner=planner, + _emit=lambda *args, **kwargs: None, + _project_name_from_service=lambda _name: "Main", + _terminate_services_from_state=lambda *args, **kwargs: None, + ) + + replace_existing_project_services_for_fresh_start( + runtime=runtime, + session=session, + candidate_state=candidate_state, + reason="explicit_no_resume", + fresh_start_replacement_services=lambda _session, *, candidate_state: {"Main Backend"}, + announce_session_identifiers=lambda _session: None, + report_progress=lambda _route, _message: None, + terminate_restart_orphan_listeners=lambda **_kwargs: None, + ) + + self.assertEqual(calls, [("release", 8001, "Main:backend")]) + self.assertEqual((backend_plan.assigned, backend_plan.final), (8000, 8000)) + self.assertEqual(backend_plan.source, "fresh_start_replacement") + def test_replace_existing_project_services_for_fresh_start_skips_non_mismatch_or_dependencies_scope(self) -> None: runtime = SimpleNamespace(_emit=lambda *args, **kwargs: self.fail("replace should not emit")) candidate_state = SimpleNamespace(run_id="run-existing") diff --git a/tests/python/startup/test_service_attach_execution.py b/tests/python/startup/test_service_attach_execution.py index 0c61b31f4..c6001af36 100644 --- a/tests/python/startup/test_service_attach_execution.py +++ b/tests/python/startup/test_service_attach_execution.py @@ -5,7 +5,10 @@ from types import SimpleNamespace from typing import cast import unittest +from unittest.mock import patch +from envctl_engine.runtime.docker_service_runtime import DockerServiceLaunch +from envctl_engine.runtime.service_manager import ServiceManager from envctl_engine.runtime.service_manager import ServiceStartDescriptor from envctl_engine.startup.service_attach_execution import ServiceAttachRunner from envctl_engine.startup.service_execution_records import PreparedServiceLaunch @@ -17,6 +20,76 @@ def port_plan(port: int) -> PortPlan: class ServiceAttachExecutionTests(unittest.TestCase): + def test_explicit_docker_mode_launches_and_annotates_container_service(self) -> None: + project_root = Path("/tmp/envctl-project") + runtime = SimpleNamespace( + env={}, + config=SimpleNamespace(raw={}), + services=ServiceManager(), + _conflict_remaining={}, + _emit=lambda event, **payload: None, + _service_start_command_resolved=lambda service_name, project_root, port: ( + ["python", "app.py", "--port", str(port)], + "configured", + ), + ) + runner = ServiceAttachRunner( + runtime=runtime, + process_runtime=SimpleNamespace(), + port_allocator=SimpleNamespace(reserve_next=lambda port, owner: port), + project_name="Main", + project_root=project_root, + backend_plan=port_plan(8000), + frontend_plan=port_plan(5173), + backend_cwd=project_root / "backend", + frontend_cwd=project_root / "frontend", + backend_log_path="/logs/backend.txt", + frontend_log_path="/logs/frontend.txt", + backend_env_extra={"PORT": "8000"}, + frontend_env_extra={}, + command_env_builder=lambda port, extra: {**extra, "PORT": str(port)}, + prepared_launches={ + "backend": PreparedServiceLaunch( + service_name="backend", + cwd=project_root / "backend", + log_path="/logs/backend.txt", + requested_port=8000, + env={"PORT": "8000"}, + command_source="configured", + ) + }, + selected_service_types={"backend"}, + additional_services=(), + backend_listener_expected=True, + rebound_delta=0, + docker_mode=True, + ) + launch = DockerServiceLaunch( + container_id="container-id", + container_name="envctl-app-main-backend", + image="example/backend:dev", + log_path="/logs/backend.txt", + ) + + with ( + patch( + "envctl_engine.startup.service_attach_execution.DockerServiceRuntime.launch", + return_value=launch, + ) as docker_launch, + patch( + "envctl_engine.startup.service_attach_execution.DockerServiceRuntime.wait_until_ready", + return_value=True, + ), + ): + records = runner.start(attach_parallel=False, on_service_retry=lambda *args: None) + + record = records["Main Backend"] + self.assertEqual(record.runtime_kind, "docker") + self.assertEqual(record.container_id, "container-id") + self.assertEqual(record.container_name, "envctl-app-main-backend") + self.assertEqual(record.container_image, "example/backend:dev") + docker_launch.assert_called_once() + def test_runner_builds_layered_descriptors_and_preserves_additional_urls(self) -> None: project_root = Path("/tmp/envctl-project") events: list[tuple[str, dict[str, object]]] = [] diff --git a/tests/python/state/test_state_action_orchestrator_logs.py b/tests/python/state/test_state_action_orchestrator_logs.py index fb6056b21..1da01021b 100644 --- a/tests/python/state/test_state_action_orchestrator_logs.py +++ b/tests/python/state/test_state_action_orchestrator_logs.py @@ -3,6 +3,7 @@ from contextlib import redirect_stdout from io import StringIO import json +import signal import tempfile import unittest from types import SimpleNamespace @@ -15,6 +16,7 @@ from envctl_engine.runtime.command_router import Route from envctl_engine.runtime.engine_runtime_runtime_support import normalize_log_line from envctl_engine.state.models import RequirementsResult, RunState, ServiceRecord +from envctl_engine.state.action_command_support import _interrupt_log_follow_on_termination from envctl_engine.state.action_orchestrator import StateActionOrchestrator from envctl_engine.test_output.parser_base import strip_ansi from envctl_engine.ui.target_selector import TargetSelection @@ -75,6 +77,17 @@ def _emit(self, event: str, **payload: object) -> None: class StateActionOrchestratorLogsTests(unittest.TestCase): + def test_log_follow_termination_signal_interrupts_for_finally_cleanup(self) -> None: + previous = signal.getsignal(signal.SIGTERM) + + with self.assertRaises(KeyboardInterrupt): + with _interrupt_log_follow_on_termination(True): + handler = signal.getsignal(signal.SIGTERM) + self.assertTrue(callable(handler)) + handler(signal.SIGTERM, None) # type: ignore[operator] + + self.assertIs(signal.getsignal(signal.SIGTERM), previous) + def test_runtime_facade_routes_state_dependencies(self) -> None: state = RunState(run_id="run-0", mode="main") runtime = _RuntimeStub(state) @@ -146,6 +159,39 @@ def test_logs_service_filter_accepts_additional_service_slug_and_display_forms(s assert runtime.seen_logs_state is not None self.assertEqual(set(runtime.seen_logs_state.services), {"Main Voice Runtime"}) + def test_logs_combined_project_and_service_filters_intersect(self) -> None: + state = RunState( + run_id="run-combined-filter", + mode="trees", + services={ + "Alpha Backend": ServiceRecord( + name="Alpha Backend", type="backend", cwd="/tmp/alpha", project="Alpha" + ), + "Alpha Frontend": ServiceRecord( + name="Alpha Frontend", type="frontend", cwd="/tmp/alpha", project="Alpha" + ), + "Beta Backend": ServiceRecord( + name="Beta Backend", type="backend", cwd="/tmp/beta", project="Beta" + ), + }, + ) + runtime = _RuntimeStub(state) + orchestrator = StateActionOrchestrator(runtime) + route = Route( + command="logs", + mode="trees", + projects=["Alpha"], + flags={"services": ["backend"], "json": True}, + ) + + output = StringIO() + with redirect_stdout(output): + code = orchestrator.execute(route) + + payload = json.loads(output.getvalue()) + self.assertEqual(code, 0) + self.assertEqual([service["name"] for service in payload["services"]], ["Alpha Backend"]) + def test_health_prints_enabled_dependency_statuses(self) -> None: state = RunState( run_id="run-1", diff --git a/tests/python/state/test_state_roundtrip.py b/tests/python/state/test_state_roundtrip.py index 781aad674..c7d87d14a 100644 --- a/tests/python/state/test_state_roundtrip.py +++ b/tests/python/state/test_state_roundtrip.py @@ -28,6 +28,10 @@ def test_json_state_roundtrip_preserves_services(self) -> None: listener_pids=[12345, 12346], status="running", started_at=1234.5, + runtime_kind="docker", + container_id="sha256:abc", + container_name="envctl-app-tree-alpha-backend", + container_image="envctl/example-backend:dev", ) }, metadata={"source": "python"}, @@ -40,6 +44,16 @@ def test_json_state_roundtrip_preserves_services(self) -> None: self.assertEqual(loaded.services["Tree Alpha Backend"].actual_port, 8001) self.assertEqual(loaded.services["Tree Alpha Backend"].listener_pids, [12345, 12346]) self.assertEqual(loaded.services["Tree Alpha Backend"].started_at, 1234.5) + self.assertEqual(loaded.services["Tree Alpha Backend"].runtime_kind, "docker") + self.assertEqual(loaded.services["Tree Alpha Backend"].container_id, "sha256:abc") + self.assertEqual( + loaded.services["Tree Alpha Backend"].container_name, + "envctl-app-tree-alpha-backend", + ) + self.assertEqual( + loaded.services["Tree Alpha Backend"].container_image, + "envctl/example-backend:dev", + ) self.assertEqual(loaded.metadata["source"], "python") def test_legacy_shell_state_is_loaded_without_source_execution(self) -> None: From 876dda8701c92dd103502931d02806b845297c87 Mon Sep 17 00:00:00 2001 From: kfiramar <83420275+kfiramar@users.noreply.github.com> Date: Sat, 11 Jul 2026 00:45:28 +0300 Subject: [PATCH 2/6] Prefer dev for pull request bases --- docs/reference/commands.md | 4 +++ .../actions/action_git_state_support.py | 26 +++++++++++++++++++ .../actions/project_action_domain.py | 7 ++++- .../ui/dashboard/pr_selection_support.py | 4 +-- .../actions/test_action_git_state_support.py | 22 ++++++++++++++++ tests/python/actions/test_actions_cli_pr.py | 22 ++++++++-------- tests/python/actions/test_actions_cli_ship.py | 2 +- 7 files changed, 72 insertions(+), 15 deletions(-) diff --git a/docs/reference/commands.md b/docs/reference/commands.md index 1ee514fb2..11d5ffb78 100644 --- a/docs/reference/commands.md +++ b/docs/reference/commands.md @@ -285,6 +285,10 @@ High-value command families: - `plan` - `import` - `test` + +When `envctl pr` or `envctl ship` opens a pull request without an explicit +`ENVCTL_PR_BASE`, it selects the first branch that exists in this order: +`dev`, `main`, then `master`. Both remote branches and local refs are checked. - `logs` - `health` - `errors` diff --git a/python/envctl_engine/actions/action_git_state_support.py b/python/envctl_engine/actions/action_git_state_support.py index d99a73ab5..2e189b67a 100644 --- a/python/envctl_engine/actions/action_git_state_support.py +++ b/python/envctl_engine/actions/action_git_state_support.py @@ -80,6 +80,32 @@ def detect_default_branch(git_root: Path, *, git_output: Callable[[Path, list[st return "main" +def detect_pr_base_branch(git_root: Path, *, git_output: Callable[[Path, list[str]], str]) -> str: + remote_heads = git_output( + git_root, + [ + "ls-remote", + "--heads", + "origin", + "refs/heads/dev", + "refs/heads/main", + "refs/heads/master", + ], + ) + remote_branches = { + line.rsplit("refs/heads/", 1)[1].strip() + for line in remote_heads.splitlines() + if "refs/heads/" in line + } + for candidate in ("dev", "main", "master"): + if candidate in remote_branches: + return candidate + for ref in (f"origin/{candidate}", candidate): + if git_output(git_root, ["rev-parse", "--verify", ref]).strip(): + return candidate + return "master" + + def existing_pr_url( git_root: Path, branch: str, diff --git a/python/envctl_engine/actions/project_action_domain.py b/python/envctl_engine/actions/project_action_domain.py index 316f75a1f..813047190 100644 --- a/python/envctl_engine/actions/project_action_domain.py +++ b/python/envctl_engine/actions/project_action_domain.py @@ -70,6 +70,7 @@ "_unmerged_stage_entries", "_write_pr_body_file", "detect_default_branch", + "detect_pr_base_branch", "existing_pr_url", "probe_dirty_worktree", "resolve_git_root", @@ -296,6 +297,10 @@ def detect_default_branch(git_root: Path) -> str: return git_state_support.detect_default_branch(git_root, git_output=_git_output) +def detect_pr_base_branch(git_root: Path) -> str: + return git_state_support.detect_pr_base_branch(git_root, git_output=_git_output) + + def existing_pr_url(git_root: Path, branch: str) -> str: return git_state_support.existing_pr_url( git_root, @@ -335,7 +340,7 @@ def _resolve_pr_base_branch(context: ActionProjectContext, git_root: Path) -> st explicit = str(context.env.get("ENVCTL_PR_BASE", "")).strip() if explicit: return explicit - return detect_default_branch(git_root) + return detect_pr_base_branch(git_root) _resolve_analyze_mode = review_iteration_support.resolve_analyze_mode diff --git a/python/envctl_engine/ui/dashboard/pr_selection_support.py b/python/envctl_engine/ui/dashboard/pr_selection_support.py index cc6fa4c45..bfbc06fdd 100644 --- a/python/envctl_engine/ui/dashboard/pr_selection_support.py +++ b/python/envctl_engine/ui/dashboard/pr_selection_support.py @@ -4,7 +4,7 @@ import subprocess from typing import Any, Callable, cast -from envctl_engine.actions.project_action_domain import detect_default_branch +from envctl_engine.actions.project_action_domain import detect_pr_base_branch from envctl_engine.runtime.command_router import Route from envctl_engine.state.models import RunState from envctl_engine.ui.dashboard.pr_flow import run_pr_flow @@ -83,7 +83,7 @@ def default_pr_base_branch( owner: Any, runtime: Any, *, - detect_default_branch_fn: Callable[[Path], str] = detect_default_branch, + detect_default_branch_fn: Callable[[Path], str] = detect_pr_base_branch, pr_git_root_fn: Callable[[Any, Any], Path] = pr_git_root, ) -> str: git_root = pr_git_root_fn(owner, runtime) diff --git a/tests/python/actions/test_action_git_state_support.py b/tests/python/actions/test_action_git_state_support.py index 30acf4c4b..be6a7c674 100644 --- a/tests/python/actions/test_action_git_state_support.py +++ b/tests/python/actions/test_action_git_state_support.py @@ -42,6 +42,28 @@ def local_main(_git_root: Path, args: list[str]) -> str: self.assertEqual(git_state.detect_default_branch(Path("/repo"), git_output=local_main), "main") + def test_detect_pr_base_branch_prefers_remote_or_local_dev_then_main_then_master(self) -> None: + def resolve(existing_refs: set[str], remote_heads: set[str] | None = None) -> str: + def fake_git_output(_git_root: Path, args: list[str]) -> str: + if args[:2] == ["ls-remote", "--heads"]: + return "".join( + f"abc123\trefs/heads/{branch}\n" for branch in sorted(remote_heads or set()) + ) + self.assertEqual(args[:2], ["rev-parse", "--verify"]) + return "abc123\n" if args[2] in existing_refs else "" + + return git_state.detect_pr_base_branch(Path("/repo"), git_output=fake_git_output) + + self.assertEqual(resolve({"origin/dev", "origin/main", "origin/master"}), "dev") + self.assertEqual(resolve({"dev", "origin/main"}), "dev") + self.assertEqual(resolve({"origin/main", "origin/master"}), "main") + self.assertEqual(resolve({"main", "origin/master"}), "main") + self.assertEqual(resolve({"origin/master"}), "master") + self.assertEqual(resolve({"master"}), "master") + self.assertEqual(resolve(set()), "master") + self.assertEqual(resolve({"origin/main"}, {"dev", "main"}), "dev") + self.assertEqual(resolve({"dev"}, {"main"}), "dev") + def test_existing_pr_url_uses_gh_pr_list_for_open_branch(self) -> None: calls: list[tuple[list[str], str]] = [] diff --git a/tests/python/actions/test_actions_cli_pr.py b/tests/python/actions/test_actions_cli_pr.py index e92c9dd72..0a85ace1f 100644 --- a/tests/python/actions/test_actions_cli_pr.py +++ b/tests/python/actions/test_actions_cli_pr.py @@ -31,7 +31,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch( "envctl_engine.actions.project_action_domain.existing_pr_url", @@ -93,7 +93,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.probe_dirty_worktree") as dirty_probe, @@ -144,7 +144,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.probe_dirty_worktree") as dirty_probe, @@ -221,7 +221,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -300,7 +300,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -367,7 +367,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -429,7 +429,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 with ( patch.dict(os.environ, {"ENVCTL_PR_BODY": "Typed PR body\n\nWith details."}, clear=False), - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -493,7 +493,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 patch.dict(os.environ, {"ENVCTL_ACTION_INTERACTIVE": "1"}, clear=False), patch("sys.stdin.isatty", return_value=True), patch("builtins.input", side_effect=AssertionError("input() should not be called for pr action")), - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -551,7 +551,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -600,7 +600,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -657,7 +657,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), diff --git a/tests/python/actions/test_actions_cli_ship.py b/tests/python/actions/test_actions_cli_ship.py index 569e6da2a..5fc02c739 100644 --- a/tests/python/actions/test_actions_cli_ship.py +++ b/tests/python/actions/test_actions_cli_ship.py @@ -236,7 +236,7 @@ def fake_run(git_root: Path, args: list[str]): # noqa: ANN001 with ( patch("envctl_engine.actions.project_action_domain.shutil.which", return_value="/usr/bin/git"), - patch("envctl_engine.actions.project_action_domain.detect_default_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain._run_git", side_effect=fake_run), patch("envctl_engine.actions.project_action_domain.run_commit_action", return_value=0) as commit_action, From 9ba77115dbe43380b4fa1e02ba8fdb0f1d2298a0 Mon Sep 17 00:00:00 2001 From: kfiramar <83420275+kfiramar@users.noreply.github.com> Date: Sat, 11 Jul 2026 01:02:25 +0300 Subject: [PATCH 3/6] fix: serialize startup and guard bulk actions --- .../action_command_execution_support.py | 17 ++++++ .../actions/project_action_domain.py | 7 +-- python/envctl_engine/debug/debug_utils.py | 5 -- .../runtime/help_topic_actions.py | 13 +++-- python/envctl_engine/startup/lifecycle.py | 20 +++++++ .../ui/dashboard/pr_selection_support.py | 18 +++++- python/envctl_engine/ui/selection_support.py | 4 +- .../test_action_command_execution_support.py | 54 +++++++++++++++++ tests/python/actions/test_actions_cli_pr.py | 22 +++---- tests/python/actions/test_actions_cli_ship.py | 2 +- .../actions/test_actions_migrate_parity.py | 3 +- .../debug/test_debug_utils_file_lock.py | 14 +++++ .../startup/test_startup_lifecycle_lock.py | 58 +++++++++++++++++++ tests/python/ui/test_selection_support.py | 8 +++ 14 files changed, 212 insertions(+), 33 deletions(-) create mode 100644 tests/python/debug/test_debug_utils_file_lock.py create mode 100644 tests/python/startup/test_startup_lifecycle_lock.py diff --git a/python/envctl_engine/actions/action_command_execution_support.py b/python/envctl_engine/actions/action_command_execution_support.py index f18734d8f..51e394167 100644 --- a/python/envctl_engine/actions/action_command_execution_support.py +++ b/python/envctl_engine/actions/action_command_execution_support.py @@ -18,6 +18,8 @@ "migrate": "run_migrate_action", } +_BULK_MUTATING_COMMANDS = frozenset({"commit", "migrate", "pr", "ship"}) + class ActionSpinner(Protocol): def start(self) -> None: ... @@ -59,6 +61,12 @@ def execute(self) -> int: self._emit_finish(1, error=error) return 1 + bulk_confirmation_error = self._bulk_confirmation_error(targets) + if bulk_confirmation_error is not None: + print(bulk_confirmation_error) + self._emit_finish(1, error=bulk_confirmation_error) + return 1 + handler = self._handler() if handler is None: return self.runtime.unsupported_command(self.route.command) @@ -70,6 +78,15 @@ def execute(self) -> int: self._emit_finish(code) return code + def _bulk_confirmation_error(self, targets: list[object]) -> str | None: + if self.route.command not in _BULK_MUTATING_COMMANDS: + return None + if not bool(self.route.flags.get("all")) or len(targets) <= 1: + return None + if bool(self.route.flags.get("yes")) or bool(self.route.flags.get("force")): + return None + return f"{self.route.command} --all targets {len(targets)} projects and requires --yes or --force" + def _execute_special_worktree_command(self) -> int | None: if self.route.command == "self-destruct-worktree": code = self.orchestrator.run_self_destruct_worktree_action(self.route) diff --git a/python/envctl_engine/actions/project_action_domain.py b/python/envctl_engine/actions/project_action_domain.py index 813047190..aea45f876 100644 --- a/python/envctl_engine/actions/project_action_domain.py +++ b/python/envctl_engine/actions/project_action_domain.py @@ -70,7 +70,6 @@ "_unmerged_stage_entries", "_write_pr_body_file", "detect_default_branch", - "detect_pr_base_branch", "existing_pr_url", "probe_dirty_worktree", "resolve_git_root", @@ -297,10 +296,6 @@ def detect_default_branch(git_root: Path) -> str: return git_state_support.detect_default_branch(git_root, git_output=_git_output) -def detect_pr_base_branch(git_root: Path) -> str: - return git_state_support.detect_pr_base_branch(git_root, git_output=_git_output) - - def existing_pr_url(git_root: Path, branch: str) -> str: return git_state_support.existing_pr_url( git_root, @@ -340,7 +335,7 @@ def _resolve_pr_base_branch(context: ActionProjectContext, git_root: Path) -> st explicit = str(context.env.get("ENVCTL_PR_BASE", "")).strip() if explicit: return explicit - return detect_pr_base_branch(git_root) + return git_state_support.detect_pr_base_branch(git_root, git_output=_git_output) _resolve_analyze_mode = review_iteration_support.resolve_analyze_mode diff --git a/python/envctl_engine/debug/debug_utils.py b/python/envctl_engine/debug/debug_utils.py index 83ddcbbd2..7511cee73 100644 --- a/python/envctl_engine/debug/debug_utils.py +++ b/python/envctl_engine/debug/debug_utils.py @@ -84,11 +84,6 @@ def file_lock(path: Path, *, timeout: float) -> Iterator[None]: except OSError: pass handle.close() - try: - if path.exists() and path.stat().st_size == 0: - path.unlink() - except OSError: - pass def debug_env_value(env: dict[str, str], key: str) -> str | None: diff --git a/python/envctl_engine/runtime/help_topic_actions.py b/python/envctl_engine/runtime/help_topic_actions.py index 84b358a6c..2d4182ee1 100644 --- a/python/envctl_engine/runtime/help_topic_actions.py +++ b/python/envctl_engine/runtime/help_topic_actions.py @@ -65,7 +65,7 @@ "pr": CommandHelpTopic( command="pr", summary="create a pull request for the selected branch/worktree", - usage=("envctl pr [--project ] [--pr-base ]",), + usage=("envctl pr [--project |--all --yes] [--pr-base ]",), what_it_does=( "resolves the current branch, checks for an existing PR, and creates one with gh or repo helper scripts", "commits and pushes dirty worktree changes first using envctl commit behavior when needed", @@ -73,6 +73,7 @@ ), flags=( "--project create PR for selected worktree/project", + "--all --yes explicitly approve creating/updating PRs for every discovered target", "--pr-base set the target base branch", "--interactive opt into prompts/selection instead of default headless action mode", ), @@ -83,7 +84,7 @@ "commit": CommandHelpTopic( command="commit", summary="commit-only fallback that preserves envctl-local control artifacts", - usage=("envctl commit [--project |--main] [-m |--commit-message-file ]",), + usage=("envctl commit [--project |--main|--all --yes] [-m |--commit-message-file ]",), what_it_does=( "use envctl ship for normal AI handoff; reserve commit for commit-only maintenance or fallback flows", "stages normal changed paths and intentionally skips protected envctl-local artifacts", @@ -96,6 +97,7 @@ "--commit-message-file read commit message from a file", "--project commit inside selected worktree/project", "--main target the main checkout", + "--all --yes explicitly approve committing every discovered target", ), examples=( "envctl commit --main -m 'Ship focused fix'", @@ -107,7 +109,7 @@ "ship": CommandHelpTopic( command="ship", summary="commit, push, create/update PR, and report GitHub checks for the current or selected target", - usage=("envctl ship [--project ] [-m ] [--human]",), + usage=("envctl ship [--project |--all --yes] [-m ] [--human]",), what_it_does=( "when run inside a generated worktree, infers that worktree without requiring --project", "owns the normal AI handoff flow instead of requiring separate commit, push, or PR commands", @@ -123,6 +125,7 @@ ), flags=( "--project ship one worktree/project", + "--all --yes explicitly approve shipping every discovered target", "-m use explicit commit message text for the commit phase", "--json compatibility no-op; JSON is the default", "--human print compact terminal output instead of JSON", @@ -155,7 +158,7 @@ "migrate": CommandHelpTopic( command="migrate", summary="run backend database migrations for selected targets", - usage=("envctl migrate [--project |--main|--all]",), + usage=("envctl migrate [--project |--main|--all --yes]",), what_it_does=( "runs the configured/default backend migration command, usually Alembic upgrade head", "loads backend env files and reuses saved dependency URLs when a current run exists", @@ -164,7 +167,7 @@ flags=( "--project migrate one worktree/project", "--main migrate the main checkout", - "--all migrate all selected/discovered targets where supported", + "--all --yes explicitly approve migrating all discovered targets", ), examples=("envctl migrate --project feature-a-1", "envctl migrate --main"), aliases=("migration", "migrations", "m", "--migrate"), diff --git a/python/envctl_engine/startup/lifecycle.py b/python/envctl_engine/startup/lifecycle.py index a82529b67..2eba8eaf0 100644 --- a/python/envctl_engine/startup/lifecycle.py +++ b/python/envctl_engine/startup/lifecycle.py @@ -1,8 +1,10 @@ from __future__ import annotations from functools import partial +from pathlib import Path from typing import Any +from envctl_engine.debug.debug_utils import file_lock from envctl_engine.planning.plan_agent.launch import launch_plan_agent_terminals from envctl_engine.planning.plan_agent.omx_transport import validate_plan_agent_attach_target from envctl_engine.planning.plan_agent.tmux_transport import attach_plan_agent_terminal @@ -50,12 +52,30 @@ select_start_tree_projects, trees_start_selection_required, ) +from envctl_engine.shared.parsing import parse_float from envctl_engine.ui.debug_snapshot import emit_startup_plan_handoff_snapshot from envctl_engine.ui.spinner import spinner, use_spinner_policy from envctl_engine.ui.spinner_service import emit_spinner_policy, resolve_spinner_policy def execute_startup_lifecycle(orchestrator: Any, route: Any) -> int: + runtime = orchestrator.runtime + timeout_raw = runtime.env.get("ENVCTL_STARTUP_LOCK_TIMEOUT_SECONDS") or runtime.config.raw.get( + "ENVCTL_STARTUP_LOCK_TIMEOUT_SECONDS" + ) + timeout = max(parse_float(timeout_raw, 3600.0), 1.0) + lock_path = Path(runtime.runtime_root) / "locks" / "startup.lock" + try: + with file_lock(lock_path, timeout=timeout): + return _execute_startup_lifecycle_locked(orchestrator, route) + except TimeoutError as exc: + message = f"Startup could not acquire the repository lifecycle lock: {exc}" + runtime._emit("startup.lock.timeout", lock_path=str(lock_path), timeout_seconds=timeout) + print(message) + return 1 + + +def _execute_startup_lifecycle_locked(orchestrator: Any, route: Any) -> int: runtime = orchestrator.runtime session = create_startup_session(runtime, route) finalize_failure = partial( diff --git a/python/envctl_engine/ui/dashboard/pr_selection_support.py b/python/envctl_engine/ui/dashboard/pr_selection_support.py index bfbc06fdd..85cfad303 100644 --- a/python/envctl_engine/ui/dashboard/pr_selection_support.py +++ b/python/envctl_engine/ui/dashboard/pr_selection_support.py @@ -4,7 +4,7 @@ import subprocess from typing import Any, Callable, cast -from envctl_engine.actions.project_action_domain import detect_pr_base_branch +from envctl_engine.actions.action_git_state_support import detect_pr_base_branch from envctl_engine.runtime.command_router import Route from envctl_engine.state.models import RunState from envctl_engine.ui.dashboard.pr_flow import run_pr_flow @@ -12,6 +12,20 @@ from envctl_engine.ui.selector_model import SelectorItem +def _dashboard_git_output(git_root: Path, args: list[str]) -> str: + result = subprocess.run( + ["git", "-C", str(git_root), *args], + text=True, + capture_output=True, + check=False, + ) + return result.stdout if result.returncode == 0 else "" + + +def _detect_pr_base_branch(git_root: Path) -> str: + return detect_pr_base_branch(git_root, git_output=_dashboard_git_output) + + def apply_pr_selection(owner: Any, route: Route, state: RunState, rt: object) -> Route | None: runtime_any = cast(Any, rt) default_branch = owner._default_pr_base_branch(runtime_any) @@ -83,7 +97,7 @@ def default_pr_base_branch( owner: Any, runtime: Any, *, - detect_default_branch_fn: Callable[[Path], str] = detect_pr_base_branch, + detect_default_branch_fn: Callable[[Path], str] = _detect_pr_base_branch, pr_git_root_fn: Callable[[Any, Any], Path] = pr_git_root, ) -> str: git_root = pr_git_root_fn(owner, runtime) diff --git a/python/envctl_engine/ui/selection_support.py b/python/envctl_engine/ui/selection_support.py index 99ab5f157..b46efe457 100644 --- a/python/envctl_engine/ui/selection_support.py +++ b/python/envctl_engine/ui/selection_support.py @@ -110,9 +110,11 @@ def no_target_selected_message( return f"No {label} target selected." mode = str(route.mode).strip().lower() discovery = "envctl --list-trees --json" if mode == "trees" else "envctl --list-targets --main --json" + all_selector = "--all --yes" if command in {"commit", "migrate", "pr", "ship"} else "--all" return ( f"No {label} target selected. " - f"In headless mode, run '{discovery}' and retry with '--project ', '--service ', or '--all'." + f"In headless mode, run '{discovery}' and retry with '--project ', '--service ', " + f"or '{all_selector}'." ) diff --git a/tests/python/actions/test_action_command_execution_support.py b/tests/python/actions/test_action_command_execution_support.py index b982bfa98..bb8213ad8 100644 --- a/tests/python/actions/test_action_command_execution_support.py +++ b/tests/python/actions/test_action_command_execution_support.py @@ -44,6 +44,7 @@ def __init__(self) -> None: self.statuses: list[str] = [] self.restored = False self.spinner = _Spinner() + self.ship_calls = 0 def resolve_targets(self, _route, *, trees_only: bool) -> tuple[list[object], str | None]: return [SimpleNamespace(name="Main")], None @@ -66,6 +67,10 @@ def restore() -> None: def run_test_action(self, _route, _targets: list[object]) -> int: return 0 + def run_ship_action(self, _route, _targets: list[object]) -> int: + self.ship_calls += 1 + return 0 + class _DeferredOutputOrchestrator(_Orchestrator): def __init__(self) -> None: @@ -136,6 +141,55 @@ def test_execute_action_command_returns_target_resolution_error(self) -> None: orchestrator.runtime.events, ) + def test_execute_action_command_requires_confirmation_for_bulk_mutation(self) -> None: + orchestrator = _Orchestrator() + orchestrator.resolve_targets = lambda _route, trees_only: ( # type: ignore[assignment] + [SimpleNamespace(name="one"), SimpleNamespace(name="two")], + None, + ) + route = SimpleNamespace(command="ship", mode="trees", flags={"all": True}) + + code = execute_action_command( + orchestrator, + route, + spinner_factory=lambda *_args, **_kwargs: _policy_context(orchestrator.spinner), + resolve_spinner_policy_fn=lambda _env: SimpleNamespace(enabled=False), + emit_spinner_policy_fn=lambda *_args, **_kwargs: None, + use_spinner_policy_fn=_policy_context, + ) + + self.assertEqual(code, 1) + self.assertEqual(orchestrator.ship_calls, 0) + self.assertIn( + { + "event": "action.command.finish", + "command": "ship", + "code": 1, + "error": "ship --all targets 2 projects and requires --yes or --force", + }, + orchestrator.runtime.events, + ) + + def test_execute_action_command_allows_confirmed_bulk_mutation(self) -> None: + orchestrator = _Orchestrator() + orchestrator.resolve_targets = lambda _route, trees_only: ( # type: ignore[assignment] + [SimpleNamespace(name="one"), SimpleNamespace(name="two")], + None, + ) + route = SimpleNamespace(command="ship", mode="trees", flags={"all": True, "yes": True}) + + code = execute_action_command( + orchestrator, + route, + spinner_factory=lambda *_args, **_kwargs: _policy_context(orchestrator.spinner), + resolve_spinner_policy_fn=lambda _env: SimpleNamespace(enabled=False), + emit_spinner_policy_fn=lambda *_args, **_kwargs: None, + use_spinner_policy_fn=_policy_context, + ) + + self.assertEqual(code, 0) + self.assertEqual(orchestrator.ship_calls, 1) + def test_execute_action_command_flushes_deferred_output_before_finish(self) -> None: orchestrator = _DeferredOutputOrchestrator() route = SimpleNamespace(command="test", mode="main", flags={}) diff --git a/tests/python/actions/test_actions_cli_pr.py b/tests/python/actions/test_actions_cli_pr.py index 0a85ace1f..745843f6f 100644 --- a/tests/python/actions/test_actions_cli_pr.py +++ b/tests/python/actions/test_actions_cli_pr.py @@ -31,7 +31,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch( "envctl_engine.actions.project_action_domain.existing_pr_url", @@ -93,7 +93,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.probe_dirty_worktree") as dirty_probe, @@ -144,7 +144,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.probe_dirty_worktree") as dirty_probe, @@ -221,7 +221,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -300,7 +300,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -367,7 +367,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -429,7 +429,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 with ( patch.dict(os.environ, {"ENVCTL_PR_BODY": "Typed PR body\n\nWith details."}, clear=False), - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -493,7 +493,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 patch.dict(os.environ, {"ENVCTL_ACTION_INTERACTIVE": "1"}, clear=False), patch("sys.stdin.isatty", return_value=True), patch("builtins.input", side_effect=AssertionError("input() should not be called for pr action")), - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -551,7 +551,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -600,7 +600,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), @@ -657,7 +657,7 @@ def fake_run(args: list[str], **_kwargs): # noqa: ANN001 return subprocess.CompletedProcess(args=command, returncode=1, stdout="", stderr="unexpected command") with ( - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain.existing_pr_url", return_value=""), patch("envctl_engine.actions.project_action_domain.shutil.which", side_effect=fake_which), diff --git a/tests/python/actions/test_actions_cli_ship.py b/tests/python/actions/test_actions_cli_ship.py index 5fc02c739..0a8640510 100644 --- a/tests/python/actions/test_actions_cli_ship.py +++ b/tests/python/actions/test_actions_cli_ship.py @@ -236,7 +236,7 @@ def fake_run(git_root: Path, args: list[str]): # noqa: ANN001 with ( patch("envctl_engine.actions.project_action_domain.shutil.which", return_value="/usr/bin/git"), - patch("envctl_engine.actions.project_action_domain.detect_pr_base_branch", return_value="main"), + patch("envctl_engine.actions.project_action_domain.git_state_support.detect_pr_base_branch", return_value="main"), patch("envctl_engine.actions.project_action_domain._git_output", side_effect=fake_git_output), patch("envctl_engine.actions.project_action_domain._run_git", side_effect=fake_run), patch("envctl_engine.actions.project_action_domain.run_commit_action", return_value=0) as commit_action, diff --git a/tests/python/actions/test_actions_migrate_parity.py b/tests/python/actions/test_actions_migrate_parity.py index 84bc451e6..b2217f6b3 100644 --- a/tests/python/actions/test_actions_migrate_parity.py +++ b/tests/python/actions/test_actions_migrate_parity.py @@ -546,7 +546,7 @@ def test_migrate_action_uses_distinct_env_contracts_for_each_target_in_multi_tar fake_runner = _FakeRunner(returncode=0) engine.process_runner = fake_runner # type: ignore[assignment] - route = parse_route(["migrate", "--all"], env={"ENVCTL_DEFAULT_MODE": "trees"}) + route = parse_route(["migrate", "--all", "--yes"], env={"ENVCTL_DEFAULT_MODE": "trees"}) code = engine.dispatch(route) self.assertEqual(code, 0) @@ -567,4 +567,3 @@ def test_migrate_action_uses_distinct_env_contracts_for_each_target_in_multi_tar ) self.assertEqual(first_env.get("SQLALCHEMY_DATABASE_URL"), first_env.get("DATABASE_URL")) self.assertEqual(second_env.get("SQLALCHEMY_DATABASE_URL"), second_env.get("DATABASE_URL")) - diff --git a/tests/python/debug/test_debug_utils_file_lock.py b/tests/python/debug/test_debug_utils_file_lock.py new file mode 100644 index 000000000..46cb5a970 --- /dev/null +++ b/tests/python/debug/test_debug_utils_file_lock.py @@ -0,0 +1,14 @@ +from __future__ import annotations + +from pathlib import Path + +from envctl_engine.debug.debug_utils import file_lock + + +def test_file_lock_keeps_stable_inode_after_release(tmp_path: Path) -> None: + lock_path = tmp_path / "stable.lock" + + with file_lock(lock_path, timeout=1.0): + inode = lock_path.stat().st_ino + + assert lock_path.stat().st_ino == inode diff --git a/tests/python/startup/test_startup_lifecycle_lock.py b/tests/python/startup/test_startup_lifecycle_lock.py new file mode 100644 index 000000000..a7c39f0f5 --- /dev/null +++ b/tests/python/startup/test_startup_lifecycle_lock.py @@ -0,0 +1,58 @@ +from __future__ import annotations + +from contextlib import contextmanager +from pathlib import Path +from types import SimpleNamespace + +from envctl_engine.startup import lifecycle + + +def test_execute_startup_lifecycle_holds_repo_scope_lock(monkeypatch, tmp_path: Path) -> None: + seen: list[object] = [] + + @contextmanager + def recording_lock(path: Path, *, timeout: float): + seen.extend((path, timeout, "entered")) + yield + seen.append("exited") + + runtime = SimpleNamespace( + runtime_root=tmp_path, + env={}, + config=SimpleNamespace(raw={}), + ) + orchestrator = SimpleNamespace(runtime=runtime) + monkeypatch.setattr(lifecycle, "file_lock", recording_lock) + monkeypatch.setattr( + lifecycle, + "_execute_startup_lifecycle_locked", + lambda received_orchestrator, route: seen.append((received_orchestrator, route)) or 7, + ) + + assert lifecycle.execute_startup_lifecycle(orchestrator, "route") == 7 + assert seen == [tmp_path / "locks" / "startup.lock", 3600.0, "entered", (orchestrator, "route"), "exited"] + + +def test_execute_startup_lifecycle_reports_lock_timeout(monkeypatch, tmp_path: Path, capsys) -> None: + @contextmanager + def timing_out_lock(path: Path, *, timeout: float): + raise TimeoutError(f"Timed out acquiring lock: {path}") + yield + + events: list[tuple[str, dict[str, object]]] = [] + runtime = SimpleNamespace( + runtime_root=tmp_path, + env={"ENVCTL_STARTUP_LOCK_TIMEOUT_SECONDS": "2.5"}, + config=SimpleNamespace(raw={}), + _emit=lambda event, **payload: events.append((event, payload)), + ) + monkeypatch.setattr(lifecycle, "file_lock", timing_out_lock) + + assert lifecycle.execute_startup_lifecycle(SimpleNamespace(runtime=runtime), "route") == 1 + assert "repository lifecycle lock" in capsys.readouterr().out + assert events == [ + ( + "startup.lock.timeout", + {"lock_path": str(tmp_path / "locks" / "startup.lock"), "timeout_seconds": 2.5}, + ) + ] diff --git a/tests/python/ui/test_selection_support.py b/tests/python/ui/test_selection_support.py index bca70906e..a988856b0 100644 --- a/tests/python/ui/test_selection_support.py +++ b/tests/python/ui/test_selection_support.py @@ -108,6 +108,14 @@ def test_no_target_selected_message_and_explicit_target_detection_match_route_co "--project ", no_target_selected_message("restart", route=_route(mode="trees"), interactive_allowed=False), ) + self.assertIn( + "--all --yes", + no_target_selected_message("ship", route=_route(mode="trees"), interactive_allowed=False), + ) + self.assertNotIn( + "--all --yes", + no_target_selected_message("test", route=_route(mode="trees"), interactive_allowed=False), + ) self.assertTrue(route_has_explicit_target(_route(flags={"all": True}), runtime)) self.assertTrue(route_has_explicit_target(_route(projects=["Main"]), runtime)) From 7d807874c59a0e2c67bfea21b4527d1cea6e5fe6 Mon Sep 17 00:00:00 2001 From: kfiramar <83420275+kfiramar@users.noreply.github.com> Date: Sat, 11 Jul 2026 22:32:11 +0300 Subject: [PATCH 4/6] fix Docker runtime review findings --- .../actions/action_git_state_support.py | 20 ++++-- .../runtime/docker_service_runtime.py | 39 ++++++++++- .../runtime/engine_runtime_commands.py | 6 +- .../runtime/lifecycle_dependency_stop.py | 5 +- .../runtime/lifecycle_service_termination.py | 5 +- .../startup/resume_restore_execution.py | 9 +++ .../actions/test_action_git_state_support.py | 14 +++- .../runtime/test_docker_service_runtime.py | 70 ++++++++++++++++++- .../runtime/test_engine_runtime_commands.py | 11 ++- .../runtime/test_lifecycle_dependency_stop.py | 34 +++++++++ .../startup/test_resume_restore_policy.py | 35 +++++++++- 11 files changed, 228 insertions(+), 20 deletions(-) diff --git a/python/envctl_engine/actions/action_git_state_support.py b/python/envctl_engine/actions/action_git_state_support.py index 2e189b67a..83fbe4738 100644 --- a/python/envctl_engine/actions/action_git_state_support.py +++ b/python/envctl_engine/actions/action_git_state_support.py @@ -71,15 +71,27 @@ def classify_dirty_porcelain(status_output: str) -> tuple[bool, bool, bool]: def detect_default_branch(git_root: Path, *, git_output: Callable[[Path, list[str]], str]) -> str: - ref = git_output(git_root, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]).strip() - if ref.startswith("origin/"): - return ref.split("origin/", 1)[1] + origin_default = _origin_default_branch(git_root, git_output=git_output) + if origin_default: + return origin_default for candidate in ("main", "master"): if git_output(git_root, ["rev-parse", "--verify", candidate]).strip(): return candidate return "main" +def _origin_default_branch(git_root: Path, *, git_output: Callable[[Path, list[str]], str]) -> str: + ref = git_output(git_root, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]).strip() + if ref.startswith("origin/"): + return ref.split("origin/", 1)[1] + remote_head = git_output(git_root, ["ls-remote", "--symref", "origin", "HEAD"]) + for line in remote_head.splitlines(): + prefix = "ref: refs/heads/" + if line.startswith(prefix) and line.rstrip().endswith("\tHEAD"): + return line[len(prefix) :].split("\t", 1)[0].strip() + return "" + + def detect_pr_base_branch(git_root: Path, *, git_output: Callable[[Path, list[str]], str]) -> str: remote_heads = git_output( git_root, @@ -103,7 +115,7 @@ def detect_pr_base_branch(git_root: Path, *, git_output: Callable[[Path, list[st for ref in (f"origin/{candidate}", candidate): if git_output(git_root, ["rev-parse", "--verify", ref]).strip(): return candidate - return "master" + return _origin_default_branch(git_root, git_output=git_output) or "master" def existing_pr_url( diff --git a/python/envctl_engine/runtime/docker_service_runtime.py b/python/envctl_engine/runtime/docker_service_runtime.py index bc14c3542..781182b66 100644 --- a/python/envctl_engine/runtime/docker_service_runtime.py +++ b/python/envctl_engine/runtime/docker_service_runtime.py @@ -256,13 +256,37 @@ def follow_logs(self, container: str, log_path: str) -> int | None: pid = getattr(process, "pid", None) return pid if isinstance(pid, int) and pid > 0 else None - def stop(self, container: str, *, remove: bool = True) -> bool: + def stop( + self, + container: str, + *, + remove: bool = True, + verify_ownership: bool = False, + ) -> bool: if not container: return True + if verify_ownership and not self._container_owned_by_runtime_scope(container): + return False command = ["docker", "rm", "--force", container] if remove else ["docker", "stop", container] result = self.process_runtime.run(command, timeout=30.0) return result.returncode == 0 or "No such container" in str(result.stderr or "") + def _container_owned_by_runtime_scope(self, container: str) -> bool: + inspected = self.process_runtime.run( + [ + "docker", + "container", + "inspect", + "--format", + f"{{{{index .Config.Labels {json.dumps(_RUNTIME_SCOPE_LABEL)}}}}}", + container, + ], + timeout=10.0, + ) + if inspected.returncode != 0: + return "No such" in str(inspected.stderr or "") + return str(inspected.stdout or "").strip() == self._runtime_scope_identity() + def container_running(self, container: str) -> bool: if not container: return False @@ -548,6 +572,7 @@ def _setting(self, service_name: str, suffix: str) -> str: def _container_port(self, service_name: str, host_port: int, *, image: str) -> int: value = self._setting(service_name, "DOCKER_PORT") if not value: + self._ensure_image_available(image) exposed = self._single_exposed_port(image) return exposed if exposed is not None else host_port try: @@ -558,6 +583,18 @@ def _container_port(self, service_name: str, host_port: int, *, image: str) -> i raise RuntimeError(f"Invalid Docker container port for {service_name}: {value!r}") return port + def _ensure_image_available(self, image: str) -> None: + if self._image_exists(image): + return + self.runtime._emit("service.container.pull", image=image) + pulled = self.process_runtime.run( + ["docker", "pull", image], + timeout=self._timeout("ENVCTL_DOCKER_PULL_TIMEOUT_SECONDS", 300.0), + ) + if pulled.returncode != 0: + detail = str(pulled.stderr or pulled.stdout or f"failed to pull Docker image {image}").strip() + raise RuntimeError(detail) + def _single_exposed_port(self, image: str) -> int | None: result = self.process_runtime.run( ["docker", "image", "inspect", "--format", "{{json .Config.ExposedPorts}}", image], diff --git a/python/envctl_engine/runtime/engine_runtime_commands.py b/python/envctl_engine/runtime/engine_runtime_commands.py index 39c5a5c95..22843e289 100644 --- a/python/envctl_engine/runtime/engine_runtime_commands.py +++ b/python/envctl_engine/runtime/engine_runtime_commands.py @@ -106,17 +106,15 @@ def service_start_command_resolved( project_root: Path | None = None, port: int = 0, ) -> tuple[list[str], str]: - command_exists = runtime._command_exists if docker_service_uses_image_command(runtime, service_name): - def command_exists(_executable: str) -> bool: - return True + return [], "docker_image" result = resolve_service_start_command( service_name=service_name, project_root=(project_root or runtime.config.base_dir), port=port, env=runtime.env, config_raw=runtime.config.raw, - command_exists=command_exists, + command_exists=runtime._command_exists, ) return result.command, result.source diff --git a/python/envctl_engine/runtime/lifecycle_dependency_stop.py b/python/envctl_engine/runtime/lifecycle_dependency_stop.py index c18c7ba02..4f7f5ac06 100644 --- a/python/envctl_engine/runtime/lifecycle_dependency_stop.py +++ b/python/envctl_engine/runtime/lifecycle_dependency_stop.py @@ -118,7 +118,10 @@ def stop_requirement_component_containers(runtime: Any, component: Mapping[str, if removed.returncode != 0: detail = removed.stderr or removed.stdout or "failed stopping Supabase containers" raise RuntimeError(str(detail).strip()) - return + return + # Native Supabase DB startup records the concrete container name and + # does not attach a Compose project label. Fall through to direct + # removal when the saved component is not a Compose stack. removed = process_runner.run(["docker", "rm", "--force", container_name], timeout=30.0) if removed.returncode != 0 and "No such container" not in str(removed.stderr or ""): raise RuntimeError(str(removed.stderr or removed.stdout or f"failed stopping {container_name}").strip()) diff --git a/python/envctl_engine/runtime/lifecycle_service_termination.py b/python/envctl_engine/runtime/lifecycle_service_termination.py index 88ca18af7..e468e6cbc 100644 --- a/python/envctl_engine/runtime/lifecycle_service_termination.py +++ b/python/envctl_engine/runtime/lifecycle_service_termination.py @@ -51,7 +51,10 @@ def terminate_service_record(runtime: Any, service: object, *, aggressive: bool, container = str( getattr(service, "container_id", "") or getattr(service, "container_name", "") or "" ).strip() - stopped = DockerServiceRuntime(runtime, runtime.process_runner).stop(container) + stopped = DockerServiceRuntime(runtime, runtime.process_runner).stop( + container, + verify_ownership=verify_ownership, + ) pid = getattr(service, "pid", None) if isinstance(pid, int) and pid > 0: try: diff --git a/python/envctl_engine/startup/resume_restore_execution.py b/python/envctl_engine/startup/resume_restore_execution.py index 6367c0661..d5be89152 100644 --- a/python/envctl_engine/startup/resume_restore_execution.py +++ b/python/envctl_engine/startup/resume_restore_execution.py @@ -7,6 +7,7 @@ from typing import Callable, Mapping from envctl_engine.runtime.command_router import Route +from envctl_engine.runtime.docker_service_runtime import state_uses_docker_services from envctl_engine.state.models import RunState from envctl_engine.startup.resume_restore_project import ResumeProjectRestoreRunner, _round_ms from envctl_engine.startup.resume_restore_results import apply_restore_result @@ -113,6 +114,7 @@ def _execute_restore_missing( use_single_spinner = use_spinner and not use_project_spinner_group suppress_timing_output = use_project_spinner_group or use_single_spinner route_for_startup = _route_for_resume_restore(route, state=state, suppress_timing_output=suppress_timing_output) + _apply_restore_runtime_mode(rt, route_for_startup) if use_single_spinner: rt._emit( # type: ignore[attr-defined] "ui.spinner.lifecycle", @@ -240,6 +242,8 @@ def _route_for_resume_restore( **route_for_startup.flags, "_resume_restore": True, } + if state_uses_docker_services(state): + flags["docker"] = True if suppress_timing_output: flags["_suppress_timing_print"] = True return Route( @@ -252,6 +256,11 @@ def _route_for_resume_restore( ) +def _apply_restore_runtime_mode(runtime: object, route: Route) -> None: + if bool(route.flags.get("docker")): + getattr(runtime, "env")["DOCKER_MODE"] = "true" + + def _run_restore_jobs( *, sorted_projects: list[str], diff --git a/tests/python/actions/test_action_git_state_support.py b/tests/python/actions/test_action_git_state_support.py index be6a7c674..3afca2459 100644 --- a/tests/python/actions/test_action_git_state_support.py +++ b/tests/python/actions/test_action_git_state_support.py @@ -43,12 +43,22 @@ def local_main(_git_root: Path, args: list[str]) -> str: self.assertEqual(git_state.detect_default_branch(Path("/repo"), git_output=local_main), "main") def test_detect_pr_base_branch_prefers_remote_or_local_dev_then_main_then_master(self) -> None: - def resolve(existing_refs: set[str], remote_heads: set[str] | None = None) -> str: + def resolve( + existing_refs: set[str], + remote_heads: set[str] | None = None, + *, + origin_default: str = "", + symbolic_default: bool = True, + ) -> str: def fake_git_output(_git_root: Path, args: list[str]) -> str: if args[:2] == ["ls-remote", "--heads"]: return "".join( f"abc123\trefs/heads/{branch}\n" for branch in sorted(remote_heads or set()) ) + if args == ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]: + return f"origin/{origin_default}\n" if origin_default and symbolic_default else "" + if args == ["ls-remote", "--symref", "origin", "HEAD"]: + return f"ref: refs/heads/{origin_default}\tHEAD\nabc123\tHEAD\n" if origin_default else "" self.assertEqual(args[:2], ["rev-parse", "--verify"]) return "abc123\n" if args[2] in existing_refs else "" @@ -63,6 +73,8 @@ def fake_git_output(_git_root: Path, args: list[str]) -> str: self.assertEqual(resolve(set()), "master") self.assertEqual(resolve({"origin/main"}, {"dev", "main"}), "dev") self.assertEqual(resolve({"dev"}, {"main"}), "dev") + self.assertEqual(resolve(set(), origin_default="develop"), "develop") + self.assertEqual(resolve(set(), origin_default="trunk", symbolic_default=False), "trunk") def test_existing_pr_url_uses_gh_pr_list_for_open_branch(self) -> None: calls: list[tuple[list[str], str]] = [] diff --git a/tests/python/runtime/test_docker_service_runtime.py b/tests/python/runtime/test_docker_service_runtime.py index 0e01dea3d..0328c8fdd 100644 --- a/tests/python/runtime/test_docker_service_runtime.py +++ b/tests/python/runtime/test_docker_service_runtime.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from pathlib import Path from types import SimpleNamespace @@ -23,12 +24,15 @@ def __init__(self) -> None: self.terminated: list[int] = [] self.build_env: dict[str, str] = {} self.existing_container: tuple[bool, str] | None = None + self.local_images: set[str] = set() + self.remote_exposed_ports: dict[str, dict[str, object]] = {} def run(self, command, **_kwargs): argv = [str(part) for part in command] self.calls.append(argv) if argv[:2] == ["docker", "build"]: self.build_env = dict(_kwargs.get("env", {})) + self.local_images.add(argv[argv.index("--tag") + 1]) if argv[:2] == ["docker", "run"]: env_file = Path(argv[argv.index("--env-file") + 1]) self.env_file_payload = env_file.read_text(encoding="utf-8") @@ -37,11 +41,23 @@ def run(self, command, **_kwargs): if self.existing_container is None: return SimpleNamespace(returncode=1, stdout="", stderr="No such container") running, owner = self.existing_container + format_value = argv[argv.index("--format") + 1] + if ".State.Running" not in format_value: + return SimpleNamespace(returncode=0, stdout=f"{owner}\n", stderr="") return SimpleNamespace(returncode=0, stdout=f"{str(running).lower()}|{owner}\n", stderr="") if argv[:3] == ["docker", "inspect", "--format"]: return SimpleNamespace(returncode=0 if self.running else 1, stdout="true\n" if self.running else "", stderr="") if argv[:3] == ["docker", "image", "inspect"]: - return SimpleNamespace(returncode=1, stdout="", stderr="missing") + image = argv[-1] + if image not in self.local_images: + return SimpleNamespace(returncode=1, stdout="", stderr="missing") + if "--format" in argv: + payload = self.remote_exposed_ports.get(image, {}) + return SimpleNamespace(returncode=0, stdout=json.dumps(payload) + "\n", stderr="") + return SimpleNamespace(returncode=0, stdout="[]\n", stderr="") + if argv[:2] == ["docker", "pull"]: + self.local_images.add(argv[-1]) + return SimpleNamespace(returncode=0, stdout="pulled\n", stderr="") if argv[:3] == ["docker", "rm", "--force"]: return SimpleNamespace(returncode=0, stdout="", stderr="") if argv[:2] == ["docker", "logs"]: @@ -256,6 +272,58 @@ def test_configured_image_runs_without_build_and_preserves_browser_public_url(tm assert launch.image == "example/frontend:dev" +def test_remote_image_is_pulled_before_exposed_port_detection(tmp_path: Path) -> None: + runner = _Runner() + image = "example/frontend:remote" + runner.remote_exposed_ports[image] = {"80/tcp": {}} + runtime, _events = _runtime(tmp_path, runner, env={"ENVCTL_FRONTEND_DOCKER_IMAGE": image}) + + DockerServiceRuntime(runtime, runner).launch( + project_name="Main", + project_root=tmp_path, + service_name="frontend", + cwd=tmp_path, + command=[], + env={}, + host_port=9100, + container_port=9100, + listener_expected=True, + log_path=str(tmp_path / "frontend.log"), + ) + + pull_index = runner.calls.index(["docker", "pull", image]) + exposed_inspect_index = next( + index + for index, call in enumerate(runner.calls) + if call[:3] == ["docker", "image", "inspect"] and "--format" in call + ) + run_index = next(index for index, call in enumerate(runner.calls) if call[:2] == ["docker", "run"]) + assert pull_index < exposed_inspect_index < run_index + assert "127.0.0.1:9100:80" in runner.calls[run_index] + + +def test_verified_stop_refuses_container_from_another_runtime_scope(tmp_path: Path) -> None: + runner = _Runner() + runner.existing_container = (True, "another-runtime") + runtime, _events = _runtime(tmp_path, runner) + + stopped = terminate_service_record( + runtime, + ServiceRecord( + name="Main Backend", + type="backend", + cwd=str(tmp_path), + runtime_kind="docker", + container_id="reused-container-id", + ), + aggressive=False, + verify_ownership=True, + ) + + assert not stopped + assert not any(call[:3] == ["docker", "rm", "--force"] for call in runner.calls) + + def test_container_state_drives_health_and_stop(tmp_path: Path) -> None: runner = _Runner() runtime, events = _runtime(tmp_path, runner) diff --git a/tests/python/runtime/test_engine_runtime_commands.py b/tests/python/runtime/test_engine_runtime_commands.py index 6730825d6..a8732d123 100644 --- a/tests/python/runtime/test_engine_runtime_commands.py +++ b/tests/python/runtime/test_engine_runtime_commands.py @@ -20,20 +20,19 @@ class EngineRuntimeCommandsTests(unittest.TestCase): - def test_docker_image_command_does_not_require_container_executable_on_host(self) -> None: + def test_docker_image_command_skips_host_command_resolution(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: root = Path(tmpdir) - (root / "frontend").mkdir() runtime = SimpleNamespace( env={ "DOCKER_MODE": "true", - "ENVCTL_FRONTEND_START_CMD": "container-only-server --port {port}", + "ENVCTL_FRONTEND_DOCKER_IMAGE": "example/frontend:dev", }, config=SimpleNamespace( base_dir=root, raw={"FRONTEND_DIR": "frontend"}, ), - _command_exists=lambda _executable: False, + _command_exists=lambda _executable: self.fail("host command lookup must be skipped"), ) command, source = service_start_command_resolved( @@ -43,8 +42,8 @@ def test_docker_image_command_does_not_require_container_executable_on_host(self port=8010, ) - self.assertEqual(command, ["container-only-server", "--port", "8010"]) - self.assertEqual(source, "configured") + self.assertEqual(command, []) + self.assertEqual(source, "docker_image") def test_docker_service_command_still_requires_executable_on_host(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: diff --git a/tests/python/runtime/test_lifecycle_dependency_stop.py b/tests/python/runtime/test_lifecycle_dependency_stop.py index 9f0ab968d..fc5e7b4b3 100644 --- a/tests/python/runtime/test_lifecycle_dependency_stop.py +++ b/tests/python/runtime/test_lifecycle_dependency_stop.py @@ -55,6 +55,40 @@ def run(command, **_kwargs): # noqa: ANN001 ) self.assertIn(["docker", "rm", "--force", "one", "two"], calls) + def test_stop_native_supabase_falls_back_to_saved_container_name(self) -> None: + calls: list[list[str]] = [] + + def run(command, **_kwargs): # noqa: ANN001 + argv = [str(part) for part in command] + calls.append(argv) + if argv[:3] == ["docker", "ps", "--all"]: + return SimpleNamespace(returncode=0, stdout="", stderr="") + return SimpleNamespace(returncode=0, stdout="", stderr="") + + stop_requirement_component_containers( + SimpleNamespace(process_runner=SimpleNamespace(run=run)), + { + "id": "supabase", + "enabled": True, + "container_name": "envctl-feature-supabase-db-1", + }, + ) + + self.assertEqual( + calls, + [ + [ + "docker", + "ps", + "--all", + "--quiet", + "--filter", + "label=com.docker.compose.project=envctl-feature-supabase-db-1", + ], + ["docker", "rm", "--force", "envctl-feature-supabase-db-1"], + ], + ) + def test_select_dependency_components_filters_unknown_projects_dependencies_and_disabled_components(self) -> None: state = RunState( run_id="run-1", diff --git a/tests/python/startup/test_resume_restore_policy.py b/tests/python/startup/test_resume_restore_policy.py index a5fa2b510..26c49ee52 100644 --- a/tests/python/startup/test_resume_restore_policy.py +++ b/tests/python/startup/test_resume_restore_policy.py @@ -6,6 +6,10 @@ from types import SimpleNamespace from envctl_engine.runtime.command_router import Route, parse_route +from envctl_engine.startup.resume_restore_execution import ( + _apply_restore_runtime_mode, + _route_for_resume_restore, +) from envctl_engine.startup.resume_restore_policy import ( _configured_restore_service_types, _requirements_reuse_decision, @@ -16,10 +20,39 @@ apply_ports_to_context, project_root, ) -from envctl_engine.state.models import RequirementsResult, ServiceRecord +from envctl_engine.state.models import RequirementsResult, RunState, ServiceRecord class ResumeRestorePolicyTests(unittest.TestCase): + def test_docker_state_forces_docker_mode_for_direct_resume_restore(self) -> None: + state = RunState( + run_id="run-docker", + mode="main", + services={ + "Main Backend": ServiceRecord( + name="Main Backend", + type="backend", + cwd="/repo/backend", + runtime_kind="docker", + container_id="stale-container", + ) + }, + ) + route = Route(command="resume", mode="main", flags={"batch": True}) + + restored_route = _route_for_resume_restore( + route, + state=state, + suppress_timing_output=False, + ) + + self.assertTrue(restored_route.flags["docker"]) + self.assertTrue(restored_route.flags["_resume_restore"]) + self.assertTrue(restored_route.flags["batch"]) + runtime = SimpleNamespace(env={}) + _apply_restore_runtime_mode(runtime, restored_route) + self.assertEqual(runtime.env["DOCKER_MODE"], "true") + def test_plan_resume_defaults_to_parallel_tree_restore(self) -> None: captured_routes: list[Route] = [] From 593e35a653ca1602d6ff6594f551385ed4358fba Mon Sep 17 00:00:00 2001 From: kfiramar <83420275+kfiramar@users.noreply.github.com> Date: Sat, 11 Jul 2026 23:16:15 +0300 Subject: [PATCH 5/6] fix second-round Docker runtime review findings --- .../runtime/docker_service_runtime.py | 25 ++++-- .../runtime/engine_runtime_commands.py | 7 +- .../runtime/lifecycle_cleanup_orchestrator.py | 18 +++- python/envctl_engine/startup/finalization.py | 6 +- python/envctl_engine/startup/lifecycle.py | 27 +++++- .../startup/run_reuse_decision.py | 5 +- .../startup/run_reuse_fresh_start.py | 2 + .../startup/service_attach_execution.py | 33 ++++--- .../startup/service_project_execution.py | 52 +++++++---- .../startup/startup_selection_support.py | 7 ++ .../envctl_engine/state/action_log_support.py | 29 +++++- .../runtime/test_engine_runtime_commands.py | 15 ++++ ...t_lifecycle_cleanup_spinner_integration.py | 79 ++++++++++++++++ .../python/startup/test_run_reuse_support.py | 90 +++++++++++++++++++ .../startup/test_service_attach_execution.py | 22 ++++- .../startup/test_startup_lifecycle_lock.py | 35 +++++++- .../test_startup_orchestrator_profiles.py | 6 +- .../startup/test_support_module_decoupling.py | 89 ++++++++++++++++++ .../state/test_state_action_log_support.py | 24 +++++ 19 files changed, 520 insertions(+), 51 deletions(-) diff --git a/python/envctl_engine/runtime/docker_service_runtime.py b/python/envctl_engine/runtime/docker_service_runtime.py index 781182b66..e52118116 100644 --- a/python/envctl_engine/runtime/docker_service_runtime.py +++ b/python/envctl_engine/runtime/docker_service_runtime.py @@ -63,14 +63,14 @@ def docker_service_mode_enabled(runtime: Any) -> bool: return parse_bool(str(env.get("DOCKER_MODE", "")), False) -def docker_service_uses_image_command( +def docker_service_container_command_source( runtime: Any, service_name: str, *, docker_mode: bool | None = None, -) -> bool: +) -> str | None: if not (docker_service_mode_enabled(runtime) if docker_mode is None else docker_mode): - return False + return None env = getattr(runtime, "env", {}) raw = getattr(getattr(runtime, "config", None), "raw", {}) suffix = re.sub(r"[^A-Z0-9]+", "_", service_name.upper()).strip("_") or "SERVICE" @@ -83,8 +83,23 @@ def setting(name: str) -> str: return "" if setting("DOCKER_COMMAND"): - return False - return (setting("DOCKER_COMMAND_MODE") or "image").lower() in {"image", "default"} + return "docker_command" + if (setting("DOCKER_COMMAND_MODE") or "image").lower() in {"image", "default"}: + return "docker_image" + return None + + +def docker_service_uses_image_command( + runtime: Any, + service_name: str, + *, + docker_mode: bool | None = None, +) -> bool: + return docker_service_container_command_source( + runtime, + service_name, + docker_mode=docker_mode, + ) == "docker_image" def state_uses_docker_services(state: object) -> bool: diff --git a/python/envctl_engine/runtime/engine_runtime_commands.py b/python/envctl_engine/runtime/engine_runtime_commands.py index 22843e289..690c6f427 100644 --- a/python/envctl_engine/runtime/engine_runtime_commands.py +++ b/python/envctl_engine/runtime/engine_runtime_commands.py @@ -7,7 +7,7 @@ from typing import Any, Mapping from envctl_engine.runtime.command_resolution import resolve_requirement_start_command, resolve_service_start_command -from envctl_engine.runtime.docker_service_runtime import docker_service_uses_image_command +from envctl_engine.runtime.docker_service_runtime import docker_service_container_command_source SERVICE_LAUNCH_ENV_REMOVALS = { "ACTIONS_CACHE_URL", @@ -106,8 +106,9 @@ def service_start_command_resolved( project_root: Path | None = None, port: int = 0, ) -> tuple[list[str], str]: - if docker_service_uses_image_command(runtime, service_name): - return [], "docker_image" + container_command_source = docker_service_container_command_source(runtime, service_name) + if container_command_source is not None: + return [], container_command_source result = resolve_service_start_command( service_name=service_name, project_root=(project_root or runtime.config.base_dir), diff --git a/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py b/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py index f145dc768..a2654fb56 100644 --- a/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py +++ b/python/envctl_engine/runtime/lifecycle_cleanup_orchestrator.py @@ -266,6 +266,19 @@ def _release_selected_dependency_components( def _stop_requirement_component_containers(self, component: Mapping[str, object]) -> None: stop_requirement_component_containers(self.runtime, component) + def _stop_requirement_component_containers_best_effort(self, component: Mapping[str, object]) -> None: + try: + self._stop_requirement_component_containers(component) + except Exception as exc: # noqa: BLE001 + component_id = str(component.get("id") or "dependency").strip() or "dependency" + detail = str(exc).strip() or exc.__class__.__name__ + self.runtime._emit( # type: ignore[attr-defined] + "cleanup.dependency_container.warning", + component=component_id, + detail=detail, + ) + print(f"Warning: could not stop {component_id} container during cleanup: {detail}") + def _release_requirement_component_ports(self, component: Mapping[str, object]) -> None: port_allocator = self._port_allocator(self.runtime) release_requirement_component_ports(component, release_port_fn=port_allocator.release) @@ -327,7 +340,10 @@ def clear_runtime_state(self, *, command: str, aggressive: bool = False, route: for definition in dependency_definitions(): component = requirements.component(definition.id) if bool(component.get("enabled", False)): - self._stop_requirement_component_containers(component) + if command in {"stop-all", "blast-all"}: + self._stop_requirement_component_containers_best_effort(component) + else: + self._stop_requirement_component_containers(component) if aggressive and self.blast_all_ecosystem_enabled(): self.blast_all_ecosystem_cleanup(route=route) diff --git a/python/envctl_engine/startup/finalization.py b/python/envctl_engine/startup/finalization.py index 77ce8611d..59ca72d26 100644 --- a/python/envctl_engine/startup/finalization.py +++ b/python/envctl_engine/startup/finalization.py @@ -96,6 +96,7 @@ def finalize_successful_startup( print_headless_plan_session_summary: Callable[[StartupSession], None], maybe_attach_plan_agent_terminal: Callable[[StartupSession], int | None], finalize_plan_agent_degraded_handoff: Callable[[StartupSession], int], + run_interactive_dashboard_loop: Callable[[RunState], int] | None = None, ) -> int: if session.plan_agent_handoff_degraded: return finalize_plan_agent_degraded_handoff(session) @@ -171,7 +172,8 @@ def finalize_successful_startup( if attach_code is not None: return attach_code if runtime._should_enter_post_start_interactive(session.effective_route): - return runtime._run_interactive_dashboard_loop(run_state) + dashboard_runner = run_interactive_dashboard_loop or runtime._run_interactive_dashboard_loop + return dashboard_runner(run_state) return 0 @@ -181,6 +183,7 @@ def finalize_successful_startup_with_runtime( *, validate_plan_agent_handoff: Callable[..., None], print_fn: Callable[[str], None] = print, + run_interactive_dashboard_loop: Callable[[RunState], int] | None = None, ) -> int: orchestrator_like = type("_StartupFinalizationRuntimeFacade", (), {"runtime": runtime})() return finalize_successful_startup( @@ -228,6 +231,7 @@ def finalize_successful_startup_with_runtime( validate_plan_agent_handoff=validate_plan_agent_handoff, print_fn=print_fn, ), + run_interactive_dashboard_loop=run_interactive_dashboard_loop, ) diff --git a/python/envctl_engine/startup/lifecycle.py b/python/envctl_engine/startup/lifecycle.py index 2eba8eaf0..7da06ef18 100644 --- a/python/envctl_engine/startup/lifecycle.py +++ b/python/envctl_engine/startup/lifecycle.py @@ -2,7 +2,7 @@ from functools import partial from pathlib import Path -from typing import Any +from typing import Any, Callable from envctl_engine.debug.debug_utils import file_lock from envctl_engine.planning.plan_agent.launch import launch_plan_agent_terminals @@ -53,6 +53,7 @@ trees_start_selection_required, ) from envctl_engine.shared.parsing import parse_float +from envctl_engine.state.models import RunState from envctl_engine.ui.debug_snapshot import emit_startup_plan_handoff_snapshot from envctl_engine.ui.spinner import spinner, use_spinner_policy from envctl_engine.ui.spinner_service import emit_spinner_policy, resolve_spinner_policy @@ -65,17 +66,36 @@ def execute_startup_lifecycle(orchestrator: Any, route: Any) -> int: ) timeout = max(parse_float(timeout_raw, 3600.0), 1.0) lock_path = Path(runtime.runtime_root) / "locks" / "startup.lock" + pending_dashboard_state: RunState | None = None + + def defer_interactive_dashboard(state: RunState) -> int: + nonlocal pending_dashboard_state + pending_dashboard_state = state + return 0 + try: with file_lock(lock_path, timeout=timeout): - return _execute_startup_lifecycle_locked(orchestrator, route) + code = _execute_startup_lifecycle_locked( + orchestrator, + route, + run_interactive_dashboard_loop=defer_interactive_dashboard, + ) except TimeoutError as exc: message = f"Startup could not acquire the repository lifecycle lock: {exc}" runtime._emit("startup.lock.timeout", lock_path=str(lock_path), timeout_seconds=timeout) print(message) return 1 + if code == 0 and pending_dashboard_state is not None: + return runtime._run_interactive_dashboard_loop(pending_dashboard_state) + return code -def _execute_startup_lifecycle_locked(orchestrator: Any, route: Any) -> int: +def _execute_startup_lifecycle_locked( + orchestrator: Any, + route: Any, + *, + run_interactive_dashboard_loop: Callable[[RunState], int] | None = None, +) -> int: runtime = orchestrator.runtime session = create_startup_session(runtime, route) finalize_failure = partial( @@ -113,6 +133,7 @@ def _execute_startup_lifecycle_locked(orchestrator: Any, route: Any) -> int: runtime, validate_plan_agent_attach_target, ), + run_interactive_dashboard_loop=run_interactive_dashboard_loop, ) except RuntimeError as exc: return finalize_failure(error=str(exc)) diff --git a/python/envctl_engine/startup/run_reuse_decision.py b/python/envctl_engine/startup/run_reuse_decision.py index 4c0a9228e..a77c851d7 100644 --- a/python/envctl_engine/startup/run_reuse_decision.py +++ b/python/envctl_engine/startup/run_reuse_decision.py @@ -336,7 +336,10 @@ def _candidate_fresh( def mark_run_reused(metadata: Mapping[str, object] | None, *, reason: str) -> dict[str, object]: updated = dict(metadata or {}) raw_count = updated.get("run_reuse_count", 0) - count = int(raw_count) if isinstance(raw_count, (bool, float, int, str)) else 0 + try: + count = int(raw_count) if isinstance(raw_count, (bool, float, int, str)) else 0 + except (OverflowError, TypeError, ValueError): + count = 0 updated["run_reuse_count"] = max(0, count) + 1 updated["last_reopened_at"] = datetime.now(tz=UTC).isoformat() updated["last_reuse_reason"] = reason diff --git a/python/envctl_engine/startup/run_reuse_fresh_start.py b/python/envctl_engine/startup/run_reuse_fresh_start.py index e1b350e21..53efcd16d 100644 --- a/python/envctl_engine/startup/run_reuse_fresh_start.py +++ b/python/envctl_engine/startup/run_reuse_fresh_start.py @@ -33,9 +33,11 @@ def fresh_start_replacement_services( if str(getattr(context, "name", "")).strip() } runtime_scope = str(route.flags.get("runtime_scope") or "").strip().lower() + has_app_launch_selection = any(key in route.flags for key in ("launch_backend", "launch_frontend")) replace_all_for_explicit_fresh_start = ( bool(route.flags.get("no_resume")) and not bool(route.flags.get("services")) + and not has_app_launch_selection and runtime_scope not in {"backend", "frontend", "dependencies", "fullstack"} ) selected: set[str] = set() diff --git a/python/envctl_engine/startup/service_attach_execution.py b/python/envctl_engine/startup/service_attach_execution.py index 686e44664..86aa1897d 100644 --- a/python/envctl_engine/startup/service_attach_execution.py +++ b/python/envctl_engine/startup/service_attach_execution.py @@ -11,8 +11,8 @@ from envctl_engine.runtime.docker_service_runtime import ( DockerServiceLaunch, DockerServiceRuntime, + docker_service_container_command_source, docker_service_mode_enabled, - docker_service_uses_image_command, ) from envctl_engine.startup.service_execution_policy import ordered_service_layers from envctl_engine.startup.service_execution_records import PreparedServiceLaunch @@ -310,11 +310,7 @@ def start_backend(self, port: int) -> tuple[bool, str | None, int | None]: if conflict_result is not None: return conflict_result command_resolve_started = time.monotonic() - command, _resolved_source = self.runtime._service_start_command_resolved( - service_name="backend", - project_root=self.project_root, - port=port, - ) + command = self._core_service_command("backend", port) self._emit_attach_phase("backend", "command_resolution", command_resolve_started) return self._launch_prepared_process( service_name="backend", @@ -337,11 +333,7 @@ def start_frontend(self, port: int) -> tuple[bool, str | None, int | None]: owner=f"{self.project_name}:services:frontend-launch", ) command_resolve_started = time.monotonic() - command, _resolved_source = self.runtime._service_start_command_resolved( - service_name="frontend", - project_root=self.project_root, - port=launch_port, - ) + command = self._core_service_command("frontend", launch_port) self._emit_attach_phase("frontend", "command_resolution", command_resolve_started) return self._launch_prepared_process( service_name="frontend", @@ -353,6 +345,21 @@ def start_frontend(self, port: int) -> tuple[bool, str | None, int | None]: pid_fallback=os.getpid() + 1, ) + def _core_service_command(self, service_name: str, port: int) -> list[str]: + container_command_source = docker_service_container_command_source( + self.runtime, + service_name, + docker_mode=self.docker_mode or docker_service_mode_enabled(self.runtime), + ) + if container_command_source is not None: + return [] + command, _resolved_source = self.runtime._service_start_command_resolved( + service_name=service_name, + project_root=self.project_root, + port=port, + ) + return command + def start_additional_service(self, service_name: str, port: int) -> tuple[bool, str | None, int | None]: conflict_result = self._conflicting_start_result(service_name, port) if conflict_result is not None: @@ -362,11 +369,11 @@ def start_additional_service(self, service_name: str, port: int) -> tuple[bool, if service is None: return False, f"unknown additional service {service_name}", None command_resolve_started = time.monotonic() - if docker_service_uses_image_command( + if docker_service_container_command_source( self.runtime, service_name, docker_mode=self.docker_mode or docker_service_mode_enabled(self.runtime), - ): + ) is not None: command = [] else: command = self.runtime._split_command( diff --git a/python/envctl_engine/startup/service_project_execution.py b/python/envctl_engine/startup/service_project_execution.py index 3a2c04d30..89077ea3f 100644 --- a/python/envctl_engine/startup/service_project_execution.py +++ b/python/envctl_engine/startup/service_project_execution.py @@ -6,7 +6,10 @@ from typing import cast from envctl_engine.runtime.command_resolution import CommandExists, suggest_service_start_command -from envctl_engine.runtime.docker_service_runtime import docker_service_mode_enabled +from envctl_engine.runtime.docker_service_runtime import ( + docker_service_container_command_source, + docker_service_mode_enabled, +) from envctl_engine.runtime.command_router import Route from envctl_engine.runtime.runtime_context import resolve_process_runtime from envctl_engine.startup.startup_selection_support import _restart_service_types_for_project @@ -282,12 +285,18 @@ def project_env_for_service(service_name: str) -> dict[str, str]: reason="all_services_disabled", ) return {} - if route is not None and route.command == "plan" and _no_local_app_system_configured( - rt, - mode=effective_mode, - project_root=context.root, - selected_service_types=selected_service_types, - configured_additional_services=configured_additional_services, + use_docker_services = bool(route is not None and route.flags.get("docker")) or docker_service_mode_enabled(rt) + if ( + route is not None + and route.command == "plan" + and not use_docker_services + and _no_local_app_system_configured( + rt, + mode=effective_mode, + project_root=context.root, + selected_service_types=selected_service_types, + configured_additional_services=configured_additional_services, + ) ): rt._emit( "service.attach.skipped", @@ -303,7 +312,6 @@ def project_env_for_service(service_name: str) -> dict[str, str]: return {} service_started = time.monotonic() - use_docker_services = bool(route is not None and route.flags.get("docker")) or docker_service_mode_enabled(rt) prepare_backend_duration_ms = 0.0 prepare_frontend_duration_ms = 0.0 prepare_backend = "backend" in selected_service_types @@ -387,18 +395,30 @@ def prepare_frontend_runtime() -> float: rebound_delta = parse_int(rt.env.get("ENVCTL_TEST_FRONTEND_REBOUND_DELTA"), 0) backend_command_source = None if "backend" in selected_service_types: - backend_command_source = rt._service_command_source( - service_name="backend", - project_root=context.root, - port=backend_plan.final, + backend_command_source = docker_service_container_command_source( + rt, + "backend", + docker_mode=use_docker_services, ) + if backend_command_source is None: + backend_command_source = rt._service_command_source( + service_name="backend", + project_root=context.root, + port=backend_plan.final, + ) frontend_command_source = None if "frontend" in selected_service_types: - frontend_command_source = rt._service_command_source( - service_name="frontend", - project_root=context.root, - port=(frontend_plan.final + rebound_delta if rebound_delta > 0 else frontend_plan.final), + frontend_command_source = docker_service_container_command_source( + rt, + "frontend", + docker_mode=use_docker_services, ) + if frontend_command_source is None: + frontend_command_source = rt._service_command_source( + service_name="frontend", + project_root=context.root, + port=(frontend_plan.final + rebound_delta if rebound_delta > 0 else frontend_plan.final), + ) prepared_launches: dict[str, PreparedServiceLaunch] = {} command_env_builder = resolve_command_env_builder(rt) diff --git a/python/envctl_engine/startup/startup_selection_support.py b/python/envctl_engine/startup/startup_selection_support.py index 2235e19a6..80d1475b4 100644 --- a/python/envctl_engine/startup/startup_selection_support.py +++ b/python/envctl_engine/startup/startup_selection_support.py @@ -472,6 +472,13 @@ def _restart_service_types_for_project( def _apply_startup_service_launch_flags(service_types: set[str], *, route: Route) -> set[str]: selected = set(service_types) + if "launch_backend" in route.flags or "launch_frontend" in route.flags: + explicitly_selected: set[str] = set() + if route.flags.get("launch_backend") is True: + explicitly_selected.add("backend") + if route.flags.get("launch_frontend") is True: + explicitly_selected.add("frontend") + return selected.intersection(explicitly_selected) if route.flags.get("launch_backend") is False: selected.discard("backend") if route.flags.get("launch_frontend") is False: diff --git a/python/envctl_engine/state/action_log_support.py b/python/envctl_engine/state/action_log_support.py index b73a67e63..64d8f6fc7 100644 --- a/python/envctl_engine/state/action_log_support.py +++ b/python/envctl_engine/state/action_log_support.py @@ -204,7 +204,34 @@ def clear_one(service: ServiceRecord) -> tuple[int, int, int, int, str | None]: if not raw_path: return 0, 0, 1, 0, (None if quiet else f"{service.name}: log=n/a") log_path = Path(raw_path) + is_docker = str(getattr(service, "runtime_kind", "process") or "process").lower() == "docker" if not log_path.is_file(): + if is_docker: + try: + Path(f"{log_path}.docker-since").touch() + line = f"{service.name}: Docker log cursor advanced at {log_path}" + return 1, 0, 0, 0, ( + None + if quiet + else render_paths_in_terminal_text( + line, + paths=[log_path], + env=env, + interactive_tty=interactive_tty, + ) + ) + except OSError as exc: + line = f"{service.name}: failed to clear Docker logs at {log_path} ({exc})" + return 0, 0, 0, 1, ( + None + if quiet + else render_paths_in_terminal_text( + line, + paths=[log_path], + env=env, + interactive_tty=interactive_tty, + ) + ) line = f"{service.name}: log missing at {log_path}" return 0, 1, 0, 0, ( None @@ -219,7 +246,7 @@ def clear_one(service: ServiceRecord) -> tuple[int, int, int, int, str | None]: try: with log_path.open("w", encoding="utf-8"): pass - if str(getattr(service, "runtime_kind", "process") or "process").lower() == "docker": + if is_docker: Path(f"{log_path}.docker-since").touch() line = f"{service.name}: log cleared at {log_path}" return 1, 0, 0, 0, ( diff --git a/tests/python/runtime/test_engine_runtime_commands.py b/tests/python/runtime/test_engine_runtime_commands.py index a8732d123..f0918c21f 100644 --- a/tests/python/runtime/test_engine_runtime_commands.py +++ b/tests/python/runtime/test_engine_runtime_commands.py @@ -45,6 +45,21 @@ def test_docker_image_command_skips_host_command_resolution(self) -> None: self.assertEqual(command, []) self.assertEqual(source, "docker_image") + def test_explicit_docker_command_skips_host_command_resolution(self) -> None: + runtime = SimpleNamespace( + env={ + "DOCKER_MODE": "true", + "ENVCTL_BACKEND_DOCKER_COMMAND": "uvicorn app:api --port 8000", + }, + config=SimpleNamespace(base_dir=Path("/repo"), raw={}), + _command_exists=lambda _executable: self.fail("host command lookup must be skipped"), + ) + + command, source = service_start_command_resolved(runtime, service_name="backend", port=8000) + + self.assertEqual(command, []) + self.assertEqual(source, "docker_command") + def test_docker_service_command_still_requires_executable_on_host(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: root = Path(tmpdir) diff --git a/tests/python/runtime/test_lifecycle_cleanup_spinner_integration.py b/tests/python/runtime/test_lifecycle_cleanup_spinner_integration.py index d28f7f304..02ebfe3a6 100644 --- a/tests/python/runtime/test_lifecycle_cleanup_spinner_integration.py +++ b/tests/python/runtime/test_lifecycle_cleanup_spinner_integration.py @@ -106,6 +106,85 @@ def _terminate_services_from_state(self, *args, **kwargs): # noqa: ANN001, ARG0 class LifecycleCleanupSpinnerIntegrationTests(unittest.TestCase): + def test_clear_runtime_state_purges_after_dependency_container_cleanup_failure(self) -> None: + for command, aggressive in (("stop-all", False), ("blast-all", True)): + with self.subTest(command=command): + runtime = _RuntimeStub() + runtime._try_load_existing_state = lambda *args, **kwargs: RunState( # type: ignore[method-assign] + run_id="run-1", + mode="main", + requirements={ + "Main": RequirementsResult( + project="Main", + redis={ + "id": "redis", + "enabled": True, + "container_name": "envctl-redis-main", + }, + ) + }, + ) + orchestrator = LifecycleCleanupOrchestrator(runtime) + orchestrator.blast_all_ecosystem_enabled = lambda: False # type: ignore[method-assign] + orchestrator.blast_all_purge_legacy_state_artifacts = lambda: None # type: ignore[method-assign] + output = StringIO() + + with ( + patch( + "envctl_engine.runtime.lifecycle_cleanup_orchestrator.stop_requirement_component_containers", + side_effect=RuntimeError("Docker daemon is unavailable"), + ), + redirect_stdout(output), + ): + orchestrator.clear_runtime_state(command=command, aggressive=aggressive) + + self.assertEqual(runtime.state_repository.purge_calls, [aggressive]) + self.assertIn("could not stop redis container", output.getvalue()) + warnings = [ + event + for event in runtime.events + if event.get("event") == "cleanup.dependency_container.warning" + ] + self.assertEqual( + warnings, + [ + { + "event": "cleanup.dependency_container.warning", + "component": "redis", + "detail": "Docker daemon is unavailable", + } + ], + ) + + def test_targeted_runtime_cleanup_remains_fail_closed(self) -> None: + runtime = _RuntimeStub() + runtime._try_load_existing_state = lambda *args, **kwargs: RunState( # type: ignore[method-assign] + run_id="run-1", + mode="main", + requirements={ + "Main": RequirementsResult( + project="Main", + redis={ + "id": "redis", + "enabled": True, + "container_name": "envctl-redis-main", + }, + ) + }, + ) + orchestrator = LifecycleCleanupOrchestrator(runtime) + + with ( + patch( + "envctl_engine.runtime.lifecycle_cleanup_orchestrator.stop_requirement_component_containers", + side_effect=RuntimeError("Docker daemon is unavailable"), + ), + self.assertRaisesRegex(RuntimeError, "Docker daemon is unavailable"), + ): + orchestrator.clear_runtime_state(command="stop", aggressive=False) + + self.assertEqual(runtime.state_repository.purge_calls, []) + def test_stop_all_emits_spinner_policy_and_lifecycle(self) -> None: runtime = _RuntimeStub() orchestrator = LifecycleCleanupOrchestrator(runtime) diff --git a/tests/python/startup/test_run_reuse_support.py b/tests/python/startup/test_run_reuse_support.py index 4f80f1735..21db183f1 100644 --- a/tests/python/startup/test_run_reuse_support.py +++ b/tests/python/startup/test_run_reuse_support.py @@ -7,6 +7,7 @@ from envctl_engine.startup.run_reuse_support import ( dashboard_stopped_service_entries, fresh_start_replacement_services, + mark_run_reused, prepare_dashboard_stopped_service_restore, replace_existing_project_services_for_fresh_start, metadata_without_dashboard_stopped_services, @@ -15,6 +16,12 @@ class RunReuseSupportTests(unittest.TestCase): + def test_mark_run_reused_recovers_from_malformed_counter(self) -> None: + updated = mark_run_reused({"run_reuse_count": "not-a-number"}, reason="exact_match") + + self.assertEqual(updated["run_reuse_count"], 1) + self.assertEqual(updated["last_reuse_reason"], "exact_match") + def test_run_reuse_debug_orch_groups_only_apply_to_plan_commands(self) -> None: runtime = SimpleNamespace(env={"ENVCTL_DEBUG_PLAN_ORCH_GROUP": "alpha+ beta,gamma ,,"}) @@ -219,6 +226,89 @@ def test_unfiltered_explicit_no_resume_replaces_tracked_removed_additional_servi self.assertEqual(selected, {"Main Backend", "Main Old Worker"}) + def test_explicit_no_resume_honors_launch_only_service_selection(self) -> None: + state = SimpleNamespace( + services={ + "Main Backend": SimpleNamespace(name="Main Backend", project="Main", type="backend"), + "Main Frontend": SimpleNamespace(name="Main Frontend", project="Main", type="frontend"), + "Main Old Worker": SimpleNamespace( + name="Main Old Worker", + project="Main", + type="old-worker", + service_slug="old-worker", + ), + } + ) + + cases = ( + ( + { + "no_resume": True, + "launch_backend": True, + "launch_frontend": False, + "launch_dependencies": False, + }, + {"Main Backend"}, + ), + ( + { + "no_resume": True, + "launch_backend": False, + "launch_frontend": True, + "launch_dependencies": False, + }, + {"Main Frontend"}, + ), + ( + { + "no_resume": True, + "launch_backend": False, + "launch_frontend": False, + "launch_dependencies": False, + }, + set(), + ), + ) + for flags, expected in cases: + with self.subTest(flags=flags): + selected = fresh_start_replacement_services( + route=Route(command="start", mode="main", flags=flags), + selected_contexts=[SimpleNamespace(name="Main")], + candidate_state=state, + configured_service_types={"backend", "frontend"}, + additional_services=(), + project_name_from_service=lambda _name: "Main", + ) + self.assertEqual(selected, expected) + + def test_explicit_no_resume_with_no_dependencies_still_replaces_all_app_services(self) -> None: + state = SimpleNamespace( + services={ + "Main Backend": SimpleNamespace(name="Main Backend", project="Main", type="backend"), + "Main Old Worker": SimpleNamespace( + name="Main Old Worker", + project="Main", + type="old-worker", + service_slug="old-worker", + ), + } + ) + + selected = fresh_start_replacement_services( + route=Route( + command="start", + mode="main", + flags={"no_resume": True, "launch_dependencies": False}, + ), + selected_contexts=[SimpleNamespace(name="Main")], + candidate_state=state, + configured_service_types={"backend"}, + additional_services=(), + project_name_from_service=lambda _name: "Main", + ) + + self.assertEqual(selected, {"Main Backend", "Main Old Worker"}) + def test_replace_existing_project_services_for_fresh_start_terminates_selected_services_and_orphans(self) -> None: route = Route(command="start", mode="trees", raw_args=["start"], flags={}) session = SimpleNamespace(effective_route=route, runtime_mode="trees") diff --git a/tests/python/startup/test_service_attach_execution.py b/tests/python/startup/test_service_attach_execution.py index c6001af36..d9688aed7 100644 --- a/tests/python/startup/test_service_attach_execution.py +++ b/tests/python/startup/test_service_attach_execution.py @@ -28,9 +28,8 @@ def test_explicit_docker_mode_launches_and_annotates_container_service(self) -> services=ServiceManager(), _conflict_remaining={}, _emit=lambda event, **payload: None, - _service_start_command_resolved=lambda service_name, project_root, port: ( - ["python", "app.py", "--port", str(port)], - "configured", + _service_start_command_resolved=lambda **kwargs: self.fail( + f"host command resolution must be skipped: {kwargs}" ), ) runner = ServiceAttachRunner( @@ -89,6 +88,23 @@ def test_explicit_docker_mode_launches_and_annotates_container_service(self) -> self.assertEqual(record.container_name, "envctl-app-main-backend") self.assertEqual(record.container_image, "example/backend:dev") docker_launch.assert_called_once() + self.assertEqual(docker_launch.call_args.kwargs["command"], []) + + runtime.config.raw["ENVCTL_BACKEND_DOCKER_COMMAND"] = "uvicorn app:api --port 8000" + self.assertEqual(runner._core_service_command("backend", 8001), []) + + runtime.config.raw.pop("ENVCTL_BACKEND_DOCKER_COMMAND") + runtime.config.raw["ENVCTL_BACKEND_DOCKER_COMMAND_MODE"] = "service" + resolved_calls: list[dict[str, object]] = [] + runtime._service_start_command_resolved = lambda **kwargs: ( + resolved_calls.append(kwargs) or ["container-service", "--port", str(kwargs["port"])], + "configured", + ) + self.assertEqual( + runner._core_service_command("backend", 8001), + ["container-service", "--port", "8001"], + ) + self.assertEqual(resolved_calls, [{"service_name": "backend", "project_root": project_root, "port": 8001}]) def test_runner_builds_layered_descriptors_and_preserves_additional_urls(self) -> None: project_root = Path("/tmp/envctl-project") diff --git a/tests/python/startup/test_startup_lifecycle_lock.py b/tests/python/startup/test_startup_lifecycle_lock.py index a7c39f0f5..d89206466 100644 --- a/tests/python/startup/test_startup_lifecycle_lock.py +++ b/tests/python/startup/test_startup_lifecycle_lock.py @@ -26,13 +26,46 @@ def recording_lock(path: Path, *, timeout: float): monkeypatch.setattr( lifecycle, "_execute_startup_lifecycle_locked", - lambda received_orchestrator, route: seen.append((received_orchestrator, route)) or 7, + lambda received_orchestrator, route, **_kwargs: seen.append((received_orchestrator, route)) or 7, ) assert lifecycle.execute_startup_lifecycle(orchestrator, "route") == 7 assert seen == [tmp_path / "locks" / "startup.lock", 3600.0, "entered", (orchestrator, "route"), "exited"] +def test_execute_startup_lifecycle_releases_lock_before_dashboard(monkeypatch, tmp_path: Path) -> None: + seen: list[str] = [] + state = SimpleNamespace(run_id="run-1") + + @contextmanager + def recording_lock(_path: Path, *, timeout: float): + _ = timeout + seen.append("lock-entered") + yield + seen.append("lock-exited") + + def execute_locked(_orchestrator, _route, *, run_interactive_dashboard_loop): # noqa: ANN001 + seen.append("startup-finalized") + return run_interactive_dashboard_loop(state) + + def dashboard(received_state: object) -> int: + assert received_state is state + seen.append("dashboard-entered") + return 0 + + runtime = SimpleNamespace( + runtime_root=tmp_path, + env={}, + config=SimpleNamespace(raw={}), + _run_interactive_dashboard_loop=dashboard, + ) + monkeypatch.setattr(lifecycle, "file_lock", recording_lock) + monkeypatch.setattr(lifecycle, "_execute_startup_lifecycle_locked", execute_locked) + + assert lifecycle.execute_startup_lifecycle(SimpleNamespace(runtime=runtime), "route") == 0 + assert seen == ["lock-entered", "startup-finalized", "lock-exited", "dashboard-entered"] + + def test_execute_startup_lifecycle_reports_lock_timeout(monkeypatch, tmp_path: Path, capsys) -> None: @contextmanager def timing_out_lock(path: Path, *, timeout: float): diff --git a/tests/python/startup/test_startup_orchestrator_profiles.py b/tests/python/startup/test_startup_orchestrator_profiles.py index a69b2e0e0..2fae84505 100644 --- a/tests/python/startup/test_startup_orchestrator_profiles.py +++ b/tests/python/startup/test_startup_orchestrator_profiles.py @@ -365,7 +365,7 @@ def test_startup_only_flags_select_individual_worktree_launch_parts(self) -> Non _restart_service_types_for_project( route=route, project_name="feature-a-1", - default_service_types={"backend", "frontend"}, + default_service_types={"backend", "frontend", "voice-runtime"}, ), {"frontend"}, ) @@ -376,7 +376,7 @@ def test_startup_only_flags_select_individual_worktree_launch_parts(self) -> Non _restart_service_types_for_project( route=route, project_name="feature-a-1", - default_service_types={"backend", "frontend"}, + default_service_types={"backend", "frontend", "voice-runtime"}, ), {"backend"}, ) @@ -387,7 +387,7 @@ def test_startup_only_flags_select_individual_worktree_launch_parts(self) -> Non _restart_service_types_for_project( route=route, project_name="feature-a-1", - default_service_types={"backend", "frontend"}, + default_service_types={"backend", "frontend", "voice-runtime"}, ), set(), ) diff --git a/tests/python/startup/test_support_module_decoupling.py b/tests/python/startup/test_support_module_decoupling.py index f58d1e238..7345c99f9 100644 --- a/tests/python/startup/test_support_module_decoupling.py +++ b/tests/python/startup/test_support_module_decoupling.py @@ -1013,6 +1013,95 @@ def test_start_project_services_skips_default_backend_frontend_when_no_local_sys self.assertEqual(len(warnings), 1) self.assertIn("No local app system is configured", str(warnings[0])) + def test_start_project_services_does_not_skip_docker_only_plan_app_system(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + project_root = root / "repo" + project_root.mkdir(parents=True, exist_ok=True) + run_dir = root / "runtime" / "run-1" + events: list[tuple[str, dict[str, object]]] = [] + prep_calls: list[str] = [] + + runtime = SimpleNamespace( + port_planner=_PortAllocatorStub(), + env={}, + config=SimpleNamespace( + raw={"ENVCTL_BACKEND_DOCKER_IMAGE": "example/backend:dev"}, + explicit_keys=("ENVCTL_BACKEND_DOCKER_IMAGE",), + backend_dir_name="backend", + frontend_dir_name="frontend", + additional_services=(), + backend_dependency_env_section_present=False, + frontend_dependency_env_section_present=False, + main_backend_dependency_env_section_present=False, + main_frontend_dependency_env_section_present=False, + trees_backend_dependency_env_section_present=False, + trees_frontend_dependency_env_section_present=False, + service_dependency_env_section_present={}, + mode_service_dependency_env_section_present={}, + all_app_service_names_for_mode=lambda mode, project_root=None: ("backend", "frontend"), + ), + services=SimpleNamespace(), + _invoke_envctl_hook=lambda **kwargs: SimpleNamespace(found=False, success=False, payload=None), + _run_dir_path=lambda run_id: run_dir, + _project_service_env=lambda context, requirements, route=None, service_name=None: {}, + _resolve_backend_env_file=lambda context, backend_cwd: (None, False), + _resolve_frontend_env_file=lambda context, frontend_cwd: None, + _service_env_from_file=lambda base_env, env_file, include_app_env_file, env_file_authoritative=False: dict(base_env), + _service_enabled_for_mode=lambda mode, service: True, + process_runner=SimpleNamespace(), + _prepare_backend_runtime=lambda **kwargs: prep_calls.append("backend"), + _prepare_frontend_runtime=lambda **kwargs: prep_calls.append("frontend"), + _service_command_source=lambda **kwargs: self.fail( + f"host command source must be skipped: {kwargs}" + ), + _command_exists=lambda command: False, + _emit=lambda event, **payload: events.append((event, payload)), + ) + orchestrator = SimpleNamespace( + runtime=runtime, + _restart_service_types_for_project=lambda **kwargs: {"backend", "frontend"}, + _suppress_timing_output=lambda route: True, + ) + context = SimpleNamespace( + name="Main", + root=project_root, + ports={ + "backend": SimpleNamespace(final=8000), + "frontend": SimpleNamespace(final=3000), + }, + ) + route = parse_route( + ["--plan", "feature-a", "--docker", "--entire-system", "--batch"], + env={}, + ) + + with mock.patch( + "envctl_engine.startup.service_project_execution.ServiceAttachRunner" + ) as runner_cls: + runner_cls.return_value.start.return_value = {} + records = start_project_services( + orchestrator, + context, + requirements=RequirementsResult(project="Main", components={}, health="healthy", failures=[]), + run_id="run-1", + route=route, + ) + + self.assertEqual(records, {}) + self.assertEqual(prep_calls, []) + self.assertTrue(runner_cls.call_args.kwargs["docker_mode"]) + self.assertEqual( + runner_cls.call_args.kwargs["prepared_launches"]["backend"].command_source, + "docker_image", + ) + self.assertFalse( + any( + event == "service.attach.skipped" and payload.get("reason") == "no_system_configured" + for event, payload in events + ) + ) + def test_start_project_services_keeps_missing_command_failure_for_explicit_enablement(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: root = Path(tmpdir) diff --git a/tests/python/state/test_state_action_log_support.py b/tests/python/state/test_state_action_log_support.py index ee9500ab0..c58fef5d5 100644 --- a/tests/python/state/test_state_action_log_support.py +++ b/tests/python/state/test_state_action_log_support.py @@ -103,6 +103,30 @@ def test_clear_service_logs_truncates_available_files_and_reports_missing_paths( self.assertIn("Main Frontend: log missing", rendered) self.assertIn("Main Worker: log=n/a", rendered) + def test_clear_service_logs_advances_docker_cursor_without_snapshot_file(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + log_path = Path(tmp) / "docker-backend.log" + marker = Path(f"{log_path}.docker-since") + state = RunState( + run_id="run-docker", + mode="main", + services={ + "Main Backend": ServiceRecord( + name="Main Backend", + type="backend", + cwd=tmp, + log_path=str(log_path), + runtime_kind="docker", + ) + }, + ) + + summary = StateActionLogSupport.clear_service_logs(state, quiet=True) + + self.assertEqual(summary, (1, 0, 0, 0)) + self.assertFalse(log_path.exists()) + self.assertTrue(marker.is_file()) + if __name__ == "__main__": unittest.main() From 8e3b1341822d2c1866cc90212fbbc0ecc168ecbf Mon Sep 17 00:00:00 2001 From: kfiramar <83420275+kfiramar@users.noreply.github.com> Date: Sat, 11 Jul 2026 23:45:55 +0300 Subject: [PATCH 6/6] fix final Docker runtime review edge cases --- .../actions/action_git_state_support.py | 8 +- .../runtime/docker_service_runtime.py | 21 ++++- .../runtime/lifecycle_dependency_stop.py | 10 +- .../startup/service_attach_execution.py | 18 ++++ .../actions/test_action_git_state_support.py | 28 +++++- .../runtime/test_docker_service_runtime.py | 27 ++++++ .../runtime/test_lifecycle_dependency_stop.py | 8 +- .../startup/test_service_attach_execution.py | 94 +++++++++++++++++++ 8 files changed, 205 insertions(+), 9 deletions(-) diff --git a/python/envctl_engine/actions/action_git_state_support.py b/python/envctl_engine/actions/action_git_state_support.py index 83fbe4738..4aa8b0944 100644 --- a/python/envctl_engine/actions/action_git_state_support.py +++ b/python/envctl_engine/actions/action_git_state_support.py @@ -93,6 +93,11 @@ def _origin_default_branch(git_root: Path, *, git_output: Callable[[Path, list[s def detect_pr_base_branch(git_root: Path, *, git_output: Callable[[Path, list[str]], str]) -> str: + for candidate in ("dev", "main", "master"): + for ref in (f"origin/{candidate}", candidate): + if git_output(git_root, ["rev-parse", "--verify", ref]).strip(): + return candidate + remote_heads = git_output( git_root, [ @@ -112,9 +117,6 @@ def detect_pr_base_branch(git_root: Path, *, git_output: Callable[[Path, list[st for candidate in ("dev", "main", "master"): if candidate in remote_branches: return candidate - for ref in (f"origin/{candidate}", candidate): - if git_output(git_root, ["rev-parse", "--verify", ref]).strip(): - return candidate return _origin_default_branch(git_root, git_output=git_output) or "master" diff --git a/python/envctl_engine/runtime/docker_service_runtime.py b/python/envctl_engine/runtime/docker_service_runtime.py index e52118116..54be280e0 100644 --- a/python/envctl_engine/runtime/docker_service_runtime.py +++ b/python/envctl_engine/runtime/docker_service_runtime.py @@ -32,6 +32,9 @@ _PUBLIC_ENV_KEYS = { "BACKEND_CORS_ORIGINS", "CORS_ORIGINS", + "CORS_ORIGINS_RAW", + "ENVCTL_SOURCE_FRONTEND_URL", + "FRONTEND_BASE_URL", "FRONTEND_URL", "PUBLIC_URL", } @@ -538,6 +541,7 @@ def _docker_build_arguments( def _container_env(self, values: Mapping[str, str]) -> dict[str, str]: result: dict[str, str] = {} + browser_facing_keys = self._browser_facing_env_keys() for raw_key, raw_value in values.items(): key = str(raw_key) if key in _HOST_ENV_KEYS or key.startswith("TERM_") or key.startswith("LC_"): @@ -549,7 +553,7 @@ def _container_env(self, values: Mapping[str, str]) -> dict[str, str]: }: result[key] = "0.0.0.0" continue - if key not in _PUBLIC_ENV_KEYS and not key.startswith(_PUBLIC_ENV_PREFIXES): + if key not in browser_facing_keys and not key.startswith(_PUBLIC_ENV_PREFIXES): value = re.sub( r"(://(?:[^/@\s]+@)?)(localhost|127\.0\.0\.1)(?=[:/]|$)", r"\1host.docker.internal", @@ -562,6 +566,21 @@ def _container_env(self, values: Mapping[str, str]) -> dict[str, str]: result["ENVCTL_HOST_GATEWAY"] = "host.docker.internal" return result + def _browser_facing_env_keys(self) -> set[str]: + keys = set(_PUBLIC_ENV_KEYS) + runtime_env = getattr(self.runtime, "env", {}) + config_raw = getattr(getattr(self.runtime, "config", None), "raw", {}) + cors_key = str( + runtime_env.get( + "ENVCTL_BACKEND_CORS_ENV_KEY", + config_raw.get("ENVCTL_BACKEND_CORS_ENV_KEY", ""), + ) + or "" + ).strip() + if cors_key: + keys.add(cors_key) + return keys + def _write_env_file(self, *, project_name: str, service_name: str, values: Mapping[str, str]) -> Path: run_dir = Path(self.runtime._run_dir_path(getattr(self.runtime, "_active_run_id", None) or "docker")) run_dir.mkdir(parents=True, exist_ok=True) diff --git a/python/envctl_engine/runtime/lifecycle_dependency_stop.py b/python/envctl_engine/runtime/lifecycle_dependency_stop.py index 4f7f5ac06..a2a9481e0 100644 --- a/python/envctl_engine/runtime/lifecycle_dependency_stop.py +++ b/python/envctl_engine/runtime/lifecycle_dependency_stop.py @@ -99,6 +99,7 @@ def stop_requirement_component_containers(runtime: Any, component: Mapping[str, if process_runner is None: return if str(component.get("id") or "").strip().lower() == "supabase": + compose_project = _supabase_compose_project(container_name) listed = process_runner.run( [ "docker", @@ -106,7 +107,7 @@ def stop_requirement_component_containers(runtime: Any, component: Mapping[str, "--all", "--quiet", "--filter", - f"label=com.docker.compose.project={container_name}", + f"label=com.docker.compose.project={compose_project}", ], timeout=30.0, ) @@ -125,3 +126,10 @@ def stop_requirement_component_containers(runtime: Any, component: Mapping[str, removed = process_runner.run(["docker", "rm", "--force", container_name], timeout=30.0) if removed.returncode != 0 and "No such container" not in str(removed.stderr or ""): raise RuntimeError(str(removed.stderr or removed.stdout or f"failed stopping {container_name}").strip()) + + +def _supabase_compose_project(container_name: str) -> str: + db_container_suffix = "-supabase-db-1" + if container_name.endswith(db_container_suffix): + return container_name[: -len(db_container_suffix)] + return container_name diff --git a/python/envctl_engine/startup/service_attach_execution.py b/python/envctl_engine/startup/service_attach_execution.py index 86aa1897d..fa3f2226e 100644 --- a/python/envctl_engine/startup/service_attach_execution.py +++ b/python/envctl_engine/startup/service_attach_execution.py @@ -278,6 +278,9 @@ def _container_actual_port( if ready: return requested if listener_expected else None detail = f"container {launch.container_name} exited or did not become ready" + cleanup_detail = self._remove_failed_container_launch(service_name, launch) + if cleanup_detail: + detail = f"{detail}; {cleanup_detail}" self.runtime._emit( "service.failure", project=self.project_name, @@ -288,6 +291,21 @@ def _container_actual_port( ) raise RuntimeError(detail) + def _remove_failed_container_launch( + self, + service_name: str, + launch: DockerServiceLaunch, + ) -> str | None: + try: + removed = DockerServiceRuntime(self.runtime, self.process_runtime).stop(launch.container_id) + except Exception as exc: # noqa: BLE001 - preserve the launch in state for a later stop retry + return f"failed to remove container: {exc}" + if not removed: + return "failed to remove container" + if self._container_launches.get(service_name) == launch: + self._container_launches.pop(service_name, None) + return None + def _annotate_container_records(self, records: dict[str, ServiceRecord]) -> None: for record in records.values(): service_name = str(record.service_slug or record.type) diff --git a/tests/python/actions/test_action_git_state_support.py b/tests/python/actions/test_action_git_state_support.py index 3afca2459..2d9974942 100644 --- a/tests/python/actions/test_action_git_state_support.py +++ b/tests/python/actions/test_action_git_state_support.py @@ -71,11 +71,35 @@ def fake_git_output(_git_root: Path, args: list[str]) -> str: self.assertEqual(resolve({"origin/master"}), "master") self.assertEqual(resolve({"master"}), "master") self.assertEqual(resolve(set()), "master") - self.assertEqual(resolve({"origin/main"}, {"dev", "main"}), "dev") - self.assertEqual(resolve({"dev"}, {"main"}), "dev") + self.assertEqual(resolve(set(), {"dev", "main"}), "dev") + self.assertEqual(resolve(set(), {"main", "master"}), "main") self.assertEqual(resolve(set(), origin_default="develop"), "develop") self.assertEqual(resolve(set(), origin_default="trunk", symbolic_default=False), "trunk") + def test_detect_pr_base_branch_does_not_probe_origin_when_local_candidate_exists(self) -> None: + calls: list[list[str]] = [] + + def local_main(_git_root: Path, args: list[str]) -> str: + calls.append(args) + if args == ["rev-parse", "--verify", "origin/main"]: + return "abc123\n" + if args[0] == "ls-remote": + raise AssertionError("remote must not be probed when a local base ref is available") + return "" + + self.assertEqual( + git_state.detect_pr_base_branch(Path("/repo"), git_output=local_main), + "main", + ) + self.assertEqual( + calls, + [ + ["rev-parse", "--verify", "origin/dev"], + ["rev-parse", "--verify", "dev"], + ["rev-parse", "--verify", "origin/main"], + ], + ) + def test_existing_pr_url_uses_gh_pr_list_for_open_branch(self) -> None: calls: list[tuple[list[str], str]] = [] diff --git a/tests/python/runtime/test_docker_service_runtime.py b/tests/python/runtime/test_docker_service_runtime.py index 0328c8fdd..84ed397aa 100644 --- a/tests/python/runtime/test_docker_service_runtime.py +++ b/tests/python/runtime/test_docker_service_runtime.py @@ -272,6 +272,33 @@ def test_configured_image_runs_without_build_and_preserves_browser_public_url(tm assert launch.image == "example/frontend:dev" +def test_container_env_preserves_browser_contract_urls_and_rewrites_internal_urls(tmp_path: Path) -> None: + runner = _Runner() + runtime, _events = _runtime( + tmp_path, + runner, + env={"ENVCTL_BACKEND_CORS_ENV_KEY": "ALLOWED_BROWSER_ORIGINS"}, + ) + + projected = DockerServiceRuntime(runtime, runner)._container_env( + { + "FRONTEND_BASE_URL": "http://localhost:5173", + "ENVCTL_SOURCE_FRONTEND_URL": "http://127.0.0.1:5173", + "CORS_ORIGINS_RAW": "http://localhost:5173,http://127.0.0.1:5173", + "ALLOWED_BROWSER_ORIGINS": "http://localhost:5173", + "DATABASE_URL": "postgres://user:pass@localhost:5432/app", + "REDIS_URL": "redis://127.0.0.1:6379/0", + } + ) + + assert projected["FRONTEND_BASE_URL"] == "http://localhost:5173" + assert projected["ENVCTL_SOURCE_FRONTEND_URL"] == "http://127.0.0.1:5173" + assert projected["CORS_ORIGINS_RAW"] == "http://localhost:5173,http://127.0.0.1:5173" + assert projected["ALLOWED_BROWSER_ORIGINS"] == "http://localhost:5173" + assert projected["DATABASE_URL"] == "postgres://user:pass@host.docker.internal:5432/app" + assert projected["REDIS_URL"] == "redis://host.docker.internal:6379/0" + + def test_remote_image_is_pulled_before_exposed_port_detection(tmp_path: Path) -> None: runner = _Runner() image = "example/frontend:remote" diff --git a/tests/python/runtime/test_lifecycle_dependency_stop.py b/tests/python/runtime/test_lifecycle_dependency_stop.py index fc5e7b4b3..da6f36971 100644 --- a/tests/python/runtime/test_lifecycle_dependency_stop.py +++ b/tests/python/runtime/test_lifecycle_dependency_stop.py @@ -45,7 +45,11 @@ def run(command, **_kwargs): # noqa: ANN001 ) stop_requirement_component_containers( runtime, - {"id": "supabase", "enabled": True, "container_name": "envctl-supabase-alpha"}, + { + "id": "supabase", + "enabled": True, + "container_name": "envctl-supabase-alpha-supabase-db-1", + }, ) self.assertIn(["docker", "rm", "--force", "envctl-redis-alpha"], calls) @@ -83,7 +87,7 @@ def run(command, **_kwargs): # noqa: ANN001 "--all", "--quiet", "--filter", - "label=com.docker.compose.project=envctl-feature-supabase-db-1", + "label=com.docker.compose.project=envctl-feature", ], ["docker", "rm", "--force", "envctl-feature-supabase-db-1"], ], diff --git a/tests/python/startup/test_service_attach_execution.py b/tests/python/startup/test_service_attach_execution.py index d9688aed7..4664803f3 100644 --- a/tests/python/startup/test_service_attach_execution.py +++ b/tests/python/startup/test_service_attach_execution.py @@ -106,6 +106,100 @@ def test_explicit_docker_mode_launches_and_annotates_container_service(self) -> ) self.assertEqual(resolved_calls, [{"service_name": "backend", "project_root": project_root, "port": 8001}]) + def test_failed_optional_docker_readiness_removes_container_before_degrading(self) -> None: + project_root = Path("/tmp/envctl-project") + optional_service = SimpleNamespace( + name="voice-runtime", + start_cmd="python voice.py --port {port}", + depends_on=(), + start_order=25, + expect_listener=True, + critical=False, + env_suffix="VOICE_RUNTIME", + ) + runtime = SimpleNamespace( + env={"ENVCTL_VOICE_RUNTIME_DOCKER_IMAGE": "example/voice:dev"}, + config=SimpleNamespace( + raw={}, + app_service_by_name=lambda name: optional_service if name == "voice-runtime" else None, + ), + services=ServiceManager(), + _conflict_remaining={}, + _emit=lambda event, **payload: None, + _split_command=lambda *args, **kwargs: self.fail("host command resolution must be skipped"), + ) + runner = ServiceAttachRunner( + runtime=runtime, + process_runtime=SimpleNamespace(), + port_allocator=SimpleNamespace(reserve_next=lambda port, owner: port), + project_name="Main", + project_root=project_root, + backend_plan=port_plan(8000), + frontend_plan=port_plan(5173), + backend_cwd=project_root / "backend", + frontend_cwd=project_root / "frontend", + backend_log_path="/logs/backend.txt", + frontend_log_path="/logs/frontend.txt", + backend_env_extra={}, + frontend_env_extra={}, + command_env_builder=lambda port, extra: {**extra, "PORT": str(port)}, + prepared_launches={ + "voice-runtime": PreparedServiceLaunch( + service_name="voice-runtime", + cwd=project_root / "voice-runtime", + log_path="/logs/voice.txt", + requested_port=8010, + env={"PORT": "8010"}, + command_source="configured", + ) + }, + selected_service_types={"voice-runtime"}, + additional_services=(optional_service,), + backend_listener_expected=True, + rebound_delta=0, + docker_mode=True, + ) + launch = DockerServiceLaunch( + container_id="failed-container-id", + container_name="envctl-app-main-voice-runtime", + image="example/voice:dev", + log_path="/logs/voice.txt", + ) + + with ( + patch( + "envctl_engine.startup.service_attach_execution.DockerServiceRuntime.launch", + return_value=launch, + ), + patch( + "envctl_engine.startup.service_attach_execution.DockerServiceRuntime.wait_until_ready", + return_value=False, + ), + patch( + "envctl_engine.startup.service_attach_execution.DockerServiceRuntime.stop", + return_value=True, + ) as stop_container, + ): + records = runner.start(attach_parallel=False, on_service_retry=lambda *args: None) + + record = records["Main Voice Runtime"] + self.assertEqual(record.status, "degraded") + self.assertTrue(record.degraded) + self.assertIsNone(record.container_id) + self.assertEqual(runner._container_launches, {}) + stop_container.assert_called_once_with("failed-container-id") + + runner._container_launches["voice-runtime"] = launch + with patch( + "envctl_engine.startup.service_attach_execution.DockerServiceRuntime.stop", + return_value=False, + ): + self.assertEqual( + runner._remove_failed_container_launch("voice-runtime", launch), + "failed to remove container", + ) + self.assertEqual(runner._container_launches, {"voice-runtime": launch}) + def test_runner_builds_layered_descriptors_and_preserves_additional_urls(self) -> None: project_root = Path("/tmp/envctl-project") events: list[tuple[str, dict[str, object]]] = []