Skip to content

connection

Class info

Classes

Name Children Inherits
Connection
aclientp.connection
Minimal JSON-RPC 2.0 connection over newline-delimited JSON frames.
    StreamEvent
    aclientp.connection
    Stream event.

      🛈 DocStrings

      ACP Connection.

      NotificationFilter module-attribute

      NotificationFilter = Callable[[str, JsonValue | None], bool]
      

      Selects notifications by (method, params).

      Connection

      Minimal JSON-RPC 2.0 connection over newline-delimited JSON frames.

      Using anyio streams for cross-platform compatibility.

      • Outgoing messages always include {"jsonrpc": "2.0"}
      • Requests and notifications are dispatched to a single async handler
      • Responses resolve pending futures by id
      • Malformed frames from the peer are logged and skipped, never fatal
      • Once the peer disconnects or the connection is closed, pending requests fail and new sends raise ConnectionError

      Ordering: notifications run concurrently (each in its own task, started in the order received), but a request only returns (or raises the peer's error) once the notifications received before its response have been handled. A caller sees the effects of everything the peer sent before answering, e.g. a turn's or a replay's updates before the response that ends it. Not waited for: the notification the calling task itself handles (a handler sending a request), and notifications received after the response. A notification handler that waits for something the caller only does after its request returns would deadlock.

      Source code in src/aclientp/connection.py
       77
       78
       79
       80
       81
       82
       83
       84
       85
       86
       87
       88
       89
       90
       91
       92
       93
       94
       95
       96
       97
       98
       99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
      163
      164
      165
      166
      167
      168
      169
      170
      171
      172
      173
      174
      175
      176
      177
      178
      179
      180
      181
      182
      183
      184
      185
      186
      187
      188
      189
      190
      191
      192
      193
      194
      195
      196
      197
      198
      199
      200
      201
      202
      203
      204
      205
      206
      207
      208
      209
      210
      211
      212
      213
      214
      215
      216
      217
      218
      219
      220
      221
      222
      223
      224
      225
      226
      227
      228
      229
      230
      231
      232
      233
      234
      235
      236
      237
      238
      239
      240
      241
      242
      243
      244
      245
      246
      247
      248
      249
      250
      251
      252
      253
      254
      255
      256
      257
      258
      259
      260
      261
      262
      263
      264
      265
      266
      267
      268
      269
      270
      271
      272
      273
      274
      275
      276
      277
      278
      279
      280
      281
      282
      283
      284
      285
      286
      287
      288
      289
      290
      291
      292
      293
      294
      295
      296
      297
      298
      299
      300
      301
      302
      303
      304
      305
      306
      307
      308
      309
      310
      311
      312
      313
      314
      315
      316
      317
      318
      319
      320
      321
      322
      323
      324
      325
      326
      327
      328
      329
      330
      331
      332
      333
      334
      335
      336
      337
      338
      339
      340
      341
      342
      343
      344
      345
      346
      347
      348
      349
      350
      351
      352
      353
      354
      355
      356
      357
      358
      359
      360
      361
      362
      363
      364
      365
      366
      367
      368
      369
      370
      371
      372
      373
      374
      375
      376
      377
      378
      379
      380
      381
      382
      383
      384
      385
      386
      387
      388
      389
      390
      391
      392
      393
      394
      395
      396
      397
      398
      399
      400
      401
      402
      403
      404
      405
      406
      407
      408
      409
      410
      411
      412
      413
      414
      415
      416
      417
      418
      419
      420
      421
      422
      423
      424
      425
      426
      427
      428
      429
      430
      431
      432
      433
      434
      435
      436
      437
      438
      439
      440
      441
      442
      443
      444
      445
      446
      447
      448
      449
      450
      451
      452
      453
      454
      455
      456
      457
      458
      459
      460
      461
      462
      463
      464
      465
      466
      467
      468
      469
      470
      471
      472
      473
      474
      475
      476
      477
      478
      479
      480
      481
      482
      483
      484
      485
      486
      487
      488
      489
      490
      491
      492
      493
      494
      495
      496
      497
      498
      499
      500
      501
      502
      503
      504
      505
      506
      507
      508
      509
      510
      511
      512
      513
      514
      515
      class Connection:
          """Minimal JSON-RPC 2.0 connection over newline-delimited JSON frames.
      
          Using anyio streams for cross-platform compatibility.
      
          - Outgoing messages always include {"jsonrpc": "2.0"}
          - Requests and notifications are dispatched to a single async handler
          - Responses resolve pending futures by id
          - Malformed frames from the peer are logged and skipped, never fatal
          - Once the peer disconnects or the connection is closed, pending requests fail
            and new sends raise ``ConnectionError``
      
          Ordering: notifications run concurrently (each in its own task, started in the
          order received), but a request only returns (or raises the peer's error) once the
          notifications received before its response have been handled. A caller sees the
          effects of everything the peer sent before answering, e.g. a turn's or a replay's
          updates before the response that ends it. Not waited for: the notification the
          calling task itself handles (a handler sending a request), and notifications
          received after the response. A notification handler that waits for something the
          caller only does after its request returns would deadlock.
          """
      
          def __init__(
              self,
              handler: MethodHandler,
              writer: ByteSendStream,
              reader: ByteReceiveStream,
              *,
              queue: MessageQueue | None = None,
              state_store: MessageStateStore | None = None,
              dispatcher_type: type[MessageDispatcher] | None = None,
              sender_type: type[MessageSender] | None = None,
              observers: list[StreamObserver] | None = None,
              reject_batches: bool = False,
              strict_request_ids: bool = False,
          ) -> None:
              self._handler = handler
              # ACP v2: answer a JSON array (a JSON-RPC batch) with a single -32600 instead
              # of skipping it, and reject requests whose ID is not a string or integer.
              self.reject_batches = reject_batches
              self.strict_request_ids = strict_request_ids
              self._writer = writer
              self._reader = reader
              self._text_reader = TextReceiveStream(reader)
              self._next_request_id = 0
              self._state = state_store or InMemoryMessageStateStore()
              self._tasks = TaskSupervisor(source="acp.Connection", error_handlers=[self._on_task_error])
              self._queue = queue or InMemoryMessageQueue()
              self._closed = False
              self._disconnected = asyncio.Event()
              self._observers: list[StreamObserver] = list(observers or [])
              # Incoming requests currently being handled, for `$/cancel_request`
              self._inflight: dict[RequestId, asyncio.Task[dict[str, Any]]] = {}
              # Notifications received but not handled yet, with their (method, params)
              self._pending_notifications: dict[asyncio.Future[None], tuple[str, JsonValue | None]] = {}
              # Requests sent and not answered yet; per answered one, the notifications that
              # were pending when its response came
              self._awaiting: set[int] = set()
              self._handled_before: dict[int, set[asyncio.Future[None]]] = {}
              sender_type = sender_type or MessageSender
              self._sender = sender_type(self._writer, self._tasks)
              dispatcher_type = dispatcher_type or DefaultMessageDispatcher
              self._dispatcher = dispatcher_type(
                  queue=self._queue,
                  supervisor=self._tasks,
                  store=self._state,
                  request_runner=self._run_request,
                  notification_runner=self._run_notification,
              )
              self._dispatcher.start()
              self._recv_task = self._tasks.create(
                  self._receive_loop(),
                  name="acp.Connection.receive",
                  on_error=self._on_receive_error,
              )
      
          @property
          def closed(self) -> bool:
              """Whether the connection was closed or the peer disconnected."""
              return self._closed or self._disconnected.is_set()
      
          async def close(self) -> None:
              """Stop the receive loop and cancel any in-flight handler tasks.
      
              Messages already queued for sending (e.g. ``$/cancel_request``) are flushed
              first. A handler may close its own connection; no response is guaranteed
              for the request it is handling.
              """
              if self._closed:
                  return
              self._closed = True
              await self._dispatcher.stop()
              await self._sender.close()
              await self._tasks.shutdown()
              self._disconnect(ConnectionError("Connection closed"))
      
          async def wait_closed(self) -> None:
              """Wait until the peer disconnected (EOF) or the connection was closed."""
              await self._disconnected.wait()
      
          async def __aenter__(self) -> Self:
              return self
      
          async def __aexit__(self, *args: object) -> None:
              await self.close()
      
          async def send_request(self, method: str, params: JsonValue | None = None) -> Any:
              """Send a request and wait for the peer's response.
      
              Returns (or raises the peer's error) once the notifications received before the
              response have been handled (see the class docs). Cancelling the caller sends
              ``$/cancel_request`` for a request still in flight.
      
              Raises:
                  RequestError: The peer answered with an error.
                  ConnectionError: The connection is closed or got closed before a response.
              """
              self._raise_if_closed()
              request_id = self._next_request_id
              self._next_request_id += 1
              future = self._state.register_outgoing(request_id, method)
              payload = {"jsonrpc": "2.0", "id": request_id, "method": method, "params": params}
              self._awaiting.add(request_id)
              try:
                  await self._sender.send(payload)
                  self._notify_observers("outgoing", payload)
                  try:
                      return await future
                  finally:  # Answered (result or error): first what the peer sent before
                      self._awaiting.discard(request_id)
                      if (before := self._handled_before.pop(request_id, None)) is not None:
                          await self._wait_handled(before)
              except BaseException as exc:
                  answered = future.done() and not future.cancelled()
                  future.cancel()  # Nobody waits for it anymore
                  if not answered:
                      self._state.reject_outgoing(request_id, RequestError.request_cancelled())
                      # A request cancelled while queued for sending still gets written.
                      if isinstance(exc, asyncio.CancelledError):
                          self._send_cancel_request(request_id)
                  raise
              finally:
                  self._awaiting.discard(request_id)
                  self._handled_before.pop(request_id, None)
      
          async def request_object(self, method: str, params: JsonValue | None = None) -> dict[str, Any]:
              """Send a request whose result is a JSON object, as for all ACP methods.
      
              A ``null`` result is treated as an empty object.
      
              Raises:
                  RequestError: The peer answered with an error or a non-object result.
                  ConnectionError: The connection is closed or got closed before a response.
              """
              match await self.send_request(method, params):
                  case None:
                      return {}
                  case dict() as result:
                      return result
                  case other:
                      details = f"Expected an object result for {method}, got {type(other).__name__}"
                      raise RequestError.internal_error({"details": details})
      
          async def send_notification(self, method: str, params: JsonValue | None = None) -> None:
              """Send a notification.
      
              Raises:
                  ConnectionError: The connection is closed.
              """
              self._raise_if_closed()
              payload = {"jsonrpc": "2.0", "method": method, "params": params}
              await self._sender.send(payload)
              self._notify_observers("outgoing", payload)
      
          async def cancel_request(self, request_id: RequestId) -> None:
              """Ask the peer to cancel a request this side issued (``$/cancel_request``)."""
              await self.send_notification(CANCEL_REQUEST_METHOD, {"requestId": request_id})
      
          async def wait_for_notifications(self, select: NotificationFilter | None = None) -> None:
              """Wait until the notifications received so far have been handled.
      
              Requests already wait for the notifications received before their response
              (see the class docs); this waits for all received so far, e.g. before acting on
              a notification. Notifications received later, and the one handled by the
              calling task, are not waited for.
      
              Args:
                  select: Only wait for notifications matching this ``(method, params)`` filter.
              """
              pending = {
                  done
                  for done, (method, params) in self._pending_notifications.items()
                  if select is None or select(method, params)
              }
              await self._wait_handled(pending)
      
          async def _wait_handled(self, notifications: set[asyncio.Future[None]]) -> None:
              """Wait until ``notifications`` are handled (except the calling task's own)."""
              current = _current_notification.get()
              if pending := {done for done in notifications if done is not current and not done.done()}:
                  await asyncio.wait(pending)
      
          def _send_cancel_request(self, request_id: RequestId) -> None:
              """Enqueue ``$/cancel_request`` without awaiting, so it survives cancellation."""
              if self.closed:
                  return
              payload = {
                  "jsonrpc": "2.0",
                  "method": CANCEL_REQUEST_METHOD,
                  "params": {"requestId": request_id},
              }
              try:
                  written = self._sender.send_nowait(payload)
              except ConnectionError:
                  return
              self._notify_observers("outgoing", payload)
      
              def _log_failure(fut: asyncio.Future[None]) -> None:
                  if not fut.cancelled() and (exc := fut.exception()) is not None:
                      logger.debug("Failed to send $/cancel_request", request_id=request_id, exc=exc)
      
              written.add_done_callback(_log_failure)
      
          def _handle_cancel_request(self, params: Any) -> None:
              if not isinstance(params, dict) or not is_request_id(request_id := params.get("requestId")):
                  return
              # Unknown or already answered requests are ignored, as required by the spec.
              if (task := self._inflight.get(request_id)) is not None:
                  task.cancel()
      
          async def _receive_loop(self) -> None:
              """Read newline-delimited JSON messages using anyio TextReceiveStream.
      
              This approach has no 64KB line limit unlike asyncio's readline(), and the
              incremental decoder handles UTF-8 sequences split across chunks.
              """
              buffer = ""
              try:
                  async for chunk in self._text_reader:
                      lines = (buffer + chunk).split("\n")
                      buffer = lines.pop()  # Keep incomplete line in buffer
                      for line in lines:
                          await self._handle_line(line)
                  # A final frame without trailing newline
                  await self._handle_line(buffer)
              except asyncio.CancelledError:
                  return
              except (anyio.ClosedResourceError, anyio.BrokenResourceError, anyio.EndOfStream):
                  pass
              # EOF / closed: reject in-flight requests so callers get an error
              # instead of hanging forever (e.g. subprocess crash during initialize).
              self._disconnect(ConnectionError("Connection closed: remote end sent EOF"))
      
          async def _handle_line(self, line: str) -> None:
              if self.reject_batches and _is_batch(line):
                  await self._reply_error(None, RequestError.invalid_request({"details": "batch"}))
                  return
              if message := self._decode_line(line):
                  self._notify_observers("incoming", message)
                  await self._process_message(message)
      
          async def _reply_error(self, request_id: RequestId | None, error: RequestError) -> None:
              payload = {"jsonrpc": "2.0", "id": request_id, "error": error.to_error_obj()}
              try:
                  await self._sender.send(payload)
              except ConnectionError:
                  if not self.closed:
                      raise
                  return
              self._notify_observers("outgoing", payload)
      
          def _decode_line(self, line: str) -> dict[str, Any] | None:
              """Decode one frame, returning None for blank or malformed ones.
      
              Like the Rust and TypeScript SDKs, malformed frames are skipped rather than
              answered, so a peer printing stray output to stdout cannot break the session.
              """
              if not (line := line.strip()):
                  return None
              try:
                  message = anyenv.load_json(line)
              except anyenv.JsonLoadError:
                  logger.warning("Skipping unparsable JSON-RPC frame", line=line[:_LOG_LINE_LIMIT])
                  return None
              if not isinstance(message, dict) or not message:
                  # Includes batches, which ACP does not use.
                  logger.warning("Skipping non-object JSON-RPC frame", line=line[:_LOG_LINE_LIMIT])
                  return None
              return message
      
          async def _process_message(self, message: dict[str, Any]) -> None:
              match message:
                  case {"method": str() as method, **rest} if "id" not in rest:
                      if method == CANCEL_REQUEST_METHOD:
                          self._handle_cancel_request(message.get("params"))
                          return
                      done = asyncio.get_running_loop().create_future()
                      self._pending_notifications[done] = (method, message.get("params"))
                      await self._queue.publish(RpcTask("notification", message, done))
                  case {"method": str(), "id": request_id}:
                      if self.strict_request_ids and not is_request_id(request_id):
                          error = RequestError.invalid_request({"details": "ID must be string/int"})
                          await self._reply_error(None, error)
                          return
                      await self._queue.publish(RpcTask("request", message))
                  case {"id": _}:
                      self._handle_response(message)
                  case _:
                      logger.warning("Ignoring invalid JSON-RPC message", message=message)
      
          def _notify_observers(self, direction: StreamDirection, message: dict[str, Any]) -> None:
              if not self._observers:
                  return
              snapshot = copy.deepcopy(message)
              event = StreamEvent(direction, snapshot)
              for observer in list(self._observers):
                  try:
                      result = observer(event)
                  except Exception:
                      logger.exception("Stream observer failed")
                      continue
                  if inspect.isawaitable(result) and not self._tasks.closed:
                      name = f"acp.Connection.observer.{direction}"
                      self._tasks.create(result, name=name, on_error=self._on_observer_error)
      
          def _on_observer_error(self, task: asyncio.Task[Any], exc: BaseException) -> None:
              logger.error("Stream observer coroutine failed", exc_info=exc)
      
          async def _run_request(self, task: RpcTask) -> dict[str, Any]:
              """Run a request handler and send its response.
      
              The handler runs in its own task, so ``$/cancel_request`` can cancel it without
              also cancelling delivery of the response the peer still expects.
              """
              message = task.message
              request_id = message["id"]
              handler = self._tasks.create(self._execute_request(message), name="acp.Connection.handler")
              if is_request_id(request_id):
                  self._inflight[request_id] = handler
      
                  def _forget(done: asyncio.Task[dict[str, Any]]) -> None:
                      if self._inflight.get(request_id) is done:
                          del self._inflight[request_id]
      
                  handler.add_done_callback(_forget)
              # Unlike `await handler`, this does not re-raise the handler's cancellation.
              await asyncio.wait({handler})
              if handler.cancelled():  # By `$/cancel_request` or from inside the handler
                  err = RequestError.request_cancelled()
                  payload = {"jsonrpc": "2.0", "id": request_id, "error": err.to_error_obj()}
              else:
                  payload = handler.result()
              try:
                  await self._sender.send(payload)
              except ConnectionError:
                  if not self.closed:
                      raise
                  # The connection went away while the handler was running.
                  logger.debug("Dropping response: connection closed", request_id=request_id)
                  return payload
              self._notify_observers("outgoing", payload)
              return payload
      
          async def _execute_request(self, message: dict[str, Any]) -> dict[str, Any]:
              """Run the handler and turn its outcome into a response payload."""
              payload: dict[str, Any] = {"jsonrpc": "2.0", "id": message["id"]}
              method = message["method"]
              try:
                  result = await self._handler(method, message.get("params"), False)
              except RequestError as exc:
                  payload["error"] = exc.to_error_obj()
              except ValidationError as exc:
                  errors = exc.errors(include_url=False, include_context=False)
                  payload["error"] = RequestError.invalid_params({"errors": errors}).to_error_obj()
              except Exception as exc:
                  logger.exception("Unhandled error while handling request", method=method)
                  try:
                      data = anyenv.load_json(str(exc), return_type=dict)
                  except Exception:  # noqa: BLE001
                      data = {"details": str(exc)}
                  payload["error"] = RequestError.internal_error(data).to_error_obj()
              else:
                  if isinstance(result, BaseModel):
                      result = result.model_dump(mode="json", by_alias=True, exclude_none=True)
                  payload["result"] = result
              return payload
      
          async def _run_notification(self, task: RpcTask) -> None:
              method = task.message["method"]
              token = _current_notification.set(task.done)
              try:
                  await self._handler(method, task.message.get("params"), True)
              except Exception:
                  logger.exception("Unhandled error while handling notification", method=method)
              finally:
                  _current_notification.reset(token)
                  if task.done is not None:
                      self._complete_notification(task.done)
      
          def _complete_notification(self, done: asyncio.Future[None]) -> None:
              self._pending_notifications.pop(done, None)
              if not done.done():
                  done.set_result(None)
      
          def _handle_response(self, message: dict[str, Any]) -> None:
              request_id = message["id"]
              # Outgoing ids are integers; anything else (e.g. `id: null` in a peer's parse
              # error report) cannot belong to a request of ours.
              if not isinstance(request_id, int) or isinstance(request_id, bool):
                  logger.warning("Ignoring response with unknown id", message=message)
                  return
              if request_id in self._awaiting:
                  # What the peer sent before this response is handled before it is delivered
                  self._handled_before[request_id] = set(self._pending_notifications)
              if "error" in message:
                  error = RequestError.from_error_obj(message["error"])
                  self._state.reject_outgoing(request_id, error)
              else:
                  self._state.resolve_outgoing(request_id, message.get("result"))
      
          def _raise_if_closed(self) -> None:
              if self.closed:
                  raise ConnectionError("Connection closed")
      
          def _disconnect(self, error: ConnectionError) -> None:
              self._disconnected.set()
              self._state.reject_all_outgoing(error)
              # Notifications that will never run must not block `wait_for_notifications`.
              # Queued ones still run after a peer EOF, but not after close().
              if self._closed:
                  for done in list(self._pending_notifications):
                      self._complete_notification(done)
      
          def _on_receive_error(self, task: asyncio.Task[Any], exc: BaseException) -> None:
              logger.error("Receive loop failed", exc_info=exc)
              self._disconnect(ConnectionError(f"Connection failed: {exc!r}"))
      
          def _on_task_error(self, task: asyncio.Task[Any], exc: BaseException) -> None:
              logger.error("Background task failed", task=task.get_name(), exc_info=exc)
      

      closed property

      closed: bool
      

      Whether the connection was closed or the peer disconnected.

      cancel_request async

      cancel_request(request_id: RequestId) -> None
      

      Ask the peer to cancel a request this side issued ($/cancel_request).

      Source code in src/aclientp/connection.py
      async def cancel_request(self, request_id: RequestId) -> None:
          """Ask the peer to cancel a request this side issued (``$/cancel_request``)."""
          await self.send_notification(CANCEL_REQUEST_METHOD, {"requestId": request_id})
      

      close async

      close() -> None
      

      Stop the receive loop and cancel any in-flight handler tasks.

      Messages already queued for sending (e.g. $/cancel_request) are flushed first. A handler may close its own connection; no response is guaranteed for the request it is handling.

      Source code in src/aclientp/connection.py
      async def close(self) -> None:
          """Stop the receive loop and cancel any in-flight handler tasks.
      
          Messages already queued for sending (e.g. ``$/cancel_request``) are flushed
          first. A handler may close its own connection; no response is guaranteed
          for the request it is handling.
          """
          if self._closed:
              return
          self._closed = True
          await self._dispatcher.stop()
          await self._sender.close()
          await self._tasks.shutdown()
          self._disconnect(ConnectionError("Connection closed"))
      

      request_object async

      request_object(method: str, params: JsonValue | None = None) -> dict[str, Any]
      

      Send a request whose result is a JSON object, as for all ACP methods.

      A null result is treated as an empty object.

      Raises:

      Type Description
      RequestError

      The peer answered with an error or a non-object result.

      ConnectionError

      The connection is closed or got closed before a response.

      Source code in src/aclientp/connection.py
      async def request_object(self, method: str, params: JsonValue | None = None) -> dict[str, Any]:
          """Send a request whose result is a JSON object, as for all ACP methods.
      
          A ``null`` result is treated as an empty object.
      
          Raises:
              RequestError: The peer answered with an error or a non-object result.
              ConnectionError: The connection is closed or got closed before a response.
          """
          match await self.send_request(method, params):
              case None:
                  return {}
              case dict() as result:
                  return result
              case other:
                  details = f"Expected an object result for {method}, got {type(other).__name__}"
                  raise RequestError.internal_error({"details": details})
      

      send_notification async

      send_notification(method: str, params: JsonValue | None = None) -> None
      

      Send a notification.

      Raises:

      Type Description
      ConnectionError

      The connection is closed.

      Source code in src/aclientp/connection.py
      async def send_notification(self, method: str, params: JsonValue | None = None) -> None:
          """Send a notification.
      
          Raises:
              ConnectionError: The connection is closed.
          """
          self._raise_if_closed()
          payload = {"jsonrpc": "2.0", "method": method, "params": params}
          await self._sender.send(payload)
          self._notify_observers("outgoing", payload)
      

      send_request async

      send_request(method: str, params: JsonValue | None = None) -> Any
      

      Send a request and wait for the peer's response.

      Returns (or raises the peer's error) once the notifications received before the response have been handled (see the class docs). Cancelling the caller sends $/cancel_request for a request still in flight.

      Raises:

      Type Description
      RequestError

      The peer answered with an error.

      ConnectionError

      The connection is closed or got closed before a response.

      Source code in src/aclientp/connection.py
      async def send_request(self, method: str, params: JsonValue | None = None) -> Any:
          """Send a request and wait for the peer's response.
      
          Returns (or raises the peer's error) once the notifications received before the
          response have been handled (see the class docs). Cancelling the caller sends
          ``$/cancel_request`` for a request still in flight.
      
          Raises:
              RequestError: The peer answered with an error.
              ConnectionError: The connection is closed or got closed before a response.
          """
          self._raise_if_closed()
          request_id = self._next_request_id
          self._next_request_id += 1
          future = self._state.register_outgoing(request_id, method)
          payload = {"jsonrpc": "2.0", "id": request_id, "method": method, "params": params}
          self._awaiting.add(request_id)
          try:
              await self._sender.send(payload)
              self._notify_observers("outgoing", payload)
              try:
                  return await future
              finally:  # Answered (result or error): first what the peer sent before
                  self._awaiting.discard(request_id)
                  if (before := self._handled_before.pop(request_id, None)) is not None:
                      await self._wait_handled(before)
          except BaseException as exc:
              answered = future.done() and not future.cancelled()
              future.cancel()  # Nobody waits for it anymore
              if not answered:
                  self._state.reject_outgoing(request_id, RequestError.request_cancelled())
                  # A request cancelled while queued for sending still gets written.
                  if isinstance(exc, asyncio.CancelledError):
                      self._send_cancel_request(request_id)
              raise
          finally:
              self._awaiting.discard(request_id)
              self._handled_before.pop(request_id, None)
      

      wait_closed async

      wait_closed() -> None
      

      Wait until the peer disconnected (EOF) or the connection was closed.

      Source code in src/aclientp/connection.py
      async def wait_closed(self) -> None:
          """Wait until the peer disconnected (EOF) or the connection was closed."""
          await self._disconnected.wait()
      

      wait_for_notifications async

      wait_for_notifications(select: NotificationFilter | None = None) -> None
      

      Wait until the notifications received so far have been handled.

      Requests already wait for the notifications received before their response (see the class docs); this waits for all received so far, e.g. before acting on a notification. Notifications received later, and the one handled by the calling task, are not waited for.

      Parameters:

      Name Type Description Default
      select NotificationFilter | None

      Only wait for notifications matching this (method, params) filter.

      None
      Source code in src/aclientp/connection.py
      async def wait_for_notifications(self, select: NotificationFilter | None = None) -> None:
          """Wait until the notifications received so far have been handled.
      
          Requests already wait for the notifications received before their response
          (see the class docs); this waits for all received so far, e.g. before acting on
          a notification. Notifications received later, and the one handled by the
          calling task, are not waited for.
      
          Args:
              select: Only wait for notifications matching this ``(method, params)`` filter.
          """
          pending = {
              done
              for done, (method, params) in self._pending_notifications.items()
              if select is None or select(method, params)
          }
          await self._wait_handled(pending)
      

      StreamEvent dataclass

      Stream event.

      Source code in src/aclientp/connection.py
      @dataclass(frozen=True, slots=True)
      class StreamEvent:
          """Stream event."""
      
          direction: StreamDirection
          message: dict[str, Any]
      

      is_request_id

      is_request_id(value: Any) -> TypeIs[RequestId]
      

      Whether value is a JSON-RPC request id usable for correlation.

      bool is excluded explicitly: Python would otherwise alias true with 1.

      Source code in src/aclientp/connection.py
      def is_request_id(value: Any) -> TypeIs[RequestId]:
          """Whether ``value`` is a JSON-RPC request id usable for correlation.
      
          ``bool`` is excluded explicitly: Python would otherwise alias ``true`` with ``1``.
          """
          return isinstance(value, str) or (isinstance(value, int) and not isinstance(value, bool))