44use super :: * ;
55use crate :: service:: workspace:: { WorkspaceInfo , WorkspaceKind } ;
66use openbitfun_product_domains:: session_import:: ExternalSessionCatalog ;
7+ #[ cfg( test) ]
8+ use openbitfun_product_domains:: session_import:: ExternalSessionImportWarning ;
9+ use openbitfun_services_core:: session:: SessionDiscoveryDiagnostic ;
710
811#[ derive( Default ) ]
912struct DiscoveryState {
@@ -18,6 +21,13 @@ struct DiscoveryState {
1821pub struct DiscoverySnapshot {
1922 pub pending : bool ,
2023 pub warnings : Vec < String > ,
24+ pub diagnostics : Vec < SessionDiscoveryDiagnostic > ,
25+ }
26+
27+ #[ derive( Debug , Default ) ]
28+ struct DiscoveryResult {
29+ warnings : Vec < String > ,
30+ diagnostics : Vec < SessionDiscoveryDiagnostic > ,
2131}
2232
2333type DiscoveryLocks = HashMap < String , Arc < tokio:: sync:: Mutex < DiscoveryState > > > ;
@@ -57,6 +67,7 @@ pub async fn discover_workspace(workspace: &WorkspaceInfo, refresh: bool) -> Dis
5767 return DiscoverySnapshot {
5868 pending : false ,
5969 warnings : vec ! [ "External session discovery preferences could not be read" . into( ) ] ,
70+ diagnostics : Vec :: new ( ) ,
6071 } ;
6172 }
6273 } ;
@@ -69,6 +80,7 @@ pub async fn discover_workspace(workspace: &WorkspaceInfo, refresh: bool) -> Dis
6980 warnings : vec ! [
7081 "Automatic external session discovery is unavailable for SSH workspaces" . into( ) ,
7182 ] ,
83+ diagnostics : Vec :: new ( ) ,
7284 }
7385 } ;
7486 }
@@ -98,7 +110,7 @@ async fn schedule_discovery<F, Fut>(
98110) -> DiscoverySnapshot
99111where
100112 F : FnOnce ( Arc < AtomicBool > ) -> Fut + Send + ' static ,
101- Fut : std:: future:: Future < Output = Vec < String > > + Send + ' static ,
113+ Fut : std:: future:: Future < Output = DiscoveryResult > + Send + ' static ,
102114{
103115 let lock = DISCOVERY
104116 . lock ( )
@@ -128,6 +140,7 @@ where
128140 state. snapshot = DiscoverySnapshot {
129141 pending : true ,
130142 warnings : Vec :: new ( ) ,
143+ diagnostics : Vec :: new ( ) ,
131144 } ;
132145 let worker = state. worker . clone ( ) ;
133146 let completion_state = lock. clone ( ) ;
@@ -138,12 +151,13 @@ where
138151 if cancelled. load ( Ordering :: Acquire ) {
139152 return ;
140153 }
141- let warnings = work ( cancelled. clone ( ) ) . await ;
154+ let result = work ( cancelled. clone ( ) ) . await ;
142155 let mut state = completion_state. lock ( ) . await ;
143156 if Arc :: ptr_eq ( & state. cancelled , & cancelled) && !cancelled. load ( Ordering :: Acquire ) {
144157 state. snapshot = DiscoverySnapshot {
145158 pending : false ,
146- warnings,
159+ warnings : result. warnings ,
160+ diagnostics : result. diagnostics ,
147161 } ;
148162 }
149163 } ) ;
@@ -165,9 +179,18 @@ pub async fn discover_for_storage_path(storage_path: &Path) -> DiscoverySnapshot
165179 let discovery = discover_workspace ( workspace, false ) . await ;
166180 snapshot. pending |= discovery. pending ;
167181 snapshot. warnings . extend ( discovery. warnings ) ;
182+ snapshot. diagnostics . extend ( discovery. diagnostics ) ;
168183 }
169184 snapshot. warnings . sort ( ) ;
170185 snapshot. warnings . dedup ( ) ;
186+ snapshot. diagnostics . sort_by ( |left, right| {
187+ left. ecosystem_id
188+ . cmp ( & right. ecosystem_id )
189+ . then_with ( || left. code . cmp ( & right. code ) )
190+ } ) ;
191+ snapshot
192+ . diagnostics
193+ . dedup_by ( |left, right| left. ecosystem_id == right. ecosystem_id && left. code == right. code ) ;
171194 snapshot
172195}
173196
@@ -205,27 +228,46 @@ async fn discover_enabled(
205228 enabled : & [ String ] ,
206229 cancelled : Arc < AtomicBool > ,
207230 requested_at : Instant ,
208- ) -> Vec < String > {
231+ ) -> DiscoveryResult {
209232 let Some ( coordinator) = crate :: agentic:: coordination:: get_global_coordinator ( ) else {
210- return vec ! [ "Runtime ownership is unavailable for external session discovery" . into( ) ] ;
233+ return DiscoveryResult {
234+ warnings : vec ! [
235+ "Runtime ownership is unavailable for external session discovery" . into( ) ,
236+ ] ,
237+ ..DiscoveryResult :: default ( )
238+ } ;
211239 } ;
212240 if let Err ( error) =
213241 coordinator. ensure_workspace_runtime_ownership ( & workspace. root_path , None , None )
214242 {
215243 log:: warn!( "External session discovery could not acquire workspace ownership: {error}" ) ;
216- return vec ! [
217- "Another runtime owns this workspace; automatic session import is unavailable" . into( ) ,
218- ] ;
244+ return DiscoveryResult {
245+ warnings : vec ! [
246+ "Another runtime owns this workspace; automatic session import is unavailable"
247+ . into( ) ,
248+ ] ,
249+ ..DiscoveryResult :: default ( )
250+ } ;
219251 }
220252 let Some ( service) = crate :: service:: workspace:: get_global_workspace_service ( ) else {
221- return vec ! [ "Workspace service is unavailable for external session discovery" . into( ) ] ;
253+ return DiscoveryResult {
254+ warnings : vec ! [
255+ "Workspace service is unavailable for external session discovery" . into( ) ,
256+ ] ,
257+ ..DiscoveryResult :: default ( )
258+ } ;
222259 } ;
223260 let workspaces = service. list_workspace_infos ( ) . await ;
224261 let ( store, project) = match import_storage ( workspace) . await {
225262 Ok ( storage) => storage,
226- Err ( error) => return vec ! [ error. detail] ,
263+ Err ( error) => {
264+ return DiscoveryResult {
265+ warnings : vec ! [ error. detail] ,
266+ ..DiscoveryResult :: default ( )
267+ }
268+ }
227269 } ;
228- let mut warnings = Vec :: new ( ) ;
270+ let mut result = DiscoveryResult :: default ( ) ;
229271 for ecosystem in enabled {
230272 if cancelled. load ( Ordering :: Acquire ) {
231273 break ;
@@ -240,28 +282,28 @@ async fn discover_enabled(
240282 . await ;
241283 match catalog {
242284 Ok ( catalog) => {
243- warnings . extend (
244- register_catalog (
245- & store ,
246- workspace ,
247- & project ,
248- & workspaces ,
249- & catalog ,
250- cancelled . as_ref ( ) ,
251- )
252- . await ,
253- ) ;
285+ let registration = register_catalog (
286+ & store ,
287+ workspace ,
288+ & project ,
289+ & workspaces ,
290+ & catalog ,
291+ cancelled . as_ref ( ) ,
292+ )
293+ . await ;
294+ result . warnings . extend ( registration . warnings ) ;
295+ result . diagnostics . extend ( registration . diagnostics ) ;
254296 }
255297 error => {
256298 log:: warn!( "External session discovery failed for {ecosystem}: {error:?}" ) ;
257- warnings. push ( format ! (
299+ result . warnings . push ( format ! (
258300 "{} session history could not be discovered" ,
259301 source. name( )
260302 ) ) ;
261303 }
262304 }
263305 }
264- warnings
306+ result
265307}
266308
267309#[ derive( Debug ) ]
@@ -395,25 +437,55 @@ async fn register_catalog(
395437 workspaces : & [ WorkspaceInfo ] ,
396438 catalog : & SourceCatalog ,
397439 cancelled : & AtomicBool ,
398- ) -> Vec < String > {
440+ ) -> DiscoveryResult {
399441 if cancelled. load ( Ordering :: Acquire ) {
400- return Vec :: new ( ) ;
442+ return DiscoveryResult :: default ( ) ;
401443 }
402- let mut warnings = Vec :: new ( ) ;
403- if !catalog. catalog . diagnostics . is_empty ( ) {
404- warnings. push ( format ! (
444+ let mut result = DiscoveryResult :: default ( ) ;
445+ let has_workspace_unresolved = catalog
446+ . catalog
447+ . diagnostics
448+ . iter ( )
449+ . any ( |diagnostic| diagnostic. code == "workspace_unresolved" ) ;
450+ let has_read_failure = catalog
451+ . catalog
452+ . diagnostics
453+ . iter ( )
454+ . any ( |diagnostic| diagnostic. code != "workspace_unresolved" ) ;
455+ if has_read_failure {
456+ result. warnings . push ( format ! (
405457 "Some {} source records could not be read" ,
406458 catalog. catalog. ecosystem_id
407459 ) ) ;
408460 }
461+ if has_workspace_unresolved {
462+ result. warnings . push ( format ! (
463+ "Some {} sessions could not be matched to a workspace" ,
464+ catalog. catalog. ecosystem_id
465+ ) ) ;
466+ }
467+ result
468+ . diagnostics
469+ . extend (
470+ catalog
471+ . catalog
472+ . diagnostics
473+ . iter ( )
474+ . map ( |diagnostic| SessionDiscoveryDiagnostic {
475+ ecosystem_id : catalog. catalog . ecosystem_id . clone ( ) ,
476+ code : diagnostic. code . clone ( ) ,
477+ } ) ,
478+ ) ;
409479 let candidates = catalog. workspace_candidates ( workspace, workspaces, normalized_path) ;
410480 if let Err ( error) =
411481 super :: deferred:: register ( store. sessions_root ( ) , workspace, project, candidates) . await
412482 {
413483 log:: warn!( "External session index could not be updated: {error}" ) ;
414- warnings. push ( "External session discovery index could not be updated" . into ( ) ) ;
484+ result
485+ . warnings
486+ . push ( "External session discovery index could not be updated" . into ( ) ) ;
415487 }
416- warnings
488+ result
417489}
418490
419491pub ( super ) fn belongs_to_workspace (
@@ -464,7 +536,10 @@ pub(super) mod tests {
464536 None ,
465537 |_| async move {
466538 wait. await . unwrap ( ) ;
467- vec ! [ "fixture warning" . into( ) ]
539+ DiscoveryResult {
540+ warnings : vec ! [ "fixture warning" . into( ) ] ,
541+ ..DiscoveryResult :: default ( )
542+ }
468543 } ,
469544 )
470545 . await ;
@@ -510,7 +585,10 @@ pub(super) mod tests {
510585 tokio:: task:: yield_now ( ) . await ;
511586 }
512587 stopped. send ( ( ) ) . unwrap ( ) ;
513- vec ! [ "must not become visible" . into( ) ]
588+ DiscoveryResult {
589+ warnings : vec ! [ "must not become visible" . into( ) ] ,
590+ ..DiscoveryResult :: default ( )
591+ }
514592 } ,
515593 )
516594 . await ;
@@ -576,6 +654,52 @@ pub(super) mod tests {
576654 }
577655 }
578656
657+ #[ tokio:: test]
658+ async fn catalog_diagnostics_keep_read_and_workspace_failures_separate ( ) {
659+ let temp = tempfile:: tempdir ( ) . unwrap ( ) ;
660+ let workspace = workspace ( "project" , temp. path ( ) , WorkspaceKind :: Normal ) ;
661+ let mut source_catalog = catalog ( "cursor" , vec ! [ ] ) ;
662+ source_catalog. diagnostics = vec ! [
663+ ExternalSessionImportWarning {
664+ code: "source_unreadable" . into( ) ,
665+ detail: "fixture source could not be read" . into( ) ,
666+ } ,
667+ ExternalSessionImportWarning {
668+ code: "workspace_unresolved" . into( ) ,
669+ detail: "fixture source has no workspace" . into( ) ,
670+ } ,
671+ ] ;
672+
673+ let result = register_catalog (
674+ & OfflineSessionImportStore :: new ( temp. path ( ) . join ( "native" ) ) ,
675+ & workspace,
676+ & workspace,
677+ std:: slice:: from_ref ( & workspace) ,
678+ & SourceCatalog :: new ( source_catalog) ,
679+ & AtomicBool :: new ( false ) ,
680+ )
681+ . await ;
682+
683+ assert_eq ! (
684+ result. warnings,
685+ vec![
686+ "Some cursor source records could not be read" ,
687+ "Some cursor sessions could not be matched to a workspace" ,
688+ ]
689+ ) ;
690+ assert_eq ! (
691+ result
692+ . diagnostics
693+ . iter( )
694+ . map( |diagnostic| ( diagnostic. ecosystem_id. as_str( ) , diagnostic. code. as_str( ) ) )
695+ . collect:: <Vec <_>>( ) ,
696+ vec![
697+ ( "cursor" , "source_unreadable" ) ,
698+ ( "cursor" , "workspace_unresolved" ) ,
699+ ]
700+ ) ;
701+ }
702+
579703 #[ tokio:: test]
580704 async fn centralized_metadata_storage_discovers_all_codex_history_beyond_existing_imports ( ) {
581705 let temp = tempfile:: tempdir ( ) . unwrap ( ) ;
@@ -646,7 +770,10 @@ pub(super) mod tests {
646770 & AtomicBool :: new ( false ) ,
647771 )
648772 . await ;
649- assert ! ( warnings. is_empty( ) , "{warnings:?}" ) ;
773+ assert ! (
774+ warnings. warnings. is_empty( ) && warnings. diagnostics. is_empty( ) ,
775+ "{warnings:?}"
776+ ) ;
650777 let native = store. list_metadata_including_internal ( ) . await . unwrap ( ) ;
651778 assert_eq ! (
652779 native. len( ) ,
@@ -710,7 +837,7 @@ pub(super) mod tests {
710837 vec![ "codex" . into( ) ] ,
711838 false ,
712839 Some ( activation) ,
713- |_| async { Vec :: new ( ) }
840+ |_| async { DiscoveryResult :: default ( ) }
714841 )
715842 . await
716843 . pending
@@ -772,7 +899,7 @@ pub(super) mod tests {
772899 & AtomicBool :: new ( true ) ,
773900 )
774901 . await ;
775- assert ! ( warnings. is_empty( ) ) ;
902+ assert ! ( warnings. warnings . is_empty ( ) && warnings . diagnostics . is_empty( ) ) ;
776903 assert ! ( !root. exists( ) ) ;
777904 }
778905
0 commit comments