unlost 0.20.3

Unlost - Local-first code memory for a workspace.
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
use anyhow::Context;
use std::io::Write;
use std::path::Path;

// ============================================================================
// CHANGELOG.md ingestion as capsules
// ============================================================================

/// A parsed version entry from a Keep-a-Changelog-style CHANGELOG.md.
#[derive(Debug, Clone)]
struct ChangelogEntry {
    /// Semver string, e.g. "0.7.0"
    version: String,
    /// Release date string as written in the file, e.g. "2026-02-19"
    date: String,
    /// Unix timestamp in milliseconds derived from `date`, or 0 if unparseable.
    timestamp_ms: i64,
    /// First bullet point of the section — used as the capsule `decision`.
    first_bullet: String,
    /// Full section body (all lines after the `## [version]` header).
    body: String,
}

/// Parse a CHANGELOG.md and return one entry per `## [version]` section.
///
/// Handles the Keep-a-Changelog format:
/// ```markdown
/// ## [0.7.0] - 2026-02-19
/// ### Added
/// - **Feature**: Description.
/// ```
///
/// Entries are returned oldest-first (ascending timestamp) so that capsule
/// timestamps are monotonically increasing, matching the git commit pattern.
fn parse_changelog(content: &str) -> Vec<ChangelogEntry> {
    let mut entries: Vec<ChangelogEntry> = Vec::new();

    let mut current_version: Option<String> = None;
    let mut current_date: Option<String> = None;
    let mut current_lines: Vec<String> = Vec::new();

    for line in content.lines() {
        // Detect version header: `## [0.7.0] - 2026-02-19` (or without date)
        if line.starts_with("## [") {
            // Flush previous entry
            if let (Some(ver), Some(date)) = (current_version.take(), current_date.take()) {
                if let Some(entry) = build_entry(ver, date, std::mem::take(&mut current_lines)) {
                    entries.push(entry);
                }
            }
            current_lines.clear();

            // Parse: `## [VERSION] - DATE`
            let inner = &line[3..]; // strip "## "
            // inner looks like "[0.7.0] - 2026-02-19" or "[Unreleased]"
            if let Some(close) = inner.find(']') {
                let ver = inner[1..close].trim().to_string();
                if ver.eq_ignore_ascii_case("unreleased") {
                    // Skip unreleased sections — no date, not yet shipped
                    continue;
                }
                current_version = Some(ver);
                // Date part after `] - `
                let rest = inner[close + 1..].trim();
                let date = if let Some(stripped) = rest.strip_prefix('-') {
                    stripped.trim().to_string()
                } else {
                    String::new()
                };
                current_date = Some(date);
            }
        } else if current_version.is_some() {
            // Accumulate body lines under the current version
            current_lines.push(line.to_string());
        }
    }

    // Flush last entry
    if let (Some(ver), Some(date)) = (current_version, current_date) {
        if let Some(entry) = build_entry(ver, date, current_lines) {
            entries.push(entry);
        }
    }

    // Sort oldest-first so LanceDB timestamps are monotonically increasing
    entries.sort_by_key(|e| e.timestamp_ms);
    entries
}

/// Build a `ChangelogEntry` from the accumulated lines of one version block.
fn build_entry(version: String, date: String, lines: Vec<String>) -> Option<ChangelogEntry> {
    // Collect non-empty lines for the body, stripping leading/trailing blank lines.
    let body_lines: Vec<&str> = lines
        .iter()
        .map(|l| l.as_str())
        .collect::<Vec<_>>()
        .into_iter()
        .skip_while(|l| l.trim().is_empty())
        .collect::<Vec<_>>();

    // Trim trailing blank lines
    let body_lines: Vec<&str> = {
        let mut v = body_lines;
        while v
            .last()
            .map(|l: &&str| l.trim().is_empty())
            .unwrap_or(false)
        {
            v.pop();
        }
        v
    };

    if body_lines.is_empty() {
        return None;
    }

    let body = body_lines.join("\n");

    // First bullet: find the first line starting with `- ` (possibly after a `### ` header)
    let first_bullet = body_lines
        .iter()
        .find(|l| l.trim_start().starts_with("- "))
        .map(|l| {
            // Strip leading `- ` and any bold markers like `**Title**: `
            let trimmed = l.trim_start().trim_start_matches("- ");
            // Strip **bold**: prefix (common in this changelog style)
            if trimmed.starts_with("**") {
                if let Some(end) = trimmed.find("**: ") {
                    return trimmed[end + 4..].trim().to_string();
                }
                if let Some(end) = trimmed.find("**:") {
                    return trimmed[end + 3..].trim().to_string();
                }
            }
            trimmed.to_string()
        })
        .unwrap_or_else(|| body_lines[0].trim().to_string());

    // Parse timestamp from "YYYY-MM-DD"
    let timestamp_ms = parse_date_ms(&date);

    Some(ChangelogEntry {
        version,
        date,
        timestamp_ms,
        first_bullet,
        body,
    })
}

/// Parse "YYYY-MM-DD" into Unix milliseconds (midnight UTC). Returns 0 on failure.
fn parse_date_ms(date: &str) -> i64 {
    let parts: Vec<&str> = date.split('-').collect();
    if parts.len() != 3 {
        return 0;
    }
    let year: i64 = parts[0].parse().unwrap_or(0);
    let month: i64 = parts[1].parse().unwrap_or(0);
    let day: i64 = parts[2].parse().unwrap_or(0);
    if year == 0 || month == 0 || day == 0 {
        return 0;
    }
    // Days since Unix epoch via the civil-date algorithm (no external deps)
    // Reference: https://howardhinnant.github.io/date_algorithms.html
    let y = if month <= 2 { year - 1 } else { year };
    let m = month as i64;
    let d = day as i64;
    let era = if y >= 0 { y } else { y - 399 } / 400;
    let yoe = y - era * 400;
    let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146097 + doe - 719468;
    days * 86_400 * 1000
}

// ── Deduplication ────────────────────────────────────────────────────────────

fn ingested_versions_path(ws: &crate::WorkspacePaths) -> std::path::PathBuf {
    crate::workspace::unlost_workspace_dir(&ws.id)
        .join("changelog")
        .join("ingested.txt")
}

fn load_ingested_versions(ws: &crate::WorkspacePaths) -> std::collections::HashSet<String> {
    let path = ingested_versions_path(ws);
    match std::fs::read_to_string(&path) {
        Ok(s) => s
            .lines()
            .map(|l| l.trim().to_string())
            .filter(|l| !l.is_empty())
            .collect(),
        Err(_) => std::collections::HashSet::new(),
    }
}

fn append_ingested_versions(ws: &crate::WorkspacePaths, versions: &[String]) {
    if versions.is_empty() {
        return;
    }
    let path = ingested_versions_path(ws);
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    let mut f = match std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&path)
    {
        Ok(f) => f,
        Err(_) => return,
    };
    for v in versions {
        let _ = writeln!(f, "{}", v.trim());
    }
}

// ── Public ingestion entry point ──────────────────────────────────────────────

/// Ingest a `CHANGELOG.md` file as capsules into the workspace's LanceDB.
///
/// Each `## [version]` section becomes one `IntentCapsule` with
/// `category: "Changelog"`. Deduplicates by version string across runs so
/// re-running `unlost init` or `unlost replay` is safe.
///
/// Returns the number of newly ingested version entries.
pub async fn ingest_changelog(
    ws: &crate::WorkspacePaths,
    changelog_path: &Path,
    embedder: &crate::embed::Embedder,
    use_color: bool,
) -> anyhow::Result<usize> {
    let content = match std::fs::read_to_string(changelog_path) {
        Ok(c) => c,
        Err(_) => return Ok(0), // No CHANGELOG.md — silently skip
    };

    let already_ingested = load_ingested_versions(ws);
    let entries = parse_changelog(&content);

    let new_entries: Vec<ChangelogEntry> = entries
        .into_iter()
        .filter(|e| !already_ingested.contains(&e.version))
        .collect();

    if new_entries.is_empty() {
        return Ok(0);
    }

    std::fs::create_dir_all(&ws.db_dir).context("create db_dir")?;
    let db = lancedb::connect(ws.db_dir.to_string_lossy().as_ref())
        .execute()
        .await?;
    let _ = crate::storage::ensure_capsules_table(&db).await?;

    let mut ingested = 0usize;
    let mut new_versions: Vec<String> = Vec::new();

    for entry in &new_entries {
        let capsule = entry_to_capsule(entry);

        let source_pointer = changelog_path
            .to_str()
            .filter(|s| !s.is_empty())
            .map(|p| format!("changelog+version://{p}#{}", entry.version));
        let meta = crate::ResponseMeta {
            source: "changelog".to_string(),
            upstream_host: "changelog".to_string(),
            request_path: entry.version.clone(),
            http_status: 0,
            agent_session_id: None,
            source_pointer,
            usage: None,
        };

        match crate::storage::insert_capsule_row(
            &db,
            embedder,
            0,
            0,
            entry.timestamp_ms,
            &meta,
            None,
            None,
            &capsule,
            &crate::types::TurnEval::default(),
            None,
            None, // head_sha: not applicable for changelog ingestion
            None, // commit_sha: not applicable for changelog ingestion
        )
        .await
        {
            Ok(_) => {
                new_versions.push(entry.version.clone());
                ingested += 1;
            }
            Err(e) => {
                tracing::warn!(
                    version = %entry.version,
                    error = %e,
                    "failed to insert changelog capsule"
                );
            }
        }
    }

    append_ingested_versions(ws, &new_versions);

    if ingested > 0 && use_color {
        println!(
            "\x1b[2m  + {} changelog version{} indexed\x1b[0m",
            ingested,
            if ingested == 1 { "" } else { "s" }
        );
    } else if ingested > 0 {
        println!("  + {} changelog version(s) indexed", ingested);
    }

    Ok(ingested)
}

/// Convert a `ChangelogEntry` into an `IntentCapsule`.
fn entry_to_capsule(e: &ChangelogEntry) -> crate::IntentCapsule {
    crate::IntentCapsule {
        category: "Changelog".to_string(),
        intent: format!("Release {} on {}", e.version, e.date),
        decision: e.first_bullet.clone(),
        rationale: e.body.clone(),
        next_steps: Vec::new(),
        symbols: Vec::new(),
        user_symbols: Vec::new(),
        failure_mode: crate::types::FailureMode::None,
        failure_signals: None,
        extraction_mode: crate::types::ExtractionMode::None,
        questions: vec![],
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    const SAMPLE: &str = r#"# Changelog

## [0.7.0] - 2026-02-19

### Added
- **`unlost brief`**: New command that produces a staff-engineer-style codebase debrief.
- **Git commit ingestion**: Git commits are now first-class capsules.

### Changed
- **Git capsule routing**: Git capsules are included in `brief` and `query`.

## [0.6.5] - 2026-02-18

### Fixed
- **LLM Schema Compatibility**: Fixed invalid JSON schema for `extraction_mode`.

## [Unreleased]

### Added
- Work in progress.
"#;

    #[test]
    fn test_parse_count() {
        let entries = parse_changelog(SAMPLE);
        // Unreleased should be skipped; 0.6.5 comes before 0.7.0 (oldest-first)
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].version, "0.6.5");
        assert_eq!(entries[1].version, "0.7.0");
    }

    #[test]
    fn test_parse_fields() {
        let entries = parse_changelog(SAMPLE);
        let v07 = &entries[1];
        assert_eq!(v07.date, "2026-02-19");
        assert!(v07.timestamp_ms > 0);
        assert!(v07.body.contains("unlost brief"));
        // first_bullet should be the description after stripping bold prefix
        assert!(
            v07.first_bullet.contains("codebase debrief") || v07.first_bullet.contains("brief"),
            "unexpected first_bullet: {}",
            v07.first_bullet
        );
    }

    #[test]
    fn test_parse_date_ms() {
        // 1970-01-01 = 0
        assert_eq!(parse_date_ms("1970-01-01"), 0);
        // 2026-02-19 should be positive
        assert!(parse_date_ms("2026-02-19") > 0);
        // Invalid date
        assert_eq!(parse_date_ms("not-a-date"), 0);
    }

    #[test]
    fn test_no_entries_for_empty() {
        assert!(parse_changelog("# Changelog\n").is_empty());
        assert!(parse_changelog("").is_empty());
    }
}