Skip to content

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

refactor(flows): extract shared audit close and route draft promotion through catalog

Steven Enamakel1ad501266e78
diff --git a/crates/openhuman-core/src/flows/caps_host/code.rs b/crates/openhuman-core/src/flows/caps_host/code.rs
index 62afabbf2b..2b307277ae 100644
--- a/crates/openhuman-core/src/flows/caps_host/code.rs
+++ b/crates/openhuman-core/src/flows/caps_host/code.rs
@@ -200,20 +200,7 @@ impl OpenHumanCode {
// Close out the approval audit with the run's success/failure (mirrors
// OpenHumanTools/OpenHumanHttp).
- if let Some(id) = audit_id {
- if let Some(gate) = crate::security::approval::ApprovalGate::try_global() {
- let exec = if outcome.is_ok() {
- crate::security::approval::ExecutionOutcome::Success
- } else {
- crate::security::approval::ExecutionOutcome::Failure
- };
- gate.record_execution(
- &id,
- exec,
- outcome.as_ref().err().map(ToString::to_string).as_deref(),
- );
- }
- }
+ close_audit(audit_id.as_deref(), &outcome);
outcome
}
diff --git a/crates/openhuman-core/src/flows/caps_host/tier.rs b/crates/openhuman-core/src/flows/caps_host/tier.rs
index e694a359e9..c064aa7893 100644
--- a/crates/openhuman-core/src/flows/caps_host/tier.rs
+++ b/crates/openhuman-core/src/flows/caps_host/tier.rs
@@ -6,7 +6,7 @@
//! actually performs the round-trip when a human is needed.
use serde_json::Value;
-use tinyflows::error::{EngineError, Result};
+use super::{EngineError, Result};
use crate::security::{GateDecision, SecurityPolicy};
use tinybox_bus::CommandClass;
@@ -164,6 +164,28 @@ fn tier_config_error(_error: &str) -> EngineError {
))
}
+/// Finish the audit row opened by a gated capability after its side effect
+/// settles. Gate decisions without an audit id remain a no-op.
+pub(crate) fn close_audit<T, E: std::fmt::Display>(
+ audit_id: Option<&str>,
+ result: &std::result::Result<T, E>,
+) {
+ let Some(audit_id) = audit_id else {
+ return;
+ };
+ let Some(gate) = crate::security::approval::ApprovalGate::try_global() else {
+ return;
+ };
+ let (outcome, error) = match result {
+ Ok(_) => (crate::security::approval::ExecutionOutcome::Success, None),
+ Err(error) => (
+ crate::security::approval::ExecutionOutcome::Failure,
+ Some(error.to_string()),
+ ),
+ };
+ gate.record_execution(audit_id, outcome, error.as_deref());
+}
+
fn stricter_tier(
tier: crate::security::AutonomyLevel,
ceiling: crate::security::AutonomyLevel,
diff --git a/crates/openhuman-core/src/flows/ops/drafts.rs b/crates/openhuman-core/src/flows/ops/drafts.rs
index afa90e2cb4..c3e96c4fbf 100644
--- a/crates/openhuman-core/src/flows/ops/drafts.rs
+++ b/crates/openhuman-core/src/flows/ops/drafts.rs
@@ -101,47 +101,46 @@ pub(crate) async fn flows_draft_promote_with_context(
require_approval: Option<bool>,
) -> Result<Outcome<Flow>, String> {
ctx.enter(async move {
- let draft = draft_store::get_draft(config, id)
- .map_err(|e| e.to_string())?
- .ok_or_else(|| format!("draft '{id}' not found"))?;
-
- tracing::debug!(
- target: "flows",
- draft_id = %id,
- promotes_to = draft.flow_id.as_deref().unwrap_or("<new flow>"),
- "[flows] flows_draft_promote: promoting draft through the create/update gates"
- );
-
- let outcome = match &draft.flow_id {
- Some(flow_id) => {
- flows_update_with_context(
- ctx,
- config,
- flow_id,
- Some(draft.name.clone()),
- Some(draft.graph.clone()),
- require_approval,
- None,
- )
- .await?
- }
- None => {
- flows_create_with_context(
- ctx,
- config,
- draft.name.clone(),
- draft.graph.clone(),
- require_approval.unwrap_or(false),
+ let callbacks = super::authoring_host::register(Arc::new(config.clone()), ctx.clone())
+ .await?;
+ let storage = super::catalog_storage::register(ctx, config).await?;
+ let proxy = crate::modules::tinyflows::catalog_proxy(config).await?;
+ let response: tinyflows_bus::catalog::PromoteDraftResponse = proxy
+ .call(
+ tinyflows_bus::catalog::members::PROMOTE_DRAFT,
+ &(
+ tinyflows_bus::catalog::PromoteDraftRequest {
+ storage: storage.storage(),
+ host: callbacks.draft_promotion.clone(),
+ id: id.to_owned(),
+ require_approval,
+ },
+ ),
)
- .await?
- }
- };
+ .await
+ .map_err(|error| error.to_string())?;
- // Only remove the draft once the flow write succeeded.
- if let Err(e) = draft_store::delete_draft(config, id) {
- tracing::warn!(target: "flows", draft_id = %id, error = %e, "[flows] flows_draft_promote: flow saved but draft file could not be removed");
- }
- Ok(outcome)
+ match response {
+ tinyflows_bus::catalog::PromoteDraftResponse::Error(error) => Err(error.message),
+ tinyflows_bus::catalog::PromoteDraftResponse::Ok(result) => {
+ let flow: Flow = crate::flows::store::roundtrip(result.workflow)
+ .map_err(|error| error.to_string())?;
+ let mut logs = result
+ .metadata
+ .get("logs")
+ .and_then(Value::as_array)
+ .into_iter()
+ .flatten()
+ .filter_map(Value::as_str)
+ .map(str::to_owned)
+ .collect::<Vec<_>>();
+ if let Some(warning) = result.cleanup_warning {
+ tracing::warn!(target: "flows", draft_id = %id, %warning, "[flows] promoted draft cleanup failed");
+ logs.push(warning);
+ }
+ Ok(Outcome::new(flow, logs))
+ }
+ }
})
.await
}