Skip to content

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

Rust

test(mcp): drive /recap through the production auth stack

Giovanni Coutinhoa840a990b310
diff --git a/crates/ai-memory-mcp/tests/suite/mod.rs b/crates/ai-memory-mcp/tests/suite/mod.rs
index 1c8fb5c4..19e1ecae 100644
--- a/crates/ai-memory-mcp/tests/suite/mod.rs
+++ b/crates/ai-memory-mcp/tests/suite/mod.rs
@@ -36,6 +36,7 @@ mod query_pin_first;
mod query_reasoning;
mod query_superseded;
mod read_page_related;
+mod recap_identity;
mod retrieval_via_tools;
mod slot_identity;
mod stress_autoscope;
diff --git a/crates/ai-memory-mcp/tests/suite/recap_identity.rs b/crates/ai-memory-mcp/tests/suite/recap_identity.rs
new file mode 100644
index 00000000..c84c7001
--- /dev/null
+++ b/crates/ai-memory-mcp/tests/suite/recap_identity.rs
@@ -0,0 +1,297 @@
+//! `GET /recap` through the production auth stack.
+//!
+//! The recap's unit tests inject an `ActorContext` straight into the handler.
+//! These drive the real `require_bearer` middleware with real `aim_` DB-user
+//! tokens, so the identity the recap filters on is the one the middleware
+//! derives from a credential, the same way `/hook` stamps a session's owner.
+//! Every refusal must read exactly like an unknown session id: a 403, or a
+//! body naming the project, would let a prober map session ids to projects.
+
+use ai_memory_core::{ApiCredentialId, NewUser, SessionId, UserId, UserRole};
+use ai_memory_hooks::{HookState, ProjectCacheStore, SubagentSessionSet, hook_router};
+use ai_memory_mcp::auth::{AuthState, require_bearer};
+use ai_memory_store::{AccessMode, GrantLevel, Store, TokenPepper, generate_api_key, hash_token};
+use ai_memory_wiki::Wiki;
+use axum::Router;
+use axum::body::Body;
+use axum::http::{Request, StatusCode};
+use serde_json::json;
+use std::sync::Arc;
+use std::time::Duration;
+use tempfile::TempDir;
+use tower::ServiceExt;
+
+const ROOT_TOKEN: &str = "the-root-token";
+const PROJECT: &str = "recap-shared";
+const NO_SESSION: &str = "no session is recorded under this id for you";
+
+struct Harness {
+ router: Router,
+ store: Store,
+ ingest_semaphore: Arc<tokio::sync::Semaphore>,
+ _tmp: TempDir,
+}
+
+struct DbUser {
+ id: UserId,
+ token: String,
+}
+
+async fn harness() -> Harness {
+ let tmp = TempDir::new().expect("tempdir");
+ let store = Store::open(tmp.path()).expect("store");
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .expect("ws");
+ let baked = store
+ .writer
+ .get_or_create_project(ws, "scratch", None)
+ .await
+ .expect("baked");
+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).expect("wiki");
+ // One permit makes "the hook finished" observable: the handler takes it
+ // before answering 202 and the spawned task releases it when done.
+ let ingest_semaphore = Arc::new(tokio::sync::Semaphore::new(1));
+ let hooks = hook_router(HookState {
+ ingest_metrics: Arc::new(ai_memory_core::IngestMetrics::default()),
+ workspace_id: ws,
+ project_id: baked,
+ writer: store.writer.clone(),
+ reader: store.reader.clone(),
+ wiki,
+ consolidator: None,
+ sanitizer: ai_memory_core::Sanitizer::default(),
+ project_cache: Arc::new(tokio::sync::Mutex::new(ProjectCacheStore::default())),
+ active_project: ai_memory_core::ActiveProject::new(),
+ ingest_semaphore: ingest_semaphore.clone(),
+ ingest_gates: ai_memory_hooks::IngestGates::default(),
+ ingest_drop_list: ai_memory_hooks::IngestDropList::default(),
+ consolidate_on_session_end: false,
+ session_consolidation_notify: None,
+ profile_notify: None,
+ capture_assistant_enabled: false,
+ claim_handoff_on_session_start: false,
+ create_handoff_on_session_end: false,
+ per_user_slots: false,
+ mid_session_routing: ai_memory_core::MidSessionRouting::default(),
+ profile: ai_memory_core::profile::ProfileSettings::default(),
+ subagent_sessions: Arc::new(tokio::sync::Mutex::new(SubagentSessionSet::default())),
+ ingest_rate: Arc::new(tokio::sync::Mutex::new(
+ ai_memory_hooks::IngestRateLimiter::disabled(),
+ )),
+ home_dir: None,
+ trusted_proxy_identity: false,
+ });
+ let auth = AuthState::new(Some(ROOT_TOKEN.to_string())).with_multiuser(
+ TokenPepper::new("test-pepper-recap-identity-1234567890ab"),
+ store.reader.clone(),
+ store.writer.clone(),
+ );
+ let router = hooks.layer(axum::middleware::from_fn_with_state(
+ Arc::new(auth),
+ require_bearer,
+ ));
+ Harness {
+ router,
+ store,
+ ingest_semaphore,
+ _tmp: tmp,
+ }
+}
+
+async fn db_user(h: &Harness, username: &str) -> DbUser {
+ let pepper = TokenPepper::new("test-pepper-recap-identity-1234567890ab");
+ let token = generate_api_key().expect("token");
+ let id = h
+ .store
+ .writer
+ .create_human_user(
+ NewUser {
+ username: username.into(),
+ name: None,
+ email: None,
+ },
+ UserRole::User,
+ None,
+ false,
+ )
+ .await
+ .expect("user");
+ h.store
+ .writer
+ .create_api_credential(
+ ApiCredentialId::new(),
+ id,
+ "test".into(),
+ hash_token(&token, &pepper),
+ None,
+ )
+ .await
+ .expect("credential");
+ DbUser { id, token }
+}
+
+async fn body_text(resp: axum::response::Response) -> String {
+ let bytes = axum::body::to_bytes(resp.into_body(), 1_000_000)
+ .await
+ .expect("body");
+ String::from_utf8(bytes.to_vec()).expect("utf8")
+}
+
+/// POST one hook event as `token` and wait until the server has stored it.
+async fn hook(h: &Harness, token: &str, session: &str, event: &str, prompt: Option<&str>) {
+ let mut body = json!({ "session_id": session, "cwd": format!("/tmp/aim-test/{PROJECT}") });
+ if let Some(prompt) = prompt {
+ body["prompt"] = json!(prompt);
+ }
+ let req = Request::builder()
+ .method("POST")
+ .uri(format!(
+ "/hook?event={event}&agent=claude-code&project={PROJECT}"
+ ))
+ .header("host", "localhost")
+ .header("content-type", "application/json")
+ .header("authorization", format!("Bearer {token}"))
+ .body(Body::from(body.to_string()))
+ .expect("hook req");
+ let resp = h.router.clone().oneshot(req).await.expect("hook");
+ assert_eq!(resp.status(), StatusCode::ACCEPTED, "{event} not accepted");
+ let _ = body_text(resp).await;
+ tokio::time::timeout(Duration::from_secs(5), async {
+ while h.ingest_semaphore.available_permits() != 1 {
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .expect("hook processing finished");
+}
+
+/// A session with one compaction window holding `WINDOW-PROMPT-<tag>`.
+async fn compacted_session(h: &Harness, token: &str, session: &str, tag: &str) {
+ hook(
+ h,
+ token,
+ session,
+ "user-prompt-submit",
+ Some("an older prompt"),
+ )
+ .await;
+ hook(h, token, session, "pre-compact", None).await;
+ let window = format!("WINDOW-PROMPT-{tag}");
+ hook(h, token, session, "user-prompt-submit", Some(&window)).await;
+ hook(h, token, session, "pre-compact", None).await;
+}
+
+async fn recap(h: &Harness, token: &str, session: &str) -> (StatusCode, String) {
+ let req = Request::builder()
+ .uri(format!("/recap?session_id={session}&agent=claude-code"))
+ .header("host", "localhost")
+ .header("authorization", format!("Bearer {token}"))
+ .body(Body::empty())
+ .expect("recap req");
+ let resp = h.router.clone().oneshot(req).await.expect("recap");
+ let status = resp.status();
+ (status, body_text(resp).await)
+}
+
+fn assert_reads_as_unknown(status: StatusCode, body: &str) {
+ assert_eq!(
+ status,
+ StatusCode::OK,
+ "a refusal must not be a distinct status: {body}"
+ );
+ assert!(
+ body.contains(NO_SESSION),
+ "expected the unknown-id notice: {body}"
+ );
+ assert!(
+ !body.contains("WINDOW-PROMPT"),
+ "prompt text leaked: {body}"
+ );
+ assert!(!body.contains(PROJECT), "project name leaked: {body}");
+}
+
+#[tokio::test]
+async fn a_db_user_cannot_read_another_users_session_through_the_middleware() {
+ let h = harness().await;
+ let alice = db_user(&h, "alice").await;
+ let bob = db_user(&h, "bob").await;
+ let session = "11111111-1111-4111-8111-111111111111";
+ compacted_session(&h, &alice.token, session, "ALICE").await;
+
+ let (status, body) = recap(&h, &bob.token, session).await;
+ assert_reads_as_unknown(status, &body);
+
+ // Control: the owner's own token reads the window.
+ let (status, body) = recap(&h, &alice.token, session).await;
+ assert_eq!(status, StatusCode::OK);
+ assert!(body.contains("WINDOW-PROMPT-ALICE"), "{body}");
+}
+
+#[tokio::test]
+async fn an_unattributed_caller_cannot_read_an_owned_session() {
+ let h = harness().await;
+ let alice = db_user(&h, "alice").await;
+ let owned = "22222222-2222-4222-8222-222222222222";
+ compacted_session(&h, &alice.token, owned, "ALICE").await;
+
+ // The root token without `root_username` carries no actor: the same
+ // unattributed identity `/handoff` filters on.
+ let (status, body) = recap(&h, ROOT_TOKEN, owned).await;
+ assert_reads_as_unknown(status, &body);
+
+ // Control: an unowned session written by that caller stays readable to it.
+ let unowned = "33333333-3333-4333-8333-333333333333";
+ compacted_session(&h, ROOT_TOKEN, unowned, "SHARED").await;
+ let (status, body) = recap(&h, ROOT_TOKEN, unowned).await;
+ assert_eq!(status, StatusCode::OK);
+ assert!(body.contains("WINDOW-PROMPT-SHARED"), "{body}");
+}
+
+#[tokio::test]
+async fn a_db_user_without_a_grant_reads_their_own_restricted_session_as_unknown() {
+ let h = harness().await;
+ let alice = db_user(&h, "alice").await;
+ let bob = db_user(&h, "bob").await;
+ // Alice's first hook creates the project, so she is its creator and Bob,
+ // who writes his own session into it while it is open, is not.
+ let first = "55555555-5555-4555-8555-555555555555";
+ hook(
+ &h,
+ &alice.token,
+ first,
+ "user-prompt-submit",
+ Some("alice was here"),
+ )
+ .await;
+ let session = "44444444-4444-4444-8444-444444444444";
+ compacted_session(&h, &bob.token, session, "BOB").await;
+ let (_, project) = h
+ .store
+ .reader
+ .session_project_ids(SessionId::from_native(session))
+ .await
+ .expect("lookup")
+ .expect("session stored");
+ h.store
+ .writer
+ .set_access_mode(project, AccessMode::Restricted)
+ .await
+ .expect("restrict");
+
+ // The owner filter would pass; only the grant check refuses.
+ let (status, body) = recap(&h, &bob.token, session).await;
+ assert_reads_as_unknown(status, &body);
+
+ // Control: a read grant restores it.
+ h.store
+ .writer
+ .grant_memory(bob.id, project, GrantLevel::Read, None)
+ .await
+ .expect("grant");
+ let (status, body) = recap(&h, &bob.token, session).await;
+ assert_eq!(status, StatusCode::OK);
+ assert!(body.contains("WINDOW-PROMPT-BOB"), "{body}");
+}
diff --git a/docs/security-boundaries.md b/docs/security-boundaries.md
index 992223bb..85ef66a5 100644
--- a/docs/security-boundaries.md
+++ b/docs/security-boundaries.md
@@ -47,7 +47,7 @@ means the boundary is not built yet.
| 4f | Finalize-on-exit closes only a session the run provably owns (invariant #16) | `ai-memory-cli/src/commands/run.rs` `finalize_hookless_session` runs only for `harness.lacks_session_end_hook()` harnesses (Command Code, Kiro v2/v3, Antigravity); `own_native_session` returns a session only when it is named, chosen before the spawn, or linked under this run's own id in this checkout — a merely *discovered* native session (possibly a concurrent launch's) is never finalized | `run.rs` `a_session_linked_during_the_run_wins_over_discovery` — `own_native_session` returns `None` for the unlinked and other-checkout (`nested`) cases, so a foreign/concurrent session is refused; the named/linked control is finalized (#944) | STRONG |
| 4g | `memory_handoff_accept` `status` never points a caller at another session's baton | `ai-memory-store/src/reader.rs` `handoff_claimed_by_live_session` — the caller-supplied session id (header or native `_meta`, routing-only per row 6b) selects which claim to check, but the answer is bounded by the resolved `(workspace_id, project_id)`, the authenticated `handoff_owner_sql` owner filter, `state='accepted'`, and a still-open receiving session; `ai-memory-mcp/src/server.rs` `unclaimed_handoff` reports `consumed_by_hook` only on that match (and, for a requested `handoff_id`, only when it is the matched row), `none_pending` otherwise | `handoff_ownership.rs` `a_session_start_claim_is_confirmed_only_to_its_own_live_session` (forged session id under another operator, unattributed reader, foreign project, sibling session and ended session all empty; owner and root controls see the claim); `ai-memory-mcp` `tests/suite/handoff_identity.rs` `consumed_by_hook_is_reported_only_to_the_session_that_claimed_it` (over the production transport: a colleague forwarding the session id, the owner from another session, and a request with no session id all get `none_pending`; the claiming session gets `consumed_by_hook`) | STRONG |
| 4h | Offer-mode SessionStart never consumes or injects handoff-controlled text | `ai-memory-hooks/src/router.rs` `fetch_and_accept_handoff_at` branches on the startup configuration before admission/claim and `render_handoff_notice` emits only typed metadata (`HandoffId`, `AgentKind`, computed age); the normal writer CAS remains the only later claim path | `router.rs` `handoff_notice_is_metadata_only_and_never_carries_the_summary` plants hostile summary/questions/steps/files; `offer_mode_leaves_the_baton_open_and_notice_omits_the_summary` exercises the production hook route, asserts the row remains open, then proves the legitimate exact-id writer claim still consumes it (#1030) | STRONG |
-| 4i | The compaction recap reads one session's own captured text and never writes or claims | `ai-memory-hooks/src/recap.rs` `handle_recap`/`build_recap` — anchored on the session row (`session_project_ids`), then `grants::authorize_resolved(.., ProjectAccess::Read)` on that project and `session_summary_scoped` with the `/handoff` owner filter (`User(key)` or `Unattributed`), so another operator's session answers like an unknown id; open handoffs are read with `list_handoffs` under the same owner filter and never claimed; nothing is written (no claim, active-project publish, observation, or page); stored text is rendered inside the untrusted-history fence with its markers escaped (`escape_untrusted_history_markers`) | `recap.rs` `recap_returns_the_window_and_writes_nothing` (the handoff stays open and the session's row count is unchanged), `another_operator_cannot_read_an_owned_session` (owner control: `owner_reads_own_session`), `db_user_without_grant_on_restricted_project_gets_403` (no prompt text in the body), `recap_for_unknown_session_is_a_notice`, `fence_markers_in_stored_text_are_escaped` | STRONG |
+| 4i | The compaction recap reads one session's own captured text and never writes or claims | `ai-memory-hooks/src/recap.rs` `handle_recap`/`build_recap` — anchored on the session row (`session_project_ids`), then `grants::authorize_resolved(.., ProjectAccess::Read)` on that project, whose refusal answers exactly like an unknown id (`200` with the no-session notice: no `403`, no project name), and `session_summary_scoped` with the `/handoff` owner filter (`User(key)` or `Unattributed`), so another operator's session answers like an unknown id; open handoffs are read with `list_handoffs` under the same owner filter and never claimed; nothing is written (no claim, active-project publish, observation, or page); stored text is rendered inside the untrusted-history fence with its markers escaped (`escape_untrusted_history_markers`) | `recap.rs` `recap_returns_the_window_and_writes_nothing` (the handoff stays open and the session's row count is unchanged), `another_operator_cannot_read_an_owned_session` (owner control: `owner_reads_own_session`), `db_user_without_grant_reads_a_session_as_unknown` (no status, project name or prompt text gives the refusal away; control: a read grant restores it); through the production `require_bearer` with real `aim_` tokens, `ai-memory-mcp/tests/suite/recap_identity.rs` `a_db_user_cannot_read_another_users_session_through_the_middleware` (owner control), `an_unattributed_caller_cannot_read_an_owned_session` (control: an unowned session stays readable) and `a_db_user_without_a_grant_reads_their_own_restricted_session_as_unknown` (control: a read grant), `recap_for_unknown_session_is_a_notice`, `fence_markers_in_stored_text_are_escaped` | STRONG |
| 5a | Pages shared: `author_id` is never a read filter (invariant #16) | `ai-memory-store/src/reader.rs` `search_pages`/`page_body_by_ids` — `author_id` is an attribution JOIN only, never a WHERE term; `observation_stream_votes` lets every session vote whatever its owner (the owner filter only withholds the observation excerpt and ids, never the vote or the page) | `multi_session.rs` — a page with a **non-null** `author_id` (operator A) is readable by operator B in the same project; `multi_session.rs` `another_operators_observations_lift_their_session_page_in_a_shared_project` (operator B's owned session, authored page and observations lift that page for operator A's query); `ai-memory-mcp` `server.rs` `observation_stream_evidence_stays_inside_a_restricted_project` (a second operator with only a read grant gets the session page lifted by the first operator's observation); `multi_session.rs` `observation_excerpts_follow_the_session_owner_while_the_vote_stays_shared` (B still gets A's session page ranked by A's observation, with the page's own snippet) | STRONG |
| 5b | Page supersession (loser stays reachable) | `ai-memory-store/src/ops.rs` `upsert_page_in_tx` — demote `is_latest=0` (never delete) + `supersedes` chain; read side: `reader.rs` `page_versions` lists every row at the path, with no `is_latest` filter, for `/web/w/…/h/*path` and `/api/v1/…/history/*path`. It does apply TTL expiry (`reader.rs` `history_visible`): a version whose own `expires_at` passed is neither listed nor readable by id, a path whose live version expired shows no history at all (the forget sweep hard-deletes every row there), and a `supersedes` link to a hidden version reads as `null`. Expiry is the only thing that hides a superseded version; a concurrent write never does | `multi_session.rs` `concurrent_writes_to_one_path_supersede_rather_than_destroy`; `retrieval_superseded.rs`; `multi_session.rs` `page_history_keeps_the_overwritten_version_and_stays_in_scope`; `ai-memory-web` `history_replay.rs` `history_keeps_both_sides_of_a_concurrent_divergent_write` (two attributed writers on one base: all three versions and both authors listed, one `is_latest`, the two writers' versions diff by id) and `history_lists_k_plus_one_versions_newest_first_and_diffs_the_latest`; bite: an `is_latest = 1` filter in `page_versions` fails both; `history_hides_expired_versions_and_expired_pages` (an expired superseded version and every version of a page whose live version expired answer 404 by path and by id on HTML and JSON, and the live successor's `supersedes` is `null`; control: a TTL in the future is listed and diffable; bites with the per-row predicate removed, with the live-version clause disabled, and with the `supersedes` hiding removed) | STRONG |
| 5c | Watcher reindex and Wiki writes share per-page serialization (invariant #16) | `ai-memory-wiki/src/wiki.rs` `reindex_page` takes `mutation_lock.read` before `reindex_page_locked` takes `lock_page`, covering disk read through the single-writer upsert; callers already holding the exclusive global guard do not reacquire it. `write_page` uses the same page mutex and `apply_batch` retains sorted, deduplicated locks; embedding runs after both guards are released | `wiki.rs` `reindex_page_lock_waits_before_reading_and_allows_another_path` (transient snapshot refused while locked, another path progresses, release indexes the committed bytes without rewriting disk), `reindex_page_lock_serializes_write_through_the_upsert` and `reindex_page_lock_and_reversed_duplicate_batches_do_not_deadlock` (real concurrent operations, final disk/SQL winner and supersession ancestry preserved); all three fail with the reindex page guard removed. `reindex_page_lock_releases_both_guards_before_embedding` checks both body and abstract embedding; `decay_cleanup_reindex_does_not_embed_under_the_exclusive_lock` covers the exclusive caller | STRONG for a shared Wiki handle and its clones. External editors and independently constructed Wiki handles do not participate in these locks |