11use std:: { ffi:: OsStr , net:: TcpListener } ;
22
3- use crate :: services:: ServiceKind ;
3+ use crate :: services:: { ServiceKind , SERVICE_KINDS } ;
44
5- /// A native window keeps one endpoint pair across runtime repairs. Normal
5+ /// Desktop workspace UI surface. Workbench is the default; LegacyDashboard is an
6+ /// explicit opt-in escape hatch (`LOOPX_DESKTOP_SURFACE=legacy|dashboard`) while
7+ /// remaining legacy dashboard panels are absorbed and retired.
8+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
9+ pub ( crate ) enum WorkspaceSurface {
10+ Workbench ,
11+ LegacyDashboard ,
12+ }
13+
14+ impl WorkspaceSurface {
15+ pub fn from_env ( value : Option < & OsStr > ) -> std:: io:: Result < Self > {
16+ let Some ( raw) = value else {
17+ return Ok ( Self :: Workbench ) ;
18+ } ;
19+ let normalized = raw. to_string_lossy ( ) . trim ( ) . to_ascii_lowercase ( ) ;
20+ match normalized. as_str ( ) {
21+ "" | "workbench" => Ok ( Self :: Workbench ) ,
22+ "legacy" | "dashboard" => Ok ( Self :: LegacyDashboard ) ,
23+ _ => Err ( std:: io:: Error :: new (
24+ std:: io:: ErrorKind :: InvalidInput ,
25+ "LOOPX_DESKTOP_SURFACE must be workbench, legacy, or dashboard" ,
26+ ) ) ,
27+ }
28+ }
29+ }
30+
31+ /// A native window keeps one endpoint set across runtime repairs. Normal
632/// windows share the machine services; explicit private endpoints are opt-in.
733#[ derive( Clone , Copy ) ]
834pub ( crate ) struct ServiceEndpoints {
935 status : u16 ,
1036 chat : u16 ,
37+ workbench : u16 ,
1138 pub isolated : bool ,
39+ pub surface : WorkspaceSurface ,
1240}
1341
1442impl ServiceEndpoints {
43+ #[ cfg( test) ]
1544 pub fn for_window ( private_endpoints : Option < & OsStr > ) -> std:: io:: Result < Self > {
45+ Self :: for_window_with_surface ( private_endpoints, None )
46+ }
47+
48+ pub fn for_window_with_surface (
49+ private_endpoints : Option < & OsStr > ,
50+ surface_override : Option < & OsStr > ,
51+ ) -> std:: io:: Result < Self > {
1652 let isolated = match private_endpoints {
1753 None => false ,
1854 Some ( value) if value == "0" => false ,
@@ -24,25 +60,39 @@ impl ServiceEndpoints {
2460 ) )
2561 }
2662 } ;
27- Self :: allocate ( isolated)
63+ let surface = WorkspaceSurface :: from_env ( surface_override) ?;
64+ Self :: allocate_for_surface ( isolated, surface)
2865 }
2966
67+ #[ cfg( test) ]
3068 pub fn allocate ( isolated : bool ) -> std:: io:: Result < Self > {
69+ Self :: allocate_for_surface ( isolated, WorkspaceSurface :: Workbench )
70+ }
71+
72+ pub fn allocate_for_surface (
73+ isolated : bool ,
74+ surface : WorkspaceSurface ,
75+ ) -> std:: io:: Result < Self > {
3176 if !isolated {
3277 return Ok ( Self {
3378 status : 8766 ,
3479 chat : 8767 ,
80+ workbench : 8796 ,
3581 isolated,
82+ surface,
3683 } ) ;
3784 }
38- // Keep both reservations until their distinct ports have been chosen.
85+ // Keep all reservations until distinct ports have been chosen.
3986 // The CLI owns binding; an intervening listener fails closed at start.
4087 let status = reserve_private_endpoint ( ) ?;
4188 let chat = reserve_private_endpoint ( ) ?;
89+ let workbench = reserve_private_endpoint ( ) ?;
4290 Ok ( Self {
4391 status : status. local_addr ( ) ?. port ( ) ,
4492 chat : chat. local_addr ( ) ?. port ( ) ,
93+ workbench : workbench. local_addr ( ) ?. port ( ) ,
4594 isolated,
95+ surface,
4696 } )
4797 }
4898
@@ -64,10 +114,13 @@ impl ServiceEndpoints {
64114 } ;
65115 let status = reserve ( ) ?;
66116 let chat = reserve ( ) ?;
117+ let workbench = reserve ( ) ?;
67118 Ok ( Self {
68119 status : status. local_addr ( ) ?. port ( ) ,
69120 chat : chat. local_addr ( ) ?. port ( ) ,
121+ workbench : workbench. local_addr ( ) ?. port ( ) ,
70122 isolated : true ,
123+ surface : WorkspaceSurface :: LegacyDashboard ,
71124 } )
72125 }
73126
@@ -78,17 +131,32 @@ impl ServiceEndpoints {
78131 }
79132 }
80133
134+ pub fn workbench_port ( self ) -> u16 {
135+ self . workbench
136+ }
137+
138+ pub fn required_services ( self ) -> & ' static [ ServiceKind ] {
139+ const WORKBENCH_CORE_ONLY : [ ServiceKind ; 1 ] = [ ServiceKind :: Chat ] ;
140+ match self . surface {
141+ WorkspaceSurface :: Workbench => & WORKBENCH_CORE_ONLY ,
142+ WorkspaceSurface :: LegacyDashboard => & SERVICE_KINDS ,
143+ }
144+ }
145+
81146 #[ cfg( any( not( dev) , test) ) ]
82147 pub fn workspace_origin ( self ) -> String {
83- format ! ( "http://127.0.0.1:{}/chat/" , self . chat)
148+ match self . surface {
149+ WorkspaceSurface :: Workbench => format ! ( "http://127.0.0.1:{}/" , self . workbench) ,
150+ WorkspaceSurface :: LegacyDashboard => format ! ( "http://127.0.0.1:{}/chat/" , self . chat) ,
151+ }
84152 }
85153}
86154
87155fn reserve_private_endpoint ( ) -> std:: io:: Result < TcpListener > {
88156 // A host may customize its ephemeral range to include the CLI ports.
89157 for _ in 0 ..8 {
90158 let listener = TcpListener :: bind ( ( "127.0.0.1" , 0 ) ) ?;
91- if ![ 8766 , 8767 ] . contains ( & listener. local_addr ( ) ?. port ( ) ) {
159+ if ![ 8766 , 8767 , 8796 ] . contains ( & listener. local_addr ( ) ?. port ( ) ) {
92160 return Ok ( listener) ;
93161 }
94162 }
@@ -103,11 +171,15 @@ mod tests {
103171 use super :: * ;
104172
105173 #[ test]
106- fn ordinary_window_uses_the_same_machine_service_addresses ( ) {
174+ fn ordinary_window_defaults_to_workbench_and_shares_machine_service_addresses ( ) {
107175 for choice in [ None , Some ( OsStr :: new ( "0" ) ) ] {
108176 let endpoints = ServiceEndpoints :: for_window ( choice) . unwrap ( ) ;
177+ assert_eq ! ( endpoints. surface, WorkspaceSurface :: Workbench ) ;
109178 assert_eq ! ( endpoints. port( ServiceKind :: Status ) , 8766 ) ;
110179 assert_eq ! ( endpoints. port( ServiceKind :: Chat ) , 8767 ) ;
180+ assert_eq ! ( endpoints. workbench_port( ) , 8796 ) ;
181+ assert_eq ! ( endpoints. required_services( ) , & [ ServiceKind :: Chat ] ) ;
182+ assert_eq ! ( endpoints. workspace_origin( ) , "http://127.0.0.1:8796/" ) ;
111183 assert ! ( !endpoints. isolated) ;
112184 }
113185 assert ! (
@@ -117,6 +189,23 @@ mod tests {
117189 ) ;
118190 }
119191
192+ #[ test]
193+ fn explicit_legacy_surface_preserves_dashboard_origin_and_dual_services ( ) {
194+ for choice in [ "legacy" , "dashboard" , "LEGACY" ] {
195+ let endpoints =
196+ ServiceEndpoints :: for_window_with_surface ( None , Some ( OsStr :: new ( choice) ) ) . unwrap ( ) ;
197+ assert_eq ! ( endpoints. surface, WorkspaceSurface :: LegacyDashboard ) ;
198+ assert_eq ! (
199+ endpoints. required_services( ) ,
200+ & [ ServiceKind :: Status , ServiceKind :: Chat ]
201+ ) ;
202+ assert_eq ! ( endpoints. workspace_origin( ) , "http://127.0.0.1:8767/chat/" ) ;
203+ }
204+ let invalid =
205+ ServiceEndpoints :: for_window_with_surface ( None , Some ( OsStr :: new ( "unknown" ) ) ) . err ( ) ;
206+ assert_eq ! ( invalid. unwrap( ) . kind( ) , std:: io:: ErrorKind :: InvalidInput ) ;
207+ }
208+
120209 #[ test]
121210 fn invalid_private_endpoint_choice_cannot_fall_through_to_machine_state ( ) {
122211 for choice in [ "" , "true" , "yes" , "2" ] {
@@ -135,6 +224,7 @@ mod tests {
135224 let endpoints = ServiceEndpoints :: allocate ( false ) . unwrap ( ) ;
136225 assert_eq ! ( endpoints. port( ServiceKind :: Status ) , 8766 ) ;
137226 assert_eq ! ( endpoints. port( ServiceKind :: Chat ) , 8767 ) ;
227+ assert_eq ! ( endpoints. workbench_port( ) , 8796 ) ;
138228 assert ! ( !endpoints. isolated) ;
139229 }
140230
@@ -143,11 +233,21 @@ mod tests {
143233 let endpoints = ServiceEndpoints :: allocate ( true ) . unwrap ( ) ;
144234 assert ! ( endpoints. isolated) ;
145235 assert_ne ! ( endpoints. status, endpoints. chat) ;
146- assert ! ( ![ 8766 , 8767 ] . contains( & endpoints. status) ) ;
147- assert ! ( ![ 8766 , 8767 ] . contains( & endpoints. chat) ) ;
236+ assert_ne ! ( endpoints. chat, endpoints. workbench) ;
237+ assert_ne ! ( endpoints. status, endpoints. workbench) ;
238+ assert ! ( ![ 8766 , 8767 , 8796 ] . contains( & endpoints. status) ) ;
239+ assert ! ( ![ 8766 , 8767 , 8796 ] . contains( & endpoints. chat) ) ;
240+ assert ! ( ![ 8766 , 8767 , 8796 ] . contains( & endpoints. workbench) ) ;
148241 assert_eq ! (
149242 endpoints. workspace_origin( ) ,
150- format!( "http://127.0.0.1:{}/chat/" , endpoints. chat)
243+ format!( "http://127.0.0.1:{}/" , endpoints. workbench)
244+ ) ;
245+ let legacy =
246+ ServiceEndpoints :: allocate_for_surface ( true , WorkspaceSurface :: LegacyDashboard )
247+ . unwrap ( ) ;
248+ assert_eq ! (
249+ legacy. workspace_origin( ) ,
250+ format!( "http://127.0.0.1:{}/chat/" , legacy. chat)
151251 ) ;
152252 }
153253}
0 commit comments