second-brain-core 0.13.0

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
use std::collections::HashSet;
use std::ffi::OsStr;
use std::path::Path;

use anyhow::Result;
use chrono::{DateTime, Utc};
use rusqlite::params;
use uuid::Uuid;

use super::SqliteStore;

/// Claude Code nests a subagent's transcript under its session's directory;
/// the main agent's transcript never sits there.
const SUBAGENT_TRANSCRIPT: &str = "%/subagents/%";

/// A top-level Claude Code session with transcript lines in a window.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActiveSession {
    pub conversation_id: Uuid,
    pub project_path: Option<String>,
}

impl SqliteStore {
    /// Main-agent Claude Code sessions with a transcript line timed in
    /// `[since, until)`, read from the raw chunks' `session_at`, so
    /// archived and consolidated chunks still count. Subagent transcripts
    /// are left out because no note is ever asked of them.
    pub fn claude_code_sessions_between(
        &self,
        since: DateTime<Utc>,
        until: DateTime<Utc>,
        project_path: Option<&str>,
    ) -> Result<Vec<ActiveSession>> {
        let conn = self.reader();
        let mut stmt = conn.prepare(
            "SELECT conversation_id, MAX(project_path) FROM memories \
             WHERE source = 'claude-code' AND conversation_id IS NOT NULL \
             AND session_at >= ?1 AND session_at < ?2 \
             AND source_id NOT LIKE ?3 \
             AND (?4 IS NULL OR project_path = ?4) \
             GROUP BY conversation_id",
        )?;
        let rows = stmt.query_map(
            params![
                since.to_rfc3339(),
                until.to_rfc3339(),
                SUBAGENT_TRANSCRIPT,
                project_path
            ],
            |r| Ok((r.get::<_, String>(0)?, r.get::<_, Option<String>>(1)?)),
        )?;
        let mut sessions = Vec::new();
        for row in rows {
            let (id, project_path) = row?;
            let Ok(conversation_id) = Uuid::parse_str(&id) else {
                continue;
            };
            sessions.push(ActiveSession {
                conversation_id,
                project_path,
            });
        }
        Ok(sessions)
    }

    /// Which of `ids` came from Claude Code session `session` by
    /// `session_of`: a raw of its transcript or of a subagent's beneath
    /// it, or a note filed under its id.
    pub fn memories_from_session(&self, ids: &[Uuid], session: &str) -> Result<HashSet<Uuid>> {
        let mut found = HashSet::new();
        if ids.is_empty() {
            return Ok(found);
        }
        let conn = self.reader();
        for chunk in ids.chunks(500) {
            let holes = vec!["?"; chunk.len()].join(", ");
            let mut stmt = conn.prepare(&format!(
                "SELECT id, source_id FROM memories WHERE id IN ({holes})"
            ))?;
            let rows = stmt.query_map(
                rusqlite::params_from_iter(chunk.iter().map(Uuid::to_string)),
                |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)),
            )?;
            for row in rows {
                let (id, source_id) = row?;
                if session_of(&source_id).as_deref() == Some(session)
                    && let Ok(id) = Uuid::parse_str(&id)
                {
                    found.insert(id);
                }
            }
        }
        Ok(found)
    }
}

/// The Claude Code session a transcript path belongs to: its file stem, or
/// for a subagent transcript (`<session>/subagents/<agent>.jsonl`) the
/// directory above `subagents`. An agent note's source_id is the bare
/// session id, which is its own stem.
pub fn session_of(source_id: &str) -> Option<String> {
    let path = Path::new(source_id);
    let parent = path.parent();
    let name = if parent.and_then(Path::file_name) == Some(OsStr::new("subagents")) {
        parent?.parent()?.file_name()?
    } else {
        path.file_stem()?
    };
    Some(name.to_string_lossy().into_owned())
}

#[cfg(test)]
mod tests {
    use chrono::{Duration, TimeZone};

    use super::*;
    use crate::schema::{Conversation, Memory, MemoryType};
    use crate::store::Store;

    fn at(hour: i64) -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 11, 1, 0, 0, 0).unwrap() + Duration::hours(hour)
    }

    fn raw(
        store: &SqliteStore,
        source: &str,
        path: &str,
        session: &str,
        project: &str,
        hour: Option<i64>,
    ) {
        let mut memory = Memory::new(
            format!("chunk of {path} at {hour:?}"),
            MemoryType::Episodic,
            source.to_string(),
            path.to_string(),
        )
        .with_project_path(Some(project.to_string()));
        memory.conversation_id = Some(Conversation::claude_code_id(session));
        memory.session_at = hour.map(at);
        store.store_memory(&memory).unwrap();
    }

    #[test]
    fn sessions_with_a_main_transcript_line_in_the_window_are_active() {
        let store = SqliteStore::in_memory("m".to_string()).unwrap();
        raw(&store, "claude-code", "/p/a/s1.jsonl", "s1", "-a", Some(1));
        raw(&store, "claude-code", "/p/a/s1.jsonl", "s1", "-a", Some(5));
        raw(&store, "claude-code", "/p/a/s2.jsonl", "s2", "-a", Some(12));
        raw(&store, "claude-code", "/p/b/s3.jsonl", "s3", "-b", Some(6));
        raw(&store, "claude-code", "/p/b/s4.jsonl", "s4", "-b", Some(2));
        raw(&store, "claude-code", "/p/b/s5.jsonl", "s5", "-b", None);
        raw(
            &store,
            "claude-code",
            "/p/b/s3/subagents/agent-x.jsonl",
            "s3/x",
            "-b",
            Some(6),
        );
        raw(&store, "codex", "/c/s6.jsonl", "s6", "-b", Some(6));

        let mut active = store
            .claude_code_sessions_between(at(4), at(12), None)
            .unwrap();
        active.sort_by_key(|s| s.project_path.clone());
        assert_eq!(
            active,
            vec![
                ActiveSession {
                    conversation_id: Conversation::claude_code_id("s1"),
                    project_path: Some("-a".to_string()),
                },
                ActiveSession {
                    conversation_id: Conversation::claude_code_id("s3"),
                    project_path: Some("-b".to_string()),
                },
            ]
        );

        let only_a = store
            .claude_code_sessions_between(at(0), at(13), Some("-a"))
            .unwrap();
        let mut ids: Vec<_> = only_a.into_iter().map(|s| s.conversation_id).collect();
        ids.sort();
        let mut expected = vec![
            Conversation::claude_code_id("s1"),
            Conversation::claude_code_id("s2"),
        ];
        expected.sort();
        assert_eq!(ids, expected);
    }

    #[test]
    fn memories_from_a_session_are_its_transcripts_and_its_notes() {
        let store = SqliteStore::in_memory("m".to_string()).unwrap();
        let stored = |source: &str, source_id: &str| {
            let memory = Memory::new(
                format!("{source} {source_id}"),
                MemoryType::Episodic,
                source.to_string(),
                source_id.to_string(),
            );
            store.store_memory(&memory).unwrap();
            memory.id
        };
        let main = stored("claude-code", "/h/.claude/projects/-a/s1.jsonl");
        let subagent = stored(
            "claude-code",
            "/h/.claude/projects/-a/s1/subagents/agent-x.jsonl",
        );
        let note = stored("agent-note", "s1");
        let other = stored("claude-code", "/h/.claude/projects/-a/s2.jsonl");
        let longer = stored("claude-code", "/h/.claude/projects/-a/s1x.jsonl");
        let prefixed = stored("agent-note", "xs1");
        let unknown = uuid::Uuid::new_v4();

        let found = store
            .memories_from_session(
                &[main, subagent, note, other, longer, prefixed, unknown],
                "s1",
            )
            .unwrap();
        let expected: std::collections::HashSet<Uuid> = [main, subagent, note].into();
        assert_eq!(found, expected);
        assert!(store.memories_from_session(&[], "s1").unwrap().is_empty());
    }

    #[test]
    fn the_session_of_a_transcript_path_is_its_stem_or_the_dir_above_subagents() {
        for (path, session) in [
            ("/p/-w/abc.jsonl", Some("abc")),
            ("/p/-w/abc/subagents/agent-1.jsonl", Some("abc")),
            ("abc.jsonl", Some("abc")),
            ("abc", Some("abc")),
            ("subagents/agent-1.jsonl", None),
            ("", None),
        ] {
            assert_eq!(session_of(path).as_deref(), session, "{path:?}");
        }
    }
}