Skip to content

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

fix(codec): handle missing session host codec gracefully

Steven Enamakelf2247ae07250
diff --git a/crates/openhuman-core/src/agent/session_host/codec.rs b/crates/openhuman-core/src/agent/session_host/codec.rs
index b5eaefbc21..56053238ef 100644
--- a/crates/openhuman-core/src/agent/session_host/codec.rs
+++ b/crates/openhuman-core/src/agent/session_host/codec.rs
@@ -4,11 +4,7 @@
//! the conversion to OpenHuman's established wire rows; keeping it here makes
//! the runtime usable by non-OpenHuman hosts without inheriting our metadata.
-use crate::agent::{
- message_convert,
- messages::{chat_message_from_transcript, transcript_message_from_chat},
- tinyagents::host::OpenHumanRunContext,
-};
+use crate::agent::{message_convert, tinyagents::host::OpenHumanRunContext};
use tinyagents_runtime::{RuntimeError, TranscriptCodec, TranscriptTurnOptions};
use tinyagents_session::transcript::view::TOOL_RESULT_FAILURES_METADATA_KEY;
use tinyagents_session::transcript::{
@@ -23,13 +19,7 @@ pub struct OpenHumanTranscriptCodec;
impl TranscriptCodec<OpenHumanRunContext> for OpenHumanTranscriptCodec {
fn decode_history(&self, transcript: &SessionTranscript) -> Result<Vec<Message>, RuntimeError> {
- let rows = transcript
- .messages
- .iter()
- .cloned()
- .map(chat_message_from_transcript)
- .collect::<Vec<_>>();
- Ok(message_convert::history_to_messages(&rows))
+ Ok(message_convert::history_to_messages(&transcript.messages))
}
fn reconcile(
@@ -42,7 +32,6 @@ impl TranscriptCodec<OpenHumanRunContext> for OpenHumanTranscriptCodec {
let mut rows = next
.iter()
.filter_map(message_convert::message_to_native_chat_message)
- .map(|message| transcript_message_from_chat(&message))
.collect::<Vec<_>>();
// `Message` intentionally cannot represent all durable transcript
diff --git a/crates/openhuman-core/src/agent/session_import/projector.rs b/crates/openhuman-core/src/agent/session_import/projector.rs
index 5bd7d771f7..a7351da50c 100644
--- a/crates/openhuman-core/src/agent/session_import/projector.rs
+++ b/crates/openhuman-core/src/agent/session_import/projector.rs
@@ -3,28 +3,107 @@
//!
//! The importer, the live dual-write and the shadow read (all in
//! `tinyagents_session::transcript::import`) take this as their
-//! `JournalProjector`. It goes through the host's `TranscriptMessage` shape so the
-//! journal carries the reconstructed sidecar metadata (`openhuman_turn_usage`,
-//! tool-failure and replay markers) exactly as a transcript read-back does.
+//! `JournalProjector`. The journal record carries the row's typed sidecar
+//! (turn usage, tool-failure and replay markers) as `openhuman_*` keys inside
+//! `extra_metadata`; that key encoding is the journal's wire contract and is
+//! derived here, in one direction only, from the typed fields of the vendor row.
use tinyagents_session::transcript::import::types::JournalMessage;
use tinyagents_session::transcript::TranscriptMessage;
-use tinyagents_session::transcript::TranscriptMessage;
-use crate::agent::messages::{chat_message_from_transcript};
+const TURN_USAGE_METADATA_KEY: &str = "openhuman_turn_usage";
+const TOOL_FAILURE_METADATA_KEY: &str = "openhuman_tool_failure";
+const REPLAYED_METADATA_KEY: &str = "openhuman_replayed";
+const WRAPPED_VALUE_KEY: &str = "openhuman_wrapped_value";
+const WRAPPED_FLAG: &str = "wrapped";
+
+fn would_wrap(extra: &Option<serde_json::Value>) -> bool {
+ matches!(extra, Some(value) if !value.is_object())
+}
+
+/// Insert a marker into the row's metadata. A non-object original is kept
+/// under [`WRAPPED_VALUE_KEY`] so no provider metadata is discarded.
+fn insert_host_metadata(
+ extra: &mut Option<serde_json::Value>,
+ key: &str,
+ value: serde_json::Value,
+) {
+ let mut map = match extra.take() {
+ Some(serde_json::Value::Object(map)) => map,
+ Some(existing) => {
+ let mut map = serde_json::Map::new();
+ map.insert(WRAPPED_VALUE_KEY.to_string(), existing);
+ map
+ }
+ None => serde_json::Map::new(),
+ };
+ map.insert(key.to_string(), value);
+ *extra = Some(serde_json::Value::Object(map));
+}
-impl From<&TranscriptMessage> for JournalMessage {
- fn from(msg: &TranscriptMessage) -> Self {
- Self {
- id: msg.id.clone(),
- role: msg.role.clone(),
- content: msg.content.clone(),
- extra_metadata: msg.extra_metadata.clone(),
+/// The journal `extra_metadata` for one durable row.
+fn journal_extra_metadata(message: &TranscriptMessage) -> Option<serde_json::Value> {
+ let mut extra = message.extra_metadata.clone();
+ if let Some(usage) = message.turn_usage.as_ref() {
+ if let Ok(payload) = serde_json::to_value(usage) {
+ insert_host_metadata(&mut extra, TURN_USAGE_METADATA_KEY, payload);
}
}
+ if let Some(failure) = message
+ .tool_failure
+ .as_ref()
+ .filter(|failure| failure.failed)
+ {
+ let mut payload = serde_json::Map::new();
+ payload.insert("failure".to_string(), serde_json::Value::Bool(true));
+ if let Some(detail) = failure
+ .detail
+ .as_deref()
+ .map(str::trim)
+ .filter(|detail| !detail.is_empty())
+ {
+ payload.insert(
+ "detail".to_string(),
+ serde_json::Value::String(detail.to_string()),
+ );
+ }
+ if would_wrap(&extra) {
+ payload.insert(WRAPPED_FLAG.to_string(), serde_json::Value::Bool(true));
+ }
+ insert_host_metadata(
+ &mut extra,
+ TOOL_FAILURE_METADATA_KEY,
+ serde_json::Value::Object(payload),
+ );
+ }
+ if message.preserve_request_id {
+ let mut payload = serde_json::Map::new();
+ payload.insert(
+ "request_id".to_string(),
+ message
+ .request_id
+ .clone()
+ .map(serde_json::Value::String)
+ .unwrap_or(serde_json::Value::Null),
+ );
+ if would_wrap(&extra) {
+ payload.insert(WRAPPED_FLAG.to_string(), serde_json::Value::Bool(true));
+ }
+ insert_host_metadata(
+ &mut extra,
+ REPLAYED_METADATA_KEY,
+ serde_json::Value::Object(payload),
+ );
+ }
+ extra
}
/// The `JournalProjector` OpenHuman passes to the importer.
pub fn journal_message_from_transcript(message: TranscriptMessage) -> JournalMessage {
- JournalMessage::from(&chat_message_from_transcript(message))
+ JournalMessage {
+ id: message.id.clone(),
+ role: message.role.clone(),
+ content: message.content.clone(),
+ extra_metadata: journal_extra_metadata(&message),
+ }
}