@@ -420,6 +420,26 @@ fn apply_discovered_provider(form: &mut CreateProviderForm, discovered: Discover
420420// Provider detail view (Get)
421421// ---------------------------------------------------------------------------
422422
423+ /// Availability of strictly serialized provider profile YAML in the detail view.
424+ pub enum ProviderProfileYaml {
425+ /// The provider has no associated profile.
426+ Absent ,
427+ /// The profile serialized successfully.
428+ Valid ( String ) ,
429+ /// A profile exists, but its YAML serialization failed.
430+ Invalid ,
431+ }
432+
433+ impl ProviderProfileYaml {
434+ /// Return YAML only when strict profile serialization succeeded.
435+ pub fn yaml ( & self ) -> Option < & str > {
436+ match self {
437+ Self :: Valid ( yaml) => Some ( yaml) ,
438+ Self :: Absent | Self :: Invalid => None ,
439+ }
440+ }
441+ }
442+
423443pub struct ProviderDetailView {
424444 pub name : String ,
425445 pub provider_id : String ,
@@ -430,7 +450,7 @@ pub struct ProviderDetailView {
430450 pub show_raw_provider : bool ,
431451 pub raw_profile_scroll : usize ,
432452 pub raw_provider_scroll : usize ,
433- pub raw_profile_yaml : Option < String > ,
453+ pub raw_profile_yaml : ProviderProfileYaml ,
434454 pub raw_provider_yaml : String ,
435455 pub profile_name : Option < String > ,
436456 pub profile_category : Option < String > ,
@@ -2933,7 +2953,7 @@ impl App {
29332953 KeyCode :: Esc | KeyCode :: Enter => {
29342954 self . provider_detail = None ;
29352955 }
2936- KeyCode :: Char ( 'y' ) if detail. raw_profile_yaml . is_some ( ) => {
2956+ KeyCode :: Char ( 'y' ) if detail. raw_profile_yaml . yaml ( ) . is_some ( ) => {
29372957 detail. show_raw_profile = !detail. show_raw_profile ;
29382958 detail. show_raw_provider = false ;
29392959 detail. raw_profile_scroll = 0 ;
@@ -2946,7 +2966,7 @@ impl App {
29462966 KeyCode :: Char ( 'j' ) | KeyCode :: Down if detail. show_raw_profile => {
29472967 let max_scroll = detail
29482968 . raw_profile_yaml
2949- . as_ref ( )
2969+ . yaml ( )
29502970 . map_or ( 0 , |raw| raw. lines ( ) . count ( ) . saturating_sub ( 1 ) ) ;
29512971 detail. raw_profile_scroll = ( detail. raw_profile_scroll + 1 ) . min ( max_scroll) ;
29522972 }
@@ -3441,9 +3461,12 @@ impl App {
34413461 } ,
34423462 ) ;
34433463
3444- let raw_profile_yaml = profile. and_then ( |profile| {
3464+ let raw_profile_yaml = profile. map_or ( ProviderProfileYaml :: Absent , |profile| {
34453465 let dto = ProviderTypeProfile :: from_proto ( profile) ;
3446- openshell_providers:: profile_to_yaml ( & dto) . ok ( )
3466+ // Serializer errors may echo authored values. Keep the failure
3467+ // distinct from absence without retaining potentially secret text.
3468+ openshell_providers:: profile_to_yaml ( & dto)
3469+ . map_or ( ProviderProfileYaml :: Invalid , ProviderProfileYaml :: Valid )
34473470 } ) ;
34483471
34493472 ProviderDetailView {
@@ -3660,6 +3683,201 @@ mod tests {
36603683 KeyEvent :: new ( code, KeyModifiers :: NONE )
36613684 }
36623685
3686+ fn detail_mcp_profile ( versions : & [ & str ] ) -> openshell_core:: proto:: ProviderProfile {
3687+ openshell_core:: proto:: ProviderProfile {
3688+ id : "mcp-example" . to_string ( ) ,
3689+ display_name : "MCP Example" . to_string ( ) ,
3690+ endpoints : vec ! [ openshell_core:: proto:: NetworkEndpoint {
3691+ host: "mcp.example.com" . to_string( ) ,
3692+ port: 443 ,
3693+ protocol: "mcp" . to_string( ) ,
3694+ mcp: Some ( openshell_core:: proto:: McpOptions {
3695+ versions: versions. iter( ) . map( ToString :: to_string) . collect( ) ,
3696+ ..Default :: default ( )
3697+ } ) ,
3698+ ..Default :: default ( )
3699+ } ] ,
3700+ ..Default :: default ( )
3701+ }
3702+ }
3703+
3704+ fn detail_app ( profile : Option < openshell_core:: proto:: ProviderProfile > ) -> App {
3705+ let mut app = test_app ( ) ;
3706+ let provider = openshell_core:: proto:: Provider {
3707+ metadata : Some ( openshell_core:: proto:: ObjectMeta {
3708+ id : "provider-id" . to_string ( ) ,
3709+ name : "example-provider" . to_string ( ) ,
3710+ ..Default :: default ( )
3711+ } ) ,
3712+ r#type : "mcp-example" . to_string ( ) ,
3713+ credentials : HashMap :: from ( [ ( "API_KEY" . to_string ( ) , "test-only-secret" . to_string ( ) ) ] ) ,
3714+ ..Default :: default ( )
3715+ } ;
3716+ app. provider_entries . push ( ProviderListEntry {
3717+ provider : provider. clone ( ) ,
3718+ profile,
3719+ } ) ;
3720+ app. provider_detail = Some ( app. provider_detail_from_provider ( & provider) ) ;
3721+ app
3722+ }
3723+
3724+ fn render_provider_detail ( app : & App ) -> String {
3725+ let mut terminal = ratatui:: Terminal :: new ( ratatui:: backend:: TestBackend :: new ( 100 , 36 ) )
3726+ . expect ( "test terminal" ) ;
3727+ terminal
3728+ . draw ( |frame| crate :: ui:: create_provider:: draw_detail ( frame, app, frame. size ( ) ) )
3729+ . expect ( "provider detail renders" ) ;
3730+ terminal
3731+ . backend ( )
3732+ . buffer ( )
3733+ . content ( )
3734+ . iter ( )
3735+ . map ( ratatui:: buffer:: Cell :: symbol)
3736+ . collect ( )
3737+ }
3738+
3739+ #[ tokio:: test]
3740+ async fn provider_detail_absent_profile_keeps_object_yaml_available ( ) {
3741+ let mut app = detail_app ( None ) ;
3742+ let detail = app. provider_detail . as_ref ( ) . expect ( "detail view" ) ;
3743+ assert ! ( matches!(
3744+ detail. raw_profile_yaml,
3745+ ProviderProfileYaml :: Absent
3746+ ) ) ;
3747+ let text = render_provider_detail ( & app) ;
3748+ assert ! ( text. contains( "Profile: <none> (legacy/unprofiled provider)" ) ) ;
3749+ assert ! ( !text. contains( "Profile YAML unavailable" ) ) ;
3750+ assert ! ( !text. contains( "[y]" ) ) ;
3751+
3752+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'y' ) ) ) ;
3753+ assert ! (
3754+ !app. provider_detail
3755+ . as_ref( )
3756+ . expect( "detail view" )
3757+ . show_raw_profile
3758+ ) ;
3759+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'o' ) ) ) ;
3760+ let text = render_provider_detail ( & app) ;
3761+ assert ! ( text. contains( "Provider Object YAML" ) ) ;
3762+ assert ! ( text. contains( "<redacted>" ) ) ;
3763+ assert ! ( !text. contains( "test-only-secret" ) ) ;
3764+ }
3765+
3766+ #[ tokio:: test]
3767+ async fn provider_detail_valid_profile_preserves_yaml_navigation ( ) {
3768+ let profile = detail_mcp_profile ( & [ "2025-11-25" ] ) ;
3769+ let expected_yaml =
3770+ openshell_providers:: profile_to_yaml ( & ProviderTypeProfile :: from_proto ( & profile) )
3771+ . expect ( "valid profile YAML" ) ;
3772+ let mut app = detail_app ( Some ( profile) ) ;
3773+ assert_eq ! (
3774+ app. provider_detail
3775+ . as_ref( )
3776+ . expect( "detail view" )
3777+ . raw_profile_yaml
3778+ . yaml( ) ,
3779+ Some ( expected_yaml. as_str( ) )
3780+ ) ;
3781+ assert ! ( render_provider_detail( & app) . contains( "[y] Profile YAML" ) ) ;
3782+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'y' ) ) ) ;
3783+ let text = render_provider_detail ( & app) ;
3784+ assert ! ( text. contains( "Provider Profile YAML" ) ) ;
3785+ assert ! ( text. contains( "mcp-example" ) ) ;
3786+ assert ! ( !text. contains( "test-only-secret" ) ) ;
3787+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'j' ) ) ) ;
3788+ assert_eq ! (
3789+ app. provider_detail
3790+ . as_ref( )
3791+ . expect( "detail view" )
3792+ . raw_profile_scroll,
3793+ 1
3794+ ) ;
3795+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'k' ) ) ) ;
3796+ assert_eq ! (
3797+ app. provider_detail
3798+ . as_ref( )
3799+ . expect( "detail view" )
3800+ . raw_profile_scroll,
3801+ 0
3802+ ) ;
3803+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'o' ) ) ) ;
3804+ assert ! (
3805+ app. provider_detail
3806+ . as_ref( )
3807+ . expect( "detail view" )
3808+ . show_raw_provider
3809+ ) ;
3810+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'y' ) ) ) ;
3811+ let detail = app. provider_detail . as_ref ( ) . expect ( "detail view" ) ;
3812+ assert ! ( detail. show_raw_profile) ;
3813+ assert ! ( !detail. show_raw_provider) ;
3814+ app. handle_provider_detail_key ( key ( KeyCode :: Esc ) ) ;
3815+ assert ! (
3816+ !app. provider_detail
3817+ . as_ref( )
3818+ . expect( "detail view" )
3819+ . show_raw_profile
3820+ ) ;
3821+ app. handle_provider_detail_key ( key ( KeyCode :: Esc ) ) ;
3822+ assert ! ( app. provider_detail. is_none( ) ) ;
3823+ }
3824+
3825+ #[ tokio:: test]
3826+ async fn provider_detail_invalid_profile_remains_visible_without_yaml_action ( ) {
3827+ for versions in [ vec ! [ "2025-11-25" , "2025-11-25" ] , vec ! [ "latest" ] ] {
3828+ let mut app = detail_app ( Some ( detail_mcp_profile ( & versions) ) ) ;
3829+ let detail = app. provider_detail . as_ref ( ) . expect ( "detail view" ) ;
3830+ assert ! ( matches!(
3831+ detail. raw_profile_yaml,
3832+ ProviderProfileYaml :: Invalid
3833+ ) ) ;
3834+ assert_eq ! ( detail. profile_name. as_deref( ) , Some ( "MCP Example" ) ) ;
3835+ let text = render_provider_detail ( & app) ;
3836+ assert ! ( text. contains( "example-provider" ) ) ;
3837+ assert ! ( text. contains( "Profile YAML unavailable: serialization failed." ) ) ;
3838+ assert ! ( text. contains( "Correct the profile at its source, then reopen this view." ) ) ;
3839+ assert ! ( !text. contains( "legacy/unprofiled" ) ) ;
3840+ assert ! ( !text. contains( "[y]" ) ) ;
3841+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'y' ) ) ) ;
3842+ assert ! (
3843+ !app. provider_detail
3844+ . as_ref( )
3845+ . expect( "detail view" )
3846+ . show_raw_profile
3847+ ) ;
3848+ app. handle_provider_detail_key ( key ( KeyCode :: Char ( 'o' ) ) ) ;
3849+ let text = render_provider_detail ( & app) ;
3850+ assert ! ( text. contains( "Provider Object YAML" ) ) ;
3851+ assert ! ( text. contains( "<redacted>" ) ) ;
3852+ assert ! ( !text. contains( "test-only-secret" ) ) ;
3853+ }
3854+ }
3855+
3856+ #[ tokio:: test]
3857+ async fn provider_detail_multibyte_profile_error_is_bounded_and_redacted ( ) {
3858+ let malformed = "秘密é🚧\n private-profile-value" . repeat ( 2048 ) ;
3859+ let profile = detail_mcp_profile ( & [ malformed. as_str ( ) ] ) ;
3860+ let error =
3861+ openshell_providers:: profile_to_yaml ( & ProviderTypeProfile :: from_proto ( & profile) )
3862+ . expect_err ( "malformed revision must fail strict serialization" ) ;
3863+ assert ! ( error. to_string( ) . contains( "秘密" ) ) ;
3864+ let app = detail_app ( Some ( profile) ) ;
3865+ assert ! ( matches!(
3866+ app. provider_detail
3867+ . as_ref( )
3868+ . expect( "detail view" )
3869+ . raw_profile_yaml,
3870+ ProviderProfileYaml :: Invalid
3871+ ) ) ;
3872+ let text = render_provider_detail ( & app) ;
3873+ assert ! ( text. contains( "Profile YAML unavailable: serialization failed." ) ) ;
3874+ assert ! ( !text. contains( "秘密" ) ) ;
3875+ assert ! ( !text. contains( "private-profile-value" ) ) ;
3876+ assert ! ( !text. contains( "test-only-secret" ) ) ;
3877+ let ordinary_failure = detail_app ( Some ( detail_mcp_profile ( & [ "latest" ] ) ) ) ;
3878+ assert_eq ! ( text, render_provider_detail( & ordinary_failure) ) ;
3879+ }
3880+
36633881 #[ tokio:: test]
36643882 async fn create_provider_enter_with_empty_profile_catalog_is_recoverable ( ) {
36653883 let mut app = test_app ( ) ;
0 commit comments