6.0 KiB
RFC: Make JSON-RPC completion and transport directional
Status: proposed
English | 中文
Problem
The JSON-RPC bridge models both endpoints as symmetric peers although the shipped protocol is directional. The TypeScript server accepts requests and emits responses or notifications, but its transport also implements unused outbound requests and inbound notification dispatch. The Python SDK sends requests and receives responses or notifications, but it also queues unused inbound server requests and exposes response helpers.
session/prompt also reports one settled turn through two protocol shapes. The server emits session.finished and then returns the constant { accepted: true }; the Python SDK discards that response and waits for the notification to recover the status. Because the response is written only after the handler returns, the notification necessarily precedes the constant response on the same stream.
The unused halves add pending-request maps, generated IDs, request queues, close-time rejection paths, response helpers, and a second completion waiter without serving a production caller.
Proposal
Specialize each endpoint to its actual role. The TypeScript transport will retain inbound requests, outbound responses, and outbound notifications. The Python client will retain outbound requests and inbound responses or notifications. Delete the opposite-direction request machinery from each side.
Return the settled outcome directly from session/prompt as { status, reason } after agent.whenIdle(). Delete session.finished, the constant acceptance response, and the Python post-response completion loop. session.event and subagent notifications still stream before the response, and durable session events remain the source for final-response reconstruction.
Implementation plan
- In
packages/ui/jsonrpc/src/server.ts, replaceSessionPromptResult.acceptedwithstatus: 'ok' | 'error' | 'aborted'and the capturedTurnEndReason.HarnessSdkServer.prompt()will returncompletedasok,abortedasaborted, and every other current or merge-extensible reason aserror; reaching idle without aturn/endremains an invariant error. Remove onlysession.finished, leavingsession.event,subagent.started, andsubagent.finishedunchanged. - In
packages/ui/jsonrpc/src/transport.ts, replaceJsonRpcTransportPeerwith a server-side notification surface and retainonRequest(),notify(),start(),flush(), andclose(). Remove generated request IDs, the pending-response map, outboundrequest(), inbound response and notification dispatch, and close-time pending-request rejection. Incoming response- and notification-shaped frames will be ignored, while request result, method-not-found, and handler-error responses retain their current behavior and remain ordered after notifications emitted by the awaited handler. - In
python/sdk/src/deepseek_harness/client.py,models.py, and__init__.py, removeIncomingRequest,_requests,notify(),next_request(),respond(), andrespond_error(). Add a public validatedSessionPromptResponsecarrying status and reason, return it fromsession_prompt(), and keep an explicit reader guard that ignores unexpected server-request frames instead of allowing them to match a response waiter. - In
python/sdk/src/deepseek_harness/api.py, buildTurnResult.statusand a newTurnResult.reasonfromSessionPromptResponse, then delete thesession.finishedbranch and second completion loop. Keep the subscription open during the request and preserve_request_raw()'s final notification drain so the lastturn/endevent and any subagent notification written before the response are collected beforeSession.run()reconstructs the final assistant message. - Replace the symmetric transport-pair cases in
packages/ui/jsonrpc/tests/transport.spec.tswith raw client-input/server-output coverage, and updateserver.spec.ts,plugin-apply.spec.ts, andbuilt-scope-carrier.e2e.tsfor direct outcomes, ordering, overlap, shutdown, and the narrowed fake. Updatepython/sdk/tests/test_client.pyfor response-based settlement, unexpected-request-frame handling, callback and concurrency behavior, and the removed public helpers. Update the JSON-RPC and bilingual Python SDK READMEs, export JSDoc and declarations,scripts/smoke-python-runtime.py, and the Python single-executable snapshot.
Alternatives considered
Keep a generic symmetric JSON-RPC peer for future methods. Server-initiated requests may eventually support interactive permissions, but no typed method or production consumer exists. The pre-release protocol can add the smallest required direction when that feature is designed instead of carrying an unexercised peer today.
Keep session.finished for streaming clients. Turn settlement is not incremental data: the request response already marks the same boundary and follows all earlier notifications on the ordered stream. A second terminal notification creates two representations that clients must reconcile.
Acceptance criteria
- The TypeScript endpoint cannot originate requests or consume notifications.
- The Python endpoint cannot originate notifications or consume server requests.
session/promptreturns the authoritativeok,error, orabortedoutcome and reason after turn settlement.- Session events and subagent lifecycle notifications emitted during the turn arrive before the response.
- Same-session overlap rejection, framing, multibyte input, handler errors, flush, shutdown ordering, and final-response reconstruction retain their behavior.
- TypeScript bridge tests, Python SDK tests, built JSON-RPC coverage, snapshots, and generated API documentation pass.
Risks
This deliberately narrows the pre-release wire protocol. Raw clients listening only for session.finished, or embedders using the unused symmetric transport methods, must move to the prompt response. A future server-initiated request requires a new typed protocol addition rather than reusing generic dormant machinery.