Skip to content

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

fix(agent): mirror the read-back transcript in the session store dual-write

shanu215a73615b53
diff --git a/src/openhuman/agent/harness/session/turn/session_io_impl_01_part_02.rs b/src/openhuman/agent/harness/session/turn/session_io_impl_01_part_02.rs
index cd5644fa6d..1f06d6c286 100644
--- a/src/openhuman/agent/harness/session/turn/session_io_impl_01_part_02.rs
+++ b/src/openhuman/agent/harness/session/turn/session_io_impl_01_part_02.rs
@@ -125,7 +125,7 @@ impl Agent {
// (`OPENHUMAN_SESSION_DUAL_WRITE` is a kill switch). Only runs
// after the legacy JSONL append above succeeds; the legacy path
// is primary and untouched (issue #4249, 04.1).
- self.maybe_dual_write_session_store(&path, messages, &meta, turn_usage);
+ self.maybe_dual_write_session_store(&path);
}
Err(err) => {
// Restore the tracked state so a transient failure doesn't make
@@ -150,13 +150,7 @@ impl Agent {
/// chat turn. Records reuse the importer's normalization
/// ([`crate::openhuman::agent::session_import`]) so live and imported records are
/// shape-identical. Reads stay 100% legacy until 04.2.
- fn maybe_dual_write_session_store(
- &self,
- path: &std::path::Path,
- messages: &[ChatMessage],
- meta: &transcript::TranscriptMeta,
- turn_usage: Option<&transcript::TurnUsage>,
- ) {
+ fn maybe_dual_write_session_store(&self, path: &std::path::Path) {
use crate::openhuman::agent::session_import::live;
// Config flag (default ON) gates the mirror; the env kill switch can
@@ -179,26 +173,35 @@ impl Agent {
return;
};
- // Rebuild the exact message shape the importer sees after a JSONL
- // round-trip: attach this turn's usage to the last assistant message so
- // its `openhuman_turn_usage` metadata matches an imported record.
- let mut msgs = messages.to_vec();
- if let Some(usage) = turn_usage {
- if let Some(idx) = msgs.iter().rposition(|m| m.role == "assistant") {
- transcript::attach_turn_usage_metadata(&mut msgs[idx], usage);
- }
- }
- let session_transcript = transcript::SessionTranscript {
- meta: meta.clone(),
- messages: msgs,
- };
let workspace = self.workspace_dir.clone();
+ let path = path.to_path_buf();
log::debug!(
"[session-store] dual-write scheduled stem={stem} workspace={}",
workspace.display()
);
tokio::spawn(async move {
+ // Mirror the exact transcript the shadow reader compares against.
+ // `shadow_read_compare` normalizes `read_transcript(path)` — the
+ // legacy JSONL after its write→read round-trip — so build the store
+ // record from that same read rather than from the in-memory turn.
+ // Reconstructing it by hand let sidecar `extra_metadata` (turn-usage,
+ // tool-failure marker, reasoning) diverge on resumed sessions even
+ // though every message body, id and role matched, which is what the
+ // parity soak flagged as `[session_shadow_read] DIVERGENCE` (#6149).
+ // Read-and-mirror runs here on the same best-effort background task
+ // that already fully rewrites the journal stream; on a read error,
+ // skip the mirror for this turn (legacy stays authoritative).
+ let session_transcript = match transcript::read_transcript(&path) {
+ Ok(t) => t,
+ Err(err) => {
+ log::debug!(
+ "[session-store] dual-write skipped: transcript read-back failed for {}: {err:#}",
+ path.display()
+ );
+ return;
+ }
+ };
if let Err(err) = live::write_live_turn(&workspace, &stem, &session_transcript).await {
log::warn!("[session-store] dual-write failed stem={stem}: {err:#}");
}
diff --git a/src/openhuman/agent/session_import/live_tests.rs b/src/openhuman/agent/session_import/live_tests.rs
index 04a431261b..167ee011c1 100644
--- a/src/openhuman/agent/session_import/live_tests.rs
+++ b/src/openhuman/agent/session_import/live_tests.rs
@@ -21,8 +21,8 @@ use super::live::{
use super::ops::store_root;
use super::types::{JournalMessage, SessionDescriptor, NS_SESSIONS};
use crate::openhuman::agent::harness::session::transcript::{
- attach_turn_usage_metadata, read_transcript, write_transcript, MessageUsage, SessionTranscript,
- TranscriptMeta, TurnUsage,
+ attach_tool_failure_metadata, attach_turn_usage_metadata, read_transcript, write_transcript,
+ MessageUsage, SessionTranscript, TranscriptMeta, TurnUsage,
};
use crate::openhuman::agent::messages::ChatMessage;
use crate::openhuman::inference::provider::ToolCall;
@@ -73,6 +73,26 @@ fn turn_usage() -> TurnUsage {
}
}
+/// A multi-turn session whose messages carry the sidecar `extra_metadata` the
+/// single-turn happy-path fixture cannot express: a system message, a failed
+/// `tool` message tagged with `openhuman_tool_failure`, two assistant turns, and
+/// the trailing assistant that this turn's usage attaches to. The tool-failure
+/// marker is the deterministic asymmetry for #6149 — `write_transcript` strips it
+/// onto the line's top-level `failure`/`failure_detail` fields and the read-back
+/// never restores it to `extra_metadata`, so an in-memory reconstruction keeps a
+/// key the legacy JSONL round-trip has dropped.
+fn rich_base_messages() -> Vec<ChatMessage> {
+ let mut failed_tool = ChatMessage::tool("read_file failed: boom");
+ attach_tool_failure_metadata(&mut failed_tool, Some("boom"));
+ vec![
+ ChatMessage::system("you are the orchestrator"),
+ ChatMessage::user("read the file"),
+ ChatMessage::assistant("calling read_file"),
+ failed_tool,
+ ChatMessage::assistant("done"),
+ ]
+}
+
/// Read the store journal stream back into `JournalMessage`s, mirroring the
/// importer's `journal_readback` helper.
async fn journal_readback(ws: &Path, stream: &str) -> Vec<JournalMessage> {
@@ -223,36 +243,87 @@ async fn shadow_read_roundtrip_matches_legacy() {
let stem = "1719_orchestrator";
let jsonl_path = ws.path().join("session_raw").join(format!("{stem}.jsonl"));
- let base_messages = vec![ChatMessage::user("hi"), ChatMessage::assistant("done")];
+ // A rich session (system + tool-failure + two assistant turns) so the fixture
+ // can actually express the sidecar-metadata asymmetries; a two-message
+ // happy-path turn cannot, which is why the pre-#6149 version passed vacuously.
+ let base_messages = rich_base_messages();
let meta = meta("t-root");
let usage = turn_usage();
- // (1) Legacy authoritative write. (2) Live dual-write into the store.
+ // (1) Legacy authoritative write.
write_transcript(&jsonl_path, &base_messages, &meta, Some(&usage)).expect("legacy write");
+
+ // (2) Live dual-write, mirrored exactly as the fixed
+ // `maybe_dual_write_session_store` does (#6149): the store record IS
+ // `read_transcript(path)` — the legacy JSONL after its write→read round-trip,
+ // the same value the shadow reader compares against — not a hand-rebuilt copy
+ // of the in-memory turn.
+ let mirrored = read_transcript(&jsonl_path).expect("read legacy transcript for mirror");
+ write_live_turn(ws.path(), stem, &mirrored)
+ .await
+ .expect("live dual-write");
+
+ // The legacy reader materializes this SessionTranscript for a resume; the
+ // shadow reader compares it against the store stream.
+ let legacy = read_transcript(&jsonl_path).expect("read legacy transcript");
+ let outcome = shadow_read_compare(ws.path(), stem, &legacy).await;
+ assert_eq!(
+ outcome,
+ ShadowReadOutcome::Match {
+ messages: legacy.messages.len()
+ },
+ "round-tripped shadow read must match the legacy render with no divergence"
+ );
+}
+
+/// Regression guard for #6149. Building the store record from the *in-memory*
+/// turn — the pre-fix `maybe_dual_write_session_store` behaviour — instead of
+/// mirroring `read_transcript` diverges on sidecar `extra_metadata` even though
+/// every message body, id and role is byte-identical. Here the
+/// `openhuman_tool_failure` marker survives on the in-memory tool message while
+/// the legacy JSONL round-trip drops it, so the shadow reader reports a
+/// divergence at that message. The fix mirrors the round-tripped read, which is
+/// why `shadow_read_roundtrip_matches_legacy` above stays a clean `Match`.
+#[tokio::test]
+async fn in_memory_store_reconstruction_diverges_from_legacy_on_sidecar_metadata() {
+ let ws = TempDir::new().expect("tempdir");
+ let stem = "1719_orchestrator";
+ let jsonl_path = ws.path().join("session_raw").join(format!("{stem}.jsonl"));
+
+ let base_messages = rich_base_messages();
+ let meta = meta("t-root");
+ let usage = turn_usage();
+
+ write_transcript(&jsonl_path, &base_messages, &meta, Some(&usage)).expect("legacy write");
+
+ // Pre-fix store construction: clone the in-memory turn, attach usage to the
+ // last assistant, and mirror THAT — never round-tripping through the JSONL,
+ // so the tool-failure marker the round-trip strips is still present.
let mut live_messages = base_messages.clone();
let last_assistant = live_messages
.iter()
.rposition(|m| m.role == "assistant")
.expect("assistant message present");
attach_turn_usage_metadata(&mut live_messages[last_assistant], &usage);
- let store_transcript = SessionTranscript {
+ let reconstructed = SessionTranscript {
meta: meta.clone(),
messages: live_messages,
};
- write_live_turn(ws.path(), stem, &store_transcript)
+ write_live_turn(ws.path(), stem, &reconstructed)
.await
.expect("live dual-write");
- // The legacy reader materializes this SessionTranscript for a resume; the
- // shadow reader compares it against the store stream.
let legacy = read_transcript(&jsonl_path).expect("read legacy transcript");
let outcome = shadow_read_compare(ws.path(), stem, &legacy).await;
- assert_eq!(
- outcome,
- ShadowReadOutcome::Match {
- messages: legacy.messages.len()
- },
- "round-tripped shadow read must match the legacy render with no divergence"
+ assert!(
+ matches!(
+ outcome,
+ ShadowReadOutcome::Divergence {
+ first_diff: Some(_),
+ ..
+ }
+ ),
+ "the in-memory reconstruction must diverge on the dropped tool-failure marker, got {outcome:?}"
);
}