Skip to content

Use msgspec models for the server-side wire protocol - #8

Draft
messa wants to merge 1 commit into
202606_messa_srcfrom
202606_refactor_data_handling
Draft

Use msgspec models for the server-side wire protocol#8
messa wants to merge 1 commit into
202606_messa_srcfrom
202606_refactor_data_handling

Conversation

@messa

@messa messa commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Decode and encode the agent↔server wire-protocol JSON with typed msgspec models on the server side, instead of working with untyped dicts and hand-rolled validation.

Stacked on #7. The base of this PR is 202606_messa_src, so the diff shows only the changes specific to this PR. Merge #7 first, then this can be retargeted to main.

Why server-only

The agent is designed to run on Debian 11/12 without a venv — on the system Python with apt packages only — where python3-msgspec does not exist (it only landed in Debian 13). So the agent stays stdlib-only and keeps using json. The wire format remains plain JSON, so the json-encoding agent and the msgspec-decoding server interoperate unchanged; only the server (which runs in Docker with a uv venv) gains the msgspec dependency.

Changes

  • New protocol.py with msgspec models: Header, Prefix, Auth, DataMeta, OkLengthReply.
  • recv_command now returns the raw metadata bytes; handle_client decodes them into typed models via a new decode_message() helper that turns msgspec validation/decode errors into ProtocolError (replacing the manual field checks).
  • send_reply encodes via msgspec; check_client_auth works on the Auth model.
  • Added msgspec to the server dependencies and re-locked (server/uv.lock, and e2e_tests/uv.lock since e2e installs the server).

Side effect: client token no longer logged

The server previously logged parsed metadata at debug level (Received %s %r), which included the client token from the header. recv_command now logs only the metadata byte length.

Testing

make check is green for all three components: agent (2, unchanged/stdlib), server (12), e2e (7 — including the real TLS round-trip, which exercises the json-agent ↔ msgspec-server path end to end).

Note: msgspec is a compiled package; this was verified locally on Python 3.13. Whether a wheel exists for Python 3.14 (the top of the CI matrix) will be confirmed by CI. The agent is unaffected (stdlib only).

🤖 Generated with Claude Code

Introduce msgspec models (protocol.py) for the JSON metadata exchanged
with the agent and decode/encode them on the server instead of working
with untyped dicts. recv_command now returns the raw metadata bytes,
which handle_client decodes into a typed Header / DataMeta through a new
decode_message() helper; msgspec validation errors are turned into
ProtocolError, replacing the manual field checks.

The wire format stays plain JSON, so the stdlib-only agent — which must
keep running on Debian 11/12 without a venv, where python3-msgspec is
unavailable — interoperates unchanged. Only the server gains the msgspec
dependency.

As a side effect the server no longer logs the raw metadata (which
included the client token from the header): recv_command now logs only
the metadata byte length.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@messa

messa commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

Musim si to jeste promyslet, jestli je toto cesta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant