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 (flag / XDG 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 XDG db.default_path and default CWD).
60    #[arg(
61        long,
62                help = "Path to graphrag.sqlite"
63    )]
64    pub db: Option<String>,
65}
66
67/// Character-level change summary between two consecutive versions.
68#[derive(Serialize)]
69struct VersionChanges {
70    added_chars: usize,
71    removed_chars: usize,
72}
73
74#[derive(Serialize)]
75struct HistoryVersion {
76    version: i64,
77    name: String,
78    #[serde(rename = "type")]
79    memory_type: String,
80    description: String,
81    #[serde(skip_serializing_if = "Option::is_none")]
82    body: Option<String>,
83    metadata: serde_json::Value,
84    /// Past-tense action label derived from `change_reason`; always populated
85    /// so consumers do not see `null` for the documented `action` contract
86    /// (M-A6 fix in v1.0.40). Known mappings: `create→created`, `edit→edited`,
87    /// `rename→renamed`, `restore→restored`, `merge→merged`, `forget→forgotten`.
88    /// Unknown verbs are passed through unchanged.
89    action: String,
90    change_reason: String,
91    changed_by: Option<String>,
92    created_at: i64,
93    created_at_iso: String,
94    #[serde(skip_serializing_if = "Option::is_none")]
95    pub changes: Option<VersionChanges>,
96}
97
98/// Maps the raw `change_reason` stored in `memory_versions` to the past-tense
99/// `action` exposed in the JSON contract. Centralized so future call sites
100/// (e.g. `read --include-history`) reuse the same mapping.
101fn change_reason_to_action(reason: &str) -> String {
102    match reason {
103        "create" => "created",
104        "edit" => "edited",
105        "update" => "updated",
106        "rename" => "renamed",
107        "restore" => "restored",
108        "merge" => "merged",
109        "forget" => "forgotten",
110        other => other,
111    }
112    .to_string()
113}
114
115#[derive(Serialize)]
116struct HistoryResponse {
117    name: String,
118    namespace: String,
119    /// True when the memory is currently soft-deleted (forgotten).
120    /// Allows the user to discover the version for `restore` even after `forget`.
121    deleted: bool,
122    versions: Vec<HistoryVersion>,
123    /// Total execution time in milliseconds from handler start to serialisation.
124    elapsed_ms: u64,
125}
126
127pub fn run(args: HistoryArgs) -> Result<(), AppError> {
128    let start = std::time::Instant::now();
129    // Resolve name from positional or --name flag; both are optional, at least one is required.
130    let name = args.name_positional.or(args.name).ok_or_else(|| {
131        AppError::Validation("name required: pass as positional argument or via --name".to_string())
132    })?;
133    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
134    let paths = AppPaths::resolve(args.db.as_deref())?;
135    crate::storage::connection::ensure_db_ready(&paths)?;
136    let conn = open_ro(&paths.db)?;
137
138    // v1.0.22 P0: direct query WITHOUT deleted_at filter — history MUST return versions
139    // of forgotten memories so the user can discover the version to use in `restore`.
140    // The old find_by_name filtered deleted_at IS NULL and was a dead-end in the forget+restore workflow.
141    let row: Option<(i64, Option<i64>)> = conn
142        .query_row(
143            "SELECT id, deleted_at FROM memories WHERE namespace = ?1 AND name = ?2",
144            params![namespace, name],
145            |r| Ok((r.get(0)?, r.get(1)?)),
146        )
147        .optional()?;
148    let (memory_id, deleted_at) =
149        row.ok_or_else(|| AppError::NotFound(errors_msg::memory_not_found(&name, &namespace)))?;
150    let deleted = deleted_at.is_some();
151
152    let mut stmt = conn.prepare_cached(
153        "SELECT version, name, type, description, body, metadata,
154                change_reason, changed_by, created_at
155         FROM memory_versions
156         WHERE memory_id = ?1
157         ORDER BY version ASC",
158    )?;
159
160    let no_body = args.no_body;
161    let want_diff = args.diff;
162    let mut versions = stmt
163        .query_map(params![memory_id], |r| {
164            let created_at: i64 = r.get(8)?;
165            let created_at_iso = crate::tz::epoch_to_iso(created_at);
166            let body_str: String = r.get(4)?;
167            let metadata_str: String = r.get(5)?;
168            let metadata_value: serde_json::Value = serde_json::from_str(&metadata_str)
169                .unwrap_or(serde_json::Value::Object(serde_json::Map::new()));
170            let change_reason: String = r.get(6)?;
171            let action = change_reason_to_action(&change_reason);
172            Ok(HistoryVersion {
173                version: r.get(0)?,
174                name: r.get(1)?,
175                memory_type: r.get(2)?,
176                description: r.get(3)?,
177                body: if no_body { None } else { Some(body_str) },
178                metadata: metadata_value,
179                action,
180                change_reason,
181                changed_by: r.get(7)?,
182                created_at,
183                created_at_iso,
184                changes: None,
185            })
186        })?
187        .collect::<Result<Vec<_>, _>>()?;
188
189    if want_diff && !versions.is_empty() {
190        let body_lens: Vec<usize> = versions
191            .iter()
192            .map(|v| v.body.as_deref().map_or(0, str::len))
193            .collect();
194
195        versions[0].changes = Some(VersionChanges {
196            added_chars: body_lens[0],
197            removed_chars: 0,
198        });
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        // v1.0.68 (test fix): timezone-agnostic — parse the ISO and compare
297        // the instant with the Unix epoch.  The previous starts_with check
298        // leaked the SQLITE_GRAPHRAG_DISPLAY_TZ env var from sibling tests
299        // and failed on hosts whose default display timezone is not UTC.
300        let iso = crate::tz::epoch_to_iso(0);
301        let parsed = chrono::DateTime::parse_from_rfc3339(&iso)
302            .unwrap_or_else(|e| panic!("expected RFC3339, got `{iso}`: {e}"));
303        assert_eq!(
304            parsed.timestamp(),
305            chrono::DateTime::UNIX_EPOCH.timestamp(),
306            "epoch 0 must map to the Unix epoch instant, got: {iso}"
307        );
308    }
309
310    #[test]
311    fn typical_epoch_yields_iso_rfc3339() {
312        let iso = crate::tz::epoch_to_iso(1_745_000_000);
313        assert!(!iso.is_empty(), "created_at_iso must not be empty");
314        assert!(iso.contains('T'), "created_at_iso must contain T separator");
315        // With UTC the offset is +00:00; verifies general format without relying on the global tz
316        assert!(
317            iso.contains('+') || iso.contains('-'),
318            "must contain offset sign, got: {iso}"
319        );
320    }
321
322    #[test]
323    fn invalid_epoch_returns_fallback() {
324        let iso = crate::tz::epoch_to_iso(i64::MIN);
325        assert!(
326            !iso.is_empty(),
327            "invalid epoch must return non-empty fallback"
328        );
329    }
330}