@@ -4,10 +4,14 @@ use std::path::PathBuf;
44use anyhow:: { Context , Result , bail} ;
55use serde:: { Deserialize , Serialize } ;
66
7- use crate :: service:: { TerminalId , TerminalInfo } ;
7+ use crate :: service:: { ProcessMode , TerminalId , TerminalInfo } ;
88
99pub const PROTOCOL_VERSION : u32 = 7 ;
1010/// Additive capabilities of this daemon. Breaking changes bump `PROTOCOL_VERSION`.
11+ /// `pipe` (non-PTY processes) is Unix-only for now.
12+ #[ cfg( unix) ]
13+ pub const FEATURES : & [ & str ] = & [ "rename" , "pipe" ] ;
14+ #[ cfg( not( unix) ) ]
1115pub const FEATURES : & [ & str ] = & [ "rename" ] ;
1216
1317pub fn features ( ) -> Vec < String > {
@@ -38,17 +42,31 @@ pub enum Request {
3842 ticket : Option < String > ,
3943 } ,
4044 PrepareHandoff ,
45+ /// Old daemons ignore `mode` and would start a PTY, so clients send
46+ /// `"pipe"` only to a daemon that lists the `pipe` feature.
4147 Create {
4248 program : String ,
4349 args : Vec < String > ,
4450 cwd : PathBuf ,
51+ #[ serde( default ) ]
4552 title : String ,
4653 group_id : String ,
4754 env : std:: collections:: HashMap < String , String > ,
55+ /// Terminal size; required for `pty`, ignored for `pipe`.
56+ #[ serde( default ) ]
4857 cols : u16 ,
58+ #[ serde( default ) ]
4959 rows : u16 ,
60+ #[ serde( default , skip_serializing_if = "ProcessMode::is_pty" ) ]
61+ mode : ProcessMode ,
62+ } ,
63+ /// Without `mode`, lists PTYs only, so older servers never see pipe processes.
64+ /// `mode` is left out of the JSON when unset: older daemons reject a `list`
65+ /// with any extra key.
66+ List {
67+ #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
68+ mode : Option < ProcessMode > ,
5069 } ,
51- List ,
5270 Write {
5371 id : TerminalId ,
5472 attachment_id : Option < String > ,
@@ -161,6 +179,9 @@ pub enum Response {
161179 } ,
162180 Exited {
163181 exit_code : Option < u32 > ,
182+ /// Signal name such as `SIGTERM` when a pipe process was killed by one.
183+ #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
184+ signal : Option < String > ,
164185 final_offset : u64 ,
165186 } ,
166187 ControllerChanged {
@@ -335,4 +356,82 @@ mod tests {
335356 serde_json:: Value :: Null
336357 ) ;
337358 }
359+
360+ #[ test]
361+ fn pipe_fields_keep_the_wire_compatible_with_older_peers ( ) {
362+ use crate :: service:: { ProcessMode , TerminalLifecycle } ;
363+
364+ // An older daemon rejects `list` with any extra key, and an older
365+ // client sends a bare `list` and a `create` with no mode.
366+ assert_eq ! (
367+ serde_json:: to_value( Request :: List { mode: None } ) . unwrap( ) ,
368+ serde_json:: json!( { "op" : "list" } )
369+ ) ;
370+ assert ! ( matches!(
371+ serde_json:: from_str:: <Request >( r#"{"op":"list"}"# ) . unwrap( ) ,
372+ Request :: List { mode: None }
373+ ) ) ;
374+ assert ! ( matches!(
375+ serde_json:: from_str:: <Request >( r#"{"op":"list","mode":"pipe"}"# ) . unwrap( ) ,
376+ Request :: List {
377+ mode: Some ( ProcessMode :: Pipe )
378+ }
379+ ) ) ;
380+ let create = serde_json:: json!( {
381+ "op" : "create" , "program" : "sh" , "args" : [ ] , "cwd" : "/" ,
382+ "title" : "t" , "group_id" : "g" , "env" : { } , "cols" : 80 , "rows" : 24 ,
383+ } ) ;
384+ assert ! ( matches!(
385+ serde_json:: from_value:: <Request >( create. clone( ) ) . unwrap( ) ,
386+ Request :: Create {
387+ mode: ProcessMode :: Pty ,
388+ cols: 80 ,
389+ ..
390+ }
391+ ) ) ;
392+ // A PTY create serializes exactly as before.
393+ assert_eq ! (
394+ serde_json:: to_value( serde_json:: from_value:: <Request >( create. clone( ) ) . unwrap( ) )
395+ . unwrap( ) ,
396+ create
397+ ) ;
398+ assert ! ( matches!(
399+ serde_json:: from_str:: <Request >(
400+ r#"{"op":"create","program":"sh","args":[],"cwd":"/","group_id":"g","env":{},"mode":"pipe"}"#
401+ )
402+ . unwrap( ) ,
403+ Request :: Create {
404+ mode: ProcessMode :: Pipe ,
405+ cols: 0 ,
406+ rows: 0 ,
407+ ..
408+ }
409+ ) ) ;
410+ // Exits without a signal look exactly as before.
411+ assert_eq ! (
412+ serde_json:: to_value( Response :: Exited {
413+ exit_code: Some ( 1 ) ,
414+ signal: None ,
415+ final_offset: 2 ,
416+ } )
417+ . unwrap( ) ,
418+ serde_json:: json!( { "type" : "exited" , "exit_code" : 1 , "final_offset" : 2 } )
419+ ) ;
420+ assert_eq ! (
421+ serde_json:: to_value( TerminalLifecycle :: Exited {
422+ exit_code: Some ( 1 ) ,
423+ signal: None ,
424+ } )
425+ . unwrap( ) ,
426+ serde_json:: json!( { "status" : "exited" , "exit_code" : 1 } )
427+ ) ;
428+ assert_eq ! (
429+ serde_json:: from_str:: <TerminalLifecycle >( r#"{"status":"exited","exit_code":null}"# )
430+ . unwrap( ) ,
431+ TerminalLifecycle :: Exited {
432+ exit_code: None ,
433+ signal: None ,
434+ }
435+ ) ;
436+ }
338437}
0 commit comments