Carry errors across the wire as JSON, not as Elixir terms - #2
Merged
Conversation
A failed tool call returned this to the client:
"structuredContent": {"error":
"%{reason: \"invalid arguments: missing required property: a\", tool: :t}"}
That is inspect/1 output on a map: map-literal syntax, a bare atom with its
leading colon, Elixir-escaped inner quotes. The useful parts -- which tool,
which property, why -- were all present and recoverable only by parsing Elixir.
It was wrong at every protocol revision, which is why it was not folded into the
negotiation slice.
Red first:
5 tests, 3 failures
a validation failure carries structured fields, not an inspected map
the human-readable content carries no Elixir syntax either
a host's error term is carried as JSON, not as an inspected term
Green: 5 tests 0 failures, suite 33 0, gate exit 0.
was "error": "%{reason: \"invalid arguments: ...\", tool: :t}"
now "error": {"tool": "widget",
"reason": "invalid arguments: missing required property: a"}
"text": "widget: invalid arguments: missing required property: a"
structuredContent carries the error as data so a client can read a field;
content carries a sentence a person can read. format_reason/1 and its inspect/1
fallback are gone, and no inspect/1 remains in server.ex.
A host's error term is carried as JSON too, so a dispatch returning
{:error, %{code: "upstream_timeout", retry_after: 30}} reaches the client as an
object with those fields rather than as a stringified term. A plain-string error
survives unchanged.
One existing assertion used =~ against what was a string and is now an object;
it now reads the reason field. That is a consequence of the shape change.
The tests assert the absence of Elixir syntax by pattern -- map literals and
bare atoms -- rather than by comparing to a fixed string, so a future regression
of a different shape is still caught.
Signed-off-by: Ayla Croft <aylacroft@proton.me>
HackTuah
force-pushed
the
slice/002-wire-error-payloads
branch
from
September 6, 2026 20:47
021645a to
231677f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One commit, red first. Independent of PR #1 — branched from
main, touches no file that PR touches.The defect
A failed tool call returned this to the client:
inspect/1output on an Elixir map — map-literal syntax, a bare atom with its leading colon, Elixir-escaped inner quotes. The useful parts (which tool, which property, why) were all present and recoverable only by parsing Elixir, by a client with no reason to know what language the server is written in.It was wrong at every protocol revision, which is why it was kept out of the negotiation slice rather than folded into it.
Red first
Green: 5/5, suite 33/33, gate exit 0.
What changed
structuredContentcarries the error as data, so a client can read a field.contentcarries a sentence a person can read.format_reason/1and itsinspect/1fallback are gone, and noinspect/1remains inserver.ex.A host's error term is carried as JSON too: a dispatch returning
{:error, %{code: "upstream_timeout", retry_after: 30}}reaches the client as an object with those fields. A plain-string error survives unchanged.One thing worth noticing in the tests
They assert the absence of Elixir syntax by pattern — map literals and bare atoms — rather than comparing against a fixed expected string. A regression of a different shape is still caught, where a string comparison would only catch this exact one.
One existing assertion used
=~against what was a string and is now an object; it now reads thereasonfield. That is a consequence of the shape change, not an accommodation of it.