Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(memory): use updated_at for staleness (#2389)

Eric Hayes5b523afdf82c
diff --git a/rust/cloud-storage/memory/.sqlx/query-2927c247896749abc3a07a94702655f1046994e3869605f098852ee2a2853c8c.json b/rust/cloud-storage/memory/.sqlx/query-2927c247896749abc3a07a94702655f1046994e3869605f098852ee2a2853c8c.json
new file mode 100644
index 0000000000..588d28fa47
--- /dev/null
+++ b/rust/cloud-storage/memory/.sqlx/query-2927c247896749abc3a07a94702655f1046994e3869605f098852ee2a2853c8c.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE memory\n SET created_at = NOW() - INTERVAL '10 days',\n updated_at = NOW() - INTERVAL '10 days'\n WHERE user_id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "2927c247896749abc3a07a94702655f1046994e3869605f098852ee2a2853c8c"
+}
diff --git a/rust/cloud-storage/memory/.sqlx/query-343e4f6931752a30ceb2e078834aacd0a8486948494a4dc14b62e943590e97f6.json b/rust/cloud-storage/memory/.sqlx/query-a0ccb1b43344dc0501f538d62f7b3209dfe7e2e60fa1396cfb3efff85e9d8c4d.json
similarity index 64%
rename from rust/cloud-storage/memory/.sqlx/query-343e4f6931752a30ceb2e078834aacd0a8486948494a4dc14b62e943590e97f6.json
rename to rust/cloud-storage/memory/.sqlx/query-a0ccb1b43344dc0501f538d62f7b3209dfe7e2e60fa1396cfb3efff85e9d8c4d.json
index 3d96b51e53..6b155d41f0 100644
--- a/rust/cloud-storage/memory/.sqlx/query-343e4f6931752a30ceb2e078834aacd0a8486948494a4dc14b62e943590e97f6.json
+++ b/rust/cloud-storage/memory/.sqlx/query-a0ccb1b43344dc0501f538d62f7b3209dfe7e2e60fa1396cfb3efff85e9d8c4d.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT memory, created_at as \"created_at!\"\n FROM memory\n WHERE user_id = $1\n ORDER BY created_at DESC\n LIMIT 1\n ",
+ "query": "\n SELECT memory, updated_at as \"updated_at!\"\n FROM memory\n WHERE user_id = $1\n ORDER BY updated_at DESC\n LIMIT 1\n ",
"describe": {
"columns": [
{
@@ -10,7 +10,7 @@
},
{
"ordinal": 1,
- "name": "created_at!",
+ "name": "updated_at!",
"type_info": "Timestamptz"
}
],
@@ -24,5 +24,5 @@
false
]
},
- "hash": "343e4f6931752a30ceb2e078834aacd0a8486948494a4dc14b62e943590e97f6"
+ "hash": "a0ccb1b43344dc0501f538d62f7b3209dfe7e2e60fa1396cfb3efff85e9d8c4d"
}
diff --git a/rust/cloud-storage/memory/src/domain/ports.rs b/rust/cloud-storage/memory/src/domain/ports.rs
index 1fedaaffe2..df34f860d7 100644
--- a/rust/cloud-storage/memory/src/domain/ports.rs
+++ b/rust/cloud-storage/memory/src/domain/ports.rs
@@ -22,13 +22,13 @@ pub type Result<T> = std::result::Result<T, MemoryError>;
pub type Memory = String;
-/// A memory record with its creation timestamp.
+/// A memory record with its latest refresh timestamp.
#[derive(Debug)]
pub struct MemoryRecord {
/// The memory text.
pub memory: Memory,
- /// When this memory was generated.
- pub created_at: DateTime<Utc>,
+ /// When this memory was last generated or refreshed.
+ pub updated_at: DateTime<Utc>,
}
pub trait MemoryRepo: Send + Sync + 'static {
diff --git a/rust/cloud-storage/memory/src/domain/service.rs b/rust/cloud-storage/memory/src/domain/service.rs
index 5e6c4dd44b..c920ff062a 100644
--- a/rust/cloud-storage/memory/src/domain/service.rs
+++ b/rust/cloud-storage/memory/src/domain/service.rs
@@ -102,7 +102,7 @@ where
let needs_generation = match &record {
Some(r) => {
- let age = Utc::now() - r.created_at;
+ let age = Utc::now() - r.updated_at;
age > chrono::Duration::from_std(MAX_AGE).unwrap_or(chrono::TimeDelta::MAX)
}
None => true,
diff --git a/rust/cloud-storage/memory/src/outbound/pg_memory_repo.rs b/rust/cloud-storage/memory/src/outbound/pg_memory_repo.rs
index 5afa9c08af..a110f981b2 100644
--- a/rust/cloud-storage/memory/src/outbound/pg_memory_repo.rs
+++ b/rust/cloud-storage/memory/src/outbound/pg_memory_repo.rs
@@ -41,10 +41,10 @@ impl MemoryRepo for PgMemoryRepo {
async fn get_latest_memory(&self, user: MacroUserIdStr<'_>) -> Result<Option<MemoryRecord>> {
let row = sqlx::query!(
r#"
- SELECT memory, created_at as "created_at!"
+ SELECT memory, updated_at as "updated_at!"
FROM memory
WHERE user_id = $1
- ORDER BY created_at DESC
+ ORDER BY updated_at DESC
LIMIT 1
"#,
user.as_ref(),
@@ -54,7 +54,7 @@ impl MemoryRepo for PgMemoryRepo {
Ok(row.map(|r| MemoryRecord {
memory: r.memory,
- created_at: r.created_at,
+ updated_at: r.updated_at,
}))
}
diff --git a/rust/cloud-storage/memory/src/outbound/pg_memory_repo/test.rs b/rust/cloud-storage/memory/src/outbound/pg_memory_repo/test.rs
index 41f3310903..467487dd47 100644
--- a/rust/cloud-storage/memory/src/outbound/pg_memory_repo/test.rs
+++ b/rust/cloud-storage/memory/src/outbound/pg_memory_repo/test.rs
@@ -87,7 +87,7 @@ async fn memories_are_scoped_to_user(pool: Pool<Postgres>) {
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
-async fn get_latest_includes_created_at(pool: Pool<Postgres>) {
+async fn get_latest_includes_updated_at(pool: Pool<Postgres>) {
let repo = PgMemoryRepo::new(pool);
let user = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
@@ -96,5 +96,36 @@ async fn get_latest_includes_created_at(pool: Pool<Postgres>) {
.unwrap();
let record = repo.get_latest_memory(user).await.unwrap().unwrap();
- assert!(record.created_at <= chrono::Utc::now());
+ assert!(record.updated_at <= chrono::Utc::now());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn get_latest_uses_refresh_time_for_staleness(pool: Pool<Postgres>) {
+ let repo = PgMemoryRepo::new(pool.clone());
+ let user = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+
+ repo.save_memory(&"first memory".to_string(), user.clone())
+ .await
+ .unwrap();
+
+ sqlx::query!(
+ r#"
+ UPDATE memory
+ SET created_at = NOW() - INTERVAL '10 days',
+ updated_at = NOW() - INTERVAL '10 days'
+ WHERE user_id = $1
+ "#,
+ user.as_ref()
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+
+ repo.save_memory(&"fresh memory".to_string(), user.clone())
+ .await
+ .unwrap();
+
+ let record = repo.get_latest_memory(user).await.unwrap().unwrap();
+ assert_eq!(record.memory, "fresh memory");
+ assert!(record.updated_at > chrono::Utc::now() - chrono::Duration::minutes(1));
}