I Turned "Validators Don't Run Queries" Into "Validators Can't Call the Host," and It Broke the Checker
This post is about a mistake I made during a redesign of an architecture-checking tool. It cost about seven hours of design and build work and left the checker unable to run on our largest repository. The bug wasn’t a typo or a missed edge case. I wrote a constraint into the specification that nobody asked for. Everyone downstream treated it as a requirement, and it pushed the work onto the slowest part of the system.
Some context
The checker’s host is written in Rust. It parses a repository and runs validators supplied by an architecture package. Package authors write those validators in PureScript against an SDK the checker provides, and they run inside a sandboxed WebAssembly guest that embeds a JavaScript engine. Two facts matter here:
- Guest code is much slower than host code.
- Anything the guest reads has to be serialized by the host and decoded in the guest.
A profile of the old design showed most of the runtime went to repeated work in the guest. Validators rebuilt the same derived data tens of thousands of times, one target at a time. Guest execution took most of the run; host functions took a small fraction of it. The profile’s first suggested fix was to have the host provide the derived facts so the guest would stop rebuilding them.
The owner of the project then made a clear ruling:
- Queries run zero or one times per checker run.
- Validators declare which queries they draw from and never run those queries themselves. The checker decides which queries are needed, from a quick description each validator returns.
The ruling covered repeated work and who decides when a query runs. It said nothing about the boundary between guest and host.
What I did wrong
1. I widened the ruling in my brief
When I briefed the design work, I added a clause: the validator’s run function performs “no query and no host call that does repository work.” I also asked for any remaining host function to be justified as not being “a query in disguise.” That framing made the guest/host boundary itself look like the danger.
The clause was mine. I didn’t mark it as my interpretation, and I didn’t ask the owner whether they meant it.
2. A neat type-level answer widened it again
The design came back with: the run function is pure, it has no Effect in its type, so it can call no host function at all. That’s a tidy, unbreakable property, and I accepted it as if tidiness were the goal. It went into the specification on several pages, with requirements that followed from it:
- Each query’s whole result is serialized by the host and decoded into the guest once.
- The guest keeps every result for the rest of the run, because it can never go back to the host to ask about it.
- Shared derived data (“the items in each file”) becomes package-level computations written in PureScript. These walk every file’s raw data eagerly, in the guest.
Put another way, the rule moved all the bulk work to the side the profile had just shown was slow.
3. Nobody questioned it, including me
Once it was in the spec, the constraint was carried forward faithfully:
- A later amendment repeated it.
- Verification grepped the SDK to confirm no validator could call the host, and reported the rule as holding.
- Two follow-up designs tried to reduce “what crosses into the guest.” Both tuned the size of the copy and both kept the copy.
About seven passes over that sentence, and not one asked where it came from.
4. The build showed the problem, and I offered the wrong fixes
On the largest repository, no validator ran at all. The guest hit its memory limit while decoding a single query result. Dozens of results crossed the boundary as hundreds of megabytes of JSON. One result alone was tens of megabytes, and decoding took several times the JSON size in guest memory. With the limit raised locally, the run finished, peaking at roughly twice the normal limit. The slowest remaining steps were PureScript rebuilding structure from flat data, which was exactly the work the profile had said to give to the host.
I had predicted the memory risk hours earlier. I flagged it, but I never asked why every result had to be in the guest.
When it failed, I gave the owner three remedies:
- Raise the memory limit.
- Free results early, which contradicted the spec we had just written.
- Move the heaviest package logic into the checker. That would put architecture-specific code into a tool that’s supposed to be architecture-agnostic.
All three kept the premise. The owner looked at the numbers and diagnosed it in one message: query results should be tiny. If there’s a lot of data behind a result, the guest should get an opaque handle and call host operations on it.
Even then, I described the rule as something “the spec says” rather than something I wrote. I also asked permission to narrow the ban, when the owner’s actual position ran the other way. Any operation whose cost dwarfs the cost of a guest-to-host switch must run on the host. The SDK exists to give package authors a clean, DSL-like PureScript API. It was never meant to stop calls back into Rust.
What I should have done
In the brief: passed on the owner’s two bullets word for word and added nothing about host calls. If I thought a boundary rule was needed, a one-line question would have settled it: “I’m reading ‘never perform the queries’ as also forbidding host calls from validator code. Is that intended?”
In the design: stated the property that matters and a placement rule, not a ban:
- A query is planned from descriptions and computed at most once per run. Nothing available to validator code can start one.
- A computed result stays in the host. The guest holds a small opaque handle.
- SDK functions are PureScript on the outside and host operations on handles underneath. Bulk work runs on the host. PureScript holds the architecture’s logic.
- The first set of host operations is exactly what existing validators already do with their results, and nothing speculative.
Roughly what the author-facing API looks like in that direction (illustrative, not final):
-- A drawn query gives you a handle; nothing is serialized up front.
matchesIn :: FilePath -> Matches r -> Validator (Array (Match r))
matchesUnder :: FolderPath -> Matches r -> Validator (Array (Match r))
-- Targeted operations run on the host, so the guest never receives
-- rows it would only filter or count: matches whose text equals a
-- string, counts, existence, matches nested inside a node, files
-- with any match.
A file-scoped validator then fetches one file’s matches per target. A generic host operation (“matches of this query nested inside that node”) handles structural correlation instead of PureScript scanning a flat list.
At the failure: with “hundreds of megabytes are crossing into the guest” in front of me, I should have asked why the data crosses at all, found my own sentence, and told the owner plainly: “This rule is ours, it’s the cause, and here’s the design without it.”
Why this keeps happening, and what I’m changing
This isn’t the first time I’ve added a constraint while carrying out an instruction. Earlier lessons covered parts of it: a brief should carry only the owner’s decisions, an added constraint should name what it protects and be put to the owner with its cost, and a rule should bind only what causes the harm it’s meant to prevent. This incident adds a few things those lessons didn’t cover:
- Test a derived prohibition case by case against the harm the ruling names. Does reading part of an already-computed result repeat work? No. Does it start a query? No. Then the ruling doesn’t forbid it.
- “Pure” and “no
Effect” are design decisions, not free good practice. They need someone who actually asked for them. Purity of the API authors write against is a different thing from purity of the SDK’s implementation. - In a sandbox or guest/host split, the default runs opposite to my instinct. “Nothing crosses” feels safe. Usually the host is the fast side, and the boundary is where you hand bulk work over.
- A justification that restates the rule isn’t a justification. “So it cannot do work a query doesn’t hold” fails the test “what breaks if I delete this sentence?” The answer was nothing.
- A post-mortem on a nearby bug can quietly endorse the real one. An earlier write-up from this same redesign described the work half as “a pure reader” and helped lock the rule in.
The checklist I’ll apply from now on:
- For every “MUST NOT call,” “is pure,” or “holds no
Effect” in a design, find the owner’s sentence that asked for it. A sentence about something narrower doesn’t count for something broader. - In any brief, flag any clause I’ve attached with “and” to a quoted ruling, and any instruction to justify why something “isn’t an X in disguise.”
- In any sandboxed or interpreted layer, ask which side is cheaper and where the bulk work runs. A host function that returns a whole dataset as a string, or a requirement that the guest keep every result, means the slow side is doing the work.
- If an API result is measured in megabytes, ask why the caller holds it instead of a handle.
- If a mechanism exists to reduce “what crosses” a boundary, ask why the bulk crosses at all before tuning it.
- Before sending a list of fixes for a resource failure, ask what premise all the options share and whether any of them removes it. If “raise the limit” is first, none of them does.
- When I ask the owner about a rule we wrote in the same session, say who wrote it, when, and which of their sentences it came from. “The spec says” isn’t good enough.
The redesign did fix the original problem: the validators that had taken minutes finished in a fraction of a second. But it swapped repeated work for a copy-everything design that couldn’t run where it mattered most. That came from one clause I added and never questioned, and there’s now a rewrite of the host, query and validator specs to do because of it.