Files
deepagents/libs/code/scripts/check_imports.py
Mason Daugherty 220dfc0e6b fix(code): defer server graph construction (#4300)
Import-only checks were constructing the server graph, which let runtime
startup behavior leak into local validation and touch a developer’s real
dcode config. The server graph is now exposed as a LangGraph factory so
MCP discovery runs only when the server actually builds the graph, and
the import checker runs against an isolated home directory.

## Changes

- Switch the generated LangGraph reference from `server_graph.py:graph`
to `server_graph.py:make_graph`, relying on LangGraph’s factory support
instead of constructing the graph at module import time.
- Keep startup error marker behavior inside `make_graph()` so server
startup failures still surface cleanly to the parent process, while
plain imports remain side-effect free.
- Run `check_imports.py` with a temporary `HOME` so import validation
cannot read or depend on local `~/.deepagents` config, MCP auth tokens,
or other user state.
- Update server graph tests to assert MCP discovery does not happen on
import and still happens when `make_graph()` is invoked.
- Override `UV_FROZEN` only for the `uv lock --check` command so `make
check` performs the real lockfile freshness check without warning.

## Testing

- `make -C libs/code check PYTHON_FILES=
PYTEST_EXTRA="tests/unit_tests/test_server_graph.py
tests/unit_tests/test_server_manager.py -q" COV_ARGS=`
- `make -C libs/code check_imports`
- Focused ruff, ty, and pytest checks for the touched server
graph/server manager files
2026-06-26 00:11:29 -04:00

45 lines
1.5 KiB
Python

"""Check imports script.
Quickly verify that a list of Python files can be loaded by the Python interpreter
without raising any errors. Ran before running more expensive tests. Useful in
Makefiles.
If loading a file fails, the script prints the problematic filename and the detailed
error traceback.
"""
import os
import random
import string
import sys
import tempfile
import traceback
from importlib.machinery import SourceFileLoader
if __name__ == "__main__":
files = sys.argv[1:]
has_failure = False
with tempfile.TemporaryDirectory() as home:
# Point the home directory at a throwaway dir so importing a module can't
# read or depend on the developer's real `~` state (e.g. `~/.deepagents`
# config, MCP auth tokens). `Path.home()` resolves from `HOME` on POSIX
# and `USERPROFILE` / `HOMEDRIVE`+`HOMEPATH` on Windows, so override all
# of them to keep the isolation cross-platform.
os.environ["HOME"] = home
os.environ["USERPROFILE"] = home
os.environ.pop("HOMEDRIVE", None)
os.environ.pop("HOMEPATH", None)
for file in files:
try:
module_name = "".join(
random.choice(string.ascii_letters) for _ in range(20)
)
SourceFileLoader(module_name, file).load_module()
except Exception:
has_failure = True
print(file)
traceback.print_exc()
print()
sys.exit(1 if has_failure else 0)