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