sqlite_graphrag/commands/
reclassify.rs1use crate::entity_type::EntityType;
11use crate::errors::AppError;
12use crate::i18n::errors_msg;
13use crate::output::{self, OutputFormat};
14use crate::paths::AppPaths;
15use crate::storage::connection::open_rw;
16use crate::storage::entities;
17use rusqlite::params;
18use serde::Serialize;
19
20#[derive(clap::Args)]
21#[command(after_long_help = "EXAMPLES:\n \
22 # Reclassify a single entity from its current type to 'tool'\n \
23 sqlite-graphrag reclassify --name tokio-runtime --new-type tool\n\n \
24 # Reclassify all 'concept' entities to 'tool' in one shot (batch)\n \
25 sqlite-graphrag reclassify --from-type concept --to-type tool --batch\n\n \
26 # Reclassify in a specific namespace\n \
27 sqlite-graphrag reclassify --name alice --new-type person --namespace my-project\n\n\
28NOTE:\n \
29 Single mode requires --name and at least one of --new-type or --description.\n \
30 Batch mode requires --from-type, --to-type and --batch.\n \
31 Providing --name together with --batch is an error.\n\n\
32VALID ENTITY TYPES:\n \
33 project, tool, person, file, concept, incident, decision,\n \
34 memory, dashboard, issue_tracker, organization, location, date")]
35pub struct ReclassifyArgs {
37 #[arg(long, conflicts_with_all = ["from_type", "batch"])]
39 pub name: Option<String>,
40 #[arg(long, value_enum, value_name = "TYPE", visible_alias = "entity-type")]
42 pub new_type: Option<EntityType>,
43 #[arg(long, value_name = "TEXT")]
45 pub description: Option<String>,
46 #[arg(
48 long,
49 value_enum,
50 value_name = "TYPE",
51 requires = "to_type",
52 requires = "batch"
53 )]
54 pub from_type: Option<EntityType>,
55 #[arg(long, value_enum, value_name = "TYPE", requires = "from_type")]
57 pub to_type: Option<EntityType>,
58 #[arg(long, default_value_t = false, requires = "from_type")]
60 pub batch: bool,
61 #[arg(long)]
63 pub namespace: Option<String>,
64 #[arg(long, value_enum, default_value = "json")]
66 pub format: OutputFormat,
67 #[arg(long, hide = true, help = "No-op; JSON is always emitted on stdout")]
69 pub json: bool,
70 #[arg(long)]
72 pub db: Option<String>,
73}
74
75#[derive(Serialize)]
76struct ReclassifyResponse {
77 action: String,
78 count: usize,
79 #[serde(skip_serializing_if = "Option::is_none")]
80 description_updated: Option<bool>,
81 namespace: String,
82 elapsed_ms: u64,
84}
85
86pub fn run(args: ReclassifyArgs) -> Result<(), AppError> {
88 let inicio = std::time::Instant::now();
89 let namespace = crate::namespace::resolve_namespace(args.namespace.as_deref())?;
90 let paths = AppPaths::resolve(args.db.as_deref())?;
91
92 crate::storage::connection::ensure_db_ready(&paths)?;
93
94 let mut conn = open_rw(&paths.db)?;
95
96 let count = if args.batch {
97 let from_type = args.from_type.ok_or_else(|| {
99 AppError::Validation(crate::i18n::validation::from_type_required_batch())
100 })?;
101 let to_type = args.to_type.ok_or_else(|| {
102 AppError::Validation(crate::i18n::validation::to_type_required_batch())
103 })?;
104
105 let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
106 let affected = tx.execute(
107 "UPDATE entities SET type = ?1, updated_at = unixepoch()
108 WHERE type = ?2 AND namespace = ?3",
109 params![to_type.as_str(), from_type.as_str(), namespace],
110 )?;
111 tx.commit()?;
112 if affected == 0 {
113 tracing::warn!(target: "reclassify",
114 from_type = from_type.as_str(),
115 namespace = %namespace,
116 "reclassify batch matched zero entities — verify --from-type value exists"
117 );
118 }
119 affected
120 } else {
121 let entity_name = args.name.as_deref().ok_or_else(|| {
123 AppError::Validation(crate::i18n::validation::name_required_single_mode())
124 })?;
125 if args.new_type.is_none() && args.description.is_none() {
126 return Err(AppError::Validation(
127 "at least one of --new-type or --description is required in single mode"
128 .to_string(),
129 ));
130 }
131
132 entities::find_entity_id(&conn, &namespace, entity_name)?.ok_or_else(|| {
134 AppError::NotFound(errors_msg::entity_not_found(entity_name, &namespace))
135 })?;
136
137 let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
138 let mut affected = 0;
139 if let Some(new_type) = args.new_type {
140 affected = tx.execute(
141 "UPDATE entities SET type = ?1, updated_at = unixepoch()
142 WHERE name = ?2 AND namespace = ?3",
143 params![new_type.as_str(), entity_name, namespace],
144 )?;
145 }
146 if let Some(ref desc) = args.description {
147 let rows = tx.execute(
148 "UPDATE entities SET description = ?1, updated_at = unixepoch()
149 WHERE name = ?2 AND namespace = ?3",
150 params![desc, entity_name, namespace],
151 )?;
152 if affected == 0 {
153 affected = rows;
154 }
155 }
156 tx.commit()?;
157 affected
158 };
159
160 conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
161
162 let response = ReclassifyResponse {
163 action: "reclassified".to_string(),
164 count,
165 description_updated: if args.description.is_some() {
166 Some(true)
167 } else {
168 None
169 },
170 namespace: namespace.clone(),
171 elapsed_ms: inicio.elapsed().as_millis() as u64,
172 };
173
174 match args.format {
175 OutputFormat::Json => output::emit_json(&response)?,
176 OutputFormat::Text | OutputFormat::Markdown => {
177 output::emit_text(&format!(
178 "reclassified: {} entities [{}]",
179 response.count, response.namespace
180 ));
181 }
182 }
183
184 Ok(())
185}
186
187#[cfg(test)]
188mod tests {
189 use super::*;
190
191 #[derive(clap::Parser)]
192 struct TestCli {
193 #[command(flatten)]
194 args: ReclassifyArgs,
195 }
196
197 #[test]
198 fn entity_type_flag_is_a_visible_alias_of_new_type() {
199 use clap::Parser;
202 let cli = TestCli::try_parse_from(["reclassify", "--name", "e", "--entity-type", "tool"])
203 .expect("--entity-type must parse as an alias of --new-type");
204 assert!(cli.args.new_type.is_some());
205 }
206
207 #[test]
208 fn reclassify_response_serializes_all_fields() {
209 let resp = ReclassifyResponse {
210 action: "reclassified".to_string(),
211 count: 5,
212 description_updated: None,
213 namespace: "global".to_string(),
214 elapsed_ms: 12,
215 };
216 let json = serde_json::to_value(&resp).expect("serialization failed");
217 assert_eq!(json["action"], "reclassified");
218 assert_eq!(json["count"], 5);
219 assert_eq!(json["namespace"], "global");
220 assert!(json["elapsed_ms"].is_number());
221 assert!(json.get("description_updated").is_none());
222 }
223
224 #[test]
225 fn reclassify_response_count_zero_is_valid() {
226 let resp = ReclassifyResponse {
227 action: "reclassified".to_string(),
228 count: 0,
229 description_updated: None,
230 namespace: "my-project".to_string(),
231 elapsed_ms: 3,
232 };
233 let json = serde_json::to_value(&resp).expect("serialization failed");
234 assert_eq!(json["count"], 0);
235 assert_eq!(json["action"], "reclassified");
236 }
237
238 #[test]
239 fn reclassify_response_action_is_reclassified() {
240 let resp = ReclassifyResponse {
241 action: "reclassified".to_string(),
242 count: 1,
243 description_updated: None,
244 namespace: "ns".to_string(),
245 elapsed_ms: 1,
246 };
247 assert_eq!(resp.action, "reclassified");
248 }
249
250 #[test]
251 fn reclassify_response_description_updated_present_when_set() {
252 let resp = ReclassifyResponse {
253 action: "reclassified".to_string(),
254 count: 1,
255 description_updated: Some(true),
256 namespace: "global".to_string(),
257 elapsed_ms: 2,
258 };
259 let json = serde_json::to_value(&resp).expect("serialization failed");
260 assert_eq!(json["description_updated"], true);
261 }
262}