Personalized content ranking database
Go to file
jordan 6385425a92
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ranking: make Hot and New age-aware; fix the same gap in three more places
`score_hot` hardcoded `age_hours = 24.0`, so the divisor in
`log10(max(views,1)) / (age_hours + 2)^gravity` was constant across the candidate
set and `Sort::Hot` reduced EXACTLY to `log10(max(views, 1))` -- a view-count
ranking wearing a recency sort's name. Four built-in profiles use it (`hot`,
`for_you`, `following`, `brief`); anyone tuning `gravity` was tuning a no-op.

The in-code comment justified this by saying a per-entity `created_at` lookup
needs an `EntityId -> created_at_ns` reverse map that "is not built". That was
stale, and it was the load-bearing claim: `created_at` has been materialized INTO
item metadata on every write since `Items::metadata_with_created_at`, the executor
has held an `EntityId -> metadata` map since M6p3, and the replication record
carries the materialized map so replicas cannot diverge. No index, storage change,
schema change or migration -- the scorer reads the map it already had, exactly the
way `read_duration` does three lines away.

`Sort::New` used `entity_id as f64`. Wrong twice: it assumed IDs are assigned in
creation order, and it used the ID's MAGNITUDE as the base score, so on a catalog
of N items the sort contributed ~N against a boost sum in single digits. Recency
did not participate in the ranking, it annihilated every boost. Now negated age in
hours -- same ordering, boost-comparable scale.

Three more instances of the same defect class, found by auditing rather than
assuming the report was complete:

1. Both age sorts were missing from `needs_metadata_for_sort`, so a profile with
   no session and no diversity never loaded the map the fix depends on.
2. Every metadata sort was DEAD on the SEARCH path. Its metadata pre-load was
   gated on `session_context.is_some()` and never consulted `profile.sort`, AND
   the `ProfileExecutor` it built never had `with_item_metadata` called at all --
   the map it did compute went only to the keyword-hint argument, which the sort
   scorers do not read. `shortest`/`longest` scored NEG_INFINITY and the
   alphabetical sorts the missing-title sentinel, for every candidate, silently.
3. Under `ReducedCandidates` load the candidate cap kept the highest entity IDs,
   correct only while `Sort::New` meant "highest ID". Left alone it would discard
   the genuinely newest items BEFORE scoring -- wrong only when degraded, the
   hardest case to notice. Now keyed off the `created_at` index via the new
   `RangeIndex::top_n_descending`.

The decision "which sorts read item metadata" now lives on `Sort` itself as an
exhaustive match. It was a `matches!` in one executor while a second executor had
its own different copy, which is precisely how a metadata-reading sort came to be
omitted from both.

MEASURED, not inferred:
- Real server, 10 items, equal views, ages 2-20 days: before every score was 0.5
  (all-equal set folded to the normalizer's midpoint) and the feed returned
  oldest-first forever; after, 1.0 -> 0.0 strictly descending, newest first.
- `new` with zero signals returns the exact REVERSE of candidate-scan order.
- `alphabetical_asc`, `shortest`, `longest` verified end to end with title and
  duration order both opposing entity id.
- Metadata point-read cost at 2,000 candidates (the ceiling: `scan_candidates`
  caps at `max(limit*10, 200)` and `limit > 500` is rejected): 7.25ms, 3.6us per
  candidate. Guarded at 250ms.

THE BUG REPORT'S CENTRAL PROMISE IS FALSE and the changelog says so. §7 claimed
this fix lets a zero-signal corpus rank newest-first so a consumer could delete
its workaround. It arithmetically cannot: the numerator `log10(max(views,1))` is
exactly 0.0 for 0 OR 1 views, so the age divisor has nothing to scale and every
candidate still ties -- confirmed on the live server, all ten scores 0.5.
Age-awareness begins at the second view. Fixing cold-start needs recency to be
ADDITIVE rather than a pure divisor, which reorders every existing Hot consumer,
so it is a separate decision. `sort_hot_zero_view_corpus_still_ties_regardless_of_
age` pins the limit so it cannot be rediscovered by accident.

Three existing tests asserted the old entity-ID behaviour. Inverted to assert real
recency, not loosened -- and each fixture now makes id order and creation order
DISAGREE, because an ordering assertion where the two candidate orderings agree is
satisfied by the defect too. Three of my own new tests were vacuous for exactly
that reason and were caught by mutation-testing; one was also flaky (it passed in
a 12-test run and failed run alone, because retrieval order for exactly-tied
vectors is not deterministic). Every new assertion is mutation-proven against the
implementation it replaces.

Full lib suite 2130 passed. Clippy 66 warnings vs 66 at baseline, zero added.
2026-08-31 19:58:01 -06:00
.claude feat(m11): cluster security (m11p7) + perf instrumentation floor 2026-06-13 01:25:35 -06:00
.codex/agents feat(m12): multi-vector user preference modeling + ANN candidate-gen 2026-06-23 09:52:36 -06:00
.sdlc p0: specify Beachhead Validation, advancing all three features to specified 2026-08-16 12:39:39 -06:00
ai-lookup docs(m12): refresh API, specs, ops, and roadmap to the shipped M12 reality 2026-06-23 21:39:55 -06:00
applications fleet remediation: make the workspace gate runnable, then fix what it caught 2026-08-16 12:38:14 -06:00
demo harden: restore CI verification, remove four wire-level fabrications, instrument the 401 path 2026-08-30 20:55:58 -06:00
docker fleet remediation: make the workspace gate runnable, then fix what it caught 2026-08-16 12:38:14 -06:00
docs ci: gate the image on deterministic suites; the rolling upgrade becomes pre-release 2026-08-31 02:24:43 -06:00
hooks hooks: fail the file-length check only for new files, warn for existing ones 2026-08-17 17:47:39 -06:00
k8s ranking: make Hot and New age-aware; fix the same gap in three more places 2026-08-31 19:58:01 -06:00
scripts harden: restore CI verification, remove four wire-level fabrications, instrument the 401 path 2026-08-30 20:55:58 -06:00
site feat: complete M6-M7 + Enterprise Readiness milestones; split oversized source files per CODING_GUIDELINES §9 2026-02-23 22:41:16 -07:00
tests/e2e verify: flip the two log tripwires post-roll, calibrate the backup assertions 2026-08-30 22:06:20 -06:00
tidal ranking: make Hot and New age-aware; fix the same gap in three more places 2026-08-31 19:58:01 -06:00
tidal-net fix(cluster): discharge a reseed marker on served evidence, never on a frontier 2026-08-21 00:40:06 -06:00
tidal-server ci: disable incremental compilation, surface the nested build's error 2026-08-31 01:31:03 -06:00
tidal-stress test(e2e): verify ranking semantics with a content-feed app, and route three product findings 2026-08-23 22:42:02 -06:00
tidalctl harden: restore CI verification, remove four wire-level fabrications, instrument the 401 path 2026-08-30 20:55:58 -06:00
.dockerignore feat(tidal-stress): open-loop capacity load generator (thepeach feed workload) 2026-06-10 21:54:21 -06:00
.gitignore ranking: make Hot and New age-aware; fix the same gap in three more places 2026-08-31 19:58:01 -06:00
.woodpecker.yaml ci: gate the image on deterministic suites; the rolling upgrade becomes pre-release 2026-08-31 02:24:43 -06:00
AGENTS.md e2e: get the Playwright harness green end to end, and close the stale-evidence gap 2026-08-30 15:27:56 -06:00
API.md harden: restore CI verification, remove four wire-level fabrications, instrument the 401 path 2026-08-30 20:55:58 -06:00
ARCHITECTURE.md docs(m12): refresh API, specs, ops, and roadmap to the shipped M12 reality 2026-06-23 21:39:55 -06:00
Cargo.lock feat(observability): HTTP metrics, structured logs, dashboard, live tidalctl 2026-08-23 10:31:57 -06:00
Cargo.toml fleet remediation: make the workspace gate runnable, then fix what it caught 2026-08-16 12:38:14 -06:00
CHANGELOG.md ranking: make Hot and New age-aware; fix the same gap in three more places 2026-08-31 19:58:01 -06:00
CLAUDE.md e2e: get the Playwright harness green end to end, and close the stale-evidence gap 2026-08-30 15:27:56 -06:00
CODING_GUIDELINES.md vector search: normalize the query, instrument the blob path, expose per-group vector counts 2026-08-30 13:57:36 -06:00
CONTRIBUTING.md fleet remediation: make the workspace gate runnable, then fix what it caught 2026-08-16 12:38:14 -06:00
forage-discover.sh feat: complete M8 replication primitives + forage enhancements + docs 2026-02-24 13:17:19 -07:00
package-lock.json test(e2e): Playwright evidence harness for the deploy-verification runbook 2026-08-23 14:03:29 -06:00
package.json e2e: get the Playwright harness green end to end, and close the stale-evidence gap 2026-08-30 15:27:56 -06:00
playwright.config.ts test(e2e): verify ranking semantics with a content-feed app, and route three product findings 2026-08-23 22:42:02 -06:00
playwright.demo.config.ts test(e2e): Playwright evidence harness for the deploy-verification runbook 2026-08-23 14:03:29 -06:00
playwright.semantics.config.ts test(e2e): verify ranking semantics with a content-feed app, and route three product findings 2026-08-23 22:42:02 -06:00
QUICKSTART.md docs: withdraw the pre-release "not ready for production" disclaimer 2026-07-30 19:03:34 -06:00
README.md docs: withdraw the pre-release "not ready for production" disclaimer 2026-07-30 19:03:34 -06:00
remotion.config.ts test(e2e): Playwright evidence harness for the deploy-verification runbook 2026-08-23 14:03:29 -06:00
rust-toolchain.toml chore(toolchain): declare the release cross target in the pin 2026-08-17 20:29:52 -06:00
SEQUENCE.md chore: initialize tidalDB repository with schema foundation and standards 2026-02-20 12:52:20 -07:00
thoughts.md chore: initialize tidalDB repository with schema foundation and standards 2026-02-20 12:52:20 -07:00
tsconfig.json test(e2e): verify ranking semantics with a content-feed app, and route three product findings 2026-08-23 22:42:02 -06:00
USE_CASES.md chore: initialize tidalDB repository with schema foundation and standards 2026-02-20 12:52:20 -07:00
VISION.md feat: complete Milestones 2–4 — RETRIEVE query, vector index, ranking profiles, diversity, entity system, sessions 2026-02-21 16:24:48 -07:00

tidalDB

An embeddable Rust database for the personalized content ranking problem.

Production-ready. M0M12 shipped: crash-safe storage, ranked retrieval, hybrid search, ANN vector retrieval, and a quorum-acked HA cluster running in production on k3s. The API surface is stable for shipped features.


Every content platform eventually builds the same distributed system from scratch: Elasticsearch for retrieval, Redis for hot signals, Kafka for event ingestion, a feature store for user profiles, a vector database for semantic search, and a ranking service that stitches them together. The seams between those systems are where correctness dies — stale signals, inconsistent ranking, cache invalidation bugs, ETL lag.

The root cause: existing databases treat ranking as an afterthought. They have no native concept of signals that evolve over time, no understanding of user context, no diversity as a query constraint.

Ranking is not a feature. It is a primitive.

tidalDB is a single-node, embeddable Rust library built for one question: given a user and a context, what content should they see, and in what order? No server, no network protocol, no client SDK. Link it into your process.


What it looks like

use std::collections::HashMap;
use std::time::Duration;
use tidaldb::{TidalDb, query::retrieve::Retrieve, schema::{DecaySpec, EntityId, EntityKind, SchemaBuilder, Timestamp, Window}};

// Declare signals with native decay — no application formulas.
let mut schema = SchemaBuilder::new();
let _ = schema.signal("view", EntityKind::Item, DecaySpec::Exponential {
    half_life: Duration::from_secs(7 * 24 * 3600),
}).windows(&[Window::OneHour, Window::TwentyFourHours, Window::AllTime]).velocity(true).add();
let _ = schema.signal("like", EntityKind::Item, DecaySpec::Exponential {
    half_life: Duration::from_secs(30 * 24 * 3600),
}).windows(&[Window::AllTime]).velocity(false).add();
let schema = schema.build()?;

// Open — ephemeral for tests, persistent for production.
let db = TidalDb::builder().ephemeral().with_schema(schema).open()?;

// Ingest content with metadata.
let mut meta = HashMap::new();
meta.insert("title".to_string(), "Introduction to Jazz Piano".to_string());
meta.insert("category".to_string(), "music".to_string());
db.write_item_with_metadata(EntityId::new(1), &meta)?;

// Write an embedding (you generate it, tidalDB indexes and ranks over it).
db.write_item_embedding(EntityId::new(1), &your_model.embed("Introduction to Jazz Piano"))?;

// Record engagement — the feedback loop closes here, no ETL required.
db.signal("view", EntityId::new(1), 1.0, Timestamp::now())?;
db.signal_with_context("like", EntityId::new(1), 1.0, Timestamp::now(), Some(user_id), Some(creator_id))?;

// Retrieve a ranked feed. Name the profile. tidalDB executes the pipeline.
let results = db.retrieve(&Retrieve::builder().for_user(user_id).profile("for_you").limit(50).build()?)?;

// Search: BM25 + semantic similarity fused via RRF.
let results = db.search(&Search::builder().query("jazz piano tutorial").for_user(user_id).limit(20).build()?)?;

db.close()?;

What it replaces

System tidalDB equivalent
Elasticsearch Tantivy BM25 text index (derived, crash-recoverable)
Redis Lock-free in-memory signal ledger — decay scores, windowed counters
Kafka Write-ahead log — durable, ordered, replayable
Feature store Signal aggregates + user preference vectors (updated at write time)
Vector DB USearch HNSW — embedded, f16 quantized, predicate-filtered ANN
Ranking service 25 named profiles, scored at query time, swappable by name

Key capabilities

  • Signals with native decay — declare view with a 7-day half-life; the database applies it at query time. No trending_score_7d field to maintain.
  • 25 built-in ranking profilestrending, hot, for_you, following, related, hidden_gems, top_week, shuffle, controversial, and more. Name the profile; the database executes the full pipeline.
  • Hybrid search — BM25 full-text + ANN semantic similarity, fused via Reciprocal Rank Fusion, personalized by user preference vector.
  • Composable filters — filter by category, format, duration, language, engagement threshold, location, collection membership, and more — any combination, all composable.
  • Diversity as a query constraintmax_per_creator: 2 belongs in the query, not your API layer.
  • Feedback loop in the write path — a signal write atomically updates the item's ledger, the user's preference vector, and relationship weights. The next ranking query — 100ms later — reflects it.
  • Cold start handled — new content gets an exploration budget; new users get sensible defaults. No application logic required.
  • Cohort-scoped trending — "trending among US users aged 18-24 who engage with jazz" is one query, not a pipeline.
  • Embeddable first — runs in your process. Arc<TidalDb> is Send + Sync. No operational overhead.

Getting started

Pick the path that matches how you plan to use tidalDB today. Every option below is self-contained and ships in this repo.

1. Embed tidalDB inside your Rust service (library mode)

Setup

  1. Add the dependency (the tidaldb crate is at tidal/ in this repository):
    [dependencies]
    tidaldb = { git = "https://github.com/orchard9/tidaldb", rev = "..." }
    # or, for a local checkout: tidaldb = { path = "path/to/tidaldb/tidal" }
    
  2. Define your schema before opening the database (decay, windows, text fields, embeddings). The snippet in Quickstart, Step 2 is a ready-to-copy template.
  3. Choose storage mode when building:
    let db = tidaldb::TidalDb::builder()
        .with_schema(schema)
        .ephemeral()               // in-memory for tests
        // .with_data_dir("/var/lib/tidaldb") // persistent deployment
        .open()?;
    
  4. Run the end-to-end sample:
    cargo run --manifest-path tidal/Cargo.toml --example quickstart
    

Usage

  • Call db.signal(...), db.signal_with_context(...), and db.retrieve(...) / db.search(...) from the same process; no network stack required.
  • Wrap the instance in Arc<TidalDb> to share it across threads or tasks.
  • Persisted deployments can be inspected with the CLI tool: cargo run -p tidalctl -- status --path /var/lib/tidaldb.
  • Full walkthrough: QUICKSTART.md and API.md.

2. Run the standalone HTTP server (tidal-server)

Why: you want a ready-to-run HTTP facade without writing Axum/Actix glue.

cargo run -p tidal-server -- \
  standalone \
  --listen 127.0.0.1:9400 \
  --schema tidal-server/config/default-schema.yaml

Options:

  • --data-dir /var/lib/tidaldb switches to persistent storage.
  • Provide your own schema file (YAML) to match your signal mix.

Usage:

# register metadata + embedding
curl -X POST http://127.0.0.1:9400/items \
  -H 'Content-Type: application/json' \
  -d '{ "entity_id": 1, "metadata": { "title": "Jazz Piano", "category": "music" } }'
curl -X POST http://127.0.0.1:9400/embeddings \
  -H 'Content-Type: application/json' \
  -d '{ "entity_id": 1, "values": [0.1, 0.2, 0.3] }'

# write engagement (supports user/creator context)
curl -X POST http://127.0.0.1:9400/signals \
  -H 'Content-Type: application/json' \
  -d '{ "entity_id": 1, "signal": "view", "weight": 1.0, "user_id": 42 }'

# query
curl "http://127.0.0.1:9400/feed?user_id=42&profile=for_you&limit=20"
curl "http://127.0.0.1:9400/search?query=jazz%20piano&user_id=42&limit=5"
curl http://127.0.0.1:9400/health

The default schema lives at tidal-server/config/default-schema.yaml. Edit it (or provide your own path) to align with your applications signals, text fields, and embedding slots.

3. Wrap it in an HTTP service you control

Expose tidalDB through your favorite web framework; the repo ships runnable templates.

  • Axum sample (tidal/examples/axum_embedding.rs)

    cargo run --example axum_embedding --manifest-path tidal/Cargo.toml
    

    Usage:

    curl -X POST http://127.0.0.1:3000/signal \
         -H 'Content-Type: application/json' \
         -d '{ "entity_id": 1, "signal": "view", "weight": 1.0 }'
    curl "http://127.0.0.1:3000/feed?user_id=42"
    curl http://127.0.0.1:3000/health
    

    The example handles schema setup, wraps Arc<TidalDb> in Axum State, and maps TidalError to HTTP responses.

  • Actix sample (tidal/examples/actix_embedding.rs)

    cargo run --example actix_embedding --manifest-path tidal/Cargo.toml
    # curl http://127.0.0.1:3001/health
    

    Demonstrates sharing Arc<TidalDb> through web::Data and using Actixs shutdown hooks.

Use either sample as a starting point for microservices that prefer a client/server boundary.

4. Run the Forage demo server (Axum + UI)

Want to see tidalDB powering a live personalization surface? Forage is a thin Axum server + feed UI that talks to a tidalDB instance embedded in-process.

cargo run -p forage-server --manifest-path applications/forage/server/Cargo.toml
open http://localhost:4242

Flags:

  • --ephemeral to keep everything in-memory.
  • --data-dir ~/.forage/data to point at a custom persistent directory.

Usage:

curl -X POST http://localhost:4242/signal \
     -H "Content-Type: application/json" \
     -d '{ "user_id": 1, "item_id": 42, "signal_type": "view" }'
curl "http://localhost:4242/feed?user=1&limit=7"

The UI shows seeded users, exploration labels, and real-time adaptation; see applications/forage/README.md for the full loop.

5. Run the cluster server + Docker image

Need a real high-availability endpoint? Run tidal-server in cluster mode. This is a genuine HA cluster — quorum-acked writes, automatic leader election + failover, elastic seed-join membership, inter-node mTLS, and per-node Prometheus metrics — deployed in production on k3s as one StatefulSet (3 pods = 3 regions = 3 voters, full-placement RF3 so every pod hosts all shard groups, HTTPS + mTLS on :9500). It exposes /signals, /feed, /search plus cluster-management routes.

Because a standalone node is the right answer for most deployments, cluster mode requires an explicit opt-in flag (--experimental-cluster, or TIDAL_ALLOW_EXPERIMENTAL_CLUSTER=1) so nobody starts a multi-node fabric by accident. Reach for it deliberately when you need multi-node availability or read-scale.

cargo run -p tidal-server -- \
  cluster \
  --listen 0.0.0.0:9500 \
  --schema tidal-server/config/default-schema.yaml \
  --topology tidal-server/config/default-cluster.yaml \
  --experimental-cluster

Key endpoints:

curl https://127.0.0.1:9500/health
curl -X POST https://127.0.0.1:9500/signals -d '{ "entity_id": 1, "signal": "view", "weight": 1.0 }'
curl "https://127.0.0.1:9500/feed?profile=trending&region=eu-west"
curl https://127.0.0.1:9500/cluster/status
# /cluster/promote is a fenced MAINTENANCE verb: a graceful, voluntary
# leadership handoff. It is NOT the failover path — kill the leader and the
# survivors elect a successor automatically, with zero operator action.
curl -X POST https://127.0.0.1:9500/cluster/promote -d '{ "region": "eu-west" }'

Cluster mode replicates global signals only (no user_id / creator_id contexts) so that followers stay in sync with the leader's replicated log. For Kubernetes deployment, scaling, failover drills, and the operational API see docs/runbooks/kubernetes.md and docs/runbooks/cluster.md.

Prefer containers? Build the provided image and run it anywhere:

docker build -f docker/cluster/Dockerfile -t tidal-cluster .
docker run --rm -p 9500:9500 tidal-cluster

Mount your own schema/topology files with -v if you want different regions or signal definitions.

6. Simulate a multi-region cluster in tests

The raw SimulatedCluster harness (no HTTP) remains available for property tests and fuzzing.

cargo test --test m8_uat
cargo test --test m8_uat uat_step3 -- --nocapture   # run a single scenario

Tweak tidal/tests/m8_uat.rs to script specific replication, failover, and migration scenarios inside your own test suites.

MSRV: Rust 1.91


Documentation

Document Contents
QUICKSTART.md Step-by-step guide: schema, ingest, signals, ranking, search
API.md Full API reference with code examples
Build a feed app End-to-end TikTok/Reels-style "For You" feed tutorial
Embedding integration Wiring a real embedding model into the write + query paths
Server deployment Running tidal-server: config, auth, OpenAPI, Docker
Kubernetes runbook Deploying on k8s (manifests in k8s/)
VISION.md Problem statement and design thesis
ARCHITECTURE.md Storage, signal system, vector index, query pipeline
USE_CASES.md 14 content discovery surfaces, filter and sort references

Status

Milestones completed:

  • Storage engine, WAL, entity store, signal ledger
  • RETRIEVE query: candidate retrieval, filtering, scoring, diversity, pagination
  • Vector index (USearch HNSW) with adaptive filtered search; ANN candidate generation in RETRIEVE with honored per-query ef_search
  • Multi-vector user preference modeling (per-user interest clusters with decayed importance)
  • 25 built-in ranking profiles
  • BM25 full-text search (Tantivy) + hybrid RRF fusion
  • Creator search and creator profiles
  • Cohort-scoped signal aggregation and trending
  • Social graph (follows, blocks, following feed)
  • Collections, saved searches, autocomplete suggestions
  • Session and agent context (short-lived signals, preference decay)
  • Crash recovery, graceful degradation, rate limiting, diagnostics
  • Scale: tested to 1M items; scale benchmarks passing
  • High-availability cluster: quorum-acked writes, automatic election + failover, elastic seed-join membership, inter-node mTLS, per-node Prometheus — running in production on k3s

tidalDB is production-ready. The API surface is stable for the implemented features, and every shipped guarantee is covered by the chaos and soak suites in docs/planning/ROADMAP.md. Semantic versioning applies from here: additive changes ship in minor releases, and any breaking change gets a documented migration path in CHANGELOG.md.