Skip to content

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

fix(web-chat): name the fingerprint field that caused a session cache miss

M3gA-Minddecf3de68c9d
diff --git a/crates/openhuman-core/src/web_chat/session.rs b/crates/openhuman-core/src/web_chat/session.rs
index a64814b82c..9571bab8b7 100644
--- a/crates/openhuman-core/src/web_chat/session.rs
+++ b/crates/openhuman-core/src/web_chat/session.rs
@@ -128,6 +128,103 @@ pub(crate) fn locale_reply_directive(locale: &str) -> Option<String> {
))
}
+/// Byte offset of the first difference between two signature strings, or
+/// `None` when one is a prefix of the other (then the length difference is the
+/// whole story).
+fn first_difference_at(prior: &str, next: &str) -> Option<usize> {
+ prior
+ .as_bytes()
+ .iter()
+ .zip(next.as_bytes())
+ .position(|(a, b)| a != b)
+}
+
+/// Summarise a differing signature field without printing it.
+///
+/// `autonomy_signature` and `model_registry_signature` are whole JSON subtrees
+/// of `Config` — `config.autonomy` carries the user's `action_dir` and other
+/// filesystem paths, and the registry is long. Logging either raw would be both
+/// a log bomb on the chat hot path and a needless disclosure, so the log names
+/// the field and gives the reader enough to find the change in their own config:
+/// the two lengths and where the strings first diverge.
+fn describe_signature_change(field: &str, prior: &str, next: &str) -> String {
+ match first_difference_at(prior, next) {
+ Some(offset) => format!(
+ "{field}: differs at byte {offset} (prior {} bytes, now {} bytes)",
+ prior.len(),
+ next.len()
+ ),
+ // No differing byte in the overlap: one is a prefix of the other, i.e.
+ // something was appended to or removed from the end of the subtree.
+ None => format!(
+ "{field}: length changed (prior {} bytes, now {} bytes)",
+ prior.len(),
+ next.len()
+ ),
+ }
+}
+
+/// The fingerprint fields that actually differ between the thread's cached
+/// entry and this turn.
+///
+/// The cache-miss log used to print two hand-picked fields, `target_agent_id`
+/// and `provider_binding`. When those two matched — which is the common case,
+/// since the target agent is hard-coded to `"orchestrator"`
+/// ([`pick_target_agent_id`]) — the log reported a miss while showing nothing
+/// that had missed, so the warning could not be acted on. Four of the six
+/// fields were invisible. This names the ones that differ instead of guessing
+/// which two are interesting.
+///
+/// Returns an empty vector when the fingerprints are equal, which the caller
+/// treats as a bug worth saying so in the log rather than silently omitting:
+/// reaching the miss arm with no differing field would mean `PartialEq` and
+/// this function disagree.
+pub(super) fn fingerprint_diff(
+ prior: &SessionCacheFingerprint,
+ next: &SessionCacheFingerprint,
+) -> Vec<String> {
+ let mut diff = Vec::new();
+ if prior.model_override != next.model_override {
+ diff.push(format!(
+ "model_override: {:?} -> {:?}",
+ prior.model_override, next.model_override
+ ));
+ }
+ if prior.temperature != next.temperature {
+ diff.push(format!(
+ "temperature: {:?} -> {:?}",
+ prior.temperature, next.temperature
+ ));
+ }
+ if prior.target_agent_id != next.target_agent_id {
+ diff.push(format!(
+ "target_agent_id: {} -> {}",
+ prior.target_agent_id, next.target_agent_id
+ ));
+ }
+ if prior.provider_binding != next.provider_binding {
+ diff.push(format!(
+ "provider_binding: {} -> {}",
+ prior.provider_binding, next.provider_binding
+ ));
+ }
+ if prior.autonomy_signature != next.autonomy_signature {
+ diff.push(describe_signature_change(
+ "autonomy_signature",
+ &prior.autonomy_signature,
+ &next.autonomy_signature,
+ ));
+ }
+ if prior.model_registry_signature != next.model_registry_signature {
+ diff.push(describe_signature_change(
+ "model_registry_signature",
+ &prior.model_registry_signature,
+ &next.model_registry_signature,
+ ));
+ }
+ diff
+}
+
pub(super) fn build_session_fingerprint(
config: &Config,
model_override: Option<String>,
@@ -224,16 +321,26 @@ pub(crate) async fn checkout_session_agent(
(entry.agent, entry.fingerprint)
}
Some(prior_entry) => {
+ // Name the field(s) that actually differ. The previous log printed
+ // `target_agent_id` and `provider_binding` only, and both match on
+ // the common path, so a miss was reported with no visible cause
+ // (openhuman#6414).
+ let changed = fingerprint_diff(&prior_entry.fingerprint, &fingerprint);
+ let reason = if changed.is_empty() {
+ // Unreachable via `PartialEq` — reaching the miss arm with no
+ // differing field would mean the derived equality and
+ // `fingerprint_diff` disagree. Say so rather than log nothing.
+ "no field differs (fingerprint_diff disagrees with PartialEq)".to_string()
+ } else {
+ changed.join("; ")
+ };
log::info!(
"[web-channel] cache miss — rebuilding session agent \
- (was id={}, now id={}; prior_provider_binding={}, now={}) \
- for client={} thread={}",
- prior_entry.fingerprint.target_agent_id,
- target_agent_id,
- prior_entry.fingerprint.provider_binding,
- fingerprint.provider_binding,
+ (changed: {}) for client={} thread={} id={}",
+ reason,
client_id,
- thread_id
+ thread_id,
+ target_agent_id
);
(
build_session_agent(
diff --git a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
index e100119720..a0880f48e7 100644
--- a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
+++ b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
@@ -12,12 +12,13 @@ use tinyagents_session::transcript::{write_transcript, TranscriptMeta};
use super::{
checkin_session_agent, checkin_session_agent_if_vacant, checkout_session_agent,
- CheckedOutSession, CheckoutPolicy,
+ fingerprint_diff, CheckedOutSession, CheckoutPolicy,
};
use crate::agent::messages::{ChatMessage, ConversationMessage};
use crate::agent::OpenHumanSessionHost;
use crate::config::Config;
use crate::web_chat::ops::{key_for, THREAD_SESSIONS};
+use crate::web_chat::types::SessionCacheFingerprint;
fn test_config(tmp: &tempfile::TempDir) -> Config {
let config = Config {
@@ -365,3 +366,133 @@ async fn a_thread_binds_one_stable_session_across_cold_boots() {
);
evict(&thread_id).await;
}
+
+/// A fingerprint with every field set, so a test can change exactly one and
+/// know the diff it expects is the only one available.
+fn sample_fingerprint() -> SessionCacheFingerprint {
+ SessionCacheFingerprint {
+ model_override: Some("hint:chat".to_string()),
+ temperature: Some(0.7),
+ target_agent_id: "orchestrator".to_string(),
+ provider_binding: "openhuman".to_string(),
+ autonomy_signature: r#"{"approval_required":true,"action_dir":"/home/u/w"}"#.to_string(),
+ model_registry_signature: r#"[{"id":"m","provider":"p","vision":false}]"#.to_string(),
+ }
+}
+
+#[test]
+fn fingerprint_diff_is_empty_for_equal_fingerprints() {
+ // Guards the miss log's own "no field differs" branch: if this ever became
+ // non-empty for equal inputs, every cache HIT would be reported as a miss
+ // with a fabricated reason.
+ assert!(fingerprint_diff(&sample_fingerprint(), &sample_fingerprint()).is_empty());
+}
+
+#[test]
+fn fingerprint_diff_names_the_field_the_old_log_could_not_show() {
+ // The four fields the previous log never printed. Each is checked on its
+ // own so the assertion cannot pass because some *other* field differed —
+ // the exact way the old two-field log misled readers (openhuman#6414).
+ let base = sample_fingerprint();
+
+ let mut model = base.clone();
+ model.model_override = Some("gpt-4".to_string());
+ let diff = fingerprint_diff(&base, &model);
+ assert_eq!(
+ diff.len(),
+ 1,
+ "expected exactly one differing field: {diff:?}"
+ );
+ assert!(
+ diff[0].starts_with("model_override:"),
+ "diff must name model_override, got {diff:?}"
+ );
+
+ let mut temp = base.clone();
+ temp.temperature = Some(0.1);
+ let diff = fingerprint_diff(&base, &temp);
+ assert_eq!(
+ diff.len(),
+ 1,
+ "expected exactly one differing field: {diff:?}"
+ );
+ assert!(
+ diff[0].starts_with("temperature:"),
+ "diff must name temperature, got {diff:?}"
+ );
+
+ let mut autonomy = base.clone();
+ autonomy.autonomy_signature =
+ r#"{"approval_required":false,"action_dir":"/home/u/w"}"#.to_string();
+ let diff = fingerprint_diff(&base, &autonomy);
+ assert_eq!(
+ diff.len(),
+ 1,
+ "expected exactly one differing field: {diff:?}"
+ );
+ assert!(
+ diff[0].starts_with("autonomy_signature:"),
+ "diff must name autonomy_signature, got {diff:?}"
+ );
+
+ let mut registry = base.clone();
+ registry.model_registry_signature = r#"[{"id":"m","provider":"p","vision":true}]"#.to_string();
+ let diff = fingerprint_diff(&base, &registry);
+ assert_eq!(
+ diff.len(),
+ 1,
+ "expected exactly one differing field: {diff:?}"
+ );
+ assert!(
+ diff[0].starts_with("model_registry_signature:"),
+ "diff must name model_registry_signature, got {diff:?}"
+ );
+}
+
+#[test]
+fn fingerprint_diff_summarises_signatures_without_printing_them() {
+ // `config.autonomy` carries the user's filesystem paths, so the miss log
+ // must not echo the subtree onto the chat hot path. The summary has to be
+ // useful *and* quiet: it names where the two diverge, not what they say.
+ let base = sample_fingerprint();
+ let mut changed = base.clone();
+ changed.autonomy_signature =
+ r#"{"approval_required":false,"action_dir":"/home/u/secret-dir"}"#.to_string();
+
+ let diff = fingerprint_diff(&base, &changed);
+ assert_eq!(diff.len(), 1, "{diff:?}");
+ let line = &diff[0];
+
+ assert!(
+ !line.contains("secret-dir") && !line.contains("action_dir"),
+ "signature contents must not be logged, got {line}"
+ );
+ assert!(
+ line.contains("differs at byte"),
+ "summary must locate the change, got {line}"
+ );
+ assert!(
+ line.contains(&base.autonomy_signature.len().to_string()),
+ "summary must carry the prior length, got {line}"
+ );
+}
+
+#[test]
+fn fingerprint_diff_reports_every_differing_field() {
+ // A miss caused by two fields at once must not report only the first.
+ let base = sample_fingerprint();
+ let mut next = base.clone();
+ next.temperature = None;
+ next.provider_binding = "byok".to_string();
+
+ let diff = fingerprint_diff(&base, &next);
+ assert_eq!(diff.len(), 2, "{diff:?}");
+ assert!(
+ diff.iter().any(|d| d.starts_with("temperature:")),
+ "{diff:?}"
+ );
+ assert!(
+ diff.iter().any(|d| d.starts_with("provider_binding:")),
+ "{diff:?}"
+ );
+}