Skip to content

API Reference parrot.tools.repl_worker.inprocess

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

parrot.tools.repl_worker.inprocess

View source

In-process execution handle — the pre-FEAT-380 REPL behaviour behind the WorkerHandle surface (escape hatch, opt-in).

PythonREPLTool(execution_mode="inprocess") (or the PYTHON_REPL_EXECUTION_MODE=inprocess environment variable) makes the tool hand out an :class:InProcessHandle instead of a WorkerHandle: generated code runs on the tool's own self.locals/self.globals inside the host process, on the tool's dedicated _repl_executor thread — exactly what the tool did before the worker-process sandbox (FEAT-380 Module 5) shipped.

What you give up (this is why it is opt-in and logged as a WARNING):

  • no process isolation: a crash (segfault, os._exit) takes the host down;
  • no RLIMIT_AS / RLIMIT_CPU / RLIMIT_NOFILE on the generated code;
  • no SIGKILL on deadline_ms — a runaway snippet keeps its executor thread busy until it finishes (the caller still gets a bounded error, and the handle stays busy until then, see :meth:InProcessHandle.execute);
  • _execute_code captures output with contextlib.redirect_stdout, which swaps the process-global sys.stdout for the duration of the snippet: anything else the host prints to stdout meanwhile (another tool's output, a print-based log) can land in this snippet's result. This is the pre-FEAT-380 behaviour and one more reason the worker mode is the default (code-review finding, accepted as a documented limitation).

What you keep: the allowlist + AST denylist gate (_execute_code(..., enforce_security=True) runs unchanged), the namespace API, DataFrame seeding, and the {status, result, error} return contract — so every caller written against WorkerHandle (PythonPandasTool seeding/audit, the namespace API, WorkingMemoryToolkit wiring) works without branching.

Intended use: a deployment-level kill switch while the worker-pool path is being battle-tested, and environments where spawning a child interpreter is not possible. It is NOT an automatic fallback — the tool never silently downgrades from worker to inprocess.

Class InProcessHandle

WorkerHandle-compatible handle that executes on the host tool instance.

One handle per PythonREPLTool instance (the same isolation unit the worker path uses — one worker per instance). Requests are serialized by an asyncio.Lock like the worker's control pipe, so two concurrent execute() calls on the same tool never interleave on self.locals.

Parameter Type Default Description
tool 'PythonREPLTool' The host PythonREPLTool whose _execute_code / locals / globals back this handle.
executor concurrent.futures.Executor Executor the blocking _execute_code call runs on — PythonREPLTool._repl_executor (FEAT-380 Module 1, AC1), never the loop's shared default executor.
deadline_ms int Budget for one execute() call, mirroring WorkerConfig.deadline_ms. Expiry returns the G5 error dict but cannot stop the thread (see :meth:execute).

Attributes:

Attribute Type Description
known_vars list[str]
is_alive bool True until :meth:kill is called.
is_ready bool An in-process handle is ready the moment it exists.
generation str Stable identity of this in-process generation (FEAT-538). Mirrors :attr:WorkerHandle.generation so a REPL binding is validated the same way in both modes.

Method InProcessHandle.start

No-op: nothing to spawn.

start() -> None

Method InProcessHandle.wait_ready

No-op: there is no bootstrap to wait for.

wait_ready(timeout_s: float | None = None) -> None

Parameter Type Default Description
timeout_s float | None None

Method InProcessHandle.ping

Health check — alive means healthy here.

ping(timeout_s: float = 10.0) -> bool

Parameter Type Default Description
timeout_s float 10.0

Returns: bool

Method InProcessHandle.death_summary

No process, so no exit code and no stderr tail.

death_summary() -> tuple[int | None, str]

Returns: tuple[int | None, str]

Method InProcessHandle.verdict

Always "unavailable" — there is no child process to observe (FEAT-521).

execution_mode="inprocess" runs on the host's own thread pool (no ProcessObserver, no idle/busy/hung classification, no memory guardrails — see the module docstring's "What you give up"). Exposing the same verdict() shape as WorkerHandle.observer.verdict() lets callers branch uniformly instead of special-casing this handle type.

verdict() -> str

Returns: str — The literal string "unavailable".

Method InProcessHandle.kill

Mark the handle dead. The host namespace is left untouched.

kill() -> None

Method InProcessHandle.execute

Run code on the host tool's namespace within deadline_ms.

execute(code: str, debug: bool = False) -> str | dict

Parameter Type Default Description
code str Python source (already host-gated by PythonREPLTool; the gate runs again inside _execute_code as it does in the worker).
debug bool False Mirrors PythonREPLTool's flag.

Returns: str | dict — The output string on success, or the {status, result, error} dict WorkerHandle.execute() returns on error/timeout. On a deadline breach the namespace is NOT lost (no process died) but the snippet is still running on its executor thread — the error text says so, because unlike the worker path nothing can SIGKILL it.

Method InProcessHandle.get_var

Read one namespace variable.

get_var(name: str) -> Any

Parameter Type Default Description
name str

Returns: Any

Method InProcessHandle.set_var

Write one namespace variable (mirrored into locals and globals).

set_var(name: str, value: Any) -> None

Parameter Type Default Description
name str
value Any

Method InProcessHandle.inject_dataframe

Bind a DataFrame — a plain assignment, no Arrow/shm hop needed.

In strict evidence mode the same dtype check runs even though no serialization is required. Skipping it here would make the in-process adapter silently more permissive than the subprocess one: a frame that a real worker would refuse as unverifiable evidence would quietly succeed, and the two modes would disagree about what counts as evidence.

inject_dataframe(name: str, df: Any, *, strict: bool = False, envelope: Any = None, max_bytes: int | None = None) -> None

Parameter Type Default Description
name str Variable name to bind the DataFrame to.
df Any The pandas.DataFrame to bind.
strict bool False FEAT-538 strict evidence mode.
envelope Any None Bounded runtime context. Accepted for signature parity with :class:WorkerHandle; nothing crosses a process boundary here, so there is nothing to revalidate.
max_bytes int | None None Strict-mode ceiling for the encoded size.

Raises:

  • StrictTransportError — In strict mode, when Arrow could not represent the frame or it exceeds max_bytes.

Method InProcessHandle.list_vars

Public (non-underscore) names in the namespace.

list_vars() -> list[str]

Returns: list[str]

Method InProcessHandle.snapshot

Serializable dump of the namespace (modules/callables skipped).

snapshot() -> dict[str, Any]

Returns: dict[str, Any]

Method InProcessHandle.reset

Reset the REPL environment — this handle retires, like a killed worker.

reset_environment() flags the pending reset; the tool's next _get_worker_handle() rebuilds the namespace and hands out a fresh handle, exactly as the worker path replaces the process.

reset() -> None

Alias PythonREPLTool

Re-exported from parrot.tools.pythonrepl.PythonREPLTool.

Attribute logger

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