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    if want_diff && !versions.is_empty() {
191        let body_lens: Vec<usize> = versions
192            .iter()
193            .map(|v| v.body.as_deref().map_or(0, str::len))
194            .collect();
195
196        versions[0].changes = Some(VersionChanges {
197            added_chars: body_lens[0],
198            removed_chars: 0,
199        });
200
201        for i in 1..versions.len() {
202            let old_len = body_lens[i - 1];
203            let new_len = body_lens[i];
204            versions[i].changes = Some(VersionChanges {
205                added_chars: new_len.saturating_sub(old_len),
206                removed_chars: old_len.saturating_sub(new_len),
207            });
208        }
209    }
210
211    output::emit_json(&HistoryResponse {
212        name,
213        namespace,
214        deleted,
215        versions,
216        elapsed_ms: start.elapsed().as_millis() as u64,
217    })?;
218
219    Ok(())
220}
221
222#[cfg(test)]
223mod tests {
224    use super::{change_reason_to_action, VersionChanges};
225
226    // Bug M-A6: action is always populated and maps known reasons to past tense.
227    #[test]
228    fn version_changes_serializes_correctly() {
229        let changes = VersionChanges {
230            added_chars: 10,
231            removed_chars: 3,
232        };
233        let json = serde_json::to_value(&changes).expect("serialization failed");
234        assert_eq!(json["added_chars"], 10u64);
235        assert_eq!(json["removed_chars"], 3u64);
236    }
237
238    #[test]
239    fn added_chars_saturating_sub_no_underflow() {
240        // new body shorter than old — added_chars must be 0, not wrapping
241        let old_len: usize = 100;
242        let new_len: usize = 40;
243        let added = new_len.saturating_sub(old_len);
244        let removed = old_len.saturating_sub(new_len);
245        assert_eq!(added, 0);
246        assert_eq!(removed, 60);
247    }
248
249    #[test]
250    fn removed_chars_saturating_sub_no_underflow() {
251        // new body longer than old — removed_chars must be 0
252        let old_len: usize = 20;
253        let new_len: usize = 80;
254        let added = new_len.saturating_sub(old_len);
255        let removed = old_len.saturating_sub(new_len);
256        assert_eq!(added, 60);
257        assert_eq!(removed, 0);
258    }
259
260    #[test]
261    fn change_reason_create_maps_to_created() {
262        assert_eq!(change_reason_to_action("create"), "created");
263    }
264
265    #[test]
266    fn change_reason_edit_maps_to_edited() {
267        assert_eq!(change_reason_to_action("edit"), "edited");
268    }
269
270    #[test]
271    fn change_reason_rename_maps_to_renamed() {
272        assert_eq!(change_reason_to_action("rename"), "renamed");
273    }
274
275    #[test]
276    fn change_reason_restore_maps_to_restored() {
277        assert_eq!(change_reason_to_action("restore"), "restored");
278    }
279
280    #[test]
281    fn change_reason_merge_maps_to_merged() {
282        assert_eq!(change_reason_to_action("merge"), "merged");
283    }
284
285    #[test]
286    fn change_reason_forget_maps_to_forgotten() {
287        assert_eq!(change_reason_to_action("forget"), "forgotten");
288    }
289
290    #[test]
291    fn change_reason_unknown_passes_through() {
292        assert_eq!(change_reason_to_action("custom-action"), "custom-action");
293    }
294
295    #[test]
296    fn epoch_zero_yields_valid_iso() {
297        // epoch_to_iso uses chrono-tz with explicit offset (+00:00 for UTC)
298        let iso = crate::tz::epoch_to_iso(0);
299        assert!(iso.starts_with("1970-01-01T00:00:00"), "got: {iso}");
300        assert!(iso.contains("00:00"), "must contain offset, got: {iso}");
301    }
302
303    #[test]
304    fn typical_epoch_yields_iso_rfc3339() {
305        let iso = crate::tz::epoch_to_iso(1_745_000_000);
306        assert!(!iso.is_empty(), "created_at_iso must not be empty");
307        assert!(iso.contains('T'), "created_at_iso must contain T separator");
308        // With UTC the offset is +00:00; verifies general format without relying on the global tz
309        assert!(
310            iso.contains('+') || iso.contains('-'),
311            "must contain offset sign, got: {iso}"
312        );
313    }
314
315    #[test]
316    fn invalid_epoch_returns_fallback() {
317        let iso = crate::tz::epoch_to_iso(i64::MIN);
318        assert!(
319            !iso.is_empty(),
320            "invalid epoch must return non-empty fallback"
321        );
322    }
323}