Custom Post Type Tag Capabilities Not Working

It seems that although WP core creates four capability mappings for the built-in post_tag taxonomy (manage_terms => manage_post_tags, edit_terms => edit_post_tags, delete_terms => delete_post_tags, assign_terms => assign_post_tags) when it is registered, it uses different values when checking if the user has one of those capabilities.

If you look at the implementation of the map_meta_cap function in WP core (in wp-includes/capabilities.php lines 513-523), you can see that the edit_posts capability is returned for assign_post_tags and manage_categories is returned for manage_post_tags, edit_post_tags and delete_post_tags. To fix this, you can add a filter and return the expected values:

add_filter( 'map_meta_cap', function( $caps, $cap, $user_id, $args ) {
    $caps_to_fix = [
        'manage_post_tags',
        'edit_post_tags',
        'delete_post_tags',
        'assign_post_tags',
    ];

    if ( in_array( $cap, $caps_to_fix ) ) {
        $caps = [ $cap ];
    }

    return $caps;
}, 10, 4 );