MusePi

Natives Shell, PTY, Process, and Key Internals

This document covers execution/process/terminal primitives in @musepi/pi-natives: shell, pty, ps, and keys, using the architecture terms from docs/natives-architecture.md.

Implementation files

Layer ownership

Shell subsystem (shell)

API model

Shell execution modes:

  1. One-shot via executeShell(options, onChunk?).
  2. Persistent session via new Shell(options?) then shell.run(...) repeatedly.

Both stream merged stdout/stderr text through a threadsafe callback and return { exitCode?, cancelled, timedOut, minimized? }.

Related synchronous helper:

ShellOptions supports sessionEnv, snapshotPath, and optional output minimizer. ShellExecuteOptions supports command-scoped env, session-level sessionEnv, snapshotPath, timeout/signal, and optional minimizer. ShellRunOptions supports command, cwd, command-scoped env, timeout, and signal.

Session creation and environment model

Rust creates brush_core::Shell with:

Session env behavior:

Runtime lifecycle and state transitions

Persistent shell (Shell.run) uses this state machine:

One-shot shell (executeShell) always creates and drops a fresh session per call.

Streaming/output and minimizer behavior

Cancellation, timeout, and abort

Shell.abort() behavior:

Failure behavior

Common surfaced errors include:

PTY subsystem (pty)

API model

new PtySession() exposes:

PtyStartOptions supports command, optional cwd, optional env, timeoutMs, signal, cols, rows, and optional shell. The default shell is sh.

Runtime lifecycle and state transitions

PtySession state machine:

Concurrency guard:

Spawn/attach/write/read/terminate patterns

Output path:

Termination path:

Cancellation and timeout semantics

Failure behavior

Error surfaces include:

Control call failures when not running:

Process subsystem (ps)

API model

Current JS surface is the Process class:

ProcessTerminateOptions supports { group?, gracefulMs?, timeoutMs?, signal? }. ProcessWaitOptions supports { timeoutMs?, signal? }.

Behavior

The platform-specific implementation lives in pi_shell::process; crates/pi-natives/src/ps.rs is a N-API shim plus re-exports used by PTY termination.

Key parsing subsystem (keys)

API model

Exposed helpers:

Parsing model

The parser combines:

Modifier handling:

Layout behavior:

Failure behavior

JS API ↔ Rust export mapping

Shell + PTY + Process

JS API Rust N-API export Notes
executeShell(options, onChunk?) executeShell (execute_shell) One-shot shell execution
new Shell(options?) Shell class Persistent shell session
shell.run(options, onChunk?) Shell::run Reuses session on keepalive control flow
shell.abort() Shell::abort Aborts active run for that shell instance
applyBashFixups(command) applyBashFixups (apply_bash_fixups) Synchronous command rewrite helper
new PtySession() PtySession class Stateful PTY session
pty.start(options, onChunk?) PtySession::start Interactive PTY run
pty.write(data) PtySession::write Raw stdin passthrough
pty.resize(cols, rows) PtySession::resize Clamped terminal dimensions
pty.kill() PtySession::kill Terminates active PTY child/targets
Process.fromPid(pid) Process::from_pid Stable process reference lookup
Process.fromPath(path) Process::from_path Executable-path process lookup
process.killTree(signal?) Process::kill_tree Children-first process tree termination
process.terminate(options?) Process::terminate Graceful then hard process termination
process.waitForExit(options?) Process::wait_for_exit Async exit wait
process.children() Process::children Direct children as Process[]
process.status() Process::status running / exited

Keys

JS API Rust N-API export Notes
matchesKittySequence(data, cp, mod) matchesKittySequence (matches_kitty_sequence) Kitty codepoint+modifier match
parseKey(data, kittyProtocolActive) parseKey (parse_key) Normalized key-id parser
matchesLegacySequence(data, keyName) matchesLegacySequence (matches_legacy_sequence) Exact legacy sequence map check
parseKittySequence(data) parseKittySequence (parse_kitty_sequence) Structured Kitty parse result
matchesKey(data, keyId, kittyProtocolActive) matchesKey (matches_key) High-level key matcher

Abandoned session cleanup and finalization notes