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