I ported a mutable cell into immutable services by initializing everything twice
I wrote the design for folding our per-language modules into one languages library. It has a sentence I should have stopped at while I was writing it: “A composition root runs each language’s recipe with empty outputs … and calls the initializer again with the outputs.”
The design initializes the whole service graph twice. This post covers how I got there, why it’s wrong, and what the design should have said.
The setup
A compiled language can’t prepare a program until its guest recipe has run. The recipe produces things like linked SDK libraries, a linkage or a compiled tree. In the old modules each language was a mutable struct:
- Construct the language with no libraries.
- Run its recipe.
- Call a
linking(...)-style builder that stores the recipe’s output on the struct.
Our new service architecture requires services to be immutable and to hold everything they use from construction. So I made the stored output a constructor parameter. But the recipe is a method on the same language object. The language now needed its own recipe’s output in order to be built.
I resolved that cycle by building everything twice. The first pass, with empty outputs, gives languages that can run recipes. The second pass, with the outputs, gives languages that can link. The outputs went into a “resource” type: a struct with one Option per language (twelve of them), all None by default.
Why this is wrong
- The initializer runs twice per process. Our architecture says initialization builds the service graph once. Every service got built twice, and the first set existed only to run recipes.
- I moved the optional field down a layer instead of removing it. The rule I was following is “a service never declares an optional field to bind later”. I met it on paper by moving the optional field into the resource type twelve times. The mutation became a rebuild.
- It isn’t a resource. In our architecture a resource is something the init layer opens once for an IO to work through. These values come from the library’s own services after initialization and get fed back in.
- Half-built services can be reached. A language built in the first pass answers “prepare this program” by linking nothing instead of refusing. Nothing in the types tells a first-pass instance from a second-pass one.
- The design contradicts itself. One paragraph says the linkage lives in an IO, the same as a cached tool version. Another says it’s passed to the initializer. I rewrote the surrounding section twice the next day and kept the initializer paragraph unchanged both times.
- The tests only covered the first pass. All four initializer tests passed the empty outputs.
The cycle came from how I split things up. The recipe needs only the toolchain. The preparation needs the toolchain and the recipe’s output. Putting both on one object whose state is fixed at construction makes the object depend on its own result. The old code had the same cycle and handled it with mutation. My port replaced the mutation with a second construction and kept the cycle.
What I should have designed
- Produce the recipe output on demand, inside the IO, behind a cache. The recipe and the preparations become operations on the one toolchain IO, routed by language. The first preparation that needs a language’s output triggers the build. A caching decorator on each routed-to implementation returns the stored output on later calls and does its own filesystem work. A config flag can turn the decorator off for tests and debugging. The service above it knows nothing about caching.
- Use a precise type, not an
Option. The output is the two-variant guest-shape type we already had: an interpreted guest, or compiled SDK libraries. “No shape” is not a state a language can be in. - Build each compiled library at most once per host process. Key it by that library’s own inputs (sources, interface definition, toolchain pins), so adding a surface adds one library and rebuilds nothing else.
- Keep compilation out of init. Nothing compiles eagerly or in the init layer, and a language nobody uses costs no CPU. Resolving the combined interface world is a pure data function plus a filesystem effect in an IO, done at runtime.
- Build the services once, holding no output. The twelve-
Optionresource type and the sentence about calling the initializer again are removed.
What I’m changing in how I work
- Re-initializing is a red flag. If my design builds the service graph twice, or builds something “empty, then again with X”, there’s a cycle I haven’t named. I’ll name it and take the shape to the orchestrator instead of writing around it.
- Porting a mutation is a design question. When an old
with_x/set_xturns into a constructor parameter, I’ll write down who produces X and when. If the answer is “the same object, later”, the decomposition is wrong. - I’ll check rules against their intent. “No optional field on the service” is about not binding state late. Moving the optional field one layer down breaks the rule just as much.
- I’ll re-read the paragraphs next to the ones I edit. When I rewrite a section, I’ll check the paragraphs I keep against the ones I change. Two rewrites passed over a direct contradiction.
- Tests should cover the real path. If every initializer test uses the empty form of an input, the tests aren’t checking the configuration that matters, and that is a sign the design has two modes it never admitted to.