Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
272 lines
10 KiB
Rust
272 lines
10 KiB
Rust
use std::collections::HashMap;
|
|
use std::collections::HashSet;
|
|
use std::sync::Arc;
|
|
|
|
use akode_exec_server::EnvironmentAccessKey;
|
|
use akode_protocol::protocol::Product;
|
|
use akode_protocol::protocol::SkillScope;
|
|
use akode_skills::LoadedSkillRoot;
|
|
use akode_skills::SkillRootSnapshots;
|
|
use akode_utils_path_uri::PathUri;
|
|
use akode_utils_plugins::PluginSkillRoot;
|
|
use akode_utils_plugins::migrated_command_skills_root;
|
|
use futures::FutureExt;
|
|
use futures::StreamExt;
|
|
use tokio::sync::OnceCell;
|
|
use tokio::sync::Semaphore;
|
|
|
|
use super::HostSkillRoot;
|
|
use super::MAX_CONCURRENT_ROOT_SCANS;
|
|
use super::canonicalize_for_skill_identity;
|
|
use super::host::HostSkillRootSnapshot;
|
|
use super::load_host_skill_root;
|
|
use crate::SkillLoadOutcome;
|
|
use crate::host_service::RequestSkillRootSnapshots;
|
|
use crate::host_service::config_skill_root_cache_key;
|
|
|
|
pub(crate) async fn load_and_merge_host_skill_roots(
|
|
roots: Vec<HostSkillRoot<'_>>,
|
|
root_scan_slots: &Semaphore,
|
|
restriction_product: Option<Product>,
|
|
plugin_skill_snapshots: Option<&SkillRootSnapshots<PluginSkillRoot>>,
|
|
) -> SkillLoadOutcome {
|
|
load_and_merge_host_skill_roots_with_request_snapshots(
|
|
roots,
|
|
root_scan_slots,
|
|
restriction_product,
|
|
plugin_skill_snapshots,
|
|
/*request_root_snapshots*/ None,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub(crate) async fn load_and_merge_host_skill_roots_with_request_snapshots(
|
|
roots: Vec<HostSkillRoot<'_>>,
|
|
root_scan_slots: &Semaphore,
|
|
restriction_product: Option<Product>,
|
|
plugin_skill_snapshots: Option<&SkillRootSnapshots<PluginSkillRoot>>,
|
|
request_root_snapshots: Option<&RequestSkillRootSnapshots>,
|
|
) -> SkillLoadOutcome {
|
|
let migrated_command_roots_by_plugin = roots
|
|
.iter()
|
|
.filter_map(HostSkillRoot::plugin_root)
|
|
.map(|plugin_root| (plugin_root.clone(), Arc::new(OnceCell::new())))
|
|
.collect::<HashMap<_, _>>();
|
|
let migrated_command_roots = &migrated_command_roots_by_plugin;
|
|
let mut loads = Vec::with_capacity(roots.len());
|
|
for (root_index, root) in roots.into_iter().enumerate() {
|
|
loads.push(async move {
|
|
let plugin_root = root.plugin_root().cloned();
|
|
let migrated_command_root = plugin_root
|
|
.as_ref()
|
|
.and_then(|plugin_root| migrated_command_roots.get(plugin_root))
|
|
.cloned();
|
|
let plugin_skill_root = root.plugin_skill_root();
|
|
let file_system = root.file_system;
|
|
let request_snapshot = if plugin_skill_root.is_none() {
|
|
request_root_snapshots.map(|snapshots| {
|
|
Arc::clone(
|
|
snapshots
|
|
.write()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
.entry(config_skill_root_cache_key(&root))
|
|
.or_insert_with(|| Arc::new(OnceCell::new())),
|
|
)
|
|
})
|
|
} else {
|
|
None
|
|
};
|
|
let load = async {
|
|
let _root_scan_slot = root_scan_slots
|
|
.acquire()
|
|
.await
|
|
.unwrap_or_else(|_| unreachable!());
|
|
let cached_snapshot = plugin_skill_root
|
|
.as_ref()
|
|
.and_then(|plugin_skill_root| plugin_skill_snapshots?.get(plugin_skill_root));
|
|
match cached_snapshot {
|
|
Some(snapshot) => HostSkillRootSnapshot {
|
|
root: snapshot.root,
|
|
skills: snapshot.skills,
|
|
skill_discovery_path_by_path: snapshot.skill_discovery_path_by_path,
|
|
errors: snapshot.errors,
|
|
file_system: file_system.cache_key(),
|
|
is_agent_plugin: snapshot.is_agent_plugin,
|
|
},
|
|
None => {
|
|
let snapshot = load_host_skill_root(root).await;
|
|
if let Some(plugin_skill_snapshots) = plugin_skill_snapshots
|
|
&& let Some(plugin_skill_root) = plugin_skill_root
|
|
{
|
|
plugin_skill_snapshots.insert(
|
|
plugin_skill_root,
|
|
LoadedSkillRoot {
|
|
root: snapshot.root.clone(),
|
|
skills: snapshot.skills.clone(),
|
|
skill_discovery_path_by_path: Arc::clone(
|
|
&snapshot.skill_discovery_path_by_path,
|
|
),
|
|
errors: snapshot.errors.clone(),
|
|
is_agent_plugin: snapshot.is_agent_plugin,
|
|
},
|
|
);
|
|
}
|
|
snapshot
|
|
}
|
|
}
|
|
}
|
|
.boxed();
|
|
let snapshot = if let Some(snapshot) = request_snapshot {
|
|
snapshot.get_or_init(|| load).await.clone()
|
|
} else {
|
|
load.await
|
|
};
|
|
let migrated_command_root = match (plugin_root.as_ref(), migrated_command_root) {
|
|
(Some(plugin_root), Some(migrated_command_root)) => Some(
|
|
migrated_command_root
|
|
.get_or_init(|| async {
|
|
canonicalize_for_skill_identity(
|
|
file_system,
|
|
&migrated_command_skills_root(plugin_root),
|
|
)
|
|
.await
|
|
})
|
|
.await
|
|
.clone(),
|
|
),
|
|
_ => None,
|
|
};
|
|
(root_index, migrated_command_root, snapshot)
|
|
});
|
|
}
|
|
let mut indexed_snapshots = futures::stream::iter(loads)
|
|
.buffer_unordered(MAX_CONCURRENT_ROOT_SCANS)
|
|
.collect::<Vec<_>>()
|
|
.await;
|
|
indexed_snapshots.sort_unstable_by_key(|(root_index, _, _)| *root_index);
|
|
|
|
for (_, _, snapshot) in &mut indexed_snapshots {
|
|
snapshot
|
|
.skills
|
|
.retain(|skill| skill.matches_product_restriction_for_product(restriction_product));
|
|
}
|
|
|
|
let mut native_plugin_skill_names = HashMap::<String, HashSet<String>>::new();
|
|
for skill in indexed_snapshots
|
|
.iter()
|
|
.flat_map(|(_, migrated_command_root, snapshot)| {
|
|
snapshot.skills.iter().filter(|skill| {
|
|
!migrated_command_root.as_ref().is_some_and(|migrated_root| {
|
|
skill
|
|
.path_to_skills_md
|
|
.starts_with(&PathUri::from_abs_path(migrated_root))
|
|
})
|
|
})
|
|
})
|
|
{
|
|
if let Some(plugin_id) = &skill.plugin_id {
|
|
native_plugin_skill_names
|
|
.entry(plugin_id.clone())
|
|
.or_default()
|
|
.insert(skill.name.clone());
|
|
}
|
|
}
|
|
for (_, migrated_command_root, snapshot) in &mut indexed_snapshots {
|
|
let Some(migrated_command_root) = migrated_command_root else {
|
|
continue;
|
|
};
|
|
snapshot.skills.retain(|skill| {
|
|
!skill
|
|
.path_to_skills_md
|
|
.starts_with(&PathUri::from_abs_path(migrated_command_root))
|
|
|| skill.plugin_id.as_ref().is_none_or(|plugin_id| {
|
|
native_plugin_skill_names
|
|
.get(plugin_id)
|
|
.is_none_or(|names| !names.contains(&skill.name))
|
|
})
|
|
});
|
|
}
|
|
|
|
merge_host_skill_root_snapshots(
|
|
indexed_snapshots
|
|
.into_iter()
|
|
.map(|(_, _, snapshot)| snapshot)
|
|
.collect(),
|
|
)
|
|
}
|
|
|
|
fn merge_host_skill_root_snapshots(snapshots: Vec<HostSkillRootSnapshot>) -> SkillLoadOutcome {
|
|
let mut skills = Vec::new();
|
|
let mut errors = Vec::new();
|
|
let mut skill_roots = Vec::new();
|
|
let mut skill_root_by_path = HashMap::new();
|
|
let mut skill_discovery_path_by_path = HashMap::new();
|
|
let mut agent_plugin_skill_paths = HashSet::new();
|
|
let mut file_systems_by_skill_path = HashMap::<PathUri, EnvironmentAccessKey>::new();
|
|
|
|
for snapshot in snapshots {
|
|
if !snapshot.skills.is_empty() && !skill_roots.contains(&snapshot.root) {
|
|
skill_roots.push(snapshot.root.clone());
|
|
}
|
|
for skill in &snapshot.skills {
|
|
let path = skill.path_to_skills_md.clone();
|
|
if !skill_root_by_path.contains_key(&path) {
|
|
skill_root_by_path.insert(path.clone(), snapshot.root.clone());
|
|
if let Some(discovery_path) = snapshot.skill_discovery_path_by_path.get(&path) {
|
|
skill_discovery_path_by_path.insert(path.clone(), discovery_path.clone());
|
|
}
|
|
file_systems_by_skill_path.insert(path.clone(), snapshot.file_system.clone());
|
|
if snapshot.is_agent_plugin {
|
|
agent_plugin_skill_paths.insert(path);
|
|
}
|
|
}
|
|
}
|
|
skills.extend(snapshot.skills);
|
|
errors.extend(snapshot.errors);
|
|
}
|
|
|
|
let mut seen_paths = HashSet::new();
|
|
skills.retain(|skill| seen_paths.insert(skill.path_to_skills_md.clone()));
|
|
let retained_paths = skills
|
|
.iter()
|
|
.map(|skill| skill.path_to_skills_md.clone())
|
|
.collect::<HashSet<_>>();
|
|
skill_root_by_path.retain(|path, _| retained_paths.contains(path));
|
|
skill_discovery_path_by_path.retain(|path, _| retained_paths.contains(path));
|
|
let retained_roots = skill_root_by_path.values().collect::<HashSet<_>>();
|
|
skill_roots.retain(|root| retained_roots.contains(root));
|
|
file_systems_by_skill_path.retain(|path, _| retained_paths.contains(path));
|
|
agent_plugin_skill_paths.retain(|path| retained_paths.contains(path));
|
|
// Keep native path ordering: URI escaping can reorder same-name skills.
|
|
skills.sort_by_cached_key(|skill| {
|
|
(
|
|
scope_rank(skill.scope),
|
|
skill.name.clone(),
|
|
skill.path_to_skills_md.to_abs_path().ok(),
|
|
)
|
|
});
|
|
|
|
SkillLoadOutcome::from_parts(
|
|
skills,
|
|
errors,
|
|
skill_roots,
|
|
skill_root_by_path,
|
|
skill_discovery_path_by_path,
|
|
agent_plugin_skill_paths,
|
|
file_systems_by_skill_path,
|
|
)
|
|
}
|
|
|
|
fn scope_rank(scope: SkillScope) -> u8 {
|
|
match scope {
|
|
SkillScope::Repo => 0,
|
|
SkillScope::User => 1,
|
|
SkillScope::System => 2,
|
|
SkillScope::Admin => 3,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "host_merge_tests.rs"]
|
|
mod tests;
|