lantern 0.2.4

Local-first, provenance-aware semantic search for agent activity
Documentation
//! Positive-only query-success signal for chunks.
//!
//! A chunk's `query_success_count` is a non-negative tally of "this chunk
//! helped answer a query successfully" events. It is intentionally separate
//! from `feedback_score` so an explicit thumbs-up/-down vote stays distinct
//! from an implicit, observed-success signal: a caller can record a query
//! success without claiming the user rated the chunk. Scoring lives in
//! [`crate::search::compute_confidence`]; this module is just the write path
//! and a thin read helper for tests and library callers.
//!
//! `0` is the neutral default and produces identical confidence to a store
//! that never records any query successes, so the v11 migration is a ranking
//! no-op for existing data.

use anyhow::{Context, Result};
use rusqlite::OptionalExtension;

use crate::store::Store;

/// Increment `chunk_id`'s query-success counter by 1 and return the new count.
///
/// Errors if the chunk does not exist. The update is a single UPDATE statement
/// so concurrent successes from multiple writers compose additively.
pub fn record_query_success(store: &Store, chunk_id: &str) -> Result<i64> {
    let conn = store.conn();
    let rows = conn
        .execute(
            "UPDATE chunks SET query_success_count = query_success_count + 1 WHERE id = ?1",
            rusqlite::params![chunk_id],
        )
        .with_context(|| format!("recording query success for chunk {chunk_id}"))?;
    if rows == 0 {
        anyhow::bail!("no chunk with id {chunk_id}");
    }
    get_query_success_count(store, chunk_id)?
        .ok_or_else(|| anyhow::anyhow!("chunk {chunk_id} disappeared after query-success update"))
}

/// Read the current query-success count for `chunk_id`, or `None` if the chunk
/// is not in the store.
pub fn get_query_success_count(store: &Store, chunk_id: &str) -> Result<Option<i64>> {
    let conn = store.conn();
    let count = conn
        .query_row(
            "SELECT query_success_count FROM chunks WHERE id = ?1",
            rusqlite::params![chunk_id],
            |row| row.get::<_, i64>(0),
        )
        .optional()?;
    Ok(count)
}