@@ -603,7 +603,8 @@ pub async fn trigger_acemcp_index_update(project_root_path: String) -> Result<St
603603pub async fn trigger_acemcp_index_rebuild ( project_root_path : String ) -> Result < String , String > {
604604 // 先清理本地索引记录(projects.json + projects_status.json)
605605 // 全量重建不主动停止文件监听,避免影响自动索引
606- purge_project_index_records ( & project_root_path, false ) ?;
606+ purge_project_index_records ( & project_root_path, false )
607+ . map_err ( |e| format ! ( "全量重建前清理索引记录失败: {}" , e) ) ?;
607608
608609 // 再触发索引更新(全量重建)
609610 AcemcpTool :: trigger_index_update ( project_root_path)
@@ -703,6 +704,10 @@ pub fn stop_all_watching() -> Result<(), String> {
703704// 辅助函数:规范化路径 key(去除扩展路径前缀,统一使用正斜杠)
704705fn normalize_path_key ( path : & str ) -> String {
705706 let mut normalized = path. to_string ( ) ;
707+ // 尝试规范化路径(失败则保持原样)
708+ if let Ok ( canon) = std:: path:: PathBuf :: from ( path) . canonicalize ( ) {
709+ normalized = canon. to_string_lossy ( ) . to_string ( ) ;
710+ }
706711 // 去除 Windows 扩展长度路径前缀
707712 if normalized. starts_with ( "\\ \\ ?\\ " ) {
708713 normalized = normalized[ 4 ..] . to_string ( ) ;
@@ -736,8 +741,19 @@ fn purge_project_index_records(project_root_path: &str, stop_watching: bool) ->
736741 // 1. 从 projects.json 中删除项目的 blob 列表
737742 let projects_path = data_dir. join ( "projects.json" ) ;
738743 if projects_path. exists ( ) {
739- if let Ok ( data) = fs:: read_to_string ( & projects_path) {
740- if let Ok ( mut projects) = serde_json:: from_str :: < HashMap < String , Vec < String > > > ( & data) {
744+ match fs:: read_to_string ( & projects_path) {
745+ Ok ( data) => {
746+ let mut needs_write = false ;
747+ let mut projects: HashMap < String , Vec < String > > = match serde_json:: from_str ( & data) {
748+ Ok ( val) => val,
749+ Err ( e) => {
750+ log:: warn!( "[purge_project_index_records] projects.json 解析失败,将重置文件: {}" , e) ;
751+ needs_write = true ;
752+ projects_deleted = true ;
753+ HashMap :: new ( )
754+ }
755+ } ;
756+
741757 let existing_keys: Vec < & String > = projects. keys ( ) . collect ( ) ;
742758 log:: info!( "[purge_project_index_records] projects.json 中现有项目: {:?}" , existing_keys) ;
743759
@@ -748,14 +764,23 @@ fn purge_project_index_records(project_root_path: &str, stop_watching: bool) ->
748764 if let Some ( key) = key_to_remove {
749765 log:: info!( "[purge_project_index_records] 找到匹配的 key: {}" , key) ;
750766 projects. remove ( & key) ;
751- if let Ok ( new_data) = serde_json:: to_string_pretty ( & projects) {
752- let _ = fs:: write ( & projects_path, new_data) ;
753- log:: info!( "[purge_project_index_records] ✓ 已从 projects.json 删除项目: {}" , key) ;
754- projects_deleted = true ;
755- }
767+ needs_write = true ;
768+ projects_deleted = true ;
769+ } else if needs_write {
770+ log:: warn!( "[purge_project_index_records] 未找到匹配项目,但 projects.json 已被重置" ) ;
756771 } else {
757772 log:: warn!( "[purge_project_index_records] ✗ 在 projects.json 中未找到匹配的项目,规范化路径: {}" , normalized_root) ;
758773 }
774+
775+ if needs_write {
776+ let new_data = serde_json:: to_string_pretty ( & projects)
777+ . map_err ( |e| format ! ( "序列化 projects.json 失败: {} (路径: {})" , e, projects_path. display( ) ) ) ?;
778+ fs:: write ( & projects_path, new_data)
779+ . map_err ( |e| format ! ( "写入 projects.json 失败: {} (路径: {})" , e, projects_path. display( ) ) ) ?;
780+ }
781+ }
782+ Err ( e) => {
783+ return Err ( format ! ( "读取 projects.json 失败: {} (路径: {})" , e, projects_path. display( ) ) ) ;
759784 }
760785 }
761786 } else {
@@ -765,31 +790,42 @@ fn purge_project_index_records(project_root_path: &str, stop_watching: bool) ->
765790 // 2. 从 projects_status.json 中删除项目状态
766791 let status_path = data_dir. join ( "projects_status.json" ) ;
767792 if status_path. exists ( ) {
768- if let Ok ( data) = fs:: read_to_string ( & status_path) {
769- if let Ok ( mut status) = serde_json:: from_str :: < serde_json:: Value > ( & data) {
770- if let Some ( projects) = status. get_mut ( "projects" ) {
771- if let Some ( map) = projects. as_object_mut ( ) {
772- let existing_keys: Vec < & String > = map. keys ( ) . collect ( ) ;
773- log:: info!( "[purge_project_index_records] projects_status.json 中现有项目: {:?}" , existing_keys) ;
774-
775- let key_to_remove: Option < String > = map. keys ( )
776- . find ( |k| normalize_path_key ( k) == normalized_root)
777- . cloned ( ) ;
778-
779- if let Some ( key) = key_to_remove {
780- log:: info!( "[purge_project_index_records] 找到匹配的 key: {}" , key) ;
781- map. remove ( & key) ;
782- if let Ok ( new_data) = serde_json:: to_string_pretty ( & status) {
783- let _ = fs:: write ( & status_path, new_data) ;
784- log:: info!( "[purge_project_index_records] ✓ 已从 projects_status.json 删除项目: {}" , key) ;
785- status_deleted = true ;
786- }
787- } else {
788- log:: warn!( "[purge_project_index_records] ✗ 在 projects_status.json 中未找到匹配的项目,规范化路径: {}" , normalized_root) ;
789- }
793+ match fs:: read_to_string ( & status_path) {
794+ Ok ( data) => {
795+ let mut needs_write = false ;
796+ let mut status: ProjectsIndexStatus = match serde_json:: from_str ( & data) {
797+ Ok ( val) => val,
798+ Err ( e) => {
799+ log:: warn!( "[purge_project_index_records] projects_status.json 解析失败,将重置文件: {}" , e) ;
800+ needs_write = true ;
801+ status_deleted = true ;
802+ ProjectsIndexStatus :: default ( )
790803 }
804+ } ;
805+
806+ let existing_keys: Vec < & String > = status. projects . keys ( ) . collect ( ) ;
807+ log:: info!( "[purge_project_index_records] projects_status.json 中现有项目: {:?}" , existing_keys) ;
808+
809+ if status. projects . remove ( & normalized_root) . is_some ( ) {
810+ needs_write = true ;
811+ status_deleted = true ;
812+ log:: info!( "[purge_project_index_records] ✓ 已从 projects_status.json 删除项目: {}" , normalized_root) ;
813+ } else if needs_write {
814+ log:: warn!( "[purge_project_index_records] 未找到匹配项目,但 projects_status.json 已被重置" ) ;
815+ } else {
816+ log:: warn!( "[purge_project_index_records] ✗ 在 projects_status.json 中未找到匹配的项目,规范化路径: {}" , normalized_root) ;
817+ }
818+
819+ if needs_write {
820+ let new_data = serde_json:: to_string_pretty ( & status)
821+ . map_err ( |e| format ! ( "序列化 projects_status.json 失败: {} (路径: {})" , e, status_path. display( ) ) ) ?;
822+ fs:: write ( & status_path, new_data)
823+ . map_err ( |e| format ! ( "写入 projects_status.json 失败: {} (路径: {})" , e, status_path. display( ) ) ) ?;
791824 }
792825 }
826+ Err ( e) => {
827+ return Err ( format ! ( "读取 projects_status.json 失败: {} (路径: {})" , e, status_path. display( ) ) ) ;
828+ }
793829 }
794830 } else {
795831 log:: warn!( "[purge_project_index_records] projects_status.json 文件不存在: {:?}" , status_path) ;
0 commit comments