Skip to content

Commit 47ee982

Browse files
authored
perf(cli): defer local ASGI app import (#828)
Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 4d22c39 commit 47ee982

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/basic_memory/mcp/async_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from loguru import logger
77

88
import logfire
9-
from basic_memory.api.app import app as fastapi_app
109
from basic_memory.config import ConfigManager, ProjectMode
1110

1211

@@ -37,6 +36,9 @@ def _build_timeout() -> Timeout:
3736

3837
def _asgi_client(timeout: Timeout) -> AsyncClient:
3938
"""Create a local ASGI client."""
39+
# Import on first local-client use so CLI help/version paths can import
40+
# routing helpers without constructing the full FastAPI router graph.
41+
from basic_memory.api.app import app as fastapi_app
4042
from basic_memory.workspace_context import workspace_permalink_headers
4143

4244
return AsyncClient(

tests/cli/test_cli_exit.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,26 @@ def test_bm_version_does_not_import_heavy_modules():
8080
assert "CLEAN" in result.stdout, (
8181
f"Heavy modules loaded during --version: {result.stdout.strip()}"
8282
)
83+
84+
85+
def test_bm_help_does_not_import_api_app():
86+
"""Regression test: 'bm --help' must not build the FastAPI app graph."""
87+
check_script = (
88+
"import sys; "
89+
"sys.argv = ['bm', '--help']; "
90+
"import basic_memory.cli.main; "
91+
"heavy = [m for m in sys.modules "
92+
"if m == 'basic_memory.api.app' or m.startswith('basic_memory.api.v2.routers')]; "
93+
"print(','.join(heavy) if heavy else 'CLEAN')"
94+
)
95+
result = subprocess.run(
96+
["uv", "run", "python", "-c", check_script],
97+
capture_output=True,
98+
text=True,
99+
timeout=10,
100+
cwd=Path(__file__).parent.parent.parent,
101+
)
102+
assert result.returncode == 0
103+
assert "CLEAN" in result.stdout, (
104+
f"API app modules loaded during --help: {result.stdout.strip()}"
105+
)

0 commit comments

Comments
 (0)