Repository navigation
fix(jsonrpc): don't open a stream when streaming is not supported - #1356
Conversation
When the agent card does not support streaming, SendStreamingMessage and SubscribeToTask over JSON-RPC answered HTTP 200 with an SSE stream whose only event was the UnsupportedOperationError. RestHandler already returns that error as a plain response, and as discussed in a2aproject#791, a StreamResponse cannot carry an error, so an error known before any event exists is returned without opening a stream. JSONRPCHandler now throws the preflight UnsupportedOperationError, and A2AServerRoutes lets an error thrown by the handler propagate, so it is written as an ordinary JSON-RPC error response that keeps the request id. Other preflight errors are still sent inside the stream, as in RestHandler.
|
The |
|
Rerunning now! |
redhat-chai-bot
left a comment
There was a problem hiding this comment.
Nice, well-scoped fix. The JSON-RPC binding was the last transport to open a stream before reporting UnsupportedOperationError — the REST handler already returns a plain error response, and gRPC does the equivalent. Aligning all three transports and fixing the ACTS CORE-CAP-002 conformance failure makes sense.
What I like:
- Selective re-throw design. Only
UnsupportedOperationErrorescapes the handler as a thrown exception; otherA2AErrorsubtypes frompreflight()are still wrapped in a one-item stream. That's the right call — "this operation is fundamentally unsupported" is categorically different from a runtime failure that happens to occur during preflight, and it matches howRestHandleralready treats it. - Request-ID fix. The
invokeJSONRPCHandlerchange to preserve the request ID in theA2AErrorResponseis a good bonus — previously anyA2AErrorthrown after successful request parsing would produce anullID, which breaks the JSON-RPC contract (idMUST echo the request ID on error responses that correlate to a request). - Test quality. The route-level tests in
A2AServerRoutesTestverify the full HTTP response shape (content-typeapplication/json, correct ID, correct error code/message), and the handler-level tests inJSONRPCHandlerTestare dramatically simplified by switching from verboseFlow.Subscriberboilerplate toassertThrows. Net -9 lines despite adding new test coverage.
Minor observations — no changes needed:
- In
processStreamingRequest, removing thetry-catchmeans that anyA2AErrorthrown byonMessageSendStream/onSubscribeToTaskwould now propagate up toinvokeJSONRPCHandleras a plain error. That's safe today because both methods only re-throwUnsupportedOperationErrorand catch everything else internally. If a future change to those methods introduces a new thrownA2AErrorafterpreflight(), it would also bypass the stream. Worth keeping in mind, but not a problem here — the comment in the diff documents the design intent clearly. - The
throws A2AErroron the method signatures is a contract change. SinceJSONRPCHandleris an internal SDK class (not part of the public SPI), this shouldn't break downstream consumers — just noting it for the changelog.
Looks good overall. The CI re-run is in progress (10/14 checks passed, 4 builds running); the previous failure was an unrelated flaky test in V03ResponseIdProbeTest.
Chai persona: A2A Java SDK
AI-generated. Review for accuracy.
| SendStreamingMessageRequest request, ServerCallContext context) { | ||
| SendStreamingMessageRequest request, ServerCallContext context) throws A2AError { | ||
| String tenant = request.getParams() != null ? request.getParams().tenant() : null; | ||
| EffectiveAgentCard effective; |
There was a problem hiding this comment.
Clean separation: UnsupportedOperationError is re-thrown to avoid opening a stream, while other A2AError subtypes stay as stream items. This matches RestHandler's behavior where only UnsupportedOperationError is caught and returned as a plain response.
Nit: the catch (Throwable throwable) on line 325 is new to this method — it wasn't there before the refactor. That's fine (defensive), but the existing onSubscribeToTask doesn't have the same Throwable catch for preflight. Intentional asymmetry, or just not needed there because onSubscribeToTask's preflight was already structured with a try-catch?
Chai persona: A2A Java SDK
AI-generated. Review for accuracy.
There was a problem hiding this comment.
Thanks for re-running the jobs, @kabir! I looked into this one.
The catch (Throwable) here isn't new: on main, preflight sits inside this method's single try, which already ends with catch (Throwable). I only split preflight into its own try so that UnsupportedOperationError can be rethrown, and kept that catch.
The asymmetry was already there in onSubscribeToTask, whose preflight try catches only A2AError. It's the only preflight call in JSONRPCHandler without a catch (Throwable), and RestHandler has one around preflight in both sendStreamingMessage and subscribeToTask. So when preflight throws something else, for example the IllegalStateException from AgentCardValidator when the card lists a transport that isn't on the classpath, SendStreamingMessage gets an InternalError inside the stream with the request id, while SubscribeToTask falls through to the catch (Throwable) in A2AServerRoutes.invokeJSONRPCHandler and gets a plain InternalError with "id": null.
I pushed d1b2958, which adds the same catch (Throwable) to onSubscribeToTask, plus a test that both streaming methods return an InternalError with the request id when no agent card is configured. If you'd rather keep this PR to the capability error, I'm happy to drop that commit and open it separately.
There was a problem hiding this comment.
Great catch — you're right that the catch (Throwable) was already there on main inside the single try; splitting it out just made it more visible. My mistake calling it new.
And nice find on the real asymmetry in onSubscribeToTask. The IllegalStateException from AgentCardValidator is a good concrete example — SendStreamingMessage would surface it as an InternalError with the request ID in the stream, while SubscribeToTask would let it escape to invokeJSONRPCHandler with a null ID. Adding the matching catch (Throwable) there makes sense to me, and including it in this PR keeps the two methods consistent in one diff.
Chai persona: A2A Java SDK
AI-generated. Review for accuracy.
| } catch (A2AError e) { | ||
| error = new A2AErrorResponse(e); | ||
| error = request == null ? new A2AErrorResponse(e) : new A2AErrorResponse(request.getId(), e); | ||
| } catch (InvalidParamsJsonMappingException e) { |
There was a problem hiding this comment.
Good fix. Before this, any A2AError thrown after the request was successfully parsed would produce a null ID in the response — the ternary handles both the parsing-failure case (request is null → null ID) and the handler-failure case (request parsed → echo the ID) correctly.
Chai persona: A2A Java SDK
AI-generated. Review for accuracy.
kabir
left a comment
There was a problem hiding this comment.
@Shay-Deng Would you be able to look at the nitpick the bot mentions in JSONRPCHandler?
onMessageSendStream reports an unexpected exception from preflight as an InternalError inside the stream, and RestHandler does the same for both streaming methods. onSubscribeToTask only caught A2AError there, so such an exception escaped the handler and the route answered with a plain InternalError without the request id. Catch Throwable there too.
redhat-chai-bot
left a comment
There was a problem hiding this comment.
Re-reviewed after d1b2958. The follow-up commit adds the matching catch (Throwable) to onSubscribeToTask's preflight try, plus testNoCardAtAllStreamsInternalErrorForBothStreamingMethods covering both methods. The two streaming handlers are now symmetric in their error handling — both treat UnsupportedOperationError as a re-throw and wrap everything else (A2AError, Throwable) as a stream item with the request ID.
CI is all green (14/14), mergeable, and the previous feedback is addressed. Looks good to me.
Chai persona: A2A Java SDK
AI-generated. Review for accuracy.
Description
When the agent card doesn't support streaming, a JSON-RPC
SendStreamingMessageorSubscribeToTaskgets HTTP 200 with an SSE stream whose only event is the error:Section 3.3.4 says these calls MUST return
UnsupportedOperationError. In #791, the same question came up forSubscribeToTaskon a terminal task, and the conclusion was that aStreamResponsecan't carry an error, so an error known up front has to come back without opening a stream.RestHandleralready does this for the capability check, and the gRPC binding passes the same check. JSON-RPC is the only binding here that still opens the stream.It shows up as ACTS
CORE-CAP-002(SHOULD), the only failure in the a2a-java nightly:expected an error, but the stream opened and produced 1 event(s), on JSON-RPC only.Changes:
JSONRPCHandler.onMessageSendStream/onSubscribeToTaskthrow theUnsupportedOperationErrorfrom the streaming preflight instead of wrapping it in a one-item publisher. Other preflight errors still go into the stream, which matchesRestHandler, where onlyUnsupportedOperationErroris caught and returned as a plain response.A2AServerRoutes.processStreamingRequestno longer turns an error thrown by the handler into a stream item. The error reachesinvokeJSONRPCHandler, which writes the usual JSON-RPC error response.invokeJSONRPCHandlernow uses the request id for anA2AErrorthrown after the request was parsed. It usednew A2AErrorResponse(e), so the id wasnull; that already applied to errors fromauthorizeTaskAccesson the streaming path. Errors thrown while parsing still have anullid.With the change:
Tests:
JSONRPCHandlerTest:testStreamingNotSupportedError,testStreamingNotSupportedErrorOnSubscribeToTaskandcapabilityErrorsRetainStreamingMessageAndPushErrorTypeexpected the error as the first stream item. They now expect it to be thrown, with the same error type and message.A2AServerRoutesTest: two new tests, one forSendStreamingMessageand one forSubscribeToTask. When the handler throwsUnsupportedOperationError, the route answers with a plain JSON-RPC error that keeps the request id.mainthese 5 tests fail; with this change they pass.capabilityErrorsRetainStreamingMessageAndPushErrorTypecame in with refactor!: centralize server validation #1213. If keeping the JSON-RPC capability error inside the stream was intended there, I'm happy to rework this.Testing:
mvn install(JDK 21) fortransport/jsonrpc,reference/jsonrpcandreference/multiversion-jsonrpc, after installing their dependencies: 73 tests intransport/jsonrpc(1 skipped, as onmain) and 239 inreference/jsonrpc, including the Quarkus JSON-RPC tests. All pass.Full ACTS corpus from a2a-itk
main(604239c), all three bindings, against theitk/agent built frommainand from this branch. I ran the agent myself and passed it with--sut-url, plus a second instance withITK_ACTS_REDUCED_CAPABILITIES=1as--reduced-sut-url. Of 283 results, the only change isCORE-CAP-002on JSON-RPC, from fail to pass. The 7 skippedSEC-AUTH-*results are the same on both runs, because I didn't start anITK_ACTS_AUTHinstance.Follow the
CONTRIBUTINGGuide.Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
Ensure the tests pass
Appropriate READMEs were updated (if necessary)
AI assistance: developed with help from Claude Code. I reviewed the change, and the checks listed under Testing were run locally.