x-algorithm-feed
```markdown
What this skill does
```markdown
---
name: x-algorithm-feed
description: Rust implementation of X's For You feed ranking algorithm by xai-org
triggers:
- implement x for you feed algorithm
- twitter recommendation algorithm rust
- x feed ranking system
- build social media feed ranking
- xai recommendation algorithm
- for you feed implementation
- x algorithm ranking rust
- social feed personalization algorithm
---
# X Algorithm (For You Feed)
> Skill by [ara.so](https://ara.so) — Daily 2026 Skills collection.
The X Algorithm (`xai-org/x-algorithm`) is the open-source Rust implementation powering the "For You" feed on X (formerly Twitter). It provides candidate sourcing, feature extraction, scoring, and ranking logic for personalizing social media feeds at scale.
---
## What It Does
- **Candidate sourcing**: Fetches tweet candidates from multiple sources (social graph, interest graph, trending)
- **Feature extraction**: Extracts user, tweet, and engagement features
- **Scoring & ranking**: Scores candidates using ML models and heuristics
- **Filtering**: Applies safety, diversity, and relevance filters
- **Serving**: Exposes ranked feed results via a gRPC/REST API
---
## Installation & Setup
### Prerequisites
- Rust 1.75+ (`rustup install stable`)
- Cargo
- (Optional) Docker for containerized deployment
### Clone & Build
```bash
git clone https://github.com/xai-org/x-algorithm.git
cd x-algorithm
cargo build --release
```
### Run Tests
```bash
cargo test
cargo test --lib # unit tests only
cargo test --integration # integration tests
```
### Run the Service
```bash
cargo run --release -- --config config/default.toml
```
---
## Configuration
The algorithm is configured via TOML files and environment variables.
### `config/default.toml` (example structure)
```toml
[server]
host = "0.0.0.0"
port = 8080
workers = 8
[ranking]
max_candidates = 1500
final_feed_size = 150
diversity_factor = 0.3
recency_weight = 0.25
[scoring]
model_path = "./models/ranker_v1.bin"
engagement_weight = 0.4
relevance_weight = 0.35
recency_weight = 0.25
[sources]
social_graph_weight = 0.5
interest_graph_weight = 0.3
trending_weight = 0.2
[filters]
safe_search = true
min_quality_score = 0.1
max_age_hours = 48
```
### Environment Variables
```bash
export X_ALGO_CONFIG_PATH=/etc/x-algorithm/config.toml
export X_ALGO_MODEL_PATH=/models/ranker_v1.bin
export X_ALGO_LOG_LEVEL=info
export X_ALGO_METRICS_PORT=9090
export X_ALGO_REDIS_URL=redis://localhost:6379
export X_ALGO_DB_URL=postgres://user:pass@localhost/xalgo
```
---
## Key Modules & API
### Core Data Types
```rust
use x_algorithm::types::{Tweet, User, FeedRequest, FeedResponse, CandidateScore};
// Tweet candidate
let tweet = Tweet {
id: 1234567890,
author_id: 987654321,
text: "Hello world".to_string(),
created_at: chrono::Utc::now(),
engagement: EngagementStats {
likes: 42,
retweets: 10,
replies: 5,
views: 1000,
},
..Default::default()
};
// Feed request
let request = FeedRequest {
user_id: 111222333,
cursor: None,
count: 50,
context: RequestContext {
device: DeviceType::Mobile,
language: "en".to_string(),
},
};
```
### Pipeline Usage
```rust
use x_algorithm::{
pipeline::FeedPipeline,
config::AlgorithmConfig,
sources::{SocialGraphSource, InterestGraphSource, TrendingSource},
ranker::NeuralRanker,
filters::{SafetyFilter, DiversityFilter},
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = AlgorithmConfig::from_file("config/default.toml")?;
// Build the pipeline
let pipeline = FeedPipeline::builder()
.with_source(SocialGraphSource::new(&config))
.with_source(InterestGraphSource::new(&config))
.with_source(TrendingSource::new(&config))
.with_ranker(NeuralRanker::load(&config.scoring.model_path)?)
.with_filter(SafetyFilter::default())
.with_filter(DiversityFilter::new(config.ranking.diversity_factor))
.build();
// Generate feed for a user
let request = FeedRequest::for_user(111222333);
let feed = pipeline.generate_feed(request).await?;
for item in &feed.items {
println!("Tweet {} | Score: {:.4}", item.tweet_id, item.score);
}
Ok(())
}
```
### Candidate Sourcing
```rust
use x_algorithm::sources::{CandidateSource, SocialGraphSource};
let source = SocialGraphSource::new(&config);
// Fetch candidates for a user
let candidates = source.fetch_candidates(
user_id,
FetchOptions {
max_candidates: 500,
since_timestamp: chrono::Utc::now() - chrono::Duration::hours(24),
include_retweets: true,
}
).await?;
println!("Fetched {} candidates", candidates.len());
```
### Feature Extraction
```rust
use x_algorithm::features::{FeatureExtractor, UserFeatures, TweetFeatures};
let extractor = FeatureExtractor::new(&config);
// Extract features for scoring
let features = extractor.extract(
&tweet,
&author,
&viewer_user,
&social_context,
).await?;
// Features include:
// - author_follower_count_log
// - tweet_age_seconds
// - engagement_rate
// - semantic_similarity_to_user_interests
// - author_quality_score
// - network_distance
println!("Feature vector: {:?}", features.as_slice());
```
### Scoring & Ranking
```rust
use x_algorithm::ranker::{Ranker, NeuralRanker, ScoredCandidate};
let ranker = NeuralRanker::load("models/ranker_v1.bin")?;
// Score a batch of candidates
let scored: Vec<ScoredCandidate> = ranker
.score_batch(&candidates, &user_context)
.await?;
// Sort by score descending
let mut ranked = scored;
ranked.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
// Take top N
let top_feed: Vec<_> = ranked.into_iter().take(150).collect();
```
### Custom Ranker Implementation
```rust
use x_algorithm::ranker::{Ranker, ScoredCandidate};
use x_algorithm::types::{Candidate, UserContext};
use async_trait::async_trait;
pub struct CustomHeuristicRanker {
recency_weight: f64,
engagement_weight: f64,
}
#[async_trait]
impl Ranker for CustomHeuristicRanker {
async fn score_batch(
&self,
candidates: &[Candidate],
context: &UserContext,
) -> anyhow::Result<Vec<ScoredCandidate>> {
let scored = candidates
.iter()
.map(|c| {
let age_hours = c.tweet.age_hours();
let recency_score = (-0.1 * age_hours).exp(); // exponential decay
let engagement_rate = c.tweet.engagement.likes as f64
/ (c.tweet.engagement.views as f64 + 1.0);
let score = self.recency_weight * recency_score
+ self.engagement_weight * engagement_rate;
ScoredCandidate {
candidate: c.clone(),
score,
score_breakdown: ScoreBreakdown {
recency: recency_score,
engagement: engagement_rate,
relevance: 0.0,
},
}
})
.collect();
Ok(scored)
}
}
```
### Filtering
```rust
use x_algorithm::filters::{Filter, DiversityFilter, FilterResult};
// Diversity filter ensures feed variety
let diversity_filter = DiversityFilter::new(DiversityConfig {
max_per_author: 2,
max_same_topic_ratio: 0.3,
min_content_similarity_distance: 0.15,
});
let filtered = diversity_filter.apply(ranked_candidates)?;
// Safety filter
use x_algorithm::filters::SafetyFilter;
let safety_filter = SafetyFilter::with_config(SafetyConfig {
block_nsfw: true,
min_author_quality: 0.2,
block_spam_signals: true,
});
let safe_feed = safety_filter.apply(filtered)?;
```
---
## Common Patterns
### Full Feed Generation Flow
```rust
use x_algorithm::prelude::*;
pub struct FeedService {
pipeline: FeedPipeline,
}
impl FeedService {
pub async fn new() -> anyhow::ReRelated in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing — use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.