Skip to content

Commit 6b11312

Browse files
authored
feat(process-daemon): rename terminals (anomalyco#60324)
1 parent 17f4634 commit 6b11312

13 files changed

Lines changed: 391 additions & 23 deletions

File tree

‎packages/process-daemon/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ ownership deadline is due, or a two-second safety net elapses. Windows still
7171
polls every 25 ms.
7272

7373
The Windows named-pipe backend keeps protocol 7 and the registration fields
74-
`instance_id`, `pid`, `protocol`, `socket`, and `token`. Treat `socket` as an opaque
74+
`instance_id`, `pid`, `protocol`, `socket`, `token`, and `features`. Treat `socket` as an opaque
7575
local endpoint: on Windows it is `\\.\pipe\opencode-pty-<instance_id>`, not a
7676
filesystem socket. A random per-instance name, exclusive first pipe instance,
7777
current-user access control, and rejection of remote clients protect the endpoint;

‎packages/process-daemon/SPEC.md‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ replay, and explicit lifecycle state.
2727
- Promotion, canonical resize, and input are one ordered actor command.
2828
- Subscription replay and live events cross one ordered actor boundary.
2929
- OSC 0/2 title changes update authoritative terminal metadata and publish ordered subscription events.
30+
- A user title set by rename takes precedence over OSC titles; title changes publish only when the effective title changes.
3031
- The deepest TTY-attached process in the foreground process group updates authoritative process metadata.
3132
- Controller delivery applies backpressure; observers tolerate bounded output bursts and are disconnected when their backlog remains full.
3233
- Adjacent output is coalesced before fanout, flushing at 8 KiB, after 1 ms, or before the next non-output event.
@@ -57,7 +58,7 @@ A snapshot is built on the actor thread and contains owned values only:
5758
- Plain parsed screen text
5859
- VT checkpoint synthesized by libghostty formatter
5960
- Raw replay head and tail offsets
60-
- Current title derived from libghostty state
61+
- Current effective title and user title
6162
- Current foreground process derived from PTY process state
6263

6364
No borrowed libghostty object crosses a thread boundary.
@@ -88,6 +89,24 @@ bounded. Results exceeding that budget fail rather than truncate. Complete
8889
responses remain subject to the transport's 8 MiB frame limit. Protocol 7
8990
supports `read_rows` alongside the existing snapshot and replay operations.
9091

92+
## Titles
93+
94+
`TerminalInfo.title` is the effective title: the user title, else the latest
95+
OSC 0/2 title, else the title given at creation. An empty OSC title (a program
96+
resetting it) or an empty user title counts as unset. `TerminalInfo.user_title`
97+
is always serialized, as `null` when unset.
98+
99+
`Request::Rename { id, title: Option<String> }` (`op: "rename"`) sets the user
100+
title, or clears it when `title` is null or empty, and returns `Response::Ok`.
101+
The terminal actor handles it as one command, so it is ordered with output.
102+
OSC titles keep being tracked while a user title is set and show through again
103+
once it is cleared.
104+
105+
Rename and OSC updates publish `TitleChanged` with the effective title only
106+
when it changes; an OSC change hidden behind a user title publishes nothing.
107+
108+
`list` returns terminals in creation order.
109+
91110
## Failure Boundary
92111

93112
Losing the owning server connection stops `opencode-pty` and its terminals.
@@ -108,11 +127,19 @@ only observe or operate an existing daemon and never start one.
108127
Protocol v7 uses four-byte big-endian framing with bounded UTF-8 JSON control
109128
frames and tagged raw binary output frames. It supports ping, create, list,
110129
write, resize, atomic interaction promotion, snapshot, read_rows, replay, replay-to-live
111-
subscriptions, title updates, controller takeover, terminate, and destructive service
130+
subscriptions, title updates, rename, controller takeover, terminate, and destructive service
112131
shutdown, ownership acquisition, and restart handoff preparation.
113132

133+
`Pong` and the registration carry `features`, the daemon's additive
134+
capabilities (currently `["rename"]`). Additive changes, such as a new request
135+
or field older clients can ignore, extend `features` and keep the protocol
136+
version; clients check `features` before sending a new request, since a daemon
137+
that cannot parse one drops the connection. Only breaking changes bump
138+
`PROTOCOL_VERSION`, which clients reject on mismatch. A missing `features`
139+
means none.
140+
114141
Registration is written atomically with private permissions and contains an
115-
instance ID, PID, protocol version, socket path, and random credential. A
142+
instance ID, PID, protocol version, features, socket path, and random credential. A
116143
service lock elects one process and protects stale socket cleanup. On Unix, the
117144
socket uses a fixed-length hash of the canonical runtime path
118145
under a private per-user `/tmp` directory to stay below platform path limits.

‎packages/process-daemon/src/client.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl TerminalClient {
144144
instance_id,
145145
pid,
146146
protocol,
147+
..
147148
} if instance_id == client.registration.instance_id
148149
&& pid == client.registration.pid
149150
&& protocol == PROTOCOL_VERSION =>

‎packages/process-daemon/src/daemon.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct Registration {
1414
/// Opaque local transport endpoint, not necessarily a filesystem entry.
1515
pub socket: PathBuf,
1616
pub token: String,
17+
#[serde(default)]
18+
pub features: Vec<String>,
1719
}
1820

1921
/// Shared parent of runtime directories, inside OpenCode's state directory.

‎packages/process-daemon/src/daemon/server.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ fn dispatch(
447447
instance_id: registration.instance_id.clone(),
448448
pid: registration.pid,
449449
protocol: registration.protocol,
450+
features: registration.features.clone(),
450451
},
451452
Request::Create {
452453
program,
@@ -534,6 +535,10 @@ fn dispatch(
534535
cursor_y: rows.cursor_y,
535536
}
536537
}
538+
Request::Rename { id, title } => {
539+
service.rename(id, title)?;
540+
Response::Ok
541+
}
537542
Request::Replay { id, offset } => {
538543
let replay = service.replay(id, offset)?;
539544
Response::Replay {

‎packages/process-daemon/src/daemon/sweep.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ mod tests {
106106
use std::path::PathBuf;
107107

108108
use super::*;
109-
use crate::protocol::PROTOCOL_VERSION;
109+
use crate::protocol::{PROTOCOL_VERSION, features};
110110

111111
fn random_id() -> String {
112112
format!("{:032x}", rand::random::<u128>())
@@ -135,6 +135,7 @@ mod tests {
135135
protocol: PROTOCOL_VERSION,
136136
socket: PathBuf::from("/nonexistent.sock"),
137137
token: random_id(),
138+
features: features(),
138139
};
139140
fs::write(
140141
directory.join(REGISTRATION_FILE),

‎packages/process-daemon/src/daemon/unix.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use fs2::FileExt;
1010
use sha2::{Digest, Sha256};
1111

1212
use super::{LOCK_FILE, Registration, read_registration, registration_path};
13-
use crate::protocol::PROTOCOL_VERSION;
13+
use crate::protocol::{PROTOCOL_VERSION, features};
1414
use crate::transport::Listener;
1515

1616
pub(super) struct Runtime {
@@ -48,6 +48,7 @@ impl Runtime {
4848
protocol: PROTOCOL_VERSION,
4949
socket: socket.path.clone(),
5050
token: random_id(),
51+
features: features(),
5152
};
5253
write_registration(&directory, &registration)?;
5354
let sweeper = {
@@ -364,6 +365,7 @@ mod tests {
364365
protocol: PROTOCOL_VERSION,
365366
socket: socket.clone(),
366367
token: random_id(),
368+
features: features(),
367369
};
368370
write_registration(&abandoned, &registration).unwrap();
369371

‎packages/process-daemon/src/daemon/windows.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use windows_sys::Win32::System::WindowsProgramming::{
2828
};
2929

3030
use super::{LOCK_FILE, REGISTRATION_FILE, Registration, registration_path, sweep};
31-
use crate::protocol::PROTOCOL_VERSION;
31+
use crate::protocol::{PROTOCOL_VERSION, features};
3232
use crate::transport::Listener;
3333
use crate::transport::windows::{owned_handle, security::PrivateSecurity};
3434

@@ -112,6 +112,7 @@ impl Runtime {
112112
pid: std::process::id(),
113113
protocol: PROTOCOL_VERSION,
114114
token: format!("{:032x}", rand::random::<u128>()),
115+
features: features(),
115116
};
116117
let listener = Listener::bind(&registration.socket)?;
117118
write_registration(&directory, &directory_handle, &registration, &security)?;
@@ -364,6 +365,7 @@ mod tests {
364365
protocol: PROTOCOL_VERSION,
365366
socket: PathBuf::from(r"\\.\pipe\opencode-pty-first"),
366367
token: "secret".into(),
368+
features: features(),
367369
};
368370
write_registration(&directory, &directory_handle, &registration, &security).unwrap();
369371
let path = directory.join(REGISTRATION_FILE);

‎packages/process-daemon/src/protocol.rs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ use serde::{Deserialize, Serialize};
77
use crate::service::{TerminalId, TerminalInfo};
88

99
pub const PROTOCOL_VERSION: u32 = 7;
10+
/// Additive capabilities of this daemon. Breaking changes bump `PROTOCOL_VERSION`.
11+
pub const FEATURES: &[&str] = &["rename"];
12+
13+
pub fn features() -> Vec<String> {
14+
FEATURES.iter().map(|feature| feature.to_string()).collect()
15+
}
1016
pub const MAX_FRAME_BYTES: usize = 8 * 1024 * 1024;
1117
const OUTPUT_FRAME_TAG: u8 = 0;
1218

@@ -74,6 +80,10 @@ pub enum Request {
7480
id: TerminalId,
7581
rows: Option<u16>,
7682
},
83+
Rename {
84+
id: TerminalId,
85+
title: Option<String>,
86+
},
7787
Replay {
7888
id: TerminalId,
7989
offset: u64,
@@ -103,6 +113,8 @@ pub enum Response {
103113
instance_id: String,
104114
pid: u32,
105115
protocol: u32,
116+
#[serde(default)]
117+
features: Vec<String>,
106118
},
107119
Created {
108120
terminal: TerminalInfo,
@@ -284,4 +296,43 @@ mod tests {
284296
}
285297
assert_eq!(PROTOCOL_VERSION, 7);
286298
}
299+
300+
#[test]
301+
fn additive_fields_default_when_absent() {
302+
assert!(matches!(
303+
serde_json::from_str::<Response>(
304+
r#"{"type":"pong","instance_id":"a","pid":1,"protocol":7}"#
305+
)
306+
.unwrap(),
307+
Response::Pong { features, .. } if features.is_empty()
308+
));
309+
assert!(matches!(
310+
serde_json::from_str::<Request>(r#"{"op":"rename","id":1,"title":null}"#).unwrap(),
311+
Request::Rename { id: 1, title: None }
312+
));
313+
assert!(matches!(
314+
serde_json::from_str::<Request>(r#"{"op":"rename","id":1}"#).unwrap(),
315+
Request::Rename { id: 1, title: None }
316+
));
317+
let terminal = serde_json::json!({
318+
"id": 1,
319+
"pid": null,
320+
"title": "shell",
321+
"foreground_process": null,
322+
"group_id": "group",
323+
"command": [],
324+
"cwd": "/",
325+
"cols": 80,
326+
"rows": 24,
327+
"lifecycle": {"status": "running"},
328+
"output_head": 0,
329+
"output_tail": 0,
330+
});
331+
let info = serde_json::from_value::<TerminalInfo>(terminal).unwrap();
332+
assert_eq!(info.user_title, None);
333+
assert_eq!(
334+
serde_json::to_value(&info).unwrap()["user_title"],
335+
serde_json::Value::Null
336+
);
337+
}
287338
}

0 commit comments

Comments
 (0)