aKode: cache del prompt più stabile e attese lunghe dei server locali (punto 4, blocco A)

- Ordine fisso dei blocchi di contesto: le sezioni uguali in ogni sessione
  (skill, permessi) vengono prima, quelle delle estensioni (<code_map>,
  riepilogo della memoria) dopo; prima <code_map> stava subito dopo le
  istruzioni di sistema e cambiava da un repository all'altro.
- Strumenti in ordine stabile (per nome, namespace dopo); nella sessione resta
  l'ordine della prima richiesta e i nuovi strumenti vanno in coda, mai in mezzo.
- La prima richiesta aspetta fino a 5 s (prima 1 s) i server MCP in avvio.
- Ogni byte ricevuto azzera il timer di inattività: commenti SSE ed eventi
  imeai.queue/imeai.prefill compresi. Il timeout vale anche per l'attesa delle
  intestazioni ed è di 30 minuti per i server locali (5 per gli altri).
- Niente keepalive TCP nel client HTTP: reqwest sonda dopo 15 s e chiude dopo
  3 sonde senza risposta; attraverso NetBird le sonde non passano e ogni
  risposta partita dopo 60 s veniva tagliata e richiesta da capo.
- Test adattati al nuovo ordine degli strumenti (snapshot con soli riordini) e
  alla nuova attesa dei server MCP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
andrea.difonzo
2026-11-16 17:59:12 +01:00
parent 437c6b7f5a
commit 4e1f551b84
26 changed files with 504 additions and 114 deletions

View File

@@ -2455,13 +2455,13 @@ async fn capture_binding_shares_optional_startup_grace_across_connection_sets()
let first_started = tokio::time::Instant::now();
let first = tokio::time::timeout(
Duration::from_millis(1500),
crate::mcp::DEFAULT_OPTIONAL_MCP_STARTUP_GRACE + Duration::from_millis(500),
capture_binding(&create_connection_set()),
)
.await
.expect("the first thread should receive the optional startup grace");
assert!(first.tools().is_empty());
assert_eq!(first_started.elapsed(), Duration::from_secs(1));
assert_eq!(first_started.elapsed(), crate::mcp::DEFAULT_OPTIONAL_MCP_STARTUP_GRACE);
let second = tokio::time::timeout(
Duration::from_millis(1),
@@ -2491,13 +2491,13 @@ async fn capture_binding_shares_optional_startup_grace_across_connection_sets()
let restored_started = tokio::time::Instant::now();
let restored = tokio::time::timeout(
Duration::from_millis(1500),
crate::mcp::DEFAULT_OPTIONAL_MCP_STARTUP_GRACE + Duration::from_millis(500),
capture_binding(&create_connection_set()),
)
.await
.expect("restoring the startup grace should create a fresh deadline");
assert!(restored.tools().is_empty());
assert_eq!(restored_started.elapsed(), Duration::from_secs(1));
assert_eq!(restored_started.elapsed(), crate::mcp::DEFAULT_OPTIONAL_MCP_STARTUP_GRACE);
let mut updated_config = crate::mcp::tests::test_mcp_config(std::env::temp_dir());
updated_config.optional_mcp_startup_grace = Duration::from_millis(250);
@@ -2553,13 +2553,13 @@ async fn capture_binding_shares_optional_startup_grace_across_connection_sets()
for _ in 0..2 {
let started = tokio::time::Instant::now();
let binding = tokio::time::timeout(
Duration::from_millis(1500),
crate::mcp::DEFAULT_OPTIONAL_MCP_STARTUP_GRACE + Duration::from_millis(500),
capture_binding(&create_connection_set()),
)
.await
.expect("non-cacheable servers should keep their per-thread startup grace");
assert!(binding.tools().is_empty());
assert_eq!(started.elapsed(), Duration::from_secs(1));
assert_eq!(started.elapsed(), crate::mcp::DEFAULT_OPTIONAL_MCP_STARTUP_GRACE);
}
}

View File

@@ -186,8 +186,11 @@ pub struct McpConfig {
pub mcp_server_catalog: ResolvedMcpCatalog,
}
/// Default amount of time a tool catalog capture waits for optional MCP servers.
pub const DEFAULT_OPTIONAL_MCP_STARTUP_GRACE: Duration = Duration::from_secs(1);
/// Default amount of time the first tool catalog capture of a session waits for optional MCP
/// servers. The wait happens once per session: with the complete tool list in the first
/// request, the prompt prefix cached by local servers stays valid (tools that still arrive
/// later are appended at the end of the list).
pub const DEFAULT_OPTIONAL_MCP_STARTUP_GRACE: Duration = Duration::from_secs(5);
impl McpConfig {
/// Resolves enabled runtime servers against the exact attachment permissions being published.