Skip to main content

second_brain_adapters/
claude_code.rs

1use std::collections::HashSet;
2use std::path::PathBuf;
3
4use anyhow::{Context, Result};
5use chrono::{DateTime, Utc};
6use uuid::Uuid;
7
8use second_brain_core::schema::{Conversation, Memory, MemoryType};
9
10use crate::{Adapter, ConversationSource, ExtractedEntity, IngestResult};
11
12pub struct ClaudeCodeAdapter {
13    base_path: PathBuf,
14    machine_id: String,
15}
16
17impl ClaudeCodeAdapter {
18    pub fn new(machine_id: String) -> Self {
19        let base_path = dirs_home().join(".claude");
20        Self {
21            base_path,
22            machine_id,
23        }
24    }
25
26    pub fn with_path(base_path: PathBuf, machine_id: String) -> Self {
27        Self {
28            base_path,
29            machine_id,
30        }
31    }
32
33    #[allow(clippy::type_complexity)]
34    fn parse_conversation_file(
35        &self,
36        path: &PathBuf,
37    ) -> Result<(
38        Vec<Memory>,
39        Vec<(ExtractedEntity, Uuid)>,
40        Option<DateTime<Utc>>,
41    )> {
42        let content = std::fs::read_to_string(path)
43            .with_context(|| format!("reading conversation file: {}", path.display()))?;
44
45        let mut memories = Vec::new();
46        let mut all_entities: Vec<(ExtractedEntity, Uuid)> = Vec::new();
47        let mut seen_entities: HashSet<String> = HashSet::new();
48        let mut first_timestamp: Option<DateTime<Utc>> = None;
49        let source_id = path.to_string_lossy().to_string();
50
51        for line in content.lines() {
52            if line.trim().is_empty() {
53                continue;
54            }
55
56            let msg: serde_json::Value = match serde_json::from_str(line) {
57                Ok(v) => v,
58                Err(_) => continue,
59            };
60
61            let msg_type = msg.get("type").and_then(|t| t.as_str()).unwrap_or("");
62
63            if first_timestamp.is_none()
64                && let Some(ts) = msg.get("timestamp").and_then(|t| t.as_str())
65            {
66                first_timestamp = DateTime::parse_from_rfc3339(ts)
67                    .map(|dt| dt.with_timezone(&Utc))
68                    .ok();
69            }
70
71            if msg_type != "assistant" {
72                continue;
73            }
74
75            let message = match msg.get("message") {
76                Some(m) => m,
77                None => continue,
78            };
79
80            let content_blocks = match message.get("content").and_then(|c| c.as_array()) {
81                Some(blocks) => blocks,
82                None => continue,
83            };
84
85            for block in content_blocks {
86                let block_type = block.get("type").and_then(|t| t.as_str()).unwrap_or("");
87
88                let text = match block_type {
89                    "text" => block.get("text").and_then(|t| t.as_str()),
90                    _ => None,
91                };
92
93                if let Some(text) = text {
94                    let chunks = chunk_text(text);
95                    for chunk in chunks {
96                        if !is_memorable(&chunk) {
97                            continue;
98                        }
99                        let memory_type = classify_memory(&chunk);
100                        let memory = Memory::new(
101                            chunk.clone(),
102                            memory_type,
103                            "claude-code".to_string(),
104                            source_id.clone(),
105                        );
106                        let memory_id = memory.id;
107                        memories.push(memory);
108
109                        for entity in extract_entities(&chunk) {
110                            let key = entity.name.to_lowercase();
111                            if seen_entities.insert(key) {
112                                all_entities.push((entity, memory_id));
113                            }
114                        }
115                    }
116                }
117            }
118        }
119
120        Ok((memories, all_entities, first_timestamp))
121    }
122}
123
124impl Adapter for ClaudeCodeAdapter {
125    fn name(&self) -> &str {
126        "claude-code"
127    }
128
129    fn discover_conversations(&self) -> Result<Vec<ConversationSource>> {
130        let projects_dir = self.base_path.join("projects");
131        let mut sources = Vec::new();
132
133        if !projects_dir.exists() {
134            return Ok(sources);
135        }
136
137        let mut dirs = vec![projects_dir];
138        while let Some(dir) = dirs.pop() {
139            for entry in std::fs::read_dir(&dir)? {
140                let entry = entry?;
141                let path = entry.path();
142                if path.is_dir() {
143                    dirs.push(path);
144                } else if path.extension().is_some_and(|e| e == "jsonl") {
145                    sources.push(ConversationSource {
146                        id: path.to_string_lossy().to_string(),
147                        path: path.to_string_lossy().to_string(),
148                        source_name: "claude-code".to_string(),
149                        machine_id: self.machine_id.clone(),
150                    });
151                }
152            }
153        }
154
155        Ok(sources)
156    }
157
158    fn ingest_conversation(&self, source: &ConversationSource) -> Result<IngestResult> {
159        let path = PathBuf::from(&source.path);
160        let (memories, entities, started_at) = self.parse_conversation_file(&path)?;
161
162        let project_dir = path
163            .parent()
164            .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()));
165
166        let conversation = Conversation {
167            id: Uuid::new_v4(),
168            source: "claude-code".to_string(),
169            machine_id: source.machine_id.clone(),
170            started_at: started_at.unwrap_or_else(Utc::now),
171            project_path: project_dir.clone(),
172        };
173
174        let mut memories = memories;
175        for mem in &mut memories {
176            mem.project_path = project_dir.clone();
177        }
178
179        let entity_count = entities.len();
180
181        Ok(IngestResult {
182            conversation,
183            memories,
184            entities,
185            entities_extracted: entity_count,
186        })
187    }
188}
189
190fn dirs_home() -> PathBuf {
191    std::env::var("HOME")
192        .map(PathBuf::from)
193        .unwrap_or_else(|_| PathBuf::from("/tmp"))
194}
195
196fn chunk_text(text: &str) -> Vec<String> {
197    let mut chunks = Vec::new();
198    let mut current = String::new();
199
200    for paragraph in text.split("\n\n") {
201        let trimmed = paragraph.trim();
202        if trimmed.is_empty() {
203            continue;
204        }
205
206        if current.len() + trimmed.len() > 1500 && !current.is_empty() {
207            chunks.push(std::mem::take(&mut current));
208        }
209
210        if !current.is_empty() {
211            current.push_str("\n\n");
212        }
213        current.push_str(trimmed);
214
215        if current.len() >= 800 {
216            chunks.push(std::mem::take(&mut current));
217        }
218    }
219
220    if !current.is_empty() {
221        chunks.push(current);
222    }
223
224    if chunks.is_empty() && !text.trim().is_empty() {
225        chunks.push(text.trim().to_string());
226    }
227
228    chunks
229}
230
231fn is_memorable(text: &str) -> bool {
232    let trimmed = text.trim();
233
234    if trimmed.len() < 80 {
235        return false;
236    }
237
238    let code_block_count = trimmed.matches("```").count() / 2;
239    if code_block_count > 8 {
240        return false;
241    }
242
243    let lines: Vec<&str> = trimmed.lines().collect();
244    let non_code_lines: Vec<&str> = lines
245        .iter()
246        .filter(|l| {
247            let t = l.trim();
248            !t.is_empty()
249                && !t.starts_with("```")
250                && !t.starts_with('\t')
251                && !t.starts_with("//")
252                && !t.starts_with("  ")
253        })
254        .copied()
255        .collect();
256
257    let word_count = trimmed.split_whitespace().count();
258    if non_code_lines.len() < 2 && word_count < 15 {
259        return false;
260    }
261
262    let lower = trimmed.to_lowercase();
263
264    let first_line_lower = non_code_lines
265        .first()
266        .map(|l| l.trim().to_lowercase())
267        .unwrap_or_default();
268
269    if is_noise_opening(&first_line_lower) && !has_substance(&lower) {
270        return false;
271    }
272
273    let noise_count = count_noise_phrases(&lower);
274
275    if noise_count >= 2 {
276        return false;
277    }
278
279    if noise_count == 1 && non_code_lines.len() < 4 {
280        return false;
281    }
282
283    if is_pure_narration(&lower) && non_code_lines.len() < 5 {
284        return false;
285    }
286
287    true
288}
289
290fn is_noise_opening(first_line: &str) -> bool {
291    let patterns = [
292        "let me ",
293        "i'll ",
294        "i will ",
295        "now let me",
296        "now i'll",
297        "let's ",
298        "good question",
299        "good, ",
300        "good ",
301        "great, ",
302        "perfect",
303        "excellent",
304        "alright",
305        "ok, ",
306        "okay, ",
307        "sure, ",
308        "certainly",
309        "absolutely",
310        "interesting",
311        "i see",
312        "i notice",
313        "i can see",
314        "looking at",
315        "checking ",
316        "exploring ",
317        "examining ",
318        "navigating ",
319        "reading ",
320    ];
321    patterns.iter().any(|p| first_line.starts_with(p))
322}
323
324fn has_substance(text: &str) -> bool {
325    let markers = [
326        "because",
327        "the fix",
328        "the solution",
329        "the issue",
330        "the problem",
331        "decided",
332        "trade-off",
333        "tradeoff",
334        "architecture",
335        "we chose",
336        "root cause",
337        "the reason",
338        "this means",
339        "this works",
340        "the approach",
341        "conclusion",
342        "in summary",
343    ];
344    markers.iter().any(|m| text.contains(m))
345}
346
347fn count_noise_phrases(text: &str) -> usize {
348    let phrases = [
349        "let me check",
350        "let me look",
351        "let me explore",
352        "let me examine",
353        "let me navigate",
354        "let me read",
355        "let me search",
356        "let me find",
357        "let me verify",
358        "let me investigate",
359        "let me continue",
360        "let me review",
361        "let me start by",
362        "let me run",
363        "i'll check",
364        "i'll look",
365        "i'll explore",
366        "i'll examine",
367        "i'll search",
368        "i'll read",
369        "i'll investigate",
370        "i'll start by",
371        "i'll run",
372        "now let me",
373    ];
374    phrases.iter().filter(|p| text.contains(*p)).count()
375}
376
377fn is_pure_narration(text: &str) -> bool {
378    let patterns = [
379        "i have the project context",
380        "i have a complete picture",
381        "mark the exploration task",
382        "mark the task",
383        "committed changes:",
384        "let me mark",
385        "task done",
386        "task complete",
387    ];
388    patterns.iter().any(|p| text.contains(p))
389}
390
391fn classify_memory(text: &str) -> MemoryType {
392    let lower = text.to_lowercase();
393
394    let task_markers = [
395        "todo:",
396        "todo ",
397        "fixme",
398        "action item",
399        "remaining:",
400        "left to do",
401        "need to implement",
402        "needs to be done",
403        "next step:",
404        "next steps:",
405        "backlog:",
406    ];
407    if task_markers.iter().any(|m| lower.contains(m)) {
408        return MemoryType::Task;
409    }
410
411    let decision_markers = [
412        "decided",
413        "chose",
414        "went with",
415        "picked",
416        "agreed",
417        "choosing",
418        "selected",
419        "switched to",
420        "we'll use",
421        "we'll go with",
422        "the decision",
423        "opting for",
424    ];
425    if decision_markers.iter().any(|m| lower.contains(m)) {
426        return MemoryType::Decision;
427    }
428
429    let architecture_markers = [
430        "architecture",
431        "data model",
432        "schema design",
433        "the approach is",
434        "system design",
435        "component design",
436        "abstraction",
437        "design pattern",
438        "data flow",
439        "the pattern",
440        "module structure",
441        "separation of concern",
442    ];
443    if architecture_markers.iter().any(|m| lower.contains(m)) {
444        return MemoryType::Architecture;
445    }
446
447    let debugging_markers = [
448        "root cause",
449        "the bug",
450        "the issue is",
451        "the problem is",
452        "stack trace",
453        "error was",
454        "the fix is",
455        "debugging",
456        "regression",
457        "crash",
458        "panicked",
459        "segfault",
460    ];
461    if debugging_markers.iter().any(|m| lower.contains(m)) {
462        return MemoryType::Debugging;
463    }
464
465    if lower.contains('?') {
466        let interrogative = [
467            "how ",
468            "what ",
469            "why ",
470            "whether ",
471            "should we",
472            "could we",
473            "where ",
474            "when ",
475            "which ",
476        ];
477        if interrogative.iter().any(|m| lower.contains(m)) {
478            return MemoryType::Question;
479        }
480    }
481
482    let procedural_markers = [
483        "to do this",
484        "steps:",
485        "step 1",
486        "first,",
487        "then,",
488        "finally,",
489        "run ",
490        "install ",
491        "execute",
492        "command:",
493        "configure",
494        "set up",
495        "to fix",
496        "solution:",
497        "you need to",
498        "you can ",
499        "you should",
500        "here's how",
501    ];
502    if procedural_markers.iter().any(|m| lower.contains(m)) {
503        return MemoryType::Procedural;
504    }
505
506    let episodic_markers = [
507        "migrated",
508        "deployed",
509        "shipped",
510        "found that",
511        "discovered",
512        "realized",
513        "confirmed",
514        "concluded",
515    ];
516    if episodic_markers.iter().any(|m| lower.contains(m)) {
517        return MemoryType::Episodic;
518    }
519
520    MemoryType::Semantic
521}
522
523#[cfg(test)]
524mod tests {
525    use super::*;
526
527    #[test]
528    fn classifies_task() {
529        assert_eq!(
530            classify_memory("TODO: implement the retry logic for failed sync batches"),
531            MemoryType::Task
532        );
533        assert_eq!(
534            classify_memory(
535                "There's a FIXME in the auth module that needs addressing before release"
536            ),
537            MemoryType::Task
538        );
539        assert_eq!(
540            classify_memory(
541                "Action item from the review: add rate limiting to the ingest endpoint"
542            ),
543            MemoryType::Task
544        );
545    }
546
547    #[test]
548    fn classifies_decision() {
549        assert_eq!(
550            classify_memory(
551                "We decided to use KuzuDB instead of Neo4j because it embeds and needs no separate server"
552            ),
553            MemoryType::Decision
554        );
555        assert_eq!(
556            classify_memory("Chose axum over actix-web for the HTTP framework"),
557            MemoryType::Decision
558        );
559        assert_eq!(
560            classify_memory(
561                "Went with a daemon architecture so only one process holds the DB lock"
562            ),
563            MemoryType::Decision
564        );
565    }
566
567    #[test]
568    fn classifies_architecture() {
569        assert_eq!(
570            classify_memory(
571                "The architecture uses a single daemon that owns the database and serves queries over HTTP"
572            ),
573            MemoryType::Architecture
574        );
575        assert_eq!(
576            classify_memory(
577                "The data model separates memories from entities with MENTIONS relations between them"
578            ),
579            MemoryType::Architecture
580        );
581        assert_eq!(
582            classify_memory("The design pattern here is event sourcing via the SyncLog table"),
583            MemoryType::Architecture
584        );
585    }
586
587    #[test]
588    fn classifies_debugging() {
589        assert_eq!(
590            classify_memory(
591                "The root cause was a race condition in the file watcher debounce logic"
592            ),
593            MemoryType::Debugging
594        );
595        assert_eq!(
596            classify_memory(
597                "The issue is that KuzuDB returns an error when the embedding array is empty"
598            ),
599            MemoryType::Debugging
600        );
601        assert_eq!(
602            classify_memory(
603                "The bug manifests as a 413 error because axum has a default 2MB body limit"
604            ),
605            MemoryType::Debugging
606        );
607    }
608
609    #[test]
610    fn classifies_question() {
611        assert_eq!(
612            classify_memory(
613                "How should we handle the case where the LLM is unreachable during consolidation?"
614            ),
615            MemoryType::Question
616        );
617        assert_eq!(
618            classify_memory(
619                "What happens when two machines sync conflicting edits to the same memory?"
620            ),
621            MemoryType::Question
622        );
623    }
624
625    #[test]
626    fn classifies_procedural() {
627        assert_eq!(
628            classify_memory(
629                "To fix the auth issue you need to regenerate the token and update the environment variable"
630            ),
631            MemoryType::Procedural
632        );
633        assert_eq!(
634            classify_memory(
635                "Steps: first install the dependency, then configure the TOML file, finally restart the daemon"
636            ),
637            MemoryType::Procedural
638        );
639    }
640
641    #[test]
642    fn classifies_episodic() {
643        assert_eq!(
644            classify_memory(
645                "Deployed the new sync module to the Raspberry Pi and it handled 50k records without issues"
646            ),
647            MemoryType::Episodic
648        );
649        assert_eq!(
650            classify_memory(
651                "Discovered that the embedding model outputs 384 dimensions, not 1536 as documented"
652            ),
653            MemoryType::Episodic
654        );
655    }
656
657    #[test]
658    fn classifies_semantic_fallback() {
659        assert_eq!(
660            classify_memory(
661                "KuzuDB stores graph data in a columnar format optimized for analytical queries"
662            ),
663            MemoryType::Semantic
664        );
665        assert_eq!(
666            classify_memory(
667                "BGE-small-en-v1.5 produces 384-dimensional vectors suitable for semantic search"
668            ),
669            MemoryType::Semantic
670        );
671    }
672
673    #[test]
674    fn rejects_short_text() {
675        assert!(!is_memorable("short"));
676        assert!(!is_memorable("This is under eighty characters for sure."));
677    }
678
679    #[test]
680    fn rejects_noise_openings_without_substance() {
681        assert!(!is_memorable(
682            "Let me check the file structure to understand what we're working with here. I'll explore the codebase now."
683        ));
684        assert!(!is_memorable(
685            "I'll look at the configuration files and see what's going on in the project directory structure for this."
686        ));
687        assert!(!is_memorable(
688            "Looking at the codebase now. Let me navigate through the directory to find the relevant source files we need."
689        ));
690    }
691
692    #[test]
693    fn keeps_noise_opening_with_substance() {
694        assert!(is_memorable(
695            "Let me explain the root cause of this bug. The issue is that the token expiry check uses less-than instead of less-than-or-equal, which means tokens expire one second early. The fix is straightforward."
696        ));
697    }
698
699    #[test]
700    fn rejects_multiple_noise_phrases() {
701        assert!(!is_memorable(
702            "Let me check the logs. I'll also look at the configuration. Let me explore the routes to understand the full picture of this system."
703        ));
704    }
705
706    #[test]
707    fn rejects_pure_narration() {
708        assert!(!is_memorable(
709            "Task complete. Committed changes: updated the auth middleware to fix the token validation."
710        ));
711        assert!(!is_memorable(
712            "Let me mark the exploration task as done now that we've reviewed all the relevant files in the project."
713        ));
714    }
715
716    #[test]
717    fn keeps_architectural_decisions() {
718        assert!(is_memorable(
719            "We decided to use a daemon architecture because KuzuDB takes an exclusive file lock. Only one process can open the database at a time, so we need a single daemon that owns the DB and serves everything else over HTTP."
720        ));
721    }
722
723    #[test]
724    fn keeps_debugging_insights() {
725        assert!(is_memorable(
726            "Found that the 413 error was caused by axum's default 2MB body limit. The sync payload for 115k records exceeds that. The fix is adding DefaultBodyLimit::max(64MB) to the router layer."
727        ));
728    }
729
730    #[test]
731    fn keeps_technical_explanations() {
732        assert!(is_memorable(
733            "The paginated export uses raw sequence order instead of sort_for_import because sorting reorders records by type, which breaks cursor-based pagination. The cursor tracks sequence numbers, so records must stay in sequence order."
734        ));
735    }
736
737    #[test]
738    fn rejects_code_heavy_blocks() {
739        let mostly_code = "Here's the fix:\n```rust\nfn main() {\n    println!(\"hello\");\n}\n```\n```rust\nfn other() {\n    println!(\"world\");\n}\n```\n```rust\nfn third() {}\n```\n```rust\nfn fourth() {}\n```\n```rust\nfn fifth() {}\n```\n```rust\nfn sixth() {}\n```\n```rust\nfn seventh() {}\n```\n```rust\nfn eighth() {}\n```\n```rust\nfn ninth() {}\n```";
740        assert!(!is_memorable(mostly_code));
741    }
742
743    #[test]
744    fn rejects_single_noise_phrase_short_message() {
745        assert!(!is_memorable(
746            "Let me verify that the build works correctly before we move on to testing."
747        ));
748    }
749}
750
751const TECH_NAMES: &[&str] = &[
752    "rust",
753    "python",
754    "javascript",
755    "typescript",
756    "go",
757    "java",
758    "c++",
759    "ruby",
760    "swift",
761    "kotlin",
762    "react",
763    "vue",
764    "angular",
765    "svelte",
766    "next.js",
767    "nuxt",
768    "django",
769    "flask",
770    "fastapi",
771    "express",
772    "axum",
773    "actix",
774    "tokio",
775    "serde",
776    "docker",
777    "kubernetes",
778    "k8s",
779    "terraform",
780    "ansible",
781    "nginx",
782    "apache",
783    "redis",
784    "postgres",
785    "postgresql",
786    "mysql",
787    "sqlite",
788    "mongodb",
789    "kuzudb",
790    "neo4j",
791    "elasticsearch",
792    "kafka",
793    "rabbitmq",
794    "nats",
795    "grpc",
796    "graphql",
797    "rest",
798    "websocket",
799    "ssh",
800    "tailscale",
801    "wireguard",
802    "git",
803    "github",
804    "gitlab",
805    "linux",
806    "ubuntu",
807    "debian",
808    "macos",
809    "windows",
810    "arm64",
811    "aarch64",
812    "x86",
813    "onnx",
814    "pytorch",
815    "tensorflow",
816    "huggingface",
817    "openai",
818    "anthropic",
819    "claude",
820    "gpt",
821    "llm",
822    "mcp",
823    "lsp",
824    "systemd",
825    "cron",
826    "tmux",
827    "vim",
828    "neovim",
829    "cargo",
830    "npm",
831    "pip",
832    "uv",
833    "bun",
834    "deno",
835    "node.js",
836    "node",
837    "aws",
838    "gcp",
839    "azure",
840    "s3",
841    "ec2",
842    "lambda",
843    "cloudflare",
844    "raspberry pi",
845    "pi",
846    "jetson",
847    "esp32",
848    "arduino",
849    "toml",
850    "yaml",
851    "json",
852    "jsonl",
853    "csv",
854    "protobuf",
855    "msgpack",
856];
857
858fn extract_entities(text: &str) -> Vec<ExtractedEntity> {
859    let mut entities = Vec::new();
860    let mut seen = HashSet::new();
861    let lower = text.to_lowercase();
862
863    for &tech in TECH_NAMES {
864        if lower.contains(tech) {
865            let key = tech.to_lowercase();
866            if seen.insert(key) {
867                entities.push(ExtractedEntity {
868                    name: tech.to_string(),
869                    entity_type: "technology".to_string(),
870                });
871            }
872        }
873    }
874
875    for word in text.split_whitespace() {
876        let clean = word.trim_matches(|c: char| {
877            !c.is_alphanumeric() && c != '/' && c != '.' && c != '-' && c != '_'
878        });
879        if clean.contains('/') && clean.contains('.') {
880            let ext_match = clean.rsplit('.').next().unwrap_or("");
881            let is_source_file = matches!(
882                ext_match,
883                "rs" | "py"
884                    | "ts"
885                    | "tsx"
886                    | "js"
887                    | "jsx"
888                    | "go"
889                    | "java"
890                    | "rb"
891                    | "sh"
892                    | "toml"
893                    | "json"
894                    | "yaml"
895                    | "yml"
896                    | "md"
897                    | "sql"
898                    | "html"
899                    | "css"
900            );
901            if is_source_file {
902                let key = clean.to_lowercase();
903                if seen.insert(key) {
904                    entities.push(ExtractedEntity {
905                        name: clean.to_string(),
906                        entity_type: "file_path".to_string(),
907                    });
908                }
909            }
910        }
911    }
912
913    entities
914}