Files
imeai-aKode/akode-rs/ext/memoria/src/tests.rs
andrea.difonzo a005236bcb aKode: integrazione della memoria di imeAI (plugin memoria)
- nuovo crate ext/memoria: aggiunge le istruzioni del plugin (instructions.md)
  come developer policy solo se il plugin memoria è abilitato e il suo server
  MCP è pronto; nessuna modifica alle istruzioni di base, [features] memories
  resta spento
- pollutes_memory configurabile per server MCP (predefinito true)
- project_header per i server HTTP: progetto ricavato dalla cartella della
  sessione (origin come host/owner/repo, altrimenti la cartella), connessioni
  separate per progetto
- token Bearer dei server MCP anche dal portachiavi di aKode, solo sotto
  mcp:<server> (mai le chiavi dei provider dei modelli)
- tool namespace (MCP, multi-agente) appiattiti in <namespace>__<tool> per i
  server compatibili OpenAI che accettano solo function tool (llama.cpp,
  ik_llama.cpp, vLLM li scartavano); capacità del provider tool_namespaces
- prove: parsing e propagazione, intestazione del progetto, iniezione solo se
  attivo e connesso, appiattimento e mappatura delle chiamate, chiavi dei
  provider mai consegnate ai server MCP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 17:55:21 +01:00

159 lines
5.2 KiB
Rust

use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use akode_config::types::PluginConfig;
use akode_config::types::PluginsConfigToml;
use akode_core_plugins::store::PluginStore;
use akode_extension_api::ContextContributor;
use akode_extension_api::ExtensionData;
use akode_extension_api::PromptSlot;
use akode_protocol::models::ContentItem;
use pretty_assertions::assert_eq;
use super::CONTENT_KIND;
use super::INSTRUCTIONS_FILE;
use super::MemoriaExtension;
use super::MemoriaThreadState;
use super::ReadyFuture;
use super::ServerReadiness;
use super::instructions_path_from;
const INSTRUCTIONS: &str = "## Long-term memory (memoria)\n\nCall `memory_recall` first.";
/// Fake MCP catalog: only the listed servers are ready.
struct ReadyServers(Vec<&'static str>);
impl ServerReadiness for ReadyServers {
fn is_ready<'a>(&'a self, server: &'a str) -> ReadyFuture<'a> {
let ready = self.0.contains(&server);
Box::pin(async move { ready })
}
}
fn plugin_config(enabled: Option<bool>) -> PluginConfig {
serde_json::from_value(serde_json::json!({ "enabled": enabled }))
.expect("plugin config should deserialize")
}
/// Installs `memoria@imeai` in a temporary AKODE_HOME and returns its instructions path.
fn install_plugin(codex_home: &Path, with_instructions: bool) -> std::path::PathBuf {
let root = codex_home.join("plugins/cache/imeai/memoria/0.1.0");
std::fs::create_dir_all(root.join(".codex-plugin")).expect("create plugin root");
let path = root.join(INSTRUCTIONS_FILE);
if with_instructions {
std::fs::write(&path, format!("\n{INSTRUCTIONS}\n")).expect("write instructions");
}
path
}
fn thread_store(
instructions_path: Option<std::path::PathBuf>,
ready: Option<Vec<&'static str>>,
) -> ExtensionData {
let store = ExtensionData::new("thread");
store.insert(MemoriaThreadState {
instructions_path,
servers: ready.map(|ready| Arc::new(ReadyServers(ready)) as Arc<dyn ServerReadiness>),
});
store
}
async fn fragments(thread_store: &ExtensionData) -> Vec<akode_extension_api::PromptFragment> {
MemoriaExtension
.contribute_thread_context(&ExtensionData::new("session"), thread_store)
.await
}
#[test]
fn instructions_path_requires_an_enabled_installed_plugin() {
let codex_home = tempfile::tempdir().expect("tempdir");
let store = PluginStore::new(codex_home.path().to_path_buf());
let policy = PluginsConfigToml::default();
let enabled = HashMap::from([("memoria@imeai".to_string(), plugin_config(Some(true)))]);
// Configured but not installed yet.
assert_eq!(instructions_path_from(&enabled, &policy, &store), None);
let expected = install_plugin(codex_home.path(), /*with_instructions*/ true);
assert_eq!(
instructions_path_from(&enabled, &policy, &store),
Some(expected.clone())
);
// Omitting `enabled` keeps an installed plugin active, like the plugin loader.
let inherited = HashMap::from([("memoria@imeai".to_string(), plugin_config(None))]);
assert_eq!(
instructions_path_from(&inherited, &policy, &store),
Some(expected)
);
let disabled = HashMap::from([("memoria@imeai".to_string(), plugin_config(Some(false)))]);
assert_eq!(instructions_path_from(&disabled, &policy, &store), None);
let other = HashMap::from([("other@imeai".to_string(), plugin_config(Some(true)))]);
assert_eq!(instructions_path_from(&other, &policy, &store), None);
assert_eq!(
instructions_path_from(&HashMap::new(), &policy, &store),
None
);
}
#[tokio::test]
async fn injects_instructions_when_plugin_enabled_and_server_ready() {
let codex_home = tempfile::tempdir().expect("tempdir");
let path = install_plugin(codex_home.path(), /*with_instructions*/ true);
let fragments = fragments(&thread_store(Some(path), Some(vec!["memoria"]))).await;
assert_eq!(fragments.len(), 1);
assert_eq!(fragments[0].slot(), PromptSlot::DeveloperPolicy);
assert_eq!(fragments[0].content_kind().0, CONTENT_KIND);
assert_eq!(
fragments[0].content(),
&ContentItem::InputText {
text: INSTRUCTIONS.to_string()
}
);
}
#[tokio::test]
async fn skips_instructions_when_plugin_disabled() {
assert!(
fragments(&thread_store(
/*instructions_path*/ None,
Some(vec!["memoria"])
))
.await
.is_empty()
);
assert!(fragments(&ExtensionData::new("thread")).await.is_empty());
}
#[tokio::test]
async fn skips_instructions_when_server_not_connected() {
let codex_home = tempfile::tempdir().expect("tempdir");
let path = install_plugin(codex_home.path(), /*with_instructions*/ true);
assert!(
fragments(&thread_store(Some(path.clone()), Some(vec!["other"])))
.await
.is_empty()
);
assert!(
fragments(&thread_store(Some(path), /*ready*/ None))
.await
.is_empty()
);
}
#[tokio::test]
async fn skips_instructions_when_file_missing() {
let codex_home = tempfile::tempdir().expect("tempdir");
let path = install_plugin(codex_home.path(), /*with_instructions*/ false);
assert!(
fragments(&thread_store(Some(path), Some(vec!["memoria"])))
.await
.is_empty()
);
}