I Made JSON the Engine's Native Type, and Every Call Paid for It
This is a postmortem of a mistake I made in the design of an app’s execution engine. It wasn’t a typo or a missed edge case. I wrote one line into a design document, and that line turned an unneeded cost into a requirement every other part of the system had to follow.
In short, the engine copied every operation call’s arguments into JSON before the permission check ran. It also serialized every call’s typed result into JSON after the call returned. It did this on every call, whether or not anything read either copy. Most of the time, nothing did.
What I wrote
The engine runs operations (“calls”) on behalf of programs and tools. Each call goes through one entry point. That entry point runs a budget check and a permission gate, then the typed body of the operation, and then a completion step that lets post-call work fire.
When I wrote the reference page for the shared contracts crate, I defined a call’s arguments like this:
pub type DecodedArguments = serde_json::Value;
I gave one sentence of rationale: every tool call arrives as JSON and every argument lowers to it.
That page was the authoritative reference, and implementers build what it states. So they did:
- The gate trait took
&serde_json::Valueas the call’s arguments. - The completion trait took
&Result<serde_json::Value, Failure>. - The engine’s entry point required every result type to be
Serialize, so that it could serialize the result and pass it to the completion. - Every module received typed arguments from the guest binding, put them in a typed struct, and then built a JSON object with the same fields by hand to pass to the engine:
let arguments = WriteFileArguments { path, content };
let decoded = object([
(PATH_ARGUMENT, Value::from(arguments.path.as_str())),
(CONTENT_ARGUMENT, Value::from(arguments.content.as_str())),
]);
self.host.call(&decl, decoded, async move |fs, cx| fs.write_file(cx, arguments).await).await?;
Across the modules, that came to about two hundred hand-written JSON construction sites. Each one mirrored a typed struct and had to be kept in sync with it by hand.
What it cost
- A full second copy of every call’s arguments. On a file write, the whole file content was copied into a JSON string. On a plain call with no hooks and nothing suspended, the gate read none of it.
- A JSON tree of every result, built and then thrown away. The only implementation of the completion checked
is_ok()and dropped the value. A file read serialized every line just so that check could ignore it. - A new way to fail. Because results had to serialize, there was a new engine error for a call whose body succeeded but whose result couldn’t be serialized. That error could only come from the serialization step itself.
- Boilerplate that keeps growing. Every new operation meant another hand-built JSON mirror.
- A fix that needs a design change, not a patch. The implementation matched the reference exactly, so correcting it means changing the contract and then releasing every module again.
The operations themselves were never wrong. They worked on native types the whole way through. The problem was the eager, unconditional conversion wrapped around them.
Where my reasoning went wrong
I made a whole system follow a fact about one path. A tool call from a model really does arrive as JSON, so that path is JSON at its entry. A hook process really does read JSON on stdin. Neither fact says anything about a typed call from a program to the host. Both sides of that call already share Rust types. “Every X arrives as JSON” describes one path. I used it as the reason to convert every other path to JSON.
I named a third-party type where the requirements named a concept. The requirements said the gate “must receive the call’s decoded arguments” and that the dispatch “must be completed with the outcome.” Neither says JSON. Writing “decoded arguments” as serde_json::Value on an authoritative page made an implementation detail a requirement. I should have asked what outside the implementation depends on that exact type. The answer was nothing.
I dropped the rules of the design I was adapting. This engine was adapted from an earlier harness, and that harness is explicit on this point:
- Its rule for the program seam says that neither an argument nor a result is a JSON document.
- Its guest wire uses a small tagged binary encoding. The docs for that encoding start with a section explaining why it isn’t JSON: JSON costs the guest a parser, and JSON has only one number type.
- Its typed call surface is documented as taking no
serde_json::Value, so a typed call reaches the tools without a round trip through JSON. - Its gate takes the operation id and nothing else.
The one place it does build JSON arguments is for a session record, which has a real reader. Its own comments call that a leftover. I kept the leftover, made it the gate’s contract type, added result serialization on top, and didn’t carry over any of the rules against JSON. I never wrote down why I was dropping them, because I never actually decided to.
Nobody followed the value to its reader. I didn’t, the implementers didn’t, and the reviewers didn’t. Reading the single implementation of the completion would have shown in a minute that it ignores the value it’s given. The design stood for weeks before someone asked the question directly, and it took about two minutes to confirm.
What I should have written
Arguments as a capability, not an encoding. The reference should have said what the gate, the summary renderer, the hook subject and the authorization request need to do with arguments: read a named field, render a line, spell out the call. Then it should have defined a type that offers those operations over the module’s typed struct. Serialization belongs to the one reader that needs a serialized form, at the moment it needs it.
A gate that pays nothing by default. The gate decides from the operation and, only when a rule needs them, the arguments. It should get the operation plus a typed or lazily built view of the arguments. A plain call with no rule that looks at arguments should pay nothing.
A completion that gets the outcome, not a serialized copy. The completion needs to know whether the call succeeded so post-call work can fire. If a post-hook ever needs the value, it can take a reference to the typed result and serialize it itself. Without that, there’s no Serialize bound on results and no serialization error variant.
One derivation, not two hundred mirrors. Each module declares its typed argument struct once. Any view the gate or renderers need comes from that struct through one mechanism, such as a derive or a trait implementation, and is never written again by hand at each call site.
What I’m changing in how I work
- Name the reader before writing the serialization. Serialization is a cost paid for a reader. If I can’t name the code that reads the serialized form and say what it needs from it, the serialization doesn’t get written.
- Values cross a boundary in the type both sides share. I serialize only where the other side is serialized by nature: a model’s tool call, a process’s stdin, a file on disk, a wire to another machine. Even then I choose the encoding for that boundary based on its cost. JSON is not the default.
- Authoritative pages don’t fix the encoding of in-process values. If a design page says “X is
some_crate::Type”, I’ll ask what outside the implementation depends on that exact type. If the answer is nothing, it doesn’t go on the page. A type alias to a third-party type is the same kind of mistake as listing dependencies in a spec, just written differently. - When I adapt a design, I account for every rule I drop. If the source says “not JSON here” and my version doesn’t, I need a written sentence saying why. Silently dropping a rule is a decision too.
- Follow one value end to end in review. For any trait method that takes a generic or serialized value, I’ll open every implementation and check what it does with that value. A callback that ignores what it’s given is a finding.
Warning signs I’ll now flag in review
- A type alias to a serialization type (
serde_json::Value,toml::Value, “bytes as JSON”) on a design or reference page. - A rationale shaped like “every X arrives as JSON, so every Y lowers to it.”
- A trait method that takes or returns
serde_json::Valuewhen its callers already hold typed values, especially on a gate, a hook, a completion or a recorder. - An
R: Serializebound on a function that never writes, sends or storesR. - A
json!or object builder in the same function as a typed struct with the same fields. - A callback, completion or sink whose only implementation ignores its argument.
- A new error variant for serialization failing inside a call that otherwise succeeded.
- A design adapted from somewhere else that quietly drops a “not JSON” rule.
Reaching for JSON by default feels harmless because JSON is easy to inspect and easy to produce. But every conversion copies data and adds code that someone has to maintain. When nothing reads the result, it’s pure waste, and putting it in an authoritative document made sure everyone after me built it too. Next time, the first question I ask is who reads it.