Tracing: DAO layer + handler spans on tracing, drop all Context params (#141)
trace_db_call no longer takes an OpenTelemetry Context: it opens a db.<query_type>.<operation> span (same name via otel.name, same db.query_type / db.operation attrs) on the caller's ambient tracing span and runs the DAO body inside it. The cx / ctx / context parameter is gone from every DAO trait method, impl and call site, and from the helpers and background drains that only threaded it. The remaining request-handler spans (files, tags, faces, image, video, favorites, duplicates, memories) move to #[instrument]; the Context::current_with_span / with_remote_span_context plumbing is gone, as is extract_context_from_request (the tracing-actix-web root span covers it). otel::set_span_attributes replaces BoxedSpan::set_attributes. Background-job raw spans (thumbnails, video actors) are left for #142. tests/otel_db_spans.rs pins name, attributes, status and nesting (across an instrumented spawn); otel_traceparent now uses an #[instrument] handler. CLAUDE.md documents the tracing conventions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1c508cdabd
commit
b6deca2a52
55 files changed
+1942
-3852
No files matched your search
+70
-167
@@ -77,22 +77,12 @@ pub struct ImportPersona {
|
||||
}
|
||||
|
||||
pub trait PersonaDao: Sync + Send {
|
||||
fn list_personas(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
) -> Result<Vec<Persona>, DbError>;
|
||||
fn list_personas(&mut self, user_id: i32) -> Result<Vec<Persona>, DbError>;
|
||||
|
||||
fn get_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
) -> Result<Option<Persona>, DbError>;
|
||||
fn get_persona(&mut self, user_id: i32, persona_id: &str) -> Result<Option<Persona>, DbError>;
|
||||
|
||||
fn create_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
name: &str,
|
||||
@@ -103,28 +93,17 @@ pub trait PersonaDao: Sync + Send {
|
||||
|
||||
fn update_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
patch: PersonaPatch,
|
||||
) -> Result<Option<Persona>, DbError>;
|
||||
|
||||
fn delete_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
) -> Result<bool, DbError>;
|
||||
fn delete_persona(&mut self, user_id: i32, persona_id: &str) -> Result<bool, DbError>;
|
||||
|
||||
/// Idempotent bulk import. INSERT OR IGNORE on (user_id, persona_id)
|
||||
/// — re-uploading the same set is a no-op. Returns the number of rows
|
||||
/// actually inserted (skipped duplicates don't count).
|
||||
fn bulk_import(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
personas: &[ImportPersona],
|
||||
) -> Result<usize, DbError>;
|
||||
fn bulk_import(&mut self, user_id: i32, personas: &[ImportPersona]) -> Result<usize, DbError>;
|
||||
|
||||
// ── Persona-chat (open chat with persona) persistence ───────────
|
||||
//
|
||||
@@ -137,7 +116,6 @@ pub trait PersonaDao: Sync + Send {
|
||||
/// another user — the two are deliberately indistinguishable to callers.
|
||||
fn get_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
conversation_id: &str,
|
||||
) -> Result<Option<PersonaChatRow>, DbError>;
|
||||
@@ -145,18 +123,13 @@ pub trait PersonaDao: Sync + Send {
|
||||
/// Every conversation this user has going, newest first. Backs the chat
|
||||
/// list screen, so it returns the transcript blob too — the snippet is
|
||||
/// derived from the tree rather than denormalized into its own column.
|
||||
fn list_persona_chats(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
) -> Result<Vec<PersonaChatRow>, DbError>;
|
||||
fn list_persona_chats(&mut self, user_id: i32) -> Result<Vec<PersonaChatRow>, DbError>;
|
||||
|
||||
/// Start a new conversation with a persona, returning its id. Several
|
||||
/// conversations with the same persona are expected, so this never
|
||||
/// reuses an existing row.
|
||||
fn create_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
created_at: i64,
|
||||
@@ -169,7 +142,6 @@ pub trait PersonaDao: Sync + Send {
|
||||
/// this user's.
|
||||
fn update_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
conversation_id: &str,
|
||||
messages_json: &str,
|
||||
@@ -181,7 +153,6 @@ pub trait PersonaDao: Sync + Send {
|
||||
/// enough of an exchange to summarize.
|
||||
fn set_persona_chat_title(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
conversation_id: &str,
|
||||
title: &str,
|
||||
@@ -189,12 +160,7 @@ pub trait PersonaDao: Sync + Send {
|
||||
|
||||
/// Delete a conversation outright. Backs both the list screen's delete
|
||||
/// affordance and the reset endpoint.
|
||||
fn delete_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
conversation_id: &str,
|
||||
) -> Result<(), DbError>;
|
||||
fn delete_persona_chat(&mut self, user_id: i32, conversation_id: &str) -> Result<(), DbError>;
|
||||
}
|
||||
|
||||
pub struct SqlitePersonaDao {
|
||||
@@ -220,12 +186,8 @@ impl SqlitePersonaDao {
|
||||
}
|
||||
|
||||
impl PersonaDao for SqlitePersonaDao {
|
||||
fn list_personas(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
) -> Result<Vec<Persona>, DbError> {
|
||||
trace_db_call(cx, "query", "list_personas", |_span| {
|
||||
fn list_personas(&mut self, uid: i32) -> Result<Vec<Persona>, DbError> {
|
||||
trace_db_call("query", "list_personas", |_span| {
|
||||
use schema::personas::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
personas
|
||||
@@ -237,13 +199,8 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn get_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
) -> Result<Option<Persona>, DbError> {
|
||||
trace_db_call(cx, "query", "get_persona", |_span| {
|
||||
fn get_persona(&mut self, uid: i32, pid: &str) -> Result<Option<Persona>, DbError> {
|
||||
trace_db_call("query", "get_persona", |_span| {
|
||||
use schema::personas::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
personas
|
||||
@@ -258,7 +215,6 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
|
||||
fn create_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
nm: &str,
|
||||
@@ -266,7 +222,7 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
builtin: bool,
|
||||
include_all: bool,
|
||||
) -> Result<Persona, DbError> {
|
||||
trace_db_call(cx, "insert", "create_persona", |_span| {
|
||||
trace_db_call("insert", "create_persona", |_span| {
|
||||
use schema::personas::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
let now = chrono::Utc::now().timestamp_millis();
|
||||
@@ -298,12 +254,11 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
|
||||
fn update_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
patch: PersonaPatch,
|
||||
) -> Result<Option<Persona>, DbError> {
|
||||
trace_db_call(cx, "update", "update_persona", |_span| {
|
||||
trace_db_call("update", "update_persona", |_span| {
|
||||
use schema::personas::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
let now = chrono::Utc::now().timestamp_millis();
|
||||
@@ -359,13 +314,8 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
.map_err(|e| DbError::log(DbErrorKind::UpdateError, e))
|
||||
}
|
||||
|
||||
fn delete_persona(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
) -> Result<bool, DbError> {
|
||||
trace_db_call(cx, "delete", "delete_persona", |_span| {
|
||||
fn delete_persona(&mut self, uid: i32, pid: &str) -> Result<bool, DbError> {
|
||||
trace_db_call("delete", "delete_persona", |_span| {
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
// One transaction so a persona and its conversations can't get
|
||||
// out of step. There is no FK between the two tables, and the
|
||||
@@ -392,13 +342,8 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn bulk_import(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
rows: &[ImportPersona],
|
||||
) -> Result<usize, DbError> {
|
||||
trace_db_call(cx, "insert", "bulk_import_personas", |_span| {
|
||||
fn bulk_import(&mut self, uid: i32, rows: &[ImportPersona]) -> Result<usize, DbError> {
|
||||
trace_db_call("insert", "bulk_import_personas", |_span| {
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
let now = chrono::Utc::now().timestamp_millis();
|
||||
let mut inserted = 0usize;
|
||||
@@ -428,13 +373,8 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
.map_err(|e| DbError::log(DbErrorKind::InsertError, e))
|
||||
}
|
||||
|
||||
fn get_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
cid: &str,
|
||||
) -> Result<Option<PersonaChatRow>, DbError> {
|
||||
trace_db_call(cx, "query", "get_persona_chat", |_span| {
|
||||
fn get_persona_chat(&mut self, uid: i32, cid: &str) -> Result<Option<PersonaChatRow>, DbError> {
|
||||
trace_db_call("query", "get_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
persona_chat_conversations
|
||||
@@ -459,12 +399,8 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn list_persona_chats(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
) -> Result<Vec<PersonaChatRow>, DbError> {
|
||||
trace_db_call(cx, "query", "list_persona_chats", |_span| {
|
||||
fn list_persona_chats(&mut self, uid: i32) -> Result<Vec<PersonaChatRow>, DbError> {
|
||||
trace_db_call("query", "list_persona_chats", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
persona_chat_conversations
|
||||
@@ -488,12 +424,11 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
|
||||
fn create_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
created: i64,
|
||||
) -> Result<String, DbError> {
|
||||
trace_db_call(cx, "insert", "create_persona_chat", |_span| {
|
||||
trace_db_call("insert", "create_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
let new_id = uuid::Uuid::new_v4().to_string();
|
||||
@@ -519,14 +454,13 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
|
||||
fn update_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
cid: &str,
|
||||
json: &str,
|
||||
count: i32,
|
||||
updated: i64,
|
||||
) -> Result<usize, DbError> {
|
||||
trace_db_call(cx, "update", "update_persona_chat", |_span| {
|
||||
trace_db_call("update", "update_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
diesel::update(
|
||||
@@ -547,12 +481,11 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
|
||||
fn set_persona_chat_title(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
cid: &str,
|
||||
new_title: &str,
|
||||
) -> Result<(), DbError> {
|
||||
trace_db_call(cx, "update", "set_persona_chat_title", |_span| {
|
||||
trace_db_call("update", "set_persona_chat_title", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
diesel::update(
|
||||
@@ -568,13 +501,8 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn delete_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
cid: &str,
|
||||
) -> Result<(), DbError> {
|
||||
trace_db_call(cx, "delete", "delete_persona_chat", |_span| {
|
||||
fn delete_persona_chat(&mut self, uid: i32, cid: &str) -> Result<(), DbError> {
|
||||
trace_db_call("delete", "delete_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
diesel::delete(
|
||||
@@ -611,31 +539,29 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn create_and_list_round_trip() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("alice");
|
||||
|
||||
// The migration seeds 3 built-ins for any existing user; alice
|
||||
// was created post-migration so she starts empty.
|
||||
let p = dao
|
||||
.create_persona(&cx, uid, "custom-1", "Custom A", "prompt A", false, false)
|
||||
.create_persona(uid, "custom-1", "Custom A", "prompt A", false, false)
|
||||
.unwrap();
|
||||
assert_eq!(p.persona_id, "custom-1");
|
||||
assert_eq!(p.user_id, uid);
|
||||
assert!(!p.is_built_in);
|
||||
|
||||
let list = dao.list_personas(&cx, uid).unwrap();
|
||||
let list = dao.list_personas(uid).unwrap();
|
||||
assert_eq!(list.len(), 1);
|
||||
assert_eq!(list[0].persona_id, "custom-1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_constraint_blocks_duplicate_persona_id() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("bob");
|
||||
|
||||
dao.create_persona(&cx, uid, "x", "X", "p", false, false)
|
||||
dao.create_persona(uid, "x", "X", "p", false, false)
|
||||
.unwrap();
|
||||
let err = dao.create_persona(&cx, uid, "x", "X2", "p2", false, false);
|
||||
let err = dao.create_persona(uid, "x", "X2", "p2", false, false);
|
||||
assert!(
|
||||
err.is_err(),
|
||||
"second insert with same persona_id should fail"
|
||||
@@ -644,7 +570,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn bulk_import_is_idempotent() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("carol");
|
||||
|
||||
let rows = vec![
|
||||
@@ -664,12 +589,12 @@ mod tests {
|
||||
},
|
||||
];
|
||||
|
||||
let first = dao.bulk_import(&cx, uid, &rows).unwrap();
|
||||
let first = dao.bulk_import(uid, &rows).unwrap();
|
||||
assert_eq!(first, 2);
|
||||
let second = dao.bulk_import(&cx, uid, &rows).unwrap();
|
||||
let second = dao.bulk_import(uid, &rows).unwrap();
|
||||
assert_eq!(second, 0, "re-import should insert nothing");
|
||||
|
||||
assert_eq!(dao.list_personas(&cx, uid).unwrap().len(), 2);
|
||||
assert_eq!(dao.list_personas(uid).unwrap().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -683,14 +608,12 @@ mod tests {
|
||||
// depth — but keep this test passing so anyone who removes
|
||||
// the handler guard gets a failing call site, not silent data
|
||||
// corruption.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("eve");
|
||||
|
||||
dao.create_persona(&cx, uid, "default", "Default", "old", true, false)
|
||||
dao.create_persona(uid, "default", "Default", "old", true, false)
|
||||
.unwrap();
|
||||
let updated = dao
|
||||
.update_persona(
|
||||
&cx,
|
||||
uid,
|
||||
"default",
|
||||
PersonaPatch {
|
||||
@@ -713,14 +636,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn update_toggles_include_all_memories() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("dan");
|
||||
|
||||
dao.create_persona(&cx, uid, "j", "Journal", "p", true, false)
|
||||
dao.create_persona(uid, "j", "Journal", "p", true, false)
|
||||
.unwrap();
|
||||
let updated = dao
|
||||
.update_persona(
|
||||
&cx,
|
||||
uid,
|
||||
"j",
|
||||
PersonaPatch {
|
||||
@@ -755,22 +676,16 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn persona_chat_get_returns_none_for_an_unknown_conversation() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p1");
|
||||
assert!(
|
||||
dao.get_persona_chat(&cx, uid, "no-such-id")
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
assert!(dao.get_persona_chat(uid, "no-such-id").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_create_then_get_round_trip() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p2");
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let cid = dao.create_persona_chat(uid, "journal", 100).unwrap();
|
||||
|
||||
let row = dao.get_persona_chat(&cx, uid, &cid).unwrap().unwrap();
|
||||
let row = dao.get_persona_chat(uid, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.conversation_id, cid);
|
||||
assert_eq!(row.persona_id, "journal");
|
||||
assert_eq!(row.title, "", "unnamed until the first turn completes");
|
||||
@@ -783,31 +698,27 @@ mod tests {
|
||||
fn persona_chat_create_makes_a_separate_row_each_time() {
|
||||
// The whole point of the conversation_id key: one persona, several
|
||||
// independent threads.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p3");
|
||||
let first = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let second = dao.create_persona_chat(&cx, uid, "journal", 200).unwrap();
|
||||
let first = dao.create_persona_chat(uid, "journal", 100).unwrap();
|
||||
let second = dao.create_persona_chat(uid, "journal", 200).unwrap();
|
||||
|
||||
assert_ne!(first, second);
|
||||
assert_eq!(dao.list_persona_chats(&cx, uid).unwrap().len(), 2);
|
||||
assert_eq!(dao.list_persona_chats(uid).unwrap().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_update_replaces_the_transcript() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p4");
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let cid = dao.create_persona_chat(uid, "journal", 100).unwrap();
|
||||
|
||||
let rows = dao
|
||||
.update_persona_chat(&cx, uid, &cid, "first", 1, 150)
|
||||
.unwrap();
|
||||
let rows = dao.update_persona_chat(uid, &cid, "first", 1, 150).unwrap();
|
||||
assert_eq!(rows, 1);
|
||||
let rows = dao
|
||||
.update_persona_chat(&cx, uid, &cid, "second", 2, 200)
|
||||
.update_persona_chat(uid, &cid, "second", 2, 200)
|
||||
.unwrap();
|
||||
assert_eq!(rows, 1);
|
||||
|
||||
let row = dao.get_persona_chat(&cx, uid, &cid).unwrap().unwrap();
|
||||
let row = dao.get_persona_chat(uid, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.messages_json, "second");
|
||||
assert_eq!(row.turn_count, 2);
|
||||
assert_eq!(row.updated_at, 200);
|
||||
@@ -818,36 +729,32 @@ mod tests {
|
||||
fn persona_chat_update_reports_zero_rows_for_an_unknown_conversation() {
|
||||
// The turn loop treats 0 as "the conversation went away mid-turn"
|
||||
// rather than silently succeeding.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p5");
|
||||
let rows = dao
|
||||
.update_persona_chat(&cx, uid, "no-such-id", "x", 1, 1)
|
||||
.update_persona_chat(uid, "no-such-id", "x", 1, 1)
|
||||
.unwrap();
|
||||
assert_eq!(rows, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_set_title_names_the_conversation() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p6");
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let cid = dao.create_persona_chat(uid, "journal", 100).unwrap();
|
||||
|
||||
dao.set_persona_chat_title(&cx, uid, &cid, "June recap")
|
||||
.unwrap();
|
||||
let row = dao.get_persona_chat(&cx, uid, &cid).unwrap().unwrap();
|
||||
dao.set_persona_chat_title(uid, &cid, "June recap").unwrap();
|
||||
let row = dao.get_persona_chat(uid, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.title, "June recap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_list_orders_newest_first() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p7");
|
||||
let older = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let newer = dao.create_persona_chat(&cx, uid, "coach", 300).unwrap();
|
||||
let middle = dao.create_persona_chat(&cx, uid, "default", 200).unwrap();
|
||||
let older = dao.create_persona_chat(uid, "journal", 100).unwrap();
|
||||
let newer = dao.create_persona_chat(uid, "coach", 300).unwrap();
|
||||
let middle = dao.create_persona_chat(uid, "default", 200).unwrap();
|
||||
|
||||
let ids: Vec<String> = dao
|
||||
.list_persona_chats(&cx, uid)
|
||||
.list_persona_chats(uid)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|r| r.conversation_id)
|
||||
@@ -861,20 +768,18 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn persona_chat_list_is_empty_for_a_user_who_has_never_chatted() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p8");
|
||||
assert!(dao.list_persona_chats(&cx, uid).unwrap().is_empty());
|
||||
assert!(dao.list_persona_chats(uid).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_delete_removes_the_conversation() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p9");
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let cid = dao.create_persona_chat(uid, "journal", 100).unwrap();
|
||||
|
||||
dao.delete_persona_chat(&cx, uid, &cid).unwrap();
|
||||
assert!(dao.get_persona_chat(&cx, uid, &cid).unwrap().is_none());
|
||||
assert!(dao.list_persona_chats(&cx, uid).unwrap().is_empty());
|
||||
dao.delete_persona_chat(uid, &cid).unwrap();
|
||||
assert!(dao.get_persona_chat(uid, &cid).unwrap().is_none());
|
||||
assert!(dao.list_persona_chats(uid).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -882,18 +787,17 @@ mod tests {
|
||||
// No FK between the tables, and the chat list hides conversations
|
||||
// whose persona is gone — so an orphan row would be an invisible,
|
||||
// unreachable transcript sitting in the database forever.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p-cascade");
|
||||
dao.create_persona(&cx, uid, "custom-1", "Custom", "prompt", false, false)
|
||||
dao.create_persona(uid, "custom-1", "Custom", "prompt", false, false)
|
||||
.unwrap();
|
||||
dao.create_persona_chat(&cx, uid, "custom-1", 100).unwrap();
|
||||
dao.create_persona_chat(&cx, uid, "custom-1", 200).unwrap();
|
||||
let survivor = dao.create_persona_chat(&cx, uid, "other", 300).unwrap();
|
||||
dao.create_persona_chat(uid, "custom-1", 100).unwrap();
|
||||
dao.create_persona_chat(uid, "custom-1", 200).unwrap();
|
||||
let survivor = dao.create_persona_chat(uid, "other", 300).unwrap();
|
||||
|
||||
assert!(dao.delete_persona(&cx, uid, "custom-1").unwrap());
|
||||
assert!(dao.delete_persona(uid, "custom-1").unwrap());
|
||||
|
||||
let remaining: Vec<String> = dao
|
||||
.list_persona_chats(&cx, uid)
|
||||
.list_persona_chats(uid)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|r| r.conversation_id)
|
||||
@@ -909,27 +813,26 @@ mod tests {
|
||||
fn persona_chat_reads_and_writes_are_scoped_to_the_owner() {
|
||||
// A conversation_id is a bearer token for someone's transcript, so
|
||||
// holding one must not grant another user access to it.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid1) = dao_with_user("owner");
|
||||
let uid2 = second_user(&dao, "intruder");
|
||||
let cid = dao.create_persona_chat(&cx, uid1, "journal", 100).unwrap();
|
||||
dao.update_persona_chat(&cx, uid1, &cid, "private", 1, 100)
|
||||
let cid = dao.create_persona_chat(uid1, "journal", 100).unwrap();
|
||||
dao.update_persona_chat(uid1, &cid, "private", 1, 100)
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
dao.get_persona_chat(&cx, uid2, &cid).unwrap().is_none(),
|
||||
dao.get_persona_chat(uid2, &cid).unwrap().is_none(),
|
||||
"another user cannot read it"
|
||||
);
|
||||
assert_eq!(
|
||||
dao.update_persona_chat(&cx, uid2, &cid, "tampered", 9, 999)
|
||||
dao.update_persona_chat(uid2, &cid, "tampered", 9, 999)
|
||||
.unwrap(),
|
||||
0,
|
||||
"another user cannot write it"
|
||||
);
|
||||
dao.delete_persona_chat(&cx, uid2, &cid).unwrap();
|
||||
dao.delete_persona_chat(uid2, &cid).unwrap();
|
||||
|
||||
let row = dao.get_persona_chat(&cx, uid1, &cid).unwrap().unwrap();
|
||||
let row = dao.get_persona_chat(uid1, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.messages_json, "private", "and cannot delete it");
|
||||
assert!(dao.list_persona_chats(&cx, uid2).unwrap().is_empty());
|
||||
assert!(dao.list_persona_chats(uid2).unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user