sqlite_graphrag/commands/
edit.rs1use 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 #[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 #[arg(long)]
31 pub name: Option<String>,
32 #[arg(long, conflicts_with_all = ["body_file", "body_stdin"])]
34 pub body: Option<String>,
35 #[arg(long, conflicts_with_all = ["body", "body_stdin"])]
37 pub body_file: Option<std::path::PathBuf>,
38 #[arg(long, conflicts_with_all = ["body", "body_file"])]
40 pub body_stdin: bool,
41 #[arg(long)]
43 pub description: Option<String>,
44 #[arg(long, value_enum, visible_alias = "type", 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 #[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 #[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 elapsed_ms: u64,
91 #[serde(skip_serializing_if = "Option::is_none")]
96 backend_invoked: Option<&'static str>,
97}
98
99pub fn run(args: EditArgs, llm_backend: crate::cli::LlmBackendChoice) -> Result<(), AppError> {
100 use crate::constants::*;
101
102 let inicio = std::time::Instant::now();
103 tracing::debug!(target: "edit", name = ?args.name_positional.as_deref().or(args.name.as_deref()), "updating memory");
104 let name = args.name_positional.or(args.name).ok_or_else(|| {
106 AppError::Validation("name required: pass as positional argument or via --name".to_string())
107 })?;
108 let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
109
110 let paths = AppPaths::resolve(args.db.as_deref())?;
111 crate::storage::connection::ensure_db_ready(&paths)?;
112 let mut conn = open_rw(&paths.db)?;
113
114 let (memory_id, current_updated_at, _current_version) =
115 memories::find_by_name(&conn, &namespace, &name)?
116 .ok_or_else(|| AppError::NotFound(errors_msg::memory_not_found(&name, &namespace)))?;
117
118 if let Some(expected) = args.expected_updated_at {
119 if expected != current_updated_at {
120 return Err(AppError::Conflict(errors_msg::optimistic_lock_conflict(
121 expected,
122 current_updated_at,
123 )));
124 }
125 }
126
127 let mut raw_body: Option<String> = None;
128 if args.body.is_some() || args.body_file.is_some() || args.body_stdin {
129 let b = if let Some(b) = args.body {
130 b
131 } else if let Some(path) = &args.body_file {
132 let file_size = std::fs::metadata(path).map_err(AppError::Io)?.len();
133 if file_size > MAX_MEMORY_BODY_LEN as u64 {
134 return Err(AppError::LimitExceeded(
135 crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
136 ));
137 }
138 std::fs::read_to_string(path).map_err(AppError::Io)?
139 } else {
140 crate::stdin_helper::read_stdin_with_timeout(60)?
141 };
142 if b.len() > MAX_MEMORY_BODY_LEN {
143 return Err(AppError::LimitExceeded(
144 crate::i18n::validation::body_exceeds(MAX_MEMORY_BODY_LEN),
145 ));
146 }
147 raw_body = Some(b);
148 }
149
150 if let Some(ref desc) = args.description {
151 if desc.len() > MAX_MEMORY_DESCRIPTION_LEN {
152 return Err(AppError::Validation(
153 crate::i18n::validation::description_exceeds(MAX_MEMORY_DESCRIPTION_LEN),
154 ));
155 }
156 }
157
158 let row = memories::read_by_name(&conn, &namespace, &name)?
159 .ok_or_else(|| AppError::Internal(anyhow::anyhow!("memory row not found after check")))?;
160
161 let body_changed = raw_body.is_some();
162 let new_body = raw_body.unwrap_or(row.body.clone());
163 let new_description = args.description.unwrap_or(row.description.clone());
164 let new_hash = blake3::hash(new_body.as_bytes()).to_hex().to_string();
165 let body_changed = body_changed && new_hash != row.body_hash;
167 let memory_type = args
168 .memory_type
169 .map(|t| t.as_str().to_string())
170 .unwrap_or_else(|| row.memory_type.clone());
171 let type_changed = memory_type != row.memory_type;
172 let metadata = row.metadata.clone();
173
174 let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
175
176 let affected = if let Some(ts) = args.expected_updated_at {
177 tx.execute(
178 "UPDATE memories SET description=?2, body=?3, body_hash=?4, type=?5
179 WHERE id=?1 AND updated_at=?6 AND deleted_at IS NULL",
180 rusqlite::params![
181 memory_id,
182 new_description,
183 new_body,
184 new_hash,
185 memory_type,
186 ts
187 ],
188 )?
189 } else {
190 tx.execute(
191 "UPDATE memories SET description=?2, body=?3, body_hash=?4, type=?5
192 WHERE id=?1 AND deleted_at IS NULL",
193 rusqlite::params![memory_id, new_description, new_body, new_hash, memory_type],
194 )?
195 };
196
197 if affected == 0 {
198 return Err(AppError::Conflict(
199 "optimistic lock conflict: memory was modified by another process".to_string(),
200 ));
201 }
202
203 let mut backend_invoked: Option<&'static str> = None;
207
208 if body_changed || type_changed || args.force_reembed {
209 output::emit_progress_i18n(
210 "Re-computing embedding for edited body...",
211 crate::i18n::validation::runtime_pt::edit_recomputing_embedding(),
212 );
213 let skip_embed = crate::embedder::should_skip_embedding_on_failure();
217 let embedding: Option<(Vec<f32>, &'static str)> =
218 match crate::embedder::embed_passage_with_choice(
219 &paths.models,
220 &new_body,
221 Some(llm_backend),
222 ) {
223 Ok((emb, kind)) => Some((emb, kind.as_str())),
224 Err(AppError::Validation(msg)) => return Err(AppError::Validation(msg)),
225 Err(e) if skip_embed => {
226 tracing::warn!(error = %e, "edit: embedding failed; --skip-embedding-on-failure active, persisting without embedding");
227 None
228 }
229 Err(e) => return Err(e),
230 };
231 if let Some((ref emb, kind)) = embedding {
232 backend_invoked = Some(kind);
233 let snippet: String = new_body.chars().take(300).collect();
234 memories::upsert_vec(
235 &tx,
236 memory_id,
237 &namespace,
238 &memory_type,
239 emb,
240 &name,
241 &snippet,
242 )?;
243 }
244 }
245
246 let next_v = versions::next_version(&tx, memory_id)?;
247
248 versions::insert_version(
249 &tx,
250 memory_id,
251 next_v,
252 &name,
253 &memory_type,
254 &new_description,
255 &new_body,
256 &metadata,
257 None,
258 "edit",
259 )?;
260
261 memories::sync_fts_after_update(
262 &tx,
263 memory_id,
264 &row.name,
265 &row.description,
266 &row.body,
267 &row.name,
268 &new_description,
269 &new_body,
270 )?;
271
272 tx.commit()?;
273
274 conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
275
276 output::emit_json(&EditResponse {
277 memory_id,
278 name,
279 action: "updated".to_string(),
280 version: next_v,
281 elapsed_ms: inicio.elapsed().as_millis() as u64,
282 backend_invoked,
283 })?;
284
285 Ok(())
286}
287
288#[cfg(test)]
289mod tests {
290 use super::*;
291
292 #[derive(clap::Parser)]
293 struct TestCli {
294 #[command(flatten)]
295 args: EditArgs,
296 }
297
298 #[test]
299 fn type_flag_is_a_visible_alias_of_memory_type() {
300 use clap::Parser;
303 let cli = TestCli::try_parse_from(["edit", "--name", "m", "--type", "decision"])
304 .expect("--type must parse as an alias of --memory-type");
305 assert!(cli.args.memory_type.is_some());
306 let cli = TestCli::try_parse_from(["edit", "--name", "m", "--memory-type", "decision"])
307 .expect("--memory-type must keep working");
308 assert!(cli.args.memory_type.is_some());
309 }
310
311 #[test]
312 fn edit_response_serializes_all_fields() {
313 let resp = EditResponse {
314 memory_id: 42,
315 name: "my-memory".to_string(),
316 action: "updated".to_string(),
317 version: 3,
318 elapsed_ms: 7,
319 backend_invoked: None,
320 };
321 let json = serde_json::to_value(&resp).expect("serialization failed");
322 assert_eq!(json["memory_id"], 42i64);
323 assert_eq!(json["name"], "my-memory");
324 assert_eq!(json["action"], "updated");
325 assert_eq!(json["version"], 3i64);
326 assert!(json["elapsed_ms"].is_number());
327 }
328
329 #[test]
330 fn edit_response_action_contains_updated() {
331 let resp = EditResponse {
332 memory_id: 1,
333 name: "n".to_string(),
334 action: "updated".to_string(),
335 version: 1,
336 elapsed_ms: 0,
337 backend_invoked: None,
338 };
339 assert_eq!(
340 resp.action, "updated",
341 "action must be 'updated' for successful edits"
342 );
343 }
344
345 #[test]
346 fn edit_body_exceeds_limit_returns_error() {
347 let limit = crate::constants::MAX_MEMORY_BODY_LEN;
348 let large_body: String = "a".repeat(limit + 1);
349 assert!(
350 large_body.len() > limit,
351 "body above limit must have length > MAX_MEMORY_BODY_LEN"
352 );
353 }
354
355 #[test]
356 fn edit_description_exceeds_limit_returns_error() {
357 let limit = crate::constants::MAX_MEMORY_DESCRIPTION_LEN;
358 let large_desc: String = "d".repeat(limit + 1);
359 assert!(
360 large_desc.len() > limit,
361 "description above limit must have length > MAX_MEMORY_DESCRIPTION_LEN"
362 );
363 }
364}