Skip to content

API Reference parrot.tools.repl_worker.transport

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

parrot.tools.repl_worker.transport

View source

DataFrame transport: Arrow IPC over shared memory (FEAT-380 Module 7 / G9).

DataFrames cross the host -> worker boundary without an expensive copy: Arrow IPC over a multiprocessing.shared_memory block is the primary path; pickle+base64 is the fallback only for dtypes Arrow cannot represent, and that fallback always logs a warning (mandatory, not optional — spec G9: "pickle solo como fallback con warning").

Shared-memory ownership (documented, per the task's Key Constraint — "leaked shm segments fail CI"):

  • The host creates the block, writes the Arrow IPC stream, and sends its name (never the worker).
  • The worker opens the block, decodes it, and close()s its handle — it never unlink()s (see worker.py's dispatch).
  • The host unlink()s the block only after receiving the worker's ACK (the framed response to the inject_df request) — see handle.py::WorkerHandle.inject_dataframe — guaranteeing the worker has already finished reading before the block is freed.

Zero-copy applies only where Arrow/pandas dtype conversion is compatible (e.g. plain numeric/string columns); object columns holding non-Arrow types still copy during to_pandas()/from_pandas(). This module never claims otherwise in logs or docs.

Attribute DEFAULT_STRICT_MAX_BYTES

Type: int

Class EncodedDataFrame

Wire-ready encoding of one DataFrame, produced by :func:encode_dataframe.

Attributes:

Attribute Type Description
format str
shm_name Optional[str]
size Optional[int]
payload Optional[str]

Class StrictTransportError

A value cannot cross the worker boundary under strict evidence rules.

Raised before any pickle payload is constructed (FEAT-538 §2 "REPL Recovery"). Task-memory evidence must never travel as pickle: the point of a strict load is that what arrives in the worker is provably the artifact that was fingerprinted, and an arbitrary pickle payload proves nothing about its own contents.

This is deliberately not a subclass of the Arrow errors: a caller catching Arrow-conversion failures to fall back to pickle must not accidentally swallow a strict refusal.

Function decode_dataframe_from_shm

Worker side: read a DataFrame back from an Arrow IPC shared-memory block.

decode_dataframe_from_shm(shm_name: str, size: int) -> pd.DataFrame

Parameter Type Default Description
shm_name str Name of the shared-memory block the host created.
size int Exact byte length of the IPC stream within the block.

Returns: pd.DataFrame — The decoded DataFrame.

Function decode_pickle_payload

Worker side: decode the pickle fallback payload.

decode_pickle_payload(payload: str) -> pd.DataFrame

Parameter Type Default Description
payload str Base64-encoded pickle bytes produced by :func:encode_dataframe.

Returns: pd.DataFrame — The decoded DataFrame.

Function encode_dataframe

Serialize df for host -> worker transport.

Tries Arrow IPC into a brand-new SharedMemory block first; falls back to pickle+base64 (logged) when pyarrow cannot represent a column's dtype.

encode_dataframe(df: pd.DataFrame, name: str, *, strict: bool = False, max_bytes: Optional[int] = None) -> EncodedDataFrame

Parameter Type Default Description
df pd.DataFrame The DataFrame to encode.
name str Variable name it will be bound to — used only for the fallback warning message.
strict bool False Opt-in strict evidence mode (FEAT-538). When True, an Arrow conversion failure raises :class:StrictTransportError before any pickle payload is constructed, and the encoded size is checked against max_bytes. Defaults to False, which preserves the legacy behaviour byte for byte.
max_bytes Optional[int] None Ceiling for the encoded Arrow stream. Only consulted in strict mode; None uses :data:DEFAULT_STRICT_MAX_BYTES.

Returns: EncodedDataFrame — class:EncodedDataFrame describing what to put on the wire. The caller (WorkerHandle.inject_dataframe) owns unlinking the shm block once the worker has ack'd (see module docstring).

Raises:

  • StrictTransportError — In strict mode only — when Arrow cannot represent a column, or the encoded stream exceeds max_bytes. No shared-memory block is left behind in either case.

Function ensure_strict_json

Validate that value is safe JSON evidence, or refuse it.

Strict evidence values travel as plain JSON, never as pickle. Only None/bool/int/float/str and list/dict composed of them are permitted. tuple is refused even though it looks JSON-safe: it round-trips as a list, so accepting one would mean the value that arrives is not the value that was sent — and for evidence, that is the whole question.

ensure_strict_json(value: Any, *, name: str, max_bytes: Optional[int] = None, _depth: int = 0) -> Any

Parameter Type Default Description
value Any The value to validate.
name str Variable name, for the error message.
max_bytes Optional[int] None Ceiling for the canonical encoding. None uses :data:DEFAULT_STRICT_MAX_BYTES.
_depth int 0 Internal recursion depth guard.

Returns: Any — value unchanged, when it is acceptable.

Raises:

  • StrictTransportError — If the value contains an unsupported type, a non-string mapping key, exceeds the depth guard, or exceeds max_bytes once encoded.

Attribute logger

Function unlink_shm

Host side: free a shared-memory block after the worker has ack'd.

Best-effort — a block that's already gone (e.g. a retried cleanup) is not an error.

unlink_shm(shm_name: str) -> None

Parameter Type Default Description
shm_name str Name of the block to free.

Function validate_strict_dataframe

Check that df would be acceptable strict evidence, without sending it.

Used by the in-process adapter, which binds a DataFrame by plain assignment and so never serializes anything. Running the identical check there is what stops the in-process mode from being silently more permissive than the subprocess one — a frame a real worker would refuse as unverifiable evidence must not quietly succeed just because no process boundary happened to be involved.

validate_strict_dataframe(df: pd.DataFrame, name: str, *, max_bytes: Optional[int] = None) -> None

Parameter Type Default Description
df pd.DataFrame The DataFrame to validate.
name str Variable name, for the error message.
max_bytes Optional[int] None Ceiling for the encoded Arrow stream. None uses :data:DEFAULT_STRICT_MAX_BYTES.

Raises:

  • StrictTransportError — If Arrow cannot represent the frame, or the encoded stream would exceed max_bytes.

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