Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

codec 66: carry require_cwd on the wire so a refused directory is an error, not $HOME

RoversX385edb1ebc54
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index 1e6ce707..4d3cb4f1 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -520,7 +520,9 @@ macro_rules! pdu {
/// (GetPaneRenderChangesResponse.keyboard_encoding). Its own number,
/// because a server built between the two changes would otherwise
/// answer 64 and mis-decode every render push.
-pub const CODEC_VERSION: usize = 65;
+/// 66: CommandSpec carries require_cwd, so a spawn whose named directory
+/// cannot be opened fails on the server instead of landing in `$HOME`.
+pub const CODEC_VERSION: usize = 66;
// Defines the Pdu enum.
// Each struct has an explicit identifying number.
@@ -1883,15 +1885,17 @@ mod golden {
cwd: Some(b"/tmp".to_vec()),
umask: Some(0o022),
controlling_tty: false,
+ require_cwd: false,
}
}
- // The v60 baseline for CommandSpec, captured when the type was
- // introduced. The other literals guard against changing an old format;
- // this one guards against accidentally changing the new one.
+ // The CommandSpec baseline, captured at v60 when the type was
+ // introduced and re-captured for codec 66 (the trailing `0` is
+ // `require_cwd`). The other literals guard against changing an old
+ // format; this one guards against accidentally changing the new one.
const COMMAND_SPEC: &[u8] = &[
2, 4, 112, 114, 111, 103, 3, 102, 128, 111, 2, 4, 80, 65, 84, 72, 8, 47, 117, 115, 114, 47,
- 98, 105, 110, 1, 3, 70, 79, 79, 3, 98, 97, 114, 0, 1, 4, 47, 116, 109, 112, 1, 18, 0,
+ 98, 105, 110, 1, 3, 70, 79, 79, 3, 98, 97, 114, 0, 1, 4, 47, 116, 109, 112, 1, 18, 0, 0,
];
#[test]
@@ -2068,6 +2072,7 @@ mod test {
cwd: Some(b"/tmp".to_vec()),
umask: Some(0o022),
controlling_tty: false,
+ require_cwd: false,
};
let pdus = [
@@ -2228,7 +2233,7 @@ mod test {
// The exact assertion is the tripwire: whoever bumps the codec must
// come here, confirm the round-trips still cover the new version,
// and advance it deliberately.
- assert_eq!(CODEC_VERSION, 65);
+ assert_eq!(CODEC_VERSION, 66);
use thinkterm_proto::{AgentEvidence, AgentState, AgentStatus};
fn round_trip(pdu: Pdu) {
diff --git a/mux/src/command_spec.rs b/mux/src/command_spec.rs
index fece0b12..0013a32e 100644
--- a/mux/src/command_spec.rs
+++ b/mux/src/command_spec.rs
@@ -27,6 +27,7 @@ impl CommandSpecExt for CommandSpec {
cwd: cmd.get_cwd().map(|d| os_to_bytes(d)),
umask: umask_of(cmd),
controlling_tty: cmd.get_controlling_tty(),
+ require_cwd: cmd.get_require_cwd(),
}
}
@@ -56,6 +57,7 @@ impl CommandSpecExt for CommandSpec {
}
apply_umask(&mut cmd, self.umask);
cmd.set_controlling_tty(self.controlling_tty);
+ cmd.set_require_cwd(self.require_cwd);
cmd
}
}
@@ -170,6 +172,17 @@ mod tests {
assert_eq!(back.get_umask(), Some(0o022));
}
+ /// The flag is what turns a refused cwd into an error on the server
+ /// instead of a silent `$HOME`; losing it re-opens that defect.
+ #[test]
+ fn require_cwd_survives() {
+ let mut cmd = CommandBuilder::new("prog");
+ cmd.cwd("/some/dir");
+ cmd.set_require_cwd(true);
+ assert!(round_trip(&cmd).get_require_cwd());
+ assert!(!round_trip(&CommandBuilder::new("prog")).get_require_cwd());
+ }
+
/// cwd survives byte-for-byte.
#[test]
fn cwd_survives() {
diff --git a/mux/src/domain.rs b/mux/src/domain.rs
index 8aa4fd39..4cfc89e7 100644
--- a/mux/src/domain.rs
+++ b/mux/src/domain.rs
@@ -873,11 +873,21 @@ pub struct RequiredCwdUnavailable {
pub detail: String,
}
+const REQUIRED_CWD_PHRASE: &str = " cannot be opened, and this command requires it: ";
+const REQUIRED_CWD_TAG: &str = " [required-cwd:";
+
+/// The message doubles as the wire form: a mux server reports errors as one
+/// string, so the GUI recovers this type with `from_message` on that side.
impl std::fmt::Display for RequiredCwdUnavailable {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
+ let kind = match self.kind {
+ std::io::ErrorKind::PermissionDenied => "permission-denied",
+ std::io::ErrorKind::NotFound => "not-found",
+ _ => "other",
+ };
write!(
fmt,
- "Directory {} cannot be opened, and this command requires it: {}",
+ "Directory {}{REQUIRED_CWD_PHRASE}{}{REQUIRED_CWD_TAG}{kind}]",
self.dir.display(),
self.detail
)
@@ -886,6 +896,28 @@ impl std::fmt::Display for RequiredCwdUnavailable {
impl std::error::Error for RequiredCwdUnavailable {}
+impl RequiredCwdUnavailable {
+ /// Recover the refusal from a message a server sent back, wherever the
+ /// server's own prefixes put it. `None` for any other message.
+ pub fn from_message(message: &str) -> Option<Self> {
+ let phrase_at = message.find(REQUIRED_CWD_PHRASE)?;
+ let dir_at = message[..phrase_at].rfind("Directory ")? + "Directory ".len();
+ let rest = &message[phrase_at + REQUIRED_CWD_PHRASE.len()..];
+ let tag_at = rest.rfind(REQUIRED_CWD_TAG)?;
+ let close = rest[tag_at + REQUIRED_CWD_TAG.len()..].find(']')?;
+ let kind = match &rest[tag_at + REQUIRED_CWD_TAG.len()..][..close] {
+ "permission-denied" => std::io::ErrorKind::PermissionDenied,
+ "not-found" => std::io::ErrorKind::NotFound,
+ _ => std::io::ErrorKind::Other,
+ };
+ Some(Self {
+ dir: PathBuf::from(&message[dir_at..phrase_at]),
+ kind,
+ detail: rest[..tag_at].to_string(),
+ })
+ }
+}
+
/// Whether a cwd that could not be listed should fail the spawn outright.
/// Pulled out so the `sudo -i` regression is pinned by a test: making every
/// inherited PermissionDenied fatal would turn "split a pane out of a root
@@ -897,7 +929,7 @@ fn required_cwd_error_is_fatal(require_cwd: bool) -> bool {
#[cfg(test)]
mod cwd_refusal_tests {
- use super::required_cwd_error_is_fatal;
+ use super::{required_cwd_error_is_fatal, RequiredCwdUnavailable};
/// The `sudo -i` case, and every other pane that merely inherited its cwd:
/// a refusal still degrades to the home directory.
@@ -911,4 +943,31 @@ mod cwd_refusal_tests {
fn every_requested_cwd_error_is_fatal() {
assert!(required_cwd_error_is_fatal(true));
}
+
+ /// The server sends its error as one string with its own prefixes; the
+ /// GUI must still get the directory and the kind back out of it.
+ #[test]
+ fn the_refusal_survives_a_trip_through_a_server_message() {
+ let refused = RequiredCwdUnavailable {
+ dir: std::path::PathBuf::from("/Users/x/Documents/a b: c"),
+ kind: std::io::ErrorKind::PermissionDenied,
+ detail: "Operation not permitted (os error 1)".to_string(),
+ };
+ let message = format!("Error: spawn: {refused}");
+ let back = RequiredCwdUnavailable::from_message(&message).expect("recovered");
+ assert_eq!(back.dir, refused.dir);
+ assert_eq!(back.kind, refused.kind);
+ assert_eq!(back.detail, refused.detail);
+
+ let missing = RequiredCwdUnavailable {
+ kind: std::io::ErrorKind::NotFound,
+ ..refused
+ };
+ assert_eq!(
+ RequiredCwdUnavailable::from_message(&missing.to_string())
+ .map(|back| back.kind),
+ Some(std::io::ErrorKind::NotFound)
+ );
+ assert!(RequiredCwdUnavailable::from_message("Error: something else").is_none());
+ }
}
diff --git a/pty/src/cmdbuilder.rs b/pty/src/cmdbuilder.rs
index 72dd7ba3..772019ac 100644
--- a/pty/src/cmdbuilder.rs
+++ b/pty/src/cmdbuilder.rs
@@ -837,8 +837,8 @@ mod tests {
/// the new field in thinkterm_proto::CommandSpec (and bump
/// CODEC_VERSION) or record here why it does not travel.
///
- /// `require_cwd` deliberately does not travel: it is local-domain policy,
- /// and a remote server re-derives its own against its own filesystem.
+ /// `require_cwd` travels since codec 66: the local session host spawns in
+ /// a separate process, and the refusal has to reach the GUI as an error.
#[test]
fn every_field_is_accounted_for_on_the_wire() {
#[cfg(unix)]
diff --git a/thinkterm-proto/src/command.rs b/thinkterm-proto/src/command.rs
index 8da59223..ef5b2a1f 100644
--- a/thinkterm-proto/src/command.rs
+++ b/thinkterm-proto/src/command.rs
@@ -43,6 +43,12 @@ pub struct CommandSpec {
/// Whether the pty becomes the child's controlling terminal. False when
/// spawning across a flatpak boundary.
pub controlling_tty: bool,
+
+ /// Whether `cwd` is a requirement (the spawn fails if it cannot be
+ /// opened) or a preference (degrades to the home directory). Carried so
+ /// a server refuses out loud instead of silently substituting `$HOME`
+ /// for a directory the sender named on purpose.
+ pub require_cwd: bool,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
@@ -76,6 +82,7 @@ impl std::fmt::Debug for CommandSpec {
.field("cwd", &self.cwd.as_deref().map(String::from_utf8_lossy))
.field("umask", &self.umask)
.field("controlling_tty", &self.controlling_tty)
+ .field("require_cwd", &self.require_cwd)
.finish()
}
}
diff --git a/wezterm-gui/src/termwindow/ui/folder_problem.rs b/wezterm-gui/src/termwindow/ui/folder_problem.rs
index c76f7ee6..516e29bb 100644
--- a/wezterm-gui/src/termwindow/ui/folder_problem.rs
+++ b/wezterm-gui/src/termwindow/ui/folder_problem.rs
@@ -128,12 +128,19 @@ impl ProjectRootUnavailable {
if let Some(failure) = err.downcast_ref::<Self>() {
return Some(failure.clone());
}
- err.downcast_ref::<mux::domain::RequiredCwdUnavailable>()
- .map(|refused| Self {
- path: refused.dir.clone(),
- problem: FolderProblem::from_read_dir_kind(refused.kind),
- detail: refused.detail.clone(),
- })
+ let from_refusal = |refused: mux::domain::RequiredCwdUnavailable| Self {
+ path: refused.dir,
+ problem: FolderProblem::from_read_dir_kind(refused.kind),
+ detail: refused.detail,
+ };
+ if let Some(refused) = err.downcast_ref::<mux::domain::RequiredCwdUnavailable>() {
+ return Some(from_refusal(refused.clone()));
+ }
+ // A mux server (the local session host included) reports its refusal
+ // as one string; the type is recovered from the message.
+ err.chain()
+ .find_map(|layer| mux::domain::RequiredCwdUnavailable::from_message(&layer.to_string()))
+ .map(from_refusal)
}
}
@@ -182,6 +189,23 @@ mod test {
}
}
+ /// The local session host spawns in another process and answers with a
+ /// string, which has to open the same page as an in-process refusal.
+ #[test]
+ fn a_refusal_relayed_by_a_mux_server_is_recovered() {
+ let refused = mux::domain::RequiredCwdUnavailable {
+ dir: std::path::PathBuf::from("/Users/x/Documents/proj"),
+ kind: ErrorKind::PermissionDenied,
+ detail: "Operation not permitted (os error 1)".to_string(),
+ };
+ let relayed = anyhow::anyhow!("Error: {refused}").context("spawn thread tab");
+ let failure = ProjectRootUnavailable::from_error_chain(&relayed).expect("recovered");
+ assert_eq!(failure.path, refused.dir);
+ assert_eq!(failure.problem, FolderProblem::UnreadableRoot);
+ assert_eq!(failure.detail, refused.detail);
+ assert!(ProjectRootUnavailable::from_error_chain(&anyhow::anyhow!("other")).is_none());
+ }
+
/// The picker button is macOS-only and permission-only: a missing folder
/// is not handed back by selecting it, even there.
#[test]