Skip to main content

aft/commands/
status.rs

1//! AFT status command — returns the current state of indexes, features, and configuration.
2
3use crate::context::AppContext;
4use crate::context::SemanticIndexStatus;
5use crate::db::compression_events::CompressionAggregate;
6use crate::protocol::{RawRequest, Response, StatusPayload, DEFAULT_SESSION_ID};
7
8#[derive(Debug, Clone, Default, serde::Serialize)]
9pub struct CompressionStats {
10    pub project: CompressionAggregateSerde,
11    pub session: CompressionAggregateSerde,
12}
13
14#[derive(Debug, Clone, Default, serde::Serialize)]
15pub struct CompressionAggregateSerde {
16    pub events: u64,
17    pub original_tokens: u64,
18    pub compressed_tokens: u64,
19    pub savings_tokens: u64,
20}
21
22impl From<CompressionAggregate> for CompressionAggregateSerde {
23    fn from(agg: CompressionAggregate) -> Self {
24        Self {
25            events: agg.events,
26            original_tokens: agg.original_tokens,
27            compressed_tokens: agg.compressed_tokens,
28            savings_tokens: agg.savings_tokens(),
29        }
30    }
31}
32
33pub fn handle_status(req: &RawRequest, ctx: &AppContext) -> Response {
34    Response::success(
35        &req.id,
36        ctx.build_status_snapshot_for_session(req.session()),
37    )
38}
39
40impl AppContext {
41    pub fn build_status_snapshot(&self) -> StatusPayload {
42        self.build_status_snapshot_for_session(DEFAULT_SESSION_ID)
43    }
44
45    pub fn build_status_snapshot_for_session(&self, session_id: &str) -> StatusPayload {
46        let config = self.config();
47
48        // Search index status
49        let search_index_info = {
50            let index = self
51                .search_index()
52                .read()
53                .unwrap_or_else(std::sync::PoisonError::into_inner);
54            match index.as_ref() {
55                Some(idx) if idx.ready => {
56                    let file_count = idx.file_count();
57                    let trigram_count = idx.trigram_count();
58                    serde_json::json!({
59                        "status": "ready",
60                        "files": file_count,
61                        "trigrams": trigram_count,
62                    })
63                }
64                Some(_) => serde_json::json!({ "status": "building" }),
65                None => {
66                    let status = if config.search_index {
67                        "loading"
68                    } else {
69                        "disabled"
70                    };
71                    serde_json::json!({ "status": status })
72                }
73            }
74        };
75
76        // Semantic index status
77        let semantic_index_info = {
78            let status = self
79                .semantic_index_status()
80                .read()
81                .unwrap_or_else(std::sync::PoisonError::into_inner)
82                .clone();
83            let refreshing_count = status.refreshing_count();
84            let index = self
85                .semantic_index()
86                .read()
87                .unwrap_or_else(std::sync::PoisonError::into_inner);
88            match index.as_ref() {
89                Some(idx) => {
90                    let status_label = match status {
91                        SemanticIndexStatus::Ready { .. } => "ready",
92                        _ => idx.status_label(),
93                    };
94                    serde_json::json!({
95                        "status": status_label,
96                        "state": status_label,
97                        "refreshing_count": refreshing_count,
98                        "entries": idx.entry_count(),
99                        "dimension": idx.dimension(),
100                        "backend": idx.backend_label().unwrap_or(config.semantic_backend_label()),
101                        "model": idx.model_label().unwrap_or(config.semantic.model.as_str()),
102                    })
103                }
104                None => match status {
105                    SemanticIndexStatus::Disabled => serde_json::json!({
106                        "status": "disabled",
107                        "state": "disabled",
108                        "refreshing_count": 0,
109                        "backend": config.semantic_backend_label(),
110                        "model": config.semantic.model.as_str(),
111                    }),
112                    SemanticIndexStatus::Building {
113                        stage,
114                        files,
115                        entries_done,
116                        entries_total,
117                    } => serde_json::json!({
118                        "status": "loading",
119                        "state": "loading",
120                        "refreshing_count": 0,
121                        "stage": stage,
122                        "files": files,
123                        "entries_done": entries_done,
124                        "entries_total": entries_total,
125                        "backend": config.semantic_backend_label(),
126                        "model": config.semantic.model.as_str(),
127                    }),
128                    SemanticIndexStatus::Ready { refreshing, .. } => serde_json::json!({
129                        "status": "ready",
130                        "state": "ready",
131                        "refreshing_count": refreshing.len(),
132                        "backend": config.semantic_backend_label(),
133                        "model": config.semantic.model.as_str(),
134                    }),
135                    SemanticIndexStatus::Failed(error) => serde_json::json!({
136                        "status": "failed",
137                        "state": "failed",
138                        "refreshing_count": 0,
139                        "error": error,
140                        "backend": config.semantic_backend_label(),
141                        "model": config.semantic.model.as_str(),
142                    }),
143                },
144            }
145        };
146
147        // Disk cache sizes — scoped to the **current project** only.
148        //
149        // Both trigram (`<storage_dir>/index/<key>/`) and semantic
150        // (`<storage_dir>/semantic/<key>/`) caches are partitioned per project by
151        // `project_cache_key(project_root)`. Earlier this function reported the
152        // recursive size of the entire `index/` and `semantic/` directories,
153        // which summed disk usage across **every** project the user had ever
154        // opened. The TUI sidebar surfaced that total as if it were the current
155        // project's footprint, which was misleading (e.g. a 4.8 MB project with
156        // 9 sibling projects appeared to use 16+ GB).
157        //
158        // We now resolve the per-project key from `config.project_root` and
159        // size only that project's slice. When the project key can't be
160        // resolved (no project_root), fall back to zeros — the cross-project
161        // total is never the right answer to display per-session.
162        let storage_dir = config.storage_dir.as_ref().map(|d| d.display().to_string());
163        let disk_info = match (&config.storage_dir, &config.project_root) {
164            (Some(dir), Some(root)) => {
165                let key = crate::search_index::artifact_cache_key(root);
166                let trigram_size = dir_size(&dir.join("index").join(&key));
167                let semantic_size = dir_size(&dir.join("semantic").join(&key));
168                serde_json::json!({
169                    "storage_dir": dir.display().to_string(),
170                    "project_cache_key": key,
171                    "trigram_disk_bytes": trigram_size,
172                    "semantic_disk_bytes": semantic_size,
173                })
174            }
175            (Some(dir), None) => serde_json::json!({
176                "storage_dir": dir.display().to_string(),
177                "project_cache_key": null,
178                "trigram_disk_bytes": 0,
179                "semantic_disk_bytes": 0,
180            }),
181            _ => serde_json::json!({
182                "storage_dir": null,
183                "project_cache_key": null,
184                "trigram_disk_bytes": 0,
185                "semantic_disk_bytes": 0,
186            }),
187        };
188
189        // LSP servers
190        let lsp_count = self.lsp_server_count();
191
192        // Symbol cache stats
193        let symbol_cache_stats = self.symbol_cache_stats();
194
195        // Per-session undo/checkpoint counts (issue #14 — one shared bridge serves
196        // many sessions; surface both the global footprint and the current
197        // session's own slice so `/aft-status` can split them in the UI).
198        let backups_enabled = config.backup.enabled.unwrap_or(true);
199        let checkpoint_total = if backups_enabled {
200            self.checkpoint().lock().total_count()
201        } else {
202            0
203        };
204        let session_checkpoints = if backups_enabled {
205            self.checkpoint().lock().list(session_id).len()
206        } else {
207            0
208        };
209        let session_tracked_files = if backups_enabled {
210            self.backup().lock().tracked_files(session_id).len()
211        } else {
212            0
213        };
214        let compression = self.compression_stats_for_session(session_id);
215
216        // Degraded-mode reasons recorded by `handle_configure` when the
217        // project root doesn't look like a real project (`home_root`). Heavy
218        // subsystems are auto-disabled in that mode; the plugin / TUI sidebar
219        // surface the reason so users know why and can decide whether to open a
220        // project subdirectory. Empty list = full-featured mode.
221        let degraded_reasons = self.degraded_reasons();
222        let degraded = !degraded_reasons.is_empty();
223
224        // Agent status-bar counts (the `[AFT E· W· | D· U· C· | T·]` glance).
225        // Surfaced for the TUI sidebar so users see the same code-health view
226        // agents get. `None` until the Tier-2 cache is populated at least once
227        // (so we never render fabricated zeros) — emitted as JSON null then,
228        // and the sidebar hides the section.
229        let status_bar = match self.status_bar_counts() {
230            Some(counts) => serde_json::json!({
231                "errors": counts.errors,
232                "warnings": counts.warnings,
233                "dead_code": counts.dead_code,
234                "unused_exports": counts.unused_exports,
235                "duplicates": counts.duplicates,
236                "todos": counts.todos,
237                "tier2_stale": counts.tier2_stale,
238            }),
239            None => serde_json::Value::Null,
240        };
241
242        serde_json::json!({
243            "version": env!("CARGO_PKG_VERSION"),
244            "project_root": config.project_root.as_ref().map(|p| p.display().to_string()),
245            "canonical_root": self.canonical_cache_root_opt().map(|p| p.display().to_string()),
246            "cache_role": self.cache_role(),
247            "degraded": degraded,
248            "degraded_reasons": degraded_reasons,
249            "features": {
250                "format_on_edit": config.format_on_edit,
251                "validate_on_edit": config.validate_on_edit.as_deref().unwrap_or("off"),
252                "restrict_to_project_root": config.restrict_to_project_root,
253                "search_index": config.search_index,
254                "semantic_search": config.semantic_search,
255                "callgraph_store": config.callgraph_store,
256                "backup": backups_enabled,
257            },
258            "search_index": search_index_info,
259            "semantic_index": semantic_index_info,
260            "status_bar": status_bar,
261            "disk": disk_info,
262            "lsp_servers": lsp_count,
263            "symbol_cache": symbol_cache_stats,
264            "compression": compression,
265            "storage_dir": storage_dir,
266            // Project-wide (all sessions): total in-memory checkpoint count.
267            "checkpoints_total": checkpoint_total,
268            // Current session slice: only when the caller passed `session_id`.
269            "session": {
270                "id": session_id,
271                "tracked_files": session_tracked_files,
272                "checkpoints": session_checkpoints,
273            },
274        })
275    }
276
277    fn compression_stats_for_session(&self, session_id: &str) -> CompressionStats {
278        let mut compression = CompressionStats::default();
279        let Some(project_root) = self.config().project_root.clone() else {
280            return compression;
281        };
282        let Some(db) = self.db() else {
283            return compression;
284        };
285        let Ok(conn) = db.lock() else {
286            return compression;
287        };
288
289        let harness = self.harness().storage_segment();
290        let project_key = crate::path_identity::project_scope_key(&project_root);
291        if let Ok(project_agg) =
292            crate::db::compression_events::aggregate_for_project(&conn, &harness, &project_key)
293        {
294            compression.project = project_agg.into();
295        }
296        if let Ok(session_agg) = crate::db::compression_events::aggregate_for_session(
297            &conn,
298            &harness,
299            &project_key,
300            session_id,
301        ) {
302            compression.session = session_agg.into();
303        }
304
305        compression
306    }
307}
308
309/// Recursively compute the total size of a directory.
310fn dir_size(path: &std::path::Path) -> u64 {
311    if !path.exists() {
312        return 0;
313    }
314    dir_size_recursive(path)
315}
316
317fn dir_size_recursive(path: &std::path::Path) -> u64 {
318    let mut total = 0u64;
319    let entries = match std::fs::read_dir(path) {
320        Ok(e) => e,
321        Err(_) => return 0,
322    };
323    for entry in entries.flatten() {
324        let ft = match entry.file_type() {
325            Ok(ft) => ft,
326            Err(_) => continue,
327        };
328        if ft.is_file() {
329            total += entry.metadata().map(|m| m.len()).unwrap_or(0);
330        } else if ft.is_dir() {
331            total += dir_size_recursive(&entry.path());
332        }
333    }
334    total
335}
336
337#[cfg(test)]
338mod tests {
339    use super::handle_status;
340    use crate::config::Config;
341    use crate::context::AppContext;
342    use crate::parser::TreeSitterProvider;
343    use crate::protocol::RawRequest;
344    use serde_json::json;
345
346    fn request() -> RawRequest {
347        RawRequest {
348            id: "status".to_string(),
349            command: "status".to_string(),
350            lsp_hints: None,
351            session_id: None,
352            params: json!({}),
353        }
354    }
355
356    #[test]
357    fn status_exposes_cache_role_and_canonical_root() {
358        let ctx = AppContext::new(Box::new(TreeSitterProvider::new()), Config::default());
359        let response = handle_status(&request(), &ctx);
360        assert_eq!(response.data["cache_role"], "not_initialized");
361        assert!(response.data["canonical_root"].is_null());
362
363        let temp = tempfile::tempdir().unwrap();
364        ctx.update_config(|config| {
365            config.project_root = Some(temp.path().to_path_buf());
366        });
367        ctx.set_canonical_cache_root(std::fs::canonicalize(temp.path()).unwrap());
368        ctx.set_cache_role(false, None);
369        let response = handle_status(&request(), &ctx);
370        assert_eq!(response.data["cache_role"], "main");
371        assert!(response.data["canonical_root"].as_str().is_some());
372
373        ctx.set_cache_role(true, None);
374        let response = handle_status(&request(), &ctx);
375        assert_eq!(response.data["cache_role"], "worktree");
376    }
377
378    #[test]
379    fn status_status_bar_is_null_until_tier2_populated() {
380        let ctx = AppContext::new(Box::new(TreeSitterProvider::new()), Config::default());
381        let response = handle_status(&request(), &ctx);
382        // No Tier-2 scan has run yet, so the status-bar glance must be null
383        // (never fabricated zeros). The key is always present so the TS
384        // coercion can distinguish "field absent" from "not populated".
385        assert!(response.data.get("status_bar").is_some());
386        assert!(response.data["status_bar"].is_null());
387
388        // Once Tier-2 counts are populated, the snapshot carries the glance.
389        ctx.update_status_bar_tier2(Some(3), Some(2), Some(1), Some(5), false);
390        let response = handle_status(&request(), &ctx);
391        assert_eq!(response.data["status_bar"]["dead_code"], 3);
392        assert_eq!(response.data["status_bar"]["unused_exports"], 2);
393        assert_eq!(response.data["status_bar"]["duplicates"], 1);
394        assert_eq!(response.data["status_bar"]["tier2_stale"], false);
395    }
396}