#!/usr/bin/env python3 """Compatibility shim -- the implementation lives in lib_py.delegation.registry. This file's path is a wire contract (agent instructions, the bash wrapper, SKILL.md, BOOTSTRAP.md, deploy/install.sh, tests). It binds the ``lib_py.delegation`` *package* to the tree next to this file, once per process, without touching ``sys.path``; ``import registry`` then yields the very same module object as ``lib_py.delegation.registry``. Why the package and not this one module: a per-module bind leaves a non-leaf module (one that imports its siblings) importing dependencies from whichever tree happens to be on the path, mixing a frozen copy with a live one. Why bind-once and never rebind: replacing a bound package would split existing references and break monkeypatching. See docs/contracts/issue-5/ (D2) for the context matrix. """ import importlib, importlib.util, os, sys _SKILLS = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) _PKG = "lib_py.delegation" if _PKG not in sys.modules: # bind once per process; never rebind try: import lib_py # parent resolved normally; shims never rebind it except ModuleNotFoundError: sys.path.append(_SKILLS) # standalone only: no lib_py exists anywhere to shadow import lib_py _d = os.path.join(_SKILLS, "lib_py", "delegation") _spec = importlib.util.spec_from_file_location( _PKG, os.path.join(_d, "__init__.py"), submodule_search_locations=[_d]) _pkg = importlib.util.module_from_spec(_spec) sys.modules[_PKG] = _pkg # register BEFORE exec so __init__'s own imports resolve here _spec.loader.exec_module(_pkg) lib_py.delegation = _pkg _impl = importlib.import_module(_PKG + ".registry") # resolved through the bound package's __path__ if __name__ != "__main__": sys.modules[__name__] = _impl # `import registry` is lib_py.delegation.registry else: _main = getattr(_impl, "main", None) # a module without a CLI keeps exit status 0 sys.exit(_main() if _main is not None else 0)