Skip to content

PERF: reduce CLI cold-start latency for repeated tool invocations #584

Closed

Description

@phernandez

Problem

bm CLI tool calls have significant cold-start overhead in interactive/plugin integrations because each invocation re-pays Python import/bootstrap cost.

Related downstream issue: basicmachines-co/openclaw-basic-memory#18

Impact

Per-call startup latency compounds heavily in composited workflows (multiple tool invocations per user request), causing multi-second perceived lag.

Requested improvements (any combination)

  1. Lightweight/fast path entrypoint for common tool commands with minimal imports.
  2. Optional long-running local daemon mode for CLI tools to reuse initialized state.
  3. More aggressive lazy-loading for heavy semantic/cloud dependencies unless command path needs them.

Acceptance criteria

  • Documented benchmark for representative commands (search-notes, read-note, build-context, project list).
  • Noticeable median latency reduction for local-only commands in warm and cold scenarios.
  • No behavior regressions for existing CLI contract.

Activity

  1. claude commented on Feb 18, 2026

    @claude
    Contributor

    Claude finished @phernandez's task in 1m 57s —— View job


    Issue Triage: PERF: reduce CLI cold-start latency for repeated tool invocations

    • Classify issue type and priority
    • Investigate current CLI startup architecture
    • Check for duplicate issues
    • Apply appropriate labels
    • Provide triage summary

    Classification

    Field Value
    Type Enhancement (performance improvement)
    Priority Medium — affects UX in composited workflows but not a blocking defect
    Component CLI (basic_memory/cli/)
    Complexity Complex — daemon mode is architectural; lazy-loading requires careful dependency analysis

    Labels applied: enhancement


    Architecture Analysis

    After reviewing the CLI startup path, here's what happens on every bm tool <cmd> invocation:

    Cold-start chain:

    1. main.py → imports basic_memory.cli.app (triggers loguru, typer, ConfigManager, promo, config)
    2. app.py @app.callback() → runs CliContainer.create() → ConfigManager() → reads/parses config TOML
    3. Then all command modules are imported (including heavy ones like cloud/, mcp/, importers)
    4. tool.py module-level imports pull in MCP tools, schemas, clients — these chain-import FastAPI, SQLAlchemy, httpx, pydantic, etc.
    5. For each tool command: spawns asyncio event loop, connects to in-process ASGI app, waits for response

    What's already been done:

    • main.py has a --version fast path that skips command module imports
    • app_callback skips ensure_initialization for tool, project, status, etc.

    What's still expensive:

    • Module-level tool.py imports 6+ MCP tool modules and their full dependency chains
    • app.py imports promo.py, container.py, config.py unconditionally
    • 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.:

    @tool_app.command("search-notes")
    def search_notes(...):
        from basic_memory.mcp.tools import search_notes as mcp_search  # deferred
        ...

    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 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.


    No duplicates found.

  2. added
    enhancement
    on Feb 18, 2026
  3. phernandez commented on Feb 18, 2026

    @phernandez
    MemberAuthor
  4. added this to the v0.19.0 milestone on Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions