mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

The provider/lifetime/scope behavior matrix (P0-10)

The complete, verified behavior of every provider lifetime when resolved

Source: mjdi/docs/scope-behavior-matrix.md

The complete, verified behavior of every provider lifetime when resolved through the container and through a Scope — pinned by tests/test_scope_matrix.mojo and consistent with docs/close-semantics.md.

The matrix

Provider lifetimeResolved fromInstance behaviorCached inReleased by
transientcontainerfresh instance per resolvenowhere (caller owns it)ordinary Mojo deinitialization
transientscopefresh instance per resolvenowhere (caller owns it)ordinary Mojo deinitialization
singletoncontainerone canonical instance per containerthe container's cache slotContainer.close() (with pre_destruct)
singletonscopeone canonical instance per scope (per-context singleton)that scope's cache slotScope.close_scope() (with pre_destruct)
scopedcontainerrejected — raisable MJDI-R001——
scopedscopeone canonical instance per scope, shared inside itthat scope's cache slotScope.close_scope() (with pre_destruct)

Key consequences, each verified:

  • Singletons are per-owning-context. A singleton resolved through a scope is cached in the scope, not in the container: two scopes hold two independent canonical instances, and neither is shared with the container's own singleton slot. This is the documented behavior since scopes were introduced; the matrix tests now pin it (construction counters: container 1, each scope 1).
  • Scoped providers cannot leak past their scope. Resolving them from the container is a compile-time-catchable configuration plus a runtime guard (MJDI-R001); within a scope they are shared (one construction for N resolutions).
  • Transients are never cached by either owner — construction counters and insertion counters both pin this.

Cache ownership and cleanup

  • Every cache slot belongs to exactly one owner (the container or one scope). No slot is ever shared, so every canonical instance is released exactly once, by its owner, with pre_destruct for lifecycle services.
  • Container.close() releases only the container's slots; scopes created from it keep theirs (and can even be created and used after the container closed, because value slots survive close). Verified: a scope's cached singleton survives close() on its container.
  • Scope.close_scope() releases only that scope's slots, newest-registered first (see docs/close-semantics.md for the full ordering contract).

Value slots and scopes

  • create_scope() mirrors the container's currently-set value slots into the scope (a copy at creation time).
  • Later container.set_value[T] calls do not affect existing scopes (verified: the scope still resolves the pre-creation value).
  • scope.set_value[T] shadows the mirrored value for that scope only; the shadow never leaks into the container (both directions verified).
  • Value slots survive close()/close_scope() — configuration, not lifecycle state (see lifecycle safety diagnostics for why values are deliberately not tracked).

Proposed breaking changes

None. 0.9.0 documents and tests the existing semantics; no behavior in the matrix changed in this release. If a future release proposes to change any row of this matrix (for example, container-shared singletons across scopes), it must be announced here first, as a migration note, and released behind a new specialization parameter — never silently.