Skip to content

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

fix(multimodal): prefer blob stash over legacy sidecars for attachments

Steven Enamakel035135b4da06
diff --git a/crates/openhuman-core/src/agent/multimodal.rs b/crates/openhuman-core/src/agent/multimodal.rs
index 4e7ac3eb5d..059e55b587 100644
--- a/crates/openhuman-core/src/agent/multimodal.rs
+++ b/crates/openhuman-core/src/agent/multimodal.rs
@@ -620,12 +620,12 @@ fn fallback_attachments_dir() -> PathBuf {
.unwrap_or_else(|| std::env::temp_dir().join("openhuman-attachments"))
}
-/// `id -> marker target` for the placeholders in `messages`: a legacy sidecar
-/// file in the attachments directory (its path), else the blob stash's bytes as
-/// a `data:` URI. The marker target is carried as a `PathBuf` only because that
-/// is the harness's index type; it is rendered verbatim inside `[IMAGE:…]`.
+/// `id -> marker target` for the placeholders in `messages`: the blob stash's
+/// bytes as a `data:` URI, else (for an id the stash lacks) a legacy sidecar
+/// file in the attachments directory, as its path. The marker target is carried
+/// as a `PathBuf` only because that is the harness's index type; it is rendered
+/// verbatim inside `[IMAGE:…]`.
async fn build_attachment_index(messages: &[TranscriptMessage]) -> HashMap<String, PathBuf> {
- let mut index = legacy_attachment_index();
let mut wanted = Vec::new();
for row in messages {
wanted.extend(placeholder_ids(&row.content));
@@ -635,27 +635,33 @@ async fn build_attachment_index(messages: &[TranscriptMessage]) -> HashMap<Strin
}
}
}
- wanted.retain(|id| !index.contains_key(id));
wanted.sort();
wanted.dedup();
+ let mut index = HashMap::new();
if wanted.is_empty() {
return index;
}
- let Some(stash) = stash() else {
- return index;
- };
- for id in wanted {
- match stash.read_data_uri(&id).await {
- Ok(Some(data_uri)) => {
- index.insert(id, PathBuf::from(data_uri));
+ if let Some(stash) = stash() {
+ for id in &wanted {
+ match stash.read_data_uri(id).await {
+ Ok(Some(data_uri)) => {
+ index.insert(id.clone(), PathBuf::from(data_uri));
+ }
+ Ok(None) => {}
+ Err(err) => tracing::warn!(
+ target: "multimodal",
+ id = %id,
+ reason = %err,
+ "[multimodal::images][stash] could not read attachment; trying legacy sidecars"
+ ),
+ }
+ }
+ }
+ if index.len() < wanted.len() {
+ for (id, path) in legacy_attachment_index() {
+ if wanted.contains(&id) {
+ index.entry(id).or_insert(path);
}
- Ok(None) => {}
- Err(err) => tracing::warn!(
- target: "multimodal",
- id = %id,
- reason = %err,
- "[multimodal::images][stash] could not read attachment; placeholder keeps its text"
- ),
}
}
index
@@ -677,8 +683,11 @@ fn placeholder_ids(text: &str) -> Vec<String> {
/// directory (written before the blob stash). Symlinks and nested entries are
/// ignored.
fn legacy_attachment_index() -> HashMap<String, PathBuf> {
- let dir = attachments_dir();
- let Ok(entries) = std::fs::read_dir(&dir) else {
+ legacy_attachment_index_in(&attachments_dir())
+}
+
+fn legacy_attachment_index_in(dir: &std::path::Path) -> HashMap<String, PathBuf> {
+ let Ok(entries) = std::fs::read_dir(dir) else {
return HashMap::new();
};
entries
diff --git a/crates/openhuman-core/src/agent/multimodal_attachment_handling_tests.rs b/crates/openhuman-core/src/agent/multimodal_attachment_handling_tests.rs
index 834038d4cb..cfb16aa463 100644
--- a/crates/openhuman-core/src/agent/multimodal_attachment_handling_tests.rs
+++ b/crates/openhuman-core/src/agent/multimodal_attachment_handling_tests.rs
@@ -310,3 +310,16 @@ async fn sweep_keeps_fresh_attachments() {
"fresh attachment {id} retained after sweep"
);
}
+
+#[test]
+fn legacy_sidecar_files_stay_indexed_by_id() {
+ // Conversations written before the blob stash keep `<id>.<ext>` files in the
+ // attachments directory; their placeholders must still resolve.
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("abc123.png"), b"png").unwrap();
+ std::fs::create_dir(dir.path().join("nested")).unwrap();
+ std::fs::write(dir.path().join("bad name.png"), b"png").unwrap();
+ let index = legacy_attachment_index_in(dir.path());
+ assert_eq!(index.len(), 1, "{index:?}");
+ assert_eq!(index["abc123"], dir.path().join("abc123.png"));
+}
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index bd3c8da2f6..4824976caf 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -26,9 +26,10 @@ mod workspace;
pub use rebound::{rebind_all, ReboundSessionStores};
pub use workspace::{
- attachment_stash_for, current_documents_for, default_turn_states_for, ledger_for, provider_for_workspace, release_workspace_defaults,
- require_ledger, status_store_for, stores_for, stores_or_ephemeral, turn_states_for, with_ledger,
- with_ledger_async, WorkspaceSessionStores,
+ attachment_stash_for, current_documents_for, default_turn_states_for, ledger_for,
+ provider_for_workspace, release_workspace_defaults, require_ledger, status_store_for,
+ stores_for, stores_or_ephemeral, turn_states_for, with_ledger, with_ledger_async,
+ WorkspaceSessionStores,
};
static PROVIDER: LazyLock<RwLock<Option<Arc<dyn SessionStoreProvider>>>> =
diff --git a/crates/openhuman-core/src/agent/session_store/mod_tests.rs b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
index 84a97d1e45..6c09f57b9f 100644
--- a/crates/openhuman-core/src/agent/session_store/mod_tests.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
@@ -224,5 +224,9 @@ async fn a_stashed_attachment_survives_a_restart_of_the_workspace_store() {
resumed.read_data_uri("att-1").await.unwrap().as_deref(),
Some(data_uri)
);
- assert!(resumed.read_data_uri("never-stashed").await.unwrap().is_none());
+ assert!(resumed
+ .read_data_uri("never-stashed")
+ .await
+ .unwrap()
+ .is_none());
}
diff --git a/crates/openhuman-core/src/flows/ops/streaming.rs b/crates/openhuman-core/src/flows/ops/streaming.rs
index 7be0aeacb2..5c94dc082a 100644
--- a/crates/openhuman-core/src/flows/ops/streaming.rs
+++ b/crates/openhuman-core/src/flows/ops/streaming.rs
@@ -147,9 +147,8 @@ pub(super) async fn finalize_flow_stream(
tinyagents_session::turn_state::TurnLifecycle::Interrupted
};
let now = chrono::Utc::now().to_rfc3339();
- if let Err(err) =
- crate::agent::session_store::turn_states_for(&config.workspace_dir)
- .settle_turn(&target.thread_id, &target.request_id, lifecycle, &now)
+ if let Err(err) = crate::agent::session_store::turn_states_for(&config.workspace_dir)
+ .settle_turn(&target.thread_id, &target.request_id, lifecycle, &now)
{
tracing::warn!(
target: "flows",
diff --git a/crates/openhuman-core/src/threads/ops/crud.rs b/crates/openhuman-core/src/threads/ops/crud.rs
index 1e097ebae1..dcff852b28 100644
--- a/crates/openhuman-core/src/threads/ops/crud.rs
+++ b/crates/openhuman-core/src/threads/ops/crud.rs
@@ -442,12 +442,14 @@ async fn thread_delete_inner(
// mirrors conversation-derived state) remains on disk; the
// thread row itself is already gone at this point so the caller
// sees a partial failure they can act on instead of silent drift.
- crate::agent::session_store::turn_states_for(&dir).delete(&request.thread_id).map_err(|err| {
- format!(
- "thread {} deleted but turn-snapshot cleanup failed: {err}",
- request.thread_id
- )
- })?;
+ crate::agent::session_store::turn_states_for(&dir)
+ .delete(&request.thread_id)
+ .map_err(|err| {
+ format!(
+ "thread {} deleted but turn-snapshot cleanup failed: {err}",
+ request.thread_id
+ )
+ })?;
// The thread's conversation memory goes too, for good (by `memory_ids`).
// Memory off (signed out) or a failed forget queues the deletion for the
// next sign-in (`memory::deletion`) rather than failing the delete: the
diff --git a/crates/openhuman-core/src/threads/ops/edit.rs b/crates/openhuman-core/src/threads/ops/edit.rs
index e3d7fed953..c2490cb53c 100644
--- a/crates/openhuman-core/src/threads/ops/edit.rs
+++ b/crates/openhuman-core/src/threads/ops/edit.rs
@@ -378,8 +378,7 @@ async fn clear_dropped_turn_states(
let thread_id_owned = thread_id.to_string();
let cut_request_id_owned = cut_request_id.to_string();
let result = tokio::task::spawn_blocking(move || {
- let turns =
- store.list_thread(&thread_id_owned)?;
+ let turns = store.list_thread(&thread_id_owned)?;
let Some(cut_started_at) = turns
.iter()
.find(|t| t.request_id == cut_request_id_owned)
diff --git a/crates/openhuman-core/src/threads/ops/edit_turn_state_tests.rs b/crates/openhuman-core/src/threads/ops/edit_turn_state_tests.rs
index 8ea481eb1f..6eafeec902 100644
--- a/crates/openhuman-core/src/threads/ops/edit_turn_state_tests.rs
+++ b/crates/openhuman-core/src/threads/ops/edit_turn_state_tests.rs
@@ -8,8 +8,8 @@
use super::*;
use crate::threads::store::{self as conversations, run_reply_message_id, ConversationMessage};
use serde_json::json;
-use tempfile::TempDir;
use std::sync::Arc;
+use tempfile::TempDir;
use tinyagents_session::port::TurnStates;
use tinyagents_session::turn_state::types::TurnState;
@@ -26,17 +26,21 @@ async fn clear_dropped_turn_states_drops_cut_turn_and_every_later_turn() {
let dir = TempDir::new().expect("tempdir");
let thread_id = "thread-turn-states";
- store(dir.path()).put(&turn_state(thread_id, "req-1", "2026-09-24T00:00:00Z"))
- .expect("put turn 1");
- store(dir.path()).put(&turn_state(thread_id, "req-2", "2026-09-24T00:01:00Z"))
- .expect("put turn 2");
- store(dir.path()).put(&turn_state(thread_id, "req-3", "2026-09-24T00:02:00Z"))
- .expect("put turn 3");
+ store(dir.path())
+ .put(&turn_state(thread_id, "req-1", "2026-09-24T00:00:00Z"))
+ .expect("put turn 1");
+ store(dir.path())
+ .put(&turn_state(thread_id, "req-2", "2026-09-24T00:01:00Z"))
+ .expect("put turn 2");
+ store(dir.path())
+ .put(&turn_state(thread_id, "req-3", "2026-09-24T00:02:00Z"))
+ .expect("put turn 3");
clear_dropped_turn_states(dir.path(), thread_id, "req-2").await;
- let remaining =
- store(dir.path()).list_thread(thread_id).expect("list_thread");
+ let remaining = store(dir.path())
+ .list_thread(thread_id)
+ .expect("list_thread");
let remaining_ids: Vec<&str> = remaining.iter().map(|t| t.request_id.as_str()).collect();
assert_eq!(
remaining_ids,
@@ -51,18 +55,21 @@ async fn clear_dropped_turn_states_is_best_effort_when_cut_turn_never_got_a_snap
let dir = TempDir::new().expect("tempdir");
let thread_id = "thread-turn-states-missing";
- store(dir.path()).put(&turn_state(thread_id, "req-1", "2026-09-24T00:00:00Z"))
- .expect("put turn 1");
- store(dir.path()).put(&turn_state(thread_id, "req-3", "2026-09-24T00:02:00Z"))
- .expect("put turn 3");
+ store(dir.path())
+ .put(&turn_state(thread_id, "req-1", "2026-09-24T00:00:00Z"))
+ .expect("put turn 1");
+ store(dir.path())
+ .put(&turn_state(thread_id, "req-3", "2026-09-24T00:02:00Z"))
+ .expect("put turn 3");
// "req-2" never produced a snapshot (e.g. it errored before its first
// progress event) — nothing to drop but itself, and unrelated turns must
// be left alone.
clear_dropped_turn_states(dir.path(), thread_id, "req-2").await;
- let remaining =
- store(dir.path()).list_thread(thread_id).expect("list_thread");
+ let remaining = store(dir.path())
+ .list_thread(thread_id)
+ .expect("list_thread");
let mut remaining_ids: Vec<&str> = remaining.iter().map(|t| t.request_id.as_str()).collect();
remaining_ids.sort();
assert_eq!(
diff --git a/crates/openhuman-core/src/threads/ops/purge.rs b/crates/openhuman-core/src/threads/ops/purge.rs
index 2890889bc3..5444b656bf 100644
--- a/crates/openhuman-core/src/threads/ops/purge.rs
+++ b/crates/openhuman-core/src/threads/ops/purge.rs
@@ -47,7 +47,8 @@ async fn threads_purge_inner(
// snapshot files (which `list()` would warn-and-skip) are also
// removed — a destructive cleanup must not leave behind anything
// it failed to deserialize. Failures surface as RPC errors.
- crate::agent::session_store::turn_states_for(&dir).clear_all()
+ crate::agent::session_store::turn_states_for(&dir)
+ .clear_all()
.map_err(|err| format!("threads purged but turn-snapshot cleanup failed: {err}"))?;
Ok(envelope(
PurgeConversationThreadsResponse {
diff --git a/crates/openhuman-core/src/threads/ops/turn_state_ops.rs b/crates/openhuman-core/src/threads/ops/turn_state_ops.rs
index 4bc1b47d5a..5137a1c0ce 100644
--- a/crates/openhuman-core/src/threads/ops/turn_state_ops.rs
+++ b/crates/openhuman-core/src/threads/ops/turn_state_ops.rs
@@ -1,13 +1,13 @@
//! RPC accessors over persisted in-flight turn snapshots.
use super::support::{counts, envelope, workspace_dir};
+use crate::agent::session_store::turn_states_for;
use crate::core::Outcome;
use crate::threads::turn_state::{
ClearTurnStateRequest, ClearTurnStateResponse, GetTurnStateForRequestRequest,
GetTurnStateRequest, GetTurnStateResponse, ListTurnStatesResponse,
};
use crate::threads::{ApiEnvelope, EmptyRequest};
-use crate::agent::session_store::turn_states_for;
/// Returns the persisted in-flight turn snapshot for a thread, if any.
pub async fn turn_state_get(
diff --git a/crates/openhuman-core/src/threads/ops_conversion_tests.rs b/crates/openhuman-core/src/threads/ops_conversion_tests.rs
index 65c52ecd70..f01010e650 100644
--- a/crates/openhuman-core/src/threads/ops_conversion_tests.rs
+++ b/crates/openhuman-core/src/threads/ops_conversion_tests.rs
@@ -398,7 +398,12 @@ async fn threads_purge_removes_every_turn_state_snapshot() {
let store = crate::agent::session_store::turn_states_for(&dir);
store.put(&snapshot).expect("put snapshot");
store
- .put(&TurnState::started("thread-b", "req-2", 4, "2026-01-01T00:00:01Z"))
+ .put(&TurnState::started(
+ "thread-b",
+ "req-2",
+ 4,
+ "2026-01-01T00:00:01Z",
+ ))
.expect("put second snapshot");
// Queue background sub-agent results across sessions; a full purge must wipe
diff --git a/crates/openhuman-core/src/threads/turn_state/mirror.rs b/crates/openhuman-core/src/threads/turn_state/mirror.rs
index 98163950a2..e804ac30ef 100644
--- a/crates/openhuman-core/src/threads/turn_state/mirror.rs
+++ b/crates/openhuman-core/src/threads/turn_state/mirror.rs
@@ -24,11 +24,11 @@ mod observe;
pub use observe::ObserveProgress;
-#[cfg(test)]
-use tinyagents_session::turn_state::mirror::MAX_PERSISTED_TRANSCRIPT_ITEM;
#[cfg(test)]
use tinyagents_session::port::{InMemoryTurnStates, TurnStates};
#[cfg(test)]
+use tinyagents_session::turn_state::mirror::MAX_PERSISTED_TRANSCRIPT_ITEM;
+#[cfg(test)]
use tinyagents_session::turn_state::types::{
SubagentToolCall, SubagentTranscriptItem, ToolTimelineStatus, TranscriptItem, TurnLifecycle,
TurnPhase,
diff --git a/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs b/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs
index a7b6e130fc..82cee92e99 100644
--- a/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs
+++ b/crates/openhuman-core/src/threads/turn_state/mirror_observe_tests.rs
@@ -444,7 +444,8 @@ fn text_delta_appends_streaming_text_without_flushing() {
#[test]
fn turn_completed_keeps_snapshot_as_completed_and_finish_is_noop() {
- let store = std::sync::Arc::new(InMemoryTurnStates::default()) as std::sync::Arc<dyn TurnStates>;
+ let store =
+ std::sync::Arc::new(InMemoryTurnStates::default()) as std::sync::Arc<dyn TurnStates>;
let mut mirror = TurnStateMirror::new(store.clone(), "t", "req-1");
mirror.observe(&AgentProgress::TurnCompleted {
iterations: 3,
@@ -467,7 +468,8 @@ fn turn_completed_keeps_snapshot_as_completed_and_finish_is_noop() {
#[test]
fn finish_without_turn_completed_marks_interrupted() {
- let store = std::sync::Arc::new(InMemoryTurnStates::default()) as std::sync::Arc<dyn TurnStates>;
+ let store =
+ std::sync::Arc::new(InMemoryTurnStates::default()) as std::sync::Arc<dyn TurnStates>;
let mut mirror = TurnStateMirror::new(store.clone(), "t", "req-1");
mirror.observe(&AgentProgress::IterationStarted {
iteration: 1,
diff --git a/crates/openhuman-core/src/threads/turn_state/mirror_tests.rs b/crates/openhuman-core/src/threads/turn_state/mirror_tests.rs
index 26b6377e1b..30e9c49c1b 100644
--- a/crates/openhuman-core/src/threads/turn_state/mirror_tests.rs
+++ b/crates/openhuman-core/src/threads/turn_state/mirror_tests.rs
@@ -6,7 +6,8 @@ use tempfile::tempdir;
fn fresh(thread_id: &str) -> (tempfile::TempDir, TurnStateMirror) {
let dir = tempdir().expect("tempdir");
- let store = std::sync::Arc::new(InMemoryTurnStates::default()) as std::sync::Arc<dyn TurnStates>;
+ let store =
+ std::sync::Arc::new(InMemoryTurnStates::default()) as std::sync::Arc<dyn TurnStates>;
let mirror = TurnStateMirror::new(store, thread_id, "req-1");
(dir, mirror)
}
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs b/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
index 9c2ed92435..864a380cc4 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge_tests.rs
@@ -103,7 +103,8 @@ async fn tool_call_completed_forwards_real_output_on_tool_result() {
config_path: tmp.path().join("config.toml"),
..Default::default()
};
- let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default()) as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
+ let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default())
+ as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
let (tx, rx) = tokio::sync::mpsc::channel(16);
let mut bus = super::super::event_bus::subscribe_web_channel_events();
spawn_progress_bridge(
@@ -213,7 +214,8 @@ fn spawn_test_bridge(
) -> tokio::sync::mpsc::Sender<AgentProgress> {
let (tx, rx) = tokio::sync::mpsc::channel::<AgentProgress>(16);
let dir = tempfile::tempdir().expect("tempdir");
- let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default()) as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
+ let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default())
+ as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
// Keep the tempdir alive for the bridge task's lifetime by leaking it —
// a test-only allocation; the OS reclaims it on process exit.
std::mem::forget(dir);
@@ -408,7 +410,8 @@ fn drain_test_config(tmp: &tempfile::TempDir) -> Config {
#[tokio::test]
async fn wait_drained_returns_after_queued_events_are_forwarded() {
let tmp = tempfile::TempDir::new().unwrap();
- let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default()) as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
+ let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default())
+ as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
let (tx, rx) = tokio::sync::mpsc::channel(16);
let mut bus = super::super::event_bus::subscribe_web_channel_events();
let handle = spawn_progress_bridge(
@@ -465,7 +468,8 @@ async fn wait_drained_returns_after_queued_events_are_forwarded() {
#[tokio::test]
async fn wait_drained_is_bounded_when_the_turn_never_completes() {
let tmp = tempfile::TempDir::new().unwrap();
- let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default()) as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
+ let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default())
+ as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
let (tx, rx) = tokio::sync::mpsc::channel::<AgentProgress>(16);
let handle = spawn_progress_bridge(
rx,
@@ -488,7 +492,8 @@ async fn wait_drained_is_bounded_when_the_turn_never_completes() {
#[tokio::test]
async fn short_narration_is_flushed_on_the_rounds_first_tool_call() {
let tmp = tempfile::TempDir::new().unwrap();
- let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default()) as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
+ let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default())
+ as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
let (tx, rx) = tokio::sync::mpsc::channel(16);
let mut bus = super::super::event_bus::subscribe_web_channel_events();
let _handle = spawn_progress_bridge(
@@ -680,7 +685,8 @@ async fn hold_text_stream_suppresses_text_delta_but_keeps_interim() {
config_path: tmp.path().join("config.toml"),
..Default::default()
};
- let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default()) as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
+ let store = std::sync::Arc::new(tinyagents_session::port::InMemoryTurnStates::default())
+ as std::sync::Arc<dyn tinyagents_session::port::TurnStates>;
let (tx, rx) = tokio::sync::mpsc::channel(16);
let mut bus = super::super::event_bus::subscribe_web_channel_events();
spawn_progress_bridge(