Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/pb-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions packages/pb-client/src/pb_client/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]]]
Expand Down
7 changes: 7 additions & 0 deletions packages/pb-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 8 additions & 55 deletions packages/pb-core/src/pb_core/protocol.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.

Expand Down
6 changes: 6 additions & 0 deletions packages/pb-service/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 67 additions & 7 deletions packages/pb-service/src/pb_service/methods/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 9 additions & 2 deletions tests/e2e/test_service_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading