dhis3/go
Field NotesTrackerPhase 1

The Thousand-Mile Form

A nurse in a small clinic in Panama opens her tablet one morning to find four of the week's records marked with the little grey sync icon that means the office in the capital has not yet accepted this. She syncs them one by one; the icon stays grey. She reads the error, which says API call failed, errorcode E1005, and she goes to find somebody who knows what that is. The somebody is the NGO's lone DHIS2 administrator, who posts a careful report on the community forum describing exactly what she tried, and discovers, after some patient back and forth with the development team in Oslo, that she has found a bug. The bug is that a rule in her tracker form hides a field when the patient is an adult, and the server does not know how to accept the hiding, and so the record cannot land.

She is not alone this fortnight. She is one of four.

Four small bugs, same shape

Scan the Discourse feed since mid-September and the pattern is quietly loud. A clinician in Panama cannot sync a record because a HIDEFIELD program rule blanks a tracked-entity attribute that the server still expects populated. A field worker enters -.62 for a paediatric z-score and the Android Capture app accepts it offline, then the server rejects it on sync because the decimal has no leading zero. A programme implementer finds that an expression using d2:validatePattern stops firing after an app update, because the regex engine quietly changed how it reads backslashes. An NGO using versions 3.3.1 through 3.4.2 of the Capture app sees HIDE and MANDATORY rules fire inconsistently on the same boolean data element.

Four reports in three weeks. Four different programme managers. Four different countries, as far as one can tell from the usernames. Four slightly different symptoms. The response from the Oslo team is to its credit genuinely fast and genuinely useful, with Jira tickets filed, docs updated, patches noted, and more than one developer apologising for the inconvenience. That is what good upstream looks like, and the community should remember to say so from time to time. Even so, four bugs of this precise shape in three weeks is not an accident. It is a condition.

The condition is this. A tracker form in DHIS2 carries, besides its fields, a small body of rules that decide what the form does when a user fills it in: show this field, hide that one, make this one mandatory, flag this value as invalid, calculate the next from the last three. The rules are written once by the implementer, in an expression language that reads a little like a spreadsheet formula. They are then executed, when the clinician opens the form, by one of three engines. The web Capture app has one. The Android Capture app has another. The server has a third, which runs when the record is sent in and the server has to decide whether to let it in.

Three engines. One rulebook. They are supposed to agree.

When they disagree

An 1880s patent-style schematic. A tablet on a wooden clinic desk at the left is connected by a heavy cable, through two mechanical rule-stamping engines on wheeled carts, to a tall administrative cabinet at the right. Each stamp engine lowers an ornate verdict stamp onto the same paper form travelling along the cable. The first engine stamps ACCEPTED. The second stamps REJECTED. Pipes and brass valves sit around each machine; aged parchment background, black ink stippling, 19th-century serif typography.

The shape of each failure is the same shape. The user does something the form in front of them allows. The form hands the something to the engine nearest to hand (the tablet's) which accepts it. The engine in the capital (the server's) refuses it. The sync queue goes grey. The record does not land. Nobody is told the record did not land, except for a four-character error code and a grey icon. The clinician gets on with her morning, because clinic mornings do not pause for sync queues, and the records back up, and the data-use meeting next month shows seventeen patients where there were in truth twenty-one, and somebody who has never met the nurse in Panama writes a sentence in a report about declining case ascertainment.

I draw the fork below. One input at the top; two columns, each running the same three stages (parse, evaluate, apply); three orange disks marking the stages at which the two columns are known to disagree; and, at the bottom, the user-visible outcome of the disagreement. The four known divergences are listed under the diagram. The point of disagreement is not always the same point, which is part of what makes this condition tiring to live with.

Flowchart titled "One rule, two engines, four known divergences". A single program-rule input (showing example values like minus-point-six-two and a d2:validatePattern expression) forks into two parallel columns. The left column, headed OFFLINE ON THE TABLET, is the Android Capture rule engine; the right column, headed ONLINE AT THE SERVER, is the DHIS2 server rule engine. Each column runs three stages: parse, evaluate, apply. Three accent-coloured discs sit between the columns at the three stages, marking the divergence points. The left column terminates in "Record saved locally"; the right column terminates in an accent-bordered "Sync rejected, E1005". A numbered legend below lists the four known forum-documented divergences: single versus double backslash in regex, numeric value coercion of the leading-zero decimal, HIDEFIELD on tracked-entity attributes, and HIDE plus MANDATORY on booleans across app versions 3.3.1 through 3.4.2.

A reader who has ever written a validation rule for a web form and a mobile client of the same form will recognise the condition instantly, because every project of any size that has ever shipped a mobile client of a web application has run into it. The web team writes the rule in JavaScript against the DOM. The mobile team writes the rule in Kotlin against the data model. The server team writes the rule in whatever the server speaks, and they are of course all the same rule, until the day a release ships one engine before the others, and now the rules disagree, and the only person who finds out is the user whose record will not save.

Why this is hard, honestly

Fixing it in principle is easy. Write the rule once, in one language, with one semantics, and execute the same artefact in all three places. Fixing it in practice is a long, patient, unglamorous business, which is why nobody has yet entirely done it. Rules get written by implementers who think in spreadsheet-formula and are not pleased to be told they must now learn WebAssembly or a sandboxed JavaScript runtime instead. Rules have to execute offline on a tablet with a two-year-old chip, which rules out some portable options the server team would otherwise reach for first. Rules have to execute on a server that may be a decade older than the mobile client that just sent in the record, which pushes the compatibility story in the other direction. And nobody has budget for the rule engine, because the budget was for the form.

A good answer is possible. It would look like this: a single, versioned expression language, defined in a small specification document; one reference implementation, probably in Rust or Go with bindings to the three host languages; one shared test suite of several thousand cases, every one of which runs in all three places on every pull request; and a strict rule that no feature lands in any engine until the other two have landed it, with a tag in the metadata to tell the server what version of the rule language the client last upgraded to. One rulebook, three implementations, one test. The pattern is not novel. It is roughly what the Lua community does for Lua, what the WebAssembly community does for Wasm, what the SQLite team does for SQL. The outcome of doing it well is dullness, which is the correct outcome. Nobody writes a forum post about a rule that evaluated the same on the tablet and the server. Silence is the trophy.

What our project is trying

Our rewrite in Go, which we have been writing about here on this blog, is in no position to solve this problem for DHIS2. It is barely in a position to serve /api/organisationUnits without the Java reference laughing at it. But the problem the four forum posts describe sits squarely under the design pressure that shaped the rewrite in the first place. If a Go gateway is going to stand between the clients and the Java core, it will see every program-rule-validated payload the Android app sends in, and it will have no safe choice but to run the rule engine on the way through, because the whole point of a differential oracle is to not accept anything the reference server would reject. Which means we will build a rule engine too. Which means we will add a fourth engine to the three that already disagree, unless we are patient and principled about it.

Patience here looks like this. We treat the rule language as a specification problem before it is an implementation problem. We write out, in careful English and in a few thousand test cases, exactly what every operator in the expression language is supposed to do, including the edge cases the forum has now helpfully documented. We publish the test suite under a licence that lets the Oslo team adopt it if they wish. We implement it in Go for our side. We stay available if anyone else wants a port into Kotlin or TypeScript. And we never ship a change to any part of the engine without the whole suite going green.

That is a longer project than this rewrite. It is also the kind of project that gets you out of the four-bugs-in-three-weeks condition for good, and it is the kind of project a reader of this blog, who is probably a programme implementer or a ministry adviser, can help with. If you have ever written a program rule that worked in one engine and not another, the forum thread for your bug is a public document and your test case is the raw material of the test suite. Nothing is wasted. The condition is quietly loud; it will be, in time, quietly answered.

The nurse in Panama

The nurse in Panama does not read the Discourse forum, and she does not know what a differential oracle is, and she has no view on the merits of WebAssembly as a sandboxed rule-execution runtime. What she knows is that four of the week's patients did not land, and that she had to sync them one by one, and that the next week she will probably have to do it again. She is the person for whom this is being written. She is the person for whom any of it is being written. The engineering we talk about on this blog has value only to the degree that her Monday is less annoying as a result of it.

Four small bugs this fortnight. A patient quietly submitted ticket, a patient reply from Oslo, a Jira entry, a fix. The sector works, in its way. And somewhere, in a workshop next to the one these posts come from, a specification is beginning to be written, in careful English and in test cases that do not yet exist, for a thing that will not ship this year, or next year, or quite possibly the one after. The gauge is on the wall. The oil is in the can. The work is slow. The work continues.