mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Explain resolution (P1-04)

explain_resolutionregistry, T answers "why is T resolved this

Source: mjdi/docs/explain-resolution.md

explain_resolution[registry, T]() answers "why is T resolved this way?" with a human-readable explanation derived from the same provider and lifetime metadata the graph export uses — the same comptime chain lookups resolution dispatches through — so the explanation describes actual behavior, not inferred behavior.

from mjdi import Registry, explain_resolution
from mjdi.provider import provider_scope

comptime reg = (
    Registry()
    .register_value[AppConfig]()
    .register[UserRepository, provider_scope.singleton]()
    .register[Session, provider_scope.scoped]()
)

print(explain_resolution[type_of(reg), Session]())

Output (the complete shape; every section is present for every registered service):

Service: Session
Provider: class (ClassProvider)
Registration: register[T]
Effective lifetime: scoped
Dependencies:
  UserRepository — class, singleton, provided
Construction path:
  Injectable.create: default-construct -> resolve Inject fields in declaration order -> post_construct (when declared) -> return or cache
Cache owner: the owning Scope
Scope required: yes — resolving directly from the container is rejected at runtime with the scoped diagnostic; obtain one with container.create_scope()
Copyable requirement: yes — consumers receive copies of the cached canonical instance

What every explanation includes

  • Requested service — the service you asked about.
  • Selected provider — the descriptor kind and struct name (ClassProvider, ValueProvider, FactoryProvider, AliasProvider, or LinearProvider for linear services).
  • Registration — the registration method that produced the node (including the override family — an explanation of an overridden service reports override[T] and the overridden lifetime).
  • Effective lifetime — what a resolution actually observes (aliases inherit their target's lifetime through the chain).
  • Dependency chain — every Inject dependency with its provider kind and lifetime, and whether it is provided (MISSING PROVIDER flags invalid graphs; the explanation never validates, so you can inspect a broken registry).
  • Construction path — how the instance is built per provider kind: the Injectable.create wiring sequence, the value slot, the factory execution policy, the alias construction path, or the linear in-place construction.
  • Cache ownership — which context owns the canonical instance (the resolving context for singletons, with the scope caveat; the owning Scope for scoped services; none for transient/value/linear).
  • Scope requirement — whether a Scope is required, and why.
  • Ownership constraints — the copyability requirement (cached lifetimes hand out copies), or the linear rules for linear services.
  • Validation results — the node's graph problems (missing dependencies, cycles, lifetime conflicts), rendered as Validation problems: when present.

T must be registered in the registry; otherwise compilation fails with the explain-specific diagnostic (compile-fail fixture cf_explain_unregistered).

Example: a factory product

Service: Widget
Provider: factory (FactoryProvider)
Registration: register_factory[T, factory]
Effective lifetime: singleton
Dependencies: (none)
Construction path:
  the registered factory function runs once per resolution (transient) or once per owning context (singleton/scoped)
Cache owner: the resolving context — the Container, or the Scope it was resolved through (a singleton resolved inside a scope caches in that scope)
Scope required: no
Copyable requirement: yes — consumers receive copies of the cached canonical instance