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(plugins): run plugins in a process of their own

RoversXd9a5c5a4e317
diff --git a/Cargo.lock b/Cargo.lock
index f77be927..0236b5cb 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8576,6 +8576,32 @@ dependencies = [
"wgpu",
]
+[[package]]
+name = "thinkterm-plugin-channel"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "dirs-next",
+ "log",
+ "serde",
+ "serde_json",
+ "tempfile",
+ "wezterm-uds",
+]
+
+[[package]]
+name = "thinkterm-plugin-server"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "log",
+ "serde_json",
+ "tempfile",
+ "thinkterm-plugin-channel",
+ "thinkterm-snippets",
+ "wezterm-uds",
+]
+
[[package]]
name = "thinkterm-proto"
version = "0.1.0"
@@ -8741,9 +8767,11 @@ dependencies = [
"thinkterm-font-core",
"thinkterm-font-web",
"thinkterm-i18n",
+ "thinkterm-plugin-channel",
"thinkterm-proto",
"thinkterm-render",
"thinkterm-session",
+ "thinkterm-snippets",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
@@ -10532,6 +10560,7 @@ dependencies = [
"thinkterm-core",
"thinkterm-file-index",
"thinkterm-i18n",
+ "thinkterm-plugin-channel",
"thinkterm-proto",
"thinkterm-render",
"thinkterm-snippets",
@@ -10665,6 +10694,7 @@ dependencies = [
"soketto",
"tempfile",
"termwiz 0.24.0",
+ "thinkterm-plugin-channel",
"thinkterm-proto",
"url",
"uuid",
diff --git a/Cargo.toml b/Cargo.toml
index 4db367f1..12ae119e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,6 +14,8 @@ members = [
"thinkterm-proto",
"thinkterm-render",
"thinkterm-mobile",
+ "thinkterm-plugin-channel",
+ "thinkterm-plugin-server",
"thinkterm-session",
"thinkterm-snippets",
"thinkterm-web",
@@ -179,10 +181,11 @@ thinkterm-file-index = { path = "thinkterm-file-index" }
thinkterm-font-core = { path = "thinkterm-font-core" }
thinkterm-font-web = { path = "thinkterm-font-web" }
thinkterm-i18n = { path = "thinkterm-i18n" }
+thinkterm-plugin-channel = { path = "thinkterm-plugin-channel", default-features = false }
thinkterm-proto = { path = "thinkterm-proto" }
thinkterm-render = { path = "thinkterm-render" }
thinkterm-session = { path = "thinkterm-session" }
-thinkterm-snippets = { path = "thinkterm-snippets" }
+thinkterm-snippets = { path = "thinkterm-snippets", default-features = false }
thinkterm-web = { path = "thinkterm-web" }
thinkterm-update = { path = "thinkterm-update" }
core-foundation = "=0.10.0"
diff --git a/thinkterm-plugin-channel/Cargo.toml b/thinkterm-plugin-channel/Cargo.toml
new file mode 100644
index 00000000..16771c46
--- /dev/null
+++ b/thinkterm-plugin-channel/Cargo.toml
@@ -0,0 +1,22 @@
+[package]
+name = "thinkterm-plugin-channel"
+version = "0.1.0"
+edition = "2021"
+publish = false
+
+[features]
+default = ["native"]
+# Finding, starting and talking to the plugin host. Without it the crate is
+# the message format alone, which a browser speaks through the mux.
+native = ["dep:anyhow", "dep:dirs-next", "dep:log", "dep:wezterm-uds"]
+
+[dependencies]
+anyhow = { workspace = true, optional = true }
+dirs-next = { workspace = true, optional = true }
+log = { workspace = true, optional = true }
+serde = { workspace = true, features = ["derive", "std"] }
+serde_json.workspace = true
+wezterm-uds = { workspace = true, optional = true }
+
+[dev-dependencies]
+tempfile.workspace = true
diff --git a/thinkterm-plugin-channel/src/client.rs b/thinkterm-plugin-channel/src/client.rs
new file mode 100644
index 00000000..0292b0f3
--- /dev/null
+++ b/thinkterm-plugin-channel/src/client.rs
@@ -0,0 +1,544 @@
+//! Reaching the host: connecting to it, starting it when nothing answers,
+//! and replacing one that speaks an older protocol.
+
+use crate::wire::{read_frame, write_frame, FromHost, ToHost, PROTOCOL};
+use anyhow::{anyhow, bail, Context};
+use serde_json::Value;
+use std::collections::HashMap;
+use std::fs::{self, File, OpenOptions};
+use std::io::{self, ErrorKind};
+use std::net::Shutdown;
+use std::path::{Path, PathBuf};
+use std::process::{Command, Stdio};
+use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
+use std::time::{Duration, Instant};
+use wezterm_uds::UnixStream;
+
+/// How long a start may take before the client gives up on it. Generous:
+/// the first run of a freshly installed program can wait on the system's
+/// checks of it.
+const START_TIMEOUT: Duration = Duration::from_secs(10);
+/// Between attempts to reach a host that is starting or quitting.
+const RETRY: Duration = Duration::from_millis(20);
+/// A host that accepted a connection says hello at once; one that does not
+/// within this is not a host to use.
+const HELLO_TIMEOUT: Duration = Duration::from_secs(5);
+/// The log is the host's standard error, and it says little; one that grew
+/// past this anyway starts again empty.
+const LOG_LIMIT: u64 = 1024 * 1024;
+
+/// Everything a client needs to find the host, or start it.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Host {
+ pub socket: PathBuf,
+ pub lock: PathBuf,
+ pub data_dir: PathBuf,
+ pub log: PathBuf,
+ pub program: PathBuf,
+}
+
+enum Reached {
+ Ready(Connection),
+ /// A host from an older build: it is asked to quit.
+ Older(Connection),
+ Newer(u32),
+}
+
+impl Host {
+ /// The host this build ships, where every client on this machine looks
+ /// for it.
+ pub fn for_this_build() -> io::Result<Self> {
+ Ok(Self {
+ socket: crate::paths::socket(),
+ lock: crate::paths::lock(),
+ data_dir: crate::paths::data_dir(),
+ log: crate::paths::log(),
+ program: crate::paths::host_program()?,
+ })
+ }
+
+ /// A connection to the host, which is started first when nothing
+ /// answers. Blocking for as long as a start takes: never call it on a
+ /// thread that paints.
+ pub fn connect(&self) -> anyhow::Result<Connection> {
+ let deadline = Instant::now() + START_TIMEOUT;
+ let mut started = false;
+ loop {
+ match self.reach() {
+ Ok(Reached::Ready(connection)) => return Ok(connection),
+ Ok(Reached::Older(mut connection)) => {
+ // It removes its socket before it exits; until then the
+ // next attempt finds it still there and asks again.
+ log::info!("replacing a plugin host from an older build");
+ let _ = connection.send(&ToHost::Quit);
+ }
+ Ok(Reached::Newer(protocol)) => bail!(
+ "a newer ThinkTerm's plugin host is running (protocol {protocol}, \
+ this build speaks {PROTOCOL})"
+ ),
+ Err(err) if nobody_listens(&err) => {
+ if !started {
+ self.start()?;
+ started = true;
+ }
+ }
+ // Accepted and closed: a host on its way out.
+ Err(err) if is_transient(&err) => {}
+ Err(err) => {
+ return Err(err)
+ .with_context(|| format!("connect to {}", self.socket.display()))
+ }
+ }
+ if Instant::now() >= deadline {
+ bail!(
+ "the plugin host did not answer at {} within {START_TIMEOUT:?}",
+ self.socket.display()
+ );
+ }
+ std::thread::sleep(RETRY);
+ }
+ }
+
+ fn reach(&self) -> io::Result<Reached> {
+ let mut stream = UnixStream::connect(&self.socket)?;
+ stream.set_read_timeout(Some(HELLO_TIMEOUT))?;
+ let hello = read_frame(&mut stream)?;
+ stream.set_read_timeout(None)?;
+ let connection = Connection { stream };
+ match FromHost::decode(&hello) {
+ Ok(FromHost::Hello { protocol }) if protocol == PROTOCOL => {
+ Ok(Reached::Ready(connection))
+ }
+ Ok(FromHost::Hello { protocol }) if protocol < PROTOCOL => {
+ Ok(Reached::Older(connection))
+ }
+ Ok(FromHost::Hello { protocol }) => Ok(Reached::Newer(protocol)),
+ _ => Err(io::Error::new(
+ ErrorKind::InvalidData,
+ "the plugin host did not say hello",
+ )),
+ }
+ }
+
+ /// Starts the host in the background. It holds the lock while it runs,
+ /// so a start that races another's gives way to it.
+ fn start(&self) -> anyhow::Result<()> {
+ if let Some(dir) = self.socket.parent() {
+ create_private_dir(dir)?;
+ }
+ let log = open_log(&self.log)?;
+ let mut command = Command::new(&self.program);
+ command
+ .arg("--socket")
+ .arg(&self.socket)
+ .arg("--lock")
+ .arg(&self.lock)
+ .arg("--data-dir")
+ .arg(&self.data_dir)
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(log);
+ // Its own process group: a Ctrl-C meant for the terminal that
+ // started a client is not meant for the host.
+ #[cfg(unix)]
+ {
+ use std::os::unix::process::CommandExt;
+ command.process_group(0);
+ }
+ #[cfg(windows)]
+ {
+ use std::os::windows::process::CommandExt;
+ const CREATE_NO_WINDOW: u32 = 0x0800_0000;
+ command.creation_flags(CREATE_NO_WINDOW);
+ }
+ let mut child = command
+ .spawn()
+ .map_err(|err| anyhow!("start {}: {err}", self.program.display()))?;
+ log::info!("started the plugin host, pid {}", child.id());
+ // Reaped when it exits, long after this returns. If this process
+ // exits first, the system reaps it instead.
+ let waiter = std::thread::Builder::new()
+ .name("plugin-host-wait".into())
+ .stack_size(64 * 1024)
+ .spawn(move || {
+ let _ = child.wait();
+ });
+ if let Err(err) = waiter {
+ log::warn!("cannot wait for the plugin host: {err}");
+ }
+ Ok(())
+ }
+}
+
+/// Nothing is bound at the socket: no file, or a file its host left behind.
+fn nobody_listens(err: &io::Error) -> bool {
+ matches!(
+ err.kind(),
+ ErrorKind::NotFound | ErrorKind::ConnectionRefused
+ )
+}
+
+fn is_transient(err: &io::Error) -> bool {
+ matches!(
+ err.kind(),
+ ErrorKind::UnexpectedEof
+ | ErrorKind::ConnectionReset
+ | ErrorKind::ConnectionAborted
+ | ErrorKind::BrokenPipe
+ )
+}
+
+fn create_private_dir(dir: &Path) -> io::Result<()> {
+ let mut builder = fs::DirBuilder::new();
+ builder.recursive(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::DirBuilderExt;
+ builder.mode(0o700);
+ }
+ builder.create(dir)
+}
+
+fn open_log(path: &Path) -> io::Result<File> {
+ let long = fs::metadata(path).is_ok_and(|meta| meta.len() > LOG_LIMIT);
+ OpenOptions::new()
+ .create(true)
+ .append(!long)
+ .write(true)
+ .truncate(long)
+ .open(path)
+}
+
+/// One connection to the host, past its hello.
+#[derive(Debug)]
+pub struct Connection {
+ stream: UnixStream,
+}
+
+impl Connection {
+ pub fn send(&mut self, message: &ToHost) -> io::Result<()> {
+ write_frame(&mut self.stream, &message.encode())
+ }
+
+ pub fn recv(&mut self) -> io::Result<FromHost> {
+ let frame = read_frame(&mut self.stream)?;
+ FromHost::decode(&frame).map_err(|err| io::Error::new(ErrorKind::InvalidData, err))
+ }
+
+ /// A second handle on the connection, so one thread can wait for what
+ /// the host sends while another sends to it.
+ pub fn try_clone(&self) -> io::Result<Self> {
+ Ok(Self {
+ stream: self.stream.try_clone()?,
+ })
+ }
+
+ /// Ends the connection for every handle on it: a `recv` waiting on
+ /// another thread returns.
+ pub fn shutdown(&self) {
+ let _ = self.stream.shutdown(Shutdown::Both);
+ }
+
+ /// The socket, for a caller that carries frames without reading them.
+ pub fn into_stream(self) -> UnixStream {
+ self.stream
+ }
+}
+
+/// A call's answer, or why there is none.
+pub type Answer = Result<Value, String>;
+
+type Reply = Box<dyn FnOnce(Answer) + Send>;
+
+/// The pause before trying a host that could not be reached or went away,
+/// doubling up to the second while it stays away.
+const SESSION_RETRY_FIRST: Duration = Duration::from_millis(250);
+const SESSION_RETRY_LAST: Duration = Duration::from_secs(30);
+/// How long a call waits for its answer before it is told there is none:
+/// a host that hangs must not leave a save waiting for good.
+pub const CALL_TIMEOUT: Duration = Duration::from_secs(10);
+
+/// A lasting connection to the host, for a client that stays on a plugin --
+/// a panel on show -- rather than asking it once. It runs on a thread of its
+/// own: the host is started when it is not running, a lost connection is
+/// made again with a pause that grows while the host keeps going away, and
+/// every call is answered, with an error when the host cannot answer it.
+/// Dropping the session ends it.
+pub struct Session {
+ jobs: Sender<Job>,
+}
+
+enum Job {
+ Call {
+ plugin: String,
+ body: Value,
+ reply: Reply,
+ },
+ /// What the host sent over connection number `.0`.
+ Heard(u64, io::Result<FromHost>),
+ Stop,
+}
+
+/// What a session tells its owner, on the session's thread.
+#[derive(Debug)]
+pub enum Notice {
+ /// Connected, for the first time or again: whatever the owner follows
+ /// is to be asked for anew.
+ Connected,
+ /// The host cannot be reached, and why. The session keeps trying.
+ Trouble(String),
+ /// Something a plugin told the clients watching it.
+ Event { plugin: String, body: Value },
+}
+
+impl Session {
+ pub fn start(host: Host, notice: impl FnMut(Notice) + Send + 'static) -> io::Result<Self> {
+ Self::start_with(host, CALL_TIMEOUT, notice)
+ }
+
+ /// A session whose calls wait `call_timeout` for their answers.
+ pub fn start_with(
+ host: Host,
+ call_timeout: Duration,
+ notice: impl FnMut(Notice) + Send + 'static,
+ ) -> io::Result<Self> {
+ let (jobs, queue) = mpsc::channel();
+ let readers = jobs.clone();
+ std::thread::Builder::new()
+ .name("plugin-session".into())
+ .spawn(move || serve_session(host, call_timeout, queue, readers, notice))?;
+ Ok(Self { jobs })
+ }
+
+ /// Asks `plugin`. `reply` is called with the answer on the session's
+ /// thread, or with why there is none.
+ pub fn call(&self, plugin: &str, body: Value, reply: impl FnOnce(Answer) + Send + 'static) {
+ let job = Job::Call {
+ plugin: plugin.to_string(),
+ body,
+ reply: Box::new(reply),
+ };
+ if let Err(mpsc::SendError(Job::Call { reply, .. })) = self.jobs.send(job) {
+ reply(Err("the plugin session has ended".into()));
+ }
+ }
+}
+
+impl Drop for Session {
+ fn drop(&mut self) {
+ let _ = self.jobs.send(Job::Stop);
+ }
+}
+
+/// The calls sent and not yet answered: when each gives up, and whom to
+/// tell.
+type Waiting = HashMap<u64, (Instant, Reply)>;
+
+fn serve_session(
+ host: Host,
+ call_timeout: Duration,
+ queue: Receiver<Job>,
+ readers: Sender<Job>,
+ mut notice: impl FnMut(Notice),
+) {
+ let mut next_id = 0u64;
+ let mut generation = 0u64;
+ let mut retry = SESSION_RETRY_FIRST;
+ // Calls made while there was no connection, sent on the next one.
+ let mut queued: Vec<(String, Value, Reply)> = Vec::new();
+ loop {
+ let connected = host.connect().and_then(|connection| {
+ let reader = connection.try_clone()?;
+ Ok((connection, reader))
+ });
+ let (mut connection, mut reader) = match connected {
+ Ok(handles) => handles,
+ Err(err) => {
+ let why = format!("{err:#}");
+ for (_, _, reply) in queued.drain(..) {
+ reply(Err(why.clone()));
+ }
+ notice(Notice::Trouble(why));
+ if !pause(&queue, retry, &mut queued) {
+ return;
+ }
+ retry = (retry * 2).min(SESSION_RETRY_LAST);
+ continue;
+ }
+ };
+ generation += 1;
+ let heard = readers.clone();
+ let this = generation;
+ let listening = std::thread::Builder::new()
+ .name("plugin-session-read".into())
+ .spawn(move || loop {
+ let message = reader.recv();
+ let ended = message.is_err();
+ if heard.send(Job::Heard(this, message)).is_err() || ended {
+ return;
+ }
+ });
+ if let Err(err) = listening {
+ log::warn!("cannot read from the plugin host: {err}");
+ connection.shutdown();
+ if !pause(&queue, retry, &mut queued) {
+ return;
+ }
+ continue;
+ }
+ notice(Notice::Connected);
+
+ let mut waiting = Waiting::new();
+ let mut alive = true;
+ for (plugin, body, reply) in queued.drain(..) {
+ if alive {
+ let deadline = Instant::now() + call_timeout;
+ alive = send_call(
+ &mut connection,
+ &mut next_id,
+ &mut waiting,
+ (plugin, body, reply),
+ deadline,
+ );
+ } else {
+ reply(Err("the plugin host went away".into()));
+ }
+ }
+ while alive {
+ let job = match waiting.values().map(|(deadline, _)| *deadline).min() {
+ Some(deadline) => {
+ match queue.recv_timeout(deadline.saturating_duration_since(Instant::now())) {
+ Ok(job) => Ok(job),
+ Err(RecvTimeoutError::Timeout) => {
+ give_up_overdue(&mut waiting, call_timeout);
+ continue;
+ }
+ Err(RecvTimeoutError::Disconnected) => Err(()),
+ }
+ }
+ None => queue.recv().map_err(drop),
+ };
+ match job {
+ Ok(Job::Call {
+ plugin,
+ body,
+ reply,
+ }) => {
+ let deadline = Instant::now() + call_timeout;
+ alive = send_call(
+ &mut connection,
+ &mut next_id,
+ &mut waiting,
+ (plugin, body, reply),
+ deadline,
+ )
+ }
+ Ok(Job::Heard(from, _)) if from != generation => {}
+ Ok(Job::Heard(_, Ok(FromHost::Ok { id, body }))) => {
+ retry = SESSION_RETRY_FIRST;
+ if let Some((_, reply)) = waiting.remove(&id) {
+ reply(Ok(body));
+ }
+ }
+ Ok(Job::Heard(_, Ok(FromHost::Error { id, message }))) => {
+ if let Some((_, reply)) = waiting.remove(&id) {
+ reply(Err(message));
+ }
+ }
+ Ok(Job::Heard(_, Ok(FromHost::Event { plugin, body }))) => {
+ notice(Notice::Event { plugin, body })
+ }
+ Ok(Job::Heard(_, Ok(FromHost::Hello { .. }))) => {}
+ Ok(Job::Heard(_, Err(_))) => alive = false,
+ Ok(Job::Stop) | Err(()) => {
+ connection.shutdown();
+ for (_, (_, reply)) in waiting.drain() {
+ reply(Err("the plugin session has ended".into()));
+ }
+ return;
+ }
+ }
+ }
+ connection.shutdown();
+ for (_, (_, reply)) in waiting.drain() {
+ reply(Err("the plugin host went away".into()));
+ }
+ // A host that answered reset this; one that keeps failing is asked
+ // less and less often.
+ if !pause(&queue, retry, &mut queued) {
+ return;
+ }
+ retry = (retry * 2).min(SESSION_RETRY_LAST);
+ }
+}
+
+/// Sends a call, to be answered through `reply` by `deadline`. False when
+/// the host has gone, which `reply` is told.
+fn send_call(
+ connection: &mut Connection,
+ next_id: &mut u64,
+ waiting: &mut Waiting,
+ (plugin, body, reply): (String, Value, Reply),
+ deadline: Instant,
+) -> bool {
+ *next_id += 1;
+ let call = ToHost::Call {
+ id: *next_id,
+ plugin,
+ body,
+ };
+ match connection.send(&call) {
+ Ok(()) => {
+ waiting.insert(*next_id, (deadline, reply));
+ true
+ }
+ Err(err) => {
+ reply(Err(format!("the plugin host went away: {err}")));
+ false
+ }
+ }
+}
+
+/// Tells the calls whose time is up that no answer is coming. One that
+/// arrives later finds nobody waiting and is dropped.
+fn give_up_overdue(waiting: &mut Waiting, call_timeout: Duration) {
+ let now = Instant::now();
+ let overdue: Vec<u64> = waiting
+ .iter()
+ .filter(|(_, (deadline, _))| *deadline <= now)
+ .map(|(id, _)| *id)
+ .collect();
+ for id in overdue {
+ if let Some((_, reply)) = waiting.remove(&id) {
+ reply(Err(format!(
+ "the plugin host did not answer within {call_timeout:?}"
+ )));
+ }
+ }
+}
+
+/// Waits `delay` before the next attempt, keeping the calls made meanwhile;
+/// a call cuts the wait short, since someone is waiting on it. False when
+/// the session was ended.
+fn pause(queue: &Receiver<Job>, delay: Duration, queued: &mut Vec<(String, Value, Reply)>) -> bool {
+ let until = Instant::now() + delay;
+ loop {
+ let left = until.saturating_duration_since(Instant::now());
+ if left.is_zero() {
+ return true;
+ }
+ match queue.recv_timeout(left) {
+ Ok(Job::Call {
+ plugin,
+ body,
+ reply,
+ }) => {
+ queued.push((plugin, body, reply));
+ return true;
+ }
+ Ok(Job::Heard(..)) => {}
+ Ok(Job::Stop) | Err(RecvTimeoutError::Disconnected) => return false,
+ Err(RecvTimeoutError::Timeout) => return true,
+ }
+ }
+}
diff --git a/thinkterm-plugin-channel/src/lib.rs b/thinkterm-plugin-channel/src/lib.rs
new file mode 100644
index 00000000..a34dc68c
--- /dev/null
+++ b/thinkterm-plugin-channel/src/lib.rs
@@ -0,0 +1,22 @@
+//! The plugin channel: how ThinkTerm's clients reach the plugin host.
+//!
+//! The mux runs terminals and nothing else. What a client shows beside them
+//! -- the snippets today -- is kept by the plugin host, a process of its
+//! own, started when something first asks for it and gone again once
+//! nothing has been connected to it for a while. A slow or crashed plugin
+//! is then a slow or crashed plugin, never a stalled terminal, and a
+//! machine where nobody opens a plugin never runs one.
+//!
+//! A client on the host's machine talks to it over a unix socket. A
+//! browser can only reach the mux, so the mux carries its frames there and
+//! back without looking inside them.
+//!
+//! A frame is a little-endian `u32` length and that many bytes of JSON: a
+//! [`wire::ToHost`] one way, a [`wire::FromHost`] the other. What a call's
+//! body means is up to the plugin it names.
+
+#[cfg(feature = "native")]
+pub mod client;
+#[cfg(feature = "native")]
+pub mod paths;
+pub mod wire;
diff --git a/thinkterm-plugin-channel/src/paths.rs b/thinkterm-plugin-channel/src/paths.rs
new file mode 100644
index 00000000..1e773d5a
--- /dev/null
+++ b/thinkterm-plugin-channel/src/paths.rs
@@ -0,0 +1,79 @@
+//! Where the plugin host lives on this machine. Every client works these
+//! out the same way, so they all find the one host.
+
+use std::path::PathBuf;
+
+/// The directory the mux keeps its own socket in (config's `RUNTIME_DIR`),
+/// worked out the same way without loading config: the host is a small
+/// program, and this is all it would want from it.
+pub fn runtime_dir() -> PathBuf {
+ match dirs_next::runtime_dir() {
+ Some(dir) => dir.join("thinkterm"),
+ None => home_dir().join(".local/share/thinkterm"),
+ }
+}
+
+/// The GUI's data directory (thinkterm-core's `frontend_data_dir`), where
+/// the plugins keep what they own, `snippets.json` among it.
+pub fn data_dir() -> PathBuf {
+ dirs_next::data_dir()
+ .unwrap_or_else(|| home_dir().join(".local/share"))
+ .join("ThinkTerm")
+}
+
+fn home_dir() -> PathBuf {
+ dirs_next::home_dir().unwrap_or_default()
+}
+
+/// `base` in [`runtime_dir`], scoped to the build profile the way config's
+/// `runtime_file_name` scopes the mux's files: a debug build never reaches
+/// the release build's host.
+fn runtime_file(base: &str) -> PathBuf {
+ let name = if cfg!(debug_assertions) {
+ format!("{base}-debug")
+ } else {
+ base.to_string()
+ };
+ runtime_dir().join(name)
+}
+
+pub fn socket() -> PathBuf {
+ runtime_file("plugins-sock")
+}
+
+/// Held by the running host for as long as it runs: there is one at a time.
+pub fn lock() -> PathBuf {
+ runtime_file("plugins-lock")
+}
+
+/// What the host says while it runs; its standard error.
+pub fn log() -> PathBuf {
+ runtime_file("plugins-log")
+}
+
+/// The host this build ships, beside the running executable. Beside the
+/// file itself, not a link to it: the command-line tools are linked into a
+/// bin directory from the app bundle, and the host is not.
+pub fn host_program() -> std::io::Result<PathBuf> {
+ let name = if cfg!(windows) {
+ "thinkterm-plugin-server.exe"
+ } else {
+ "thinkterm-plugin-server"
+ };
+ let exe = std::env::current_exe()?;
+ let exe = exe.canonicalize().unwrap_or(exe);
+ Ok(exe.with_file_name(name))
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn the_host_files_share_the_mux_runtime_dir() {
+ let dir = super::runtime_dir();
+ assert_eq!(dir.file_name().unwrap(), "thinkterm");
+ for path in [super::socket(), super::lock(), super::log()] {
+ assert_eq!(path.parent().unwrap(), dir);
+ }
+ assert_eq!(super::data_dir().file_name().unwrap(), "ThinkTerm");
+ }
+}
diff --git a/thinkterm-plugin-channel/src/wire.rs b/thinkterm-plugin-channel/src/wire.rs
new file mode 100644
index 00000000..41fa2d63
--- /dev/null
+++ b/thinkterm-plugin-channel/src/wire.rs
@@ -0,0 +1,159 @@
+//! The messages, and the frames that carry them.
+
+use serde::{Deserialize, Serialize};
+use serde_json::Value;
+use std::io::{self, Read, Write};
+
+/// The version of this format. A client that finds a host speaking an
+/// older one asks it to quit and starts its own; one that finds a newer
+/// host leaves it alone.
+pub const PROTOCOL: u32 = 1;
+
+/// The largest frame either side sends or accepts, so a confused peer
+/// cannot have the other allocate without bound. A thousand scripts of
+/// eight kilobytes each fit twice over.
+pub const MAX_FRAME: usize = 16 * 1024 * 1024;
+
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum ToHost {
+ /// Ask `plugin` something. Answered by an [`FromHost::Ok`] or an
+ /// [`FromHost::Error`] with the same `id`, which the client picks.
+ Call {
+ id: u64,
+ plugin: String,
+ body: Value,
+ },
+ /// Exit now: a newer build is replacing this host.
+ Quit,
+}
+
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum FromHost {
+ /// The first message on every connection.
+ Hello {
+ protocol: u32,
+ },
+ Ok {
+ id: u64,
+ body: Value,
+ },
+ Error {
+ id: u64,
+ message: String,
+ },
+ /// What a plugin tells every connection watching it.
+ Event {
+ plugin: String,
+ body: Value,
+ },
+}
+
+impl ToHost {
+ pub fn encode(&self) -> Vec<u8> {
+ serde_json::to_vec(self).expect("a message of strings and JSON values always serialises")
+ }
+
+ pub fn decode(frame: &[u8]) -> serde_json::Result<Self> {
+ serde_json::from_slice(frame)
+ }
+}
+
+impl FromHost {
+ pub fn encode(&self) -> Vec<u8> {
+ serde_json::to_vec(self).expect("a message of strings and JSON values always serialises")
+ }
+
+ pub fn decode(frame: &[u8]) -> serde_json::Result<Self> {
+ serde_json::from_slice(frame)
+ }
+}
+
+/// The length a frame header announces, if it is one this side accepts.
+pub fn frame_len(header: [u8; 4]) -> Option<usize> {
+ let len = u32::from_le_bytes(header) as usize;
+ (len <= MAX_FRAME).then_some(len)
+}
+
+/// The header for a frame of `len` bytes, if it is not too long to send.
+pub fn frame_header(len: usize) -> Option<[u8; 4]> {
+ (len <= MAX_FRAME).then(|| (len as u32).to_le_bytes())
+}
+
+fn too_long() -> io::Error {
+ io::Error::new(
+ io::ErrorKind::InvalidData,
+ format!("a plugin frame is limited to {MAX_FRAME} bytes"),
+ )
+}
+
+/// Writes `payload` as one frame, header and body in a single write so a
+/// frame is never split between two writers sharing a socket.
+pub fn write_frame(to: &mut impl Write, payload: &[u8]) -> io::Result<()> {
+ let header = frame_header(payload.len()).ok_or_else(too_long)?;
+ let mut frame = Vec::with_capacity(header.len() + payload.len());
+ frame.extend_from_slice(&header);
+ frame.extend_from_slice(payload);
+ to.write_all(&frame)?;
+ to.flush()
+}
+
+/// Reads one frame's body.
+pub fn read_frame(from: &mut impl Read) -> io::Result<Vec<u8>> {
+ let mut header = [0u8; 4];
+ from.read_exact(&mut header)?;
+ let len = frame_len(header).ok_or_else(too_long)?;
+ let mut payload = vec![0u8; len];
+ from.read_exact(&mut payload)?;
+ Ok(payload)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ #[test]
+ fn messages_are_tagged_json() {
+ let call = ToHost::Call {
+ id: 7,
+ plugin: "snippets".into(),
+ body: json!({"op": "watch"}),
+ };
+ assert_eq!(
+ serde_json::to_value(&call).unwrap(),
+ json!({"call": {"id": 7, "plugin": "snippets", "body": {"op": "watch"}}})
+ );
+ assert_eq!(ToHost::decode(&call.encode()).unwrap(), call);
+ assert_eq!(serde_json::to_value(ToHost::Quit).unwrap(), json!("quit"));
+ let hello = FromHost::Hello { protocol: PROTOCOL };
+ assert_eq!(
+ serde_json::to_value(&hello).unwrap(),
+ json!({"hello": {"protocol": PROTOCOL}})
+ );
+ assert_eq!(FromHost::decode(&hello.encode()).unwrap(), hello);
+ }
+
+ #[test]
+ fn frames_round_trip_and_refuse_what_is_too_long() {
+ let mut wire = Vec::new();
+ write_frame(&mut wire, b"one").unwrap();
+ write_frame(&mut wire, b"").unwrap();
+ let mut from = wire.as_slice();
+ assert_eq!(read_frame(&mut from).unwrap(), b"one");
+ assert_eq!(read_frame(&mut from).unwrap(), b"");
+ assert_eq!(
+ read_frame(&mut from).unwrap_err().kind(),
+ io::ErrorKind::UnexpectedEof
+ );
+
+ let huge = ((MAX_FRAME + 1) as u32).to_le_bytes();
+ assert_eq!(
+ read_frame(&mut huge.as_slice()).unwrap_err().kind(),
+ io::ErrorKind::InvalidData
+ );
+ assert!(frame_header(MAX_FRAME).is_some());
+ assert!(frame_header(MAX_FRAME + 1).is_none());
+ }
+}
diff --git a/thinkterm-plugin-server/Cargo.toml b/thinkterm-plugin-server/Cargo.toml
new file mode 100644
index 00000000..15419159
--- /dev/null
+++ b/thinkterm-plugin-server/Cargo.toml
@@ -0,0 +1,23 @@
+[package]
+name = "thinkterm-plugin-server"
+version = "0.1.0"
+edition = "2021"
+publish = false
+
+# The process ThinkTerm's plugins run in, apart from the mux and the GUI.
+# Kept small on purpose: it runs whenever a plugin is in use.
+
+[[bin]]
+name = "thinkterm-plugin-server"
+path = "src/main.rs"
+
+[dependencies]
+anyhow.workspace = true
+log.workspace = true
+serde_json.workspace = true
+thinkterm-plugin-channel = { workspace = true, features = ["native"] }
+thinkterm-snippets = { workspace = true, features = ["native"] }
+wezterm-uds.workspace = true
+
+[dev-dependencies]
+tempfile.workspace = true
diff --git a/thinkterm-plugin-server/src/host.rs b/thinkterm-plugin-server/src/host.rs
new file mode 100644
index 00000000..65bb3dd8
--- /dev/null
+++ b/thinkterm-plugin-server/src/host.rs
@@ -0,0 +1,276 @@
+//! The connected clients, and the plugins they call.
+//!
+//! Each client has a thread reading its calls and one writing what it is
+//! sent, from a queue of its own: a client that stops reading loses its
+//! connection instead of holding up everyone else's events.
+
+use serde_json::Value;
+use std::collections::{HashMap, HashSet};
+use std::fs;
+use std::net::Shutdown;
+use std::path::PathBuf;
+use std::sync::mpsc::{sync_channel, SyncSender, TrySendError};
+use std::sync::{Arc, Mutex, MutexGuard};
+use std::time::{Duration, Instant};
+use thinkterm_plugin_channel::wire::{read_frame, write_frame, FromHost, ToHost, PROTOCOL};
+use wezterm_uds::UnixStream;
+
+/// Frames that may wait for one client. A client this far behind is not
+/// reading, and is disconnected.
+const QUEUE: usize = 256;
+/// The threads here parse JSON and move bytes; a host with two clients has
+/// no use for megabytes of stack each.
+const STACK: usize = 256 * 1024;
+
+/// A plugin as the host runs it.
+pub trait Plugin: Send {
+ fn name(&self) -> &'static str;
+
+ /// Answers `body`, a call from a client. The plugin may make the caller
+ /// one of its watchers and leave events for all of them in `outcome`.
+ fn call(&mut self, body: Value, outcome: &mut Outcome) -> anyhow::Result<Value>;
+}
+
+#[derive(Default)]
+pub struct Outcome {
+ /// The caller is sent this plugin's events from now on.
+ pub watch: bool,
+ /// Sent to every watcher, the caller included, after the answer.
+ pub events: Vec<Value>,
+}
+
+struct Client {
+ outbox: SyncSender<Vec<u8>>,
+ /// Shut down to drop a client whose writer may be stuck in a write.
+ socket: UnixStream,
+ watching: HashSet<&'static str>,
+}
+
+struct State {
+ plugins: Vec<Box<dyn Plugin>>,
+ clients: HashMap<u64, Client>,
+ next_client: u64,
+ /// When the last client left, or the host started; `None` while anyone
+ /// is connected.
+ idle_since: Option<Instant>,
+}
+
+pub struct Host {
+ state: Mutex<State>,
+ socket: PathBuf,
+ idle: Duration,
+}
+
+impl Host {
+ pub fn new(socket: PathBuf, idle: Duration, plugins: Vec<Box<dyn Plugin>>) -> Arc<Self> {
+ Arc::new(Self {
+ state: Mutex::new(State {
+ plugins,
+ clients: HashMap::new(),
+ next_client: 1,
+ idle_since: Some(Instant::now()),
+ }),
+ socket,
+ idle,
+ })
+ }
+
+ fn state(&self) -> MutexGuard<'_, State> {
+ // A plugin that panicked mid-call leaves its own data as it was;
+ // the table of clients is updated only here, between calls.
+ self.state
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ }
+
+ pub fn accept(self: &Arc<Self>, stream: UnixStream) {
+ let (mut writer, socket) = match stream
+ .try_clone()
+ .and_then(|w| Ok((w, stream.try_clone()?)))
+ {
+ Ok(handles) => handles,
+ Err(err) => {
+ log::warn!("taking more handles on a client: {err}");
+ return;
+ }
+ };
+ let (outbox, queued) = sync_channel::<Vec<u8>>(QUEUE);
+ let _ = outbox.try_send(FromHost::Hello { protocol: PROTOCOL }.encode());
+ let id = {
+ let mut state = self.state();
+ let id = state.next_client;
+ state.next_client += 1;
+ state.clients.insert(
+ id,
+ Client {
+ outbox,
+ socket,
+ watching: HashSet::new(),
+ },
+ );
+ state.idle_since = None;
+ id
+ };
+
+ let written = std::thread::Builder::new()
+ .name(format!("client-{id}-out"))
+ .stack_size(STACK)
+ .spawn(move || {
+ for frame in queued {
+ if write_frame(&mut writer, &frame).is_err() {
+ break;
+ }
+ }
+ // Dropped from the table, or gone: either way the reader
+ // is done with it too.
+ let _ = writer.shutdown(Shutdown::Both);
+ });
+ let host = Arc::clone(self);
+ let reading = std::thread::Builder::new()
+ .name(format!("client-{id}-in"))
+ .stack_size(STACK)
+ .spawn(move || {
+ host.serve(id, stream);
+ host.leave(id);
+ });
+ if let Err(err) = written.and(reading) {
+ log::warn!("starting the threads for a client: {err}");
+ self.leave(id);
+ }
+ }
+
+ fn serve(&self, client: u64, mut stream: UnixStream) {
+ loop {
+ let frame = match read_frame(&mut stream) {
+ Ok(frame) => frame,
+ Err(_) => return,
+ };
+ match ToHost::decode(&frame) {
+ Ok(ToHost::Call { id, plugin, body }) => self.call(client, id, &plugin, body),
+ Ok(ToHost::Quit) => {
+ log::info!("asked to quit by a newer build");
+ // Once any call in progress has finished.
+ self.exit(self.state());
+ }
+ Err(err) => {
+ log::warn!("client {client} sent something that is not a message: {err}");
+ return;
+ }
+ }
+ }
+ }
+
+ fn call(&self, client: u64, id: u64, plugin: &str, body: Value) {
+ let mut state = self.state();
+ let state = &mut *state;
+ let mut outcome = Outcome::default();
+ let (name, answer) = match state.plugins.iter_mut().find(|p| p.name() == plugin) {
+ Some(found) => (found.name(), found.call(body, &mut outcome)),
+ None => {
+ let answer = FromHost::Error {
+ id,
+ message: format!("there is no plugin named {plugin:?}"),
+ };
+ deliver(&mut state.clients, client, answer.encode());
+ return;
+ }
+ };
+ let answer = match answer {
+ Ok(body) => FromHost::Ok { id, body },
+ Err(err) => FromHost::Error {
+ id,
+ message: format!("{err:#}"),
+ },
+ };
+ if outcome.watch {
+ if let Some(caller) = state.clients.get_mut(&client) {
+ caller.watching.insert(name);
+ }
+ }
+ deliver(&mut state.clients, client, answer.encode());
+ for body in outcome.events {
+ let frame = FromHost::Event {
+ plugin: name.to_string(),
+ body,
+ }
+ .encode();
+ let watchers: Vec<u64> = state
+ .clients
+ .iter()
+ .filter(|(_, watcher)| watcher.watching.contains(name))
+ .map(|(id, _)| *id)
+ .collect();
+ for watcher in watchers {
+ deliver(&mut state.clients, watcher, frame.clone());
+ }
+ }
+ if state.clients.is_empty() {
+ state.idle_since.get_or_insert_with(Instant::now);
+ }
+ }
+
+ fn leave(&self, client: u64) {
+ let mut state = self.state();
+ state.clients.remove(&client);
+ if state.clients.is_empty() {
+ state.idle_since.get_or_insert_with(Instant::now);
+ }
+ }
+
+ /// Exits once nobody has been connected for the idle time.
+ pub fn exit_when_idle(self: &Arc<Self>) {
+ let host = Arc::clone(self);
+ let reaper = std::thread::Builder::new()
+ .name("idle".into())
+ .stack_size(STACK)
+ .spawn(move || loop {
+ let wait = {
+ // Decided with the table locked, so a client being
+ // accepted either registers first or finds the socket
+ // gone and starts a new host.
+ let state = host.state();
+ match state.idle_since {
+ Some(since) if since.elapsed() >= host.idle => {
+ log::info!("nobody connected for {:?}; exiting", host.idle);
+ host.exit(state);
+ }
+ Some(since) => host.idle - since.elapsed(),
+ None => host.idle,
+ }
+ };
+ std::thread::sleep(wait);
+ });
+ if let Err(err) = reaper {
+ log::warn!("cannot watch for idleness, so this host stays up: {err}");
+ }
+ }
+
+ /// Removes the socket, so the next client starts a new host rather
+ /// than finding a dead one, and exits. Every change is on disk by the
+ /// time it is answered, and holding the table means no call is under
+ /// way, so there is nothing left to save.
+ fn exit(&self, _held: MutexGuard<'_, State>) -> ! {
+ let _ = fs::remove_file(&self.socket);
+ std::process::exit(0);
+ }
+}
+
+/// Queues `frame` for `client`; one that is not keeping up is dropped,
+/// which closes its queue and so its connection.
+fn deliver(clients: &mut HashMap<u64, Client>, client: u64, frame: Vec<u8>) {
+ let Some(to) = clients.get(&client) else {
+ return;
+ };
+ match to.outbox.try_send(frame) {
+ Ok(()) => {}
+ Err(TrySendError::Full(_)) => {
+ log::warn!("client {client} stopped reading; disconnecting it");
+ if let Some(stuck) = clients.remove(&client) {
+ let _ = stuck.socket.shutdown(Shutdown::Both);
+ }
+ }
+ Err(TrySendError::Disconnected(_)) => {
+ clients.remove(&client);
+ }
+ }
+}
diff --git a/thinkterm-plugin-server/src/main.rs b/thinkterm-plugin-server/src/main.rs
new file mode 100644
index 00000000..8fce0d74
--- /dev/null
+++ b/thinkterm-plugin-server/src/main.rs
@@ -0,0 +1,193 @@
+//! The plugin host: ThinkTerm's plugins, in a process of their own.
+//!
+//! A client starts it when it first needs a plugin and nothing answers at
+//! the socket (see thinkterm-plugin-channel); it exits by itself once nothing has
+//! been connected for a while. One runs at a time: it holds a lock for as
+//! long as it runs, and a second one gives way.
+
+// Started in the background, never from a console: no window of its own.
+#![cfg_attr(windows, windows_subsystem = "windows")]
+
+mod host;
+mod snippets;
+
+use anyhow::Context;
+use std::fs::{self, File, OpenOptions, TryLockError};
+use std::io::ErrorKind;
+use std::path::{Path, PathBuf};
+use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
+use thinkterm_plugin_channel::paths;
+use wezterm_uds::UnixListener;
+
+/// How long the host stays up with nobody connected to it.
+const IDLE_EXIT: Duration = Duration::from_secs(30);
+/// How long a host waits for the lock before leaving it to the one that
+/// holds it: long enough for a host that was asked to quit to be gone.
+const LOCK_WAIT: Duration = Duration::from_secs(3);
+
+struct Args {
+ socket: PathBuf,
+ lock: PathBuf,
+ data_dir: PathBuf,
+ idle: Duration,
+}
+
+fn parse_args() -> Result<Args, String> {
+ let mut args = Args {
+ socket: paths::socket(),
+ lock: paths::lock(),
+ data_dir: paths::data_dir(),
+ idle: IDLE_EXIT,
+ };
+ let mut given = std::env::args_os().skip(1);
+ while let Some(arg) = given.next() {
+ let name = arg.to_string_lossy().into_owned();
+ let value = given
+ .next()
+ .ok_or_else(|| format!("{name} needs a value"))?;
+ match name.as_str() {
+ "--socket" => args.socket = value.into(),
+ "--lock" => args.lock = value.into(),
+ "--data-dir" => args.data_dir = value.into(),
+ "--idle-secs" => {
+ let secs = value
+ .to_str()
+ .and_then(|secs| secs.parse().ok())
+ .ok_or_else(|| format!("{name} needs a number of seconds"))?;
+ args.idle = Duration::from_secs(secs);
+ }
+ _ => return Err(format!("unknown argument {name}")),
+ }
+ }
+ Ok(args)
+}
+
+/// Standard error, which the client that started the host points at the
+/// host's log.
+struct Stderr;
+
+impl log::Log for Stderr {
+ fn enabled(&self, metadata: &log::Metadata) -> bool {
+ metadata.level() <= log::Level::Info
+ }
+
+ fn log(&self, record: &log::Record) {
+ if self.enabled(record.metadata()) {
+ let secs = SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .map(|since| since.as_secs())
+ .unwrap_or(0);
+ eprintln!(
+ "{secs} {} [{}] {}",
+ std::process::id(),
+ record.level(),
+ record.args()
+ );
+ }
+ }
+
+ fn flush(&self) {}
+}
+
+static LOGGER: Stderr = Stderr;
+
+fn main() {
+ if log::set_logger(&LOGGER).is_ok() {
+ log::set_max_level(log::LevelFilter::Info);
+ }
+ let args = match parse_args() {
+ Ok(args) => args,
+ Err(err) => {
+ eprintln!("thinkterm-plugin-server: {err}");
+ std::process::exit(2);
+ }
+ };
+ if let Err(err) = run(args) {
+ log::error!("{err:#}");
+ std::process::exit(1);
+ }
+}
+
+fn run(args: Args) -> anyhow::Result<()> {
+ let Some(_lock) = take_lock(&args.lock)? else {
+ log::info!("another plugin host is running; leaving it be");
+ return Ok(());
+ };
+ // Nothing else listens at the socket while this holds the lock: a file
+ // there was left by a host that died.
+ match fs::remove_file(&args.socket) {
+ Ok(()) => {}
+ Err(err) if err.kind() == ErrorKind::NotFound => {}
+ Err(err) => return Err(err).with_context(|| format!("remove {}", args.socket.display())),
+ }
+ let listener = UnixListener::bind(&args.socket)
+ .with_context(|| format!("listen at {}", args.socket.display()))?;
+ make_private(&args.socket);
+ log::info!("listening at {}", args.socket.display());
+
+ let plugins: Vec<Box<dyn host::Plugin>> = vec![Box::new(snippets::Snippets::new(
+ args.data_dir.join("snippets.json"),
+ ))];
+ let host = host::Host::new(args.socket, args.idle, plugins);
+ host.exit_when_idle();
+ for stream in listener.incoming() {
+ match stream {
+ Ok(stream) => host.accept(stream),
+ Err(err) => {
+ log::warn!("accepting a client: {err}");
+ std::thread::sleep(Duration::from_millis(100));
+ }
+ }
+ }
+ Ok(())
+}
+
+/// The lock that makes this the only host, waiting a little for one that is
+/// on its way out; `None` when another host keeps it.
+fn take_lock(path: &Path) -> anyhow::Result<Option<File>> {
+ if let Some(dir) = path.parent() {
+ let mut builder = fs::DirBuilder::new();
+ builder.recursive(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::DirBuilderExt;
+ builder.mode(0o700);
+ }
+ builder
+ .create(dir)
+ .with_context(|| format!("create {}", dir.display()))?;
+ }
+ let file = OpenOptions::new()
+ .create(true)
+ .write(true)
+ .truncate(false)
+ .open(path)
+ .with_context(|| format!("open {}", path.display()))?;
+ let deadline = Instant::now() + LOCK_WAIT;
+ loop {
+ match file.try_lock() {
+ Ok(()) => return Ok(Some(file)),
+ Err(TryLockError::WouldBlock) if Instant::now() < deadline => {
+ std::thread::sleep(Duration::from_millis(50));
+ }
+ Err(TryLockError::WouldBlock) => return Ok(None),
+ Err(TryLockError::Error(err)) => {
+ return Err(err).with_context(|| format!("lock {}", path.display()))
+ }
+ }
+ }
+}
+
+/// Only this user may connect. The directory is private already; this is
+/// for a socket placed somewhere that is not.
+fn make_private(path: &Path) {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ if let Err(err) = fs::set_permissions(path, fs::Permissions::from_mode(0o600)) {
+ log::warn!("making {} private: {err}", path.display());
+ }
+ }
+ #[cfg(not(unix))]
+ let _ = path;
+}
diff --git a/thinkterm-plugin-server/src/snippets.rs b/thinkterm-plugin-server/src/snippets.rs
new file mode 100644
index 00000000..b9f908cc
--- /dev/null
+++ b/thinkterm-plugin-server/src/snippets.rs
@@ -0,0 +1,354 @@
+//! The snippets plugin: the one copy of the user's snippets, the file it is
+//! kept in, and everything done with it. Clients show what it answers.
+
+use crate::host::{Outcome, Plugin};
+use anyhow::Context;
+use serde_json::Value;
+use std::path::{Path, PathBuf};
+use std::time::{SystemTime, UNIX_EPOCH};
+use thinkterm_snippets::wire::{Event, Request, Row, Saved, Snippet, PLUGIN};
+use thinkterm_snippets::{file, SnippetRecord, SnippetStore};
+
+pub struct Snippets {
+ path: PathBuf,
+ /// Read by the first call.
+ store: Option<SnippetStore>,
+ /// The file as this host last read or wrote it, to notice it being
+ /// replaced from outside -- by a restored backup, say.
+ seen: Option<Stamp>,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+struct Stamp {
+ len: u64,
+ modified: Option<SystemTime>,
+}
+
+fn stamp(path: &Path) -> Option<Stamp> {
+ let meta = std::fs::metadata(path).ok()?;
+ Some(Stamp {
+ len: meta.len(),
+ modified: meta.modified().ok(),
+ })
+}
+
+fn now_ms() -> u64 {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .map(|since| since.as_millis().min(u128::from(u64::MAX)) as u64)
+ .unwrap_or(0)
+}
+
+fn changed() -> Value {
+ serde_json::to_value(Event::Changed).expect("an event always serialises")
+}
+
+fn row(snippet: &SnippetRecord) -> Row {
+ Row {
+ id: snippet.id.clone(),
+ title: snippet.title.clone(),
+ preview: thinkterm_snippets::preview(&snippet.body),
+ }
+}
+
+impl Snippets {
+ pub fn new(path: PathBuf) -> Self {
+ Self {
+ path,
+ store: None,
+ seen: None,
+ }
+ }
+
+ /// The store as the file has it: read on first use and again whenever
+ /// the file changed behind this host's back, which is a change for
+ /// every client too. A file that does not read is an error, never an
+ /// empty store: an empty one would be written over it by the next save.
+ fn current(&mut self, outcome: &mut Outcome) -> anyhow::Result<&mut SnippetStore> {
+ let now = stamp(&self.path);
+ if self.store.is_none() || now != self.seen {
+ let store = file::load(&self.path)?;
+ if self.store.is_some() {
+ log::info!("{} changed on disk; reloaded it", self.path.display());
+ outcome.events.push(changed());
+ }
+ self.store = Some(store);
+ self.seen = now;
+ }
+ Ok(self.store.as_mut().expect("just loaded"))
+ }
+
+ /// Keeps `next` once it is on disk, and tells the clients.
+ fn commit(&mut self, next: SnippetStore, outcome: &mut Outcome) -> anyhow::Result<()> {
+ file::save(&self.path, &next)?;
+ self.store = Some(next);
+ self.seen = stamp(&self.path);
+ outcome.events.push(changed());
+ Ok(())
+ }
+}
+
+impl Plugin for Snippets {
+ fn name(&self) -> &'static str {
+ PLUGIN
+ }
+
+ fn call(&mut self, body: Value, outcome: &mut Outcome) -> anyhow::Result<Value> {
+ let request: Request =
+ serde_json::from_value(body).context("not a request the snippets plugin knows")?;
+ let answer = match request {
+ Request::List { query } => {
+ let store = self.current(outcome)?;
+ let rows: Vec<Row> = store.matching(&query).map(row).collect();
+ outcome.watch = true;
+ serde_json::to_value(rows)?
+ }
+ Request::Get { id } => {
+ let store = self.current(outcome)?;
+ let snippet = store.get(&id).map(|snippet| Snippet {
+ id: snippet.id.clone(),
+ title: snippet.title.clone(),
+ body: snippet.body.clone(),
+ });
+ serde_json::to_value(snippet)?
+ }
+ Request::Text { id, run } => {
+ let store = self.current(outcome)?;
+ let text = store.get(&id).and_then(|snippet| {
+ if run {
+ thinkterm_snippets::run_text(&snippet.body)
+ } else {
+ Some(snippet.body.clone())
+ }
+ });
+ serde_json::to_value(text)?
+ }
+ Request::Save { id, title, body } => {
+ let body = body.trim();
+ if body.is_empty() {
+ return Ok(serde_json::to_value(Saved::Empty)?);
+ }
+ // Saved before it is kept, so a change that did not reach
+ // the disk is neither kept nor announced.
+ let mut next = self.current(outcome)?.clone();
+ let now = now_ms();
+ let saved = match id {
+ None => {
+ let fresh = thinkterm_snippets::new_id(now);
+ next.create(fresh, &title, body.to_string(), now).id
+ }
+ Some(id) => match next.update(&id, &title, body.to_string(), now) {
+ Some(record) => record.id,
+ None => return Ok(serde_json::to_value(Saved::Gone)?),
+ },
+ };
+ self.commit(next, outcome)?;
+ serde_json::to_value(Saved::Saved { id: saved })?
+ }
+ Request::Delete { id } => {
+ let mut next = self.current(outcome)?.clone();
+ let deleted = next.delete(&id, now_ms());
+ if deleted {
+ self.commit(next, outcome)?;
+ }
+ Value::Bool(deleted)
+ }
+ };
+ Ok(answer)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ fn ask(plugin: &mut Snippets, request: Request) -> (Value, Outcome) {
+ let mut outcome = Outcome::default();
+ let answer = plugin
+ .call(serde_json::to_value(request).unwrap(), &mut outcome)
+ .unwrap();
+ (answer, outcome)
+ }
+
+ fn list(plugin: &mut Snippets, query: &str) -> Vec<Row> {
+ let (answer, outcome) = ask(
+ plugin,
+ Request::List {
+ query: query.into(),
+ },
+ );
+ assert!(outcome.watch, "a list is followed");
+ serde_json::from_value(answer).unwrap()
+ }
+
+ fn save(plugin: &mut Snippets, id: Option<&str>, title: &str, body: &str) -> Saved {
+ let (answer, _) = ask(
+ plugin,
+ Request::Save {
+ id: id.map(str::to_string),
+ title: title.into(),
+ body: body.into(),
+ },
+ );
+ serde_json::from_value(answer).unwrap()
+ }
+
+ fn plugin_in(dir: &tempfile::TempDir) -> Snippets {
+ Snippets::new(dir.path().join("snippets.json"))
+ }
+
+ #[test]
+ fn a_save_is_on_disk_in_the_list_and_announced() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut plugin = plugin_in(&dir);
+ assert!(list(&mut plugin, "").is_empty());
+
+ let mut outcome = Outcome::default();
+ let answer = plugin
+ .call(
+ json!({"op": "save", "title": "", "body": " git status \n"}),
+ &mut outcome,
+ )
+ .unwrap();
+ let Saved::Saved { id } = serde_json::from_value(answer).unwrap() else {
+ panic!("not saved");
+ };
+ assert_eq!(outcome.events, [changed()]);
+ assert!(id.starts_with("snippet-"));
+ let rows = list(&mut plugin, "");
+ assert_eq!(
+ rows,
+ [Row {
+ id: id.clone(),
+ title: "git status".into(),
+ preview: "git status".into()
+ }]
+ );
+ let on_disk = file::load(&dir.path().join("snippets.json")).unwrap();
+ assert_eq!(on_disk.get(&id).unwrap().body, "git status");
+ }
+
+ #[test]
+ fn an_edit_a_delete_and_a_search() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut plugin = plugin_in(&dir);
+ let Saved::Saved { id } = save(&mut plugin, None, "Pull", "git pull --rebase") else {
+ panic!("not saved");
+ };
+ save(&mut plugin, None, "", "ls -la");
+ assert_eq!(list(&mut plugin, "PULL").len(), 1);
+ assert_eq!(list(&mut plugin, "").len(), 2);
+
+ assert_eq!(
+ save(&mut plugin, Some(&id), "Fetch", "git fetch"),
+ Saved::Saved { id: id.clone() }
+ );
+ let (answer, _) = ask(&mut plugin, Request::Get { id: id.clone() });
+ let snippet: Snippet = serde_json::from_value(answer).unwrap();
+ assert_eq!(
+ (snippet.title.as_str(), snippet.body.as_str()),
+ ("Fetch", "git fetch")
+ );
+
+ let (answer, outcome) = ask(&mut plugin, Request::Delete { id: id.clone() });
+ assert_eq!(answer, json!(true));
+ assert_eq!(outcome.events, [changed()]);
+ let (answer, outcome) = ask(&mut plugin, Request::Delete { id: id.clone() });
+ assert_eq!(answer, json!(false));
+ assert!(outcome.events.is_empty(), "nothing changed");
+ assert_eq!(save(&mut plugin, Some(&id), "", "git fetch"), Saved::Gone);
+ let (answer, _) = ask(&mut plugin, Request::Get { id });
+ assert_eq!(answer, Value::Null);
+ }
+
+ #[test]
+ fn an_empty_script_is_not_saved() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut plugin = plugin_in(&dir);
+ assert_eq!(save(&mut plugin, None, "Title", " \n\t"), Saved::Empty);
+ assert!(list(&mut plugin, "").is_empty());
+ assert!(!dir.path().join("snippets.json").exists());
+ }
+
+ #[test]
+ fn the_text_a_pane_is_sent() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut plugin = plugin_in(&dir);
+ let Saved::Saved { id } = save(&mut plugin, None, "", "one\ntwo") else {
+ panic!("not saved");
+ };
+ let text = |plugin: &mut Snippets, run: bool| {
+ ask(
+ plugin,
+ Request::Text {
+ id: id.clone(),
+ run,
+ },
+ )
+ .0
+ };
+ assert_eq!(text(&mut plugin, false), json!("one\ntwo"));
+ assert_eq!(text(&mut plugin, true), json!("one\rtwo\r"));
+ let (answer, _) = ask(
+ &mut plugin,
+ Request::Text {
+ id: "gone".into(),
+ run: true,
+ },
+ );
+ assert_eq!(answer, Value::Null);
+ }
+
+ #[test]
+ fn a_file_replaced_from_outside_is_reread_and_announced() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("snippets.json");
+ let mut plugin = plugin_in(&dir);
+ save(&mut plugin, None, "", "ls");
+
+ let mut restored = SnippetStore::default();
+ restored.create("b".into(), "", "pwd --physical".into(), 2);
+ file::save(&path, &restored).unwrap();
+
+ let mut outcome = Outcome::default();
+ let answer = plugin
+ .call(json!({"op": "list", "query": ""}), &mut outcome)
+ .unwrap();
+ let rows: Vec<Row> = serde_json::from_value(answer).unwrap();
+ assert_eq!(
+ rows.iter().map(|r| r.id.as_str()).collect::<Vec<_>>(),
+ ["b"]
+ );
+ assert_eq!(outcome.events, [changed()]);
+ }
+
+ #[test]
+ fn a_file_that_does_not_read_is_left_alone() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("snippets.json");
+ std::fs::write(&path, "not json").unwrap();
+ let mut plugin = plugin_in(&dir);
+ let mut outcome = Outcome::default();
+ assert!(plugin
+ .call(json!({"op": "list", "query": ""}), &mut outcome)
+ .is_err());
+ assert!(plugin
+ .call(
+ json!({"op": "save", "title": "", "body": "ls"}),
+ &mut outcome
+ )
+ .is_err());
+ assert_eq!(std::fs::read_to_string(&path).unwrap(), "not json");
+ }
+
+ #[test]
+ fn an_unknown_request_is_an_error() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut plugin = plugin_in(&dir);
+ let mut outcome = Outcome::default();
+ assert!(plugin
+ .call(json!({"op": "frobnicate"}), &mut outcome)
+ .is_err());
+ }
+}
diff --git a/thinkterm-plugin-server/tests/host.rs b/thinkterm-plugin-server/tests/host.rs
new file mode 100644
index 00000000..307814ed
--- /dev/null
+++ b/thinkterm-plugin-server/tests/host.rs
@@ -0,0 +1,389 @@
+//! The host as its clients see it: a real process, started by the client
+//! library, on a socket in a directory of the test's own.
+
+use serde_json::{json, Value};
+use std::path::{Path, PathBuf};
+use std::process::{Child, Command};
+use std::sync::mpsc;
+use std::time::{Duration, Instant};
+use thinkterm_plugin_channel::client::{Answer, Connection, Host, Notice, Session};
+use thinkterm_plugin_channel::wire::{read_frame, write_frame, FromHost, ToHost, PROTOCOL};
+use thinkterm_snippets::wire::{Event, Request, Row, Saved, PLUGIN};
+
+const WAIT: Duration = Duration::from_secs(10);
+
+fn host_in(dir: &Path) -> Host {
+ Host {
+ socket: dir.join("sock"),
+ lock: dir.join("lock"),
+ data_dir: dir.join("data"),
+ log: dir.join("log"),
+ program: PathBuf::from(env!("CARGO_BIN_EXE_thinkterm-plugin-server")),
+ }
+}
+
+/// A host started by hand, with arguments the client library does not pass.
+fn spawn(host: &Host, extra: &[&str]) -> Child {
+ Command::new(&host.program)
+ .arg("--socket")
+ .arg(&host.socket)
+ .arg("--lock")
+ .arg(&host.lock)
+ .arg("--data-dir")
+ .arg(&host.data_dir)
+ .args(extra)
+ .spawn()
+ .unwrap()
+}
+
+/// Leaves no host running after the test.
+fn stop(host: &Host) {
+ if let Ok(mut connection) = host.connect() {
+ let _ = connection.send(&ToHost::Quit);
+ }
+}
+
+fn wait_until(what: &str, mut done: impl FnMut() -> bool) {
+ let deadline = Instant::now() + WAIT;
+ while !done() {
+ assert!(Instant::now() < deadline, "timed out waiting for {what}");
+ std::thread::sleep(Duration::from_millis(20));
+ }
+}
+
+fn call(connection: &mut Connection, id: u64, request: Request) {
+ connection
+ .send(&ToHost::Call {
+ id,
+ plugin: PLUGIN.into(),
+ body: serde_json::to_value(request).unwrap(),
+ })
+ .unwrap();
+}
+
+fn answer(connection: &mut Connection, id: u64) -> Value {
+ match connection.recv().unwrap() {
+ FromHost::Ok { id: got, body } if got == id => body,
+ other => panic!("expected the answer to {id}, got {other:?}"),
+ }
+}
+
+fn event(connection: &mut Connection) -> Event {
+ match connection.recv().unwrap() {
+ FromHost::Event { plugin, body } if plugin == PLUGIN => {
+ serde_json::from_value(body).unwrap()
+ }
+ other => panic!("expected an event, got {other:?}"),
+ }
+}
+
+fn list(connection: &mut Connection, id: u64) -> Vec<Row> {
+ call(
+ connection,
+ id,
+ Request::List {
+ query: String::new(),
+ },
+ );
+ serde_json::from_value(answer(connection, id)).unwrap()
+}
+
+fn save(connection: &mut Connection, id: u64, body: &str) -> Saved {
+ call(
+ connection,
+ id,
+ Request::Save {
+ id: None,
+ title: String::new(),
+ body: body.into(),
+ },
+ );
+ serde_json::from_value(answer(connection, id)).unwrap()
+}
+
+#[test]
+fn two_clients_share_one_store_and_hear_each_others_changes() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let mut desktop = host.connect().unwrap();
+ let mut browser = host.connect().unwrap();
+ assert!(list(&mut desktop, 1).is_empty());
+ assert!(list(&mut browser, 1).is_empty());
+
+ assert!(matches!(
+ save(&mut desktop, 2, "ls -la"),
+ Saved::Saved { .. }
+ ));
+ assert_eq!(event(&mut desktop), Event::Changed);
+ assert_eq!(event(&mut browser), Event::Changed);
+ let rows = list(&mut browser, 2);
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].preview, "ls -la");
+
+ let on_disk = thinkterm_snippets::file::load(&host.data_dir.join("snippets.json")).unwrap();
+ assert_eq!(on_disk.get(&rows[0].id).unwrap().body, "ls -la");
+ stop(&host);
+}
+
+#[test]
+fn a_host_that_went_away_is_started_again_with_the_same_snippets() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let mut client = host.connect().unwrap();
+ list(&mut client, 1);
+ assert!(matches!(
+ save(&mut client, 2, "make test"),
+ Saved::Saved { .. }
+ ));
+
+ client.send(&ToHost::Quit).unwrap();
+ // The event for the save may still be on its way; the end comes after it.
+ wait_until("the host to go", || client.recv().is_err());
+ wait_until("the socket to go", || !host.socket.exists());
+
+ let mut again = host.connect().unwrap();
+ assert_eq!(list(&mut again, 1)[0].preview, "make test");
+ stop(&host);
+}
+
+#[test]
+fn a_call_to_no_plugin_is_answered_with_an_error() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let mut client = host.connect().unwrap();
+ client
+ .send(&ToHost::Call {
+ id: 9,
+ plugin: "nothing".into(),
+ body: json!({}),
+ })
+ .unwrap();
+ match client.recv().unwrap() {
+ FromHost::Error { id: 9, message } => assert!(message.contains("nothing"), "{message}"),
+ other => panic!("expected an error, got {other:?}"),
+ }
+ stop(&host);
+}
+
+#[test]
+fn only_one_host_runs_at_a_time() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let mut first = spawn(&host, &[]);
+ wait_until("the first host to listen", || host.socket.exists());
+ let mut second = spawn(&host, &[]);
+ let mut status = None;
+ wait_until("the second host to give way", || {
+ status = second.try_wait().unwrap();
+ status.is_some()
+ });
+ assert!(status.unwrap().success());
+ assert!(
+ first.try_wait().unwrap().is_none(),
+ "the first host keeps running"
+ );
+ list(&mut host.connect().unwrap(), 1);
+ stop(&host);
+ wait_until("the first host to quit", || {
+ first.try_wait().unwrap().is_some()
+ });
+}
+
+#[test]
+fn the_host_exits_once_nobody_is_connected() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let mut child = spawn(&host, &["--idle-secs", "1"]);
+ wait_until("the host to listen", || host.socket.exists());
+ let mut client = host.connect().unwrap();
+ list(&mut client, 1);
+ // Connected, it stays.
+ std::thread::sleep(Duration::from_millis(1500));
+ assert!(child.try_wait().unwrap().is_none());
+ drop(client);
+ wait_until("the idle host to exit", || {
+ child.try_wait().unwrap().is_some()
+ });
+ assert!(!host.socket.exists(), "an exiting host removes its socket");
+}
+
+/// A listener at the host's socket that says hello with `protocol` and
+/// hands back what it is sent first.
+fn impostor(host: &Host, protocol: u32) -> std::thread::JoinHandle<Option<ToHost>> {
+ std::fs::create_dir_all(host.socket.parent().unwrap()).unwrap();
+ let listener = wezterm_uds::UnixListener::bind(&host.socket).unwrap();
+ let socket = host.socket.clone();
+ std::thread::spawn(move || {
+ let (mut stream, _) = listener.accept().unwrap();
+ write_frame(&mut stream, &FromHost::Hello { protocol }.encode()).unwrap();
+ let first = read_frame(&mut stream)
+ .ok()
+ .and_then(|frame| ToHost::decode(&frame).ok());
+ if first == Some(ToHost::Quit) {
+ // As a host does: the socket goes before the process.
+ let _ = std::fs::remove_file(&socket);
+ }
+ first
+ })
+}
+
+#[test]
+fn a_host_from_an_older_build_is_replaced() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let older = impostor(&host, PROTOCOL - 1);
+ let mut client = host.connect().unwrap();
+ assert_eq!(older.join().unwrap(), Some(ToHost::Quit));
+ list(&mut client, 1);
+ stop(&host);
+}
+
+#[test]
+fn a_host_from_a_newer_build_is_left_alone() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let newer = impostor(&host, PROTOCOL + 1);
+ let err = host.connect().unwrap_err();
+ assert!(format!("{err:#}").contains("newer"), "{err:#}");
+ // It was not asked to quit: the connection just closed.
+ assert_eq!(newer.join().unwrap(), None);
+ assert!(host.socket.exists());
+}
+
+/// A session, and the notices it gives.
+fn session(host: &Host) -> (Session, mpsc::Receiver<Notice>) {
+ let (tx, rx) = mpsc::channel();
+ let session = Session::start(host.clone(), move |notice| {
+ let _ = tx.send(notice);
+ })
+ .unwrap();
+ (session, rx)
+}
+
+fn ask(session: &Session, request: Request) -> Answer {
+ let (tx, rx) = mpsc::channel();
+ session.call(
+ PLUGIN,
+ serde_json::to_value(request).unwrap(),
+ move |answer| {
+ let _ = tx.send(answer);
+ },
+ );
+ rx.recv_timeout(WAIT).expect("every call is answered")
+}
+
+fn next_notice(notices: &mpsc::Receiver<Notice>, wanted: impl Fn(&Notice) -> bool) -> Notice {
+ let deadline = Instant::now() + WAIT;
+ loop {
+ let left = deadline.saturating_duration_since(Instant::now());
+ let notice = notices.recv_timeout(left).expect("a notice");
+ if wanted(&notice) {
+ return notice;
+ }
+ }
+}
+
+#[test]
+fn a_session_follows_the_host_across_a_restart() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ let (session, notices) = session(&host);
+ next_notice(&notices, |n| matches!(n, Notice::Connected));
+ let listed = ask(
+ &session,
+ Request::List {
+ query: String::new(),
+ },
+ );
+ assert_eq!(listed, Ok(json!([])));
+ let saved = ask(
+ &session,
+ Request::Save {
+ id: None,
+ title: "Status".into(),
+ body: "git status".into(),
+ },
+ )
+ .unwrap();
+ assert_eq!(saved["outcome"], "saved");
+ let Notice::Event { plugin, body } =
+ next_notice(&notices, |n| matches!(n, Notice::Event { .. }))
+ else {
+ unreachable!()
+ };
+ assert_eq!(
+ (plugin.as_str(), body),
+ (PLUGIN, json!({"event": "changed"}))
+ );
+
+ // The host goes; the session starts another and says so.
+ stop(&host);
+ next_notice(&notices, |n| matches!(n, Notice::Connected));
+ let rows: Vec<Row> = serde_json::from_value(
+ ask(
+ &session,
+ Request::List {
+ query: "STATUS".into(),
+ },
+ )
+ .unwrap(),
+ )
+ .unwrap();
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].title, "Status");
+ drop(session);
+ stop(&host);
+}
+
+#[test]
+fn a_session_with_no_host_to_start_answers_every_call() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut host = host_in(dir.path());
+ host.program = dir.path().join("no-such-program");
+ let (session, notices) = session(&host);
+ let why = ask(
+ &session,
+ Request::List {
+ query: String::new(),
+ },
+ )
+ .unwrap_err();
+ assert!(why.contains("no-such-program"), "{why}");
+ let trouble = next_notice(&notices, |n| matches!(n, Notice::Trouble(_)));
+ assert!(matches!(trouble, Notice::Trouble(why) if why.contains("no-such-program")));
+}
+
+#[test]
+fn a_call_to_a_host_that_hangs_is_given_up_on() {
+ let dir = tempfile::tempdir().unwrap();
+ let host = host_in(dir.path());
+ // Says hello, takes every call, answers none.
+ let listener = wezterm_uds::UnixListener::bind(&host.socket).unwrap();
+ let hung = std::thread::spawn(move || {
+ let (mut stream, _) = listener.accept().unwrap();
+ write_frame(
+ &mut stream,
+ &FromHost::Hello { protocol: PROTOCOL }.encode(),
+ )
+ .unwrap();
+ while read_frame(&mut stream).is_ok() {}
+ });
+ let (tx, notices) = mpsc::channel();
+ let session = Session::start_with(host.clone(), Duration::from_millis(300), move |notice| {
+ let _ = tx.send(notice);
+ })
+ .unwrap();
+ next_notice(&notices, |n| matches!(n, Notice::Connected));
+ let started = Instant::now();
+ let why = ask(
+ &session,
+ Request::List {
+ query: String::new(),
+ },
+ )
+ .unwrap_err();
+ assert!(why.contains("did not answer"), "{why}");
+ assert!(started.elapsed() >= Duration::from_millis(300));
+ drop(session);
+ hung.join().unwrap();
+}
diff --git a/thinkterm-snippets/Cargo.toml b/thinkterm-snippets/Cargo.toml
index c3641e5f..29d3e926 100644
--- a/thinkterm-snippets/Cargo.toml
+++ b/thinkterm-snippets/Cargo.toml
@@ -14,7 +14,7 @@ native = ["dep:log", "dep:tempfile", "dep:uuid"]
anyhow.workspace = true
log = { workspace = true, optional = true }
memchr.workspace = true
-serde = { workspace = true, features = ["derive"] }
+serde = { workspace = true, features = ["derive", "std"] }
serde_json.workspace = true
tempfile = { workspace = true, optional = true }
uuid = { workspace = true, features = ["v4"], optional = true }
diff --git a/thinkterm-snippets/src/lib.rs b/thinkterm-snippets/src/lib.rs
index 9fe4af9c..57ccf865 100644
--- a/thinkterm-snippets/src/lib.rs
+++ b/thinkterm-snippets/src/lib.rs
@@ -15,6 +15,8 @@ use std::sync::OnceLock;
#[cfg(feature = "native")]
pub mod file;
+pub mod view;
+pub mod wire;
pub type SnippetId = String;
@@ -86,6 +88,36 @@ pub fn title_from_body(body: &str) -> String {
.collect()
}
+/// The line a snippet shows under its title: the body's first non-empty
+/// line, shortened.
+pub fn preview(body: &str) -> String {
+ body.lines()
+ .find_map(|line| {
+ let trimmed = line.trim();
+ (!trimmed.is_empty()).then_some(trimmed)
+ })
+ .unwrap_or("")
+ .chars()
+ .take(96)
+ .collect()
+}
+
+/// What running a snippet types into a pane: the body without its blank
+/// first and last lines, each line ended with Enter. `None` for a body with
+/// nothing to run.
+pub fn run_text(body: &str) -> Option<String> {
+ let normalized = body.replace("\r\n", "\n").replace('\r', "\n");
+ let lines: Vec<_> = normalized.split('\n').collect();
+ let start = lines.iter().position(|line| !line.trim().is_empty())?;
+ let end = lines.iter().rposition(|line| !line.trim().is_empty())?;
+ let trimmed = lines[start..=end].join("\n");
+
+ let mut buffer = String::with_capacity(trimmed.len() + 1);
+ buffer.push_str(&trimmed);
+ buffer.push('\n');
+ Some(buffer.replace('\n', "\r"))
+}
+
fn normalized_title(title: &str, body: &str) -> String {
let title = title.trim();
if title.is_empty() {
@@ -347,6 +379,33 @@ mod tests {
assert_eq!(title_from_body("\n ls -la\npwd"), "ls -la");
}
+ #[test]
+ fn the_preview_is_the_first_line_with_text() {
+ assert_eq!(preview("\n \n git status \nls"), "git status");
+ assert_eq!(preview(&"x".repeat(200)).chars().count(), 96);
+ assert_eq!(preview(" \n"), "");
+ }
+
+ #[test]
+ fn run_text_appends_single_enter() {
+ assert_eq!(run_text("sudo apt update").unwrap(), "sudo apt update\r");
+ }
+
+ #[test]
+ fn run_text_trims_outer_blank_lines() {
+ assert_eq!(run_text("\n\ncmd\r\n").unwrap(), "cmd\r");
+ }
+
+ #[test]
+ fn run_text_preserves_internal_script_lines() {
+ assert_eq!(run_text("one\ntwo").unwrap(), "one\rtwo\r");
+ }
+
+ #[test]
+ fn run_text_ignores_blank_body() {
+ assert!(run_text("\n \r\n\t").is_none());
+ }
+
#[test]
fn create_puts_the_new_snippet_first() {
let mut store = SnippetStore::default();
diff --git a/thinkterm-snippets/src/view.rs b/thinkterm-snippets/src/view.rs
new file mode 100644
index 00000000..573bbe50
--- /dev/null
+++ b/thinkterm-snippets/src/view.rs
@@ -0,0 +1,175 @@
+//! What a client shows of the snippets: the rows the plugin host sent for
+//! its search, and when to ask for them again. Every client follows the
+//! same rules -- one request at a time, the newest search asked for once
+//! it lands -- so a desktop window and a browser page behave alike, and
+//! typing in a search box never has a request per keystroke in flight.
+
+use crate::wire::Row;
+
+#[derive(Debug, Default)]
+pub struct Listing {
+ /// What the search box holds.
+ query: String,
+ /// The rows on show; `None` until the host first answers.
+ rows: Option<Vec<Row>>,
+ /// The search the rows on show answer; `None` once a change made them
+ /// out of date.
+ answers: Option<String>,
+ /// A request is on its way.
+ asking: bool,
+ /// Requests sent, for probes.
+ asked: u64,
+}
+
+impl Listing {
+ pub fn query(&self) -> &str {
+ &self.query
+ }
+
+ pub fn set_query(&mut self, query: &str) {
+ if self.query != query {
+ self.query = query.to_string();
+ }
+ }
+
+ /// The rows on show; `None` until the host first answers.
+ pub fn rows(&self) -> Option<&[Row]> {
+ self.rows.as_deref()
+ }
+
+ /// How many times the rows were asked for.
+ pub fn asked(&self) -> u64 {
+ self.asked
+ }
+
+ /// The search the rows on show answer: the search box's, once they
+ /// have caught up with it.
+ pub fn answers(&self) -> Option<&str> {
+ self.answers.as_deref()
+ }
+
+ /// The search to ask the host about now, if any: none while a request
+ /// is on its way, nor while the rows on show answer the search and
+ /// nothing changed since.
+ pub fn next(&mut self) -> Option<String> {
+ if self.asking || self.answers.as_deref() == Some(self.query.as_str()) {
+ return None;
+ }
+ self.asking = true;
+ self.asked += 1;
+ Some(self.query.clone())
+ }
+
+ /// The host's answer to the request for `query`. Rows answering an
+ /// older search are shown until the newest one's arrive. An error
+ /// leaves the rows on show, is handed back, and is not asked about
+ /// again until something changes: an unreachable host is not asked in
+ /// a loop.
+ pub fn answered(
+ &mut self,
+ query: String,
+ answer: Result<Vec<Row>, String>,
+ ) -> Result<(), String> {
+ self.asking = false;
+ self.answers = Some(query);
+ self.rows = Some(answer?);
+ Ok(())
+ }
+
+ /// The snippets changed, or the host is back: the rows are asked for
+ /// again.
+ pub fn changed(&mut self) {
+ self.answers = None;
+ }
+
+ /// Lets the rows go; the search stays, and is asked about afresh.
+ pub fn clear(&mut self) {
+ self.rows = None;
+ self.answers = None;
+ self.asking = false;
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn rows(ids: &[&str]) -> Vec<Row> {
+ ids.iter()
+ .map(|id| Row {
+ id: id.to_string(),
+ title: id.to_string(),
+ preview: String::new(),
+ })
+ .collect()
+ }
+
+ fn ids(listing: &Listing) -> Vec<&str> {
+ listing
+ .rows()
+ .unwrap_or_default()
+ .iter()
+ .map(|row| row.id.as_str())
+ .collect()
+ }
+
+ #[test]
+ fn one_request_at_a_time_and_the_newest_search_wins() {
+ let mut listing = Listing::default();
+ assert_eq!(listing.next().as_deref(), Some(""));
+ // Typing while the first request is out asks for nothing more...
+ for query in ["g", "gi", "git"] {
+ listing.set_query(query);
+ assert_eq!(listing.next(), None);
+ }
+ // ...until it lands; then only the newest search is asked for.
+ listing
+ .answered(String::new(), Ok(rows(&["a", "b"])))
+ .unwrap();
+ assert_eq!(ids(&listing), ["a", "b"], "shown meanwhile");
+ assert_eq!(listing.next().as_deref(), Some("git"));
+ listing.answered("git".into(), Ok(rows(&["a"]))).unwrap();
+ assert_eq!(listing.next(), None, "answered, and nothing changed");
+ assert_eq!(ids(&listing), ["a"]);
+ assert_eq!(listing.asked(), 2);
+ }
+
+ #[test]
+ fn a_change_asks_again() {
+ let mut listing = Listing::default();
+ listing.next();
+ listing.answered(String::new(), Ok(rows(&["a"]))).unwrap();
+ listing.changed();
+ assert_eq!(listing.next().as_deref(), Some(""));
+ }
+
+ #[test]
+ fn an_error_keeps_the_rows_and_is_not_asked_about_in_a_loop() {
+ let mut listing = Listing::default();
+ listing.next();
+ assert_eq!(
+ listing.answered(String::new(), Err("no host".into())),
+ Err("no host".into())
+ );
+ assert!(listing.rows().is_none());
+ assert_eq!(listing.next(), None);
+ listing.changed();
+ listing.next();
+ listing.answered(String::new(), Ok(rows(&["a"]))).unwrap();
+ listing.changed();
+ listing.next();
+ assert!(listing.answered(String::new(), Err("gone".into())).is_err());
+ assert_eq!(ids(&listing), ["a"]);
+ }
+
+ #[test]
+ fn clearing_lets_the_rows_go_and_asks_afresh() {
+ let mut listing = Listing::default();
+ listing.set_query("git");
+ listing.next();
+ listing.answered("git".into(), Ok(rows(&["a"]))).unwrap();
+ listing.clear();
+ assert!(listing.rows().is_none());
+ assert_eq!(listing.next().as_deref(), Some("git"));
+ }
+}
diff --git a/thinkterm-snippets/src/wire.rs b/thinkterm-snippets/src/wire.rs
new file mode 100644
index 00000000..b273565f
--- /dev/null
+++ b/thinkterm-snippets/src/wire.rs
@@ -0,0 +1,111 @@
+//! The snippets plugin's messages: JSON, carried in the plugin channel as a
+//! call's body, its answer and the events a client is sent.
+//!
+//! The plugin keeps the snippets and does everything with them: searching,
+//! saving, deleting, turning one into what a pane is sent. A client shows
+//! what it is given and says what the user did; it keeps no copy of its own.
+
+use crate::SnippetId;
+use serde::{Deserialize, Serialize};
+
+/// The name calls address and events carry.
+pub const PLUGIN: &str = "snippets";
+
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(tag = "op", rename_all = "snake_case")]
+pub enum Request {
+ /// The snippets matching `query`, as a list shows them, newest first.
+ /// Answered with `Vec<Row>`. The caller is told of every change after
+ /// it, and asks again.
+ List { query: String },
+ /// One snippet whole, for an editor. Answered with a `Snippet`, or
+ /// `null` when there is no such snippet.
+ Get { id: SnippetId },
+ /// What a pane is sent for a snippet: its body as a paste, or with
+ /// `run` typed and run line by line. Answered with a string, or `null`
+ /// when there is no such snippet or nothing in it to run.
+ Text { id: SnippetId, run: bool },
+ /// Save what an editor holds, a new snippet when `id` is absent.
+ /// Answered with `Saved`.
+ Save {
+ #[serde(default)]
+ id: Option<SnippetId>,
+ title: String,
+ body: String,
+ },
+ /// Answered with whether there was such a snippet to delete.
+ Delete { id: SnippetId },
+}
+
+/// One snippet as a list shows it.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Row {
+ pub id: SnippetId,
+ pub title: String,
+ /// The first line with text in it, shortened.
+ pub preview: String,
+}
+
+/// One snippet whole, for an editor.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Snippet {
+ pub id: SnippetId,
+ pub title: String,
+ pub body: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "outcome", rename_all = "snake_case")]
+pub enum Saved {
+ /// Saved, under this id: a new snippet's, just given.
+ Saved { id: SnippetId },
+ /// Nothing was saved: the script is empty.
+ Empty,
+ /// Nothing was saved: the snippet being edited was deleted meanwhile.
+ Gone,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "event", rename_all = "snake_case")]
+pub enum Event {
+ /// The snippets changed, here or from outside: a list on show is to be
+ /// asked for again.
+ Changed,
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::{json, to_value};
+
+ #[test]
+ fn requests_answers_and_events_are_tagged_json() {
+ assert_eq!(
+ to_value(Request::List {
+ query: "git".into()
+ })
+ .unwrap(),
+ json!({"op": "list", "query": "git"})
+ );
+ // A new snippet's save names no id.
+ let save: Request =
+ serde_json::from_value(json!({"op": "save", "title": "", "body": "ls"})).unwrap();
+ assert_eq!(
+ save,
+ Request::Save {
+ id: None,
+ title: String::new(),
+ body: "ls".into()
+ }
+ );
+ assert_eq!(
+ to_value(Saved::Saved { id: "a".into() }).unwrap(),
+ json!({"outcome": "saved", "id": "a"})
+ );
+ assert_eq!(to_value(Saved::Empty).unwrap(), json!({"outcome": "empty"}));
+ assert_eq!(
+ to_value(Event::Changed).unwrap(),
+ json!({"event": "changed"})
+ );
+ }
+}
diff --git a/wezterm-gui/Cargo.toml b/wezterm-gui/Cargo.toml
index 6c311652..5945db8c 100644
--- a/wezterm-gui/Cargo.toml
+++ b/wezterm-gui/Cargo.toml
@@ -118,7 +118,8 @@ thiserror.workspace = true
tiny-skia.workspace = true
thinkterm-syntax = { path = "../thinkterm-syntax" }
thinkterm-core.workspace = true
-thinkterm-snippets.workspace = true
+thinkterm-plugin-channel = { workspace = true, features = ["native"] }
+thinkterm-snippets = { workspace = true, features = ["native"] }
trash.workspace = true
umask.workspace = true
unicode-normalization.workspace = true
diff --git a/wezterm-uds/src/lib.rs b/wezterm-uds/src/lib.rs
index dde579dc..09b20490 100644
--- a/wezterm-uds/src/lib.rs
+++ b/wezterm-uds/src/lib.rs
@@ -101,6 +101,12 @@ impl UnixStream {
pub fn connect<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
Ok(Self(StreamImpl::connect(path)?))
}
+
+ /// A second handle on the same socket, wrapped like the first, so one
+ /// thread can read while another writes.
+ pub fn try_clone(&self) -> std::io::Result<Self> {
+ Ok(Self(self.0.try_clone()?))
+ }
}
impl std::ops::Deref for UnixStream {