mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Construction ordering contract (P1-18)

mjdi's construction, caching, and teardown behavior is deterministic

Source: mjdi/docs/construction-ordering.md

mjdi's construction, caching, and teardown behavior is deterministic and tested. This page is the contract; tests/test_construction_order.mojo pins every rule below against the actual event stream (the rules are defined in terms of the lifecycle trace, so the tests assert the real behavior, not prose).

Construction order

Resolving a service constructs its dependencies depth-first:

  • Construction entry order is depth-first pre-order — the requested service's construction starts first, then each dependency's as its Inject field is resolved (trace: construct: Top, construct: Middle, construct: Base for Top -> Middle -> Base).
  • Completion order is depth-first post-order — every dependency's construction (including its post_construct) finishes before its consumer's wiring completes (post-construct: Base fires before post-construct: Middle before post-construct: Top). A post_construct hook may therefore safely read its own resolved dependencies (that is what "wiring finished" means).

Cache initialization order

Canonical instances cache innermost-first: with Top -> Middle -> Base all singletons, Base is cached before Middle before Top (each service caches at its own completion). Cache initialization order is the first-resolution order.

Factory execution order

Factories execute at resolution order: the first resolution of a cached factory's service runs the factory once; later resolutions are cache hits. Two cached factories resolved in a fixed order run in that order.

Release order (the teardown contract)

close() / close_scope() release cached canonical instances in reverse registration order — the most recently registered provider releases first:

  • Register dependencies before dependents (the natural composition-root order, and what every example and template does) and release order is dependents first: each dependent's pre_destruct fires while its dependencies' canonical instances are still cached and fully usable.
  • pre_destruct fires immediately before each instance's slot drops, one instance at a time, in that order.
  • Scope cleanup (close_scope) and container cleanup (close) use the same rule over their own caches.
  • Value slots are never released by close (configuration, not lifecycle state — see Scopes and lifetimes).

Why reverse registration rather than a computed dependency order: the registry chain is an immutable linked list whose only intrinsic order is registration order. Reverse registration order is the chain-walk order, is fully deterministic, is testable, and — when you register dependencies before dependents, which the graph validation rules effectively steer you to — coincides with dependents-first teardown.

Closing is non-terminal (and ordering still holds)

close() releases caches; value slots survive; the next resolution re-constructs in the same deterministic order (pinned by the tests: after a close, the full construction sequence replays). This is why there is no "resolution after closure" diagnostic — see Container guard.

The linear contract

Linear services are constructed in place, lent to exactly one visitor, and destroyed immediately after the visitor returns (or on the error path) — their destruction order within a program is therefore simply the order of their consumption. The destruction is exactly once (the compiler rejects a second destroy — see Linear destruction diagnostics).

Where the guarantees come from

  • The chain walk (clear_slots) is a fixed-order recursion over the immutable chain — release order is structural.
  • The scanner populates Inject fields in field declaration order within one construction; the recursive resolution provides the depth-first order across services.
  • post_construct runs inside create after population; pre_destruct runs inside each descriptor's slot release before the drop.