diff --git a/packages/pb-client/CHANGELOG.md b/packages/pb-client/CHANGELOG.md index 2398bf6..d66a6e8 100644 --- a/packages/pb-client/CHANGELOG.md +++ b/packages/pb-client/CHANGELOG.md @@ -5,6 +5,11 @@ Notable changes to `pybricks-client`. The format is based on ## [Unreleased] +### Changed + +- The protocol version this client speaks is declared in pb-client, as + `pb_client.service.PROTOCOL_VERSION`, rather than read from pybricks-core. + ## [1.0.0b2] - 2026-11-23 ### Changed diff --git a/packages/pb-client/src/pb_client/service.py b/packages/pb-client/src/pb_client/service.py index 84d5037..19bfdbc 100644 --- a/packages/pb-client/src/pb_client/service.py +++ b/packages/pb-client/src/pb_client/service.py @@ -17,7 +17,7 @@ import logging from collections.abc import AsyncGenerator, AsyncIterator from contextlib import aclosing, asynccontextmanager -from typing import Any, cast +from typing import Any, Final, cast import anyio from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream @@ -43,13 +43,33 @@ HUB_STATE, HUB_STATUS, HUB_STDOUT, - PROTOCOL_VERSION, STDIO_CAPABILITY, ) logger = logging.getLogger(__name__) -__all__ = ["DeviceInfo", "HubInfo", "Service", "ServiceInfo", "connect"] +PROTOCOL_VERSION: Final = 4 +"""The version of the service protocol this client speaks. + +Sent when connecting, and a service that cannot serve it refuses the +connection, saying which of the two is the older. It is a promise about what +this package does, so it is declared here, and the service declares the +versions it serves; in this repository the two are moved together. + +2 since the chosen hub became something the service remembers, which changed +what ``hub.use`` and ``hub.list`` say. 4 since ``hub.connect`` goes to the chosen +hub and refuses several with ``HUB_AMBIGUOUS``, which a client of 3 does not +know, and says whether the hub was ``already_connected``. +""" + +__all__ = [ + "PROTOCOL_VERSION", + "DeviceInfo", + "HubInfo", + "Service", + "ServiceInfo", + "connect", +] Listener = MemoryObjectSendStream[tuple[str, dict[str, Any]]] diff --git a/packages/pb-core/CHANGELOG.md b/packages/pb-core/CHANGELOG.md index d0f54fd..9646f20 100644 --- a/packages/pb-core/CHANGELOG.md +++ b/packages/pb-core/CHANGELOG.md @@ -5,6 +5,13 @@ Notable changes to `pybricks-core`. The format is based on ## [Unreleased] +### Removed + +- `PROTOCOL_VERSION` and `MIN_PROTOCOL_VERSION`. pb-client and pb-service + declare the protocol version they implement themselves, so a new + pybricks-core can no longer make an older one claim a version it does not + speak. + ## [1.0.0b2] - 2026-11-23 ### Added diff --git a/packages/pb-core/src/pb_core/protocol.py b/packages/pb-core/src/pb_core/protocol.py index 8ab392d..b3c416e 100644 --- a/packages/pb-core/src/pb_core/protocol.py +++ b/packages/pb-core/src/pb_core/protocol.py @@ -1,30 +1,14 @@ # SPDX-License-Identifier: MIT # Copyright (c) 2026 The Pybricks Authors -"""Version of the service protocol itself. - -Distinct from any package version. Because the service outlives the command that -started it, a freshly installed client will sometimes meet a service started by -an older install, so both sides state this in the opening handshake and the -service refuses versions it cannot serve. - -**What moves this number.** An added method, field or named capability does -not: capabilities are negotiated by name, so a client newer than the running -service asks for the one feature it needs rather than failing over a number. A -*changed meaning* for something that already exists does, because there is no -way to ask about one -- both sides go on truthfully claiming the same version -while meaning different things by it, and the failure is silent. That has -happened once already: ``hub.use`` with no hub in the request meant "forget -the chosen hub" and now means "report it", and a service left running from -before the change threw away the answer to the question it was asked. - -**When it moves.** In the commit that changes the protocol, whether or not a -release is anywhere near. A release is not the only way this code reaches -anyone -- it can be installed from git, and then whatever is between releases -is what somebody is running -- so the number has to be right at every commit -rather than at every tag. It is cheap: nothing outside a checkout speaks an -unreleased version, so the only effect of moving it is that a stale service -says so instead of misbehaving. +"""Names shared by both ends of the service protocol. + +Notification methods and capability names, which the client and the service +have to spell the same. **Not the protocol's version**: that is a promise about +behaviour, so it belongs to the code that behaves -- pb-client declares the one +it speaks and pb-service the ones it serves. It used to be here, which let a new +pybricks-core tell an older client or service that it spoke a version it had +never implemented. """ from __future__ import annotations @@ -37,40 +21,9 @@ "HUB_STATE", "HUB_STATUS", "HUB_STDOUT", - "MIN_PROTOCOL_VERSION", - "PROTOCOL_VERSION", "STDIO_CAPABILITY", ] -PROTOCOL_VERSION: Final = 4 -"""The version this build speaks. - -2 since the chosen hub became something the service remembers, which changed -what ``hub.use`` and ``hub.list`` say. 4 since ``hub.connect`` goes to the chosen -hub and refuses several with ``HUB_AMBIGUOUS``, which a client of 3 does not -know, and says whether the hub was ``already_connected``. -""" - -MIN_PROTOCOL_VERSION: Final = 4 -"""The oldest client version this build still accepts. - -Equal to :data:`PROTOCOL_VERSION`, and moved with it, so that a mismatch is -refused in either direction rather than only when the client is the newer of -the two. During development the service routinely outlives several changes to -the wire in a day. - -This follows :data:`PROTOCOL_VERSION` through the beta releases as well, which -is a decision rather than an oversight: the wire is still expected to move, and -while the two numbers are equal a mismatch is reported with a message naming -which half is old, instead of a stale service being tolerated and then -misbehaving. - -At the first **stable** release it stops following: it stays at the oldest -version the build genuinely serves, and the gap between the two numbers becomes -the compatibility promise. Raising it is then the deliberate act of dropping -support for a released client. -""" - HUB_FOUND: Final = "hub.found" """Notification sent to a session for each advertisement seen while it scans. diff --git a/packages/pb-service/CHANGELOG.md b/packages/pb-service/CHANGELOG.md index bb50109..6763544 100644 --- a/packages/pb-service/CHANGELOG.md +++ b/packages/pb-service/CHANGELOG.md @@ -5,6 +5,12 @@ Notable changes to `pybricks-service`. The format is based on ## [Unreleased] +### Changed + +- The protocol versions this service serves are declared in pb-service, as + `PROTOCOL_VERSION` and `MIN_PROTOCOL_VERSION` in + `pb_service.methods.service`, rather than read from pybricks-core. + ## [1.0.0b3] - 2026-11-23 ### Changed diff --git a/packages/pb-service/src/pb_service/methods/service.py b/packages/pb-service/src/pb_service/methods/service.py index b014358..16176ed 100644 --- a/packages/pb-service/src/pb_service/methods/service.py +++ b/packages/pb-service/src/pb_service/methods/service.py @@ -9,22 +9,82 @@ import platform import secrets import sys -from typing import Any +from typing import Any, Final from pb_core import paths from pb_core.jsonrpc import ErrorCode, JsonRpcError -from pb_core.protocol import ( - MIN_PROTOCOL_VERSION, - PROTOCOL_VERSION, - STDIO_CAPABILITY, -) +from pb_core.protocol import STDIO_CAPABILITY from pb_service import __version__ from pb_service.methods import method, require from pb_service.session import Session logger = logging.getLogger(__name__) -__all__ = ["service_hello", "service_info", "service_ping", "service_shutdown"] +__all__ = [ + "MIN_PROTOCOL_VERSION", + "PROTOCOL_VERSION", + "service_hello", + "service_info", + "service_ping", + "service_shutdown", +] + +PROTOCOL_VERSION: Final = 4 +"""The newest version of the service protocol this service serves. + +Distinct from any package version. Because the service outlives the command that +started it, a freshly installed client will sometimes meet a service started by +an older install, so both sides state this in the opening handshake and the +service refuses versions it cannot serve. + +**What moves this number.** An added method, field or named capability does +not: capabilities are negotiated by name, so a client newer than the running +service asks for the one feature it needs rather than failing over a number. A +*changed meaning* for something that already exists does, because there is no +way to ask about one -- both sides go on truthfully claiming the same version +while meaning different things by it, and the failure is silent. That has +happened once already: ``hub.use`` with no hub in the request meant "forget +the chosen hub" and now means "report it", and a service left running from +before the change threw away the answer to the question it was asked. + +**When it moves.** In the commit that changes the protocol, whether or not a +release is anywhere near. A release is not the only way this code reaches +anyone -- it can be installed from git, and then whatever is between releases +is what somebody is running -- so the number has to be right at every commit +rather than at every tag. It is cheap: nothing outside a checkout speaks an +unreleased version, so the only effect of moving it is that a stale service +says so instead of misbehaving. + +**Where it lives.** Here, and in pb-client for the version it speaks, because +it is a promise about what this code does. It used to be read from +pybricks-core, which let a new pybricks-core make an older service claim a +version it did not implement. + +2 since the chosen hub became something the service remembers, which changed +what ``hub.use`` and ``hub.list`` say. 4 since ``hub.connect`` goes to the chosen +hub and refuses several with ``HUB_AMBIGUOUS``, which a client of 3 does not +know, and says whether the hub was ``already_connected``. +""" + +MIN_PROTOCOL_VERSION: Final = 4 +"""The oldest client version this service still accepts. + +Equal to :data:`PROTOCOL_VERSION`, and moved with it, so that a mismatch is +refused in either direction rather than only when the client is the newer of +the two. During development the service routinely outlives several changes to +the wire in a day. + +This follows :data:`PROTOCOL_VERSION` through the beta releases as well, which +is a decision rather than an oversight: the wire is still expected to move, and +while the two numbers are equal a mismatch is reported with a message naming +which half is old, instead of a stale service being tolerated and then +misbehaving. + +At the first **stable** release it stops following: it stays at the oldest +version the build genuinely serves, and the gap between the two numbers becomes +the compatibility promise. Raising it is then the deliberate act of dropping +support for a released client. +""" @method( diff --git a/tests/e2e/test_service_roundtrip.py b/tests/e2e/test_service_roundtrip.py index 4062ef0..7bfab18 100644 --- a/tests/e2e/test_service_roundtrip.py +++ b/tests/e2e/test_service_roundtrip.py @@ -23,11 +23,11 @@ import pb_client # Imported to register the RPC methods. -import pb_service.methods.service # noqa: F401 # pyright: ignore[reportUnusedImport] +import pb_service.methods.service as service_methods +from pb_client.service import PROTOCOL_VERSION from pb_core import auth, ipc from pb_core.ipc import Endpoint from pb_core.jsonrpc import ErrorCode, JsonRpcError, connect_peer -from pb_core.protocol import PROTOCOL_VERSION from pb_service import service as service_module pytestmark = pytest.mark.anyio @@ -237,3 +237,10 @@ async def test_a_service_starts_while_bluetooth_never_opens( pb_client.connect(endpoint, autostart=False) as service, ): await service.ping() + + +def test_the_client_speaks_what_the_service_serves() -> None: + # Each declares its own, because each is a promise about its own code; in + # one checkout they are moved together. + assert PROTOCOL_VERSION == service_methods.PROTOCOL_VERSION + assert service_methods.MIN_PROTOCOL_VERSION <= PROTOCOL_VERSION