diff --git a/codex-rs/tui/src/bottom_pane/chat_composer.rs b/codex-rs/tui/src/bottom_pane/chat_composer.rs index 52156cad0a..8f629baefa 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer.rs @@ -37,6 +37,7 @@ //! owning plugin is listed. `$` lists individual skills and apps, but not plugins. //! Disabling `mentions_v2` restores file-only `@` search and adds plugins back to `$`. //! Confirmed `$` picker selections inform future ordering within each match tier. +//! A cached plugin skill keeps its ranking when its installed version changes. //! //! # Key Event Routing //! @@ -4263,8 +4264,7 @@ impl ChatComposer { popup.set_mentions(mentions); } _ => { - let mut popup = - SkillPopup::new(mentions).with_frequencies(self.mention_usage.frequencies()); + let mut popup = SkillPopup::new(mentions).with_frequencies(&self.mention_usage); popup.set_query(&query); self.popups.active = ActivePopup::Skill(popup); } @@ -7165,16 +7165,48 @@ mod tests { #[test] fn skill_popup_prefers_confirmed_selections_within_match_tier() { + let home = tempfile::tempdir().expect("temporary home"); + let cache = home + .path() + .join(codex_core_plugins::store::PLUGINS_CACHE_DIR); + #[cfg(unix)] + { + let target = home.path().join("plugin-cache"); + std::fs::create_dir(&target).unwrap(); + std::fs::create_dir(cache.parent().unwrap()).unwrap(); + std::os::unix::fs::symlink(target, &cache).unwrap(); + } + #[cfg(not(unix))] + std::fs::create_dir_all(&cache).unwrap(); + let old_path = cache.join("example/tools/1.0.0/skills/status/SKILL.md"); + std::fs::write( + home.path().join("mention_usage.json"), + serde_json::to_vec(&[old_path]).unwrap(), + ) + .unwrap(); + let current_path = dunce::canonicalize(cache) + .unwrap() + .join("example/tools/2.0.0/skills/status/SKILL.md"); + let status = SkillMetadata { + path: codex_utils_path_uri::PathUri::from_host_native_path(current_path) + .unwrap() + .into(), + ..test_skill_metadata("status") + }; snapshot_composer_state( "skill_popup_prefers_confirmed_selections_within_match_tier", /*enhanced_keys_supported*/ false, |composer| { + composer.mention_usage = MentionUsage::load( + home.path(), + codex_config::types::HistoryPersistence::SaveAll, + ); composer.set_skill_mentions(Some(vec![ test_skill_metadata("statsig"), - test_skill_metadata("status"), + status.clone(), test_skill_metadata("hello-statsig"), ])); - for name in ["hello-statsig", "hello-statsig", "status"] { + for name in ["hello-statsig", "hello-statsig"] { let text = format!("${name}"); composer.set_text_content(text.clone(), Vec::new(), Vec::new()); composer.set_current_cursor(text.len()); @@ -7191,8 +7223,8 @@ mod tests { assert_eq!( popup .selected_mention() - .map(|item| item.insert_text.as_str()), - Some("$status") + .map(|item| (item.insert_text.as_str(), item.path.as_deref())), + Some(("$status", Some(status.path.as_str()))) ); }, ); diff --git a/codex-rs/tui/src/bottom_pane/mention_usage.rs b/codex-rs/tui/src/bottom_pane/mention_usage.rs index 3ba298bce4..f06c63cffc 100644 --- a/codex-rs/tui/src/bottom_pane/mention_usage.rs +++ b/codex-rs/tui/src/bottom_pane/mention_usage.rs @@ -95,6 +95,10 @@ impl MentionUsage { counts } + pub(super) fn codex_home(&self) -> Option<&Path> { + self.storage.as_ref()?.path.parent() + } + pub(super) fn record(&mut self, identity: &str) { append_recent(&mut self.recent, identity.to_owned()); let Some(Storage { diff --git a/codex-rs/tui/src/bottom_pane/skill_popup.rs b/codex-rs/tui/src/bottom_pane/skill_popup.rs index 4e51cd4f8b..654bdae8e6 100644 --- a/codex-rs/tui/src/bottom_pane/skill_popup.rs +++ b/codex-rs/tui/src/bottom_pane/skill_popup.rs @@ -1,3 +1,5 @@ +use codex_core_plugins::store::PLUGINS_CACHE_DIR; +use codex_utils_path_uri::PathUri; use crossterm::event::KeyCode; use ratatui::buffer::Buffer; use ratatui::layout::Constraint; @@ -9,7 +11,11 @@ use ratatui::widgets::Widget; use ratatui::widgets::WidgetRef; use std::cmp::Reverse; use std::collections::HashMap; +use std::path::Component; +use std::path::Path; +use std::path::PathBuf; +use super::mention_usage::MentionUsage; use super::picker_rows::render_rows_single_line; use super::popup_consts::MAX_POPUP_ROWS; use super::scroll_state::ScrollState; @@ -90,6 +96,7 @@ pub(crate) struct SkillPopup { query: String, mentions: Vec, frequencies: HashMap, + plugin_cache_roots: Vec, state: ScrollState, } @@ -99,15 +106,54 @@ impl SkillPopup { query: String::new(), mentions, frequencies: HashMap::new(), + plugin_cache_roots: Vec::new(), state: ScrollState::new(), } } - pub(crate) fn with_frequencies(mut self, frequencies: HashMap) -> Self { - self.frequencies = frequencies; + pub(super) fn with_frequencies(mut self, usage: &MentionUsage) -> Self { + if let Some(home) = usage.codex_home() { + let root = home.join(PLUGINS_CACHE_DIR); + // Skill discovery returns canonical paths rendered for the app server API. + if let Ok(canonical) = + dunce::canonicalize(&root).and_then(PathUri::from_host_native_path) + { + self.plugin_cache_roots + .push(canonical.inferred_native_path_string().into()); + } + self.plugin_cache_roots.push(root); + } + for (path, count) in usage.frequencies() { + let key = self.plugin_skill_key(&path).unwrap_or(path); + *self.frequencies.entry(key).or_default() += count; + } self } + fn plugin_skill_key(&self, path: &str) -> Option { + let skill = Path::new(path); + let relative = self + .plugin_cache_roots + .iter() + .find_map(|root| skill.strip_prefix(root).ok())?; + let mut parts = relative.components(); + if let ( + Some(Component::Normal(marketplace)), + Some(Component::Normal(plugin)), + Some(Component::Normal(_version)), + ) = (parts.next(), parts.next(), parts.next()) + && parts + .as_path() + .file_name() + .is_some_and(|name| name == "SKILL.md") + { + let mut key = Path::new(marketplace).join(plugin).join("*"); + key.extend(parts.map(Component::as_os_str)); + return Some(format!("plugin-skill://{}", key.display())); + } + None + } + pub(crate) fn set_mentions(&mut self, mentions: Vec) { self.mentions = mentions; self.clamp_selection(); @@ -212,8 +258,11 @@ impl SkillPopup { }; let frequency = mention .path - .as_ref() - .and_then(|path| self.frequencies.get(path)) + .as_deref() + .and_then(|path| { + let key = self.plugin_skill_key(path); + self.frequencies.get(key.as_deref().unwrap_or(path)) + }) .copied() .unwrap_or_default(); ( @@ -461,8 +510,8 @@ mod tests { &["logging-best-practices", "Logging Best Practices"], ), named_mention_item("PR Babysitter", &["babysit-pr", "PR Babysitter"]), - ]) - .with_frequencies(HashMap::from([("plugin://github".to_owned(), 50)])); + ]); + popup.frequencies.insert("plugin://github".to_owned(), 50); popup.set_query("pr"); let filtered_items: Vec<(String, Option)> = popup