Skip to content

0174: Built-in module implementations load on first use

  • Implementation built

Context and Problem Statement

Every hz call boots the kernel, and the kernel discovers every built-in module by importing its package (ADR 0004). A package's __init__.py is its declaration (Module(...), settings, services, operations, jobs, lets), but it imports the implementation files at module level too, so hz --version loaded 286 of the 344 built-in files. On hosted Linux runners each call took 2 to 3 s, and the README procedure (about 15 calls) overran its 100 s budget. The command tree, help, OpenAPI and MCP schemas need the declarations; how do we stop loading the implementations without changing any of them?

Decision Drivers

  • Help, --command-tree, REST and web OpenAPI, MCP tool schemas and command output stay byte-identical.
  • Historical import paths (from heinzel.modules.org import install_reminders), class identity, pickling and every monkeypatch seam keep working; the layering (spec, services, kernel) is untouched.
  • A declaration that grows an implementation dependency is caught by a test, not by a slow runner.
  • Plugins need no change.

Considered Options

  • PEP 810 lazy imports, declared per file with __lazy_modules__ (chosen)
  • A generated declaration manifest read instead of importing packages: the kernel, the injector and every head read live Let objects (functions, pydantic types, service classes); a manifest would need a second, serialized model of all of them and a parity proof for each head.
  • A split declaration module per package (_declaration.py plus _impl.py): moves hundreds of names, and each moved class needs the cross-version identity facades of a move; the same laziness comes from the import statement.
  • Function-local imports: changes the historical module attributes and their patch seams.
  • Global lazy imports (-X lazy_imports=all): breaks import cycles in the kernel facades and makes import-time side effects of every package lazy.

Decision Outcome

Chosen option: PEP 810 lazy imports declared with __lazy_modules__, because the declaration stays where it is, the names stay where they were, and Python resolves them on first use.

  1. Every file under heinzel.modules lists in a literal, sorted __lazy_modules__ the built-in modules (heinzel.modules.*) it imports at module level but uses only inside functions. Python 3.15 binds those names lazily; a module that the declaration reads at import (a settings model, a service class, a decorator argument, a type in a signature or a class body) stays eager.
  2. The implementation file loads when an operation, job, provider or service factory first touches the name: a call, an attribute read on the package, from package import name, or monkeypatch.setattr (which reads the old value first). After that the name is the same object as before, so identity, pickling and patches behave as they did.
  3. Discovery, the kernel and the heads do not change. Help, OpenAPI and MCP schemas read the same declarations.
  4. Only built-in modules use it; a plugin may add its own __lazy_modules__ (opt-in), nothing else changes for it.
  5. The resident manifest (tests/fixtures/published_resident/record.py) records each binding still pending after boot as a lazy line: like an import inside a function, it loads a file of the new tree after an in-place update, and the cross-version test checks it from the next release on.

Consequences

  • Good: hz --version loads about a third fewer built-in files; the CLI start is faster on every call and head.
  • Good: the declaration files say in one place which implementation modules they defer.
  • Bad: an import error in a deferred file surfaces at the first call, not at boot; a test resolves every lazy binding of every built-in module so that it surfaces in CI.
  • Bad: a module whose import has side effects must not be listed; the test keeps listed modules to those whose names only function bodies use.
  • Bad: service classes, settings models and signature types still load at boot. Making them cheap needs a split of the service classes, which this record leaves open.

Confirmation

tests/unit/kernel/lazy_modules_test.py fails when a module-level built-in import used only by functions is missing from __lazy_modules__, when a listed module is not imported, when a lazy binding does not resolve, and when the CLI start loads a sentinel implementation. Ruff's TID255 fails when a listed module is resolved at import anyway.

More Information

  • PEP 810: explicit lazy imports, Python 3.15.
  • Measured timings and the parity method are in the pull request that built this record.