Skip to main content

sqlite_graphrag/commands/
edit.rs

1//! Handler for the `edit` CLI subcommand.
2
3use crate::errors::AppError;
4use crate::i18n::errors_msg;
5use crate::output;
6use crate::paths::AppPaths;
7use crate::storage::connection::open_rw;
8use crate::storage::{memories, versions};
9use serde::Serialize;
10
11#[derive(clap::Args)]
12#[command(after_long_help = "EXAMPLES:\n  \
13    # Edit body inline\n  \
14    sqlite-graphrag edit onboarding --body \"updated content\"\n\n  \
15    # Edit body from a file\n  \
16    sqlite-graphrag edit onboarding --body-file ./updated.md\n\n  \
17    # Edit body from stdin (pipe)\n  \
18    cat updated.md | sqlite-graphrag edit onboarding --body-stdin\n\n  \
19    # Update only the description\n  \
20    sqlite-graphrag edit onboarding --description \"new short description\"")]
21pub struct EditArgs {
22    /// Memory name as a positional argument. Alternative to `--name`.
23    #[arg(
24        value_name = "NAME",
25        conflicts_with = "name",
26        help = "Memory name to edit; alternative to --name"
27    )]
28    pub name_positional: Option<String>,
29    /// Memory name to edit. Soft-deleted memories are not editable; use `restore` first.
30    #[arg(long)]
31    pub name: Option<String>,
32    /// New inline body content. Mutually exclusive with --body-file and --body-stdin.
33    #[arg(long, conflicts_with_all = ["body_file", "body_stdin"])]
34    pub body: Option<String>,
35    /// Read new body from a file. Mutually exclusive with --body and --body-stdin.
36    #[arg(long, conflicts_with_all = ["body", "body_stdin"])]
37    pub body_file: Option<std::path::PathBuf>,
38    /// Read new body from stdin until EOF. Mutually exclusive with --body and --body-file.
39    #[arg(long, conflicts_with_all = ["body", "body_file"])]
40    pub body_stdin: bool,
41    /// New description (≤500 chars) replacing the existing one.
42    #[arg(long)]
43    pub description: Option<String>,
44    /// Change the memory type (e.g. note, skill, decision).
45    #[arg(long, value_enum, help = "Change memory type")]
46    pub memory_type: Option<crate::cli::MemoryType>,
47    #[arg(
48        long,
49        value_name = "EPOCH_OR_RFC3339",
50        value_parser = crate::parsers::parse_expected_updated_at,
51        long_help = "Optimistic lock: reject if updated_at does not match. \
52Accepts Unix epoch (e.g. 1700000000) or RFC 3339 (e.g. 2026-04-19T12:00:00Z)."
53    )]
54    pub expected_updated_at: Option<i64>,
55    #[arg(
56        long,
57        help = "Namespace (env: SQLITE_GRAPHRAG_NAMESPACE, default: global)"
58    )]
59    pub namespace: Option<String>,
60    #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
61    pub json: bool,
62    #[arg(long, env = "SQLITE_GRAPHRAG_DB_PATH")]
63    pub db: Option<String>,
64    /// G42/S9 (v1.0.79): regenerate the embedding even when the body is
65    /// unchanged. This is the supported way to re-embed a memory (the
66    /// pre-v1.0.79 docs suggested `edit --description "<same>"`, which
67    /// is a no-op and never re-embeds).
68    #[arg(
69        long,
70        default_value_t = false,
71        help = "Regenerate the embedding even when the body is unchanged (G42/S9)"
72    )]
73    pub force_reembed: bool,
74    /// G42/S3 (v1.0.79): maximum simultaneous LLM embedding subprocesses.
75    /// Only relevant for future multi-item edit paths; a single-body edit
76    /// performs one LLM call regardless.
77    #[arg(long, default_value_t = 4, value_name = "N",
78          value_parser = clap::value_parser!(u64).range(1..=32),
79          help = "Maximum simultaneous LLM embedding subprocesses (default: 4, clamp [1,32])")]
80    pub llm_parallelism: u64,
81}
82
83#[derive(Serialize)]
84struct EditResponse {
85    memory_id: i64,
86    name: String,
87    action: String,
88    version: i64,
89    /// Total execution time in milliseconds from handler start to serialisation.
90    elapsed_ms: u64,
91}
92
93pub fn run(args: EditArgs) -> Result<(), AppError> {
94    use crate::constants::*;
95
96    let inicio = std::time::Instant::now();
97    tracing::debug!(target: "edit", name = ?args.name_positional.as_deref().or(args.name.as_deref()), "updating memory");
98    // Resolve name from positional or --name flag; both are optional, at least one is required.
99    let name = args.name_positional.or(args.name).ok_or_else(|| {
100        AppError::Validation("name required: pass as positional argument or via --name".to_string())
101    })?;
102    let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
103
104    let paths = AppPaths::resolve(args.db.as_deref())?;
105    crate::storage::connection::ensure_db_ready(&paths)?;
106    let mut conn = open_rw(&paths.db)?;
107
108    let (memory_id, current_updated_at, _current_version) =
109        memories::find_by_name(&conn, &namespace, &name)?
110            .ok_or_else(|| AppError::NotFound(errors_msg::memory_not_found(&name, &namespace)))?;
111
112    if let Some(expected) = args.expected_updated_at {
113        if expected != current_updated_at {
114            return Err(AppError::Conflict(errors_msg::optimistic_lock_conflict(
115                expected,
116                current_updated_at,
117            )));
118        }
119    }
120
121    let mut raw_body: Option<String> = None;
122    if args.body.is_some() || args.body_file.is_some() || args.body_stdin {
123        let b = if let Some(b) = args.body {
124            b
125        } else if let Some(path) = &args.body_file {
126            let file_size = std::fs::metadata(path).map_err(AppError::Io)?.len();
127            if file_size > MAX_MEMORY_BODY_LEN as u64 {
128                return Err(AppError::LimitExceeded(
129                    crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
130                ));
131            }
132            std::fs::read_to_string(path).map_err(AppError::Io)?
133        } else {
134            crate::stdin_helper::read_stdin_with_timeout(60)?
135        };
136        if b.len() > MAX_MEMORY_BODY_LEN {
137            return Err(AppError::LimitExceeded(
138                crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
139            ));
140        }
141        raw_body = Some(b);
142    }
143
144    if let Some(ref desc) = args.description {
145        if desc.len() > MAX_MEMORY_DESCRIPTION_LEN {
146            return Err(AppError::Validation(
147                crate::i18n::validation::description_exceeds(MAX_MEMORY_DESCRIPTION_LEN),
148            ));
149        }
150    }
151
152    let row = memories::read_by_name(&conn, &namespace, &name)?
153        .ok_or_else(|| AppError::Internal(anyhow::anyhow!("memory row not found after check")))?;
154
155    let body_changed = raw_body.is_some();
156    let new_body = raw_body.unwrap_or(row.body.clone());
157    let new_description = args.description.unwrap_or(row.description.clone());
158    let new_hash = blake3::hash(new_body.as_bytes()).to_hex().to_string();
159    // Skip re-embedding when body content is identical to the stored version.
160    let body_changed = body_changed && new_hash != row.body_hash;
161    let memory_type = args
162        .memory_type
163        .map(|t| t.as_str().to_string())
164        .unwrap_or_else(|| row.memory_type.clone());
165    let type_changed = memory_type != row.memory_type;
166    let metadata = row.metadata.clone();
167
168    let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
169
170    let affected = if let Some(ts) = args.expected_updated_at {
171        tx.execute(
172            "UPDATE memories SET description=?2, body=?3, body_hash=?4, type=?5
173             WHERE id=?1 AND updated_at=?6 AND deleted_at IS NULL",
174            rusqlite::params![
175                memory_id,
176                new_description,
177                new_body,
178                new_hash,
179                memory_type,
180                ts
181            ],
182        )?
183    } else {
184        tx.execute(
185            "UPDATE memories SET description=?2, body=?3, body_hash=?4, type=?5
186             WHERE id=?1 AND deleted_at IS NULL",
187            rusqlite::params![memory_id, new_description, new_body, new_hash, memory_type],
188        )?
189    };
190
191    if affected == 0 {
192        return Err(AppError::Conflict(
193            "optimistic lock conflict: memory was modified by another process".to_string(),
194        ));
195    }
196
197    if body_changed || type_changed || args.force_reembed {
198        output::emit_progress_i18n(
199            "Re-computing embedding for edited body...",
200            crate::i18n::validation::runtime_pt::edit_recomputing_embedding(),
201        );
202        let embedding = crate::embedder::embed_passage_local(&paths.models, &new_body)?;
203        let snippet: String = new_body.chars().take(300).collect();
204        memories::upsert_vec(
205            &tx,
206            memory_id,
207            &namespace,
208            &memory_type,
209            &embedding,
210            &name,
211            &snippet,
212        )?;
213    }
214
215    let next_v = versions::next_version(&tx, memory_id)?;
216
217    versions::insert_version(
218        &tx,
219        memory_id,
220        next_v,
221        &name,
222        &memory_type,
223        &new_description,
224        &new_body,
225        &metadata,
226        None,
227        "edit",
228    )?;
229
230    memories::sync_fts_after_update(
231        &tx,
232        memory_id,
233        &row.name,
234        &row.description,
235        &row.body,
236        &row.name,
237        &new_description,
238        &new_body,
239    )?;
240
241    tx.commit()?;
242
243    conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
244
245    output::emit_json(&EditResponse {
246        memory_id,
247        name,
248        action: "updated".to_string(),
249        version: next_v,
250        elapsed_ms: inicio.elapsed().as_millis() as u64,
251    })?;
252
253    Ok(())
254}
255
256#[cfg(test)]
257mod tests {
258    use super::*;
259
260    #[test]
261    fn edit_response_serializes_all_fields() {
262        let resp = EditResponse {
263            memory_id: 42,
264            name: "my-memory".to_string(),
265            action: "updated".to_string(),
266            version: 3,
267            elapsed_ms: 7,
268        };
269        let json = serde_json::to_value(&resp).expect("serialization failed");
270        assert_eq!(json["memory_id"], 42i64);
271        assert_eq!(json["name"], "my-memory");
272        assert_eq!(json["action"], "updated");
273        assert_eq!(json["version"], 3i64);
274        assert!(json["elapsed_ms"].is_number());
275    }
276
277    #[test]
278    fn edit_response_action_contains_updated() {
279        let resp = EditResponse {
280            memory_id: 1,
281            name: "n".to_string(),
282            action: "updated".to_string(),
283            version: 1,
284            elapsed_ms: 0,
285        };
286        assert_eq!(
287            resp.action, "updated",
288            "action must be 'updated' for successful edits"
289        );
290    }
291
292    #[test]
293    fn edit_body_exceeds_limit_returns_error() {
294        let limit = crate::constants::MAX_MEMORY_BODY_LEN;
295        let large_body: String = "a".repeat(limit + 1);
296        assert!(
297            large_body.len() > limit,
298            "body above limit must have length > MAX_MEMORY_BODY_LEN"
299        );
300    }
301
302    #[test]
303    fn edit_description_exceeds_limit_returns_error() {
304        let limit = crate::constants::MAX_MEMORY_DESCRIPTION_LEN;
305        let large_desc: String = "d".repeat(limit + 1);
306        assert!(
307            large_desc.len() > limit,
308            "description above limit must have length > MAX_MEMORY_DESCRIPTION_LEN"
309        );
310    }
311}