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.

feat(proto): carry each pane's foreground program in codec 74

RoversXba105ea73c02
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index 6639d072..c4f5426e 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -474,7 +474,7 @@ macro_rules! pdu {
metrics::histogram!("pdu.size.rate", "pdu" => stringify!($name)).record(decoded.data.len() as f64);
Ok(DecodedPdu {
serial: decoded.serial,
- pdu: Pdu::$name(deserialize(decoded.data.as_slice(), decoded.is_compressed)?).sanitize_agent_status()
+ pdu: Pdu::$name(deserialize(decoded.data.as_slice(), decoded.is_compressed)?).sanitize_pane_metadata()
})
}
,)*
@@ -507,7 +507,7 @@ macro_rules! pdu {
metrics::histogram!("pdu.size", "pdu" => stringify!($name)).record(decoded.data.len() as f64);
#[cfg(not(target_family = "wasm"))]
let deserialize_started = std::time::Instant::now();
- let pdu = Pdu::$name(deserialize(decoded.data.as_slice(), decoded.is_compressed)?).sanitize_agent_status();
+ let pdu = Pdu::$name(deserialize(decoded.data.as_slice(), decoded.is_compressed)?).sanitize_pane_metadata();
#[cfg(not(target_family = "wasm"))]
if decoded.data.len() > 64 * 1024 {
log::debug!(
@@ -580,7 +580,10 @@ macro_rules! pdu {
/// 73: The plugin channel: a client reaches the plugin host on the server's
/// machine through its mux connection (PluginFrame), which the server
/// carries unread.
-pub const CODEC_VERSION: usize = 73;
+/// 74: The mux that owns a pane reports the program leading its terminal
+/// (ForegroundProgramChanged), with a request/response pair for
+/// cold-start delivery, so a client can show what a remote pane runs.
+pub const CODEC_VERSION: usize = 74;
// Defines the Pdu enum.
// Each struct has an explicit identifying number.
@@ -675,6 +678,9 @@ pdu! {
DefaultPalette: 94,
MoveTab: 95,
PluginFrame: 96,
+ ForegroundProgramChanged: 100,
+ GetForegroundPrograms: 101,
+ GetForegroundProgramsResponse: 102,
}
impl Pdu {
@@ -788,6 +794,7 @@ impl Pdu {
| Pdu::SetClipboard(SetClipboard { pane_id, .. })
| Pdu::PaneFocused(PaneFocused { pane_id })
| Pdu::AgentStatusChanged(AgentStatusChanged { pane_id, .. })
+ | Pdu::ForegroundProgramChanged(ForegroundProgramChanged { pane_id, .. })
| Pdu::PaneRemoved(PaneRemoved { pane_id }) => Some(*pane_id),
_ => None,
}
@@ -1271,7 +1278,7 @@ impl Pdu {
/// Discard invalid metadata after consuming its complete frame, so a
/// malformed identity cannot terminate unrelated terminal traffic. Actual
/// framing/deserialization errors and the decoder allocation limit remain fatal.
- fn sanitize_agent_status(mut self) -> Self {
+ fn sanitize_pane_metadata(mut self) -> Self {
match &mut self {
Self::AgentStatusChanged(update) => {
if update
@@ -1287,6 +1294,20 @@ impl Pdu {
.statuses
.retain(|entry| entry.status.within_budget());
}
+ Self::ForegroundProgramChanged(update) => {
+ if update
+ .program
+ .as_ref()
+ .is_some_and(|program| !program.within_budget())
+ {
+ update.program = None;
+ }
+ }
+ Self::GetForegroundProgramsResponse(response) => {
+ response
+ .programs
+ .retain(|entry| entry.program.within_budget());
+ }
_ => {}
}
self
@@ -1327,6 +1348,31 @@ pub struct GetAgentStatusesResponse {
pub statuses: Vec<AgentStatusEntry>,
}
+/// Unilateral: the program leading `pane_id`'s terminal changed, as seen by
+/// the mux that owns the pane. `None` means it could not be told.
+#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
+pub struct ForegroundProgramChanged {
+ pub pane_id: PaneId,
+ pub program: Option<thinkterm_proto::ForegroundProgram>,
+}
+
+/// Ask a mux for the foreground program of every pane it knows about, so a
+/// client that attaches late starts from the truth rather than from the
+/// next change.
+#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
+pub struct GetForegroundPrograms {}
+
+#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
+pub struct ForegroundProgramEntry {
+ pub pane_id: PaneId,
+ pub program: thinkterm_proto::ForegroundProgram,
+}
+
+#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
+pub struct GetForegroundProgramsResponse {
+ pub programs: Vec<ForegroundProgramEntry>,
+}
+
/// Select an existing authoritative Thread, or create the canonical
/// Default/Home/main landing when the server has no usable Thread, and make
/// sure that the selected Thread has a live terminal. The server validates the
@@ -2581,7 +2627,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, 73);
+ assert_eq!(CODEC_VERSION, 74);
use thinkterm_proto::{AgentEvidence, AgentState, AgentStatus};
fn round_trip(pdu: Pdu) {
@@ -2619,6 +2665,87 @@ mod test {
}));
}
+ #[test]
+ fn foreground_program_protocol_round_trip_at_version_74() {
+ // Same tripwire as the agent-status test above.
+ assert_eq!(CODEC_VERSION, 74);
+ use thinkterm_proto::ForegroundProgram;
+
+ fn round_trip(pdu: &Pdu) -> Pdu {
+ let mut encoded = Vec::new();
+ pdu.encode(&mut encoded, 0x74).unwrap();
+ let decoded = Pdu::decode(encoded.as_slice()).unwrap();
+ assert_eq!(decoded.serial, 0x74);
+ decoded.pdu
+ }
+
+ let program = ForegroundProgram {
+ executable: "node".to_string(),
+ runs: Some("npm".to_string()),
+ };
+ for pdu in [
+ Pdu::ForegroundProgramChanged(ForegroundProgramChanged {
+ pane_id: 7,
+ program: Some(program.clone()),
+ }),
+ Pdu::ForegroundProgramChanged(ForegroundProgramChanged {
+ pane_id: 7,
+ program: None,
+ }),
+ Pdu::GetForegroundPrograms(GetForegroundPrograms {}),
+ Pdu::GetForegroundProgramsResponse(GetForegroundProgramsResponse {
+ programs: vec![ForegroundProgramEntry {
+ pane_id: 7,
+ program: program.clone(),
+ }],
+ }),
+ ] {
+ assert_eq!(round_trip(&pdu), pdu);
+ }
+
+ // A name that is a path, not a file name, is dropped on arrival
+ // rather than handed to whoever draws it.
+ let smuggled = ForegroundProgram {
+ executable: "node".to_string(),
+ runs: Some("../../etc/passwd".to_string()),
+ };
+ assert_eq!(
+ round_trip(&Pdu::ForegroundProgramChanged(ForegroundProgramChanged {
+ pane_id: 7,
+ program: Some(smuggled.clone()),
+ })),
+ Pdu::ForegroundProgramChanged(ForegroundProgramChanged {
+ pane_id: 7,
+ program: None,
+ })
+ );
+ assert_eq!(
+ round_trip(&Pdu::GetForegroundProgramsResponse(
+ GetForegroundProgramsResponse {
+ programs: vec![
+ ForegroundProgramEntry {
+ pane_id: 7,
+ program: smuggled,
+ },
+ ForegroundProgramEntry {
+ pane_id: 8,
+ program
+ },
+ ],
+ }
+ )),
+ Pdu::GetForegroundProgramsResponse(GetForegroundProgramsResponse {
+ programs: vec![ForegroundProgramEntry {
+ pane_id: 8,
+ program: ForegroundProgram {
+ executable: "node".to_string(),
+ runs: Some("npm".to_string()),
+ },
+ }],
+ })
+ );
+ }
+
#[test]
fn plugin_frames_round_trip_as_bytes() {
for data in [vec![], b"{\"call\":{}}".to_vec(), vec![0u8; 70_000]] {
diff --git a/thinkterm-proto/src/lib.rs b/thinkterm-proto/src/lib.rs
index b46dad96..d0d0388c 100644
--- a/thinkterm-proto/src/lib.rs
+++ b/thinkterm-proto/src/lib.rs
@@ -18,6 +18,7 @@ pub mod command;
pub mod keyassignment;
pub mod layout;
pub mod pane;
+pub mod program;
pub mod renderable;
pub mod split;
@@ -27,6 +28,7 @@ pub use command::{CommandSpec, EnvVar};
pub use keyassignment::{PaneDirection, ScrollbackEraseMode, SpawnTabDomain};
pub use layout::{PaneEntry, PaneNode, PaneStackEntry, SerdeUrl};
pub use pane::{Pattern, SearchResult};
+pub use program::ForegroundProgram;
pub use renderable::{RenderableDimensions, StableCursorPosition};
pub use split::{SplitDirection, SplitDirectionAndSize, SplitRequest, SplitSize};
diff --git a/thinkterm-proto/src/program.rs b/thinkterm-proto/src/program.rs
new file mode 100644
index 00000000..132e03ab
--- /dev/null
+++ b/thinkterm-proto/src/program.rs
@@ -0,0 +1,58 @@
+//! Foreground program wire types.
+//!
+//! The mux that owns a pane reports which program leads its terminal; a
+//! client decides for itself what that means for presentation (the icon on
+//! the pane's tab, say). Only the facts travel.
+//!
+//! Varbincode is positional: **field order is the wire contract**. Bump the
+//! codec version when changing anything here.
+
+use serde::{Deserialize, Serialize};
+
+/// Longest name either field may carry: a file name, which no platform
+/// lets exceed 255 bytes.
+pub const MAX_PROGRAM_NAME_BYTES: usize = 255;
+
+#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
+pub struct ForegroundProgram {
+ /// File name of the executable leading the terminal's foreground
+ /// process group: `vim`, `node`, `python3.12`, `zsh`.
+ pub executable: String,
+ /// What that executable runs, when it is an interpreter, a shell or a
+ /// launcher: the script's file name (`npm` for `node …/bin/npm`,
+ /// `deploy.sh`), the module (`pytest` for `python -m pytest`) or the
+ /// command (`vim` for `sudo vim`). `None` when it runs nothing of the
+ /// kind, as an interactive shell does.
+ pub runs: Option<String>,
+}
+
+impl ForegroundProgram {
+ /// Both names are plain, non-empty file names within the budget.
+ pub fn within_budget(&self) -> bool {
+ is_program_name(&self.executable) && self.runs.as_deref().is_none_or(is_program_name)
+ }
+}
+
+fn is_program_name(name: &str) -> bool {
+ !name.is_empty() && name.len() <= MAX_PROGRAM_NAME_BYTES && !name.contains(['/', '\\', '\0'])
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn budget_rejects_paths_and_oversized_names() {
+ let program = |executable: &str, runs: Option<&str>| ForegroundProgram {
+ executable: executable.to_string(),
+ runs: runs.map(str::to_string),
+ };
+ assert!(program("node", Some("npm")).within_budget());
+ assert!(program("zsh", None).within_budget());
+ assert!(!program("", None).within_budget());
+ assert!(!program("node", Some("")).within_budget());
+ assert!(!program("/usr/bin/node", None).within_budget());
+ assert!(!program("node", Some("bin/npm")).within_budget());
+ assert!(!program(&"x".repeat(MAX_PROGRAM_NAME_BYTES + 1), None).within_budget());
+ }
+}