toolpath-claude 0.6.2

Derive Toolpath provenance documents from Claude conversation logs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use crate::error::{ConvoError, Result};
use crate::types::{Conversation, ConversationEntry, HistoryEntry};
use std::fs::File;
use std::io::{BufRead, BufReader, Seek, SeekFrom};
use std::path::Path;

pub struct ConversationReader;

impl ConversationReader {
    pub fn read_conversation<P: AsRef<Path>>(path: P) -> Result<Conversation> {
        let path = path.as_ref();
        if !path.exists() {
            return Err(ConvoError::ConversationNotFound(path.display().to_string()));
        }

        let file = File::open(path)?;
        let reader = BufReader::new(file);

        let session_id = path
            .file_stem()
            .and_then(|s| s.to_str())
            .ok_or_else(|| ConvoError::InvalidFormat(path.to_path_buf()))?
            .to_string();

        let mut conversation = Conversation::new(session_id);

        for (line_num, line) in reader.lines().enumerate() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }

            // Try to parse as a conversation entry
            match serde_json::from_str::<ConversationEntry>(&line) {
                Ok(entry) => {
                    // Only add entries with valid UUIDs (skip metadata entries)
                    if !entry.uuid.is_empty() {
                        conversation.add_entry(entry);
                    }
                }
                Err(_) => {
                    // Try to parse as a generic JSON value to check the type
                    if let Ok(value) = serde_json::from_str::<serde_json::Value>(&line)
                        && let Some(entry_type) = value.get("type").and_then(|t| t.as_str())
                    {
                        // Known metadata types we can safely ignore
                        if entry_type == "file-history-snapshot" {
                            // Silently skip file snapshots
                            continue;
                        }
                    }

                    // Only warn about truly unexpected parse failures
                    if line_num < 5 || std::env::var("CLAUDE_CLI_DEBUG").is_ok() {
                        eprintln!(
                            "Warning: Failed to parse line {} in {:?}: entry type not recognized",
                            line_num + 1,
                            path.file_name().unwrap_or_default()
                        );
                    }
                }
            }
        }

        Ok(conversation)
    }

    pub fn read_conversation_metadata<P: AsRef<Path>>(
        path: P,
    ) -> Result<crate::types::ConversationMetadata> {
        let path = path.as_ref();
        if !path.exists() {
            return Err(ConvoError::ConversationNotFound(path.display().to_string()));
        }

        let session_id = path
            .file_stem()
            .and_then(|s| s.to_str())
            .ok_or_else(|| ConvoError::InvalidFormat(path.to_path_buf()))?
            .to_string();

        let file = File::open(path)?;
        let reader = BufReader::new(file);

        let mut message_count = 0;
        let mut started_at = None;
        let mut last_activity = None;
        let mut project_path = String::new();

        for line in reader.lines() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }

            // Try to parse as ConversationEntry, skip if it fails (likely a metadata entry)
            if let Ok(entry) = serde_json::from_str::<ConversationEntry>(&line) {
                // Only process entries with valid UUIDs
                if !entry.uuid.is_empty() {
                    if entry.message.is_some() {
                        message_count += 1;
                    }

                    if project_path.is_empty()
                        && let Some(cwd) = entry.cwd
                    {
                        project_path = cwd;
                    }

                    if !entry.timestamp.is_empty()
                        && let Ok(timestamp) =
                            entry.timestamp.parse::<chrono::DateTime<chrono::Utc>>()
                    {
                        if started_at.is_none() || Some(timestamp) < started_at {
                            started_at = Some(timestamp);
                        }
                        if last_activity.is_none() || Some(timestamp) > last_activity {
                            last_activity = Some(timestamp);
                        }
                    }
                }
            }
        }

        Ok(crate::types::ConversationMetadata {
            session_id,
            project_path,
            file_path: path.to_path_buf(),
            message_count,
            started_at,
            last_activity,
        })
    }

    pub fn read_history<P: AsRef<Path>>(path: P) -> Result<Vec<HistoryEntry>> {
        let path = path.as_ref();
        if !path.exists() {
            return Ok(Vec::new());
        }

        let file = File::open(path)?;
        let reader = BufReader::new(file);
        let mut history = Vec::new();

        for line in reader.lines() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }

            match serde_json::from_str::<HistoryEntry>(&line) {
                Ok(entry) => history.push(entry),
                Err(e) => {
                    eprintln!("Warning: Failed to parse history line: {}", e);
                }
            }
        }

        Ok(history)
    }

    /// Read conversation entries starting from a byte offset.
    /// Returns the new entries and the new byte offset (end of file position).
    ///
    /// This is used for incremental reading - call with offset=0 initially,
    /// then use the returned offset for subsequent calls to only read new entries.
    pub fn read_from_offset<P: AsRef<Path>>(
        path: P,
        byte_offset: u64,
    ) -> Result<(Vec<ConversationEntry>, u64)> {
        let path = path.as_ref();
        if !path.exists() {
            return Err(ConvoError::ConversationNotFound(path.display().to_string()));
        }

        let mut file = File::open(path)?;
        let file_len = file.metadata()?.len();

        // If offset is beyond file length, file may have been truncated/rotated
        // Return empty with current file length as new offset
        if byte_offset > file_len {
            return Ok((Vec::new(), file_len));
        }

        // Seek to the offset
        file.seek(SeekFrom::Start(byte_offset))?;

        let reader = BufReader::new(file);
        let mut entries = Vec::new();
        let mut current_offset = byte_offset;

        for line in reader.lines() {
            let line = line?;
            // Track offset: line length + newline character
            current_offset += line.len() as u64 + 1;

            if line.trim().is_empty() {
                continue;
            }

            // Try to parse as a conversation entry
            if let Ok(entry) = serde_json::from_str::<ConversationEntry>(&line) {
                // Only add entries with valid UUIDs (skip metadata entries)
                if !entry.uuid.is_empty() {
                    entries.push(entry);
                }
            }
            // Silently skip unparseable lines (metadata, file-history-snapshot, etc.)
        }

        Ok((entries, current_offset))
    }

    /// Read the first session_id found in a conversation file.
    ///
    /// Scans at most 10 lines, returning the first non-empty `session_id`
    /// field from a parseable `ConversationEntry`. Returns `None` if the
    /// file doesn't exist, can't be read, or has no session_id in the
    /// first 10 lines.
    pub fn read_first_session_id<P: AsRef<Path>>(path: P) -> Option<String> {
        let file = File::open(path.as_ref()).ok()?;
        let reader = BufReader::new(file);

        for line in reader.lines().take(10) {
            let line = line.ok()?;
            if line.trim().is_empty() {
                continue;
            }
            if let Ok(entry) = serde_json::from_str::<ConversationEntry>(&line)
                && let Some(sid) = &entry.session_id
                && !sid.is_empty()
            {
                return Some(sid.clone());
            }
        }
        None
    }

    /// Get the current file size for a conversation file.
    /// Useful for checking if a file has grown since last read.
    pub fn file_size<P: AsRef<Path>>(path: P) -> Result<u64> {
        let path = path.as_ref();
        if !path.exists() {
            return Err(ConvoError::ConversationNotFound(path.display().to_string()));
        }
        Ok(std::fs::metadata(path)?.len())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_read_conversation() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"123","timestamp":"2024-01-01T00:00:00Z","sessionId":"test","message":{{"role":"user","content":"Hello"}}}}"#
        )
        .unwrap();
        writeln!(
            temp,
            r#"{{"type":"assistant","uuid":"456","timestamp":"2024-01-01T00:00:01Z","sessionId":"test","message":{{"role":"assistant","content":"Hi there"}}}}"#
        )
        .unwrap();
        temp.flush().unwrap();

        let convo = ConversationReader::read_conversation(temp.path()).unwrap();
        assert_eq!(convo.entries.len(), 2);
        assert_eq!(convo.message_count(), 2);
        assert_eq!(convo.user_messages().len(), 1);
        assert_eq!(convo.assistant_messages().len(), 1);
    }

    #[test]
    fn test_read_history() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(
            temp,
            r#"{{"display":"Test query","pastedContents":{{}},"timestamp":1234567890,"project":"/test/project","sessionId":"session-123"}}"#
        )
        .unwrap();
        temp.flush().unwrap();

        let history = ConversationReader::read_history(temp.path()).unwrap();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].display, "Test query");
        assert_eq!(history[0].project, Some("/test/project".to_string()));
    }

    #[test]
    fn test_read_history_nonexistent() {
        let history = ConversationReader::read_history("/nonexistent/file.jsonl").unwrap();
        assert!(history.is_empty());
    }

    #[test]
    fn test_read_conversation_metadata() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","cwd":"/my/project","message":{{"role":"user","content":"Hello"}}}}"#
        ).unwrap();
        writeln!(
            temp,
            r#"{{"type":"assistant","uuid":"u2","timestamp":"2024-01-01T00:01:00Z","message":{{"role":"assistant","content":"Hi"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let meta = ConversationReader::read_conversation_metadata(temp.path()).unwrap();
        assert_eq!(meta.message_count, 2);
        assert_eq!(meta.project_path, "/my/project");
        assert!(meta.started_at.is_some());
        assert!(meta.last_activity.is_some());
    }

    #[test]
    fn test_read_conversation_metadata_nonexistent() {
        let result = ConversationReader::read_conversation_metadata("/nonexistent/file.jsonl");
        assert!(result.is_err());
    }

    #[test]
    fn test_read_from_offset_initial() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hello"}}}}"#
        ).unwrap();
        writeln!(
            temp,
            r#"{{"type":"assistant","uuid":"u2","timestamp":"2024-01-01T00:00:01Z","message":{{"role":"assistant","content":"Hi"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let (entries, new_offset) = ConversationReader::read_from_offset(temp.path(), 0).unwrap();
        assert_eq!(entries.len(), 2);
        assert!(new_offset > 0);
    }

    #[test]
    fn test_read_from_offset_incremental() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hello"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let (entries1, offset1) = ConversationReader::read_from_offset(temp.path(), 0).unwrap();
        assert_eq!(entries1.len(), 1);

        // Append another entry
        writeln!(
            temp,
            r#"{{"type":"assistant","uuid":"u2","timestamp":"2024-01-01T00:00:01Z","message":{{"role":"assistant","content":"Hi"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let (entries2, _) = ConversationReader::read_from_offset(temp.path(), offset1).unwrap();
        assert_eq!(entries2.len(), 1);
        assert_eq!(entries2[0].uuid, "u2");
    }

    #[test]
    fn test_read_from_offset_past_eof() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(temp, r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hi"}}}}"#).unwrap();
        temp.flush().unwrap();

        let (entries, _) = ConversationReader::read_from_offset(temp.path(), 99999).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn test_read_from_offset_nonexistent() {
        let result = ConversationReader::read_from_offset("/nonexistent/file.jsonl", 0);
        assert!(result.is_err());
    }

    #[test]
    fn test_file_size() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(temp, "some content").unwrap();
        temp.flush().unwrap();

        let size = ConversationReader::file_size(temp.path()).unwrap();
        assert!(size > 0);
    }

    #[test]
    fn test_file_size_nonexistent() {
        let result = ConversationReader::file_size("/nonexistent/file.jsonl");
        assert!(result.is_err());
    }

    #[test]
    fn test_read_conversation_nonexistent() {
        let result = ConversationReader::read_conversation("/nonexistent/file.jsonl");
        assert!(result.is_err());
    }

    #[test]
    fn test_read_conversation_skips_empty_uuid() {
        let mut temp = NamedTempFile::new().unwrap();
        // Entry with empty UUID (metadata) should be skipped
        writeln!(
            temp,
            r#"{{"type":"init","uuid":"","timestamp":"2024-01-01T00:00:00Z"}}"#
        )
        .unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hi"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let convo = ConversationReader::read_conversation(temp.path()).unwrap();
        assert_eq!(convo.entries.len(), 1);
    }

    #[test]
    fn test_read_conversation_skips_file_history_snapshot() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(temp, r#"{{"type":"file-history-snapshot","data":{{}}}}"#).unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hi"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let convo = ConversationReader::read_conversation(temp.path()).unwrap();
        assert_eq!(convo.entries.len(), 1);
    }

    #[test]
    fn test_read_conversation_handles_unknown_type() {
        let mut temp = NamedTempFile::new().unwrap();
        // Unknown type that isn't file-history-snapshot
        writeln!(temp, r#"{{"type":"some-unknown-type","data":"whatever"}}"#).unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hi"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let convo = ConversationReader::read_conversation(temp.path()).unwrap();
        assert_eq!(convo.entries.len(), 1);
    }

    #[test]
    fn test_read_conversation_metadata_empty_file() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(temp).unwrap(); // Just blank lines
        temp.flush().unwrap();

        let meta = ConversationReader::read_conversation_metadata(temp.path()).unwrap();
        assert_eq!(meta.message_count, 0);
        assert!(meta.started_at.is_none());
        assert!(meta.last_activity.is_none());
    }

    #[test]
    fn test_read_from_offset_skips_metadata() {
        let mut temp = NamedTempFile::new().unwrap();
        // Metadata entry with empty UUID
        writeln!(
            temp,
            r#"{{"type":"init","uuid":"","timestamp":"2024-01-01T00:00:00Z"}}"#
        )
        .unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hi"}}}}"#
        ).unwrap();
        temp.flush().unwrap();

        let (entries, _) = ConversationReader::read_from_offset(temp.path(), 0).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].uuid, "u1");
    }

    #[test]
    fn test_read_first_session_id() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","sessionId":"sess-abc","message":{{"role":"user","content":"Hi"}}}}"#
        )
        .unwrap();
        temp.flush().unwrap();

        let sid = ConversationReader::read_first_session_id(temp.path());
        assert_eq!(sid, Some("sess-abc".to_string()));
    }

    #[test]
    fn test_read_first_session_id_no_session_id() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hi"}}}}"#
        )
        .unwrap();
        temp.flush().unwrap();

        let sid = ConversationReader::read_first_session_id(temp.path());
        assert!(sid.is_none());
    }

    #[test]
    fn test_read_first_session_id_nonexistent() {
        let sid = ConversationReader::read_first_session_id("/nonexistent/file.jsonl");
        assert!(sid.is_none());
    }

    #[test]
    fn test_read_conversation_handles_blank_lines() {
        let mut temp = NamedTempFile::new().unwrap();
        writeln!(temp).unwrap(); // blank line
        writeln!(
            temp,
            r#"{{"type":"user","uuid":"u1","timestamp":"2024-01-01T00:00:00Z","message":{{"role":"user","content":"Hi"}}}}"#
        ).unwrap();
        writeln!(temp).unwrap(); // blank line
        temp.flush().unwrap();

        let convo = ConversationReader::read_conversation(temp.path()).unwrap();
        assert_eq!(convo.entries.len(), 1);
    }
}