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 lifetime | Resolved from | Instance behavior | Cached in | Released by |
|---|---|---|---|---|
| transient | container | fresh instance per resolve | nowhere (caller owns it) | ordinary Mojo deinitialization |
| transient | scope | fresh instance per resolve | nowhere (caller owns it) | ordinary Mojo deinitialization |
| singleton | container | one canonical instance per container | the container's cache slot | Container.close() (with pre_destruct) |
| singleton | scope | one canonical instance per scope (per-context singleton) | that scope's cache slot | Scope.close_scope() (with pre_destruct) |
| scoped | container | rejected — raisable MJDI-R001 | — | — |
| scoped | scope | one canonical instance per scope, shared inside it | that scope's cache slot | Scope.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_destructfor 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 survivesclose()on its container.Scope.close_scope()releases only that scope's slots, newest-registered first (seedocs/close-semantics.mdfor 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.