Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

test(admin): cover the identity-less caller and a saturating window

Samir Hanna Verza9a50a6d6e71d
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 2d03ae81..7d206739 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -950,11 +950,11 @@ async fn handle_sessions_by_agent(
&actor_ext.map_or_else(ai_memory_core::ActorContext::anonymous, |ext| ext.0),
)
};
- match state
+ let counts: ai_memory_store::StoreResult<Vec<ai_memory_store::AgentSessionCount>> = state
.reader
.session_counts_by_agent(ws, proj, owner_filter, since_us)
- .await
- {
+ .await;
+ match counts {
Ok(counts) => (
StatusCode::OK,
Json(
@@ -5491,6 +5491,31 @@ mod tests {
"since_days=0 means the whole history, not an empty window: {json}"
);
+ // A window wider than the epoch saturates into "all history" rather
+ // than overflowing: `u32::MAX` days times a day of microseconds does
+ // not fit in the i64 the cutoff is computed in.
+ let resp = router
+ .clone()
+ .oneshot(
+ Request::builder()
+ .uri(
+ "/admin/sessions/by-agent\
+ ?workspace=default&project=target&since_days=4294967295",
+ )
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+ let body = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
+ let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ assert_eq!(
+ json["by_agent"].as_array().unwrap().len(),
+ 2,
+ "a saturating window counts everything, it does not panic or empty: {json}"
+ );
+
// Unknown scope fails closed with a 404 — never auto-created.
let resp = router
.oneshot(
diff --git a/crates/ai-memory-store/tests/sessions_by_agent.rs b/crates/ai-memory-store/tests/sessions_by_agent.rs
index 5d71da2d..13a95392 100644
--- a/crates/ai-memory-store/tests/sessions_by_agent.rs
+++ b/crates/ai-memory-store/tests/sessions_by_agent.rs
@@ -166,3 +166,43 @@ async fn counts_are_scoped_to_the_asking_operator() {
"the recovery switch reports all operators"
);
}
+
+/// A caller the server cannot name — no bearer identity, no proxy-asserted
+/// user — resolves to `Unattributed` and must see only the shared rows.
+/// This is the shape a single-user install has for every request, so it is
+/// also the path that must keep working when nobody configured multi-user.
+#[tokio::test]
+async fn an_unidentified_caller_sees_only_shared_rows() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let (ws, proj) = scope(&store).await;
+
+ session(
+ &store,
+ ws,
+ proj,
+ AgentKind::ClaudeCode,
+ Some(operator("alice")),
+ )
+ .await;
+ session(&store, ws, proj, AgentKind::Cursor, None).await;
+
+ let anonymous = OwnerFilter::for_actor_context(&ActorContext::anonymous());
+ assert_eq!(
+ anonymous,
+ OwnerFilter::Unattributed,
+ "a caller with no identity must not resolve to a named owner",
+ );
+
+ let counts = store
+ .reader
+ .session_counts_by_agent(ws, proj, anonymous, None)
+ .await
+ .unwrap();
+ let agents: Vec<&str> = counts.iter().map(|c| c.agent.as_str()).collect();
+ assert_eq!(
+ agents,
+ vec!["cursor"],
+ "shared rows only — alice's session is not this caller's to count",
+ );
+}