Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(memory): scope prefetch cache key to memory config

Steven Enamakelc24f5988a4c8
diff --git a/crates/openhuman-core/src/memory/lifecycle/prefetch.rs b/crates/openhuman-core/src/memory/lifecycle/prefetch.rs
index c56e32a717..3e56c2fdcb 100644
--- a/crates/openhuman-core/src/memory/lifecycle/prefetch.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/prefetch.rs
@@ -55,24 +55,46 @@ struct Key {
}
impl Key {
- fn new(config: &Config, identity: &ResolvedIdentity, thread: &str, compaction: bool) -> Self {
- let mut fingerprint = std::collections::hash_map::DefaultHasher::new();
- // Keep only the digest, never serialized settings or credentials.
- serde_json::to_value(config)
- .ok()
- .and_then(|value| serde_json::to_vec(&value).ok())
- .unwrap_or_default()
- .hash(&mut fingerprint);
- config.config_path.hash(&mut fingerprint);
- config.action_dir.hash(&mut fingerprint);
+ /// `config_fingerprint` is supplied by the host so the cache key can
+ /// later consume the host's config generation directly (e.g. TurnCache).
+ fn with_fingerprint(
+ config: &Config,
+ config_fingerprint: u64,
+ identity: &ResolvedIdentity,
+ thread: &str,
+ compaction: bool,
+ ) -> Self {
Self {
workspace: config.workspace_dir.clone(),
- config: fingerprint.finish(),
+ config: config_fingerprint,
identity: identity.clone(),
thread: thread.to_owned(),
compaction,
}
}
+
+ #[cfg(test)]
+ fn new(config: &Config, identity: &ResolvedIdentity, thread: &str, compaction: bool) -> Self {
+ Self::with_fingerprint(
+ config,
+ memory_config_fingerprint(config),
+ identity,
+ thread,
+ compaction,
+ )
+ }
+}
+
+/// Fingerprints only memory settings that affect recall and its sources. The
+/// full Config is large and unrelated settings must not invalidate turn packs.
+fn memory_config_fingerprint(config: &Config) -> u64 {
+ let mut fingerprint = std::collections::hash_map::DefaultHasher::new();
+ // Keep only the digest, never serialized settings or credentials.
+ serde_json::to_vec(&config.memory)
+ .unwrap_or_default()
+ .hash(&mut fingerprint);
+ config.config_path.hash(&mut fingerprint);
+ fingerprint.finish()
}
struct Cache(Prefetcher<Key, TurnPack>);
@@ -154,7 +176,13 @@ pub(crate) fn pre_turn(
return None;
}
let cache = cache(ctx);
- let key = Key::new(&config, &identity, &input.thread_id, false);
+ let key = Key::with_fingerprint(
+ &config,
+ memory_config_fingerprint(&config),
+ &identity,
+ &input.thread_id,
+ false,
+ );
let pack = identity.recall.then(|| completed(&cache, &key)).flatten();
tracing::debug!(thread_id = %key.thread, agent_id = %key.identity.agent_id, cache_hit = pack.is_some(), "[memory:prefetch] pre_turn cache");
if let Some(pack) = &pack {
@@ -210,9 +238,12 @@ pub(crate) fn compaction(
return None;
}
let cache = cache(ctx);
- let key = Key::new(&config, &identity, &thread, true);
+ let fingerprint = memory_config_fingerprint(&config);
+ let key = Key::with_fingerprint(&config, fingerprint, &identity, &thread, true);
let pack = completed(&cache, &key)
- .or_else(|| completed(&cache, &Key::new(&config, &identity, &thread, false)));
+ .or_else(|| {
+ completed(&cache, &Key::with_fingerprint(&config, fingerprint, &identity, &thread, false))
+ });
enqueue(
cache,
key,
@@ -232,7 +263,13 @@ pub(crate) async fn settle_for_test(
compaction: bool,
) {
let cache = cache(ctx);
- let key = Key::new(config, identity, thread, compaction);
+ let key = Key::with_fingerprint(
+ config,
+ memory_config_fingerprint(config),
+ identity,
+ thread,
+ compaction,
+ );
tokio::time::timeout(Duration::from_secs(2), async {
while !cache.0.is_idle(&key) {
tokio::task::yield_now().await;