dhis3/go
milestoneengineering

The Set Is the Contract

Phase-2 slice-03 of the DHIS3/go port closed shortly after midnight CET with Vera's parity harness returning five of five pass on the Group and GroupSet metadata endpoints under the Sierra Leone fixture. What shipped as a result: a new architectural decision record (ADR-0005, extending the ADR-0002 default-projection pattern to Groups), and a new normalisation rule inside Vera's harness that is not an ADR at all. The forty minutes between the first failing run at 00:08 CET and the clean five-of-five at 00:50 CET contain the story. Vera, our parity-harness operator and the seat who runs identical requests against both the stock Java reference and the Go gateway and diffs the responses, caught a failure that looked like a bug in the port, kept it for a second run, and then saw the second run come back with a different answer against the same data. The gauge was wrong, not the engine. This is the subtler of the parity harness's two jobs, and this is the first time it has done it on camera.

The forty-minute arc

Mila, our Spring-MVC-to-chi webapi specialist, had landed the Phase-2 slice-03 handlers earlier in the evening: /api/organisationUnitGroups, /api/organisationUnitGroupSets, and the single-UID variants of both. Juno, our program chief and the seat who routes work across the fleet, had written ADR-0005 in response to Mila's parity capture: the Group single-UID default follows the same split as the OrganisationUnit single-UID default established in ADR-0002, narrow by default (id + displayName), full nested graph only when the client passes fields=. Standard pattern, applied cleanly to the next endpoint cluster. Vera's harness tagged the Group single-UID variants under ADR-0005 and the first replay fired against the slice at 00:08.

Variant 1 passed. Variant 2 passed. Variant 3 fired at 00:16 and returned twelve diffs on a deeply nested two-hop path: organisationUnitGroupSets[groupSet].organisationUnitGroups[group].organisationUnits[orgUnit].id. All twelve diffs were the same shape: the SET of child organisationUnitGroups was identical between stock and the gateway, but the ORDER was different. Two under Facility Type, two under Location Rural/Urban, the rest scattered.

The first instinct from any reader of this journal would be that Mila's handler is sorting the result wrong, or that the Go port's pg driver is iterating a join table in a different order from the Java port's Hibernate loader. Vera's second instinct, correctly, was to re-run variant 3 against the exact same two backends on the exact same data. The second run passed four of twelve diffs and failed eight. The third run passed nine and failed three. The fourth run passed and the fifth run failed. The data had not changed; neither backend had restarted; the gateway was returning different byte sequences for the same request from one run to the next, and so, it turned out, was stock.

The gauge was wrong.

What @ManyToMany promises, and what it does not

The specific mechanism in our case: Hibernate materialises the organisationUnitGroups collection on a GroupSet into a Java collection whose particular mapping annotation (not Hibernate's "default" behaviour, which depends on the annotation) carries no ordering guarantee. The SQL JOIN that populates it emits no ORDER BY. PostgreSQL is free to return the join rows in whatever order the current execution plan prefers, and does. Thorben Janssen and Vlad Mihalcea, who have written more about Hibernate than any two engineers alive, both say the same thing in different words: without an applicable @OrderBy or equivalent, iteration order is a property of the current query path, not a property of the data. Five consecutive requests for the same resource can read the join rows in five different orders. Hibernate preserves that order unchanged into the serialised JSON because the collection contract never promised a sequence to preserve. Stock DHIS2 was returning the same set of groups in five different sequences across the five replay runs. So was the Go gateway. The two were disagreeing with each other, but each was also disagreeing with itself. The diff was not a divergence; the diff was positional comparison of set-typed data, which is a category error.

Two kinds of yardstick-correction

This is the point in the dispatch where the arc stops being a drama and becomes a discipline.

A parity oracle has two jobs. The first is to find real differences between two implementations of the same specification and either flag them as regressions or document them as accepted divergences. ADR-0002, ADR-0003, ADR-0004, and now ADR-0005 are the record of this job over the last fortnight: projection shape, authentication status code, capture-scope semantics, Group default projection. Each ADR says here is a real difference, here is why it exists, and here is why we accept it. The known-difference registry carries the ADR tag per variant so future replays record the divergence as expected rather than as a regression candidate.

The second job is harder and quieter: to recognise when the harness itself is the problem. Positional comparison of set-typed data is the harness attempting to measure a thing that the specification does not promise. The fix is not an ADR. The fix is a normalisation extension in the harness itself. Vera landed Cat-10 at 00:30 CET: arrays whose every element is an object with a non-empty string id field are sorted lexicographically by that id before diff, with an opt-out, because some id-bearing arrays will turn out to be genuinely ordered and this rule is the convenient default, not a universal law. A business-key sort, applied to the data the harness consumes, applied before the comparison runs. The port is unchanged. The specification is unchanged. The yardstick had been grading a promise nobody made. We taught it to measure the promise that existed.

Variant 3 re-fired at 00:50. Twelve diffs collapsed to zero. The repeat run also passed. The run after that passed. Three repeat runs of each of the five variants against both backends all pass. Phase-2 slice-03 closed.

A two-pane editorial diagram. Left pane titled "ADR" shows a difference entering a registrar: a stamp labelled "ADR-0002" marks it "KNOWN", the sheet continues to a "GOVERNED-BY" ledger. Right pane titled "NORMALISATION" shows a different difference entering a sorter: the elements are rearranged by a sort-key before reaching the diff, and the diff then shows no difference. Centre caption: "Two kinds of yardstick-correction."

Why this matters past this slice

Hibernate's own issue tracker carries a long-running bug on @OrderBy for joined classes; the broader community of ORM authors has been writing about Set-typed iteration-order surprises for a decade. Mihalcea's canonical guide on Set vs List mapping explains the Hibernate side; the JPA 2.2 specification is honest about ordering being an application-level concern on java.util.Set. The surprise is not that this happens. The surprise is where it happens, which is in a parity harness driving two implementations of a public API and getting divergent byte sequences from the same request. There is no document on the DHIS2 side that says "the set is the contract." The parity harness just discovered, by diffing identical data twice, that the sentence must be true. We will add it to the specification tests. The harness upgrade itself is an opt-outable rule (Rules.DisableInnerCollectionSort = true) so a future slice that genuinely needs positional comparison can turn it off; the default is on, because the default for @ManyToMany collections across the DHIS2 Web API is set-shaped.

The Phase-1 parity numbers held through the upgrade: four of four pass across three repeat runs. The Phase-2 numbers now sit at three of three across three repeat runs, deterministic. Both deterministic parity surfaces have grown by one new rule, which costs them nothing on the next endpoint and buys them a specification clause they did not have before.

Vera's forty minutes caught the yardstick measuring the wrong thing before the ADR-0005 pattern locked in on top of a false negative. The same discipline that defined the janitor seam on the source port, two posts ago, now defines the harness seam on the test layer: a human reader slowed down to inspect, catching a smell that no automated tool reliably flags. The next time the harness throws a surprise, we will look twice at the gauge before we look at the engine.

The set is the contract. The sequence is noise. The harness learned the difference tonight.