The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(thread): cover thread edit operations
Steven Enamakel2ee58da30369
diff --git a/crates/openhuman-core/src/threads/ops/edit_tests.rs b/crates/openhuman-core/src/threads/ops/edit_tests.rsindex c48fadda99..5bba8fc06c 100644--- a/crates/openhuman-core/src/threads/ops/edit_tests.rs+++ b/crates/openhuman-core/src/threads/ops/edit_tests.rs@@ -1,25 +1,30 @@//! Transcript-level tests for `threads::ops::edit`'s private truncation//! helpers (`truncate_transcript_before_turn`, `truncate_transcript_for_regenerate`).//!-//! Every fixture is written with the session crate's own writer-//! (`append_transcript_turn`), never hand-rolled JSONL, so a wire-format-//! change upstream surfaces here as a failure instead of silently diverging-//! from what the core actually persists. See `edit_turn_state_tests.rs` for-//! `clear_dropped_turn_states` / `next_reply_request_id_after`, which need-//! different fixtures (turn-state snapshots, the message-log store) and are-//! split out to keep both files well under the layout line limit.+//! Every fixture is written through the transcript port the session host+//! writes through (`TranscriptStore::locator` -> `open_session` ++//! `append_turn`), never through a file path, so the helpers are exercised+//! against exactly the store and key a real turn lands in. See+//! `edit_turn_state_tests.rs` for `clear_dropped_turn_states` /+//! `next_reply_request_id_after`, which need different fixtures (turn-state+//! snapshots, the message-log store).use super::*;-use std::path::{Path, PathBuf};+use std::path::Path;use tempfile::TempDir;-use tinyagents_session::transcript::{- append_transcript_turn, resolve_keyed_transcript_path, session_stem, MessageUsage, TurnUsage,-};+use tinyagents_session::transcript::{MessageUsage, TranscriptTurn, TurnUsage};const AGENT_ID: &str = "test-agent";-/// The store key the fixtures' legacy JSONL is imported under.+/// The store key a desktop turn of the default chat agent is written under.const STORE_KEY: &str = "orchestrator";+fn store(workspace: &Path) -> TranscriptStore {+ TranscriptStore {+ workspace_dir: workspace.to_path_buf(),+ key: STORE_KEY.to_string(),+ }+}+fn base_meta(agent_id: &str, thread_id: &str) -> TranscriptMeta {TranscriptMeta {agent_name: agent_id.to_string(),@@ -62,16 +67,17 @@ fn turn_usage() -> TurnUsage {}}-/// Write a two-turn root transcript for `thread_id`/`AGENT_ID` using the same-/// writer the runtime uses (`append_transcript_turn`), and return the file-/// path plus the two turns' `request_id`s.-fn write_two_turn_transcript(workspace: &Path, thread_id: &str) -> (PathBuf, String, String) {+/// Write a two-turn root transcript for `thread_id`/`AGENT_ID` into `store`+/// the way the session host does, and return the two turns' `request_id`s.+fn write_two_turn_transcript(store: &TranscriptStore, thread_id: &str) -> (String, String) {let session = SessionRef::scoped(thread_id, AGENT_ID);- let path = resolve_keyed_transcript_path(workspace, &session_stem(&session))- .expect("resolve fixture transcript path");+ let meta = base_meta(AGENT_ID, thread_id);+ let history = store+ .locator()+ .open_session(&session, meta.clone())+ .expect("open fixture session");let req1 = "turn-1".to_string();let req2 = "turn-2".to_string();- let meta = base_meta(AGENT_ID, thread_id);let turn1 = vec![TranscriptMessage::new("user", "user prompt 1"),@@ -79,66 +85,82 @@ fn write_two_turn_transcript(workspace: &Path, thread_id: &str) -> (PathBuf, Str];let mut turn1_meta = meta.clone();turn1_meta.turn_count = 1;- append_transcript_turn(- &path,- &[],- &turn1,- &turn1_meta,- Some(&turn_usage()),- Some(&req1),- )- .expect("append turn 1");+ history+ .append_turn(TranscriptTurn {+ prev: &[],+ next: &turn1,+ meta: &turn1_meta,+ turn_usage: Some(&turn_usage()),+ request_id: Some(&req1),+ tools: None,+ })+ .expect("append turn 1");let mut turn2 = turn1.clone();turn2.push(TranscriptMessage::new("user", "user prompt 2"));turn2.push(TranscriptMessage::assistant("answer 2"));- let mut turn2_meta = meta.clone();+ let mut turn2_meta = meta;turn2_meta.turn_count = 2;- append_transcript_turn(- &path,- &turn1,- &turn2,- &turn2_meta,- Some(&turn_usage()),- Some(&req2),- )- .expect("append turn 2");-- (path, req1, req2)+ history+ .append_turn(TranscriptTurn {+ prev: &turn1,+ next: &turn2,+ meta: &turn2_meta,+ turn_usage: Some(&turn_usage()),+ request_id: Some(&req2),+ tools: None,+ })+ .expect("append turn 2");++ (req1, req2)}/// A root transcript with a single system row and no user/assistant turn at/// all — feeds the "nothing to regenerate" `Ok(None)` case.-fn write_turnless_transcript(workspace: &Path, thread_id: &str) -> PathBuf {+fn write_turnless_transcript(store: &TranscriptStore, thread_id: &str) {let session = SessionRef::scoped(thread_id, AGENT_ID);- let path = resolve_keyed_transcript_path(workspace, &session_stem(&session))- .expect("resolve fixture transcript path");let meta = base_meta(AGENT_ID, thread_id);let messages = vec![TranscriptMessage::new("system", "boot preamble")];- append_transcript_turn(&path, &[], &messages, &meta, None, None).expect("append system row");- path+ store+ .locator()+ .open_session(&session, meta.clone())+ .expect("open fixture session")+ .append_turn(TranscriptTurn {+ prev: &[],+ next: &messages,+ meta: &meta,+ turn_usage: None,+ request_id: None,+ tools: None,+ })+ .expect("append system row");+}++/// The first (sealed) generation of the thread's session, read back through+/// the store: a fork never erases it.+fn sealed_generation(store: &TranscriptStore, thread_id: &str) -> Vec<TranscriptMessage> {+ store+ .locator()+ .read_session_transcript(&SessionRef::scoped(thread_id, AGENT_ID))+ .expect("sealed generation stays in the store")+ .read_session()+ .expect("read sealed")+ .expect("sealed transcript exists")+ .messages}#[test]-fn truncate_transcript_before_turn_cuts_head_and_seals_original_untouched() {+fn truncate_transcript_before_turn_cuts_head_and_keeps_the_sealed_generation() {let dir = TempDir::new().expect("tempdir");+ let store = store(dir.path());let thread_id = "thread-before-turn";- let (path, req1, req2) = write_two_turn_transcript(dir.path(), thread_id);+ let (req1, req2) = write_two_turn_transcript(&store, thread_id);- let original_bytes = std::fs::read(&path).expect("read original transcript");-- truncate_transcript_before_turn(dir.path(), STORE_KEY, thread_id, &req2).expect("truncate");-- // Sealed generation preserved byte-for-byte: "a compaction never- // erases" applies identically to this edit-driven fork.- let bytes_after = std::fs::read(&path).expect("re-read original transcript");- assert_eq!(- original_bytes, bytes_after,- "sealed generation must stay byte-for-byte untouched"- );+ truncate_transcript_before_turn(&store, thread_id, &req2).expect("truncate");// Head truncated: the new head ends right before turn 2's first row.- let (_, _, head) = resolve_head_transcript(dir.path(), STORE_KEY, thread_id).expect("resolve head");+ let (head_ref, _, head) = resolve_head_transcript(&store, thread_id).expect("resolve head");+ assert_eq!(head_ref.generation, 1, "the fork opened the next generation");assert!(head.messages.iter()@@ -147,33 +169,27 @@ fn truncate_transcript_before_turn_cuts_head_and_seals_original_untouched() {head.messages);let last = head.messages.last().expect("head keeps turn 1");- // The sealed generation is still readable through the store port, with- // both turns, even though the model now reads only the new head.- let locator = crate::agent::session_store::transcripts_or_files(STORE_KEY, dir.path());- let sealed = locator- .read_session_transcript(&SessionRef::scoped(thread_id, AGENT_ID))- .expect("sealed generation stays in the store")- .read_session()- .expect("read sealed")- .expect("sealed transcript exists");+ assert_eq!(last.request_id.as_deref(), Some(req1.as_str()));+ assert_eq!(last.content, "answer 1", "turn 1's answer is kept intact");++ // "A compaction never erases" applies identically to this edit-driven+ // fork: the sealed generation still holds both turns.assert!(- sealed- .messages+ sealed_generation(&store, thread_id).iter().any(|m| m.request_id.as_deref() == Some(req2.as_str())),"the sealed generation keeps the dropped turn");- assert_eq!(last.request_id.as_deref(), Some(req1.as_str()));- assert_eq!(last.content, "answer 1", "turn 1's answer is kept intact");}#[test]fn truncate_transcript_before_turn_errs_when_turn_not_found() {let dir = TempDir::new().expect("tempdir");+ let store = store(dir.path());let thread_id = "thread-missing-turn";- write_two_turn_transcript(dir.path(), thread_id);+ write_two_turn_transcript(&store, thread_id);- let err = truncate_transcript_before_turn(dir.path(), STORE_KEY, thread_id, "no-such-turn")+ let err = truncate_transcript_before_turn(&store, thread_id, "no-such-turn").expect_err("unknown request_id must fail");assert!(err.contains("no-such-turn"),@@ -181,27 +197,28 @@ fn truncate_transcript_before_turn_errs_when_turn_not_found() {);}+#[test]+fn truncate_transcript_before_turn_errs_when_thread_has_no_transcript() {+ let dir = TempDir::new().expect("tempdir");+ let err = truncate_transcript_before_turn(&store(dir.path()), "thread-none", "turn-1")+ .expect_err("a thread with no transcript has nothing to cut");+ assert!(err.contains("has no session transcript"), "{err}");+}+#[test]fn truncate_transcript_for_regenerate_targets_specific_turn() {let dir = TempDir::new().expect("tempdir");+ let store = store(dir.path());let thread_id = "thread-regen-target";- let (path, req1, req2) = write_two_turn_transcript(dir.path(), thread_id);- let original_bytes = std::fs::read(&path).expect("read original transcript");+ let (_req1, req2) = write_two_turn_transcript(&store, thread_id);- let (prompt, request_id) =- truncate_transcript_for_regenerate(dir.path(), STORE_KEY, thread_id, Some(&req2))- .expect("truncate")- .expect("a turn to regenerate");+ let (prompt, request_id) = truncate_transcript_for_regenerate(&store, thread_id, Some(&req2))+ .expect("truncate")+ .expect("a turn to regenerate");assert_eq!(prompt, "user prompt 2");assert_eq!(request_id, req2);- let bytes_after = std::fs::read(&path).expect("re-read original transcript");- assert_eq!(- original_bytes, bytes_after,- "sealed generation must stay byte-for-byte untouched"- );-- let (_, _, head) = resolve_head_transcript(dir.path(), STORE_KEY, thread_id).expect("resolve head");+ let (_, _, head) = resolve_head_transcript(&store, thread_id).expect("resolve head");let last = head.messages.last().expect("head keeps turn 2's prompt");assert_eq!(last.role, "user");assert_eq!(last.content, "user prompt 2");@@ -210,16 +227,22 @@ fn truncate_transcript_for_regenerate_targets_specific_turn() {"turn 2's answer must be dropped: {:?}",head.messages);- let _ = req1;+ assert!(+ sealed_generation(&store, thread_id)+ .iter()+ .any(|m| m.content == "answer 2"),+ "the sealed generation keeps the dropped answer"+ );}#[test]fn truncate_transcript_for_regenerate_none_resolves_last_turn() {let dir = TempDir::new().expect("tempdir");+ let store = store(dir.path());let thread_id = "thread-regen-last";- let (_, _req1, req2) = write_two_turn_transcript(dir.path(), thread_id);+ let (_req1, req2) = write_two_turn_transcript(&store, thread_id);- let (prompt, request_id) = truncate_transcript_for_regenerate(dir.path(), STORE_KEY, thread_id, None)+ let (prompt, request_id) = truncate_transcript_for_regenerate(&store, thread_id, None).expect("truncate").expect("a turn to regenerate");@@ -230,10 +253,11 @@ fn truncate_transcript_for_regenerate_none_resolves_last_turn() {#[test]fn truncate_transcript_for_regenerate_returns_none_when_no_turn_exists() {let dir = TempDir::new().expect("tempdir");+ let store = store(dir.path());let thread_id = "thread-no-turns";- write_turnless_transcript(dir.path(), thread_id);+ write_turnless_transcript(&store, thread_id);- let result = truncate_transcript_for_regenerate(dir.path(), STORE_KEY, thread_id, None).expect("truncate");+ let result = truncate_transcript_for_regenerate(&store, thread_id, None).expect("truncate");assert_eq!(result, None, "no user/assistant turn to regenerate");}@@ -244,7 +268,80 @@ fn truncate_transcript_for_regenerate_returns_none_when_no_turn_exists() {fn truncate_transcript_for_regenerate_returns_none_when_thread_has_no_transcript() {let dir = TempDir::new().expect("tempdir");- let result = truncate_transcript_for_regenerate(dir.path(), STORE_KEY, "thread-without-transcript", None)- .expect("a thread with no transcript has no turn to regenerate, not an error");+ let result =+ truncate_transcript_for_regenerate(&store(dir.path()), "thread-without-transcript", None)+ .expect("a thread with no transcript has no turn to regenerate, not an error");assert_eq!(result, None);}++/// A SaaS profile's turns are written under `<profile>~<agent>`. The edit+/// path resolves its key from the request's tenant (not the config alone),+/// so it finds and forks that same transcript, and the unprefixed key of the+/// same workspace never sees it.+#[test]+fn a_profile_prefixed_key_round_trips_through_edit_and_regenerate() {+ let dir = TempDir::new().expect("tempdir");+ let mut config = crate::config::Config::default();+ config.workspace_dir = dir.path().to_path_buf();+ let tenant = crate::core::runtime::Tenant {+ profile: Some("profile-a".to_string()),+ agent: None,+ };++ // What the session host writes under: the ambient-tenant resolution, fed+ // the same tenant the request carries.+ let writer_key = crate::agent::session_store::agent_key_for(+ &tenant,+ &crate::web_chat::pick_target_agent_id(&config),+ );+ let store = transcript_store_for(&config, &tenant);+ assert_eq!(store.key, writer_key, "the reader resolves the writer's key");+ assert_eq!(store.key, "profile-a~default", "the key keeps the profile prefix");+ assert_eq!(store.workspace_dir, dir.path());++ let thread_id = "thread-profile";+ let (req1, req2) = write_two_turn_transcript(&store, thread_id);++ let unprefixed = TranscriptStore {+ workspace_dir: dir.path().to_path_buf(),+ key: crate::web_chat::pick_target_agent_id(&config),+ };+ assert_eq!(+ truncate_transcript_for_regenerate(&unprefixed, thread_id, None).expect("lookup"),+ None,+ "the config-only key does not see the profile's transcript"+ );++ truncate_transcript_before_turn(&store, thread_id, &req2).expect("edit forks the transcript");+ let (_, _, head) = resolve_head_transcript(&store, thread_id).expect("resolve head");+ assert_eq!(+ head.messages.last().and_then(|m| m.request_id.as_deref()),+ Some(req1.as_str()),+ "the edit cut the profile's own transcript"+ );++ let (prompt, request_id) = truncate_transcript_for_regenerate(&store, thread_id, None)+ .expect("regenerate")+ .expect("turn 1 is left to regenerate");+ assert_eq!(prompt, "user prompt 1");+ assert_eq!(request_id, req1);+}++#[test]+fn transcript_store_for_a_desktop_tenant_uses_the_chat_agent_and_an_embedded_agent_its_own() {+ let dir = TempDir::new().expect("tempdir");+ let mut config = crate::config::Config::default();+ config.workspace_dir = dir.path().to_path_buf();++ let desktop = transcript_store_for(&config, &crate::core::runtime::Tenant::default());+ assert_eq!(desktop.key, crate::web_chat::pick_target_agent_id(&config));++ let embedded = transcript_store_for(+ &config,+ &crate::core::runtime::Tenant {+ profile: None,+ agent: Some("agent-7".to_string()),+ },+ );+ assert_eq!(embedded.key, "agent-7");+}