Skip to content

API Reference parrot.tools.repl_worker.observer

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

parrot.tools.repl_worker.observer

View source

Host-side process observer for REPL workers (FEAT-521 Module 2).

ProcessObserver is the continuous, cheap observation primitive the rest of FEAT-521 builds on: one instance per live :class:~parrot.tools. repl_worker.handle.WorkerHandle, sampling its child process (CPU time, RSS, status, thread count, and on Linux the kernel wait channel) on a fixed interval from spawn until kill, and turning that sample stream into a first-class busy/hung verdict (spec G1/G2) plus soft/hard RSS guardrails (spec G4).

Observation is strictly host-side: this module never reads or writes the control pipe (spec Non-Goals — no heartbeat frames), so no protocol reordering is possible. A :class:ProcessObserver never raises out of :meth:ProcessObserver.run — a dead or inaccessible process, or an unsupported (non-POSIX) host, degrades the verdict to "unavailable" instead of propagating an exception (spec G1).

The /proc-parsing helpers in this module are also used by :func:parrot.tools.repl_worker.handle.probe_process_state, which now delegates to them and keeps its own signature/return-value contract as a thin compatibility wrapper (spec §3 Module 2).

Alias MemoryVerdict

Re-exported from parrot.tools.repl_worker.protocol.MemoryVerdict.

Class ProcessObserver

Continuously samples one worker process and derives a busy/hung verdict.

Owned by a single :class:~parrot.tools.repl_worker.handle.WorkerHandle, started as a background task alongside its stdio drain task and torn down with it. See spec §2 "Architectural Design" for the full verdict table and the soft/hard RSS guardrail semantics.

Parameter Type Default Description
pid int The worker process id to sample.
config WorkerConfig The owning worker's :class:WorkerConfig (poll cadence, stall window, and RSS thresholds are read from here).
on_hard_breach Callable[[MemoryVerdict], Awaitable[None]] Awaited exactly once, with the measured :class:MemoryVerdict, the first time a sample crosses config.memory_hard_limit_bytes. After it is awaited, :meth:run returns.

Attributes:

Attribute Type Description
pid The observed process id.
memory_pressure Optional[tuple[int, int]] (rss, soft_limit) while over memory_soft_limit_bytes, else None.
memory_verdict Optional[MemoryVerdict] The recorded hard-breach verdict, or None if none has occurred.

Method ProcessObserver.run

Sample the process on observer_poll_ms until it dies or is unusable.

Never raises: a non-POSIX host, a psutil failure resolving the process, or psutil.NoSuchProcess/AccessDenied mid-loop all end the loop (setting the verdict to "unavailable" where appropriate) instead of propagating. Returns immediately after the one-shot hard-breach callback has been awaited (spec: "loop stops").

run() -> None

Method ProcessObserver.mark_busy

Record that a request round-trip started (called around _roundtrip()).

mark_busy() -> None

Method ProcessObserver.mark_idle

Record that the in-flight request round-trip finished (or none is pending).

mark_idle() -> None

Method ProcessObserver.verdict

Derive the current busy/hung verdict from the sample ring.

verdict() -> Verdict

Returns: Verdict — One of "booting", "settled", "computing", "stalled", or "unavailable" (see spec §2 table).

Method ProcessObserver.last

The most recent sample, or None if none has been taken yet.

last() -> Optional[ProcessSample]

Returns: Optional[ProcessSample]

Method ProcessObserver.cpu_progress

CPU seconds advanced over the trailing window_s window.

cpu_progress(window_s: float) -> float

Parameter Type Default Description
window_s float Length of the trailing window, in seconds.

Returns: float — last.cpu_s - baseline.cpu_s where baseline is the oldest sample within the window (or the oldest sample in the ring, if the ring doesn't span the full window yet). 0.0 when fewer than two samples are available.

Method ProcessObserver.describe

One-line description of the current verdict and last sample.

Used to enrich every timeout/bootstrap/namespace-loss error message (spec G2/G3 "no blank errors").

describe() -> str

Returns: str — E.g. "verdict=stalled cpu=58.10s rss=1996488704 state=S wchan=pipe_read threads=4", or "verdict=booting (no samples yet)" before the first sample.

Alias ProcessSample

Re-exported from parrot.tools.repl_worker.protocol.ProcessSample.

Alias Verdict

Re-exported from parrot.tools.repl_worker.protocol.Verdict.

Alias WorkerConfig

Re-exported from parrot.tools.repl_worker.protocol.WorkerConfig.

Attribute logger

Function read_proc_cpu_seconds

Read cumulative user+system CPU seconds from /proc/<pid>/stat.

read_proc_cpu_seconds(pid: int) -> float

Parameter Type Default Description
pid int The process to inspect.

Returns: float — User + system CPU time, in seconds.

Raises:

  • OSError — the stat file could not be read.
  • ValueError — the stat file's contents could not be parsed as integers.
  • IndexError — the stat file did not contain the expected fields.

Function read_proc_status

Parse /proc/<pid>/status into a {field: value} mapping.

read_proc_status(pid: int) -> dict[str, str]

Parameter Type Default Description
pid int The process to inspect.

Returns: dict[str, str] — A mapping of status field names (e.g. "State", "Threads", "VmPeak") to their raw string values.

Raises:

  • OSError — the status file could not be read (process gone, not Linux, permission denied, ...).

Function read_proc_wchan

Read /proc/<pid>/wchan (Linux only).

read_proc_wchan(pid: int) -> str

Parameter Type Default Description
pid int The process to inspect.

Returns: str — The kernel wait channel name, "0" when the process is not blocked on one, or "" when unavailable (non-Linux host or a read failure).

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