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