Skip to content

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

fix(agent): restore scalar extra_metadata when a side-channel marker is removed

M3gA-Mindbe75f12b86c7
diff --git a/crates/openhuman-core/src/agent/harness/session/session_dispatch_and_resume_tests.rs b/crates/openhuman-core/src/agent/harness/session/session_dispatch_and_resume_tests.rs
index a520f1918a..a8bedc2344 100644
--- a/crates/openhuman-core/src/agent/harness/session/session_dispatch_and_resume_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/session/session_dispatch_and_resume_tests.rs
@@ -357,64 +357,6 @@ fn seed_resume_from_messages_primes_cached_transcript() {
assert_eq!(cached[2].content, "$80,000");
}
-/// #6282: rows seeded from the conversation log belong to earlier turns and
-/// carry no request id, so persisting them together with the next turn must
-/// not stamp them with that turn's request.
-#[test]
-fn prose_seeded_rows_are_not_restamped_with_the_resuming_request() {
- use crate::agent::harness::session::transcript::{
- append_transcript_turn, read_transcript_display, DisplayRecord,
- };
- use crate::agent::messages::{ChatMessage, ConversationMessage};
-
- let mut agent = build_minimal_agent_with_definition_name(Some("orchestrator"));
- agent
- .seed_resume_from_messages(
- vec![
- ("user".to_string(), "install it".to_string()),
- ("agent".to_string(), "Something went wrong.".to_string()),
- ],
- "what happened?",
- )
- .expect("seed");
- // The resuming turn: its own message, then the seeded prefix absorbed ahead
- // of it and rendered for persistence, exactly as the turn driver does.
- agent.history = vec![ConversationMessage::Chat(ChatMessage::user(
- "what happened?",
- ))];
- agent.absorb_resumed_transcript_prefix();
- let messages = agent.tool_dispatcher.to_provider_messages(&agent.history);
-
- let dir = tempfile::TempDir::new().expect("temp dir");
- let path = dir.path().join("seeded.jsonl");
- append_transcript_turn(
- &path,
- &[],
- &messages,
- &fake_transcript_meta("thr_seeded"),
- None,
- Some("req-now"),
- )
- .expect("persist seeded turn");
-
- let rows: Vec<(String, Option<String>)> = read_transcript_display(&path)
- .expect("display read")
- .records
- .into_iter()
- .filter_map(|record| match record {
- DisplayRecord::Message(m) => Some((m.message.content, m.request_id)),
- _ => None,
- })
- .collect();
- let ids: Vec<Option<&str>> = rows.iter().map(|(_, id)| id.as_deref()).collect();
- assert_eq!(
- ids,
- vec![None, None, None, Some("req-now")],
- "the seeded prefix (its system prompt included) keeps no request id; only \
- this turn's new message takes it: {rows:?}"
- );
-}
-
/// `seed_resume_from_messages` must not stomp the existing context if
/// the agent has already been warmed (in-process session cache hit).
/// Otherwise the cache-miss branch in the web channel would erase
diff --git a/crates/openhuman-core/src/agent/harness/session/session_resume_request_id_tests.rs b/crates/openhuman-core/src/agent/harness/session/session_resume_request_id_tests.rs
new file mode 100644
index 0000000000..af9ae16ce3
--- /dev/null
+++ b/crates/openhuman-core/src/agent/harness/session/session_resume_request_id_tests.rs
@@ -0,0 +1,133 @@
+use super::*;
+
+/// #6282: rows seeded from the conversation log belong to earlier turns and
+/// carry no request id, so persisting them together with the next turn must
+/// not stamp them with that turn's request.
+#[test]
+fn prose_seeded_rows_are_not_restamped_with_the_resuming_request() {
+ use crate::agent::harness::session::transcript::{
+ append_transcript_turn, read_transcript_display, DisplayRecord,
+ };
+ use crate::agent::messages::{ChatMessage, ConversationMessage};
+
+ let mut agent = build_minimal_agent_with_definition_name(Some("orchestrator"));
+ agent
+ .seed_resume_from_messages(
+ vec![
+ ("user".to_string(), "install it".to_string()),
+ ("agent".to_string(), "Something went wrong.".to_string()),
+ ],
+ "what happened?",
+ )
+ .expect("seed");
+ // The resuming turn: its own message, then the seeded prefix absorbed ahead
+ // of it and rendered for persistence, exactly as the turn driver does.
+ agent.history = vec![ConversationMessage::Chat(ChatMessage::user(
+ "what happened?",
+ ))];
+ agent.absorb_resumed_transcript_prefix();
+ let messages = agent.tool_dispatcher.to_provider_messages(&agent.history);
+
+ let dir = tempfile::TempDir::new().expect("temp dir");
+ let path = dir.path().join("seeded.jsonl");
+ append_transcript_turn(
+ &path,
+ &[],
+ &messages,
+ &fake_transcript_meta("thr_seeded"),
+ None,
+ Some("req-now"),
+ )
+ .expect("persist seeded turn");
+
+ let rows: Vec<(String, Option<String>)> = read_transcript_display(&path)
+ .expect("display read")
+ .records
+ .into_iter()
+ .filter_map(|record| match record {
+ DisplayRecord::Message(m) => Some((m.message.content, m.request_id)),
+ _ => None,
+ })
+ .collect();
+ let ids: Vec<Option<&str>> = rows.iter().map(|(_, id)| id.as_deref()).collect();
+ assert_eq!(
+ ids,
+ vec![None, None, None, Some("req-now")],
+ "the seeded prefix (its system prompt included) keeps no request id; only \
+ this turn's new message takes it: {rows:?}"
+ );
+}
+
+/// #6282: a thread transcript written without request ids (a CLI or full-rewrite
+/// transcript) resumed into a request-scoped turn must not have its replayed
+/// rows restamped with the resuming request, which would group every earlier
+/// message into the current turn.
+#[test]
+fn a_resumed_request_less_transcript_is_not_restamped_with_the_resuming_request() {
+ use super::super::transcript::{self, read_transcript_display, DisplayRecord};
+ use crate::agent::messages::{ChatMessage, ConversationMessage};
+
+ let ws = tempfile::TempDir::new().expect("temp workspace");
+ let wsp = ws.path().to_path_buf();
+ let thread_id = "thr_request_less";
+ let path = transcript::resolve_keyed_transcript_path(&wsp, "1700000000_orchestrator")
+ .expect("resolve transcript path");
+ transcript::write_transcript(
+ &path,
+ &[
+ ChatMessage::system("stored prompt"),
+ ChatMessage::user("first question"),
+ ChatMessage::assistant("first answer"),
+ ],
+ &fake_transcript_meta(thread_id),
+ None,
+ )
+ .expect("write transcript");
+
+ let mem: Arc<dyn Memory> = crate::memory::test_support::noop_memory();
+ let mut agent = Agent::builder()
+ .chat_model(Arc::new(MockProvider {
+ responses: Mutex::new(vec![]),
+ }))
+ .tools(vec![Box::new(MockTool)])
+ .memory(mem)
+ .tool_dispatcher(Box::new(NativeToolDispatcher))
+ .workspace_dir(wsp.clone())
+ .build()
+ .expect("agent build should succeed");
+ assert!(agent.seed_resume_from_thread_transcript(thread_id));
+
+ // The resuming turn: its own message, then the replayed prefix absorbed
+ // ahead of it, exactly as the turn driver does.
+ agent.history = vec![ConversationMessage::Chat(ChatMessage::user(
+ "second question",
+ ))];
+ agent.absorb_resumed_transcript_prefix();
+ let messages = agent.tool_dispatcher.to_provider_messages(&agent.history);
+
+ let out = wsp.join("resumed.jsonl");
+ transcript::append_transcript_turn(
+ &out,
+ &[],
+ &messages,
+ &fake_transcript_meta(thread_id),
+ None,
+ Some("req-2"),
+ )
+ .expect("persist resumed turn");
+ let rows: Vec<(String, Option<String>)> = read_transcript_display(&out)
+ .expect("display read")
+ .records
+ .into_iter()
+ .filter_map(|record| match record {
+ DisplayRecord::Message(m) => Some((m.message.content, m.request_id)),
+ _ => None,
+ })
+ .collect();
+ let ids: Vec<Option<&str>> = rows.iter().map(|(_, id)| id.as_deref()).collect();
+ assert_eq!(
+ ids,
+ vec![None, None, None, Some("req-2")],
+ "replayed request-less rows keep no request id; only the resuming turn's row takes it: {rows:?}"
+ );
+}
diff --git a/crates/openhuman-core/src/agent/harness/session/session_tests.rs b/crates/openhuman-core/src/agent/harness/session/session_tests.rs
index 00a3c92fae..6909fd23c0 100644
--- a/crates/openhuman-core/src/agent/harness/session/session_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/session/session_tests.rs
@@ -559,6 +559,8 @@ mod session_builder_and_listener_tests;
mod session_dispatch_and_resume_tests;
#[path = "session_prefix_stability_tests.rs"]
mod session_prefix_stability_tests;
+#[path = "session_resume_request_id_tests.rs"]
+mod session_resume_request_id_tests;
#[path = "session_thread_resume_scoped_tests.rs"]
mod session_thread_resume_scoped_tests;
#[path = "session_thread_resume_tests.rs"]
diff --git a/crates/openhuman-core/src/agent/harness/session/session_thread_resume_tests.rs b/crates/openhuman-core/src/agent/harness/session/session_thread_resume_tests.rs
index aa178be84f..fa2a799cac 100644
--- a/crates/openhuman-core/src/agent/harness/session/session_thread_resume_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/session/session_thread_resume_tests.rs
@@ -704,77 +704,3 @@ async fn a_resumed_transcript_prefix_is_absorbed_into_history() {
],
);
}
-
-/// #6282: a thread transcript written without request ids (a CLI or full-rewrite
-/// transcript) resumed into a request-scoped turn must not have its replayed
-/// rows restamped with the resuming request, which would group every earlier
-/// message into the current turn.
-#[test]
-fn a_resumed_request_less_transcript_is_not_restamped_with_the_resuming_request() {
- use super::super::transcript::{self, read_transcript_display, DisplayRecord};
- use crate::agent::messages::{ChatMessage, ConversationMessage};
-
- let ws = tempfile::TempDir::new().expect("temp workspace");
- let wsp = ws.path().to_path_buf();
- let thread_id = "thr_request_less";
- let path = transcript::resolve_keyed_transcript_path(&wsp, "1700000000_orchestrator")
- .expect("resolve transcript path");
- transcript::write_transcript(
- &path,
- &[
- ChatMessage::system("stored prompt"),
- ChatMessage::user("first question"),
- ChatMessage::assistant("first answer"),
- ],
- &fake_transcript_meta(thread_id),
- None,
- )
- .expect("write transcript");
-
- let mem: Arc<dyn Memory> = crate::memory::test_support::noop_memory();
- let mut agent = Agent::builder()
- .chat_model(Arc::new(MockProvider {
- responses: Mutex::new(vec![]),
- }))
- .tools(vec![Box::new(MockTool)])
- .memory(mem)
- .tool_dispatcher(Box::new(NativeToolDispatcher))
- .workspace_dir(wsp.clone())
- .build()
- .expect("agent build should succeed");
- assert!(agent.seed_resume_from_thread_transcript(thread_id));
-
- // The resuming turn: its own message, then the replayed prefix absorbed
- // ahead of it, exactly as the turn driver does.
- agent.history = vec![ConversationMessage::Chat(ChatMessage::user(
- "second question",
- ))];
- agent.absorb_resumed_transcript_prefix();
- let messages = agent.tool_dispatcher.to_provider_messages(&agent.history);
-
- let out = wsp.join("resumed.jsonl");
- transcript::append_transcript_turn(
- &out,
- &[],
- &messages,
- &fake_transcript_meta(thread_id),
- None,
- Some("req-2"),
- )
- .expect("persist resumed turn");
- let rows: Vec<(String, Option<String>)> = read_transcript_display(&out)
- .expect("display read")
- .records
- .into_iter()
- .filter_map(|record| match record {
- DisplayRecord::Message(m) => Some((m.message.content, m.request_id)),
- _ => None,
- })
- .collect();
- let ids: Vec<Option<&str>> = rows.iter().map(|(_, id)| id.as_deref()).collect();
- assert_eq!(
- ids,
- vec![None, None, None, Some("req-2")],
- "replayed request-less rows keep no request id; only the resuming turn's row takes it: {rows:?}"
- );
-}
diff --git a/crates/openhuman-core/src/agent/harness/session/transcript/metadata.rs b/crates/openhuman-core/src/agent/harness/session/transcript/metadata.rs
index 21bb8809ec..c385f24263 100644
--- a/crates/openhuman-core/src/agent/harness/session/transcript/metadata.rs
+++ b/crates/openhuman-core/src/agent/harness/session/transcript/metadata.rs
@@ -27,31 +27,34 @@ const TOOL_FAILURE_METADATA_KEY: &str = "openhuman_tool_failure";
/// the failure marker.
const REPLAYED_METADATA_KEY: &str = "openhuman_replayed";
+/// Key a non-object `extra_metadata` value is moved under when a side-channel
+/// marker has to be added next to it. Distinct from any caller key, so
+/// [`take_metadata`] can tell the wrap apart from a real object and restore
+/// the original value once the marker is removed.
+const WRAPPED_VALUE_KEY: &str = "openhuman_wrapped_value";
+
/// Insert `value` under `key` in `message.extra_metadata`, wrapping a
-/// non-object value under `"value"` so nothing already there is lost.
+/// non-object value under [`WRAPPED_VALUE_KEY`] so nothing already there is
+/// lost.
fn insert_metadata(message: &mut ChatMessage, key: &str, value: serde_json::Value) {
- match message.extra_metadata.take() {
- Some(serde_json::Value::Object(mut map)) => {
- map.insert(key.to_string(), value);
- message.extra_metadata = Some(serde_json::Value::Object(map));
- }
+ let mut map = match message.extra_metadata.take() {
+ Some(serde_json::Value::Object(map)) => map,
Some(existing) => {
let mut map = serde_json::Map::new();
- map.insert("value".to_string(), existing);
- map.insert(key.to_string(), value);
- message.extra_metadata = Some(serde_json::Value::Object(map));
- }
- None => {
- let mut map = serde_json::Map::new();
- map.insert(key.to_string(), value);
- message.extra_metadata = Some(serde_json::Value::Object(map));
+ map.insert(WRAPPED_VALUE_KEY.to_string(), existing);
+ map
}
- }
+ None => serde_json::Map::new(),
+ };
+ map.insert(key.to_string(), value);
+ message.extra_metadata = Some(serde_json::Value::Object(map));
}
-/// Pop `key` out of a cloned `extra_metadata` map. If removing it emptied the
-/// object, drop `extra_metadata` entirely so a legacy-identical line stays
-/// legacy-identical.
+/// Pop `key` out of a cloned `extra_metadata` map, then undo what adding it
+/// did: an object left empty becomes no `extra_metadata` (a legacy-identical
+/// line stays legacy-identical), and an object holding only a wrapped scalar
+/// becomes that scalar again, so a replayed or failed row persists its
+/// original metadata exactly.
fn take_metadata(extra: &mut Option<serde_json::Value>, key: &str) -> Option<serde_json::Value> {
let serde_json::Value::Object(map) = extra.as_mut()? else {
return None;
@@ -59,6 +62,10 @@ fn take_metadata(extra: &mut Option<serde_json::Value>, key: &str) -> Option<ser
let value = map.remove(key)?;
if map.is_empty() {
*extra = None;
+ } else if map.len() == 1 {
+ if let Some(wrapped) = map.remove(WRAPPED_VALUE_KEY) {
+ *extra = Some(wrapped);
+ }
}
Some(value)
}
diff --git a/crates/openhuman-core/src/agent/harness/session/transcript_thread_and_append_tests.rs b/crates/openhuman-core/src/agent/harness/session/transcript_thread_and_append_tests.rs
index 1b67a47604..a7b39503fa 100644
--- a/crates/openhuman-core/src/agent/harness/session/transcript_thread_and_append_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/session/transcript_thread_and_append_tests.rs
@@ -586,6 +586,47 @@ fn resumed_rows_keep_their_request_id_and_failure_flag() {
);
}
+/// #6282 review: a row whose `extra_metadata` is a scalar keeps that exact
+/// scalar through a resume. Adding the replayed marker next to it must not
+/// leave the value wrapped in an object once the writer strips the marker.
+#[test]
+fn resumed_row_keeps_scalar_extra_metadata() {
+ let dir = TempDir::new().unwrap();
+ let meta = sample_meta();
+
+ let mut noted = ChatMessage::user("noted question");
+ noted.extra_metadata = Some(serde_json::json!("pinned"));
+ let first = dir.path().join("scalar-first.jsonl");
+ AppendHarness::new(first.clone()).turn(
+ &[ChatMessage::system("sys"), noted],
+ &meta,
+ None,
+ Some("req-1"),
+ );
+
+ let resumed = read_transcript(&first).unwrap().messages;
+ let second = dir.path().join("scalar-second.jsonl");
+ AppendHarness::new(second.clone()).turn(&resumed, &meta, None, Some("req-2"));
+
+ let reread = read_transcript(&second).unwrap().messages;
+ assert_eq!(
+ reread[1].extra_metadata.as_ref().map(|value| {
+ // The reader re-attaches the replayed marker in memory; strip it
+ // the way the writer does before comparing the persisted value.
+ let mut value = Some(value.clone());
+ super::super::metadata::take_replayed_request_id(&mut value);
+ value
+ }),
+ Some(Some(serde_json::json!("pinned"))),
+ "the scalar must survive the resume unwrapped"
+ );
+ let raw = fs::read_to_string(&second).unwrap();
+ assert!(
+ raw.contains("\"extra_metadata\":\"pinned\""),
+ "the persisted line must carry the original scalar: {raw}"
+ );
+}
+
/// An interrupted partial is appended to the file, is visible in the display
/// read flagged `interrupted`, and is SKIPPED by the model-context read (a
/// resumed context never carries a truncated answer).