Skip to main content

sqlite_graphrag/commands/
history.rs

1//! Handler for the `history` CLI subcommand.
2
3use crate::errors::AppError;
4use crate::i18n::errors_msg;
5use crate::output;
6use crate::paths::AppPaths;
7use crate::storage::connection::open_ro;
8use rusqlite::params;
9use rusqlite::OptionalExtension;
10use serde::Serialize;
11
12#[derive(clap::Args)]
13#[command(after_long_help = "EXAMPLES:\n  \
14    # List all versions of a memory (positional form)\n  \
15    sqlite-graphrag history onboarding\n\n  \
16    # List versions using the named flag form\n  \
17    sqlite-graphrag history --name onboarding\n\n  \
18    # Omit body content to reduce response size\n  \
19    sqlite-graphrag history onboarding --no-body\n\n  \
20    # Include character-level change summary between versions\n  \
21    sqlite-graphrag history onboarding --diff\n\n\
22DIFF OUTPUT:\n  \
23    When --diff is active, each version (except the first) includes a `changes`\n  \
24    object with `added_chars` and `removed_chars` — the character count difference\n  \
25    between that version and its predecessor.")]
26pub struct HistoryArgs {
27    /// Memory name as a positional argument. Alternative to `--name`.
28    #[arg(
29        value_name = "NAME",
30        conflicts_with = "name",
31        help = "Memory name whose version history to return; alternative to --name"
32    )]
33    pub name_positional: Option<String>,
34    /// Memory name whose version history will be returned. Includes soft-deleted memories
35    /// so that `restore --version <V>` workflow remains discoverable after `forget`.
36    #[arg(long)]
37    pub name: Option<String>,
38    #[arg(
39        long,
40        help = "Namespace (env: SQLITE_GRAPHRAG_NAMESPACE, default: global)"
41    )]
42    pub namespace: Option<String>,
43    /// Omit body content from each version to reduce response size.
44    #[arg(
45        long,
46        default_value_t = false,
47        help = "Omit body content from response"
48    )]
49    pub no_body: bool,
50    /// Include character-level change summary between consecutive versions.
51    #[arg(
52        long,
53        default_value_t = false,
54        help = "Include character-level change summary between consecutive versions"
55    )]
56    pub diff: bool,
57    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
58    pub json: bool,
59    /// Path to graphrag.sqlite (overrides SQLITE_GRAPHRAG_DB_PATH and default CWD).
60    #[arg(
61        long,
62        env = "SQLITE_GRAPHRAG_DB_PATH",
63        help = "Path to graphrag.sqlite"
64    )]
65    pub db: Option<String>,
66}
67
68/// Character-level change summary between two consecutive versions.
69#[derive(Serialize)]
70struct VersionChanges {
71    added_chars: usize,
72    removed_chars: usize,
73}
74
75#[derive(Serialize)]
76struct HistoryVersion {
77    version: i64,
78    name: String,
79    #[serde(rename = "type")]
80    memory_type: String,
81    description: String,
82    #[serde(skip_serializing_if = "Option::is_none")]
83    body: Option<String>,
84    metadata: serde_json::Value,
85    /// Past-tense action label derived from `change_reason`; always populated
86    /// so consumers do not see `null` for the documented `action` contract
87    /// (M-A6 fix in v1.0.40). Known mappings: `create→created`, `edit→edited`,
88    /// `rename→renamed`, `restore→restored`, `merge→merged`, `forget→forgotten`.
89    /// Unknown verbs are passed through unchanged.
90    action: String,
91    change_reason: String,
92    changed_by: Option<String>,
93    created_at: i64,
94    created_at_iso: String,
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub changes: Option<VersionChanges>,
97}
98
99/// Maps the raw `change_reason` stored in `memory_versions` to the past-tense
100/// `action` exposed in the JSON contract. Centralized so future call sites
101/// (e.g. `read --include-history`) reuse the same mapping.
102fn change_reason_to_action(reason: &str) -> String {
103    match reason {
104        "create" => "created",
105        "edit" => "edited",
106        "update" => "updated",
107        "rename" => "renamed",
108        "restore" => "restored",
109        "merge" => "merged",
110        "forget" => "forgotten",
111        other => other,
112    }
113    .to_string()
114}
115
116#[derive(Serialize)]
117struct HistoryResponse {
118    name: String,
119    namespace: String,
120    /// True when the memory is currently soft-deleted (forgotten).
121    /// Allows the user to discover the version for `restore` even after `forget`.
122    deleted: bool,
123    versions: Vec<HistoryVersion>,
124    /// Total execution time in milliseconds from handler start to serialisation.
125    elapsed_ms: u64,
126}
127
128pub fn run(args: HistoryArgs) -> Result<(), AppError> {
129    let start = std::time::Instant::now();
130    // Resolve name from positional or --name flag; both are optional, at least one is required.
131    let name = args.name_positional.or(args.name).ok_or_else(|| {
132        AppError::Validation("name required: pass as positional argument or via --name".to_string())
133    })?;
134    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
135    let paths = AppPaths::resolve(args.db.as_deref())?;
136    crate::storage::connection::ensure_db_ready(&paths)?;
137    let conn = open_ro(&paths.db)?;
138
139    // v1.0.22 P0: direct query WITHOUT deleted_at filter — history MUST return versions
140    // of forgotten memories so the user can discover the version to use in `restore`.
141    // The old find_by_name filtered deleted_at IS NULL and was a dead-end in the forget+restore workflow.
142    let row: Option<(i64, Option<i64>)> = conn
143        .query_row(
144            "SELECT id, deleted_at FROM memories WHERE namespace = ?1 AND name = ?2",
145            params![namespace, name],
146            |r| Ok((r.get(0)?, r.get(1)?)),
147        )
148        .optional()?;
149    let (memory_id, deleted_at) =
150        row.ok_or_else(|| AppError::NotFound(errors_msg::memory_not_found(&name, &namespace)))?;
151    let deleted = deleted_at.is_some();
152
153    let mut stmt = conn.prepare(
154        "SELECT version, name, type, description, body, metadata,
155                change_reason, changed_by, created_at
156         FROM memory_versions
157         WHERE memory_id = ?1
158         ORDER BY version ASC",
159    )?;
160
161    let no_body = args.no_body;
162    let want_diff = args.diff;
163    let mut versions = stmt
164        .query_map(params![memory_id], |r| {
165            let created_at: i64 = r.get(8)?;
166            let created_at_iso = crate::tz::epoch_to_iso(created_at);
167            let body_str: String = r.get(4)?;
168            let metadata_str: String = r.get(5)?;
169            let metadata_value: serde_json::Value = serde_json::from_str(&metadata_str)
170                .unwrap_or(serde_json::Value::Object(serde_json::Map::new()));
171            let change_reason: String = r.get(6)?;
172            let action = change_reason_to_action(&change_reason);
173            Ok(HistoryVersion {
174                version: r.get(0)?,
175                name: r.get(1)?,
176                memory_type: r.get(2)?,
177                description: r.get(3)?,
178                body: if no_body { None } else { Some(body_str) },
179                metadata: metadata_value,
180                action,
181                change_reason,
182                changed_by: r.get(7)?,
183                created_at,
184                created_at_iso,
185                changes: None,
186            })
187        })?
188        .collect::<Result<Vec<_>, _>>()?;
189
190    // Compute character-level change summaries between consecutive versions.
191    // Version N receives the diff relative to version N-1 (i.e., what changed going forward).
192    // Version 1 (the first) has no predecessor so `changes` stays `None`.
193    if want_diff && versions.len() > 1 {
194        // Collect body lengths first to avoid borrowing issues.
195        let body_lens: Vec<usize> = versions
196            .iter()
197            .map(|v| v.body.as_deref().map_or(0, str::len))
198            .collect();
199
200        for i in 1..versions.len() {
201            let old_len = body_lens[i - 1];
202            let new_len = body_lens[i];
203            versions[i].changes = Some(VersionChanges {
204                added_chars: new_len.saturating_sub(old_len),
205                removed_chars: old_len.saturating_sub(new_len),
206            });
207        }
208    }
209
210    output::emit_json(&HistoryResponse {
211        name,
212        namespace,
213        deleted,
214        versions,
215        elapsed_ms: start.elapsed().as_millis() as u64,
216    })?;
217
218    Ok(())
219}
220
221#[cfg(test)]
222mod tests {
223    use super::{change_reason_to_action, VersionChanges};
224
225    // Bug M-A6: action is always populated and maps known reasons to past tense.
226    #[test]
227    fn version_changes_serializes_correctly() {
228        let changes = VersionChanges {
229            added_chars: 10,
230            removed_chars: 3,
231        };
232        let json = serde_json::to_value(&changes).expect("serialization failed");
233        assert_eq!(json["added_chars"], 10u64);
234        assert_eq!(json["removed_chars"], 3u64);
235    }
236
237    #[test]
238    fn added_chars_saturating_sub_no_underflow() {
239        // new body shorter than old — added_chars must be 0, not wrapping
240        let old_len: usize = 100;
241        let new_len: usize = 40;
242        let added = new_len.saturating_sub(old_len);
243        let removed = old_len.saturating_sub(new_len);
244        assert_eq!(added, 0);
245        assert_eq!(removed, 60);
246    }
247
248    #[test]
249    fn removed_chars_saturating_sub_no_underflow() {
250        // new body longer than old — removed_chars must be 0
251        let old_len: usize = 20;
252        let new_len: usize = 80;
253        let added = new_len.saturating_sub(old_len);
254        let removed = old_len.saturating_sub(new_len);
255        assert_eq!(added, 60);
256        assert_eq!(removed, 0);
257    }
258
259    #[test]
260    fn change_reason_create_maps_to_created() {
261        assert_eq!(change_reason_to_action("create"), "created");
262    }
263
264    #[test]
265    fn change_reason_edit_maps_to_edited() {
266        assert_eq!(change_reason_to_action("edit"), "edited");
267    }
268
269    #[test]
270    fn change_reason_rename_maps_to_renamed() {
271        assert_eq!(change_reason_to_action("rename"), "renamed");
272    }
273
274    #[test]
275    fn change_reason_restore_maps_to_restored() {
276        assert_eq!(change_reason_to_action("restore"), "restored");
277    }
278
279    #[test]
280    fn change_reason_merge_maps_to_merged() {
281        assert_eq!(change_reason_to_action("merge"), "merged");
282    }
283
284    #[test]
285    fn change_reason_forget_maps_to_forgotten() {
286        assert_eq!(change_reason_to_action("forget"), "forgotten");
287    }
288
289    #[test]
290    fn change_reason_unknown_passes_through() {
291        assert_eq!(change_reason_to_action("custom-action"), "custom-action");
292    }
293
294    #[test]
295    fn epoch_zero_yields_valid_iso() {
296        // epoch_to_iso uses chrono-tz with explicit offset (+00:00 for UTC)
297        let iso = crate::tz::epoch_to_iso(0);
298        assert!(iso.starts_with("1970-01-01T00:00:00"), "got: {iso}");
299        assert!(iso.contains("00:00"), "must contain offset, got: {iso}");
300    }
301
302    #[test]
303    fn typical_epoch_yields_iso_rfc3339() {
304        let iso = crate::tz::epoch_to_iso(1_745_000_000);
305        assert!(!iso.is_empty(), "created_at_iso must not be empty");
306        assert!(iso.contains('T'), "created_at_iso must contain T separator");
307        // With UTC the offset is +00:00; verifies general format without relying on the global tz
308        assert!(
309            iso.contains('+') || iso.contains('-'),
310            "must contain offset sign, got: {iso}"
311        );
312    }
313
314    #[test]
315    fn invalid_epoch_returns_fallback() {
316        let iso = crate::tz::epoch_to_iso(i64::MIN);
317        assert!(
318            !iso.is_empty(),
319            "invalid epoch must return non-empty fallback"
320        );
321    }
322}