Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion visibility-filtering/hydration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use safety_label_hydrator::{SafetyLabelHydration, SafetyLabelHydrator};
use socialgraph_hydrator::SocialgraphHydrator;
use std::collections::HashMap;
use std::sync::Arc;
use tes_hydrator::TesHydrator;
use tes_hydrator::{retain_candidates_with_usable_edit_control, TesHydrator};
use viewer_hydrator::ViewerHydrator;
use xai_core_entities::gizmoduck_client::GizmoduckClient;
use xai_core_entities::tweet_entity_service_client::TESClient;
Expand Down Expand Up @@ -207,6 +207,8 @@ impl HydrationPipeline {
label_response,
} = safety_labels;

let candidates =
retain_candidates_with_usable_edit_control(candidates, &tes_tweet_keyed);
let tweet_features = self.tes_hydrator.assemble_tweet_features(
&candidates,
&core_datas,
Expand Down
130 changes: 129 additions & 1 deletion visibility-filtering/hydration/tes_hydrator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hydration::batch::TweetHydrationBatch;
use crate::hydration::batch::{Hydrated, TweetHydrationBatch};
use crate::hydration::metrics::{record_batch_size, timed_keyed_rpc, timed_results};
use crate::models::{
CoreFeature, MediaFeature, NsfwFeature, TweetCandidateInput, TweetFeatures, TweetId,
Expand Down Expand Up @@ -28,6 +28,26 @@ pub(crate) struct TweetHydration {
pub(crate) media: TweetHydrationBatch<MediaFeature>,
}

impl TweetHydration {
/// TES `get_edit_control` Failed must not assemble as `None`.
/// `is_stale_tweet` treats missing edit control as current, so a stale
/// tombstone (pre-edit labeled text) would serve. Genuine NotFound
/// (never edited) is not Failed.
pub(crate) fn edit_control_lookup_failed(&self, id: TweetId) -> bool {
matches!(self.edit_control.hydrated(&id), Some(Hydrated::Failed(_)))
}
}

pub(crate) fn retain_candidates_with_usable_edit_control(
candidates: Vec<TweetCandidateInput>,
tweet_keyed: &TweetHydration,
) -> Vec<TweetCandidateInput> {
candidates
.into_iter()
.filter(|c| !tweet_keyed.edit_control_lookup_failed(c.tweet_id))
.collect()
}

impl TesHydrator {
pub async fn fetch_pure_core(
&self,
Expand Down Expand Up @@ -474,4 +494,112 @@ mod tests {
assert!(f.core.text.is_empty());
assert!(!f.media.has_media);
}

fn found_edit_control(id: u64) -> TweetHydrationBatch<EditControl> {
found(id, EditControl::Initial(Default::default()))
}

fn not_found_edit_control(id: u64) -> TweetHydrationBatch<EditControl> {
TweetHydrationBatch::from_results(
[TweetId(id)],
HashMap::from([(TweetId(id), Ok::<_, anyhow::Error>(None))]),
)
}

fn failed_edit_control(id: u64) -> TweetHydrationBatch<EditControl> {
TweetHydrationBatch::from_results(
[TweetId(id)],
HashMap::from([(
TweetId(id),
Err::<Option<EditControl>, _>("tes unavailable"),
)]),
)
}

#[test]
fn edit_control_lookup_failed_is_false_when_found_or_not_found() {
let found = TweetHydration {
edit_control: found_edit_control(10),
..Default::default()
};
let not_found = TweetHydration {
edit_control: not_found_edit_control(10),
..Default::default()
};

assert!(!found.edit_control_lookup_failed(TweetId(10)));
assert!(!not_found.edit_control_lookup_failed(TweetId(10)));
assert!(!TweetHydration::default().edit_control_lookup_failed(TweetId(10)));
}

#[test]
fn assemble_collapses_failed_edit_control_to_none() {
let candidates = vec![candidate(10, 100)];
let core_datas = HashMap::from([(
TweetId(10),
PureCoreData {
author_id: 100,
..Default::default()
},
)]);
let tweet_keyed = TweetHydration {
edit_control: failed_edit_control(10),
..Default::default()
};

let features = hydrator().assemble_tweet_features(&candidates, &core_datas, &tweet_keyed);

assert!(features[&TweetId(10)].edit_control.is_none());
}

#[test]
fn failed_edit_control_lookup_is_edit_failure() {
let keyed = TweetHydration {
edit_control: failed_edit_control(10),
..Default::default()
};

assert!(keyed.edit_control_lookup_failed(TweetId(10)));
assert!(!keyed.edit_control_lookup_failed(TweetId(11)));
}

#[test]
fn timed_out_edit_control_batch_is_edit_failure() {
let keyed = TweetHydration {
edit_control: TweetHydrationBatch::timed_out([TweetId(10)]),
..Default::default()
};

assert!(keyed.edit_control_lookup_failed(TweetId(10)));
}

#[test]
fn retain_drops_only_ids_whose_edit_control_rpc_failed() {
let keyed = TweetHydration {
edit_control: TweetHydrationBatch::from_results(
[TweetId(10), TweetId(11), TweetId(12)],
HashMap::from([
(
TweetId(10),
Err::<Option<EditControl>, _>("tes unavailable"),
),
(TweetId(11), Ok::<_, anyhow::Error>(None)),
(
TweetId(12),
Ok::<_, anyhow::Error>(Some(EditControl::Initial(Default::default()))),
),
]),
),
..Default::default()
};
let kept = retain_candidates_with_usable_edit_control(
vec![candidate(10, 100), candidate(11, 100), candidate(12, 100)],
&keyed,
);

assert_eq!(
kept.iter().map(|c| c.tweet_id.0).collect::<Vec<_>>(),
vec![11, 12]
);
}
}