Skip to content

Commit d27d163

Browse files
JegernOUTTolegklimov
authored andcommitted
Handle malformed database by removing and reinitializing vecdb
1 parent fa8e521 commit d27d163

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

refact-agent/engine/src/vecdb/vdb_highlev.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,32 @@ async fn _create_vecdb(
7676
&base_dir_cache,
7777
&base_dir_config,
7878
cmdline.clone(),
79-
constants,
79+
constants.clone(),
8080
&api_key
8181
).await {
8282
Ok(res) => Some(res),
8383
Err(err) => {
84-
error!("Ooops database is broken!
85-
Last error message: {}
86-
You can report this issue here:
87-
https://github.com/smallcloudai/refact-lsp/issues
88-
Also, you can run this to erase your db:
89-
`rm -rf ~/.cache/refact/refact_vecdb_cache`
90-
After that restart this LSP server or your IDE.", err);
84+
match crate::vecdb::vdb_sqlite::get_db_path(
85+
&base_dir_cache, &constants.embedding_model, constants.embedding_size
86+
).await.map(|x| PathBuf::from(x)) {
87+
Ok(db_path) => {
88+
if db_path.exists() {
89+
error!("Removing vecdb database: {:?} since it's malformed: {}", db_path, err);
90+
std::fs::remove_file(db_path).map_err(|x| format!("Couldn't remove the malformed vecdb: {x}. Vecdb initialization aborted: {}", err))?;
91+
VecDb::init(
92+
&base_dir_cache,
93+
&base_dir_config,
94+
cmdline.clone(),
95+
constants.clone(),
96+
&api_key
97+
).await.map_err(|x| format!("Cannot initialize vecdb after removing a malformed db: {x}"))?;
98+
}
99+
}
100+
Err(_) => {
101+
error!("Vecdb cannot be initialized: {err}");
102+
}
103+
};
104+
91105
return Err(err);
92106
}
93107
};

refact-agent/engine/src/vecdb/vdb_sqlite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct DataColumn {
2929
type_: String,
3030
}
3131

32-
async fn get_db_path(cache_dir: &PathBuf, model_name: &String, embedding_size: i32) -> Result<String, String> {
32+
pub async fn get_db_path(cache_dir: &PathBuf, model_name: &String, embedding_size: i32) -> Result<String, String> {
3333
let old_path = cache_dir
3434
.join("refact_vecdb_cache")
3535
.join(format!("model_{}_esize_{}.sqlite",

0 commit comments

Comments
 (0)