refactor: make the two remaining heavy __init__ modules lazy
tracker-policy-pr / check-pr (pull_request) Successful in 13s
test / integration-docker (pull_request) Successful in 35s
test / unit (pull_request) Successful in 51s
lint / lint (push) Failing after 58s
test / integration-firecracker (pull_request) Successful in 3m32s
test / coverage (pull_request) Successful in 19s
test / publish-infra (pull_request) Has been skipped

Audit of package __init__ import cost turned up two eager ones:

orchestrator/__init__.py (27 -> 2): was pure `from .X import Y`
re-exports (registry_store, broker, docker_broker, gateway, service,
server). Because importing any submodule runs the parent __init__, the
CLI migration gate (orchestrator.store.store_manager, hit on every
command) transitively dragged backend.docker, docker_broker, server, and
http.server. Converted to the same lazy __getattr__ + _LAZY facade used
by manifest/backend/egress/git_gate; the re-export API is unchanged and
the docker/http drag is gone.

cli/commands/__init__.py (78 -> 23): the registry imported all twelve
handlers to build COMMANDS, so importing one command pulled all of them
plus their deps. COMMANDS now maps each name to a thin lazy wrapper that
imports its handler's module on first dispatch. Values stay callable, so
the dispatcher and the patch.dict dispatch tests are unchanged; a CLI run
now loads only the one command it dispatches. The remaining 23 is the
dispatcher's own baseline (store_manager migration gate + help + log).

Full unit suite green (2243); `bb help` + dispatch/migration-gate tests
verified.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-24 18:54:41 -04:00
parent 3efb014ace
commit e2740842a0
2 changed files with 94 additions and 61 deletions
+39 -28
View File
@@ -1,41 +1,52 @@
"""CLI subcommand registry. """CLI subcommand registry.
One module per `bot-bottle <command>`, each exposing a `cmd_<name>(argv)` One module per `bot-bottle <command>`, each exposing a `cmd_<name>(argv)`
handler. This package `__init__` assembles them into the COMMANDS table handler. This package `__init__` maps command names to their handlers
the dispatcher (`bot_bottle.cli.__main__`) reads. Shared CLI helpers **lazily**: a short-lived CLI run dispatches exactly one command, so
(`constants`, `tui`) stay one level up in the `cli` package. importing all twelve handlers (and their transitive deps — backend,
manifest, orchestrator, …) up front is wasted work. Each COMMANDS value is
a thin wrapper that imports its handler's module on first call. Shared CLI
helpers (`constants`, `tui`) stay one level up in the `cli` package.
""" """
from __future__ import annotations from __future__ import annotations
from .backend import cmd_backend from importlib import import_module
from .cleanup import cmd_cleanup from typing import Callable
from .commit import cmd_commit
from .edit import cmd_edit
from .help import cmd_help
from .info import cmd_info
from .init import cmd_init
from .list import cmd_list
from .login import cmd_login
from .resume import cmd_resume
from .start import cmd_start
from .supervise import cmd_supervise
COMMANDS = { # command name -> "<submodule>:<handler attr>". Kept as strings so building
"backend": cmd_backend, # the registry imports nothing; the module loads only when dispatched.
"cleanup": cmd_cleanup, _HANDLERS: dict[str, str] = {
"commit": cmd_commit, "backend": "backend:cmd_backend",
"edit": cmd_edit, "cleanup": "cleanup:cmd_cleanup",
"help": cmd_help, "commit": "commit:cmd_commit",
"info": cmd_info, "edit": "edit:cmd_edit",
"init": cmd_init, "help": "help:cmd_help",
"list": cmd_list, "info": "info:cmd_info",
"login": cmd_login, "init": "init:cmd_init",
"resume": cmd_resume, "list": "list:cmd_list",
"start": cmd_start, "login": "login:cmd_login",
"supervise": cmd_supervise, "resume": "resume:cmd_resume",
"start": "start:cmd_start",
"supervise": "supervise:cmd_supervise",
} }
def _lazy(spec: str) -> Callable[[list[str]], "int | None"]:
"""Wrap a `<module>:<attr>` handler so its module is imported only when
the command is actually dispatched, not when the registry is built."""
module, attr = spec.split(":")
def run(argv: list[str]) -> "int | None":
handler = getattr(import_module(f".{module}", __name__), attr)
return handler(argv)
run.__name__ = attr
return run
COMMANDS = {name: _lazy(spec) for name, spec in _HANDLERS.items()}
# Commands that manage host prerequisites (or are otherwise store-free) and # Commands that manage host prerequisites (or are otherwise store-free) and
# must run before — or without — a migrated DB. `backend` provisions/probes # must run before — or without — a migrated DB. `backend` provisions/probes
# the host (TAP pool, /dev/kvm, firecracker) and never opens the store, so # the host (TAP pool, /dev/kvm, firecracker) and never opens the store, so
+49 -27
View File
@@ -5,7 +5,7 @@ A single persistent per-host service that will run the gateway functions
agent launches. This package is being built bottom-up, starting with the agent launches. This package is being built bottom-up, starting with the
backend-neutral "consolidation core" that needs no VM packaging: backend-neutral "consolidation core" that needs no VM packaging:
* `registry` — the SQLite runtime-state store + fail-closed * `store.registry_store` — the SQLite runtime-state store + fail-closed
attribution (source IP + per-bottle identity token). attribution (source IP + per-bottle identity token).
* `broker` — the signed, structured launch-request contract + a * `broker` — the signed, structured launch-request contract + a
`LaunchBroker` (stub for the harness) that verifies `LaunchBroker` (stub for the harness) that verifies
@@ -28,36 +28,58 @@ orchestrator -> firecracker).
from __future__ import annotations from __future__ import annotations
from .store.registry_store import BottleRecord, RegistryStore, new_identity_token from typing import TYPE_CHECKING, Any
from .broker import (
if TYPE_CHECKING:
from .store.registry_store import BottleRecord, RegistryStore, new_identity_token
from .broker import (
BrokerAuthError, BrokerAuthError,
LaunchBroker, LaunchBroker,
LaunchRequest, LaunchRequest,
StubBroker, StubBroker,
sign_request, sign_request,
verify_request, verify_request,
) )
from .docker_broker import DockerBroker, DockerBrokerError from .docker_broker import DockerBroker, DockerBrokerError
from ..gateway import Gateway, GatewayError from ..gateway import Gateway, GatewayError
from .service import Orchestrator from .service import Orchestrator
from .server import OrchestratorServer, dispatch, make_server from .server import OrchestratorServer, dispatch, make_server
__all__ = [
"BottleRecord", # Facade name -> submodule that defines it. Lazy so importing a leaf (or the
"RegistryStore", # parent package, e.g. via `orchestrator.store.store_manager`) doesn't drag the
"new_identity_token", # whole orchestrator — server, docker_broker + the docker backend, http — while
"BrokerAuthError", # `from bot_bottle.orchestrator import RegistryStore` keeps working.
"LaunchBroker", _LAZY: dict[str, str] = {
"LaunchRequest", "BottleRecord": ".store.registry_store",
"StubBroker", "RegistryStore": ".store.registry_store",
"DockerBroker", "new_identity_token": ".store.registry_store",
"DockerBrokerError", "BrokerAuthError": ".broker",
"Gateway", "LaunchBroker": ".broker",
"GatewayError", "LaunchRequest": ".broker",
"sign_request", "StubBroker": ".broker",
"verify_request", "sign_request": ".broker",
"Orchestrator", "verify_request": ".broker",
"OrchestratorServer", "DockerBroker": ".docker_broker",
"dispatch", "DockerBrokerError": ".docker_broker",
"make_server", "Gateway": "..gateway",
] "GatewayError": "..gateway",
"Orchestrator": ".service",
"OrchestratorServer": ".server",
"dispatch": ".server",
"make_server": ".server",
}
def __getattr__(name: str) -> Any:
src = _LAZY.get(name)
if src is None:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
from importlib import import_module
value = getattr(import_module(src, __name__), name)
globals()[name] = value
return value
__all__ = list(_LAZY)