fix(langgraph-dev): rotate langgraph_dev.log when it exceeds 50MB (#270)

* 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>
This commit is contained in:
houren Antony
2026-06-09 22:55:44 +08:00
committed by GitHub
parent cd2baa9588
commit 2dc1e227eb
7 changed files with 484 additions and 106 deletions
+34 -21
View File
@@ -8,6 +8,7 @@ Verifies the single-env-var enum routing:
from __future__ import annotations
import dataclasses
import subprocess
from pathlib import Path
@@ -21,7 +22,7 @@ class _PopenAbort(Exception):
after the env dict is constructed but before health-polling runs."""
def _patch_start_prereqs(monkeypatch, tmp_path: Path) -> dict:
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."""
@@ -42,10 +43,12 @@ def _patch_start_prereqs(monkeypatch, tmp_path: Path) -> dict:
manager, "_wait_for_port_release", lambda _port, timeout=10.0: True
)
# _PID_DIR / _LOG_FILE point under user dir — redirect to tmp.
pid_dir = tmp_path / "pid_dir"
monkeypatch.setattr(manager, "_PID_DIR", pid_dir)
monkeypatch.setattr(manager, "_LOG_FILE", tmp_path / "langgraph_dev.log")
# 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
@@ -57,8 +60,8 @@ def _patch_start_prereqs(monkeypatch, tmp_path: Path) -> dict:
return captured
def test_deploy_mode_true_sets_full(monkeypatch, tmp_path):
captured = _patch_start_prereqs(monkeypatch, tmp_path)
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(
@@ -73,8 +76,8 @@ def test_deploy_mode_true_sets_full(monkeypatch, tmp_path):
)
def test_deploy_mode_false_default_sets_stripped(monkeypatch, tmp_path):
captured = _patch_start_prereqs(monkeypatch, tmp_path)
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
@@ -89,9 +92,11 @@ def test_deploy_mode_false_default_sets_stripped(monkeypatch, tmp_path):
)
def test_deploy_mode_explicitly_false_sets_stripped(monkeypatch, tmp_path):
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)
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
with pytest.raises(_PopenAbort):
manager.start_langgraph_dev(
@@ -104,11 +109,13 @@ def test_deploy_mode_explicitly_false_sets_stripped(monkeypatch, tmp_path):
assert env.get("EVOSCIENTIST_DEPLOY_MODE") == "stripped"
def test_deploy_mode_always_set_to_one_of_full_or_stripped(monkeypatch, tmp_path):
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)
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
with pytest.raises(_PopenAbort):
manager.start_langgraph_dev(
workspace_dir=tmp_path,
@@ -122,12 +129,14 @@ def test_deploy_mode_always_set_to_one_of_full_or_stripped(monkeypatch, tmp_path
)
def test_inherited_stripped_overridden_when_deploy_mode_true(monkeypatch, tmp_path):
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)
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
with pytest.raises(_PopenAbort):
manager.start_langgraph_dev(
@@ -142,12 +151,14 @@ def test_inherited_stripped_overridden_when_deploy_mode_true(monkeypatch, tmp_pa
)
def test_inherited_full_overridden_when_deploy_mode_false(monkeypatch, tmp_path):
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)
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
with pytest.raises(_PopenAbort):
manager.start_langgraph_dev(
@@ -161,14 +172,14 @@ def test_inherited_full_overridden_when_deploy_mode_false(monkeypatch, tmp_path)
)
def test_inherited_arbitrary_value_overridden(monkeypatch, tmp_path):
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)
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
with pytest.raises(_PopenAbort):
manager.start_langgraph_dev(
@@ -185,10 +196,12 @@ def test_inherited_arbitrary_value_overridden(monkeypatch, tmp_path):
)
def test_workspace_dir_env_var_set_regardless_of_mode(monkeypatch, tmp_path):
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)
captured = _patch_start_prereqs(monkeypatch, tmp_path, runtime_paths)
with pytest.raises(_PopenAbort):
manager.start_langgraph_dev(
workspace_dir=tmp_path,