Skip to content

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

fix(core): preserve turn origin when scoping a context without one

Steven Enamakel4506d35148d8
diff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rs
index 99295591b1..63eb9e7425 100644
--- a/crates/openhuman-core/src/core/runtime/context.rs
+++ b/crates/openhuman-core/src/core/runtime/context.rs
@@ -531,13 +531,31 @@ impl CoreContext {
/// Run `fut` with `ctx` as the ambient [`CoreContext::current`]: dispatch wraps
/// each handler in this, and multi-tenant hosts pass the tenant's context.
+ ///
+ /// A turn's origin survives a change of context: `ctx` without an origin of
+ /// its own, scoped inside a turn that has one (an embedded agent's derived
+ /// context replacing the caller's, say), keeps that turn's origin. Dropping
+ /// it would turn a labelled turn into an unlabelled one the approval gate
+ /// refuses.
pub async fn scope<F: Future>(ctx: Arc<CoreContext>, fut: F) -> F::Output {
- CURRENT_CONTEXT.scope(ctx, fut).await
+ CURRENT_CONTEXT.scope(Self::keeping_turn_origin(ctx), fut).await
}
/// [`scope`](Self::scope) for a synchronous closure.
pub fn sync_scope<R>(ctx: Arc<CoreContext>, f: impl FnOnce() -> R) -> R {
- CURRENT_CONTEXT.sync_scope(ctx, f)
+ CURRENT_CONTEXT.sync_scope(Self::keeping_turn_origin(ctx), f)
+ }
+
+ /// `ctx`, carrying the origin of the turn it is being scoped inside when it
+ /// has none of its own.
+ fn keeping_turn_origin(ctx: Arc<CoreContext>) -> Arc<CoreContext> {
+ if ctx.turn_origin.is_some() {
+ return ctx;
+ }
+ match Self::scoped_for_turn().and_then(|outer| outer.turn_origin.clone()) {
+ Some(origin) => ctx.derive_channel_turn(None, Some(origin)),
+ None => ctx,
+ }
}
/// Capture the current context now and carry it across a subsequently
diff --git a/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs b/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
index 179eb1f833..b4647b5c67 100644
--- a/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
@@ -217,3 +217,59 @@ async fn scoped_spawn_does_not_invent_missing_turn_authority() {
.await;
assert!(crate::agent::turn_origin::current().is_none());
}
+
+/// Regression: an embedded agent's turn scopes the agent's own derived context
+/// (which carries no origin) inside the turn that the caller labelled. The label
+/// must survive that replacement, or the gate sees an unlabelled turn and refuses
+/// every external-effect tool.
+#[tokio::test]
+async fn a_turn_origin_survives_a_change_of_context() {
+ let outer = ctx("/tmp/origin-survives-outer");
+ let inner = ctx("/tmp/origin-survives-inner");
+ let origin = crate::agent::turn_origin::AgentTurnOrigin::TrustedAutomation {
+ job_id: "flow".into(),
+ source: crate::agent::turn_origin::TrustedAutomationSource::Workflow {
+ require_approval: true,
+ },
+ };
+ CoreContext::scope_with_turn_origin(outer, Some(origin), async {
+ CoreContext::scope(inner.clone(), async {
+ assert!(matches!(
+ CoreContext::current_turn_origin(),
+ Some(crate::agent::turn_origin::AgentTurnOrigin::TrustedAutomation { .. })
+ ));
+ // It is the inner context that is scoped (its own state), not the outer.
+ assert!(CoreContext::current()
+ .unwrap()
+ .workspace_dir()
+ .unwrap()
+ .ends_with("origin-survives-inner"));
+ })
+ .await;
+ CoreContext::sync_scope(inner, || {
+ assert!(CoreContext::current_turn_origin().is_some());
+ });
+ })
+ .await;
+ assert!(CoreContext::current_turn_origin().is_none());
+}
+
+/// An inner context that names its own origin keeps it.
+#[tokio::test]
+async fn a_contexts_own_origin_wins_over_the_enclosing_turns() {
+ let outer = ctx("/tmp/origin-own-outer");
+ let inner = ctx("/tmp/origin-own-inner");
+ let own = crate::agent::turn_origin::AgentTurnOrigin::Cli;
+ let enclosing = crate::agent::turn_origin::AgentTurnOrigin::DirectChat;
+ let inner = inner.derive_channel_turn(None, Some(own));
+ CoreContext::scope_with_turn_origin(outer, Some(enclosing), async {
+ CoreContext::scope(inner, async {
+ assert!(matches!(
+ CoreContext::current_turn_origin(),
+ Some(crate::agent::turn_origin::AgentTurnOrigin::Cli)
+ ));
+ })
+ .await;
+ })
+ .await;
+}