Skip to main content

pi-client

Transport-neutral client for remote pi sessions. PiClient exchanges length-prefixed CBOR messages through a small ByteTransport protocol. The core package has no platform-specific imports; pi_client.unix provides the Unix-domain-socket transport.

from pi_client import ByteTransportHandlers, PiClient, PiClientOptions


class MyTransport:
    async def send(self, chunk: bytes) -> None:
        # Deliver chunks in invocation order and honor backpressure.
        return None

    def close(self) -> None:
        return None


async def transport_factory(handlers: ByteTransportHandlers) -> MyTransport:
    # Connect using WebSocket, Unix socket, or another ordered byte transport.
    # Call handlers.on_data(), handlers.on_close(), or handlers.on_error() for inbound activity.
    return MyTransport()


async def main() -> None:
    client = PiClient(PiClientOptions(transport_factory=transport_factory))
    await client.connect()
    session = await client.create_session(cwd="/workspace")
    unsubscribe = session.subscribe(lambda snapshot: print(snapshot))
    await session.prompt("Inspect this project")
    unsubscribe()
    await session.dispose()
    await client.dispose()

Call handlers.on_data(chunk) for inbound bytes, handlers.on_close() for an orderly terminal close, and handlers.on_error(error) for transport failures. A factory must create a fresh transport for every connection attempt and complete any transport-specific authentication before resolving. For example, a WebSocket factory can provide credentials in its upgrade request.

PiClient does not reconnect automatically. Call reconnect() after disconnection. One connection can attach several sessions. Requests are correlated by ID. Server snapshots and successful response snapshots are authoritative, while progress events do not mutate snapshot state optimistically. Read cached session metadata from client.snapshot["sessions"] when a snapshot exists; call list_sessions() to request refreshed durable metadata from the server. Runtime state is available after acquiring a session.

acquire_session() returns an independent SessionHandle; handles cannot be constructed directly for active protocol use. Use mode="exclusive" for a lifecycle or mutation coordinator and mode="shared" when multiple low-level consumers intentionally share the session. Exclusive acquisition fails with PiSessionOwnershipError while any lease exists, and shared acquisition fails while an exclusive lease exists. attach_session() is a shared-acquisition convenience method. create_session() returns an exclusive handle for the newly created session.

Calling dispose() or detach() releases only that handle. A handle rejects commands as soon as release begins. The client sends the protocol detach request after the final handle is released. If explicit detach() fails, the handle becomes active again for retry. If cleanup-oriented dispose() fails, it reports the protocol error but relinquishes local ownership; PiClient reconciles the failed protocol cleanup before the next acquisition. A released handle becomes unavailable without affecting other shared handles. Server removal or disconnection invalidates every handle for the affected attachment, and disposing an invalidated handle is a no-op. Commands fail with PiDisconnectedError while the client is disconnected and PiSessionDetachedError when the client is connected but a handle is releasing, released, or invalidated. SessionHandle supports async with.

subscribe() observes authoritative snapshots. on_event() observes protocol events. Both return an unsubscribe function. Structured errors returned by the server are exposed as PiServerError.

Limits and security

PiClientOptions.max_frame_length bounds inbound and outbound CBOR payloads. Configure matching limits on the client and server. Transports should separately bound queued outbound bytes and preserve send order.

Treat peers as untrusted. Use a secure transport with appropriate access controls and authenticate during transport establishment.

Subscriber exceptions are isolated from protocol state. Set on_listener_error in PiClientOptions to report them to application logging or diagnostics.

Unix-domain sockets

Consumers can use the separately exported Unix-domain socket transport:

from pi_client import PiClient, PiClientOptions, create_unix_transport_factory


async def main() -> None:
    client = PiClient(
        PiClientOptions(
            transport_factory=create_unix_transport_factory(
                ".scratch/pi.sock",
                max_pending_bytes=64 * 1024 * 1024,
            ),
        )
    )

    await client.connect()

max_pending_bytes bounds queued outbound data. It defaults to four times the protocol frame limit. The transport preserves send order and waits for socket backpressure before resolving each send.

The pi_client root remains transport-neutral except for re-exporting the Unix factory for convenience. The implementation also lives at the explicit pi_client.unix module path.

Development

From the repository root:

uv sync --all-packages
uv run pytest packages/pi-client
uv run ruff check packages/pi-client

pp-rpc-client is developed in HSPK/pp_rpc_client. It was split out of the pp monorepo; sibling packages (pp-ai, pp-agent-core, pp-tui, pp-coding-agent, ...) each live in their own repository and are consumed from PyPI.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pp_rpc_client-0.2.1.tar.gz (28.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pp_rpc_client-0.2.1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file pp_rpc_client-0.2.1.tar.gz.

File metadata

  • Download URL: pp_rpc_client-0.2.1.tar.gz
  • Upload date:
  • Size: 28.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pp_rpc_client-0.2.1.tar.gz
Algorithm Hash digest
SHA256 cdde8da3dceb5c868a1052d1a5cd6eb126c64cf5fbc2c2867d0b9520c13bec26
MD5 fb6fc54877f8e1757930a90762321e8b
BLAKE2b-256 eb654ca1f3786e2b4f1a73c90a239274dd5d0b717817d157d732aa24ebbe53bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pp_rpc_client-0.2.1.tar.gz:

Publisher: release.yml on HSPK/pp_rpc_client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pp_rpc_client-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pp_rpc_client-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pp_rpc_client-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9d98cb60a0dfc3fe16ec48cc29315f5eaacd010ef00a50c1d5099bab2e482b69
MD5 cf0e4bb5c899a4f834709ca5c5ca2e14
BLAKE2b-256 62290179429d3463c85287d051f6e5259570c6049aef891bff601f398bf99f31

See more details on using hashes here.

Provenance

The following attestation bundles were made for pp_rpc_client-0.2.1-py3-none-any.whl:

Publisher: release.yml on HSPK/pp_rpc_client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page