From 0b79dd298784e7f3cf051fe9376c31e61274f28c Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:27:04 -0400 Subject: [PATCH 01/11] feat(process-daemon): write pipe output to a file --- packages/process-daemon/SPEC.md | 12 +- packages/process-daemon/src/client.rs | 3 + packages/process-daemon/src/daemon/server.rs | 2 + packages/process-daemon/src/protocol.rs | 16 ++- packages/process-daemon/src/service.rs | 11 ++ packages/process-daemon/src/service/pipe.rs | 31 ++++- packages/process-daemon/tests/ownership.rs | 15 ++- packages/process-daemon/tests/pipe.rs | 113 ++++++++++++++++++ packages/process-daemon/tests/service.rs | 1 + .../tests/support/resize_fixture.rs | 1 + .../tests/support/terminal_fixture.rs | 1 + .../process-daemon/tests/windows-daemon.rs | 2 + 12 files changed, 202 insertions(+), 6 deletions(-) diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index aceaede1b2f7..39cc551c486b 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -141,6 +141,16 @@ a PTY instead, so clients check `features` first. `terminate` sends SIGKILL to the group and to the child, reaps it, and never waits on a descendant that left the group and still holds the pipe. +`Create` also takes `output_path` for pipe processes (feature `output_path`): +an absolute path the daemon opens before spawning (created with mode `0600` +if missing, otherwise appended to) and writes every output byte to, before +the byte reaches the replay buffer or any subscriber. The file therefore keeps +all output, including what falls out of the 2 MiB replay window or arrives +while no server is attached during a handoff. A path that cannot be opened +fails `create` without starting the process; a later write error stops the +file and is logged, and the process keeps running. `output_path` is rejected +for PTYs. + `List` takes `mode` too. Without it, `list` returns PTYs only, so servers that predate pipe processes never see them. Clients leave `mode` out of the request when it is unset: older daemons reject a `list` with any extra field. @@ -169,7 +179,7 @@ subscriptions, title updates, rename, controller takeover, terminate, and destru shutdown, ownership acquisition, and restart handoff preparation. `Pong` and the registration carry `features`, the daemon's additive -capabilities (currently `["rename", "pipe"]` on Unix and `["rename"]` on Windows). Additive changes, such as a new request +capabilities (currently `["rename", "pipe", "output_path"]` on Unix and `["rename"]` on Windows). Additive changes, such as a new request or field older clients can ignore, extend `features` and keep the protocol version; clients check `features` before sending a new request, since a daemon that cannot parse one drops the connection. Only breaking changes bump diff --git a/packages/process-daemon/src/client.rs b/packages/process-daemon/src/client.rs index 084363e1dbdb..5db77578ed5b 100644 --- a/packages/process-daemon/src/client.rs +++ b/packages/process-daemon/src/client.rs @@ -166,6 +166,7 @@ impl TerminalClient { cols: request.cols, rows: request.rows, mode: request.mode, + output_path: request.output_path, })? { Response::Created { terminal } => Ok(terminal), response => unexpected(response), @@ -595,6 +596,7 @@ fn play(directory: &Path) -> Result<()> { cols: 100, rows: 30, mode: ProcessMode::Pty, + output_path: None, } }; let info = client.create(request)?; @@ -673,6 +675,7 @@ fn play(directory: &Path) -> Result<()> { cols: 100, rows: 30, mode: ProcessMode::Pty, + output_path: None, })?; active = Some(info.id); thread::sleep(Duration::from_millis(200)); diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index f1686ab04b48..f92bb90af7c8 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -559,6 +559,7 @@ fn dispatch( cols, rows, mode, + output_path, } => Response::Created { terminal: service.create(CreateTerminal { program, @@ -570,6 +571,7 @@ fn dispatch( cols, rows, mode, + output_path, })?, }, Request::List { mode } => Response::Terminals { diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index 64e16990a8ca..ed17a425f05f 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -10,7 +10,7 @@ pub const PROTOCOL_VERSION: u32 = 7; /// Additive capabilities of this daemon. Breaking changes bump `PROTOCOL_VERSION`. /// `pipe` (non-PTY processes) is Unix-only for now. #[cfg(unix)] -pub const FEATURES: &[&str] = &["rename", "pipe"]; +pub const FEATURES: &[&str] = &["rename", "pipe", "output_path"]; #[cfg(not(unix))] pub const FEATURES: &[&str] = &["rename"]; @@ -59,6 +59,10 @@ pub enum Request { rows: u16, #[serde(default, skip_serializing_if = "ProcessMode::is_pty")] mode: ProcessMode, + /// Pipe only: an absolute path the daemon appends every output byte + /// to. Send it only to a daemon that lists the `output_path` feature. + #[serde(default, skip_serializing_if = "Option::is_none")] + output_path: Option, }, /// Without `mode`, lists PTYs only, so older servers never see pipe processes. /// `mode` is left out of the JSON when unset: older daemons reject a `list` @@ -407,6 +411,16 @@ mod tests { .. } )); + assert!(matches!( + serde_json::from_str::( + r#"{"op":"create","program":"sh","args":[],"cwd":"/","group_id":"g","env":{},"mode":"pipe","output_path":"/tmp/out"}"# + ) + .unwrap(), + Request::Create { + output_path: Some(path), + .. + } if path == std::path::Path::new("/tmp/out") + )); // Exits without a signal look exactly as before. assert_eq!( serde_json::to_value(Response::Exited { diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index e3873779d1ff..3abc23809233 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -179,6 +179,8 @@ pub struct CreateTerminal { pub cols: u16, pub rows: u16, pub mode: ProcessMode, + /// Pipe only: an absolute path to append every output byte to. + pub output_path: Option, } impl CreateTerminal { @@ -199,6 +201,7 @@ impl CreateTerminal { cols: DEFAULT_COLS, rows: DEFAULT_ROWS, mode: ProcessMode::Pty, + output_path: None, }) } } @@ -225,6 +228,14 @@ impl TerminalService { } pub fn create(&self, request: CreateTerminal) -> Result { + if let Some(path) = &request.output_path { + if request.mode.is_pty() { + bail!("output_path is only supported for pipe processes"); + } + if !path.is_absolute() { + bail!("output_path must be absolute"); + } + } match request.mode { ProcessMode::Pty => validate_size(request.cols, request.rows)?, #[cfg(not(unix))] diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 4af60ccaf3f4..22ed8dd9b53b 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -11,11 +11,16 @@ //! - A descendant can keep the pipe open after the child exits (`cmd &`, build //! servers). The reader stops [`DRAIN_AFTER_EXIT`] after the exit instead of //! waiting for an EOF that may never come. +//! - With `output_path`, the reader appends every byte to that file before +//! handing it to the actor, so output survives the 2 MiB replay window, +//! arrives before any subscriber, and stays complete across a handoff. //! - The reader polls a cancel pipe too, so shutdown never waits on a pipe that //! a descendant which left the process group still holds open. -use std::io::{self, PipeReader, PipeWriter, Read}; +use std::fs::{File, OpenOptions}; +use std::io::{self, PipeReader, PipeWriter, Read, Write}; use std::os::fd::AsFd; +use std::os::unix::fs::OpenOptionsExt; use std::os::unix::process::CommandExt; use std::process::{Command, Stdio}; use std::sync::{Arc, Mutex, PoisonError, RwLock}; @@ -90,6 +95,19 @@ impl TerminalHandle { exited, exited_writer, } = Pipes::new()?; + // Before spawning, so a bad path fails the request with nothing to clean up. + let sink = request + .output_path + .as_deref() + .map(|path| { + OpenOptions::new() + .create(true) + .append(true) + .mode(0o600) + .open(path) + .with_context(|| format!("failed to open output file {}", path.display())) + }) + .transpose()?; let mut command = Command::new(&request.program); command .args(&request.args) @@ -143,7 +161,7 @@ impl TerminalHandle { let reader_actor_tx = actor.actor_tx.clone(); let started = thread::Builder::new() .name(format!("opencode-pty-reader-{id}")) - .spawn(move || read_output(output, cancel, exited, reader_actor_tx)) + .spawn(move || read_output(output, sink, cancel, exited, reader_actor_tx)) .context("failed to spawn terminal reader") .and_then(|reader| { joins.push(reader); @@ -348,6 +366,7 @@ fn signal_name(signal: i32) -> String { /// deadline. fn read_output( output: PipeReader, + mut sink: Option, cancel: PipeReader, exited: PipeReader, actor: Sender, @@ -426,6 +445,14 @@ fn read_output( return; } Ok(length) => { + // The file first, so it always holds at least what + // subscribers have seen, even if the actor is backpressured. + if let Some(file) = &mut sink + && let Err(error) = file.write_all(&buffer[..length]) + { + eprintln!("opencode-pty stopped writing output file: {error}"); + sink = None; + } if actor .send(ActorMessage::Output(buffer[..length].to_vec())) .is_err() diff --git a/packages/process-daemon/tests/ownership.rs b/packages/process-daemon/tests/ownership.rs index afadcfdfe1ed..8ef4b71e6217 100644 --- a/packages/process-daemon/tests/ownership.rs +++ b/packages/process-daemon/tests/ownership.rs @@ -111,6 +111,7 @@ impl Daemon { cols: 80, rows: 24, mode: Default::default(), + output_path: None, }) { Response::Created { terminal } => terminal, response => panic!("create: {response:?}"), @@ -315,10 +316,13 @@ fn rename_is_advertised_and_served() { use opencode_pty::protocol::{SubscriptionEvent, read_subscription_event}; let mut daemon = Daemon::start(); - assert_eq!(daemon.registration.features, ["rename", "pipe"]); + assert_eq!( + daemon.registration.features, + ["rename", "pipe", "output_path"] + ); assert!(matches!( daemon.request(Request::Ping), - Response::Pong { features, .. } if features == ["rename", "pipe"] + Response::Pong { features, .. } if features == ["rename", "pipe", "output_path"] )); let (owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); @@ -588,6 +592,7 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { let (owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); let terminal = daemon.terminal("exec cat"); + let output_file = daemon.root.join("pipe.out"); let Response::Created { terminal: process } = daemon.request(Request::Create { program: "/bin/sh".into(), args: vec![ @@ -601,6 +606,7 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { cols: 0, rows: 0, mode: ProcessMode::Pipe, + output_path: Some(output_file.clone()), }) else { panic!("create pipe"); }; @@ -657,6 +663,10 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { }; assert_eq!(exit, Some(4)); assert_eq!(String::from_utf8(output).unwrap(), "before\nafter\n"); + assert_eq!( + std::fs::read_to_string(&output_file).unwrap(), + "before\nafter\n" + ); assert!(matches!( daemon.request(Request::Terminate { id: process.id }), Response::Ok @@ -685,6 +695,7 @@ fn pipe_processes_do_not_inherit_the_daemons_blocked_signals() { cols: 0, rows: 0, mode: ProcessMode::Pipe, + output_path: None, }) else { panic!("create pipe"); }; diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index 78fc1e986a43..c41b8ef6a862 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -19,6 +19,7 @@ fn pipe(script: &str) -> CreateTerminal { cols: 0, rows: 0, mode: ProcessMode::Pipe, + output_path: None, } } @@ -300,3 +301,115 @@ fn pipe_processes_need_no_terminal_size() { let info = service.create(pipe("true")).unwrap(); service.terminate(info.id).unwrap(); } + +fn temp_path(name: &str) -> std::path::PathBuf { + std::env::temp_dir().join(format!( + "opencode-pty-pipe-{name}-{:032x}", + rand::random::() + )) +} + +fn wait_exited(service: &TerminalService, id: TerminalId) -> TerminalLifecycle { + let deadline = Instant::now() + Duration::from_secs(10); + loop { + let lifecycle = service + .list_mode(ProcessMode::Pipe) + .unwrap() + .into_iter() + .find(|info| info.id == id) + .unwrap() + .lifecycle; + if lifecycle != TerminalLifecycle::Running { + return lifecycle; + } + assert!(Instant::now() < deadline, "pipe process did not exit"); + std::thread::sleep(Duration::from_millis(10)); + } +} + +#[test] +fn output_path_keeps_everything_beyond_the_replay_window_without_a_subscriber() { + use std::os::unix::fs::PermissionsExt; + + // A replay window far smaller than the output. + let service = TerminalService::new(1024); + let path = temp_path("full"); + let info = service + .create(CreateTerminal { + output_path: Some(path.clone()), + ..pipe("seq 1 20000; echo done >&2") + }) + .unwrap(); + assert_eq!( + wait_exited(&service, info.id), + TerminalLifecycle::Exited { + exit_code: Some(0), + signal: None + } + ); + let expected = (1..=20000).map(|n| format!("{n}\n")).collect::() + "done\n"; + assert_eq!(std::fs::read_to_string(&path).unwrap(), expected); + assert_eq!( + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, + 0o600 + ); + let replay = service.replay(info.id, 0).unwrap(); + assert!(replay.truncated); + assert_eq!(replay.end_offset, expected.len() as u64); + service.terminate(info.id).unwrap(); + std::fs::remove_file(path).unwrap(); +} + +#[test] +fn output_path_appends_to_an_existing_file() { + let service = TerminalService::default(); + let path = temp_path("append"); + std::fs::write(&path, "earlier\n").unwrap(); + let info = service + .create(CreateTerminal { + output_path: Some(path.clone()), + ..pipe("echo later") + }) + .unwrap(); + wait_exited(&service, info.id); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "earlier\nlater\n"); + service.terminate(info.id).unwrap(); + std::fs::remove_file(path).unwrap(); +} + +#[test] +fn output_path_must_be_an_absolute_file_for_a_pipe_process() { + let service = TerminalService::default(); + let missing = temp_path("missing-directory").join("out"); + for (request, error) in [ + ( + CreateTerminal { + output_path: Some("relative.out".into()), + ..pipe("true") + }, + "must be absolute", + ), + ( + CreateTerminal { + output_path: Some(temp_path("pty")), + cols: 80, + rows: 24, + mode: ProcessMode::Pty, + ..pipe("true") + }, + "only supported for pipe processes", + ), + ( + CreateTerminal { + output_path: Some(missing), + ..pipe("touch should-not-run") + }, + "failed to open output file", + ), + ] { + let message = format!("{:#}", service.create(request).unwrap_err()); + assert!(message.contains(error), "{message}"); + } + assert!(service.list_mode(ProcessMode::Pipe).unwrap().is_empty()); + assert!(!std::path::Path::new("should-not-run").exists()); +} diff --git a/packages/process-daemon/tests/service.rs b/packages/process-daemon/tests/service.rs index 2988f3952b44..530aa844ae76 100644 --- a/packages/process-daemon/tests/service.rs +++ b/packages/process-daemon/tests/service.rs @@ -70,6 +70,7 @@ fn command(script: &str) -> CreateTerminal { cols: 80, rows: 24, mode: Default::default(), + output_path: None, } } diff --git a/packages/process-daemon/tests/support/resize_fixture.rs b/packages/process-daemon/tests/support/resize_fixture.rs index 70f4577d06e1..efc8dcda4841 100644 --- a/packages/process-daemon/tests/support/resize_fixture.rs +++ b/packages/process-daemon/tests/support/resize_fixture.rs @@ -44,6 +44,7 @@ impl ResizeFixture { cols: 80, rows: 24, mode: Default::default(), + output_path: None, }) .unwrap(); diff --git a/packages/process-daemon/tests/support/terminal_fixture.rs b/packages/process-daemon/tests/support/terminal_fixture.rs index 47b2f294a5e7..173ca08be9c9 100644 --- a/packages/process-daemon/tests/support/terminal_fixture.rs +++ b/packages/process-daemon/tests/support/terminal_fixture.rs @@ -63,6 +63,7 @@ impl Fixture { cols: 80, rows: 24, mode: Default::default(), + output_path: None, } } diff --git a/packages/process-daemon/tests/windows-daemon.rs b/packages/process-daemon/tests/windows-daemon.rs index ac3a2e6e0d99..00efd2db3fd1 100644 --- a/packages/process-daemon/tests/windows-daemon.rs +++ b/packages/process-daemon/tests/windows-daemon.rs @@ -94,6 +94,7 @@ impl Daemon { cols, rows, mode, + output_path, } = request; match self.request(Request::Create { program, @@ -105,6 +106,7 @@ impl Daemon { cols, rows, mode, + output_path, }) { Response::Created { terminal } => terminal, response => panic!("create: {response:?}"), From ef6afaa81a82858e86b1bd30aa682723b86bcc1c Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:32:17 -0400 Subject: [PATCH 02/11] fix(process-daemon): report output file write errors and refuse symlinks --- packages/process-daemon/SPEC.md | 4 +- packages/process-daemon/src/service.rs | 15 ++++ packages/process-daemon/src/service/pipe.rs | 17 ++++- packages/process-daemon/tests/pipe.rs | 85 ++++++++++++++++----- 4 files changed, 102 insertions(+), 19 deletions(-) diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index 39cc551c486b..c10755168385 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -148,7 +148,9 @@ the byte reaches the replay buffer or any subscriber. The file therefore keeps all output, including what falls out of the 2 MiB replay window or arrives while no server is attached during a handoff. A path that cannot be opened fails `create` without starting the process; a later write error stops the -file and is logged, and the process keeps running. `output_path` is rejected +file, sets `TerminalInfo.output_error` (omitted otherwise), and the process +keeps running. The file is closed before the exit is published, and a symlink +as the final path component is refused. `output_path` is rejected for PTYs. `List` takes `mode` too. Without it, `list` returns PTYs only, so servers that diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index 3abc23809233..e4468e18a96b 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -87,6 +87,10 @@ pub struct TerminalInfo { pub lifecycle: TerminalLifecycle, pub output_head: u64, pub output_tail: u64, + /// Set when writing a pipe process's `output_path` failed; the file then + /// lacks everything from that point on. Omitted when there is no error. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub output_error: Option, } #[derive(Clone, Debug)] @@ -832,6 +836,7 @@ fn new_info( lifecycle: TerminalLifecycle::Running, output_head: 0, output_tail: 0, + output_error: None, })) } @@ -859,6 +864,9 @@ enum ActorMessage { /// A pipe process ended by a signal; the value is the signal's name. #[cfg(unix)] ChildKilled(String), + /// Writing the `output_path` file failed; it stops at this point. + #[cfg(unix)] + OutputFileFailed(String), #[cfg(not(windows))] RefreshForegroundProcess { reply: Reply>, @@ -1352,6 +1360,12 @@ fn run_actor(config: ActorConfig) -> Result<()> { } } #[cfg(unix)] + Ok(ActorMessage::OutputFileFailed(message)) => { + if let Ok(mut value) = info.write() { + value.output_error = Some(message); + } + } + #[cfg(unix)] Ok(ActorMessage::ChildKilled(signal)) => { child_exit = Some(Ok((None, Some(signal)))); } @@ -1945,6 +1959,7 @@ mod tests { lifecycle: TerminalLifecycle::Running, output_head: 0, output_tail: 0, + output_error: None, } } diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 22ed8dd9b53b..1f4bd299f949 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -104,6 +104,8 @@ impl TerminalHandle { .create(true) .append(true) .mode(0o600) + // Never write through a symlink planted at the final component. + .custom_flags(libc::O_NOFOLLOW) .open(path) .with_context(|| format!("failed to open output file {}", path.display())) }) @@ -388,6 +390,8 @@ fn read_output( .revents() .is_some_and(|events| events.contains(PollFlags::POLLHUP)); if !hung_up { + // Close the file before anyone can observe the exit. + drop(sink); let _ = actor.send(ActorMessage::ReaderEof); return; } @@ -441,6 +445,8 @@ fn read_output( if ready(&fds[0]) { match output.read(&mut buffer) { Ok(0) => { + // Close the file before anyone can observe the exit. + drop(sink); let _ = actor.send(ActorMessage::ReaderEof); return; } @@ -450,8 +456,17 @@ fn read_output( if let Some(file) = &mut sink && let Err(error) = file.write_all(&buffer[..length]) { - eprintln!("opencode-pty stopped writing output file: {error}"); + // Ordered before this chunk's output, so the error is + // visible no later than the bytes the file lacks. sink = None; + if actor + .send(ActorMessage::OutputFileFailed(format!( + "failed to write output file: {error}" + ))) + .is_err() + { + return; + } } if actor .send(ActorMessage::Output(buffer[..length].to_vec())) diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index c41b8ef6a862..2064c53129b7 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -302,11 +302,22 @@ fn pipe_processes_need_no_terminal_size() { service.terminate(info.id).unwrap(); } -fn temp_path(name: &str) -> std::path::PathBuf { - std::env::temp_dir().join(format!( - "opencode-pty-pipe-{name}-{:032x}", - rand::random::() - )) +/// A unique temporary path, removed when dropped even if a test fails. +struct TempPath(std::path::PathBuf); + +impl TempPath { + fn new(name: &str) -> Self { + Self(std::env::temp_dir().join(format!( + "opencode-pty-pipe-{name}-{:032x}", + rand::random::() + ))) + } +} + +impl Drop for TempPath { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } } fn wait_exited(service: &TerminalService, id: TerminalId) -> TerminalLifecycle { @@ -333,7 +344,8 @@ fn output_path_keeps_everything_beyond_the_replay_window_without_a_subscriber() // A replay window far smaller than the output. let service = TerminalService::new(1024); - let path = temp_path("full"); + let file = TempPath::new("full"); + let path = &file.0; let info = service .create(CreateTerminal { output_path: Some(path.clone()), @@ -348,23 +360,27 @@ fn output_path_keeps_everything_beyond_the_replay_window_without_a_subscriber() } ); let expected = (1..=20000).map(|n| format!("{n}\n")).collect::() + "done\n"; - assert_eq!(std::fs::read_to_string(&path).unwrap(), expected); + assert_eq!(std::fs::read_to_string(path).unwrap(), expected); assert_eq!( - std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, + std::fs::metadata(path).unwrap().permissions().mode() & 0o777, 0o600 ); let replay = service.replay(info.id, 0).unwrap(); assert!(replay.truncated); assert_eq!(replay.end_offset, expected.len() as u64); + assert_eq!( + service.list_mode(ProcessMode::Pipe).unwrap()[0].output_error, + None + ); service.terminate(info.id).unwrap(); - std::fs::remove_file(path).unwrap(); } #[test] fn output_path_appends_to_an_existing_file() { let service = TerminalService::default(); - let path = temp_path("append"); - std::fs::write(&path, "earlier\n").unwrap(); + let file = TempPath::new("append"); + let path = &file.0; + std::fs::write(path, "earlier\n").unwrap(); let info = service .create(CreateTerminal { output_path: Some(path.clone()), @@ -372,15 +388,20 @@ fn output_path_appends_to_an_existing_file() { }) .unwrap(); wait_exited(&service, info.id); - assert_eq!(std::fs::read_to_string(&path).unwrap(), "earlier\nlater\n"); + assert_eq!(std::fs::read_to_string(path).unwrap(), "earlier\nlater\n"); service.terminate(info.id).unwrap(); - std::fs::remove_file(path).unwrap(); } #[test] fn output_path_must_be_an_absolute_file_for_a_pipe_process() { let service = TerminalService::default(); - let missing = temp_path("missing-directory").join("out"); + let missing = TempPath::new("missing-directory").0.join("out"); + let marker = TempPath::new("should-not-run"); + let target = TempPath::new("target"); + std::fs::write(&target.0, "").unwrap(); + let link = TempPath::new("link"); + std::os::unix::fs::symlink(&target.0, &link.0).unwrap(); + let touch = format!("touch '{}'", marker.0.display()); for (request, error) in [ ( CreateTerminal { @@ -391,7 +412,7 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { ), ( CreateTerminal { - output_path: Some(temp_path("pty")), + output_path: Some(TempPath::new("pty").0.clone()), cols: 80, rows: 24, mode: ProcessMode::Pty, @@ -402,7 +423,14 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { ( CreateTerminal { output_path: Some(missing), - ..pipe("touch should-not-run") + ..pipe(&touch) + }, + "failed to open output file", + ), + ( + CreateTerminal { + output_path: Some(link.0.clone()), + ..pipe(&touch) }, "failed to open output file", ), @@ -411,5 +439,28 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { assert!(message.contains(error), "{message}"); } assert!(service.list_mode(ProcessMode::Pipe).unwrap().is_empty()); - assert!(!std::path::Path::new("should-not-run").exists()); + // Give a wrongly spawned child time to run before checking. + std::thread::sleep(Duration::from_millis(100)); + assert!(!marker.0.exists()); +} + +#[cfg(target_os = "linux")] +#[test] +fn a_failing_output_file_is_reported_and_the_process_keeps_running() { + let service = TerminalService::default(); + let info = service + .create(CreateTerminal { + output_path: Some("/dev/full".into()), + ..pipe("echo lost; echo kept; exit 5") + }) + .unwrap(); + let finished = run(&service, info.id); + assert_eq!(finished.output, "lost\nkept\n"); + assert_eq!(finished.exit_code, Some(5)); + let error = service.list_mode(ProcessMode::Pipe).unwrap()[0] + .output_error + .clone() + .unwrap(); + assert!(error.contains("failed to write output file"), "{error}"); + service.terminate(info.id).unwrap(); } From b54cf68ad4bfb8c7e9d91fcf80ae39464f925d78 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:35:10 -0400 Subject: [PATCH 03/11] fix(process-daemon): report output file errors on the exit event --- packages/process-daemon/src/daemon/server.rs | 2 ++ packages/process-daemon/src/protocol.rs | 5 +++++ packages/process-daemon/src/service.rs | 7 ++++++- packages/process-daemon/src/transport/unix.rs | 2 ++ packages/process-daemon/src/transport/windows.rs | 1 + packages/process-daemon/tests/pipe.rs | 8 +++++++- 6 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index f92bb90af7c8..5bb13d37171f 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -505,10 +505,12 @@ fn stream_subscription( exit_code, signal, final_offset, + output_error, } => Response::Exited { exit_code, signal, final_offset, + output_error, }, StreamEvent::ControllerChanged { attachment_id, diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index ed17a425f05f..82f5bebc78a5 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -187,6 +187,10 @@ pub enum Response { #[serde(default, skip_serializing_if = "Option::is_none")] signal: Option, final_offset: u64, + /// Same as `TerminalInfo.output_error`, so a live subscriber learns the + /// output file is incomplete without another request. + #[serde(default, skip_serializing_if = "Option::is_none")] + output_error: Option, }, ControllerChanged { attachment_id: Option, @@ -427,6 +431,7 @@ mod tests { exit_code: Some(1), signal: None, final_offset: 2, + output_error: None, }) .unwrap(), serde_json::json!({"type": "exited", "exit_code": 1, "final_offset": 2}) diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index e4468e18a96b..dd04d9305a0e 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -136,6 +136,7 @@ pub enum StreamEvent { exit_code: Option, signal: Option, final_offset: u64, + output_error: Option, }, ControllerChanged { attachment_id: Option, @@ -1399,16 +1400,19 @@ fn run_actor(config: ActorConfig) -> Result<()> { && let Some(result) = child_exit.take() { let (exit_code, signal) = result.clone().unwrap_or_default(); + let mut output_error = None; if let Ok(mut value) = info.write() { value.lifecycle = match result { Ok((exit_code, signal)) => TerminalLifecycle::Exited { exit_code, signal }, Err(message) => TerminalLifecycle::Failed { message }, }; + output_error = value.output_error.clone(); } let event = StreamEvent::Exited { exit_code, signal, final_offset: replay.tail, + output_error, }; broadcast( &shutdown, @@ -2144,7 +2148,8 @@ mod tests { [StreamEvent::Exited { exit_code: Some(23), signal: None, - final_offset: 5 + final_offset: 5, + output_error: None, }] )); } diff --git a/packages/process-daemon/src/transport/unix.rs b/packages/process-daemon/src/transport/unix.rs index 5be4764267a6..d4ee215bda2d 100644 --- a/packages/process-daemon/src/transport/unix.rs +++ b/packages/process-daemon/src/transport/unix.rs @@ -393,6 +393,7 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_error: None, }, ) .unwrap(); @@ -404,6 +405,7 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_error: None, } )); assert_eq!(client.read(&mut [0]).unwrap(), 0); diff --git a/packages/process-daemon/src/transport/windows.rs b/packages/process-daemon/src/transport/windows.rs index cc9d04405d23..9fc7eea8c9ba 100644 --- a/packages/process-daemon/src/transport/windows.rs +++ b/packages/process-daemon/src/transport/windows.rs @@ -705,6 +705,7 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_error: None, }, ) .unwrap(); diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index 2064c53129b7..92b8b712502d 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -25,6 +25,7 @@ fn pipe(script: &str) -> CreateTerminal { struct Finished { output: String, + output_error: Option, exit_code: Option, signal: Option, final_offset: u64, @@ -49,9 +50,11 @@ fn run(service: &TerminalService, id: TerminalId) -> Finished { exit_code, signal, final_offset, + output_error, } => { return Finished { output: String::from_utf8(output).unwrap(), + output_error, exit_code, signal, final_offset, @@ -96,6 +99,7 @@ fn reports_ordered_output_and_exit_code_without_a_terminal() { ); assert_eq!(finished.exit_code, Some(7)); assert_eq!(finished.signal, None); + assert_eq!(finished.output_error, None); assert_eq!(finished.final_offset, finished.output.len() as u64); let listed = service.list_mode(ProcessMode::Pipe).unwrap(); assert_eq!( @@ -396,6 +400,7 @@ fn output_path_appends_to_an_existing_file() { fn output_path_must_be_an_absolute_file_for_a_pipe_process() { let service = TerminalService::default(); let missing = TempPath::new("missing-directory").0.join("out"); + let pty = TempPath::new("pty"); let marker = TempPath::new("should-not-run"); let target = TempPath::new("target"); std::fs::write(&target.0, "").unwrap(); @@ -412,7 +417,7 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { ), ( CreateTerminal { - output_path: Some(TempPath::new("pty").0.clone()), + output_path: Some(pty.0.clone()), cols: 80, rows: 24, mode: ProcessMode::Pty, @@ -457,6 +462,7 @@ fn a_failing_output_file_is_reported_and_the_process_keeps_running() { let finished = run(&service, info.id); assert_eq!(finished.output, "lost\nkept\n"); assert_eq!(finished.exit_code, Some(5)); + assert!(finished.output_error.is_some()); let error = service.list_mode(ProcessMode::Pipe).unwrap()[0] .output_error .clone() From 8aca0974d69eb8a5d439036b34d8d7b7d8da9ae4 Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 00:09:15 +0000 Subject: [PATCH 04/11] feat(process-daemon): protocol v8 for shell commands Bump the protocol to 8 and drop the compatibility code for older peers: no feature flags, required fields, a split create_pty/create_pipe, and List with a required mode. - ProcessInfo (was TerminalInfo) with pty/pipe groups and timestamps. - create_pipe replaces the environment, requires an absolute cwd, an output file (truncated, umask mode) and opaque metadata (1 MiB cap). - The output file is the only buffer: no replay for pipe processes. - output_lines counts newlines that reached the file. - get, wait (one Exited), signal (keeps the record) and set_metadata. - Unknown requests get an error instead of a dropped connection. - A failed pipe reader still publishes the exit. --- packages/process-daemon/src/client.rs | 115 ++- packages/process-daemon/src/daemon.rs | 1 - packages/process-daemon/src/daemon/server.rs | 345 +++++++-- packages/process-daemon/src/protocol.rs | 480 ++++++++----- packages/process-daemon/src/service.rs | 634 ++++++++++++----- packages/process-daemon/src/service/pipe.rs | 132 +++- packages/process-daemon/src/transport/unix.rs | 4 + .../process-daemon/src/transport/windows.rs | 2 + packages/process-daemon/tests/ownership.rs | 128 ++-- packages/process-daemon/tests/pipe.rs | 671 +++++++++++------- packages/process-daemon/tests/rows.rs | 26 +- packages/process-daemon/tests/runtime.rs | 49 +- packages/process-daemon/tests/service.rs | 94 ++- .../tests/support/resize_fixture.rs | 6 +- .../tests/support/terminal_fixture.rs | 8 +- .../process-daemon/tests/windows-daemon.rs | 40 +- 16 files changed, 1839 insertions(+), 896 deletions(-) diff --git a/packages/process-daemon/src/client.rs b/packages/process-daemon/src/client.rs index 5db77578ed5b..3c3abdbccc8c 100644 --- a/packages/process-daemon/src/client.rs +++ b/packages/process-daemon/src/client.rs @@ -13,7 +13,10 @@ use crate::protocol::{ AttachmentRole, Envelope, SubscriptionEvent, read_frame, read_subscription_event, write_frame, }; use crate::protocol::{PROTOCOL_VERSION, Request, Response}; -use crate::service::{CreateTerminal, ProcessMode, TerminalId, TerminalInfo, TerminalRows}; +use crate::service::{ + CreatePipe, CreatePty, PipeMetadata, ProcessInfo, ProcessMode, ProcessSignal, TerminalId, + TerminalRows, +}; #[cfg(unix)] use crate::transport::Connection; @@ -27,7 +30,7 @@ pub struct TerminalClient { #[derive(Debug)] pub struct RemoteSnapshot { - pub terminal: TerminalInfo, + pub terminal: ProcessInfo, pub text: String, pub checkpoint: Vec, pub cursor_x: u16, @@ -46,7 +49,7 @@ pub struct RemoteReplay { #[cfg(unix)] pub struct TerminalSubscription { stream: Connection, - pub terminal: TerminalInfo, + pub terminal: ProcessInfo, pub role: AttachmentRole, pub generation: u64, pub replay: RemoteReplay, @@ -155,31 +158,67 @@ impl TerminalClient { } } - pub fn create(&self, request: CreateTerminal) -> Result { - match self.request(Request::Create { + pub fn create_pty(&self, request: CreatePty) -> Result { + match self.request(Request::CreatePty { program: request.program, args: request.args, cwd: request.cwd, - title: request.title, - group_id: request.group_id, env: request.env, + group_id: request.group_id, + title: request.title, cols: request.cols, rows: request.rows, - mode: request.mode, + })? { + Response::Created { process } => Ok(process), + response => unexpected(response), + } + } + + pub fn create_pipe(&self, request: CreatePipe) -> Result { + match self.request(Request::CreatePipe { + program: request.program, + args: request.args, + cwd: request.cwd, + env: request.env, + group_id: request.group_id, output_path: request.output_path, + metadata: request.metadata, })? { - Response::Created { terminal } => Ok(terminal), + Response::Created { process } => Ok(process), response => unexpected(response), } } - pub fn list(&self) -> Result> { - match self.request(Request::List { mode: None })? { - Response::Terminals { terminals } => Ok(terminals), + pub fn list(&self, mode: ProcessMode) -> Result> { + match self.request(Request::List { mode })? { + Response::Processes { processes } => Ok(processes), response => unexpected(response), } } + pub fn get(&self, id: TerminalId) -> Result { + match self.request(Request::Get { id })? { + Response::Process { process } => Ok(process), + response => unexpected(response), + } + } + + /// Blocks until the pipe process exits and returns the `Exited` response. + pub fn wait(&self, id: TerminalId) -> Result { + match self.request(Request::Wait { id })? { + response @ Response::Exited { .. } => Ok(response), + response => unexpected(response), + } + } + + pub fn signal(&self, id: TerminalId, signal: ProcessSignal) -> Result<()> { + self.expect_ok(Request::Signal { id, signal }) + } + + pub fn set_metadata(&self, id: TerminalId, metadata: PipeMetadata) -> Result<()> { + self.expect_ok(Request::SetMetadata { id, metadata }) + } + pub fn write(&self, id: TerminalId, data: Vec) -> Result<()> { self.write_for(id, None, data) } @@ -251,7 +290,7 @@ impl TerminalClient { pub fn snapshot(&self, id: TerminalId) -> Result { match self.request(Request::Snapshot { id })? { Response::Snapshot { - terminal, + process: terminal, text, checkpoint_base64, cursor_x, @@ -272,12 +311,12 @@ impl TerminalClient { pub fn read_rows(&self, id: TerminalId, rows: Option) -> Result { match self.request(Request::ReadRows { id, rows })? { Response::Rows { - terminal, + process, lines, cursor_x, cursor_y, } => Ok(TerminalRows { - terminal, + process, lines, cursor_x, cursor_y, @@ -336,7 +375,7 @@ impl TerminalClient { )?; match read_frame(&mut stream)? { Response::Attached { - terminal, + process: terminal, role, generation, requested_offset, @@ -487,7 +526,7 @@ fn status(directory: &Path) -> Result<()> { "opencode-pty running: pid={} instance={} terminals={}", client.registration.pid, client.registration.instance_id, - client.list()?.len() + client.list(ProcessMode::Pty)?.len() ); Ok(()) } @@ -555,7 +594,10 @@ fn watch(_directory: &Path, _id: TerminalId) -> Result<()> { fn play(directory: &Path) -> Result<()> { let client = TerminalClient::start(directory)?; - let mut active = client.list()?.first().map(|terminal| terminal.id); + let mut active = client + .list(ProcessMode::Pty)? + .first() + .map(|terminal| terminal.id); println!("\n opencode-pty playground"); println!( " owning service pid={} · `quit` stops all terminals\n", @@ -584,9 +626,9 @@ fn play(directory: &Path) -> Result<()> { "new" => { let words = shell_words::split(rest)?; let request = if words.is_empty() { - CreateTerminal::shell()? + CreatePty::shell()? } else { - CreateTerminal { + CreatePty { title: words[0].clone(), group_id: format!("playground:{}", env::current_dir()?.display()), env: std::collections::HashMap::new(), @@ -595,11 +637,9 @@ fn play(directory: &Path) -> Result<()> { cwd: env::current_dir()?, cols: 100, rows: 30, - mode: ProcessMode::Pty, - output_path: None, } }; - let info = client.create(request)?; + let info = client.create_pty(request)?; active = Some(info.id); println!("created terminal {} (pid {:?})", info.id, info.pid); } @@ -659,13 +699,16 @@ fn play(directory: &Path) -> Result<()> { let id = optional_id(rest, active)?; client.terminate(id)?; if active == Some(id) { - active = client.list()?.first().map(|terminal| terminal.id); + active = client + .list(ProcessMode::Pty)? + .first() + .map(|terminal| terminal.id); } println!("terminated terminal {id}"); } "demo" => { let executable = env::current_exe()?; - let info = client.create(CreateTerminal { + let info = client.create_pty(CreatePty { title: "query-fixture".to_string(), group_id: format!("playground:{}", env::current_dir()?.display()), env: std::collections::HashMap::new(), @@ -674,8 +717,6 @@ fn play(directory: &Path) -> Result<()> { cwd: env::current_dir()?, cols: 100, rows: 30, - mode: ProcessMode::Pty, - output_path: None, })?; active = Some(info.id); thread::sleep(Duration::from_millis(200)); @@ -757,33 +798,41 @@ fn print_help() { } fn print_terminals(client: &TerminalClient) -> Result<()> { - let terminals = client.list()?; + let terminals = client.list(ProcessMode::Pty)?; if terminals.is_empty() { println!("no terminals; try `new`"); return Ok(()); } for terminal in terminals { + let Some(pty) = &terminal.pty else { + continue; + }; println!( "{} pid={:?} {}x{} {:?} raw={}..{} {}", terminal.id, terminal.pid, - terminal.cols, - terminal.rows, + pty.cols, + pty.rows, terminal.lifecycle, terminal.output_head, terminal.output_tail, - terminal.title, + pty.title, ); } Ok(()) } fn print_snapshot(snapshot: &RemoteSnapshot) { + let (cols, rows) = snapshot + .terminal + .pty + .as_ref() + .map_or((0, 0), |pty| (pty.cols, pty.rows)); println!( "\n╭─ terminal {} · {}x{} · cursor {},{} · {:?}", snapshot.terminal.id, - snapshot.terminal.cols, - snapshot.terminal.rows, + cols, + rows, snapshot.cursor_x, snapshot.cursor_y, snapshot.terminal.lifecycle, diff --git a/packages/process-daemon/src/daemon.rs b/packages/process-daemon/src/daemon.rs index 1243749dda61..e86fa1675a75 100644 --- a/packages/process-daemon/src/daemon.rs +++ b/packages/process-daemon/src/daemon.rs @@ -14,7 +14,6 @@ pub struct Registration { /// Opaque local transport endpoint, not necessarily a filesystem entry. pub socket: PathBuf, pub token: String, - #[serde(default)] pub features: Vec, } diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index 5bb13d37171f..80b6ab011855 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -11,8 +11,8 @@ use base64::Engine; use super::control::Control; use super::{Registration, platform}; use crate::ownership::{Ownership, Snapshot}; -use crate::protocol::{Envelope, Request, Response, read_frame, write_frame, write_output_frame}; -use crate::service::{CreateTerminal, StreamEvent, TerminalService}; +use crate::protocol::{Request, Response, read_envelope, write_frame, write_output_frame}; +use crate::service::{CreatePipe, CreatePty, StreamEvent, TerminalService}; use crate::transport::{Cancellation, Connection, Listener, Readiness, Wake}; const ACCEPT_BACKOFF: Duration = Duration::from_millis(100); @@ -313,8 +313,8 @@ fn handle_connection( registration: &Registration, control: &Control, ) -> Result<()> { - let envelope: Envelope = read_frame(&mut stream)?; - if envelope.token != registration.token { + let (token, request) = read_envelope(&mut stream)?; + if token != registration.token { return send_response( &mut stream, &Response::Error { @@ -322,10 +322,14 @@ fn handle_connection( }, ); } + let request = match request { + Ok(request) => request, + Err(message) => return send_response(&mut stream, &Response::Error { message }), + }; if let Request::Own { instance_id, ticket, - } = envelope.request + } = request { let claim = if instance_id != registration.instance_id { Err(anyhow!("daemon instance_id mismatch")) @@ -350,13 +354,16 @@ fn handle_connection( control.update_ownership(|ownership| ownership.disconnect(generation, Instant::now()))?; return result; } + if let Request::Wait { id } = request { + return wait_for_exit(&mut stream, service, control, id); + } if let Request::Subscribe { id, offset, attachment_id, role, takeover, - } = envelope.request + } = request { return stream_subscription( &mut stream, @@ -371,9 +378,9 @@ fn handle_connection( }, ); } - let stopping = matches!(envelope.request, Request::Shutdown); + let stopping = matches!(request, Request::Shutdown); let response = - dispatch(envelope.request, service, registration).unwrap_or_else(|error| Response::Error { + dispatch(request, service, registration).unwrap_or_else(|error| Response::Error { message: format!("{error:#}"), }); let result = send_response(&mut stream, &response); @@ -391,7 +398,7 @@ fn owner_connection( ) -> Result<()> { write_frame(&mut *stream, &Response::Owned)?; while !control.stopping() { - let envelope: Envelope = read_frame(&mut *stream)?; + let (token, request) = read_envelope(&mut *stream)?; if !control.is_owner(generation)? { return send_response( stream, @@ -400,15 +407,15 @@ fn owner_connection( }, ); } - let stopping = - envelope.token == registration.token && matches!(envelope.request, Request::Shutdown); - let response = if envelope.token != registration.token { + let stopping = token == registration.token && matches!(request, Ok(Request::Shutdown)); + let response = if token != registration.token { Response::Error { message: "authentication failed".to_string(), } } else { - match envelope.request { - Request::PrepareHandoff => { + match request { + Err(message) => Response::Error { message }, + Ok(Request::PrepareHandoff) => { let unix_ms = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() as u64; let handoff = control.update_ownership(|ownership| { ownership.prepare(generation, Instant::now(), unix_ms) @@ -418,8 +425,8 @@ fn owner_connection( expires_at: handoff.expires_at, } } - Request::Shutdown => Response::Ok, - _ => Response::Error { + Ok(Request::Shutdown) => Response::Ok, + Ok(_) => Response::Error { message: "owner connection only accepts prepare_handoff or shutdown" .to_string(), }, @@ -462,7 +469,7 @@ fn stream_subscription( write_frame( &mut *stream, &Response::Attached { - terminal: attachment.terminal.clone(), + process: attachment.process.clone(), role: attachment.role, generation: attachment.generation, requested_offset: attachment.replay.requested_offset, @@ -501,17 +508,7 @@ fn stream_subscription( generation, checkpoint_base64: base64::engine::general_purpose::STANDARD.encode(checkpoint), }, - StreamEvent::Exited { - exit_code, - signal, - final_offset, - output_error, - } => Response::Exited { - exit_code, - signal, - final_offset, - output_error, - }, + StreamEvent::Exited(exit) => exit.into(), StreamEvent::ControllerChanged { attachment_id, generation, @@ -533,6 +530,46 @@ fn stream_subscription( result } +/// Answers a `wait` with one `Exited` once the pipe process exits. Errors +/// that need no waiting (unknown id, a PTY) are answered before monitoring. +fn wait_for_exit( + stream: &mut Connection, + service: &TerminalService, + control: &Control, + id: crate::service::TerminalId, +) -> Result<()> { + let wait = match service.wait(id) { + Ok(wait) => wait, + Err(error) => { + return send_response( + stream, + &Response::Error { + message: format!("{error:#}"), + }, + ); + } + }; + let monitor = stream.monitor_disconnect()?; + // `Err` when the client is gone (or the daemon is stopping): nobody to tell. + let exit = crossbeam_channel::select_biased! { + recv(wait.exit) -> exit => Ok(exit.ok()), + recv(monitor.disconnected()) -> _ => Err(()), + recv(wait.actor_done) -> _ => Ok(wait.exit.try_recv().ok()), + }; + let response = match exit { + Err(()) => None, + Ok(Some(exit)) => Some(Response::from(exit)), + // Terminated before it exited; the daemon stopping is not news. + Ok(None) if control.stopping() => None, + Ok(None) => Some(Response::Error { + message: "terminated".to_string(), + }), + }; + let result = response.map_or(Ok(()), |response| write_frame(&mut *stream, &response)); + monitor.finish(); + result +} + fn send_response(stream: &mut Connection, response: &Response) -> Result<()> { write_frame(&mut *stream, response)?; stream.finish_response()?; @@ -551,34 +588,60 @@ fn dispatch( protocol: registration.protocol, features: registration.features.clone(), }, - Request::Create { + Request::CreatePty { program, args, cwd, - title, - group_id, env, + group_id, + title, cols, rows, - mode, - output_path, } => Response::Created { - terminal: service.create(CreateTerminal { + process: service.create_pty(CreatePty { program, args, cwd, - title, - group_id, env, + group_id, + title, cols, rows, - mode, + })?, + }, + Request::CreatePipe { + program, + args, + cwd, + env, + group_id, + output_path, + metadata, + } => Response::Created { + process: service.create_pipe(CreatePipe { + program, + args, + cwd, + env, + group_id, output_path, + metadata, })?, }, - Request::List { mode } => Response::Terminals { - terminals: service.list_mode(mode.unwrap_or_default())?, + Request::List { mode } => Response::Processes { + processes: service.list(mode)?, + }, + Request::Get { id } => Response::Process { + process: service.get(id)?, }, + Request::Signal { id, signal } => { + service.signal(id, signal)?; + Response::Ok + } + Request::SetMetadata { id, metadata } => { + service.set_metadata(id, metadata)?; + Response::Ok + } Request::Write { id, attachment_id, @@ -624,7 +687,7 @@ fn dispatch( Request::Snapshot { id } => { let snapshot = service.snapshot(id)?; Response::Snapshot { - terminal: snapshot.info, + process: snapshot.info, text: snapshot.text, checkpoint_base64: base64::engine::general_purpose::STANDARD .encode(snapshot.checkpoint), @@ -635,7 +698,7 @@ fn dispatch( Request::ReadRows { id, rows } => { let rows = service.read_rows(id, rows)?; Response::Rows { - terminal: rows.terminal, + process: rows.process, lines: rows.lines, cursor_x: rows.cursor_x, cursor_y: rows.cursor_y, @@ -655,7 +718,9 @@ fn dispatch( data_base64: base64::engine::general_purpose::STANDARD.encode(replay.bytes), } } - Request::Subscribe { .. } => unreachable!("subscriptions are handled before dispatch"), + Request::Subscribe { .. } | Request::Wait { .. } => { + unreachable!("streaming requests are handled before dispatch") + } Request::Terminate { id } => { service.terminate(id)?; Response::Ok @@ -678,6 +743,7 @@ mod tests { use crossbeam_channel::Receiver; use super::*; + use crate::protocol::{Envelope, read_frame}; const TEST_HANDOFF: Duration = Duration::from_secs(60); /// Long enough that only a real wake can make a test pass in time. @@ -947,6 +1013,201 @@ mod tests { assert_eq!(stream.read(&mut [0_u8; 1]).unwrap(), 0); } + #[test] + fn unknown_requests_get_an_error_reply() { + let daemon = TestDaemon::start(Duration::from_secs(60), TEST_HANDOFF, NO_SAFETY_NET); + let mut owner = daemon.claim(); + let mut stream = daemon.connect(); + write_frame( + &mut stream, + &serde_json::json!({ + "token": daemon.registration.token, + "request": {"op": "teleport"}, + }), + ) + .unwrap(); + assert!(matches!( + read_frame(&mut stream).unwrap(), + Response::Error { message } if message == "unsupported request: teleport" + )); + // A bad token is reported before the request is looked at. + let mut stream = daemon.connect(); + write_frame( + &mut stream, + &serde_json::json!({"token": "wrong", "request": {"op": "teleport"}}), + ) + .unwrap(); + assert!(matches!( + read_frame(&mut stream).unwrap(), + Response::Error { message } if message == "authentication failed" + )); + // The owner connection survives an unknown request too. + write_frame( + &mut owner, + &serde_json::json!({ + "token": daemon.registration.token, + "request": {"op": "teleport"}, + }), + ) + .unwrap(); + assert!(matches!( + read_frame(&mut owner).unwrap(), + Response::Error { message } if message == "unsupported request: teleport" + )); + assert!(matches!( + daemon.send(&mut owner, Request::PrepareHandoff), + Response::Handoff { .. } + )); + } + + fn create_pipe(daemon: &TestDaemon, script: &str) -> crate::service::ProcessInfo { + std::fs::create_dir_all(&daemon.root).unwrap(); + let output_path = daemon + .root + .join(format!("{:016x}.out", rand::random::())); + match daemon.send( + &mut daemon.connect(), + Request::CreatePipe { + program: "/bin/sh".into(), + args: vec!["-c".into(), script.into()], + cwd: "/".into(), + env: Default::default(), + group_id: "test".into(), + output_path, + metadata: Default::default(), + }, + ) { + Response::Created { process } => process, + response => panic!("create_pipe: {response:?}"), + } + } + + fn start_wait(daemon: &TestDaemon, id: u64) -> Connection { + let mut stream = daemon.connect(); + write_frame( + &mut stream, + &Envelope { + token: daemon.registration.token.clone(), + request: Request::Wait { id }, + }, + ) + .unwrap(); + stream + } + + #[test] + fn wait_replies_once_with_the_exit() { + let daemon = TestDaemon::start(Duration::from_secs(60), TEST_HANDOFF, NO_SAFETY_NET); + let _owner = daemon.claim(); + let process = create_pipe(&daemon, "sleep 0.2; echo one; exit 3"); + let mut waits = [ + start_wait(&daemon, process.id), + start_wait(&daemon, process.id), + ]; + for stream in &mut waits { + assert!(matches!( + read_frame(&mut *stream).unwrap(), + Response::Exited { + exit_code: Some(3), + output_lines: Some(1), + final_offset: 4, + .. + } + )); + // Then the connection closes. + assert_eq!(stream.read(&mut [0_u8; 1]).unwrap(), 0); + } + // Already exited: answered at once. + assert!(matches!( + read_frame(start_wait(&daemon, process.id)).unwrap(), + Response::Exited { + exit_code: Some(3), + .. + } + )); + } + + #[test] + fn wait_errors_for_unknown_ids_and_terminals() { + let daemon = TestDaemon::start(Duration::from_secs(60), TEST_HANDOFF, NO_SAFETY_NET); + let _owner = daemon.claim(); + assert!(matches!( + read_frame(start_wait(&daemon, 42)).unwrap(), + Response::Error { message } if message.contains("not found") + )); + let Response::Created { process } = daemon.send( + &mut daemon.connect(), + Request::CreatePty { + program: "/bin/sh".into(), + args: vec!["-c".into(), "exec cat".into()], + cwd: "/".into(), + env: Default::default(), + group_id: "test".into(), + title: "t".into(), + cols: 80, + rows: 24, + }, + ) else { + panic!("create_pty"); + }; + assert!(matches!( + read_frame(start_wait(&daemon, process.id)).unwrap(), + Response::Error { message } if message.contains("not a pipe process") + )); + } + + #[test] + fn terminate_ends_a_wait_with_an_error() { + let daemon = TestDaemon::start(Duration::from_secs(60), TEST_HANDOFF, NO_SAFETY_NET); + let _owner = daemon.claim(); + let process = create_pipe(&daemon, "exec sleep 30"); + let mut wait = start_wait(&daemon, process.id); + // Let the wait register before terminating. + thread::sleep(Duration::from_millis(100)); + assert!(matches!( + daemon.send(&mut daemon.connect(), Request::Terminate { id: process.id }), + Response::Ok + )); + assert!(matches!( + read_frame(&mut wait).unwrap(), + Response::Error { message } if message == "terminated" + )); + } + + #[test] + fn a_client_leaving_a_wait_ends_its_handler() { + let daemon = TestDaemon::start(Duration::from_secs(60), TEST_HANDOFF, NO_SAFETY_NET); + let _owner = daemon.claim(); + let process = create_pipe(&daemon, "exec sleep 30"); + thread::sleep(Duration::from_millis(100)); + let before = daemon.iterations(); + drop(start_wait(&daemon, process.id)); + // The handler's `Done` wakes the loop once it returns. + let deadline = Instant::now() + Duration::from_secs(3); + while daemon.iterations() < before + 2 { + assert!(Instant::now() < deadline, "wait handler did not return"); + thread::sleep(Duration::from_millis(10)); + } + // The process is untouched. + assert!(matches!( + daemon.send(&mut daemon.connect(), Request::Get { id: process.id }), + Response::Process { process: info } + if info.lifecycle == crate::service::TerminalLifecycle::Running + )); + } + + #[test] + fn daemon_shutdown_closes_a_wait_without_a_reply() { + let daemon = TestDaemon::start(Duration::from_secs(60), TEST_HANDOFF, NO_SAFETY_NET); + let _owner = daemon.claim(); + let process = create_pipe(&daemon, "exec sleep 30"); + let mut wait = start_wait(&daemon, process.id); + thread::sleep(Duration::from_millis(100)); + daemon.control.request_stop(); + daemon.stopped_within(Duration::from_secs(3)); + assert!(read_frame::(&mut wait).is_err()); + } + /// Breaks the wake invariant on purpose and expects the detector to catch it. fn expect_missed_wake(break_invariant: impl Fn(&Control, u64) -> bool) { let daemon = TestDaemon::start( diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index 82f5bebc78a5..0d42fced2490 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -1,18 +1,18 @@ +use std::collections::HashMap; use std::io::{Read, Write}; use std::path::PathBuf; use anyhow::{Context, Result, bail}; use serde::{Deserialize, Serialize}; -use crate::service::{ProcessMode, TerminalId, TerminalInfo}; +use crate::service::{ + PipeMetadata, ProcessExit, ProcessInfo, ProcessMode, ProcessSignal, TerminalId, +}; -pub const PROTOCOL_VERSION: u32 = 7; -/// Additive capabilities of this daemon. Breaking changes bump `PROTOCOL_VERSION`. -/// `pipe` (non-PTY processes) is Unix-only for now. -#[cfg(unix)] -pub const FEATURES: &[&str] = &["rename", "pipe", "output_path"]; -#[cfg(not(unix))] -pub const FEATURES: &[&str] = &["rename"]; +pub const PROTOCOL_VERSION: u32 = 8; +/// Additive capabilities of this daemon. Breaking changes bump +/// `PROTOCOL_VERSION`; everything in protocol 8 needs no feature. +pub const FEATURES: &[&str] = &[]; pub fn features() -> Vec { FEATURES.iter().map(|feature| feature.to_string()).collect() @@ -42,34 +42,46 @@ pub enum Request { ticket: Option, }, PrepareHandoff, - /// Old daemons ignore `mode` and would start a PTY, so clients send - /// `"pipe"` only to a daemon that lists the `pipe` feature. - Create { + CreatePty { program: String, args: Vec, cwd: PathBuf, - #[serde(default)] - title: String, + env: HashMap, group_id: String, - env: std::collections::HashMap, - /// Terminal size; required for `pty`, ignored for `pipe`. - #[serde(default)] + title: String, cols: u16, - #[serde(default)] rows: u16, - #[serde(default, skip_serializing_if = "ProcessMode::is_pty")] - mode: ProcessMode, - /// Pipe only: an absolute path the daemon appends every output byte - /// to. Send it only to a daemon that lists the `output_path` feature. - #[serde(default, skip_serializing_if = "Option::is_none")] - output_path: Option, }, - /// Without `mode`, lists PTYs only, so older servers never see pipe processes. - /// `mode` is left out of the JSON when unset: older daemons reject a `list` - /// with any extra key. + /// Unix only. The child gets exactly `env`; `cwd` and `output_path` must + /// be absolute. + CreatePipe { + program: String, + args: Vec, + cwd: PathBuf, + env: HashMap, + group_id: String, + output_path: PathBuf, + metadata: PipeMetadata, + }, List { - #[serde(default, skip_serializing_if = "Option::is_none")] - mode: Option, + mode: ProcessMode, + }, + Get { + id: TerminalId, + }, + /// Pipe only. Answered with one `Exited` once the process has exited. + Wait { + id: TerminalId, + }, + /// Pipe only. Signals the process group without removing the process. + Signal { + id: TerminalId, + signal: ProcessSignal, + }, + /// Pipe only. Replaces the metadata given to `create_pipe`. + SetMetadata { + id: TerminalId, + metadata: PipeMetadata, }, Write { id: TerminalId, @@ -135,25 +147,27 @@ pub enum Response { instance_id: String, pid: u32, protocol: u32, - #[serde(default)] features: Vec, }, Created { - terminal: TerminalInfo, + process: ProcessInfo, + }, + Process { + process: ProcessInfo, }, - Terminals { - terminals: Vec, + Processes { + processes: Vec, }, Ok, Snapshot { - terminal: TerminalInfo, + process: ProcessInfo, text: String, checkpoint_base64: String, cursor_x: u16, cursor_y: u16, }, Rows { - terminal: TerminalInfo, + process: ProcessInfo, lines: Vec, cursor_x: u16, cursor_y: u16, @@ -166,7 +180,7 @@ pub enum Response { data_base64: String, }, Attached { - terminal: TerminalInfo, + process: ProcessInfo, role: AttachmentRole, generation: u64, requested_offset: u64, @@ -187,10 +201,14 @@ pub enum Response { #[serde(default, skip_serializing_if = "Option::is_none")] signal: Option, final_offset: u64, - /// Same as `TerminalInfo.output_error`, so a live subscriber learns the - /// output file is incomplete without another request. + /// Pipe only: newlines in the output file. Omitted for PTYs. + #[serde(default, skip_serializing_if = "Option::is_none")] + output_lines: Option, + /// Same as `pipe.output_error`, so a waiter learns the output file is + /// incomplete without another request. #[serde(default, skip_serializing_if = "Option::is_none")] output_error: Option, + exited_at: u64, }, ControllerChanged { attachment_id: Option, @@ -207,6 +225,45 @@ pub enum Response { }, } +impl From for Response { + fn from(exit: ProcessExit) -> Self { + Response::Exited { + exit_code: exit.exit_code, + signal: exit.signal, + final_offset: exit.final_offset, + output_lines: exit.output_lines, + output_error: exit.output_error, + exited_at: exit.exited_at, + } + } +} + +/// Reads a request envelope. A request this daemon cannot parse (an unknown +/// `op`, or bad fields) is returned as an error message for the client +/// rather than failing the connection, so clients can probe for requests. +pub fn read_envelope(reader: impl Read) -> Result<(String, Result)> { + #[derive(Deserialize)] + struct RawEnvelope { + token: String, + request: serde_json::Value, + } + let raw: RawEnvelope = read_frame(reader)?; + let op = raw + .request + .get("op") + .and_then(serde_json::Value::as_str) + .unwrap_or_default() + .to_string(); + let request = serde_json::from_value::(raw.request).map_err(|error| { + if error.to_string().starts_with("unknown variant") { + format!("unsupported request: {op}") + } else { + format!("invalid {op} request: {error}") + } + }); + Ok((raw.token, request)) +} + pub fn write_frame(mut writer: impl Write, value: &impl Serialize) -> Result<()> { let payload = serde_json::to_vec(value)?; if payload.len() > MAX_FRAME_BYTES { @@ -285,172 +342,261 @@ fn read_payload(mut reader: impl Read) -> Result> { reader.read_exact(&mut payload)?; Ok(payload) } - #[cfg(test)] mod tests { + use serde_json::json; + use super::*; + use crate::service::{PipeInfo, PtyInfo, TerminalLifecycle}; + + fn request(value: serde_json::Value) -> Request { + serde_json::from_value(value).unwrap() + } + + /// Serializing must give back exactly the JSON that was parsed. + fn round_trip Deserialize<'de>>(value: serde_json::Value) -> T { + let parsed = serde_json::from_value::(value.clone()).unwrap(); + assert_eq!(serde_json::to_value(&parsed).unwrap(), value); + parsed + } #[test] - fn read_rows_accepts_optional_height_and_u16_counts() { - for json in [ - r#"{"op":"read_rows","id":1}"#, - r#"{"op":"read_rows","id":1,"rows":null}"#, - ] { - assert!(matches!( - serde_json::from_str::(json).unwrap(), - Request::ReadRows { id: 1, rows: None } - )); - } - for rows in [0, 1, u16::MAX] { - let value = serde_json::to_value(Request::ReadRows { - id: 1, - rows: Some(rows), - }) - .unwrap(); - assert_eq!( - value, - serde_json::json!({"op": "read_rows", "id": 1, "rows": rows}) - ); + fn version_8_has_no_features() { + assert_eq!(PROTOCOL_VERSION, 8); + assert!(features().is_empty()); + // `features` is required. + assert!( + serde_json::from_value::( + json!({"type": "pong", "instance_id": "a", "pid": 1, "protocol": 8}) + ) + .is_err() + ); + round_trip::( + json!({"type": "pong", "instance_id": "a", "pid": 1, "protocol": 8, "features": []}), + ); + } + + #[test] + fn create_requests_take_only_their_own_fields() { + let pty = json!({ + "op": "create_pty", "program": "sh", "args": ["-l"], "cwd": "/", + "env": {"A": "1"}, "group_id": "g", "title": "t", "cols": 80, "rows": 24, + }); + assert!(matches!( + round_trip::(pty.clone()), + Request::CreatePty { + cols: 80, + rows: 24, + .. + } + )); + for field in ["title", "cols", "rows", "group_id"] { + let mut missing = pty.clone(); + missing.as_object_mut().unwrap().remove(field); assert!( - matches!(serde_json::from_value::(value).unwrap(), Request::ReadRows { rows: Some(count), .. } if count == rows) + serde_json::from_value::(missing).is_err(), + "{field}" ); } - for rows in [-1, 65536] { + let pipe = json!({ + "op": "create_pipe", "program": "sh", "args": ["-c", "true"], "cwd": "/", + "env": {}, "group_id": "g", "output_path": "/tmp/out", + "metadata": {"command": "true", "nested": {"a": [1]}}, + }); + assert!(matches!( + round_trip::(pipe.clone()), + Request::CreatePipe { output_path, metadata, .. } + if output_path == std::path::Path::new("/tmp/out") && metadata["command"] == "true" + )); + for field in ["output_path", "metadata", "env"] { + let mut missing = pipe.clone(); + missing.as_object_mut().unwrap().remove(field); assert!( - serde_json::from_value::( - serde_json::json!({"op": "read_rows", "id": 1, "rows": rows}) - ) - .is_err() + serde_json::from_value::(missing).is_err(), + "{field}" ); } - assert_eq!(PROTOCOL_VERSION, 7); + let mut not_an_object = pipe.clone(); + not_an_object["metadata"] = json!("text"); + assert!(serde_json::from_value::(not_an_object).is_err()); + // The v7 `create` is gone. + assert!( + serde_json::from_value::(json!({ + "op": "create", "program": "sh", "args": [], "cwd": "/", "title": "t", + "group_id": "g", "env": {}, "cols": 80, "rows": 24, + })) + .is_err() + ); } #[test] - fn additive_fields_default_when_absent() { + fn process_requests() { assert!(matches!( - serde_json::from_str::( - r#"{"type":"pong","instance_id":"a","pid":1,"protocol":7}"# - ) - .unwrap(), - Response::Pong { features, .. } if features.is_empty() + round_trip::(json!({"op": "list", "mode": "pipe"})), + Request::List { + mode: ProcessMode::Pipe + } )); + assert!(serde_json::from_value::(json!({"op": "list"})).is_err()); assert!(matches!( - serde_json::from_str::(r#"{"op":"rename","id":1,"title":null}"#).unwrap(), - Request::Rename { id: 1, title: None } + round_trip::(json!({"op": "get", "id": 3})), + Request::Get { id: 3 } )); assert!(matches!( - serde_json::from_str::(r#"{"op":"rename","id":1}"#).unwrap(), - Request::Rename { id: 1, title: None } + round_trip::(json!({"op": "wait", "id": 3})), + Request::Wait { id: 3 } )); - let terminal = serde_json::json!({ - "id": 1, - "pid": null, - "title": "shell", - "foreground_process": null, - "group_id": "group", - "command": [], - "cwd": "/", - "cols": 80, - "rows": 24, - "lifecycle": {"status": "running"}, - "output_head": 0, - "output_tail": 0, - }); - let info = serde_json::from_value::(terminal).unwrap(); - assert_eq!(info.user_title, None); - assert_eq!( - serde_json::to_value(&info).unwrap()["user_title"], - serde_json::Value::Null - ); - } - - #[test] - fn pipe_fields_keep_the_wire_compatible_with_older_peers() { - use crate::service::{ProcessMode, TerminalLifecycle}; - - // An older daemon rejects `list` with any extra key, and an older - // client sends a bare `list` and a `create` with no mode. - assert_eq!( - serde_json::to_value(Request::List { mode: None }).unwrap(), - serde_json::json!({"op": "list"}) + for (name, signal) in [ + ("SIGTERM", ProcessSignal::Term), + ("SIGINT", ProcessSignal::Int), + ("SIGKILL", ProcessSignal::Kill), + ] { + assert!(matches!( + round_trip::(json!({"op": "signal", "id": 3, "signal": name})), + Request::Signal { id: 3, signal: parsed } if parsed == signal + )); + } + assert!( + serde_json::from_value::(json!({"op": "signal", "id": 3, "signal": "SIGHUP"})) + .is_err() ); assert!(matches!( - serde_json::from_str::(r#"{"op":"list"}"#).unwrap(), - Request::List { mode: None } + round_trip::(json!({"op": "set_metadata", "id": 3, "metadata": {"a": 1}})), + Request::SetMetadata { id: 3, metadata } if metadata["a"] == 1 )); assert!(matches!( - serde_json::from_str::(r#"{"op":"list","mode":"pipe"}"#).unwrap(), - Request::List { - mode: Some(ProcessMode::Pipe) - } + request(json!({"op": "read_rows", "id": 1})), + Request::ReadRows { id: 1, rows: None } + )); + assert!(matches!( + request(json!({"op": "rename", "id": 1})), + Request::Rename { id: 1, title: None } )); - let create = serde_json::json!({ - "op": "create", "program": "sh", "args": [], "cwd": "/", - "title": "t", "group_id": "g", "env": {}, "cols": 80, "rows": 24, + } + + #[test] + fn process_info_groups_by_mode() { + let pty = json!({ + "id": 1, "mode": "pty", "pid": 10, "command": ["sh"], "cwd": "/", + "group_id": "session", "lifecycle": {"status": "running"}, + "output_head": 0, "output_tail": 5, "started_at": 100, "exited_at": null, + "pty": { + "title": "sh", "user_title": null, "foreground_process": "vim", + "cols": 80, "rows": 24, + }, }); + let info = round_trip::(pty); + assert!(info.pipe.is_none()); + assert!(matches!(info.pty, Some(PtyInfo { cols: 80, .. }))); + let pipe = json!({ + "id": 2, "mode": "pipe", "pid": 11, "command": ["sh", "-c", "ls"], "cwd": "/", + "group_id": "project", "lifecycle": {"status": "exited", "exit_code": null, "signal": "SIGTERM"}, + "output_head": 7, "output_tail": 7, "started_at": 100, "exited_at": 200, + "pipe": { + "output_path": "/tmp/sh_1.out", "output_lines": 1, + "output_error": "failed to write output file: disk full", + "metadata": {"id": "sh_1"}, + }, + }); + let info = round_trip::(pipe.clone()); + assert!(info.pty.is_none()); assert!(matches!( - serde_json::from_value::(create.clone()).unwrap(), - Request::Create { - mode: ProcessMode::Pty, - cols: 80, + info.pipe, + Some(PipeInfo { + output_lines: 1, .. - } + }) )); - // A PTY create serializes exactly as before. assert_eq!( - serde_json::to_value(serde_json::from_value::(create.clone()).unwrap()) - .unwrap(), - create + info.lifecycle, + TerminalLifecycle::Exited { + exit_code: None, + signal: Some("SIGTERM".into()) + } ); + // `output_error` is omitted when there is none. + let mut clean = pipe; + clean["pipe"] + .as_object_mut() + .unwrap() + .remove("output_error"); + round_trip::(clean.clone()); + // `mode` and the timestamps are required. + for field in ["mode", "started_at"] { + let mut missing = clean.clone(); + missing.as_object_mut().unwrap().remove(field); + assert!( + serde_json::from_value::(missing).is_err(), + "{field}" + ); + } + } + + #[test] + fn responses_name_the_process() { + let info = json!({ + "id": 1, "mode": "pty", "pid": null, "command": [], "cwd": "/", + "group_id": "g", "lifecycle": {"status": "running"}, + "output_head": 0, "output_tail": 0, "started_at": 1, "exited_at": null, + "pty": {"title": "t", "user_title": null, "foreground_process": null, "cols": 1, "rows": 1}, + }); + for value in [ + json!({"type": "created", "process": info}), + json!({"type": "process", "process": info}), + json!({"type": "processes", "processes": [info]}), + json!({"type": "snapshot", "process": info, "text": "", "checkpoint_base64": "", "cursor_x": 0, "cursor_y": 0}), + json!({"type": "rows", "process": info, "lines": [], "cursor_x": 0, "cursor_y": 0}), + json!({ + "type": "attached", "process": info, "role": "observer", "generation": 0, + "requested_offset": 0, "available_offset": 0, "end_offset": 0, + "truncated": false, "replay_base64": "", + }), + ] { + round_trip::(value); + } + } + + #[test] + fn exited_carries_lines_only_for_pipes() { + let pty = json!({"type": "exited", "exit_code": 0, "final_offset": 3, "exited_at": 9}); assert!(matches!( - serde_json::from_str::( - r#"{"op":"create","program":"sh","args":[],"cwd":"/","group_id":"g","env":{},"mode":"pipe"}"# - ) - .unwrap(), - Request::Create { - mode: ProcessMode::Pipe, - cols: 0, - rows: 0, + round_trip::(pty), + Response::Exited { + output_lines: None, + exited_at: 9, .. } )); - assert!(matches!( - serde_json::from_str::( - r#"{"op":"create","program":"sh","args":[],"cwd":"/","group_id":"g","env":{},"mode":"pipe","output_path":"/tmp/out"}"# + let pipe = json!({ + "type": "exited", "exit_code": null, "signal": "SIGKILL", "final_offset": 3, + "output_lines": 2, "output_error": "failed", "exited_at": 9, + }); + round_trip::(pipe); + assert!( + serde_json::from_value::( + json!({"type": "exited", "exit_code": 0, "final_offset": 3}) ) - .unwrap(), - Request::Create { - output_path: Some(path), - .. - } if path == std::path::Path::new("/tmp/out") - )); - // Exits without a signal look exactly as before. - assert_eq!( - serde_json::to_value(Response::Exited { - exit_code: Some(1), - signal: None, - final_offset: 2, - output_error: None, - }) - .unwrap(), - serde_json::json!({"type": "exited", "exit_code": 1, "final_offset": 2}) - ); - assert_eq!( - serde_json::to_value(TerminalLifecycle::Exited { - exit_code: Some(1), - signal: None, - }) - .unwrap(), - serde_json::json!({"status": "exited", "exit_code": 1}) - ); - assert_eq!( - serde_json::from_str::(r#"{"status":"exited","exit_code":null}"#) - .unwrap(), - TerminalLifecycle::Exited { - exit_code: None, - signal: None, - } + .is_err() ); } + + #[test] + fn unknown_requests_are_reported_not_fatal() { + let parse = |request: serde_json::Value| { + let mut frame = Vec::new(); + write_frame(&mut frame, &json!({"token": "t", "request": request})).unwrap(); + read_envelope(frame.as_slice()).unwrap() + }; + let (token, request) = parse(json!({"op": "teleport", "id": 1})); + assert_eq!(token, "t"); + assert_eq!(request.unwrap_err(), "unsupported request: teleport"); + let (_, request) = parse(json!({"op": "get"})); + assert!(request.unwrap_err().starts_with("invalid get request")); + let (_, request) = parse(json!({"op": "ping"})); + assert!(matches!(request, Ok(Request::Ping))); + // A frame that is not an envelope at all still fails the connection. + let mut frame = Vec::new(); + write_frame(&mut frame, &json!({"request": {"op": "ping"}})).unwrap(); + assert!(read_envelope(frame.as_slice()).is_err()); + } } diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index dd04d9305a0e..31201b373d6c 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -37,11 +37,10 @@ pub type TerminalId = u64; /// How a process is attached. `Pty` runs under a pseudo-terminal parsed by /// libghostty; `Pipe` runs with stdin closed and stdout and stderr merged into -/// one pipe, and supports only output, replay, subscription and termination. -#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] +/// one pipe written to an output file. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum ProcessMode { - #[default] Pty, Pipe, } @@ -52,6 +51,22 @@ impl ProcessMode { } } +/// Opaque JSON object a client stores with a pipe process. +pub type PipeMetadata = serde_json::Map; +/// Largest accepted pipe metadata, measured as serialized JSON. +pub const MAX_METADATA_BYTES: usize = 1024 * 1024; + +/// Signals a client may send to a pipe process. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] +pub enum ProcessSignal { + #[serde(rename = "SIGTERM")] + Term, + #[serde(rename = "SIGINT")] + Int, + #[serde(rename = "SIGKILL")] + Kill, +} + #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "snake_case", tag = "status")] pub enum TerminalLifecycle { @@ -68,34 +83,68 @@ pub enum TerminalLifecycle { } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct TerminalInfo { +pub struct ProcessInfo { pub id: TerminalId, - #[serde(default)] pub mode: ProcessMode, pub pid: Option, + pub command: Vec, + pub cwd: PathBuf, + pub group_id: String, + pub lifecycle: TerminalLifecycle, + pub output_head: u64, + pub output_tail: u64, + /// Milliseconds since the Unix epoch. + pub started_at: u64, + /// Milliseconds since the Unix epoch, set once the exit is final (the + /// same moment `Exited` is sent), never before `started_at`. + pub exited_at: Option, + /// Set exactly when `mode` is `pty`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub pty: Option, + /// Set exactly when `mode` is `pipe`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub pipe: Option, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PtyInfo { /// Effective title: the user title, else the latest non-empty OSC title, /// else the title given at creation. pub title: String, - #[serde(default)] pub user_title: Option, pub foreground_process: Option, - pub group_id: String, - pub command: Vec, - pub cwd: PathBuf, pub cols: u16, pub rows: u16, - pub lifecycle: TerminalLifecycle, - pub output_head: u64, - pub output_tail: u64, - /// Set when writing a pipe process's `output_path` failed; the file then - /// lacks everything from that point on. Omitted when there is no error. +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PipeInfo { + pub output_path: PathBuf, + /// Newline bytes written to the output file so far. Updated together with + /// `output_tail`. + pub output_lines: u64, + /// Set when writing the output file failed; the file then lacks + /// everything from that point on. Omitted when there is no error. #[serde(default, skip_serializing_if = "Option::is_none")] pub output_error: Option, + pub metadata: PipeMetadata, +} + +/// The final state of a process, sent once to every waiter and subscriber. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ProcessExit { + pub exit_code: Option, + pub signal: Option, + pub final_offset: u64, + /// Pipe only. + pub output_lines: Option, + pub output_error: Option, + pub exited_at: u64, } #[derive(Clone, Debug)] pub struct TerminalSnapshot { - pub info: TerminalInfo, + pub info: ProcessInfo, pub text: String, pub checkpoint: Vec, pub cursor_x: u16, @@ -104,7 +153,7 @@ pub struct TerminalSnapshot { #[derive(Clone, Debug)] pub struct TerminalRows { - pub terminal: TerminalInfo, + pub process: ProcessInfo, pub lines: Vec, pub cursor_x: u16, pub cursor_y: u16, @@ -132,12 +181,7 @@ pub enum StreamEvent { generation: u64, checkpoint: Vec, }, - Exited { - exit_code: Option, - signal: Option, - final_offset: u64, - output_error: Option, - }, + Exited(ProcessExit), ControllerChanged { attachment_id: Option, generation: u64, @@ -151,7 +195,7 @@ pub enum StreamEvent { } pub struct TerminalAttachment { - pub terminal: TerminalInfo, + pub process: ProcessInfo, pub role: AttachmentRole, pub generation: u64, pub replay: RawReplay, @@ -173,22 +217,38 @@ impl Drop for TerminalAttachment { } } +/// A pending `wait` on a pipe process. +pub struct ProcessWait { + /// Receives the exit once. Disconnects without a value if the process is + /// terminated first. + pub exit: Receiver, + /// Disconnects when the process's actor stops (terminate or shutdown). + pub actor_done: Receiver<()>, +} + +impl ProcessWait { + /// Blocks until the process exits; errors if it is terminated first. + pub fn recv(&self) -> Result { + crossbeam_channel::select_biased! { + recv(self.exit) -> exit => exit.map_err(|_| anyhow!("terminated")), + recv(self.actor_done) -> _ => self.exit.try_recv().map_err(|_| anyhow!("terminated")), + } + } +} + #[derive(Clone, Debug)] -pub struct CreateTerminal { +pub struct CreatePty { pub program: String, pub args: Vec, pub cwd: PathBuf, - pub title: String, - pub group_id: String, pub env: HashMap, + pub group_id: String, + pub title: String, pub cols: u16, pub rows: u16, - pub mode: ProcessMode, - /// Pipe only: an absolute path to append every output byte to. - pub output_path: Option, } -impl CreateTerminal { +impl CreatePty { pub fn shell() -> Result { let program = default_shell(); let title = PathBuf::from(&program) @@ -200,17 +260,29 @@ impl CreateTerminal { program, args: Vec::new(), cwd: env::current_dir()?, - title, - group_id: format!("local:{}", env::current_dir()?.display()), env: HashMap::new(), + group_id: format!("local:{}", env::current_dir()?.display()), + title, cols: DEFAULT_COLS, rows: DEFAULT_ROWS, - mode: ProcessMode::Pty, - output_path: None, }) } } +#[derive(Clone, Debug)] +pub struct CreatePipe { + pub program: String, + pub args: Vec, + /// Absolute. + pub cwd: PathBuf, + /// The child's whole environment; nothing is inherited from the daemon. + pub env: HashMap, + pub group_id: String, + /// Absolute. Truncated on open; receives every output byte. + pub output_path: PathBuf, + pub metadata: PipeMetadata, +} + pub struct TerminalService { terminals: Mutex>>, created: AtomicU64, @@ -232,26 +304,35 @@ impl TerminalService { } } - pub fn create(&self, request: CreateTerminal) -> Result { - if let Some(path) = &request.output_path { - if request.mode.is_pty() { - bail!("output_path is only supported for pipe processes"); - } - if !path.is_absolute() { - bail!("output_path must be absolute"); - } - } - match request.mode { - ProcessMode::Pty => validate_size(request.cols, request.rows)?, - #[cfg(not(unix))] - ProcessMode::Pipe => bail!("pipe processes are not supported on this platform"), - #[cfg(unix)] - ProcessMode::Pipe => {} + pub fn create_pty(&self, request: CreatePty) -> Result { + validate_size(request.cols, request.rows)?; + validate_program(&request.program)?; + let replay_capacity = self.replay_capacity; + self.insert(|id, order| TerminalHandle::spawn_pty(id, order, request, replay_capacity)) + } + + /// Starts a pipe process. Unix only. + pub fn create_pipe(&self, request: CreatePipe) -> Result { + // Everything is checked before the output file is opened, so a + // rejected request never creates or truncates it. + validate_program(&request.program)?; + if !request.cwd.is_absolute() { + bail!("cwd must be absolute"); } - if request.program.trim().is_empty() { - bail!("terminal program cannot be empty"); + if !request.output_path.is_absolute() { + bail!("output_path must be absolute"); } + validate_metadata(&request.metadata)?; + #[cfg(unix)] + return self.insert(|id, order| TerminalHandle::spawn_pipe(id, order, request)); + #[cfg(not(unix))] + bail!("pipe processes are not supported on this platform"); + } + fn insert( + &self, + spawn: impl FnOnce(TerminalId, u64) -> Result, + ) -> Result { let id = loop { // JSON clients can represent integers exactly only through 53 bits. let id = rand::random::() & ((1_u64 << 53) - 1); @@ -266,7 +347,7 @@ impl TerminalService { } }; let order = self.created.fetch_add(1, Ordering::Relaxed); - let handle = TerminalHandle::spawn(id, order, request, self.replay_capacity)?; + let handle = spawn(id, order)?; let info = handle.info(); self.terminals .lock() @@ -275,12 +356,7 @@ impl TerminalService { Ok(info) } - /// Lists PTYs; see [`Self::list_mode`] for pipe processes. - pub fn list(&self) -> Result> { - self.list_mode(ProcessMode::Pty) - } - - pub fn list_mode(&self, mode: ProcessMode) -> Result> { + pub fn list(&self, mode: ProcessMode) -> Result> { // Filter before refreshing, so a busy actor of the other mode never // delays this listing. let mut handles = self @@ -292,17 +368,64 @@ impl TerminalService { .cloned() .collect::>(); handles.sort_by_key(|terminal| terminal.order); - let mut terminals = Vec::with_capacity(handles.len()); - for terminal in handles { - // Windows has no foreground query; do not wait behind actor - // backpressure just to read its already-cached metadata. - #[cfg(not(windows))] - if terminal.mode.is_pty() { - terminal.request(|reply| ActorMessage::RefreshForegroundProcess { reply })?; + handles + .into_iter() + .map(|terminal| terminal.refreshed_info()) + .collect() + } + + /// One process of either mode. + pub fn get(&self, id: TerminalId) -> Result { + self.handle(id)?.refreshed_info() + } + + /// Registers a waiter for a pipe process's exit. If it has already exited, + /// the exit is ready at once. + pub fn wait(&self, id: TerminalId) -> Result { + let process = self.pipe(id)?; + let (reply, exit) = bounded(1); + crossbeam_channel::select_biased! { + send(process.actor_tx, ActorMessage::Wait { reply }) -> result => { + result.map_err(|_| anyhow!("terminated"))?; } - terminals.push(terminal.info()); + recv(process.actor_done) -> _ => bail!("terminated"), } - Ok(terminals) + Ok(ProcessWait { + exit, + actor_done: process.actor_done.clone(), + }) + } + + /// Signals a pipe process and its process group. The process stays + /// listed; signalling one that already exited is allowed. + pub fn signal(&self, id: TerminalId, signal: ProcessSignal) -> Result<()> { + let process = self.pipe(id)?; + #[cfg(unix)] + return process + .process + .as_ref() + .ok_or_else(|| anyhow!("process {id} cannot be signalled"))? + .signal(signal); + #[cfg(not(unix))] + { + let _ = (process, signal); + bail!("pipe processes are not supported on this platform"); + } + } + + /// Replaces a pipe process's metadata. + pub fn set_metadata(&self, id: TerminalId, metadata: PipeMetadata) -> Result<()> { + validate_metadata(&metadata)?; + let process = self.pipe(id)?; + let mut info = process + .info + .write() + .map_err(|_| anyhow!("terminal info lock poisoned"))?; + info.pipe + .as_mut() + .ok_or_else(|| anyhow!("process {id} is not a pipe process"))? + .metadata = metadata; + Ok(()) } pub fn write(&self, id: TerminalId, bytes: Vec) -> Result<()> { @@ -404,7 +527,7 @@ impl TerminalService { } pub fn replay(&self, id: TerminalId, offset: u64) -> Result { - self.get(id)? + self.handle(id)? .request(|reply| ActorMessage::Replay { offset, reply }) } @@ -416,7 +539,7 @@ impl TerminalService { role: AttachmentRole, takeover: bool, ) -> Result { - let terminal = self.get(id)?; + let terminal = self.handle(id)?; let (reply_tx, reply_rx) = bounded(1); terminal .actor_tx @@ -430,7 +553,7 @@ impl TerminalService { .map_err(|_| anyhow!("terminal actor stopped"))?; let attached = await_reply(&reply_rx, &terminal.actor_done)?; Ok(TerminalAttachment { - terminal: terminal.info(), + process: terminal.info(), role, generation: attached.generation, replay: attached.replay, @@ -447,26 +570,34 @@ impl TerminalService { .lock() .map_err(|_| anyhow!("terminal registry lock poisoned"))? .remove(&id) - .ok_or_else(|| anyhow!("terminal {id} not found"))?; + .ok_or_else(|| anyhow!("process {id} not found"))?; terminal.shutdown() } - /// Like `get`, but for requests that need a terminal, which pipe processes lack. + /// Like `handle`, but for requests that need a terminal, which pipe processes lack. fn pty(&self, id: TerminalId) -> Result> { - let terminal = self.get(id)?; + let terminal = self.handle(id)?; if !terminal.mode.is_pty() { - bail!("terminal {id} is a pipe process, which has no terminal"); + bail!("process {id} is a pipe process, which has no terminal"); } Ok(terminal) } - fn get(&self, id: TerminalId) -> Result> { + fn pipe(&self, id: TerminalId) -> Result> { + let process = self.handle(id)?; + if process.mode != ProcessMode::Pipe { + bail!("process {id} is a terminal, not a pipe process"); + } + Ok(process) + } + + fn handle(&self, id: TerminalId) -> Result> { self.terminals .lock() .map_err(|_| anyhow!("terminal registry lock poisoned"))? .get(&id) .cloned() - .ok_or_else(|| anyhow!("terminal {id} not found")) + .ok_or_else(|| anyhow!("process {id} not found")) } pub(crate) fn shutdown(&self) { @@ -512,7 +643,10 @@ struct TerminalHandle { cancel: Mutex>>, /// Runs after the joins; reaps a pipe process kept as a zombie until now. reap: Mutex>>, - info: Arc>, + /// Pipe only: the child, for `signal`. + #[cfg(unix)] + process: Option>, + info: Arc>, joins: Mutex>>>, } @@ -532,7 +666,7 @@ fn start_actor( mode: ProcessMode, master: ActorMaster, writes: Sender, - info: &Arc>, + info: &Arc>, replay_capacity: usize, (cols, rows): (u16, u16), ) -> Result { @@ -586,25 +720,10 @@ fn start_actor( } impl TerminalHandle { - fn spawn( - id: TerminalId, - order: u64, - request: CreateTerminal, - replay_capacity: usize, - ) -> Result { - match request.mode { - ProcessMode::Pty => Self::spawn_pty(id, order, request, replay_capacity), - #[cfg(unix)] - ProcessMode::Pipe => Self::spawn_pipe(id, order, request, replay_capacity), - #[cfg(not(unix))] - ProcessMode::Pipe => bail!("pipe processes are not supported on this platform"), - } - } - fn spawn_pty( id: TerminalId, order: u64, - request: CreateTerminal, + request: CreatePty, replay_capacity: usize, ) -> Result { let pty_system = native_pty_system(); @@ -642,7 +761,24 @@ impl TerminalHandle { .context("failed to take PTY writer")?; drop(pair.slave); - let info = new_info(id, ProcessMode::Pty, pid, &request); + let info = Arc::new(RwLock::new(ProcessInfo { + pty: Some(PtyInfo { + title: request.title.clone(), + user_title: None, + foreground_process: None, + cols: request.cols, + rows: request.rows, + }), + ..new_info( + id, + ProcessMode::Pty, + pid, + &request.program, + &request.args, + &request.cwd, + &request.group_id, + ) + })); let (writer_tx, writer_rx) = bounded(WRITER_QUEUE_CAPACITY); #[cfg(windows)] @@ -691,7 +827,7 @@ impl TerminalHandle { Ok(length) => { if forwarding { forwarding = reader_actor_tx - .send(ActorMessage::Output(buffer[..length].to_vec())) + .send(ActorMessage::output(buffer[..length].to_vec())) .is_ok(); #[cfg(unix)] if !forwarding { @@ -744,6 +880,8 @@ impl TerminalHandle { killer: Mutex::new(killer), cancel: Mutex::new(None), reap: Mutex::new(None), + #[cfg(unix)] + process: None, info, joins: Mutex::new(Some(vec![ actor.thread, @@ -754,13 +892,24 @@ impl TerminalHandle { }) } - fn info(&self) -> TerminalInfo { + fn info(&self) -> ProcessInfo { self.info .read() .map(|info| info.clone()) .unwrap_or_else(|poisoned| poisoned.into_inner().clone()) } + /// The info, with a PTY's foreground process refreshed first. + fn refreshed_info(&self) -> Result { + // Windows has no foreground query; do not wait behind actor + // backpressure just to read its already-cached metadata. + #[cfg(not(windows))] + if self.mode.is_pty() { + self.request(|reply| ActorMessage::RefreshForegroundProcess { reply })?; + } + Ok(self.info()) + } + fn request(&self, make: impl FnOnce(Reply>) -> ActorMessage) -> Result { let (reply_tx, reply_rx) = bounded(1); self.actor_tx @@ -805,40 +954,40 @@ impl TerminalHandle { } } +/// Info for a new process, without its `pty` or `pipe` group. fn new_info( id: TerminalId, mode: ProcessMode, pid: Option, - request: &CreateTerminal, -) -> Arc> { - let title = match mode { - ProcessMode::Pipe if request.title.is_empty() => PathBuf::from(&request.program) - .file_name() - .and_then(|value| value.to_str()) - .unwrap_or(&request.program) - .to_string(), - _ => request.title.clone(), - }; - Arc::new(RwLock::new(TerminalInfo { + program: &str, + args: &[String], + cwd: &std::path::Path, + group_id: &str, +) -> ProcessInfo { + ProcessInfo { id, mode, pid, - title, - user_title: None, - foreground_process: None, - group_id: request.group_id.clone(), - command: std::iter::once(request.program.clone()) - .chain(request.args.iter().cloned()) + command: std::iter::once(program.to_string()) + .chain(args.iter().cloned()) .collect(), - cwd: request.cwd.clone(), - // Pipe processes have no terminal size. - cols: if mode.is_pty() { request.cols } else { 0 }, - rows: if mode.is_pty() { request.rows } else { 0 }, + cwd: cwd.to_path_buf(), + group_id: group_id.to_string(), lifecycle: TerminalLifecycle::Running, output_head: 0, output_tail: 0, - output_error: None, - })) + started_at: unix_ms(), + exited_at: None, + pty: None, + pipe: None, + } +} + +fn unix_ms() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_millis() as u64) + .unwrap_or_default() } type Reply = Sender; @@ -857,7 +1006,12 @@ fn await_reply(reply: &Receiver>, actor_done: &Receiver<()>) -> Res } enum ActorMessage { - Output(Vec), + /// `newlines` counts the `\n` bytes of `bytes` that reached the output + /// file; always 0 for PTYs. + Output { + bytes: Vec, + newlines: u64, + }, ReaderEof, ReaderFailed(String), WriterFailed(String), @@ -915,6 +1069,16 @@ enum ActorMessage { Detach { attachment_id: String, }, + /// Replies once with the final exit, at once if it is already known. + Wait { + reply: Sender, + }, +} + +impl ActorMessage { + fn output(bytes: Vec) -> Self { + Self::Output { bytes, newlines: 0 } + } } struct Attached { @@ -986,7 +1150,7 @@ struct ActorConfig { messages: Receiver, writes: Sender, shutdown: Receiver<()>, - info: Arc>, + info: Arc>, replay_capacity: usize, cols: u16, rows: u16, @@ -1021,8 +1185,10 @@ fn run_actor(config: ActorConfig) -> Result<()> { let create_title = info .read() .map_err(|_| anyhow!("terminal info lock poisoned"))? - .title - .clone(); + .pty + .as_ref() + .map(|pty| pty.title.clone()) + .unwrap_or_default(); let mut osc_title = None::; let mut replay = ReplayBuffer::new(replay_capacity); let mut subscribers = HashMap::::new(); @@ -1030,7 +1196,9 @@ fn run_actor(config: ActorConfig) -> Result<()> { let mut controller_generation = 0_u64; let mut child_exit = None::, Option), String>>; let mut reader_eof = false; - let mut final_exit = None::; + let mut final_exit = None::; + let mut waiters = Vec::>::new(); + let mut output_lines = 0_u64; let mut pending_message = None; let _ = init_tx.send(Ok(())); @@ -1050,8 +1218,9 @@ fn run_actor(config: ActorConfig) -> Result<()> { } let _ = reply.send(Ok(())); } - Ok(ActorMessage::Output(bytes)) => { + Ok(ActorMessage::Output { bytes, newlines }) => { let (start, end) = replay.append(&bytes); + output_lines += newlines; if let Some(terminal) = terminal.as_mut() { terminal.vt_write(&bytes); // Once the Windows master is handed off for closing, the writer @@ -1067,7 +1236,7 @@ fn run_actor(config: ActorConfig) -> Result<()> { break Err(error); } } - update_offsets(&info, &replay); + update_offsets(&info, &replay, output_lines); broadcast( &shutdown, &mut subscribers, @@ -1121,9 +1290,11 @@ fn run_actor(config: ActorConfig) -> Result<()> { let resized = terminal.resize(cols, rows); forward_replies(terminal, &writes, &shutdown)?; resized?; - if let Ok(mut value) = info.write() { - value.cols = cols; - value.rows = rows; + if let Ok(mut value) = info.write() + && let Some(pty) = value.pty.as_mut() + { + pty.cols = cols; + pty.rows = rows; } let generation = controller_generation; let checkpoint = format_terminal(terminal, Format::Vt)?.into_bytes(); @@ -1180,7 +1351,12 @@ fn run_actor(config: ActorConfig) -> Result<()> { } let changed = info .read() - .map(|value| value.cols != cols || value.rows != rows) + .map(|value| { + value + .pty + .as_ref() + .is_none_or(|pty| pty.cols != cols || pty.rows != rows) + }) .unwrap_or(true); if changed { let terminal = emulator(&mut terminal)?; @@ -1193,9 +1369,11 @@ fn run_actor(config: ActorConfig) -> Result<()> { let resized = terminal.resize(cols, rows); forward_replies(terminal, &writes, &shutdown)?; resized?; - if let Ok(mut value) = info.write() { - value.cols = cols; - value.rows = rows; + if let Ok(mut value) = info.write() + && let Some(pty) = value.pty.as_mut() + { + pty.cols = cols; + pty.rows = rows; } let generation = controller_generation; let checkpoint = format_terminal(terminal, Format::Vt)?.into_bytes(); @@ -1231,7 +1409,7 @@ fn run_actor(config: ActorConfig) -> Result<()> { let terminal = emulator(&mut terminal)?; Ok(TerminalRows { lines: format_rows(terminal, rows)?, - terminal: info + process: info .read() .map_err(|_| anyhow!("terminal info lock poisoned"))? .clone(), @@ -1318,10 +1496,10 @@ fn run_actor(config: ActorConfig) -> Result<()> { }, ); } - if let Some(event) = &final_exit { + if let Some(exit) = &final_exit { // This fresh receiver is still actor-owned and has at // most its initial ControllerChanged event queued. - let _ = events_tx.send(event.clone()); + let _ = events_tx.send(StreamEvent::Exited(exit.clone())); } Ok(Attached { generation: controller_generation, @@ -1360,10 +1538,18 @@ fn run_actor(config: ActorConfig) -> Result<()> { ); } } + Ok(ActorMessage::Wait { reply }) => match &final_exit { + Some(exit) => { + let _ = reply.try_send(exit.clone()); + } + None => waiters.push(reply), + }, #[cfg(unix)] Ok(ActorMessage::OutputFileFailed(message)) => { - if let Ok(mut value) = info.write() { - value.output_error = Some(message); + if let Ok(mut value) = info.write() + && let Some(pipe) = value.pipe.as_mut() + { + pipe.output_error = Some(message); } } #[cfg(unix)] @@ -1380,6 +1566,14 @@ fn run_actor(config: ActorConfig) -> Result<()> { break Err(error); } } + Ok(ActorMessage::ReaderFailed(message)) if mode == ProcessMode::Pipe => { + // The pipe reader has stopped and closed the output file, so + // nothing more can arrive: let the exit still be published. + reader_eof = true; + if let Ok(mut value) = info.write() { + value.lifecycle = TerminalLifecycle::Failed { message }; + } + } Ok(ActorMessage::ReaderFailed(message)) | Ok(ActorMessage::WriterFailed(message)) => { // Closing ConPTY can finish a pending write with BrokenPipe // after the final exit event. Never overwrite a published exit. @@ -1400,28 +1594,37 @@ fn run_actor(config: ActorConfig) -> Result<()> { && let Some(result) = child_exit.take() { let (exit_code, signal) = result.clone().unwrap_or_default(); - let mut output_error = None; + let mut exit = ProcessExit { + exit_code, + signal, + final_offset: replay.tail, + output_lines: (mode == ProcessMode::Pipe).then_some(output_lines), + output_error: None, + exited_at: unix_ms(), + }; if let Ok(mut value) = info.write() { value.lifecycle = match result { Ok((exit_code, signal)) => TerminalLifecycle::Exited { exit_code, signal }, Err(message) => TerminalLifecycle::Failed { message }, }; - output_error = value.output_error.clone(); + exit.exited_at = exit.exited_at.max(value.started_at); + value.exited_at = Some(exit.exited_at); + exit.output_error = value + .pipe + .as_ref() + .and_then(|pipe| pipe.output_error.clone()); + } + for waiter in waiters.drain(..) { + let _ = waiter.try_send(exit.clone()); } - let event = StreamEvent::Exited { - exit_code, - signal, - final_offset: replay.tail, - output_error, - }; broadcast( &shutdown, &mut subscribers, &mut controller, &mut controller_generation, - event.clone(), + StreamEvent::Exited(exit.clone()), ); - final_exit = Some(event); + final_exit = Some(exit); } }; // Release senders blocked on the actor queue before closing ConPTY. Its @@ -1465,13 +1668,23 @@ fn receive_actor_message( recv(messages) -> message => message.map_err(|_| crossbeam_channel::RecvTimeoutError::Disconnected)?, }, }; - let ActorMessage::Output(mut bytes) = message else { + let ActorMessage::Output { + mut bytes, + mut newlines, + } = message + else { return Ok(message); }; let deadline = Instant::now() + OUTPUT_BATCH_DELAY; while bytes.len() < OUTPUT_BATCH_BYTES { match messages.recv_deadline(deadline) { - Ok(ActorMessage::Output(next)) => bytes.extend(next), + Ok(ActorMessage::Output { + bytes: next, + newlines: next_newlines, + }) => { + bytes.extend(next); + newlines += next_newlines; + } Ok(message) => { *pending = Some(message); break; @@ -1480,20 +1693,22 @@ fn receive_actor_message( | Err(crossbeam_channel::RecvTimeoutError::Disconnected) => break, } } - Ok(ActorMessage::Output(bytes)) + Ok(ActorMessage::Output { bytes, newlines }) } #[cfg(not(windows))] fn publish_foreground_process( shutdown: &Receiver<()>, master: &dyn MasterPty, - info: &Arc>, + info: &Arc>, subscribers: &mut HashMap, controller: &mut Option, controller_generation: &mut u64, ) { let process = foreground_process(master); - let changed = if let Ok(mut value) = info.write() { + let changed = if let Ok(mut value) = info.write() + && let Some(value) = value.pty.as_mut() + { if value.foreground_process == process { false } else { @@ -1554,7 +1769,7 @@ fn foreground_process(_master: &dyn MasterPty) -> Option { } #[cfg(target_os = "linux")] -struct ProcessInfo { +struct ProcStat { pid: i32, ppid: i32, pgrp: i32, @@ -1562,13 +1777,13 @@ struct ProcessInfo { } #[cfg(target_os = "linux")] -fn read_process(pid: i32) -> Option { +fn read_process(pid: i32) -> Option { let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; let fields = stat .get(stat.rfind(") ")? + 2..)? .split_whitespace() .collect::>(); - Some(ProcessInfo { + Some(ProcStat { pid, ppid: fields.get(1)?.parse().ok()?, pgrp: fields.get(2)?.parse().ok()?, @@ -1651,7 +1866,7 @@ fn run_writer( /// Applies an optional user-title update and stores the effective title under /// one write guard, returning the title only when it changed. fn refresh_title( - info: &Arc>, + info: &Arc>, user_title: Option>, osc_title: Option<&str>, create_title: &str, @@ -1659,6 +1874,9 @@ fn refresh_title( let mut value = info .write() .map_err(|_| anyhow!("terminal info lock poisoned"))?; + let Some(value) = value.pty.as_mut() else { + return Ok(None); + }; if let Some(user_title) = user_title { value.user_title = user_title; } @@ -1675,10 +1893,7 @@ fn refresh_title( Ok(Some(title)) } -fn make_snapshot( - terminal: &Terminal, - info: &Arc>, -) -> Result { +fn make_snapshot(terminal: &Terminal, info: &Arc>) -> Result { let text = format_terminal(terminal, Format::Plain)?; let checkpoint = format_terminal(terminal, Format::Vt)?.into_bytes(); let info = info @@ -1740,10 +1955,13 @@ fn format_terminal(terminal: &Terminal, format: Format) -> Result { )) } -fn update_offsets(info: &Arc>, replay: &ReplayBuffer) { +fn update_offsets(info: &Arc>, replay: &ReplayBuffer, output_lines: u64) { if let Ok(mut info) = info.write() { info.output_head = replay.head; info.output_tail = replay.tail; + if let Some(pipe) = info.pipe.as_mut() { + pipe.output_lines = output_lines; + } } } @@ -1885,6 +2103,20 @@ fn authorize_controller(controller: &Option, attachment_id: Option<&str> Ok(()) } +fn validate_program(program: &str) -> Result<()> { + if program.trim().is_empty() { + bail!("program cannot be empty"); + } + Ok(()) +} + +fn validate_metadata(metadata: &PipeMetadata) -> Result<()> { + if serde_json::to_vec(metadata)?.len() > MAX_METADATA_BYTES { + bail!("metadata exceeds {MAX_METADATA_BYTES} byte limit"); + } + Ok(()) +} + fn validate_size(cols: u16, rows: u16) -> Result<()> { if cols == 0 || rows == 0 { bail!("terminal dimensions must be positive"); @@ -1947,23 +2179,24 @@ mod tests { } } - fn test_info() -> TerminalInfo { - TerminalInfo { - id: 1, - mode: ProcessMode::Pty, - pid: None, - title: "test".into(), - user_title: None, - foreground_process: None, - group_id: "test".into(), - command: vec![], - cwd: PathBuf::new(), - cols: 80, - rows: 24, - lifecycle: TerminalLifecycle::Running, - output_head: 0, - output_tail: 0, - output_error: None, + fn test_info() -> ProcessInfo { + ProcessInfo { + pty: Some(PtyInfo { + title: "test".into(), + user_title: None, + foreground_process: None, + cols: 80, + rows: 24, + }), + ..new_info( + 1, + ProcessMode::Pty, + None, + "test", + &[], + std::path::Path::new(""), + "test", + ) } } @@ -2016,6 +2249,8 @@ mod tests { killer: Mutex::new(Box::new(TestKiller)), cancel: Mutex::new(None), reap: Mutex::new(None), + #[cfg(unix)] + process: None, info, joins: Mutex::new(Some(vec![ actor, @@ -2036,11 +2271,12 @@ mod tests { service.terminals.lock().unwrap().insert(1, Arc::new(actor)); // Even a closed actor channel is irrelevant to cached Windows metadata. - let terminals = service.list().unwrap(); + let terminals = service.list(ProcessMode::Pty).unwrap(); assert_eq!(terminals.len(), 1); assert_eq!(terminals[0].id, 1); - assert_eq!(terminals[0].title, "test"); - assert!(terminals[0].foreground_process.is_none()); + let pty = terminals[0].pty.as_ref().unwrap(); + assert_eq!(pty.title, "test"); + assert!(pty.foreground_process.is_none()); } #[test] @@ -2062,7 +2298,7 @@ mod tests { if !eof_first { actor .actor_tx - .send(ActorMessage::Output(b"final".to_vec())) + .send(ActorMessage::output(b"final".to_vec())) .unwrap(); } actor @@ -2103,7 +2339,7 @@ mod tests { fn late_attachments_receive_final_exit_after_replay_and_control() { let (actor, _writer) = test_actor(); for message in [ - ActorMessage::Output(b"final".to_vec()), + ActorMessage::output(b"final".to_vec()), ActorMessage::ChildExited(Ok(Some(23))), ActorMessage::ReaderEof, ] { @@ -2145,12 +2381,14 @@ mod tests { } assert!(matches!( events.as_slice(), - [StreamEvent::Exited { + [StreamEvent::Exited(ProcessExit { exit_code: Some(23), signal: None, final_offset: 5, + output_lines: None, output_error: None, - }] + .. + })] )); } } @@ -2181,7 +2419,7 @@ mod tests { let retained_events = events.clone(); let (disconnect_tx, disconnected) = bounded(0); let attachment = TerminalAttachment { - terminal: test_info(), + process: test_info(), role: AttachmentRole::Controller, generation: 1, replay: ReplayBuffer::new(0).read_from(0).unwrap(), @@ -2301,7 +2539,7 @@ mod tests { let osc = |title: &str| { actor .actor_tx - .send(ActorMessage::Output( + .send(ActorMessage::output( format!("\x1b]2;{title}\x07").into_bytes(), )) .unwrap() @@ -2311,6 +2549,8 @@ mod tests { .request(|reply| ActorMessage::Snapshot { reply }) .unwrap() .info + .pty + .unwrap() }; osc("shell"); @@ -2410,7 +2650,7 @@ mod tests { for _ in 1..SUBSCRIBER_QUEUE_CAPACITY { actor .actor_tx - .send(ActorMessage::Output(b"x".to_vec())) + .send(ActorMessage::output(b"x".to_vec())) .unwrap(); actor .request(|reply| ActorMessage::Replay { offset: 0, reply }) @@ -2419,7 +2659,7 @@ mod tests { assert!(attached.events.is_full()); actor .actor_tx - .send(ActorMessage::Output(b"!".to_vec())) + .send(ActorMessage::output(b"!".to_vec())) .unwrap(); let (done, finished) = std_mpsc::channel(); let shutdown = thread::spawn(move || { @@ -2838,17 +3078,23 @@ mod tests { let (_shutdown_tx, shutdown) = bounded(0); let (messages_tx, messages) = bounded(4); messages_tx - .send(ActorMessage::Output(b"abc".to_vec())) + .send(ActorMessage::Output { + bytes: b"a\nc".to_vec(), + newlines: 1, + }) .unwrap(); messages_tx - .send(ActorMessage::Output(b"def".to_vec())) + .send(ActorMessage::Output { + bytes: b"\n\nf".to_vec(), + newlines: 2, + }) .unwrap(); messages_tx.send(ActorMessage::ReaderEof).unwrap(); let mut pending = None; assert!(matches!( receive_actor_message(&messages, &mut pending, &shutdown).unwrap(), - ActorMessage::Output(bytes) if bytes == b"abcdef" + ActorMessage::Output { bytes, newlines: 3 } if bytes == b"a\nc\n\nf" )); assert!(matches!( receive_actor_message(&messages, &mut pending, &shutdown).unwrap(), diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 1f4bd299f949..d85aa1389121 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -11,9 +11,9 @@ //! - A descendant can keep the pipe open after the child exits (`cmd &`, build //! servers). The reader stops [`DRAIN_AFTER_EXIT`] after the exit instead of //! waiting for an EOF that may never come. -//! - With `output_path`, the reader appends every byte to that file before -//! handing it to the actor, so output survives the 2 MiB replay window, -//! arrives before any subscriber, and stays complete across a handoff. +//! - The reader writes every byte to the output file before handing it to the +//! actor, which keeps no replay buffer: the file is the only copy of the +//! output. Leftover writers get `SIGPIPE` once the reader stops. //! - The reader polls a cancel pipe too, so shutdown never waits on a pipe that //! a descendant which left the process group still holds open. @@ -27,15 +27,15 @@ use std::sync::{Arc, Mutex, PoisonError, RwLock}; use std::thread; use std::time::{Duration, Instant}; -use anyhow::{Context, Result}; +use anyhow::{Context, Result, bail}; use crossbeam_channel::{Sender, bounded}; use nix::errno::Errno; use nix::poll::{PollFd, PollFlags, PollTimeout, poll}; use portable_pty::ChildKiller; use super::{ - ActorMaster, ActorMessage, CreateTerminal, ProcessMode, TerminalHandle, TerminalId, new_info, - start_actor, + ActorMaster, ActorMessage, CreatePipe, PipeInfo, ProcessInfo, ProcessMode, ProcessSignal, + TerminalHandle, TerminalId, new_info, start_actor, }; /// How long output is still read after the child exits. @@ -80,12 +80,7 @@ impl Pipes { } impl TerminalHandle { - pub(super) fn spawn_pipe( - id: TerminalId, - order: u64, - request: CreateTerminal, - replay_capacity: usize, - ) -> Result { + pub(super) fn spawn_pipe(id: TerminalId, order: u64, request: CreatePipe) -> Result { let Pipes { output, output_writer, @@ -96,24 +91,25 @@ impl TerminalHandle { exited_writer, } = Pipes::new()?; // Before spawning, so a bad path fails the request with nothing to clean up. - let sink = request - .output_path - .as_deref() - .map(|path| { - OpenOptions::new() - .create(true) - .append(true) - .mode(0o600) - // Never write through a symlink planted at the final component. - .custom_flags(libc::O_NOFOLLOW) - .open(path) - .with_context(|| format!("failed to open output file {}", path.display())) - }) - .transpose()?; + // The reader is the file's only writer, so it needs no append mode. + let sink = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + // Never write through a symlink planted at the final component. + .custom_flags(libc::O_NOFOLLOW) + .open(&request.output_path) + .with_context(|| { + format!( + "failed to open output file {}", + request.output_path.display() + ) + })?; let mut command = Command::new(&request.program); command .args(&request.args) .current_dir(&request.cwd) + .env_clear() .envs(&request.env) .stdin(Stdio::null()) .stdout(output_writer) @@ -138,7 +134,23 @@ impl TerminalHandle { // Dropping `Child` neither waits nor kills; `Process` reaps it. drop(child); - let info = new_info(id, ProcessMode::Pipe, Some(process.pid), &request); + let info = Arc::new(RwLock::new(ProcessInfo { + pipe: Some(PipeInfo { + output_path: request.output_path.clone(), + output_lines: 0, + output_error: None, + metadata: request.metadata, + }), + ..new_info( + id, + ProcessMode::Pipe, + Some(process.pid), + &request.program, + &request.args, + &request.cwd, + &request.group_id, + ) + })); // Nothing is ever written to a pipe process; a disconnected queue // turns any stray write into an error. let (writes, _) = bounded(1); @@ -148,7 +160,8 @@ impl TerminalHandle { ActorMaster { pty: None }, writes, &info, - replay_capacity, + // The output file is the only buffer. + 0, (0, 0), ) { Ok(actor) => actor, @@ -210,6 +223,7 @@ impl TerminalHandle { killer: Mutex::new(Box::new(GroupKiller(Arc::clone(&process)))), cancel: Mutex::new(Some(Box::new(cancel_writer))), reap: Mutex::new(Some(Box::new(move || reap.reap()))), + process: Some(process), info, joins: Mutex::new(Some(joins)), }) @@ -245,7 +259,7 @@ fn reset_signals() -> io::Result<()> { /// A pipe child that stays a zombie after it exits, until `reap`. #[derive(Debug)] -struct Process { +pub(super) struct Process { pid: u32, reaped: Mutex, } @@ -265,6 +279,30 @@ impl Process { } } + /// Sends `signal` to the process group and the child. Allowed after the + /// exit, to reach what is left of the group; refused after `reap`. + pub(super) fn signal(&self, signal: ProcessSignal) -> Result<()> { + let signal = match signal { + ProcessSignal::Term => libc::SIGTERM, + ProcessSignal::Int => libc::SIGINT, + ProcessSignal::Kill => libc::SIGKILL, + }; + let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner); + if *reaped { + bail!("process has been reaped"); + } + let pid = i32::try_from(self.pid).context("invalid pid")?; + // SAFETY: the unreaped child keeps its PID and group ID reserved. + // Signal the child directly too, in case it moved to another group. + for result in unsafe { [libc::killpg(pid, signal), libc::kill(pid, signal)] } { + // A zombie group with no live members gives ESRCH on macOS. + if result == -1 && Errno::last() != Errno::ESRCH { + return Err(io::Error::last_os_error()).context("failed to signal process"); + } + } + Ok(()) + } + /// Call only after the waiter has returned: kill the group first, so the /// child has exited and this does not block. fn reap(&self) { @@ -368,12 +406,14 @@ fn signal_name(signal: i32) -> String { /// deadline. fn read_output( output: PipeReader, - mut sink: Option, + sink: File, cancel: PipeReader, exited: PipeReader, actor: Sender, ) { let mut output = output; + // Dropped after a write error: the rest is forwarded but not counted. + let mut sink = Some(sink); let mut buffer = [0_u8; 8192]; let mut deadline = None::; let mut writers_closed = false; @@ -415,6 +455,8 @@ fn read_output( match poll(&mut fds[..count], timeout) { Err(Errno::EINTR) | Ok(0) => continue, Err(error) => { + // Close the file before anyone can observe the exit. + drop(sink); let _ = actor.send(ActorMessage::ReaderFailed(error.to_string())); return; } @@ -453,8 +495,9 @@ fn read_output( Ok(length) => { // The file first, so it always holds at least what // subscribers have seen, even if the actor is backpressured. + let mut newlines = 0; if let Some(file) = &mut sink - && let Err(error) = file.write_all(&buffer[..length]) + && let Err(error) = write_counting(file, &buffer[..length], &mut newlines) { // Ordered before this chunk's output, so the error is // visible no later than the bytes the file lacks. @@ -469,7 +512,10 @@ fn read_output( } } if actor - .send(ActorMessage::Output(buffer[..length].to_vec())) + .send(ActorMessage::Output { + bytes: buffer[..length].to_vec(), + newlines, + }) .is_err() { return; @@ -477,6 +523,7 @@ fn read_output( } Err(error) if error.kind() == io::ErrorKind::Interrupted => {} Err(error) => { + drop(sink); let _ = actor.send(ActorMessage::ReaderFailed(error.to_string())); return; } @@ -484,3 +531,24 @@ fn read_output( } } } + +/// Writes all of `bytes`, adding to `newlines` the `\n` bytes that reached +/// the file, including those of a partial write before an error. +fn write_counting(file: &mut File, bytes: &[u8], newlines: &mut u64) -> io::Result<()> { + let mut rest = bytes; + while !rest.is_empty() { + match file.write(rest) { + Ok(0) => return Err(io::ErrorKind::WriteZero.into()), + Ok(written) => { + *newlines += rest[..written] + .iter() + .filter(|&&byte| byte == b'\n') + .count() as u64; + rest = &rest[written..]; + } + Err(error) if error.kind() == io::ErrorKind::Interrupted => {} + Err(error) => return Err(error), + } + } + Ok(()) +} diff --git a/packages/process-daemon/src/transport/unix.rs b/packages/process-daemon/src/transport/unix.rs index d4ee215bda2d..6faed82f3b43 100644 --- a/packages/process-daemon/src/transport/unix.rs +++ b/packages/process-daemon/src/transport/unix.rs @@ -393,7 +393,9 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_lines: None, output_error: None, + exited_at: 1, }, ) .unwrap(); @@ -405,7 +407,9 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_lines: None, output_error: None, + exited_at: 1, } )); assert_eq!(client.read(&mut [0]).unwrap(), 0); diff --git a/packages/process-daemon/src/transport/windows.rs b/packages/process-daemon/src/transport/windows.rs index 9fc7eea8c9ba..5b2e5f26e761 100644 --- a/packages/process-daemon/src/transport/windows.rs +++ b/packages/process-daemon/src/transport/windows.rs @@ -705,7 +705,9 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_lines: None, output_error: None, + exited_at: 1, }, ) .unwrap(); diff --git a/packages/process-daemon/tests/ownership.rs b/packages/process-daemon/tests/ownership.rs index 8ef4b71e6217..e1c17ee5396a 100644 --- a/packages/process-daemon/tests/ownership.rs +++ b/packages/process-daemon/tests/ownership.rs @@ -13,7 +13,7 @@ use opencode_pty::daemon::Registration; use opencode_pty::protocol::{ AttachmentRole, Envelope, Request, Response, read_frame, write_frame, }; -use opencode_pty::service::TerminalInfo; +use opencode_pty::service::{ProcessInfo, ProcessMode}; struct Daemon { child: Child, @@ -52,7 +52,7 @@ impl Daemon { } thread::sleep(Duration::from_millis(10)); }; - assert_eq!(registration.protocol, 7); + assert_eq!(registration.protocol, 8); Self { child, root, @@ -100,20 +100,18 @@ impl Daemon { (stream, response) } - fn terminal(&self, command: &str) -> TerminalInfo { - match self.request(Request::Create { + fn terminal(&self, command: &str) -> ProcessInfo { + match self.request(Request::CreatePty { program: "/bin/sh".into(), args: vec!["-c".into(), command.into()], cwd: self.directory.clone(), - title: "ownership".into(), - group_id: "ownership".into(), env: Default::default(), + group_id: "ownership".into(), + title: "ownership".into(), cols: 80, rows: 24, - mode: Default::default(), - output_path: None, }) { - Response::Created { terminal } => terminal, + Response::Created { process } => process, response => panic!("create: {response:?}"), } } @@ -164,7 +162,7 @@ impl Drop for Daemon { } } -fn assert_terminal_stopped(terminal: &TerminalInfo) { +fn assert_terminal_stopped(terminal: &ProcessInfo) { // SAFETY: signal zero only checks whether this child PID still exists. assert_eq!(unsafe { libc::kill(terminal.pid.unwrap() as i32, 0) }, -1); assert_eq!( @@ -312,17 +310,14 @@ fn shutdown_ends_an_idle_subscription() { } #[test] -fn rename_is_advertised_and_served() { +fn rename_is_served() { use opencode_pty::protocol::{SubscriptionEvent, read_subscription_event}; let mut daemon = Daemon::start(); - assert_eq!( - daemon.registration.features, - ["rename", "pipe", "output_path"] - ); + assert!(daemon.registration.features.is_empty()); assert!(matches!( daemon.request(Request::Ping), - Response::Pong { features, .. } if features == ["rename", "pipe", "output_path"] + Response::Pong { protocol: 8, features, .. } if features.is_empty() )); let (owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); @@ -358,9 +353,9 @@ fn rename_is_advertised_and_served() { } } assert!( - matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } - if terminals.len() == 1 && terminals[0].title == "renamed" - && terminals[0].user_title.as_deref() == Some("renamed")) + matches!(daemon.request(Request::List { mode: ProcessMode::Pty }), Response::Processes { processes } + if processes.len() == 1 && processes[0].pty.as_ref().unwrap().title == "renamed" + && processes[0].pty.as_ref().unwrap().user_title.as_deref() == Some("renamed")) ); assert!(matches!( daemon.request(Request::Rename { @@ -414,7 +409,7 @@ fn handoff_preserves_daemon_and_terminal_and_shutdown_overrides_it() { if instance_id == daemon.registration.instance_id && pid == daemon.child.id()) ); assert!( - matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } + matches!(daemon.request(Request::List { mode: ProcessMode::Pty }), Response::Processes { processes: terminals } if terminals.len() == 1 && terminals[0].id == terminal.id && terminals[0].pid == terminal.pid) ); assert!(matches!( @@ -558,7 +553,11 @@ fn stop_signals_stop_terminals_and_clean_up() { assert_eq!(unsafe { libc::kill(probe.pid.unwrap() as i32, signal) }, 0); let deadline = Instant::now() + Duration::from_secs(3); loop { - let Response::Terminals { terminals } = daemon.request(Request::List { mode: None }) + let Response::Processes { + processes: terminals, + } = daemon.request(Request::List { + mode: ProcessMode::Pty, + }) else { panic!("list failed"); }; @@ -584,43 +583,36 @@ fn stop_signals_stop_terminals_and_clean_up() { } #[test] -fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { - use opencode_pty::protocol::{SubscriptionEvent, read_subscription_event}; - use opencode_pty::service::ProcessMode; - +fn pipe_processes_survive_handoff_and_stay_out_of_pty_lists() { let mut daemon = Daemon::start(); let (owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); let terminal = daemon.terminal("exec cat"); let output_file = daemon.root.join("pipe.out"); - let Response::Created { terminal: process } = daemon.request(Request::Create { + let Response::Created { process } = daemon.request(Request::CreatePipe { program: "/bin/sh".into(), args: vec![ "-c".into(), "echo before; sleep 1; echo after; exit 4".into(), ], cwd: daemon.directory.clone(), - title: String::new(), - group_id: "ownership".into(), env: Default::default(), - cols: 0, - rows: 0, - mode: ProcessMode::Pipe, - output_path: Some(output_file.clone()), + group_id: "ownership".into(), + output_path: output_file.clone(), + metadata: Default::default(), }) else { panic!("create pipe"); }; assert_eq!(process.mode, ProcessMode::Pipe); let ids = |mode| match daemon.request(Request::List { mode }) { - Response::Terminals { terminals } => terminals + Response::Processes { processes } => processes .into_iter() - .map(|terminal| terminal.id) + .map(|process| process.id) .collect::>(), response => panic!("list: {response:?}"), }; - assert_eq!(ids(None), vec![terminal.id]); - assert_eq!(ids(Some(ProcessMode::Pty)), vec![terminal.id]); - assert_eq!(ids(Some(ProcessMode::Pipe)), vec![process.id]); + assert_eq!(ids(ProcessMode::Pty), vec![terminal.id]); + assert_eq!(ids(ProcessMode::Pipe), vec![process.id]); assert!(matches!( daemon.request(Request::Snapshot { id: process.id }), Response::Error { message } if message.contains("pipe process") @@ -635,34 +627,20 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { assert!(matches!(response, Response::Owned)); drop(old); - let mut subscription = daemon.connect(); - let Response::Attached { replay_base64, .. } = daemon.send( - &mut subscription, - Request::Subscribe { - id: process.id, - offset: 0, - attachment_id: "pipe-observer".into(), - role: AttachmentRole::Observer, - takeover: false, - }, - ) else { - panic!("subscribe"); - }; - let mut output = base64::engine::general_purpose::STANDARD - .decode(replay_base64) + let mut wait = daemon.connect(); + wait.set_read_timeout(Some(Duration::from_secs(10))) .unwrap(); - let exit = loop { - match read_subscription_event(&mut subscription).unwrap() { - SubscriptionEvent::Output { bytes, .. } => output.extend(bytes), - SubscriptionEvent::Response(response) => { - if let Response::Exited { exit_code, .. } = *response { - break exit_code; - } - } - } + let Response::Exited { + exit_code, + output_lines, + final_offset, + .. + } = daemon.send(&mut wait, Request::Wait { id: process.id }) + else { + panic!("wait"); }; - assert_eq!(exit, Some(4)); - assert_eq!(String::from_utf8(output).unwrap(), "before\nafter\n"); + assert_eq!(exit_code, Some(4)); + assert_eq!((output_lines, final_offset), (Some(2), 13)); assert_eq!( std::fs::read_to_string(&output_file).unwrap(), "before\nafter\n" @@ -671,7 +649,7 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { daemon.request(Request::Terminate { id: process.id }), Response::Ok )); - drop(subscription); + drop(wait); drop(successor); daemon.wait(); assert_terminal_stopped(&terminal); @@ -679,23 +657,20 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { #[test] fn pipe_processes_do_not_inherit_the_daemons_blocked_signals() { - use opencode_pty::service::{ProcessMode, TerminalLifecycle}; + use opencode_pty::service::TerminalLifecycle; let mut daemon = Daemon::start(); let (_owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); for (signal, name) in [(libc::SIGTERM, "SIGTERM"), (libc::SIGINT, "SIGINT")] { - let Response::Created { terminal: process } = daemon.request(Request::Create { + let Response::Created { process } = daemon.request(Request::CreatePipe { program: "/bin/sh".into(), args: vec!["-c".into(), "exec sleep 1000".into()], cwd: daemon.directory.clone(), - title: String::new(), - group_id: "ownership".into(), env: Default::default(), - cols: 0, - rows: 0, - mode: ProcessMode::Pipe, - output_path: None, + group_id: "ownership".into(), + output_path: daemon.root.join(format!("{name}.out")), + metadata: Default::default(), }) else { panic!("create pipe"); }; @@ -706,9 +681,12 @@ fn pipe_processes_do_not_inherit_the_daemons_blocked_signals() { ); let deadline = Instant::now() + Duration::from_secs(5); loop { - let Response::Terminals { terminals } = daemon.request(Request::List { - mode: Some(ProcessMode::Pipe), - }) else { + let Response::Processes { + processes: terminals, + } = daemon.request(Request::List { + mode: ProcessMode::Pipe, + }) + else { panic!("list failed"); }; let lifecycle = terminals diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index 92b8b712502d..ee10d1ce1b82 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -1,68 +1,79 @@ #![cfg(unix)] +use std::collections::HashMap; use std::env; +use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; -use opencode_pty::protocol::AttachmentRole; use opencode_pty::service::{ - CreateTerminal, ProcessMode, StreamEvent, TerminalId, TerminalLifecycle, TerminalService, + CreatePipe, CreatePty, MAX_METADATA_BYTES, PipeMetadata, ProcessExit, ProcessInfo, ProcessMode, + ProcessSignal, TerminalId, TerminalLifecycle, TerminalService, }; -fn pipe(script: &str) -> CreateTerminal { - CreateTerminal { +/// A unique temporary path, removed when dropped even if a test fails. +struct TempPath(PathBuf); + +impl TempPath { + fn new(name: &str) -> Self { + Self(env::temp_dir().join(format!( + "opencode-pty-pipe-{name}-{:032x}", + rand::random::() + ))) + } +} + +impl Drop for TempPath { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } +} + +fn pipe(script: &str, output: &Path) -> CreatePipe { + CreatePipe { program: "/bin/sh".to_string(), args: vec!["-c".to_string(), script.to_string()], cwd: env::current_dir().expect("cwd"), - title: String::new(), + env: HashMap::from([("PATH".to_string(), env::var("PATH").unwrap_or_default())]), group_id: "pipe".to_string(), - env: std::collections::HashMap::new(), - cols: 0, - rows: 0, - mode: ProcessMode::Pipe, - output_path: None, + output_path: output.to_path_buf(), + metadata: PipeMetadata::new(), } } +/// Starts `script` with a fresh output file. +fn start(service: &TerminalService, script: &str) -> (ProcessInfo, TempPath) { + let file = TempPath::new("out"); + let info = service.create_pipe(pipe(script, &file.0)).unwrap(); + (info, file) +} + struct Finished { output: String, - output_error: Option, - exit_code: Option, - signal: Option, - final_offset: u64, + exit: ProcessExit, elapsed: Duration, } -/// Collects output from offset 0 until the exit event. -fn run(service: &TerminalService, id: TerminalId) -> Finished { +/// Waits for the exit and reads the output file. +fn run(service: &TerminalService, id: TerminalId, file: &TempPath) -> Finished { let started = Instant::now(); - let attachment = service - .attach(id, 0, "test".into(), AttachmentRole::Observer, false) - .unwrap(); - let mut output = attachment.replay.bytes.clone(); + let exit = service.wait(id).unwrap().recv().unwrap(); + Finished { + output: String::from_utf8_lossy(&std::fs::read(&file.0).unwrap()).into_owned(), + exit, + elapsed: started.elapsed(), + } +} + +/// Polls the output file until it holds a whole first line. +fn first_line(file: &TempPath) -> String { + let deadline = Instant::now() + Duration::from_secs(5); loop { - match attachment - .events - .recv_timeout(Duration::from_secs(10)) - .expect("pipe process did not exit") - { - StreamEvent::Output { bytes, .. } => output.extend(bytes), - StreamEvent::Exited { - exit_code, - signal, - final_offset, - output_error, - } => { - return Finished { - output: String::from_utf8(output).unwrap(), - output_error, - exit_code, - signal, - final_offset, - elapsed: started.elapsed(), - }; - } - _ => {} + let output = std::fs::read_to_string(&file.0).unwrap_or_default(); + if let Some((line, _)) = output.split_once('\n') { + return line.to_string(); } + assert!(Instant::now() < deadline, "no output line"); + std::thread::sleep(Duration::from_millis(10)); } } @@ -79,29 +90,36 @@ fn wait_dead(pid: i32) { } } +fn newlines(bytes: &[u8]) -> u64 { + bytes.iter().filter(|&&byte| byte == b'\n').count() as u64 +} + #[test] fn reports_ordered_output_and_exit_code_without_a_terminal() { let service = TerminalService::default(); - let info = service - .create(pipe( - "for i in 1 2 3; do echo out$i; echo err$i >&2; done - [ -t 1 ] && echo tty || echo notty - cat; echo stdin-closed - exit 7", - )) - .unwrap(); + let (info, file) = start( + &service, + "for i in 1 2 3; do echo out$i; echo err$i >&2; done + [ -t 1 ] && echo tty || echo notty + cat; echo stdin-closed + exit 7", + ); assert_eq!(info.mode, ProcessMode::Pipe); - assert_eq!(info.title, "sh"); - let finished = run(&service, info.id); + assert!(info.pty.is_none()); + let pipe = info.pipe.as_ref().unwrap(); + assert_eq!(pipe.output_path, file.0); + assert_eq!(info.exited_at, None); + let finished = run(&service, info.id, &file); assert_eq!( finished.output, "out1\nerr1\nout2\nerr2\nout3\nerr3\nnotty\nstdin-closed\n" ); - assert_eq!(finished.exit_code, Some(7)); - assert_eq!(finished.signal, None); - assert_eq!(finished.output_error, None); - assert_eq!(finished.final_offset, finished.output.len() as u64); - let listed = service.list_mode(ProcessMode::Pipe).unwrap(); + assert_eq!(finished.exit.exit_code, Some(7)); + assert_eq!(finished.exit.signal, None); + assert_eq!(finished.exit.output_error, None); + assert_eq!(finished.exit.output_lines, Some(8)); + assert_eq!(finished.exit.final_offset, finished.output.len() as u64); + let listed = service.list(ProcessMode::Pipe).unwrap(); assert_eq!( listed[0].lifecycle, TerminalLifecycle::Exited { @@ -109,6 +127,11 @@ fn reports_ordered_output_and_exit_code_without_a_terminal() { signal: None } ); + assert_eq!(listed[0].exited_at, Some(finished.exit.exited_at)); + assert!(listed[0].started_at <= finished.exit.exited_at); + assert!(listed[0].started_at > 0); + assert_eq!(listed[0].pipe.as_ref().unwrap().output_lines, 8); + assert_eq!(listed[0].output_tail, finished.exit.final_offset); service.terminate(info.id).unwrap(); } @@ -117,49 +140,46 @@ fn sigpipe_is_not_inherited_as_ignored() { // Rust ignores SIGPIPE in the daemon. A child that inherited that could // never die from it, so `producer | head` pipelines would misbehave. let service = TerminalService::default(); - let info = service - .create(pipe("kill -PIPE $$; echo survived")) - .unwrap(); - let finished = run(&service, info.id); + let (info, file) = start(&service, "kill -PIPE $$; echo survived"); + let finished = run(&service, info.id, &file); assert_eq!(finished.output, ""); - assert_eq!(finished.signal.as_deref(), Some("SIGPIPE")); + assert_eq!(finished.exit.signal.as_deref(), Some("SIGPIPE")); service.terminate(info.id).unwrap(); } #[test] fn reports_the_signal_that_ended_the_process() { let service = TerminalService::default(); - let info = service.create(pipe("kill -TERM $$")).unwrap(); - let finished = run(&service, info.id); - assert_eq!(finished.exit_code, None); - assert_eq!(finished.signal.as_deref(), Some("SIGTERM")); + let (info, file) = start(&service, "kill -TERM $$"); + let finished = run(&service, info.id, &file); + assert_eq!(finished.exit.exit_code, None); + assert_eq!(finished.exit.signal.as_deref(), Some("SIGTERM")); service.terminate(info.id).unwrap(); } #[test] fn fast_exit_without_output_is_reported() { let service = TerminalService::default(); - let info = service.create(pipe("exit 3")).unwrap(); - let finished = run(&service, info.id); + let (info, file) = start(&service, "exit 3"); + let finished = run(&service, info.id, &file); assert_eq!(finished.output, ""); - assert_eq!(finished.exit_code, Some(3)); - assert_eq!(finished.final_offset, 0); + assert_eq!(finished.exit.exit_code, Some(3)); + assert_eq!(finished.exit.final_offset, 0); + assert_eq!(finished.exit.output_lines, Some(0)); service.terminate(info.id).unwrap(); } #[test] fn a_descendant_holding_the_pipe_delays_the_exit_by_at_most_the_drain() { let service = TerminalService::default(); - let info = service - .create(pipe("sleep 30 & echo $!; echo started")) - .unwrap(); - let finished = run(&service, info.id); + let (info, file) = start(&service, "sleep 30 & echo $!; echo started"); + let finished = run(&service, info.id, &file); assert!( finished.output.ends_with("started\n"), "{}", finished.output ); - assert_eq!(finished.exit_code, Some(0)); + assert_eq!(finished.exit.exit_code, Some(0)); assert!( finished.elapsed < Duration::from_secs(4), "{:?}", @@ -185,26 +205,11 @@ impl Drop for KillOnDrop { #[test] fn terminate_does_not_wait_for_a_descendant_that_left_the_group() { let service = TerminalService::default(); - let info = service - .create(pipe( - "perl -e 'use POSIX; POSIX::setsid(); $| = 1; print \"$$\\n\"; sleep 30' & wait", - )) - .unwrap(); - let attachment = service - .attach(info.id, 0, "test".into(), AttachmentRole::Observer, false) - .unwrap(); - let mut output = attachment.replay.bytes.clone(); - while !output.ends_with(b"\n") { - if let StreamEvent::Output { bytes, .. } = attachment - .events - .recv_timeout(Duration::from_secs(5)) - .unwrap() - { - output.extend(bytes); - } - } - let escaped = KillOnDrop(String::from_utf8(output).unwrap().trim().parse().unwrap()); - drop(attachment); + let (info, file) = start( + &service, + "perl -e 'use POSIX; POSIX::setsid(); $| = 1; print \"$$\\n\"; sleep 30' & wait", + ); + let escaped = KillOnDrop(first_line(&file).parse().unwrap()); let started = Instant::now(); service.terminate(info.id).unwrap(); assert!(started.elapsed() < Duration::from_secs(2)); @@ -215,36 +220,22 @@ fn terminate_does_not_wait_for_a_descendant_that_left_the_group() { #[test] fn an_exited_process_is_kept_until_terminate() { let service = TerminalService::default(); - let info = service.create(pipe("exit 0")).unwrap(); - run(&service, info.id); + let (info, file) = start(&service, "exit 0"); + run(&service, info.id, &file); let pid = info.pid.unwrap() as i32; // The zombie keeps the PID and process group reserved. assert!(alive(pid)); - assert_eq!(service.list_mode(ProcessMode::Pipe).unwrap().len(), 1); + assert_eq!(service.list(ProcessMode::Pipe).unwrap().len(), 1); service.terminate(info.id).unwrap(); assert!(!alive(pid)); - assert!(service.list_mode(ProcessMode::Pipe).unwrap().is_empty()); + assert!(service.list(ProcessMode::Pipe).unwrap().is_empty()); } #[test] fn terminate_kills_the_process_group() { let service = TerminalService::default(); - let info = service.create(pipe("sleep 30 & echo $!; wait")).unwrap(); - let attachment = service - .attach(info.id, 0, "test".into(), AttachmentRole::Observer, false) - .unwrap(); - let mut output = attachment.replay.bytes.clone(); - while !output.ends_with(b"\n") { - if let StreamEvent::Output { bytes, .. } = attachment - .events - .recv_timeout(Duration::from_secs(5)) - .unwrap() - { - output.extend(bytes); - } - } - let child: i32 = String::from_utf8(output).unwrap().trim().parse().unwrap(); - drop(attachment); + let (info, file) = start(&service, "sleep 30 & echo $!; wait"); + let child: i32 = first_line(&file).parse().unwrap(); service.terminate(info.id).unwrap(); wait_dead(child); assert!(!alive(info.pid.unwrap() as i32)); @@ -254,14 +245,20 @@ fn terminate_kills_the_process_group() { fn terminal_requests_are_rejected_and_lists_are_separate() { let service = TerminalService::default(); let pty = service - .create(CreateTerminal { + .create_pty(CreatePty { + program: "/bin/sh".into(), + args: vec!["-c".into(), "exec cat".into()], + cwd: env::current_dir().unwrap(), + env: HashMap::new(), + group_id: "pipe".into(), + title: "pty".into(), cols: 80, rows: 24, - mode: ProcessMode::Pty, - ..pipe("exec cat") }) .unwrap(); - let pipe = service.create(pipe("exec sleep 30")).unwrap(); + assert!(pty.pipe.is_none()); + assert_eq!(pty.pty.as_ref().unwrap().cols, 80); + let (pipe, _file) = start(&service, "echo replayed; exec sleep 30"); let id = pipe.id; for result in [ service.write(id, b"x".to_vec()), @@ -275,10 +272,26 @@ fn terminal_requests_are_rejected_and_lists_are_separate() { let error = result.unwrap_err().to_string(); assert!(error.contains("pipe process"), "{error}"); } - assert!(service.replay(id, 0).is_ok()); + for result in [ + service.wait(pty.id).map(|_| ()), + service.signal(pty.id, ProcessSignal::Term), + service.set_metadata(pty.id, PipeMetadata::new()), + ] { + let error = result.unwrap_err().to_string(); + assert!(error.contains("not a pipe process"), "{error}"); + } + // The output file is the only buffer: nothing to replay. + let deadline = Instant::now() + Duration::from_secs(5); + while service.get(id).unwrap().output_tail == 0 { + assert!(Instant::now() < deadline); + std::thread::sleep(Duration::from_millis(10)); + } + let replay = service.replay(id, 0).unwrap(); + assert!(replay.bytes.is_empty()); + assert_eq!(replay.end_offset, 9); let listed = |mode| { service - .list_mode(mode) + .list(mode) .unwrap() .into_iter() .map(|info| info.id) @@ -286,187 +299,341 @@ fn terminal_requests_are_rejected_and_lists_are_separate() { }; assert_eq!(listed(ProcessMode::Pty), vec![pty.id]); assert_eq!(listed(ProcessMode::Pipe), vec![pipe.id]); - assert_eq!(service.list().unwrap().len(), 1); service.terminate(pty.id).unwrap(); service.terminate(pipe.id).unwrap(); } #[test] -fn pipe_processes_need_no_terminal_size() { +fn get_returns_what_list_returns() { let service = TerminalService::default(); - assert!( - service - .create(CreateTerminal { - mode: ProcessMode::Pty, - ..pipe("true") - }) - .is_err() - ); - let info = service.create(pipe("true")).unwrap(); + let (info, file) = start(&service, "echo one; echo two"); + let mut metadata = PipeMetadata::new(); + metadata.insert("command".into(), "echo".into()); + service.set_metadata(info.id, metadata).unwrap(); + run(&service, info.id, &file); + let got = serde_json::to_value(service.get(info.id).unwrap()).unwrap(); + let listed = serde_json::to_value(&service.list(ProcessMode::Pipe).unwrap()[0]).unwrap(); + assert_eq!(got, listed); + assert_eq!(got["pipe"]["output_path"], file.0.to_str().unwrap()); + assert_eq!(got["pipe"]["output_lines"], 2); + assert_eq!(got["pipe"]["metadata"]["command"], "echo"); + assert!(got.get("pty").is_none()); service.terminate(info.id).unwrap(); -} - -/// A unique temporary path, removed when dropped even if a test fails. -struct TempPath(std::path::PathBuf); - -impl TempPath { - fn new(name: &str) -> Self { - Self(std::env::temp_dir().join(format!( - "opencode-pty-pipe-{name}-{:032x}", - rand::random::() - ))) - } -} - -impl Drop for TempPath { - fn drop(&mut self) { - let _ = std::fs::remove_file(&self.0); - } -} - -fn wait_exited(service: &TerminalService, id: TerminalId) -> TerminalLifecycle { - let deadline = Instant::now() + Duration::from_secs(10); - loop { - let lifecycle = service - .list_mode(ProcessMode::Pipe) - .unwrap() - .into_iter() - .find(|info| info.id == id) - .unwrap() - .lifecycle; - if lifecycle != TerminalLifecycle::Running { - return lifecycle; - } - assert!(Instant::now() < deadline, "pipe process did not exit"); - std::thread::sleep(Duration::from_millis(10)); + for error in [ + service.get(info.id).unwrap_err(), + service.get(12345).unwrap_err(), + ] { + assert!(error.to_string().contains("not found"), "{error}"); } } #[test] -fn output_path_keeps_everything_beyond_the_replay_window_without_a_subscriber() { - use std::os::unix::fs::PermissionsExt; - - // A replay window far smaller than the output. - let service = TerminalService::new(1024); +fn output_file_keeps_everything_and_is_truncated_on_open() { + let service = TerminalService::default(); let file = TempPath::new("full"); - let path = &file.0; + std::fs::write(&file.0, "earlier output that must go\n").unwrap(); let info = service - .create(CreateTerminal { - output_path: Some(path.clone()), - ..pipe("seq 1 20000; echo done >&2") - }) + .create_pipe(pipe("seq 1 20000; echo done >&2", &file.0)) .unwrap(); - assert_eq!( - wait_exited(&service, info.id), - TerminalLifecycle::Exited { - exit_code: Some(0), - signal: None - } - ); + let exit = service.wait(info.id).unwrap().recv().unwrap(); let expected = (1..=20000).map(|n| format!("{n}\n")).collect::() + "done\n"; - assert_eq!(std::fs::read_to_string(path).unwrap(), expected); - assert_eq!( - std::fs::metadata(path).unwrap().permissions().mode() & 0o777, - 0o600 - ); - let replay = service.replay(info.id, 0).unwrap(); - assert!(replay.truncated); - assert_eq!(replay.end_offset, expected.len() as u64); - assert_eq!( - service.list_mode(ProcessMode::Pipe).unwrap()[0].output_error, - None - ); + assert_eq!(std::fs::read_to_string(&file.0).unwrap(), expected); + assert_eq!(exit.final_offset, expected.len() as u64); + assert_eq!(exit.output_lines, Some(20001)); + assert_eq!(exit.output_error, None); service.terminate(info.id).unwrap(); } #[test] -fn output_path_appends_to_an_existing_file() { - let service = TerminalService::default(); - let file = TempPath::new("append"); - let path = &file.0; - std::fs::write(path, "earlier\n").unwrap(); - let info = service - .create(CreateTerminal { - output_path: Some(path.clone()), - ..pipe("echo later") - }) - .unwrap(); - wait_exited(&service, info.id); - assert_eq!(std::fs::read_to_string(path).unwrap(), "earlier\nlater\n"); - service.terminate(info.id).unwrap(); +fn output_file_mode_follows_the_umask() { + use std::os::unix::fs::PermissionsExt; + + // umask is process-wide: set it only in a child copy of this test. + if let Ok(umask) = env::var("OPENCODE_PTY_UMASK_TEST_CHILD") { + let umask = u32::from_str_radix(&umask, 8).unwrap(); + // SAFETY: this child process runs only this test. + unsafe { libc::umask(umask as libc::mode_t) }; + let service = TerminalService::default(); + let (info, file) = start(&service, "true"); + run(&service, info.id, &file); + let mode = std::fs::metadata(&file.0).unwrap().permissions().mode() & 0o777; + assert_eq!(mode, 0o666 & !umask); + service.terminate(info.id).unwrap(); + return; + } + for umask in ["022", "027"] { + let output = std::process::Command::new(env::current_exe().unwrap()) + .args(["--exact", "output_file_mode_follows_the_umask"]) + .env("OPENCODE_PTY_UMASK_TEST_CHILD", umask) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + } } #[test] -fn output_path_must_be_an_absolute_file_for_a_pipe_process() { +fn rejected_requests_never_touch_the_output_file_or_run() { let service = TerminalService::default(); let missing = TempPath::new("missing-directory").0.join("out"); - let pty = TempPath::new("pty"); let marker = TempPath::new("should-not-run"); let target = TempPath::new("target"); - std::fs::write(&target.0, "").unwrap(); + std::fs::write(&target.0, "kept").unwrap(); let link = TempPath::new("link"); std::os::unix::fs::symlink(&target.0, &link.0).unwrap(); + let existing = TempPath::new("existing"); + std::fs::write(&existing.0, "kept").unwrap(); let touch = format!("touch '{}'", marker.0.display()); + let mut huge = PipeMetadata::new(); + huge.insert("command".into(), "x".repeat(MAX_METADATA_BYTES).into()); for (request, error) in [ + (pipe(&touch, Path::new("relative.out")), "must be absolute"), ( - CreateTerminal { - output_path: Some("relative.out".into()), - ..pipe("true") + CreatePipe { + cwd: "relative".into(), + ..pipe(&touch, &existing.0) }, - "must be absolute", + "cwd must be absolute", ), ( - CreateTerminal { - output_path: Some(pty.0.clone()), - cols: 80, - rows: 24, - mode: ProcessMode::Pty, - ..pipe("true") + CreatePipe { + metadata: huge, + ..pipe(&touch, &existing.0) }, - "only supported for pipe processes", - ), - ( - CreateTerminal { - output_path: Some(missing), - ..pipe(&touch) - }, - "failed to open output file", - ), - ( - CreateTerminal { - output_path: Some(link.0.clone()), - ..pipe(&touch) - }, - "failed to open output file", + "metadata exceeds", ), + (pipe(&touch, &missing), "failed to open output file"), + (pipe(&touch, &link.0), "failed to open output file"), ] { - let message = format!("{:#}", service.create(request).unwrap_err()); + let message = format!("{:#}", service.create_pipe(request).unwrap_err()); assert!(message.contains(error), "{message}"); } - assert!(service.list_mode(ProcessMode::Pipe).unwrap().is_empty()); + assert!(service.list(ProcessMode::Pipe).unwrap().is_empty()); + assert_eq!(std::fs::read_to_string(&existing.0).unwrap(), "kept"); + assert_eq!(std::fs::read_to_string(&target.0).unwrap(), "kept"); // Give a wrongly spawned child time to run before checking. std::thread::sleep(Duration::from_millis(100)); assert!(!marker.0.exists()); } -#[cfg(target_os = "linux")] #[test] -fn a_failing_output_file_is_reported_and_the_process_keeps_running() { +fn the_child_gets_exactly_the_given_environment() { + // Something the test process (and so a daemon) certainly has. + assert!(env::var_os("PATH").is_some()); let service = TerminalService::default(); + let file = TempPath::new("env"); let info = service - .create(CreateTerminal { - output_path: Some("/dev/full".into()), - ..pipe("echo lost; echo kept; exit 5") + .create_pipe(CreatePipe { + program: "/usr/bin/env".into(), + args: vec![], + env: HashMap::from([("ONLY".to_string(), "this".to_string())]), + ..pipe("", &file.0) }) .unwrap(); - let finished = run(&service, info.id); - assert_eq!(finished.output, "lost\nkept\n"); - assert_eq!(finished.exit_code, Some(5)); - assert!(finished.output_error.is_some()); - let error = service.list_mode(ProcessMode::Pipe).unwrap()[0] - .output_error - .clone() + let finished = run(&service, info.id, &file); + assert_eq!(finished.output, "ONLY=this\n"); + service.terminate(info.id).unwrap(); +} + +#[test] +fn output_lines_count_newlines_in_the_file() { + let service = TerminalService::default(); + for (script, expected) in [ + ("printf 'a\\nb\\nc'", Some(2)), + ("printf 'a\\n\\n\\nb\\n'", Some(4)), + ("printf ''", Some(0)), + ("seq 1 200000", Some(200000)), + // Many small writes the actor batches together. + ("for i in $(seq 1 2000); do echo $i; done", Some(2000)), + ("head -c 300000 /dev/urandom", None), + ] { + let (info, file) = start(&service, script); + let exit = run(&service, info.id, &file).exit; + let bytes = std::fs::read(&file.0).unwrap(); + assert_eq!(exit.output_lines, Some(newlines(&bytes)), "{script}"); + if let Some(expected) = expected { + assert_eq!(exit.output_lines, Some(expected), "{script}"); + } + assert_eq!(exit.final_offset, bytes.len() as u64, "{script}"); + let listed = service.get(info.id).unwrap(); + assert_eq!( + listed.pipe.unwrap().output_lines, + exit.output_lines.unwrap() + ); + service.terminate(info.id).unwrap(); + } +} + +#[cfg(target_os = "linux")] +#[test] +fn a_failing_output_file_is_reported_and_the_process_keeps_running() { + let service = TerminalService::default(); + let info = service + .create_pipe(pipe("echo lost; echo kept; exit 5", Path::new("/dev/full"))) .unwrap(); + let exit = service.wait(info.id).unwrap().recv().unwrap(); + assert_eq!(exit.exit_code, Some(5)); + // Counts cover only bytes in the file, but offsets cover everything read. + assert_eq!(exit.output_lines, Some(0)); + assert_eq!(exit.final_offset, 10); + let error = exit.output_error.unwrap(); assert!(error.contains("failed to write output file"), "{error}"); + let pipe = service.get(info.id).unwrap().pipe.unwrap(); + assert_eq!(pipe.output_error, Some(error)); + assert_eq!(pipe.output_lines, 0); + service.terminate(info.id).unwrap(); +} + +#[test] +fn wait_blocks_until_the_exit_and_answers_every_waiter() { + let service = TerminalService::default(); + let (info, file) = start(&service, "sleep 0.3; echo done; exit 2"); + let waits = (0..3) + .map(|_| service.wait(info.id).unwrap()) + .collect::>(); + assert!(waits[0].exit.try_recv().is_err()); + let exits = waits + .iter() + .map(|wait| wait.recv().unwrap()) + .collect::>(); + assert!(exits.iter().all(|exit| *exit == exits[0])); + assert_eq!(exits[0].exit_code, Some(2)); + assert_eq!(exits[0].output_lines, Some(1)); + assert_eq!(std::fs::read_to_string(&file.0).unwrap(), "done\n"); + // After the exit, a new waiter is answered at once. + let started = Instant::now(); + assert_eq!(service.wait(info.id).unwrap().recv().unwrap(), exits[0]); + assert!(started.elapsed() < Duration::from_millis(500)); + service.terminate(info.id).unwrap(); + assert!(service.wait(info.id).is_err()); +} + +#[test] +fn terminate_ends_a_pending_wait_with_an_error() { + let service = TerminalService::default(); + let (info, _file) = start(&service, "exec sleep 30"); + let wait = service.wait(info.id).unwrap(); + service.terminate(info.id).unwrap(); + assert_eq!(wait.recv().unwrap_err().to_string(), "terminated"); +} + +#[test] +fn signal_stops_a_process_and_keeps_its_record() { + let service = TerminalService::default(); + let (info, file) = start(&service, "echo started; exec sleep 30"); + first_line(&file); + service.signal(info.id, ProcessSignal::Term).unwrap(); + let exit = service.wait(info.id).unwrap().recv().unwrap(); + assert_eq!(exit.signal.as_deref(), Some("SIGTERM")); + assert_eq!(exit.output_lines, Some(1)); + assert_eq!( + service.get(info.id).unwrap().lifecycle, + TerminalLifecycle::Exited { + exit_code: None, + signal: Some("SIGTERM".into()) + } + ); + // Nothing is left in the group (macOS reports ESRCH); still fine. + service.signal(info.id, ProcessSignal::Kill).unwrap(); + service.terminate(info.id).unwrap(); + assert!(service.signal(info.id, ProcessSignal::Kill).is_err()); +} + +#[test] +fn a_trapped_sigterm_survives_until_sigkill() { + let service = TerminalService::default(); + let (info, file) = start( + &service, + "trap 'echo trapped' TERM; echo ready; while true; do sleep 0.05; done", + ); + first_line(&file); + service.signal(info.id, ProcessSignal::Term).unwrap(); + let wait = service.wait(info.id).unwrap(); + let deadline = Instant::now() + Duration::from_secs(5); + while !std::fs::read_to_string(&file.0) + .unwrap() + .contains("trapped") + { + assert!(Instant::now() < deadline, "trap did not run"); + std::thread::sleep(Duration::from_millis(10)); + } + assert!(wait.exit.try_recv().is_err()); + service.signal(info.id, ProcessSignal::Kill).unwrap(); + assert_eq!(wait.recv().unwrap().signal.as_deref(), Some("SIGKILL")); + service.terminate(info.id).unwrap(); +} + +#[test] +fn signal_reaches_a_backgrounded_child_in_the_group() { + let service = TerminalService::default(); + let (info, file) = start(&service, "sleep 30 & echo $!; wait"); + let child: i32 = first_line(&file).parse().unwrap(); + // Not SIGINT: a non-interactive shell starts background jobs ignoring it. + service.signal(info.id, ProcessSignal::Term).unwrap(); + wait_dead(child); + let exit = service.wait(info.id).unwrap().recv().unwrap(); + assert!(exit.signal.is_some() || exit.exit_code.is_some()); + assert_eq!(service.list(ProcessMode::Pipe).unwrap().len(), 1); + service.terminate(info.id).unwrap(); +} + +#[test] +fn signal_after_exit_reaches_leftover_group_members() { + let service = TerminalService::default(); + let (info, file) = start(&service, "sleep 30 & echo $!"); + let exit = run(&service, info.id, &file); + let sleeper = KillOnDrop(exit.output.trim().parse().unwrap()); + assert!(alive(sleeper.0)); + service.signal(info.id, ProcessSignal::Kill).unwrap(); + wait_dead(sleeper.0); + service.terminate(info.id).unwrap(); +} + +#[test] +fn metadata_round_trips_and_is_capped() { + let service = TerminalService::default(); + let file = TempPath::new("metadata"); + let metadata = serde_json::json!({ + "id": "sh_1", + "command": "echo hi", + "location": { "directory": "/tmp", "workspaceID": null }, + }); + let serde_json::Value::Object(metadata) = metadata else { + unreachable!() + }; + let info = service + .create_pipe(CreatePipe { + metadata: metadata.clone(), + ..pipe("exec sleep 30", &file.0) + }) + .unwrap(); + assert_eq!(info.pipe.unwrap().metadata, metadata); + assert_eq!( + service.get(info.id).unwrap().pipe.unwrap().metadata, + metadata + ); + let mut changed = metadata.clone(); + changed.insert("status".into(), "killed".into()); + service.set_metadata(info.id, changed.clone()).unwrap(); + assert_eq!( + service.list(ProcessMode::Pipe).unwrap()[0] + .pipe + .as_ref() + .unwrap() + .metadata, + changed + ); + let mut huge = PipeMetadata::new(); + huge.insert("command".into(), "x".repeat(MAX_METADATA_BYTES).into()); + let error = service.set_metadata(info.id, huge).unwrap_err(); + assert!(error.to_string().contains("metadata exceeds"), "{error}"); + assert_eq!( + service.get(info.id).unwrap().pipe.unwrap().metadata, + changed + ); service.terminate(info.id).unwrap(); } diff --git a/packages/process-daemon/tests/rows.rs b/packages/process-daemon/tests/rows.rs index 1e222530b743..a747e0a580b6 100644 --- a/packages/process-daemon/tests/rows.rs +++ b/packages/process-daemon/tests/rows.rs @@ -7,7 +7,7 @@ use std::time::{Duration, Instant}; use opencode_pty::client::TerminalClient; use opencode_pty::daemon::{REGISTRATION_FILE, Registration}; use opencode_pty::protocol::{Envelope, Request, Response, read_frame, write_frame}; -use opencode_pty::service::CreateTerminal; +use opencode_pty::service::CreatePty; #[test] fn daemon_client_read_rows_roundtrip() { @@ -16,17 +16,23 @@ fn daemon_client_read_rows_roundtrip() { if let Some(directory) = std::env::var_os("OPENCODE_PTY_ROWS_TEST_CHILD") { let directory = std::path::PathBuf::from(directory); let client = TerminalClient::discover(&directory).unwrap(); - let mut request = CreateTerminal::shell().unwrap(); + let mut request = CreatePty::shell().unwrap(); request.program = "/bin/sh".to_string(); request.args = vec!["-c".to_string(), "printf 'one\ntwo\n\nfour\n'".to_string()]; request.cols = 10; request.rows = 3; - let terminal = client.create(request).unwrap(); + let terminal = client.create_pty(request).unwrap(); let deadline = Instant::now() + Duration::from_secs(5); loop { let rows = client.read_rows(terminal.id, None).unwrap(); if rows.lines == ["", "four", ""] { - assert_eq!((rows.terminal.cols, rows.terminal.rows), (10, 3)); + assert_eq!( + ( + rows.process.pty.as_ref().unwrap().cols, + rows.process.pty.as_ref().unwrap().rows + ), + (10, 3) + ); assert_eq!((rows.cursor_x, rows.cursor_y), (0, 2)); break; } @@ -47,7 +53,7 @@ fn daemon_client_read_rows_roundtrip() { ); let registration = opencode_pty::daemon::read_registration(&directory).unwrap(); - assert_eq!(registration.protocol, 7); + assert_eq!(registration.protocol, 8); // Exercise omitted rows independently of the Rust client's null encoding. let mut stream = std::os::unix::net::UnixStream::connect(registration.socket).unwrap(); write_frame( @@ -61,12 +67,18 @@ fn daemon_client_read_rows_roundtrip() { let response: serde_json::Value = read_frame(&mut stream).unwrap(); assert_eq!(response["type"], "rows"); assert_eq!(response["lines"], serde_json::json!(["", "four", ""])); - assert_eq!(response["terminal"]["id"], terminal.id); + assert_eq!(response["process"]["id"], terminal.id); assert_eq!(response["cursor_y"], 2); client.resize(terminal.id, 12, 4).unwrap(); let rows = client.read_rows(terminal.id, None).unwrap(); assert_eq!(rows.lines, ["two", "", "four", ""]); - assert_eq!((rows.terminal.cols, rows.terminal.rows), (12, 4)); + assert_eq!( + ( + rows.process.pty.as_ref().unwrap().cols, + rows.process.pty.as_ref().unwrap().rows + ), + (12, 4) + ); client.terminate(terminal.id).unwrap(); return; } diff --git a/packages/process-daemon/tests/runtime.rs b/packages/process-daemon/tests/runtime.rs index 06d975f65502..f3ac45c3bdf2 100644 --- a/packages/process-daemon/tests/runtime.rs +++ b/packages/process-daemon/tests/runtime.rs @@ -8,7 +8,7 @@ use std::time::{Duration, Instant}; use opencode_pty::protocol::AttachmentRole; use opencode_pty::service::{ - StreamEvent, TerminalId, TerminalLifecycle, TerminalService, TerminalSnapshot, + ProcessMode, StreamEvent, TerminalId, TerminalLifecycle, TerminalService, TerminalSnapshot, }; use terminal_fixture::{Command, Deadline, Fixture, TempDir}; @@ -32,7 +32,7 @@ fn real_child_input_output_unicode_and_snapshots() { let _deadline = Deadline::new(); let fixture = Fixture::new(); let service = TerminalService::default(); - let info = service.create(fixture.request()).unwrap(); + let info = service.create_pty(fixture.request()).unwrap(); assert!(info.pid.is_some_and(|pid| pid != 0)); let mut child = fixture.connect(); child.command(Command::Output( @@ -88,7 +88,7 @@ fn child_cwd_environment_and_quoted_program_path() { .env .insert("PTY_FIXTURE_VALUE".into(), "value café 界 = ok".into()); let expected_args = request.args.clone(); - let info = service.create(request).unwrap(); + let info = service.create_pty(request).unwrap(); let mut child = fixture.connect(); let context = child.command(Command::Context); assert_eq!( @@ -119,7 +119,7 @@ fn executable_is_resolved_from_child_path() { request .env .insert("PATH".into(), directory.0.to_str().unwrap().into()); - let info = service.create(request).unwrap(); + let info = service.create_pty(request).unwrap(); let mut child = fixture.connect(); child.command(Command::Output("\r\nPATH_OK".into())); wait_text(&service, info.id, "PATH_OK"); @@ -131,7 +131,7 @@ fn resize_updates_real_console_and_parser() { let _deadline = Deadline::new(); let fixture = Fixture::new(); let service = TerminalService::default(); - let info = service.create(fixture.request()).unwrap(); + let info = service.create_pty(fixture.request()).unwrap(); let mut child = fixture.connect(); assert_eq!(child.command(Command::Size), serde_json::json!([80, 24])); let observer = service @@ -158,7 +158,13 @@ fn resize_updates_real_console_and_parser() { thread::sleep(Duration::from_millis(10)); } let snapshot = service.snapshot(info.id).unwrap(); - assert_eq!((snapshot.info.cols, snapshot.info.rows), (cols, rows)); + assert_eq!( + ( + snapshot.info.pty.as_ref().unwrap().cols, + snapshot.info.pty.as_ref().unwrap().rows + ), + (cols, rows) + ); assert_eq!( service.read_rows(info.id, None).unwrap().lines.len(), rows as usize @@ -189,7 +195,7 @@ fn terminal_query_response_reaches_child() { let _deadline = Deadline::new(); let fixture = Fixture::new(); let service = TerminalService::default(); - let info = service.create(fixture.request()).unwrap(); + let info = service.create_pty(fixture.request()).unwrap(); let mut child = fixture.connect(); child.command(Command::Output("\x1b[2J\x1b[H\x1b[3;7H\x1b[6n".into())); let response = b"\x1b[3;7R"; @@ -217,8 +223,8 @@ fn independent_terminals_keep_bounded_replay() { let alpha_fixture = Fixture::new(); let beta_fixture = Fixture::new(); let service = TerminalService::new(64); - let alpha = service.create(alpha_fixture.request()).unwrap(); - let beta = service.create(beta_fixture.request()).unwrap(); + let alpha = service.create_pty(alpha_fixture.request()).unwrap(); + let beta = service.create_pty(beta_fixture.request()).unwrap(); assert_ne!(alpha.id, beta.id); let mut a = alpha_fixture.connect(); let mut b = beta_fixture.connect(); @@ -241,7 +247,7 @@ fn independent_terminals_keep_bounded_replay() { serde_json::json!(b"still-alive") ); service.terminate(beta.id).unwrap(); - assert!(service.list().unwrap().is_empty()); + assert!(service.list(ProcessMode::Pty).unwrap().is_empty()); } #[test] @@ -259,7 +265,7 @@ fn check_normal_exit(code: u32) { let _deadline = Deadline::new(); let fixture = Fixture::new(); let service = TerminalService::default(); - let info = service.create(fixture.request()).unwrap(); + let info = service.create_pty(fixture.request()).unwrap(); let mut child = fixture.connect(); let observer = service .attach( @@ -294,13 +300,10 @@ fn check_normal_exit(code: u32) { bytes.extend(output); offset = end; } - StreamEvent::Exited { - exit_code, - final_offset, - .. - } => { - assert_eq!(exit_code, Some(code)); - assert_eq!(final_offset, offset); + StreamEvent::Exited(exit) => { + assert_eq!(exit.exit_code, Some(code)); + assert_eq!(exit.final_offset, offset); + assert_eq!(exit.output_lines, None); break; } _ => {} @@ -366,7 +369,7 @@ fn termination_cycles(block_input: bool) { let service = TerminalService::default(); for _ in 0..3 { let fixture = Fixture::new(); - let info = service.create(fixture.request()).unwrap(); + let info = service.create_pty(fixture.request()).unwrap(); let _child = fixture.connect(); let process = ChildProcess::open(info.pid.unwrap()); // Child waits on the private control channel, never draining PTY stdin. @@ -375,11 +378,11 @@ fn termination_cycles(block_input: bool) { } service.terminate(info.id).unwrap(); process.assert_exited(); - assert!(service.list().unwrap().is_empty()); + assert!(service.list(ProcessMode::Pty).unwrap().is_empty()); assert!(service.terminate(info.id).is_err()); } let fixture = Fixture::new(); - let info = service.create(fixture.request()).unwrap(); + let info = service.create_pty(fixture.request()).unwrap(); let _child = fixture.connect(); let process = ChildProcess::open(info.pid.unwrap()); if block_input { @@ -427,7 +430,7 @@ fn terminating_a_shell_releases_its_console_child() { "-EncodedCommand".into(), base64::engine::general_purpose::STANDARD.encode(script), ]; - let info = service.create(request).unwrap(); + let info = service.create_pty(request).unwrap(); let root = ChildProcess::open(info.pid.unwrap()); let mut child = fixture.connect(); let pid = u32::try_from(child.command(Command::Context)["pid"].as_u64().unwrap()).unwrap(); @@ -492,7 +495,7 @@ fn shutdown_drains_conpty_while_a_child_is_producing_output() { let _deadline = Deadline::new(); let fixture = Fixture::new(); let service = TerminalService::default(); - let info = service.create(fixture.request()).unwrap(); + let info = service.create_pty(fixture.request()).unwrap(); let mut child = fixture.connect(); let process = ChildProcess::open(info.pid.unwrap()); child.command(Command::Flood); diff --git a/packages/process-daemon/tests/service.rs b/packages/process-daemon/tests/service.rs index 530aa844ae76..c19faf85bf58 100644 --- a/packages/process-daemon/tests/service.rs +++ b/packages/process-daemon/tests/service.rs @@ -7,7 +7,7 @@ use std::env; use std::thread; use std::time::{Duration, Instant}; -use opencode_pty::service::{CreateTerminal, TerminalLifecycle, TerminalService}; +use opencode_pty::service::{CreatePty, ProcessMode, TerminalLifecycle, TerminalService}; use opencode_pty::{protocol::AttachmentRole, service::StreamEvent}; use resize_fixture::ResizeFixture; @@ -59,8 +59,8 @@ fn repeated_resize_reports_are_delivered_once() { fixture.expect_input(b"!\x1b[0n"); } -fn command(script: &str) -> CreateTerminal { - CreateTerminal { +fn command(script: &str) -> CreatePty { + CreatePty { program: "/bin/sh".to_string(), args: vec!["-c".to_string(), script.to_string()], cwd: env::current_dir().expect("cwd"), @@ -69,8 +69,6 @@ fn command(script: &str) -> CreateTerminal { env: std::collections::HashMap::new(), cols: 80, rows: 24, - mode: Default::default(), - output_path: None, } } @@ -92,7 +90,7 @@ fn read_rows_snapshots_live_state_without_taking_control() { command(r"printf '\033]2;rows-title\007one\ntwo\nthree\nfour\nfive'; sleep 1"); request.cols = 10; request.rows = 3; - let terminal = service.create(request).expect("terminal created"); + let terminal = service.create_pty(request).expect("terminal created"); wait_for(|| { service .read_rows(terminal.id, None) @@ -111,11 +109,17 @@ fn read_rows_snapshots_live_state_without_taking_control() { let replay = service.replay(terminal.id, 0).unwrap(); let rows = service.read_rows(terminal.id, Some(u16::MAX)).unwrap(); assert_eq!(rows.lines, ["one", "two", "three", "four", "five"]); - assert_eq!(rows.terminal.id, terminal.id); - assert_eq!(rows.terminal.title, "rows-title"); - assert_eq!((rows.terminal.cols, rows.terminal.rows), (10, 3)); + assert_eq!(rows.process.id, terminal.id); + assert_eq!(rows.process.pty.as_ref().unwrap().title, "rows-title"); + assert_eq!( + ( + rows.process.pty.as_ref().unwrap().cols, + rows.process.pty.as_ref().unwrap().rows + ), + (10, 3) + ); assert_eq!((rows.cursor_x, rows.cursor_y), (4, 2)); - assert_eq!(rows.terminal.output_tail, replay.end_offset); + assert_eq!(rows.process.output_tail, replay.end_offset); assert_eq!( service.snapshot(terminal.id).unwrap().checkpoint, snapshot.checkpoint @@ -127,7 +131,13 @@ fn read_rows_snapshots_live_state_without_taking_control() { .unwrap(); let rows = service.read_rows(terminal.id, None).unwrap(); assert_eq!(rows.lines, ["two", "three", "four", "five"]); - assert_eq!((rows.terminal.cols, rows.terminal.rows), (12, 4)); + assert_eq!( + ( + rows.process.pty.as_ref().unwrap().cols, + rows.process.pty.as_ref().unwrap().rows + ), + (12, 4) + ); assert_eq!((rows.cursor_x, rows.cursor_y), (4, 3)); assert!( service @@ -145,10 +155,10 @@ fn read_rows_snapshots_live_state_without_taking_control() { fn terminals_run_independently_and_one_exit_does_not_stop_service() { let service = TerminalService::new(64 * 1024); let alpha = service - .create(command("printf 'alpha\\n'; sleep 0.05")) + .create_pty(command("printf 'alpha\\n'; sleep 0.05")) .expect("alpha created"); let beta = service - .create(command("printf 'beta\\n'; sleep 0.2")) + .create_pty(command("printf 'beta\\n'; sleep 0.2")) .expect("beta created"); wait_for(|| { @@ -160,7 +170,7 @@ fn terminals_run_independently_and_one_exit_does_not_stop_service() { .is_ok_and(|snapshot| snapshot.text.contains("beta")) }); wait_for(|| { - service.list().is_ok_and(|terminals| { + service.list(ProcessMode::Pty).is_ok_and(|terminals| { terminals.iter().any(|terminal| { terminal.id == alpha.id && matches!(terminal.lifecycle, TerminalLifecycle::Exited { .. }) @@ -169,7 +179,7 @@ fn terminals_run_independently_and_one_exit_does_not_stop_service() { }); let gamma = service - .create(command("printf 'gamma\\n'; sleep 0.05")) + .create_pty(command("printf 'gamma\\n'; sleep 0.05")) .expect("service creates terminal after child exit"); wait_for(|| { service @@ -186,11 +196,11 @@ fn terminals_run_independently_and_one_exit_does_not_stop_service() { fn hidden_terminal_output_uses_bounded_replay() { let service = TerminalService::new(32); let terminal = service - .create(command("printf '0123456789abcdefghijklmnopqrstuvwxyz'")) + .create_pty(command("printf '0123456789abcdefghijklmnopqrstuvwxyz'")) .expect("terminal created"); wait_for(|| { - service.list().is_ok_and(|items| { + service.list(ProcessMode::Pty).is_ok_and(|items| { items .iter() .any(|item| item.id == terminal.id && item.output_tail >= 36) @@ -208,7 +218,7 @@ fn hidden_terminal_output_uses_bounded_replay() { fn title_changes_update_metadata_and_notify_subscribers() { let service = TerminalService::new(64 * 1024); let terminal = service - .create(command( + .create_pty(command( r"sleep 0.05; printf '\033]2;dynamic-title\007'; sleep 0.2", )) .expect("terminal created"); @@ -238,10 +248,10 @@ fn title_changes_update_metadata_and_notify_subscribers() { }; assert_eq!(title, "dynamic-title"); - assert!(service.list().is_ok_and(|items| { - items - .iter() - .any(|item| item.id == terminal.id && item.title == "dynamic-title") + assert!(service.list(ProcessMode::Pty).is_ok_and(|items| { + items.iter().any(|item| { + item.id == terminal.id && item.pty.as_ref().unwrap().title == "dynamic-title" + }) })); service.terminate(terminal.id).expect("terminal removed"); @@ -251,7 +261,7 @@ fn title_changes_update_metadata_and_notify_subscribers() { fn rename_updates_metadata_and_notifies_subscribers_once() { let service = TerminalService::new(64 * 1024); let terminal = service - .create(command("exec cat")) + .create_pty(command("exec cat")) .expect("terminal created"); let observer = service .attach( @@ -270,13 +280,16 @@ fn rename_updates_metadata_and_notifies_subscribers_once() { .rename(terminal.id, Some("renamed".to_string())) .unwrap(); let snapshot = service.snapshot(terminal.id).unwrap(); - assert_eq!(snapshot.info.title, "renamed"); - assert_eq!(snapshot.info.user_title.as_deref(), Some("renamed")); - assert!(service.list().is_ok_and(|items| { + assert_eq!(snapshot.info.pty.as_ref().unwrap().title, "renamed"); + assert_eq!( + snapshot.info.pty.as_ref().unwrap().user_title.as_deref(), + Some("renamed") + ); + assert!(service.list(ProcessMode::Pty).is_ok_and(|items| { items.iter().any(|item| { item.id == terminal.id - && item.title == "renamed" - && item.user_title.as_deref() == Some("renamed") + && item.pty.as_ref().unwrap().title == "renamed" + && item.pty.as_ref().unwrap().user_title.as_deref() == Some("renamed") }) })); service.terminate(terminal.id).expect("terminal removed"); @@ -297,19 +310,19 @@ fn rename_updates_metadata_and_notifies_subscribers_once() { fn list_returns_terminals_in_creation_order() { let service = TerminalService::new(64 * 1024); let created = (0..6) - .map(|_| service.create(command("exec cat")).unwrap().id) + .map(|_| service.create_pty(command("exec cat")).unwrap().id) .collect::>(); let listed = service - .list() + .list(ProcessMode::Pty) .unwrap() .into_iter() .map(|terminal| terminal.id) .collect::>(); assert_eq!(listed, created); service.terminate(created[2]).unwrap(); - let id = service.create(command("exec cat")).unwrap().id; + let id = service.create_pty(command("exec cat")).unwrap().id; let listed = service - .list() + .list(ProcessMode::Pty) .unwrap() .into_iter() .map(|terminal| terminal.id) @@ -330,7 +343,7 @@ fn list_returns_terminals_in_creation_order() { fn foreground_process_tracks_deepest_tty_attached_job() { let service = TerminalService::new(64 * 1024); let terminal = service - .create(command("exec /bin/sh")) + .create_pty(command("exec /bin/sh")) .expect("terminal created"); let controller = service .attach( @@ -357,7 +370,9 @@ fn foreground_process_tracks_deepest_tty_attached_job() { Instant::now() < deadline, "timed out waiting for foreground process" ); - service.list().expect("foreground process refreshed"); + service + .list(ProcessMode::Pty) + .expect("foreground process refreshed"); match controller.events.recv_timeout(Duration::from_millis(50)) { Ok(StreamEvent::ForegroundProcessChanged { process: Some(process), @@ -370,9 +385,10 @@ fn foreground_process_tracks_deepest_tty_attached_job() { }; assert_eq!(process, "sleep"); - assert!(service.list().is_ok_and(|items| { + assert!(service.list(ProcessMode::Pty).is_ok_and(|items| { items.iter().any(|item| { - item.id == terminal.id && item.foreground_process.as_deref() == Some("sleep") + item.id == terminal.id + && item.pty.as_ref().unwrap().foreground_process.as_deref() == Some("sleep") }) })); @@ -383,7 +399,7 @@ fn foreground_process_tracks_deepest_tty_attached_job() { fn bursty_output_does_not_disconnect_a_healthy_observer() { let service = TerminalService::new(2 * 1024 * 1024); let terminal = service - .create(command("sleep 0.05; head -c 1048576 /dev/zero; sleep 0.2")) + .create_pty(command("sleep 0.05; head -c 1048576 /dev/zero; sleep 0.2")) .expect("terminal created"); let observer = service .attach( @@ -396,7 +412,7 @@ fn bursty_output_does_not_disconnect_a_healthy_observer() { .expect("observer attached"); wait_for(|| { - service.list().is_ok_and(|items| { + service.list(ProcessMode::Pty).is_ok_and(|items| { items .iter() .any(|item| item.id == terminal.id && item.output_tail >= 1024 * 1024) @@ -428,7 +444,7 @@ fn bursty_output_does_not_disconnect_a_healthy_observer() { fn controller_switches_without_disconnecting_observers() { let service = TerminalService::new(64 * 1024); let terminal = service - .create(command("sleep 0.05; printf 'streamed\\n'; sleep 0.2")) + .create_pty(command("sleep 0.05; printf 'streamed\\n'; sleep 0.2")) .expect("terminal created"); let controller = service .attach( diff --git a/packages/process-daemon/tests/support/resize_fixture.rs b/packages/process-daemon/tests/support/resize_fixture.rs index efc8dcda4841..2f2ef30c1697 100644 --- a/packages/process-daemon/tests/support/resize_fixture.rs +++ b/packages/process-daemon/tests/support/resize_fixture.rs @@ -5,7 +5,7 @@ use std::thread; use std::time::{Duration, Instant}; use nix::sys::termios::{SetArg, cfmakeraw, tcgetattr, tcsetattr}; -use opencode_pty::service::{CreateTerminal, TerminalId, TerminalService}; +use opencode_pty::service::{CreatePty, TerminalId, TerminalService}; const CHILD: &str = "OPENCODE_PTY_RESIZE_TEST_CHILD"; @@ -29,7 +29,7 @@ impl ResizeFixture { listener.set_nonblocking(true).unwrap(); let service = TerminalService::default(); let terminal = service - .create(CreateTerminal { + .create_pty(CreatePty { program: env::current_exe().unwrap().to_string_lossy().into_owned(), args: vec![ "--exact".into(), @@ -43,8 +43,6 @@ impl ResizeFixture { env: [(CHILD.into(), listener.local_addr().unwrap().to_string())].into(), cols: 80, rows: 24, - mode: Default::default(), - output_path: None, }) .unwrap(); diff --git a/packages/process-daemon/tests/support/terminal_fixture.rs b/packages/process-daemon/tests/support/terminal_fixture.rs index 173ca08be9c9..a61b858967c8 100644 --- a/packages/process-daemon/tests/support/terminal_fixture.rs +++ b/packages/process-daemon/tests/support/terminal_fixture.rs @@ -13,7 +13,7 @@ use std::sync::mpsc; use std::thread; use std::time::{Duration, Instant}; -use opencode_pty::service::CreateTerminal; +use opencode_pty::service::CreatePty; use serde::{Deserialize, Serialize}; const ADDRESS: &str = "OPENCODE_PTY_FIXTURE_ADDRESS"; @@ -40,8 +40,8 @@ impl Fixture { Self { listener } } - pub fn request(&self) -> CreateTerminal { - CreateTerminal { + pub fn request(&self) -> CreatePty { + CreatePty { program: env::current_exe().unwrap().to_str().unwrap().to_owned(), args: [ "--ignored", @@ -62,8 +62,6 @@ impl Fixture { .into(), cols: 80, rows: 24, - mode: Default::default(), - output_path: None, } } diff --git a/packages/process-daemon/tests/windows-daemon.rs b/packages/process-daemon/tests/windows-daemon.rs index 00efd2db3fd1..abc42353d7e3 100644 --- a/packages/process-daemon/tests/windows-daemon.rs +++ b/packages/process-daemon/tests/windows-daemon.rs @@ -18,7 +18,7 @@ use opencode_pty::protocol::{ AttachmentRole, Envelope, Request, Response, SubscriptionEvent, read_frame, read_subscription_event, write_frame, }; -use opencode_pty::service::{CreateTerminal, TerminalInfo, TerminalLifecycle}; +use opencode_pty::service::{CreatePty, ProcessInfo, ProcessMode, TerminalLifecycle}; use terminal_fixture::{Command as FixtureCommand, Deadline, Fixture, TempDir}; use windows_sys::Win32::Foundation::WAIT_OBJECT_0; use windows_sys::Win32::System::Threading::{ @@ -83,32 +83,28 @@ impl Daemon { (stream, response) } - fn create(&self, request: CreateTerminal) -> TerminalInfo { - let CreateTerminal { + fn create(&self, request: CreatePty) -> ProcessInfo { + let CreatePty { program, args, cwd, - title, - group_id, env, + group_id, + title, cols, rows, - mode, - output_path, } = request; - match self.request(Request::Create { + match self.request(Request::CreatePty { program, args, cwd, - title, - group_id, env, + group_id, + title, cols, rows, - mode, - output_path, }) { - Response::Created { terminal } => terminal, + Response::Created { process } => process, response => panic!("create: {response:?}"), } } @@ -139,14 +135,14 @@ impl Daemon { (stream, response) } - fn snapshot(&self, id: u64) -> (TerminalInfo, String) { + fn snapshot(&self, id: u64) -> (ProcessInfo, String) { match self.request(Request::Snapshot { id }) { - Response::Snapshot { terminal, text, .. } => (terminal, text), + Response::Snapshot { process, text, .. } => (process, text), response => panic!("snapshot: {response:?}"), } } - fn wait_text(&self, id: u64, expected: &str) -> (TerminalInfo, String) { + fn wait_text(&self, id: u64, expected: &str) -> (ProcessInfo, String) { let deadline = Instant::now() + Duration::from_secs(5); loop { let snapshot = self.snapshot(id); @@ -200,7 +196,7 @@ fn registration(child: &mut Child, directory: &Path) -> Registration { if let Ok(registration) = opencode_pty::daemon::read_registration(directory) && registration.pid == child.id() { - assert_eq!(registration.protocol, 7); + assert_eq!(registration.protocol, 8); assert_eq!( registration.socket, PathBuf::from(format!( @@ -519,7 +515,7 @@ fn natural_exit_delivers_contiguous_output_and_retains_final_state() { assert_eq!(info.output_tail, offset); assert!(text.contains("FINAL_DAEMON_MARKER"), "{text:?}"); let Response::Rows { - terminal: rows_info, + process: rows_info, lines, .. } = daemon.request(Request::ReadRows { @@ -568,7 +564,7 @@ fn natural_exit_delivers_contiguous_output_and_retains_final_state() { Response::Ok )); assert!( - matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } if terminals.is_empty()) + matches!(daemon.request(Request::List { mode: ProcessMode::Pty }), Response::Processes { processes: terminals } if terminals.is_empty()) ); assert!(matches!(daemon.request(Request::Shutdown), Response::Ok)); drop(owner); @@ -659,10 +655,10 @@ fn observer_disconnect_and_control_input_preserve_independent_terminals() { assert!(!first_text.contains("SECOND_ONLY_MARKER")); assert!(!second_text.contains("FIRST_ONLY_MARKER")); assert!( - matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } if terminals.len() == 2) + matches!(daemon.request(Request::List { mode: ProcessMode::Pty }), Response::Processes { processes: terminals } if terminals.len() == 2) ); assert!( - matches!(daemon.request(Request::Ping), Response::Pong { instance_id, pid, protocol: 7, .. } + matches!(daemon.request(Request::Ping), Response::Pong { instance_id, pid, protocol: 8, .. } if instance_id == daemon.registration.instance_id && pid == daemon.registration.pid) ); assert!(matches!(daemon.own(None).1, Response::Error { .. })); @@ -673,7 +669,7 @@ fn observer_disconnect_and_control_input_preserve_independent_terminals() { )); drop(first_control); assert!( - matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } + matches!(daemon.request(Request::List { mode: ProcessMode::Pty }), Response::Processes { processes: terminals } if terminals.len() == 1 && terminals[0].id == second.id) ); let input = b"after-first-exit"; From 0a38a5348e02b478839b814dcef0ed56e67ac2ba Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 00:10:12 +0000 Subject: [PATCH 05/11] docs(process-daemon): describe protocol v8 --- packages/process-daemon/README.md | 12 +- packages/process-daemon/SPEC.md | 186 ++++++++++++++++++++---------- 2 files changed, 131 insertions(+), 67 deletions(-) diff --git a/packages/process-daemon/README.md b/packages/process-daemon/README.md index c03ce089dfb7..2b28271794b7 100644 --- a/packages/process-daemon/README.md +++ b/packages/process-daemon/README.md @@ -17,7 +17,7 @@ terminals. Observer commands connect to an existing daemon without taking owners The service uses a private authenticated local byte stream (Unix socket or Windows named pipe) and an atomic registration file. -Integrations launch `opencode-pty daemon --name NAME [--runtime-dir DIR]` (protocol 7). +Integrations launch `opencode-pty daemon --name NAME [--runtime-dir DIR]` (protocol 8). The server must claim the daemon within 5 seconds by sending the authenticated framed envelope `{"token":"...","request":{"op":"own","instance_id":"..."}}`. The response is `{"type":"owned"}`; that connection stays open as the sole @@ -70,7 +70,7 @@ socket pair until a connection arrives, a stop or ownership change wakes it, an ownership deadline is due, or a two-second safety net elapses. Windows still polls every 25 ms. -The Windows named-pipe backend keeps protocol 7 and the registration fields +The Windows named-pipe backend keeps protocol 8 and the registration fields `instance_id`, `pid`, `protocol`, `socket`, `token`, and `features`. Treat `socket` as an opaque local endpoint: on Windows it is `\\.\pipe\opencode-pty-`, not a filesystem socket. A random per-instance name, exclusive first pipe instance, @@ -111,7 +111,7 @@ so the runtime directory must be on a local NTFS volume (Windows 10 1709 or later); there is no non-atomic fallback. The Windows Rust `TerminalClient` and interactive CLI are not ported. Integrations -can use protocol 7 directly and the minimal `daemon::PipeConnection` byte-stream +can use protocol 8 directly and the minimal `daemon::PipeConnection` byte-stream helper. Connect with `PipeConnection::connect_to(socket, pid)`, which rejects a pipe whose server is not the registered daemon process: after a crash the stale registration's pipe name is free for another local user to create. Integrations @@ -211,9 +211,9 @@ return all available rows. Soft-wrapped rows stay separate, trailing whitespace is trimmed, and blank rows (including trailing blank rows) are empty strings. The alternate screen never exposes primary-screen history. -The protocol 7 request is `{"op":"read_rows","id":1,"rows":30}`; +The protocol 8 request is `{"op":"read_rows","id":1,"rows":30}`; omitted or `null` `rows` uses the current height. Its response is -`{"type":"rows","terminal":{...},"lines":[...],"cursor_x":0,"cursor_y":0}`. +`{"type":"rows","process":{...},"lines":[...],"cursor_x":0,"cursor_y":0}`. Metadata, lines, and zero-based active-screen cursor coordinates come from one actor snapshot. Reading does not move the viewport, change selection, or take control. Existing snapshots, checkpoints, and replay are unchanged. @@ -311,7 +311,7 @@ can consume application terminal queries itself; the tests also check its cursor-inheritance query reaches our reader and Ghostty parser. The reusable fixture in `tests/support/terminal_fixture.rs` returns a -`CreateTerminal` request and uses a separate control/observation channel, so +`CreatePty` request and uses a separate control/observation channel, so daemon tests can reuse it without a shell or Rust client. Include it as `terminal_fixture`, call `Fixture::request`, create the terminal, then call `Fixture::connect`. `Command::Output` writes real console stdout; `Command::Read` diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index c10755168385..e47ef468c0e4 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -66,7 +66,7 @@ No borrowed libghostty object crosses a thread boundary. ## Row Reads `Request::ReadRows { id, rows: Option }` (`op: "read_rows"`) returns -`Response::Rows { terminal, lines, cursor_x, cursor_y }` (`type: "rows"`). Both +`Response::Rows { process, lines, cursor_x, cursor_y }` (`type: "rows"`). Both the service and Rust client expose `read_rows(id, rows)`. Omitted or null rows defaults to the current terminal height; zero is rejected. A larger count includes retained history, up to all available rows of the active buffer. @@ -86,15 +86,14 @@ checkpoint, or replay state is changed. Row reads have a 1 MiB budget for JSON-escaped row strings, array punctuation, and owned-string slots (including blank rows); formatting buffers are also bounded. Results exceeding that budget fail rather than truncate. Complete -responses remain subject to the transport's 8 MiB frame limit. Protocol 7 -supports `read_rows` alongside the existing snapshot and replay operations. +responses remain subject to the transport's 8 MiB frame limit. ## Titles -`TerminalInfo.title` is the effective title: the user title, else the latest +`pty.title` is the effective title: the user title, else the latest OSC 0/2 title, else the title given at creation. An empty OSC title (a program -resetting it) or an empty user title counts as unset. `TerminalInfo.user_title` -is always serialized, as `null` when unset. +resetting it) or an empty user title counts as unset. `pty.user_title` is +always serialized, as `null` when unset. `Request::Rename { id, title: Option }` (`op: "rename"`) sets the user title, or clears it when `title` is null or empty, and returns `Response::Ok`. @@ -105,57 +104,116 @@ once it is cleared. Rename and OSC updates publish `TitleChanged` with the effective title only when it changes; an OSC change hidden behind a user title publishes nothing. -`list` returns terminals in creation order. +## Processes + +Every process, PTY or pipe, is described by `ProcessInfo`: + +- `id`, `mode` (`"pty"` or `"pipe"`), `pid`, `command` (program and + arguments), `cwd`, `group_id` (an opaque string), `lifecycle`, + `output_head` and `output_tail` (absolute byte offsets), `started_at` and + `exited_at` (milliseconds since the Unix epoch; `exited_at` is `null` until + the exit is final, that is until `Exited` is sent, and never before + `started_at`). +- `pty`, set only for PTYs: `title`, `user_title`, `foreground_process`, + `cols`, `rows`. +- `pipe`, set only for pipe processes: `output_path`, `output_lines`, + `output_error` (left out when there is none) and `metadata`. + +Exactly one of `pty` and `pipe` is present, matching `mode`; the other key is +left out. + +`list { mode }` (`mode` required) returns the processes of that mode in +creation order as `Response::Processes { processes }`. `get { id }` returns one +process of either mode as `Response::Process { process }`, or an error for an +unknown id. Both refresh a PTY's foreground process first. + +`Created`, `Snapshot`, `Rows` and `Attached` carry the info as `process`. ## Pipe Processes -`Create` takes `mode: "pty" | "pipe"` (default `"pty"`). A pipe process runs -any command without a terminal; it is Unix-only for now, and only daemons that -list the `pipe` feature accept it. Older daemons ignore `mode` and would start -a PTY instead, so clients check `features` first. +`create_pty { program, args, cwd, env, group_id, title, cols, rows }` starts a +PTY. `create_pipe { program, args, cwd, env, group_id, output_path, metadata }` +starts a pipe process: any command without a terminal. Both return +`Created { process }`. Pipe processes are Unix-only for now; a Windows daemon +answers `create_pipe` with an error. -- `title`, `cols` and `rows` are optional for pipe processes; the title - defaults to the program's file name and the size is reported as `0x0`. +- The child gets exactly `env`: the daemon's own environment is not + inherited, and no `TERM` or `COLORTERM` is added. `cwd` must be absolute. - The child gets its own process group, an empty signal mask with default actions (the daemon blocks SIGTERM and SIGINT, and std keeps the mask across - exec), stdin on `/dev/null`, and one pipe for - both stdout and stderr, so their bytes stay in order. No `TERM` or - `COLORTERM` is added, and nothing parses the output: there is no emulator, + exec), stdin on `/dev/null`, and one pipe for both stdout and stderr, so + their bytes stay in order. Nothing parses the output: there is no emulator, no terminal replies, no OSC title and no foreground process. -- Output goes through the same raw replay and subscriptions as a PTY, with the - same absolute offsets. A pipe has no controller or observer distinction for - writing, since it accepts no input. +- `output_path` must be absolute. The daemon opens it before spawning, + truncating an existing file or creating one with mode `0666` minus the + daemon's umask, and refuses a symlink as the final path component. The + request is fully validated (cwd, paths, metadata size) before the file is + opened, so a rejected request never creates or truncates it; a path that + cannot be opened fails the request without starting the process. +- The reader writes every output byte to the file before handing it to the + actor. The file is the only copy of the output: a pipe process has no replay + buffer, so `replay` and `subscribe` return an empty replay (with correct + offsets) and live events only. Output never needs to cross the socket. +- A write error stops the file and sets `pipe.output_error`; the process keeps + running and its output is still counted in the offsets. - `write`, `resize`, `control`, `input`, `snapshot`, `read_rows` and `rename` - fail for pipe processes. `replay`, `subscribe` and `terminate` - work. -- Exit is published once both the exit status and the end of output are - known. A descendant that keeps the pipe open (`cmd &`) delays that by at - most one second after the child exits; output after that is not read. -- `Exited` (the lifecycle and the subscription response) carries `signal`, - the signal's name such as `"SIGTERM"`, when a pipe process was killed by - one; `exit_code` is then null. The field is left out when there is no - signal, so PTY exits serialize as before. + fail for pipe processes; `wait`, `signal` and `set_metadata` fail for PTYs. +- Exit is published once both the exit status and the end of output are known. + A descendant that keeps the pipe open (`cmd &`, dev servers) delays that by + at most one second after the child exits; the reader then closes the pipe, + so leftover writers get `SIGPIPE`/`EPIPE`. This is deliberate: the process + is reported as finished, and nothing reads what comes later. The file is + closed before the exit is published. +- `Exited` and the lifecycle carry `signal`, the signal's name such as + `"SIGTERM"`, when the process was killed by one; `exit_code` is then null. + `signal` is left out when there is none. - An exited pipe process stays listed, as a zombie, until `terminate`. The - zombie keeps its PID and process group ID reserved, so `terminate` can kill - any descendants left in the group without reaching an unrelated process. - `terminate` sends SIGKILL to the group and to the child, reaps it, and never - waits on a descendant that left the group and still holds the pipe. - -`Create` also takes `output_path` for pipe processes (feature `output_path`): -an absolute path the daemon opens before spawning (created with mode `0600` -if missing, otherwise appended to) and writes every output byte to, before -the byte reaches the replay buffer or any subscriber. The file therefore keeps -all output, including what falls out of the 2 MiB replay window or arrives -while no server is attached during a handoff. A path that cannot be opened -fails `create` without starting the process; a later write error stops the -file, sets `TerminalInfo.output_error` (omitted otherwise), and the process -keeps running. The file is closed before the exit is published, and a symlink -as the final path component is refused. `output_path` is rejected -for PTYs. - -`List` takes `mode` too. Without it, `list` returns PTYs only, so servers that -predate pipe processes never see them. Clients leave `mode` out of the request -when it is unset: older daemons reject a `list` with any extra field. + zombie keeps its PID and process group ID reserved, so killing the group can + never reach an unrelated process. `terminate` sends SIGKILL to the group and + to the child, reaps it and removes the process; it never waits on a + descendant that left the group and still holds the pipe. The daemon never + deletes output files. + +### Line count + +`pipe.output_lines` counts the `\n` bytes written to the file. The reader +counts them in each successfully written slice (including a partial write +before an error), and the actor adds them in the same step that advances +`output_tail`, under the same lock, so `get` and `list` always return a +matching pair. After a write error nothing more is counted. + +The file can hold a little more than `output_tail`, since the reader writes +before the actor counts: read only up to `output_tail` when pairing bytes with +the count. After `Exited` the file is closed and its size equals +`final_offset`, unless `output_error` is set: output read after a write error +still advances `output_tail` and `final_offset` but is not in the file, so use +the file's real size. + +### Wait, signal and metadata + +`wait { id }` (pipe only) is held open until the process exits, then answered +with one `Exited` and closed. It never sends output. A process that already +exited is answered at once, so `list` followed by `wait` after a server +restart cannot miss an exit. An unknown id or a PTY is answered with an +`Error` at once. If the process is terminated first, the reply is +`Error { message: "terminated" }`; if the daemon stops, the connection closes +without a reply. Several waiters may wait on one process. + +`Exited` carries `exit_code`, `signal`, `final_offset`, `output_lines` (pipe +only; left out for PTYs), `output_error` (left out when there is none) and +`exited_at`. PTY subscriptions send the same response. + +`signal { id, signal }` (pipe only; `"SIGTERM"`, `"SIGINT"` or `"SIGKILL"`) +signals the process group and the child, and returns `Ok`. Unlike +`terminate`, it keeps the process: `wait` then gets a normal `Exited` with +`signal`. Signalling an exited process is allowed and reaches whatever is left +of its group (an empty group is not an error); after `terminate` the process +is gone and `signal` fails. + +`metadata` on `create_pipe` is an opaque JSON object the daemon stores and +returns as `pipe.metadata` without interpreting it. `set_metadata { id, +metadata }` (pipe only) replaces it and returns `Ok`. Both reject metadata +over 1 MiB of serialized JSON. ## Failure Boundary @@ -174,19 +232,25 @@ connected owner; the superseded connection can no longer prepare handoffs or stop the daemon, and its disconnect does not affect the new owner. The playground holds ownership until it exits; other CLI commands only observe or operate an existing daemon and never start one. -Protocol v7 uses four-byte big-endian framing with bounded UTF-8 JSON control -frames and tagged raw binary output frames. It supports ping, create, list, -write, resize, atomic interaction promotion, snapshot, read_rows, replay, replay-to-live -subscriptions, title updates, rename, controller takeover, terminate, and destructive service -shutdown, ownership acquisition, and restart handoff preparation. - -`Pong` and the registration carry `features`, the daemon's additive -capabilities (currently `["rename", "pipe", "output_path"]` on Unix and `["rename"]` on Windows). Additive changes, such as a new request -or field older clients can ignore, extend `features` and keep the protocol -version; clients check `features` before sending a new request, since a daemon -that cannot parse one drops the connection. Only breaking changes bump -`PROTOCOL_VERSION`, which clients reject on mismatch. A missing `features` -means none. +Protocol v8 uses four-byte big-endian framing with bounded UTF-8 JSON control +frames and tagged raw binary output frames. Requests (`op`): `ping`, `own`, +`prepare_handoff`, `create_pty`, `create_pipe`, `list`, `get`, `wait`, +`signal`, `set_metadata`, `write`, `resize`, `control` (atomic interaction +promotion), `input`, `snapshot`, `read_rows`, `rename`, `replay`, `subscribe` +(replay-to-live, with title, resize, controller and exit events), +`terminate` and `shutdown` (destructive). + +A request the daemon cannot parse is answered with +`Error { message: "unsupported request: " }` for an unknown `op` (or +`"invalid request: ..."` for bad fields) instead of dropping the +connection, once the token has been checked. Clients can therefore probe for +a request. + +`Pong` and the registration carry `features` (required), the daemon's additive +capabilities. Protocol 8 needs none, so the list is empty. Later additive +changes, such as a new request or field older clients can ignore, extend +`features` and keep the protocol version. Only breaking changes bump +`PROTOCOL_VERSION`, which clients reject on mismatch. Registration is written atomically with private permissions and contains an instance ID, PID, protocol version, features, socket path, and random credential. A From cdaee6b05ac51b262a8ca33a0b24d205be60e1fd Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 00:33:32 +0000 Subject: [PATCH 06/11] fix(process-daemon): report a failed pipe reader as an output error --- packages/process-daemon/src/service.rs | 121 +++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 7 deletions(-) diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index 31201b373d6c..8b07c1cb895a 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -1568,10 +1568,14 @@ fn run_actor(config: ActorConfig) -> Result<()> { } Ok(ActorMessage::ReaderFailed(message)) if mode == ProcessMode::Pipe => { // The pipe reader has stopped and closed the output file, so - // nothing more can arrive: let the exit still be published. + // nothing more can arrive: let the exit still be published, + // and report the failure where the exit carries it. reader_eof = true; - if let Ok(mut value) = info.write() { - value.lifecycle = TerminalLifecycle::Failed { message }; + if let Ok(mut value) = info.write() + && let Some(pipe) = value.pipe.as_mut() + { + pipe.output_error + .get_or_insert_with(|| format!("failed to read process output: {message}")); } } Ok(ActorMessage::ReaderFailed(message)) | Ok(ActorMessage::WriterFailed(message)) => { @@ -2203,6 +2207,11 @@ mod tests { // Real actor/parser and bounded channels, without OS pipe capacity or a // large output flood. Native child/handle coverage lives in tests/runtime.rs. fn test_actor() -> (TerminalHandle, Receiver) { + test_actor_in(ProcessMode::Pty) + } + + /// A pipe-mode actor has no terminal and a `pipe` info group. + fn test_actor_in(mode: ProcessMode) -> (TerminalHandle, Receiver) { let (actor_tx, messages) = bounded(ACTOR_QUEUE_CAPACITY); let (writes, writer_rx) = bounded(1); let (shutdown_tx, shutdown) = bounded(0); @@ -2215,15 +2224,31 @@ mod tests { } }); let (init_tx, init_rx) = std_mpsc::sync_channel(1); - let info = Arc::new(RwLock::new(test_info())); + let info = match mode { + ProcessMode::Pty => test_info(), + ProcessMode::Pipe => ProcessInfo { + mode, + pty: None, + pipe: Some(PipeInfo { + output_path: PathBuf::from("/out"), + output_lines: 0, + output_error: None, + metadata: PipeMetadata::new(), + }), + ..test_info() + }, + }; + let info = Arc::new(RwLock::new(info)); let actor_info = Arc::clone(&info); let (actor_done_tx, actor_done) = bounded::<()>(0); let actor = thread::spawn(move || { let _actor_done = actor_done_tx; run_actor(ActorConfig { - mode: ProcessMode::Pty, + mode, master: ActorMaster { - pty: Some(Box::new(TestMaster)), + pty: mode + .is_pty() + .then(|| Box::new(TestMaster) as Box), #[cfg(windows)] close_tx, }, @@ -2242,7 +2267,7 @@ mod tests { ( TerminalHandle { order: 0, - mode: ProcessMode::Pty, + mode, actor_tx, actor_done, shutdown_tx: Mutex::new(Some(shutdown_tx)), @@ -2335,6 +2360,88 @@ mod tests { } } + #[test] + fn a_failed_pipe_reader_still_publishes_the_exit_with_the_error() { + for failed_first in [true, false] { + let (actor, _writer) = test_actor_in(ProcessMode::Pipe); + let wait = { + let (reply, exit) = bounded(1); + actor.actor_tx.send(ActorMessage::Wait { reply }).unwrap(); + exit + }; + actor + .actor_tx + .send(ActorMessage::Output { + bytes: b"a\n".to_vec(), + newlines: 1, + }) + .unwrap(); + let exited = || ActorMessage::ChildExited(Ok(Some(1))); + if failed_first { + actor + .actor_tx + .send(ActorMessage::ReaderFailed("boom".into())) + .unwrap(); + // A round trip, so the failure has been handled. + actor + .request(|reply| ActorMessage::Replay { offset: 0, reply }) + .unwrap(); + // Not exited yet: no `Failed` lifecycle in the meantime. + let info = actor.info(); + assert_eq!(info.lifecycle, TerminalLifecycle::Running); + assert_eq!(info.exited_at, None); + assert!(wait.try_recv().is_err()); + actor.actor_tx.send(exited()).unwrap(); + } else { + actor.actor_tx.send(exited()).unwrap(); + actor + .request(|reply| ActorMessage::Replay { offset: 0, reply }) + .unwrap(); + assert!(wait.try_recv().is_err()); + actor + .actor_tx + .send(ActorMessage::ReaderFailed("boom".into())) + .unwrap(); + } + let exit = wait.recv_timeout(Duration::from_secs(5)).unwrap(); + let error = "failed to read process output: boom".to_string(); + assert_eq!(exit.output_error.as_ref(), Some(&error)); + assert_eq!((exit.exit_code, exit.output_lines), (Some(1), Some(1))); + let info = actor.info(); + assert_eq!( + info.lifecycle, + TerminalLifecycle::Exited { + exit_code: Some(1), + signal: None + } + ); + assert_eq!(info.exited_at, Some(exit.exited_at)); + assert_eq!(info.pipe.unwrap().output_error, Some(error)); + actor.shutdown().unwrap(); + } + } + + #[test] + fn a_failed_pipe_reader_keeps_an_earlier_output_file_error() { + let (actor, _writer) = test_actor_in(ProcessMode::Pipe); + let (reply, wait) = bounded(1); + for message in [ + ActorMessage::Wait { reply }, + ActorMessage::ChildExited(Ok(Some(0))), + #[cfg(unix)] + ActorMessage::OutputFileFailed("disk full".into()), + ActorMessage::ReaderFailed("boom".into()), + ] { + actor.actor_tx.send(message).unwrap(); + } + let exit = wait.recv_timeout(Duration::from_secs(5)).unwrap(); + #[cfg(unix)] + assert_eq!(exit.output_error.as_deref(), Some("disk full")); + #[cfg(not(unix))] + assert!(exit.output_error.is_some()); + actor.shutdown().unwrap(); + } + #[test] fn late_attachments_receive_final_exit_after_replay_and_control() { let (actor, _writer) = test_actor(); From 6b09655feaad035228de902ddc8d5e20af9f44dc Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 00:33:32 +0000 Subject: [PATCH 07/11] fix(process-daemon): read errno right after each signal call --- packages/process-daemon/src/service/pipe.rs | 75 ++++++++++++++++++--- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index d85aa1389121..91a1ecfdecf1 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -293,14 +293,10 @@ impl Process { } let pid = i32::try_from(self.pid).context("invalid pid")?; // SAFETY: the unreaped child keeps its PID and group ID reserved. + check_signal(unsafe { libc::killpg(pid, signal) })?; // Signal the child directly too, in case it moved to another group. - for result in unsafe { [libc::killpg(pid, signal), libc::kill(pid, signal)] } { - // A zombie group with no live members gives ESRCH on macOS. - if result == -1 && Errno::last() != Errno::ESRCH { - return Err(io::Error::last_os_error()).context("failed to signal process"); - } - } - Ok(()) + // SAFETY: as above. + check_signal(unsafe { libc::kill(pid, signal) }) } /// Call only after the waiter has returned: kill the group first, so the @@ -324,6 +320,18 @@ impl Process { } } +/// Reads errno right after a `kill`/`killpg` result. A zombie group with no +/// live members gives ESRCH on macOS, which is not an error here. +fn check_signal(result: libc::c_int) -> Result<()> { + if result == -1 { + let errno = Errno::last(); + if errno != Errno::ESRCH { + return Err(io::Error::from(errno)).context("failed to signal process"); + } + } + Ok(()) +} + #[derive(Debug)] struct GroupKiller(Arc); @@ -497,7 +505,8 @@ fn read_output( // subscribers have seen, even if the actor is backpressured. let mut newlines = 0; if let Some(file) = &mut sink - && let Err(error) = write_counting(file, &buffer[..length], &mut newlines) + && let Err(error) = + write_counting(&mut *file, &buffer[..length], &mut newlines) { // Ordered before this chunk's output, so the error is // visible no later than the bytes the file lacks. @@ -534,7 +543,7 @@ fn read_output( /// Writes all of `bytes`, adding to `newlines` the `\n` bytes that reached /// the file, including those of a partial write before an error. -fn write_counting(file: &mut File, bytes: &[u8], newlines: &mut u64) -> io::Result<()> { +fn write_counting(mut file: impl Write, bytes: &[u8], newlines: &mut u64) -> io::Result<()> { let mut rest = bytes; while !rest.is_empty() { match file.write(rest) { @@ -552,3 +561,51 @@ fn write_counting(file: &mut File, bytes: &[u8], newlines: &mut u64) -> io::Resu } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + /// Accepts `accept` bytes in small writes, then fails. + struct FailingWriter { + accept: usize, + written: Vec, + } + + impl Write for FailingWriter { + fn write(&mut self, bytes: &[u8]) -> io::Result { + let room = self.accept - self.written.len(); + if room == 0 { + return Err(io::Error::from_raw_os_error(libc::ENOSPC)); + } + let length = bytes.len().min(room).min(3); + self.written.extend_from_slice(&bytes[..length]); + Ok(length) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + } + + #[test] + fn a_partial_write_counts_only_the_newlines_written() { + let mut writer = FailingWriter { + accept: 7, + written: Vec::new(), + }; + let mut newlines = 0; + let error = write_counting(&mut writer, b"a\nb\nc\nd\ne\n", &mut newlines).unwrap_err(); + assert_eq!(error.raw_os_error(), Some(libc::ENOSPC)); + assert_eq!(writer.written, b"a\nb\nc\nd"); + assert_eq!(newlines, 3); + + let mut writer = FailingWriter { + accept: 100, + written: Vec::new(), + }; + let mut newlines = 1; + write_counting(&mut writer, b"x\ny\n", &mut newlines).unwrap(); + assert_eq!(newlines, 3); + } +} From 18df22e9ad4dbf76172f11ef3ce61d7822d9fe89 Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 00:33:32 +0000 Subject: [PATCH 08/11] fix(process-daemon): tell bad request fields from unknown requests --- packages/process-daemon/src/protocol.rs | 41 +++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index 0d42fced2490..1624fdaa5ceb 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -248,19 +248,22 @@ pub fn read_envelope(reader: impl Read) -> Result<(String, Result(raw.request).map_err(|error| { - if error.to_string().starts_with("unknown variant") { - format!("unsupported request: {op}") - } else { - format!("invalid {op} request: {error}") - } - }); + let Some(op) = raw.request.get("op").and_then(serde_json::Value::as_str) else { + return Ok((raw.token, Err("invalid request: missing field `op`".into()))); + }; + let op = op.to_string(); + // With only the tag, the one possible "unknown variant" is the op itself; + // a bad enum value in a known request must not look like an unknown op. + let known = serde_json::from_value::(serde_json::json!({ "op": op })).map_or_else( + |error| !error.to_string().starts_with("unknown variant"), + |_| true, + ); + let request = if known { + serde_json::from_value::(raw.request) + .map_err(|error| format!("invalid {op} request: {error}")) + } else { + Err(format!("unsupported request: {op}")) + }; Ok((raw.token, request)) } @@ -592,6 +595,18 @@ mod tests { assert_eq!(request.unwrap_err(), "unsupported request: teleport"); let (_, request) = parse(json!({"op": "get"})); assert!(request.unwrap_err().starts_with("invalid get request")); + let (_, request) = parse(json!({"op": "signal", "id": 1, "signal": "SIGHUP"})); + assert!( + request + .unwrap_err() + .starts_with("invalid signal request: unknown variant `SIGHUP`") + ); + let (_, request) = parse(json!({"op": "list", "mode": "bogus"})); + assert!(request.unwrap_err().starts_with("invalid list request")); + let (_, request) = parse(json!({"id": 1})); + assert_eq!(request.unwrap_err(), "invalid request: missing field `op`"); + let (_, request) = parse(json!({"op": 3})); + assert_eq!(request.unwrap_err(), "invalid request: missing field `op`"); let (_, request) = parse(json!({"op": "ping"})); assert!(matches!(request, Ok(Request::Ping))); // A frame that is not an envelope at all still fails the connection. From 3587d798cf47a74c11eb7e0547b552e0467c8011 Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 00:33:32 +0000 Subject: [PATCH 09/11] test(process-daemon): cover get on a PTY and old registrations --- packages/process-daemon/src/client.rs | 29 ++++++++++++++++++++++++ packages/process-daemon/tests/service.rs | 7 ++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/process-daemon/src/client.rs b/packages/process-daemon/src/client.rs index 3c3abdbccc8c..df85c0f4c483 100644 --- a/packages/process-daemon/src/client.rs +++ b/packages/process-daemon/src/client.rs @@ -869,3 +869,32 @@ fn optional_id(value: &str, active: Option) -> Result { parse_id(value) } } + +#[cfg(all(test, unix))] +mod tests { + use super::*; + + #[test] + fn discover_rejects_another_protocol_version() { + let directory = env::temp_dir().join(format!( + "opencode-pty-client-{:032x}", + rand::random::() + )); + std::fs::create_dir_all(&directory).unwrap(); + std::fs::write( + directory.join(crate::daemon::REGISTRATION_FILE), + serde_json::to_vec(&serde_json::json!({ + "instance_id": "old", "pid": 1, "protocol": 7, + "socket": directory.join("missing.sock"), "token": "t", "features": ["rename"], + })) + .unwrap(), + ) + .unwrap(); + let error = TerminalClient::discover(&directory).err().unwrap(); + let _ = std::fs::remove_dir_all(&directory); + assert_eq!( + error.to_string(), + "opencode-pty protocol mismatch: service=7, client=8" + ); + } +} diff --git a/packages/process-daemon/tests/service.rs b/packages/process-daemon/tests/service.rs index c19faf85bf58..8c7b92ef30b9 100644 --- a/packages/process-daemon/tests/service.rs +++ b/packages/process-daemon/tests/service.rs @@ -370,9 +370,12 @@ fn foreground_process_tracks_deepest_tty_attached_job() { Instant::now() < deadline, "timed out waiting for foreground process" ); - service - .list(ProcessMode::Pty) + // `get` refreshes the foreground process like `list` does. + let info = service + .get(terminal.id) .expect("foreground process refreshed"); + assert_eq!(info.mode, ProcessMode::Pty); + assert!(info.pipe.is_none()); match controller.events.recv_timeout(Duration::from_millis(50)) { Ok(StreamEvent::ForegroundProcessChanged { process: Some(process), From 369dc6294f491b77629f27014318d3b279eff94d Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 00:33:32 +0000 Subject: [PATCH 10/11] docs(process-daemon): correct pipe validation and daemon start notes --- packages/process-daemon/SPEC.md | 10 ++++++---- packages/process-daemon/src/transport/windows.rs | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index e47ef468c0e4..ec4180bc76dd 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -147,9 +147,10 @@ answers `create_pipe` with an error. - `output_path` must be absolute. The daemon opens it before spawning, truncating an existing file or creating one with mode `0666` minus the daemon's umask, and refuses a symlink as the final path component. The - request is fully validated (cwd, paths, metadata size) before the file is - opened, so a rejected request never creates or truncates it; a path that - cannot be opened fails the request without starting the process. + request is validated (program, absolute paths, metadata size) before the + file is opened, so such a rejected request never creates or truncates it; a + path that cannot be opened fails the request without starting the process. + A missing program or `cwd` fails at spawn, after the file was opened. - The reader writes every output byte to the file before handing it to the actor. The file is the only copy of the output: a pipe process has no replay buffer, so `replay` and `subscribe` return an empty replay (with correct @@ -282,6 +283,7 @@ registration, leaving the rest to a later sweep. The sweep checks liveness through the registered process object. OpenCode chooses a fresh UUID runtime directory for each server, independent of -the database. It starts the daemon only when the first terminal is created. +the database. It starts the daemon only when the first terminal or pipe +process is created. Only an explicit restart handoff descriptor lets a replacement server reuse the previous daemon's runtime directory and claim its ownership. diff --git a/packages/process-daemon/src/transport/windows.rs b/packages/process-daemon/src/transport/windows.rs index 5b2e5f26e761..31b60a1519e2 100644 --- a/packages/process-daemon/src/transport/windows.rs +++ b/packages/process-daemon/src/transport/windows.rs @@ -286,7 +286,7 @@ impl Connection { Ok(Cancellation(Arc::clone(&self.pipe))) } - /// Protocol 7 has an explicit response/final event, not a pipe half-close. + /// The protocol has an explicit response/final event, not a pipe half-close. /// Retain the pipe until the client reads that frame and closes its end. /// An uncooperative peer is cancelled after a bounded grace period. pub(crate) fn finish_response(&mut self) -> io::Result<()> { From 4b82d027e9294b30b59facb87f558006edc224aa Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 02:26:54 +0000 Subject: [PATCH 11/11] fix(process-daemon): accept EPERM when signalling an exited child on macOS --- packages/process-daemon/src/service/pipe.rs | 27 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 91a1ecfdecf1..09672926b9fa 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -293,10 +293,10 @@ impl Process { } let pid = i32::try_from(self.pid).context("invalid pid")?; // SAFETY: the unreaped child keeps its PID and group ID reserved. - check_signal(unsafe { libc::killpg(pid, signal) })?; + check_signal(unsafe { libc::killpg(pid, signal) }, self.pid)?; // Signal the child directly too, in case it moved to another group. // SAFETY: as above. - check_signal(unsafe { libc::kill(pid, signal) }) + check_signal(unsafe { libc::kill(pid, signal) }, self.pid) } /// Call only after the waiter has returned: kill the group first, so the @@ -322,10 +322,12 @@ impl Process { /// Reads errno right after a `kill`/`killpg` result. A zombie group with no /// live members gives ESRCH on macOS, which is not an error here. -fn check_signal(result: libc::c_int) -> Result<()> { +/// Signalling an exited child whose group is empty fails with `ESRCH`, or +/// with `EPERM` on macOS (the zombie is still there); neither is an error. +fn check_signal(result: libc::c_int, pid: u32) -> Result<()> { if result == -1 { let errno = Errno::last(); - if errno != Errno::ESRCH { + if errno != Errno::ESRCH && !(errno == Errno::EPERM && has_exited(pid)) { return Err(io::Error::from(errno)).context("failed to signal process"); } } @@ -351,6 +353,23 @@ enum Exit { Signal(String), } +/// Whether the child has exited, without reaping it. +fn has_exited(pid: u32) -> bool { + // SAFETY: siginfo_t is plain data, and all-zero is a valid value. + let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() }; + // SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie. + let result = unsafe { + libc::waitid( + libc::P_PID, + pid as libc::id_t, + &mut info, + libc::WEXITED | libc::WNOWAIT | libc::WNOHANG, + ) + }; + // SAFETY: waitid filled `info`; a zero PID means the child is still running. + result == 0 && unsafe { info.si_pid() } != 0 +} + /// Waits for the child to exit without reaping it. fn wait_exit(pid: u32) -> io::Result { loop {