Skip to content

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

feat(subagent-sessions): persist scoped lifecycle key in durable sessions

Steven Enamakele88c56f19c32
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs
index 043c668435..9f386758b7 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/storage_schema.rs
@@ -38,7 +38,7 @@ pub(super) fn legacy_collections() -> Vec<CollectionSpec> {
}
/// The camelCase v1 field and its snake_case v2 name, for the plain copies.
-const COPIES: [(&str, &str); 16] = [
+const COPIES: [(&str, &str); 17] = [
("subagentSessionId", "subagent_session_id"),
("parentSession", "parent_session"),
("parentThreadId", "parent_thread_id"),
@@ -51,6 +51,7 @@ const COPIES: [(&str, &str); 16] = [
("taskKey", "task_key"),
("taskTitle", "task_title"),
("currentTaskId", "current_task_id"),
+ ("lifecycleKey", "lifecycle_key"),
("status", "status"),
("reusable", "reusable"),
("latestHistory", "latest_history"),
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
index cbcab9fb6a..72cbe0dfee 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
@@ -60,6 +60,8 @@ fn collections() -> Vec<CollectionSpec> {
/// ([`DurableSubagentSession`]) is camelCase with RFC 3339 strings.
#[derive(Serialize, Deserialize)]
struct StoredSession {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ lifecycle_key: Option<tinyagents_orchestration::subagent::SubagentTaskKey>,
subagent_session_id: String,
parent_session: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -98,6 +100,7 @@ impl From<&DurableSubagentSession> for StoredSession {
let created_ms = rfc3339_ms(&s.created_at).unwrap_or_default();
let updated_ms = rfc3339_ms(&s.updated_at).unwrap_or(created_ms);
Self {
+ lifecycle_key: s.lifecycle_key.clone(),
subagent_session_id: s.subagent_session_id.clone(),
parent_session: s.parent_session.clone(),
parent_thread_id: s.parent_thread_id.clone(),
@@ -124,6 +127,7 @@ impl From<&DurableSubagentSession> for StoredSession {
impl From<StoredSession> for DurableSubagentSession {
fn from(s: StoredSession) -> Self {
Self {
+ lifecycle_key: s.lifecycle_key,
subagent_session_id: s.subagent_session_id,
parent_session: s.parent_session,
parent_thread_id: s.parent_thread_id,
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
index 6998431619..c49ecd7499 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
@@ -210,3 +210,19 @@ fn a_late_importer_rechecks_the_marker_under_the_claim() {
.collect();
assert_eq!(ids, ["first"]);
}
+
+#[test]
+fn a_durable_session_keeps_its_scoped_lifecycle_key_after_document_reopen() {
+ let storage = MemoryStorage::new();
+ let mut saved = session("scoped-lifecycle");
+ let key = tinyagents_orchestration::subagent::SubagentTaskKey {
+ root_run_id: "root".into(),
+ parent_run_id: "parent".into(),
+ thread_id: Some("thread".into()),
+ task_id: "task".into(),
+ };
+ saved.lifecycle_key = Some(key.clone());
+ docs_in(&storage, "local").save(&[saved]).unwrap();
+ let loaded = docs_in(&storage, "local").load().unwrap();
+ assert_eq!(loaded[0].lifecycle_key, Some(key));
+}
diff --git a/crates/openhuman-core/src/agent/subagent_host/lifecycle_store.rs b/crates/openhuman-core/src/agent/subagent_host/lifecycle_store.rs
index 156d13802b..e62cf08819 100644
--- a/crates/openhuman-core/src/agent/subagent_host/lifecycle_store.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/lifecycle_store.rs
@@ -1,18 +1,18 @@
-//! The sub-agent lifecycle records on the storage port.
+//! Migration from retired OpenHuman sub-agent checkpoints.
//!
-//! One record per scoped task key (`record/<key component>` in `tsd.kv`, owner
-//! `subagents`, namespace `lifecycle`) holds the key's pause checkpoint and
-//! its terminal outcome together, so "a terminal closes the pause" is one
-//! compare-and-swap rather than two files under a lock. A second value per task id
-//! (`index/<task component>`) lists the scoped keys that share the id.
+//! The host's former `subagents/lifecycle` KV values and lifecycle JSON
+//! files are compatibility sources. This adapter reads their product fields,
+//! imports missing file records once per backend/scope, and converts them
+//! into TinyAgents' neutral document records without changing source files
+//! or existing KV values.
//!
-//! Before schema v2 these were `<workspace>/.openhuman/subagent_checkpoints/
-//! lifecycle/<component>.{pause,terminal}.json` and `task-<component>.index.json`
-//! guarded by `flock` files. [`LifecycleStore::open`] imports them once per
-//! backend and scope (`tsd.migrations`) and leaves the files in place.
+//! TinyAgents' `DocumentSubagentPersistence` owns active pause/terminal
+//! transitions, compare-and-swap and live-task lookup. The host retains only
+//! legacy schema conversion, product metadata and storage scope selection.
use std::path::{Path, PathBuf};
+#[cfg(test)]
use sha2::{Digest, Sha256};
use tinyagents_orchestration::subagent::{SubagentError, SubagentOutcome, SubagentTaskKey};
@@ -76,6 +76,7 @@ fn failure(error: impl std::fmt::Display) -> SubagentError {
SubagentError::Persistence(format!("{error:#}"))
}
+#[cfg(test)]
fn hex(value: &str) -> String {
value
.as_bytes()
@@ -89,6 +90,7 @@ fn hex(value: &str) -> String {
/// inherit a rendered parent identifier, however; that exceptional form is
/// hashed while keeping every key field in the digest input so distinct
/// lifecycle scopes cannot alias.
+#[cfg(test)]
fn bounded(encoded: String) -> String {
if encoded.len() <= 180 {
encoded
@@ -97,6 +99,7 @@ fn bounded(encoded: String) -> String {
}
}
+#[cfg(test)]
pub(super) fn key_component(key: &SubagentTaskKey) -> String {
bounded(format!(
"{}-{}-{}-{}",
@@ -107,6 +110,7 @@ pub(super) fn key_component(key: &SubagentTaskKey) -> String {
))
}
+#[cfg(test)]
pub(super) fn task_component(task_id: &str) -> String {
bounded(hex(task_id))
}
@@ -129,7 +133,7 @@ fn workspace_of(checkpoint_dir: &Path) -> PathBuf {
checkpoint_dir.to_path_buf()
}
-/// The lifecycle store of one checkpoint directory's workspace.
+/// Legacy migration sources and destination documents in one acting scope.
pub(super) struct LifecycleStore {
kv: StateKv,
documents: std::sync::Arc<dyn tinystoragedrivers::DocumentStore>,
@@ -222,15 +226,18 @@ impl LifecycleStore {
Ok(store)
}
+ #[cfg(test)]
fn record_name(key: &SubagentTaskKey) -> String {
format!("record/{}", key_component(key))
}
+ #[cfg(test)]
fn index_name(task_id: &str) -> String {
format!("index/{}", task_component(task_id))
}
/// The record of `key` (empty when nothing was written).
+ #[cfg(test)]
pub(super) fn record(&self, key: &SubagentTaskKey) -> Result<LifecycleRecord, SubagentError> {
Ok(self
.kv
@@ -241,6 +248,7 @@ impl LifecycleStore {
/// Changes the record of `key` under compare-and-swap; `change` may run
/// more than once, and returns its result and whether it changed the record.
+ #[cfg(test)]
pub(super) fn update<R: Send + 'static>(
&self,
key: &SubagentTaskKey,
@@ -252,6 +260,7 @@ impl LifecycleStore {
}
/// The scoped keys that share `task_id`.
+ #[cfg(test)]
pub(super) fn index_keys(&self, task_id: &str) -> Result<Vec<SubagentTaskKey>, SubagentError> {
Ok(self
.kv
@@ -260,36 +269,6 @@ impl LifecycleStore {
.unwrap_or_default()
.keys)
}
-
- pub(super) fn add_index_key(&self, key: &SubagentTaskKey) -> Result<(), SubagentError> {
- let key = key.clone();
- self.kv
- .update::<TaskIndex, ()>(&Self::index_name(&key.task_id), {
- let key = key.clone();
- move |index| {
- if index.keys.contains(&key) {
- return ((), false);
- }
- index.keys.push(key.clone());
- ((), true)
- }
- })
- .map_err(failure)
- }
-
- pub(super) fn remove_index_key(&self, key: &SubagentTaskKey) -> Result<(), SubagentError> {
- let key = key.clone();
- self.kv
- .update::<TaskIndex, ()>(&Self::index_name(&key.task_id), {
- let key = key.clone();
- move |index| {
- let before = index.keys.len();
- index.keys.retain(|candidate| candidate != &key);
- ((), index.keys.len() != before)
- }
- })
- .map_err(failure)
- }
}
/// Copies the legacy lifecycle files of `dir`, never replacing a record that
@@ -323,7 +302,7 @@ fn import_legacy(kv: &StateKv, dir: &Path) -> anyhow::Result<()> {
super::persistence::OpenHumanPersistence::checkpoint_from_persisted_pause(
persisted,
)
- .map_err(serde::de::Error::custom)
+ .map_err(<serde_json::Error as serde::de::Error>::custom)
});
match checkpoint {
Ok(pause) => {
diff --git a/crates/openhuman-core/src/integrations/mod.rs b/crates/openhuman-core/src/integrations/mod.rs
index 659bb907bd..b39353299e 100644
--- a/crates/openhuman-core/src/integrations/mod.rs
+++ b/crates/openhuman-core/src/integrations/mod.rs
@@ -14,3 +14,8 @@ mod connected_identities;
pub(crate) mod recorded_tools;
pub mod task_sources;
pub use connected_identities::render_connected_identities;
+
+// Product prompt metadata shared by connector discovery and agent surfaces.
+pub use crate::agent::prompts::{
+ ConnectedIntegration, ConnectedIntegrationTool, GatedIntegrationTool, IntegrationConnection,
+};