diff --git a/codex-rs/core/tests/suite/scenarios.rs b/codex-rs/core/tests/suite/scenarios.rs index 8b0d38baa8..801be6ad20 100644 --- a/codex-rs/core/tests/suite/scenarios.rs +++ b/codex-rs/core/tests/suite/scenarios.rs @@ -2335,7 +2335,7 @@ async fn skill_read_recovers_omitted_installation_and_reads_selected_environment )?; let cwd = PathUri::from_host_native_path(&workspace_path)?; test.submit_turn_with_environments( - "Read the review and deployment instructions, find all deployment installations, then read the instructions from env-04.", + "Follow [$deploy](skill://executor-04/deploy/SKILL.md). Read the review and deployment instructions, find all deployment installations, then read the instructions from env-04.", Some(vec![TurnEnvironmentSelection { selected_capability_roots: Default::default(), environment_id: "env-04".to_string(), @@ -2378,7 +2378,7 @@ async fn skill_read_recovers_omitted_installation_and_reads_selected_environment insta::assert_snapshot!( "skill_read_installation_recovery_request_history", context_snapshot::format_context_snapshot( - "Astra reads Cloud instructions, discovers an omitted deployment installation, and reads its instructions from env-04.", + "Astra loads the explicitly selected env-04 skill, reads Cloud instructions, discovers an omitted installation, and reads env-04 again.", &bodies.iter().map(SnapshotEntry::body).collect::>(), &ContextSnapshotOptions::default().rewrite_known_segments(), ) diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__skill_read_installation_recovery_request_history.snap b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__skill_read_installation_recovery_request_history.snap index 7d34aca660..cb1ec81a6f 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__skill_read_installation_recovery_request_history.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__skill_read_installation_recovery_request_history.snap @@ -1,8 +1,8 @@ --- source: core/tests/suite/scenarios.rs -expression: "context_snapshot::format_context_snapshot(\"Astra reads Cloud instructions, discovers an omitted deployment installation, and reads its instructions from env-04.\",\n&bodies.iter().map(SnapshotEntry::body).collect::>(),\n&ContextSnapshotOptions::default().rewrite_known_segments(),)" +expression: "context_snapshot::format_context_snapshot(\"Astra loads the explicitly selected env-04 skill, reads Cloud instructions, discovers an omitted installation, and reads env-04 again.\",\n&bodies.iter().map(SnapshotEntry::body).collect::>(),\n&ContextSnapshotOptions::default().rewrite_known_segments(),)" --- -Scenario: Astra reads Cloud instructions, discovers an omitted deployment installation, and reads its instructions from env-04. +Scenario: Astra loads the explicitly selected env-04 skill, reads Cloud instructions, discovers an omitted installation, and reads env-04 again. ## Window 1 -- request 1 (request) -- @@ -33,11 +33,18 @@ Scenario: Astra reads Cloud instructions, discovers an omitted deployment instal 05:message/user: 06:message/user: - Read the review and deployment instructions, find all deployment installations, then read the instructions from env-04. -07:configuration_update:reasoning={"effort":"low"} + Follow [$deploy](skill://executor-04/deploy/SKILL.md). Read the review and deployment instructions, find...nstallations, then read the instructions from env-04. [hash=4a1e6a02ee134811] +07:message/user: + + deploy + skill://executor-04/deploy/SKILL.md + # Deployment in env-04 + Run scripts/check.py from this installation. + +08:configuration_update:reasoning={"effort":"low"} -- request 2 (request) -- -08:function_call/skills.read:{"package":"skill://executor-00/review","source":{"kind":"cloud"}} -09:function_call_output:{ +09:function_call/skills.read:{"package":"skill://executor-00/review","source":{"kind":"cloud"}} +10:function_call_output:{ "contents": "# Deployment\\nRun scripts/check.py from an available installation.", "installations": [ { @@ -57,8 +64,8 @@ Scenario: Astra reads Cloud instructions, discovers an omitted deployment instal "resource": "skill://deployment/review/SKILL.md" } -- request 3 (request) -- -10:function_call/skills.read:{"package":"skill://deployment/deploy"} -11:function_call_output:{ +11:function_call/skills.read:{"package":"skill://deployment/deploy"} +12:function_call_output:{ "contents": "# Deployment\\nRun scripts/check.py from an available installation.", "installations": [ { @@ -76,8 +83,8 @@ Scenario: Astra reads Cloud instructions, discovers an omitted deployment instal "resource": "skill://deployment/deploy/SKILL.md" } -- request 4 (request) -- -12:function_call/skills.installations:{"package":"skill://deployment/deploy"} -13:function_call_output:{ +13:function_call/skills.installations:{"package":"skill://deployment/deploy"} +14:function_call_output:{ "installations": [ { "package": "skill://deployment/deploy", @@ -95,8 +102,8 @@ Scenario: Astra reads Cloud instructions, discovers an omitted deployment instal "next_cursor": null } -- request 5 (request) -- -14:function_call/skills.read:{"package":"skill://deployment/deploy","source":{"environment_id":"env-04","kind":"executor"}} -15:function_call_output:{ +15:function_call/skills.read:{"package":"skill://deployment/deploy","source":{"environment_id":"env-04","kind":"executor"}} +16:function_call_output:{ "contents": "# Deployment in env-04\\nRun scripts/check.py from this installation.", "installations": [ { diff --git a/codex-rs/ext/skills/src/extension.rs b/codex-rs/ext/skills/src/extension.rs index c9abd4aca4..88855e78c9 100644 --- a/codex-rs/ext/skills/src/extension.rs +++ b/codex-rs/ext/skills/src/extension.rs @@ -29,9 +29,9 @@ use codex_protocol::openai_models::ModelInfo; use crate::SkillsExtensionConfig; use crate::catalog::SkillCatalog; -use crate::catalog::SkillCatalogEntry; use crate::catalog::SkillReadResult; use crate::catalog::SkillSourceKind; +use crate::catalog::SkillSourceLocation; use crate::fragments::AvailableSkillsInstructions; use crate::fragments::SkillInstructions; use crate::fragments::SkillResourceAccess; @@ -385,10 +385,10 @@ where let mut injected_host_skill_prompts = InjectedHostSkillPrompts::default(); let analytics = SkillAnalytics::from_stores(session_store, thread_store); - for entry in &selected_entries { + for (entry, source) in &selected_entries { match self .read_main_prompt( - entry, + source, host_snapshot.clone(), mcp_resources.clone(), &thread_state, @@ -413,29 +413,32 @@ where let fragment = SkillInstructions { name: truncate_utf8_to_bytes(&entry.name, MAX_SKILL_NAME_BYTES).0, path: truncate_utf8_to_bytes( - entry.rendered_path(), + source + .display_path + .as_deref() + .unwrap_or(source.main_prompt.as_str()), MAX_SKILL_PATH_BYTES, ) .0, contents, - resource_access: (!entry.prompt_visible) - .then_some(&entry.authority) + resource_access: (!source.prompt_visible) + .then_some(&source.authority) .and_then(SkillToolAuthority::from_authority) .map(|authority| SkillResourceAccess { authority, - package: entry.id.0.clone(), - main_resource: entry.main_prompt.as_str().to_string(), + package: source.package.0.clone(), + main_resource: source.main_prompt.as_str().to_string(), }), }; fragments.push(Box::new(fragment)); - if entry.authority.kind == SkillSourceKind::Host { - injected_host_skill_prompts.insert_path(entry.main_prompt.as_str()); + if source.authority.kind == SkillSourceKind::Host { + injected_host_skill_prompts.insert_path(source.main_prompt.as_str()); } else if let Some(analytics) = analytics.as_ref() && let Some(model_info) = thread_store.get::() { analytics.track_skill_invocation( entry, - &entry.primary_source(), + source, model_info.slug.clone(), input.turn_id.clone(), InvocationType::Explicit, @@ -454,9 +457,9 @@ where } if let Some(host_snapshot) = &host_snapshot { - for entry in selected_entries + for (entry, _) in selected_entries .iter() - .filter(|entry| entry.authority.kind != SkillSourceKind::Host) + .filter(|(_, source)| source.authority.kind != SkillSourceKind::Host) { for host_skill in host_snapshot .outcome() @@ -481,16 +484,16 @@ where } impl SkillsExtension { - #[tracing::instrument(level = "trace", skip_all, fields(skill = %entry.name))] + #[tracing::instrument(level = "trace", skip_all, fields(package = %source.package.0))] async fn read_main_prompt( &self, - entry: &SkillCatalogEntry, + source: &SkillSourceLocation, host_snapshot: Option>, mcp_resources: Option>, thread_state: &SkillsThreadState, input: &TurnInputContext<'_>, ) -> Result { - let context = match entry.authority.kind { + let context = match source.authority.kind { SkillSourceKind::Cloud => SkillReadContext::Cloud { mcp_resources }, SkillSourceKind::Custom(_) => SkillReadContext::Custom { mcp_resources }, SkillSourceKind::Host => SkillReadContext::Host { @@ -502,7 +505,7 @@ impl SkillsExtension { .collect(), }, SkillSourceKind::Executor => { - let fs = entry + let fs = source .main_prompt .environment_path() .and_then(|(id, _)| { @@ -522,9 +525,9 @@ impl SkillsExtension { .read_skill( &self.providers, SkillReadRequest { - authority: entry.authority.clone(), - package: entry.id.clone(), - resource: entry.main_prompt.clone(), + authority: source.authority.clone(), + package: source.package.clone(), + resource: source.main_prompt.clone(), context, }, ) diff --git a/codex-rs/ext/skills/src/selection.rs b/codex-rs/ext/skills/src/selection.rs index 9d100f0c0d..65413d3b1b 100644 --- a/codex-rs/ext/skills/src/selection.rs +++ b/codex-rs/ext/skills/src/selection.rs @@ -7,6 +7,7 @@ use crate::catalog::SkillAuthority; use crate::catalog::SkillCatalog; use crate::catalog::SkillCatalogEntry; use crate::catalog::SkillPackageId; +use crate::catalog::SkillSourceLocation; const SKILL_PATH_PREFIX: &str = "skill://"; @@ -21,20 +22,19 @@ const SKILL_PATH_PREFIX: &str = "skill://"; pub(crate) fn collect_explicit_skill_mentions( inputs: &[UserInput], catalog: &SkillCatalog, -) -> Vec { - let mut selected = Vec::new(); - let mut seen = HashSet::new(); +) -> Vec<(SkillCatalogEntry, SkillSourceLocation)> { + let mut choices = Vec::new(); let mut blocked_plain_names = HashSet::new(); for input in inputs { match input { UserInput::Skill { name, path } => { blocked_plain_names.insert(name.clone()); - select_by_path(catalog, &path.to_string_lossy(), &mut seen, &mut selected); + select_by_path(catalog, &path.to_string_lossy(), &mut choices); } UserInput::Mention { name, path } if path_is_skill(path) => { blocked_plain_names.insert(name.clone()); - select_by_path(catalog, path, &mut seen, &mut selected); + select_by_path(catalog, path, &mut choices); } UserInput::Text { .. } | UserInput::Image { .. } @@ -54,12 +54,7 @@ pub(crate) fn collect_explicit_skill_mentions( let mentions = extract_tool_mentions(text); for path in mentions.paths() { if path_is_skill(path) { - select_by_path( - catalog, - normalize_skill_path(path), - &mut seen, - &mut selected, - ); + select_by_path(catalog, path, &mut choices); } } for name in mentions.plain_names() { @@ -71,51 +66,69 @@ pub(crate) fn collect_explicit_skill_mentions( .iter() .find(|entry| entry.enabled && entry.name == name) { - push_selected(entry, &mut seen, &mut selected); + choices.push((entry, None)); } } } + // Only use a skill's default if no mention chose a specific copy, regardless of order. + let mut chosen_skills: HashSet<_> = choices + .iter() + .filter(|(_, source)| source.is_some()) + .map(|(entry, _)| (&entry.authority, &entry.id)) + .collect(); + let mut seen = HashSet::new(); + let mut selected = Vec::new(); + for (entry, source) in choices { + if source.is_none() && !chosen_skills.insert((&entry.authority, &entry.id)) { + continue; + } + let source = source.unwrap_or_else(|| entry.primary_source()); + if seen.insert(SkillCatalogEntryKey::from(&source)) { + selected.push((entry.clone(), source)); + } + } selected } -fn select_by_path( - catalog: &SkillCatalog, +fn select_by_path<'a>( + catalog: &'a SkillCatalog, path: &str, - seen: &mut HashSet, - selected: &mut Vec, + choices: &mut Vec<(&'a SkillCatalogEntry, Option)>, ) { let normalized_path = normalize_skill_path(path); - for entry in catalog.entries.iter().filter(|entry| entry.enabled) { - if entry_matches_path(entry, normalized_path) { - push_selected(entry, seen, selected); - } + let mut matches = catalog + .entries + .iter() + .filter(|entry| entry.enabled) + .flat_map(|entry| { + entry + .source_locations() + .filter(move |source| { + source.enabled + && [ + Some(source.main_prompt.as_str()), + Some(source.package.0.as_str()), + source.display_path.as_deref(), + ] + .into_iter() + .flatten() + .any(|path| normalize_skill_path(path) == normalized_path) + }) + .map(move |source| (entry, source)) + }) + .peekable(); + let Some((entry, source)) = matches.next() else { + return; + }; + if matches.peek().is_none() { + choices.push((entry, Some(source))); + } else { + choices.push((entry, None)); + choices.extend(matches.map(|(entry, _)| (entry, None))); } } -fn push_selected( - entry: &SkillCatalogEntry, - seen: &mut HashSet, - selected: &mut Vec, -) { - let key = SkillCatalogEntryKey::from(entry); - if seen.insert(key) { - selected.push(entry.clone()); - } -} - -fn entry_matches_path(entry: &SkillCatalogEntry, path: &str) -> bool { - entry.source_locations().any(|source| { - source.enabled - && (source.main_prompt.as_str() == path - || source.package.0 == path - || source - .display_path - .as_deref() - .is_some_and(|display_path| normalize_skill_path(display_path) == path)) - }) -} - fn path_is_skill(path: &str) -> bool { path.starts_with(SKILL_PATH_PREFIX) || path @@ -132,13 +145,15 @@ fn normalize_skill_path(path: &str) -> &str { struct SkillCatalogEntryKey { authority: SkillAuthority, package: SkillPackageId, + environment_id: Option, } -impl From<&SkillCatalogEntry> for SkillCatalogEntryKey { - fn from(entry: &SkillCatalogEntry) -> Self { +impl From<&SkillSourceLocation> for SkillCatalogEntryKey { + fn from(source: &SkillSourceLocation) -> Self { Self { - authority: entry.authority.clone(), - package: entry.id.clone(), + authority: source.authority.clone(), + package: source.package.clone(), + environment_id: source.main_prompt.environment_id().map(str::to_owned), } } } diff --git a/codex-rs/ext/skills/tests/skills_extension/merge_catalog_tests.rs b/codex-rs/ext/skills/tests/skills_extension/merge_catalog_tests.rs index 3c1b75a98c..2590e48a2d 100644 --- a/codex-rs/ext/skills/tests/skills_extension/merge_catalog_tests.rs +++ b/codex-rs/ext/skills/tests/skills_extension/merge_catalog_tests.rs @@ -1184,15 +1184,15 @@ async fn v2_rendering_and_tools_retain_their_step_catalog() -> TestResult { async fn v2_implicit_and_explicit_invocations_retain_their_step_catalog() -> TestResult { let installed = installed_skill("matching", "work")?.with_plugin_id(Some("matching".to_string())); - let cloud = cloud_skill("work").hidden_from_prompt(); + // Both copies share a displayed path; the executor package still identifies just one. + let cloud = cloud_skill("work") + .with_display_path(installed.main_prompt.as_str()) + .hidden_from_prompt(); let cloud_provider = provider(vec![cloud.clone()]); - let reads = cloud_provider.read_requests.clone(); - let harness = MergeHarness::new( - provider(vec![installed.clone()]), - cloud_provider, - default_config(), - ) - .await?; + let cloud_reads = cloud_provider.read_requests.clone(); + let executor_provider = provider(vec![installed.clone()]); + let executor_reads = executor_provider.read_requests.clone(); + let harness = MergeHarness::new(executor_provider, cloud_provider, default_config()).await?; harness .turn_store .insert(selected_plugins(&[None, Some("env-1:matching")], &[])); @@ -1229,17 +1229,33 @@ async fn v2_implicit_and_explicit_invocations_retain_their_step_catalog() -> Tes )), ); assert!(detect(&next_step).is_none()); - for (step_store, allowed) in [(&harness.step_store, true), (&next_step, false)] { + let ambiguous = UserInput::Text { + text: format!("[$deploy:work]({})", installed.main_prompt.as_str()), + text_elements: Vec::new(), + }; + let specific = UserInput::Text { + text: format!("[$deploy:work]({}) $deploy:work", installed.id.0), + text_elements: Vec::new(), + }; + // Put the ambiguous link first regardless of the parser's link order. + let mentions = [ambiguous, specific]; + for (step_store, user_input, allowed) in [ + (&harness.step_store, &mentions[..], true), + (&harness.step_store, &mentions[..1], true), + (&next_step, &mentions[1..], false), + ] { let fragments = prepared_turn_input( &harness.registry, TurnInputContext { step_store, turn_id: "turn-1".to_string(), - user_input: &[UserInput::Mention { - name: installed.name.clone(), - path: installed.main_prompt.as_str().to_string(), + user_input, + environments: vec![codex_extension_api::TurnInputEnvironment { + environment_id: "env-1".to_string(), + cwd: workdir.clone(), + is_primary: true, + fs: &FileSystemEnvironmentAccessor::unrestricted(&LOCAL_FS), }], - environments: Vec::new(), }, /*extension_metrics*/ None, &harness.session_store, @@ -1249,18 +1265,19 @@ async fn v2_implicit_and_explicit_invocations_retain_their_step_catalog() -> Tes .await; assert_eq!(fragments.len(), usize::from(allowed)); if allowed { - assert!( - fragments[0] - .render() - .expect_text() - .contains("deploy:work") - ); + let rendered = fragments[0].render().expect_text(); + assert!(rendered.contains("deploy:work")); + assert!(rendered.contains(installed.main_prompt.as_str())); } } assert_eq!( - read_request_keys(&reads), + read_request_keys(&cloud_reads), vec![(cloud.authority, cloud.id, cloud.main_prompt)] ); + assert_eq!( + read_request_keys(&executor_reads), + vec![(installed.authority, installed.id, installed.main_prompt)] + ); Ok(()) }