Skip to content

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

fix(agent): make the replayed-metadata wrap marker-recorded and multi-marker safe

M3gA-Mindf27cfd73adcf
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
index 8824a1f7e9..724806cc3f 100644
--- 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
@@ -1,5 +1,87 @@
use super::*;
+/// #6282 review: a resume leaves every row's own `extra_metadata` exactly as it
+/// was — a scalar, a caller object that happens to use the wrapper key, and a
+/// scalar under *two* host markers (a failed tool row that is also replayed,
+/// where removing the first marker must not drop the second) — while each row
+/// keeps the request it was first written under.
+#[test]
+fn resumed_rows_keep_their_own_extra_metadata() {
+ use crate::agent::harness::session::transcript::{
+ append_transcript_turn, attach_tool_failure_metadata, read_transcript,
+ read_transcript_display, DisplayRecord,
+ };
+ use crate::agent::messages::ChatMessage;
+
+ let dir = tempfile::TempDir::new().expect("temp dir");
+ let meta = fake_transcript_meta("thr_metadata");
+
+ let mut noted = ChatMessage::user("noted question");
+ noted.extra_metadata = Some(serde_json::json!("pinned"));
+ let mut collides = ChatMessage::user("caller uses the wrapper key");
+ collides.extra_metadata = Some(serde_json::json!({ "openhuman_wrapped_value": "pinned" }));
+ let mut failed_tool = ChatMessage::tool(r#"{"tool_call_id":"call-1","content":"boom"}"#);
+ failed_tool.extra_metadata = Some(serde_json::json!("tool-note"));
+ attach_tool_failure_metadata(&mut failed_tool, Some("boom"));
+
+ let first = dir.path().join("first.jsonl");
+ append_transcript_turn(
+ &first,
+ &[],
+ &[ChatMessage::system("sys"), noted, collides, failed_tool],
+ &meta,
+ None,
+ Some("req-1"),
+ )
+ .expect("turn 1");
+
+ // Resume into a fresh file: the reader re-attaches each row's provenance,
+ // the writer strips the markers again.
+ let resumed = read_transcript(&first).expect("read").messages;
+ let second = dir.path().join("second.jsonl");
+ append_transcript_turn(&second, &[], &resumed, &meta, None, Some("req-2")).expect("turn 2");
+
+ let rows: Vec<_> = read_transcript_display(&second)
+ .expect("display read")
+ .records
+ .into_iter()
+ .filter_map(|record| match record {
+ DisplayRecord::Message(m) => Some(m),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(
+ rows.iter()
+ .map(|row| row.request_id.as_deref())
+ .collect::<Vec<_>>(),
+ vec![Some("req-1"); 4],
+ "every replayed row keeps the request that wrote it"
+ );
+ assert_eq!(
+ rows[0].message.extra_metadata, None,
+ "a row that had no metadata keeps none"
+ );
+ assert_eq!(
+ rows[1].message.extra_metadata,
+ Some(serde_json::json!("pinned")),
+ "a scalar survives the resume unwrapped"
+ );
+ assert_eq!(
+ rows[2].message.extra_metadata,
+ Some(serde_json::json!({ "openhuman_wrapped_value": "pinned" })),
+ "a caller object using the wrapper key stays an object"
+ );
+ assert_eq!(
+ rows[3].message.extra_metadata,
+ Some(serde_json::json!("tool-note")),
+ "a scalar under both the failure and replay markers survives intact"
+ );
+ assert!(
+ rows[3].failure && rows[3].failure_detail.as_deref() == Some("boom"),
+ "the replayed tool row keeps its failure flag and detail"
+ );
+}
+
/// #6282 review: when a resumed transcript is later reduced, the writer's
/// compaction record carries the full reduced set. Replayed rows inside it must
/// keep the request they were first written under, the same as on a plain
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 16f4439487..bfe02ccfa4 100644
--- a/crates/openhuman-core/src/agent/harness/session/transcript/metadata.rs
+++ b/crates/openhuman-core/src/agent/harness/session/transcript/metadata.rs
@@ -86,8 +86,26 @@ fn take_metadata(extra: &mut Option<serde_json::Value>, key: &str) -> Option<ser
if map.is_empty() {
*extra = None;
} else if wrapped {
- if let Some(original) = map.remove(WRAPPED_VALUE_KEY) {
- *extra = Some(original);
+ // Another marker may still sit beside the wrapped value (a replayed row
+ // that also failed carries both). Restoring now would drop it, so hand
+ // the wrap on and let whichever host marker is removed last restore.
+ match [TOOL_FAILURE_METADATA_KEY, REPLAYED_METADATA_KEY]
+ .into_iter()
+ .find(|remaining| map.contains_key(*remaining))
+ {
+ Some(carrier) => {
+ if let Some(serde_json::Value::Object(marker)) = map.get_mut(carrier) {
+ marker.insert(WRAPPED_FLAG.to_string(), serde_json::Value::Bool(true));
+ }
+ }
+ // Only the wrapped value is left: restore it. Anything else still
+ // there (turn usage) is not ours to strip, so the wrap stays.
+ None if map.len() == 1 => {
+ if let Some(original) = map.remove(WRAPPED_VALUE_KEY) {
+ *extra = Some(original);
+ }
+ }
+ None => {}
}
}
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 67c180c9c8..1b67a47604 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,56 +586,6 @@ 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 once the writer strips the marker — and a caller
-/// object that itself uses the wrapper key is never mistaken for such a wrap.
-#[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 mut collides = ChatMessage::user("caller uses the wrapper key");
- collides.extra_metadata = Some(serde_json::json!({ "openhuman_wrapped_value": "pinned" }));
- let first = dir.path().join("scalar-first.jsonl");
- AppendHarness::new(first.clone()).turn(
- &[ChatMessage::system("sys"), noted, collides],
- &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"));
-
- // The reader re-attaches the replayed marker in memory; strip it the way
- // the writer does before comparing the persisted value.
- let persisted = |message: &ChatMessage| {
- let mut value = message.extra_metadata.clone();
- super::super::metadata::take_replayed_request_id(&mut value);
- value
- };
- let reread = read_transcript(&second).unwrap().messages;
- assert_eq!(
- persisted(&reread[1]),
- Some(serde_json::json!("pinned")),
- "the scalar must survive the resume unwrapped"
- );
- assert_eq!(
- persisted(&reread[2]),
- Some(serde_json::json!({ "openhuman_wrapped_value": "pinned" })),
- "a caller object using the wrapper key must stay an object"
- );
- 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).