Skip to content

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

fix(agent_chat): handle empty chat history in agent chat operation

Steven Enamakel4a7babc5e73a
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs b/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs
index b03ad243d7..e9d8190c27 100644
--- a/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs
@@ -71,6 +71,32 @@ pub async fn agent_chat(
.await
}
+/// The log line every successful agent chat turn carries.
+const AGENT_CHAT_COMPLETED_LOG: &str = "agent chat completed";
+
+/// What one agent chat turn produced: its reply and whether it stopped at the
+/// tool-iteration cap rather than finishing.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct AgentChatReply {
+ /// The reply text. When `hit_cap` is set this is the turn's checkpoint.
+ pub text: String,
+ /// The turn paused at its iteration cap (see
+ /// [`OpenHumanSessionHost::last_turn_hit_cap`]).
+ pub hit_cap: bool,
+}
+
+impl AgentChatReply {
+ /// The historical `Outcome<String>` shape, for library callers.
+ pub fn into_outcome(self) -> Outcome<String> {
+ Outcome::single_log(self.text, AGENT_CHAT_COMPLETED_LOG)
+ }
+
+ /// stub
+ pub fn into_rpc_json(self) -> Result<serde_json::Value, String> {
+ self.into_outcome().into_cli_compatible_json()
+ }
+}
+
/// Which session [`agent_chat_for`] builds the turn on.
#[derive(Clone, Copy)]
pub enum AgentChatTarget<'a> {
@@ -390,3 +416,7 @@ pub async fn agent_chat_simple(
Ok(Outcome::single_log(response, "agent simple chat completed"))
}
+
+#[cfg(test)]
+#[path = "agent_chat_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat_tests.rs b/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat_tests.rs
new file mode 100644
index 0000000000..a85d1dbb5f
--- /dev/null
+++ b/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat_tests.rs
@@ -0,0 +1,70 @@
+use super::*;
+use serde_json::json;
+
+/// A turn that finished on its own keeps the historical envelope and adds
+/// `hit_cap: false`; no `checkpoint` key appears.
+#[test]
+fn a_finished_turn_reports_hit_cap_false() {
+ let json = AgentChatReply {
+ text: "done".into(),
+ hit_cap: false,
+ }
+ .into_rpc_json()
+ .expect("serializes");
+
+ assert_eq!(
+ json,
+ json!({ "result": "done", "logs": ["agent chat completed"], "hit_cap": false })
+ );
+}
+
+/// #6958: a capped turn used to look like success to a headless caller. It
+/// now says so, and carries the checkpoint text as its own field.
+#[test]
+fn a_capped_turn_reports_hit_cap_and_the_checkpoint() {
+ let json = AgentChatReply {
+ text: "Changed foo.rs. **Still to do**: bar.rs".into(),
+ hit_cap: true,
+ }
+ .into_rpc_json()
+ .expect("serializes");
+
+ assert_eq!(json["hit_cap"], json!(true));
+ assert_eq!(
+ json["checkpoint"],
+ json!("Changed foo.rs. **Still to do**: bar.rs")
+ );
+}
+
+/// Backward compatibility: existing clients read the reply from `result` (or
+/// peel the envelope with `unwrap_rpc`), and still get the plain string.
+#[test]
+fn the_reply_text_stays_where_existing_clients_read_it() {
+ for hit_cap in [false, true] {
+ let json = AgentChatReply {
+ text: "the reply".into(),
+ hit_cap,
+ }
+ .into_rpc_json()
+ .expect("serializes");
+ assert_eq!(json["result"], json!("the reply"));
+ assert_eq!(json["logs"], json!(["agent chat completed"]));
+ assert_eq!(
+ crate::core::unwrap_rpc(&json),
+ &json!("the reply"),
+ "unwrap_rpc must still land on the reply text"
+ );
+ }
+}
+
+/// The library path (`agent_chat_for`) keeps returning `Outcome<String>`.
+#[test]
+fn the_outcome_keeps_its_historical_shape() {
+ let outcome = AgentChatReply {
+ text: "hi".into(),
+ hit_cap: true,
+ }
+ .into_outcome();
+ assert_eq!(outcome.value, "hi");
+ assert_eq!(outcome.logs, vec!["agent chat completed".to_string()]);
+}