Skip to main content

sqlite_graphrag/commands/
restore.rs

1//! Handler for the `restore` CLI subcommand.
2
3use crate::errors::AppError;
4use crate::i18n::errors_msg;
5use crate::output;
6use crate::output::JsonOutputFormat;
7use crate::paths::AppPaths;
8use crate::storage::connection::open_rw;
9use crate::storage::memories;
10use crate::storage::versions;
11use rusqlite::params;
12use rusqlite::OptionalExtension;
13use serde::Serialize;
14
15#[derive(clap::Args)]
16#[command(after_long_help = "EXAMPLES:\n  \
17    # Restore the latest non-`restore` version of a memory\n  \
18    sqlite-graphrag restore --name onboarding\n\n  \
19    # Restore a specific version\n  \
20    sqlite-graphrag restore --name onboarding --version 3\n\n  \
21    # Restore within a specific namespace\n  \
22    sqlite-graphrag restore --name onboarding --namespace my-project")]
23pub struct RestoreArgs {
24    /// Memory name as a positional argument. Alternative to `--name`.
25    #[arg(
26        value_name = "NAME",
27        conflicts_with = "name",
28        help = "Memory name to restore; alternative to --name"
29    )]
30    pub name_positional: Option<String>,
31    /// Memory name to restore (must exist, including soft-deleted/forgotten).
32    #[arg(long)]
33    pub name: Option<String>,
34    /// Version to restore. When omitted, defaults to the latest non-`restore` version
35    /// from `memory_versions`. This makes the forget+restore workflow work without
36    /// requiring the user to discover the version first.
37    #[arg(long)]
38    pub version: Option<i64>,
39    #[arg(
40        long,
41        help = "Namespace (env: SQLITE_GRAPHRAG_NAMESPACE, default: global)"
42    )]
43    pub namespace: Option<String>,
44    /// Optimistic locking: reject if the current updated_at does not match (exit 3).
45    #[arg(
46        long,
47        value_name = "EPOCH_OR_RFC3339",
48        value_parser = crate::parsers::parse_expected_updated_at,
49        long_help = "Optimistic lock: reject if updated_at does not match. \
50Accepts Unix epoch (e.g. 1700000000) or RFC 3339 (e.g. 2026-04-19T12:00:00Z)."
51    )]
52    pub expected_updated_at: Option<i64>,
53    /// Output format.
54    #[arg(long, value_enum, default_value_t = JsonOutputFormat::Json)]
55    pub format: JsonOutputFormat,
56    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
57    pub json: bool,
58    #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
59    pub db: Option<String>,
60}
61
62#[derive(Serialize)]
63struct RestoreResponse {
64    /// Always `"restored"` — signals the completed action to shell callers and LLM agents.
65    action: String,
66    memory_id: i64,
67    name: String,
68    version: i64,
69    restored_from: i64,
70    /// Total execution time in milliseconds from handler start to serialisation.
71    elapsed_ms: u64,
72}
73
74pub fn run(args: RestoreArgs, llm_backend: crate::cli::LlmBackendChoice) -> Result<(), AppError> {
75    let start = std::time::Instant::now();
76    let _ = args.format;
77    tracing::debug!(target: "restore", name = ?args.name_positional.as_deref().or(args.name.as_deref()), version = ?args.version, "restoring version");
78    let name = args
79        .name_positional
80        .as_deref()
81        .or(args.name.as_deref())
82        .ok_or_else(|| {
83            AppError::Validation(
84                "name required: pass as positional argument or via --name".to_string(),
85            )
86        })?
87        .to_string();
88    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
89    let paths = AppPaths::resolve(args.db.as_deref())?;
90    let mut conn = open_rw(&paths.db)?;
91
92    // PRD line 1118: query WITHOUT a deleted_at filter — restore must work on soft-deleted memories
93    let result: Option<(i64, i64)> = conn
94        .query_row(
95            "SELECT id, updated_at FROM memories WHERE namespace = ?1 AND name = ?2",
96            params![namespace, name],
97            |r| Ok((r.get(0)?, r.get(1)?)),
98        )
99        .optional()?;
100    let (memory_id, current_updated_at) = result
101        .ok_or_else(|| AppError::NotFound(errors_msg::memory_not_found(&name, &namespace)))?;
102
103    if let Some(expected) = args.expected_updated_at {
104        if expected != current_updated_at {
105            return Err(AppError::Conflict(errors_msg::optimistic_lock_conflict(
106                expected,
107                current_updated_at,
108            )));
109        }
110    }
111
112    // v1.0.22 P0: resolve optional `--version`. When absent, uses the highest version
113    // whose `change_reason` is not 'restore' (recovers the real state, not meta-restore).
114    // Lets the forget+restore workflow function without manually reading memory_versions.
115    let target_version: i64 = match args.version {
116        Some(v) => v,
117        None => {
118            let last: Option<i64> = conn
119                .query_row(
120                    "SELECT MAX(version) FROM memory_versions
121                     WHERE memory_id = ?1 AND change_reason != 'restore'",
122                    params![memory_id],
123                    |r| r.get(0),
124                )
125                .optional()?
126                .flatten();
127            let v = last.ok_or_else(|| {
128                AppError::NotFound(errors_msg::memory_not_found(&name, &namespace))
129            })?;
130            tracing::info!(target: "restore",
131                "restore --version omitted; using latest non-restore version: {}",
132                v
133            );
134            v
135        }
136    };
137
138    let version_row: (String, String, String, String, String) = {
139        let mut stmt = conn.prepare_cached(
140            "SELECT name, type, description, body, metadata
141             FROM memory_versions
142             WHERE memory_id = ?1 AND version = ?2",
143        )?;
144
145        stmt.query_row(params![memory_id, target_version], |r| {
146            Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?))
147        })
148        .map_err(|_| AppError::NotFound(errors_msg::version_not_found(target_version, &name)))?
149    };
150
151    let (_old_name, old_type, old_description, old_body, old_metadata) = version_row;
152
153    // Read current FTS-indexed values before the UPDATE so sync_fts_after_update
154    // can issue the correct DELETE command for the external-content FTS5 table.
155    let (cur_name, cur_desc, cur_body): (String, String, String) = conn.query_row(
156        "SELECT name, description, body FROM memories WHERE id = ?1",
157        params![memory_id],
158        |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
159    )?;
160
161    // v1.0.21 P1-D: re-embed restored body to keep `vec_memories` synchronized
162    // with `memories`. Without this, semantic queries used the post-forget version
163    // vector, causing inconsistent recall (vec_memories=2 vs memories=3 after forget+restore).
164    output::emit_progress_i18n(
165        "Re-computing embedding for restored memory...",
166        crate::i18n::validation::runtime_pt::restore_recomputing_embedding(),
167    );
168    let skip_embed = crate::embedder::should_skip_embedding_on_failure();
169    let embedding: Option<Vec<f32>> = match crate::embedder::embed_passage_with_choice(&paths.models, &old_body, Some(llm_backend)) {
170        Ok((emb, _backend)) => Some(emb),
171        Err(AppError::Validation(msg)) => return Err(AppError::Validation(msg)),
172        Err(e) if skip_embed => {
173            tracing::warn!(error = %e, "restore: embedding failed; --skip-embedding-on-failure active, persisting without embedding");
174            None
175        }
176        Err(e) => return Err(e),
177    };
178    let snippet: String = old_body.chars().take(300).collect();
179
180    let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
181
182    // deleted_at = NULL reactivates soft-deleted memories; no deleted_at filter in the WHERE
183    let affected = if let Some(ts) = args.expected_updated_at {
184        tx.execute(
185            "UPDATE memories SET type=?2, description=?3, body=?4, body_hash=?5, deleted_at=NULL
186             WHERE id=?1 AND updated_at=?6",
187            rusqlite::params![
188                memory_id,
189                old_type,
190                old_description,
191                old_body,
192                blake3::hash(old_body.as_bytes()).to_hex().to_string(),
193                ts
194            ],
195        )?
196    } else {
197        tx.execute(
198            "UPDATE memories SET type=?2, description=?3, body=?4, body_hash=?5, deleted_at=NULL
199             WHERE id=?1",
200            rusqlite::params![
201                memory_id,
202                old_type,
203                old_description,
204                old_body,
205                blake3::hash(old_body.as_bytes()).to_hex().to_string()
206            ],
207        )?
208    };
209
210    if affected == 0 {
211        return Err(AppError::Conflict(errors_msg::concurrent_process_conflict()));
212    }
213
214    let next_v = versions::next_version(&tx, memory_id)?;
215
216    versions::insert_version(
217        &tx,
218        memory_id,
219        next_v,
220        &cur_name,
221        &old_type,
222        &old_description,
223        &old_body,
224        &old_metadata,
225        None,
226        "restore",
227    )?;
228
229    if let Some(ref emb) = embedding {
230        memories::upsert_vec(
231            &tx, memory_id, &namespace, &old_type, emb, &cur_name, &snippet,
232        )?;
233    }
234
235    memories::sync_fts_after_update(
236        &tx,
237        memory_id,
238        &cur_name,
239        &cur_desc,
240        &cur_body,
241        &cur_name,
242        &old_description,
243        &old_body,
244    )?;
245
246    tx.commit()?;
247
248    conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
249
250    output::emit_json(&RestoreResponse {
251        action: "restored".to_string(),
252        memory_id,
253        name: cur_name.clone(),
254        version: next_v,
255        restored_from: target_version,
256        elapsed_ms: start.elapsed().as_millis() as u64,
257    })?;
258
259    Ok(())
260}
261
262#[cfg(test)]
263mod tests {
264    use crate::errors::AppError;
265
266    #[test]
267    fn optimistic_lock_conflict_returns_exit_3() {
268        let err = AppError::Conflict(
269            "optimistic lock conflict: expected updated_at=50, but current is 99".to_string(),
270        );
271        assert_eq!(err.exit_code(), 3);
272        assert!(err.to_string().contains("conflict"));
273    }
274
275    #[test]
276    fn restore_response_includes_action_field() {
277        let resp = super::RestoreResponse {
278            action: "restored".to_string(),
279            memory_id: 1,
280            name: "test-mem".to_string(),
281            version: 3,
282            restored_from: 2,
283            elapsed_ms: 42,
284        };
285        let json = serde_json::to_value(&resp).expect("serialization failed");
286        assert_eq!(json["action"], "restored");
287        assert_eq!(json["memory_id"], 1);
288        assert_eq!(json["version"], 3);
289        assert_eq!(json["restored_from"], 2);
290    }
291}