calute.operators.pty#
Persistent PTY session management for operator tooling.
Provides PTYSessionManager, which creates and manages
pseudo-terminal (PTY) backed subprocess sessions. Each session is
identified by a unique session_id and can be written to, read from,
interrupted, or closed independently.
- class calute.operators.pty.PTYSession(session_id: str, process: Popen[str], master_fd: int, command: str, workdir: str)[source]#
Bases:
objectLive PTY-backed subprocess session.
- session_id#
Unique identifier for this session, generated by
PTYSessionManager.- Type
str
- process#
The underlying
subprocess.Popenobject that owns the child process.- Type
subprocess.Popen[str]
- master_fd#
File descriptor for the master side of the PTY pair. Used for reading output and writing input.
- Type
int
- command#
The original shell command that was launched.
- Type
str
- workdir#
Absolute path of the working directory the session was started in.
- Type
str
- command: str#
- master_fd: int#
- process: Popen[str]#
- session_id: str#
- workdir: str#
- class calute.operators.pty.PTYSessionManager[source]#
Bases:
objectManage persistent PTY-backed shell sessions.
Maintains a registry of active
PTYSessioninstances keyed bysession_id. Sessions can be created, written to, listed, and closed.- _sessions#
Internal mapping from
session_idto the correspondingPTYSession.
- close(session_id: str) dict[str, Any][source]#
Terminate and forget a PTY session.
Attempts a graceful
SIGTERMfirst, escalating toSIGKILLif the process does not exit within two seconds. The master file descriptor is closed and the session is removed from the registry.- Parameters
session_id – Identifier of the PTY session to close.
- Returns
session_id: The session identifier.closed: AlwaysTrue.exit_code: Final process exit code.
- Return type
A dictionary containing
- Raises
ValueError – If the
session_idis not recognised.
- create_session(cmd: str, *, workdir: str | None = None, env: dict[str, str] | None = None, login: bool = True, yield_time_ms: int = 1000, max_output_chars: int = 4000) dict[str, Any][source]#
Start a PTY session and return its initial output.
Creates a new pseudo-terminal pair, spawns a shell process with the given command, and captures initial output up to the specified limits.
- Parameters
cmd – Shell command to execute inside the PTY.
workdir – Working directory for the new session. When
None, the current process working directory is used.env – Additional environment variables merged on top of the current process environment.
login – When
True, the shell is started with login semantics (-lflag) for bash and zsh so that profile files are sourced.yield_time_ms – Maximum time in milliseconds to wait for initial output before returning.
max_output_chars – Maximum number of characters to capture from the initial output.
- Returns
session_id: Unique identifier for the new session.command: The shell command that was launched.workdir: Resolved working directory path.stdout: Initial output captured from the PTY.running:Trueif the process is still alive.exit_code: Process exit code, orNoneif still running.
- Return type
A dictionary containing
- list_sessions() list[dict[str, Any]][source]#
Return summaries for current PTY sessions.
- Returns
A list of dictionaries, each containing the
session_id, originalcommand,workdir,runningstatus, andexit_codeof a tracked session.
- write(session_id: str, *, chars: str = '', close_stdin: bool = False, interrupt: bool = False, yield_time_ms: int = 1000, max_output_chars: int = 4000) dict[str, Any][source]#
Write to a PTY session and read back incremental output.
Sends characters, an EOF signal, or an interrupt to the running process and then collects whatever output the process produces within the specified time window.
- Parameters
session_id – Identifier of the target PTY session.
chars – Text to write to the process stdin. Include
\nto simulate pressing Enter.close_stdin – When
True, send an EOF (Ctrl-D) to the process after writing any provided characters.interrupt – When
True, sendSIGINTto the process group before reading output.yield_time_ms – Maximum time in milliseconds to wait for output after sending input.
max_output_chars – Maximum number of output characters to collect.
- Returns
session_id: The session identifier.stdout: Incremental output captured since the write.running:Trueif the process is still alive.exit_code: Process exit code, orNoneif still running.
- Return type
A dictionary containing
- Raises
ValueError – If the
session_idis not recognised.