Repository navigation
Harden JSON-RPC server mode protocol - #10779
Amaury Levé (Evangelink) merged 26 commits into
Conversation
Add explicit protocol negotiation, enforce lifecycle and error semantics, make completion handling resilient, and align both JSON serializers with a machine-readable protocol schema. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Pull request overview
Hardens MTP JSON-RPC server mode through protocol negotiation, lifecycle enforcement, serializer parity, and updated documentation.
Changes:
- Adds independent protocol-version negotiation and client validation.
- Standardizes request errors, cancellation, and terminal update ordering.
- Adds protocol schema, documentation, and cross-serializer tests.
Show a summary per file
| File | Description |
|---|---|
test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs |
Tests lifecycle, negotiation, cancellation, and completion ordering. |
test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/FormatterUtilitiesTests.cs |
Tests IDs, params, capabilities, and serialization. |
test/UnitTests/Microsoft.Testing.Platform.ServerMode.Client.Sources.UnitTests/MtpServerClientTests.cs |
Tests client negotiation and legacy compatibility. |
test/UnitTests/Microsoft.Testing.Platform.ServerMode.Client.Sources.UnitTests/FakeMtpServer.cs |
Adds negotiated version to the fake server. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.RpcMessageSerializers.cs |
Serializes negotiated protocol versions. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.Deserializers.cs |
Handles params, versions, and notifications. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.cs |
Normalizes numeric-string IDs. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/RpcMessages.cs |
Extends initialization models and corrects coverage capability. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/PassiveNode.cs |
Advertises the server-mode protocol version. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/JsonRpcMethods.cs |
Defines protocol versions and wire keys. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/Json/Json.Serializers.cs |
Adds protocol-version serialization. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/Json/Json.Deserializers.cs |
Aligns IDs, params, and version deserialization. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/Json/Json.cs |
Treats JSON null as an absent optional value. |
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/ErrorCodes.cs |
Adds unsupported-protocol error code. |
src/Platform/Microsoft.Testing.Platform/InternalAPI/InternalAPI.Unshipped.txt |
Tracks new internal APIs. |
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.RequestExecution.cs |
Negotiates versions and standardizes method errors. |
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.Messaging.cs |
Supports best-effort terminal notifications. |
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.MessageLoop.cs |
Enforces lifecycle and response ordering. |
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs |
Stores initialization state. |
src/Platform/Microsoft.Testing.Platform.ServerMode.Client.Sources/Client/SerializerUtilities.ClientSerializers.cs |
Serializes client-supported versions. |
src/Platform/Microsoft.Testing.Platform.ServerMode.Client.Sources/Client/MtpServerClientOptions.cs |
Exposes protocol-version configuration. |
src/Platform/Microsoft.Testing.Platform.ServerMode.Client.Sources/Client/MtpServerClient.cs |
Negotiates and validates the server version. |
src/Platform/Microsoft.Testing.Platform.ServerMode.Client.Sources/Client/IMtpServerClient.cs |
Exposes the negotiated version. |
docs/RFCs/019-Code-Coverage-Messages.md |
Clarifies coverage capability semantics. |
docs/mstest-runner-protocol/server-mode-1.0.schema.json |
Defines the Draft 2020-12 wire schema. |
docs/mstest-runner-protocol/001-protocol-intro.md |
Updates the protocol specification. |
Review details
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Suppressed comments (2)
src/Platform/Microsoft.Testing.Platform.ServerMode.Client.Sources/Client/MtpServerClient.cs:226
- A present but non-string
protocolVersionis silently converted tonull, so the client treats a malformed negotiation response as a compatible legacy server and bypasses the new version check. Only an absent property should activate legacy compatibility; reject the property when it is present with the wrong type.
string? protocolVersion = result.TryGetValue(JsonRpcStrings.ProtocolVersion, out object? protocolVersionObj)
? protocolVersionObj as string
: null;
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.Deserializers.cs:155
- Jsonite treats every non-string
protocolVersionvalue as if the property were absent, while the System.Text.Json path rejects the same malformed value. This preserves a serializer-dependent legacy fallback; explicitly reject non-null, non-string values.
string? protocolVersion = GetOptionalPropertyFromJson(properties, JsonRpcStrings.ProtocolVersion) as string;
- Files reviewed: 26/26 changed files
- Comments generated: 4
- Review effort level: Balanced
Resolve protocol documentation and server-mode test conflicts while retaining upstream deadline-state coverage. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Review details
Suppressed comments (1)
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/PassiveNode.cs:76
- The passive endpoint now advertises
1.0.0unconditionally instead of negotiating theInitializeRequestArgs.ProtocolVersionsit just received. A client that offers only an incompatible version therefore gets a successful initialize response selecting a version it never offered, unlike the primary server. Negotiate here too and returnProtocolVersionNotSupportedwhen there is no match.
ProtocolVersion = JsonRpcProtocolVersions.Current,
- Files reviewed: 26/26 changed files
- Comments generated: 7
- Review effort level: Balanced
Align passive-node negotiation, legacy empty-version handling, serializer type validation, RPC ID schema validation, and Markdown formatting. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Review details
Suppressed comments (2)
src/Platform/Microsoft.Testing.Platform.ServerMode.Client.Sources/Client/MtpServerClient.cs:136
- An omitted
protocolVersionmeans the server is using the legacy 1.0.0 protocol, but this guard skips validation for that case. A client configured with only a future version such as 2.0.0 therefore accepts a legacy server and proceeds using a wire contract it declared unsupported. ValidatenullasV1while preserving the exposed nullable value for legacy-server detection.
if (capabilities.ProtocolVersion is { } negotiatedProtocolVersion
&& !IsSupportedProtocolVersion(negotiatedProtocolVersion))
{
throw new MtpServerClientException(
$"The server negotiated unsupported protocol version '{negotiatedProtocolVersion}'. "
+ $"Supported versions: {string.Join(", ", _options.SupportedProtocolVersions)}.");
docs/mstest-runner-protocol/server-mode-1.0.schema.json:99
- Because
additionalPropertiesis enabled andidis not constrained here, a payload such as anexitnotification with"id": 1validates against this schema. The runtime treats every non-null ID as a request instead, so the published schema accepts a message with different protocol semantics. Constrain an optional notification ID tonull; this preserves the implementation's deliberateid: nullnotification behavior while rejecting request IDs.
"properties": {
"jsonrpc": {
"const": "2.0"
},
"method": {
- Files reviewed: 27/27 changed files
- Comments generated: 3
- Review effort level: Balanced
Normalize omitted versions to V1, pin the 1.0 schema response version, and reject top-level IDs on notifications. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Review details
Suppressed comments (2)
Previously missed (2) — in code that hasn't changed since the last review.
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.Deserializers.cs:34
- This edited file is currently UTF-8 without a BOM, but
.editorconfig:66-67requiresutf-8-bomfor all C# files. Re-save the file with an EF BB BF BOM.
object? rawParams = GetOptionalPropertyFromJson(properties, JsonRpcStrings.Params);
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.RpcMessageSerializers.cs:80
- This edited file is currently UTF-8 without a BOM, but
.editorconfig:66-67requiresutf-8-bomfor all C# files. Re-save the file with an EF BB BF BOM.
if (res.ProtocolVersion is not null)
- Files reviewed: 27/27 changed files
- Comments generated: 2
- Review effort level: Balanced
Retain numeric-string ID representation in responses and publish initialized state only after the initialize response write succeeds. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
Re-encode the edited serializer files as UTF-8 with BOM per repository policy. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Review details
Suppressed comments (2)
Previously missed (1) — in code that hasn't changed since the last review.
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.Deserializers.cs:65
- The formatter paths still disagree on malformed nested request parameters. In this Jsonite path,
Deserialize<RunRequestArgs>/Deserialize<DiscoverRequestArgs>useas stringforfilterandas ICollection<object>fortests(lines 203-223), so a payload such as"filter": 42is silently treated as no filter and can execute all tests; the System.Text.Json path throws and returnsInvalidParams. Validate an optional property’s type whenever it is present rather than silently casting it to null.
catch (Exception ex) when (ex is MessageFormatException or InvalidCastException)
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.MessageLoop.cs:249
- There is a race after a successful initialize response: the transport write can make the response visible to the client before this continuation stores
Initialized. A client that sends its first request immediately after reading the response can therefore be rejected withServerNotInitialized. Make response publication and the lifecycle transition an ordered operation (for example, coordinate the transition with the message lock or queue requests observed while initialization is completing).
if (isInitializeRequest)
{
Volatile.Write(ref _initializeState, Initialized);
}
- Files reviewed: 27/27 changed files
- Comments generated: 2
- Review effort level: Balanced
Preserve string IDs on client replies, queue requests during initialization, validate Jsonite optional fields, and support primitive error data. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
docs/mstest-runner-protocol/server-mode-1.0.schema.json:205
- The new schema requires an integer
processId, while001-protocol-intro.md:206-210still says this field may be null. The implementation also requiresint, so the published prose and machine-readable contract now disagree; update the protocol document to describe the implemented non-null requirement (or add nullable handling consistently).
"processId": {
"type": "integer"
},
- Files reviewed: 28/28 changed files
- Comments generated: 3
- Review effort level: Balanced
Document the required integer process ID used by the protocol 1.0 schema and implementation. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
docs/mstest-runner-protocol/server-mode-1.0.schema.json:286
- This shared selection schema advertises optional
testsandfilterfor discovery (and run), matchingDiscoverRequestArgs/RunRequestArgs, but the prose contract still definesDiscoverTestsParamswith onlyrunIdandRunTestsParamswithoutfilter(001-protocol-intro.md:526-528,601-609). Align those interfaces so the two published protocol contracts describe the same valid requests.
"$ref": "#/$defs/testSelectionParams"
- Files reviewed: 28/28 changed files
- Comments generated: 1
- Review effort level: Balanced
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Review details
Suppressed comments (3)
Previously missed (3) — in code that hasn't changed since the last review.
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.cs:85
- On the Jsonite path, fractional/exponent numbers are parsed as
doublebefore reaching this check. A non-integral wire ID such as1.00000000000000001rounds to1.0, passesMath.Truncate, and is accepted on net462/netstandard2.0, while the System.Text.Json path and the schema reject it. Preserve an exact numeric representation for ID validation (for example, parse this value asdecimalor validate the raw token) so both formatters reject non-integral IDs consistently.
double idDouble when idDouble == Math.Truncate(idDouble)
&& idDouble >= int.MinValue
&& idDouble <= int.MaxValue => (int)idDouble,
docs/mstest-runner-protocol/server-mode-1.0.schema.json:235
- The source client copies
SupportedProtocolVersionsdirectly to this array and the server accepts duplicates, butIReadOnlyCollection<string>does not require uniqueness. A caller configuring["1.0.0", "1.0.0"]therefore emits a request that works in the implementation but fails this published schema. Since duplicates do not affect negotiation, removeuniqueItems(or deduplicate/validate the client option and server input).
"items": {
"type": "string"
},
"uniqueItems": true
docs/mstest-runner-protocol/001-protocol-intro.md:268
- The schema permits an explicit
nullhere, and both deserializers treat it like an omitted legacy version, but this TypeScript contract only permits a string or absence. Includenullso the prose contract matches the schema and implemented legacy behavior.
// Independently negotiated wire-protocol version.
protocolVersion?: string,
- Files reviewed: 28/28 changed files
- Comments generated: 0 new
- Review effort level: Balanced
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
|
One or more custom setup steps configured for this repository failed during this Copilot code review run: Setup steps run before each review. If the review above is missing context, or no review was posted at all, the failing step above may be the cause. See the workflow run for failure details, fix your setup steps configuration, and re-request a review. Note You can configure setup steps for Copilot code review separately from Copilot cloud agent with a |
There was a problem hiding this comment.
Review details
Suppressed comments (3)
Previously missed (1) — in code that hasn't changed since the last review.
docs/mstest-runner-protocol/server-mode-1.0.schema.json:707
- This response schema permits an additional top-level
method, so a payload such as{"jsonrpc":"2.0","id":1,"method":"bogus","result":null}validates as a success response. Both deserializers checkmethodfirst and classify that payload as a request instead, so the schema accepts a message with different runtime semantics. Explicitly prohibitmethodon response envelopes, as the notification envelope already prohibitsid.
This issue also appears on line 743 of the same file.
"additionalProperties": true
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.MessageLoop.cs:286
- A permitted retry can reuse the failed initialize request's ID immediately after receiving its error, but that old ID remains in
_clientToServerRequestsuntil the error-writingfinallycompletes. In that race, thisTryAddreturns false and is ignored, so the retry runs with an unregisteredRpcInvocationState; cancellation targets the stale request and the new state is never disposed. Ensure registration succeeds (or wait/remove the completed state before accepting reuse) while preserving the required error-before-waiter ordering.
rpcState ??= new RpcInvocationState();
if (!requestRegistered)
{
_clientToServerRequests.TryAdd(GetRequestKey(request.Id, request.StringId), rpcState);
docs/mstest-runner-protocol/server-mode-1.0.schema.json:743
- The error-response envelope also allows a top-level
method, but the runtime then deserializes the frame as a request and never observes itserror. Prohibitmethodhere so every schema-valid error response is classified as an error by both formatter implementations.
"additionalProperties": true
- Files reviewed: 29/29 changed files
- Comments generated: 1
- Review effort level: Balanced
Build Failure AnalysisVerdict: Genuine build failure — identical root cause across all 5 build legs (Linux Release, Windows Debug, Windows Release, Windows Root causeThe This PR's Suggested fixBecause ...then commit the regenerated Notes
Build overview (Linux Release leg, representative of all 5)
|
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c1399eff-1328-4e8f-92d4-d730891fbefd
There was a problem hiding this comment.
Copilot review overview
Review tier: Balanced
Findings: None
Issues resolved since last review (1)
| Severity | Finding |
|---|---|
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/Json/Json.Deserializers.cs — An explicit JSON null ID is treated as if the member were absent, so `{ "id": null, "method": ...… View resolved comment |

Summary
Validation
Microsoft.Testing.Platformfornet8.0,net9.0, andnetstandard2.0net8.0andnet462for both serializer implementationsserver-mode-1.0.schema.jsonagainst the Draft 2020-12 metaschema