Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(session_store): move agent transcript files into tinyagents-session

Steven Enamakelb2028d54052f
diff --git a/crates/openhuman-core/src/agent/session_store/agent_transcripts.rs b/crates/openhuman-core/src/agent/session_store/agent_transcripts.rs
deleted file mode 100644
index af226e7e1d..0000000000
--- a/crates/openhuman-core/src/agent/session_store/agent_transcripts.rs
+++ /dev/null
@@ -1,189 +0,0 @@
-//! Transcript files of one embedded agent.
-//!
-//! An agent derived on a shared workspace keeps its transcripts under
-//! `<workspace>/agents/<id>/session_raw/`. Conversations it wrote before that
-//! layout, in `<workspace>/session_raw/`, stay readable: a lookup that misses
-//! the agent's own directory falls back to the shared one, and the first time
-//! such a session is opened for writing its file is copied into the agent's
-//! directory. The shared file is never modified.
-
-use std::path::{Path, PathBuf};
-use std::sync::Arc;
-
-use tinyagents_session::transcript::{
- find_root_transcript_for_thread_scoped, resolve_keyed_transcript_path, session_stem,
- FileTranscriptLocator, SessionAdoption, SessionRef, TranscriptHistory, TranscriptLocator,
- TranscriptMessage, TranscriptMeta, TranscriptPartial, TranscriptRead,
-};
-
-/// The workspace root an agent's transcripts are written under.
-#[must_use]
-pub fn agent_transcript_root(workspace_dir: &Path, agent_id: &str) -> PathBuf {
- workspace_dir.join("agents").join(agent_id)
-}
-
-/// The agent's own transcript files, with a read-only fallback to the shared
-/// workspace's.
-pub struct AgentTranscriptFiles {
- own: FileTranscriptLocator,
- own_root: PathBuf,
- legacy: FileTranscriptLocator,
- legacy_root: PathBuf,
-}
-
-impl AgentTranscriptFiles {
- /// `agent_id`'s transcripts on `workspace_dir`.
- #[must_use]
- pub fn new(workspace_dir: &Path, agent_id: &str) -> Self {
- let own_root = agent_transcript_root(workspace_dir, agent_id);
- Self {
- own: FileTranscriptLocator::new(own_root.clone()),
- own_root,
- legacy: FileTranscriptLocator::new(workspace_dir.to_path_buf()),
- legacy_root: workspace_dir.to_path_buf(),
- }
- }
-
- /// Copies `stem` from the shared directory into the agent's when only
- /// the shared one has it.
- fn adopt_stem(&self, stem: &str) {
- let Ok(own) = resolve_keyed_transcript_path(&self.own_root, stem) else {
- return;
- };
- if own.exists() {
- return;
- }
- let legacy = self
- .legacy_root
- .join("session_raw")
- .join(own.file_name().unwrap_or_default());
- if legacy.is_file() {
- self.copy_in(&legacy, &own);
- }
- }
-
- /// Copies the shared root transcript of `thread_id` into the agent's
- /// directory when the agent has none of its own.
- fn adopt_thread_root(&self, thread_id: &str, agent_id: Option<&str>) {
- if find_root_transcript_for_thread_scoped(&self.own_root, thread_id, agent_id).is_some() {
- return;
- }
- let Some(legacy) =
- find_root_transcript_for_thread_scoped(&self.legacy_root, thread_id, agent_id)
- else {
- return;
- };
- let Some(name) = legacy.file_name() else {
- return;
- };
- let own_dir = self.own_root.join("session_raw");
- if std::fs::create_dir_all(&own_dir).is_ok() {
- self.copy_in(&legacy, &own_dir.join(name));
- }
- }
-
- fn copy_in(&self, legacy: &Path, own: &Path) {
- match std::fs::copy(legacy, own) {
- Ok(_) => log::debug!(
- "[session_store] adopted shared transcript into the agent's directory file={}",
- own.file_name().unwrap_or_default().to_string_lossy()
- ),
- Err(error) => log::warn!(
- "[session_store] could not adopt shared transcript file={} error={error}",
- own.file_name().unwrap_or_default().to_string_lossy()
- ),
- }
- }
-}
-
-impl TranscriptLocator for AgentTranscriptFiles {
- fn destination_key(&self) -> Option<String> {
- self.own.destination_key()
- }
-
- fn latest_for_agent(&self, agent_name: &str) -> Option<Arc<dyn TranscriptRead>> {
- self.own
- .latest_for_agent(agent_name)
- .or_else(|| self.legacy.latest_for_agent(agent_name))
- }
-
- fn root_for_thread(&self, thread_id: &str) -> Option<Arc<dyn TranscriptRead>> {
- self.own
- .root_for_thread(thread_id)
- .or_else(|| self.legacy.root_for_thread(thread_id))
- }
-
- fn root_for_thread_scoped(
- &self,
- thread_id: &str,
- agent_id: Option<&str>,
- ) -> Option<Arc<dyn TranscriptRead>> {
- self.own
- .root_for_thread_scoped(thread_id, agent_id)
- .or_else(|| self.legacy.root_for_thread_scoped(thread_id, agent_id))
- }
-
- fn open_stem(
- &self,
- stem: &str,
- seed: TranscriptMeta,
- ) -> anyhow::Result<Arc<dyn TranscriptHistory>> {
- self.adopt_stem(stem);
- self.own.open_stem(stem, seed)
- }
-
- fn session_exists(&self, session: &SessionRef) -> bool {
- self.adopt_stem(&session_stem(session));
- self.own.session_exists(session)
- }
-
- fn read_session_transcript(&self, session: &SessionRef) -> Option<Arc<dyn TranscriptRead>> {
- self.adopt_stem(&session_stem(session));
- self.own.read_session_transcript(session)
- }
-
- fn adopt_legacy(
- &self,
- session: &SessionRef,
- thread_id: &str,
- seed: &TranscriptMeta,
- ) -> anyhow::Result<Option<SessionAdoption>> {
- self.own.adopt_legacy(session, thread_id, seed)
- }
-
- fn append_interrupted_partial(
- &self,
- thread_id: &str,
- agent_id: Option<&str>,
- partial: &TranscriptPartial,
- request_id: Option<&str>,
- ) -> anyhow::Result<bool> {
- self.adopt_thread_root(thread_id, agent_id);
- self.own
- .append_interrupted_partial(thread_id, agent_id, partial, request_id)
- }
-
- fn begin_generation(
- &self,
- session: &SessionRef,
- seed: TranscriptMeta,
- ) -> anyhow::Result<(SessionRef, Arc<dyn TranscriptHistory>)> {
- self.adopt_stem(&session_stem(session));
- self.own.begin_generation(session, seed)
- }
-
- fn begin_generation_from_baseline(
- &self,
- session: &SessionRef,
- seed: TranscriptMeta,
- baseline: &[TranscriptMessage],
- ) -> anyhow::Result<(SessionRef, Arc<dyn TranscriptHistory>)> {
- self.adopt_stem(&session_stem(session));
- self.own
- .begin_generation_from_baseline(session, seed, baseline)
- }
-}
-
-#[cfg(test)]
-#[path = "agent_transcripts_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/agent/session_store/agent_transcripts_tests.rs b/crates/openhuman-core/src/agent/session_store/agent_transcripts_tests.rs
deleted file mode 100644
index d01f009b8c..0000000000
--- a/crates/openhuman-core/src/agent/session_store/agent_transcripts_tests.rs
+++ /dev/null
@@ -1,114 +0,0 @@
-use super::*;
-use tinyagents_session::transcript::TranscriptMessage;
-
-fn meta() -> TranscriptMeta {
- TranscriptMeta {
- session_id: None,
- parent_session_id: None,
- agent_name: "alpha".into(),
- agent_id: Some("alpha".into()),
- agent_type: None,
- dispatcher: "native".into(),
- provider: None,
- model: None,
- created: "2026-10-01T00:00:00Z".into(),
- updated: "2026-10-01T00:00:00Z".into(),
- turn_count: 1,
- prefix_message_count: None,
- input_tokens: 0,
- output_tokens: 0,
- cached_input_tokens: 0,
- charged_amount_usd: 0.0,
- thread_id: Some("thread-1".into()),
- task_id: None,
- }
-}
-
-fn messages(session: &dyn TranscriptRead) -> Vec<String> {
- session
- .read_session()
- .expect("readable")
- .expect("written")
- .messages
- .iter()
- .map(|message| message.content.clone())
- .collect()
-}
-
-#[test]
-fn new_sessions_are_written_under_the_agents_directory() {
- let workspace = tempfile::tempdir().expect("tempdir");
- let files = AgentTranscriptFiles::new(workspace.path(), "alpha");
- let session = SessionRef::scoped("thread-1", "alpha");
-
- files
- .open_session(&session, meta())
- .expect("bind")
- .replace(&[TranscriptMessage::user("hello")])
- .expect("write");
-
- let own = workspace.path().join("agents/alpha/session_raw");
- assert!(std::fs::read_dir(&own).expect("own dir").next().is_some());
- assert!(!workspace
- .path()
- .join("session_raw")
- .join(format!("{}.jsonl", session_stem(&session)))
- .exists());
-}
-
-#[test]
-fn a_shared_session_is_read_then_continued_without_touching_the_shared_file() {
- let workspace = tempfile::tempdir().expect("tempdir");
- let session = SessionRef::scoped("thread-1", "alpha");
- FileTranscriptLocator::new(workspace.path())
- .open_session(&session, meta())
- .expect("bind shared")
- .replace(&[TranscriptMessage::user("before")])
- .expect("write shared");
- let shared_path = workspace
- .path()
- .join("session_raw")
- .join(format!("{}.jsonl", session_stem(&session)));
- let shared_bytes = std::fs::read(&shared_path).expect("shared file");
-
- let files = AgentTranscriptFiles::new(workspace.path(), "alpha");
- let found = files
- .read_session_transcript(&session)
- .expect("the shared session is found");
- assert_eq!(messages(found.as_ref()), ["before"]);
-
- files
- .open_session(&session, meta())
- .expect("bind own")
- .replace(&[
- TranscriptMessage::user("before"),
- TranscriptMessage::user("after"),
- ])
- .expect("continue");
-
- assert_eq!(
- std::fs::read(&shared_path).expect("shared file"),
- shared_bytes
- );
- let continued = files
- .read_session_transcript(&session)
- .expect("own session");
- assert_eq!(messages(continued.as_ref()), ["before", "after"]);
-}
-
-#[test]
-fn another_agent_does_not_adopt_a_session_scoped_to_someone_else() {
- let workspace = tempfile::tempdir().expect("tempdir");
- let alpha_session = SessionRef::scoped("thread-1", "alpha");
- AgentTranscriptFiles::new(workspace.path(), "alpha")
- .open_session(&alpha_session, meta())
- .expect("bind")
- .replace(&[TranscriptMessage::user("alpha only")])
- .expect("write");
-
- let beta = AgentTranscriptFiles::new(workspace.path(), "beta");
- assert!(beta
- .read_session_transcript(&SessionRef::scoped("thread-1", "beta"))
- .is_none());
- assert!(beta.read_session_transcript(&alpha_session).is_none());
-}
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index 4824976caf..72934fcf51 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -16,10 +16,9 @@
use std::sync::{Arc, LazyLock, PoisonError, RwLock};
-pub use agent_transcripts::{agent_transcript_root, AgentTranscriptFiles};
use tinyagents_session::port::{AgentStores, SessionStoreProvider};
+use tinyagents_session::transcript::{agent_transcript_root, AgentTranscriptFiles};
-mod agent_transcripts;
pub mod ledger_sync;
mod rebound;
mod workspace;
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
index 3f9d54a2ab..99b3d17a41 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
@@ -75,7 +75,7 @@ pub(crate) async fn append_to_origin_transcript(
let session = SessionRef::scoped(append.thread_id.to_string(), append.agent_id.to_string());
if let Some(agent) = crate::agent::session_store::current_embedded_agent() {
use tinyagents_session::transcript::TranscriptLocator;
- crate::agent::session_store::AgentTranscriptFiles::new(workspace_dir, &agent)
+ tinyagents_session::transcript::AgentTranscriptFiles::new(workspace_dir, &agent)
.session_exists(&session);
}
let locator =
diff --git a/crates/openhuman-embed/src/agent/layout.rs b/crates/openhuman-embed/src/agent/layout.rs
index 2f03d49949..cdb1f9b4b0 100644
--- a/crates/openhuman-embed/src/agent/layout.rs
+++ b/crates/openhuman-embed/src/agent/layout.rs
@@ -43,7 +43,7 @@ impl AgentLayout {
Self {
home: workspace_dir.join("agents").join(id),
skills: workspace_dir.join("agents").join(id).join("skills"),
- transcripts: openhuman_core::agent::session_store::agent_transcript_root(
+ transcripts: tinyagents_session::transcript::agent_transcript_root(
workspace_dir,
id,
)
diff --git a/crates/openhuman-embed/tests/harness_embed.rs b/crates/openhuman-embed/tests/harness_embed.rs
index 998ad7e79c..4c63f87698 100644
--- a/crates/openhuman-embed/tests/harness_embed.rs
+++ b/crates/openhuman-embed/tests/harness_embed.rs
@@ -212,7 +212,7 @@ fn a_harness_runs_a_turn_against_the_provider_it_was_given() {
// own transcript root under the workspace (the old
// `session_db/` files are gone). A harness session id is its
// thread id.
- let transcripts = openhuman_core::agent::session_store::AgentTranscriptFiles::new(
+ let transcripts = tinyagents_session::transcript::AgentTranscriptFiles::new(
&workspace_dir,
"harness",
);