You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PERF: reduce CLI cold-start latency for repeated tool invocations #584
bm CLI tool calls have significant cold-start overhead in interactive/plugin integrations because each invocation re-pays Python import/bootstrap cost.
Cloud sub-commands and importers are always imported (even for search-notes)
No lazy imports inside tool.py — all MCP tools loaded regardless of which one is invoked
Requested Features Assessment
1. Lightweight fast-path entrypoint — Feasible. The biggest wins come from deferring MCP tool imports inside tool.py until a command is actually invoked. main.py already has the pattern for --version; the same idea can apply per-subcommand.
2. Daemon mode — Complex. Would require a socket/pipe IPC layer, lifecycle management, and a protocol for invoking commands in the long-lived process. Significant architectural work. The ASGI in-process model (run_with_cleanup) means the "server" is already restarted each invocation.
3. Lazy-loading heavy dependencies — Feasible and highest ROI. Cloud dependencies (rclone, jwt, aiofiles) and importer dependencies (HTML parsers, etc.) are loaded on every bm tool search-notes even when not needed.
Suggested Implementation Approach
Phase 1 (high-ROI, low risk): Per-command lazy imports in tool.py
Move the MCP tool imports inside the command functions rather than at module level. E.g.:
In main.py, move cloud command imports behind a flag or registration callback that only fires when the cloud subcommand is invoked.
Phase 3 (complex, optional): Daemon/socket mode
A persistent bm daemon process that tool commands connect to via a Unix socket, amortizing Python startup and ASGI initialization across calls. Would need careful process lifecycle management.
Acceptance Criteria from Issue
The issue asks for:
Benchmarks for search-notes, read-note, build-context, project list
Noticeable median latency reduction in warm and cold scenarios
No behavior regressions
A good first PR would add a benchmark script/fixture, implement Phase 1 lazy imports, and show before/after timing numbers.
Activity
claude commentedon Feb 18, 2026
Claude finished @phernandez's task in 1m 57s —— View job
Issue Triage: PERF: reduce CLI cold-start latency for repeated tool invocations
Classification
basic_memory/cli/)Labels applied:
enhancementArchitecture Analysis
After reviewing the CLI startup path, here's what happens on every
bm tool <cmd>invocation:Cold-start chain:
main.py→ importsbasic_memory.cli.app(triggersloguru,typer,ConfigManager,promo,config)app.py@app.callback()→ runsCliContainer.create()→ConfigManager()→ reads/parses config TOMLcloud/,mcp/, importers)tool.pymodule-level imports pull in MCP tools, schemas, clients — these chain-import FastAPI, SQLAlchemy, httpx, pydantic, etc.asyncioevent loop, connects to in-process ASGI app, waits for responseWhat's already been done:
main.pyhas a--versionfast path that skips command module importsapp_callbackskipsensure_initializationfortool,project,status, etc.What's still expensive:
tool.pyimports 6+ MCP tool modules and their full dependency chainsapp.pyimportspromo.py,container.py,config.pyunconditionallysearch-notes)tool.py— all MCP tools loaded regardless of which one is invokedRequested Features Assessment
1. Lightweight fast-path entrypoint — Feasible. The biggest wins come from deferring MCP tool imports inside
tool.pyuntil a command is actually invoked.main.pyalready has the pattern for--version; the same idea can apply per-subcommand.2. Daemon mode — Complex. Would require a socket/pipe IPC layer, lifecycle management, and a protocol for invoking commands in the long-lived process. Significant architectural work. The ASGI in-process model (
run_with_cleanup) means the "server" is already restarted each invocation.3. Lazy-loading heavy dependencies — Feasible and highest ROI. Cloud dependencies (
rclone,jwt,aiofiles) and importer dependencies (HTML parsers, etc.) are loaded on everybm tool search-noteseven when not needed.Suggested Implementation Approach
Phase 1 (high-ROI, low risk): Per-command lazy imports in
tool.pyMove the MCP tool imports inside the command functions rather than at module level. E.g.:
This avoids pulling in FastAPI/SQLAlchemy/httpx until the command actually runs.
Phase 2 (medium complexity): Lazy cloud sub-command registration
In
main.py, move cloud command imports behind a flag or registration callback that only fires when thecloudsubcommand is invoked.Phase 3 (complex, optional): Daemon/socket mode
A persistent
bm daemonprocess that tool commands connect to via a Unix socket, amortizing Python startup and ASGI initialization across calls. Would need careful process lifecycle management.Acceptance Criteria from Issue
The issue asks for:
search-notes,read-note,build-context,project listA good first PR would add a benchmark script/fixture, implement Phase 1 lazy imports, and show before/after timing numbers.
No duplicates found.
phernandez commentedon Feb 18, 2026
Downstream tracking