open-loops 1.5.0

Recupere o contexto de trabalhos pausados: o que começou, onde parou, qual o próximo passo
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Adapter for Claude Code sessions (~/.claude/projects/<path-encoded>/*.jsonl).
//! WARNING: internal Claude Code format, not a public API — may change.
//! Parsing is therefore tolerant: a bad line is skipped, never aborts (spec risk 1).
use super::{SessionExcerpt, SessionSource};
use crate::index::Index;
use anyhow::Result;
use chrono::{DateTime, Duration, Utc};
use std::path::{Path, PathBuf};

pub struct ClaudeCode {
    pub projects_dir: PathBuf,
}

/// Claude Code encodes the project path by replacing path separators and '.' with '-'.
/// e.g. /home/g/repo/x -> -home-g-repo-x, C:\Users\g\app -> C--Users-g-app
pub fn encode_project_path(p: &Path) -> String {
    let raw = p.to_string_lossy();
    // Windows canonicalize() may add \\?\ — Claude Code encodes the normal path.
    let raw = raw.strip_prefix(r"\\?\").unwrap_or(&raw);
    raw.replace(['/', '\\', '.', ':'], "-")
}

/// Extracts text from a session jsonl line. None for non-message,
/// corrupted, or empty lines (tolerant parsing).
pub fn extract_text(line: &str) -> Option<String> {
    let v: serde_json::Value = serde_json::from_str(line).ok()?;
    let role = v.get("type")?.as_str()?;
    if role != "user" && role != "assistant" {
        return None;
    }
    let content = v.get("message")?.get("content")?;
    let text = match content {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Array(parts) => parts
            .iter()
            .filter_map(|p| p.get("text").and_then(|t| t.as_str()))
            .collect::<Vec<_>>()
            .join("\n"),
        _ => return None,
    };
    let text = text.trim();
    if text.is_empty() {
        None
    } else {
        Some(format!("[{role}] {text}"))
    }
}

/// Reads the last `max_bytes` of the file and extracts message text.
/// The end of the conversation concentrates the "where I left off" signal (spec decision).
fn read_tail_text(path: &Path, max_bytes: u64) -> Result<String> {
    let raw = std::fs::read(path)?;
    let start = raw.len().saturating_sub(max_bytes as usize);
    let tail = String::from_utf8_lossy(&raw[start..]);
    let mut lines = tail.lines();
    if start > 0 {
        lines.next(); // first line may be cut mid-way
    }
    Ok(lines
        .filter_map(extract_text)
        .collect::<Vec<_>>()
        .join("\n"))
}

impl ClaudeCode {
    /// Core excerpt logic, optionally accelerated by an FTS index.
    ///
    /// `None` index = in-memory path (reads bounded tail for every candidate).
    /// `Some(index)` = FTS path (uses `session_mentions` for the mention probe;
    /// still reads the bounded tail for the final selected sessions).
    ///
    /// # Errors
    ///
    /// Returns an error if the project directory cannot be read.
    pub fn excerpts_indexed(
        &self,
        repo_path: &Path,
        branch: &str,
        window: (DateTime<Utc>, DateTime<Utc>),
        max_sessions: usize,
        max_kb: u64,
        index: Option<&Index>,
    ) -> Result<Vec<SessionExcerpt>> {
        let dir = self.projects_dir.join(encode_project_path(repo_path));
        if !dir.is_dir() {
            return Ok(vec![]);
        }
        let pad = Duration::days(7);
        let (start, end) = (window.0 - pad, window.1 + pad);

        // When an index is available, get FTS-based mention set up-front.
        // On index error, fall back to None (in-memory mention probe below).
        let fts_mentions: Option<std::collections::HashSet<PathBuf>> = index.map(|idx| {
            // Upsert every candidate file's bounded tail into the index.
            // This is safe to call here: upsert skips unchanged (path,mtime) rows.
            if let Ok(entries) = std::fs::read_dir(&dir) {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.extension().is_none_or(|e| e != "jsonl") {
                        continue;
                    }
                    let Ok(meta) = entry.metadata() else { continue };
                    let Ok(modified) = meta.modified() else {
                        continue;
                    };
                    let mtime = modified
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_secs() as i64;
                    let size = meta.len() as i64;
                    // Read bounded tail for indexing (skip on read error).
                    if let Ok(tail) = read_tail_text(&path, max_kb * 1024) {
                        idx.upsert_session(&path, repo_path, mtime, size, &tail);
                    }
                }
            }
            idx.session_mentions(repo_path, branch)
        });

        // Collect candidates: (modified, path, in_window, mentions_branch).
        // For in-memory path: read the bounded tail ONCE for both mention probe
        // AND excerpt text (fixes #14 — no unbounded whole-file read).
        struct Candidate {
            modified: DateTime<Utc>,
            path: PathBuf,
            in_window: bool,
            mentions_branch: bool,
            /// Tail text read during candidate collection (in-memory path only).
            tail: Option<String>,
        }

        let mut candidates: Vec<Candidate> = Vec::new();
        for entry in std::fs::read_dir(&dir)?.flatten() {
            let path = entry.path();
            if path.extension().is_none_or(|e| e != "jsonl") {
                continue;
            }
            let Ok(meta) = entry.metadata() else { continue };
            let Ok(modified) = meta.modified() else {
                continue;
            };
            let modified: DateTime<Utc> = modified.into();
            let in_window = modified >= start && modified <= end;

            if let Some(ref fts_set) = fts_mentions {
                // FTS path: mention signal comes from the index, no file read here.
                let mentions_branch = fts_set.contains(&path);
                if in_window || mentions_branch {
                    candidates.push(Candidate {
                        modified,
                        path,
                        in_window,
                        mentions_branch,
                        tail: None,
                    });
                }
            } else {
                // In-memory path: read bounded tail ONCE for mention probe (#14 fix).
                let tail = read_tail_text(&path, max_kb * 1024).unwrap_or_default();
                let mentions_branch = tail.contains(branch);
                if in_window || mentions_branch {
                    candidates.push(Candidate {
                        modified,
                        path,
                        in_window,
                        mentions_branch,
                        tail: Some(tail),
                    });
                }
            }
        }

        // Stable total-order sort: mentions_branch DESC, in_window DESC,
        // modified DESC, path ASC (#15 fix — deterministic tie-break).
        candidates.sort_by(|a, b| {
            b.mentions_branch
                .cmp(&a.mentions_branch)
                .then(b.in_window.cmp(&a.in_window))
                .then(b.modified.cmp(&a.modified))
                .then(a.path.cmp(&b.path))
        });

        // Build output: filter empty-text sessions BEFORE truncate (#15 fix).
        let mut out = Vec::new();
        for cand in candidates {
            if out.len() >= max_sessions {
                break;
            }
            // Get the text: already read (in-memory path) or read now (FTS path).
            let text = if let Some(t) = cand.tail {
                t
            } else {
                read_tail_text(&cand.path, max_kb * 1024).unwrap_or_default()
            };
            if text.is_empty() {
                // Skip empty sessions — do NOT count them toward the max_sessions
                // limit (this is the #15 regression fix).
                continue;
            }
            let source = cand
                .path
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_default();
            out.push(SessionExcerpt {
                source,
                modified: cand.modified,
                text,
                in_window: cand.in_window,
                mentions_branch: cand.mentions_branch,
            });
        }
        Ok(out)
    }
}

impl SessionSource for ClaudeCode {
    /// Excerpts of the sessions most relevant to the branch.
    ///
    /// Delegates to [`ClaudeCode::excerpts_indexed`] with no index (in-memory path).
    ///
    /// # Errors
    ///
    /// Returns an error if the project directory cannot be read.
    fn excerpts(
        &self,
        repo_path: &Path,
        branch: &str,
        window: (DateTime<Utc>, DateTime<Utc>),
        max_sessions: usize,
        max_kb: u64,
    ) -> Result<Vec<SessionExcerpt>> {
        self.excerpts_indexed(repo_path, branch, window, max_sessions, max_kb, None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::index::Index;
    use crate::sessions::SessionSource;
    use chrono::{Duration, Utc};
    use std::path::Path;

    #[test]
    fn encode_project_path_matches_claude_code_format() {
        assert_eq!(
            encode_project_path(Path::new("/home/g/repo/me/open-loops")),
            "-home-g-repo-me-open-loops"
        );
        assert_eq!(
            encode_project_path(Path::new("/home/g/my.app")),
            "-home-g-my-app"
        );
    }

    #[test]
    #[cfg(windows)]
    fn encode_project_path_handles_windows_separators() {
        assert_eq!(
            encode_project_path(Path::new(r"C:\Users\g\app")),
            "C--Users-g-app"
        );
    }

    #[test]
    fn extract_text_captures_user_assistant_and_ignores_rest() {
        let user = r#"{"type":"user","message":{"content":"quero implementar login"}}"#;
        let asst = r#"{"type":"assistant","message":{"content":[{"type":"text","text":"vou criar feat/login"}]}}"#;
        let meta = r#"{"type":"summary","summary":"x"}"#;
        assert_eq!(
            extract_text(user).unwrap(),
            "[user] quero implementar login"
        );
        assert_eq!(
            extract_text(asst).unwrap(),
            "[assistant] vou criar feat/login"
        );
        assert!(extract_text(meta).is_none());
        assert!(extract_text("corrupted non-json line").is_none());
    }

    #[test]
    fn excerpts_selects_by_window_tolerates_garbage_and_limits_count() {
        let tmp = tempfile::tempdir().unwrap();
        let projects = tmp.path().to_path_buf();
        let repo = Path::new("/home/g/app");
        let dir = projects.join(encode_project_path(repo));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("sessao1.jsonl"),
            concat!(
                r#"{"type":"user","message":{"content":"quero implementar login"}}"#, "\n",
                "lixo nao-json\n",
                r#"{"type":"assistant","message":{"content":[{"type":"text","text":"proximo passo: validar token"}]}}"#, "\n",
            ),
        )
        .unwrap();
        // files of other formats are ignored
        std::fs::write(dir.join("nota.txt"), "nada").unwrap();

        let src = ClaudeCode {
            projects_dir: projects,
        };
        let now = Utc::now();
        let window = (now - Duration::days(1), now + Duration::days(1));
        let ex = src.excerpts(repo, "feat/login", window, 3, 50).unwrap();
        assert_eq!(ex.len(), 1);
        assert!(ex[0].text.contains("[user] quero implementar login"));
        assert!(ex[0].text.contains("proximo passo: validar token"));
        assert_eq!(ex[0].source, "sessao1.jsonl");
    }

    #[test]
    fn excerpts_empty_when_project_dir_does_not_exist() {
        let tmp = tempfile::tempdir().unwrap();
        let src = ClaudeCode {
            projects_dir: tmp.path().to_path_buf(),
        };
        let now = Utc::now();
        let ex = src
            .excerpts(Path::new("/nao/existe"), "b", (now, now), 3, 50)
            .unwrap();
        assert!(ex.is_empty());
    }

    #[test]
    fn excerpts_includes_session_outside_window_if_it_mentions_branch() {
        let tmp = tempfile::tempdir().unwrap();
        let projects = tmp.path().to_path_buf();
        let repo = Path::new("/home/g/app");
        let dir = projects.join(encode_project_path(repo));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("antiga.jsonl"),
            concat!(
                r#"{"type":"user","message":{"content":"implementando feat/login agora"}}"#,
                "\n",
            ),
        )
        .unwrap();

        let src = ClaudeCode {
            projects_dir: projects,
        };
        let now = Utc::now();
        // window two years ago — file mtime is now (outside the window)
        let passado = now - Duration::days(730);
        let window = (passado - Duration::days(1), passado);
        let ex = src.excerpts(repo, "feat/login", window, 3, 50).unwrap();
        assert_eq!(ex.len(), 1, "mention heuristic must include the session");
        assert!(ex[0].text.contains("feat/login"));
    }

    #[test]
    fn excerpts_truncates_large_file_and_skips_cut_line() {
        let tmp = tempfile::tempdir().unwrap();
        let projects = tmp.path().to_path_buf();
        let repo = Path::new("/home/g/app");
        let dir = projects.join(encode_project_path(repo));
        std::fs::create_dir_all(&dir).unwrap();

        // padding with summary lines (not extracted) to force file > 1 KB
        let pad_line = format!("{{\"type\":\"summary\",\"x\":\"{}\"}}\n", "A".repeat(80));
        let mut content = pad_line.repeat(15); // ~1500 bytes
        content.push_str(r#"{"type":"user","message":{"content":"contexto final"}}"#);
        content.push('\n');
        assert!(content.len() > 1024);

        std::fs::write(dir.join("grande.jsonl"), &content).unwrap();

        let src = ClaudeCode {
            projects_dir: projects,
        };
        let now = Utc::now();
        let window = (now - Duration::days(1), now + Duration::days(1));
        // max_kb=1 forces truncation: start > 0 → first line of the tail is skipped
        let ex = src.excerpts(repo, "feat/x", window, 3, 1).unwrap();
        assert_eq!(ex.len(), 1);
        assert!(ex[0].text.contains("contexto final"));
    }

    #[test]
    fn excerpts_skips_session_with_only_messages_without_text() {
        let tmp = tempfile::tempdir().unwrap();
        let projects = tmp.path().to_path_buf();
        let repo = Path::new("/home/g/app");
        let dir = projects.join(encode_project_path(repo));
        std::fs::create_dir_all(&dir).unwrap();
        // only summary and tool_result lines — extract_text returns None for all of them
        std::fs::write(
            dir.join("vazia.jsonl"),
            concat!(
                r#"{"type":"summary","summary":"nada util"}"#,
                "\n",
                r#"{"type":"tool_result","content":[]}"#,
                "\n",
            ),
        )
        .unwrap();

        let src = ClaudeCode {
            projects_dir: projects,
        };
        let now = Utc::now();
        let window = (now - Duration::days(1), now + Duration::days(1));
        let ex = src.excerpts(repo, "feat/x", window, 3, 50).unwrap();
        assert!(
            ex.is_empty(),
            "session with no extractable text must be skipped"
        );
    }

    // -----------------------------------------------------------------------
    // (a) #15 — same mtime: stable tie-break by path ASC
    // -----------------------------------------------------------------------

    #[test]
    fn excerpts_same_mtime_deterministic_order_by_path() {
        let tmp = tempfile::tempdir().unwrap();
        let projects = tmp.path().to_path_buf();
        let repo = Path::new("/home/g/app");
        let dir = projects.join(encode_project_path(repo));
        std::fs::create_dir_all(&dir).unwrap();

        let line = r#"{"type":"user","message":{"content":"trabalho"}}"#.to_string() + "\n";
        // Write two sessions with the same content; they will have the same mtime
        // (filesystem resolution may differ, so we use max_sessions=1 and verify
        // that the one selected is always the lexicographically first path).
        std::fs::write(dir.join("zzz.jsonl"), &line).unwrap();
        std::fs::write(dir.join("aaa.jsonl"), &line).unwrap();

        // Force identical mtime on both files.
        let now_sys = std::time::SystemTime::now();
        filetime::set_file_mtime(
            dir.join("aaa.jsonl"),
            filetime::FileTime::from_system_time(now_sys),
        )
        .unwrap();
        filetime::set_file_mtime(
            dir.join("zzz.jsonl"),
            filetime::FileTime::from_system_time(now_sys),
        )
        .unwrap();

        let src = ClaudeCode {
            projects_dir: projects,
        };
        let now = chrono::Utc::now();
        let window = (now - Duration::days(1), now + Duration::days(1));

        // max_sessions=1: stable tie-break must pick "aaa.jsonl" every time.
        let ex = src.excerpts(repo, "feat/x", window, 1, 50).unwrap();
        assert_eq!(ex.len(), 1, "must return exactly 1 session");
        assert_eq!(ex[0].source, "aaa.jsonl", "tie-break must pick path ASC");
    }

    // -----------------------------------------------------------------------
    // (b) #15 — empty session excluded BEFORE truncate, real session survives
    // -----------------------------------------------------------------------

    #[test]
    fn excerpts_empty_session_excluded_before_max_sessions_truncate() {
        let tmp = tempfile::tempdir().unwrap();
        let projects = tmp.path().to_path_buf();
        let repo = Path::new("/home/g/app");
        let dir = projects.join(encode_project_path(repo));
        std::fs::create_dir_all(&dir).unwrap();

        // Session with no extractable text (only summary lines).
        std::fs::write(
            dir.join("vazia.jsonl"),
            "{\"type\":\"summary\",\"summary\":\"nada\"}\n",
        )
        .unwrap();
        // A real session with extractable text.
        std::fs::write(
            dir.join("real.jsonl"),
            "{\"type\":\"user\",\"message\":{\"content\":\"trabalho real\"}}\n",
        )
        .unwrap();

        let src = ClaudeCode {
            projects_dir: projects,
        };
        let now = chrono::Utc::now();
        let window = (now - Duration::days(1), now + Duration::days(1));

        // max_sessions=1: if empty session consumed the slot the real one would
        // be dropped — the fixed code must exclude empties BEFORE truncating.
        let ex = src.excerpts(repo, "feat/x", window, 1, 50).unwrap();
        assert_eq!(ex.len(), 1, "real session must survive despite empty peer");
        assert!(
            ex[0].text.contains("trabalho real"),
            "must be the real session"
        );
    }

    // -----------------------------------------------------------------------
    // (c) FTS mention probe via in-memory index
    // -----------------------------------------------------------------------

    #[test]
    fn excerpts_indexed_uses_fts_for_mention_probe() {
        let tmp = tempfile::tempdir().unwrap();
        let projects = tmp.path().to_path_buf();
        let repo = Path::new("/home/g/app");
        let dir = projects.join(encode_project_path(repo));
        std::fs::create_dir_all(&dir).unwrap();

        let branch = "feat/fts-probe";
        let session_path = dir.join("fts.jsonl");
        let content =
            format!("{{\"type\":\"user\",\"message\":{{\"content\":\"working on {branch}\"}}}}\n");
        std::fs::write(&session_path, &content).unwrap();

        let src = ClaudeCode {
            projects_dir: projects,
        };
        let now = chrono::Utc::now();
        // Window that does NOT cover this file's mtime — must rely on mention probe.
        let passado = now - Duration::days(730);
        let window = (passado - Duration::days(1), passado);

        // Index the session's bounded tail.
        let index = Index::open_in_memory();
        let tail = read_tail_text(&session_path, 50 * 1024).unwrap();
        let mtime = std::fs::metadata(&session_path)
            .unwrap()
            .modified()
            .unwrap()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;
        let size = std::fs::metadata(&session_path).unwrap().len() as i64;
        index.upsert_session(&session_path, repo, mtime, size, &tail);

        // excerpts_indexed with Some(index) must find the session via FTS.
        let ex = src
            .excerpts_indexed(repo, branch, window, 3, 50, Some(&index))
            .unwrap();
        assert_eq!(
            ex.len(),
            1,
            "FTS probe must find the branch-mentioning session"
        );
        assert!(ex[0].text.contains(branch));
    }

    // -----------------------------------------------------------------------
    // (d) upsert_session is idempotent — unchanged (path,mtime) is not re-indexed
    // -----------------------------------------------------------------------

    #[test]
    fn upsert_session_skips_reindex_when_path_mtime_unchanged() {
        let index = Index::open_in_memory();
        let path = Path::new("/fake/session.jsonl");
        let repo = Path::new("/home/g/app");
        let mtime: i64 = 1_700_000_000;
        let size: i64 = 42;
        let text1 = "[user] first index";
        let text2 = "[user] second index should not overwrite";

        // First upsert: stores text1.
        index.upsert_session(path, repo, mtime, size, text1);

        // Second upsert with SAME (path, mtime): must NOT overwrite the FTS row.
        index.upsert_session(path, repo, mtime, size, text2);

        // The FTS index must still contain text1 but NOT text2.
        let mentions = index.session_mentions(repo, "first");
        assert!(
            mentions.contains(&path.to_path_buf()),
            "first index text must be retrievable"
        );

        let mentions2 = index.session_mentions(repo, "second");
        assert!(
            !mentions2.contains(&path.to_path_buf()),
            "second upsert must have been skipped (same mtime)"
        );
    }
}