Skip to content

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

fix(desktop): bind confirmations to tool run thread

Steven Enamakel1b0166ba7f5a
diff --git a/crates/openhuman-core/src/desktop/control/confirmation.rs b/crates/openhuman-core/src/desktop/control/confirmation.rs
index 9e2aa89bdf..be9be5691b 100644
--- a/crates/openhuman-core/src/desktop/control/confirmation.rs
+++ b/crates/openhuman-core/src/desktop/control/confirmation.rs
@@ -8,7 +8,6 @@ use serde::Serialize;
use serde_json::{json, Value};
use tinydesktop_bus::names;
-use crate::agent::turn_origin::{self, AgentTurnOrigin};
use crate::config::Config;
const TTL: Duration = Duration::from_secs(10 * 60);
@@ -48,17 +47,8 @@ fn table() -> &'static Mutex<HashMap<String, Pending>> {
TABLE.get_or_init(|| Mutex::new(HashMap::new()))
}
-fn origin_key() -> String {
- match turn_origin::current() {
- Some(AgentTurnOrigin::WebChat { thread_id, .. }) => format!("web:{thread_id}"),
- Some(AgentTurnOrigin::DirectChat) => "direct-chat".to_owned(),
- Some(AgentTurnOrigin::Cli) => "cli".to_owned(),
- _ => "untrusted".to_owned(),
- }
-}
-
/// Capture only the operation and target named by a module confirmation stop.
-pub(super) fn record(app: &str, goal: &str, data: &Value) {
+pub(super) fn record(app: &str, goal: &str, thread_id: &str, data: &Value) {
if data.get("stop").and_then(Value::as_str) != Some("confirmation_required") {
return;
}
@@ -114,7 +104,7 @@ pub(super) fn record(app: &str, goal: &str, data: &Value) {
Pending {
app: app.to_owned(),
goal: goal.to_owned(),
- origin: origin_key(),
+ origin: thread_id.to_owned(),
operation: operation.to_owned(),
target,
target_name: Some(target_name),
@@ -189,7 +179,10 @@ pub async fn confirm(config: &Config, id: &str, approve: bool) -> Result<Value,
}
/// Consume an approved decision before making one continuation call.
-pub(super) fn take_approved(id: &str) -> Result<(String, String), String> {
+pub(super) fn take_approved(id: &str, thread_id: Option<&str>) -> Result<(String, String), String> {
+ let thread_id = thread_id
+ .filter(|thread_id| !thread_id.is_empty())
+ .ok_or("desktop confirmation requires a threaded agent run")?;
let mut guard = table()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
@@ -203,7 +196,7 @@ pub(super) fn take_approved(id: &str) -> Result<(String, String), String> {
if !item.approved {
return Err("desktop action needs the user's explicit confirmation".to_owned());
}
- if item.origin != origin_key() {
+ if item.origin != thread_id {
return Err("desktop confirmation does not match this thread".to_owned());
}
let item = guard.remove(id).expect("checked above");
diff --git a/crates/openhuman-core/src/desktop/control/confirmation_tests.rs b/crates/openhuman-core/src/desktop/control/confirmation_tests.rs
index 21b2f03e60..942d9643e5 100644
--- a/crates/openhuman-core/src/desktop/control/confirmation_tests.rs
+++ b/crates/openhuman-core/src/desktop/control/confirmation_tests.rs
@@ -7,13 +7,14 @@ async fn confirmation_requires_trusted_one_use_decision_for_same_goal() {
record(
"TextEdit",
"make a note",
+ "thread-a",
&json!({
"stop":"confirmation_required", "confirmation_id":id,
"pending":{"operation":"CLICK", "reason":"May save a file",
"target":{"ref_id":"@s1:e2", "name":"Save", "role":"button"}}
}),
);
- assert!(take_approved(&id).is_err());
+ assert!(take_approved(&id, Some("thread-a")).is_err());
let listed = pending();
let item = listed
.iter()
@@ -26,10 +27,18 @@ async fn confirmation_requires_trusted_one_use_decision_for_same_goal() {
let approved = confirm(&config, &id, true).await.unwrap();
assert_eq!(approved["approve"], true);
assert_eq!(
- take_approved(&id).unwrap(),
+ take_approved(&id, None).unwrap_err(),
+ "desktop confirmation requires a threaded agent run"
+ );
+ assert_eq!(
+ take_approved(&id, Some("thread-b")).unwrap_err(),
+ "desktop confirmation does not match this thread"
+ );
+ assert_eq!(
+ take_approved(&id, Some("thread-a")).unwrap(),
("TextEdit".to_owned(), "make a note".to_owned())
);
- assert!(take_approved(&id).is_err());
+ assert!(take_approved(&id, Some("thread-a")).is_err());
}
#[test]
@@ -39,6 +48,7 @@ fn unnamed_target_cannot_be_approved() {
record(
"TextEdit",
"test",
+ "thread-a",
&json!({
"stop":"confirmation_required", "confirmation_id":id,
"pending":{"operation":"CLICK", "target":{"ref_id":"@s1:e2", "role":"button"}}
diff --git a/crates/openhuman-core/src/desktop/control/tools.rs b/crates/openhuman-core/src/desktop/control/tools.rs
index ec51023f64..d5d327fb27 100644
--- a/crates/openhuman-core/src/desktop/control/tools.rs
+++ b/crates/openhuman-core/src/desktop/control/tools.rs
@@ -9,7 +9,7 @@ use tinydesktop_bus::{
names, DesktopResponse, FindRequest, LaunchRequest, ListAppsRequest, ListWindowsRequest,
RefRequest, SnapshotRequest, TypeRequest,
};
-use tinytools::{PermissionLevel, Tool, ToolExposure, ToolResult};
+use tinytools::{PermissionLevel, Tool, ToolCallOptions, ToolExposure, ToolResult, ToolRunContext};
use crate::config::Config;
@@ -201,6 +201,19 @@ impl Tool for DesktopTool {
}
async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
+ self.execute_with_context(args, ToolCallOptions::default(), None)
+ .await
+ }
+
+ async fn execute_with_context(
+ &self,
+ args: Value,
+ _options: ToolCallOptions,
+ context: Option<&dyn ToolRunContext>,
+ ) -> anyhow::Result<ToolResult> {
+ let thread_id = context
+ .and_then(ToolRunContext::thread_id)
+ .filter(|thread_id| !thread_id.is_empty());
if !super::ops::listener_is_loopback() || !super::ops::enabled(&self.config) {
return Ok(ToolResult::error(
"Desktop control is disabled in Connections.",
@@ -317,8 +330,8 @@ impl Tool for DesktopTool {
let (app, goal, continuation) =
if matches!(self.kind, DesktopToolKind::ContinueGoal) {
let id = required(&args, "confirmation_id")?;
- let (app, goal) =
- super::confirmation::take_approved(&id).map_err(anyhow::Error::msg)?;
+ let (app, goal) = super::confirmation::take_approved(&id, thread_id)
+ .map_err(anyhow::Error::msg)?;
(app, goal, Some(json!({"id":id,"approve":true})))
} else {
(required(&args, "app")?, required(&args, "goal")?, None)
@@ -379,7 +392,14 @@ impl Tool for DesktopTool {
Ok(response) if response.ok => {
let data = response.data.unwrap_or(Value::Null);
if let Some((app, goal)) = goal_identity.filter(|_| approvals_enabled) {
- super::confirmation::record(&app, &goal, &data);
+ if data.get("stop").and_then(Value::as_str) == Some("confirmation_required") {
+ let Some(thread_id) = thread_id else {
+ return Ok(ToolResult::error(
+ "desktop confirmation requires a threaded agent run",
+ ));
+ };
+ super::confirmation::record(&app, &goal, thread_id, &data);
+ }
}
let rendered = serde_json::to_string(&data)?;
// A bounded model result; full screenshots are not exposed through this tool.
diff --git a/crates/openhuman-core/src/desktop/control/tools_tests.rs b/crates/openhuman-core/src/desktop/control/tools_tests.rs
index 68264e2496..183a02a707 100644
--- a/crates/openhuman-core/src/desktop/control/tools_tests.rs
+++ b/crates/openhuman-core/src/desktop/control/tools_tests.rs
@@ -40,7 +40,7 @@ fn goal_and_continuation_have_distinct_required_inputs() {
assert!(continuation.parameters_schema()["properties"]
.get("approve")
.is_none());
- assert!(super::super::confirmation::take_approved("unknown").is_err());
+ assert!(super::super::confirmation::take_approved("unknown", Some("thread-a")).is_err());
}
#[tokio::test]