Skip to content

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

refactor(agent): simplify cross-run journal merge ordering

Steven Enamakel284633395659
diff --git a/crates/openhuman-core/src/agent/tinyagents/journal.rs b/crates/openhuman-core/src/agent/tinyagents/journal.rs
index cba55c2945..9dd736cf88 100644
--- a/crates/openhuman-core/src/agent/tinyagents/journal.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/journal.rs
@@ -272,8 +272,8 @@ pub(crate) async fn read_run_events(
}
/// Read a run and its lineage-linked descendants while retaining each journal
-/// stream's offset order. The stable merge uses timestamps across runs and
-/// places child terminal records before a same-timestamp parent acknowledgement.
+/// stream's offset order. The stable merge uses timestamps across runs while
+/// retaining each stream's event offsets.
pub(crate) async fn read_run_tree_events(
ctx: &DispatchCtx,
root_run_id: &str,
@@ -287,12 +287,14 @@ pub(crate) async fn read_run_tree_events(
let mut run_ids = statuses
.into_iter()
.map(|status| status.run_id.as_str().to_owned())
+ .filter(|run_id| run_id != root_run_id)
.collect::<Vec<_>>();
- if !run_ids.iter().any(|run_id| run_id == root_run_id) {
- run_ids.push(root_run_id.to_owned());
- }
run_ids.sort();
run_ids.dedup();
+ // Keep the parent stream first for equal timestamps. The merge itself
+ // does not invent event-specific precedence; the native collector must
+ // handle terminal facts in either arrival order.
+ run_ids.insert(0, root_run_id.to_owned());
let journal = StoreEventJournal::new(journal_store);
let mut streams = Vec::with_capacity(run_ids.len());
@@ -309,8 +311,8 @@ pub(crate) async fn read_run_tree_events(
}
/// Stable timestamp merge for independent run journals. Offsets are preserved
-/// within every source stream; ties place child lifecycle/terminal facts before
-/// parent acknowledgements and final run completion.
+/// within every source stream; equal timestamps retain source-list order, with
+/// the root run supplied first by `read_run_tree_events`.
pub(crate) fn merge_run_observations(streams: Vec<Vec<AgentObservation>>) -> Vec<AgentObservation> {
let mut cursors = vec![0usize; streams.len()];
let mut merged = Vec::new();
@@ -320,15 +322,10 @@ pub(crate) fn merge_run_observations(streams: Vec<Vec<AgentObservation>>) -> Vec
.enumerate()
.filter_map(|(stream_index, stream)| {
let observation = stream.get(cursors[stream_index])?;
- Some((
- stream_index,
- observation.ts_ms,
- cross_run_event_order(&observation.event),
- observation.offset,
- ))
+ Some((stream_index, observation.ts_ms, observation.offset))
})
- .min_by_key(|(_, ts_ms, event_order, offset)| (*ts_ms, *event_order, *offset));
- let Some((stream_index, _, _, _)) = next else {
+ .min_by_key(|(stream_index, ts_ms, offset)| (*ts_ms, *stream_index, *offset));
+ let Some((stream_index, _, _)) = next else {
break;
};
merged.push(streams[stream_index][cursors[stream_index]].clone());
@@ -337,17 +334,6 @@ pub(crate) fn merge_run_observations(streams: Vec<Vec<AgentObservation>>) -> Vec
merged
}
-fn cross_run_event_order(event: &tinyagents_harness::events::AgentEvent) -> u8 {
- use tinyagents_harness::events::AgentEvent;
- match event {
- AgentEvent::SubAgentRunStarted { .. } => 0,
- AgentEvent::RunCompleted { .. } => 4,
- AgentEvent::SubAgentRunCompleted { .. } => 3,
- AgentEvent::RunFailed { .. } => 2,
- _ => 1,
- }
-}
-
#[cfg(test)]
#[path = "journal_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/core/observability/trace_export_adoption_tests.rs b/crates/openhuman-core/src/core/observability/trace_export_adoption_tests.rs
index 4776feceb3..08dcb145eb 100644
--- a/crates/openhuman-core/src/core/observability/trace_export_adoption_tests.rs
+++ b/crates/openhuman-core/src/core/observability/trace_export_adoption_tests.rs
@@ -247,3 +247,89 @@ fn per_run_trace_snapshots_keep_reused_call_ids_scoped_and_content_gated() {
assert!(snapshot.iter().any(|span| span["name"] == "agent.iteration"));
assert!(snapshot.iter().any(|span| span["name"] == "subagent.iteration"));
}
+
+#[test]
+fn child_native_failure_survives_parent_acknowledgement_at_same_timestamp() {
+ use tinyagents_harness::terminal::{TerminalOutcome, TerminalReason};
+ use tinyagents_harness::observability::trace_export::{SpanKind, SpanStatus};
+
+ let parent = vec![
+ root_observation(
+ 0,
+ 10,
+ AgentEvent::RunStarted {
+ run_id: RunId::new("root-run"),
+ thread_id: None,
+ },
+ ),
+ root_observation(
+ 1,
+ 11,
+ AgentEvent::SubAgentRunStarted {
+ name: "researcher".into(),
+ depth: 1,
+ run_id: RunId::new("child-run"),
+ },
+ ),
+ root_observation(
+ 2,
+ 20,
+ AgentEvent::SubAgentRunCompleted {
+ run_id: RunId::new("child-run"),
+ },
+ ),
+ root_observation(
+ 3,
+ 30,
+ AgentEvent::RunCompleted {
+ run_id: RunId::new("root-run"),
+ outcome: Some(TerminalOutcome::new(TerminalReason::Completed, "done")),
+ },
+ ),
+ ];
+ let child = vec![
+ child_observation(
+ 0,
+ 12,
+ AgentEvent::RunStarted {
+ run_id: RunId::new("child-run"),
+ thread_id: None,
+ },
+ ),
+ child_observation(
+ 1,
+ 20,
+ AgentEvent::RunFailed {
+ run_id: RunId::new("child-run"),
+ error: "child provider failed".into(),
+ outcome: Some(TerminalOutcome::new(
+ TerminalReason::ToolFailed,
+ "child provider failed",
+ )),
+ },
+ ),
+ ];
+ let observations =
+ crate::agent::tinyagents::journal::merge_run_observations(vec![parent, child]);
+ assert!(matches!(
+ observations[3].event,
+ AgentEvent::SubAgentRunCompleted { .. }
+ ));
+ assert!(matches!(observations[4].event, AgentEvent::RunFailed { .. }));
+
+ let mut collector = NativeSpanCollector::new(
+ TraceContext::new("root-trace", None)
+ .with_run_lineage(Some("root-run".into()), None, Some("root-run".into())),
+ );
+ for observation in &observations {
+ collector.record(observation);
+ }
+ collector.finish(30);
+ let child_span = collector
+ .spans()
+ .iter()
+ .find(|span| span.kind == SpanKind::Subagent)
+ .expect("native child span");
+ assert_eq!(child_span.status, SpanStatus::Error);
+ assert_eq!(child_span.attributes["error.message"], "child provider failed");
+}