mod embedding;
pub mod fixtures;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::Arc;
use adk_artifact::{ArtifactService, DeleteRequest};
use adk_core::audit_log::{AuditRecordDraft, AuditRecordKind, AuditRecordOutcome};
use adk_core::{AdkError, Content, ErrorCategory};
use adk_memory::graph::{CreateEntityInput, CreateRelationInput};
use adk_memory::{GraphMemoryService, MemoryEntry, MemoryService};
use adk_rag::{
Chunk, CollectionSpec, Field, Filter, RagError, Scope, SearchResult, VectorQuery, VectorStore,
};
use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use serde_json::json;
pub use embedding::SiglipTextEmbedding;
use crate::audit::VISION_AUDIT_KIND;
use crate::cx::VisionCx;
use crate::digest::Digest;
use crate::error::{Result, VisionError};
use crate::geometry::BBox;
use crate::media::{ArtifactLocator, ImageRef, SourceImage, artifact_uri};
use crate::runtime::{CallOptions, CallRecord, VisionOutput, VisionRuntime};
use crate::traits::EmbeddingSpace;
pub const VISION_MEMORY_INDEXED: &str = "vision.memory.indexed";
pub const VISION_MEMORY_ERASED: &str = "vision.memory.erased";
pub const SPACE_SCOPE: &str = ":adk-vision-space";
const SPACE_RECORD: &str = "space";
pub mod keys {
pub const KIND: &str = "kind";
pub const DIGEST: &str = "digest";
pub const ARTIFACT: &str = "artifact";
pub const MIME_TYPE: &str = "mimeType";
pub const SIZE: &str = "size";
pub const REGION: &str = "region";
pub const SOURCE: &str = "source";
pub const CAPTURED_AT: &str = "capturedAt";
pub const DAY: &str = "day";
pub const RUN: &str = "run";
pub const SPACE: &str = "space";
pub const LABELS: &str = "labels";
pub const CAPTION: &str = "caption";
}
pub const FILTERABLE: [&str; 5] = [keys::KIND, keys::DAY, keys::RUN, keys::SPACE, keys::DIGEST];
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum VisualKind {
Frame,
Page,
Image,
}
impl VisualKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Frame => "frame",
Self::Page => "page",
Self::Image => "image",
}
}
fn parse(value: &str) -> Self {
match value {
"frame" => Self::Frame,
"page" => Self::Page,
_ => Self::Image,
}
}
}
pub fn space_key(space: &EmbeddingSpace) -> String {
let metric = match space.metric {
crate::traits::Metric::Cosine => "cosine",
crate::traits::Metric::Dot => "dot",
crate::traits::Metric::L2 => "l2",
};
format!("{}@{}/{}/{metric}", space.model, space.revision, space.dimensions)
}
#[derive(Clone, Debug)]
pub struct VisualItem {
pub image: ImageRef,
pub pixels: SourceImage,
pub caption: String,
pub source: String,
pub captured_at: DateTime<Utc>,
pub kind: VisualKind,
pub region: Option<BBox>,
pub labels: Vec<String>,
pub run: Option<String>,
}
impl VisualItem {
pub fn new(
image: ImageRef,
pixels: SourceImage,
caption: impl Into<String>,
source: impl Into<String>,
captured_at: DateTime<Utc>,
) -> Self {
Self {
image,
pixels,
caption: caption.into(),
source: source.into(),
captured_at,
kind: VisualKind::Frame,
region: None,
labels: Vec::new(),
run: None,
}
}
pub fn with_kind(mut self, kind: VisualKind) -> Self {
self.kind = kind;
self
}
pub fn with_region(mut self, region: BBox) -> Self {
self.region = Some(region);
self
}
pub fn with_labels<I, S>(mut self, labels: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.labels.extend(labels.into_iter().map(Into::into));
self
}
pub fn with_run(mut self, run: impl Into<String>) -> Self {
self.run = Some(run.into());
self
}
pub fn chunk_id(&self) -> String {
let region = self.region.map(|r| format!("{},{},{},{}", r.x0, r.y0, r.x1, r.y1));
let key = format!("{}\0{}\0{}", self.source, self.image.digest, region.unwrap_or_default());
format!("vm-{}", &Digest::of(key.as_bytes()).hex()[..32])
}
}
#[derive(Clone, Debug, Default)]
pub struct VisualQuery {
pub top_k: usize,
pub days: Vec<NaiveDate>,
pub kind: Option<VisualKind>,
pub source: Option<String>,
pub filter: Option<Filter>,
}
impl VisualQuery {
pub fn top(top_k: usize) -> Self {
Self { top_k, ..Self::default() }
}
pub fn captured_on(mut self, days: impl IntoIterator<Item = NaiveDate>) -> Self {
self.days.extend(days);
self
}
pub fn kind(mut self, kind: VisualKind) -> Self {
self.kind = Some(kind);
self
}
pub fn source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into());
self
}
pub fn filter(mut self, filter: Filter) -> Self {
self.filter = Some(filter);
self
}
fn compile(&self, space: &str) -> Filter {
let mut all = vec![Filter::eq(Field::metadata(keys::SPACE), space)];
if !self.days.is_empty() {
all.push(Filter::is_in(
Field::metadata(keys::DAY),
self.days.iter().map(|d| d.format("%Y-%m-%d").to_string()),
));
}
if let Some(kind) = self.kind {
all.push(Filter::eq(Field::metadata(keys::KIND), kind.as_str()));
}
if let Some(source) = &self.source {
all.push(Filter::eq(Field::DocumentId, source.clone()));
}
if let Some(filter) = &self.filter {
all.push(filter.clone());
}
Filter::and(all)
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VisualHit {
pub id: String,
pub score: f32,
pub caption: String,
pub artifact: ArtifactLocator,
pub artifact_uri: String,
pub digest: Option<Digest>,
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub region: Option<BBox>,
pub source: String,
pub kind: VisualKind,
pub captured_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub run: Option<String>,
pub labels: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub caption_id: Option<String>,
}
impl VisualHit {
pub fn from_result(result: &SearchResult) -> Result<Self> {
let chunk = &result.chunk;
let meta = |key: &str| chunk.metadata.get(key).cloned();
let artifact: ArtifactLocator = meta(keys::ARTIFACT)
.and_then(|json| serde_json::from_str(&json).ok())
.ok_or_else(|| VisionError::Parse {
backend: "visual-memory".into(),
message: format!("chunk `{}` carries no artifact locator", chunk.id),
})?;
Ok(Self {
id: chunk.id.clone(),
score: result.score,
caption: chunk.text.clone(),
artifact_uri: chunk.source_uri.clone().unwrap_or_else(|| artifact_uri(&artifact)),
artifact,
digest: meta(keys::DIGEST).and_then(|d| d.parse().ok()),
mime_type: meta(keys::MIME_TYPE),
region: meta(keys::REGION).and_then(|r| serde_json::from_str(&r).ok()),
source: chunk.document_id.clone(),
kind: meta(keys::KIND).map_or(VisualKind::Image, |k| VisualKind::parse(&k)),
captured_at: meta(keys::CAPTURED_AT)
.and_then(|t| DateTime::parse_from_rfc3339(&t).ok())
.map(|t| t.to_utc()),
run: meta(keys::RUN),
labels: meta(keys::LABELS)
.map(|l| l.split('\n').filter(|s| !s.is_empty()).map(str::to_string).collect())
.unwrap_or_default(),
caption_id: meta(keys::CAPTION),
})
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Erasure {
pub source: String,
pub chunks: u64,
pub captions: u64,
pub entities: usize,
pub artifacts: Vec<ArtifactLocator>,
pub artifacts_kept: Vec<ArtifactLocator>,
}
pub struct VisualMemory {
runtime: Arc<VisionRuntime>,
options: CallOptions,
store: Arc<dyn VectorStore>,
collection: String,
space: EmbeddingSpace,
space_key: String,
captions: Option<Arc<dyn MemoryService>>,
graph: Option<Arc<GraphMemoryService>>,
artifacts: Option<Arc<dyn ArtifactService>>,
}
impl std::fmt::Debug for VisualMemory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VisualMemory")
.field("collection", &self.collection)
.field("space", &self.space)
.field("captions", &self.captions.is_some())
.field("graph", &self.graph.is_some())
.field("artifacts", &self.artifacts.is_some())
.finish_non_exhaustive()
}
}
#[must_use]
pub struct VisualMemoryBuilder {
runtime: Arc<VisionRuntime>,
options: CallOptions,
store: Arc<dyn VectorStore>,
collection: String,
captions: Option<Arc<dyn MemoryService>>,
graph: Option<Arc<GraphMemoryService>>,
artifacts: Option<Arc<dyn ArtifactService>>,
}
impl VisualMemoryBuilder {
pub fn call_options(mut self, options: CallOptions) -> Self {
self.options = options;
self
}
pub fn captions(mut self, memory: Arc<dyn MemoryService>) -> Self {
self.captions = Some(memory);
self
}
pub fn graph(mut self, graph: Arc<GraphMemoryService>) -> Self {
self.graph = Some(graph);
self
}
pub fn artifacts(mut self, artifacts: Arc<dyn ArtifactService>) -> Self {
self.artifacts = Some(artifacts);
self
}
pub async fn open(self) -> Result<VisualMemory> {
let space = self.runtime.embedding_space(&self.options)?;
let memory = VisualMemory {
space_key: space_key(&space),
space,
runtime: self.runtime,
options: self.options,
store: self.store,
collection: self.collection,
captions: self.captions,
graph: self.graph,
artifacts: self.artifacts,
};
let spec = CollectionSpec::new(memory.space.dimensions).with_filterable(FILTERABLE);
memory.store.create_collection(&memory.collection, &spec).await.map_err(rag)?;
let reserved = space_scope();
if memory.store.count(&memory.collection, &reserved, None).await.map_err(rag)? == 0 {
memory
.store
.upsert(&memory.collection, &reserved, &[memory.space_record()?])
.await
.map_err(rag)?;
tracing::info!(
collection = %memory.collection,
space = %memory.space_key,
"visual memory collection created"
);
} else {
memory.check_space().await?;
}
Ok(memory)
}
}
fn space_scope() -> Scope {
Scope::new(SPACE_SCOPE).expect("the reserved scope key is valid")
}
fn rag(err: RagError) -> VisionError {
VisionError::Core(AdkError::from(err))
}
fn kind() -> AuditRecordKind {
AuditRecordKind::Other(VISION_AUDIT_KIND.to_string())
}
impl VisualMemory {
pub fn builder(
runtime: Arc<VisionRuntime>,
store: Arc<dyn VectorStore>,
collection: impl Into<String>,
) -> VisualMemoryBuilder {
VisualMemoryBuilder {
runtime,
options: CallOptions::default(),
store,
collection: collection.into(),
captions: None,
graph: None,
artifacts: None,
}
}
pub fn space(&self) -> &EmbeddingSpace {
&self.space
}
pub fn collection(&self) -> &str {
&self.collection
}
pub fn store(&self) -> &Arc<dyn VectorStore> {
&self.store
}
pub fn text_embedding(&self) -> SiglipTextEmbedding {
SiglipTextEmbedding::from_parts(
Arc::clone(&self.runtime),
self.options.clone(),
self.space.clone(),
)
}
fn axis(&self) -> Vec<f32> {
let mut axis = vec![0.0; self.space.dimensions];
if let Some(first) = axis.first_mut() {
*first = 1.0;
}
axis
}
fn space_record(&self) -> Result<Chunk> {
let text = serde_json::to_string(&self.space).map_err(|e| VisionError::Backend {
backend: "visual-memory".into(),
message: format!("the space record could not be written: {e}"),
})?;
Ok(Chunk::new(SPACE_RECORD, SPACE_RECORD, text)
.with_embedding(self.axis())
.with_metadata(keys::SPACE, self.space_key.clone()))
}
async fn check_space(&self) -> Result<()> {
let reserved = space_scope();
let mine = Filter::eq(Field::metadata(keys::SPACE), self.space_key.clone());
if self.store.count(&self.collection, &reserved, Some(&mine)).await.map_err(rag)? == 1 {
return Ok(());
}
let recorded = match self
.store
.search(&self.collection, &reserved, &VectorQuery::new(&self.axis(), 1))
.await
{
Ok(found) => found
.first()
.and_then(|r| serde_json::from_str::<EmbeddingSpace>(&r.chunk.text).ok())
.map_or_else(
|| "an unreadable space record".to_string(),
|space| space_key(&space),
),
Err(err) => format!(
"a space whose record cannot be read with {} dimensions ({err})",
self.space.dimensions
),
};
Err(VisionError::SpaceMismatch {
collection: self.collection.clone(),
recorded,
offered: self.space_key.clone(),
})
}
async fn check_embedder(&self) -> Result<()> {
let current = self.runtime.embedding_space(&self.options)?;
if current != self.space {
return Err(VisionError::SpaceMismatch {
collection: self.collection.clone(),
recorded: self.space_key.clone(),
offered: space_key(¤t),
});
}
self.check_space().await
}
pub async fn index(
&self,
cx: &VisionCx,
scope: &Scope,
items: &[VisualItem],
) -> Result<VisionOutput<Vec<String>>> {
cx.check()?;
self.check_embedder().await?;
for item in items {
if item.pixels.digest() != item.image.digest {
return Err(VisionError::InvalidInput(format!(
"item `{}` has pixels of {} but names image {}; index the image it names",
item.chunk_id(),
item.pixels.digest(),
item.image.digest
)));
}
}
let (vectors, calls) = self.embed_items(cx, items).await?;
let mut chunks = Vec::with_capacity(items.len());
for (item, vector) in items.iter().zip(vectors) {
chunks.push(self.chunk(item, vector)?);
}
self.store.upsert(&self.collection, scope, &chunks).await.map_err(rag)?;
if let Some(captions) = &self.captions {
for (item, chunk) in items.iter().zip(&chunks) {
let at = &item.image.artifact;
captions.delete_memory(&at.app_name, &at.user_id, &chunk.id).await?;
let entry = MemoryEntry::new(
Content::new("model").with_text(item.caption.clone()),
"vision",
)
.with_id(chunk.id.clone())
.with_timestamp(item.captured_at)
.with_artifact(artifact_uri(at), item.image.mime_type.clone());
captions.add_session(&at.app_name, &at.user_id, &chunk.id, vec![entry]).await?;
}
}
if let Some(graph) = &self.graph {
self.record_graph(graph, items).await?;
}
let ids: Vec<String> = chunks.iter().map(|c| c.id.clone()).collect();
let digests: BTreeSet<String> = items.iter().map(|i| i.image.digest.to_string()).collect();
let sources: BTreeSet<&str> = items.iter().map(|i| i.source.as_str()).collect();
let runs: BTreeSet<&str> = items.iter().filter_map(|i| i.run.as_deref()).collect();
let mut draft = AuditRecordDraft::new(kind(), VISION_MEMORY_INDEXED)
.outcome(AuditRecordOutcome::Succeeded)
.ext("collection", json!(self.collection))
.ext("scope", json!(scope.as_str()))
.ext("space", json!(self.space_key))
.ext("chunks", json!(ids.len()))
.ext("digests", json!(digests))
.ext("sources", json!(sources))
.ext("runs", json!(runs));
if let Some(first) = digests.first() {
draft = draft.payload_ref(first.clone());
}
cx.audit(draft).await?;
tracing::debug!(
collection = %self.collection,
chunks = ids.len(),
"visual memory indexed"
);
Ok(VisionOutput { value: ids, calls })
}
async fn embed_items(
&self,
cx: &VisionCx,
items: &[VisualItem],
) -> Result<(Vec<Vec<f32>>, Vec<CallRecord>)> {
let mut vectors: Vec<Option<Vec<f32>>> = vec![None; items.len()];
let mut calls = Vec::new();
let whole: Vec<usize> = (0..items.len()).filter(|&i| items[i].region.is_none()).collect();
if !whole.is_empty() {
let images: Vec<&SourceImage> = whole.iter().map(|&i| &items[i].pixels).collect();
let out = self.runtime.embed_images(cx, &images, &self.options).await?;
calls.extend(out.calls);
for (&i, v) in whole.iter().zip(out.value) {
vectors[i] = Some(v);
}
}
for (i, item) in items.iter().enumerate() {
if let Some(region) = item.region {
let mut options = self.options.clone();
options.region = Some(region);
let out = self.runtime.embed_images(cx, &[&item.pixels], &options).await?;
calls.extend(out.calls);
vectors[i] = out.value.into_iter().next();
}
}
let vectors = vectors
.into_iter()
.map(|v| {
v.ok_or_else(|| VisionError::Parse {
backend: self.space.model.clone(),
message: "the embedder returned fewer vectors than images".into(),
})
})
.collect::<Result<Vec<_>>>()?;
Ok((vectors, calls))
}
fn chunk(&self, item: &VisualItem, vector: Vec<f32>) -> Result<Chunk> {
let at = &item.image.artifact;
let locator = serde_json::to_string(at).map_err(|e| VisionError::Backend {
backend: "visual-memory".into(),
message: format!("the artifact locator could not be written: {e}"),
})?;
let mut chunk = Chunk::new(item.chunk_id(), item.source.clone(), item.caption.clone())
.with_embedding(vector)
.with_source_uri(artifact_uri(at))
.with_metadata(keys::KIND, item.kind.as_str())
.with_metadata(keys::DIGEST, item.image.digest.to_string())
.with_metadata(keys::ARTIFACT, locator)
.with_metadata(keys::MIME_TYPE, item.image.mime_type.clone())
.with_metadata(keys::SIZE, format!("{}x{}", item.image.width, item.image.height))
.with_metadata(keys::SOURCE, item.source.clone())
.with_metadata(
keys::CAPTURED_AT,
item.captured_at.to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
)
.with_metadata(keys::DAY, item.captured_at.date_naive().format("%Y-%m-%d").to_string())
.with_metadata(keys::SPACE, self.space_key.clone());
if let Some(region) = item.region {
let region = serde_json::to_string(®ion).map_err(|e| VisionError::Backend {
backend: "visual-memory".into(),
message: format!("the region could not be written: {e}"),
})?;
chunk = chunk.with_metadata(keys::REGION, region);
}
if let Some(run) = &item.run {
chunk = chunk.with_metadata(keys::RUN, run.clone());
}
if !item.labels.is_empty() {
chunk = chunk.with_metadata(keys::LABELS, item.labels.join("\n"));
}
if self.captions.is_some() {
chunk = chunk.with_metadata(keys::CAPTION, item.chunk_id());
}
Ok(chunk)
}
async fn record_graph(&self, graph: &GraphMemoryService, items: &[VisualItem]) -> Result<()> {
let mut by_user: BTreeMap<(&str, &str), Vec<&VisualItem>> = BTreeMap::new();
for item in items {
let at = &item.image.artifact;
by_user.entry((at.app_name.as_str(), at.user_id.as_str())).or_default().push(item);
}
for ((app, user), items) in by_user {
let mut sources: BTreeMap<&str, Vec<String>> = BTreeMap::new();
let mut labels: BTreeSet<(&str, &str)> = BTreeSet::new();
for item in &items {
sources.entry(item.source.as_str()).or_default().push(format!(
"{}: {} [{}]",
item.captured_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true),
item.caption,
artifact_uri(&item.image.artifact)
));
for label in &item.labels {
labels.insert((item.source.as_str(), label.as_str()));
}
}
let mut entities: Vec<CreateEntityInput> = sources
.into_iter()
.map(|(source, observations)| CreateEntityInput {
name: source.to_string(),
entity_type: "vision.source".to_string(),
observations,
})
.collect();
let names: BTreeSet<&str> = labels.iter().map(|(_, label)| *label).collect();
entities.extend(names.into_iter().map(|label| CreateEntityInput {
name: label.to_string(),
entity_type: "vision.label".to_string(),
observations: Vec::new(),
}));
graph.create_entities(app, user, entities).await?;
if !labels.is_empty() {
let relations = labels
.into_iter()
.map(|(source, label)| CreateRelationInput {
source: source.to_string(),
relation_type: "observed".to_string(),
target: label.to_string(),
})
.collect();
graph.create_relations(app, user, relations).await?;
}
}
Ok(())
}
async fn search_vector(
&self,
scope: &Scope,
vector: &[f32],
query: &VisualQuery,
) -> Result<Vec<VisualHit>> {
let filter = query.compile(&self.space_key);
let found = self
.store
.search(
&self.collection,
scope,
&VectorQuery::new(vector, query.top_k).with_filter(&filter),
)
.await
.map_err(rag)?;
found.iter().map(VisualHit::from_result).collect()
}
pub async fn search_text(
&self,
cx: &VisionCx,
scope: &Scope,
text: &str,
query: &VisualQuery,
) -> Result<VisionOutput<Vec<VisualHit>>> {
cx.check()?;
self.check_embedder().await?;
let out = self.runtime.embed_texts(cx, &[text], &self.options).await?;
let vector = out.value.into_iter().next().ok_or_else(|| VisionError::Parse {
backend: self.space.model.clone(),
message: "the embedder returned no vector for the query".into(),
})?;
let hits = self.search_vector(scope, &vector, query).await?;
Ok(VisionOutput { value: hits, calls: out.calls })
}
pub async fn search_image(
&self,
cx: &VisionCx,
scope: &Scope,
example: &SourceImage,
query: &VisualQuery,
) -> Result<VisionOutput<Vec<VisualHit>>> {
cx.check()?;
self.check_embedder().await?;
let out = self.runtime.embed_images(cx, &[example], &self.options).await?;
let vector = out.value.into_iter().next().ok_or_else(|| VisionError::Parse {
backend: self.space.model.clone(),
message: "the embedder returned no vector for the example".into(),
})?;
let hits = self.search_vector(scope, &vector, query).await?;
Ok(VisionOutput { value: hits, calls: out.calls })
}
async fn list(&self, scope: &Scope, filter: &Filter) -> Result<Vec<VisualHit>> {
let n = self.store.count(&self.collection, scope, Some(filter)).await.map_err(rag)?;
if n == 0 {
return Ok(Vec::new());
}
let top_k = usize::try_from(n).unwrap_or(usize::MAX);
let axis = self.axis();
let found = self
.store
.search(&self.collection, scope, &VectorQuery::new(&axis, top_k).with_filter(filter))
.await
.map_err(rag)?;
found.iter().map(VisualHit::from_result).collect()
}
pub async fn erase_source(
&self,
cx: &VisionCx,
scope: &Scope,
source: &str,
) -> Result<Erasure> {
if cx.cancellation().is_cancelled() {
return Err(VisionError::Cancelled);
}
let by_source = Filter::eq(Field::DocumentId, source);
let hits = self.list(scope, &by_source).await?;
let chunks =
self.store.delete_where(&self.collection, scope, &by_source).await.map_err(rag)?;
let mut erasure = Erasure { source: source.to_string(), chunks, ..Erasure::default() };
if let Some(captions) = &self.captions {
let ids: BTreeSet<(&str, &str, &str)> = hits
.iter()
.filter_map(|h| {
h.caption_id
.as_deref()
.map(|id| (h.artifact.app_name.as_str(), h.artifact.user_id.as_str(), id))
})
.collect();
for (app, user, id) in ids {
erasure.captions += captions.delete_memory(app, user, id).await?;
}
}
if let Some(graph) = &self.graph {
let users: BTreeSet<(&str, &str)> = hits
.iter()
.map(|h| (h.artifact.app_name.as_str(), h.artifact.user_id.as_str()))
.collect();
for (app, user) in users {
erasure.entities +=
graph.delete_entities(app, user, vec![source.to_string()]).await?.len();
}
}
let mut seen = HashMap::new();
for hit in &hits {
seen.entry(hit.artifact_uri.clone()).or_insert_with(|| hit.artifact.clone());
}
let mut uris: Vec<_> = seen.into_iter().collect();
uris.sort_by(|a, b| a.0.cmp(&b.0));
for (uri, locator) in uris {
let still = Filter::eq(Field::SourceUri, uri);
if self.store.count(&self.collection, scope, Some(&still)).await.map_err(rag)? > 0 {
erasure.artifacts_kept.push(locator);
continue;
}
if let Some(artifacts) = &self.artifacts {
let deleted = artifacts
.delete(DeleteRequest {
app_name: locator.app_name.clone(),
user_id: locator.user_id.clone(),
session_id: locator.session_id.clone(),
file_name: locator.file_name.clone(),
version: None,
})
.await;
match deleted {
Ok(()) => {}
Err(err) if err.category == ErrorCategory::NotFound => {}
Err(err) => return Err(err.into()),
}
erasure.artifacts.push(locator);
}
}
let digests: BTreeSet<String> =
hits.iter().filter_map(|h| h.digest.map(|d| d.to_string())).collect();
cx.audit(
AuditRecordDraft::new(kind(), VISION_MEMORY_ERASED)
.outcome(AuditRecordOutcome::Succeeded)
.payload_ref(source.to_string())
.ext("collection", json!(self.collection))
.ext("scope", json!(scope.as_str()))
.ext("source", json!(source))
.ext("chunks", json!(erasure.chunks))
.ext("captions", json!(erasure.captions))
.ext("entities", json!(erasure.entities))
.ext("digests", json!(digests))
.ext("artifacts", json!(erasure.artifacts))
.ext("artifactsKept", json!(erasure.artifacts_kept)),
)
.await?;
tracing::info!(
collection = %self.collection,
chunks = erasure.chunks,
captions = erasure.captions,
artifacts = erasure.artifacts.len(),
"visual memory source erased"
);
Ok(erasure)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chunk_ids_are_stable_per_source_image_and_region() {
let pixels =
SourceImage::from_pixels(crate::media::DecodedImage::filled(4, 4, [9; 4])).unwrap();
let image = ImageRef {
artifact: ArtifactLocator::new("a", "u", "s", "f.png", 0),
digest: pixels.digest(),
mime_type: "image/png".into(),
width: 4,
height: 4,
provenance: crate::media::Provenance::Upload,
};
let at = DateTime::from_timestamp(1_763_380_800, 0).unwrap();
let item = VisualItem::new(image, pixels, "c", "cam-1", at);
assert_eq!(item.chunk_id(), item.clone().with_run("r2").chunk_id());
assert_ne!(item.chunk_id(), item.clone().with_region(BBox::new(0., 0., 2., 2.)).chunk_id());
let mut other = item.clone();
other.source = "cam-2".into();
assert_ne!(item.chunk_id(), other.chunk_id());
assert!(item.chunk_id().starts_with("vm-") && item.chunk_id().len() == 35);
}
#[test]
fn queries_always_filter_by_space() {
let day = NaiveDate::from_ymd_opt(2026, 11, 17).unwrap();
let filter = VisualQuery::top(5).captured_on([day]).kind(VisualKind::Frame).compile("s");
let mut chunk = Chunk::new("c", "cam-1", "t")
.with_metadata(keys::SPACE, "s")
.with_metadata(keys::DAY, "2026-11-17")
.with_metadata(keys::KIND, "frame");
assert!(filter.matches(&chunk));
chunk.metadata.insert(keys::SPACE.into(), "other".into());
assert!(!filter.matches(&chunk));
assert!(filter.validate().is_ok());
}
}