2dc1e227eb
* fix(langgraph-dev): rotate langgraph_dev.log when it exceeds 50MB ``_LOG_FILE`` (``~/.config/evoscientist/langgraph_dev.log``) was opened in ``start_langgraph_dev`` with plain ``"ab"`` and never rotated, so it grew unbounded over weeks/months of heavy use — especially when chatty MCP servers spawned by langgraph dev filled it, or when failure paths produced stack traces. Implement the recommended option 1 from #209: filesize-based rollover. When the active log exceeds 50MB on the next ``start_langgraph_dev`` invocation, rename it to ``langgraph_dev.log.1`` (overwriting any existing backup) via ``os.replace`` and start fresh. Single-backup policy keeps the disk footprint bounded at roughly 2x threshold. Rotation is best-effort: ``_rotate_log_if_needed`` logs and swallows OSError so a permission error or racing rename can't block langgraph dev from starting. The next ``start`` invocation will try again — worst case the log grows for one more session. Options 2 (timestamped per-session + 7-day sweep) and 3 (``RotatingFileHandler`` + pipe) are explicitly NOT done — option 1 is simplest, no async machinery, matches the issue's recommendation. Closes #209 * test(langgraph-dev): redirect _PID_DIR in rotate integration test Address CodeRabbit review comment on #270: the ``TestStartLanggraphDevRotatesLog::test_rotate_called_before_open`` test patched only ``_LOG_FILE`` to a tmp path, but ``start_langgraph_dev`` also calls ``_PID_DIR.mkdir(...)`` as part of its prelude, which would create a real directory under ``~/.config/evoscientist/`` on a dev machine. Redirect ``_PID_DIR`` to ``tmp_path / "pids"`` too so the test stays fully isolated. Add a final assertion that ``pid_dir.is_dir()`` holds, proving the function reached past the mkdir call. * refactor(langgraph-dev): bundle runtime paths into LanggraphRuntimePaths @din0s review follow-up on #270: the previous test isolation patched only ``_LOG_FILE`` (and after a second round, ``_PID_DIR``), but ``start_langgraph_dev`` still touches 5 distinct on-disk paths. Patching any subset of those still leaves the others pointing at the user's real ``~/.config/evoscientist/`` — exactly the case that produced the "Port 6174 cannot be bound after waiting 60s" symptom on the reviewer's machine. Replace the five free-floating module-level constants (``_PID_DIR`` / ``_PID_FILE`` / ``_LOG_FILE`` / ``_WORKSPACE_SIDECAR`` / ``_FILE_LOCK_PATH``) with a single ``LanggraphRuntimePaths`` frozen dataclass exposed as a module-level ``RUNTIME`` instance. Production code accesses ``RUNTIME.pid_file`` etc.; tests can now substitute the *whole* bundle in one assignment: monkeypatch.setattr( manager, "RUNTIME", manager.LanggraphRuntimePaths.for_directory(tmp_path / "runtime"), ) The classmethod ``for_directory(pid_dir)`` builds an isolated bundle rooted at a single dir, so the test author doesn't spell out every path field. Tests that only care about one field (e.g. pid_file during the stale-process kill path) use ``dataclasses.replace(manager.RUNTIME, pid_file=X)`` — frozen dataclass-friendly, no need to enumerate the other four fields. The dataclass's docstring records the migration rationale (the old five-name layout invited inconsistent patches). External callers of the old constants updated: - ``EvoScientist/deploy/server.py`` and ``webui.py`` now import ``RUNTIME`` and use ``RUNTIME.log_file`` for the on-screen log path hint. The other imports they had (``_DEFAULT_PORT``, ``_is_port_occupied``, ``_read_workspace_sidecar``) are still module-level functions/values, untouched. Test updates: - ``tests/test_langgraph_manager.py``: ``patch.object(manager, "_XXX", X)`` patterns now go through ``dataclasses.replace(manager.RUNTIME, xxx=X)``; the ``TestStartLanggraphDevRotatesLog::test_rotate_called_before_open`` test (from the previous #270 review iteration) uses ``for_directory`` for one-shot isolation. - ``tests/test_langgraph_dev_workspace_sidecar.py``: each test now goes through a tiny ``_isolated_runtime(monkeypatch, tmp_path)`` helper that calls ``for_directory``. - ``tests/test_langgraph_dev_deploy_mode.py``: same ``for_directory`` swap. No production behavior change. All ``langgraph_dev``-side tests (``test_langgraph_manager.py`` 26/26, ``test_langgraph_dev_workspace_sidecar.py`` 14/14, ``test_langgraph_dev_deploy_mode.py`` 14/14, ``test_cli_deploy.py`` 18/18 — which indirectly exercises deploy/server.py and deploy/webui.py imports) pass. Full-project test count unchanged from baseline; the remaining 22 Windows-only pre-existing failures (test_background ``os.killpg``, test_file_mentions tilde, mcp_client ``shutil.which``, test_sessions 8.3 short path) are documented as out-of-scope for #207. * style: apply ruff format to langgraph_dev test + module files CI lint check on #270 failed: Run ruff format --check . Would reformat: EvoScientist/langgraph_dev/manager.py Would reformat: tests/test_langgraph_manager.py Plus two test files touched by the prior consolidation commit that ``ruff format`` hadn't seen yet: tests/test_langgraph_dev_deploy_mode.py tests/test_langgraph_dev_workspace_sidecar.py Just formatting. No logic change. All 75 refactor-related tests pass. * fix(test): use for_directory for full path isolation + patch _can_bind_port to skip real socket ops Two fixes for TestStartLanggraphDevRotatesLog: 1. Replace dataclasses.replace(manager.RUNTIME, ...) with LanggraphRuntimePaths.for_directory(pid_dir) so pid_file, workspace_sidecar, and lock_file are also temp-rooted (prevents leak to ~/.config/evoscientist/). 2. Monkeypatch _can_bind_port to always return True so the bind-poll loop in _wait_for_port_bindable passes immediately without touching real sockets (fixes 60s timeout on machines where port 6174 is already in use). * fix: cross-platform compatibility for Windows CI runners - background.py: replace POSIX-only os.killpg/os.getpgid with cross-platform _kill_process_tree() helper. On Windows falls back to Popen.terminate()/Popen.kill() (TerminateProcess); on POSIX keeps existing os.killpg logic. - test_backends.py: replace mkdir -p shell execution in test_literal_workspace_path_replaced with preprocessing-boundary assertion (patch LocalShellBackend.execute, capture command, assert workspace path was rewritten to ./). Avoids POSIX-only mkdir -p on Windows runners. - test_file_mentions.py: monkeypatch USERPROFILE on Windows so ntpath.expanduser() resolves ~ to tmp_path even when HOME is unset on CI runners. * refactor(test): add runtime_paths fixture to isolate manager.RUNTIME Adds a reusable fixture that monkeypatches manager.RUNTIME to a temp-rooted LanggraphRuntimePaths.for_directory(). Tests that need specific fields can still dataclasses.replace(runtime_paths, ...) but the baseline is always temp-isolated, preventing leaks to ~/.config/evoscientist/. Updated test_langgraph_dev_deploy_mode.py, test_langgraph_dev_workspace_sidecar.py, and test_langgraph_manager.py to use the fixture, consolidating sequential lock_file + pid_dir patches into single dataclasses.replace calls. * Revert "fix: cross-platform compatibility for Windows CI runners" This reverts commit eb025d24af32e195a982cd40f6d70dba885c4019. * style: ruff format conftest.py * fix: address review issues in log-rotation + runtime paths - Use for_directory(tmp_path/pids) as base in ensure_langgraph_dev tests so pid_file/log_file are co-located with pid_dir, not split across paths - Remove unused runtime_paths param from test_no_existing_file_is_noop - Replace manager.RUNTIME with runtime_paths in two sidecar tests - Use for_directory(DEFAULT_PID_DIR) instead of explicit construction - Fix stale _LOG_FILE reference in TestRotateLogIfNeeded docstring * style: ruff format test files --------- Co-authored-by: Xi Zhang <106144707+X-iZhang@users.noreply.github.com>
270 lines
10 KiB
Python
270 lines
10 KiB
Python
"""Tests for ``start_langgraph_dev(deploy_mode=...)`` env var injection.
|
|
|
|
Verifies the single-env-var enum routing:
|
|
- ``deploy_mode=True`` → ``EVOSCIENTIST_DEPLOY_MODE=full``
|
|
- ``deploy_mode=False`` → ``EVOSCIENTIST_DEPLOY_MODE=stripped``
|
|
- (parent process / plain import) → ``EVOSCIENTIST_DEPLOY_MODE`` unset
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import dataclasses
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from EvoScientist.langgraph_dev import manager
|
|
|
|
|
|
class _PopenAbort(Exception):
|
|
"""Raised by the fake ``Popen`` to short-circuit ``start_langgraph_dev``
|
|
after the env dict is constructed but before health-polling runs."""
|
|
|
|
|
|
def _patch_start_prereqs(monkeypatch, tmp_path: Path, runtime_paths) -> dict:
|
|
"""Mock everything ``start_langgraph_dev`` does before ``subprocess.Popen``
|
|
so we can run it end-to-end up to the point where the env dict is captured.
|
|
Returns a ``captured`` dict that the test populates from the fake Popen."""
|
|
captured: dict = {}
|
|
|
|
monkeypatch.setattr(manager, "_langgraph_exe", lambda: "/usr/bin/langgraph")
|
|
|
|
fake_config = tmp_path / "langgraph.json"
|
|
fake_config.write_text("{}")
|
|
monkeypatch.setattr(manager, "_packaged_langgraph_config", lambda: fake_config)
|
|
|
|
# No conflicts, no stale process — straight to spawn.
|
|
monkeypatch.setattr(manager, "is_langgraph_dev_running", lambda **_: False)
|
|
monkeypatch.setattr(manager, "_is_port_occupied", lambda _port: False)
|
|
monkeypatch.setattr(manager, "_wait_for_port_bindable", lambda _port: True)
|
|
monkeypatch.setattr(manager, "_kill_owned_stale_process", lambda _port: False)
|
|
monkeypatch.setattr(
|
|
manager, "_wait_for_port_release", lambda _port, timeout=10.0: True
|
|
)
|
|
|
|
# Redirect the log file — pid_dir already rooted under tmp via the fixture.
|
|
monkeypatch.setattr(
|
|
manager,
|
|
"RUNTIME",
|
|
dataclasses.replace(runtime_paths, log_file=tmp_path / "langgraph_dev.log"),
|
|
)
|
|
|
|
def _fake_popen(args, **kwargs):
|
|
captured["args"] = args
|
|
captured["env"] = kwargs.get("env", {})
|
|
captured["cwd"] = kwargs.get("cwd")
|
|
raise _PopenAbort("env captured")
|
|
|
|
monkeypatch.setattr(subprocess, "Popen", _fake_popen)
|
|
return captured
|
|
|
|
|
|
def test_deploy_mode_true_sets_full(monkeypatch, tmp_path, runtime_paths):
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
|
|
with pytest.raises(_PopenAbort):
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16174,
|
|
deploy_mode=True,
|
|
)
|
|
|
|
env = captured["env"]
|
|
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == "full", (
|
|
"deploy_mode=True must inject EVOSCIENTIST_DEPLOY_MODE=full"
|
|
)
|
|
|
|
|
|
def test_deploy_mode_false_default_sets_stripped(monkeypatch, tmp_path, runtime_paths):
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
|
|
with pytest.raises(_PopenAbort):
|
|
# deploy_mode omitted → defaults to False
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16175,
|
|
)
|
|
|
|
env = captured["env"]
|
|
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == "stripped", (
|
|
"deploy_mode=False (default) must inject EVOSCIENTIST_DEPLOY_MODE=stripped"
|
|
)
|
|
|
|
|
|
def test_deploy_mode_explicitly_false_sets_stripped(
|
|
monkeypatch, tmp_path, runtime_paths
|
|
):
|
|
"""Same as default, but with deploy_mode=False stated explicitly."""
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
|
|
with pytest.raises(_PopenAbort):
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16176,
|
|
deploy_mode=False,
|
|
)
|
|
|
|
env = captured["env"]
|
|
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == "stripped"
|
|
|
|
|
|
def test_deploy_mode_always_set_to_one_of_full_or_stripped(
|
|
monkeypatch, tmp_path, runtime_paths
|
|
):
|
|
"""Regression: the subprocess always sees exactly one of the two enum
|
|
values for ``EVOSCIENTIST_DEPLOY_MODE`` — never unset, never garbage."""
|
|
for deploy_mode, expected in ((True, "full"), (False, "stripped")):
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
with pytest.raises(_PopenAbort):
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16177,
|
|
deploy_mode=deploy_mode,
|
|
)
|
|
env = captured["env"]
|
|
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == expected, (
|
|
f"deploy_mode={deploy_mode}: expected EVOSCIENTIST_DEPLOY_MODE="
|
|
f"{expected!r}, got {env.get('EVOSCIENTIST_DEPLOY_MODE')!r}"
|
|
)
|
|
|
|
|
|
def test_inherited_stripped_overridden_when_deploy_mode_true(
|
|
monkeypatch, tmp_path, runtime_paths
|
|
):
|
|
"""If the parent process exports ``EVOSCIENTIST_DEPLOY_MODE=stripped`` and
|
|
we ask for deploy mode, the subprocess env must see the resolved value
|
|
(``full``), not the stale inherited one."""
|
|
monkeypatch.setenv("EVOSCIENTIST_DEPLOY_MODE", "stripped")
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
|
|
with pytest.raises(_PopenAbort):
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16180,
|
|
deploy_mode=True,
|
|
)
|
|
|
|
env = captured["env"]
|
|
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == "full", (
|
|
"inherited stripped value must be overridden when deploy_mode=True"
|
|
)
|
|
|
|
|
|
def test_inherited_full_overridden_when_deploy_mode_false(
|
|
monkeypatch, tmp_path, runtime_paths
|
|
):
|
|
"""Symmetric: parent exports ``EVOSCIENTIST_DEPLOY_MODE=full``, CLI/serve
|
|
calls start_langgraph_dev with default (deploy_mode=False), inherited
|
|
value must be overridden to ``stripped``."""
|
|
monkeypatch.setenv("EVOSCIENTIST_DEPLOY_MODE", "full")
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
|
|
with pytest.raises(_PopenAbort):
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16181,
|
|
)
|
|
|
|
env = captured["env"]
|
|
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == "stripped", (
|
|
"inherited full value must be overridden when deploy_mode=False"
|
|
)
|
|
|
|
|
|
def test_inherited_arbitrary_value_overridden(monkeypatch, tmp_path, runtime_paths):
|
|
"""Defense against an unexpected inherited value (e.g. legacy ``true``
|
|
from before the enum rename, or any user-set garbage). The resolved
|
|
deploy_mode always wins."""
|
|
for inherited in ("true", "garbage", "FULL", ""):
|
|
for deploy_mode, expected in ((True, "full"), (False, "stripped")):
|
|
monkeypatch.setenv("EVOSCIENTIST_DEPLOY_MODE", inherited)
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
|
|
with pytest.raises(_PopenAbort):
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16182,
|
|
deploy_mode=deploy_mode,
|
|
)
|
|
|
|
env = captured["env"]
|
|
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == expected, (
|
|
f"inherited={inherited!r}, deploy_mode={deploy_mode}: "
|
|
f"expected EVOSCIENTIST_DEPLOY_MODE={expected!r}, "
|
|
f"got {env.get('EVOSCIENTIST_DEPLOY_MODE')!r}"
|
|
)
|
|
|
|
|
|
def test_workspace_dir_env_var_set_regardless_of_mode(
|
|
monkeypatch, tmp_path, runtime_paths
|
|
):
|
|
"""EVOSCIENTIST_WORKSPACE_DIR is independent of deploy_mode."""
|
|
for deploy_mode in (True, False):
|
|
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
|
|
with pytest.raises(_PopenAbort):
|
|
manager.start_langgraph_dev(
|
|
workspace_dir=tmp_path,
|
|
port=16178,
|
|
deploy_mode=deploy_mode,
|
|
)
|
|
assert captured["env"].get("EVOSCIENTIST_WORKSPACE_DIR") == str(tmp_path)
|
|
|
|
|
|
# =============================================================================
|
|
# Module-load behavior — _ASYNC_SUBAGENTS_AVAILABLE reads env var on import
|
|
# =============================================================================
|
|
|
|
|
|
def test_async_subagents_available_init_from_env_full(monkeypatch):
|
|
"""When ``EVOSCIENTIST_DEPLOY_MODE=full`` is set in the env at module
|
|
import time, ``_ASYNC_SUBAGENTS_AVAILABLE`` initializes to True so the
|
|
deployed main agent's ``_maybe_swap_async_subagents`` swaps eagerly
|
|
without waiting for ``start_langgraph_dev`` to flip the flag (which it
|
|
can't — the deploy subprocess never calls that function on itself)."""
|
|
monkeypatch.setenv("EVOSCIENTIST_DEPLOY_MODE", "full")
|
|
# Re-import the module to re-run the module-level initialization.
|
|
import importlib
|
|
|
|
import EvoScientist.langgraph_dev.manager as mgr
|
|
|
|
reloaded = importlib.reload(mgr)
|
|
try:
|
|
assert reloaded._ASYNC_SUBAGENTS_AVAILABLE is True
|
|
assert reloaded.is_async_subagents_available() is True
|
|
finally:
|
|
# Restore: reload again without the env var so subsequent tests
|
|
# see the normal initialization.
|
|
monkeypatch.delenv("EVOSCIENTIST_DEPLOY_MODE", raising=False)
|
|
importlib.reload(mgr)
|
|
|
|
|
|
def test_async_subagents_available_init_false_for_stripped(monkeypatch):
|
|
"""``stripped`` is the CLI/serve subprocess mode — async sub-agents stay
|
|
disabled at module-load time (they get enabled later by ``ensure_langgraph_dev``
|
|
in the parent process, NOT by the subprocess flipping its own flag)."""
|
|
monkeypatch.setenv("EVOSCIENTIST_DEPLOY_MODE", "stripped")
|
|
import importlib
|
|
|
|
import EvoScientist.langgraph_dev.manager as mgr
|
|
|
|
reloaded = importlib.reload(mgr)
|
|
try:
|
|
assert reloaded._ASYNC_SUBAGENTS_AVAILABLE is False
|
|
finally:
|
|
monkeypatch.delenv("EVOSCIENTIST_DEPLOY_MODE", raising=False)
|
|
importlib.reload(mgr)
|
|
|
|
|
|
def test_async_subagents_available_init_false_without_env(monkeypatch):
|
|
"""When the env var is unset, ``_ASYNC_SUBAGENTS_AVAILABLE`` initializes
|
|
to False — the pre-existing safety behavior (fall back to sync if
|
|
langgraph dev isn't reachable)."""
|
|
monkeypatch.delenv("EVOSCIENTIST_DEPLOY_MODE", raising=False)
|
|
import importlib
|
|
|
|
import EvoScientist.langgraph_dev.manager as mgr
|
|
|
|
reloaded = importlib.reload(mgr)
|
|
assert reloaded._ASYNC_SUBAGENTS_AVAILABLE is False
|