Skip to content

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

Rust

refactor(admission): adopt ai_memory_core::ActorContext (single identity source)

Djalma Júniorc333d48a71a5
diff --git a/crates/ai-memory-mcp/src/actor.rs b/crates/ai-memory-mcp/src/actor.rs
index a27ba9fd..be9b27b5 100644
--- a/crates/ai-memory-mcp/src/actor.rs
+++ b/crates/ai-memory-mcp/src/actor.rs
@@ -35,7 +35,7 @@
//! the actor is completely anonymous (the chain still runs with a
//! default context — webhooks decide what to do with an empty actor).
-use ai_memory_wiki::ActorContext;
+use ai_memory_core::ActorContext;
use axum::http::HeaderMap;
fn header_str(headers: &HeaderMap, name: &str) -> Option<String> {
@@ -57,6 +57,10 @@ pub fn actor_from_headers(headers: &HeaderMap) -> ActorContext {
sub: header_str(headers, "x-memory-actor-sub"),
client: header_str(headers, "x-memory-actor-client"),
session_id: header_str(headers, "x-memory-actor-session-id"),
+ // `name`/`email` aren't carried by the `X-Memory-Actor-*` bridge;
+ // the four-rung auth middleware fills those when it injects the
+ // canonical `Extension<ActorContext>` directly.
+ ..ActorContext::default()
}
}
diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rs
index 5aed971a..d2c264dd 100644
--- a/crates/ai-memory-mcp/src/server.rs
+++ b/crates/ai-memory-mcp/src/server.rs
@@ -1012,8 +1012,9 @@ impl AiMemoryServer {
// skip list is present (a skip-only write still needs to carry it).
let skip_webhooks = crate::actor::skip_webhooks_from_headers(&parts.headers);
let admission_ctx = if actor.has_any() || !skip_webhooks.is_empty() {
+ // Actor is NOT carried here — `write_page` fills the webhook
+ // context from `req.actor` (single identity source).
Some(ai_memory_wiki::AdmissionContext {
- actor,
op: ai_memory_wiki::AdmissionOp::WritePage,
skip_webhooks,
..ai_memory_wiki::AdmissionContext::default()
@@ -1034,7 +1035,7 @@ impl AiMemoryServer {
title: args.title,
admission_ctx,
author_id: None,
- actor: ai_memory_core::ActorContext::anonymous(),
+ actor,
})
.await
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
diff --git a/crates/ai-memory-wiki/src/admission.rs b/crates/ai-memory-wiki/src/admission.rs
index f13f729b..4bd71fb5 100644
--- a/crates/ai-memory-wiki/src/admission.rs
+++ b/crates/ai-memory-wiki/src/admission.rs
@@ -106,40 +106,6 @@ pub struct WebhookConfig {
pub events: Vec<AdmissionOp>,
}
-/// Identity of the actor that triggered a write. Populated by the caller
-/// (MCP tool, admin endpoint, hook router) from validated JWT claims +
-/// injected headers (typically `X-Memory-Actor-*` from `mcp-auth`).
-///
-/// All fields are optional: internal callers (CLI bootstrap, tests) may
-/// leave them blank; webhooks must handle that gracefully.
-#[derive(Debug, Clone, Default, Serialize, Deserialize)]
-pub struct ActorContext {
- /// `claude-code` | `codex` | `opencode` | `hook` | `cli` | …
- pub agent: Option<String>,
- /// Keycloak `preferred_username` (e.g. `djalmajr`).
- pub user: Option<String>,
- /// JWT `sub` claim (stable user UUID).
- pub sub: Option<String>,
- /// DCR client UUID (identifies the agent install).
- pub client: Option<String>,
- /// Session id from the agent (if known).
- pub session_id: Option<String>,
-}
-
-impl ActorContext {
- /// `true` if at least one identity field is set. Useful for callers
- /// that want to skip building an [`AdmissionContext`] entirely when
- /// the actor is completely anonymous.
- #[must_use]
- pub fn has_any(&self) -> bool {
- self.agent.is_some()
- || self.user.is_some()
- || self.sub.is_some()
- || self.client.is_some()
- || self.session_id.is_some()
- }
-}
-
/// Per-write context passed to each webhook. Cheap to construct
/// (mostly references at the request layer, owned here for serialisation).
#[derive(Debug, Clone, Default, Serialize)]
@@ -156,8 +122,12 @@ pub struct AdmissionContext {
/// (auto-filled from `project_id` when the wiki has a store reader).
#[serde(default)]
pub project: String,
- /// Identity of the actor that triggered the write.
- pub actor: ActorContext,
+ /// Identity of the actor that triggered the write. The canonical
+ /// multi-user [`ai_memory_core::ActorContext`] (since the v0.8 merge);
+ /// `Wiki::write_page` fills it from `WritePageRequest::actor` so the
+ /// webhook payload and the on-disk `last_modified_by` block share one
+ /// identity source.
+ pub actor: ai_memory_core::ActorContext,
/// Which lifecycle op fired the chain.
pub op: AdmissionOp,
/// Names from `X-Memory-Skip-Admission-Chain` (CSV at the request layer);
diff --git a/crates/ai-memory-wiki/src/lib.rs b/crates/ai-memory-wiki/src/lib.rs
index 36285bc9..bdd92dcf 100644
--- a/crates/ai-memory-wiki/src/lib.rs
+++ b/crates/ai-memory-wiki/src/lib.rs
@@ -15,8 +15,8 @@ mod watcher;
mod wiki;
pub use admission::{
- ActorContext, AdmissionChain, AdmissionContext, AdmissionOp, FailurePolicy,
- MAX_ADMISSION_WEBHOOKS, MAX_RESPONSE_BYTES, WebhookConfig,
+ AdmissionChain, AdmissionContext, AdmissionOp, FailurePolicy, MAX_ADMISSION_WEBHOOKS,
+ MAX_RESPONSE_BYTES, WebhookConfig,
};
pub use error::{WikiError, WikiResult};
pub use git::{COMMIT_AUTHOR_EMAIL, COMMIT_AUTHOR_NAME, GitAdapter};
diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rs
index 2b894776..47a7661b 100644
--- a/crates/ai-memory-wiki/src/wiki.rs
+++ b/crates/ai-memory-wiki/src/wiki.rs
@@ -449,6 +449,10 @@ impl Wiki {
// (via the upsert below) atomically. See `crate::admission`.
if let Some(chain) = &self.admission_chain {
let mut ctx = admission_ctx.unwrap_or_default();
+ // Single identity source: the webhook actor is the same
+ // `ActorContext` used for on-disk attribution (req.actor),
+ // populated by the auth layer — not a separate header bridge.
+ ctx.actor = actor.clone();
// Resolve workspace + project names from the store reader (if
// attached) so webhooks address pages by the human-readable
// names the engine uses on disk and in the UI. Best-effort —
diff --git a/crates/ai-memory-wiki/tests/admission.rs b/crates/ai-memory-wiki/tests/admission.rs
index cef86151..8a868ed2 100644
--- a/crates/ai-memory-wiki/tests/admission.rs
+++ b/crates/ai-memory-wiki/tests/admission.rs
@@ -12,10 +12,9 @@ use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
-use ai_memory_core::PagePath;
+use ai_memory_core::{ActorContext, PagePath};
use ai_memory_wiki::{
- ActorContext, AdmissionChain, AdmissionContext, AdmissionOp, FailurePolicy, Markdown,
- WebhookConfig,
+ AdmissionChain, AdmissionContext, AdmissionOp, FailurePolicy, Markdown, WebhookConfig,
};
use axum::http::{HeaderMap, StatusCode};
use axum::response::IntoResponse;
@@ -370,6 +369,7 @@ async fn actor_context_is_propagated_in_payload() {
sub: Some("8f3a-uuid".into()),
client: Some("72836f52-uuid".into()),
session_id: Some("019e6d-session".into()),
+ ..ActorContext::default()
},
..AdmissionContext::default()
};