use anyhow::{Context, Result};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use surrealdb::RecordId as SurrealRecordId;
use surrealdb::sql::{Thing, Value};
use crate::knowledge::KnowledgeEntry;
use crate::types::{
Agent, ApplicabilityType, Category, ContentType, EntryType, Project, Relationship,
RelationshipType, Session, SessionType, SourceType,
};
macro_rules! with_db {
($self:expr, $db:ident, $body:expr) => {
match &$self.conn {
SurrealConnection::Embedded($db) => $body,
SurrealConnection::Network($db) => $body,
}
};
}
mod connection;
mod knowledge;
mod trait_impl;
pub use connection::SurrealConnection;
use connection::normalize_datetime;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tag {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<String>,
}
#[derive(Debug, Clone)]
pub(crate) struct RecordId(Thing);
impl RecordId {
fn new(table: &str, id: &str) -> Self {
Self(Thing::from((table, id)))
}
fn as_thing(&self) -> &Thing {
&self.0
}
fn into_thing(self) -> Thing {
self.0
}
fn to_record_id(&self) -> SurrealRecordId {
SurrealRecordId::from((self.0.tb.as_str(), self.0.id.to_string().as_str()))
}
}
pub struct SurrealDatabase {
conn: SurrealConnection,
}
#[derive(Debug, Deserialize)]
struct ExistsRow {
id: String,
}
impl SurrealDatabase {
pub fn backup_content_internal(
&self,
entry: &KnowledgeEntry,
operation: &str,
agent: Option<&str>,
) -> Result<String> {
Self::runtime().block_on(self.backup_content_async(entry, operation, agent))
}
async fn backup_content_async(
&self,
entry: &KnowledgeEntry,
operation: &str,
agent: Option<&str>,
) -> Result<String> {
let entry_id = entry.id.clone();
let content_hash = entry.content_hash.clone().unwrap_or_default();
let backup_id = format!(
"{}_{}",
entry_id.replace("kn-", ""),
Utc::now().format("%Y%m%dT%H%M%S%.3f")
);
let _response = with_db!(self, db, {
db.query(
"CREATE type::thing('memory_backup', $backup_id) SET
entry_id = $entry_id,
title = $title,
body = $body,
content_hash = $content_hash,
operation = $operation,
source_agent = $source_agent,
created_at = time::now()
",
)
.bind(("backup_id", backup_id.clone()))
.bind(("entry_id", entry_id.clone()))
.bind(("title", entry.title.clone()))
.bind(("body", entry.body.clone()))
.bind(("content_hash", content_hash))
.bind(("operation", operation.to_string()))
.bind(("source_agent", agent.map(|s| s.to_string())))
.await
.context("Failed to create memory backup")
})?;
let _ = self.purge_backups_async(&entry_id, 10).await;
Ok(backup_id)
}
pub fn list_backups_internal(&self, entry_id: &str) -> Result<Vec<crate::types::MemoryBackup>> {
Self::runtime().block_on(self.list_backups_async(entry_id))
}
async fn list_backups_async(&self, entry_id: &str) -> Result<Vec<crate::types::MemoryBackup>> {
let mut response = with_db!(self, db, {
db.query(
"SELECT meta::id(id) AS id, entry_id, title, body, content_hash,
operation, source_agent, created_at
FROM memory_backup
WHERE entry_id = $entry_id
ORDER BY created_at DESC",
)
.bind(("entry_id", entry_id.to_string()))
.await
.context("Failed to list memory backups")
})?;
let backups: Vec<crate::types::MemoryBackup> = response.take(0)?;
Ok(backups)
}
pub fn latest_backup_internal(
&self,
entry_id: &str,
) -> Result<Option<crate::types::MemoryBackup>> {
Self::runtime().block_on(self.latest_backup_async(entry_id))
}
async fn latest_backup_async(
&self,
entry_id: &str,
) -> Result<Option<crate::types::MemoryBackup>> {
let mut response = with_db!(self, db, {
db.query(
"SELECT meta::id(id) AS id, entry_id, title, body, content_hash,
operation, source_agent, created_at
FROM memory_backup
WHERE entry_id = $entry_id
ORDER BY created_at DESC
LIMIT 1",
)
.bind(("entry_id", entry_id.to_string()))
.await
.context("Failed to get latest backup")
})?;
let backups: Vec<crate::types::MemoryBackup> = response.take(0)?;
Ok(backups.into_iter().next())
}
pub fn purge_backups_internal(&self, entry_id: &str, keep: usize) -> Result<()> {
Self::runtime().block_on(self.purge_backups_async(entry_id, keep))
}
async fn purge_backups_async(&self, entry_id: &str, keep: usize) -> Result<()> {
let _response = with_db!(self, db, {
db.query(
"DELETE FROM memory_backup
WHERE entry_id = $entry_id
AND id NOT IN (
SELECT VALUE id FROM memory_backup
WHERE entry_id = $entry_id
ORDER BY created_at DESC
LIMIT $keep
)",
)
.bind(("entry_id", entry_id.to_string()))
.bind(("keep", keep as i64))
.await
.context("Failed to purge old backups")
})?;
Ok(())
}
pub fn wake_cascade(
&self,
ctx: &crate::store::AgentContext,
limit: usize,
min_resonance: Option<i32>,
days: i64,
) -> Result<crate::store::WakeCascade> {
Self::runtime().block_on(self.wake_cascade_async(ctx, limit, min_resonance, days))
}
async fn wake_cascade_async(
&self,
ctx: &crate::store::AgentContext,
limit: usize,
min_resonance: Option<i32>,
days: i64,
) -> Result<crate::store::WakeCascade> {
if let Some(threshold) = min_resonance {
let blooms = self.query_blooms_by_resonance(ctx, threshold).await?;
return Ok(crate::store::WakeCascade {
core: blooms,
recent: Vec::new(),
bridges: Vec::new(),
});
}
let core = self.query_core_blooms(ctx, limit).await?;
let remaining = limit.saturating_sub(core.len());
let core_ids: std::collections::HashSet<String> =
core.iter().map(|e| e.id.clone()).collect();
let all_recent = self.query_recent_blooms(ctx, remaining * 2, days).await?;
let recent: Vec<_> = all_recent
.into_iter()
.filter(|e| !core_ids.contains(&e.id))
.take(remaining)
.collect();
let remaining = remaining.saturating_sub(recent.len());
let mut anchor_ids: Vec<String> = core
.iter()
.chain(recent.iter())
.map(|e| e.id.clone())
.collect();
anchor_ids.sort();
anchor_ids.dedup();
let bridges = if anchor_ids.is_empty() || remaining == 0 {
Vec::new()
} else {
let mut existing_ids = core_ids;
existing_ids.extend(recent.iter().map(|e| e.id.clone()));
let all_bridges = self
.query_bridge_blooms(ctx, remaining * 2, &anchor_ids)
.await?;
all_bridges
.into_iter()
.filter(|e| !existing_ids.contains(&e.id))
.take(remaining)
.collect()
};
Ok(crate::store::WakeCascade {
core,
recent,
bridges,
})
}
async fn query_blooms_by_resonance(
&self,
ctx: &crate::store::AgentContext,
threshold: i32,
) -> Result<Vec<crate::knowledge::KnowledgeEntry>> {
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let sql = format!(
"SELECT {}
FROM knowledge
WHERE resonance >= $threshold
AND (resonance_type IS NONE OR resonance_type != 'ephemeral')
{}
ORDER BY resonance DESC",
Self::knowledge_select_fields(),
visibility_clause
);
let mut response = with_db!(self, db, {
let mut query = db.query(&sql).bind(("threshold", threshold));
if let Some(agent) = current_agent {
query = query.bind(("current_agent", agent));
}
query.await.context("Failed to query blooms by resonance")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
async fn query_core_blooms(
&self,
ctx: &crate::store::AgentContext,
limit: usize,
) -> Result<Vec<crate::knowledge::KnowledgeEntry>> {
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let sql = format!(
"SELECT *,
(wake_order IS NOT NULL) AS has_wake_order,
wake_order ?? 999999 AS effective_wake_order
FROM (
SELECT {}
FROM knowledge
WHERE resonance >= 8
AND (resonance_type IS NONE OR resonance_type != 'ephemeral')
{}
)
ORDER BY
has_wake_order DESC,
effective_wake_order ASC,
resonance DESC
LIMIT $limit",
Self::knowledge_select_fields(),
visibility_clause
);
let mut response = with_db!(self, db, {
let mut query = db.query(&sql).bind(("limit", limit as i64));
if let Some(agent) = current_agent {
query = query.bind(("current_agent", agent));
}
query.await.context("Failed to query core blooms")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
async fn query_recent_blooms(
&self,
ctx: &crate::store::AgentContext,
limit: usize,
days: i64,
) -> Result<Vec<crate::knowledge::KnowledgeEntry>> {
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let cutoff = chrono::Utc::now() - chrono::Duration::days(days);
let cutoff_str = cutoff.to_rfc3339();
let sql = format!(
"SELECT *,
(wake_order IS NOT NULL) AS has_wake_order,
wake_order ?? 999999 AS effective_wake_order
FROM (
SELECT {}
FROM knowledge
WHERE last_activated > <datetime>$cutoff
AND (resonance_type IS NONE OR resonance_type != 'ephemeral')
{}
)
ORDER BY
has_wake_order DESC,
effective_wake_order ASC,
resonance DESC
LIMIT $limit",
Self::knowledge_select_fields(),
visibility_clause
);
let mut response = with_db!(self, db, {
let mut query = db
.query(&sql)
.bind(("cutoff", cutoff_str))
.bind(("limit", limit as i64));
if let Some(agent) = current_agent {
query = query.bind(("current_agent", agent));
}
query.await.context("Failed to query recent blooms")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
async fn query_bridge_blooms(
&self,
ctx: &crate::store::AgentContext,
limit: usize,
anchor_ids: &[String],
) -> Result<Vec<crate::knowledge::KnowledgeEntry>> {
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let sql = format!(
"SELECT *,
(wake_order IS NOT NULL) AS has_wake_order,
wake_order ?? 999999 AS effective_wake_order
FROM (
SELECT {}
FROM knowledge
WHERE array::len(array::intersect(anchors, $anchor_ids)) > 0
AND resonance >= 5
{}
)
ORDER BY
has_wake_order DESC,
effective_wake_order ASC,
resonance DESC
LIMIT $limit",
Self::knowledge_select_fields(),
visibility_clause
);
let mut response = with_db!(self, db, {
let mut query = db
.query(&sql)
.bind(("anchor_ids", anchor_ids.to_vec()))
.bind(("limit", limit as i64));
if let Some(agent) = current_agent {
query = query.bind(("current_agent", agent));
}
query.await.context("Failed to query bridge blooms")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
pub fn update_activations(&self, ids: &[String]) -> Result<()> {
Self::runtime().block_on(self.update_activations_async(ids))
}
async fn update_activations_async(&self, ids: &[String]) -> Result<()> {
if ids.is_empty() {
return Ok(());
}
let clean_ids: Vec<String> = ids
.iter()
.map(|id| id.strip_prefix("kn-").unwrap_or(id).to_string())
.collect();
let things: Vec<Thing> = clean_ids
.iter()
.map(|id| Thing::from(("knowledge", id.as_str())))
.collect();
let mut response = with_db!(self, db, {
db.query(
"UPDATE knowledge SET
activation_count += 1,
last_activated = time::now()
WHERE id IN $ids",
)
.bind(("ids", things))
.await
.context("Failed to update activations")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!(
"Failed to update activations: {:?}",
errors
));
}
Ok(())
}
pub fn update_summary(
&self,
id: &str,
summary: &str,
ctx: &crate::store::AgentContext,
) -> Result<bool> {
Self::runtime().block_on(self.update_summary_async(id, summary, ctx))
}
async fn update_summary_async(
&self,
id: &str,
summary: &str,
ctx: &crate::store::AgentContext,
) -> Result<bool> {
let id_part = id.strip_prefix("kn-").unwrap_or(id);
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let check_sql = format!(
"SELECT count() AS c FROM knowledge WHERE meta::id(id) = $id {} GROUP ALL",
visibility_clause
);
let mut check_response = with_db!(self, db, {
let mut query = db.query(&check_sql).bind(("id", id_part.to_string()));
if let Some(ref agent) = current_agent {
query = query.bind(("current_agent", agent.clone()));
}
query
.await
.context("Failed to check knowledge record existence for summary update")
})?;
let count_results: Vec<serde_json::Value> = check_response.take(0)?;
let exists = count_results
.first()
.and_then(|v| v["c"].as_i64())
.unwrap_or(0)
> 0;
if !exists {
return Ok(false);
}
let update_sql = format!(
"UPDATE knowledge SET summary = $summary WHERE meta::id(id) = $id {}",
visibility_clause
);
let mut response = with_db!(self, db, {
let mut query = db
.query(&update_sql)
.bind(("id", id_part.to_string()))
.bind(("summary", summary.to_string()));
if let Some(ref agent) = current_agent {
query = query.bind(("current_agent", agent.clone()));
}
query.await.context("Failed to update summary")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!("Failed to update summary: {:?}", errors));
}
Ok(true)
}
pub fn increment_activation_count(&self, ids: &[String]) -> Result<()> {
Self::runtime().block_on(self.increment_activation_count_async(ids))
}
async fn increment_activation_count_async(&self, ids: &[String]) -> Result<()> {
if ids.is_empty() {
return Ok(());
}
let clean_ids: Vec<String> = ids
.iter()
.map(|id| id.strip_prefix("kn-").unwrap_or(id).to_string())
.collect();
let things: Vec<Thing> = clean_ids
.iter()
.map(|id| Thing::from(("knowledge", id.as_str())))
.collect();
let mut response = with_db!(self, db, {
db.query(
"UPDATE knowledge SET
activation_count += 1
WHERE id IN $ids",
)
.bind(("ids", things))
.await
.context("Failed to increment activation counts")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!(
"Failed to increment activation counts: {:?}",
errors
));
}
Ok(())
}
pub fn query_recent_facts(&self, days: i32) -> Result<Vec<KnowledgeEntry>> {
Self::runtime().block_on(self.query_recent_facts_async(days))
}
async fn query_recent_facts_async(&self, days: i32) -> Result<Vec<KnowledgeEntry>> {
let expr = Self::effective_resonance_expr();
let sql = format!(
"SELECT {},
({expr}) AS effective_resonance
FROM knowledge
WHERE resonance_type = 'ephemeral'
AND created_at > time::now() - duration::from::days($days)
AND ({expr}) > 0.5
ORDER BY effective_resonance DESC",
Self::knowledge_select_fields(),
expr = expr
);
let mut response = with_db!(self, db, {
db.query(&sql)
.bind(("days", days))
.await
.context("Failed to execute recent facts query")
})?;
let results: Vec<serde_json::Value> = response
.take(0)
.context("Failed to parse recent facts results")?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
pub fn query_recent_facts_all_types(&self, days: i32) -> Result<Vec<KnowledgeEntry>> {
Self::runtime().block_on(self.query_recent_facts_all_types_async(days))
}
async fn query_recent_facts_all_types_async(&self, days: i32) -> Result<Vec<KnowledgeEntry>> {
let expr = Self::effective_resonance_expr();
let sql = format!(
"SELECT {},
({expr}) AS effective_resonance
FROM knowledge
WHERE created_at > time::now() - duration::from::days($days)
AND ({expr}) > 0.5
ORDER BY effective_resonance DESC",
Self::knowledge_select_fields(),
expr = expr
);
let mut response = with_db!(self, db, {
db.query(&sql)
.bind(("days", days))
.await
.context("Failed to execute recent facts (all types) query")
})?;
let results: Vec<serde_json::Value> = response
.take(0)
.context("Failed to parse recent facts (all types) results")?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
pub fn reinforce(
&self,
id: &str,
amount: i32,
cap: Option<i32>,
ctx: &crate::store::AgentContext,
) -> Result<Option<crate::store::ReinforcementResult>> {
Self::runtime().block_on(self.reinforce_async(id, amount, cap, ctx))
}
async fn reinforce_async(
&self,
id: &str,
amount: i32,
cap: Option<i32>,
ctx: &crate::store::AgentContext,
) -> Result<Option<crate::store::ReinforcementResult>> {
let normalized_id = if id.starts_with("kn-") {
id.to_string()
} else {
format!("kn-{}", id)
};
let id_part = normalized_id.strip_prefix("kn-").unwrap_or(&normalized_id);
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let select_sql = format!(
"SELECT resonance, activation_count FROM knowledge WHERE meta::id(id) = $id {}",
visibility_clause
);
let mut response = with_db!(self, db, {
let mut query = db.query(&select_sql).bind(("id", id_part.to_string()));
if let Some(ref agent) = current_agent {
query = query.bind(("current_agent", agent.clone()));
}
query.await.context("Failed to select entry for reinforce")
})?;
let results: Vec<serde_json::Value> = response
.take(0)
.context("Failed to parse entry for reinforce")?;
let current = match results.first() {
Some(v) => v,
None => return Ok(None),
};
let old_resonance = current
.get("resonance")
.and_then(|v| v.as_i64())
.unwrap_or(0) as i32;
let old_activation_count = current
.get("activation_count")
.and_then(|v| v.as_i64())
.unwrap_or(0) as i32;
let mut new_resonance = old_resonance + amount;
let capped = if let Some(cap_value) = cap {
if new_resonance > cap_value {
new_resonance = cap_value;
true
} else {
false
}
} else {
false
};
let new_activation_count = old_activation_count + 1;
let update_sql = format!(
"UPDATE knowledge SET
resonance = $new_resonance,
last_activated = time::now(),
activation_count = $new_count,
updated_at = time::now()
WHERE meta::id(id) = $id {}",
visibility_clause
);
let mut update_response = with_db!(self, db, {
let mut query = db
.query(&update_sql)
.bind(("id", id_part.to_string()))
.bind(("new_resonance", new_resonance))
.bind(("new_count", new_activation_count));
if let Some(ref agent) = current_agent {
query = query.bind(("current_agent", agent.clone()));
}
query.await.context("Failed to update entry for reinforce")
})?;
let errors = update_response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!("Failed to reinforce entry: {:?}", errors));
}
let now = Utc::now().to_rfc3339();
Ok(Some(crate::store::ReinforcementResult {
id: normalized_id,
old_resonance,
new_resonance,
amount_added: amount,
capped,
last_activated: now,
activation_count: new_activation_count,
}))
}
pub fn list_categories(&self) -> Result<Vec<Category>> {
Self::runtime().block_on(self.list_categories_async())
}
async fn list_categories_async(&self) -> Result<Vec<Category>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, <string>created_at AS created_at FROM category ORDER BY id")
.await
.context("Failed to list categories")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut categories = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
categories.push(Category {
id,
description: obj["description"].as_str().unwrap_or_default().to_string(),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(categories)
}
pub fn list_projects(&self) -> Result<Vec<Project>> {
Self::runtime().block_on(self.list_projects_async())
}
async fn list_projects_async(&self) -> Result<Vec<Project>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, name, path, repo_url, description, active, <string>created_at AS created_at, <string>updated_at AS updated_at FROM project ORDER BY name")
.await
.context("Failed to list projects")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut projects = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
projects.push(Project {
id,
name: obj["name"].as_str().unwrap_or_default().to_string(),
path: obj["path"].as_str().map(|s| s.to_string()),
repo_url: obj["repo_url"].as_str().map(|s| s.to_string()),
description: obj["description"].as_str().map(|s| s.to_string()),
active: obj["active"].as_bool().unwrap_or(true),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
updated_at: obj["updated_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(projects)
}
pub fn list_agents(&self) -> Result<Vec<Agent>> {
Self::runtime().block_on(self.list_agents_async())
}
async fn list_agents_async(&self) -> Result<Vec<Agent>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, domain, <string>created_at AS created_at, <string>updated_at AS updated_at FROM agent ORDER BY id")
.await
.context("Failed to list agents")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut agents = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
agents.push(Agent {
id,
description: obj["description"].as_str().map(|s| s.to_string()),
domain: obj["domain"].as_str().map(|s| s.to_string()),
created_at: obj["created_at"].as_str().map(|s| s.to_string()),
updated_at: obj["updated_at"].as_str().map(|s| s.to_string()),
});
}
Ok(agents)
}
pub fn list_tags(&self) -> Result<Vec<Tag>> {
Self::runtime().block_on(self.list_tags_async())
}
async fn list_tags_async(&self) -> Result<Vec<Tag>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, name, <string>created_at AS created_at FROM tag ORDER BY name")
.await
.context("Failed to list tags")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut tags = Vec::new();
for obj in results {
tags.push(Tag {
name: obj["name"].as_str().unwrap_or_default().to_string(),
created_at: obj["created_at"].as_str().map(|s| s.to_string()),
});
}
Ok(tags)
}
pub fn list_all_tags(&self, category: Option<&str>) -> Result<Vec<String>> {
Self::runtime().block_on(self.list_all_tags_async(category.map(|s| s.to_string())))
}
async fn list_all_tags_async(&self, category: Option<String>) -> Result<Vec<String>> {
let mut tags = if let Some(cat) = category {
let mut response = with_db!(self, db, {
db.query(
"SELECT VALUE name FROM tag \
WHERE <-tagged_with<-knowledge.category CONTAINS type::thing('category', $cat)",
)
.bind(("cat", cat))
.await
.context("Failed to list tags by category")
})?;
let tags: Vec<String> = response.take(0).unwrap_or_default();
tags
} else {
let mut response = with_db!(self, db, {
db.query("SELECT VALUE name FROM tag WHERE <-tagged_with")
.await
.context("Failed to list all tags")
})?;
let tags: Vec<String> = response.take(0).unwrap_or_default();
tags
};
tags.sort();
Ok(tags)
}
pub fn list_applicability_types(&self) -> Result<Vec<ApplicabilityType>> {
Self::runtime().block_on(self.list_applicability_types_async())
}
async fn list_applicability_types_async(&self) -> Result<Vec<ApplicabilityType>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, scope, <string>created_at AS created_at FROM applicability_type ORDER BY id")
.await
.context("Failed to list applicability types")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut types = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
types.push(ApplicabilityType {
id,
description: obj["description"].as_str().unwrap_or_default().to_string(),
scope: obj["scope"].as_str().map(|s| s.to_string()),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(types)
}
pub fn upsert_project_internal(&self, project: &Project) -> Result<RecordId> {
Self::runtime().block_on(self.upsert_project_async(project))
}
async fn upsert_project_async(&self, project: &Project) -> Result<RecordId> {
let record_id = RecordId::new("project", &project.id);
let now = Utc::now().to_rfc3339();
let created_at = if project.created_at.is_empty() {
now.clone()
} else {
project.created_at.clone()
};
let updated_at = if project.updated_at.is_empty() {
now.clone()
} else {
project.updated_at.clone()
};
let mut response = with_db!(self, db, {
db.query(
"UPSERT type::thing('project', $id) SET
name = $name,
path = $path,
repo_url = $repo_url,
description = $description,
active = $active,
created_at = <datetime>$created_at,
updated_at = <datetime>$updated_at
",
)
.bind(("id", project.id.clone()))
.bind(("name", project.name.clone()))
.bind(("path", project.path.clone()))
.bind(("repo_url", project.repo_url.clone()))
.bind(("description", project.description.clone()))
.bind(("active", project.active))
.bind(("created_at", normalize_datetime(&created_at)))
.bind(("updated_at", normalize_datetime(&updated_at)))
.await
.context("Failed to upsert project")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!("SurrealDB returned errors: {:?}", errors));
}
Ok(record_id)
}
pub fn add_relationship(&self, from: &str, to: &str, rel_type: &str) -> Result<()> {
Self::runtime().block_on(self.add_relationship_async(from, to, rel_type))
}
async fn add_relationship_async(&self, from: &str, to: &str, rel_type: &str) -> Result<()> {
let from_id = from.strip_prefix("kn-").unwrap_or(from);
let to_id = to.strip_prefix("kn-").unwrap_or(to);
let from_thing = Thing::from(("knowledge", from_id));
let to_thing = Thing::from(("knowledge", to_id));
let rel_type_thing = Thing::from(("relationship_type", rel_type));
with_db!(self, db, {
db.query("RELATE $from->relates_to->$to SET relationship_type = $rel_type, created_at = time::now()")
.bind(("from", from_thing))
.bind(("to", to_thing))
.bind(("rel_type", rel_type_thing))
.await
.context("Failed to create relationship")
})?;
Ok(())
}
pub fn list_relationships(&self, entry_id: &str) -> Result<Vec<Relationship>> {
Self::runtime().block_on(self.list_relationships_async(entry_id))
}
async fn list_relationships_async(&self, entry_id: &str) -> Result<Vec<Relationship>> {
let id_part = entry_id.strip_prefix("kn-").unwrap_or(entry_id);
let entry_thing = Thing::from(("knowledge", id_part));
#[derive(Debug, Deserialize)]
struct RelRow {
id: String,
from_entry_id: String,
to_entry_id: String,
relationship_type: String,
#[serde(default)]
created_at: Option<String>,
}
let mut response = with_db!(self, db, {
db.query(
"SELECT meta::id(id) AS id,
meta::id(in) AS from_entry_id,
meta::id(out) AS to_entry_id,
meta::id(relationship_type) AS relationship_type,
<string>created_at AS created_at
FROM relates_to
WHERE in = $entry OR out = $entry
ORDER BY created_at DESC",
)
.bind(("entry", entry_thing))
.await
.context("Failed to query relationships")
})?;
let results: Vec<RelRow> = response.take(0)?;
let relationships = results
.into_iter()
.map(|row| Relationship {
id: row.id,
from_entry_id: format!("kn-{}", row.from_entry_id),
to_entry_id: format!("kn-{}", row.to_entry_id),
relationship_type: row.relationship_type,
created_at: row.created_at.unwrap_or_else(|| "unknown".to_string()),
})
.collect();
Ok(relationships)
}
pub fn delete_relationship(&self, from: &str, to: &str, rel_type: &str) -> Result<bool> {
Self::runtime().block_on(self.delete_relationship_async(from, to, rel_type))
}
pub fn delete_relationship_by_id(&self, id: &str) -> Result<bool> {
Self::runtime().block_on(self.delete_relationship_by_id_async(id))
}
async fn delete_relationship_by_id_async(&self, id: &str) -> Result<bool> {
let mut check = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id FROM relates_to WHERE meta::id(id) = $id LIMIT 1")
.bind(("id", id.to_string()))
.await
.context("Failed to check relationship existence")
})?;
let exists: Vec<ExistsRow> = check.take(0)?;
if exists.is_empty() {
return Ok(false);
}
with_db!(self, db, {
db.query("DELETE relates_to WHERE meta::id(id) = $id")
.bind(("id", id.to_string()))
.await
.context("Failed to delete relationship by id")
})?;
Ok(true)
}
async fn delete_relationship_async(
&self,
from: &str,
to: &str,
rel_type: &str,
) -> Result<bool> {
let from_id = from.strip_prefix("kn-").unwrap_or(from);
let to_id = to.strip_prefix("kn-").unwrap_or(to);
let from_thing = Thing::from(("knowledge", from_id));
let to_thing = Thing::from(("knowledge", to_id));
let rel_type_thing = Thing::from(("relationship_type", rel_type));
let mut check = with_db!(self, db, {
db.query(
"SELECT meta::id(id) AS id FROM relates_to
WHERE in = $from AND out = $to AND relationship_type = $rel_type
LIMIT 1",
)
.bind(("from", from_thing.clone()))
.bind(("to", to_thing.clone()))
.bind(("rel_type", rel_type_thing.clone()))
.await
.context("Failed to check relationship existence")
})?;
let exists: Vec<ExistsRow> = check.take(0)?;
if exists.is_empty() {
return Ok(false);
}
with_db!(self, db, {
db.query(
"DELETE relates_to
WHERE in = $from AND out = $to AND relationship_type = $rel_type",
)
.bind(("from", from_thing))
.bind(("to", to_thing))
.bind(("rel_type", rel_type_thing))
.await
.context("Failed to delete relationship")
})?;
Ok(true)
}
pub fn get_facts_for_session(&self, session_id: &str) -> Result<Vec<String>> {
Self::runtime().block_on(self.get_facts_for_session_async(session_id))
}
async fn get_facts_for_session_async(&self, session_id: &str) -> Result<Vec<String>> {
let session_id_normalized = session_id.strip_prefix("kn-").unwrap_or(session_id);
let session_thing = Thing::from(("knowledge", session_id_normalized));
let mut response = with_db!(self, db, {
db.query(
"SELECT VALUE meta::id(in) FROM relates_to
WHERE out = $session_id AND relationship_type = relationship_type:extracted_from",
)
.bind(("session_id", session_thing))
.await
.context("Failed to query facts for session")
})?;
let fact_ids: Vec<String> = response.take(0).unwrap_or_default();
let facts_with_prefix: Vec<String> = fact_ids
.into_iter()
.map(|id| format!("kn-{}", id))
.collect();
Ok(facts_with_prefix)
}
pub fn get_session_for_fact(&self, fact_id: &str) -> Result<Option<String>> {
Self::runtime().block_on(self.get_session_for_fact_async(fact_id))
}
async fn get_session_for_fact_async(&self, fact_id: &str) -> Result<Option<String>> {
let fact_id_normalized = fact_id.strip_prefix("kn-").unwrap_or(fact_id);
let fact_thing = Thing::from(("knowledge", fact_id_normalized));
let mut response = with_db!(self, db, {
db.query(
"SELECT VALUE meta::id(out) FROM relates_to
WHERE in = $fact AND relationship_type = relationship_type:extracted_from",
)
.bind(("fact", fact_thing))
.await
.context("Failed to query session for fact")
})?;
let session_ids: Vec<String> = response.take(0).unwrap_or_default();
Ok(session_ids.first().map(|id| format!("kn-{}", id)))
}
pub fn get_tags_for_entry(&self, entry_id: &str) -> Result<Vec<String>> {
Self::runtime().block_on(self.get_tags_for_entry_async(entry_id))
}
async fn get_tags_for_entry_async(&self, entry_id: &str) -> Result<Vec<String>> {
let id_part = entry_id.strip_prefix("kn-").unwrap_or(entry_id);
let entry_thing = Thing::from(("knowledge", id_part));
let mut tags_response = with_db!(self, db, {
db.query("SELECT VALUE out.name FROM tagged_with WHERE in = $knowledge")
.bind(("knowledge", entry_thing))
.await
.context("Failed to query tags")
})?;
let tags: Vec<String> = tags_response.take(0).unwrap_or_default();
Ok(tags)
}
pub fn set_tags_for_entry(&self, _entry_id: &str, _tags: &[String]) -> Result<()> {
Ok(())
}
pub fn get_applicability_for_entry(&self, entry_id: &str) -> Result<Vec<String>> {
Self::runtime().block_on(self.get_applicability_for_entry_async(entry_id))
}
async fn get_applicability_for_entry_async(&self, entry_id: &str) -> Result<Vec<String>> {
let id_part = entry_id.strip_prefix("kn-").unwrap_or(entry_id);
let entry_thing = Thing::from(("knowledge", id_part));
let mut app_response = with_db!(self, db, {
db.query("SELECT VALUE meta::id(out) FROM applies_to WHERE in = $knowledge")
.bind(("knowledge", entry_thing))
.await
.context("Failed to query applicability")
})?;
let applicability_raw: Vec<Thing> = app_response.take(0).unwrap_or_default();
let applicability: Vec<String> = applicability_raw
.into_iter()
.map(|t| t.id.to_string())
.collect();
Ok(applicability)
}
pub fn set_applicability_for_entry(&self, _entry_id: &str, _ids: &[String]) -> Result<()> {
Ok(())
}
pub fn upsert_applicability_type(&self, atype: &ApplicabilityType) -> Result<()> {
Self::runtime().block_on(self.upsert_applicability_type_async(atype))
}
async fn upsert_applicability_type_async(&self, atype: &ApplicabilityType) -> Result<()> {
let now = Utc::now().to_rfc3339();
let created_at = if atype.created_at.is_empty() {
now
} else {
atype.created_at.clone()
};
let mut response = with_db!(self, db, {
db.query(
"UPSERT type::thing('applicability_type', $id) SET
description = $description,
scope = $scope,
created_at = <datetime>$created_at
",
)
.bind(("id", atype.id.clone()))
.bind(("description", atype.description.clone()))
.bind(("scope", atype.scope.clone()))
.bind(("created_at", normalize_datetime(&created_at)))
.await
.context("Failed to upsert applicability type")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!("SurrealDB returned errors: {:?}", errors));
}
Ok(())
}
pub fn get_category(&self, id: &str) -> Result<Option<Category>> {
Self::runtime().block_on(self.get_category_async(id))
}
async fn get_category_async(&self, id: &str) -> Result<Option<Category>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, <string>created_at AS created_at FROM category WHERE id = type::thing('category', $id)")
.bind(("id", id.to_string()))
.await
.context("Failed to query category")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
if results.is_empty() {
return Ok(None);
}
let obj = &results[0];
let id_str = obj["id"].as_str().unwrap_or_default().to_string();
Ok(Some(Category {
id: id_str,
description: obj["description"].as_str().unwrap_or_default().to_string(),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
}))
}
pub fn upsert_category(&self, category: &Category) -> Result<()> {
Self::runtime().block_on(self.upsert_category_async(category))
}
async fn upsert_category_async(&self, category: &Category) -> Result<()> {
let now = Utc::now().to_rfc3339();
let created_at = if category.created_at.is_empty() {
now
} else {
category.created_at.clone()
};
let mut response = with_db!(self, db, {
db.query(
"UPSERT type::thing('category', $id) SET
description = $description,
created_at = <datetime>$created_at
",
)
.bind(("id", category.id.clone()))
.bind(("description", category.description.clone()))
.bind(("created_at", normalize_datetime(&created_at)))
.await
.context("Failed to upsert category")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!("SurrealDB returned errors: {:?}", errors));
}
Ok(())
}
pub fn delete_category(&self, id: &str) -> Result<bool> {
Self::runtime().block_on(self.delete_category_async(id))
}
async fn delete_category_async(&self, id: &str) -> Result<bool> {
let category_thing = Thing::from(("category", id));
let mut count_response = with_db!(self, db, {
db.query("SELECT count() AS c FROM knowledge WHERE category = $category GROUP ALL")
.bind(("category", category_thing.clone()))
.await
.context("Failed to count knowledge entries for category")
})?;
let count_results: Vec<serde_json::Value> = count_response.take(0)?;
let count = count_results
.first()
.and_then(|v| v["c"].as_i64())
.unwrap_or(0);
if count > 0 {
return Err(anyhow::anyhow!(
"Cannot remove category '{}': {} entries still use it",
id,
count
));
}
let record_id = RecordId::new("category", id);
let result: Option<Value> = with_db!(self, db, {
db.delete(record_id.to_record_id())
.await
.context("Failed to delete category")
})?;
Ok(result.is_some())
}
pub fn get_project(&self, id: &str) -> Result<Option<Project>> {
Self::runtime().block_on(self.get_project_async(id))
}
async fn get_project_async(&self, id: &str) -> Result<Option<Project>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, name, path, repo_url, description, active, <string>created_at AS created_at, <string>updated_at AS updated_at FROM project WHERE id = type::thing('project', $id)")
.bind(("id", id.to_string()))
.await
.context("Failed to query project")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
if results.is_empty() {
return Ok(None);
}
let obj = &results[0];
let id_str = obj["id"].as_str().unwrap_or_default().to_string();
Ok(Some(Project {
id: id_str,
name: obj["name"].as_str().unwrap_or_default().to_string(),
path: obj["path"].as_str().map(|s| s.to_string()),
repo_url: obj["repo_url"].as_str().map(|s| s.to_string()),
description: obj["description"].as_str().map(|s| s.to_string()),
active: obj["active"].as_bool().unwrap_or(true),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
updated_at: obj["updated_at"].as_str().unwrap_or_default().to_string(),
}))
}
pub fn get_agent(&self, id: &str) -> Result<Option<Agent>> {
Self::runtime().block_on(self.get_agent_async(id))
}
async fn get_agent_async(&self, id: &str) -> Result<Option<Agent>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, domain, <string>created_at AS created_at, <string>updated_at AS updated_at FROM agent WHERE id = type::thing('agent', $id)")
.bind(("id", id.to_string()))
.await
.context("Failed to query agent")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
if results.is_empty() {
return Ok(None);
}
let obj = &results[0];
let id_str = obj["id"].as_str().unwrap_or_default().to_string();
Ok(Some(Agent {
id: id_str,
description: obj["description"].as_str().map(|s| s.to_string()),
domain: obj["domain"].as_str().map(|s| s.to_string()),
created_at: obj["created_at"].as_str().map(|s| s.to_string()),
updated_at: obj["updated_at"].as_str().map(|s| s.to_string()),
}))
}
pub fn upsert_agent(&self, agent: &Agent) -> Result<()> {
Self::runtime().block_on(self.upsert_agent_async(agent))
}
async fn upsert_agent_async(&self, agent: &Agent) -> Result<()> {
let now = Utc::now().to_rfc3339();
let created_at = agent.created_at.clone().unwrap_or_else(|| now.clone());
let updated_at = agent.updated_at.clone().unwrap_or_else(|| now.clone());
let mut response = with_db!(self, db, {
db.query(
"UPSERT type::thing('agent', $id) SET
description = $description,
domain = $domain,
created_at = <datetime>$created_at,
updated_at = <datetime>$updated_at
",
)
.bind(("id", agent.id.clone()))
.bind(("description", agent.description.clone()))
.bind(("domain", agent.domain.clone()))
.bind(("created_at", normalize_datetime(&created_at)))
.bind(("updated_at", normalize_datetime(&updated_at)))
.await
.context("Failed to upsert agent")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!("SurrealDB returned errors: {:?}", errors));
}
Ok(())
}
pub fn get_tags_for_project(&self, _project_id: &str) -> Result<Vec<String>> {
Ok(vec![])
}
pub fn set_tags_for_project(&self, _project_id: &str, _tags: &[String]) -> Result<()> {
Ok(())
}
pub fn get_applicability_for_project(&self, _project_id: &str) -> Result<Vec<String>> {
Ok(vec![])
}
pub fn set_applicability_for_project(&self, _project_id: &str, _ids: &[String]) -> Result<()> {
Ok(())
}
pub fn edit_content(
&self,
id: &str,
ctx: &crate::store::AgentContext,
old_text: &str,
new_text: &str,
replace_all: bool,
nth: Option<usize>,
) -> Result<crate::store::EditResult> {
let entry = self
.get_knowledge(id, ctx)?
.ok_or_else(|| anyhow::anyhow!("Entry not found: {}", id))?;
let body = entry
.body
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Entry has no body content"))?;
let result = crate::content_ops::edit_content(body, old_text, new_text, replace_all, nth)?;
let mut updated = entry;
let content_hash = KnowledgeEntry::compute_hash(&result.new_content);
updated.body = Some(result.new_content.clone());
updated.updated_at = Some(chrono::Utc::now().to_rfc3339());
updated.content_hash = Some(content_hash);
self.upsert_knowledge_internal(&updated)?;
Ok(crate::store::EditResult {
replacements: result.replacements,
new_content: result.new_content,
})
}
pub fn append_content(
&self,
id: &str,
ctx: &crate::store::AgentContext,
content: &str,
) -> Result<()> {
let entry = self
.get_knowledge(id, ctx)?
.ok_or_else(|| anyhow::anyhow!("Entry not found: {}", id))?;
let new_body = crate::content_ops::append_content(entry.body.as_deref(), content);
let mut updated = entry;
let content_hash = KnowledgeEntry::compute_hash(&new_body);
updated.body = Some(new_body);
updated.updated_at = Some(chrono::Utc::now().to_rfc3339());
updated.content_hash = Some(content_hash);
self.upsert_knowledge_internal(&updated)?;
Ok(())
}
pub fn prepend_content(
&self,
id: &str,
ctx: &crate::store::AgentContext,
content: &str,
) -> Result<()> {
let entry = self
.get_knowledge(id, ctx)?
.ok_or_else(|| anyhow::anyhow!("Entry not found: {}", id))?;
let new_body = crate::content_ops::prepend_content(entry.body.as_deref(), content);
let mut updated = entry;
let content_hash = KnowledgeEntry::compute_hash(&new_body);
updated.body = Some(new_body);
updated.updated_at = Some(chrono::Utc::now().to_rfc3339());
updated.content_hash = Some(content_hash);
self.upsert_knowledge_internal(&updated)?;
Ok(())
}
pub fn list_tables(&self) -> Result<Vec<String>> {
Self::runtime().block_on(self.list_tables_async())
}
async fn list_tables_async(&self) -> Result<Vec<String>> {
let mut response = with_db!(self, db, {
db.query("INFO FOR DB")
.await
.context("Failed to query database info")
})?;
let info: Option<serde_json::Value> = response.take(0)?;
let mut tables = Vec::new();
if let Some(info_json) = info
&& let Some(tables_obj) = info_json.get("tables").and_then(|v| v.as_object())
{
for table_name in tables_obj.keys() {
tables.push(table_name.clone());
}
tables.sort();
}
Ok(tables)
}
pub fn count(&self) -> Result<usize> {
Self::runtime().block_on(self.count_async())
}
async fn count_async(&self) -> Result<usize> {
let mut response = with_db!(self, db, {
db.query("SELECT count() AS c FROM knowledge GROUP ALL")
.await
.context("Failed to count knowledge entries")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let count = results.first().and_then(|v| v["c"].as_i64()).unwrap_or(0) as usize;
Ok(count)
}
pub fn graph_health(&self) -> Result<serde_json::Value> {
Self::runtime().block_on(self.graph_health_async())
}
async fn graph_health_async(&self) -> Result<serde_json::Value> {
let mut response = with_db!(self, db, {
db.query(
"SELECT
count() AS total,
math::sum(IF embedding IS NOT NONE THEN 1 ELSE 0 END) AS embedded,
math::sum(IF anchors IS NOT NONE AND array::len(anchors) > 0 THEN 1 ELSE 0 END) AS anchored,
math::sum(IF (last_activated IS NONE OR last_activated < time::now() - duration::from::days(30)) AND resonance >= 5 THEN 1 ELSE 0 END) AS stale_high_res
FROM knowledge GROUP ALL",
)
.await
.context("Failed to query graph health")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let row = results.into_iter().next().unwrap_or_default();
let total = row["total"].as_i64().unwrap_or(0);
let embedded = row["embedded"].as_i64().unwrap_or(0);
let anchored = row["anchored"].as_i64().unwrap_or(0);
let stale_high_res = row["stale_high_res"].as_i64().unwrap_or(0);
let pct = |n: i64| -> i64 {
if total == 0 {
0
} else {
(n * 100 + total / 2) / total
}
};
Ok(serde_json::json!({
"total": total,
"embedded": embedded,
"anchored": anchored,
"stale_high_res": stale_high_res,
"embedded_pct": pct(embedded),
"anchored_pct": pct(anchored),
"stale_high_res_pct": pct(stale_high_res),
}))
}
pub fn growth_sparkline(&self) -> Result<serde_json::Value> {
Self::runtime().block_on(self.growth_sparkline_async())
}
async fn growth_sparkline_async(&self) -> Result<serde_json::Value> {
let results: Vec<serde_json::Value> = {
let mut response = with_db!(self, db, {
db.query(
"SELECT
(<int>time::unix(<datetime>created_at) / 604800) AS week_bucket,
count() AS cnt
FROM knowledge
WHERE created_at > time::now() - duration::from::days(56)
GROUP BY week_bucket
ORDER BY week_bucket",
)
.await
.context("Failed to query growth sparkline")
})?;
response.take(0).unwrap_or_default()
};
let mut bucket_map: std::collections::BTreeMap<i64, i64> =
std::collections::BTreeMap::new();
for row in &results {
let bucket = row["week_bucket"].as_i64().unwrap_or(0);
let cnt = row["cnt"].as_i64().unwrap_or(0);
bucket_map.insert(bucket, cnt);
}
let now_secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
let current_bucket = now_secs / 604800;
let counts: Vec<i64> = (0i64..8)
.map(|offset| {
let bucket = current_bucket - (7 - offset);
*bucket_map.get(&bucket).unwrap_or(&0)
})
.collect();
Ok(serde_json::json!(counts))
}
pub fn open_threads(&self) -> Result<serde_json::Value> {
Self::runtime().block_on(self.open_threads_async())
}
async fn open_threads_async(&self) -> Result<serde_json::Value> {
let mut response = with_db!(self, db, {
db.query(
"SELECT
meta::id(id) AS id,
body,
summary,
<string>created_at AS created_at,
resonance,
->tagged_with->tag.name AS tags
FROM knowledge
WHERE category = category:thread
AND (summary IS NONE OR summary.state IS NONE OR summary.state = 'open')
ORDER BY created_at DESC",
)
.await
.context("Failed to query open threads")
})?;
let rows: Vec<serde_json::Value> = response.take(0).unwrap_or_default();
let now_secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as f64)
.unwrap_or(0.0);
let mut threads: Vec<serde_json::Value> = rows
.into_iter()
.filter_map(|row| {
let id = row["id"].as_str().unwrap_or("").to_string();
if id.is_empty() {
return None;
}
let summary_raw = &row["summary"];
let state = if summary_raw.is_null()
|| summary_raw.is_string() && summary_raw.as_str().unwrap_or("").is_empty()
{
"open".to_string()
} else {
let s: serde_json::Value = if let Some(s) = summary_raw.as_str() {
serde_json::from_str(s).unwrap_or(serde_json::Value::Null)
} else {
summary_raw.clone()
};
s.get("state")
.and_then(|v| v.as_str())
.unwrap_or("open")
.to_string()
};
if state != "open" {
return None;
}
let resonance = row["resonance"].as_i64().unwrap_or(0);
let created_at = row["created_at"].as_str().unwrap_or("").to_string();
let tags = row["tags"].clone();
Some(serde_json::json!({
"id": format!("kn-{}", id),
"body": row["body"],
"state": state,
"created_at": created_at,
"resonance": resonance,
"tags": tags,
"_score": Self::decay_score(resonance, &created_at, now_secs),
}))
})
.collect();
threads.sort_by(|a, b| {
let sa = a["_score"].as_f64().unwrap_or(0.0);
let sb = b["_score"].as_f64().unwrap_or(0.0);
sb.partial_cmp(&sa).unwrap_or(std::cmp::Ordering::Equal)
});
for t in &mut threads {
if let Some(obj) = t.as_object_mut() {
obj.remove("_score");
}
}
Ok(serde_json::json!(threads))
}
fn decay_score(resonance: i64, created_at: &str, now_secs: f64) -> f64 {
let weeks = chrono::DateTime::parse_from_rfc3339(&created_at.replace('Z', "+00:00"))
.map(|dt| {
let created_secs = dt.timestamp() as f64;
(now_secs - created_secs) / (7.0 * 86400.0)
})
.unwrap_or(52.0);
resonance as f64 * 0.95_f64.powf(weeks)
}
pub fn list_all(&self, ctx: &crate::store::AgentContext) -> Result<Vec<KnowledgeEntry>> {
Self::runtime().block_on(self.list_all_async(ctx))
}
async fn list_all_async(
&self,
ctx: &crate::store::AgentContext,
) -> Result<Vec<KnowledgeEntry>> {
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let where_clause = visibility_clause.replacen("AND", "WHERE", 1);
let sql = format!(
"SELECT {}
FROM knowledge
{}
ORDER BY id",
Self::knowledge_select_fields(),
where_clause
);
let mut response = with_db!(self, db, {
let mut query = db.query(&sql);
if let Some(agent) = current_agent {
query = query.bind(("current_agent", agent));
}
query.await.context("Failed to query all knowledge entries")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
pub fn list_by_category(
&self,
category: &str,
ctx: &crate::store::AgentContext,
filter: &crate::store::KnowledgeFilter,
) -> Result<Vec<KnowledgeEntry>> {
Self::runtime().block_on(self.list_by_category_async(category, ctx, filter))
}
pub fn count_by_category(
&self,
category: &str,
ctx: &crate::store::AgentContext,
filter: &crate::store::KnowledgeFilter,
) -> Result<usize> {
Self::runtime().block_on(self.count_by_category_async(category, ctx, filter))
}
async fn count_by_category_async(
&self,
category: &str,
ctx: &crate::store::AgentContext,
filter: &crate::store::KnowledgeFilter,
) -> Result<usize> {
let category_thing = Thing::from(("category", category));
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let resonance_clause = Self::build_resonance_filter(filter);
let sql = format!(
"SELECT count() AS c FROM (
SELECT id FROM knowledge
WHERE category = $category {} {}
) GROUP ALL",
visibility_clause, resonance_clause
);
let mut response = with_db!(self, db, {
let mut query = db.query(&sql).bind(("category", category_thing));
if let Some(agent) = current_agent {
query = query.bind(("current_agent", agent));
}
query.await.context("Failed to count knowledge by category")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let count = results.first().and_then(|v| v["c"].as_i64()).unwrap_or(0) as usize;
Ok(count)
}
async fn list_by_category_async(
&self,
category: &str,
ctx: &crate::store::AgentContext,
filter: &crate::store::KnowledgeFilter,
) -> Result<Vec<KnowledgeEntry>> {
let category_thing = Thing::from(("category", category));
let (visibility_clause, current_agent) = Self::build_visibility_filter(ctx);
let resonance_clause = Self::build_resonance_filter(filter);
let sql = format!(
"SELECT {}
FROM knowledge
WHERE category = $category {} {}
ORDER BY id",
Self::knowledge_select_fields(),
visibility_clause,
resonance_clause
);
let mut response = with_db!(self, db, {
let mut query = db.query(&sql).bind(("category", category_thing));
if let Some(agent) = current_agent {
query = query.bind(("current_agent", agent));
}
query.await.context("Failed to query knowledge by category")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut entries = Vec::new();
for obj in results {
entries.push(self.value_to_knowledge_entry(obj).await?);
}
Ok(entries)
}
pub fn list_sessions(&self, _project_id: Option<&str>) -> Result<Vec<Session>> {
Ok(vec![])
}
pub fn get_session(&self, _id: &str) -> Result<Option<Session>> {
Ok(None)
}
pub fn upsert_session(&self, _session: &Session) -> Result<()> {
Ok(())
}
pub fn list_source_types(&self) -> Result<Vec<SourceType>> {
Self::runtime().block_on(self.list_source_types_async())
}
async fn list_source_types_async(&self) -> Result<Vec<SourceType>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, <string>created_at AS created_at FROM source_type ORDER BY id")
.await
.context("Failed to list source types")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut types = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
types.push(SourceType {
id,
description: obj["description"].as_str().unwrap_or_default().to_string(),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(types)
}
pub fn list_entry_types(&self) -> Result<Vec<EntryType>> {
Self::runtime().block_on(self.list_entry_types_async())
}
async fn list_entry_types_async(&self) -> Result<Vec<EntryType>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, <string>created_at AS created_at FROM entry_type ORDER BY id")
.await
.context("Failed to list entry types")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut types = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
types.push(EntryType {
id,
description: obj["description"].as_str().unwrap_or_default().to_string(),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(types)
}
pub fn list_content_types(&self) -> Result<Vec<ContentType>> {
Self::runtime().block_on(self.list_content_types_async())
}
async fn list_content_types_async(&self) -> Result<Vec<ContentType>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, file_extensions, <string>created_at AS created_at FROM content_type ORDER BY id")
.await
.context("Failed to list content types")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut types = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
let file_extensions = obj["file_extensions"].as_array().map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect()
});
types.push(ContentType {
id,
description: obj["description"].as_str().unwrap_or_default().to_string(),
file_extensions,
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(types)
}
pub fn list_session_types(&self) -> Result<Vec<SessionType>> {
Self::runtime().block_on(self.list_session_types_async())
}
async fn list_session_types_async(&self) -> Result<Vec<SessionType>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, <string>created_at AS created_at FROM session_type ORDER BY id")
.await
.context("Failed to list session types")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut types = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
types.push(SessionType {
id,
description: obj["description"].as_str().unwrap_or_default().to_string(),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(types)
}
pub fn list_relationship_types(&self) -> Result<Vec<RelationshipType>> {
Self::runtime().block_on(self.list_relationship_types_async())
}
async fn list_relationship_types_async(&self) -> Result<Vec<RelationshipType>> {
let mut response = with_db!(self, db, {
db.query("SELECT meta::id(id) AS id, description, directional, <string>created_at AS created_at FROM relationship_type ORDER BY id")
.await
.context("Failed to list relationship types")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
let mut types = Vec::new();
for obj in results {
let id = obj["id"].as_str().unwrap_or_default().to_string();
types.push(RelationshipType {
id,
description: obj["description"].as_str().unwrap_or_default().to_string(),
directional: obj["directional"].as_bool().unwrap_or(false),
created_at: obj["created_at"].as_str().unwrap_or_default().to_string(),
});
}
Ok(types)
}
pub fn create_wake_session(&self, session: &crate::wake_token::WakeSession) -> Result<String> {
Self::runtime().block_on(self.create_wake_session_async(session))
}
async fn create_wake_session_async(
&self,
session: &crate::wake_token::WakeSession,
) -> Result<String> {
let bloom_chunk_meta_json = serde_json::to_value(&session.bloom_chunk_meta)?;
let created_at = chrono::DateTime::from_timestamp(session.created_at, 0)
.unwrap_or_else(chrono::Utc::now)
.to_rfc3339();
let mut response = with_db!(self, db, {
db.query(
"CREATE type::thing('wake_session', $session_id) SET
bloom_ids = $bloom_ids,
current_index = $current_index,
current_chunk_index = $current_chunk_index,
step = $step,
attempts_on_current = $attempts_on_current,
remembered_count = $remembered_count,
needed_help_count = $needed_help_count,
skipped_count = $skipped_count,
created_at = <datetime>$created_at,
bloom_chunk_meta = $bloom_chunk_meta
",
)
.bind(("session_id", session.session_id.clone()))
.bind(("bloom_ids", session.bloom_ids.clone()))
.bind(("current_index", session.current_index as i64))
.bind(("current_chunk_index", session.current_chunk_index as i64))
.bind(("step", session.step as i64))
.bind(("attempts_on_current", session.attempts_on_current as i64))
.bind(("remembered_count", session.remembered_count as i64))
.bind(("needed_help_count", session.needed_help_count as i64))
.bind(("skipped_count", session.skipped_count as i64))
.bind(("created_at", normalize_datetime(&created_at)))
.bind(("bloom_chunk_meta", bloom_chunk_meta_json))
.await
.context("Failed to create wake session")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!(
"SurrealDB error creating wake session: {:?}",
errors
));
}
Ok(session.session_id.clone())
}
pub fn get_wake_session(
&self,
session_id: &str,
) -> Result<Option<crate::wake_token::WakeSession>> {
Self::runtime().block_on(self.get_wake_session_async(session_id))
}
async fn get_wake_session_async(
&self,
session_id: &str,
) -> Result<Option<crate::wake_token::WakeSession>> {
let mut response = with_db!(self, db, {
db.query(
"SELECT
meta::id(id) AS session_id,
bloom_ids,
current_index,
current_chunk_index,
step,
attempts_on_current,
remembered_count,
needed_help_count,
skipped_count,
<int>time::unix(<datetime>created_at) AS created_at,
bloom_chunk_meta
FROM type::thing('wake_session', $session_id)",
)
.bind(("session_id", session_id.to_string()))
.await
.context("Failed to get wake session")
})?;
let results: Vec<serde_json::Value> = response.take(0)?;
if results.is_empty() {
return Ok(None);
}
let obj = &results[0];
let session_id_str = obj["session_id"].as_str().unwrap_or_default().to_string();
let bloom_ids: Vec<String> = obj["bloom_ids"]
.as_array()
.unwrap_or(&vec![])
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect();
let current_index = obj["current_index"].as_u64().unwrap_or(0) as usize;
let raw_chunk_idx = obj["current_chunk_index"].as_u64().unwrap_or(0);
let current_chunk_index = u16::try_from(raw_chunk_idx).map_err(|_| {
anyhow::anyhow!(
"wake_session.current_chunk_index {} exceeds u16::MAX; \
session is corrupt or schema has drifted",
raw_chunk_idx
)
})?;
let step = obj["step"].as_u64().unwrap_or(0) as u32;
let attempts_on_current = obj["attempts_on_current"].as_u64().unwrap_or(0) as u8;
let remembered_count = obj["remembered_count"].as_u64().unwrap_or(0) as u32;
let needed_help_count = obj["needed_help_count"].as_u64().unwrap_or(0) as u32;
let skipped_count = obj["skipped_count"].as_u64().unwrap_or(0) as u32;
let created_at = obj["created_at"]
.as_i64()
.unwrap_or_else(|| chrono::Utc::now().timestamp());
let bloom_chunk_meta: Vec<crate::wake_token::BloomChunkMeta> =
match obj.get("bloom_chunk_meta") {
Some(v) if !v.is_null() => serde_json::from_value(v.clone()).unwrap_or_default(),
_ => Vec::new(),
};
let bloom_chunk_meta = if bloom_chunk_meta.len() == bloom_ids.len() {
bloom_chunk_meta
} else {
bloom_ids
.iter()
.map(|_| crate::wake_token::BloomChunkMeta {
authored_phrase_count: 0,
is_phraseless: true,
..Default::default()
})
.collect()
};
Ok(Some(crate::wake_token::WakeSession {
session_id: session_id_str,
bloom_ids,
current_index,
current_chunk_index,
step,
attempts_on_current,
remembered_count,
needed_help_count,
skipped_count,
created_at,
bloom_chunk_meta,
}))
}
pub fn update_wake_session(&self, session: &crate::wake_token::WakeSession) -> Result<()> {
Self::runtime().block_on(self.update_wake_session_async(session))
}
async fn update_wake_session_async(
&self,
session: &crate::wake_token::WakeSession,
) -> Result<()> {
let bloom_chunk_meta_json = serde_json::to_value(&session.bloom_chunk_meta)?;
let mut response = with_db!(self, db, {
db.query(
"UPDATE type::thing('wake_session', $session_id) SET
current_index = $current_index,
current_chunk_index = $current_chunk_index,
step = $step,
attempts_on_current = $attempts_on_current,
remembered_count = $remembered_count,
needed_help_count = $needed_help_count,
skipped_count = $skipped_count,
bloom_chunk_meta = $bloom_chunk_meta
",
)
.bind(("session_id", session.session_id.clone()))
.bind(("current_index", session.current_index as i64))
.bind(("current_chunk_index", session.current_chunk_index as i64))
.bind(("step", session.step as i64))
.bind(("attempts_on_current", session.attempts_on_current as i64))
.bind(("remembered_count", session.remembered_count as i64))
.bind(("needed_help_count", session.needed_help_count as i64))
.bind(("skipped_count", session.skipped_count as i64))
.bind(("bloom_chunk_meta", bloom_chunk_meta_json))
.await
.context("Failed to update wake session")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!(
"SurrealDB error updating wake session: {:?}",
errors
));
}
Ok(())
}
pub fn delete_wake_session(&self, session_id: &str) -> Result<()> {
Self::runtime().block_on(self.delete_wake_session_async(session_id))
}
async fn delete_wake_session_async(&self, session_id: &str) -> Result<()> {
let mut response = with_db!(self, db, {
db.query("DELETE type::thing('wake_session', $session_id)")
.bind(("session_id", session_id.to_string()))
.await
.context("Failed to delete wake session")
})?;
let errors = response.take_errors();
if !errors.is_empty() {
return Err(anyhow::anyhow!(
"SurrealDB error deleting wake session: {:?}",
errors
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::store::KnowledgeStore;
#[test]
fn test_open_in_memory() {
let _db = SurrealDatabase::open_in_memory().unwrap();
}
#[test]
fn test_schema_applies_without_error() {
let _db = SurrealDatabase::open_in_memory().unwrap();
}
#[test]
fn test_open_with_path() {
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
let db_path = temp_dir.path().join("test.surreal");
let _db = SurrealDatabase::open(&db_path).unwrap();
assert!(db_path.exists());
assert!(db_path.is_dir());
}
#[test]
fn test_upsert_applicability_type_with_datetime() {
use crate::types::ApplicabilityType;
let db = SurrealDatabase::open_in_memory().unwrap();
let atype = ApplicabilityType {
id: "test_type".to_string(),
description: "Test applicability type".to_string(),
scope: Some("test".to_string()),
created_at: "2025-11-29T12:00:00Z".to_string(),
};
db.upsert_applicability_type(&atype).unwrap();
}
#[test]
fn test_upsert_project_with_datetime() {
use crate::types::Project;
let db = SurrealDatabase::open_in_memory().unwrap();
let project = Project {
id: "test_project".to_string(),
name: "Test Project".to_string(),
path: Some("/test/path".to_string()),
repo_url: None,
description: Some("Test description".to_string()),
active: true,
created_at: "2025-11-29T12:00:00Z".to_string(),
updated_at: "2025-11-29T12:30:00Z".to_string(),
};
db.upsert_project(&project).unwrap();
}
#[test]
fn test_upsert_agent_with_datetime() {
use crate::types::Agent;
let db = SurrealDatabase::open_in_memory().unwrap();
let agent = Agent {
id: "test_agent".to_string(),
description: Some("Test agent".to_string()),
domain: Some("testing".to_string()),
created_at: Some("2025-11-29T12:00:00Z".to_string()),
updated_at: Some("2025-11-29T12:30:00Z".to_string()),
};
db.upsert_agent(&agent).unwrap();
}
fn make_test_entry(
id: &str,
resonance: i32,
decay_rate: f64,
) -> crate::knowledge::KnowledgeEntry {
use chrono::Utc;
let now = Utc::now().to_rfc3339();
crate::knowledge::KnowledgeEntry {
id: id.to_string(),
category_id: "test".to_string(),
title: format!("Test Entry {}", id),
body: Some("Test body".to_string()),
summary: None,
applicability: vec![],
source_project_id: None,
source_agent_id: None,
file_path: None,
tags: vec![],
created_at: Some(now.clone()),
updated_at: Some(now.clone()),
content_hash: Some("test-hash".to_string()),
source_type_id: Some("manual".to_string()),
entry_type_id: Some("primary".to_string()),
session_id: None,
ephemeral: false,
content_type_id: Some("text".to_string()),
owner: None,
visibility: "public".to_string(),
resonance,
resonance_type: Some("ephemeral".to_string()),
last_activated: Some(now),
activation_count: 0,
decay_rate,
anchors: vec![],
wake_phrases: vec![],
wake_order: None,
wake_phrase: None,
embedding: None,
embedding_model: None,
embedded_at: None,
format: "markdown".to_string(),
effective_resonance: None,
}
}
#[test]
fn test_id_normalization_double_prefix() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry = make_test_entry("kn-test123", 5, 0.5);
db.upsert_knowledge(&entry).unwrap();
let ctx = crate::store::AgentContext::public_only();
let result = db.get("kn-kn-test123", &ctx).unwrap();
assert!(result.is_none(), "Double prefix should not match");
let result = db.get("kn-test123", &ctx).unwrap();
assert!(result.is_some(), "Normal prefix should match");
}
#[test]
fn test_id_normalization_case_sensitivity() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry = make_test_entry("kn-test456", 5, 0.5);
db.upsert_knowledge(&entry).unwrap();
let ctx = crate::store::AgentContext::public_only();
let result = db.get("KN-test456", &ctx).unwrap();
assert!(
result.is_none(),
"Uppercase KN should not match lowercase kn"
);
}
#[test]
fn test_id_normalization_empty_suffix() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let result = db.get("kn-", &ctx);
assert!(result.is_ok(), "Empty suffix should not panic");
}
#[test]
fn test_id_normalization_no_prefix() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry = make_test_entry("kn-test789", 5, 0.5);
db.upsert_knowledge(&entry).unwrap();
let ctx = crate::store::AgentContext::public_only();
let result = db.get("test789", &ctx).unwrap();
assert!(result.is_some(), "ID without prefix should still match");
}
#[test]
fn test_decay_formula_zero_days() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry = make_test_entry("kn-fresh", 10, 0.5);
db.upsert_knowledge(&entry).unwrap();
let facts = db.query_recent_facts(1).unwrap();
assert!(!facts.is_empty(), "Should find fresh facts");
}
#[test]
fn test_decay_formula_negative_days() {
let db = SurrealDatabase::open_in_memory().unwrap();
let result = db.query_recent_facts(-1);
assert!(result.is_ok(), "Negative days should not panic");
}
#[test]
fn test_decay_formula_extreme_resonance() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-transcendent", 13, 0.0);
entry.resonance_type = Some("ephemeral".to_string());
db.upsert_knowledge(&entry).unwrap();
let result = db.query_recent_facts(30);
assert!(
result.is_ok(),
"Extreme resonance should not break decay formula"
);
let facts = result.unwrap();
assert!(!facts.is_empty(), "Should find transcendent fact");
}
#[test]
fn test_decay_formula_max_int_resonance() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-maxres", i32::MAX, 0.0);
entry.resonance_type = Some("ephemeral".to_string());
db.upsert_knowledge(&entry).unwrap();
let result = db.query_recent_facts(30);
assert!(result.is_ok(), "MAX resonance should not overflow");
}
#[test]
fn test_tiered_decay_low_resonance_ephemeral() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-low-res", 2, 0.0);
entry.resonance_type = Some("ephemeral".to_string());
db.upsert_knowledge(&entry).unwrap();
let result = db.query_recent_facts(7).unwrap();
assert!(
!result.is_empty(),
"Low-resonance ephemeral entry should be returned when freshly created"
);
}
#[test]
fn test_tiered_decay_mid_resonance_ephemeral() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-mid-res", 5, 0.0);
entry.resonance_type = Some("ephemeral".to_string());
db.upsert_knowledge(&entry).unwrap();
let result = db.query_recent_facts(7).unwrap();
assert!(
!result.is_empty(),
"Mid-resonance ephemeral entry should be returned when freshly created"
);
}
#[test]
fn test_tiered_decay_high_resonance_ephemeral() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-high-res", 7, 0.0);
entry.resonance_type = Some("ephemeral".to_string());
db.upsert_knowledge(&entry).unwrap();
let result = db.query_recent_facts(7).unwrap();
assert!(
!result.is_empty(),
"High-resonance ephemeral entry should be returned when freshly created"
);
}
#[test]
fn test_tiered_decay_ordering_over_time() {
use chrono::Utc;
let db = SurrealDatabase::open_in_memory().unwrap();
let thirty_days_ago = (Utc::now() - chrono::Duration::days(30)).to_rfc3339();
let mut low = make_test_entry("kn-decay-low", 3, 0.0);
low.resonance_type = Some("ephemeral".to_string());
low.last_activated = Some(thirty_days_ago.clone());
db.upsert_knowledge(&low).unwrap();
let mut high = make_test_entry("kn-decay-high", 7, 0.0);
high.resonance_type = Some("ephemeral".to_string());
high.last_activated = Some(thirty_days_ago);
db.upsert_knowledge(&high).unwrap();
let results = db.query_recent_facts(60).unwrap();
let low_found = results.iter().any(|e| e.id == "kn-decay-low");
let high_found = results.iter().any(|e| e.id == "kn-decay-high");
assert!(
low_found,
"Low-resonance entry should still pass > 0.5 filter after 30 days"
);
assert!(
high_found,
"High-resonance entry should pass > 0.5 filter after 30 days"
);
let low_pos = results.iter().position(|e| e.id == "kn-decay-low").unwrap();
let high_pos = results
.iter()
.position(|e| e.id == "kn-decay-high")
.unwrap();
assert!(
high_pos < low_pos,
"High-resonance entry (slower decay) should rank above low-resonance entry after 30 days"
);
}
#[test]
fn test_bloom_exemption_foundational() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-foundational", 9, 0.0);
entry.resonance_type = Some("foundational".to_string());
db.upsert_knowledge(&entry).unwrap();
let ephemeral_results = db.query_recent_facts(30).unwrap();
let found_in_ephemeral = ephemeral_results.iter().any(|e| e.id == "kn-foundational");
assert!(
!found_in_ephemeral,
"Foundational entry should not appear in ephemeral fact query"
);
let ctx = crate::store::AgentContext::public_only();
let direct = db.get("kn-foundational", &ctx).unwrap();
assert!(
direct.is_some(),
"Foundational entry should be directly retrievable"
);
}
#[test]
fn test_bloom_exemption_transformative() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-transformative", 8, 0.0);
entry.resonance_type = Some("transformative".to_string());
db.upsert_knowledge(&entry).unwrap();
let ephemeral_results = db.query_recent_facts(30).unwrap();
let found_in_ephemeral = ephemeral_results
.iter()
.any(|e| e.id == "kn-transformative");
assert!(
!found_in_ephemeral,
"Transformative entry should not appear in ephemeral fact query"
);
let ctx = crate::store::AgentContext::public_only();
let direct = db.get("kn-transformative", &ctx).unwrap();
assert!(
direct.is_some(),
"Transformative entry should be directly retrievable"
);
}
#[test]
fn test_increment_activation_count_no_timestamp_reset() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry = make_test_entry("kn-incr-test", 5, 0.0);
db.upsert_knowledge(&entry).unwrap();
let ctx = crate::store::AgentContext::public_only();
let before = db.get("kn-incr-test", &ctx).unwrap().unwrap();
let initial_count = before.activation_count;
let initial_last_activated = before.last_activated.clone();
db.increment_activation_count(&["kn-incr-test".to_string()])
.unwrap();
let after = db.get("kn-incr-test", &ctx).unwrap().unwrap();
assert_eq!(
after.activation_count,
initial_count + 1,
"activation_count should increment by 1"
);
assert_eq!(
after.last_activated, initial_last_activated,
"last_activated should not be reset by increment_activation_count"
);
}
#[test]
fn test_thread_duplicate_detection() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry1 = make_test_entry("kn-thread1", 5, 0.5);
let mut entry2 = make_test_entry("kn-thread2", 5, 0.5);
entry2.body = Some(" TEST BODY ".to_string());
db.upsert_knowledge(&entry1).unwrap();
db.upsert_knowledge(&entry2).unwrap();
let ctx = crate::store::AgentContext::public_only();
assert!(db.get("kn-thread1", &ctx).unwrap().is_some());
assert!(db.get("kn-thread2", &ctx).unwrap().is_some());
}
#[test]
fn test_session_linkage_round_trip() {
let db = SurrealDatabase::open_in_memory().unwrap();
let session = make_test_entry("kn-session123", 0, 0.0);
db.upsert_knowledge(&session).unwrap();
let mut fact = make_test_entry("kn-fact456", 5, 0.5);
fact.session_id = Some("kn-session123".to_string());
db.upsert_knowledge(&fact).unwrap();
db.add_relationship("kn-fact456", "kn-session123", "extracted_from")
.unwrap();
let facts = db.get_facts_for_session("kn-session123").unwrap();
assert_eq!(facts.len(), 1, "Should find one fact for session");
assert_eq!(
facts[0], "kn-fact456",
"Should return full fact ID with prefix"
);
let session_id = db.get_session_for_fact("kn-fact456").unwrap();
assert!(session_id.is_some(), "Should find session for fact");
assert_eq!(
session_id.unwrap(),
"kn-session123",
"Should return full session ID with prefix"
);
}
#[test]
fn test_session_linkage_multiple_facts() {
let db = SurrealDatabase::open_in_memory().unwrap();
let session = make_test_entry("kn-multisession", 0, 0.0);
db.upsert_knowledge(&session).unwrap();
for i in 1..=5 {
let mut fact = make_test_entry(&format!("kn-fact{}", i), 5, 0.5);
fact.session_id = Some("kn-multisession".to_string());
db.upsert_knowledge(&fact).unwrap();
db.add_relationship(
&format!("kn-fact{}", i),
"kn-multisession",
"extracted_from",
)
.unwrap();
}
let facts = db.get_facts_for_session("kn-multisession").unwrap();
assert_eq!(facts.len(), 5, "Should find all 5 facts for session");
}
#[test]
fn test_session_linkage_orphaned_fact() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut fact = make_test_entry("kn-orphan", 5, 0.5);
fact.session_id = Some("kn-ghost".to_string());
db.upsert_knowledge(&fact).unwrap();
let facts = db.get_facts_for_session("kn-ghost").unwrap();
assert_eq!(
facts.len(),
0,
"Orphaned fact should not appear without relationship"
);
let session = db.get_session_for_fact("kn-orphan").unwrap();
assert!(session.is_none(), "Orphaned fact should have no session");
}
#[test]
fn test_normalize_content_edge_cases() {
use crate::knowledge::KnowledgeEntry;
assert_eq!(KnowledgeEntry::normalize_content(""), "");
assert_eq!(KnowledgeEntry::normalize_content(" \n\t "), "");
let unicode = "Hello 世界! Привет мир!";
let normalized = KnowledgeEntry::normalize_content(unicode);
assert!(normalized.contains("hello"), "Should lowercase ASCII");
assert!(normalized.contains("世界"), "Should preserve unicode");
let messy = " hello\n\n world\t\ttest ";
assert_eq!(KnowledgeEntry::normalize_content(messy), "hello world test");
}
#[test]
fn test_wake_cascade_empty_anchors() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let mut entry = make_test_entry("kn-solo", 9, 0.0);
entry.resonance_type = Some("foundational".to_string());
entry.anchors = vec![];
db.upsert_knowledge(&entry).unwrap();
let cascade = db.wake_cascade(&ctx, 50, Some(7), 7).unwrap();
assert!(!cascade.core.is_empty(), "Should find core bloom");
}
#[test]
fn test_wake_cascade_circular_anchors() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut bloom_a = make_test_entry("kn-circular-a", 9, 0.0);
bloom_a.resonance_type = Some("foundational".to_string());
bloom_a.anchors = vec!["kn-circular-b".to_string()];
let mut bloom_b = make_test_entry("kn-circular-b", 9, 0.0);
bloom_b.resonance_type = Some("foundational".to_string());
bloom_b.anchors = vec!["kn-circular-a".to_string()];
db.upsert_knowledge(&bloom_a).unwrap();
db.upsert_knowledge(&bloom_b).unwrap();
let ctx = crate::store::AgentContext::public_only();
let result = db.wake_cascade(&ctx, 50, Some(7), 7);
assert!(
result.is_ok(),
"Circular anchors should not cause infinite loop"
);
}
#[test]
fn test_privacy_filtering_public_only() {
let db = SurrealDatabase::open_in_memory().unwrap();
let public_entry = make_test_entry("kn-public", 5, 0.5);
db.upsert_knowledge(&public_entry).unwrap();
let mut private_entry = make_test_entry("kn-private", 5, 0.5);
private_entry.visibility = "private".to_string();
private_entry.owner = Some("test_agent".to_string());
db.upsert_knowledge(&private_entry).unwrap();
let ctx = crate::store::AgentContext::public_only();
assert!(
db.get("kn-public", &ctx).unwrap().is_some(),
"Should see public entry"
);
assert!(
db.get("kn-private", &ctx).unwrap().is_none(),
"Should not see private entry"
);
}
#[test]
fn test_privacy_filtering_agent_context() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut private_entry = make_test_entry("kn-my-private", 5, 0.5);
private_entry.visibility = "private".to_string();
private_entry.owner = Some("test_agent".to_string());
db.upsert_knowledge(&private_entry).unwrap();
let mut other_entry = make_test_entry("kn-other-private", 5, 0.5);
other_entry.visibility = "private".to_string();
other_entry.owner = Some("other_agent".to_string());
db.upsert_knowledge(&other_entry).unwrap();
let ctx = crate::store::AgentContext::for_agent("test_agent");
assert!(
db.get("kn-my-private", &ctx).unwrap().is_some(),
"Should see own private entry"
);
assert!(
db.get("kn-other-private", &ctx).unwrap().is_none(),
"Should not see other's private entry"
);
}
#[test]
fn test_delete_cross_agent_visibility_blocked() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-private-del-target", 5, 0.0);
entry.visibility = "private".to_string();
entry.owner = Some("agent-a".to_string());
db.upsert_knowledge(&entry).unwrap();
let ctx_b = crate::store::AgentContext::for_agent("agent-b");
let result = db.delete("kn-private-del-target", &ctx_b).unwrap();
assert!(
!result,
"agent-b should not be able to delete agent-a's private entry"
);
let ctx_a = crate::store::AgentContext::for_agent("agent-a");
let still_exists = db.get("kn-private-del-target", &ctx_a).unwrap();
assert!(
still_exists.is_some(),
"Entry should still exist for agent-a after failed cross-agent delete"
);
}
#[test]
fn test_update_summary_cross_agent_visibility_blocked() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-private-summary-target", 5, 0.0);
entry.visibility = "private".to_string();
entry.owner = Some("agent-a".to_string());
entry.summary = Some(r#"{"state":"open"}"#.to_string());
db.upsert_knowledge(&entry).unwrap();
let ctx_b = crate::store::AgentContext::for_agent("agent-b");
let result = db
.update_summary(
"kn-private-summary-target",
r#"{"state":"compromised"}"#,
&ctx_b,
)
.unwrap();
assert!(
!result,
"agent-b should not be able to update summary on agent-a's private entry"
);
let ctx_a = crate::store::AgentContext::for_agent("agent-a");
let unchanged = db
.get("kn-private-summary-target", &ctx_a)
.unwrap()
.unwrap();
let summary: serde_json::Value =
serde_json::from_str(unchanged.summary.as_deref().unwrap()).unwrap();
assert_eq!(
summary["state"], "open",
"Summary should be unchanged after failed cross-agent update"
);
}
#[test]
fn test_reinforce_basic() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let mut entry = make_test_entry("kn-test-reinforce", 5, 0.0);
entry.activation_count = 10;
db.upsert_knowledge(&entry).unwrap();
let result = db
.reinforce("kn-test-reinforce", 2, Some(10), &ctx)
.unwrap()
.expect("reinforce should return Some for visible entry");
assert_eq!(result.id, "kn-test-reinforce");
assert_eq!(result.old_resonance, 5);
assert_eq!(result.new_resonance, 7);
assert_eq!(result.amount_added, 2);
assert!(!result.capped);
assert_eq!(result.activation_count, 11);
let updated = db.get("kn-test-reinforce", &ctx).unwrap().unwrap();
assert_eq!(updated.resonance, 7);
assert_eq!(updated.activation_count, 11);
assert!(updated.last_activated.is_some());
}
#[test]
fn test_reinforce_with_cap() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let entry = make_test_entry("kn-test-cap", 9, 0.0);
db.upsert_knowledge(&entry).unwrap();
let result = db
.reinforce("kn-test-cap", 5, Some(10), &ctx)
.unwrap()
.expect("reinforce should return Some for visible entry");
assert_eq!(result.old_resonance, 9);
assert_eq!(result.new_resonance, 10);
assert_eq!(result.amount_added, 5);
assert!(result.capped);
let updated = db.get("kn-test-cap", &ctx).unwrap().unwrap();
assert_eq!(updated.resonance, 10);
}
#[test]
fn test_reinforce_without_cap() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let entry = make_test_entry("kn-test-no-cap", 9, 0.0);
db.upsert_knowledge(&entry).unwrap();
let result = db
.reinforce("kn-test-no-cap", 5, None, &ctx)
.unwrap()
.expect("reinforce should return Some for visible entry");
assert_eq!(result.old_resonance, 9);
assert_eq!(result.new_resonance, 14);
assert!(!result.capped);
let updated = db.get("kn-test-no-cap", &ctx).unwrap().unwrap();
assert_eq!(updated.resonance, 14);
}
#[test]
fn test_reinforce_nonexistent() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let result = db.reinforce("kn-nonexistent", 1, Some(10), &ctx).unwrap();
assert!(
result.is_none(),
"reinforce should return None for nonexistent entry"
);
}
#[test]
fn test_reinforce_id_normalization() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let entry = make_test_entry("kn-test-norm", 5, 0.0);
db.upsert_knowledge(&entry).unwrap();
let result = db
.reinforce("test-norm", 2, Some(10), &ctx)
.unwrap()
.expect("reinforce should return Some for visible entry");
assert_eq!(result.id, "kn-test-norm");
assert_eq!(result.new_resonance, 7);
}
#[test]
fn test_reinforce_cross_agent_visibility_blocked() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-private-reinforce-target", 5, 0.0);
entry.visibility = "private".to_string();
entry.owner = Some("agent-a".to_string());
entry.activation_count = 3;
db.upsert_knowledge(&entry).unwrap();
let ctx_b = crate::store::AgentContext::for_agent("agent-b");
let result = db
.reinforce("kn-private-reinforce-target", 2, Some(10), &ctx_b)
.unwrap();
assert!(
result.is_none(),
"agent-b should not be able to reinforce agent-a's private entry"
);
let ctx_a = crate::store::AgentContext::for_agent("agent-a");
let unchanged = db
.get("kn-private-reinforce-target", &ctx_a)
.unwrap()
.unwrap();
assert_eq!(
unchanged.resonance, 5,
"Resonance should be unchanged after failed cross-agent reinforce"
);
assert_eq!(
unchanged.activation_count, 3,
"Activation count should be unchanged after failed cross-agent reinforce"
);
}
#[test]
fn test_reinforce_own_private_entry() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut entry = make_test_entry("kn-private-reinforce-own", 5, 0.0);
entry.visibility = "private".to_string();
entry.owner = Some("agent-a".to_string());
entry.activation_count = 3;
db.upsert_knowledge(&entry).unwrap();
let ctx_a = crate::store::AgentContext::for_agent("agent-a");
let result = db
.reinforce("kn-private-reinforce-own", 2, Some(10), &ctx_a)
.unwrap()
.expect("agent-a should be able to reinforce their own private entry");
assert_eq!(result.old_resonance, 5);
assert_eq!(result.new_resonance, 7);
assert_eq!(result.activation_count, 4);
let updated = db.get("kn-private-reinforce-own", &ctx_a).unwrap().unwrap();
assert_eq!(updated.resonance, 7);
assert_eq!(updated.activation_count, 4);
}
#[test]
fn test_update_summary_persists() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let mut entry = make_test_entry("kn-summary-test", 5, 0.0);
entry.summary = Some(r#"{"state":"open","topic":"test thread"}"#.to_string());
db.upsert_knowledge(&entry).unwrap();
let new_summary = r#"{"state":"closed","topic":"test thread"}"#;
let result = db
.update_summary("kn-summary-test", new_summary, &ctx)
.unwrap();
assert!(
result,
"update_summary should return true for visible entry"
);
let updated = db.get("kn-summary-test", &ctx).unwrap().unwrap();
let summary: serde_json::Value =
serde_json::from_str(updated.summary.as_deref().unwrap()).unwrap();
assert_eq!(summary["state"], "closed");
assert_eq!(summary["topic"], "test thread");
}
#[test]
fn test_update_summary_id_normalization() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let mut entry = make_test_entry("kn-summary-norm", 5, 0.0);
entry.summary = Some(r#"{"state":"open"}"#.to_string());
db.upsert_knowledge(&entry).unwrap();
let result = db
.update_summary("summary-norm", r#"{"state":"closed"}"#, &ctx)
.unwrap();
assert!(result, "update_summary should return true with raw ID");
let updated = db.get("kn-summary-norm", &ctx).unwrap().unwrap();
let summary: serde_json::Value =
serde_json::from_str(updated.summary.as_deref().unwrap()).unwrap();
assert_eq!(summary["state"], "closed");
let result2 = db
.update_summary("kn-summary-norm", r#"{"state":"reopened"}"#, &ctx)
.unwrap();
assert!(
result2,
"update_summary should return true with prefixed ID"
);
let updated2 = db.get("kn-summary-norm", &ctx).unwrap().unwrap();
let summary2: serde_json::Value =
serde_json::from_str(updated2.summary.as_deref().unwrap()).unwrap();
assert_eq!(summary2["state"], "reopened");
}
#[test]
fn test_close_thread_with_no_summary() {
let db = SurrealDatabase::open_in_memory().unwrap();
let ctx = crate::store::AgentContext::public_only();
let mut entry = make_test_entry("kn-no-summary-thread", 5, 0.0);
entry.summary = None;
db.upsert_knowledge(&entry).unwrap();
let closed_summary = r#"{"state":"closed","topic":"pre-convention thread"}"#;
let result = db
.update_summary("kn-no-summary-thread", closed_summary, &ctx)
.unwrap();
assert!(
result,
"update_summary should return true for entry with no prior summary"
);
let updated = db.get("kn-no-summary-thread", &ctx).unwrap().unwrap();
let summary: serde_json::Value =
serde_json::from_str(updated.summary.as_deref().unwrap()).unwrap();
assert_eq!(summary["state"], "closed");
assert_eq!(summary["topic"], "pre-convention thread");
}
#[test]
fn test_get_summary_state_returns_none_for_no_summary() {
let entry = make_test_entry("kn-state-none", 5, 0.0);
assert!(
entry.summary.is_none(),
"make_test_entry should produce summary: None"
);
assert_eq!(
entry.get_summary_state(),
None,
"get_summary_state() must return None when summary is absent"
);
}
#[test]
fn test_query_recent_facts_all_types_includes_foundational() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut foundational = make_test_entry("kn-all-types-foundational", 9, 0.0);
foundational.resonance_type = Some("foundational".to_string());
db.upsert_knowledge(&foundational).unwrap();
let mut ephemeral = make_test_entry("kn-all-types-ephemeral", 5, 0.0);
ephemeral.resonance_type = Some("ephemeral".to_string());
db.upsert_knowledge(&ephemeral).unwrap();
let ephemeral_results = db.query_recent_facts(30).unwrap();
assert!(
!ephemeral_results
.iter()
.any(|e| e.id == "kn-all-types-foundational"),
"Foundational entry should not appear in ephemeral-only query"
);
assert!(
ephemeral_results
.iter()
.any(|e| e.id == "kn-all-types-ephemeral"),
"Ephemeral entry should appear in ephemeral-only query"
);
let all_results = db.query_recent_facts_all_types(30).unwrap();
assert!(
all_results
.iter()
.any(|e| e.id == "kn-all-types-foundational"),
"Foundational entry should appear in all-types query"
);
assert!(
all_results.iter().any(|e| e.id == "kn-all-types-ephemeral"),
"Ephemeral entry should appear in all-types query"
);
}
#[test]
fn test_query_recent_facts_all_types_includes_transformative() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut transformative = make_test_entry("kn-all-types-transformative", 8, 0.0);
transformative.resonance_type = Some("transformative".to_string());
db.upsert_knowledge(&transformative).unwrap();
let all_results = db.query_recent_facts_all_types(30).unwrap();
assert!(
all_results
.iter()
.any(|e| e.id == "kn-all-types-transformative"),
"Transformative entry should appear in all-types query"
);
}
#[test]
fn test_query_recent_facts_all_types_respects_decay_threshold() {
let db = SurrealDatabase::open_in_memory().unwrap();
let mut high = make_test_entry("kn-all-types-high", 8, 0.0);
high.resonance_type = Some("ephemeral".to_string());
db.upsert_knowledge(&high).unwrap();
let results = db.query_recent_facts_all_types(30).unwrap();
assert!(
results.iter().any(|e| e.id == "kn-all-types-high"),
"High-resonance ephemeral entry should appear in all-types query"
);
}
fn make_tagged_entry(
id: &str,
category: &str,
tags: Vec<String>,
) -> crate::knowledge::KnowledgeEntry {
let mut entry = make_test_entry(id, 5, 0.0);
entry.category_id = category.to_string();
entry.tags = tags;
entry
}
#[test]
fn test_list_all_tags_returns_distinct_tags() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry1 = make_tagged_entry(
"kn-tag1",
"pattern",
vec!["rust".to_string(), "async".to_string()],
);
db.upsert_knowledge(&entry1).unwrap();
let entry2 = make_tagged_entry(
"kn-tag2",
"technique",
vec!["rust".to_string(), "error-handling".to_string()],
);
db.upsert_knowledge(&entry2).unwrap();
let tags = db.list_all_tags(None).unwrap();
assert_eq!(tags.len(), 3);
assert_eq!(tags, vec!["async", "error-handling", "rust"]);
}
#[test]
fn test_list_all_tags_with_category_filter() {
let db = SurrealDatabase::open_in_memory().unwrap();
let entry1 = make_tagged_entry(
"kn-tag3",
"pattern",
vec!["rust".to_string(), "async".to_string()],
);
db.upsert_knowledge(&entry1).unwrap();
let entry2 = make_tagged_entry(
"kn-tag4",
"technique",
vec!["rust".to_string(), "error-handling".to_string()],
);
db.upsert_knowledge(&entry2).unwrap();
let pattern_tags = db.list_all_tags(Some("pattern")).unwrap();
assert_eq!(pattern_tags.len(), 2);
assert_eq!(pattern_tags, vec!["async", "rust"]);
let technique_tags = db.list_all_tags(Some("technique")).unwrap();
assert_eq!(technique_tags.len(), 2);
assert_eq!(technique_tags, vec!["error-handling", "rust"]);
}
#[test]
fn test_list_all_tags_empty_database() {
let db = SurrealDatabase::open_in_memory().unwrap();
let tags = db.list_all_tags(None).unwrap();
assert!(tags.is_empty());
let tags = db.list_all_tags(Some("pattern")).unwrap();
assert!(tags.is_empty());
}
}