Skip to content

API Reference parrot.e2e.control

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.e2e.control

View source

Private supervisor control and stdio RPC channel (FEAT-581, M3).

Implements the spec §2 "Process Lifecycle and Worktree Isolation" contract:

For stdio, a private Unix control socket mediates request/response
exchange; requests are serialized and checked for matching JSON-RPC
IDs. Never expose raw stdio through a PID file.

and the frozen wire shape from the TASK-3517 research spike (sdd/state/FEAT-581/research/lifecycle.md §3.2(a)/§5 item 8): a newline-delimited JSON-RPC 2.0 envelope over a Unix domain socket (mode 0600, in a mode-0700 run directory — see :mod:parrot.e2e.state), every request/response correlated by its "id".

This module owns exactly the channel, not what travels through it:

  • :class:ControlServer accepts connections for one run and dispatches an incoming method to a caller-supplied handler mapping — it has no knowledge of what "start"/"status"/"stop"/"stdio" do. The concrete :class:parrot.e2e.supervisor.E2ESupervisor (a separate, not-yet-landed M3 task) wires its own operations into that mapping.
  • :class:ControlClient sends one request and returns its result.

Every request is checked, most-definitive-first, before any handler ever runs: malformed envelope shape, then a stale run_id, then a foreign owner_id — a duck-typed or partially-populated request can never reach a handler by accident. Every read (server and client alike) goes through :func:asyncio.wait_for; the legacy tests/mcp/test_mcp_local_e2e.py _recv helper's blocking readline() (flagged as a real gap by the TASK-3517 spike, §6 item 3) is deliberately not reproduced here.

This module never re-derives access to a child's stdio from its PID or a PID file — mediating a "stdio" operation means forwarding an already-open pipe a handler itself retains; this module holds no process handles of its own. Nor does it ever place a secret on a CLI argv: a :class:ControlClient is constructed from a socket path, run ID and owner ID, the same values the spec's M5 pytest bridge says are "supplied in env by the runner" (§3 "M5"), never encoded into a spawned command line.

Class ControlChannelError

Malformed control-channel request/response, foreign owner or stale run ID.

Subclasses :class:parrot.e2e.errors.E2EConfigError (exit code 2, spec §2) — every failure this module raises happens before any target protocol logic runs, the same class of error that exit code already covers. This module defines no exit-code mapping of its own.

Parameter Type Default Description
message str Human-readable description of the failure.
reason_code Optional[str] None Machine-readable reason code (also carried in the JSON-RPC error envelope's error.data.reason_code).
code int _ERR_HANDLER_FAILURE The JSON-RPC error code for this failure.

Attributes:

Attribute Type Description
code The JSON-RPC error code (reserved range or this module's own implementation-defined range) recorded in an error envelope's error.code.

Class ControlClient

Client for one run's private control channel (spec §2, frozen wire shape).

Every :meth:request opens one connection, sends one JSON-RPC request, waits for its correlated response and closes — serialized exchange, per spec §2 ("requests are serialized"). The connection's writer is always closed on return, on a server-reported error, and on cancellation.

Parameter Type Default Description
socket_path Path Path to the run's private Unix control socket (normally supplied via an environment variable, per spec §3 "M5", never a CLI argv token).
run_id str This run's stable ID; every request is stamped with it so a stale client from a previous run is refused, not silently served.
owner_id str This caller's own identity; the server refuses a request whose owner_id disagrees with its own.
request_deadline_s float DEFAULT_REQUEST_DEADLINE_S Maximum seconds to wait for a connection, for the write to drain, and for one response.

Method ControlClient.request

Send one JSON-RPC request and return its result payload.

request(operation: str, payload: dict[str, JsonValue]) -> dict[str, JsonValue]

Parameter Type Default Description
operation str The control operation name (JSON-RPC method, e.g. "start"/"status"/"stop"/"stdio").
payload dict[str, JsonValue] Operation-specific parameters (JSON-RPC params).

Returns: dict[str, JsonValue] — The handler's result payload, as returned by the server.

Raises:

  • ControlChannelError — On a connection failure, a malformed or ID-mismatched response, a size or deadline violation, or a server-reported error (foreign owner, stale run, unknown operation, or the operation handler's own failure).

Attribute ControlHandler

Class ControlServer

Server for one run's private control channel (spec §2, frozen wire shape).

One instance mediates exactly one run's owner-bound requests over a private Unix socket. :meth:start binds the socket (mode 0600, immediately accepting connections in the background — no separate "serve forever" call is needed); :meth:stop closes it and removes the socket file, idempotently.

Every accepted request is checked, most-definitive-first, before its method is ever dispatched: envelope shape, then run_id (a stale client from a previous run), then owner_id (a foreign caller) — an unauthorized or malformed request never reaches a handler.

Parameter Type Default Description
socket_path Path Path this channel's Unix socket is bound at (a mode-0700 run directory is the caller's responsibility, see :func:parrot.e2e.state.run_dir).
run_id str This run's stable ID; requests naming a different one are refused as stale.
owner_id str This run's recorded owner; requests naming a different one are refused as foreign.
operations Mapping[str, ControlHandler] Mapping of operation name (JSON-RPC method) to an async handler. This server has no built-in operations of its own — an unrecognised method is always rejected.
request_deadline_s float DEFAULT_REQUEST_DEADLINE_S Maximum seconds allowed for one request's read and for its handler's own execution.

Attributes:

Attribute Type Description
socket_path Path The path this channel's Unix socket is (or will be) bound at.

Method ControlServer.start

Bind the private Unix socket (mode 0600) and begin accepting connections.

start() -> None

Raises:

  • ControlChannelError — If socket_path is a symlink (refused, never traversed/replaced through) or the socket cannot be bound.

Method ControlServer.stop

Stop accepting connections and remove the socket file.

Idempotent and safe to call from a cancellation/finally handler: calling it more than once, or before :meth:start, never raises.

stop() -> None

Attribute DEFAULT_REQUEST_DEADLINE_S

Attribute MAX_MESSAGE_BYTES

Attribute SOCKET_FILE_MODE

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally