Honor the skill installation named in an explicit mention (#64805)

## Why

When a catalog entry groups multiple installations of a skill, a link to
a specific installation can load instructions from the primary
installation, such as the Cloud copy.

## What changed

Carry the matched installation through to the instruction read and
resource metadata. A path that uniquely identifies an enabled
installation takes precedence over name-only and ambiguous mentions of
the same skill, regardless of mention order. Default mentions still
use the primary installation, and distinct executor environments can
each be selected in the same turn.

## Testing

Extend the merged catalog test to cover a path shared by Cloud and
executor copies and a package link that selects the executor copy.
Update the installation recovery scenario to show that an explicit link
loads the selected environment's instructions into the initial request.

GitOrigin-RevId: f4495c5442b9f4c14d5cdcc4c717717e2119b088
This commit is contained in:
sayan-oai
2026-11-30 20:49:39 +00:00
committed by copyberry
parent b26c32c843
commit 829ddd4442
5 changed files with 143 additions and 101 deletions
+2 -2
View File
@@ -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::<Vec<_>>(),
&ContextSnapshotOptions::default().rewrite_known_segments(),
)
@@ -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::<Vec<_>>(),\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::<Vec<_>>(),\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:
<ENVIRONMENT_CONTEXT>
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:
<skill>
<name>deploy</name>
<path>skill://executor-04/deploy/SKILL.md</path>
# Deployment in env-04
Run scripts/check.py from this installation.
</skill>
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": [
{
+23 -20
View File
@@ -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::<ModelInfo>()
{
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<C> SkillsExtension<C> {
#[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<Arc<HostSkillsSnapshot>>,
mcp_resources: Option<Arc<McpResourceClient>>,
thread_state: &SkillsThreadState,
input: &TurnInputContext<'_>,
) -> Result<SkillReadResult, String> {
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<C> SkillsExtension<C> {
.collect(),
},
SkillSourceKind::Executor => {
let fs = entry
let fs = source
.main_prompt
.environment_path()
.and_then(|(id, _)| {
@@ -522,9 +525,9 @@ impl<C> SkillsExtension<C> {
.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,
},
)
+62 -47
View File
@@ -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<SkillCatalogEntry> {
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<SkillCatalogEntryKey>,
selected: &mut Vec<SkillCatalogEntry>,
choices: &mut Vec<(&'a SkillCatalogEntry, Option<SkillSourceLocation>)>,
) {
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<SkillCatalogEntryKey>,
selected: &mut Vec<SkillCatalogEntry>,
) {
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<String>,
}
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),
}
}
}
@@ -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("<name>deploy:work</name>")
);
let rendered = fragments[0].render().expect_text();
assert!(rendered.contains("<name>deploy:work</name>"));
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(())
}